max77803-regulator.c 14 KB

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