da9063-regulator.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  1. // SPDX-License-Identifier: GPL-2.0+
  2. //
  3. // Regulator driver for DA9063 PMIC series
  4. //
  5. // Copyright 2012 Dialog Semiconductors Ltd.
  6. // Copyright 2013 Philipp Zabel, Pengutronix
  7. //
  8. // Author: Krystian Garbaciak <krystian.garbaciak@diasemi.com>
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/init.h>
  12. #include <linux/err.h>
  13. #include <linux/slab.h>
  14. #include <linux/of.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/regmap.h>
  17. #include <linux/regulator/driver.h>
  18. #include <linux/regulator/machine.h>
  19. #include <linux/regulator/of_regulator.h>
  20. #include <linux/mfd/da9063/core.h>
  21. #include <linux/mfd/da9063/registers.h>
  22. /* Definition for registering regmap bit fields using a mask */
  23. #define BFIELD(_reg, _mask) \
  24. REG_FIELD(_reg, __builtin_ffs((int)_mask) - 1, \
  25. sizeof(unsigned int) * 8 - __builtin_clz((_mask)) - 1)
  26. /* DA9063 and DA9063L regulator IDs */
  27. enum {
  28. /* BUCKs */
  29. DA9063_ID_BCORE1,
  30. DA9063_ID_BCORE2,
  31. DA9063_ID_BPRO,
  32. DA9063_ID_BMEM,
  33. DA9063_ID_BIO,
  34. DA9063_ID_BPERI,
  35. /* BCORE1 and BCORE2 in merged mode */
  36. DA9063_ID_BCORES_MERGED,
  37. /* BMEM and BIO in merged mode */
  38. DA9063_ID_BMEM_BIO_MERGED,
  39. /* When two BUCKs are merged, they cannot be reused separately */
  40. /* LDOs on both DA9063 and DA9063L */
  41. DA9063_ID_LDO3,
  42. DA9063_ID_LDO7,
  43. DA9063_ID_LDO8,
  44. DA9063_ID_LDO9,
  45. DA9063_ID_LDO11,
  46. /* DA9063-only LDOs */
  47. DA9063_ID_LDO1,
  48. DA9063_ID_LDO2,
  49. DA9063_ID_LDO4,
  50. DA9063_ID_LDO5,
  51. DA9063_ID_LDO6,
  52. DA9063_ID_LDO10,
  53. };
  54. /* Old regulator platform data */
  55. struct da9063_regulator_data {
  56. int id;
  57. struct regulator_init_data *initdata;
  58. };
  59. struct da9063_regulators_pdata {
  60. unsigned n_regulators;
  61. struct da9063_regulator_data *regulator_data;
  62. };
  63. /* Regulator capabilities and registers description */
  64. struct da9063_regulator_info {
  65. struct regulator_desc desc;
  66. /* DA9063 main register fields */
  67. struct reg_field mode; /* buck mode of operation */
  68. struct reg_field suspend;
  69. struct reg_field sleep;
  70. struct reg_field suspend_sleep;
  71. unsigned int suspend_vsel_reg;
  72. /* DA9063 event detection bit */
  73. struct reg_field oc_event;
  74. };
  75. /* Macros for LDO */
  76. #define DA9063_LDO(chip, regl_name, min_mV, step_mV, max_mV) \
  77. .desc.id = chip##_ID_##regl_name, \
  78. .desc.name = __stringify(chip##_##regl_name), \
  79. .desc.ops = &da9063_ldo_ops, \
  80. .desc.min_uV = (min_mV) * 1000, \
  81. .desc.uV_step = (step_mV) * 1000, \
  82. .desc.n_voltages = (((max_mV) - (min_mV))/(step_mV) + 1 \
  83. + (DA9063_V##regl_name##_BIAS)), \
  84. .desc.enable_reg = DA9063_REG_##regl_name##_CONT, \
  85. .desc.enable_mask = DA9063_LDO_EN, \
  86. .desc.vsel_reg = DA9063_REG_V##regl_name##_A, \
  87. .desc.vsel_mask = DA9063_V##regl_name##_MASK, \
  88. .desc.linear_min_sel = DA9063_V##regl_name##_BIAS, \
  89. .sleep = BFIELD(DA9063_REG_V##regl_name##_A, DA9063_LDO_SL), \
  90. .suspend_sleep = BFIELD(DA9063_REG_V##regl_name##_B, DA9063_LDO_SL), \
  91. .suspend_vsel_reg = DA9063_REG_V##regl_name##_B
  92. /* Macros for voltage DC/DC converters (BUCKs) */
  93. #define DA9063_BUCK(chip, regl_name, min_mV, step_mV, max_mV, limits_array, \
  94. creg, cmask) \
  95. .desc.id = chip##_ID_##regl_name, \
  96. .desc.name = __stringify(chip##_##regl_name), \
  97. .desc.ops = &da9063_buck_ops, \
  98. .desc.min_uV = (min_mV) * 1000, \
  99. .desc.uV_step = (step_mV) * 1000, \
  100. .desc.n_voltages = ((max_mV) - (min_mV))/(step_mV) + 1, \
  101. .desc.csel_reg = (creg), \
  102. .desc.csel_mask = (cmask), \
  103. .desc.curr_table = limits_array, \
  104. .desc.n_current_limits = ARRAY_SIZE(limits_array)
  105. #define DA9063_BUCK_COMMON_FIELDS(regl_name) \
  106. .desc.enable_reg = DA9063_REG_##regl_name##_CONT, \
  107. .desc.enable_mask = DA9063_BUCK_EN, \
  108. .desc.vsel_reg = DA9063_REG_V##regl_name##_A, \
  109. .desc.vsel_mask = DA9063_VBUCK_MASK, \
  110. .desc.linear_min_sel = DA9063_VBUCK_BIAS, \
  111. .sleep = BFIELD(DA9063_REG_V##regl_name##_A, DA9063_BUCK_SL), \
  112. .suspend_sleep = BFIELD(DA9063_REG_V##regl_name##_B, DA9063_BUCK_SL), \
  113. .suspend_vsel_reg = DA9063_REG_V##regl_name##_B, \
  114. .mode = BFIELD(DA9063_REG_##regl_name##_CFG, DA9063_BUCK_MODE_MASK)
  115. /* Defines asignment of regulators info table to chip model */
  116. struct da9063_dev_model {
  117. const struct da9063_regulator_info *regulator_info;
  118. unsigned n_regulators;
  119. enum da9063_type type;
  120. };
  121. /* Single regulator settings */
  122. struct da9063_regulator {
  123. struct regulator_desc desc;
  124. struct regulator_dev *rdev;
  125. struct da9063 *hw;
  126. const struct da9063_regulator_info *info;
  127. struct regmap_field *mode;
  128. struct regmap_field *suspend;
  129. struct regmap_field *sleep;
  130. struct regmap_field *suspend_sleep;
  131. };
  132. /* Encapsulates all information for the regulators driver */
  133. struct da9063_regulators {
  134. unsigned n_regulators;
  135. /* Array size to be defined during init. Keep at end. */
  136. struct da9063_regulator regulator[0];
  137. };
  138. /* BUCK modes for DA9063 */
  139. enum {
  140. BUCK_MODE_MANUAL, /* 0 */
  141. BUCK_MODE_SLEEP, /* 1 */
  142. BUCK_MODE_SYNC, /* 2 */
  143. BUCK_MODE_AUTO /* 3 */
  144. };
  145. /* Regulator operations */
  146. /* Current limits array (in uA) for BCORE1, BCORE2, BPRO.
  147. Entry indexes corresponds to register values. */
  148. static const unsigned int da9063_buck_a_limits[] = {
  149. 500000, 600000, 700000, 800000, 900000, 1000000, 1100000, 1200000,
  150. 1300000, 1400000, 1500000, 1600000, 1700000, 1800000, 1900000, 2000000
  151. };
  152. /* Current limits array (in uA) for BMEM, BIO, BPERI.
  153. Entry indexes corresponds to register values. */
  154. static const unsigned int da9063_buck_b_limits[] = {
  155. 1500000, 1600000, 1700000, 1800000, 1900000, 2000000, 2100000, 2200000,
  156. 2300000, 2400000, 2500000, 2600000, 2700000, 2800000, 2900000, 3000000
  157. };
  158. /* Current limits array (in uA) for merged BCORE1 and BCORE2.
  159. Entry indexes corresponds to register values. */
  160. static const unsigned int da9063_bcores_merged_limits[] = {
  161. 1000000, 1200000, 1400000, 1600000, 1800000, 2000000, 2200000, 2400000,
  162. 2600000, 2800000, 3000000, 3200000, 3400000, 3600000, 3800000, 4000000
  163. };
  164. /* Current limits array (in uA) for merged BMEM and BIO.
  165. Entry indexes corresponds to register values. */
  166. static const unsigned int da9063_bmem_bio_merged_limits[] = {
  167. 3000000, 3200000, 3400000, 3600000, 3800000, 4000000, 4200000, 4400000,
  168. 4600000, 4800000, 5000000, 5200000, 5400000, 5600000, 5800000, 6000000
  169. };
  170. static int da9063_buck_set_mode(struct regulator_dev *rdev, unsigned mode)
  171. {
  172. struct da9063_regulator *regl = rdev_get_drvdata(rdev);
  173. unsigned val;
  174. switch (mode) {
  175. case REGULATOR_MODE_FAST:
  176. val = BUCK_MODE_SYNC;
  177. break;
  178. case REGULATOR_MODE_NORMAL:
  179. val = BUCK_MODE_AUTO;
  180. break;
  181. case REGULATOR_MODE_STANDBY:
  182. val = BUCK_MODE_SLEEP;
  183. break;
  184. default:
  185. return -EINVAL;
  186. }
  187. return regmap_field_write(regl->mode, val);
  188. }
  189. /*
  190. * Bucks use single mode register field for normal operation
  191. * and suspend state.
  192. * There are 3 modes to map to: FAST, NORMAL, and STANDBY.
  193. */
  194. static unsigned da9063_buck_get_mode(struct regulator_dev *rdev)
  195. {
  196. struct da9063_regulator *regl = rdev_get_drvdata(rdev);
  197. struct regmap_field *field;
  198. unsigned int val, mode = 0;
  199. int ret;
  200. ret = regmap_field_read(regl->mode, &val);
  201. if (ret < 0)
  202. return ret;
  203. switch (val) {
  204. default:
  205. case BUCK_MODE_MANUAL:
  206. mode = REGULATOR_MODE_FAST | REGULATOR_MODE_STANDBY;
  207. /* Sleep flag bit decides the mode */
  208. break;
  209. case BUCK_MODE_SLEEP:
  210. return REGULATOR_MODE_STANDBY;
  211. case BUCK_MODE_SYNC:
  212. return REGULATOR_MODE_FAST;
  213. case BUCK_MODE_AUTO:
  214. return REGULATOR_MODE_NORMAL;
  215. }
  216. /* Detect current regulator state */
  217. ret = regmap_field_read(regl->suspend, &val);
  218. if (ret < 0)
  219. return 0;
  220. /* Read regulator mode from proper register, depending on state */
  221. if (val)
  222. field = regl->suspend_sleep;
  223. else
  224. field = regl->sleep;
  225. ret = regmap_field_read(field, &val);
  226. if (ret < 0)
  227. return 0;
  228. if (val)
  229. mode &= REGULATOR_MODE_STANDBY;
  230. else
  231. mode &= REGULATOR_MODE_NORMAL | REGULATOR_MODE_FAST;
  232. return mode;
  233. }
  234. /*
  235. * LDOs use sleep flags - one for normal and one for suspend state.
  236. * There are 2 modes to map to: NORMAL and STANDBY (sleep) for each state.
  237. */
  238. static int da9063_ldo_set_mode(struct regulator_dev *rdev, unsigned mode)
  239. {
  240. struct da9063_regulator *regl = rdev_get_drvdata(rdev);
  241. unsigned val;
  242. switch (mode) {
  243. case REGULATOR_MODE_NORMAL:
  244. val = 0;
  245. break;
  246. case REGULATOR_MODE_STANDBY:
  247. val = 1;
  248. break;
  249. default:
  250. return -EINVAL;
  251. }
  252. return regmap_field_write(regl->sleep, val);
  253. }
  254. static unsigned da9063_ldo_get_mode(struct regulator_dev *rdev)
  255. {
  256. struct da9063_regulator *regl = rdev_get_drvdata(rdev);
  257. struct regmap_field *field;
  258. int ret, val;
  259. /* Detect current regulator state */
  260. ret = regmap_field_read(regl->suspend, &val);
  261. if (ret < 0)
  262. return 0;
  263. /* Read regulator mode from proper register, depending on state */
  264. if (val)
  265. field = regl->suspend_sleep;
  266. else
  267. field = regl->sleep;
  268. ret = regmap_field_read(field, &val);
  269. if (ret < 0)
  270. return 0;
  271. if (val)
  272. return REGULATOR_MODE_STANDBY;
  273. else
  274. return REGULATOR_MODE_NORMAL;
  275. }
  276. static int da9063_buck_get_status(struct regulator_dev *rdev)
  277. {
  278. int ret = regulator_is_enabled_regmap(rdev);
  279. if (ret == 0) {
  280. ret = REGULATOR_STATUS_OFF;
  281. } else if (ret > 0) {
  282. ret = da9063_buck_get_mode(rdev);
  283. if (ret > 0)
  284. ret = regulator_mode_to_status(ret);
  285. else if (ret == 0)
  286. ret = -EIO;
  287. }
  288. return ret;
  289. }
  290. static int da9063_ldo_get_status(struct regulator_dev *rdev)
  291. {
  292. int ret = regulator_is_enabled_regmap(rdev);
  293. if (ret == 0) {
  294. ret = REGULATOR_STATUS_OFF;
  295. } else if (ret > 0) {
  296. ret = da9063_ldo_get_mode(rdev);
  297. if (ret > 0)
  298. ret = regulator_mode_to_status(ret);
  299. else if (ret == 0)
  300. ret = -EIO;
  301. }
  302. return ret;
  303. }
  304. static int da9063_set_suspend_voltage(struct regulator_dev *rdev, int uV)
  305. {
  306. struct da9063_regulator *regl = rdev_get_drvdata(rdev);
  307. const struct da9063_regulator_info *rinfo = regl->info;
  308. int ret, sel;
  309. sel = regulator_map_voltage_linear(rdev, uV, uV);
  310. if (sel < 0)
  311. return sel;
  312. sel <<= ffs(rdev->desc->vsel_mask) - 1;
  313. ret = regmap_update_bits(regl->hw->regmap, rinfo->suspend_vsel_reg,
  314. rdev->desc->vsel_mask, sel);
  315. return ret;
  316. }
  317. static int da9063_suspend_enable(struct regulator_dev *rdev)
  318. {
  319. struct da9063_regulator *regl = rdev_get_drvdata(rdev);
  320. return regmap_field_write(regl->suspend, 1);
  321. }
  322. static int da9063_suspend_disable(struct regulator_dev *rdev)
  323. {
  324. struct da9063_regulator *regl = rdev_get_drvdata(rdev);
  325. return regmap_field_write(regl->suspend, 0);
  326. }
  327. static int da9063_buck_set_suspend_mode(struct regulator_dev *rdev, unsigned mode)
  328. {
  329. struct da9063_regulator *regl = rdev_get_drvdata(rdev);
  330. int val;
  331. switch (mode) {
  332. case REGULATOR_MODE_FAST:
  333. val = BUCK_MODE_SYNC;
  334. break;
  335. case REGULATOR_MODE_NORMAL:
  336. val = BUCK_MODE_AUTO;
  337. break;
  338. case REGULATOR_MODE_STANDBY:
  339. val = BUCK_MODE_SLEEP;
  340. break;
  341. default:
  342. return -EINVAL;
  343. }
  344. return regmap_field_write(regl->mode, val);
  345. }
  346. static int da9063_ldo_set_suspend_mode(struct regulator_dev *rdev, unsigned mode)
  347. {
  348. struct da9063_regulator *regl = rdev_get_drvdata(rdev);
  349. unsigned val;
  350. switch (mode) {
  351. case REGULATOR_MODE_NORMAL:
  352. val = 0;
  353. break;
  354. case REGULATOR_MODE_STANDBY:
  355. val = 1;
  356. break;
  357. default:
  358. return -EINVAL;
  359. }
  360. return regmap_field_write(regl->suspend_sleep, val);
  361. }
  362. static const struct regulator_ops da9063_buck_ops = {
  363. .enable = regulator_enable_regmap,
  364. .disable = regulator_disable_regmap,
  365. .is_enabled = regulator_is_enabled_regmap,
  366. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  367. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  368. .list_voltage = regulator_list_voltage_linear,
  369. .set_current_limit = regulator_set_current_limit_regmap,
  370. .get_current_limit = regulator_get_current_limit_regmap,
  371. .set_mode = da9063_buck_set_mode,
  372. .get_mode = da9063_buck_get_mode,
  373. .get_status = da9063_buck_get_status,
  374. .set_suspend_voltage = da9063_set_suspend_voltage,
  375. .set_suspend_enable = da9063_suspend_enable,
  376. .set_suspend_disable = da9063_suspend_disable,
  377. .set_suspend_mode = da9063_buck_set_suspend_mode,
  378. };
  379. static const struct regulator_ops da9063_ldo_ops = {
  380. .enable = regulator_enable_regmap,
  381. .disable = regulator_disable_regmap,
  382. .is_enabled = regulator_is_enabled_regmap,
  383. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  384. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  385. .list_voltage = regulator_list_voltage_linear,
  386. .set_mode = da9063_ldo_set_mode,
  387. .get_mode = da9063_ldo_get_mode,
  388. .get_status = da9063_ldo_get_status,
  389. .set_suspend_voltage = da9063_set_suspend_voltage,
  390. .set_suspend_enable = da9063_suspend_enable,
  391. .set_suspend_disable = da9063_suspend_disable,
  392. .set_suspend_mode = da9063_ldo_set_suspend_mode,
  393. };
  394. /* Info of regulators for DA9063 */
  395. static const struct da9063_regulator_info da9063_regulator_info[] = {
  396. {
  397. DA9063_BUCK(DA9063, BCORE1, 300, 10, 1570,
  398. da9063_buck_a_limits,
  399. DA9063_REG_BUCK_ILIM_C, DA9063_BCORE1_ILIM_MASK),
  400. DA9063_BUCK_COMMON_FIELDS(BCORE1),
  401. .suspend = BFIELD(DA9063_REG_DVC_1, DA9063_VBCORE1_SEL),
  402. },
  403. {
  404. DA9063_BUCK(DA9063, BCORE2, 300, 10, 1570,
  405. da9063_buck_a_limits,
  406. DA9063_REG_BUCK_ILIM_C, DA9063_BCORE2_ILIM_MASK),
  407. DA9063_BUCK_COMMON_FIELDS(BCORE2),
  408. .suspend = BFIELD(DA9063_REG_DVC_1, DA9063_VBCORE2_SEL),
  409. },
  410. {
  411. DA9063_BUCK(DA9063, BPRO, 530, 10, 1800,
  412. da9063_buck_a_limits,
  413. DA9063_REG_BUCK_ILIM_B, DA9063_BPRO_ILIM_MASK),
  414. DA9063_BUCK_COMMON_FIELDS(BPRO),
  415. .suspend = BFIELD(DA9063_REG_DVC_1, DA9063_VBPRO_SEL),
  416. },
  417. {
  418. DA9063_BUCK(DA9063, BMEM, 800, 20, 3340,
  419. da9063_buck_b_limits,
  420. DA9063_REG_BUCK_ILIM_A, DA9063_BMEM_ILIM_MASK),
  421. DA9063_BUCK_COMMON_FIELDS(BMEM),
  422. .suspend = BFIELD(DA9063_REG_DVC_1, DA9063_VBMEM_SEL),
  423. },
  424. {
  425. DA9063_BUCK(DA9063, BIO, 800, 20, 3340,
  426. da9063_buck_b_limits,
  427. DA9063_REG_BUCK_ILIM_A, DA9063_BIO_ILIM_MASK),
  428. DA9063_BUCK_COMMON_FIELDS(BIO),
  429. .suspend = BFIELD(DA9063_REG_DVC_2, DA9063_VBIO_SEL),
  430. },
  431. {
  432. DA9063_BUCK(DA9063, BPERI, 800, 20, 3340,
  433. da9063_buck_b_limits,
  434. DA9063_REG_BUCK_ILIM_B, DA9063_BPERI_ILIM_MASK),
  435. DA9063_BUCK_COMMON_FIELDS(BPERI),
  436. .suspend = BFIELD(DA9063_REG_DVC_1, DA9063_VBPERI_SEL),
  437. },
  438. {
  439. DA9063_BUCK(DA9063, BCORES_MERGED, 300, 10, 1570,
  440. da9063_bcores_merged_limits,
  441. DA9063_REG_BUCK_ILIM_C, DA9063_BCORE1_ILIM_MASK),
  442. /* BCORES_MERGED uses the same register fields as BCORE1 */
  443. DA9063_BUCK_COMMON_FIELDS(BCORE1),
  444. .suspend = BFIELD(DA9063_REG_DVC_1, DA9063_VBCORE1_SEL),
  445. },
  446. {
  447. DA9063_BUCK(DA9063, BMEM_BIO_MERGED, 800, 20, 3340,
  448. da9063_bmem_bio_merged_limits,
  449. DA9063_REG_BUCK_ILIM_A, DA9063_BMEM_ILIM_MASK),
  450. /* BMEM_BIO_MERGED uses the same register fields as BMEM */
  451. DA9063_BUCK_COMMON_FIELDS(BMEM),
  452. .suspend = BFIELD(DA9063_REG_DVC_1, DA9063_VBMEM_SEL),
  453. },
  454. {
  455. DA9063_LDO(DA9063, LDO3, 900, 20, 3440),
  456. .suspend = BFIELD(DA9063_REG_DVC_1, DA9063_VLDO3_SEL),
  457. .oc_event = BFIELD(DA9063_REG_STATUS_D, DA9063_LDO3_LIM),
  458. },
  459. {
  460. DA9063_LDO(DA9063, LDO7, 900, 50, 3600),
  461. .suspend = BFIELD(DA9063_REG_LDO7_CONT, DA9063_VLDO7_SEL),
  462. .oc_event = BFIELD(DA9063_REG_STATUS_D, DA9063_LDO7_LIM),
  463. },
  464. {
  465. DA9063_LDO(DA9063, LDO8, 900, 50, 3600),
  466. .suspend = BFIELD(DA9063_REG_LDO8_CONT, DA9063_VLDO8_SEL),
  467. .oc_event = BFIELD(DA9063_REG_STATUS_D, DA9063_LDO8_LIM),
  468. },
  469. {
  470. DA9063_LDO(DA9063, LDO9, 950, 50, 3600),
  471. .suspend = BFIELD(DA9063_REG_LDO9_CONT, DA9063_VLDO9_SEL),
  472. },
  473. {
  474. DA9063_LDO(DA9063, LDO11, 900, 50, 3600),
  475. .suspend = BFIELD(DA9063_REG_LDO11_CONT, DA9063_VLDO11_SEL),
  476. .oc_event = BFIELD(DA9063_REG_STATUS_D, DA9063_LDO11_LIM),
  477. },
  478. /* The following LDOs are present only on DA9063, not on DA9063L */
  479. {
  480. DA9063_LDO(DA9063, LDO1, 600, 20, 1860),
  481. .suspend = BFIELD(DA9063_REG_DVC_1, DA9063_VLDO1_SEL),
  482. },
  483. {
  484. DA9063_LDO(DA9063, LDO2, 600, 20, 1860),
  485. .suspend = BFIELD(DA9063_REG_DVC_1, DA9063_VLDO2_SEL),
  486. },
  487. {
  488. DA9063_LDO(DA9063, LDO4, 900, 20, 3440),
  489. .suspend = BFIELD(DA9063_REG_DVC_2, DA9063_VLDO4_SEL),
  490. .oc_event = BFIELD(DA9063_REG_STATUS_D, DA9063_LDO4_LIM),
  491. },
  492. {
  493. DA9063_LDO(DA9063, LDO5, 900, 50, 3600),
  494. .suspend = BFIELD(DA9063_REG_LDO5_CONT, DA9063_VLDO5_SEL),
  495. },
  496. {
  497. DA9063_LDO(DA9063, LDO6, 900, 50, 3600),
  498. .suspend = BFIELD(DA9063_REG_LDO6_CONT, DA9063_VLDO6_SEL),
  499. },
  500. {
  501. DA9063_LDO(DA9063, LDO10, 900, 50, 3600),
  502. .suspend = BFIELD(DA9063_REG_LDO10_CONT, DA9063_VLDO10_SEL),
  503. },
  504. };
  505. /* Link chip model with regulators info table */
  506. static struct da9063_dev_model regulators_models[] = {
  507. {
  508. .regulator_info = da9063_regulator_info,
  509. .n_regulators = ARRAY_SIZE(da9063_regulator_info),
  510. .type = PMIC_TYPE_DA9063,
  511. },
  512. {
  513. .regulator_info = da9063_regulator_info,
  514. .n_regulators = ARRAY_SIZE(da9063_regulator_info) - 6,
  515. .type = PMIC_TYPE_DA9063L,
  516. },
  517. { }
  518. };
  519. /* Regulator interrupt handlers */
  520. static irqreturn_t da9063_ldo_lim_event(int irq, void *data)
  521. {
  522. struct da9063_regulators *regulators = data;
  523. struct da9063 *hw = regulators->regulator[0].hw;
  524. struct da9063_regulator *regl;
  525. int bits, i , ret;
  526. ret = regmap_read(hw->regmap, DA9063_REG_STATUS_D, &bits);
  527. if (ret < 0)
  528. return IRQ_NONE;
  529. for (i = regulators->n_regulators - 1; i >= 0; i--) {
  530. regl = &regulators->regulator[i];
  531. if (regl->info->oc_event.reg != DA9063_REG_STATUS_D)
  532. continue;
  533. if (BIT(regl->info->oc_event.lsb) & bits) {
  534. regulator_lock(regl->rdev);
  535. regulator_notifier_call_chain(regl->rdev,
  536. REGULATOR_EVENT_OVER_CURRENT, NULL);
  537. regulator_unlock(regl->rdev);
  538. }
  539. }
  540. return IRQ_HANDLED;
  541. }
  542. /*
  543. * Probing and Initialisation functions
  544. */
  545. static const struct regulator_init_data *da9063_get_regulator_initdata(
  546. const struct da9063_regulators_pdata *regl_pdata, int id)
  547. {
  548. int i;
  549. for (i = 0; i < regl_pdata->n_regulators; i++) {
  550. if (id == regl_pdata->regulator_data[i].id)
  551. return regl_pdata->regulator_data[i].initdata;
  552. }
  553. return NULL;
  554. }
  555. static struct of_regulator_match da9063_matches[] = {
  556. [DA9063_ID_BCORE1] = { .name = "bcore1" },
  557. [DA9063_ID_BCORE2] = { .name = "bcore2" },
  558. [DA9063_ID_BPRO] = { .name = "bpro", },
  559. [DA9063_ID_BMEM] = { .name = "bmem", },
  560. [DA9063_ID_BIO] = { .name = "bio", },
  561. [DA9063_ID_BPERI] = { .name = "bperi", },
  562. [DA9063_ID_BCORES_MERGED] = { .name = "bcores-merged" },
  563. [DA9063_ID_BMEM_BIO_MERGED] = { .name = "bmem-bio-merged", },
  564. [DA9063_ID_LDO3] = { .name = "ldo3", },
  565. [DA9063_ID_LDO7] = { .name = "ldo7", },
  566. [DA9063_ID_LDO8] = { .name = "ldo8", },
  567. [DA9063_ID_LDO9] = { .name = "ldo9", },
  568. [DA9063_ID_LDO11] = { .name = "ldo11", },
  569. /* The following LDOs are present only on DA9063, not on DA9063L */
  570. [DA9063_ID_LDO1] = { .name = "ldo1", },
  571. [DA9063_ID_LDO2] = { .name = "ldo2", },
  572. [DA9063_ID_LDO4] = { .name = "ldo4", },
  573. [DA9063_ID_LDO5] = { .name = "ldo5", },
  574. [DA9063_ID_LDO6] = { .name = "ldo6", },
  575. [DA9063_ID_LDO10] = { .name = "ldo10", },
  576. };
  577. static struct da9063_regulators_pdata *da9063_parse_regulators_dt(
  578. struct platform_device *pdev,
  579. struct of_regulator_match **da9063_reg_matches)
  580. {
  581. struct da9063 *da9063 = dev_get_drvdata(pdev->dev.parent);
  582. struct da9063_regulators_pdata *pdata;
  583. struct da9063_regulator_data *rdata;
  584. struct device_node *node;
  585. int da9063_matches_len = ARRAY_SIZE(da9063_matches);
  586. int i, n, num;
  587. if (da9063->type == PMIC_TYPE_DA9063L)
  588. da9063_matches_len -= 6;
  589. node = of_get_child_by_name(pdev->dev.parent->of_node, "regulators");
  590. if (!node) {
  591. dev_err(&pdev->dev, "Regulators device node not found\n");
  592. return ERR_PTR(-ENODEV);
  593. }
  594. num = of_regulator_match(&pdev->dev, node, da9063_matches,
  595. da9063_matches_len);
  596. of_node_put(node);
  597. if (num < 0) {
  598. dev_err(&pdev->dev, "Failed to match regulators\n");
  599. return ERR_PTR(-EINVAL);
  600. }
  601. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  602. if (!pdata)
  603. return ERR_PTR(-ENOMEM);
  604. pdata->regulator_data = devm_kcalloc(&pdev->dev,
  605. num, sizeof(*pdata->regulator_data),
  606. GFP_KERNEL);
  607. if (!pdata->regulator_data)
  608. return ERR_PTR(-ENOMEM);
  609. pdata->n_regulators = num;
  610. n = 0;
  611. for (i = 0; i < da9063_matches_len; i++) {
  612. if (!da9063_matches[i].init_data)
  613. continue;
  614. rdata = &pdata->regulator_data[n];
  615. rdata->id = i;
  616. rdata->initdata = da9063_matches[i].init_data;
  617. n++;
  618. }
  619. *da9063_reg_matches = da9063_matches;
  620. return pdata;
  621. }
  622. static int da9063_regulator_probe(struct platform_device *pdev)
  623. {
  624. struct da9063 *da9063 = dev_get_drvdata(pdev->dev.parent);
  625. struct of_regulator_match *da9063_reg_matches = NULL;
  626. struct da9063_regulators_pdata *regl_pdata;
  627. const struct da9063_dev_model *model;
  628. struct da9063_regulators *regulators;
  629. struct da9063_regulator *regl;
  630. struct regulator_config config;
  631. bool bcores_merged, bmem_bio_merged;
  632. int id, irq, n, n_regulators, ret, val;
  633. regl_pdata = da9063_parse_regulators_dt(pdev, &da9063_reg_matches);
  634. if (IS_ERR(regl_pdata) || regl_pdata->n_regulators == 0) {
  635. dev_err(&pdev->dev,
  636. "No regulators defined for the platform\n");
  637. return -ENODEV;
  638. }
  639. /* Find regulators set for particular device model */
  640. for (model = regulators_models; model->regulator_info; model++) {
  641. if (model->type == da9063->type)
  642. break;
  643. }
  644. if (!model->regulator_info) {
  645. dev_err(&pdev->dev, "Chip model not recognised (%u)\n",
  646. da9063->type);
  647. return -ENODEV;
  648. }
  649. ret = regmap_read(da9063->regmap, DA9063_REG_CONFIG_H, &val);
  650. if (ret < 0) {
  651. dev_err(&pdev->dev,
  652. "Error while reading BUCKs configuration\n");
  653. return ret;
  654. }
  655. bcores_merged = val & DA9063_BCORE_MERGE;
  656. bmem_bio_merged = val & DA9063_BUCK_MERGE;
  657. n_regulators = model->n_regulators;
  658. if (bcores_merged)
  659. n_regulators -= 2; /* remove BCORE1, BCORE2 */
  660. else
  661. n_regulators--; /* remove BCORES_MERGED */
  662. if (bmem_bio_merged)
  663. n_regulators -= 2; /* remove BMEM, BIO */
  664. else
  665. n_regulators--; /* remove BMEM_BIO_MERGED */
  666. /* Allocate memory required by usable regulators */
  667. regulators = devm_kzalloc(&pdev->dev, struct_size(regulators,
  668. regulator, n_regulators), GFP_KERNEL);
  669. if (!regulators)
  670. return -ENOMEM;
  671. regulators->n_regulators = n_regulators;
  672. platform_set_drvdata(pdev, regulators);
  673. /* Register all regulators declared in platform information */
  674. n = 0;
  675. id = 0;
  676. while (n < regulators->n_regulators) {
  677. /* Skip regulator IDs depending on merge mode configuration */
  678. switch (id) {
  679. case DA9063_ID_BCORE1:
  680. case DA9063_ID_BCORE2:
  681. if (bcores_merged) {
  682. id++;
  683. continue;
  684. }
  685. break;
  686. case DA9063_ID_BMEM:
  687. case DA9063_ID_BIO:
  688. if (bmem_bio_merged) {
  689. id++;
  690. continue;
  691. }
  692. break;
  693. case DA9063_ID_BCORES_MERGED:
  694. if (!bcores_merged) {
  695. id++;
  696. continue;
  697. }
  698. break;
  699. case DA9063_ID_BMEM_BIO_MERGED:
  700. if (!bmem_bio_merged) {
  701. id++;
  702. continue;
  703. }
  704. break;
  705. }
  706. /* Initialise regulator structure */
  707. regl = &regulators->regulator[n];
  708. regl->hw = da9063;
  709. regl->info = &model->regulator_info[id];
  710. regl->desc = regl->info->desc;
  711. regl->desc.type = REGULATOR_VOLTAGE;
  712. regl->desc.owner = THIS_MODULE;
  713. if (regl->info->mode.reg) {
  714. regl->mode = devm_regmap_field_alloc(&pdev->dev,
  715. da9063->regmap, regl->info->mode);
  716. if (IS_ERR(regl->mode))
  717. return PTR_ERR(regl->mode);
  718. }
  719. if (regl->info->suspend.reg) {
  720. regl->suspend = devm_regmap_field_alloc(&pdev->dev,
  721. da9063->regmap, regl->info->suspend);
  722. if (IS_ERR(regl->suspend))
  723. return PTR_ERR(regl->suspend);
  724. }
  725. if (regl->info->sleep.reg) {
  726. regl->sleep = devm_regmap_field_alloc(&pdev->dev,
  727. da9063->regmap, regl->info->sleep);
  728. if (IS_ERR(regl->sleep))
  729. return PTR_ERR(regl->sleep);
  730. }
  731. if (regl->info->suspend_sleep.reg) {
  732. regl->suspend_sleep = devm_regmap_field_alloc(&pdev->dev,
  733. da9063->regmap, regl->info->suspend_sleep);
  734. if (IS_ERR(regl->suspend_sleep))
  735. return PTR_ERR(regl->suspend_sleep);
  736. }
  737. /* Register regulator */
  738. memset(&config, 0, sizeof(config));
  739. config.dev = &pdev->dev;
  740. config.init_data = da9063_get_regulator_initdata(regl_pdata, id);
  741. config.driver_data = regl;
  742. if (da9063_reg_matches)
  743. config.of_node = da9063_reg_matches[id].of_node;
  744. config.regmap = da9063->regmap;
  745. regl->rdev = devm_regulator_register(&pdev->dev, &regl->desc,
  746. &config);
  747. if (IS_ERR(regl->rdev)) {
  748. dev_err(&pdev->dev,
  749. "Failed to register %s regulator\n",
  750. regl->desc.name);
  751. return PTR_ERR(regl->rdev);
  752. }
  753. id++;
  754. n++;
  755. }
  756. /* LDOs overcurrent event support */
  757. irq = platform_get_irq_byname(pdev, "LDO_LIM");
  758. if (irq < 0)
  759. return irq;
  760. ret = devm_request_threaded_irq(&pdev->dev, irq,
  761. NULL, da9063_ldo_lim_event,
  762. IRQF_TRIGGER_LOW | IRQF_ONESHOT,
  763. "LDO_LIM", regulators);
  764. if (ret) {
  765. dev_err(&pdev->dev, "Failed to request LDO_LIM IRQ.\n");
  766. return ret;
  767. }
  768. return 0;
  769. }
  770. static struct platform_driver da9063_regulator_driver = {
  771. .driver = {
  772. .name = DA9063_DRVNAME_REGULATORS,
  773. },
  774. .probe = da9063_regulator_probe,
  775. };
  776. static int __init da9063_regulator_init(void)
  777. {
  778. return platform_driver_register(&da9063_regulator_driver);
  779. }
  780. subsys_initcall(da9063_regulator_init);
  781. static void __exit da9063_regulator_cleanup(void)
  782. {
  783. platform_driver_unregister(&da9063_regulator_driver);
  784. }
  785. module_exit(da9063_regulator_cleanup);
  786. /* Module information */
  787. MODULE_AUTHOR("Krystian Garbaciak <krystian.garbaciak@diasemi.com>");
  788. MODULE_DESCRIPTION("DA9063 regulators driver");
  789. MODULE_LICENSE("GPL");
  790. MODULE_ALIAS("platform:" DA9063_DRVNAME_REGULATORS);