da9052-regulator.c 13 KB

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