s2mps11.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  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/s2mps14.h>
  33. struct s2mps11_info {
  34. unsigned int rdev_num;
  35. int ramp_delay2;
  36. int ramp_delay34;
  37. int ramp_delay5;
  38. int ramp_delay16;
  39. int ramp_delay7810;
  40. int ramp_delay9;
  41. /*
  42. * One bit for each S2MPS14 regulator whether the suspend mode
  43. * was enabled.
  44. */
  45. unsigned int s2mps14_suspend_state:30;
  46. /* Array of size rdev_num with GPIO-s for external sleep control */
  47. int *ext_control_gpio;
  48. };
  49. static int get_ramp_delay(int ramp_delay)
  50. {
  51. unsigned char cnt = 0;
  52. ramp_delay /= 6250;
  53. while (true) {
  54. ramp_delay = ramp_delay >> 1;
  55. if (ramp_delay == 0)
  56. break;
  57. cnt++;
  58. }
  59. if (cnt > 3)
  60. cnt = 3;
  61. return cnt;
  62. }
  63. static int s2mps11_regulator_set_voltage_time_sel(struct regulator_dev *rdev,
  64. unsigned int old_selector,
  65. unsigned int new_selector)
  66. {
  67. struct s2mps11_info *s2mps11 = rdev_get_drvdata(rdev);
  68. unsigned int ramp_delay = 0;
  69. int old_volt, new_volt;
  70. switch (rdev_get_id(rdev)) {
  71. case S2MPS11_BUCK2:
  72. ramp_delay = s2mps11->ramp_delay2;
  73. break;
  74. case S2MPS11_BUCK3:
  75. case S2MPS11_BUCK4:
  76. ramp_delay = s2mps11->ramp_delay34;
  77. break;
  78. case S2MPS11_BUCK5:
  79. ramp_delay = s2mps11->ramp_delay5;
  80. break;
  81. case S2MPS11_BUCK6:
  82. case S2MPS11_BUCK1:
  83. ramp_delay = s2mps11->ramp_delay16;
  84. break;
  85. case S2MPS11_BUCK7:
  86. case S2MPS11_BUCK8:
  87. case S2MPS11_BUCK10:
  88. ramp_delay = s2mps11->ramp_delay7810;
  89. break;
  90. case S2MPS11_BUCK9:
  91. ramp_delay = s2mps11->ramp_delay9;
  92. }
  93. if (ramp_delay == 0)
  94. ramp_delay = rdev->desc->ramp_delay;
  95. old_volt = rdev->desc->min_uV + (rdev->desc->uV_step * old_selector);
  96. new_volt = rdev->desc->min_uV + (rdev->desc->uV_step * new_selector);
  97. return DIV_ROUND_UP(abs(new_volt - old_volt), ramp_delay);
  98. }
  99. static int s2mps11_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay)
  100. {
  101. struct s2mps11_info *s2mps11 = rdev_get_drvdata(rdev);
  102. unsigned int ramp_val, ramp_shift, ramp_reg = S2MPS11_REG_RAMP_BUCK;
  103. unsigned int ramp_enable = 1, enable_shift = 0;
  104. int ret;
  105. switch (rdev_get_id(rdev)) {
  106. case S2MPS11_BUCK1:
  107. if (ramp_delay > s2mps11->ramp_delay16)
  108. s2mps11->ramp_delay16 = ramp_delay;
  109. else
  110. ramp_delay = s2mps11->ramp_delay16;
  111. ramp_shift = S2MPS11_BUCK16_RAMP_SHIFT;
  112. break;
  113. case S2MPS11_BUCK2:
  114. enable_shift = S2MPS11_BUCK2_RAMP_EN_SHIFT;
  115. if (!ramp_delay) {
  116. ramp_enable = 0;
  117. break;
  118. }
  119. s2mps11->ramp_delay2 = ramp_delay;
  120. ramp_shift = S2MPS11_BUCK2_RAMP_SHIFT;
  121. ramp_reg = S2MPS11_REG_RAMP;
  122. break;
  123. case S2MPS11_BUCK3:
  124. enable_shift = S2MPS11_BUCK3_RAMP_EN_SHIFT;
  125. if (!ramp_delay) {
  126. ramp_enable = 0;
  127. break;
  128. }
  129. if (ramp_delay > s2mps11->ramp_delay34)
  130. s2mps11->ramp_delay34 = ramp_delay;
  131. else
  132. ramp_delay = s2mps11->ramp_delay34;
  133. ramp_shift = S2MPS11_BUCK34_RAMP_SHIFT;
  134. ramp_reg = S2MPS11_REG_RAMP;
  135. break;
  136. case S2MPS11_BUCK4:
  137. enable_shift = S2MPS11_BUCK4_RAMP_EN_SHIFT;
  138. if (!ramp_delay) {
  139. ramp_enable = 0;
  140. break;
  141. }
  142. if (ramp_delay > s2mps11->ramp_delay34)
  143. s2mps11->ramp_delay34 = ramp_delay;
  144. else
  145. ramp_delay = s2mps11->ramp_delay34;
  146. ramp_shift = S2MPS11_BUCK34_RAMP_SHIFT;
  147. ramp_reg = S2MPS11_REG_RAMP;
  148. break;
  149. case S2MPS11_BUCK5:
  150. s2mps11->ramp_delay5 = ramp_delay;
  151. ramp_shift = S2MPS11_BUCK5_RAMP_SHIFT;
  152. break;
  153. case S2MPS11_BUCK6:
  154. enable_shift = S2MPS11_BUCK6_RAMP_EN_SHIFT;
  155. if (!ramp_delay) {
  156. ramp_enable = 0;
  157. break;
  158. }
  159. if (ramp_delay > s2mps11->ramp_delay16)
  160. s2mps11->ramp_delay16 = ramp_delay;
  161. else
  162. ramp_delay = s2mps11->ramp_delay16;
  163. ramp_shift = S2MPS11_BUCK16_RAMP_SHIFT;
  164. break;
  165. case S2MPS11_BUCK7:
  166. case S2MPS11_BUCK8:
  167. case S2MPS11_BUCK10:
  168. if (ramp_delay > s2mps11->ramp_delay7810)
  169. s2mps11->ramp_delay7810 = ramp_delay;
  170. else
  171. ramp_delay = s2mps11->ramp_delay7810;
  172. ramp_shift = S2MPS11_BUCK7810_RAMP_SHIFT;
  173. break;
  174. case S2MPS11_BUCK9:
  175. s2mps11->ramp_delay9 = ramp_delay;
  176. ramp_shift = S2MPS11_BUCK9_RAMP_SHIFT;
  177. break;
  178. default:
  179. return 0;
  180. }
  181. if (!ramp_enable)
  182. goto ramp_disable;
  183. /* Ramp delay can be enabled/disabled only for buck[2346] */
  184. if ((rdev_get_id(rdev) >= S2MPS11_BUCK2 &&
  185. rdev_get_id(rdev) <= S2MPS11_BUCK4) ||
  186. rdev_get_id(rdev) == S2MPS11_BUCK6) {
  187. ret = regmap_update_bits(rdev->regmap, S2MPS11_REG_RAMP,
  188. 1 << enable_shift, 1 << enable_shift);
  189. if (ret) {
  190. dev_err(&rdev->dev, "failed to enable ramp rate\n");
  191. return ret;
  192. }
  193. }
  194. ramp_val = get_ramp_delay(ramp_delay);
  195. return regmap_update_bits(rdev->regmap, ramp_reg, 0x3 << ramp_shift,
  196. ramp_val << ramp_shift);
  197. ramp_disable:
  198. return regmap_update_bits(rdev->regmap, S2MPS11_REG_RAMP,
  199. 1 << enable_shift, 0);
  200. }
  201. static struct regulator_ops s2mps11_ldo_ops = {
  202. .list_voltage = regulator_list_voltage_linear,
  203. .map_voltage = regulator_map_voltage_linear,
  204. .is_enabled = regulator_is_enabled_regmap,
  205. .enable = regulator_enable_regmap,
  206. .disable = regulator_disable_regmap,
  207. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  208. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  209. .set_voltage_time_sel = regulator_set_voltage_time_sel,
  210. };
  211. static struct regulator_ops s2mps11_buck_ops = {
  212. .list_voltage = regulator_list_voltage_linear,
  213. .map_voltage = regulator_map_voltage_linear,
  214. .is_enabled = regulator_is_enabled_regmap,
  215. .enable = regulator_enable_regmap,
  216. .disable = regulator_disable_regmap,
  217. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  218. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  219. .set_voltage_time_sel = s2mps11_regulator_set_voltage_time_sel,
  220. .set_ramp_delay = s2mps11_set_ramp_delay,
  221. };
  222. #define regulator_desc_s2mps11_ldo1(num) { \
  223. .name = "LDO"#num, \
  224. .id = S2MPS11_LDO##num, \
  225. .ops = &s2mps11_ldo_ops, \
  226. .type = REGULATOR_VOLTAGE, \
  227. .owner = THIS_MODULE, \
  228. .min_uV = S2MPS11_LDO_MIN, \
  229. .uV_step = S2MPS11_LDO_STEP1, \
  230. .n_voltages = S2MPS11_LDO_N_VOLTAGES, \
  231. .vsel_reg = S2MPS11_REG_L1CTRL + num - 1, \
  232. .vsel_mask = S2MPS11_LDO_VSEL_MASK, \
  233. .enable_reg = S2MPS11_REG_L1CTRL + num - 1, \
  234. .enable_mask = S2MPS11_ENABLE_MASK \
  235. }
  236. #define regulator_desc_s2mps11_ldo2(num) { \
  237. .name = "LDO"#num, \
  238. .id = S2MPS11_LDO##num, \
  239. .ops = &s2mps11_ldo_ops, \
  240. .type = REGULATOR_VOLTAGE, \
  241. .owner = THIS_MODULE, \
  242. .min_uV = S2MPS11_LDO_MIN, \
  243. .uV_step = S2MPS11_LDO_STEP2, \
  244. .n_voltages = S2MPS11_LDO_N_VOLTAGES, \
  245. .vsel_reg = S2MPS11_REG_L1CTRL + num - 1, \
  246. .vsel_mask = S2MPS11_LDO_VSEL_MASK, \
  247. .enable_reg = S2MPS11_REG_L1CTRL + num - 1, \
  248. .enable_mask = S2MPS11_ENABLE_MASK \
  249. }
  250. #define regulator_desc_s2mps11_buck1_4(num) { \
  251. .name = "BUCK"#num, \
  252. .id = S2MPS11_BUCK##num, \
  253. .ops = &s2mps11_buck_ops, \
  254. .type = REGULATOR_VOLTAGE, \
  255. .owner = THIS_MODULE, \
  256. .min_uV = S2MPS11_BUCK_MIN1, \
  257. .uV_step = S2MPS11_BUCK_STEP1, \
  258. .n_voltages = S2MPS11_BUCK_N_VOLTAGES, \
  259. .ramp_delay = S2MPS11_RAMP_DELAY, \
  260. .vsel_reg = S2MPS11_REG_B1CTRL2 + (num - 1) * 2, \
  261. .vsel_mask = S2MPS11_BUCK_VSEL_MASK, \
  262. .enable_reg = S2MPS11_REG_B1CTRL1 + (num - 1) * 2, \
  263. .enable_mask = S2MPS11_ENABLE_MASK \
  264. }
  265. #define regulator_desc_s2mps11_buck5 { \
  266. .name = "BUCK5", \
  267. .id = S2MPS11_BUCK5, \
  268. .ops = &s2mps11_buck_ops, \
  269. .type = REGULATOR_VOLTAGE, \
  270. .owner = THIS_MODULE, \
  271. .min_uV = S2MPS11_BUCK_MIN1, \
  272. .uV_step = S2MPS11_BUCK_STEP1, \
  273. .n_voltages = S2MPS11_BUCK_N_VOLTAGES, \
  274. .ramp_delay = S2MPS11_RAMP_DELAY, \
  275. .vsel_reg = S2MPS11_REG_B5CTRL2, \
  276. .vsel_mask = S2MPS11_BUCK_VSEL_MASK, \
  277. .enable_reg = S2MPS11_REG_B5CTRL1, \
  278. .enable_mask = S2MPS11_ENABLE_MASK \
  279. }
  280. #define regulator_desc_s2mps11_buck6_8(num) { \
  281. .name = "BUCK"#num, \
  282. .id = S2MPS11_BUCK##num, \
  283. .ops = &s2mps11_buck_ops, \
  284. .type = REGULATOR_VOLTAGE, \
  285. .owner = THIS_MODULE, \
  286. .min_uV = S2MPS11_BUCK_MIN1, \
  287. .uV_step = S2MPS11_BUCK_STEP1, \
  288. .n_voltages = S2MPS11_BUCK_N_VOLTAGES, \
  289. .ramp_delay = S2MPS11_RAMP_DELAY, \
  290. .vsel_reg = S2MPS11_REG_B6CTRL2 + (num - 6) * 2, \
  291. .vsel_mask = S2MPS11_BUCK_VSEL_MASK, \
  292. .enable_reg = S2MPS11_REG_B6CTRL1 + (num - 6) * 2, \
  293. .enable_mask = S2MPS11_ENABLE_MASK \
  294. }
  295. #define regulator_desc_s2mps11_buck9 { \
  296. .name = "BUCK9", \
  297. .id = S2MPS11_BUCK9, \
  298. .ops = &s2mps11_buck_ops, \
  299. .type = REGULATOR_VOLTAGE, \
  300. .owner = THIS_MODULE, \
  301. .min_uV = S2MPS11_BUCK_MIN3, \
  302. .uV_step = S2MPS11_BUCK_STEP3, \
  303. .n_voltages = S2MPS11_BUCK_N_VOLTAGES, \
  304. .ramp_delay = S2MPS11_RAMP_DELAY, \
  305. .vsel_reg = S2MPS11_REG_B9CTRL2, \
  306. .vsel_mask = S2MPS11_BUCK_VSEL_MASK, \
  307. .enable_reg = S2MPS11_REG_B9CTRL1, \
  308. .enable_mask = S2MPS11_ENABLE_MASK \
  309. }
  310. #define regulator_desc_s2mps11_buck10 { \
  311. .name = "BUCK10", \
  312. .id = S2MPS11_BUCK10, \
  313. .ops = &s2mps11_buck_ops, \
  314. .type = REGULATOR_VOLTAGE, \
  315. .owner = THIS_MODULE, \
  316. .min_uV = S2MPS11_BUCK_MIN2, \
  317. .uV_step = S2MPS11_BUCK_STEP2, \
  318. .n_voltages = S2MPS11_BUCK_N_VOLTAGES, \
  319. .ramp_delay = S2MPS11_RAMP_DELAY, \
  320. .vsel_reg = S2MPS11_REG_B10CTRL2, \
  321. .vsel_mask = S2MPS11_BUCK_VSEL_MASK, \
  322. .enable_reg = S2MPS11_REG_B10CTRL1, \
  323. .enable_mask = S2MPS11_ENABLE_MASK \
  324. }
  325. static const struct regulator_desc s2mps11_regulators[] = {
  326. regulator_desc_s2mps11_ldo2(1),
  327. regulator_desc_s2mps11_ldo1(2),
  328. regulator_desc_s2mps11_ldo1(3),
  329. regulator_desc_s2mps11_ldo1(4),
  330. regulator_desc_s2mps11_ldo1(5),
  331. regulator_desc_s2mps11_ldo2(6),
  332. regulator_desc_s2mps11_ldo1(7),
  333. regulator_desc_s2mps11_ldo1(8),
  334. regulator_desc_s2mps11_ldo1(9),
  335. regulator_desc_s2mps11_ldo1(10),
  336. regulator_desc_s2mps11_ldo2(11),
  337. regulator_desc_s2mps11_ldo1(12),
  338. regulator_desc_s2mps11_ldo1(13),
  339. regulator_desc_s2mps11_ldo1(14),
  340. regulator_desc_s2mps11_ldo1(15),
  341. regulator_desc_s2mps11_ldo1(16),
  342. regulator_desc_s2mps11_ldo1(17),
  343. regulator_desc_s2mps11_ldo1(18),
  344. regulator_desc_s2mps11_ldo1(19),
  345. regulator_desc_s2mps11_ldo1(20),
  346. regulator_desc_s2mps11_ldo1(21),
  347. regulator_desc_s2mps11_ldo2(22),
  348. regulator_desc_s2mps11_ldo2(23),
  349. regulator_desc_s2mps11_ldo1(24),
  350. regulator_desc_s2mps11_ldo1(25),
  351. regulator_desc_s2mps11_ldo1(26),
  352. regulator_desc_s2mps11_ldo2(27),
  353. regulator_desc_s2mps11_ldo1(28),
  354. regulator_desc_s2mps11_ldo1(29),
  355. regulator_desc_s2mps11_ldo1(30),
  356. regulator_desc_s2mps11_ldo1(31),
  357. regulator_desc_s2mps11_ldo1(32),
  358. regulator_desc_s2mps11_ldo1(33),
  359. regulator_desc_s2mps11_ldo1(34),
  360. regulator_desc_s2mps11_ldo1(35),
  361. regulator_desc_s2mps11_ldo1(36),
  362. regulator_desc_s2mps11_ldo1(37),
  363. regulator_desc_s2mps11_ldo1(38),
  364. regulator_desc_s2mps11_buck1_4(1),
  365. regulator_desc_s2mps11_buck1_4(2),
  366. regulator_desc_s2mps11_buck1_4(3),
  367. regulator_desc_s2mps11_buck1_4(4),
  368. regulator_desc_s2mps11_buck5,
  369. regulator_desc_s2mps11_buck6_8(6),
  370. regulator_desc_s2mps11_buck6_8(7),
  371. regulator_desc_s2mps11_buck6_8(8),
  372. regulator_desc_s2mps11_buck9,
  373. regulator_desc_s2mps11_buck10,
  374. };
  375. static int s2mps14_regulator_enable(struct regulator_dev *rdev)
  376. {
  377. struct s2mps11_info *s2mps11 = rdev_get_drvdata(rdev);
  378. unsigned int val;
  379. if (s2mps11->s2mps14_suspend_state & (1 << rdev_get_id(rdev)))
  380. val = S2MPS14_ENABLE_SUSPEND;
  381. else if (gpio_is_valid(s2mps11->ext_control_gpio[rdev_get_id(rdev)]))
  382. val = S2MPS14_ENABLE_EXT_CONTROL;
  383. else
  384. val = rdev->desc->enable_mask;
  385. return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
  386. rdev->desc->enable_mask, val);
  387. }
  388. static int s2mps14_regulator_set_suspend_disable(struct regulator_dev *rdev)
  389. {
  390. int ret;
  391. unsigned int val;
  392. struct s2mps11_info *s2mps11 = rdev_get_drvdata(rdev);
  393. /* LDO3 should be always on and does not support suspend mode */
  394. if (rdev_get_id(rdev) == S2MPS14_LDO3)
  395. return 0;
  396. ret = regmap_read(rdev->regmap, rdev->desc->enable_reg, &val);
  397. if (ret < 0)
  398. return ret;
  399. s2mps11->s2mps14_suspend_state |= (1 << rdev_get_id(rdev));
  400. /*
  401. * Don't enable suspend mode if regulator is already disabled because
  402. * this would effectively for a short time turn on the regulator after
  403. * resuming.
  404. * However we still want to toggle the suspend_state bit for regulator
  405. * in case if it got enabled before suspending the system.
  406. */
  407. if (!(val & rdev->desc->enable_mask))
  408. return 0;
  409. return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
  410. rdev->desc->enable_mask, S2MPS14_ENABLE_SUSPEND);
  411. }
  412. static struct regulator_ops s2mps14_reg_ops = {
  413. .list_voltage = regulator_list_voltage_linear,
  414. .map_voltage = regulator_map_voltage_linear,
  415. .is_enabled = regulator_is_enabled_regmap,
  416. .enable = s2mps14_regulator_enable,
  417. .disable = regulator_disable_regmap,
  418. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  419. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  420. .set_voltage_time_sel = regulator_set_voltage_time_sel,
  421. .set_suspend_disable = s2mps14_regulator_set_suspend_disable,
  422. };
  423. #define regulator_desc_s2mps14_ldo1(num) { \
  424. .name = "LDO"#num, \
  425. .id = S2MPS14_LDO##num, \
  426. .ops = &s2mps14_reg_ops, \
  427. .type = REGULATOR_VOLTAGE, \
  428. .owner = THIS_MODULE, \
  429. .min_uV = S2MPS14_LDO_MIN_800MV, \
  430. .uV_step = S2MPS14_LDO_STEP_25MV, \
  431. .n_voltages = S2MPS14_LDO_N_VOLTAGES, \
  432. .vsel_reg = S2MPS14_REG_L1CTRL + num - 1, \
  433. .vsel_mask = S2MPS14_LDO_VSEL_MASK, \
  434. .enable_reg = S2MPS14_REG_L1CTRL + num - 1, \
  435. .enable_mask = S2MPS14_ENABLE_MASK \
  436. }
  437. #define regulator_desc_s2mps14_ldo2(num) { \
  438. .name = "LDO"#num, \
  439. .id = S2MPS14_LDO##num, \
  440. .ops = &s2mps14_reg_ops, \
  441. .type = REGULATOR_VOLTAGE, \
  442. .owner = THIS_MODULE, \
  443. .min_uV = S2MPS14_LDO_MIN_1800MV, \
  444. .uV_step = S2MPS14_LDO_STEP_25MV, \
  445. .n_voltages = S2MPS14_LDO_N_VOLTAGES, \
  446. .vsel_reg = S2MPS14_REG_L1CTRL + num - 1, \
  447. .vsel_mask = S2MPS14_LDO_VSEL_MASK, \
  448. .enable_reg = S2MPS14_REG_L1CTRL + num - 1, \
  449. .enable_mask = S2MPS14_ENABLE_MASK \
  450. }
  451. #define regulator_desc_s2mps14_ldo3(num) { \
  452. .name = "LDO"#num, \
  453. .id = S2MPS14_LDO##num, \
  454. .ops = &s2mps14_reg_ops, \
  455. .type = REGULATOR_VOLTAGE, \
  456. .owner = THIS_MODULE, \
  457. .min_uV = S2MPS14_LDO_MIN_800MV, \
  458. .uV_step = S2MPS14_LDO_STEP_12_5MV, \
  459. .n_voltages = S2MPS14_LDO_N_VOLTAGES, \
  460. .vsel_reg = S2MPS14_REG_L1CTRL + num - 1, \
  461. .vsel_mask = S2MPS14_LDO_VSEL_MASK, \
  462. .enable_reg = S2MPS14_REG_L1CTRL + num - 1, \
  463. .enable_mask = S2MPS14_ENABLE_MASK \
  464. }
  465. #define regulator_desc_s2mps14_buck1235(num) { \
  466. .name = "BUCK"#num, \
  467. .id = S2MPS14_BUCK##num, \
  468. .ops = &s2mps14_reg_ops, \
  469. .type = REGULATOR_VOLTAGE, \
  470. .owner = THIS_MODULE, \
  471. .min_uV = S2MPS14_BUCK1235_MIN_600MV, \
  472. .uV_step = S2MPS14_BUCK1235_STEP_6_25MV, \
  473. .n_voltages = S2MPS14_BUCK_N_VOLTAGES, \
  474. .linear_min_sel = S2MPS14_BUCK1235_START_SEL, \
  475. .ramp_delay = S2MPS14_BUCK_RAMP_DELAY, \
  476. .vsel_reg = S2MPS14_REG_B1CTRL2 + (num - 1) * 2, \
  477. .vsel_mask = S2MPS14_BUCK_VSEL_MASK, \
  478. .enable_reg = S2MPS14_REG_B1CTRL1 + (num - 1) * 2, \
  479. .enable_mask = S2MPS14_ENABLE_MASK \
  480. }
  481. #define regulator_desc_s2mps14_buck4(num) { \
  482. .name = "BUCK"#num, \
  483. .id = S2MPS14_BUCK##num, \
  484. .ops = &s2mps14_reg_ops, \
  485. .type = REGULATOR_VOLTAGE, \
  486. .owner = THIS_MODULE, \
  487. .min_uV = S2MPS14_BUCK4_MIN_1400MV, \
  488. .uV_step = S2MPS14_BUCK4_STEP_12_5MV, \
  489. .n_voltages = S2MPS14_BUCK_N_VOLTAGES, \
  490. .linear_min_sel = S2MPS14_BUCK4_START_SEL, \
  491. .ramp_delay = S2MPS14_BUCK_RAMP_DELAY, \
  492. .vsel_reg = S2MPS14_REG_B1CTRL2 + (num - 1) * 2, \
  493. .vsel_mask = S2MPS14_BUCK_VSEL_MASK, \
  494. .enable_reg = S2MPS14_REG_B1CTRL1 + (num - 1) * 2, \
  495. .enable_mask = S2MPS14_ENABLE_MASK \
  496. }
  497. static const struct regulator_desc s2mps14_regulators[] = {
  498. regulator_desc_s2mps14_ldo3(1),
  499. regulator_desc_s2mps14_ldo3(2),
  500. regulator_desc_s2mps14_ldo1(3),
  501. regulator_desc_s2mps14_ldo1(4),
  502. regulator_desc_s2mps14_ldo3(5),
  503. regulator_desc_s2mps14_ldo3(6),
  504. regulator_desc_s2mps14_ldo1(7),
  505. regulator_desc_s2mps14_ldo2(8),
  506. regulator_desc_s2mps14_ldo3(9),
  507. regulator_desc_s2mps14_ldo3(10),
  508. regulator_desc_s2mps14_ldo1(11),
  509. regulator_desc_s2mps14_ldo2(12),
  510. regulator_desc_s2mps14_ldo2(13),
  511. regulator_desc_s2mps14_ldo2(14),
  512. regulator_desc_s2mps14_ldo2(15),
  513. regulator_desc_s2mps14_ldo2(16),
  514. regulator_desc_s2mps14_ldo2(17),
  515. regulator_desc_s2mps14_ldo2(18),
  516. regulator_desc_s2mps14_ldo1(19),
  517. regulator_desc_s2mps14_ldo1(20),
  518. regulator_desc_s2mps14_ldo1(21),
  519. regulator_desc_s2mps14_ldo3(22),
  520. regulator_desc_s2mps14_ldo1(23),
  521. regulator_desc_s2mps14_ldo2(24),
  522. regulator_desc_s2mps14_ldo2(25),
  523. regulator_desc_s2mps14_buck1235(1),
  524. regulator_desc_s2mps14_buck1235(2),
  525. regulator_desc_s2mps14_buck1235(3),
  526. regulator_desc_s2mps14_buck4(4),
  527. regulator_desc_s2mps14_buck1235(5),
  528. };
  529. static int s2mps14_pmic_enable_ext_control(struct s2mps11_info *s2mps11,
  530. struct regulator_dev *rdev)
  531. {
  532. return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
  533. rdev->desc->enable_mask, S2MPS14_ENABLE_EXT_CONTROL);
  534. }
  535. static void s2mps14_pmic_dt_parse_ext_control_gpio(struct platform_device *pdev,
  536. struct of_regulator_match *rdata, struct s2mps11_info *s2mps11)
  537. {
  538. int *gpio = s2mps11->ext_control_gpio;
  539. unsigned int i;
  540. unsigned int valid_regulators[3] = { S2MPS14_LDO10, S2MPS14_LDO11,
  541. S2MPS14_LDO12 };
  542. for (i = 0; i < ARRAY_SIZE(valid_regulators); i++) {
  543. unsigned int reg = valid_regulators[i];
  544. if (!rdata[reg].init_data || !rdata[reg].of_node)
  545. continue;
  546. gpio[reg] = of_get_named_gpio(rdata[reg].of_node,
  547. "samsung,ext-control-gpios", 0);
  548. if (gpio_is_valid(gpio[reg]))
  549. dev_dbg(&pdev->dev, "Using GPIO %d for ext-control over %d/%s\n",
  550. gpio[reg], reg, rdata[reg].name);
  551. }
  552. }
  553. static int s2mps11_pmic_dt_parse(struct platform_device *pdev,
  554. struct of_regulator_match *rdata, struct s2mps11_info *s2mps11,
  555. enum sec_device_type dev_type)
  556. {
  557. struct device_node *reg_np;
  558. reg_np = of_get_child_by_name(pdev->dev.parent->of_node, "regulators");
  559. if (!reg_np) {
  560. dev_err(&pdev->dev, "could not find regulators sub-node\n");
  561. return -EINVAL;
  562. }
  563. of_regulator_match(&pdev->dev, reg_np, rdata, s2mps11->rdev_num);
  564. if (dev_type == S2MPS14X)
  565. s2mps14_pmic_dt_parse_ext_control_gpio(pdev, rdata, s2mps11);
  566. of_node_put(reg_np);
  567. return 0;
  568. }
  569. static int s2mps11_pmic_probe(struct platform_device *pdev)
  570. {
  571. struct sec_pmic_dev *iodev = dev_get_drvdata(pdev->dev.parent);
  572. struct sec_platform_data *pdata = NULL;
  573. struct of_regulator_match *rdata = NULL;
  574. struct regulator_config config = { };
  575. struct s2mps11_info *s2mps11;
  576. int i, ret = 0;
  577. const struct regulator_desc *regulators;
  578. enum sec_device_type dev_type;
  579. s2mps11 = devm_kzalloc(&pdev->dev, sizeof(struct s2mps11_info),
  580. GFP_KERNEL);
  581. if (!s2mps11)
  582. return -ENOMEM;
  583. dev_type = platform_get_device_id(pdev)->driver_data;
  584. switch (dev_type) {
  585. case S2MPS11X:
  586. s2mps11->rdev_num = ARRAY_SIZE(s2mps11_regulators);
  587. regulators = s2mps11_regulators;
  588. break;
  589. case S2MPS14X:
  590. s2mps11->rdev_num = ARRAY_SIZE(s2mps14_regulators);
  591. regulators = s2mps14_regulators;
  592. break;
  593. default:
  594. dev_err(&pdev->dev, "Invalid device type: %u\n", dev_type);
  595. return -EINVAL;
  596. };
  597. s2mps11->ext_control_gpio = devm_kzalloc(&pdev->dev,
  598. sizeof(*s2mps11->ext_control_gpio) * s2mps11->rdev_num,
  599. GFP_KERNEL);
  600. if (!s2mps11->ext_control_gpio)
  601. return -ENOMEM;
  602. /*
  603. * 0 is a valid GPIO so initialize all GPIO-s to negative value
  604. * to indicate that external control won't be used for this regulator.
  605. */
  606. for (i = 0; i < s2mps11->rdev_num; i++)
  607. s2mps11->ext_control_gpio[i] = -EINVAL;
  608. if (!iodev->dev->of_node) {
  609. if (iodev->pdata) {
  610. pdata = iodev->pdata;
  611. goto common_reg;
  612. } else {
  613. dev_err(pdev->dev.parent,
  614. "Platform data or DT node not supplied\n");
  615. return -ENODEV;
  616. }
  617. }
  618. rdata = kzalloc(sizeof(*rdata) * s2mps11->rdev_num, GFP_KERNEL);
  619. if (!rdata)
  620. return -ENOMEM;
  621. for (i = 0; i < s2mps11->rdev_num; i++)
  622. rdata[i].name = regulators[i].name;
  623. ret = s2mps11_pmic_dt_parse(pdev, rdata, s2mps11, dev_type);
  624. if (ret)
  625. goto out;
  626. common_reg:
  627. platform_set_drvdata(pdev, s2mps11);
  628. config.dev = &pdev->dev;
  629. config.regmap = iodev->regmap_pmic;
  630. config.driver_data = s2mps11;
  631. config.ena_gpio_flags = GPIOF_OUT_INIT_HIGH;
  632. for (i = 0; i < s2mps11->rdev_num; i++) {
  633. struct regulator_dev *regulator;
  634. if (pdata) {
  635. config.init_data = pdata->regulators[i].initdata;
  636. config.of_node = pdata->regulators[i].reg_node;
  637. } else {
  638. config.init_data = rdata[i].init_data;
  639. config.of_node = rdata[i].of_node;
  640. }
  641. config.ena_gpio = s2mps11->ext_control_gpio[i];
  642. regulator = devm_regulator_register(&pdev->dev,
  643. &regulators[i], &config);
  644. if (IS_ERR(regulator)) {
  645. ret = PTR_ERR(regulator);
  646. dev_err(&pdev->dev, "regulator init failed for %d\n",
  647. i);
  648. goto out;
  649. }
  650. if (gpio_is_valid(s2mps11->ext_control_gpio[i])) {
  651. ret = s2mps14_pmic_enable_ext_control(s2mps11,
  652. regulator);
  653. if (ret < 0) {
  654. dev_err(&pdev->dev,
  655. "failed to enable GPIO control over %s: %d\n",
  656. regulator->desc->name, ret);
  657. goto out;
  658. }
  659. }
  660. }
  661. out:
  662. kfree(rdata);
  663. return ret;
  664. }
  665. static const struct platform_device_id s2mps11_pmic_id[] = {
  666. { "s2mps11-pmic", S2MPS11X},
  667. { "s2mps14-pmic", S2MPS14X},
  668. { },
  669. };
  670. MODULE_DEVICE_TABLE(platform, s2mps11_pmic_id);
  671. static struct platform_driver s2mps11_pmic_driver = {
  672. .driver = {
  673. .name = "s2mps11-pmic",
  674. .owner = THIS_MODULE,
  675. },
  676. .probe = s2mps11_pmic_probe,
  677. .id_table = s2mps11_pmic_id,
  678. };
  679. static int __init s2mps11_pmic_init(void)
  680. {
  681. return platform_driver_register(&s2mps11_pmic_driver);
  682. }
  683. subsys_initcall(s2mps11_pmic_init);
  684. static void __exit s2mps11_pmic_exit(void)
  685. {
  686. platform_driver_unregister(&s2mps11_pmic_driver);
  687. }
  688. module_exit(s2mps11_pmic_exit);
  689. /* Module information */
  690. MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>");
  691. MODULE_DESCRIPTION("SAMSUNG S2MPS11/S2MPS14 Regulator Driver");
  692. MODULE_LICENSE("GPL");