max8907-regulator.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /*
  2. * max8907-regulator.c -- support regulators in max8907
  3. *
  4. * Copyright (C) 2010 Gyungoh Yoo <jack.yoo@maxim-ic.com>
  5. * Copyright (C) 2010-2012, NVIDIA CORPORATION. All rights reserved.
  6. *
  7. * Portions based on drivers/regulator/tps65910-regulator.c,
  8. * Copyright 2010 Texas Instruments Inc.
  9. * Author: Graeme Gregory <gg@slimlogic.co.uk>
  10. * Author: Jorge Eduardo Candelaria <jedu@slimlogic.co.uk>
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License version 2 as
  14. * published by the Free Software Foundation.
  15. */
  16. #include <linux/err.h>
  17. #include <linux/init.h>
  18. #include <linux/mfd/core.h>
  19. #include <linux/mfd/max8907.h>
  20. #include <linux/module.h>
  21. #include <linux/of.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/regulator/driver.h>
  24. #include <linux/regulator/machine.h>
  25. #include <linux/regulator/of_regulator.h>
  26. #include <linux/regmap.h>
  27. #include <linux/slab.h>
  28. #define MAX8907_II2RR_VERSION_MASK 0xF0
  29. #define MAX8907_II2RR_VERSION_REV_A 0x00
  30. #define MAX8907_II2RR_VERSION_REV_B 0x10
  31. #define MAX8907_II2RR_VERSION_REV_C 0x30
  32. struct max8907_regulator {
  33. struct regulator_desc desc[MAX8907_NUM_REGULATORS];
  34. };
  35. #define REG_MBATT() \
  36. [MAX8907_MBATT] = { \
  37. .name = "MBATT", \
  38. .supply_name = "mbatt", \
  39. .id = MAX8907_MBATT, \
  40. .ops = &max8907_mbatt_ops, \
  41. .type = REGULATOR_VOLTAGE, \
  42. .owner = THIS_MODULE, \
  43. }
  44. #define REG_LDO(ids, supply, base, min, max, step) \
  45. [MAX8907_##ids] = { \
  46. .name = #ids, \
  47. .supply_name = supply, \
  48. .id = MAX8907_##ids, \
  49. .n_voltages = ((max) - (min)) / (step) + 1, \
  50. .ops = &max8907_ldo_ops, \
  51. .type = REGULATOR_VOLTAGE, \
  52. .owner = THIS_MODULE, \
  53. .min_uV = (min), \
  54. .uV_step = (step), \
  55. .vsel_reg = (base) + MAX8907_VOUT, \
  56. .vsel_mask = 0x3f, \
  57. .enable_reg = (base) + MAX8907_CTL, \
  58. .enable_mask = MAX8907_MASK_LDO_EN, \
  59. }
  60. #define REG_FIXED(ids, supply, voltage) \
  61. [MAX8907_##ids] = { \
  62. .name = #ids, \
  63. .supply_name = supply, \
  64. .id = MAX8907_##ids, \
  65. .n_voltages = 1, \
  66. .ops = &max8907_fixed_ops, \
  67. .type = REGULATOR_VOLTAGE, \
  68. .owner = THIS_MODULE, \
  69. .min_uV = (voltage), \
  70. }
  71. #define REG_OUT5V(ids, supply, base, voltage) \
  72. [MAX8907_##ids] = { \
  73. .name = #ids, \
  74. .supply_name = supply, \
  75. .id = MAX8907_##ids, \
  76. .n_voltages = 1, \
  77. .ops = &max8907_out5v_ops, \
  78. .type = REGULATOR_VOLTAGE, \
  79. .owner = THIS_MODULE, \
  80. .min_uV = (voltage), \
  81. .enable_reg = (base), \
  82. .enable_mask = MAX8907_MASK_OUT5V_EN, \
  83. }
  84. #define REG_BBAT(ids, supply, base, min, max, step) \
  85. [MAX8907_##ids] = { \
  86. .name = #ids, \
  87. .supply_name = supply, \
  88. .id = MAX8907_##ids, \
  89. .n_voltages = ((max) - (min)) / (step) + 1, \
  90. .ops = &max8907_bbat_ops, \
  91. .type = REGULATOR_VOLTAGE, \
  92. .owner = THIS_MODULE, \
  93. .min_uV = (min), \
  94. .uV_step = (step), \
  95. .vsel_reg = (base), \
  96. .vsel_mask = MAX8907_MASK_VBBATTCV, \
  97. }
  98. #define LDO_750_50(id, supply, base) REG_LDO(id, supply, (base), \
  99. 750000, 3900000, 50000)
  100. #define LDO_650_25(id, supply, base) REG_LDO(id, supply, (base), \
  101. 650000, 2225000, 25000)
  102. static struct regulator_ops max8907_mbatt_ops = {
  103. };
  104. static struct regulator_ops max8907_ldo_ops = {
  105. .list_voltage = regulator_list_voltage_linear,
  106. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  107. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  108. .enable = regulator_enable_regmap,
  109. .disable = regulator_disable_regmap,
  110. .is_enabled = regulator_is_enabled_regmap,
  111. };
  112. static struct regulator_ops max8907_ldo_hwctl_ops = {
  113. .list_voltage = regulator_list_voltage_linear,
  114. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  115. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  116. };
  117. static struct regulator_ops max8907_fixed_ops = {
  118. .list_voltage = regulator_list_voltage_linear,
  119. };
  120. static struct regulator_ops max8907_out5v_ops = {
  121. .list_voltage = regulator_list_voltage_linear,
  122. .enable = regulator_enable_regmap,
  123. .disable = regulator_disable_regmap,
  124. .is_enabled = regulator_is_enabled_regmap,
  125. };
  126. static struct regulator_ops max8907_out5v_hwctl_ops = {
  127. .list_voltage = regulator_list_voltage_linear,
  128. };
  129. static struct regulator_ops max8907_bbat_ops = {
  130. .list_voltage = regulator_list_voltage_linear,
  131. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  132. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  133. };
  134. static struct regulator_desc max8907_regulators[] = {
  135. REG_MBATT(),
  136. REG_LDO(SD1, "in-v1", MAX8907_REG_SDCTL1, 650000, 2225000, 25000),
  137. REG_LDO(SD2, "in-v2", MAX8907_REG_SDCTL2, 637500, 1425000, 12500),
  138. REG_LDO(SD3, "in-v3", MAX8907_REG_SDCTL3, 750000, 3900000, 50000),
  139. LDO_750_50(LDO1, "in1", MAX8907_REG_LDOCTL1),
  140. LDO_650_25(LDO2, "in2", MAX8907_REG_LDOCTL2),
  141. LDO_650_25(LDO3, "in3", MAX8907_REG_LDOCTL3),
  142. LDO_750_50(LDO4, "in4", MAX8907_REG_LDOCTL4),
  143. LDO_750_50(LDO5, "in5", MAX8907_REG_LDOCTL5),
  144. LDO_750_50(LDO6, "in6", MAX8907_REG_LDOCTL6),
  145. LDO_750_50(LDO7, "in7", MAX8907_REG_LDOCTL7),
  146. LDO_750_50(LDO8, "in8", MAX8907_REG_LDOCTL8),
  147. LDO_750_50(LDO9, "in9", MAX8907_REG_LDOCTL9),
  148. LDO_750_50(LDO10, "in10", MAX8907_REG_LDOCTL10),
  149. LDO_750_50(LDO11, "in11", MAX8907_REG_LDOCTL11),
  150. LDO_750_50(LDO12, "in12", MAX8907_REG_LDOCTL12),
  151. LDO_750_50(LDO13, "in13", MAX8907_REG_LDOCTL13),
  152. LDO_750_50(LDO14, "in14", MAX8907_REG_LDOCTL14),
  153. LDO_750_50(LDO15, "in15", MAX8907_REG_LDOCTL15),
  154. LDO_750_50(LDO16, "in16", MAX8907_REG_LDOCTL16),
  155. LDO_650_25(LDO17, "in17", MAX8907_REG_LDOCTL17),
  156. LDO_650_25(LDO18, "in18", MAX8907_REG_LDOCTL18),
  157. LDO_750_50(LDO19, "in19", MAX8907_REG_LDOCTL19),
  158. LDO_750_50(LDO20, "in20", MAX8907_REG_LDOCTL20),
  159. REG_OUT5V(OUT5V, "mbatt", MAX8907_REG_OUT5VEN, 5000000),
  160. REG_OUT5V(OUT33V, "mbatt", MAX8907_REG_OUT33VEN, 3300000),
  161. REG_BBAT(BBAT, "MBATT", MAX8907_REG_BBAT_CNFG,
  162. 2400000, 3000000, 200000),
  163. REG_FIXED(SDBY, "MBATT", 1200000),
  164. REG_FIXED(VRTC, "MBATT", 3300000),
  165. };
  166. #ifdef CONFIG_OF
  167. #define MATCH(_name, _id) \
  168. [MAX8907_##_id] = { \
  169. .name = #_name, \
  170. .driver_data = (void *)&max8907_regulators[MAX8907_##_id], \
  171. }
  172. static struct of_regulator_match max8907_matches[] = {
  173. MATCH(mbatt, MBATT),
  174. MATCH(sd1, SD1),
  175. MATCH(sd2, SD2),
  176. MATCH(sd3, SD3),
  177. MATCH(ldo1, LDO1),
  178. MATCH(ldo2, LDO2),
  179. MATCH(ldo3, LDO3),
  180. MATCH(ldo4, LDO4),
  181. MATCH(ldo5, LDO5),
  182. MATCH(ldo6, LDO6),
  183. MATCH(ldo7, LDO7),
  184. MATCH(ldo8, LDO8),
  185. MATCH(ldo9, LDO9),
  186. MATCH(ldo10, LDO10),
  187. MATCH(ldo11, LDO11),
  188. MATCH(ldo12, LDO12),
  189. MATCH(ldo13, LDO13),
  190. MATCH(ldo14, LDO14),
  191. MATCH(ldo15, LDO15),
  192. MATCH(ldo16, LDO16),
  193. MATCH(ldo17, LDO17),
  194. MATCH(ldo18, LDO18),
  195. MATCH(ldo19, LDO19),
  196. MATCH(ldo20, LDO20),
  197. MATCH(out5v, OUT5V),
  198. MATCH(out33v, OUT33V),
  199. MATCH(bbat, BBAT),
  200. MATCH(sdby, SDBY),
  201. MATCH(vrtc, VRTC),
  202. };
  203. static int max8907_regulator_parse_dt(struct platform_device *pdev)
  204. {
  205. struct device_node *np, *regulators;
  206. int ret;
  207. np = pdev->dev.parent->of_node;
  208. if (!np)
  209. return 0;
  210. regulators = of_get_child_by_name(np, "regulators");
  211. if (!regulators) {
  212. dev_err(&pdev->dev, "regulators node not found\n");
  213. return -EINVAL;
  214. }
  215. ret = of_regulator_match(&pdev->dev, regulators, max8907_matches,
  216. ARRAY_SIZE(max8907_matches));
  217. of_node_put(regulators);
  218. if (ret < 0) {
  219. dev_err(&pdev->dev, "Error parsing regulator init data: %d\n",
  220. ret);
  221. return ret;
  222. }
  223. return 0;
  224. }
  225. static inline struct regulator_init_data *match_init_data(int index)
  226. {
  227. return max8907_matches[index].init_data;
  228. }
  229. static inline struct device_node *match_of_node(int index)
  230. {
  231. return max8907_matches[index].of_node;
  232. }
  233. #else
  234. static int max8907_regulator_parse_dt(struct platform_device *pdev)
  235. {
  236. return 0;
  237. }
  238. static inline struct regulator_init_data *match_init_data(int index)
  239. {
  240. return NULL;
  241. }
  242. static inline struct device_node *match_of_node(int index)
  243. {
  244. return NULL;
  245. }
  246. #endif
  247. static int max8907_regulator_probe(struct platform_device *pdev)
  248. {
  249. struct max8907 *max8907 = dev_get_drvdata(pdev->dev.parent);
  250. struct max8907_platform_data *pdata = dev_get_platdata(max8907->dev);
  251. int ret;
  252. struct max8907_regulator *pmic;
  253. unsigned int val;
  254. int i;
  255. struct regulator_config config = {};
  256. struct regulator_init_data *idata;
  257. const char *mbatt_rail_name = NULL;
  258. ret = max8907_regulator_parse_dt(pdev);
  259. if (ret)
  260. return ret;
  261. pmic = devm_kzalloc(&pdev->dev, sizeof(*pmic), GFP_KERNEL);
  262. if (!pmic)
  263. return -ENOMEM;
  264. platform_set_drvdata(pdev, pmic);
  265. memcpy(pmic->desc, max8907_regulators, sizeof(pmic->desc));
  266. /* Backwards compatibility with MAX8907B; SD1 uses different voltages */
  267. regmap_read(max8907->regmap_gen, MAX8907_REG_II2RR, &val);
  268. if ((val & MAX8907_II2RR_VERSION_MASK) ==
  269. MAX8907_II2RR_VERSION_REV_B) {
  270. pmic->desc[MAX8907_SD1].min_uV = 637500;
  271. pmic->desc[MAX8907_SD1].uV_step = 12500;
  272. pmic->desc[MAX8907_SD1].n_voltages =
  273. (1425000 - 637500) / 12500 + 1;
  274. }
  275. for (i = 0; i < MAX8907_NUM_REGULATORS; i++) {
  276. struct regulator_dev *rdev;
  277. config.dev = pdev->dev.parent;
  278. if (pdata)
  279. idata = pdata->init_data[i];
  280. else
  281. idata = match_init_data(i);
  282. config.init_data = idata;
  283. config.driver_data = pmic;
  284. config.regmap = max8907->regmap_gen;
  285. config.of_node = match_of_node(i);
  286. switch (pmic->desc[i].id) {
  287. case MAX8907_MBATT:
  288. if (idata && idata->constraints.name)
  289. mbatt_rail_name = idata->constraints.name;
  290. else
  291. mbatt_rail_name = pmic->desc[i].name;
  292. break;
  293. case MAX8907_BBAT:
  294. case MAX8907_SDBY:
  295. case MAX8907_VRTC:
  296. idata->supply_regulator = mbatt_rail_name;
  297. break;
  298. }
  299. if (pmic->desc[i].ops == &max8907_ldo_ops) {
  300. regmap_read(config.regmap, pmic->desc[i].enable_reg,
  301. &val);
  302. if ((val & MAX8907_MASK_LDO_SEQ) !=
  303. MAX8907_MASK_LDO_SEQ)
  304. pmic->desc[i].ops = &max8907_ldo_hwctl_ops;
  305. } else if (pmic->desc[i].ops == &max8907_out5v_ops) {
  306. regmap_read(config.regmap, pmic->desc[i].enable_reg,
  307. &val);
  308. if ((val & (MAX8907_MASK_OUT5V_VINEN |
  309. MAX8907_MASK_OUT5V_ENSRC)) !=
  310. MAX8907_MASK_OUT5V_ENSRC)
  311. pmic->desc[i].ops = &max8907_out5v_hwctl_ops;
  312. }
  313. rdev = devm_regulator_register(&pdev->dev,
  314. &pmic->desc[i], &config);
  315. if (IS_ERR(rdev)) {
  316. dev_err(&pdev->dev,
  317. "failed to register %s regulator\n",
  318. pmic->desc[i].name);
  319. return PTR_ERR(rdev);
  320. }
  321. }
  322. return 0;
  323. }
  324. static struct platform_driver max8907_regulator_driver = {
  325. .driver = {
  326. .name = "max8907-regulator",
  327. },
  328. .probe = max8907_regulator_probe,
  329. };
  330. static int __init max8907_regulator_init(void)
  331. {
  332. return platform_driver_register(&max8907_regulator_driver);
  333. }
  334. subsys_initcall(max8907_regulator_init);
  335. static void __exit max8907_reg_exit(void)
  336. {
  337. platform_driver_unregister(&max8907_regulator_driver);
  338. }
  339. module_exit(max8907_reg_exit);
  340. MODULE_DESCRIPTION("MAX8907 regulator driver");
  341. MODULE_AUTHOR("Gyungoh Yoo <jack.yoo@maxim-ic.com>");
  342. MODULE_LICENSE("GPL v2");
  343. MODULE_ALIAS("platform:max8907-regulator");