s2mps11.c 41 KB

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