Merge remote-tracking branches 'spi/fix/dt', 'spi/fix/fsl-dspi' and 'spi/fix/fsl...
[cascardo/linux.git] / drivers / staging / greybus / arche-platform.c
1 /*
2  * Arche Platform driver to enable Unipro link.
3  *
4  * Copyright 2014-2015 Google Inc.
5  * Copyright 2014-2015 Linaro Ltd.
6  *
7  * Released under the GPLv2 only.
8  */
9
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 <linux/interrupt.h>
21 #include <linux/irq.h>
22 #include <linux/suspend.h>
23 #include <linux/time.h>
24 #include "arche_platform.h"
25 #include "greybus.h"
26
27 #include <linux/usb/usb3613.h>
28
29 #define WD_COLDBOOT_PULSE_WIDTH_MS      30
30
31 enum svc_wakedetect_state {
32         WD_STATE_IDLE,                  /* Default state = pulled high/low */
33         WD_STATE_BOOT_INIT,             /* WD = falling edge (low) */
34         WD_STATE_COLDBOOT_TRIG,         /* WD = rising edge (high), > 30msec */
35         WD_STATE_STANDBYBOOT_TRIG,      /* As of now not used ?? */
36         WD_STATE_COLDBOOT_START,        /* Cold boot process started */
37         WD_STATE_STANDBYBOOT_START,     /* Not used */
38         WD_STATE_TIMESYNC,
39 };
40
41 struct arche_platform_drvdata {
42         /* Control GPIO signals to and from AP <=> SVC */
43         int svc_reset_gpio;
44         bool is_reset_act_hi;
45         int svc_sysboot_gpio;
46         int wake_detect_gpio; /* bi-dir,maps to WAKE_MOD & WAKE_FRAME signals */
47
48         enum arche_platform_state state;
49
50         int svc_refclk_req;
51         struct clk *svc_ref_clk;
52
53         struct pinctrl *pinctrl;
54         struct pinctrl_state *pin_default;
55
56         int num_apbs;
57
58         enum svc_wakedetect_state wake_detect_state;
59         int wake_detect_irq;
60         spinlock_t wake_lock;                   /* Protect wake_detect_state */
61         struct mutex platform_state_mutex;      /* Protect state */
62         wait_queue_head_t wq;                   /* WQ for arche_pdata->state */
63         unsigned long wake_detect_start;
64         struct notifier_block pm_notifier;
65
66         struct device *dev;
67         struct gb_timesync_svc *timesync_svc_pdata;
68 };
69
70 static int arche_apb_bootret_assert(struct device *dev, void *data)
71 {
72         apb_bootret_assert(dev);
73         return 0;
74 }
75
76 static int arche_apb_bootret_deassert(struct device *dev, void *data)
77 {
78         apb_bootret_deassert(dev);
79         return 0;
80 }
81
82 /* Requires calling context to hold arche_pdata->platform_state_mutex */
83 static void arche_platform_set_state(struct arche_platform_drvdata *arche_pdata,
84                                      enum arche_platform_state state)
85 {
86         arche_pdata->state = state;
87 }
88
89 /*
90  * arche_platform_change_state: Change the operational state
91  *
92  * This exported function allows external drivers to change the state
93  * of the arche-platform driver.
94  * Note that this function only supports transitions between two states
95  * with limited functionality.
96  *
97  *  - ARCHE_PLATFORM_STATE_TIME_SYNC:
98  *    Once set, allows timesync operations between SVC <=> AP and makes
99  *    sure that arche-platform driver ignores any subsequent events/pulses
100  *    from SVC over wake/detect.
101  *
102  *  - ARCHE_PLATFORM_STATE_ACTIVE:
103  *    Puts back driver to active state, where any pulse from SVC on wake/detect
104  *    line would trigger either cold/standby boot.
105  *    Note: Transition request from this function does not trigger cold/standby
106  *          boot. It just puts back driver book keeping variable back to ACTIVE
107  *          state and restores the interrupt.
108  *
109  * Returns -ENODEV if device not found, -EAGAIN if the driver cannot currently
110  * satisfy the requested state-transition or -EINVAL for all other
111  * state-transition requests.
112  */
113 int arche_platform_change_state(enum arche_platform_state state,
114                                 struct gb_timesync_svc *timesync_svc_pdata)
115 {
116         struct arche_platform_drvdata *arche_pdata;
117         struct platform_device *pdev;
118         struct device_node *np;
119         int ret = -EAGAIN;
120         unsigned long flags;
121
122         np = of_find_compatible_node(NULL, NULL, "google,arche-platform");
123         if (!np) {
124                 pr_err("google,arche-platform device node not found\n");
125                 return -ENODEV;
126         }
127
128         pdev = of_find_device_by_node(np);
129         if (!pdev) {
130                 pr_err("arche-platform device not found\n");
131                 return -ENODEV;
132         }
133
134         arche_pdata = platform_get_drvdata(pdev);
135
136         mutex_lock(&arche_pdata->platform_state_mutex);
137         spin_lock_irqsave(&arche_pdata->wake_lock, flags);
138
139         if (arche_pdata->state == state) {
140                 ret = 0;
141                 goto exit;
142         }
143
144         switch (state) {
145         case ARCHE_PLATFORM_STATE_TIME_SYNC:
146                 if (arche_pdata->state != ARCHE_PLATFORM_STATE_ACTIVE) {
147                         ret = -EINVAL;
148                         goto exit;
149                 }
150                 if (arche_pdata->wake_detect_state != WD_STATE_IDLE) {
151                         dev_err(arche_pdata->dev,
152                                 "driver busy with wake/detect line ops\n");
153                         goto  exit;
154                 }
155                 device_for_each_child(arche_pdata->dev, NULL,
156                                       arche_apb_bootret_assert);
157                 arche_pdata->wake_detect_state = WD_STATE_TIMESYNC;
158                 break;
159         case ARCHE_PLATFORM_STATE_ACTIVE:
160                 if (arche_pdata->state != ARCHE_PLATFORM_STATE_TIME_SYNC) {
161                         ret = -EINVAL;
162                         goto exit;
163                 }
164                 device_for_each_child(arche_pdata->dev, NULL,
165                                       arche_apb_bootret_deassert);
166                 arche_pdata->wake_detect_state = WD_STATE_IDLE;
167                 break;
168         case ARCHE_PLATFORM_STATE_OFF:
169         case ARCHE_PLATFORM_STATE_STANDBY:
170         case ARCHE_PLATFORM_STATE_FW_FLASHING:
171                 dev_err(arche_pdata->dev, "busy, request to retry later\n");
172                 goto exit;
173         default:
174                 ret = -EINVAL;
175                 dev_err(arche_pdata->dev,
176                         "invalid state transition request\n");
177                 goto exit;
178         }
179         arche_pdata->timesync_svc_pdata = timesync_svc_pdata;
180         arche_platform_set_state(arche_pdata, state);
181         if (state == ARCHE_PLATFORM_STATE_ACTIVE)
182                 wake_up(&arche_pdata->wq);
183
184         ret = 0;
185 exit:
186         spin_unlock_irqrestore(&arche_pdata->wake_lock, flags);
187         mutex_unlock(&arche_pdata->platform_state_mutex);
188         of_node_put(np);
189         return ret;
190 }
191 EXPORT_SYMBOL_GPL(arche_platform_change_state);
192
193 /* Requires arche_pdata->wake_lock is held by calling context */
194 static void arche_platform_set_wake_detect_state(
195                                 struct arche_platform_drvdata *arche_pdata,
196                                 enum svc_wakedetect_state state)
197 {
198         arche_pdata->wake_detect_state = state;
199 }
200
201 static inline void svc_reset_onoff(unsigned int gpio, bool onoff)
202 {
203         gpio_set_value(gpio, onoff);
204 }
205
206 static int apb_cold_boot(struct device *dev, void *data)
207 {
208         int ret;
209
210         ret = apb_ctrl_coldboot(dev);
211         if (ret)
212                 dev_warn(dev, "failed to coldboot\n");
213
214         /*Child nodes are independent, so do not exit coldboot operation */
215         return 0;
216 }
217
218 static int apb_poweroff(struct device *dev, void *data)
219 {
220         apb_ctrl_poweroff(dev);
221
222         /* Enable HUB3613 into HUB mode. */
223         if (usb3613_hub_mode_ctrl(false))
224                 dev_warn(dev, "failed to control hub device\n");
225
226         return 0;
227 }
228
229 static void arche_platform_wd_irq_en(struct arche_platform_drvdata *arche_pdata)
230 {
231         /* Enable interrupt here, to read event back from SVC */
232         gpio_direction_input(arche_pdata->wake_detect_gpio);
233         enable_irq(arche_pdata->wake_detect_irq);
234 }
235
236 static irqreturn_t arche_platform_wd_irq_thread(int irq, void *devid)
237 {
238         struct arche_platform_drvdata *arche_pdata = devid;
239         unsigned long flags;
240
241         spin_lock_irqsave(&arche_pdata->wake_lock, flags);
242         if (arche_pdata->wake_detect_state != WD_STATE_COLDBOOT_TRIG) {
243                 /* Something is wrong */
244                 spin_unlock_irqrestore(&arche_pdata->wake_lock, flags);
245                 return IRQ_HANDLED;
246         }
247
248         arche_platform_set_wake_detect_state(arche_pdata,
249                                              WD_STATE_COLDBOOT_START);
250         spin_unlock_irqrestore(&arche_pdata->wake_lock, flags);
251
252         /* It should complete power cycle, so first make sure it is poweroff */
253         device_for_each_child(arche_pdata->dev, NULL, apb_poweroff);
254
255         /* Bring APB out of reset: cold boot sequence */
256         device_for_each_child(arche_pdata->dev, NULL, apb_cold_boot);
257
258         /* Enable HUB3613 into HUB mode. */
259         if (usb3613_hub_mode_ctrl(true))
260                 dev_warn(arche_pdata->dev, "failed to control hub device\n");
261
262         spin_lock_irqsave(&arche_pdata->wake_lock, flags);
263         arche_platform_set_wake_detect_state(arche_pdata, WD_STATE_IDLE);
264         spin_unlock_irqrestore(&arche_pdata->wake_lock, flags);
265
266         return IRQ_HANDLED;
267 }
268
269 static irqreturn_t arche_platform_wd_irq(int irq, void *devid)
270 {
271         struct arche_platform_drvdata *arche_pdata = devid;
272         unsigned long flags;
273
274         spin_lock_irqsave(&arche_pdata->wake_lock, flags);
275
276         if (arche_pdata->wake_detect_state == WD_STATE_TIMESYNC) {
277                 gb_timesync_irq(arche_pdata->timesync_svc_pdata);
278                 goto exit;
279         }
280
281         if (gpio_get_value(arche_pdata->wake_detect_gpio)) {
282                 /* wake/detect rising */
283
284                 /*
285                  * If wake/detect line goes high after low, within less than
286                  * 30msec, then standby boot sequence is initiated, which is not
287                  * supported/implemented as of now. So ignore it.
288                  */
289                 if (arche_pdata->wake_detect_state == WD_STATE_BOOT_INIT) {
290                         if (time_before(jiffies,
291                                         arche_pdata->wake_detect_start +
292                                         msecs_to_jiffies(WD_COLDBOOT_PULSE_WIDTH_MS))) {
293                                 arche_platform_set_wake_detect_state(arche_pdata,
294                                                                      WD_STATE_IDLE);
295                         } else {
296                                 /* Check we are not in middle of irq thread already */
297                                 if (arche_pdata->wake_detect_state !=
298                                                 WD_STATE_COLDBOOT_START) {
299                                         arche_platform_set_wake_detect_state(arche_pdata,
300                                                                              WD_STATE_COLDBOOT_TRIG);
301                                         spin_unlock_irqrestore(
302                                                 &arche_pdata->wake_lock,
303                                                 flags);
304                                         return IRQ_WAKE_THREAD;
305                                 }
306                         }
307                 }
308         } else {
309                 /* wake/detect falling */
310                 if (arche_pdata->wake_detect_state == WD_STATE_IDLE) {
311                         arche_pdata->wake_detect_start = jiffies;
312                         /*
313                          * In the begining, when wake/detect goes low (first time), we assume
314                          * it is meant for coldboot and set the flag. If wake/detect line stays low
315                          * beyond 30msec, then it is coldboot else fallback to standby boot.
316                          */
317                         arche_platform_set_wake_detect_state(arche_pdata,
318                                                              WD_STATE_BOOT_INIT);
319                 }
320         }
321
322 exit:
323         spin_unlock_irqrestore(&arche_pdata->wake_lock, flags);
324
325         return IRQ_HANDLED;
326 }
327
328 /*
329  * Requires arche_pdata->platform_state_mutex to be held
330  */
331 static int arche_platform_coldboot_seq(struct arche_platform_drvdata *arche_pdata)
332 {
333         int ret;
334
335         if (arche_pdata->state == ARCHE_PLATFORM_STATE_ACTIVE)
336                 return 0;
337
338         dev_info(arche_pdata->dev, "Booting from cold boot state\n");
339
340         svc_reset_onoff(arche_pdata->svc_reset_gpio,
341                         arche_pdata->is_reset_act_hi);
342
343         gpio_set_value(arche_pdata->svc_sysboot_gpio, 0);
344         usleep_range(100, 200);
345
346         ret = clk_prepare_enable(arche_pdata->svc_ref_clk);
347         if (ret) {
348                 dev_err(arche_pdata->dev, "failed to enable svc_ref_clk: %d\n",
349                                 ret);
350                 return ret;
351         }
352
353         /* bring SVC out of reset */
354         svc_reset_onoff(arche_pdata->svc_reset_gpio,
355                         !arche_pdata->is_reset_act_hi);
356
357         arche_platform_set_state(arche_pdata, ARCHE_PLATFORM_STATE_ACTIVE);
358
359         return 0;
360 }
361
362 /*
363  * Requires arche_pdata->platform_state_mutex to be held
364  */
365 static int arche_platform_fw_flashing_seq(struct arche_platform_drvdata *arche_pdata)
366 {
367         int ret;
368
369         if (arche_pdata->state == ARCHE_PLATFORM_STATE_FW_FLASHING)
370                 return 0;
371
372         dev_info(arche_pdata->dev, "Switching to FW flashing state\n");
373
374         svc_reset_onoff(arche_pdata->svc_reset_gpio,
375                         arche_pdata->is_reset_act_hi);
376
377         gpio_set_value(arche_pdata->svc_sysboot_gpio, 1);
378
379         usleep_range(100, 200);
380
381         ret = clk_prepare_enable(arche_pdata->svc_ref_clk);
382         if (ret) {
383                 dev_err(arche_pdata->dev, "failed to enable svc_ref_clk: %d\n",
384                                 ret);
385                 return ret;
386         }
387
388         svc_reset_onoff(arche_pdata->svc_reset_gpio,
389                         !arche_pdata->is_reset_act_hi);
390
391         arche_platform_set_state(arche_pdata, ARCHE_PLATFORM_STATE_FW_FLASHING);
392
393         return 0;
394 }
395
396 /*
397  * Requires arche_pdata->platform_state_mutex to be held
398  */
399 static void arche_platform_poweroff_seq(struct arche_platform_drvdata *arche_pdata)
400 {
401         unsigned long flags;
402
403         if (arche_pdata->state == ARCHE_PLATFORM_STATE_OFF)
404                 return;
405
406         /* If in fw_flashing mode, then no need to repeate things again */
407         if (arche_pdata->state != ARCHE_PLATFORM_STATE_FW_FLASHING) {
408                 disable_irq(arche_pdata->wake_detect_irq);
409
410                 spin_lock_irqsave(&arche_pdata->wake_lock, flags);
411                 arche_platform_set_wake_detect_state(arche_pdata,
412                                                      WD_STATE_IDLE);
413                 spin_unlock_irqrestore(&arche_pdata->wake_lock, flags);
414         }
415
416         clk_disable_unprepare(arche_pdata->svc_ref_clk);
417
418         /* As part of exit, put APB back in reset state */
419         svc_reset_onoff(arche_pdata->svc_reset_gpio,
420                         arche_pdata->is_reset_act_hi);
421
422         arche_platform_set_state(arche_pdata, ARCHE_PLATFORM_STATE_OFF);
423 }
424
425 static ssize_t state_store(struct device *dev,
426                 struct device_attribute *attr, const char *buf, size_t count)
427 {
428         struct platform_device *pdev = to_platform_device(dev);
429         struct arche_platform_drvdata *arche_pdata = platform_get_drvdata(pdev);
430         int ret = 0;
431
432 retry:
433         mutex_lock(&arche_pdata->platform_state_mutex);
434         if (arche_pdata->state == ARCHE_PLATFORM_STATE_TIME_SYNC) {
435                 mutex_unlock(&arche_pdata->platform_state_mutex);
436                 ret = wait_event_interruptible(
437                         arche_pdata->wq,
438                         arche_pdata->state != ARCHE_PLATFORM_STATE_TIME_SYNC);
439                 if (ret)
440                         return ret;
441                 goto retry;
442         }
443
444         if (sysfs_streq(buf, "off")) {
445                 if (arche_pdata->state == ARCHE_PLATFORM_STATE_OFF)
446                         goto exit;
447
448                 /*  If SVC goes down, bring down APB's as well */
449                 device_for_each_child(arche_pdata->dev, NULL, apb_poweroff);
450
451                 arche_platform_poweroff_seq(arche_pdata);
452
453         } else if (sysfs_streq(buf, "active")) {
454                 if (arche_pdata->state == ARCHE_PLATFORM_STATE_ACTIVE)
455                         goto exit;
456
457                 /* First we want to make sure we power off everything
458                  * and then activate back again */
459                 device_for_each_child(arche_pdata->dev, NULL, apb_poweroff);
460                 arche_platform_poweroff_seq(arche_pdata);
461
462                 arche_platform_wd_irq_en(arche_pdata);
463                 ret = arche_platform_coldboot_seq(arche_pdata);
464                 if (ret)
465                         goto exit;
466
467         } else if (sysfs_streq(buf, "standby")) {
468                 if (arche_pdata->state == ARCHE_PLATFORM_STATE_STANDBY)
469                         goto exit;
470
471                 dev_warn(arche_pdata->dev, "standby state not supported\n");
472         } else if (sysfs_streq(buf, "fw_flashing")) {
473                 if (arche_pdata->state == ARCHE_PLATFORM_STATE_FW_FLASHING)
474                         goto exit;
475
476                 /*
477                  * Here we only control SVC.
478                  *
479                  * In case of FW_FLASHING mode we do not want to control
480                  * APBs, as in case of V2, SPI bus is shared between both
481                  * the APBs. So let user chose which APB he wants to flash.
482                  */
483                 arche_platform_poweroff_seq(arche_pdata);
484
485                 ret = arche_platform_fw_flashing_seq(arche_pdata);
486                 if (ret)
487                         goto exit;
488         } else {
489                 dev_err(arche_pdata->dev, "unknown state\n");
490                 ret = -EINVAL;
491         }
492
493 exit:
494         mutex_unlock(&arche_pdata->platform_state_mutex);
495         return ret ? ret : count;
496 }
497
498 static ssize_t state_show(struct device *dev,
499                 struct device_attribute *attr, char *buf)
500 {
501         struct arche_platform_drvdata *arche_pdata = dev_get_drvdata(dev);
502
503         switch (arche_pdata->state) {
504         case ARCHE_PLATFORM_STATE_OFF:
505                 return sprintf(buf, "off\n");
506         case ARCHE_PLATFORM_STATE_ACTIVE:
507                 return sprintf(buf, "active\n");
508         case ARCHE_PLATFORM_STATE_STANDBY:
509                 return sprintf(buf, "standby\n");
510         case ARCHE_PLATFORM_STATE_FW_FLASHING:
511                 return sprintf(buf, "fw_flashing\n");
512         case ARCHE_PLATFORM_STATE_TIME_SYNC:
513                 return sprintf(buf, "time_sync\n");
514         default:
515                 return sprintf(buf, "unknown state\n");
516         }
517 }
518
519 static DEVICE_ATTR_RW(state);
520
521 static int arche_platform_pm_notifier(struct notifier_block *notifier,
522                                       unsigned long pm_event, void *unused)
523 {
524         struct arche_platform_drvdata *arche_pdata =
525                 container_of(notifier, struct arche_platform_drvdata,
526                              pm_notifier);
527         int ret = NOTIFY_DONE;
528
529         mutex_lock(&arche_pdata->platform_state_mutex);
530         switch (pm_event) {
531         case PM_SUSPEND_PREPARE:
532                 if (arche_pdata->state != ARCHE_PLATFORM_STATE_ACTIVE) {
533                         ret = NOTIFY_STOP;
534                         break;
535                 }
536                 device_for_each_child(arche_pdata->dev, NULL, apb_poweroff);
537                 arche_platform_poweroff_seq(arche_pdata);
538                 break;
539         case PM_POST_SUSPEND:
540                 if (arche_pdata->state != ARCHE_PLATFORM_STATE_OFF)
541                         break;
542
543                 arche_platform_wd_irq_en(arche_pdata);
544                 arche_platform_coldboot_seq(arche_pdata);
545                 break;
546         default:
547                 break;
548         }
549         mutex_unlock(&arche_pdata->platform_state_mutex);
550
551         return ret;
552 }
553
554 static int arche_platform_probe(struct platform_device *pdev)
555 {
556         struct arche_platform_drvdata *arche_pdata;
557         struct device *dev = &pdev->dev;
558         struct device_node *np = dev->of_node;
559         int ret;
560
561         arche_pdata = devm_kzalloc(&pdev->dev, sizeof(*arche_pdata), GFP_KERNEL);
562         if (!arche_pdata)
563                 return -ENOMEM;
564
565         /* setup svc reset gpio */
566         arche_pdata->is_reset_act_hi = of_property_read_bool(np,
567                                         "svc,reset-active-high");
568         arche_pdata->svc_reset_gpio = of_get_named_gpio(np, "svc,reset-gpio", 0);
569         if (arche_pdata->svc_reset_gpio < 0) {
570                 dev_err(dev, "failed to get reset-gpio\n");
571                 return arche_pdata->svc_reset_gpio;
572         }
573         ret = devm_gpio_request(dev, arche_pdata->svc_reset_gpio, "svc-reset");
574         if (ret) {
575                 dev_err(dev, "failed to request svc-reset gpio:%d\n", ret);
576                 return ret;
577         }
578         ret = gpio_direction_output(arche_pdata->svc_reset_gpio,
579                                         arche_pdata->is_reset_act_hi);
580         if (ret) {
581                 dev_err(dev, "failed to set svc-reset gpio dir:%d\n", ret);
582                 return ret;
583         }
584         arche_platform_set_state(arche_pdata, ARCHE_PLATFORM_STATE_OFF);
585
586         arche_pdata->svc_sysboot_gpio = of_get_named_gpio(np,
587                                         "svc,sysboot-gpio", 0);
588         if (arche_pdata->svc_sysboot_gpio < 0) {
589                 dev_err(dev, "failed to get sysboot gpio\n");
590                 return arche_pdata->svc_sysboot_gpio;
591         }
592         ret = devm_gpio_request(dev, arche_pdata->svc_sysboot_gpio, "sysboot0");
593         if (ret) {
594                 dev_err(dev, "failed to request sysboot0 gpio:%d\n", ret);
595                 return ret;
596         }
597         ret = gpio_direction_output(arche_pdata->svc_sysboot_gpio, 0);
598         if (ret) {
599                 dev_err(dev, "failed to set svc-reset gpio dir:%d\n", ret);
600                 return ret;
601         }
602
603         /* setup the clock request gpio first */
604         arche_pdata->svc_refclk_req = of_get_named_gpio(np,
605                                         "svc,refclk-req-gpio", 0);
606         if (arche_pdata->svc_refclk_req < 0) {
607                 dev_err(dev, "failed to get svc clock-req gpio\n");
608                 return arche_pdata->svc_refclk_req;
609         }
610         ret = devm_gpio_request(dev, arche_pdata->svc_refclk_req, "svc-clk-req");
611         if (ret) {
612                 dev_err(dev, "failed to request svc-clk-req gpio: %d\n", ret);
613                 return ret;
614         }
615         ret = gpio_direction_input(arche_pdata->svc_refclk_req);
616         if (ret) {
617                 dev_err(dev, "failed to set svc-clk-req gpio dir :%d\n", ret);
618                 return ret;
619         }
620
621         /* setup refclk2 to follow the pin */
622         arche_pdata->svc_ref_clk = devm_clk_get(dev, "svc_ref_clk");
623         if (IS_ERR(arche_pdata->svc_ref_clk)) {
624                 ret = PTR_ERR(arche_pdata->svc_ref_clk);
625                 dev_err(dev, "failed to get svc_ref_clk: %d\n", ret);
626                 return ret;
627         }
628
629         platform_set_drvdata(pdev, arche_pdata);
630
631         arche_pdata->num_apbs = of_get_child_count(np);
632         dev_dbg(dev, "Number of APB's available - %d\n", arche_pdata->num_apbs);
633
634         arche_pdata->wake_detect_gpio = of_get_named_gpio(np, "svc,wake-detect-gpio", 0);
635         if (arche_pdata->wake_detect_gpio < 0) {
636                 dev_err(dev, "failed to get wake detect gpio\n");
637                 return arche_pdata->wake_detect_gpio;
638         }
639
640         ret = devm_gpio_request(dev, arche_pdata->wake_detect_gpio, "wake detect");
641         if (ret) {
642                 dev_err(dev, "Failed requesting wake_detect gpio %d\n",
643                                 arche_pdata->wake_detect_gpio);
644                 return ret;
645         }
646
647         arche_platform_set_wake_detect_state(arche_pdata, WD_STATE_IDLE);
648
649         arche_pdata->dev = &pdev->dev;
650
651         spin_lock_init(&arche_pdata->wake_lock);
652         mutex_init(&arche_pdata->platform_state_mutex);
653         init_waitqueue_head(&arche_pdata->wq);
654         arche_pdata->wake_detect_irq =
655                 gpio_to_irq(arche_pdata->wake_detect_gpio);
656
657         ret = devm_request_threaded_irq(dev, arche_pdata->wake_detect_irq,
658                         arche_platform_wd_irq,
659                         arche_platform_wd_irq_thread,
660                         IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING | IRQF_ONESHOT,
661                         dev_name(dev), arche_pdata);
662         if (ret) {
663                 dev_err(dev, "failed to request wake detect IRQ %d\n", ret);
664                 return ret;
665         }
666         disable_irq(arche_pdata->wake_detect_irq);
667
668         ret = device_create_file(dev, &dev_attr_state);
669         if (ret) {
670                 dev_err(dev, "failed to create state file in sysfs\n");
671                 return ret;
672         }
673
674         ret = of_platform_populate(np, NULL, NULL, dev);
675         if (ret) {
676                 dev_err(dev, "failed to populate child nodes %d\n", ret);
677                 goto err_device_remove;
678         }
679
680         arche_pdata->pm_notifier.notifier_call = arche_platform_pm_notifier;
681         ret = register_pm_notifier(&arche_pdata->pm_notifier);
682
683         if (ret) {
684                 dev_err(dev, "failed to register pm notifier %d\n", ret);
685                 goto err_device_remove;
686         }
687
688         /* Register callback pointer */
689         arche_platform_change_state_cb = arche_platform_change_state;
690
691         /* Explicitly power off if requested */
692         if (!of_property_read_bool(pdev->dev.of_node, "arche,init-off")) {
693                 mutex_lock(&arche_pdata->platform_state_mutex);
694                 ret = arche_platform_coldboot_seq(arche_pdata);
695                 if (ret) {
696                         dev_err(dev, "Failed to cold boot svc %d\n", ret);
697                         goto err_coldboot;
698                 }
699                 arche_platform_wd_irq_en(arche_pdata);
700                 mutex_unlock(&arche_pdata->platform_state_mutex);
701         }
702
703         dev_info(dev, "Device registered successfully\n");
704         return 0;
705
706 err_coldboot:
707         mutex_unlock(&arche_pdata->platform_state_mutex);
708 err_device_remove:
709         device_remove_file(&pdev->dev, &dev_attr_state);
710         return ret;
711 }
712
713 static int arche_remove_child(struct device *dev, void *unused)
714 {
715         struct platform_device *pdev = to_platform_device(dev);
716
717         platform_device_unregister(pdev);
718
719         return 0;
720 }
721
722 static int arche_platform_remove(struct platform_device *pdev)
723 {
724         struct arche_platform_drvdata *arche_pdata = platform_get_drvdata(pdev);
725
726         unregister_pm_notifier(&arche_pdata->pm_notifier);
727         device_remove_file(&pdev->dev, &dev_attr_state);
728         device_for_each_child(&pdev->dev, NULL, arche_remove_child);
729         arche_platform_poweroff_seq(arche_pdata);
730         platform_set_drvdata(pdev, NULL);
731
732         if (usb3613_hub_mode_ctrl(false))
733                 dev_warn(arche_pdata->dev, "failed to control hub device\n");
734                 /* TODO: Should we do anything more here ?? */
735         return 0;
736 }
737
738 static int arche_platform_suspend(struct device *dev)
739 {
740         /*
741          * If timing profile premits, we may shutdown bridge
742          * completely
743          *
744          * TODO: sequence ??
745          *
746          * Also, need to make sure we meet precondition for unipro suspend
747          * Precondition: Definition ???
748          */
749         return 0;
750 }
751
752 static int arche_platform_resume(struct device *dev)
753 {
754         /*
755          * Atleast for ES2 we have to meet the delay requirement between
756          * unipro switch and AP bridge init, depending on whether bridge is in
757          * OFF state or standby state.
758          *
759          * Based on whether bridge is in standby or OFF state we may have to
760          * assert multiple signals. Please refer to WDM spec, for more info.
761          *
762          */
763         return 0;
764 }
765
766 static void arche_platform_shutdown(struct platform_device *pdev)
767 {
768         struct arche_platform_drvdata *arche_pdata = platform_get_drvdata(pdev);
769
770         arche_platform_poweroff_seq(arche_pdata);
771
772         usb3613_hub_mode_ctrl(false);
773 }
774
775 static SIMPLE_DEV_PM_OPS(arche_platform_pm_ops,
776                         arche_platform_suspend,
777                         arche_platform_resume);
778
779 static const struct of_device_id arche_platform_of_match[] = {
780         { .compatible = "google,arche-platform", }, /* Use PID/VID of SVC device */
781         { },
782 };
783
784 static const struct of_device_id arche_combined_id[] = {
785         { .compatible = "google,arche-platform", }, /* Use PID/VID of SVC device */
786         { .compatible = "usbffff,2", },
787         { },
788 };
789 MODULE_DEVICE_TABLE(of, arche_combined_id);
790
791 static struct platform_driver arche_platform_device_driver = {
792         .probe          = arche_platform_probe,
793         .remove         = arche_platform_remove,
794         .shutdown       = arche_platform_shutdown,
795         .driver         = {
796                 .name   = "arche-platform-ctrl",
797                 .pm     = &arche_platform_pm_ops,
798                 .of_match_table = arche_platform_of_match,
799         }
800 };
801
802 static int __init arche_init(void)
803 {
804         int retval;
805
806         retval = platform_driver_register(&arche_platform_device_driver);
807         if (retval)
808                 return retval;
809
810         retval = arche_apb_init();
811         if (retval)
812                 platform_driver_unregister(&arche_platform_device_driver);
813
814         return retval;
815 }
816 module_init(arche_init);
817
818 static void __exit arche_exit(void)
819 {
820         arche_apb_exit();
821         platform_driver_unregister(&arche_platform_device_driver);
822 }
823 module_exit(arche_exit);
824
825 MODULE_LICENSE("GPL v2");
826 MODULE_AUTHOR("Vaibhav Hiremath <vaibhav.hiremath@linaro.org>");
827 MODULE_DESCRIPTION("Arche Platform Driver");