s2mps11.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096
  1. /*
  2. * s2mps11.c
  3. *
  4. * Copyright (c) 2012-2014 Samsung Electronics Co., Ltd
  5. * http://www.samsung.com
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2 of the License, or (at your
  10. * option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. */
  18. #include <linux/bug.h>
  19. #include <linux/err.h>
  20. #include <linux/gpio.h>
  21. #include <linux/slab.h>
  22. #include <linux/module.h>
  23. #include <linux/of.h>
  24. #include <linux/regmap.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/regulator/driver.h>
  27. #include <linux/regulator/machine.h>
  28. #include <linux/regulator/of_regulator.h>
  29. #include <linux/of_gpio.h>
  30. #include <linux/mfd/samsung/core.h>
  31. #include <linux/mfd/samsung/s2mps11.h>
  32. #include <linux/mfd/samsung/s2mps13.h>
  33. #include <linux/mfd/samsung/s2mps14.h>
  34. #include <linux/mfd/samsung/s2mpu02.h>
  35. struct s2mps11_info {
  36. unsigned int rdev_num;
  37. int ramp_delay2;
  38. int ramp_delay34;
  39. int ramp_delay5;
  40. int ramp_delay16;
  41. int ramp_delay7810;
  42. int ramp_delay9;
  43. enum sec_device_type dev_type;
  44. /*
  45. * One bit for each S2MPS13/S2MPS14/S2MPU02 regulator whether
  46. * the suspend mode was enabled.
  47. */
  48. unsigned long long s2mps14_suspend_state:50;
  49. /* Array of size rdev_num with GPIO-s for external sleep control */
  50. int *ext_control_gpio;
  51. };
  52. static int get_ramp_delay(int ramp_delay)
  53. {
  54. unsigned char cnt = 0;
  55. ramp_delay /= 6250;
  56. while (true) {
  57. ramp_delay = ramp_delay >> 1;
  58. if (ramp_delay == 0)
  59. break;
  60. cnt++;
  61. }
  62. if (cnt > 3)
  63. cnt = 3;
  64. return cnt;
  65. }
  66. static int s2mps11_regulator_set_voltage_time_sel(struct regulator_dev *rdev,
  67. unsigned int old_selector,
  68. unsigned int new_selector)
  69. {
  70. struct s2mps11_info *s2mps11 = rdev_get_drvdata(rdev);
  71. unsigned int ramp_delay = 0;
  72. int old_volt, new_volt;
  73. switch (rdev_get_id(rdev)) {
  74. case S2MPS11_BUCK2:
  75. ramp_delay = s2mps11->ramp_delay2;
  76. break;
  77. case S2MPS11_BUCK3:
  78. case S2MPS11_BUCK4:
  79. ramp_delay = s2mps11->ramp_delay34;
  80. break;
  81. case S2MPS11_BUCK5:
  82. ramp_delay = s2mps11->ramp_delay5;
  83. break;
  84. case S2MPS11_BUCK6:
  85. case S2MPS11_BUCK1:
  86. ramp_delay = s2mps11->ramp_delay16;
  87. break;
  88. case S2MPS11_BUCK7:
  89. case S2MPS11_BUCK8:
  90. case S2MPS11_BUCK10:
  91. ramp_delay = s2mps11->ramp_delay7810;
  92. break;
  93. case S2MPS11_BUCK9:
  94. ramp_delay = s2mps11->ramp_delay9;
  95. }
  96. if (ramp_delay == 0)
  97. ramp_delay = rdev->desc->ramp_delay;
  98. old_volt = rdev->desc->min_uV + (rdev->desc->uV_step * old_selector);
  99. new_volt = rdev->desc->min_uV + (rdev->desc->uV_step * new_selector);
  100. return DIV_ROUND_UP(abs(new_volt - old_volt), ramp_delay);
  101. }
  102. static int s2mps11_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay)
  103. {
  104. struct s2mps11_info *s2mps11 = rdev_get_drvdata(rdev);
  105. unsigned int ramp_val, ramp_shift, ramp_reg = S2MPS11_REG_RAMP_BUCK;
  106. unsigned int ramp_enable = 1, enable_shift = 0;
  107. int ret;
  108. switch (rdev_get_id(rdev)) {
  109. case S2MPS11_BUCK1:
  110. if (ramp_delay > s2mps11->ramp_delay16)
  111. s2mps11->ramp_delay16 = ramp_delay;
  112. else
  113. ramp_delay = s2mps11->ramp_delay16;
  114. ramp_shift = S2MPS11_BUCK16_RAMP_SHIFT;
  115. break;
  116. case S2MPS11_BUCK2:
  117. enable_shift = S2MPS11_BUCK2_RAMP_EN_SHIFT;
  118. if (!ramp_delay) {
  119. ramp_enable = 0;
  120. break;
  121. }
  122. s2mps11->ramp_delay2 = ramp_delay;
  123. ramp_shift = S2MPS11_BUCK2_RAMP_SHIFT;
  124. ramp_reg = S2MPS11_REG_RAMP;
  125. break;
  126. case S2MPS11_BUCK3:
  127. enable_shift = S2MPS11_BUCK3_RAMP_EN_SHIFT;
  128. if (!ramp_delay) {
  129. ramp_enable = 0;
  130. break;
  131. }
  132. if (ramp_delay > s2mps11->ramp_delay34)
  133. s2mps11->ramp_delay34 = ramp_delay;
  134. else
  135. ramp_delay = s2mps11->ramp_delay34;
  136. ramp_shift = S2MPS11_BUCK34_RAMP_SHIFT;
  137. ramp_reg = S2MPS11_REG_RAMP;
  138. break;
  139. case S2MPS11_BUCK4:
  140. enable_shift = S2MPS11_BUCK4_RAMP_EN_SHIFT;
  141. if (!ramp_delay) {
  142. ramp_enable = 0;
  143. break;
  144. }
  145. if (ramp_delay > s2mps11->ramp_delay34)
  146. s2mps11->ramp_delay34 = ramp_delay;
  147. else
  148. ramp_delay = s2mps11->ramp_delay34;
  149. ramp_shift = S2MPS11_BUCK34_RAMP_SHIFT;
  150. ramp_reg = S2MPS11_REG_RAMP;
  151. break;
  152. case S2MPS11_BUCK5:
  153. s2mps11->ramp_delay5 = ramp_delay;
  154. ramp_shift = S2MPS11_BUCK5_RAMP_SHIFT;
  155. break;
  156. case S2MPS11_BUCK6:
  157. enable_shift = S2MPS11_BUCK6_RAMP_EN_SHIFT;
  158. if (!ramp_delay) {
  159. ramp_enable = 0;
  160. break;
  161. }
  162. if (ramp_delay > s2mps11->ramp_delay16)
  163. s2mps11->ramp_delay16 = ramp_delay;
  164. else
  165. ramp_delay = s2mps11->ramp_delay16;
  166. ramp_shift = S2MPS11_BUCK16_RAMP_SHIFT;
  167. break;
  168. case S2MPS11_BUCK7:
  169. case S2MPS11_BUCK8:
  170. case S2MPS11_BUCK10:
  171. if (ramp_delay > s2mps11->ramp_delay7810)
  172. s2mps11->ramp_delay7810 = ramp_delay;
  173. else
  174. ramp_delay = s2mps11->ramp_delay7810;
  175. ramp_shift = S2MPS11_BUCK7810_RAMP_SHIFT;
  176. break;
  177. case S2MPS11_BUCK9:
  178. s2mps11->ramp_delay9 = ramp_delay;
  179. ramp_shift = S2MPS11_BUCK9_RAMP_SHIFT;
  180. break;
  181. default:
  182. return 0;
  183. }
  184. if (!ramp_enable)
  185. goto ramp_disable;
  186. /* Ramp delay can be enabled/disabled only for buck[2346] */
  187. if ((rdev_get_id(rdev) >= S2MPS11_BUCK2 &&
  188. rdev_get_id(rdev) <= S2MPS11_BUCK4) ||
  189. rdev_get_id(rdev) == S2MPS11_BUCK6) {
  190. ret = regmap_update_bits(rdev->regmap, S2MPS11_REG_RAMP,
  191. 1 << enable_shift, 1 << enable_shift);
  192. if (ret) {
  193. dev_err(&rdev->dev, "failed to enable ramp rate\n");
  194. return ret;
  195. }
  196. }
  197. ramp_val = get_ramp_delay(ramp_delay);
  198. return regmap_update_bits(rdev->regmap, ramp_reg, 0x3 << ramp_shift,
  199. ramp_val << ramp_shift);
  200. ramp_disable:
  201. return regmap_update_bits(rdev->regmap, S2MPS11_REG_RAMP,
  202. 1 << enable_shift, 0);
  203. }
  204. static struct regulator_ops s2mps11_ldo_ops = {
  205. .list_voltage = regulator_list_voltage_linear,
  206. .map_voltage = regulator_map_voltage_linear,
  207. .is_enabled = regulator_is_enabled_regmap,
  208. .enable = regulator_enable_regmap,
  209. .disable = regulator_disable_regmap,
  210. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  211. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  212. .set_voltage_time_sel = regulator_set_voltage_time_sel,
  213. };
  214. static struct regulator_ops s2mps11_buck_ops = {
  215. .list_voltage = regulator_list_voltage_linear,
  216. .map_voltage = regulator_map_voltage_linear,
  217. .is_enabled = regulator_is_enabled_regmap,
  218. .enable = regulator_enable_regmap,
  219. .disable = regulator_disable_regmap,
  220. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  221. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  222. .set_voltage_time_sel = s2mps11_regulator_set_voltage_time_sel,
  223. .set_ramp_delay = s2mps11_set_ramp_delay,
  224. };
  225. #define regulator_desc_s2mps11_ldo(num, step) { \
  226. .name = "LDO"#num, \
  227. .id = S2MPS11_LDO##num, \
  228. .ops = &s2mps11_ldo_ops, \
  229. .type = REGULATOR_VOLTAGE, \
  230. .owner = THIS_MODULE, \
  231. .min_uV = MIN_800_MV, \
  232. .uV_step = step, \
  233. .n_voltages = S2MPS11_LDO_N_VOLTAGES, \
  234. .vsel_reg = S2MPS11_REG_L1CTRL + num - 1, \
  235. .vsel_mask = S2MPS11_LDO_VSEL_MASK, \
  236. .enable_reg = S2MPS11_REG_L1CTRL + num - 1, \
  237. .enable_mask = S2MPS11_ENABLE_MASK \
  238. }
  239. #define regulator_desc_s2mps11_buck1_4(num) { \
  240. .name = "BUCK"#num, \
  241. .id = S2MPS11_BUCK##num, \
  242. .ops = &s2mps11_buck_ops, \
  243. .type = REGULATOR_VOLTAGE, \
  244. .owner = THIS_MODULE, \
  245. .min_uV = MIN_600_MV, \
  246. .uV_step = STEP_6_25_MV, \
  247. .n_voltages = S2MPS11_BUCK_N_VOLTAGES, \
  248. .ramp_delay = S2MPS11_RAMP_DELAY, \
  249. .vsel_reg = S2MPS11_REG_B1CTRL2 + (num - 1) * 2, \
  250. .vsel_mask = S2MPS11_BUCK_VSEL_MASK, \
  251. .enable_reg = S2MPS11_REG_B1CTRL1 + (num - 1) * 2, \
  252. .enable_mask = S2MPS11_ENABLE_MASK \
  253. }
  254. #define regulator_desc_s2mps11_buck5 { \
  255. .name = "BUCK5", \
  256. .id = S2MPS11_BUCK5, \
  257. .ops = &s2mps11_buck_ops, \
  258. .type = REGULATOR_VOLTAGE, \
  259. .owner = THIS_MODULE, \
  260. .min_uV = MIN_600_MV, \
  261. .uV_step = STEP_6_25_MV, \
  262. .n_voltages = S2MPS11_BUCK_N_VOLTAGES, \
  263. .ramp_delay = S2MPS11_RAMP_DELAY, \
  264. .vsel_reg = S2MPS11_REG_B5CTRL2, \
  265. .vsel_mask = S2MPS11_BUCK_VSEL_MASK, \
  266. .enable_reg = S2MPS11_REG_B5CTRL1, \
  267. .enable_mask = S2MPS11_ENABLE_MASK \
  268. }
  269. #define regulator_desc_s2mps11_buck6_10(num, min, step) { \
  270. .name = "BUCK"#num, \
  271. .id = S2MPS11_BUCK##num, \
  272. .ops = &s2mps11_buck_ops, \
  273. .type = REGULATOR_VOLTAGE, \
  274. .owner = THIS_MODULE, \
  275. .min_uV = min, \
  276. .uV_step = step, \
  277. .n_voltages = S2MPS11_BUCK_N_VOLTAGES, \
  278. .ramp_delay = S2MPS11_RAMP_DELAY, \
  279. .vsel_reg = S2MPS11_REG_B6CTRL2 + (num - 6) * 2, \
  280. .vsel_mask = S2MPS11_BUCK_VSEL_MASK, \
  281. .enable_reg = S2MPS11_REG_B6CTRL1 + (num - 6) * 2, \
  282. .enable_mask = S2MPS11_ENABLE_MASK \
  283. }
  284. static const struct regulator_desc s2mps11_regulators[] = {
  285. regulator_desc_s2mps11_ldo(1, STEP_25_MV),
  286. regulator_desc_s2mps11_ldo(2, STEP_50_MV),
  287. regulator_desc_s2mps11_ldo(3, STEP_50_MV),
  288. regulator_desc_s2mps11_ldo(4, STEP_50_MV),
  289. regulator_desc_s2mps11_ldo(5, STEP_50_MV),
  290. regulator_desc_s2mps11_ldo(6, STEP_25_MV),
  291. regulator_desc_s2mps11_ldo(7, STEP_50_MV),
  292. regulator_desc_s2mps11_ldo(8, STEP_50_MV),
  293. regulator_desc_s2mps11_ldo(9, STEP_50_MV),
  294. regulator_desc_s2mps11_ldo(10, STEP_50_MV),
  295. regulator_desc_s2mps11_ldo(11, STEP_25_MV),
  296. regulator_desc_s2mps11_ldo(12, STEP_50_MV),
  297. regulator_desc_s2mps11_ldo(13, STEP_50_MV),
  298. regulator_desc_s2mps11_ldo(14, STEP_50_MV),
  299. regulator_desc_s2mps11_ldo(15, STEP_50_MV),
  300. regulator_desc_s2mps11_ldo(16, STEP_50_MV),
  301. regulator_desc_s2mps11_ldo(17, STEP_50_MV),
  302. regulator_desc_s2mps11_ldo(18, STEP_50_MV),
  303. regulator_desc_s2mps11_ldo(19, STEP_50_MV),
  304. regulator_desc_s2mps11_ldo(20, STEP_50_MV),
  305. regulator_desc_s2mps11_ldo(21, STEP_50_MV),
  306. regulator_desc_s2mps11_ldo(22, STEP_25_MV),
  307. regulator_desc_s2mps11_ldo(23, STEP_25_MV),
  308. regulator_desc_s2mps11_ldo(24, STEP_50_MV),
  309. regulator_desc_s2mps11_ldo(25, STEP_50_MV),
  310. regulator_desc_s2mps11_ldo(26, STEP_50_MV),
  311. regulator_desc_s2mps11_ldo(27, STEP_25_MV),
  312. regulator_desc_s2mps11_ldo(28, STEP_50_MV),
  313. regulator_desc_s2mps11_ldo(29, STEP_50_MV),
  314. regulator_desc_s2mps11_ldo(30, STEP_50_MV),
  315. regulator_desc_s2mps11_ldo(31, STEP_50_MV),
  316. regulator_desc_s2mps11_ldo(32, STEP_50_MV),
  317. regulator_desc_s2mps11_ldo(33, STEP_50_MV),
  318. regulator_desc_s2mps11_ldo(34, STEP_50_MV),
  319. regulator_desc_s2mps11_ldo(35, STEP_50_MV),
  320. regulator_desc_s2mps11_ldo(36, STEP_50_MV),
  321. regulator_desc_s2mps11_ldo(37, STEP_50_MV),
  322. regulator_desc_s2mps11_ldo(38, STEP_50_MV),
  323. regulator_desc_s2mps11_buck1_4(1),
  324. regulator_desc_s2mps11_buck1_4(2),
  325. regulator_desc_s2mps11_buck1_4(3),
  326. regulator_desc_s2mps11_buck1_4(4),
  327. regulator_desc_s2mps11_buck5,
  328. regulator_desc_s2mps11_buck6_10(6, MIN_600_MV, STEP_6_25_MV),
  329. regulator_desc_s2mps11_buck6_10(7, MIN_600_MV, STEP_6_25_MV),
  330. regulator_desc_s2mps11_buck6_10(8, MIN_600_MV, STEP_6_25_MV),
  331. regulator_desc_s2mps11_buck6_10(9, MIN_3000_MV, STEP_25_MV),
  332. regulator_desc_s2mps11_buck6_10(10, MIN_750_MV, STEP_12_5_MV),
  333. };
  334. static struct regulator_ops s2mps14_reg_ops;
  335. #define regulator_desc_s2mps13_ldo(num, min, step, min_sel) { \
  336. .name = "LDO"#num, \
  337. .id = S2MPS13_LDO##num, \
  338. .ops = &s2mps14_reg_ops, \
  339. .type = REGULATOR_VOLTAGE, \
  340. .owner = THIS_MODULE, \
  341. .min_uV = min, \
  342. .uV_step = step, \
  343. .linear_min_sel = min_sel, \
  344. .n_voltages = S2MPS14_LDO_N_VOLTAGES, \
  345. .vsel_reg = S2MPS13_REG_L1CTRL + num - 1, \
  346. .vsel_mask = S2MPS14_LDO_VSEL_MASK, \
  347. .enable_reg = S2MPS13_REG_L1CTRL + num - 1, \
  348. .enable_mask = S2MPS14_ENABLE_MASK \
  349. }
  350. #define regulator_desc_s2mps13_buck(num, min, step, min_sel) { \
  351. .name = "BUCK"#num, \
  352. .id = S2MPS13_BUCK##num, \
  353. .ops = &s2mps14_reg_ops, \
  354. .type = REGULATOR_VOLTAGE, \
  355. .owner = THIS_MODULE, \
  356. .min_uV = min, \
  357. .uV_step = step, \
  358. .linear_min_sel = min_sel, \
  359. .n_voltages = S2MPS14_BUCK_N_VOLTAGES, \
  360. .ramp_delay = S2MPS13_BUCK_RAMP_DELAY, \
  361. .vsel_reg = S2MPS13_REG_B1OUT + (num - 1) * 2, \
  362. .vsel_mask = S2MPS14_BUCK_VSEL_MASK, \
  363. .enable_reg = S2MPS13_REG_B1CTRL + (num - 1) * 2, \
  364. .enable_mask = S2MPS14_ENABLE_MASK \
  365. }
  366. #define regulator_desc_s2mps13_buck7(num, min, step, min_sel) { \
  367. .name = "BUCK"#num, \
  368. .id = S2MPS13_BUCK##num, \
  369. .ops = &s2mps14_reg_ops, \
  370. .type = REGULATOR_VOLTAGE, \
  371. .owner = THIS_MODULE, \
  372. .min_uV = min, \
  373. .uV_step = step, \
  374. .linear_min_sel = min_sel, \
  375. .n_voltages = S2MPS14_BUCK_N_VOLTAGES, \
  376. .ramp_delay = S2MPS13_BUCK_RAMP_DELAY, \
  377. .vsel_reg = S2MPS13_REG_B1OUT + (num) * 2 - 1, \
  378. .vsel_mask = S2MPS14_BUCK_VSEL_MASK, \
  379. .enable_reg = S2MPS13_REG_B1CTRL + (num - 1) * 2, \
  380. .enable_mask = S2MPS14_ENABLE_MASK \
  381. }
  382. #define regulator_desc_s2mps13_buck8_10(num, min, step, min_sel) { \
  383. .name = "BUCK"#num, \
  384. .id = S2MPS13_BUCK##num, \
  385. .ops = &s2mps14_reg_ops, \
  386. .type = REGULATOR_VOLTAGE, \
  387. .owner = THIS_MODULE, \
  388. .min_uV = min, \
  389. .uV_step = step, \
  390. .linear_min_sel = min_sel, \
  391. .n_voltages = S2MPS14_BUCK_N_VOLTAGES, \
  392. .ramp_delay = S2MPS13_BUCK_RAMP_DELAY, \
  393. .vsel_reg = S2MPS13_REG_B1OUT + (num) * 2 - 1, \
  394. .vsel_mask = S2MPS14_BUCK_VSEL_MASK, \
  395. .enable_reg = S2MPS13_REG_B1CTRL + (num) * 2 - 1, \
  396. .enable_mask = S2MPS14_ENABLE_MASK \
  397. }
  398. static const struct regulator_desc s2mps13_regulators[] = {
  399. regulator_desc_s2mps13_ldo(1, MIN_800_MV, STEP_12_5_MV, 0x00),
  400. regulator_desc_s2mps13_ldo(2, MIN_1400_MV, STEP_50_MV, 0x0C),
  401. regulator_desc_s2mps13_ldo(3, MIN_1000_MV, STEP_25_MV, 0x08),
  402. regulator_desc_s2mps13_ldo(4, MIN_800_MV, STEP_12_5_MV, 0x00),
  403. regulator_desc_s2mps13_ldo(5, MIN_800_MV, STEP_12_5_MV, 0x00),
  404. regulator_desc_s2mps13_ldo(6, MIN_800_MV, STEP_12_5_MV, 0x00),
  405. regulator_desc_s2mps13_ldo(7, MIN_1000_MV, STEP_25_MV, 0x08),
  406. regulator_desc_s2mps13_ldo(8, MIN_1000_MV, STEP_25_MV, 0x08),
  407. regulator_desc_s2mps13_ldo(9, MIN_1000_MV, STEP_25_MV, 0x08),
  408. regulator_desc_s2mps13_ldo(10, MIN_1400_MV, STEP_50_MV, 0x0C),
  409. regulator_desc_s2mps13_ldo(11, MIN_800_MV, STEP_25_MV, 0x10),
  410. regulator_desc_s2mps13_ldo(12, MIN_800_MV, STEP_25_MV, 0x10),
  411. regulator_desc_s2mps13_ldo(13, MIN_800_MV, STEP_25_MV, 0x10),
  412. regulator_desc_s2mps13_ldo(14, MIN_800_MV, STEP_12_5_MV, 0x00),
  413. regulator_desc_s2mps13_ldo(15, MIN_800_MV, STEP_12_5_MV, 0x00),
  414. regulator_desc_s2mps13_ldo(16, MIN_1400_MV, STEP_50_MV, 0x0C),
  415. regulator_desc_s2mps13_ldo(17, MIN_1400_MV, STEP_50_MV, 0x0C),
  416. regulator_desc_s2mps13_ldo(18, MIN_1000_MV, STEP_25_MV, 0x08),
  417. regulator_desc_s2mps13_ldo(19, MIN_1000_MV, STEP_25_MV, 0x08),
  418. regulator_desc_s2mps13_ldo(20, MIN_1400_MV, STEP_50_MV, 0x0C),
  419. regulator_desc_s2mps13_ldo(21, MIN_1000_MV, STEP_25_MV, 0x08),
  420. regulator_desc_s2mps13_ldo(22, MIN_1000_MV, STEP_25_MV, 0x08),
  421. regulator_desc_s2mps13_ldo(23, MIN_800_MV, STEP_12_5_MV, 0x00),
  422. regulator_desc_s2mps13_ldo(24, MIN_800_MV, STEP_12_5_MV, 0x00),
  423. regulator_desc_s2mps13_ldo(25, MIN_1400_MV, STEP_50_MV, 0x0C),
  424. regulator_desc_s2mps13_ldo(26, MIN_1400_MV, STEP_50_MV, 0x0C),
  425. regulator_desc_s2mps13_ldo(27, MIN_1400_MV, STEP_50_MV, 0x0C),
  426. regulator_desc_s2mps13_ldo(28, MIN_1000_MV, STEP_25_MV, 0x08),
  427. regulator_desc_s2mps13_ldo(29, MIN_1400_MV, STEP_50_MV, 0x0C),
  428. regulator_desc_s2mps13_ldo(30, MIN_1400_MV, STEP_50_MV, 0x0C),
  429. regulator_desc_s2mps13_ldo(31, MIN_1000_MV, STEP_25_MV, 0x08),
  430. regulator_desc_s2mps13_ldo(32, MIN_1000_MV, STEP_25_MV, 0x08),
  431. regulator_desc_s2mps13_ldo(33, MIN_1400_MV, STEP_50_MV, 0x0C),
  432. regulator_desc_s2mps13_ldo(34, MIN_1000_MV, STEP_25_MV, 0x08),
  433. regulator_desc_s2mps13_ldo(35, MIN_1400_MV, STEP_50_MV, 0x0C),
  434. regulator_desc_s2mps13_ldo(36, MIN_800_MV, STEP_12_5_MV, 0x00),
  435. regulator_desc_s2mps13_ldo(37, MIN_1000_MV, STEP_25_MV, 0x08),
  436. regulator_desc_s2mps13_ldo(38, MIN_1400_MV, STEP_50_MV, 0x0C),
  437. regulator_desc_s2mps13_ldo(39, MIN_1000_MV, STEP_25_MV, 0x08),
  438. regulator_desc_s2mps13_ldo(40, MIN_1400_MV, STEP_50_MV, 0x0C),
  439. regulator_desc_s2mps13_buck(1, MIN_500_MV, STEP_6_25_MV, 0x10),
  440. regulator_desc_s2mps13_buck(2, MIN_500_MV, STEP_6_25_MV, 0x10),
  441. regulator_desc_s2mps13_buck(3, MIN_500_MV, STEP_6_25_MV, 0x10),
  442. regulator_desc_s2mps13_buck(4, MIN_500_MV, STEP_6_25_MV, 0x10),
  443. regulator_desc_s2mps13_buck(5, MIN_500_MV, STEP_6_25_MV, 0x10),
  444. regulator_desc_s2mps13_buck(6, MIN_500_MV, STEP_6_25_MV, 0x10),
  445. regulator_desc_s2mps13_buck7(7, MIN_500_MV, STEP_6_25_MV, 0x10),
  446. regulator_desc_s2mps13_buck8_10(8, MIN_1000_MV, STEP_12_5_MV, 0x20),
  447. regulator_desc_s2mps13_buck8_10(9, MIN_1000_MV, STEP_12_5_MV, 0x20),
  448. regulator_desc_s2mps13_buck8_10(10, MIN_500_MV, STEP_6_25_MV, 0x10),
  449. };
  450. static int s2mps14_regulator_enable(struct regulator_dev *rdev)
  451. {
  452. struct s2mps11_info *s2mps11 = rdev_get_drvdata(rdev);
  453. unsigned int val;
  454. switch (s2mps11->dev_type) {
  455. case S2MPS13X:
  456. case S2MPS14X:
  457. if (s2mps11->s2mps14_suspend_state & (1 << rdev_get_id(rdev)))
  458. val = S2MPS14_ENABLE_SUSPEND;
  459. else if (gpio_is_valid(s2mps11->ext_control_gpio[rdev_get_id(rdev)]))
  460. val = S2MPS14_ENABLE_EXT_CONTROL;
  461. else
  462. val = rdev->desc->enable_mask;
  463. break;
  464. case S2MPU02:
  465. if (s2mps11->s2mps14_suspend_state & (1 << rdev_get_id(rdev)))
  466. val = S2MPU02_ENABLE_SUSPEND;
  467. else
  468. val = rdev->desc->enable_mask;
  469. break;
  470. default:
  471. return -EINVAL;
  472. }
  473. return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
  474. rdev->desc->enable_mask, val);
  475. }
  476. static int s2mps14_regulator_set_suspend_disable(struct regulator_dev *rdev)
  477. {
  478. int ret;
  479. unsigned int val, state;
  480. struct s2mps11_info *s2mps11 = rdev_get_drvdata(rdev);
  481. int rdev_id = rdev_get_id(rdev);
  482. /* Below LDO should be always on or does not support suspend mode. */
  483. switch (s2mps11->dev_type) {
  484. case S2MPS13X:
  485. case S2MPS14X:
  486. switch (rdev_id) {
  487. case S2MPS14_LDO3:
  488. return 0;
  489. default:
  490. state = S2MPS14_ENABLE_SUSPEND;
  491. break;
  492. }
  493. break;
  494. case S2MPU02:
  495. switch (rdev_id) {
  496. case S2MPU02_LDO13:
  497. case S2MPU02_LDO14:
  498. case S2MPU02_LDO15:
  499. case S2MPU02_LDO17:
  500. case S2MPU02_BUCK7:
  501. state = S2MPU02_DISABLE_SUSPEND;
  502. break;
  503. default:
  504. state = S2MPU02_ENABLE_SUSPEND;
  505. break;
  506. }
  507. break;
  508. default:
  509. return -EINVAL;
  510. }
  511. ret = regmap_read(rdev->regmap, rdev->desc->enable_reg, &val);
  512. if (ret < 0)
  513. return ret;
  514. s2mps11->s2mps14_suspend_state |= (1 << rdev_get_id(rdev));
  515. /*
  516. * Don't enable suspend mode if regulator is already disabled because
  517. * this would effectively for a short time turn on the regulator after
  518. * resuming.
  519. * However we still want to toggle the suspend_state bit for regulator
  520. * in case if it got enabled before suspending the system.
  521. */
  522. if (!(val & rdev->desc->enable_mask))
  523. return 0;
  524. return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
  525. rdev->desc->enable_mask, state);
  526. }
  527. static struct regulator_ops s2mps14_reg_ops = {
  528. .list_voltage = regulator_list_voltage_linear,
  529. .map_voltage = regulator_map_voltage_linear,
  530. .is_enabled = regulator_is_enabled_regmap,
  531. .enable = s2mps14_regulator_enable,
  532. .disable = regulator_disable_regmap,
  533. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  534. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  535. .set_voltage_time_sel = regulator_set_voltage_time_sel,
  536. .set_suspend_disable = s2mps14_regulator_set_suspend_disable,
  537. };
  538. #define regulator_desc_s2mps14_ldo(num, min, step) { \
  539. .name = "LDO"#num, \
  540. .id = S2MPS14_LDO##num, \
  541. .ops = &s2mps14_reg_ops, \
  542. .type = REGULATOR_VOLTAGE, \
  543. .owner = THIS_MODULE, \
  544. .min_uV = min, \
  545. .uV_step = step, \
  546. .n_voltages = S2MPS14_LDO_N_VOLTAGES, \
  547. .vsel_reg = S2MPS14_REG_L1CTRL + num - 1, \
  548. .vsel_mask = S2MPS14_LDO_VSEL_MASK, \
  549. .enable_reg = S2MPS14_REG_L1CTRL + num - 1, \
  550. .enable_mask = S2MPS14_ENABLE_MASK \
  551. }
  552. #define regulator_desc_s2mps14_buck(num, min, step, min_sel) { \
  553. .name = "BUCK"#num, \
  554. .id = S2MPS14_BUCK##num, \
  555. .ops = &s2mps14_reg_ops, \
  556. .type = REGULATOR_VOLTAGE, \
  557. .owner = THIS_MODULE, \
  558. .min_uV = min, \
  559. .uV_step = step, \
  560. .n_voltages = S2MPS14_BUCK_N_VOLTAGES, \
  561. .linear_min_sel = min_sel, \
  562. .ramp_delay = S2MPS14_BUCK_RAMP_DELAY, \
  563. .vsel_reg = S2MPS14_REG_B1CTRL2 + (num - 1) * 2, \
  564. .vsel_mask = S2MPS14_BUCK_VSEL_MASK, \
  565. .enable_reg = S2MPS14_REG_B1CTRL1 + (num - 1) * 2, \
  566. .enable_mask = S2MPS14_ENABLE_MASK \
  567. }
  568. static const struct regulator_desc s2mps14_regulators[] = {
  569. regulator_desc_s2mps14_ldo(1, MIN_800_MV, STEP_12_5_MV),
  570. regulator_desc_s2mps14_ldo(2, MIN_800_MV, STEP_12_5_MV),
  571. regulator_desc_s2mps14_ldo(3, MIN_800_MV, STEP_25_MV),
  572. regulator_desc_s2mps14_ldo(4, MIN_800_MV, STEP_25_MV),
  573. regulator_desc_s2mps14_ldo(5, MIN_800_MV, STEP_12_5_MV),
  574. regulator_desc_s2mps14_ldo(6, MIN_800_MV, STEP_12_5_MV),
  575. regulator_desc_s2mps14_ldo(7, MIN_800_MV, STEP_25_MV),
  576. regulator_desc_s2mps14_ldo(8, MIN_1800_MV, STEP_25_MV),
  577. regulator_desc_s2mps14_ldo(9, MIN_800_MV, STEP_12_5_MV),
  578. regulator_desc_s2mps14_ldo(10, MIN_800_MV, STEP_12_5_MV),
  579. regulator_desc_s2mps14_ldo(11, MIN_800_MV, STEP_25_MV),
  580. regulator_desc_s2mps14_ldo(12, MIN_1800_MV, STEP_25_MV),
  581. regulator_desc_s2mps14_ldo(13, MIN_1800_MV, STEP_25_MV),
  582. regulator_desc_s2mps14_ldo(14, MIN_1800_MV, STEP_25_MV),
  583. regulator_desc_s2mps14_ldo(15, MIN_1800_MV, STEP_25_MV),
  584. regulator_desc_s2mps14_ldo(16, MIN_1800_MV, STEP_25_MV),
  585. regulator_desc_s2mps14_ldo(17, MIN_1800_MV, STEP_25_MV),
  586. regulator_desc_s2mps14_ldo(18, MIN_1800_MV, STEP_25_MV),
  587. regulator_desc_s2mps14_ldo(19, MIN_800_MV, STEP_25_MV),
  588. regulator_desc_s2mps14_ldo(20, MIN_800_MV, STEP_25_MV),
  589. regulator_desc_s2mps14_ldo(21, MIN_800_MV, STEP_25_MV),
  590. regulator_desc_s2mps14_ldo(22, MIN_800_MV, STEP_12_5_MV),
  591. regulator_desc_s2mps14_ldo(23, MIN_800_MV, STEP_25_MV),
  592. regulator_desc_s2mps14_ldo(24, MIN_1800_MV, STEP_25_MV),
  593. regulator_desc_s2mps14_ldo(25, MIN_1800_MV, STEP_25_MV),
  594. regulator_desc_s2mps14_buck(1, MIN_600_MV, STEP_6_25_MV,
  595. S2MPS14_BUCK1235_START_SEL),
  596. regulator_desc_s2mps14_buck(2, MIN_600_MV, STEP_6_25_MV,
  597. S2MPS14_BUCK1235_START_SEL),
  598. regulator_desc_s2mps14_buck(3, MIN_600_MV, STEP_6_25_MV,
  599. S2MPS14_BUCK1235_START_SEL),
  600. regulator_desc_s2mps14_buck(4, MIN_1400_MV, STEP_12_5_MV,
  601. S2MPS14_BUCK4_START_SEL),
  602. regulator_desc_s2mps14_buck(5, MIN_600_MV, STEP_6_25_MV,
  603. S2MPS14_BUCK1235_START_SEL),
  604. };
  605. static int s2mps14_pmic_enable_ext_control(struct s2mps11_info *s2mps11,
  606. struct regulator_dev *rdev)
  607. {
  608. return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
  609. rdev->desc->enable_mask, S2MPS14_ENABLE_EXT_CONTROL);
  610. }
  611. static void s2mps14_pmic_dt_parse_ext_control_gpio(struct platform_device *pdev,
  612. struct of_regulator_match *rdata, struct s2mps11_info *s2mps11)
  613. {
  614. int *gpio = s2mps11->ext_control_gpio;
  615. unsigned int i;
  616. unsigned int valid_regulators[3] = { S2MPS14_LDO10, S2MPS14_LDO11,
  617. S2MPS14_LDO12 };
  618. for (i = 0; i < ARRAY_SIZE(valid_regulators); i++) {
  619. unsigned int reg = valid_regulators[i];
  620. if (!rdata[reg].init_data || !rdata[reg].of_node)
  621. continue;
  622. gpio[reg] = of_get_named_gpio(rdata[reg].of_node,
  623. "samsung,ext-control-gpios", 0);
  624. if (gpio_is_valid(gpio[reg]))
  625. dev_dbg(&pdev->dev, "Using GPIO %d for ext-control over %d/%s\n",
  626. gpio[reg], reg, rdata[reg].name);
  627. }
  628. }
  629. static int s2mps11_pmic_dt_parse(struct platform_device *pdev,
  630. struct of_regulator_match *rdata, struct s2mps11_info *s2mps11)
  631. {
  632. struct device_node *reg_np;
  633. reg_np = of_get_child_by_name(pdev->dev.parent->of_node, "regulators");
  634. if (!reg_np) {
  635. dev_err(&pdev->dev, "could not find regulators sub-node\n");
  636. return -EINVAL;
  637. }
  638. of_regulator_match(&pdev->dev, reg_np, rdata, s2mps11->rdev_num);
  639. if (s2mps11->dev_type == S2MPS14X)
  640. s2mps14_pmic_dt_parse_ext_control_gpio(pdev, rdata, s2mps11);
  641. of_node_put(reg_np);
  642. return 0;
  643. }
  644. static int s2mpu02_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay)
  645. {
  646. unsigned int ramp_val, ramp_shift, ramp_reg;
  647. switch (rdev_get_id(rdev)) {
  648. case S2MPU02_BUCK1:
  649. ramp_shift = S2MPU02_BUCK1_RAMP_SHIFT;
  650. break;
  651. case S2MPU02_BUCK2:
  652. ramp_shift = S2MPU02_BUCK2_RAMP_SHIFT;
  653. break;
  654. case S2MPU02_BUCK3:
  655. ramp_shift = S2MPU02_BUCK3_RAMP_SHIFT;
  656. break;
  657. case S2MPU02_BUCK4:
  658. ramp_shift = S2MPU02_BUCK4_RAMP_SHIFT;
  659. break;
  660. default:
  661. return 0;
  662. }
  663. ramp_reg = S2MPU02_REG_RAMP1;
  664. ramp_val = get_ramp_delay(ramp_delay);
  665. return regmap_update_bits(rdev->regmap, ramp_reg,
  666. S2MPU02_BUCK1234_RAMP_MASK << ramp_shift,
  667. ramp_val << ramp_shift);
  668. }
  669. static struct regulator_ops s2mpu02_ldo_ops = {
  670. .list_voltage = regulator_list_voltage_linear,
  671. .map_voltage = regulator_map_voltage_linear,
  672. .is_enabled = regulator_is_enabled_regmap,
  673. .enable = s2mps14_regulator_enable,
  674. .disable = regulator_disable_regmap,
  675. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  676. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  677. .set_voltage_time_sel = regulator_set_voltage_time_sel,
  678. .set_suspend_disable = s2mps14_regulator_set_suspend_disable,
  679. };
  680. static struct regulator_ops s2mpu02_buck_ops = {
  681. .list_voltage = regulator_list_voltage_linear,
  682. .map_voltage = regulator_map_voltage_linear,
  683. .is_enabled = regulator_is_enabled_regmap,
  684. .enable = s2mps14_regulator_enable,
  685. .disable = regulator_disable_regmap,
  686. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  687. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  688. .set_voltage_time_sel = regulator_set_voltage_time_sel,
  689. .set_suspend_disable = s2mps14_regulator_set_suspend_disable,
  690. .set_ramp_delay = s2mpu02_set_ramp_delay,
  691. };
  692. #define regulator_desc_s2mpu02_ldo1(num) { \
  693. .name = "LDO"#num, \
  694. .id = S2MPU02_LDO##num, \
  695. .ops = &s2mpu02_ldo_ops, \
  696. .type = REGULATOR_VOLTAGE, \
  697. .owner = THIS_MODULE, \
  698. .min_uV = S2MPU02_LDO_MIN_900MV, \
  699. .uV_step = S2MPU02_LDO_STEP_12_5MV, \
  700. .linear_min_sel = S2MPU02_LDO_GROUP1_START_SEL, \
  701. .n_voltages = S2MPU02_LDO_N_VOLTAGES, \
  702. .vsel_reg = S2MPU02_REG_L1CTRL, \
  703. .vsel_mask = S2MPU02_LDO_VSEL_MASK, \
  704. .enable_reg = S2MPU02_REG_L1CTRL, \
  705. .enable_mask = S2MPU02_ENABLE_MASK \
  706. }
  707. #define regulator_desc_s2mpu02_ldo2(num) { \
  708. .name = "LDO"#num, \
  709. .id = S2MPU02_LDO##num, \
  710. .ops = &s2mpu02_ldo_ops, \
  711. .type = REGULATOR_VOLTAGE, \
  712. .owner = THIS_MODULE, \
  713. .min_uV = S2MPU02_LDO_MIN_1050MV, \
  714. .uV_step = S2MPU02_LDO_STEP_25MV, \
  715. .linear_min_sel = S2MPU02_LDO_GROUP2_START_SEL, \
  716. .n_voltages = S2MPU02_LDO_N_VOLTAGES, \
  717. .vsel_reg = S2MPU02_REG_L2CTRL1, \
  718. .vsel_mask = S2MPU02_LDO_VSEL_MASK, \
  719. .enable_reg = S2MPU02_REG_L2CTRL1, \
  720. .enable_mask = S2MPU02_ENABLE_MASK \
  721. }
  722. #define regulator_desc_s2mpu02_ldo3(num) { \
  723. .name = "LDO"#num, \
  724. .id = S2MPU02_LDO##num, \
  725. .ops = &s2mpu02_ldo_ops, \
  726. .type = REGULATOR_VOLTAGE, \
  727. .owner = THIS_MODULE, \
  728. .min_uV = S2MPU02_LDO_MIN_900MV, \
  729. .uV_step = S2MPU02_LDO_STEP_12_5MV, \
  730. .linear_min_sel = S2MPU02_LDO_GROUP1_START_SEL, \
  731. .n_voltages = S2MPU02_LDO_N_VOLTAGES, \
  732. .vsel_reg = S2MPU02_REG_L3CTRL + num - 3, \
  733. .vsel_mask = S2MPU02_LDO_VSEL_MASK, \
  734. .enable_reg = S2MPU02_REG_L3CTRL + num - 3, \
  735. .enable_mask = S2MPU02_ENABLE_MASK \
  736. }
  737. #define regulator_desc_s2mpu02_ldo4(num) { \
  738. .name = "LDO"#num, \
  739. .id = S2MPU02_LDO##num, \
  740. .ops = &s2mpu02_ldo_ops, \
  741. .type = REGULATOR_VOLTAGE, \
  742. .owner = THIS_MODULE, \
  743. .min_uV = S2MPU02_LDO_MIN_1050MV, \
  744. .uV_step = S2MPU02_LDO_STEP_25MV, \
  745. .linear_min_sel = S2MPU02_LDO_GROUP2_START_SEL, \
  746. .n_voltages = S2MPU02_LDO_N_VOLTAGES, \
  747. .vsel_reg = S2MPU02_REG_L3CTRL + num - 3, \
  748. .vsel_mask = S2MPU02_LDO_VSEL_MASK, \
  749. .enable_reg = S2MPU02_REG_L3CTRL + num - 3, \
  750. .enable_mask = S2MPU02_ENABLE_MASK \
  751. }
  752. #define regulator_desc_s2mpu02_ldo5(num) { \
  753. .name = "LDO"#num, \
  754. .id = S2MPU02_LDO##num, \
  755. .ops = &s2mpu02_ldo_ops, \
  756. .type = REGULATOR_VOLTAGE, \
  757. .owner = THIS_MODULE, \
  758. .min_uV = S2MPU02_LDO_MIN_1600MV, \
  759. .uV_step = S2MPU02_LDO_STEP_50MV, \
  760. .linear_min_sel = S2MPU02_LDO_GROUP3_START_SEL, \
  761. .n_voltages = S2MPU02_LDO_N_VOLTAGES, \
  762. .vsel_reg = S2MPU02_REG_L3CTRL + num - 3, \
  763. .vsel_mask = S2MPU02_LDO_VSEL_MASK, \
  764. .enable_reg = S2MPU02_REG_L3CTRL + num - 3, \
  765. .enable_mask = S2MPU02_ENABLE_MASK \
  766. }
  767. #define regulator_desc_s2mpu02_buck1234(num) { \
  768. .name = "BUCK"#num, \
  769. .id = S2MPU02_BUCK##num, \
  770. .ops = &s2mpu02_buck_ops, \
  771. .type = REGULATOR_VOLTAGE, \
  772. .owner = THIS_MODULE, \
  773. .min_uV = S2MPU02_BUCK1234_MIN_600MV, \
  774. .uV_step = S2MPU02_BUCK1234_STEP_6_25MV, \
  775. .n_voltages = S2MPU02_BUCK_N_VOLTAGES, \
  776. .linear_min_sel = S2MPU02_BUCK1234_START_SEL, \
  777. .ramp_delay = S2MPU02_BUCK_RAMP_DELAY, \
  778. .vsel_reg = S2MPU02_REG_B1CTRL2 + (num - 1) * 2, \
  779. .vsel_mask = S2MPU02_BUCK_VSEL_MASK, \
  780. .enable_reg = S2MPU02_REG_B1CTRL1 + (num - 1) * 2, \
  781. .enable_mask = S2MPU02_ENABLE_MASK \
  782. }
  783. #define regulator_desc_s2mpu02_buck5(num) { \
  784. .name = "BUCK"#num, \
  785. .id = S2MPU02_BUCK##num, \
  786. .ops = &s2mpu02_ldo_ops, \
  787. .type = REGULATOR_VOLTAGE, \
  788. .owner = THIS_MODULE, \
  789. .min_uV = S2MPU02_BUCK5_MIN_1081_25MV, \
  790. .uV_step = S2MPU02_BUCK5_STEP_6_25MV, \
  791. .n_voltages = S2MPU02_BUCK_N_VOLTAGES, \
  792. .linear_min_sel = S2MPU02_BUCK5_START_SEL, \
  793. .ramp_delay = S2MPU02_BUCK_RAMP_DELAY, \
  794. .vsel_reg = S2MPU02_REG_B5CTRL2, \
  795. .vsel_mask = S2MPU02_BUCK_VSEL_MASK, \
  796. .enable_reg = S2MPU02_REG_B5CTRL1, \
  797. .enable_mask = S2MPU02_ENABLE_MASK \
  798. }
  799. #define regulator_desc_s2mpu02_buck6(num) { \
  800. .name = "BUCK"#num, \
  801. .id = S2MPU02_BUCK##num, \
  802. .ops = &s2mpu02_ldo_ops, \
  803. .type = REGULATOR_VOLTAGE, \
  804. .owner = THIS_MODULE, \
  805. .min_uV = S2MPU02_BUCK6_MIN_1700MV, \
  806. .uV_step = S2MPU02_BUCK6_STEP_2_50MV, \
  807. .n_voltages = S2MPU02_BUCK_N_VOLTAGES, \
  808. .linear_min_sel = S2MPU02_BUCK6_START_SEL, \
  809. .ramp_delay = S2MPU02_BUCK_RAMP_DELAY, \
  810. .vsel_reg = S2MPU02_REG_B6CTRL2, \
  811. .vsel_mask = S2MPU02_BUCK_VSEL_MASK, \
  812. .enable_reg = S2MPU02_REG_B6CTRL1, \
  813. .enable_mask = S2MPU02_ENABLE_MASK \
  814. }
  815. #define regulator_desc_s2mpu02_buck7(num) { \
  816. .name = "BUCK"#num, \
  817. .id = S2MPU02_BUCK##num, \
  818. .ops = &s2mpu02_ldo_ops, \
  819. .type = REGULATOR_VOLTAGE, \
  820. .owner = THIS_MODULE, \
  821. .min_uV = S2MPU02_BUCK7_MIN_900MV, \
  822. .uV_step = S2MPU02_BUCK7_STEP_6_25MV, \
  823. .n_voltages = S2MPU02_BUCK_N_VOLTAGES, \
  824. .linear_min_sel = S2MPU02_BUCK7_START_SEL, \
  825. .ramp_delay = S2MPU02_BUCK_RAMP_DELAY, \
  826. .vsel_reg = S2MPU02_REG_B7CTRL2, \
  827. .vsel_mask = S2MPU02_BUCK_VSEL_MASK, \
  828. .enable_reg = S2MPU02_REG_B7CTRL1, \
  829. .enable_mask = S2MPU02_ENABLE_MASK \
  830. }
  831. static const struct regulator_desc s2mpu02_regulators[] = {
  832. regulator_desc_s2mpu02_ldo1(1),
  833. regulator_desc_s2mpu02_ldo2(2),
  834. regulator_desc_s2mpu02_ldo4(3),
  835. regulator_desc_s2mpu02_ldo5(4),
  836. regulator_desc_s2mpu02_ldo4(5),
  837. regulator_desc_s2mpu02_ldo3(6),
  838. regulator_desc_s2mpu02_ldo3(7),
  839. regulator_desc_s2mpu02_ldo4(8),
  840. regulator_desc_s2mpu02_ldo5(9),
  841. regulator_desc_s2mpu02_ldo3(10),
  842. regulator_desc_s2mpu02_ldo4(11),
  843. regulator_desc_s2mpu02_ldo5(12),
  844. regulator_desc_s2mpu02_ldo5(13),
  845. regulator_desc_s2mpu02_ldo5(14),
  846. regulator_desc_s2mpu02_ldo5(15),
  847. regulator_desc_s2mpu02_ldo5(16),
  848. regulator_desc_s2mpu02_ldo4(17),
  849. regulator_desc_s2mpu02_ldo5(18),
  850. regulator_desc_s2mpu02_ldo3(19),
  851. regulator_desc_s2mpu02_ldo4(20),
  852. regulator_desc_s2mpu02_ldo5(21),
  853. regulator_desc_s2mpu02_ldo5(22),
  854. regulator_desc_s2mpu02_ldo5(23),
  855. regulator_desc_s2mpu02_ldo4(24),
  856. regulator_desc_s2mpu02_ldo5(25),
  857. regulator_desc_s2mpu02_ldo4(26),
  858. regulator_desc_s2mpu02_ldo5(27),
  859. regulator_desc_s2mpu02_ldo5(28),
  860. regulator_desc_s2mpu02_buck1234(1),
  861. regulator_desc_s2mpu02_buck1234(2),
  862. regulator_desc_s2mpu02_buck1234(3),
  863. regulator_desc_s2mpu02_buck1234(4),
  864. regulator_desc_s2mpu02_buck5(5),
  865. regulator_desc_s2mpu02_buck6(6),
  866. regulator_desc_s2mpu02_buck7(7),
  867. };
  868. static int s2mps11_pmic_probe(struct platform_device *pdev)
  869. {
  870. struct sec_pmic_dev *iodev = dev_get_drvdata(pdev->dev.parent);
  871. struct sec_platform_data *pdata = NULL;
  872. struct of_regulator_match *rdata = NULL;
  873. struct regulator_config config = { };
  874. struct s2mps11_info *s2mps11;
  875. int i, ret = 0;
  876. const struct regulator_desc *regulators;
  877. s2mps11 = devm_kzalloc(&pdev->dev, sizeof(struct s2mps11_info),
  878. GFP_KERNEL);
  879. if (!s2mps11)
  880. return -ENOMEM;
  881. s2mps11->dev_type = platform_get_device_id(pdev)->driver_data;
  882. switch (s2mps11->dev_type) {
  883. case S2MPS11X:
  884. s2mps11->rdev_num = ARRAY_SIZE(s2mps11_regulators);
  885. regulators = s2mps11_regulators;
  886. break;
  887. case S2MPS13X:
  888. s2mps11->rdev_num = ARRAY_SIZE(s2mps13_regulators);
  889. regulators = s2mps13_regulators;
  890. break;
  891. case S2MPS14X:
  892. s2mps11->rdev_num = ARRAY_SIZE(s2mps14_regulators);
  893. regulators = s2mps14_regulators;
  894. break;
  895. case S2MPU02:
  896. s2mps11->rdev_num = ARRAY_SIZE(s2mpu02_regulators);
  897. regulators = s2mpu02_regulators;
  898. break;
  899. default:
  900. dev_err(&pdev->dev, "Invalid device type: %u\n",
  901. s2mps11->dev_type);
  902. return -EINVAL;
  903. }
  904. s2mps11->ext_control_gpio = devm_kmalloc(&pdev->dev,
  905. sizeof(*s2mps11->ext_control_gpio) * s2mps11->rdev_num,
  906. GFP_KERNEL);
  907. if (!s2mps11->ext_control_gpio)
  908. return -ENOMEM;
  909. /*
  910. * 0 is a valid GPIO so initialize all GPIO-s to negative value
  911. * to indicate that external control won't be used for this regulator.
  912. */
  913. for (i = 0; i < s2mps11->rdev_num; i++)
  914. s2mps11->ext_control_gpio[i] = -EINVAL;
  915. if (!iodev->dev->of_node) {
  916. if (iodev->pdata) {
  917. pdata = iodev->pdata;
  918. goto common_reg;
  919. } else {
  920. dev_err(pdev->dev.parent,
  921. "Platform data or DT node not supplied\n");
  922. return -ENODEV;
  923. }
  924. }
  925. rdata = kzalloc(sizeof(*rdata) * s2mps11->rdev_num, GFP_KERNEL);
  926. if (!rdata)
  927. return -ENOMEM;
  928. for (i = 0; i < s2mps11->rdev_num; i++)
  929. rdata[i].name = regulators[i].name;
  930. ret = s2mps11_pmic_dt_parse(pdev, rdata, s2mps11);
  931. if (ret)
  932. goto out;
  933. common_reg:
  934. platform_set_drvdata(pdev, s2mps11);
  935. config.dev = &pdev->dev;
  936. config.regmap = iodev->regmap_pmic;
  937. config.driver_data = s2mps11;
  938. config.ena_gpio_flags = GPIOF_OUT_INIT_HIGH;
  939. config.ena_gpio_initialized = true;
  940. for (i = 0; i < s2mps11->rdev_num; i++) {
  941. struct regulator_dev *regulator;
  942. if (pdata) {
  943. config.init_data = pdata->regulators[i].initdata;
  944. config.of_node = pdata->regulators[i].reg_node;
  945. } else {
  946. config.init_data = rdata[i].init_data;
  947. config.of_node = rdata[i].of_node;
  948. }
  949. config.ena_gpio = s2mps11->ext_control_gpio[i];
  950. regulator = devm_regulator_register(&pdev->dev,
  951. &regulators[i], &config);
  952. if (IS_ERR(regulator)) {
  953. ret = PTR_ERR(regulator);
  954. dev_err(&pdev->dev, "regulator init failed for %d\n",
  955. i);
  956. goto out;
  957. }
  958. if (gpio_is_valid(s2mps11->ext_control_gpio[i])) {
  959. ret = s2mps14_pmic_enable_ext_control(s2mps11,
  960. regulator);
  961. if (ret < 0) {
  962. dev_err(&pdev->dev,
  963. "failed to enable GPIO control over %s: %d\n",
  964. regulator->desc->name, ret);
  965. goto out;
  966. }
  967. }
  968. }
  969. out:
  970. kfree(rdata);
  971. return ret;
  972. }
  973. static const struct platform_device_id s2mps11_pmic_id[] = {
  974. { "s2mps11-pmic", S2MPS11X},
  975. { "s2mps13-pmic", S2MPS13X},
  976. { "s2mps14-pmic", S2MPS14X},
  977. { "s2mpu02-pmic", S2MPU02},
  978. { },
  979. };
  980. MODULE_DEVICE_TABLE(platform, s2mps11_pmic_id);
  981. static struct platform_driver s2mps11_pmic_driver = {
  982. .driver = {
  983. .name = "s2mps11-pmic",
  984. },
  985. .probe = s2mps11_pmic_probe,
  986. .id_table = s2mps11_pmic_id,
  987. };
  988. static int __init s2mps11_pmic_init(void)
  989. {
  990. return platform_driver_register(&s2mps11_pmic_driver);
  991. }
  992. subsys_initcall(s2mps11_pmic_init);
  993. static void __exit s2mps11_pmic_exit(void)
  994. {
  995. platform_driver_unregister(&s2mps11_pmic_driver);
  996. }
  997. module_exit(s2mps11_pmic_exit);
  998. /* Module information */
  999. MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>");
  1000. MODULE_DESCRIPTION("SAMSUNG S2MPS11/S2MPS14/S2MPU02 Regulator Driver");
  1001. MODULE_LICENSE("GPL");