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