tca6416-keypad.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Driver for keys on TCA6416 I2C IO expander
  4. *
  5. * Copyright (C) 2010 Texas Instruments
  6. *
  7. * Author : Sriramakrishnan.A.G. <srk@ti.com>
  8. */
  9. #include <linux/types.h>
  10. #include <linux/module.h>
  11. #include <linux/init.h>
  12. #include <linux/delay.h>
  13. #include <linux/slab.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/workqueue.h>
  16. #include <linux/i2c.h>
  17. #include <linux/input.h>
  18. #include <linux/tca6416_keypad.h>
  19. #define TCA6416_INPUT 0
  20. #define TCA6416_OUTPUT 1
  21. #define TCA6416_INVERT 2
  22. #define TCA6416_DIRECTION 3
  23. #define TCA6416_POLL_INTERVAL 100 /* msec */
  24. static const struct i2c_device_id tca6416_id[] = {
  25. { "tca6416-keys", 16, },
  26. { "tca6408-keys", 8, },
  27. { }
  28. };
  29. MODULE_DEVICE_TABLE(i2c, tca6416_id);
  30. struct tca6416_keypad_chip {
  31. uint16_t reg_output;
  32. uint16_t reg_direction;
  33. uint16_t reg_input;
  34. struct i2c_client *client;
  35. struct input_dev *input;
  36. int io_size;
  37. u16 pinmask;
  38. bool use_polling;
  39. struct tca6416_button buttons[];
  40. };
  41. static int tca6416_write_reg(struct tca6416_keypad_chip *chip, int reg, u16 val)
  42. {
  43. int error;
  44. error = chip->io_size > 8 ?
  45. i2c_smbus_write_word_data(chip->client, reg << 1, val) :
  46. i2c_smbus_write_byte_data(chip->client, reg, val);
  47. if (error < 0) {
  48. dev_err(&chip->client->dev,
  49. "%s failed, reg: %d, val: %d, error: %d\n",
  50. __func__, reg, val, error);
  51. return error;
  52. }
  53. return 0;
  54. }
  55. static int tca6416_read_reg(struct tca6416_keypad_chip *chip, int reg, u16 *val)
  56. {
  57. int retval;
  58. retval = chip->io_size > 8 ?
  59. i2c_smbus_read_word_data(chip->client, reg << 1) :
  60. i2c_smbus_read_byte_data(chip->client, reg);
  61. if (retval < 0) {
  62. dev_err(&chip->client->dev, "%s failed, reg: %d, error: %d\n",
  63. __func__, reg, retval);
  64. return retval;
  65. }
  66. *val = (u16)retval;
  67. return 0;
  68. }
  69. static void tca6416_keys_scan(struct input_dev *input)
  70. {
  71. struct tca6416_keypad_chip *chip = input_get_drvdata(input);
  72. u16 reg_val, val;
  73. int error, i, pin_index;
  74. error = tca6416_read_reg(chip, TCA6416_INPUT, &reg_val);
  75. if (error)
  76. return;
  77. reg_val &= chip->pinmask;
  78. /* Figure out which lines have changed */
  79. val = reg_val ^ chip->reg_input;
  80. chip->reg_input = reg_val;
  81. for (i = 0, pin_index = 0; i < 16; i++) {
  82. if (val & (1 << i)) {
  83. struct tca6416_button *button = &chip->buttons[pin_index];
  84. unsigned int type = button->type ?: EV_KEY;
  85. int state = ((reg_val & (1 << i)) ? 1 : 0)
  86. ^ button->active_low;
  87. input_event(input, type, button->code, !!state);
  88. input_sync(input);
  89. }
  90. if (chip->pinmask & (1 << i))
  91. pin_index++;
  92. }
  93. }
  94. /*
  95. * This is threaded IRQ handler and this can (and will) sleep.
  96. */
  97. static irqreturn_t tca6416_keys_isr(int irq, void *dev_id)
  98. {
  99. tca6416_keys_scan(dev_id);
  100. return IRQ_HANDLED;
  101. }
  102. static int tca6416_keys_open(struct input_dev *dev)
  103. {
  104. struct tca6416_keypad_chip *chip = input_get_drvdata(dev);
  105. if (!chip->use_polling) {
  106. /* Get initial device state in case it has switches */
  107. tca6416_keys_scan(dev);
  108. enable_irq(chip->client->irq);
  109. }
  110. return 0;
  111. }
  112. static void tca6416_keys_close(struct input_dev *dev)
  113. {
  114. struct tca6416_keypad_chip *chip = input_get_drvdata(dev);
  115. if (!chip->use_polling)
  116. disable_irq(chip->client->irq);
  117. }
  118. static int tca6416_setup_registers(struct tca6416_keypad_chip *chip)
  119. {
  120. int error;
  121. error = tca6416_read_reg(chip, TCA6416_OUTPUT, &chip->reg_output);
  122. if (error)
  123. return error;
  124. error = tca6416_read_reg(chip, TCA6416_DIRECTION, &chip->reg_direction);
  125. if (error)
  126. return error;
  127. /* ensure that keypad pins are set to input */
  128. error = tca6416_write_reg(chip, TCA6416_DIRECTION,
  129. chip->reg_direction | chip->pinmask);
  130. if (error)
  131. return error;
  132. error = tca6416_read_reg(chip, TCA6416_DIRECTION, &chip->reg_direction);
  133. if (error)
  134. return error;
  135. error = tca6416_read_reg(chip, TCA6416_INPUT, &chip->reg_input);
  136. if (error)
  137. return error;
  138. chip->reg_input &= chip->pinmask;
  139. return 0;
  140. }
  141. static int tca6416_keypad_probe(struct i2c_client *client)
  142. {
  143. const struct i2c_device_id *id = i2c_client_get_device_id(client);
  144. struct tca6416_keys_platform_data *pdata;
  145. struct tca6416_keypad_chip *chip;
  146. struct input_dev *input;
  147. int error;
  148. int i;
  149. /* Check functionality */
  150. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE)) {
  151. dev_err(&client->dev, "%s adapter not supported\n",
  152. dev_driver_string(&client->adapter->dev));
  153. return -ENODEV;
  154. }
  155. pdata = dev_get_platdata(&client->dev);
  156. if (!pdata) {
  157. dev_dbg(&client->dev, "no platform data\n");
  158. return -EINVAL;
  159. }
  160. chip = devm_kzalloc(&client->dev,
  161. struct_size(chip, buttons, pdata->nbuttons),
  162. GFP_KERNEL);
  163. if (!chip)
  164. return -ENOMEM;
  165. input = devm_input_allocate_device(&client->dev);
  166. if (!input)
  167. return -ENOMEM;
  168. chip->client = client;
  169. chip->input = input;
  170. chip->io_size = id->driver_data;
  171. chip->pinmask = pdata->pinmask;
  172. chip->use_polling = pdata->use_polling;
  173. input->phys = "tca6416-keys/input0";
  174. input->name = client->name;
  175. input->open = tca6416_keys_open;
  176. input->close = tca6416_keys_close;
  177. input->id.bustype = BUS_HOST;
  178. input->id.vendor = 0x0001;
  179. input->id.product = 0x0001;
  180. input->id.version = 0x0100;
  181. /* Enable auto repeat feature of Linux input subsystem */
  182. if (pdata->rep)
  183. __set_bit(EV_REP, input->evbit);
  184. for (i = 0; i < pdata->nbuttons; i++) {
  185. unsigned int type;
  186. chip->buttons[i] = pdata->buttons[i];
  187. type = (pdata->buttons[i].type) ?: EV_KEY;
  188. input_set_capability(input, type, pdata->buttons[i].code);
  189. }
  190. input_set_drvdata(input, chip);
  191. /*
  192. * Initialize cached registers from their original values.
  193. * we can't share this chip with another i2c master.
  194. */
  195. error = tca6416_setup_registers(chip);
  196. if (error)
  197. return error;
  198. if (chip->use_polling) {
  199. error = input_setup_polling(input, tca6416_keys_scan);
  200. if (error) {
  201. dev_err(&client->dev, "Failed to setup polling\n");
  202. return error;
  203. }
  204. input_set_poll_interval(input, TCA6416_POLL_INTERVAL);
  205. } else {
  206. error = devm_request_threaded_irq(&client->dev, client->irq,
  207. NULL, tca6416_keys_isr,
  208. IRQF_TRIGGER_FALLING |
  209. IRQF_ONESHOT |
  210. IRQF_NO_AUTOEN,
  211. "tca6416-keypad", input);
  212. if (error) {
  213. dev_dbg(&client->dev,
  214. "Unable to claim irq %d; error %d\n",
  215. client->irq, error);
  216. return error;
  217. }
  218. }
  219. error = input_register_device(input);
  220. if (error) {
  221. dev_dbg(&client->dev,
  222. "Unable to register input device, error: %d\n", error);
  223. return error;
  224. }
  225. i2c_set_clientdata(client, chip);
  226. return 0;
  227. }
  228. static struct i2c_driver tca6416_keypad_driver = {
  229. .driver = {
  230. .name = "tca6416-keypad",
  231. },
  232. .probe = tca6416_keypad_probe,
  233. .id_table = tca6416_id,
  234. };
  235. static int __init tca6416_keypad_init(void)
  236. {
  237. return i2c_add_driver(&tca6416_keypad_driver);
  238. }
  239. subsys_initcall(tca6416_keypad_init);
  240. static void __exit tca6416_keypad_exit(void)
  241. {
  242. i2c_del_driver(&tca6416_keypad_driver);
  243. }
  244. module_exit(tca6416_keypad_exit);
  245. MODULE_AUTHOR("Sriramakrishnan <srk@ti.com>");
  246. MODULE_DESCRIPTION("Keypad driver over tca6416 IO expander");
  247. MODULE_LICENSE("GPL");