max14577.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. * max14577.c - Regulator driver for the Maxim 14577
  3. *
  4. * Copyright (C) 2013 Samsung Electronics
  5. * Krzysztof Kozlowski <k.kozlowski@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. #include <linux/module.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/regulator/driver.h>
  20. #include <linux/mfd/max14577.h>
  21. #include <linux/mfd/max14577-private.h>
  22. #include <linux/regulator/of_regulator.h>
  23. struct max14577_regulator {
  24. struct device *dev;
  25. struct max14577 *max14577;
  26. struct regulator_dev **regulators;
  27. };
  28. static int max14577_reg_is_enabled(struct regulator_dev *rdev)
  29. {
  30. int rid = rdev_get_id(rdev);
  31. struct regmap *rmap = rdev->regmap;
  32. u8 reg_data;
  33. switch (rid) {
  34. case MAX14577_CHARGER:
  35. max14577_read_reg(rmap, MAX14577_CHG_REG_CHG_CTRL2, &reg_data);
  36. if ((reg_data & CHGCTRL2_MBCHOSTEN_MASK) == 0)
  37. return 0;
  38. max14577_read_reg(rmap, MAX14577_CHG_REG_STATUS3, &reg_data);
  39. if ((reg_data & STATUS3_CGMBC_MASK) == 0)
  40. return 0;
  41. /* MBCHOSTEN and CGMBC are on */
  42. return 1;
  43. default:
  44. return -EINVAL;
  45. }
  46. }
  47. static int max14577_reg_get_current_limit(struct regulator_dev *rdev)
  48. {
  49. u8 reg_data;
  50. struct regmap *rmap = rdev->regmap;
  51. if (rdev_get_id(rdev) != MAX14577_CHARGER)
  52. return -EINVAL;
  53. max14577_read_reg(rmap, MAX14577_CHG_REG_CHG_CTRL4, &reg_data);
  54. if ((reg_data & CHGCTRL4_MBCICHWRCL_MASK) == 0)
  55. return MAX14577_REGULATOR_CURRENT_LIMIT_MIN;
  56. reg_data = ((reg_data & CHGCTRL4_MBCICHWRCH_MASK) >>
  57. CHGCTRL4_MBCICHWRCH_SHIFT);
  58. return MAX14577_REGULATOR_CURRENT_LIMIT_HIGH_START +
  59. reg_data * MAX14577_REGULATOR_CURRENT_LIMIT_HIGH_STEP;
  60. }
  61. static int max14577_reg_set_current_limit(struct regulator_dev *rdev,
  62. int min_uA, int max_uA)
  63. {
  64. int i, current_bits = 0xf;
  65. u8 reg_data;
  66. if (rdev_get_id(rdev) != MAX14577_CHARGER)
  67. return -EINVAL;
  68. if (min_uA > MAX14577_REGULATOR_CURRENT_LIMIT_MAX ||
  69. max_uA < MAX14577_REGULATOR_CURRENT_LIMIT_MIN)
  70. return -EINVAL;
  71. if (max_uA < MAX14577_REGULATOR_CURRENT_LIMIT_HIGH_START) {
  72. /* Less than 200 mA, so set 90mA (turn only Low Bit off) */
  73. u8 reg_data = 0x0 << CHGCTRL4_MBCICHWRCL_SHIFT;
  74. return max14577_update_reg(rdev->regmap,
  75. MAX14577_CHG_REG_CHG_CTRL4,
  76. CHGCTRL4_MBCICHWRCL_MASK, reg_data);
  77. }
  78. /* max_uA is in range: <LIMIT_HIGH_START, inifinite>, so search for
  79. * valid current starting from LIMIT_MAX. */
  80. for (i = MAX14577_REGULATOR_CURRENT_LIMIT_MAX;
  81. i >= MAX14577_REGULATOR_CURRENT_LIMIT_HIGH_START;
  82. i -= MAX14577_REGULATOR_CURRENT_LIMIT_HIGH_STEP) {
  83. if (i <= max_uA)
  84. break;
  85. current_bits--;
  86. }
  87. BUG_ON(current_bits < 0); /* Cannot happen */
  88. /* Turn Low Bit on (use range 200mA-950 mA) */
  89. reg_data = 0x1 << CHGCTRL4_MBCICHWRCL_SHIFT;
  90. /* and set proper High Bits */
  91. reg_data |= current_bits << CHGCTRL4_MBCICHWRCH_SHIFT;
  92. return max14577_update_reg(rdev->regmap, MAX14577_CHG_REG_CHG_CTRL4,
  93. CHGCTRL4_MBCICHWRCL_MASK | CHGCTRL4_MBCICHWRCH_MASK,
  94. reg_data);
  95. }
  96. static struct regulator_ops max14577_safeout_ops = {
  97. .is_enabled = regulator_is_enabled_regmap,
  98. .enable = regulator_enable_regmap,
  99. .disable = regulator_disable_regmap,
  100. .list_voltage = regulator_list_voltage_linear,
  101. };
  102. static struct regulator_ops max14577_charger_ops = {
  103. .is_enabled = max14577_reg_is_enabled,
  104. .enable = regulator_enable_regmap,
  105. .disable = regulator_disable_regmap,
  106. .get_current_limit = max14577_reg_get_current_limit,
  107. .set_current_limit = max14577_reg_set_current_limit,
  108. };
  109. static const struct regulator_desc supported_regulators[] = {
  110. [MAX14577_SAFEOUT] = {
  111. .name = "SAFEOUT",
  112. .id = MAX14577_SAFEOUT,
  113. .ops = &max14577_safeout_ops,
  114. .type = REGULATOR_VOLTAGE,
  115. .owner = THIS_MODULE,
  116. .n_voltages = 1,
  117. .min_uV = MAX14577_REGULATOR_SAFEOUT_VOLTAGE,
  118. .enable_reg = MAX14577_REG_CONTROL2,
  119. .enable_mask = CTRL2_SFOUTORD_MASK,
  120. },
  121. [MAX14577_CHARGER] = {
  122. .name = "CHARGER",
  123. .id = MAX14577_CHARGER,
  124. .ops = &max14577_charger_ops,
  125. .type = REGULATOR_CURRENT,
  126. .owner = THIS_MODULE,
  127. .enable_reg = MAX14577_CHG_REG_CHG_CTRL2,
  128. .enable_mask = CHGCTRL2_MBCHOSTEN_MASK,
  129. },
  130. };
  131. #ifdef CONFIG_OF
  132. static struct of_regulator_match max14577_regulator_matches[] = {
  133. { .name = "SAFEOUT", },
  134. { .name = "CHARGER", },
  135. };
  136. static int max14577_regulator_dt_parse_pdata(struct platform_device *pdev)
  137. {
  138. int ret;
  139. struct device_node *np;
  140. np = of_get_child_by_name(pdev->dev.parent->of_node, "regulators");
  141. if (!np) {
  142. dev_err(&pdev->dev, "Failed to get child OF node for regulators\n");
  143. return -EINVAL;
  144. }
  145. ret = of_regulator_match(&pdev->dev, np, max14577_regulator_matches,
  146. MAX14577_REG_MAX);
  147. if (ret < 0)
  148. dev_err(&pdev->dev, "Error parsing regulator init data: %d\n", ret);
  149. else
  150. ret = 0;
  151. of_node_put(np);
  152. return ret;
  153. }
  154. static inline struct regulator_init_data *match_init_data(int index)
  155. {
  156. return max14577_regulator_matches[index].init_data;
  157. }
  158. static inline struct device_node *match_of_node(int index)
  159. {
  160. return max14577_regulator_matches[index].of_node;
  161. }
  162. #else /* CONFIG_OF */
  163. static int max14577_regulator_dt_parse_pdata(struct platform_device *pdev)
  164. {
  165. return 0;
  166. }
  167. static inline struct regulator_init_data *match_init_data(int index)
  168. {
  169. return NULL;
  170. }
  171. static inline struct device_node *match_of_node(int index)
  172. {
  173. return NULL;
  174. }
  175. #endif /* CONFIG_OF */
  176. static int max14577_regulator_probe(struct platform_device *pdev)
  177. {
  178. struct max14577 *max14577 = dev_get_drvdata(pdev->dev.parent);
  179. struct max14577_platform_data *pdata = dev_get_platdata(max14577->dev);
  180. int i, ret;
  181. struct regulator_config config = {};
  182. ret = max14577_regulator_dt_parse_pdata(pdev);
  183. if (ret)
  184. return ret;
  185. config.dev = &pdev->dev;
  186. config.regmap = max14577->regmap;
  187. for (i = 0; i < ARRAY_SIZE(supported_regulators); i++) {
  188. struct regulator_dev *regulator;
  189. /*
  190. * Index of supported_regulators[] is also the id and must
  191. * match index of pdata->regulators[].
  192. */
  193. if (pdata && pdata->regulators) {
  194. config.init_data = pdata->regulators[i].initdata;
  195. config.of_node = pdata->regulators[i].of_node;
  196. } else {
  197. config.init_data = match_init_data(i);
  198. config.of_node = match_of_node(i);
  199. }
  200. regulator = devm_regulator_register(&pdev->dev,
  201. &supported_regulators[i], &config);
  202. if (IS_ERR(regulator)) {
  203. ret = PTR_ERR(regulator);
  204. dev_err(&pdev->dev,
  205. "Regulator init failed for ID %d with error: %d\n",
  206. i, ret);
  207. return ret;
  208. }
  209. }
  210. return ret;
  211. }
  212. static struct platform_driver max14577_regulator_driver = {
  213. .driver = {
  214. .owner = THIS_MODULE,
  215. .name = "max14577-regulator",
  216. },
  217. .probe = max14577_regulator_probe,
  218. };
  219. static int __init max14577_regulator_init(void)
  220. {
  221. BUILD_BUG_ON(MAX14577_REGULATOR_CURRENT_LIMIT_HIGH_START +
  222. MAX14577_REGULATOR_CURRENT_LIMIT_HIGH_STEP * 0xf !=
  223. MAX14577_REGULATOR_CURRENT_LIMIT_MAX);
  224. BUILD_BUG_ON(ARRAY_SIZE(supported_regulators) != MAX14577_REG_MAX);
  225. return platform_driver_register(&max14577_regulator_driver);
  226. }
  227. subsys_initcall(max14577_regulator_init);
  228. static void __exit max14577_regulator_exit(void)
  229. {
  230. platform_driver_unregister(&max14577_regulator_driver);
  231. }
  232. module_exit(max14577_regulator_exit);
  233. MODULE_AUTHOR("Krzysztof Kozlowski <k.kozlowski@samsung.com>");
  234. MODULE_DESCRIPTION("MAXIM 14577 regulator driver");
  235. MODULE_LICENSE("GPL");
  236. MODULE_ALIAS("platform:max14577-regulator");