s5m8767.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015
  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 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. const struct sec_voltage_desc *desc;
  302. int reg_id = rdev_get_id(rdev);
  303. desc = reg_voltage_map[reg_id];
  304. if ((old_sel < new_sel) && s5m8767->ramp_delay)
  305. return DIV_ROUND_UP(desc->step * (new_sel - old_sel),
  306. s5m8767->ramp_delay * 1000);
  307. return 0;
  308. }
  309. static const struct regulator_ops s5m8767_ops = {
  310. .list_voltage = regulator_list_voltage_linear,
  311. .is_enabled = regulator_is_enabled_regmap,
  312. .enable = regulator_enable_regmap,
  313. .disable = regulator_disable_regmap,
  314. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  315. .set_voltage_sel = s5m8767_set_voltage_sel,
  316. .set_voltage_time_sel = s5m8767_set_voltage_time_sel,
  317. };
  318. static const struct regulator_ops s5m8767_buck78_ops = {
  319. .list_voltage = regulator_list_voltage_linear,
  320. .is_enabled = regulator_is_enabled_regmap,
  321. .enable = regulator_enable_regmap,
  322. .disable = regulator_disable_regmap,
  323. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  324. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  325. };
  326. #define s5m8767_regulator_desc(_name) { \
  327. .name = #_name, \
  328. .id = S5M8767_##_name, \
  329. .ops = &s5m8767_ops, \
  330. .type = REGULATOR_VOLTAGE, \
  331. .owner = THIS_MODULE, \
  332. }
  333. #define s5m8767_regulator_buck78_desc(_name) { \
  334. .name = #_name, \
  335. .id = S5M8767_##_name, \
  336. .ops = &s5m8767_buck78_ops, \
  337. .type = REGULATOR_VOLTAGE, \
  338. .owner = THIS_MODULE, \
  339. }
  340. static struct regulator_desc regulators[] = {
  341. s5m8767_regulator_desc(LDO1),
  342. s5m8767_regulator_desc(LDO2),
  343. s5m8767_regulator_desc(LDO3),
  344. s5m8767_regulator_desc(LDO4),
  345. s5m8767_regulator_desc(LDO5),
  346. s5m8767_regulator_desc(LDO6),
  347. s5m8767_regulator_desc(LDO7),
  348. s5m8767_regulator_desc(LDO8),
  349. s5m8767_regulator_desc(LDO9),
  350. s5m8767_regulator_desc(LDO10),
  351. s5m8767_regulator_desc(LDO11),
  352. s5m8767_regulator_desc(LDO12),
  353. s5m8767_regulator_desc(LDO13),
  354. s5m8767_regulator_desc(LDO14),
  355. s5m8767_regulator_desc(LDO15),
  356. s5m8767_regulator_desc(LDO16),
  357. s5m8767_regulator_desc(LDO17),
  358. s5m8767_regulator_desc(LDO18),
  359. s5m8767_regulator_desc(LDO19),
  360. s5m8767_regulator_desc(LDO20),
  361. s5m8767_regulator_desc(LDO21),
  362. s5m8767_regulator_desc(LDO22),
  363. s5m8767_regulator_desc(LDO23),
  364. s5m8767_regulator_desc(LDO24),
  365. s5m8767_regulator_desc(LDO25),
  366. s5m8767_regulator_desc(LDO26),
  367. s5m8767_regulator_desc(LDO27),
  368. s5m8767_regulator_desc(LDO28),
  369. s5m8767_regulator_desc(BUCK1),
  370. s5m8767_regulator_desc(BUCK2),
  371. s5m8767_regulator_desc(BUCK3),
  372. s5m8767_regulator_desc(BUCK4),
  373. s5m8767_regulator_desc(BUCK5),
  374. s5m8767_regulator_desc(BUCK6),
  375. s5m8767_regulator_buck78_desc(BUCK7),
  376. s5m8767_regulator_buck78_desc(BUCK8),
  377. s5m8767_regulator_desc(BUCK9),
  378. };
  379. /*
  380. * Enable GPIO control over BUCK9 in regulator_config for that regulator.
  381. */
  382. static void s5m8767_regulator_config_ext_control(struct s5m8767_info *s5m8767,
  383. struct sec_regulator_data *rdata,
  384. struct regulator_config *config)
  385. {
  386. int i, mode = 0;
  387. if (rdata->id != S5M8767_BUCK9)
  388. return;
  389. /* Check if opmode for regulator matches S5M8767_ENCTRL_USE_GPIO */
  390. for (i = 0; i < s5m8767->num_regulators; i++) {
  391. const struct sec_opmode_data *opmode = &s5m8767->opmode[i];
  392. if (opmode->id == rdata->id) {
  393. mode = s5m8767_opmode_reg[rdata->id][opmode->mode];
  394. break;
  395. }
  396. }
  397. if (mode != S5M8767_ENCTRL_USE_GPIO) {
  398. dev_warn(s5m8767->dev,
  399. "ext-control for %s: mismatched op_mode (%x), ignoring\n",
  400. rdata->reg_node->name, mode);
  401. return;
  402. }
  403. if (!rdata->ext_control_gpiod) {
  404. dev_warn(s5m8767->dev,
  405. "ext-control for %s: GPIO not valid, ignoring\n",
  406. rdata->reg_node->name);
  407. return;
  408. }
  409. config->ena_gpiod = rdata->ext_control_gpiod;
  410. }
  411. /*
  412. * Turn on GPIO control over BUCK9.
  413. */
  414. static int s5m8767_enable_ext_control(struct s5m8767_info *s5m8767,
  415. struct regulator_dev *rdev)
  416. {
  417. int id = rdev_get_id(rdev);
  418. int ret, reg, enable_ctrl;
  419. if (id != S5M8767_BUCK9)
  420. return -EINVAL;
  421. ret = s5m8767_get_register(s5m8767, id, &reg, &enable_ctrl);
  422. if (ret)
  423. return ret;
  424. return regmap_update_bits(s5m8767->iodev->regmap_pmic,
  425. reg, S5M8767_ENCTRL_MASK,
  426. S5M8767_ENCTRL_USE_GPIO << S5M8767_ENCTRL_SHIFT);
  427. }
  428. #ifdef CONFIG_OF
  429. static int s5m8767_pmic_dt_parse_dvs_gpio(struct sec_pmic_dev *iodev,
  430. struct sec_platform_data *pdata,
  431. struct device_node *pmic_np)
  432. {
  433. int i, gpio;
  434. for (i = 0; i < 3; i++) {
  435. gpio = of_get_named_gpio(pmic_np,
  436. "s5m8767,pmic-buck-dvs-gpios", i);
  437. if (!gpio_is_valid(gpio)) {
  438. dev_err(iodev->dev, "invalid gpio[%d]: %d\n", i, gpio);
  439. return -EINVAL;
  440. }
  441. pdata->buck_gpios[i] = gpio;
  442. }
  443. return 0;
  444. }
  445. static int s5m8767_pmic_dt_parse_ds_gpio(struct sec_pmic_dev *iodev,
  446. struct sec_platform_data *pdata,
  447. struct device_node *pmic_np)
  448. {
  449. int i, gpio;
  450. for (i = 0; i < 3; i++) {
  451. gpio = of_get_named_gpio(pmic_np,
  452. "s5m8767,pmic-buck-ds-gpios", i);
  453. if (!gpio_is_valid(gpio)) {
  454. dev_err(iodev->dev, "invalid gpio[%d]: %d\n", i, gpio);
  455. return -EINVAL;
  456. }
  457. pdata->buck_ds[i] = gpio;
  458. }
  459. return 0;
  460. }
  461. static int s5m8767_pmic_dt_parse_pdata(struct platform_device *pdev,
  462. struct sec_platform_data *pdata)
  463. {
  464. struct sec_pmic_dev *iodev = dev_get_drvdata(pdev->dev.parent);
  465. struct device_node *pmic_np, *regulators_np, *reg_np;
  466. struct sec_regulator_data *rdata;
  467. struct sec_opmode_data *rmode;
  468. unsigned int i, dvs_voltage_nr = 8, ret;
  469. pmic_np = iodev->dev->of_node;
  470. if (!pmic_np) {
  471. dev_err(iodev->dev, "could not find pmic sub-node\n");
  472. return -ENODEV;
  473. }
  474. regulators_np = of_get_child_by_name(pmic_np, "regulators");
  475. if (!regulators_np) {
  476. dev_err(iodev->dev, "could not find regulators sub-node\n");
  477. return -EINVAL;
  478. }
  479. /* count the number of regulators to be supported in pmic */
  480. pdata->num_regulators = of_get_child_count(regulators_np);
  481. rdata = devm_kcalloc(&pdev->dev,
  482. pdata->num_regulators, sizeof(*rdata),
  483. GFP_KERNEL);
  484. if (!rdata)
  485. return -ENOMEM;
  486. rmode = devm_kcalloc(&pdev->dev,
  487. pdata->num_regulators, sizeof(*rmode),
  488. GFP_KERNEL);
  489. if (!rmode)
  490. return -ENOMEM;
  491. pdata->regulators = rdata;
  492. pdata->opmode = rmode;
  493. for_each_child_of_node(regulators_np, reg_np) {
  494. for (i = 0; i < ARRAY_SIZE(regulators); i++)
  495. if (!of_node_cmp(reg_np->name, regulators[i].name))
  496. break;
  497. if (i == ARRAY_SIZE(regulators)) {
  498. dev_warn(iodev->dev,
  499. "don't know how to configure regulator %s\n",
  500. reg_np->name);
  501. continue;
  502. }
  503. rdata->ext_control_gpiod = devm_gpiod_get_from_of_node(&pdev->dev,
  504. reg_np,
  505. "s5m8767,pmic-ext-control-gpios",
  506. 0,
  507. GPIOD_OUT_HIGH,
  508. "s5m8767");
  509. if (IS_ERR(rdata->ext_control_gpiod))
  510. return PTR_ERR(rdata->ext_control_gpiod);
  511. rdata->id = i;
  512. rdata->initdata = of_get_regulator_init_data(
  513. &pdev->dev, reg_np,
  514. &regulators[i]);
  515. rdata->reg_node = reg_np;
  516. rdata++;
  517. rmode->id = i;
  518. if (of_property_read_u32(reg_np, "op_mode",
  519. &rmode->mode)) {
  520. dev_warn(iodev->dev,
  521. "no op_mode property property at %pOF\n",
  522. reg_np);
  523. rmode->mode = S5M8767_OPMODE_NORMAL_MODE;
  524. }
  525. rmode++;
  526. }
  527. of_node_put(regulators_np);
  528. if (of_get_property(pmic_np, "s5m8767,pmic-buck2-uses-gpio-dvs", NULL)) {
  529. pdata->buck2_gpiodvs = true;
  530. if (of_property_read_u32_array(pmic_np,
  531. "s5m8767,pmic-buck2-dvs-voltage",
  532. pdata->buck2_voltage, dvs_voltage_nr)) {
  533. dev_err(iodev->dev, "buck2 voltages not specified\n");
  534. return -EINVAL;
  535. }
  536. }
  537. if (of_get_property(pmic_np, "s5m8767,pmic-buck3-uses-gpio-dvs", NULL)) {
  538. pdata->buck3_gpiodvs = true;
  539. if (of_property_read_u32_array(pmic_np,
  540. "s5m8767,pmic-buck3-dvs-voltage",
  541. pdata->buck3_voltage, dvs_voltage_nr)) {
  542. dev_err(iodev->dev, "buck3 voltages not specified\n");
  543. return -EINVAL;
  544. }
  545. }
  546. if (of_get_property(pmic_np, "s5m8767,pmic-buck4-uses-gpio-dvs", NULL)) {
  547. pdata->buck4_gpiodvs = true;
  548. if (of_property_read_u32_array(pmic_np,
  549. "s5m8767,pmic-buck4-dvs-voltage",
  550. pdata->buck4_voltage, dvs_voltage_nr)) {
  551. dev_err(iodev->dev, "buck4 voltages not specified\n");
  552. return -EINVAL;
  553. }
  554. }
  555. if (pdata->buck2_gpiodvs || pdata->buck3_gpiodvs ||
  556. pdata->buck4_gpiodvs) {
  557. ret = s5m8767_pmic_dt_parse_dvs_gpio(iodev, pdata, pmic_np);
  558. if (ret)
  559. return -EINVAL;
  560. if (of_property_read_u32(pmic_np,
  561. "s5m8767,pmic-buck-default-dvs-idx",
  562. &pdata->buck_default_idx)) {
  563. pdata->buck_default_idx = 0;
  564. } else {
  565. if (pdata->buck_default_idx >= 8) {
  566. pdata->buck_default_idx = 0;
  567. dev_info(iodev->dev,
  568. "invalid value for default dvs index, use 0\n");
  569. }
  570. }
  571. }
  572. ret = s5m8767_pmic_dt_parse_ds_gpio(iodev, pdata, pmic_np);
  573. if (ret)
  574. return -EINVAL;
  575. if (of_get_property(pmic_np, "s5m8767,pmic-buck2-ramp-enable", NULL))
  576. pdata->buck2_ramp_enable = true;
  577. if (of_get_property(pmic_np, "s5m8767,pmic-buck3-ramp-enable", NULL))
  578. pdata->buck3_ramp_enable = true;
  579. if (of_get_property(pmic_np, "s5m8767,pmic-buck4-ramp-enable", NULL))
  580. pdata->buck4_ramp_enable = true;
  581. if (pdata->buck2_ramp_enable || pdata->buck3_ramp_enable
  582. || pdata->buck4_ramp_enable) {
  583. if (of_property_read_u32(pmic_np, "s5m8767,pmic-buck-ramp-delay",
  584. &pdata->buck_ramp_delay))
  585. pdata->buck_ramp_delay = 0;
  586. }
  587. return 0;
  588. }
  589. #else
  590. static int s5m8767_pmic_dt_parse_pdata(struct platform_device *pdev,
  591. struct sec_platform_data *pdata)
  592. {
  593. return 0;
  594. }
  595. #endif /* CONFIG_OF */
  596. static int s5m8767_pmic_probe(struct platform_device *pdev)
  597. {
  598. struct sec_pmic_dev *iodev = dev_get_drvdata(pdev->dev.parent);
  599. struct sec_platform_data *pdata = iodev->pdata;
  600. struct regulator_config config = { };
  601. struct s5m8767_info *s5m8767;
  602. int i, ret, buck_init;
  603. if (!pdata) {
  604. dev_err(pdev->dev.parent, "Platform data not supplied\n");
  605. return -ENODEV;
  606. }
  607. if (iodev->dev->of_node) {
  608. ret = s5m8767_pmic_dt_parse_pdata(pdev, pdata);
  609. if (ret)
  610. return ret;
  611. }
  612. if (pdata->buck2_gpiodvs) {
  613. if (pdata->buck3_gpiodvs || pdata->buck4_gpiodvs) {
  614. dev_err(&pdev->dev, "S5M8767 GPIO DVS NOT VALID\n");
  615. return -EINVAL;
  616. }
  617. }
  618. if (pdata->buck3_gpiodvs) {
  619. if (pdata->buck2_gpiodvs || pdata->buck4_gpiodvs) {
  620. dev_err(&pdev->dev, "S5M8767 GPIO DVS NOT VALID\n");
  621. return -EINVAL;
  622. }
  623. }
  624. if (pdata->buck4_gpiodvs) {
  625. if (pdata->buck2_gpiodvs || pdata->buck3_gpiodvs) {
  626. dev_err(&pdev->dev, "S5M8767 GPIO DVS NOT VALID\n");
  627. return -EINVAL;
  628. }
  629. }
  630. s5m8767 = devm_kzalloc(&pdev->dev, sizeof(struct s5m8767_info),
  631. GFP_KERNEL);
  632. if (!s5m8767)
  633. return -ENOMEM;
  634. s5m8767->dev = &pdev->dev;
  635. s5m8767->iodev = iodev;
  636. s5m8767->num_regulators = pdata->num_regulators;
  637. platform_set_drvdata(pdev, s5m8767);
  638. s5m8767->buck_gpioindex = pdata->buck_default_idx;
  639. s5m8767->buck2_gpiodvs = pdata->buck2_gpiodvs;
  640. s5m8767->buck3_gpiodvs = pdata->buck3_gpiodvs;
  641. s5m8767->buck4_gpiodvs = pdata->buck4_gpiodvs;
  642. s5m8767->buck_gpios[0] = pdata->buck_gpios[0];
  643. s5m8767->buck_gpios[1] = pdata->buck_gpios[1];
  644. s5m8767->buck_gpios[2] = pdata->buck_gpios[2];
  645. s5m8767->buck_ds[0] = pdata->buck_ds[0];
  646. s5m8767->buck_ds[1] = pdata->buck_ds[1];
  647. s5m8767->buck_ds[2] = pdata->buck_ds[2];
  648. s5m8767->ramp_delay = pdata->buck_ramp_delay;
  649. s5m8767->buck2_ramp = pdata->buck2_ramp_enable;
  650. s5m8767->buck3_ramp = pdata->buck3_ramp_enable;
  651. s5m8767->buck4_ramp = pdata->buck4_ramp_enable;
  652. s5m8767->opmode = pdata->opmode;
  653. buck_init = s5m8767_convert_voltage_to_sel(&buck_voltage_val2,
  654. pdata->buck2_init);
  655. regmap_write(s5m8767->iodev->regmap_pmic, S5M8767_REG_BUCK2DVS2,
  656. buck_init);
  657. buck_init = s5m8767_convert_voltage_to_sel(&buck_voltage_val2,
  658. pdata->buck3_init);
  659. regmap_write(s5m8767->iodev->regmap_pmic, S5M8767_REG_BUCK3DVS2,
  660. buck_init);
  661. buck_init = s5m8767_convert_voltage_to_sel(&buck_voltage_val2,
  662. pdata->buck4_init);
  663. regmap_write(s5m8767->iodev->regmap_pmic, S5M8767_REG_BUCK4DVS2,
  664. buck_init);
  665. for (i = 0; i < 8; i++) {
  666. if (s5m8767->buck2_gpiodvs) {
  667. s5m8767->buck2_vol[i] =
  668. s5m8767_convert_voltage_to_sel(
  669. &buck_voltage_val2,
  670. pdata->buck2_voltage[i]);
  671. }
  672. if (s5m8767->buck3_gpiodvs) {
  673. s5m8767->buck3_vol[i] =
  674. s5m8767_convert_voltage_to_sel(
  675. &buck_voltage_val2,
  676. pdata->buck3_voltage[i]);
  677. }
  678. if (s5m8767->buck4_gpiodvs) {
  679. s5m8767->buck4_vol[i] =
  680. s5m8767_convert_voltage_to_sel(
  681. &buck_voltage_val2,
  682. pdata->buck4_voltage[i]);
  683. }
  684. }
  685. if (pdata->buck2_gpiodvs || pdata->buck3_gpiodvs ||
  686. pdata->buck4_gpiodvs) {
  687. if (!gpio_is_valid(pdata->buck_gpios[0]) ||
  688. !gpio_is_valid(pdata->buck_gpios[1]) ||
  689. !gpio_is_valid(pdata->buck_gpios[2])) {
  690. dev_err(&pdev->dev, "GPIO NOT VALID\n");
  691. return -EINVAL;
  692. }
  693. ret = devm_gpio_request(&pdev->dev, pdata->buck_gpios[0],
  694. "S5M8767 SET1");
  695. if (ret)
  696. return ret;
  697. ret = devm_gpio_request(&pdev->dev, pdata->buck_gpios[1],
  698. "S5M8767 SET2");
  699. if (ret)
  700. return ret;
  701. ret = devm_gpio_request(&pdev->dev, pdata->buck_gpios[2],
  702. "S5M8767 SET3");
  703. if (ret)
  704. return ret;
  705. /* SET1 GPIO */
  706. gpio_direction_output(pdata->buck_gpios[0],
  707. (s5m8767->buck_gpioindex >> 2) & 0x1);
  708. /* SET2 GPIO */
  709. gpio_direction_output(pdata->buck_gpios[1],
  710. (s5m8767->buck_gpioindex >> 1) & 0x1);
  711. /* SET3 GPIO */
  712. gpio_direction_output(pdata->buck_gpios[2],
  713. (s5m8767->buck_gpioindex >> 0) & 0x1);
  714. }
  715. ret = devm_gpio_request(&pdev->dev, pdata->buck_ds[0], "S5M8767 DS2");
  716. if (ret)
  717. return ret;
  718. ret = devm_gpio_request(&pdev->dev, pdata->buck_ds[1], "S5M8767 DS3");
  719. if (ret)
  720. return ret;
  721. ret = devm_gpio_request(&pdev->dev, pdata->buck_ds[2], "S5M8767 DS4");
  722. if (ret)
  723. return ret;
  724. /* DS2 GPIO */
  725. gpio_direction_output(pdata->buck_ds[0], 0x0);
  726. /* DS3 GPIO */
  727. gpio_direction_output(pdata->buck_ds[1], 0x0);
  728. /* DS4 GPIO */
  729. gpio_direction_output(pdata->buck_ds[2], 0x0);
  730. if (pdata->buck2_gpiodvs || pdata->buck3_gpiodvs ||
  731. pdata->buck4_gpiodvs) {
  732. regmap_update_bits(s5m8767->iodev->regmap_pmic,
  733. S5M8767_REG_BUCK2CTRL, 1 << 1,
  734. (pdata->buck2_gpiodvs) ? (1 << 1) : (0 << 1));
  735. regmap_update_bits(s5m8767->iodev->regmap_pmic,
  736. S5M8767_REG_BUCK3CTRL, 1 << 1,
  737. (pdata->buck3_gpiodvs) ? (1 << 1) : (0 << 1));
  738. regmap_update_bits(s5m8767->iodev->regmap_pmic,
  739. S5M8767_REG_BUCK4CTRL, 1 << 1,
  740. (pdata->buck4_gpiodvs) ? (1 << 1) : (0 << 1));
  741. }
  742. /* Initialize GPIO DVS registers */
  743. for (i = 0; i < 8; i++) {
  744. if (s5m8767->buck2_gpiodvs) {
  745. regmap_write(s5m8767->iodev->regmap_pmic,
  746. S5M8767_REG_BUCK2DVS1 + i,
  747. s5m8767->buck2_vol[i]);
  748. }
  749. if (s5m8767->buck3_gpiodvs) {
  750. regmap_write(s5m8767->iodev->regmap_pmic,
  751. S5M8767_REG_BUCK3DVS1 + i,
  752. s5m8767->buck3_vol[i]);
  753. }
  754. if (s5m8767->buck4_gpiodvs) {
  755. regmap_write(s5m8767->iodev->regmap_pmic,
  756. S5M8767_REG_BUCK4DVS1 + i,
  757. s5m8767->buck4_vol[i]);
  758. }
  759. }
  760. if (s5m8767->buck2_ramp)
  761. regmap_update_bits(s5m8767->iodev->regmap_pmic,
  762. S5M8767_REG_DVSRAMP, 0x08, 0x08);
  763. if (s5m8767->buck3_ramp)
  764. regmap_update_bits(s5m8767->iodev->regmap_pmic,
  765. S5M8767_REG_DVSRAMP, 0x04, 0x04);
  766. if (s5m8767->buck4_ramp)
  767. regmap_update_bits(s5m8767->iodev->regmap_pmic,
  768. S5M8767_REG_DVSRAMP, 0x02, 0x02);
  769. if (s5m8767->buck2_ramp || s5m8767->buck3_ramp
  770. || s5m8767->buck4_ramp) {
  771. unsigned int val;
  772. switch (s5m8767->ramp_delay) {
  773. case 5:
  774. val = S5M8767_DVS_BUCK_RAMP_5;
  775. break;
  776. case 10:
  777. val = S5M8767_DVS_BUCK_RAMP_10;
  778. break;
  779. case 25:
  780. val = S5M8767_DVS_BUCK_RAMP_25;
  781. break;
  782. case 50:
  783. val = S5M8767_DVS_BUCK_RAMP_50;
  784. break;
  785. case 100:
  786. val = S5M8767_DVS_BUCK_RAMP_100;
  787. break;
  788. default:
  789. val = S5M8767_DVS_BUCK_RAMP_10;
  790. }
  791. regmap_update_bits(s5m8767->iodev->regmap_pmic,
  792. S5M8767_REG_DVSRAMP,
  793. S5M8767_DVS_BUCK_RAMP_MASK,
  794. val << S5M8767_DVS_BUCK_RAMP_SHIFT);
  795. }
  796. for (i = 0; i < pdata->num_regulators; i++) {
  797. const struct sec_voltage_desc *desc;
  798. int id = pdata->regulators[i].id;
  799. int enable_reg, enable_val;
  800. struct regulator_dev *rdev;
  801. desc = reg_voltage_map[id];
  802. if (desc) {
  803. regulators[id].n_voltages =
  804. (desc->max - desc->min) / desc->step + 1;
  805. regulators[id].min_uV = desc->min;
  806. regulators[id].uV_step = desc->step;
  807. regulators[id].vsel_reg =
  808. s5m8767_get_vsel_reg(id, s5m8767);
  809. if (id < S5M8767_BUCK1)
  810. regulators[id].vsel_mask = 0x3f;
  811. else
  812. regulators[id].vsel_mask = 0xff;
  813. ret = s5m8767_get_register(s5m8767, id, &enable_reg,
  814. &enable_val);
  815. if (ret) {
  816. dev_err(s5m8767->dev, "error reading registers\n");
  817. return ret;
  818. }
  819. regulators[id].enable_reg = enable_reg;
  820. regulators[id].enable_mask = S5M8767_ENCTRL_MASK;
  821. regulators[id].enable_val = enable_val;
  822. }
  823. config.dev = s5m8767->dev;
  824. config.init_data = pdata->regulators[i].initdata;
  825. config.driver_data = s5m8767;
  826. config.regmap = iodev->regmap_pmic;
  827. config.of_node = pdata->regulators[i].reg_node;
  828. config.ena_gpiod = NULL;
  829. if (pdata->regulators[i].ext_control_gpiod)
  830. s5m8767_regulator_config_ext_control(s5m8767,
  831. &pdata->regulators[i], &config);
  832. rdev = devm_regulator_register(&pdev->dev, &regulators[id],
  833. &config);
  834. if (IS_ERR(rdev)) {
  835. ret = PTR_ERR(rdev);
  836. dev_err(s5m8767->dev, "regulator init failed for %d\n",
  837. id);
  838. return ret;
  839. }
  840. if (pdata->regulators[i].ext_control_gpiod) {
  841. ret = s5m8767_enable_ext_control(s5m8767, rdev);
  842. if (ret < 0) {
  843. dev_err(s5m8767->dev,
  844. "failed to enable gpio control over %s: %d\n",
  845. rdev->desc->name, ret);
  846. return ret;
  847. }
  848. }
  849. }
  850. return 0;
  851. }
  852. static const struct platform_device_id s5m8767_pmic_id[] = {
  853. { "s5m8767-pmic", 0},
  854. { },
  855. };
  856. MODULE_DEVICE_TABLE(platform, s5m8767_pmic_id);
  857. static struct platform_driver s5m8767_pmic_driver = {
  858. .driver = {
  859. .name = "s5m8767-pmic",
  860. },
  861. .probe = s5m8767_pmic_probe,
  862. .id_table = s5m8767_pmic_id,
  863. };
  864. static int __init s5m8767_pmic_init(void)
  865. {
  866. return platform_driver_register(&s5m8767_pmic_driver);
  867. }
  868. subsys_initcall(s5m8767_pmic_init);
  869. static void __exit s5m8767_pmic_exit(void)
  870. {
  871. platform_driver_unregister(&s5m8767_pmic_driver);
  872. }
  873. module_exit(s5m8767_pmic_exit);
  874. /* Module information */
  875. MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>");
  876. MODULE_DESCRIPTION("SAMSUNG S5M8767 Regulator Driver");
  877. MODULE_LICENSE("GPL");