1 // SPDX-License-Identifier: GPL-2.0
3 * Arche Platform driver to enable Unipro link.
5 * Copyright 2014-2015 Google Inc.
6 * Copyright 2014-2015 Linaro Ltd.
10 #include <linux/delay.h>
11 #include <linux/gpio.h>
12 #include <linux/init.h>
13 #include <linux/module.h>
14 #include <linux/of_gpio.h>
15 #include <linux/of_platform.h>
16 #include <linux/pinctrl/consumer.h>
17 #include <linux/platform_device.h>
19 #include <linux/interrupt.h>
20 #include <linux/irq.h>
21 #include <linux/suspend.h>
22 #include <linux/time.h>
23 #include "arche_platform.h"
26 #if IS_ENABLED(CONFIG_USB_HSIC_USB3613)
27 #include <linux/usb/usb3613.h>
29 static inline int usb3613_hub_mode_ctrl(bool unused)
35 #define WD_COLDBOOT_PULSE_WIDTH_MS 30
37 enum svc_wakedetect_state {
38 WD_STATE_IDLE, /* Default state = pulled high/low */
39 WD_STATE_BOOT_INIT, /* WD = falling edge (low) */
40 WD_STATE_COLDBOOT_TRIG, /* WD = rising edge (high), > 30msec */
41 WD_STATE_STANDBYBOOT_TRIG, /* As of now not used ?? */
42 WD_STATE_COLDBOOT_START, /* Cold boot process started */
43 WD_STATE_STANDBYBOOT_START, /* Not used */
46 struct arche_platform_drvdata {
47 /* Control GPIO signals to and from AP <=> SVC */
51 int wake_detect_gpio; /* bi-dir,maps to WAKE_MOD & WAKE_FRAME signals */
53 enum arche_platform_state state;
56 struct clk *svc_ref_clk;
58 struct pinctrl *pinctrl;
59 struct pinctrl_state *pin_default;
63 enum svc_wakedetect_state wake_detect_state;
65 spinlock_t wake_lock; /* Protect wake_detect_state */
66 struct mutex platform_state_mutex; /* Protect state */
67 unsigned long wake_detect_start;
68 struct notifier_block pm_notifier;
73 /* Requires calling context to hold arche_pdata->platform_state_mutex */
74 static void arche_platform_set_state(struct arche_platform_drvdata *arche_pdata,
75 enum arche_platform_state state)
77 arche_pdata->state = state;
80 /* Requires arche_pdata->wake_lock is held by calling context */
81 static void arche_platform_set_wake_detect_state(
82 struct arche_platform_drvdata *arche_pdata,
83 enum svc_wakedetect_state state)
85 arche_pdata->wake_detect_state = state;
88 static inline void svc_reset_onoff(unsigned int gpio, bool onoff)
90 gpio_set_value(gpio, onoff);
93 static int apb_cold_boot(struct device *dev, void *data)
97 ret = apb_ctrl_coldboot(dev);
99 dev_warn(dev, "failed to coldboot\n");
101 /*Child nodes are independent, so do not exit coldboot operation */
105 static int apb_poweroff(struct device *dev, void *data)
107 apb_ctrl_poweroff(dev);
109 /* Enable HUB3613 into HUB mode. */
110 if (usb3613_hub_mode_ctrl(false))
111 dev_warn(dev, "failed to control hub device\n");
116 static void arche_platform_wd_irq_en(struct arche_platform_drvdata *arche_pdata)
118 /* Enable interrupt here, to read event back from SVC */
119 gpio_direction_input(arche_pdata->wake_detect_gpio);
120 enable_irq(arche_pdata->wake_detect_irq);
123 static irqreturn_t arche_platform_wd_irq_thread(int irq, void *devid)
125 struct arche_platform_drvdata *arche_pdata = devid;
128 spin_lock_irqsave(&arche_pdata->wake_lock, flags);
129 if (arche_pdata->wake_detect_state != WD_STATE_COLDBOOT_TRIG) {
130 /* Something is wrong */
131 spin_unlock_irqrestore(&arche_pdata->wake_lock, flags);
135 arche_platform_set_wake_detect_state(arche_pdata,
136 WD_STATE_COLDBOOT_START);
137 spin_unlock_irqrestore(&arche_pdata->wake_lock, flags);
139 /* It should complete power cycle, so first make sure it is poweroff */
140 device_for_each_child(arche_pdata->dev, NULL, apb_poweroff);
142 /* Bring APB out of reset: cold boot sequence */
143 device_for_each_child(arche_pdata->dev, NULL, apb_cold_boot);
145 /* Enable HUB3613 into HUB mode. */
146 if (usb3613_hub_mode_ctrl(true))
147 dev_warn(arche_pdata->dev, "failed to control hub device\n");
149 spin_lock_irqsave(&arche_pdata->wake_lock, flags);
150 arche_platform_set_wake_detect_state(arche_pdata, WD_STATE_IDLE);
151 spin_unlock_irqrestore(&arche_pdata->wake_lock, flags);
156 static irqreturn_t arche_platform_wd_irq(int irq, void *devid)
158 struct arche_platform_drvdata *arche_pdata = devid;
161 spin_lock_irqsave(&arche_pdata->wake_lock, flags);
163 if (gpio_get_value(arche_pdata->wake_detect_gpio)) {
164 /* wake/detect rising */
167 * If wake/detect line goes high after low, within less than
168 * 30msec, then standby boot sequence is initiated, which is not
169 * supported/implemented as of now. So ignore it.
171 if (arche_pdata->wake_detect_state == WD_STATE_BOOT_INIT) {
172 if (time_before(jiffies,
173 arche_pdata->wake_detect_start +
174 msecs_to_jiffies(WD_COLDBOOT_PULSE_WIDTH_MS))) {
175 arche_platform_set_wake_detect_state(arche_pdata,
179 * Check we are not in middle of irq thread
182 if (arche_pdata->wake_detect_state !=
183 WD_STATE_COLDBOOT_START) {
184 arche_platform_set_wake_detect_state(arche_pdata,
185 WD_STATE_COLDBOOT_TRIG);
186 spin_unlock_irqrestore(
187 &arche_pdata->wake_lock,
189 return IRQ_WAKE_THREAD;
194 /* wake/detect falling */
195 if (arche_pdata->wake_detect_state == WD_STATE_IDLE) {
196 arche_pdata->wake_detect_start = jiffies;
198 * In the beginning, when wake/detect goes low
199 * (first time), we assume it is meant for coldboot
200 * and set the flag. If wake/detect line stays low
201 * beyond 30msec, then it is coldboot else fallback
204 arche_platform_set_wake_detect_state(arche_pdata,
209 spin_unlock_irqrestore(&arche_pdata->wake_lock, flags);
215 * Requires arche_pdata->platform_state_mutex to be held
218 arche_platform_coldboot_seq(struct arche_platform_drvdata *arche_pdata)
222 if (arche_pdata->state == ARCHE_PLATFORM_STATE_ACTIVE)
225 dev_info(arche_pdata->dev, "Booting from cold boot state\n");
227 svc_reset_onoff(arche_pdata->svc_reset_gpio,
228 arche_pdata->is_reset_act_hi);
230 gpio_set_value(arche_pdata->svc_sysboot_gpio, 0);
231 usleep_range(100, 200);
233 ret = clk_prepare_enable(arche_pdata->svc_ref_clk);
235 dev_err(arche_pdata->dev, "failed to enable svc_ref_clk: %d\n",
240 /* bring SVC out of reset */
241 svc_reset_onoff(arche_pdata->svc_reset_gpio,
242 !arche_pdata->is_reset_act_hi);
244 arche_platform_set_state(arche_pdata, ARCHE_PLATFORM_STATE_ACTIVE);
250 * Requires arche_pdata->platform_state_mutex to be held
253 arche_platform_fw_flashing_seq(struct arche_platform_drvdata *arche_pdata)
257 if (arche_pdata->state == ARCHE_PLATFORM_STATE_FW_FLASHING)
260 dev_info(arche_pdata->dev, "Switching to FW flashing state\n");
262 svc_reset_onoff(arche_pdata->svc_reset_gpio,
263 arche_pdata->is_reset_act_hi);
265 gpio_set_value(arche_pdata->svc_sysboot_gpio, 1);
267 usleep_range(100, 200);
269 ret = clk_prepare_enable(arche_pdata->svc_ref_clk);
271 dev_err(arche_pdata->dev, "failed to enable svc_ref_clk: %d\n",
276 svc_reset_onoff(arche_pdata->svc_reset_gpio,
277 !arche_pdata->is_reset_act_hi);
279 arche_platform_set_state(arche_pdata, ARCHE_PLATFORM_STATE_FW_FLASHING);
285 * Requires arche_pdata->platform_state_mutex to be held
288 arche_platform_poweroff_seq(struct arche_platform_drvdata *arche_pdata)
292 if (arche_pdata->state == ARCHE_PLATFORM_STATE_OFF)
295 /* If in fw_flashing mode, then no need to repeate things again */
296 if (arche_pdata->state != ARCHE_PLATFORM_STATE_FW_FLASHING) {
297 disable_irq(arche_pdata->wake_detect_irq);
299 spin_lock_irqsave(&arche_pdata->wake_lock, flags);
300 arche_platform_set_wake_detect_state(arche_pdata,
302 spin_unlock_irqrestore(&arche_pdata->wake_lock, flags);
305 clk_disable_unprepare(arche_pdata->svc_ref_clk);
307 /* As part of exit, put APB back in reset state */
308 svc_reset_onoff(arche_pdata->svc_reset_gpio,
309 arche_pdata->is_reset_act_hi);
311 arche_platform_set_state(arche_pdata, ARCHE_PLATFORM_STATE_OFF);
314 static ssize_t state_store(struct device *dev,
315 struct device_attribute *attr,
316 const char *buf, size_t count)
318 struct arche_platform_drvdata *arche_pdata = dev_get_drvdata(dev);
321 mutex_lock(&arche_pdata->platform_state_mutex);
323 if (sysfs_streq(buf, "off")) {
324 if (arche_pdata->state == ARCHE_PLATFORM_STATE_OFF)
327 /* If SVC goes down, bring down APB's as well */
328 device_for_each_child(arche_pdata->dev, NULL, apb_poweroff);
330 arche_platform_poweroff_seq(arche_pdata);
332 } else if (sysfs_streq(buf, "active")) {
333 if (arche_pdata->state == ARCHE_PLATFORM_STATE_ACTIVE)
336 /* First we want to make sure we power off everything
337 * and then activate back again
339 device_for_each_child(arche_pdata->dev, NULL, apb_poweroff);
340 arche_platform_poweroff_seq(arche_pdata);
342 arche_platform_wd_irq_en(arche_pdata);
343 ret = arche_platform_coldboot_seq(arche_pdata);
347 } else if (sysfs_streq(buf, "standby")) {
348 if (arche_pdata->state == ARCHE_PLATFORM_STATE_STANDBY)
351 dev_warn(arche_pdata->dev, "standby state not supported\n");
352 } else if (sysfs_streq(buf, "fw_flashing")) {
353 if (arche_pdata->state == ARCHE_PLATFORM_STATE_FW_FLASHING)
357 * Here we only control SVC.
359 * In case of FW_FLASHING mode we do not want to control
360 * APBs, as in case of V2, SPI bus is shared between both
361 * the APBs. So let user chose which APB he wants to flash.
363 arche_platform_poweroff_seq(arche_pdata);
365 ret = arche_platform_fw_flashing_seq(arche_pdata);
369 dev_err(arche_pdata->dev, "unknown state\n");
374 mutex_unlock(&arche_pdata->platform_state_mutex);
375 return ret ? ret : count;
378 static ssize_t state_show(struct device *dev,
379 struct device_attribute *attr, char *buf)
381 struct arche_platform_drvdata *arche_pdata = dev_get_drvdata(dev);
383 switch (arche_pdata->state) {
384 case ARCHE_PLATFORM_STATE_OFF:
385 return sprintf(buf, "off\n");
386 case ARCHE_PLATFORM_STATE_ACTIVE:
387 return sprintf(buf, "active\n");
388 case ARCHE_PLATFORM_STATE_STANDBY:
389 return sprintf(buf, "standby\n");
390 case ARCHE_PLATFORM_STATE_FW_FLASHING:
391 return sprintf(buf, "fw_flashing\n");
393 return sprintf(buf, "unknown state\n");
397 static DEVICE_ATTR_RW(state);
399 static int arche_platform_pm_notifier(struct notifier_block *notifier,
400 unsigned long pm_event, void *unused)
402 struct arche_platform_drvdata *arche_pdata =
403 container_of(notifier, struct arche_platform_drvdata,
405 int ret = NOTIFY_DONE;
407 mutex_lock(&arche_pdata->platform_state_mutex);
409 case PM_SUSPEND_PREPARE:
410 if (arche_pdata->state != ARCHE_PLATFORM_STATE_ACTIVE) {
414 device_for_each_child(arche_pdata->dev, NULL, apb_poweroff);
415 arche_platform_poweroff_seq(arche_pdata);
417 case PM_POST_SUSPEND:
418 if (arche_pdata->state != ARCHE_PLATFORM_STATE_OFF)
421 arche_platform_wd_irq_en(arche_pdata);
422 arche_platform_coldboot_seq(arche_pdata);
427 mutex_unlock(&arche_pdata->platform_state_mutex);
432 static int arche_platform_probe(struct platform_device *pdev)
434 struct arche_platform_drvdata *arche_pdata;
435 struct device *dev = &pdev->dev;
436 struct device_node *np = dev->of_node;
439 arche_pdata = devm_kzalloc(&pdev->dev, sizeof(*arche_pdata),
444 /* setup svc reset gpio */
445 arche_pdata->is_reset_act_hi = of_property_read_bool(np,
446 "svc,reset-active-high");
447 arche_pdata->svc_reset_gpio = of_get_named_gpio(np,
450 if (!gpio_is_valid(arche_pdata->svc_reset_gpio)) {
451 dev_err(dev, "failed to get reset-gpio\n");
452 return arche_pdata->svc_reset_gpio;
454 ret = devm_gpio_request(dev, arche_pdata->svc_reset_gpio, "svc-reset");
456 dev_err(dev, "failed to request svc-reset gpio:%d\n", ret);
459 ret = gpio_direction_output(arche_pdata->svc_reset_gpio,
460 arche_pdata->is_reset_act_hi);
462 dev_err(dev, "failed to set svc-reset gpio dir:%d\n", ret);
465 arche_platform_set_state(arche_pdata, ARCHE_PLATFORM_STATE_OFF);
467 arche_pdata->svc_sysboot_gpio = of_get_named_gpio(np,
470 if (!gpio_is_valid(arche_pdata->svc_sysboot_gpio)) {
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",
489 if (!gpio_is_valid(arche_pdata->svc_refclk_req)) {
490 dev_err(dev, "failed to get svc clock-req gpio\n");
491 return arche_pdata->svc_refclk_req;
493 ret = devm_gpio_request(dev, arche_pdata->svc_refclk_req,
496 dev_err(dev, "failed to request svc-clk-req gpio: %d\n", ret);
499 ret = gpio_direction_input(arche_pdata->svc_refclk_req);
501 dev_err(dev, "failed to set svc-clk-req gpio dir :%d\n", ret);
505 /* setup refclk2 to follow the pin */
506 arche_pdata->svc_ref_clk = devm_clk_get(dev, "svc_ref_clk");
507 if (IS_ERR(arche_pdata->svc_ref_clk)) {
508 ret = PTR_ERR(arche_pdata->svc_ref_clk);
509 dev_err(dev, "failed to get svc_ref_clk: %d\n", ret);
513 platform_set_drvdata(pdev, arche_pdata);
515 arche_pdata->num_apbs = of_get_child_count(np);
516 dev_dbg(dev, "Number of APB's available - %d\n", arche_pdata->num_apbs);
518 arche_pdata->wake_detect_gpio = of_get_named_gpio(np,
519 "svc,wake-detect-gpio",
521 if (arche_pdata->wake_detect_gpio < 0) {
522 dev_err(dev, "failed to get wake detect gpio\n");
523 return arche_pdata->wake_detect_gpio;
526 ret = devm_gpio_request(dev, arche_pdata->wake_detect_gpio,
529 dev_err(dev, "Failed requesting wake_detect gpio %d\n",
530 arche_pdata->wake_detect_gpio);
534 arche_platform_set_wake_detect_state(arche_pdata, WD_STATE_IDLE);
536 arche_pdata->dev = &pdev->dev;
538 spin_lock_init(&arche_pdata->wake_lock);
539 mutex_init(&arche_pdata->platform_state_mutex);
540 arche_pdata->wake_detect_irq =
541 gpio_to_irq(arche_pdata->wake_detect_gpio);
543 ret = devm_request_threaded_irq(dev, arche_pdata->wake_detect_irq,
544 arche_platform_wd_irq,
545 arche_platform_wd_irq_thread,
546 IRQF_TRIGGER_FALLING |
547 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
548 dev_name(dev), arche_pdata);
550 dev_err(dev, "failed to request wake detect IRQ %d\n", ret);
553 disable_irq(arche_pdata->wake_detect_irq);
555 ret = device_create_file(dev, &dev_attr_state);
557 dev_err(dev, "failed to create state file in sysfs\n");
561 ret = of_platform_populate(np, NULL, NULL, dev);
563 dev_err(dev, "failed to populate child nodes %d\n", ret);
564 goto err_device_remove;
567 arche_pdata->pm_notifier.notifier_call = arche_platform_pm_notifier;
568 ret = register_pm_notifier(&arche_pdata->pm_notifier);
571 dev_err(dev, "failed to register pm notifier %d\n", ret);
572 goto err_device_remove;
575 /* Explicitly power off if requested */
576 if (!of_property_read_bool(pdev->dev.of_node, "arche,init-off")) {
577 mutex_lock(&arche_pdata->platform_state_mutex);
578 ret = arche_platform_coldboot_seq(arche_pdata);
580 dev_err(dev, "Failed to cold boot svc %d\n", ret);
583 arche_platform_wd_irq_en(arche_pdata);
584 mutex_unlock(&arche_pdata->platform_state_mutex);
587 dev_info(dev, "Device registered successfully\n");
591 mutex_unlock(&arche_pdata->platform_state_mutex);
593 device_remove_file(&pdev->dev, &dev_attr_state);
597 static int arche_remove_child(struct device *dev, void *unused)
599 struct platform_device *pdev = to_platform_device(dev);
601 platform_device_unregister(pdev);
606 static int arche_platform_remove(struct platform_device *pdev)
608 struct arche_platform_drvdata *arche_pdata = platform_get_drvdata(pdev);
610 unregister_pm_notifier(&arche_pdata->pm_notifier);
611 device_remove_file(&pdev->dev, &dev_attr_state);
612 device_for_each_child(&pdev->dev, NULL, arche_remove_child);
613 arche_platform_poweroff_seq(arche_pdata);
615 if (usb3613_hub_mode_ctrl(false))
616 dev_warn(arche_pdata->dev, "failed to control hub device\n");
617 /* TODO: Should we do anything more here ?? */
621 static __maybe_unused int arche_platform_suspend(struct device *dev)
624 * If timing profile premits, we may shutdown bridge
629 * Also, need to make sure we meet precondition for unipro suspend
630 * Precondition: Definition ???
635 static __maybe_unused int arche_platform_resume(struct device *dev)
638 * Atleast for ES2 we have to meet the delay requirement between
639 * unipro switch and AP bridge init, depending on whether bridge is in
640 * OFF state or standby state.
642 * Based on whether bridge is in standby or OFF state we may have to
643 * assert multiple signals. Please refer to WDM spec, for more info.
649 static void arche_platform_shutdown(struct platform_device *pdev)
651 struct arche_platform_drvdata *arche_pdata = platform_get_drvdata(pdev);
653 arche_platform_poweroff_seq(arche_pdata);
655 usb3613_hub_mode_ctrl(false);
658 static SIMPLE_DEV_PM_OPS(arche_platform_pm_ops,
659 arche_platform_suspend,
660 arche_platform_resume);
662 static const struct of_device_id arche_platform_of_match[] = {
663 /* Use PID/VID of SVC device */
664 { .compatible = "google,arche-platform", },
668 static const struct of_device_id arche_combined_id[] = {
669 /* Use PID/VID of SVC device */
670 { .compatible = "google,arche-platform", },
671 { .compatible = "usbffff,2", },
674 MODULE_DEVICE_TABLE(of, arche_combined_id);
676 static struct platform_driver arche_platform_device_driver = {
677 .probe = arche_platform_probe,
678 .remove = arche_platform_remove,
679 .shutdown = arche_platform_shutdown,
681 .name = "arche-platform-ctrl",
682 .pm = &arche_platform_pm_ops,
683 .of_match_table = arche_platform_of_match,
687 static int __init arche_init(void)
691 retval = platform_driver_register(&arche_platform_device_driver);
695 retval = arche_apb_init();
697 platform_driver_unregister(&arche_platform_device_driver);
701 module_init(arche_init);
703 static void __exit arche_exit(void)
706 platform_driver_unregister(&arche_platform_device_driver);
708 module_exit(arche_exit);
710 MODULE_LICENSE("GPL v2");
712 MODULE_DESCRIPTION("Arche Platform Driver");