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