da9062-regulator.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  1. /*
  2. * da9062-regulator.c - REGULATOR device driver for DA9062
  3. * Copyright (C) 2015 Dialog Semiconductor Ltd.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  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/da9062/core.h>
  27. #include <linux/mfd/da9062/registers.h>
  28. /* Regulator IDs */
  29. enum {
  30. DA9062_ID_BUCK1,
  31. DA9062_ID_BUCK2,
  32. DA9062_ID_BUCK3,
  33. DA9062_ID_BUCK4,
  34. DA9062_ID_LDO1,
  35. DA9062_ID_LDO2,
  36. DA9062_ID_LDO3,
  37. DA9062_ID_LDO4,
  38. DA9062_MAX_REGULATORS,
  39. };
  40. /* Regulator capabilities and registers description */
  41. struct da9062_regulator_info {
  42. struct regulator_desc desc;
  43. /* Current limiting */
  44. unsigned int n_current_limits;
  45. const int *current_limits;
  46. /* Main register fields */
  47. struct reg_field mode;
  48. struct reg_field suspend;
  49. struct reg_field sleep;
  50. struct reg_field suspend_sleep;
  51. unsigned int suspend_vsel_reg;
  52. struct reg_field ilimit;
  53. /* Event detection bit */
  54. struct reg_field oc_event;
  55. };
  56. /* Single regulator settings */
  57. struct da9062_regulator {
  58. struct regulator_desc desc;
  59. struct regulator_dev *rdev;
  60. struct da9062 *hw;
  61. const struct da9062_regulator_info *info;
  62. struct regmap_field *mode;
  63. struct regmap_field *suspend;
  64. struct regmap_field *sleep;
  65. struct regmap_field *suspend_sleep;
  66. struct regmap_field *ilimit;
  67. };
  68. /* Encapsulates all information for the regulators driver */
  69. struct da9062_regulators {
  70. int irq_ldo_lim;
  71. unsigned n_regulators;
  72. /* Array size to be defined during init. Keep at end. */
  73. struct da9062_regulator regulator[0];
  74. };
  75. /* BUCK modes */
  76. enum {
  77. BUCK_MODE_MANUAL, /* 0 */
  78. BUCK_MODE_SLEEP, /* 1 */
  79. BUCK_MODE_SYNC, /* 2 */
  80. BUCK_MODE_AUTO /* 3 */
  81. };
  82. /* Regulator operations */
  83. /* Current limits array (in uA) BUCK1 and BUCK3.
  84. Entry indexes corresponds to register values. */
  85. static const int da9062_buck_a_limits[] = {
  86. 500000, 600000, 700000, 800000, 900000, 1000000, 1100000, 1200000,
  87. 1300000, 1400000, 1500000, 1600000, 1700000, 1800000, 1900000, 2000000
  88. };
  89. /* Current limits array (in uA) for BUCK2.
  90. Entry indexes corresponds to register values. */
  91. static const int da9062_buck_b_limits[] = {
  92. 1500000, 1600000, 1700000, 1800000, 1900000, 2000000, 2100000, 2200000,
  93. 2300000, 2400000, 2500000, 2600000, 2700000, 2800000, 2900000, 3000000
  94. };
  95. static int da9062_set_current_limit(struct regulator_dev *rdev,
  96. int min_ua, int max_ua)
  97. {
  98. struct da9062_regulator *regl = rdev_get_drvdata(rdev);
  99. const struct da9062_regulator_info *rinfo = regl->info;
  100. int n, tval;
  101. for (n = 0; n < rinfo->n_current_limits; n++) {
  102. tval = rinfo->current_limits[n];
  103. if (tval >= min_ua && tval <= max_ua)
  104. return regmap_field_write(regl->ilimit, n);
  105. }
  106. return -EINVAL;
  107. }
  108. static int da9062_get_current_limit(struct regulator_dev *rdev)
  109. {
  110. struct da9062_regulator *regl = rdev_get_drvdata(rdev);
  111. const struct da9062_regulator_info *rinfo = regl->info;
  112. unsigned int sel;
  113. int ret;
  114. ret = regmap_field_read(regl->ilimit, &sel);
  115. if (ret < 0)
  116. return ret;
  117. if (sel >= rinfo->n_current_limits)
  118. sel = rinfo->n_current_limits - 1;
  119. return rinfo->current_limits[sel];
  120. }
  121. static int da9062_buck_set_mode(struct regulator_dev *rdev, unsigned mode)
  122. {
  123. struct da9062_regulator *regl = rdev_get_drvdata(rdev);
  124. unsigned val;
  125. switch (mode) {
  126. case REGULATOR_MODE_FAST:
  127. val = BUCK_MODE_SYNC;
  128. break;
  129. case REGULATOR_MODE_NORMAL:
  130. val = BUCK_MODE_AUTO;
  131. break;
  132. case REGULATOR_MODE_STANDBY:
  133. val = BUCK_MODE_SLEEP;
  134. break;
  135. default:
  136. return -EINVAL;
  137. }
  138. return regmap_field_write(regl->mode, val);
  139. }
  140. /*
  141. * Bucks use single mode register field for normal operation
  142. * and suspend state.
  143. * There are 3 modes to map to: FAST, NORMAL, and STANDBY.
  144. */
  145. static unsigned da9062_buck_get_mode(struct regulator_dev *rdev)
  146. {
  147. struct da9062_regulator *regl = rdev_get_drvdata(rdev);
  148. struct regmap_field *field;
  149. unsigned int val, mode = 0;
  150. int ret;
  151. ret = regmap_field_read(regl->mode, &val);
  152. if (ret < 0)
  153. return ret;
  154. switch (val) {
  155. default:
  156. case BUCK_MODE_MANUAL:
  157. mode = REGULATOR_MODE_FAST | REGULATOR_MODE_STANDBY;
  158. /* Sleep flag bit decides the mode */
  159. break;
  160. case BUCK_MODE_SLEEP:
  161. return REGULATOR_MODE_STANDBY;
  162. case BUCK_MODE_SYNC:
  163. return REGULATOR_MODE_FAST;
  164. case BUCK_MODE_AUTO:
  165. return REGULATOR_MODE_NORMAL;
  166. }
  167. /* Detect current regulator state */
  168. ret = regmap_field_read(regl->suspend, &val);
  169. if (ret < 0)
  170. return 0;
  171. /* Read regulator mode from proper register, depending on state */
  172. if (val)
  173. field = regl->suspend_sleep;
  174. else
  175. field = regl->sleep;
  176. ret = regmap_field_read(field, &val);
  177. if (ret < 0)
  178. return 0;
  179. if (val)
  180. mode &= REGULATOR_MODE_STANDBY;
  181. else
  182. mode &= REGULATOR_MODE_NORMAL | REGULATOR_MODE_FAST;
  183. return mode;
  184. }
  185. /*
  186. * LDOs use sleep flags - one for normal and one for suspend state.
  187. * There are 2 modes to map to: NORMAL and STANDBY (sleep) for each state.
  188. */
  189. static int da9062_ldo_set_mode(struct regulator_dev *rdev, unsigned mode)
  190. {
  191. struct da9062_regulator *regl = rdev_get_drvdata(rdev);
  192. unsigned val;
  193. switch (mode) {
  194. case REGULATOR_MODE_NORMAL:
  195. val = 0;
  196. break;
  197. case REGULATOR_MODE_STANDBY:
  198. val = 1;
  199. break;
  200. default:
  201. return -EINVAL;
  202. }
  203. return regmap_field_write(regl->sleep, val);
  204. }
  205. static unsigned da9062_ldo_get_mode(struct regulator_dev *rdev)
  206. {
  207. struct da9062_regulator *regl = rdev_get_drvdata(rdev);
  208. struct regmap_field *field;
  209. int ret, val;
  210. /* Detect current regulator state */
  211. ret = regmap_field_read(regl->suspend, &val);
  212. if (ret < 0)
  213. return 0;
  214. /* Read regulator mode from proper register, depending on state */
  215. if (val)
  216. field = regl->suspend_sleep;
  217. else
  218. field = regl->sleep;
  219. ret = regmap_field_read(field, &val);
  220. if (ret < 0)
  221. return 0;
  222. if (val)
  223. return REGULATOR_MODE_STANDBY;
  224. else
  225. return REGULATOR_MODE_NORMAL;
  226. }
  227. static int da9062_buck_get_status(struct regulator_dev *rdev)
  228. {
  229. int ret = regulator_is_enabled_regmap(rdev);
  230. if (ret == 0) {
  231. ret = REGULATOR_STATUS_OFF;
  232. } else if (ret > 0) {
  233. ret = da9062_buck_get_mode(rdev);
  234. if (ret > 0)
  235. ret = regulator_mode_to_status(ret);
  236. else if (ret == 0)
  237. ret = -EIO;
  238. }
  239. return ret;
  240. }
  241. static int da9062_ldo_get_status(struct regulator_dev *rdev)
  242. {
  243. int ret = regulator_is_enabled_regmap(rdev);
  244. if (ret == 0) {
  245. ret = REGULATOR_STATUS_OFF;
  246. } else if (ret > 0) {
  247. ret = da9062_ldo_get_mode(rdev);
  248. if (ret > 0)
  249. ret = regulator_mode_to_status(ret);
  250. else if (ret == 0)
  251. ret = -EIO;
  252. }
  253. return ret;
  254. }
  255. static int da9062_set_suspend_voltage(struct regulator_dev *rdev, int uv)
  256. {
  257. struct da9062_regulator *regl = rdev_get_drvdata(rdev);
  258. const struct da9062_regulator_info *rinfo = regl->info;
  259. int ret, sel;
  260. sel = regulator_map_voltage_linear(rdev, uv, uv);
  261. if (sel < 0)
  262. return sel;
  263. sel <<= ffs(rdev->desc->vsel_mask) - 1;
  264. ret = regmap_update_bits(regl->hw->regmap, rinfo->suspend_vsel_reg,
  265. rdev->desc->vsel_mask, sel);
  266. return ret;
  267. }
  268. static int da9062_suspend_enable(struct regulator_dev *rdev)
  269. {
  270. struct da9062_regulator *regl = rdev_get_drvdata(rdev);
  271. return regmap_field_write(regl->suspend, 1);
  272. }
  273. static int da9062_suspend_disable(struct regulator_dev *rdev)
  274. {
  275. struct da9062_regulator *regl = rdev_get_drvdata(rdev);
  276. return regmap_field_write(regl->suspend, 0);
  277. }
  278. static int da9062_buck_set_suspend_mode(struct regulator_dev *rdev,
  279. unsigned mode)
  280. {
  281. struct da9062_regulator *regl = rdev_get_drvdata(rdev);
  282. int val;
  283. switch (mode) {
  284. case REGULATOR_MODE_FAST:
  285. val = BUCK_MODE_SYNC;
  286. break;
  287. case REGULATOR_MODE_NORMAL:
  288. val = BUCK_MODE_AUTO;
  289. break;
  290. case REGULATOR_MODE_STANDBY:
  291. val = BUCK_MODE_SLEEP;
  292. break;
  293. default:
  294. return -EINVAL;
  295. }
  296. return regmap_field_write(regl->mode, val);
  297. }
  298. static int da9062_ldo_set_suspend_mode(struct regulator_dev *rdev,
  299. unsigned mode)
  300. {
  301. struct da9062_regulator *regl = rdev_get_drvdata(rdev);
  302. unsigned val;
  303. switch (mode) {
  304. case REGULATOR_MODE_NORMAL:
  305. val = 0;
  306. break;
  307. case REGULATOR_MODE_STANDBY:
  308. val = 1;
  309. break;
  310. default:
  311. return -EINVAL;
  312. }
  313. return regmap_field_write(regl->suspend_sleep, val);
  314. }
  315. static const struct regulator_ops da9062_buck_ops = {
  316. .enable = regulator_enable_regmap,
  317. .disable = regulator_disable_regmap,
  318. .is_enabled = regulator_is_enabled_regmap,
  319. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  320. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  321. .list_voltage = regulator_list_voltage_linear,
  322. .set_current_limit = da9062_set_current_limit,
  323. .get_current_limit = da9062_get_current_limit,
  324. .set_mode = da9062_buck_set_mode,
  325. .get_mode = da9062_buck_get_mode,
  326. .get_status = da9062_buck_get_status,
  327. .set_suspend_voltage = da9062_set_suspend_voltage,
  328. .set_suspend_enable = da9062_suspend_enable,
  329. .set_suspend_disable = da9062_suspend_disable,
  330. .set_suspend_mode = da9062_buck_set_suspend_mode,
  331. };
  332. static const struct regulator_ops da9062_ldo_ops = {
  333. .enable = regulator_enable_regmap,
  334. .disable = regulator_disable_regmap,
  335. .is_enabled = regulator_is_enabled_regmap,
  336. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  337. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  338. .list_voltage = regulator_list_voltage_linear,
  339. .set_mode = da9062_ldo_set_mode,
  340. .get_mode = da9062_ldo_get_mode,
  341. .get_status = da9062_ldo_get_status,
  342. .set_suspend_voltage = da9062_set_suspend_voltage,
  343. .set_suspend_enable = da9062_suspend_enable,
  344. .set_suspend_disable = da9062_suspend_disable,
  345. .set_suspend_mode = da9062_ldo_set_suspend_mode,
  346. };
  347. /* Regulator information */
  348. static const struct da9062_regulator_info local_regulator_info[] = {
  349. {
  350. .desc.id = DA9062_ID_BUCK1,
  351. .desc.name = "DA9062 BUCK1",
  352. .desc.of_match = of_match_ptr("buck1"),
  353. .desc.regulators_node = of_match_ptr("regulators"),
  354. .desc.ops = &da9062_buck_ops,
  355. .desc.min_uV = (300) * 1000,
  356. .desc.uV_step = (10) * 1000,
  357. .desc.n_voltages = ((1570) - (300))/(10) + 1,
  358. .current_limits = da9062_buck_a_limits,
  359. .n_current_limits = ARRAY_SIZE(da9062_buck_a_limits),
  360. .desc.enable_reg = DA9062AA_BUCK1_CONT,
  361. .desc.enable_mask = DA9062AA_BUCK1_EN_MASK,
  362. .desc.vsel_reg = DA9062AA_VBUCK1_A,
  363. .desc.vsel_mask = DA9062AA_VBUCK1_A_MASK,
  364. .desc.linear_min_sel = 0,
  365. .sleep = REG_FIELD(DA9062AA_VBUCK1_A,
  366. __builtin_ffs((int)DA9062AA_BUCK1_SL_A_MASK) - 1,
  367. sizeof(unsigned int) * 8 -
  368. __builtin_clz((DA9062AA_BUCK1_SL_A_MASK)) - 1),
  369. .suspend_sleep = REG_FIELD(DA9062AA_VBUCK1_B,
  370. __builtin_ffs((int)DA9062AA_BUCK1_SL_B_MASK) - 1,
  371. sizeof(unsigned int) * 8 -
  372. __builtin_clz((DA9062AA_BUCK1_SL_B_MASK)) - 1),
  373. .suspend_vsel_reg = DA9062AA_VBUCK1_B,
  374. .mode = REG_FIELD(DA9062AA_BUCK1_CFG,
  375. __builtin_ffs((int)DA9062AA_BUCK1_MODE_MASK) - 1,
  376. sizeof(unsigned int) * 8 -
  377. __builtin_clz((DA9062AA_BUCK1_MODE_MASK)) - 1),
  378. .suspend = REG_FIELD(DA9062AA_DVC_1,
  379. __builtin_ffs((int)DA9062AA_VBUCK1_SEL_MASK) - 1,
  380. sizeof(unsigned int) * 8 -
  381. __builtin_clz((DA9062AA_VBUCK1_SEL_MASK)) - 1),
  382. .ilimit = REG_FIELD(DA9062AA_BUCK_ILIM_C,
  383. __builtin_ffs((int)DA9062AA_BUCK1_ILIM_MASK) - 1,
  384. sizeof(unsigned int) * 8 -
  385. __builtin_clz((DA9062AA_BUCK1_ILIM_MASK)) - 1),
  386. },
  387. {
  388. .desc.id = DA9062_ID_BUCK2,
  389. .desc.name = "DA9062 BUCK2",
  390. .desc.of_match = of_match_ptr("buck2"),
  391. .desc.regulators_node = of_match_ptr("regulators"),
  392. .desc.ops = &da9062_buck_ops,
  393. .desc.min_uV = (300) * 1000,
  394. .desc.uV_step = (10) * 1000,
  395. .desc.n_voltages = ((1570) - (300))/(10) + 1,
  396. .current_limits = da9062_buck_a_limits,
  397. .n_current_limits = ARRAY_SIZE(da9062_buck_a_limits),
  398. .desc.enable_reg = DA9062AA_BUCK2_CONT,
  399. .desc.enable_mask = DA9062AA_BUCK2_EN_MASK,
  400. .desc.vsel_reg = DA9062AA_VBUCK2_A,
  401. .desc.vsel_mask = DA9062AA_VBUCK2_A_MASK,
  402. .desc.linear_min_sel = 0,
  403. .sleep = REG_FIELD(DA9062AA_VBUCK2_A,
  404. __builtin_ffs((int)DA9062AA_BUCK2_SL_A_MASK) - 1,
  405. sizeof(unsigned int) * 8 -
  406. __builtin_clz((DA9062AA_BUCK2_SL_A_MASK)) - 1),
  407. .suspend_sleep = REG_FIELD(DA9062AA_VBUCK2_B,
  408. __builtin_ffs((int)DA9062AA_BUCK2_SL_B_MASK) - 1,
  409. sizeof(unsigned int) * 8 -
  410. __builtin_clz((DA9062AA_BUCK2_SL_B_MASK)) - 1),
  411. .suspend_vsel_reg = DA9062AA_VBUCK2_B,
  412. .mode = REG_FIELD(DA9062AA_BUCK2_CFG,
  413. __builtin_ffs((int)DA9062AA_BUCK2_MODE_MASK) - 1,
  414. sizeof(unsigned int) * 8 -
  415. __builtin_clz((DA9062AA_BUCK2_MODE_MASK)) - 1),
  416. .suspend = REG_FIELD(DA9062AA_DVC_1,
  417. __builtin_ffs((int)DA9062AA_VBUCK2_SEL_MASK) - 1,
  418. sizeof(unsigned int) * 8 -
  419. __builtin_clz((DA9062AA_VBUCK2_SEL_MASK)) - 1),
  420. .ilimit = REG_FIELD(DA9062AA_BUCK_ILIM_C,
  421. __builtin_ffs((int)DA9062AA_BUCK2_ILIM_MASK) - 1,
  422. sizeof(unsigned int) * 8 -
  423. __builtin_clz((DA9062AA_BUCK2_ILIM_MASK)) - 1),
  424. },
  425. {
  426. .desc.id = DA9062_ID_BUCK3,
  427. .desc.name = "DA9062 BUCK3",
  428. .desc.of_match = of_match_ptr("buck3"),
  429. .desc.regulators_node = of_match_ptr("regulators"),
  430. .desc.ops = &da9062_buck_ops,
  431. .desc.min_uV = (800) * 1000,
  432. .desc.uV_step = (20) * 1000,
  433. .desc.n_voltages = ((3340) - (800))/(20) + 1,
  434. .current_limits = da9062_buck_b_limits,
  435. .n_current_limits = ARRAY_SIZE(da9062_buck_b_limits),
  436. .desc.enable_reg = DA9062AA_BUCK3_CONT,
  437. .desc.enable_mask = DA9062AA_BUCK3_EN_MASK,
  438. .desc.vsel_reg = DA9062AA_VBUCK3_A,
  439. .desc.vsel_mask = DA9062AA_VBUCK3_A_MASK,
  440. .desc.linear_min_sel = 0,
  441. .sleep = REG_FIELD(DA9062AA_VBUCK3_A,
  442. __builtin_ffs((int)DA9062AA_BUCK3_SL_A_MASK) - 1,
  443. sizeof(unsigned int) * 8 -
  444. __builtin_clz((DA9062AA_BUCK3_SL_A_MASK)) - 1),
  445. .suspend_sleep = REG_FIELD(DA9062AA_VBUCK3_B,
  446. __builtin_ffs((int)DA9062AA_BUCK3_SL_B_MASK) - 1,
  447. sizeof(unsigned int) * 8 -
  448. __builtin_clz((DA9062AA_BUCK3_SL_B_MASK)) - 1),
  449. .suspend_vsel_reg = DA9062AA_VBUCK3_B,
  450. .mode = REG_FIELD(DA9062AA_BUCK3_CFG,
  451. __builtin_ffs((int)DA9062AA_BUCK3_MODE_MASK) - 1,
  452. sizeof(unsigned int) * 8 -
  453. __builtin_clz((DA9062AA_BUCK3_MODE_MASK)) - 1),
  454. .suspend = REG_FIELD(DA9062AA_DVC_1,
  455. __builtin_ffs((int)DA9062AA_VBUCK3_SEL_MASK) - 1,
  456. sizeof(unsigned int) * 8 -
  457. __builtin_clz((DA9062AA_VBUCK3_SEL_MASK)) - 1),
  458. .ilimit = REG_FIELD(DA9062AA_BUCK_ILIM_A,
  459. __builtin_ffs((int)DA9062AA_BUCK3_ILIM_MASK) - 1,
  460. sizeof(unsigned int) * 8 -
  461. __builtin_clz((DA9062AA_BUCK3_ILIM_MASK)) - 1),
  462. },
  463. {
  464. .desc.id = DA9062_ID_BUCK4,
  465. .desc.name = "DA9062 BUCK4",
  466. .desc.of_match = of_match_ptr("buck4"),
  467. .desc.regulators_node = of_match_ptr("regulators"),
  468. .desc.ops = &da9062_buck_ops,
  469. .desc.min_uV = (530) * 1000,
  470. .desc.uV_step = (10) * 1000,
  471. .desc.n_voltages = ((1800) - (530))/(10) + 1,
  472. .current_limits = da9062_buck_a_limits,
  473. .n_current_limits = ARRAY_SIZE(da9062_buck_a_limits),
  474. .desc.enable_reg = DA9062AA_BUCK4_CONT,
  475. .desc.enable_mask = DA9062AA_BUCK4_EN_MASK,
  476. .desc.vsel_reg = DA9062AA_VBUCK4_A,
  477. .desc.vsel_mask = DA9062AA_VBUCK4_A_MASK,
  478. .desc.linear_min_sel = 0,
  479. .sleep = REG_FIELD(DA9062AA_VBUCK4_A,
  480. __builtin_ffs((int)DA9062AA_BUCK4_SL_A_MASK) - 1,
  481. sizeof(unsigned int) * 8 -
  482. __builtin_clz((DA9062AA_BUCK4_SL_A_MASK)) - 1),
  483. .suspend_sleep = REG_FIELD(DA9062AA_VBUCK4_B,
  484. __builtin_ffs((int)DA9062AA_BUCK4_SL_B_MASK) - 1,
  485. sizeof(unsigned int) * 8 -
  486. __builtin_clz((DA9062AA_BUCK4_SL_B_MASK)) - 1),
  487. .suspend_vsel_reg = DA9062AA_VBUCK4_B,
  488. .mode = REG_FIELD(DA9062AA_BUCK4_CFG,
  489. __builtin_ffs((int)DA9062AA_BUCK4_MODE_MASK) - 1,
  490. sizeof(unsigned int) * 8 -
  491. __builtin_clz((DA9062AA_BUCK4_MODE_MASK)) - 1),
  492. .suspend = REG_FIELD(DA9062AA_DVC_1,
  493. __builtin_ffs((int)DA9062AA_VBUCK4_SEL_MASK) - 1,
  494. sizeof(unsigned int) * 8 -
  495. __builtin_clz((DA9062AA_VBUCK4_SEL_MASK)) - 1),
  496. .ilimit = REG_FIELD(DA9062AA_BUCK_ILIM_B,
  497. __builtin_ffs((int)DA9062AA_BUCK4_ILIM_MASK) - 1,
  498. sizeof(unsigned int) * 8 -
  499. __builtin_clz((DA9062AA_BUCK4_ILIM_MASK)) - 1),
  500. },
  501. {
  502. .desc.id = DA9062_ID_LDO1,
  503. .desc.name = "DA9062 LDO1",
  504. .desc.of_match = of_match_ptr("ldo1"),
  505. .desc.regulators_node = of_match_ptr("regulators"),
  506. .desc.ops = &da9062_ldo_ops,
  507. .desc.min_uV = (900) * 1000,
  508. .desc.uV_step = (50) * 1000,
  509. .desc.n_voltages = ((3600) - (900))/(50) + 1,
  510. .desc.enable_reg = DA9062AA_LDO1_CONT,
  511. .desc.enable_mask = DA9062AA_LDO1_EN_MASK,
  512. .desc.vsel_reg = DA9062AA_VLDO1_A,
  513. .desc.vsel_mask = DA9062AA_VLDO1_A_MASK,
  514. .desc.linear_min_sel = 0,
  515. .sleep = REG_FIELD(DA9062AA_VLDO1_A,
  516. __builtin_ffs((int)DA9062AA_LDO1_SL_A_MASK) - 1,
  517. sizeof(unsigned int) * 8 -
  518. __builtin_clz((DA9062AA_LDO1_SL_A_MASK)) - 1),
  519. .suspend_sleep = REG_FIELD(DA9062AA_VLDO1_B,
  520. __builtin_ffs((int)DA9062AA_LDO1_SL_B_MASK) - 1,
  521. sizeof(unsigned int) * 8 -
  522. __builtin_clz((DA9062AA_LDO1_SL_B_MASK)) - 1),
  523. .suspend_vsel_reg = DA9062AA_VLDO1_B,
  524. .suspend = REG_FIELD(DA9062AA_DVC_1,
  525. __builtin_ffs((int)DA9062AA_VLDO1_SEL_MASK) - 1,
  526. sizeof(unsigned int) * 8 -
  527. __builtin_clz((DA9062AA_VLDO1_SEL_MASK)) - 1),
  528. .oc_event = REG_FIELD(DA9062AA_STATUS_D,
  529. __builtin_ffs((int)DA9062AA_LDO1_ILIM_MASK) - 1,
  530. sizeof(unsigned int) * 8 -
  531. __builtin_clz((DA9062AA_LDO1_ILIM_MASK)) - 1),
  532. },
  533. {
  534. .desc.id = DA9062_ID_LDO2,
  535. .desc.name = "DA9062 LDO2",
  536. .desc.of_match = of_match_ptr("ldo2"),
  537. .desc.regulators_node = of_match_ptr("regulators"),
  538. .desc.ops = &da9062_ldo_ops,
  539. .desc.min_uV = (900) * 1000,
  540. .desc.uV_step = (50) * 1000,
  541. .desc.n_voltages = ((3600) - (600))/(50) + 1,
  542. .desc.enable_reg = DA9062AA_LDO2_CONT,
  543. .desc.enable_mask = DA9062AA_LDO2_EN_MASK,
  544. .desc.vsel_reg = DA9062AA_VLDO2_A,
  545. .desc.vsel_mask = DA9062AA_VLDO2_A_MASK,
  546. .desc.linear_min_sel = 0,
  547. .sleep = REG_FIELD(DA9062AA_VLDO2_A,
  548. __builtin_ffs((int)DA9062AA_LDO2_SL_A_MASK) - 1,
  549. sizeof(unsigned int) * 8 -
  550. __builtin_clz((DA9062AA_LDO2_SL_A_MASK)) - 1),
  551. .suspend_sleep = REG_FIELD(DA9062AA_VLDO2_B,
  552. __builtin_ffs((int)DA9062AA_LDO2_SL_B_MASK) - 1,
  553. sizeof(unsigned int) * 8 -
  554. __builtin_clz((DA9062AA_LDO2_SL_B_MASK)) - 1),
  555. .suspend_vsel_reg = DA9062AA_VLDO2_B,
  556. .suspend = REG_FIELD(DA9062AA_DVC_1,
  557. __builtin_ffs((int)DA9062AA_VLDO2_SEL_MASK) - 1,
  558. sizeof(unsigned int) * 8 -
  559. __builtin_clz((DA9062AA_VLDO2_SEL_MASK)) - 1),
  560. .oc_event = REG_FIELD(DA9062AA_STATUS_D,
  561. __builtin_ffs((int)DA9062AA_LDO2_ILIM_MASK) - 1,
  562. sizeof(unsigned int) * 8 -
  563. __builtin_clz((DA9062AA_LDO2_ILIM_MASK)) - 1),
  564. },
  565. {
  566. .desc.id = DA9062_ID_LDO3,
  567. .desc.name = "DA9062 LDO3",
  568. .desc.of_match = of_match_ptr("ldo3"),
  569. .desc.regulators_node = of_match_ptr("regulators"),
  570. .desc.ops = &da9062_ldo_ops,
  571. .desc.min_uV = (900) * 1000,
  572. .desc.uV_step = (50) * 1000,
  573. .desc.n_voltages = ((3600) - (900))/(50) + 1,
  574. .desc.enable_reg = DA9062AA_LDO3_CONT,
  575. .desc.enable_mask = DA9062AA_LDO3_EN_MASK,
  576. .desc.vsel_reg = DA9062AA_VLDO3_A,
  577. .desc.vsel_mask = DA9062AA_VLDO3_A_MASK,
  578. .desc.linear_min_sel = 0,
  579. .sleep = REG_FIELD(DA9062AA_VLDO3_A,
  580. __builtin_ffs((int)DA9062AA_LDO3_SL_A_MASK) - 1,
  581. sizeof(unsigned int) * 8 -
  582. __builtin_clz((DA9062AA_LDO3_SL_A_MASK)) - 1),
  583. .suspend_sleep = REG_FIELD(DA9062AA_VLDO3_B,
  584. __builtin_ffs((int)DA9062AA_LDO3_SL_B_MASK) - 1,
  585. sizeof(unsigned int) * 8 -
  586. __builtin_clz((DA9062AA_LDO3_SL_B_MASK)) - 1),
  587. .suspend_vsel_reg = DA9062AA_VLDO3_B,
  588. .suspend = REG_FIELD(DA9062AA_DVC_1,
  589. __builtin_ffs((int)DA9062AA_VLDO3_SEL_MASK) - 1,
  590. sizeof(unsigned int) * 8 -
  591. __builtin_clz((DA9062AA_VLDO3_SEL_MASK)) - 1),
  592. .oc_event = REG_FIELD(DA9062AA_STATUS_D,
  593. __builtin_ffs((int)DA9062AA_LDO3_ILIM_MASK) - 1,
  594. sizeof(unsigned int) * 8 -
  595. __builtin_clz((DA9062AA_LDO3_ILIM_MASK)) - 1),
  596. },
  597. {
  598. .desc.id = DA9062_ID_LDO4,
  599. .desc.name = "DA9062 LDO4",
  600. .desc.of_match = of_match_ptr("ldo4"),
  601. .desc.regulators_node = of_match_ptr("regulators"),
  602. .desc.ops = &da9062_ldo_ops,
  603. .desc.min_uV = (900) * 1000,
  604. .desc.uV_step = (50) * 1000,
  605. .desc.n_voltages = ((3600) - (900))/(50) + 1,
  606. .desc.enable_reg = DA9062AA_LDO4_CONT,
  607. .desc.enable_mask = DA9062AA_LDO4_EN_MASK,
  608. .desc.vsel_reg = DA9062AA_VLDO4_A,
  609. .desc.vsel_mask = DA9062AA_VLDO4_A_MASK,
  610. .desc.linear_min_sel = 0,
  611. .sleep = REG_FIELD(DA9062AA_VLDO4_A,
  612. __builtin_ffs((int)DA9062AA_LDO4_SL_A_MASK) - 1,
  613. sizeof(unsigned int) * 8 -
  614. __builtin_clz((DA9062AA_LDO4_SL_A_MASK)) - 1),
  615. .suspend_sleep = REG_FIELD(DA9062AA_VLDO4_B,
  616. __builtin_ffs((int)DA9062AA_LDO4_SL_B_MASK) - 1,
  617. sizeof(unsigned int) * 8 -
  618. __builtin_clz((DA9062AA_LDO4_SL_B_MASK)) - 1),
  619. .suspend_vsel_reg = DA9062AA_VLDO4_B,
  620. .suspend = REG_FIELD(DA9062AA_DVC_1,
  621. __builtin_ffs((int)DA9062AA_VLDO4_SEL_MASK) - 1,
  622. sizeof(unsigned int) * 8 -
  623. __builtin_clz((DA9062AA_VLDO4_SEL_MASK)) - 1),
  624. .oc_event = REG_FIELD(DA9062AA_STATUS_D,
  625. __builtin_ffs((int)DA9062AA_LDO4_ILIM_MASK) - 1,
  626. sizeof(unsigned int) * 8 -
  627. __builtin_clz((DA9062AA_LDO4_ILIM_MASK)) - 1),
  628. },
  629. };
  630. /* Regulator interrupt handlers */
  631. static irqreturn_t da9062_ldo_lim_event(int irq, void *data)
  632. {
  633. struct da9062_regulators *regulators = data;
  634. struct da9062 *hw = regulators->regulator[0].hw;
  635. struct da9062_regulator *regl;
  636. int handled = IRQ_NONE;
  637. int bits, i, ret;
  638. ret = regmap_read(hw->regmap, DA9062AA_STATUS_D, &bits);
  639. if (ret < 0) {
  640. dev_err(hw->dev,
  641. "Failed to read LDO overcurrent indicator\n");
  642. goto ldo_lim_error;
  643. }
  644. for (i = regulators->n_regulators - 1; i >= 0; i--) {
  645. regl = &regulators->regulator[i];
  646. if (regl->info->oc_event.reg != DA9062AA_STATUS_D)
  647. continue;
  648. if (BIT(regl->info->oc_event.lsb) & bits) {
  649. regulator_notifier_call_chain(regl->rdev,
  650. REGULATOR_EVENT_OVER_CURRENT, NULL);
  651. handled = IRQ_HANDLED;
  652. }
  653. }
  654. ldo_lim_error:
  655. return handled;
  656. }
  657. static int da9062_regulator_probe(struct platform_device *pdev)
  658. {
  659. struct da9062 *chip = dev_get_drvdata(pdev->dev.parent);
  660. struct da9062_regulators *regulators;
  661. struct da9062_regulator *regl;
  662. struct regulator_config config = { };
  663. int irq, n, ret;
  664. size_t size;
  665. /* Allocate memory required by usable regulators */
  666. size = sizeof(struct da9062_regulators) +
  667. DA9062_MAX_REGULATORS * sizeof(struct da9062_regulator);
  668. regulators = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
  669. if (!regulators)
  670. return -ENOMEM;
  671. regulators->n_regulators = DA9062_MAX_REGULATORS;
  672. platform_set_drvdata(pdev, regulators);
  673. n = 0;
  674. while (n < regulators->n_regulators) {
  675. /* Initialise regulator structure */
  676. regl = &regulators->regulator[n];
  677. regl->hw = chip;
  678. regl->info = &local_regulator_info[n];
  679. regl->desc = regl->info->desc;
  680. regl->desc.type = REGULATOR_VOLTAGE;
  681. regl->desc.owner = THIS_MODULE;
  682. if (regl->info->mode.reg)
  683. regl->mode = devm_regmap_field_alloc(
  684. &pdev->dev,
  685. chip->regmap,
  686. regl->info->mode);
  687. if (regl->info->suspend.reg)
  688. regl->suspend = devm_regmap_field_alloc(
  689. &pdev->dev,
  690. chip->regmap,
  691. regl->info->suspend);
  692. if (regl->info->sleep.reg)
  693. regl->sleep = devm_regmap_field_alloc(
  694. &pdev->dev,
  695. chip->regmap,
  696. regl->info->sleep);
  697. if (regl->info->suspend_sleep.reg)
  698. regl->suspend_sleep = devm_regmap_field_alloc(
  699. &pdev->dev,
  700. chip->regmap,
  701. regl->info->suspend_sleep);
  702. if (regl->info->ilimit.reg)
  703. regl->ilimit = devm_regmap_field_alloc(
  704. &pdev->dev,
  705. chip->regmap,
  706. regl->info->ilimit);
  707. /* Register regulator */
  708. memset(&config, 0, sizeof(config));
  709. config.dev = chip->dev;
  710. config.driver_data = regl;
  711. config.regmap = chip->regmap;
  712. regl->rdev = devm_regulator_register(&pdev->dev, &regl->desc,
  713. &config);
  714. if (IS_ERR(regl->rdev)) {
  715. dev_err(&pdev->dev,
  716. "Failed to register %s regulator\n",
  717. regl->desc.name);
  718. return PTR_ERR(regl->rdev);
  719. }
  720. n++;
  721. }
  722. /* LDOs overcurrent event support */
  723. irq = platform_get_irq_byname(pdev, "LDO_LIM");
  724. if (irq < 0) {
  725. dev_err(&pdev->dev, "Failed to get IRQ.\n");
  726. return irq;
  727. }
  728. regulators->irq_ldo_lim = irq;
  729. ret = devm_request_threaded_irq(&pdev->dev, irq,
  730. NULL, da9062_ldo_lim_event,
  731. IRQF_TRIGGER_LOW | IRQF_ONESHOT,
  732. "LDO_LIM", regulators);
  733. if (ret) {
  734. dev_warn(&pdev->dev,
  735. "Failed to request LDO_LIM IRQ.\n");
  736. regulators->irq_ldo_lim = -ENXIO;
  737. }
  738. return 0;
  739. }
  740. static struct platform_driver da9062_regulator_driver = {
  741. .driver = {
  742. .name = "da9062-regulators",
  743. },
  744. .probe = da9062_regulator_probe,
  745. };
  746. static int __init da9062_regulator_init(void)
  747. {
  748. return platform_driver_register(&da9062_regulator_driver);
  749. }
  750. subsys_initcall(da9062_regulator_init);
  751. static void __exit da9062_regulator_cleanup(void)
  752. {
  753. platform_driver_unregister(&da9062_regulator_driver);
  754. }
  755. module_exit(da9062_regulator_cleanup);
  756. /* Module information */
  757. MODULE_AUTHOR("S Twiss <stwiss.opensource@diasemi.com>");
  758. MODULE_DESCRIPTION("REGULATOR device driver for Dialog DA9062");
  759. MODULE_LICENSE("GPL");
  760. MODULE_ALIAS("platform:da9062-regulators");