]>
Commit | Line | Data |
---|---|---|
e6ff10f2 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
c860476b AW |
2 | // |
3 | // DA9121 Single-channel dual-phase 10A buck converter | |
4 | // | |
5 | // Copyright (C) 2020 Axis Communications AB | |
6 | // | |
7 | // DA9130 Single-channel dual-phase 10A buck converter (Automotive) | |
8 | // DA9217 Single-channel dual-phase 6A buck converter | |
9 | // DA9122 Dual-channel single-phase 5A buck converter | |
10 | // DA9131 Dual-channel single-phase 5A buck converter (Automotive) | |
11 | // DA9220 Dual-channel single-phase 3A buck converter | |
12 | // DA9132 Dual-channel single-phase 3A buck converter (Automotive) | |
13 | // | |
14 | // Copyright (C) 2020 Dialog Semiconductor | |
e6ff10f2 VW |
15 | |
16 | #include <linux/of_device.h> | |
46c413d5 | 17 | #include <linux/of_gpio.h> |
e6ff10f2 VW |
18 | #include <linux/regulator/of_regulator.h> |
19 | #include <linux/regulator/machine.h> | |
20 | #include <linux/regulator/driver.h> | |
21 | #include <linux/module.h> | |
22 | #include <linux/regmap.h> | |
23 | #include <linux/err.h> | |
24 | #include <linux/i2c.h> | |
c860476b AW |
25 | #include <linux/regulator/da9121.h> |
26 | ||
86f162c9 | 27 | #include "da9121-regulator.h" |
e6ff10f2 | 28 | |
f3fbd556 AW |
29 | /* Chip data */ |
30 | struct da9121 { | |
31 | struct device *dev; | |
46c413d5 | 32 | struct da9121_pdata *pdata; |
c860476b | 33 | struct regmap *regmap; |
9929900d | 34 | struct regulator_dev *rdev[DA9121_IDX_MAX]; |
f3fbd556 AW |
35 | int variant_id; |
36 | }; | |
37 | ||
46c413d5 AW |
38 | /* Define ranges for different variants, enabling translation to/from |
39 | * registers. Maximums give scope to allow for transients. | |
40 | */ | |
41 | struct da9121_range { | |
42 | int val_min; | |
43 | int val_max; | |
44 | int val_stp; | |
45 | int reg_min; | |
46 | int reg_max; | |
47 | }; | |
48 | ||
49 | struct da9121_range da9121_10A_2phase_current = { | |
50 | .val_min = 7000000, | |
51 | .val_max = 20000000, | |
52 | .val_stp = 1000000, | |
53 | .reg_min = 1, | |
54 | .reg_max = 14, | |
55 | }; | |
56 | ||
57 | struct da9121_range da9121_6A_2phase_current = { | |
58 | .val_min = 7000000, | |
59 | .val_max = 12000000, | |
60 | .val_stp = 1000000, | |
61 | .reg_min = 1, | |
62 | .reg_max = 6, | |
63 | }; | |
64 | ||
65 | struct da9121_range da9121_5A_1phase_current = { | |
66 | .val_min = 3500000, | |
67 | .val_max = 10000000, | |
68 | .val_stp = 500000, | |
69 | .reg_min = 1, | |
70 | .reg_max = 14, | |
71 | }; | |
72 | ||
73 | struct da9121_range da9121_3A_1phase_current = { | |
74 | .val_min = 3500000, | |
75 | .val_max = 6000000, | |
76 | .val_stp = 500000, | |
77 | .reg_min = 1, | |
78 | .reg_max = 6, | |
79 | }; | |
80 | ||
81 | struct da9121_variant_info { | |
82 | int num_bucks; | |
83 | int num_phases; | |
84 | struct da9121_range *current_range; | |
85 | }; | |
86 | ||
87 | static const struct da9121_variant_info variant_parameters[] = { | |
88 | { 1, 2, &da9121_10A_2phase_current }, //DA9121_TYPE_DA9121_DA9130 | |
89 | { 2, 1, &da9121_3A_1phase_current }, //DA9121_TYPE_DA9220_DA9132 | |
90 | { 2, 1, &da9121_5A_1phase_current }, //DA9121_TYPE_DA9122_DA9131 | |
91 | { 1, 2, &da9121_6A_2phase_current }, //DA9121_TYPE_DA9217 | |
92 | }; | |
93 | ||
e6ff10f2 VW |
94 | static const struct regulator_ops da9121_buck_ops = { |
95 | .enable = regulator_enable_regmap, | |
96 | .disable = regulator_disable_regmap, | |
97 | .is_enabled = regulator_is_enabled_regmap, | |
98 | .set_voltage_sel = regulator_set_voltage_sel_regmap, | |
99 | .get_voltage_sel = regulator_get_voltage_sel_regmap, | |
100 | .list_voltage = regulator_list_voltage_linear, | |
101 | }; | |
102 | ||
91863239 AW |
103 | static struct of_regulator_match da9121_matches[] = { |
104 | [DA9121_IDX_BUCK1] = { .name = "buck1" }, | |
105 | [DA9121_IDX_BUCK2] = { .name = "buck2" }, | |
106 | }; | |
107 | ||
46c413d5 AW |
108 | static int da9121_of_parse_cb(struct device_node *np, |
109 | const struct regulator_desc *desc, | |
110 | struct regulator_config *config) | |
111 | { | |
112 | struct da9121 *chip = config->driver_data; | |
113 | struct da9121_pdata *pdata; | |
114 | struct gpio_desc *ena_gpiod; | |
115 | ||
116 | if (chip->pdata == NULL) { | |
117 | pdata = devm_kzalloc(chip->dev, sizeof(*pdata), GFP_KERNEL); | |
118 | if (!pdata) | |
119 | return -ENOMEM; | |
120 | } else { | |
121 | pdata = chip->pdata; | |
122 | } | |
123 | ||
124 | pdata->num_buck++; | |
125 | ||
126 | if (pdata->num_buck > variant_parameters[chip->variant_id].num_bucks) { | |
127 | dev_err(chip->dev, "Error: excessive regulators for device\n"); | |
128 | return -ENODEV; | |
129 | } | |
130 | ||
131 | ena_gpiod = fwnode_gpiod_get_index(of_fwnode_handle(np), "enable", 0, | |
132 | GPIOD_OUT_HIGH | | |
133 | GPIOD_FLAGS_BIT_NONEXCLUSIVE, | |
134 | "da9121-enable"); | |
135 | if (!IS_ERR(ena_gpiod)) | |
136 | config->ena_gpiod = ena_gpiod; | |
137 | ||
138 | if (variant_parameters[chip->variant_id].num_bucks == 2) { | |
139 | uint32_t ripple_cancel; | |
140 | uint32_t ripple_reg; | |
141 | int ret; | |
142 | ||
143 | if (of_property_read_u32(da9121_matches[pdata->num_buck].of_node, | |
144 | "dlg,ripple-cancel", &ripple_cancel)) { | |
145 | if (pdata->num_buck > 1) | |
146 | ripple_reg = DA9xxx_REG_BUCK_BUCK2_7; | |
147 | else | |
148 | ripple_reg = DA9121_REG_BUCK_BUCK1_7; | |
149 | ||
150 | ret = regmap_update_bits(chip->regmap, ripple_reg, | |
151 | DA9xxx_MASK_BUCK_BUCKx_7_CHx_RIPPLE_CANCEL, | |
152 | ripple_cancel); | |
153 | if (ret < 0) | |
154 | dev_err(chip->dev, "Cannot set ripple mode, err: %d\n", ret); | |
155 | } | |
156 | } | |
157 | ||
158 | return 0; | |
159 | } | |
160 | ||
c860476b AW |
161 | #define DA9121_MIN_MV 300 |
162 | #define DA9121_MAX_MV 1900 | |
163 | #define DA9121_STEP_MV 10 | |
164 | #define DA9121_MIN_SEL (DA9121_MIN_MV / DA9121_STEP_MV) | |
165 | #define DA9121_N_VOLTAGES (((DA9121_MAX_MV - DA9121_MIN_MV) / DA9121_STEP_MV) \ | |
166 | + 1 + DA9121_MIN_SEL) | |
167 | ||
e6ff10f2 | 168 | static const struct regulator_desc da9121_reg = { |
91863239 | 169 | .id = DA9121_IDX_BUCK1, |
e6ff10f2 VW |
170 | .name = "da9121", |
171 | .of_match = "buck1", | |
46c413d5 | 172 | .of_parse_cb = da9121_of_parse_cb, |
e6ff10f2 | 173 | .owner = THIS_MODULE, |
91863239 | 174 | .regulators_node = of_match_ptr("regulators"), |
e6ff10f2 VW |
175 | .ops = &da9121_buck_ops, |
176 | .type = REGULATOR_VOLTAGE, | |
177 | .n_voltages = DA9121_N_VOLTAGES, | |
178 | .min_uV = DA9121_MIN_MV * 1000, | |
179 | .uV_step = DA9121_STEP_MV * 1000, | |
180 | .linear_min_sel = DA9121_MIN_SEL, | |
86f162c9 AW |
181 | .vsel_reg = DA9121_REG_BUCK_BUCK1_5, |
182 | .vsel_mask = DA9121_MASK_BUCK_BUCKx_5_CHx_A_VOUT, | |
183 | .enable_reg = DA9121_REG_BUCK_BUCK1_0, | |
184 | .enable_mask = DA9121_MASK_BUCK_BUCKx_0_CHx_EN, | |
e6ff10f2 VW |
185 | /* Default value of BUCK_BUCK1_0.CH1_SRC_DVC_UP */ |
186 | .ramp_delay = 20000, | |
187 | /* tBUCK_EN */ | |
188 | .enable_time = 20, | |
189 | }; | |
190 | ||
91863239 AW |
191 | static const struct regulator_desc da9220_reg[2] = { |
192 | { | |
193 | .id = DA9121_IDX_BUCK1, | |
194 | .name = "DA9220/DA9132 BUCK1", | |
195 | .of_match = "buck1", | |
46c413d5 | 196 | .of_parse_cb = da9121_of_parse_cb, |
91863239 AW |
197 | .owner = THIS_MODULE, |
198 | .regulators_node = of_match_ptr("regulators"), | |
199 | .ops = &da9121_buck_ops, | |
200 | .type = REGULATOR_VOLTAGE, | |
201 | .n_voltages = DA9121_N_VOLTAGES, | |
202 | .min_uV = DA9121_MIN_MV * 1000, | |
203 | .uV_step = DA9121_STEP_MV * 1000, | |
204 | .linear_min_sel = DA9121_MIN_SEL, | |
205 | .enable_reg = DA9121_REG_BUCK_BUCK1_0, | |
206 | .enable_mask = DA9121_MASK_BUCK_BUCKx_0_CHx_EN, | |
207 | .vsel_reg = DA9121_REG_BUCK_BUCK1_5, | |
208 | .vsel_mask = DA9121_MASK_BUCK_BUCKx_5_CHx_A_VOUT, | |
209 | }, | |
210 | { | |
211 | .id = DA9121_IDX_BUCK2, | |
212 | .name = "DA9220/DA9132 BUCK2", | |
213 | .of_match = "buck2", | |
46c413d5 | 214 | .of_parse_cb = da9121_of_parse_cb, |
91863239 AW |
215 | .owner = THIS_MODULE, |
216 | .regulators_node = of_match_ptr("regulators"), | |
217 | .ops = &da9121_buck_ops, | |
218 | .type = REGULATOR_VOLTAGE, | |
219 | .n_voltages = DA9121_N_VOLTAGES, | |
220 | .min_uV = DA9121_MIN_MV * 1000, | |
221 | .uV_step = DA9121_STEP_MV * 1000, | |
222 | .linear_min_sel = DA9121_MIN_SEL, | |
223 | .enable_reg = DA9xxx_REG_BUCK_BUCK2_0, | |
224 | .enable_mask = DA9121_MASK_BUCK_BUCKx_0_CHx_EN, | |
225 | .vsel_reg = DA9xxx_REG_BUCK_BUCK2_5, | |
226 | .vsel_mask = DA9121_MASK_BUCK_BUCKx_5_CHx_A_VOUT, | |
227 | } | |
228 | }; | |
229 | ||
230 | static const struct regulator_desc da9122_reg[2] = { | |
231 | { | |
232 | .id = DA9121_IDX_BUCK1, | |
233 | .name = "DA9122/DA9131 BUCK1", | |
234 | .of_match = "buck1", | |
46c413d5 | 235 | .of_parse_cb = da9121_of_parse_cb, |
91863239 AW |
236 | .owner = THIS_MODULE, |
237 | .regulators_node = of_match_ptr("regulators"), | |
238 | .ops = &da9121_buck_ops, | |
239 | .type = REGULATOR_VOLTAGE, | |
240 | .n_voltages = DA9121_N_VOLTAGES, | |
241 | .min_uV = DA9121_MIN_MV * 1000, | |
242 | .uV_step = DA9121_STEP_MV * 1000, | |
243 | .linear_min_sel = DA9121_MIN_SEL, | |
244 | .enable_reg = DA9121_REG_BUCK_BUCK1_0, | |
245 | .enable_mask = DA9121_MASK_BUCK_BUCKx_0_CHx_EN, | |
246 | .vsel_reg = DA9121_REG_BUCK_BUCK1_5, | |
247 | .vsel_mask = DA9121_MASK_BUCK_BUCKx_5_CHx_A_VOUT, | |
248 | }, | |
249 | { | |
250 | .id = DA9121_IDX_BUCK2, | |
251 | .name = "DA9122/DA9131 BUCK2", | |
252 | .of_match = "buck2", | |
46c413d5 | 253 | .of_parse_cb = da9121_of_parse_cb, |
91863239 AW |
254 | .owner = THIS_MODULE, |
255 | .regulators_node = of_match_ptr("regulators"), | |
256 | .ops = &da9121_buck_ops, | |
257 | .type = REGULATOR_VOLTAGE, | |
258 | .n_voltages = DA9121_N_VOLTAGES, | |
259 | .min_uV = DA9121_MIN_MV * 1000, | |
260 | .uV_step = DA9121_STEP_MV * 1000, | |
261 | .linear_min_sel = DA9121_MIN_SEL, | |
262 | .enable_reg = DA9xxx_REG_BUCK_BUCK2_0, | |
263 | .enable_mask = DA9121_MASK_BUCK_BUCKx_0_CHx_EN, | |
264 | .vsel_reg = DA9xxx_REG_BUCK_BUCK2_5, | |
265 | .vsel_mask = DA9121_MASK_BUCK_BUCKx_5_CHx_A_VOUT, | |
266 | } | |
267 | }; | |
268 | ||
269 | static const struct regulator_desc da9217_reg = { | |
270 | .id = DA9121_IDX_BUCK1, | |
271 | .name = "DA9217 BUCK1", | |
272 | .of_match = "buck1", | |
46c413d5 | 273 | .of_parse_cb = da9121_of_parse_cb, |
91863239 AW |
274 | .owner = THIS_MODULE, |
275 | .regulators_node = of_match_ptr("regulators"), | |
276 | .ops = &da9121_buck_ops, | |
277 | .type = REGULATOR_VOLTAGE, | |
278 | .n_voltages = DA9121_N_VOLTAGES, | |
279 | .min_uV = DA9121_MIN_MV * 1000, | |
280 | .uV_step = DA9121_STEP_MV * 1000, | |
281 | .linear_min_sel = DA9121_MIN_SEL, | |
282 | .enable_reg = DA9121_REG_BUCK_BUCK1_0, | |
283 | .enable_mask = DA9121_MASK_BUCK_BUCKx_0_CHx_EN, | |
284 | .vsel_reg = DA9121_REG_BUCK_BUCK1_5, | |
285 | .vsel_mask = DA9121_MASK_BUCK_BUCKx_5_CHx_A_VOUT, | |
286 | }; | |
287 | ||
288 | static const struct regulator_desc *local_da9121_regulators[][DA9121_IDX_MAX] = { | |
289 | [DA9121_TYPE_DA9121_DA9130] = { &da9121_reg, NULL }, | |
290 | [DA9121_TYPE_DA9220_DA9132] = { &da9220_reg[0], &da9220_reg[1] }, | |
291 | [DA9121_TYPE_DA9122_DA9131] = { &da9122_reg[0], &da9122_reg[1] }, | |
292 | [DA9121_TYPE_DA9217] = { &da9217_reg, NULL }, | |
293 | }; | |
294 | ||
9929900d AW |
295 | static int da9121_set_regulator_config(struct da9121 *chip) |
296 | { | |
297 | struct regulator_config config = { }; | |
298 | unsigned int max_matches = variant_parameters[chip->variant_id].num_bucks; | |
299 | int ret = 0; | |
300 | int i; | |
301 | ||
302 | for (i = 0; i < max_matches; i++) { | |
303 | const struct regulator_desc *regl_desc = | |
304 | local_da9121_regulators[chip->variant_id][i]; | |
305 | ||
306 | config.dev = chip->dev; | |
307 | config.driver_data = chip; | |
308 | config.regmap = chip->regmap; | |
309 | ||
310 | chip->rdev[i] = devm_regulator_register(chip->dev, | |
311 | regl_desc, &config); | |
312 | if (IS_ERR(chip->rdev[i])) { | |
313 | dev_err(chip->dev, "Failed to register regulator %s, %d/%d\n", | |
314 | regl_desc->name, (i+1), max_matches); | |
315 | ret = PTR_ERR(chip->rdev[i]); | |
316 | goto error; | |
317 | } | |
318 | } | |
319 | ||
320 | error: | |
321 | return ret; | |
322 | } | |
323 | ||
c860476b AW |
324 | /* DA9121 chip register model */ |
325 | static const struct regmap_range da9121_1ch_readable_ranges[] = { | |
326 | regmap_reg_range(DA9121_REG_SYS_STATUS_0, DA9121_REG_SYS_MASK_3), | |
327 | regmap_reg_range(DA9121_REG_SYS_CONFIG_2, DA9121_REG_SYS_CONFIG_3), | |
328 | regmap_reg_range(DA9121_REG_SYS_GPIO0_0, DA9121_REG_SYS_GPIO2_1), | |
329 | regmap_reg_range(DA9121_REG_BUCK_BUCK1_0, DA9121_REG_BUCK_BUCK1_6), | |
330 | regmap_reg_range(DA9121_REG_OTP_DEVICE_ID, DA9121_REG_OTP_CONFIG_ID), | |
331 | }; | |
332 | ||
333 | static const struct regmap_access_table da9121_1ch_readable_table = { | |
334 | .yes_ranges = da9121_1ch_readable_ranges, | |
335 | .n_yes_ranges = ARRAY_SIZE(da9121_1ch_readable_ranges), | |
336 | }; | |
337 | ||
338 | static const struct regmap_range da9121_2ch_readable_ranges[] = { | |
339 | regmap_reg_range(DA9121_REG_SYS_STATUS_0, DA9121_REG_SYS_MASK_3), | |
340 | regmap_reg_range(DA9121_REG_SYS_CONFIG_2, DA9121_REG_SYS_CONFIG_3), | |
341 | regmap_reg_range(DA9121_REG_SYS_GPIO0_0, DA9121_REG_SYS_GPIO2_1), | |
342 | regmap_reg_range(DA9121_REG_BUCK_BUCK1_0, DA9121_REG_BUCK_BUCK1_7), | |
343 | regmap_reg_range(DA9xxx_REG_BUCK_BUCK2_0, DA9xxx_REG_BUCK_BUCK2_7), | |
344 | regmap_reg_range(DA9121_REG_OTP_DEVICE_ID, DA9121_REG_OTP_CONFIG_ID), | |
345 | }; | |
346 | ||
347 | static const struct regmap_access_table da9121_2ch_readable_table = { | |
348 | .yes_ranges = da9121_2ch_readable_ranges, | |
349 | .n_yes_ranges = ARRAY_SIZE(da9121_2ch_readable_ranges), | |
350 | }; | |
351 | ||
352 | static const struct regmap_range da9121_1ch_writeable_ranges[] = { | |
353 | regmap_reg_range(DA9121_REG_SYS_EVENT_0, DA9121_REG_SYS_MASK_3), | |
354 | regmap_reg_range(DA9121_REG_SYS_CONFIG_2, DA9121_REG_SYS_CONFIG_3), | |
355 | regmap_reg_range(DA9121_REG_SYS_GPIO0_0, DA9121_REG_SYS_GPIO2_1), | |
356 | regmap_reg_range(DA9121_REG_BUCK_BUCK1_0, DA9121_REG_BUCK_BUCK1_2), | |
357 | regmap_reg_range(DA9121_REG_BUCK_BUCK1_4, DA9121_REG_BUCK_BUCK1_6), | |
358 | }; | |
359 | ||
360 | static const struct regmap_access_table da9121_1ch_writeable_table = { | |
361 | .yes_ranges = da9121_1ch_writeable_ranges, | |
362 | .n_yes_ranges = ARRAY_SIZE(da9121_1ch_writeable_ranges), | |
363 | }; | |
364 | ||
365 | static const struct regmap_range da9121_2ch_writeable_ranges[] = { | |
366 | regmap_reg_range(DA9121_REG_SYS_EVENT_0, DA9121_REG_SYS_MASK_3), | |
367 | regmap_reg_range(DA9121_REG_SYS_CONFIG_2, DA9121_REG_SYS_CONFIG_3), | |
368 | regmap_reg_range(DA9121_REG_SYS_GPIO0_0, DA9121_REG_SYS_GPIO2_1), | |
369 | regmap_reg_range(DA9121_REG_BUCK_BUCK1_0, DA9121_REG_BUCK_BUCK1_2), | |
370 | regmap_reg_range(DA9121_REG_BUCK_BUCK1_4, DA9121_REG_BUCK_BUCK1_7), | |
371 | regmap_reg_range(DA9xxx_REG_BUCK_BUCK2_0, DA9xxx_REG_BUCK_BUCK2_2), | |
372 | regmap_reg_range(DA9xxx_REG_BUCK_BUCK2_4, DA9xxx_REG_BUCK_BUCK2_7), | |
373 | }; | |
374 | ||
375 | static const struct regmap_access_table da9121_2ch_writeable_table = { | |
376 | .yes_ranges = da9121_2ch_writeable_ranges, | |
377 | .n_yes_ranges = ARRAY_SIZE(da9121_2ch_writeable_ranges), | |
378 | }; | |
379 | ||
380 | ||
381 | static const struct regmap_range da9121_volatile_ranges[] = { | |
382 | regmap_reg_range(DA9121_REG_SYS_STATUS_0, DA9121_REG_SYS_EVENT_2), | |
383 | regmap_reg_range(DA9121_REG_SYS_GPIO0_0, DA9121_REG_SYS_GPIO2_1), | |
384 | regmap_reg_range(DA9121_REG_BUCK_BUCK1_0, DA9121_REG_BUCK_BUCK1_6), | |
385 | }; | |
386 | ||
387 | static const struct regmap_access_table da9121_volatile_table = { | |
388 | .yes_ranges = da9121_volatile_ranges, | |
389 | .n_yes_ranges = ARRAY_SIZE(da9121_volatile_ranges), | |
390 | }; | |
391 | ||
392 | /* DA9121 regmap config for 1 channel variants */ | |
393 | static struct regmap_config da9121_1ch_regmap_config = { | |
394 | .reg_bits = 8, | |
395 | .val_bits = 8, | |
396 | .max_register = DA9121_REG_OTP_CONFIG_ID, | |
397 | .rd_table = &da9121_1ch_readable_table, | |
398 | .wr_table = &da9121_1ch_writeable_table, | |
399 | .volatile_table = &da9121_volatile_table, | |
400 | .cache_type = REGCACHE_RBTREE, | |
401 | }; | |
402 | ||
403 | /* DA9121 regmap config for 2 channel variants */ | |
404 | static struct regmap_config da9121_2ch_regmap_config = { | |
405 | .reg_bits = 8, | |
406 | .val_bits = 8, | |
407 | .max_register = DA9121_REG_OTP_CONFIG_ID, | |
408 | .rd_table = &da9121_2ch_readable_table, | |
409 | .wr_table = &da9121_2ch_writeable_table, | |
410 | .volatile_table = &da9121_volatile_table, | |
411 | .cache_type = REGCACHE_RBTREE, | |
412 | }; | |
413 | ||
414 | static int da9121_check_device_type(struct i2c_client *i2c, struct da9121 *chip) | |
415 | { | |
416 | u32 device_id; | |
417 | u8 chip_id = chip->variant_id; | |
418 | u32 variant_id; | |
419 | u8 variant_mrc, variant_vrc; | |
420 | char *type; | |
421 | const char *name; | |
422 | bool config_match = false; | |
423 | int ret = 0; | |
424 | ||
425 | ret = regmap_read(chip->regmap, DA9121_REG_OTP_DEVICE_ID, &device_id); | |
426 | if (ret < 0) { | |
427 | dev_err(chip->dev, "Cannot read device ID: %d\n", ret); | |
428 | goto error; | |
429 | } | |
430 | ||
431 | ret = regmap_read(chip->regmap, DA9121_REG_OTP_VARIANT_ID, &variant_id); | |
432 | if (ret < 0) { | |
433 | dev_err(chip->dev, "Cannot read variant ID: %d\n", ret); | |
434 | goto error; | |
435 | } | |
436 | ||
437 | if (device_id != DA9121_DEVICE_ID) { | |
438 | dev_err(chip->dev, "Invalid device ID: 0x%02x\n", device_id); | |
439 | ret = -ENODEV; | |
440 | goto error; | |
441 | } | |
442 | ||
443 | variant_vrc = variant_id & DA9121_MASK_OTP_VARIANT_ID_VRC; | |
444 | ||
445 | switch (variant_vrc) { | |
446 | case DA9121_VARIANT_VRC: | |
447 | type = "DA9121/DA9130"; | |
448 | config_match = (chip_id == DA9121_TYPE_DA9121_DA9130); | |
449 | break; | |
450 | case DA9220_VARIANT_VRC: | |
451 | type = "DA9220/DA9132"; | |
452 | config_match = (chip_id == DA9121_TYPE_DA9220_DA9132); | |
453 | break; | |
454 | case DA9122_VARIANT_VRC: | |
455 | type = "DA9122/DA9131"; | |
456 | config_match = (chip_id == DA9121_TYPE_DA9122_DA9131); | |
457 | break; | |
458 | case DA9217_VARIANT_VRC: | |
459 | type = "DA9217"; | |
460 | config_match = (chip_id == DA9121_TYPE_DA9217); | |
461 | break; | |
462 | default: | |
463 | type = "Unknown"; | |
464 | break; | |
465 | } | |
466 | ||
467 | dev_info(chip->dev, | |
468 | "Device detected (device-ID: 0x%02X, var-ID: 0x%02X, %s)\n", | |
469 | device_id, variant_id, type); | |
470 | ||
471 | if (!config_match) { | |
472 | dev_err(chip->dev, "Device tree configuration '%s' does not match detected device.\n", name); | |
473 | ret = -EINVAL; | |
474 | goto error; | |
475 | } | |
476 | ||
477 | variant_mrc = (variant_id & DA9121_MASK_OTP_VARIANT_ID_MRC) | |
478 | >> DA9121_SHIFT_OTP_VARIANT_ID_MRC; | |
479 | ||
480 | if ((device_id == DA9121_DEVICE_ID) && | |
481 | (variant_mrc < DA9121_VARIANT_MRC_BASE)) { | |
482 | dev_err(chip->dev, | |
483 | "Cannot support variant MRC: 0x%02X\n", variant_mrc); | |
484 | ret = -EINVAL; | |
485 | } | |
486 | error: | |
487 | return ret; | |
488 | } | |
489 | ||
490 | static int da9121_assign_chip_model(struct i2c_client *i2c, | |
491 | struct da9121 *chip) | |
492 | { | |
493 | struct regmap_config *regmap; | |
494 | int ret = 0; | |
495 | ||
496 | chip->dev = &i2c->dev; | |
497 | ||
498 | switch (chip->variant_id) { | |
499 | case DA9121_TYPE_DA9121_DA9130: | |
500 | fallthrough; | |
501 | case DA9121_TYPE_DA9217: | |
502 | regmap = &da9121_1ch_regmap_config; | |
503 | break; | |
504 | case DA9121_TYPE_DA9122_DA9131: | |
505 | fallthrough; | |
506 | case DA9121_TYPE_DA9220_DA9132: | |
507 | regmap = &da9121_2ch_regmap_config; | |
508 | break; | |
509 | } | |
510 | ||
91863239 AW |
511 | /* Set these up for of_regulator_match call which may want .of_map_modes */ |
512 | da9121_matches[0].desc = local_da9121_regulators[chip->variant_id][0]; | |
513 | da9121_matches[1].desc = local_da9121_regulators[chip->variant_id][1]; | |
514 | ||
c860476b AW |
515 | chip->regmap = devm_regmap_init_i2c(i2c, regmap); |
516 | if (IS_ERR(chip->regmap)) { | |
517 | ret = PTR_ERR(chip->regmap); | |
518 | dev_err(chip->dev, "Failed to configure a register map: %d\n", | |
519 | ret); | |
520 | } | |
521 | ||
522 | ret = da9121_check_device_type(i2c, chip); | |
523 | ||
524 | return ret; | |
525 | } | |
526 | ||
e6ff10f2 | 527 | static const struct of_device_id da9121_dt_ids[] = { |
f3fbd556 AW |
528 | { .compatible = "dlg,da9121", .data = (void *) DA9121_TYPE_DA9121_DA9130 }, |
529 | { .compatible = "dlg,da9130", .data = (void *) DA9121_TYPE_DA9121_DA9130 }, | |
530 | { .compatible = "dlg,da9217", .data = (void *) DA9121_TYPE_DA9217 }, | |
531 | { .compatible = "dlg,da9122", .data = (void *) DA9121_TYPE_DA9122_DA9131 }, | |
532 | { .compatible = "dlg,da9131", .data = (void *) DA9121_TYPE_DA9122_DA9131 }, | |
533 | { .compatible = "dlg,da9220", .data = (void *) DA9121_TYPE_DA9220_DA9132 }, | |
534 | { .compatible = "dlg,da9132", .data = (void *) DA9121_TYPE_DA9220_DA9132 }, | |
e6ff10f2 VW |
535 | { } |
536 | }; | |
537 | MODULE_DEVICE_TABLE(of, da9121_dt_ids); | |
538 | ||
f3fbd556 AW |
539 | static inline int da9121_of_get_id(struct device *dev) |
540 | { | |
541 | const struct of_device_id *id = of_match_device(da9121_dt_ids, dev); | |
542 | ||
543 | if (!id) { | |
544 | dev_err(dev, "%s: Failed\n", __func__); | |
545 | return -EINVAL; | |
546 | } | |
547 | return (uintptr_t)id->data; | |
548 | } | |
549 | ||
e6ff10f2 VW |
550 | static int da9121_i2c_probe(struct i2c_client *i2c, |
551 | const struct i2c_device_id *id) | |
552 | { | |
f3fbd556 AW |
553 | struct da9121 *chip; |
554 | int ret = 0; | |
e6ff10f2 | 555 | |
f3fbd556 AW |
556 | chip = devm_kzalloc(&i2c->dev, sizeof(struct da9121), GFP_KERNEL); |
557 | if (!chip) { | |
558 | ret = -ENOMEM; | |
559 | goto error; | |
560 | } | |
561 | ||
46c413d5 | 562 | chip->pdata = i2c->dev.platform_data; |
f3fbd556 AW |
563 | chip->variant_id = da9121_of_get_id(&i2c->dev); |
564 | ||
c860476b AW |
565 | ret = da9121_assign_chip_model(i2c, chip); |
566 | if (ret < 0) | |
567 | goto error; | |
e6ff10f2 | 568 | |
9929900d | 569 | ret = da9121_set_regulator_config(chip); |
e6ff10f2 | 570 | |
f3fbd556 AW |
571 | error: |
572 | return ret; | |
e6ff10f2 VW |
573 | } |
574 | ||
575 | static const struct i2c_device_id da9121_i2c_id[] = { | |
f3fbd556 AW |
576 | {"da9121", DA9121_TYPE_DA9121_DA9130}, |
577 | {"da9130", DA9121_TYPE_DA9121_DA9130}, | |
578 | {"da9217", DA9121_TYPE_DA9217}, | |
579 | {"da9122", DA9121_TYPE_DA9122_DA9131}, | |
580 | {"da9131", DA9121_TYPE_DA9122_DA9131}, | |
581 | {"da9220", DA9121_TYPE_DA9220_DA9132}, | |
582 | {"da9132", DA9121_TYPE_DA9220_DA9132}, | |
e6ff10f2 VW |
583 | {}, |
584 | }; | |
585 | MODULE_DEVICE_TABLE(i2c, da9121_i2c_id); | |
586 | ||
587 | static struct i2c_driver da9121_regulator_driver = { | |
588 | .driver = { | |
589 | .name = "da9121", | |
590 | .of_match_table = of_match_ptr(da9121_dt_ids), | |
591 | }, | |
592 | .probe = da9121_i2c_probe, | |
593 | .id_table = da9121_i2c_id, | |
594 | }; | |
595 | ||
596 | module_i2c_driver(da9121_regulator_driver); | |
597 | ||
598 | MODULE_LICENSE("GPL v2"); |