da9063-regulator.c 25 KB

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