qt2160.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /*
  2. * qt2160.c - Atmel AT42QT2160 Touch Sense Controller
  3. *
  4. * Copyright (C) 2009 Raphael Derosso Pereira <raphaelpereira@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/init.h>
  22. #include <linux/module.h>
  23. #include <linux/slab.h>
  24. #include <linux/jiffies.h>
  25. #include <linux/i2c.h>
  26. #include <linux/irq.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/input.h>
  29. #define QT2160_VALID_CHIPID 0x11
  30. #define QT2160_CMD_CHIPID 0
  31. #define QT2160_CMD_CODEVER 1
  32. #define QT2160_CMD_GSTAT 2
  33. #define QT2160_CMD_KEYS3 3
  34. #define QT2160_CMD_KEYS4 4
  35. #define QT2160_CMD_SLIDE 5
  36. #define QT2160_CMD_GPIOS 6
  37. #define QT2160_CMD_SUBVER 7
  38. #define QT2160_CMD_CALIBRATE 10
  39. #define QT2160_CYCLE_INTERVAL (2*HZ)
  40. static unsigned char qt2160_key2code[] = {
  41. KEY_0, KEY_1, KEY_2, KEY_3,
  42. KEY_4, KEY_5, KEY_6, KEY_7,
  43. KEY_8, KEY_9, KEY_A, KEY_B,
  44. KEY_C, KEY_D, KEY_E, KEY_F,
  45. };
  46. struct qt2160_data {
  47. struct i2c_client *client;
  48. struct input_dev *input;
  49. struct delayed_work dwork;
  50. spinlock_t lock; /* Protects canceling/rescheduling of dwork */
  51. unsigned short keycodes[ARRAY_SIZE(qt2160_key2code)];
  52. u16 key_matrix;
  53. };
  54. static int qt2160_read_block(struct i2c_client *client,
  55. u8 inireg, u8 *buffer, unsigned int count)
  56. {
  57. int error, idx = 0;
  58. /*
  59. * Can't use SMBus block data read. Check for I2C functionality to speed
  60. * things up whenever possible. Otherwise we will be forced to read
  61. * sequentially.
  62. */
  63. if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  64. error = i2c_smbus_write_byte(client, inireg + idx);
  65. if (error) {
  66. dev_err(&client->dev,
  67. "couldn't send request. Returned %d\n", error);
  68. return error;
  69. }
  70. error = i2c_master_recv(client, buffer, count);
  71. if (error != count) {
  72. dev_err(&client->dev,
  73. "couldn't read registers. Returned %d bytes\n", error);
  74. return error;
  75. }
  76. } else {
  77. while (count--) {
  78. int data;
  79. error = i2c_smbus_write_byte(client, inireg + idx);
  80. if (error) {
  81. dev_err(&client->dev,
  82. "couldn't send request. Returned %d\n", error);
  83. return error;
  84. }
  85. data = i2c_smbus_read_byte(client);
  86. if (data < 0) {
  87. dev_err(&client->dev,
  88. "couldn't read register. Returned %d\n", data);
  89. return data;
  90. }
  91. buffer[idx++] = data;
  92. }
  93. }
  94. return 0;
  95. }
  96. static int qt2160_get_key_matrix(struct qt2160_data *qt2160)
  97. {
  98. struct i2c_client *client = qt2160->client;
  99. struct input_dev *input = qt2160->input;
  100. u8 regs[6];
  101. u16 old_matrix, new_matrix;
  102. int ret, i, mask;
  103. dev_dbg(&client->dev, "requesting keys...\n");
  104. /*
  105. * Read all registers from General Status Register
  106. * to GPIOs register
  107. */
  108. ret = qt2160_read_block(client, QT2160_CMD_GSTAT, regs, 6);
  109. if (ret) {
  110. dev_err(&client->dev,
  111. "could not perform chip read.\n");
  112. return ret;
  113. }
  114. old_matrix = qt2160->key_matrix;
  115. qt2160->key_matrix = new_matrix = (regs[2] << 8) | regs[1];
  116. mask = 0x01;
  117. for (i = 0; i < 16; ++i, mask <<= 1) {
  118. int keyval = new_matrix & mask;
  119. if ((old_matrix & mask) != keyval) {
  120. input_report_key(input, qt2160->keycodes[i], keyval);
  121. dev_dbg(&client->dev, "key %d %s\n",
  122. i, keyval ? "pressed" : "released");
  123. }
  124. }
  125. input_sync(input);
  126. return 0;
  127. }
  128. static irqreturn_t qt2160_irq(int irq, void *_qt2160)
  129. {
  130. struct qt2160_data *qt2160 = _qt2160;
  131. unsigned long flags;
  132. spin_lock_irqsave(&qt2160->lock, flags);
  133. mod_delayed_work(system_wq, &qt2160->dwork, 0);
  134. spin_unlock_irqrestore(&qt2160->lock, flags);
  135. return IRQ_HANDLED;
  136. }
  137. static void qt2160_schedule_read(struct qt2160_data *qt2160)
  138. {
  139. spin_lock_irq(&qt2160->lock);
  140. schedule_delayed_work(&qt2160->dwork, QT2160_CYCLE_INTERVAL);
  141. spin_unlock_irq(&qt2160->lock);
  142. }
  143. static void qt2160_worker(struct work_struct *work)
  144. {
  145. struct qt2160_data *qt2160 =
  146. container_of(work, struct qt2160_data, dwork.work);
  147. dev_dbg(&qt2160->client->dev, "worker\n");
  148. qt2160_get_key_matrix(qt2160);
  149. /* Avoid device lock up by checking every so often */
  150. qt2160_schedule_read(qt2160);
  151. }
  152. static int qt2160_read(struct i2c_client *client, u8 reg)
  153. {
  154. int ret;
  155. ret = i2c_smbus_write_byte(client, reg);
  156. if (ret) {
  157. dev_err(&client->dev,
  158. "couldn't send request. Returned %d\n", ret);
  159. return ret;
  160. }
  161. ret = i2c_smbus_read_byte(client);
  162. if (ret < 0) {
  163. dev_err(&client->dev,
  164. "couldn't read register. Returned %d\n", ret);
  165. return ret;
  166. }
  167. return ret;
  168. }
  169. static int qt2160_write(struct i2c_client *client, u8 reg, u8 data)
  170. {
  171. int ret;
  172. ret = i2c_smbus_write_byte_data(client, reg, data);
  173. if (ret < 0)
  174. dev_err(&client->dev,
  175. "couldn't write data. Returned %d\n", ret);
  176. return ret;
  177. }
  178. static bool qt2160_identify(struct i2c_client *client)
  179. {
  180. int id, ver, rev;
  181. /* Read Chid ID to check if chip is valid */
  182. id = qt2160_read(client, QT2160_CMD_CHIPID);
  183. if (id != QT2160_VALID_CHIPID) {
  184. dev_err(&client->dev, "ID %d not supported\n", id);
  185. return false;
  186. }
  187. /* Read chip firmware version */
  188. ver = qt2160_read(client, QT2160_CMD_CODEVER);
  189. if (ver < 0) {
  190. dev_err(&client->dev, "could not get firmware version\n");
  191. return false;
  192. }
  193. /* Read chip firmware revision */
  194. rev = qt2160_read(client, QT2160_CMD_SUBVER);
  195. if (rev < 0) {
  196. dev_err(&client->dev, "could not get firmware revision\n");
  197. return false;
  198. }
  199. dev_info(&client->dev, "AT42QT2160 firmware version %d.%d.%d\n",
  200. ver >> 4, ver & 0xf, rev);
  201. return true;
  202. }
  203. static int qt2160_probe(struct i2c_client *client,
  204. const struct i2c_device_id *id)
  205. {
  206. struct qt2160_data *qt2160;
  207. struct input_dev *input;
  208. int i;
  209. int error;
  210. /* Check functionality */
  211. error = i2c_check_functionality(client->adapter,
  212. I2C_FUNC_SMBUS_BYTE);
  213. if (!error) {
  214. dev_err(&client->dev, "%s adapter not supported\n",
  215. dev_driver_string(&client->adapter->dev));
  216. return -ENODEV;
  217. }
  218. if (!qt2160_identify(client))
  219. return -ENODEV;
  220. /* Chip is valid and active. Allocate structure */
  221. qt2160 = kzalloc(sizeof(struct qt2160_data), GFP_KERNEL);
  222. input = input_allocate_device();
  223. if (!qt2160 || !input) {
  224. dev_err(&client->dev, "insufficient memory\n");
  225. error = -ENOMEM;
  226. goto err_free_mem;
  227. }
  228. qt2160->client = client;
  229. qt2160->input = input;
  230. INIT_DELAYED_WORK(&qt2160->dwork, qt2160_worker);
  231. spin_lock_init(&qt2160->lock);
  232. input->name = "AT42QT2160 Touch Sense Keyboard";
  233. input->id.bustype = BUS_I2C;
  234. input->keycode = qt2160->keycodes;
  235. input->keycodesize = sizeof(qt2160->keycodes[0]);
  236. input->keycodemax = ARRAY_SIZE(qt2160_key2code);
  237. __set_bit(EV_KEY, input->evbit);
  238. __clear_bit(EV_REP, input->evbit);
  239. for (i = 0; i < ARRAY_SIZE(qt2160_key2code); i++) {
  240. qt2160->keycodes[i] = qt2160_key2code[i];
  241. __set_bit(qt2160_key2code[i], input->keybit);
  242. }
  243. __clear_bit(KEY_RESERVED, input->keybit);
  244. /* Calibrate device */
  245. error = qt2160_write(client, QT2160_CMD_CALIBRATE, 1);
  246. if (error) {
  247. dev_err(&client->dev, "failed to calibrate device\n");
  248. goto err_free_mem;
  249. }
  250. if (client->irq) {
  251. error = request_irq(client->irq, qt2160_irq,
  252. IRQF_TRIGGER_FALLING, "qt2160", qt2160);
  253. if (error) {
  254. dev_err(&client->dev,
  255. "failed to allocate irq %d\n", client->irq);
  256. goto err_free_mem;
  257. }
  258. }
  259. error = input_register_device(qt2160->input);
  260. if (error) {
  261. dev_err(&client->dev,
  262. "Failed to register input device\n");
  263. goto err_free_irq;
  264. }
  265. i2c_set_clientdata(client, qt2160);
  266. qt2160_schedule_read(qt2160);
  267. return 0;
  268. err_free_irq:
  269. if (client->irq)
  270. free_irq(client->irq, qt2160);
  271. err_free_mem:
  272. input_free_device(input);
  273. kfree(qt2160);
  274. return error;
  275. }
  276. static int qt2160_remove(struct i2c_client *client)
  277. {
  278. struct qt2160_data *qt2160 = i2c_get_clientdata(client);
  279. /* Release IRQ so no queue will be scheduled */
  280. if (client->irq)
  281. free_irq(client->irq, qt2160);
  282. cancel_delayed_work_sync(&qt2160->dwork);
  283. input_unregister_device(qt2160->input);
  284. kfree(qt2160);
  285. return 0;
  286. }
  287. static const struct i2c_device_id qt2160_idtable[] = {
  288. { "qt2160", 0, },
  289. { }
  290. };
  291. MODULE_DEVICE_TABLE(i2c, qt2160_idtable);
  292. static struct i2c_driver qt2160_driver = {
  293. .driver = {
  294. .name = "qt2160",
  295. .owner = THIS_MODULE,
  296. },
  297. .id_table = qt2160_idtable,
  298. .probe = qt2160_probe,
  299. .remove = qt2160_remove,
  300. };
  301. module_i2c_driver(qt2160_driver);
  302. MODULE_AUTHOR("Raphael Derosso Pereira <raphaelpereira@gmail.com>");
  303. MODULE_DESCRIPTION("Driver for AT42QT2160 Touch Sensor");
  304. MODULE_LICENSE("GPL");