Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * twl4030_keypad.c - driver for 8x8 keypad controller in twl4030 chips
0004  *
0005  * Copyright (C) 2007 Texas Instruments, Inc.
0006  * Copyright (C) 2008 Nokia Corporation
0007  *
0008  * Code re-written for 2430SDP by:
0009  * Syed Mohammed Khasim <x0khasim@ti.com>
0010  *
0011  * Initial Code:
0012  * Manjunatha G K <manjugk@ti.com>
0013  */
0014 
0015 #include <linux/kernel.h>
0016 #include <linux/module.h>
0017 #include <linux/interrupt.h>
0018 #include <linux/input.h>
0019 #include <linux/platform_device.h>
0020 #include <linux/mfd/twl.h>
0021 #include <linux/slab.h>
0022 #include <linux/of.h>
0023 
0024 /*
0025  * The TWL4030 family chips include a keypad controller that supports
0026  * up to an 8x8 switch matrix.  The controller can issue system wakeup
0027  * events, since it uses only the always-on 32KiHz oscillator, and has
0028  * an internal state machine that decodes pressed keys, including
0029  * multi-key combinations.
0030  *
0031  * This driver lets boards define what keycodes they wish to report for
0032  * which scancodes, as part of the "struct twl4030_keypad_data" used in
0033  * the probe() routine.
0034  *
0035  * See the TPS65950 documentation; that's the general availability
0036  * version of the TWL5030 second generation part.
0037  */
0038 #define TWL4030_MAX_ROWS    8   /* TWL4030 hard limit */
0039 #define TWL4030_MAX_COLS    8
0040 /*
0041  * Note that we add space for an extra column so that we can handle
0042  * row lines connected to the gnd (see twl4030_col_xlate()).
0043  */
0044 #define TWL4030_ROW_SHIFT   4
0045 #define TWL4030_KEYMAP_SIZE (TWL4030_MAX_ROWS << TWL4030_ROW_SHIFT)
0046 
0047 struct twl4030_keypad {
0048     unsigned short  keymap[TWL4030_KEYMAP_SIZE];
0049     u16     kp_state[TWL4030_MAX_ROWS];
0050     bool        autorepeat;
0051     unsigned int    n_rows;
0052     unsigned int    n_cols;
0053     int     irq;
0054 
0055     struct device *dbg_dev;
0056     struct input_dev *input;
0057 };
0058 
0059 /*----------------------------------------------------------------------*/
0060 
0061 /* arbitrary prescaler value 0..7 */
0062 #define PTV_PRESCALER           4
0063 
0064 /* Register Offsets */
0065 #define KEYP_CTRL           0x00
0066 #define KEYP_DEB            0x01
0067 #define KEYP_LONG_KEY           0x02
0068 #define KEYP_LK_PTV         0x03
0069 #define KEYP_TIMEOUT_L          0x04
0070 #define KEYP_TIMEOUT_H          0x05
0071 #define KEYP_KBC            0x06
0072 #define KEYP_KBR            0x07
0073 #define KEYP_SMS            0x08
0074 #define KEYP_FULL_CODE_7_0      0x09    /* row 0 column status */
0075 #define KEYP_FULL_CODE_15_8     0x0a    /* ... row 1 ... */
0076 #define KEYP_FULL_CODE_23_16        0x0b
0077 #define KEYP_FULL_CODE_31_24        0x0c
0078 #define KEYP_FULL_CODE_39_32        0x0d
0079 #define KEYP_FULL_CODE_47_40        0x0e
0080 #define KEYP_FULL_CODE_55_48        0x0f
0081 #define KEYP_FULL_CODE_63_56        0x10
0082 #define KEYP_ISR1           0x11
0083 #define KEYP_IMR1           0x12
0084 #define KEYP_ISR2           0x13
0085 #define KEYP_IMR2           0x14
0086 #define KEYP_SIR            0x15
0087 #define KEYP_EDR            0x16    /* edge triggers */
0088 #define KEYP_SIH_CTRL           0x17
0089 
0090 /* KEYP_CTRL_REG Fields */
0091 #define KEYP_CTRL_SOFT_NRST     BIT(0)
0092 #define KEYP_CTRL_SOFTMODEN     BIT(1)
0093 #define KEYP_CTRL_LK_EN         BIT(2)
0094 #define KEYP_CTRL_TOE_EN        BIT(3)
0095 #define KEYP_CTRL_TOLE_EN       BIT(4)
0096 #define KEYP_CTRL_RP_EN         BIT(5)
0097 #define KEYP_CTRL_KBD_ON        BIT(6)
0098 
0099 /* KEYP_DEB, KEYP_LONG_KEY, KEYP_TIMEOUT_x*/
0100 #define KEYP_PERIOD_US(t, prescale) ((t) / (31 << ((prescale) + 1)) - 1)
0101 
0102 /* KEYP_LK_PTV_REG Fields */
0103 #define KEYP_LK_PTV_PTV_SHIFT       5
0104 
0105 /* KEYP_{IMR,ISR,SIR} Fields */
0106 #define KEYP_IMR1_MIS           BIT(3)
0107 #define KEYP_IMR1_TO            BIT(2)
0108 #define KEYP_IMR1_LK            BIT(1)
0109 #define KEYP_IMR1_KP            BIT(0)
0110 
0111 /* KEYP_EDR Fields */
0112 #define KEYP_EDR_KP_FALLING     0x01
0113 #define KEYP_EDR_KP_RISING      0x02
0114 #define KEYP_EDR_KP_BOTH        0x03
0115 #define KEYP_EDR_LK_FALLING     0x04
0116 #define KEYP_EDR_LK_RISING      0x08
0117 #define KEYP_EDR_TO_FALLING     0x10
0118 #define KEYP_EDR_TO_RISING      0x20
0119 #define KEYP_EDR_MIS_FALLING        0x40
0120 #define KEYP_EDR_MIS_RISING     0x80
0121 
0122 
0123 /*----------------------------------------------------------------------*/
0124 
0125 static int twl4030_kpread(struct twl4030_keypad *kp,
0126         u8 *data, u32 reg, u8 num_bytes)
0127 {
0128     int ret = twl_i2c_read(TWL4030_MODULE_KEYPAD, data, reg, num_bytes);
0129 
0130     if (ret < 0)
0131         dev_warn(kp->dbg_dev,
0132             "Couldn't read TWL4030: %X - ret %d[%x]\n",
0133              reg, ret, ret);
0134 
0135     return ret;
0136 }
0137 
0138 static int twl4030_kpwrite_u8(struct twl4030_keypad *kp, u8 data, u32 reg)
0139 {
0140     int ret = twl_i2c_write_u8(TWL4030_MODULE_KEYPAD, data, reg);
0141 
0142     if (ret < 0)
0143         dev_warn(kp->dbg_dev,
0144             "Could not write TWL4030: %X - ret %d[%x]\n",
0145              reg, ret, ret);
0146 
0147     return ret;
0148 }
0149 
0150 static inline u16 twl4030_col_xlate(struct twl4030_keypad *kp, u8 col)
0151 {
0152     /*
0153      * If all bits in a row are active for all columns then
0154      * we have that row line connected to gnd. Mark this
0155      * key on as if it was on matrix position n_cols (i.e.
0156      * one higher than the size of the matrix).
0157      */
0158     if (col == 0xFF)
0159         return 1 << kp->n_cols;
0160     else
0161         return col & ((1 << kp->n_cols) - 1);
0162 }
0163 
0164 static int twl4030_read_kp_matrix_state(struct twl4030_keypad *kp, u16 *state)
0165 {
0166     u8 new_state[TWL4030_MAX_ROWS];
0167     int row;
0168     int ret = twl4030_kpread(kp, new_state,
0169                  KEYP_FULL_CODE_7_0, kp->n_rows);
0170     if (ret >= 0)
0171         for (row = 0; row < kp->n_rows; row++)
0172             state[row] = twl4030_col_xlate(kp, new_state[row]);
0173 
0174     return ret;
0175 }
0176 
0177 static bool twl4030_is_in_ghost_state(struct twl4030_keypad *kp, u16 *key_state)
0178 {
0179     int i;
0180     u16 check = 0;
0181 
0182     for (i = 0; i < kp->n_rows; i++) {
0183         u16 col = key_state[i];
0184 
0185         if ((col & check) && hweight16(col) > 1)
0186             return true;
0187 
0188         check |= col;
0189     }
0190 
0191     return false;
0192 }
0193 
0194 static void twl4030_kp_scan(struct twl4030_keypad *kp, bool release_all)
0195 {
0196     struct input_dev *input = kp->input;
0197     u16 new_state[TWL4030_MAX_ROWS];
0198     int col, row;
0199 
0200     if (release_all) {
0201         memset(new_state, 0, sizeof(new_state));
0202     } else {
0203         /* check for any changes */
0204         int ret = twl4030_read_kp_matrix_state(kp, new_state);
0205 
0206         if (ret < 0)    /* panic ... */
0207             return;
0208 
0209         if (twl4030_is_in_ghost_state(kp, new_state))
0210             return;
0211     }
0212 
0213     /* check for changes and print those */
0214     for (row = 0; row < kp->n_rows; row++) {
0215         int changed = new_state[row] ^ kp->kp_state[row];
0216 
0217         if (!changed)
0218             continue;
0219 
0220         /* Extra column handles "all gnd" rows */
0221         for (col = 0; col < kp->n_cols + 1; col++) {
0222             int code;
0223 
0224             if (!(changed & (1 << col)))
0225                 continue;
0226 
0227             dev_dbg(kp->dbg_dev, "key [%d:%d] %s\n", row, col,
0228                 (new_state[row] & (1 << col)) ?
0229                 "press" : "release");
0230 
0231             code = MATRIX_SCAN_CODE(row, col, TWL4030_ROW_SHIFT);
0232             input_event(input, EV_MSC, MSC_SCAN, code);
0233             input_report_key(input, kp->keymap[code],
0234                      new_state[row] & (1 << col));
0235         }
0236         kp->kp_state[row] = new_state[row];
0237     }
0238     input_sync(input);
0239 }
0240 
0241 /*
0242  * Keypad interrupt handler
0243  */
0244 static irqreturn_t do_kp_irq(int irq, void *_kp)
0245 {
0246     struct twl4030_keypad *kp = _kp;
0247     u8 reg;
0248     int ret;
0249 
0250     /* Read & Clear TWL4030 pending interrupt */
0251     ret = twl4030_kpread(kp, &reg, KEYP_ISR1, 1);
0252 
0253     /*
0254      * Release all keys if I2C has gone bad or
0255      * the KEYP has gone to idle state.
0256      */
0257     if (ret >= 0 && (reg & KEYP_IMR1_KP))
0258         twl4030_kp_scan(kp, false);
0259     else
0260         twl4030_kp_scan(kp, true);
0261 
0262     return IRQ_HANDLED;
0263 }
0264 
0265 static int twl4030_kp_program(struct twl4030_keypad *kp)
0266 {
0267     u8 reg;
0268     int i;
0269 
0270     /* Enable controller, with hardware decoding but not autorepeat */
0271     reg = KEYP_CTRL_SOFT_NRST | KEYP_CTRL_SOFTMODEN
0272         | KEYP_CTRL_TOE_EN | KEYP_CTRL_KBD_ON;
0273     if (twl4030_kpwrite_u8(kp, reg, KEYP_CTRL) < 0)
0274         return -EIO;
0275 
0276     /*
0277      * NOTE: we could use sih_setup() here to package keypad
0278      * event sources as four different IRQs ... but we don't.
0279      */
0280 
0281     /* Enable TO rising and KP rising and falling edge detection */
0282     reg = KEYP_EDR_KP_BOTH | KEYP_EDR_TO_RISING;
0283     if (twl4030_kpwrite_u8(kp, reg, KEYP_EDR) < 0)
0284         return -EIO;
0285 
0286     /* Set PTV prescaler Field */
0287     reg = (PTV_PRESCALER << KEYP_LK_PTV_PTV_SHIFT);
0288     if (twl4030_kpwrite_u8(kp, reg, KEYP_LK_PTV) < 0)
0289         return -EIO;
0290 
0291     /* Set key debounce time to 20 ms */
0292     i = KEYP_PERIOD_US(20000, PTV_PRESCALER);
0293     if (twl4030_kpwrite_u8(kp, i, KEYP_DEB) < 0)
0294         return -EIO;
0295 
0296     /* Set timeout period to 200 ms */
0297     i = KEYP_PERIOD_US(200000, PTV_PRESCALER);
0298     if (twl4030_kpwrite_u8(kp, (i & 0xFF), KEYP_TIMEOUT_L) < 0)
0299         return -EIO;
0300 
0301     if (twl4030_kpwrite_u8(kp, (i >> 8), KEYP_TIMEOUT_H) < 0)
0302         return -EIO;
0303 
0304     /*
0305      * Enable Clear-on-Read; disable remembering events that fire
0306      * after the IRQ but before our handler acks (reads) them.
0307      */
0308     reg = TWL4030_SIH_CTRL_COR_MASK | TWL4030_SIH_CTRL_PENDDIS_MASK;
0309     if (twl4030_kpwrite_u8(kp, reg, KEYP_SIH_CTRL) < 0)
0310         return -EIO;
0311 
0312     /* initialize key state; irqs update it from here on */
0313     if (twl4030_read_kp_matrix_state(kp, kp->kp_state) < 0)
0314         return -EIO;
0315 
0316     return 0;
0317 }
0318 
0319 /*
0320  * Registers keypad device with input subsystem
0321  * and configures TWL4030 keypad registers
0322  */
0323 static int twl4030_kp_probe(struct platform_device *pdev)
0324 {
0325     struct twl4030_keypad_data *pdata = dev_get_platdata(&pdev->dev);
0326     const struct matrix_keymap_data *keymap_data = NULL;
0327     struct twl4030_keypad *kp;
0328     struct input_dev *input;
0329     u8 reg;
0330     int error;
0331 
0332     kp = devm_kzalloc(&pdev->dev, sizeof(*kp), GFP_KERNEL);
0333     if (!kp)
0334         return -ENOMEM;
0335 
0336     input = devm_input_allocate_device(&pdev->dev);
0337     if (!input)
0338         return -ENOMEM;
0339 
0340     /* get the debug device */
0341     kp->dbg_dev     = &pdev->dev;
0342     kp->input       = input;
0343 
0344     /* setup input device */
0345     input->name     = "TWL4030 Keypad";
0346     input->phys     = "twl4030_keypad/input0";
0347 
0348     input->id.bustype   = BUS_HOST;
0349     input->id.vendor    = 0x0001;
0350     input->id.product   = 0x0001;
0351     input->id.version   = 0x0003;
0352 
0353     if (pdata) {
0354         if (!pdata->rows || !pdata->cols || !pdata->keymap_data) {
0355             dev_err(&pdev->dev, "Missing platform_data\n");
0356             return -EINVAL;
0357         }
0358 
0359         kp->n_rows = pdata->rows;
0360         kp->n_cols = pdata->cols;
0361         kp->autorepeat = pdata->rep;
0362         keymap_data = pdata->keymap_data;
0363     } else {
0364         error = matrix_keypad_parse_properties(&pdev->dev, &kp->n_rows,
0365                                &kp->n_cols);
0366         if (error)
0367             return error;
0368 
0369         kp->autorepeat = true;
0370     }
0371 
0372     if (kp->n_rows > TWL4030_MAX_ROWS || kp->n_cols > TWL4030_MAX_COLS) {
0373         dev_err(&pdev->dev,
0374             "Invalid rows/cols amount specified in platform/devicetree data\n");
0375         return -EINVAL;
0376     }
0377 
0378     kp->irq = platform_get_irq(pdev, 0);
0379     if (kp->irq < 0)
0380         return kp->irq;
0381 
0382     error = matrix_keypad_build_keymap(keymap_data, NULL,
0383                        TWL4030_MAX_ROWS,
0384                        1 << TWL4030_ROW_SHIFT,
0385                        kp->keymap, input);
0386     if (error) {
0387         dev_err(kp->dbg_dev, "Failed to build keymap\n");
0388         return error;
0389     }
0390 
0391     input_set_capability(input, EV_MSC, MSC_SCAN);
0392     /* Enable auto repeat feature of Linux input subsystem */
0393     if (kp->autorepeat)
0394         __set_bit(EV_REP, input->evbit);
0395 
0396     error = input_register_device(input);
0397     if (error) {
0398         dev_err(kp->dbg_dev,
0399             "Unable to register twl4030 keypad device\n");
0400         return error;
0401     }
0402 
0403     error = twl4030_kp_program(kp);
0404     if (error)
0405         return error;
0406 
0407     /*
0408      * This ISR will always execute in kernel thread context because of
0409      * the need to access the TWL4030 over the I2C bus.
0410      *
0411      * NOTE:  we assume this host is wired to TWL4040 INT1, not INT2 ...
0412      */
0413     error = devm_request_threaded_irq(&pdev->dev, kp->irq, NULL, do_kp_irq,
0414                       0, pdev->name, kp);
0415     if (error) {
0416         dev_info(kp->dbg_dev, "request_irq failed for irq no=%d: %d\n",
0417             kp->irq, error);
0418         return error;
0419     }
0420 
0421     /* Enable KP and TO interrupts now. */
0422     reg = (u8) ~(KEYP_IMR1_KP | KEYP_IMR1_TO);
0423     if (twl4030_kpwrite_u8(kp, reg, KEYP_IMR1)) {
0424         /* mask all events - we don't care about the result */
0425         (void) twl4030_kpwrite_u8(kp, 0xff, KEYP_IMR1);
0426         return -EIO;
0427     }
0428 
0429     return 0;
0430 }
0431 
0432 #ifdef CONFIG_OF
0433 static const struct of_device_id twl4030_keypad_dt_match_table[] = {
0434     { .compatible = "ti,twl4030-keypad" },
0435     {},
0436 };
0437 MODULE_DEVICE_TABLE(of, twl4030_keypad_dt_match_table);
0438 #endif
0439 
0440 /*
0441  * NOTE: twl4030 are multi-function devices connected via I2C.
0442  * So this device is a child of an I2C parent, thus it needs to
0443  * support unplug/replug (which most platform devices don't).
0444  */
0445 
0446 static struct platform_driver twl4030_kp_driver = {
0447     .probe      = twl4030_kp_probe,
0448     .driver     = {
0449         .name   = "twl4030_keypad",
0450         .of_match_table = of_match_ptr(twl4030_keypad_dt_match_table),
0451     },
0452 };
0453 module_platform_driver(twl4030_kp_driver);
0454 
0455 MODULE_AUTHOR("Texas Instruments");
0456 MODULE_DESCRIPTION("TWL4030 Keypad Driver");
0457 MODULE_LICENSE("GPL");
0458 MODULE_ALIAS("platform:twl4030_keypad");