blob: 922497b65ab05af74f92a4e0627b12b7b8bca44f [file] [log] [blame]
Thomas Gleixnerfda8d262019-05-28 09:57:06 -07001// SPDX-License-Identifier: GPL-2.0-only
Michael Hennerich9d2e1732011-05-19 00:59:32 -07002/*
Michael Hennerich3f48e732011-10-18 21:12:39 -07003 * Description: keypad driver for ADP5589, ADP5585
Michael Hennerich9d2e1732011-05-19 00:59:32 -07004 * I2C QWERTY Keypad and IO Expander
5 * Bugs: Enter bugs at http://blackfin.uclinux.org/
6 *
7 * Copyright (C) 2010-2011 Analog Devices Inc.
Michael Hennerich9d2e1732011-05-19 00:59:32 -07008 */
9
10#include <linux/module.h>
Michael Hennerich9d2e1732011-05-19 00:59:32 -070011#include <linux/interrupt.h>
12#include <linux/irq.h>
13#include <linux/workqueue.h>
14#include <linux/errno.h>
15#include <linux/pm.h>
16#include <linux/platform_device.h>
17#include <linux/input.h>
18#include <linux/i2c.h>
19#include <linux/gpio.h>
20#include <linux/slab.h>
21
22#include <linux/input/adp5589.h>
23
Michael Hennerich3f48e732011-10-18 21:12:39 -070024/* ADP5589/ADP5585 Common Registers */
25#define ADP5589_5_ID 0x00
26#define ADP5589_5_INT_STATUS 0x01
27#define ADP5589_5_STATUS 0x02
28#define ADP5589_5_FIFO_1 0x03
29#define ADP5589_5_FIFO_2 0x04
30#define ADP5589_5_FIFO_3 0x05
31#define ADP5589_5_FIFO_4 0x06
32#define ADP5589_5_FIFO_5 0x07
33#define ADP5589_5_FIFO_6 0x08
34#define ADP5589_5_FIFO_7 0x09
35#define ADP5589_5_FIFO_8 0x0A
36#define ADP5589_5_FIFO_9 0x0B
37#define ADP5589_5_FIFO_10 0x0C
38#define ADP5589_5_FIFO_11 0x0D
39#define ADP5589_5_FIFO_12 0x0E
40#define ADP5589_5_FIFO_13 0x0F
41#define ADP5589_5_FIFO_14 0x10
42#define ADP5589_5_FIFO_15 0x11
43#define ADP5589_5_FIFO_16 0x12
44#define ADP5589_5_GPI_INT_STAT_A 0x13
45#define ADP5589_5_GPI_INT_STAT_B 0x14
46
47/* ADP5589 Registers */
48#define ADP5589_GPI_INT_STAT_C 0x15
49#define ADP5589_GPI_STATUS_A 0x16
50#define ADP5589_GPI_STATUS_B 0x17
51#define ADP5589_GPI_STATUS_C 0x18
52#define ADP5589_RPULL_CONFIG_A 0x19
53#define ADP5589_RPULL_CONFIG_B 0x1A
54#define ADP5589_RPULL_CONFIG_C 0x1B
55#define ADP5589_RPULL_CONFIG_D 0x1C
56#define ADP5589_RPULL_CONFIG_E 0x1D
57#define ADP5589_GPI_INT_LEVEL_A 0x1E
58#define ADP5589_GPI_INT_LEVEL_B 0x1F
59#define ADP5589_GPI_INT_LEVEL_C 0x20
60#define ADP5589_GPI_EVENT_EN_A 0x21
61#define ADP5589_GPI_EVENT_EN_B 0x22
62#define ADP5589_GPI_EVENT_EN_C 0x23
63#define ADP5589_GPI_INTERRUPT_EN_A 0x24
64#define ADP5589_GPI_INTERRUPT_EN_B 0x25
65#define ADP5589_GPI_INTERRUPT_EN_C 0x26
66#define ADP5589_DEBOUNCE_DIS_A 0x27
67#define ADP5589_DEBOUNCE_DIS_B 0x28
68#define ADP5589_DEBOUNCE_DIS_C 0x29
69#define ADP5589_GPO_DATA_OUT_A 0x2A
70#define ADP5589_GPO_DATA_OUT_B 0x2B
71#define ADP5589_GPO_DATA_OUT_C 0x2C
72#define ADP5589_GPO_OUT_MODE_A 0x2D
73#define ADP5589_GPO_OUT_MODE_B 0x2E
74#define ADP5589_GPO_OUT_MODE_C 0x2F
75#define ADP5589_GPIO_DIRECTION_A 0x30
76#define ADP5589_GPIO_DIRECTION_B 0x31
77#define ADP5589_GPIO_DIRECTION_C 0x32
78#define ADP5589_UNLOCK1 0x33
79#define ADP5589_UNLOCK2 0x34
80#define ADP5589_EXT_LOCK_EVENT 0x35
81#define ADP5589_UNLOCK_TIMERS 0x36
82#define ADP5589_LOCK_CFG 0x37
83#define ADP5589_RESET1_EVENT_A 0x38
84#define ADP5589_RESET1_EVENT_B 0x39
85#define ADP5589_RESET1_EVENT_C 0x3A
86#define ADP5589_RESET2_EVENT_A 0x3B
87#define ADP5589_RESET2_EVENT_B 0x3C
88#define ADP5589_RESET_CFG 0x3D
89#define ADP5589_PWM_OFFT_LOW 0x3E
90#define ADP5589_PWM_OFFT_HIGH 0x3F
91#define ADP5589_PWM_ONT_LOW 0x40
92#define ADP5589_PWM_ONT_HIGH 0x41
93#define ADP5589_PWM_CFG 0x42
94#define ADP5589_CLOCK_DIV_CFG 0x43
95#define ADP5589_LOGIC_1_CFG 0x44
96#define ADP5589_LOGIC_2_CFG 0x45
97#define ADP5589_LOGIC_FF_CFG 0x46
98#define ADP5589_LOGIC_INT_EVENT_EN 0x47
99#define ADP5589_POLL_PTIME_CFG 0x48
100#define ADP5589_PIN_CONFIG_A 0x49
101#define ADP5589_PIN_CONFIG_B 0x4A
102#define ADP5589_PIN_CONFIG_C 0x4B
103#define ADP5589_PIN_CONFIG_D 0x4C
104#define ADP5589_GENERAL_CFG 0x4D
105#define ADP5589_INT_EN 0x4E
106
107/* ADP5585 Registers */
108#define ADP5585_GPI_STATUS_A 0x15
109#define ADP5585_GPI_STATUS_B 0x16
110#define ADP5585_RPULL_CONFIG_A 0x17
111#define ADP5585_RPULL_CONFIG_B 0x18
112#define ADP5585_RPULL_CONFIG_C 0x19
113#define ADP5585_RPULL_CONFIG_D 0x1A
114#define ADP5585_GPI_INT_LEVEL_A 0x1B
115#define ADP5585_GPI_INT_LEVEL_B 0x1C
116#define ADP5585_GPI_EVENT_EN_A 0x1D
117#define ADP5585_GPI_EVENT_EN_B 0x1E
118#define ADP5585_GPI_INTERRUPT_EN_A 0x1F
119#define ADP5585_GPI_INTERRUPT_EN_B 0x20
120#define ADP5585_DEBOUNCE_DIS_A 0x21
121#define ADP5585_DEBOUNCE_DIS_B 0x22
122#define ADP5585_GPO_DATA_OUT_A 0x23
123#define ADP5585_GPO_DATA_OUT_B 0x24
124#define ADP5585_GPO_OUT_MODE_A 0x25
125#define ADP5585_GPO_OUT_MODE_B 0x26
126#define ADP5585_GPIO_DIRECTION_A 0x27
127#define ADP5585_GPIO_DIRECTION_B 0x28
128#define ADP5585_RESET1_EVENT_A 0x29
129#define ADP5585_RESET1_EVENT_B 0x2A
130#define ADP5585_RESET1_EVENT_C 0x2B
131#define ADP5585_RESET2_EVENT_A 0x2C
132#define ADP5585_RESET2_EVENT_B 0x2D
133#define ADP5585_RESET_CFG 0x2E
134#define ADP5585_PWM_OFFT_LOW 0x2F
135#define ADP5585_PWM_OFFT_HIGH 0x30
136#define ADP5585_PWM_ONT_LOW 0x31
137#define ADP5585_PWM_ONT_HIGH 0x32
138#define ADP5585_PWM_CFG 0x33
139#define ADP5585_LOGIC_CFG 0x34
140#define ADP5585_LOGIC_FF_CFG 0x35
141#define ADP5585_LOGIC_INT_EVENT_EN 0x36
142#define ADP5585_POLL_PTIME_CFG 0x37
143#define ADP5585_PIN_CONFIG_A 0x38
144#define ADP5585_PIN_CONFIG_B 0x39
145#define ADP5585_PIN_CONFIG_D 0x3A
146#define ADP5585_GENERAL_CFG 0x3B
147#define ADP5585_INT_EN 0x3C
148
149/* ID Register */
150#define ADP5589_5_DEVICE_ID_MASK 0xF
151#define ADP5589_5_MAN_ID_MASK 0xF
152#define ADP5589_5_MAN_ID_SHIFT 4
153#define ADP5589_5_MAN_ID 0x02
154
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700155/* GENERAL_CFG Register */
156#define OSC_EN (1 << 7)
157#define CORE_CLK(x) (((x) & 0x3) << 5)
Michael Hennerich3f48e732011-10-18 21:12:39 -0700158#define LCK_TRK_LOGIC (1 << 4) /* ADP5589 only */
159#define LCK_TRK_GPI (1 << 3) /* ADP5589 only */
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700160#define INT_CFG (1 << 1)
161#define RST_CFG (1 << 0)
162
163/* INT_EN Register */
Michael Hennerich3f48e732011-10-18 21:12:39 -0700164#define LOGIC2_IEN (1 << 5) /* ADP5589 only */
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700165#define LOGIC1_IEN (1 << 4)
Michael Hennerich3f48e732011-10-18 21:12:39 -0700166#define LOCK_IEN (1 << 3) /* ADP5589 only */
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700167#define OVRFLOW_IEN (1 << 2)
168#define GPI_IEN (1 << 1)
169#define EVENT_IEN (1 << 0)
170
171/* Interrupt Status Register */
Michael Hennerich3f48e732011-10-18 21:12:39 -0700172#define LOGIC2_INT (1 << 5) /* ADP5589 only */
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700173#define LOGIC1_INT (1 << 4)
Michael Hennerich3f48e732011-10-18 21:12:39 -0700174#define LOCK_INT (1 << 3) /* ADP5589 only */
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700175#define OVRFLOW_INT (1 << 2)
176#define GPI_INT (1 << 1)
177#define EVENT_INT (1 << 0)
178
179/* STATUS Register */
Michael Hennerich3f48e732011-10-18 21:12:39 -0700180#define LOGIC2_STAT (1 << 7) /* ADP5589 only */
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700181#define LOGIC1_STAT (1 << 6)
Michael Hennerich3f48e732011-10-18 21:12:39 -0700182#define LOCK_STAT (1 << 5) /* ADP5589 only */
Guido Martínezc615dcb2015-05-06 16:35:21 -0700183#define KEC 0x1F
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700184
185/* PIN_CONFIG_D Register */
186#define C4_EXTEND_CFG (1 << 6) /* RESET2 */
187#define R4_EXTEND_CFG (1 << 5) /* RESET1 */
188
189/* LOCK_CFG */
190#define LOCK_EN (1 << 0)
191
192#define PTIME_MASK 0x3
Michael Hennerich3f48e732011-10-18 21:12:39 -0700193#define LTIME_MASK 0x3 /* ADP5589 only */
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700194
195/* Key Event Register xy */
196#define KEY_EV_PRESSED (1 << 7)
197#define KEY_EV_MASK (0x7F)
198
199#define KEYP_MAX_EVENT 16
Michael Hennerich3f48e732011-10-18 21:12:39 -0700200#define ADP5589_MAXGPIO 19
201#define ADP5585_MAXGPIO 11 /* 10 on the ADP5585-01, 11 on ADP5585-02 */
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700202
Michael Hennerich3f48e732011-10-18 21:12:39 -0700203enum {
204 ADP5589,
205 ADP5585_01,
206 ADP5585_02
207};
208
209struct adp_constants {
210 u8 maxgpio;
211 u8 keymapsize;
212 u8 gpi_pin_row_base;
213 u8 gpi_pin_row_end;
214 u8 gpi_pin_col_base;
215 u8 gpi_pin_base;
216 u8 gpi_pin_end;
217 u8 gpimapsize_max;
218 u8 max_row_num;
219 u8 max_col_num;
220 u8 row_mask;
221 u8 col_mask;
222 u8 col_shift;
223 u8 c4_extend_cfg;
224 u8 (*bank) (u8 offset);
225 u8 (*bit) (u8 offset);
226 u8 (*reg) (u8 reg);
227};
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700228
229struct adp5589_kpad {
230 struct i2c_client *client;
231 struct input_dev *input;
Michael Hennerich3f48e732011-10-18 21:12:39 -0700232 const struct adp_constants *var;
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700233 unsigned short keycode[ADP5589_KEYMAPSIZE];
234 const struct adp5589_gpi_map *gpimap;
235 unsigned short gpimapsize;
236 unsigned extend_cfg;
Michael Hennerich3f48e732011-10-18 21:12:39 -0700237 bool is_adp5585;
Lars-Peter Clausen7008daf2016-02-09 09:35:33 -0800238 bool support_row5;
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700239#ifdef CONFIG_GPIOLIB
Michael Hennerich3f48e732011-10-18 21:12:39 -0700240 unsigned char gpiomap[ADP5589_MAXGPIO];
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700241 bool export_gpio;
242 struct gpio_chip gc;
243 struct mutex gpio_lock; /* Protect cached dir, dat_out */
244 u8 dat_out[3];
245 u8 dir[3];
246#endif
247};
248
Michael Hennerich3f48e732011-10-18 21:12:39 -0700249/*
250 * ADP5589 / ADP5585 derivative / variant handling
251 */
252
253
254/* ADP5589 */
255
256static unsigned char adp5589_bank(unsigned char offset)
257{
258 return offset >> 3;
259}
260
261static unsigned char adp5589_bit(unsigned char offset)
262{
263 return 1u << (offset & 0x7);
264}
265
266static unsigned char adp5589_reg(unsigned char reg)
267{
268 return reg;
269}
270
271static const struct adp_constants const_adp5589 = {
272 .maxgpio = ADP5589_MAXGPIO,
273 .keymapsize = ADP5589_KEYMAPSIZE,
274 .gpi_pin_row_base = ADP5589_GPI_PIN_ROW_BASE,
275 .gpi_pin_row_end = ADP5589_GPI_PIN_ROW_END,
276 .gpi_pin_col_base = ADP5589_GPI_PIN_COL_BASE,
277 .gpi_pin_base = ADP5589_GPI_PIN_BASE,
278 .gpi_pin_end = ADP5589_GPI_PIN_END,
279 .gpimapsize_max = ADP5589_GPIMAPSIZE_MAX,
280 .c4_extend_cfg = 12,
281 .max_row_num = ADP5589_MAX_ROW_NUM,
282 .max_col_num = ADP5589_MAX_COL_NUM,
283 .row_mask = ADP5589_ROW_MASK,
284 .col_mask = ADP5589_COL_MASK,
285 .col_shift = ADP5589_COL_SHIFT,
286 .bank = adp5589_bank,
287 .bit = adp5589_bit,
288 .reg = adp5589_reg,
289};
290
291/* ADP5585 */
292
293static unsigned char adp5585_bank(unsigned char offset)
294{
295 return offset > ADP5585_MAX_ROW_NUM;
296}
297
298static unsigned char adp5585_bit(unsigned char offset)
299{
300 return (offset > ADP5585_MAX_ROW_NUM) ?
301 1u << (offset - ADP5585_COL_SHIFT) : 1u << offset;
302}
303
304static const unsigned char adp5585_reg_lut[] = {
305 [ADP5589_GPI_STATUS_A] = ADP5585_GPI_STATUS_A,
306 [ADP5589_GPI_STATUS_B] = ADP5585_GPI_STATUS_B,
307 [ADP5589_RPULL_CONFIG_A] = ADP5585_RPULL_CONFIG_A,
308 [ADP5589_RPULL_CONFIG_B] = ADP5585_RPULL_CONFIG_B,
309 [ADP5589_RPULL_CONFIG_C] = ADP5585_RPULL_CONFIG_C,
310 [ADP5589_RPULL_CONFIG_D] = ADP5585_RPULL_CONFIG_D,
311 [ADP5589_GPI_INT_LEVEL_A] = ADP5585_GPI_INT_LEVEL_A,
312 [ADP5589_GPI_INT_LEVEL_B] = ADP5585_GPI_INT_LEVEL_B,
313 [ADP5589_GPI_EVENT_EN_A] = ADP5585_GPI_EVENT_EN_A,
314 [ADP5589_GPI_EVENT_EN_B] = ADP5585_GPI_EVENT_EN_B,
315 [ADP5589_GPI_INTERRUPT_EN_A] = ADP5585_GPI_INTERRUPT_EN_A,
316 [ADP5589_GPI_INTERRUPT_EN_B] = ADP5585_GPI_INTERRUPT_EN_B,
317 [ADP5589_DEBOUNCE_DIS_A] = ADP5585_DEBOUNCE_DIS_A,
318 [ADP5589_DEBOUNCE_DIS_B] = ADP5585_DEBOUNCE_DIS_B,
319 [ADP5589_GPO_DATA_OUT_A] = ADP5585_GPO_DATA_OUT_A,
320 [ADP5589_GPO_DATA_OUT_B] = ADP5585_GPO_DATA_OUT_B,
321 [ADP5589_GPO_OUT_MODE_A] = ADP5585_GPO_OUT_MODE_A,
322 [ADP5589_GPO_OUT_MODE_B] = ADP5585_GPO_OUT_MODE_B,
323 [ADP5589_GPIO_DIRECTION_A] = ADP5585_GPIO_DIRECTION_A,
324 [ADP5589_GPIO_DIRECTION_B] = ADP5585_GPIO_DIRECTION_B,
325 [ADP5589_RESET1_EVENT_A] = ADP5585_RESET1_EVENT_A,
326 [ADP5589_RESET1_EVENT_B] = ADP5585_RESET1_EVENT_B,
327 [ADP5589_RESET1_EVENT_C] = ADP5585_RESET1_EVENT_C,
328 [ADP5589_RESET2_EVENT_A] = ADP5585_RESET2_EVENT_A,
329 [ADP5589_RESET2_EVENT_B] = ADP5585_RESET2_EVENT_B,
330 [ADP5589_RESET_CFG] = ADP5585_RESET_CFG,
331 [ADP5589_PWM_OFFT_LOW] = ADP5585_PWM_OFFT_LOW,
332 [ADP5589_PWM_OFFT_HIGH] = ADP5585_PWM_OFFT_HIGH,
333 [ADP5589_PWM_ONT_LOW] = ADP5585_PWM_ONT_LOW,
334 [ADP5589_PWM_ONT_HIGH] = ADP5585_PWM_ONT_HIGH,
335 [ADP5589_PWM_CFG] = ADP5585_PWM_CFG,
336 [ADP5589_LOGIC_1_CFG] = ADP5585_LOGIC_CFG,
337 [ADP5589_LOGIC_FF_CFG] = ADP5585_LOGIC_FF_CFG,
338 [ADP5589_LOGIC_INT_EVENT_EN] = ADP5585_LOGIC_INT_EVENT_EN,
339 [ADP5589_POLL_PTIME_CFG] = ADP5585_POLL_PTIME_CFG,
340 [ADP5589_PIN_CONFIG_A] = ADP5585_PIN_CONFIG_A,
341 [ADP5589_PIN_CONFIG_B] = ADP5585_PIN_CONFIG_B,
342 [ADP5589_PIN_CONFIG_D] = ADP5585_PIN_CONFIG_D,
343 [ADP5589_GENERAL_CFG] = ADP5585_GENERAL_CFG,
344 [ADP5589_INT_EN] = ADP5585_INT_EN,
345};
346
347static unsigned char adp5585_reg(unsigned char reg)
348{
349 return adp5585_reg_lut[reg];
350}
351
352static const struct adp_constants const_adp5585 = {
353 .maxgpio = ADP5585_MAXGPIO,
354 .keymapsize = ADP5585_KEYMAPSIZE,
355 .gpi_pin_row_base = ADP5585_GPI_PIN_ROW_BASE,
356 .gpi_pin_row_end = ADP5585_GPI_PIN_ROW_END,
357 .gpi_pin_col_base = ADP5585_GPI_PIN_COL_BASE,
358 .gpi_pin_base = ADP5585_GPI_PIN_BASE,
359 .gpi_pin_end = ADP5585_GPI_PIN_END,
360 .gpimapsize_max = ADP5585_GPIMAPSIZE_MAX,
361 .c4_extend_cfg = 10,
362 .max_row_num = ADP5585_MAX_ROW_NUM,
363 .max_col_num = ADP5585_MAX_COL_NUM,
364 .row_mask = ADP5585_ROW_MASK,
365 .col_mask = ADP5585_COL_MASK,
366 .col_shift = ADP5585_COL_SHIFT,
367 .bank = adp5585_bank,
368 .bit = adp5585_bit,
369 .reg = adp5585_reg,
370};
371
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700372static int adp5589_read(struct i2c_client *client, u8 reg)
373{
374 int ret = i2c_smbus_read_byte_data(client, reg);
375
376 if (ret < 0)
377 dev_err(&client->dev, "Read Error\n");
378
379 return ret;
380}
381
382static int adp5589_write(struct i2c_client *client, u8 reg, u8 val)
383{
384 return i2c_smbus_write_byte_data(client, reg, val);
385}
386
387#ifdef CONFIG_GPIOLIB
388static int adp5589_gpio_get_value(struct gpio_chip *chip, unsigned off)
389{
Linus Walleij839850f2015-12-08 16:32:08 +0100390 struct adp5589_kpad *kpad = gpiochip_get_data(chip);
Michael Hennerich3f48e732011-10-18 21:12:39 -0700391 unsigned int bank = kpad->var->bank(kpad->gpiomap[off]);
392 unsigned int bit = kpad->var->bit(kpad->gpiomap[off]);
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700393
Michael Hennerich3f48e732011-10-18 21:12:39 -0700394 return !!(adp5589_read(kpad->client,
395 kpad->var->reg(ADP5589_GPI_STATUS_A) + bank) &
396 bit);
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700397}
398
399static void adp5589_gpio_set_value(struct gpio_chip *chip,
400 unsigned off, int val)
401{
Linus Walleij839850f2015-12-08 16:32:08 +0100402 struct adp5589_kpad *kpad = gpiochip_get_data(chip);
Michael Hennerich3f48e732011-10-18 21:12:39 -0700403 unsigned int bank = kpad->var->bank(kpad->gpiomap[off]);
404 unsigned int bit = kpad->var->bit(kpad->gpiomap[off]);
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700405
406 mutex_lock(&kpad->gpio_lock);
407
408 if (val)
409 kpad->dat_out[bank] |= bit;
410 else
411 kpad->dat_out[bank] &= ~bit;
412
Michael Hennerich3f48e732011-10-18 21:12:39 -0700413 adp5589_write(kpad->client, kpad->var->reg(ADP5589_GPO_DATA_OUT_A) +
414 bank, kpad->dat_out[bank]);
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700415
416 mutex_unlock(&kpad->gpio_lock);
417}
418
419static int adp5589_gpio_direction_input(struct gpio_chip *chip, unsigned off)
420{
Linus Walleij839850f2015-12-08 16:32:08 +0100421 struct adp5589_kpad *kpad = gpiochip_get_data(chip);
Michael Hennerich3f48e732011-10-18 21:12:39 -0700422 unsigned int bank = kpad->var->bank(kpad->gpiomap[off]);
423 unsigned int bit = kpad->var->bit(kpad->gpiomap[off]);
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700424 int ret;
425
426 mutex_lock(&kpad->gpio_lock);
427
428 kpad->dir[bank] &= ~bit;
Michael Hennerich3f48e732011-10-18 21:12:39 -0700429 ret = adp5589_write(kpad->client,
430 kpad->var->reg(ADP5589_GPIO_DIRECTION_A) + bank,
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700431 kpad->dir[bank]);
432
433 mutex_unlock(&kpad->gpio_lock);
434
435 return ret;
436}
437
438static int adp5589_gpio_direction_output(struct gpio_chip *chip,
439 unsigned off, int val)
440{
Linus Walleij839850f2015-12-08 16:32:08 +0100441 struct adp5589_kpad *kpad = gpiochip_get_data(chip);
Michael Hennerich3f48e732011-10-18 21:12:39 -0700442 unsigned int bank = kpad->var->bank(kpad->gpiomap[off]);
443 unsigned int bit = kpad->var->bit(kpad->gpiomap[off]);
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700444 int ret;
445
446 mutex_lock(&kpad->gpio_lock);
447
448 kpad->dir[bank] |= bit;
449
450 if (val)
451 kpad->dat_out[bank] |= bit;
452 else
453 kpad->dat_out[bank] &= ~bit;
454
Michael Hennerich3f48e732011-10-18 21:12:39 -0700455 ret = adp5589_write(kpad->client, kpad->var->reg(ADP5589_GPO_DATA_OUT_A)
456 + bank, kpad->dat_out[bank]);
457 ret |= adp5589_write(kpad->client,
458 kpad->var->reg(ADP5589_GPIO_DIRECTION_A) + bank,
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700459 kpad->dir[bank]);
460
461 mutex_unlock(&kpad->gpio_lock);
462
463 return ret;
464}
465
Bill Pemberton5298cc42012-11-23 21:38:25 -0800466static int adp5589_build_gpiomap(struct adp5589_kpad *kpad,
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700467 const struct adp5589_kpad_platform_data *pdata)
468{
Michael Hennerich3f48e732011-10-18 21:12:39 -0700469 bool pin_used[ADP5589_MAXGPIO];
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700470 int n_unused = 0;
471 int i;
472
473 memset(pin_used, false, sizeof(pin_used));
474
Michael Hennerich3f48e732011-10-18 21:12:39 -0700475 for (i = 0; i < kpad->var->maxgpio; i++)
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700476 if (pdata->keypad_en_mask & (1 << i))
477 pin_used[i] = true;
478
479 for (i = 0; i < kpad->gpimapsize; i++)
Michael Hennerich3f48e732011-10-18 21:12:39 -0700480 pin_used[kpad->gpimap[i].pin - kpad->var->gpi_pin_base] = true;
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700481
482 if (kpad->extend_cfg & R4_EXTEND_CFG)
483 pin_used[4] = true;
484
485 if (kpad->extend_cfg & C4_EXTEND_CFG)
Michael Hennerich3f48e732011-10-18 21:12:39 -0700486 pin_used[kpad->var->c4_extend_cfg] = true;
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700487
Lars-Peter Clausen7008daf2016-02-09 09:35:33 -0800488 if (!kpad->support_row5)
Michael Hennerich3f48e732011-10-18 21:12:39 -0700489 pin_used[5] = true;
490
491 for (i = 0; i < kpad->var->maxgpio; i++)
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700492 if (!pin_used[i])
493 kpad->gpiomap[n_unused++] = i;
494
495 return n_unused;
496}
497
Bill Pemberton5298cc42012-11-23 21:38:25 -0800498static int adp5589_gpio_add(struct adp5589_kpad *kpad)
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700499{
500 struct device *dev = &kpad->client->dev;
Jingoo Hanc838cb32013-12-05 19:21:10 -0800501 const struct adp5589_kpad_platform_data *pdata = dev_get_platdata(dev);
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700502 const struct adp5589_gpio_platform_data *gpio_data = pdata->gpio_data;
503 int i, error;
504
505 if (!gpio_data)
506 return 0;
507
Lars-Peter Clausenb8374b82019-07-14 14:30:36 -0700508 kpad->gc.parent = dev;
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700509 kpad->gc.ngpio = adp5589_build_gpiomap(kpad, pdata);
510 if (kpad->gc.ngpio == 0) {
511 dev_info(dev, "No unused gpios left to export\n");
512 return 0;
513 }
514
515 kpad->export_gpio = true;
516
517 kpad->gc.direction_input = adp5589_gpio_direction_input;
518 kpad->gc.direction_output = adp5589_gpio_direction_output;
519 kpad->gc.get = adp5589_gpio_get_value;
520 kpad->gc.set = adp5589_gpio_set_value;
521 kpad->gc.can_sleep = 1;
522
523 kpad->gc.base = gpio_data->gpio_start;
524 kpad->gc.label = kpad->client->name;
525 kpad->gc.owner = THIS_MODULE;
526
527 mutex_init(&kpad->gpio_lock);
528
Linus Walleij839850f2015-12-08 16:32:08 +0100529 error = gpiochip_add_data(&kpad->gc, kpad);
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700530 if (error) {
Linus Walleij839850f2015-12-08 16:32:08 +0100531 dev_err(dev, "gpiochip_add_data() failed, err: %d\n", error);
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700532 return error;
533 }
534
Michael Hennerich3f48e732011-10-18 21:12:39 -0700535 for (i = 0; i <= kpad->var->bank(kpad->var->maxgpio); i++) {
536 kpad->dat_out[i] = adp5589_read(kpad->client, kpad->var->reg(
537 ADP5589_GPO_DATA_OUT_A) + i);
538 kpad->dir[i] = adp5589_read(kpad->client, kpad->var->reg(
539 ADP5589_GPIO_DIRECTION_A) + i);
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700540 }
541
542 if (gpio_data->setup) {
543 error = gpio_data->setup(kpad->client,
544 kpad->gc.base, kpad->gc.ngpio,
545 gpio_data->context);
546 if (error)
547 dev_warn(dev, "setup failed, %d\n", error);
548 }
549
550 return 0;
551}
552
Bill Pembertone2619cf2012-11-23 21:50:47 -0800553static void adp5589_gpio_remove(struct adp5589_kpad *kpad)
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700554{
555 struct device *dev = &kpad->client->dev;
Jingoo Hanc838cb32013-12-05 19:21:10 -0800556 const struct adp5589_kpad_platform_data *pdata = dev_get_platdata(dev);
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700557 const struct adp5589_gpio_platform_data *gpio_data = pdata->gpio_data;
558 int error;
559
560 if (!kpad->export_gpio)
561 return;
562
563 if (gpio_data->teardown) {
564 error = gpio_data->teardown(kpad->client,
565 kpad->gc.base, kpad->gc.ngpio,
566 gpio_data->context);
567 if (error)
568 dev_warn(dev, "teardown failed %d\n", error);
569 }
570
abdoulaye berthe88d5e522014-07-12 22:30:14 +0200571 gpiochip_remove(&kpad->gc);
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700572}
573#else
574static inline int adp5589_gpio_add(struct adp5589_kpad *kpad)
575{
576 return 0;
577}
578
579static inline void adp5589_gpio_remove(struct adp5589_kpad *kpad)
580{
581}
582#endif
583
584static void adp5589_report_switches(struct adp5589_kpad *kpad,
585 int key, int key_val)
586{
587 int i;
588
589 for (i = 0; i < kpad->gpimapsize; i++) {
590 if (key_val == kpad->gpimap[i].pin) {
591 input_report_switch(kpad->input,
592 kpad->gpimap[i].sw_evt,
593 key & KEY_EV_PRESSED);
594 break;
595 }
596 }
597}
598
599static void adp5589_report_events(struct adp5589_kpad *kpad, int ev_cnt)
600{
601 int i;
602
603 for (i = 0; i < ev_cnt; i++) {
Michael Hennerich3f48e732011-10-18 21:12:39 -0700604 int key = adp5589_read(kpad->client, ADP5589_5_FIFO_1 + i);
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700605 int key_val = key & KEY_EV_MASK;
606
Michael Hennerich3f48e732011-10-18 21:12:39 -0700607 if (key_val >= kpad->var->gpi_pin_base &&
608 key_val <= kpad->var->gpi_pin_end) {
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700609 adp5589_report_switches(kpad, key, key_val);
610 } else {
611 input_report_key(kpad->input,
612 kpad->keycode[key_val - 1],
613 key & KEY_EV_PRESSED);
614 }
615 }
616}
617
618static irqreturn_t adp5589_irq(int irq, void *handle)
619{
620 struct adp5589_kpad *kpad = handle;
621 struct i2c_client *client = kpad->client;
622 int status, ev_cnt;
623
Michael Hennerich3f48e732011-10-18 21:12:39 -0700624 status = adp5589_read(client, ADP5589_5_INT_STATUS);
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700625
626 if (status & OVRFLOW_INT) /* Unlikely and should never happen */
627 dev_err(&client->dev, "Event Overflow Error\n");
628
629 if (status & EVENT_INT) {
Michael Hennerich3f48e732011-10-18 21:12:39 -0700630 ev_cnt = adp5589_read(client, ADP5589_5_STATUS) & KEC;
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700631 if (ev_cnt) {
632 adp5589_report_events(kpad, ev_cnt);
633 input_sync(kpad->input);
634 }
635 }
636
Michael Hennerich3f48e732011-10-18 21:12:39 -0700637 adp5589_write(client, ADP5589_5_INT_STATUS, status); /* Status is W1C */
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700638
639 return IRQ_HANDLED;
640}
641
Bill Pemberton5298cc42012-11-23 21:38:25 -0800642static int adp5589_get_evcode(struct adp5589_kpad *kpad, unsigned short key)
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700643{
644 int i;
645
Michael Hennerich3f48e732011-10-18 21:12:39 -0700646 for (i = 0; i < kpad->var->keymapsize; i++)
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700647 if (key == kpad->keycode[i])
648 return (i + 1) | KEY_EV_PRESSED;
649
650 dev_err(&kpad->client->dev, "RESET/UNLOCK key not in keycode map\n");
651
652 return -EINVAL;
653}
654
Bill Pemberton5298cc42012-11-23 21:38:25 -0800655static int adp5589_setup(struct adp5589_kpad *kpad)
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700656{
657 struct i2c_client *client = kpad->client;
658 const struct adp5589_kpad_platform_data *pdata =
Jingoo Hanc838cb32013-12-05 19:21:10 -0800659 dev_get_platdata(&client->dev);
Michael Hennerich3f48e732011-10-18 21:12:39 -0700660 u8 (*reg) (u8) = kpad->var->reg;
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700661 unsigned char evt_mode1 = 0, evt_mode2 = 0, evt_mode3 = 0;
662 unsigned char pull_mask = 0;
Michael Hennerich3f48e732011-10-18 21:12:39 -0700663 int i, ret;
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700664
Michael Hennerich3f48e732011-10-18 21:12:39 -0700665 ret = adp5589_write(client, reg(ADP5589_PIN_CONFIG_A),
666 pdata->keypad_en_mask & kpad->var->row_mask);
667 ret |= adp5589_write(client, reg(ADP5589_PIN_CONFIG_B),
668 (pdata->keypad_en_mask >> kpad->var->col_shift) &
669 kpad->var->col_mask);
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700670
Michael Hennerich3f48e732011-10-18 21:12:39 -0700671 if (!kpad->is_adp5585)
672 ret |= adp5589_write(client, ADP5589_PIN_CONFIG_C,
673 (pdata->keypad_en_mask >> 16) & 0xFF);
674
675 if (!kpad->is_adp5585 && pdata->en_keylock) {
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700676 ret |= adp5589_write(client, ADP5589_UNLOCK1,
677 pdata->unlock_key1);
678 ret |= adp5589_write(client, ADP5589_UNLOCK2,
679 pdata->unlock_key2);
680 ret |= adp5589_write(client, ADP5589_UNLOCK_TIMERS,
681 pdata->unlock_timer & LTIME_MASK);
682 ret |= adp5589_write(client, ADP5589_LOCK_CFG, LOCK_EN);
683 }
684
685 for (i = 0; i < KEYP_MAX_EVENT; i++)
Michael Hennerich3f48e732011-10-18 21:12:39 -0700686 ret |= adp5589_read(client, ADP5589_5_FIFO_1 + i);
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700687
688 for (i = 0; i < pdata->gpimapsize; i++) {
689 unsigned short pin = pdata->gpimap[i].pin;
690
Michael Hennerich3f48e732011-10-18 21:12:39 -0700691 if (pin <= kpad->var->gpi_pin_row_end) {
692 evt_mode1 |= (1 << (pin - kpad->var->gpi_pin_row_base));
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700693 } else {
694 evt_mode2 |=
Michael Hennerich3f48e732011-10-18 21:12:39 -0700695 ((1 << (pin - kpad->var->gpi_pin_col_base)) & 0xFF);
696 if (!kpad->is_adp5585)
697 evt_mode3 |= ((1 << (pin -
698 kpad->var->gpi_pin_col_base)) >> 8);
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700699 }
700 }
701
702 if (pdata->gpimapsize) {
Michael Hennerich3f48e732011-10-18 21:12:39 -0700703 ret |= adp5589_write(client, reg(ADP5589_GPI_EVENT_EN_A),
704 evt_mode1);
705 ret |= adp5589_write(client, reg(ADP5589_GPI_EVENT_EN_B),
706 evt_mode2);
707 if (!kpad->is_adp5585)
708 ret |= adp5589_write(client,
709 reg(ADP5589_GPI_EVENT_EN_C),
710 evt_mode3);
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700711 }
712
713 if (pdata->pull_dis_mask & pdata->pullup_en_100k &
Michael Hennerich3f48e732011-10-18 21:12:39 -0700714 pdata->pullup_en_300k & pdata->pulldown_en_300k)
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700715 dev_warn(&client->dev, "Conflicting pull resistor config\n");
716
Michael Hennerich3f48e732011-10-18 21:12:39 -0700717 for (i = 0; i <= kpad->var->max_row_num; i++) {
718 unsigned val = 0, bit = (1 << i);
719 if (pdata->pullup_en_300k & bit)
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700720 val = 0;
Michael Hennerich3f48e732011-10-18 21:12:39 -0700721 else if (pdata->pulldown_en_300k & bit)
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700722 val = 1;
Michael Hennerich3f48e732011-10-18 21:12:39 -0700723 else if (pdata->pullup_en_100k & bit)
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700724 val = 2;
Michael Hennerich3f48e732011-10-18 21:12:39 -0700725 else if (pdata->pull_dis_mask & bit)
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700726 val = 3;
727
728 pull_mask |= val << (2 * (i & 0x3));
729
Guido Martínez195e6102015-05-06 16:33:40 -0700730 if (i % 4 == 3 || i == kpad->var->max_row_num) {
Michael Hennerich3f48e732011-10-18 21:12:39 -0700731 ret |= adp5589_write(client, reg(ADP5585_RPULL_CONFIG_A)
732 + (i >> 2), pull_mask);
733 pull_mask = 0;
734 }
735 }
736
737 for (i = 0; i <= kpad->var->max_col_num; i++) {
738 unsigned val = 0, bit = 1 << (i + kpad->var->col_shift);
739 if (pdata->pullup_en_300k & bit)
740 val = 0;
741 else if (pdata->pulldown_en_300k & bit)
742 val = 1;
743 else if (pdata->pullup_en_100k & bit)
744 val = 2;
745 else if (pdata->pull_dis_mask & bit)
746 val = 3;
747
748 pull_mask |= val << (2 * (i & 0x3));
749
Guido Martínez195e6102015-05-06 16:33:40 -0700750 if (i % 4 == 3 || i == kpad->var->max_col_num) {
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700751 ret |= adp5589_write(client,
Michael Hennerich3f48e732011-10-18 21:12:39 -0700752 reg(ADP5585_RPULL_CONFIG_C) +
753 (i >> 2), pull_mask);
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700754 pull_mask = 0;
755 }
756 }
757
758 if (pdata->reset1_key_1 && pdata->reset1_key_2 && pdata->reset1_key_3) {
Michael Hennerich3f48e732011-10-18 21:12:39 -0700759 ret |= adp5589_write(client, reg(ADP5589_RESET1_EVENT_A),
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700760 adp5589_get_evcode(kpad,
761 pdata->reset1_key_1));
Michael Hennerich3f48e732011-10-18 21:12:39 -0700762 ret |= adp5589_write(client, reg(ADP5589_RESET1_EVENT_B),
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700763 adp5589_get_evcode(kpad,
764 pdata->reset1_key_2));
Michael Hennerich3f48e732011-10-18 21:12:39 -0700765 ret |= adp5589_write(client, reg(ADP5589_RESET1_EVENT_C),
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700766 adp5589_get_evcode(kpad,
767 pdata->reset1_key_3));
768 kpad->extend_cfg |= R4_EXTEND_CFG;
769 }
770
771 if (pdata->reset2_key_1 && pdata->reset2_key_2) {
Michael Hennerich3f48e732011-10-18 21:12:39 -0700772 ret |= adp5589_write(client, reg(ADP5589_RESET2_EVENT_A),
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700773 adp5589_get_evcode(kpad,
774 pdata->reset2_key_1));
Michael Hennerich3f48e732011-10-18 21:12:39 -0700775 ret |= adp5589_write(client, reg(ADP5589_RESET2_EVENT_B),
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700776 adp5589_get_evcode(kpad,
777 pdata->reset2_key_2));
778 kpad->extend_cfg |= C4_EXTEND_CFG;
779 }
780
781 if (kpad->extend_cfg) {
Michael Hennerich3f48e732011-10-18 21:12:39 -0700782 ret |= adp5589_write(client, reg(ADP5589_RESET_CFG),
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700783 pdata->reset_cfg);
Michael Hennerich3f48e732011-10-18 21:12:39 -0700784 ret |= adp5589_write(client, reg(ADP5589_PIN_CONFIG_D),
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700785 kpad->extend_cfg);
786 }
787
Michael Hennerich3f48e732011-10-18 21:12:39 -0700788 ret |= adp5589_write(client, reg(ADP5589_DEBOUNCE_DIS_A),
789 pdata->debounce_dis_mask & kpad->var->row_mask);
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700790
Michael Hennerich3f48e732011-10-18 21:12:39 -0700791 ret |= adp5589_write(client, reg(ADP5589_DEBOUNCE_DIS_B),
792 (pdata->debounce_dis_mask >> kpad->var->col_shift)
793 & kpad->var->col_mask);
794
795 if (!kpad->is_adp5585)
796 ret |= adp5589_write(client, reg(ADP5589_DEBOUNCE_DIS_C),
797 (pdata->debounce_dis_mask >> 16) & 0xFF);
798
799 ret |= adp5589_write(client, reg(ADP5589_POLL_PTIME_CFG),
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700800 pdata->scan_cycle_time & PTIME_MASK);
Michael Hennerich3f48e732011-10-18 21:12:39 -0700801 ret |= adp5589_write(client, ADP5589_5_INT_STATUS,
802 (kpad->is_adp5585 ? 0 : LOGIC2_INT) |
803 LOGIC1_INT | OVRFLOW_INT |
804 (kpad->is_adp5585 ? 0 : LOCK_INT) |
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700805 GPI_INT | EVENT_INT); /* Status is W1C */
806
Michael Hennerich3f48e732011-10-18 21:12:39 -0700807 ret |= adp5589_write(client, reg(ADP5589_GENERAL_CFG),
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700808 INT_CFG | OSC_EN | CORE_CLK(3));
Michael Hennerich3f48e732011-10-18 21:12:39 -0700809 ret |= adp5589_write(client, reg(ADP5589_INT_EN),
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700810 OVRFLOW_IEN | GPI_IEN | EVENT_IEN);
811
812 if (ret < 0) {
813 dev_err(&client->dev, "Write Error\n");
814 return ret;
815 }
816
817 return 0;
818}
819
Bill Pemberton5298cc42012-11-23 21:38:25 -0800820static void adp5589_report_switch_state(struct adp5589_kpad *kpad)
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700821{
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700822 int gpi_stat_tmp, pin_loc;
823 int i;
Michael Hennerich3f48e732011-10-18 21:12:39 -0700824 int gpi_stat1 = adp5589_read(kpad->client,
825 kpad->var->reg(ADP5589_GPI_STATUS_A));
826 int gpi_stat2 = adp5589_read(kpad->client,
827 kpad->var->reg(ADP5589_GPI_STATUS_B));
828 int gpi_stat3 = !kpad->is_adp5585 ?
829 adp5589_read(kpad->client, ADP5589_GPI_STATUS_C) : 0;
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700830
831 for (i = 0; i < kpad->gpimapsize; i++) {
832 unsigned short pin = kpad->gpimap[i].pin;
833
Michael Hennerich3f48e732011-10-18 21:12:39 -0700834 if (pin <= kpad->var->gpi_pin_row_end) {
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700835 gpi_stat_tmp = gpi_stat1;
Michael Hennerich3f48e732011-10-18 21:12:39 -0700836 pin_loc = pin - kpad->var->gpi_pin_row_base;
837 } else if ((pin - kpad->var->gpi_pin_col_base) < 8) {
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700838 gpi_stat_tmp = gpi_stat2;
Michael Hennerich3f48e732011-10-18 21:12:39 -0700839 pin_loc = pin - kpad->var->gpi_pin_col_base;
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700840 } else {
841 gpi_stat_tmp = gpi_stat3;
Michael Hennerich3f48e732011-10-18 21:12:39 -0700842 pin_loc = pin - kpad->var->gpi_pin_col_base - 8;
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700843 }
844
845 if (gpi_stat_tmp < 0) {
846 dev_err(&kpad->client->dev,
Michael Hennerich3f48e732011-10-18 21:12:39 -0700847 "Can't read GPIO_DAT_STAT switch %d, default to OFF\n",
848 pin);
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700849 gpi_stat_tmp = 0;
850 }
851
852 input_report_switch(kpad->input,
853 kpad->gpimap[i].sw_evt,
854 !(gpi_stat_tmp & (1 << pin_loc)));
855 }
856
857 input_sync(kpad->input);
858}
859
Lars-Peter Clausencb3efd52019-10-23 13:52:23 -0700860static int adp5589_keypad_add(struct adp5589_kpad *kpad, unsigned int revid)
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700861{
Lars-Peter Clausencb3efd52019-10-23 13:52:23 -0700862 struct i2c_client *client = kpad->client;
Michael Hennerich3f48e732011-10-18 21:12:39 -0700863 const struct adp5589_kpad_platform_data *pdata =
Jingoo Hanc838cb32013-12-05 19:21:10 -0800864 dev_get_platdata(&client->dev);
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700865 struct input_dev *input;
Lars-Peter Clausencb3efd52019-10-23 13:52:23 -0700866 unsigned int i;
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700867 int error;
868
Michael Hennerich3f48e732011-10-18 21:12:39 -0700869 if (!((pdata->keypad_en_mask & kpad->var->row_mask) &&
870 (pdata->keypad_en_mask >> kpad->var->col_shift)) ||
871 !pdata->keymap) {
872 dev_err(&client->dev, "no rows, cols or keymap from pdata\n");
Lars-Peter Clausencb3efd52019-10-23 13:52:23 -0700873 return -EINVAL;
Michael Hennerich3f48e732011-10-18 21:12:39 -0700874 }
875
876 if (pdata->keymapsize != kpad->var->keymapsize) {
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700877 dev_err(&client->dev, "invalid keymapsize\n");
Lars-Peter Clausencb3efd52019-10-23 13:52:23 -0700878 return -EINVAL;
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700879 }
880
881 if (!pdata->gpimap && pdata->gpimapsize) {
882 dev_err(&client->dev, "invalid gpimap from pdata\n");
Lars-Peter Clausencb3efd52019-10-23 13:52:23 -0700883 return -EINVAL;
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700884 }
885
Michael Hennerich3f48e732011-10-18 21:12:39 -0700886 if (pdata->gpimapsize > kpad->var->gpimapsize_max) {
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700887 dev_err(&client->dev, "invalid gpimapsize\n");
Lars-Peter Clausencb3efd52019-10-23 13:52:23 -0700888 return -EINVAL;
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700889 }
890
891 for (i = 0; i < pdata->gpimapsize; i++) {
892 unsigned short pin = pdata->gpimap[i].pin;
893
Michael Hennerich3f48e732011-10-18 21:12:39 -0700894 if (pin < kpad->var->gpi_pin_base ||
895 pin > kpad->var->gpi_pin_end) {
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700896 dev_err(&client->dev, "invalid gpi pin data\n");
Lars-Peter Clausencb3efd52019-10-23 13:52:23 -0700897 return -EINVAL;
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700898 }
899
Michael Hennerich3f48e732011-10-18 21:12:39 -0700900 if ((1 << (pin - kpad->var->gpi_pin_row_base)) &
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700901 pdata->keypad_en_mask) {
902 dev_err(&client->dev, "invalid gpi row/col data\n");
Lars-Peter Clausencb3efd52019-10-23 13:52:23 -0700903 return -EINVAL;
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700904 }
905 }
906
907 if (!client->irq) {
908 dev_err(&client->dev, "no IRQ?\n");
Lars-Peter Clausencb3efd52019-10-23 13:52:23 -0700909 return -EINVAL;
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700910 }
911
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700912 input = input_allocate_device();
Lars-Peter Clausencb3efd52019-10-23 13:52:23 -0700913 if (!input)
914 return -ENOMEM;
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700915
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700916 kpad->input = input;
917
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700918 input->name = client->name;
919 input->phys = "adp5589-keys/input0";
920 input->dev.parent = &client->dev;
921
922 input_set_drvdata(input, kpad);
923
924 input->id.bustype = BUS_I2C;
925 input->id.vendor = 0x0001;
926 input->id.product = 0x0001;
927 input->id.version = revid;
928
929 input->keycodesize = sizeof(kpad->keycode[0]);
930 input->keycodemax = pdata->keymapsize;
931 input->keycode = kpad->keycode;
932
933 memcpy(kpad->keycode, pdata->keymap,
934 pdata->keymapsize * input->keycodesize);
935
936 kpad->gpimap = pdata->gpimap;
937 kpad->gpimapsize = pdata->gpimapsize;
938
939 /* setup input device */
940 __set_bit(EV_KEY, input->evbit);
941
942 if (pdata->repeat)
943 __set_bit(EV_REP, input->evbit);
944
945 for (i = 0; i < input->keycodemax; i++)
Andrew Liue4cfb032013-11-23 10:06:36 -0800946 if (kpad->keycode[i] <= KEY_MAX)
947 __set_bit(kpad->keycode[i], input->keybit);
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700948 __clear_bit(KEY_RESERVED, input->keybit);
949
950 if (kpad->gpimapsize)
951 __set_bit(EV_SW, input->evbit);
952 for (i = 0; i < kpad->gpimapsize; i++)
953 __set_bit(kpad->gpimap[i].sw_evt, input->swbit);
954
955 error = input_register_device(input);
956 if (error) {
957 dev_err(&client->dev, "unable to register input device\n");
Michael Hennerich3f48e732011-10-18 21:12:39 -0700958 goto err_free_input;
Michael Hennerich9d2e1732011-05-19 00:59:32 -0700959 }
960
961 error = request_threaded_irq(client->irq, NULL, adp5589_irq,
962 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
963 client->dev.driver->name, kpad);
964 if (error) {
965 dev_err(&client->dev, "irq %d busy?\n", client->irq);
966 goto err_unreg_dev;
967 }
968
Lars-Peter Clausencb3efd52019-10-23 13:52:23 -0700969 device_init_wakeup(&client->dev, 1);
970
971 return 0;
972
973err_unreg_dev:
974 input_unregister_device(input);
975 input = NULL;
976err_free_input:
977 input_free_device(input);
978
979 return error;
980}
981
982static void adp5589_keypad_remove(struct adp5589_kpad *kpad)
983{
984 if (kpad->input) {
985 free_irq(kpad->client->irq, kpad);
986 input_unregister_device(kpad->input);
987 }
988}
989
990static int adp5589_probe(struct i2c_client *client,
991 const struct i2c_device_id *id)
992{
993 struct adp5589_kpad *kpad;
994 const struct adp5589_kpad_platform_data *pdata =
995 dev_get_platdata(&client->dev);
996 unsigned int revid;
997 int error, ret;
998
999 if (!i2c_check_functionality(client->adapter,
1000 I2C_FUNC_SMBUS_BYTE_DATA)) {
1001 dev_err(&client->dev, "SMBUS Byte Data not Supported\n");
1002 return -EIO;
1003 }
1004
1005 if (!pdata) {
1006 dev_err(&client->dev, "no platform data?\n");
1007 return -EINVAL;
1008 }
1009
Alexandru Ardelean41d7d262020-11-18 22:40:22 -08001010 kpad = devm_kzalloc(&client->dev, sizeof(*kpad), GFP_KERNEL);
Lars-Peter Clausencb3efd52019-10-23 13:52:23 -07001011 if (!kpad)
1012 return -ENOMEM;
1013
1014 kpad->client = client;
1015
1016 switch (id->driver_data) {
1017 case ADP5585_02:
1018 kpad->support_row5 = true;
Gustavo A. R. Silva6f49c4f2020-07-07 11:24:55 -07001019 fallthrough;
Lars-Peter Clausencb3efd52019-10-23 13:52:23 -07001020 case ADP5585_01:
1021 kpad->is_adp5585 = true;
1022 kpad->var = &const_adp5585;
1023 break;
1024 case ADP5589:
1025 kpad->support_row5 = true;
1026 kpad->var = &const_adp5589;
1027 break;
1028 }
1029
1030 ret = adp5589_read(client, ADP5589_5_ID);
Alexandru Ardelean41d7d262020-11-18 22:40:22 -08001031 if (ret < 0)
1032 return ret;
Lars-Peter Clausencb3efd52019-10-23 13:52:23 -07001033
1034 revid = (u8) ret & ADP5589_5_DEVICE_ID_MASK;
1035
1036 if (pdata->keymapsize) {
1037 error = adp5589_keypad_add(kpad, revid);
1038 if (error)
Alexandru Ardelean41d7d262020-11-18 22:40:22 -08001039 return error;
Lars-Peter Clausencb3efd52019-10-23 13:52:23 -07001040 }
1041
Michael Hennerich9d2e1732011-05-19 00:59:32 -07001042 error = adp5589_setup(kpad);
1043 if (error)
Lars-Peter Clausencb3efd52019-10-23 13:52:23 -07001044 goto err_keypad_remove;
Michael Hennerich9d2e1732011-05-19 00:59:32 -07001045
1046 if (kpad->gpimapsize)
1047 adp5589_report_switch_state(kpad);
1048
1049 error = adp5589_gpio_add(kpad);
1050 if (error)
Lars-Peter Clausencb3efd52019-10-23 13:52:23 -07001051 goto err_keypad_remove;
Michael Hennerich9d2e1732011-05-19 00:59:32 -07001052
Michael Hennerich9d2e1732011-05-19 00:59:32 -07001053 i2c_set_clientdata(client, kpad);
1054
1055 dev_info(&client->dev, "Rev.%d keypad, irq %d\n", revid, client->irq);
1056 return 0;
1057
Lars-Peter Clausencb3efd52019-10-23 13:52:23 -07001058err_keypad_remove:
1059 adp5589_keypad_remove(kpad);
Michael Hennerich9d2e1732011-05-19 00:59:32 -07001060
1061 return error;
1062}
1063
Bill Pembertone2619cf2012-11-23 21:50:47 -08001064static int adp5589_remove(struct i2c_client *client)
Michael Hennerich9d2e1732011-05-19 00:59:32 -07001065{
1066 struct adp5589_kpad *kpad = i2c_get_clientdata(client);
1067
Michael Hennerich3f48e732011-10-18 21:12:39 -07001068 adp5589_write(client, kpad->var->reg(ADP5589_GENERAL_CFG), 0);
Lars-Peter Clausencb3efd52019-10-23 13:52:23 -07001069 adp5589_keypad_remove(kpad);
Michael Hennerich9d2e1732011-05-19 00:59:32 -07001070 adp5589_gpio_remove(kpad);
Michael Hennerich9d2e1732011-05-19 00:59:32 -07001071
1072 return 0;
1073}
1074
1075#ifdef CONFIG_PM_SLEEP
1076static int adp5589_suspend(struct device *dev)
1077{
1078 struct adp5589_kpad *kpad = dev_get_drvdata(dev);
1079 struct i2c_client *client = kpad->client;
1080
Lars-Peter Clausencb3efd52019-10-23 13:52:23 -07001081 if (!kpad->input)
1082 return 0;
1083
Michael Hennerich9d2e1732011-05-19 00:59:32 -07001084 disable_irq(client->irq);
1085
1086 if (device_may_wakeup(&client->dev))
1087 enable_irq_wake(client->irq);
1088
1089 return 0;
1090}
1091
1092static int adp5589_resume(struct device *dev)
1093{
1094 struct adp5589_kpad *kpad = dev_get_drvdata(dev);
1095 struct i2c_client *client = kpad->client;
1096
Lars-Peter Clausencb3efd52019-10-23 13:52:23 -07001097 if (!kpad->input)
1098 return 0;
1099
Michael Hennerich9d2e1732011-05-19 00:59:32 -07001100 if (device_may_wakeup(&client->dev))
1101 disable_irq_wake(client->irq);
1102
1103 enable_irq(client->irq);
1104
1105 return 0;
1106}
1107#endif
1108
1109static SIMPLE_DEV_PM_OPS(adp5589_dev_pm_ops, adp5589_suspend, adp5589_resume);
1110
1111static const struct i2c_device_id adp5589_id[] = {
Michael Hennerich3f48e732011-10-18 21:12:39 -07001112 {"adp5589-keys", ADP5589},
1113 {"adp5585-keys", ADP5585_01},
1114 {"adp5585-02-keys", ADP5585_02}, /* Adds ROW5 to ADP5585 */
Michael Hennerich9d2e1732011-05-19 00:59:32 -07001115 {}
1116};
1117
1118MODULE_DEVICE_TABLE(i2c, adp5589_id);
1119
1120static struct i2c_driver adp5589_driver = {
1121 .driver = {
1122 .name = KBUILD_MODNAME,
Michael Hennerich9d2e1732011-05-19 00:59:32 -07001123 .pm = &adp5589_dev_pm_ops,
1124 },
1125 .probe = adp5589_probe,
Bill Pemberton1cb0aa82012-11-23 21:27:39 -08001126 .remove = adp5589_remove,
Michael Hennerich9d2e1732011-05-19 00:59:32 -07001127 .id_table = adp5589_id,
1128};
1129
Axel Lin1b92c1c2012-03-16 23:05:41 -07001130module_i2c_driver(adp5589_driver);
Michael Hennerich9d2e1732011-05-19 00:59:32 -07001131
1132MODULE_LICENSE("GPL");
1133MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
Michael Hennerich3f48e732011-10-18 21:12:39 -07001134MODULE_DESCRIPTION("ADP5589/ADP5585 Keypad driver");