]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/input/keyboard/lm8323.c
Merge branch 'omap-fixes'
[linux-2.6-omap-h63xx.git] / drivers / input / keyboard / lm8323.c
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
129 struct lm8323_pwm {
130         int                     id;
131         int                     enabled;
132         int                     fade_time;
133         int                     brightness;
134         int                     desired_brightness;
135         int                     running;
136         /* pwm lock */
137         struct mutex            lock;
138         struct work_struct      work;
139         struct led_classdev     cdev;
140 };
141
142 struct lm8323_chip {
143         /* device lock */
144         struct mutex            lock;
145         struct i2c_client       *client;
146         struct work_struct      work;
147         struct input_dev        *idev;
148         unsigned                kp_enabled:1;
149         unsigned                pm_suspend:1;
150         unsigned                keys_down;
151         char                    phys[32];
152         s16                     keymap[LM8323_KEYMAP_SIZE];
153         int                     size_x;
154         int                     size_y;
155         int                     debounce_time;
156         int                     active_time;
157         struct lm8323_pwm       pwm1;
158         struct lm8323_pwm       pwm2;
159         struct lm8323_pwm       pwm3;
160 };
161
162 #define client_to_lm8323(c)     container_of(c, struct lm8323_chip, client)
163 #define dev_to_lm8323(d)        container_of(d, struct lm8323_chip, client->dev)
164 #define work_to_lm8323(w)       container_of(w, struct lm8323_chip, work)
165 #define cdev_to_pwm(c)          container_of(c, struct lm8323_pwm, cdev)
166 #define work_to_pwm(w)          container_of(w, struct lm8323_pwm, work)
167
168 static struct lm8323_chip *pwm_to_lm8323(struct lm8323_pwm *pwm)
169 {
170         switch (pwm->id) {
171         case 1:
172                 return container_of(pwm, struct lm8323_chip, pwm1);
173         case 2:
174                 return container_of(pwm, struct lm8323_chip, pwm2);
175         case 3:
176                 return container_of(pwm, struct lm8323_chip, pwm3);
177         default:
178                 return NULL;
179         }
180 }
181
182 #define LM8323_MAX_DATA 8
183
184 /*
185  * To write, we just access the chip's address in write mode, and dump the
186  * command and data out on the bus.  The command byte and data are taken as
187  * sequential u8s out of varargs, to a maximum of LM8323_MAX_DATA.
188  */
189 static int lm8323_write(struct lm8323_chip *lm, int len, ...)
190 {
191         int ret, i;
192         va_list ap;
193         u8 data[LM8323_MAX_DATA];
194
195         va_start(ap, len);
196
197         if (unlikely(len > LM8323_MAX_DATA)) {
198                 dev_err(&lm->client->dev, "tried to send %d bytes\n", len);
199                 va_end(ap);
200                 return 0;
201         }
202
203         for (i = 0; i < len; i++)
204                 data[i] = va_arg(ap, int);
205
206         va_end(ap);
207
208         /*
209          * If the host is asleep while we send the data, we can get a NACK
210          * back while it wakes up, so try again, once.
211          */
212         ret = i2c_master_send(lm->client, data, len);
213         if (unlikely(ret == -EREMOTEIO))
214                 ret = i2c_master_send(lm->client, data, len);
215         if (unlikely(ret != len))
216                 dev_err(&lm->client->dev, "sent %d bytes of %d total\n",
217                         len, ret);
218
219         return ret;
220 }
221
222 /*
223  * To read, we first send the command byte to the chip and end the transaction,
224  * then access the chip in read mode, at which point it will send the data.
225  */
226 static int lm8323_read(struct lm8323_chip *lm, u8 cmd, u8 *buf, int len)
227 {
228         int ret;
229
230         /*
231          * If the host is asleep while we send the byte, we can get a NACK
232          * back while it wakes up, so try again, once.
233          */
234         ret = i2c_master_send(lm->client, &cmd, 1);
235         if (unlikely(ret == -EREMOTEIO))
236                 ret = i2c_master_send(lm->client, &cmd, 1);
237         if (unlikely(ret != 1)) {
238                 dev_err(&lm->client->dev, "sending read cmd 0x%02x failed\n",
239                         cmd);
240                 return 0;
241         }
242
243         ret = i2c_master_recv(lm->client, buf, len);
244         if (unlikely(ret != len))
245                 dev_err(&lm->client->dev, "wanted %d bytes, got %d\n",
246                         len, ret);
247
248         return ret;
249 }
250
251 /*
252  * Set the chip active time (idle time before it enters halt).
253  */
254 static void lm8323_set_active_time(struct lm8323_chip *lm, int time)
255 {
256         lm8323_write(lm, 2, LM8323_CMD_SET_ACTIVE, time >> 2);
257 }
258
259 /*
260  * The signals are AT-style: the low 7 bits are the keycode, and the top
261  * bit indicates the state (1 for down, 0 for up).
262  */
263 static inline u8 lm8323_whichkey(u8 event)
264 {
265         return event & 0x7f;
266 }
267
268 static inline int lm8323_ispress(u8 event)
269 {
270         return (event & 0x80) ? 1 : 0;
271 }
272
273 static void process_keys(struct lm8323_chip *lm)
274 {
275         u8 event;
276         u8 key_fifo[LM8323_FIFO_LEN + 1];
277         int old_keys_down = lm->keys_down;
278         int ret;
279         int i = 0;
280
281         /*
282          * Read all key events from the FIFO at once. Next READ_FIFO clears the
283          * FIFO even if we didn't read all events previously.
284          */
285         ret = lm8323_read(lm, LM8323_CMD_READ_FIFO, key_fifo, LM8323_FIFO_LEN);
286
287         if (ret < 0) {
288                 dev_err(&lm->client->dev, "Failed reading fifo \n");
289                 return;
290         }
291         key_fifo[ret] = 0;
292
293         while ((event = key_fifo[i])) {
294                 u8 key = lm8323_whichkey(event);
295                 int isdown = lm8323_ispress(event);
296                 s16 keycode = lm->keymap[key];
297
298                 if (likely(keycode > 0)) {
299                         dev_vdbg(&lm->client->dev, "key 0x%02x %s\n", key,
300                               isdown ? "down" : "up");
301                         if (likely(lm->kp_enabled)) {
302                                 input_report_key(lm->idev, keycode, isdown);
303                                 input_sync(lm->idev);
304                         }
305                         if (isdown)
306                                 lm->keys_down++;
307                         else
308                                 lm->keys_down--;
309                 } else {
310                         dev_err(&lm->client->dev, "keycode 0x%02x not mapped "
311                                 "to any key\n", key);
312                 }
313                 i++;
314         }
315
316         /*
317          * Errata: We need to ensure that the chip never enters halt mode
318          * during a keypress, so set active time to 0.  When it's released,
319          * we can enter halt again, so set the active time back to normal.
320          */
321         if (!old_keys_down && lm->keys_down)
322                 lm8323_set_active_time(lm, 0);
323         if (old_keys_down && !lm->keys_down)
324                 lm8323_set_active_time(lm, lm->active_time);
325 }
326
327 static void lm8323_process_error(struct lm8323_chip *lm)
328 {
329         u8 error;
330
331         if (lm8323_read(lm, LM8323_CMD_READ_ERR, &error, 1) == 1) {
332                 if (error & ERR_FIFOOVER)
333                         dev_vdbg(&lm->client->dev, "fifo overflow!\n");
334                 if (error & ERR_KEYOVR)
335                         dev_vdbg(&lm->client->dev,
336                                         "more than two keys pressed\n");
337                 if (error & ERR_CMDUNK)
338                         dev_vdbg(&lm->client->dev,
339                                         "unknown command submitted\n");
340                 if (error & ERR_BADPAR)
341                         dev_vdbg(&lm->client->dev, "bad command parameter\n");
342         }
343 }
344
345 static void lm8323_reset(struct lm8323_chip *lm)
346 {
347         /* The docs say we must pass 0xAA as the data byte. */
348         lm8323_write(lm, 2, LM8323_CMD_RESET, 0xAA);
349 }
350
351 static int lm8323_configure(struct lm8323_chip *lm)
352 {
353         int keysize = (lm->size_x << 4) | lm->size_y;
354         int clock = (CLK_SLOWCLKEN | CLK_RCPWM_EXTERNAL);
355         int debounce = lm->debounce_time >> 2;
356         int active = lm->active_time >> 2;
357
358         /*
359          * Active time must be greater than the debounce time: if it's
360          * a close-run thing, give ourselves a 12ms buffer.
361          */
362         if (debounce >= active)
363                 active = debounce + 3;
364
365         lm8323_write(lm, 2, LM8323_CMD_WRITE_CFG, 0);
366         lm8323_write(lm, 2, LM8323_CMD_WRITE_CLOCK, clock);
367         lm8323_write(lm, 2, LM8323_CMD_SET_KEY_SIZE, keysize);
368         lm8323_set_active_time(lm, lm->active_time);
369         lm8323_write(lm, 2, LM8323_CMD_SET_DEBOUNCE, debounce);
370         lm8323_write(lm, 3, LM8323_CMD_WRITE_PORT_STATE, 0xff, 0xff);
371         lm8323_write(lm, 3, LM8323_CMD_WRITE_PORT_SEL, 0, 0);
372
373         /*
374          * Not much we can do about errors at this point, so just hope
375          * for the best.
376          */
377
378         return 0;
379 }
380
381 static void pwm_done(struct lm8323_pwm *pwm)
382 {
383         mutex_lock(&pwm->lock);
384         pwm->running = 0;
385         if (pwm->desired_brightness != pwm->brightness)
386                 schedule_work(&pwm->work);
387         mutex_unlock(&pwm->lock);
388 }
389
390 /*
391  * Bottom half: handle the interrupt by posting key events, or dealing with
392  * errors appropriately.
393  */
394 static void lm8323_work(struct work_struct *work)
395 {
396         struct lm8323_chip *lm = work_to_lm8323(work);
397         u8 ints;
398
399         mutex_lock(&lm->lock);
400
401         while ((lm8323_read(lm, LM8323_CMD_READ_INT, &ints, 1) == 1) && ints) {
402                 if (likely(ints & INT_KEYPAD))
403                         process_keys(lm);
404                 if (ints & INT_ROTATOR) {
405                         /* We don't currently support the rotator. */
406                         dev_vdbg(&lm->client->dev, "rotator fired\n");
407                 }
408                 if (ints & INT_ERROR) {
409                         dev_vdbg(&lm->client->dev, "error!\n");
410                         lm8323_process_error(lm);
411                 }
412                 if (ints & INT_NOINIT) {
413                         dev_err(&lm->client->dev, "chip lost config; "
414                                                   "reinitialising\n");
415                         lm8323_configure(lm);
416                 }
417                 if (ints & INT_PWM1) {
418                         dev_vdbg(&lm->client->dev, "pwm1 engine completed\n");
419                         pwm_done(&lm->pwm1);
420                 }
421                 if (ints & INT_PWM2) {
422                         dev_vdbg(&lm->client->dev, "pwm2 engine completed\n");
423                         pwm_done(&lm->pwm2);
424                 }
425                 if (ints & INT_PWM3) {
426                         dev_vdbg(&lm->client->dev, "pwm3 engine completed\n");
427                         pwm_done(&lm->pwm3);
428                 }
429         }
430
431         mutex_unlock(&lm->lock);
432 }
433
434 /*
435  * We cannot use I2C in interrupt context, so we just schedule work.
436  */
437 static irqreturn_t lm8323_irq(int irq, void *data)
438 {
439         struct lm8323_chip *lm = data;
440
441         schedule_work(&lm->work);
442
443         return IRQ_HANDLED;
444 }
445
446 /*
447  * Read the chip ID.
448  */
449 static int lm8323_read_id(struct lm8323_chip *lm, u8 *buf)
450 {
451         int bytes;
452
453         bytes = lm8323_read(lm, LM8323_CMD_READ_ID, buf, 2);
454         if (unlikely(bytes != 2))
455                 return -EIO;
456
457         return 0;
458 }
459
460 static void lm8323_write_pwm_one(struct lm8323_pwm *pwm, int pos, u16 cmd)
461 {
462         struct lm8323_chip *lm = pwm_to_lm8323(pwm);
463
464         lm8323_write(lm, 4, LM8323_CMD_PWM_WRITE, (pos << 2) | pwm->id,
465                      (cmd & 0xff00) >> 8, cmd & 0x00ff);
466 }
467
468 /*
469  * Write a script into a given PWM engine, concluding with PWM_END.
470  * If 'kill' is nonzero, the engine will be shut down at the end
471  * of the script, producing a zero output. Otherwise the engine
472  * will be kept running at the final PWM level indefinitely.
473  */
474 static void lm8323_write_pwm(struct lm8323_pwm *pwm, int kill,
475                              int len, const u16 *cmds)
476 {
477         struct lm8323_chip *lm = pwm_to_lm8323(pwm);
478         int i;
479
480         for (i = 0; i < len; i++)
481                 lm8323_write_pwm_one(pwm, i, cmds[i]);
482
483         lm8323_write_pwm_one(pwm, i++, PWM_END(kill));
484         lm8323_write(lm, 2, LM8323_CMD_START_PWM, pwm->id);
485         pwm->running = 1;
486 }
487
488 static void lm8323_pwm_work(struct work_struct *work)
489 {
490         struct lm8323_pwm *pwm = work_to_pwm(work);
491         int div512, perstep, steps, hz, up, kill;
492         u16 pwm_cmds[3];
493         int num_cmds = 0;
494
495         mutex_lock(&pwm->lock);
496
497         /*
498          * Do nothing if we're already at the requested level,
499          * or previous setting is not yet complete. In the latter
500          * case we will be called again when the previous PWM script
501          * finishes.
502          */
503         if (pwm->running || pwm->desired_brightness == pwm->brightness) {
504                 mutex_unlock(&pwm->lock);
505                 return;
506         }
507
508         kill = (pwm->desired_brightness == 0);
509         up = (pwm->desired_brightness > pwm->brightness);
510         steps = abs(pwm->desired_brightness - pwm->brightness);
511
512         /*
513          * Convert time (in ms) into a divisor (512 or 16 on a refclk of
514          * 32768Hz), and number of ticks per step.
515          */
516         if ((pwm->fade_time / steps) > (32768 / 512)) {
517                 div512 = 1;
518                 hz = 32768 / 512;
519         } else {
520                 div512 = 0;
521                 hz = 32768 / 16;
522         }
523
524         perstep = (hz * pwm->fade_time) / (steps * 1000);
525
526         if (perstep == 0)
527                 perstep = 1;
528         else if (perstep > 63)
529                 perstep = 63;
530
531         while (steps) {
532                 int s;
533
534                 s = min(126, steps);
535                 pwm_cmds[num_cmds++] = PWM_RAMP(div512, perstep, s, up);
536                 steps -= s;
537         }
538
539         lm8323_write_pwm(pwm, kill, num_cmds, pwm_cmds);
540
541         pwm->brightness = pwm->desired_brightness;
542         mutex_unlock(&pwm->lock);
543 }
544
545 static void lm8323_pwm_set_brightness(struct led_classdev *led_cdev,
546                                       enum led_brightness brightness)
547 {
548         struct lm8323_pwm *pwm = cdev_to_pwm(led_cdev);
549         struct lm8323_chip *lm = pwm_to_lm8323(pwm);
550
551         mutex_lock(&pwm->lock);
552         pwm->desired_brightness = brightness;
553         mutex_unlock(&pwm->lock);
554
555         if (in_interrupt()) {
556                 schedule_work(&pwm->work);
557         } else {
558                 /*
559                  * Schedule PWM work as usual unless we are going into suspend
560                  */
561                 mutex_lock(&lm->lock);
562                 if (likely(!lm->pm_suspend))
563                         schedule_work(&pwm->work);
564                 else
565                         lm8323_pwm_work(&pwm->work);
566                 mutex_unlock(&lm->lock);
567         }
568 }
569
570 static ssize_t lm8323_pwm_show_time(struct device *dev,
571                 struct device_attribute *attr, char *buf)
572 {
573         struct led_classdev *led_cdev = dev_get_drvdata(dev);
574         struct lm8323_pwm *pwm = cdev_to_pwm(led_cdev);
575
576         return sprintf(buf, "%d\n", pwm->fade_time);
577 }
578
579 static ssize_t lm8323_pwm_store_time(struct device *dev,
580                 struct device_attribute *attr, const char *buf, size_t len)
581 {
582         struct led_classdev *led_cdev = dev_get_drvdata(dev);
583         struct lm8323_pwm *pwm = cdev_to_pwm(led_cdev);
584         int ret;
585         int time;
586
587         ret = strict_strtoul(buf, 10, &time);
588         /* Numbers only, please. */
589         if (ret)
590                 return -EINVAL;
591
592         pwm->fade_time = time;
593
594         return strlen(buf);
595 }
596 static DEVICE_ATTR(time, 0644, lm8323_pwm_show_time, lm8323_pwm_store_time);
597
598 static int init_pwm(struct lm8323_chip *lm, int id, struct device *dev,
599                     const char *name)
600 {
601         struct lm8323_pwm *pwm = NULL;
602
603         BUG_ON(id > 3);
604
605         switch (id) {
606         case 1:
607                 pwm = &lm->pwm1;
608                 break;
609         case 2:
610                 pwm = &lm->pwm2;
611                 break;
612         case 3:
613                 pwm = &lm->pwm3;
614                 break;
615         }
616
617         pwm->id = id;
618         pwm->fade_time = 0;
619         pwm->brightness = 0;
620         pwm->desired_brightness = 0;
621         pwm->running = 0;
622         mutex_init(&pwm->lock);
623         if (name) {
624                 pwm->cdev.name = name;
625                 pwm->cdev.brightness_set = lm8323_pwm_set_brightness;
626                 if (led_classdev_register(dev, &pwm->cdev) < 0) {
627                         dev_err(dev, "couldn't register PWM %d\n", id);
628                         return -1;
629                 }
630                 if (device_create_file(pwm->cdev.dev,
631                                              &dev_attr_time) < 0) {
632                         dev_err(dev, "couldn't register time attribute\n");
633                         led_classdev_unregister(&pwm->cdev);
634                         return -1;
635                 }
636                 INIT_WORK(&pwm->work, lm8323_pwm_work);
637                 pwm->enabled = 1;
638         } else {
639                 pwm->enabled = 0;
640         }
641
642         return 0;
643 }
644
645 static struct i2c_driver lm8323_i2c_driver;
646
647 static ssize_t lm8323_show_disable(struct device *dev,
648                                    struct device_attribute *attr, char *buf)
649 {
650         struct lm8323_chip *lm = dev_get_drvdata(dev);
651
652         return sprintf(buf, "%u\n", !lm->kp_enabled);
653 }
654
655 static ssize_t lm8323_set_disable(struct device *dev,
656                                   struct device_attribute *attr,
657                                   const char *buf, size_t count)
658 {
659         struct lm8323_chip *lm = dev_get_drvdata(dev);
660         int ret;
661         int i;
662
663         ret = strict_strtoul(buf, 10, &i);
664
665         mutex_lock(&lm->lock);
666         lm->kp_enabled = !i;
667         mutex_unlock(&lm->lock);
668
669         return count;
670 }
671 static DEVICE_ATTR(disable_kp, 0644, lm8323_show_disable, lm8323_set_disable);
672
673 static int lm8323_probe(struct i2c_client *client,
674                 const struct i2c_device_id *id)
675 {
676         struct lm8323_platform_data *pdata;
677         struct input_dev *idev;
678         struct lm8323_chip *lm;
679         int i, err = 0;
680         unsigned long tmo;
681         u8 data[2];
682
683         lm = kzalloc(sizeof *lm, GFP_KERNEL);
684         if (!lm)
685                 return -ENOMEM;
686
687         i2c_set_clientdata(client, lm);
688         lm->client = client;
689         pdata = client->dev.platform_data;
690         if (!pdata || !pdata->size_x || !pdata->size_y) {
691                 dev_err(&client->dev, "missing platform_data\n");
692                 err = -EINVAL;
693                 goto fail2;
694         }
695
696         lm->size_x = pdata->size_x;
697         if (lm->size_x > 8) {
698                 dev_err(&client->dev, "invalid x size %d specified\n",
699                                 lm->size_x);
700                 err = -EINVAL;
701                 goto fail2;
702         }
703
704         lm->size_y = pdata->size_y;
705         if (lm->size_y > 12) {
706                 dev_err(&client->dev, "invalid y size %d specified\n",
707                                 lm->size_y);
708                 err = -EINVAL;
709                 goto fail2;
710         }
711
712         dev_vdbg(&client->dev, "Keypad size: %d x %d\n",
713                         lm->size_x, lm->size_y);
714
715         lm->debounce_time = pdata->debounce_time;
716         lm->active_time = pdata->active_time;
717
718         lm8323_reset(lm);
719
720         /* Nothing's set up to service the IRQ yet, so just spin for max.
721          * 100ms until we can configure. */
722         tmo = jiffies + msecs_to_jiffies(100);
723         while (lm8323_read(lm, LM8323_CMD_READ_INT, data, 1) == 1) {
724                 if (data[0] & INT_NOINIT)
725                         break;
726
727                 if (time_after(jiffies, tmo)) {
728                         dev_err(&client->dev,
729                                         "timeout waiting for initialisation\n");
730                         break;
731                 }
732
733                 msleep(1);
734         }
735         lm8323_configure(lm);
736
737         /* If a true probe check the device */
738         if (lm8323_read_id(lm, data) != 0) {
739                 dev_err(&client->dev, "device not found\n");
740                 err = -ENODEV;
741                 goto fail2;
742         }
743
744         if (init_pwm(lm, 1, &client->dev, pdata->pwm1_name) < 0)
745                 goto fail3;
746         if (init_pwm(lm, 2, &client->dev, pdata->pwm2_name) < 0)
747                 goto fail4;
748         if (init_pwm(lm, 3, &client->dev, pdata->pwm3_name) < 0)
749                 goto fail5;
750
751         mutex_init(&lm->lock);
752         INIT_WORK(&lm->work, lm8323_work);
753
754         err = request_irq(client->irq, lm8323_irq,
755                           IRQF_TRIGGER_FALLING | IRQF_DISABLED,
756                           "lm8323", lm);
757         if (err) {
758                 dev_err(&client->dev, "could not get IRQ %d\n", client->irq);
759                 goto fail6;
760         }
761
762         device_init_wakeup(&client->dev, 1);
763         enable_irq_wake(client->irq);
764
765         lm->kp_enabled = 1;
766         err = device_create_file(&client->dev, &dev_attr_disable_kp);
767         if (err < 0)
768                 goto fail7;
769
770         idev = input_allocate_device();
771         if (!idev) {
772                 err = -ENOMEM;
773                 goto fail8;
774         }
775
776         if (pdata->name)
777                 idev->name = pdata->name;
778         else
779                 idev->name = "LM8323 keypad";
780         snprintf(lm->phys, sizeof(lm->phys), "%s/input-kp", dev_name(&client->dev));
781         idev->phys = lm->phys;
782
783         lm->keys_down = 0;
784         idev->evbit[0] = BIT(EV_KEY);
785         for (i = 0; i < LM8323_KEYMAP_SIZE; i++) {
786                 if (pdata->keymap[i] > 0)
787                         __set_bit(pdata->keymap[i], idev->keybit);
788
789                 lm->keymap[i] = pdata->keymap[i];
790         }
791
792         if (pdata->repeat)
793                 __set_bit(EV_REP, idev->evbit);
794
795         lm->idev = idev;
796         err = input_register_device(idev);
797         if (err) {
798                 dev_dbg(&client->dev, "error registering input device\n");
799                 goto fail8;
800         }
801
802         return 0;
803
804 fail8:
805         device_remove_file(&client->dev, &dev_attr_disable_kp);
806 fail7:
807         free_irq(client->irq, lm);
808 fail6:
809         if (lm->pwm3.enabled)
810                 led_classdev_unregister(&lm->pwm3.cdev);
811 fail5:
812         if (lm->pwm2.enabled)
813                 led_classdev_unregister(&lm->pwm2.cdev);
814 fail4:
815         if (lm->pwm1.enabled)
816                 led_classdev_unregister(&lm->pwm1.cdev);
817 fail3:
818 fail2:
819         kfree(lm);
820         return err;
821 }
822
823 static int lm8323_remove(struct i2c_client *client)
824 {
825         struct lm8323_chip *lm = i2c_get_clientdata(client);
826
827         disable_irq_wake(client->irq);
828         free_irq(client->irq, lm);
829         cancel_work_sync(&lm->work);
830         input_unregister_device(lm->idev);
831         device_remove_file(&lm->client->dev, &dev_attr_disable_kp);
832         if (lm->pwm3.enabled)
833                 led_classdev_unregister(&lm->pwm3.cdev);
834         if (lm->pwm2.enabled)
835                 led_classdev_unregister(&lm->pwm2.cdev);
836         if (lm->pwm1.enabled)
837                 led_classdev_unregister(&lm->pwm1.cdev);
838         kfree(lm);
839
840         return 0;
841 }
842
843 #ifdef CONFIG_PM
844 /*
845  * We don't need to explicitly suspend the chip, as it already switches off
846  * when there's no activity.
847  */
848 static int lm8323_suspend(struct i2c_client *client, pm_message_t mesg)
849 {
850         struct lm8323_chip *lm = i2c_get_clientdata(client);
851
852         set_irq_wake(client->irq, 0);
853         disable_irq(client->irq);
854
855         mutex_lock(&lm->lock);
856         lm->pm_suspend = 1;
857         mutex_unlock(&lm->lock);
858
859         if (lm->pwm1.enabled)
860                 led_classdev_suspend(&lm->pwm1.cdev);
861         if (lm->pwm2.enabled)
862                 led_classdev_suspend(&lm->pwm2.cdev);
863         if (lm->pwm3.enabled)
864                 led_classdev_suspend(&lm->pwm3.cdev);
865
866         return 0;
867 }
868
869 static int lm8323_resume(struct i2c_client *client)
870 {
871         struct lm8323_chip *lm = i2c_get_clientdata(client);
872
873         mutex_lock(&lm->lock);
874         lm->pm_suspend = 0;
875         mutex_unlock(&lm->lock);
876
877         if (lm->pwm1.enabled)
878                 led_classdev_resume(&lm->pwm1.cdev);
879         if (lm->pwm2.enabled)
880                 led_classdev_resume(&lm->pwm2.cdev);
881         if (lm->pwm3.enabled)
882                 led_classdev_resume(&lm->pwm3.cdev);
883
884         enable_irq(client->irq);
885         set_irq_wake(client->irq, 1);
886
887         return 0;
888 }
889 #else
890 #define lm8323_suspend  NULL
891 #define lm8323_resume   NULL
892 #endif
893
894 static const struct i2c_device_id lm8323_id[] = {
895         { "lm8323", 0 },
896         { }
897 };
898
899 static struct i2c_driver lm8323_i2c_driver = {
900         .driver = {
901                 .name    = "lm8323",
902         },
903         .probe          = lm8323_probe,
904         .remove         = lm8323_remove,
905         .suspend        = lm8323_suspend,
906         .resume         = lm8323_resume,
907         .id_table       = lm8323_id,
908 };
909 MODULE_DEVICE_TABLE(i2c, lm8323_id);
910
911 static int __init lm8323_init(void)
912 {
913         return i2c_add_driver(&lm8323_i2c_driver);
914 }
915 module_init(lm8323_init);
916
917 static void __exit lm8323_exit(void)
918 {
919         i2c_del_driver(&lm8323_i2c_driver);
920 }
921 module_exit(lm8323_exit);
922
923 MODULE_AUTHOR("Timo O. Karjalainen <timo.o.karjalainen@nokia.com>");
924 MODULE_AUTHOR("Daniel Stone");
925 MODULE_AUTHOR("Felipe Balbi <felipe.balbi@nokia.com>");
926 MODULE_DESCRIPTION("LM8323 keypad driver");
927 MODULE_LICENSE("GPL");
928