s2mps11.c 35 KB

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