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/malloc.h>
152 #include <sys/module.h>
154 #include <sys/conf.h>
156 #include <sys/fcntl.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"
181 typedef struct elopacket elopacket_t;
183 typedef struct atmel_track {
188 int report; /* what we have to report */
191 #define ATMEL_TRACK_RELEASED 0
192 #define ATMEL_TRACK_PRESSED 1
194 #define ATMEL_REPORT_PRESS 0x0001
195 #define ATMEL_REPORT_MOVE 0x0002
196 #define ATMEL_REPORT_RELEASE 0x0004
198 #define ATMEL_MAXTRACK 10
200 struct atmel_mxt_softc {
202 int count; /* >0 if device opened */
206 struct kqinfo kqinfo;
215 struct mxt_rollup core;
216 struct mxt_object *msgprocobj;
217 struct mxt_object *cmdprocobj;
228 atmel_track_t track[ATMEL_MAXTRACK];
232 elopacket_t pend_rep; /* pending reply to command */
233 int pend_ack; /* pending reply mode */
235 int last_active_tick;
236 int last_calibrate_tick;
237 int data_signal; /* something ready to read */
238 int blocked; /* someone is blocking */
240 int sample_rate; /* samples/sec */
244 typedef struct atmel_mxt_softc atmel_mxt_softc_t;
246 #define ATMEL_POLL_SHUTDOWN 0x0001
248 #define PEND_ACK_NONE 0 /* no reply to command pending */
249 #define PEND_ACK_RESPOND 1 /* reply w/response and ack */
250 #define PEND_ACK_ACK 2 /* reply w/ack only */
252 #define REPORT_NONE 0x0000
253 #define REPORT_ALL 0x0001
256 * Async debug variable commands are executed by the poller and will
259 static int atmel_mxt_idle_freq = 1;
260 SYSCTL_INT(_debug, OID_AUTO, atmel_mxt_idle_freq, CTLFLAG_RW,
261 &atmel_mxt_idle_freq, 0, "");
262 static int atmel_mxt_slow_freq = 20;
263 SYSCTL_INT(_debug, OID_AUTO, atmel_mxt_slow_freq, CTLFLAG_RW,
264 &atmel_mxt_slow_freq, 0, "");
265 static int atmel_mxt_norm_freq = 100;
266 SYSCTL_INT(_debug, OID_AUTO, atmel_mxt_norm_freq, CTLFLAG_RW,
267 &atmel_mxt_norm_freq, 0, "");
268 static int atmel_mxt_minpressure = 16;
269 SYSCTL_INT(_debug, OID_AUTO, atmel_mxt_minpressure, CTLFLAG_RW,
270 &atmel_mxt_minpressure, 0, "");
271 static int atmel_mxt_reset = 0;
272 SYSCTL_INT(_debug, OID_AUTO, atmel_mxt_reset, CTLFLAG_RW,
273 &atmel_mxt_reset, 0, "");
276 * Run a calibration command every N seconds only when idle. 0 to disable.
277 * Default every 30 seconds.
279 static int atmel_mxt_autocalibrate = 30;
280 SYSCTL_INT(_debug, OID_AUTO, atmel_mxt_autocalibrate, CTLFLAG_RW,
281 &atmel_mxt_autocalibrate, 0, "");
284 * run a calibration on module startup.
286 static int atmel_mxt_debug = 0;
287 SYSCTL_INT(_debug, OID_AUTO, atmel_mxt_debug, CTLFLAG_RW,
288 &atmel_mxt_debug, 0, "");
290 static void atmel_mxt_poll_thread(void *arg);
291 static void atmel_find_active_state(atmel_mxt_softc_t *sc);
292 static int atmel_mxt_raw_input(atmel_mxt_softc_t *sc, mxt_message_t *msg);
293 static struct mxt_object *mxt_findobject(struct mxt_rollup *core, int type);
294 static int mxt_read_reg(atmel_mxt_softc_t *sc, uint16_t reg,
295 void *rbuf, int bytes);
296 static int mxt_write_reg_buf(atmel_mxt_softc_t *sc, uint16_t reg,
297 void *xbuf, int bytes);
298 static int mxt_write_reg(atmel_mxt_softc_t *sc, uint16_t reg, uint8_t val);
299 static int mxt_read_object(atmel_mxt_softc_t *sc, struct mxt_object *obj,
300 void *rbuf, int rbytes);
301 static int mxt_write_object_off(atmel_mxt_softc_t *sc, struct mxt_object *obj,
302 int offset, uint8_t val);
303 static void atmel_reset_device(atmel_mxt_softc_t *sc);
307 msgflagsstr(uint8_t flags)
311 buf[0] = (flags & MXT_MSGF_DETECT) ? 'D' : '.';
312 buf[1] = (flags & MXT_MSGF_PRESS) ? 'P' : '.';
313 buf[2] = (flags & MXT_MSGF_RELEASE) ? 'R' : '.';
314 buf[3] = (flags & MXT_MSGF_MOVE) ? 'M' : '.';
315 buf[4] = (flags & MXT_MSGF_VECTOR) ? 'V' : '.';
316 buf[5] = (flags & MXT_MSGF_AMP) ? 'A' : '.';
317 buf[6] = (flags & MXT_MSGF_SUPPRESS) ? 'S' : '.';
318 buf[7] = (flags & MXT_MSGF_UNGRIP) ? 'U' : '.';
325 atmel_mxt_lock(atmel_mxt_softc_t *sc)
327 lockmgr(&sc->lk, LK_EXCLUSIVE);
332 atmel_mxt_unlock(atmel_mxt_softc_t *sc)
334 lockmgr(&sc->lk, LK_RELEASE);
338 * Notify if possible receive data ready. Must be called
339 * without the lock held to avoid deadlocking in kqueue.
343 atmel_mxt_notify(atmel_mxt_softc_t *sc)
345 if (sc->data_signal) {
346 KNOTE(&sc->kqinfo.ki_note, 0);
350 wakeup(&sc->blocked);
352 atmel_mxt_unlock(sc);
357 * Initialize the device
361 init_device(atmel_mxt_softc_t *sc, int probe)
367 if (mxt_read_reg(sc, 0, &sc->core.info, sizeof(sc->core.info))) {
368 device_printf(sc->dev, "init_device read-reg failed\n");
371 sc->core.nobjs = sc->core.info.num_objects;
373 device_printf(sc->dev,
374 "%d configuration objects\n",
375 sc->core.info.num_objects);
377 if (sc->core.nobjs < 0 || sc->core.nobjs > 1024) {
378 device_printf(sc->dev,
379 "init_device nobjs (%d) out of bounds\n",
383 blksize = sizeof(sc->core.info) +
384 sc->core.nobjs * sizeof(struct mxt_object);
385 totsize = blksize + sizeof(struct mxt_raw_crc);
387 sc->core.buf = kmalloc(totsize, M_DEVBUF, M_WAITOK | M_ZERO);
388 if (mxt_read_reg(sc, 0, sc->core.buf, totsize)) {
389 device_printf(sc->dev,
390 "init_device cannot read configuration space\n");
393 kprintf("COREBUF %p %d\n", sc->core.buf, blksize);
394 crc = obp_convert_crc((void *)((uint8_t *)sc->core.buf + blksize));
395 if (obp_crc24(sc->core.buf, blksize) != crc) {
396 device_printf(sc->dev,
397 "init_device: configuration space "
398 "crc mismatch %08x/%08x\n",
399 crc, obp_crc24(sc->core.buf, blksize));
406 for (i = 0; i < sizeof(sc->core.info); ++i)
407 kprintf(" %02x", sc->core.buf[i]);
408 kprintf("\nconfig: ");
409 while (i < blksize) {
410 kprintf(" %02x", sc->core.buf[i]);
415 sc->core.objs = (void *)((uint8_t *)sc->core.buf +
416 sizeof(sc->core.info));
417 sc->msgprocobj = mxt_findobject(&sc->core, MXT_GEN_MESSAGEPROCESSOR);
418 sc->cmdprocobj = mxt_findobject(&sc->core, MXT_GEN_COMMANDPROCESSOR);
419 if (sc->msgprocobj == NULL) {
420 device_printf(sc->dev,
421 "init_device: cannot find msgproc config\n");
426 if (sc->msgprocobj == NULL) {
428 kfree(sc->core.buf, M_DEVBUF);
434 kfree(sc->core.buf, M_DEVBUF);
437 atmel_reset_device(sc);
444 * Device infrastructure
446 #define ATMEL_SOFTC(unit) \
447 ((atmel_mxt_softc_t *)devclass_get_softc(atmel_mxt_devclass, (unit)))
449 static void atmel_mxt_identify(driver_t *driver, device_t parent);
450 static int atmel_mxt_probe(device_t);
451 static int atmel_mxt_attach(device_t);
452 static int atmel_mxt_detach(device_t);
454 static devclass_t atmel_mxt_devclass;
456 static device_method_t atmel_mxt_methods[] = {
457 /* device interface */
458 DEVMETHOD(device_identify, atmel_mxt_identify),
459 DEVMETHOD(device_probe, atmel_mxt_probe),
460 DEVMETHOD(device_attach, atmel_mxt_attach),
461 DEVMETHOD(device_detach, atmel_mxt_detach),
464 /* smbus interface */
465 DEVMETHOD(smbus_intr, smbus_generic_intr),
471 static driver_t atmel_mxt_driver = {
474 sizeof(atmel_mxt_softc_t),
477 static d_open_t atmel_mxtopen;
478 static d_close_t atmel_mxtclose;
479 static d_ioctl_t atmel_mxtioctl;
480 static d_read_t atmel_mxtread;
481 static d_write_t atmel_mxtwrite;
482 static d_kqfilter_t atmel_mxtkqfilter;
484 static struct dev_ops atmel_mxt_ops = {
485 { "atmel_mxt", 0, 0 },
486 .d_open = atmel_mxtopen,
487 .d_close = atmel_mxtclose,
488 .d_ioctl = atmel_mxtioctl,
489 .d_read = atmel_mxtread,
490 .d_write = atmel_mxtwrite,
491 .d_kqfilter = atmel_mxtkqfilter,
495 atmel_mxt_identify(driver_t *driver, device_t parent)
497 if (device_find_child(parent, "atmel_mxt", -1) == NULL)
498 BUS_ADD_CHILD(parent, parent, 0, "atmel_mxt", -1);
502 atmel_mxt_probe(device_t dev)
504 atmel_mxt_softc_t sc;
507 bzero(&sc, sizeof(sc));
509 sc.unit = device_get_unit(dev);
512 * Only match against specific addresses to avoid blowing up
513 * other I2C devices (?). At least for now.
515 * 0x400 (from smbus) - means specific device address probe,
516 * rather than generic.
518 * 0x4A - cypress trackpad on the acer c720.
520 if ((sc.unit & 0x04FF) != (0x0400 | 0x04A))
522 sc.addr = sc.unit & 0x3FF;
523 error = init_device(&sc, 1);
527 device_set_desc(dev, "Atmel MXT TouchScreen");
529 return (BUS_PROBE_VENDOR);
533 atmel_mxt_attach(device_t dev)
535 atmel_mxt_softc_t *sc;
537 sc = (atmel_mxt_softc_t *)device_get_softc(dev);
541 bzero(sc, sizeof(*sc));
543 lockinit(&sc->lk, "atmel_mxt", 0, 0);
544 sc->reporting_mode = 1;
547 sc->unit = device_get_unit(dev);
548 if ((sc->unit & 0x04FF) != (0x0400 | 0x04A))
550 sc->addr = sc->unit & 0x3FF;
551 sc->last_active_tick = ticks;
552 sc->last_calibrate_tick = ticks - atmel_mxt_autocalibrate * hz;
554 if (init_device(sc, 0))
557 if (sc->unit & 0x0400) {
558 sc->devnode = make_dev(&atmel_mxt_ops, sc->unit,
559 UID_ROOT, GID_WHEEL, 0600,
561 sc->unit >> 11, sc->unit & 1023);
563 sc->devnode = make_dev(&atmel_mxt_ops, sc->unit,
564 UID_ROOT, GID_WHEEL, 0600, "atmel%d", sc->unit);
566 device_printf(dev, "atmel mxt touchscreen driver attached\n");
567 /* device_printf(dev, "...."); */
570 * Start the polling thread.
572 lwkt_create(atmel_mxt_poll_thread, sc,
573 &sc->poll_td, NULL, 0, -1, "atmel_mxt-poll");
579 atmel_mxt_detach(device_t dev)
581 atmel_mxt_softc_t *sc;
583 sc = (atmel_mxt_softc_t *)device_get_softc(dev);
586 * Cleanup our poller thread
588 atomic_set_int(&sc->poll_flags, ATMEL_POLL_SHUTDOWN);
589 while (sc->poll_td) {
590 wakeup(&sc->poll_flags);
591 tsleep(&sc->poll_td, 0, "atmel_mxtdet", hz);
595 dev_ops_remove_minor(&atmel_mxt_ops, device_get_unit(dev));
597 devfs_assume_knotes(sc->devnode, &sc->kqinfo);
599 kfree(sc->core.buf, M_DEVBUF);
602 sc->msgprocobj = NULL;
603 sc->cmdprocobj = NULL;
609 * USER DEVICE I/O FUNCTIONS
612 atmel_mxtopen (struct dev_open_args *ap)
614 cdev_t dev = ap->a_head.a_dev;
615 atmel_mxt_softc_t *sc = ATMEL_SOFTC(minor(dev));
629 atmel_mxtclose(struct dev_close_args *ap)
631 cdev_t dev = ap->a_head.a_dev;
632 atmel_mxt_softc_t *sc = ATMEL_SOFTC(minor(dev));
637 if (sc->count == 0) {
638 /* This is not supposed to happen. */
642 if (sc->count-- == 0) {
643 sc->reporting_mode = 0;
650 atmel_mxtread(struct dev_read_args *ap)
652 cdev_t dev = ap->a_head.a_dev;
653 atmel_mxt_softc_t *sc = ATMEL_SOFTC(minor(dev));
655 struct uio *uio = ap->a_uio;
656 int ioflag = ap->a_ioflag;
659 atmel_track_t *track;
662 * Load next ready event, block if necessary.
668 switch(sc->pend_ack) {
670 if (sc->tracking && sc->track[sc->tracking].report) {
674 track = &sc->track[sc->tracking];
675 bzero(&pkt, sizeof(pkt));
677 if (track->report & ATMEL_REPORT_PRESS) {
679 track->report &= ~ATMEL_REPORT_PRESS;
680 } else if (track->report & ATMEL_REPORT_MOVE) {
682 track->report &= ~ATMEL_REPORT_MOVE;
683 } else if (track->report &
684 ATMEL_REPORT_RELEASE) {
686 track->report &= ~ATMEL_REPORT_RELEASE;
688 pkt.byte3 = track->x & 0xFF;
689 pkt.byte4 = track->x >> 8;
690 pkt.byte5 = track->y & 0xFF;
691 pkt.byte6 = track->y >> 8;
692 pkt.byte7 = track->pressure & 0xFF;
693 pkt.byte8 = track->pressure >> 8;
694 } else if (ioflag & IO_NDELAY) {
696 * Non-blocking, nothing ready
701 * Blocking, nothing ready
705 error = lksleep(&sc->blocked, &sc->lk,
706 PCATCH, "atmelw", 0);
711 case PEND_ACK_RESPOND:
713 sc->pend_ack = PEND_ACK_ACK;
716 bzero(&pkt, sizeof(pkt));
718 sc->pend_ack = PEND_ACK_NONE;
721 atmel_find_active_state(sc);
724 atmel_mxt_unlock(sc);
727 * If no error we can return the event loaded into pkt.
734 for (i = 0; i < sizeof(pkt) - 1; ++i)
735 csum += ((uint8_t *)&pkt)[i];
740 error = uiomove((void *)&pkt, n, uio);
747 atmel_mxtwrite(struct dev_write_args *ap)
749 cdev_t dev = ap->a_head.a_dev;
750 atmel_mxt_softc_t *sc = ATMEL_SOFTC(minor(dev));
751 struct uio *uio = ap->a_uio;
758 while (uio->uio_resid) {
759 bzero(&pkt, sizeof(pkt));
763 error = uiomove((void *)&pkt, n, uio);
772 bzero(&sc->pend_rep, sizeof(sc->pend_rep));
773 sc->pend_rep.cmd = 'I';
774 sc->pend_ack = PEND_ACK_RESPOND;
778 * ELO_OWNER request owner
780 bzero(&sc->pend_rep, sizeof(sc->pend_rep));
781 sc->pend_rep.cmd = 'O';
782 sc->pend_ack = PEND_ACK_RESPOND;
786 * ELO_MODE control packet
788 sc->pend_ack = PEND_ACK_ACK;
792 * ELO_REPORT control packet
794 sc->pend_ack = PEND_ACK_ACK;
797 atmel_mxt_unlock(sc);
802 static void atmel_mxt_filt_detach(struct knote *);
803 static int atmel_mxt_filt(struct knote *, long);
805 static struct filterops atmel_mxt_filtops =
806 { FILTEROP_ISFD, NULL, atmel_mxt_filt_detach, atmel_mxt_filt };
809 atmel_mxtkqfilter(struct dev_kqfilter_args *ap)
811 cdev_t dev = ap->a_head.a_dev;
812 atmel_mxt_softc_t *sc = ATMEL_SOFTC(minor(dev));
813 struct knote *kn = ap->a_kn;
816 switch(kn->kn_filter) {
818 kn->kn_fop = &atmel_mxt_filtops;
819 kn->kn_hook = (void *)sc;
823 ap->a_result = EOPNOTSUPP;
826 klist = &sc->kqinfo.ki_note;
827 knote_insert(klist, kn);
833 atmel_mxt_filt_detach(struct knote *kn)
835 atmel_mxt_softc_t *sc = (atmel_mxt_softc_t *)kn->kn_hook;
838 klist = &sc->kqinfo.ki_note;
839 knote_remove(klist, kn);
843 atmel_mxt_filt(struct knote *kn, long hint)
845 atmel_mxt_softc_t *sc = (atmel_mxt_softc_t *)kn->kn_hook;
853 atmel_mxt_unlock(sc);
859 atmel_mxtioctl(struct dev_ioctl_args *ap)
861 cdev_t dev = ap->a_head.a_dev;
862 device_t bus; /* smbbus */
863 /*struct atmel_mxtcmd *s = (struct atmel_mxtcmd *)ap->a_data;*/
865 atmel_mxt_softc_t *sc = ATMEL_SOFTC(minor(dev));
874 * NOTE: smbus_*() functions automatically recurse the parent to
875 * get to the actual device driver.
877 bus = device_get_parent(sc->dev); /* smbus */
879 /* Allocate the bus. */
880 if ((error = smbus_request_bus(bus, sc->dev,
881 (ap->a_fflag & O_NONBLOCK) ?
882 SMB_DONTWAIT : (SMB_WAIT | SMB_INTR))))
888 error = inputev_ioctl(&sc->iev, ap->a_cmd, ap->a_data);
894 smbus_release_bus(bus, sc->dev);
900 * MAJOR SUPPORT FUNCTIONS
904 atmel_mxt_poll_thread(void *arg)
906 atmel_mxt_softc_t *sc = arg;
908 int freq = atmel_mxt_norm_freq;
911 while ((sc->poll_flags & ATMEL_POLL_SHUTDOWN) == 0) {
919 error = mxt_read_object(sc, sc->msgprocobj,
922 isidle = atmel_mxt_raw_input(sc, &msg);
928 * don't let the last_active_tick or last_calibrate_tick
929 * delta calculation overflow.
931 if ((ticks - sc->last_active_tick) > 1000 * hz)
932 sc->last_active_tick = ticks - 1000 * hz;
933 if ((ticks - sc->last_calibrate_tick) > 1000 * hz)
934 sc->last_calibrate_tick = ticks - 1000 * hz;
939 if (atmel_mxt_reset) {
941 atmel_reset_device(sc);
945 * Automatically calibrate when the touchpad has been
946 * idle atmel_mxt_autocalibrate seconds, and recalibrate
947 * on the same interval while it remains idle.
949 * If we don't do this the touchscreen can get really out
950 * of whack over time and basically stop functioning properly.
951 * It's unclear why the device does not do this automatically.
953 * Response occurs in the message stream (which we just
956 if (sc->cmdprocobj && atmel_mxt_autocalibrate &&
957 ((ticks - sc->last_calibrate_tick) >
958 atmel_mxt_autocalibrate * hz) &&
959 ((ticks - sc->last_active_tick) >
960 atmel_mxt_autocalibrate * hz)) {
961 sc->last_calibrate_tick = ticks;
962 mxt_write_object_off(sc, sc->cmdprocobj,
963 MXT_CMDPROC_CALIBRATE_OFF, 1);
965 tsleep(&sc->poll_flags, 0, "atmpol", (hz + freq - 1) / freq);
968 freq = atmel_mxt_idle_freq;
970 freq = atmel_mxt_slow_freq;
972 freq = atmel_mxt_norm_freq;
975 wakeup(&sc->poll_td);
980 atmel_reset_device(atmel_mxt_softc_t *sc)
983 mxt_write_object_off(sc, sc->cmdprocobj, MXT_CMDPROC_RESET_OFF, 1);
984 tsleep(&dummy, 0, "atmel_reset", hz * 2);
988 * Calculate currently active state, if any
992 atmel_find_active_state(atmel_mxt_softc_t *sc)
994 atmel_track_t *track;
997 track = &sc->track[sc->tracking];
998 if (track->report == 0) {
999 for (i = 0; i < ATMEL_MAXTRACK; ++i) {
1000 track = &sc->track[i];
1001 if (track->report) {
1007 if (track->report == 0 && sc->pend_ack == PEND_ACK_NONE) {
1008 sc->data_signal = 0;
1010 sc->data_signal = 1;
1015 * Return non-zero if we are idle
1019 atmel_mxt_raw_input(atmel_mxt_softc_t *sc, mxt_message_t *msg)
1021 atmel_track_t *track;
1024 if (atmel_mxt_debug) {
1025 kprintf("track=%02x f=%s x=%-4d y=%-4d p=%d amp=%d\n",
1027 msgflagsstr(msg->touch.flags),
1028 (msg->touch.pos[0] << 4) |
1029 ((msg->touch.pos[2] >> 4) & 0x0F),
1030 (msg->touch.pos[1] << 4) |
1031 ((msg->touch.pos[2]) & 0x0F),
1033 msg->touch.amplitude);
1038 * If message buffer is empty and no fingers are currently pressed
1039 * return idle, else we are not idle.
1041 if (msg->any.reportid == 0xFF)
1045 * Process message buffer. For now ignore any messages with
1046 * reportids that we do not understand.
1048 * note: reportid==1 typicallk acknowledges calibrations (?)
1050 if (msg->any.reportid < 3 || msg->any.reportid >= ATMEL_MAXTRACK)
1053 sc->last_active_tick = ticks;
1055 track = &sc->track[msg->any.reportid];
1056 track->x = (msg->touch.pos[0] << 4) |
1057 ((msg->touch.pos[2] >> 4) & 0x0F);
1058 track->y = (msg->touch.pos[1] << 4) |
1059 (msg->touch.pos[2] & 0x0F);
1060 track->pressure = msg->touch.amplitude;
1062 track->x = track->x * 3000 / 1361;
1063 track->y = track->y * 3000 / 3064;
1065 if (msg->touch.flags & MXT_MSGF_DETECT) {
1066 track->status = ATMEL_TRACK_PRESSED;
1067 if (msg->touch.flags & MXT_MSGF_PRESS) {
1068 track->report |= ATMEL_REPORT_PRESS;
1070 if (msg->touch.flags & MXT_MSGF_MOVE) {
1071 track->report |= ATMEL_REPORT_MOVE;
1074 track->status = ATMEL_TRACK_RELEASED;
1075 track->report |= ATMEL_REPORT_RELEASE;
1077 atmel_find_active_state(sc);
1080 atmel_mxt_unlock(sc);
1082 atmel_mxt_notify(sc);
1083 if (sc->track_fingers)
1094 mxt_findobject(struct mxt_rollup *core, int type)
1098 for (i = 0; i < core->nobjs; ++i) {
1099 if (core->objs[i].type == type)
1100 return(&core->objs[i]);
1106 mxt_read_reg(atmel_mxt_softc_t *sc, uint16_t reg, void *rbuf, int bytes)
1113 wreg[0] = reg & 255;
1116 bus = device_get_parent(sc->dev);
1117 if ((error = smbus_request_bus(bus, sc->dev, SMB_WAIT | SMB_INTR)) != 0)
1119 error = smbus_trans(bus, sc->addr, 0,
1124 rbuf, bytes, &rbytes);
1125 smbus_release_bus(bus, sc->dev);
1127 if (bytes != rbytes) {
1128 device_printf(sc->dev,
1129 "smbus_trans reg %d short read %d/%d\n",
1130 reg, bytes, rbytes);
1138 mxt_write_reg_buf(atmel_mxt_softc_t *sc, uint16_t reg, void *xbuf, int bytes)
1144 KKASSERT(bytes < sizeof(wbuf) - 2);
1145 wbuf[0] = reg & 255;
1147 bcopy(xbuf, wbuf + 2, bytes);
1149 bus = device_get_parent(sc->dev);
1150 if ((error = smbus_request_bus(bus, sc->dev, SMB_WAIT | SMB_INTR)) != 0)
1152 error = smbus_trans(bus, sc->addr, 0,
1158 smbus_release_bus(bus, sc->dev);
1163 mxt_write_reg(atmel_mxt_softc_t *sc, uint16_t reg, uint8_t val)
1165 return mxt_write_reg_buf(sc, reg, &val, 1);
1169 mxt_read_object(atmel_mxt_softc_t *sc, struct mxt_object *obj,
1170 void *rbuf, int rbytes)
1172 uint16_t reg = obj->start_pos_lsb + (obj->start_pos_msb << 8);
1173 int bytes = obj->size_minus_one + 1;
1177 return mxt_read_reg(sc, reg, rbuf, bytes);
1181 mxt_write_object_off(atmel_mxt_softc_t *sc, struct mxt_object *obj,
1182 int offset, uint8_t val)
1184 uint16_t reg = obj->start_pos_lsb + (obj->start_pos_msb << 8);
1187 return mxt_write_reg(sc, reg, val);
1190 DRIVER_MODULE(atmel_mxt, smbus, atmel_mxt_driver, atmel_mxt_devclass,
1192 MODULE_DEPEND(atmel_mxt, smbus, SMBUS_MINVER, SMBUS_PREFVER, SMBUS_MAXVER);
1193 MODULE_VERSION(atmel_mxt, 1);