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