88pm860x-ts.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. #include <linux/device.h>
  20. #define MEAS_LEN (8)
  21. #define ACCURATE_BIT (12)
  22. /* touch register */
  23. #define MEAS_EN3 (0x52)
  24. #define MEAS_TSIX_1 (0x8D)
  25. #define MEAS_TSIX_2 (0x8E)
  26. #define MEAS_TSIY_1 (0x8F)
  27. #define MEAS_TSIY_2 (0x90)
  28. #define MEAS_TSIZ1_1 (0x91)
  29. #define MEAS_TSIZ1_2 (0x92)
  30. #define MEAS_TSIZ2_1 (0x93)
  31. #define MEAS_TSIZ2_2 (0x94)
  32. /* bit definitions of touch */
  33. #define MEAS_PD_EN (1 << 3)
  34. #define MEAS_TSIX_EN (1 << 4)
  35. #define MEAS_TSIY_EN (1 << 5)
  36. #define MEAS_TSIZ1_EN (1 << 6)
  37. #define MEAS_TSIZ2_EN (1 << 7)
  38. struct pm860x_touch {
  39. struct input_dev *idev;
  40. struct i2c_client *i2c;
  41. struct pm860x_chip *chip;
  42. int irq;
  43. int res_x; /* resistor of Xplate */
  44. };
  45. static irqreturn_t pm860x_touch_handler(int irq, void *data)
  46. {
  47. struct pm860x_touch *touch = data;
  48. struct pm860x_chip *chip = touch->chip;
  49. unsigned char buf[MEAS_LEN];
  50. int x, y, pen_down;
  51. int z1, z2, rt = 0;
  52. int ret;
  53. ret = pm860x_bulk_read(touch->i2c, MEAS_TSIX_1, MEAS_LEN, buf);
  54. if (ret < 0)
  55. goto out;
  56. pen_down = buf[1] & (1 << 6);
  57. x = ((buf[0] & 0xFF) << 4) | (buf[1] & 0x0F);
  58. y = ((buf[2] & 0xFF) << 4) | (buf[3] & 0x0F);
  59. z1 = ((buf[4] & 0xFF) << 4) | (buf[5] & 0x0F);
  60. z2 = ((buf[6] & 0xFF) << 4) | (buf[7] & 0x0F);
  61. if (pen_down) {
  62. if ((x != 0) && (z1 != 0) && (touch->res_x != 0)) {
  63. rt = z2 / z1 - 1;
  64. rt = (rt * touch->res_x * x) >> ACCURATE_BIT;
  65. dev_dbg(chip->dev, "z1:%d, z2:%d, rt:%d\n",
  66. z1, z2, rt);
  67. }
  68. input_report_abs(touch->idev, ABS_X, x);
  69. input_report_abs(touch->idev, ABS_Y, y);
  70. input_report_abs(touch->idev, ABS_PRESSURE, rt);
  71. input_report_key(touch->idev, BTN_TOUCH, 1);
  72. dev_dbg(chip->dev, "pen down at [%d, %d].\n", x, y);
  73. } else {
  74. input_report_abs(touch->idev, ABS_PRESSURE, 0);
  75. input_report_key(touch->idev, BTN_TOUCH, 0);
  76. dev_dbg(chip->dev, "pen release\n");
  77. }
  78. input_sync(touch->idev);
  79. out:
  80. return IRQ_HANDLED;
  81. }
  82. static int pm860x_touch_open(struct input_dev *dev)
  83. {
  84. struct pm860x_touch *touch = input_get_drvdata(dev);
  85. int data, ret;
  86. data = MEAS_PD_EN | MEAS_TSIX_EN | MEAS_TSIY_EN
  87. | MEAS_TSIZ1_EN | MEAS_TSIZ2_EN;
  88. ret = pm860x_set_bits(touch->i2c, MEAS_EN3, data, data);
  89. if (ret < 0)
  90. goto out;
  91. return 0;
  92. out:
  93. return ret;
  94. }
  95. static void pm860x_touch_close(struct input_dev *dev)
  96. {
  97. struct pm860x_touch *touch = input_get_drvdata(dev);
  98. int data;
  99. data = MEAS_PD_EN | MEAS_TSIX_EN | MEAS_TSIY_EN
  100. | MEAS_TSIZ1_EN | MEAS_TSIZ2_EN;
  101. pm860x_set_bits(touch->i2c, MEAS_EN3, data, 0);
  102. }
  103. #ifdef CONFIG_OF
  104. static int pm860x_touch_dt_init(struct platform_device *pdev,
  105. struct pm860x_chip *chip,
  106. int *res_x)
  107. {
  108. struct device_node *np = pdev->dev.parent->of_node;
  109. struct i2c_client *i2c = (chip->id == CHIP_PM8607) ? chip->client \
  110. : chip->companion;
  111. int data, n, ret;
  112. if (!np)
  113. return -ENODEV;
  114. np = of_find_node_by_name(np, "touch");
  115. if (!np) {
  116. dev_err(&pdev->dev, "Can't find touch node\n");
  117. return -EINVAL;
  118. }
  119. /* set GPADC MISC1 register */
  120. data = 0;
  121. if (!of_property_read_u32(np, "marvell,88pm860x-gpadc-prebias", &n))
  122. data |= (n << 1) & PM8607_GPADC_PREBIAS_MASK;
  123. if (!of_property_read_u32(np, "marvell,88pm860x-gpadc-slot-cycle", &n))
  124. data |= (n << 3) & PM8607_GPADC_SLOT_CYCLE_MASK;
  125. if (!of_property_read_u32(np, "marvell,88pm860x-gpadc-off-scale", &n))
  126. data |= (n << 5) & PM8607_GPADC_OFF_SCALE_MASK;
  127. if (!of_property_read_u32(np, "marvell,88pm860x-gpadc-sw-cal", &n))
  128. data |= (n << 7) & PM8607_GPADC_SW_CAL_MASK;
  129. if (data) {
  130. ret = pm860x_reg_write(i2c, PM8607_GPADC_MISC1, data);
  131. if (ret < 0)
  132. return -EINVAL;
  133. }
  134. /* set tsi prebias time */
  135. if (!of_property_read_u32(np, "marvell,88pm860x-tsi-prebias", &data)) {
  136. ret = pm860x_reg_write(i2c, PM8607_TSI_PREBIAS, data);
  137. if (ret < 0)
  138. return -EINVAL;
  139. }
  140. /* set prebias & prechg time of pen detect */
  141. data = 0;
  142. if (!of_property_read_u32(np, "marvell,88pm860x-pen-prebias", &n))
  143. data |= n & PM8607_PD_PREBIAS_MASK;
  144. if (!of_property_read_u32(np, "marvell,88pm860x-pen-prechg", &n))
  145. data |= n & PM8607_PD_PRECHG_MASK;
  146. if (data) {
  147. ret = pm860x_reg_write(i2c, PM8607_PD_PREBIAS, data);
  148. if (ret < 0)
  149. return -EINVAL;
  150. }
  151. of_property_read_u32(np, "marvell,88pm860x-resistor-X", res_x);
  152. return 0;
  153. }
  154. #else
  155. #define pm860x_touch_dt_init(x, y, z) (-1)
  156. #endif
  157. static int pm860x_touch_probe(struct platform_device *pdev)
  158. {
  159. struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent);
  160. struct pm860x_touch_pdata *pdata = dev_get_platdata(&pdev->dev);
  161. struct pm860x_touch *touch;
  162. struct i2c_client *i2c = (chip->id == CHIP_PM8607) ? chip->client \
  163. : chip->companion;
  164. int irq, ret, res_x = 0, data = 0;
  165. irq = platform_get_irq(pdev, 0);
  166. if (irq < 0) {
  167. dev_err(&pdev->dev, "No IRQ resource!\n");
  168. return -EINVAL;
  169. }
  170. if (pm860x_touch_dt_init(pdev, chip, &res_x)) {
  171. if (pdata) {
  172. /* set GPADC MISC1 register */
  173. data = 0;
  174. data |= (pdata->gpadc_prebias << 1)
  175. & PM8607_GPADC_PREBIAS_MASK;
  176. data |= (pdata->slot_cycle << 3)
  177. & PM8607_GPADC_SLOT_CYCLE_MASK;
  178. data |= (pdata->off_scale << 5)
  179. & PM8607_GPADC_OFF_SCALE_MASK;
  180. data |= (pdata->sw_cal << 7)
  181. & PM8607_GPADC_SW_CAL_MASK;
  182. if (data) {
  183. ret = pm860x_reg_write(i2c,
  184. PM8607_GPADC_MISC1, data);
  185. if (ret < 0)
  186. return -EINVAL;
  187. }
  188. /* set tsi prebias time */
  189. if (pdata->tsi_prebias) {
  190. data = pdata->tsi_prebias;
  191. ret = pm860x_reg_write(i2c,
  192. PM8607_TSI_PREBIAS, data);
  193. if (ret < 0)
  194. return -EINVAL;
  195. }
  196. /* set prebias & prechg time of pen detect */
  197. data = 0;
  198. data |= pdata->pen_prebias
  199. & PM8607_PD_PREBIAS_MASK;
  200. data |= (pdata->pen_prechg << 5)
  201. & PM8607_PD_PRECHG_MASK;
  202. if (data) {
  203. ret = pm860x_reg_write(i2c,
  204. PM8607_PD_PREBIAS, data);
  205. if (ret < 0)
  206. return -EINVAL;
  207. }
  208. res_x = pdata->res_x;
  209. } else {
  210. dev_err(&pdev->dev, "failed to get platform data\n");
  211. return -EINVAL;
  212. }
  213. }
  214. /* enable GPADC */
  215. ret = pm860x_set_bits(i2c, PM8607_GPADC_MISC1, PM8607_GPADC_EN,
  216. PM8607_GPADC_EN);
  217. if (ret)
  218. return ret;
  219. touch = devm_kzalloc(&pdev->dev, sizeof(struct pm860x_touch),
  220. GFP_KERNEL);
  221. if (!touch)
  222. return -ENOMEM;
  223. platform_set_drvdata(pdev, touch);
  224. touch->idev = devm_input_allocate_device(&pdev->dev);
  225. if (!touch->idev) {
  226. dev_err(&pdev->dev, "Failed to allocate input device!\n");
  227. return -ENOMEM;
  228. }
  229. touch->idev->name = "88pm860x-touch";
  230. touch->idev->phys = "88pm860x/input0";
  231. touch->idev->id.bustype = BUS_I2C;
  232. touch->idev->dev.parent = &pdev->dev;
  233. touch->idev->open = pm860x_touch_open;
  234. touch->idev->close = pm860x_touch_close;
  235. touch->chip = chip;
  236. touch->i2c = i2c;
  237. touch->irq = irq;
  238. touch->res_x = res_x;
  239. input_set_drvdata(touch->idev, touch);
  240. ret = devm_request_threaded_irq(&pdev->dev, touch->irq, NULL,
  241. pm860x_touch_handler, IRQF_ONESHOT,
  242. "touch", touch);
  243. if (ret < 0)
  244. return ret;
  245. __set_bit(EV_ABS, touch->idev->evbit);
  246. __set_bit(ABS_X, touch->idev->absbit);
  247. __set_bit(ABS_Y, touch->idev->absbit);
  248. __set_bit(ABS_PRESSURE, touch->idev->absbit);
  249. __set_bit(EV_SYN, touch->idev->evbit);
  250. __set_bit(EV_KEY, touch->idev->evbit);
  251. __set_bit(BTN_TOUCH, touch->idev->keybit);
  252. input_set_abs_params(touch->idev, ABS_X, 0, 1 << ACCURATE_BIT, 0, 0);
  253. input_set_abs_params(touch->idev, ABS_Y, 0, 1 << ACCURATE_BIT, 0, 0);
  254. input_set_abs_params(touch->idev, ABS_PRESSURE, 0, 1 << ACCURATE_BIT,
  255. 0, 0);
  256. ret = input_register_device(touch->idev);
  257. if (ret < 0) {
  258. dev_err(chip->dev, "Failed to register touch!\n");
  259. return ret;
  260. }
  261. platform_set_drvdata(pdev, touch);
  262. return 0;
  263. }
  264. static struct platform_driver pm860x_touch_driver = {
  265. .driver = {
  266. .name = "88pm860x-touch",
  267. },
  268. .probe = pm860x_touch_probe,
  269. };
  270. module_platform_driver(pm860x_touch_driver);
  271. MODULE_DESCRIPTION("Touchscreen driver for Marvell Semiconductor 88PM860x");
  272. MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
  273. MODULE_LICENSE("GPL");
  274. MODULE_ALIAS("platform:88pm860x-touch");