Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 Simon Budig, <simon.budig@kernelconcepts.de> |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 3 | * Daniel Wagener <daniel.wagener@kernelconcepts.de> (M09 firmware support) |
Lothar Waßmann | dac90dc2 | 2014-03-28 09:23:02 -0700 | [diff] [blame] | 4 | * Lothar Waßmann <LW@KARO-electronics.de> (DT support) |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 5 | * |
| 6 | * This software is licensed under the terms of the GNU General Public |
| 7 | * License version 2, as published by the Free Software Foundation, and |
| 8 | * may be copied, distributed, and modified under those terms. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public |
| 16 | * License along with this library; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | */ |
| 19 | |
| 20 | /* |
| 21 | * This is a driver for the EDT "Polytouch" family of touch controllers |
| 22 | * based on the FocalTech FT5x06 line of chips. |
| 23 | * |
| 24 | * Development of this driver has been sponsored by Glyn: |
| 25 | * http://www.glyn.com/Products/Displays |
| 26 | */ |
| 27 | |
| 28 | #include <linux/module.h> |
| 29 | #include <linux/ratelimit.h> |
| 30 | #include <linux/interrupt.h> |
| 31 | #include <linux/input.h> |
| 32 | #include <linux/i2c.h> |
| 33 | #include <linux/uaccess.h> |
| 34 | #include <linux/delay.h> |
| 35 | #include <linux/debugfs.h> |
| 36 | #include <linux/slab.h> |
Franklin S Cooper Jr | 13c23cd | 2015-09-11 17:30:34 -0700 | [diff] [blame] | 37 | #include <linux/gpio/consumer.h> |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 38 | #include <linux/input/mt.h> |
Maxime Ripard | 2c00559 | 2015-03-21 20:18:06 -0700 | [diff] [blame] | 39 | #include <linux/input/touchscreen.h> |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 40 | |
| 41 | #define MAX_SUPPORT_POINTS 5 |
| 42 | |
| 43 | #define WORK_REGISTER_THRESHOLD 0x00 |
| 44 | #define WORK_REGISTER_REPORT_RATE 0x08 |
| 45 | #define WORK_REGISTER_GAIN 0x30 |
| 46 | #define WORK_REGISTER_OFFSET 0x31 |
| 47 | #define WORK_REGISTER_NUM_X 0x33 |
| 48 | #define WORK_REGISTER_NUM_Y 0x34 |
| 49 | |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 50 | #define M09_REGISTER_THRESHOLD 0x80 |
| 51 | #define M09_REGISTER_GAIN 0x92 |
| 52 | #define M09_REGISTER_OFFSET 0x93 |
| 53 | #define M09_REGISTER_NUM_X 0x94 |
| 54 | #define M09_REGISTER_NUM_Y 0x95 |
| 55 | |
| 56 | #define NO_REGISTER 0xff |
| 57 | |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 58 | #define WORK_REGISTER_OPMODE 0x3c |
| 59 | #define FACTORY_REGISTER_OPMODE 0x01 |
| 60 | |
| 61 | #define TOUCH_EVENT_DOWN 0x00 |
| 62 | #define TOUCH_EVENT_UP 0x01 |
| 63 | #define TOUCH_EVENT_ON 0x02 |
| 64 | #define TOUCH_EVENT_RESERVED 0x03 |
| 65 | |
| 66 | #define EDT_NAME_LEN 23 |
| 67 | #define EDT_SWITCH_MODE_RETRIES 10 |
| 68 | #define EDT_SWITCH_MODE_DELAY 5 /* msec */ |
| 69 | #define EDT_RAW_DATA_RETRIES 100 |
| 70 | #define EDT_RAW_DATA_DELAY 1 /* msec */ |
| 71 | |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 72 | enum edt_ver { |
| 73 | M06, |
| 74 | M09, |
| 75 | }; |
| 76 | |
| 77 | struct edt_reg_addr { |
| 78 | int reg_threshold; |
| 79 | int reg_report_rate; |
| 80 | int reg_gain; |
| 81 | int reg_offset; |
| 82 | int reg_num_x; |
| 83 | int reg_num_y; |
| 84 | }; |
| 85 | |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 86 | struct edt_ft5x06_ts_data { |
| 87 | struct i2c_client *client; |
| 88 | struct input_dev *input; |
| 89 | u16 num_x; |
| 90 | u16 num_y; |
| 91 | |
Franklin S Cooper Jr | 13c23cd | 2015-09-11 17:30:34 -0700 | [diff] [blame] | 92 | struct gpio_desc *reset_gpio; |
| 93 | struct gpio_desc *wake_gpio; |
Lothar Waßmann | dac90dc2 | 2014-03-28 09:23:02 -0700 | [diff] [blame] | 94 | |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 95 | #if defined(CONFIG_DEBUG_FS) |
| 96 | struct dentry *debug_dir; |
| 97 | u8 *raw_buffer; |
| 98 | size_t raw_bufsize; |
| 99 | #endif |
| 100 | |
| 101 | struct mutex mutex; |
| 102 | bool factory_mode; |
| 103 | int threshold; |
| 104 | int gain; |
| 105 | int offset; |
| 106 | int report_rate; |
| 107 | |
| 108 | char name[EDT_NAME_LEN]; |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 109 | |
| 110 | struct edt_reg_addr reg_addr; |
| 111 | enum edt_ver version; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 112 | }; |
| 113 | |
| 114 | static int edt_ft5x06_ts_readwrite(struct i2c_client *client, |
| 115 | u16 wr_len, u8 *wr_buf, |
| 116 | u16 rd_len, u8 *rd_buf) |
| 117 | { |
| 118 | struct i2c_msg wrmsg[2]; |
| 119 | int i = 0; |
| 120 | int ret; |
| 121 | |
| 122 | if (wr_len) { |
| 123 | wrmsg[i].addr = client->addr; |
| 124 | wrmsg[i].flags = 0; |
| 125 | wrmsg[i].len = wr_len; |
| 126 | wrmsg[i].buf = wr_buf; |
| 127 | i++; |
| 128 | } |
| 129 | if (rd_len) { |
| 130 | wrmsg[i].addr = client->addr; |
| 131 | wrmsg[i].flags = I2C_M_RD; |
| 132 | wrmsg[i].len = rd_len; |
| 133 | wrmsg[i].buf = rd_buf; |
| 134 | i++; |
| 135 | } |
| 136 | |
| 137 | ret = i2c_transfer(client->adapter, wrmsg, i); |
| 138 | if (ret < 0) |
| 139 | return ret; |
| 140 | if (ret != i) |
| 141 | return -EIO; |
| 142 | |
| 143 | return 0; |
| 144 | } |
| 145 | |
| 146 | static bool edt_ft5x06_ts_check_crc(struct edt_ft5x06_ts_data *tsdata, |
| 147 | u8 *buf, int buflen) |
| 148 | { |
| 149 | int i; |
| 150 | u8 crc = 0; |
| 151 | |
| 152 | for (i = 0; i < buflen - 1; i++) |
| 153 | crc ^= buf[i]; |
| 154 | |
| 155 | if (crc != buf[buflen-1]) { |
| 156 | dev_err_ratelimited(&tsdata->client->dev, |
| 157 | "crc error: 0x%02x expected, got 0x%02x\n", |
| 158 | crc, buf[buflen-1]); |
| 159 | return false; |
| 160 | } |
| 161 | |
| 162 | return true; |
| 163 | } |
| 164 | |
| 165 | static irqreturn_t edt_ft5x06_ts_isr(int irq, void *dev_id) |
| 166 | { |
| 167 | struct edt_ft5x06_ts_data *tsdata = dev_id; |
| 168 | struct device *dev = &tsdata->client->dev; |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 169 | u8 cmd; |
| 170 | u8 rdbuf[29]; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 171 | int i, type, x, y, id; |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 172 | int offset, tplen, datalen; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 173 | int error; |
| 174 | |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 175 | switch (tsdata->version) { |
| 176 | case M06: |
| 177 | cmd = 0xf9; /* tell the controller to send touch data */ |
| 178 | offset = 5; /* where the actual touch data starts */ |
| 179 | tplen = 4; /* data comes in so called frames */ |
| 180 | datalen = 26; /* how much bytes to listen for */ |
| 181 | break; |
| 182 | |
| 183 | case M09: |
| 184 | cmd = 0x02; |
| 185 | offset = 1; |
| 186 | tplen = 6; |
| 187 | datalen = 29; |
| 188 | break; |
| 189 | |
| 190 | default: |
| 191 | goto out; |
| 192 | } |
| 193 | |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 194 | memset(rdbuf, 0, sizeof(rdbuf)); |
| 195 | |
| 196 | error = edt_ft5x06_ts_readwrite(tsdata->client, |
| 197 | sizeof(cmd), &cmd, |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 198 | datalen, rdbuf); |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 199 | if (error) { |
| 200 | dev_err_ratelimited(dev, "Unable to fetch data, error: %d\n", |
| 201 | error); |
| 202 | goto out; |
| 203 | } |
| 204 | |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 205 | /* M09 does not send header or CRC */ |
| 206 | if (tsdata->version == M06) { |
| 207 | if (rdbuf[0] != 0xaa || rdbuf[1] != 0xaa || |
| 208 | rdbuf[2] != datalen) { |
| 209 | dev_err_ratelimited(dev, |
| 210 | "Unexpected header: %02x%02x%02x!\n", |
| 211 | rdbuf[0], rdbuf[1], rdbuf[2]); |
| 212 | goto out; |
| 213 | } |
| 214 | |
| 215 | if (!edt_ft5x06_ts_check_crc(tsdata, rdbuf, datalen)) |
| 216 | goto out; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 217 | } |
| 218 | |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 219 | for (i = 0; i < MAX_SUPPORT_POINTS; i++) { |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 220 | u8 *buf = &rdbuf[i * tplen + offset]; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 221 | bool down; |
| 222 | |
| 223 | type = buf[0] >> 6; |
| 224 | /* ignore Reserved events */ |
| 225 | if (type == TOUCH_EVENT_RESERVED) |
| 226 | continue; |
| 227 | |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 228 | /* M06 sometimes sends bogus coordinates in TOUCH_DOWN */ |
| 229 | if (tsdata->version == M06 && type == TOUCH_EVENT_DOWN) |
Lothar Waßmann | ee3e946 | 2014-03-28 09:28:17 -0700 | [diff] [blame] | 230 | continue; |
| 231 | |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 232 | x = ((buf[0] << 8) | buf[1]) & 0x0fff; |
| 233 | y = ((buf[2] << 8) | buf[3]) & 0x0fff; |
| 234 | id = (buf[2] >> 4) & 0x0f; |
Lothar Waßmann | 1730d81 | 2014-03-28 09:20:08 -0700 | [diff] [blame] | 235 | down = type != TOUCH_EVENT_UP; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 236 | |
| 237 | input_mt_slot(tsdata->input, id); |
| 238 | input_mt_report_slot_state(tsdata->input, MT_TOOL_FINGER, down); |
| 239 | |
| 240 | if (!down) |
| 241 | continue; |
| 242 | |
| 243 | input_report_abs(tsdata->input, ABS_MT_POSITION_X, x); |
| 244 | input_report_abs(tsdata->input, ABS_MT_POSITION_Y, y); |
| 245 | } |
| 246 | |
| 247 | input_mt_report_pointer_emulation(tsdata->input, true); |
| 248 | input_sync(tsdata->input); |
| 249 | |
| 250 | out: |
| 251 | return IRQ_HANDLED; |
| 252 | } |
| 253 | |
| 254 | static int edt_ft5x06_register_write(struct edt_ft5x06_ts_data *tsdata, |
| 255 | u8 addr, u8 value) |
| 256 | { |
| 257 | u8 wrbuf[4]; |
| 258 | |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 259 | switch (tsdata->version) { |
| 260 | case M06: |
| 261 | wrbuf[0] = tsdata->factory_mode ? 0xf3 : 0xfc; |
| 262 | wrbuf[1] = tsdata->factory_mode ? addr & 0x7f : addr & 0x3f; |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 263 | wrbuf[2] = value; |
| 264 | wrbuf[3] = wrbuf[0] ^ wrbuf[1] ^ wrbuf[2]; |
| 265 | return edt_ft5x06_ts_readwrite(tsdata->client, 4, |
| 266 | wrbuf, 0, NULL); |
| 267 | case M09: |
| 268 | wrbuf[0] = addr; |
| 269 | wrbuf[1] = value; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 270 | |
Robert Woerle | cc071ac | 2014-06-07 22:20:23 -0700 | [diff] [blame] | 271 | return edt_ft5x06_ts_readwrite(tsdata->client, 2, |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 272 | wrbuf, 0, NULL); |
| 273 | |
| 274 | default: |
| 275 | return -EINVAL; |
| 276 | } |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | static int edt_ft5x06_register_read(struct edt_ft5x06_ts_data *tsdata, |
| 280 | u8 addr) |
| 281 | { |
| 282 | u8 wrbuf[2], rdbuf[2]; |
| 283 | int error; |
| 284 | |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 285 | switch (tsdata->version) { |
| 286 | case M06: |
| 287 | wrbuf[0] = tsdata->factory_mode ? 0xf3 : 0xfc; |
| 288 | wrbuf[1] = tsdata->factory_mode ? addr & 0x7f : addr & 0x3f; |
| 289 | wrbuf[1] |= tsdata->factory_mode ? 0x80 : 0x40; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 290 | |
Dan Carpenter | e2c3ecf | 2014-04-03 09:17:05 -0700 | [diff] [blame] | 291 | error = edt_ft5x06_ts_readwrite(tsdata->client, 2, wrbuf, 2, |
| 292 | rdbuf); |
| 293 | if (error) |
| 294 | return error; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 295 | |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 296 | if ((wrbuf[0] ^ wrbuf[1] ^ rdbuf[0]) != rdbuf[1]) { |
| 297 | dev_err(&tsdata->client->dev, |
| 298 | "crc error: 0x%02x expected, got 0x%02x\n", |
| 299 | wrbuf[0] ^ wrbuf[1] ^ rdbuf[0], |
| 300 | rdbuf[1]); |
| 301 | return -EIO; |
| 302 | } |
| 303 | break; |
| 304 | |
| 305 | case M09: |
| 306 | wrbuf[0] = addr; |
| 307 | error = edt_ft5x06_ts_readwrite(tsdata->client, 1, |
| 308 | wrbuf, 1, rdbuf); |
| 309 | if (error) |
| 310 | return error; |
| 311 | break; |
| 312 | |
| 313 | default: |
| 314 | return -EINVAL; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | return rdbuf[0]; |
| 318 | } |
| 319 | |
| 320 | struct edt_ft5x06_attribute { |
| 321 | struct device_attribute dattr; |
| 322 | size_t field_offset; |
| 323 | u8 limit_low; |
| 324 | u8 limit_high; |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 325 | u8 addr_m06; |
| 326 | u8 addr_m09; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 327 | }; |
| 328 | |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 329 | #define EDT_ATTR(_field, _mode, _addr_m06, _addr_m09, \ |
| 330 | _limit_low, _limit_high) \ |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 331 | struct edt_ft5x06_attribute edt_ft5x06_attr_##_field = { \ |
| 332 | .dattr = __ATTR(_field, _mode, \ |
| 333 | edt_ft5x06_setting_show, \ |
| 334 | edt_ft5x06_setting_store), \ |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 335 | .field_offset = offsetof(struct edt_ft5x06_ts_data, _field), \ |
| 336 | .addr_m06 = _addr_m06, \ |
| 337 | .addr_m09 = _addr_m09, \ |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 338 | .limit_low = _limit_low, \ |
| 339 | .limit_high = _limit_high, \ |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | static ssize_t edt_ft5x06_setting_show(struct device *dev, |
| 343 | struct device_attribute *dattr, |
| 344 | char *buf) |
| 345 | { |
| 346 | struct i2c_client *client = to_i2c_client(dev); |
| 347 | struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client); |
| 348 | struct edt_ft5x06_attribute *attr = |
| 349 | container_of(dattr, struct edt_ft5x06_attribute, dattr); |
Lothar Waßmann | 1730d81 | 2014-03-28 09:20:08 -0700 | [diff] [blame] | 350 | u8 *field = (u8 *)tsdata + attr->field_offset; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 351 | int val; |
| 352 | size_t count = 0; |
| 353 | int error = 0; |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 354 | u8 addr; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 355 | |
| 356 | mutex_lock(&tsdata->mutex); |
| 357 | |
| 358 | if (tsdata->factory_mode) { |
| 359 | error = -EIO; |
| 360 | goto out; |
| 361 | } |
| 362 | |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 363 | switch (tsdata->version) { |
| 364 | case M06: |
| 365 | addr = attr->addr_m06; |
| 366 | break; |
| 367 | |
| 368 | case M09: |
| 369 | addr = attr->addr_m09; |
| 370 | break; |
| 371 | |
| 372 | default: |
| 373 | error = -ENODEV; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 374 | goto out; |
| 375 | } |
| 376 | |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 377 | if (addr != NO_REGISTER) { |
| 378 | val = edt_ft5x06_register_read(tsdata, addr); |
| 379 | if (val < 0) { |
| 380 | error = val; |
| 381 | dev_err(&tsdata->client->dev, |
| 382 | "Failed to fetch attribute %s, error %d\n", |
| 383 | dattr->attr.name, error); |
| 384 | goto out; |
| 385 | } |
| 386 | } else { |
| 387 | val = *field; |
| 388 | } |
| 389 | |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 390 | if (val != *field) { |
| 391 | dev_warn(&tsdata->client->dev, |
| 392 | "%s: read (%d) and stored value (%d) differ\n", |
| 393 | dattr->attr.name, val, *field); |
| 394 | *field = val; |
| 395 | } |
| 396 | |
| 397 | count = scnprintf(buf, PAGE_SIZE, "%d\n", val); |
| 398 | out: |
| 399 | mutex_unlock(&tsdata->mutex); |
| 400 | return error ?: count; |
| 401 | } |
| 402 | |
| 403 | static ssize_t edt_ft5x06_setting_store(struct device *dev, |
| 404 | struct device_attribute *dattr, |
| 405 | const char *buf, size_t count) |
| 406 | { |
| 407 | struct i2c_client *client = to_i2c_client(dev); |
| 408 | struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client); |
| 409 | struct edt_ft5x06_attribute *attr = |
| 410 | container_of(dattr, struct edt_ft5x06_attribute, dattr); |
Lothar Waßmann | 1730d81 | 2014-03-28 09:20:08 -0700 | [diff] [blame] | 411 | u8 *field = (u8 *)tsdata + attr->field_offset; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 412 | unsigned int val; |
| 413 | int error; |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 414 | u8 addr; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 415 | |
| 416 | mutex_lock(&tsdata->mutex); |
| 417 | |
| 418 | if (tsdata->factory_mode) { |
| 419 | error = -EIO; |
| 420 | goto out; |
| 421 | } |
| 422 | |
| 423 | error = kstrtouint(buf, 0, &val); |
| 424 | if (error) |
| 425 | goto out; |
| 426 | |
| 427 | if (val < attr->limit_low || val > attr->limit_high) { |
| 428 | error = -ERANGE; |
| 429 | goto out; |
| 430 | } |
| 431 | |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 432 | switch (tsdata->version) { |
| 433 | case M06: |
| 434 | addr = attr->addr_m06; |
| 435 | break; |
| 436 | |
| 437 | case M09: |
| 438 | addr = attr->addr_m09; |
| 439 | break; |
| 440 | |
| 441 | default: |
| 442 | error = -ENODEV; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 443 | goto out; |
| 444 | } |
| 445 | |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 446 | if (addr != NO_REGISTER) { |
| 447 | error = edt_ft5x06_register_write(tsdata, addr, val); |
| 448 | if (error) { |
| 449 | dev_err(&tsdata->client->dev, |
| 450 | "Failed to update attribute %s, error: %d\n", |
| 451 | dattr->attr.name, error); |
| 452 | goto out; |
| 453 | } |
| 454 | } |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 455 | *field = val; |
| 456 | |
| 457 | out: |
| 458 | mutex_unlock(&tsdata->mutex); |
| 459 | return error ?: count; |
| 460 | } |
| 461 | |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 462 | static EDT_ATTR(gain, S_IWUSR | S_IRUGO, WORK_REGISTER_GAIN, |
| 463 | M09_REGISTER_GAIN, 0, 31); |
| 464 | static EDT_ATTR(offset, S_IWUSR | S_IRUGO, WORK_REGISTER_OFFSET, |
| 465 | M09_REGISTER_OFFSET, 0, 31); |
| 466 | static EDT_ATTR(threshold, S_IWUSR | S_IRUGO, WORK_REGISTER_THRESHOLD, |
| 467 | M09_REGISTER_THRESHOLD, 20, 80); |
| 468 | static EDT_ATTR(report_rate, S_IWUSR | S_IRUGO, WORK_REGISTER_REPORT_RATE, |
| 469 | NO_REGISTER, 3, 14); |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 470 | |
| 471 | static struct attribute *edt_ft5x06_attrs[] = { |
| 472 | &edt_ft5x06_attr_gain.dattr.attr, |
| 473 | &edt_ft5x06_attr_offset.dattr.attr, |
| 474 | &edt_ft5x06_attr_threshold.dattr.attr, |
| 475 | &edt_ft5x06_attr_report_rate.dattr.attr, |
| 476 | NULL |
| 477 | }; |
| 478 | |
| 479 | static const struct attribute_group edt_ft5x06_attr_group = { |
| 480 | .attrs = edt_ft5x06_attrs, |
| 481 | }; |
| 482 | |
| 483 | #ifdef CONFIG_DEBUG_FS |
| 484 | static int edt_ft5x06_factory_mode(struct edt_ft5x06_ts_data *tsdata) |
| 485 | { |
| 486 | struct i2c_client *client = tsdata->client; |
| 487 | int retries = EDT_SWITCH_MODE_RETRIES; |
| 488 | int ret; |
| 489 | int error; |
| 490 | |
| 491 | disable_irq(client->irq); |
| 492 | |
| 493 | if (!tsdata->raw_buffer) { |
| 494 | tsdata->raw_bufsize = tsdata->num_x * tsdata->num_y * |
| 495 | sizeof(u16); |
| 496 | tsdata->raw_buffer = kzalloc(tsdata->raw_bufsize, GFP_KERNEL); |
| 497 | if (!tsdata->raw_buffer) { |
| 498 | error = -ENOMEM; |
| 499 | goto err_out; |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | /* mode register is 0x3c when in the work mode */ |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 504 | if (tsdata->version == M09) |
| 505 | goto m09_out; |
| 506 | |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 507 | error = edt_ft5x06_register_write(tsdata, WORK_REGISTER_OPMODE, 0x03); |
| 508 | if (error) { |
| 509 | dev_err(&client->dev, |
| 510 | "failed to switch to factory mode, error %d\n", error); |
| 511 | goto err_out; |
| 512 | } |
| 513 | |
| 514 | tsdata->factory_mode = true; |
| 515 | do { |
| 516 | mdelay(EDT_SWITCH_MODE_DELAY); |
| 517 | /* mode register is 0x01 when in factory mode */ |
| 518 | ret = edt_ft5x06_register_read(tsdata, FACTORY_REGISTER_OPMODE); |
| 519 | if (ret == 0x03) |
| 520 | break; |
| 521 | } while (--retries > 0); |
| 522 | |
| 523 | if (retries == 0) { |
| 524 | dev_err(&client->dev, "not in factory mode after %dms.\n", |
| 525 | EDT_SWITCH_MODE_RETRIES * EDT_SWITCH_MODE_DELAY); |
| 526 | error = -EIO; |
| 527 | goto err_out; |
| 528 | } |
| 529 | |
| 530 | return 0; |
| 531 | |
| 532 | err_out: |
| 533 | kfree(tsdata->raw_buffer); |
| 534 | tsdata->raw_buffer = NULL; |
| 535 | tsdata->factory_mode = false; |
| 536 | enable_irq(client->irq); |
| 537 | |
| 538 | return error; |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 539 | |
| 540 | m09_out: |
| 541 | dev_err(&client->dev, "No factory mode support for M09\n"); |
| 542 | return -EINVAL; |
| 543 | |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | static int edt_ft5x06_work_mode(struct edt_ft5x06_ts_data *tsdata) |
| 547 | { |
| 548 | struct i2c_client *client = tsdata->client; |
| 549 | int retries = EDT_SWITCH_MODE_RETRIES; |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 550 | struct edt_reg_addr *reg_addr = &tsdata->reg_addr; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 551 | int ret; |
| 552 | int error; |
| 553 | |
| 554 | /* mode register is 0x01 when in the factory mode */ |
| 555 | error = edt_ft5x06_register_write(tsdata, FACTORY_REGISTER_OPMODE, 0x1); |
| 556 | if (error) { |
| 557 | dev_err(&client->dev, |
| 558 | "failed to switch to work mode, error: %d\n", error); |
| 559 | return error; |
| 560 | } |
| 561 | |
| 562 | tsdata->factory_mode = false; |
| 563 | |
| 564 | do { |
| 565 | mdelay(EDT_SWITCH_MODE_DELAY); |
| 566 | /* mode register is 0x01 when in factory mode */ |
| 567 | ret = edt_ft5x06_register_read(tsdata, WORK_REGISTER_OPMODE); |
| 568 | if (ret == 0x01) |
| 569 | break; |
| 570 | } while (--retries > 0); |
| 571 | |
| 572 | if (retries == 0) { |
| 573 | dev_err(&client->dev, "not in work mode after %dms.\n", |
| 574 | EDT_SWITCH_MODE_RETRIES * EDT_SWITCH_MODE_DELAY); |
| 575 | tsdata->factory_mode = true; |
| 576 | return -EIO; |
| 577 | } |
| 578 | |
Sachin Kamat | 8efcc50 | 2013-03-28 01:14:42 -0700 | [diff] [blame] | 579 | kfree(tsdata->raw_buffer); |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 580 | tsdata->raw_buffer = NULL; |
| 581 | |
| 582 | /* restore parameters */ |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 583 | edt_ft5x06_register_write(tsdata, reg_addr->reg_threshold, |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 584 | tsdata->threshold); |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 585 | edt_ft5x06_register_write(tsdata, reg_addr->reg_gain, |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 586 | tsdata->gain); |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 587 | edt_ft5x06_register_write(tsdata, reg_addr->reg_offset, |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 588 | tsdata->offset); |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 589 | if (reg_addr->reg_report_rate) |
| 590 | edt_ft5x06_register_write(tsdata, reg_addr->reg_report_rate, |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 591 | tsdata->report_rate); |
| 592 | |
| 593 | enable_irq(client->irq); |
| 594 | |
| 595 | return 0; |
| 596 | } |
| 597 | |
| 598 | static int edt_ft5x06_debugfs_mode_get(void *data, u64 *mode) |
| 599 | { |
| 600 | struct edt_ft5x06_ts_data *tsdata = data; |
| 601 | |
| 602 | *mode = tsdata->factory_mode; |
| 603 | |
| 604 | return 0; |
| 605 | }; |
| 606 | |
| 607 | static int edt_ft5x06_debugfs_mode_set(void *data, u64 mode) |
| 608 | { |
| 609 | struct edt_ft5x06_ts_data *tsdata = data; |
| 610 | int retval = 0; |
| 611 | |
| 612 | if (mode > 1) |
| 613 | return -ERANGE; |
| 614 | |
| 615 | mutex_lock(&tsdata->mutex); |
| 616 | |
| 617 | if (mode != tsdata->factory_mode) { |
| 618 | retval = mode ? edt_ft5x06_factory_mode(tsdata) : |
Lothar Waßmann | 1730d81 | 2014-03-28 09:20:08 -0700 | [diff] [blame] | 619 | edt_ft5x06_work_mode(tsdata); |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | mutex_unlock(&tsdata->mutex); |
| 623 | |
| 624 | return retval; |
| 625 | }; |
| 626 | |
| 627 | DEFINE_SIMPLE_ATTRIBUTE(debugfs_mode_fops, edt_ft5x06_debugfs_mode_get, |
| 628 | edt_ft5x06_debugfs_mode_set, "%llu\n"); |
| 629 | |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 630 | static ssize_t edt_ft5x06_debugfs_raw_data_read(struct file *file, |
| 631 | char __user *buf, size_t count, loff_t *off) |
| 632 | { |
| 633 | struct edt_ft5x06_ts_data *tsdata = file->private_data; |
| 634 | struct i2c_client *client = tsdata->client; |
| 635 | int retries = EDT_RAW_DATA_RETRIES; |
| 636 | int val, i, error; |
| 637 | size_t read = 0; |
| 638 | int colbytes; |
| 639 | char wrbuf[3]; |
| 640 | u8 *rdbuf; |
| 641 | |
| 642 | if (*off < 0 || *off >= tsdata->raw_bufsize) |
| 643 | return 0; |
| 644 | |
| 645 | mutex_lock(&tsdata->mutex); |
| 646 | |
| 647 | if (!tsdata->factory_mode || !tsdata->raw_buffer) { |
| 648 | error = -EIO; |
| 649 | goto out; |
| 650 | } |
| 651 | |
| 652 | error = edt_ft5x06_register_write(tsdata, 0x08, 0x01); |
| 653 | if (error) { |
| 654 | dev_dbg(&client->dev, |
| 655 | "failed to write 0x08 register, error %d\n", error); |
| 656 | goto out; |
| 657 | } |
| 658 | |
| 659 | do { |
| 660 | msleep(EDT_RAW_DATA_DELAY); |
| 661 | val = edt_ft5x06_register_read(tsdata, 0x08); |
| 662 | if (val < 1) |
| 663 | break; |
| 664 | } while (--retries > 0); |
| 665 | |
| 666 | if (val < 0) { |
| 667 | error = val; |
| 668 | dev_dbg(&client->dev, |
| 669 | "failed to read 0x08 register, error %d\n", error); |
| 670 | goto out; |
| 671 | } |
| 672 | |
| 673 | if (retries == 0) { |
| 674 | dev_dbg(&client->dev, |
| 675 | "timed out waiting for register to settle\n"); |
| 676 | error = -ETIMEDOUT; |
| 677 | goto out; |
| 678 | } |
| 679 | |
| 680 | rdbuf = tsdata->raw_buffer; |
| 681 | colbytes = tsdata->num_y * sizeof(u16); |
| 682 | |
| 683 | wrbuf[0] = 0xf5; |
| 684 | wrbuf[1] = 0x0e; |
| 685 | for (i = 0; i < tsdata->num_x; i++) { |
| 686 | wrbuf[2] = i; /* column index */ |
| 687 | error = edt_ft5x06_ts_readwrite(tsdata->client, |
| 688 | sizeof(wrbuf), wrbuf, |
| 689 | colbytes, rdbuf); |
| 690 | if (error) |
| 691 | goto out; |
| 692 | |
| 693 | rdbuf += colbytes; |
| 694 | } |
| 695 | |
| 696 | read = min_t(size_t, count, tsdata->raw_bufsize - *off); |
Axel Lin | 35b1da4 | 2012-09-19 15:56:23 -0700 | [diff] [blame] | 697 | if (copy_to_user(buf, tsdata->raw_buffer + *off, read)) { |
| 698 | error = -EFAULT; |
| 699 | goto out; |
| 700 | } |
| 701 | |
| 702 | *off += read; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 703 | out: |
| 704 | mutex_unlock(&tsdata->mutex); |
| 705 | return error ?: read; |
| 706 | }; |
| 707 | |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 708 | static const struct file_operations debugfs_raw_data_fops = { |
Wei Yongjun | f6c0df6 | 2012-10-17 23:55:54 -0700 | [diff] [blame] | 709 | .open = simple_open, |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 710 | .read = edt_ft5x06_debugfs_raw_data_read, |
| 711 | }; |
| 712 | |
Bill Pemberton | 5298cc4 | 2012-11-23 21:38:25 -0800 | [diff] [blame] | 713 | static void |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 714 | edt_ft5x06_ts_prepare_debugfs(struct edt_ft5x06_ts_data *tsdata, |
| 715 | const char *debugfs_name) |
| 716 | { |
| 717 | tsdata->debug_dir = debugfs_create_dir(debugfs_name, NULL); |
| 718 | if (!tsdata->debug_dir) |
| 719 | return; |
| 720 | |
| 721 | debugfs_create_u16("num_x", S_IRUSR, tsdata->debug_dir, &tsdata->num_x); |
| 722 | debugfs_create_u16("num_y", S_IRUSR, tsdata->debug_dir, &tsdata->num_y); |
| 723 | |
| 724 | debugfs_create_file("mode", S_IRUSR | S_IWUSR, |
| 725 | tsdata->debug_dir, tsdata, &debugfs_mode_fops); |
| 726 | debugfs_create_file("raw_data", S_IRUSR, |
| 727 | tsdata->debug_dir, tsdata, &debugfs_raw_data_fops); |
| 728 | } |
| 729 | |
Bill Pemberton | e2619cf | 2012-11-23 21:50:47 -0800 | [diff] [blame] | 730 | static void |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 731 | edt_ft5x06_ts_teardown_debugfs(struct edt_ft5x06_ts_data *tsdata) |
| 732 | { |
Fabian Frederick | f69c6ec | 2014-07-09 09:45:57 -0700 | [diff] [blame] | 733 | debugfs_remove_recursive(tsdata->debug_dir); |
Guenter Roeck | a1d0fa7 | 2012-08-21 22:01:45 -0700 | [diff] [blame] | 734 | kfree(tsdata->raw_buffer); |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | #else |
| 738 | |
| 739 | static inline void |
| 740 | edt_ft5x06_ts_prepare_debugfs(struct edt_ft5x06_ts_data *tsdata, |
| 741 | const char *debugfs_name) |
| 742 | { |
| 743 | } |
| 744 | |
| 745 | static inline void |
| 746 | edt_ft5x06_ts_teardown_debugfs(struct edt_ft5x06_ts_data *tsdata) |
| 747 | { |
| 748 | } |
| 749 | |
| 750 | #endif /* CONFIG_DEBUGFS */ |
| 751 | |
Bill Pemberton | 5298cc4 | 2012-11-23 21:38:25 -0800 | [diff] [blame] | 752 | static int edt_ft5x06_ts_identify(struct i2c_client *client, |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 753 | struct edt_ft5x06_ts_data *tsdata, |
| 754 | char *fw_version) |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 755 | { |
| 756 | u8 rdbuf[EDT_NAME_LEN]; |
| 757 | char *p; |
| 758 | int error; |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 759 | char *model_name = tsdata->name; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 760 | |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 761 | /* see what we find if we assume it is a M06 * |
| 762 | * if we get less than EDT_NAME_LEN, we don't want |
| 763 | * to have garbage in there |
| 764 | */ |
| 765 | memset(rdbuf, 0, sizeof(rdbuf)); |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 766 | error = edt_ft5x06_ts_readwrite(client, 1, "\xbb", |
| 767 | EDT_NAME_LEN - 1, rdbuf); |
| 768 | if (error) |
| 769 | return error; |
| 770 | |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 771 | /* if we find something consistent, stay with that assumption |
| 772 | * at least M09 won't send 3 bytes here |
| 773 | */ |
Rasmus Villemoes | 0f3ae5b | 2014-10-13 15:54:48 -0700 | [diff] [blame] | 774 | if (!(strncasecmp(rdbuf + 1, "EP0", 3))) { |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 775 | tsdata->version = M06; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 776 | |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 777 | /* remove last '$' end marker */ |
| 778 | rdbuf[EDT_NAME_LEN - 1] = '\0'; |
| 779 | if (rdbuf[EDT_NAME_LEN - 2] == '$') |
| 780 | rdbuf[EDT_NAME_LEN - 2] = '\0'; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 781 | |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 782 | /* look for Model/Version separator */ |
| 783 | p = strchr(rdbuf, '*'); |
| 784 | if (p) |
| 785 | *p++ = '\0'; |
| 786 | strlcpy(model_name, rdbuf + 1, EDT_NAME_LEN); |
| 787 | strlcpy(fw_version, p ? p : "", EDT_NAME_LEN); |
| 788 | } else { |
| 789 | /* since there are only two versions around (M06, M09) */ |
| 790 | tsdata->version = M09; |
| 791 | |
| 792 | error = edt_ft5x06_ts_readwrite(client, 1, "\xA6", |
| 793 | 2, rdbuf); |
| 794 | if (error) |
| 795 | return error; |
| 796 | |
| 797 | strlcpy(fw_version, rdbuf, 2); |
| 798 | |
| 799 | error = edt_ft5x06_ts_readwrite(client, 1, "\xA8", |
| 800 | 1, rdbuf); |
| 801 | if (error) |
| 802 | return error; |
| 803 | |
| 804 | snprintf(model_name, EDT_NAME_LEN, "EP0%i%i0M09", |
| 805 | rdbuf[0] >> 4, rdbuf[0] & 0x0F); |
| 806 | } |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 807 | |
| 808 | return 0; |
| 809 | } |
| 810 | |
Dmitry Torokhov | 2e23b7a | 2015-09-12 09:54:15 -0700 | [diff] [blame^] | 811 | static void edt_ft5x06_ts_get_defaults(struct device *dev, |
Dmitry Torokhov | 22ddbac | 2015-09-12 09:37:03 -0700 | [diff] [blame] | 812 | struct edt_ft5x06_ts_data *tsdata) |
Lothar Waßmann | dac90dc2 | 2014-03-28 09:23:02 -0700 | [diff] [blame] | 813 | { |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 814 | struct edt_reg_addr *reg_addr = &tsdata->reg_addr; |
Dmitry Torokhov | 2e23b7a | 2015-09-12 09:54:15 -0700 | [diff] [blame^] | 815 | u32 val; |
| 816 | int error; |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 817 | |
Dmitry Torokhov | 2e23b7a | 2015-09-12 09:54:15 -0700 | [diff] [blame^] | 818 | error = device_property_read_u32(dev, "threshold", &val); |
| 819 | if (!error) |
| 820 | reg_addr->reg_threshold = val; |
| 821 | |
| 822 | error = device_property_read_u32(dev, "gain", &val); |
| 823 | if (!error) |
| 824 | reg_addr->reg_gain = val; |
| 825 | |
| 826 | error = device_property_read_u32(dev, "offset", &val); |
| 827 | if (!error) |
| 828 | reg_addr->reg_offset = val; |
Lothar Waßmann | dac90dc2 | 2014-03-28 09:23:02 -0700 | [diff] [blame] | 829 | } |
| 830 | |
Bill Pemberton | 5298cc4 | 2012-11-23 21:38:25 -0800 | [diff] [blame] | 831 | static void |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 832 | edt_ft5x06_ts_get_parameters(struct edt_ft5x06_ts_data *tsdata) |
| 833 | { |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 834 | struct edt_reg_addr *reg_addr = &tsdata->reg_addr; |
| 835 | |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 836 | tsdata->threshold = edt_ft5x06_register_read(tsdata, |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 837 | reg_addr->reg_threshold); |
| 838 | tsdata->gain = edt_ft5x06_register_read(tsdata, reg_addr->reg_gain); |
| 839 | tsdata->offset = edt_ft5x06_register_read(tsdata, reg_addr->reg_offset); |
| 840 | if (reg_addr->reg_report_rate != NO_REGISTER) |
| 841 | tsdata->report_rate = edt_ft5x06_register_read(tsdata, |
| 842 | reg_addr->reg_report_rate); |
| 843 | tsdata->num_x = edt_ft5x06_register_read(tsdata, reg_addr->reg_num_x); |
| 844 | tsdata->num_y = edt_ft5x06_register_read(tsdata, reg_addr->reg_num_y); |
| 845 | } |
| 846 | |
| 847 | static void |
| 848 | edt_ft5x06_ts_set_regs(struct edt_ft5x06_ts_data *tsdata) |
| 849 | { |
| 850 | struct edt_reg_addr *reg_addr = &tsdata->reg_addr; |
| 851 | |
| 852 | switch (tsdata->version) { |
| 853 | case M06: |
| 854 | reg_addr->reg_threshold = WORK_REGISTER_THRESHOLD; |
| 855 | reg_addr->reg_report_rate = WORK_REGISTER_REPORT_RATE; |
| 856 | reg_addr->reg_gain = WORK_REGISTER_GAIN; |
| 857 | reg_addr->reg_offset = WORK_REGISTER_OFFSET; |
| 858 | reg_addr->reg_num_x = WORK_REGISTER_NUM_X; |
| 859 | reg_addr->reg_num_y = WORK_REGISTER_NUM_Y; |
| 860 | break; |
| 861 | |
| 862 | case M09: |
| 863 | reg_addr->reg_threshold = M09_REGISTER_THRESHOLD; |
| 864 | reg_addr->reg_gain = M09_REGISTER_GAIN; |
| 865 | reg_addr->reg_offset = M09_REGISTER_OFFSET; |
| 866 | reg_addr->reg_num_x = M09_REGISTER_NUM_X; |
| 867 | reg_addr->reg_num_y = M09_REGISTER_NUM_Y; |
| 868 | break; |
| 869 | } |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 870 | } |
| 871 | |
Bill Pemberton | 5298cc4 | 2012-11-23 21:38:25 -0800 | [diff] [blame] | 872 | static int edt_ft5x06_ts_probe(struct i2c_client *client, |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 873 | const struct i2c_device_id *id) |
| 874 | { |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 875 | struct edt_ft5x06_ts_data *tsdata; |
| 876 | struct input_dev *input; |
| 877 | int error; |
| 878 | char fw_version[EDT_NAME_LEN]; |
| 879 | |
| 880 | dev_dbg(&client->dev, "probing for EDT FT5x06 I2C\n"); |
| 881 | |
Lothar Waßmann | 02300bd | 2014-01-16 16:26:55 -0800 | [diff] [blame] | 882 | tsdata = devm_kzalloc(&client->dev, sizeof(*tsdata), GFP_KERNEL); |
| 883 | if (!tsdata) { |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 884 | dev_err(&client->dev, "failed to allocate driver data.\n"); |
Lothar Waßmann | 02300bd | 2014-01-16 16:26:55 -0800 | [diff] [blame] | 885 | return -ENOMEM; |
| 886 | } |
| 887 | |
Franklin S Cooper Jr | 13c23cd | 2015-09-11 17:30:34 -0700 | [diff] [blame] | 888 | tsdata->reset_gpio = devm_gpiod_get_optional(&client->dev, |
| 889 | "reset", GPIOD_OUT_HIGH); |
| 890 | if (IS_ERR(tsdata->reset_gpio)) { |
| 891 | error = PTR_ERR(tsdata->reset_gpio); |
| 892 | dev_err(&client->dev, |
| 893 | "Failed to request GPIO reset pin, error %d\n", error); |
| 894 | return error; |
Lothar Waßmann | dac90dc2 | 2014-03-28 09:23:02 -0700 | [diff] [blame] | 895 | } |
| 896 | |
Franklin S Cooper Jr | 13c23cd | 2015-09-11 17:30:34 -0700 | [diff] [blame] | 897 | tsdata->wake_gpio = devm_gpiod_get_optional(&client->dev, |
| 898 | "wake", GPIOD_OUT_LOW); |
| 899 | if (IS_ERR(tsdata->wake_gpio)) { |
| 900 | error = PTR_ERR(tsdata->wake_gpio); |
| 901 | dev_err(&client->dev, |
| 902 | "Failed to request GPIO wake pin, error %d\n", error); |
Lothar Waßmann | dac90dc2 | 2014-03-28 09:23:02 -0700 | [diff] [blame] | 903 | return error; |
Franklin S Cooper Jr | 13c23cd | 2015-09-11 17:30:34 -0700 | [diff] [blame] | 904 | } |
Lothar Waßmann | dac90dc2 | 2014-03-28 09:23:02 -0700 | [diff] [blame] | 905 | |
Franklin S Cooper Jr | 13c23cd | 2015-09-11 17:30:34 -0700 | [diff] [blame] | 906 | if (tsdata->wake_gpio) { |
| 907 | usleep_range(5000, 6000); |
| 908 | gpiod_set_value_cansleep(tsdata->wake_gpio, 1); |
| 909 | } |
| 910 | |
| 911 | if (tsdata->reset_gpio) { |
| 912 | usleep_range(5000, 6000); |
| 913 | gpiod_set_value_cansleep(tsdata->reset_gpio, 0); |
| 914 | msleep(300); |
Lothar Waßmann | dac90dc2 | 2014-03-28 09:23:02 -0700 | [diff] [blame] | 915 | } |
| 916 | |
Lothar Waßmann | 02300bd | 2014-01-16 16:26:55 -0800 | [diff] [blame] | 917 | input = devm_input_allocate_device(&client->dev); |
| 918 | if (!input) { |
| 919 | dev_err(&client->dev, "failed to allocate input device.\n"); |
| 920 | return -ENOMEM; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 921 | } |
| 922 | |
| 923 | mutex_init(&tsdata->mutex); |
| 924 | tsdata->client = client; |
| 925 | tsdata->input = input; |
| 926 | tsdata->factory_mode = false; |
| 927 | |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 928 | error = edt_ft5x06_ts_identify(client, tsdata, fw_version); |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 929 | if (error) { |
| 930 | dev_err(&client->dev, "touchscreen probe failed\n"); |
Lothar Waßmann | 02300bd | 2014-01-16 16:26:55 -0800 | [diff] [blame] | 931 | return error; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 932 | } |
| 933 | |
Lothar Waßmann | fd335ab | 2014-03-28 09:31:14 -0700 | [diff] [blame] | 934 | edt_ft5x06_ts_set_regs(tsdata); |
Dmitry Torokhov | 2e23b7a | 2015-09-12 09:54:15 -0700 | [diff] [blame^] | 935 | edt_ft5x06_ts_get_defaults(&client->dev, tsdata); |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 936 | edt_ft5x06_ts_get_parameters(tsdata); |
| 937 | |
| 938 | dev_dbg(&client->dev, |
| 939 | "Model \"%s\", Rev. \"%s\", %dx%d sensors\n", |
| 940 | tsdata->name, fw_version, tsdata->num_x, tsdata->num_y); |
| 941 | |
| 942 | input->name = tsdata->name; |
| 943 | input->id.bustype = BUS_I2C; |
| 944 | input->dev.parent = &client->dev; |
| 945 | |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 946 | input_set_abs_params(input, ABS_MT_POSITION_X, |
| 947 | 0, tsdata->num_x * 64 - 1, 0, 0); |
| 948 | input_set_abs_params(input, ABS_MT_POSITION_Y, |
| 949 | 0, tsdata->num_y * 64 - 1, 0, 0); |
Maxime Ripard | 2c00559 | 2015-03-21 20:18:06 -0700 | [diff] [blame] | 950 | |
Dmitry Torokhov | 22ddbac | 2015-09-12 09:37:03 -0700 | [diff] [blame] | 951 | touchscreen_parse_properties(input, true); |
Maxime Ripard | 2c00559 | 2015-03-21 20:18:06 -0700 | [diff] [blame] | 952 | |
Dmitry Torokhov | 38e1b72 | 2015-06-01 10:41:11 -0700 | [diff] [blame] | 953 | error = input_mt_init_slots(input, MAX_SUPPORT_POINTS, INPUT_MT_DIRECT); |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 954 | if (error) { |
| 955 | dev_err(&client->dev, "Unable to init MT slots.\n"); |
Lothar Waßmann | 02300bd | 2014-01-16 16:26:55 -0800 | [diff] [blame] | 956 | return error; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 957 | } |
| 958 | |
| 959 | input_set_drvdata(input, tsdata); |
| 960 | i2c_set_clientdata(client, tsdata); |
| 961 | |
Lothar Waßmann | dac90dc2 | 2014-03-28 09:23:02 -0700 | [diff] [blame] | 962 | error = devm_request_threaded_irq(&client->dev, client->irq, NULL, |
| 963 | edt_ft5x06_ts_isr, |
| 964 | IRQF_TRIGGER_FALLING | IRQF_ONESHOT, |
| 965 | client->name, tsdata); |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 966 | if (error) { |
| 967 | dev_err(&client->dev, "Unable to request touchscreen IRQ.\n"); |
Lothar Waßmann | 02300bd | 2014-01-16 16:26:55 -0800 | [diff] [blame] | 968 | return error; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 969 | } |
| 970 | |
| 971 | error = sysfs_create_group(&client->dev.kobj, &edt_ft5x06_attr_group); |
| 972 | if (error) |
Lothar Waßmann | 02300bd | 2014-01-16 16:26:55 -0800 | [diff] [blame] | 973 | return error; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 974 | |
| 975 | error = input_register_device(input); |
Lothar Waßmann | dac90dc2 | 2014-03-28 09:23:02 -0700 | [diff] [blame] | 976 | if (error) |
| 977 | goto err_remove_attrs; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 978 | |
| 979 | edt_ft5x06_ts_prepare_debugfs(tsdata, dev_driver_string(&client->dev)); |
| 980 | device_init_wakeup(&client->dev, 1); |
| 981 | |
| 982 | dev_dbg(&client->dev, |
Lothar Waßmann | dac90dc2 | 2014-03-28 09:23:02 -0700 | [diff] [blame] | 983 | "EDT FT5x06 initialized: IRQ %d, WAKE pin %d, Reset pin %d.\n", |
Franklin S Cooper Jr | 13c23cd | 2015-09-11 17:30:34 -0700 | [diff] [blame] | 984 | client->irq, desc_to_gpio(tsdata->wake_gpio), |
| 985 | desc_to_gpio(tsdata->reset_gpio)); |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 986 | |
| 987 | return 0; |
Lothar Waßmann | dac90dc2 | 2014-03-28 09:23:02 -0700 | [diff] [blame] | 988 | |
| 989 | err_remove_attrs: |
| 990 | sysfs_remove_group(&client->dev.kobj, &edt_ft5x06_attr_group); |
| 991 | return error; |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 992 | } |
| 993 | |
Bill Pemberton | e2619cf | 2012-11-23 21:50:47 -0800 | [diff] [blame] | 994 | static int edt_ft5x06_ts_remove(struct i2c_client *client) |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 995 | { |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 996 | struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client); |
| 997 | |
| 998 | edt_ft5x06_ts_teardown_debugfs(tsdata); |
| 999 | sysfs_remove_group(&client->dev.kobj, &edt_ft5x06_attr_group); |
| 1000 | |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 1001 | return 0; |
| 1002 | } |
| 1003 | |
Jingoo Han | 02b6a58 | 2014-11-02 00:04:14 -0700 | [diff] [blame] | 1004 | static int __maybe_unused edt_ft5x06_ts_suspend(struct device *dev) |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 1005 | { |
| 1006 | struct i2c_client *client = to_i2c_client(dev); |
| 1007 | |
| 1008 | if (device_may_wakeup(dev)) |
| 1009 | enable_irq_wake(client->irq); |
| 1010 | |
| 1011 | return 0; |
| 1012 | } |
| 1013 | |
Jingoo Han | 02b6a58 | 2014-11-02 00:04:14 -0700 | [diff] [blame] | 1014 | static int __maybe_unused edt_ft5x06_ts_resume(struct device *dev) |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 1015 | { |
| 1016 | struct i2c_client *client = to_i2c_client(dev); |
| 1017 | |
| 1018 | if (device_may_wakeup(dev)) |
| 1019 | disable_irq_wake(client->irq); |
| 1020 | |
| 1021 | return 0; |
| 1022 | } |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 1023 | |
| 1024 | static SIMPLE_DEV_PM_OPS(edt_ft5x06_ts_pm_ops, |
| 1025 | edt_ft5x06_ts_suspend, edt_ft5x06_ts_resume); |
| 1026 | |
| 1027 | static const struct i2c_device_id edt_ft5x06_ts_id[] = { |
Lothar Waßmann | 1730d81 | 2014-03-28 09:20:08 -0700 | [diff] [blame] | 1028 | { "edt-ft5x06", 0, }, |
| 1029 | { /* sentinel */ } |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 1030 | }; |
| 1031 | MODULE_DEVICE_TABLE(i2c, edt_ft5x06_ts_id); |
| 1032 | |
Lothar Waßmann | dac90dc2 | 2014-03-28 09:23:02 -0700 | [diff] [blame] | 1033 | #ifdef CONFIG_OF |
| 1034 | static const struct of_device_id edt_ft5x06_of_match[] = { |
| 1035 | { .compatible = "edt,edt-ft5206", }, |
| 1036 | { .compatible = "edt,edt-ft5306", }, |
| 1037 | { .compatible = "edt,edt-ft5406", }, |
| 1038 | { /* sentinel */ } |
| 1039 | }; |
| 1040 | MODULE_DEVICE_TABLE(of, edt_ft5x06_of_match); |
| 1041 | #endif |
| 1042 | |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 1043 | static struct i2c_driver edt_ft5x06_ts_driver = { |
| 1044 | .driver = { |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 1045 | .name = "edt_ft5x06", |
Lothar Waßmann | dac90dc2 | 2014-03-28 09:23:02 -0700 | [diff] [blame] | 1046 | .of_match_table = of_match_ptr(edt_ft5x06_of_match), |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 1047 | .pm = &edt_ft5x06_ts_pm_ops, |
| 1048 | }, |
| 1049 | .id_table = edt_ft5x06_ts_id, |
| 1050 | .probe = edt_ft5x06_ts_probe, |
Bill Pemberton | 1cb0aa8 | 2012-11-23 21:27:39 -0800 | [diff] [blame] | 1051 | .remove = edt_ft5x06_ts_remove, |
Simon Budig | 43c4d13 | 2012-07-24 23:29:36 -0700 | [diff] [blame] | 1052 | }; |
| 1053 | |
| 1054 | module_i2c_driver(edt_ft5x06_ts_driver); |
| 1055 | |
| 1056 | MODULE_AUTHOR("Simon Budig <simon.budig@kernelconcepts.de>"); |
| 1057 | MODULE_DESCRIPTION("EDT FT5x06 I2C Touchscreen Driver"); |
| 1058 | MODULE_LICENSE("GPL"); |