max8997-regulator.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242
  1. /*
  2. * max8997.c - Regulator driver for the Maxim 8997/8966
  3. *
  4. * Copyright (C) 2011 Samsung Electronics
  5. * MyungJoo Ham <myungjoo.ham@samsung.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. * This driver is based on max8998.c
  22. */
  23. #include <linux/bug.h>
  24. #include <linux/err.h>
  25. #include <linux/gpio.h>
  26. #include <linux/of_gpio.h>
  27. #include <linux/slab.h>
  28. #include <linux/module.h>
  29. #include <linux/platform_device.h>
  30. #include <linux/regulator/driver.h>
  31. #include <linux/regulator/machine.h>
  32. #include <linux/mfd/max8997.h>
  33. #include <linux/mfd/max8997-private.h>
  34. #include <linux/regulator/of_regulator.h>
  35. struct max8997_data {
  36. struct device *dev;
  37. struct max8997_dev *iodev;
  38. int num_regulators;
  39. int ramp_delay; /* in mV/us */
  40. bool buck1_gpiodvs;
  41. bool buck2_gpiodvs;
  42. bool buck5_gpiodvs;
  43. u8 buck1_vol[8];
  44. u8 buck2_vol[8];
  45. u8 buck5_vol[8];
  46. int buck125_gpios[3];
  47. int buck125_gpioindex;
  48. bool ignore_gpiodvs_side_effect;
  49. u8 saved_states[MAX8997_REG_MAX];
  50. };
  51. static const unsigned int safeoutvolt[] = {
  52. 4850000,
  53. 4900000,
  54. 4950000,
  55. 3300000,
  56. };
  57. static inline void max8997_set_gpio(struct max8997_data *max8997)
  58. {
  59. int set3 = (max8997->buck125_gpioindex) & 0x1;
  60. int set2 = ((max8997->buck125_gpioindex) >> 1) & 0x1;
  61. int set1 = ((max8997->buck125_gpioindex) >> 2) & 0x1;
  62. gpio_set_value(max8997->buck125_gpios[0], set1);
  63. gpio_set_value(max8997->buck125_gpios[1], set2);
  64. gpio_set_value(max8997->buck125_gpios[2], set3);
  65. }
  66. struct voltage_map_desc {
  67. int min;
  68. int max;
  69. int step;
  70. };
  71. /* Voltage maps in uV */
  72. static const struct voltage_map_desc ldo_voltage_map_desc = {
  73. .min = 800000, .max = 3950000, .step = 50000,
  74. }; /* LDO1 ~ 18, 21 all */
  75. static const struct voltage_map_desc buck1245_voltage_map_desc = {
  76. .min = 650000, .max = 2225000, .step = 25000,
  77. }; /* Buck1, 2, 4, 5 */
  78. static const struct voltage_map_desc buck37_voltage_map_desc = {
  79. .min = 750000, .max = 3900000, .step = 50000,
  80. }; /* Buck3, 7 */
  81. /* current map in uA */
  82. static const struct voltage_map_desc charger_current_map_desc = {
  83. .min = 200000, .max = 950000, .step = 50000,
  84. };
  85. static const struct voltage_map_desc topoff_current_map_desc = {
  86. .min = 50000, .max = 200000, .step = 10000,
  87. };
  88. static const struct voltage_map_desc *reg_voltage_map[] = {
  89. [MAX8997_LDO1] = &ldo_voltage_map_desc,
  90. [MAX8997_LDO2] = &ldo_voltage_map_desc,
  91. [MAX8997_LDO3] = &ldo_voltage_map_desc,
  92. [MAX8997_LDO4] = &ldo_voltage_map_desc,
  93. [MAX8997_LDO5] = &ldo_voltage_map_desc,
  94. [MAX8997_LDO6] = &ldo_voltage_map_desc,
  95. [MAX8997_LDO7] = &ldo_voltage_map_desc,
  96. [MAX8997_LDO8] = &ldo_voltage_map_desc,
  97. [MAX8997_LDO9] = &ldo_voltage_map_desc,
  98. [MAX8997_LDO10] = &ldo_voltage_map_desc,
  99. [MAX8997_LDO11] = &ldo_voltage_map_desc,
  100. [MAX8997_LDO12] = &ldo_voltage_map_desc,
  101. [MAX8997_LDO13] = &ldo_voltage_map_desc,
  102. [MAX8997_LDO14] = &ldo_voltage_map_desc,
  103. [MAX8997_LDO15] = &ldo_voltage_map_desc,
  104. [MAX8997_LDO16] = &ldo_voltage_map_desc,
  105. [MAX8997_LDO17] = &ldo_voltage_map_desc,
  106. [MAX8997_LDO18] = &ldo_voltage_map_desc,
  107. [MAX8997_LDO21] = &ldo_voltage_map_desc,
  108. [MAX8997_BUCK1] = &buck1245_voltage_map_desc,
  109. [MAX8997_BUCK2] = &buck1245_voltage_map_desc,
  110. [MAX8997_BUCK3] = &buck37_voltage_map_desc,
  111. [MAX8997_BUCK4] = &buck1245_voltage_map_desc,
  112. [MAX8997_BUCK5] = &buck1245_voltage_map_desc,
  113. [MAX8997_BUCK6] = NULL,
  114. [MAX8997_BUCK7] = &buck37_voltage_map_desc,
  115. [MAX8997_EN32KHZ_AP] = NULL,
  116. [MAX8997_EN32KHZ_CP] = NULL,
  117. [MAX8997_ENVICHG] = NULL,
  118. [MAX8997_ESAFEOUT1] = NULL,
  119. [MAX8997_ESAFEOUT2] = NULL,
  120. [MAX8997_CHARGER_CV] = NULL,
  121. [MAX8997_CHARGER] = &charger_current_map_desc,
  122. [MAX8997_CHARGER_TOPOFF] = &topoff_current_map_desc,
  123. };
  124. static int max8997_list_voltage_charger_cv(struct regulator_dev *rdev,
  125. unsigned int selector)
  126. {
  127. int rid = rdev_get_id(rdev);
  128. if (rid != MAX8997_CHARGER_CV)
  129. goto err;
  130. switch (selector) {
  131. case 0x00:
  132. return 4200000;
  133. case 0x01 ... 0x0E:
  134. return 4000000 + 20000 * (selector - 0x01);
  135. case 0x0F:
  136. return 4350000;
  137. default:
  138. return -EINVAL;
  139. }
  140. err:
  141. return -EINVAL;
  142. }
  143. static int max8997_list_voltage(struct regulator_dev *rdev,
  144. unsigned int selector)
  145. {
  146. const struct voltage_map_desc *desc;
  147. int rid = rdev_get_id(rdev);
  148. int val;
  149. if (rid >= ARRAY_SIZE(reg_voltage_map) ||
  150. rid < 0)
  151. return -EINVAL;
  152. desc = reg_voltage_map[rid];
  153. if (desc == NULL)
  154. return -EINVAL;
  155. val = desc->min + desc->step * selector;
  156. if (val > desc->max)
  157. return -EINVAL;
  158. return val;
  159. }
  160. static int max8997_get_enable_register(struct regulator_dev *rdev,
  161. int *reg, int *mask, int *pattern)
  162. {
  163. int rid = rdev_get_id(rdev);
  164. switch (rid) {
  165. case MAX8997_LDO1 ... MAX8997_LDO21:
  166. *reg = MAX8997_REG_LDO1CTRL + (rid - MAX8997_LDO1);
  167. *mask = 0xC0;
  168. *pattern = 0xC0;
  169. break;
  170. case MAX8997_BUCK1:
  171. *reg = MAX8997_REG_BUCK1CTRL;
  172. *mask = 0x01;
  173. *pattern = 0x01;
  174. break;
  175. case MAX8997_BUCK2:
  176. *reg = MAX8997_REG_BUCK2CTRL;
  177. *mask = 0x01;
  178. *pattern = 0x01;
  179. break;
  180. case MAX8997_BUCK3:
  181. *reg = MAX8997_REG_BUCK3CTRL;
  182. *mask = 0x01;
  183. *pattern = 0x01;
  184. break;
  185. case MAX8997_BUCK4:
  186. *reg = MAX8997_REG_BUCK4CTRL;
  187. *mask = 0x01;
  188. *pattern = 0x01;
  189. break;
  190. case MAX8997_BUCK5:
  191. *reg = MAX8997_REG_BUCK5CTRL;
  192. *mask = 0x01;
  193. *pattern = 0x01;
  194. break;
  195. case MAX8997_BUCK6:
  196. *reg = MAX8997_REG_BUCK6CTRL;
  197. *mask = 0x01;
  198. *pattern = 0x01;
  199. break;
  200. case MAX8997_BUCK7:
  201. *reg = MAX8997_REG_BUCK7CTRL;
  202. *mask = 0x01;
  203. *pattern = 0x01;
  204. break;
  205. case MAX8997_EN32KHZ_AP ... MAX8997_EN32KHZ_CP:
  206. *reg = MAX8997_REG_MAINCON1;
  207. *mask = 0x01 << (rid - MAX8997_EN32KHZ_AP);
  208. *pattern = 0x01 << (rid - MAX8997_EN32KHZ_AP);
  209. break;
  210. case MAX8997_ENVICHG:
  211. *reg = MAX8997_REG_MBCCTRL1;
  212. *mask = 0x80;
  213. *pattern = 0x80;
  214. break;
  215. case MAX8997_ESAFEOUT1 ... MAX8997_ESAFEOUT2:
  216. *reg = MAX8997_REG_SAFEOUTCTRL;
  217. *mask = 0x40 << (rid - MAX8997_ESAFEOUT1);
  218. *pattern = 0x40 << (rid - MAX8997_ESAFEOUT1);
  219. break;
  220. case MAX8997_CHARGER:
  221. *reg = MAX8997_REG_MBCCTRL2;
  222. *mask = 0x40;
  223. *pattern = 0x40;
  224. break;
  225. default:
  226. /* Not controllable or not exists */
  227. return -EINVAL;
  228. }
  229. return 0;
  230. }
  231. static int max8997_reg_is_enabled(struct regulator_dev *rdev)
  232. {
  233. struct max8997_data *max8997 = rdev_get_drvdata(rdev);
  234. struct i2c_client *i2c = max8997->iodev->i2c;
  235. int ret, reg, mask, pattern;
  236. u8 val;
  237. ret = max8997_get_enable_register(rdev, &reg, &mask, &pattern);
  238. if (ret)
  239. return ret;
  240. ret = max8997_read_reg(i2c, reg, &val);
  241. if (ret)
  242. return ret;
  243. return (val & mask) == pattern;
  244. }
  245. static int max8997_reg_enable(struct regulator_dev *rdev)
  246. {
  247. struct max8997_data *max8997 = rdev_get_drvdata(rdev);
  248. struct i2c_client *i2c = max8997->iodev->i2c;
  249. int ret, reg, mask, pattern;
  250. ret = max8997_get_enable_register(rdev, &reg, &mask, &pattern);
  251. if (ret)
  252. return ret;
  253. return max8997_update_reg(i2c, reg, pattern, mask);
  254. }
  255. static int max8997_reg_disable(struct regulator_dev *rdev)
  256. {
  257. struct max8997_data *max8997 = rdev_get_drvdata(rdev);
  258. struct i2c_client *i2c = max8997->iodev->i2c;
  259. int ret, reg, mask, pattern;
  260. ret = max8997_get_enable_register(rdev, &reg, &mask, &pattern);
  261. if (ret)
  262. return ret;
  263. return max8997_update_reg(i2c, reg, ~pattern, mask);
  264. }
  265. static int max8997_get_voltage_register(struct regulator_dev *rdev,
  266. int *_reg, int *_shift, int *_mask)
  267. {
  268. struct max8997_data *max8997 = rdev_get_drvdata(rdev);
  269. int rid = rdev_get_id(rdev);
  270. int reg, shift = 0, mask = 0x3f;
  271. switch (rid) {
  272. case MAX8997_LDO1 ... MAX8997_LDO21:
  273. reg = MAX8997_REG_LDO1CTRL + (rid - MAX8997_LDO1);
  274. break;
  275. case MAX8997_BUCK1:
  276. reg = MAX8997_REG_BUCK1DVS1;
  277. if (max8997->buck1_gpiodvs)
  278. reg += max8997->buck125_gpioindex;
  279. break;
  280. case MAX8997_BUCK2:
  281. reg = MAX8997_REG_BUCK2DVS1;
  282. if (max8997->buck2_gpiodvs)
  283. reg += max8997->buck125_gpioindex;
  284. break;
  285. case MAX8997_BUCK3:
  286. reg = MAX8997_REG_BUCK3DVS;
  287. break;
  288. case MAX8997_BUCK4:
  289. reg = MAX8997_REG_BUCK4DVS;
  290. break;
  291. case MAX8997_BUCK5:
  292. reg = MAX8997_REG_BUCK5DVS1;
  293. if (max8997->buck5_gpiodvs)
  294. reg += max8997->buck125_gpioindex;
  295. break;
  296. case MAX8997_BUCK7:
  297. reg = MAX8997_REG_BUCK7DVS;
  298. break;
  299. case MAX8997_ESAFEOUT1 ... MAX8997_ESAFEOUT2:
  300. reg = MAX8997_REG_SAFEOUTCTRL;
  301. shift = (rid == MAX8997_ESAFEOUT2) ? 2 : 0;
  302. mask = 0x3;
  303. break;
  304. case MAX8997_CHARGER_CV:
  305. reg = MAX8997_REG_MBCCTRL3;
  306. shift = 0;
  307. mask = 0xf;
  308. break;
  309. case MAX8997_CHARGER:
  310. reg = MAX8997_REG_MBCCTRL4;
  311. shift = 0;
  312. mask = 0xf;
  313. break;
  314. case MAX8997_CHARGER_TOPOFF:
  315. reg = MAX8997_REG_MBCCTRL5;
  316. shift = 0;
  317. mask = 0xf;
  318. break;
  319. default:
  320. return -EINVAL;
  321. }
  322. *_reg = reg;
  323. *_shift = shift;
  324. *_mask = mask;
  325. return 0;
  326. }
  327. static int max8997_get_voltage_sel(struct regulator_dev *rdev)
  328. {
  329. struct max8997_data *max8997 = rdev_get_drvdata(rdev);
  330. struct i2c_client *i2c = max8997->iodev->i2c;
  331. int reg, shift, mask, ret;
  332. u8 val;
  333. ret = max8997_get_voltage_register(rdev, &reg, &shift, &mask);
  334. if (ret)
  335. return ret;
  336. ret = max8997_read_reg(i2c, reg, &val);
  337. if (ret)
  338. return ret;
  339. val >>= shift;
  340. val &= mask;
  341. return val;
  342. }
  343. static inline int max8997_get_voltage_proper_val(
  344. const struct voltage_map_desc *desc,
  345. int min_vol, int max_vol)
  346. {
  347. int i;
  348. if (desc == NULL)
  349. return -EINVAL;
  350. if (max_vol < desc->min || min_vol > desc->max)
  351. return -EINVAL;
  352. if (min_vol < desc->min)
  353. min_vol = desc->min;
  354. i = DIV_ROUND_UP(min_vol - desc->min, desc->step);
  355. if (desc->min + desc->step * i > max_vol)
  356. return -EINVAL;
  357. return i;
  358. }
  359. static int max8997_set_voltage_charger_cv(struct regulator_dev *rdev,
  360. int min_uV, int max_uV, unsigned *selector)
  361. {
  362. struct max8997_data *max8997 = rdev_get_drvdata(rdev);
  363. struct i2c_client *i2c = max8997->iodev->i2c;
  364. int rid = rdev_get_id(rdev);
  365. int lb, ub;
  366. int reg, shift = 0, mask, ret = 0;
  367. u8 val = 0x0;
  368. if (rid != MAX8997_CHARGER_CV)
  369. return -EINVAL;
  370. ret = max8997_get_voltage_register(rdev, &reg, &shift, &mask);
  371. if (ret)
  372. return ret;
  373. if (max_uV < 4000000 || min_uV > 4350000)
  374. return -EINVAL;
  375. if (min_uV <= 4000000) {
  376. if (max_uV >= 4000000)
  377. return -EINVAL;
  378. else
  379. val = 0x1;
  380. } else if (min_uV <= 4200000 && max_uV >= 4200000)
  381. val = 0x0;
  382. else {
  383. lb = (min_uV - 4000001) / 20000 + 2;
  384. ub = (max_uV - 4000000) / 20000 + 1;
  385. if (lb > ub)
  386. return -EINVAL;
  387. if (lb < 0xf)
  388. val = lb;
  389. else {
  390. if (ub >= 0xf)
  391. val = 0xf;
  392. else
  393. return -EINVAL;
  394. }
  395. }
  396. *selector = val;
  397. ret = max8997_update_reg(i2c, reg, val << shift, mask);
  398. return ret;
  399. }
  400. /*
  401. * For LDO1 ~ LDO21, BUCK1~5, BUCK7, CHARGER, CHARGER_TOPOFF
  402. * BUCK1, 2, and 5 are available if they are not controlled by gpio
  403. */
  404. static int max8997_set_voltage_ldobuck(struct regulator_dev *rdev,
  405. int min_uV, int max_uV, unsigned *selector)
  406. {
  407. struct max8997_data *max8997 = rdev_get_drvdata(rdev);
  408. struct i2c_client *i2c = max8997->iodev->i2c;
  409. const struct voltage_map_desc *desc;
  410. int rid = rdev_get_id(rdev);
  411. int i, reg, shift, mask, ret;
  412. switch (rid) {
  413. case MAX8997_LDO1 ... MAX8997_LDO21:
  414. break;
  415. case MAX8997_BUCK1 ... MAX8997_BUCK5:
  416. break;
  417. case MAX8997_BUCK6:
  418. return -EINVAL;
  419. case MAX8997_BUCK7:
  420. break;
  421. case MAX8997_CHARGER:
  422. break;
  423. case MAX8997_CHARGER_TOPOFF:
  424. break;
  425. default:
  426. return -EINVAL;
  427. }
  428. desc = reg_voltage_map[rid];
  429. i = max8997_get_voltage_proper_val(desc, min_uV, max_uV);
  430. if (i < 0)
  431. return i;
  432. ret = max8997_get_voltage_register(rdev, &reg, &shift, &mask);
  433. if (ret)
  434. return ret;
  435. ret = max8997_update_reg(i2c, reg, i << shift, mask << shift);
  436. *selector = i;
  437. return ret;
  438. }
  439. static int max8997_set_voltage_buck_time_sel(struct regulator_dev *rdev,
  440. unsigned int old_selector,
  441. unsigned int new_selector)
  442. {
  443. struct max8997_data *max8997 = rdev_get_drvdata(rdev);
  444. int rid = rdev_get_id(rdev);
  445. const struct voltage_map_desc *desc = reg_voltage_map[rid];
  446. /* Delay is required only if the voltage is increasing */
  447. if (old_selector >= new_selector)
  448. return 0;
  449. /* No need to delay if gpio_dvs_mode */
  450. switch (rid) {
  451. case MAX8997_BUCK1:
  452. if (max8997->buck1_gpiodvs)
  453. return 0;
  454. break;
  455. case MAX8997_BUCK2:
  456. if (max8997->buck2_gpiodvs)
  457. return 0;
  458. break;
  459. case MAX8997_BUCK5:
  460. if (max8997->buck5_gpiodvs)
  461. return 0;
  462. break;
  463. }
  464. switch (rid) {
  465. case MAX8997_BUCK1:
  466. case MAX8997_BUCK2:
  467. case MAX8997_BUCK4:
  468. case MAX8997_BUCK5:
  469. return DIV_ROUND_UP(desc->step * (new_selector - old_selector),
  470. max8997->ramp_delay * 1000);
  471. }
  472. return 0;
  473. }
  474. /*
  475. * Assess the damage on the voltage setting of BUCK1,2,5 by the change.
  476. *
  477. * When GPIO-DVS mode is used for multiple bucks, changing the voltage value
  478. * of one of the bucks may affect that of another buck, which is the side
  479. * effect of the change (set_voltage). This function examines the GPIO-DVS
  480. * configurations and checks whether such side-effect exists.
  481. */
  482. static int max8997_assess_side_effect(struct regulator_dev *rdev,
  483. u8 new_val, int *best)
  484. {
  485. struct max8997_data *max8997 = rdev_get_drvdata(rdev);
  486. int rid = rdev_get_id(rdev);
  487. u8 *buckx_val[3];
  488. bool buckx_gpiodvs[3];
  489. int side_effect[8];
  490. int min_side_effect = INT_MAX;
  491. int i;
  492. *best = -1;
  493. switch (rid) {
  494. case MAX8997_BUCK1:
  495. rid = 0;
  496. break;
  497. case MAX8997_BUCK2:
  498. rid = 1;
  499. break;
  500. case MAX8997_BUCK5:
  501. rid = 2;
  502. break;
  503. default:
  504. return -EINVAL;
  505. }
  506. buckx_val[0] = max8997->buck1_vol;
  507. buckx_val[1] = max8997->buck2_vol;
  508. buckx_val[2] = max8997->buck5_vol;
  509. buckx_gpiodvs[0] = max8997->buck1_gpiodvs;
  510. buckx_gpiodvs[1] = max8997->buck2_gpiodvs;
  511. buckx_gpiodvs[2] = max8997->buck5_gpiodvs;
  512. for (i = 0; i < 8; i++) {
  513. int others;
  514. if (new_val != (buckx_val[rid])[i]) {
  515. side_effect[i] = -1;
  516. continue;
  517. }
  518. side_effect[i] = 0;
  519. for (others = 0; others < 3; others++) {
  520. int diff;
  521. if (others == rid)
  522. continue;
  523. if (buckx_gpiodvs[others] == false)
  524. continue; /* Not affected */
  525. diff = (buckx_val[others])[i] -
  526. (buckx_val[others])[max8997->buck125_gpioindex];
  527. if (diff > 0)
  528. side_effect[i] += diff;
  529. else if (diff < 0)
  530. side_effect[i] -= diff;
  531. }
  532. if (side_effect[i] == 0) {
  533. *best = i;
  534. return 0; /* NO SIDE EFFECT! Use This! */
  535. }
  536. if (side_effect[i] < min_side_effect) {
  537. min_side_effect = side_effect[i];
  538. *best = i;
  539. }
  540. }
  541. if (*best == -1)
  542. return -EINVAL;
  543. return side_effect[*best];
  544. }
  545. /*
  546. * For Buck 1 ~ 5 and 7. If it is not controlled by GPIO, this calls
  547. * max8997_set_voltage_ldobuck to do the job.
  548. */
  549. static int max8997_set_voltage_buck(struct regulator_dev *rdev,
  550. int min_uV, int max_uV, unsigned *selector)
  551. {
  552. struct max8997_data *max8997 = rdev_get_drvdata(rdev);
  553. int rid = rdev_get_id(rdev);
  554. const struct voltage_map_desc *desc;
  555. int new_val, new_idx, damage, tmp_val, tmp_idx, tmp_dmg;
  556. bool gpio_dvs_mode = false;
  557. if (rid < MAX8997_BUCK1 || rid > MAX8997_BUCK7)
  558. return -EINVAL;
  559. switch (rid) {
  560. case MAX8997_BUCK1:
  561. if (max8997->buck1_gpiodvs)
  562. gpio_dvs_mode = true;
  563. break;
  564. case MAX8997_BUCK2:
  565. if (max8997->buck2_gpiodvs)
  566. gpio_dvs_mode = true;
  567. break;
  568. case MAX8997_BUCK5:
  569. if (max8997->buck5_gpiodvs)
  570. gpio_dvs_mode = true;
  571. break;
  572. }
  573. if (!gpio_dvs_mode)
  574. return max8997_set_voltage_ldobuck(rdev, min_uV, max_uV,
  575. selector);
  576. desc = reg_voltage_map[rid];
  577. new_val = max8997_get_voltage_proper_val(desc, min_uV, max_uV);
  578. if (new_val < 0)
  579. return new_val;
  580. tmp_dmg = INT_MAX;
  581. tmp_idx = -1;
  582. tmp_val = -1;
  583. do {
  584. damage = max8997_assess_side_effect(rdev, new_val, &new_idx);
  585. if (damage == 0)
  586. goto out;
  587. if (tmp_dmg > damage) {
  588. tmp_idx = new_idx;
  589. tmp_val = new_val;
  590. tmp_dmg = damage;
  591. }
  592. new_val++;
  593. } while (desc->min + desc->step * new_val <= desc->max);
  594. new_idx = tmp_idx;
  595. new_val = tmp_val;
  596. if (max8997->ignore_gpiodvs_side_effect == false)
  597. return -EINVAL;
  598. dev_warn(&rdev->dev,
  599. "MAX8997 GPIO-DVS Side Effect Warning: GPIO SET: %d -> %d\n",
  600. max8997->buck125_gpioindex, tmp_idx);
  601. out:
  602. if (new_idx < 0 || new_val < 0)
  603. return -EINVAL;
  604. max8997->buck125_gpioindex = new_idx;
  605. max8997_set_gpio(max8997);
  606. *selector = new_val;
  607. return 0;
  608. }
  609. /* For SAFEOUT1 and SAFEOUT2 */
  610. static int max8997_set_voltage_safeout_sel(struct regulator_dev *rdev,
  611. unsigned selector)
  612. {
  613. struct max8997_data *max8997 = rdev_get_drvdata(rdev);
  614. struct i2c_client *i2c = max8997->iodev->i2c;
  615. int rid = rdev_get_id(rdev);
  616. int reg, shift = 0, mask, ret;
  617. if (rid != MAX8997_ESAFEOUT1 && rid != MAX8997_ESAFEOUT2)
  618. return -EINVAL;
  619. ret = max8997_get_voltage_register(rdev, &reg, &shift, &mask);
  620. if (ret)
  621. return ret;
  622. return max8997_update_reg(i2c, reg, selector << shift, mask << shift);
  623. }
  624. static int max8997_reg_disable_suspend(struct regulator_dev *rdev)
  625. {
  626. struct max8997_data *max8997 = rdev_get_drvdata(rdev);
  627. struct i2c_client *i2c = max8997->iodev->i2c;
  628. int ret, reg, mask, pattern;
  629. int rid = rdev_get_id(rdev);
  630. ret = max8997_get_enable_register(rdev, &reg, &mask, &pattern);
  631. if (ret)
  632. return ret;
  633. max8997_read_reg(i2c, reg, &max8997->saved_states[rid]);
  634. if (rid == MAX8997_LDO1 ||
  635. rid == MAX8997_LDO10 ||
  636. rid == MAX8997_LDO21) {
  637. dev_dbg(&rdev->dev, "Conditional Power-Off for %s\n",
  638. rdev->desc->name);
  639. return max8997_update_reg(i2c, reg, 0x40, mask);
  640. }
  641. dev_dbg(&rdev->dev, "Full Power-Off for %s (%xh -> %xh)\n",
  642. rdev->desc->name, max8997->saved_states[rid] & mask,
  643. (~pattern) & mask);
  644. return max8997_update_reg(i2c, reg, ~pattern, mask);
  645. }
  646. static struct regulator_ops max8997_ldo_ops = {
  647. .list_voltage = max8997_list_voltage,
  648. .is_enabled = max8997_reg_is_enabled,
  649. .enable = max8997_reg_enable,
  650. .disable = max8997_reg_disable,
  651. .get_voltage_sel = max8997_get_voltage_sel,
  652. .set_voltage = max8997_set_voltage_ldobuck,
  653. .set_suspend_disable = max8997_reg_disable_suspend,
  654. };
  655. static struct regulator_ops max8997_buck_ops = {
  656. .list_voltage = max8997_list_voltage,
  657. .is_enabled = max8997_reg_is_enabled,
  658. .enable = max8997_reg_enable,
  659. .disable = max8997_reg_disable,
  660. .get_voltage_sel = max8997_get_voltage_sel,
  661. .set_voltage = max8997_set_voltage_buck,
  662. .set_voltage_time_sel = max8997_set_voltage_buck_time_sel,
  663. .set_suspend_disable = max8997_reg_disable_suspend,
  664. };
  665. static struct regulator_ops max8997_fixedvolt_ops = {
  666. .list_voltage = max8997_list_voltage,
  667. .is_enabled = max8997_reg_is_enabled,
  668. .enable = max8997_reg_enable,
  669. .disable = max8997_reg_disable,
  670. .set_suspend_disable = max8997_reg_disable_suspend,
  671. };
  672. static struct regulator_ops max8997_safeout_ops = {
  673. .list_voltage = regulator_list_voltage_table,
  674. .is_enabled = max8997_reg_is_enabled,
  675. .enable = max8997_reg_enable,
  676. .disable = max8997_reg_disable,
  677. .get_voltage_sel = max8997_get_voltage_sel,
  678. .set_voltage_sel = max8997_set_voltage_safeout_sel,
  679. .set_suspend_disable = max8997_reg_disable_suspend,
  680. };
  681. static struct regulator_ops max8997_fixedstate_ops = {
  682. .list_voltage = max8997_list_voltage_charger_cv,
  683. .get_voltage_sel = max8997_get_voltage_sel,
  684. .set_voltage = max8997_set_voltage_charger_cv,
  685. };
  686. static int max8997_set_current_limit(struct regulator_dev *rdev,
  687. int min_uA, int max_uA)
  688. {
  689. unsigned dummy;
  690. int rid = rdev_get_id(rdev);
  691. if (rid != MAX8997_CHARGER && rid != MAX8997_CHARGER_TOPOFF)
  692. return -EINVAL;
  693. /* Reuse max8997_set_voltage_ldobuck to set current_limit. */
  694. return max8997_set_voltage_ldobuck(rdev, min_uA, max_uA, &dummy);
  695. }
  696. static int max8997_get_current_limit(struct regulator_dev *rdev)
  697. {
  698. int sel, rid = rdev_get_id(rdev);
  699. if (rid != MAX8997_CHARGER && rid != MAX8997_CHARGER_TOPOFF)
  700. return -EINVAL;
  701. sel = max8997_get_voltage_sel(rdev);
  702. if (sel < 0)
  703. return sel;
  704. /* Reuse max8997_list_voltage to get current_limit. */
  705. return max8997_list_voltage(rdev, sel);
  706. }
  707. static struct regulator_ops max8997_charger_ops = {
  708. .is_enabled = max8997_reg_is_enabled,
  709. .enable = max8997_reg_enable,
  710. .disable = max8997_reg_disable,
  711. .get_current_limit = max8997_get_current_limit,
  712. .set_current_limit = max8997_set_current_limit,
  713. };
  714. static struct regulator_ops max8997_charger_fixedstate_ops = {
  715. .get_current_limit = max8997_get_current_limit,
  716. .set_current_limit = max8997_set_current_limit,
  717. };
  718. #define MAX8997_VOLTAGE_REGULATOR(_name, _ops) {\
  719. .name = #_name, \
  720. .id = MAX8997_##_name, \
  721. .ops = &_ops, \
  722. .type = REGULATOR_VOLTAGE, \
  723. .owner = THIS_MODULE, \
  724. }
  725. #define MAX8997_CURRENT_REGULATOR(_name, _ops) {\
  726. .name = #_name, \
  727. .id = MAX8997_##_name, \
  728. .ops = &_ops, \
  729. .type = REGULATOR_CURRENT, \
  730. .owner = THIS_MODULE, \
  731. }
  732. static struct regulator_desc regulators[] = {
  733. MAX8997_VOLTAGE_REGULATOR(LDO1, max8997_ldo_ops),
  734. MAX8997_VOLTAGE_REGULATOR(LDO2, max8997_ldo_ops),
  735. MAX8997_VOLTAGE_REGULATOR(LDO3, max8997_ldo_ops),
  736. MAX8997_VOLTAGE_REGULATOR(LDO4, max8997_ldo_ops),
  737. MAX8997_VOLTAGE_REGULATOR(LDO5, max8997_ldo_ops),
  738. MAX8997_VOLTAGE_REGULATOR(LDO6, max8997_ldo_ops),
  739. MAX8997_VOLTAGE_REGULATOR(LDO7, max8997_ldo_ops),
  740. MAX8997_VOLTAGE_REGULATOR(LDO8, max8997_ldo_ops),
  741. MAX8997_VOLTAGE_REGULATOR(LDO9, max8997_ldo_ops),
  742. MAX8997_VOLTAGE_REGULATOR(LDO10, max8997_ldo_ops),
  743. MAX8997_VOLTAGE_REGULATOR(LDO11, max8997_ldo_ops),
  744. MAX8997_VOLTAGE_REGULATOR(LDO12, max8997_ldo_ops),
  745. MAX8997_VOLTAGE_REGULATOR(LDO13, max8997_ldo_ops),
  746. MAX8997_VOLTAGE_REGULATOR(LDO14, max8997_ldo_ops),
  747. MAX8997_VOLTAGE_REGULATOR(LDO15, max8997_ldo_ops),
  748. MAX8997_VOLTAGE_REGULATOR(LDO16, max8997_ldo_ops),
  749. MAX8997_VOLTAGE_REGULATOR(LDO17, max8997_ldo_ops),
  750. MAX8997_VOLTAGE_REGULATOR(LDO18, max8997_ldo_ops),
  751. MAX8997_VOLTAGE_REGULATOR(LDO21, max8997_ldo_ops),
  752. MAX8997_VOLTAGE_REGULATOR(BUCK1, max8997_buck_ops),
  753. MAX8997_VOLTAGE_REGULATOR(BUCK2, max8997_buck_ops),
  754. MAX8997_VOLTAGE_REGULATOR(BUCK3, max8997_buck_ops),
  755. MAX8997_VOLTAGE_REGULATOR(BUCK4, max8997_buck_ops),
  756. MAX8997_VOLTAGE_REGULATOR(BUCK5, max8997_buck_ops),
  757. MAX8997_VOLTAGE_REGULATOR(BUCK6, max8997_fixedvolt_ops),
  758. MAX8997_VOLTAGE_REGULATOR(BUCK7, max8997_buck_ops),
  759. MAX8997_VOLTAGE_REGULATOR(EN32KHZ_AP, max8997_fixedvolt_ops),
  760. MAX8997_VOLTAGE_REGULATOR(EN32KHZ_CP, max8997_fixedvolt_ops),
  761. MAX8997_VOLTAGE_REGULATOR(ENVICHG, max8997_fixedvolt_ops),
  762. MAX8997_VOLTAGE_REGULATOR(ESAFEOUT1, max8997_safeout_ops),
  763. MAX8997_VOLTAGE_REGULATOR(ESAFEOUT2, max8997_safeout_ops),
  764. MAX8997_VOLTAGE_REGULATOR(CHARGER_CV, max8997_fixedstate_ops),
  765. MAX8997_CURRENT_REGULATOR(CHARGER, max8997_charger_ops),
  766. MAX8997_CURRENT_REGULATOR(CHARGER_TOPOFF,
  767. max8997_charger_fixedstate_ops),
  768. };
  769. #ifdef CONFIG_OF
  770. static int max8997_pmic_dt_parse_dvs_gpio(struct platform_device *pdev,
  771. struct max8997_platform_data *pdata,
  772. struct device_node *pmic_np)
  773. {
  774. int i, gpio;
  775. for (i = 0; i < 3; i++) {
  776. gpio = of_get_named_gpio(pmic_np,
  777. "max8997,pmic-buck125-dvs-gpios", i);
  778. if (!gpio_is_valid(gpio)) {
  779. dev_err(&pdev->dev, "invalid gpio[%d]: %d\n", i, gpio);
  780. return -EINVAL;
  781. }
  782. pdata->buck125_gpios[i] = gpio;
  783. }
  784. return 0;
  785. }
  786. static int max8997_pmic_dt_parse_pdata(struct platform_device *pdev,
  787. struct max8997_platform_data *pdata)
  788. {
  789. struct max8997_dev *iodev = dev_get_drvdata(pdev->dev.parent);
  790. struct device_node *pmic_np, *regulators_np, *reg_np;
  791. struct max8997_regulator_data *rdata;
  792. unsigned int i, dvs_voltage_nr = 1, ret;
  793. pmic_np = iodev->dev->of_node;
  794. if (!pmic_np) {
  795. dev_err(&pdev->dev, "could not find pmic sub-node\n");
  796. return -ENODEV;
  797. }
  798. regulators_np = of_get_child_by_name(pmic_np, "regulators");
  799. if (!regulators_np) {
  800. dev_err(&pdev->dev, "could not find regulators sub-node\n");
  801. return -EINVAL;
  802. }
  803. /* count the number of regulators to be supported in pmic */
  804. pdata->num_regulators = of_get_child_count(regulators_np);
  805. rdata = devm_kzalloc(&pdev->dev, sizeof(*rdata) *
  806. pdata->num_regulators, GFP_KERNEL);
  807. if (!rdata) {
  808. of_node_put(regulators_np);
  809. return -ENOMEM;
  810. }
  811. pdata->regulators = rdata;
  812. for_each_child_of_node(regulators_np, reg_np) {
  813. for (i = 0; i < ARRAY_SIZE(regulators); i++)
  814. if (!of_node_cmp(reg_np->name, regulators[i].name))
  815. break;
  816. if (i == ARRAY_SIZE(regulators)) {
  817. dev_warn(&pdev->dev, "don't know how to configure regulator %s\n",
  818. reg_np->name);
  819. continue;
  820. }
  821. rdata->id = i;
  822. rdata->initdata = of_get_regulator_init_data(&pdev->dev,
  823. reg_np,
  824. &regulators[i]);
  825. rdata->reg_node = reg_np;
  826. rdata++;
  827. }
  828. of_node_put(regulators_np);
  829. if (of_get_property(pmic_np, "max8997,pmic-buck1-uses-gpio-dvs", NULL))
  830. pdata->buck1_gpiodvs = true;
  831. if (of_get_property(pmic_np, "max8997,pmic-buck2-uses-gpio-dvs", NULL))
  832. pdata->buck2_gpiodvs = true;
  833. if (of_get_property(pmic_np, "max8997,pmic-buck5-uses-gpio-dvs", NULL))
  834. pdata->buck5_gpiodvs = true;
  835. if (pdata->buck1_gpiodvs || pdata->buck2_gpiodvs ||
  836. pdata->buck5_gpiodvs) {
  837. ret = max8997_pmic_dt_parse_dvs_gpio(pdev, pdata, pmic_np);
  838. if (ret)
  839. return -EINVAL;
  840. if (of_property_read_u32(pmic_np,
  841. "max8997,pmic-buck125-default-dvs-idx",
  842. &pdata->buck125_default_idx)) {
  843. pdata->buck125_default_idx = 0;
  844. } else {
  845. if (pdata->buck125_default_idx >= 8) {
  846. pdata->buck125_default_idx = 0;
  847. dev_info(&pdev->dev, "invalid value for default dvs index, using 0 instead\n");
  848. }
  849. }
  850. if (of_get_property(pmic_np,
  851. "max8997,pmic-ignore-gpiodvs-side-effect", NULL))
  852. pdata->ignore_gpiodvs_side_effect = true;
  853. dvs_voltage_nr = 8;
  854. }
  855. if (of_property_read_u32_array(pmic_np,
  856. "max8997,pmic-buck1-dvs-voltage",
  857. pdata->buck1_voltage, dvs_voltage_nr)) {
  858. dev_err(&pdev->dev, "buck1 voltages not specified\n");
  859. return -EINVAL;
  860. }
  861. if (of_property_read_u32_array(pmic_np,
  862. "max8997,pmic-buck2-dvs-voltage",
  863. pdata->buck2_voltage, dvs_voltage_nr)) {
  864. dev_err(&pdev->dev, "buck2 voltages not specified\n");
  865. return -EINVAL;
  866. }
  867. if (of_property_read_u32_array(pmic_np,
  868. "max8997,pmic-buck5-dvs-voltage",
  869. pdata->buck5_voltage, dvs_voltage_nr)) {
  870. dev_err(&pdev->dev, "buck5 voltages not specified\n");
  871. return -EINVAL;
  872. }
  873. return 0;
  874. }
  875. #else
  876. static int max8997_pmic_dt_parse_pdata(struct platform_device *pdev,
  877. struct max8997_platform_data *pdata)
  878. {
  879. return 0;
  880. }
  881. #endif /* CONFIG_OF */
  882. static int max8997_pmic_probe(struct platform_device *pdev)
  883. {
  884. struct max8997_dev *iodev = dev_get_drvdata(pdev->dev.parent);
  885. struct max8997_platform_data *pdata = iodev->pdata;
  886. struct regulator_config config = { };
  887. struct regulator_dev *rdev;
  888. struct max8997_data *max8997;
  889. struct i2c_client *i2c;
  890. int i, ret, nr_dvs;
  891. u8 max_buck1 = 0, max_buck2 = 0, max_buck5 = 0;
  892. if (!pdata) {
  893. dev_err(&pdev->dev, "No platform init data supplied.\n");
  894. return -ENODEV;
  895. }
  896. if (iodev->dev->of_node) {
  897. ret = max8997_pmic_dt_parse_pdata(pdev, pdata);
  898. if (ret)
  899. return ret;
  900. }
  901. max8997 = devm_kzalloc(&pdev->dev, sizeof(struct max8997_data),
  902. GFP_KERNEL);
  903. if (!max8997)
  904. return -ENOMEM;
  905. max8997->dev = &pdev->dev;
  906. max8997->iodev = iodev;
  907. max8997->num_regulators = pdata->num_regulators;
  908. platform_set_drvdata(pdev, max8997);
  909. i2c = max8997->iodev->i2c;
  910. max8997->buck125_gpioindex = pdata->buck125_default_idx;
  911. max8997->buck1_gpiodvs = pdata->buck1_gpiodvs;
  912. max8997->buck2_gpiodvs = pdata->buck2_gpiodvs;
  913. max8997->buck5_gpiodvs = pdata->buck5_gpiodvs;
  914. memcpy(max8997->buck125_gpios, pdata->buck125_gpios, sizeof(int) * 3);
  915. max8997->ignore_gpiodvs_side_effect = pdata->ignore_gpiodvs_side_effect;
  916. nr_dvs = (pdata->buck1_gpiodvs || pdata->buck2_gpiodvs ||
  917. pdata->buck5_gpiodvs) ? 8 : 1;
  918. for (i = 0; i < nr_dvs; i++) {
  919. max8997->buck1_vol[i] = ret =
  920. max8997_get_voltage_proper_val(
  921. &buck1245_voltage_map_desc,
  922. pdata->buck1_voltage[i],
  923. pdata->buck1_voltage[i] +
  924. buck1245_voltage_map_desc.step);
  925. if (ret < 0)
  926. return ret;
  927. max8997->buck2_vol[i] = ret =
  928. max8997_get_voltage_proper_val(
  929. &buck1245_voltage_map_desc,
  930. pdata->buck2_voltage[i],
  931. pdata->buck2_voltage[i] +
  932. buck1245_voltage_map_desc.step);
  933. if (ret < 0)
  934. return ret;
  935. max8997->buck5_vol[i] = ret =
  936. max8997_get_voltage_proper_val(
  937. &buck1245_voltage_map_desc,
  938. pdata->buck5_voltage[i],
  939. pdata->buck5_voltage[i] +
  940. buck1245_voltage_map_desc.step);
  941. if (ret < 0)
  942. return ret;
  943. if (max_buck1 < max8997->buck1_vol[i])
  944. max_buck1 = max8997->buck1_vol[i];
  945. if (max_buck2 < max8997->buck2_vol[i])
  946. max_buck2 = max8997->buck2_vol[i];
  947. if (max_buck5 < max8997->buck5_vol[i])
  948. max_buck5 = max8997->buck5_vol[i];
  949. }
  950. /* For the safety, set max voltage before setting up */
  951. for (i = 0; i < 8; i++) {
  952. max8997_update_reg(i2c, MAX8997_REG_BUCK1DVS1 + i,
  953. max_buck1, 0x3f);
  954. max8997_update_reg(i2c, MAX8997_REG_BUCK2DVS1 + i,
  955. max_buck2, 0x3f);
  956. max8997_update_reg(i2c, MAX8997_REG_BUCK5DVS1 + i,
  957. max_buck5, 0x3f);
  958. }
  959. /* Initialize all the DVS related BUCK registers */
  960. for (i = 0; i < nr_dvs; i++) {
  961. max8997_update_reg(i2c, MAX8997_REG_BUCK1DVS1 + i,
  962. max8997->buck1_vol[i],
  963. 0x3f);
  964. max8997_update_reg(i2c, MAX8997_REG_BUCK2DVS1 + i,
  965. max8997->buck2_vol[i],
  966. 0x3f);
  967. max8997_update_reg(i2c, MAX8997_REG_BUCK5DVS1 + i,
  968. max8997->buck5_vol[i],
  969. 0x3f);
  970. }
  971. /*
  972. * If buck 1, 2, and 5 do not care DVS GPIO settings, ignore them.
  973. * If at least one of them cares, set gpios.
  974. */
  975. if (pdata->buck1_gpiodvs || pdata->buck2_gpiodvs ||
  976. pdata->buck5_gpiodvs) {
  977. if (!gpio_is_valid(pdata->buck125_gpios[0]) ||
  978. !gpio_is_valid(pdata->buck125_gpios[1]) ||
  979. !gpio_is_valid(pdata->buck125_gpios[2])) {
  980. dev_err(&pdev->dev, "GPIO NOT VALID\n");
  981. return -EINVAL;
  982. }
  983. ret = devm_gpio_request(&pdev->dev, pdata->buck125_gpios[0],
  984. "MAX8997 SET1");
  985. if (ret)
  986. return ret;
  987. ret = devm_gpio_request(&pdev->dev, pdata->buck125_gpios[1],
  988. "MAX8997 SET2");
  989. if (ret)
  990. return ret;
  991. ret = devm_gpio_request(&pdev->dev, pdata->buck125_gpios[2],
  992. "MAX8997 SET3");
  993. if (ret)
  994. return ret;
  995. gpio_direction_output(pdata->buck125_gpios[0],
  996. (max8997->buck125_gpioindex >> 2)
  997. & 0x1); /* SET1 */
  998. gpio_direction_output(pdata->buck125_gpios[1],
  999. (max8997->buck125_gpioindex >> 1)
  1000. & 0x1); /* SET2 */
  1001. gpio_direction_output(pdata->buck125_gpios[2],
  1002. (max8997->buck125_gpioindex >> 0)
  1003. & 0x1); /* SET3 */
  1004. }
  1005. /* DVS-GPIO disabled */
  1006. max8997_update_reg(i2c, MAX8997_REG_BUCK1CTRL, (pdata->buck1_gpiodvs) ?
  1007. (1 << 1) : (0 << 1), 1 << 1);
  1008. max8997_update_reg(i2c, MAX8997_REG_BUCK2CTRL, (pdata->buck2_gpiodvs) ?
  1009. (1 << 1) : (0 << 1), 1 << 1);
  1010. max8997_update_reg(i2c, MAX8997_REG_BUCK5CTRL, (pdata->buck5_gpiodvs) ?
  1011. (1 << 1) : (0 << 1), 1 << 1);
  1012. /* Misc Settings */
  1013. max8997->ramp_delay = 10; /* set 10mV/us, which is the default */
  1014. max8997_write_reg(i2c, MAX8997_REG_BUCKRAMP, (0xf << 4) | 0x9);
  1015. for (i = 0; i < pdata->num_regulators; i++) {
  1016. const struct voltage_map_desc *desc;
  1017. int id = pdata->regulators[i].id;
  1018. desc = reg_voltage_map[id];
  1019. if (desc) {
  1020. regulators[id].n_voltages =
  1021. (desc->max - desc->min) / desc->step + 1;
  1022. } else if (id == MAX8997_ESAFEOUT1 || id == MAX8997_ESAFEOUT2) {
  1023. regulators[id].volt_table = safeoutvolt;
  1024. regulators[id].n_voltages = ARRAY_SIZE(safeoutvolt);
  1025. } else if (id == MAX8997_CHARGER_CV) {
  1026. regulators[id].n_voltages = 16;
  1027. }
  1028. config.dev = max8997->dev;
  1029. config.init_data = pdata->regulators[i].initdata;
  1030. config.driver_data = max8997;
  1031. config.of_node = pdata->regulators[i].reg_node;
  1032. rdev = devm_regulator_register(&pdev->dev, &regulators[id],
  1033. &config);
  1034. if (IS_ERR(rdev)) {
  1035. dev_err(max8997->dev, "regulator init failed for %d\n",
  1036. id);
  1037. return PTR_ERR(rdev);
  1038. }
  1039. }
  1040. return 0;
  1041. }
  1042. static const struct platform_device_id max8997_pmic_id[] = {
  1043. { "max8997-pmic", 0},
  1044. { },
  1045. };
  1046. MODULE_DEVICE_TABLE(platform, max8997_pmic_id);
  1047. static struct platform_driver max8997_pmic_driver = {
  1048. .driver = {
  1049. .name = "max8997-pmic",
  1050. },
  1051. .probe = max8997_pmic_probe,
  1052. .id_table = max8997_pmic_id,
  1053. };
  1054. static int __init max8997_pmic_init(void)
  1055. {
  1056. return platform_driver_register(&max8997_pmic_driver);
  1057. }
  1058. subsys_initcall(max8997_pmic_init);
  1059. static void __exit max8997_pmic_cleanup(void)
  1060. {
  1061. platform_driver_unregister(&max8997_pmic_driver);
  1062. }
  1063. module_exit(max8997_pmic_cleanup);
  1064. MODULE_DESCRIPTION("MAXIM 8997/8966 Regulator Driver");
  1065. MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
  1066. MODULE_LICENSE("GPL");