2 * Arche Platform driver to enable Unipro link.
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/init.h>
14 #include <linux/module.h>
15 #include <linux/of_gpio.h>
16 #include <linux/of_platform.h>
17 #include <linux/pinctrl/consumer.h>
18 #include <linux/platform_device.h>
20 #include <linux/interrupt.h>
21 #include <linux/irq.h>
22 #include <linux/suspend.h>
23 #include <linux/time.h>
24 #include "arche_platform.h"
27 #if IS_ENABLED(CONFIG_USB_HSIC_USB3613)
28 #include <linux/usb/usb3613.h>
30 static inline int usb3613_hub_mode_ctrl(bool unused)
36 #define WD_COLDBOOT_PULSE_WIDTH_MS 30
38 enum svc_wakedetect_state {
39 WD_STATE_IDLE, /* Default state = pulled high/low */
40 WD_STATE_BOOT_INIT, /* WD = falling edge (low) */
41 WD_STATE_COLDBOOT_TRIG, /* WD = rising edge (high), > 30msec */
42 WD_STATE_STANDBYBOOT_TRIG, /* As of now not used ?? */
43 WD_STATE_COLDBOOT_START, /* Cold boot process started */
44 WD_STATE_STANDBYBOOT_START, /* Not used */
47 struct arche_platform_drvdata {
48 /* Control GPIO signals to and from AP <=> SVC */
52 int wake_detect_gpio; /* bi-dir,maps to WAKE_MOD & WAKE_FRAME signals */
54 enum arche_platform_state state;
57 struct clk *svc_ref_clk;
59 struct pinctrl *pinctrl;
60 struct pinctrl_state *pin_default;
64 enum svc_wakedetect_state wake_detect_state;
66 spinlock_t wake_lock; /* Protect wake_detect_state */
67 struct mutex platform_state_mutex; /* Protect state */
68 unsigned long wake_detect_start;
69 struct notifier_block pm_notifier;
74 /* Requires calling context to hold arche_pdata->platform_state_mutex */
75 static void arche_platform_set_state(struct arche_platform_drvdata *arche_pdata,
76 enum arche_platform_state state)
78 arche_pdata->state = state;
81 /* Requires arche_pdata->wake_lock is held by calling context */
82 static void arche_platform_set_wake_detect_state(
83 struct arche_platform_drvdata *arche_pdata,
84 enum svc_wakedetect_state state)
86 arche_pdata->wake_detect_state = state;
89 static inline void svc_reset_onoff(unsigned int gpio, bool onoff)
91 gpio_set_value(gpio, onoff);
94 static int apb_cold_boot(struct device *dev, void *data)
98 ret = apb_ctrl_coldboot(dev);
100 dev_warn(dev, "failed to coldboot\n");
102 /*Child nodes are independent, so do not exit coldboot operation */
106 static int apb_poweroff(struct device *dev, void *data)
108 apb_ctrl_poweroff(dev);
110 /* Enable HUB3613 into HUB mode. */
111 if (usb3613_hub_mode_ctrl(false))
112 dev_warn(dev, "failed to control hub device\n");
117 static void arche_platform_wd_irq_en(struct arche_platform_drvdata *arche_pdata)
119 /* Enable interrupt here, to read event back from SVC */
120 gpio_direction_input(arche_pdata->wake_detect_gpio);
121 enable_irq(arche_pdata->wake_detect_irq);
124 static irqreturn_t arche_platform_wd_irq_thread(int irq, void *devid)
126 struct arche_platform_drvdata *arche_pdata = devid;
129 spin_lock_irqsave(&arche_pdata->wake_lock, flags);
130 if (arche_pdata->wake_detect_state != WD_STATE_COLDBOOT_TRIG) {
131 /* Something is wrong */
132 spin_unlock_irqrestore(&arche_pdata->wake_lock, flags);
136 arche_platform_set_wake_detect_state(arche_pdata,
137 WD_STATE_COLDBOOT_START);
138 spin_unlock_irqrestore(&arche_pdata->wake_lock, flags);
140 /* It should complete power cycle, so first make sure it is poweroff */
141 device_for_each_child(arche_pdata->dev, NULL, apb_poweroff);
143 /* Bring APB out of reset: cold boot sequence */
144 device_for_each_child(arche_pdata->dev, NULL, apb_cold_boot);
146 /* Enable HUB3613 into HUB mode. */
147 if (usb3613_hub_mode_ctrl(true))
148 dev_warn(arche_pdata->dev, "failed to control hub device\n");
150 spin_lock_irqsave(&arche_pdata->wake_lock, flags);
151 arche_platform_set_wake_detect_state(arche_pdata, WD_STATE_IDLE);
152 spin_unlock_irqrestore(&arche_pdata->wake_lock, flags);
157 static irqreturn_t arche_platform_wd_irq(int irq, void *devid)
159 struct arche_platform_drvdata *arche_pdata = devid;
162 spin_lock_irqsave(&arche_pdata->wake_lock, flags);
164 if (gpio_get_value(arche_pdata->wake_detect_gpio)) {
165 /* wake/detect rising */
168 * If wake/detect line goes high after low, within less than
169 * 30msec, then standby boot sequence is initiated, which is not
170 * supported/implemented as of now. So ignore it.
172 if (arche_pdata->wake_detect_state == WD_STATE_BOOT_INIT) {
173 if (time_before(jiffies,
174 arche_pdata->wake_detect_start +
175 msecs_to_jiffies(WD_COLDBOOT_PULSE_WIDTH_MS))) {
176 arche_platform_set_wake_detect_state(arche_pdata,
180 * Check we are not in middle of irq thread
183 if (arche_pdata->wake_detect_state !=
184 WD_STATE_COLDBOOT_START) {
185 arche_platform_set_wake_detect_state(arche_pdata,
186 WD_STATE_COLDBOOT_TRIG);
187 spin_unlock_irqrestore(
188 &arche_pdata->wake_lock,
190 return IRQ_WAKE_THREAD;
195 /* wake/detect falling */
196 if (arche_pdata->wake_detect_state == WD_STATE_IDLE) {
197 arche_pdata->wake_detect_start = jiffies;
199 * In the beginning, when wake/detect goes low
200 * (first time), we assume it is meant for coldboot
201 * and set the flag. If wake/detect line stays low
202 * beyond 30msec, then it is coldboot else fallback
205 arche_platform_set_wake_detect_state(arche_pdata,
210 spin_unlock_irqrestore(&arche_pdata->wake_lock, flags);
216 * Requires arche_pdata->platform_state_mutex to be held
219 arche_platform_coldboot_seq(struct arche_platform_drvdata *arche_pdata)
223 if (arche_pdata->state == ARCHE_PLATFORM_STATE_ACTIVE)
226 dev_info(arche_pdata->dev, "Booting from cold boot state\n");
228 svc_reset_onoff(arche_pdata->svc_reset_gpio,
229 arche_pdata->is_reset_act_hi);
231 gpio_set_value(arche_pdata->svc_sysboot_gpio, 0);
232 usleep_range(100, 200);
234 ret = clk_prepare_enable(arche_pdata->svc_ref_clk);
236 dev_err(arche_pdata->dev, "failed to enable svc_ref_clk: %d\n",
241 /* bring SVC out of reset */
242 svc_reset_onoff(arche_pdata->svc_reset_gpio,
243 !arche_pdata->is_reset_act_hi);
245 arche_platform_set_state(arche_pdata, ARCHE_PLATFORM_STATE_ACTIVE);
251 * Requires arche_pdata->platform_state_mutex to be held
254 arche_platform_fw_flashing_seq(struct arche_platform_drvdata *arche_pdata)
258 if (arche_pdata->state == ARCHE_PLATFORM_STATE_FW_FLASHING)
261 dev_info(arche_pdata->dev, "Switching to FW flashing state\n");
263 svc_reset_onoff(arche_pdata->svc_reset_gpio,
264 arche_pdata->is_reset_act_hi);
266 gpio_set_value(arche_pdata->svc_sysboot_gpio, 1);
268 usleep_range(100, 200);
270 ret = clk_prepare_enable(arche_pdata->svc_ref_clk);
272 dev_err(arche_pdata->dev, "failed to enable svc_ref_clk: %d\n",
277 svc_reset_onoff(arche_pdata->svc_reset_gpio,
278 !arche_pdata->is_reset_act_hi);
280 arche_platform_set_state(arche_pdata, ARCHE_PLATFORM_STATE_FW_FLASHING);
286 * Requires arche_pdata->platform_state_mutex to be held
289 arche_platform_poweroff_seq(struct arche_platform_drvdata *arche_pdata)
293 if (arche_pdata->state == ARCHE_PLATFORM_STATE_OFF)
296 /* If in fw_flashing mode, then no need to repeate things again */
297 if (arche_pdata->state != ARCHE_PLATFORM_STATE_FW_FLASHING) {
298 disable_irq(arche_pdata->wake_detect_irq);
300 spin_lock_irqsave(&arche_pdata->wake_lock, flags);
301 arche_platform_set_wake_detect_state(arche_pdata,
303 spin_unlock_irqrestore(&arche_pdata->wake_lock, flags);
306 clk_disable_unprepare(arche_pdata->svc_ref_clk);
308 /* As part of exit, put APB back in reset state */
309 svc_reset_onoff(arche_pdata->svc_reset_gpio,
310 arche_pdata->is_reset_act_hi);
312 arche_platform_set_state(arche_pdata, ARCHE_PLATFORM_STATE_OFF);
315 static ssize_t state_store(struct device *dev,
316 struct device_attribute *attr, const char *buf, size_t count)
318 struct platform_device *pdev = to_platform_device(dev);
319 struct arche_platform_drvdata *arche_pdata = platform_get_drvdata(pdev);
322 mutex_lock(&arche_pdata->platform_state_mutex);
324 if (sysfs_streq(buf, "off")) {
325 if (arche_pdata->state == ARCHE_PLATFORM_STATE_OFF)
328 /* If SVC goes down, bring down APB's as well */
329 device_for_each_child(arche_pdata->dev, NULL, apb_poweroff);
331 arche_platform_poweroff_seq(arche_pdata);
333 } else if (sysfs_streq(buf, "active")) {
334 if (arche_pdata->state == ARCHE_PLATFORM_STATE_ACTIVE)
337 /* First we want to make sure we power off everything
338 * and then activate back again
340 device_for_each_child(arche_pdata->dev, NULL, apb_poweroff);
341 arche_platform_poweroff_seq(arche_pdata);
343 arche_platform_wd_irq_en(arche_pdata);
344 ret = arche_platform_coldboot_seq(arche_pdata);
348 } else if (sysfs_streq(buf, "standby")) {
349 if (arche_pdata->state == ARCHE_PLATFORM_STATE_STANDBY)
352 dev_warn(arche_pdata->dev, "standby state not supported\n");
353 } else if (sysfs_streq(buf, "fw_flashing")) {
354 if (arche_pdata->state == ARCHE_PLATFORM_STATE_FW_FLASHING)
358 * Here we only control SVC.
360 * In case of FW_FLASHING mode we do not want to control
361 * APBs, as in case of V2, SPI bus is shared between both
362 * the APBs. So let user chose which APB he wants to flash.
364 arche_platform_poweroff_seq(arche_pdata);
366 ret = arche_platform_fw_flashing_seq(arche_pdata);
370 dev_err(arche_pdata->dev, "unknown state\n");
375 mutex_unlock(&arche_pdata->platform_state_mutex);
376 return ret ? ret : count;
379 static ssize_t state_show(struct device *dev,
380 struct device_attribute *attr, char *buf)
382 struct arche_platform_drvdata *arche_pdata = dev_get_drvdata(dev);
384 switch (arche_pdata->state) {
385 case ARCHE_PLATFORM_STATE_OFF:
386 return sprintf(buf, "off\n");
387 case ARCHE_PLATFORM_STATE_ACTIVE:
388 return sprintf(buf, "active\n");
389 case ARCHE_PLATFORM_STATE_STANDBY:
390 return sprintf(buf, "standby\n");
391 case ARCHE_PLATFORM_STATE_FW_FLASHING:
392 return sprintf(buf, "fw_flashing\n");
394 return sprintf(buf, "unknown state\n");
398 static DEVICE_ATTR_RW(state);
400 static int arche_platform_pm_notifier(struct notifier_block *notifier,
401 unsigned long pm_event, void *unused)
403 struct arche_platform_drvdata *arche_pdata =
404 container_of(notifier, struct arche_platform_drvdata,
406 int ret = NOTIFY_DONE;
408 mutex_lock(&arche_pdata->platform_state_mutex);
410 case PM_SUSPEND_PREPARE:
411 if (arche_pdata->state != ARCHE_PLATFORM_STATE_ACTIVE) {
415 device_for_each_child(arche_pdata->dev, NULL, apb_poweroff);
416 arche_platform_poweroff_seq(arche_pdata);
418 case PM_POST_SUSPEND:
419 if (arche_pdata->state != ARCHE_PLATFORM_STATE_OFF)
422 arche_platform_wd_irq_en(arche_pdata);
423 arche_platform_coldboot_seq(arche_pdata);
428 mutex_unlock(&arche_pdata->platform_state_mutex);
433 static int arche_platform_probe(struct platform_device *pdev)
435 struct arche_platform_drvdata *arche_pdata;
436 struct device *dev = &pdev->dev;
437 struct device_node *np = dev->of_node;
440 arche_pdata = devm_kzalloc(&pdev->dev, sizeof(*arche_pdata),
445 /* setup svc reset gpio */
446 arche_pdata->is_reset_act_hi = of_property_read_bool(np,
447 "svc,reset-active-high");
448 arche_pdata->svc_reset_gpio = of_get_named_gpio(np,
451 if (arche_pdata->svc_reset_gpio < 0) {
452 dev_err(dev, "failed to get reset-gpio\n");
453 return arche_pdata->svc_reset_gpio;
455 ret = devm_gpio_request(dev, arche_pdata->svc_reset_gpio, "svc-reset");
457 dev_err(dev, "failed to request svc-reset gpio:%d\n", ret);
460 ret = gpio_direction_output(arche_pdata->svc_reset_gpio,
461 arche_pdata->is_reset_act_hi);
463 dev_err(dev, "failed to set svc-reset gpio dir:%d\n", ret);
466 arche_platform_set_state(arche_pdata, ARCHE_PLATFORM_STATE_OFF);
468 arche_pdata->svc_sysboot_gpio = of_get_named_gpio(np,
469 "svc,sysboot-gpio", 0);
470 if (arche_pdata->svc_sysboot_gpio < 0) {
471 dev_err(dev, "failed to get sysboot gpio\n");
472 return arche_pdata->svc_sysboot_gpio;
474 ret = devm_gpio_request(dev, arche_pdata->svc_sysboot_gpio, "sysboot0");
476 dev_err(dev, "failed to request sysboot0 gpio:%d\n", ret);
479 ret = gpio_direction_output(arche_pdata->svc_sysboot_gpio, 0);
481 dev_err(dev, "failed to set svc-reset gpio dir:%d\n", ret);
485 /* setup the clock request gpio first */
486 arche_pdata->svc_refclk_req = of_get_named_gpio(np,
487 "svc,refclk-req-gpio", 0);
488 if (arche_pdata->svc_refclk_req < 0) {
489 dev_err(dev, "failed to get svc clock-req gpio\n");
490 return arche_pdata->svc_refclk_req;
492 ret = devm_gpio_request(dev, arche_pdata->svc_refclk_req,
495 dev_err(dev, "failed to request svc-clk-req gpio: %d\n", ret);
498 ret = gpio_direction_input(arche_pdata->svc_refclk_req);
500 dev_err(dev, "failed to set svc-clk-req gpio dir :%d\n", ret);
504 /* setup refclk2 to follow the pin */
505 arche_pdata->svc_ref_clk = devm_clk_get(dev, "svc_ref_clk");
506 if (IS_ERR(arche_pdata->svc_ref_clk)) {
507 ret = PTR_ERR(arche_pdata->svc_ref_clk);
508 dev_err(dev, "failed to get svc_ref_clk: %d\n", ret);
512 platform_set_drvdata(pdev, arche_pdata);
514 arche_pdata->num_apbs = of_get_child_count(np);
515 dev_dbg(dev, "Number of APB's available - %d\n", arche_pdata->num_apbs);
517 arche_pdata->wake_detect_gpio = of_get_named_gpio(np,
518 "svc,wake-detect-gpio",
520 if (arche_pdata->wake_detect_gpio < 0) {
521 dev_err(dev, "failed to get wake detect gpio\n");
522 return arche_pdata->wake_detect_gpio;
525 ret = devm_gpio_request(dev, arche_pdata->wake_detect_gpio,
528 dev_err(dev, "Failed requesting wake_detect gpio %d\n",
529 arche_pdata->wake_detect_gpio);
533 arche_platform_set_wake_detect_state(arche_pdata, WD_STATE_IDLE);
535 arche_pdata->dev = &pdev->dev;
537 spin_lock_init(&arche_pdata->wake_lock);
538 mutex_init(&arche_pdata->platform_state_mutex);
539 arche_pdata->wake_detect_irq =
540 gpio_to_irq(arche_pdata->wake_detect_gpio);
542 ret = devm_request_threaded_irq(dev, arche_pdata->wake_detect_irq,
543 arche_platform_wd_irq,
544 arche_platform_wd_irq_thread,
545 IRQF_TRIGGER_FALLING |
546 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
547 dev_name(dev), arche_pdata);
549 dev_err(dev, "failed to request wake detect IRQ %d\n", ret);
552 disable_irq(arche_pdata->wake_detect_irq);
554 ret = device_create_file(dev, &dev_attr_state);
556 dev_err(dev, "failed to create state file in sysfs\n");
560 ret = of_platform_populate(np, NULL, NULL, dev);
562 dev_err(dev, "failed to populate child nodes %d\n", ret);
563 goto err_device_remove;
566 arche_pdata->pm_notifier.notifier_call = arche_platform_pm_notifier;
567 ret = register_pm_notifier(&arche_pdata->pm_notifier);
570 dev_err(dev, "failed to register pm notifier %d\n", ret);
571 goto err_device_remove;
574 /* Explicitly power off if requested */
575 if (!of_property_read_bool(pdev->dev.of_node, "arche,init-off")) {
576 mutex_lock(&arche_pdata->platform_state_mutex);
577 ret = arche_platform_coldboot_seq(arche_pdata);
579 dev_err(dev, "Failed to cold boot svc %d\n", ret);
582 arche_platform_wd_irq_en(arche_pdata);
583 mutex_unlock(&arche_pdata->platform_state_mutex);
586 dev_info(dev, "Device registered successfully\n");
590 mutex_unlock(&arche_pdata->platform_state_mutex);
592 device_remove_file(&pdev->dev, &dev_attr_state);
596 static int arche_remove_child(struct device *dev, void *unused)
598 struct platform_device *pdev = to_platform_device(dev);
600 platform_device_unregister(pdev);
605 static int arche_platform_remove(struct platform_device *pdev)
607 struct arche_platform_drvdata *arche_pdata = platform_get_drvdata(pdev);
609 unregister_pm_notifier(&arche_pdata->pm_notifier);
610 device_remove_file(&pdev->dev, &dev_attr_state);
611 device_for_each_child(&pdev->dev, NULL, arche_remove_child);
612 arche_platform_poweroff_seq(arche_pdata);
614 if (usb3613_hub_mode_ctrl(false))
615 dev_warn(arche_pdata->dev, "failed to control hub device\n");
616 /* TODO: Should we do anything more here ?? */
620 static __maybe_unused int arche_platform_suspend(struct device *dev)
623 * If timing profile premits, we may shutdown bridge
628 * Also, need to make sure we meet precondition for unipro suspend
629 * Precondition: Definition ???
634 static __maybe_unused int arche_platform_resume(struct device *dev)
637 * Atleast for ES2 we have to meet the delay requirement between
638 * unipro switch and AP bridge init, depending on whether bridge is in
639 * OFF state or standby state.
641 * Based on whether bridge is in standby or OFF state we may have to
642 * assert multiple signals. Please refer to WDM spec, for more info.
648 static void arche_platform_shutdown(struct platform_device *pdev)
650 struct arche_platform_drvdata *arche_pdata = platform_get_drvdata(pdev);
652 arche_platform_poweroff_seq(arche_pdata);
654 usb3613_hub_mode_ctrl(false);
657 static SIMPLE_DEV_PM_OPS(arche_platform_pm_ops,
658 arche_platform_suspend,
659 arche_platform_resume);
661 static const struct of_device_id arche_platform_of_match[] = {
662 /* Use PID/VID of SVC device */
663 { .compatible = "google,arche-platform", },
667 static const struct of_device_id arche_combined_id[] = {
668 /* Use PID/VID of SVC device */
669 { .compatible = "google,arche-platform", },
670 { .compatible = "usbffff,2", },
673 MODULE_DEVICE_TABLE(of, arche_combined_id);
675 static struct platform_driver arche_platform_device_driver = {
676 .probe = arche_platform_probe,
677 .remove = arche_platform_remove,
678 .shutdown = arche_platform_shutdown,
680 .name = "arche-platform-ctrl",
681 .pm = &arche_platform_pm_ops,
682 .of_match_table = arche_platform_of_match,
686 static int __init arche_init(void)
690 retval = platform_driver_register(&arche_platform_device_driver);
694 retval = arche_apb_init();
696 platform_driver_unregister(&arche_platform_device_driver);
700 module_init(arche_init);
702 static void __exit arche_exit(void)
705 platform_driver_unregister(&arche_platform_device_driver);
707 module_exit(arche_exit);
709 MODULE_LICENSE("GPL v2");
710 MODULE_AUTHOR("Vaibhav Hiremath <vaibhav.hiremath@linaro.org>");
711 MODULE_DESCRIPTION("Arche Platform Driver");