88pm860x-ts.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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 <[email protected]>
  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 device_node *np = pdev->dev.parent->of_node;
  106. struct i2c_client *i2c = (chip->id == CHIP_PM8607) ? chip->client \
  107. : chip->companion;
  108. int data, n, ret;
  109. if (!np)
  110. return -ENODEV;
  111. np = of_get_child_by_name(np, "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. goto err_put_node;
  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. goto err_put_node;
  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. goto err_put_node;
  147. }
  148. of_property_read_u32(np, "marvell,88pm860x-resistor-X", res_x);
  149. of_node_put(np);
  150. return 0;
  151. err_put_node:
  152. of_node_put(np);
  153. return -EINVAL;
  154. }
  155. #else
  156. #define pm860x_touch_dt_init(x, y, z) (-1)
  157. #endif
  158. static int pm860x_touch_probe(struct platform_device *pdev)
  159. {
  160. struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent);
  161. struct pm860x_touch_pdata *pdata = dev_get_platdata(&pdev->dev);
  162. struct pm860x_touch *touch;
  163. struct i2c_client *i2c = (chip->id == CHIP_PM8607) ? chip->client \
  164. : chip->companion;
  165. int irq, ret, res_x = 0, data = 0;
  166. irq = platform_get_irq(pdev, 0);
  167. if (irq < 0)
  168. return -EINVAL;
  169. if (pm860x_touch_dt_init(pdev, chip, &res_x)) {
  170. if (pdata) {
  171. /* set GPADC MISC1 register */
  172. data = 0;
  173. data |= (pdata->gpadc_prebias << 1)
  174. & PM8607_GPADC_PREBIAS_MASK;
  175. data |= (pdata->slot_cycle << 3)
  176. & PM8607_GPADC_SLOT_CYCLE_MASK;
  177. data |= (pdata->off_scale << 5)
  178. & PM8607_GPADC_OFF_SCALE_MASK;
  179. data |= (pdata->sw_cal << 7)
  180. & PM8607_GPADC_SW_CAL_MASK;
  181. if (data) {
  182. ret = pm860x_reg_write(i2c,
  183. PM8607_GPADC_MISC1, data);
  184. if (ret < 0)
  185. return -EINVAL;
  186. }
  187. /* set tsi prebias time */
  188. if (pdata->tsi_prebias) {
  189. data = pdata->tsi_prebias;
  190. ret = pm860x_reg_write(i2c,
  191. PM8607_TSI_PREBIAS, data);
  192. if (ret < 0)
  193. return -EINVAL;
  194. }
  195. /* set prebias & prechg time of pen detect */
  196. data = 0;
  197. data |= pdata->pen_prebias
  198. & PM8607_PD_PREBIAS_MASK;
  199. data |= (pdata->pen_prechg << 5)
  200. & PM8607_PD_PRECHG_MASK;
  201. if (data) {
  202. ret = pm860x_reg_write(i2c,
  203. PM8607_PD_PREBIAS, data);
  204. if (ret < 0)
  205. return -EINVAL;
  206. }
  207. res_x = pdata->res_x;
  208. } else {
  209. dev_err(&pdev->dev, "failed to get platform data\n");
  210. return -EINVAL;
  211. }
  212. }
  213. /* enable GPADC */
  214. ret = pm860x_set_bits(i2c, PM8607_GPADC_MISC1, PM8607_GPADC_EN,
  215. PM8607_GPADC_EN);
  216. if (ret)
  217. return ret;
  218. touch = devm_kzalloc(&pdev->dev, sizeof(struct pm860x_touch),
  219. GFP_KERNEL);
  220. if (!touch)
  221. return -ENOMEM;
  222. touch->idev = devm_input_allocate_device(&pdev->dev);
  223. if (!touch->idev) {
  224. dev_err(&pdev->dev, "Failed to allocate input device!\n");
  225. return -ENOMEM;
  226. }
  227. touch->idev->name = "88pm860x-touch";
  228. touch->idev->phys = "88pm860x/input0";
  229. touch->idev->id.bustype = BUS_I2C;
  230. touch->idev->dev.parent = &pdev->dev;
  231. touch->idev->open = pm860x_touch_open;
  232. touch->idev->close = pm860x_touch_close;
  233. touch->chip = chip;
  234. touch->i2c = i2c;
  235. touch->irq = irq;
  236. touch->res_x = res_x;
  237. input_set_drvdata(touch->idev, touch);
  238. ret = devm_request_threaded_irq(&pdev->dev, touch->irq, NULL,
  239. pm860x_touch_handler, IRQF_ONESHOT,
  240. "touch", touch);
  241. if (ret < 0)
  242. return ret;
  243. __set_bit(EV_ABS, touch->idev->evbit);
  244. __set_bit(ABS_X, touch->idev->absbit);
  245. __set_bit(ABS_Y, touch->idev->absbit);
  246. __set_bit(ABS_PRESSURE, touch->idev->absbit);
  247. __set_bit(EV_SYN, touch->idev->evbit);
  248. __set_bit(EV_KEY, touch->idev->evbit);
  249. __set_bit(BTN_TOUCH, touch->idev->keybit);
  250. input_set_abs_params(touch->idev, ABS_X, 0, 1 << ACCURATE_BIT, 0, 0);
  251. input_set_abs_params(touch->idev, ABS_Y, 0, 1 << ACCURATE_BIT, 0, 0);
  252. input_set_abs_params(touch->idev, ABS_PRESSURE, 0, 1 << ACCURATE_BIT,
  253. 0, 0);
  254. ret = input_register_device(touch->idev);
  255. if (ret < 0) {
  256. dev_err(chip->dev, "Failed to register touch!\n");
  257. return ret;
  258. }
  259. return 0;
  260. }
  261. static struct platform_driver pm860x_touch_driver = {
  262. .driver = {
  263. .name = "88pm860x-touch",
  264. },
  265. .probe = pm860x_touch_probe,
  266. };
  267. module_platform_driver(pm860x_touch_driver);
  268. MODULE_DESCRIPTION("Touchscreen driver for Marvell Semiconductor 88PM860x");
  269. MODULE_AUTHOR("Haojian Zhuang <[email protected]>");
  270. MODULE_LICENSE("GPL");
  271. MODULE_ALIAS("platform:88pm860x-touch");