]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/staging/greybus/arche-apb-ctrl.c
greybus: timesync: Add timesync core driver
[karo-tx-linux.git] / drivers / staging / greybus / arche-apb-ctrl.c
1 /*
2  * Arche Platform driver to control APB.
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/interrupt.h>
14 #include <linux/of_gpio.h>
15 #include <linux/of_irq.h>
16 #include <linux/module.h>
17 #include <linux/pinctrl/consumer.h>
18 #include <linux/platform_device.h>
19 #include <linux/pm.h>
20 #include <linux/regulator/consumer.h>
21 #include <linux/spinlock.h>
22 #include "arche_platform.h"
23
24
25 struct arche_apb_ctrl_drvdata {
26         /* Control GPIO signals to and from AP <=> AP Bridges */
27         int resetn_gpio;
28         int boot_ret_gpio;
29         int pwroff_gpio;
30         int wake_in_gpio;
31         int wake_out_gpio;
32         int pwrdn_gpio;
33
34         enum arche_platform_state state;
35         bool init_disabled;
36
37         struct regulator *vcore;
38         struct regulator *vio;
39
40         int clk_en_gpio;
41         struct clk *clk;
42
43         struct pinctrl *pinctrl;
44         struct pinctrl_state *pin_default;
45 };
46
47 /*
48  * Note that these low level api's are active high
49  */
50 static inline void deassert_reset(unsigned int gpio)
51 {
52         gpio_set_value(gpio, 1);
53 }
54
55 static inline void assert_reset(unsigned int gpio)
56 {
57         gpio_set_value(gpio, 0);
58 }
59
60 /*
61  * Note: Please do not modify the below sequence, as it is as per the spec
62  */
63 static int coldboot_seq(struct platform_device *pdev)
64 {
65         struct device *dev = &pdev->dev;
66         struct arche_apb_ctrl_drvdata *apb = platform_get_drvdata(pdev);
67         int ret;
68
69         if (apb->init_disabled ||
70                         apb->state == ARCHE_PLATFORM_STATE_ACTIVE)
71                 return 0;
72
73         /* Hold APB in reset state */
74         assert_reset(apb->resetn_gpio);
75
76         /* Enable power to APB */
77         if (!IS_ERR(apb->vcore)) {
78                 ret = regulator_enable(apb->vcore);
79                 if (ret) {
80                         dev_err(dev, "failed to enable core regulator\n");
81                         return ret;
82                 }
83         }
84
85         if (!IS_ERR(apb->vio)) {
86                 ret = regulator_enable(apb->vio);
87                 if (ret) {
88                         dev_err(dev, "failed to enable IO regulator\n");
89                         return ret;
90                 }
91         }
92
93         apb_bootret_deassert(dev);
94
95         /* On DB3 clock was not mandatory */
96         if (gpio_is_valid(apb->clk_en_gpio))
97                 gpio_set_value(apb->clk_en_gpio, 1);
98
99         usleep_range(100, 200);
100
101         /* deassert reset to APB : Active-low signal */
102         deassert_reset(apb->resetn_gpio);
103
104         apb->state = ARCHE_PLATFORM_STATE_ACTIVE;
105
106         return 0;
107 }
108
109 static int fw_flashing_seq(struct platform_device *pdev)
110 {
111         struct device *dev = &pdev->dev;
112         struct arche_apb_ctrl_drvdata *apb = platform_get_drvdata(pdev);
113         int ret;
114
115         if (apb->init_disabled ||
116                         apb->state == ARCHE_PLATFORM_STATE_FW_FLASHING)
117                 return 0;
118
119         ret = regulator_enable(apb->vcore);
120         if (ret) {
121                 dev_err(dev, "failed to enable core regulator\n");
122                 return ret;
123         }
124
125         ret = regulator_enable(apb->vio);
126         if (ret) {
127                 dev_err(dev, "failed to enable IO regulator\n");
128                 return ret;
129         }
130
131         /* for flashing device should be in reset state */
132         assert_reset(apb->resetn_gpio);
133         apb->state = ARCHE_PLATFORM_STATE_FW_FLASHING;
134
135         return 0;
136 }
137
138 static int standby_boot_seq(struct platform_device *pdev)
139 {
140         struct arche_apb_ctrl_drvdata *apb = platform_get_drvdata(pdev);
141
142         if (apb->init_disabled)
143                 return 0;
144
145         /* Even if it is in OFF state, then we do not want to change the state */
146         if (apb->state == ARCHE_PLATFORM_STATE_STANDBY ||
147                         apb->state == ARCHE_PLATFORM_STATE_OFF)
148                 return 0;
149
150         /*
151          * As per WDM spec, do nothing
152          *
153          * Pasted from WDM spec,
154          *  - A falling edge on POWEROFF_L is detected (a)
155          *  - WDM enters standby mode, but no output signals are changed
156          * */
157
158         /* TODO: POWEROFF_L is input to WDM module  */
159         apb->state = ARCHE_PLATFORM_STATE_STANDBY;
160         return 0;
161 }
162
163 static void poweroff_seq(struct platform_device *pdev)
164 {
165         struct arche_apb_ctrl_drvdata *apb = platform_get_drvdata(pdev);
166
167         if (apb->init_disabled || apb->state == ARCHE_PLATFORM_STATE_OFF)
168                 return;
169
170         /* disable the clock */
171         if (gpio_is_valid(apb->clk_en_gpio))
172                 gpio_set_value(apb->clk_en_gpio, 0);
173
174         if (!IS_ERR(apb->vcore) && regulator_is_enabled(apb->vcore) > 0)
175                 regulator_disable(apb->vcore);
176
177         if (!IS_ERR(apb->vio) && regulator_is_enabled(apb->vio) > 0)
178                 regulator_disable(apb->vio);
179
180         /* As part of exit, put APB back in reset state */
181         assert_reset(apb->resetn_gpio);
182         apb->state = ARCHE_PLATFORM_STATE_OFF;
183
184         /* TODO: May have to send an event to SVC about this exit */
185 }
186
187 void apb_bootret_assert(struct device *dev)
188 {
189         struct arche_apb_ctrl_drvdata *apb = dev_get_drvdata(dev);
190
191         gpio_set_value(apb->boot_ret_gpio, 1);
192 }
193
194 void apb_bootret_deassert(struct device *dev)
195 {
196         struct arche_apb_ctrl_drvdata *apb = dev_get_drvdata(dev);
197
198         gpio_set_value(apb->boot_ret_gpio, 0);
199 }
200
201 int apb_ctrl_coldboot(struct device *dev)
202 {
203         return coldboot_seq(to_platform_device(dev));
204 }
205
206 int apb_ctrl_fw_flashing(struct device *dev)
207 {
208         return fw_flashing_seq(to_platform_device(dev));
209 }
210
211 int apb_ctrl_standby_boot(struct device *dev)
212 {
213         return standby_boot_seq(to_platform_device(dev));
214 }
215
216 void apb_ctrl_poweroff(struct device *dev)
217 {
218         poweroff_seq(to_platform_device(dev));
219 }
220
221 static ssize_t state_store(struct device *dev,
222                 struct device_attribute *attr, const char *buf, size_t count)
223 {
224         struct platform_device *pdev = to_platform_device(dev);
225         struct arche_apb_ctrl_drvdata *apb = platform_get_drvdata(pdev);
226         int ret = 0;
227         bool is_disabled;
228
229         if (sysfs_streq(buf, "off")) {
230                 if (apb->state == ARCHE_PLATFORM_STATE_OFF)
231                         return count;
232
233                 poweroff_seq(pdev);
234         } else if (sysfs_streq(buf, "active")) {
235                 if (apb->state == ARCHE_PLATFORM_STATE_ACTIVE)
236                         return count;
237
238                 poweroff_seq(pdev);
239                 is_disabled = apb->init_disabled;
240                 apb->init_disabled = false;
241                 ret = coldboot_seq(pdev);
242                 if (ret)
243                         apb->init_disabled = is_disabled;
244         } else if (sysfs_streq(buf, "standby")) {
245                 if (apb->state == ARCHE_PLATFORM_STATE_STANDBY)
246                         return count;
247
248                 ret = standby_boot_seq(pdev);
249         } else if (sysfs_streq(buf, "fw_flashing")) {
250                 if (apb->state == ARCHE_PLATFORM_STATE_FW_FLASHING)
251                         return count;
252
253                 /* First we want to make sure we power off everything
254                  * and then enter FW flashing state */
255                 poweroff_seq(pdev);
256                 ret = fw_flashing_seq(pdev);
257         } else {
258                 dev_err(dev, "unknown state\n");
259                 ret = -EINVAL;
260         }
261
262         return ret ? ret : count;
263 }
264
265 static ssize_t state_show(struct device *dev,
266                 struct device_attribute *attr, char *buf)
267 {
268         struct arche_apb_ctrl_drvdata *apb = dev_get_drvdata(dev);
269
270         switch (apb->state) {
271         case ARCHE_PLATFORM_STATE_OFF:
272                 return sprintf(buf, "off%s\n",
273                                 apb->init_disabled ? ",disabled" : "");
274         case ARCHE_PLATFORM_STATE_ACTIVE:
275                 return sprintf(buf, "active\n");
276         case ARCHE_PLATFORM_STATE_STANDBY:
277                 return sprintf(buf, "standby\n");
278         case ARCHE_PLATFORM_STATE_FW_FLASHING:
279                 return sprintf(buf, "fw_flashing\n");
280         default:
281                 return sprintf(buf, "unknown state\n");
282         }
283 }
284
285 static DEVICE_ATTR_RW(state);
286
287 static int apb_ctrl_get_devtree_data(struct platform_device *pdev,
288                 struct arche_apb_ctrl_drvdata *apb)
289 {
290         struct device *dev = &pdev->dev;
291         struct device_node *np = dev->of_node;
292         int ret;
293
294         apb->resetn_gpio = of_get_named_gpio(np, "reset-gpios", 0);
295         if (apb->resetn_gpio < 0) {
296                 dev_err(dev, "failed to get reset gpio\n");
297                 return apb->resetn_gpio;
298         }
299         ret = devm_gpio_request_one(dev, apb->resetn_gpio,
300                         GPIOF_OUT_INIT_LOW, "apb-reset");
301         if (ret) {
302                 dev_err(dev, "Failed requesting reset gpio %d\n",
303                                 apb->resetn_gpio);
304                 return ret;
305         }
306
307         apb->boot_ret_gpio = of_get_named_gpio(np, "boot-ret-gpios", 0);
308         if (apb->boot_ret_gpio < 0) {
309                 dev_err(dev, "failed to get boot retention gpio\n");
310                 return apb->boot_ret_gpio;
311         }
312         ret = devm_gpio_request_one(dev, apb->boot_ret_gpio,
313                         GPIOF_OUT_INIT_LOW, "boot retention");
314         if (ret) {
315                 dev_err(dev, "Failed requesting bootret gpio %d\n",
316                                 apb->boot_ret_gpio);
317                 return ret;
318         }
319
320         /* It's not mandatory to support power management interface */
321         apb->pwroff_gpio = of_get_named_gpio(np, "pwr-off-gpios", 0);
322         if (apb->pwroff_gpio < 0) {
323                 dev_err(dev, "failed to get power off gpio\n");
324                 return apb->pwroff_gpio;
325         }
326         ret = devm_gpio_request_one(dev, apb->pwroff_gpio,
327                         GPIOF_IN, "pwroff_n");
328         if (ret) {
329                 dev_err(dev, "Failed requesting pwroff_n gpio %d\n",
330                                 apb->pwroff_gpio);
331                 return ret;
332         }
333
334         /* Do not make clock mandatory as of now (for DB3) */
335         apb->clk_en_gpio = of_get_named_gpio(np, "clock-en-gpio", 0);
336         if (apb->clk_en_gpio < 0) {
337                 dev_warn(dev, "failed to get clock en gpio\n");
338         } else if (gpio_is_valid(apb->clk_en_gpio)) {
339                 ret = devm_gpio_request_one(dev, apb->clk_en_gpio,
340                                 GPIOF_OUT_INIT_LOW, "apb_clk_en");
341                 if (ret) {
342                         dev_warn(dev, "Failed requesting APB clock en gpio %d\n",
343                                         apb->clk_en_gpio);
344                         return ret;
345                 }
346         }
347
348         apb->pwrdn_gpio = of_get_named_gpio(np, "pwr-down-gpios", 0);
349         if (apb->pwrdn_gpio < 0)
350                 dev_warn(dev, "failed to get power down gpio\n");
351
352         /* Regulators are optional, as we may have fixed supply coming in */
353         apb->vcore = devm_regulator_get(dev, "vcore");
354         if (IS_ERR(apb->vcore))
355                 dev_warn(dev, "no core regulator found\n");
356
357         apb->vio = devm_regulator_get(dev, "vio");
358         if (IS_ERR(apb->vio))
359                 dev_warn(dev, "no IO regulator found\n");
360
361         apb->pinctrl = devm_pinctrl_get(&pdev->dev);
362         if (IS_ERR(apb->pinctrl)) {
363                 dev_err(&pdev->dev, "could not get pinctrl handle\n");
364                 return PTR_ERR(apb->pinctrl);
365         }
366         apb->pin_default = pinctrl_lookup_state(apb->pinctrl, "default");
367         if (IS_ERR(apb->pin_default)) {
368                 dev_err(&pdev->dev, "could not get default pin state\n");
369                 return PTR_ERR(apb->pin_default);
370         }
371
372         return 0;
373 }
374
375 static int arche_apb_ctrl_probe(struct platform_device *pdev)
376 {
377         int ret;
378         struct arche_apb_ctrl_drvdata *apb;
379         struct device *dev = &pdev->dev;
380
381         apb = devm_kzalloc(&pdev->dev, sizeof(*apb), GFP_KERNEL);
382         if (!apb)
383                 return -ENOMEM;
384
385         ret = apb_ctrl_get_devtree_data(pdev, apb);
386         if (ret) {
387                 dev_err(dev, "failed to get apb devicetree data %d\n", ret);
388                 return ret;
389         }
390
391         /* Initially set APB to OFF state */
392         apb->state = ARCHE_PLATFORM_STATE_OFF;
393         /* Check whether device needs to be enabled on boot */
394         if (of_property_read_bool(pdev->dev.of_node, "ara,init-disable"))
395                 apb->init_disabled = true;
396
397         platform_set_drvdata(pdev, apb);
398
399         /* Create sysfs interface to allow user to change state dynamically */
400         ret = device_create_file(dev, &dev_attr_state);
401         if (ret) {
402                 dev_err(dev, "failed to create state file in sysfs\n");
403                 return ret;
404         }
405
406         dev_info(&pdev->dev, "Device registered successfully\n");
407         return 0;
408 }
409
410 static int arche_apb_ctrl_remove(struct platform_device *pdev)
411 {
412         device_remove_file(&pdev->dev, &dev_attr_state);
413         poweroff_seq(pdev);
414         platform_set_drvdata(pdev, NULL);
415
416         return 0;
417 }
418
419 static int arche_apb_ctrl_suspend(struct device *dev)
420 {
421         /*
422          * If timing profile permits, we may shutdown bridge
423          * completely
424          *
425          * TODO: sequence ??
426          *
427          * Also, need to make sure we meet precondition for unipro suspend
428          * Precondition: Definition ???
429          */
430         return 0;
431 }
432
433 static int arche_apb_ctrl_resume(struct device *dev)
434 {
435         /*
436          * Atleast for ES2 we have to meet the delay requirement between
437          * unipro switch and AP bridge init, depending on whether bridge is in
438          * OFF state or standby state.
439          *
440          * Based on whether bridge is in standby or OFF state we may have to
441          * assert multiple signals. Please refer to WDM spec, for more info.
442          *
443          */
444         return 0;
445 }
446
447 static SIMPLE_DEV_PM_OPS(arche_apb_ctrl_pm_ops, arche_apb_ctrl_suspend,
448                          arche_apb_ctrl_resume);
449
450 static struct of_device_id arche_apb_ctrl_of_match[] = {
451         { .compatible = "usbffff,2", },
452         { },
453 };
454
455 static struct platform_driver arche_apb_ctrl_device_driver = {
456         .probe          = arche_apb_ctrl_probe,
457         .remove         = arche_apb_ctrl_remove,
458         .driver         = {
459                 .name   = "arche-apb-ctrl",
460                 .pm     = &arche_apb_ctrl_pm_ops,
461                 .of_match_table = arche_apb_ctrl_of_match,
462         }
463 };
464
465 int __init arche_apb_init(void)
466 {
467         return platform_driver_register(&arche_apb_ctrl_device_driver);
468 }
469
470 void __exit arche_apb_exit(void)
471 {
472         platform_driver_unregister(&arche_apb_ctrl_device_driver);
473 }