da903x.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // Regulators driver for Dialog Semiconductor DA903x
  4. //
  5. // Copyright (C) 2006-2008 Marvell International Ltd.
  6. // Copyright (C) 2008 Compulab Ltd.
  7. #include <linux/kernel.h>
  8. #include <linux/init.h>
  9. #include <linux/err.h>
  10. #include <linux/module.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/regulator/driver.h>
  13. #include <linux/regulator/machine.h>
  14. #include <linux/mfd/da903x.h>
  15. /* DA9030 Registers */
  16. #define DA9030_INVAL (-1)
  17. #define DA9030_LDO1011 (0x10)
  18. #define DA9030_LDO15 (0x11)
  19. #define DA9030_LDO1416 (0x12)
  20. #define DA9030_LDO1819 (0x13)
  21. #define DA9030_LDO17 (0x14)
  22. #define DA9030_BUCK2DVM1 (0x15)
  23. #define DA9030_BUCK2DVM2 (0x16)
  24. #define DA9030_RCTL11 (0x17)
  25. #define DA9030_RCTL21 (0x18)
  26. #define DA9030_LDO1 (0x90)
  27. #define DA9030_LDO23 (0x91)
  28. #define DA9030_LDO45 (0x92)
  29. #define DA9030_LDO6 (0x93)
  30. #define DA9030_LDO78 (0x94)
  31. #define DA9030_LDO912 (0x95)
  32. #define DA9030_BUCK (0x96)
  33. #define DA9030_RCTL12 (0x97)
  34. #define DA9030_RCTL22 (0x98)
  35. #define DA9030_LDO_UNLOCK (0xa0)
  36. #define DA9030_LDO_UNLOCK_MASK (0xe0)
  37. #define DA9034_OVER1 (0x10)
  38. /* DA9034 Registers */
  39. #define DA9034_INVAL (-1)
  40. #define DA9034_OVER2 (0x11)
  41. #define DA9034_OVER3 (0x12)
  42. #define DA9034_LDO643 (0x13)
  43. #define DA9034_LDO987 (0x14)
  44. #define DA9034_LDO1110 (0x15)
  45. #define DA9034_LDO1312 (0x16)
  46. #define DA9034_LDO1514 (0x17)
  47. #define DA9034_VCC1 (0x20)
  48. #define DA9034_ADTV1 (0x23)
  49. #define DA9034_ADTV2 (0x24)
  50. #define DA9034_AVRC (0x25)
  51. #define DA9034_CDTV1 (0x26)
  52. #define DA9034_CDTV2 (0x27)
  53. #define DA9034_CVRC (0x28)
  54. #define DA9034_SDTV1 (0x29)
  55. #define DA9034_SDTV2 (0x2a)
  56. #define DA9034_SVRC (0x2b)
  57. #define DA9034_MDTV1 (0x32)
  58. #define DA9034_MDTV2 (0x33)
  59. #define DA9034_MVRC (0x34)
  60. /* DA9035 Registers. DA9034 Registers are comptabile to DA9035. */
  61. #define DA9035_OVER3 (0x12)
  62. #define DA9035_VCC2 (0x1f)
  63. #define DA9035_3DTV1 (0x2c)
  64. #define DA9035_3DTV2 (0x2d)
  65. #define DA9035_3VRC (0x2e)
  66. #define DA9035_AUTOSKIP (0x2f)
  67. struct da903x_regulator_info {
  68. struct regulator_desc desc;
  69. int max_uV;
  70. int vol_reg;
  71. int vol_shift;
  72. int vol_nbits;
  73. int update_reg;
  74. int update_bit;
  75. int enable_reg;
  76. int enable_bit;
  77. };
  78. static inline struct device *to_da903x_dev(struct regulator_dev *rdev)
  79. {
  80. return rdev_get_dev(rdev)->parent->parent;
  81. }
  82. static inline int check_range(struct da903x_regulator_info *info,
  83. int min_uV, int max_uV)
  84. {
  85. if (min_uV < info->desc.min_uV || min_uV > info->max_uV)
  86. return -EINVAL;
  87. return 0;
  88. }
  89. /* DA9030/DA9034 common operations */
  90. static int da903x_set_voltage_sel(struct regulator_dev *rdev, unsigned selector)
  91. {
  92. struct da903x_regulator_info *info = rdev_get_drvdata(rdev);
  93. struct device *da9034_dev = to_da903x_dev(rdev);
  94. uint8_t val, mask;
  95. if (rdev->desc->n_voltages == 1)
  96. return -EINVAL;
  97. val = selector << info->vol_shift;
  98. mask = ((1 << info->vol_nbits) - 1) << info->vol_shift;
  99. return da903x_update(da9034_dev, info->vol_reg, val, mask);
  100. }
  101. static int da903x_get_voltage_sel(struct regulator_dev *rdev)
  102. {
  103. struct da903x_regulator_info *info = rdev_get_drvdata(rdev);
  104. struct device *da9034_dev = to_da903x_dev(rdev);
  105. uint8_t val, mask;
  106. int ret;
  107. if (rdev->desc->n_voltages == 1)
  108. return 0;
  109. ret = da903x_read(da9034_dev, info->vol_reg, &val);
  110. if (ret)
  111. return ret;
  112. mask = ((1 << info->vol_nbits) - 1) << info->vol_shift;
  113. val = (val & mask) >> info->vol_shift;
  114. return val;
  115. }
  116. static int da903x_enable(struct regulator_dev *rdev)
  117. {
  118. struct da903x_regulator_info *info = rdev_get_drvdata(rdev);
  119. struct device *da9034_dev = to_da903x_dev(rdev);
  120. return da903x_set_bits(da9034_dev, info->enable_reg,
  121. 1 << info->enable_bit);
  122. }
  123. static int da903x_disable(struct regulator_dev *rdev)
  124. {
  125. struct da903x_regulator_info *info = rdev_get_drvdata(rdev);
  126. struct device *da9034_dev = to_da903x_dev(rdev);
  127. return da903x_clr_bits(da9034_dev, info->enable_reg,
  128. 1 << info->enable_bit);
  129. }
  130. static int da903x_is_enabled(struct regulator_dev *rdev)
  131. {
  132. struct da903x_regulator_info *info = rdev_get_drvdata(rdev);
  133. struct device *da9034_dev = to_da903x_dev(rdev);
  134. uint8_t reg_val;
  135. int ret;
  136. ret = da903x_read(da9034_dev, info->enable_reg, &reg_val);
  137. if (ret)
  138. return ret;
  139. return !!(reg_val & (1 << info->enable_bit));
  140. }
  141. /* DA9030 specific operations */
  142. static int da9030_set_ldo1_15_voltage_sel(struct regulator_dev *rdev,
  143. unsigned selector)
  144. {
  145. struct da903x_regulator_info *info = rdev_get_drvdata(rdev);
  146. struct device *da903x_dev = to_da903x_dev(rdev);
  147. uint8_t val, mask;
  148. int ret;
  149. val = selector << info->vol_shift;
  150. mask = ((1 << info->vol_nbits) - 1) << info->vol_shift;
  151. val |= DA9030_LDO_UNLOCK; /* have to set UNLOCK bits */
  152. mask |= DA9030_LDO_UNLOCK_MASK;
  153. /* write twice */
  154. ret = da903x_update(da903x_dev, info->vol_reg, val, mask);
  155. if (ret)
  156. return ret;
  157. return da903x_update(da903x_dev, info->vol_reg, val, mask);
  158. }
  159. static int da9030_map_ldo14_voltage(struct regulator_dev *rdev,
  160. int min_uV, int max_uV)
  161. {
  162. struct da903x_regulator_info *info = rdev_get_drvdata(rdev);
  163. int thresh, sel;
  164. if (check_range(info, min_uV, max_uV)) {
  165. pr_err("invalid voltage range (%d, %d) uV\n", min_uV, max_uV);
  166. return -EINVAL;
  167. }
  168. thresh = (info->max_uV + info->desc.min_uV) / 2;
  169. if (min_uV < thresh) {
  170. sel = DIV_ROUND_UP(thresh - min_uV, info->desc.uV_step);
  171. sel |= 0x4;
  172. } else {
  173. sel = DIV_ROUND_UP(min_uV - thresh, info->desc.uV_step);
  174. }
  175. return sel;
  176. }
  177. static int da9030_list_ldo14_voltage(struct regulator_dev *rdev,
  178. unsigned selector)
  179. {
  180. struct da903x_regulator_info *info = rdev_get_drvdata(rdev);
  181. int volt;
  182. if (selector & 0x4)
  183. volt = rdev->desc->min_uV +
  184. rdev->desc->uV_step * (3 - (selector & ~0x4));
  185. else
  186. volt = (info->max_uV + rdev->desc->min_uV) / 2 +
  187. rdev->desc->uV_step * (selector & ~0x4);
  188. if (volt > info->max_uV)
  189. return -EINVAL;
  190. return volt;
  191. }
  192. /* DA9034 specific operations */
  193. static int da9034_set_dvc_voltage_sel(struct regulator_dev *rdev,
  194. unsigned selector)
  195. {
  196. struct da903x_regulator_info *info = rdev_get_drvdata(rdev);
  197. struct device *da9034_dev = to_da903x_dev(rdev);
  198. uint8_t val, mask;
  199. int ret;
  200. val = selector << info->vol_shift;
  201. mask = ((1 << info->vol_nbits) - 1) << info->vol_shift;
  202. ret = da903x_update(da9034_dev, info->vol_reg, val, mask);
  203. if (ret)
  204. return ret;
  205. ret = da903x_set_bits(da9034_dev, info->update_reg,
  206. 1 << info->update_bit);
  207. return ret;
  208. }
  209. static const struct regulator_linear_range da9034_ldo12_ranges[] = {
  210. REGULATOR_LINEAR_RANGE(1700000, 0, 7, 50000),
  211. REGULATOR_LINEAR_RANGE(2700000, 8, 15, 50000),
  212. };
  213. static const struct regulator_ops da903x_regulator_ldo_ops = {
  214. .set_voltage_sel = da903x_set_voltage_sel,
  215. .get_voltage_sel = da903x_get_voltage_sel,
  216. .list_voltage = regulator_list_voltage_linear,
  217. .map_voltage = regulator_map_voltage_linear,
  218. .enable = da903x_enable,
  219. .disable = da903x_disable,
  220. .is_enabled = da903x_is_enabled,
  221. };
  222. /* NOTE: this is dedicated for the insane DA9030 LDO14 */
  223. static const struct regulator_ops da9030_regulator_ldo14_ops = {
  224. .set_voltage_sel = da903x_set_voltage_sel,
  225. .get_voltage_sel = da903x_get_voltage_sel,
  226. .list_voltage = da9030_list_ldo14_voltage,
  227. .map_voltage = da9030_map_ldo14_voltage,
  228. .enable = da903x_enable,
  229. .disable = da903x_disable,
  230. .is_enabled = da903x_is_enabled,
  231. };
  232. /* NOTE: this is dedicated for the DA9030 LDO1 and LDO15 that have locks */
  233. static const struct regulator_ops da9030_regulator_ldo1_15_ops = {
  234. .set_voltage_sel = da9030_set_ldo1_15_voltage_sel,
  235. .get_voltage_sel = da903x_get_voltage_sel,
  236. .list_voltage = regulator_list_voltage_linear,
  237. .map_voltage = regulator_map_voltage_linear,
  238. .enable = da903x_enable,
  239. .disable = da903x_disable,
  240. .is_enabled = da903x_is_enabled,
  241. };
  242. static const struct regulator_ops da9034_regulator_dvc_ops = {
  243. .set_voltage_sel = da9034_set_dvc_voltage_sel,
  244. .get_voltage_sel = da903x_get_voltage_sel,
  245. .list_voltage = regulator_list_voltage_linear,
  246. .map_voltage = regulator_map_voltage_linear,
  247. .enable = da903x_enable,
  248. .disable = da903x_disable,
  249. .is_enabled = da903x_is_enabled,
  250. };
  251. /* NOTE: this is dedicated for the insane LDO12 */
  252. static const struct regulator_ops da9034_regulator_ldo12_ops = {
  253. .set_voltage_sel = da903x_set_voltage_sel,
  254. .get_voltage_sel = da903x_get_voltage_sel,
  255. .list_voltage = regulator_list_voltage_linear_range,
  256. .map_voltage = regulator_map_voltage_linear_range,
  257. .enable = da903x_enable,
  258. .disable = da903x_disable,
  259. .is_enabled = da903x_is_enabled,
  260. };
  261. #define DA903x_LDO(_pmic, _id, min, max, step, vreg, shift, nbits, ereg, ebit) \
  262. { \
  263. .desc = { \
  264. .name = "LDO" #_id, \
  265. .ops = &da903x_regulator_ldo_ops, \
  266. .type = REGULATOR_VOLTAGE, \
  267. .id = _pmic##_ID_LDO##_id, \
  268. .n_voltages = (step) ? ((max - min) / step + 1) : 1, \
  269. .owner = THIS_MODULE, \
  270. .min_uV = (min) * 1000, \
  271. .uV_step = (step) * 1000, \
  272. }, \
  273. .max_uV = (max) * 1000, \
  274. .vol_reg = _pmic##_##vreg, \
  275. .vol_shift = (shift), \
  276. .vol_nbits = (nbits), \
  277. .enable_reg = _pmic##_##ereg, \
  278. .enable_bit = (ebit), \
  279. }
  280. #define DA903x_DVC(_pmic, _id, min, max, step, vreg, nbits, ureg, ubit, ereg, ebit) \
  281. { \
  282. .desc = { \
  283. .name = #_id, \
  284. .ops = &da9034_regulator_dvc_ops, \
  285. .type = REGULATOR_VOLTAGE, \
  286. .id = _pmic##_ID_##_id, \
  287. .n_voltages = (step) ? ((max - min) / step + 1) : 1, \
  288. .owner = THIS_MODULE, \
  289. .min_uV = (min) * 1000, \
  290. .uV_step = (step) * 1000, \
  291. }, \
  292. .max_uV = (max) * 1000, \
  293. .vol_reg = _pmic##_##vreg, \
  294. .vol_shift = (0), \
  295. .vol_nbits = (nbits), \
  296. .update_reg = _pmic##_##ureg, \
  297. .update_bit = (ubit), \
  298. .enable_reg = _pmic##_##ereg, \
  299. .enable_bit = (ebit), \
  300. }
  301. #define DA9034_LDO(_id, min, max, step, vreg, shift, nbits, ereg, ebit) \
  302. DA903x_LDO(DA9034, _id, min, max, step, vreg, shift, nbits, ereg, ebit)
  303. #define DA9030_LDO(_id, min, max, step, vreg, shift, nbits, ereg, ebit) \
  304. DA903x_LDO(DA9030, _id, min, max, step, vreg, shift, nbits, ereg, ebit)
  305. #define DA9030_DVC(_id, min, max, step, vreg, nbits, ureg, ubit, ereg, ebit) \
  306. DA903x_DVC(DA9030, _id, min, max, step, vreg, nbits, ureg, ubit, \
  307. ereg, ebit)
  308. #define DA9034_DVC(_id, min, max, step, vreg, nbits, ureg, ubit, ereg, ebit) \
  309. DA903x_DVC(DA9034, _id, min, max, step, vreg, nbits, ureg, ubit, \
  310. ereg, ebit)
  311. #define DA9035_DVC(_id, min, max, step, vreg, nbits, ureg, ubit, ereg, ebit) \
  312. DA903x_DVC(DA9035, _id, min, max, step, vreg, nbits, ureg, ubit, \
  313. ereg, ebit)
  314. static struct da903x_regulator_info da903x_regulator_info[] = {
  315. /* DA9030 */
  316. DA9030_DVC(BUCK2, 850, 1625, 25, BUCK2DVM1, 5, BUCK2DVM1, 7, RCTL11, 0),
  317. DA9030_LDO( 1, 1200, 3200, 100, LDO1, 0, 5, RCTL12, 1),
  318. DA9030_LDO( 2, 1800, 3200, 100, LDO23, 0, 4, RCTL12, 2),
  319. DA9030_LDO( 3, 1800, 3200, 100, LDO23, 4, 4, RCTL12, 3),
  320. DA9030_LDO( 4, 1800, 3200, 100, LDO45, 0, 4, RCTL12, 4),
  321. DA9030_LDO( 5, 1800, 3200, 100, LDO45, 4, 4, RCTL12, 5),
  322. DA9030_LDO( 6, 1800, 3200, 100, LDO6, 0, 4, RCTL12, 6),
  323. DA9030_LDO( 7, 1800, 3200, 100, LDO78, 0, 4, RCTL12, 7),
  324. DA9030_LDO( 8, 1800, 3200, 100, LDO78, 4, 4, RCTL22, 0),
  325. DA9030_LDO( 9, 1800, 3200, 100, LDO912, 0, 4, RCTL22, 1),
  326. DA9030_LDO(10, 1800, 3200, 100, LDO1011, 0, 4, RCTL22, 2),
  327. DA9030_LDO(11, 1800, 3200, 100, LDO1011, 4, 4, RCTL22, 3),
  328. DA9030_LDO(12, 1800, 3200, 100, LDO912, 4, 4, RCTL22, 4),
  329. DA9030_LDO(14, 2760, 2940, 30, LDO1416, 0, 3, RCTL11, 4),
  330. DA9030_LDO(15, 1100, 2650, 50, LDO15, 0, 5, RCTL11, 5),
  331. DA9030_LDO(16, 1100, 2650, 50, LDO1416, 3, 5, RCTL11, 6),
  332. DA9030_LDO(17, 1800, 3200, 100, LDO17, 0, 4, RCTL11, 7),
  333. DA9030_LDO(18, 1800, 3200, 100, LDO1819, 0, 4, RCTL21, 2),
  334. DA9030_LDO(19, 1800, 3200, 100, LDO1819, 4, 4, RCTL21, 1),
  335. DA9030_LDO(13, 2100, 2100, 0, INVAL, 0, 0, RCTL11, 3), /* fixed @2.1V */
  336. /* DA9034 */
  337. DA9034_DVC(BUCK1, 725, 1500, 25, ADTV2, 5, VCC1, 0, OVER1, 0),
  338. DA9034_DVC(BUCK2, 725, 1500, 25, CDTV2, 5, VCC1, 2, OVER1, 1),
  339. DA9034_DVC(LDO2, 725, 1500, 25, SDTV2, 5, VCC1, 4, OVER1, 2),
  340. DA9034_DVC(LDO1, 1700, 2075, 25, MDTV1, 4, VCC1, 6, OVER3, 4),
  341. DA9034_LDO( 3, 1800, 3300, 100, LDO643, 0, 4, OVER3, 5),
  342. DA9034_LDO( 4, 1800, 2900,1100, LDO643, 4, 1, OVER3, 6),
  343. DA9034_LDO( 6, 2500, 2850, 50, LDO643, 5, 3, OVER2, 0),
  344. DA9034_LDO( 7, 2700, 3050, 50, LDO987, 0, 3, OVER2, 1),
  345. DA9034_LDO( 8, 2700, 2850, 50, LDO987, 3, 2, OVER2, 2),
  346. DA9034_LDO( 9, 2700, 3050, 50, LDO987, 5, 3, OVER2, 3),
  347. DA9034_LDO(10, 2700, 3050, 50, LDO1110, 0, 3, OVER2, 4),
  348. DA9034_LDO(11, 1800, 3300, 100, LDO1110, 4, 4, OVER2, 5),
  349. DA9034_LDO(12, 1700, 3050, 50, LDO1312, 0, 4, OVER3, 6),
  350. DA9034_LDO(13, 1800, 3300, 100, LDO1312, 4, 4, OVER2, 7),
  351. DA9034_LDO(14, 1800, 3300, 100, LDO1514, 0, 4, OVER3, 0),
  352. DA9034_LDO(15, 1800, 3300, 100, LDO1514, 4, 4, OVER3, 1),
  353. DA9034_LDO(5, 3100, 3100, 0, INVAL, 0, 0, OVER3, 7), /* fixed @3.1V */
  354. /* DA9035 */
  355. DA9035_DVC(BUCK3, 1800, 2200, 100, 3DTV1, 3, VCC2, 0, OVER3, 3),
  356. };
  357. static inline struct da903x_regulator_info *find_regulator_info(int id)
  358. {
  359. struct da903x_regulator_info *ri;
  360. int i;
  361. for (i = 0; i < ARRAY_SIZE(da903x_regulator_info); i++) {
  362. ri = &da903x_regulator_info[i];
  363. if (ri->desc.id == id)
  364. return ri;
  365. }
  366. return NULL;
  367. }
  368. static int da903x_regulator_probe(struct platform_device *pdev)
  369. {
  370. struct da903x_regulator_info *ri = NULL;
  371. struct regulator_dev *rdev;
  372. struct regulator_config config = { };
  373. ri = find_regulator_info(pdev->id);
  374. if (ri == NULL) {
  375. dev_err(&pdev->dev, "invalid regulator ID specified\n");
  376. return -EINVAL;
  377. }
  378. /* Workaround for the weird LDO12 voltage setting */
  379. if (ri->desc.id == DA9034_ID_LDO12) {
  380. ri->desc.ops = &da9034_regulator_ldo12_ops;
  381. ri->desc.n_voltages = 16;
  382. ri->desc.linear_ranges = da9034_ldo12_ranges;
  383. ri->desc.n_linear_ranges = ARRAY_SIZE(da9034_ldo12_ranges);
  384. }
  385. if (ri->desc.id == DA9030_ID_LDO14)
  386. ri->desc.ops = &da9030_regulator_ldo14_ops;
  387. if (ri->desc.id == DA9030_ID_LDO1 || ri->desc.id == DA9030_ID_LDO15)
  388. ri->desc.ops = &da9030_regulator_ldo1_15_ops;
  389. config.dev = &pdev->dev;
  390. config.init_data = dev_get_platdata(&pdev->dev);
  391. config.driver_data = ri;
  392. rdev = devm_regulator_register(&pdev->dev, &ri->desc, &config);
  393. if (IS_ERR(rdev)) {
  394. dev_err(&pdev->dev, "failed to register regulator %s\n",
  395. ri->desc.name);
  396. return PTR_ERR(rdev);
  397. }
  398. platform_set_drvdata(pdev, rdev);
  399. return 0;
  400. }
  401. static struct platform_driver da903x_regulator_driver = {
  402. .driver = {
  403. .name = "da903x-regulator",
  404. },
  405. .probe = da903x_regulator_probe,
  406. };
  407. static int __init da903x_regulator_init(void)
  408. {
  409. return platform_driver_register(&da903x_regulator_driver);
  410. }
  411. subsys_initcall(da903x_regulator_init);
  412. static void __exit da903x_regulator_exit(void)
  413. {
  414. platform_driver_unregister(&da903x_regulator_driver);
  415. }
  416. module_exit(da903x_regulator_exit);
  417. MODULE_LICENSE("GPL");
  418. MODULE_AUTHOR("Eric Miao <eric.miao@marvell.com>"
  419. "Mike Rapoport <mike@compulab.co.il>");
  420. MODULE_DESCRIPTION("Regulator Driver for Dialog Semiconductor DA903X PMIC");
  421. MODULE_ALIAS("platform:da903x-regulator");