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