arche-apb-ctrl.c 13 KB

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