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