0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include <linux/types.h>
0011 #include <linux/module.h>
0012 #include <linux/init.h>
0013 #include <linux/delay.h>
0014 #include <linux/slab.h>
0015 #include <linux/interrupt.h>
0016 #include <linux/workqueue.h>
0017 #include <linux/gpio.h>
0018 #include <linux/i2c.h>
0019 #include <linux/input.h>
0020 #include <linux/tca6416_keypad.h>
0021
0022 #define TCA6416_INPUT 0
0023 #define TCA6416_OUTPUT 1
0024 #define TCA6416_INVERT 2
0025 #define TCA6416_DIRECTION 3
0026
0027 static const struct i2c_device_id tca6416_id[] = {
0028 { "tca6416-keys", 16, },
0029 { "tca6408-keys", 8, },
0030 { }
0031 };
0032 MODULE_DEVICE_TABLE(i2c, tca6416_id);
0033
0034 struct tca6416_drv_data {
0035 struct input_dev *input;
0036 struct tca6416_button data[];
0037 };
0038
0039 struct tca6416_keypad_chip {
0040 uint16_t reg_output;
0041 uint16_t reg_direction;
0042 uint16_t reg_input;
0043
0044 struct i2c_client *client;
0045 struct input_dev *input;
0046 struct delayed_work dwork;
0047 int io_size;
0048 int irqnum;
0049 u16 pinmask;
0050 bool use_polling;
0051 struct tca6416_button buttons[];
0052 };
0053
0054 static int tca6416_write_reg(struct tca6416_keypad_chip *chip, int reg, u16 val)
0055 {
0056 int error;
0057
0058 error = chip->io_size > 8 ?
0059 i2c_smbus_write_word_data(chip->client, reg << 1, val) :
0060 i2c_smbus_write_byte_data(chip->client, reg, val);
0061 if (error < 0) {
0062 dev_err(&chip->client->dev,
0063 "%s failed, reg: %d, val: %d, error: %d\n",
0064 __func__, reg, val, error);
0065 return error;
0066 }
0067
0068 return 0;
0069 }
0070
0071 static int tca6416_read_reg(struct tca6416_keypad_chip *chip, int reg, u16 *val)
0072 {
0073 int retval;
0074
0075 retval = chip->io_size > 8 ?
0076 i2c_smbus_read_word_data(chip->client, reg << 1) :
0077 i2c_smbus_read_byte_data(chip->client, reg);
0078 if (retval < 0) {
0079 dev_err(&chip->client->dev, "%s failed, reg: %d, error: %d\n",
0080 __func__, reg, retval);
0081 return retval;
0082 }
0083
0084 *val = (u16)retval;
0085 return 0;
0086 }
0087
0088 static void tca6416_keys_scan(struct tca6416_keypad_chip *chip)
0089 {
0090 struct input_dev *input = chip->input;
0091 u16 reg_val, val;
0092 int error, i, pin_index;
0093
0094 error = tca6416_read_reg(chip, TCA6416_INPUT, ®_val);
0095 if (error)
0096 return;
0097
0098 reg_val &= chip->pinmask;
0099
0100
0101 val = reg_val ^ chip->reg_input;
0102 chip->reg_input = reg_val;
0103
0104 for (i = 0, pin_index = 0; i < 16; i++) {
0105 if (val & (1 << i)) {
0106 struct tca6416_button *button = &chip->buttons[pin_index];
0107 unsigned int type = button->type ?: EV_KEY;
0108 int state = ((reg_val & (1 << i)) ? 1 : 0)
0109 ^ button->active_low;
0110
0111 input_event(input, type, button->code, !!state);
0112 input_sync(input);
0113 }
0114
0115 if (chip->pinmask & (1 << i))
0116 pin_index++;
0117 }
0118 }
0119
0120
0121
0122
0123 static irqreturn_t tca6416_keys_isr(int irq, void *dev_id)
0124 {
0125 struct tca6416_keypad_chip *chip = dev_id;
0126
0127 tca6416_keys_scan(chip);
0128
0129 return IRQ_HANDLED;
0130 }
0131
0132 static void tca6416_keys_work_func(struct work_struct *work)
0133 {
0134 struct tca6416_keypad_chip *chip =
0135 container_of(work, struct tca6416_keypad_chip, dwork.work);
0136
0137 tca6416_keys_scan(chip);
0138 schedule_delayed_work(&chip->dwork, msecs_to_jiffies(100));
0139 }
0140
0141 static int tca6416_keys_open(struct input_dev *dev)
0142 {
0143 struct tca6416_keypad_chip *chip = input_get_drvdata(dev);
0144
0145
0146 tca6416_keys_scan(chip);
0147
0148 if (chip->use_polling)
0149 schedule_delayed_work(&chip->dwork, msecs_to_jiffies(100));
0150 else
0151 enable_irq(chip->irqnum);
0152
0153 return 0;
0154 }
0155
0156 static void tca6416_keys_close(struct input_dev *dev)
0157 {
0158 struct tca6416_keypad_chip *chip = input_get_drvdata(dev);
0159
0160 if (chip->use_polling)
0161 cancel_delayed_work_sync(&chip->dwork);
0162 else
0163 disable_irq(chip->irqnum);
0164 }
0165
0166 static int tca6416_setup_registers(struct tca6416_keypad_chip *chip)
0167 {
0168 int error;
0169
0170 error = tca6416_read_reg(chip, TCA6416_OUTPUT, &chip->reg_output);
0171 if (error)
0172 return error;
0173
0174 error = tca6416_read_reg(chip, TCA6416_DIRECTION, &chip->reg_direction);
0175 if (error)
0176 return error;
0177
0178
0179 error = tca6416_write_reg(chip, TCA6416_DIRECTION,
0180 chip->reg_direction | chip->pinmask);
0181 if (error)
0182 return error;
0183
0184 error = tca6416_read_reg(chip, TCA6416_DIRECTION, &chip->reg_direction);
0185 if (error)
0186 return error;
0187
0188 error = tca6416_read_reg(chip, TCA6416_INPUT, &chip->reg_input);
0189 if (error)
0190 return error;
0191
0192 chip->reg_input &= chip->pinmask;
0193
0194 return 0;
0195 }
0196
0197 static int tca6416_keypad_probe(struct i2c_client *client,
0198 const struct i2c_device_id *id)
0199 {
0200 struct tca6416_keys_platform_data *pdata;
0201 struct tca6416_keypad_chip *chip;
0202 struct input_dev *input;
0203 int error;
0204 int i;
0205
0206
0207 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE)) {
0208 dev_err(&client->dev, "%s adapter not supported\n",
0209 dev_driver_string(&client->adapter->dev));
0210 return -ENODEV;
0211 }
0212
0213 pdata = dev_get_platdata(&client->dev);
0214 if (!pdata) {
0215 dev_dbg(&client->dev, "no platform data\n");
0216 return -EINVAL;
0217 }
0218
0219 chip = kzalloc(struct_size(chip, buttons, pdata->nbuttons), GFP_KERNEL);
0220 input = input_allocate_device();
0221 if (!chip || !input) {
0222 error = -ENOMEM;
0223 goto fail1;
0224 }
0225
0226 chip->client = client;
0227 chip->input = input;
0228 chip->io_size = id->driver_data;
0229 chip->pinmask = pdata->pinmask;
0230 chip->use_polling = pdata->use_polling;
0231
0232 INIT_DELAYED_WORK(&chip->dwork, tca6416_keys_work_func);
0233
0234 input->phys = "tca6416-keys/input0";
0235 input->name = client->name;
0236 input->dev.parent = &client->dev;
0237
0238 input->open = tca6416_keys_open;
0239 input->close = tca6416_keys_close;
0240
0241 input->id.bustype = BUS_HOST;
0242 input->id.vendor = 0x0001;
0243 input->id.product = 0x0001;
0244 input->id.version = 0x0100;
0245
0246
0247 if (pdata->rep)
0248 __set_bit(EV_REP, input->evbit);
0249
0250 for (i = 0; i < pdata->nbuttons; i++) {
0251 unsigned int type;
0252
0253 chip->buttons[i] = pdata->buttons[i];
0254 type = (pdata->buttons[i].type) ?: EV_KEY;
0255 input_set_capability(input, type, pdata->buttons[i].code);
0256 }
0257
0258 input_set_drvdata(input, chip);
0259
0260
0261
0262
0263
0264 error = tca6416_setup_registers(chip);
0265 if (error)
0266 goto fail1;
0267
0268 if (!chip->use_polling) {
0269 if (pdata->irq_is_gpio)
0270 chip->irqnum = gpio_to_irq(client->irq);
0271 else
0272 chip->irqnum = client->irq;
0273
0274 error = request_threaded_irq(chip->irqnum, NULL,
0275 tca6416_keys_isr,
0276 IRQF_TRIGGER_FALLING |
0277 IRQF_ONESHOT | IRQF_NO_AUTOEN,
0278 "tca6416-keypad", chip);
0279 if (error) {
0280 dev_dbg(&client->dev,
0281 "Unable to claim irq %d; error %d\n",
0282 chip->irqnum, error);
0283 goto fail1;
0284 }
0285 }
0286
0287 error = input_register_device(input);
0288 if (error) {
0289 dev_dbg(&client->dev,
0290 "Unable to register input device, error: %d\n", error);
0291 goto fail2;
0292 }
0293
0294 i2c_set_clientdata(client, chip);
0295 device_init_wakeup(&client->dev, 1);
0296
0297 return 0;
0298
0299 fail2:
0300 if (!chip->use_polling) {
0301 free_irq(chip->irqnum, chip);
0302 enable_irq(chip->irqnum);
0303 }
0304 fail1:
0305 input_free_device(input);
0306 kfree(chip);
0307 return error;
0308 }
0309
0310 static int tca6416_keypad_remove(struct i2c_client *client)
0311 {
0312 struct tca6416_keypad_chip *chip = i2c_get_clientdata(client);
0313
0314 if (!chip->use_polling) {
0315 free_irq(chip->irqnum, chip);
0316 enable_irq(chip->irqnum);
0317 }
0318
0319 input_unregister_device(chip->input);
0320 kfree(chip);
0321
0322 return 0;
0323 }
0324
0325 #ifdef CONFIG_PM_SLEEP
0326 static int tca6416_keypad_suspend(struct device *dev)
0327 {
0328 struct i2c_client *client = to_i2c_client(dev);
0329 struct tca6416_keypad_chip *chip = i2c_get_clientdata(client);
0330
0331 if (device_may_wakeup(dev))
0332 enable_irq_wake(chip->irqnum);
0333
0334 return 0;
0335 }
0336
0337 static int tca6416_keypad_resume(struct device *dev)
0338 {
0339 struct i2c_client *client = to_i2c_client(dev);
0340 struct tca6416_keypad_chip *chip = i2c_get_clientdata(client);
0341
0342 if (device_may_wakeup(dev))
0343 disable_irq_wake(chip->irqnum);
0344
0345 return 0;
0346 }
0347 #endif
0348
0349 static SIMPLE_DEV_PM_OPS(tca6416_keypad_dev_pm_ops,
0350 tca6416_keypad_suspend, tca6416_keypad_resume);
0351
0352 static struct i2c_driver tca6416_keypad_driver = {
0353 .driver = {
0354 .name = "tca6416-keypad",
0355 .pm = &tca6416_keypad_dev_pm_ops,
0356 },
0357 .probe = tca6416_keypad_probe,
0358 .remove = tca6416_keypad_remove,
0359 .id_table = tca6416_id,
0360 };
0361
0362 static int __init tca6416_keypad_init(void)
0363 {
0364 return i2c_add_driver(&tca6416_keypad_driver);
0365 }
0366
0367 subsys_initcall(tca6416_keypad_init);
0368
0369 static void __exit tca6416_keypad_exit(void)
0370 {
0371 i2c_del_driver(&tca6416_keypad_driver);
0372 }
0373 module_exit(tca6416_keypad_exit);
0374
0375 MODULE_AUTHOR("Sriramakrishnan <srk@ti.com>");
0376 MODULE_DESCRIPTION("Keypad driver over tca6416 IO expander");
0377 MODULE_LICENSE("GPL");