Path: blob/master/drivers/input/keyboard/twl4030_keypad.c
15109 views
/*1* twl4030_keypad.c - driver for 8x8 keypad controller in twl4030 chips2*3* Copyright (C) 2007 Texas Instruments, Inc.4* Copyright (C) 2008 Nokia Corporation5*6* Code re-written for 2430SDP by:7* Syed Mohammed Khasim <[email protected]>8*9* Initial Code:10* Manjunatha G K <[email protected]>11*12* This program is free software; you can redistribute it and/or modify13* it under the terms of the GNU General Public License as published by14* the Free Software Foundation; either version 2 of the License, or15* (at your option) any later version.16*17* This program is distributed in the hope that it will be useful,18* but WITHOUT ANY WARRANTY; without even the implied warranty of19* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the20* GNU General Public License for more details.21*22* You should have received a copy of the GNU General Public License23* along with this program; if not, write to the Free Software24* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA25*/2627#include <linux/kernel.h>28#include <linux/module.h>29#include <linux/init.h>30#include <linux/interrupt.h>31#include <linux/input.h>32#include <linux/platform_device.h>33#include <linux/i2c/twl.h>34#include <linux/slab.h>353637/*38* The TWL4030 family chips include a keypad controller that supports39* up to an 8x8 switch matrix. The controller can issue system wakeup40* events, since it uses only the always-on 32KiHz oscillator, and has41* an internal state machine that decodes pressed keys, including42* multi-key combinations.43*44* This driver lets boards define what keycodes they wish to report for45* which scancodes, as part of the "struct twl4030_keypad_data" used in46* the probe() routine.47*48* See the TPS65950 documentation; that's the general availability49* version of the TWL5030 second generation part.50*/51#define TWL4030_MAX_ROWS 8 /* TWL4030 hard limit */52#define TWL4030_MAX_COLS 853/*54* Note that we add space for an extra column so that we can handle55* row lines connected to the gnd (see twl4030_col_xlate()).56*/57#define TWL4030_ROW_SHIFT 458#define TWL4030_KEYMAP_SIZE (TWL4030_MAX_ROWS << TWL4030_ROW_SHIFT)5960struct twl4030_keypad {61unsigned short keymap[TWL4030_KEYMAP_SIZE];62u16 kp_state[TWL4030_MAX_ROWS];63unsigned n_rows;64unsigned n_cols;65unsigned irq;6667struct device *dbg_dev;68struct input_dev *input;69};7071/*----------------------------------------------------------------------*/7273/* arbitrary prescaler value 0..7 */74#define PTV_PRESCALER 47576/* Register Offsets */77#define KEYP_CTRL 0x0078#define KEYP_DEB 0x0179#define KEYP_LONG_KEY 0x0280#define KEYP_LK_PTV 0x0381#define KEYP_TIMEOUT_L 0x0482#define KEYP_TIMEOUT_H 0x0583#define KEYP_KBC 0x0684#define KEYP_KBR 0x0785#define KEYP_SMS 0x0886#define KEYP_FULL_CODE_7_0 0x09 /* row 0 column status */87#define KEYP_FULL_CODE_15_8 0x0a /* ... row 1 ... */88#define KEYP_FULL_CODE_23_16 0x0b89#define KEYP_FULL_CODE_31_24 0x0c90#define KEYP_FULL_CODE_39_32 0x0d91#define KEYP_FULL_CODE_47_40 0x0e92#define KEYP_FULL_CODE_55_48 0x0f93#define KEYP_FULL_CODE_63_56 0x1094#define KEYP_ISR1 0x1195#define KEYP_IMR1 0x1296#define KEYP_ISR2 0x1397#define KEYP_IMR2 0x1498#define KEYP_SIR 0x1599#define KEYP_EDR 0x16 /* edge triggers */100#define KEYP_SIH_CTRL 0x17101102/* KEYP_CTRL_REG Fields */103#define KEYP_CTRL_SOFT_NRST BIT(0)104#define KEYP_CTRL_SOFTMODEN BIT(1)105#define KEYP_CTRL_LK_EN BIT(2)106#define KEYP_CTRL_TOE_EN BIT(3)107#define KEYP_CTRL_TOLE_EN BIT(4)108#define KEYP_CTRL_RP_EN BIT(5)109#define KEYP_CTRL_KBD_ON BIT(6)110111/* KEYP_DEB, KEYP_LONG_KEY, KEYP_TIMEOUT_x*/112#define KEYP_PERIOD_US(t, prescale) ((t) / (31 << (prescale + 1)) - 1)113114/* KEYP_LK_PTV_REG Fields */115#define KEYP_LK_PTV_PTV_SHIFT 5116117/* KEYP_{IMR,ISR,SIR} Fields */118#define KEYP_IMR1_MIS BIT(3)119#define KEYP_IMR1_TO BIT(2)120#define KEYP_IMR1_LK BIT(1)121#define KEYP_IMR1_KP BIT(0)122123/* KEYP_EDR Fields */124#define KEYP_EDR_KP_FALLING 0x01125#define KEYP_EDR_KP_RISING 0x02126#define KEYP_EDR_KP_BOTH 0x03127#define KEYP_EDR_LK_FALLING 0x04128#define KEYP_EDR_LK_RISING 0x08129#define KEYP_EDR_TO_FALLING 0x10130#define KEYP_EDR_TO_RISING 0x20131#define KEYP_EDR_MIS_FALLING 0x40132#define KEYP_EDR_MIS_RISING 0x80133134135/*----------------------------------------------------------------------*/136137static int twl4030_kpread(struct twl4030_keypad *kp,138u8 *data, u32 reg, u8 num_bytes)139{140int ret = twl_i2c_read(TWL4030_MODULE_KEYPAD, data, reg, num_bytes);141142if (ret < 0)143dev_warn(kp->dbg_dev,144"Couldn't read TWL4030: %X - ret %d[%x]\n",145reg, ret, ret);146147return ret;148}149150static int twl4030_kpwrite_u8(struct twl4030_keypad *kp, u8 data, u32 reg)151{152int ret = twl_i2c_write_u8(TWL4030_MODULE_KEYPAD, data, reg);153154if (ret < 0)155dev_warn(kp->dbg_dev,156"Could not write TWL4030: %X - ret %d[%x]\n",157reg, ret, ret);158159return ret;160}161162static inline u16 twl4030_col_xlate(struct twl4030_keypad *kp, u8 col)163{164/* If all bits in a row are active for all coloumns then165* we have that row line connected to gnd. Mark this166* key on as if it was on matrix position n_cols (ie167* one higher than the size of the matrix).168*/169if (col == 0xFF)170return 1 << kp->n_cols;171else172return col & ((1 << kp->n_cols) - 1);173}174175static int twl4030_read_kp_matrix_state(struct twl4030_keypad *kp, u16 *state)176{177u8 new_state[TWL4030_MAX_ROWS];178int row;179int ret = twl4030_kpread(kp, new_state,180KEYP_FULL_CODE_7_0, kp->n_rows);181if (ret >= 0)182for (row = 0; row < kp->n_rows; row++)183state[row] = twl4030_col_xlate(kp, new_state[row]);184185return ret;186}187188static bool twl4030_is_in_ghost_state(struct twl4030_keypad *kp, u16 *key_state)189{190int i;191u16 check = 0;192193for (i = 0; i < kp->n_rows; i++) {194u16 col = key_state[i];195196if ((col & check) && hweight16(col) > 1)197return true;198199check |= col;200}201202return false;203}204205static void twl4030_kp_scan(struct twl4030_keypad *kp, bool release_all)206{207struct input_dev *input = kp->input;208u16 new_state[TWL4030_MAX_ROWS];209int col, row;210211if (release_all)212memset(new_state, 0, sizeof(new_state));213else {214/* check for any changes */215int ret = twl4030_read_kp_matrix_state(kp, new_state);216217if (ret < 0) /* panic ... */218return;219220if (twl4030_is_in_ghost_state(kp, new_state))221return;222}223224/* check for changes and print those */225for (row = 0; row < kp->n_rows; row++) {226int changed = new_state[row] ^ kp->kp_state[row];227228if (!changed)229continue;230231/* Extra column handles "all gnd" rows */232for (col = 0; col < kp->n_cols + 1; col++) {233int code;234235if (!(changed & (1 << col)))236continue;237238dev_dbg(kp->dbg_dev, "key [%d:%d] %s\n", row, col,239(new_state[row] & (1 << col)) ?240"press" : "release");241242code = MATRIX_SCAN_CODE(row, col, TWL4030_ROW_SHIFT);243input_event(input, EV_MSC, MSC_SCAN, code);244input_report_key(input, kp->keymap[code],245new_state[row] & (1 << col));246}247kp->kp_state[row] = new_state[row];248}249input_sync(input);250}251252/*253* Keypad interrupt handler254*/255static irqreturn_t do_kp_irq(int irq, void *_kp)256{257struct twl4030_keypad *kp = _kp;258u8 reg;259int ret;260261/* Read & Clear TWL4030 pending interrupt */262ret = twl4030_kpread(kp, ®, KEYP_ISR1, 1);263264/* Release all keys if I2C has gone bad or265* the KEYP has gone to idle state */266if (ret >= 0 && (reg & KEYP_IMR1_KP))267twl4030_kp_scan(kp, false);268else269twl4030_kp_scan(kp, true);270271return IRQ_HANDLED;272}273274static int __devinit twl4030_kp_program(struct twl4030_keypad *kp)275{276u8 reg;277int i;278279/* Enable controller, with hardware decoding but not autorepeat */280reg = KEYP_CTRL_SOFT_NRST | KEYP_CTRL_SOFTMODEN281| KEYP_CTRL_TOE_EN | KEYP_CTRL_KBD_ON;282if (twl4030_kpwrite_u8(kp, reg, KEYP_CTRL) < 0)283return -EIO;284285/* NOTE: we could use sih_setup() here to package keypad286* event sources as four different IRQs ... but we don't.287*/288289/* Enable TO rising and KP rising and falling edge detection */290reg = KEYP_EDR_KP_BOTH | KEYP_EDR_TO_RISING;291if (twl4030_kpwrite_u8(kp, reg, KEYP_EDR) < 0)292return -EIO;293294/* Set PTV prescaler Field */295reg = (PTV_PRESCALER << KEYP_LK_PTV_PTV_SHIFT);296if (twl4030_kpwrite_u8(kp, reg, KEYP_LK_PTV) < 0)297return -EIO;298299/* Set key debounce time to 20 ms */300i = KEYP_PERIOD_US(20000, PTV_PRESCALER);301if (twl4030_kpwrite_u8(kp, i, KEYP_DEB) < 0)302return -EIO;303304/* Set timeout period to 100 ms */305i = KEYP_PERIOD_US(200000, PTV_PRESCALER);306if (twl4030_kpwrite_u8(kp, (i & 0xFF), KEYP_TIMEOUT_L) < 0)307return -EIO;308309if (twl4030_kpwrite_u8(kp, (i >> 8), KEYP_TIMEOUT_H) < 0)310return -EIO;311312/*313* Enable Clear-on-Read; disable remembering events that fire314* after the IRQ but before our handler acks (reads) them,315*/316reg = TWL4030_SIH_CTRL_COR_MASK | TWL4030_SIH_CTRL_PENDDIS_MASK;317if (twl4030_kpwrite_u8(kp, reg, KEYP_SIH_CTRL) < 0)318return -EIO;319320/* initialize key state; irqs update it from here on */321if (twl4030_read_kp_matrix_state(kp, kp->kp_state) < 0)322return -EIO;323324return 0;325}326327/*328* Registers keypad device with input subsystem329* and configures TWL4030 keypad registers330*/331static int __devinit twl4030_kp_probe(struct platform_device *pdev)332{333struct twl4030_keypad_data *pdata = pdev->dev.platform_data;334const struct matrix_keymap_data *keymap_data;335struct twl4030_keypad *kp;336struct input_dev *input;337u8 reg;338int error;339340if (!pdata || !pdata->rows || !pdata->cols || !pdata->keymap_data ||341pdata->rows > TWL4030_MAX_ROWS || pdata->cols > TWL4030_MAX_COLS) {342dev_err(&pdev->dev, "Invalid platform_data\n");343return -EINVAL;344}345346keymap_data = pdata->keymap_data;347348kp = kzalloc(sizeof(*kp), GFP_KERNEL);349input = input_allocate_device();350if (!kp || !input) {351error = -ENOMEM;352goto err1;353}354355/* Get the debug Device */356kp->dbg_dev = &pdev->dev;357kp->input = input;358359kp->n_rows = pdata->rows;360kp->n_cols = pdata->cols;361kp->irq = platform_get_irq(pdev, 0);362363/* setup input device */364__set_bit(EV_KEY, input->evbit);365366/* Enable auto repeat feature of Linux input subsystem */367if (pdata->rep)368__set_bit(EV_REP, input->evbit);369370input_set_capability(input, EV_MSC, MSC_SCAN);371372input->name = "TWL4030 Keypad";373input->phys = "twl4030_keypad/input0";374input->dev.parent = &pdev->dev;375376input->id.bustype = BUS_HOST;377input->id.vendor = 0x0001;378input->id.product = 0x0001;379input->id.version = 0x0003;380381input->keycode = kp->keymap;382input->keycodesize = sizeof(kp->keymap[0]);383input->keycodemax = ARRAY_SIZE(kp->keymap);384385matrix_keypad_build_keymap(keymap_data, TWL4030_ROW_SHIFT,386input->keycode, input->keybit);387388error = input_register_device(input);389if (error) {390dev_err(kp->dbg_dev,391"Unable to register twl4030 keypad device\n");392goto err1;393}394395error = twl4030_kp_program(kp);396if (error)397goto err2;398399/*400* This ISR will always execute in kernel thread context because of401* the need to access the TWL4030 over the I2C bus.402*403* NOTE: we assume this host is wired to TWL4040 INT1, not INT2 ...404*/405error = request_threaded_irq(kp->irq, NULL, do_kp_irq,4060, pdev->name, kp);407if (error) {408dev_info(kp->dbg_dev, "request_irq failed for irq no=%d\n",409kp->irq);410goto err2;411}412413/* Enable KP and TO interrupts now. */414reg = (u8) ~(KEYP_IMR1_KP | KEYP_IMR1_TO);415if (twl4030_kpwrite_u8(kp, reg, KEYP_IMR1)) {416error = -EIO;417goto err3;418}419420platform_set_drvdata(pdev, kp);421return 0;422423err3:424/* mask all events - we don't care about the result */425(void) twl4030_kpwrite_u8(kp, 0xff, KEYP_IMR1);426free_irq(kp->irq, NULL);427err2:428input_unregister_device(input);429input = NULL;430err1:431input_free_device(input);432kfree(kp);433return error;434}435436static int __devexit twl4030_kp_remove(struct platform_device *pdev)437{438struct twl4030_keypad *kp = platform_get_drvdata(pdev);439440free_irq(kp->irq, kp);441input_unregister_device(kp->input);442platform_set_drvdata(pdev, NULL);443kfree(kp);444445return 0;446}447448/*449* NOTE: twl4030 are multi-function devices connected via I2C.450* So this device is a child of an I2C parent, thus it needs to451* support unplug/replug (which most platform devices don't).452*/453454static struct platform_driver twl4030_kp_driver = {455.probe = twl4030_kp_probe,456.remove = __devexit_p(twl4030_kp_remove),457.driver = {458.name = "twl4030_keypad",459.owner = THIS_MODULE,460},461};462463static int __init twl4030_kp_init(void)464{465return platform_driver_register(&twl4030_kp_driver);466}467module_init(twl4030_kp_init);468469static void __exit twl4030_kp_exit(void)470{471platform_driver_unregister(&twl4030_kp_driver);472}473module_exit(twl4030_kp_exit);474475MODULE_AUTHOR("Texas Instruments");476MODULE_DESCRIPTION("TWL4030 Keypad Driver");477MODULE_LICENSE("GPL");478MODULE_ALIAS("platform:twl4030_keypad");479480481482