max77843.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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_get_current_limit(struct regulator_dev *rdev)
  32. {
  33. struct regmap *regmap = rdev->regmap;
  34. unsigned int chg_min_uA = rdev->constraints->min_uA;
  35. unsigned int chg_max_uA = rdev->constraints->max_uA;
  36. unsigned int val;
  37. int ret;
  38. unsigned int reg, sel;
  39. ret = regmap_read(regmap, MAX77843_CHG_REG_CHG_CNFG_02, &reg);
  40. if (ret) {
  41. dev_err(&rdev->dev, "Failed to read charger register\n");
  42. return ret;
  43. }
  44. sel = reg & MAX77843_CHG_FAST_CHG_CURRENT_MASK;
  45. if (sel < 0x03)
  46. sel = 0;
  47. else
  48. sel -= 2;
  49. val = chg_min_uA + MAX77843_CHG_FAST_CHG_CURRENT_STEP * sel;
  50. if (val > chg_max_uA)
  51. return -EINVAL;
  52. return val;
  53. }
  54. static int max77843_reg_set_current_limit(struct regulator_dev *rdev,
  55. int min_uA, int max_uA)
  56. {
  57. struct regmap *regmap = rdev->regmap;
  58. unsigned int chg_min_uA = rdev->constraints->min_uA;
  59. int sel = 0;
  60. while (chg_min_uA + MAX77843_CHG_FAST_CHG_CURRENT_STEP * sel < min_uA)
  61. sel++;
  62. if (chg_min_uA + MAX77843_CHG_FAST_CHG_CURRENT_STEP * sel > max_uA)
  63. return -EINVAL;
  64. sel += 2;
  65. return regmap_write(regmap, MAX77843_CHG_REG_CHG_CNFG_02, sel);
  66. }
  67. static struct regulator_ops max77843_charger_ops = {
  68. .is_enabled = regulator_is_enabled_regmap,
  69. .enable = regulator_enable_regmap,
  70. .disable = regulator_disable_regmap,
  71. .get_current_limit = max77843_reg_get_current_limit,
  72. .set_current_limit = max77843_reg_set_current_limit,
  73. };
  74. static struct regulator_ops max77843_regulator_ops = {
  75. .is_enabled = regulator_is_enabled_regmap,
  76. .enable = regulator_enable_regmap,
  77. .disable = regulator_disable_regmap,
  78. .list_voltage = regulator_list_voltage_table,
  79. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  80. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  81. };
  82. #define MAX77843_SAFEOUT(num) { \
  83. .name = "SAFEOUT" # num, \
  84. .id = MAX77843_SAFEOUT ## num, \
  85. .ops = &max77843_regulator_ops, \
  86. .of_match = of_match_ptr("SAFEOUT" # num), \
  87. .regulators_node = of_match_ptr("regulators"), \
  88. .type = REGULATOR_VOLTAGE, \
  89. .owner = THIS_MODULE, \
  90. .n_voltages = ARRAY_SIZE(max77843_safeout_voltage_table), \
  91. .volt_table = max77843_safeout_voltage_table, \
  92. .enable_reg = MAX77843_SYS_REG_SAFEOUTCTRL, \
  93. .enable_mask = MAX77843_REG_SAFEOUTCTRL_ENSAFEOUT ## num, \
  94. .vsel_reg = MAX77843_SYS_REG_SAFEOUTCTRL, \
  95. .vsel_mask = MAX77843_REG_SAFEOUTCTRL_SAFEOUT ## num ## _MASK, \
  96. }
  97. static const struct regulator_desc max77843_supported_regulators[] = {
  98. [MAX77843_SAFEOUT1] = MAX77843_SAFEOUT(1),
  99. [MAX77843_SAFEOUT2] = MAX77843_SAFEOUT(2),
  100. [MAX77843_CHARGER] = {
  101. .name = "CHARGER",
  102. .id = MAX77843_CHARGER,
  103. .ops = &max77843_charger_ops,
  104. .of_match = of_match_ptr("CHARGER"),
  105. .regulators_node = of_match_ptr("regulators"),
  106. .type = REGULATOR_CURRENT,
  107. .owner = THIS_MODULE,
  108. .enable_reg = MAX77843_CHG_REG_CHG_CNFG_00,
  109. .enable_mask = MAX77843_CHG_MASK | MAX77843_CHG_BUCK_MASK,
  110. .enable_val = MAX77843_CHG_MASK | MAX77843_CHG_BUCK_MASK,
  111. },
  112. };
  113. static struct regmap *max77843_get_regmap(struct max77843 *max77843, int reg_id)
  114. {
  115. switch (reg_id) {
  116. case MAX77843_SAFEOUT1:
  117. case MAX77843_SAFEOUT2:
  118. return max77843->regmap;
  119. case MAX77843_CHARGER:
  120. return max77843->regmap_chg;
  121. default:
  122. return max77843->regmap;
  123. }
  124. }
  125. static int max77843_regulator_probe(struct platform_device *pdev)
  126. {
  127. struct max77843 *max77843 = dev_get_drvdata(pdev->dev.parent);
  128. struct regulator_config config = {};
  129. int i;
  130. config.dev = max77843->dev;
  131. config.driver_data = max77843;
  132. for (i = 0; i < ARRAY_SIZE(max77843_supported_regulators); i++) {
  133. struct regulator_dev *regulator;
  134. config.regmap = max77843_get_regmap(max77843,
  135. max77843_supported_regulators[i].id);
  136. regulator = devm_regulator_register(&pdev->dev,
  137. &max77843_supported_regulators[i], &config);
  138. if (IS_ERR(regulator)) {
  139. dev_err(&pdev->dev,
  140. "Failed to regiser regulator-%d\n", i);
  141. return PTR_ERR(regulator);
  142. }
  143. }
  144. return 0;
  145. }
  146. static const struct platform_device_id max77843_regulator_id[] = {
  147. { "max77843-regulator", },
  148. { /* sentinel */ },
  149. };
  150. static struct platform_driver max77843_regulator_driver = {
  151. .driver = {
  152. .name = "max77843-regulator",
  153. },
  154. .probe = max77843_regulator_probe,
  155. .id_table = max77843_regulator_id,
  156. };
  157. static int __init max77843_regulator_init(void)
  158. {
  159. return platform_driver_register(&max77843_regulator_driver);
  160. }
  161. subsys_initcall(max77843_regulator_init);
  162. static void __exit max77843_regulator_exit(void)
  163. {
  164. platform_driver_unregister(&max77843_regulator_driver);
  165. }
  166. module_exit(max77843_regulator_exit);
  167. MODULE_AUTHOR("Jaewon Kim <jaewon02.kim@samsung.com>");
  168. MODULE_AUTHOR("Beomho Seo <beomho.seo@samsung.com>");
  169. MODULE_DESCRIPTION("Maxim MAX77843 regulator driver");
  170. MODULE_LICENSE("GPL");