tca6416-keypad.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*
  2. * Driver for keys on TCA6416 I2C IO expander
  3. *
  4. * Copyright (C) 2010 Texas Instruments
  5. *
  6. * Author : Sriramakrishnan.A.G. <srk@ti.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #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>
  23. #define TCA6416_INPUT 0
  24. #define TCA6416_OUTPUT 1
  25. #define TCA6416_INVERT 2
  26. #define TCA6416_DIRECTION 3
  27. static const struct i2c_device_id tca6416_id[] = {
  28. { "tca6416-keys", 16, },
  29. { }
  30. };
  31. MODULE_DEVICE_TABLE(i2c, tca6416_id);
  32. struct tca6416_drv_data {
  33. struct input_dev *input;
  34. struct tca6416_button data[0];
  35. };
  36. struct tca6416_keypad_chip {
  37. uint16_t reg_output;
  38. uint16_t reg_direction;
  39. uint16_t reg_input;
  40. struct i2c_client *client;
  41. struct input_dev *input;
  42. struct delayed_work dwork;
  43. u16 pinmask;
  44. int irqnum;
  45. bool use_polling;
  46. struct tca6416_button buttons[0];
  47. };
  48. static int tca6416_write_reg(struct tca6416_keypad_chip *chip, int reg, u16 val)
  49. {
  50. int error;
  51. error = i2c_smbus_write_word_data(chip->client, reg << 1, val);
  52. if (error < 0) {
  53. dev_err(&chip->client->dev,
  54. "%s failed, reg: %d, val: %d, error: %d\n",
  55. __func__, reg, val, error);
  56. return error;
  57. }
  58. return 0;
  59. }
  60. static int tca6416_read_reg(struct tca6416_keypad_chip *chip, int reg, u16 *val)
  61. {
  62. int retval;
  63. retval = i2c_smbus_read_word_data(chip->client, reg << 1);
  64. if (retval < 0) {
  65. dev_err(&chip->client->dev, "%s failed, reg: %d, error: %d\n",
  66. __func__, reg, retval);
  67. return retval;
  68. }
  69. *val = (u16)retval;
  70. return 0;
  71. }
  72. static void tca6416_keys_scan(struct tca6416_keypad_chip *chip)
  73. {
  74. struct input_dev *input = chip->input;
  75. u16 reg_val, val;
  76. int error, i, pin_index;
  77. error = tca6416_read_reg(chip, TCA6416_INPUT, &reg_val);
  78. if (error)
  79. return;
  80. reg_val &= chip->pinmask;
  81. /* Figure out which lines have changed */
  82. val = reg_val ^ chip->reg_input;
  83. chip->reg_input = reg_val;
  84. for (i = 0, pin_index = 0; i < 16; i++) {
  85. if (val & (1 << i)) {
  86. struct tca6416_button *button = &chip->buttons[pin_index];
  87. unsigned int type = button->type ?: EV_KEY;
  88. int state = ((reg_val & (1 << i)) ? 1 : 0)
  89. ^ button->active_low;
  90. input_event(input, type, button->code, !!state);
  91. input_sync(input);
  92. }
  93. if (chip->pinmask & (1 << i))
  94. pin_index++;
  95. }
  96. }
  97. /*
  98. * This is threaded IRQ handler and this can (and will) sleep.
  99. */
  100. static irqreturn_t tca6416_keys_isr(int irq, void *dev_id)
  101. {
  102. struct tca6416_keypad_chip *chip = dev_id;
  103. tca6416_keys_scan(chip);
  104. return IRQ_HANDLED;
  105. }
  106. static void tca6416_keys_work_func(struct work_struct *work)
  107. {
  108. struct tca6416_keypad_chip *chip =
  109. container_of(work, struct tca6416_keypad_chip, dwork.work);
  110. tca6416_keys_scan(chip);
  111. schedule_delayed_work(&chip->dwork, msecs_to_jiffies(100));
  112. }
  113. static int tca6416_keys_open(struct input_dev *dev)
  114. {
  115. struct tca6416_keypad_chip *chip = input_get_drvdata(dev);
  116. /* Get initial device state in case it has switches */
  117. tca6416_keys_scan(chip);
  118. if (chip->use_polling)
  119. schedule_delayed_work(&chip->dwork, msecs_to_jiffies(100));
  120. else
  121. enable_irq(chip->irqnum);
  122. return 0;
  123. }
  124. static void tca6416_keys_close(struct input_dev *dev)
  125. {
  126. struct tca6416_keypad_chip *chip = input_get_drvdata(dev);
  127. if (chip->use_polling)
  128. cancel_delayed_work_sync(&chip->dwork);
  129. else
  130. disable_irq(chip->irqnum);
  131. }
  132. static int __devinit tca6416_setup_registers(struct tca6416_keypad_chip *chip)
  133. {
  134. int error;
  135. error = tca6416_read_reg(chip, TCA6416_OUTPUT, &chip->reg_output);
  136. if (error)
  137. return error;
  138. error = tca6416_read_reg(chip, TCA6416_DIRECTION, &chip->reg_direction);
  139. if (error)
  140. return error;
  141. /* ensure that keypad pins are set to input */
  142. error = tca6416_write_reg(chip, TCA6416_DIRECTION,
  143. chip->reg_direction | chip->pinmask);
  144. if (error)
  145. return error;
  146. error = tca6416_read_reg(chip, TCA6416_DIRECTION, &chip->reg_direction);
  147. if (error)
  148. return error;
  149. error = tca6416_read_reg(chip, TCA6416_INPUT, &chip->reg_input);
  150. if (error)
  151. return error;
  152. chip->reg_input &= chip->pinmask;
  153. return 0;
  154. }
  155. static int __devinit tca6416_keypad_probe(struct i2c_client *client,
  156. const struct i2c_device_id *id)
  157. {
  158. struct tca6416_keys_platform_data *pdata;
  159. struct tca6416_keypad_chip *chip;
  160. struct input_dev *input;
  161. int error;
  162. int i;
  163. /* Check functionality */
  164. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE)) {
  165. dev_err(&client->dev, "%s adapter not supported\n",
  166. dev_driver_string(&client->adapter->dev));
  167. return -ENODEV;
  168. }
  169. pdata = client->dev.platform_data;
  170. if (!pdata) {
  171. dev_dbg(&client->dev, "no platform data\n");
  172. return -EINVAL;
  173. }
  174. chip = kzalloc(sizeof(struct tca6416_keypad_chip) +
  175. pdata->nbuttons * sizeof(struct tca6416_button),
  176. GFP_KERNEL);
  177. input = input_allocate_device();
  178. if (!chip || !input) {
  179. error = -ENOMEM;
  180. goto fail1;
  181. }
  182. chip->client = client;
  183. chip->input = input;
  184. chip->pinmask = pdata->pinmask;
  185. chip->use_polling = pdata->use_polling;
  186. INIT_DELAYED_WORK(&chip->dwork, tca6416_keys_work_func);
  187. input->phys = "tca6416-keys/input0";
  188. input->name = client->name;
  189. input->dev.parent = &client->dev;
  190. input->open = tca6416_keys_open;
  191. input->close = tca6416_keys_close;
  192. input->id.bustype = BUS_HOST;
  193. input->id.vendor = 0x0001;
  194. input->id.product = 0x0001;
  195. input->id.version = 0x0100;
  196. /* Enable auto repeat feature of Linux input subsystem */
  197. if (pdata->rep)
  198. __set_bit(EV_REP, input->evbit);
  199. for (i = 0; i < pdata->nbuttons; i++) {
  200. unsigned int type;
  201. chip->buttons[i] = pdata->buttons[i];
  202. type = (pdata->buttons[i].type) ?: EV_KEY;
  203. input_set_capability(input, type, pdata->buttons[i].code);
  204. }
  205. input_set_drvdata(input, chip);
  206. /*
  207. * Initialize cached registers from their original values.
  208. * we can't share this chip with another i2c master.
  209. */
  210. error = tca6416_setup_registers(chip);
  211. if (error)
  212. goto fail1;
  213. if (!chip->use_polling) {
  214. if (pdata->irq_is_gpio)
  215. chip->irqnum = gpio_to_irq(client->irq);
  216. else
  217. chip->irqnum = client->irq;
  218. error = request_threaded_irq(chip->irqnum, NULL,
  219. tca6416_keys_isr,
  220. IRQF_TRIGGER_FALLING,
  221. "tca6416-keypad", chip);
  222. if (error) {
  223. dev_dbg(&client->dev,
  224. "Unable to claim irq %d; error %d\n",
  225. chip->irqnum, error);
  226. goto fail1;
  227. }
  228. disable_irq(chip->irqnum);
  229. }
  230. error = input_register_device(input);
  231. if (error) {
  232. dev_dbg(&client->dev,
  233. "Unable to register input device, error: %d\n", error);
  234. goto fail2;
  235. }
  236. i2c_set_clientdata(client, chip);
  237. return 0;
  238. fail2:
  239. if (!chip->use_polling) {
  240. free_irq(chip->irqnum, chip);
  241. enable_irq(chip->irqnum);
  242. }
  243. fail1:
  244. input_free_device(input);
  245. kfree(chip);
  246. return error;
  247. }
  248. static int __devexit tca6416_keypad_remove(struct i2c_client *client)
  249. {
  250. struct tca6416_keypad_chip *chip = i2c_get_clientdata(client);
  251. if (!chip->use_polling) {
  252. free_irq(chip->irqnum, chip);
  253. enable_irq(chip->irqnum);
  254. }
  255. input_unregister_device(chip->input);
  256. kfree(chip);
  257. return 0;
  258. }
  259. static struct i2c_driver tca6416_keypad_driver = {
  260. .driver = {
  261. .name = "tca6416-keypad",
  262. },
  263. .probe = tca6416_keypad_probe,
  264. .remove = __devexit_p(tca6416_keypad_remove),
  265. .id_table = tca6416_id,
  266. };
  267. static int __init tca6416_keypad_init(void)
  268. {
  269. return i2c_add_driver(&tca6416_keypad_driver);
  270. }
  271. subsys_initcall(tca6416_keypad_init);
  272. static void __exit tca6416_keypad_exit(void)
  273. {
  274. i2c_del_driver(&tca6416_keypad_driver);
  275. }
  276. module_exit(tca6416_keypad_exit);
  277. MODULE_AUTHOR("Sriramakrishnan <srk@ti.com>");
  278. MODULE_DESCRIPTION("Keypad driver over tca6146 IO expander");
  279. MODULE_LICENSE("GPL");