max77693.c 8.2 KB

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