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