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