88pm860x-ts.c 6.2 KB

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