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