s5m8767.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  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_gpiod_get_from_of_node(
  505. &pdev->dev,
  506. reg_np,
  507. "s5m8767,pmic-ext-control-gpios",
  508. 0,
  509. GPIOD_OUT_HIGH | GPIOD_FLAGS_BIT_NONEXCLUSIVE,
  510. "s5m8767");
  511. if (PTR_ERR(rdata->ext_control_gpiod) == -ENOENT) {
  512. rdata->ext_control_gpiod = NULL;
  513. } else if (IS_ERR(rdata->ext_control_gpiod)) {
  514. of_node_put(reg_np);
  515. of_node_put(regulators_np);
  516. return PTR_ERR(rdata->ext_control_gpiod);
  517. }
  518. rdata->id = i;
  519. rdata->initdata = of_get_regulator_init_data(
  520. &pdev->dev, reg_np,
  521. &regulators[i]);
  522. rdata->reg_node = reg_np;
  523. rdata++;
  524. rmode->id = i;
  525. if (of_property_read_u32(reg_np, "op_mode",
  526. &rmode->mode)) {
  527. dev_warn(iodev->dev,
  528. "no op_mode property property at %pOF\n",
  529. reg_np);
  530. rmode->mode = S5M8767_OPMODE_NORMAL_MODE;
  531. }
  532. rmode++;
  533. }
  534. of_node_put(regulators_np);
  535. if (of_get_property(pmic_np, "s5m8767,pmic-buck2-uses-gpio-dvs", NULL)) {
  536. pdata->buck2_gpiodvs = true;
  537. if (of_property_read_u32_array(pmic_np,
  538. "s5m8767,pmic-buck2-dvs-voltage",
  539. pdata->buck2_voltage, dvs_voltage_nr)) {
  540. dev_err(iodev->dev, "buck2 voltages not specified\n");
  541. return -EINVAL;
  542. }
  543. }
  544. if (of_get_property(pmic_np, "s5m8767,pmic-buck3-uses-gpio-dvs", NULL)) {
  545. pdata->buck3_gpiodvs = true;
  546. if (of_property_read_u32_array(pmic_np,
  547. "s5m8767,pmic-buck3-dvs-voltage",
  548. pdata->buck3_voltage, dvs_voltage_nr)) {
  549. dev_err(iodev->dev, "buck3 voltages not specified\n");
  550. return -EINVAL;
  551. }
  552. }
  553. if (of_get_property(pmic_np, "s5m8767,pmic-buck4-uses-gpio-dvs", NULL)) {
  554. pdata->buck4_gpiodvs = true;
  555. if (of_property_read_u32_array(pmic_np,
  556. "s5m8767,pmic-buck4-dvs-voltage",
  557. pdata->buck4_voltage, dvs_voltage_nr)) {
  558. dev_err(iodev->dev, "buck4 voltages not specified\n");
  559. return -EINVAL;
  560. }
  561. }
  562. if (pdata->buck2_gpiodvs || pdata->buck3_gpiodvs ||
  563. pdata->buck4_gpiodvs) {
  564. ret = s5m8767_pmic_dt_parse_dvs_gpio(iodev, pdata, pmic_np);
  565. if (ret)
  566. return -EINVAL;
  567. if (of_property_read_u32(pmic_np,
  568. "s5m8767,pmic-buck-default-dvs-idx",
  569. &pdata->buck_default_idx)) {
  570. pdata->buck_default_idx = 0;
  571. } else {
  572. if (pdata->buck_default_idx >= 8) {
  573. pdata->buck_default_idx = 0;
  574. dev_info(iodev->dev,
  575. "invalid value for default dvs index, use 0\n");
  576. }
  577. }
  578. }
  579. ret = s5m8767_pmic_dt_parse_ds_gpio(iodev, pdata, pmic_np);
  580. if (ret)
  581. return -EINVAL;
  582. if (of_get_property(pmic_np, "s5m8767,pmic-buck2-ramp-enable", NULL))
  583. pdata->buck2_ramp_enable = true;
  584. if (of_get_property(pmic_np, "s5m8767,pmic-buck3-ramp-enable", NULL))
  585. pdata->buck3_ramp_enable = true;
  586. if (of_get_property(pmic_np, "s5m8767,pmic-buck4-ramp-enable", NULL))
  587. pdata->buck4_ramp_enable = true;
  588. if (pdata->buck2_ramp_enable || pdata->buck3_ramp_enable
  589. || pdata->buck4_ramp_enable) {
  590. if (of_property_read_u32(pmic_np, "s5m8767,pmic-buck-ramp-delay",
  591. &pdata->buck_ramp_delay))
  592. pdata->buck_ramp_delay = 0;
  593. }
  594. return 0;
  595. }
  596. #else
  597. static int s5m8767_pmic_dt_parse_pdata(struct platform_device *pdev,
  598. struct sec_platform_data *pdata)
  599. {
  600. return 0;
  601. }
  602. #endif /* CONFIG_OF */
  603. static int s5m8767_pmic_probe(struct platform_device *pdev)
  604. {
  605. struct sec_pmic_dev *iodev = dev_get_drvdata(pdev->dev.parent);
  606. struct sec_platform_data *pdata = iodev->pdata;
  607. struct regulator_config config = { };
  608. struct s5m8767_info *s5m8767;
  609. int i, ret, buck_init;
  610. if (!pdata) {
  611. dev_err(pdev->dev.parent, "Platform data not supplied\n");
  612. return -ENODEV;
  613. }
  614. if (iodev->dev->of_node) {
  615. ret = s5m8767_pmic_dt_parse_pdata(pdev, pdata);
  616. if (ret)
  617. return ret;
  618. }
  619. if (pdata->buck2_gpiodvs) {
  620. if (pdata->buck3_gpiodvs || pdata->buck4_gpiodvs) {
  621. dev_err(&pdev->dev, "S5M8767 GPIO DVS NOT VALID\n");
  622. return -EINVAL;
  623. }
  624. }
  625. if (pdata->buck3_gpiodvs) {
  626. if (pdata->buck2_gpiodvs || pdata->buck4_gpiodvs) {
  627. dev_err(&pdev->dev, "S5M8767 GPIO DVS NOT VALID\n");
  628. return -EINVAL;
  629. }
  630. }
  631. if (pdata->buck4_gpiodvs) {
  632. if (pdata->buck2_gpiodvs || pdata->buck3_gpiodvs) {
  633. dev_err(&pdev->dev, "S5M8767 GPIO DVS NOT VALID\n");
  634. return -EINVAL;
  635. }
  636. }
  637. s5m8767 = devm_kzalloc(&pdev->dev, sizeof(struct s5m8767_info),
  638. GFP_KERNEL);
  639. if (!s5m8767)
  640. return -ENOMEM;
  641. s5m8767->dev = &pdev->dev;
  642. s5m8767->iodev = iodev;
  643. s5m8767->num_regulators = pdata->num_regulators;
  644. platform_set_drvdata(pdev, s5m8767);
  645. s5m8767->buck_gpioindex = pdata->buck_default_idx;
  646. s5m8767->buck2_gpiodvs = pdata->buck2_gpiodvs;
  647. s5m8767->buck3_gpiodvs = pdata->buck3_gpiodvs;
  648. s5m8767->buck4_gpiodvs = pdata->buck4_gpiodvs;
  649. s5m8767->buck_gpios[0] = pdata->buck_gpios[0];
  650. s5m8767->buck_gpios[1] = pdata->buck_gpios[1];
  651. s5m8767->buck_gpios[2] = pdata->buck_gpios[2];
  652. s5m8767->buck_ds[0] = pdata->buck_ds[0];
  653. s5m8767->buck_ds[1] = pdata->buck_ds[1];
  654. s5m8767->buck_ds[2] = pdata->buck_ds[2];
  655. s5m8767->ramp_delay = pdata->buck_ramp_delay;
  656. s5m8767->buck2_ramp = pdata->buck2_ramp_enable;
  657. s5m8767->buck3_ramp = pdata->buck3_ramp_enable;
  658. s5m8767->buck4_ramp = pdata->buck4_ramp_enable;
  659. s5m8767->opmode = pdata->opmode;
  660. buck_init = s5m8767_convert_voltage_to_sel(&buck_voltage_val2,
  661. pdata->buck2_init);
  662. regmap_write(s5m8767->iodev->regmap_pmic, S5M8767_REG_BUCK2DVS2,
  663. buck_init);
  664. buck_init = s5m8767_convert_voltage_to_sel(&buck_voltage_val2,
  665. pdata->buck3_init);
  666. regmap_write(s5m8767->iodev->regmap_pmic, S5M8767_REG_BUCK3DVS2,
  667. buck_init);
  668. buck_init = s5m8767_convert_voltage_to_sel(&buck_voltage_val2,
  669. pdata->buck4_init);
  670. regmap_write(s5m8767->iodev->regmap_pmic, S5M8767_REG_BUCK4DVS2,
  671. buck_init);
  672. for (i = 0; i < 8; i++) {
  673. if (s5m8767->buck2_gpiodvs) {
  674. s5m8767->buck2_vol[i] =
  675. s5m8767_convert_voltage_to_sel(
  676. &buck_voltage_val2,
  677. pdata->buck2_voltage[i]);
  678. }
  679. if (s5m8767->buck3_gpiodvs) {
  680. s5m8767->buck3_vol[i] =
  681. s5m8767_convert_voltage_to_sel(
  682. &buck_voltage_val2,
  683. pdata->buck3_voltage[i]);
  684. }
  685. if (s5m8767->buck4_gpiodvs) {
  686. s5m8767->buck4_vol[i] =
  687. s5m8767_convert_voltage_to_sel(
  688. &buck_voltage_val2,
  689. pdata->buck4_voltage[i]);
  690. }
  691. }
  692. if (pdata->buck2_gpiodvs || pdata->buck3_gpiodvs ||
  693. pdata->buck4_gpiodvs) {
  694. if (!gpio_is_valid(pdata->buck_gpios[0]) ||
  695. !gpio_is_valid(pdata->buck_gpios[1]) ||
  696. !gpio_is_valid(pdata->buck_gpios[2])) {
  697. dev_err(&pdev->dev, "GPIO NOT VALID\n");
  698. return -EINVAL;
  699. }
  700. ret = devm_gpio_request(&pdev->dev, pdata->buck_gpios[0],
  701. "S5M8767 SET1");
  702. if (ret)
  703. return ret;
  704. ret = devm_gpio_request(&pdev->dev, pdata->buck_gpios[1],
  705. "S5M8767 SET2");
  706. if (ret)
  707. return ret;
  708. ret = devm_gpio_request(&pdev->dev, pdata->buck_gpios[2],
  709. "S5M8767 SET3");
  710. if (ret)
  711. return ret;
  712. /* SET1 GPIO */
  713. gpio_direction_output(pdata->buck_gpios[0],
  714. (s5m8767->buck_gpioindex >> 2) & 0x1);
  715. /* SET2 GPIO */
  716. gpio_direction_output(pdata->buck_gpios[1],
  717. (s5m8767->buck_gpioindex >> 1) & 0x1);
  718. /* SET3 GPIO */
  719. gpio_direction_output(pdata->buck_gpios[2],
  720. (s5m8767->buck_gpioindex >> 0) & 0x1);
  721. }
  722. ret = devm_gpio_request(&pdev->dev, pdata->buck_ds[0], "S5M8767 DS2");
  723. if (ret)
  724. return ret;
  725. ret = devm_gpio_request(&pdev->dev, pdata->buck_ds[1], "S5M8767 DS3");
  726. if (ret)
  727. return ret;
  728. ret = devm_gpio_request(&pdev->dev, pdata->buck_ds[2], "S5M8767 DS4");
  729. if (ret)
  730. return ret;
  731. /* DS2 GPIO */
  732. gpio_direction_output(pdata->buck_ds[0], 0x0);
  733. /* DS3 GPIO */
  734. gpio_direction_output(pdata->buck_ds[1], 0x0);
  735. /* DS4 GPIO */
  736. gpio_direction_output(pdata->buck_ds[2], 0x0);
  737. if (pdata->buck2_gpiodvs || pdata->buck3_gpiodvs ||
  738. pdata->buck4_gpiodvs) {
  739. regmap_update_bits(s5m8767->iodev->regmap_pmic,
  740. S5M8767_REG_BUCK2CTRL, 1 << 1,
  741. (pdata->buck2_gpiodvs) ? (1 << 1) : (0 << 1));
  742. regmap_update_bits(s5m8767->iodev->regmap_pmic,
  743. S5M8767_REG_BUCK3CTRL, 1 << 1,
  744. (pdata->buck3_gpiodvs) ? (1 << 1) : (0 << 1));
  745. regmap_update_bits(s5m8767->iodev->regmap_pmic,
  746. S5M8767_REG_BUCK4CTRL, 1 << 1,
  747. (pdata->buck4_gpiodvs) ? (1 << 1) : (0 << 1));
  748. }
  749. /* Initialize GPIO DVS registers */
  750. for (i = 0; i < 8; i++) {
  751. if (s5m8767->buck2_gpiodvs) {
  752. regmap_write(s5m8767->iodev->regmap_pmic,
  753. S5M8767_REG_BUCK2DVS1 + i,
  754. s5m8767->buck2_vol[i]);
  755. }
  756. if (s5m8767->buck3_gpiodvs) {
  757. regmap_write(s5m8767->iodev->regmap_pmic,
  758. S5M8767_REG_BUCK3DVS1 + i,
  759. s5m8767->buck3_vol[i]);
  760. }
  761. if (s5m8767->buck4_gpiodvs) {
  762. regmap_write(s5m8767->iodev->regmap_pmic,
  763. S5M8767_REG_BUCK4DVS1 + i,
  764. s5m8767->buck4_vol[i]);
  765. }
  766. }
  767. if (s5m8767->buck2_ramp)
  768. regmap_update_bits(s5m8767->iodev->regmap_pmic,
  769. S5M8767_REG_DVSRAMP, 0x08, 0x08);
  770. if (s5m8767->buck3_ramp)
  771. regmap_update_bits(s5m8767->iodev->regmap_pmic,
  772. S5M8767_REG_DVSRAMP, 0x04, 0x04);
  773. if (s5m8767->buck4_ramp)
  774. regmap_update_bits(s5m8767->iodev->regmap_pmic,
  775. S5M8767_REG_DVSRAMP, 0x02, 0x02);
  776. if (s5m8767->buck2_ramp || s5m8767->buck3_ramp
  777. || s5m8767->buck4_ramp) {
  778. unsigned int val;
  779. switch (s5m8767->ramp_delay) {
  780. case 5:
  781. val = S5M8767_DVS_BUCK_RAMP_5;
  782. break;
  783. case 10:
  784. val = S5M8767_DVS_BUCK_RAMP_10;
  785. break;
  786. case 25:
  787. val = S5M8767_DVS_BUCK_RAMP_25;
  788. break;
  789. case 50:
  790. val = S5M8767_DVS_BUCK_RAMP_50;
  791. break;
  792. case 100:
  793. val = S5M8767_DVS_BUCK_RAMP_100;
  794. break;
  795. default:
  796. val = S5M8767_DVS_BUCK_RAMP_10;
  797. }
  798. regmap_update_bits(s5m8767->iodev->regmap_pmic,
  799. S5M8767_REG_DVSRAMP,
  800. S5M8767_DVS_BUCK_RAMP_MASK,
  801. val << S5M8767_DVS_BUCK_RAMP_SHIFT);
  802. }
  803. for (i = 0; i < pdata->num_regulators; i++) {
  804. const struct sec_voltage_desc *desc;
  805. int id = pdata->regulators[i].id;
  806. int enable_reg, enable_val;
  807. struct regulator_dev *rdev;
  808. desc = reg_voltage_map[id];
  809. if (desc) {
  810. regulators[id].n_voltages =
  811. (desc->max - desc->min) / desc->step + 1;
  812. regulators[id].min_uV = desc->min;
  813. regulators[id].uV_step = desc->step;
  814. regulators[id].vsel_reg =
  815. s5m8767_get_vsel_reg(id, s5m8767);
  816. if (id < S5M8767_BUCK1)
  817. regulators[id].vsel_mask = 0x3f;
  818. else
  819. regulators[id].vsel_mask = 0xff;
  820. ret = s5m8767_get_register(s5m8767, id, &enable_reg,
  821. &enable_val);
  822. if (ret) {
  823. dev_err(s5m8767->dev, "error reading registers\n");
  824. return ret;
  825. }
  826. regulators[id].enable_reg = enable_reg;
  827. regulators[id].enable_mask = S5M8767_ENCTRL_MASK;
  828. regulators[id].enable_val = enable_val;
  829. }
  830. config.dev = s5m8767->dev;
  831. config.init_data = pdata->regulators[i].initdata;
  832. config.driver_data = s5m8767;
  833. config.regmap = iodev->regmap_pmic;
  834. config.of_node = pdata->regulators[i].reg_node;
  835. config.ena_gpiod = NULL;
  836. if (pdata->regulators[i].ext_control_gpiod) {
  837. /* Assigns config.ena_gpiod */
  838. s5m8767_regulator_config_ext_control(s5m8767,
  839. &pdata->regulators[i], &config);
  840. /*
  841. * Hand the GPIO descriptor management over to the
  842. * regulator core, remove it from devres management.
  843. */
  844. devm_gpiod_unhinge(s5m8767->dev, config.ena_gpiod);
  845. }
  846. rdev = devm_regulator_register(&pdev->dev, &regulators[id],
  847. &config);
  848. if (IS_ERR(rdev)) {
  849. ret = PTR_ERR(rdev);
  850. dev_err(s5m8767->dev, "regulator init failed for %d\n",
  851. id);
  852. return ret;
  853. }
  854. if (pdata->regulators[i].ext_control_gpiod) {
  855. ret = s5m8767_enable_ext_control(s5m8767, rdev);
  856. if (ret < 0) {
  857. dev_err(s5m8767->dev,
  858. "failed to enable gpio control over %s: %d\n",
  859. rdev->desc->name, ret);
  860. return ret;
  861. }
  862. }
  863. }
  864. return 0;
  865. }
  866. static const struct platform_device_id s5m8767_pmic_id[] = {
  867. { "s5m8767-pmic", 0},
  868. { },
  869. };
  870. MODULE_DEVICE_TABLE(platform, s5m8767_pmic_id);
  871. static struct platform_driver s5m8767_pmic_driver = {
  872. .driver = {
  873. .name = "s5m8767-pmic",
  874. },
  875. .probe = s5m8767_pmic_probe,
  876. .id_table = s5m8767_pmic_id,
  877. };
  878. static int __init s5m8767_pmic_init(void)
  879. {
  880. return platform_driver_register(&s5m8767_pmic_driver);
  881. }
  882. subsys_initcall(s5m8767_pmic_init);
  883. static void __exit s5m8767_pmic_exit(void)
  884. {
  885. platform_driver_unregister(&s5m8767_pmic_driver);
  886. }
  887. module_exit(s5m8767_pmic_exit);
  888. /* Module information */
  889. MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>");
  890. MODULE_DESCRIPTION("SAMSUNG S5M8767 Regulator Driver");
  891. MODULE_LICENSE("GPL");