cy8ctmg110_ts.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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 != len + 1) {
  76. dev_err(&client->dev, "i2c write data cmd failed\n");
  77. return ret < 0 ? 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. ts->reset_pin = pdata->reset_pin;
  163. ts->irq_pin = pdata->irq_pin;
  164. snprintf(ts->phys, sizeof(ts->phys),
  165. "%s/input0", dev_name(&client->dev));
  166. input_dev->name = CY8CTMG110_DRIVER_NAME " Touchscreen";
  167. input_dev->phys = ts->phys;
  168. input_dev->id.bustype = BUS_I2C;
  169. input_dev->dev.parent = &client->dev;
  170. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  171. input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
  172. input_set_abs_params(input_dev, ABS_X,
  173. CY8CTMG110_X_MIN, CY8CTMG110_X_MAX, 4, 0);
  174. input_set_abs_params(input_dev, ABS_Y,
  175. CY8CTMG110_Y_MIN, CY8CTMG110_Y_MAX, 4, 0);
  176. if (ts->reset_pin) {
  177. err = gpio_request(ts->reset_pin, NULL);
  178. if (err) {
  179. dev_err(&client->dev,
  180. "Unable to request GPIO pin %d.\n",
  181. ts->reset_pin);
  182. goto err_free_mem;
  183. }
  184. }
  185. cy8ctmg110_power(ts, true);
  186. cy8ctmg110_set_sleepmode(ts, false);
  187. err = gpio_request(ts->irq_pin, "touch_irq_key");
  188. if (err < 0) {
  189. dev_err(&client->dev,
  190. "Failed to request GPIO %d, error %d\n",
  191. ts->irq_pin, err);
  192. goto err_shutoff_device;
  193. }
  194. err = gpio_direction_input(ts->irq_pin);
  195. if (err < 0) {
  196. dev_err(&client->dev,
  197. "Failed to configure input direction for GPIO %d, error %d\n",
  198. ts->irq_pin, err);
  199. goto err_free_irq_gpio;
  200. }
  201. client->irq = gpio_to_irq(ts->irq_pin);
  202. if (client->irq < 0) {
  203. err = client->irq;
  204. dev_err(&client->dev,
  205. "Unable to get irq number for GPIO %d, error %d\n",
  206. ts->irq_pin, err);
  207. goto err_free_irq_gpio;
  208. }
  209. err = request_threaded_irq(client->irq, NULL, cy8ctmg110_irq_thread,
  210. IRQF_TRIGGER_RISING, "touch_reset_key", ts);
  211. if (err < 0) {
  212. dev_err(&client->dev,
  213. "irq %d busy? error %d\n", client->irq, err);
  214. goto err_free_irq_gpio;
  215. }
  216. err = input_register_device(input_dev);
  217. if (err)
  218. goto err_free_irq;
  219. i2c_set_clientdata(client, ts);
  220. device_init_wakeup(&client->dev, 1);
  221. return 0;
  222. err_free_irq:
  223. free_irq(client->irq, ts);
  224. err_free_irq_gpio:
  225. gpio_free(ts->irq_pin);
  226. err_shutoff_device:
  227. cy8ctmg110_set_sleepmode(ts, true);
  228. cy8ctmg110_power(ts, false);
  229. if (ts->reset_pin)
  230. gpio_free(ts->reset_pin);
  231. err_free_mem:
  232. input_free_device(input_dev);
  233. kfree(ts);
  234. return err;
  235. }
  236. #ifdef CONFIG_PM
  237. static int cy8ctmg110_suspend(struct device *dev)
  238. {
  239. struct i2c_client *client = to_i2c_client(dev);
  240. struct cy8ctmg110 *ts = i2c_get_clientdata(client);
  241. if (device_may_wakeup(&client->dev))
  242. enable_irq_wake(client->irq);
  243. else {
  244. cy8ctmg110_set_sleepmode(ts, true);
  245. cy8ctmg110_power(ts, false);
  246. }
  247. return 0;
  248. }
  249. static int cy8ctmg110_resume(struct device *dev)
  250. {
  251. struct i2c_client *client = to_i2c_client(dev);
  252. struct cy8ctmg110 *ts = i2c_get_clientdata(client);
  253. if (device_may_wakeup(&client->dev))
  254. disable_irq_wake(client->irq);
  255. else {
  256. cy8ctmg110_power(ts, true);
  257. cy8ctmg110_set_sleepmode(ts, false);
  258. }
  259. return 0;
  260. }
  261. static SIMPLE_DEV_PM_OPS(cy8ctmg110_pm, cy8ctmg110_suspend, cy8ctmg110_resume);
  262. #endif
  263. static int __devexit cy8ctmg110_remove(struct i2c_client *client)
  264. {
  265. struct cy8ctmg110 *ts = i2c_get_clientdata(client);
  266. cy8ctmg110_set_sleepmode(ts, true);
  267. cy8ctmg110_power(ts, false);
  268. free_irq(client->irq, ts);
  269. input_unregister_device(ts->input);
  270. gpio_free(ts->irq_pin);
  271. if (ts->reset_pin)
  272. gpio_free(ts->reset_pin);
  273. kfree(ts);
  274. return 0;
  275. }
  276. static const struct i2c_device_id cy8ctmg110_idtable[] = {
  277. { CY8CTMG110_DRIVER_NAME, 1 },
  278. { }
  279. };
  280. MODULE_DEVICE_TABLE(i2c, cy8ctmg110_idtable);
  281. static struct i2c_driver cy8ctmg110_driver = {
  282. .driver = {
  283. .owner = THIS_MODULE,
  284. .name = CY8CTMG110_DRIVER_NAME,
  285. #ifdef CONFIG_PM
  286. .pm = &cy8ctmg110_pm,
  287. #endif
  288. },
  289. .id_table = cy8ctmg110_idtable,
  290. .probe = cy8ctmg110_probe,
  291. .remove = __devexit_p(cy8ctmg110_remove),
  292. };
  293. static int __init cy8ctmg110_init(void)
  294. {
  295. return i2c_add_driver(&cy8ctmg110_driver);
  296. }
  297. static void __exit cy8ctmg110_exit(void)
  298. {
  299. i2c_del_driver(&cy8ctmg110_driver);
  300. }
  301. module_init(cy8ctmg110_init);
  302. module_exit(cy8ctmg110_exit);
  303. MODULE_AUTHOR("Samuli Konttila <samuli.konttila@aavamobile.com>");
  304. MODULE_DESCRIPTION("cy8ctmg110 TouchScreen Driver");
  305. MODULE_LICENSE("GPL v2");