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