tca6416-keypad.c 8.6 KB

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