]>
Commit | Line | Data |
---|---|---|
5141d415 CLG |
1 | /* |
2 | * PCA9552 I2C LED blinker | |
3 | * | |
4 | * https://www.nxp.com/docs/en/application-note/AN264.pdf | |
5 | * | |
6 | * Copyright (c) 2017-2018, IBM Corporation. | |
7 | * | |
8 | * This work is licensed under the terms of the GNU GPL, version 2 or | |
9 | * later. See the COPYING file in the top-level directory. | |
10 | */ | |
11 | ||
12 | #include "qemu/osdep.h" | |
13 | #include "qemu/log.h" | |
0b8fa32f | 14 | #include "qemu/module.h" |
5141d415 CLG |
15 | #include "hw/misc/pca9552.h" |
16 | #include "hw/misc/pca9552_regs.h" | |
d6454270 | 17 | #include "migration/vmstate.h" |
a90d8f84 JS |
18 | #include "qapi/error.h" |
19 | #include "qapi/visitor.h" | |
5141d415 CLG |
20 | |
21 | #define PCA9552_LED_ON 0x0 | |
22 | #define PCA9552_LED_OFF 0x1 | |
23 | #define PCA9552_LED_PWM0 0x2 | |
24 | #define PCA9552_LED_PWM1 0x3 | |
25 | ||
a90d8f84 JS |
26 | static const char *led_state[] = {"on", "off", "pwm0", "pwm1"}; |
27 | ||
5141d415 CLG |
28 | static uint8_t pca9552_pin_get_config(PCA9552State *s, int pin) |
29 | { | |
30 | uint8_t reg = PCA9552_LS0 + (pin / 4); | |
31 | uint8_t shift = (pin % 4) << 1; | |
32 | ||
33 | return extract32(s->regs[reg], shift, 2); | |
34 | } | |
35 | ||
36 | static void pca9552_update_pin_input(PCA9552State *s) | |
37 | { | |
38 | int i; | |
39 | ||
40 | for (i = 0; i < s->nr_leds; i++) { | |
41 | uint8_t input_reg = PCA9552_INPUT0 + (i / 8); | |
42 | uint8_t input_shift = (i % 8); | |
43 | uint8_t config = pca9552_pin_get_config(s, i); | |
44 | ||
45 | switch (config) { | |
46 | case PCA9552_LED_ON: | |
47 | s->regs[input_reg] |= 1 << input_shift; | |
48 | break; | |
49 | case PCA9552_LED_OFF: | |
50 | s->regs[input_reg] &= ~(1 << input_shift); | |
51 | break; | |
52 | case PCA9552_LED_PWM0: | |
53 | case PCA9552_LED_PWM1: | |
54 | /* TODO */ | |
55 | default: | |
56 | break; | |
57 | } | |
58 | } | |
59 | } | |
60 | ||
61 | static uint8_t pca9552_read(PCA9552State *s, uint8_t reg) | |
62 | { | |
63 | switch (reg) { | |
64 | case PCA9552_INPUT0: | |
65 | case PCA9552_INPUT1: | |
66 | case PCA9552_PSC0: | |
67 | case PCA9552_PWM0: | |
68 | case PCA9552_PSC1: | |
69 | case PCA9552_PWM1: | |
70 | case PCA9552_LS0: | |
71 | case PCA9552_LS1: | |
72 | case PCA9552_LS2: | |
73 | case PCA9552_LS3: | |
74 | return s->regs[reg]; | |
75 | default: | |
76 | qemu_log_mask(LOG_GUEST_ERROR, "%s: unexpected read to register %d\n", | |
77 | __func__, reg); | |
78 | return 0xFF; | |
79 | } | |
80 | } | |
81 | ||
82 | static void pca9552_write(PCA9552State *s, uint8_t reg, uint8_t data) | |
83 | { | |
84 | switch (reg) { | |
85 | case PCA9552_PSC0: | |
86 | case PCA9552_PWM0: | |
87 | case PCA9552_PSC1: | |
88 | case PCA9552_PWM1: | |
89 | s->regs[reg] = data; | |
90 | break; | |
91 | ||
92 | case PCA9552_LS0: | |
93 | case PCA9552_LS1: | |
94 | case PCA9552_LS2: | |
95 | case PCA9552_LS3: | |
96 | s->regs[reg] = data; | |
97 | pca9552_update_pin_input(s); | |
98 | break; | |
99 | ||
100 | case PCA9552_INPUT0: | |
101 | case PCA9552_INPUT1: | |
102 | default: | |
103 | qemu_log_mask(LOG_GUEST_ERROR, "%s: unexpected write to register %d\n", | |
104 | __func__, reg); | |
105 | } | |
106 | } | |
107 | ||
108 | /* | |
109 | * When Auto-Increment is on, the register address is incremented | |
110 | * after each byte is sent to or received by the device. The index | |
111 | * rollovers to 0 when the maximum register address is reached. | |
112 | */ | |
113 | static void pca9552_autoinc(PCA9552State *s) | |
114 | { | |
115 | if (s->pointer != 0xFF && s->pointer & PCA9552_AUTOINC) { | |
116 | uint8_t reg = s->pointer & 0xf; | |
117 | ||
118 | reg = (reg + 1) % (s->max_reg + 1); | |
119 | s->pointer = reg | PCA9552_AUTOINC; | |
120 | } | |
121 | } | |
122 | ||
2ac4c5f4 | 123 | static uint8_t pca9552_recv(I2CSlave *i2c) |
5141d415 CLG |
124 | { |
125 | PCA9552State *s = PCA9552(i2c); | |
126 | uint8_t ret; | |
127 | ||
128 | ret = pca9552_read(s, s->pointer & 0xf); | |
129 | ||
130 | /* | |
131 | * From the Specs: | |
132 | * | |
133 | * Important Note: When a Read sequence is initiated and the | |
134 | * AI bit is set to Logic Level 1, the Read Sequence MUST | |
135 | * start by a register different from 0. | |
136 | * | |
137 | * I don't know what should be done in this case, so throw an | |
138 | * error. | |
139 | */ | |
140 | if (s->pointer == PCA9552_AUTOINC) { | |
141 | qemu_log_mask(LOG_GUEST_ERROR, | |
142 | "%s: Autoincrement read starting with register 0\n", | |
143 | __func__); | |
144 | } | |
145 | ||
146 | pca9552_autoinc(s); | |
147 | ||
148 | return ret; | |
149 | } | |
150 | ||
151 | static int pca9552_send(I2CSlave *i2c, uint8_t data) | |
152 | { | |
153 | PCA9552State *s = PCA9552(i2c); | |
154 | ||
155 | /* First byte sent by is the register address */ | |
156 | if (s->len == 0) { | |
157 | s->pointer = data; | |
158 | s->len++; | |
159 | } else { | |
160 | pca9552_write(s, s->pointer & 0xf, data); | |
161 | ||
162 | pca9552_autoinc(s); | |
163 | } | |
164 | ||
165 | return 0; | |
166 | } | |
167 | ||
168 | static int pca9552_event(I2CSlave *i2c, enum i2c_event event) | |
169 | { | |
170 | PCA9552State *s = PCA9552(i2c); | |
171 | ||
172 | s->len = 0; | |
173 | return 0; | |
174 | } | |
175 | ||
a90d8f84 JS |
176 | static void pca9552_get_led(Object *obj, Visitor *v, const char *name, |
177 | void *opaque, Error **errp) | |
178 | { | |
179 | PCA9552State *s = PCA9552(obj); | |
180 | int led, rc, reg; | |
181 | uint8_t state; | |
182 | ||
183 | rc = sscanf(name, "led%2d", &led); | |
184 | if (rc != 1) { | |
185 | error_setg(errp, "%s: error reading %s", __func__, name); | |
186 | return; | |
187 | } | |
188 | if (led < 0 || led > s->nr_leds) { | |
189 | error_setg(errp, "%s invalid led %s", __func__, name); | |
190 | return; | |
191 | } | |
192 | /* | |
193 | * Get the LSx register as the qom interface should expose the device | |
194 | * state, not the modeled 'input line' behaviour which would come from | |
195 | * reading the INPUTx reg | |
196 | */ | |
197 | reg = PCA9552_LS0 + led / 4; | |
198 | state = (pca9552_read(s, reg) >> (led % 8)) & 0x3; | |
199 | visit_type_str(v, name, (char **)&led_state[state], errp); | |
200 | } | |
201 | ||
202 | /* | |
203 | * Return an LED selector register value based on an existing one, with | |
204 | * the appropriate 2-bit state value set for the given LED number (0-3). | |
205 | */ | |
206 | static inline uint8_t pca955x_ledsel(uint8_t oldval, int led_num, int state) | |
207 | { | |
208 | return (oldval & (~(0x3 << (led_num << 1)))) | | |
209 | ((state & 0x3) << (led_num << 1)); | |
210 | } | |
211 | ||
212 | static void pca9552_set_led(Object *obj, Visitor *v, const char *name, | |
213 | void *opaque, Error **errp) | |
214 | { | |
215 | PCA9552State *s = PCA9552(obj); | |
216 | Error *local_err = NULL; | |
217 | int led, rc, reg, val; | |
218 | uint8_t state; | |
219 | char *state_str; | |
220 | ||
221 | visit_type_str(v, name, &state_str, &local_err); | |
222 | if (local_err) { | |
223 | error_propagate(errp, local_err); | |
224 | return; | |
225 | } | |
226 | rc = sscanf(name, "led%2d", &led); | |
227 | if (rc != 1) { | |
228 | error_setg(errp, "%s: error reading %s", __func__, name); | |
229 | return; | |
230 | } | |
231 | if (led < 0 || led > s->nr_leds) { | |
232 | error_setg(errp, "%s invalid led %s", __func__, name); | |
233 | return; | |
234 | } | |
235 | ||
236 | for (state = 0; state < ARRAY_SIZE(led_state); state++) { | |
237 | if (!strcmp(state_str, led_state[state])) { | |
238 | break; | |
239 | } | |
240 | } | |
241 | if (state >= ARRAY_SIZE(led_state)) { | |
242 | error_setg(errp, "%s invalid led state %s", __func__, state_str); | |
243 | return; | |
244 | } | |
245 | ||
246 | reg = PCA9552_LS0 + led / 4; | |
247 | val = pca9552_read(s, reg); | |
248 | val = pca955x_ledsel(val, led % 4, state); | |
249 | pca9552_write(s, reg, val); | |
250 | } | |
251 | ||
5141d415 CLG |
252 | static const VMStateDescription pca9552_vmstate = { |
253 | .name = "PCA9552", | |
254 | .version_id = 0, | |
255 | .minimum_version_id = 0, | |
256 | .fields = (VMStateField[]) { | |
257 | VMSTATE_UINT8(len, PCA9552State), | |
258 | VMSTATE_UINT8(pointer, PCA9552State), | |
259 | VMSTATE_UINT8_ARRAY(regs, PCA9552State, PCA9552_NR_REGS), | |
260 | VMSTATE_I2C_SLAVE(i2c, PCA9552State), | |
261 | VMSTATE_END_OF_LIST() | |
262 | } | |
263 | }; | |
264 | ||
265 | static void pca9552_reset(DeviceState *dev) | |
266 | { | |
267 | PCA9552State *s = PCA9552(dev); | |
268 | ||
269 | s->regs[PCA9552_PSC0] = 0xFF; | |
270 | s->regs[PCA9552_PWM0] = 0x80; | |
271 | s->regs[PCA9552_PSC1] = 0xFF; | |
272 | s->regs[PCA9552_PWM1] = 0x80; | |
273 | s->regs[PCA9552_LS0] = 0x55; /* all OFF */ | |
274 | s->regs[PCA9552_LS1] = 0x55; | |
275 | s->regs[PCA9552_LS2] = 0x55; | |
276 | s->regs[PCA9552_LS3] = 0x55; | |
277 | ||
278 | pca9552_update_pin_input(s); | |
279 | ||
280 | s->pointer = 0xFF; | |
281 | s->len = 0; | |
282 | } | |
283 | ||
284 | static void pca9552_initfn(Object *obj) | |
285 | { | |
286 | PCA9552State *s = PCA9552(obj); | |
a90d8f84 | 287 | int led; |
5141d415 CLG |
288 | |
289 | /* If support for the other PCA955X devices are implemented, these | |
290 | * constant values might be part of class structure describing the | |
291 | * PCA955X device | |
292 | */ | |
293 | s->max_reg = PCA9552_LS3; | |
294 | s->nr_leds = 16; | |
a90d8f84 JS |
295 | |
296 | for (led = 0; led < s->nr_leds; led++) { | |
297 | char *name; | |
298 | ||
299 | name = g_strdup_printf("led%d", led); | |
300 | object_property_add(obj, name, "bool", pca9552_get_led, pca9552_set_led, | |
301 | NULL, NULL, NULL); | |
302 | g_free(name); | |
303 | } | |
5141d415 CLG |
304 | } |
305 | ||
306 | static void pca9552_class_init(ObjectClass *klass, void *data) | |
307 | { | |
308 | DeviceClass *dc = DEVICE_CLASS(klass); | |
309 | I2CSlaveClass *k = I2C_SLAVE_CLASS(klass); | |
310 | ||
311 | k->event = pca9552_event; | |
312 | k->recv = pca9552_recv; | |
313 | k->send = pca9552_send; | |
314 | dc->reset = pca9552_reset; | |
315 | dc->vmsd = &pca9552_vmstate; | |
316 | } | |
317 | ||
318 | static const TypeInfo pca9552_info = { | |
319 | .name = TYPE_PCA9552, | |
320 | .parent = TYPE_I2C_SLAVE, | |
321 | .instance_init = pca9552_initfn, | |
322 | .instance_size = sizeof(PCA9552State), | |
323 | .class_init = pca9552_class_init, | |
324 | }; | |
325 | ||
326 | static void pca9552_register_types(void) | |
327 | { | |
328 | type_register_static(&pca9552_info); | |
329 | } | |
330 | ||
331 | type_init(pca9552_register_types) |