cy8ctmg110_ts.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. {
  89. .addr = client->addr,
  90. .len = 1,
  91. .buf = &cmd
  92. },
  93. /* Second read data from position */
  94. {
  95. .addr = client->addr,
  96. .flags = I2C_M_RD,
  97. .len = len,
  98. .buf = data
  99. }
  100. };
  101. ret = i2c_transfer(client->adapter, msg, 2);
  102. if (ret < 0)
  103. return ret;
  104. return 0;
  105. }
  106. static int cy8ctmg110_touch_pos(struct cy8ctmg110 *tsc)
  107. {
  108. struct input_dev *input = tsc->input;
  109. unsigned char reg_p[CY8CTMG110_REG_MAX];
  110. int x, y;
  111. memset(reg_p, 0, CY8CTMG110_REG_MAX);
  112. /* Reading coordinates */
  113. if (cy8ctmg110_read_regs(tsc, reg_p, 9, CY8CTMG110_TOUCH_X1) != 0)
  114. return -EIO;
  115. y = reg_p[2] << 8 | reg_p[3];
  116. x = reg_p[0] << 8 | reg_p[1];
  117. /* Number of touch */
  118. if (reg_p[8] == 0) {
  119. input_report_key(input, BTN_TOUCH, 0);
  120. } else {
  121. input_report_key(input, BTN_TOUCH, 1);
  122. input_report_abs(input, ABS_X, x);
  123. input_report_abs(input, ABS_Y, y);
  124. }
  125. input_sync(input);
  126. return 0;
  127. }
  128. static int cy8ctmg110_set_sleepmode(struct cy8ctmg110 *ts, bool sleep)
  129. {
  130. unsigned char reg_p[3];
  131. if (sleep) {
  132. reg_p[0] = 0x00;
  133. reg_p[1] = 0xff;
  134. reg_p[2] = 5;
  135. } else {
  136. reg_p[0] = 0x10;
  137. reg_p[1] = 0xff;
  138. reg_p[2] = 0;
  139. }
  140. return cy8ctmg110_write_regs(ts, CY8CTMG110_TOUCH_WAKEUP_TIME, 3, reg_p);
  141. }
  142. static irqreturn_t cy8ctmg110_irq_thread(int irq, void *dev_id)
  143. {
  144. struct cy8ctmg110 *tsc = dev_id;
  145. cy8ctmg110_touch_pos(tsc);
  146. return IRQ_HANDLED;
  147. }
  148. static int cy8ctmg110_probe(struct i2c_client *client,
  149. const struct i2c_device_id *id)
  150. {
  151. const struct cy8ctmg110_pdata *pdata = dev_get_platdata(&client->dev);
  152. struct cy8ctmg110 *ts;
  153. struct input_dev *input_dev;
  154. int err;
  155. /* No pdata no way forward */
  156. if (pdata == NULL) {
  157. dev_err(&client->dev, "no pdata\n");
  158. return -ENODEV;
  159. }
  160. if (!i2c_check_functionality(client->adapter,
  161. I2C_FUNC_SMBUS_READ_WORD_DATA))
  162. return -EIO;
  163. ts = kzalloc(sizeof(struct cy8ctmg110), GFP_KERNEL);
  164. input_dev = input_allocate_device();
  165. if (!ts || !input_dev) {
  166. err = -ENOMEM;
  167. goto err_free_mem;
  168. }
  169. ts->client = client;
  170. ts->input = input_dev;
  171. ts->reset_pin = pdata->reset_pin;
  172. ts->irq_pin = pdata->irq_pin;
  173. snprintf(ts->phys, sizeof(ts->phys),
  174. "%s/input0", dev_name(&client->dev));
  175. input_dev->name = CY8CTMG110_DRIVER_NAME " Touchscreen";
  176. input_dev->phys = ts->phys;
  177. input_dev->id.bustype = BUS_I2C;
  178. input_dev->dev.parent = &client->dev;
  179. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  180. input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
  181. input_set_abs_params(input_dev, ABS_X,
  182. CY8CTMG110_X_MIN, CY8CTMG110_X_MAX, 4, 0);
  183. input_set_abs_params(input_dev, ABS_Y,
  184. CY8CTMG110_Y_MIN, CY8CTMG110_Y_MAX, 4, 0);
  185. if (ts->reset_pin) {
  186. err = gpio_request(ts->reset_pin, NULL);
  187. if (err) {
  188. dev_err(&client->dev,
  189. "Unable to request GPIO pin %d.\n",
  190. ts->reset_pin);
  191. goto err_free_mem;
  192. }
  193. }
  194. cy8ctmg110_power(ts, true);
  195. cy8ctmg110_set_sleepmode(ts, false);
  196. err = gpio_request(ts->irq_pin, "touch_irq_key");
  197. if (err < 0) {
  198. dev_err(&client->dev,
  199. "Failed to request GPIO %d, error %d\n",
  200. ts->irq_pin, err);
  201. goto err_shutoff_device;
  202. }
  203. err = gpio_direction_input(ts->irq_pin);
  204. if (err < 0) {
  205. dev_err(&client->dev,
  206. "Failed to configure input direction for GPIO %d, error %d\n",
  207. ts->irq_pin, err);
  208. goto err_free_irq_gpio;
  209. }
  210. client->irq = gpio_to_irq(ts->irq_pin);
  211. if (client->irq < 0) {
  212. err = client->irq;
  213. dev_err(&client->dev,
  214. "Unable to get irq number for GPIO %d, error %d\n",
  215. ts->irq_pin, err);
  216. goto err_free_irq_gpio;
  217. }
  218. err = request_threaded_irq(client->irq, NULL, cy8ctmg110_irq_thread,
  219. IRQF_TRIGGER_RISING | IRQF_ONESHOT,
  220. "touch_reset_key", ts);
  221. if (err < 0) {
  222. dev_err(&client->dev,
  223. "irq %d busy? error %d\n", client->irq, err);
  224. goto err_free_irq_gpio;
  225. }
  226. err = input_register_device(input_dev);
  227. if (err)
  228. goto err_free_irq;
  229. i2c_set_clientdata(client, ts);
  230. device_init_wakeup(&client->dev, 1);
  231. return 0;
  232. err_free_irq:
  233. free_irq(client->irq, ts);
  234. err_free_irq_gpio:
  235. gpio_free(ts->irq_pin);
  236. err_shutoff_device:
  237. cy8ctmg110_set_sleepmode(ts, true);
  238. cy8ctmg110_power(ts, false);
  239. if (ts->reset_pin)
  240. gpio_free(ts->reset_pin);
  241. err_free_mem:
  242. input_free_device(input_dev);
  243. kfree(ts);
  244. return err;
  245. }
  246. static int __maybe_unused cy8ctmg110_suspend(struct device *dev)
  247. {
  248. struct i2c_client *client = to_i2c_client(dev);
  249. struct cy8ctmg110 *ts = i2c_get_clientdata(client);
  250. if (device_may_wakeup(&client->dev))
  251. enable_irq_wake(client->irq);
  252. else {
  253. cy8ctmg110_set_sleepmode(ts, true);
  254. cy8ctmg110_power(ts, false);
  255. }
  256. return 0;
  257. }
  258. static int __maybe_unused cy8ctmg110_resume(struct device *dev)
  259. {
  260. struct i2c_client *client = to_i2c_client(dev);
  261. struct cy8ctmg110 *ts = i2c_get_clientdata(client);
  262. if (device_may_wakeup(&client->dev))
  263. disable_irq_wake(client->irq);
  264. else {
  265. cy8ctmg110_power(ts, true);
  266. cy8ctmg110_set_sleepmode(ts, false);
  267. }
  268. return 0;
  269. }
  270. static SIMPLE_DEV_PM_OPS(cy8ctmg110_pm, cy8ctmg110_suspend, cy8ctmg110_resume);
  271. static int cy8ctmg110_remove(struct i2c_client *client)
  272. {
  273. struct cy8ctmg110 *ts = i2c_get_clientdata(client);
  274. cy8ctmg110_set_sleepmode(ts, true);
  275. cy8ctmg110_power(ts, false);
  276. free_irq(client->irq, ts);
  277. input_unregister_device(ts->input);
  278. gpio_free(ts->irq_pin);
  279. if (ts->reset_pin)
  280. gpio_free(ts->reset_pin);
  281. kfree(ts);
  282. return 0;
  283. }
  284. static const struct i2c_device_id cy8ctmg110_idtable[] = {
  285. { CY8CTMG110_DRIVER_NAME, 1 },
  286. { }
  287. };
  288. MODULE_DEVICE_TABLE(i2c, cy8ctmg110_idtable);
  289. static struct i2c_driver cy8ctmg110_driver = {
  290. .driver = {
  291. .name = CY8CTMG110_DRIVER_NAME,
  292. .pm = &cy8ctmg110_pm,
  293. },
  294. .id_table = cy8ctmg110_idtable,
  295. .probe = cy8ctmg110_probe,
  296. .remove = cy8ctmg110_remove,
  297. };
  298. module_i2c_driver(cy8ctmg110_driver);
  299. MODULE_AUTHOR("Samuli Konttila <samuli.konttila@aavamobile.com>");
  300. MODULE_DESCRIPTION("cy8ctmg110 TouchScreen Driver");
  301. MODULE_LICENSE("GPL v2");