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,
179 /* Check we are not in middle of irq thread already */
180 if (arche_pdata->wake_detect_state !=
181 WD_STATE_COLDBOOT_START) {
182 arche_platform_set_wake_detect_state(arche_pdata,
183 WD_STATE_COLDBOOT_TRIG);
184 spin_unlock_irqrestore(
185 &arche_pdata->wake_lock,
187 return IRQ_WAKE_THREAD;
192 /* wake/detect falling */
193 if (arche_pdata->wake_detect_state == WD_STATE_IDLE) {
194 arche_pdata->wake_detect_start = jiffies;
196 * In the begining, when wake/detect goes low
197 * (first time), we assume it is meant for coldboot
198 * and set the flag. If wake/detect line stays low
199 * beyond 30msec, then it is coldboot else fallback
202 arche_platform_set_wake_detect_state(arche_pdata,
207 spin_unlock_irqrestore(&arche_pdata->wake_lock, flags);
213 * Requires arche_pdata->platform_state_mutex to be held
216 arche_platform_coldboot_seq(struct arche_platform_drvdata *arche_pdata)
220 if (arche_pdata->state == ARCHE_PLATFORM_STATE_ACTIVE)
223 dev_info(arche_pdata->dev, "Booting from cold boot state\n");
225 svc_reset_onoff(arche_pdata->svc_reset_gpio,
226 arche_pdata->is_reset_act_hi);
228 gpio_set_value(arche_pdata->svc_sysboot_gpio, 0);
229 usleep_range(100, 200);
231 ret = clk_prepare_enable(arche_pdata->svc_ref_clk);
233 dev_err(arche_pdata->dev, "failed to enable svc_ref_clk: %d\n",
238 /* bring SVC out of reset */
239 svc_reset_onoff(arche_pdata->svc_reset_gpio,
240 !arche_pdata->is_reset_act_hi);
242 arche_platform_set_state(arche_pdata, ARCHE_PLATFORM_STATE_ACTIVE);
248 * Requires arche_pdata->platform_state_mutex to be held
251 arche_platform_fw_flashing_seq(struct arche_platform_drvdata *arche_pdata)
255 if (arche_pdata->state == ARCHE_PLATFORM_STATE_FW_FLASHING)
258 dev_info(arche_pdata->dev, "Switching to FW flashing state\n");
260 svc_reset_onoff(arche_pdata->svc_reset_gpio,
261 arche_pdata->is_reset_act_hi);
263 gpio_set_value(arche_pdata->svc_sysboot_gpio, 1);
265 usleep_range(100, 200);
267 ret = clk_prepare_enable(arche_pdata->svc_ref_clk);
269 dev_err(arche_pdata->dev, "failed to enable svc_ref_clk: %d\n",
274 svc_reset_onoff(arche_pdata->svc_reset_gpio,
275 !arche_pdata->is_reset_act_hi);
277 arche_platform_set_state(arche_pdata, ARCHE_PLATFORM_STATE_FW_FLASHING);
283 * Requires arche_pdata->platform_state_mutex to be held
286 arche_platform_poweroff_seq(struct arche_platform_drvdata *arche_pdata)
290 if (arche_pdata->state == ARCHE_PLATFORM_STATE_OFF)
293 /* If in fw_flashing mode, then no need to repeate things again */
294 if (arche_pdata->state != ARCHE_PLATFORM_STATE_FW_FLASHING) {
295 disable_irq(arche_pdata->wake_detect_irq);
297 spin_lock_irqsave(&arche_pdata->wake_lock, flags);
298 arche_platform_set_wake_detect_state(arche_pdata,
300 spin_unlock_irqrestore(&arche_pdata->wake_lock, flags);
303 clk_disable_unprepare(arche_pdata->svc_ref_clk);
305 /* As part of exit, put APB back in reset state */
306 svc_reset_onoff(arche_pdata->svc_reset_gpio,
307 arche_pdata->is_reset_act_hi);
309 arche_platform_set_state(arche_pdata, ARCHE_PLATFORM_STATE_OFF);
312 static ssize_t state_store(struct device *dev,
313 struct device_attribute *attr, const char *buf, size_t count)
315 struct platform_device *pdev = to_platform_device(dev);
316 struct arche_platform_drvdata *arche_pdata = platform_get_drvdata(pdev);
319 mutex_lock(&arche_pdata->platform_state_mutex);
321 if (sysfs_streq(buf, "off")) {
322 if (arche_pdata->state == ARCHE_PLATFORM_STATE_OFF)
325 /* If SVC goes down, bring down APB's as well */
326 device_for_each_child(arche_pdata->dev, NULL, apb_poweroff);
328 arche_platform_poweroff_seq(arche_pdata);
330 } else if (sysfs_streq(buf, "active")) {
331 if (arche_pdata->state == ARCHE_PLATFORM_STATE_ACTIVE)
334 /* First we want to make sure we power off everything
335 * and then activate back again
337 device_for_each_child(arche_pdata->dev, NULL, apb_poweroff);
338 arche_platform_poweroff_seq(arche_pdata);
340 arche_platform_wd_irq_en(arche_pdata);
341 ret = arche_platform_coldboot_seq(arche_pdata);
345 } else if (sysfs_streq(buf, "standby")) {
346 if (arche_pdata->state == ARCHE_PLATFORM_STATE_STANDBY)
349 dev_warn(arche_pdata->dev, "standby state not supported\n");
350 } else if (sysfs_streq(buf, "fw_flashing")) {
351 if (arche_pdata->state == ARCHE_PLATFORM_STATE_FW_FLASHING)
355 * Here we only control SVC.
357 * In case of FW_FLASHING mode we do not want to control
358 * APBs, as in case of V2, SPI bus is shared between both
359 * the APBs. So let user chose which APB he wants to flash.
361 arche_platform_poweroff_seq(arche_pdata);
363 ret = arche_platform_fw_flashing_seq(arche_pdata);
367 dev_err(arche_pdata->dev, "unknown state\n");
372 mutex_unlock(&arche_pdata->platform_state_mutex);
373 return ret ? ret : count;
376 static ssize_t state_show(struct device *dev,
377 struct device_attribute *attr, char *buf)
379 struct arche_platform_drvdata *arche_pdata = dev_get_drvdata(dev);
381 switch (arche_pdata->state) {
382 case ARCHE_PLATFORM_STATE_OFF:
383 return sprintf(buf, "off\n");
384 case ARCHE_PLATFORM_STATE_ACTIVE:
385 return sprintf(buf, "active\n");
386 case ARCHE_PLATFORM_STATE_STANDBY:
387 return sprintf(buf, "standby\n");
388 case ARCHE_PLATFORM_STATE_FW_FLASHING:
389 return sprintf(buf, "fw_flashing\n");
391 return sprintf(buf, "unknown state\n");
395 static DEVICE_ATTR_RW(state);
397 static int arche_platform_pm_notifier(struct notifier_block *notifier,
398 unsigned long pm_event, void *unused)
400 struct arche_platform_drvdata *arche_pdata =
401 container_of(notifier, struct arche_platform_drvdata,
403 int ret = NOTIFY_DONE;
405 mutex_lock(&arche_pdata->platform_state_mutex);
407 case PM_SUSPEND_PREPARE:
408 if (arche_pdata->state != ARCHE_PLATFORM_STATE_ACTIVE) {
412 device_for_each_child(arche_pdata->dev, NULL, apb_poweroff);
413 arche_platform_poweroff_seq(arche_pdata);
415 case PM_POST_SUSPEND:
416 if (arche_pdata->state != ARCHE_PLATFORM_STATE_OFF)
419 arche_platform_wd_irq_en(arche_pdata);
420 arche_platform_coldboot_seq(arche_pdata);
425 mutex_unlock(&arche_pdata->platform_state_mutex);
430 static int arche_platform_probe(struct platform_device *pdev)
432 struct arche_platform_drvdata *arche_pdata;
433 struct device *dev = &pdev->dev;
434 struct device_node *np = dev->of_node;
437 arche_pdata = devm_kzalloc(&pdev->dev, sizeof(*arche_pdata),
442 /* setup svc reset gpio */
443 arche_pdata->is_reset_act_hi = of_property_read_bool(np,
444 "svc,reset-active-high");
445 arche_pdata->svc_reset_gpio = of_get_named_gpio(np,
448 if (arche_pdata->svc_reset_gpio < 0) {
449 dev_err(dev, "failed to get reset-gpio\n");
450 return arche_pdata->svc_reset_gpio;
452 ret = devm_gpio_request(dev, arche_pdata->svc_reset_gpio, "svc-reset");
454 dev_err(dev, "failed to request svc-reset gpio:%d\n", ret);
457 ret = gpio_direction_output(arche_pdata->svc_reset_gpio,
458 arche_pdata->is_reset_act_hi);
460 dev_err(dev, "failed to set svc-reset gpio dir:%d\n", ret);
463 arche_platform_set_state(arche_pdata, ARCHE_PLATFORM_STATE_OFF);
465 arche_pdata->svc_sysboot_gpio = of_get_named_gpio(np,
466 "svc,sysboot-gpio", 0);
467 if (arche_pdata->svc_sysboot_gpio < 0) {
468 dev_err(dev, "failed to get sysboot gpio\n");
469 return arche_pdata->svc_sysboot_gpio;
471 ret = devm_gpio_request(dev, arche_pdata->svc_sysboot_gpio, "sysboot0");
473 dev_err(dev, "failed to request sysboot0 gpio:%d\n", ret);
476 ret = gpio_direction_output(arche_pdata->svc_sysboot_gpio, 0);
478 dev_err(dev, "failed to set svc-reset gpio dir:%d\n", ret);
482 /* setup the clock request gpio first */
483 arche_pdata->svc_refclk_req = of_get_named_gpio(np,
484 "svc,refclk-req-gpio", 0);
485 if (arche_pdata->svc_refclk_req < 0) {
486 dev_err(dev, "failed to get svc clock-req gpio\n");
487 return arche_pdata->svc_refclk_req;
489 ret = devm_gpio_request(dev, arche_pdata->svc_refclk_req,
492 dev_err(dev, "failed to request svc-clk-req gpio: %d\n", ret);
495 ret = gpio_direction_input(arche_pdata->svc_refclk_req);
497 dev_err(dev, "failed to set svc-clk-req gpio dir :%d\n", ret);
501 /* setup refclk2 to follow the pin */
502 arche_pdata->svc_ref_clk = devm_clk_get(dev, "svc_ref_clk");
503 if (IS_ERR(arche_pdata->svc_ref_clk)) {
504 ret = PTR_ERR(arche_pdata->svc_ref_clk);
505 dev_err(dev, "failed to get svc_ref_clk: %d\n", ret);
509 platform_set_drvdata(pdev, arche_pdata);
511 arche_pdata->num_apbs = of_get_child_count(np);
512 dev_dbg(dev, "Number of APB's available - %d\n", arche_pdata->num_apbs);
514 arche_pdata->wake_detect_gpio = of_get_named_gpio(np,
515 "svc,wake-detect-gpio",
517 if (arche_pdata->wake_detect_gpio < 0) {
518 dev_err(dev, "failed to get wake detect gpio\n");
519 return arche_pdata->wake_detect_gpio;
522 ret = devm_gpio_request(dev, arche_pdata->wake_detect_gpio,
525 dev_err(dev, "Failed requesting wake_detect gpio %d\n",
526 arche_pdata->wake_detect_gpio);
530 arche_platform_set_wake_detect_state(arche_pdata, WD_STATE_IDLE);
532 arche_pdata->dev = &pdev->dev;
534 spin_lock_init(&arche_pdata->wake_lock);
535 mutex_init(&arche_pdata->platform_state_mutex);
536 arche_pdata->wake_detect_irq =
537 gpio_to_irq(arche_pdata->wake_detect_gpio);
539 ret = devm_request_threaded_irq(dev, arche_pdata->wake_detect_irq,
540 arche_platform_wd_irq,
541 arche_platform_wd_irq_thread,
542 IRQF_TRIGGER_FALLING |
543 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
544 dev_name(dev), arche_pdata);
546 dev_err(dev, "failed to request wake detect IRQ %d\n", ret);
549 disable_irq(arche_pdata->wake_detect_irq);
551 ret = device_create_file(dev, &dev_attr_state);
553 dev_err(dev, "failed to create state file in sysfs\n");
557 ret = of_platform_populate(np, NULL, NULL, dev);
559 dev_err(dev, "failed to populate child nodes %d\n", ret);
560 goto err_device_remove;
563 arche_pdata->pm_notifier.notifier_call = arche_platform_pm_notifier;
564 ret = register_pm_notifier(&arche_pdata->pm_notifier);
567 dev_err(dev, "failed to register pm notifier %d\n", ret);
568 goto err_device_remove;
571 /* Explicitly power off if requested */
572 if (!of_property_read_bool(pdev->dev.of_node, "arche,init-off")) {
573 mutex_lock(&arche_pdata->platform_state_mutex);
574 ret = arche_platform_coldboot_seq(arche_pdata);
576 dev_err(dev, "Failed to cold boot svc %d\n", ret);
579 arche_platform_wd_irq_en(arche_pdata);
580 mutex_unlock(&arche_pdata->platform_state_mutex);
583 dev_info(dev, "Device registered successfully\n");
587 mutex_unlock(&arche_pdata->platform_state_mutex);
589 device_remove_file(&pdev->dev, &dev_attr_state);
593 static int arche_remove_child(struct device *dev, void *unused)
595 struct platform_device *pdev = to_platform_device(dev);
597 platform_device_unregister(pdev);
602 static int arche_platform_remove(struct platform_device *pdev)
604 struct arche_platform_drvdata *arche_pdata = platform_get_drvdata(pdev);
606 unregister_pm_notifier(&arche_pdata->pm_notifier);
607 device_remove_file(&pdev->dev, &dev_attr_state);
608 device_for_each_child(&pdev->dev, NULL, arche_remove_child);
609 arche_platform_poweroff_seq(arche_pdata);
610 platform_set_drvdata(pdev, NULL);
612 if (usb3613_hub_mode_ctrl(false))
613 dev_warn(arche_pdata->dev, "failed to control hub device\n");
614 /* TODO: Should we do anything more here ?? */
618 static __maybe_unused int arche_platform_suspend(struct device *dev)
621 * If timing profile premits, we may shutdown bridge
626 * Also, need to make sure we meet precondition for unipro suspend
627 * Precondition: Definition ???
632 static __maybe_unused int arche_platform_resume(struct device *dev)
635 * Atleast for ES2 we have to meet the delay requirement between
636 * unipro switch and AP bridge init, depending on whether bridge is in
637 * OFF state or standby state.
639 * Based on whether bridge is in standby or OFF state we may have to
640 * assert multiple signals. Please refer to WDM spec, for more info.
646 static void arche_platform_shutdown(struct platform_device *pdev)
648 struct arche_platform_drvdata *arche_pdata = platform_get_drvdata(pdev);
650 arche_platform_poweroff_seq(arche_pdata);
652 usb3613_hub_mode_ctrl(false);
655 static SIMPLE_DEV_PM_OPS(arche_platform_pm_ops,
656 arche_platform_suspend,
657 arche_platform_resume);
659 static const struct of_device_id arche_platform_of_match[] = {
660 { .compatible = "google,arche-platform", }, /* Use PID/VID of SVC device */
664 static const struct of_device_id arche_combined_id[] = {
665 { .compatible = "google,arche-platform", }, /* Use PID/VID of SVC device */
666 { .compatible = "usbffff,2", },
669 MODULE_DEVICE_TABLE(of, arche_combined_id);
671 static struct platform_driver arche_platform_device_driver = {
672 .probe = arche_platform_probe,
673 .remove = arche_platform_remove,
674 .shutdown = arche_platform_shutdown,
676 .name = "arche-platform-ctrl",
677 .pm = &arche_platform_pm_ops,
678 .of_match_table = arche_platform_of_match,
682 static int __init arche_init(void)
686 retval = platform_driver_register(&arche_platform_device_driver);
690 retval = arche_apb_init();
692 platform_driver_unregister(&arche_platform_device_driver);
696 module_init(arche_init);
698 static void __exit arche_exit(void)
701 platform_driver_unregister(&arche_platform_device_driver);
703 module_exit(arche_exit);
705 MODULE_LICENSE("GPL v2");
706 MODULE_AUTHOR("Vaibhav Hiremath <vaibhav.hiremath@linaro.org>");
707 MODULE_DESCRIPTION("Arche Platform Driver");