aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/input/keyboard
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/input/keyboard')
-rw-r--r--drivers/input/keyboard/Kconfig21
-rw-r--r--drivers/input/keyboard/Makefile2
-rw-r--r--drivers/input/keyboard/ep93xx_keypad.c470
-rw-r--r--drivers/input/keyboard/gpio_keys.c35
-rw-r--r--drivers/input/keyboard/lm8323.c878
5 files changed, 1384 insertions, 22 deletions
diff --git a/drivers/input/keyboard/Kconfig b/drivers/input/keyboard/Kconfig
index ea2638b41982..9d8f796c6745 100644
--- a/drivers/input/keyboard/Kconfig
+++ b/drivers/input/keyboard/Kconfig
@@ -250,6 +250,17 @@ config KEYBOARD_HP7XX
250 To compile this driver as a module, choose M here: the 250 To compile this driver as a module, choose M here: the
251 module will be called jornada720_kbd. 251 module will be called jornada720_kbd.
252 252
253config KEYBOARD_LM8323
254 tristate "LM8323 keypad chip"
255 depends on I2C
256 depends on LEDS_CLASS
257 help
258 If you say yes here you get support for the National Semiconductor
259 LM8323 keypad controller.
260
261 To compile this driver as a module, choose M here: the
262 module will be called lm8323.
263
253config KEYBOARD_OMAP 264config KEYBOARD_OMAP
254 tristate "TI OMAP keypad support" 265 tristate "TI OMAP keypad support"
255 depends on (ARCH_OMAP1 || ARCH_OMAP2) 266 depends on (ARCH_OMAP1 || ARCH_OMAP2)
@@ -332,4 +343,14 @@ config KEYBOARD_SH_KEYSC
332 343
333 To compile this driver as a module, choose M here: the 344 To compile this driver as a module, choose M here: the
334 module will be called sh_keysc. 345 module will be called sh_keysc.
346
347config KEYBOARD_EP93XX
348 tristate "EP93xx Matrix Keypad support"
349 depends on ARCH_EP93XX
350 help
351 Say Y here to enable the matrix keypad on the Cirrus EP93XX.
352
353 To compile this driver as a module, choose M here: the
354 module will be called ep93xx_keypad.
355
335endif 356endif
diff --git a/drivers/input/keyboard/Makefile b/drivers/input/keyboard/Makefile
index 36351e1190f9..156b647a259b 100644
--- a/drivers/input/keyboard/Makefile
+++ b/drivers/input/keyboard/Makefile
@@ -18,6 +18,7 @@ obj-$(CONFIG_KEYBOARD_SPITZ) += spitzkbd.o
18obj-$(CONFIG_KEYBOARD_TOSA) += tosakbd.o 18obj-$(CONFIG_KEYBOARD_TOSA) += tosakbd.o
19obj-$(CONFIG_KEYBOARD_HIL) += hil_kbd.o 19obj-$(CONFIG_KEYBOARD_HIL) += hil_kbd.o
20obj-$(CONFIG_KEYBOARD_HIL_OLD) += hilkbd.o 20obj-$(CONFIG_KEYBOARD_HIL_OLD) += hilkbd.o
21obj-$(CONFIG_KEYBOARD_LM8323) += lm8323.o
21obj-$(CONFIG_KEYBOARD_OMAP) += omap-keypad.o 22obj-$(CONFIG_KEYBOARD_OMAP) += omap-keypad.o
22obj-$(CONFIG_KEYBOARD_PXA27x) += pxa27x_keypad.o 23obj-$(CONFIG_KEYBOARD_PXA27x) += pxa27x_keypad.o
23obj-$(CONFIG_KEYBOARD_PXA930_ROTARY) += pxa930_rotary.o 24obj-$(CONFIG_KEYBOARD_PXA930_ROTARY) += pxa930_rotary.o
@@ -28,3 +29,4 @@ obj-$(CONFIG_KEYBOARD_HP7XX) += jornada720_kbd.o
28obj-$(CONFIG_KEYBOARD_MAPLE) += maple_keyb.o 29obj-$(CONFIG_KEYBOARD_MAPLE) += maple_keyb.o
29obj-$(CONFIG_KEYBOARD_BFIN) += bf54x-keys.o 30obj-$(CONFIG_KEYBOARD_BFIN) += bf54x-keys.o
30obj-$(CONFIG_KEYBOARD_SH_KEYSC) += sh_keysc.o 31obj-$(CONFIG_KEYBOARD_SH_KEYSC) += sh_keysc.o
32obj-$(CONFIG_KEYBOARD_EP93XX) += ep93xx_keypad.o
diff --git a/drivers/input/keyboard/ep93xx_keypad.c b/drivers/input/keyboard/ep93xx_keypad.c
new file mode 100644
index 000000000000..181d30e3018e
--- /dev/null
+++ b/drivers/input/keyboard/ep93xx_keypad.c
@@ -0,0 +1,470 @@
1/*
2 * Driver for the Cirrus EP93xx matrix keypad controller.
3 *
4 * Copyright (c) 2008 H Hartley Sweeten <hsweeten@visionengravers.com>
5 *
6 * Based on the pxa27x matrix keypad controller by Rodolfo Giometti.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 *
12 * NOTE:
13 *
14 * The 3-key reset is triggered by pressing the 3 keys in
15 * Row 0, Columns 2, 4, and 7 at the same time. This action can
16 * be disabled by setting the EP93XX_KEYPAD_DISABLE_3_KEY flag.
17 *
18 * Normal operation for the matrix does not autorepeat the key press.
19 * This action can be enabled by setting the EP93XX_KEYPAD_AUTOREPEAT
20 * flag.
21 */
22
23#include <linux/platform_device.h>
24#include <linux/interrupt.h>
25#include <linux/input.h>
26#include <linux/clk.h>
27
28#include <mach/hardware.h>
29#include <mach/gpio.h>
30#include <mach/ep93xx_keypad.h>
31
32/*
33 * Keypad Interface Register offsets
34 */
35#define KEY_INIT 0x00 /* Key Scan Initialization register */
36#define KEY_DIAG 0x04 /* Key Scan Diagnostic register */
37#define KEY_REG 0x08 /* Key Value Capture register */
38
39/* Key Scan Initialization Register bit defines */
40#define KEY_INIT_DBNC_MASK (0x00ff0000)
41#define KEY_INIT_DBNC_SHIFT (16)
42#define KEY_INIT_DIS3KY (1<<15)
43#define KEY_INIT_DIAG (1<<14)
44#define KEY_INIT_BACK (1<<13)
45#define KEY_INIT_T2 (1<<12)
46#define KEY_INIT_PRSCL_MASK (0x000003ff)
47#define KEY_INIT_PRSCL_SHIFT (0)
48
49/* Key Scan Diagnostic Register bit defines */
50#define KEY_DIAG_MASK (0x0000003f)
51#define KEY_DIAG_SHIFT (0)
52
53/* Key Value Capture Register bit defines */
54#define KEY_REG_K (1<<15)
55#define KEY_REG_INT (1<<14)
56#define KEY_REG_2KEYS (1<<13)
57#define KEY_REG_1KEY (1<<12)
58#define KEY_REG_KEY2_MASK (0x00000fc0)
59#define KEY_REG_KEY2_SHIFT (6)
60#define KEY_REG_KEY1_MASK (0x0000003f)
61#define KEY_REG_KEY1_SHIFT (0)
62
63#define keypad_readl(off) __raw_readl(keypad->mmio_base + (off))
64#define keypad_writel(v, off) __raw_writel((v), keypad->mmio_base + (off))
65
66#define MAX_MATRIX_KEY_NUM (MAX_MATRIX_KEY_ROWS * MAX_MATRIX_KEY_COLS)
67
68struct ep93xx_keypad {
69 struct ep93xx_keypad_platform_data *pdata;
70
71 struct clk *clk;
72 struct input_dev *input_dev;
73 void __iomem *mmio_base;
74
75 int irq;
76 int enabled;
77
78 int key1;
79 int key2;
80
81 unsigned int matrix_keycodes[MAX_MATRIX_KEY_NUM];
82};
83
84static void ep93xx_keypad_build_keycode(struct ep93xx_keypad *keypad)
85{
86 struct ep93xx_keypad_platform_data *pdata = keypad->pdata;
87 struct input_dev *input_dev = keypad->input_dev;
88 int i;
89
90 for (i = 0; i < pdata->matrix_key_map_size; i++) {
91 unsigned int key = pdata->matrix_key_map[i];
92 int row = (key >> 28) & 0xf;
93 int col = (key >> 24) & 0xf;
94 int code = key & 0xffffff;
95
96 keypad->matrix_keycodes[(row << 3) + col] = code;
97 __set_bit(code, input_dev->keybit);
98 }
99}
100
101static irqreturn_t ep93xx_keypad_irq_handler(int irq, void *dev_id)
102{
103 struct ep93xx_keypad *keypad = dev_id;
104 struct input_dev *input_dev = keypad->input_dev;
105 unsigned int status = keypad_readl(KEY_REG);
106 int keycode, key1, key2;
107
108 keycode = (status & KEY_REG_KEY1_MASK) >> KEY_REG_KEY1_SHIFT;
109 key1 = keypad->matrix_keycodes[keycode];
110
111 keycode = (status & KEY_REG_KEY2_MASK) >> KEY_REG_KEY2_SHIFT;
112 key2 = keypad->matrix_keycodes[keycode];
113
114 if (status & KEY_REG_2KEYS) {
115 if (keypad->key1 && key1 != keypad->key1 && key2 != keypad->key1)
116 input_report_key(input_dev, keypad->key1, 0);
117
118 if (keypad->key2 && key1 != keypad->key2 && key2 != keypad->key2)
119 input_report_key(input_dev, keypad->key2, 0);
120
121 input_report_key(input_dev, key1, 1);
122 input_report_key(input_dev, key2, 1);
123
124 keypad->key1 = key1;
125 keypad->key2 = key2;
126
127 } else if (status & KEY_REG_1KEY) {
128 if (keypad->key1 && key1 != keypad->key1)
129 input_report_key(input_dev, keypad->key1, 0);
130
131 if (keypad->key2 && key1 != keypad->key2)
132 input_report_key(input_dev, keypad->key2, 0);
133
134 input_report_key(input_dev, key1, 1);
135
136 keypad->key1 = key1;
137 keypad->key2 = 0;
138
139 } else {
140 input_report_key(input_dev, keypad->key1, 0);
141 input_report_key(input_dev, keypad->key2, 0);
142
143 keypad->key1 = keypad->key2 = 0;
144 }
145 input_sync(input_dev);
146
147 return IRQ_HANDLED;
148}
149
150static void ep93xx_keypad_config(struct ep93xx_keypad *keypad)
151{
152 struct ep93xx_keypad_platform_data *pdata = keypad->pdata;
153 unsigned int val = 0;
154
155 clk_set_rate(keypad->clk, pdata->flags & EP93XX_KEYPAD_KDIV);
156
157 if (pdata->flags & EP93XX_KEYPAD_DISABLE_3_KEY)
158 val |= KEY_INIT_DIS3KY;
159 if (pdata->flags & EP93XX_KEYPAD_DIAG_MODE)
160 val |= KEY_INIT_DIAG;
161 if (pdata->flags & EP93XX_KEYPAD_BACK_DRIVE)
162 val |= KEY_INIT_BACK;
163 if (pdata->flags & EP93XX_KEYPAD_TEST_MODE)
164 val |= KEY_INIT_T2;
165
166 val |= ((pdata->debounce << KEY_INIT_DBNC_SHIFT) & KEY_INIT_DBNC_MASK);
167
168 val |= ((pdata->prescale << KEY_INIT_PRSCL_SHIFT) & KEY_INIT_PRSCL_MASK);
169
170 keypad_writel(val, KEY_INIT);
171}
172
173static int ep93xx_keypad_open(struct input_dev *pdev)
174{
175 struct ep93xx_keypad *keypad = input_get_drvdata(pdev);
176
177 if (!keypad->enabled) {
178 ep93xx_keypad_config(keypad);
179 clk_enable(keypad->clk);
180 keypad->enabled = 1;
181 }
182
183 return 0;
184}
185
186static void ep93xx_keypad_close(struct input_dev *pdev)
187{
188 struct ep93xx_keypad *keypad = input_get_drvdata(pdev);
189
190 if (keypad->enabled) {
191 clk_disable(keypad->clk);
192 keypad->enabled = 0;
193 }
194}
195
196
197#ifdef CONFIG_PM
198/*
199 * NOTE: I don't know if this is correct, or will work on the ep93xx.
200 *
201 * None of the existing ep93xx drivers have power management support.
202 * But, this is basically what the pxa27x_keypad driver does.
203 */
204static int ep93xx_keypad_suspend(struct platform_device *pdev,
205 pm_message_t state)
206{
207 struct ep93xx_keypad *keypad = platform_get_drvdata(pdev);
208 struct input_dev *input_dev = keypad->input_dev;
209
210 mutex_lock(&input_dev->mutex);
211
212 if (keypad->enabled) {
213 clk_disable(keypad->clk);
214 keypad->enabled = 0;
215 }
216
217 mutex_unlock(&input_dev->mutex);
218
219 if (device_may_wakeup(&pdev->dev))
220 enable_irq_wake(keypad->irq);
221
222 return 0;
223}
224
225static int ep93xx_keypad_resume(struct platform_device *pdev)
226{
227 struct ep93xx_keypad *keypad = platform_get_drvdata(pdev);
228 struct input_dev *input_dev = keypad->input_dev;
229
230 if (device_may_wakeup(&pdev->dev))
231 disable_irq_wake(keypad->irq);
232
233 mutex_lock(&input_dev->mutex);
234
235 if (input_dev->users) {
236 if (!keypad->enabled) {
237 ep93xx_keypad_config(keypad);
238 clk_enable(keypad->clk);
239 keypad->enabled = 1;
240 }
241 }
242
243 mutex_unlock(&input_dev->mutex);
244
245 return 0;
246}
247#else /* !CONFIG_PM */
248#define ep93xx_keypad_suspend NULL
249#define ep93xx_keypad_resume NULL
250#endif /* !CONFIG_PM */
251
252static int __devinit ep93xx_keypad_probe(struct platform_device *pdev)
253{
254 struct ep93xx_keypad *keypad;
255 struct ep93xx_keypad_platform_data *pdata = pdev->dev.platform_data;
256 struct input_dev *input_dev;
257 struct resource *res;
258 int irq, err, i, gpio;
259
260 if (!pdata ||
261 !pdata->matrix_key_rows ||
262 pdata->matrix_key_rows > MAX_MATRIX_KEY_ROWS ||
263 !pdata->matrix_key_cols ||
264 pdata->matrix_key_cols > MAX_MATRIX_KEY_COLS) {
265 dev_err(&pdev->dev, "invalid or missing platform data\n");
266 return -EINVAL;
267 }
268
269 keypad = kzalloc(sizeof(struct ep93xx_keypad), GFP_KERNEL);
270 if (!keypad) {
271 dev_err(&pdev->dev, "failed to allocate driver data\n");
272 return -ENOMEM;
273 }
274
275 keypad->pdata = pdata;
276
277 irq = platform_get_irq(pdev, 0);
278 if (irq < 0) {
279 dev_err(&pdev->dev, "failed to get keypad irq\n");
280 err = -ENXIO;
281 goto failed_free;
282 }
283
284 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
285 if (!res) {
286 dev_err(&pdev->dev, "failed to get I/O memory\n");
287 err = -ENXIO;
288 goto failed_free;
289 }
290
291 res = request_mem_region(res->start, resource_size(res), pdev->name);
292 if (!res) {
293 dev_err(&pdev->dev, "failed to request I/O memory\n");
294 err = -EBUSY;
295 goto failed_free;
296 }
297
298 keypad->mmio_base = ioremap(res->start, resource_size(res));
299 if (keypad->mmio_base == NULL) {
300 dev_err(&pdev->dev, "failed to remap I/O memory\n");
301 err = -ENXIO;
302 goto failed_free_mem;
303 }
304
305 /* Request the needed GPIO's */
306 gpio = EP93XX_GPIO_LINE_ROW0;
307 for (i = 0; i < keypad->pdata->matrix_key_rows; i++, gpio++) {
308 err = gpio_request(gpio, pdev->name);
309 if (err) {
310 dev_err(&pdev->dev, "failed to request gpio-%d\n",
311 gpio);
312 goto failed_free_rows;
313 }
314 }
315
316 gpio = EP93XX_GPIO_LINE_COL0;
317 for (i = 0; i < keypad->pdata->matrix_key_cols; i++, gpio++) {
318 err = gpio_request(gpio, pdev->name);
319 if (err) {
320 dev_err(&pdev->dev, "failed to request gpio-%d\n",
321 gpio);
322 goto failed_free_cols;
323 }
324 }
325
326 keypad->clk = clk_get(&pdev->dev, "key_clk");
327 if (IS_ERR(keypad->clk)) {
328 dev_err(&pdev->dev, "failed to get keypad clock\n");
329 err = PTR_ERR(keypad->clk);
330 goto failed_free_io;
331 }
332
333 /* Create and register the input driver */
334 input_dev = input_allocate_device();
335 if (!input_dev) {
336 dev_err(&pdev->dev, "failed to allocate input device\n");
337 err = -ENOMEM;
338 goto failed_put_clk;
339 }
340
341 keypad->input_dev = input_dev;
342
343 input_dev->name = pdev->name;
344 input_dev->id.bustype = BUS_HOST;
345 input_dev->open = ep93xx_keypad_open;
346 input_dev->close = ep93xx_keypad_close;
347 input_dev->dev.parent = &pdev->dev;
348 input_dev->keycode = keypad->matrix_keycodes;
349 input_dev->keycodesize = sizeof(keypad->matrix_keycodes[0]);
350 input_dev->keycodemax = ARRAY_SIZE(keypad->matrix_keycodes);
351
352 input_set_drvdata(input_dev, keypad);
353
354 input_dev->evbit[0] = BIT_MASK(EV_KEY);
355 if (keypad->pdata->flags & EP93XX_KEYPAD_AUTOREPEAT)
356 input_dev->evbit[0] |= BIT_MASK(EV_REP);
357
358 ep93xx_keypad_build_keycode(keypad);
359 platform_set_drvdata(pdev, keypad);
360
361 err = request_irq(irq, ep93xx_keypad_irq_handler, IRQF_DISABLED,
362 pdev->name, keypad);
363 if (err) {
364 dev_err(&pdev->dev, "failed to request IRQ\n");
365 goto failed_free_dev;
366 }
367
368 keypad->irq = irq;
369
370 /* Register the input device */
371 err = input_register_device(input_dev);
372 if (err) {
373 dev_err(&pdev->dev, "failed to register input device\n");
374 goto failed_free_irq;
375 }
376
377 device_init_wakeup(&pdev->dev, 1);
378
379 return 0;
380
381failed_free_irq:
382 free_irq(irq, pdev);
383 platform_set_drvdata(pdev, NULL);
384failed_free_dev:
385 input_free_device(input_dev);
386failed_put_clk:
387 clk_put(keypad->clk);
388failed_free_io:
389 i = keypad->pdata->matrix_key_cols - 1;
390 gpio = EP93XX_GPIO_LINE_COL0 + i;
391failed_free_cols:
392 for ( ; i >= 0; i--, gpio--)
393 gpio_free(gpio);
394 i = keypad->pdata->matrix_key_rows - 1;
395 gpio = EP93XX_GPIO_LINE_ROW0 + i;
396failed_free_rows:
397 for ( ; i >= 0; i--, gpio--)
398 gpio_free(gpio);
399 iounmap(keypad->mmio_base);
400failed_free_mem:
401 release_mem_region(res->start, resource_size(res));
402failed_free:
403 kfree(keypad);
404 return err;
405}
406
407static int __devexit ep93xx_keypad_remove(struct platform_device *pdev)
408{
409 struct ep93xx_keypad *keypad = platform_get_drvdata(pdev);
410 struct resource *res;
411 int i, gpio;
412
413 free_irq(keypad->irq, pdev);
414
415 platform_set_drvdata(pdev, NULL);
416
417 if (keypad->enabled)
418 clk_disable(keypad->clk);
419 clk_put(keypad->clk);
420
421 input_unregister_device(keypad->input_dev);
422
423 i = keypad->pdata->matrix_key_cols - 1;
424 gpio = EP93XX_GPIO_LINE_COL0 + i;
425 for ( ; i >= 0; i--, gpio--)
426 gpio_free(gpio);
427
428 i = keypad->pdata->matrix_key_rows - 1;
429 gpio = EP93XX_GPIO_LINE_ROW0 + i;
430 for ( ; i >= 0; i--, gpio--)
431 gpio_free(gpio);
432
433 iounmap(keypad->mmio_base);
434
435 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
436 release_mem_region(res->start, resource_size(res));
437
438 kfree(keypad);
439
440 return 0;
441}
442
443static struct platform_driver ep93xx_keypad_driver = {
444 .driver = {
445 .name = "ep93xx-keypad",
446 .owner = THIS_MODULE,
447 },
448 .probe = ep93xx_keypad_probe,
449 .remove = __devexit_p(ep93xx_keypad_remove),
450 .suspend = ep93xx_keypad_suspend,
451 .resume = ep93xx_keypad_resume,
452};
453
454static int __init ep93xx_keypad_init(void)
455{
456 return platform_driver_register(&ep93xx_keypad_driver);
457}
458
459static void __exit ep93xx_keypad_exit(void)
460{
461 platform_driver_unregister(&ep93xx_keypad_driver);
462}
463
464module_init(ep93xx_keypad_init);
465module_exit(ep93xx_keypad_exit);
466
467MODULE_LICENSE("GPL");
468MODULE_AUTHOR("H Hartley Sweeten <hsweeten@visionengravers.com>");
469MODULE_DESCRIPTION("EP93xx Matrix Keypad Controller");
470MODULE_ALIAS("platform:ep93xx-keypad");
diff --git a/drivers/input/keyboard/gpio_keys.c b/drivers/input/keyboard/gpio_keys.c
index ad67d763fdbd..2157cd7de00c 100644
--- a/drivers/input/keyboard/gpio_keys.c
+++ b/drivers/input/keyboard/gpio_keys.c
@@ -22,13 +22,14 @@
22#include <linux/platform_device.h> 22#include <linux/platform_device.h>
23#include <linux/input.h> 23#include <linux/input.h>
24#include <linux/gpio_keys.h> 24#include <linux/gpio_keys.h>
25#include <linux/workqueue.h>
25 26
26#include <asm/gpio.h> 27#include <asm/gpio.h>
27 28
28struct gpio_button_data { 29struct gpio_button_data {
29 struct gpio_keys_button *button; 30 struct gpio_keys_button *button;
30 struct input_dev *input; 31 struct input_dev *input;
31 struct timer_list timer; 32 struct delayed_work work;
32}; 33};
33 34
34struct gpio_keys_drvdata { 35struct gpio_keys_drvdata {
@@ -36,8 +37,10 @@ struct gpio_keys_drvdata {
36 struct gpio_button_data data[0]; 37 struct gpio_button_data data[0];
37}; 38};
38 39
39static void gpio_keys_report_event(struct gpio_button_data *bdata) 40static void gpio_keys_report_event(struct work_struct *work)
40{ 41{
42 struct gpio_button_data *bdata =
43 container_of(work, struct gpio_button_data, work.work);
41 struct gpio_keys_button *button = bdata->button; 44 struct gpio_keys_button *button = bdata->button;
42 struct input_dev *input = bdata->input; 45 struct input_dev *input = bdata->input;
43 unsigned int type = button->type ?: EV_KEY; 46 unsigned int type = button->type ?: EV_KEY;
@@ -47,25 +50,17 @@ static void gpio_keys_report_event(struct gpio_button_data *bdata)
47 input_sync(input); 50 input_sync(input);
48} 51}
49 52
50static void gpio_check_button(unsigned long _data)
51{
52 struct gpio_button_data *data = (struct gpio_button_data *)_data;
53
54 gpio_keys_report_event(data);
55}
56
57static irqreturn_t gpio_keys_isr(int irq, void *dev_id) 53static irqreturn_t gpio_keys_isr(int irq, void *dev_id)
58{ 54{
59 struct gpio_button_data *bdata = dev_id; 55 struct gpio_button_data *bdata = dev_id;
60 struct gpio_keys_button *button = bdata->button; 56 struct gpio_keys_button *button = bdata->button;
57 unsigned long delay;
61 58
62 BUG_ON(irq != gpio_to_irq(button->gpio)); 59 BUG_ON(irq != gpio_to_irq(button->gpio));
63 60
64 if (button->debounce_interval) 61 delay = button->debounce_interval ?
65 mod_timer(&bdata->timer, 62 msecs_to_jiffies(button->debounce_interval) : 0;
66 jiffies + msecs_to_jiffies(button->debounce_interval)); 63 schedule_delayed_work(&bdata->work, delay);
67 else
68 gpio_keys_report_event(bdata);
69 64
70 return IRQ_HANDLED; 65 return IRQ_HANDLED;
71} 66}
@@ -112,8 +107,7 @@ static int __devinit gpio_keys_probe(struct platform_device *pdev)
112 107
113 bdata->input = input; 108 bdata->input = input;
114 bdata->button = button; 109 bdata->button = button;
115 setup_timer(&bdata->timer, 110 INIT_DELAYED_WORK(&bdata->work, gpio_keys_report_event);
116 gpio_check_button, (unsigned long)bdata);
117 111
118 error = gpio_request(button->gpio, button->desc ?: "gpio_keys"); 112 error = gpio_request(button->gpio, button->desc ?: "gpio_keys");
119 if (error < 0) { 113 if (error < 0) {
@@ -142,8 +136,7 @@ static int __devinit gpio_keys_probe(struct platform_device *pdev)
142 } 136 }
143 137
144 error = request_irq(irq, gpio_keys_isr, 138 error = request_irq(irq, gpio_keys_isr,
145 IRQF_SAMPLE_RANDOM | IRQF_TRIGGER_RISING | 139 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
146 IRQF_TRIGGER_FALLING,
147 button->desc ? button->desc : "gpio_keys", 140 button->desc ? button->desc : "gpio_keys",
148 bdata); 141 bdata);
149 if (error) { 142 if (error) {
@@ -173,8 +166,7 @@ static int __devinit gpio_keys_probe(struct platform_device *pdev)
173 fail2: 166 fail2:
174 while (--i >= 0) { 167 while (--i >= 0) {
175 free_irq(gpio_to_irq(pdata->buttons[i].gpio), &ddata->data[i]); 168 free_irq(gpio_to_irq(pdata->buttons[i].gpio), &ddata->data[i]);
176 if (pdata->buttons[i].debounce_interval) 169 cancel_delayed_work_sync(&ddata->data[i].work);
177 del_timer_sync(&ddata->data[i].timer);
178 gpio_free(pdata->buttons[i].gpio); 170 gpio_free(pdata->buttons[i].gpio);
179 } 171 }
180 172
@@ -198,8 +190,7 @@ static int __devexit gpio_keys_remove(struct platform_device *pdev)
198 for (i = 0; i < pdata->nbuttons; i++) { 190 for (i = 0; i < pdata->nbuttons; i++) {
199 int irq = gpio_to_irq(pdata->buttons[i].gpio); 191 int irq = gpio_to_irq(pdata->buttons[i].gpio);
200 free_irq(irq, &ddata->data[i]); 192 free_irq(irq, &ddata->data[i]);
201 if (pdata->buttons[i].debounce_interval) 193 cancel_delayed_work_sync(&ddata->data[i].work);
202 del_timer_sync(&ddata->data[i].timer);
203 gpio_free(pdata->buttons[i].gpio); 194 gpio_free(pdata->buttons[i].gpio);
204 } 195 }
205 196
diff --git a/drivers/input/keyboard/lm8323.c b/drivers/input/keyboard/lm8323.c
new file mode 100644
index 000000000000..574eda2a4957
--- /dev/null
+++ b/drivers/input/keyboard/lm8323.c
@@ -0,0 +1,878 @@
1/*
2 * drivers/i2c/chips/lm8323.c
3 *
4 * Copyright (C) 2007-2009 Nokia Corporation
5 *
6 * Written by Daniel Stone <daniel.stone@nokia.com>
7 * Timo O. Karjalainen <timo.o.karjalainen@nokia.com>
8 *
9 * Updated by Felipe Balbi <felipe.balbi@nokia.com>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation (version 2 of the License only).
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24
25#include <linux/module.h>
26#include <linux/i2c.h>
27#include <linux/interrupt.h>
28#include <linux/sched.h>
29#include <linux/mutex.h>
30#include <linux/delay.h>
31#include <linux/input.h>
32#include <linux/leds.h>
33#include <linux/i2c/lm8323.h>
34
35/* Commands to send to the chip. */
36#define LM8323_CMD_READ_ID 0x80 /* Read chip ID. */
37#define LM8323_CMD_WRITE_CFG 0x81 /* Set configuration item. */
38#define LM8323_CMD_READ_INT 0x82 /* Get interrupt status. */
39#define LM8323_CMD_RESET 0x83 /* Reset, same as external one */
40#define LM8323_CMD_WRITE_PORT_SEL 0x85 /* Set GPIO in/out. */
41#define LM8323_CMD_WRITE_PORT_STATE 0x86 /* Set GPIO pullup. */
42#define LM8323_CMD_READ_PORT_SEL 0x87 /* Get GPIO in/out. */
43#define LM8323_CMD_READ_PORT_STATE 0x88 /* Get GPIO pullup. */
44#define LM8323_CMD_READ_FIFO 0x89 /* Read byte from FIFO. */
45#define LM8323_CMD_RPT_READ_FIFO 0x8a /* Read FIFO (no increment). */
46#define LM8323_CMD_SET_ACTIVE 0x8b /* Set active time. */
47#define LM8323_CMD_READ_ERR 0x8c /* Get error status. */
48#define LM8323_CMD_READ_ROTATOR 0x8e /* Read rotator status. */
49#define LM8323_CMD_SET_DEBOUNCE 0x8f /* Set debouncing time. */
50#define LM8323_CMD_SET_KEY_SIZE 0x90 /* Set keypad size. */
51#define LM8323_CMD_READ_KEY_SIZE 0x91 /* Get keypad size. */
52#define LM8323_CMD_READ_CFG 0x92 /* Get configuration item. */
53#define LM8323_CMD_WRITE_CLOCK 0x93 /* Set clock config. */
54#define LM8323_CMD_READ_CLOCK 0x94 /* Get clock config. */
55#define LM8323_CMD_PWM_WRITE 0x95 /* Write PWM script. */
56#define LM8323_CMD_START_PWM 0x96 /* Start PWM engine. */
57#define LM8323_CMD_STOP_PWM 0x97 /* Stop PWM engine. */
58
59/* Interrupt status. */
60#define INT_KEYPAD 0x01 /* Key event. */
61#define INT_ROTATOR 0x02 /* Rotator event. */
62#define INT_ERROR 0x08 /* Error: use CMD_READ_ERR. */
63#define INT_NOINIT 0x10 /* Lost configuration. */
64#define INT_PWM1 0x20 /* PWM1 stopped. */
65#define INT_PWM2 0x40 /* PWM2 stopped. */
66#define INT_PWM3 0x80 /* PWM3 stopped. */
67
68/* Errors (signalled by INT_ERROR, read with CMD_READ_ERR). */
69#define ERR_BADPAR 0x01 /* Bad parameter. */
70#define ERR_CMDUNK 0x02 /* Unknown command. */
71#define ERR_KEYOVR 0x04 /* Too many keys pressed. */
72#define ERR_FIFOOVER 0x40 /* FIFO overflow. */
73
74/* Configuration keys (CMD_{WRITE,READ}_CFG). */
75#define CFG_MUX1SEL 0x01 /* Select MUX1_OUT input. */
76#define CFG_MUX1EN 0x02 /* Enable MUX1_OUT. */
77#define CFG_MUX2SEL 0x04 /* Select MUX2_OUT input. */
78#define CFG_MUX2EN 0x08 /* Enable MUX2_OUT. */
79#define CFG_PSIZE 0x20 /* Package size (must be 0). */
80#define CFG_ROTEN 0x40 /* Enable rotator. */
81
82/* Clock settings (CMD_{WRITE,READ}_CLOCK). */
83#define CLK_RCPWM_INTERNAL 0x00
84#define CLK_RCPWM_EXTERNAL 0x03
85#define CLK_SLOWCLKEN 0x08 /* Enable 32.768kHz clock. */
86#define CLK_SLOWCLKOUT 0x40 /* Enable slow pulse output. */
87
88/* The possible addresses corresponding to CONFIG1 and CONFIG2 pin wirings. */
89#define LM8323_I2C_ADDR00 (0x84 >> 1) /* 1000 010x */
90#define LM8323_I2C_ADDR01 (0x86 >> 1) /* 1000 011x */
91#define LM8323_I2C_ADDR10 (0x88 >> 1) /* 1000 100x */
92#define LM8323_I2C_ADDR11 (0x8A >> 1) /* 1000 101x */
93
94/* Key event fifo length */
95#define LM8323_FIFO_LEN 15
96
97/* Commands for PWM engine; feed in with PWM_WRITE. */
98/* Load ramp counter from duty cycle field (range 0 - 0xff). */
99#define PWM_SET(v) (0x4000 | ((v) & 0xff))
100/* Go to start of script. */
101#define PWM_GOTOSTART 0x0000
102/*
103 * Stop engine (generates interrupt). If reset is 1, clear the program
104 * counter, else leave it.
105 */
106#define PWM_END(reset) (0xc000 | (!!(reset) << 11))
107/*
108 * Ramp. If s is 1, divide clock by 512, else divide clock by 16.
109 * Take t clock scales (up to 63) per step, for n steps (up to 126).
110 * If u is set, ramp up, else ramp down.
111 */
112#define PWM_RAMP(s, t, n, u) ((!!(s) << 14) | ((t) & 0x3f) << 8 | \
113 ((n) & 0x7f) | ((u) ? 0 : 0x80))
114/*
115 * Loop (i.e. jump back to pos) for a given number of iterations (up to 63).
116 * If cnt is zero, execute until PWM_END is encountered.
117 */
118#define PWM_LOOP(cnt, pos) (0xa000 | (((cnt) & 0x3f) << 7) | \
119 ((pos) & 0x3f))
120/*
121 * Wait for trigger. Argument is a mask of channels, shifted by the channel
122 * number, e.g. 0xa for channels 3 and 1. Note that channels are numbered
123 * from 1, not 0.
124 */
125#define PWM_WAIT_TRIG(chans) (0xe000 | (((chans) & 0x7) << 6))
126/* Send trigger. Argument is same as PWM_WAIT_TRIG. */
127#define PWM_SEND_TRIG(chans) (0xe000 | ((chans) & 0x7))
128
129struct lm8323_pwm {
130 int id;
131 int fade_time;
132 int brightness;
133 int desired_brightness;
134 bool enabled;
135 bool running;
136 /* pwm lock */
137 struct mutex lock;
138 struct work_struct work;
139 struct led_classdev cdev;
140 struct lm8323_chip *chip;
141};
142
143struct lm8323_chip {
144 /* device lock */
145 struct mutex lock;
146 struct i2c_client *client;
147 struct work_struct work;
148 struct input_dev *idev;
149 bool kp_enabled;
150 bool pm_suspend;
151 unsigned keys_down;
152 char phys[32];
153 unsigned short keymap[LM8323_KEYMAP_SIZE];
154 int size_x;
155 int size_y;
156 int debounce_time;
157 int active_time;
158 struct lm8323_pwm pwm[LM8323_NUM_PWMS];
159};
160
161#define client_to_lm8323(c) container_of(c, struct lm8323_chip, client)
162#define dev_to_lm8323(d) container_of(d, struct lm8323_chip, client->dev)
163#define work_to_lm8323(w) container_of(w, struct lm8323_chip, work)
164#define cdev_to_pwm(c) container_of(c, struct lm8323_pwm, cdev)
165#define work_to_pwm(w) container_of(w, struct lm8323_pwm, work)
166
167#define LM8323_MAX_DATA 8
168
169/*
170 * To write, we just access the chip's address in write mode, and dump the
171 * command and data out on the bus. The command byte and data are taken as
172 * sequential u8s out of varargs, to a maximum of LM8323_MAX_DATA.
173 */
174static int lm8323_write(struct lm8323_chip *lm, int len, ...)
175{
176 int ret, i;
177 va_list ap;
178 u8 data[LM8323_MAX_DATA];
179
180 va_start(ap, len);
181
182 if (unlikely(len > LM8323_MAX_DATA)) {
183 dev_err(&lm->client->dev, "tried to send %d bytes\n", len);
184 va_end(ap);
185 return 0;
186 }
187
188 for (i = 0; i < len; i++)
189 data[i] = va_arg(ap, int);
190
191 va_end(ap);
192
193 /*
194 * If the host is asleep while we send the data, we can get a NACK
195 * back while it wakes up, so try again, once.
196 */
197 ret = i2c_master_send(lm->client, data, len);
198 if (unlikely(ret == -EREMOTEIO))
199 ret = i2c_master_send(lm->client, data, len);
200 if (unlikely(ret != len))
201 dev_err(&lm->client->dev, "sent %d bytes of %d total\n",
202 len, ret);
203
204 return ret;
205}
206
207/*
208 * To read, we first send the command byte to the chip and end the transaction,
209 * then access the chip in read mode, at which point it will send the data.
210 */
211static int lm8323_read(struct lm8323_chip *lm, u8 cmd, u8 *buf, int len)
212{
213 int ret;
214
215 /*
216 * If the host is asleep while we send the byte, we can get a NACK
217 * back while it wakes up, so try again, once.
218 */
219 ret = i2c_master_send(lm->client, &cmd, 1);
220 if (unlikely(ret == -EREMOTEIO))
221 ret = i2c_master_send(lm->client, &cmd, 1);
222 if (unlikely(ret != 1)) {
223 dev_err(&lm->client->dev, "sending read cmd 0x%02x failed\n",
224 cmd);
225 return 0;
226 }
227
228 ret = i2c_master_recv(lm->client, buf, len);
229 if (unlikely(ret != len))
230 dev_err(&lm->client->dev, "wanted %d bytes, got %d\n",
231 len, ret);
232
233 return ret;
234}
235
236/*
237 * Set the chip active time (idle time before it enters halt).
238 */
239static void lm8323_set_active_time(struct lm8323_chip *lm, int time)
240{
241 lm8323_write(lm, 2, LM8323_CMD_SET_ACTIVE, time >> 2);
242}
243
244/*
245 * The signals are AT-style: the low 7 bits are the keycode, and the top
246 * bit indicates the state (1 for down, 0 for up).
247 */
248static inline u8 lm8323_whichkey(u8 event)
249{
250 return event & 0x7f;
251}
252
253static inline int lm8323_ispress(u8 event)
254{
255 return (event & 0x80) ? 1 : 0;
256}
257
258static void process_keys(struct lm8323_chip *lm)
259{
260 u8 event;
261 u8 key_fifo[LM8323_FIFO_LEN + 1];
262 int old_keys_down = lm->keys_down;
263 int ret;
264 int i = 0;
265
266 /*
267 * Read all key events from the FIFO at once. Next READ_FIFO clears the
268 * FIFO even if we didn't read all events previously.
269 */
270 ret = lm8323_read(lm, LM8323_CMD_READ_FIFO, key_fifo, LM8323_FIFO_LEN);
271
272 if (ret < 0) {
273 dev_err(&lm->client->dev, "Failed reading fifo \n");
274 return;
275 }
276 key_fifo[ret] = 0;
277
278 while ((event = key_fifo[i++])) {
279 u8 key = lm8323_whichkey(event);
280 int isdown = lm8323_ispress(event);
281 unsigned short keycode = lm->keymap[key];
282
283 dev_vdbg(&lm->client->dev, "key 0x%02x %s\n",
284 key, isdown ? "down" : "up");
285
286 if (lm->kp_enabled) {
287 input_event(lm->idev, EV_MSC, MSC_SCAN, key);
288 input_report_key(lm->idev, keycode, isdown);
289 input_sync(lm->idev);
290 }
291
292 if (isdown)
293 lm->keys_down++;
294 else
295 lm->keys_down--;
296 }
297
298 /*
299 * Errata: We need to ensure that the chip never enters halt mode
300 * during a keypress, so set active time to 0. When it's released,
301 * we can enter halt again, so set the active time back to normal.
302 */
303 if (!old_keys_down && lm->keys_down)
304 lm8323_set_active_time(lm, 0);
305 if (old_keys_down && !lm->keys_down)
306 lm8323_set_active_time(lm, lm->active_time);
307}
308
309static void lm8323_process_error(struct lm8323_chip *lm)
310{
311 u8 error;
312
313 if (lm8323_read(lm, LM8323_CMD_READ_ERR, &error, 1) == 1) {
314 if (error & ERR_FIFOOVER)
315 dev_vdbg(&lm->client->dev, "fifo overflow!\n");
316 if (error & ERR_KEYOVR)
317 dev_vdbg(&lm->client->dev,
318 "more than two keys pressed\n");
319 if (error & ERR_CMDUNK)
320 dev_vdbg(&lm->client->dev,
321 "unknown command submitted\n");
322 if (error & ERR_BADPAR)
323 dev_vdbg(&lm->client->dev, "bad command parameter\n");
324 }
325}
326
327static void lm8323_reset(struct lm8323_chip *lm)
328{
329 /* The docs say we must pass 0xAA as the data byte. */
330 lm8323_write(lm, 2, LM8323_CMD_RESET, 0xAA);
331}
332
333static int lm8323_configure(struct lm8323_chip *lm)
334{
335 int keysize = (lm->size_x << 4) | lm->size_y;
336 int clock = (CLK_SLOWCLKEN | CLK_RCPWM_EXTERNAL);
337 int debounce = lm->debounce_time >> 2;
338 int active = lm->active_time >> 2;
339
340 /*
341 * Active time must be greater than the debounce time: if it's
342 * a close-run thing, give ourselves a 12ms buffer.
343 */
344 if (debounce >= active)
345 active = debounce + 3;
346
347 lm8323_write(lm, 2, LM8323_CMD_WRITE_CFG, 0);
348 lm8323_write(lm, 2, LM8323_CMD_WRITE_CLOCK, clock);
349 lm8323_write(lm, 2, LM8323_CMD_SET_KEY_SIZE, keysize);
350 lm8323_set_active_time(lm, lm->active_time);
351 lm8323_write(lm, 2, LM8323_CMD_SET_DEBOUNCE, debounce);
352 lm8323_write(lm, 3, LM8323_CMD_WRITE_PORT_STATE, 0xff, 0xff);
353 lm8323_write(lm, 3, LM8323_CMD_WRITE_PORT_SEL, 0, 0);
354
355 /*
356 * Not much we can do about errors at this point, so just hope
357 * for the best.
358 */
359
360 return 0;
361}
362
363static void pwm_done(struct lm8323_pwm *pwm)
364{
365 mutex_lock(&pwm->lock);
366 pwm->running = false;
367 if (pwm->desired_brightness != pwm->brightness)
368 schedule_work(&pwm->work);
369 mutex_unlock(&pwm->lock);
370}
371
372/*
373 * Bottom half: handle the interrupt by posting key events, or dealing with
374 * errors appropriately.
375 */
376static void lm8323_work(struct work_struct *work)
377{
378 struct lm8323_chip *lm = work_to_lm8323(work);
379 u8 ints;
380 int i;
381
382 mutex_lock(&lm->lock);
383
384 while ((lm8323_read(lm, LM8323_CMD_READ_INT, &ints, 1) == 1) && ints) {
385 if (likely(ints & INT_KEYPAD))
386 process_keys(lm);
387 if (ints & INT_ROTATOR) {
388 /* We don't currently support the rotator. */
389 dev_vdbg(&lm->client->dev, "rotator fired\n");
390 }
391 if (ints & INT_ERROR) {
392 dev_vdbg(&lm->client->dev, "error!\n");
393 lm8323_process_error(lm);
394 }
395 if (ints & INT_NOINIT) {
396 dev_err(&lm->client->dev, "chip lost config; "
397 "reinitialising\n");
398 lm8323_configure(lm);
399 }
400 for (i = 0; i < LM8323_NUM_PWMS; i++) {
401 if (ints & (1 << (INT_PWM1 + i))) {
402 dev_vdbg(&lm->client->dev,
403 "pwm%d engine completed\n", i);
404 pwm_done(&lm->pwm[i]);
405 }
406 }
407 }
408
409 mutex_unlock(&lm->lock);
410}
411
412/*
413 * We cannot use I2C in interrupt context, so we just schedule work.
414 */
415static irqreturn_t lm8323_irq(int irq, void *data)
416{
417 struct lm8323_chip *lm = data;
418
419 schedule_work(&lm->work);
420
421 return IRQ_HANDLED;
422}
423
424/*
425 * Read the chip ID.
426 */
427static int lm8323_read_id(struct lm8323_chip *lm, u8 *buf)
428{
429 int bytes;
430
431 bytes = lm8323_read(lm, LM8323_CMD_READ_ID, buf, 2);
432 if (unlikely(bytes != 2))
433 return -EIO;
434
435 return 0;
436}
437
438static void lm8323_write_pwm_one(struct lm8323_pwm *pwm, int pos, u16 cmd)
439{
440 lm8323_write(pwm->chip, 4, LM8323_CMD_PWM_WRITE, (pos << 2) | pwm->id,
441 (cmd & 0xff00) >> 8, cmd & 0x00ff);
442}
443
444/*
445 * Write a script into a given PWM engine, concluding with PWM_END.
446 * If 'kill' is nonzero, the engine will be shut down at the end
447 * of the script, producing a zero output. Otherwise the engine
448 * will be kept running at the final PWM level indefinitely.
449 */
450static void lm8323_write_pwm(struct lm8323_pwm *pwm, int kill,
451 int len, const u16 *cmds)
452{
453 int i;
454
455 for (i = 0; i < len; i++)
456 lm8323_write_pwm_one(pwm, i, cmds[i]);
457
458 lm8323_write_pwm_one(pwm, i++, PWM_END(kill));
459 lm8323_write(pwm->chip, 2, LM8323_CMD_START_PWM, pwm->id);
460 pwm->running = true;
461}
462
463static void lm8323_pwm_work(struct work_struct *work)
464{
465 struct lm8323_pwm *pwm = work_to_pwm(work);
466 int div512, perstep, steps, hz, up, kill;
467 u16 pwm_cmds[3];
468 int num_cmds = 0;
469
470 mutex_lock(&pwm->lock);
471
472 /*
473 * Do nothing if we're already at the requested level,
474 * or previous setting is not yet complete. In the latter
475 * case we will be called again when the previous PWM script
476 * finishes.
477 */
478 if (pwm->running || pwm->desired_brightness == pwm->brightness)
479 goto out;
480
481 kill = (pwm->desired_brightness == 0);
482 up = (pwm->desired_brightness > pwm->brightness);
483 steps = abs(pwm->desired_brightness - pwm->brightness);
484
485 /*
486 * Convert time (in ms) into a divisor (512 or 16 on a refclk of
487 * 32768Hz), and number of ticks per step.
488 */
489 if ((pwm->fade_time / steps) > (32768 / 512)) {
490 div512 = 1;
491 hz = 32768 / 512;
492 } else {
493 div512 = 0;
494 hz = 32768 / 16;
495 }
496
497 perstep = (hz * pwm->fade_time) / (steps * 1000);
498
499 if (perstep == 0)
500 perstep = 1;
501 else if (perstep > 63)
502 perstep = 63;
503
504 while (steps) {
505 int s;
506
507 s = min(126, steps);
508 pwm_cmds[num_cmds++] = PWM_RAMP(div512, perstep, s, up);
509 steps -= s;
510 }
511
512 lm8323_write_pwm(pwm, kill, num_cmds, pwm_cmds);
513 pwm->brightness = pwm->desired_brightness;
514
515 out:
516 mutex_unlock(&pwm->lock);
517}
518
519static void lm8323_pwm_set_brightness(struct led_classdev *led_cdev,
520 enum led_brightness brightness)
521{
522 struct lm8323_pwm *pwm = cdev_to_pwm(led_cdev);
523 struct lm8323_chip *lm = pwm->chip;
524
525 mutex_lock(&pwm->lock);
526 pwm->desired_brightness = brightness;
527 mutex_unlock(&pwm->lock);
528
529 if (in_interrupt()) {
530 schedule_work(&pwm->work);
531 } else {
532 /*
533 * Schedule PWM work as usual unless we are going into suspend
534 */
535 mutex_lock(&lm->lock);
536 if (likely(!lm->pm_suspend))
537 schedule_work(&pwm->work);
538 else
539 lm8323_pwm_work(&pwm->work);
540 mutex_unlock(&lm->lock);
541 }
542}
543
544static ssize_t lm8323_pwm_show_time(struct device *dev,
545 struct device_attribute *attr, char *buf)
546{
547 struct led_classdev *led_cdev = dev_get_drvdata(dev);
548 struct lm8323_pwm *pwm = cdev_to_pwm(led_cdev);
549
550 return sprintf(buf, "%d\n", pwm->fade_time);
551}
552
553static ssize_t lm8323_pwm_store_time(struct device *dev,
554 struct device_attribute *attr, const char *buf, size_t len)
555{
556 struct led_classdev *led_cdev = dev_get_drvdata(dev);
557 struct lm8323_pwm *pwm = cdev_to_pwm(led_cdev);
558 int ret;
559 unsigned long time;
560
561 ret = strict_strtoul(buf, 10, &time);
562 /* Numbers only, please. */
563 if (ret)
564 return -EINVAL;
565
566 pwm->fade_time = time;
567
568 return strlen(buf);
569}
570static DEVICE_ATTR(time, 0644, lm8323_pwm_show_time, lm8323_pwm_store_time);
571
572static int init_pwm(struct lm8323_chip *lm, int id, struct device *dev,
573 const char *name)
574{
575 struct lm8323_pwm *pwm;
576
577 BUG_ON(id > 3);
578
579 pwm = &lm->pwm[id - 1];
580
581 pwm->id = id;
582 pwm->fade_time = 0;
583 pwm->brightness = 0;
584 pwm->desired_brightness = 0;
585 pwm->running = false;
586 pwm->enabled = false;
587 INIT_WORK(&pwm->work, lm8323_pwm_work);
588 mutex_init(&pwm->lock);
589 pwm->chip = lm;
590
591 if (name) {
592 pwm->cdev.name = name;
593 pwm->cdev.brightness_set = lm8323_pwm_set_brightness;
594 if (led_classdev_register(dev, &pwm->cdev) < 0) {
595 dev_err(dev, "couldn't register PWM %d\n", id);
596 return -1;
597 }
598 if (device_create_file(pwm->cdev.dev,
599 &dev_attr_time) < 0) {
600 dev_err(dev, "couldn't register time attribute\n");
601 led_classdev_unregister(&pwm->cdev);
602 return -1;
603 }
604 pwm->enabled = true;
605 }
606
607 return 0;
608}
609
610static struct i2c_driver lm8323_i2c_driver;
611
612static ssize_t lm8323_show_disable(struct device *dev,
613 struct device_attribute *attr, char *buf)
614{
615 struct lm8323_chip *lm = dev_get_drvdata(dev);
616
617 return sprintf(buf, "%u\n", !lm->kp_enabled);
618}
619
620static ssize_t lm8323_set_disable(struct device *dev,
621 struct device_attribute *attr,
622 const char *buf, size_t count)
623{
624 struct lm8323_chip *lm = dev_get_drvdata(dev);
625 int ret;
626 unsigned long i;
627
628 ret = strict_strtoul(buf, 10, &i);
629
630 mutex_lock(&lm->lock);
631 lm->kp_enabled = !i;
632 mutex_unlock(&lm->lock);
633
634 return count;
635}
636static DEVICE_ATTR(disable_kp, 0644, lm8323_show_disable, lm8323_set_disable);
637
638static int __devinit lm8323_probe(struct i2c_client *client,
639 const struct i2c_device_id *id)
640{
641 struct lm8323_platform_data *pdata = client->dev.platform_data;
642 struct input_dev *idev;
643 struct lm8323_chip *lm;
644 int i, err;
645 unsigned long tmo;
646 u8 data[2];
647
648 if (!pdata || !pdata->size_x || !pdata->size_y) {
649 dev_err(&client->dev, "missing platform_data\n");
650 return -EINVAL;
651 }
652
653 if (pdata->size_x > 8) {
654 dev_err(&client->dev, "invalid x size %d specified\n",
655 pdata->size_x);
656 return -EINVAL;
657 }
658
659 if (pdata->size_y > 12) {
660 dev_err(&client->dev, "invalid y size %d specified\n",
661 pdata->size_y);
662 return -EINVAL;
663 }
664
665 lm = kzalloc(sizeof *lm, GFP_KERNEL);
666 idev = input_allocate_device();
667 if (!lm || !idev) {
668 err = -ENOMEM;
669 goto fail1;
670 }
671
672 i2c_set_clientdata(client, lm);
673
674 lm->client = client;
675 lm->idev = idev;
676 mutex_init(&lm->lock);
677 INIT_WORK(&lm->work, lm8323_work);
678
679 lm->size_x = pdata->size_x;
680 lm->size_y = pdata->size_y;
681 dev_vdbg(&client->dev, "Keypad size: %d x %d\n",
682 lm->size_x, lm->size_y);
683
684 lm->debounce_time = pdata->debounce_time;
685 lm->active_time = pdata->active_time;
686
687 lm8323_reset(lm);
688
689 /* Nothing's set up to service the IRQ yet, so just spin for max.
690 * 100ms until we can configure. */
691 tmo = jiffies + msecs_to_jiffies(100);
692 while (lm8323_read(lm, LM8323_CMD_READ_INT, data, 1) == 1) {
693 if (data[0] & INT_NOINIT)
694 break;
695
696 if (time_after(jiffies, tmo)) {
697 dev_err(&client->dev,
698 "timeout waiting for initialisation\n");
699 break;
700 }
701
702 msleep(1);
703 }
704
705 lm8323_configure(lm);
706
707 /* If a true probe check the device */
708 if (lm8323_read_id(lm, data) != 0) {
709 dev_err(&client->dev, "device not found\n");
710 err = -ENODEV;
711 goto fail1;
712 }
713
714 for (i = 0; i < LM8323_NUM_PWMS; i++) {
715 err = init_pwm(lm, i + 1, &client->dev, pdata->pwm_names[i]);
716 if (err < 0)
717 goto fail2;
718 }
719
720 lm->kp_enabled = true;
721 err = device_create_file(&client->dev, &dev_attr_disable_kp);
722 if (err < 0)
723 goto fail2;
724
725 idev->name = pdata->name ? : "LM8323 keypad";
726 snprintf(lm->phys, sizeof(lm->phys),
727 "%s/input-kp", dev_name(&client->dev));
728 idev->phys = lm->phys;
729
730 idev->evbit[0] = BIT(EV_KEY) | BIT(EV_MSC);
731 __set_bit(MSC_SCAN, idev->mscbit);
732 for (i = 0; i < LM8323_KEYMAP_SIZE; i++) {
733 __set_bit(pdata->keymap[i], idev->keybit);
734 lm->keymap[i] = pdata->keymap[i];
735 }
736 __clear_bit(KEY_RESERVED, idev->keybit);
737
738 if (pdata->repeat)
739 __set_bit(EV_REP, idev->evbit);
740
741 err = input_register_device(idev);
742 if (err) {
743 dev_dbg(&client->dev, "error registering input device\n");
744 goto fail3;
745 }
746
747 err = request_irq(client->irq, lm8323_irq,
748 IRQF_TRIGGER_FALLING | IRQF_DISABLED,
749 "lm8323", lm);
750 if (err) {
751 dev_err(&client->dev, "could not get IRQ %d\n", client->irq);
752 goto fail4;
753 }
754
755 device_init_wakeup(&client->dev, 1);
756 enable_irq_wake(client->irq);
757
758 return 0;
759
760fail4:
761 input_unregister_device(idev);
762 idev = NULL;
763fail3:
764 device_remove_file(&client->dev, &dev_attr_disable_kp);
765fail2:
766 while (--i >= 0)
767 if (lm->pwm[i].enabled)
768 led_classdev_unregister(&lm->pwm[i].cdev);
769fail1:
770 input_free_device(idev);
771 kfree(lm);
772 return err;
773}
774
775static int __devexit lm8323_remove(struct i2c_client *client)
776{
777 struct lm8323_chip *lm = i2c_get_clientdata(client);
778 int i;
779
780 disable_irq_wake(client->irq);
781 free_irq(client->irq, lm);
782 cancel_work_sync(&lm->work);
783
784 input_unregister_device(lm->idev);
785
786 device_remove_file(&lm->client->dev, &dev_attr_disable_kp);
787
788 for (i = 0; i < 3; i++)
789 if (lm->pwm[i].enabled)
790 led_classdev_unregister(&lm->pwm[i].cdev);
791
792 kfree(lm);
793
794 return 0;
795}
796
797#ifdef CONFIG_PM
798/*
799 * We don't need to explicitly suspend the chip, as it already switches off
800 * when there's no activity.
801 */
802static int lm8323_suspend(struct i2c_client *client, pm_message_t mesg)
803{
804 struct lm8323_chip *lm = i2c_get_clientdata(client);
805 int i;
806
807 set_irq_wake(client->irq, 0);
808 disable_irq(client->irq);
809
810 mutex_lock(&lm->lock);
811 lm->pm_suspend = true;
812 mutex_unlock(&lm->lock);
813
814 for (i = 0; i < 3; i++)
815 if (lm->pwm[i].enabled)
816 led_classdev_suspend(&lm->pwm[i].cdev);
817
818 return 0;
819}
820
821static int lm8323_resume(struct i2c_client *client)
822{
823 struct lm8323_chip *lm = i2c_get_clientdata(client);
824 int i;
825
826 mutex_lock(&lm->lock);
827 lm->pm_suspend = false;
828 mutex_unlock(&lm->lock);
829
830 for (i = 0; i < 3; i++)
831 if (lm->pwm[i].enabled)
832 led_classdev_resume(&lm->pwm[i].cdev);
833
834 enable_irq(client->irq);
835 set_irq_wake(client->irq, 1);
836
837 return 0;
838}
839#else
840#define lm8323_suspend NULL
841#define lm8323_resume NULL
842#endif
843
844static const struct i2c_device_id lm8323_id[] = {
845 { "lm8323", 0 },
846 { }
847};
848
849static struct i2c_driver lm8323_i2c_driver = {
850 .driver = {
851 .name = "lm8323",
852 },
853 .probe = lm8323_probe,
854 .remove = __devexit_p(lm8323_remove),
855 .suspend = lm8323_suspend,
856 .resume = lm8323_resume,
857 .id_table = lm8323_id,
858};
859MODULE_DEVICE_TABLE(i2c, lm8323_id);
860
861static int __init lm8323_init(void)
862{
863 return i2c_add_driver(&lm8323_i2c_driver);
864}
865module_init(lm8323_init);
866
867static void __exit lm8323_exit(void)
868{
869 i2c_del_driver(&lm8323_i2c_driver);
870}
871module_exit(lm8323_exit);
872
873MODULE_AUTHOR("Timo O. Karjalainen <timo.o.karjalainen@nokia.com>");
874MODULE_AUTHOR("Daniel Stone");
875MODULE_AUTHOR("Felipe Balbi <felipe.balbi@nokia.com>");
876MODULE_DESCRIPTION("LM8323 keypad driver");
877MODULE_LICENSE("GPL");
878