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>
23 #include "arche_platform.h"
25 #if IS_ENABLED(CONFIG_USB_HSIC_USB3613)
26 #include <linux/usb/usb3613.h>
28 static inline int usb3613_hub_mode_ctrl(bool unused)
34 #define WD_COLDBOOT_PULSE_WIDTH_MS 30
36 enum svc_wakedetect_state {
37 WD_STATE_IDLE, /* Default state = pulled high/low */
38 WD_STATE_BOOT_INIT, /* WD = falling edge (low) */
39 WD_STATE_COLDBOOT_TRIG, /* WD = rising edge (high), > 30msec */
40 WD_STATE_STANDBYBOOT_TRIG, /* As of now not used ?? */
41 WD_STATE_COLDBOOT_START, /* Cold boot process started */
42 WD_STATE_STANDBYBOOT_START, /* Not used */
45 struct arche_platform_drvdata {
46 /* Control GPIO signals to and from AP <=> SVC */
47 struct gpio_desc *svc_reset;
49 struct gpio_desc *svc_sysboot;
50 struct gpio_desc *wake_detect; /* bi-dir,maps to WAKE_MOD & WAKE_FRAME signals */
52 enum arche_platform_state state;
54 struct gpio_desc *svc_refclk_req;
55 struct clk *svc_ref_clk;
57 struct pinctrl *pinctrl;
58 struct pinctrl_state *pin_default;
62 enum svc_wakedetect_state wake_detect_state;
64 spinlock_t wake_lock; /* Protect wake_detect_state */
65 struct mutex platform_state_mutex; /* Protect state */
66 unsigned long wake_detect_start;
67 struct notifier_block pm_notifier;
72 /* Requires calling context to hold arche_pdata->platform_state_mutex */
73 static void arche_platform_set_state(struct arche_platform_drvdata *arche_pdata,
74 enum arche_platform_state state)
76 arche_pdata->state = state;
79 /* Requires arche_pdata->wake_lock is held by calling context */
80 static void arche_platform_set_wake_detect_state(
81 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(
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 beginning, 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, arche_pdata->is_reset_act_hi);
227 gpiod_set_value(arche_pdata->svc_sysboot, 0);
228 usleep_range(100, 200);
230 ret = clk_prepare_enable(arche_pdata->svc_ref_clk);
232 dev_err(arche_pdata->dev, "failed to enable svc_ref_clk: %d\n",
237 /* bring SVC out of reset */
238 svc_reset_onoff(arche_pdata->svc_reset, !arche_pdata->is_reset_act_hi);
240 arche_platform_set_state(arche_pdata, ARCHE_PLATFORM_STATE_ACTIVE);
246 * Requires arche_pdata->platform_state_mutex to be held
249 arche_platform_fw_flashing_seq(struct arche_platform_drvdata *arche_pdata)
253 if (arche_pdata->state == ARCHE_PLATFORM_STATE_FW_FLASHING)
256 dev_info(arche_pdata->dev, "Switching to FW flashing state\n");
258 svc_reset_onoff(arche_pdata->svc_reset, arche_pdata->is_reset_act_hi);
260 gpiod_set_value(arche_pdata->svc_sysboot, 1);
262 usleep_range(100, 200);
264 ret = clk_prepare_enable(arche_pdata->svc_ref_clk);
266 dev_err(arche_pdata->dev, "failed to enable svc_ref_clk: %d\n",
271 svc_reset_onoff(arche_pdata->svc_reset, !arche_pdata->is_reset_act_hi);
273 arche_platform_set_state(arche_pdata, ARCHE_PLATFORM_STATE_FW_FLASHING);
279 * Requires arche_pdata->platform_state_mutex to be held
282 arche_platform_poweroff_seq(struct arche_platform_drvdata *arche_pdata)
286 if (arche_pdata->state == ARCHE_PLATFORM_STATE_OFF)
289 /* If in fw_flashing mode, then no need to repeate things again */
290 if (arche_pdata->state != ARCHE_PLATFORM_STATE_FW_FLASHING) {
291 disable_irq(arche_pdata->wake_detect_irq);
293 spin_lock_irqsave(&arche_pdata->wake_lock, flags);
294 arche_platform_set_wake_detect_state(arche_pdata,
296 spin_unlock_irqrestore(&arche_pdata->wake_lock, flags);
299 clk_disable_unprepare(arche_pdata->svc_ref_clk);
301 /* As part of exit, put APB back in reset state */
302 svc_reset_onoff(arche_pdata->svc_reset, arche_pdata->is_reset_act_hi);
304 arche_platform_set_state(arche_pdata, ARCHE_PLATFORM_STATE_OFF);
307 static ssize_t state_store(struct device *dev,
308 struct device_attribute *attr,
309 const char *buf, size_t count)
311 struct arche_platform_drvdata *arche_pdata = dev_get_drvdata(dev);
314 mutex_lock(&arche_pdata->platform_state_mutex);
316 if (sysfs_streq(buf, "off")) {
317 if (arche_pdata->state == ARCHE_PLATFORM_STATE_OFF)
320 /* If SVC goes down, bring down APB's as well */
321 device_for_each_child(arche_pdata->dev, NULL, apb_poweroff);
323 arche_platform_poweroff_seq(arche_pdata);
325 } else if (sysfs_streq(buf, "active")) {
326 if (arche_pdata->state == ARCHE_PLATFORM_STATE_ACTIVE)
329 /* First we want to make sure we power off everything
330 * and then activate back again
332 device_for_each_child(arche_pdata->dev, NULL, apb_poweroff);
333 arche_platform_poweroff_seq(arche_pdata);
335 arche_platform_wd_irq_en(arche_pdata);
336 ret = arche_platform_coldboot_seq(arche_pdata);
340 } else if (sysfs_streq(buf, "standby")) {
341 if (arche_pdata->state == ARCHE_PLATFORM_STATE_STANDBY)
344 dev_warn(arche_pdata->dev, "standby state not supported\n");
345 } else if (sysfs_streq(buf, "fw_flashing")) {
346 if (arche_pdata->state == ARCHE_PLATFORM_STATE_FW_FLASHING)
350 * Here we only control SVC.
352 * In case of FW_FLASHING mode we do not want to control
353 * APBs, as in case of V2, SPI bus is shared between both
354 * the APBs. So let user chose which APB he wants to flash.
356 arche_platform_poweroff_seq(arche_pdata);
358 ret = arche_platform_fw_flashing_seq(arche_pdata);
362 dev_err(arche_pdata->dev, "unknown state\n");
367 mutex_unlock(&arche_pdata->platform_state_mutex);
368 return ret ? ret : count;
371 static ssize_t state_show(struct device *dev,
372 struct device_attribute *attr, char *buf)
374 struct arche_platform_drvdata *arche_pdata = dev_get_drvdata(dev);
376 switch (arche_pdata->state) {
377 case ARCHE_PLATFORM_STATE_OFF:
378 return sprintf(buf, "off\n");
379 case ARCHE_PLATFORM_STATE_ACTIVE:
380 return sprintf(buf, "active\n");
381 case ARCHE_PLATFORM_STATE_STANDBY:
382 return sprintf(buf, "standby\n");
383 case ARCHE_PLATFORM_STATE_FW_FLASHING:
384 return sprintf(buf, "fw_flashing\n");
386 return sprintf(buf, "unknown state\n");
390 static DEVICE_ATTR_RW(state);
392 static int arche_platform_pm_notifier(struct notifier_block *notifier,
393 unsigned long pm_event, void *unused)
395 struct arche_platform_drvdata *arche_pdata =
396 container_of(notifier, struct arche_platform_drvdata,
398 int ret = NOTIFY_DONE;
400 mutex_lock(&arche_pdata->platform_state_mutex);
402 case PM_SUSPEND_PREPARE:
403 if (arche_pdata->state != ARCHE_PLATFORM_STATE_ACTIVE) {
407 device_for_each_child(arche_pdata->dev, NULL, apb_poweroff);
408 arche_platform_poweroff_seq(arche_pdata);
410 case PM_POST_SUSPEND:
411 if (arche_pdata->state != ARCHE_PLATFORM_STATE_OFF)
414 arche_platform_wd_irq_en(arche_pdata);
415 arche_platform_coldboot_seq(arche_pdata);
420 mutex_unlock(&arche_pdata->platform_state_mutex);
425 static int arche_platform_probe(struct platform_device *pdev)
427 struct arche_platform_drvdata *arche_pdata;
428 struct device *dev = &pdev->dev;
429 struct device_node *np = dev->of_node;
433 arche_pdata = devm_kzalloc(&pdev->dev, sizeof(*arche_pdata),
438 /* setup svc reset gpio */
439 arche_pdata->is_reset_act_hi = of_property_read_bool(np,
440 "svc,reset-active-high");
441 if (arche_pdata->is_reset_act_hi)
442 flags = GPIOD_OUT_HIGH;
444 flags = GPIOD_OUT_LOW;
446 arche_pdata->svc_reset = devm_gpiod_get(dev, "svc,reset", flags);
447 if (IS_ERR(arche_pdata->svc_reset)) {
448 ret = PTR_ERR(arche_pdata->svc_reset);
449 dev_err(dev, "failed to request svc-reset GPIO: %d\n", ret);
452 arche_platform_set_state(arche_pdata, ARCHE_PLATFORM_STATE_OFF);
454 arche_pdata->svc_sysboot = devm_gpiod_get(dev, "svc,sysboot",
456 if (IS_ERR(arche_pdata->svc_sysboot)) {
457 ret = PTR_ERR(arche_pdata->svc_sysboot);
458 dev_err(dev, "failed to request sysboot0 GPIO: %d\n", ret);
462 /* setup the clock request gpio first */
463 arche_pdata->svc_refclk_req = devm_gpiod_get(dev, "svc,refclk-req",
465 if (IS_ERR(arche_pdata->svc_refclk_req)) {
466 ret = PTR_ERR(arche_pdata->svc_refclk_req);
467 dev_err(dev, "failed to request svc-clk-req GPIO: %d\n", ret);
471 /* setup refclk2 to follow the pin */
472 arche_pdata->svc_ref_clk = devm_clk_get(dev, "svc_ref_clk");
473 if (IS_ERR(arche_pdata->svc_ref_clk)) {
474 ret = PTR_ERR(arche_pdata->svc_ref_clk);
475 dev_err(dev, "failed to get svc_ref_clk: %d\n", ret);
479 platform_set_drvdata(pdev, arche_pdata);
481 arche_pdata->num_apbs = of_get_child_count(np);
482 dev_dbg(dev, "Number of APB's available - %d\n", arche_pdata->num_apbs);
484 arche_pdata->wake_detect = devm_gpiod_get(dev, "svc,wake-detect",
486 if (IS_ERR(arche_pdata->wake_detect)) {
487 ret = PTR_ERR(arche_pdata->wake_detect);
488 dev_err(dev, "Failed requesting wake_detect GPIO: %d\n", ret);
492 arche_platform_set_wake_detect_state(arche_pdata, WD_STATE_IDLE);
494 arche_pdata->dev = &pdev->dev;
496 spin_lock_init(&arche_pdata->wake_lock);
497 mutex_init(&arche_pdata->platform_state_mutex);
498 arche_pdata->wake_detect_irq =
499 gpiod_to_irq(arche_pdata->wake_detect);
501 ret = devm_request_threaded_irq(dev, arche_pdata->wake_detect_irq,
502 arche_platform_wd_irq,
503 arche_platform_wd_irq_thread,
504 IRQF_TRIGGER_FALLING |
505 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
506 dev_name(dev), arche_pdata);
508 dev_err(dev, "failed to request wake detect IRQ %d\n", ret);
511 disable_irq(arche_pdata->wake_detect_irq);
513 ret = device_create_file(dev, &dev_attr_state);
515 dev_err(dev, "failed to create state file in sysfs\n");
519 ret = of_platform_populate(np, NULL, NULL, dev);
521 dev_err(dev, "failed to populate child nodes %d\n", ret);
522 goto err_device_remove;
525 arche_pdata->pm_notifier.notifier_call = arche_platform_pm_notifier;
526 ret = register_pm_notifier(&arche_pdata->pm_notifier);
529 dev_err(dev, "failed to register pm notifier %d\n", ret);
530 goto err_device_remove;
533 /* Explicitly power off if requested */
534 if (!of_property_read_bool(pdev->dev.of_node, "arche,init-off")) {
535 mutex_lock(&arche_pdata->platform_state_mutex);
536 ret = arche_platform_coldboot_seq(arche_pdata);
538 dev_err(dev, "Failed to cold boot svc %d\n", ret);
541 arche_platform_wd_irq_en(arche_pdata);
542 mutex_unlock(&arche_pdata->platform_state_mutex);
545 dev_info(dev, "Device registered successfully\n");
549 mutex_unlock(&arche_pdata->platform_state_mutex);
551 device_remove_file(&pdev->dev, &dev_attr_state);
555 static int arche_remove_child(struct device *dev, void *unused)
557 struct platform_device *pdev = to_platform_device(dev);
559 platform_device_unregister(pdev);
564 static int arche_platform_remove(struct platform_device *pdev)
566 struct arche_platform_drvdata *arche_pdata = platform_get_drvdata(pdev);
568 unregister_pm_notifier(&arche_pdata->pm_notifier);
569 device_remove_file(&pdev->dev, &dev_attr_state);
570 device_for_each_child(&pdev->dev, NULL, arche_remove_child);
571 arche_platform_poweroff_seq(arche_pdata);
573 if (usb3613_hub_mode_ctrl(false))
574 dev_warn(arche_pdata->dev, "failed to control hub device\n");
575 /* TODO: Should we do anything more here ?? */
579 static __maybe_unused int arche_platform_suspend(struct device *dev)
582 * If timing profile premits, we may shutdown bridge
587 * Also, need to make sure we meet precondition for unipro suspend
588 * Precondition: Definition ???
593 static __maybe_unused int arche_platform_resume(struct device *dev)
596 * Atleast for ES2 we have to meet the delay requirement between
597 * unipro switch and AP bridge init, depending on whether bridge is in
598 * OFF state or standby state.
600 * Based on whether bridge is in standby or OFF state we may have to
601 * assert multiple signals. Please refer to WDM spec, for more info.
607 static void arche_platform_shutdown(struct platform_device *pdev)
609 struct arche_platform_drvdata *arche_pdata = platform_get_drvdata(pdev);
611 arche_platform_poweroff_seq(arche_pdata);
613 usb3613_hub_mode_ctrl(false);
616 static SIMPLE_DEV_PM_OPS(arche_platform_pm_ops,
617 arche_platform_suspend,
618 arche_platform_resume);
620 static const struct of_device_id arche_platform_of_match[] = {
621 /* Use PID/VID of SVC device */
622 { .compatible = "google,arche-platform", },
625 MODULE_DEVICE_TABLE(of, arche_platform_of_match);
627 static struct platform_driver arche_platform_device_driver = {
628 .probe = arche_platform_probe,
629 .remove = arche_platform_remove,
630 .shutdown = arche_platform_shutdown,
632 .name = "arche-platform-ctrl",
633 .pm = &arche_platform_pm_ops,
634 .of_match_table = arche_platform_of_match,
638 static int __init arche_init(void)
642 retval = platform_driver_register(&arche_platform_device_driver);
646 retval = arche_apb_init();
648 platform_driver_unregister(&arche_platform_device_driver);
652 module_init(arche_init);
654 static void __exit arche_exit(void)
657 platform_driver_unregister(&arche_platform_device_driver);
659 module_exit(arche_exit);
661 MODULE_LICENSE("GPL v2");
662 MODULE_AUTHOR("Vaibhav Hiremath <vaibhav.hiremath@linaro.org>");
663 MODULE_DESCRIPTION("Arche Platform Driver");