act8945a-regulator.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Voltage regulation driver for active-semi ACT8945A PMIC
  4. *
  5. * Copyright (C) 2015 Atmel Corporation
  6. *
  7. * Author: Wenyou Yang <wenyou.yang@atmel.com>
  8. */
  9. #include <linux/module.h>
  10. #include <linux/of_device.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/regmap.h>
  13. #include <linux/regulator/driver.h>
  14. #include <linux/regulator/machine.h>
  15. #include <dt-bindings/regulator/active-semi,8945a-regulator.h>
  16. /**
  17. * ACT8945A Global Register Map.
  18. */
  19. #define ACT8945A_SYS_MODE 0x00
  20. #define ACT8945A_SYS_CTRL 0x01
  21. #define ACT8945A_SYS_UNLK_REGS 0x0b
  22. #define ACT8945A_DCDC1_VSET1 0x20
  23. #define ACT8945A_DCDC1_VSET2 0x21
  24. #define ACT8945A_DCDC1_CTRL 0x22
  25. #define ACT8945A_DCDC1_SUS 0x24
  26. #define ACT8945A_DCDC2_VSET1 0x30
  27. #define ACT8945A_DCDC2_VSET2 0x31
  28. #define ACT8945A_DCDC2_CTRL 0x32
  29. #define ACT8945A_DCDC2_SUS 0x34
  30. #define ACT8945A_DCDC3_VSET1 0x40
  31. #define ACT8945A_DCDC3_VSET2 0x41
  32. #define ACT8945A_DCDC3_CTRL 0x42
  33. #define ACT8945A_DCDC3_SUS 0x44
  34. #define ACT8945A_LDO1_VSET 0x50
  35. #define ACT8945A_LDO1_CTRL 0x51
  36. #define ACT8945A_LDO1_SUS 0x52
  37. #define ACT8945A_LDO2_VSET 0x54
  38. #define ACT8945A_LDO2_CTRL 0x55
  39. #define ACT8945A_LDO2_SUS 0x56
  40. #define ACT8945A_LDO3_VSET 0x60
  41. #define ACT8945A_LDO3_CTRL 0x61
  42. #define ACT8945A_LDO3_SUS 0x62
  43. #define ACT8945A_LDO4_VSET 0x64
  44. #define ACT8945A_LDO4_CTRL 0x65
  45. #define ACT8945A_LDO4_SUS 0x66
  46. /**
  47. * Field Definitions.
  48. */
  49. #define ACT8945A_ENA 0x80 /* ON - [7] */
  50. #define ACT8945A_VSEL_MASK 0x3F /* VSET - [5:0] */
  51. /**
  52. * ACT8945A Voltage Number
  53. */
  54. #define ACT8945A_VOLTAGE_NUM 64
  55. enum {
  56. ACT8945A_ID_DCDC1,
  57. ACT8945A_ID_DCDC2,
  58. ACT8945A_ID_DCDC3,
  59. ACT8945A_ID_LDO1,
  60. ACT8945A_ID_LDO2,
  61. ACT8945A_ID_LDO3,
  62. ACT8945A_ID_LDO4,
  63. ACT8945A_ID_MAX,
  64. };
  65. struct act8945a_pmic {
  66. struct regmap *regmap;
  67. u32 op_mode[ACT8945A_ID_MAX];
  68. };
  69. static const struct regulator_linear_range act8945a_voltage_ranges[] = {
  70. REGULATOR_LINEAR_RANGE(600000, 0, 23, 25000),
  71. REGULATOR_LINEAR_RANGE(1200000, 24, 47, 50000),
  72. REGULATOR_LINEAR_RANGE(2400000, 48, 63, 100000),
  73. };
  74. static int act8945a_set_suspend_state(struct regulator_dev *rdev, bool enable)
  75. {
  76. struct regmap *regmap = rdev->regmap;
  77. int id = rdev_get_id(rdev);
  78. int reg, val;
  79. switch (id) {
  80. case ACT8945A_ID_DCDC1:
  81. reg = ACT8945A_DCDC1_SUS;
  82. val = 0xa8;
  83. break;
  84. case ACT8945A_ID_DCDC2:
  85. reg = ACT8945A_DCDC2_SUS;
  86. val = 0xa8;
  87. break;
  88. case ACT8945A_ID_DCDC3:
  89. reg = ACT8945A_DCDC3_SUS;
  90. val = 0xa8;
  91. break;
  92. case ACT8945A_ID_LDO1:
  93. reg = ACT8945A_LDO1_SUS;
  94. val = 0xe8;
  95. break;
  96. case ACT8945A_ID_LDO2:
  97. reg = ACT8945A_LDO2_SUS;
  98. val = 0xe8;
  99. break;
  100. case ACT8945A_ID_LDO3:
  101. reg = ACT8945A_LDO3_SUS;
  102. val = 0xe8;
  103. break;
  104. case ACT8945A_ID_LDO4:
  105. reg = ACT8945A_LDO4_SUS;
  106. val = 0xe8;
  107. break;
  108. default:
  109. return -EINVAL;
  110. }
  111. if (enable)
  112. val |= BIT(4);
  113. /*
  114. * Ask the PMIC to enable/disable this output when entering hibernate
  115. * mode.
  116. */
  117. return regmap_write(regmap, reg, val);
  118. }
  119. static int act8945a_set_suspend_enable(struct regulator_dev *rdev)
  120. {
  121. return act8945a_set_suspend_state(rdev, true);
  122. }
  123. static int act8945a_set_suspend_disable(struct regulator_dev *rdev)
  124. {
  125. return act8945a_set_suspend_state(rdev, false);
  126. }
  127. static unsigned int act8945a_of_map_mode(unsigned int mode)
  128. {
  129. switch (mode) {
  130. case ACT8945A_REGULATOR_MODE_FIXED:
  131. case ACT8945A_REGULATOR_MODE_NORMAL:
  132. return REGULATOR_MODE_NORMAL;
  133. case ACT8945A_REGULATOR_MODE_LOWPOWER:
  134. return REGULATOR_MODE_STANDBY;
  135. default:
  136. return REGULATOR_MODE_INVALID;
  137. }
  138. }
  139. static int act8945a_set_mode(struct regulator_dev *rdev, unsigned int mode)
  140. {
  141. struct act8945a_pmic *act8945a = rdev_get_drvdata(rdev);
  142. struct regmap *regmap = rdev->regmap;
  143. int id = rdev_get_id(rdev);
  144. int reg, ret, val = 0;
  145. switch (id) {
  146. case ACT8945A_ID_DCDC1:
  147. reg = ACT8945A_DCDC1_CTRL;
  148. break;
  149. case ACT8945A_ID_DCDC2:
  150. reg = ACT8945A_DCDC2_CTRL;
  151. break;
  152. case ACT8945A_ID_DCDC3:
  153. reg = ACT8945A_DCDC3_CTRL;
  154. break;
  155. case ACT8945A_ID_LDO1:
  156. reg = ACT8945A_LDO1_CTRL;
  157. break;
  158. case ACT8945A_ID_LDO2:
  159. reg = ACT8945A_LDO2_CTRL;
  160. break;
  161. case ACT8945A_ID_LDO3:
  162. reg = ACT8945A_LDO3_CTRL;
  163. break;
  164. case ACT8945A_ID_LDO4:
  165. reg = ACT8945A_LDO4_CTRL;
  166. break;
  167. default:
  168. return -EINVAL;
  169. }
  170. switch (mode) {
  171. case REGULATOR_MODE_STANDBY:
  172. if (id > ACT8945A_ID_DCDC3)
  173. val = BIT(5);
  174. break;
  175. case REGULATOR_MODE_NORMAL:
  176. if (id <= ACT8945A_ID_DCDC3)
  177. val = BIT(5);
  178. break;
  179. default:
  180. return -EINVAL;
  181. }
  182. ret = regmap_update_bits(regmap, reg, BIT(5), val);
  183. if (ret)
  184. return ret;
  185. act8945a->op_mode[id] = mode;
  186. return 0;
  187. }
  188. static unsigned int act8945a_get_mode(struct regulator_dev *rdev)
  189. {
  190. struct act8945a_pmic *act8945a = rdev_get_drvdata(rdev);
  191. int id = rdev_get_id(rdev);
  192. if (id < ACT8945A_ID_DCDC1 || id >= ACT8945A_ID_MAX)
  193. return -EINVAL;
  194. return act8945a->op_mode[id];
  195. }
  196. static const struct regulator_ops act8945a_ops = {
  197. .list_voltage = regulator_list_voltage_linear_range,
  198. .map_voltage = regulator_map_voltage_linear_range,
  199. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  200. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  201. .enable = regulator_enable_regmap,
  202. .disable = regulator_disable_regmap,
  203. .set_mode = act8945a_set_mode,
  204. .get_mode = act8945a_get_mode,
  205. .is_enabled = regulator_is_enabled_regmap,
  206. .set_suspend_enable = act8945a_set_suspend_enable,
  207. .set_suspend_disable = act8945a_set_suspend_disable,
  208. };
  209. #define ACT89xx_REG(_name, _family, _id, _vsel_reg, _supply) \
  210. [_family##_ID_##_id] = { \
  211. .name = _name, \
  212. .supply_name = _supply, \
  213. .of_match = of_match_ptr("REG_"#_id), \
  214. .of_map_mode = act8945a_of_map_mode, \
  215. .regulators_node = of_match_ptr("regulators"), \
  216. .id = _family##_ID_##_id, \
  217. .type = REGULATOR_VOLTAGE, \
  218. .ops = &act8945a_ops, \
  219. .n_voltages = ACT8945A_VOLTAGE_NUM, \
  220. .linear_ranges = act8945a_voltage_ranges, \
  221. .n_linear_ranges = ARRAY_SIZE(act8945a_voltage_ranges), \
  222. .vsel_reg = _family##_##_id##_##_vsel_reg, \
  223. .vsel_mask = ACT8945A_VSEL_MASK, \
  224. .enable_reg = _family##_##_id##_CTRL, \
  225. .enable_mask = ACT8945A_ENA, \
  226. .owner = THIS_MODULE, \
  227. }
  228. static const struct regulator_desc act8945a_regulators[] = {
  229. ACT89xx_REG("DCDC_REG1", ACT8945A, DCDC1, VSET1, "vp1"),
  230. ACT89xx_REG("DCDC_REG2", ACT8945A, DCDC2, VSET1, "vp2"),
  231. ACT89xx_REG("DCDC_REG3", ACT8945A, DCDC3, VSET1, "vp3"),
  232. ACT89xx_REG("LDO_REG1", ACT8945A, LDO1, VSET, "inl45"),
  233. ACT89xx_REG("LDO_REG2", ACT8945A, LDO2, VSET, "inl45"),
  234. ACT89xx_REG("LDO_REG3", ACT8945A, LDO3, VSET, "inl67"),
  235. ACT89xx_REG("LDO_REG4", ACT8945A, LDO4, VSET, "inl67"),
  236. };
  237. static const struct regulator_desc act8945a_alt_regulators[] = {
  238. ACT89xx_REG("DCDC_REG1", ACT8945A, DCDC1, VSET2, "vp1"),
  239. ACT89xx_REG("DCDC_REG2", ACT8945A, DCDC2, VSET2, "vp2"),
  240. ACT89xx_REG("DCDC_REG3", ACT8945A, DCDC3, VSET2, "vp3"),
  241. ACT89xx_REG("LDO_REG1", ACT8945A, LDO1, VSET, "inl45"),
  242. ACT89xx_REG("LDO_REG2", ACT8945A, LDO2, VSET, "inl45"),
  243. ACT89xx_REG("LDO_REG3", ACT8945A, LDO3, VSET, "inl67"),
  244. ACT89xx_REG("LDO_REG4", ACT8945A, LDO4, VSET, "inl67"),
  245. };
  246. static int act8945a_pmic_probe(struct platform_device *pdev)
  247. {
  248. struct regulator_config config = { };
  249. const struct regulator_desc *regulators;
  250. struct act8945a_pmic *act8945a;
  251. struct regulator_dev *rdev;
  252. int i, num_regulators;
  253. bool voltage_select;
  254. act8945a = devm_kzalloc(&pdev->dev, sizeof(*act8945a), GFP_KERNEL);
  255. if (!act8945a)
  256. return -ENOMEM;
  257. act8945a->regmap = dev_get_regmap(pdev->dev.parent, NULL);
  258. if (!act8945a->regmap) {
  259. dev_err(&pdev->dev,
  260. "could not retrieve regmap from parent device\n");
  261. return -EINVAL;
  262. }
  263. voltage_select = of_property_read_bool(pdev->dev.parent->of_node,
  264. "active-semi,vsel-high");
  265. if (voltage_select) {
  266. regulators = act8945a_alt_regulators;
  267. num_regulators = ARRAY_SIZE(act8945a_alt_regulators);
  268. } else {
  269. regulators = act8945a_regulators;
  270. num_regulators = ARRAY_SIZE(act8945a_regulators);
  271. }
  272. config.dev = &pdev->dev;
  273. config.dev->of_node = pdev->dev.parent->of_node;
  274. config.driver_data = act8945a;
  275. for (i = 0; i < num_regulators; i++) {
  276. rdev = devm_regulator_register(&pdev->dev, &regulators[i],
  277. &config);
  278. if (IS_ERR(rdev)) {
  279. dev_err(&pdev->dev,
  280. "failed to register %s regulator\n",
  281. regulators[i].name);
  282. return PTR_ERR(rdev);
  283. }
  284. }
  285. platform_set_drvdata(pdev, act8945a);
  286. /* Unlock expert registers. */
  287. return regmap_write(act8945a->regmap, ACT8945A_SYS_UNLK_REGS, 0xef);
  288. }
  289. static int __maybe_unused act8945a_suspend(struct device *pdev)
  290. {
  291. struct act8945a_pmic *act8945a = dev_get_drvdata(pdev);
  292. /*
  293. * Ask the PMIC to enter the suspend mode on the next PWRHLD
  294. * transition.
  295. */
  296. return regmap_write(act8945a->regmap, ACT8945A_SYS_CTRL, 0x42);
  297. }
  298. static SIMPLE_DEV_PM_OPS(act8945a_pm, act8945a_suspend, NULL);
  299. static void act8945a_pmic_shutdown(struct platform_device *pdev)
  300. {
  301. struct act8945a_pmic *act8945a = platform_get_drvdata(pdev);
  302. /*
  303. * Ask the PMIC to shutdown everything on the next PWRHLD transition.
  304. */
  305. regmap_write(act8945a->regmap, ACT8945A_SYS_CTRL, 0x0);
  306. }
  307. static struct platform_driver act8945a_pmic_driver = {
  308. .driver = {
  309. .name = "act8945a-regulator",
  310. .pm = &act8945a_pm,
  311. },
  312. .probe = act8945a_pmic_probe,
  313. .shutdown = act8945a_pmic_shutdown,
  314. };
  315. module_platform_driver(act8945a_pmic_driver);
  316. MODULE_DESCRIPTION("Active-semi ACT8945A voltage regulator driver");
  317. MODULE_AUTHOR("Wenyou Yang <wenyou.yang@atmel.com>");
  318. MODULE_LICENSE("GPL");