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