as3711-regulator.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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/platform_device.h>
  16. #include <linux/regmap.h>
  17. #include <linux/regulator/driver.h>
  18. #include <linux/slab.h>
  19. struct as3711_regulator_info {
  20. struct regulator_desc desc;
  21. unsigned int max_uV;
  22. };
  23. struct as3711_regulator {
  24. struct as3711_regulator_info *reg_info;
  25. struct regulator_dev *rdev;
  26. };
  27. static int as3711_list_voltage_sd(struct regulator_dev *rdev,
  28. unsigned int selector)
  29. {
  30. if (selector >= rdev->desc->n_voltages)
  31. return -EINVAL;
  32. if (!selector)
  33. return 0;
  34. if (selector < 0x41)
  35. return 600000 + selector * 12500;
  36. if (selector < 0x71)
  37. return 1400000 + (selector - 0x40) * 25000;
  38. return 2600000 + (selector - 0x70) * 50000;
  39. }
  40. static int as3711_list_voltage_aldo(struct regulator_dev *rdev,
  41. unsigned int selector)
  42. {
  43. if (selector >= rdev->desc->n_voltages)
  44. return -EINVAL;
  45. if (selector < 0x10)
  46. return 1200000 + selector * 50000;
  47. return 1800000 + (selector - 0x10) * 100000;
  48. }
  49. static int as3711_list_voltage_dldo(struct regulator_dev *rdev,
  50. unsigned int selector)
  51. {
  52. if (selector >= rdev->desc->n_voltages ||
  53. (selector > 0x10 && selector < 0x20))
  54. return -EINVAL;
  55. if (selector < 0x11)
  56. return 900000 + selector * 50000;
  57. return 1750000 + (selector - 0x20) * 50000;
  58. }
  59. static int as3711_bound_check(struct regulator_dev *rdev,
  60. int *min_uV, int *max_uV)
  61. {
  62. struct as3711_regulator *reg = rdev_get_drvdata(rdev);
  63. struct as3711_regulator_info *info = reg->reg_info;
  64. dev_dbg(&rdev->dev, "%s(), %d, %d, %d\n", __func__,
  65. *min_uV, rdev->desc->min_uV, info->max_uV);
  66. if (*max_uV < *min_uV ||
  67. *min_uV > info->max_uV || rdev->desc->min_uV > *max_uV)
  68. return -EINVAL;
  69. if (rdev->desc->n_voltages == 1)
  70. return 0;
  71. if (*max_uV > info->max_uV)
  72. *max_uV = info->max_uV;
  73. if (*min_uV < rdev->desc->min_uV)
  74. *min_uV = rdev->desc->min_uV;
  75. return *min_uV;
  76. }
  77. static int as3711_sel_check(int min, int max, int bottom, int step)
  78. {
  79. int sel, voltage;
  80. /* Round up min, when dividing: keeps us within the range */
  81. sel = DIV_ROUND_UP(min - bottom, step);
  82. voltage = sel * step + bottom;
  83. pr_debug("%s(): select %d..%d in %d+N*%d: %d\n", __func__,
  84. min, max, bottom, step, sel);
  85. if (voltage > max)
  86. return -EINVAL;
  87. return sel;
  88. }
  89. static int as3711_map_voltage_sd(struct regulator_dev *rdev,
  90. int min_uV, int max_uV)
  91. {
  92. int ret;
  93. ret = as3711_bound_check(rdev, &min_uV, &max_uV);
  94. if (ret <= 0)
  95. return ret;
  96. if (min_uV <= 1400000)
  97. return as3711_sel_check(min_uV, max_uV, 600000, 12500);
  98. if (min_uV <= 2600000)
  99. return as3711_sel_check(min_uV, max_uV, 1400000, 25000) + 0x40;
  100. return as3711_sel_check(min_uV, max_uV, 2600000, 50000) + 0x70;
  101. }
  102. /*
  103. * The regulator API supports 4 modes of operataion: FAST, NORMAL, IDLE and
  104. * STANDBY. We map them in the following way to AS3711 SD1-4 DCDC modes:
  105. * FAST: sdX_fast=1
  106. * NORMAL: low_noise=1
  107. * IDLE: low_noise=0
  108. */
  109. static int as3711_set_mode_sd(struct regulator_dev *rdev, unsigned int mode)
  110. {
  111. unsigned int fast_bit = rdev->desc->enable_mask,
  112. low_noise_bit = fast_bit << 4;
  113. u8 val;
  114. switch (mode) {
  115. case REGULATOR_MODE_FAST:
  116. val = fast_bit | low_noise_bit;
  117. break;
  118. case REGULATOR_MODE_NORMAL:
  119. val = low_noise_bit;
  120. break;
  121. case REGULATOR_MODE_IDLE:
  122. val = 0;
  123. break;
  124. default:
  125. return -EINVAL;
  126. }
  127. return regmap_update_bits(rdev->regmap, AS3711_SD_CONTROL_1,
  128. low_noise_bit | fast_bit, val);
  129. }
  130. static unsigned int as3711_get_mode_sd(struct regulator_dev *rdev)
  131. {
  132. unsigned int fast_bit = rdev->desc->enable_mask,
  133. low_noise_bit = fast_bit << 4, mask = fast_bit | low_noise_bit;
  134. unsigned int val;
  135. int ret = regmap_read(rdev->regmap, AS3711_SD_CONTROL_1, &val);
  136. if (ret < 0)
  137. return ret;
  138. if ((val & mask) == mask)
  139. return REGULATOR_MODE_FAST;
  140. if ((val & mask) == low_noise_bit)
  141. return REGULATOR_MODE_NORMAL;
  142. if (!(val & mask))
  143. return REGULATOR_MODE_IDLE;
  144. return -EINVAL;
  145. }
  146. static int as3711_map_voltage_aldo(struct regulator_dev *rdev,
  147. int min_uV, int max_uV)
  148. {
  149. int ret;
  150. ret = as3711_bound_check(rdev, &min_uV, &max_uV);
  151. if (ret <= 0)
  152. return ret;
  153. if (min_uV <= 1800000)
  154. return as3711_sel_check(min_uV, max_uV, 1200000, 50000);
  155. return as3711_sel_check(min_uV, max_uV, 1800000, 100000) + 0x10;
  156. }
  157. static int as3711_map_voltage_dldo(struct regulator_dev *rdev,
  158. int min_uV, int max_uV)
  159. {
  160. int ret;
  161. ret = as3711_bound_check(rdev, &min_uV, &max_uV);
  162. if (ret <= 0)
  163. return ret;
  164. if (min_uV <= 1700000)
  165. return as3711_sel_check(min_uV, max_uV, 900000, 50000);
  166. return as3711_sel_check(min_uV, max_uV, 1750000, 50000) + 0x20;
  167. }
  168. static struct regulator_ops as3711_sd_ops = {
  169. .is_enabled = regulator_is_enabled_regmap,
  170. .enable = regulator_enable_regmap,
  171. .disable = regulator_disable_regmap,
  172. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  173. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  174. .list_voltage = as3711_list_voltage_sd,
  175. .map_voltage = as3711_map_voltage_sd,
  176. .get_mode = as3711_get_mode_sd,
  177. .set_mode = as3711_set_mode_sd,
  178. };
  179. static struct regulator_ops as3711_aldo_ops = {
  180. .is_enabled = regulator_is_enabled_regmap,
  181. .enable = regulator_enable_regmap,
  182. .disable = regulator_disable_regmap,
  183. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  184. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  185. .list_voltage = as3711_list_voltage_aldo,
  186. .map_voltage = as3711_map_voltage_aldo,
  187. };
  188. static struct regulator_ops as3711_dldo_ops = {
  189. .is_enabled = regulator_is_enabled_regmap,
  190. .enable = regulator_enable_regmap,
  191. .disable = regulator_disable_regmap,
  192. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  193. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  194. .list_voltage = as3711_list_voltage_dldo,
  195. .map_voltage = as3711_map_voltage_dldo,
  196. };
  197. #define AS3711_REG(_id, _en_reg, _en_bit, _vmask, _vshift, _min_uV, _max_uV, _sfx) \
  198. [AS3711_REGULATOR_ ## _id] = { \
  199. .desc = { \
  200. .name = "as3711-regulator-" # _id, \
  201. .id = AS3711_REGULATOR_ ## _id, \
  202. .n_voltages = (_vmask + 1), \
  203. .ops = &as3711_ ## _sfx ## _ops, \
  204. .type = REGULATOR_VOLTAGE, \
  205. .owner = THIS_MODULE, \
  206. .vsel_reg = AS3711_ ## _id ## _VOLTAGE, \
  207. .vsel_mask = _vmask << _vshift, \
  208. .enable_reg = AS3711_ ## _en_reg, \
  209. .enable_mask = BIT(_en_bit), \
  210. .min_uV = _min_uV, \
  211. }, \
  212. .max_uV = _max_uV, \
  213. }
  214. static struct as3711_regulator_info as3711_reg_info[] = {
  215. AS3711_REG(SD_1, SD_CONTROL, 0, 0x7f, 0, 612500, 3350000, sd),
  216. AS3711_REG(SD_2, SD_CONTROL, 1, 0x7f, 0, 612500, 3350000, sd),
  217. AS3711_REG(SD_3, SD_CONTROL, 2, 0x7f, 0, 612500, 3350000, sd),
  218. AS3711_REG(SD_4, SD_CONTROL, 3, 0x7f, 0, 612500, 3350000, sd),
  219. AS3711_REG(LDO_1, LDO_1_VOLTAGE, 7, 0x1f, 0, 1200000, 3300000, aldo),
  220. AS3711_REG(LDO_2, LDO_2_VOLTAGE, 7, 0x1f, 0, 1200000, 3300000, aldo),
  221. AS3711_REG(LDO_3, LDO_3_VOLTAGE, 7, 0x3f, 0, 900000, 3300000, dldo),
  222. AS3711_REG(LDO_4, LDO_4_VOLTAGE, 7, 0x3f, 0, 900000, 3300000, dldo),
  223. AS3711_REG(LDO_5, LDO_5_VOLTAGE, 7, 0x3f, 0, 900000, 3300000, dldo),
  224. AS3711_REG(LDO_6, LDO_6_VOLTAGE, 7, 0x3f, 0, 900000, 3300000, dldo),
  225. AS3711_REG(LDO_7, LDO_7_VOLTAGE, 7, 0x3f, 0, 900000, 3300000, dldo),
  226. AS3711_REG(LDO_8, LDO_8_VOLTAGE, 7, 0x3f, 0, 900000, 3300000, dldo),
  227. /* StepUp output voltage depends on supplying regulator */
  228. };
  229. #define AS3711_REGULATOR_NUM ARRAY_SIZE(as3711_reg_info)
  230. static int as3711_regulator_probe(struct platform_device *pdev)
  231. {
  232. struct as3711_regulator_pdata *pdata = dev_get_platdata(&pdev->dev);
  233. struct as3711 *as3711 = dev_get_drvdata(pdev->dev.parent);
  234. struct regulator_init_data *reg_data;
  235. struct regulator_config config = {.dev = &pdev->dev,};
  236. struct as3711_regulator *reg = NULL;
  237. struct as3711_regulator *regs;
  238. struct regulator_dev *rdev;
  239. struct as3711_regulator_info *ri;
  240. int ret;
  241. int id;
  242. if (!pdata)
  243. dev_dbg(&pdev->dev, "No platform data...\n");
  244. regs = devm_kzalloc(&pdev->dev, AS3711_REGULATOR_NUM *
  245. sizeof(struct as3711_regulator), GFP_KERNEL);
  246. if (!regs) {
  247. dev_err(&pdev->dev, "Memory allocation failed exiting..\n");
  248. return -ENOMEM;
  249. }
  250. for (id = 0, ri = as3711_reg_info; id < AS3711_REGULATOR_NUM; ++id, ri++) {
  251. reg_data = pdata ? pdata->init_data[id] : NULL;
  252. /* No need to register if there is no regulator data */
  253. if (!reg_data)
  254. continue;
  255. reg = &regs[id];
  256. reg->reg_info = ri;
  257. config.init_data = reg_data;
  258. config.driver_data = reg;
  259. config.regmap = as3711->regmap;
  260. rdev = regulator_register(&ri->desc, &config);
  261. if (IS_ERR(rdev)) {
  262. dev_err(&pdev->dev, "Failed to register regulator %s\n",
  263. ri->desc.name);
  264. ret = PTR_ERR(rdev);
  265. goto eregreg;
  266. }
  267. reg->rdev = rdev;
  268. }
  269. platform_set_drvdata(pdev, regs);
  270. return 0;
  271. eregreg:
  272. while (--id >= 0)
  273. regulator_unregister(regs[id].rdev);
  274. return ret;
  275. }
  276. static int as3711_regulator_remove(struct platform_device *pdev)
  277. {
  278. struct as3711_regulator *regs = platform_get_drvdata(pdev);
  279. int id;
  280. for (id = 0; id < AS3711_REGULATOR_NUM; ++id)
  281. regulator_unregister(regs[id].rdev);
  282. return 0;
  283. }
  284. static struct platform_driver as3711_regulator_driver = {
  285. .driver = {
  286. .name = "as3711-regulator",
  287. .owner = THIS_MODULE,
  288. },
  289. .probe = as3711_regulator_probe,
  290. .remove = as3711_regulator_remove,
  291. };
  292. static int __init as3711_regulator_init(void)
  293. {
  294. return platform_driver_register(&as3711_regulator_driver);
  295. }
  296. subsys_initcall(as3711_regulator_init);
  297. static void __exit as3711_regulator_exit(void)
  298. {
  299. platform_driver_unregister(&as3711_regulator_driver);
  300. }
  301. module_exit(as3711_regulator_exit);
  302. MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");
  303. MODULE_DESCRIPTION("AS3711 regulator driver");
  304. MODULE_ALIAS("platform:as3711-regulator");
  305. MODULE_LICENSE("GPL v2");