max8998.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915
  1. /*
  2. * max8998.c - Voltage regulator driver for the Maxim 8998
  3. *
  4. * Copyright (C) 2009-2010 Samsung Electronics
  5. * Kyungmin Park <kyungmin.park@samsung.com>
  6. * Marek Szyprowski <m.szyprowski@samsung.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include <linux/module.h>
  23. #include <linux/init.h>
  24. #include <linux/i2c.h>
  25. #include <linux/err.h>
  26. #include <linux/gpio.h>
  27. #include <linux/slab.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/mutex.h>
  30. #include <linux/delay.h>
  31. #include <linux/platform_device.h>
  32. #include <linux/regulator/driver.h>
  33. #include <linux/mfd/max8998.h>
  34. #include <linux/mfd/max8998-private.h>
  35. struct max8998_data {
  36. struct device *dev;
  37. struct max8998_dev *iodev;
  38. int num_regulators;
  39. struct regulator_dev **rdev;
  40. u8 buck1_vol[4]; /* voltages for selection */
  41. u8 buck2_vol[2];
  42. unsigned int buck1_idx; /* index to last changed voltage */
  43. /* value in a set */
  44. unsigned int buck2_idx;
  45. };
  46. struct voltage_map_desc {
  47. int min;
  48. int max;
  49. int step;
  50. };
  51. /* Voltage maps */
  52. static const struct voltage_map_desc ldo23_voltage_map_desc = {
  53. .min = 800, .step = 50, .max = 1300,
  54. };
  55. static const struct voltage_map_desc ldo456711_voltage_map_desc = {
  56. .min = 1600, .step = 100, .max = 3600,
  57. };
  58. static const struct voltage_map_desc ldo8_voltage_map_desc = {
  59. .min = 3000, .step = 100, .max = 3600,
  60. };
  61. static const struct voltage_map_desc ldo9_voltage_map_desc = {
  62. .min = 2800, .step = 100, .max = 3100,
  63. };
  64. static const struct voltage_map_desc ldo10_voltage_map_desc = {
  65. .min = 950, .step = 50, .max = 1300,
  66. };
  67. static const struct voltage_map_desc ldo1213_voltage_map_desc = {
  68. .min = 800, .step = 100, .max = 3300,
  69. };
  70. static const struct voltage_map_desc ldo1415_voltage_map_desc = {
  71. .min = 1200, .step = 100, .max = 3300,
  72. };
  73. static const struct voltage_map_desc ldo1617_voltage_map_desc = {
  74. .min = 1600, .step = 100, .max = 3600,
  75. };
  76. static const struct voltage_map_desc buck12_voltage_map_desc = {
  77. .min = 750, .step = 25, .max = 1525,
  78. };
  79. static const struct voltage_map_desc buck3_voltage_map_desc = {
  80. .min = 1600, .step = 100, .max = 3600,
  81. };
  82. static const struct voltage_map_desc buck4_voltage_map_desc = {
  83. .min = 800, .step = 100, .max = 2300,
  84. };
  85. static const struct voltage_map_desc *ldo_voltage_map[] = {
  86. NULL,
  87. NULL,
  88. &ldo23_voltage_map_desc, /* LDO2 */
  89. &ldo23_voltage_map_desc, /* LDO3 */
  90. &ldo456711_voltage_map_desc, /* LDO4 */
  91. &ldo456711_voltage_map_desc, /* LDO5 */
  92. &ldo456711_voltage_map_desc, /* LDO6 */
  93. &ldo456711_voltage_map_desc, /* LDO7 */
  94. &ldo8_voltage_map_desc, /* LDO8 */
  95. &ldo9_voltage_map_desc, /* LDO9 */
  96. &ldo10_voltage_map_desc, /* LDO10 */
  97. &ldo456711_voltage_map_desc, /* LDO11 */
  98. &ldo1213_voltage_map_desc, /* LDO12 */
  99. &ldo1213_voltage_map_desc, /* LDO13 */
  100. &ldo1415_voltage_map_desc, /* LDO14 */
  101. &ldo1415_voltage_map_desc, /* LDO15 */
  102. &ldo1617_voltage_map_desc, /* LDO16 */
  103. &ldo1617_voltage_map_desc, /* LDO17 */
  104. &buck12_voltage_map_desc, /* BUCK1 */
  105. &buck12_voltage_map_desc, /* BUCK2 */
  106. &buck3_voltage_map_desc, /* BUCK3 */
  107. &buck4_voltage_map_desc, /* BUCK4 */
  108. };
  109. static int max8998_list_voltage(struct regulator_dev *rdev,
  110. unsigned int selector)
  111. {
  112. const struct voltage_map_desc *desc;
  113. int ldo = rdev_get_id(rdev);
  114. int val;
  115. if (ldo >= ARRAY_SIZE(ldo_voltage_map))
  116. return -EINVAL;
  117. desc = ldo_voltage_map[ldo];
  118. if (desc == NULL)
  119. return -EINVAL;
  120. val = desc->min + desc->step * selector;
  121. if (val > desc->max)
  122. return -EINVAL;
  123. return val * 1000;
  124. }
  125. static int max8998_get_enable_register(struct regulator_dev *rdev,
  126. int *reg, int *shift)
  127. {
  128. int ldo = rdev_get_id(rdev);
  129. switch (ldo) {
  130. case MAX8998_LDO2 ... MAX8998_LDO5:
  131. *reg = MAX8998_REG_ONOFF1;
  132. *shift = 3 - (ldo - MAX8998_LDO2);
  133. break;
  134. case MAX8998_LDO6 ... MAX8998_LDO13:
  135. *reg = MAX8998_REG_ONOFF2;
  136. *shift = 7 - (ldo - MAX8998_LDO6);
  137. break;
  138. case MAX8998_LDO14 ... MAX8998_LDO17:
  139. *reg = MAX8998_REG_ONOFF3;
  140. *shift = 7 - (ldo - MAX8998_LDO14);
  141. break;
  142. case MAX8998_BUCK1 ... MAX8998_BUCK4:
  143. *reg = MAX8998_REG_ONOFF1;
  144. *shift = 7 - (ldo - MAX8998_BUCK1);
  145. break;
  146. case MAX8998_EN32KHZ_AP ... MAX8998_ENVICHG:
  147. *reg = MAX8998_REG_ONOFF4;
  148. *shift = 7 - (ldo - MAX8998_EN32KHZ_AP);
  149. break;
  150. case MAX8998_ESAFEOUT1 ... MAX8998_ESAFEOUT2:
  151. *reg = MAX8998_REG_CHGR2;
  152. *shift = 7 - (ldo - MAX8998_ESAFEOUT1);
  153. break;
  154. default:
  155. return -EINVAL;
  156. }
  157. return 0;
  158. }
  159. static int max8998_ldo_is_enabled(struct regulator_dev *rdev)
  160. {
  161. struct max8998_data *max8998 = rdev_get_drvdata(rdev);
  162. struct i2c_client *i2c = max8998->iodev->i2c;
  163. int ret, reg, shift = 8;
  164. u8 val;
  165. ret = max8998_get_enable_register(rdev, &reg, &shift);
  166. if (ret)
  167. return ret;
  168. ret = max8998_read_reg(i2c, reg, &val);
  169. if (ret)
  170. return ret;
  171. return val & (1 << shift);
  172. }
  173. static int max8998_ldo_enable(struct regulator_dev *rdev)
  174. {
  175. struct max8998_data *max8998 = rdev_get_drvdata(rdev);
  176. struct i2c_client *i2c = max8998->iodev->i2c;
  177. int reg, shift = 8, ret;
  178. ret = max8998_get_enable_register(rdev, &reg, &shift);
  179. if (ret)
  180. return ret;
  181. return max8998_update_reg(i2c, reg, 1<<shift, 1<<shift);
  182. }
  183. static int max8998_ldo_disable(struct regulator_dev *rdev)
  184. {
  185. struct max8998_data *max8998 = rdev_get_drvdata(rdev);
  186. struct i2c_client *i2c = max8998->iodev->i2c;
  187. int reg, shift = 8, ret;
  188. ret = max8998_get_enable_register(rdev, &reg, &shift);
  189. if (ret)
  190. return ret;
  191. return max8998_update_reg(i2c, reg, 0, 1<<shift);
  192. }
  193. static int max8998_get_voltage_register(struct regulator_dev *rdev,
  194. int *_reg, int *_shift, int *_mask)
  195. {
  196. int ldo = rdev_get_id(rdev);
  197. struct max8998_data *max8998 = rdev_get_drvdata(rdev);
  198. int reg, shift = 0, mask = 0xff;
  199. switch (ldo) {
  200. case MAX8998_LDO2 ... MAX8998_LDO3:
  201. reg = MAX8998_REG_LDO2_LDO3;
  202. mask = 0xf;
  203. if (ldo == MAX8998_LDO2)
  204. shift = 4;
  205. else
  206. shift = 0;
  207. break;
  208. case MAX8998_LDO4 ... MAX8998_LDO7:
  209. reg = MAX8998_REG_LDO4 + (ldo - MAX8998_LDO4);
  210. break;
  211. case MAX8998_LDO8 ... MAX8998_LDO9:
  212. reg = MAX8998_REG_LDO8_LDO9;
  213. mask = 0xf;
  214. if (ldo == MAX8998_LDO8)
  215. shift = 4;
  216. else
  217. shift = 0;
  218. break;
  219. case MAX8998_LDO10 ... MAX8998_LDO11:
  220. reg = MAX8998_REG_LDO10_LDO11;
  221. if (ldo == MAX8998_LDO10) {
  222. shift = 5;
  223. mask = 0x7;
  224. } else {
  225. shift = 0;
  226. mask = 0x1f;
  227. }
  228. break;
  229. case MAX8998_LDO12 ... MAX8998_LDO17:
  230. reg = MAX8998_REG_LDO12 + (ldo - MAX8998_LDO12);
  231. break;
  232. case MAX8998_BUCK1:
  233. reg = MAX8998_REG_BUCK1_VOLTAGE1 + max8998->buck1_idx;
  234. break;
  235. case MAX8998_BUCK2:
  236. reg = MAX8998_REG_BUCK2_VOLTAGE1 + max8998->buck2_idx;
  237. break;
  238. case MAX8998_BUCK3:
  239. reg = MAX8998_REG_BUCK3;
  240. break;
  241. case MAX8998_BUCK4:
  242. reg = MAX8998_REG_BUCK4;
  243. break;
  244. default:
  245. return -EINVAL;
  246. }
  247. *_reg = reg;
  248. *_shift = shift;
  249. *_mask = mask;
  250. return 0;
  251. }
  252. static int max8998_get_voltage(struct regulator_dev *rdev)
  253. {
  254. struct max8998_data *max8998 = rdev_get_drvdata(rdev);
  255. struct i2c_client *i2c = max8998->iodev->i2c;
  256. int reg, shift = 0, mask, ret;
  257. u8 val;
  258. ret = max8998_get_voltage_register(rdev, &reg, &shift, &mask);
  259. if (ret)
  260. return ret;
  261. ret = max8998_read_reg(i2c, reg, &val);
  262. if (ret)
  263. return ret;
  264. val >>= shift;
  265. val &= mask;
  266. return max8998_list_voltage(rdev, val);
  267. }
  268. static int max8998_set_voltage_ldo(struct regulator_dev *rdev,
  269. int min_uV, int max_uV, unsigned *selector)
  270. {
  271. struct max8998_data *max8998 = rdev_get_drvdata(rdev);
  272. struct i2c_client *i2c = max8998->iodev->i2c;
  273. int min_vol = min_uV / 1000, max_vol = max_uV / 1000;
  274. const struct voltage_map_desc *desc;
  275. int ldo = rdev_get_id(rdev);
  276. int reg, shift = 0, mask, ret;
  277. int i = 0;
  278. if (ldo >= ARRAY_SIZE(ldo_voltage_map))
  279. return -EINVAL;
  280. desc = ldo_voltage_map[ldo];
  281. if (desc == NULL)
  282. return -EINVAL;
  283. if (max_vol < desc->min || min_vol > desc->max)
  284. return -EINVAL;
  285. while (desc->min + desc->step*i < min_vol &&
  286. desc->min + desc->step*i < desc->max)
  287. i++;
  288. if (desc->min + desc->step*i > max_vol)
  289. return -EINVAL;
  290. *selector = i;
  291. ret = max8998_get_voltage_register(rdev, &reg, &shift, &mask);
  292. if (ret)
  293. return ret;
  294. ret = max8998_update_reg(i2c, reg, i<<shift, mask<<shift);
  295. return ret;
  296. }
  297. static inline void buck1_gpio_set(int gpio1, int gpio2, int v)
  298. {
  299. gpio_set_value(gpio1, v & 0x1);
  300. gpio_set_value(gpio2, (v >> 1) & 0x1);
  301. }
  302. static inline void buck2_gpio_set(int gpio, int v)
  303. {
  304. gpio_set_value(gpio, v & 0x1);
  305. }
  306. static int max8998_set_voltage_buck(struct regulator_dev *rdev,
  307. int min_uV, int max_uV, unsigned *selector)
  308. {
  309. struct max8998_data *max8998 = rdev_get_drvdata(rdev);
  310. struct max8998_platform_data *pdata =
  311. dev_get_platdata(max8998->iodev->dev);
  312. struct i2c_client *i2c = max8998->iodev->i2c;
  313. int min_vol = min_uV / 1000, max_vol = max_uV / 1000;
  314. const struct voltage_map_desc *desc;
  315. int buck = rdev_get_id(rdev);
  316. int reg, shift = 0, mask, ret;
  317. int difference = 0, i = 0, j = 0, previous_vol = 0;
  318. u8 val = 0;
  319. static u8 buck1_last_val;
  320. if (buck >= ARRAY_SIZE(ldo_voltage_map))
  321. return -EINVAL;
  322. desc = ldo_voltage_map[buck];
  323. if (desc == NULL)
  324. return -EINVAL;
  325. if (max_vol < desc->min || min_vol > desc->max)
  326. return -EINVAL;
  327. while (desc->min + desc->step*i < min_vol &&
  328. desc->min + desc->step*i < desc->max)
  329. i++;
  330. if (desc->min + desc->step*i > max_vol)
  331. return -EINVAL;
  332. *selector = i;
  333. ret = max8998_get_voltage_register(rdev, &reg, &shift, &mask);
  334. if (ret)
  335. return ret;
  336. previous_vol = max8998_get_voltage(rdev);
  337. /* Check if voltage needs to be changed */
  338. /* if previous_voltage equal new voltage, return */
  339. if (previous_vol == max8998_list_voltage(rdev, i)) {
  340. dev_dbg(max8998->dev, "No voltage change, old:%d, new:%d\n",
  341. previous_vol, max8998_list_voltage(rdev, i));
  342. return ret;
  343. }
  344. switch (buck) {
  345. case MAX8998_BUCK1:
  346. dev_dbg(max8998->dev,
  347. "BUCK1, i:%d, buck1_vol1:%d, buck1_vol2:%d\n"
  348. "buck1_vol3:%d, buck1_vol4:%d\n",
  349. i, max8998->buck1_vol[0], max8998->buck1_vol[1],
  350. max8998->buck1_vol[2], max8998->buck1_vol[3]);
  351. if (gpio_is_valid(pdata->buck1_set1) &&
  352. gpio_is_valid(pdata->buck1_set2)) {
  353. /* check if requested voltage */
  354. /* value is already defined */
  355. for (j = 0; j < ARRAY_SIZE(max8998->buck1_vol); j++) {
  356. if (max8998->buck1_vol[j] == i) {
  357. max8998->buck1_idx = j;
  358. buck1_gpio_set(pdata->buck1_set1,
  359. pdata->buck1_set2, j);
  360. goto buck1_exit;
  361. }
  362. }
  363. if (pdata->buck_voltage_lock)
  364. return -EINVAL;
  365. /* no predefine regulator found */
  366. max8998->buck1_idx = (buck1_last_val % 2) + 2;
  367. dev_dbg(max8998->dev, "max8998->buck1_idx:%d\n",
  368. max8998->buck1_idx);
  369. max8998->buck1_vol[max8998->buck1_idx] = i;
  370. ret = max8998_get_voltage_register(rdev, &reg,
  371. &shift,
  372. &mask);
  373. ret = max8998_write_reg(i2c, reg, i);
  374. buck1_gpio_set(pdata->buck1_set1,
  375. pdata->buck1_set2, max8998->buck1_idx);
  376. buck1_last_val++;
  377. buck1_exit:
  378. dev_dbg(max8998->dev, "%s: SET1:%d, SET2:%d\n",
  379. i2c->name, gpio_get_value(pdata->buck1_set1),
  380. gpio_get_value(pdata->buck1_set2));
  381. break;
  382. } else {
  383. ret = max8998_write_reg(i2c, reg, i);
  384. }
  385. break;
  386. case MAX8998_BUCK2:
  387. dev_dbg(max8998->dev,
  388. "BUCK2, i:%d buck2_vol1:%d, buck2_vol2:%d\n"
  389. , i, max8998->buck2_vol[0], max8998->buck2_vol[1]);
  390. if (gpio_is_valid(pdata->buck2_set3)) {
  391. /* check if requested voltage */
  392. /* value is already defined */
  393. for (j = 0; j < ARRAY_SIZE(max8998->buck2_vol); j++) {
  394. if (max8998->buck2_vol[j] == i) {
  395. max8998->buck2_idx = j;
  396. buck2_gpio_set(pdata->buck2_set3, j);
  397. goto buck2_exit;
  398. }
  399. }
  400. if (pdata->buck_voltage_lock)
  401. return -EINVAL;
  402. max8998_get_voltage_register(rdev,
  403. &reg, &shift, &mask);
  404. ret = max8998_write_reg(i2c, reg, i);
  405. max8998->buck2_vol[max8998->buck2_idx] = i;
  406. buck2_gpio_set(pdata->buck2_set3, max8998->buck2_idx);
  407. buck2_exit:
  408. dev_dbg(max8998->dev, "%s: SET3:%d\n", i2c->name,
  409. gpio_get_value(pdata->buck2_set3));
  410. } else {
  411. ret = max8998_write_reg(i2c, reg, i);
  412. }
  413. break;
  414. case MAX8998_BUCK3:
  415. case MAX8998_BUCK4:
  416. ret = max8998_update_reg(i2c, reg, i<<shift, mask<<shift);
  417. break;
  418. }
  419. /* Voltage stabilization */
  420. max8998_read_reg(i2c, MAX8998_REG_ONOFF4, &val);
  421. /* lp3974 hasn't got ENRAMP bit - ramp is assumed as true */
  422. /* MAX8998 has ENRAMP bit implemented, so test it*/
  423. if (max8998->iodev->type == TYPE_MAX8998 && !(val & MAX8998_ENRAMP))
  424. return ret;
  425. difference = desc->min + desc->step*i - previous_vol/1000;
  426. if (difference > 0)
  427. udelay(DIV_ROUND_UP(difference, (val & 0x0f) + 1));
  428. return ret;
  429. }
  430. static struct regulator_ops max8998_ldo_ops = {
  431. .list_voltage = max8998_list_voltage,
  432. .is_enabled = max8998_ldo_is_enabled,
  433. .enable = max8998_ldo_enable,
  434. .disable = max8998_ldo_disable,
  435. .get_voltage = max8998_get_voltage,
  436. .set_voltage = max8998_set_voltage_ldo,
  437. .set_suspend_enable = max8998_ldo_enable,
  438. .set_suspend_disable = max8998_ldo_disable,
  439. };
  440. static struct regulator_ops max8998_buck_ops = {
  441. .list_voltage = max8998_list_voltage,
  442. .is_enabled = max8998_ldo_is_enabled,
  443. .enable = max8998_ldo_enable,
  444. .disable = max8998_ldo_disable,
  445. .get_voltage = max8998_get_voltage,
  446. .set_voltage = max8998_set_voltage_buck,
  447. .set_suspend_enable = max8998_ldo_enable,
  448. .set_suspend_disable = max8998_ldo_disable,
  449. };
  450. static struct regulator_ops max8998_others_ops = {
  451. .is_enabled = max8998_ldo_is_enabled,
  452. .enable = max8998_ldo_enable,
  453. .disable = max8998_ldo_disable,
  454. .set_suspend_enable = max8998_ldo_enable,
  455. .set_suspend_disable = max8998_ldo_disable,
  456. };
  457. static struct regulator_desc regulators[] = {
  458. {
  459. .name = "LDO2",
  460. .id = MAX8998_LDO2,
  461. .ops = &max8998_ldo_ops,
  462. .type = REGULATOR_VOLTAGE,
  463. .owner = THIS_MODULE,
  464. }, {
  465. .name = "LDO3",
  466. .id = MAX8998_LDO3,
  467. .ops = &max8998_ldo_ops,
  468. .type = REGULATOR_VOLTAGE,
  469. .owner = THIS_MODULE,
  470. }, {
  471. .name = "LDO4",
  472. .id = MAX8998_LDO4,
  473. .ops = &max8998_ldo_ops,
  474. .type = REGULATOR_VOLTAGE,
  475. .owner = THIS_MODULE,
  476. }, {
  477. .name = "LDO5",
  478. .id = MAX8998_LDO5,
  479. .ops = &max8998_ldo_ops,
  480. .type = REGULATOR_VOLTAGE,
  481. .owner = THIS_MODULE,
  482. }, {
  483. .name = "LDO6",
  484. .id = MAX8998_LDO6,
  485. .ops = &max8998_ldo_ops,
  486. .type = REGULATOR_VOLTAGE,
  487. .owner = THIS_MODULE,
  488. }, {
  489. .name = "LDO7",
  490. .id = MAX8998_LDO7,
  491. .ops = &max8998_ldo_ops,
  492. .type = REGULATOR_VOLTAGE,
  493. .owner = THIS_MODULE,
  494. }, {
  495. .name = "LDO8",
  496. .id = MAX8998_LDO8,
  497. .ops = &max8998_ldo_ops,
  498. .type = REGULATOR_VOLTAGE,
  499. .owner = THIS_MODULE,
  500. }, {
  501. .name = "LDO9",
  502. .id = MAX8998_LDO9,
  503. .ops = &max8998_ldo_ops,
  504. .type = REGULATOR_VOLTAGE,
  505. .owner = THIS_MODULE,
  506. }, {
  507. .name = "LDO10",
  508. .id = MAX8998_LDO10,
  509. .ops = &max8998_ldo_ops,
  510. .type = REGULATOR_VOLTAGE,
  511. .owner = THIS_MODULE,
  512. }, {
  513. .name = "LDO11",
  514. .id = MAX8998_LDO11,
  515. .ops = &max8998_ldo_ops,
  516. .type = REGULATOR_VOLTAGE,
  517. .owner = THIS_MODULE,
  518. }, {
  519. .name = "LDO12",
  520. .id = MAX8998_LDO12,
  521. .ops = &max8998_ldo_ops,
  522. .type = REGULATOR_VOLTAGE,
  523. .owner = THIS_MODULE,
  524. }, {
  525. .name = "LDO13",
  526. .id = MAX8998_LDO13,
  527. .ops = &max8998_ldo_ops,
  528. .type = REGULATOR_VOLTAGE,
  529. .owner = THIS_MODULE,
  530. }, {
  531. .name = "LDO14",
  532. .id = MAX8998_LDO14,
  533. .ops = &max8998_ldo_ops,
  534. .type = REGULATOR_VOLTAGE,
  535. .owner = THIS_MODULE,
  536. }, {
  537. .name = "LDO15",
  538. .id = MAX8998_LDO15,
  539. .ops = &max8998_ldo_ops,
  540. .type = REGULATOR_VOLTAGE,
  541. .owner = THIS_MODULE,
  542. }, {
  543. .name = "LDO16",
  544. .id = MAX8998_LDO16,
  545. .ops = &max8998_ldo_ops,
  546. .type = REGULATOR_VOLTAGE,
  547. .owner = THIS_MODULE,
  548. }, {
  549. .name = "LDO17",
  550. .id = MAX8998_LDO17,
  551. .ops = &max8998_ldo_ops,
  552. .type = REGULATOR_VOLTAGE,
  553. .owner = THIS_MODULE,
  554. }, {
  555. .name = "BUCK1",
  556. .id = MAX8998_BUCK1,
  557. .ops = &max8998_buck_ops,
  558. .type = REGULATOR_VOLTAGE,
  559. .owner = THIS_MODULE,
  560. }, {
  561. .name = "BUCK2",
  562. .id = MAX8998_BUCK2,
  563. .ops = &max8998_buck_ops,
  564. .type = REGULATOR_VOLTAGE,
  565. .owner = THIS_MODULE,
  566. }, {
  567. .name = "BUCK3",
  568. .id = MAX8998_BUCK3,
  569. .ops = &max8998_buck_ops,
  570. .type = REGULATOR_VOLTAGE,
  571. .owner = THIS_MODULE,
  572. }, {
  573. .name = "BUCK4",
  574. .id = MAX8998_BUCK4,
  575. .ops = &max8998_buck_ops,
  576. .type = REGULATOR_VOLTAGE,
  577. .owner = THIS_MODULE,
  578. }, {
  579. .name = "EN32KHz AP",
  580. .id = MAX8998_EN32KHZ_AP,
  581. .ops = &max8998_others_ops,
  582. .type = REGULATOR_VOLTAGE,
  583. .owner = THIS_MODULE,
  584. }, {
  585. .name = "EN32KHz CP",
  586. .id = MAX8998_EN32KHZ_CP,
  587. .ops = &max8998_others_ops,
  588. .type = REGULATOR_VOLTAGE,
  589. .owner = THIS_MODULE,
  590. }, {
  591. .name = "ENVICHG",
  592. .id = MAX8998_ENVICHG,
  593. .ops = &max8998_others_ops,
  594. .type = REGULATOR_VOLTAGE,
  595. .owner = THIS_MODULE,
  596. }, {
  597. .name = "ESAFEOUT1",
  598. .id = MAX8998_ESAFEOUT1,
  599. .ops = &max8998_others_ops,
  600. .type = REGULATOR_VOLTAGE,
  601. .owner = THIS_MODULE,
  602. }, {
  603. .name = "ESAFEOUT2",
  604. .id = MAX8998_ESAFEOUT2,
  605. .ops = &max8998_others_ops,
  606. .type = REGULATOR_VOLTAGE,
  607. .owner = THIS_MODULE,
  608. }
  609. };
  610. static __devinit int max8998_pmic_probe(struct platform_device *pdev)
  611. {
  612. struct max8998_dev *iodev = dev_get_drvdata(pdev->dev.parent);
  613. struct max8998_platform_data *pdata = dev_get_platdata(iodev->dev);
  614. struct regulator_dev **rdev;
  615. struct max8998_data *max8998;
  616. struct i2c_client *i2c;
  617. int i, ret, size;
  618. if (!pdata) {
  619. dev_err(pdev->dev.parent, "No platform init data supplied\n");
  620. return -ENODEV;
  621. }
  622. max8998 = kzalloc(sizeof(struct max8998_data), GFP_KERNEL);
  623. if (!max8998)
  624. return -ENOMEM;
  625. size = sizeof(struct regulator_dev *) * pdata->num_regulators;
  626. max8998->rdev = kzalloc(size, GFP_KERNEL);
  627. if (!max8998->rdev) {
  628. kfree(max8998);
  629. return -ENOMEM;
  630. }
  631. rdev = max8998->rdev;
  632. max8998->dev = &pdev->dev;
  633. max8998->iodev = iodev;
  634. max8998->num_regulators = pdata->num_regulators;
  635. platform_set_drvdata(pdev, max8998);
  636. i2c = max8998->iodev->i2c;
  637. max8998->buck1_idx = pdata->buck1_default_idx;
  638. max8998->buck2_idx = pdata->buck2_default_idx;
  639. /* NOTE: */
  640. /* For unused GPIO NOT marked as -1 (thereof equal to 0) WARN_ON */
  641. /* will be displayed */
  642. /* Check if MAX8998 voltage selection GPIOs are defined */
  643. if (gpio_is_valid(pdata->buck1_set1) &&
  644. gpio_is_valid(pdata->buck1_set2)) {
  645. /* Check if SET1 is not equal to 0 */
  646. if (!pdata->buck1_set1) {
  647. printk(KERN_ERR "MAX8998 SET1 GPIO defined as 0 !\n");
  648. WARN_ON(!pdata->buck1_set1);
  649. ret = -EIO;
  650. goto err_free_mem;
  651. }
  652. /* Check if SET2 is not equal to 0 */
  653. if (!pdata->buck1_set2) {
  654. printk(KERN_ERR "MAX8998 SET2 GPIO defined as 0 !\n");
  655. WARN_ON(!pdata->buck1_set2);
  656. ret = -EIO;
  657. goto err_free_mem;
  658. }
  659. gpio_request(pdata->buck1_set1, "MAX8998 BUCK1_SET1");
  660. gpio_direction_output(pdata->buck1_set1,
  661. max8998->buck1_idx & 0x1);
  662. gpio_request(pdata->buck1_set2, "MAX8998 BUCK1_SET2");
  663. gpio_direction_output(pdata->buck1_set2,
  664. (max8998->buck1_idx >> 1) & 0x1);
  665. /* Set predefined value for BUCK1 register 1 */
  666. i = 0;
  667. while (buck12_voltage_map_desc.min +
  668. buck12_voltage_map_desc.step*i
  669. < (pdata->buck1_voltage1 / 1000))
  670. i++;
  671. max8998->buck1_vol[0] = i;
  672. ret = max8998_write_reg(i2c, MAX8998_REG_BUCK1_VOLTAGE1, i);
  673. if (ret)
  674. goto err_free_mem;
  675. /* Set predefined value for BUCK1 register 2 */
  676. i = 0;
  677. while (buck12_voltage_map_desc.min +
  678. buck12_voltage_map_desc.step*i
  679. < (pdata->buck1_voltage2 / 1000))
  680. i++;
  681. max8998->buck1_vol[1] = i;
  682. ret = max8998_write_reg(i2c, MAX8998_REG_BUCK1_VOLTAGE2, i);
  683. if (ret)
  684. goto err_free_mem;
  685. /* Set predefined value for BUCK1 register 3 */
  686. i = 0;
  687. while (buck12_voltage_map_desc.min +
  688. buck12_voltage_map_desc.step*i
  689. < (pdata->buck1_voltage3 / 1000))
  690. i++;
  691. max8998->buck1_vol[2] = i;
  692. ret = max8998_write_reg(i2c, MAX8998_REG_BUCK1_VOLTAGE3, i);
  693. if (ret)
  694. goto err_free_mem;
  695. /* Set predefined value for BUCK1 register 4 */
  696. i = 0;
  697. while (buck12_voltage_map_desc.min +
  698. buck12_voltage_map_desc.step*i
  699. < (pdata->buck1_voltage4 / 1000))
  700. i++;
  701. max8998->buck1_vol[3] = i;
  702. ret = max8998_write_reg(i2c, MAX8998_REG_BUCK1_VOLTAGE4, i);
  703. if (ret)
  704. goto err_free_mem;
  705. }
  706. if (gpio_is_valid(pdata->buck2_set3)) {
  707. /* Check if SET3 is not equal to 0 */
  708. if (!pdata->buck2_set3) {
  709. printk(KERN_ERR "MAX8998 SET3 GPIO defined as 0 !\n");
  710. WARN_ON(!pdata->buck2_set3);
  711. ret = -EIO;
  712. goto err_free_mem;
  713. }
  714. gpio_request(pdata->buck2_set3, "MAX8998 BUCK2_SET3");
  715. gpio_direction_output(pdata->buck2_set3,
  716. max8998->buck2_idx & 0x1);
  717. /* BUCK2 register 1 */
  718. i = 0;
  719. while (buck12_voltage_map_desc.min +
  720. buck12_voltage_map_desc.step*i
  721. < (pdata->buck2_voltage1 / 1000))
  722. i++;
  723. max8998->buck2_vol[0] = i;
  724. ret = max8998_write_reg(i2c, MAX8998_REG_BUCK2_VOLTAGE1, i);
  725. if (ret)
  726. goto err_free_mem;
  727. /* BUCK2 register 2 */
  728. i = 0;
  729. while (buck12_voltage_map_desc.min +
  730. buck12_voltage_map_desc.step*i
  731. < (pdata->buck2_voltage2 / 1000))
  732. i++;
  733. max8998->buck2_vol[1] = i;
  734. ret = max8998_write_reg(i2c, MAX8998_REG_BUCK2_VOLTAGE2, i);
  735. if (ret)
  736. goto err_free_mem;
  737. }
  738. for (i = 0; i < pdata->num_regulators; i++) {
  739. const struct voltage_map_desc *desc;
  740. int id = pdata->regulators[i].id;
  741. int index = id - MAX8998_LDO2;
  742. desc = ldo_voltage_map[id];
  743. if (desc && regulators[index].ops != &max8998_others_ops) {
  744. int count = (desc->max - desc->min) / desc->step + 1;
  745. regulators[index].n_voltages = count;
  746. }
  747. rdev[i] = regulator_register(&regulators[index], max8998->dev,
  748. pdata->regulators[i].initdata, max8998, NULL);
  749. if (IS_ERR(rdev[i])) {
  750. ret = PTR_ERR(rdev[i]);
  751. dev_err(max8998->dev, "regulator init failed\n");
  752. rdev[i] = NULL;
  753. goto err;
  754. }
  755. }
  756. return 0;
  757. err:
  758. for (i = 0; i < max8998->num_regulators; i++)
  759. if (rdev[i])
  760. regulator_unregister(rdev[i]);
  761. err_free_mem:
  762. kfree(max8998->rdev);
  763. kfree(max8998);
  764. return ret;
  765. }
  766. static int __devexit max8998_pmic_remove(struct platform_device *pdev)
  767. {
  768. struct max8998_data *max8998 = platform_get_drvdata(pdev);
  769. struct regulator_dev **rdev = max8998->rdev;
  770. int i;
  771. for (i = 0; i < max8998->num_regulators; i++)
  772. if (rdev[i])
  773. regulator_unregister(rdev[i]);
  774. kfree(max8998->rdev);
  775. kfree(max8998);
  776. return 0;
  777. }
  778. static const struct platform_device_id max8998_pmic_id[] = {
  779. { "max8998-pmic", TYPE_MAX8998 },
  780. { "lp3974-pmic", TYPE_LP3974 },
  781. { }
  782. };
  783. MODULE_DEVICE_TABLE(platform, max8998_pmic_id);
  784. static struct platform_driver max8998_pmic_driver = {
  785. .driver = {
  786. .name = "max8998-pmic",
  787. .owner = THIS_MODULE,
  788. },
  789. .probe = max8998_pmic_probe,
  790. .remove = __devexit_p(max8998_pmic_remove),
  791. .id_table = max8998_pmic_id,
  792. };
  793. static int __init max8998_pmic_init(void)
  794. {
  795. return platform_driver_register(&max8998_pmic_driver);
  796. }
  797. subsys_initcall(max8998_pmic_init);
  798. static void __exit max8998_pmic_cleanup(void)
  799. {
  800. platform_driver_unregister(&max8998_pmic_driver);
  801. }
  802. module_exit(max8998_pmic_cleanup);
  803. MODULE_DESCRIPTION("MAXIM 8998 voltage regulator driver");
  804. MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");
  805. MODULE_LICENSE("GPL");