WIP FPC-III support
[linux/fpc-iii.git] / drivers / staging / greybus / arche-apb-ctrl.c
blobbbf3ba744fc441b71e84cd38ec65bfadeb103b2c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Arche Platform driver to control APB.
5 * Copyright 2014-2015 Google Inc.
6 * Copyright 2014-2015 Linaro Ltd.
7 */
9 #include <linux/clk.h>
10 #include <linux/delay.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/interrupt.h>
13 #include <linux/of_irq.h>
14 #include <linux/module.h>
15 #include <linux/pinctrl/consumer.h>
16 #include <linux/platform_device.h>
17 #include <linux/pm.h>
18 #include <linux/regulator/consumer.h>
19 #include <linux/spinlock.h>
20 #include "arche_platform.h"
22 static void apb_bootret_deassert(struct device *dev);
24 struct arche_apb_ctrl_drvdata {
25 /* Control GPIO signals to and from AP <=> AP Bridges */
26 struct gpio_desc *resetn;
27 struct gpio_desc *boot_ret;
28 struct gpio_desc *pwroff;
29 struct gpio_desc *wake_in;
30 struct gpio_desc *wake_out;
31 struct gpio_desc *pwrdn;
33 enum arche_platform_state state;
34 bool init_disabled;
36 struct regulator *vcore;
37 struct regulator *vio;
39 struct gpio_desc *clk_en;
40 struct clk *clk;
42 struct pinctrl *pinctrl;
43 struct pinctrl_state *pin_default;
45 /* V2: SPI Bus control */
46 struct gpio_desc *spi_en;
47 bool spi_en_polarity_high;
51 * Note that these low level api's are active high
53 static inline void deassert_reset(struct gpio_desc *gpio)
55 gpiod_set_raw_value(gpio, 1);
58 static inline void assert_reset(struct gpio_desc *gpio)
60 gpiod_set_raw_value(gpio, 0);
64 * Note: Please do not modify the below sequence, as it is as per the spec
66 static int coldboot_seq(struct platform_device *pdev)
68 struct device *dev = &pdev->dev;
69 struct arche_apb_ctrl_drvdata *apb = platform_get_drvdata(pdev);
70 int ret;
72 if (apb->init_disabled ||
73 apb->state == ARCHE_PLATFORM_STATE_ACTIVE)
74 return 0;
76 /* Hold APB in reset state */
77 assert_reset(apb->resetn);
79 if (apb->state == ARCHE_PLATFORM_STATE_FW_FLASHING && apb->spi_en)
80 devm_gpiod_put(dev, apb->spi_en);
82 /* Enable power to APB */
83 if (!IS_ERR(apb->vcore)) {
84 ret = regulator_enable(apb->vcore);
85 if (ret) {
86 dev_err(dev, "failed to enable core regulator\n");
87 return ret;
91 if (!IS_ERR(apb->vio)) {
92 ret = regulator_enable(apb->vio);
93 if (ret) {
94 dev_err(dev, "failed to enable IO regulator\n");
95 return ret;
99 apb_bootret_deassert(dev);
101 /* On DB3 clock was not mandatory */
102 if (apb->clk_en)
103 gpiod_set_value(apb->clk_en, 1);
105 usleep_range(100, 200);
107 /* deassert reset to APB : Active-low signal */
108 deassert_reset(apb->resetn);
110 apb->state = ARCHE_PLATFORM_STATE_ACTIVE;
112 return 0;
115 static int fw_flashing_seq(struct platform_device *pdev)
117 struct device *dev = &pdev->dev;
118 struct arche_apb_ctrl_drvdata *apb = platform_get_drvdata(pdev);
119 int ret;
121 if (apb->init_disabled ||
122 apb->state == ARCHE_PLATFORM_STATE_FW_FLASHING)
123 return 0;
125 ret = regulator_enable(apb->vcore);
126 if (ret) {
127 dev_err(dev, "failed to enable core regulator\n");
128 return ret;
131 ret = regulator_enable(apb->vio);
132 if (ret) {
133 dev_err(dev, "failed to enable IO regulator\n");
134 return ret;
137 if (apb->spi_en) {
138 unsigned long flags;
140 if (apb->spi_en_polarity_high)
141 flags = GPIOD_OUT_HIGH;
142 else
143 flags = GPIOD_OUT_LOW;
145 apb->spi_en = devm_gpiod_get(dev, "spi-en", flags);
146 if (IS_ERR(apb->spi_en)) {
147 ret = PTR_ERR(apb->spi_en);
148 dev_err(dev, "Failed requesting SPI bus en GPIO: %d\n",
149 ret);
150 return ret;
154 /* for flashing device should be in reset state */
155 assert_reset(apb->resetn);
156 apb->state = ARCHE_PLATFORM_STATE_FW_FLASHING;
158 return 0;
161 static int standby_boot_seq(struct platform_device *pdev)
163 struct device *dev = &pdev->dev;
164 struct arche_apb_ctrl_drvdata *apb = platform_get_drvdata(pdev);
166 if (apb->init_disabled)
167 return 0;
170 * Even if it is in OFF state,
171 * then we do not want to change the state
173 if (apb->state == ARCHE_PLATFORM_STATE_STANDBY ||
174 apb->state == ARCHE_PLATFORM_STATE_OFF)
175 return 0;
177 if (apb->state == ARCHE_PLATFORM_STATE_FW_FLASHING && apb->spi_en)
178 devm_gpiod_put(dev, apb->spi_en);
181 * As per WDM spec, do nothing
183 * Pasted from WDM spec,
184 * - A falling edge on POWEROFF_L is detected (a)
185 * - WDM enters standby mode, but no output signals are changed
188 /* TODO: POWEROFF_L is input to WDM module */
189 apb->state = ARCHE_PLATFORM_STATE_STANDBY;
190 return 0;
193 static void poweroff_seq(struct platform_device *pdev)
195 struct device *dev = &pdev->dev;
196 struct arche_apb_ctrl_drvdata *apb = platform_get_drvdata(pdev);
198 if (apb->init_disabled || apb->state == ARCHE_PLATFORM_STATE_OFF)
199 return;
201 if (apb->state == ARCHE_PLATFORM_STATE_FW_FLASHING && apb->spi_en)
202 devm_gpiod_put(dev, apb->spi_en);
204 /* disable the clock */
205 if (apb->clk_en)
206 gpiod_set_value(apb->clk_en, 0);
208 if (!IS_ERR(apb->vcore) && regulator_is_enabled(apb->vcore) > 0)
209 regulator_disable(apb->vcore);
211 if (!IS_ERR(apb->vio) && regulator_is_enabled(apb->vio) > 0)
212 regulator_disable(apb->vio);
214 /* As part of exit, put APB back in reset state */
215 assert_reset(apb->resetn);
216 apb->state = ARCHE_PLATFORM_STATE_OFF;
218 /* TODO: May have to send an event to SVC about this exit */
221 static void apb_bootret_deassert(struct device *dev)
223 struct arche_apb_ctrl_drvdata *apb = dev_get_drvdata(dev);
225 gpiod_set_value(apb->boot_ret, 0);
228 int apb_ctrl_coldboot(struct device *dev)
230 return coldboot_seq(to_platform_device(dev));
233 int apb_ctrl_fw_flashing(struct device *dev)
235 return fw_flashing_seq(to_platform_device(dev));
238 int apb_ctrl_standby_boot(struct device *dev)
240 return standby_boot_seq(to_platform_device(dev));
243 void apb_ctrl_poweroff(struct device *dev)
245 poweroff_seq(to_platform_device(dev));
248 static ssize_t state_store(struct device *dev,
249 struct device_attribute *attr,
250 const char *buf, size_t count)
252 struct platform_device *pdev = to_platform_device(dev);
253 struct arche_apb_ctrl_drvdata *apb = platform_get_drvdata(pdev);
254 int ret = 0;
255 bool is_disabled;
257 if (sysfs_streq(buf, "off")) {
258 if (apb->state == ARCHE_PLATFORM_STATE_OFF)
259 return count;
261 poweroff_seq(pdev);
262 } else if (sysfs_streq(buf, "active")) {
263 if (apb->state == ARCHE_PLATFORM_STATE_ACTIVE)
264 return count;
266 poweroff_seq(pdev);
267 is_disabled = apb->init_disabled;
268 apb->init_disabled = false;
269 ret = coldboot_seq(pdev);
270 if (ret)
271 apb->init_disabled = is_disabled;
272 } else if (sysfs_streq(buf, "standby")) {
273 if (apb->state == ARCHE_PLATFORM_STATE_STANDBY)
274 return count;
276 ret = standby_boot_seq(pdev);
277 } else if (sysfs_streq(buf, "fw_flashing")) {
278 if (apb->state == ARCHE_PLATFORM_STATE_FW_FLASHING)
279 return count;
282 * First we want to make sure we power off everything
283 * and then enter FW flashing state
285 poweroff_seq(pdev);
286 ret = fw_flashing_seq(pdev);
287 } else {
288 dev_err(dev, "unknown state\n");
289 ret = -EINVAL;
292 return ret ? ret : count;
295 static ssize_t state_show(struct device *dev,
296 struct device_attribute *attr, char *buf)
298 struct arche_apb_ctrl_drvdata *apb = dev_get_drvdata(dev);
300 switch (apb->state) {
301 case ARCHE_PLATFORM_STATE_OFF:
302 return sprintf(buf, "off%s\n",
303 apb->init_disabled ? ",disabled" : "");
304 case ARCHE_PLATFORM_STATE_ACTIVE:
305 return sprintf(buf, "active\n");
306 case ARCHE_PLATFORM_STATE_STANDBY:
307 return sprintf(buf, "standby\n");
308 case ARCHE_PLATFORM_STATE_FW_FLASHING:
309 return sprintf(buf, "fw_flashing\n");
310 default:
311 return sprintf(buf, "unknown state\n");
315 static DEVICE_ATTR_RW(state);
317 static int apb_ctrl_get_devtree_data(struct platform_device *pdev,
318 struct arche_apb_ctrl_drvdata *apb)
320 struct device *dev = &pdev->dev;
321 int ret;
323 apb->resetn = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW);
324 if (IS_ERR(apb->resetn)) {
325 ret = PTR_ERR(apb->resetn);
326 dev_err(dev, "Failed requesting reset GPIO: %d\n", ret);
327 return ret;
330 apb->boot_ret = devm_gpiod_get(dev, "boot-ret", GPIOD_OUT_LOW);
331 if (IS_ERR(apb->boot_ret)) {
332 ret = PTR_ERR(apb->boot_ret);
333 dev_err(dev, "Failed requesting bootret GPIO: %d\n", ret);
334 return ret;
337 /* It's not mandatory to support power management interface */
338 apb->pwroff = devm_gpiod_get_optional(dev, "pwr-off", GPIOD_IN);
339 if (IS_ERR(apb->pwroff)) {
340 ret = PTR_ERR(apb->pwroff);
341 dev_err(dev, "Failed requesting pwroff_n GPIO: %d\n", ret);
342 return ret;
345 /* Do not make clock mandatory as of now (for DB3) */
346 apb->clk_en = devm_gpiod_get_optional(dev, "clock-en", GPIOD_OUT_LOW);
347 if (IS_ERR(apb->clk_en)) {
348 ret = PTR_ERR(apb->clk_en);
349 dev_err(dev, "Failed requesting APB clock en GPIO: %d\n", ret);
350 return ret;
353 apb->pwrdn = devm_gpiod_get(dev, "pwr-down", GPIOD_OUT_LOW);
354 if (IS_ERR(apb->pwrdn)) {
355 ret = PTR_ERR(apb->pwrdn);
356 dev_warn(dev, "Failed requesting power down GPIO: %d\n", ret);
357 return ret;
360 /* Regulators are optional, as we may have fixed supply coming in */
361 apb->vcore = devm_regulator_get(dev, "vcore");
362 if (IS_ERR(apb->vcore))
363 dev_warn(dev, "no core regulator found\n");
365 apb->vio = devm_regulator_get(dev, "vio");
366 if (IS_ERR(apb->vio))
367 dev_warn(dev, "no IO regulator found\n");
369 apb->pinctrl = devm_pinctrl_get(&pdev->dev);
370 if (IS_ERR(apb->pinctrl)) {
371 dev_err(&pdev->dev, "could not get pinctrl handle\n");
372 return PTR_ERR(apb->pinctrl);
374 apb->pin_default = pinctrl_lookup_state(apb->pinctrl, "default");
375 if (IS_ERR(apb->pin_default)) {
376 dev_err(&pdev->dev, "could not get default pin state\n");
377 return PTR_ERR(apb->pin_default);
380 /* Only applicable for platform >= V2 */
381 if (of_property_read_bool(pdev->dev.of_node, "gb,spi-en-active-high"))
382 apb->spi_en_polarity_high = true;
384 return 0;
387 static int arche_apb_ctrl_probe(struct platform_device *pdev)
389 int ret;
390 struct arche_apb_ctrl_drvdata *apb;
391 struct device *dev = &pdev->dev;
393 apb = devm_kzalloc(&pdev->dev, sizeof(*apb), GFP_KERNEL);
394 if (!apb)
395 return -ENOMEM;
397 ret = apb_ctrl_get_devtree_data(pdev, apb);
398 if (ret) {
399 dev_err(dev, "failed to get apb devicetree data %d\n", ret);
400 return ret;
403 /* Initially set APB to OFF state */
404 apb->state = ARCHE_PLATFORM_STATE_OFF;
405 /* Check whether device needs to be enabled on boot */
406 if (of_property_read_bool(pdev->dev.of_node, "arche,init-disable"))
407 apb->init_disabled = true;
409 platform_set_drvdata(pdev, apb);
411 /* Create sysfs interface to allow user to change state dynamically */
412 ret = device_create_file(dev, &dev_attr_state);
413 if (ret) {
414 dev_err(dev, "failed to create state file in sysfs\n");
415 return ret;
418 dev_info(&pdev->dev, "Device registered successfully\n");
419 return 0;
422 static int arche_apb_ctrl_remove(struct platform_device *pdev)
424 device_remove_file(&pdev->dev, &dev_attr_state);
425 poweroff_seq(pdev);
426 platform_set_drvdata(pdev, NULL);
428 return 0;
431 static int __maybe_unused arche_apb_ctrl_suspend(struct device *dev)
434 * If timing profile permits, we may shutdown bridge
435 * completely
437 * TODO: sequence ??
439 * Also, need to make sure we meet precondition for unipro suspend
440 * Precondition: Definition ???
442 return 0;
445 static int __maybe_unused arche_apb_ctrl_resume(struct device *dev)
448 * Atleast for ES2 we have to meet the delay requirement between
449 * unipro switch and AP bridge init, depending on whether bridge is in
450 * OFF state or standby state.
452 * Based on whether bridge is in standby or OFF state we may have to
453 * assert multiple signals. Please refer to WDM spec, for more info.
456 return 0;
459 static void arche_apb_ctrl_shutdown(struct platform_device *pdev)
461 apb_ctrl_poweroff(&pdev->dev);
464 static SIMPLE_DEV_PM_OPS(arche_apb_ctrl_pm_ops, arche_apb_ctrl_suspend,
465 arche_apb_ctrl_resume);
467 static const struct of_device_id arche_apb_ctrl_of_match[] = {
468 { .compatible = "usbffff,2", },
469 { },
472 static struct platform_driver arche_apb_ctrl_device_driver = {
473 .probe = arche_apb_ctrl_probe,
474 .remove = arche_apb_ctrl_remove,
475 .shutdown = arche_apb_ctrl_shutdown,
476 .driver = {
477 .name = "arche-apb-ctrl",
478 .pm = &arche_apb_ctrl_pm_ops,
479 .of_match_table = arche_apb_ctrl_of_match,
483 int __init arche_apb_init(void)
485 return platform_driver_register(&arche_apb_ctrl_device_driver);
488 void __exit arche_apb_exit(void)
490 platform_driver_unregister(&arche_apb_ctrl_device_driver);