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