88pm860x-ts.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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 pm860x_touch_dt_init(struct platform_device *pdev,
  104. struct pm860x_chip *chip,
  105. int *res_x)
  106. {
  107. struct device_node *np = pdev->dev.parent->of_node;
  108. struct i2c_client *i2c = (chip->id == CHIP_PM8607) ? chip->client \
  109. : chip->companion;
  110. int data, n, ret;
  111. if (!np)
  112. return -ENODEV;
  113. np = of_find_node_by_name(np, "touch");
  114. if (!np) {
  115. dev_err(&pdev->dev, "Can't find touch node\n");
  116. return -EINVAL;
  117. }
  118. /* set GPADC MISC1 register */
  119. data = 0;
  120. if (!of_property_read_u32(np, "marvell,88pm860x-gpadc-prebias", &n))
  121. data |= (n << 1) & PM8607_GPADC_PREBIAS_MASK;
  122. if (!of_property_read_u32(np, "marvell,88pm860x-gpadc-slot-cycle", &n))
  123. data |= (n << 3) & PM8607_GPADC_SLOT_CYCLE_MASK;
  124. if (!of_property_read_u32(np, "marvell,88pm860x-gpadc-off-scale", &n))
  125. data |= (n << 5) & PM8607_GPADC_OFF_SCALE_MASK;
  126. if (!of_property_read_u32(np, "marvell,88pm860x-gpadc-sw-cal", &n))
  127. data |= (n << 7) & PM8607_GPADC_SW_CAL_MASK;
  128. if (data) {
  129. ret = pm860x_reg_write(i2c, PM8607_GPADC_MISC1, data);
  130. if (ret < 0)
  131. return -EINVAL;
  132. }
  133. /* set tsi prebias time */
  134. if (!of_property_read_u32(np, "marvell,88pm860x-tsi-prebias", &data)) {
  135. ret = pm860x_reg_write(i2c, PM8607_TSI_PREBIAS, data);
  136. if (ret < 0)
  137. return -EINVAL;
  138. }
  139. /* set prebias & prechg time of pen detect */
  140. data = 0;
  141. if (!of_property_read_u32(np, "marvell,88pm860x-pen-prebias", &n))
  142. data |= n & PM8607_PD_PREBIAS_MASK;
  143. if (!of_property_read_u32(np, "marvell,88pm860x-pen-prechg", &n))
  144. data |= n & PM8607_PD_PRECHG_MASK;
  145. if (data) {
  146. ret = pm860x_reg_write(i2c, PM8607_PD_PREBIAS, data);
  147. if (ret < 0)
  148. return -EINVAL;
  149. }
  150. of_property_read_u32(np, "marvell,88pm860x-resistor-X", res_x);
  151. return 0;
  152. }
  153. #else
  154. #define pm860x_touch_dt_init(x, y, z) (-1)
  155. #endif
  156. static int pm860x_touch_probe(struct platform_device *pdev)
  157. {
  158. struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent);
  159. struct pm860x_touch_pdata *pdata = pdev->dev.platform_data;
  160. struct pm860x_touch *touch;
  161. struct i2c_client *i2c = (chip->id == CHIP_PM8607) ? chip->client \
  162. : chip->companion;
  163. int irq, ret, res_x = 0, data = 0;
  164. irq = platform_get_irq(pdev, 0);
  165. if (irq < 0) {
  166. dev_err(&pdev->dev, "No IRQ resource!\n");
  167. return -EINVAL;
  168. }
  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 = kzalloc(sizeof(struct pm860x_touch), GFP_KERNEL);
  219. if (touch == NULL)
  220. return -ENOMEM;
  221. dev_set_drvdata(&pdev->dev, touch);
  222. touch->idev = input_allocate_device();
  223. if (touch->idev == NULL) {
  224. dev_err(&pdev->dev, "Failed to allocate input device!\n");
  225. ret = -ENOMEM;
  226. goto out;
  227. }
  228. touch->idev->name = "88pm860x-touch";
  229. touch->idev->phys = "88pm860x/input0";
  230. touch->idev->id.bustype = BUS_I2C;
  231. touch->idev->dev.parent = &pdev->dev;
  232. touch->idev->open = pm860x_touch_open;
  233. touch->idev->close = pm860x_touch_close;
  234. touch->chip = chip;
  235. touch->i2c = i2c;
  236. touch->irq = irq;
  237. touch->res_x = res_x;
  238. input_set_drvdata(touch->idev, touch);
  239. ret = request_threaded_irq(touch->irq, NULL, pm860x_touch_handler,
  240. IRQF_ONESHOT, "touch", touch);
  241. if (ret < 0)
  242. goto out_irq;
  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. goto out_rg;
  258. }
  259. platform_set_drvdata(pdev, touch);
  260. return 0;
  261. out_rg:
  262. free_irq(touch->irq, touch);
  263. out_irq:
  264. input_free_device(touch->idev);
  265. out:
  266. kfree(touch);
  267. return ret;
  268. }
  269. static int pm860x_touch_remove(struct platform_device *pdev)
  270. {
  271. struct pm860x_touch *touch = platform_get_drvdata(pdev);
  272. input_unregister_device(touch->idev);
  273. free_irq(touch->irq, touch);
  274. platform_set_drvdata(pdev, NULL);
  275. kfree(touch);
  276. return 0;
  277. }
  278. static struct platform_driver pm860x_touch_driver = {
  279. .driver = {
  280. .name = "88pm860x-touch",
  281. .owner = THIS_MODULE,
  282. },
  283. .probe = pm860x_touch_probe,
  284. .remove = pm860x_touch_remove,
  285. };
  286. module_platform_driver(pm860x_touch_driver);
  287. MODULE_DESCRIPTION("Touchscreen driver for Marvell Semiconductor 88PM860x");
  288. MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
  289. MODULE_LICENSE("GPL");
  290. MODULE_ALIAS("platform:88pm860x-touch");