Todd Fischer | 7525996 | 2010-04-05 17:53:12 -0600 | [diff] [blame] | 1 | /* |
Todd Fischer | 7525996 | 2010-04-05 17:53:12 -0600 | [diff] [blame] | 2 | * Touchscreen driver for the tps6507x chip. |
| 3 | * |
| 4 | * Copyright (c) 2009 RidgeRun (todd.fischer@ridgerun.com) |
| 5 | * |
| 6 | * Credits: |
| 7 | * |
| 8 | * Using code from tsc2007, MtekVision Co., Ltd. |
| 9 | * |
| 10 | * For licencing details see kernel-base/COPYING |
| 11 | * |
| 12 | * TPS65070, TPS65073, TPS650731, and TPS650732 support |
| 13 | * 10 bit touch screen interface. |
| 14 | */ |
| 15 | |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/workqueue.h> |
| 18 | #include <linux/slab.h> |
| 19 | #include <linux/input.h> |
Dmitry Torokhov | 5705b8a | 2013-04-15 11:11:00 -0700 | [diff] [blame] | 20 | #include <linux/input-polldev.h> |
Todd Fischer | 7525996 | 2010-04-05 17:53:12 -0600 | [diff] [blame] | 21 | #include <linux/platform_device.h> |
| 22 | #include <linux/mfd/tps6507x.h> |
| 23 | #include <linux/input/tps6507x-ts.h> |
| 24 | #include <linux/delay.h> |
| 25 | |
| 26 | #define TSC_DEFAULT_POLL_PERIOD 30 /* ms */ |
| 27 | #define TPS_DEFAULT_MIN_PRESSURE 0x30 |
| 28 | #define MAX_10BIT ((1 << 10) - 1) |
| 29 | |
| 30 | #define TPS6507X_ADCONFIG_CONVERT_TS (TPS6507X_ADCONFIG_AD_ENABLE | \ |
| 31 | TPS6507X_ADCONFIG_START_CONVERSION | \ |
| 32 | TPS6507X_ADCONFIG_INPUT_REAL_TSC) |
| 33 | #define TPS6507X_ADCONFIG_POWER_DOWN_TS (TPS6507X_ADCONFIG_INPUT_REAL_TSC) |
| 34 | |
| 35 | struct ts_event { |
| 36 | u16 x; |
| 37 | u16 y; |
| 38 | u16 pressure; |
| 39 | }; |
| 40 | |
| 41 | struct tps6507x_ts { |
Todd Fischer | 7525996 | 2010-04-05 17:53:12 -0600 | [diff] [blame] | 42 | struct device *dev; |
Dmitry Torokhov | 5705b8a | 2013-04-15 11:11:00 -0700 | [diff] [blame] | 43 | struct input_polled_dev *poll_dev; |
Todd Fischer | 7525996 | 2010-04-05 17:53:12 -0600 | [diff] [blame] | 44 | struct tps6507x_dev *mfd; |
Dmitry Torokhov | 5705b8a | 2013-04-15 11:11:00 -0700 | [diff] [blame] | 45 | char phys[32]; |
| 46 | struct ts_event tc; |
Dmitry Torokhov | 02a7160 | 2013-04-15 11:01:21 -0700 | [diff] [blame] | 47 | u16 min_pressure; |
Dmitry Torokhov | 02a7160 | 2013-04-15 11:01:21 -0700 | [diff] [blame] | 48 | bool pendown; |
Todd Fischer | 7525996 | 2010-04-05 17:53:12 -0600 | [diff] [blame] | 49 | }; |
| 50 | |
| 51 | static int tps6507x_read_u8(struct tps6507x_ts *tsc, u8 reg, u8 *data) |
| 52 | { |
Javier Martinez Canillas | e5e66ed | 2015-10-02 11:19:54 -0700 | [diff] [blame] | 53 | return tsc->mfd->read_dev(tsc->mfd, reg, 1, data); |
Todd Fischer | 7525996 | 2010-04-05 17:53:12 -0600 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | static int tps6507x_write_u8(struct tps6507x_ts *tsc, u8 reg, u8 data) |
| 57 | { |
| 58 | return tsc->mfd->write_dev(tsc->mfd, reg, 1, &data); |
| 59 | } |
| 60 | |
| 61 | static s32 tps6507x_adc_conversion(struct tps6507x_ts *tsc, |
| 62 | u8 tsc_mode, u16 *value) |
| 63 | { |
| 64 | s32 ret; |
| 65 | u8 adc_status; |
| 66 | u8 result; |
| 67 | |
| 68 | /* Route input signal to A/D converter */ |
| 69 | |
| 70 | ret = tps6507x_write_u8(tsc, TPS6507X_REG_TSCMODE, tsc_mode); |
| 71 | if (ret) { |
| 72 | dev_err(tsc->dev, "TSC mode read failed\n"); |
| 73 | goto err; |
| 74 | } |
| 75 | |
| 76 | /* Start A/D conversion */ |
| 77 | |
| 78 | ret = tps6507x_write_u8(tsc, TPS6507X_REG_ADCONFIG, |
| 79 | TPS6507X_ADCONFIG_CONVERT_TS); |
| 80 | if (ret) { |
| 81 | dev_err(tsc->dev, "ADC config write failed\n"); |
| 82 | return ret; |
| 83 | } |
| 84 | |
| 85 | do { |
| 86 | ret = tps6507x_read_u8(tsc, TPS6507X_REG_ADCONFIG, |
| 87 | &adc_status); |
| 88 | if (ret) { |
| 89 | dev_err(tsc->dev, "ADC config read failed\n"); |
| 90 | goto err; |
| 91 | } |
| 92 | } while (adc_status & TPS6507X_ADCONFIG_START_CONVERSION); |
| 93 | |
| 94 | ret = tps6507x_read_u8(tsc, TPS6507X_REG_ADRESULT_2, &result); |
| 95 | if (ret) { |
| 96 | dev_err(tsc->dev, "ADC result 2 read failed\n"); |
| 97 | goto err; |
| 98 | } |
| 99 | |
| 100 | *value = (result & TPS6507X_REG_ADRESULT_2_MASK) << 8; |
| 101 | |
| 102 | ret = tps6507x_read_u8(tsc, TPS6507X_REG_ADRESULT_1, &result); |
| 103 | if (ret) { |
| 104 | dev_err(tsc->dev, "ADC result 1 read failed\n"); |
| 105 | goto err; |
| 106 | } |
| 107 | |
| 108 | *value |= result; |
| 109 | |
| 110 | dev_dbg(tsc->dev, "TSC channel %d = 0x%X\n", tsc_mode, *value); |
| 111 | |
| 112 | err: |
| 113 | return ret; |
| 114 | } |
| 115 | |
| 116 | /* Need to call tps6507x_adc_standby() after using A/D converter for the |
| 117 | * touch screen interrupt to work properly. |
| 118 | */ |
| 119 | |
| 120 | static s32 tps6507x_adc_standby(struct tps6507x_ts *tsc) |
| 121 | { |
| 122 | s32 ret; |
| 123 | s32 loops = 0; |
| 124 | u8 val; |
| 125 | |
| 126 | ret = tps6507x_write_u8(tsc, TPS6507X_REG_ADCONFIG, |
| 127 | TPS6507X_ADCONFIG_INPUT_TSC); |
| 128 | if (ret) |
| 129 | return ret; |
| 130 | |
| 131 | ret = tps6507x_write_u8(tsc, TPS6507X_REG_TSCMODE, |
| 132 | TPS6507X_TSCMODE_STANDBY); |
| 133 | if (ret) |
| 134 | return ret; |
| 135 | |
| 136 | ret = tps6507x_read_u8(tsc, TPS6507X_REG_INT, &val); |
| 137 | if (ret) |
| 138 | return ret; |
| 139 | |
| 140 | while (val & TPS6507X_REG_TSC_INT) { |
| 141 | mdelay(10); |
| 142 | ret = tps6507x_read_u8(tsc, TPS6507X_REG_INT, &val); |
| 143 | if (ret) |
| 144 | return ret; |
| 145 | loops++; |
| 146 | } |
| 147 | |
| 148 | return ret; |
| 149 | } |
| 150 | |
Dmitry Torokhov | 5705b8a | 2013-04-15 11:11:00 -0700 | [diff] [blame] | 151 | static void tps6507x_ts_poll(struct input_polled_dev *poll_dev) |
Todd Fischer | 7525996 | 2010-04-05 17:53:12 -0600 | [diff] [blame] | 152 | { |
Dmitry Torokhov | 5705b8a | 2013-04-15 11:11:00 -0700 | [diff] [blame] | 153 | struct tps6507x_ts *tsc = poll_dev->private; |
| 154 | struct input_dev *input_dev = poll_dev->input; |
Dmitry Torokhov | 02a7160 | 2013-04-15 11:01:21 -0700 | [diff] [blame] | 155 | bool pendown; |
Todd Fischer | 7525996 | 2010-04-05 17:53:12 -0600 | [diff] [blame] | 156 | s32 ret; |
| 157 | |
Dmitry Torokhov | 02a7160 | 2013-04-15 11:01:21 -0700 | [diff] [blame] | 158 | ret = tps6507x_adc_conversion(tsc, TPS6507X_TSCMODE_PRESSURE, |
| 159 | &tsc->tc.pressure); |
Todd Fischer | 7525996 | 2010-04-05 17:53:12 -0600 | [diff] [blame] | 160 | if (ret) |
| 161 | goto done; |
| 162 | |
| 163 | pendown = tsc->tc.pressure > tsc->min_pressure; |
| 164 | |
| 165 | if (unlikely(!pendown && tsc->pendown)) { |
| 166 | dev_dbg(tsc->dev, "UP\n"); |
| 167 | input_report_key(input_dev, BTN_TOUCH, 0); |
| 168 | input_report_abs(input_dev, ABS_PRESSURE, 0); |
| 169 | input_sync(input_dev); |
Dmitry Torokhov | 02a7160 | 2013-04-15 11:01:21 -0700 | [diff] [blame] | 170 | tsc->pendown = false; |
Todd Fischer | 7525996 | 2010-04-05 17:53:12 -0600 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | if (pendown) { |
| 174 | |
| 175 | if (!tsc->pendown) { |
| 176 | dev_dbg(tsc->dev, "DOWN\n"); |
| 177 | input_report_key(input_dev, BTN_TOUCH, 1); |
| 178 | } else |
| 179 | dev_dbg(tsc->dev, "still down\n"); |
| 180 | |
| 181 | ret = tps6507x_adc_conversion(tsc, TPS6507X_TSCMODE_X_POSITION, |
| 182 | &tsc->tc.x); |
| 183 | if (ret) |
| 184 | goto done; |
| 185 | |
| 186 | ret = tps6507x_adc_conversion(tsc, TPS6507X_TSCMODE_Y_POSITION, |
| 187 | &tsc->tc.y); |
| 188 | if (ret) |
| 189 | goto done; |
| 190 | |
| 191 | input_report_abs(input_dev, ABS_X, tsc->tc.x); |
| 192 | input_report_abs(input_dev, ABS_Y, tsc->tc.y); |
| 193 | input_report_abs(input_dev, ABS_PRESSURE, tsc->tc.pressure); |
| 194 | input_sync(input_dev); |
Dmitry Torokhov | 02a7160 | 2013-04-15 11:01:21 -0700 | [diff] [blame] | 195 | tsc->pendown = true; |
Todd Fischer | 7525996 | 2010-04-05 17:53:12 -0600 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | done: |
Dmitry Torokhov | 5705b8a | 2013-04-15 11:11:00 -0700 | [diff] [blame] | 199 | tps6507x_adc_standby(tsc); |
Todd Fischer | 7525996 | 2010-04-05 17:53:12 -0600 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | static int tps6507x_ts_probe(struct platform_device *pdev) |
| 203 | { |
Todd Fischer | 7525996 | 2010-04-05 17:53:12 -0600 | [diff] [blame] | 204 | struct tps6507x_dev *tps6507x_dev = dev_get_drvdata(pdev->dev.parent); |
Dmitry Torokhov | 5705b8a | 2013-04-15 11:11:00 -0700 | [diff] [blame] | 205 | const struct tps6507x_board *tps_board; |
| 206 | const struct touchscreen_init_data *init_data; |
| 207 | struct tps6507x_ts *tsc; |
| 208 | struct input_polled_dev *poll_dev; |
Todd Fischer | 7525996 | 2010-04-05 17:53:12 -0600 | [diff] [blame] | 209 | struct input_dev *input_dev; |
Dmitry Torokhov | 5705b8a | 2013-04-15 11:11:00 -0700 | [diff] [blame] | 210 | int error; |
Todd Fischer | 7525996 | 2010-04-05 17:53:12 -0600 | [diff] [blame] | 211 | |
Dmitry Torokhov | 5705b8a | 2013-04-15 11:11:00 -0700 | [diff] [blame] | 212 | /* |
Todd Fischer | 7525996 | 2010-04-05 17:53:12 -0600 | [diff] [blame] | 213 | * tps_board points to pmic related constants |
| 214 | * coming from the board-evm file. |
| 215 | */ |
Dmitry Torokhov | 5705b8a | 2013-04-15 11:11:00 -0700 | [diff] [blame] | 216 | tps_board = dev_get_platdata(tps6507x_dev->dev); |
Todd Fischer | 7525996 | 2010-04-05 17:53:12 -0600 | [diff] [blame] | 217 | if (!tps_board) { |
| 218 | dev_err(tps6507x_dev->dev, |
| 219 | "Could not find tps6507x platform data\n"); |
Dmitry Torokhov | 5705b8a | 2013-04-15 11:11:00 -0700 | [diff] [blame] | 220 | return -ENODEV; |
Todd Fischer | 7525996 | 2010-04-05 17:53:12 -0600 | [diff] [blame] | 221 | } |
| 222 | |
Dmitry Torokhov | 5705b8a | 2013-04-15 11:11:00 -0700 | [diff] [blame] | 223 | /* |
Todd Fischer | 7525996 | 2010-04-05 17:53:12 -0600 | [diff] [blame] | 224 | * init_data points to array of regulator_init structures |
| 225 | * coming from the board-evm file. |
| 226 | */ |
Todd Fischer | 7525996 | 2010-04-05 17:53:12 -0600 | [diff] [blame] | 227 | init_data = tps_board->tps6507x_ts_init_data; |
| 228 | |
| 229 | tsc = kzalloc(sizeof(struct tps6507x_ts), GFP_KERNEL); |
| 230 | if (!tsc) { |
| 231 | dev_err(tps6507x_dev->dev, "failed to allocate driver data\n"); |
Dmitry Torokhov | 5705b8a | 2013-04-15 11:11:00 -0700 | [diff] [blame] | 232 | return -ENOMEM; |
Todd Fischer | 7525996 | 2010-04-05 17:53:12 -0600 | [diff] [blame] | 233 | } |
| 234 | |
Todd Fischer | 7525996 | 2010-04-05 17:53:12 -0600 | [diff] [blame] | 235 | tsc->mfd = tps6507x_dev; |
| 236 | tsc->dev = tps6507x_dev->dev; |
Dmitry Torokhov | 5705b8a | 2013-04-15 11:11:00 -0700 | [diff] [blame] | 237 | tsc->min_pressure = init_data ? |
| 238 | init_data->min_pressure : TPS_DEFAULT_MIN_PRESSURE; |
| 239 | |
| 240 | snprintf(tsc->phys, sizeof(tsc->phys), |
| 241 | "%s/input0", dev_name(tsc->dev)); |
| 242 | |
| 243 | poll_dev = input_allocate_polled_device(); |
| 244 | if (!poll_dev) { |
| 245 | dev_err(tsc->dev, "Failed to allocate polled input device.\n"); |
Todd Fischer | 7525996 | 2010-04-05 17:53:12 -0600 | [diff] [blame] | 246 | error = -ENOMEM; |
Dmitry Torokhov | 5705b8a | 2013-04-15 11:11:00 -0700 | [diff] [blame] | 247 | goto err_free_mem; |
Todd Fischer | 7525996 | 2010-04-05 17:53:12 -0600 | [diff] [blame] | 248 | } |
| 249 | |
Dmitry Torokhov | 5705b8a | 2013-04-15 11:11:00 -0700 | [diff] [blame] | 250 | tsc->poll_dev = poll_dev; |
| 251 | |
| 252 | poll_dev->private = tsc; |
| 253 | poll_dev->poll = tps6507x_ts_poll; |
| 254 | poll_dev->poll_interval = init_data ? |
| 255 | init_data->poll_period : TSC_DEFAULT_POLL_PERIOD; |
| 256 | |
| 257 | input_dev = poll_dev->input; |
Todd Fischer | 7525996 | 2010-04-05 17:53:12 -0600 | [diff] [blame] | 258 | input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS); |
| 259 | input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH); |
| 260 | |
| 261 | input_set_abs_params(input_dev, ABS_X, 0, MAX_10BIT, 0, 0); |
| 262 | input_set_abs_params(input_dev, ABS_Y, 0, MAX_10BIT, 0, 0); |
| 263 | input_set_abs_params(input_dev, ABS_PRESSURE, 0, MAX_10BIT, 0, 0); |
| 264 | |
| 265 | input_dev->name = "TPS6507x Touchscreen"; |
Todd Fischer | 7525996 | 2010-04-05 17:53:12 -0600 | [diff] [blame] | 266 | input_dev->phys = tsc->phys; |
Dmitry Torokhov | 5705b8a | 2013-04-15 11:11:00 -0700 | [diff] [blame] | 267 | input_dev->dev.parent = tsc->dev; |
| 268 | input_dev->id.bustype = BUS_I2C; |
Todd Fischer | 7525996 | 2010-04-05 17:53:12 -0600 | [diff] [blame] | 269 | if (init_data) { |
Todd Fischer | 7525996 | 2010-04-05 17:53:12 -0600 | [diff] [blame] | 270 | input_dev->id.vendor = init_data->vendor; |
| 271 | input_dev->id.product = init_data->product; |
| 272 | input_dev->id.version = init_data->version; |
Todd Fischer | 7525996 | 2010-04-05 17:53:12 -0600 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | error = tps6507x_adc_standby(tsc); |
| 276 | if (error) |
Dmitry Torokhov | 5705b8a | 2013-04-15 11:11:00 -0700 | [diff] [blame] | 277 | goto err_free_polled_dev; |
Todd Fischer | 7525996 | 2010-04-05 17:53:12 -0600 | [diff] [blame] | 278 | |
Dmitry Torokhov | 5705b8a | 2013-04-15 11:11:00 -0700 | [diff] [blame] | 279 | error = input_register_polled_device(poll_dev); |
Todd Fischer | 7525996 | 2010-04-05 17:53:12 -0600 | [diff] [blame] | 280 | if (error) |
Dmitry Torokhov | 5705b8a | 2013-04-15 11:11:00 -0700 | [diff] [blame] | 281 | goto err_free_polled_dev; |
Todd Fischer | 7525996 | 2010-04-05 17:53:12 -0600 | [diff] [blame] | 282 | |
Dmitry Torokhov | 5705b8a | 2013-04-15 11:11:00 -0700 | [diff] [blame] | 283 | platform_set_drvdata(pdev, tsc); |
Todd Fischer | 7525996 | 2010-04-05 17:53:12 -0600 | [diff] [blame] | 284 | |
| 285 | return 0; |
| 286 | |
Dmitry Torokhov | 5705b8a | 2013-04-15 11:11:00 -0700 | [diff] [blame] | 287 | err_free_polled_dev: |
| 288 | input_free_polled_device(poll_dev); |
| 289 | err_free_mem: |
Todd Fischer | 7525996 | 2010-04-05 17:53:12 -0600 | [diff] [blame] | 290 | kfree(tsc); |
Todd Fischer | 7525996 | 2010-04-05 17:53:12 -0600 | [diff] [blame] | 291 | return error; |
| 292 | } |
| 293 | |
Bill Pemberton | e2619cf | 2012-11-23 21:50:47 -0800 | [diff] [blame] | 294 | static int tps6507x_ts_remove(struct platform_device *pdev) |
Todd Fischer | 7525996 | 2010-04-05 17:53:12 -0600 | [diff] [blame] | 295 | { |
Dmitry Torokhov | 5705b8a | 2013-04-15 11:11:00 -0700 | [diff] [blame] | 296 | struct tps6507x_ts *tsc = platform_get_drvdata(pdev); |
| 297 | struct input_polled_dev *poll_dev = tsc->poll_dev; |
Todd Fischer | 7525996 | 2010-04-05 17:53:12 -0600 | [diff] [blame] | 298 | |
Dmitry Torokhov | 5705b8a | 2013-04-15 11:11:00 -0700 | [diff] [blame] | 299 | input_unregister_polled_device(poll_dev); |
| 300 | input_free_polled_device(poll_dev); |
Todd Fischer | 7525996 | 2010-04-05 17:53:12 -0600 | [diff] [blame] | 301 | |
Todd Fischer | 7525996 | 2010-04-05 17:53:12 -0600 | [diff] [blame] | 302 | kfree(tsc); |
| 303 | |
| 304 | return 0; |
| 305 | } |
| 306 | |
| 307 | static struct platform_driver tps6507x_ts_driver = { |
| 308 | .driver = { |
| 309 | .name = "tps6507x-ts", |
Todd Fischer | 7525996 | 2010-04-05 17:53:12 -0600 | [diff] [blame] | 310 | }, |
| 311 | .probe = tps6507x_ts_probe, |
Bill Pemberton | 1cb0aa8 | 2012-11-23 21:27:39 -0800 | [diff] [blame] | 312 | .remove = tps6507x_ts_remove, |
Todd Fischer | 7525996 | 2010-04-05 17:53:12 -0600 | [diff] [blame] | 313 | }; |
JJ Ding | cdcc96e | 2011-11-29 11:14:13 -0800 | [diff] [blame] | 314 | module_platform_driver(tps6507x_ts_driver); |
Todd Fischer | 7525996 | 2010-04-05 17:53:12 -0600 | [diff] [blame] | 315 | |
| 316 | MODULE_AUTHOR("Todd Fischer <todd.fischer@ridgerun.com>"); |
| 317 | MODULE_DESCRIPTION("TPS6507x - TouchScreen driver"); |
| 318 | MODULE_LICENSE("GPL v2"); |
Uwe Kleine-König | 79026ff | 2012-04-03 09:33:58 -0700 | [diff] [blame] | 319 | MODULE_ALIAS("platform:tps6507x-ts"); |