max8998.c 23 KB

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