s5m8767.c 20 KB

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