max77693.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * max77693.c - Regulator driver for the Maxim 77693
  3. *
  4. * Copyright (C) 2013 Samsung Electronics
  5. * Jonghwa Lee <jonghwa3.lee@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. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. * This driver is based on max77686.c
  22. */
  23. #include <linux/err.h>
  24. #include <linux/slab.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/module.h>
  27. #include <linux/export.h>
  28. #include <linux/regulator/driver.h>
  29. #include <linux/regulator/machine.h>
  30. #include <linux/mfd/max77693.h>
  31. #include <linux/mfd/max77693-private.h>
  32. #include <linux/regulator/of_regulator.h>
  33. #include <linux/regmap.h>
  34. #define CHGIN_ILIM_STEP_20mA 20000
  35. /*
  36. * CHARGER regulator - Min : 20mA, Max : 2580mA, step : 20mA
  37. * 0x00, 0x01, 0x2, 0x03 = 60 mA
  38. * 0x04 ~ 0x7E = (60 + (X - 3) * 20) mA
  39. */
  40. static int max77693_chg_get_current_limit(struct regulator_dev *rdev)
  41. {
  42. unsigned int chg_min_uA = rdev->constraints->min_uA;
  43. unsigned int chg_max_uA = rdev->constraints->max_uA;
  44. unsigned int reg, sel;
  45. unsigned int val;
  46. int ret;
  47. ret = regmap_read(rdev->regmap, MAX77693_CHG_REG_CHG_CNFG_09, &reg);
  48. if (ret < 0)
  49. return ret;
  50. sel = reg & CHG_CNFG_09_CHGIN_ILIM_MASK;
  51. /* the first four codes for charger current are all 60mA */
  52. if (sel <= 3)
  53. sel = 0;
  54. else
  55. sel -= 3;
  56. val = chg_min_uA + CHGIN_ILIM_STEP_20mA * sel;
  57. if (val > chg_max_uA)
  58. return -EINVAL;
  59. return val;
  60. }
  61. static int max77693_chg_set_current_limit(struct regulator_dev *rdev,
  62. int min_uA, int max_uA)
  63. {
  64. unsigned int chg_min_uA = rdev->constraints->min_uA;
  65. int sel = 0;
  66. while (chg_min_uA + CHGIN_ILIM_STEP_20mA * sel < min_uA)
  67. sel++;
  68. if (chg_min_uA + CHGIN_ILIM_STEP_20mA * sel > max_uA)
  69. return -EINVAL;
  70. /* the first four codes for charger current are all 60mA */
  71. sel += 3;
  72. return regmap_write(rdev->regmap,
  73. MAX77693_CHG_REG_CHG_CNFG_09, sel);
  74. }
  75. /* end of CHARGER regulator ops */
  76. static const unsigned int max77693_safeout_table[] = {
  77. 4850000,
  78. 4900000,
  79. 4950000,
  80. 3300000,
  81. };
  82. static struct regulator_ops max77693_safeout_ops = {
  83. .list_voltage = regulator_list_voltage_table,
  84. .is_enabled = regulator_is_enabled_regmap,
  85. .enable = regulator_enable_regmap,
  86. .disable = regulator_disable_regmap,
  87. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  88. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  89. };
  90. static struct regulator_ops max77693_charger_ops = {
  91. .is_enabled = regulator_is_enabled_regmap,
  92. .enable = regulator_enable_regmap,
  93. .disable = regulator_disable_regmap,
  94. .get_current_limit = max77693_chg_get_current_limit,
  95. .set_current_limit = max77693_chg_set_current_limit,
  96. };
  97. #define regulator_desc_esafeout(_num) { \
  98. .name = "ESAFEOUT"#_num, \
  99. .id = MAX77693_ESAFEOUT##_num, \
  100. .of_match = of_match_ptr("ESAFEOUT"#_num), \
  101. .regulators_node = of_match_ptr("regulators"), \
  102. .n_voltages = 4, \
  103. .ops = &max77693_safeout_ops, \
  104. .type = REGULATOR_VOLTAGE, \
  105. .owner = THIS_MODULE, \
  106. .volt_table = max77693_safeout_table, \
  107. .vsel_reg = MAX77693_CHG_REG_SAFEOUT_CTRL, \
  108. .vsel_mask = SAFEOUT_CTRL_SAFEOUT##_num##_MASK, \
  109. .enable_reg = MAX77693_CHG_REG_SAFEOUT_CTRL, \
  110. .enable_mask = SAFEOUT_CTRL_ENSAFEOUT##_num##_MASK , \
  111. }
  112. static const struct regulator_desc regulators[] = {
  113. regulator_desc_esafeout(1),
  114. regulator_desc_esafeout(2),
  115. {
  116. .name = "CHARGER",
  117. .id = MAX77693_CHARGER,
  118. .of_match = of_match_ptr("CHARGER"),
  119. .regulators_node = of_match_ptr("regulators"),
  120. .ops = &max77693_charger_ops,
  121. .type = REGULATOR_CURRENT,
  122. .owner = THIS_MODULE,
  123. .enable_reg = MAX77693_CHG_REG_CHG_CNFG_00,
  124. .enable_mask = CHG_CNFG_00_CHG_MASK |
  125. CHG_CNFG_00_BUCK_MASK,
  126. .enable_val = CHG_CNFG_00_CHG_MASK | CHG_CNFG_00_BUCK_MASK,
  127. },
  128. };
  129. static int max77693_pmic_probe(struct platform_device *pdev)
  130. {
  131. struct max77693_dev *iodev = dev_get_drvdata(pdev->dev.parent);
  132. int i;
  133. struct regulator_config config = { };
  134. config.dev = iodev->dev;
  135. config.regmap = iodev->regmap;
  136. for (i = 0; i < ARRAY_SIZE(regulators); i++) {
  137. struct regulator_dev *rdev;
  138. rdev = devm_regulator_register(&pdev->dev,
  139. &regulators[i], &config);
  140. if (IS_ERR(rdev)) {
  141. dev_err(&pdev->dev,
  142. "Failed to initialize regulator-%d\n", i);
  143. return PTR_ERR(rdev);
  144. }
  145. }
  146. return 0;
  147. }
  148. static const struct platform_device_id max77693_pmic_id[] = {
  149. {"max77693-pmic", 0},
  150. {},
  151. };
  152. MODULE_DEVICE_TABLE(platform, max77693_pmic_id);
  153. static struct platform_driver max77693_pmic_driver = {
  154. .driver = {
  155. .name = "max77693-pmic",
  156. },
  157. .probe = max77693_pmic_probe,
  158. .id_table = max77693_pmic_id,
  159. };
  160. module_platform_driver(max77693_pmic_driver);
  161. MODULE_DESCRIPTION("MAXIM MAX77693 regulator driver");
  162. MODULE_AUTHOR("Jonghwa Lee <jonghwa3.lee@samsung.com>");
  163. MODULE_LICENSE("GPL");