tca6416-keypad.c 8.4 KB

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