cy8ctmg110_ts.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /*
  2. * Driver for cypress touch screen controller
  3. *
  4. * Copyright (c) 2009 Aava Mobile
  5. *
  6. * Some cleanups by Alan Cox <alan@linux.intel.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. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <linux/module.h>
  22. #include <linux/kernel.h>
  23. #include <linux/input.h>
  24. #include <linux/slab.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/io.h>
  27. #include <linux/i2c.h>
  28. #include <linux/gpio.h>
  29. #include <linux/input/cy8ctmg110_pdata.h>
  30. #define CY8CTMG110_DRIVER_NAME "cy8ctmg110"
  31. /* Touch coordinates */
  32. #define CY8CTMG110_X_MIN 0
  33. #define CY8CTMG110_Y_MIN 0
  34. #define CY8CTMG110_X_MAX 759
  35. #define CY8CTMG110_Y_MAX 465
  36. /* cy8ctmg110 register definitions */
  37. #define CY8CTMG110_TOUCH_WAKEUP_TIME 0
  38. #define CY8CTMG110_TOUCH_SLEEP_TIME 2
  39. #define CY8CTMG110_TOUCH_X1 3
  40. #define CY8CTMG110_TOUCH_Y1 5
  41. #define CY8CTMG110_TOUCH_X2 7
  42. #define CY8CTMG110_TOUCH_Y2 9
  43. #define CY8CTMG110_FINGERS 11
  44. #define CY8CTMG110_GESTURE 12
  45. #define CY8CTMG110_REG_MAX 13
  46. /*
  47. * The touch driver structure.
  48. */
  49. struct cy8ctmg110 {
  50. struct input_dev *input;
  51. char phys[32];
  52. struct i2c_client *client;
  53. int reset_pin;
  54. int irq_pin;
  55. };
  56. /*
  57. * cy8ctmg110_power is the routine that is called when touch hardware
  58. * will powered off or on.
  59. */
  60. static void cy8ctmg110_power(struct cy8ctmg110 *ts, bool poweron)
  61. {
  62. if (ts->reset_pin)
  63. gpio_direction_output(ts->reset_pin, 1 - poweron);
  64. }
  65. static int cy8ctmg110_write_regs(struct cy8ctmg110 *tsc, unsigned char reg,
  66. unsigned char len, unsigned char *value)
  67. {
  68. struct i2c_client *client = tsc->client;
  69. int ret;
  70. unsigned char i2c_data[6];
  71. BUG_ON(len > 5);
  72. i2c_data[0] = reg;
  73. memcpy(i2c_data + 1, value, len);
  74. ret = i2c_master_send(client, i2c_data, len + 1);
  75. if (ret != 1) {
  76. dev_err(&client->dev, "i2c write data cmd failed\n");
  77. return ret ? ret : -EIO;
  78. }
  79. return 0;
  80. }
  81. static int cy8ctmg110_read_regs(struct cy8ctmg110 *tsc,
  82. unsigned char *data, unsigned char len, unsigned char cmd)
  83. {
  84. struct i2c_client *client = tsc->client;
  85. int ret;
  86. struct i2c_msg msg[2] = {
  87. /* first write slave position to i2c devices */
  88. { client->addr, 0, 1, &cmd },
  89. /* Second read data from position */
  90. { client->addr, I2C_M_RD, len, data }
  91. };
  92. ret = i2c_transfer(client->adapter, msg, 2);
  93. if (ret < 0)
  94. return ret;
  95. return 0;
  96. }
  97. static int cy8ctmg110_touch_pos(struct cy8ctmg110 *tsc)
  98. {
  99. struct input_dev *input = tsc->input;
  100. unsigned char reg_p[CY8CTMG110_REG_MAX];
  101. int x, y;
  102. memset(reg_p, 0, CY8CTMG110_REG_MAX);
  103. /* Reading coordinates */
  104. if (cy8ctmg110_read_regs(tsc, reg_p, 9, CY8CTMG110_TOUCH_X1) != 0)
  105. return -EIO;
  106. y = reg_p[2] << 8 | reg_p[3];
  107. x = reg_p[0] << 8 | reg_p[1];
  108. /* Number of touch */
  109. if (reg_p[8] == 0) {
  110. input_report_key(input, BTN_TOUCH, 0);
  111. } else {
  112. input_report_key(input, BTN_TOUCH, 1);
  113. input_report_abs(input, ABS_X, x);
  114. input_report_abs(input, ABS_Y, y);
  115. }
  116. input_sync(input);
  117. return 0;
  118. }
  119. static int cy8ctmg110_set_sleepmode(struct cy8ctmg110 *ts, bool sleep)
  120. {
  121. unsigned char reg_p[3];
  122. if (sleep) {
  123. reg_p[0] = 0x00;
  124. reg_p[1] = 0xff;
  125. reg_p[2] = 5;
  126. } else {
  127. reg_p[0] = 0x10;
  128. reg_p[1] = 0xff;
  129. reg_p[2] = 0;
  130. }
  131. return cy8ctmg110_write_regs(ts, CY8CTMG110_TOUCH_WAKEUP_TIME, 3, reg_p);
  132. }
  133. static irqreturn_t cy8ctmg110_irq_thread(int irq, void *dev_id)
  134. {
  135. struct cy8ctmg110 *tsc = dev_id;
  136. cy8ctmg110_touch_pos(tsc);
  137. return IRQ_HANDLED;
  138. }
  139. static int __devinit cy8ctmg110_probe(struct i2c_client *client,
  140. const struct i2c_device_id *id)
  141. {
  142. const struct cy8ctmg110_pdata *pdata = client->dev.platform_data;
  143. struct cy8ctmg110 *ts;
  144. struct input_dev *input_dev;
  145. int err;
  146. /* No pdata no way forward */
  147. if (pdata == NULL) {
  148. dev_err(&client->dev, "no pdata\n");
  149. return -ENODEV;
  150. }
  151. if (!i2c_check_functionality(client->adapter,
  152. I2C_FUNC_SMBUS_READ_WORD_DATA))
  153. return -EIO;
  154. ts = kzalloc(sizeof(struct cy8ctmg110), GFP_KERNEL);
  155. input_dev = input_allocate_device();
  156. if (!ts || !input_dev) {
  157. err = -ENOMEM;
  158. goto err_free_mem;
  159. }
  160. ts->client = client;
  161. ts->input = input_dev;
  162. snprintf(ts->phys, sizeof(ts->phys),
  163. "%s/input0", dev_name(&client->dev));
  164. input_dev->name = CY8CTMG110_DRIVER_NAME " Touchscreen";
  165. input_dev->phys = ts->phys;
  166. input_dev->id.bustype = BUS_I2C;
  167. input_dev->dev.parent = &client->dev;
  168. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  169. input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
  170. input_set_abs_params(input_dev, ABS_X,
  171. CY8CTMG110_X_MIN, CY8CTMG110_X_MAX, 4, 0);
  172. input_set_abs_params(input_dev, ABS_Y,
  173. CY8CTMG110_Y_MIN, CY8CTMG110_Y_MAX, 4, 0);
  174. if (ts->reset_pin) {
  175. err = gpio_request(ts->reset_pin, NULL);
  176. if (err) {
  177. dev_err(&client->dev,
  178. "Unable to request GPIO pin %d.\n",
  179. ts->reset_pin);
  180. goto err_free_mem;
  181. }
  182. }
  183. cy8ctmg110_power(ts, true);
  184. cy8ctmg110_set_sleepmode(ts, false);
  185. err = gpio_request(ts->irq_pin, "touch_irq_key");
  186. if (err < 0) {
  187. dev_err(&client->dev,
  188. "Failed to request GPIO %d, error %d\n",
  189. ts->irq_pin, err);
  190. goto err_shutoff_device;
  191. }
  192. err = gpio_direction_input(ts->irq_pin);
  193. if (err < 0) {
  194. dev_err(&client->dev,
  195. "Failed to configure input direction for GPIO %d, error %d\n",
  196. ts->irq_pin, err);
  197. goto err_free_irq_gpio;
  198. }
  199. client->irq = gpio_to_irq(ts->irq_pin);
  200. if (client->irq < 0) {
  201. err = client->irq;
  202. dev_err(&client->dev,
  203. "Unable to get irq number for GPIO %d, error %d\n",
  204. ts->irq_pin, err);
  205. goto err_free_irq_gpio;
  206. }
  207. err = request_threaded_irq(client->irq, NULL, cy8ctmg110_irq_thread,
  208. IRQF_TRIGGER_RISING, "touch_reset_key", ts);
  209. if (err < 0) {
  210. dev_err(&client->dev,
  211. "irq %d busy? error %d\n", client->irq, err);
  212. goto err_free_irq_gpio;
  213. }
  214. err = input_register_device(input_dev);
  215. if (err)
  216. goto err_free_irq;
  217. i2c_set_clientdata(client, ts);
  218. device_init_wakeup(&client->dev, 1);
  219. return 0;
  220. err_free_irq:
  221. free_irq(client->irq, ts);
  222. err_free_irq_gpio:
  223. gpio_free(ts->irq_pin);
  224. err_shutoff_device:
  225. cy8ctmg110_set_sleepmode(ts, true);
  226. cy8ctmg110_power(ts, false);
  227. if (ts->reset_pin)
  228. gpio_free(ts->reset_pin);
  229. err_free_mem:
  230. input_free_device(input_dev);
  231. kfree(ts);
  232. return err;
  233. }
  234. #ifdef CONFIG_PM
  235. static int cy8ctmg110_suspend(struct device *dev)
  236. {
  237. struct i2c_client *client = to_i2c_client(dev);
  238. struct cy8ctmg110 *ts = i2c_get_clientdata(client);
  239. if (device_may_wakeup(&client->dev))
  240. enable_irq_wake(client->irq);
  241. else {
  242. cy8ctmg110_set_sleepmode(ts, true);
  243. cy8ctmg110_power(ts, false);
  244. }
  245. return 0;
  246. }
  247. static int cy8ctmg110_resume(struct device *dev)
  248. {
  249. struct i2c_client *client = to_i2c_client(dev);
  250. struct cy8ctmg110 *ts = i2c_get_clientdata(client);
  251. if (device_may_wakeup(&client->dev))
  252. disable_irq_wake(client->irq);
  253. else {
  254. cy8ctmg110_power(ts, true);
  255. cy8ctmg110_set_sleepmode(ts, false);
  256. }
  257. return 0;
  258. }
  259. static SIMPLE_DEV_PM_OPS(cy8ctmg110_pm, cy8ctmg110_suspend, cy8ctmg110_resume);
  260. #endif
  261. static int __devexit cy8ctmg110_remove(struct i2c_client *client)
  262. {
  263. struct cy8ctmg110 *ts = i2c_get_clientdata(client);
  264. cy8ctmg110_set_sleepmode(ts, true);
  265. cy8ctmg110_power(ts, false);
  266. free_irq(client->irq, ts);
  267. input_unregister_device(ts->input);
  268. gpio_free(ts->irq_pin);
  269. if (ts->reset_pin)
  270. gpio_free(ts->reset_pin);
  271. kfree(ts);
  272. return 0;
  273. }
  274. static struct i2c_device_id cy8ctmg110_idtable[] = {
  275. { CY8CTMG110_DRIVER_NAME, 1 },
  276. { }
  277. };
  278. MODULE_DEVICE_TABLE(i2c, cy8ctmg110_idtable);
  279. static struct i2c_driver cy8ctmg110_driver = {
  280. .driver = {
  281. .owner = THIS_MODULE,
  282. .name = CY8CTMG110_DRIVER_NAME,
  283. #ifdef CONFIG_PM
  284. .pm = &cy8ctmg110_pm,
  285. #endif
  286. },
  287. .id_table = cy8ctmg110_idtable,
  288. .probe = cy8ctmg110_probe,
  289. .remove = __devexit_p(cy8ctmg110_remove),
  290. };
  291. static int __init cy8ctmg110_init(void)
  292. {
  293. return i2c_add_driver(&cy8ctmg110_driver);
  294. }
  295. static void __exit cy8ctmg110_exit(void)
  296. {
  297. i2c_del_driver(&cy8ctmg110_driver);
  298. }
  299. module_init(cy8ctmg110_init);
  300. module_exit(cy8ctmg110_exit);
  301. MODULE_AUTHOR("Samuli Konttila <samuli.konttila@aavamobile.com>");
  302. MODULE_DESCRIPTION("cy8ctmg110 TouchScreen Driver");
  303. MODULE_LICENSE("GPL v2");