Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * drivers/i2c/chips/lm8323.c
0004  *
0005  * Copyright (C) 2007-2009 Nokia Corporation
0006  *
0007  * Written by Daniel Stone <daniel.stone@nokia.com>
0008  *            Timo O. Karjalainen <timo.o.karjalainen@nokia.com>
0009  *
0010  * Updated by Felipe Balbi <felipe.balbi@nokia.com>
0011  */
0012 
0013 #include <linux/module.h>
0014 #include <linux/i2c.h>
0015 #include <linux/interrupt.h>
0016 #include <linux/sched.h>
0017 #include <linux/mutex.h>
0018 #include <linux/delay.h>
0019 #include <linux/input.h>
0020 #include <linux/leds.h>
0021 #include <linux/platform_data/lm8323.h>
0022 #include <linux/pm.h>
0023 #include <linux/slab.h>
0024 
0025 /* Commands to send to the chip. */
0026 #define LM8323_CMD_READ_ID      0x80 /* Read chip ID. */
0027 #define LM8323_CMD_WRITE_CFG        0x81 /* Set configuration item. */
0028 #define LM8323_CMD_READ_INT     0x82 /* Get interrupt status. */
0029 #define LM8323_CMD_RESET        0x83 /* Reset, same as external one */
0030 #define LM8323_CMD_WRITE_PORT_SEL   0x85 /* Set GPIO in/out. */
0031 #define LM8323_CMD_WRITE_PORT_STATE 0x86 /* Set GPIO pullup. */
0032 #define LM8323_CMD_READ_PORT_SEL    0x87 /* Get GPIO in/out. */
0033 #define LM8323_CMD_READ_PORT_STATE  0x88 /* Get GPIO pullup. */
0034 #define LM8323_CMD_READ_FIFO        0x89 /* Read byte from FIFO. */
0035 #define LM8323_CMD_RPT_READ_FIFO    0x8a /* Read FIFO (no increment). */
0036 #define LM8323_CMD_SET_ACTIVE       0x8b /* Set active time. */
0037 #define LM8323_CMD_READ_ERR     0x8c /* Get error status. */
0038 #define LM8323_CMD_READ_ROTATOR     0x8e /* Read rotator status. */
0039 #define LM8323_CMD_SET_DEBOUNCE     0x8f /* Set debouncing time. */
0040 #define LM8323_CMD_SET_KEY_SIZE     0x90 /* Set keypad size. */
0041 #define LM8323_CMD_READ_KEY_SIZE    0x91 /* Get keypad size. */
0042 #define LM8323_CMD_READ_CFG     0x92 /* Get configuration item. */
0043 #define LM8323_CMD_WRITE_CLOCK      0x93 /* Set clock config. */
0044 #define LM8323_CMD_READ_CLOCK       0x94 /* Get clock config. */
0045 #define LM8323_CMD_PWM_WRITE        0x95 /* Write PWM script. */
0046 #define LM8323_CMD_START_PWM        0x96 /* Start PWM engine. */
0047 #define LM8323_CMD_STOP_PWM     0x97 /* Stop PWM engine. */
0048 
0049 /* Interrupt status. */
0050 #define INT_KEYPAD          0x01 /* Key event. */
0051 #define INT_ROTATOR         0x02 /* Rotator event. */
0052 #define INT_ERROR           0x08 /* Error: use CMD_READ_ERR. */
0053 #define INT_NOINIT          0x10 /* Lost configuration. */
0054 #define INT_PWM1            0x20 /* PWM1 stopped. */
0055 #define INT_PWM2            0x40 /* PWM2 stopped. */
0056 #define INT_PWM3            0x80 /* PWM3 stopped. */
0057 
0058 /* Errors (signalled by INT_ERROR, read with CMD_READ_ERR). */
0059 #define ERR_BADPAR          0x01 /* Bad parameter. */
0060 #define ERR_CMDUNK          0x02 /* Unknown command. */
0061 #define ERR_KEYOVR          0x04 /* Too many keys pressed. */
0062 #define ERR_FIFOOVER            0x40 /* FIFO overflow. */
0063 
0064 /* Configuration keys (CMD_{WRITE,READ}_CFG). */
0065 #define CFG_MUX1SEL         0x01 /* Select MUX1_OUT input. */
0066 #define CFG_MUX1EN          0x02 /* Enable MUX1_OUT. */
0067 #define CFG_MUX2SEL         0x04 /* Select MUX2_OUT input. */
0068 #define CFG_MUX2EN          0x08 /* Enable MUX2_OUT. */
0069 #define CFG_PSIZE           0x20 /* Package size (must be 0). */
0070 #define CFG_ROTEN           0x40 /* Enable rotator. */
0071 
0072 /* Clock settings (CMD_{WRITE,READ}_CLOCK). */
0073 #define CLK_RCPWM_INTERNAL      0x00
0074 #define CLK_RCPWM_EXTERNAL      0x03
0075 #define CLK_SLOWCLKEN           0x08 /* Enable 32.768kHz clock. */
0076 #define CLK_SLOWCLKOUT          0x40 /* Enable slow pulse output. */
0077 
0078 /* The possible addresses corresponding to CONFIG1 and CONFIG2 pin wirings. */
0079 #define LM8323_I2C_ADDR00       (0x84 >> 1) /* 1000 010x */
0080 #define LM8323_I2C_ADDR01       (0x86 >> 1) /* 1000 011x */
0081 #define LM8323_I2C_ADDR10       (0x88 >> 1) /* 1000 100x */
0082 #define LM8323_I2C_ADDR11       (0x8A >> 1) /* 1000 101x */
0083 
0084 /* Key event fifo length */
0085 #define LM8323_FIFO_LEN         15
0086 
0087 /* Commands for PWM engine; feed in with PWM_WRITE. */
0088 /* Load ramp counter from duty cycle field (range 0 - 0xff). */
0089 #define PWM_SET(v)          (0x4000 | ((v) & 0xff))
0090 /* Go to start of script. */
0091 #define PWM_GOTOSTART           0x0000
0092 /*
0093  * Stop engine (generates interrupt).  If reset is 1, clear the program
0094  * counter, else leave it.
0095  */
0096 #define PWM_END(reset)          (0xc000 | (!!(reset) << 11))
0097 /*
0098  * Ramp.  If s is 1, divide clock by 512, else divide clock by 16.
0099  * Take t clock scales (up to 63) per step, for n steps (up to 126).
0100  * If u is set, ramp up, else ramp down.
0101  */
0102 #define PWM_RAMP(s, t, n, u)        ((!!(s) << 14) | ((t) & 0x3f) << 8 | \
0103                      ((n) & 0x7f) | ((u) ? 0 : 0x80))
0104 /*
0105  * Loop (i.e. jump back to pos) for a given number of iterations (up to 63).
0106  * If cnt is zero, execute until PWM_END is encountered.
0107  */
0108 #define PWM_LOOP(cnt, pos)      (0xa000 | (((cnt) & 0x3f) << 7) | \
0109                      ((pos) & 0x3f))
0110 /*
0111  * Wait for trigger.  Argument is a mask of channels, shifted by the channel
0112  * number, e.g. 0xa for channels 3 and 1.  Note that channels are numbered
0113  * from 1, not 0.
0114  */
0115 #define PWM_WAIT_TRIG(chans)        (0xe000 | (((chans) & 0x7) << 6))
0116 /* Send trigger.  Argument is same as PWM_WAIT_TRIG. */
0117 #define PWM_SEND_TRIG(chans)        (0xe000 | ((chans) & 0x7))
0118 
0119 struct lm8323_pwm {
0120     int         id;
0121     int         fade_time;
0122     int         brightness;
0123     int         desired_brightness;
0124     bool            enabled;
0125     bool            running;
0126     /* pwm lock */
0127     struct mutex        lock;
0128     struct work_struct  work;
0129     struct led_classdev cdev;
0130     struct lm8323_chip  *chip;
0131 };
0132 
0133 struct lm8323_chip {
0134     /* device lock */
0135     struct mutex        lock;
0136     struct i2c_client   *client;
0137     struct input_dev    *idev;
0138     bool            kp_enabled;
0139     bool            pm_suspend;
0140     unsigned        keys_down;
0141     char            phys[32];
0142     unsigned short      keymap[LM8323_KEYMAP_SIZE];
0143     int         size_x;
0144     int         size_y;
0145     int         debounce_time;
0146     int         active_time;
0147     struct lm8323_pwm   pwm[LM8323_NUM_PWMS];
0148 };
0149 
0150 #define client_to_lm8323(c) container_of(c, struct lm8323_chip, client)
0151 #define dev_to_lm8323(d)    container_of(d, struct lm8323_chip, client->dev)
0152 #define cdev_to_pwm(c)      container_of(c, struct lm8323_pwm, cdev)
0153 #define work_to_pwm(w)      container_of(w, struct lm8323_pwm, work)
0154 
0155 #define LM8323_MAX_DATA 8
0156 
0157 /*
0158  * To write, we just access the chip's address in write mode, and dump the
0159  * command and data out on the bus.  The command byte and data are taken as
0160  * sequential u8s out of varargs, to a maximum of LM8323_MAX_DATA.
0161  */
0162 static int lm8323_write(struct lm8323_chip *lm, int len, ...)
0163 {
0164     int ret, i;
0165     va_list ap;
0166     u8 data[LM8323_MAX_DATA];
0167 
0168     va_start(ap, len);
0169 
0170     if (unlikely(len > LM8323_MAX_DATA)) {
0171         dev_err(&lm->client->dev, "tried to send %d bytes\n", len);
0172         va_end(ap);
0173         return 0;
0174     }
0175 
0176     for (i = 0; i < len; i++)
0177         data[i] = va_arg(ap, int);
0178 
0179     va_end(ap);
0180 
0181     /*
0182      * If the host is asleep while we send the data, we can get a NACK
0183      * back while it wakes up, so try again, once.
0184      */
0185     ret = i2c_master_send(lm->client, data, len);
0186     if (unlikely(ret == -EREMOTEIO))
0187         ret = i2c_master_send(lm->client, data, len);
0188     if (unlikely(ret != len))
0189         dev_err(&lm->client->dev, "sent %d bytes of %d total\n",
0190             len, ret);
0191 
0192     return ret;
0193 }
0194 
0195 /*
0196  * To read, we first send the command byte to the chip and end the transaction,
0197  * then access the chip in read mode, at which point it will send the data.
0198  */
0199 static int lm8323_read(struct lm8323_chip *lm, u8 cmd, u8 *buf, int len)
0200 {
0201     int ret;
0202 
0203     /*
0204      * If the host is asleep while we send the byte, we can get a NACK
0205      * back while it wakes up, so try again, once.
0206      */
0207     ret = i2c_master_send(lm->client, &cmd, 1);
0208     if (unlikely(ret == -EREMOTEIO))
0209         ret = i2c_master_send(lm->client, &cmd, 1);
0210     if (unlikely(ret != 1)) {
0211         dev_err(&lm->client->dev, "sending read cmd 0x%02x failed\n",
0212             cmd);
0213         return 0;
0214     }
0215 
0216     ret = i2c_master_recv(lm->client, buf, len);
0217     if (unlikely(ret != len))
0218         dev_err(&lm->client->dev, "wanted %d bytes, got %d\n",
0219             len, ret);
0220 
0221     return ret;
0222 }
0223 
0224 /*
0225  * Set the chip active time (idle time before it enters halt).
0226  */
0227 static void lm8323_set_active_time(struct lm8323_chip *lm, int time)
0228 {
0229     lm8323_write(lm, 2, LM8323_CMD_SET_ACTIVE, time >> 2);
0230 }
0231 
0232 /*
0233  * The signals are AT-style: the low 7 bits are the keycode, and the top
0234  * bit indicates the state (1 for down, 0 for up).
0235  */
0236 static inline u8 lm8323_whichkey(u8 event)
0237 {
0238     return event & 0x7f;
0239 }
0240 
0241 static inline int lm8323_ispress(u8 event)
0242 {
0243     return (event & 0x80) ? 1 : 0;
0244 }
0245 
0246 static void process_keys(struct lm8323_chip *lm)
0247 {
0248     u8 event;
0249     u8 key_fifo[LM8323_FIFO_LEN + 1];
0250     int old_keys_down = lm->keys_down;
0251     int ret;
0252     int i = 0;
0253 
0254     /*
0255      * Read all key events from the FIFO at once. Next READ_FIFO clears the
0256      * FIFO even if we didn't read all events previously.
0257      */
0258     ret = lm8323_read(lm, LM8323_CMD_READ_FIFO, key_fifo, LM8323_FIFO_LEN);
0259 
0260     if (ret < 0) {
0261         dev_err(&lm->client->dev, "Failed reading fifo \n");
0262         return;
0263     }
0264     key_fifo[ret] = 0;
0265 
0266     while ((event = key_fifo[i++])) {
0267         u8 key = lm8323_whichkey(event);
0268         int isdown = lm8323_ispress(event);
0269         unsigned short keycode = lm->keymap[key];
0270 
0271         dev_vdbg(&lm->client->dev, "key 0x%02x %s\n",
0272              key, isdown ? "down" : "up");
0273 
0274         if (lm->kp_enabled) {
0275             input_event(lm->idev, EV_MSC, MSC_SCAN, key);
0276             input_report_key(lm->idev, keycode, isdown);
0277             input_sync(lm->idev);
0278         }
0279 
0280         if (isdown)
0281             lm->keys_down++;
0282         else
0283             lm->keys_down--;
0284     }
0285 
0286     /*
0287      * Errata: We need to ensure that the chip never enters halt mode
0288      * during a keypress, so set active time to 0.  When it's released,
0289      * we can enter halt again, so set the active time back to normal.
0290      */
0291     if (!old_keys_down && lm->keys_down)
0292         lm8323_set_active_time(lm, 0);
0293     if (old_keys_down && !lm->keys_down)
0294         lm8323_set_active_time(lm, lm->active_time);
0295 }
0296 
0297 static void lm8323_process_error(struct lm8323_chip *lm)
0298 {
0299     u8 error;
0300 
0301     if (lm8323_read(lm, LM8323_CMD_READ_ERR, &error, 1) == 1) {
0302         if (error & ERR_FIFOOVER)
0303             dev_vdbg(&lm->client->dev, "fifo overflow!\n");
0304         if (error & ERR_KEYOVR)
0305             dev_vdbg(&lm->client->dev,
0306                     "more than two keys pressed\n");
0307         if (error & ERR_CMDUNK)
0308             dev_vdbg(&lm->client->dev,
0309                     "unknown command submitted\n");
0310         if (error & ERR_BADPAR)
0311             dev_vdbg(&lm->client->dev, "bad command parameter\n");
0312     }
0313 }
0314 
0315 static void lm8323_reset(struct lm8323_chip *lm)
0316 {
0317     /* The docs say we must pass 0xAA as the data byte. */
0318     lm8323_write(lm, 2, LM8323_CMD_RESET, 0xAA);
0319 }
0320 
0321 static int lm8323_configure(struct lm8323_chip *lm)
0322 {
0323     int keysize = (lm->size_x << 4) | lm->size_y;
0324     int clock = (CLK_SLOWCLKEN | CLK_RCPWM_EXTERNAL);
0325     int debounce = lm->debounce_time >> 2;
0326     int active = lm->active_time >> 2;
0327 
0328     /*
0329      * Active time must be greater than the debounce time: if it's
0330      * a close-run thing, give ourselves a 12ms buffer.
0331      */
0332     if (debounce >= active)
0333         active = debounce + 3;
0334 
0335     lm8323_write(lm, 2, LM8323_CMD_WRITE_CFG, 0);
0336     lm8323_write(lm, 2, LM8323_CMD_WRITE_CLOCK, clock);
0337     lm8323_write(lm, 2, LM8323_CMD_SET_KEY_SIZE, keysize);
0338     lm8323_set_active_time(lm, lm->active_time);
0339     lm8323_write(lm, 2, LM8323_CMD_SET_DEBOUNCE, debounce);
0340     lm8323_write(lm, 3, LM8323_CMD_WRITE_PORT_STATE, 0xff, 0xff);
0341     lm8323_write(lm, 3, LM8323_CMD_WRITE_PORT_SEL, 0, 0);
0342 
0343     /*
0344      * Not much we can do about errors at this point, so just hope
0345      * for the best.
0346      */
0347 
0348     return 0;
0349 }
0350 
0351 static void pwm_done(struct lm8323_pwm *pwm)
0352 {
0353     mutex_lock(&pwm->lock);
0354     pwm->running = false;
0355     if (pwm->desired_brightness != pwm->brightness)
0356         schedule_work(&pwm->work);
0357     mutex_unlock(&pwm->lock);
0358 }
0359 
0360 /*
0361  * Bottom half: handle the interrupt by posting key events, or dealing with
0362  * errors appropriately.
0363  */
0364 static irqreturn_t lm8323_irq(int irq, void *_lm)
0365 {
0366     struct lm8323_chip *lm = _lm;
0367     u8 ints;
0368     int i;
0369 
0370     mutex_lock(&lm->lock);
0371 
0372     while ((lm8323_read(lm, LM8323_CMD_READ_INT, &ints, 1) == 1) && ints) {
0373         if (likely(ints & INT_KEYPAD))
0374             process_keys(lm);
0375         if (ints & INT_ROTATOR) {
0376             /* We don't currently support the rotator. */
0377             dev_vdbg(&lm->client->dev, "rotator fired\n");
0378         }
0379         if (ints & INT_ERROR) {
0380             dev_vdbg(&lm->client->dev, "error!\n");
0381             lm8323_process_error(lm);
0382         }
0383         if (ints & INT_NOINIT) {
0384             dev_err(&lm->client->dev, "chip lost config; "
0385                           "reinitialising\n");
0386             lm8323_configure(lm);
0387         }
0388         for (i = 0; i < LM8323_NUM_PWMS; i++) {
0389             if (ints & (INT_PWM1 << i)) {
0390                 dev_vdbg(&lm->client->dev,
0391                      "pwm%d engine completed\n", i);
0392                 pwm_done(&lm->pwm[i]);
0393             }
0394         }
0395     }
0396 
0397     mutex_unlock(&lm->lock);
0398 
0399     return IRQ_HANDLED;
0400 }
0401 
0402 /*
0403  * Read the chip ID.
0404  */
0405 static int lm8323_read_id(struct lm8323_chip *lm, u8 *buf)
0406 {
0407     int bytes;
0408 
0409     bytes = lm8323_read(lm, LM8323_CMD_READ_ID, buf, 2);
0410     if (unlikely(bytes != 2))
0411         return -EIO;
0412 
0413     return 0;
0414 }
0415 
0416 static void lm8323_write_pwm_one(struct lm8323_pwm *pwm, int pos, u16 cmd)
0417 {
0418     lm8323_write(pwm->chip, 4, LM8323_CMD_PWM_WRITE, (pos << 2) | pwm->id,
0419              (cmd & 0xff00) >> 8, cmd & 0x00ff);
0420 }
0421 
0422 /*
0423  * Write a script into a given PWM engine, concluding with PWM_END.
0424  * If 'kill' is nonzero, the engine will be shut down at the end
0425  * of the script, producing a zero output. Otherwise the engine
0426  * will be kept running at the final PWM level indefinitely.
0427  */
0428 static void lm8323_write_pwm(struct lm8323_pwm *pwm, int kill,
0429                  int len, const u16 *cmds)
0430 {
0431     int i;
0432 
0433     for (i = 0; i < len; i++)
0434         lm8323_write_pwm_one(pwm, i, cmds[i]);
0435 
0436     lm8323_write_pwm_one(pwm, i++, PWM_END(kill));
0437     lm8323_write(pwm->chip, 2, LM8323_CMD_START_PWM, pwm->id);
0438     pwm->running = true;
0439 }
0440 
0441 static void lm8323_pwm_work(struct work_struct *work)
0442 {
0443     struct lm8323_pwm *pwm = work_to_pwm(work);
0444     int div512, perstep, steps, hz, up, kill;
0445     u16 pwm_cmds[3];
0446     int num_cmds = 0;
0447 
0448     mutex_lock(&pwm->lock);
0449 
0450     /*
0451      * Do nothing if we're already at the requested level,
0452      * or previous setting is not yet complete. In the latter
0453      * case we will be called again when the previous PWM script
0454      * finishes.
0455      */
0456     if (pwm->running || pwm->desired_brightness == pwm->brightness)
0457         goto out;
0458 
0459     kill = (pwm->desired_brightness == 0);
0460     up = (pwm->desired_brightness > pwm->brightness);
0461     steps = abs(pwm->desired_brightness - pwm->brightness);
0462 
0463     /*
0464      * Convert time (in ms) into a divisor (512 or 16 on a refclk of
0465      * 32768Hz), and number of ticks per step.
0466      */
0467     if ((pwm->fade_time / steps) > (32768 / 512)) {
0468         div512 = 1;
0469         hz = 32768 / 512;
0470     } else {
0471         div512 = 0;
0472         hz = 32768 / 16;
0473     }
0474 
0475     perstep = (hz * pwm->fade_time) / (steps * 1000);
0476 
0477     if (perstep == 0)
0478         perstep = 1;
0479     else if (perstep > 63)
0480         perstep = 63;
0481 
0482     while (steps) {
0483         int s;
0484 
0485         s = min(126, steps);
0486         pwm_cmds[num_cmds++] = PWM_RAMP(div512, perstep, s, up);
0487         steps -= s;
0488     }
0489 
0490     lm8323_write_pwm(pwm, kill, num_cmds, pwm_cmds);
0491     pwm->brightness = pwm->desired_brightness;
0492 
0493  out:
0494     mutex_unlock(&pwm->lock);
0495 }
0496 
0497 static void lm8323_pwm_set_brightness(struct led_classdev *led_cdev,
0498                       enum led_brightness brightness)
0499 {
0500     struct lm8323_pwm *pwm = cdev_to_pwm(led_cdev);
0501     struct lm8323_chip *lm = pwm->chip;
0502 
0503     mutex_lock(&pwm->lock);
0504     pwm->desired_brightness = brightness;
0505     mutex_unlock(&pwm->lock);
0506 
0507     if (in_interrupt()) {
0508         schedule_work(&pwm->work);
0509     } else {
0510         /*
0511          * Schedule PWM work as usual unless we are going into suspend
0512          */
0513         mutex_lock(&lm->lock);
0514         if (likely(!lm->pm_suspend))
0515             schedule_work(&pwm->work);
0516         else
0517             lm8323_pwm_work(&pwm->work);
0518         mutex_unlock(&lm->lock);
0519     }
0520 }
0521 
0522 static ssize_t lm8323_pwm_show_time(struct device *dev,
0523         struct device_attribute *attr, char *buf)
0524 {
0525     struct led_classdev *led_cdev = dev_get_drvdata(dev);
0526     struct lm8323_pwm *pwm = cdev_to_pwm(led_cdev);
0527 
0528     return sprintf(buf, "%d\n", pwm->fade_time);
0529 }
0530 
0531 static ssize_t lm8323_pwm_store_time(struct device *dev,
0532         struct device_attribute *attr, const char *buf, size_t len)
0533 {
0534     struct led_classdev *led_cdev = dev_get_drvdata(dev);
0535     struct lm8323_pwm *pwm = cdev_to_pwm(led_cdev);
0536     int ret, time;
0537 
0538     ret = kstrtoint(buf, 10, &time);
0539     /* Numbers only, please. */
0540     if (ret)
0541         return ret;
0542 
0543     pwm->fade_time = time;
0544 
0545     return strlen(buf);
0546 }
0547 static DEVICE_ATTR(time, 0644, lm8323_pwm_show_time, lm8323_pwm_store_time);
0548 
0549 static struct attribute *lm8323_pwm_attrs[] = {
0550     &dev_attr_time.attr,
0551     NULL
0552 };
0553 ATTRIBUTE_GROUPS(lm8323_pwm);
0554 
0555 static int init_pwm(struct lm8323_chip *lm, int id, struct device *dev,
0556             const char *name)
0557 {
0558     struct lm8323_pwm *pwm;
0559 
0560     BUG_ON(id > 3);
0561 
0562     pwm = &lm->pwm[id - 1];
0563 
0564     pwm->id = id;
0565     pwm->fade_time = 0;
0566     pwm->brightness = 0;
0567     pwm->desired_brightness = 0;
0568     pwm->running = false;
0569     pwm->enabled = false;
0570     INIT_WORK(&pwm->work, lm8323_pwm_work);
0571     mutex_init(&pwm->lock);
0572     pwm->chip = lm;
0573 
0574     if (name) {
0575         pwm->cdev.name = name;
0576         pwm->cdev.brightness_set = lm8323_pwm_set_brightness;
0577         pwm->cdev.groups = lm8323_pwm_groups;
0578         if (led_classdev_register(dev, &pwm->cdev) < 0) {
0579             dev_err(dev, "couldn't register PWM %d\n", id);
0580             return -1;
0581         }
0582         pwm->enabled = true;
0583     }
0584 
0585     return 0;
0586 }
0587 
0588 static struct i2c_driver lm8323_i2c_driver;
0589 
0590 static ssize_t lm8323_show_disable(struct device *dev,
0591                    struct device_attribute *attr, char *buf)
0592 {
0593     struct lm8323_chip *lm = dev_get_drvdata(dev);
0594 
0595     return sprintf(buf, "%u\n", !lm->kp_enabled);
0596 }
0597 
0598 static ssize_t lm8323_set_disable(struct device *dev,
0599                   struct device_attribute *attr,
0600                   const char *buf, size_t count)
0601 {
0602     struct lm8323_chip *lm = dev_get_drvdata(dev);
0603     int ret;
0604     unsigned int i;
0605 
0606     ret = kstrtouint(buf, 10, &i);
0607     if (ret)
0608         return ret;
0609 
0610     mutex_lock(&lm->lock);
0611     lm->kp_enabled = !i;
0612     mutex_unlock(&lm->lock);
0613 
0614     return count;
0615 }
0616 static DEVICE_ATTR(disable_kp, 0644, lm8323_show_disable, lm8323_set_disable);
0617 
0618 static int lm8323_probe(struct i2c_client *client,
0619                   const struct i2c_device_id *id)
0620 {
0621     struct lm8323_platform_data *pdata = dev_get_platdata(&client->dev);
0622     struct input_dev *idev;
0623     struct lm8323_chip *lm;
0624     int pwm;
0625     int i, err;
0626     unsigned long tmo;
0627     u8 data[2];
0628 
0629     if (!pdata || !pdata->size_x || !pdata->size_y) {
0630         dev_err(&client->dev, "missing platform_data\n");
0631         return -EINVAL;
0632     }
0633 
0634     if (pdata->size_x > 8) {
0635         dev_err(&client->dev, "invalid x size %d specified\n",
0636             pdata->size_x);
0637         return -EINVAL;
0638     }
0639 
0640     if (pdata->size_y > 12) {
0641         dev_err(&client->dev, "invalid y size %d specified\n",
0642             pdata->size_y);
0643         return -EINVAL;
0644     }
0645 
0646     lm = kzalloc(sizeof *lm, GFP_KERNEL);
0647     idev = input_allocate_device();
0648     if (!lm || !idev) {
0649         err = -ENOMEM;
0650         goto fail1;
0651     }
0652 
0653     lm->client = client;
0654     lm->idev = idev;
0655     mutex_init(&lm->lock);
0656 
0657     lm->size_x = pdata->size_x;
0658     lm->size_y = pdata->size_y;
0659     dev_vdbg(&client->dev, "Keypad size: %d x %d\n",
0660          lm->size_x, lm->size_y);
0661 
0662     lm->debounce_time = pdata->debounce_time;
0663     lm->active_time = pdata->active_time;
0664 
0665     lm8323_reset(lm);
0666 
0667     /* Nothing's set up to service the IRQ yet, so just spin for max.
0668      * 100ms until we can configure. */
0669     tmo = jiffies + msecs_to_jiffies(100);
0670     while (lm8323_read(lm, LM8323_CMD_READ_INT, data, 1) == 1) {
0671         if (data[0] & INT_NOINIT)
0672             break;
0673 
0674         if (time_after(jiffies, tmo)) {
0675             dev_err(&client->dev,
0676                 "timeout waiting for initialisation\n");
0677             break;
0678         }
0679 
0680         msleep(1);
0681     }
0682 
0683     lm8323_configure(lm);
0684 
0685     /* If a true probe check the device */
0686     if (lm8323_read_id(lm, data) != 0) {
0687         dev_err(&client->dev, "device not found\n");
0688         err = -ENODEV;
0689         goto fail1;
0690     }
0691 
0692     for (pwm = 0; pwm < LM8323_NUM_PWMS; pwm++) {
0693         err = init_pwm(lm, pwm + 1, &client->dev,
0694                    pdata->pwm_names[pwm]);
0695         if (err < 0)
0696             goto fail2;
0697     }
0698 
0699     lm->kp_enabled = true;
0700     err = device_create_file(&client->dev, &dev_attr_disable_kp);
0701     if (err < 0)
0702         goto fail2;
0703 
0704     idev->name = pdata->name ? : "LM8323 keypad";
0705     snprintf(lm->phys, sizeof(lm->phys),
0706          "%s/input-kp", dev_name(&client->dev));
0707     idev->phys = lm->phys;
0708 
0709     idev->evbit[0] = BIT(EV_KEY) | BIT(EV_MSC);
0710     __set_bit(MSC_SCAN, idev->mscbit);
0711     for (i = 0; i < LM8323_KEYMAP_SIZE; i++) {
0712         __set_bit(pdata->keymap[i], idev->keybit);
0713         lm->keymap[i] = pdata->keymap[i];
0714     }
0715     __clear_bit(KEY_RESERVED, idev->keybit);
0716 
0717     if (pdata->repeat)
0718         __set_bit(EV_REP, idev->evbit);
0719 
0720     err = input_register_device(idev);
0721     if (err) {
0722         dev_dbg(&client->dev, "error registering input device\n");
0723         goto fail3;
0724     }
0725 
0726     err = request_threaded_irq(client->irq, NULL, lm8323_irq,
0727               IRQF_TRIGGER_LOW|IRQF_ONESHOT, "lm8323", lm);
0728     if (err) {
0729         dev_err(&client->dev, "could not get IRQ %d\n", client->irq);
0730         goto fail4;
0731     }
0732 
0733     i2c_set_clientdata(client, lm);
0734 
0735     device_init_wakeup(&client->dev, 1);
0736     enable_irq_wake(client->irq);
0737 
0738     return 0;
0739 
0740 fail4:
0741     input_unregister_device(idev);
0742     idev = NULL;
0743 fail3:
0744     device_remove_file(&client->dev, &dev_attr_disable_kp);
0745 fail2:
0746     while (--pwm >= 0)
0747         if (lm->pwm[pwm].enabled)
0748             led_classdev_unregister(&lm->pwm[pwm].cdev);
0749 fail1:
0750     input_free_device(idev);
0751     kfree(lm);
0752     return err;
0753 }
0754 
0755 static int lm8323_remove(struct i2c_client *client)
0756 {
0757     struct lm8323_chip *lm = i2c_get_clientdata(client);
0758     int i;
0759 
0760     disable_irq_wake(client->irq);
0761     free_irq(client->irq, lm);
0762 
0763     input_unregister_device(lm->idev);
0764 
0765     device_remove_file(&lm->client->dev, &dev_attr_disable_kp);
0766 
0767     for (i = 0; i < 3; i++)
0768         if (lm->pwm[i].enabled)
0769             led_classdev_unregister(&lm->pwm[i].cdev);
0770 
0771     kfree(lm);
0772 
0773     return 0;
0774 }
0775 
0776 #ifdef CONFIG_PM_SLEEP
0777 /*
0778  * We don't need to explicitly suspend the chip, as it already switches off
0779  * when there's no activity.
0780  */
0781 static int lm8323_suspend(struct device *dev)
0782 {
0783     struct i2c_client *client = to_i2c_client(dev);
0784     struct lm8323_chip *lm = i2c_get_clientdata(client);
0785     int i;
0786 
0787     irq_set_irq_wake(client->irq, 0);
0788     disable_irq(client->irq);
0789 
0790     mutex_lock(&lm->lock);
0791     lm->pm_suspend = true;
0792     mutex_unlock(&lm->lock);
0793 
0794     for (i = 0; i < 3; i++)
0795         if (lm->pwm[i].enabled)
0796             led_classdev_suspend(&lm->pwm[i].cdev);
0797 
0798     return 0;
0799 }
0800 
0801 static int lm8323_resume(struct device *dev)
0802 {
0803     struct i2c_client *client = to_i2c_client(dev);
0804     struct lm8323_chip *lm = i2c_get_clientdata(client);
0805     int i;
0806 
0807     mutex_lock(&lm->lock);
0808     lm->pm_suspend = false;
0809     mutex_unlock(&lm->lock);
0810 
0811     for (i = 0; i < 3; i++)
0812         if (lm->pwm[i].enabled)
0813             led_classdev_resume(&lm->pwm[i].cdev);
0814 
0815     enable_irq(client->irq);
0816     irq_set_irq_wake(client->irq, 1);
0817 
0818     return 0;
0819 }
0820 #endif
0821 
0822 static SIMPLE_DEV_PM_OPS(lm8323_pm_ops, lm8323_suspend, lm8323_resume);
0823 
0824 static const struct i2c_device_id lm8323_id[] = {
0825     { "lm8323", 0 },
0826     { }
0827 };
0828 
0829 static struct i2c_driver lm8323_i2c_driver = {
0830     .driver = {
0831         .name   = "lm8323",
0832         .pm = &lm8323_pm_ops,
0833     },
0834     .probe      = lm8323_probe,
0835     .remove     = lm8323_remove,
0836     .id_table   = lm8323_id,
0837 };
0838 MODULE_DEVICE_TABLE(i2c, lm8323_id);
0839 
0840 module_i2c_driver(lm8323_i2c_driver);
0841 
0842 MODULE_AUTHOR("Timo O. Karjalainen <timo.o.karjalainen@nokia.com>");
0843 MODULE_AUTHOR("Daniel Stone");
0844 MODULE_AUTHOR("Felipe Balbi <felipe.balbi@nokia.com>");
0845 MODULE_DESCRIPTION("LM8323 keypad driver");
0846 MODULE_LICENSE("GPL");
0847