88pm860x-ts.c 5.9 KB

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