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