greybus: arche-platform: Control usb3613 from arche-platform
[linux-2.6/btrfs-unstable.git] / drivers / staging / greybus / arche-platform.c
blob591cf9dc507144b52bc4805a6593fdf958887c96
1 /*
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.
8 */
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>
19 #include <linux/pm.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 */
26 int svc_reset_gpio;
27 bool is_reset_act_hi;
28 int svc_sysboot_gpio;
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;
39 int num_apbs;
41 struct delayed_work delayed_work;
42 struct device *dev;
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)
52 int ret;
54 ret = apb_ctrl_coldboot(dev);
55 if (ret)
56 dev_warn(dev, "failed to coldboot\n");
58 /*Child nodes are independent, so do not exit coldboot operation */
59 return 0;
62 static int apb_fw_flashing_state(struct device *dev, void *data)
64 int ret;
66 ret = apb_ctrl_fw_flashing(dev);
67 if (ret)
68 dev_warn(dev, "failed to switch to fw flashing state\n");
70 /*Child nodes are independent, so do not exit coldboot operation */
71 return 0;
74 static int apb_poweroff(struct device *dev, void *data)
76 apb_ctrl_poweroff(dev);
78 return 0;
81 /**
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);
88 int timeout = 50;
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);
104 do {
105 /* Read the wake_detect GPIO, for WAKE_OUT event from SVC */
106 if (gpio_get_value(arche_pdata->wake_detect_gpio) == 0)
107 break;
109 msleep(100);
110 } while(timeout--);
112 if (timeout < 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));
118 return;
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)
134 int ret;
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);
145 if (ret) {
146 dev_err(arche_pdata->dev, "failed to enable svc_ref_clk: %d\n",
147 ret);
148 return ret;
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;
157 return 0;
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);
196 int ret = 0;
198 if (sysfs_streq(buf, "off")) {
199 if (arche_pdata->state == ARCHE_PLATFORM_STATE_OFF)
200 return count;
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);
208 if (ret)
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)
213 return count;
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)
220 return count;
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)
225 return count;
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);
234 if (ret)
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);
241 } else {
242 dev_err(arche_pdata->dev, "unknown state\n");
243 ret = -EINVAL;
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");
263 default:
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;
275 int ret;
277 arche_pdata = devm_kzalloc(&pdev->dev, sizeof(*arche_pdata), GFP_KERNEL);
278 if (!arche_pdata)
279 return -ENOMEM;
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");
290 if (ret) {
291 dev_err(dev, "failed to request svc-reset gpio:%d\n", ret);
292 return ret;
294 ret = gpio_direction_output(arche_pdata->svc_reset_gpio,
295 arche_pdata->is_reset_act_hi);
296 if (ret) {
297 dev_err(dev, "failed to set svc-reset gpio dir:%d\n", ret);
298 return 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");
309 if (ret) {
310 dev_err(dev, "failed to request sysboot0 gpio:%d\n", ret);
311 return ret;
313 ret = gpio_direction_output(arche_pdata->svc_sysboot_gpio, 0);
314 if (ret) {
315 dev_err(dev, "failed to set svc-reset gpio dir:%d\n", ret);
316 return 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");
327 if (ret) {
328 dev_err(dev, "failed to request svc-clk-req gpio: %d\n", ret);
329 return ret;
331 ret = gpio_direction_input(arche_pdata->svc_refclk_req);
332 if (ret) {
333 dev_err(dev, "failed to set svc-clk-req gpio dir :%d\n", ret);
334 return 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);
342 return 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;
354 return ret;
357 ret = devm_gpio_request(dev, arche_pdata->wake_detect_gpio, "wake detect");
358 if (ret) {
359 dev_err(dev, "Failed requesting wake_detect gpio %d\n",
360 arche_pdata->wake_detect_gpio);
361 return ret;
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);
369 if (ret) {
370 dev_err(dev, "failed to create state file in sysfs\n");
371 return ret;
374 ret = arche_platform_coldboot_seq(arche_pdata);
375 if (ret) {
376 dev_err(dev, "Failed to cold boot svc %d\n", ret);
377 return ret;
380 ret = of_platform_populate(np, NULL, NULL, dev);
381 if (ret) {
382 dev_err(dev, "failed to populate child nodes %d\n", ret);
383 return 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");
390 return 0;
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);
399 return 0;
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 ?? */
415 return 0;
418 static int arche_platform_suspend(struct device *dev)
421 * If timing profile premits, we may shutdown bridge
422 * completely
424 * TODO: sequence ??
426 * Also, need to make sure we meet precondition for unipro suspend
427 * Precondition: Definition ???
429 return 0;
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.
443 return 0;
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 */
452 { },
455 static struct of_device_id arche_apb_ctrl_of_match[] = {
456 { .compatible = "usbffff,2", },
457 { },
460 static struct of_device_id arche_combined_id[] = {
461 { .compatible = "google,arche-platform", }, /* Use PID/VID of SVC device */
462 { .compatible = "usbffff,2", },
463 { },
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,
470 .driver = {
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,
480 .driver = {
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)
489 int retval;
491 retval = platform_driver_register(&arche_platform_device_driver);
492 if (retval)
493 return retval;
495 retval = platform_driver_register(&arche_apb_ctrl_device_driver);
496 if (retval)
497 platform_driver_unregister(&arche_platform_device_driver);
499 return retval;
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");