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