]>
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> |
b4b27776 | 18 | #include <linux/gpio/consumer.h> |
e6ff10f2 VW |
19 | #include <linux/regulator/of_regulator.h> |
20 | #include <linux/regulator/machine.h> | |
21 | #include <linux/regulator/driver.h> | |
22 | #include <linux/module.h> | |
23 | #include <linux/regmap.h> | |
24 | #include <linux/err.h> | |
25 | #include <linux/i2c.h> | |
c860476b | 26 | #include <linux/regulator/da9121.h> |
40bb5b02 AW |
27 | #include <linux/interrupt.h> |
28 | #include <linux/workqueue.h> | |
c860476b | 29 | |
86f162c9 | 30 | #include "da9121-regulator.h" |
e6ff10f2 | 31 | |
f3fbd556 AW |
32 | /* Chip data */ |
33 | struct da9121 { | |
34 | struct device *dev; | |
40bb5b02 | 35 | struct delayed_work work; |
46c413d5 | 36 | struct da9121_pdata *pdata; |
c860476b | 37 | struct regmap *regmap; |
9929900d | 38 | struct regulator_dev *rdev[DA9121_IDX_MAX]; |
40bb5b02 AW |
39 | unsigned int persistent[2]; |
40 | unsigned int passive_delay; | |
41 | int chip_irq; | |
f3fbd556 | 42 | int variant_id; |
013592be | 43 | int subvariant_id; |
f3fbd556 AW |
44 | }; |
45 | ||
46c413d5 AW |
46 | /* Define ranges for different variants, enabling translation to/from |
47 | * registers. Maximums give scope to allow for transients. | |
48 | */ | |
49 | struct da9121_range { | |
50 | int val_min; | |
51 | int val_max; | |
52 | int val_stp; | |
53 | int reg_min; | |
54 | int reg_max; | |
55 | }; | |
56 | ||
c8dbf2f2 | 57 | static struct da9121_range da9121_10A_2phase_current = { |
46c413d5 AW |
58 | .val_min = 7000000, |
59 | .val_max = 20000000, | |
60 | .val_stp = 1000000, | |
61 | .reg_min = 1, | |
62 | .reg_max = 14, | |
63 | }; | |
64 | ||
c8dbf2f2 | 65 | static struct da9121_range da9121_6A_2phase_current = { |
46c413d5 AW |
66 | .val_min = 7000000, |
67 | .val_max = 12000000, | |
68 | .val_stp = 1000000, | |
69 | .reg_min = 1, | |
70 | .reg_max = 6, | |
71 | }; | |
72 | ||
c8dbf2f2 | 73 | static struct da9121_range da9121_5A_1phase_current = { |
46c413d5 AW |
74 | .val_min = 3500000, |
75 | .val_max = 10000000, | |
76 | .val_stp = 500000, | |
77 | .reg_min = 1, | |
78 | .reg_max = 14, | |
79 | }; | |
80 | ||
c8dbf2f2 | 81 | static struct da9121_range da9121_3A_1phase_current = { |
46c413d5 AW |
82 | .val_min = 3500000, |
83 | .val_max = 6000000, | |
84 | .val_stp = 500000, | |
85 | .reg_min = 1, | |
86 | .reg_max = 6, | |
87 | }; | |
88 | ||
89 | struct da9121_variant_info { | |
90 | int num_bucks; | |
91 | int num_phases; | |
92 | struct da9121_range *current_range; | |
93 | }; | |
94 | ||
95 | static const struct da9121_variant_info variant_parameters[] = { | |
96 | { 1, 2, &da9121_10A_2phase_current }, //DA9121_TYPE_DA9121_DA9130 | |
97 | { 2, 1, &da9121_3A_1phase_current }, //DA9121_TYPE_DA9220_DA9132 | |
98 | { 2, 1, &da9121_5A_1phase_current }, //DA9121_TYPE_DA9122_DA9131 | |
99 | { 1, 2, &da9121_6A_2phase_current }, //DA9121_TYPE_DA9217 | |
100 | }; | |
101 | ||
5c4b62af AW |
102 | struct da9121_field { |
103 | unsigned int reg; | |
104 | unsigned int msk; | |
105 | }; | |
106 | ||
107 | static const struct da9121_field da9121_current_field[2] = { | |
108 | { DA9121_REG_BUCK_BUCK1_2, DA9121_MASK_BUCK_BUCKx_2_CHx_ILIM }, | |
109 | { DA9xxx_REG_BUCK_BUCK2_2, DA9121_MASK_BUCK_BUCKx_2_CHx_ILIM }, | |
110 | }; | |
111 | ||
65ac9704 AW |
112 | static const struct da9121_field da9121_mode_field[2] = { |
113 | { DA9121_REG_BUCK_BUCK1_4, DA9121_MASK_BUCK_BUCKx_4_CHx_A_MODE }, | |
114 | { DA9xxx_REG_BUCK_BUCK2_4, DA9121_MASK_BUCK_BUCKx_4_CHx_A_MODE }, | |
115 | }; | |
116 | ||
40bb5b02 AW |
117 | struct status_event_data { |
118 | int buck_id; /* 0=core, 1/2-buck */ | |
119 | int reg_index; /* index for status/event/mask register selection */ | |
120 | int status_bit; /* bit masks... */ | |
121 | int event_bit; | |
122 | int mask_bit; | |
123 | unsigned long notification; /* Notification for status inception */ | |
124 | char *warn; /* if NULL, notify - otherwise dev_warn this string */ | |
125 | }; | |
126 | ||
127 | #define DA9121_STATUS(id, bank, name, notification, warning) \ | |
128 | { id, bank, \ | |
129 | DA9121_MASK_SYS_STATUS_##bank##_##name, \ | |
130 | DA9121_MASK_SYS_EVENT_##bank##_E_##name, \ | |
131 | DA9121_MASK_SYS_MASK_##bank##_M_##name, \ | |
132 | notification, warning } | |
133 | ||
134 | /* For second buck related event bits that are specific to DA9122, DA9220 variants */ | |
135 | #define DA9xxx_STATUS(id, bank, name, notification, warning) \ | |
136 | { id, bank, \ | |
137 | DA9xxx_MASK_SYS_STATUS_##bank##_##name, \ | |
138 | DA9xxx_MASK_SYS_EVENT_##bank##_E_##name, \ | |
139 | DA9xxx_MASK_SYS_MASK_##bank##_M_##name, \ | |
140 | notification, warning } | |
141 | ||
142 | /* The status signals that may need servicing, depending on device variant. | |
143 | * After assertion, they persist; so event is notified, the IRQ disabled, | |
144 | * and status polled until clear again and IRQ is reenabled. | |
145 | * | |
146 | * SG/PG1/PG2 should be set when device first powers up and should never | |
147 | * re-occur. When this driver starts, it is expected that these will have | |
148 | * self-cleared for when the IRQs are enabled, so these should never be seen. | |
149 | * If seen, the implication is that the device has reset. | |
150 | * | |
151 | * GPIO0/1/2 are not configured for use by default, so should not be seen. | |
152 | */ | |
c8dbf2f2 | 153 | static const struct status_event_data status_event_handling[] = { |
40bb5b02 AW |
154 | DA9xxx_STATUS(0, 0, SG, 0, "Handled E_SG\n"), |
155 | DA9121_STATUS(0, 0, TEMP_CRIT, (REGULATOR_EVENT_OVER_TEMP|REGULATOR_EVENT_DISABLE), NULL), | |
156 | DA9121_STATUS(0, 0, TEMP_WARN, REGULATOR_EVENT_OVER_TEMP, NULL), | |
157 | DA9121_STATUS(1, 1, PG1, 0, "Handled E_PG1\n"), | |
158 | DA9121_STATUS(1, 1, OV1, REGULATOR_EVENT_REGULATION_OUT, NULL), | |
159 | DA9121_STATUS(1, 1, UV1, REGULATOR_EVENT_UNDER_VOLTAGE, NULL), | |
160 | DA9121_STATUS(1, 1, OC1, REGULATOR_EVENT_OVER_CURRENT, NULL), | |
161 | DA9xxx_STATUS(2, 1, PG2, 0, "Handled E_PG2\n"), | |
162 | DA9xxx_STATUS(2, 1, OV2, REGULATOR_EVENT_REGULATION_OUT, NULL), | |
163 | DA9xxx_STATUS(2, 1, UV2, REGULATOR_EVENT_UNDER_VOLTAGE, NULL), | |
164 | DA9xxx_STATUS(2, 1, OC2, REGULATOR_EVENT_OVER_CURRENT, NULL), | |
165 | DA9121_STATUS(0, 2, GPIO0, 0, "Handled E_GPIO0\n"), | |
166 | DA9121_STATUS(0, 2, GPIO1, 0, "Handled E_GPIO1\n"), | |
167 | DA9121_STATUS(0, 2, GPIO2, 0, "Handled E_GPIO2\n"), | |
168 | }; | |
169 | ||
5c4b62af AW |
170 | static int da9121_get_current_limit(struct regulator_dev *rdev) |
171 | { | |
172 | struct da9121 *chip = rdev_get_drvdata(rdev); | |
173 | int id = rdev_get_id(rdev); | |
174 | struct da9121_range *range = | |
175 | variant_parameters[chip->variant_id].current_range; | |
176 | unsigned int val = 0; | |
177 | int ret = 0; | |
178 | ||
179 | ret = regmap_read(chip->regmap, da9121_current_field[id].reg, &val); | |
180 | if (ret < 0) { | |
181 | dev_err(chip->dev, "Cannot read BUCK register: %d\n", ret); | |
182 | goto error; | |
183 | } | |
184 | ||
185 | if (val < range->reg_min) { | |
186 | ret = -EACCES; | |
187 | goto error; | |
188 | } | |
189 | ||
190 | if (val > range->reg_max) { | |
191 | ret = -EINVAL; | |
192 | goto error; | |
193 | } | |
194 | ||
195 | return range->val_min + (range->val_stp * (val - range->reg_min)); | |
196 | error: | |
197 | return ret; | |
198 | } | |
199 | ||
200 | static int da9121_ceiling_selector(struct regulator_dev *rdev, | |
201 | int min, int max, | |
202 | unsigned int *selector) | |
203 | { | |
204 | struct da9121 *chip = rdev_get_drvdata(rdev); | |
205 | struct da9121_range *range = | |
206 | variant_parameters[chip->variant_id].current_range; | |
207 | unsigned int level; | |
208 | unsigned int i = 0; | |
209 | unsigned int sel = 0; | |
210 | int ret = 0; | |
211 | ||
212 | if (range->val_min > max || range->val_max < min) { | |
213 | dev_err(chip->dev, | |
214 | "Requested current out of regulator capability\n"); | |
215 | ret = -EINVAL; | |
216 | goto error; | |
217 | } | |
218 | ||
219 | level = range->val_max; | |
220 | for (i = range->reg_max; i >= range->reg_min; i--) { | |
221 | if (level <= max) { | |
222 | sel = i; | |
223 | break; | |
224 | } | |
225 | level -= range->val_stp; | |
226 | } | |
227 | ||
228 | if (level < min) { | |
229 | dev_err(chip->dev, | |
230 | "Best match falls below minimum requested current\n"); | |
231 | ret = -EINVAL; | |
232 | goto error; | |
233 | } | |
234 | ||
235 | *selector = sel; | |
236 | error: | |
237 | return ret; | |
238 | } | |
239 | ||
240 | static int da9121_set_current_limit(struct regulator_dev *rdev, | |
241 | int min_ua, int max_ua) | |
242 | { | |
243 | struct da9121 *chip = rdev_get_drvdata(rdev); | |
244 | int id = rdev_get_id(rdev); | |
245 | struct da9121_range *range = | |
246 | variant_parameters[chip->variant_id].current_range; | |
247 | unsigned int sel = 0; | |
248 | int ret = 0; | |
249 | ||
250 | if (min_ua < range->val_min || | |
251 | max_ua > range->val_max) { | |
252 | ret = -EINVAL; | |
253 | goto error; | |
254 | } | |
255 | ||
256 | ret = da9121_ceiling_selector(rdev, min_ua, max_ua, &sel); | |
257 | if (ret < 0) | |
258 | goto error; | |
259 | ||
260 | ret = regmap_update_bits(chip->regmap, | |
261 | da9121_current_field[id].reg, | |
262 | da9121_current_field[id].msk, | |
263 | (unsigned int)sel); | |
264 | if (ret < 0) | |
265 | dev_err(chip->dev, "Cannot update BUCK current limit, err: %d\n", ret); | |
266 | ||
267 | error: | |
268 | return ret; | |
269 | } | |
270 | ||
65ac9704 AW |
271 | static unsigned int da9121_map_mode(unsigned int mode) |
272 | { | |
273 | switch (mode) { | |
274 | case DA9121_BUCK_MODE_FORCE_PWM: | |
275 | return REGULATOR_MODE_FAST; | |
276 | case DA9121_BUCK_MODE_FORCE_PWM_SHEDDING: | |
277 | return REGULATOR_MODE_NORMAL; | |
278 | case DA9121_BUCK_MODE_AUTO: | |
279 | return REGULATOR_MODE_IDLE; | |
280 | case DA9121_BUCK_MODE_FORCE_PFM: | |
281 | return REGULATOR_MODE_STANDBY; | |
282 | default: | |
283 | return -EINVAL; | |
284 | } | |
285 | } | |
286 | ||
287 | static int da9121_buck_set_mode(struct regulator_dev *rdev, unsigned int mode) | |
288 | { | |
289 | struct da9121 *chip = rdev_get_drvdata(rdev); | |
290 | int id = rdev_get_id(rdev); | |
291 | unsigned int val; | |
292 | ||
293 | switch (mode) { | |
294 | case REGULATOR_MODE_FAST: | |
295 | val = DA9121_BUCK_MODE_FORCE_PWM; | |
296 | break; | |
297 | case REGULATOR_MODE_NORMAL: | |
298 | val = DA9121_BUCK_MODE_FORCE_PWM_SHEDDING; | |
299 | break; | |
300 | case REGULATOR_MODE_IDLE: | |
301 | val = DA9121_BUCK_MODE_AUTO; | |
302 | break; | |
303 | case REGULATOR_MODE_STANDBY: | |
304 | val = DA9121_BUCK_MODE_FORCE_PFM; | |
305 | break; | |
306 | default: | |
307 | return -EINVAL; | |
308 | } | |
309 | ||
310 | return regmap_update_bits(chip->regmap, | |
311 | da9121_mode_field[id].reg, | |
312 | da9121_mode_field[id].msk, | |
313 | val); | |
314 | } | |
315 | ||
316 | static unsigned int da9121_buck_get_mode(struct regulator_dev *rdev) | |
317 | { | |
318 | struct da9121 *chip = rdev_get_drvdata(rdev); | |
319 | int id = rdev_get_id(rdev); | |
320 | unsigned int val; | |
321 | int ret = 0; | |
322 | ||
323 | ret = regmap_read(chip->regmap, da9121_mode_field[id].reg, &val); | |
324 | if (ret < 0) { | |
325 | dev_err(chip->dev, "Cannot read BUCK register: %d\n", ret); | |
326 | return -EINVAL; | |
327 | } | |
328 | ||
329 | return da9121_map_mode(val & da9121_mode_field[id].msk); | |
330 | } | |
331 | ||
e6ff10f2 VW |
332 | static const struct regulator_ops da9121_buck_ops = { |
333 | .enable = regulator_enable_regmap, | |
334 | .disable = regulator_disable_regmap, | |
335 | .is_enabled = regulator_is_enabled_regmap, | |
336 | .set_voltage_sel = regulator_set_voltage_sel_regmap, | |
337 | .get_voltage_sel = regulator_get_voltage_sel_regmap, | |
338 | .list_voltage = regulator_list_voltage_linear, | |
5c4b62af AW |
339 | .get_current_limit = da9121_get_current_limit, |
340 | .set_current_limit = da9121_set_current_limit, | |
65ac9704 AW |
341 | .set_mode = da9121_buck_set_mode, |
342 | .get_mode = da9121_buck_get_mode, | |
e6ff10f2 VW |
343 | }; |
344 | ||
91863239 AW |
345 | static struct of_regulator_match da9121_matches[] = { |
346 | [DA9121_IDX_BUCK1] = { .name = "buck1" }, | |
347 | [DA9121_IDX_BUCK2] = { .name = "buck2" }, | |
348 | }; | |
349 | ||
46c413d5 AW |
350 | static int da9121_of_parse_cb(struct device_node *np, |
351 | const struct regulator_desc *desc, | |
352 | struct regulator_config *config) | |
353 | { | |
354 | struct da9121 *chip = config->driver_data; | |
355 | struct da9121_pdata *pdata; | |
356 | struct gpio_desc *ena_gpiod; | |
357 | ||
358 | if (chip->pdata == NULL) { | |
359 | pdata = devm_kzalloc(chip->dev, sizeof(*pdata), GFP_KERNEL); | |
360 | if (!pdata) | |
361 | return -ENOMEM; | |
362 | } else { | |
363 | pdata = chip->pdata; | |
364 | } | |
365 | ||
366 | pdata->num_buck++; | |
367 | ||
368 | if (pdata->num_buck > variant_parameters[chip->variant_id].num_bucks) { | |
369 | dev_err(chip->dev, "Error: excessive regulators for device\n"); | |
370 | return -ENODEV; | |
371 | } | |
372 | ||
373 | ena_gpiod = fwnode_gpiod_get_index(of_fwnode_handle(np), "enable", 0, | |
374 | GPIOD_OUT_HIGH | | |
375 | GPIOD_FLAGS_BIT_NONEXCLUSIVE, | |
376 | "da9121-enable"); | |
377 | if (!IS_ERR(ena_gpiod)) | |
378 | config->ena_gpiod = ena_gpiod; | |
379 | ||
380 | if (variant_parameters[chip->variant_id].num_bucks == 2) { | |
381 | uint32_t ripple_cancel; | |
382 | uint32_t ripple_reg; | |
383 | int ret; | |
384 | ||
9536ce63 | 385 | if (of_property_read_u32(da9121_matches[pdata->num_buck-1].of_node, |
46c413d5 AW |
386 | "dlg,ripple-cancel", &ripple_cancel)) { |
387 | if (pdata->num_buck > 1) | |
388 | ripple_reg = DA9xxx_REG_BUCK_BUCK2_7; | |
389 | else | |
390 | ripple_reg = DA9121_REG_BUCK_BUCK1_7; | |
391 | ||
392 | ret = regmap_update_bits(chip->regmap, ripple_reg, | |
393 | DA9xxx_MASK_BUCK_BUCKx_7_CHx_RIPPLE_CANCEL, | |
394 | ripple_cancel); | |
395 | if (ret < 0) | |
396 | dev_err(chip->dev, "Cannot set ripple mode, err: %d\n", ret); | |
397 | } | |
398 | } | |
399 | ||
400 | return 0; | |
401 | } | |
402 | ||
c860476b AW |
403 | #define DA9121_MIN_MV 300 |
404 | #define DA9121_MAX_MV 1900 | |
405 | #define DA9121_STEP_MV 10 | |
406 | #define DA9121_MIN_SEL (DA9121_MIN_MV / DA9121_STEP_MV) | |
407 | #define DA9121_N_VOLTAGES (((DA9121_MAX_MV - DA9121_MIN_MV) / DA9121_STEP_MV) \ | |
408 | + 1 + DA9121_MIN_SEL) | |
409 | ||
e6ff10f2 | 410 | static const struct regulator_desc da9121_reg = { |
91863239 | 411 | .id = DA9121_IDX_BUCK1, |
e6ff10f2 VW |
412 | .name = "da9121", |
413 | .of_match = "buck1", | |
46c413d5 | 414 | .of_parse_cb = da9121_of_parse_cb, |
e6ff10f2 | 415 | .owner = THIS_MODULE, |
91863239 | 416 | .regulators_node = of_match_ptr("regulators"), |
65ac9704 | 417 | .of_map_mode = da9121_map_mode, |
e6ff10f2 VW |
418 | .ops = &da9121_buck_ops, |
419 | .type = REGULATOR_VOLTAGE, | |
420 | .n_voltages = DA9121_N_VOLTAGES, | |
421 | .min_uV = DA9121_MIN_MV * 1000, | |
422 | .uV_step = DA9121_STEP_MV * 1000, | |
423 | .linear_min_sel = DA9121_MIN_SEL, | |
86f162c9 AW |
424 | .vsel_reg = DA9121_REG_BUCK_BUCK1_5, |
425 | .vsel_mask = DA9121_MASK_BUCK_BUCKx_5_CHx_A_VOUT, | |
426 | .enable_reg = DA9121_REG_BUCK_BUCK1_0, | |
427 | .enable_mask = DA9121_MASK_BUCK_BUCKx_0_CHx_EN, | |
e6ff10f2 VW |
428 | /* Default value of BUCK_BUCK1_0.CH1_SRC_DVC_UP */ |
429 | .ramp_delay = 20000, | |
430 | /* tBUCK_EN */ | |
431 | .enable_time = 20, | |
432 | }; | |
433 | ||
91863239 AW |
434 | static const struct regulator_desc da9220_reg[2] = { |
435 | { | |
436 | .id = DA9121_IDX_BUCK1, | |
437 | .name = "DA9220/DA9132 BUCK1", | |
438 | .of_match = "buck1", | |
46c413d5 | 439 | .of_parse_cb = da9121_of_parse_cb, |
91863239 AW |
440 | .owner = THIS_MODULE, |
441 | .regulators_node = of_match_ptr("regulators"), | |
65ac9704 | 442 | .of_map_mode = da9121_map_mode, |
91863239 AW |
443 | .ops = &da9121_buck_ops, |
444 | .type = REGULATOR_VOLTAGE, | |
445 | .n_voltages = DA9121_N_VOLTAGES, | |
446 | .min_uV = DA9121_MIN_MV * 1000, | |
447 | .uV_step = DA9121_STEP_MV * 1000, | |
448 | .linear_min_sel = DA9121_MIN_SEL, | |
449 | .enable_reg = DA9121_REG_BUCK_BUCK1_0, | |
450 | .enable_mask = DA9121_MASK_BUCK_BUCKx_0_CHx_EN, | |
451 | .vsel_reg = DA9121_REG_BUCK_BUCK1_5, | |
452 | .vsel_mask = DA9121_MASK_BUCK_BUCKx_5_CHx_A_VOUT, | |
453 | }, | |
454 | { | |
455 | .id = DA9121_IDX_BUCK2, | |
456 | .name = "DA9220/DA9132 BUCK2", | |
457 | .of_match = "buck2", | |
46c413d5 | 458 | .of_parse_cb = da9121_of_parse_cb, |
91863239 AW |
459 | .owner = THIS_MODULE, |
460 | .regulators_node = of_match_ptr("regulators"), | |
65ac9704 | 461 | .of_map_mode = da9121_map_mode, |
91863239 AW |
462 | .ops = &da9121_buck_ops, |
463 | .type = REGULATOR_VOLTAGE, | |
464 | .n_voltages = DA9121_N_VOLTAGES, | |
465 | .min_uV = DA9121_MIN_MV * 1000, | |
466 | .uV_step = DA9121_STEP_MV * 1000, | |
467 | .linear_min_sel = DA9121_MIN_SEL, | |
468 | .enable_reg = DA9xxx_REG_BUCK_BUCK2_0, | |
469 | .enable_mask = DA9121_MASK_BUCK_BUCKx_0_CHx_EN, | |
470 | .vsel_reg = DA9xxx_REG_BUCK_BUCK2_5, | |
471 | .vsel_mask = DA9121_MASK_BUCK_BUCKx_5_CHx_A_VOUT, | |
472 | } | |
473 | }; | |
474 | ||
475 | static const struct regulator_desc da9122_reg[2] = { | |
476 | { | |
477 | .id = DA9121_IDX_BUCK1, | |
478 | .name = "DA9122/DA9131 BUCK1", | |
479 | .of_match = "buck1", | |
46c413d5 | 480 | .of_parse_cb = da9121_of_parse_cb, |
91863239 AW |
481 | .owner = THIS_MODULE, |
482 | .regulators_node = of_match_ptr("regulators"), | |
65ac9704 | 483 | .of_map_mode = da9121_map_mode, |
91863239 AW |
484 | .ops = &da9121_buck_ops, |
485 | .type = REGULATOR_VOLTAGE, | |
486 | .n_voltages = DA9121_N_VOLTAGES, | |
487 | .min_uV = DA9121_MIN_MV * 1000, | |
488 | .uV_step = DA9121_STEP_MV * 1000, | |
489 | .linear_min_sel = DA9121_MIN_SEL, | |
490 | .enable_reg = DA9121_REG_BUCK_BUCK1_0, | |
491 | .enable_mask = DA9121_MASK_BUCK_BUCKx_0_CHx_EN, | |
492 | .vsel_reg = DA9121_REG_BUCK_BUCK1_5, | |
493 | .vsel_mask = DA9121_MASK_BUCK_BUCKx_5_CHx_A_VOUT, | |
494 | }, | |
495 | { | |
496 | .id = DA9121_IDX_BUCK2, | |
497 | .name = "DA9122/DA9131 BUCK2", | |
498 | .of_match = "buck2", | |
46c413d5 | 499 | .of_parse_cb = da9121_of_parse_cb, |
91863239 AW |
500 | .owner = THIS_MODULE, |
501 | .regulators_node = of_match_ptr("regulators"), | |
65ac9704 | 502 | .of_map_mode = da9121_map_mode, |
91863239 AW |
503 | .ops = &da9121_buck_ops, |
504 | .type = REGULATOR_VOLTAGE, | |
505 | .n_voltages = DA9121_N_VOLTAGES, | |
506 | .min_uV = DA9121_MIN_MV * 1000, | |
507 | .uV_step = DA9121_STEP_MV * 1000, | |
508 | .linear_min_sel = DA9121_MIN_SEL, | |
509 | .enable_reg = DA9xxx_REG_BUCK_BUCK2_0, | |
510 | .enable_mask = DA9121_MASK_BUCK_BUCKx_0_CHx_EN, | |
511 | .vsel_reg = DA9xxx_REG_BUCK_BUCK2_5, | |
512 | .vsel_mask = DA9121_MASK_BUCK_BUCKx_5_CHx_A_VOUT, | |
513 | } | |
514 | }; | |
515 | ||
516 | static const struct regulator_desc da9217_reg = { | |
517 | .id = DA9121_IDX_BUCK1, | |
518 | .name = "DA9217 BUCK1", | |
519 | .of_match = "buck1", | |
46c413d5 | 520 | .of_parse_cb = da9121_of_parse_cb, |
91863239 AW |
521 | .owner = THIS_MODULE, |
522 | .regulators_node = of_match_ptr("regulators"), | |
65ac9704 | 523 | .of_map_mode = da9121_map_mode, |
91863239 AW |
524 | .ops = &da9121_buck_ops, |
525 | .type = REGULATOR_VOLTAGE, | |
526 | .n_voltages = DA9121_N_VOLTAGES, | |
527 | .min_uV = DA9121_MIN_MV * 1000, | |
528 | .uV_step = DA9121_STEP_MV * 1000, | |
529 | .linear_min_sel = DA9121_MIN_SEL, | |
530 | .enable_reg = DA9121_REG_BUCK_BUCK1_0, | |
531 | .enable_mask = DA9121_MASK_BUCK_BUCKx_0_CHx_EN, | |
532 | .vsel_reg = DA9121_REG_BUCK_BUCK1_5, | |
533 | .vsel_mask = DA9121_MASK_BUCK_BUCKx_5_CHx_A_VOUT, | |
534 | }; | |
535 | ||
536 | static const struct regulator_desc *local_da9121_regulators[][DA9121_IDX_MAX] = { | |
537 | [DA9121_TYPE_DA9121_DA9130] = { &da9121_reg, NULL }, | |
538 | [DA9121_TYPE_DA9220_DA9132] = { &da9220_reg[0], &da9220_reg[1] }, | |
539 | [DA9121_TYPE_DA9122_DA9131] = { &da9122_reg[0], &da9122_reg[1] }, | |
540 | [DA9121_TYPE_DA9217] = { &da9217_reg, NULL }, | |
541 | }; | |
542 | ||
40bb5b02 AW |
543 | static void da9121_status_poll_on(struct work_struct *work) |
544 | { | |
545 | struct da9121 *chip = container_of(work, struct da9121, work.work); | |
546 | int status[3] = {0}; | |
547 | int clear[3] = {0}; | |
548 | unsigned long delay; | |
549 | int i; | |
550 | int ret; | |
551 | ||
552 | ret = regmap_bulk_read(chip->regmap, DA9121_REG_SYS_STATUS_0, status, 2); | |
553 | if (ret < 0) { | |
554 | dev_err(chip->dev, | |
555 | "Failed to read STATUS registers: %d\n", ret); | |
556 | goto error; | |
557 | } | |
558 | ||
559 | /* Possible events are tested to be within range for the variant, potentially | |
560 | * masked by the IRQ handler (not just warned about), as having been masked, | |
561 | * and the respective state cleared - then flagged to unmask for next IRQ. | |
562 | */ | |
563 | for (i = 0; i < ARRAY_SIZE(status_event_handling); i++) { | |
564 | const struct status_event_data *item = &status_event_handling[i]; | |
565 | int reg_idx = item->reg_index; | |
566 | bool relevant = (item->buck_id <= variant_parameters[chip->variant_id].num_bucks); | |
567 | bool supported = (item->warn == NULL); | |
568 | bool persisting = (chip->persistent[reg_idx] & item->event_bit); | |
569 | bool now_cleared = !(status[reg_idx] & item->status_bit); | |
570 | ||
571 | if (relevant && supported && persisting && now_cleared) { | |
572 | clear[reg_idx] |= item->mask_bit; | |
573 | chip->persistent[reg_idx] &= ~item->event_bit; | |
574 | } | |
575 | } | |
576 | ||
577 | for (i = 0; i < 2; i++) { | |
578 | if (clear[i]) { | |
579 | unsigned int reg = DA9121_REG_SYS_MASK_0 + i; | |
580 | unsigned int mbit = clear[i]; | |
581 | ||
582 | ret = regmap_update_bits(chip->regmap, reg, mbit, 0); | |
583 | if (ret < 0) { | |
584 | dev_err(chip->dev, | |
585 | "Failed to unmask 0x%02x %d\n", | |
586 | reg, ret); | |
587 | goto error; | |
588 | } | |
589 | } | |
590 | } | |
591 | ||
592 | if (chip->persistent[0] | chip->persistent[1]) { | |
593 | delay = msecs_to_jiffies(chip->passive_delay); | |
594 | queue_delayed_work(system_freezable_wq, &chip->work, delay); | |
595 | } | |
596 | ||
597 | error: | |
598 | return; | |
599 | } | |
600 | ||
601 | static irqreturn_t da9121_irq_handler(int irq, void *data) | |
602 | { | |
603 | struct da9121 *chip = data; | |
604 | struct regulator_dev *rdev; | |
605 | int event[3] = {0}; | |
606 | int handled[3] = {0}; | |
607 | int mask[3] = {0}; | |
608 | int ret = IRQ_NONE; | |
609 | int i; | |
610 | int err; | |
611 | ||
612 | err = regmap_bulk_read(chip->regmap, DA9121_REG_SYS_EVENT_0, event, 3); | |
613 | if (err < 0) { | |
614 | dev_err(chip->dev, "Failed to read EVENT registers %d\n", err); | |
615 | ret = IRQ_NONE; | |
616 | goto error; | |
617 | } | |
618 | ||
619 | err = regmap_bulk_read(chip->regmap, DA9121_REG_SYS_MASK_0, mask, 3); | |
620 | if (err < 0) { | |
621 | dev_err(chip->dev, | |
622 | "Failed to read MASK registers: %d\n", ret); | |
623 | ret = IRQ_NONE; | |
624 | goto error; | |
625 | } | |
626 | ||
627 | rdev = chip->rdev[DA9121_IDX_BUCK1]; | |
628 | ||
629 | /* Possible events are tested to be within range for the variant, currently | |
630 | * enabled, and having triggered this IRQ. The event may then be notified, | |
631 | * or a warning given for unexpected events - those from device POR, and | |
632 | * currently unsupported GPIO configurations. | |
633 | */ | |
634 | for (i = 0; i < ARRAY_SIZE(status_event_handling); i++) { | |
635 | const struct status_event_data *item = &status_event_handling[i]; | |
636 | int reg_idx = item->reg_index; | |
637 | bool relevant = (item->buck_id <= variant_parameters[chip->variant_id].num_bucks); | |
638 | bool enabled = !(mask[reg_idx] & item->mask_bit); | |
639 | bool active = (event[reg_idx] & item->event_bit); | |
640 | bool notify = (item->warn == NULL); | |
641 | ||
642 | if (relevant && enabled && active) { | |
643 | if (notify) { | |
644 | chip->persistent[reg_idx] |= item->event_bit; | |
645 | regulator_notifier_call_chain(rdev, item->notification, NULL); | |
646 | } else { | |
647 | dev_warn(chip->dev, item->warn); | |
648 | handled[reg_idx] |= item->event_bit; | |
649 | ret = IRQ_HANDLED; | |
650 | } | |
651 | } | |
652 | } | |
653 | ||
654 | for (i = 0; i < 3; i++) { | |
655 | if (event[i] != handled[i]) { | |
656 | dev_warn(chip->dev, | |
657 | "Unhandled event(s) in bank%d 0x%02x\n", i, | |
658 | event[i] ^ handled[i]); | |
659 | } | |
660 | } | |
661 | ||
662 | /* Mask the interrupts for persistent events OV, OC, UV, WARN, CRIT */ | |
663 | for (i = 0; i < 2; i++) { | |
664 | if (handled[i]) { | |
665 | unsigned int reg = DA9121_REG_SYS_MASK_0 + i; | |
666 | unsigned int mbit = handled[i]; | |
667 | ||
668 | err = regmap_update_bits(chip->regmap, reg, mbit, mbit); | |
669 | if (err < 0) { | |
670 | dev_err(chip->dev, | |
671 | "Failed to mask 0x%02x interrupt %d\n", | |
672 | reg, err); | |
673 | ret = IRQ_NONE; | |
674 | goto error; | |
675 | } | |
676 | } | |
677 | } | |
678 | ||
679 | /* clear the events */ | |
680 | if (handled[0] | handled[1] | handled[2]) { | |
681 | err = regmap_bulk_write(chip->regmap, DA9121_REG_SYS_EVENT_0, handled, 3); | |
682 | if (err < 0) { | |
683 | dev_err(chip->dev, "Fail to write EVENTs %d\n", err); | |
684 | ret = IRQ_NONE; | |
685 | goto error; | |
686 | } | |
687 | } | |
688 | ||
689 | queue_delayed_work(system_freezable_wq, &chip->work, 0); | |
690 | error: | |
691 | return ret; | |
692 | } | |
693 | ||
9929900d AW |
694 | static int da9121_set_regulator_config(struct da9121 *chip) |
695 | { | |
696 | struct regulator_config config = { }; | |
697 | unsigned int max_matches = variant_parameters[chip->variant_id].num_bucks; | |
698 | int ret = 0; | |
699 | int i; | |
700 | ||
701 | for (i = 0; i < max_matches; i++) { | |
702 | const struct regulator_desc *regl_desc = | |
703 | local_da9121_regulators[chip->variant_id][i]; | |
704 | ||
705 | config.dev = chip->dev; | |
706 | config.driver_data = chip; | |
707 | config.regmap = chip->regmap; | |
708 | ||
709 | chip->rdev[i] = devm_regulator_register(chip->dev, | |
710 | regl_desc, &config); | |
711 | if (IS_ERR(chip->rdev[i])) { | |
712 | dev_err(chip->dev, "Failed to register regulator %s, %d/%d\n", | |
713 | regl_desc->name, (i+1), max_matches); | |
714 | ret = PTR_ERR(chip->rdev[i]); | |
715 | goto error; | |
716 | } | |
717 | } | |
718 | ||
719 | error: | |
720 | return ret; | |
721 | } | |
722 | ||
c860476b AW |
723 | /* DA9121 chip register model */ |
724 | static const struct regmap_range da9121_1ch_readable_ranges[] = { | |
725 | regmap_reg_range(DA9121_REG_SYS_STATUS_0, DA9121_REG_SYS_MASK_3), | |
726 | regmap_reg_range(DA9121_REG_SYS_CONFIG_2, DA9121_REG_SYS_CONFIG_3), | |
727 | regmap_reg_range(DA9121_REG_SYS_GPIO0_0, DA9121_REG_SYS_GPIO2_1), | |
728 | regmap_reg_range(DA9121_REG_BUCK_BUCK1_0, DA9121_REG_BUCK_BUCK1_6), | |
729 | regmap_reg_range(DA9121_REG_OTP_DEVICE_ID, DA9121_REG_OTP_CONFIG_ID), | |
730 | }; | |
731 | ||
732 | static const struct regmap_access_table da9121_1ch_readable_table = { | |
733 | .yes_ranges = da9121_1ch_readable_ranges, | |
734 | .n_yes_ranges = ARRAY_SIZE(da9121_1ch_readable_ranges), | |
735 | }; | |
736 | ||
737 | static const struct regmap_range da9121_2ch_readable_ranges[] = { | |
738 | regmap_reg_range(DA9121_REG_SYS_STATUS_0, DA9121_REG_SYS_MASK_3), | |
739 | regmap_reg_range(DA9121_REG_SYS_CONFIG_2, DA9121_REG_SYS_CONFIG_3), | |
740 | regmap_reg_range(DA9121_REG_SYS_GPIO0_0, DA9121_REG_SYS_GPIO2_1), | |
741 | regmap_reg_range(DA9121_REG_BUCK_BUCK1_0, DA9121_REG_BUCK_BUCK1_7), | |
742 | regmap_reg_range(DA9xxx_REG_BUCK_BUCK2_0, DA9xxx_REG_BUCK_BUCK2_7), | |
743 | regmap_reg_range(DA9121_REG_OTP_DEVICE_ID, DA9121_REG_OTP_CONFIG_ID), | |
744 | }; | |
745 | ||
746 | static const struct regmap_access_table da9121_2ch_readable_table = { | |
747 | .yes_ranges = da9121_2ch_readable_ranges, | |
748 | .n_yes_ranges = ARRAY_SIZE(da9121_2ch_readable_ranges), | |
749 | }; | |
750 | ||
751 | static const struct regmap_range da9121_1ch_writeable_ranges[] = { | |
752 | regmap_reg_range(DA9121_REG_SYS_EVENT_0, DA9121_REG_SYS_MASK_3), | |
753 | regmap_reg_range(DA9121_REG_SYS_CONFIG_2, DA9121_REG_SYS_CONFIG_3), | |
754 | regmap_reg_range(DA9121_REG_SYS_GPIO0_0, DA9121_REG_SYS_GPIO2_1), | |
755 | regmap_reg_range(DA9121_REG_BUCK_BUCK1_0, DA9121_REG_BUCK_BUCK1_2), | |
756 | regmap_reg_range(DA9121_REG_BUCK_BUCK1_4, DA9121_REG_BUCK_BUCK1_6), | |
757 | }; | |
758 | ||
759 | static const struct regmap_access_table da9121_1ch_writeable_table = { | |
760 | .yes_ranges = da9121_1ch_writeable_ranges, | |
761 | .n_yes_ranges = ARRAY_SIZE(da9121_1ch_writeable_ranges), | |
762 | }; | |
763 | ||
764 | static const struct regmap_range da9121_2ch_writeable_ranges[] = { | |
765 | regmap_reg_range(DA9121_REG_SYS_EVENT_0, DA9121_REG_SYS_MASK_3), | |
766 | regmap_reg_range(DA9121_REG_SYS_CONFIG_2, DA9121_REG_SYS_CONFIG_3), | |
767 | regmap_reg_range(DA9121_REG_SYS_GPIO0_0, DA9121_REG_SYS_GPIO2_1), | |
768 | regmap_reg_range(DA9121_REG_BUCK_BUCK1_0, DA9121_REG_BUCK_BUCK1_2), | |
769 | regmap_reg_range(DA9121_REG_BUCK_BUCK1_4, DA9121_REG_BUCK_BUCK1_7), | |
770 | regmap_reg_range(DA9xxx_REG_BUCK_BUCK2_0, DA9xxx_REG_BUCK_BUCK2_2), | |
771 | regmap_reg_range(DA9xxx_REG_BUCK_BUCK2_4, DA9xxx_REG_BUCK_BUCK2_7), | |
772 | }; | |
773 | ||
774 | static const struct regmap_access_table da9121_2ch_writeable_table = { | |
775 | .yes_ranges = da9121_2ch_writeable_ranges, | |
776 | .n_yes_ranges = ARRAY_SIZE(da9121_2ch_writeable_ranges), | |
777 | }; | |
778 | ||
779 | ||
780 | static const struct regmap_range da9121_volatile_ranges[] = { | |
781 | regmap_reg_range(DA9121_REG_SYS_STATUS_0, DA9121_REG_SYS_EVENT_2), | |
782 | regmap_reg_range(DA9121_REG_SYS_GPIO0_0, DA9121_REG_SYS_GPIO2_1), | |
783 | regmap_reg_range(DA9121_REG_BUCK_BUCK1_0, DA9121_REG_BUCK_BUCK1_6), | |
784 | }; | |
785 | ||
786 | static const struct regmap_access_table da9121_volatile_table = { | |
787 | .yes_ranges = da9121_volatile_ranges, | |
788 | .n_yes_ranges = ARRAY_SIZE(da9121_volatile_ranges), | |
789 | }; | |
790 | ||
791 | /* DA9121 regmap config for 1 channel variants */ | |
792 | static struct regmap_config da9121_1ch_regmap_config = { | |
793 | .reg_bits = 8, | |
794 | .val_bits = 8, | |
795 | .max_register = DA9121_REG_OTP_CONFIG_ID, | |
796 | .rd_table = &da9121_1ch_readable_table, | |
797 | .wr_table = &da9121_1ch_writeable_table, | |
798 | .volatile_table = &da9121_volatile_table, | |
799 | .cache_type = REGCACHE_RBTREE, | |
800 | }; | |
801 | ||
802 | /* DA9121 regmap config for 2 channel variants */ | |
803 | static struct regmap_config da9121_2ch_regmap_config = { | |
804 | .reg_bits = 8, | |
805 | .val_bits = 8, | |
806 | .max_register = DA9121_REG_OTP_CONFIG_ID, | |
807 | .rd_table = &da9121_2ch_readable_table, | |
808 | .wr_table = &da9121_2ch_writeable_table, | |
809 | .volatile_table = &da9121_volatile_table, | |
810 | .cache_type = REGCACHE_RBTREE, | |
811 | }; | |
812 | ||
813 | static int da9121_check_device_type(struct i2c_client *i2c, struct da9121 *chip) | |
814 | { | |
815 | u32 device_id; | |
c860476b AW |
816 | u32 variant_id; |
817 | u8 variant_mrc, variant_vrc; | |
818 | char *type; | |
c860476b AW |
819 | bool config_match = false; |
820 | int ret = 0; | |
821 | ||
822 | ret = regmap_read(chip->regmap, DA9121_REG_OTP_DEVICE_ID, &device_id); | |
823 | if (ret < 0) { | |
824 | dev_err(chip->dev, "Cannot read device ID: %d\n", ret); | |
825 | goto error; | |
826 | } | |
827 | ||
828 | ret = regmap_read(chip->regmap, DA9121_REG_OTP_VARIANT_ID, &variant_id); | |
829 | if (ret < 0) { | |
830 | dev_err(chip->dev, "Cannot read variant ID: %d\n", ret); | |
831 | goto error; | |
832 | } | |
833 | ||
834 | if (device_id != DA9121_DEVICE_ID) { | |
835 | dev_err(chip->dev, "Invalid device ID: 0x%02x\n", device_id); | |
836 | ret = -ENODEV; | |
837 | goto error; | |
838 | } | |
839 | ||
840 | variant_vrc = variant_id & DA9121_MASK_OTP_VARIANT_ID_VRC; | |
841 | ||
013592be AW |
842 | switch (chip->subvariant_id) { |
843 | case DA9121_SUBTYPE_DA9121: | |
844 | type = "DA9121"; | |
845 | config_match = (variant_vrc == DA9121_VARIANT_VRC); | |
c860476b | 846 | break; |
013592be AW |
847 | case DA9121_SUBTYPE_DA9130: |
848 | type = "DA9130"; | |
849 | config_match = (variant_vrc == DA9130_VARIANT_VRC); | |
c860476b | 850 | break; |
013592be AW |
851 | case DA9121_SUBTYPE_DA9220: |
852 | type = "DA9220"; | |
853 | config_match = (variant_vrc == DA9220_VARIANT_VRC); | |
c860476b | 854 | break; |
013592be AW |
855 | case DA9121_SUBTYPE_DA9132: |
856 | type = "DA9132"; | |
857 | config_match = (variant_vrc == DA9132_VARIANT_VRC); | |
858 | break; | |
859 | case DA9121_SUBTYPE_DA9122: | |
860 | type = "DA9122"; | |
861 | config_match = (variant_vrc == DA9122_VARIANT_VRC); | |
862 | break; | |
863 | case DA9121_SUBTYPE_DA9131: | |
864 | type = "DA9131"; | |
865 | config_match = (variant_vrc == DA9131_VARIANT_VRC); | |
866 | break; | |
867 | case DA9121_SUBTYPE_DA9217: | |
c860476b | 868 | type = "DA9217"; |
013592be | 869 | config_match = (variant_vrc == DA9217_VARIANT_VRC); |
c860476b AW |
870 | break; |
871 | default: | |
872 | type = "Unknown"; | |
873 | break; | |
874 | } | |
875 | ||
876 | dev_info(chip->dev, | |
877 | "Device detected (device-ID: 0x%02X, var-ID: 0x%02X, %s)\n", | |
878 | device_id, variant_id, type); | |
879 | ||
880 | if (!config_match) { | |
416c29e9 | 881 | dev_err(chip->dev, "Device tree configuration does not match detected device.\n"); |
c860476b AW |
882 | ret = -EINVAL; |
883 | goto error; | |
884 | } | |
885 | ||
886 | variant_mrc = (variant_id & DA9121_MASK_OTP_VARIANT_ID_MRC) | |
887 | >> DA9121_SHIFT_OTP_VARIANT_ID_MRC; | |
888 | ||
889 | if ((device_id == DA9121_DEVICE_ID) && | |
890 | (variant_mrc < DA9121_VARIANT_MRC_BASE)) { | |
891 | dev_err(chip->dev, | |
892 | "Cannot support variant MRC: 0x%02X\n", variant_mrc); | |
893 | ret = -EINVAL; | |
894 | } | |
895 | error: | |
896 | return ret; | |
897 | } | |
898 | ||
899 | static int da9121_assign_chip_model(struct i2c_client *i2c, | |
900 | struct da9121 *chip) | |
901 | { | |
902 | struct regmap_config *regmap; | |
903 | int ret = 0; | |
904 | ||
905 | chip->dev = &i2c->dev; | |
906 | ||
013592be AW |
907 | /* Use configured subtype to select the regulator descriptor index and |
908 | * register map, common to both consumer and automotive grade variants | |
909 | */ | |
910 | switch (chip->subvariant_id) { | |
911 | case DA9121_SUBTYPE_DA9121: | |
912 | case DA9121_SUBTYPE_DA9130: | |
913 | chip->variant_id = DA9121_TYPE_DA9121_DA9130; | |
c860476b AW |
914 | regmap = &da9121_1ch_regmap_config; |
915 | break; | |
013592be AW |
916 | case DA9121_SUBTYPE_DA9217: |
917 | chip->variant_id = DA9121_TYPE_DA9217; | |
918 | regmap = &da9121_1ch_regmap_config; | |
919 | break; | |
920 | case DA9121_SUBTYPE_DA9122: | |
921 | case DA9121_SUBTYPE_DA9131: | |
922 | chip->variant_id = DA9121_TYPE_DA9122_DA9131; | |
923 | regmap = &da9121_2ch_regmap_config; | |
924 | break; | |
925 | case DA9121_SUBTYPE_DA9220: | |
926 | case DA9121_SUBTYPE_DA9132: | |
927 | chip->variant_id = DA9121_TYPE_DA9220_DA9132; | |
c860476b AW |
928 | regmap = &da9121_2ch_regmap_config; |
929 | break; | |
930 | } | |
931 | ||
91863239 AW |
932 | /* Set these up for of_regulator_match call which may want .of_map_modes */ |
933 | da9121_matches[0].desc = local_da9121_regulators[chip->variant_id][0]; | |
934 | da9121_matches[1].desc = local_da9121_regulators[chip->variant_id][1]; | |
935 | ||
c860476b AW |
936 | chip->regmap = devm_regmap_init_i2c(i2c, regmap); |
937 | if (IS_ERR(chip->regmap)) { | |
938 | ret = PTR_ERR(chip->regmap); | |
939 | dev_err(chip->dev, "Failed to configure a register map: %d\n", | |
940 | ret); | |
8db06423 | 941 | return ret; |
c860476b AW |
942 | } |
943 | ||
944 | ret = da9121_check_device_type(i2c, chip); | |
945 | ||
946 | return ret; | |
947 | } | |
948 | ||
40bb5b02 AW |
949 | static int da9121_config_irq(struct i2c_client *i2c, |
950 | struct da9121 *chip) | |
951 | { | |
952 | unsigned int p_delay = DA9121_DEFAULT_POLLING_PERIOD_MS; | |
953 | const int mask_all[4] = { 0, 0, 0xFF, 0xFF }; | |
954 | int ret = 0; | |
955 | ||
956 | chip->chip_irq = i2c->irq; | |
957 | ||
958 | if (chip->chip_irq != 0) { | |
959 | if (!of_property_read_u32(chip->dev->of_node, | |
960 | "dlg,irq-polling-delay-passive-ms", | |
961 | &p_delay)) { | |
962 | if (p_delay < DA9121_MIN_POLLING_PERIOD_MS || | |
963 | p_delay > DA9121_MAX_POLLING_PERIOD_MS) { | |
964 | dev_warn(chip->dev, | |
965 | "Out-of-range polling period %d ms\n", | |
966 | p_delay); | |
967 | p_delay = DA9121_DEFAULT_POLLING_PERIOD_MS; | |
968 | } | |
969 | } | |
970 | ||
971 | chip->passive_delay = p_delay; | |
972 | ||
5e191d2e | 973 | ret = request_threaded_irq(chip->chip_irq, NULL, |
40bb5b02 AW |
974 | da9121_irq_handler, |
975 | IRQF_TRIGGER_LOW|IRQF_ONESHOT, | |
976 | "da9121", chip); | |
977 | if (ret != 0) { | |
978 | dev_err(chip->dev, "Failed IRQ request: %d\n", | |
979 | chip->chip_irq); | |
980 | goto error; | |
981 | } | |
982 | ||
983 | ret = regmap_bulk_write(chip->regmap, DA9121_REG_SYS_MASK_0, mask_all, 4); | |
984 | if (ret != 0) { | |
985 | dev_err(chip->dev, "Failed to set IRQ masks: %d\n", | |
986 | ret); | |
5e191d2e | 987 | goto regmap_error; |
40bb5b02 AW |
988 | } |
989 | ||
990 | INIT_DELAYED_WORK(&chip->work, da9121_status_poll_on); | |
991 | dev_info(chip->dev, "Interrupt polling period set at %d ms\n", | |
992 | chip->passive_delay); | |
993 | } | |
994 | error: | |
995 | return ret; | |
5e191d2e AW |
996 | regmap_error: |
997 | free_irq(chip->chip_irq, chip); | |
998 | return ret; | |
40bb5b02 AW |
999 | } |
1000 | ||
e6ff10f2 | 1001 | static const struct of_device_id da9121_dt_ids[] = { |
013592be AW |
1002 | { .compatible = "dlg,da9121", .data = (void *) DA9121_SUBTYPE_DA9121 }, |
1003 | { .compatible = "dlg,da9130", .data = (void *) DA9121_SUBTYPE_DA9130 }, | |
1004 | { .compatible = "dlg,da9217", .data = (void *) DA9121_SUBTYPE_DA9217 }, | |
1005 | { .compatible = "dlg,da9122", .data = (void *) DA9121_SUBTYPE_DA9122 }, | |
1006 | { .compatible = "dlg,da9131", .data = (void *) DA9121_SUBTYPE_DA9131 }, | |
1007 | { .compatible = "dlg,da9220", .data = (void *) DA9121_SUBTYPE_DA9220 }, | |
1008 | { .compatible = "dlg,da9132", .data = (void *) DA9121_SUBTYPE_DA9132 }, | |
e6ff10f2 VW |
1009 | { } |
1010 | }; | |
1011 | MODULE_DEVICE_TABLE(of, da9121_dt_ids); | |
1012 | ||
f3fbd556 AW |
1013 | static inline int da9121_of_get_id(struct device *dev) |
1014 | { | |
1015 | const struct of_device_id *id = of_match_device(da9121_dt_ids, dev); | |
1016 | ||
1017 | if (!id) { | |
1018 | dev_err(dev, "%s: Failed\n", __func__); | |
1019 | return -EINVAL; | |
1020 | } | |
1021 | return (uintptr_t)id->data; | |
1022 | } | |
1023 | ||
e6ff10f2 VW |
1024 | static int da9121_i2c_probe(struct i2c_client *i2c, |
1025 | const struct i2c_device_id *id) | |
1026 | { | |
f3fbd556 | 1027 | struct da9121 *chip; |
40bb5b02 | 1028 | const int mask_all[4] = { 0xFF, 0xFF, 0xFF, 0xFF }; |
f3fbd556 | 1029 | int ret = 0; |
e6ff10f2 | 1030 | |
f3fbd556 AW |
1031 | chip = devm_kzalloc(&i2c->dev, sizeof(struct da9121), GFP_KERNEL); |
1032 | if (!chip) { | |
1033 | ret = -ENOMEM; | |
1034 | goto error; | |
1035 | } | |
1036 | ||
46c413d5 | 1037 | chip->pdata = i2c->dev.platform_data; |
013592be | 1038 | chip->subvariant_id = da9121_of_get_id(&i2c->dev); |
f3fbd556 | 1039 | |
c860476b AW |
1040 | ret = da9121_assign_chip_model(i2c, chip); |
1041 | if (ret < 0) | |
1042 | goto error; | |
e6ff10f2 | 1043 | |
40bb5b02 AW |
1044 | ret = regmap_bulk_write(chip->regmap, DA9121_REG_SYS_MASK_0, mask_all, 4); |
1045 | if (ret != 0) { | |
1046 | dev_err(chip->dev, "Failed to set IRQ masks: %d\n", ret); | |
1047 | goto error; | |
1048 | } | |
1049 | ||
9929900d | 1050 | ret = da9121_set_regulator_config(chip); |
40bb5b02 AW |
1051 | if (ret < 0) |
1052 | goto error; | |
1053 | ||
1054 | ret = da9121_config_irq(i2c, chip); | |
e6ff10f2 | 1055 | |
f3fbd556 AW |
1056 | error: |
1057 | return ret; | |
e6ff10f2 VW |
1058 | } |
1059 | ||
40bb5b02 AW |
1060 | static int da9121_i2c_remove(struct i2c_client *i2c) |
1061 | { | |
1062 | struct da9121 *chip = i2c_get_clientdata(i2c); | |
1063 | const int mask_all[4] = { 0xFF, 0xFF, 0xFF, 0xFF }; | |
1064 | int ret = 0; | |
1065 | ||
5e191d2e | 1066 | free_irq(chip->chip_irq, chip); |
40bb5b02 AW |
1067 | cancel_delayed_work_sync(&chip->work); |
1068 | ||
1069 | ret = regmap_bulk_write(chip->regmap, DA9121_REG_SYS_MASK_0, mask_all, 4); | |
1070 | if (ret != 0) | |
1071 | dev_err(chip->dev, "Failed to set IRQ masks: %d\n", ret); | |
1072 | return ret; | |
1073 | } | |
1074 | ||
e6ff10f2 | 1075 | static const struct i2c_device_id da9121_i2c_id[] = { |
f3fbd556 AW |
1076 | {"da9121", DA9121_TYPE_DA9121_DA9130}, |
1077 | {"da9130", DA9121_TYPE_DA9121_DA9130}, | |
1078 | {"da9217", DA9121_TYPE_DA9217}, | |
1079 | {"da9122", DA9121_TYPE_DA9122_DA9131}, | |
1080 | {"da9131", DA9121_TYPE_DA9122_DA9131}, | |
1081 | {"da9220", DA9121_TYPE_DA9220_DA9132}, | |
1082 | {"da9132", DA9121_TYPE_DA9220_DA9132}, | |
e6ff10f2 VW |
1083 | {}, |
1084 | }; | |
1085 | MODULE_DEVICE_TABLE(i2c, da9121_i2c_id); | |
1086 | ||
1087 | static struct i2c_driver da9121_regulator_driver = { | |
1088 | .driver = { | |
1089 | .name = "da9121", | |
1090 | .of_match_table = of_match_ptr(da9121_dt_ids), | |
1091 | }, | |
1092 | .probe = da9121_i2c_probe, | |
40bb5b02 | 1093 | .remove = da9121_i2c_remove, |
e6ff10f2 VW |
1094 | .id_table = da9121_i2c_id, |
1095 | }; | |
1096 | ||
1097 | module_i2c_driver(da9121_regulator_driver); | |
1098 | ||
1099 | MODULE_LICENSE("GPL v2"); |