2 * drivers/i2c/chips/lm8323.c
4 * Copyright (C) 2007 Nokia Corporation
6 * Written by Daniel Stone <daniel.stone@nokia.com>
7 * Timo O. Karjalainen <timo.o.karjalainen@nokia.com>
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).
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.
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
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>
33 /* Commands to send to the chip. */
34 #define LM8323_CMD_READ_ID 0x80 /* Read chip ID. */
35 #define LM8323_CMD_WRITE_CFG 0x81 /* Set configuration item. */
36 #define LM8323_CMD_READ_INT 0x82 /* Get interrupt status. */
37 #define LM8323_CMD_RESET 0x83 /* Reset, same as external one */
38 #define LM8323_CMD_WRITE_PORT_SEL 0x85 /* Set GPIO in/out. */
39 #define LM8323_CMD_WRITE_PORT_STATE 0x86 /* Set GPIO pullup. */
40 #define LM8323_CMD_READ_PORT_SEL 0x87 /* Get GPIO in/out. */
41 #define LM8323_CMD_READ_PORT_STATE 0x88 /* Get GPIO pullup. */
42 #define LM8323_CMD_READ_FIFO 0x89 /* Read byte from FIFO. */
43 #define LM8323_CMD_RPT_READ_FIFO 0x8a /* Read FIFO (no increment). */
44 #define LM8323_CMD_SET_ACTIVE 0x8b /* Set active time. */
45 #define LM8323_CMD_READ_ERR 0x8c /* Get error status. */
46 #define LM8323_CMD_READ_ROTATOR 0x8e /* Read rotator status. */
47 #define LM8323_CMD_SET_DEBOUNCE 0x8f /* Set debouncing time. */
48 #define LM8323_CMD_SET_KEY_SIZE 0x90 /* Set keypad size. */
49 #define LM8323_CMD_READ_KEY_SIZE 0x91 /* Get keypad size. */
50 #define LM8323_CMD_READ_CFG 0x92 /* Get configuration item. */
51 #define LM8323_CMD_WRITE_CLOCK 0x93 /* Set clock config. */
52 #define LM8323_CMD_READ_CLOCK 0x94 /* Get clock config. */
53 #define LM8323_CMD_PWM_WRITE 0x95 /* Write PWM script. */
54 #define LM8323_CMD_START_PWM 0x96 /* Start PWM engine. */
55 #define LM8323_CMD_STOP_PWM 0x97 /* Stop PWM engine. */
57 /* Interrupt status. */
58 #define INT_KEYPAD 0x01 /* Key event. */
59 #define INT_ROTATOR 0x02 /* Rotator event. */
60 #define INT_ERROR 0x08 /* Error: use CMD_READ_ERR. */
61 #define INT_NOINIT 0x10 /* Lost configuration. */
62 #define INT_PWM1 0x20 /* PWM1 stopped. */
63 #define INT_PWM2 0x40 /* PWM2 stopped. */
64 #define INT_PWM3 0x80 /* PWM3 stopped. */
66 /* Errors (signalled by INT_ERROR, read with CMD_READ_ERR). */
67 #define ERR_BADPAR 0x01 /* Bad parameter. */
68 #define ERR_CMDUNK 0x02 /* Unknown command. */
69 #define ERR_KEYOVR 0x04 /* Too many keys pressed. */
70 #define ERR_FIFOOVER 0x40 /* FIFO overflow. */
72 /* Configuration keys (CMD_{WRITE,READ}_CFG). */
73 #define CFG_MUX1SEL 0x01 /* Select MUX1_OUT input. */
74 #define CFG_MUX1EN 0x02 /* Enable MUX1_OUT. */
75 #define CFG_MUX2SEL 0x04 /* Select MUX2_OUT input. */
76 #define CFG_MUX2EN 0x08 /* Enable MUX2_OUT. */
77 #define CFG_PSIZE 0x20 /* Package size (must be 0). */
78 #define CFG_ROTEN 0x40 /* Enable rotator. */
80 /* Clock settings (CMD_{WRITE,READ}_CLOCK). */
81 #define CLK_RCPWM_INTERNAL 0x00
82 #define CLK_RCPWM_EXTERNAL 0x03
83 #define CLK_SLOWCLKEN 0x08 /* Enable 32.768kHz clock. */
84 #define CLK_SLOWCLKOUT 0x40 /* Enable slow pulse output. */
86 /* The possible addresses corresponding to CONFIG1 and CONFIG2 pin wirings. */
87 #define LM8323_I2C_ADDR00 (0x84 >> 1) /* 1000 010x */
88 #define LM8323_I2C_ADDR01 (0x86 >> 1) /* 1000 011x */
89 #define LM8323_I2C_ADDR10 (0x88 >> 1) /* 1000 100x */
90 #define LM8323_I2C_ADDR11 (0x8A >> 1) /* 1000 101x */
92 /* Key event fifo length */
93 #define LM8323_FIFO_LEN 15
95 /* Commands for PWM engine; feed in with PWM_WRITE. */
96 /* Load ramp counter from duty cycle field (range 0 - 0xff). */
97 #define PWM_SET(v) (0x4000 | ((v) & 0xff))
98 /* Go to start of script. */
99 #define PWM_GOTOSTART 0x0000
101 * Stop engine (generates interrupt). If reset is 1, clear the program
102 * counter, else leave it.
104 #define PWM_END(reset) (0xc000 | (!!(reset) << 11))
106 * Ramp. If s is 1, divide clock by 512, else divide clock by 16.
107 * Take t clock scales (up to 63) per step, for n steps (up to 126).
108 * If u is set, ramp up, else ramp down.
110 #define PWM_RAMP(s, t, n, u) ((!!(s) << 14) | ((t) & 0x3f) << 8 | \
111 ((n) & 0x7f) | ((u) ? 0 : 0x80))
113 * Loop (i.e. jump back to pos) for a given number of iterations (up to 63).
114 * If cnt is zero, execute until PWM_END is encountered.
116 #define PWM_LOOP(cnt, pos) (0xa000 | (((cnt) & 0x3f) << 7) | \
119 * Wait for trigger. Argument is a mask of channels, shifted by the channel
120 * number, e.g. 0xa for channels 3 and 1. Note that channels are numbered
123 #define PWM_WAIT_TRIG(chans) (0xe000 | (((chans) & 0x7) << 6))
124 /* Send trigger. Argument is same as PWM_WAIT_TRIG. */
125 #define PWM_SEND_TRIG(chans) (0xe000 | ((chans) & 0x7))
132 int desired_brightness;
135 struct work_struct work;
136 struct led_classdev cdev;
141 struct i2c_client *client;
142 struct work_struct work;
143 struct input_dev *idev;
144 unsigned kp_enabled : 1;
145 unsigned pm_suspend : 1;
148 s16 keymap[LM8323_KEYMAP_SIZE];
153 struct lm8323_pwm pwm1;
154 struct lm8323_pwm pwm2;
155 struct lm8323_pwm pwm3;
158 #define client_to_lm8323(c) container_of(c, struct lm8323_chip, client)
159 #define dev_to_lm8323(d) container_of(d, struct lm8323_chip, client->dev)
160 #define work_to_lm8323(w) container_of(w, struct lm8323_chip, work)
161 #define cdev_to_pwm(c) container_of(c, struct lm8323_pwm, cdev)
162 #define work_to_pwm(w) container_of(w, struct lm8323_pwm, work)
164 static struct lm8323_chip *pwm_to_lm8323(struct lm8323_pwm *pwm)
168 return container_of(pwm, struct lm8323_chip, pwm1);
170 return container_of(pwm, struct lm8323_chip, pwm2);
172 return container_of(pwm, struct lm8323_chip, pwm3);
178 #define LM8323_MAX_DATA 8
181 * To write, we just access the chip's address in write mode, and dump the
182 * command and data out on the bus. The command byte and data are taken as
183 * sequential u8s out of varargs, to a maximum of LM8323_MAX_DATA.
185 static int lm8323_write(struct lm8323_chip *lm, int len, ...)
189 u8 data[LM8323_MAX_DATA];
193 if (unlikely(len > LM8323_MAX_DATA)) {
194 dev_err(&lm->client->dev, "tried to send %d bytes\n", len);
199 for (i = 0; i < len; i++)
200 data[i] = va_arg(ap, int);
205 * If the host is asleep while we send the data, we can get a NACK
206 * back while it wakes up, so try again, once.
208 ret = i2c_master_send(lm->client, data, len);
209 if (unlikely(ret == -EREMOTEIO))
210 ret = i2c_master_send(lm->client, data, len);
211 if (unlikely(ret != len))
212 dev_err(&lm->client->dev, "sent %d bytes of %d total\n",
219 * To read, we first send the command byte to the chip and end the transaction,
220 * then access the chip in read mode, at which point it will send the data.
222 static int lm8323_read(struct lm8323_chip *lm, u8 cmd, u8 *buf, int len)
227 * If the host is asleep while we send the byte, we can get a NACK
228 * back while it wakes up, so try again, once.
230 ret = i2c_master_send(lm->client, &cmd, 1);
231 if (unlikely(ret == -EREMOTEIO))
232 ret = i2c_master_send(lm->client, &cmd, 1);
233 if (unlikely(ret != 1)) {
234 dev_err(&lm->client->dev, "sending read cmd 0x%02x failed\n",
239 ret = i2c_master_recv(lm->client, buf, len);
240 if (unlikely(ret != len))
241 dev_err(&lm->client->dev, "wanted %d bytes, got %d\n",
248 * Set the chip active time (idle time before it enters halt).
250 static void lm8323_set_active_time(struct lm8323_chip *lm, int time)
252 lm8323_write(lm, 2, LM8323_CMD_SET_ACTIVE, time >> 2);
256 * The signals are AT-style: the low 7 bits are the keycode, and the top
257 * bit indicates the state (1 for down, 0 for up).
259 static inline u8 lm8323_whichkey(u8 event)
264 static inline int lm8323_ispress(u8 event)
266 return (event & 0x80) ? 1 : 0;
269 static void process_keys(struct lm8323_chip *lm)
272 u8 key_fifo[LM8323_FIFO_LEN + 1];
273 int old_keys_down = lm->keys_down;
278 * Read all key events from the FIFO at once. Next READ_FIFO clears the
279 * FIFO even if we didn't read all events previously.
281 ret = lm8323_read(lm, LM8323_CMD_READ_FIFO, key_fifo, LM8323_FIFO_LEN);
284 dev_err(&lm->client->dev, "Failed reading fifo \n");
289 while ((event = key_fifo[i])) {
290 u8 key = lm8323_whichkey(event);
291 int isdown = lm8323_ispress(event);
292 s16 keycode = lm->keymap[key];
294 if (likely(keycode > 0)) {
295 dev_vdbg(&lm->client->dev, "key 0x%02x %s\n", key,
296 isdown ? "down" : "up");
297 if (likely(lm->kp_enabled)) {
298 input_report_key(lm->idev, keycode, isdown);
299 input_sync(lm->idev);
306 dev_err(&lm->client->dev, "keycode 0x%02x not mapped "
307 "to any key\n", key);
313 * Errata: We need to ensure that the chip never enters halt mode
314 * during a keypress, so set active time to 0. When it's released,
315 * we can enter halt again, so set the active time back to normal.
317 if (!old_keys_down && lm->keys_down)
318 lm8323_set_active_time(lm, 0);
319 if (old_keys_down && !lm->keys_down)
320 lm8323_set_active_time(lm, lm->active_time);
323 static void lm8323_process_error(struct lm8323_chip *lm)
327 if (lm8323_read(lm, LM8323_CMD_READ_ERR, &error, 1) == 1) {
328 if (error & ERR_FIFOOVER)
329 dev_vdbg(&lm->client->dev, "fifo overflow!\n");
330 if (error & ERR_KEYOVR)
331 dev_vdbg(&lm->client->dev, "more than two keys pressed\n");
332 if (error & ERR_CMDUNK)
333 dev_vdbg(&lm->client->dev, "unknown command submitted\n");
334 if (error & ERR_BADPAR)
335 dev_vdbg(&lm->client->dev, "bad command parameter\n");
339 static void lm8323_reset(struct lm8323_chip *lm)
341 /* The docs say we must pass 0xAA as the data byte. */
342 lm8323_write(lm, 2, LM8323_CMD_RESET, 0xAA);
345 static int lm8323_configure(struct lm8323_chip *lm)
347 int keysize = (lm->size_x << 4) | lm->size_y;
348 int clock = (CLK_SLOWCLKEN | CLK_RCPWM_EXTERNAL);
349 int debounce = lm->debounce_time >> 2;
350 int active = lm->active_time >> 2;
353 * Active time must be greater than the debounce time: if it's
354 * a close-run thing, give ourselves a 12ms buffer.
356 if (debounce >= active)
357 active = debounce + 3;
359 lm8323_write(lm, 2, LM8323_CMD_WRITE_CFG, 0);
360 lm8323_write(lm, 2, LM8323_CMD_WRITE_CLOCK, clock);
361 lm8323_write(lm, 2, LM8323_CMD_SET_KEY_SIZE, keysize);
362 lm8323_set_active_time(lm, lm->active_time);
363 lm8323_write(lm, 2, LM8323_CMD_SET_DEBOUNCE, debounce);
364 lm8323_write(lm, 3, LM8323_CMD_WRITE_PORT_STATE, 0xff, 0xff);
365 lm8323_write(lm, 3, LM8323_CMD_WRITE_PORT_SEL, 0, 0);
368 * Not much we can do about errors at this point, so just hope
375 static void pwm_done(struct lm8323_pwm *pwm)
377 mutex_lock(&pwm->lock);
379 if (pwm->desired_brightness != pwm->brightness)
380 schedule_work(&pwm->work);
381 mutex_unlock(&pwm->lock);
385 * Bottom half: handle the interrupt by posting key events, or dealing with
386 * errors appropriately.
388 static void lm8323_work(struct work_struct *work)
390 struct lm8323_chip *lm = work_to_lm8323(work);
393 mutex_lock(&lm->lock);
395 while ((lm8323_read(lm, LM8323_CMD_READ_INT, &ints, 1) == 1) && ints) {
396 if (likely(ints & INT_KEYPAD))
398 if (ints & INT_ROTATOR) {
399 /* We don't currently support the rotator. */
400 dev_vdbg(&lm->client->dev, "rotator fired\n");
402 if (ints & INT_ERROR) {
403 dev_vdbg(&lm->client->dev, "error!\n");
404 lm8323_process_error(lm);
406 if (ints & INT_NOINIT) {
407 dev_err(&lm->client->dev, "chip lost config; "
409 lm8323_configure(lm);
411 if (ints & INT_PWM1) {
412 dev_vdbg(&lm->client->dev, "pwm1 engine completed\n");
415 if (ints & INT_PWM2) {
416 dev_vdbg(&lm->client->dev, "pwm2 engine completed\n");
419 if (ints & INT_PWM3) {
420 dev_vdbg(&lm->client->dev, "pwm3 engine completed\n");
425 mutex_unlock(&lm->lock);
429 * We cannot use I2C in interrupt context, so we just schedule work.
431 static irqreturn_t lm8323_irq(int irq, void *data)
433 struct lm8323_chip *lm = data;
435 schedule_work(&lm->work);
443 static int lm8323_read_id(struct lm8323_chip *lm, u8 *buf)
447 bytes = lm8323_read(lm, LM8323_CMD_READ_ID, buf, 2);
448 if (unlikely(bytes != 2))
454 static void lm8323_write_pwm_one(struct lm8323_pwm *pwm, int pos, u16 cmd)
456 struct lm8323_chip *lm = pwm_to_lm8323(pwm);
458 lm8323_write(lm, 4, LM8323_CMD_PWM_WRITE, (pos << 2) | pwm->id,
459 (cmd & 0xff00) >> 8, cmd & 0x00ff);
463 * Write a script into a given PWM engine, concluding with PWM_END.
464 * If 'kill' is nonzero, the engine will be shut down at the end
465 * of the script, producing a zero output. Otherwise the engine
466 * will be kept running at the final PWM level indefinitely.
468 static void lm8323_write_pwm(struct lm8323_pwm *pwm, int kill,
469 int len, const u16 *cmds)
471 struct lm8323_chip *lm = pwm_to_lm8323(pwm);
474 for (i = 0; i < len; i++)
475 lm8323_write_pwm_one(pwm, i, cmds[i]);
477 lm8323_write_pwm_one(pwm, i++, PWM_END(kill));
478 lm8323_write(lm, 2, LM8323_CMD_START_PWM, pwm->id);
482 static void lm8323_pwm_work(struct work_struct *work)
484 struct lm8323_pwm *pwm = work_to_pwm(work);
485 int div512, perstep, steps, hz, up, kill;
489 mutex_lock(&pwm->lock);
492 * Do nothing if we're already at the requested level,
493 * or previous setting is not yet complete. In the latter
494 * case we will be called again when the previous PWM script
497 if (pwm->running || pwm->desired_brightness == pwm->brightness) {
498 mutex_unlock(&pwm->lock);
502 kill = (pwm->desired_brightness == 0);
503 up = (pwm->desired_brightness > pwm->brightness);
504 steps = abs(pwm->desired_brightness - pwm->brightness);
507 * Convert time (in ms) into a divisor (512 or 16 on a refclk of
508 * 32768Hz), and number of ticks per step.
510 if ((pwm->fade_time / steps) > (32768 / 512)) {
519 perstep = (hz * pwm->fade_time) / (steps * 1000);
523 else if (perstep > 63)
530 pwm_cmds[num_cmds++] = PWM_RAMP(div512, perstep, s, up);
534 lm8323_write_pwm(pwm, kill, num_cmds, pwm_cmds);
536 pwm->brightness = pwm->desired_brightness;
537 mutex_unlock(&pwm->lock);
540 static void lm8323_pwm_set_brightness(struct led_classdev *led_cdev,
541 enum led_brightness brightness)
543 struct lm8323_pwm *pwm = cdev_to_pwm(led_cdev);
544 struct lm8323_chip *lm = pwm_to_lm8323(pwm);
546 mutex_lock(&pwm->lock);
547 pwm->desired_brightness = brightness;
548 mutex_unlock(&pwm->lock);
550 if (in_interrupt()) {
551 schedule_work(&pwm->work);
554 * Schedule PWM work as usual unless we are going into suspend
556 mutex_lock(&lm->lock);
557 if (likely(!lm->pm_suspend))
558 schedule_work(&pwm->work);
560 lm8323_pwm_work(&pwm->work);
561 mutex_unlock(&lm->lock);
565 static ssize_t lm8323_pwm_show_time(struct device *dev,
566 struct device_attribute *attr, char *buf)
568 struct led_classdev *led_cdev = dev_get_drvdata(dev);
569 struct lm8323_pwm *pwm = cdev_to_pwm(led_cdev);
571 return sprintf(buf, "%d\n", pwm->fade_time);
574 static ssize_t lm8323_pwm_store_time(struct device *dev,
575 struct device_attribute *attr, const char *buf, size_t len)
577 struct led_classdev *led_cdev = dev_get_drvdata(dev);
578 struct lm8323_pwm *pwm = cdev_to_pwm(led_cdev);
582 ret = sscanf(buf, "%d", &time);
583 /* Numbers only, please. */
587 pwm->fade_time = time;
591 static DEVICE_ATTR(time, 0644, lm8323_pwm_show_time, lm8323_pwm_store_time);
593 static int init_pwm(struct lm8323_chip *lm, int id, struct device *dev,
596 struct lm8323_pwm *pwm = NULL;
615 pwm->desired_brightness = 0;
617 mutex_init(&pwm->lock);
619 pwm->cdev.name = name;
620 pwm->cdev.brightness_set = lm8323_pwm_set_brightness;
621 if (led_classdev_register(dev, &pwm->cdev) < 0) {
622 dev_err(dev, "couldn't register PWM %d\n", id);
625 if (device_create_file(pwm->cdev.dev,
626 &dev_attr_time) < 0) {
627 dev_err(dev, "couldn't register time attribute\n");
628 led_classdev_unregister(&pwm->cdev);
631 INIT_WORK(&pwm->work, lm8323_pwm_work);
640 static struct i2c_driver lm8323_i2c_driver;
642 static ssize_t lm8323_show_disable(struct device *dev,
643 struct device_attribute *attr, char *buf)
645 struct lm8323_chip *lm = dev_get_drvdata(dev);
647 return sprintf(buf, "%u\n", !lm->kp_enabled);
650 static ssize_t lm8323_set_disable(struct device *dev,
651 struct device_attribute *attr,
652 const char *buf, size_t count)
654 struct lm8323_chip *lm = dev_get_drvdata(dev);
658 i = sscanf(buf, "%d", &ret);
660 mutex_lock(&lm->lock);
662 mutex_unlock(&lm->lock);
666 static DEVICE_ATTR(disable_kp, 0644, lm8323_show_disable, lm8323_set_disable);
668 static int lm8323_probe(struct i2c_client *client,
669 const struct i2c_device_id *id)
671 struct lm8323_platform_data *pdata;
672 struct input_dev *idev;
673 struct lm8323_chip *lm;
678 lm = kzalloc(sizeof *lm, GFP_KERNEL);
682 i2c_set_clientdata(client, lm);
684 pdata = client->dev.platform_data;
685 if (!pdata || !pdata->size_x || !pdata->size_y) {
686 dev_err(&client->dev, "missing platform_data\n");
691 lm->size_x = pdata->size_x;
692 if (lm->size_x > 8) {
693 dev_err(&client->dev, "invalid x size %d specified\n",
699 lm->size_y = pdata->size_y;
700 if (lm->size_y > 12) {
701 dev_err(&client->dev, "invalid y size %d specified\n",
707 dev_vdbg(&client->dev, "Keypad size: %d x %d\n", lm->size_x, lm->size_y);
709 lm->debounce_time = pdata->debounce_time;
710 lm->active_time = pdata->active_time;
714 /* Nothing's set up to service the IRQ yet, so just spin for max.
715 * 100ms until we can configure. */
716 tmo = jiffies + msecs_to_jiffies(100);
717 while (lm8323_read(lm, LM8323_CMD_READ_INT, data, 1) == 1) {
718 if (data[0] & INT_NOINIT)
721 if (time_after(jiffies, tmo)) {
722 dev_err(&client->dev,
723 "timeout waiting for initialisation\n");
729 lm8323_configure(lm);
731 /* If a true probe check the device */
732 if (lm8323_read_id(lm, data) != 0) {
733 dev_err(&client->dev, "device not found\n");
738 if (init_pwm(lm, 1, &client->dev, pdata->pwm1_name) < 0)
740 if (init_pwm(lm, 2, &client->dev, pdata->pwm2_name) < 0)
742 if (init_pwm(lm, 3, &client->dev, pdata->pwm3_name) < 0)
745 mutex_init(&lm->lock);
746 INIT_WORK(&lm->work, lm8323_work);
748 err = request_irq(client->irq, lm8323_irq,
749 IRQF_TRIGGER_FALLING | IRQF_DISABLED |
750 IRQF_SAMPLE_RANDOM, "lm8323", lm);
752 dev_err(&client->dev, "could not get IRQ %d\n", client->irq);
756 set_irq_wake(client->irq, 1);
759 err = device_create_file(&client->dev, &dev_attr_disable_kp);
763 idev = input_allocate_device();
770 idev->name = pdata->name;
772 idev->name = "LM8323 keypad";
773 snprintf(lm->phys, sizeof(lm->phys), "%s/input-kp", client->dev.bus_id);
774 idev->phys = lm->phys;
777 idev->evbit[0] = BIT(EV_KEY);
778 for (i = 0; i < LM8323_KEYMAP_SIZE; i++) {
779 if (pdata->keymap[i] > 0)
780 set_bit(pdata->keymap[i], idev->keybit);
782 lm->keymap[i] = pdata->keymap[i];
786 set_bit(EV_REP, idev->evbit);
789 err = input_register_device(idev);
791 dev_dbg(&client->dev, "error registering input device\n");
798 device_remove_file(&client->dev, &dev_attr_disable_kp);
800 free_irq(client->irq, lm);
802 if (lm->pwm3.enabled)
803 led_classdev_unregister(&lm->pwm3.cdev);
805 if (lm->pwm2.enabled)
806 led_classdev_unregister(&lm->pwm2.cdev);
808 if (lm->pwm1.enabled)
809 led_classdev_unregister(&lm->pwm1.cdev);
816 static int lm8323_remove(struct i2c_client *client)
818 struct lm8323_chip *lm = i2c_get_clientdata(client);
820 free_irq(client->irq, lm);
821 cancel_work_sync(&lm->work);
822 input_unregister_device(lm->idev);
823 device_remove_file(&lm->client->dev, &dev_attr_disable_kp);
824 if (lm->pwm3.enabled)
825 led_classdev_unregister(&lm->pwm3.cdev);
826 if (lm->pwm2.enabled)
827 led_classdev_unregister(&lm->pwm2.cdev);
828 if (lm->pwm1.enabled)
829 led_classdev_unregister(&lm->pwm1.cdev);
836 * We don't need to explicitly suspend the chip, as it already switches off
837 * when there's no activity.
839 static int lm8323_suspend(struct i2c_client *client, pm_message_t mesg)
841 struct lm8323_chip *lm = i2c_get_clientdata(client);
843 set_irq_wake(client->irq, 0);
844 disable_irq(client->irq);
846 mutex_lock(&lm->lock);
848 mutex_unlock(&lm->lock);
850 if (lm->pwm1.enabled)
851 led_classdev_suspend(&lm->pwm1.cdev);
852 if (lm->pwm2.enabled)
853 led_classdev_suspend(&lm->pwm2.cdev);
854 if (lm->pwm3.enabled)
855 led_classdev_suspend(&lm->pwm3.cdev);
860 static int lm8323_resume(struct i2c_client *client)
862 struct lm8323_chip *lm = i2c_get_clientdata(client);
864 mutex_lock(&lm->lock);
866 mutex_unlock(&lm->lock);
868 if (lm->pwm1.enabled)
869 led_classdev_resume(&lm->pwm1.cdev);
870 if (lm->pwm2.enabled)
871 led_classdev_resume(&lm->pwm2.cdev);
872 if (lm->pwm3.enabled)
873 led_classdev_resume(&lm->pwm3.cdev);
875 enable_irq(client->irq);
876 set_irq_wake(client->irq, 1);
881 static const struct i2c_device_id lm8323_id[] = {
886 static struct i2c_driver lm8323_i2c_driver = {
890 .probe = lm8323_probe,
891 .remove = lm8323_remove,
892 .suspend = lm8323_suspend,
893 .resume = lm8323_resume,
894 .id_table = lm8323_id,
896 MODULE_DEVICE_TABLE(i2c, lm8323_id);
898 static int __init lm8323_init(void)
900 return i2c_add_driver(&lm8323_i2c_driver);
902 module_init(lm8323_init);
904 static void __exit lm8323_exit(void)
906 i2c_del_driver(&lm8323_i2c_driver);
908 module_exit(lm8323_exit);
910 MODULE_AUTHOR("Timo O. Karjalainen <timo.o.karjalainen@nokia.com>, Daniel Stone");
911 MODULE_DESCRIPTION("LM8323 keypad driver");
912 MODULE_LICENSE("GPL");