1 // SPDX-License-Identifier: GPL-2.0
3 * Arche Platform driver to control APB.
5 * Copyright 2014-2015 Google Inc.
6 * Copyright 2014-2015 Linaro Ltd.
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>
19 #include <linux/regulator/consumer.h>
20 #include <linux/spinlock.h>
21 #include "arche_platform.h"
24 static void apb_bootret_deassert(struct device
*dev
);
26 struct arche_apb_ctrl_drvdata
{
27 /* Control GPIO signals to and from AP <=> AP Bridges */
35 enum arche_platform_state state
;
38 struct regulator
*vcore
;
39 struct regulator
*vio
;
44 struct pinctrl
*pinctrl
;
45 struct pinctrl_state
*pin_default
;
47 /* V2: SPI Bus control */
49 bool spi_en_polarity_high
;
53 * Note that these low level api's are active high
55 static inline void deassert_reset(unsigned int gpio
)
57 gpio_set_value(gpio
, 1);
60 static inline void assert_reset(unsigned int gpio
)
62 gpio_set_value(gpio
, 0);
66 * Note: Please do not modify the below sequence, as it is as per the spec
68 static int coldboot_seq(struct platform_device
*pdev
)
70 struct device
*dev
= &pdev
->dev
;
71 struct arche_apb_ctrl_drvdata
*apb
= platform_get_drvdata(pdev
);
74 if (apb
->init_disabled
||
75 apb
->state
== ARCHE_PLATFORM_STATE_ACTIVE
)
78 /* Hold APB in reset state */
79 assert_reset(apb
->resetn_gpio
);
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
);
85 /* Enable power to APB */
86 if (!IS_ERR(apb
->vcore
)) {
87 ret
= regulator_enable(apb
->vcore
);
89 dev_err(dev
, "failed to enable core regulator\n");
94 if (!IS_ERR(apb
->vio
)) {
95 ret
= regulator_enable(apb
->vio
);
97 dev_err(dev
, "failed to enable IO regulator\n");
102 apb_bootret_deassert(dev
);
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);
108 usleep_range(100, 200);
110 /* deassert reset to APB : Active-low signal */
111 deassert_reset(apb
->resetn_gpio
);
113 apb
->state
= ARCHE_PLATFORM_STATE_ACTIVE
;
118 static int fw_flashing_seq(struct platform_device
*pdev
)
120 struct device
*dev
= &pdev
->dev
;
121 struct arche_apb_ctrl_drvdata
*apb
= platform_get_drvdata(pdev
);
124 if (apb
->init_disabled
||
125 apb
->state
== ARCHE_PLATFORM_STATE_FW_FLASHING
)
128 ret
= regulator_enable(apb
->vcore
);
130 dev_err(dev
, "failed to enable core regulator\n");
134 ret
= regulator_enable(apb
->vio
);
136 dev_err(dev
, "failed to enable IO regulator\n");
140 if (gpio_is_valid(apb
->spi_en_gpio
)) {
143 if (apb
->spi_en_polarity_high
)
144 flags
= GPIOF_OUT_INIT_HIGH
;
146 flags
= GPIOF_OUT_INIT_LOW
;
148 ret
= devm_gpio_request_one(dev
, apb
->spi_en_gpio
,
149 flags
, "apb_spi_en");
151 dev_err(dev
, "Failed requesting SPI bus en gpio %d\n",
157 /* for flashing device should be in reset state */
158 assert_reset(apb
->resetn_gpio
);
159 apb
->state
= ARCHE_PLATFORM_STATE_FW_FLASHING
;
164 static int standby_boot_seq(struct platform_device
*pdev
)
166 struct device
*dev
= &pdev
->dev
;
167 struct arche_apb_ctrl_drvdata
*apb
= platform_get_drvdata(pdev
);
169 if (apb
->init_disabled
)
173 * Even if it is in OFF state,
174 * then we do not want to change the state
176 if (apb
->state
== ARCHE_PLATFORM_STATE_STANDBY
||
177 apb
->state
== ARCHE_PLATFORM_STATE_OFF
)
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
);
185 * As per WDM spec, do nothing
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
192 /* TODO: POWEROFF_L is input to WDM module */
193 apb
->state
= ARCHE_PLATFORM_STATE_STANDBY
;
197 static void poweroff_seq(struct platform_device
*pdev
)
199 struct device
*dev
= &pdev
->dev
;
200 struct arche_apb_ctrl_drvdata
*apb
= platform_get_drvdata(pdev
);
202 if (apb
->init_disabled
|| apb
->state
== ARCHE_PLATFORM_STATE_OFF
)
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
);
209 /* disable the clock */
210 if (gpio_is_valid(apb
->clk_en_gpio
))
211 gpio_set_value(apb
->clk_en_gpio
, 0);
213 if (!IS_ERR(apb
->vcore
) && regulator_is_enabled(apb
->vcore
) > 0)
214 regulator_disable(apb
->vcore
);
216 if (!IS_ERR(apb
->vio
) && regulator_is_enabled(apb
->vio
) > 0)
217 regulator_disable(apb
->vio
);
219 /* As part of exit, put APB back in reset state */
220 assert_reset(apb
->resetn_gpio
);
221 apb
->state
= ARCHE_PLATFORM_STATE_OFF
;
223 /* TODO: May have to send an event to SVC about this exit */
226 static void apb_bootret_deassert(struct device
*dev
)
228 struct arche_apb_ctrl_drvdata
*apb
= dev_get_drvdata(dev
);
230 gpio_set_value(apb
->boot_ret_gpio
, 0);
233 int apb_ctrl_coldboot(struct device
*dev
)
235 return coldboot_seq(to_platform_device(dev
));
238 int apb_ctrl_fw_flashing(struct device
*dev
)
240 return fw_flashing_seq(to_platform_device(dev
));
243 int apb_ctrl_standby_boot(struct device
*dev
)
245 return standby_boot_seq(to_platform_device(dev
));
248 void apb_ctrl_poweroff(struct device
*dev
)
250 poweroff_seq(to_platform_device(dev
));
253 static ssize_t
state_store(struct device
*dev
,
254 struct device_attribute
*attr
,
255 const char *buf
, size_t count
)
257 struct platform_device
*pdev
= to_platform_device(dev
);
258 struct arche_apb_ctrl_drvdata
*apb
= platform_get_drvdata(pdev
);
262 if (sysfs_streq(buf
, "off")) {
263 if (apb
->state
== ARCHE_PLATFORM_STATE_OFF
)
267 } else if (sysfs_streq(buf
, "active")) {
268 if (apb
->state
== ARCHE_PLATFORM_STATE_ACTIVE
)
272 is_disabled
= apb
->init_disabled
;
273 apb
->init_disabled
= false;
274 ret
= coldboot_seq(pdev
);
276 apb
->init_disabled
= is_disabled
;
277 } else if (sysfs_streq(buf
, "standby")) {
278 if (apb
->state
== ARCHE_PLATFORM_STATE_STANDBY
)
281 ret
= standby_boot_seq(pdev
);
282 } else if (sysfs_streq(buf
, "fw_flashing")) {
283 if (apb
->state
== ARCHE_PLATFORM_STATE_FW_FLASHING
)
287 * First we want to make sure we power off everything
288 * and then enter FW flashing state
291 ret
= fw_flashing_seq(pdev
);
293 dev_err(dev
, "unknown state\n");
297 return ret
? ret
: count
;
300 static ssize_t
state_show(struct device
*dev
,
301 struct device_attribute
*attr
, char *buf
)
303 struct arche_apb_ctrl_drvdata
*apb
= dev_get_drvdata(dev
);
305 switch (apb
->state
) {
306 case ARCHE_PLATFORM_STATE_OFF
:
307 return sprintf(buf
, "off%s\n",
308 apb
->init_disabled
? ",disabled" : "");
309 case ARCHE_PLATFORM_STATE_ACTIVE
:
310 return sprintf(buf
, "active\n");
311 case ARCHE_PLATFORM_STATE_STANDBY
:
312 return sprintf(buf
, "standby\n");
313 case ARCHE_PLATFORM_STATE_FW_FLASHING
:
314 return sprintf(buf
, "fw_flashing\n");
316 return sprintf(buf
, "unknown state\n");
320 static DEVICE_ATTR_RW(state
);
322 static int apb_ctrl_get_devtree_data(struct platform_device
*pdev
,
323 struct arche_apb_ctrl_drvdata
*apb
)
325 struct device
*dev
= &pdev
->dev
;
326 struct device_node
*np
= dev
->of_node
;
329 apb
->resetn_gpio
= of_get_named_gpio(np
, "reset-gpios", 0);
330 if (apb
->resetn_gpio
< 0) {
331 dev_err(dev
, "failed to get reset gpio\n");
332 return apb
->resetn_gpio
;
334 ret
= devm_gpio_request_one(dev
, apb
->resetn_gpio
,
335 GPIOF_OUT_INIT_LOW
, "apb-reset");
337 dev_err(dev
, "Failed requesting reset gpio %d\n",
342 apb
->boot_ret_gpio
= of_get_named_gpio(np
, "boot-ret-gpios", 0);
343 if (apb
->boot_ret_gpio
< 0) {
344 dev_err(dev
, "failed to get boot retention gpio\n");
345 return apb
->boot_ret_gpio
;
347 ret
= devm_gpio_request_one(dev
, apb
->boot_ret_gpio
,
348 GPIOF_OUT_INIT_LOW
, "boot retention");
350 dev_err(dev
, "Failed requesting bootret gpio %d\n",
355 /* It's not mandatory to support power management interface */
356 apb
->pwroff_gpio
= of_get_named_gpio(np
, "pwr-off-gpios", 0);
357 if (apb
->pwroff_gpio
< 0) {
358 dev_err(dev
, "failed to get power off gpio\n");
359 return apb
->pwroff_gpio
;
361 ret
= devm_gpio_request_one(dev
, apb
->pwroff_gpio
,
362 GPIOF_IN
, "pwroff_n");
364 dev_err(dev
, "Failed requesting pwroff_n gpio %d\n",
369 /* Do not make clock mandatory as of now (for DB3) */
370 apb
->clk_en_gpio
= of_get_named_gpio(np
, "clock-en-gpio", 0);
371 if (apb
->clk_en_gpio
< 0) {
372 dev_warn(dev
, "failed to get clock en gpio\n");
373 } else if (gpio_is_valid(apb
->clk_en_gpio
)) {
374 ret
= devm_gpio_request_one(dev
, apb
->clk_en_gpio
,
375 GPIOF_OUT_INIT_LOW
, "apb_clk_en");
377 dev_warn(dev
, "Failed requesting APB clock en gpio %d\n",
383 apb
->pwrdn_gpio
= of_get_named_gpio(np
, "pwr-down-gpios", 0);
384 if (apb
->pwrdn_gpio
< 0)
385 dev_warn(dev
, "failed to get power down gpio\n");
387 /* Regulators are optional, as we may have fixed supply coming in */
388 apb
->vcore
= devm_regulator_get(dev
, "vcore");
389 if (IS_ERR(apb
->vcore
))
390 dev_warn(dev
, "no core regulator found\n");
392 apb
->vio
= devm_regulator_get(dev
, "vio");
393 if (IS_ERR(apb
->vio
))
394 dev_warn(dev
, "no IO regulator found\n");
396 apb
->pinctrl
= devm_pinctrl_get(&pdev
->dev
);
397 if (IS_ERR(apb
->pinctrl
)) {
398 dev_err(&pdev
->dev
, "could not get pinctrl handle\n");
399 return PTR_ERR(apb
->pinctrl
);
401 apb
->pin_default
= pinctrl_lookup_state(apb
->pinctrl
, "default");
402 if (IS_ERR(apb
->pin_default
)) {
403 dev_err(&pdev
->dev
, "could not get default pin state\n");
404 return PTR_ERR(apb
->pin_default
);
407 /* Only applicable for platform >= V2 */
408 apb
->spi_en_gpio
= of_get_named_gpio(np
, "spi-en-gpio", 0);
409 if (apb
->spi_en_gpio
>= 0) {
410 if (of_property_read_bool(pdev
->dev
.of_node
,
411 "spi-en-active-high"))
412 apb
->spi_en_polarity_high
= true;
418 static int arche_apb_ctrl_probe(struct platform_device
*pdev
)
421 struct arche_apb_ctrl_drvdata
*apb
;
422 struct device
*dev
= &pdev
->dev
;
424 apb
= devm_kzalloc(&pdev
->dev
, sizeof(*apb
), GFP_KERNEL
);
428 ret
= apb_ctrl_get_devtree_data(pdev
, apb
);
430 dev_err(dev
, "failed to get apb devicetree data %d\n", ret
);
434 /* Initially set APB to OFF state */
435 apb
->state
= ARCHE_PLATFORM_STATE_OFF
;
436 /* Check whether device needs to be enabled on boot */
437 if (of_property_read_bool(pdev
->dev
.of_node
, "arche,init-disable"))
438 apb
->init_disabled
= true;
440 platform_set_drvdata(pdev
, apb
);
442 /* Create sysfs interface to allow user to change state dynamically */
443 ret
= device_create_file(dev
, &dev_attr_state
);
445 dev_err(dev
, "failed to create state file in sysfs\n");
449 dev_info(&pdev
->dev
, "Device registered successfully\n");
453 static int arche_apb_ctrl_remove(struct platform_device
*pdev
)
455 device_remove_file(&pdev
->dev
, &dev_attr_state
);
457 platform_set_drvdata(pdev
, NULL
);
462 static int __maybe_unused
arche_apb_ctrl_suspend(struct device
*dev
)
465 * If timing profile permits, we may shutdown bridge
470 * Also, need to make sure we meet precondition for unipro suspend
471 * Precondition: Definition ???
476 static int __maybe_unused
arche_apb_ctrl_resume(struct device
*dev
)
479 * Atleast for ES2 we have to meet the delay requirement between
480 * unipro switch and AP bridge init, depending on whether bridge is in
481 * OFF state or standby state.
483 * Based on whether bridge is in standby or OFF state we may have to
484 * assert multiple signals. Please refer to WDM spec, for more info.
490 static void arche_apb_ctrl_shutdown(struct platform_device
*pdev
)
492 apb_ctrl_poweroff(&pdev
->dev
);
495 static SIMPLE_DEV_PM_OPS(arche_apb_ctrl_pm_ops
, arche_apb_ctrl_suspend
,
496 arche_apb_ctrl_resume
);
498 static const struct of_device_id arche_apb_ctrl_of_match
[] = {
499 { .compatible
= "usbffff,2", },
503 static struct platform_driver arche_apb_ctrl_device_driver
= {
504 .probe
= arche_apb_ctrl_probe
,
505 .remove
= arche_apb_ctrl_remove
,
506 .shutdown
= arche_apb_ctrl_shutdown
,
508 .name
= "arche-apb-ctrl",
509 .pm
= &arche_apb_ctrl_pm_ops
,
510 .of_match_table
= arche_apb_ctrl_of_match
,
514 int __init
arche_apb_init(void)
516 return platform_driver_register(&arche_apb_ctrl_device_driver
);
519 void __exit
arche_apb_exit(void)
521 platform_driver_unregister(&arche_apb_ctrl_device_driver
);