max77843.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * max77843.c - Regulator driver for the Maxim MAX77843
  3. *
  4. * Copyright (C) 2015 Samsung Electronics
  5. * Author: Jaewon Kim <jaewon02.kim@samsung.com>
  6. * Author: Beomho Seo <beomho.seo@samsung.com>
  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. #include <linux/module.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/regulator/driver.h>
  16. #include <linux/regulator/machine.h>
  17. #include <linux/mfd/max77843-private.h>
  18. #include <linux/regulator/of_regulator.h>
  19. enum max77843_regulator_type {
  20. MAX77843_SAFEOUT1 = 0,
  21. MAX77843_SAFEOUT2,
  22. MAX77843_CHARGER,
  23. MAX77843_NUM,
  24. };
  25. static const unsigned int max77843_safeout_voltage_table[] = {
  26. 4850000,
  27. 4900000,
  28. 4950000,
  29. 3300000,
  30. };
  31. static int max77843_reg_is_enabled(struct regulator_dev *rdev)
  32. {
  33. struct regmap *regmap = rdev->regmap;
  34. int ret;
  35. unsigned int reg;
  36. ret = regmap_read(regmap, rdev->desc->enable_reg, &reg);
  37. if (ret) {
  38. dev_err(&rdev->dev, "Fialed to read charger register\n");
  39. return ret;
  40. }
  41. return (reg & rdev->desc->enable_mask) == rdev->desc->enable_mask;
  42. }
  43. static int max77843_reg_get_current_limit(struct regulator_dev *rdev)
  44. {
  45. struct regmap *regmap = rdev->regmap;
  46. unsigned int chg_min_uA = rdev->constraints->min_uA;
  47. unsigned int chg_max_uA = rdev->constraints->max_uA;
  48. unsigned int val;
  49. int ret;
  50. unsigned int reg, sel;
  51. ret = regmap_read(regmap, MAX77843_CHG_REG_CHG_CNFG_02, &reg);
  52. if (ret) {
  53. dev_err(&rdev->dev, "Failed to read charger register\n");
  54. return ret;
  55. }
  56. sel = reg & MAX77843_CHG_FAST_CHG_CURRENT_MASK;
  57. if (sel < 0x03)
  58. sel = 0;
  59. else
  60. sel -= 2;
  61. val = chg_min_uA + MAX77843_CHG_FAST_CHG_CURRENT_STEP * sel;
  62. if (val > chg_max_uA)
  63. return -EINVAL;
  64. return val;
  65. }
  66. static int max77843_reg_set_current_limit(struct regulator_dev *rdev,
  67. int min_uA, int max_uA)
  68. {
  69. struct regmap *regmap = rdev->regmap;
  70. unsigned int chg_min_uA = rdev->constraints->min_uA;
  71. int sel = 0;
  72. while (chg_min_uA + MAX77843_CHG_FAST_CHG_CURRENT_STEP * sel < min_uA)
  73. sel++;
  74. if (chg_min_uA + MAX77843_CHG_FAST_CHG_CURRENT_STEP * sel > max_uA)
  75. return -EINVAL;
  76. sel += 2;
  77. return regmap_write(regmap, MAX77843_CHG_REG_CHG_CNFG_02, sel);
  78. }
  79. static struct regulator_ops max77843_charger_ops = {
  80. .is_enabled = max77843_reg_is_enabled,
  81. .enable = regulator_enable_regmap,
  82. .disable = regulator_disable_regmap,
  83. .get_current_limit = max77843_reg_get_current_limit,
  84. .set_current_limit = max77843_reg_set_current_limit,
  85. };
  86. static struct regulator_ops max77843_regulator_ops = {
  87. .is_enabled = regulator_is_enabled_regmap,
  88. .enable = regulator_enable_regmap,
  89. .disable = regulator_disable_regmap,
  90. .list_voltage = regulator_list_voltage_table,
  91. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  92. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  93. };
  94. static const struct regulator_desc max77843_supported_regulators[] = {
  95. [MAX77843_SAFEOUT1] = {
  96. .name = "SAFEOUT1",
  97. .id = MAX77843_SAFEOUT1,
  98. .ops = &max77843_regulator_ops,
  99. .of_match = of_match_ptr("SAFEOUT1"),
  100. .regulators_node = of_match_ptr("regulators"),
  101. .type = REGULATOR_VOLTAGE,
  102. .owner = THIS_MODULE,
  103. .n_voltages = ARRAY_SIZE(max77843_safeout_voltage_table),
  104. .volt_table = max77843_safeout_voltage_table,
  105. .enable_reg = MAX77843_SYS_REG_SAFEOUTCTRL,
  106. .enable_mask = MAX77843_REG_SAFEOUTCTRL_ENSAFEOUT1,
  107. .vsel_reg = MAX77843_SYS_REG_SAFEOUTCTRL,
  108. .vsel_mask = MAX77843_REG_SAFEOUTCTRL_SAFEOUT1_MASK,
  109. },
  110. [MAX77843_SAFEOUT2] = {
  111. .name = "SAFEOUT2",
  112. .id = MAX77843_SAFEOUT2,
  113. .ops = &max77843_regulator_ops,
  114. .of_match = of_match_ptr("SAFEOUT2"),
  115. .regulators_node = of_match_ptr("regulators"),
  116. .type = REGULATOR_VOLTAGE,
  117. .owner = THIS_MODULE,
  118. .n_voltages = ARRAY_SIZE(max77843_safeout_voltage_table),
  119. .volt_table = max77843_safeout_voltage_table,
  120. .enable_reg = MAX77843_SYS_REG_SAFEOUTCTRL,
  121. .enable_mask = MAX77843_REG_SAFEOUTCTRL_ENSAFEOUT2,
  122. .vsel_reg = MAX77843_SYS_REG_SAFEOUTCTRL,
  123. .vsel_mask = MAX77843_REG_SAFEOUTCTRL_SAFEOUT2_MASK,
  124. },
  125. [MAX77843_CHARGER] = {
  126. .name = "CHARGER",
  127. .id = MAX77843_CHARGER,
  128. .ops = &max77843_charger_ops,
  129. .of_match = of_match_ptr("CHARGER"),
  130. .regulators_node = of_match_ptr("regulators"),
  131. .type = REGULATOR_CURRENT,
  132. .owner = THIS_MODULE,
  133. .enable_reg = MAX77843_CHG_REG_CHG_CNFG_00,
  134. .enable_mask = MAX77843_CHG_MASK,
  135. },
  136. };
  137. static struct regmap *max77843_get_regmap(struct max77843 *max77843, int reg_id)
  138. {
  139. switch (reg_id) {
  140. case MAX77843_SAFEOUT1:
  141. case MAX77843_SAFEOUT2:
  142. return max77843->regmap;
  143. case MAX77843_CHARGER:
  144. return max77843->regmap_chg;
  145. default:
  146. return max77843->regmap;
  147. }
  148. }
  149. static int max77843_regulator_probe(struct platform_device *pdev)
  150. {
  151. struct max77843 *max77843 = dev_get_drvdata(pdev->dev.parent);
  152. struct regulator_config config = {};
  153. int i;
  154. config.dev = max77843->dev;
  155. config.driver_data = max77843;
  156. for (i = 0; i < ARRAY_SIZE(max77843_supported_regulators); i++) {
  157. struct regulator_dev *regulator;
  158. config.regmap = max77843_get_regmap(max77843,
  159. max77843_supported_regulators[i].id);
  160. regulator = devm_regulator_register(&pdev->dev,
  161. &max77843_supported_regulators[i], &config);
  162. if (IS_ERR(regulator)) {
  163. dev_err(&pdev->dev,
  164. "Failed to regiser regulator-%d\n", i);
  165. return PTR_ERR(regulator);
  166. }
  167. }
  168. return 0;
  169. }
  170. static const struct platform_device_id max77843_regulator_id[] = {
  171. { "max77843-regulator", },
  172. { /* sentinel */ },
  173. };
  174. static struct platform_driver max77843_regulator_driver = {
  175. .driver = {
  176. .name = "max77843-regulator",
  177. },
  178. .probe = max77843_regulator_probe,
  179. .id_table = max77843_regulator_id,
  180. };
  181. static int __init max77843_regulator_init(void)
  182. {
  183. return platform_driver_register(&max77843_regulator_driver);
  184. }
  185. subsys_initcall(max77843_regulator_init);
  186. static void __exit max77843_regulator_exit(void)
  187. {
  188. platform_driver_unregister(&max77843_regulator_driver);
  189. }
  190. module_exit(max77843_regulator_exit);
  191. MODULE_AUTHOR("Jaewon Kim <jaewon02.kim@samsung.com>");
  192. MODULE_AUTHOR("Beomho Seo <beomho.seo@samsung.com>");
  193. MODULE_DESCRIPTION("Maxim MAX77843 regulator driver");
  194. MODULE_LICENSE("GPL");