s5m8767.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  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. 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 int s5m8767_get_register(struct regulator_dev *rdev, int *reg)
  128. {
  129. int reg_id = rdev_get_id(rdev);
  130. switch (reg_id) {
  131. case S5M8767_LDO1 ... S5M8767_LDO2:
  132. *reg = S5M8767_REG_LDO1CTRL + (reg_id - S5M8767_LDO1);
  133. break;
  134. case S5M8767_LDO3 ... S5M8767_LDO28:
  135. *reg = S5M8767_REG_LDO3CTRL + (reg_id - S5M8767_LDO3);
  136. break;
  137. case S5M8767_BUCK1:
  138. *reg = S5M8767_REG_BUCK1CTRL1;
  139. break;
  140. case S5M8767_BUCK2 ... S5M8767_BUCK4:
  141. *reg = S5M8767_REG_BUCK2CTRL + (reg_id - S5M8767_BUCK2) * 9;
  142. break;
  143. case S5M8767_BUCK5:
  144. *reg = S5M8767_REG_BUCK5CTRL1;
  145. break;
  146. case S5M8767_BUCK6 ... S5M8767_BUCK9:
  147. *reg = S5M8767_REG_BUCK6CTRL1 + (reg_id - S5M8767_BUCK6) * 2;
  148. break;
  149. default:
  150. return -EINVAL;
  151. }
  152. return 0;
  153. }
  154. static int s5m8767_reg_is_enabled(struct regulator_dev *rdev)
  155. {
  156. struct s5m8767_info *s5m8767 = rdev_get_drvdata(rdev);
  157. int ret, reg;
  158. int mask = 0xc0, pattern = 0xc0;
  159. u8 val;
  160. ret = s5m8767_get_register(rdev, &reg);
  161. if (ret == -EINVAL)
  162. return 1;
  163. else if (ret)
  164. return ret;
  165. ret = s5m_reg_read(s5m8767->iodev, reg, &val);
  166. if (ret)
  167. return ret;
  168. return (val & mask) == pattern;
  169. }
  170. static int s5m8767_reg_enable(struct regulator_dev *rdev)
  171. {
  172. struct s5m8767_info *s5m8767 = rdev_get_drvdata(rdev);
  173. int ret, reg;
  174. int mask = 0xc0, pattern = 0xc0;
  175. ret = s5m8767_get_register(rdev, &reg);
  176. if (ret)
  177. return ret;
  178. return s5m_reg_update(s5m8767->iodev, reg, pattern, mask);
  179. }
  180. static int s5m8767_reg_disable(struct regulator_dev *rdev)
  181. {
  182. struct s5m8767_info *s5m8767 = rdev_get_drvdata(rdev);
  183. int ret, reg;
  184. int mask = 0xc0, pattern = 0xc0;
  185. ret = s5m8767_get_register(rdev, &reg);
  186. if (ret)
  187. return ret;
  188. return s5m_reg_update(s5m8767->iodev, reg, ~pattern, mask);
  189. }
  190. static int s5m8767_get_voltage_register(struct regulator_dev *rdev, int *_reg)
  191. {
  192. struct s5m8767_info *s5m8767 = rdev_get_drvdata(rdev);
  193. int reg_id = rdev_get_id(rdev);
  194. int reg;
  195. switch (reg_id) {
  196. case S5M8767_LDO1 ... S5M8767_LDO2:
  197. reg = S5M8767_REG_LDO1CTRL + (reg_id - S5M8767_LDO1);
  198. break;
  199. case S5M8767_LDO3 ... S5M8767_LDO28:
  200. reg = S5M8767_REG_LDO3CTRL + (reg_id - S5M8767_LDO3);
  201. break;
  202. case S5M8767_BUCK1:
  203. reg = S5M8767_REG_BUCK1CTRL2;
  204. break;
  205. case S5M8767_BUCK2:
  206. reg = S5M8767_REG_BUCK2DVS1;
  207. if (s5m8767->buck2_gpiodvs)
  208. reg += s5m8767->buck_gpioindex;
  209. break;
  210. case S5M8767_BUCK3:
  211. reg = S5M8767_REG_BUCK3DVS1;
  212. if (s5m8767->buck3_gpiodvs)
  213. reg += s5m8767->buck_gpioindex;
  214. break;
  215. case S5M8767_BUCK4:
  216. reg = S5M8767_REG_BUCK4DVS1;
  217. if (s5m8767->buck4_gpiodvs)
  218. reg += s5m8767->buck_gpioindex;
  219. break;
  220. case S5M8767_BUCK5:
  221. reg = S5M8767_REG_BUCK5CTRL2;
  222. break;
  223. case S5M8767_BUCK6 ... S5M8767_BUCK9:
  224. reg = S5M8767_REG_BUCK6CTRL2 + (reg_id - S5M8767_BUCK6) * 2;
  225. break;
  226. default:
  227. return -EINVAL;
  228. }
  229. *_reg = reg;
  230. return 0;
  231. }
  232. static int s5m8767_get_voltage_sel(struct regulator_dev *rdev)
  233. {
  234. struct s5m8767_info *s5m8767 = rdev_get_drvdata(rdev);
  235. int reg, mask, ret;
  236. int reg_id = rdev_get_id(rdev);
  237. u8 val;
  238. ret = s5m8767_get_voltage_register(rdev, &reg);
  239. if (ret)
  240. return ret;
  241. mask = (reg_id < S5M8767_BUCK1) ? 0x3f : 0xff;
  242. ret = s5m_reg_read(s5m8767->iodev, reg, &val);
  243. if (ret)
  244. return ret;
  245. val &= mask;
  246. return val;
  247. }
  248. static int s5m8767_convert_voltage_to_sel(
  249. const struct s5m_voltage_desc *desc,
  250. int min_vol, int max_vol)
  251. {
  252. int selector = 0;
  253. if (desc == NULL)
  254. return -EINVAL;
  255. if (max_vol < desc->min || min_vol > desc->max)
  256. return -EINVAL;
  257. selector = (min_vol - desc->min) / desc->step;
  258. if (desc->min + desc->step * selector > max_vol)
  259. return -EINVAL;
  260. return selector;
  261. }
  262. static int s5m8767_set_voltage(struct regulator_dev *rdev,
  263. int min_uV, int max_uV, unsigned *selector)
  264. {
  265. struct s5m8767_info *s5m8767 = rdev_get_drvdata(rdev);
  266. const struct s5m_voltage_desc *desc;
  267. int reg_id = rdev_get_id(rdev);
  268. int sel, reg, mask, ret;
  269. u8 val;
  270. switch (reg_id) {
  271. case S5M8767_LDO1 ... S5M8767_LDO28:
  272. mask = 0x3f;
  273. break;
  274. case S5M8767_BUCK1 ... S5M8767_BUCK6:
  275. mask = 0xff;
  276. break;
  277. case S5M8767_BUCK7 ... S5M8767_BUCK8:
  278. return -EINVAL;
  279. case S5M8767_BUCK9:
  280. mask = 0xff;
  281. break;
  282. default:
  283. return -EINVAL;
  284. }
  285. desc = reg_voltage_map[reg_id];
  286. sel = s5m8767_convert_voltage_to_sel(desc, min_uV, max_uV);
  287. if (sel < 0)
  288. return sel;
  289. ret = s5m8767_get_voltage_register(rdev, &reg);
  290. if (ret)
  291. return ret;
  292. s5m_reg_read(s5m8767->iodev, reg, &val);
  293. val &= ~mask;
  294. val |= sel;
  295. ret = s5m_reg_write(s5m8767->iodev, reg, val);
  296. *selector = sel;
  297. return ret;
  298. }
  299. static inline void s5m8767_set_high(struct s5m8767_info *s5m8767)
  300. {
  301. int temp_index = s5m8767->buck_gpioindex;
  302. gpio_set_value(s5m8767->buck_gpios[0], (temp_index >> 2) & 0x1);
  303. gpio_set_value(s5m8767->buck_gpios[1], (temp_index >> 1) & 0x1);
  304. gpio_set_value(s5m8767->buck_gpios[2], temp_index & 0x1);
  305. }
  306. static inline void s5m8767_set_low(struct s5m8767_info *s5m8767)
  307. {
  308. int temp_index = s5m8767->buck_gpioindex;
  309. gpio_set_value(s5m8767->buck_gpios[2], temp_index & 0x1);
  310. gpio_set_value(s5m8767->buck_gpios[1], (temp_index >> 1) & 0x1);
  311. gpio_set_value(s5m8767->buck_gpios[0], (temp_index >> 2) & 0x1);
  312. }
  313. static int s5m8767_set_voltage_buck(struct regulator_dev *rdev,
  314. int min_uV, int max_uV, unsigned *selector)
  315. {
  316. struct s5m8767_info *s5m8767 = rdev_get_drvdata(rdev);
  317. int reg_id = rdev_get_id(rdev);
  318. const struct s5m_voltage_desc *desc;
  319. int new_val, old_val, i = 0;
  320. if (reg_id < S5M8767_BUCK1 || reg_id > S5M8767_BUCK6)
  321. return -EINVAL;
  322. switch (reg_id) {
  323. case S5M8767_BUCK1:
  324. return s5m8767_set_voltage(rdev, min_uV, max_uV, selector);
  325. case S5M8767_BUCK2 ... S5M8767_BUCK4:
  326. break;
  327. case S5M8767_BUCK5 ... S5M8767_BUCK6:
  328. return s5m8767_set_voltage(rdev, min_uV, max_uV, selector);
  329. case S5M8767_BUCK9:
  330. return s5m8767_set_voltage(rdev, min_uV, max_uV, selector);
  331. }
  332. desc = reg_voltage_map[reg_id];
  333. new_val = s5m8767_convert_voltage_to_sel(desc, min_uV, max_uV);
  334. if (new_val < 0)
  335. return new_val;
  336. switch (reg_id) {
  337. case S5M8767_BUCK2:
  338. if (s5m8767->buck2_gpiodvs) {
  339. while (s5m8767->buck2_vol[i] != new_val)
  340. i++;
  341. } else
  342. return s5m8767_set_voltage(rdev, min_uV,
  343. max_uV, selector);
  344. break;
  345. case S5M8767_BUCK3:
  346. if (s5m8767->buck3_gpiodvs) {
  347. while (s5m8767->buck3_vol[i] != new_val)
  348. i++;
  349. } else
  350. return s5m8767_set_voltage(rdev, min_uV,
  351. max_uV, selector);
  352. break;
  353. case S5M8767_BUCK4:
  354. if (s5m8767->buck3_gpiodvs) {
  355. while (s5m8767->buck4_vol[i] != new_val)
  356. i++;
  357. } else
  358. return s5m8767_set_voltage(rdev, min_uV,
  359. max_uV, selector);
  360. break;
  361. }
  362. old_val = s5m8767->buck_gpioindex;
  363. s5m8767->buck_gpioindex = i;
  364. if (i > old_val)
  365. s5m8767_set_high(s5m8767);
  366. else
  367. s5m8767_set_low(s5m8767);
  368. *selector = new_val;
  369. return 0;
  370. }
  371. static int s5m8767_set_voltage_time_sel(struct regulator_dev *rdev,
  372. unsigned int old_sel,
  373. unsigned int new_sel)
  374. {
  375. struct s5m8767_info *s5m8767 = rdev_get_drvdata(rdev);
  376. const struct s5m_voltage_desc *desc;
  377. int reg_id = rdev_get_id(rdev);
  378. desc = reg_voltage_map[reg_id];
  379. if (old_sel < new_sel)
  380. return DIV_ROUND_UP(desc->step * (new_sel - old_sel),
  381. s5m8767->ramp_delay * 1000);
  382. return 0;
  383. }
  384. static struct regulator_ops s5m8767_ldo_ops = {
  385. .list_voltage = s5m8767_list_voltage,
  386. .is_enabled = s5m8767_reg_is_enabled,
  387. .enable = s5m8767_reg_enable,
  388. .disable = s5m8767_reg_disable,
  389. .get_voltage_sel = s5m8767_get_voltage_sel,
  390. .set_voltage = s5m8767_set_voltage,
  391. .set_voltage_time_sel = s5m8767_set_voltage_time_sel,
  392. };
  393. static struct regulator_ops s5m8767_buck_ops = {
  394. .list_voltage = s5m8767_list_voltage,
  395. .is_enabled = s5m8767_reg_is_enabled,
  396. .enable = s5m8767_reg_enable,
  397. .disable = s5m8767_reg_disable,
  398. .get_voltage_sel = s5m8767_get_voltage_sel,
  399. .set_voltage = s5m8767_set_voltage_buck,
  400. .set_voltage_time_sel = s5m8767_set_voltage_time_sel,
  401. };
  402. #define regulator_desc_ldo(num) { \
  403. .name = "LDO"#num, \
  404. .id = S5M8767_LDO##num, \
  405. .ops = &s5m8767_ldo_ops, \
  406. .type = REGULATOR_VOLTAGE, \
  407. .owner = THIS_MODULE, \
  408. }
  409. #define regulator_desc_buck(num) { \
  410. .name = "BUCK"#num, \
  411. .id = S5M8767_BUCK##num, \
  412. .ops = &s5m8767_buck_ops, \
  413. .type = REGULATOR_VOLTAGE, \
  414. .owner = THIS_MODULE, \
  415. }
  416. static struct regulator_desc regulators[] = {
  417. regulator_desc_ldo(1),
  418. regulator_desc_ldo(2),
  419. regulator_desc_ldo(3),
  420. regulator_desc_ldo(4),
  421. regulator_desc_ldo(5),
  422. regulator_desc_ldo(6),
  423. regulator_desc_ldo(7),
  424. regulator_desc_ldo(8),
  425. regulator_desc_ldo(9),
  426. regulator_desc_ldo(10),
  427. regulator_desc_ldo(11),
  428. regulator_desc_ldo(12),
  429. regulator_desc_ldo(13),
  430. regulator_desc_ldo(14),
  431. regulator_desc_ldo(15),
  432. regulator_desc_ldo(16),
  433. regulator_desc_ldo(17),
  434. regulator_desc_ldo(18),
  435. regulator_desc_ldo(19),
  436. regulator_desc_ldo(20),
  437. regulator_desc_ldo(21),
  438. regulator_desc_ldo(22),
  439. regulator_desc_ldo(23),
  440. regulator_desc_ldo(24),
  441. regulator_desc_ldo(25),
  442. regulator_desc_ldo(26),
  443. regulator_desc_ldo(27),
  444. regulator_desc_ldo(28),
  445. regulator_desc_buck(1),
  446. regulator_desc_buck(2),
  447. regulator_desc_buck(3),
  448. regulator_desc_buck(4),
  449. regulator_desc_buck(5),
  450. regulator_desc_buck(6),
  451. regulator_desc_buck(7),
  452. regulator_desc_buck(8),
  453. regulator_desc_buck(9),
  454. };
  455. static __devinit int s5m8767_pmic_probe(struct platform_device *pdev)
  456. {
  457. struct s5m87xx_dev *iodev = dev_get_drvdata(pdev->dev.parent);
  458. struct s5m_platform_data *pdata = dev_get_platdata(iodev->dev);
  459. struct regulator_dev **rdev;
  460. struct s5m8767_info *s5m8767;
  461. int i, ret, size;
  462. if (!pdata) {
  463. dev_err(pdev->dev.parent, "Platform data not supplied\n");
  464. return -ENODEV;
  465. }
  466. if (pdata->buck2_gpiodvs) {
  467. if (pdata->buck3_gpiodvs || pdata->buck4_gpiodvs) {
  468. dev_err(&pdev->dev, "S5M8767 GPIO DVS NOT VALID\n");
  469. return -EINVAL;
  470. }
  471. }
  472. if (pdata->buck3_gpiodvs) {
  473. if (pdata->buck2_gpiodvs || pdata->buck4_gpiodvs) {
  474. dev_err(&pdev->dev, "S5M8767 GPIO DVS NOT VALID\n");
  475. return -EINVAL;
  476. }
  477. }
  478. if (pdata->buck4_gpiodvs) {
  479. if (pdata->buck2_gpiodvs || pdata->buck3_gpiodvs) {
  480. dev_err(&pdev->dev, "S5M8767 GPIO DVS NOT VALID\n");
  481. return -EINVAL;
  482. }
  483. }
  484. s5m8767 = devm_kzalloc(&pdev->dev, sizeof(struct s5m8767_info),
  485. GFP_KERNEL);
  486. if (!s5m8767)
  487. return -ENOMEM;
  488. size = sizeof(struct regulator_dev *) * (S5M8767_REG_MAX - 2);
  489. s5m8767->rdev = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
  490. if (!s5m8767->rdev)
  491. return -ENOMEM;
  492. rdev = s5m8767->rdev;
  493. s5m8767->dev = &pdev->dev;
  494. s5m8767->iodev = iodev;
  495. s5m8767->num_regulators = S5M8767_REG_MAX - 2;
  496. platform_set_drvdata(pdev, s5m8767);
  497. s5m8767->buck_gpioindex = pdata->buck_default_idx;
  498. s5m8767->buck2_gpiodvs = pdata->buck2_gpiodvs;
  499. s5m8767->buck3_gpiodvs = pdata->buck3_gpiodvs;
  500. s5m8767->buck4_gpiodvs = pdata->buck4_gpiodvs;
  501. s5m8767->buck_gpios[0] = pdata->buck_gpios[0];
  502. s5m8767->buck_gpios[1] = pdata->buck_gpios[1];
  503. s5m8767->buck_gpios[2] = pdata->buck_gpios[2];
  504. s5m8767->ramp_delay = pdata->buck_ramp_delay;
  505. s5m8767->buck2_ramp = pdata->buck2_ramp_enable;
  506. s5m8767->buck3_ramp = pdata->buck3_ramp_enable;
  507. s5m8767->buck4_ramp = pdata->buck4_ramp_enable;
  508. for (i = 0; i < 8; i++) {
  509. if (s5m8767->buck2_gpiodvs) {
  510. s5m8767->buck2_vol[i] =
  511. s5m8767_convert_voltage_to_sel(
  512. &buck_voltage_val2,
  513. pdata->buck2_voltage[i],
  514. pdata->buck2_voltage[i] +
  515. buck_voltage_val2.step);
  516. }
  517. if (s5m8767->buck3_gpiodvs) {
  518. s5m8767->buck3_vol[i] =
  519. s5m8767_convert_voltage_to_sel(
  520. &buck_voltage_val2,
  521. pdata->buck3_voltage[i],
  522. pdata->buck3_voltage[i] +
  523. buck_voltage_val2.step);
  524. }
  525. if (s5m8767->buck4_gpiodvs) {
  526. s5m8767->buck4_vol[i] =
  527. s5m8767_convert_voltage_to_sel(
  528. &buck_voltage_val2,
  529. pdata->buck4_voltage[i],
  530. pdata->buck4_voltage[i] +
  531. buck_voltage_val2.step);
  532. }
  533. }
  534. if (pdata->buck2_gpiodvs || pdata->buck3_gpiodvs ||
  535. pdata->buck4_gpiodvs) {
  536. if (gpio_is_valid(pdata->buck_gpios[0]) &&
  537. gpio_is_valid(pdata->buck_gpios[1]) &&
  538. gpio_is_valid(pdata->buck_gpios[2])) {
  539. ret = gpio_request(pdata->buck_gpios[0],
  540. "S5M8767 SET1");
  541. if (ret == -EBUSY)
  542. dev_warn(&pdev->dev, "Duplicated gpio request for SET1\n");
  543. ret = gpio_request(pdata->buck_gpios[1],
  544. "S5M8767 SET2");
  545. if (ret == -EBUSY)
  546. dev_warn(&pdev->dev, "Duplicated gpio request for SET2\n");
  547. ret = gpio_request(pdata->buck_gpios[2],
  548. "S5M8767 SET3");
  549. if (ret == -EBUSY)
  550. dev_warn(&pdev->dev, "Duplicated gpio request for SET3\n");
  551. /* SET1 GPIO */
  552. gpio_direction_output(pdata->buck_gpios[0],
  553. (s5m8767->buck_gpioindex >> 2) & 0x1);
  554. /* SET2 GPIO */
  555. gpio_direction_output(pdata->buck_gpios[1],
  556. (s5m8767->buck_gpioindex >> 1) & 0x1);
  557. /* SET3 GPIO */
  558. gpio_direction_output(pdata->buck_gpios[2],
  559. (s5m8767->buck_gpioindex >> 0) & 0x1);
  560. ret = 0;
  561. } else {
  562. dev_err(&pdev->dev, "GPIO NOT VALID\n");
  563. ret = -EINVAL;
  564. return ret;
  565. }
  566. }
  567. s5m_reg_update(s5m8767->iodev, S5M8767_REG_BUCK2CTRL,
  568. (pdata->buck2_gpiodvs) ? (1 << 1) : (0 << 1), 1 << 1);
  569. s5m_reg_update(s5m8767->iodev, S5M8767_REG_BUCK3CTRL,
  570. (pdata->buck3_gpiodvs) ? (1 << 1) : (0 << 1), 1 << 1);
  571. s5m_reg_update(s5m8767->iodev, S5M8767_REG_BUCK4CTRL,
  572. (pdata->buck4_gpiodvs) ? (1 << 1) : (0 << 1), 1 << 1);
  573. /* Initialize GPIO DVS registers */
  574. for (i = 0; i < 8; i++) {
  575. if (s5m8767->buck2_gpiodvs) {
  576. s5m_reg_write(s5m8767->iodev, S5M8767_REG_BUCK2DVS1 + i,
  577. s5m8767->buck2_vol[i]);
  578. }
  579. if (s5m8767->buck3_gpiodvs) {
  580. s5m_reg_write(s5m8767->iodev, S5M8767_REG_BUCK3DVS1 + i,
  581. s5m8767->buck3_vol[i]);
  582. }
  583. if (s5m8767->buck4_gpiodvs) {
  584. s5m_reg_write(s5m8767->iodev, S5M8767_REG_BUCK4DVS1 + i,
  585. s5m8767->buck4_vol[i]);
  586. }
  587. }
  588. s5m_reg_update(s5m8767->iodev, S5M8767_REG_BUCK2CTRL, 0x78, 0xff);
  589. s5m_reg_update(s5m8767->iodev, S5M8767_REG_BUCK3CTRL, 0x58, 0xff);
  590. s5m_reg_update(s5m8767->iodev, S5M8767_REG_BUCK4CTRL, 0x78, 0xff);
  591. if (s5m8767->buck2_ramp)
  592. s5m_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP, 0x08, 0x08);
  593. if (s5m8767->buck3_ramp)
  594. s5m_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP, 0x04, 0x04);
  595. if (s5m8767->buck4_ramp)
  596. s5m_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP, 0x02, 0x02);
  597. if (s5m8767->buck2_ramp || s5m8767->buck3_ramp
  598. || s5m8767->buck4_ramp) {
  599. switch (s5m8767->ramp_delay) {
  600. case 15:
  601. s5m_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP,
  602. 0xc0, 0xf0);
  603. break;
  604. case 25:
  605. s5m_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP,
  606. 0xd0, 0xf0);
  607. break;
  608. case 50:
  609. s5m_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP,
  610. 0xe0, 0xf0);
  611. break;
  612. case 100:
  613. s5m_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP,
  614. 0xf0, 0xf0);
  615. break;
  616. default:
  617. s5m_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP,
  618. 0x90, 0xf0);
  619. }
  620. }
  621. for (i = 0; i < pdata->num_regulators; i++) {
  622. const struct s5m_voltage_desc *desc;
  623. int id = pdata->regulators[i].id;
  624. desc = reg_voltage_map[id];
  625. if (desc)
  626. regulators[id].n_voltages =
  627. (desc->max - desc->min) / desc->step + 1;
  628. rdev[i] = regulator_register(&regulators[id], s5m8767->dev,
  629. pdata->regulators[i].initdata, s5m8767, NULL);
  630. if (IS_ERR(rdev[i])) {
  631. ret = PTR_ERR(rdev[i]);
  632. dev_err(s5m8767->dev, "regulator init failed for %d\n",
  633. id);
  634. rdev[i] = NULL;
  635. goto err;
  636. }
  637. }
  638. return 0;
  639. err:
  640. for (i = 0; i < s5m8767->num_regulators; i++)
  641. if (rdev[i])
  642. regulator_unregister(rdev[i]);
  643. return ret;
  644. }
  645. static int __devexit s5m8767_pmic_remove(struct platform_device *pdev)
  646. {
  647. struct s5m8767_info *s5m8767 = platform_get_drvdata(pdev);
  648. struct regulator_dev **rdev = s5m8767->rdev;
  649. int i;
  650. for (i = 0; i < s5m8767->num_regulators; i++)
  651. if (rdev[i])
  652. regulator_unregister(rdev[i]);
  653. return 0;
  654. }
  655. static const struct platform_device_id s5m8767_pmic_id[] = {
  656. { "s5m8767-pmic", 0},
  657. { },
  658. };
  659. MODULE_DEVICE_TABLE(platform, s5m8767_pmic_id);
  660. static struct platform_driver s5m8767_pmic_driver = {
  661. .driver = {
  662. .name = "s5m8767-pmic",
  663. .owner = THIS_MODULE,
  664. },
  665. .probe = s5m8767_pmic_probe,
  666. .remove = __devexit_p(s5m8767_pmic_remove),
  667. .id_table = s5m8767_pmic_id,
  668. };
  669. static int __init s5m8767_pmic_init(void)
  670. {
  671. return platform_driver_register(&s5m8767_pmic_driver);
  672. }
  673. subsys_initcall(s5m8767_pmic_init);
  674. static void __exit s5m8767_pmic_exit(void)
  675. {
  676. platform_driver_unregister(&s5m8767_pmic_driver);
  677. }
  678. module_exit(s5m8767_pmic_exit);
  679. /* Module information */
  680. MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>");
  681. MODULE_DESCRIPTION("SAMSUNG S5M8767 Regulator Driver");
  682. MODULE_LICENSE("GPL");