File Index
Files and directories
Sign in
FILE

adp5585.c

File preview with syntax highlighting.

linux-6.19.14/drivers/mfd/adp5585.c C
  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
 24enum {
 25	ADP5585_DEV_GPIO,
 26	ADP5585_DEV_PWM,
 27	ADP5585_DEV_INPUT,
 28	ADP5585_DEV_MAX
 29};
 30
 31static 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
 37static 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
 43static const struct regmap_range adp5585_volatile_ranges[] = {
 44	regmap_reg_range(ADP5585_ID, ADP5585_GPI_STATUS_B),
 45};
 46
 47static 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
 52static const struct regmap_range adp5589_volatile_ranges[] = {
 53	regmap_reg_range(ADP5585_ID, ADP5589_GPI_STATUS_C),
 54};
 55
 56static 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
 71static 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
 82static 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
 93static 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
104static 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
117static 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
130static 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
143static 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
154static 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
163static 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
172static 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
183static 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
194static 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
226static 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
237static 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
280static 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
325static 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
350static 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	}
436
437	return 0;
438}
439
440static int adp5585_add_devices(const struct adp5585_dev *adp5585)
441{
442	struct device *dev = adp5585->dev;
443	const struct mfd_cell *cells;
444	int ret;
445
446	if (adp5585->id == ADP5585_MAN_ID_VALUE)
447		cells = adp5585_devs;
448	else
449		cells = adp5589_devs;
450
451	if (device_property_present(dev, "#pwm-cells")) {
452		/* Make sure the PWM output pin is not used by the GPIO or INPUT devices */
453		__set_bit(ADP5585_PWM_OUT, adp5585->pin_usage);
454		ret = devm_mfd_add_devices(dev, PLATFORM_DEVID_AUTO,
455					   &cells[ADP5585_DEV_PWM], 1, NULL, 0, NULL);
456		if (ret)
457			return dev_err_probe(dev, ret, "Failed to add PWM device\n");
458	}
459
460	if (device_property_present(dev, "#gpio-cells")) {
461		ret = devm_mfd_add_devices(dev, PLATFORM_DEVID_AUTO,
462					   &cells[ADP5585_DEV_GPIO], 1, NULL, 0, NULL);
463		if (ret)
464			return dev_err_probe(dev, ret, "Failed to add GPIO device\n");
465	}
466
467	if (device_property_present(adp5585->dev, "adi,keypad-pins")) {
468		ret = devm_mfd_add_devices(adp5585->dev, PLATFORM_DEVID_AUTO,
469					   &cells[ADP5585_DEV_INPUT], 1, NULL, 0, NULL);
470		if (ret)
471			return dev_err_probe(dev, ret, "Failed to add input device\n");
472	}
473
474	return 0;
475}
476
477static void adp5585_osc_disable(void *data)
478{
479	const struct adp5585_dev *adp5585 = data;
480
481	regmap_write(adp5585->regmap, ADP5585_GENERAL_CFG, 0);
482}
483
484static void adp5585_report_events(struct adp5585_dev *adp5585, int ev_cnt)
485{
486	unsigned int i;
487
488	for (i = 0; i < ev_cnt; i++) {
489		unsigned long key_val, key_press;
490		unsigned int key;
491		int ret;
492
493		ret = regmap_read(adp5585->regmap, ADP5585_FIFO_1 + i, &key);
494		if (ret)
495			return;
496
497		key_val = FIELD_GET(ADP5585_KEY_EVENT_MASK, key);
498		key_press = FIELD_GET(ADP5585_KEV_EV_PRESS_MASK, key);
499
500		blocking_notifier_call_chain(&adp5585->event_notifier, key_val, (void *)key_press);
501	}
502}
503
504static irqreturn_t adp5585_irq(int irq, void *data)
505{
506	struct adp5585_dev *adp5585 = data;
507	unsigned int status, ev_cnt;
508	int ret;
509
510	ret = regmap_read(adp5585->regmap, ADP5585_INT_STATUS, &status);
511	if (ret)
512		return IRQ_HANDLED;
513
514	if (status & ADP5585_OVRFLOW_INT)
515		dev_err_ratelimited(adp5585->dev, "Event overflow error\n");
516
517	if (!(status & ADP5585_EVENT_INT))
518		goto out_irq;
519
520	ret = regmap_read(adp5585->regmap, ADP5585_STATUS, &ev_cnt);
521	if (ret)
522		goto out_irq;
523
524	ev_cnt = FIELD_GET(ADP5585_EC_MASK, ev_cnt);
525	if (!ev_cnt)
526		goto out_irq;
527
528	adp5585_report_events(adp5585, ev_cnt);
529out_irq:
530	regmap_write(adp5585->regmap, ADP5585_INT_STATUS, status);
531	return IRQ_HANDLED;
532}
533
534static int adp5585_setup(struct adp5585_dev *adp5585)
535{
536	const struct adp5585_regs *regs = adp5585->regs;
537	unsigned int reg_val = 0, i;
538	int ret;
539
540	/* If pin_6 (ROW5/GPI6) is not available, make sure to mark it as "busy" */
541	if (!adp5585->has_pin6)
542		__set_bit(ADP5585_ROW5, adp5585->pin_usage);
543
544	/* Configure the device with reset and unlock events */
545	for (i = 0; i < adp5585->nkeys_unlock; i++) {
546		ret = regmap_write(adp5585->regmap, ADP5589_UNLOCK1 + i,
547				   adp5585->unlock_keys[i] | ADP5589_UNLOCK_EV_PRESS);
548		if (ret)
549			return ret;
550	}
551
552	if (adp5585->nkeys_unlock) {
553		ret = regmap_update_bits(adp5585->regmap, ADP5589_UNLOCK_TIMERS,
554					 ADP5589_UNLOCK_TIMER, adp5585->unlock_time);
555		if (ret)
556			return ret;
557
558		ret = regmap_set_bits(adp5585->regmap, ADP5589_LOCK_CFG, ADP5589_LOCK_EN);
559		if (ret)
560			return ret;
561	}
562
563	for (i = 0; i < adp5585->nkeys_reset1; i++) {
564		ret = regmap_write(adp5585->regmap, regs->reset1_event_a + i,
565				   adp5585->reset1_keys[i] | ADP5585_RESET_EV_PRESS);
566		if (ret)
567			return ret;
568
569		/* Mark that pin as not usable for the INPUT and GPIO devices. */
570		__set_bit(ADP5585_RESET1_OUT, adp5585->pin_usage);
571	}
572
573	for (i = 0; i < adp5585->nkeys_reset2; i++) {
574		ret = regmap_write(adp5585->regmap, regs->reset2_event_a + i,
575				   adp5585->reset2_keys[i] | ADP5585_RESET_EV_PRESS);
576		if (ret)
577			return ret;
578
579		__set_bit(adp5585->reset2_out, adp5585->pin_usage);
580	}
581
582	if (adp5585->nkeys_reset1 || adp5585->nkeys_reset2) {
583		ret = regmap_write(adp5585->regmap, regs->reset_cfg, adp5585->reset_cfg);
584		if (ret)
585			return ret;
586
587		/* If there's a reset1 event, then R4 is used as an output for the reset signal */
588		if (adp5585->nkeys_reset1)
589			reg_val = ADP5585_R4_EXTEND_CFG_RESET1;
590		/* If there's a reset2 event, then C4 is used as an output for the reset signal */
591		if (adp5585->nkeys_reset2)
592			reg_val |= ADP5585_C4_EXTEND_CFG_RESET2;
593
594		ret = regmap_update_bits(adp5585->regmap, regs->ext_cfg,
595					 ADP5585_C4_EXTEND_CFG_MASK | ADP5585_R4_EXTEND_CFG_MASK,
596					 reg_val);
597		if (ret)
598			return ret;
599	}
600
601	/* Clear any possible event by reading all the FIFO entries */
602	for (i = 0; i < ADP5585_EV_MAX; i++) {
603		ret = regmap_read(adp5585->regmap, ADP5585_FIFO_1 + i, &reg_val);
604		if (ret)
605			return ret;
606	}
607
608	ret = regmap_write(adp5585->regmap, regs->poll_ptime_cfg, adp5585->ev_poll_time);
609	if (ret)
610		return ret;
611
612	/*
613	 * Enable the internal oscillator, as it's shared between multiple
614	 * functions.
615	 */
616	ret = regmap_write(adp5585->regmap, regs->gen_cfg,
617			   ADP5585_OSC_FREQ_500KHZ | ADP5585_INT_CFG | ADP5585_OSC_EN);
618	if (ret)
619		return ret;
620
621	return devm_add_action_or_reset(adp5585->dev, adp5585_osc_disable, adp5585);
622}
623
624static int adp5585_parse_fw(struct adp5585_dev *adp5585)
625{
626	unsigned int prop_val;
627	int ret;
628
629	ret = device_property_read_u32(adp5585->dev, "poll-interval", &prop_val);
630	if (!ret) {
631		adp5585->ev_poll_time = prop_val / 10 - 1;
632		/*
633		 * ev_poll_time is the raw value to be written on the register and 0 to 3 are the
634		 * valid values.
635		 */
636		if (adp5585->ev_poll_time > 3)
637			return dev_err_probe(adp5585->dev, -EINVAL,
638					     "Invalid value(%u) for poll-interval\n", prop_val);
639	}
640
641	ret = adp5585_unlock_ev_parse(adp5585);
642	if (ret)
643		return ret;
644
645	return adp5585_reset_ev_parse(adp5585);
646}
647
648static void adp5585_irq_disable(void *data)
649{
650	struct adp5585_dev *adp5585 = data;
651
652	regmap_write(adp5585->regmap, adp5585->regs->int_en, 0);
653}
654
655static int adp5585_irq_enable(struct i2c_client *i2c,
656			      struct adp5585_dev *adp5585)
657{
658	const struct adp5585_regs *regs = adp5585->regs;
659	unsigned int stat;
660	int ret;
661
662	if (i2c->irq <= 0)
663		return 0;
664
665	ret = devm_request_threaded_irq(&i2c->dev, i2c->irq, NULL, adp5585_irq,
666					IRQF_ONESHOT, i2c->name, adp5585);
667	if (ret)
668		return ret;
669
670	/*
671	 * Clear any possible outstanding interrupt before enabling them. We do that by reading
672	 * the status register and writing back the same value.
673	 */
674	ret = regmap_read(adp5585->regmap, ADP5585_INT_STATUS, &stat);
675	if (ret)
676		return ret;
677
678	ret = regmap_write(adp5585->regmap, ADP5585_INT_STATUS, stat);
679	if (ret)
680		return ret;
681
682	ret = regmap_write(adp5585->regmap, regs->int_en, ADP5585_OVRFLOW_IEN | ADP5585_EVENT_IEN);
683	if (ret)
684		return ret;
685
686	return devm_add_action_or_reset(&i2c->dev, adp5585_irq_disable, adp5585);
687}
688
689static int adp5585_i2c_probe(struct i2c_client *i2c)
690{
691	struct regmap_config *regmap_config;
692	struct adp5585_dev *adp5585;
693	struct gpio_desc *gpio;
694	unsigned int id;
695	int ret;
696
697	adp5585 = devm_kzalloc(&i2c->dev, sizeof(*adp5585), GFP_KERNEL);
698	if (!adp5585)
699		return -ENOMEM;
700
701	i2c_set_clientdata(i2c, adp5585);
702	adp5585->dev = &i2c->dev;
703	adp5585->irq = i2c->irq;
704	BLOCKING_INIT_NOTIFIER_HEAD(&adp5585->event_notifier);
705
706	adp5585->variant = (enum adp5585_variant)(uintptr_t)i2c_get_match_data(i2c);
707	if (!adp5585->variant)
708		return -ENODEV;
709
710	regmap_config = adp5585_fill_variant_config(adp5585);
711	if (IS_ERR(regmap_config))
712		return PTR_ERR(regmap_config);
713
714	ret = devm_regulator_get_enable(&i2c->dev, "vdd");
715	if (ret)
716		return ret;
717
718	gpio = devm_gpiod_get_optional(&i2c->dev, "reset", GPIOD_OUT_HIGH);
719	if (IS_ERR(gpio))
720		return PTR_ERR(gpio);
721
722	/*
723	 * Note the timings are not documented anywhere in the datasheet. They are just
724	 * reasonable values that work.
725	 */
726	if (gpio) {
727		fsleep(30);
728		gpiod_set_value_cansleep(gpio, 0);
729		fsleep(60);
730	}
731
732	adp5585->regmap = devm_regmap_init_i2c(i2c, regmap_config);
733	if (IS_ERR(adp5585->regmap))
734		return dev_err_probe(&i2c->dev, PTR_ERR(adp5585->regmap),
735				     "Failed to initialize register map\n");
736
737	ret = regmap_read(adp5585->regmap, ADP5585_ID, &id);
738	if (ret)
739		return dev_err_probe(&i2c->dev, ret,
740				     "Failed to read device ID\n");
741
742	id &= ADP5585_MAN_ID_MASK;
743	if (id != adp5585->id)
744		return dev_err_probe(&i2c->dev, -ENODEV,
745				     "Invalid device ID 0x%02x\n", id);
746
747	adp5585->pin_usage = devm_bitmap_zalloc(&i2c->dev, adp5585->n_pins, GFP_KERNEL);
748	if (!adp5585->pin_usage)
749		return -ENOMEM;
750
751	ret = adp5585_parse_fw(adp5585);
752	if (ret)
753		return ret;
754
755	ret = adp5585_setup(adp5585);
756	if (ret)
757		return ret;
758
759	ret = adp5585_add_devices(adp5585);
760	if (ret)
761		return ret;
762
763	return adp5585_irq_enable(i2c, adp5585);
764}
765
766static int adp5585_suspend(struct device *dev)
767{
768	struct adp5585_dev *adp5585 = dev_get_drvdata(dev);
769
770	if (adp5585->irq)
771		disable_irq(adp5585->irq);
772
773	regcache_cache_only(adp5585->regmap, true);
774
775	return 0;
776}
777
778static int adp5585_resume(struct device *dev)
779{
780	struct adp5585_dev *adp5585 = dev_get_drvdata(dev);
781	int ret;
782
783	regcache_cache_only(adp5585->regmap, false);
784	regcache_mark_dirty(adp5585->regmap);
785
786	ret = regcache_sync(adp5585->regmap);
787	if (ret)
788		return ret;
789
790	if (adp5585->irq)
791		enable_irq(adp5585->irq);
792
793	return 0;
794}
795
796static DEFINE_SIMPLE_DEV_PM_OPS(adp5585_pm, adp5585_suspend, adp5585_resume);
797
798static const struct of_device_id adp5585_of_match[] = {
799	{
800		.compatible = "adi,adp5585-00",
801		.data = (void *)ADP5585_00,
802	}, {
803		.compatible = "adi,adp5585-01",
804		.data = (void *)ADP5585_01,
805	}, {
806		.compatible = "adi,adp5585-02",
807		.data = (void *)ADP5585_02,
808	}, {
809		.compatible = "adi,adp5585-03",
810		.data = (void *)ADP5585_03,
811	}, {
812		.compatible = "adi,adp5585-04",
813		.data = (void *)ADP5585_04,
814	}, {
815		.compatible = "adi,adp5589-00",
816		.data = (void *)ADP5589_00,
817	}, {
818		.compatible = "adi,adp5589-01",
819		.data = (void *)ADP5589_01,
820	}, {
821		.compatible = "adi,adp5589-02",
822		.data = (void *)ADP5589_02,
823	}, {
824		.compatible = "adi,adp5589",
825		.data = (void *)ADP5589_00,
826	},
827	{ /* sentinel */ }
828};
829MODULE_DEVICE_TABLE(of, adp5585_of_match);
830
831static struct i2c_driver adp5585_i2c_driver = {
832	.driver = {
833		.name = "adp5585",
834		.of_match_table = adp5585_of_match,
835		.pm = pm_sleep_ptr(&adp5585_pm),
836	},
837	.probe = adp5585_i2c_probe,
838};
839module_i2c_driver(adp5585_i2c_driver);
840
841MODULE_DESCRIPTION("ADP5585 core driver");
842MODULE_AUTHOR("Haibo Chen <haibo.chen@nxp.com>");
843MODULE_LICENSE("GPL");
~/ Read-only file preview.