Loading...
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Core, IRQ and I2C device driver for DA9061 and DA9062 PMICs
4 * Copyright (C) 2015-2017 Dialog Semiconductor
5 */
6
7#include <linux/kernel.h>
8#include <linux/module.h>
9#include <linux/init.h>
10#include <linux/device.h>
11#include <linux/interrupt.h>
12#include <linux/of.h>
13#include <linux/regmap.h>
14#include <linux/irq.h>
15#include <linux/mfd/core.h>
16#include <linux/i2c.h>
17#include <linux/mfd/da9062/core.h>
18#include <linux/mfd/da9062/registers.h>
19#include <linux/regulator/of_regulator.h>
20
21#define DA9062_REG_EVENT_A_OFFSET 0
22#define DA9062_REG_EVENT_B_OFFSET 1
23#define DA9062_REG_EVENT_C_OFFSET 2
24
25#define DA9062_IRQ_LOW 0
26#define DA9062_IRQ_HIGH 1
27
28static struct regmap_irq da9061_irqs[] = {
29 /* EVENT A */
30 [DA9061_IRQ_ONKEY] = {
31 .reg_offset = DA9062_REG_EVENT_A_OFFSET,
32 .mask = DA9062AA_M_NONKEY_MASK,
33 },
34 [DA9061_IRQ_WDG_WARN] = {
35 .reg_offset = DA9062_REG_EVENT_A_OFFSET,
36 .mask = DA9062AA_M_WDG_WARN_MASK,
37 },
38 [DA9061_IRQ_SEQ_RDY] = {
39 .reg_offset = DA9062_REG_EVENT_A_OFFSET,
40 .mask = DA9062AA_M_SEQ_RDY_MASK,
41 },
42 /* EVENT B */
43 [DA9061_IRQ_TEMP] = {
44 .reg_offset = DA9062_REG_EVENT_B_OFFSET,
45 .mask = DA9062AA_M_TEMP_MASK,
46 },
47 [DA9061_IRQ_LDO_LIM] = {
48 .reg_offset = DA9062_REG_EVENT_B_OFFSET,
49 .mask = DA9062AA_M_LDO_LIM_MASK,
50 },
51 [DA9061_IRQ_DVC_RDY] = {
52 .reg_offset = DA9062_REG_EVENT_B_OFFSET,
53 .mask = DA9062AA_M_DVC_RDY_MASK,
54 },
55 [DA9061_IRQ_VDD_WARN] = {
56 .reg_offset = DA9062_REG_EVENT_B_OFFSET,
57 .mask = DA9062AA_M_VDD_WARN_MASK,
58 },
59 /* EVENT C */
60 [DA9061_IRQ_GPI0] = {
61 .reg_offset = DA9062_REG_EVENT_C_OFFSET,
62 .mask = DA9062AA_M_GPI0_MASK,
63 },
64 [DA9061_IRQ_GPI1] = {
65 .reg_offset = DA9062_REG_EVENT_C_OFFSET,
66 .mask = DA9062AA_M_GPI1_MASK,
67 },
68 [DA9061_IRQ_GPI2] = {
69 .reg_offset = DA9062_REG_EVENT_C_OFFSET,
70 .mask = DA9062AA_M_GPI2_MASK,
71 },
72 [DA9061_IRQ_GPI3] = {
73 .reg_offset = DA9062_REG_EVENT_C_OFFSET,
74 .mask = DA9062AA_M_GPI3_MASK,
75 },
76 [DA9061_IRQ_GPI4] = {
77 .reg_offset = DA9062_REG_EVENT_C_OFFSET,
78 .mask = DA9062AA_M_GPI4_MASK,
79 },
80};
81
82static struct regmap_irq_chip da9061_irq_chip = {
83 .name = "da9061-irq",
84 .irqs = da9061_irqs,
85 .num_irqs = DA9061_NUM_IRQ,
86 .num_regs = 3,
87 .status_base = DA9062AA_EVENT_A,
88 .mask_base = DA9062AA_IRQ_MASK_A,
89 .ack_base = DA9062AA_EVENT_A,
90};
91
92static struct regmap_irq da9062_irqs[] = {
93 /* EVENT A */
94 [DA9062_IRQ_ONKEY] = {
95 .reg_offset = DA9062_REG_EVENT_A_OFFSET,
96 .mask = DA9062AA_M_NONKEY_MASK,
97 },
98 [DA9062_IRQ_ALARM] = {
99 .reg_offset = DA9062_REG_EVENT_A_OFFSET,
100 .mask = DA9062AA_M_ALARM_MASK,
101 },
102 [DA9062_IRQ_TICK] = {
103 .reg_offset = DA9062_REG_EVENT_A_OFFSET,
104 .mask = DA9062AA_M_TICK_MASK,
105 },
106 [DA9062_IRQ_WDG_WARN] = {
107 .reg_offset = DA9062_REG_EVENT_A_OFFSET,
108 .mask = DA9062AA_M_WDG_WARN_MASK,
109 },
110 [DA9062_IRQ_SEQ_RDY] = {
111 .reg_offset = DA9062_REG_EVENT_A_OFFSET,
112 .mask = DA9062AA_M_SEQ_RDY_MASK,
113 },
114 /* EVENT B */
115 [DA9062_IRQ_TEMP] = {
116 .reg_offset = DA9062_REG_EVENT_B_OFFSET,
117 .mask = DA9062AA_M_TEMP_MASK,
118 },
119 [DA9062_IRQ_LDO_LIM] = {
120 .reg_offset = DA9062_REG_EVENT_B_OFFSET,
121 .mask = DA9062AA_M_LDO_LIM_MASK,
122 },
123 [DA9062_IRQ_DVC_RDY] = {
124 .reg_offset = DA9062_REG_EVENT_B_OFFSET,
125 .mask = DA9062AA_M_DVC_RDY_MASK,
126 },
127 [DA9062_IRQ_VDD_WARN] = {
128 .reg_offset = DA9062_REG_EVENT_B_OFFSET,
129 .mask = DA9062AA_M_VDD_WARN_MASK,
130 },
131 /* EVENT C */
132 [DA9062_IRQ_GPI0] = {
133 .reg_offset = DA9062_REG_EVENT_C_OFFSET,
134 .mask = DA9062AA_M_GPI0_MASK,
135 },
136 [DA9062_IRQ_GPI1] = {
137 .reg_offset = DA9062_REG_EVENT_C_OFFSET,
138 .mask = DA9062AA_M_GPI1_MASK,
139 },
140 [DA9062_IRQ_GPI2] = {
141 .reg_offset = DA9062_REG_EVENT_C_OFFSET,
142 .mask = DA9062AA_M_GPI2_MASK,
143 },
144 [DA9062_IRQ_GPI3] = {
145 .reg_offset = DA9062_REG_EVENT_C_OFFSET,
146 .mask = DA9062AA_M_GPI3_MASK,
147 },
148 [DA9062_IRQ_GPI4] = {
149 .reg_offset = DA9062_REG_EVENT_C_OFFSET,
150 .mask = DA9062AA_M_GPI4_MASK,
151 },
152};
153
154static struct regmap_irq_chip da9062_irq_chip = {
155 .name = "da9062-irq",
156 .irqs = da9062_irqs,
157 .num_irqs = DA9062_NUM_IRQ,
158 .num_regs = 3,
159 .status_base = DA9062AA_EVENT_A,
160 .mask_base = DA9062AA_IRQ_MASK_A,
161 .ack_base = DA9062AA_EVENT_A,
162};
163
164static const struct resource da9061_core_resources[] = {
165 DEFINE_RES_IRQ_NAMED(DA9061_IRQ_VDD_WARN, "VDD_WARN"),
166};
167
168static const struct resource da9061_regulators_resources[] = {
169 DEFINE_RES_IRQ_NAMED(DA9061_IRQ_LDO_LIM, "LDO_LIM"),
170};
171
172static const struct resource da9061_thermal_resources[] = {
173 DEFINE_RES_IRQ_NAMED(DA9061_IRQ_TEMP, "THERMAL"),
174};
175
176static const struct resource da9061_wdt_resources[] = {
177 DEFINE_RES_IRQ_NAMED(DA9061_IRQ_WDG_WARN, "WD_WARN"),
178};
179
180static const struct resource da9061_onkey_resources[] = {
181 DEFINE_RES_IRQ_NAMED(DA9061_IRQ_ONKEY, "ONKEY"),
182};
183
184static const struct mfd_cell da9061_devs_irq[] = {
185 MFD_CELL_OF("da9061-core", da9061_core_resources, NULL, 0, 0,
186 NULL),
187 MFD_CELL_OF("da9062-regulators", da9061_regulators_resources, NULL, 0, 0,
188 NULL),
189 MFD_CELL_OF("da9061-watchdog", da9061_wdt_resources, NULL, 0, 0,
190 "dlg,da9061-watchdog"),
191 MFD_CELL_OF("da9061-thermal", da9061_thermal_resources, NULL, 0, 0,
192 "dlg,da9061-thermal"),
193 MFD_CELL_OF("da9061-onkey", da9061_onkey_resources, NULL, 0, 0,
194 "dlg,da9061-onkey"),
195};
196
197static const struct mfd_cell da9061_devs_noirq[] = {
198 MFD_CELL_OF("da9061-core", NULL, NULL, 0, 0, NULL),
199 MFD_CELL_OF("da9062-regulators", NULL, NULL, 0, 0, NULL),
200 MFD_CELL_OF("da9061-watchdog", NULL, NULL, 0, 0, "dlg,da9061-watchdog"),
201 MFD_CELL_OF("da9061-thermal", NULL, NULL, 0, 0, "dlg,da9061-thermal"),
202 MFD_CELL_OF("da9061-onkey", NULL, NULL, 0, 0, "dlg,da9061-onkey"),
203};
204
205static const struct resource da9062_core_resources[] = {
206 DEFINE_RES_NAMED(DA9062_IRQ_VDD_WARN, 1, "VDD_WARN", IORESOURCE_IRQ),
207};
208
209static const struct resource da9062_regulators_resources[] = {
210 DEFINE_RES_NAMED(DA9062_IRQ_LDO_LIM, 1, "LDO_LIM", IORESOURCE_IRQ),
211};
212
213static const struct resource da9062_thermal_resources[] = {
214 DEFINE_RES_NAMED(DA9062_IRQ_TEMP, 1, "THERMAL", IORESOURCE_IRQ),
215};
216
217static const struct resource da9062_wdt_resources[] = {
218 DEFINE_RES_NAMED(DA9062_IRQ_WDG_WARN, 1, "WD_WARN", IORESOURCE_IRQ),
219};
220
221static const struct resource da9062_rtc_resources[] = {
222 DEFINE_RES_NAMED(DA9062_IRQ_ALARM, 1, "ALARM", IORESOURCE_IRQ),
223 DEFINE_RES_NAMED(DA9062_IRQ_TICK, 1, "TICK", IORESOURCE_IRQ),
224};
225
226static const struct resource da9062_onkey_resources[] = {
227 DEFINE_RES_NAMED(DA9062_IRQ_ONKEY, 1, "ONKEY", IORESOURCE_IRQ),
228};
229
230static const struct resource da9062_gpio_resources[] = {
231 DEFINE_RES_NAMED(DA9062_IRQ_GPI0, 1, "GPI0", IORESOURCE_IRQ),
232 DEFINE_RES_NAMED(DA9062_IRQ_GPI1, 1, "GPI1", IORESOURCE_IRQ),
233 DEFINE_RES_NAMED(DA9062_IRQ_GPI2, 1, "GPI2", IORESOURCE_IRQ),
234 DEFINE_RES_NAMED(DA9062_IRQ_GPI3, 1, "GPI3", IORESOURCE_IRQ),
235 DEFINE_RES_NAMED(DA9062_IRQ_GPI4, 1, "GPI4", IORESOURCE_IRQ),
236};
237
238static const struct mfd_cell da9062_devs_irq[] = {
239 MFD_CELL_OF("da9062-core", da9062_core_resources, NULL, 0, 0,
240 NULL),
241 MFD_CELL_OF("da9062-regulators", da9062_regulators_resources, NULL, 0, 0,
242 NULL),
243 MFD_CELL_OF("da9062-watchdog", da9062_wdt_resources, NULL, 0, 0,
244 "dlg,da9062-watchdog"),
245 MFD_CELL_OF("da9062-thermal", da9062_thermal_resources, NULL, 0, 0,
246 "dlg,da9062-thermal"),
247 MFD_CELL_OF("da9062-rtc", da9062_rtc_resources, NULL, 0, 0,
248 "dlg,da9062-rtc"),
249 MFD_CELL_OF("da9062-onkey", da9062_onkey_resources, NULL, 0, 0,
250 "dlg,da9062-onkey"),
251 MFD_CELL_OF("da9062-gpio", da9062_gpio_resources, NULL, 0, 0,
252 "dlg,da9062-gpio"),
253};
254
255static const struct mfd_cell da9062_devs_noirq[] = {
256 MFD_CELL_OF("da9062-core", NULL, NULL, 0, 0, NULL),
257 MFD_CELL_OF("da9062-regulators", NULL, NULL, 0, 0, NULL),
258 MFD_CELL_OF("da9062-watchdog", NULL, NULL, 0, 0, "dlg,da9062-watchdog"),
259 MFD_CELL_OF("da9062-thermal", NULL, NULL, 0, 0, "dlg,da9062-thermal"),
260 MFD_CELL_OF("da9062-rtc", NULL, NULL, 0, 0, "dlg,da9062-rtc"),
261 MFD_CELL_OF("da9062-onkey", NULL, NULL, 0, 0, "dlg,da9062-onkey"),
262 MFD_CELL_OF("da9062-gpio", NULL, NULL, 0, 0, "dlg,da9062-gpio"),
263};
264
265static int da9062_clear_fault_log(struct da9062 *chip)
266{
267 int ret;
268 int fault_log;
269
270 ret = regmap_read(chip->regmap, DA9062AA_FAULT_LOG, &fault_log);
271 if (ret < 0)
272 return ret;
273
274 if (fault_log) {
275 if (fault_log & DA9062AA_TWD_ERROR_MASK)
276 dev_dbg(chip->dev, "Fault log entry detected: TWD_ERROR\n");
277 if (fault_log & DA9062AA_POR_MASK)
278 dev_dbg(chip->dev, "Fault log entry detected: POR\n");
279 if (fault_log & DA9062AA_VDD_FAULT_MASK)
280 dev_dbg(chip->dev, "Fault log entry detected: VDD_FAULT\n");
281 if (fault_log & DA9062AA_VDD_START_MASK)
282 dev_dbg(chip->dev, "Fault log entry detected: VDD_START\n");
283 if (fault_log & DA9062AA_TEMP_CRIT_MASK)
284 dev_dbg(chip->dev, "Fault log entry detected: TEMP_CRIT\n");
285 if (fault_log & DA9062AA_KEY_RESET_MASK)
286 dev_dbg(chip->dev, "Fault log entry detected: KEY_RESET\n");
287 if (fault_log & DA9062AA_NSHUTDOWN_MASK)
288 dev_dbg(chip->dev, "Fault log entry detected: NSHUTDOWN\n");
289 if (fault_log & DA9062AA_WAIT_SHUT_MASK)
290 dev_dbg(chip->dev, "Fault log entry detected: WAIT_SHUT\n");
291
292 ret = regmap_write(chip->regmap, DA9062AA_FAULT_LOG,
293 fault_log);
294 }
295
296 return ret;
297}
298
299static int da9062_get_device_type(struct da9062 *chip)
300{
301 int device_id, variant_id, variant_mrc, variant_vrc;
302 char *type;
303 int ret;
304
305 ret = regmap_read(chip->regmap, DA9062AA_DEVICE_ID, &device_id);
306 if (ret < 0) {
307 dev_err(chip->dev, "Cannot read chip ID.\n");
308 return -EIO;
309 }
310 if (device_id != DA9062_PMIC_DEVICE_ID) {
311 dev_err(chip->dev, "Invalid device ID: 0x%02x\n", device_id);
312 return -ENODEV;
313 }
314
315 ret = regmap_read(chip->regmap, DA9062AA_VARIANT_ID, &variant_id);
316 if (ret < 0) {
317 dev_err(chip->dev, "Cannot read chip variant id.\n");
318 return -EIO;
319 }
320
321 variant_vrc = (variant_id & DA9062AA_VRC_MASK) >> DA9062AA_VRC_SHIFT;
322
323 switch (variant_vrc) {
324 case DA9062_PMIC_VARIANT_VRC_DA9061:
325 type = "DA9061";
326 break;
327 case DA9062_PMIC_VARIANT_VRC_DA9062:
328 type = "DA9062";
329 break;
330 default:
331 type = "Unknown";
332 break;
333 }
334
335 dev_info(chip->dev,
336 "Device detected (device-ID: 0x%02X, var-ID: 0x%02X, %s)\n",
337 device_id, variant_id, type);
338
339 variant_mrc = (variant_id & DA9062AA_MRC_MASK) >> DA9062AA_MRC_SHIFT;
340
341 if (variant_mrc < DA9062_PMIC_VARIANT_MRC_AA) {
342 dev_err(chip->dev,
343 "Cannot support variant MRC: 0x%02X\n", variant_mrc);
344 return -ENODEV;
345 }
346
347 return ret;
348}
349
350static u32 da9062_configure_irq_type(struct da9062 *chip, int irq, u32 *trigger)
351{
352 u32 irq_type = 0;
353 struct irq_data *irq_data = irq_get_irq_data(irq);
354
355 if (!irq_data) {
356 dev_err(chip->dev, "Invalid IRQ: %d\n", irq);
357 return -EINVAL;
358 }
359 *trigger = irqd_get_trigger_type(irq_data);
360
361 switch (*trigger) {
362 case IRQ_TYPE_LEVEL_HIGH:
363 irq_type = DA9062_IRQ_HIGH;
364 break;
365 case IRQ_TYPE_LEVEL_LOW:
366 irq_type = DA9062_IRQ_LOW;
367 break;
368 default:
369 dev_warn(chip->dev, "Unsupported IRQ type: %d\n", *trigger);
370 return -EINVAL;
371 }
372 return regmap_update_bits(chip->regmap, DA9062AA_CONFIG_A,
373 DA9062AA_IRQ_TYPE_MASK,
374 irq_type << DA9062AA_IRQ_TYPE_SHIFT);
375}
376
377static const struct regmap_range da9061_aa_readable_ranges[] = {
378 regmap_reg_range(DA9062AA_PAGE_CON, DA9062AA_STATUS_B),
379 regmap_reg_range(DA9062AA_STATUS_D, DA9062AA_EVENT_C),
380 regmap_reg_range(DA9062AA_IRQ_MASK_A, DA9062AA_IRQ_MASK_C),
381 regmap_reg_range(DA9062AA_CONTROL_A, DA9062AA_GPIO_4),
382 regmap_reg_range(DA9062AA_GPIO_WKUP_MODE, DA9062AA_GPIO_OUT3_4),
383 regmap_reg_range(DA9062AA_BUCK1_CONT, DA9062AA_BUCK4_CONT),
384 regmap_reg_range(DA9062AA_BUCK3_CONT, DA9062AA_BUCK3_CONT),
385 regmap_reg_range(DA9062AA_LDO1_CONT, DA9062AA_LDO4_CONT),
386 regmap_reg_range(DA9062AA_DVC_1, DA9062AA_DVC_1),
387 regmap_reg_range(DA9062AA_SEQ, DA9062AA_ID_4_3),
388 regmap_reg_range(DA9062AA_ID_12_11, DA9062AA_ID_16_15),
389 regmap_reg_range(DA9062AA_ID_22_21, DA9062AA_ID_32_31),
390 regmap_reg_range(DA9062AA_SEQ_A, DA9062AA_WAIT),
391 regmap_reg_range(DA9062AA_RESET, DA9062AA_BUCK_ILIM_C),
392 regmap_reg_range(DA9062AA_BUCK1_CFG, DA9062AA_BUCK3_CFG),
393 regmap_reg_range(DA9062AA_VBUCK1_A, DA9062AA_VBUCK4_A),
394 regmap_reg_range(DA9062AA_VBUCK3_A, DA9062AA_VBUCK3_A),
395 regmap_reg_range(DA9062AA_VLDO1_A, DA9062AA_VLDO4_A),
396 regmap_reg_range(DA9062AA_CONFIG_A, DA9062AA_CONFIG_A),
397 regmap_reg_range(DA9062AA_VBUCK1_B, DA9062AA_VBUCK4_B),
398 regmap_reg_range(DA9062AA_VBUCK3_B, DA9062AA_VBUCK3_B),
399 regmap_reg_range(DA9062AA_VLDO1_B, DA9062AA_VLDO4_B),
400 regmap_reg_range(DA9062AA_INTERFACE, DA9062AA_CONFIG_E),
401 regmap_reg_range(DA9062AA_CONFIG_G, DA9062AA_CONFIG_K),
402 regmap_reg_range(DA9062AA_CONFIG_M, DA9062AA_CONFIG_M),
403 regmap_reg_range(DA9062AA_GP_ID_0, DA9062AA_GP_ID_19),
404 regmap_reg_range(DA9062AA_DEVICE_ID, DA9062AA_CONFIG_ID),
405};
406
407static const struct regmap_range da9061_aa_writeable_ranges[] = {
408 regmap_reg_range(DA9062AA_PAGE_CON, DA9062AA_PAGE_CON),
409 regmap_reg_range(DA9062AA_FAULT_LOG, DA9062AA_EVENT_C),
410 regmap_reg_range(DA9062AA_IRQ_MASK_A, DA9062AA_IRQ_MASK_C),
411 regmap_reg_range(DA9062AA_CONTROL_A, DA9062AA_GPIO_4),
412 regmap_reg_range(DA9062AA_GPIO_WKUP_MODE, DA9062AA_GPIO_OUT3_4),
413 regmap_reg_range(DA9062AA_BUCK1_CONT, DA9062AA_BUCK4_CONT),
414 regmap_reg_range(DA9062AA_BUCK3_CONT, DA9062AA_BUCK3_CONT),
415 regmap_reg_range(DA9062AA_LDO1_CONT, DA9062AA_LDO4_CONT),
416 regmap_reg_range(DA9062AA_DVC_1, DA9062AA_DVC_1),
417 regmap_reg_range(DA9062AA_SEQ, DA9062AA_ID_4_3),
418 regmap_reg_range(DA9062AA_ID_12_11, DA9062AA_ID_16_15),
419 regmap_reg_range(DA9062AA_ID_22_21, DA9062AA_ID_32_31),
420 regmap_reg_range(DA9062AA_SEQ_A, DA9062AA_WAIT),
421 regmap_reg_range(DA9062AA_RESET, DA9062AA_BUCK_ILIM_C),
422 regmap_reg_range(DA9062AA_BUCK1_CFG, DA9062AA_BUCK3_CFG),
423 regmap_reg_range(DA9062AA_VBUCK1_A, DA9062AA_VBUCK4_A),
424 regmap_reg_range(DA9062AA_VBUCK3_A, DA9062AA_VBUCK3_A),
425 regmap_reg_range(DA9062AA_VLDO1_A, DA9062AA_VLDO4_A),
426 regmap_reg_range(DA9062AA_CONFIG_A, DA9062AA_CONFIG_A),
427 regmap_reg_range(DA9062AA_VBUCK1_B, DA9062AA_VBUCK4_B),
428 regmap_reg_range(DA9062AA_VBUCK3_B, DA9062AA_VBUCK3_B),
429 regmap_reg_range(DA9062AA_VLDO1_B, DA9062AA_VLDO4_B),
430 regmap_reg_range(DA9062AA_CONFIG_J, DA9062AA_CONFIG_J),
431 regmap_reg_range(DA9062AA_GP_ID_0, DA9062AA_GP_ID_19),
432};
433
434static const struct regmap_range da9061_aa_volatile_ranges[] = {
435 regmap_reg_range(DA9062AA_PAGE_CON, DA9062AA_STATUS_B),
436 regmap_reg_range(DA9062AA_STATUS_D, DA9062AA_EVENT_C),
437 regmap_reg_range(DA9062AA_CONTROL_A, DA9062AA_CONTROL_B),
438 regmap_reg_range(DA9062AA_CONTROL_E, DA9062AA_CONTROL_F),
439 regmap_reg_range(DA9062AA_BUCK1_CONT, DA9062AA_BUCK4_CONT),
440 regmap_reg_range(DA9062AA_BUCK3_CONT, DA9062AA_BUCK3_CONT),
441 regmap_reg_range(DA9062AA_LDO1_CONT, DA9062AA_LDO4_CONT),
442 regmap_reg_range(DA9062AA_DVC_1, DA9062AA_DVC_1),
443 regmap_reg_range(DA9062AA_SEQ, DA9062AA_SEQ),
444};
445
446static const struct regmap_access_table da9061_aa_readable_table = {
447 .yes_ranges = da9061_aa_readable_ranges,
448 .n_yes_ranges = ARRAY_SIZE(da9061_aa_readable_ranges),
449};
450
451static const struct regmap_access_table da9061_aa_writeable_table = {
452 .yes_ranges = da9061_aa_writeable_ranges,
453 .n_yes_ranges = ARRAY_SIZE(da9061_aa_writeable_ranges),
454};
455
456static const struct regmap_access_table da9061_aa_volatile_table = {
457 .yes_ranges = da9061_aa_volatile_ranges,
458 .n_yes_ranges = ARRAY_SIZE(da9061_aa_volatile_ranges),
459};
460
461static const struct regmap_range_cfg da9061_range_cfg[] = {
462 {
463 .range_min = DA9062AA_PAGE_CON,
464 .range_max = DA9062AA_CONFIG_ID,
465 .selector_reg = DA9062AA_PAGE_CON,
466 .selector_mask = 1 << DA9062_I2C_PAGE_SEL_SHIFT,
467 .selector_shift = DA9062_I2C_PAGE_SEL_SHIFT,
468 .window_start = 0,
469 .window_len = 256,
470 }
471};
472
473static struct regmap_config da9061_regmap_config = {
474 .reg_bits = 8,
475 .val_bits = 8,
476 .ranges = da9061_range_cfg,
477 .num_ranges = ARRAY_SIZE(da9061_range_cfg),
478 .max_register = DA9062AA_CONFIG_ID,
479 .cache_type = REGCACHE_RBTREE,
480 .rd_table = &da9061_aa_readable_table,
481 .wr_table = &da9061_aa_writeable_table,
482 .volatile_table = &da9061_aa_volatile_table,
483};
484
485static const struct regmap_range da9062_aa_readable_ranges[] = {
486 regmap_reg_range(DA9062AA_PAGE_CON, DA9062AA_STATUS_B),
487 regmap_reg_range(DA9062AA_STATUS_D, DA9062AA_EVENT_C),
488 regmap_reg_range(DA9062AA_IRQ_MASK_A, DA9062AA_IRQ_MASK_C),
489 regmap_reg_range(DA9062AA_CONTROL_A, DA9062AA_GPIO_4),
490 regmap_reg_range(DA9062AA_GPIO_WKUP_MODE, DA9062AA_BUCK4_CONT),
491 regmap_reg_range(DA9062AA_BUCK3_CONT, DA9062AA_BUCK3_CONT),
492 regmap_reg_range(DA9062AA_LDO1_CONT, DA9062AA_LDO4_CONT),
493 regmap_reg_range(DA9062AA_DVC_1, DA9062AA_DVC_1),
494 regmap_reg_range(DA9062AA_COUNT_S, DA9062AA_SECOND_D),
495 regmap_reg_range(DA9062AA_SEQ, DA9062AA_ID_4_3),
496 regmap_reg_range(DA9062AA_ID_12_11, DA9062AA_ID_16_15),
497 regmap_reg_range(DA9062AA_ID_22_21, DA9062AA_ID_32_31),
498 regmap_reg_range(DA9062AA_SEQ_A, DA9062AA_BUCK3_CFG),
499 regmap_reg_range(DA9062AA_VBUCK2_A, DA9062AA_VBUCK4_A),
500 regmap_reg_range(DA9062AA_VBUCK3_A, DA9062AA_VBUCK3_A),
501 regmap_reg_range(DA9062AA_VLDO1_A, DA9062AA_VLDO4_A),
502 regmap_reg_range(DA9062AA_VBUCK2_B, DA9062AA_VBUCK4_B),
503 regmap_reg_range(DA9062AA_VBUCK3_B, DA9062AA_VBUCK3_B),
504 regmap_reg_range(DA9062AA_VLDO1_B, DA9062AA_VLDO4_B),
505 regmap_reg_range(DA9062AA_BBAT_CONT, DA9062AA_BBAT_CONT),
506 regmap_reg_range(DA9062AA_INTERFACE, DA9062AA_CONFIG_E),
507 regmap_reg_range(DA9062AA_CONFIG_G, DA9062AA_CONFIG_K),
508 regmap_reg_range(DA9062AA_CONFIG_M, DA9062AA_CONFIG_M),
509 regmap_reg_range(DA9062AA_TRIM_CLDR, DA9062AA_GP_ID_19),
510 regmap_reg_range(DA9062AA_DEVICE_ID, DA9062AA_CONFIG_ID),
511};
512
513static const struct regmap_range da9062_aa_writeable_ranges[] = {
514 regmap_reg_range(DA9062AA_PAGE_CON, DA9062AA_PAGE_CON),
515 regmap_reg_range(DA9062AA_FAULT_LOG, DA9062AA_EVENT_C),
516 regmap_reg_range(DA9062AA_IRQ_MASK_A, DA9062AA_IRQ_MASK_C),
517 regmap_reg_range(DA9062AA_CONTROL_A, DA9062AA_GPIO_4),
518 regmap_reg_range(DA9062AA_GPIO_WKUP_MODE, DA9062AA_BUCK4_CONT),
519 regmap_reg_range(DA9062AA_BUCK3_CONT, DA9062AA_BUCK3_CONT),
520 regmap_reg_range(DA9062AA_LDO1_CONT, DA9062AA_LDO4_CONT),
521 regmap_reg_range(DA9062AA_DVC_1, DA9062AA_DVC_1),
522 regmap_reg_range(DA9062AA_COUNT_S, DA9062AA_ALARM_Y),
523 regmap_reg_range(DA9062AA_SEQ, DA9062AA_ID_4_3),
524 regmap_reg_range(DA9062AA_ID_12_11, DA9062AA_ID_16_15),
525 regmap_reg_range(DA9062AA_ID_22_21, DA9062AA_ID_32_31),
526 regmap_reg_range(DA9062AA_SEQ_A, DA9062AA_BUCK3_CFG),
527 regmap_reg_range(DA9062AA_VBUCK2_A, DA9062AA_VBUCK4_A),
528 regmap_reg_range(DA9062AA_VBUCK3_A, DA9062AA_VBUCK3_A),
529 regmap_reg_range(DA9062AA_VLDO1_A, DA9062AA_VLDO4_A),
530 regmap_reg_range(DA9062AA_VBUCK2_B, DA9062AA_VBUCK4_B),
531 regmap_reg_range(DA9062AA_VBUCK3_B, DA9062AA_VBUCK3_B),
532 regmap_reg_range(DA9062AA_VLDO1_B, DA9062AA_VLDO4_B),
533 regmap_reg_range(DA9062AA_BBAT_CONT, DA9062AA_BBAT_CONT),
534 regmap_reg_range(DA9062AA_CONFIG_J, DA9062AA_CONFIG_J),
535 regmap_reg_range(DA9062AA_GP_ID_0, DA9062AA_GP_ID_19),
536};
537
538static const struct regmap_range da9062_aa_volatile_ranges[] = {
539 regmap_reg_range(DA9062AA_PAGE_CON, DA9062AA_STATUS_B),
540 regmap_reg_range(DA9062AA_STATUS_D, DA9062AA_EVENT_C),
541 regmap_reg_range(DA9062AA_CONTROL_A, DA9062AA_CONTROL_B),
542 regmap_reg_range(DA9062AA_CONTROL_E, DA9062AA_CONTROL_F),
543 regmap_reg_range(DA9062AA_BUCK2_CONT, DA9062AA_BUCK4_CONT),
544 regmap_reg_range(DA9062AA_BUCK3_CONT, DA9062AA_BUCK3_CONT),
545 regmap_reg_range(DA9062AA_LDO1_CONT, DA9062AA_LDO4_CONT),
546 regmap_reg_range(DA9062AA_DVC_1, DA9062AA_DVC_1),
547 regmap_reg_range(DA9062AA_COUNT_S, DA9062AA_SECOND_D),
548 regmap_reg_range(DA9062AA_SEQ, DA9062AA_SEQ),
549 regmap_reg_range(DA9062AA_EN_32K, DA9062AA_EN_32K),
550};
551
552static const struct regmap_access_table da9062_aa_readable_table = {
553 .yes_ranges = da9062_aa_readable_ranges,
554 .n_yes_ranges = ARRAY_SIZE(da9062_aa_readable_ranges),
555};
556
557static const struct regmap_access_table da9062_aa_writeable_table = {
558 .yes_ranges = da9062_aa_writeable_ranges,
559 .n_yes_ranges = ARRAY_SIZE(da9062_aa_writeable_ranges),
560};
561
562static const struct regmap_access_table da9062_aa_volatile_table = {
563 .yes_ranges = da9062_aa_volatile_ranges,
564 .n_yes_ranges = ARRAY_SIZE(da9062_aa_volatile_ranges),
565};
566
567static const struct regmap_range_cfg da9062_range_cfg[] = {
568 {
569 .range_min = DA9062AA_PAGE_CON,
570 .range_max = DA9062AA_CONFIG_ID,
571 .selector_reg = DA9062AA_PAGE_CON,
572 .selector_mask = 1 << DA9062_I2C_PAGE_SEL_SHIFT,
573 .selector_shift = DA9062_I2C_PAGE_SEL_SHIFT,
574 .window_start = 0,
575 .window_len = 256,
576 }
577};
578
579static struct regmap_config da9062_regmap_config = {
580 .reg_bits = 8,
581 .val_bits = 8,
582 .ranges = da9062_range_cfg,
583 .num_ranges = ARRAY_SIZE(da9062_range_cfg),
584 .max_register = DA9062AA_CONFIG_ID,
585 .cache_type = REGCACHE_RBTREE,
586 .rd_table = &da9062_aa_readable_table,
587 .wr_table = &da9062_aa_writeable_table,
588 .volatile_table = &da9062_aa_volatile_table,
589};
590
591static int da9062_i2c_probe(struct i2c_client *i2c)
592{
593 struct da9062 *chip;
594 unsigned int irq_base = 0;
595 const struct mfd_cell *cell;
596 const struct regmap_irq_chip *irq_chip;
597 const struct regmap_config *config;
598 int cell_num;
599 u32 trigger_type = 0;
600 int ret;
601
602 chip = devm_kzalloc(&i2c->dev, sizeof(*chip), GFP_KERNEL);
603 if (!chip)
604 return -ENOMEM;
605
606 chip->chip_type = (uintptr_t)i2c_get_match_data(i2c);
607
608 i2c_set_clientdata(i2c, chip);
609 chip->dev = &i2c->dev;
610
611 /* Start with a base configuration without IRQ */
612 switch (chip->chip_type) {
613 case COMPAT_TYPE_DA9061:
614 cell = da9061_devs_noirq;
615 cell_num = ARRAY_SIZE(da9061_devs_noirq);
616 config = &da9061_regmap_config;
617 break;
618 case COMPAT_TYPE_DA9062:
619 cell = da9062_devs_noirq;
620 cell_num = ARRAY_SIZE(da9062_devs_noirq);
621 config = &da9062_regmap_config;
622 break;
623 default:
624 dev_err(chip->dev, "Unrecognised chip type\n");
625 return -ENODEV;
626 }
627
628 chip->regmap = devm_regmap_init_i2c(i2c, config);
629 if (IS_ERR(chip->regmap)) {
630 ret = PTR_ERR(chip->regmap);
631 dev_err(chip->dev, "Failed to allocate register map: %d\n",
632 ret);
633 return ret;
634 }
635
636 /* If SMBus is not available and only I2C is possible, enter I2C mode */
637 if (i2c_check_functionality(i2c->adapter, I2C_FUNC_I2C)) {
638 dev_info(chip->dev, "Entering I2C mode!\n");
639 ret = regmap_clear_bits(chip->regmap, DA9062AA_CONFIG_J,
640 DA9062AA_TWOWIRE_TO_MASK);
641 if (ret < 0) {
642 dev_err(chip->dev, "Failed to set Two-Wire Bus Mode.\n");
643 return ret;
644 }
645 }
646
647 ret = da9062_clear_fault_log(chip);
648 if (ret < 0)
649 dev_warn(chip->dev, "Cannot clear fault log\n");
650
651 ret = da9062_get_device_type(chip);
652 if (ret)
653 return ret;
654
655 /* If IRQ is available, reconfigure it accordingly */
656 if (i2c->irq) {
657 if (chip->chip_type == COMPAT_TYPE_DA9061) {
658 cell = da9061_devs_irq;
659 cell_num = ARRAY_SIZE(da9061_devs_irq);
660 irq_chip = &da9061_irq_chip;
661 } else {
662 cell = da9062_devs_irq;
663 cell_num = ARRAY_SIZE(da9062_devs_irq);
664 irq_chip = &da9062_irq_chip;
665 }
666
667 ret = da9062_configure_irq_type(chip, i2c->irq, &trigger_type);
668 if (ret < 0) {
669 dev_err(chip->dev, "Failed to configure IRQ type\n");
670 return ret;
671 }
672
673 ret = regmap_add_irq_chip(chip->regmap, i2c->irq,
674 trigger_type | IRQF_SHARED | IRQF_ONESHOT,
675 -1, irq_chip, &chip->regmap_irq);
676 if (ret) {
677 dev_err(chip->dev, "Failed to request IRQ %d: %d\n",
678 i2c->irq, ret);
679 return ret;
680 }
681
682 irq_base = regmap_irq_chip_get_base(chip->regmap_irq);
683 }
684
685 ret = mfd_add_devices(chip->dev, PLATFORM_DEVID_NONE, cell,
686 cell_num, NULL, irq_base,
687 NULL);
688 if (ret) {
689 dev_err(chip->dev, "Cannot register child devices\n");
690 if (i2c->irq)
691 regmap_del_irq_chip(i2c->irq, chip->regmap_irq);
692 return ret;
693 }
694
695 return ret;
696}
697
698static void da9062_i2c_remove(struct i2c_client *i2c)
699{
700 struct da9062 *chip = i2c_get_clientdata(i2c);
701
702 mfd_remove_devices(chip->dev);
703 regmap_del_irq_chip(i2c->irq, chip->regmap_irq);
704}
705
706static const struct of_device_id da9062_dt_ids[] = {
707 { .compatible = "dlg,da9061", .data = (void *)COMPAT_TYPE_DA9061 },
708 { .compatible = "dlg,da9062", .data = (void *)COMPAT_TYPE_DA9062 },
709 { }
710};
711MODULE_DEVICE_TABLE(of, da9062_dt_ids);
712
713static const struct i2c_device_id da9062_i2c_id[] = {
714 { "da9061", COMPAT_TYPE_DA9061 },
715 { "da9062", COMPAT_TYPE_DA9062 },
716 { }
717};
718MODULE_DEVICE_TABLE(i2c, da9062_i2c_id);
719
720static struct i2c_driver da9062_i2c_driver = {
721 .driver = {
722 .name = "da9062",
723 .of_match_table = da9062_dt_ids,
724 },
725 .probe = da9062_i2c_probe,
726 .remove = da9062_i2c_remove,
727 .id_table = da9062_i2c_id,
728};
729
730module_i2c_driver(da9062_i2c_driver);
731
732MODULE_DESCRIPTION("Core device driver for Dialog DA9061 and DA9062");
733MODULE_AUTHOR("Steve Twiss <stwiss.opensource@diasemi.com>");
734MODULE_LICENSE("GPL");
1/*
2 * Core, IRQ and I2C device driver for DA9061 and DA9062 PMICs
3 * Copyright (C) 2015-2017 Dialog Semiconductor
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
16#include <linux/kernel.h>
17#include <linux/module.h>
18#include <linux/init.h>
19#include <linux/device.h>
20#include <linux/interrupt.h>
21#include <linux/regmap.h>
22#include <linux/irq.h>
23#include <linux/mfd/core.h>
24#include <linux/i2c.h>
25#include <linux/mfd/da9062/core.h>
26#include <linux/mfd/da9062/registers.h>
27#include <linux/regulator/of_regulator.h>
28
29#define DA9062_REG_EVENT_A_OFFSET 0
30#define DA9062_REG_EVENT_B_OFFSET 1
31#define DA9062_REG_EVENT_C_OFFSET 2
32
33static struct regmap_irq da9061_irqs[] = {
34 /* EVENT A */
35 [DA9061_IRQ_ONKEY] = {
36 .reg_offset = DA9062_REG_EVENT_A_OFFSET,
37 .mask = DA9062AA_M_NONKEY_MASK,
38 },
39 [DA9061_IRQ_WDG_WARN] = {
40 .reg_offset = DA9062_REG_EVENT_A_OFFSET,
41 .mask = DA9062AA_M_WDG_WARN_MASK,
42 },
43 [DA9061_IRQ_SEQ_RDY] = {
44 .reg_offset = DA9062_REG_EVENT_A_OFFSET,
45 .mask = DA9062AA_M_SEQ_RDY_MASK,
46 },
47 /* EVENT B */
48 [DA9061_IRQ_TEMP] = {
49 .reg_offset = DA9062_REG_EVENT_B_OFFSET,
50 .mask = DA9062AA_M_TEMP_MASK,
51 },
52 [DA9061_IRQ_LDO_LIM] = {
53 .reg_offset = DA9062_REG_EVENT_B_OFFSET,
54 .mask = DA9062AA_M_LDO_LIM_MASK,
55 },
56 [DA9061_IRQ_DVC_RDY] = {
57 .reg_offset = DA9062_REG_EVENT_B_OFFSET,
58 .mask = DA9062AA_M_DVC_RDY_MASK,
59 },
60 [DA9061_IRQ_VDD_WARN] = {
61 .reg_offset = DA9062_REG_EVENT_B_OFFSET,
62 .mask = DA9062AA_M_VDD_WARN_MASK,
63 },
64 /* EVENT C */
65 [DA9061_IRQ_GPI0] = {
66 .reg_offset = DA9062_REG_EVENT_C_OFFSET,
67 .mask = DA9062AA_M_GPI0_MASK,
68 },
69 [DA9061_IRQ_GPI1] = {
70 .reg_offset = DA9062_REG_EVENT_C_OFFSET,
71 .mask = DA9062AA_M_GPI1_MASK,
72 },
73 [DA9061_IRQ_GPI2] = {
74 .reg_offset = DA9062_REG_EVENT_C_OFFSET,
75 .mask = DA9062AA_M_GPI2_MASK,
76 },
77 [DA9061_IRQ_GPI3] = {
78 .reg_offset = DA9062_REG_EVENT_C_OFFSET,
79 .mask = DA9062AA_M_GPI3_MASK,
80 },
81 [DA9061_IRQ_GPI4] = {
82 .reg_offset = DA9062_REG_EVENT_C_OFFSET,
83 .mask = DA9062AA_M_GPI4_MASK,
84 },
85};
86
87static struct regmap_irq_chip da9061_irq_chip = {
88 .name = "da9061-irq",
89 .irqs = da9061_irqs,
90 .num_irqs = DA9061_NUM_IRQ,
91 .num_regs = 3,
92 .status_base = DA9062AA_EVENT_A,
93 .mask_base = DA9062AA_IRQ_MASK_A,
94 .ack_base = DA9062AA_EVENT_A,
95};
96
97static struct regmap_irq da9062_irqs[] = {
98 /* EVENT A */
99 [DA9062_IRQ_ONKEY] = {
100 .reg_offset = DA9062_REG_EVENT_A_OFFSET,
101 .mask = DA9062AA_M_NONKEY_MASK,
102 },
103 [DA9062_IRQ_ALARM] = {
104 .reg_offset = DA9062_REG_EVENT_A_OFFSET,
105 .mask = DA9062AA_M_ALARM_MASK,
106 },
107 [DA9062_IRQ_TICK] = {
108 .reg_offset = DA9062_REG_EVENT_A_OFFSET,
109 .mask = DA9062AA_M_TICK_MASK,
110 },
111 [DA9062_IRQ_WDG_WARN] = {
112 .reg_offset = DA9062_REG_EVENT_A_OFFSET,
113 .mask = DA9062AA_M_WDG_WARN_MASK,
114 },
115 [DA9062_IRQ_SEQ_RDY] = {
116 .reg_offset = DA9062_REG_EVENT_A_OFFSET,
117 .mask = DA9062AA_M_SEQ_RDY_MASK,
118 },
119 /* EVENT B */
120 [DA9062_IRQ_TEMP] = {
121 .reg_offset = DA9062_REG_EVENT_B_OFFSET,
122 .mask = DA9062AA_M_TEMP_MASK,
123 },
124 [DA9062_IRQ_LDO_LIM] = {
125 .reg_offset = DA9062_REG_EVENT_B_OFFSET,
126 .mask = DA9062AA_M_LDO_LIM_MASK,
127 },
128 [DA9062_IRQ_DVC_RDY] = {
129 .reg_offset = DA9062_REG_EVENT_B_OFFSET,
130 .mask = DA9062AA_M_DVC_RDY_MASK,
131 },
132 [DA9062_IRQ_VDD_WARN] = {
133 .reg_offset = DA9062_REG_EVENT_B_OFFSET,
134 .mask = DA9062AA_M_VDD_WARN_MASK,
135 },
136 /* EVENT C */
137 [DA9062_IRQ_GPI0] = {
138 .reg_offset = DA9062_REG_EVENT_C_OFFSET,
139 .mask = DA9062AA_M_GPI0_MASK,
140 },
141 [DA9062_IRQ_GPI1] = {
142 .reg_offset = DA9062_REG_EVENT_C_OFFSET,
143 .mask = DA9062AA_M_GPI1_MASK,
144 },
145 [DA9062_IRQ_GPI2] = {
146 .reg_offset = DA9062_REG_EVENT_C_OFFSET,
147 .mask = DA9062AA_M_GPI2_MASK,
148 },
149 [DA9062_IRQ_GPI3] = {
150 .reg_offset = DA9062_REG_EVENT_C_OFFSET,
151 .mask = DA9062AA_M_GPI3_MASK,
152 },
153 [DA9062_IRQ_GPI4] = {
154 .reg_offset = DA9062_REG_EVENT_C_OFFSET,
155 .mask = DA9062AA_M_GPI4_MASK,
156 },
157};
158
159static struct regmap_irq_chip da9062_irq_chip = {
160 .name = "da9062-irq",
161 .irqs = da9062_irqs,
162 .num_irqs = DA9062_NUM_IRQ,
163 .num_regs = 3,
164 .status_base = DA9062AA_EVENT_A,
165 .mask_base = DA9062AA_IRQ_MASK_A,
166 .ack_base = DA9062AA_EVENT_A,
167};
168
169static struct resource da9061_core_resources[] = {
170 DEFINE_RES_IRQ_NAMED(DA9061_IRQ_VDD_WARN, "VDD_WARN"),
171};
172
173static struct resource da9061_regulators_resources[] = {
174 DEFINE_RES_IRQ_NAMED(DA9061_IRQ_LDO_LIM, "LDO_LIM"),
175};
176
177static struct resource da9061_thermal_resources[] = {
178 DEFINE_RES_IRQ_NAMED(DA9061_IRQ_TEMP, "THERMAL"),
179};
180
181static struct resource da9061_wdt_resources[] = {
182 DEFINE_RES_IRQ_NAMED(DA9061_IRQ_WDG_WARN, "WD_WARN"),
183};
184
185static struct resource da9061_onkey_resources[] = {
186 DEFINE_RES_IRQ_NAMED(DA9061_IRQ_ONKEY, "ONKEY"),
187};
188
189static const struct mfd_cell da9061_devs[] = {
190 {
191 .name = "da9061-core",
192 .num_resources = ARRAY_SIZE(da9061_core_resources),
193 .resources = da9061_core_resources,
194 },
195 {
196 .name = "da9062-regulators",
197 .num_resources = ARRAY_SIZE(da9061_regulators_resources),
198 .resources = da9061_regulators_resources,
199 },
200 {
201 .name = "da9061-watchdog",
202 .num_resources = ARRAY_SIZE(da9061_wdt_resources),
203 .resources = da9061_wdt_resources,
204 .of_compatible = "dlg,da9061-watchdog",
205 },
206 {
207 .name = "da9061-thermal",
208 .num_resources = ARRAY_SIZE(da9061_thermal_resources),
209 .resources = da9061_thermal_resources,
210 .of_compatible = "dlg,da9061-thermal",
211 },
212 {
213 .name = "da9061-onkey",
214 .num_resources = ARRAY_SIZE(da9061_onkey_resources),
215 .resources = da9061_onkey_resources,
216 .of_compatible = "dlg,da9061-onkey",
217 },
218};
219
220static struct resource da9062_core_resources[] = {
221 DEFINE_RES_NAMED(DA9062_IRQ_VDD_WARN, 1, "VDD_WARN", IORESOURCE_IRQ),
222};
223
224static struct resource da9062_regulators_resources[] = {
225 DEFINE_RES_NAMED(DA9062_IRQ_LDO_LIM, 1, "LDO_LIM", IORESOURCE_IRQ),
226};
227
228static struct resource da9062_thermal_resources[] = {
229 DEFINE_RES_NAMED(DA9062_IRQ_TEMP, 1, "THERMAL", IORESOURCE_IRQ),
230};
231
232static struct resource da9062_wdt_resources[] = {
233 DEFINE_RES_NAMED(DA9062_IRQ_WDG_WARN, 1, "WD_WARN", IORESOURCE_IRQ),
234};
235
236static struct resource da9062_rtc_resources[] = {
237 DEFINE_RES_NAMED(DA9062_IRQ_ALARM, 1, "ALARM", IORESOURCE_IRQ),
238 DEFINE_RES_NAMED(DA9062_IRQ_TICK, 1, "TICK", IORESOURCE_IRQ),
239};
240
241static struct resource da9062_onkey_resources[] = {
242 DEFINE_RES_NAMED(DA9062_IRQ_ONKEY, 1, "ONKEY", IORESOURCE_IRQ),
243};
244
245static const struct mfd_cell da9062_devs[] = {
246 {
247 .name = "da9062-core",
248 .num_resources = ARRAY_SIZE(da9062_core_resources),
249 .resources = da9062_core_resources,
250 },
251 {
252 .name = "da9062-regulators",
253 .num_resources = ARRAY_SIZE(da9062_regulators_resources),
254 .resources = da9062_regulators_resources,
255 },
256 {
257 .name = "da9062-watchdog",
258 .num_resources = ARRAY_SIZE(da9062_wdt_resources),
259 .resources = da9062_wdt_resources,
260 .of_compatible = "dlg,da9062-wdt",
261 },
262 {
263 .name = "da9062-thermal",
264 .num_resources = ARRAY_SIZE(da9062_thermal_resources),
265 .resources = da9062_thermal_resources,
266 .of_compatible = "dlg,da9062-thermal",
267 },
268 {
269 .name = "da9062-rtc",
270 .num_resources = ARRAY_SIZE(da9062_rtc_resources),
271 .resources = da9062_rtc_resources,
272 .of_compatible = "dlg,da9062-rtc",
273 },
274 {
275 .name = "da9062-onkey",
276 .num_resources = ARRAY_SIZE(da9062_onkey_resources),
277 .resources = da9062_onkey_resources,
278 .of_compatible = "dlg,da9062-onkey",
279 },
280};
281
282static int da9062_clear_fault_log(struct da9062 *chip)
283{
284 int ret;
285 int fault_log;
286
287 ret = regmap_read(chip->regmap, DA9062AA_FAULT_LOG, &fault_log);
288 if (ret < 0)
289 return ret;
290
291 if (fault_log) {
292 if (fault_log & DA9062AA_TWD_ERROR_MASK)
293 dev_dbg(chip->dev, "Fault log entry detected: TWD_ERROR\n");
294 if (fault_log & DA9062AA_POR_MASK)
295 dev_dbg(chip->dev, "Fault log entry detected: POR\n");
296 if (fault_log & DA9062AA_VDD_FAULT_MASK)
297 dev_dbg(chip->dev, "Fault log entry detected: VDD_FAULT\n");
298 if (fault_log & DA9062AA_VDD_START_MASK)
299 dev_dbg(chip->dev, "Fault log entry detected: VDD_START\n");
300 if (fault_log & DA9062AA_TEMP_CRIT_MASK)
301 dev_dbg(chip->dev, "Fault log entry detected: TEMP_CRIT\n");
302 if (fault_log & DA9062AA_KEY_RESET_MASK)
303 dev_dbg(chip->dev, "Fault log entry detected: KEY_RESET\n");
304 if (fault_log & DA9062AA_NSHUTDOWN_MASK)
305 dev_dbg(chip->dev, "Fault log entry detected: NSHUTDOWN\n");
306 if (fault_log & DA9062AA_WAIT_SHUT_MASK)
307 dev_dbg(chip->dev, "Fault log entry detected: WAIT_SHUT\n");
308
309 ret = regmap_write(chip->regmap, DA9062AA_FAULT_LOG,
310 fault_log);
311 }
312
313 return ret;
314}
315
316static int da9062_get_device_type(struct da9062 *chip)
317{
318 int device_id, variant_id, variant_mrc, variant_vrc;
319 char *type;
320 int ret;
321
322 ret = regmap_read(chip->regmap, DA9062AA_DEVICE_ID, &device_id);
323 if (ret < 0) {
324 dev_err(chip->dev, "Cannot read chip ID.\n");
325 return -EIO;
326 }
327 if (device_id != DA9062_PMIC_DEVICE_ID) {
328 dev_err(chip->dev, "Invalid device ID: 0x%02x\n", device_id);
329 return -ENODEV;
330 }
331
332 ret = regmap_read(chip->regmap, DA9062AA_VARIANT_ID, &variant_id);
333 if (ret < 0) {
334 dev_err(chip->dev, "Cannot read chip variant id.\n");
335 return -EIO;
336 }
337
338 variant_vrc = (variant_id & DA9062AA_VRC_MASK) >> DA9062AA_VRC_SHIFT;
339
340 switch (variant_vrc) {
341 case DA9062_PMIC_VARIANT_VRC_DA9061:
342 type = "DA9061";
343 break;
344 case DA9062_PMIC_VARIANT_VRC_DA9062:
345 type = "DA9062";
346 break;
347 default:
348 type = "Unknown";
349 break;
350 }
351
352 dev_info(chip->dev,
353 "Device detected (device-ID: 0x%02X, var-ID: 0x%02X, %s)\n",
354 device_id, variant_id, type);
355
356 variant_mrc = (variant_id & DA9062AA_MRC_MASK) >> DA9062AA_MRC_SHIFT;
357
358 if (variant_mrc < DA9062_PMIC_VARIANT_MRC_AA) {
359 dev_err(chip->dev,
360 "Cannot support variant MRC: 0x%02X\n", variant_mrc);
361 return -ENODEV;
362 }
363
364 return ret;
365}
366
367static const struct regmap_range da9061_aa_readable_ranges[] = {
368 {
369 .range_min = DA9062AA_PAGE_CON,
370 .range_max = DA9062AA_STATUS_B,
371 }, {
372 .range_min = DA9062AA_STATUS_D,
373 .range_max = DA9062AA_EVENT_C,
374 }, {
375 .range_min = DA9062AA_IRQ_MASK_A,
376 .range_max = DA9062AA_IRQ_MASK_C,
377 }, {
378 .range_min = DA9062AA_CONTROL_A,
379 .range_max = DA9062AA_GPIO_4,
380 }, {
381 .range_min = DA9062AA_GPIO_WKUP_MODE,
382 .range_max = DA9062AA_GPIO_OUT3_4,
383 }, {
384 .range_min = DA9062AA_BUCK1_CONT,
385 .range_max = DA9062AA_BUCK4_CONT,
386 }, {
387 .range_min = DA9062AA_BUCK3_CONT,
388 .range_max = DA9062AA_BUCK3_CONT,
389 }, {
390 .range_min = DA9062AA_LDO1_CONT,
391 .range_max = DA9062AA_LDO4_CONT,
392 }, {
393 .range_min = DA9062AA_DVC_1,
394 .range_max = DA9062AA_DVC_1,
395 }, {
396 .range_min = DA9062AA_SEQ,
397 .range_max = DA9062AA_ID_4_3,
398 }, {
399 .range_min = DA9062AA_ID_12_11,
400 .range_max = DA9062AA_ID_16_15,
401 }, {
402 .range_min = DA9062AA_ID_22_21,
403 .range_max = DA9062AA_ID_32_31,
404 }, {
405 .range_min = DA9062AA_SEQ_A,
406 .range_max = DA9062AA_WAIT,
407 }, {
408 .range_min = DA9062AA_RESET,
409 .range_max = DA9062AA_BUCK_ILIM_C,
410 }, {
411 .range_min = DA9062AA_BUCK1_CFG,
412 .range_max = DA9062AA_BUCK3_CFG,
413 }, {
414 .range_min = DA9062AA_VBUCK1_A,
415 .range_max = DA9062AA_VBUCK4_A,
416 }, {
417 .range_min = DA9062AA_VBUCK3_A,
418 .range_max = DA9062AA_VBUCK3_A,
419 }, {
420 .range_min = DA9062AA_VLDO1_A,
421 .range_max = DA9062AA_VLDO4_A,
422 }, {
423 .range_min = DA9062AA_VBUCK1_B,
424 .range_max = DA9062AA_VBUCK4_B,
425 }, {
426 .range_min = DA9062AA_VBUCK3_B,
427 .range_max = DA9062AA_VBUCK3_B,
428 }, {
429 .range_min = DA9062AA_VLDO1_B,
430 .range_max = DA9062AA_VLDO4_B,
431 }, {
432 .range_min = DA9062AA_INTERFACE,
433 .range_max = DA9062AA_CONFIG_E,
434 }, {
435 .range_min = DA9062AA_CONFIG_G,
436 .range_max = DA9062AA_CONFIG_K,
437 }, {
438 .range_min = DA9062AA_CONFIG_M,
439 .range_max = DA9062AA_CONFIG_M,
440 }, {
441 .range_min = DA9062AA_GP_ID_0,
442 .range_max = DA9062AA_GP_ID_19,
443 }, {
444 .range_min = DA9062AA_DEVICE_ID,
445 .range_max = DA9062AA_CONFIG_ID,
446 },
447};
448
449static const struct regmap_range da9061_aa_writeable_ranges[] = {
450 {
451 .range_min = DA9062AA_PAGE_CON,
452 .range_max = DA9062AA_PAGE_CON,
453 }, {
454 .range_min = DA9062AA_FAULT_LOG,
455 .range_max = DA9062AA_EVENT_C,
456 }, {
457 .range_min = DA9062AA_IRQ_MASK_A,
458 .range_max = DA9062AA_IRQ_MASK_C,
459 }, {
460 .range_min = DA9062AA_CONTROL_A,
461 .range_max = DA9062AA_GPIO_4,
462 }, {
463 .range_min = DA9062AA_GPIO_WKUP_MODE,
464 .range_max = DA9062AA_GPIO_OUT3_4,
465 }, {
466 .range_min = DA9062AA_BUCK1_CONT,
467 .range_max = DA9062AA_BUCK4_CONT,
468 }, {
469 .range_min = DA9062AA_BUCK3_CONT,
470 .range_max = DA9062AA_BUCK3_CONT,
471 }, {
472 .range_min = DA9062AA_LDO1_CONT,
473 .range_max = DA9062AA_LDO4_CONT,
474 }, {
475 .range_min = DA9062AA_DVC_1,
476 .range_max = DA9062AA_DVC_1,
477 }, {
478 .range_min = DA9062AA_SEQ,
479 .range_max = DA9062AA_ID_4_3,
480 }, {
481 .range_min = DA9062AA_ID_12_11,
482 .range_max = DA9062AA_ID_16_15,
483 }, {
484 .range_min = DA9062AA_ID_22_21,
485 .range_max = DA9062AA_ID_32_31,
486 }, {
487 .range_min = DA9062AA_SEQ_A,
488 .range_max = DA9062AA_WAIT,
489 }, {
490 .range_min = DA9062AA_RESET,
491 .range_max = DA9062AA_BUCK_ILIM_C,
492 }, {
493 .range_min = DA9062AA_BUCK1_CFG,
494 .range_max = DA9062AA_BUCK3_CFG,
495 }, {
496 .range_min = DA9062AA_VBUCK1_A,
497 .range_max = DA9062AA_VBUCK4_A,
498 }, {
499 .range_min = DA9062AA_VBUCK3_A,
500 .range_max = DA9062AA_VBUCK3_A,
501 }, {
502 .range_min = DA9062AA_VLDO1_A,
503 .range_max = DA9062AA_VLDO4_A,
504 }, {
505 .range_min = DA9062AA_VBUCK1_B,
506 .range_max = DA9062AA_VBUCK4_B,
507 }, {
508 .range_min = DA9062AA_VBUCK3_B,
509 .range_max = DA9062AA_VBUCK3_B,
510 }, {
511 .range_min = DA9062AA_VLDO1_B,
512 .range_max = DA9062AA_VLDO4_B,
513 }, {
514 .range_min = DA9062AA_GP_ID_0,
515 .range_max = DA9062AA_GP_ID_19,
516 },
517};
518
519static const struct regmap_range da9061_aa_volatile_ranges[] = {
520 {
521 .range_min = DA9062AA_PAGE_CON,
522 .range_max = DA9062AA_STATUS_B,
523 }, {
524 .range_min = DA9062AA_STATUS_D,
525 .range_max = DA9062AA_EVENT_C,
526 }, {
527 .range_min = DA9062AA_CONTROL_A,
528 .range_max = DA9062AA_CONTROL_B,
529 }, {
530 .range_min = DA9062AA_CONTROL_E,
531 .range_max = DA9062AA_CONTROL_F,
532 }, {
533 .range_min = DA9062AA_BUCK1_CONT,
534 .range_max = DA9062AA_BUCK4_CONT,
535 }, {
536 .range_min = DA9062AA_BUCK3_CONT,
537 .range_max = DA9062AA_BUCK3_CONT,
538 }, {
539 .range_min = DA9062AA_LDO1_CONT,
540 .range_max = DA9062AA_LDO4_CONT,
541 }, {
542 .range_min = DA9062AA_DVC_1,
543 .range_max = DA9062AA_DVC_1,
544 }, {
545 .range_min = DA9062AA_SEQ,
546 .range_max = DA9062AA_SEQ,
547 },
548};
549
550static const struct regmap_access_table da9061_aa_readable_table = {
551 .yes_ranges = da9061_aa_readable_ranges,
552 .n_yes_ranges = ARRAY_SIZE(da9061_aa_readable_ranges),
553};
554
555static const struct regmap_access_table da9061_aa_writeable_table = {
556 .yes_ranges = da9061_aa_writeable_ranges,
557 .n_yes_ranges = ARRAY_SIZE(da9061_aa_writeable_ranges),
558};
559
560static const struct regmap_access_table da9061_aa_volatile_table = {
561 .yes_ranges = da9061_aa_volatile_ranges,
562 .n_yes_ranges = ARRAY_SIZE(da9061_aa_volatile_ranges),
563};
564
565static const struct regmap_range_cfg da9061_range_cfg[] = {
566 {
567 .range_min = DA9062AA_PAGE_CON,
568 .range_max = DA9062AA_CONFIG_ID,
569 .selector_reg = DA9062AA_PAGE_CON,
570 .selector_mask = 1 << DA9062_I2C_PAGE_SEL_SHIFT,
571 .selector_shift = DA9062_I2C_PAGE_SEL_SHIFT,
572 .window_start = 0,
573 .window_len = 256,
574 }
575};
576
577static struct regmap_config da9061_regmap_config = {
578 .reg_bits = 8,
579 .val_bits = 8,
580 .ranges = da9061_range_cfg,
581 .num_ranges = ARRAY_SIZE(da9061_range_cfg),
582 .max_register = DA9062AA_CONFIG_ID,
583 .cache_type = REGCACHE_RBTREE,
584 .rd_table = &da9061_aa_readable_table,
585 .wr_table = &da9061_aa_writeable_table,
586 .volatile_table = &da9061_aa_volatile_table,
587};
588
589static const struct regmap_range da9062_aa_readable_ranges[] = {
590 {
591 .range_min = DA9062AA_PAGE_CON,
592 .range_max = DA9062AA_STATUS_B,
593 }, {
594 .range_min = DA9062AA_STATUS_D,
595 .range_max = DA9062AA_EVENT_C,
596 }, {
597 .range_min = DA9062AA_IRQ_MASK_A,
598 .range_max = DA9062AA_IRQ_MASK_C,
599 }, {
600 .range_min = DA9062AA_CONTROL_A,
601 .range_max = DA9062AA_GPIO_4,
602 }, {
603 .range_min = DA9062AA_GPIO_WKUP_MODE,
604 .range_max = DA9062AA_BUCK4_CONT,
605 }, {
606 .range_min = DA9062AA_BUCK3_CONT,
607 .range_max = DA9062AA_BUCK3_CONT,
608 }, {
609 .range_min = DA9062AA_LDO1_CONT,
610 .range_max = DA9062AA_LDO4_CONT,
611 }, {
612 .range_min = DA9062AA_DVC_1,
613 .range_max = DA9062AA_DVC_1,
614 }, {
615 .range_min = DA9062AA_COUNT_S,
616 .range_max = DA9062AA_SECOND_D,
617 }, {
618 .range_min = DA9062AA_SEQ,
619 .range_max = DA9062AA_ID_4_3,
620 }, {
621 .range_min = DA9062AA_ID_12_11,
622 .range_max = DA9062AA_ID_16_15,
623 }, {
624 .range_min = DA9062AA_ID_22_21,
625 .range_max = DA9062AA_ID_32_31,
626 }, {
627 .range_min = DA9062AA_SEQ_A,
628 .range_max = DA9062AA_BUCK3_CFG,
629 }, {
630 .range_min = DA9062AA_VBUCK2_A,
631 .range_max = DA9062AA_VBUCK4_A,
632 }, {
633 .range_min = DA9062AA_VBUCK3_A,
634 .range_max = DA9062AA_VBUCK3_A,
635 }, {
636 .range_min = DA9062AA_VLDO1_A,
637 .range_max = DA9062AA_VLDO4_A,
638 }, {
639 .range_min = DA9062AA_VBUCK2_B,
640 .range_max = DA9062AA_VBUCK4_B,
641 }, {
642 .range_min = DA9062AA_VBUCK3_B,
643 .range_max = DA9062AA_VBUCK3_B,
644 }, {
645 .range_min = DA9062AA_VLDO1_B,
646 .range_max = DA9062AA_VLDO4_B,
647 }, {
648 .range_min = DA9062AA_BBAT_CONT,
649 .range_max = DA9062AA_BBAT_CONT,
650 }, {
651 .range_min = DA9062AA_INTERFACE,
652 .range_max = DA9062AA_CONFIG_E,
653 }, {
654 .range_min = DA9062AA_CONFIG_G,
655 .range_max = DA9062AA_CONFIG_K,
656 }, {
657 .range_min = DA9062AA_CONFIG_M,
658 .range_max = DA9062AA_CONFIG_M,
659 }, {
660 .range_min = DA9062AA_TRIM_CLDR,
661 .range_max = DA9062AA_GP_ID_19,
662 }, {
663 .range_min = DA9062AA_DEVICE_ID,
664 .range_max = DA9062AA_CONFIG_ID,
665 },
666};
667
668static const struct regmap_range da9062_aa_writeable_ranges[] = {
669 {
670 .range_min = DA9062AA_PAGE_CON,
671 .range_max = DA9062AA_PAGE_CON,
672 }, {
673 .range_min = DA9062AA_FAULT_LOG,
674 .range_max = DA9062AA_EVENT_C,
675 }, {
676 .range_min = DA9062AA_IRQ_MASK_A,
677 .range_max = DA9062AA_IRQ_MASK_C,
678 }, {
679 .range_min = DA9062AA_CONTROL_A,
680 .range_max = DA9062AA_GPIO_4,
681 }, {
682 .range_min = DA9062AA_GPIO_WKUP_MODE,
683 .range_max = DA9062AA_BUCK4_CONT,
684 }, {
685 .range_min = DA9062AA_BUCK3_CONT,
686 .range_max = DA9062AA_BUCK3_CONT,
687 }, {
688 .range_min = DA9062AA_LDO1_CONT,
689 .range_max = DA9062AA_LDO4_CONT,
690 }, {
691 .range_min = DA9062AA_DVC_1,
692 .range_max = DA9062AA_DVC_1,
693 }, {
694 .range_min = DA9062AA_COUNT_S,
695 .range_max = DA9062AA_ALARM_Y,
696 }, {
697 .range_min = DA9062AA_SEQ,
698 .range_max = DA9062AA_ID_4_3,
699 }, {
700 .range_min = DA9062AA_ID_12_11,
701 .range_max = DA9062AA_ID_16_15,
702 }, {
703 .range_min = DA9062AA_ID_22_21,
704 .range_max = DA9062AA_ID_32_31,
705 }, {
706 .range_min = DA9062AA_SEQ_A,
707 .range_max = DA9062AA_BUCK3_CFG,
708 }, {
709 .range_min = DA9062AA_VBUCK2_A,
710 .range_max = DA9062AA_VBUCK4_A,
711 }, {
712 .range_min = DA9062AA_VBUCK3_A,
713 .range_max = DA9062AA_VBUCK3_A,
714 }, {
715 .range_min = DA9062AA_VLDO1_A,
716 .range_max = DA9062AA_VLDO4_A,
717 }, {
718 .range_min = DA9062AA_VBUCK2_B,
719 .range_max = DA9062AA_VBUCK4_B,
720 }, {
721 .range_min = DA9062AA_VBUCK3_B,
722 .range_max = DA9062AA_VBUCK3_B,
723 }, {
724 .range_min = DA9062AA_VLDO1_B,
725 .range_max = DA9062AA_VLDO4_B,
726 }, {
727 .range_min = DA9062AA_BBAT_CONT,
728 .range_max = DA9062AA_BBAT_CONT,
729 }, {
730 .range_min = DA9062AA_GP_ID_0,
731 .range_max = DA9062AA_GP_ID_19,
732 },
733};
734
735static const struct regmap_range da9062_aa_volatile_ranges[] = {
736 {
737 .range_min = DA9062AA_PAGE_CON,
738 .range_max = DA9062AA_STATUS_B,
739 }, {
740 .range_min = DA9062AA_STATUS_D,
741 .range_max = DA9062AA_EVENT_C,
742 }, {
743 .range_min = DA9062AA_CONTROL_A,
744 .range_max = DA9062AA_CONTROL_B,
745 }, {
746 .range_min = DA9062AA_CONTROL_E,
747 .range_max = DA9062AA_CONTROL_F,
748 }, {
749 .range_min = DA9062AA_BUCK2_CONT,
750 .range_max = DA9062AA_BUCK4_CONT,
751 }, {
752 .range_min = DA9062AA_BUCK3_CONT,
753 .range_max = DA9062AA_BUCK3_CONT,
754 }, {
755 .range_min = DA9062AA_LDO1_CONT,
756 .range_max = DA9062AA_LDO4_CONT,
757 }, {
758 .range_min = DA9062AA_DVC_1,
759 .range_max = DA9062AA_DVC_1,
760 }, {
761 .range_min = DA9062AA_COUNT_S,
762 .range_max = DA9062AA_SECOND_D,
763 }, {
764 .range_min = DA9062AA_SEQ,
765 .range_max = DA9062AA_SEQ,
766 }, {
767 .range_min = DA9062AA_EN_32K,
768 .range_max = DA9062AA_EN_32K,
769 },
770};
771
772static const struct regmap_access_table da9062_aa_readable_table = {
773 .yes_ranges = da9062_aa_readable_ranges,
774 .n_yes_ranges = ARRAY_SIZE(da9062_aa_readable_ranges),
775};
776
777static const struct regmap_access_table da9062_aa_writeable_table = {
778 .yes_ranges = da9062_aa_writeable_ranges,
779 .n_yes_ranges = ARRAY_SIZE(da9062_aa_writeable_ranges),
780};
781
782static const struct regmap_access_table da9062_aa_volatile_table = {
783 .yes_ranges = da9062_aa_volatile_ranges,
784 .n_yes_ranges = ARRAY_SIZE(da9062_aa_volatile_ranges),
785};
786
787static const struct regmap_range_cfg da9062_range_cfg[] = {
788 {
789 .range_min = DA9062AA_PAGE_CON,
790 .range_max = DA9062AA_CONFIG_ID,
791 .selector_reg = DA9062AA_PAGE_CON,
792 .selector_mask = 1 << DA9062_I2C_PAGE_SEL_SHIFT,
793 .selector_shift = DA9062_I2C_PAGE_SEL_SHIFT,
794 .window_start = 0,
795 .window_len = 256,
796 }
797};
798
799static struct regmap_config da9062_regmap_config = {
800 .reg_bits = 8,
801 .val_bits = 8,
802 .ranges = da9062_range_cfg,
803 .num_ranges = ARRAY_SIZE(da9062_range_cfg),
804 .max_register = DA9062AA_CONFIG_ID,
805 .cache_type = REGCACHE_RBTREE,
806 .rd_table = &da9062_aa_readable_table,
807 .wr_table = &da9062_aa_writeable_table,
808 .volatile_table = &da9062_aa_volatile_table,
809};
810
811static const struct of_device_id da9062_dt_ids[] = {
812 { .compatible = "dlg,da9061", .data = (void *)COMPAT_TYPE_DA9061, },
813 { .compatible = "dlg,da9062", .data = (void *)COMPAT_TYPE_DA9062, },
814 { }
815};
816MODULE_DEVICE_TABLE(of, da9062_dt_ids);
817
818static int da9062_i2c_probe(struct i2c_client *i2c,
819 const struct i2c_device_id *id)
820{
821 struct da9062 *chip;
822 const struct of_device_id *match;
823 unsigned int irq_base;
824 const struct mfd_cell *cell;
825 const struct regmap_irq_chip *irq_chip;
826 const struct regmap_config *config;
827 int cell_num;
828 int ret;
829
830 chip = devm_kzalloc(&i2c->dev, sizeof(*chip), GFP_KERNEL);
831 if (!chip)
832 return -ENOMEM;
833
834 if (i2c->dev.of_node) {
835 match = of_match_node(da9062_dt_ids, i2c->dev.of_node);
836 if (!match)
837 return -EINVAL;
838
839 chip->chip_type = (uintptr_t)match->data;
840 } else {
841 chip->chip_type = id->driver_data;
842 }
843
844 i2c_set_clientdata(i2c, chip);
845 chip->dev = &i2c->dev;
846
847 if (!i2c->irq) {
848 dev_err(chip->dev, "No IRQ configured\n");
849 return -EINVAL;
850 }
851
852 switch (chip->chip_type) {
853 case COMPAT_TYPE_DA9061:
854 cell = da9061_devs;
855 cell_num = ARRAY_SIZE(da9061_devs);
856 irq_chip = &da9061_irq_chip;
857 config = &da9061_regmap_config;
858 break;
859 case COMPAT_TYPE_DA9062:
860 cell = da9062_devs;
861 cell_num = ARRAY_SIZE(da9062_devs);
862 irq_chip = &da9062_irq_chip;
863 config = &da9062_regmap_config;
864 break;
865 default:
866 dev_err(chip->dev, "Unrecognised chip type\n");
867 return -ENODEV;
868 }
869
870 chip->regmap = devm_regmap_init_i2c(i2c, config);
871 if (IS_ERR(chip->regmap)) {
872 ret = PTR_ERR(chip->regmap);
873 dev_err(chip->dev, "Failed to allocate register map: %d\n",
874 ret);
875 return ret;
876 }
877
878 ret = da9062_clear_fault_log(chip);
879 if (ret < 0)
880 dev_warn(chip->dev, "Cannot clear fault log\n");
881
882 ret = da9062_get_device_type(chip);
883 if (ret)
884 return ret;
885
886 ret = regmap_add_irq_chip(chip->regmap, i2c->irq,
887 IRQF_TRIGGER_LOW | IRQF_ONESHOT | IRQF_SHARED,
888 -1, irq_chip,
889 &chip->regmap_irq);
890 if (ret) {
891 dev_err(chip->dev, "Failed to request IRQ %d: %d\n",
892 i2c->irq, ret);
893 return ret;
894 }
895
896 irq_base = regmap_irq_chip_get_base(chip->regmap_irq);
897
898 ret = mfd_add_devices(chip->dev, PLATFORM_DEVID_NONE, cell,
899 cell_num, NULL, irq_base,
900 NULL);
901 if (ret) {
902 dev_err(chip->dev, "Cannot register child devices\n");
903 regmap_del_irq_chip(i2c->irq, chip->regmap_irq);
904 return ret;
905 }
906
907 return ret;
908}
909
910static int da9062_i2c_remove(struct i2c_client *i2c)
911{
912 struct da9062 *chip = i2c_get_clientdata(i2c);
913
914 mfd_remove_devices(chip->dev);
915 regmap_del_irq_chip(i2c->irq, chip->regmap_irq);
916
917 return 0;
918}
919
920static const struct i2c_device_id da9062_i2c_id[] = {
921 { "da9061", COMPAT_TYPE_DA9061 },
922 { "da9062", COMPAT_TYPE_DA9062 },
923 { },
924};
925MODULE_DEVICE_TABLE(i2c, da9062_i2c_id);
926
927static struct i2c_driver da9062_i2c_driver = {
928 .driver = {
929 .name = "da9062",
930 .of_match_table = of_match_ptr(da9062_dt_ids),
931 },
932 .probe = da9062_i2c_probe,
933 .remove = da9062_i2c_remove,
934 .id_table = da9062_i2c_id,
935};
936
937module_i2c_driver(da9062_i2c_driver);
938
939MODULE_DESCRIPTION("Core device driver for Dialog DA9061 and DA9062");
940MODULE_AUTHOR("Steve Twiss <stwiss.opensource@diasemi.com>");
941MODULE_LICENSE("GPL");