as3711-regulator.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /*
  2. * AS3711 PMIC regulator driver, using DCDC Step Down and LDO supplies
  3. *
  4. * Copyright (C) 2012 Renesas Electronics Corporation
  5. * Author: Guennadi Liakhovetski, <g.liakhovetski@gmx.de>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the version 2 of the GNU General Public License as
  9. * published by the Free Software Foundation
  10. */
  11. #include <linux/err.h>
  12. #include <linux/init.h>
  13. #include <linux/mfd/as3711.h>
  14. #include <linux/module.h>
  15. #include <linux/of.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/regmap.h>
  18. #include <linux/regulator/driver.h>
  19. #include <linux/regulator/of_regulator.h>
  20. #include <linux/slab.h>
  21. struct as3711_regulator_info {
  22. struct regulator_desc desc;
  23. unsigned int max_uV;
  24. };
  25. struct as3711_regulator {
  26. struct as3711_regulator_info *reg_info;
  27. struct regulator_dev *rdev;
  28. };
  29. static int as3711_list_voltage_sd(struct regulator_dev *rdev,
  30. unsigned int selector)
  31. {
  32. if (selector >= rdev->desc->n_voltages)
  33. return -EINVAL;
  34. if (!selector)
  35. return 0;
  36. if (selector < 0x41)
  37. return 600000 + selector * 12500;
  38. if (selector < 0x71)
  39. return 1400000 + (selector - 0x40) * 25000;
  40. return 2600000 + (selector - 0x70) * 50000;
  41. }
  42. static int as3711_list_voltage_aldo(struct regulator_dev *rdev,
  43. unsigned int selector)
  44. {
  45. if (selector >= rdev->desc->n_voltages)
  46. return -EINVAL;
  47. if (selector < 0x10)
  48. return 1200000 + selector * 50000;
  49. return 1800000 + (selector - 0x10) * 100000;
  50. }
  51. static int as3711_list_voltage_dldo(struct regulator_dev *rdev,
  52. unsigned int selector)
  53. {
  54. if (selector >= rdev->desc->n_voltages ||
  55. (selector > 0x10 && selector < 0x20))
  56. return -EINVAL;
  57. if (selector < 0x11)
  58. return 900000 + selector * 50000;
  59. return 1750000 + (selector - 0x20) * 50000;
  60. }
  61. static int as3711_bound_check(struct regulator_dev *rdev,
  62. int *min_uV, int *max_uV)
  63. {
  64. struct as3711_regulator *reg = rdev_get_drvdata(rdev);
  65. struct as3711_regulator_info *info = reg->reg_info;
  66. dev_dbg(&rdev->dev, "%s(), %d, %d, %d\n", __func__,
  67. *min_uV, rdev->desc->min_uV, info->max_uV);
  68. if (*max_uV < *min_uV ||
  69. *min_uV > info->max_uV || rdev->desc->min_uV > *max_uV)
  70. return -EINVAL;
  71. if (rdev->desc->n_voltages == 1)
  72. return 0;
  73. if (*max_uV > info->max_uV)
  74. *max_uV = info->max_uV;
  75. if (*min_uV < rdev->desc->min_uV)
  76. *min_uV = rdev->desc->min_uV;
  77. return *min_uV;
  78. }
  79. static int as3711_sel_check(int min, int max, int bottom, int step)
  80. {
  81. int sel, voltage;
  82. /* Round up min, when dividing: keeps us within the range */
  83. sel = DIV_ROUND_UP(min - bottom, step);
  84. voltage = sel * step + bottom;
  85. pr_debug("%s(): select %d..%d in %d+N*%d: %d\n", __func__,
  86. min, max, bottom, step, sel);
  87. if (voltage > max)
  88. return -EINVAL;
  89. return sel;
  90. }
  91. static int as3711_map_voltage_sd(struct regulator_dev *rdev,
  92. int min_uV, int max_uV)
  93. {
  94. int ret;
  95. ret = as3711_bound_check(rdev, &min_uV, &max_uV);
  96. if (ret <= 0)
  97. return ret;
  98. if (min_uV <= 1400000)
  99. return as3711_sel_check(min_uV, max_uV, 600000, 12500);
  100. if (min_uV <= 2600000)
  101. return as3711_sel_check(min_uV, max_uV, 1400000, 25000) + 0x40;
  102. return as3711_sel_check(min_uV, max_uV, 2600000, 50000) + 0x70;
  103. }
  104. /*
  105. * The regulator API supports 4 modes of operataion: FAST, NORMAL, IDLE and
  106. * STANDBY. We map them in the following way to AS3711 SD1-4 DCDC modes:
  107. * FAST: sdX_fast=1
  108. * NORMAL: low_noise=1
  109. * IDLE: low_noise=0
  110. */
  111. static int as3711_set_mode_sd(struct regulator_dev *rdev, unsigned int mode)
  112. {
  113. unsigned int fast_bit = rdev->desc->enable_mask,
  114. low_noise_bit = fast_bit << 4;
  115. u8 val;
  116. switch (mode) {
  117. case REGULATOR_MODE_FAST:
  118. val = fast_bit | low_noise_bit;
  119. break;
  120. case REGULATOR_MODE_NORMAL:
  121. val = low_noise_bit;
  122. break;
  123. case REGULATOR_MODE_IDLE:
  124. val = 0;
  125. break;
  126. default:
  127. return -EINVAL;
  128. }
  129. return regmap_update_bits(rdev->regmap, AS3711_SD_CONTROL_1,
  130. low_noise_bit | fast_bit, val);
  131. }
  132. static unsigned int as3711_get_mode_sd(struct regulator_dev *rdev)
  133. {
  134. unsigned int fast_bit = rdev->desc->enable_mask,
  135. low_noise_bit = fast_bit << 4, mask = fast_bit | low_noise_bit;
  136. unsigned int val;
  137. int ret = regmap_read(rdev->regmap, AS3711_SD_CONTROL_1, &val);
  138. if (ret < 0)
  139. return ret;
  140. if ((val & mask) == mask)
  141. return REGULATOR_MODE_FAST;
  142. if ((val & mask) == low_noise_bit)
  143. return REGULATOR_MODE_NORMAL;
  144. if (!(val & mask))
  145. return REGULATOR_MODE_IDLE;
  146. return -EINVAL;
  147. }
  148. static int as3711_map_voltage_aldo(struct regulator_dev *rdev,
  149. int min_uV, int max_uV)
  150. {
  151. int ret;
  152. ret = as3711_bound_check(rdev, &min_uV, &max_uV);
  153. if (ret <= 0)
  154. return ret;
  155. if (min_uV <= 1800000)
  156. return as3711_sel_check(min_uV, max_uV, 1200000, 50000);
  157. return as3711_sel_check(min_uV, max_uV, 1800000, 100000) + 0x10;
  158. }
  159. static int as3711_map_voltage_dldo(struct regulator_dev *rdev,
  160. int min_uV, int max_uV)
  161. {
  162. int ret;
  163. ret = as3711_bound_check(rdev, &min_uV, &max_uV);
  164. if (ret <= 0)
  165. return ret;
  166. if (min_uV <= 1700000)
  167. return as3711_sel_check(min_uV, max_uV, 900000, 50000);
  168. return as3711_sel_check(min_uV, max_uV, 1750000, 50000) + 0x20;
  169. }
  170. static struct regulator_ops as3711_sd_ops = {
  171. .is_enabled = regulator_is_enabled_regmap,
  172. .enable = regulator_enable_regmap,
  173. .disable = regulator_disable_regmap,
  174. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  175. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  176. .list_voltage = as3711_list_voltage_sd,
  177. .map_voltage = as3711_map_voltage_sd,
  178. .get_mode = as3711_get_mode_sd,
  179. .set_mode = as3711_set_mode_sd,
  180. };
  181. static struct regulator_ops as3711_aldo_ops = {
  182. .is_enabled = regulator_is_enabled_regmap,
  183. .enable = regulator_enable_regmap,
  184. .disable = regulator_disable_regmap,
  185. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  186. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  187. .list_voltage = as3711_list_voltage_aldo,
  188. .map_voltage = as3711_map_voltage_aldo,
  189. };
  190. static struct regulator_ops as3711_dldo_ops = {
  191. .is_enabled = regulator_is_enabled_regmap,
  192. .enable = regulator_enable_regmap,
  193. .disable = regulator_disable_regmap,
  194. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  195. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  196. .list_voltage = as3711_list_voltage_dldo,
  197. .map_voltage = as3711_map_voltage_dldo,
  198. };
  199. #define AS3711_REG(_id, _en_reg, _en_bit, _vmask, _vshift, _min_uV, _max_uV, _sfx) \
  200. [AS3711_REGULATOR_ ## _id] = { \
  201. .desc = { \
  202. .name = "as3711-regulator-" # _id, \
  203. .id = AS3711_REGULATOR_ ## _id, \
  204. .n_voltages = (_vmask + 1), \
  205. .ops = &as3711_ ## _sfx ## _ops, \
  206. .type = REGULATOR_VOLTAGE, \
  207. .owner = THIS_MODULE, \
  208. .vsel_reg = AS3711_ ## _id ## _VOLTAGE, \
  209. .vsel_mask = _vmask << _vshift, \
  210. .enable_reg = AS3711_ ## _en_reg, \
  211. .enable_mask = BIT(_en_bit), \
  212. .min_uV = _min_uV, \
  213. }, \
  214. .max_uV = _max_uV, \
  215. }
  216. static struct as3711_regulator_info as3711_reg_info[] = {
  217. AS3711_REG(SD_1, SD_CONTROL, 0, 0x7f, 0, 612500, 3350000, sd),
  218. AS3711_REG(SD_2, SD_CONTROL, 1, 0x7f, 0, 612500, 3350000, sd),
  219. AS3711_REG(SD_3, SD_CONTROL, 2, 0x7f, 0, 612500, 3350000, sd),
  220. AS3711_REG(SD_4, SD_CONTROL, 3, 0x7f, 0, 612500, 3350000, sd),
  221. AS3711_REG(LDO_1, LDO_1_VOLTAGE, 7, 0x1f, 0, 1200000, 3300000, aldo),
  222. AS3711_REG(LDO_2, LDO_2_VOLTAGE, 7, 0x1f, 0, 1200000, 3300000, aldo),
  223. AS3711_REG(LDO_3, LDO_3_VOLTAGE, 7, 0x3f, 0, 900000, 3300000, dldo),
  224. AS3711_REG(LDO_4, LDO_4_VOLTAGE, 7, 0x3f, 0, 900000, 3300000, dldo),
  225. AS3711_REG(LDO_5, LDO_5_VOLTAGE, 7, 0x3f, 0, 900000, 3300000, dldo),
  226. AS3711_REG(LDO_6, LDO_6_VOLTAGE, 7, 0x3f, 0, 900000, 3300000, dldo),
  227. AS3711_REG(LDO_7, LDO_7_VOLTAGE, 7, 0x3f, 0, 900000, 3300000, dldo),
  228. AS3711_REG(LDO_8, LDO_8_VOLTAGE, 7, 0x3f, 0, 900000, 3300000, dldo),
  229. /* StepUp output voltage depends on supplying regulator */
  230. };
  231. #define AS3711_REGULATOR_NUM ARRAY_SIZE(as3711_reg_info)
  232. static const char *as3711_regulator_of_names[AS3711_REGULATOR_NUM] = {
  233. [AS3711_REGULATOR_SD_1] = "sd1",
  234. [AS3711_REGULATOR_SD_2] = "sd2",
  235. [AS3711_REGULATOR_SD_3] = "sd3",
  236. [AS3711_REGULATOR_SD_4] = "sd4",
  237. [AS3711_REGULATOR_LDO_1] = "ldo1",
  238. [AS3711_REGULATOR_LDO_2] = "ldo2",
  239. [AS3711_REGULATOR_LDO_3] = "ldo3",
  240. [AS3711_REGULATOR_LDO_4] = "ldo4",
  241. [AS3711_REGULATOR_LDO_5] = "ldo5",
  242. [AS3711_REGULATOR_LDO_6] = "ldo6",
  243. [AS3711_REGULATOR_LDO_7] = "ldo7",
  244. [AS3711_REGULATOR_LDO_8] = "ldo8",
  245. };
  246. static int as3711_regulator_parse_dt(struct device *dev,
  247. struct device_node **of_node, const int count)
  248. {
  249. struct as3711_regulator_pdata *pdata = dev_get_platdata(dev);
  250. struct device_node *regulators =
  251. of_find_node_by_name(dev->parent->of_node, "regulators");
  252. struct of_regulator_match *matches, *match;
  253. int ret, i;
  254. if (!regulators) {
  255. dev_err(dev, "regulator node not found\n");
  256. return -ENODEV;
  257. }
  258. matches = devm_kzalloc(dev, sizeof(*matches) * count, GFP_KERNEL);
  259. if (!matches)
  260. return -ENOMEM;
  261. for (i = 0, match = matches; i < count; i++, match++) {
  262. match->name = as3711_regulator_of_names[i];
  263. match->driver_data = as3711_reg_info + i;
  264. }
  265. ret = of_regulator_match(dev->parent, regulators, matches, count);
  266. of_node_put(regulators);
  267. if (ret < 0) {
  268. dev_err(dev, "Error parsing regulator init data: %d\n", ret);
  269. return ret;
  270. }
  271. for (i = 0, match = matches; i < count; i++, match++)
  272. if (match->of_node) {
  273. pdata->init_data[i] = match->init_data;
  274. of_node[i] = match->of_node;
  275. }
  276. return 0;
  277. }
  278. static int as3711_regulator_probe(struct platform_device *pdev)
  279. {
  280. struct as3711_regulator_pdata *pdata = dev_get_platdata(&pdev->dev);
  281. struct as3711 *as3711 = dev_get_drvdata(pdev->dev.parent);
  282. struct regulator_init_data *reg_data;
  283. struct regulator_config config = {.dev = &pdev->dev,};
  284. struct as3711_regulator *reg = NULL;
  285. struct as3711_regulator *regs;
  286. struct device_node *of_node[AS3711_REGULATOR_NUM] = {};
  287. struct regulator_dev *rdev;
  288. struct as3711_regulator_info *ri;
  289. int ret;
  290. int id;
  291. if (!pdata) {
  292. dev_err(&pdev->dev, "No platform data...\n");
  293. return -ENODEV;
  294. }
  295. if (pdev->dev.parent->of_node) {
  296. ret = as3711_regulator_parse_dt(&pdev->dev, of_node, AS3711_REGULATOR_NUM);
  297. if (ret < 0) {
  298. dev_err(&pdev->dev, "DT parsing failed: %d\n", ret);
  299. return ret;
  300. }
  301. }
  302. regs = devm_kzalloc(&pdev->dev, AS3711_REGULATOR_NUM *
  303. sizeof(struct as3711_regulator), GFP_KERNEL);
  304. if (!regs) {
  305. dev_err(&pdev->dev, "Memory allocation failed exiting..\n");
  306. return -ENOMEM;
  307. }
  308. for (id = 0, ri = as3711_reg_info; id < AS3711_REGULATOR_NUM; ++id, ri++) {
  309. reg_data = pdata->init_data[id];
  310. /* No need to register if there is no regulator data */
  311. if (!reg_data)
  312. continue;
  313. reg = &regs[id];
  314. reg->reg_info = ri;
  315. config.init_data = reg_data;
  316. config.driver_data = reg;
  317. config.regmap = as3711->regmap;
  318. config.of_node = of_node[id];
  319. rdev = regulator_register(&ri->desc, &config);
  320. if (IS_ERR(rdev)) {
  321. dev_err(&pdev->dev, "Failed to register regulator %s\n",
  322. ri->desc.name);
  323. ret = PTR_ERR(rdev);
  324. goto eregreg;
  325. }
  326. reg->rdev = rdev;
  327. }
  328. platform_set_drvdata(pdev, regs);
  329. return 0;
  330. eregreg:
  331. while (--id >= 0)
  332. regulator_unregister(regs[id].rdev);
  333. return ret;
  334. }
  335. static int as3711_regulator_remove(struct platform_device *pdev)
  336. {
  337. struct as3711_regulator *regs = platform_get_drvdata(pdev);
  338. int id;
  339. for (id = 0; id < AS3711_REGULATOR_NUM; ++id)
  340. regulator_unregister(regs[id].rdev);
  341. return 0;
  342. }
  343. static struct platform_driver as3711_regulator_driver = {
  344. .driver = {
  345. .name = "as3711-regulator",
  346. .owner = THIS_MODULE,
  347. },
  348. .probe = as3711_regulator_probe,
  349. .remove = as3711_regulator_remove,
  350. };
  351. static int __init as3711_regulator_init(void)
  352. {
  353. return platform_driver_register(&as3711_regulator_driver);
  354. }
  355. subsys_initcall(as3711_regulator_init);
  356. static void __exit as3711_regulator_exit(void)
  357. {
  358. platform_driver_unregister(&as3711_regulator_driver);
  359. }
  360. module_exit(as3711_regulator_exit);
  361. MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");
  362. MODULE_DESCRIPTION("AS3711 regulator driver");
  363. MODULE_ALIAS("platform:as3711-regulator");
  364. MODULE_LICENSE("GPL v2");