88pm860x-ts.c 8.3 KB

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