max77693-regulator.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  2. * max77693.c - Regulator driver for the Maxim 77693 and 77843
  3. *
  4. * Copyright (C) 2013-2015 Samsung Electronics
  5. * Jonghwa Lee <jonghwa3.lee@samsung.com>
  6. * Krzysztof Kozlowski <krzk@kernel.org>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. * This driver is based on max77686.c
  23. */
  24. #include <linux/err.h>
  25. #include <linux/slab.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/module.h>
  28. #include <linux/export.h>
  29. #include <linux/regulator/driver.h>
  30. #include <linux/regulator/machine.h>
  31. #include <linux/mfd/max77693.h>
  32. #include <linux/mfd/max77693-common.h>
  33. #include <linux/mfd/max77693-private.h>
  34. #include <linux/mfd/max77843-private.h>
  35. #include <linux/regulator/of_regulator.h>
  36. #include <linux/regmap.h>
  37. /*
  38. * ID for MAX77843 regulators.
  39. * There is no need for such for MAX77693.
  40. */
  41. enum max77843_regulator_type {
  42. MAX77843_SAFEOUT1 = 0,
  43. MAX77843_SAFEOUT2,
  44. MAX77843_CHARGER,
  45. MAX77843_NUM,
  46. };
  47. /* Register differences between chargers: MAX77693 and MAX77843 */
  48. struct chg_reg_data {
  49. unsigned int linear_reg;
  50. unsigned int linear_mask;
  51. unsigned int uA_step;
  52. unsigned int min_sel;
  53. };
  54. /*
  55. * MAX77693 CHARGER regulator - Min : 20mA, Max : 2580mA, step : 20mA
  56. * 0x00, 0x01, 0x2, 0x03 = 60 mA
  57. * 0x04 ~ 0x7E = (60 + (X - 3) * 20) mA
  58. * Actually for MAX77693 the driver manipulates the maximum input current,
  59. * not the fast charge current (output). This should be fixed.
  60. *
  61. * On MAX77843 the calculation formula is the same (except values).
  62. * Fortunately it properly manipulates the fast charge current.
  63. */
  64. static int max77693_chg_get_current_limit(struct regulator_dev *rdev)
  65. {
  66. const struct chg_reg_data *reg_data = rdev_get_drvdata(rdev);
  67. unsigned int chg_min_uA = rdev->constraints->min_uA;
  68. unsigned int chg_max_uA = rdev->constraints->max_uA;
  69. unsigned int reg, sel;
  70. unsigned int val;
  71. int ret;
  72. ret = regmap_read(rdev->regmap, reg_data->linear_reg, &reg);
  73. if (ret < 0)
  74. return ret;
  75. sel = reg & reg_data->linear_mask;
  76. /* the first four codes for charger current are all 60mA */
  77. if (sel <= reg_data->min_sel)
  78. sel = 0;
  79. else
  80. sel -= reg_data->min_sel;
  81. val = chg_min_uA + reg_data->uA_step * sel;
  82. if (val > chg_max_uA)
  83. return -EINVAL;
  84. return val;
  85. }
  86. static int max77693_chg_set_current_limit(struct regulator_dev *rdev,
  87. int min_uA, int max_uA)
  88. {
  89. const struct chg_reg_data *reg_data = rdev_get_drvdata(rdev);
  90. unsigned int chg_min_uA = rdev->constraints->min_uA;
  91. int sel = 0;
  92. while (chg_min_uA + reg_data->uA_step * sel < min_uA)
  93. sel++;
  94. if (chg_min_uA + reg_data->uA_step * sel > max_uA)
  95. return -EINVAL;
  96. /* the first four codes for charger current are all 60mA */
  97. sel += reg_data->min_sel;
  98. return regmap_write(rdev->regmap, reg_data->linear_reg, sel);
  99. }
  100. /* end of CHARGER regulator ops */
  101. /* Returns regmap suitable for given regulator on chosen device */
  102. static struct regmap *max77693_get_regmap(enum max77693_types type,
  103. struct max77693_dev *max77693,
  104. int reg_id)
  105. {
  106. if (type == TYPE_MAX77693)
  107. return max77693->regmap;
  108. /* Else: TYPE_MAX77843 */
  109. switch (reg_id) {
  110. case MAX77843_SAFEOUT1:
  111. case MAX77843_SAFEOUT2:
  112. return max77693->regmap;
  113. case MAX77843_CHARGER:
  114. return max77693->regmap_chg;
  115. default:
  116. return max77693->regmap;
  117. }
  118. }
  119. static const unsigned int max77693_safeout_table[] = {
  120. 4850000,
  121. 4900000,
  122. 4950000,
  123. 3300000,
  124. };
  125. static const struct regulator_ops max77693_safeout_ops = {
  126. .list_voltage = regulator_list_voltage_table,
  127. .is_enabled = regulator_is_enabled_regmap,
  128. .enable = regulator_enable_regmap,
  129. .disable = regulator_disable_regmap,
  130. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  131. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  132. };
  133. static const struct regulator_ops max77693_charger_ops = {
  134. .is_enabled = regulator_is_enabled_regmap,
  135. .enable = regulator_enable_regmap,
  136. .disable = regulator_disable_regmap,
  137. .get_current_limit = max77693_chg_get_current_limit,
  138. .set_current_limit = max77693_chg_set_current_limit,
  139. };
  140. #define max77693_regulator_desc_esafeout(_num) { \
  141. .name = "ESAFEOUT"#_num, \
  142. .id = MAX77693_ESAFEOUT##_num, \
  143. .of_match = of_match_ptr("ESAFEOUT"#_num), \
  144. .regulators_node = of_match_ptr("regulators"), \
  145. .n_voltages = 4, \
  146. .ops = &max77693_safeout_ops, \
  147. .type = REGULATOR_VOLTAGE, \
  148. .owner = THIS_MODULE, \
  149. .volt_table = max77693_safeout_table, \
  150. .vsel_reg = MAX77693_CHG_REG_SAFEOUT_CTRL, \
  151. .vsel_mask = SAFEOUT_CTRL_SAFEOUT##_num##_MASK, \
  152. .enable_reg = MAX77693_CHG_REG_SAFEOUT_CTRL, \
  153. .enable_mask = SAFEOUT_CTRL_ENSAFEOUT##_num##_MASK , \
  154. }
  155. static const struct regulator_desc max77693_supported_regulators[] = {
  156. max77693_regulator_desc_esafeout(1),
  157. max77693_regulator_desc_esafeout(2),
  158. {
  159. .name = "CHARGER",
  160. .id = MAX77693_CHARGER,
  161. .of_match = of_match_ptr("CHARGER"),
  162. .regulators_node = of_match_ptr("regulators"),
  163. .ops = &max77693_charger_ops,
  164. .type = REGULATOR_CURRENT,
  165. .owner = THIS_MODULE,
  166. .enable_reg = MAX77693_CHG_REG_CHG_CNFG_00,
  167. .enable_mask = CHG_CNFG_00_CHG_MASK |
  168. CHG_CNFG_00_BUCK_MASK,
  169. .enable_val = CHG_CNFG_00_CHG_MASK | CHG_CNFG_00_BUCK_MASK,
  170. },
  171. };
  172. static const struct chg_reg_data max77693_chg_reg_data = {
  173. .linear_reg = MAX77693_CHG_REG_CHG_CNFG_09,
  174. .linear_mask = CHG_CNFG_09_CHGIN_ILIM_MASK,
  175. .uA_step = 20000,
  176. .min_sel = 3,
  177. };
  178. #define max77843_regulator_desc_esafeout(num) { \
  179. .name = "SAFEOUT" # num, \
  180. .id = MAX77843_SAFEOUT ## num, \
  181. .ops = &max77693_safeout_ops, \
  182. .of_match = of_match_ptr("SAFEOUT" # num), \
  183. .regulators_node = of_match_ptr("regulators"), \
  184. .type = REGULATOR_VOLTAGE, \
  185. .owner = THIS_MODULE, \
  186. .n_voltages = ARRAY_SIZE(max77693_safeout_table), \
  187. .volt_table = max77693_safeout_table, \
  188. .enable_reg = MAX77843_SYS_REG_SAFEOUTCTRL, \
  189. .enable_mask = MAX77843_REG_SAFEOUTCTRL_ENSAFEOUT ## num, \
  190. .vsel_reg = MAX77843_SYS_REG_SAFEOUTCTRL, \
  191. .vsel_mask = MAX77843_REG_SAFEOUTCTRL_SAFEOUT ## num ## _MASK, \
  192. }
  193. static const struct regulator_desc max77843_supported_regulators[] = {
  194. [MAX77843_SAFEOUT1] = max77843_regulator_desc_esafeout(1),
  195. [MAX77843_SAFEOUT2] = max77843_regulator_desc_esafeout(2),
  196. [MAX77843_CHARGER] = {
  197. .name = "CHARGER",
  198. .id = MAX77843_CHARGER,
  199. .ops = &max77693_charger_ops,
  200. .of_match = of_match_ptr("CHARGER"),
  201. .regulators_node = of_match_ptr("regulators"),
  202. .type = REGULATOR_CURRENT,
  203. .owner = THIS_MODULE,
  204. .enable_reg = MAX77843_CHG_REG_CHG_CNFG_00,
  205. .enable_mask = MAX77843_CHG_MASK,
  206. .enable_val = MAX77843_CHG_MASK,
  207. },
  208. };
  209. static const struct chg_reg_data max77843_chg_reg_data = {
  210. .linear_reg = MAX77843_CHG_REG_CHG_CNFG_02,
  211. .linear_mask = MAX77843_CHG_FAST_CHG_CURRENT_MASK,
  212. .uA_step = MAX77843_CHG_FAST_CHG_CURRENT_STEP,
  213. .min_sel = 2,
  214. };
  215. static int max77693_pmic_probe(struct platform_device *pdev)
  216. {
  217. enum max77693_types type = platform_get_device_id(pdev)->driver_data;
  218. struct max77693_dev *iodev = dev_get_drvdata(pdev->dev.parent);
  219. const struct regulator_desc *regulators;
  220. unsigned int regulators_size;
  221. int i;
  222. struct regulator_config config = { };
  223. config.dev = iodev->dev;
  224. switch (type) {
  225. case TYPE_MAX77693:
  226. regulators = max77693_supported_regulators;
  227. regulators_size = ARRAY_SIZE(max77693_supported_regulators);
  228. config.driver_data = (void *)&max77693_chg_reg_data;
  229. break;
  230. case TYPE_MAX77843:
  231. regulators = max77843_supported_regulators;
  232. regulators_size = ARRAY_SIZE(max77843_supported_regulators);
  233. config.driver_data = (void *)&max77843_chg_reg_data;
  234. break;
  235. default:
  236. dev_err(&pdev->dev, "Unsupported device type: %u\n", type);
  237. return -ENODEV;
  238. }
  239. for (i = 0; i < regulators_size; i++) {
  240. struct regulator_dev *rdev;
  241. config.regmap = max77693_get_regmap(type, iodev,
  242. regulators[i].id);
  243. rdev = devm_regulator_register(&pdev->dev,
  244. &regulators[i], &config);
  245. if (IS_ERR(rdev)) {
  246. dev_err(&pdev->dev,
  247. "Failed to initialize regulator-%d\n", i);
  248. return PTR_ERR(rdev);
  249. }
  250. }
  251. return 0;
  252. }
  253. static const struct platform_device_id max77693_pmic_id[] = {
  254. { "max77693-pmic", TYPE_MAX77693 },
  255. { "max77843-regulator", TYPE_MAX77843 },
  256. {},
  257. };
  258. MODULE_DEVICE_TABLE(platform, max77693_pmic_id);
  259. static struct platform_driver max77693_pmic_driver = {
  260. .driver = {
  261. .name = "max77693-pmic",
  262. },
  263. .probe = max77693_pmic_probe,
  264. .id_table = max77693_pmic_id,
  265. };
  266. static int __init max77693_pmic_init(void)
  267. {
  268. return platform_driver_register(&max77693_pmic_driver);
  269. }
  270. subsys_initcall(max77693_pmic_init);
  271. static void __exit max77693_pmic_cleanup(void)
  272. {
  273. platform_driver_unregister(&max77693_pmic_driver);
  274. }
  275. module_exit(max77693_pmic_cleanup);
  276. MODULE_DESCRIPTION("MAXIM 77693/77843 regulator driver");
  277. MODULE_AUTHOR("Jonghwa Lee <jonghwa3.lee@samsung.com>");
  278. MODULE_AUTHOR("Krzysztof Kozlowski <krzk@kernel.org>");
  279. MODULE_LICENSE("GPL");