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/consumer.h>
12 #include <linux/init.h>
13 #include <linux/module.h>
14 #include <linux/of_platform.h>
15 #include <linux/pinctrl/consumer.h>
16 #include <linux/platform_device.h>
18 #include <linux/interrupt.h>
19 #include <linux/irq.h>
20 #include <linux/suspend.h>
21 #include <linux/time.h>
22 #include <linux/greybus.h>
24 #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 */
48 struct gpio_desc *svc_reset;
50 struct gpio_desc *svc_sysboot;
51 struct gpio_desc *wake_detect; /* bi-dir,maps to WAKE_MOD & WAKE_FRAME signals */
53 enum arche_platform_state state;
55 struct gpio_desc *svc_refclk_req;
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(struct arche_platform_drvdata *arche_pdata,
82 enum svc_wakedetect_state state)
84 arche_pdata->wake_detect_state = state;
87 static inline void svc_reset_onoff(struct gpio_desc *gpio, bool onoff)
89 gpiod_set_raw_value(gpio, onoff);
92 static int apb_cold_boot(struct device *dev, void *data)
96 ret = apb_ctrl_coldboot(dev);
98 dev_warn(dev, "failed to coldboot\n");
100 /*Child nodes are independent, so do not exit coldboot operation */
104 static int apb_poweroff(struct device *dev, void *data)
106 apb_ctrl_poweroff(dev);
108 /* Enable HUB3613 into HUB mode. */
109 if (usb3613_hub_mode_ctrl(false))
110 dev_warn(dev, "failed to control hub device\n");
115 static void arche_platform_wd_irq_en(struct arche_platform_drvdata *arche_pdata)
117 /* Enable interrupt here, to read event back from SVC */
118 enable_irq(arche_pdata->wake_detect_irq);
121 static irqreturn_t arche_platform_wd_irq_thread(int irq, void *devid)
123 struct arche_platform_drvdata *arche_pdata = devid;
126 spin_lock_irqsave(&arche_pdata->wake_lock, flags);
127 if (arche_pdata->wake_detect_state != WD_STATE_COLDBOOT_TRIG) {
128 /* Something is wrong */
129 spin_unlock_irqrestore(&arche_pdata->wake_lock, flags);
133 arche_platform_set_wake_detect_state(arche_pdata,
134 WD_STATE_COLDBOOT_START);
135 spin_unlock_irqrestore(&arche_pdata->wake_lock, flags);
137 /* It should complete power cycle, so first make sure it is poweroff */
138 device_for_each_child(arche_pdata->dev, NULL, apb_poweroff);
140 /* Bring APB out of reset: cold boot sequence */
141 device_for_each_child(arche_pdata->dev, NULL, apb_cold_boot);
143 /* Enable HUB3613 into HUB mode. */
144 if (usb3613_hub_mode_ctrl(true))
145 dev_warn(arche_pdata->dev, "failed to control hub device\n");
147 spin_lock_irqsave(&arche_pdata->wake_lock, flags);
148 arche_platform_set_wake_detect_state(arche_pdata, WD_STATE_IDLE);
149 spin_unlock_irqrestore(&arche_pdata->wake_lock, flags);
154 static irqreturn_t arche_platform_wd_irq(int irq, void *devid)
156 struct arche_platform_drvdata *arche_pdata = devid;
159 spin_lock_irqsave(&arche_pdata->wake_lock, flags);
161 if (gpiod_get_value(arche_pdata->wake_detect)) {
162 /* wake/detect rising */
165 * If wake/detect line goes high after low, within less than
166 * 30msec, then standby boot sequence is initiated, which is not
167 * supported/implemented as of now. So ignore it.
169 if (arche_pdata->wake_detect_state == WD_STATE_BOOT_INIT) {
170 if (time_before(jiffies,
171 arche_pdata->wake_detect_start +
172 msecs_to_jiffies(WD_COLDBOOT_PULSE_WIDTH_MS))) {
173 arche_platform_set_wake_detect_state(arche_pdata,
177 * Check we are not in middle of irq thread
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(&arche_pdata->wake_lock,
186 return IRQ_WAKE_THREAD;
191 /* wake/detect falling */
192 if (arche_pdata->wake_detect_state == WD_STATE_IDLE) {
193 arche_pdata->wake_detect_start = jiffies;
195 * In the beginning, when wake/detect goes low
196 * (first time), we assume it is meant for coldboot
197 * and set the flag. If wake/detect line stays low
198 * beyond 30msec, then it is coldboot else fallback
201 arche_platform_set_wake_detect_state(arche_pdata,
206 spin_unlock_irqrestore(&arche_pdata->wake_lock, flags);
212 * Requires arche_pdata->platform_state_mutex to be held
215 arche_platform_coldboot_seq(struct arche_platform_drvdata *arche_pdata)
219 if (arche_pdata->state == ARCHE_PLATFORM_STATE_ACTIVE)
222 dev_info(arche_pdata->dev, "Booting from cold boot state\n");
224 svc_reset_onoff(arche_pdata->svc_reset, arche_pdata->is_reset_act_hi);
226 gpiod_set_value(arche_pdata->svc_sysboot, 0);
227 usleep_range(100, 200);
229 ret = clk_prepare_enable(arche_pdata->svc_ref_clk);
231 dev_err(arche_pdata->dev, "failed to enable svc_ref_clk: %d\n",
236 /* bring SVC out of reset */
237 svc_reset_onoff(arche_pdata->svc_reset, !arche_pdata->is_reset_act_hi);
239 arche_platform_set_state(arche_pdata, ARCHE_PLATFORM_STATE_ACTIVE);
245 * Requires arche_pdata->platform_state_mutex to be held
248 arche_platform_fw_flashing_seq(struct arche_platform_drvdata *arche_pdata)
252 if (arche_pdata->state == ARCHE_PLATFORM_STATE_FW_FLASHING)
255 dev_info(arche_pdata->dev, "Switching to FW flashing state\n");
257 svc_reset_onoff(arche_pdata->svc_reset, arche_pdata->is_reset_act_hi);
259 gpiod_set_value(arche_pdata->svc_sysboot, 1);
261 usleep_range(100, 200);
263 ret = clk_prepare_enable(arche_pdata->svc_ref_clk);
265 dev_err(arche_pdata->dev, "failed to enable svc_ref_clk: %d\n",
270 svc_reset_onoff(arche_pdata->svc_reset, !arche_pdata->is_reset_act_hi);
272 arche_platform_set_state(arche_pdata, ARCHE_PLATFORM_STATE_FW_FLASHING);
278 * Requires arche_pdata->platform_state_mutex to be held
281 arche_platform_poweroff_seq(struct arche_platform_drvdata *arche_pdata)
285 if (arche_pdata->state == ARCHE_PLATFORM_STATE_OFF)
288 /* If in fw_flashing mode, then no need to repeate things again */
289 if (arche_pdata->state != ARCHE_PLATFORM_STATE_FW_FLASHING) {
290 disable_irq(arche_pdata->wake_detect_irq);
292 spin_lock_irqsave(&arche_pdata->wake_lock, flags);
293 arche_platform_set_wake_detect_state(arche_pdata,
295 spin_unlock_irqrestore(&arche_pdata->wake_lock, flags);
298 clk_disable_unprepare(arche_pdata->svc_ref_clk);
300 /* As part of exit, put APB back in reset state */
301 svc_reset_onoff(arche_pdata->svc_reset, arche_pdata->is_reset_act_hi);
303 arche_platform_set_state(arche_pdata, ARCHE_PLATFORM_STATE_OFF);
306 static ssize_t state_store(struct device *dev,
307 struct device_attribute *attr,
308 const char *buf, size_t count)
310 struct arche_platform_drvdata *arche_pdata = dev_get_drvdata(dev);
313 mutex_lock(&arche_pdata->platform_state_mutex);
315 if (sysfs_streq(buf, "off")) {
316 if (arche_pdata->state == ARCHE_PLATFORM_STATE_OFF)
319 /* If SVC goes down, bring down APB's as well */
320 device_for_each_child(arche_pdata->dev, NULL, apb_poweroff);
322 arche_platform_poweroff_seq(arche_pdata);
324 } else if (sysfs_streq(buf, "active")) {
325 if (arche_pdata->state == ARCHE_PLATFORM_STATE_ACTIVE)
328 /* First we want to make sure we power off everything
329 * and then activate back again
331 device_for_each_child(arche_pdata->dev, NULL, apb_poweroff);
332 arche_platform_poweroff_seq(arche_pdata);
334 arche_platform_wd_irq_en(arche_pdata);
335 ret = arche_platform_coldboot_seq(arche_pdata);
339 } else if (sysfs_streq(buf, "standby")) {
340 if (arche_pdata->state == ARCHE_PLATFORM_STATE_STANDBY)
343 dev_warn(arche_pdata->dev, "standby state not supported\n");
344 } else if (sysfs_streq(buf, "fw_flashing")) {
345 if (arche_pdata->state == ARCHE_PLATFORM_STATE_FW_FLASHING)
349 * Here we only control SVC.
351 * In case of FW_FLASHING mode we do not want to control
352 * APBs, as in case of V2, SPI bus is shared between both
353 * the APBs. So let user chose which APB he wants to flash.
355 arche_platform_poweroff_seq(arche_pdata);
357 ret = arche_platform_fw_flashing_seq(arche_pdata);
361 dev_err(arche_pdata->dev, "unknown state\n");
366 mutex_unlock(&arche_pdata->platform_state_mutex);
367 return ret ? ret : count;
370 static ssize_t state_show(struct device *dev,
371 struct device_attribute *attr, char *buf)
373 struct arche_platform_drvdata *arche_pdata = dev_get_drvdata(dev);
375 switch (arche_pdata->state) {
376 case ARCHE_PLATFORM_STATE_OFF:
377 return sprintf(buf, "off\n");
378 case ARCHE_PLATFORM_STATE_ACTIVE:
379 return sprintf(buf, "active\n");
380 case ARCHE_PLATFORM_STATE_STANDBY:
381 return sprintf(buf, "standby\n");
382 case ARCHE_PLATFORM_STATE_FW_FLASHING:
383 return sprintf(buf, "fw_flashing\n");
385 return sprintf(buf, "unknown state\n");
389 static DEVICE_ATTR_RW(state);
391 static int arche_platform_pm_notifier(struct notifier_block *notifier,
392 unsigned long pm_event, void *unused)
394 struct arche_platform_drvdata *arche_pdata =
395 container_of(notifier, struct arche_platform_drvdata,
397 int ret = NOTIFY_DONE;
399 mutex_lock(&arche_pdata->platform_state_mutex);
401 case PM_SUSPEND_PREPARE:
402 if (arche_pdata->state != ARCHE_PLATFORM_STATE_ACTIVE) {
406 device_for_each_child(arche_pdata->dev, NULL, apb_poweroff);
407 arche_platform_poweroff_seq(arche_pdata);
409 case PM_POST_SUSPEND:
410 if (arche_pdata->state != ARCHE_PLATFORM_STATE_OFF)
413 arche_platform_wd_irq_en(arche_pdata);
414 arche_platform_coldboot_seq(arche_pdata);
419 mutex_unlock(&arche_pdata->platform_state_mutex);
424 static int arche_platform_probe(struct platform_device *pdev)
426 struct arche_platform_drvdata *arche_pdata;
427 struct device *dev = &pdev->dev;
428 struct device_node *np = dev->of_node;
432 arche_pdata = devm_kzalloc(&pdev->dev, sizeof(*arche_pdata),
437 /* setup svc reset gpio */
438 arche_pdata->is_reset_act_hi = of_property_read_bool(np,
439 "svc,reset-active-high");
440 if (arche_pdata->is_reset_act_hi)
441 flags = GPIOD_OUT_HIGH;
443 flags = GPIOD_OUT_LOW;
445 arche_pdata->svc_reset = devm_gpiod_get(dev, "svc,reset", flags);
446 if (IS_ERR(arche_pdata->svc_reset)) {
447 ret = PTR_ERR(arche_pdata->svc_reset);
448 dev_err(dev, "failed to request svc-reset GPIO: %d\n", ret);
451 arche_platform_set_state(arche_pdata, ARCHE_PLATFORM_STATE_OFF);
453 arche_pdata->svc_sysboot = devm_gpiod_get(dev, "svc,sysboot",
455 if (IS_ERR(arche_pdata->svc_sysboot)) {
456 ret = PTR_ERR(arche_pdata->svc_sysboot);
457 dev_err(dev, "failed to request sysboot0 GPIO: %d\n", ret);
461 /* setup the clock request gpio first */
462 arche_pdata->svc_refclk_req = devm_gpiod_get(dev, "svc,refclk-req",
464 if (IS_ERR(arche_pdata->svc_refclk_req)) {
465 ret = PTR_ERR(arche_pdata->svc_refclk_req);
466 dev_err(dev, "failed to request svc-clk-req GPIO: %d\n", ret);
470 /* setup refclk2 to follow the pin */
471 arche_pdata->svc_ref_clk = devm_clk_get(dev, "svc_ref_clk");
472 if (IS_ERR(arche_pdata->svc_ref_clk)) {
473 ret = PTR_ERR(arche_pdata->svc_ref_clk);
474 dev_err(dev, "failed to get svc_ref_clk: %d\n", ret);
478 platform_set_drvdata(pdev, arche_pdata);
480 arche_pdata->num_apbs = of_get_child_count(np);
481 dev_dbg(dev, "Number of APB's available - %d\n", arche_pdata->num_apbs);
483 arche_pdata->wake_detect = devm_gpiod_get(dev, "svc,wake-detect",
485 if (IS_ERR(arche_pdata->wake_detect)) {
486 ret = PTR_ERR(arche_pdata->wake_detect);
487 dev_err(dev, "Failed requesting wake_detect GPIO: %d\n", ret);
491 arche_platform_set_wake_detect_state(arche_pdata, WD_STATE_IDLE);
493 arche_pdata->dev = &pdev->dev;
495 spin_lock_init(&arche_pdata->wake_lock);
496 mutex_init(&arche_pdata->platform_state_mutex);
497 arche_pdata->wake_detect_irq =
498 gpiod_to_irq(arche_pdata->wake_detect);
500 ret = devm_request_threaded_irq(dev, arche_pdata->wake_detect_irq,
501 arche_platform_wd_irq,
502 arche_platform_wd_irq_thread,
503 IRQF_TRIGGER_FALLING |
504 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
505 dev_name(dev), arche_pdata);
507 dev_err(dev, "failed to request wake detect IRQ %d\n", ret);
510 disable_irq(arche_pdata->wake_detect_irq);
512 ret = device_create_file(dev, &dev_attr_state);
514 dev_err(dev, "failed to create state file in sysfs\n");
518 ret = of_platform_populate(np, NULL, NULL, dev);
520 dev_err(dev, "failed to populate child nodes %d\n", ret);
521 goto err_device_remove;
524 arche_pdata->pm_notifier.notifier_call = arche_platform_pm_notifier;
525 ret = register_pm_notifier(&arche_pdata->pm_notifier);
528 dev_err(dev, "failed to register pm notifier %d\n", ret);
529 goto err_device_remove;
532 /* Explicitly power off if requested */
533 if (!of_property_read_bool(pdev->dev.of_node, "arche,init-off")) {
534 mutex_lock(&arche_pdata->platform_state_mutex);
535 ret = arche_platform_coldboot_seq(arche_pdata);
537 dev_err(dev, "Failed to cold boot svc %d\n", ret);
540 arche_platform_wd_irq_en(arche_pdata);
541 mutex_unlock(&arche_pdata->platform_state_mutex);
544 dev_info(dev, "Device registered successfully\n");
548 mutex_unlock(&arche_pdata->platform_state_mutex);
550 device_remove_file(&pdev->dev, &dev_attr_state);
554 static int arche_remove_child(struct device *dev, void *unused)
556 struct platform_device *pdev = to_platform_device(dev);
558 platform_device_unregister(pdev);
563 static void arche_platform_remove(struct platform_device *pdev)
565 struct arche_platform_drvdata *arche_pdata = platform_get_drvdata(pdev);
567 unregister_pm_notifier(&arche_pdata->pm_notifier);
568 device_remove_file(&pdev->dev, &dev_attr_state);
569 device_for_each_child(&pdev->dev, NULL, arche_remove_child);
570 arche_platform_poweroff_seq(arche_pdata);
572 if (usb3613_hub_mode_ctrl(false))
573 dev_warn(arche_pdata->dev, "failed to control hub device\n");
576 static __maybe_unused int arche_platform_suspend(struct device *dev)
579 * If timing profile premits, we may shutdown bridge
584 * Also, need to make sure we meet precondition for unipro suspend
585 * Precondition: Definition ???
590 static __maybe_unused int arche_platform_resume(struct device *dev)
593 * At least for ES2 we have to meet the delay requirement between
594 * unipro switch and AP bridge init, depending on whether bridge is in
595 * OFF state or standby state.
597 * Based on whether bridge is in standby or OFF state we may have to
598 * assert multiple signals. Please refer to WDM spec, for more info.
604 static void arche_platform_shutdown(struct platform_device *pdev)
606 struct arche_platform_drvdata *arche_pdata = platform_get_drvdata(pdev);
608 arche_platform_poweroff_seq(arche_pdata);
610 usb3613_hub_mode_ctrl(false);
613 static SIMPLE_DEV_PM_OPS(arche_platform_pm_ops,
614 arche_platform_suspend,
615 arche_platform_resume);
617 static const struct of_device_id arche_platform_of_match[] = {
618 /* Use PID/VID of SVC device */
619 { .compatible = "google,arche-platform", },
623 static const struct of_device_id arche_combined_id[] = {
624 /* Use PID/VID of SVC device */
625 { .compatible = "google,arche-platform", },
626 { .compatible = "usbffff,2", },
629 MODULE_DEVICE_TABLE(of, arche_combined_id);
631 static struct platform_driver arche_platform_device_driver = {
632 .probe = arche_platform_probe,
633 .remove_new = arche_platform_remove,
634 .shutdown = arche_platform_shutdown,
636 .name = "arche-platform-ctrl",
637 .pm = &arche_platform_pm_ops,
638 .of_match_table = arche_platform_of_match,
642 static int __init arche_init(void)
646 retval = platform_driver_register(&arche_platform_device_driver);
650 retval = arche_apb_init();
652 platform_driver_unregister(&arche_platform_device_driver);
656 module_init(arche_init);
658 static void __exit arche_exit(void)
661 platform_driver_unregister(&arche_platform_device_driver);
663 module_exit(arche_exit);
665 MODULE_LICENSE("GPL v2");
666 MODULE_AUTHOR("Vaibhav Hiremath <vaibhav.hiremath@linaro.org>");
667 MODULE_DESCRIPTION("Arche Platform Driver");