s5m8767.c 21 KB

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