2 * max14577.c - Regulator driver for the Maxim 14577
4 * Copyright (C) 2013,2014 Samsung Electronics
5 * Krzysztof Kozlowski <k.kozlowski@samsung.com>
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.
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.
18 #include <linux/module.h>
19 #include <linux/platform_device.h>
20 #include <linux/regulator/driver.h>
21 #include <linux/mfd/max14577.h>
22 #include <linux/mfd/max14577-private.h>
23 #include <linux/regulator/of_regulator.h>
25 static int max14577_reg_is_enabled(struct regulator_dev *rdev)
27 int rid = rdev_get_id(rdev);
28 struct regmap *rmap = rdev->regmap;
32 case MAX14577_CHARGER:
33 max14577_read_reg(rmap, MAX14577_CHG_REG_CHG_CTRL2, ®_data);
34 if ((reg_data & CHGCTRL2_MBCHOSTEN_MASK) == 0)
36 max14577_read_reg(rmap, MAX14577_CHG_REG_STATUS3, ®_data);
37 if ((reg_data & STATUS3_CGMBC_MASK) == 0)
39 /* MBCHOSTEN and CGMBC are on */
46 static int max14577_reg_get_current_limit(struct regulator_dev *rdev)
49 struct regmap *rmap = rdev->regmap;
51 if (rdev_get_id(rdev) != MAX14577_CHARGER)
54 max14577_read_reg(rmap, MAX14577_CHG_REG_CHG_CTRL4, ®_data);
56 if ((reg_data & CHGCTRL4_MBCICHWRCL_MASK) == 0)
57 return MAX14577_REGULATOR_CURRENT_LIMIT_MIN;
59 reg_data = ((reg_data & CHGCTRL4_MBCICHWRCH_MASK) >>
60 CHGCTRL4_MBCICHWRCH_SHIFT);
61 return MAX14577_REGULATOR_CURRENT_LIMIT_HIGH_START +
62 reg_data * MAX14577_REGULATOR_CURRENT_LIMIT_HIGH_STEP;
65 static int max14577_reg_set_current_limit(struct regulator_dev *rdev,
66 int min_uA, int max_uA)
68 int i, current_bits = 0xf;
71 if (rdev_get_id(rdev) != MAX14577_CHARGER)
74 if (min_uA > MAX14577_REGULATOR_CURRENT_LIMIT_MAX ||
75 max_uA < MAX14577_REGULATOR_CURRENT_LIMIT_MIN)
78 if (max_uA < MAX14577_REGULATOR_CURRENT_LIMIT_HIGH_START) {
79 /* Less than 200 mA, so set 90mA (turn only Low Bit off) */
80 u8 reg_data = 0x0 << CHGCTRL4_MBCICHWRCL_SHIFT;
81 return max14577_update_reg(rdev->regmap,
82 MAX14577_CHG_REG_CHG_CTRL4,
83 CHGCTRL4_MBCICHWRCL_MASK, reg_data);
86 /* max_uA is in range: <LIMIT_HIGH_START, inifinite>, so search for
87 * valid current starting from LIMIT_MAX. */
88 for (i = MAX14577_REGULATOR_CURRENT_LIMIT_MAX;
89 i >= MAX14577_REGULATOR_CURRENT_LIMIT_HIGH_START;
90 i -= MAX14577_REGULATOR_CURRENT_LIMIT_HIGH_STEP) {
95 BUG_ON(current_bits < 0); /* Cannot happen */
96 /* Turn Low Bit on (use range 200mA-950 mA) */
97 reg_data = 0x1 << CHGCTRL4_MBCICHWRCL_SHIFT;
98 /* and set proper High Bits */
99 reg_data |= current_bits << CHGCTRL4_MBCICHWRCH_SHIFT;
101 return max14577_update_reg(rdev->regmap, MAX14577_CHG_REG_CHG_CTRL4,
102 CHGCTRL4_MBCICHWRCL_MASK | CHGCTRL4_MBCICHWRCH_MASK,
106 static struct regulator_ops max14577_safeout_ops = {
107 .is_enabled = regulator_is_enabled_regmap,
108 .enable = regulator_enable_regmap,
109 .disable = regulator_disable_regmap,
110 .list_voltage = regulator_list_voltage_linear,
113 static struct regulator_ops max14577_charger_ops = {
114 .is_enabled = max14577_reg_is_enabled,
115 .enable = regulator_enable_regmap,
116 .disable = regulator_disable_regmap,
117 .get_current_limit = max14577_reg_get_current_limit,
118 .set_current_limit = max14577_reg_set_current_limit,
121 static const struct regulator_desc supported_regulators[] = {
122 [MAX14577_SAFEOUT] = {
124 .id = MAX14577_SAFEOUT,
125 .ops = &max14577_safeout_ops,
126 .type = REGULATOR_VOLTAGE,
127 .owner = THIS_MODULE,
129 .min_uV = MAX14577_REGULATOR_SAFEOUT_VOLTAGE,
130 .enable_reg = MAX14577_REG_CONTROL2,
131 .enable_mask = CTRL2_SFOUTORD_MASK,
133 [MAX14577_CHARGER] = {
135 .id = MAX14577_CHARGER,
136 .ops = &max14577_charger_ops,
137 .type = REGULATOR_CURRENT,
138 .owner = THIS_MODULE,
139 .enable_reg = MAX14577_CHG_REG_CHG_CTRL2,
140 .enable_mask = CHGCTRL2_MBCHOSTEN_MASK,
145 static struct of_regulator_match max14577_regulator_matches[] = {
146 { .name = "SAFEOUT", },
147 { .name = "CHARGER", },
150 static int max14577_regulator_dt_parse_pdata(struct platform_device *pdev)
153 struct device_node *np;
155 np = of_get_child_by_name(pdev->dev.parent->of_node, "regulators");
157 dev_err(&pdev->dev, "Failed to get child OF node for regulators\n");
161 ret = of_regulator_match(&pdev->dev, np, max14577_regulator_matches,
164 dev_err(&pdev->dev, "Error parsing regulator init data: %d\n", ret);
173 static inline struct regulator_init_data *match_init_data(int index)
175 return max14577_regulator_matches[index].init_data;
178 static inline struct device_node *match_of_node(int index)
180 return max14577_regulator_matches[index].of_node;
182 #else /* CONFIG_OF */
183 static int max14577_regulator_dt_parse_pdata(struct platform_device *pdev)
187 static inline struct regulator_init_data *match_init_data(int index)
192 static inline struct device_node *match_of_node(int index)
196 #endif /* CONFIG_OF */
199 static int max14577_regulator_probe(struct platform_device *pdev)
201 struct max14577 *max14577 = dev_get_drvdata(pdev->dev.parent);
202 struct max14577_platform_data *pdata = dev_get_platdata(max14577->dev);
204 struct regulator_config config = {};
206 ret = max14577_regulator_dt_parse_pdata(pdev);
210 config.dev = &pdev->dev;
211 config.regmap = max14577->regmap;
213 for (i = 0; i < ARRAY_SIZE(supported_regulators); i++) {
214 struct regulator_dev *regulator;
216 * Index of supported_regulators[] is also the id and must
217 * match index of pdata->regulators[].
219 if (pdata && pdata->regulators) {
220 config.init_data = pdata->regulators[i].initdata;
221 config.of_node = pdata->regulators[i].of_node;
223 config.init_data = match_init_data(i);
224 config.of_node = match_of_node(i);
227 regulator = devm_regulator_register(&pdev->dev,
228 &supported_regulators[i], &config);
229 if (IS_ERR(regulator)) {
230 ret = PTR_ERR(regulator);
232 "Regulator init failed for ID %d with error: %d\n",
241 static struct platform_driver max14577_regulator_driver = {
243 .owner = THIS_MODULE,
244 .name = "max14577-regulator",
246 .probe = max14577_regulator_probe,
249 static int __init max14577_regulator_init(void)
251 BUILD_BUG_ON(MAX14577_REGULATOR_CURRENT_LIMIT_HIGH_START +
252 MAX14577_REGULATOR_CURRENT_LIMIT_HIGH_STEP * 0xf !=
253 MAX14577_REGULATOR_CURRENT_LIMIT_MAX);
254 BUILD_BUG_ON(ARRAY_SIZE(supported_regulators) != MAX14577_REG_MAX);
256 return platform_driver_register(&max14577_regulator_driver);
258 subsys_initcall(max14577_regulator_init);
260 static void __exit max14577_regulator_exit(void)
262 platform_driver_unregister(&max14577_regulator_driver);
264 module_exit(max14577_regulator_exit);
266 MODULE_AUTHOR("Krzysztof Kozlowski <k.kozlowski@samsung.com>");
267 MODULE_DESCRIPTION("MAXIM 14577 regulator driver");
268 MODULE_LICENSE("GPL");
269 MODULE_ALIAS("platform:max14577-regulator");