max77823-regulator.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. /*
  2. * max77823-regulator.c - Regulator driver for the Maxim 77804k
  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/max77823.h>
  32. #include <linux/mfd/max77823-private.h>
  33. struct max77823_data {
  34. struct device *dev;
  35. struct max77823_dev *iodev;
  36. int num_regulators;
  37. struct regulator_dev **rdev;
  38. u8 saved_states[MAX77823_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. [MAX77823_ESAFEOUT1] = NULL,
  55. [MAX77823_ESAFEOUT2] = NULL,
  56. [MAX77823_CHARGER] = &charger_current_map_desc,
  57. };
  58. static inline int max77823_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 max77823_list_voltage_safeout(struct regulator_dev *rdev,
  64. unsigned int selector)
  65. {
  66. int rid = max77823_get_rid(rdev);
  67. dev_info(&rdev->dev, "func:%s\n", __func__);
  68. if (rid == MAX77823_ESAFEOUT1 || rid == MAX77823_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 max77823_get_enable_register(struct regulator_dev *rdev,
  85. int *reg, int *mask, int *pattern)
  86. {
  87. int rid = max77823_get_rid(rdev);
  88. dev_dbg(&rdev->dev, "func:%s\n", __func__);
  89. dev_info(&rdev->dev, "%s\n", __func__);
  90. switch (rid) {
  91. case MAX77823_ESAFEOUT1...MAX77823_ESAFEOUT2:
  92. *reg = MAX77823_PMIC_SAFEOUT_LDO_Control;
  93. *mask = 0x40 << (rid - MAX77823_ESAFEOUT1);
  94. *pattern = 0x40 << (rid - MAX77823_ESAFEOUT1);
  95. break;
  96. case MAX77823_CHARGER:
  97. *reg = MAX77823_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 max77823_get_disable_register(struct regulator_dev *rdev,
  108. int *reg, int *mask, int *pattern)
  109. {
  110. int rid = max77823_get_rid(rdev);
  111. switch (rid) {
  112. case MAX77823_ESAFEOUT1...MAX77823_ESAFEOUT2:
  113. *reg = MAX77823_PMIC_SAFEOUT_LDO_Control;
  114. *mask = 0x40 << (rid - MAX77823_ESAFEOUT1);
  115. *pattern = 0x00;
  116. break;
  117. case MAX77823_CHARGER:
  118. *reg = MAX77823_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 max77823_reg_is_enabled(struct regulator_dev *rdev)
  129. {
  130. struct max77823_data *max77823 = rdev_get_drvdata(rdev);
  131. struct i2c_client *i2c = max77823->iodev->i2c;
  132. int ret, reg, mask, pattern;
  133. u8 val;
  134. dev_dbg(&rdev->dev, "func:%s\n", __func__);
  135. ret = max77823_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 = max77823_read_reg(i2c, reg, &val);
  141. if (ret)
  142. return ret;
  143. return (val & mask) == pattern;
  144. }
  145. static int max77823_reg_enable(struct regulator_dev *rdev)
  146. {
  147. struct max77823_data *max77823 = rdev_get_drvdata(rdev);
  148. struct i2c_client *i2c = max77823->iodev->i2c;
  149. int ret, reg, mask, pattern;
  150. ret = max77823_get_enable_register(rdev, &reg, &mask, &pattern);
  151. if (ret)
  152. return ret;
  153. return max77823_update_reg(i2c, reg, pattern, mask);
  154. }
  155. static int max77823_reg_disable(struct regulator_dev *rdev)
  156. {
  157. struct max77823_data *max77823 = rdev_get_drvdata(rdev);
  158. struct i2c_client *i2c = max77823->iodev->i2c;
  159. int ret, reg, mask, pattern;
  160. ret = max77823_get_disable_register(rdev, &reg, &mask, &pattern);
  161. if (ret)
  162. return ret;
  163. return max77823_update_reg(i2c, reg, pattern, mask);
  164. }
  165. static int max77823_get_voltage_register(struct regulator_dev *rdev,
  166. int *_reg, int *_shift, int *_mask)
  167. {
  168. int rid = max77823_get_rid(rdev);
  169. int reg, shift = 0, mask = 0x3f;
  170. switch (rid) {
  171. case MAX77823_ESAFEOUT1...MAX77823_ESAFEOUT2:
  172. reg = MAX77823_PMIC_SAFEOUT_LDO_Control;
  173. shift = (rid == MAX77823_ESAFEOUT2) ? 2 : 0;
  174. mask = 0x3;
  175. break;
  176. case MAX77823_CHARGER:
  177. reg = MAX77823_CHG_CNFG_09;
  178. shift = 0;
  179. mask = 0x7f;
  180. break;
  181. default:
  182. return -EINVAL;
  183. }
  184. *_reg = reg;
  185. *_shift = shift;
  186. *_mask = mask;
  187. return 0;
  188. }
  189. static int max77823_list_voltage(struct regulator_dev *rdev,
  190. unsigned int selector)
  191. {
  192. const struct voltage_map_desc *desc;
  193. int rid = max77823_get_rid(rdev);
  194. int val;
  195. dev_info(&rdev->dev, "func:%s\n", __func__);
  196. if (rid >= ARRAY_SIZE(reg_voltage_map) || rid < 0)
  197. return -EINVAL;
  198. desc = reg_voltage_map[rid];
  199. if (desc == NULL)
  200. return -EINVAL;
  201. /* the first four codes for charger current are all 60mA */
  202. if (rid == MAX77823_CHARGER) {
  203. if (selector <= 3)
  204. selector = 0;
  205. else
  206. selector -= 3;
  207. }
  208. val = desc->min + desc->step * selector;
  209. if (val > desc->max)
  210. return -EINVAL;
  211. return val * 1000;
  212. }
  213. static int max77823_get_voltage(struct regulator_dev *rdev)
  214. {
  215. struct max77823_data *max77823 = rdev_get_drvdata(rdev);
  216. struct i2c_client *i2c = max77823->iodev->i2c;
  217. int reg, shift, mask, ret;
  218. u8 val;
  219. dev_info(&rdev->dev, "func:%s\n", __func__);
  220. ret = max77823_get_voltage_register(rdev, &reg, &shift, &mask);
  221. if (ret)
  222. return ret;
  223. ret = max77823_read_reg(i2c, reg, &val);
  224. if (ret)
  225. return ret;
  226. val >>= shift;
  227. val &= mask;
  228. if (rdev->desc && rdev->desc->ops && rdev->desc->ops->list_voltage)
  229. return rdev->desc->ops->list_voltage(rdev, val);
  230. /*
  231. * max77823_list_voltage returns value for any rdev with voltage_map,
  232. * which works for "CHARGER" and "CHARGER TOPOFF" that do not have
  233. * list_voltage ops (they are current regulators).
  234. */
  235. return max77823_list_voltage(rdev, val);
  236. }
  237. static inline int max77823_get_voltage_proper_val(
  238. const struct voltage_map_desc *desc,
  239. int min_vol, int max_vol)
  240. {
  241. int i = 0;
  242. printk("%s\n", __func__);
  243. if (desc == NULL)
  244. return -EINVAL;
  245. if (max_vol < desc->min || min_vol > desc->max)
  246. return -EINVAL;
  247. while (desc->min + desc->step * i < min_vol &&
  248. desc->min + desc->step * i < desc->max)
  249. i++;
  250. if (desc->min + desc->step * i > max_vol)
  251. return -EINVAL;
  252. if (i >= (1 << desc->n_bits))
  253. return -EINVAL;
  254. return i;
  255. }
  256. static int max77823_set_voltage(struct regulator_dev *rdev,
  257. int min_uV, int max_uV)
  258. {
  259. struct max77823_data *max77823 = rdev_get_drvdata(rdev);
  260. struct i2c_client *i2c = max77823->iodev->i2c;
  261. int min_vol = min_uV / 1000, max_vol = max_uV / 1000;
  262. const struct voltage_map_desc *desc;
  263. int rid = max77823_get_rid(rdev);
  264. int reg, shift = 0, mask, ret;
  265. int i;
  266. u8 org;
  267. dev_info(&rdev->dev, "%s\n", __func__);
  268. switch (rid) {
  269. case MAX77823_CHARGER:
  270. break;
  271. default:
  272. return -EINVAL;
  273. }
  274. desc = reg_voltage_map[rid];
  275. i = max77823_get_voltage_proper_val(desc, min_vol, max_vol);
  276. if (i < 0)
  277. return i;
  278. ret = max77823_get_voltage_register(rdev, &reg, &shift, &mask);
  279. if (ret)
  280. return ret;
  281. max77823_read_reg(i2c, reg, &org);
  282. org = (org & mask) >> shift;
  283. /* the first four codes for charger current are all 60mA */
  284. if (rid == MAX77823_CHARGER)
  285. i += 3;
  286. ret = max77823_update_reg(i2c, reg, i << shift, mask << shift);
  287. return ret;
  288. }
  289. static const int safeoutvolt[] = {
  290. 3300000,
  291. 4850000,
  292. 4900000,
  293. 4950000,
  294. };
  295. /* For SAFEOUT1 and SAFEOUT2 */
  296. static int max77823_set_voltage_safeout(struct regulator_dev *rdev,
  297. int min_uV, int max_uV,
  298. unsigned *selector)
  299. {
  300. struct max77823_data *max77823 = rdev_get_drvdata(rdev);
  301. struct i2c_client *i2c = max77823->iodev->i2c;
  302. int rid = max77823_get_rid(rdev);
  303. int reg, shift = 0, mask, ret;
  304. int i = 0;
  305. u8 val;
  306. dev_info(&rdev->dev, "func:%s\n", __func__);
  307. if (rid != MAX77823_ESAFEOUT1 && rid != MAX77823_ESAFEOUT2)
  308. return -EINVAL;
  309. for (i = 0; i < ARRAY_SIZE(safeoutvolt); i++) {
  310. if (min_uV <= safeoutvolt[i] && max_uV >= safeoutvolt[i])
  311. break;
  312. }
  313. if (i >= ARRAY_SIZE(safeoutvolt))
  314. return -EINVAL;
  315. if (i == 0)
  316. val = 0x3;
  317. else
  318. val = i - 1;
  319. ret = max77823_get_voltage_register(rdev, &reg, &shift, &mask);
  320. if (ret)
  321. return ret;
  322. ret = max77823_update_reg(i2c, reg, val << shift, mask << shift);
  323. *selector = val;
  324. return ret;
  325. }
  326. static int max77823_reg_enable_suspend(struct regulator_dev *rdev)
  327. {
  328. dev_info(&rdev->dev, "func:%s\n", __func__);
  329. return 0;
  330. }
  331. static int max77823_reg_disable_suspend(struct regulator_dev *rdev)
  332. {
  333. struct max77823_data *max77823 = rdev_get_drvdata(rdev);
  334. struct i2c_client *i2c = max77823->iodev->i2c;
  335. int ret, reg, mask, pattern;
  336. int rid = max77823_get_rid(rdev);
  337. dev_info(&rdev->dev, "func:%s\n", __func__);
  338. ret = max77823_get_disable_register(rdev, &reg, &mask, &pattern);
  339. if (ret)
  340. return ret;
  341. max77823_read_reg(i2c, reg, &max77823->saved_states[rid]);
  342. dev_dbg(&rdev->dev, "Full Power-Off for %s (%xh -> %xh)\n",
  343. rdev->desc->name, max77823->saved_states[rid] & mask,
  344. (~pattern) & mask);
  345. return max77823_update_reg(i2c, reg, pattern, mask);
  346. }
  347. static struct regulator_ops max77823_safeout_ops = {
  348. .list_voltage = max77823_list_voltage_safeout,
  349. .is_enabled = max77823_reg_is_enabled,
  350. .enable = max77823_reg_enable,
  351. .disable = max77823_reg_disable,
  352. .get_voltage = max77823_get_voltage,
  353. .set_voltage = max77823_set_voltage_safeout,
  354. .set_suspend_enable = max77823_reg_enable_suspend,
  355. .set_suspend_disable = max77823_reg_disable_suspend,
  356. };
  357. static struct regulator_ops max77823_charger_ops = {
  358. .is_enabled = max77823_reg_is_enabled,
  359. .enable = max77823_reg_enable,
  360. .disable = max77823_reg_disable,
  361. .get_current_limit = max77823_get_voltage,
  362. .set_current_limit = max77823_set_voltage,
  363. };
  364. static struct regulator_desc regulators[] = {
  365. {
  366. .name = "ESAFEOUT1",
  367. .id = MAX77823_ESAFEOUT1,
  368. .ops = &max77823_safeout_ops,
  369. .type = REGULATOR_VOLTAGE,
  370. .owner = THIS_MODULE,
  371. }, {
  372. .name = "ESAFEOUT2",
  373. .id = MAX77823_ESAFEOUT2,
  374. .ops = &max77823_safeout_ops,
  375. .type = REGULATOR_VOLTAGE,
  376. .owner = THIS_MODULE,
  377. }, {
  378. .name = "CHARGER",
  379. .id = MAX77823_CHARGER,
  380. .ops = &max77823_charger_ops,
  381. .type = REGULATOR_CURRENT,
  382. .owner = THIS_MODULE,
  383. }
  384. };
  385. static __devinit int max77823_pmic_probe(struct platform_device *pdev)
  386. {
  387. struct max77823_dev *iodev = dev_get_drvdata(pdev->dev.parent);
  388. struct max77823_platform_data *pdata = dev_get_platdata(iodev->dev);
  389. struct regulator_dev **rdev;
  390. struct max77823_data *max77823;
  391. struct i2c_client *i2c;
  392. int i, ret, size;
  393. dev_info(&pdev->dev, "%s\n", __func__);
  394. printk("%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. max77823 = kzalloc(sizeof(struct max77823_data), GFP_KERNEL);
  401. if (!max77823) {
  402. pr_info("[%s:%d] if (!max77823)\n", __FILE__, __LINE__);
  403. return -ENOMEM;
  404. }
  405. size = sizeof(struct regulator_dev *) * pdata->num_regulators;
  406. max77823->rdev = kzalloc(size, GFP_KERNEL);
  407. if (!max77823->rdev) {
  408. pr_info("[%s:%d] if (!max77823->rdev)\n", __FILE__, __LINE__);
  409. kfree(max77823);
  410. return -ENOMEM;
  411. }
  412. rdev = max77823->rdev;
  413. max77823->dev = &pdev->dev;
  414. max77823->iodev = iodev;
  415. max77823->num_regulators = pdata->num_regulators;
  416. platform_set_drvdata(pdev, max77823);
  417. i2c = max77823->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->regulator[%d].id :%d\n", __FILE__,
  424. __LINE__, i, id);
  425. pr_info("[%s:%d] for in pdata->num_regulators:%d\n", __FILE__,
  426. __LINE__, pdata->num_regulators);
  427. desc = reg_voltage_map[id];
  428. if (id == MAX77823_ESAFEOUT1 || id == MAX77823_ESAFEOUT2)
  429. regulators[id].n_voltages = 4;
  430. rdev[i] = regulator_register(&regulators[id], max77823->dev,
  431. pdata->regulators[i].initdata,
  432. max77823, NULL);
  433. if (IS_ERR(rdev[i])) {
  434. ret = PTR_ERR(rdev[i]);
  435. dev_err(max77823->dev, "regulator init failed for %d\n",
  436. id);
  437. rdev[i] = NULL;
  438. goto err;
  439. }
  440. }
  441. return 0;
  442. err:
  443. pr_info("[%s:%d] err:\n", __FILE__, __LINE__);
  444. for (i = 0; i < max77823->num_regulators; i++)
  445. if (rdev[i])
  446. regulator_unregister(rdev[i]);
  447. pr_info("[%s:%d] err_alloc\n", __FILE__, __LINE__);
  448. kfree(max77823->rdev);
  449. kfree(max77823);
  450. return ret;
  451. }
  452. static int __devexit max77823_pmic_remove(struct platform_device *pdev)
  453. {
  454. struct max77823_data *max77823 = platform_get_drvdata(pdev);
  455. struct regulator_dev **rdev = max77823->rdev;
  456. int i;
  457. dev_info(&pdev->dev, "%s\n", __func__);
  458. for (i = 0; i < max77823->num_regulators; i++)
  459. if (rdev[i])
  460. regulator_unregister(rdev[i]);
  461. kfree(max77823->rdev);
  462. kfree(max77823);
  463. return 0;
  464. }
  465. static const struct platform_device_id max77823_pmic_id[] = {
  466. {"max77823-safeout", 0},
  467. {},
  468. };
  469. MODULE_DEVICE_TABLE(platform, max77823_pmic_id);
  470. static struct platform_driver max77823_pmic_driver = {
  471. .driver = {
  472. .name = "max77823-safeout",
  473. .owner = THIS_MODULE,
  474. },
  475. .probe = max77823_pmic_probe,
  476. .remove = __devexit_p(max77823_pmic_remove),
  477. .id_table = max77823_pmic_id,
  478. };
  479. static int __init max77823_pmic_init(void)
  480. {
  481. return platform_driver_register(&max77823_pmic_driver);
  482. }
  483. subsys_initcall(max77823_pmic_init);
  484. static void __exit max77823_pmic_cleanup(void)
  485. {
  486. platform_driver_unregister(&max77823_pmic_driver);
  487. }
  488. module_exit(max77823_pmic_cleanup);
  489. MODULE_DESCRIPTION("MAXIM 77823 Regulator Driver");
  490. MODULE_AUTHOR("SAMSUNG ELECTRONICS>");
  491. MODULE_LICENSE("GPL");