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 dev_info(arche_pdata
->dev
, "Booting from cold boot state\n");
138 svc_reset_onoff(arche_pdata
->svc_reset_gpio
,
139 arche_pdata
->is_reset_act_hi
);
141 gpio_set_value(arche_pdata
->svc_sysboot_gpio
, 0);
142 usleep_range(100, 200);
144 ret
= clk_prepare_enable(arche_pdata
->svc_ref_clk
);
146 dev_err(arche_pdata
->dev
, "failed to enable svc_ref_clk: %d\n",
151 /* bring SVC out of reset */
152 svc_reset_onoff(arche_pdata
->svc_reset_gpio
,
153 !arche_pdata
->is_reset_act_hi
);
155 arche_pdata
->state
= ARCHE_PLATFORM_STATE_ACTIVE
;
160 static void arche_platform_fw_flashing_seq(struct arche_platform_drvdata
*arche_pdata
)
162 dev_info(arche_pdata
->dev
, "Switching to FW flashing state\n");
164 svc_reset_onoff(arche_pdata
->svc_reset_gpio
,
165 arche_pdata
->is_reset_act_hi
);
167 gpio_set_value(arche_pdata
->svc_sysboot_gpio
, 1);
169 usleep_range(100, 200);
170 svc_reset_onoff(arche_pdata
->svc_reset_gpio
,
171 !arche_pdata
->is_reset_act_hi
);
173 arche_pdata
->state
= ARCHE_PLATFORM_STATE_FW_FLASHING
;
177 static void arche_platform_poweroff_seq(struct arche_platform_drvdata
*arche_pdata
)
179 /* Send disconnect/detach event to SVC */
180 gpio_set_value(arche_pdata
->wake_detect_gpio
, 0);
181 usleep_range(100, 200);
183 clk_disable_unprepare(arche_pdata
->svc_ref_clk
);
184 /* As part of exit, put APB back in reset state */
185 svc_reset_onoff(arche_pdata
->svc_reset_gpio
,
186 arche_pdata
->is_reset_act_hi
);
188 arche_pdata
->state
= ARCHE_PLATFORM_STATE_OFF
;
191 static ssize_t
state_store(struct device
*dev
,
192 struct device_attribute
*attr
, const char *buf
, size_t count
)
194 struct platform_device
*pdev
= to_platform_device(dev
);
195 struct arche_platform_drvdata
*arche_pdata
= platform_get_drvdata(pdev
);
198 if (sysfs_streq(buf
, "off")) {
199 if (arche_pdata
->state
== ARCHE_PLATFORM_STATE_OFF
)
202 /* If SVC goes down, bring down APB's as well */
203 device_for_each_child(arche_pdata
->dev
, NULL
, apb_poweroff
);
205 arche_platform_poweroff_seq(arche_pdata
);
207 ret
= usb3613_hub_mode_ctrl(false);
209 dev_warn(arche_pdata
->dev
, "failed to control hub device\n");
210 /* TODO: Should we do anything more here ?? */
211 } else if (sysfs_streq(buf
, "active")) {
212 if (arche_pdata
->state
== ARCHE_PLATFORM_STATE_ACTIVE
)
215 ret
= arche_platform_coldboot_seq(arche_pdata
);
216 /* Give enough time for SVC to boot and then handshake with SVC */
217 schedule_delayed_work(&arche_pdata
->delayed_work
, msecs_to_jiffies(2000));
218 } else if (sysfs_streq(buf
, "standby")) {
219 if (arche_pdata
->state
== ARCHE_PLATFORM_STATE_STANDBY
)
222 dev_warn(arche_pdata
->dev
, "standby state not supported\n");
223 } else if (sysfs_streq(buf
, "fw_flashing")) {
224 if (arche_pdata
->state
== ARCHE_PLATFORM_STATE_FW_FLASHING
)
227 /* First we want to make sure we power off everything
228 * and then enter FW flashing state */
229 device_for_each_child(arche_pdata
->dev
, NULL
, apb_poweroff
);
231 arche_platform_poweroff_seq(arche_pdata
);
233 ret
= usb3613_hub_mode_ctrl(false);
235 dev_warn(arche_pdata
->dev
, "failed to control hub device\n");
236 /* TODO: Should we do anything more here ?? */
238 arche_platform_fw_flashing_seq(arche_pdata
);
240 device_for_each_child(arche_pdata
->dev
, NULL
, apb_fw_flashing_state
);
242 dev_err(arche_pdata
->dev
, "unknown state\n");
246 return ret
? ret
: count
;
249 static ssize_t
state_show(struct device
*dev
,
250 struct device_attribute
*attr
, char *buf
)
252 struct arche_platform_drvdata
*arche_pdata
= dev_get_drvdata(dev
);
254 switch (arche_pdata
->state
) {
255 case ARCHE_PLATFORM_STATE_OFF
:
256 return sprintf(buf
, "off\n");
257 case ARCHE_PLATFORM_STATE_ACTIVE
:
258 return sprintf(buf
, "active\n");
259 case ARCHE_PLATFORM_STATE_STANDBY
:
260 return sprintf(buf
, "standby\n");
261 case ARCHE_PLATFORM_STATE_FW_FLASHING
:
262 return sprintf(buf
, "fw_flashing\n");
264 return sprintf(buf
, "unknown state\n");
268 static DEVICE_ATTR_RW(state
);
270 static int arche_platform_probe(struct platform_device
*pdev
)
272 struct arche_platform_drvdata
*arche_pdata
;
273 struct device
*dev
= &pdev
->dev
;
274 struct device_node
*np
= dev
->of_node
;
277 arche_pdata
= devm_kzalloc(&pdev
->dev
, sizeof(*arche_pdata
), GFP_KERNEL
);
281 /* setup svc reset gpio */
282 arche_pdata
->is_reset_act_hi
= of_property_read_bool(np
,
283 "svc,reset-active-high");
284 arche_pdata
->svc_reset_gpio
= of_get_named_gpio(np
, "svc,reset-gpio", 0);
285 if (arche_pdata
->svc_reset_gpio
< 0) {
286 dev_err(dev
, "failed to get reset-gpio\n");
287 return arche_pdata
->svc_reset_gpio
;
289 ret
= devm_gpio_request(dev
, arche_pdata
->svc_reset_gpio
, "svc-reset");
291 dev_err(dev
, "failed to request svc-reset gpio:%d\n", ret
);
294 ret
= gpio_direction_output(arche_pdata
->svc_reset_gpio
,
295 arche_pdata
->is_reset_act_hi
);
297 dev_err(dev
, "failed to set svc-reset gpio dir:%d\n", ret
);
300 arche_pdata
->state
= ARCHE_PLATFORM_STATE_OFF
;
302 arche_pdata
->svc_sysboot_gpio
= of_get_named_gpio(np
,
303 "svc,sysboot-gpio", 0);
304 if (arche_pdata
->svc_sysboot_gpio
< 0) {
305 dev_err(dev
, "failed to get sysboot gpio\n");
306 return arche_pdata
->svc_sysboot_gpio
;
308 ret
= devm_gpio_request(dev
, arche_pdata
->svc_sysboot_gpio
, "sysboot0");
310 dev_err(dev
, "failed to request sysboot0 gpio:%d\n", ret
);
313 ret
= gpio_direction_output(arche_pdata
->svc_sysboot_gpio
, 0);
315 dev_err(dev
, "failed to set svc-reset gpio dir:%d\n", ret
);
319 /* setup the clock request gpio first */
320 arche_pdata
->svc_refclk_req
= of_get_named_gpio(np
,
321 "svc,refclk-req-gpio", 0);
322 if (arche_pdata
->svc_refclk_req
< 0) {
323 dev_err(dev
, "failed to get svc clock-req gpio\n");
324 return arche_pdata
->svc_refclk_req
;
326 ret
= devm_gpio_request(dev
, arche_pdata
->svc_refclk_req
, "svc-clk-req");
328 dev_err(dev
, "failed to request svc-clk-req gpio: %d\n", ret
);
331 ret
= gpio_direction_input(arche_pdata
->svc_refclk_req
);
333 dev_err(dev
, "failed to set svc-clk-req gpio dir :%d\n", ret
);
337 /* setup refclk2 to follow the pin */
338 arche_pdata
->svc_ref_clk
= devm_clk_get(dev
, "svc_ref_clk");
339 if (IS_ERR(arche_pdata
->svc_ref_clk
)) {
340 ret
= PTR_ERR(arche_pdata
->svc_ref_clk
);
341 dev_err(dev
, "failed to get svc_ref_clk: %d\n", ret
);
345 platform_set_drvdata(pdev
, arche_pdata
);
347 arche_pdata
->num_apbs
= of_get_child_count(np
);
348 dev_dbg(dev
, "Number of APB's available - %d\n", arche_pdata
->num_apbs
);
350 arche_pdata
->wake_detect_gpio
= of_get_named_gpio(np
, "svc,wake-detect-gpio", 0);
351 if (arche_pdata
->wake_detect_gpio
< 0) {
352 dev_err(dev
, "failed to get wake detect gpio\n");
353 ret
= arche_pdata
->wake_detect_gpio
;
357 ret
= devm_gpio_request(dev
, arche_pdata
->wake_detect_gpio
, "wake detect");
359 dev_err(dev
, "Failed requesting wake_detect gpio %d\n",
360 arche_pdata
->wake_detect_gpio
);
363 /* deassert wake detect */
364 gpio_direction_output(arche_pdata
->wake_detect_gpio
, 0);
366 arche_pdata
->dev
= &pdev
->dev
;
368 ret
= device_create_file(dev
, &dev_attr_state
);
370 dev_err(dev
, "failed to create state file in sysfs\n");
374 ret
= arche_platform_coldboot_seq(arche_pdata
);
376 dev_err(dev
, "Failed to cold boot svc %d\n", ret
);
380 ret
= of_platform_populate(np
, NULL
, NULL
, dev
);
382 dev_err(dev
, "failed to populate child nodes %d\n", ret
);
386 INIT_DELAYED_WORK(&arche_pdata
->delayed_work
, svc_delayed_work
);
387 schedule_delayed_work(&arche_pdata
->delayed_work
, msecs_to_jiffies(2000));
389 dev_info(dev
, "Device registered successfully\n");
393 static int arche_remove_child(struct device
*dev
, void *unused
)
395 struct platform_device
*pdev
= to_platform_device(dev
);
397 platform_device_unregister(pdev
);
402 static int arche_platform_remove(struct platform_device
*pdev
)
404 struct arche_platform_drvdata
*arche_pdata
= platform_get_drvdata(pdev
);
406 device_remove_file(&pdev
->dev
, &dev_attr_state
);
407 cancel_delayed_work_sync(&arche_pdata
->delayed_work
);
408 device_for_each_child(&pdev
->dev
, NULL
, arche_remove_child
);
409 arche_platform_poweroff_seq(arche_pdata
);
410 platform_set_drvdata(pdev
, NULL
);
412 if (usb3613_hub_mode_ctrl(false))
413 dev_warn(arche_pdata
->dev
, "failed to control hub device\n");
414 /* TODO: Should we do anything more here ?? */
418 static int arche_platform_suspend(struct device
*dev
)
421 * If timing profile premits, we may shutdown bridge
426 * Also, need to make sure we meet precondition for unipro suspend
427 * Precondition: Definition ???
432 static int arche_platform_resume(struct device
*dev
)
435 * Atleast for ES2 we have to meet the delay requirement between
436 * unipro switch and AP bridge init, depending on whether bridge is in
437 * OFF state or standby state.
439 * Based on whether bridge is in standby or OFF state we may have to
440 * assert multiple signals. Please refer to WDM spec, for more info.
446 static SIMPLE_DEV_PM_OPS(arche_platform_pm_ops
,
447 arche_platform_suspend
,
448 arche_platform_resume
);
450 static struct of_device_id arche_platform_of_match
[] = {
451 { .compatible
= "google,arche-platform", }, /* Use PID/VID of SVC device */
455 static struct of_device_id arche_apb_ctrl_of_match
[] = {
456 { .compatible
= "usbffff,2", },
460 static struct of_device_id arche_combined_id
[] = {
461 { .compatible
= "google,arche-platform", }, /* Use PID/VID of SVC device */
462 { .compatible
= "usbffff,2", },
465 MODULE_DEVICE_TABLE(of
, arche_combined_id
);
467 static struct platform_driver arche_platform_device_driver
= {
468 .probe
= arche_platform_probe
,
469 .remove
= arche_platform_remove
,
471 .name
= "arche-platform-ctrl",
472 .pm
= &arche_platform_pm_ops
,
473 .of_match_table
= arche_platform_of_match
,
477 static struct platform_driver arche_apb_ctrl_device_driver
= {
478 .probe
= arche_apb_ctrl_probe
,
479 .remove
= arche_apb_ctrl_remove
,
481 .name
= "arche-apb-ctrl",
482 .pm
= &arche_apb_ctrl_pm_ops
,
483 .of_match_table
= arche_apb_ctrl_of_match
,
487 static int __init
arche_init(void)
491 retval
= platform_driver_register(&arche_platform_device_driver
);
495 retval
= platform_driver_register(&arche_apb_ctrl_device_driver
);
497 platform_driver_unregister(&arche_platform_device_driver
);
501 module_init(arche_init
);
503 static void __exit
arche_exit(void)
505 platform_driver_unregister(&arche_apb_ctrl_device_driver
);
506 platform_driver_unregister(&arche_platform_device_driver
);
508 module_exit(arche_exit
);
510 MODULE_LICENSE("GPL");
511 MODULE_AUTHOR("Vaibhav Hiremath <vaibhav.hiremath@linaro.org>");
512 MODULE_DESCRIPTION("Arche Platform Driver");