2 * Copyright (c) 2014 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * ATMEL_MXT - Atmel MXT touchscreen driver
37 * PRELIMINARY DRIVER PRELIMINARY DRIVER PRELIMINARY DRIVERE
39 * (everything is pretty much hardwired and we assume the device is already
40 * operational, which it appears to be. ONLY TESTED ON ACER C720).
42 * This driver attaches to Acer TouchScreen MXT chipsets and currently
43 * emulates the ELO touchscreen serial protocol "elographics" for X:
45 * Section "InputDevice"
46 * Identifier "TouchScreen0"
47 * Driver "elographics"
48 * Option "Device" "/dev/atmel1-4a"
51 * The MXT chipsets typically attach on haswell chromebooks on one of the I2C
52 * busses at address 0x4A. On my Acer C720 it attaches to the ig4 driver's
53 * I2C bus #1 at 0x4A. kldload ig4; kldload atmel_mxt.
55 * The kernel driver and test code is written from scratch, but some code has
56 * been snarfed (in separate files) from linux development as referenced
59 * www.atmel.com/products/touchsolutions/touchscreens/unlimited_touch.aspx
60 * git://github.com/atmel-maxtouch/obp-utils.git
62 * The linux driver was also consulted, but not used. Note that the linux
63 * driver appears to be GPL'd but uses code from obp-utils.git on github
64 * which is (c)Copyright by Atmel and uses more of a BSD-like license. The
65 * obp-* source files contain the snarfed code and include this license.
67 * The ELO touchscreen serial protocol uses 10-byte fixed-length packets:
69 * Byte 0: ELO_SYNC_BYTE ('U')
70 * Byte 1-8: Packet data
71 * Byte 9: checksum of bytes 0 to 8
73 * Our interface reads and writes only whole packets and does not do any
74 * buffer stitching. It is compatible with Xorg.
76 * Control Commands sent from Userland (only an Ack packet is returned)
78 * Byte 0: ELO_SYNC_BYTE ('U')
79 * Byte 1: ELO_MODE ('m')
90 * Byte 0: ELO_SYNC_BYTE ('U')
91 * Byte 1: ELO_REPORT ('r')
93 * Query Command sent from Userland: (expect response packet and Ack packet)
95 * Byte 0: ELO_SYNC_BYTE ('U') Request ID
96 * Byte 1: ELO_ID ('i')
98 * Byte 0: ELO_SYNC_BYTE ('U')` Request Owner
99 * Byte 1: ELO_OWNER ('o')
101 * Streaming packets sent from the driver to userland
103 * Byte 0: ELO_SYNC_BYTE ('U')
104 * Byte 1: ELO_TOUCH ('T')
105 * Byte 2: Packet type
106 * Bit 2 : Pen Up (Release) 0x04
107 * Bit 1 : Position (Stream) 0x02
108 * Bit 0 : Pen Down (Press) 0x01
109 * Byte 3: X coordinate lsb
110 * Byte 4: X coordinate msb
111 * Byte 5: Y coordinate lsb
112 * Byte 6: Y coordinate msb
113 * Byte 7: Z coordinate lsb
114 * Byte 8: Z coordinate msb
116 * Responses to commands: (one or two packets returned)
118 * Byte 0: ELO_SYNC_BYTE ('U') (if in response to query)
119 * Byte 1: toupper(command_byte) (control commands have no
120 * Byte 2-8: ... depends .... response)
122 * Byte 0: ELO_SYNC_BYTE ('U') (unconditional ack)
124 * Byte 2-8: ... depends ....
126 * NOTE! For the most part we ignore commands other than doing the handwaving
127 * to send a dummied-up response and ack as assumed by the X driver.
128 * Also, the read() and write() support only reads and writes whole
129 * packets. There is NO byte-partial buffering. The X driver appears
130 * to be compatible with these restrictions.
132 * figure out the bootstrapping and commands.
134 * Unable to locate any datasheet for the device.
138 * Currently no features. Only one finger is supported by this attachment
139 * for now and I haven't written any de-jitter and finger-transfer code.
140 * This is good enough for moving and resizing windows (given big enough
141 * widgets), and hitting browser buttons and hotlinks.
143 * Currently no scrolling control or other features. We would need to
144 * basically implement either the linux general input even infrastructure
145 * which is a LOT of code, or a mouse emulator to handle scrolling emulation.
147 #include <sys/kernel.h>
148 #include <sys/param.h>
149 #include <sys/systm.h>
150 #include <sys/device.h>
151 #include <sys/module.h>
153 #include <sys/conf.h>
155 #include <sys/fcntl.h>
156 /*#include <sys/input.h>*/
157 #include <sys/vnode.h>
158 #include <sys/sysctl.h>
159 #include <sys/event.h>
160 #include <sys/devfs.h>
162 #include <bus/smbus/smbconf.h>
163 #include <bus/smbus/smbus.h>
164 #include "atmel_mxt.h"
166 #include "smbus_if.h"
168 #include "device_if.h"
183 typedef struct elopacket elopacket_t;
185 typedef struct atmel_track {
190 int report; /* what we have to report */
193 #define ATMEL_TRACK_RELEASED 0
194 #define ATMEL_TRACK_PRESSED 1
196 #define ATMEL_REPORT_PRESS 0x0001
197 #define ATMEL_REPORT_MOVE 0x0002
198 #define ATMEL_REPORT_RELEASE 0x0004
200 #define ATMEL_MAXTRACK 10
202 struct atmel_mxt_softc {
204 int count; /* >0 if device opened */
208 struct kqinfo kqinfo;
217 struct mxt_rollup core;
218 struct mxt_object *msgprocobj;
219 struct mxt_object *cmdprocobj;
230 atmel_track_t track[ATMEL_MAXTRACK];
234 elopacket_t pend_rep; /* pending reply to command */
235 int pend_ack; /* pending reply mode */
237 int last_active_tick;
238 int last_calibrate_tick;
239 int data_signal; /* something ready to read */
240 int blocked; /* someone is blocking */
242 int sample_rate; /* samples/sec */
246 typedef struct atmel_mxt_softc atmel_mxt_softc_t;
248 #define ATMEL_POLL_SHUTDOWN 0x0001
250 #define PEND_ACK_NONE 0 /* no reply to command pending */
251 #define PEND_ACK_RESPOND 1 /* reply w/response and ack */
252 #define PEND_ACK_ACK 2 /* reply w/ack only */
254 #define REPORT_NONE 0x0000
255 #define REPORT_ALL 0x0001
258 * Async debug variable commands are executed by the poller and will
261 static int atmel_mxt_idle_freq = 1;
262 SYSCTL_INT(_debug, OID_AUTO, atmel_mxt_idle_freq, CTLFLAG_RW,
263 &atmel_mxt_idle_freq, 0, "");
264 static int atmel_mxt_slow_freq = 20;
265 SYSCTL_INT(_debug, OID_AUTO, atmel_mxt_slow_freq, CTLFLAG_RW,
266 &atmel_mxt_slow_freq, 0, "");
267 static int atmel_mxt_norm_freq = 100;
268 SYSCTL_INT(_debug, OID_AUTO, atmel_mxt_norm_freq, CTLFLAG_RW,
269 &atmel_mxt_norm_freq, 0, "");
270 static int atmel_mxt_minpressure = 16;
271 SYSCTL_INT(_debug, OID_AUTO, atmel_mxt_minpressure, CTLFLAG_RW,
272 &atmel_mxt_minpressure, 0, "");
275 * Run a calibration command every N seconds only when idle. 0 to disable.
276 * Default every 30 seconds.
278 static int atmel_mxt_autocalibrate = 30;
279 SYSCTL_INT(_debug, OID_AUTO, atmel_mxt_autocalibrate, CTLFLAG_RW,
280 &atmel_mxt_autocalibrate, 0, "");
283 * run a calibration on module startup.
285 static int atmel_mxt_debug = 0;
286 SYSCTL_INT(_debug, OID_AUTO, atmel_mxt_debug, CTLFLAG_RW,
287 &atmel_mxt_debug, 0, "");
289 static void atmel_mxt_poll_thread(void *arg);
290 static void atmel_find_active_state(atmel_mxt_softc_t *sc);
291 static int atmel_mxt_raw_input(atmel_mxt_softc_t *sc, mxt_message_t *msg);
292 static struct mxt_object *mxt_findobject(struct mxt_rollup *core, int type);
293 static int mxt_read_reg(atmel_mxt_softc_t *sc, uint16_t reg,
294 void *rbuf, int bytes);
295 static int mxt_write_reg_buf(atmel_mxt_softc_t *sc, uint16_t reg,
296 void *xbuf, int bytes);
297 static int mxt_write_reg(atmel_mxt_softc_t *sc, uint16_t reg, uint8_t val);
298 static int mxt_read_object(atmel_mxt_softc_t *sc, struct mxt_object *obj,
299 void *rbuf, int rbytes);
300 static int mxt_write_object_off(atmel_mxt_softc_t *sc, struct mxt_object *obj,
301 int offset, uint8_t val);
305 msgflagsstr(uint8_t flags)
309 buf[0] = (flags & MXT_MSGF_DETECT) ? 'D' : '.';
310 buf[1] = (flags & MXT_MSGF_PRESS) ? 'P' : '.';
311 buf[2] = (flags & MXT_MSGF_RELEASE) ? 'R' : '.';
312 buf[3] = (flags & MXT_MSGF_MOVE) ? 'M' : '.';
313 buf[4] = (flags & MXT_MSGF_VECTOR) ? 'V' : '.';
314 buf[5] = (flags & MXT_MSGF_AMP) ? 'A' : '.';
315 buf[6] = (flags & MXT_MSGF_SUPPRESS) ? 'S' : '.';
316 buf[7] = (flags & MXT_MSGF_UNGRIP) ? 'U' : '.';
323 atmel_mxt_lock(atmel_mxt_softc_t *sc)
325 lockmgr(&sc->lk, LK_EXCLUSIVE);
330 atmel_mxt_unlock(atmel_mxt_softc_t *sc)
332 lockmgr(&sc->lk, LK_RELEASE);
336 * Notify if possible receive data ready. Must be called
337 * without the lock held to avoid deadlocking in kqueue.
341 atmel_mxt_notify(atmel_mxt_softc_t *sc)
343 if (sc->data_signal) {
344 KNOTE(&sc->kqinfo.ki_note, 0);
348 wakeup(&sc->blocked);
350 atmel_mxt_unlock(sc);
355 * Initialize the device
359 init_device(atmel_mxt_softc_t *sc, int probe)
365 if (mxt_read_reg(sc, 0, &sc->core.info, sizeof(sc->core.info))) {
366 device_printf(sc->dev, "init_device read-reg failed\n");
369 sc->core.nobjs = sc->core.info.num_objects;
371 device_printf(sc->dev,
372 "%d configuration objects\n",
373 sc->core.info.num_objects);
375 if (sc->core.nobjs < 0 || sc->core.nobjs > 1024) {
376 device_printf(sc->dev,
377 "init_device nobjs (%d) out of bounds\n",
381 blksize = sizeof(sc->core.info) +
382 sc->core.nobjs * sizeof(struct mxt_object);
383 totsize = blksize + sizeof(struct mxt_raw_crc);
385 sc->core.buf = kmalloc(totsize, M_DEVBUF, M_WAITOK | M_ZERO);
386 if (mxt_read_reg(sc, 0, sc->core.buf, totsize)) {
387 device_printf(sc->dev,
388 "init_device cannot read configuration space\n");
391 kprintf("COREBUF %p %d\n", sc->core.buf, blksize);
392 crc = obp_convert_crc((void *)((uint8_t *)sc->core.buf + blksize));
393 if (obp_crc24(sc->core.buf, blksize) != crc) {
394 device_printf(sc->dev,
395 "init_device: configuration space "
396 "crc mismatch %08x/%08x\n",
397 crc, obp_crc24(sc->core.buf, blksize));
404 for (i = 0; i < sizeof(sc->core.info); ++i)
405 kprintf(" %02x", sc->core.buf[i]);
406 kprintf("\nconfig: ");
407 while (i < blksize) {
408 kprintf(" %02x", sc->core.buf[i]);
413 sc->core.objs = (void *)((uint8_t *)sc->core.buf +
414 sizeof(sc->core.info));
415 sc->msgprocobj = mxt_findobject(&sc->core, MXT_GEN_MESSAGEPROCESSOR);
416 sc->cmdprocobj = mxt_findobject(&sc->core, MXT_GEN_COMMANDPROCESSOR);
417 if (sc->msgprocobj == NULL) {
418 device_printf(sc->dev,
419 "init_device: cannot find msgproc config\n");
424 if (sc->msgprocobj == NULL) {
426 kfree(sc->core.buf, M_DEVBUF);
432 kfree(sc->core.buf, M_DEVBUF);
440 * Device infrastructure
442 #define ATMEL_SOFTC(unit) \
443 ((atmel_mxt_softc_t *)devclass_get_softc(atmel_mxt_devclass, (unit)))
445 static void atmel_mxt_identify(driver_t *driver, device_t parent);
446 static int atmel_mxt_probe(device_t);
447 static int atmel_mxt_attach(device_t);
448 static int atmel_mxt_detach(device_t);
450 static devclass_t atmel_mxt_devclass;
452 static device_method_t atmel_mxt_methods[] = {
453 /* device interface */
454 DEVMETHOD(device_identify, atmel_mxt_identify),
455 DEVMETHOD(device_probe, atmel_mxt_probe),
456 DEVMETHOD(device_attach, atmel_mxt_attach),
457 DEVMETHOD(device_detach, atmel_mxt_detach),
460 /* smbus interface */
461 DEVMETHOD(smbus_intr, smbus_generic_intr),
467 static driver_t atmel_mxt_driver = {
470 sizeof(atmel_mxt_softc_t),
473 static d_open_t atmel_mxtopen;
474 static d_close_t atmel_mxtclose;
475 static d_ioctl_t atmel_mxtioctl;
476 static d_read_t atmel_mxtread;
477 static d_write_t atmel_mxtwrite;
478 static d_kqfilter_t atmel_mxtkqfilter;
480 static struct dev_ops atmel_mxt_ops = {
481 { "atmel_mxt", 0, 0 },
482 .d_open = atmel_mxtopen,
483 .d_close = atmel_mxtclose,
484 .d_ioctl = atmel_mxtioctl,
485 .d_read = atmel_mxtread,
486 .d_write = atmel_mxtwrite,
487 .d_kqfilter = atmel_mxtkqfilter,
491 atmel_mxt_identify(driver_t *driver, device_t parent)
493 if (device_find_child(parent, "atmel_mxt", -1) == NULL)
494 BUS_ADD_CHILD(parent, parent, 0, "atmel_mxt", -1);
498 atmel_mxt_probe(device_t dev)
500 atmel_mxt_softc_t sc;
503 bzero(&sc, sizeof(sc));
505 sc.unit = device_get_unit(dev);
508 * Only match against specific addresses to avoid blowing up
509 * other I2C devices (?). At least for now.
511 * 0x400 (from smbus) - means specific device address probe,
512 * rather than generic.
514 * 0x4A - cypress trackpad on the acer c720.
516 if ((sc.unit & 0x04FF) != (0x0400 | 0x04A))
518 sc.addr = sc.unit & 0x3FF;
519 error = init_device(&sc, 1);
523 device_set_desc(dev, "Atmel MXT TouchScreen");
525 return (BUS_PROBE_VENDOR);
529 atmel_mxt_attach(device_t dev)
531 atmel_mxt_softc_t *sc;
533 sc = (atmel_mxt_softc_t *)device_get_softc(dev);
537 bzero(sc, sizeof(*sc));
539 lockinit(&sc->lk, "atmel_mxt", 0, 0);
540 sc->reporting_mode = 1;
543 sc->unit = device_get_unit(dev);
544 if ((sc->unit & 0x04FF) != (0x0400 | 0x04A))
546 sc->addr = sc->unit & 0x3FF;
547 sc->last_active_tick = ticks;
548 sc->last_calibrate_tick = ticks - atmel_mxt_autocalibrate * hz;
550 if (init_device(sc, 0))
553 if (sc->unit & 0x0400) {
554 sc->devnode = make_dev(&atmel_mxt_ops, sc->unit,
555 UID_ROOT, GID_WHEEL, 0600,
557 sc->unit >> 11, sc->unit & 1023);
559 sc->devnode = make_dev(&atmel_mxt_ops, sc->unit,
560 UID_ROOT, GID_WHEEL, 0600, "atmel%d", sc->unit);
562 device_printf(dev, "atmel mxt touchscreen driver attached\n");
563 /* device_printf(dev, "...."); */
566 * Start the polling thread.
568 lwkt_create(atmel_mxt_poll_thread, sc,
569 &sc->poll_td, NULL, 0, -1, "atmel_mxt-poll");
575 atmel_mxt_detach(device_t dev)
577 atmel_mxt_softc_t *sc;
579 sc = (atmel_mxt_softc_t *)device_get_softc(dev);
582 * Cleanup our poller thread
584 atomic_set_int(&sc->poll_flags, ATMEL_POLL_SHUTDOWN);
585 while (sc->poll_td) {
586 wakeup(&sc->poll_flags);
587 tsleep(&sc->poll_td, 0, "atmel_mxtdet", hz);
591 dev_ops_remove_minor(&atmel_mxt_ops, device_get_unit(dev));
593 devfs_assume_knotes(sc->devnode, &sc->kqinfo);
595 kfree(sc->core.buf, M_DEVBUF);
598 sc->msgprocobj = NULL;
599 sc->cmdprocobj = NULL;
605 * USER DEVICE I/O FUNCTIONS
608 atmel_mxtopen (struct dev_open_args *ap)
610 cdev_t dev = ap->a_head.a_dev;
611 atmel_mxt_softc_t *sc = ATMEL_SOFTC(minor(dev));
625 atmel_mxtclose(struct dev_close_args *ap)
627 cdev_t dev = ap->a_head.a_dev;
628 atmel_mxt_softc_t *sc = ATMEL_SOFTC(minor(dev));
633 if (sc->count == 0) {
634 /* This is not supposed to happen. */
638 if (sc->count-- == 0) {
639 sc->reporting_mode = 0;
646 atmel_mxtread(struct dev_read_args *ap)
648 cdev_t dev = ap->a_head.a_dev;
649 atmel_mxt_softc_t *sc = ATMEL_SOFTC(minor(dev));
651 struct uio *uio = ap->a_uio;
652 int ioflag = ap->a_ioflag;
655 atmel_track_t *track;
658 * Load next ready event, block if necessary.
664 switch(sc->pend_ack) {
666 if (sc->tracking && sc->track[sc->tracking].report) {
670 track = &sc->track[sc->tracking];
671 bzero(&pkt, sizeof(pkt));
673 if (track->report & ATMEL_REPORT_PRESS) {
675 track->report &= ~ATMEL_REPORT_PRESS;
676 } else if (track->report & ATMEL_REPORT_MOVE) {
678 track->report &= ~ATMEL_REPORT_MOVE;
679 } else if (track->report &
680 ATMEL_REPORT_RELEASE) {
682 track->report &= ~ATMEL_REPORT_RELEASE;
684 pkt.byte3 = track->x & 0xFF;
685 pkt.byte4 = track->x >> 8;
686 pkt.byte5 = track->y & 0xFF;
687 pkt.byte6 = track->y >> 8;
688 pkt.byte7 = track->pressure & 0xFF;
689 pkt.byte8 = track->pressure >> 8;
690 } else if (ioflag & IO_NDELAY) {
692 * Non-blocking, nothing ready
697 * Blocking, nothing ready
701 error = lksleep(&sc->blocked, &sc->lk,
702 PCATCH, "atmelw", 0);
707 case PEND_ACK_RESPOND:
709 sc->pend_ack = PEND_ACK_ACK;
712 bzero(&pkt, sizeof(pkt));
714 sc->pend_ack = PEND_ACK_NONE;
717 atmel_find_active_state(sc);
720 atmel_mxt_unlock(sc);
723 * If no error we can return the event loaded into pkt.
730 for (i = 0; i < sizeof(pkt) - 1; ++i)
731 csum += ((uint8_t *)&pkt)[i];
736 error = uiomove((void *)&pkt, n, uio);
743 atmel_mxtwrite(struct dev_write_args *ap)
745 cdev_t dev = ap->a_head.a_dev;
746 atmel_mxt_softc_t *sc = ATMEL_SOFTC(minor(dev));
747 struct uio *uio = ap->a_uio;
754 while (uio->uio_resid) {
755 bzero(&pkt, sizeof(pkt));
759 error = uiomove((void *)&pkt, n, uio);
768 bzero(&sc->pend_rep, sizeof(sc->pend_rep));
769 sc->pend_rep.cmd = 'I';
770 sc->pend_ack = PEND_ACK_RESPOND;
774 * ELO_OWNER request owner
776 bzero(&sc->pend_rep, sizeof(sc->pend_rep));
777 sc->pend_rep.cmd = 'O';
778 sc->pend_ack = PEND_ACK_RESPOND;
782 * ELO_MODE control packet
784 sc->pend_ack = PEND_ACK_ACK;
788 * ELO_REPORT control packet
790 sc->pend_ack = PEND_ACK_ACK;
793 atmel_mxt_unlock(sc);
798 static void atmel_mxt_filt_detach(struct knote *);
799 static int atmel_mxt_filt(struct knote *, long);
801 static struct filterops atmel_mxt_filtops =
802 { FILTEROP_ISFD, NULL, atmel_mxt_filt_detach, atmel_mxt_filt };
805 atmel_mxtkqfilter(struct dev_kqfilter_args *ap)
807 cdev_t dev = ap->a_head.a_dev;
808 atmel_mxt_softc_t *sc = ATMEL_SOFTC(minor(dev));
809 struct knote *kn = ap->a_kn;
812 switch(kn->kn_filter) {
814 kn->kn_fop = &atmel_mxt_filtops;
815 kn->kn_hook = (void *)sc;
819 ap->a_result = EOPNOTSUPP;
822 klist = &sc->kqinfo.ki_note;
823 knote_insert(klist, kn);
829 atmel_mxt_filt_detach(struct knote *kn)
831 atmel_mxt_softc_t *sc = (atmel_mxt_softc_t *)kn->kn_hook;
834 klist = &sc->kqinfo.ki_note;
835 knote_remove(klist, kn);
839 atmel_mxt_filt(struct knote *kn, long hint)
841 atmel_mxt_softc_t *sc = (atmel_mxt_softc_t *)kn->kn_hook;
849 atmel_mxt_unlock(sc);
855 atmel_mxtioctl(struct dev_ioctl_args *ap)
857 cdev_t dev = ap->a_head.a_dev;
858 device_t bus; /* smbbus */
859 /*struct atmel_mxtcmd *s = (struct atmel_mxtcmd *)ap->a_data;*/
861 atmel_mxt_softc_t *sc = ATMEL_SOFTC(minor(dev));
870 * NOTE: smbus_*() functions automatically recurse the parent to
871 * get to the actual device driver.
873 bus = device_get_parent(sc->dev); /* smbus */
875 /* Allocate the bus. */
876 if ((error = smbus_request_bus(bus, sc->dev,
877 (ap->a_fflag & O_NONBLOCK) ?
878 SMB_DONTWAIT : (SMB_WAIT | SMB_INTR))))
884 error = inputev_ioctl(&sc->iev, ap->a_cmd, ap->a_data);
890 smbus_release_bus(bus, sc->dev);
896 * MAJOR SUPPORT FUNCTIONS
900 atmel_mxt_poll_thread(void *arg)
902 atmel_mxt_softc_t *sc = arg;
904 int freq = atmel_mxt_norm_freq;
907 while ((sc->poll_flags & ATMEL_POLL_SHUTDOWN) == 0) {
915 error = mxt_read_object(sc, sc->msgprocobj,
918 isidle = atmel_mxt_raw_input(sc, &msg);
924 * don't let the last_active_tick or last_calibrate_tick
925 * delta calculation overflow.
927 if ((ticks - sc->last_active_tick) > 1000 * hz)
928 sc->last_active_tick = ticks - 1000 * hz;
929 if ((ticks - sc->last_calibrate_tick) > 1000 * hz)
930 sc->last_calibrate_tick = ticks - 1000 * hz;
933 * Automatically calibrate when the touchpad has been
934 * idle atmel_mxt_autocalibrate seconds, and recalibrate
935 * on the same interval while it remains idle.
937 * If we don't do this the touchscreen can get really out
938 * of whack over time and basically stop functioning properly.
939 * It's unclear why the device does not do this automatically.
941 * Response occurs in the message stream (which we just
944 if (sc->cmdprocobj && atmel_mxt_autocalibrate &&
945 ((ticks - sc->last_calibrate_tick) >
946 atmel_mxt_autocalibrate * hz) &&
947 ((ticks - sc->last_active_tick) >
948 atmel_mxt_autocalibrate * hz)) {
949 sc->last_calibrate_tick = ticks;
950 mxt_write_object_off(sc, sc->cmdprocobj,
951 MXT_CMDPROC_CALIBRATE_OFF, 1);
953 tsleep(&sc->poll_flags, 0, "atmpol", (hz + freq - 1) / freq);
956 freq = atmel_mxt_idle_freq;
958 freq = atmel_mxt_slow_freq;
960 freq = atmel_mxt_norm_freq;
963 wakeup(&sc->poll_td);
967 * Calculate currently active state, if any
971 atmel_find_active_state(atmel_mxt_softc_t *sc)
973 atmel_track_t *track;
976 track = &sc->track[sc->tracking];
977 if (track->report == 0) {
978 for (i = 0; i < ATMEL_MAXTRACK; ++i) {
979 track = &sc->track[i];
986 if (track->report == 0 && sc->pend_ack == PEND_ACK_NONE) {
994 * Return non-zero if we are idle
998 atmel_mxt_raw_input(atmel_mxt_softc_t *sc, mxt_message_t *msg)
1000 atmel_track_t *track;
1003 if (atmel_mxt_debug) {
1004 kprintf("track=%02x f=%s x=%-4d y=%-4d p=%d amp=%d\n",
1006 msgflagsstr(msg->touch.flags),
1007 (msg->touch.pos[0] << 4) |
1008 ((msg->touch.pos[2] >> 4) & 0x0F),
1009 (msg->touch.pos[1] << 4) |
1010 ((msg->touch.pos[2]) & 0x0F),
1012 msg->touch.amplitude);
1017 * If message buffer is empty and no fingers are currently pressed
1018 * return idle, else we are not idle.
1020 if (msg->any.reportid == 0xFF)
1024 * Process message buffer. For now ignore any messages with
1025 * reportids that we do not understand.
1027 * note: reportid==1 typicallk acknowledges calibrations (?)
1029 if (msg->any.reportid < 3 || msg->any.reportid >= ATMEL_MAXTRACK)
1032 sc->last_active_tick = ticks;
1034 track = &sc->track[msg->any.reportid];
1035 track->x = (msg->touch.pos[0] << 4) |
1036 ((msg->touch.pos[2] >> 4) & 0x0F);
1037 track->y = (msg->touch.pos[1] << 4) |
1038 (msg->touch.pos[2] & 0x0F);
1039 track->pressure = msg->touch.amplitude;
1041 track->x = track->x * 3000 / 1361;
1042 track->y = track->y * 3000 / 3064;
1044 if (msg->touch.flags & MXT_MSGF_DETECT) {
1045 track->status = ATMEL_TRACK_PRESSED;
1046 if (msg->touch.flags & MXT_MSGF_PRESS) {
1047 track->report |= ATMEL_REPORT_PRESS;
1049 if (msg->touch.flags & MXT_MSGF_MOVE) {
1050 track->report |= ATMEL_REPORT_MOVE;
1053 track->status = ATMEL_TRACK_RELEASED;
1054 track->report |= ATMEL_REPORT_RELEASE;
1056 atmel_find_active_state(sc);
1059 atmel_mxt_unlock(sc);
1061 atmel_mxt_notify(sc);
1062 if (sc->track_fingers)
1073 mxt_findobject(struct mxt_rollup *core, int type)
1077 for (i = 0; i < core->nobjs; ++i) {
1078 if (core->objs[i].type == type)
1079 return(&core->objs[i]);
1085 mxt_read_reg(atmel_mxt_softc_t *sc, uint16_t reg, void *rbuf, int bytes)
1092 wreg[0] = reg & 255;
1095 bus = device_get_parent(sc->dev);
1096 if ((error = smbus_request_bus(bus, sc->dev, SMB_WAIT | SMB_INTR)) != 0)
1098 error = smbus_trans(bus, sc->addr, 0,
1103 rbuf, bytes, &rbytes);
1104 smbus_release_bus(bus, sc->dev);
1106 if (bytes != rbytes) {
1107 device_printf(sc->dev,
1108 "smbus_trans reg %d short read %d/%d\n",
1109 reg, bytes, rbytes);
1117 mxt_write_reg_buf(atmel_mxt_softc_t *sc, uint16_t reg, void *xbuf, int bytes)
1123 KKASSERT(bytes < sizeof(wbuf) - 2);
1124 wbuf[0] = reg & 255;
1126 bcopy(xbuf, wbuf + 2, bytes);
1128 bus = device_get_parent(sc->dev);
1129 if ((error = smbus_request_bus(bus, sc->dev, SMB_WAIT | SMB_INTR)) != 0)
1131 error = smbus_trans(bus, sc->addr, 0,
1137 smbus_release_bus(bus, sc->dev);
1142 mxt_write_reg(atmel_mxt_softc_t *sc, uint16_t reg, uint8_t val)
1144 return mxt_write_reg_buf(sc, reg, &val, 1);
1148 mxt_read_object(atmel_mxt_softc_t *sc, struct mxt_object *obj,
1149 void *rbuf, int rbytes)
1151 uint16_t reg = obj->start_pos_lsb + (obj->start_pos_msb << 8);
1152 int bytes = obj->size_minus_one + 1;
1156 return mxt_read_reg(sc, reg, rbuf, bytes);
1160 mxt_write_object_off(atmel_mxt_softc_t *sc, struct mxt_object *obj,
1161 int offset, uint8_t val)
1163 uint16_t reg = obj->start_pos_lsb + (obj->start_pos_msb << 8);
1166 return mxt_write_reg(sc, reg, val);
1169 DRIVER_MODULE(atmel_mxt, smbus, atmel_mxt_driver, atmel_mxt_devclass,
1171 MODULE_DEPEND(atmel_mxt, smbus, SMBUS_MINVER, SMBUS_PREFVER, SMBUS_MAXVER);
1172 MODULE_VERSION(atmel_mxt, 1);