max77693-regulator.c 8.6 KB

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