s5m8767.c 25 KB

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