s5m8767.c 26 KB

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