Michael Hennerich | 88751dd | 2009-09-17 22:39:38 -0700 | [diff] [blame] | 1 | /* |
| 2 | * File: drivers/input/keyboard/adp5588_keys.c |
Michael Hennerich | 5a9003d | 2010-01-19 00:28:44 -0800 | [diff] [blame] | 3 | * Description: keypad driver for ADP5588 and ADP5587 |
| 4 | * I2C QWERTY Keypad and IO Expander |
Michael Hennerich | 88751dd | 2009-09-17 22:39:38 -0700 | [diff] [blame] | 5 | * Bugs: Enter bugs at http://blackfin.uclinux.org/ |
| 6 | * |
Michael Hennerich | 95716c0 | 2010-11-02 11:33:05 -0700 | [diff] [blame] | 7 | * Copyright (C) 2008-2010 Analog Devices Inc. |
Michael Hennerich | 88751dd | 2009-09-17 22:39:38 -0700 | [diff] [blame] | 8 | * Licensed under the GPL-2 or later. |
| 9 | */ |
| 10 | |
| 11 | #include <linux/module.h> |
Michael Hennerich | 88751dd | 2009-09-17 22:39:38 -0700 | [diff] [blame] | 12 | #include <linux/interrupt.h> |
| 13 | #include <linux/irq.h> |
| 14 | #include <linux/workqueue.h> |
| 15 | #include <linux/errno.h> |
| 16 | #include <linux/pm.h> |
| 17 | #include <linux/platform_device.h> |
| 18 | #include <linux/input.h> |
| 19 | #include <linux/i2c.h> |
Xiaolong Chen | ba9f507 | 2010-07-26 01:01:11 -0700 | [diff] [blame] | 20 | #include <linux/gpio.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 21 | #include <linux/slab.h> |
Michael Hennerich | 88751dd | 2009-09-17 22:39:38 -0700 | [diff] [blame] | 22 | |
| 23 | #include <linux/i2c/adp5588.h> |
| 24 | |
Michael Hennerich | 88751dd | 2009-09-17 22:39:38 -0700 | [diff] [blame] | 25 | /* Key Event Register xy */ |
| 26 | #define KEY_EV_PRESSED (1 << 7) |
| 27 | #define KEY_EV_MASK (0x7F) |
| 28 | |
| 29 | #define KP_SEL(x) (0xFFFF >> (16 - x)) /* 2^x-1 */ |
| 30 | |
| 31 | #define KEYP_MAX_EVENT 10 |
| 32 | |
| 33 | /* |
| 34 | * Early pre 4.0 Silicon required to delay readout by at least 25ms, |
| 35 | * since the Event Counter Register updated 25ms after the interrupt |
| 36 | * asserted. |
| 37 | */ |
| 38 | #define WA_DELAYED_READOUT_REVID(rev) ((rev) < 4) |
| 39 | |
| 40 | struct adp5588_kpad { |
| 41 | struct i2c_client *client; |
| 42 | struct input_dev *input; |
| 43 | struct delayed_work work; |
| 44 | unsigned long delay; |
| 45 | unsigned short keycode[ADP5588_KEYMAPSIZE]; |
Xiaolong CHEN | 69a4af6 | 2010-06-24 19:10:40 -0700 | [diff] [blame] | 46 | const struct adp5588_gpi_map *gpimap; |
| 47 | unsigned short gpimapsize; |
Xiaolong Chen | ba9f507 | 2010-07-26 01:01:11 -0700 | [diff] [blame] | 48 | #ifdef CONFIG_GPIOLIB |
Michael Hennerich | 95716c0 | 2010-11-02 11:33:05 -0700 | [diff] [blame] | 49 | unsigned char gpiomap[ADP5588_MAXGPIO]; |
Xiaolong Chen | ba9f507 | 2010-07-26 01:01:11 -0700 | [diff] [blame] | 50 | bool export_gpio; |
| 51 | struct gpio_chip gc; |
| 52 | struct mutex gpio_lock; /* Protect cached dir, dat_out */ |
| 53 | u8 dat_out[3]; |
| 54 | u8 dir[3]; |
| 55 | #endif |
Michael Hennerich | 88751dd | 2009-09-17 22:39:38 -0700 | [diff] [blame] | 56 | }; |
| 57 | |
| 58 | static int adp5588_read(struct i2c_client *client, u8 reg) |
| 59 | { |
| 60 | int ret = i2c_smbus_read_byte_data(client, reg); |
| 61 | |
| 62 | if (ret < 0) |
| 63 | dev_err(&client->dev, "Read Error\n"); |
| 64 | |
| 65 | return ret; |
| 66 | } |
| 67 | |
| 68 | static int adp5588_write(struct i2c_client *client, u8 reg, u8 val) |
| 69 | { |
| 70 | return i2c_smbus_write_byte_data(client, reg, val); |
| 71 | } |
| 72 | |
Xiaolong Chen | ba9f507 | 2010-07-26 01:01:11 -0700 | [diff] [blame] | 73 | #ifdef CONFIG_GPIOLIB |
| 74 | static int adp5588_gpio_get_value(struct gpio_chip *chip, unsigned off) |
| 75 | { |
| 76 | struct adp5588_kpad *kpad = container_of(chip, struct adp5588_kpad, gc); |
Michael Hennerich | 95716c0 | 2010-11-02 11:33:05 -0700 | [diff] [blame] | 77 | unsigned int bank = ADP5588_BANK(kpad->gpiomap[off]); |
| 78 | unsigned int bit = ADP5588_BIT(kpad->gpiomap[off]); |
Jean-Francois Dagenais | 910a9f5 | 2014-02-12 15:14:28 -0800 | [diff] [blame] | 79 | int val; |
Xiaolong Chen | ba9f507 | 2010-07-26 01:01:11 -0700 | [diff] [blame] | 80 | |
Jean-Francois Dagenais | 910a9f5 | 2014-02-12 15:14:28 -0800 | [diff] [blame] | 81 | mutex_lock(&kpad->gpio_lock); |
| 82 | |
| 83 | if (kpad->dir[bank] & bit) |
| 84 | val = kpad->dat_out[bank]; |
| 85 | else |
| 86 | val = adp5588_read(kpad->client, GPIO_DAT_STAT1 + bank); |
| 87 | |
| 88 | mutex_unlock(&kpad->gpio_lock); |
| 89 | |
| 90 | return !!(val & bit); |
Xiaolong Chen | ba9f507 | 2010-07-26 01:01:11 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | static void adp5588_gpio_set_value(struct gpio_chip *chip, |
| 94 | unsigned off, int val) |
| 95 | { |
| 96 | struct adp5588_kpad *kpad = container_of(chip, struct adp5588_kpad, gc); |
Michael Hennerich | 95716c0 | 2010-11-02 11:33:05 -0700 | [diff] [blame] | 97 | unsigned int bank = ADP5588_BANK(kpad->gpiomap[off]); |
| 98 | unsigned int bit = ADP5588_BIT(kpad->gpiomap[off]); |
Xiaolong Chen | ba9f507 | 2010-07-26 01:01:11 -0700 | [diff] [blame] | 99 | |
| 100 | mutex_lock(&kpad->gpio_lock); |
| 101 | |
| 102 | if (val) |
| 103 | kpad->dat_out[bank] |= bit; |
| 104 | else |
| 105 | kpad->dat_out[bank] &= ~bit; |
| 106 | |
| 107 | adp5588_write(kpad->client, GPIO_DAT_OUT1 + bank, |
| 108 | kpad->dat_out[bank]); |
| 109 | |
| 110 | mutex_unlock(&kpad->gpio_lock); |
| 111 | } |
| 112 | |
| 113 | static int adp5588_gpio_direction_input(struct gpio_chip *chip, unsigned off) |
| 114 | { |
| 115 | struct adp5588_kpad *kpad = container_of(chip, struct adp5588_kpad, gc); |
Michael Hennerich | 95716c0 | 2010-11-02 11:33:05 -0700 | [diff] [blame] | 116 | unsigned int bank = ADP5588_BANK(kpad->gpiomap[off]); |
| 117 | unsigned int bit = ADP5588_BIT(kpad->gpiomap[off]); |
Xiaolong Chen | ba9f507 | 2010-07-26 01:01:11 -0700 | [diff] [blame] | 118 | int ret; |
| 119 | |
| 120 | mutex_lock(&kpad->gpio_lock); |
| 121 | |
| 122 | kpad->dir[bank] &= ~bit; |
| 123 | ret = adp5588_write(kpad->client, GPIO_DIR1 + bank, kpad->dir[bank]); |
| 124 | |
| 125 | mutex_unlock(&kpad->gpio_lock); |
| 126 | |
| 127 | return ret; |
| 128 | } |
| 129 | |
| 130 | static int adp5588_gpio_direction_output(struct gpio_chip *chip, |
| 131 | unsigned off, int val) |
| 132 | { |
| 133 | struct adp5588_kpad *kpad = container_of(chip, struct adp5588_kpad, gc); |
Michael Hennerich | 95716c0 | 2010-11-02 11:33:05 -0700 | [diff] [blame] | 134 | unsigned int bank = ADP5588_BANK(kpad->gpiomap[off]); |
| 135 | unsigned int bit = ADP5588_BIT(kpad->gpiomap[off]); |
Xiaolong Chen | ba9f507 | 2010-07-26 01:01:11 -0700 | [diff] [blame] | 136 | int ret; |
| 137 | |
| 138 | mutex_lock(&kpad->gpio_lock); |
| 139 | |
| 140 | kpad->dir[bank] |= bit; |
| 141 | |
| 142 | if (val) |
| 143 | kpad->dat_out[bank] |= bit; |
| 144 | else |
| 145 | kpad->dat_out[bank] &= ~bit; |
| 146 | |
| 147 | ret = adp5588_write(kpad->client, GPIO_DAT_OUT1 + bank, |
| 148 | kpad->dat_out[bank]); |
| 149 | ret |= adp5588_write(kpad->client, GPIO_DIR1 + bank, |
| 150 | kpad->dir[bank]); |
| 151 | |
| 152 | mutex_unlock(&kpad->gpio_lock); |
| 153 | |
| 154 | return ret; |
| 155 | } |
| 156 | |
Bill Pemberton | 5298cc4 | 2012-11-23 21:38:25 -0800 | [diff] [blame] | 157 | static int adp5588_build_gpiomap(struct adp5588_kpad *kpad, |
Dmitry Torokhov | 0d87c72 | 2010-08-02 18:33:26 -0700 | [diff] [blame] | 158 | const struct adp5588_kpad_platform_data *pdata) |
Xiaolong Chen | ba9f507 | 2010-07-26 01:01:11 -0700 | [diff] [blame] | 159 | { |
Michael Hennerich | 95716c0 | 2010-11-02 11:33:05 -0700 | [diff] [blame] | 160 | bool pin_used[ADP5588_MAXGPIO]; |
Dmitry Torokhov | 0d87c72 | 2010-08-02 18:33:26 -0700 | [diff] [blame] | 161 | int n_unused = 0; |
| 162 | int i; |
| 163 | |
| 164 | memset(pin_used, 0, sizeof(pin_used)); |
| 165 | |
| 166 | for (i = 0; i < pdata->rows; i++) |
| 167 | pin_used[i] = true; |
| 168 | |
| 169 | for (i = 0; i < pdata->cols; i++) |
| 170 | pin_used[i + GPI_PIN_COL_BASE - GPI_PIN_BASE] = true; |
| 171 | |
| 172 | for (i = 0; i < kpad->gpimapsize; i++) |
| 173 | pin_used[kpad->gpimap[i].pin - GPI_PIN_BASE] = true; |
| 174 | |
Michael Hennerich | 95716c0 | 2010-11-02 11:33:05 -0700 | [diff] [blame] | 175 | for (i = 0; i < ADP5588_MAXGPIO; i++) |
Dmitry Torokhov | 0d87c72 | 2010-08-02 18:33:26 -0700 | [diff] [blame] | 176 | if (!pin_used[i]) |
| 177 | kpad->gpiomap[n_unused++] = i; |
| 178 | |
| 179 | return n_unused; |
| 180 | } |
| 181 | |
Bill Pemberton | 5298cc4 | 2012-11-23 21:38:25 -0800 | [diff] [blame] | 182 | static int adp5588_gpio_add(struct adp5588_kpad *kpad) |
Dmitry Torokhov | 0d87c72 | 2010-08-02 18:33:26 -0700 | [diff] [blame] | 183 | { |
| 184 | struct device *dev = &kpad->client->dev; |
Jingoo Han | c838cb3 | 2013-12-05 19:21:10 -0800 | [diff] [blame] | 185 | const struct adp5588_kpad_platform_data *pdata = dev_get_platdata(dev); |
Xiaolong Chen | ba9f507 | 2010-07-26 01:01:11 -0700 | [diff] [blame] | 186 | const struct adp5588_gpio_platform_data *gpio_data = pdata->gpio_data; |
| 187 | int i, error; |
| 188 | |
Dmitry Torokhov | 0d87c72 | 2010-08-02 18:33:26 -0700 | [diff] [blame] | 189 | if (!gpio_data) |
| 190 | return 0; |
Xiaolong Chen | ba9f507 | 2010-07-26 01:01:11 -0700 | [diff] [blame] | 191 | |
Dmitry Torokhov | 0d87c72 | 2010-08-02 18:33:26 -0700 | [diff] [blame] | 192 | kpad->gc.ngpio = adp5588_build_gpiomap(kpad, pdata); |
| 193 | if (kpad->gc.ngpio == 0) { |
Xiaolong Chen | ba9f507 | 2010-07-26 01:01:11 -0700 | [diff] [blame] | 194 | dev_info(dev, "No unused gpios left to export\n"); |
| 195 | return 0; |
| 196 | } |
| 197 | |
Dmitry Torokhov | 0d87c72 | 2010-08-02 18:33:26 -0700 | [diff] [blame] | 198 | kpad->export_gpio = true; |
| 199 | |
Xiaolong Chen | ba9f507 | 2010-07-26 01:01:11 -0700 | [diff] [blame] | 200 | kpad->gc.direction_input = adp5588_gpio_direction_input; |
| 201 | kpad->gc.direction_output = adp5588_gpio_direction_output; |
| 202 | kpad->gc.get = adp5588_gpio_get_value; |
| 203 | kpad->gc.set = adp5588_gpio_set_value; |
| 204 | kpad->gc.can_sleep = 1; |
| 205 | |
| 206 | kpad->gc.base = gpio_data->gpio_start; |
| 207 | kpad->gc.label = kpad->client->name; |
| 208 | kpad->gc.owner = THIS_MODULE; |
Jean-François Dagenais | d0a3457 | 2012-05-10 22:32:00 -0700 | [diff] [blame] | 209 | kpad->gc.names = gpio_data->names; |
Xiaolong Chen | ba9f507 | 2010-07-26 01:01:11 -0700 | [diff] [blame] | 210 | |
| 211 | mutex_init(&kpad->gpio_lock); |
| 212 | |
| 213 | error = gpiochip_add(&kpad->gc); |
| 214 | if (error) { |
| 215 | dev_err(dev, "gpiochip_add failed, err: %d\n", error); |
| 216 | return error; |
| 217 | } |
| 218 | |
Michael Hennerich | 95716c0 | 2010-11-02 11:33:05 -0700 | [diff] [blame] | 219 | for (i = 0; i <= ADP5588_BANK(ADP5588_MAXGPIO); i++) { |
Xiaolong Chen | ba9f507 | 2010-07-26 01:01:11 -0700 | [diff] [blame] | 220 | kpad->dat_out[i] = adp5588_read(kpad->client, |
| 221 | GPIO_DAT_OUT1 + i); |
| 222 | kpad->dir[i] = adp5588_read(kpad->client, GPIO_DIR1 + i); |
| 223 | } |
| 224 | |
| 225 | if (gpio_data->setup) { |
| 226 | error = gpio_data->setup(kpad->client, |
| 227 | kpad->gc.base, kpad->gc.ngpio, |
| 228 | gpio_data->context); |
| 229 | if (error) |
| 230 | dev_warn(dev, "setup failed, %d\n", error); |
| 231 | } |
| 232 | |
| 233 | return 0; |
| 234 | } |
| 235 | |
Bill Pemberton | e2619cf | 2012-11-23 21:50:47 -0800 | [diff] [blame] | 236 | static void adp5588_gpio_remove(struct adp5588_kpad *kpad) |
Xiaolong Chen | ba9f507 | 2010-07-26 01:01:11 -0700 | [diff] [blame] | 237 | { |
Dmitry Torokhov | 0d87c72 | 2010-08-02 18:33:26 -0700 | [diff] [blame] | 238 | struct device *dev = &kpad->client->dev; |
Jingoo Han | c838cb3 | 2013-12-05 19:21:10 -0800 | [diff] [blame] | 239 | const struct adp5588_kpad_platform_data *pdata = dev_get_platdata(dev); |
Xiaolong Chen | ba9f507 | 2010-07-26 01:01:11 -0700 | [diff] [blame] | 240 | const struct adp5588_gpio_platform_data *gpio_data = pdata->gpio_data; |
| 241 | int error; |
| 242 | |
| 243 | if (!kpad->export_gpio) |
| 244 | return; |
| 245 | |
| 246 | if (gpio_data->teardown) { |
| 247 | error = gpio_data->teardown(kpad->client, |
| 248 | kpad->gc.base, kpad->gc.ngpio, |
| 249 | gpio_data->context); |
| 250 | if (error) |
| 251 | dev_warn(dev, "teardown failed %d\n", error); |
| 252 | } |
| 253 | |
abdoulaye berthe | 88d5e52 | 2014-07-12 22:30:14 +0200 | [diff] [blame] | 254 | gpiochip_remove(&kpad->gc); |
Xiaolong Chen | ba9f507 | 2010-07-26 01:01:11 -0700 | [diff] [blame] | 255 | } |
| 256 | #else |
Dmitry Torokhov | 0d87c72 | 2010-08-02 18:33:26 -0700 | [diff] [blame] | 257 | static inline int adp5588_gpio_add(struct adp5588_kpad *kpad) |
Xiaolong Chen | ba9f507 | 2010-07-26 01:01:11 -0700 | [diff] [blame] | 258 | { |
| 259 | return 0; |
| 260 | } |
| 261 | |
Dmitry Torokhov | 0d87c72 | 2010-08-02 18:33:26 -0700 | [diff] [blame] | 262 | static inline void adp5588_gpio_remove(struct adp5588_kpad *kpad) |
Xiaolong Chen | ba9f507 | 2010-07-26 01:01:11 -0700 | [diff] [blame] | 263 | { |
| 264 | } |
| 265 | #endif |
| 266 | |
Xiaolong CHEN | 69a4af6 | 2010-06-24 19:10:40 -0700 | [diff] [blame] | 267 | static void adp5588_report_events(struct adp5588_kpad *kpad, int ev_cnt) |
| 268 | { |
| 269 | int i, j; |
| 270 | |
| 271 | for (i = 0; i < ev_cnt; i++) { |
| 272 | int key = adp5588_read(kpad->client, Key_EVENTA + i); |
| 273 | int key_val = key & KEY_EV_MASK; |
| 274 | |
| 275 | if (key_val >= GPI_PIN_BASE && key_val <= GPI_PIN_END) { |
| 276 | for (j = 0; j < kpad->gpimapsize; j++) { |
| 277 | if (key_val == kpad->gpimap[j].pin) { |
| 278 | input_report_switch(kpad->input, |
| 279 | kpad->gpimap[j].sw_evt, |
| 280 | key & KEY_EV_PRESSED); |
| 281 | break; |
| 282 | } |
| 283 | } |
| 284 | } else { |
| 285 | input_report_key(kpad->input, |
| 286 | kpad->keycode[key_val - 1], |
| 287 | key & KEY_EV_PRESSED); |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | |
Michael Hennerich | 88751dd | 2009-09-17 22:39:38 -0700 | [diff] [blame] | 292 | static void adp5588_work(struct work_struct *work) |
| 293 | { |
| 294 | struct adp5588_kpad *kpad = container_of(work, |
| 295 | struct adp5588_kpad, work.work); |
| 296 | struct i2c_client *client = kpad->client; |
Xiaolong CHEN | 69a4af6 | 2010-06-24 19:10:40 -0700 | [diff] [blame] | 297 | int status, ev_cnt; |
Michael Hennerich | 88751dd | 2009-09-17 22:39:38 -0700 | [diff] [blame] | 298 | |
| 299 | status = adp5588_read(client, INT_STAT); |
| 300 | |
Michael Hennerich | 95716c0 | 2010-11-02 11:33:05 -0700 | [diff] [blame] | 301 | if (status & ADP5588_OVR_FLOW_INT) /* Unlikely and should never happen */ |
Michael Hennerich | 88751dd | 2009-09-17 22:39:38 -0700 | [diff] [blame] | 302 | dev_err(&client->dev, "Event Overflow Error\n"); |
| 303 | |
Michael Hennerich | 95716c0 | 2010-11-02 11:33:05 -0700 | [diff] [blame] | 304 | if (status & ADP5588_KE_INT) { |
| 305 | ev_cnt = adp5588_read(client, KEY_LCK_EC_STAT) & ADP5588_KEC; |
Michael Hennerich | 88751dd | 2009-09-17 22:39:38 -0700 | [diff] [blame] | 306 | if (ev_cnt) { |
Xiaolong CHEN | 69a4af6 | 2010-06-24 19:10:40 -0700 | [diff] [blame] | 307 | adp5588_report_events(kpad, ev_cnt); |
Michael Hennerich | 88751dd | 2009-09-17 22:39:38 -0700 | [diff] [blame] | 308 | input_sync(kpad->input); |
| 309 | } |
| 310 | } |
| 311 | adp5588_write(client, INT_STAT, status); /* Status is W1C */ |
| 312 | } |
| 313 | |
| 314 | static irqreturn_t adp5588_irq(int irq, void *handle) |
| 315 | { |
| 316 | struct adp5588_kpad *kpad = handle; |
| 317 | |
| 318 | /* |
| 319 | * use keventd context to read the event fifo registers |
| 320 | * Schedule readout at least 25ms after notification for |
| 321 | * REVID < 4 |
| 322 | */ |
| 323 | |
| 324 | schedule_delayed_work(&kpad->work, kpad->delay); |
| 325 | |
| 326 | return IRQ_HANDLED; |
| 327 | } |
| 328 | |
Bill Pemberton | 5298cc4 | 2012-11-23 21:38:25 -0800 | [diff] [blame] | 329 | static int adp5588_setup(struct i2c_client *client) |
Michael Hennerich | 88751dd | 2009-09-17 22:39:38 -0700 | [diff] [blame] | 330 | { |
Jingoo Han | c838cb3 | 2013-12-05 19:21:10 -0800 | [diff] [blame] | 331 | const struct adp5588_kpad_platform_data *pdata = |
| 332 | dev_get_platdata(&client->dev); |
Xiaolong Chen | ba9f507 | 2010-07-26 01:01:11 -0700 | [diff] [blame] | 333 | const struct adp5588_gpio_platform_data *gpio_data = pdata->gpio_data; |
Michael Hennerich | 88751dd | 2009-09-17 22:39:38 -0700 | [diff] [blame] | 334 | int i, ret; |
Xiaolong CHEN | 69a4af6 | 2010-06-24 19:10:40 -0700 | [diff] [blame] | 335 | unsigned char evt_mode1 = 0, evt_mode2 = 0, evt_mode3 = 0; |
Michael Hennerich | 88751dd | 2009-09-17 22:39:38 -0700 | [diff] [blame] | 336 | |
| 337 | ret = adp5588_write(client, KP_GPIO1, KP_SEL(pdata->rows)); |
| 338 | ret |= adp5588_write(client, KP_GPIO2, KP_SEL(pdata->cols) & 0xFF); |
| 339 | ret |= adp5588_write(client, KP_GPIO3, KP_SEL(pdata->cols) >> 8); |
| 340 | |
| 341 | if (pdata->en_keylock) { |
| 342 | ret |= adp5588_write(client, UNLOCK1, pdata->unlock_key1); |
| 343 | ret |= adp5588_write(client, UNLOCK2, pdata->unlock_key2); |
Michael Hennerich | 95716c0 | 2010-11-02 11:33:05 -0700 | [diff] [blame] | 344 | ret |= adp5588_write(client, KEY_LCK_EC_STAT, ADP5588_K_LCK_EN); |
Michael Hennerich | 88751dd | 2009-09-17 22:39:38 -0700 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | for (i = 0; i < KEYP_MAX_EVENT; i++) |
| 348 | ret |= adp5588_read(client, Key_EVENTA); |
| 349 | |
Xiaolong CHEN | 69a4af6 | 2010-06-24 19:10:40 -0700 | [diff] [blame] | 350 | for (i = 0; i < pdata->gpimapsize; i++) { |
| 351 | unsigned short pin = pdata->gpimap[i].pin; |
| 352 | |
| 353 | if (pin <= GPI_PIN_ROW_END) { |
| 354 | evt_mode1 |= (1 << (pin - GPI_PIN_ROW_BASE)); |
| 355 | } else { |
| 356 | evt_mode2 |= ((1 << (pin - GPI_PIN_COL_BASE)) & 0xFF); |
| 357 | evt_mode3 |= ((1 << (pin - GPI_PIN_COL_BASE)) >> 8); |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | if (pdata->gpimapsize) { |
| 362 | ret |= adp5588_write(client, GPI_EM1, evt_mode1); |
| 363 | ret |= adp5588_write(client, GPI_EM2, evt_mode2); |
| 364 | ret |= adp5588_write(client, GPI_EM3, evt_mode3); |
| 365 | } |
| 366 | |
Xiaolong Chen | ba9f507 | 2010-07-26 01:01:11 -0700 | [diff] [blame] | 367 | if (gpio_data) { |
Michael Hennerich | 95716c0 | 2010-11-02 11:33:05 -0700 | [diff] [blame] | 368 | for (i = 0; i <= ADP5588_BANK(ADP5588_MAXGPIO); i++) { |
Xiaolong Chen | ba9f507 | 2010-07-26 01:01:11 -0700 | [diff] [blame] | 369 | int pull_mask = gpio_data->pullup_dis_mask; |
| 370 | |
| 371 | ret |= adp5588_write(client, GPIO_PULL1 + i, |
| 372 | (pull_mask >> (8 * i)) & 0xFF); |
| 373 | } |
| 374 | } |
| 375 | |
Michael Hennerich | 95716c0 | 2010-11-02 11:33:05 -0700 | [diff] [blame] | 376 | ret |= adp5588_write(client, INT_STAT, |
| 377 | ADP5588_CMP2_INT | ADP5588_CMP1_INT | |
| 378 | ADP5588_OVR_FLOW_INT | ADP5588_K_LCK_INT | |
| 379 | ADP5588_GPI_INT | ADP5588_KE_INT); /* Status is W1C */ |
Michael Hennerich | 88751dd | 2009-09-17 22:39:38 -0700 | [diff] [blame] | 380 | |
Michael Hennerich | 95716c0 | 2010-11-02 11:33:05 -0700 | [diff] [blame] | 381 | ret |= adp5588_write(client, CFG, ADP5588_INT_CFG | |
| 382 | ADP5588_OVR_FLOW_IEN | |
| 383 | ADP5588_KE_IEN); |
Michael Hennerich | 88751dd | 2009-09-17 22:39:38 -0700 | [diff] [blame] | 384 | |
| 385 | if (ret < 0) { |
| 386 | dev_err(&client->dev, "Write Error\n"); |
| 387 | return ret; |
| 388 | } |
| 389 | |
| 390 | return 0; |
| 391 | } |
| 392 | |
Bill Pemberton | 5298cc4 | 2012-11-23 21:38:25 -0800 | [diff] [blame] | 393 | static void adp5588_report_switch_state(struct adp5588_kpad *kpad) |
Xiaolong CHEN | 69a4af6 | 2010-06-24 19:10:40 -0700 | [diff] [blame] | 394 | { |
| 395 | int gpi_stat1 = adp5588_read(kpad->client, GPIO_DAT_STAT1); |
| 396 | int gpi_stat2 = adp5588_read(kpad->client, GPIO_DAT_STAT2); |
| 397 | int gpi_stat3 = adp5588_read(kpad->client, GPIO_DAT_STAT3); |
| 398 | int gpi_stat_tmp, pin_loc; |
| 399 | int i; |
| 400 | |
| 401 | for (i = 0; i < kpad->gpimapsize; i++) { |
| 402 | unsigned short pin = kpad->gpimap[i].pin; |
| 403 | |
| 404 | if (pin <= GPI_PIN_ROW_END) { |
| 405 | gpi_stat_tmp = gpi_stat1; |
| 406 | pin_loc = pin - GPI_PIN_ROW_BASE; |
| 407 | } else if ((pin - GPI_PIN_COL_BASE) < 8) { |
| 408 | gpi_stat_tmp = gpi_stat2; |
| 409 | pin_loc = pin - GPI_PIN_COL_BASE; |
| 410 | } else { |
| 411 | gpi_stat_tmp = gpi_stat3; |
| 412 | pin_loc = pin - GPI_PIN_COL_BASE - 8; |
| 413 | } |
| 414 | |
| 415 | if (gpi_stat_tmp < 0) { |
| 416 | dev_err(&kpad->client->dev, |
| 417 | "Can't read GPIO_DAT_STAT switch %d default to OFF\n", |
| 418 | pin); |
| 419 | gpi_stat_tmp = 0; |
| 420 | } |
| 421 | |
| 422 | input_report_switch(kpad->input, |
| 423 | kpad->gpimap[i].sw_evt, |
| 424 | !(gpi_stat_tmp & (1 << pin_loc))); |
| 425 | } |
| 426 | |
| 427 | input_sync(kpad->input); |
| 428 | } |
| 429 | |
| 430 | |
Bill Pemberton | 5298cc4 | 2012-11-23 21:38:25 -0800 | [diff] [blame] | 431 | static int adp5588_probe(struct i2c_client *client, |
| 432 | const struct i2c_device_id *id) |
Michael Hennerich | 88751dd | 2009-09-17 22:39:38 -0700 | [diff] [blame] | 433 | { |
| 434 | struct adp5588_kpad *kpad; |
Jingoo Han | c838cb3 | 2013-12-05 19:21:10 -0800 | [diff] [blame] | 435 | const struct adp5588_kpad_platform_data *pdata = |
| 436 | dev_get_platdata(&client->dev); |
Michael Hennerich | 88751dd | 2009-09-17 22:39:38 -0700 | [diff] [blame] | 437 | struct input_dev *input; |
| 438 | unsigned int revid; |
| 439 | int ret, i; |
| 440 | int error; |
| 441 | |
| 442 | if (!i2c_check_functionality(client->adapter, |
| 443 | I2C_FUNC_SMBUS_BYTE_DATA)) { |
| 444 | dev_err(&client->dev, "SMBUS Byte Data not Supported\n"); |
| 445 | return -EIO; |
| 446 | } |
| 447 | |
| 448 | if (!pdata) { |
| 449 | dev_err(&client->dev, "no platform data?\n"); |
| 450 | return -EINVAL; |
| 451 | } |
| 452 | |
| 453 | if (!pdata->rows || !pdata->cols || !pdata->keymap) { |
| 454 | dev_err(&client->dev, "no rows, cols or keymap from pdata\n"); |
| 455 | return -EINVAL; |
| 456 | } |
| 457 | |
| 458 | if (pdata->keymapsize != ADP5588_KEYMAPSIZE) { |
| 459 | dev_err(&client->dev, "invalid keymapsize\n"); |
| 460 | return -EINVAL; |
| 461 | } |
| 462 | |
Xiaolong CHEN | 69a4af6 | 2010-06-24 19:10:40 -0700 | [diff] [blame] | 463 | if (!pdata->gpimap && pdata->gpimapsize) { |
| 464 | dev_err(&client->dev, "invalid gpimap from pdata\n"); |
| 465 | return -EINVAL; |
| 466 | } |
| 467 | |
| 468 | if (pdata->gpimapsize > ADP5588_GPIMAPSIZE_MAX) { |
| 469 | dev_err(&client->dev, "invalid gpimapsize\n"); |
| 470 | return -EINVAL; |
| 471 | } |
| 472 | |
| 473 | for (i = 0; i < pdata->gpimapsize; i++) { |
| 474 | unsigned short pin = pdata->gpimap[i].pin; |
| 475 | |
| 476 | if (pin < GPI_PIN_BASE || pin > GPI_PIN_END) { |
| 477 | dev_err(&client->dev, "invalid gpi pin data\n"); |
| 478 | return -EINVAL; |
| 479 | } |
| 480 | |
| 481 | if (pin <= GPI_PIN_ROW_END) { |
| 482 | if (pin - GPI_PIN_ROW_BASE + 1 <= pdata->rows) { |
| 483 | dev_err(&client->dev, "invalid gpi row data\n"); |
| 484 | return -EINVAL; |
| 485 | } |
| 486 | } else { |
| 487 | if (pin - GPI_PIN_COL_BASE + 1 <= pdata->cols) { |
| 488 | dev_err(&client->dev, "invalid gpi col data\n"); |
| 489 | return -EINVAL; |
| 490 | } |
| 491 | } |
| 492 | } |
| 493 | |
Michael Hennerich | 88751dd | 2009-09-17 22:39:38 -0700 | [diff] [blame] | 494 | if (!client->irq) { |
| 495 | dev_err(&client->dev, "no IRQ?\n"); |
| 496 | return -EINVAL; |
| 497 | } |
| 498 | |
| 499 | kpad = kzalloc(sizeof(*kpad), GFP_KERNEL); |
| 500 | input = input_allocate_device(); |
| 501 | if (!kpad || !input) { |
| 502 | error = -ENOMEM; |
| 503 | goto err_free_mem; |
| 504 | } |
| 505 | |
| 506 | kpad->client = client; |
| 507 | kpad->input = input; |
| 508 | INIT_DELAYED_WORK(&kpad->work, adp5588_work); |
| 509 | |
| 510 | ret = adp5588_read(client, DEV_ID); |
| 511 | if (ret < 0) { |
| 512 | error = ret; |
| 513 | goto err_free_mem; |
| 514 | } |
| 515 | |
| 516 | revid = (u8) ret & ADP5588_DEVICE_ID_MASK; |
| 517 | if (WA_DELAYED_READOUT_REVID(revid)) |
| 518 | kpad->delay = msecs_to_jiffies(30); |
| 519 | |
| 520 | input->name = client->name; |
| 521 | input->phys = "adp5588-keys/input0"; |
| 522 | input->dev.parent = &client->dev; |
| 523 | |
| 524 | input_set_drvdata(input, kpad); |
| 525 | |
| 526 | input->id.bustype = BUS_I2C; |
| 527 | input->id.vendor = 0x0001; |
| 528 | input->id.product = 0x0001; |
| 529 | input->id.version = revid; |
| 530 | |
| 531 | input->keycodesize = sizeof(kpad->keycode[0]); |
| 532 | input->keycodemax = pdata->keymapsize; |
| 533 | input->keycode = kpad->keycode; |
| 534 | |
| 535 | memcpy(kpad->keycode, pdata->keymap, |
| 536 | pdata->keymapsize * input->keycodesize); |
| 537 | |
Xiaolong CHEN | 69a4af6 | 2010-06-24 19:10:40 -0700 | [diff] [blame] | 538 | kpad->gpimap = pdata->gpimap; |
| 539 | kpad->gpimapsize = pdata->gpimapsize; |
| 540 | |
Michael Hennerich | 88751dd | 2009-09-17 22:39:38 -0700 | [diff] [blame] | 541 | /* setup input device */ |
| 542 | __set_bit(EV_KEY, input->evbit); |
| 543 | |
| 544 | if (pdata->repeat) |
| 545 | __set_bit(EV_REP, input->evbit); |
| 546 | |
| 547 | for (i = 0; i < input->keycodemax; i++) |
Andrew Liu | e4cfb03 | 2013-11-23 10:06:36 -0800 | [diff] [blame] | 548 | if (kpad->keycode[i] <= KEY_MAX) |
| 549 | __set_bit(kpad->keycode[i], input->keybit); |
Michael Hennerich | 88751dd | 2009-09-17 22:39:38 -0700 | [diff] [blame] | 550 | __clear_bit(KEY_RESERVED, input->keybit); |
| 551 | |
Xiaolong CHEN | 69a4af6 | 2010-06-24 19:10:40 -0700 | [diff] [blame] | 552 | if (kpad->gpimapsize) |
| 553 | __set_bit(EV_SW, input->evbit); |
| 554 | for (i = 0; i < kpad->gpimapsize; i++) |
| 555 | __set_bit(kpad->gpimap[i].sw_evt, input->swbit); |
| 556 | |
Michael Hennerich | 88751dd | 2009-09-17 22:39:38 -0700 | [diff] [blame] | 557 | error = input_register_device(input); |
| 558 | if (error) { |
| 559 | dev_err(&client->dev, "unable to register input device\n"); |
| 560 | goto err_free_mem; |
| 561 | } |
| 562 | |
| 563 | error = request_irq(client->irq, adp5588_irq, |
Yong Zhang | ec4665c | 2011-09-07 14:04:16 -0700 | [diff] [blame] | 564 | IRQF_TRIGGER_FALLING, |
Michael Hennerich | 88751dd | 2009-09-17 22:39:38 -0700 | [diff] [blame] | 565 | client->dev.driver->name, kpad); |
| 566 | if (error) { |
| 567 | dev_err(&client->dev, "irq %d busy?\n", client->irq); |
| 568 | goto err_unreg_dev; |
| 569 | } |
| 570 | |
| 571 | error = adp5588_setup(client); |
| 572 | if (error) |
| 573 | goto err_free_irq; |
| 574 | |
Xiaolong CHEN | 69a4af6 | 2010-06-24 19:10:40 -0700 | [diff] [blame] | 575 | if (kpad->gpimapsize) |
| 576 | adp5588_report_switch_state(kpad); |
| 577 | |
Dmitry Torokhov | 0d87c72 | 2010-08-02 18:33:26 -0700 | [diff] [blame] | 578 | error = adp5588_gpio_add(kpad); |
Xiaolong Chen | ba9f507 | 2010-07-26 01:01:11 -0700 | [diff] [blame] | 579 | if (error) |
| 580 | goto err_free_irq; |
| 581 | |
Michael Hennerich | 88751dd | 2009-09-17 22:39:38 -0700 | [diff] [blame] | 582 | device_init_wakeup(&client->dev, 1); |
| 583 | i2c_set_clientdata(client, kpad); |
| 584 | |
| 585 | dev_info(&client->dev, "Rev.%d keypad, irq %d\n", revid, client->irq); |
| 586 | return 0; |
| 587 | |
| 588 | err_free_irq: |
| 589 | free_irq(client->irq, kpad); |
Pramod Gurav | b3f9db4 | 2014-10-07 09:33:16 -0700 | [diff] [blame] | 590 | cancel_delayed_work_sync(&kpad->work); |
Michael Hennerich | 88751dd | 2009-09-17 22:39:38 -0700 | [diff] [blame] | 591 | err_unreg_dev: |
| 592 | input_unregister_device(input); |
| 593 | input = NULL; |
| 594 | err_free_mem: |
| 595 | input_free_device(input); |
| 596 | kfree(kpad); |
| 597 | |
| 598 | return error; |
| 599 | } |
| 600 | |
Bill Pemberton | e2619cf | 2012-11-23 21:50:47 -0800 | [diff] [blame] | 601 | static int adp5588_remove(struct i2c_client *client) |
Michael Hennerich | 88751dd | 2009-09-17 22:39:38 -0700 | [diff] [blame] | 602 | { |
| 603 | struct adp5588_kpad *kpad = i2c_get_clientdata(client); |
| 604 | |
| 605 | adp5588_write(client, CFG, 0); |
| 606 | free_irq(client->irq, kpad); |
| 607 | cancel_delayed_work_sync(&kpad->work); |
| 608 | input_unregister_device(kpad->input); |
Dmitry Torokhov | 0d87c72 | 2010-08-02 18:33:26 -0700 | [diff] [blame] | 609 | adp5588_gpio_remove(kpad); |
Michael Hennerich | 88751dd | 2009-09-17 22:39:38 -0700 | [diff] [blame] | 610 | kfree(kpad); |
| 611 | |
| 612 | return 0; |
| 613 | } |
| 614 | |
| 615 | #ifdef CONFIG_PM |
| 616 | static int adp5588_suspend(struct device *dev) |
| 617 | { |
| 618 | struct adp5588_kpad *kpad = dev_get_drvdata(dev); |
| 619 | struct i2c_client *client = kpad->client; |
| 620 | |
| 621 | disable_irq(client->irq); |
| 622 | cancel_delayed_work_sync(&kpad->work); |
| 623 | |
| 624 | if (device_may_wakeup(&client->dev)) |
| 625 | enable_irq_wake(client->irq); |
| 626 | |
| 627 | return 0; |
| 628 | } |
| 629 | |
| 630 | static int adp5588_resume(struct device *dev) |
| 631 | { |
| 632 | struct adp5588_kpad *kpad = dev_get_drvdata(dev); |
| 633 | struct i2c_client *client = kpad->client; |
| 634 | |
| 635 | if (device_may_wakeup(&client->dev)) |
| 636 | disable_irq_wake(client->irq); |
| 637 | |
| 638 | enable_irq(client->irq); |
| 639 | |
| 640 | return 0; |
| 641 | } |
| 642 | |
Alexey Dobriyan | 4714521 | 2009-12-14 18:00:08 -0800 | [diff] [blame] | 643 | static const struct dev_pm_ops adp5588_dev_pm_ops = { |
Michael Hennerich | 88751dd | 2009-09-17 22:39:38 -0700 | [diff] [blame] | 644 | .suspend = adp5588_suspend, |
| 645 | .resume = adp5588_resume, |
| 646 | }; |
| 647 | #endif |
| 648 | |
| 649 | static const struct i2c_device_id adp5588_id[] = { |
Michael Hennerich | d537155 | 2010-10-18 17:46:03 -0700 | [diff] [blame] | 650 | { "adp5588-keys", 0 }, |
Michael Hennerich | 5a9003d | 2010-01-19 00:28:44 -0800 | [diff] [blame] | 651 | { "adp5587-keys", 0 }, |
Michael Hennerich | 88751dd | 2009-09-17 22:39:38 -0700 | [diff] [blame] | 652 | { } |
| 653 | }; |
| 654 | MODULE_DEVICE_TABLE(i2c, adp5588_id); |
| 655 | |
| 656 | static struct i2c_driver adp5588_driver = { |
| 657 | .driver = { |
| 658 | .name = KBUILD_MODNAME, |
| 659 | #ifdef CONFIG_PM |
| 660 | .pm = &adp5588_dev_pm_ops, |
| 661 | #endif |
| 662 | }, |
| 663 | .probe = adp5588_probe, |
Bill Pemberton | 1cb0aa8 | 2012-11-23 21:27:39 -0800 | [diff] [blame] | 664 | .remove = adp5588_remove, |
Michael Hennerich | 88751dd | 2009-09-17 22:39:38 -0700 | [diff] [blame] | 665 | .id_table = adp5588_id, |
| 666 | }; |
| 667 | |
Axel Lin | 1b92c1c | 2012-03-16 23:05:41 -0700 | [diff] [blame] | 668 | module_i2c_driver(adp5588_driver); |
Michael Hennerich | 88751dd | 2009-09-17 22:39:38 -0700 | [diff] [blame] | 669 | |
| 670 | MODULE_LICENSE("GPL"); |
| 671 | MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>"); |
Michael Hennerich | 5a9003d | 2010-01-19 00:28:44 -0800 | [diff] [blame] | 672 | MODULE_DESCRIPTION("ADP5588/87 Keypad driver"); |