Version:  2.0.40 2.2.26 2.4.37 3.13 3.14 3.15 3.16 3.17 3.18 3.19 4.0 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8 4.9 4.10

Linux/drivers/input/touchscreen/edt-ft5x06.c

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

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us