balrog | fd5a3b3 | 2007-04-30 02:12:42 +0000 | [diff] [blame] | 1 | /* |
balrog | 8728351 | 2007-11-13 22:52:54 +0000 | [diff] [blame] | 2 | * TI ADS7846 / TSC2046 chip emulation. |
balrog | fd5a3b3 | 2007-04-30 02:12:42 +0000 | [diff] [blame] | 3 | * |
| 4 | * Copyright (c) 2006 Openedhand Ltd. |
| 5 | * Written by Andrzej Zaborowski <balrog@zabor.org> |
| 6 | * |
| 7 | * This code is licensed under the GNU GPL v2. |
Paolo Bonzini | 6b620ca | 2012-01-13 17:44:23 +0100 | [diff] [blame] | 8 | * |
| 9 | * Contributions after 2012-01-13 are licensed under the terms of the |
| 10 | * GNU GPL, version 2 or (at your option) any later version. |
balrog | fd5a3b3 | 2007-04-30 02:12:42 +0000 | [diff] [blame] | 11 | */ |
| 12 | |
Paolo Bonzini | 83c9f4c | 2013-02-04 15:40:22 +0100 | [diff] [blame] | 13 | #include "hw/ssi.h" |
Paolo Bonzini | 28ecbae | 2012-11-28 12:06:30 +0100 | [diff] [blame] | 14 | #include "ui/console.h" |
balrog | fd5a3b3 | 2007-04-30 02:12:42 +0000 | [diff] [blame] | 15 | |
Paul Brook | a984a69 | 2009-05-14 22:35:09 +0100 | [diff] [blame] | 16 | typedef struct { |
| 17 | SSISlave ssidev; |
balrog | fd5a3b3 | 2007-04-30 02:12:42 +0000 | [diff] [blame] | 18 | qemu_irq interrupt; |
| 19 | |
| 20 | int input[8]; |
| 21 | int pressure; |
| 22 | int noise; |
| 23 | |
| 24 | int cycle; |
| 25 | int output; |
Paul Brook | a984a69 | 2009-05-14 22:35:09 +0100 | [diff] [blame] | 26 | } ADS7846State; |
balrog | fd5a3b3 | 2007-04-30 02:12:42 +0000 | [diff] [blame] | 27 | |
| 28 | /* Control-byte bitfields */ |
| 29 | #define CB_PD0 (1 << 0) |
| 30 | #define CB_PD1 (1 << 1) |
| 31 | #define CB_SER (1 << 2) |
| 32 | #define CB_MODE (1 << 3) |
| 33 | #define CB_A0 (1 << 4) |
| 34 | #define CB_A1 (1 << 5) |
| 35 | #define CB_A2 (1 << 6) |
| 36 | #define CB_START (1 << 7) |
| 37 | |
balrog | 611d718 | 2007-06-24 13:45:36 +0000 | [diff] [blame] | 38 | #define X_AXIS_DMAX 3470 |
| 39 | #define X_AXIS_MIN 290 |
| 40 | #define Y_AXIS_DMAX 3450 |
| 41 | #define Y_AXIS_MIN 200 |
balrog | fd5a3b3 | 2007-04-30 02:12:42 +0000 | [diff] [blame] | 42 | |
| 43 | #define ADS_VBAT 2000 |
| 44 | #define ADS_VAUX 2000 |
| 45 | #define ADS_TEMP0 2000 |
| 46 | #define ADS_TEMP1 3000 |
| 47 | #define ADS_XPOS(x, y) (X_AXIS_MIN + ((X_AXIS_DMAX * (x)) >> 15)) |
| 48 | #define ADS_YPOS(x, y) (Y_AXIS_MIN + ((Y_AXIS_DMAX * (y)) >> 15)) |
| 49 | #define ADS_Z1POS(x, y) 600 |
| 50 | #define ADS_Z2POS(x, y) (600 + 6000 / ADS_XPOS(x, y)) |
| 51 | |
Paul Brook | bc24a22 | 2009-05-10 01:44:56 +0100 | [diff] [blame] | 52 | static void ads7846_int_update(ADS7846State *s) |
balrog | fd5a3b3 | 2007-04-30 02:12:42 +0000 | [diff] [blame] | 53 | { |
| 54 | if (s->interrupt) |
| 55 | qemu_set_irq(s->interrupt, s->pressure == 0); |
| 56 | } |
| 57 | |
Paul Brook | a984a69 | 2009-05-14 22:35:09 +0100 | [diff] [blame] | 58 | static uint32_t ads7846_transfer(SSISlave *dev, uint32_t value) |
balrog | fd5a3b3 | 2007-04-30 02:12:42 +0000 | [diff] [blame] | 59 | { |
Paul Brook | a984a69 | 2009-05-14 22:35:09 +0100 | [diff] [blame] | 60 | ADS7846State *s = FROM_SSI_SLAVE(ADS7846State, dev); |
balrog | fd5a3b3 | 2007-04-30 02:12:42 +0000 | [diff] [blame] | 61 | |
| 62 | switch (s->cycle ++) { |
| 63 | case 0: |
| 64 | if (!(value & CB_START)) { |
| 65 | s->cycle = 0; |
| 66 | break; |
| 67 | } |
| 68 | |
| 69 | s->output = s->input[(value >> 4) & 7]; |
| 70 | |
| 71 | /* Imitate the ADC noise, some drivers expect this. */ |
| 72 | s->noise = (s->noise + 3) & 7; |
| 73 | switch ((value >> 4) & 7) { |
| 74 | case 1: s->output += s->noise ^ 2; break; |
| 75 | case 3: s->output += s->noise ^ 0; break; |
| 76 | case 4: s->output += s->noise ^ 7; break; |
| 77 | case 5: s->output += s->noise ^ 5; break; |
| 78 | } |
| 79 | |
| 80 | if (value & CB_MODE) |
| 81 | s->output >>= 4; /* 8 bits instead of 12 */ |
| 82 | |
| 83 | break; |
| 84 | case 1: |
| 85 | s->cycle = 0; |
| 86 | break; |
| 87 | } |
Paul Brook | a984a69 | 2009-05-14 22:35:09 +0100 | [diff] [blame] | 88 | return s->output; |
balrog | fd5a3b3 | 2007-04-30 02:12:42 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | static void ads7846_ts_event(void *opaque, |
| 92 | int x, int y, int z, int buttons_state) |
| 93 | { |
Paul Brook | bc24a22 | 2009-05-10 01:44:56 +0100 | [diff] [blame] | 94 | ADS7846State *s = opaque; |
balrog | fd5a3b3 | 2007-04-30 02:12:42 +0000 | [diff] [blame] | 95 | |
| 96 | if (buttons_state) { |
balrog | 611d718 | 2007-06-24 13:45:36 +0000 | [diff] [blame] | 97 | x = 0x7fff - x; |
| 98 | s->input[1] = ADS_XPOS(x, y); |
balrog | fd5a3b3 | 2007-04-30 02:12:42 +0000 | [diff] [blame] | 99 | s->input[3] = ADS_Z1POS(x, y); |
| 100 | s->input[4] = ADS_Z2POS(x, y); |
balrog | 611d718 | 2007-06-24 13:45:36 +0000 | [diff] [blame] | 101 | s->input[5] = ADS_YPOS(x, y); |
balrog | fd5a3b3 | 2007-04-30 02:12:42 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | if (s->pressure == !buttons_state) { |
| 105 | s->pressure = !!buttons_state; |
| 106 | |
balrog | aa941b9 | 2007-05-24 18:50:09 +0000 | [diff] [blame] | 107 | ads7846_int_update(s); |
balrog | fd5a3b3 | 2007-04-30 02:12:42 +0000 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | |
Juan Quintela | aefe212 | 2010-12-01 22:03:06 +0100 | [diff] [blame] | 111 | static int ads7856_post_load(void *opaque, int version_id) |
balrog | aa941b9 | 2007-05-24 18:50:09 +0000 | [diff] [blame] | 112 | { |
Juan Quintela | aefe212 | 2010-12-01 22:03:06 +0100 | [diff] [blame] | 113 | ADS7846State *s = opaque; |
balrog | aa941b9 | 2007-05-24 18:50:09 +0000 | [diff] [blame] | 114 | |
| 115 | s->pressure = 0; |
| 116 | ads7846_int_update(s); |
balrog | aa941b9 | 2007-05-24 18:50:09 +0000 | [diff] [blame] | 117 | return 0; |
| 118 | } |
| 119 | |
Juan Quintela | aefe212 | 2010-12-01 22:03:06 +0100 | [diff] [blame] | 120 | static const VMStateDescription vmstate_ads7846 = { |
| 121 | .name = "ads7846", |
Peter A. G. Crosthwaite | 6653095 | 2012-07-24 12:23:22 +1000 | [diff] [blame] | 122 | .version_id = 1, |
| 123 | .minimum_version_id = 1, |
Juan Quintela | aefe212 | 2010-12-01 22:03:06 +0100 | [diff] [blame] | 124 | .post_load = ads7856_post_load, |
Juan Quintela | 8f1e884 | 2014-05-13 16:09:35 +0100 | [diff] [blame] | 125 | .fields = (VMStateField[]) { |
Peter A. G. Crosthwaite | 6653095 | 2012-07-24 12:23:22 +1000 | [diff] [blame] | 126 | VMSTATE_SSI_SLAVE(ssidev, ADS7846State), |
Juan Quintela | aefe212 | 2010-12-01 22:03:06 +0100 | [diff] [blame] | 127 | VMSTATE_INT32_ARRAY(input, ADS7846State, 8), |
| 128 | VMSTATE_INT32(noise, ADS7846State), |
| 129 | VMSTATE_INT32(cycle, ADS7846State), |
| 130 | VMSTATE_INT32(output, ADS7846State), |
| 131 | VMSTATE_END_OF_LIST() |
| 132 | } |
| 133 | }; |
| 134 | |
Peter Crosthwaite | 1a7d9ee | 2014-02-11 16:27:50 -0800 | [diff] [blame] | 135 | static int ads7846_init(SSISlave *d) |
balrog | fd5a3b3 | 2007-04-30 02:12:42 +0000 | [diff] [blame] | 136 | { |
Peter Crosthwaite | 1a7d9ee | 2014-02-11 16:27:50 -0800 | [diff] [blame] | 137 | DeviceState *dev = DEVICE(d); |
| 138 | ADS7846State *s = FROM_SSI_SLAVE(ADS7846State, d); |
balrog | fd5a3b3 | 2007-04-30 02:12:42 +0000 | [diff] [blame] | 139 | |
Peter Crosthwaite | 1a7d9ee | 2014-02-11 16:27:50 -0800 | [diff] [blame] | 140 | qdev_init_gpio_out(dev, &s->interrupt, 1); |
balrog | fd5a3b3 | 2007-04-30 02:12:42 +0000 | [diff] [blame] | 141 | |
| 142 | s->input[0] = ADS_TEMP0; /* TEMP0 */ |
| 143 | s->input[2] = ADS_VBAT; /* VBAT */ |
| 144 | s->input[6] = ADS_VAUX; /* VAUX */ |
| 145 | s->input[7] = ADS_TEMP1; /* TEMP1 */ |
| 146 | |
| 147 | /* We want absolute coordinates */ |
| 148 | qemu_add_mouse_event_handler(ads7846_ts_event, s, 1, |
| 149 | "QEMU ADS7846-driven Touchscreen"); |
| 150 | |
| 151 | ads7846_int_update(s); |
balrog | aa941b9 | 2007-05-24 18:50:09 +0000 | [diff] [blame] | 152 | |
Juan Quintela | aefe212 | 2010-12-01 22:03:06 +0100 | [diff] [blame] | 153 | vmstate_register(NULL, -1, &vmstate_ads7846, s); |
Gerd Hoffmann | 81a322d | 2009-08-14 10:36:05 +0200 | [diff] [blame] | 154 | return 0; |
balrog | fd5a3b3 | 2007-04-30 02:12:42 +0000 | [diff] [blame] | 155 | } |
Paul Brook | a984a69 | 2009-05-14 22:35:09 +0100 | [diff] [blame] | 156 | |
Anthony Liguori | cd6c4cf | 2011-12-16 13:36:39 -0600 | [diff] [blame] | 157 | static void ads7846_class_init(ObjectClass *klass, void *data) |
| 158 | { |
| 159 | SSISlaveClass *k = SSI_SLAVE_CLASS(klass); |
| 160 | |
| 161 | k->init = ads7846_init; |
| 162 | k->transfer = ads7846_transfer; |
| 163 | } |
| 164 | |
Andreas Färber | 8c43a6f | 2013-01-10 16:19:07 +0100 | [diff] [blame] | 165 | static const TypeInfo ads7846_info = { |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 166 | .name = "ads7846", |
| 167 | .parent = TYPE_SSI_SLAVE, |
| 168 | .instance_size = sizeof(ADS7846State), |
| 169 | .class_init = ads7846_class_init, |
Paul Brook | a984a69 | 2009-05-14 22:35:09 +0100 | [diff] [blame] | 170 | }; |
| 171 | |
Andreas Färber | 83f7d43 | 2012-02-09 15:20:55 +0100 | [diff] [blame] | 172 | static void ads7846_register_types(void) |
Paul Brook | a984a69 | 2009-05-14 22:35:09 +0100 | [diff] [blame] | 173 | { |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 174 | type_register_static(&ads7846_info); |
Paul Brook | a984a69 | 2009-05-14 22:35:09 +0100 | [diff] [blame] | 175 | } |
| 176 | |
Andreas Färber | 83f7d43 | 2012-02-09 15:20:55 +0100 | [diff] [blame] | 177 | type_init(ads7846_register_types) |