s5m8767.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016
  1. // SPDX-License-Identifier: GPL-2.0+
  2. //
  3. // Copyright (c) 2011 Samsung Electronics Co., Ltd
  4. // http://www.samsung.com
  5. #include <linux/err.h>
  6. #include <linux/of_gpio.h>
  7. #include <linux/gpio/consumer.h>
  8. #include <linux/module.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/regulator/driver.h>
  11. #include <linux/regulator/machine.h>
  12. #include <linux/mfd/samsung/core.h>
  13. #include <linux/mfd/samsung/s5m8767.h>
  14. #include <linux/regulator/of_regulator.h>
  15. #include <linux/regmap.h>
  16. #define S5M8767_OPMODE_NORMAL_MODE 0x1
  17. struct s5m8767_info {
  18. struct device *dev;
  19. struct sec_pmic_dev *iodev;
  20. int num_regulators;
  21. struct sec_opmode_data *opmode;
  22. int ramp_delay;
  23. bool buck2_ramp;
  24. bool buck3_ramp;
  25. bool buck4_ramp;
  26. bool buck2_gpiodvs;
  27. bool buck3_gpiodvs;
  28. bool buck4_gpiodvs;
  29. u8 buck2_vol[8];
  30. u8 buck3_vol[8];
  31. u8 buck4_vol[8];
  32. int buck_gpios[3];
  33. int buck_ds[3];
  34. int buck_gpioindex;
  35. };
  36. struct sec_voltage_desc {
  37. int max;
  38. int min;
  39. int step;
  40. };
  41. static const struct sec_voltage_desc buck_voltage_val1 = {
  42. .max = 2225000,
  43. .min = 650000,
  44. .step = 6250,
  45. };
  46. static const struct sec_voltage_desc buck_voltage_val2 = {
  47. .max = 1600000,
  48. .min = 600000,
  49. .step = 6250,
  50. };
  51. static const struct sec_voltage_desc buck_voltage_val3 = {
  52. .max = 3000000,
  53. .min = 750000,
  54. .step = 12500,
  55. };
  56. static const struct sec_voltage_desc ldo_voltage_val1 = {
  57. .max = 3950000,
  58. .min = 800000,
  59. .step = 50000,
  60. };
  61. static const struct sec_voltage_desc ldo_voltage_val2 = {
  62. .max = 2375000,
  63. .min = 800000,
  64. .step = 25000,
  65. };
  66. static const struct sec_voltage_desc *reg_voltage_map[] = {
  67. [S5M8767_LDO1] = &ldo_voltage_val2,
  68. [S5M8767_LDO2] = &ldo_voltage_val2,
  69. [S5M8767_LDO3] = &ldo_voltage_val1,
  70. [S5M8767_LDO4] = &ldo_voltage_val1,
  71. [S5M8767_LDO5] = &ldo_voltage_val1,
  72. [S5M8767_LDO6] = &ldo_voltage_val2,
  73. [S5M8767_LDO7] = &ldo_voltage_val2,
  74. [S5M8767_LDO8] = &ldo_voltage_val2,
  75. [S5M8767_LDO9] = &ldo_voltage_val1,
  76. [S5M8767_LDO10] = &ldo_voltage_val1,
  77. [S5M8767_LDO11] = &ldo_voltage_val1,
  78. [S5M8767_LDO12] = &ldo_voltage_val1,
  79. [S5M8767_LDO13] = &ldo_voltage_val1,
  80. [S5M8767_LDO14] = &ldo_voltage_val1,
  81. [S5M8767_LDO15] = &ldo_voltage_val2,
  82. [S5M8767_LDO16] = &ldo_voltage_val1,
  83. [S5M8767_LDO17] = &ldo_voltage_val1,
  84. [S5M8767_LDO18] = &ldo_voltage_val1,
  85. [S5M8767_LDO19] = &ldo_voltage_val1,
  86. [S5M8767_LDO20] = &ldo_voltage_val1,
  87. [S5M8767_LDO21] = &ldo_voltage_val1,
  88. [S5M8767_LDO22] = &ldo_voltage_val1,
  89. [S5M8767_LDO23] = &ldo_voltage_val1,
  90. [S5M8767_LDO24] = &ldo_voltage_val1,
  91. [S5M8767_LDO25] = &ldo_voltage_val1,
  92. [S5M8767_LDO26] = &ldo_voltage_val1,
  93. [S5M8767_LDO27] = &ldo_voltage_val1,
  94. [S5M8767_LDO28] = &ldo_voltage_val1,
  95. [S5M8767_BUCK1] = &buck_voltage_val1,
  96. [S5M8767_BUCK2] = &buck_voltage_val2,
  97. [S5M8767_BUCK3] = &buck_voltage_val2,
  98. [S5M8767_BUCK4] = &buck_voltage_val2,
  99. [S5M8767_BUCK5] = &buck_voltage_val1,
  100. [S5M8767_BUCK6] = &buck_voltage_val1,
  101. [S5M8767_BUCK7] = &buck_voltage_val3,
  102. [S5M8767_BUCK8] = &buck_voltage_val3,
  103. [S5M8767_BUCK9] = &buck_voltage_val3,
  104. };
  105. static const unsigned int s5m8767_opmode_reg[][4] = {
  106. /* {OFF, ON, LOWPOWER, SUSPEND} */
  107. /* LDO1 ... LDO28 */
  108. {0x0, 0x3, 0x2, 0x1}, /* LDO1 */
  109. {0x0, 0x3, 0x2, 0x1},
  110. {0x0, 0x3, 0x2, 0x1},
  111. {0x0, 0x0, 0x0, 0x0},
  112. {0x0, 0x3, 0x2, 0x1}, /* LDO5 */
  113. {0x0, 0x3, 0x2, 0x1},
  114. {0x0, 0x3, 0x2, 0x1},
  115. {0x0, 0x3, 0x2, 0x1},
  116. {0x0, 0x3, 0x2, 0x1},
  117. {0x0, 0x3, 0x2, 0x1}, /* LDO10 */
  118. {0x0, 0x3, 0x2, 0x1},
  119. {0x0, 0x3, 0x2, 0x1},
  120. {0x0, 0x3, 0x2, 0x1},
  121. {0x0, 0x3, 0x2, 0x1},
  122. {0x0, 0x3, 0x2, 0x1}, /* LDO15 */
  123. {0x0, 0x3, 0x2, 0x1},
  124. {0x0, 0x3, 0x2, 0x1},
  125. {0x0, 0x0, 0x0, 0x0},
  126. {0x0, 0x3, 0x2, 0x1},
  127. {0x0, 0x3, 0x2, 0x1}, /* LDO20 */
  128. {0x0, 0x3, 0x2, 0x1},
  129. {0x0, 0x3, 0x2, 0x1},
  130. {0x0, 0x0, 0x0, 0x0},
  131. {0x0, 0x3, 0x2, 0x1},
  132. {0x0, 0x3, 0x2, 0x1}, /* LDO25 */
  133. {0x0, 0x3, 0x2, 0x1},
  134. {0x0, 0x3, 0x2, 0x1},
  135. {0x0, 0x3, 0x2, 0x1}, /* LDO28 */
  136. /* BUCK1 ... BUCK9 */
  137. {0x0, 0x3, 0x1, 0x1}, /* BUCK1 */
  138. {0x0, 0x3, 0x1, 0x1},
  139. {0x0, 0x3, 0x1, 0x1},
  140. {0x0, 0x3, 0x1, 0x1},
  141. {0x0, 0x3, 0x2, 0x1}, /* BUCK5 */
  142. {0x0, 0x3, 0x1, 0x1},
  143. {0x0, 0x3, 0x1, 0x1},
  144. {0x0, 0x3, 0x1, 0x1},
  145. {0x0, 0x3, 0x1, 0x1}, /* BUCK9 */
  146. };
  147. static int s5m8767_get_register(struct s5m8767_info *s5m8767, int reg_id,
  148. int *reg, int *enable_ctrl)
  149. {
  150. int i;
  151. unsigned int mode;
  152. switch (reg_id) {
  153. case S5M8767_LDO1 ... S5M8767_LDO2:
  154. *reg = S5M8767_REG_LDO1CTRL + (reg_id - S5M8767_LDO1);
  155. break;
  156. case S5M8767_LDO3 ... S5M8767_LDO28:
  157. *reg = S5M8767_REG_LDO3CTRL + (reg_id - S5M8767_LDO3);
  158. break;
  159. case S5M8767_BUCK1:
  160. *reg = S5M8767_REG_BUCK1CTRL1;
  161. break;
  162. case S5M8767_BUCK2 ... S5M8767_BUCK4:
  163. *reg = S5M8767_REG_BUCK2CTRL + (reg_id - S5M8767_BUCK2) * 9;
  164. break;
  165. case S5M8767_BUCK5:
  166. *reg = S5M8767_REG_BUCK5CTRL1;
  167. break;
  168. case S5M8767_BUCK6 ... S5M8767_BUCK9:
  169. *reg = S5M8767_REG_BUCK6CTRL1 + (reg_id - S5M8767_BUCK6) * 2;
  170. break;
  171. default:
  172. return -EINVAL;
  173. }
  174. for (i = 0; i < s5m8767->num_regulators; i++) {
  175. if (s5m8767->opmode[i].id == reg_id) {
  176. mode = s5m8767->opmode[i].mode;
  177. break;
  178. }
  179. }
  180. if (i >= s5m8767->num_regulators)
  181. return -EINVAL;
  182. *enable_ctrl = s5m8767_opmode_reg[reg_id][mode] << S5M8767_ENCTRL_SHIFT;
  183. return 0;
  184. }
  185. static int s5m8767_get_vsel_reg(int reg_id, struct s5m8767_info *s5m8767)
  186. {
  187. int reg;
  188. switch (reg_id) {
  189. case S5M8767_LDO1 ... S5M8767_LDO2:
  190. reg = S5M8767_REG_LDO1CTRL + (reg_id - S5M8767_LDO1);
  191. break;
  192. case S5M8767_LDO3 ... S5M8767_LDO28:
  193. reg = S5M8767_REG_LDO3CTRL + (reg_id - S5M8767_LDO3);
  194. break;
  195. case S5M8767_BUCK1:
  196. reg = S5M8767_REG_BUCK1CTRL2;
  197. break;
  198. case S5M8767_BUCK2:
  199. reg = S5M8767_REG_BUCK2DVS1;
  200. if (s5m8767->buck2_gpiodvs)
  201. reg += s5m8767->buck_gpioindex;
  202. break;
  203. case S5M8767_BUCK3:
  204. reg = S5M8767_REG_BUCK3DVS1;
  205. if (s5m8767->buck3_gpiodvs)
  206. reg += s5m8767->buck_gpioindex;
  207. break;
  208. case S5M8767_BUCK4:
  209. reg = S5M8767_REG_BUCK4DVS1;
  210. if (s5m8767->buck4_gpiodvs)
  211. reg += s5m8767->buck_gpioindex;
  212. break;
  213. case S5M8767_BUCK5:
  214. reg = S5M8767_REG_BUCK5CTRL2;
  215. break;
  216. case S5M8767_BUCK6 ... S5M8767_BUCK9:
  217. reg = S5M8767_REG_BUCK6CTRL2 + (reg_id - S5M8767_BUCK6) * 2;
  218. break;
  219. default:
  220. return -EINVAL;
  221. }
  222. return reg;
  223. }
  224. static int s5m8767_convert_voltage_to_sel(const struct sec_voltage_desc *desc,
  225. int min_vol)
  226. {
  227. int selector = 0;
  228. if (desc == NULL)
  229. return -EINVAL;
  230. if (min_vol > desc->max)
  231. return -EINVAL;
  232. if (min_vol < desc->min)
  233. min_vol = desc->min;
  234. selector = DIV_ROUND_UP(min_vol - desc->min, desc->step);
  235. if (desc->min + desc->step * selector > desc->max)
  236. return -EINVAL;
  237. return selector;
  238. }
  239. static inline int s5m8767_set_high(struct s5m8767_info *s5m8767)
  240. {
  241. int temp_index = s5m8767->buck_gpioindex;
  242. gpio_set_value(s5m8767->buck_gpios[0], (temp_index >> 2) & 0x1);
  243. gpio_set_value(s5m8767->buck_gpios[1], (temp_index >> 1) & 0x1);
  244. gpio_set_value(s5m8767->buck_gpios[2], temp_index & 0x1);
  245. return 0;
  246. }
  247. static inline int s5m8767_set_low(struct s5m8767_info *s5m8767)
  248. {
  249. int temp_index = s5m8767->buck_gpioindex;
  250. gpio_set_value(s5m8767->buck_gpios[2], temp_index & 0x1);
  251. gpio_set_value(s5m8767->buck_gpios[1], (temp_index >> 1) & 0x1);
  252. gpio_set_value(s5m8767->buck_gpios[0], (temp_index >> 2) & 0x1);
  253. return 0;
  254. }
  255. static int s5m8767_set_voltage_sel(struct regulator_dev *rdev,
  256. unsigned selector)
  257. {
  258. struct s5m8767_info *s5m8767 = rdev_get_drvdata(rdev);
  259. int reg_id = rdev_get_id(rdev);
  260. int old_index, index = 0;
  261. u8 *buck234_vol = NULL;
  262. switch (reg_id) {
  263. case S5M8767_LDO1 ... S5M8767_LDO28:
  264. break;
  265. case S5M8767_BUCK1 ... S5M8767_BUCK6:
  266. if (reg_id == S5M8767_BUCK2 && s5m8767->buck2_gpiodvs)
  267. buck234_vol = &s5m8767->buck2_vol[0];
  268. else if (reg_id == S5M8767_BUCK3 && s5m8767->buck3_gpiodvs)
  269. buck234_vol = &s5m8767->buck3_vol[0];
  270. else if (reg_id == S5M8767_BUCK4 && s5m8767->buck4_gpiodvs)
  271. buck234_vol = &s5m8767->buck4_vol[0];
  272. break;
  273. case S5M8767_BUCK7 ... S5M8767_BUCK8:
  274. return -EINVAL;
  275. case S5M8767_BUCK9:
  276. break;
  277. default:
  278. return -EINVAL;
  279. }
  280. /* buck234_vol != NULL means to control buck234 voltage via DVS GPIO */
  281. if (buck234_vol) {
  282. while (*buck234_vol != selector) {
  283. buck234_vol++;
  284. index++;
  285. }
  286. old_index = s5m8767->buck_gpioindex;
  287. s5m8767->buck_gpioindex = index;
  288. if (index > old_index)
  289. return s5m8767_set_high(s5m8767);
  290. else
  291. return s5m8767_set_low(s5m8767);
  292. } else {
  293. return regulator_set_voltage_sel_regmap(rdev, selector);
  294. }
  295. }
  296. static int s5m8767_set_voltage_time_sel(struct regulator_dev *rdev,
  297. unsigned int old_sel,
  298. unsigned int new_sel)
  299. {
  300. struct s5m8767_info *s5m8767 = rdev_get_drvdata(rdev);
  301. if ((old_sel < new_sel) && s5m8767->ramp_delay)
  302. return DIV_ROUND_UP(rdev->desc->uV_step * (new_sel - old_sel),
  303. s5m8767->ramp_delay * 1000);
  304. return 0;
  305. }
  306. static const struct regulator_ops s5m8767_ops = {
  307. .list_voltage = regulator_list_voltage_linear,
  308. .is_enabled = regulator_is_enabled_regmap,
  309. .enable = regulator_enable_regmap,
  310. .disable = regulator_disable_regmap,
  311. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  312. .set_voltage_sel = s5m8767_set_voltage_sel,
  313. .set_voltage_time_sel = s5m8767_set_voltage_time_sel,
  314. };
  315. static const struct regulator_ops s5m8767_buck78_ops = {
  316. .list_voltage = regulator_list_voltage_linear,
  317. .is_enabled = regulator_is_enabled_regmap,
  318. .enable = regulator_enable_regmap,
  319. .disable = regulator_disable_regmap,
  320. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  321. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  322. };
  323. #define s5m8767_regulator_desc(_name) { \
  324. .name = #_name, \
  325. .id = S5M8767_##_name, \
  326. .ops = &s5m8767_ops, \
  327. .type = REGULATOR_VOLTAGE, \
  328. .owner = THIS_MODULE, \
  329. }
  330. #define s5m8767_regulator_buck78_desc(_name) { \
  331. .name = #_name, \
  332. .id = S5M8767_##_name, \
  333. .ops = &s5m8767_buck78_ops, \
  334. .type = REGULATOR_VOLTAGE, \
  335. .owner = THIS_MODULE, \
  336. }
  337. static struct regulator_desc regulators[] = {
  338. s5m8767_regulator_desc(LDO1),
  339. s5m8767_regulator_desc(LDO2),
  340. s5m8767_regulator_desc(LDO3),
  341. s5m8767_regulator_desc(LDO4),
  342. s5m8767_regulator_desc(LDO5),
  343. s5m8767_regulator_desc(LDO6),
  344. s5m8767_regulator_desc(LDO7),
  345. s5m8767_regulator_desc(LDO8),
  346. s5m8767_regulator_desc(LDO9),
  347. s5m8767_regulator_desc(LDO10),
  348. s5m8767_regulator_desc(LDO11),
  349. s5m8767_regulator_desc(LDO12),
  350. s5m8767_regulator_desc(LDO13),
  351. s5m8767_regulator_desc(LDO14),
  352. s5m8767_regulator_desc(LDO15),
  353. s5m8767_regulator_desc(LDO16),
  354. s5m8767_regulator_desc(LDO17),
  355. s5m8767_regulator_desc(LDO18),
  356. s5m8767_regulator_desc(LDO19),
  357. s5m8767_regulator_desc(LDO20),
  358. s5m8767_regulator_desc(LDO21),
  359. s5m8767_regulator_desc(LDO22),
  360. s5m8767_regulator_desc(LDO23),
  361. s5m8767_regulator_desc(LDO24),
  362. s5m8767_regulator_desc(LDO25),
  363. s5m8767_regulator_desc(LDO26),
  364. s5m8767_regulator_desc(LDO27),
  365. s5m8767_regulator_desc(LDO28),
  366. s5m8767_regulator_desc(BUCK1),
  367. s5m8767_regulator_desc(BUCK2),
  368. s5m8767_regulator_desc(BUCK3),
  369. s5m8767_regulator_desc(BUCK4),
  370. s5m8767_regulator_desc(BUCK5),
  371. s5m8767_regulator_desc(BUCK6),
  372. s5m8767_regulator_buck78_desc(BUCK7),
  373. s5m8767_regulator_buck78_desc(BUCK8),
  374. s5m8767_regulator_desc(BUCK9),
  375. };
  376. /*
  377. * Enable GPIO control over BUCK9 in regulator_config for that regulator.
  378. */
  379. static void s5m8767_regulator_config_ext_control(struct s5m8767_info *s5m8767,
  380. struct sec_regulator_data *rdata,
  381. struct regulator_config *config)
  382. {
  383. int i, mode = 0;
  384. if (rdata->id != S5M8767_BUCK9)
  385. return;
  386. /* Check if opmode for regulator matches S5M8767_ENCTRL_USE_GPIO */
  387. for (i = 0; i < s5m8767->num_regulators; i++) {
  388. const struct sec_opmode_data *opmode = &s5m8767->opmode[i];
  389. if (opmode->id == rdata->id) {
  390. mode = s5m8767_opmode_reg[rdata->id][opmode->mode];
  391. break;
  392. }
  393. }
  394. if (mode != S5M8767_ENCTRL_USE_GPIO) {
  395. dev_warn(s5m8767->dev,
  396. "ext-control for %pOFn: mismatched op_mode (%x), ignoring\n",
  397. rdata->reg_node, mode);
  398. return;
  399. }
  400. if (!rdata->ext_control_gpiod) {
  401. dev_warn(s5m8767->dev,
  402. "ext-control for %pOFn: GPIO not valid, ignoring\n",
  403. rdata->reg_node);
  404. return;
  405. }
  406. config->ena_gpiod = rdata->ext_control_gpiod;
  407. }
  408. /*
  409. * Turn on GPIO control over BUCK9.
  410. */
  411. static int s5m8767_enable_ext_control(struct s5m8767_info *s5m8767,
  412. struct regulator_dev *rdev)
  413. {
  414. int id = rdev_get_id(rdev);
  415. int ret, reg, enable_ctrl;
  416. if (id != S5M8767_BUCK9)
  417. return -EINVAL;
  418. ret = s5m8767_get_register(s5m8767, id, &reg, &enable_ctrl);
  419. if (ret)
  420. return ret;
  421. return regmap_update_bits(s5m8767->iodev->regmap_pmic,
  422. reg, S5M8767_ENCTRL_MASK,
  423. S5M8767_ENCTRL_USE_GPIO << S5M8767_ENCTRL_SHIFT);
  424. }
  425. #ifdef CONFIG_OF
  426. static int s5m8767_pmic_dt_parse_dvs_gpio(struct sec_pmic_dev *iodev,
  427. struct sec_platform_data *pdata,
  428. struct device_node *pmic_np)
  429. {
  430. int i, gpio;
  431. for (i = 0; i < 3; i++) {
  432. gpio = of_get_named_gpio(pmic_np,
  433. "s5m8767,pmic-buck-dvs-gpios", i);
  434. if (!gpio_is_valid(gpio)) {
  435. dev_err(iodev->dev, "invalid gpio[%d]: %d\n", i, gpio);
  436. return -EINVAL;
  437. }
  438. pdata->buck_gpios[i] = gpio;
  439. }
  440. return 0;
  441. }
  442. static int s5m8767_pmic_dt_parse_ds_gpio(struct sec_pmic_dev *iodev,
  443. struct sec_platform_data *pdata,
  444. struct device_node *pmic_np)
  445. {
  446. int i, gpio;
  447. for (i = 0; i < 3; i++) {
  448. gpio = of_get_named_gpio(pmic_np,
  449. "s5m8767,pmic-buck-ds-gpios", i);
  450. if (!gpio_is_valid(gpio)) {
  451. dev_err(iodev->dev, "invalid gpio[%d]: %d\n", i, gpio);
  452. return -EINVAL;
  453. }
  454. pdata->buck_ds[i] = gpio;
  455. }
  456. return 0;
  457. }
  458. static int s5m8767_pmic_dt_parse_pdata(struct platform_device *pdev,
  459. struct sec_platform_data *pdata)
  460. {
  461. struct sec_pmic_dev *iodev = dev_get_drvdata(pdev->dev.parent);
  462. struct device_node *pmic_np, *regulators_np, *reg_np;
  463. struct sec_regulator_data *rdata;
  464. struct sec_opmode_data *rmode;
  465. unsigned int i, dvs_voltage_nr = 8, ret;
  466. pmic_np = iodev->dev->of_node;
  467. if (!pmic_np) {
  468. dev_err(iodev->dev, "could not find pmic sub-node\n");
  469. return -ENODEV;
  470. }
  471. regulators_np = of_get_child_by_name(pmic_np, "regulators");
  472. if (!regulators_np) {
  473. dev_err(iodev->dev, "could not find regulators sub-node\n");
  474. return -EINVAL;
  475. }
  476. /* count the number of regulators to be supported in pmic */
  477. pdata->num_regulators = of_get_child_count(regulators_np);
  478. rdata = devm_kcalloc(&pdev->dev,
  479. pdata->num_regulators, sizeof(*rdata),
  480. GFP_KERNEL);
  481. if (!rdata) {
  482. of_node_put(regulators_np);
  483. return -ENOMEM;
  484. }
  485. rmode = devm_kcalloc(&pdev->dev,
  486. pdata->num_regulators, sizeof(*rmode),
  487. GFP_KERNEL);
  488. if (!rmode) {
  489. of_node_put(regulators_np);
  490. return -ENOMEM;
  491. }
  492. pdata->regulators = rdata;
  493. pdata->opmode = rmode;
  494. for_each_child_of_node(regulators_np, reg_np) {
  495. for (i = 0; i < ARRAY_SIZE(regulators); i++)
  496. if (of_node_name_eq(reg_np, regulators[i].name))
  497. break;
  498. if (i == ARRAY_SIZE(regulators)) {
  499. dev_warn(iodev->dev,
  500. "don't know how to configure regulator %pOFn\n",
  501. reg_np);
  502. continue;
  503. }
  504. rdata->ext_control_gpiod = devm_fwnode_gpiod_get(
  505. &pdev->dev,
  506. of_fwnode_handle(reg_np),
  507. "s5m8767,pmic-ext-control",
  508. GPIOD_OUT_HIGH | GPIOD_FLAGS_BIT_NONEXCLUSIVE,
  509. "s5m8767");
  510. if (PTR_ERR(rdata->ext_control_gpiod) == -ENOENT) {
  511. rdata->ext_control_gpiod = NULL;
  512. } else if (IS_ERR(rdata->ext_control_gpiod)) {
  513. of_node_put(reg_np);
  514. of_node_put(regulators_np);
  515. return PTR_ERR(rdata->ext_control_gpiod);
  516. }
  517. rdata->id = i;
  518. rdata->initdata = of_get_regulator_init_data(
  519. &pdev->dev, reg_np,
  520. &regulators[i]);
  521. rdata->reg_node = reg_np;
  522. rdata++;
  523. rmode->id = i;
  524. if (of_property_read_u32(reg_np, "op_mode",
  525. &rmode->mode)) {
  526. dev_warn(iodev->dev,
  527. "no op_mode property at %pOF\n",
  528. reg_np);
  529. rmode->mode = S5M8767_OPMODE_NORMAL_MODE;
  530. }
  531. rmode++;
  532. }
  533. of_node_put(regulators_np);
  534. if (of_get_property(pmic_np, "s5m8767,pmic-buck2-uses-gpio-dvs", NULL)) {
  535. pdata->buck2_gpiodvs = true;
  536. if (of_property_read_u32_array(pmic_np,
  537. "s5m8767,pmic-buck2-dvs-voltage",
  538. pdata->buck2_voltage, dvs_voltage_nr)) {
  539. dev_err(iodev->dev, "buck2 voltages not specified\n");
  540. return -EINVAL;
  541. }
  542. }
  543. if (of_get_property(pmic_np, "s5m8767,pmic-buck3-uses-gpio-dvs", NULL)) {
  544. pdata->buck3_gpiodvs = true;
  545. if (of_property_read_u32_array(pmic_np,
  546. "s5m8767,pmic-buck3-dvs-voltage",
  547. pdata->buck3_voltage, dvs_voltage_nr)) {
  548. dev_err(iodev->dev, "buck3 voltages not specified\n");
  549. return -EINVAL;
  550. }
  551. }
  552. if (of_get_property(pmic_np, "s5m8767,pmic-buck4-uses-gpio-dvs", NULL)) {
  553. pdata->buck4_gpiodvs = true;
  554. if (of_property_read_u32_array(pmic_np,
  555. "s5m8767,pmic-buck4-dvs-voltage",
  556. pdata->buck4_voltage, dvs_voltage_nr)) {
  557. dev_err(iodev->dev, "buck4 voltages not specified\n");
  558. return -EINVAL;
  559. }
  560. }
  561. if (pdata->buck2_gpiodvs || pdata->buck3_gpiodvs ||
  562. pdata->buck4_gpiodvs) {
  563. ret = s5m8767_pmic_dt_parse_dvs_gpio(iodev, pdata, pmic_np);
  564. if (ret)
  565. return -EINVAL;
  566. if (of_property_read_u32(pmic_np,
  567. "s5m8767,pmic-buck-default-dvs-idx",
  568. &pdata->buck_default_idx)) {
  569. pdata->buck_default_idx = 0;
  570. } else {
  571. if (pdata->buck_default_idx >= 8) {
  572. pdata->buck_default_idx = 0;
  573. dev_info(iodev->dev,
  574. "invalid value for default dvs index, use 0\n");
  575. }
  576. }
  577. }
  578. ret = s5m8767_pmic_dt_parse_ds_gpio(iodev, pdata, pmic_np);
  579. if (ret)
  580. return -EINVAL;
  581. if (of_get_property(pmic_np, "s5m8767,pmic-buck2-ramp-enable", NULL))
  582. pdata->buck2_ramp_enable = true;
  583. if (of_get_property(pmic_np, "s5m8767,pmic-buck3-ramp-enable", NULL))
  584. pdata->buck3_ramp_enable = true;
  585. if (of_get_property(pmic_np, "s5m8767,pmic-buck4-ramp-enable", NULL))
  586. pdata->buck4_ramp_enable = true;
  587. if (pdata->buck2_ramp_enable || pdata->buck3_ramp_enable
  588. || pdata->buck4_ramp_enable) {
  589. if (of_property_read_u32(pmic_np, "s5m8767,pmic-buck-ramp-delay",
  590. &pdata->buck_ramp_delay))
  591. pdata->buck_ramp_delay = 0;
  592. }
  593. return 0;
  594. }
  595. #else
  596. static int s5m8767_pmic_dt_parse_pdata(struct platform_device *pdev,
  597. struct sec_platform_data *pdata)
  598. {
  599. return 0;
  600. }
  601. #endif /* CONFIG_OF */
  602. static int s5m8767_pmic_probe(struct platform_device *pdev)
  603. {
  604. struct sec_pmic_dev *iodev = dev_get_drvdata(pdev->dev.parent);
  605. struct sec_platform_data *pdata = iodev->pdata;
  606. struct regulator_config config = { };
  607. struct s5m8767_info *s5m8767;
  608. int i, ret, buck_init;
  609. if (!pdata) {
  610. dev_err(pdev->dev.parent, "Platform data not supplied\n");
  611. return -ENODEV;
  612. }
  613. if (iodev->dev->of_node) {
  614. ret = s5m8767_pmic_dt_parse_pdata(pdev, pdata);
  615. if (ret)
  616. return ret;
  617. }
  618. if (pdata->buck2_gpiodvs) {
  619. if (pdata->buck3_gpiodvs || pdata->buck4_gpiodvs) {
  620. dev_err(&pdev->dev, "S5M8767 GPIO DVS NOT VALID\n");
  621. return -EINVAL;
  622. }
  623. }
  624. if (pdata->buck3_gpiodvs) {
  625. if (pdata->buck2_gpiodvs || pdata->buck4_gpiodvs) {
  626. dev_err(&pdev->dev, "S5M8767 GPIO DVS NOT VALID\n");
  627. return -EINVAL;
  628. }
  629. }
  630. if (pdata->buck4_gpiodvs) {
  631. if (pdata->buck2_gpiodvs || pdata->buck3_gpiodvs) {
  632. dev_err(&pdev->dev, "S5M8767 GPIO DVS NOT VALID\n");
  633. return -EINVAL;
  634. }
  635. }
  636. s5m8767 = devm_kzalloc(&pdev->dev, sizeof(struct s5m8767_info),
  637. GFP_KERNEL);
  638. if (!s5m8767)
  639. return -ENOMEM;
  640. s5m8767->dev = &pdev->dev;
  641. s5m8767->iodev = iodev;
  642. s5m8767->num_regulators = pdata->num_regulators;
  643. platform_set_drvdata(pdev, s5m8767);
  644. s5m8767->buck_gpioindex = pdata->buck_default_idx;
  645. s5m8767->buck2_gpiodvs = pdata->buck2_gpiodvs;
  646. s5m8767->buck3_gpiodvs = pdata->buck3_gpiodvs;
  647. s5m8767->buck4_gpiodvs = pdata->buck4_gpiodvs;
  648. s5m8767->buck_gpios[0] = pdata->buck_gpios[0];
  649. s5m8767->buck_gpios[1] = pdata->buck_gpios[1];
  650. s5m8767->buck_gpios[2] = pdata->buck_gpios[2];
  651. s5m8767->buck_ds[0] = pdata->buck_ds[0];
  652. s5m8767->buck_ds[1] = pdata->buck_ds[1];
  653. s5m8767->buck_ds[2] = pdata->buck_ds[2];
  654. s5m8767->ramp_delay = pdata->buck_ramp_delay;
  655. s5m8767->buck2_ramp = pdata->buck2_ramp_enable;
  656. s5m8767->buck3_ramp = pdata->buck3_ramp_enable;
  657. s5m8767->buck4_ramp = pdata->buck4_ramp_enable;
  658. s5m8767->opmode = pdata->opmode;
  659. buck_init = s5m8767_convert_voltage_to_sel(&buck_voltage_val2,
  660. pdata->buck2_init);
  661. regmap_write(s5m8767->iodev->regmap_pmic, S5M8767_REG_BUCK2DVS2,
  662. buck_init);
  663. buck_init = s5m8767_convert_voltage_to_sel(&buck_voltage_val2,
  664. pdata->buck3_init);
  665. regmap_write(s5m8767->iodev->regmap_pmic, S5M8767_REG_BUCK3DVS2,
  666. buck_init);
  667. buck_init = s5m8767_convert_voltage_to_sel(&buck_voltage_val2,
  668. pdata->buck4_init);
  669. regmap_write(s5m8767->iodev->regmap_pmic, S5M8767_REG_BUCK4DVS2,
  670. buck_init);
  671. for (i = 0; i < 8; i++) {
  672. if (s5m8767->buck2_gpiodvs) {
  673. s5m8767->buck2_vol[i] =
  674. s5m8767_convert_voltage_to_sel(
  675. &buck_voltage_val2,
  676. pdata->buck2_voltage[i]);
  677. }
  678. if (s5m8767->buck3_gpiodvs) {
  679. s5m8767->buck3_vol[i] =
  680. s5m8767_convert_voltage_to_sel(
  681. &buck_voltage_val2,
  682. pdata->buck3_voltage[i]);
  683. }
  684. if (s5m8767->buck4_gpiodvs) {
  685. s5m8767->buck4_vol[i] =
  686. s5m8767_convert_voltage_to_sel(
  687. &buck_voltage_val2,
  688. pdata->buck4_voltage[i]);
  689. }
  690. }
  691. if (pdata->buck2_gpiodvs || pdata->buck3_gpiodvs ||
  692. pdata->buck4_gpiodvs) {
  693. if (!gpio_is_valid(pdata->buck_gpios[0]) ||
  694. !gpio_is_valid(pdata->buck_gpios[1]) ||
  695. !gpio_is_valid(pdata->buck_gpios[2])) {
  696. dev_err(&pdev->dev, "GPIO NOT VALID\n");
  697. return -EINVAL;
  698. }
  699. ret = devm_gpio_request(&pdev->dev, pdata->buck_gpios[0],
  700. "S5M8767 SET1");
  701. if (ret)
  702. return ret;
  703. ret = devm_gpio_request(&pdev->dev, pdata->buck_gpios[1],
  704. "S5M8767 SET2");
  705. if (ret)
  706. return ret;
  707. ret = devm_gpio_request(&pdev->dev, pdata->buck_gpios[2],
  708. "S5M8767 SET3");
  709. if (ret)
  710. return ret;
  711. /* SET1 GPIO */
  712. gpio_direction_output(pdata->buck_gpios[0],
  713. (s5m8767->buck_gpioindex >> 2) & 0x1);
  714. /* SET2 GPIO */
  715. gpio_direction_output(pdata->buck_gpios[1],
  716. (s5m8767->buck_gpioindex >> 1) & 0x1);
  717. /* SET3 GPIO */
  718. gpio_direction_output(pdata->buck_gpios[2],
  719. (s5m8767->buck_gpioindex >> 0) & 0x1);
  720. }
  721. ret = devm_gpio_request(&pdev->dev, pdata->buck_ds[0], "S5M8767 DS2");
  722. if (ret)
  723. return ret;
  724. ret = devm_gpio_request(&pdev->dev, pdata->buck_ds[1], "S5M8767 DS3");
  725. if (ret)
  726. return ret;
  727. ret = devm_gpio_request(&pdev->dev, pdata->buck_ds[2], "S5M8767 DS4");
  728. if (ret)
  729. return ret;
  730. /* DS2 GPIO */
  731. gpio_direction_output(pdata->buck_ds[0], 0x0);
  732. /* DS3 GPIO */
  733. gpio_direction_output(pdata->buck_ds[1], 0x0);
  734. /* DS4 GPIO */
  735. gpio_direction_output(pdata->buck_ds[2], 0x0);
  736. regmap_update_bits(s5m8767->iodev->regmap_pmic,
  737. S5M8767_REG_BUCK2CTRL, 1 << 1,
  738. (pdata->buck2_gpiodvs) ? (1 << 1) : (0 << 1));
  739. regmap_update_bits(s5m8767->iodev->regmap_pmic,
  740. S5M8767_REG_BUCK3CTRL, 1 << 1,
  741. (pdata->buck3_gpiodvs) ? (1 << 1) : (0 << 1));
  742. regmap_update_bits(s5m8767->iodev->regmap_pmic,
  743. S5M8767_REG_BUCK4CTRL, 1 << 1,
  744. (pdata->buck4_gpiodvs) ? (1 << 1) : (0 << 1));
  745. /* Initialize GPIO DVS registers */
  746. for (i = 0; i < 8; i++) {
  747. if (s5m8767->buck2_gpiodvs) {
  748. regmap_write(s5m8767->iodev->regmap_pmic,
  749. S5M8767_REG_BUCK2DVS1 + i,
  750. s5m8767->buck2_vol[i]);
  751. }
  752. if (s5m8767->buck3_gpiodvs) {
  753. regmap_write(s5m8767->iodev->regmap_pmic,
  754. S5M8767_REG_BUCK3DVS1 + i,
  755. s5m8767->buck3_vol[i]);
  756. }
  757. if (s5m8767->buck4_gpiodvs) {
  758. regmap_write(s5m8767->iodev->regmap_pmic,
  759. S5M8767_REG_BUCK4DVS1 + i,
  760. s5m8767->buck4_vol[i]);
  761. }
  762. }
  763. if (s5m8767->buck2_ramp)
  764. regmap_update_bits(s5m8767->iodev->regmap_pmic,
  765. S5M8767_REG_DVSRAMP, 0x08, 0x08);
  766. if (s5m8767->buck3_ramp)
  767. regmap_update_bits(s5m8767->iodev->regmap_pmic,
  768. S5M8767_REG_DVSRAMP, 0x04, 0x04);
  769. if (s5m8767->buck4_ramp)
  770. regmap_update_bits(s5m8767->iodev->regmap_pmic,
  771. S5M8767_REG_DVSRAMP, 0x02, 0x02);
  772. if (s5m8767->buck2_ramp || s5m8767->buck3_ramp
  773. || s5m8767->buck4_ramp) {
  774. unsigned int val;
  775. switch (s5m8767->ramp_delay) {
  776. case 5:
  777. val = S5M8767_DVS_BUCK_RAMP_5;
  778. break;
  779. case 10:
  780. val = S5M8767_DVS_BUCK_RAMP_10;
  781. break;
  782. case 25:
  783. val = S5M8767_DVS_BUCK_RAMP_25;
  784. break;
  785. case 50:
  786. val = S5M8767_DVS_BUCK_RAMP_50;
  787. break;
  788. case 100:
  789. val = S5M8767_DVS_BUCK_RAMP_100;
  790. break;
  791. default:
  792. val = S5M8767_DVS_BUCK_RAMP_10;
  793. }
  794. regmap_update_bits(s5m8767->iodev->regmap_pmic,
  795. S5M8767_REG_DVSRAMP,
  796. S5M8767_DVS_BUCK_RAMP_MASK,
  797. val << S5M8767_DVS_BUCK_RAMP_SHIFT);
  798. }
  799. for (i = 0; i < pdata->num_regulators; i++) {
  800. const struct sec_voltage_desc *desc;
  801. unsigned int id = pdata->regulators[i].id;
  802. int enable_reg, enable_val;
  803. struct regulator_dev *rdev;
  804. BUILD_BUG_ON(ARRAY_SIZE(regulators) != ARRAY_SIZE(reg_voltage_map));
  805. if (WARN_ON_ONCE(id >= ARRAY_SIZE(regulators)))
  806. continue;
  807. desc = reg_voltage_map[id];
  808. if (desc) {
  809. regulators[id].n_voltages =
  810. (desc->max - desc->min) / desc->step + 1;
  811. regulators[id].min_uV = desc->min;
  812. regulators[id].uV_step = desc->step;
  813. regulators[id].vsel_reg =
  814. s5m8767_get_vsel_reg(id, s5m8767);
  815. if (id < S5M8767_BUCK1)
  816. regulators[id].vsel_mask = 0x3f;
  817. else
  818. regulators[id].vsel_mask = 0xff;
  819. ret = s5m8767_get_register(s5m8767, id, &enable_reg,
  820. &enable_val);
  821. if (ret) {
  822. dev_err(s5m8767->dev, "error reading registers\n");
  823. return ret;
  824. }
  825. regulators[id].enable_reg = enable_reg;
  826. regulators[id].enable_mask = S5M8767_ENCTRL_MASK;
  827. regulators[id].enable_val = enable_val;
  828. }
  829. config.dev = s5m8767->dev;
  830. config.init_data = pdata->regulators[i].initdata;
  831. config.driver_data = s5m8767;
  832. config.regmap = iodev->regmap_pmic;
  833. config.of_node = pdata->regulators[i].reg_node;
  834. config.ena_gpiod = NULL;
  835. if (pdata->regulators[i].ext_control_gpiod) {
  836. /* Assigns config.ena_gpiod */
  837. s5m8767_regulator_config_ext_control(s5m8767,
  838. &pdata->regulators[i], &config);
  839. /*
  840. * Hand the GPIO descriptor management over to the
  841. * regulator core, remove it from devres management.
  842. */
  843. devm_gpiod_unhinge(s5m8767->dev, config.ena_gpiod);
  844. }
  845. rdev = devm_regulator_register(&pdev->dev, &regulators[id],
  846. &config);
  847. if (IS_ERR(rdev)) {
  848. ret = PTR_ERR(rdev);
  849. dev_err(s5m8767->dev, "regulator init failed for %d\n",
  850. id);
  851. return ret;
  852. }
  853. if (pdata->regulators[i].ext_control_gpiod) {
  854. ret = s5m8767_enable_ext_control(s5m8767, rdev);
  855. if (ret < 0) {
  856. dev_err(s5m8767->dev,
  857. "failed to enable gpio control over %s: %d\n",
  858. rdev->desc->name, ret);
  859. return ret;
  860. }
  861. }
  862. }
  863. return 0;
  864. }
  865. static const struct platform_device_id s5m8767_pmic_id[] = {
  866. { "s5m8767-pmic", 0},
  867. { },
  868. };
  869. MODULE_DEVICE_TABLE(platform, s5m8767_pmic_id);
  870. static struct platform_driver s5m8767_pmic_driver = {
  871. .driver = {
  872. .name = "s5m8767-pmic",
  873. },
  874. .probe = s5m8767_pmic_probe,
  875. .id_table = s5m8767_pmic_id,
  876. };
  877. module_platform_driver(s5m8767_pmic_driver);
  878. /* Module information */
  879. MODULE_AUTHOR("Sangbeom Kim <[email protected]>");
  880. MODULE_DESCRIPTION("Samsung S5M8767 Regulator Driver");
  881. MODULE_LICENSE("GPL");