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