2 * Arche Platform driver to control APB.
4 * Copyright 2014-2015 Google Inc.
5 * Copyright 2014-2015 Linaro Ltd.
7 * Released under the GPLv2 only.
10 #include <linux/clk.h>
11 #include <linux/delay.h>
12 #include <linux/gpio.h>
13 #include <linux/interrupt.h>
14 #include <linux/of_gpio.h>
15 #include <linux/of_irq.h>
16 #include <linux/module.h>
17 #include <linux/pinctrl/consumer.h>
18 #include <linux/platform_device.h>
20 #include <linux/regulator/consumer.h>
21 #include <linux/spinlock.h>
22 #include "arche_platform.h"
25 struct arche_apb_ctrl_drvdata {
26 /* Control GPIO signals to and from AP <=> AP Bridges */
34 enum arche_platform_state state;
37 struct regulator *vcore;
38 struct regulator *vio;
43 struct pinctrl *pinctrl;
44 struct pinctrl_state *pin_default;
48 * Note that these low level api's are active high
50 static inline void deassert_reset(unsigned int gpio)
52 gpio_set_value(gpio, 1);
55 static inline void assert_reset(unsigned int gpio)
57 gpio_set_value(gpio, 0);
61 * Note: Please do not modify the below sequence, as it is as per the spec
63 static int coldboot_seq(struct platform_device *pdev)
65 struct device *dev = &pdev->dev;
66 struct arche_apb_ctrl_drvdata *apb = platform_get_drvdata(pdev);
69 if (apb->init_disabled ||
70 apb->state == ARCHE_PLATFORM_STATE_ACTIVE)
73 /* Hold APB in reset state */
74 assert_reset(apb->resetn_gpio);
76 /* Enable power to APB */
77 if (!IS_ERR(apb->vcore)) {
78 ret = regulator_enable(apb->vcore);
80 dev_err(dev, "failed to enable core regulator\n");
85 if (!IS_ERR(apb->vio)) {
86 ret = regulator_enable(apb->vio);
88 dev_err(dev, "failed to enable IO regulator\n");
93 apb_bootret_deassert(dev);
95 /* On DB3 clock was not mandatory */
96 if (gpio_is_valid(apb->clk_en_gpio))
97 gpio_set_value(apb->clk_en_gpio, 1);
99 usleep_range(100, 200);
101 /* deassert reset to APB : Active-low signal */
102 deassert_reset(apb->resetn_gpio);
104 apb->state = ARCHE_PLATFORM_STATE_ACTIVE;
109 static int fw_flashing_seq(struct platform_device *pdev)
111 struct device *dev = &pdev->dev;
112 struct arche_apb_ctrl_drvdata *apb = platform_get_drvdata(pdev);
115 if (apb->init_disabled ||
116 apb->state == ARCHE_PLATFORM_STATE_FW_FLASHING)
119 ret = regulator_enable(apb->vcore);
121 dev_err(dev, "failed to enable core regulator\n");
125 ret = regulator_enable(apb->vio);
127 dev_err(dev, "failed to enable IO regulator\n");
131 /* for flashing device should be in reset state */
132 assert_reset(apb->resetn_gpio);
133 apb->state = ARCHE_PLATFORM_STATE_FW_FLASHING;
138 static int standby_boot_seq(struct platform_device *pdev)
140 struct arche_apb_ctrl_drvdata *apb = platform_get_drvdata(pdev);
142 if (apb->init_disabled)
145 /* Even if it is in OFF state, then we do not want to change the state */
146 if (apb->state == ARCHE_PLATFORM_STATE_STANDBY ||
147 apb->state == ARCHE_PLATFORM_STATE_OFF)
151 * As per WDM spec, do nothing
153 * Pasted from WDM spec,
154 * - A falling edge on POWEROFF_L is detected (a)
155 * - WDM enters standby mode, but no output signals are changed
158 /* TODO: POWEROFF_L is input to WDM module */
159 apb->state = ARCHE_PLATFORM_STATE_STANDBY;
163 static void poweroff_seq(struct platform_device *pdev)
165 struct arche_apb_ctrl_drvdata *apb = platform_get_drvdata(pdev);
167 if (apb->init_disabled || apb->state == ARCHE_PLATFORM_STATE_OFF)
170 /* disable the clock */
171 if (gpio_is_valid(apb->clk_en_gpio))
172 gpio_set_value(apb->clk_en_gpio, 0);
174 if (!IS_ERR(apb->vcore) && regulator_is_enabled(apb->vcore) > 0)
175 regulator_disable(apb->vcore);
177 if (!IS_ERR(apb->vio) && regulator_is_enabled(apb->vio) > 0)
178 regulator_disable(apb->vio);
180 /* As part of exit, put APB back in reset state */
181 assert_reset(apb->resetn_gpio);
182 apb->state = ARCHE_PLATFORM_STATE_OFF;
184 /* TODO: May have to send an event to SVC about this exit */
187 void apb_bootret_assert(struct device *dev)
189 struct arche_apb_ctrl_drvdata *apb = dev_get_drvdata(dev);
191 gpio_set_value(apb->boot_ret_gpio, 1);
194 void apb_bootret_deassert(struct device *dev)
196 struct arche_apb_ctrl_drvdata *apb = dev_get_drvdata(dev);
198 gpio_set_value(apb->boot_ret_gpio, 0);
201 int apb_ctrl_coldboot(struct device *dev)
203 return coldboot_seq(to_platform_device(dev));
206 int apb_ctrl_fw_flashing(struct device *dev)
208 return fw_flashing_seq(to_platform_device(dev));
211 int apb_ctrl_standby_boot(struct device *dev)
213 return standby_boot_seq(to_platform_device(dev));
216 void apb_ctrl_poweroff(struct device *dev)
218 poweroff_seq(to_platform_device(dev));
221 static ssize_t state_store(struct device *dev,
222 struct device_attribute *attr, const char *buf, size_t count)
224 struct platform_device *pdev = to_platform_device(dev);
225 struct arche_apb_ctrl_drvdata *apb = platform_get_drvdata(pdev);
229 if (sysfs_streq(buf, "off")) {
230 if (apb->state == ARCHE_PLATFORM_STATE_OFF)
234 } else if (sysfs_streq(buf, "active")) {
235 if (apb->state == ARCHE_PLATFORM_STATE_ACTIVE)
239 is_disabled = apb->init_disabled;
240 apb->init_disabled = false;
241 ret = coldboot_seq(pdev);
243 apb->init_disabled = is_disabled;
244 } else if (sysfs_streq(buf, "standby")) {
245 if (apb->state == ARCHE_PLATFORM_STATE_STANDBY)
248 ret = standby_boot_seq(pdev);
249 } else if (sysfs_streq(buf, "fw_flashing")) {
250 if (apb->state == ARCHE_PLATFORM_STATE_FW_FLASHING)
253 /* First we want to make sure we power off everything
254 * and then enter FW flashing state */
256 ret = fw_flashing_seq(pdev);
258 dev_err(dev, "unknown state\n");
262 return ret ? ret : count;
265 static ssize_t state_show(struct device *dev,
266 struct device_attribute *attr, char *buf)
268 struct arche_apb_ctrl_drvdata *apb = dev_get_drvdata(dev);
270 switch (apb->state) {
271 case ARCHE_PLATFORM_STATE_OFF:
272 return sprintf(buf, "off%s\n",
273 apb->init_disabled ? ",disabled" : "");
274 case ARCHE_PLATFORM_STATE_ACTIVE:
275 return sprintf(buf, "active\n");
276 case ARCHE_PLATFORM_STATE_STANDBY:
277 return sprintf(buf, "standby\n");
278 case ARCHE_PLATFORM_STATE_FW_FLASHING:
279 return sprintf(buf, "fw_flashing\n");
281 return sprintf(buf, "unknown state\n");
285 static DEVICE_ATTR_RW(state);
287 static int apb_ctrl_get_devtree_data(struct platform_device *pdev,
288 struct arche_apb_ctrl_drvdata *apb)
290 struct device *dev = &pdev->dev;
291 struct device_node *np = dev->of_node;
294 apb->resetn_gpio = of_get_named_gpio(np, "reset-gpios", 0);
295 if (apb->resetn_gpio < 0) {
296 dev_err(dev, "failed to get reset gpio\n");
297 return apb->resetn_gpio;
299 ret = devm_gpio_request_one(dev, apb->resetn_gpio,
300 GPIOF_OUT_INIT_LOW, "apb-reset");
302 dev_err(dev, "Failed requesting reset gpio %d\n",
307 apb->boot_ret_gpio = of_get_named_gpio(np, "boot-ret-gpios", 0);
308 if (apb->boot_ret_gpio < 0) {
309 dev_err(dev, "failed to get boot retention gpio\n");
310 return apb->boot_ret_gpio;
312 ret = devm_gpio_request_one(dev, apb->boot_ret_gpio,
313 GPIOF_OUT_INIT_LOW, "boot retention");
315 dev_err(dev, "Failed requesting bootret gpio %d\n",
320 /* It's not mandatory to support power management interface */
321 apb->pwroff_gpio = of_get_named_gpio(np, "pwr-off-gpios", 0);
322 if (apb->pwroff_gpio < 0) {
323 dev_err(dev, "failed to get power off gpio\n");
324 return apb->pwroff_gpio;
326 ret = devm_gpio_request_one(dev, apb->pwroff_gpio,
327 GPIOF_IN, "pwroff_n");
329 dev_err(dev, "Failed requesting pwroff_n gpio %d\n",
334 /* Do not make clock mandatory as of now (for DB3) */
335 apb->clk_en_gpio = of_get_named_gpio(np, "clock-en-gpio", 0);
336 if (apb->clk_en_gpio < 0) {
337 dev_warn(dev, "failed to get clock en gpio\n");
338 } else if (gpio_is_valid(apb->clk_en_gpio)) {
339 ret = devm_gpio_request_one(dev, apb->clk_en_gpio,
340 GPIOF_OUT_INIT_LOW, "apb_clk_en");
342 dev_warn(dev, "Failed requesting APB clock en gpio %d\n",
348 apb->pwrdn_gpio = of_get_named_gpio(np, "pwr-down-gpios", 0);
349 if (apb->pwrdn_gpio < 0)
350 dev_warn(dev, "failed to get power down gpio\n");
352 /* Regulators are optional, as we may have fixed supply coming in */
353 apb->vcore = devm_regulator_get(dev, "vcore");
354 if (IS_ERR(apb->vcore))
355 dev_warn(dev, "no core regulator found\n");
357 apb->vio = devm_regulator_get(dev, "vio");
358 if (IS_ERR(apb->vio))
359 dev_warn(dev, "no IO regulator found\n");
361 apb->pinctrl = devm_pinctrl_get(&pdev->dev);
362 if (IS_ERR(apb->pinctrl)) {
363 dev_err(&pdev->dev, "could not get pinctrl handle\n");
364 return PTR_ERR(apb->pinctrl);
366 apb->pin_default = pinctrl_lookup_state(apb->pinctrl, "default");
367 if (IS_ERR(apb->pin_default)) {
368 dev_err(&pdev->dev, "could not get default pin state\n");
369 return PTR_ERR(apb->pin_default);
375 static int arche_apb_ctrl_probe(struct platform_device *pdev)
378 struct arche_apb_ctrl_drvdata *apb;
379 struct device *dev = &pdev->dev;
381 apb = devm_kzalloc(&pdev->dev, sizeof(*apb), GFP_KERNEL);
385 ret = apb_ctrl_get_devtree_data(pdev, apb);
387 dev_err(dev, "failed to get apb devicetree data %d\n", ret);
391 /* Initially set APB to OFF state */
392 apb->state = ARCHE_PLATFORM_STATE_OFF;
393 /* Check whether device needs to be enabled on boot */
394 if (of_property_read_bool(pdev->dev.of_node, "ara,init-disable"))
395 apb->init_disabled = true;
397 platform_set_drvdata(pdev, apb);
399 /* Create sysfs interface to allow user to change state dynamically */
400 ret = device_create_file(dev, &dev_attr_state);
402 dev_err(dev, "failed to create state file in sysfs\n");
406 dev_info(&pdev->dev, "Device registered successfully\n");
410 static int arche_apb_ctrl_remove(struct platform_device *pdev)
412 device_remove_file(&pdev->dev, &dev_attr_state);
414 platform_set_drvdata(pdev, NULL);
419 static int arche_apb_ctrl_suspend(struct device *dev)
422 * If timing profile permits, we may shutdown bridge
427 * Also, need to make sure we meet precondition for unipro suspend
428 * Precondition: Definition ???
433 static int arche_apb_ctrl_resume(struct device *dev)
436 * Atleast for ES2 we have to meet the delay requirement between
437 * unipro switch and AP bridge init, depending on whether bridge is in
438 * OFF state or standby state.
440 * Based on whether bridge is in standby or OFF state we may have to
441 * assert multiple signals. Please refer to WDM spec, for more info.
447 static SIMPLE_DEV_PM_OPS(arche_apb_ctrl_pm_ops, arche_apb_ctrl_suspend,
448 arche_apb_ctrl_resume);
450 static struct of_device_id arche_apb_ctrl_of_match[] = {
451 { .compatible = "usbffff,2", },
455 static struct platform_driver arche_apb_ctrl_device_driver = {
456 .probe = arche_apb_ctrl_probe,
457 .remove = arche_apb_ctrl_remove,
459 .name = "arche-apb-ctrl",
460 .pm = &arche_apb_ctrl_pm_ops,
461 .of_match_table = arche_apb_ctrl_of_match,
465 int __init arche_apb_init(void)
467 return platform_driver_register(&arche_apb_ctrl_device_driver);
470 void __exit arche_apb_exit(void)
472 platform_driver_unregister(&arche_apb_ctrl_device_driver);