xref: /openbmc/qemu/hw/input/pxa2xx_keypad.c (revision 8f1e884b)
1 /*
2  * Intel PXA27X Keypad Controller emulation.
3  *
4  * Copyright (c) 2007 MontaVista Software, Inc
5  * Written by Armin Kuster <akuster@kama-aina.net>
6  *              or  <Akuster@mvista.com>
7  *
8  * This code is licensed under the GPLv2.
9  *
10  * Contributions after 2012-01-13 are licensed under the terms of the
11  * GNU GPL, version 2 or (at your option) any later version.
12  */
13 
14 #include "hw/hw.h"
15 #include "hw/arm/pxa.h"
16 #include "ui/console.h"
17 
18 /*
19  * Keypad
20  */
21 #define KPC         0x00    /* Keypad Interface Control register */
22 #define KPDK        0x08    /* Keypad Interface Direct Key register */
23 #define KPREC       0x10    /* Keypad Interface Rotary Encoder register */
24 #define KPMK        0x18    /* Keypad Interface Matrix Key register */
25 #define KPAS        0x20    /* Keypad Interface Automatic Scan register */
26 #define KPASMKP0    0x28    /* Keypad Interface Automatic Scan Multiple
27                                 Key Presser register 0 */
28 #define KPASMKP1    0x30    /* Keypad Interface Automatic Scan Multiple
29                                 Key Presser register 1 */
30 #define KPASMKP2    0x38    /* Keypad Interface Automatic Scan Multiple
31                                 Key Presser register 2 */
32 #define KPASMKP3    0x40    /* Keypad Interface Automatic Scan Multiple
33                                 Key Presser register 3 */
34 #define KPKDI       0x48    /* Keypad Interface Key Debounce Interval
35                                 register */
36 
37 /* Keypad defines */
38 #define KPC_AS          (0x1 << 30)  /* Automatic Scan bit */
39 #define KPC_ASACT       (0x1 << 29)  /* Automatic Scan on Activity */
40 #define KPC_MI          (0x1 << 22)  /* Matrix interrupt bit */
41 #define KPC_IMKP        (0x1 << 21)  /* Ignore Multiple Key Press */
42 #define KPC_MS7         (0x1 << 20)  /* Matrix scan line 7 */
43 #define KPC_MS6         (0x1 << 19)  /* Matrix scan line 6 */
44 #define KPC_MS5         (0x1 << 18)  /* Matrix scan line 5 */
45 #define KPC_MS4         (0x1 << 17)  /* Matrix scan line 4 */
46 #define KPC_MS3         (0x1 << 16)  /* Matrix scan line 3 */
47 #define KPC_MS2         (0x1 << 15)  /* Matrix scan line 2 */
48 #define KPC_MS1         (0x1 << 14)  /* Matrix scan line 1 */
49 #define KPC_MS0         (0x1 << 13)  /* Matrix scan line 0 */
50 #define KPC_ME          (0x1 << 12)  /* Matrix Keypad Enable */
51 #define KPC_MIE         (0x1 << 11)  /* Matrix Interrupt Enable */
52 #define KPC_DK_DEB_SEL  (0x1 <<  9)  /* Direct Keypad Debounce Select */
53 #define KPC_DI          (0x1 <<  5)  /* Direct key interrupt bit */
54 #define KPC_RE_ZERO_DEB (0x1 <<  4)  /* Rotary Encoder Zero Debounce */
55 #define KPC_REE1        (0x1 <<  3)  /* Rotary Encoder1 Enable */
56 #define KPC_REE0        (0x1 <<  2)  /* Rotary Encoder0 Enable */
57 #define KPC_DE          (0x1 <<  1)  /* Direct Keypad Enable */
58 #define KPC_DIE         (0x1 <<  0)  /* Direct Keypad interrupt Enable */
59 
60 #define KPDK_DKP        (0x1 << 31)
61 #define KPDK_DK7        (0x1 <<  7)
62 #define KPDK_DK6        (0x1 <<  6)
63 #define KPDK_DK5        (0x1 <<  5)
64 #define KPDK_DK4        (0x1 <<  4)
65 #define KPDK_DK3        (0x1 <<  3)
66 #define KPDK_DK2        (0x1 <<  2)
67 #define KPDK_DK1        (0x1 <<  1)
68 #define KPDK_DK0        (0x1 <<  0)
69 
70 #define KPREC_OF1       (0x1 << 31)
71 #define KPREC_UF1       (0x1 << 30)
72 #define KPREC_OF0       (0x1 << 15)
73 #define KPREC_UF0       (0x1 << 14)
74 
75 #define KPMK_MKP        (0x1 << 31)
76 #define KPAS_SO         (0x1 << 31)
77 #define KPASMKPx_SO     (0x1 << 31)
78 
79 
80 #define KPASMKPx_MKC(row, col)  (1 << (row + 16 * (col % 2)))
81 
82 #define PXAKBD_MAXROW   8
83 #define PXAKBD_MAXCOL   8
84 
85 struct PXA2xxKeyPadState {
86     MemoryRegion iomem;
87     qemu_irq    irq;
88     const struct  keymap *map;
89     int         pressed_cnt;
90     int         alt_code;
91 
92     uint32_t    kpc;
93     uint32_t    kpdk;
94     uint32_t    kprec;
95     uint32_t    kpmk;
96     uint32_t    kpas;
97     uint32_t    kpasmkp[4];
98     uint32_t    kpkdi;
99 };
100 
101 static void pxa27x_keypad_find_pressed_key(PXA2xxKeyPadState *kp, int *row, int *col)
102 {
103     int i;
104     for (i = 0; i < 4; i++)
105     {
106         *col = i * 2;
107         for (*row = 0; *row < 8; (*row)++) {
108             if (kp->kpasmkp[i] & (1 << *row))
109                 return;
110         }
111         *col = i * 2 + 1;
112         for (*row = 0; *row < 8; (*row)++) {
113             if (kp->kpasmkp[i] & (1 << (*row + 16)))
114                 return;
115         }
116     }
117 }
118 
119 static void pxa27x_keyboard_event (PXA2xxKeyPadState *kp, int keycode)
120 {
121     int row, col, rel, assert_irq = 0;
122     uint32_t val;
123 
124     if (keycode == 0xe0) {
125         kp->alt_code = 1;
126         return;
127     }
128 
129     if(!(kp->kpc & KPC_ME)) /* skip if not enabled */
130         return;
131 
132     rel = (keycode & 0x80) ? 1 : 0; /* key release from qemu */
133     keycode &= ~0x80; /* strip qemu key release bit */
134     if (kp->alt_code) {
135         keycode |= 0x80;
136         kp->alt_code = 0;
137     }
138 
139     row = kp->map[keycode].row;
140     col = kp->map[keycode].column;
141     if (row == -1 || col == -1) {
142         return;
143     }
144 
145     val = KPASMKPx_MKC(row, col);
146     if (rel) {
147         if (kp->kpasmkp[col / 2] & val) {
148             kp->kpasmkp[col / 2] &= ~val;
149             kp->pressed_cnt--;
150             assert_irq = 1;
151         }
152     } else {
153         if (!(kp->kpasmkp[col / 2] & val)) {
154             kp->kpasmkp[col / 2] |= val;
155             kp->pressed_cnt++;
156             assert_irq = 1;
157         }
158     }
159     kp->kpas = ((kp->pressed_cnt & 0x1f) << 26) | (0xf << 4) | 0xf;
160     if (kp->pressed_cnt == 1) {
161         kp->kpas &= ~((0xf << 4) | 0xf);
162         if (rel) {
163             pxa27x_keypad_find_pressed_key(kp, &row, &col);
164         }
165         kp->kpas |= ((row & 0xf) << 4) | (col & 0xf);
166     }
167 
168     if (!(kp->kpc & (KPC_AS | KPC_ASACT)))
169         assert_irq = 0;
170 
171     if (assert_irq && (kp->kpc & KPC_MIE)) {
172         kp->kpc |= KPC_MI;
173         qemu_irq_raise(kp->irq);
174     }
175 }
176 
177 static uint64_t pxa2xx_keypad_read(void *opaque, hwaddr offset,
178                                    unsigned size)
179 {
180     PXA2xxKeyPadState *s = (PXA2xxKeyPadState *) opaque;
181     uint32_t tmp;
182 
183     switch (offset) {
184     case KPC:
185         tmp = s->kpc;
186         if(tmp & KPC_MI)
187             s->kpc &= ~(KPC_MI);
188         if(tmp & KPC_DI)
189             s->kpc &= ~(KPC_DI);
190         qemu_irq_lower(s->irq);
191         return tmp;
192         break;
193     case KPDK:
194         return s->kpdk;
195         break;
196     case KPREC:
197         tmp = s->kprec;
198         if(tmp & KPREC_OF1)
199             s->kprec &= ~(KPREC_OF1);
200         if(tmp & KPREC_UF1)
201             s->kprec &= ~(KPREC_UF1);
202         if(tmp & KPREC_OF0)
203             s->kprec &= ~(KPREC_OF0);
204         if(tmp & KPREC_UF0)
205             s->kprec &= ~(KPREC_UF0);
206         return tmp;
207         break;
208     case KPMK:
209         tmp = s->kpmk;
210         if(tmp & KPMK_MKP)
211             s->kpmk &= ~(KPMK_MKP);
212         return tmp;
213         break;
214     case KPAS:
215         return s->kpas;
216         break;
217     case KPASMKP0:
218         return s->kpasmkp[0];
219         break;
220     case KPASMKP1:
221         return s->kpasmkp[1];
222         break;
223     case KPASMKP2:
224         return s->kpasmkp[2];
225         break;
226     case KPASMKP3:
227         return s->kpasmkp[3];
228         break;
229     case KPKDI:
230         return s->kpkdi;
231         break;
232     default:
233         hw_error("%s: Bad offset " REG_FMT "\n", __FUNCTION__, offset);
234     }
235 
236     return 0;
237 }
238 
239 static void pxa2xx_keypad_write(void *opaque, hwaddr offset,
240                                 uint64_t value, unsigned size)
241 {
242     PXA2xxKeyPadState *s = (PXA2xxKeyPadState *) opaque;
243 
244     switch (offset) {
245     case KPC:
246         s->kpc = value;
247         if (s->kpc & KPC_AS) {
248             s->kpc &= ~(KPC_AS);
249         }
250         break;
251     case KPDK:
252         s->kpdk = value;
253         break;
254     case KPREC:
255         s->kprec = value;
256         break;
257     case KPMK:
258         s->kpmk = value;
259         break;
260     case KPAS:
261         s->kpas = value;
262         break;
263     case KPASMKP0:
264         s->kpasmkp[0] = value;
265         break;
266     case KPASMKP1:
267         s->kpasmkp[1] = value;
268         break;
269     case KPASMKP2:
270         s->kpasmkp[2] = value;
271         break;
272     case KPASMKP3:
273         s->kpasmkp[3] = value;
274         break;
275     case KPKDI:
276         s->kpkdi = value;
277         break;
278 
279     default:
280         hw_error("%s: Bad offset " REG_FMT "\n", __FUNCTION__, offset);
281     }
282 }
283 
284 static const MemoryRegionOps pxa2xx_keypad_ops = {
285     .read = pxa2xx_keypad_read,
286     .write = pxa2xx_keypad_write,
287     .endianness = DEVICE_NATIVE_ENDIAN,
288 };
289 
290 static const VMStateDescription vmstate_pxa2xx_keypad = {
291     .name = "pxa2xx_keypad",
292     .version_id = 0,
293     .minimum_version_id = 0,
294     .fields = (VMStateField[]) {
295         VMSTATE_UINT32(kpc, PXA2xxKeyPadState),
296         VMSTATE_UINT32(kpdk, PXA2xxKeyPadState),
297         VMSTATE_UINT32(kprec, PXA2xxKeyPadState),
298         VMSTATE_UINT32(kpmk, PXA2xxKeyPadState),
299         VMSTATE_UINT32(kpas, PXA2xxKeyPadState),
300         VMSTATE_UINT32_ARRAY(kpasmkp, PXA2xxKeyPadState, 4),
301         VMSTATE_UINT32(kpkdi, PXA2xxKeyPadState),
302         VMSTATE_END_OF_LIST()
303     }
304 };
305 
306 PXA2xxKeyPadState *pxa27x_keypad_init(MemoryRegion *sysmem,
307                                       hwaddr base,
308                                       qemu_irq irq)
309 {
310     PXA2xxKeyPadState *s;
311 
312     s = (PXA2xxKeyPadState *) g_malloc0(sizeof(PXA2xxKeyPadState));
313     s->irq = irq;
314 
315     memory_region_init_io(&s->iomem, NULL, &pxa2xx_keypad_ops, s,
316                           "pxa2xx-keypad", 0x00100000);
317     memory_region_add_subregion(sysmem, base, &s->iomem);
318 
319     vmstate_register(NULL, 0, &vmstate_pxa2xx_keypad, s);
320 
321     return s;
322 }
323 
324 void pxa27x_register_keypad(PXA2xxKeyPadState *kp,
325                             const struct keymap *map, int size)
326 {
327     if(!map || size < 0x80) {
328         fprintf(stderr, "%s - No PXA keypad map defined\n", __FUNCTION__);
329         exit(-1);
330     }
331 
332     kp->map = map;
333     qemu_add_kbd_event_handler((QEMUPutKBDEvent *) pxa27x_keyboard_event, kp);
334 }
335