Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (C) ST-Ericsson SA 2010
0004  *
0005  * Author: Rabin Vincent <rabin.vincent@stericsson.com> for ST-Ericsson
0006  */
0007 
0008 #include <linux/module.h>
0009 #include <linux/slab.h>
0010 #include <linux/input.h>
0011 #include <linux/interrupt.h>
0012 #include <linux/platform_device.h>
0013 #include <linux/input/matrix_keypad.h>
0014 #include <linux/mfd/stmpe.h>
0015 
0016 /* These are at the same addresses in all STMPE variants */
0017 #define STMPE_KPC_COL           0x60
0018 #define STMPE_KPC_ROW_MSB       0x61
0019 #define STMPE_KPC_ROW_LSB       0x62
0020 #define STMPE_KPC_CTRL_MSB      0x63
0021 #define STMPE_KPC_CTRL_LSB      0x64
0022 #define STMPE_KPC_COMBI_KEY_0       0x65
0023 #define STMPE_KPC_COMBI_KEY_1       0x66
0024 #define STMPE_KPC_COMBI_KEY_2       0x67
0025 #define STMPE_KPC_DATA_BYTE0        0x68
0026 #define STMPE_KPC_DATA_BYTE1        0x69
0027 #define STMPE_KPC_DATA_BYTE2        0x6a
0028 #define STMPE_KPC_DATA_BYTE3        0x6b
0029 #define STMPE_KPC_DATA_BYTE4        0x6c
0030 
0031 #define STMPE_KPC_CTRL_LSB_SCAN     (0x1 << 0)
0032 #define STMPE_KPC_CTRL_LSB_DEBOUNCE (0x7f << 1)
0033 #define STMPE_KPC_CTRL_MSB_SCAN_COUNT   (0xf << 4)
0034 
0035 #define STMPE_KPC_ROW_MSB_ROWS      0xff
0036 
0037 #define STMPE_KPC_DATA_UP       (0x1 << 7)
0038 #define STMPE_KPC_DATA_ROW      (0xf << 3)
0039 #define STMPE_KPC_DATA_COL      (0x7 << 0)
0040 #define STMPE_KPC_DATA_NOKEY_MASK   0x78
0041 
0042 #define STMPE_KEYPAD_MAX_DEBOUNCE   127
0043 #define STMPE_KEYPAD_MAX_SCAN_COUNT 15
0044 
0045 #define STMPE_KEYPAD_MAX_ROWS       8
0046 #define STMPE_KEYPAD_MAX_COLS       8
0047 #define STMPE_KEYPAD_ROW_SHIFT      3
0048 #define STMPE_KEYPAD_KEYMAP_MAX_SIZE \
0049     (STMPE_KEYPAD_MAX_ROWS * STMPE_KEYPAD_MAX_COLS)
0050 
0051 
0052 #define STMPE1601_NUM_DATA  5
0053 #define STMPE2401_NUM_DATA  3
0054 #define STMPE2403_NUM_DATA  5
0055 
0056 /* Make sure it covers all cases above */
0057 #define MAX_NUM_DATA        5
0058 
0059 /**
0060  * struct stmpe_keypad_variant - model-specific attributes
0061  * @auto_increment: whether the KPC_DATA_BYTE register address
0062  *          auto-increments on multiple read
0063  * @set_pullup: whether the pins need to have their pull-ups set
0064  * @num_data: number of data bytes
0065  * @num_normal_data: number of normal keys' data bytes
0066  * @max_cols: maximum number of columns supported
0067  * @max_rows: maximum number of rows supported
0068  * @col_gpios: bitmask of gpios which can be used for columns
0069  * @row_gpios: bitmask of gpios which can be used for rows
0070  */
0071 struct stmpe_keypad_variant {
0072     bool        auto_increment;
0073     bool        set_pullup;
0074     int     num_data;
0075     int     num_normal_data;
0076     int     max_cols;
0077     int     max_rows;
0078     unsigned int    col_gpios;
0079     unsigned int    row_gpios;
0080 };
0081 
0082 static const struct stmpe_keypad_variant stmpe_keypad_variants[] = {
0083     [STMPE1601] = {
0084         .auto_increment     = true,
0085         .num_data       = STMPE1601_NUM_DATA,
0086         .num_normal_data    = 3,
0087         .max_cols       = 8,
0088         .max_rows       = 8,
0089         .col_gpios      = 0x000ff,  /* GPIO 0 - 7 */
0090         .row_gpios      = 0x0ff00,  /* GPIO 8 - 15 */
0091     },
0092     [STMPE2401] = {
0093         .auto_increment     = false,
0094         .set_pullup     = true,
0095         .num_data       = STMPE2401_NUM_DATA,
0096         .num_normal_data    = 2,
0097         .max_cols       = 8,
0098         .max_rows       = 12,
0099         .col_gpios      = 0x0000ff, /* GPIO 0 - 7*/
0100         .row_gpios      = 0x1f7f00, /* GPIO 8-14, 16-20 */
0101     },
0102     [STMPE2403] = {
0103         .auto_increment     = true,
0104         .set_pullup     = true,
0105         .num_data       = STMPE2403_NUM_DATA,
0106         .num_normal_data    = 3,
0107         .max_cols       = 8,
0108         .max_rows       = 12,
0109         .col_gpios      = 0x0000ff, /* GPIO 0 - 7*/
0110         .row_gpios      = 0x1fef00, /* GPIO 8-14, 16-20 */
0111     },
0112 };
0113 
0114 /**
0115  * struct stmpe_keypad - STMPE keypad state container
0116  * @stmpe: pointer to parent STMPE device
0117  * @input: spawned input device
0118  * @variant: STMPE variant
0119  * @debounce_ms: debounce interval, in ms.  Maximum is
0120  *       %STMPE_KEYPAD_MAX_DEBOUNCE.
0121  * @scan_count: number of key scanning cycles to confirm key data.
0122  *      Maximum is %STMPE_KEYPAD_MAX_SCAN_COUNT.
0123  * @no_autorepeat: disable key autorepeat
0124  * @rows: bitmask for the rows
0125  * @cols: bitmask for the columns
0126  * @keymap: the keymap
0127  */
0128 struct stmpe_keypad {
0129     struct stmpe *stmpe;
0130     struct input_dev *input;
0131     const struct stmpe_keypad_variant *variant;
0132     unsigned int debounce_ms;
0133     unsigned int scan_count;
0134     bool no_autorepeat;
0135     unsigned int rows;
0136     unsigned int cols;
0137     unsigned short keymap[STMPE_KEYPAD_KEYMAP_MAX_SIZE];
0138 };
0139 
0140 static int stmpe_keypad_read_data(struct stmpe_keypad *keypad, u8 *data)
0141 {
0142     const struct stmpe_keypad_variant *variant = keypad->variant;
0143     struct stmpe *stmpe = keypad->stmpe;
0144     int ret;
0145     int i;
0146 
0147     if (variant->auto_increment)
0148         return stmpe_block_read(stmpe, STMPE_KPC_DATA_BYTE0,
0149                     variant->num_data, data);
0150 
0151     for (i = 0; i < variant->num_data; i++) {
0152         ret = stmpe_reg_read(stmpe, STMPE_KPC_DATA_BYTE0 + i);
0153         if (ret < 0)
0154             return ret;
0155 
0156         data[i] = ret;
0157     }
0158 
0159     return 0;
0160 }
0161 
0162 static irqreturn_t stmpe_keypad_irq(int irq, void *dev)
0163 {
0164     struct stmpe_keypad *keypad = dev;
0165     struct input_dev *input = keypad->input;
0166     const struct stmpe_keypad_variant *variant = keypad->variant;
0167     u8 fifo[MAX_NUM_DATA];
0168     int ret;
0169     int i;
0170 
0171     ret = stmpe_keypad_read_data(keypad, fifo);
0172     if (ret < 0)
0173         return IRQ_NONE;
0174 
0175     for (i = 0; i < variant->num_normal_data; i++) {
0176         u8 data = fifo[i];
0177         int row = (data & STMPE_KPC_DATA_ROW) >> 3;
0178         int col = data & STMPE_KPC_DATA_COL;
0179         int code = MATRIX_SCAN_CODE(row, col, STMPE_KEYPAD_ROW_SHIFT);
0180         bool up = data & STMPE_KPC_DATA_UP;
0181 
0182         if ((data & STMPE_KPC_DATA_NOKEY_MASK)
0183             == STMPE_KPC_DATA_NOKEY_MASK)
0184             continue;
0185 
0186         input_event(input, EV_MSC, MSC_SCAN, code);
0187         input_report_key(input, keypad->keymap[code], !up);
0188         input_sync(input);
0189     }
0190 
0191     return IRQ_HANDLED;
0192 }
0193 
0194 static int stmpe_keypad_altfunc_init(struct stmpe_keypad *keypad)
0195 {
0196     const struct stmpe_keypad_variant *variant = keypad->variant;
0197     unsigned int col_gpios = variant->col_gpios;
0198     unsigned int row_gpios = variant->row_gpios;
0199     struct stmpe *stmpe = keypad->stmpe;
0200     u8 pureg = stmpe->regs[STMPE_IDX_GPPUR_LSB];
0201     unsigned int pins = 0;
0202     unsigned int pu_pins = 0;
0203     int ret;
0204     int i;
0205 
0206     /*
0207      * Figure out which pins need to be set to the keypad alternate
0208      * function.
0209      *
0210      * {cols,rows}_gpios are bitmasks of which pins on the chip can be used
0211      * for the keypad.
0212      *
0213      * keypad->{cols,rows} are a bitmask of which pins (of the ones useable
0214      * for the keypad) are used on the board.
0215      */
0216 
0217     for (i = 0; i < variant->max_cols; i++) {
0218         int num = __ffs(col_gpios);
0219 
0220         if (keypad->cols & (1 << i)) {
0221             pins |= 1 << num;
0222             pu_pins |= 1 << num;
0223         }
0224 
0225         col_gpios &= ~(1 << num);
0226     }
0227 
0228     for (i = 0; i < variant->max_rows; i++) {
0229         int num = __ffs(row_gpios);
0230 
0231         if (keypad->rows & (1 << i))
0232             pins |= 1 << num;
0233 
0234         row_gpios &= ~(1 << num);
0235     }
0236 
0237     ret = stmpe_set_altfunc(stmpe, pins, STMPE_BLOCK_KEYPAD);
0238     if (ret)
0239         return ret;
0240 
0241     /*
0242      * On STMPE24xx, set pin bias to pull-up on all keypad input
0243      * pins (columns), this incidentally happen to be maximum 8 pins
0244      * and placed at GPIO0-7 so only the LSB of the pull up register
0245      * ever needs to be written.
0246      */
0247     if (variant->set_pullup) {
0248         u8 val;
0249 
0250         ret = stmpe_reg_read(stmpe, pureg);
0251         if (ret)
0252             return ret;
0253 
0254         /* Do not touch unused pins, may be used for GPIO */
0255         val = ret & ~pu_pins;
0256         val |= pu_pins;
0257 
0258         ret = stmpe_reg_write(stmpe, pureg, val);
0259     }
0260 
0261     return 0;
0262 }
0263 
0264 static int stmpe_keypad_chip_init(struct stmpe_keypad *keypad)
0265 {
0266     const struct stmpe_keypad_variant *variant = keypad->variant;
0267     struct stmpe *stmpe = keypad->stmpe;
0268     int ret;
0269 
0270     if (keypad->debounce_ms > STMPE_KEYPAD_MAX_DEBOUNCE)
0271         return -EINVAL;
0272 
0273     if (keypad->scan_count > STMPE_KEYPAD_MAX_SCAN_COUNT)
0274         return -EINVAL;
0275 
0276     ret = stmpe_enable(stmpe, STMPE_BLOCK_KEYPAD);
0277     if (ret < 0)
0278         return ret;
0279 
0280     ret = stmpe_keypad_altfunc_init(keypad);
0281     if (ret < 0)
0282         return ret;
0283 
0284     ret = stmpe_reg_write(stmpe, STMPE_KPC_COL, keypad->cols);
0285     if (ret < 0)
0286         return ret;
0287 
0288     ret = stmpe_reg_write(stmpe, STMPE_KPC_ROW_LSB, keypad->rows);
0289     if (ret < 0)
0290         return ret;
0291 
0292     if (variant->max_rows > 8) {
0293         ret = stmpe_set_bits(stmpe, STMPE_KPC_ROW_MSB,
0294                      STMPE_KPC_ROW_MSB_ROWS,
0295                      keypad->rows >> 8);
0296         if (ret < 0)
0297             return ret;
0298     }
0299 
0300     ret = stmpe_set_bits(stmpe, STMPE_KPC_CTRL_MSB,
0301                  STMPE_KPC_CTRL_MSB_SCAN_COUNT,
0302                  keypad->scan_count << 4);
0303     if (ret < 0)
0304         return ret;
0305 
0306     return stmpe_set_bits(stmpe, STMPE_KPC_CTRL_LSB,
0307                   STMPE_KPC_CTRL_LSB_SCAN |
0308                   STMPE_KPC_CTRL_LSB_DEBOUNCE,
0309                   STMPE_KPC_CTRL_LSB_SCAN |
0310                   (keypad->debounce_ms << 1));
0311 }
0312 
0313 static void stmpe_keypad_fill_used_pins(struct stmpe_keypad *keypad,
0314                     u32 used_rows, u32 used_cols)
0315 {
0316     int row, col;
0317 
0318     for (row = 0; row < used_rows; row++) {
0319         for (col = 0; col < used_cols; col++) {
0320             int code = MATRIX_SCAN_CODE(row, col,
0321                             STMPE_KEYPAD_ROW_SHIFT);
0322             if (keypad->keymap[code] != KEY_RESERVED) {
0323                 keypad->rows |= 1 << row;
0324                 keypad->cols |= 1 << col;
0325             }
0326         }
0327     }
0328 }
0329 
0330 static int stmpe_keypad_probe(struct platform_device *pdev)
0331 {
0332     struct stmpe *stmpe = dev_get_drvdata(pdev->dev.parent);
0333     struct device_node *np = pdev->dev.of_node;
0334     struct stmpe_keypad *keypad;
0335     struct input_dev *input;
0336     u32 rows;
0337     u32 cols;
0338     int error;
0339     int irq;
0340 
0341     irq = platform_get_irq(pdev, 0);
0342     if (irq < 0)
0343         return irq;
0344 
0345     keypad = devm_kzalloc(&pdev->dev, sizeof(struct stmpe_keypad),
0346                   GFP_KERNEL);
0347     if (!keypad)
0348         return -ENOMEM;
0349 
0350     keypad->stmpe = stmpe;
0351     keypad->variant = &stmpe_keypad_variants[stmpe->partnum];
0352 
0353     of_property_read_u32(np, "debounce-interval", &keypad->debounce_ms);
0354     of_property_read_u32(np, "st,scan-count", &keypad->scan_count);
0355     keypad->no_autorepeat = of_property_read_bool(np, "st,no-autorepeat");
0356 
0357     input = devm_input_allocate_device(&pdev->dev);
0358     if (!input)
0359         return -ENOMEM;
0360 
0361     input->name = "STMPE keypad";
0362     input->id.bustype = BUS_I2C;
0363     input->dev.parent = &pdev->dev;
0364 
0365     error = matrix_keypad_parse_properties(&pdev->dev, &rows, &cols);
0366     if (error)
0367         return error;
0368 
0369     error = matrix_keypad_build_keymap(NULL, NULL, rows, cols,
0370                        keypad->keymap, input);
0371     if (error)
0372         return error;
0373 
0374     input_set_capability(input, EV_MSC, MSC_SCAN);
0375     if (!keypad->no_autorepeat)
0376         __set_bit(EV_REP, input->evbit);
0377 
0378     stmpe_keypad_fill_used_pins(keypad, rows, cols);
0379 
0380     keypad->input = input;
0381 
0382     error = stmpe_keypad_chip_init(keypad);
0383     if (error < 0)
0384         return error;
0385 
0386     error = devm_request_threaded_irq(&pdev->dev, irq,
0387                       NULL, stmpe_keypad_irq,
0388                       IRQF_ONESHOT, "stmpe-keypad", keypad);
0389     if (error) {
0390         dev_err(&pdev->dev, "unable to get irq: %d\n", error);
0391         return error;
0392     }
0393 
0394     error = input_register_device(input);
0395     if (error) {
0396         dev_err(&pdev->dev,
0397             "unable to register input device: %d\n", error);
0398         return error;
0399     }
0400 
0401     platform_set_drvdata(pdev, keypad);
0402 
0403     return 0;
0404 }
0405 
0406 static int stmpe_keypad_remove(struct platform_device *pdev)
0407 {
0408     struct stmpe_keypad *keypad = platform_get_drvdata(pdev);
0409 
0410     stmpe_disable(keypad->stmpe, STMPE_BLOCK_KEYPAD);
0411 
0412     return 0;
0413 }
0414 
0415 static struct platform_driver stmpe_keypad_driver = {
0416     .driver.name    = "stmpe-keypad",
0417     .driver.owner   = THIS_MODULE,
0418     .probe      = stmpe_keypad_probe,
0419     .remove     = stmpe_keypad_remove,
0420 };
0421 module_platform_driver(stmpe_keypad_driver);
0422 
0423 MODULE_LICENSE("GPL v2");
0424 MODULE_DESCRIPTION("STMPExxxx keypad driver");
0425 MODULE_AUTHOR("Rabin Vincent <rabin.vincent@stericsson.com>");