max77693.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. #define CHGIN_ILIM_STEP_20mA 20000
  34. /* CHARGER regulator ops */
  35. /* CHARGER regulator uses two bits for enabling */
  36. static int max77693_chg_is_enabled(struct regulator_dev *rdev)
  37. {
  38. int ret;
  39. u8 val;
  40. ret = max77693_read_reg(rdev->regmap, rdev->desc->enable_reg, &val);
  41. if (ret)
  42. return ret;
  43. return (val & rdev->desc->enable_mask) == rdev->desc->enable_mask;
  44. }
  45. /*
  46. * CHARGER regulator - Min : 20mA, Max : 2580mA, step : 20mA
  47. * 0x00, 0x01, 0x2, 0x03 = 60 mA
  48. * 0x04 ~ 0x7E = (60 + (X - 3) * 20) mA
  49. */
  50. static int max77693_chg_get_current_limit(struct regulator_dev *rdev)
  51. {
  52. unsigned int chg_min_uA = rdev->constraints->min_uA;
  53. unsigned int chg_max_uA = rdev->constraints->max_uA;
  54. u8 reg, sel;
  55. unsigned int val;
  56. int ret;
  57. ret = max77693_read_reg(rdev->regmap,
  58. MAX77693_CHG_REG_CHG_CNFG_09, &reg);
  59. if (ret < 0)
  60. return ret;
  61. sel = reg & CHG_CNFG_09_CHGIN_ILIM_MASK;
  62. /* the first four codes for charger current are all 60mA */
  63. if (sel <= 3)
  64. sel = 0;
  65. else
  66. sel -= 3;
  67. val = chg_min_uA + CHGIN_ILIM_STEP_20mA * 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. unsigned int chg_min_uA = rdev->constraints->min_uA;
  76. int sel = 0;
  77. while (chg_min_uA + CHGIN_ILIM_STEP_20mA * sel < min_uA)
  78. sel++;
  79. if (chg_min_uA + CHGIN_ILIM_STEP_20mA * sel > max_uA)
  80. return -EINVAL;
  81. /* the first four codes for charger current are all 60mA */
  82. sel += 3;
  83. return max77693_write_reg(rdev->regmap,
  84. MAX77693_CHG_REG_CHG_CNFG_09, sel);
  85. }
  86. /* end of CHARGER regulator ops */
  87. static const unsigned int max77693_safeout_table[] = {
  88. 4850000,
  89. 4900000,
  90. 4950000,
  91. 3300000,
  92. };
  93. static struct regulator_ops max77693_safeout_ops = {
  94. .list_voltage = regulator_list_voltage_table,
  95. .is_enabled = regulator_is_enabled_regmap,
  96. .enable = regulator_enable_regmap,
  97. .disable = regulator_disable_regmap,
  98. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  99. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  100. };
  101. static struct regulator_ops max77693_charger_ops = {
  102. .is_enabled = max77693_chg_is_enabled,
  103. .enable = regulator_enable_regmap,
  104. .disable = regulator_disable_regmap,
  105. .get_current_limit = max77693_chg_get_current_limit,
  106. .set_current_limit = max77693_chg_set_current_limit,
  107. };
  108. #define regulator_desc_esafeout(_num) { \
  109. .name = "ESAFEOUT"#_num, \
  110. .id = MAX77693_ESAFEOUT##_num, \
  111. .n_voltages = 4, \
  112. .ops = &max77693_safeout_ops, \
  113. .type = REGULATOR_VOLTAGE, \
  114. .owner = THIS_MODULE, \
  115. .volt_table = max77693_safeout_table, \
  116. .vsel_reg = MAX77693_CHG_REG_SAFEOUT_CTRL, \
  117. .vsel_mask = SAFEOUT_CTRL_SAFEOUT##_num##_MASK, \
  118. .enable_reg = MAX77693_CHG_REG_SAFEOUT_CTRL, \
  119. .enable_mask = SAFEOUT_CTRL_ENSAFEOUT##_num##_MASK , \
  120. }
  121. static struct regulator_desc regulators[] = {
  122. regulator_desc_esafeout(1),
  123. regulator_desc_esafeout(2),
  124. {
  125. .name = "CHARGER",
  126. .id = MAX77693_CHARGER,
  127. .ops = &max77693_charger_ops,
  128. .type = REGULATOR_CURRENT,
  129. .owner = THIS_MODULE,
  130. .enable_reg = MAX77693_CHG_REG_CHG_CNFG_00,
  131. .enable_mask = CHG_CNFG_00_CHG_MASK |
  132. CHG_CNFG_00_BUCK_MASK,
  133. },
  134. };
  135. #ifdef CONFIG_OF
  136. static int max77693_pmic_dt_parse_rdata(struct device *dev,
  137. struct max77693_regulator_data **rdata)
  138. {
  139. struct device_node *np;
  140. struct of_regulator_match *rmatch;
  141. struct max77693_regulator_data *tmp;
  142. int i, matched = 0;
  143. np = of_get_child_by_name(dev->parent->of_node, "regulators");
  144. if (!np)
  145. return -EINVAL;
  146. rmatch = devm_kzalloc(dev,
  147. sizeof(*rmatch) * ARRAY_SIZE(regulators), GFP_KERNEL);
  148. if (!rmatch) {
  149. of_node_put(np);
  150. return -ENOMEM;
  151. }
  152. for (i = 0; i < ARRAY_SIZE(regulators); i++)
  153. rmatch[i].name = regulators[i].name;
  154. matched = of_regulator_match(dev, np, rmatch, ARRAY_SIZE(regulators));
  155. of_node_put(np);
  156. if (matched <= 0)
  157. return matched;
  158. *rdata = devm_kzalloc(dev, sizeof(**rdata) * matched, GFP_KERNEL);
  159. if (!(*rdata))
  160. return -ENOMEM;
  161. tmp = *rdata;
  162. for (i = 0; i < matched; i++) {
  163. tmp->initdata = rmatch[i].init_data;
  164. tmp->of_node = rmatch[i].of_node;
  165. tmp->id = regulators[i].id;
  166. tmp++;
  167. }
  168. return matched;
  169. }
  170. #else
  171. static int max77693_pmic_dt_parse_rdata(struct device *dev,
  172. struct max77693_regulator_data **rdata)
  173. {
  174. return 0;
  175. }
  176. #endif /* CONFIG_OF */
  177. static int max77693_pmic_init_rdata(struct device *dev,
  178. struct max77693_regulator_data **rdata)
  179. {
  180. struct max77693_platform_data *pdata;
  181. int num_regulators = 0;
  182. pdata = dev_get_platdata(dev->parent);
  183. if (pdata) {
  184. *rdata = pdata->regulators;
  185. num_regulators = pdata->num_regulators;
  186. }
  187. if (!(*rdata) && dev->parent->of_node)
  188. num_regulators = max77693_pmic_dt_parse_rdata(dev, rdata);
  189. return num_regulators;
  190. }
  191. static int max77693_pmic_probe(struct platform_device *pdev)
  192. {
  193. struct max77693_dev *iodev = dev_get_drvdata(pdev->dev.parent);
  194. struct max77693_regulator_data *rdata = NULL;
  195. int num_rdata, i;
  196. struct regulator_config config;
  197. num_rdata = max77693_pmic_init_rdata(&pdev->dev, &rdata);
  198. if (!rdata || num_rdata <= 0) {
  199. dev_err(&pdev->dev, "No init data supplied.\n");
  200. return -ENODEV;
  201. }
  202. config.dev = &pdev->dev;
  203. config.regmap = iodev->regmap;
  204. for (i = 0; i < num_rdata; i++) {
  205. int id = rdata[i].id;
  206. struct regulator_dev *rdev;
  207. config.init_data = rdata[i].initdata;
  208. config.of_node = rdata[i].of_node;
  209. rdev = devm_regulator_register(&pdev->dev,
  210. &regulators[id], &config);
  211. if (IS_ERR(rdev)) {
  212. dev_err(&pdev->dev,
  213. "Failed to initialize regulator-%d\n", id);
  214. return PTR_ERR(rdev);
  215. }
  216. }
  217. return 0;
  218. }
  219. static const struct platform_device_id max77693_pmic_id[] = {
  220. {"max77693-pmic", 0},
  221. {},
  222. };
  223. MODULE_DEVICE_TABLE(platform, max77693_pmic_id);
  224. static struct platform_driver max77693_pmic_driver = {
  225. .driver = {
  226. .name = "max77693-pmic",
  227. .owner = THIS_MODULE,
  228. },
  229. .probe = max77693_pmic_probe,
  230. .id_table = max77693_pmic_id,
  231. };
  232. module_platform_driver(max77693_pmic_driver);
  233. MODULE_DESCRIPTION("MAXIM MAX77693 regulator driver");
  234. MODULE_AUTHOR("Jonghwa Lee <jonghwa3.lee@samsung.com>");
  235. MODULE_LICENSE("GPL");