212-regulator-add-axp20x-regulator-support.patch 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. From c3af279a9031b3375d3e5a684619c1adbbe30da9 Mon Sep 17 00:00:00 2001
  2. From: Carlo Caione <carlo@caione.org>
  3. Date: Sat, 1 Mar 2014 17:45:51 +0100
  4. Subject: [PATCH] regulator: AXP20x: Add support for regulators subsystem
  5. AXP202 and AXP209 come with two synchronous step-down DC-DCs and five
  6. LDOs. This patch introduces basic support for those regulators.
  7. Signed-off-by: Carlo Caione <carlo@caione.org>
  8. ---
  9. arch/arm/configs/sunxi_defconfig | 1 +
  10. drivers/regulator/Kconfig | 7 +
  11. drivers/regulator/Makefile | 1 +
  12. drivers/regulator/axp20x-regulator.c | 349 +++++++++++++++++++++++++++++++++++
  13. 4 files changed, 358 insertions(+)
  14. create mode 100644 drivers/regulator/axp20x-regulator.c
  15. --- a/arch/arm/configs/sunxi_defconfig
  16. +++ b/arch/arm/configs/sunxi_defconfig
  17. @@ -72,3 +72,4 @@ CONFIG_NFS_FS=y
  18. CONFIG_ROOT_NFS=y
  19. CONFIG_NLS=y
  20. CONFIG_PRINTK_TIME=y
  21. +CONFIG_REGULATOR_AXP20X=y
  22. --- a/drivers/regulator/Kconfig
  23. +++ b/drivers/regulator/Kconfig
  24. @@ -139,6 +139,13 @@ config REGULATOR_AS3722
  25. AS3722 PMIC. This will enable support for all the software
  26. controllable DCDC/LDO regulators.
  27. +config REGULATOR_AXP20X
  28. + tristate "X-POWERS AXP20X PMIC Regulators"
  29. + depends on MFD_AXP20X
  30. + help
  31. + This driver provides support for the voltage regulators on the
  32. + AXP20X PMIC.
  33. +
  34. config REGULATOR_DA903X
  35. tristate "Dialog Semiconductor DA9030/DA9034 regulators"
  36. depends on PMIC_DA903X
  37. --- a/drivers/regulator/Makefile
  38. +++ b/drivers/regulator/Makefile
  39. @@ -20,6 +20,7 @@ obj-$(CONFIG_REGULATOR_ANATOP) += anatop
  40. obj-$(CONFIG_REGULATOR_ARIZONA) += arizona-micsupp.o arizona-ldo1.o
  41. obj-$(CONFIG_REGULATOR_AS3711) += as3711-regulator.o
  42. obj-$(CONFIG_REGULATOR_AS3722) += as3722-regulator.o
  43. +obj-$(CONFIG_REGULATOR_AXP20X) += axp20x-regulator.o
  44. obj-$(CONFIG_REGULATOR_DA903X) += da903x.o
  45. obj-$(CONFIG_REGULATOR_DA9052) += da9052-regulator.o
  46. obj-$(CONFIG_REGULATOR_DA9055) += da9055-regulator.o
  47. --- /dev/null
  48. +++ b/drivers/regulator/axp20x-regulator.c
  49. @@ -0,0 +1,349 @@
  50. +/*
  51. + * axp20x regulators driver.
  52. + *
  53. + * Copyright (C) 2013 Carlo Caione <carlo@caione.org>
  54. + *
  55. + * This file is subject to the terms and conditions of the GNU General
  56. + * Public License. See the file "COPYING" in the main directory of this
  57. + * archive for more details.
  58. + *
  59. + * This program is distributed in the hope that it will be useful,
  60. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  61. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  62. + * GNU General Public License for more details.
  63. + */
  64. +
  65. +#include <linux/module.h>
  66. +#include <linux/init.h>
  67. +#include <linux/err.h>
  68. +#include <linux/platform_device.h>
  69. +#include <linux/of.h>
  70. +#include <linux/of_device.h>
  71. +#include <linux/regulator/driver.h>
  72. +#include <linux/regulator/of_regulator.h>
  73. +#include <linux/mfd/axp20x.h>
  74. +#include <linux/regmap.h>
  75. +
  76. +#define AXP20X_IO_ENABLED (0x03)
  77. +
  78. +#define AXP20X_WORKMODE_DCDC2_MASK BIT(2)
  79. +#define AXP20X_WORKMODE_DCDC3_MASK BIT(1)
  80. +
  81. +#define AXP20X_FREQ_DCDC_MASK (0x0f)
  82. +
  83. +struct axp20x_regulators {
  84. + struct regulator_desc rdesc[AXP20X_REG_ID_MAX];
  85. + struct regulator_dev *rdev[AXP20X_REG_ID_MAX];
  86. + struct axp20x_dev *axp20x;
  87. +};
  88. +
  89. +#define AXP20X_DESC(_id, _min, _max, _step, _vreg, _vmask, _ereg, _emask) \
  90. + [AXP20X_##_id] = { \
  91. + .name = #_id, \
  92. + .type = REGULATOR_VOLTAGE, \
  93. + .id = AXP20X_##_id, \
  94. + .n_voltages = (((_max) - (_min)) / (_step) + 1), \
  95. + .owner = THIS_MODULE, \
  96. + .min_uV = (_min) * 1000, \
  97. + .uV_step = (_step) * 1000, \
  98. + .vsel_reg = (_vreg), \
  99. + .vsel_mask = (_vmask), \
  100. + .enable_reg = (_ereg), \
  101. + .enable_mask = (_emask), \
  102. + .ops = &axp20x_ops, \
  103. + }
  104. +
  105. +#define AXP20X_DESC_IO(_id, _min, _max, _step, _vreg, _vmask, _ereg, _emask) \
  106. + [AXP20X_##_id] = { \
  107. + .name = #_id, \
  108. + .type = REGULATOR_VOLTAGE, \
  109. + .id = AXP20X_##_id, \
  110. + .n_voltages = (((_max) - (_min)) / (_step) + 1), \
  111. + .owner = THIS_MODULE, \
  112. + .min_uV = (_min) * 1000, \
  113. + .uV_step = (_step) * 1000, \
  114. + .vsel_reg = (_vreg), \
  115. + .vsel_mask = (_vmask), \
  116. + .enable_reg = (_ereg), \
  117. + .enable_mask = (_emask), \
  118. + .ops = &axp20x_ops_io, \
  119. + }
  120. +
  121. +#define AXP20X_DESC_FIXED(_id, _volt) \
  122. + [AXP20X_##_id] = { \
  123. + .name = #_id, \
  124. + .type = REGULATOR_VOLTAGE, \
  125. + .id = AXP20X_##_id, \
  126. + .n_voltages = 1, \
  127. + .owner = THIS_MODULE, \
  128. + .min_uV = (_volt) * 1000, \
  129. + .ops = &axp20x_ops, \
  130. + }
  131. +
  132. +#define AXP20X_DESC_TABLE(_id, _table, _vreg, _vmask, _ereg, _emask) \
  133. + [AXP20X_##_id] = { \
  134. + .name = #_id, \
  135. + .type = REGULATOR_VOLTAGE, \
  136. + .id = AXP20X_##_id, \
  137. + .n_voltages = ARRAY_SIZE(_table), \
  138. + .owner = THIS_MODULE, \
  139. + .vsel_reg = (_vreg), \
  140. + .vsel_mask = (_vmask), \
  141. + .enable_reg = (_ereg), \
  142. + .enable_mask = (_emask), \
  143. + .volt_table = (_table), \
  144. + .ops = &axp20x_ops_table, \
  145. + }
  146. +
  147. +static int axp20x_ldo4_data[] = { 1250000, 1300000, 1400000, 1500000, 1600000, 1700000,
  148. + 1800000, 1900000, 2000000, 2500000, 2700000, 2800000,
  149. + 3000000, 3100000, 3200000, 3300000 };
  150. +
  151. +static int axp20x_set_suspend_voltage(struct regulator_dev *rdev, int uV)
  152. +{
  153. + return regulator_set_voltage_sel_regmap(rdev, 0);
  154. +}
  155. +
  156. +static int axp20x_io_enable_regmap(struct regulator_dev *rdev)
  157. +{
  158. + return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
  159. + rdev->desc->enable_mask, AXP20X_IO_ENABLED);
  160. +}
  161. +
  162. +static int axp109_io_is_enabled_regmap(struct regulator_dev *rdev)
  163. +{
  164. + unsigned int val;
  165. + int ret;
  166. +
  167. + ret = regmap_read(rdev->regmap, rdev->desc->enable_reg, &val);
  168. + if (ret != 0)
  169. + return ret;
  170. +
  171. + val &= rdev->desc->enable_mask;
  172. + return (val == AXP20X_IO_ENABLED);
  173. +}
  174. +
  175. +static struct regulator_ops axp20x_ops_table = {
  176. + .set_voltage_sel = regulator_set_voltage_sel_regmap,
  177. + .get_voltage_sel = regulator_get_voltage_sel_regmap,
  178. + .list_voltage = regulator_list_voltage_table,
  179. + .enable = regulator_enable_regmap,
  180. + .disable = regulator_disable_regmap,
  181. + .is_enabled = regulator_is_enabled_regmap,
  182. + .set_suspend_enable = regulator_enable_regmap,
  183. + .set_suspend_disable = regulator_disable_regmap,
  184. + .set_suspend_voltage = axp20x_set_suspend_voltage,
  185. +};
  186. +
  187. +
  188. +static struct regulator_ops axp20x_ops = {
  189. + .set_voltage_sel = regulator_set_voltage_sel_regmap,
  190. + .get_voltage_sel = regulator_get_voltage_sel_regmap,
  191. + .list_voltage = regulator_list_voltage_linear,
  192. + .enable = regulator_enable_regmap,
  193. + .disable = regulator_disable_regmap,
  194. + .is_enabled = regulator_is_enabled_regmap,
  195. + .set_suspend_enable = regulator_enable_regmap,
  196. + .set_suspend_disable = regulator_disable_regmap,
  197. + .set_suspend_voltage = axp20x_set_suspend_voltage,
  198. +};
  199. +
  200. +static struct regulator_ops axp20x_ops_io = {
  201. + .set_voltage_sel = regulator_set_voltage_sel_regmap,
  202. + .get_voltage_sel = regulator_get_voltage_sel_regmap,
  203. + .list_voltage = regulator_list_voltage_linear,
  204. + .enable = axp20x_io_enable_regmap,
  205. + .disable = regulator_disable_regmap,
  206. + .is_enabled = axp109_io_is_enabled_regmap,
  207. + .set_suspend_enable = regulator_enable_regmap,
  208. + .set_suspend_disable = regulator_disable_regmap,
  209. + .set_suspend_voltage = axp20x_set_suspend_voltage,
  210. +};
  211. +
  212. +static struct regulator_desc axp20x_regulators[] = {
  213. + AXP20X_DESC(DCDC2, 700, 2275, 25, AXP20X_DCDC2_V_OUT, 0x3f,
  214. + AXP20X_PWR_OUT_CTRL, 0x10),
  215. + AXP20X_DESC(DCDC3, 700, 3500, 25, AXP20X_DCDC3_V_OUT, 0x7f,
  216. + AXP20X_PWR_OUT_CTRL, 0x02),
  217. + AXP20X_DESC_FIXED(LDO1, 1300),
  218. + AXP20X_DESC(LDO2, 1800, 3300, 100, AXP20X_LDO24_V_OUT, 0xf0,
  219. + AXP20X_PWR_OUT_CTRL, 0x04),
  220. + AXP20X_DESC(LDO3, 700, 3500, 25, AXP20X_LDO3_V_OUT, 0x7f,
  221. + AXP20X_PWR_OUT_CTRL, 0x40),
  222. + AXP20X_DESC_TABLE(LDO4, axp20x_ldo4_data, AXP20X_LDO24_V_OUT, 0x0f,
  223. + AXP20X_PWR_OUT_CTRL, 0x08),
  224. + AXP20X_DESC_IO(LDO5, 1800, 3300, 100, AXP20X_LDO5_V_OUT, 0xf0,
  225. + AXP20X_GPIO0_CTRL, 0x07),
  226. +};
  227. +
  228. +#define AXP_MATCH(_name, _id) \
  229. + [AXP20X_##_id] = { \
  230. + .name = #_name, \
  231. + .driver_data = (void *) &axp20x_regulators[AXP20X_##_id], \
  232. + }
  233. +
  234. +static struct of_regulator_match axp20x_matches[] = {
  235. + AXP_MATCH(dcdc2, DCDC2),
  236. + AXP_MATCH(dcdc3, DCDC3),
  237. + AXP_MATCH(ldo1, LDO1),
  238. + AXP_MATCH(ldo2, LDO2),
  239. + AXP_MATCH(ldo3, LDO3),
  240. + AXP_MATCH(ldo4, LDO4),
  241. + AXP_MATCH(ldo5, LDO5),
  242. +};
  243. +
  244. +static int axp20x_set_dcdc_freq(struct platform_device *pdev, u32 dcdcfreq)
  245. +{
  246. + struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent);
  247. +
  248. + if (dcdcfreq < 750)
  249. + dcdcfreq = 750;
  250. +
  251. + if (dcdcfreq > 1875)
  252. + dcdcfreq = 1875;
  253. +
  254. + dcdcfreq = (dcdcfreq - 750) / 75;
  255. +
  256. + return regmap_update_bits(axp20x->regmap, AXP20X_DCDC_FREQ,
  257. + AXP20X_FREQ_DCDC_MASK, dcdcfreq);
  258. +}
  259. +
  260. +static int axp20x_regulator_parse_dt(struct platform_device *pdev)
  261. +{
  262. + struct device_node *np, *regulators;
  263. + int ret;
  264. + u32 dcdcfreq;
  265. +
  266. + np = of_node_get(pdev->dev.parent->of_node);
  267. + if (!np)
  268. + return 0;
  269. +
  270. + regulators = of_find_node_by_name(np, "regulators");
  271. + if (!regulators) {
  272. + dev_err(&pdev->dev, "regulators node not found\n");
  273. + return -EINVAL;
  274. + }
  275. +
  276. + ret = of_regulator_match(&pdev->dev, regulators, axp20x_matches,
  277. + ARRAY_SIZE(axp20x_matches));
  278. + if (ret < 0) {
  279. + dev_err(&pdev->dev, "Error parsing regulator init data: %d\n",
  280. + ret);
  281. + return ret;
  282. + }
  283. +
  284. + dcdcfreq = 0x08;
  285. + of_property_read_u32(regulators, "dcdc-freq", &dcdcfreq);
  286. + ret = axp20x_set_dcdc_freq(pdev, dcdcfreq);
  287. + if (ret < 0) {
  288. + dev_err(&pdev->dev, "Error setting dcdc frequency: %d\n", ret);
  289. + return ret;
  290. + }
  291. +
  292. + of_node_put(regulators);
  293. +
  294. + return 0;
  295. +}
  296. +
  297. +static int axp20x_set_dcdc_workmode(struct regulator_dev *rdev, int id, u32 workmode)
  298. +{
  299. + unsigned int mask = AXP20X_WORKMODE_DCDC2_MASK;
  300. +
  301. + if ((id != AXP20X_DCDC2) && (id != AXP20X_DCDC3))
  302. + return -EINVAL;
  303. +
  304. + if (id == AXP20X_DCDC3)
  305. + mask = AXP20X_WORKMODE_DCDC3_MASK;
  306. +
  307. + return regmap_update_bits(rdev->regmap, AXP20X_DCDC_MODE, mask, workmode);
  308. +}
  309. +
  310. +static int axp20x_regulator_probe(struct platform_device *pdev)
  311. +{
  312. + struct axp20x_regulators *pmic;
  313. + struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent);
  314. + struct regulator_config config = { };
  315. + struct regulator_init_data *init_data;
  316. + int ret, i;
  317. + u32 workmode;
  318. +
  319. + ret = axp20x_regulator_parse_dt(pdev);
  320. + if (ret)
  321. + return ret;
  322. +
  323. + pmic = devm_kzalloc(&pdev->dev, sizeof(*pmic), GFP_KERNEL);
  324. + if (!pmic) {
  325. + dev_err(&pdev->dev, "Failed to alloc pmic\n");
  326. + return -ENOMEM;
  327. + }
  328. +
  329. + pmic->axp20x = axp20x;
  330. + memcpy(pmic->rdesc, axp20x_regulators, sizeof(pmic->rdesc));
  331. + platform_set_drvdata(pdev, pmic);
  332. +
  333. + for (i = 0; i < AXP20X_REG_ID_MAX; i++) {
  334. + init_data = axp20x_matches[i].init_data;
  335. + if (!init_data)
  336. + continue;
  337. +
  338. + config.dev = &pdev->dev;
  339. + config.init_data = init_data;
  340. + config.driver_data = pmic;
  341. + config.regmap = axp20x->regmap;
  342. + config.of_node = axp20x_matches[i].of_node;
  343. +
  344. + pmic->rdev[i] = regulator_register(&pmic->rdesc[i], &config);
  345. + if (IS_ERR(pmic->rdev[i])) {
  346. + ret = PTR_ERR(pmic->rdev[i]);
  347. + dev_err(&pdev->dev, "Failed to register %s\n",
  348. + pmic->rdesc[i].name);
  349. +
  350. + while (--i >= 0)
  351. + regulator_unregister(pmic->rdev[i]);
  352. +
  353. + return ret;
  354. + }
  355. +
  356. + ret = of_property_read_u32(axp20x_matches[i].of_node, "dcdc-workmode",
  357. + &workmode);
  358. + if (!ret) {
  359. + ret = axp20x_set_dcdc_workmode(pmic->rdev[i], i, workmode);
  360. + if (ret)
  361. + dev_err(&pdev->dev, "Failed to set workmode on %s\n",
  362. + pmic->rdesc[i].name);
  363. + }
  364. + }
  365. +
  366. + return 0;
  367. +}
  368. +
  369. +static int axp20x_regulator_remove(struct platform_device *pdev)
  370. +{
  371. + struct axp20x_regulators *pmic = platform_get_drvdata(pdev);
  372. + int i;
  373. +
  374. + for (i = 0; i < AXP20X_REG_ID_MAX; i++)
  375. + regulator_unregister(pmic->rdev[i]);
  376. +
  377. + return 0;
  378. +}
  379. +
  380. +static struct platform_driver axp20x_regulator_driver = {
  381. + .probe = axp20x_regulator_probe,
  382. + .remove = axp20x_regulator_remove,
  383. + .driver = {
  384. + .name = "axp20x-regulator",
  385. + .owner = THIS_MODULE,
  386. + },
  387. +};
  388. +
  389. +static int __init axp20x_regulator_init(void)
  390. +{
  391. + return platform_driver_register(&axp20x_regulator_driver);
  392. +}
  393. +
  394. +subsys_initcall(axp20x_regulator_init);
  395. +
  396. +MODULE_LICENSE("GPL v2");
  397. +MODULE_AUTHOR("Carlo Caione <carlo@caione.org>");
  398. +MODULE_DESCRIPTION("Regulator Driver for AXP20X PMIC");