88pm860x-ts.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * Touchscreen driver for Marvell 88PM860x
  3. *
  4. * Copyright (C) 2009 Marvell International Ltd.
  5. * Haojian Zhuang <haojian.zhuang@marvell.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/i2c.h>
  15. #include <linux/input.h>
  16. #include <linux/mfd/88pm860x.h>
  17. #define MEAS_LEN (8)
  18. #define ACCURATE_BIT (12)
  19. /* touch register */
  20. #define MEAS_EN3 (0x52)
  21. #define MEAS_TSIX_1 (0x8D)
  22. #define MEAS_TSIX_2 (0x8E)
  23. #define MEAS_TSIY_1 (0x8F)
  24. #define MEAS_TSIY_2 (0x90)
  25. #define MEAS_TSIZ1_1 (0x91)
  26. #define MEAS_TSIZ1_2 (0x92)
  27. #define MEAS_TSIZ2_1 (0x93)
  28. #define MEAS_TSIZ2_2 (0x94)
  29. /* bit definitions of touch */
  30. #define MEAS_PD_EN (1 << 3)
  31. #define MEAS_TSIX_EN (1 << 4)
  32. #define MEAS_TSIY_EN (1 << 5)
  33. #define MEAS_TSIZ1_EN (1 << 6)
  34. #define MEAS_TSIZ2_EN (1 << 7)
  35. struct pm860x_touch {
  36. struct input_dev *idev;
  37. struct i2c_client *i2c;
  38. struct pm860x_chip *chip;
  39. int irq;
  40. int res_x; /* resistor of Xplate */
  41. };
  42. static irqreturn_t pm860x_touch_handler(int irq, void *data)
  43. {
  44. struct pm860x_touch *touch = data;
  45. struct pm860x_chip *chip = touch->chip;
  46. unsigned char buf[MEAS_LEN];
  47. int x, y, pen_down;
  48. int z1, z2, rt = 0;
  49. int ret;
  50. ret = pm860x_bulk_read(touch->i2c, MEAS_TSIX_1, MEAS_LEN, buf);
  51. if (ret < 0)
  52. goto out;
  53. pen_down = buf[1] & (1 << 6);
  54. x = ((buf[0] & 0xFF) << 4) | (buf[1] & 0x0F);
  55. y = ((buf[2] & 0xFF) << 4) | (buf[3] & 0x0F);
  56. z1 = ((buf[4] & 0xFF) << 4) | (buf[5] & 0x0F);
  57. z2 = ((buf[6] & 0xFF) << 4) | (buf[7] & 0x0F);
  58. if (pen_down) {
  59. if ((x != 0) && (z1 != 0) && (touch->res_x != 0)) {
  60. rt = z2 / z1 - 1;
  61. rt = (rt * touch->res_x * x) >> ACCURATE_BIT;
  62. dev_dbg(chip->dev, "z1:%d, z2:%d, rt:%d\n",
  63. z1, z2, rt);
  64. }
  65. input_report_abs(touch->idev, ABS_X, x);
  66. input_report_abs(touch->idev, ABS_Y, y);
  67. input_report_abs(touch->idev, ABS_PRESSURE, rt);
  68. input_report_key(touch->idev, BTN_TOUCH, 1);
  69. dev_dbg(chip->dev, "pen down at [%d, %d].\n", x, y);
  70. } else {
  71. input_report_abs(touch->idev, ABS_PRESSURE, 0);
  72. input_report_key(touch->idev, BTN_TOUCH, 0);
  73. dev_dbg(chip->dev, "pen release\n");
  74. }
  75. input_sync(touch->idev);
  76. out:
  77. return IRQ_HANDLED;
  78. }
  79. static int pm860x_touch_open(struct input_dev *dev)
  80. {
  81. struct pm860x_touch *touch = input_get_drvdata(dev);
  82. int data, ret;
  83. data = MEAS_PD_EN | MEAS_TSIX_EN | MEAS_TSIY_EN
  84. | MEAS_TSIZ1_EN | MEAS_TSIZ2_EN;
  85. ret = pm860x_set_bits(touch->i2c, MEAS_EN3, data, data);
  86. if (ret < 0)
  87. goto out;
  88. return 0;
  89. out:
  90. return ret;
  91. }
  92. static void pm860x_touch_close(struct input_dev *dev)
  93. {
  94. struct pm860x_touch *touch = input_get_drvdata(dev);
  95. int data;
  96. data = MEAS_PD_EN | MEAS_TSIX_EN | MEAS_TSIY_EN
  97. | MEAS_TSIZ1_EN | MEAS_TSIZ2_EN;
  98. pm860x_set_bits(touch->i2c, MEAS_EN3, data, 0);
  99. }
  100. static int __devinit pm860x_touch_probe(struct platform_device *pdev)
  101. {
  102. struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent);
  103. struct pm860x_platform_data *pm860x_pdata = \
  104. pdev->dev.parent->platform_data;
  105. struct pm860x_touch_pdata *pdata = NULL;
  106. struct pm860x_touch *touch;
  107. int irq, ret;
  108. irq = platform_get_irq(pdev, 0);
  109. if (irq < 0) {
  110. dev_err(&pdev->dev, "No IRQ resource!\n");
  111. return -EINVAL;
  112. }
  113. if (!pm860x_pdata) {
  114. dev_err(&pdev->dev, "platform data is missing\n");
  115. return -EINVAL;
  116. }
  117. pdata = pm860x_pdata->touch;
  118. if (!pdata) {
  119. dev_err(&pdev->dev, "touchscreen data is missing\n");
  120. return -EINVAL;
  121. }
  122. touch = kzalloc(sizeof(struct pm860x_touch), GFP_KERNEL);
  123. if (touch == NULL)
  124. return -ENOMEM;
  125. dev_set_drvdata(&pdev->dev, touch);
  126. touch->idev = input_allocate_device();
  127. if (touch->idev == NULL) {
  128. dev_err(&pdev->dev, "Failed to allocate input device!\n");
  129. ret = -ENOMEM;
  130. goto out;
  131. }
  132. touch->idev->name = "88pm860x-touch";
  133. touch->idev->phys = "88pm860x/input0";
  134. touch->idev->id.bustype = BUS_I2C;
  135. touch->idev->dev.parent = &pdev->dev;
  136. touch->idev->open = pm860x_touch_open;
  137. touch->idev->close = pm860x_touch_close;
  138. touch->chip = chip;
  139. touch->i2c = (chip->id == CHIP_PM8607) ? chip->client : chip->companion;
  140. touch->irq = irq + chip->irq_base;
  141. touch->res_x = pdata->res_x;
  142. input_set_drvdata(touch->idev, touch);
  143. ret = request_threaded_irq(touch->irq, NULL, pm860x_touch_handler,
  144. IRQF_ONESHOT, "touch", touch);
  145. if (ret < 0)
  146. goto out_irq;
  147. __set_bit(EV_ABS, touch->idev->evbit);
  148. __set_bit(ABS_X, touch->idev->absbit);
  149. __set_bit(ABS_Y, touch->idev->absbit);
  150. __set_bit(ABS_PRESSURE, touch->idev->absbit);
  151. __set_bit(EV_SYN, touch->idev->evbit);
  152. __set_bit(EV_KEY, touch->idev->evbit);
  153. __set_bit(BTN_TOUCH, touch->idev->keybit);
  154. input_set_abs_params(touch->idev, ABS_X, 0, 1 << ACCURATE_BIT, 0, 0);
  155. input_set_abs_params(touch->idev, ABS_Y, 0, 1 << ACCURATE_BIT, 0, 0);
  156. input_set_abs_params(touch->idev, ABS_PRESSURE, 0, 1 << ACCURATE_BIT,
  157. 0, 0);
  158. ret = input_register_device(touch->idev);
  159. if (ret < 0) {
  160. dev_err(chip->dev, "Failed to register touch!\n");
  161. goto out_rg;
  162. }
  163. platform_set_drvdata(pdev, touch);
  164. return 0;
  165. out_rg:
  166. free_irq(touch->irq, touch);
  167. out_irq:
  168. input_free_device(touch->idev);
  169. out:
  170. kfree(touch);
  171. return ret;
  172. }
  173. static int __devexit pm860x_touch_remove(struct platform_device *pdev)
  174. {
  175. struct pm860x_touch *touch = platform_get_drvdata(pdev);
  176. input_unregister_device(touch->idev);
  177. free_irq(touch->irq, touch);
  178. platform_set_drvdata(pdev, NULL);
  179. kfree(touch);
  180. return 0;
  181. }
  182. static struct platform_driver pm860x_touch_driver = {
  183. .driver = {
  184. .name = "88pm860x-touch",
  185. .owner = THIS_MODULE,
  186. },
  187. .probe = pm860x_touch_probe,
  188. .remove = __devexit_p(pm860x_touch_remove),
  189. };
  190. static int __init pm860x_touch_init(void)
  191. {
  192. return platform_driver_register(&pm860x_touch_driver);
  193. }
  194. module_init(pm860x_touch_init);
  195. static void __exit pm860x_touch_exit(void)
  196. {
  197. platform_driver_unregister(&pm860x_touch_driver);
  198. }
  199. module_exit(pm860x_touch_exit);
  200. MODULE_DESCRIPTION("Touchscreen driver for Marvell Semiconductor 88PM860x");
  201. MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
  202. MODULE_LICENSE("GPL");
  203. MODULE_ALIAS("platform:88pm860x-touch");