xref: /linux/drivers/mfd/adp5585.c (revision bd113a13e1fa51789f55987369b80e1d8bc19389)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Analog Devices ADP5585 I/O expander, PWM controller and keypad controller
4  *
5  * Copyright 2022 NXP
6  * Copyright 2024 Ideas on Board Oy
7  * Copyright 2025 Analog Devices Inc.
8  */
9 
10 #include <linux/array_size.h>
11 #include <linux/bitfield.h>
12 #include <linux/device.h>
13 #include <linux/err.h>
14 #include <linux/i2c.h>
15 #include <linux/mfd/adp5585.h>
16 #include <linux/mfd/core.h>
17 #include <linux/mod_devicetable.h>
18 #include <linux/module.h>
19 #include <linux/regmap.h>
20 #include <linux/types.h>
21 
22 enum {
23 	ADP5585_DEV_GPIO,
24 	ADP5585_DEV_PWM,
25 	ADP5585_DEV_INPUT,
26 	ADP5585_DEV_MAX
27 };
28 
29 static const struct mfd_cell adp5585_devs[ADP5585_DEV_MAX] = {
30 	MFD_CELL_NAME("adp5585-gpio"),
31 	MFD_CELL_NAME("adp5585-pwm"),
32 	MFD_CELL_NAME("adp5585-keys"),
33 };
34 
35 static const struct mfd_cell adp5589_devs[] = {
36 	MFD_CELL_NAME("adp5589-gpio"),
37 	MFD_CELL_NAME("adp5589-pwm"),
38 	MFD_CELL_NAME("adp5589-keys"),
39 };
40 
41 static const struct regmap_range adp5585_volatile_ranges[] = {
42 	regmap_reg_range(ADP5585_ID, ADP5585_GPI_STATUS_B),
43 };
44 
45 static const struct regmap_access_table adp5585_volatile_regs = {
46 	.yes_ranges = adp5585_volatile_ranges,
47 	.n_yes_ranges = ARRAY_SIZE(adp5585_volatile_ranges),
48 };
49 
50 static const struct regmap_range adp5589_volatile_ranges[] = {
51 	regmap_reg_range(ADP5585_ID, ADP5589_GPI_STATUS_C),
52 };
53 
54 static const struct regmap_access_table adp5589_volatile_regs = {
55 	.yes_ranges = adp5589_volatile_ranges,
56 	.n_yes_ranges = ARRAY_SIZE(adp5589_volatile_ranges),
57 };
58 
59 /*
60  * Chip variants differ in the default configuration of pull-up and pull-down
61  * resistors, and therefore have different default register values:
62  *
63  * - The -00, -01 and -03 variants (collectively referred to as
64  *   ADP5585_REGMAP_00) have pull-up on all GPIO pins by default.
65  * - The -02 variant has no default pull-up or pull-down resistors.
66  * - The -04 variant has default pull-down resistors on all GPIO pins.
67  */
68 
69 static const u8 adp5585_regmap_defaults_00[ADP5585_MAX_REG + 1] = {
70 	/* 0x00 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
71 	/* 0x08 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
72 	/* 0x10 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
73 	/* 0x18 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
74 	/* 0x20 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
75 	/* 0x28 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
76 	/* 0x30 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
77 	/* 0x38 */ 0x00, 0x00, 0x00, 0x00, 0x00,
78 };
79 
80 static const u8 adp5585_regmap_defaults_02[ADP5585_MAX_REG + 1] = {
81 	/* 0x00 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
82 	/* 0x08 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
83 	/* 0x10 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc3,
84 	/* 0x18 */ 0x03, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00,
85 	/* 0x20 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
86 	/* 0x28 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
87 	/* 0x30 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
88 	/* 0x38 */ 0x00, 0x00, 0x00, 0x00, 0x00,
89 };
90 
91 static const u8 adp5585_regmap_defaults_04[ADP5585_MAX_REG + 1] = {
92 	/* 0x00 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
93 	/* 0x08 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
94 	/* 0x10 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55,
95 	/* 0x18 */ 0x05, 0x55, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
96 	/* 0x20 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
97 	/* 0x28 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
98 	/* 0x30 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
99 	/* 0x38 */ 0x00, 0x00, 0x00, 0x00, 0x00,
100 };
101 
102 static const u8 adp5589_regmap_defaults_00[ADP5589_MAX_REG + 1] = {
103 	/* 0x00 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
104 	/* 0x08 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
105 	/* 0x10 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
106 	/* 0x18 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
107 	/* 0x20 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
108 	/* 0x28 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
109 	/* 0x30 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
110 	/* 0x38 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
111 	/* 0x40 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
112 	/* 0x48 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
113 };
114 
115 static const u8 adp5589_regmap_defaults_01[ADP5589_MAX_REG + 1] = {
116 	/* 0x00 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
117 	/* 0x08 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
118 	/* 0x10 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
119 	/* 0x18 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
120 	/* 0x20 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
121 	/* 0x28 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
122 	/* 0x30 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
123 	/* 0x38 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00,
124 	/* 0x40 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
125 	/* 0x48 */ 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00,
126 };
127 
128 static const u8 adp5589_regmap_defaults_02[ADP5589_MAX_REG + 1] = {
129 	/* 0x00 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
130 	/* 0x08 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
131 	/* 0x10 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
132 	/* 0x18 */ 0x00, 0x41, 0x01, 0x00, 0x11, 0x04, 0x00, 0x00,
133 	/* 0x20 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
134 	/* 0x28 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
135 	/* 0x30 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
136 	/* 0x38 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
137 	/* 0x40 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
138 	/* 0x48 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
139 };
140 
141 static const u8 *adp5585_regmap_defaults[ADP5585_MAX] = {
142 	[ADP5585_00] = adp5585_regmap_defaults_00,
143 	[ADP5585_01] = adp5585_regmap_defaults_00,
144 	[ADP5585_02] = adp5585_regmap_defaults_02,
145 	[ADP5585_03] = adp5585_regmap_defaults_00,
146 	[ADP5585_04] = adp5585_regmap_defaults_04,
147 	[ADP5589_00] = adp5589_regmap_defaults_00,
148 	[ADP5589_01] = adp5589_regmap_defaults_01,
149 	[ADP5589_02] = adp5589_regmap_defaults_02,
150 };
151 
152 static const struct regmap_config adp5585_regmap_config_template = {
153 	.reg_bits = 8,
154 	.val_bits = 8,
155 	.max_register = ADP5585_MAX_REG,
156 	.volatile_table = &adp5585_volatile_regs,
157 	.cache_type = REGCACHE_MAPLE,
158 	.num_reg_defaults_raw = ADP5585_MAX_REG + 1,
159 };
160 
161 static const struct regmap_config adp5589_regmap_config_template = {
162 	.reg_bits = 8,
163 	.val_bits = 8,
164 	.max_register = ADP5589_MAX_REG,
165 	.volatile_table = &adp5589_volatile_regs,
166 	.cache_type = REGCACHE_MAPLE,
167 	.num_reg_defaults_raw = ADP5589_MAX_REG + 1,
168 };
169 
170 static const struct adp5585_regs adp5585_regs = {
171 	.ext_cfg = ADP5585_PIN_CONFIG_C,
172 	.int_en = ADP5585_INT_EN,
173 	.gen_cfg = ADP5585_GENERAL_CFG,
174 	.poll_ptime_cfg = ADP5585_POLL_PTIME_CFG,
175 	.reset_cfg = ADP5585_RESET_CFG,
176 	.reset1_event_a = ADP5585_RESET1_EVENT_A,
177 	.reset2_event_a = ADP5585_RESET2_EVENT_A,
178 	.pin_cfg_a = ADP5585_PIN_CONFIG_A,
179 };
180 
181 static const struct adp5585_regs adp5589_regs = {
182 	.ext_cfg = ADP5589_PIN_CONFIG_D,
183 	.int_en = ADP5589_INT_EN,
184 	.gen_cfg = ADP5589_GENERAL_CFG,
185 	.poll_ptime_cfg = ADP5589_POLL_PTIME_CFG,
186 	.reset_cfg = ADP5589_RESET_CFG,
187 	.reset1_event_a = ADP5589_RESET1_EVENT_A,
188 	.reset2_event_a = ADP5589_RESET2_EVENT_A,
189 	.pin_cfg_a = ADP5589_PIN_CONFIG_A,
190 };
191 
192 static int adp5585_validate_event(const struct adp5585_dev *adp5585, unsigned int ev)
193 {
194 	if (adp5585->has_pin6) {
195 		if (ev >= ADP5585_ROW5_KEY_EVENT_START && ev <= ADP5585_ROW5_KEY_EVENT_END)
196 			return 0;
197 		if (ev >= ADP5585_GPI_EVENT_START && ev <= ADP5585_GPI_EVENT_END)
198 			return 0;
199 
200 		return dev_err_probe(adp5585->dev, -EINVAL,
201 				     "Invalid unlock/reset event(%u) for this device\n", ev);
202 	}
203 
204 	if (ev >= ADP5585_KEY_EVENT_START && ev <= ADP5585_KEY_EVENT_END)
205 		return 0;
206 	if (ev >= ADP5585_GPI_EVENT_START && ev <= ADP5585_GPI_EVENT_END) {
207 		/*
208 		 * Some variants of the adp5585 do not have the Row 5
209 		 * (meaning pin 6 or GPIO 6) available. Instead that pin serves
210 		 * as a reset pin. So, we need to make sure no event is
211 		 * configured for it.
212 		 */
213 		if (ev == (ADP5585_GPI_EVENT_START + 5))
214 			return dev_err_probe(adp5585->dev, -EINVAL,
215 					     "Invalid unlock/reset event(%u). R5 not available\n",
216 					     ev);
217 		return 0;
218 	}
219 
220 	return dev_err_probe(adp5585->dev, -EINVAL,
221 			     "Invalid unlock/reset event(%u) for this device\n", ev);
222 }
223 
224 static int adp5589_validate_event(const struct adp5585_dev *adp5585, unsigned int ev)
225 {
226 	if (ev >= ADP5589_KEY_EVENT_START && ev <= ADP5589_KEY_EVENT_END)
227 		return 0;
228 	if (ev >= ADP5589_GPI_EVENT_START && ev <= ADP5589_GPI_EVENT_END)
229 		return 0;
230 
231 	return dev_err_probe(adp5585->dev, -EINVAL,
232 			     "Invalid unlock/reset event(%u) for this device\n", ev);
233 }
234 
235 static struct regmap_config *adp5585_fill_variant_config(struct adp5585_dev *adp5585)
236 {
237 	struct regmap_config *regmap_config;
238 
239 	switch (adp5585->variant) {
240 	case ADP5585_00:
241 	case ADP5585_01:
242 	case ADP5585_02:
243 	case ADP5585_03:
244 	case ADP5585_04:
245 		adp5585->id = ADP5585_MAN_ID_VALUE;
246 		adp5585->regs = &adp5585_regs;
247 		adp5585->n_pins = ADP5585_PIN_MAX;
248 		adp5585->reset2_out = ADP5585_RESET2_OUT;
249 		if (adp5585->variant == ADP5585_01)
250 			adp5585->has_pin6 = true;
251 		regmap_config = devm_kmemdup(adp5585->dev, &adp5585_regmap_config_template,
252 					     sizeof(*regmap_config), GFP_KERNEL);
253 		break;
254 	case ADP5589_00:
255 	case ADP5589_01:
256 	case ADP5589_02:
257 		adp5585->id = ADP5589_MAN_ID_VALUE;
258 		adp5585->regs = &adp5589_regs;
259 		adp5585->has_unlock = true;
260 		adp5585->has_pin6 = true;
261 		adp5585->n_pins = ADP5589_PIN_MAX;
262 		adp5585->reset2_out = ADP5589_RESET2_OUT;
263 		regmap_config = devm_kmemdup(adp5585->dev, &adp5589_regmap_config_template,
264 					     sizeof(*regmap_config), GFP_KERNEL);
265 		break;
266 	default:
267 		return ERR_PTR(-ENODEV);
268 	}
269 
270 	if (!regmap_config)
271 		return ERR_PTR(-ENOMEM);
272 
273 	regmap_config->reg_defaults_raw = adp5585_regmap_defaults[adp5585->variant];
274 
275 	return regmap_config;
276 }
277 
278 static int adp5585_parse_ev_array(const struct adp5585_dev *adp5585, const char *prop, u32 *events,
279 				  u32 *n_events, u32 max_evs, bool reset_ev)
280 {
281 	struct device *dev = adp5585->dev;
282 	unsigned int ev;
283 	int ret;
284 
285 	/*
286 	 * The device has the capability of handling special events through GPIs or a Keypad:
287 	 *  unlock events: Unlock the keymap until one of the configured events is detected.
288 	 *  reset events: Generate a reset pulse when one of the configured events is detected.
289 	 */
290 	ret = device_property_count_u32(dev, prop);
291 	if (ret < 0)
292 		return 0;
293 
294 	*n_events = ret;
295 
296 	if (!adp5585->has_unlock && !reset_ev)
297 		return dev_err_probe(dev, -EOPNOTSUPP, "Unlock keys not supported\n");
298 
299 	if (*n_events > max_evs)
300 		return dev_err_probe(dev, -EINVAL,
301 				     "Invalid number of keys(%u > %u) for %s\n",
302 				     *n_events, max_evs, prop);
303 
304 	ret = device_property_read_u32_array(dev, prop, events, *n_events);
305 	if (ret)
306 		return ret;
307 
308 	for (ev = 0; ev < *n_events; ev++) {
309 		if (!reset_ev && events[ev] == ADP5589_UNLOCK_WILDCARD)
310 			continue;
311 
312 		if (adp5585->id == ADP5585_MAN_ID_VALUE)
313 			ret = adp5585_validate_event(adp5585, events[ev]);
314 		else
315 			ret = adp5589_validate_event(adp5585, events[ev]);
316 		if (ret)
317 			return ret;
318 	}
319 
320 	return 0;
321 }
322 
323 static int adp5585_unlock_ev_parse(struct adp5585_dev *adp5585)
324 {
325 	struct device *dev = adp5585->dev;
326 	int ret;
327 
328 	ret = adp5585_parse_ev_array(adp5585, "adi,unlock-events", adp5585->unlock_keys,
329 				     &adp5585->nkeys_unlock, ARRAY_SIZE(adp5585->unlock_keys),
330 				     false);
331 	if (ret)
332 		return ret;
333 	if (!adp5585->nkeys_unlock)
334 		return 0;
335 
336 	ret = device_property_read_u32(dev, "adi,unlock-trigger-sec", &adp5585->unlock_time);
337 	if (!ret) {
338 		if (adp5585->unlock_time > ADP5585_MAX_UNLOCK_TIME_SEC)
339 			return dev_err_probe(dev, -EINVAL,
340 					     "Invalid unlock time(%u > %d)\n",
341 					     adp5585->unlock_time,
342 					     ADP5585_MAX_UNLOCK_TIME_SEC);
343 	}
344 
345 	return 0;
346 }
347 
348 static int adp5585_reset_ev_parse(struct adp5585_dev *adp5585)
349 {
350 	struct device *dev = adp5585->dev;
351 	u32 prop_val;
352 	int ret;
353 
354 	ret = adp5585_parse_ev_array(adp5585, "adi,reset1-events", adp5585->reset1_keys,
355 				     &adp5585->nkeys_reset1,
356 				     ARRAY_SIZE(adp5585->reset1_keys), true);
357 	if (ret)
358 		return ret;
359 
360 	ret = adp5585_parse_ev_array(adp5585, "adi,reset2-events",
361 				     adp5585->reset2_keys,
362 				     &adp5585->nkeys_reset2,
363 				     ARRAY_SIZE(adp5585->reset2_keys), true);
364 	if (ret)
365 		return ret;
366 
367 	if (!adp5585->nkeys_reset1 && !adp5585->nkeys_reset2)
368 		return 0;
369 
370 	if (adp5585->nkeys_reset1 && device_property_read_bool(dev, "adi,reset1-active-high"))
371 		adp5585->reset_cfg |= FIELD_PREP(ADP5585_RESET1_POL, 1);
372 
373 	if (adp5585->nkeys_reset2 && device_property_read_bool(dev, "adi,reset2-active-high"))
374 		adp5585->reset_cfg |= FIELD_PREP(ADP5585_RESET2_POL, 1);
375 
376 	if (device_property_read_bool(dev, "adi,rst-passthrough-enable"))
377 		adp5585->reset_cfg |= FIELD_PREP(ADP5585_RST_PASSTHRU_EN, 1);
378 
379 	ret = device_property_read_u32(dev, "adi,reset-trigger-ms", &prop_val);
380 	if (!ret) {
381 		switch (prop_val) {
382 		case 0:
383 			adp5585->reset_cfg |= FIELD_PREP(ADP5585_RESET_TRIG_TIME, 0);
384 			break;
385 		case 1000:
386 			adp5585->reset_cfg |= FIELD_PREP(ADP5585_RESET_TRIG_TIME, 1);
387 			break;
388 		case 1500:
389 			adp5585->reset_cfg |= FIELD_PREP(ADP5585_RESET_TRIG_TIME, 2);
390 			break;
391 		case 2000:
392 			adp5585->reset_cfg |= FIELD_PREP(ADP5585_RESET_TRIG_TIME, 3);
393 			break;
394 		case 2500:
395 			adp5585->reset_cfg |= FIELD_PREP(ADP5585_RESET_TRIG_TIME, 4);
396 			break;
397 		case 3000:
398 			adp5585->reset_cfg |= FIELD_PREP(ADP5585_RESET_TRIG_TIME, 5);
399 			break;
400 		case 3500:
401 			adp5585->reset_cfg |= FIELD_PREP(ADP5585_RESET_TRIG_TIME, 6);
402 			break;
403 		case 4000:
404 			adp5585->reset_cfg |= FIELD_PREP(ADP5585_RESET_TRIG_TIME, 7);
405 			break;
406 		default:
407 			return dev_err_probe(dev, -EINVAL,
408 					     "Invalid value(%u) for adi,reset-trigger-ms\n",
409 					     prop_val);
410 		}
411 	}
412 
413 	ret = device_property_read_u32(dev, "adi,reset-pulse-width-us", &prop_val);
414 	if (!ret) {
415 		switch (prop_val) {
416 		case 500:
417 			adp5585->reset_cfg |= FIELD_PREP(ADP5585_PULSE_WIDTH, 0);
418 			break;
419 		case 1000:
420 			adp5585->reset_cfg |= FIELD_PREP(ADP5585_PULSE_WIDTH, 1);
421 			break;
422 		case 2000:
423 			adp5585->reset_cfg |= FIELD_PREP(ADP5585_PULSE_WIDTH, 2);
424 			break;
425 		case 10000:
426 			adp5585->reset_cfg |= FIELD_PREP(ADP5585_PULSE_WIDTH, 3);
427 			break;
428 		default:
429 			return dev_err_probe(dev, -EINVAL,
430 					     "Invalid value(%u) for adi,reset-pulse-width-us\n",
431 					     prop_val);
432 		}
433 		return ret;
434 	}
435 
436 	return 0;
437 }
438 
439 static int adp5585_add_devices(const struct adp5585_dev *adp5585)
440 {
441 	struct device *dev = adp5585->dev;
442 	const struct mfd_cell *cells;
443 	int ret;
444 
445 	if (adp5585->id == ADP5585_MAN_ID_VALUE)
446 		cells = adp5585_devs;
447 	else
448 		cells = adp5589_devs;
449 
450 	if (device_property_present(dev, "#pwm-cells")) {
451 		/* Make sure the PWM output pin is not used by the GPIO or INPUT devices */
452 		__set_bit(ADP5585_PWM_OUT, adp5585->pin_usage);
453 		ret = devm_mfd_add_devices(dev, PLATFORM_DEVID_AUTO,
454 					   &cells[ADP5585_DEV_PWM], 1, NULL, 0, NULL);
455 		if (ret)
456 			return dev_err_probe(dev, ret, "Failed to add PWM device\n");
457 	}
458 
459 	if (device_property_present(dev, "#gpio-cells")) {
460 		ret = devm_mfd_add_devices(dev, PLATFORM_DEVID_AUTO,
461 					   &cells[ADP5585_DEV_GPIO], 1, NULL, 0, NULL);
462 		if (ret)
463 			return dev_err_probe(dev, ret, "Failed to add GPIO device\n");
464 	}
465 
466 	if (device_property_present(adp5585->dev, "adi,keypad-pins")) {
467 		ret = devm_mfd_add_devices(adp5585->dev, PLATFORM_DEVID_AUTO,
468 					   &cells[ADP5585_DEV_INPUT], 1, NULL, 0, NULL);
469 		if (ret)
470 			return dev_err_probe(dev, ret, "Failed to add input device\n");
471 	}
472 
473 	return 0;
474 }
475 
476 static void adp5585_osc_disable(void *data)
477 {
478 	const struct adp5585_dev *adp5585 = data;
479 
480 	regmap_write(adp5585->regmap, ADP5585_GENERAL_CFG, 0);
481 }
482 
483 static void adp5585_report_events(struct adp5585_dev *adp5585, int ev_cnt)
484 {
485 	unsigned int i;
486 
487 	for (i = 0; i < ev_cnt; i++) {
488 		unsigned long key_val, key_press;
489 		unsigned int key;
490 		int ret;
491 
492 		ret = regmap_read(adp5585->regmap, ADP5585_FIFO_1 + i, &key);
493 		if (ret)
494 			return;
495 
496 		key_val = FIELD_GET(ADP5585_KEY_EVENT_MASK, key);
497 		key_press = FIELD_GET(ADP5585_KEV_EV_PRESS_MASK, key);
498 
499 		blocking_notifier_call_chain(&adp5585->event_notifier, key_val, (void *)key_press);
500 	}
501 }
502 
503 static irqreturn_t adp5585_irq(int irq, void *data)
504 {
505 	struct adp5585_dev *adp5585 = data;
506 	unsigned int status, ev_cnt;
507 	int ret;
508 
509 	ret = regmap_read(adp5585->regmap, ADP5585_INT_STATUS, &status);
510 	if (ret)
511 		return IRQ_HANDLED;
512 
513 	if (status & ADP5585_OVRFLOW_INT)
514 		dev_err_ratelimited(adp5585->dev, "Event overflow error\n");
515 
516 	if (!(status & ADP5585_EVENT_INT))
517 		goto out_irq;
518 
519 	ret = regmap_read(adp5585->regmap, ADP5585_STATUS, &ev_cnt);
520 	if (ret)
521 		goto out_irq;
522 
523 	ev_cnt = FIELD_GET(ADP5585_EC_MASK, ev_cnt);
524 	if (!ev_cnt)
525 		goto out_irq;
526 
527 	adp5585_report_events(adp5585, ev_cnt);
528 out_irq:
529 	regmap_write(adp5585->regmap, ADP5585_INT_STATUS, status);
530 	return IRQ_HANDLED;
531 }
532 
533 static int adp5585_setup(struct adp5585_dev *adp5585)
534 {
535 	const struct adp5585_regs *regs = adp5585->regs;
536 	unsigned int reg_val = 0, i;
537 	int ret;
538 
539 	/* If pin_6 (ROW5/GPI6) is not available, make sure to mark it as "busy" */
540 	if (!adp5585->has_pin6)
541 		__set_bit(ADP5585_ROW5, adp5585->pin_usage);
542 
543 	/* Configure the device with reset and unlock events */
544 	for (i = 0; i < adp5585->nkeys_unlock; i++) {
545 		ret = regmap_write(adp5585->regmap, ADP5589_UNLOCK1 + i,
546 				   adp5585->unlock_keys[i] | ADP5589_UNLOCK_EV_PRESS);
547 		if (ret)
548 			return ret;
549 	}
550 
551 	if (adp5585->nkeys_unlock) {
552 		ret = regmap_update_bits(adp5585->regmap, ADP5589_UNLOCK_TIMERS,
553 					 ADP5589_UNLOCK_TIMER, adp5585->unlock_time);
554 		if (ret)
555 			return ret;
556 
557 		ret = regmap_set_bits(adp5585->regmap, ADP5589_LOCK_CFG, ADP5589_LOCK_EN);
558 		if (ret)
559 			return ret;
560 	}
561 
562 	for (i = 0; i < adp5585->nkeys_reset1; i++) {
563 		ret = regmap_write(adp5585->regmap, regs->reset1_event_a + i,
564 				   adp5585->reset1_keys[i] | ADP5585_RESET_EV_PRESS);
565 		if (ret)
566 			return ret;
567 
568 		/* Mark that pin as not usable for the INPUT and GPIO devices. */
569 		__set_bit(ADP5585_RESET1_OUT, adp5585->pin_usage);
570 	}
571 
572 	for (i = 0; i < adp5585->nkeys_reset2; i++) {
573 		ret = regmap_write(adp5585->regmap, regs->reset2_event_a + i,
574 				   adp5585->reset2_keys[i] | ADP5585_RESET_EV_PRESS);
575 		if (ret)
576 			return ret;
577 
578 		__set_bit(adp5585->reset2_out, adp5585->pin_usage);
579 	}
580 
581 	if (adp5585->nkeys_reset1 || adp5585->nkeys_reset2) {
582 		ret = regmap_write(adp5585->regmap, regs->reset_cfg, adp5585->reset_cfg);
583 		if (ret)
584 			return ret;
585 
586 		/* If there's a reset1 event, then R4 is used as an output for the reset signal */
587 		if (adp5585->nkeys_reset1)
588 			reg_val = ADP5585_R4_EXTEND_CFG_RESET1;
589 		/* If there's a reset2 event, then C4 is used as an output for the reset signal */
590 		if (adp5585->nkeys_reset2)
591 			reg_val |= ADP5585_C4_EXTEND_CFG_RESET2;
592 
593 		ret = regmap_update_bits(adp5585->regmap, regs->ext_cfg,
594 					 ADP5585_C4_EXTEND_CFG_MASK | ADP5585_R4_EXTEND_CFG_MASK,
595 					 reg_val);
596 		if (ret)
597 			return ret;
598 	}
599 
600 	/* Clear any possible event by reading all the FIFO entries */
601 	for (i = 0; i < ADP5585_EV_MAX; i++) {
602 		ret = regmap_read(adp5585->regmap, ADP5585_FIFO_1 + i, &reg_val);
603 		if (ret)
604 			return ret;
605 	}
606 
607 	ret = regmap_write(adp5585->regmap, regs->poll_ptime_cfg, adp5585->ev_poll_time);
608 	if (ret)
609 		return ret;
610 
611 	/*
612 	 * Enable the internal oscillator, as it's shared between multiple
613 	 * functions.
614 	 */
615 	ret = regmap_write(adp5585->regmap, regs->gen_cfg,
616 			   ADP5585_OSC_FREQ_500KHZ | ADP5585_INT_CFG | ADP5585_OSC_EN);
617 	if (ret)
618 		return ret;
619 
620 	return devm_add_action_or_reset(adp5585->dev, adp5585_osc_disable, adp5585);
621 }
622 
623 static int adp5585_parse_fw(struct adp5585_dev *adp5585)
624 {
625 	unsigned int prop_val;
626 	int ret;
627 
628 	ret = device_property_read_u32(adp5585->dev, "poll-interval", &prop_val);
629 	if (!ret) {
630 		adp5585->ev_poll_time = prop_val / 10 - 1;
631 		/*
632 		 * ev_poll_time is the raw value to be written on the register and 0 to 3 are the
633 		 * valid values.
634 		 */
635 		if (adp5585->ev_poll_time > 3)
636 			return dev_err_probe(adp5585->dev, -EINVAL,
637 					     "Invalid value(%u) for poll-interval\n", prop_val);
638 	}
639 
640 	ret = adp5585_unlock_ev_parse(adp5585);
641 	if (ret)
642 		return ret;
643 
644 	return adp5585_reset_ev_parse(adp5585);
645 }
646 
647 static void adp5585_irq_disable(void *data)
648 {
649 	struct adp5585_dev *adp5585 = data;
650 
651 	regmap_write(adp5585->regmap, adp5585->regs->int_en, 0);
652 }
653 
654 static int adp5585_irq_enable(struct i2c_client *i2c,
655 			      struct adp5585_dev *adp5585)
656 {
657 	const struct adp5585_regs *regs = adp5585->regs;
658 	unsigned int stat;
659 	int ret;
660 
661 	if (i2c->irq <= 0)
662 		return 0;
663 
664 	ret = devm_request_threaded_irq(&i2c->dev, i2c->irq, NULL, adp5585_irq,
665 					IRQF_ONESHOT, i2c->name, adp5585);
666 	if (ret)
667 		return ret;
668 
669 	/*
670 	 * Clear any possible outstanding interrupt before enabling them. We do that by reading
671 	 * the status register and writing back the same value.
672 	 */
673 	ret = regmap_read(adp5585->regmap, ADP5585_INT_STATUS, &stat);
674 	if (ret)
675 		return ret;
676 
677 	ret = regmap_write(adp5585->regmap, ADP5585_INT_STATUS, stat);
678 	if (ret)
679 		return ret;
680 
681 	ret = regmap_write(adp5585->regmap, regs->int_en, ADP5585_OVRFLOW_IEN | ADP5585_EVENT_IEN);
682 	if (ret)
683 		return ret;
684 
685 	return devm_add_action_or_reset(&i2c->dev, adp5585_irq_disable, adp5585);
686 }
687 
688 static int adp5585_i2c_probe(struct i2c_client *i2c)
689 {
690 	struct regmap_config *regmap_config;
691 	struct adp5585_dev *adp5585;
692 	unsigned int id;
693 	int ret;
694 
695 	adp5585 = devm_kzalloc(&i2c->dev, sizeof(*adp5585), GFP_KERNEL);
696 	if (!adp5585)
697 		return -ENOMEM;
698 
699 	i2c_set_clientdata(i2c, adp5585);
700 	adp5585->dev = &i2c->dev;
701 	adp5585->irq = i2c->irq;
702 	BLOCKING_INIT_NOTIFIER_HEAD(&adp5585->event_notifier);
703 
704 	adp5585->variant = (enum adp5585_variant)(uintptr_t)i2c_get_match_data(i2c);
705 	if (!adp5585->variant)
706 		return -ENODEV;
707 
708 	regmap_config = adp5585_fill_variant_config(adp5585);
709 	if (IS_ERR(regmap_config))
710 		return PTR_ERR(regmap_config);
711 
712 	adp5585->regmap = devm_regmap_init_i2c(i2c, regmap_config);
713 	if (IS_ERR(adp5585->regmap))
714 		return dev_err_probe(&i2c->dev, PTR_ERR(adp5585->regmap),
715 				     "Failed to initialize register map\n");
716 
717 	ret = regmap_read(adp5585->regmap, ADP5585_ID, &id);
718 	if (ret)
719 		return dev_err_probe(&i2c->dev, ret,
720 				     "Failed to read device ID\n");
721 
722 	id &= ADP5585_MAN_ID_MASK;
723 	if (id != adp5585->id)
724 		return dev_err_probe(&i2c->dev, -ENODEV,
725 				     "Invalid device ID 0x%02x\n", id);
726 
727 	adp5585->pin_usage = devm_bitmap_zalloc(&i2c->dev, adp5585->n_pins, GFP_KERNEL);
728 	if (!adp5585->pin_usage)
729 		return -ENOMEM;
730 
731 	ret = adp5585_parse_fw(adp5585);
732 	if (ret)
733 		return ret;
734 
735 	ret = adp5585_setup(adp5585);
736 	if (ret)
737 		return ret;
738 
739 	ret = adp5585_add_devices(adp5585);
740 	if (ret)
741 		return ret;
742 
743 	return adp5585_irq_enable(i2c, adp5585);
744 }
745 
746 static int adp5585_suspend(struct device *dev)
747 {
748 	struct adp5585_dev *adp5585 = dev_get_drvdata(dev);
749 
750 	if (adp5585->irq)
751 		disable_irq(adp5585->irq);
752 
753 	regcache_cache_only(adp5585->regmap, true);
754 
755 	return 0;
756 }
757 
758 static int adp5585_resume(struct device *dev)
759 {
760 	struct adp5585_dev *adp5585 = dev_get_drvdata(dev);
761 	int ret;
762 
763 	regcache_cache_only(adp5585->regmap, false);
764 	regcache_mark_dirty(adp5585->regmap);
765 
766 	ret = regcache_sync(adp5585->regmap);
767 	if (ret)
768 		return ret;
769 
770 	if (adp5585->irq)
771 		enable_irq(adp5585->irq);
772 
773 	return 0;
774 }
775 
776 static DEFINE_SIMPLE_DEV_PM_OPS(adp5585_pm, adp5585_suspend, adp5585_resume);
777 
778 static const struct of_device_id adp5585_of_match[] = {
779 	{
780 		.compatible = "adi,adp5585-00",
781 		.data = (void *)ADP5585_00,
782 	}, {
783 		.compatible = "adi,adp5585-01",
784 		.data = (void *)ADP5585_01,
785 	}, {
786 		.compatible = "adi,adp5585-02",
787 		.data = (void *)ADP5585_02,
788 	}, {
789 		.compatible = "adi,adp5585-03",
790 		.data = (void *)ADP5585_03,
791 	}, {
792 		.compatible = "adi,adp5585-04",
793 		.data = (void *)ADP5585_04,
794 	}, {
795 		.compatible = "adi,adp5589-00",
796 		.data = (void *)ADP5589_00,
797 	}, {
798 		.compatible = "adi,adp5589-01",
799 		.data = (void *)ADP5589_01,
800 	}, {
801 		.compatible = "adi,adp5589-02",
802 		.data = (void *)ADP5589_02,
803 	}, {
804 		.compatible = "adi,adp5589",
805 		.data = (void *)ADP5589_00,
806 	},
807 	{ /* sentinel */ }
808 };
809 MODULE_DEVICE_TABLE(of, adp5585_of_match);
810 
811 static struct i2c_driver adp5585_i2c_driver = {
812 	.driver = {
813 		.name = "adp5585",
814 		.of_match_table = adp5585_of_match,
815 		.pm = pm_sleep_ptr(&adp5585_pm),
816 	},
817 	.probe = adp5585_i2c_probe,
818 };
819 module_i2c_driver(adp5585_i2c_driver);
820 
821 MODULE_DESCRIPTION("ADP5585 core driver");
822 MODULE_AUTHOR("Haibo Chen <haibo.chen@nxp.com>");
823 MODULE_LICENSE("GPL");
824