Path: blob/master/drivers/input/keyboard/tca6416-keypad.c
15111 views
/*1* Driver for keys on TCA6416 I2C IO expander2*3* Copyright (C) 2010 Texas Instruments4*5* Author : Sriramakrishnan.A.G. <[email protected]>6*7* This program is free software; you can redistribute it and/or modify8* it under the terms of the GNU General Public License version 2 as9* published by the Free Software Foundation.10*/1112#include <linux/types.h>13#include <linux/module.h>14#include <linux/init.h>15#include <linux/delay.h>16#include <linux/slab.h>17#include <linux/interrupt.h>18#include <linux/workqueue.h>19#include <linux/gpio.h>20#include <linux/i2c.h>21#include <linux/input.h>22#include <linux/tca6416_keypad.h>2324#define TCA6416_INPUT 025#define TCA6416_OUTPUT 126#define TCA6416_INVERT 227#define TCA6416_DIRECTION 32829static const struct i2c_device_id tca6416_id[] = {30{ "tca6416-keys", 16, },31{ "tca6408-keys", 8, },32{ }33};34MODULE_DEVICE_TABLE(i2c, tca6416_id);3536struct tca6416_drv_data {37struct input_dev *input;38struct tca6416_button data[0];39};4041struct tca6416_keypad_chip {42uint16_t reg_output;43uint16_t reg_direction;44uint16_t reg_input;4546struct i2c_client *client;47struct input_dev *input;48struct delayed_work dwork;49int io_size;50int irqnum;51u16 pinmask;52bool use_polling;53struct tca6416_button buttons[0];54};5556static int tca6416_write_reg(struct tca6416_keypad_chip *chip, int reg, u16 val)57{58int error;5960error = chip->io_size > 8 ?61i2c_smbus_write_word_data(chip->client, reg << 1, val) :62i2c_smbus_write_byte_data(chip->client, reg, val);63if (error < 0) {64dev_err(&chip->client->dev,65"%s failed, reg: %d, val: %d, error: %d\n",66__func__, reg, val, error);67return error;68}6970return 0;71}7273static int tca6416_read_reg(struct tca6416_keypad_chip *chip, int reg, u16 *val)74{75int retval;7677retval = chip->io_size > 8 ?78i2c_smbus_read_word_data(chip->client, reg << 1) :79i2c_smbus_read_byte_data(chip->client, reg);80if (retval < 0) {81dev_err(&chip->client->dev, "%s failed, reg: %d, error: %d\n",82__func__, reg, retval);83return retval;84}8586*val = (u16)retval;87return 0;88}8990static void tca6416_keys_scan(struct tca6416_keypad_chip *chip)91{92struct input_dev *input = chip->input;93u16 reg_val, val;94int error, i, pin_index;9596error = tca6416_read_reg(chip, TCA6416_INPUT, ®_val);97if (error)98return;99100reg_val &= chip->pinmask;101102/* Figure out which lines have changed */103val = reg_val ^ chip->reg_input;104chip->reg_input = reg_val;105106for (i = 0, pin_index = 0; i < 16; i++) {107if (val & (1 << i)) {108struct tca6416_button *button = &chip->buttons[pin_index];109unsigned int type = button->type ?: EV_KEY;110int state = ((reg_val & (1 << i)) ? 1 : 0)111^ button->active_low;112113input_event(input, type, button->code, !!state);114input_sync(input);115}116117if (chip->pinmask & (1 << i))118pin_index++;119}120}121122/*123* This is threaded IRQ handler and this can (and will) sleep.124*/125static irqreturn_t tca6416_keys_isr(int irq, void *dev_id)126{127struct tca6416_keypad_chip *chip = dev_id;128129tca6416_keys_scan(chip);130131return IRQ_HANDLED;132}133134static void tca6416_keys_work_func(struct work_struct *work)135{136struct tca6416_keypad_chip *chip =137container_of(work, struct tca6416_keypad_chip, dwork.work);138139tca6416_keys_scan(chip);140schedule_delayed_work(&chip->dwork, msecs_to_jiffies(100));141}142143static int tca6416_keys_open(struct input_dev *dev)144{145struct tca6416_keypad_chip *chip = input_get_drvdata(dev);146147/* Get initial device state in case it has switches */148tca6416_keys_scan(chip);149150if (chip->use_polling)151schedule_delayed_work(&chip->dwork, msecs_to_jiffies(100));152else153enable_irq(chip->irqnum);154155return 0;156}157158static void tca6416_keys_close(struct input_dev *dev)159{160struct tca6416_keypad_chip *chip = input_get_drvdata(dev);161162if (chip->use_polling)163cancel_delayed_work_sync(&chip->dwork);164else165disable_irq(chip->irqnum);166}167168static int __devinit tca6416_setup_registers(struct tca6416_keypad_chip *chip)169{170int error;171172error = tca6416_read_reg(chip, TCA6416_OUTPUT, &chip->reg_output);173if (error)174return error;175176error = tca6416_read_reg(chip, TCA6416_DIRECTION, &chip->reg_direction);177if (error)178return error;179180/* ensure that keypad pins are set to input */181error = tca6416_write_reg(chip, TCA6416_DIRECTION,182chip->reg_direction | chip->pinmask);183if (error)184return error;185186error = tca6416_read_reg(chip, TCA6416_DIRECTION, &chip->reg_direction);187if (error)188return error;189190error = tca6416_read_reg(chip, TCA6416_INPUT, &chip->reg_input);191if (error)192return error;193194chip->reg_input &= chip->pinmask;195196return 0;197}198199static int __devinit tca6416_keypad_probe(struct i2c_client *client,200const struct i2c_device_id *id)201{202struct tca6416_keys_platform_data *pdata;203struct tca6416_keypad_chip *chip;204struct input_dev *input;205int error;206int i;207208/* Check functionality */209if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE)) {210dev_err(&client->dev, "%s adapter not supported\n",211dev_driver_string(&client->adapter->dev));212return -ENODEV;213}214215pdata = client->dev.platform_data;216if (!pdata) {217dev_dbg(&client->dev, "no platform data\n");218return -EINVAL;219}220221chip = kzalloc(sizeof(struct tca6416_keypad_chip) +222pdata->nbuttons * sizeof(struct tca6416_button),223GFP_KERNEL);224input = input_allocate_device();225if (!chip || !input) {226error = -ENOMEM;227goto fail1;228}229230chip->client = client;231chip->input = input;232chip->io_size = id->driver_data;233chip->pinmask = pdata->pinmask;234chip->use_polling = pdata->use_polling;235236INIT_DELAYED_WORK(&chip->dwork, tca6416_keys_work_func);237238input->phys = "tca6416-keys/input0";239input->name = client->name;240input->dev.parent = &client->dev;241242input->open = tca6416_keys_open;243input->close = tca6416_keys_close;244245input->id.bustype = BUS_HOST;246input->id.vendor = 0x0001;247input->id.product = 0x0001;248input->id.version = 0x0100;249250/* Enable auto repeat feature of Linux input subsystem */251if (pdata->rep)252__set_bit(EV_REP, input->evbit);253254for (i = 0; i < pdata->nbuttons; i++) {255unsigned int type;256257chip->buttons[i] = pdata->buttons[i];258type = (pdata->buttons[i].type) ?: EV_KEY;259input_set_capability(input, type, pdata->buttons[i].code);260}261262input_set_drvdata(input, chip);263264/*265* Initialize cached registers from their original values.266* we can't share this chip with another i2c master.267*/268error = tca6416_setup_registers(chip);269if (error)270goto fail1;271272if (!chip->use_polling) {273if (pdata->irq_is_gpio)274chip->irqnum = gpio_to_irq(client->irq);275else276chip->irqnum = client->irq;277278error = request_threaded_irq(chip->irqnum, NULL,279tca6416_keys_isr,280IRQF_TRIGGER_FALLING,281"tca6416-keypad", chip);282if (error) {283dev_dbg(&client->dev,284"Unable to claim irq %d; error %d\n",285chip->irqnum, error);286goto fail1;287}288disable_irq(chip->irqnum);289}290291error = input_register_device(input);292if (error) {293dev_dbg(&client->dev,294"Unable to register input device, error: %d\n", error);295goto fail2;296}297298i2c_set_clientdata(client, chip);299device_init_wakeup(&client->dev, 1);300301return 0;302303fail2:304if (!chip->use_polling) {305free_irq(chip->irqnum, chip);306enable_irq(chip->irqnum);307}308fail1:309input_free_device(input);310kfree(chip);311return error;312}313314static int __devexit tca6416_keypad_remove(struct i2c_client *client)315{316struct tca6416_keypad_chip *chip = i2c_get_clientdata(client);317318if (!chip->use_polling) {319free_irq(chip->irqnum, chip);320enable_irq(chip->irqnum);321}322323input_unregister_device(chip->input);324kfree(chip);325326return 0;327}328329#ifdef CONFIG_PM_SLEEP330static int tca6416_keypad_suspend(struct device *dev)331{332struct i2c_client *client = to_i2c_client(dev);333struct tca6416_keypad_chip *chip = i2c_get_clientdata(client);334335if (device_may_wakeup(dev))336enable_irq_wake(chip->irqnum);337338return 0;339}340341static int tca6416_keypad_resume(struct device *dev)342{343struct i2c_client *client = to_i2c_client(dev);344struct tca6416_keypad_chip *chip = i2c_get_clientdata(client);345346if (device_may_wakeup(dev))347disable_irq_wake(chip->irqnum);348349return 0;350}351#endif352353static SIMPLE_DEV_PM_OPS(tca6416_keypad_dev_pm_ops,354tca6416_keypad_suspend, tca6416_keypad_resume);355356static struct i2c_driver tca6416_keypad_driver = {357.driver = {358.name = "tca6416-keypad",359.pm = &tca6416_keypad_dev_pm_ops,360},361.probe = tca6416_keypad_probe,362.remove = __devexit_p(tca6416_keypad_remove),363.id_table = tca6416_id,364};365366static int __init tca6416_keypad_init(void)367{368return i2c_add_driver(&tca6416_keypad_driver);369}370371subsys_initcall(tca6416_keypad_init);372373static void __exit tca6416_keypad_exit(void)374{375i2c_del_driver(&tca6416_keypad_driver);376}377module_exit(tca6416_keypad_exit);378379MODULE_AUTHOR("Sriramakrishnan <[email protected]>");380MODULE_DESCRIPTION("Keypad driver over tca6146 IO expander");381MODULE_LICENSE("GPL");382383384