88pm860x-ts.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Touchscreen driver for Marvell 88PM860x
  4. *
  5. * Copyright (C) 2009 Marvell International Ltd.
  6. * Haojian Zhuang <haojian.zhuang@marvell.com>
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/of.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/i2c.h>
  13. #include <linux/input.h>
  14. #include <linux/mfd/88pm860x.h>
  15. #include <linux/slab.h>
  16. #include <linux/device.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. #ifdef CONFIG_OF
  101. static int pm860x_touch_dt_init(struct platform_device *pdev,
  102. struct pm860x_chip *chip,
  103. int *res_x)
  104. {
  105. struct i2c_client *i2c = (chip->id == CHIP_PM8607) ? chip->client \
  106. : chip->companion;
  107. int data, n, ret;
  108. if (!pdev->dev.parent->of_node)
  109. return -ENODEV;
  110. struct device_node *np __free(device_node) =
  111. of_get_child_by_name(pdev->dev.parent->of_node, "touch");
  112. if (!np) {
  113. dev_err(&pdev->dev, "Can't find touch node\n");
  114. return -EINVAL;
  115. }
  116. /* set GPADC MISC1 register */
  117. data = 0;
  118. if (!of_property_read_u32(np, "marvell,88pm860x-gpadc-prebias", &n))
  119. data |= (n << 1) & PM8607_GPADC_PREBIAS_MASK;
  120. if (!of_property_read_u32(np, "marvell,88pm860x-gpadc-slot-cycle", &n))
  121. data |= (n << 3) & PM8607_GPADC_SLOT_CYCLE_MASK;
  122. if (!of_property_read_u32(np, "marvell,88pm860x-gpadc-off-scale", &n))
  123. data |= (n << 5) & PM8607_GPADC_OFF_SCALE_MASK;
  124. if (!of_property_read_u32(np, "marvell,88pm860x-gpadc-sw-cal", &n))
  125. data |= (n << 7) & PM8607_GPADC_SW_CAL_MASK;
  126. if (data) {
  127. ret = pm860x_reg_write(i2c, PM8607_GPADC_MISC1, data);
  128. if (ret < 0)
  129. return -EINVAL;
  130. }
  131. /* set tsi prebias time */
  132. if (!of_property_read_u32(np, "marvell,88pm860x-tsi-prebias", &data)) {
  133. ret = pm860x_reg_write(i2c, PM8607_TSI_PREBIAS, data);
  134. if (ret < 0)
  135. return -EINVAL;
  136. }
  137. /* set prebias & prechg time of pen detect */
  138. data = 0;
  139. if (!of_property_read_u32(np, "marvell,88pm860x-pen-prebias", &n))
  140. data |= n & PM8607_PD_PREBIAS_MASK;
  141. if (!of_property_read_u32(np, "marvell,88pm860x-pen-prechg", &n))
  142. data |= n & PM8607_PD_PRECHG_MASK;
  143. if (data) {
  144. ret = pm860x_reg_write(i2c, PM8607_PD_PREBIAS, data);
  145. if (ret < 0)
  146. return -EINVAL;
  147. }
  148. of_property_read_u32(np, "marvell,88pm860x-resistor-X", res_x);
  149. return 0;
  150. }
  151. #else
  152. #define pm860x_touch_dt_init(x, y, z) (-1)
  153. #endif
  154. static int pm860x_touch_probe(struct platform_device *pdev)
  155. {
  156. struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent);
  157. struct pm860x_touch_pdata *pdata = dev_get_platdata(&pdev->dev);
  158. struct pm860x_touch *touch;
  159. struct i2c_client *i2c = (chip->id == CHIP_PM8607) ? chip->client \
  160. : chip->companion;
  161. int irq, ret, res_x = 0, data = 0;
  162. irq = platform_get_irq(pdev, 0);
  163. if (irq < 0)
  164. return -EINVAL;
  165. if (pm860x_touch_dt_init(pdev, chip, &res_x)) {
  166. if (pdata) {
  167. /* set GPADC MISC1 register */
  168. data = 0;
  169. data |= (pdata->gpadc_prebias << 1)
  170. & PM8607_GPADC_PREBIAS_MASK;
  171. data |= (pdata->slot_cycle << 3)
  172. & PM8607_GPADC_SLOT_CYCLE_MASK;
  173. data |= (pdata->off_scale << 5)
  174. & PM8607_GPADC_OFF_SCALE_MASK;
  175. data |= (pdata->sw_cal << 7)
  176. & PM8607_GPADC_SW_CAL_MASK;
  177. if (data) {
  178. ret = pm860x_reg_write(i2c,
  179. PM8607_GPADC_MISC1, data);
  180. if (ret < 0)
  181. return -EINVAL;
  182. }
  183. /* set tsi prebias time */
  184. if (pdata->tsi_prebias) {
  185. data = pdata->tsi_prebias;
  186. ret = pm860x_reg_write(i2c,
  187. PM8607_TSI_PREBIAS, data);
  188. if (ret < 0)
  189. return -EINVAL;
  190. }
  191. /* set prebias & prechg time of pen detect */
  192. data = 0;
  193. data |= pdata->pen_prebias
  194. & PM8607_PD_PREBIAS_MASK;
  195. data |= (pdata->pen_prechg << 5)
  196. & PM8607_PD_PRECHG_MASK;
  197. if (data) {
  198. ret = pm860x_reg_write(i2c,
  199. PM8607_PD_PREBIAS, data);
  200. if (ret < 0)
  201. return -EINVAL;
  202. }
  203. res_x = pdata->res_x;
  204. } else {
  205. dev_err(&pdev->dev, "failed to get platform data\n");
  206. return -EINVAL;
  207. }
  208. }
  209. /* enable GPADC */
  210. ret = pm860x_set_bits(i2c, PM8607_GPADC_MISC1, PM8607_GPADC_EN,
  211. PM8607_GPADC_EN);
  212. if (ret)
  213. return ret;
  214. touch = devm_kzalloc(&pdev->dev, sizeof(struct pm860x_touch),
  215. GFP_KERNEL);
  216. if (!touch)
  217. return -ENOMEM;
  218. touch->idev = devm_input_allocate_device(&pdev->dev);
  219. if (!touch->idev) {
  220. dev_err(&pdev->dev, "Failed to allocate input device!\n");
  221. return -ENOMEM;
  222. }
  223. touch->idev->name = "88pm860x-touch";
  224. touch->idev->phys = "88pm860x/input0";
  225. touch->idev->id.bustype = BUS_I2C;
  226. touch->idev->dev.parent = &pdev->dev;
  227. touch->idev->open = pm860x_touch_open;
  228. touch->idev->close = pm860x_touch_close;
  229. touch->chip = chip;
  230. touch->i2c = i2c;
  231. touch->irq = irq;
  232. touch->res_x = res_x;
  233. input_set_drvdata(touch->idev, touch);
  234. ret = devm_request_threaded_irq(&pdev->dev, touch->irq, NULL,
  235. pm860x_touch_handler, IRQF_ONESHOT,
  236. "touch", touch);
  237. if (ret < 0)
  238. return ret;
  239. __set_bit(EV_ABS, touch->idev->evbit);
  240. __set_bit(ABS_X, touch->idev->absbit);
  241. __set_bit(ABS_Y, touch->idev->absbit);
  242. __set_bit(ABS_PRESSURE, touch->idev->absbit);
  243. __set_bit(EV_SYN, touch->idev->evbit);
  244. __set_bit(EV_KEY, touch->idev->evbit);
  245. __set_bit(BTN_TOUCH, touch->idev->keybit);
  246. input_set_abs_params(touch->idev, ABS_X, 0, 1 << ACCURATE_BIT, 0, 0);
  247. input_set_abs_params(touch->idev, ABS_Y, 0, 1 << ACCURATE_BIT, 0, 0);
  248. input_set_abs_params(touch->idev, ABS_PRESSURE, 0, 1 << ACCURATE_BIT,
  249. 0, 0);
  250. ret = input_register_device(touch->idev);
  251. if (ret < 0) {
  252. dev_err(chip->dev, "Failed to register touch!\n");
  253. return ret;
  254. }
  255. return 0;
  256. }
  257. static struct platform_driver pm860x_touch_driver = {
  258. .driver = {
  259. .name = "88pm860x-touch",
  260. },
  261. .probe = pm860x_touch_probe,
  262. };
  263. module_platform_driver(pm860x_touch_driver);
  264. MODULE_DESCRIPTION("Touchscreen driver for Marvell Semiconductor 88PM860x");
  265. MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
  266. MODULE_LICENSE("GPL");
  267. MODULE_ALIAS("platform:88pm860x-touch");