da9052-regulator.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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 = (new_sel - old_sel) * info->step_uV / 6250;
  225. break;
  226. }
  227. return ret;
  228. }
  229. static const struct regulator_ops da9052_dcdc_ops = {
  230. .get_current_limit = da9052_dcdc_get_current_limit,
  231. .set_current_limit = da9052_dcdc_set_current_limit,
  232. .list_voltage = da9052_list_voltage,
  233. .map_voltage = da9052_map_voltage,
  234. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  235. .set_voltage_sel = da9052_regulator_set_voltage_sel,
  236. .set_voltage_time_sel = da9052_regulator_set_voltage_time_sel,
  237. .is_enabled = regulator_is_enabled_regmap,
  238. .enable = regulator_enable_regmap,
  239. .disable = regulator_disable_regmap,
  240. };
  241. static const struct regulator_ops da9052_ldo_ops = {
  242. .list_voltage = da9052_list_voltage,
  243. .map_voltage = da9052_map_voltage,
  244. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  245. .set_voltage_sel = da9052_regulator_set_voltage_sel,
  246. .set_voltage_time_sel = da9052_regulator_set_voltage_time_sel,
  247. .is_enabled = regulator_is_enabled_regmap,
  248. .enable = regulator_enable_regmap,
  249. .disable = regulator_disable_regmap,
  250. };
  251. #define DA9052_LDO(_id, step, min, max, sbits, ebits, abits) \
  252. {\
  253. .reg_desc = {\
  254. .name = #_id,\
  255. .ops = &da9052_ldo_ops,\
  256. .type = REGULATOR_VOLTAGE,\
  257. .id = DA9052_ID_##_id,\
  258. .n_voltages = (max - min) / step + 1, \
  259. .owner = THIS_MODULE,\
  260. .vsel_reg = DA9052_BUCKCORE_REG + DA9052_ID_##_id, \
  261. .vsel_mask = (1 << (sbits)) - 1,\
  262. .enable_reg = DA9052_BUCKCORE_REG + DA9052_ID_##_id, \
  263. .enable_mask = 1 << (ebits),\
  264. },\
  265. .min_uV = (min) * 1000,\
  266. .max_uV = (max) * 1000,\
  267. .step_uV = (step) * 1000,\
  268. .activate_bit = (abits),\
  269. }
  270. #define DA9052_DCDC(_id, step, min, max, sbits, ebits, abits) \
  271. {\
  272. .reg_desc = {\
  273. .name = #_id,\
  274. .ops = &da9052_dcdc_ops,\
  275. .type = REGULATOR_VOLTAGE,\
  276. .id = DA9052_ID_##_id,\
  277. .n_voltages = (max - min) / step + 1, \
  278. .owner = THIS_MODULE,\
  279. .vsel_reg = DA9052_BUCKCORE_REG + DA9052_ID_##_id, \
  280. .vsel_mask = (1 << (sbits)) - 1,\
  281. .enable_reg = DA9052_BUCKCORE_REG + DA9052_ID_##_id, \
  282. .enable_mask = 1 << (ebits),\
  283. },\
  284. .min_uV = (min) * 1000,\
  285. .max_uV = (max) * 1000,\
  286. .step_uV = (step) * 1000,\
  287. .activate_bit = (abits),\
  288. }
  289. static struct da9052_regulator_info da9052_regulator_info[] = {
  290. DA9052_DCDC(BUCK1, 25, 500, 2075, 6, 6, DA9052_SUPPLY_VBCOREGO),
  291. DA9052_DCDC(BUCK2, 25, 500, 2075, 6, 6, DA9052_SUPPLY_VBPROGO),
  292. DA9052_DCDC(BUCK3, 25, 950, 2525, 6, 6, DA9052_SUPPLY_VBMEMGO),
  293. DA9052_DCDC(BUCK4, 50, 1800, 3600, 5, 6, 0),
  294. DA9052_LDO(LDO1, 50, 600, 1800, 5, 6, 0),
  295. DA9052_LDO(LDO2, 25, 600, 1800, 6, 6, DA9052_SUPPLY_VLDO2GO),
  296. DA9052_LDO(LDO3, 25, 1725, 3300, 6, 6, DA9052_SUPPLY_VLDO3GO),
  297. DA9052_LDO(LDO4, 25, 1725, 3300, 6, 6, 0),
  298. DA9052_LDO(LDO5, 50, 1200, 3600, 6, 6, 0),
  299. DA9052_LDO(LDO6, 50, 1200, 3600, 6, 6, 0),
  300. DA9052_LDO(LDO7, 50, 1200, 3600, 6, 6, 0),
  301. DA9052_LDO(LDO8, 50, 1200, 3600, 6, 6, 0),
  302. DA9052_LDO(LDO9, 50, 1250, 3650, 6, 6, 0),
  303. DA9052_LDO(LDO10, 50, 1200, 3600, 6, 6, 0),
  304. };
  305. static struct da9052_regulator_info da9053_regulator_info[] = {
  306. DA9052_DCDC(BUCK1, 25, 500, 2075, 6, 6, DA9052_SUPPLY_VBCOREGO),
  307. DA9052_DCDC(BUCK2, 25, 500, 2075, 6, 6, DA9052_SUPPLY_VBPROGO),
  308. DA9052_DCDC(BUCK3, 25, 950, 2525, 6, 6, DA9052_SUPPLY_VBMEMGO),
  309. DA9052_DCDC(BUCK4, 25, 950, 2525, 6, 6, 0),
  310. DA9052_LDO(LDO1, 50, 600, 1800, 5, 6, 0),
  311. DA9052_LDO(LDO2, 25, 600, 1800, 6, 6, DA9052_SUPPLY_VLDO2GO),
  312. DA9052_LDO(LDO3, 25, 1725, 3300, 6, 6, DA9052_SUPPLY_VLDO3GO),
  313. DA9052_LDO(LDO4, 25, 1725, 3300, 6, 6, 0),
  314. DA9052_LDO(LDO5, 50, 1200, 3600, 6, 6, 0),
  315. DA9052_LDO(LDO6, 50, 1200, 3600, 6, 6, 0),
  316. DA9052_LDO(LDO7, 50, 1200, 3600, 6, 6, 0),
  317. DA9052_LDO(LDO8, 50, 1200, 3600, 6, 6, 0),
  318. DA9052_LDO(LDO9, 50, 1250, 3650, 6, 6, 0),
  319. DA9052_LDO(LDO10, 50, 1200, 3600, 6, 6, 0),
  320. };
  321. static inline struct da9052_regulator_info *find_regulator_info(u8 chip_id,
  322. int id)
  323. {
  324. struct da9052_regulator_info *info;
  325. int i;
  326. switch (chip_id) {
  327. case DA9052:
  328. for (i = 0; i < ARRAY_SIZE(da9052_regulator_info); i++) {
  329. info = &da9052_regulator_info[i];
  330. if (info->reg_desc.id == id)
  331. return info;
  332. }
  333. break;
  334. case DA9053_AA:
  335. case DA9053_BA:
  336. case DA9053_BB:
  337. case DA9053_BC:
  338. for (i = 0; i < ARRAY_SIZE(da9053_regulator_info); i++) {
  339. info = &da9053_regulator_info[i];
  340. if (info->reg_desc.id == id)
  341. return info;
  342. }
  343. break;
  344. }
  345. return NULL;
  346. }
  347. static int da9052_regulator_probe(struct platform_device *pdev)
  348. {
  349. const struct mfd_cell *cell = mfd_get_cell(pdev);
  350. struct regulator_config config = { };
  351. struct da9052_regulator *regulator;
  352. struct da9052 *da9052;
  353. struct da9052_pdata *pdata;
  354. regulator = devm_kzalloc(&pdev->dev, sizeof(struct da9052_regulator),
  355. GFP_KERNEL);
  356. if (!regulator)
  357. return -ENOMEM;
  358. da9052 = dev_get_drvdata(pdev->dev.parent);
  359. pdata = dev_get_platdata(da9052->dev);
  360. regulator->da9052 = da9052;
  361. regulator->info = find_regulator_info(regulator->da9052->chip_id,
  362. cell->id);
  363. if (regulator->info == NULL) {
  364. dev_err(&pdev->dev, "invalid regulator ID specified\n");
  365. return -EINVAL;
  366. }
  367. config.dev = &pdev->dev;
  368. config.driver_data = regulator;
  369. config.regmap = da9052->regmap;
  370. if (pdata && pdata->regulators) {
  371. config.init_data = pdata->regulators[cell->id];
  372. } else {
  373. #ifdef CONFIG_OF
  374. struct device_node *nproot = da9052->dev->of_node;
  375. struct device_node *np;
  376. if (!nproot)
  377. return -ENODEV;
  378. nproot = of_get_child_by_name(nproot, "regulators");
  379. if (!nproot)
  380. return -ENODEV;
  381. for_each_child_of_node(nproot, np) {
  382. if (!of_node_cmp(np->name,
  383. regulator->info->reg_desc.name)) {
  384. config.init_data = of_get_regulator_init_data(
  385. &pdev->dev, np,
  386. &regulator->info->reg_desc);
  387. config.of_node = np;
  388. break;
  389. }
  390. }
  391. of_node_put(nproot);
  392. #endif
  393. }
  394. regulator->rdev = devm_regulator_register(&pdev->dev,
  395. &regulator->info->reg_desc,
  396. &config);
  397. if (IS_ERR(regulator->rdev)) {
  398. dev_err(&pdev->dev, "failed to register regulator %s\n",
  399. regulator->info->reg_desc.name);
  400. return PTR_ERR(regulator->rdev);
  401. }
  402. platform_set_drvdata(pdev, regulator);
  403. return 0;
  404. }
  405. static struct platform_driver da9052_regulator_driver = {
  406. .probe = da9052_regulator_probe,
  407. .driver = {
  408. .name = "da9052-regulator",
  409. },
  410. };
  411. static int __init da9052_regulator_init(void)
  412. {
  413. return platform_driver_register(&da9052_regulator_driver);
  414. }
  415. subsys_initcall(da9052_regulator_init);
  416. static void __exit da9052_regulator_exit(void)
  417. {
  418. platform_driver_unregister(&da9052_regulator_driver);
  419. }
  420. module_exit(da9052_regulator_exit);
  421. MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>");
  422. MODULE_DESCRIPTION("Power Regulator driver for Dialog DA9052 PMIC");
  423. MODULE_LICENSE("GPL");
  424. MODULE_ALIAS("platform:da9052-regulator");