]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - drivers/regulator/max14577.c
Merge remote-tracking branches 'spi/topic/s3c64xx', 'spi/topic/sc18is602', 'spi/topic...
[mirror_ubuntu-jammy-kernel.git] / drivers / regulator / max14577.c
1 /*
2 * max14577.c - Regulator driver for the Maxim 14577
3 *
4 * Copyright (C) 2013 Samsung Electronics
5 * Krzysztof Kozlowski <k.kozlowski@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
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>
24
25 struct max14577_regulator {
26 struct device *dev;
27 struct max14577 *max14577;
28 struct regulator_dev **regulators;
29 };
30
31 static int max14577_reg_is_enabled(struct regulator_dev *rdev)
32 {
33 int rid = rdev_get_id(rdev);
34 struct regmap *rmap = rdev->regmap;
35 u8 reg_data;
36
37 switch (rid) {
38 case MAX14577_CHARGER:
39 max14577_read_reg(rmap, MAX14577_CHG_REG_CHG_CTRL2, &reg_data);
40 if ((reg_data & CHGCTRL2_MBCHOSTEN_MASK) == 0)
41 return 0;
42 max14577_read_reg(rmap, MAX14577_CHG_REG_STATUS3, &reg_data);
43 if ((reg_data & STATUS3_CGMBC_MASK) == 0)
44 return 0;
45 /* MBCHOSTEN and CGMBC are on */
46 return 1;
47 default:
48 return -EINVAL;
49 }
50 }
51
52 static int max14577_reg_get_current_limit(struct regulator_dev *rdev)
53 {
54 u8 reg_data;
55 struct regmap *rmap = rdev->regmap;
56
57 if (rdev_get_id(rdev) != MAX14577_CHARGER)
58 return -EINVAL;
59
60 max14577_read_reg(rmap, MAX14577_CHG_REG_CHG_CTRL4, &reg_data);
61
62 if ((reg_data & CHGCTRL4_MBCICHWRCL_MASK) == 0)
63 return MAX14577_REGULATOR_CURRENT_LIMIT_MIN;
64
65 reg_data = ((reg_data & CHGCTRL4_MBCICHWRCH_MASK) >>
66 CHGCTRL4_MBCICHWRCH_SHIFT);
67 return MAX14577_REGULATOR_CURRENT_LIMIT_HIGH_START +
68 reg_data * MAX14577_REGULATOR_CURRENT_LIMIT_HIGH_STEP;
69 }
70
71 static int max14577_reg_set_current_limit(struct regulator_dev *rdev,
72 int min_uA, int max_uA)
73 {
74 int i, current_bits = 0xf;
75 u8 reg_data;
76
77 if (rdev_get_id(rdev) != MAX14577_CHARGER)
78 return -EINVAL;
79
80 if (min_uA > MAX14577_REGULATOR_CURRENT_LIMIT_MAX ||
81 max_uA < MAX14577_REGULATOR_CURRENT_LIMIT_MIN)
82 return -EINVAL;
83
84 if (max_uA < MAX14577_REGULATOR_CURRENT_LIMIT_HIGH_START) {
85 /* Less than 200 mA, so set 90mA (turn only Low Bit off) */
86 u8 reg_data = 0x0 << CHGCTRL4_MBCICHWRCL_SHIFT;
87 return max14577_update_reg(rdev->regmap,
88 MAX14577_CHG_REG_CHG_CTRL4,
89 CHGCTRL4_MBCICHWRCL_MASK, reg_data);
90 }
91
92 /* max_uA is in range: <LIMIT_HIGH_START, inifinite>, so search for
93 * valid current starting from LIMIT_MAX. */
94 for (i = MAX14577_REGULATOR_CURRENT_LIMIT_MAX;
95 i >= MAX14577_REGULATOR_CURRENT_LIMIT_HIGH_START;
96 i -= MAX14577_REGULATOR_CURRENT_LIMIT_HIGH_STEP) {
97 if (i <= max_uA)
98 break;
99 current_bits--;
100 }
101 BUG_ON(current_bits < 0); /* Cannot happen */
102 /* Turn Low Bit on (use range 200mA-950 mA) */
103 reg_data = 0x1 << CHGCTRL4_MBCICHWRCL_SHIFT;
104 /* and set proper High Bits */
105 reg_data |= current_bits << CHGCTRL4_MBCICHWRCH_SHIFT;
106
107 return max14577_update_reg(rdev->regmap, MAX14577_CHG_REG_CHG_CTRL4,
108 CHGCTRL4_MBCICHWRCL_MASK | CHGCTRL4_MBCICHWRCH_MASK,
109 reg_data);
110 }
111
112 static struct regulator_ops max14577_safeout_ops = {
113 .is_enabled = regulator_is_enabled_regmap,
114 .enable = regulator_enable_regmap,
115 .disable = regulator_disable_regmap,
116 .list_voltage = regulator_list_voltage_linear,
117 };
118
119 static struct regulator_ops max14577_charger_ops = {
120 .is_enabled = max14577_reg_is_enabled,
121 .enable = regulator_enable_regmap,
122 .disable = regulator_disable_regmap,
123 .get_current_limit = max14577_reg_get_current_limit,
124 .set_current_limit = max14577_reg_set_current_limit,
125 };
126
127 static const struct regulator_desc supported_regulators[] = {
128 [MAX14577_SAFEOUT] = {
129 .name = "SAFEOUT",
130 .id = MAX14577_SAFEOUT,
131 .ops = &max14577_safeout_ops,
132 .type = REGULATOR_VOLTAGE,
133 .owner = THIS_MODULE,
134 .n_voltages = 1,
135 .min_uV = MAX14577_REGULATOR_SAFEOUT_VOLTAGE,
136 .enable_reg = MAX14577_REG_CONTROL2,
137 .enable_mask = CTRL2_SFOUTORD_MASK,
138 },
139 [MAX14577_CHARGER] = {
140 .name = "CHARGER",
141 .id = MAX14577_CHARGER,
142 .ops = &max14577_charger_ops,
143 .type = REGULATOR_CURRENT,
144 .owner = THIS_MODULE,
145 .enable_reg = MAX14577_CHG_REG_CHG_CTRL2,
146 .enable_mask = CHGCTRL2_MBCHOSTEN_MASK,
147 },
148 };
149
150 #ifdef CONFIG_OF
151 static struct of_regulator_match max14577_regulator_matches[] = {
152 { .name = "SAFEOUT", },
153 { .name = "CHARGER", },
154 };
155
156 static int max14577_regulator_dt_parse_pdata(struct platform_device *pdev)
157 {
158 int ret;
159 struct device_node *np;
160
161 np = of_get_child_by_name(pdev->dev.parent->of_node, "regulators");
162 if (!np) {
163 dev_err(&pdev->dev, "Failed to get child OF node for regulators\n");
164 return -EINVAL;
165 }
166
167 ret = of_regulator_match(&pdev->dev, np, max14577_regulator_matches,
168 MAX14577_REG_MAX);
169 if (ret < 0)
170 dev_err(&pdev->dev, "Error parsing regulator init data: %d\n", ret);
171 else
172 ret = 0;
173
174 of_node_put(np);
175
176 return ret;
177 }
178
179 static inline struct regulator_init_data *match_init_data(int index)
180 {
181 return max14577_regulator_matches[index].init_data;
182 }
183
184 static inline struct device_node *match_of_node(int index)
185 {
186 return max14577_regulator_matches[index].of_node;
187 }
188 #else /* CONFIG_OF */
189 static int max14577_regulator_dt_parse_pdata(struct platform_device *pdev)
190 {
191 return 0;
192 }
193 static inline struct regulator_init_data *match_init_data(int index)
194 {
195 return NULL;
196 }
197
198 static inline struct device_node *match_of_node(int index)
199 {
200 return NULL;
201 }
202 #endif /* CONFIG_OF */
203
204
205 static int max14577_regulator_probe(struct platform_device *pdev)
206 {
207 struct max14577 *max14577 = dev_get_drvdata(pdev->dev.parent);
208 struct max14577_platform_data *pdata = dev_get_platdata(max14577->dev);
209 int i, ret;
210 struct regulator_config config = {};
211
212 ret = max14577_regulator_dt_parse_pdata(pdev);
213 if (ret)
214 return ret;
215
216 config.dev = &pdev->dev;
217 config.regmap = max14577->regmap;
218
219 for (i = 0; i < ARRAY_SIZE(supported_regulators); i++) {
220 struct regulator_dev *regulator;
221 /*
222 * Index of supported_regulators[] is also the id and must
223 * match index of pdata->regulators[].
224 */
225 if (pdata && pdata->regulators) {
226 config.init_data = pdata->regulators[i].initdata;
227 config.of_node = pdata->regulators[i].of_node;
228 } else {
229 config.init_data = match_init_data(i);
230 config.of_node = match_of_node(i);
231 }
232
233 regulator = devm_regulator_register(&pdev->dev,
234 &supported_regulators[i], &config);
235 if (IS_ERR(regulator)) {
236 ret = PTR_ERR(regulator);
237 dev_err(&pdev->dev,
238 "Regulator init failed for ID %d with error: %d\n",
239 i, ret);
240 return ret;
241 }
242 }
243
244 return ret;
245 }
246
247 static struct platform_driver max14577_regulator_driver = {
248 .driver = {
249 .owner = THIS_MODULE,
250 .name = "max14577-regulator",
251 },
252 .probe = max14577_regulator_probe,
253 };
254
255 static int __init max14577_regulator_init(void)
256 {
257 BUILD_BUG_ON(MAX14577_REGULATOR_CURRENT_LIMIT_HIGH_START +
258 MAX14577_REGULATOR_CURRENT_LIMIT_HIGH_STEP * 0xf !=
259 MAX14577_REGULATOR_CURRENT_LIMIT_MAX);
260 BUILD_BUG_ON(ARRAY_SIZE(supported_regulators) != MAX14577_REG_MAX);
261
262 return platform_driver_register(&max14577_regulator_driver);
263 }
264 subsys_initcall(max14577_regulator_init);
265
266 static void __exit max14577_regulator_exit(void)
267 {
268 platform_driver_unregister(&max14577_regulator_driver);
269 }
270 module_exit(max14577_regulator_exit);
271
272 MODULE_AUTHOR("Krzysztof Kozlowski <k.kozlowski@samsung.com>");
273 MODULE_DESCRIPTION("MAXIM 14577 regulator driver");
274 MODULE_LICENSE("GPL");
275 MODULE_ALIAS("platform:max14577-regulator");