da9052-regulator.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. // SPDX-License-Identifier: GPL-2.0+
  2. //
  3. // da9052-regulator.c: Regulator driver for DA9052
  4. //
  5. // Copyright(c) 2011 Dialog Semiconductor Ltd.
  6. //
  7. // Author: David Dajun Chen <dchen@diasemi.com>
  8. #include <linux/module.h>
  9. #include <linux/moduleparam.h>
  10. #include <linux/init.h>
  11. #include <linux/err.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/regulator/driver.h>
  14. #include <linux/regulator/machine.h>
  15. #include <linux/of.h>
  16. #include <linux/regulator/of_regulator.h>
  17. #include <linux/mfd/da9052/da9052.h>
  18. #include <linux/mfd/da9052/reg.h>
  19. #include <linux/mfd/da9052/pdata.h>
  20. /* Buck step size */
  21. #define DA9052_BUCK_PERI_3uV_STEP 100000
  22. #define DA9052_BUCK_PERI_REG_MAP_UPTO_3uV 24
  23. #define DA9052_CONST_3uV 3000000
  24. #define DA9052_MIN_UA 0
  25. #define DA9052_MAX_UA 3
  26. #define DA9052_CURRENT_RANGE 4
  27. /* Bit masks */
  28. #define DA9052_BUCK_ILIM_MASK_EVEN 0x0c
  29. #define DA9052_BUCK_ILIM_MASK_ODD 0xc0
  30. /* DA9052 REGULATOR IDs */
  31. #define DA9052_ID_BUCK1 0
  32. #define DA9052_ID_BUCK2 1
  33. #define DA9052_ID_BUCK3 2
  34. #define DA9052_ID_BUCK4 3
  35. #define DA9052_ID_LDO1 4
  36. #define DA9052_ID_LDO2 5
  37. #define DA9052_ID_LDO3 6
  38. #define DA9052_ID_LDO4 7
  39. #define DA9052_ID_LDO5 8
  40. #define DA9052_ID_LDO6 9
  41. #define DA9052_ID_LDO7 10
  42. #define DA9052_ID_LDO8 11
  43. #define DA9052_ID_LDO9 12
  44. #define DA9052_ID_LDO10 13
  45. static const u32 da9052_current_limits[3][4] = {
  46. {700000, 800000, 1000000, 1200000}, /* DA9052-BC BUCKs */
  47. {1600000, 2000000, 2400000, 3000000}, /* DA9053-AA/Bx BUCK-CORE */
  48. {800000, 1000000, 1200000, 1500000}, /* DA9053-AA/Bx BUCK-PRO,
  49. * BUCK-MEM and BUCK-PERI
  50. */
  51. };
  52. struct da9052_regulator_info {
  53. struct regulator_desc reg_desc;
  54. int step_uV;
  55. int min_uV;
  56. int max_uV;
  57. unsigned char activate_bit;
  58. };
  59. struct da9052_regulator {
  60. struct da9052 *da9052;
  61. struct da9052_regulator_info *info;
  62. struct regulator_dev *rdev;
  63. };
  64. static int verify_range(struct da9052_regulator_info *info,
  65. int min_uV, int max_uV)
  66. {
  67. if (min_uV > info->max_uV || max_uV < info->min_uV)
  68. return -EINVAL;
  69. return 0;
  70. }
  71. static int da9052_dcdc_get_current_limit(struct regulator_dev *rdev)
  72. {
  73. struct da9052_regulator *regulator = rdev_get_drvdata(rdev);
  74. int offset = rdev_get_id(rdev);
  75. int ret, row = 2;
  76. ret = da9052_reg_read(regulator->da9052, DA9052_BUCKA_REG + offset/2);
  77. if (ret < 0)
  78. return ret;
  79. /* Determine the even or odd position of the buck current limit
  80. * register field
  81. */
  82. if (offset % 2 == 0)
  83. ret = (ret & DA9052_BUCK_ILIM_MASK_EVEN) >> 2;
  84. else
  85. ret = (ret & DA9052_BUCK_ILIM_MASK_ODD) >> 6;
  86. /* Select the appropriate current limit range */
  87. if (regulator->da9052->chip_id == DA9052)
  88. row = 0;
  89. else if (offset == 0)
  90. row = 1;
  91. return da9052_current_limits[row][ret];
  92. }
  93. static int da9052_dcdc_set_current_limit(struct regulator_dev *rdev, int min_uA,
  94. int max_uA)
  95. {
  96. struct da9052_regulator *regulator = rdev_get_drvdata(rdev);
  97. int offset = rdev_get_id(rdev);
  98. int reg_val = 0;
  99. int i, row = 2;
  100. /* Select the appropriate current limit range */
  101. if (regulator->da9052->chip_id == DA9052)
  102. row = 0;
  103. else if (offset == 0)
  104. row = 1;
  105. for (i = DA9052_CURRENT_RANGE - 1; i >= 0; i--) {
  106. if ((min_uA <= da9052_current_limits[row][i]) &&
  107. (da9052_current_limits[row][i] <= max_uA)) {
  108. reg_val = i;
  109. break;
  110. }
  111. }
  112. if (i < 0)
  113. return -EINVAL;
  114. /* Determine the even or odd position of the buck current limit
  115. * register field
  116. */
  117. if (offset % 2 == 0)
  118. return da9052_reg_update(regulator->da9052,
  119. DA9052_BUCKA_REG + offset/2,
  120. DA9052_BUCK_ILIM_MASK_EVEN,
  121. reg_val << 2);
  122. else
  123. return da9052_reg_update(regulator->da9052,
  124. DA9052_BUCKA_REG + offset/2,
  125. DA9052_BUCK_ILIM_MASK_ODD,
  126. reg_val << 6);
  127. }
  128. static int da9052_list_voltage(struct regulator_dev *rdev,
  129. unsigned int selector)
  130. {
  131. struct da9052_regulator *regulator = rdev_get_drvdata(rdev);
  132. struct da9052_regulator_info *info = regulator->info;
  133. int id = rdev_get_id(rdev);
  134. int volt_uV;
  135. if ((id == DA9052_ID_BUCK4) && (regulator->da9052->chip_id == DA9052)
  136. && (selector >= DA9052_BUCK_PERI_REG_MAP_UPTO_3uV)) {
  137. volt_uV = ((DA9052_BUCK_PERI_REG_MAP_UPTO_3uV * info->step_uV)
  138. + info->min_uV);
  139. volt_uV += (selector - DA9052_BUCK_PERI_REG_MAP_UPTO_3uV)
  140. * (DA9052_BUCK_PERI_3uV_STEP);
  141. } else {
  142. volt_uV = (selector * info->step_uV) + info->min_uV;
  143. }
  144. if (volt_uV > info->max_uV)
  145. return -EINVAL;
  146. return volt_uV;
  147. }
  148. static int da9052_map_voltage(struct regulator_dev *rdev,
  149. int min_uV, int max_uV)
  150. {
  151. struct da9052_regulator *regulator = rdev_get_drvdata(rdev);
  152. struct da9052_regulator_info *info = regulator->info;
  153. int id = rdev_get_id(rdev);
  154. int ret, sel;
  155. ret = verify_range(info, min_uV, max_uV);
  156. if (ret < 0)
  157. return ret;
  158. if (min_uV < info->min_uV)
  159. min_uV = info->min_uV;
  160. if ((id == DA9052_ID_BUCK4) && (regulator->da9052->chip_id == DA9052)
  161. && (min_uV >= DA9052_CONST_3uV)) {
  162. sel = DA9052_BUCK_PERI_REG_MAP_UPTO_3uV +
  163. DIV_ROUND_UP(min_uV - DA9052_CONST_3uV,
  164. DA9052_BUCK_PERI_3uV_STEP);
  165. } else {
  166. sel = DIV_ROUND_UP(min_uV - info->min_uV, info->step_uV);
  167. }
  168. ret = da9052_list_voltage(rdev, sel);
  169. if (ret < 0)
  170. return ret;
  171. return sel;
  172. }
  173. static int da9052_regulator_set_voltage_sel(struct regulator_dev *rdev,
  174. unsigned int selector)
  175. {
  176. struct da9052_regulator *regulator = rdev_get_drvdata(rdev);
  177. struct da9052_regulator_info *info = regulator->info;
  178. int id = rdev_get_id(rdev);
  179. int ret;
  180. ret = da9052_reg_update(regulator->da9052, rdev->desc->vsel_reg,
  181. rdev->desc->vsel_mask, selector);
  182. if (ret < 0)
  183. return ret;
  184. /* Some LDOs and DCDCs are DVC controlled which requires enabling of
  185. * the activate bit to implment the changes on the output.
  186. */
  187. switch (id) {
  188. case DA9052_ID_BUCK1:
  189. case DA9052_ID_BUCK2:
  190. case DA9052_ID_BUCK3:
  191. case DA9052_ID_LDO2:
  192. case DA9052_ID_LDO3:
  193. ret = da9052_reg_update(regulator->da9052, DA9052_SUPPLY_REG,
  194. info->activate_bit, info->activate_bit);
  195. break;
  196. }
  197. return ret;
  198. }
  199. static int da9052_regulator_set_voltage_time_sel(struct regulator_dev *rdev,
  200. unsigned int old_sel,
  201. unsigned int new_sel)
  202. {
  203. struct da9052_regulator *regulator = rdev_get_drvdata(rdev);
  204. struct da9052_regulator_info *info = regulator->info;
  205. int id = rdev_get_id(rdev);
  206. int ret = 0;
  207. /* The DVC controlled LDOs and DCDCs ramp with 6.25mV/µs after enabling
  208. * the activate bit.
  209. */
  210. switch (id) {
  211. case DA9052_ID_BUCK1:
  212. case DA9052_ID_BUCK2:
  213. case DA9052_ID_BUCK3:
  214. case DA9052_ID_LDO2:
  215. case DA9052_ID_LDO3:
  216. ret = DIV_ROUND_UP(abs(new_sel - old_sel) * info->step_uV,
  217. 6250);
  218. break;
  219. }
  220. return ret;
  221. }
  222. static const struct regulator_ops da9052_dcdc_ops = {
  223. .get_current_limit = da9052_dcdc_get_current_limit,
  224. .set_current_limit = da9052_dcdc_set_current_limit,
  225. .list_voltage = da9052_list_voltage,
  226. .map_voltage = da9052_map_voltage,
  227. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  228. .set_voltage_sel = da9052_regulator_set_voltage_sel,
  229. .set_voltage_time_sel = da9052_regulator_set_voltage_time_sel,
  230. .is_enabled = regulator_is_enabled_regmap,
  231. .enable = regulator_enable_regmap,
  232. .disable = regulator_disable_regmap,
  233. };
  234. static const struct regulator_ops da9052_ldo_ops = {
  235. .list_voltage = da9052_list_voltage,
  236. .map_voltage = da9052_map_voltage,
  237. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  238. .set_voltage_sel = da9052_regulator_set_voltage_sel,
  239. .set_voltage_time_sel = da9052_regulator_set_voltage_time_sel,
  240. .is_enabled = regulator_is_enabled_regmap,
  241. .enable = regulator_enable_regmap,
  242. .disable = regulator_disable_regmap,
  243. };
  244. #define DA9052_LDO(_id, _name, step, min, max, sbits, ebits, abits) \
  245. {\
  246. .reg_desc = {\
  247. .name = #_name,\
  248. .of_match = of_match_ptr(#_name),\
  249. .regulators_node = of_match_ptr("regulators"),\
  250. .ops = &da9052_ldo_ops,\
  251. .type = REGULATOR_VOLTAGE,\
  252. .id = DA9052_ID_##_id,\
  253. .n_voltages = (max - min) / step + 1, \
  254. .owner = THIS_MODULE,\
  255. .vsel_reg = DA9052_BUCKCORE_REG + DA9052_ID_##_id, \
  256. .vsel_mask = (1 << (sbits)) - 1,\
  257. .enable_reg = DA9052_BUCKCORE_REG + DA9052_ID_##_id, \
  258. .enable_mask = 1 << (ebits),\
  259. },\
  260. .min_uV = (min) * 1000,\
  261. .max_uV = (max) * 1000,\
  262. .step_uV = (step) * 1000,\
  263. .activate_bit = (abits),\
  264. }
  265. #define DA9052_DCDC(_id, _name, step, min, max, sbits, ebits, abits) \
  266. {\
  267. .reg_desc = {\
  268. .name = #_name,\
  269. .of_match = of_match_ptr(#_name),\
  270. .regulators_node = of_match_ptr("regulators"),\
  271. .ops = &da9052_dcdc_ops,\
  272. .type = REGULATOR_VOLTAGE,\
  273. .id = DA9052_ID_##_id,\
  274. .n_voltages = (max - min) / step + 1, \
  275. .owner = THIS_MODULE,\
  276. .vsel_reg = DA9052_BUCKCORE_REG + DA9052_ID_##_id, \
  277. .vsel_mask = (1 << (sbits)) - 1,\
  278. .enable_reg = DA9052_BUCKCORE_REG + DA9052_ID_##_id, \
  279. .enable_mask = 1 << (ebits),\
  280. },\
  281. .min_uV = (min) * 1000,\
  282. .max_uV = (max) * 1000,\
  283. .step_uV = (step) * 1000,\
  284. .activate_bit = (abits),\
  285. }
  286. static struct da9052_regulator_info da9052_regulator_info[] = {
  287. DA9052_DCDC(BUCK1, buck1, 25, 500, 2075, 6, 6, DA9052_SUPPLY_VBCOREGO),
  288. DA9052_DCDC(BUCK2, buck2, 25, 500, 2075, 6, 6, DA9052_SUPPLY_VBPROGO),
  289. DA9052_DCDC(BUCK3, buck3, 25, 950, 2525, 6, 6, DA9052_SUPPLY_VBMEMGO),
  290. DA9052_DCDC(BUCK4, buck4, 50, 1800, 3600, 5, 6, 0),
  291. DA9052_LDO(LDO1, ldo1, 50, 600, 1800, 5, 6, 0),
  292. DA9052_LDO(LDO2, ldo2, 25, 600, 1800, 6, 6, DA9052_SUPPLY_VLDO2GO),
  293. DA9052_LDO(LDO3, ldo3, 25, 1725, 3300, 6, 6, DA9052_SUPPLY_VLDO3GO),
  294. DA9052_LDO(LDO4, ldo4, 25, 1725, 3300, 6, 6, 0),
  295. DA9052_LDO(LDO5, ldo5, 50, 1200, 3600, 6, 6, 0),
  296. DA9052_LDO(LDO6, ldo6, 50, 1200, 3600, 6, 6, 0),
  297. DA9052_LDO(LDO7, ldo7, 50, 1200, 3600, 6, 6, 0),
  298. DA9052_LDO(LDO8, ldo8, 50, 1200, 3600, 6, 6, 0),
  299. DA9052_LDO(LDO9, ldo9, 50, 1250, 3650, 6, 6, 0),
  300. DA9052_LDO(LDO10, ldo10, 50, 1200, 3600, 6, 6, 0),
  301. };
  302. static struct da9052_regulator_info da9053_regulator_info[] = {
  303. DA9052_DCDC(BUCK1, buck1, 25, 500, 2075, 6, 6, DA9052_SUPPLY_VBCOREGO),
  304. DA9052_DCDC(BUCK2, buck2, 25, 500, 2075, 6, 6, DA9052_SUPPLY_VBPROGO),
  305. DA9052_DCDC(BUCK3, buck3, 25, 950, 2525, 6, 6, DA9052_SUPPLY_VBMEMGO),
  306. DA9052_DCDC(BUCK4, buck4, 25, 950, 2525, 6, 6, 0),
  307. DA9052_LDO(LDO1, ldo1, 50, 600, 1800, 5, 6, 0),
  308. DA9052_LDO(LDO2, ldo2, 25, 600, 1800, 6, 6, DA9052_SUPPLY_VLDO2GO),
  309. DA9052_LDO(LDO3, ldo3, 25, 1725, 3300, 6, 6, DA9052_SUPPLY_VLDO3GO),
  310. DA9052_LDO(LDO4, ldo4, 25, 1725, 3300, 6, 6, 0),
  311. DA9052_LDO(LDO5, ldo5, 50, 1200, 3600, 6, 6, 0),
  312. DA9052_LDO(LDO6, ldo6, 50, 1200, 3600, 6, 6, 0),
  313. DA9052_LDO(LDO7, ldo7, 50, 1200, 3600, 6, 6, 0),
  314. DA9052_LDO(LDO8, ldo8, 50, 1200, 3600, 6, 6, 0),
  315. DA9052_LDO(LDO9, ldo9, 50, 1250, 3650, 6, 6, 0),
  316. DA9052_LDO(LDO10, ldo10, 50, 1200, 3600, 6, 6, 0),
  317. };
  318. static inline struct da9052_regulator_info *find_regulator_info(u8 chip_id,
  319. int id)
  320. {
  321. struct da9052_regulator_info *info;
  322. int i;
  323. switch (chip_id) {
  324. case DA9052:
  325. for (i = 0; i < ARRAY_SIZE(da9052_regulator_info); i++) {
  326. info = &da9052_regulator_info[i];
  327. if (info->reg_desc.id == id)
  328. return info;
  329. }
  330. break;
  331. case DA9053_AA:
  332. case DA9053_BA:
  333. case DA9053_BB:
  334. case DA9053_BC:
  335. for (i = 0; i < ARRAY_SIZE(da9053_regulator_info); i++) {
  336. info = &da9053_regulator_info[i];
  337. if (info->reg_desc.id == id)
  338. return info;
  339. }
  340. break;
  341. }
  342. return NULL;
  343. }
  344. static int da9052_regulator_probe(struct platform_device *pdev)
  345. {
  346. const struct mfd_cell *cell = mfd_get_cell(pdev);
  347. struct regulator_config config = { };
  348. struct da9052_regulator *regulator;
  349. struct da9052 *da9052;
  350. struct da9052_pdata *pdata;
  351. regulator = devm_kzalloc(&pdev->dev, sizeof(struct da9052_regulator),
  352. GFP_KERNEL);
  353. if (!regulator)
  354. return -ENOMEM;
  355. da9052 = dev_get_drvdata(pdev->dev.parent);
  356. pdata = dev_get_platdata(da9052->dev);
  357. regulator->da9052 = da9052;
  358. regulator->info = find_regulator_info(regulator->da9052->chip_id,
  359. cell->id);
  360. if (regulator->info == NULL) {
  361. dev_err(&pdev->dev, "invalid regulator ID specified\n");
  362. return -EINVAL;
  363. }
  364. config.dev = da9052->dev;
  365. config.driver_data = regulator;
  366. config.regmap = da9052->regmap;
  367. if (pdata)
  368. config.init_data = pdata->regulators[cell->id];
  369. regulator->rdev = devm_regulator_register(&pdev->dev,
  370. &regulator->info->reg_desc,
  371. &config);
  372. if (IS_ERR(regulator->rdev)) {
  373. dev_err(&pdev->dev, "failed to register regulator %s\n",
  374. regulator->info->reg_desc.name);
  375. return PTR_ERR(regulator->rdev);
  376. }
  377. platform_set_drvdata(pdev, regulator);
  378. return 0;
  379. }
  380. static struct platform_driver da9052_regulator_driver = {
  381. .probe = da9052_regulator_probe,
  382. .driver = {
  383. .name = "da9052-regulator",
  384. },
  385. };
  386. static int __init da9052_regulator_init(void)
  387. {
  388. return platform_driver_register(&da9052_regulator_driver);
  389. }
  390. subsys_initcall(da9052_regulator_init);
  391. static void __exit da9052_regulator_exit(void)
  392. {
  393. platform_driver_unregister(&da9052_regulator_driver);
  394. }
  395. module_exit(da9052_regulator_exit);
  396. MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>");
  397. MODULE_DESCRIPTION("Power Regulator driver for Dialog DA9052 PMIC");
  398. MODULE_LICENSE("GPL");
  399. MODULE_ALIAS("platform:da9052-regulator");