max77888-regulator.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. /*
  2. * max77888.c - Regulator driver for the Maxim 77888
  3. *
  4. * Copyright (C) 2011 Samsung Electronics
  5. * Sukdong Kim <sukdong.kim@smasung.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 max8997.c
  22. */
  23. #include <linux/bug.h>
  24. #include <linux/delay.h>
  25. #include <linux/err.h>
  26. #include <linux/gpio.h>
  27. #include <linux/slab.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/regulator/driver.h>
  30. #include <linux/regulator/machine.h>
  31. #include <linux/module.h>
  32. #include <linux/mfd/max77888.h>
  33. #include <linux/mfd/max77888-private.h>
  34. struct max77888_data {
  35. struct device *dev;
  36. struct max77888_dev *iodev;
  37. int num_regulators;
  38. struct regulator_dev **rdev;
  39. u8 saved_states[MAX77888_REG_MAX];
  40. };
  41. struct voltage_map_desc {
  42. int min;
  43. int max;
  44. int step;
  45. unsigned int n_bits;
  46. };
  47. /* current map in mA */
  48. static const struct voltage_map_desc charger_current_map_desc = {
  49. .min = 60, .max = 2580, .step = 20, .n_bits = 7,
  50. };
  51. static const struct voltage_map_desc topoff_current_map_desc = {
  52. .min = 50, .max = 200, .step = 10, .n_bits = 4,
  53. };
  54. static const struct voltage_map_desc *reg_voltage_map[] = {
  55. [MAX77888_ESAFEOUT1] = NULL,
  56. [MAX77888_ESAFEOUT2] = NULL,
  57. [MAX77888_CHARGER] = &charger_current_map_desc,
  58. };
  59. static inline int max77888_get_rid(struct regulator_dev *rdev)
  60. {
  61. dev_info(&rdev->dev, "func:%s\n", __func__);
  62. return rdev_get_id(rdev);
  63. }
  64. static int max77888_list_voltage_safeout(struct regulator_dev *rdev,
  65. unsigned int selector)
  66. {
  67. int rid = max77888_get_rid(rdev);
  68. dev_info(&rdev->dev, "func:%s\n", __func__);
  69. if (rid == MAX77888_ESAFEOUT1 || rid == MAX77888_ESAFEOUT2) {
  70. switch (selector) {
  71. case 0:
  72. return 4850000;
  73. case 1:
  74. return 4900000;
  75. case 2:
  76. return 4950000;
  77. case 3:
  78. return 3300000;
  79. default:
  80. return -EINVAL;
  81. }
  82. }
  83. return -EINVAL;
  84. }
  85. static int max77888_get_enable_register(struct regulator_dev *rdev,
  86. int *reg, int *mask, int *pattern)
  87. {
  88. int rid = max77888_get_rid(rdev);
  89. dev_info(&rdev->dev, "func:%s\n", __func__);
  90. switch (rid) {
  91. case MAX77888_ESAFEOUT1...MAX77888_ESAFEOUT2:
  92. *reg = MAX77888_CHG_REG_SAFEOUT_CTRL;
  93. *mask = 0x40 << (rid - MAX77888_ESAFEOUT1);
  94. *pattern = 0x40 << (rid - MAX77888_ESAFEOUT1);
  95. break;
  96. case MAX77888_CHARGER:
  97. *reg = MAX77888_CHG_REG_CHG_CNFG_00;
  98. *mask = 0xf;
  99. *pattern = 0x5;
  100. break;
  101. default:
  102. /* Not controllable or not exists */
  103. return -EINVAL;
  104. }
  105. return 0;
  106. }
  107. static int max77888_get_disable_register(struct regulator_dev *rdev,
  108. int *reg, int *mask, int *pattern)
  109. {
  110. int rid = max77888_get_rid(rdev);
  111. dev_info(&rdev->dev, "func:%s\n", __func__);
  112. switch (rid) {
  113. case MAX77888_ESAFEOUT1...MAX77888_ESAFEOUT2:
  114. *reg = MAX77888_CHG_REG_SAFEOUT_CTRL;
  115. *mask = 0x40 << (rid - MAX77888_ESAFEOUT1);
  116. *pattern = 0x00;
  117. break;
  118. case MAX77888_CHARGER:
  119. *reg = MAX77888_CHG_REG_CHG_CNFG_00;
  120. *mask = 0xf;
  121. *pattern = 0x00;
  122. break;
  123. default:
  124. /* Not controllable or not exists */
  125. return -EINVAL;
  126. }
  127. return 0;
  128. }
  129. static int max77888_reg_is_enabled(struct regulator_dev *rdev)
  130. {
  131. struct max77888_data *max77888 = rdev_get_drvdata(rdev);
  132. struct i2c_client *i2c = max77888->iodev->i2c;
  133. int ret, reg, mask, pattern;
  134. u8 val;
  135. dev_info(&rdev->dev, "func:%s\n", __func__);
  136. ret = max77888_get_enable_register(rdev, &reg, &mask, &pattern);
  137. if (ret == -EINVAL)
  138. return 1; /* "not controllable" */
  139. else if (ret)
  140. return ret;
  141. ret = max77888_read_reg(i2c, reg, &val);
  142. if (ret)
  143. return ret;
  144. return (val & mask) == pattern;
  145. }
  146. static int max77888_reg_enable(struct regulator_dev *rdev)
  147. {
  148. struct max77888_data *max77888 = rdev_get_drvdata(rdev);
  149. struct i2c_client *i2c = max77888->iodev->i2c;
  150. int ret, reg, mask, pattern;
  151. dev_info(&rdev->dev, "func:%s\n", __func__);
  152. ret = max77888_get_enable_register(rdev, &reg, &mask, &pattern);
  153. if (ret)
  154. return ret;
  155. return max77888_update_reg(i2c, reg, pattern, mask);
  156. }
  157. static int max77888_reg_disable(struct regulator_dev *rdev)
  158. {
  159. struct max77888_data *max77888 = rdev_get_drvdata(rdev);
  160. struct i2c_client *i2c = max77888->iodev->i2c;
  161. int ret, reg, mask, pattern;
  162. dev_info(&rdev->dev, "func:%s\n", __func__);
  163. ret = max77888_get_disable_register(rdev, &reg, &mask, &pattern);
  164. if (ret)
  165. return ret;
  166. return max77888_update_reg(i2c, reg, ~pattern, mask);
  167. }
  168. static int max77888_get_voltage_register(struct regulator_dev *rdev,
  169. int *_reg, int *_shift, int *_mask)
  170. {
  171. int rid = max77888_get_rid(rdev);
  172. int reg, shift = 0, mask = 0x3f;
  173. dev_info(&rdev->dev, "func:%s\n", __func__);
  174. switch (rid) {
  175. case MAX77888_ESAFEOUT1...MAX77888_ESAFEOUT2:
  176. reg = MAX77888_CHG_REG_SAFEOUT_CTRL;
  177. shift = (rid == MAX77888_ESAFEOUT2) ? 2 : 0;
  178. mask = 0x3;
  179. break;
  180. case MAX77888_CHARGER:
  181. reg = MAX77888_CHG_REG_CHG_CNFG_09;
  182. shift = 0;
  183. mask = 0x7f;
  184. break;
  185. default:
  186. return -EINVAL;
  187. }
  188. *_reg = reg;
  189. *_shift = shift;
  190. *_mask = mask;
  191. return 0;
  192. }
  193. static int max77888_list_voltage(struct regulator_dev *rdev,
  194. unsigned int selector)
  195. {
  196. const struct voltage_map_desc *desc;
  197. int rid = max77888_get_rid(rdev);
  198. int val;
  199. dev_info(&rdev->dev, "func:%s\n", __func__);
  200. if (rid >= ARRAY_SIZE(reg_voltage_map) || rid < 0)
  201. return -EINVAL;
  202. desc = reg_voltage_map[rid];
  203. if (desc == NULL)
  204. return -EINVAL;
  205. /* the first four codes for charger current are all 60mA */
  206. if (rid == MAX77888_CHARGER) {
  207. if (selector <= 3)
  208. selector = 0;
  209. else
  210. selector -= 3;
  211. }
  212. val = desc->min + desc->step * selector;
  213. if (val > desc->max)
  214. return -EINVAL;
  215. return val * 1000;
  216. }
  217. static int max77888_get_voltage(struct regulator_dev *rdev)
  218. {
  219. struct max77888_data *max77888 = rdev_get_drvdata(rdev);
  220. struct i2c_client *i2c = max77888->iodev->i2c;
  221. int reg, shift, mask, ret;
  222. u8 val;
  223. dev_info(&rdev->dev, "func:%s\n", __func__);
  224. ret = max77888_get_voltage_register(rdev, &reg, &shift, &mask);
  225. if (ret)
  226. return ret;
  227. ret = max77888_read_reg(i2c, reg, &val);
  228. if (ret)
  229. return ret;
  230. val >>= shift;
  231. val &= mask;
  232. if (rdev->desc && rdev->desc->ops && rdev->desc->ops->list_voltage)
  233. return rdev->desc->ops->list_voltage(rdev, val);
  234. /*
  235. * max77888_list_voltage returns value for any rdev with voltage_map,
  236. * which works for "CHARGER" and "CHARGER TOPOFF" that do not have
  237. * list_voltage ops (they are current regulators).
  238. */
  239. return max77888_list_voltage(rdev, val);
  240. }
  241. static inline int max77888_get_voltage_proper_val(
  242. const struct voltage_map_desc *desc,
  243. int min_vol, int max_vol)
  244. {
  245. int i = 0;
  246. if (desc == NULL)
  247. return -EINVAL;
  248. if (max_vol < desc->min || min_vol > desc->max)
  249. return -EINVAL;
  250. while (desc->min + desc->step * i < min_vol &&
  251. desc->min + desc->step * i < desc->max)
  252. i++;
  253. if (desc->min + desc->step * i > max_vol)
  254. return -EINVAL;
  255. if (i >= (1 << desc->n_bits))
  256. return -EINVAL;
  257. return i;
  258. }
  259. static int max77888_set_voltage(struct regulator_dev *rdev,
  260. int min_uV, int max_uV)
  261. {
  262. struct max77888_data *max77888 = rdev_get_drvdata(rdev);
  263. struct i2c_client *i2c = max77888->iodev->i2c;
  264. int min_vol = min_uV / 1000, max_vol = max_uV / 1000;
  265. const struct voltage_map_desc *desc;
  266. int rid = max77888_get_rid(rdev);
  267. int reg, shift = 0, mask, ret;
  268. int i;
  269. u8 org;
  270. switch (rid) {
  271. case MAX77888_CHARGER:
  272. break;
  273. default:
  274. return -EINVAL;
  275. }
  276. desc = reg_voltage_map[rid];
  277. i = max77888_get_voltage_proper_val(desc, min_vol, max_vol);
  278. if (i < 0)
  279. return i;
  280. ret = max77888_get_voltage_register(rdev, &reg, &shift, &mask);
  281. if (ret)
  282. return ret;
  283. max77888_read_reg(i2c, reg, &org);
  284. org = (org & mask) >> shift;
  285. /* the first four codes for charger current are all 60mA */
  286. if (rid == MAX77888_CHARGER)
  287. i += 3;
  288. ret = max77888_update_reg(i2c, reg, i << shift, mask << shift);
  289. return ret;
  290. }
  291. static const int safeoutvolt[] = {
  292. 3300000,
  293. 4850000,
  294. 4900000,
  295. 4950000,
  296. };
  297. /* For SAFEOUT1 and SAFEOUT2 */
  298. static int max77888_set_voltage_safeout(struct regulator_dev *rdev,
  299. int min_uV, int max_uV,
  300. unsigned *selector)
  301. {
  302. struct max77888_data *max77888 = rdev_get_drvdata(rdev);
  303. struct i2c_client *i2c = max77888->iodev->i2c;
  304. int rid = max77888_get_rid(rdev);
  305. int reg, shift = 0, mask, ret;
  306. int i = 0;
  307. u8 val;
  308. dev_info(&rdev->dev, "func:%s\n", __func__);
  309. if (rid != MAX77888_ESAFEOUT1 && rid != MAX77888_ESAFEOUT2)
  310. return -EINVAL;
  311. for (i = 0; i < ARRAY_SIZE(safeoutvolt); i++) {
  312. if (min_uV <= safeoutvolt[i] && max_uV >= safeoutvolt[i])
  313. break;
  314. }
  315. if (i >= ARRAY_SIZE(safeoutvolt))
  316. return -EINVAL;
  317. if (i == 0)
  318. val = 0x3;
  319. else
  320. val = i - 1;
  321. ret = max77888_get_voltage_register(rdev, &reg, &shift, &mask);
  322. if (ret)
  323. return ret;
  324. ret = max77888_update_reg(i2c, reg, val << shift, mask << shift);
  325. *selector = val;
  326. return ret;
  327. }
  328. static int max77888_reg_enable_suspend(struct regulator_dev *rdev)
  329. {
  330. dev_info(&rdev->dev, "func:%s\n", __func__);
  331. return 0;
  332. }
  333. static int max77888_reg_disable_suspend(struct regulator_dev *rdev)
  334. {
  335. struct max77888_data *max77888 = rdev_get_drvdata(rdev);
  336. struct i2c_client *i2c = max77888->iodev->i2c;
  337. int ret, reg, mask, pattern;
  338. int rid = max77888_get_rid(rdev);
  339. dev_info(&rdev->dev, "func:%s\n", __func__);
  340. ret = max77888_get_disable_register(rdev, &reg, &mask, &pattern);
  341. if (ret)
  342. return ret;
  343. max77888_read_reg(i2c, reg, &max77888->saved_states[rid]);
  344. dev_dbg(&rdev->dev, "Full Power-Off for %s (%xh -> %xh)\n",
  345. rdev->desc->name, max77888->saved_states[rid] & mask,
  346. (~pattern) & mask);
  347. return max77888_update_reg(i2c, reg, ~pattern, mask);
  348. }
  349. static struct regulator_ops max77888_safeout_ops = {
  350. .list_voltage = max77888_list_voltage_safeout,
  351. .is_enabled = max77888_reg_is_enabled,
  352. .enable = max77888_reg_enable,
  353. .disable = max77888_reg_disable,
  354. .get_voltage = max77888_get_voltage,
  355. .set_voltage = max77888_set_voltage_safeout,
  356. .set_suspend_enable = max77888_reg_enable_suspend,
  357. .set_suspend_disable = max77888_reg_disable_suspend,
  358. };
  359. static struct regulator_ops max77888_charger_ops = {
  360. .is_enabled = max77888_reg_is_enabled,
  361. .enable = max77888_reg_enable,
  362. .disable = max77888_reg_disable,
  363. .get_current_limit = max77888_get_voltage,
  364. .set_current_limit = max77888_set_voltage,
  365. };
  366. static struct regulator_desc regulators[] = {
  367. {
  368. .name = "ESAFEOUT1",
  369. .id = MAX77888_ESAFEOUT1,
  370. .ops = &max77888_safeout_ops,
  371. .type = REGULATOR_VOLTAGE,
  372. .owner = THIS_MODULE,
  373. }, {
  374. .name = "ESAFEOUT2",
  375. .id = MAX77888_ESAFEOUT2,
  376. .ops = &max77888_safeout_ops,
  377. .type = REGULATOR_VOLTAGE,
  378. .owner = THIS_MODULE,
  379. }, {
  380. .name = "CHARGER",
  381. .id = MAX77888_CHARGER,
  382. .ops = &max77888_charger_ops,
  383. .type = REGULATOR_CURRENT,
  384. .owner = THIS_MODULE,
  385. }
  386. };
  387. static __devinit int max77888_pmic_probe(struct platform_device *pdev)
  388. {
  389. struct max77888_dev *iodev = dev_get_drvdata(pdev->dev.parent);
  390. struct max77888_platform_data *pdata = dev_get_platdata(iodev->dev);
  391. struct regulator_dev **rdev;
  392. struct max77888_data *max77888;
  393. struct i2c_client *i2c;
  394. int i, ret, size;
  395. dev_info(&pdev->dev, "%s\n", __func__);
  396. if (!pdata) {
  397. pr_info("[%s:%d] !pdata\n", __FILE__, __LINE__);
  398. dev_err(pdev->dev.parent, "No platform init data supplied.\n");
  399. return -ENODEV;
  400. }
  401. max77888 = kzalloc(sizeof(struct max77888_data), GFP_KERNEL);
  402. if (!max77888) {
  403. pr_info("[%s:%d] if (!max77888)\n", __FILE__, __LINE__);
  404. return -ENOMEM;
  405. }
  406. size = sizeof(struct regulator_dev *) * pdata->num_regulators;
  407. max77888->rdev = kzalloc(size, GFP_KERNEL);
  408. if (!max77888->rdev) {
  409. pr_info("[%s:%d] if (!max77888->rdev)\n", __FILE__, __LINE__);
  410. kfree(max77888);
  411. return -ENOMEM;
  412. }
  413. rdev = max77888->rdev;
  414. max77888->dev = &pdev->dev;
  415. max77888->iodev = iodev;
  416. max77888->num_regulators = pdata->num_regulators;
  417. platform_set_drvdata(pdev, max77888);
  418. i2c = max77888->iodev->i2c;
  419. pr_info("[%s:%d] pdata->num_regulators:%d\n", __FILE__, __LINE__,
  420. pdata->num_regulators);
  421. for (i = 0; i < pdata->num_regulators; i++) {
  422. const struct voltage_map_desc *desc;
  423. int id = pdata->regulators[i].id;
  424. pr_info("[%s:%d] for in pdata->num_regulators:%d\n", __FILE__,
  425. __LINE__, pdata->num_regulators);
  426. desc = reg_voltage_map[id];
  427. if (id == MAX77888_ESAFEOUT1 || id == MAX77888_ESAFEOUT2)
  428. regulators[id].n_voltages = 4;
  429. rdev[i] = regulator_register(&regulators[id], max77888->dev,
  430. pdata->regulators[i].initdata,
  431. max77888, NULL);
  432. if (IS_ERR(rdev[i])) {
  433. ret = PTR_ERR(rdev[i]);
  434. dev_err(max77888->dev, "regulator init failed for %d\n",
  435. id);
  436. rdev[i] = NULL;
  437. goto err;
  438. }
  439. }
  440. return 0;
  441. err:
  442. pr_info("[%s:%d] err:\n", __FILE__, __LINE__);
  443. for (i = 0; i < max77888->num_regulators; i++)
  444. if (rdev[i])
  445. regulator_unregister(rdev[i]);
  446. /* err_alloc: unused label */
  447. /* pr_info("[%s:%d] err_alloc\n", __FILE__, __LINE__); */
  448. kfree(max77888->rdev);
  449. kfree(max77888);
  450. return ret;
  451. }
  452. static int __devexit max77888_pmic_remove(struct platform_device *pdev)
  453. {
  454. struct max77888_data *max77888 = platform_get_drvdata(pdev);
  455. struct regulator_dev **rdev = max77888->rdev;
  456. int i;
  457. dev_info(&pdev->dev, "%s\n", __func__);
  458. for (i = 0; i < max77888->num_regulators; i++)
  459. if (rdev[i])
  460. regulator_unregister(rdev[i]);
  461. kfree(max77888->rdev);
  462. kfree(max77888);
  463. return 0;
  464. }
  465. static const struct platform_device_id max77888_pmic_id[] = {
  466. {"max77888-safeout", 0},
  467. {},
  468. };
  469. MODULE_DEVICE_TABLE(platform, max77888_pmic_id);
  470. static struct platform_driver max77888_pmic_driver = {
  471. .driver = {
  472. .name = "max77888-safeout",
  473. .owner = THIS_MODULE,
  474. },
  475. .probe = max77888_pmic_probe,
  476. .remove = __devexit_p(max77888_pmic_remove),
  477. .id_table = max77888_pmic_id,
  478. };
  479. static int __init max77888_pmic_init(void)
  480. {
  481. return platform_driver_register(&max77888_pmic_driver);
  482. }
  483. subsys_initcall(max77888_pmic_init);
  484. static void __exit max77888_pmic_cleanup(void)
  485. {
  486. platform_driver_unregister(&max77888_pmic_driver);
  487. }
  488. module_exit(max77888_pmic_cleanup);
  489. MODULE_DESCRIPTION("MAXIM 77888 Regulator Driver");
  490. MODULE_AUTHOR("Sukdong Kim <Sukdong.Kim@samsung.com>");
  491. MODULE_LICENSE("GPL");