s5m8767.c 20 KB

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