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 "arche_platform.h"
22 #include <linux/usb/usb3613.h>
24 struct arche_platform_drvdata {
25 /* Control GPIO signals to and from AP <=> SVC */
29 int wake_detect_gpio; /* bi-dir,maps to WAKE_MOD & WAKE_FRAME signals */
31 enum arche_platform_state state;
33 unsigned int svc_refclk_req;
34 struct clk *svc_ref_clk;
36 struct pinctrl *pinctrl;
37 struct pinctrl_state *pin_default;
41 struct delayed_work delayed_work;
45 static inline void svc_reset_onoff(unsigned int gpio, bool onoff)
47 gpio_set_value(gpio, onoff);
50 static int apb_cold_boot(struct device *dev, void *data)
54 ret = apb_ctrl_coldboot(dev);
56 dev_warn(dev, "failed to coldboot\n");
58 /*Child nodes are independent, so do not exit coldboot operation */
62 static int apb_fw_flashing_state(struct device *dev, void *data)
66 ret = apb_ctrl_fw_flashing(dev);
68 dev_warn(dev, "failed to switch to fw flashing state\n");
70 /*Child nodes are independent, so do not exit coldboot operation */
74 static int apb_poweroff(struct device *dev, void *data)
76 apb_ctrl_poweroff(dev);
82 * svc_delayed_work - Time to give SVC to boot.
84 static void svc_delayed_work(struct work_struct *work)
86 struct arche_platform_drvdata *arche_pdata =
87 container_of(work, struct arche_platform_drvdata, delayed_work.work);
91 * 1. SVC and AP boot independently, with AP<-->SVC wake/detect pin
92 * deasserted (LOW in this case)
93 * 2.1. SVC allows 360 milliseconds to elapse after switch boots to work
94 * around bug described in ENG-330.
95 * 2.2. AP asserts wake/detect pin (HIGH) (this can proceed in parallel with 2.1)
96 * 3. SVC detects assertion of wake/detect pin, and sends "wake out" signal to AP
97 * 4. AP receives "wake out" signal, takes AP Bridges through their power
98 * on reset sequence as defined in the bridge ASIC reference manuals
99 * 5. AP takes USB3613 through its power on reset sequence
100 * 6. AP enumerates AP Bridges
102 gpio_set_value(arche_pdata->wake_detect_gpio, 1);
103 gpio_direction_input(arche_pdata->wake_detect_gpio);
105 /* Read the wake_detect GPIO, for WAKE_OUT event from SVC */
106 if (gpio_get_value(arche_pdata->wake_detect_gpio) == 0)
113 /* FIXME: We may want to limit retries here */
114 dev_warn(arche_pdata->dev,
115 "Timed out on wake/detect, rescheduling handshake\n");
116 gpio_direction_output(arche_pdata->wake_detect_gpio, 0);
117 schedule_delayed_work(&arche_pdata->delayed_work, msecs_to_jiffies(2000));
121 /* Bring APB out of reset: cold boot sequence */
122 device_for_each_child(arche_pdata->dev, NULL, apb_cold_boot);
124 /* re-assert wake_detect to confirm SVC WAKE_OUT */
125 gpio_direction_output(arche_pdata->wake_detect_gpio, 1);
127 /* Enable HUB3613 into HUB mode. */
128 if (usb3613_hub_mode_ctrl(true))
129 dev_warn(arche_pdata->dev, "failed to control hub device\n");
132 static int arche_platform_coldboot_seq(struct arche_platform_drvdata *arche_pdata)
136 if (arche_pdata->state == ARCHE_PLATFORM_STATE_ACTIVE)
139 dev_info(arche_pdata->dev, "Booting from cold boot state\n");
141 svc_reset_onoff(arche_pdata->svc_reset_gpio,
142 arche_pdata->is_reset_act_hi);
144 gpio_set_value(arche_pdata->svc_sysboot_gpio, 0);
145 usleep_range(100, 200);
147 ret = clk_prepare_enable(arche_pdata->svc_ref_clk);
149 dev_err(arche_pdata->dev, "failed to enable svc_ref_clk: %d\n",
154 /* bring SVC out of reset */
155 svc_reset_onoff(arche_pdata->svc_reset_gpio,
156 !arche_pdata->is_reset_act_hi);
158 arche_pdata->state = ARCHE_PLATFORM_STATE_ACTIVE;
163 static void arche_platform_fw_flashing_seq(struct arche_platform_drvdata *arche_pdata)
165 if (arche_pdata->state == ARCHE_PLATFORM_STATE_FW_FLASHING)
168 dev_info(arche_pdata->dev, "Switching to FW flashing state\n");
170 svc_reset_onoff(arche_pdata->svc_reset_gpio,
171 arche_pdata->is_reset_act_hi);
173 gpio_set_value(arche_pdata->svc_sysboot_gpio, 1);
175 usleep_range(100, 200);
176 svc_reset_onoff(arche_pdata->svc_reset_gpio,
177 !arche_pdata->is_reset_act_hi);
179 arche_pdata->state = ARCHE_PLATFORM_STATE_FW_FLASHING;
183 static void arche_platform_poweroff_seq(struct arche_platform_drvdata *arche_pdata)
185 if (arche_pdata->state == ARCHE_PLATFORM_STATE_OFF)
188 /* Send disconnect/detach event to SVC */
189 gpio_set_value(arche_pdata->wake_detect_gpio, 0);
190 usleep_range(100, 200);
192 clk_disable_unprepare(arche_pdata->svc_ref_clk);
193 /* As part of exit, put APB back in reset state */
194 svc_reset_onoff(arche_pdata->svc_reset_gpio,
195 arche_pdata->is_reset_act_hi);
197 arche_pdata->state = ARCHE_PLATFORM_STATE_OFF;
200 static ssize_t state_store(struct device *dev,
201 struct device_attribute *attr, const char *buf, size_t count)
203 struct platform_device *pdev = to_platform_device(dev);
204 struct arche_platform_drvdata *arche_pdata = platform_get_drvdata(pdev);
207 if (sysfs_streq(buf, "off")) {
208 if (arche_pdata->state == ARCHE_PLATFORM_STATE_OFF)
211 /* If SVC goes down, bring down APB's as well */
212 device_for_each_child(arche_pdata->dev, NULL, apb_poweroff);
214 arche_platform_poweroff_seq(arche_pdata);
216 ret = usb3613_hub_mode_ctrl(false);
218 dev_warn(arche_pdata->dev, "failed to control hub device\n");
219 /* TODO: Should we do anything more here ?? */
220 } else if (sysfs_streq(buf, "active")) {
221 if (arche_pdata->state == ARCHE_PLATFORM_STATE_ACTIVE)
224 ret = arche_platform_coldboot_seq(arche_pdata);
225 /* Give enough time for SVC to boot and then handshake with SVC */
226 schedule_delayed_work(&arche_pdata->delayed_work, msecs_to_jiffies(2000));
227 } else if (sysfs_streq(buf, "standby")) {
228 if (arche_pdata->state == ARCHE_PLATFORM_STATE_STANDBY)
231 dev_warn(arche_pdata->dev, "standby state not supported\n");
232 } else if (sysfs_streq(buf, "fw_flashing")) {
233 if (arche_pdata->state == ARCHE_PLATFORM_STATE_FW_FLASHING)
236 /* First we want to make sure we power off everything
237 * and then enter FW flashing state */
238 device_for_each_child(arche_pdata->dev, NULL, apb_poweroff);
240 arche_platform_poweroff_seq(arche_pdata);
242 ret = usb3613_hub_mode_ctrl(false);
244 dev_warn(arche_pdata->dev, "failed to control hub device\n");
245 /* TODO: Should we do anything more here ?? */
247 arche_platform_fw_flashing_seq(arche_pdata);
249 device_for_each_child(arche_pdata->dev, NULL, apb_fw_flashing_state);
251 dev_err(arche_pdata->dev, "unknown state\n");
255 return ret ? ret : count;
258 static ssize_t state_show(struct device *dev,
259 struct device_attribute *attr, char *buf)
261 struct arche_platform_drvdata *arche_pdata = dev_get_drvdata(dev);
263 switch (arche_pdata->state) {
264 case ARCHE_PLATFORM_STATE_OFF:
265 return sprintf(buf, "off\n");
266 case ARCHE_PLATFORM_STATE_ACTIVE:
267 return sprintf(buf, "active\n");
268 case ARCHE_PLATFORM_STATE_STANDBY:
269 return sprintf(buf, "standby\n");
270 case ARCHE_PLATFORM_STATE_FW_FLASHING:
271 return sprintf(buf, "fw_flashing\n");
273 return sprintf(buf, "unknown state\n");
277 static DEVICE_ATTR_RW(state);
279 static int arche_platform_probe(struct platform_device *pdev)
281 struct arche_platform_drvdata *arche_pdata;
282 struct device *dev = &pdev->dev;
283 struct device_node *np = dev->of_node;
286 arche_pdata = devm_kzalloc(&pdev->dev, sizeof(*arche_pdata), GFP_KERNEL);
290 /* setup svc reset gpio */
291 arche_pdata->is_reset_act_hi = of_property_read_bool(np,
292 "svc,reset-active-high");
293 arche_pdata->svc_reset_gpio = of_get_named_gpio(np, "svc,reset-gpio", 0);
294 if (arche_pdata->svc_reset_gpio < 0) {
295 dev_err(dev, "failed to get reset-gpio\n");
296 return arche_pdata->svc_reset_gpio;
298 ret = devm_gpio_request(dev, arche_pdata->svc_reset_gpio, "svc-reset");
300 dev_err(dev, "failed to request svc-reset gpio:%d\n", ret);
303 ret = gpio_direction_output(arche_pdata->svc_reset_gpio,
304 arche_pdata->is_reset_act_hi);
306 dev_err(dev, "failed to set svc-reset gpio dir:%d\n", ret);
309 arche_pdata->state = ARCHE_PLATFORM_STATE_OFF;
311 arche_pdata->svc_sysboot_gpio = of_get_named_gpio(np,
312 "svc,sysboot-gpio", 0);
313 if (arche_pdata->svc_sysboot_gpio < 0) {
314 dev_err(dev, "failed to get sysboot gpio\n");
315 return arche_pdata->svc_sysboot_gpio;
317 ret = devm_gpio_request(dev, arche_pdata->svc_sysboot_gpio, "sysboot0");
319 dev_err(dev, "failed to request sysboot0 gpio:%d\n", ret);
322 ret = gpio_direction_output(arche_pdata->svc_sysboot_gpio, 0);
324 dev_err(dev, "failed to set svc-reset gpio dir:%d\n", ret);
328 /* setup the clock request gpio first */
329 arche_pdata->svc_refclk_req = of_get_named_gpio(np,
330 "svc,refclk-req-gpio", 0);
331 if (arche_pdata->svc_refclk_req < 0) {
332 dev_err(dev, "failed to get svc clock-req gpio\n");
333 return arche_pdata->svc_refclk_req;
335 ret = devm_gpio_request(dev, arche_pdata->svc_refclk_req, "svc-clk-req");
337 dev_err(dev, "failed to request svc-clk-req gpio: %d\n", ret);
340 ret = gpio_direction_input(arche_pdata->svc_refclk_req);
342 dev_err(dev, "failed to set svc-clk-req gpio dir :%d\n", ret);
346 /* setup refclk2 to follow the pin */
347 arche_pdata->svc_ref_clk = devm_clk_get(dev, "svc_ref_clk");
348 if (IS_ERR(arche_pdata->svc_ref_clk)) {
349 ret = PTR_ERR(arche_pdata->svc_ref_clk);
350 dev_err(dev, "failed to get svc_ref_clk: %d\n", ret);
354 platform_set_drvdata(pdev, arche_pdata);
356 arche_pdata->num_apbs = of_get_child_count(np);
357 dev_dbg(dev, "Number of APB's available - %d\n", arche_pdata->num_apbs);
359 arche_pdata->wake_detect_gpio = of_get_named_gpio(np, "svc,wake-detect-gpio", 0);
360 if (arche_pdata->wake_detect_gpio < 0) {
361 dev_err(dev, "failed to get wake detect gpio\n");
362 ret = arche_pdata->wake_detect_gpio;
366 ret = devm_gpio_request(dev, arche_pdata->wake_detect_gpio, "wake detect");
368 dev_err(dev, "Failed requesting wake_detect gpio %d\n",
369 arche_pdata->wake_detect_gpio);
372 /* deassert wake detect */
373 gpio_direction_output(arche_pdata->wake_detect_gpio, 0);
375 arche_pdata->dev = &pdev->dev;
377 ret = device_create_file(dev, &dev_attr_state);
379 dev_err(dev, "failed to create state file in sysfs\n");
383 ret = arche_platform_coldboot_seq(arche_pdata);
385 dev_err(dev, "Failed to cold boot svc %d\n", ret);
389 ret = of_platform_populate(np, NULL, NULL, dev);
391 dev_err(dev, "failed to populate child nodes %d\n", ret);
395 INIT_DELAYED_WORK(&arche_pdata->delayed_work, svc_delayed_work);
396 schedule_delayed_work(&arche_pdata->delayed_work, msecs_to_jiffies(2000));
398 dev_info(dev, "Device registered successfully\n");
402 static int arche_remove_child(struct device *dev, void *unused)
404 struct platform_device *pdev = to_platform_device(dev);
406 platform_device_unregister(pdev);
411 static int arche_platform_remove(struct platform_device *pdev)
413 struct arche_platform_drvdata *arche_pdata = platform_get_drvdata(pdev);
415 device_remove_file(&pdev->dev, &dev_attr_state);
416 cancel_delayed_work_sync(&arche_pdata->delayed_work);
417 device_for_each_child(&pdev->dev, NULL, arche_remove_child);
418 arche_platform_poweroff_seq(arche_pdata);
419 platform_set_drvdata(pdev, NULL);
421 if (usb3613_hub_mode_ctrl(false))
422 dev_warn(arche_pdata->dev, "failed to control hub device\n");
423 /* TODO: Should we do anything more here ?? */
427 static int arche_platform_suspend(struct device *dev)
430 * If timing profile premits, we may shutdown bridge
435 * Also, need to make sure we meet precondition for unipro suspend
436 * Precondition: Definition ???
441 static int arche_platform_resume(struct device *dev)
444 * Atleast for ES2 we have to meet the delay requirement between
445 * unipro switch and AP bridge init, depending on whether bridge is in
446 * OFF state or standby state.
448 * Based on whether bridge is in standby or OFF state we may have to
449 * assert multiple signals. Please refer to WDM spec, for more info.
455 static SIMPLE_DEV_PM_OPS(arche_platform_pm_ops,
456 arche_platform_suspend,
457 arche_platform_resume);
459 static struct of_device_id arche_platform_of_match[] = {
460 { .compatible = "google,arche-platform", }, /* Use PID/VID of SVC device */
464 static struct of_device_id arche_apb_ctrl_of_match[] = {
465 { .compatible = "usbffff,2", },
469 static struct of_device_id arche_combined_id[] = {
470 { .compatible = "google,arche-platform", }, /* Use PID/VID of SVC device */
471 { .compatible = "usbffff,2", },
474 MODULE_DEVICE_TABLE(of, arche_combined_id);
476 static struct platform_driver arche_platform_device_driver = {
477 .probe = arche_platform_probe,
478 .remove = arche_platform_remove,
480 .name = "arche-platform-ctrl",
481 .pm = &arche_platform_pm_ops,
482 .of_match_table = arche_platform_of_match,
486 static struct platform_driver arche_apb_ctrl_device_driver = {
487 .probe = arche_apb_ctrl_probe,
488 .remove = arche_apb_ctrl_remove,
490 .name = "arche-apb-ctrl",
491 .pm = &arche_apb_ctrl_pm_ops,
492 .of_match_table = arche_apb_ctrl_of_match,
496 static int __init arche_init(void)
500 retval = platform_driver_register(&arche_platform_device_driver);
504 retval = platform_driver_register(&arche_apb_ctrl_device_driver);
506 platform_driver_unregister(&arche_platform_device_driver);
510 module_init(arche_init);
512 static void __exit arche_exit(void)
514 platform_driver_unregister(&arche_apb_ctrl_device_driver);
515 platform_driver_unregister(&arche_platform_device_driver);
517 module_exit(arche_exit);
519 MODULE_LICENSE("GPL");
520 MODULE_AUTHOR("Vaibhav Hiremath <vaibhav.hiremath@linaro.org>");
521 MODULE_DESCRIPTION("Arche Platform Driver");