max8998.c 24 KB

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