s5m8767.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  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/slab.h>
  17. #include <linux/module.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/regulator/driver.h>
  20. #include <linux/regulator/machine.h>
  21. #include <linux/mfd/s5m87xx/s5m-core.h>
  22. #include <linux/mfd/s5m87xx/s5m-pmic.h>
  23. struct s5m8767_info {
  24. struct device *dev;
  25. struct s5m87xx_dev *iodev;
  26. int num_regulators;
  27. struct regulator_dev **rdev;
  28. struct s5m_opmode_data *opmode;
  29. int ramp_delay;
  30. bool buck2_ramp;
  31. bool buck3_ramp;
  32. bool buck4_ramp;
  33. bool buck2_gpiodvs;
  34. bool buck3_gpiodvs;
  35. bool buck4_gpiodvs;
  36. u8 buck2_vol[8];
  37. u8 buck3_vol[8];
  38. u8 buck4_vol[8];
  39. int buck_gpios[3];
  40. int buck_gpioindex;
  41. };
  42. struct s5m_voltage_desc {
  43. int max;
  44. int min;
  45. int step;
  46. };
  47. static const struct s5m_voltage_desc buck_voltage_val1 = {
  48. .max = 2225000,
  49. .min = 650000,
  50. .step = 6250,
  51. };
  52. static const struct s5m_voltage_desc buck_voltage_val2 = {
  53. .max = 1600000,
  54. .min = 600000,
  55. .step = 6250,
  56. };
  57. static const struct s5m_voltage_desc buck_voltage_val3 = {
  58. .max = 3000000,
  59. .min = 750000,
  60. .step = 12500,
  61. };
  62. static const struct s5m_voltage_desc ldo_voltage_val1 = {
  63. .max = 3950000,
  64. .min = 800000,
  65. .step = 50000,
  66. };
  67. static const struct s5m_voltage_desc ldo_voltage_val2 = {
  68. .max = 2375000,
  69. .min = 800000,
  70. .step = 25000,
  71. };
  72. static const struct s5m_voltage_desc *reg_voltage_map[] = {
  73. [S5M8767_LDO1] = &ldo_voltage_val2,
  74. [S5M8767_LDO2] = &ldo_voltage_val2,
  75. [S5M8767_LDO3] = &ldo_voltage_val1,
  76. [S5M8767_LDO4] = &ldo_voltage_val1,
  77. [S5M8767_LDO5] = &ldo_voltage_val1,
  78. [S5M8767_LDO6] = &ldo_voltage_val2,
  79. [S5M8767_LDO7] = &ldo_voltage_val2,
  80. [S5M8767_LDO8] = &ldo_voltage_val2,
  81. [S5M8767_LDO9] = &ldo_voltage_val1,
  82. [S5M8767_LDO10] = &ldo_voltage_val1,
  83. [S5M8767_LDO11] = &ldo_voltage_val1,
  84. [S5M8767_LDO12] = &ldo_voltage_val1,
  85. [S5M8767_LDO13] = &ldo_voltage_val1,
  86. [S5M8767_LDO14] = &ldo_voltage_val1,
  87. [S5M8767_LDO15] = &ldo_voltage_val2,
  88. [S5M8767_LDO16] = &ldo_voltage_val1,
  89. [S5M8767_LDO17] = &ldo_voltage_val1,
  90. [S5M8767_LDO18] = &ldo_voltage_val1,
  91. [S5M8767_LDO19] = &ldo_voltage_val1,
  92. [S5M8767_LDO20] = &ldo_voltage_val1,
  93. [S5M8767_LDO21] = &ldo_voltage_val1,
  94. [S5M8767_LDO22] = &ldo_voltage_val1,
  95. [S5M8767_LDO23] = &ldo_voltage_val1,
  96. [S5M8767_LDO24] = &ldo_voltage_val1,
  97. [S5M8767_LDO25] = &ldo_voltage_val1,
  98. [S5M8767_LDO26] = &ldo_voltage_val1,
  99. [S5M8767_LDO27] = &ldo_voltage_val1,
  100. [S5M8767_LDO28] = &ldo_voltage_val1,
  101. [S5M8767_BUCK1] = &buck_voltage_val1,
  102. [S5M8767_BUCK2] = &buck_voltage_val2,
  103. [S5M8767_BUCK3] = &buck_voltage_val2,
  104. [S5M8767_BUCK4] = &buck_voltage_val2,
  105. [S5M8767_BUCK5] = &buck_voltage_val1,
  106. [S5M8767_BUCK6] = &buck_voltage_val1,
  107. [S5M8767_BUCK7] = NULL,
  108. [S5M8767_BUCK8] = NULL,
  109. [S5M8767_BUCK9] = &buck_voltage_val3,
  110. };
  111. static int s5m8767_list_voltage(struct regulator_dev *rdev,
  112. unsigned int selector)
  113. {
  114. const struct s5m_voltage_desc *desc;
  115. int reg_id = rdev_get_id(rdev);
  116. int val;
  117. if (reg_id >= ARRAY_SIZE(reg_voltage_map) || reg_id < 0)
  118. return -EINVAL;
  119. desc = reg_voltage_map[reg_id];
  120. if (desc == NULL)
  121. return -EINVAL;
  122. val = desc->min + desc->step * selector;
  123. if (val > desc->max)
  124. return -EINVAL;
  125. return val;
  126. }
  127. static unsigned int s5m8767_opmode_reg[][4] = {
  128. /* {OFF, ON, LOWPOWER, SUSPEND} */
  129. /* LDO1 ... LDO28 */
  130. {0x0, 0x3, 0x2, 0x1}, /* LDO1 */
  131. {0x0, 0x3, 0x2, 0x1},
  132. {0x0, 0x3, 0x2, 0x1},
  133. {0x0, 0x0, 0x0, 0x0},
  134. {0x0, 0x3, 0x2, 0x1}, /* LDO5 */
  135. {0x0, 0x3, 0x2, 0x1},
  136. {0x0, 0x3, 0x2, 0x1},
  137. {0x0, 0x3, 0x2, 0x1},
  138. {0x0, 0x3, 0x2, 0x1},
  139. {0x0, 0x3, 0x2, 0x1}, /* LDO10 */
  140. {0x0, 0x3, 0x2, 0x1},
  141. {0x0, 0x3, 0x2, 0x1},
  142. {0x0, 0x3, 0x2, 0x1},
  143. {0x0, 0x3, 0x2, 0x1},
  144. {0x0, 0x3, 0x2, 0x1}, /* LDO15 */
  145. {0x0, 0x3, 0x2, 0x1},
  146. {0x0, 0x3, 0x2, 0x1},
  147. {0x0, 0x0, 0x0, 0x0},
  148. {0x0, 0x3, 0x2, 0x1},
  149. {0x0, 0x3, 0x2, 0x1}, /* LDO20 */
  150. {0x0, 0x3, 0x2, 0x1},
  151. {0x0, 0x3, 0x2, 0x1},
  152. {0x0, 0x0, 0x0, 0x0},
  153. {0x0, 0x3, 0x2, 0x1},
  154. {0x0, 0x3, 0x2, 0x1}, /* LDO25 */
  155. {0x0, 0x3, 0x2, 0x1},
  156. {0x0, 0x3, 0x2, 0x1},
  157. {0x0, 0x3, 0x2, 0x1}, /* LDO28 */
  158. /* BUCK1 ... BUCK9 */
  159. {0x0, 0x3, 0x1, 0x1}, /* BUCK1 */
  160. {0x0, 0x3, 0x1, 0x1},
  161. {0x0, 0x3, 0x1, 0x1},
  162. {0x0, 0x3, 0x1, 0x1},
  163. {0x0, 0x3, 0x2, 0x1}, /* BUCK5 */
  164. {0x0, 0x3, 0x1, 0x1},
  165. {0x0, 0x3, 0x1, 0x1},
  166. {0x0, 0x3, 0x1, 0x1},
  167. {0x0, 0x3, 0x1, 0x1}, /* BUCK9 */
  168. };
  169. static int s5m8767_get_register(struct regulator_dev *rdev, int *reg,
  170. int *enable_ctrl)
  171. {
  172. int reg_id = rdev_get_id(rdev);
  173. unsigned int mode;
  174. struct s5m8767_info *s5m8767 = rdev_get_drvdata(rdev);
  175. switch (reg_id) {
  176. case S5M8767_LDO1 ... S5M8767_LDO2:
  177. *reg = S5M8767_REG_LDO1CTRL + (reg_id - S5M8767_LDO1);
  178. break;
  179. case S5M8767_LDO3 ... S5M8767_LDO28:
  180. *reg = S5M8767_REG_LDO3CTRL + (reg_id - S5M8767_LDO3);
  181. break;
  182. case S5M8767_BUCK1:
  183. *reg = S5M8767_REG_BUCK1CTRL1;
  184. break;
  185. case S5M8767_BUCK2 ... S5M8767_BUCK4:
  186. *reg = S5M8767_REG_BUCK2CTRL + (reg_id - S5M8767_BUCK2) * 9;
  187. break;
  188. case S5M8767_BUCK5:
  189. *reg = S5M8767_REG_BUCK5CTRL1;
  190. break;
  191. case S5M8767_BUCK6 ... S5M8767_BUCK9:
  192. *reg = S5M8767_REG_BUCK6CTRL1 + (reg_id - S5M8767_BUCK6) * 2;
  193. break;
  194. default:
  195. return -EINVAL;
  196. }
  197. mode = s5m8767->opmode[reg_id].mode;
  198. *enable_ctrl = s5m8767_opmode_reg[reg_id][mode] << S5M8767_ENCTRL_SHIFT;
  199. return 0;
  200. }
  201. static int s5m8767_reg_is_enabled(struct regulator_dev *rdev)
  202. {
  203. struct s5m8767_info *s5m8767 = rdev_get_drvdata(rdev);
  204. int ret, reg;
  205. int mask = 0xc0, enable_ctrl;
  206. u8 val;
  207. ret = s5m8767_get_register(rdev, &reg, &enable_ctrl);
  208. if (ret == -EINVAL)
  209. return 1;
  210. else if (ret)
  211. return ret;
  212. ret = s5m_reg_read(s5m8767->iodev, reg, &val);
  213. if (ret)
  214. return ret;
  215. return (val & mask) == enable_ctrl;
  216. }
  217. static int s5m8767_reg_enable(struct regulator_dev *rdev)
  218. {
  219. struct s5m8767_info *s5m8767 = rdev_get_drvdata(rdev);
  220. int ret, reg;
  221. int mask = 0xc0, enable_ctrl;
  222. ret = s5m8767_get_register(rdev, &reg, &enable_ctrl);
  223. if (ret)
  224. return ret;
  225. return s5m_reg_update(s5m8767->iodev, reg, enable_ctrl, mask);
  226. }
  227. static int s5m8767_reg_disable(struct regulator_dev *rdev)
  228. {
  229. struct s5m8767_info *s5m8767 = rdev_get_drvdata(rdev);
  230. int ret, reg;
  231. int mask = 0xc0, enable_ctrl;
  232. ret = s5m8767_get_register(rdev, &reg, &enable_ctrl);
  233. if (ret)
  234. return ret;
  235. return s5m_reg_update(s5m8767->iodev, reg, ~mask, mask);
  236. }
  237. static int s5m8767_get_voltage_register(struct regulator_dev *rdev, int *_reg)
  238. {
  239. struct s5m8767_info *s5m8767 = rdev_get_drvdata(rdev);
  240. int reg_id = rdev_get_id(rdev);
  241. int reg;
  242. switch (reg_id) {
  243. case S5M8767_LDO1 ... S5M8767_LDO2:
  244. reg = S5M8767_REG_LDO1CTRL + (reg_id - S5M8767_LDO1);
  245. break;
  246. case S5M8767_LDO3 ... S5M8767_LDO28:
  247. reg = S5M8767_REG_LDO3CTRL + (reg_id - S5M8767_LDO3);
  248. break;
  249. case S5M8767_BUCK1:
  250. reg = S5M8767_REG_BUCK1CTRL2;
  251. break;
  252. case S5M8767_BUCK2:
  253. reg = S5M8767_REG_BUCK2DVS1;
  254. if (s5m8767->buck2_gpiodvs)
  255. reg += s5m8767->buck_gpioindex;
  256. break;
  257. case S5M8767_BUCK3:
  258. reg = S5M8767_REG_BUCK3DVS1;
  259. if (s5m8767->buck3_gpiodvs)
  260. reg += s5m8767->buck_gpioindex;
  261. break;
  262. case S5M8767_BUCK4:
  263. reg = S5M8767_REG_BUCK4DVS1;
  264. if (s5m8767->buck4_gpiodvs)
  265. reg += s5m8767->buck_gpioindex;
  266. break;
  267. case S5M8767_BUCK5:
  268. reg = S5M8767_REG_BUCK5CTRL2;
  269. break;
  270. case S5M8767_BUCK6 ... S5M8767_BUCK9:
  271. reg = S5M8767_REG_BUCK6CTRL2 + (reg_id - S5M8767_BUCK6) * 2;
  272. break;
  273. default:
  274. return -EINVAL;
  275. }
  276. *_reg = reg;
  277. return 0;
  278. }
  279. static int s5m8767_get_voltage_sel(struct regulator_dev *rdev)
  280. {
  281. struct s5m8767_info *s5m8767 = rdev_get_drvdata(rdev);
  282. int reg, mask, ret;
  283. int reg_id = rdev_get_id(rdev);
  284. u8 val;
  285. ret = s5m8767_get_voltage_register(rdev, &reg);
  286. if (ret)
  287. return ret;
  288. mask = (reg_id < S5M8767_BUCK1) ? 0x3f : 0xff;
  289. ret = s5m_reg_read(s5m8767->iodev, reg, &val);
  290. if (ret)
  291. return ret;
  292. val &= mask;
  293. return val;
  294. }
  295. static int s5m8767_convert_voltage_to_sel(
  296. const struct s5m_voltage_desc *desc,
  297. int min_vol, int max_vol)
  298. {
  299. int selector = 0;
  300. if (desc == NULL)
  301. return -EINVAL;
  302. if (max_vol < desc->min || min_vol > desc->max)
  303. return -EINVAL;
  304. if (min_vol < desc->min)
  305. min_vol = desc->min;
  306. selector = DIV_ROUND_UP(min_vol - desc->min, desc->step);
  307. if (desc->min + desc->step * selector > max_vol)
  308. return -EINVAL;
  309. return selector;
  310. }
  311. static inline void s5m8767_set_high(struct s5m8767_info *s5m8767)
  312. {
  313. int temp_index = s5m8767->buck_gpioindex;
  314. gpio_set_value(s5m8767->buck_gpios[0], (temp_index >> 2) & 0x1);
  315. gpio_set_value(s5m8767->buck_gpios[1], (temp_index >> 1) & 0x1);
  316. gpio_set_value(s5m8767->buck_gpios[2], temp_index & 0x1);
  317. }
  318. static inline void s5m8767_set_low(struct s5m8767_info *s5m8767)
  319. {
  320. int temp_index = s5m8767->buck_gpioindex;
  321. gpio_set_value(s5m8767->buck_gpios[2], temp_index & 0x1);
  322. gpio_set_value(s5m8767->buck_gpios[1], (temp_index >> 1) & 0x1);
  323. gpio_set_value(s5m8767->buck_gpios[0], (temp_index >> 2) & 0x1);
  324. }
  325. static int s5m8767_set_voltage(struct regulator_dev *rdev,
  326. int min_uV, int max_uV, unsigned *selector)
  327. {
  328. struct s5m8767_info *s5m8767 = rdev_get_drvdata(rdev);
  329. const struct s5m_voltage_desc *desc;
  330. int reg_id = rdev_get_id(rdev);
  331. int sel, reg, mask, ret = 0, old_index, index = 0;
  332. u8 val;
  333. u8 *buck234_vol = NULL;
  334. switch (reg_id) {
  335. case S5M8767_LDO1 ... S5M8767_LDO28:
  336. mask = 0x3f;
  337. break;
  338. case S5M8767_BUCK1 ... S5M8767_BUCK6:
  339. mask = 0xff;
  340. if (reg_id == S5M8767_BUCK2 && s5m8767->buck2_gpiodvs)
  341. buck234_vol = &s5m8767->buck2_vol[0];
  342. else if (reg_id == S5M8767_BUCK3 && s5m8767->buck3_gpiodvs)
  343. buck234_vol = &s5m8767->buck3_vol[0];
  344. else if (reg_id == S5M8767_BUCK4 && s5m8767->buck4_gpiodvs)
  345. buck234_vol = &s5m8767->buck4_vol[0];
  346. break;
  347. case S5M8767_BUCK7 ... S5M8767_BUCK8:
  348. return -EINVAL;
  349. case S5M8767_BUCK9:
  350. mask = 0xff;
  351. break;
  352. default:
  353. return -EINVAL;
  354. }
  355. desc = reg_voltage_map[reg_id];
  356. sel = s5m8767_convert_voltage_to_sel(desc, min_uV, max_uV);
  357. if (sel < 0)
  358. return sel;
  359. /* buck234_vol != NULL means to control buck234 voltage via DVS GPIO */
  360. if (buck234_vol) {
  361. while (*buck234_vol != sel) {
  362. buck234_vol++;
  363. index++;
  364. }
  365. old_index = s5m8767->buck_gpioindex;
  366. s5m8767->buck_gpioindex = index;
  367. if (index > old_index)
  368. s5m8767_set_high(s5m8767);
  369. else
  370. s5m8767_set_low(s5m8767);
  371. } else {
  372. ret = s5m8767_get_voltage_register(rdev, &reg);
  373. if (ret)
  374. return ret;
  375. s5m_reg_read(s5m8767->iodev, reg, &val);
  376. val = (val & ~mask) | sel;
  377. ret = s5m_reg_write(s5m8767->iodev, reg, val);
  378. }
  379. *selector = sel;
  380. return ret;
  381. }
  382. static int s5m8767_set_voltage_time_sel(struct regulator_dev *rdev,
  383. unsigned int old_sel,
  384. unsigned int new_sel)
  385. {
  386. struct s5m8767_info *s5m8767 = rdev_get_drvdata(rdev);
  387. const struct s5m_voltage_desc *desc;
  388. int reg_id = rdev_get_id(rdev);
  389. desc = reg_voltage_map[reg_id];
  390. if (old_sel < new_sel)
  391. return DIV_ROUND_UP(desc->step * (new_sel - old_sel),
  392. s5m8767->ramp_delay * 1000);
  393. return 0;
  394. }
  395. static struct regulator_ops s5m8767_ops = {
  396. .list_voltage = s5m8767_list_voltage,
  397. .is_enabled = s5m8767_reg_is_enabled,
  398. .enable = s5m8767_reg_enable,
  399. .disable = s5m8767_reg_disable,
  400. .get_voltage_sel = s5m8767_get_voltage_sel,
  401. .set_voltage = s5m8767_set_voltage,
  402. .set_voltage_time_sel = s5m8767_set_voltage_time_sel,
  403. };
  404. #define s5m8767_regulator_desc(_name) { \
  405. .name = #_name, \
  406. .id = S5M8767_##_name, \
  407. .ops = &s5m8767_ops, \
  408. .type = REGULATOR_VOLTAGE, \
  409. .owner = THIS_MODULE, \
  410. }
  411. static struct regulator_desc regulators[] = {
  412. s5m8767_regulator_desc(LDO1),
  413. s5m8767_regulator_desc(LDO2),
  414. s5m8767_regulator_desc(LDO3),
  415. s5m8767_regulator_desc(LDO4),
  416. s5m8767_regulator_desc(LDO5),
  417. s5m8767_regulator_desc(LDO6),
  418. s5m8767_regulator_desc(LDO7),
  419. s5m8767_regulator_desc(LDO8),
  420. s5m8767_regulator_desc(LDO9),
  421. s5m8767_regulator_desc(LDO10),
  422. s5m8767_regulator_desc(LDO11),
  423. s5m8767_regulator_desc(LDO12),
  424. s5m8767_regulator_desc(LDO13),
  425. s5m8767_regulator_desc(LDO14),
  426. s5m8767_regulator_desc(LDO15),
  427. s5m8767_regulator_desc(LDO16),
  428. s5m8767_regulator_desc(LDO17),
  429. s5m8767_regulator_desc(LDO18),
  430. s5m8767_regulator_desc(LDO19),
  431. s5m8767_regulator_desc(LDO20),
  432. s5m8767_regulator_desc(LDO21),
  433. s5m8767_regulator_desc(LDO22),
  434. s5m8767_regulator_desc(LDO23),
  435. s5m8767_regulator_desc(LDO24),
  436. s5m8767_regulator_desc(LDO25),
  437. s5m8767_regulator_desc(LDO26),
  438. s5m8767_regulator_desc(LDO27),
  439. s5m8767_regulator_desc(LDO28),
  440. s5m8767_regulator_desc(BUCK1),
  441. s5m8767_regulator_desc(BUCK2),
  442. s5m8767_regulator_desc(BUCK3),
  443. s5m8767_regulator_desc(BUCK4),
  444. s5m8767_regulator_desc(BUCK5),
  445. s5m8767_regulator_desc(BUCK6),
  446. s5m8767_regulator_desc(BUCK7),
  447. s5m8767_regulator_desc(BUCK8),
  448. s5m8767_regulator_desc(BUCK9),
  449. };
  450. static __devinit int s5m8767_pmic_probe(struct platform_device *pdev)
  451. {
  452. struct s5m87xx_dev *iodev = dev_get_drvdata(pdev->dev.parent);
  453. struct s5m_platform_data *pdata = dev_get_platdata(iodev->dev);
  454. struct regulator_config config = { };
  455. struct regulator_dev **rdev;
  456. struct s5m8767_info *s5m8767;
  457. int i, ret, size;
  458. if (!pdata) {
  459. dev_err(pdev->dev.parent, "Platform data not supplied\n");
  460. return -ENODEV;
  461. }
  462. if (pdata->buck2_gpiodvs) {
  463. if (pdata->buck3_gpiodvs || pdata->buck4_gpiodvs) {
  464. dev_err(&pdev->dev, "S5M8767 GPIO DVS NOT VALID\n");
  465. return -EINVAL;
  466. }
  467. }
  468. if (pdata->buck3_gpiodvs) {
  469. if (pdata->buck2_gpiodvs || pdata->buck4_gpiodvs) {
  470. dev_err(&pdev->dev, "S5M8767 GPIO DVS NOT VALID\n");
  471. return -EINVAL;
  472. }
  473. }
  474. if (pdata->buck4_gpiodvs) {
  475. if (pdata->buck2_gpiodvs || pdata->buck3_gpiodvs) {
  476. dev_err(&pdev->dev, "S5M8767 GPIO DVS NOT VALID\n");
  477. return -EINVAL;
  478. }
  479. }
  480. s5m8767 = devm_kzalloc(&pdev->dev, sizeof(struct s5m8767_info),
  481. GFP_KERNEL);
  482. if (!s5m8767)
  483. return -ENOMEM;
  484. size = sizeof(struct regulator_dev *) * (S5M8767_REG_MAX - 2);
  485. s5m8767->rdev = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
  486. if (!s5m8767->rdev)
  487. return -ENOMEM;
  488. rdev = s5m8767->rdev;
  489. s5m8767->dev = &pdev->dev;
  490. s5m8767->iodev = iodev;
  491. s5m8767->num_regulators = S5M8767_REG_MAX - 2;
  492. platform_set_drvdata(pdev, s5m8767);
  493. s5m8767->buck_gpioindex = pdata->buck_default_idx;
  494. s5m8767->buck2_gpiodvs = pdata->buck2_gpiodvs;
  495. s5m8767->buck3_gpiodvs = pdata->buck3_gpiodvs;
  496. s5m8767->buck4_gpiodvs = pdata->buck4_gpiodvs;
  497. s5m8767->buck_gpios[0] = pdata->buck_gpios[0];
  498. s5m8767->buck_gpios[1] = pdata->buck_gpios[1];
  499. s5m8767->buck_gpios[2] = pdata->buck_gpios[2];
  500. s5m8767->ramp_delay = pdata->buck_ramp_delay;
  501. s5m8767->buck2_ramp = pdata->buck2_ramp_enable;
  502. s5m8767->buck3_ramp = pdata->buck3_ramp_enable;
  503. s5m8767->buck4_ramp = pdata->buck4_ramp_enable;
  504. s5m8767->opmode = pdata->opmode;
  505. for (i = 0; i < 8; i++) {
  506. if (s5m8767->buck2_gpiodvs) {
  507. s5m8767->buck2_vol[i] =
  508. s5m8767_convert_voltage_to_sel(
  509. &buck_voltage_val2,
  510. pdata->buck2_voltage[i],
  511. pdata->buck2_voltage[i] +
  512. buck_voltage_val2.step);
  513. }
  514. if (s5m8767->buck3_gpiodvs) {
  515. s5m8767->buck3_vol[i] =
  516. s5m8767_convert_voltage_to_sel(
  517. &buck_voltage_val2,
  518. pdata->buck3_voltage[i],
  519. pdata->buck3_voltage[i] +
  520. buck_voltage_val2.step);
  521. }
  522. if (s5m8767->buck4_gpiodvs) {
  523. s5m8767->buck4_vol[i] =
  524. s5m8767_convert_voltage_to_sel(
  525. &buck_voltage_val2,
  526. pdata->buck4_voltage[i],
  527. pdata->buck4_voltage[i] +
  528. buck_voltage_val2.step);
  529. }
  530. }
  531. if (pdata->buck2_gpiodvs || pdata->buck3_gpiodvs ||
  532. pdata->buck4_gpiodvs) {
  533. if (gpio_is_valid(pdata->buck_gpios[0]) &&
  534. gpio_is_valid(pdata->buck_gpios[1]) &&
  535. gpio_is_valid(pdata->buck_gpios[2])) {
  536. ret = gpio_request(pdata->buck_gpios[0],
  537. "S5M8767 SET1");
  538. if (ret == -EBUSY)
  539. dev_warn(&pdev->dev, "Duplicated gpio request for SET1\n");
  540. ret = gpio_request(pdata->buck_gpios[1],
  541. "S5M8767 SET2");
  542. if (ret == -EBUSY)
  543. dev_warn(&pdev->dev, "Duplicated gpio request for SET2\n");
  544. ret = gpio_request(pdata->buck_gpios[2],
  545. "S5M8767 SET3");
  546. if (ret == -EBUSY)
  547. dev_warn(&pdev->dev, "Duplicated gpio request for SET3\n");
  548. /* SET1 GPIO */
  549. gpio_direction_output(pdata->buck_gpios[0],
  550. (s5m8767->buck_gpioindex >> 2) & 0x1);
  551. /* SET2 GPIO */
  552. gpio_direction_output(pdata->buck_gpios[1],
  553. (s5m8767->buck_gpioindex >> 1) & 0x1);
  554. /* SET3 GPIO */
  555. gpio_direction_output(pdata->buck_gpios[2],
  556. (s5m8767->buck_gpioindex >> 0) & 0x1);
  557. ret = 0;
  558. } else {
  559. dev_err(&pdev->dev, "GPIO NOT VALID\n");
  560. ret = -EINVAL;
  561. return ret;
  562. }
  563. }
  564. s5m_reg_update(s5m8767->iodev, S5M8767_REG_BUCK2CTRL,
  565. (pdata->buck2_gpiodvs) ? (1 << 1) : (0 << 1), 1 << 1);
  566. s5m_reg_update(s5m8767->iodev, S5M8767_REG_BUCK3CTRL,
  567. (pdata->buck3_gpiodvs) ? (1 << 1) : (0 << 1), 1 << 1);
  568. s5m_reg_update(s5m8767->iodev, S5M8767_REG_BUCK4CTRL,
  569. (pdata->buck4_gpiodvs) ? (1 << 1) : (0 << 1), 1 << 1);
  570. /* Initialize GPIO DVS registers */
  571. for (i = 0; i < 8; i++) {
  572. if (s5m8767->buck2_gpiodvs) {
  573. s5m_reg_write(s5m8767->iodev, S5M8767_REG_BUCK2DVS1 + i,
  574. s5m8767->buck2_vol[i]);
  575. }
  576. if (s5m8767->buck3_gpiodvs) {
  577. s5m_reg_write(s5m8767->iodev, S5M8767_REG_BUCK3DVS1 + i,
  578. s5m8767->buck3_vol[i]);
  579. }
  580. if (s5m8767->buck4_gpiodvs) {
  581. s5m_reg_write(s5m8767->iodev, S5M8767_REG_BUCK4DVS1 + i,
  582. s5m8767->buck4_vol[i]);
  583. }
  584. }
  585. s5m_reg_update(s5m8767->iodev, S5M8767_REG_BUCK2CTRL, 0x78, 0xff);
  586. s5m_reg_update(s5m8767->iodev, S5M8767_REG_BUCK3CTRL, 0x58, 0xff);
  587. s5m_reg_update(s5m8767->iodev, S5M8767_REG_BUCK4CTRL, 0x78, 0xff);
  588. if (s5m8767->buck2_ramp)
  589. s5m_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP, 0x08, 0x08);
  590. if (s5m8767->buck3_ramp)
  591. s5m_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP, 0x04, 0x04);
  592. if (s5m8767->buck4_ramp)
  593. s5m_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP, 0x02, 0x02);
  594. if (s5m8767->buck2_ramp || s5m8767->buck3_ramp
  595. || s5m8767->buck4_ramp) {
  596. switch (s5m8767->ramp_delay) {
  597. case 15:
  598. s5m_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP,
  599. 0xc0, 0xf0);
  600. break;
  601. case 25:
  602. s5m_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP,
  603. 0xd0, 0xf0);
  604. break;
  605. case 50:
  606. s5m_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP,
  607. 0xe0, 0xf0);
  608. break;
  609. case 100:
  610. s5m_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP,
  611. 0xf0, 0xf0);
  612. break;
  613. default:
  614. s5m_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP,
  615. 0x90, 0xf0);
  616. }
  617. }
  618. for (i = 0; i < pdata->num_regulators; i++) {
  619. const struct s5m_voltage_desc *desc;
  620. int id = pdata->regulators[i].id;
  621. desc = reg_voltage_map[id];
  622. if (desc)
  623. regulators[id].n_voltages =
  624. (desc->max - desc->min) / desc->step + 1;
  625. config.dev = s5m8767->dev;
  626. config.init_data = pdata->regulators[i].initdata;
  627. config.driver_data = s5m8767;
  628. rdev[i] = regulator_register(&regulators[id], &config);
  629. if (IS_ERR(rdev[i])) {
  630. ret = PTR_ERR(rdev[i]);
  631. dev_err(s5m8767->dev, "regulator init failed for %d\n",
  632. id);
  633. rdev[i] = NULL;
  634. goto err;
  635. }
  636. }
  637. return 0;
  638. err:
  639. for (i = 0; i < s5m8767->num_regulators; i++)
  640. if (rdev[i])
  641. regulator_unregister(rdev[i]);
  642. return ret;
  643. }
  644. static int __devexit s5m8767_pmic_remove(struct platform_device *pdev)
  645. {
  646. struct s5m8767_info *s5m8767 = platform_get_drvdata(pdev);
  647. struct regulator_dev **rdev = s5m8767->rdev;
  648. int i;
  649. for (i = 0; i < s5m8767->num_regulators; i++)
  650. if (rdev[i])
  651. regulator_unregister(rdev[i]);
  652. return 0;
  653. }
  654. static const struct platform_device_id s5m8767_pmic_id[] = {
  655. { "s5m8767-pmic", 0},
  656. { },
  657. };
  658. MODULE_DEVICE_TABLE(platform, s5m8767_pmic_id);
  659. static struct platform_driver s5m8767_pmic_driver = {
  660. .driver = {
  661. .name = "s5m8767-pmic",
  662. .owner = THIS_MODULE,
  663. },
  664. .probe = s5m8767_pmic_probe,
  665. .remove = __devexit_p(s5m8767_pmic_remove),
  666. .id_table = s5m8767_pmic_id,
  667. };
  668. static int __init s5m8767_pmic_init(void)
  669. {
  670. return platform_driver_register(&s5m8767_pmic_driver);
  671. }
  672. subsys_initcall(s5m8767_pmic_init);
  673. static void __exit s5m8767_pmic_exit(void)
  674. {
  675. platform_driver_unregister(&s5m8767_pmic_driver);
  676. }
  677. module_exit(s5m8767_pmic_exit);
  678. /* Module information */
  679. MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>");
  680. MODULE_DESCRIPTION("SAMSUNG S5M8767 Regulator Driver");
  681. MODULE_LICENSE("GPL");