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