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