2 * TI TWL92230C energy-management companion device for the OMAP24xx.
3 * Aka. Menelaus (N4200 MENELAUS1_V2.2)
5 * Copyright (C) 2008 Nokia Corporation
6 * Written by Andrzej Zaborowski <andrew@openedhand.com>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 or
11 * (at your option) version 3 of the License.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, see <http://www.gnu.org/licenses/>.
22 #include "qemu/osdep.h"
23 #include "qemu-common.h"
24 #include "qemu/timer.h"
25 #include "hw/i2c/i2c.h"
27 #include "migration/qemu-file-types.h"
28 #include "migration/vmstate.h"
29 #include "sysemu/sysemu.h"
30 #include "ui/console.h"
32 #include "qemu/module.h"
36 #define TYPE_TWL92230 "twl92230"
37 #define TWL92230(obj) OBJECT_CHECK(MenelausState, (obj), TYPE_TWL92230)
39 typedef struct MenelausState
{
72 uint16_t rtc_next_vmstate
;
77 static inline void menelaus_update(MenelausState
*s
)
79 qemu_set_irq(s
->out
[3], s
->status
& ~s
->mask
);
82 static inline void menelaus_rtc_start(MenelausState
*s
)
84 s
->rtc
.next
+= qemu_clock_get_ms(rtc_clock
);
85 timer_mod(s
->rtc
.hz_tm
, s
->rtc
.next
);
88 static inline void menelaus_rtc_stop(MenelausState
*s
)
90 timer_del(s
->rtc
.hz_tm
);
91 s
->rtc
.next
-= qemu_clock_get_ms(rtc_clock
);
96 static void menelaus_rtc_update(MenelausState
*s
)
98 qemu_get_timedate(&s
->rtc
.tm
, s
->rtc
.sec_offset
);
101 static void menelaus_alm_update(MenelausState
*s
)
103 if ((s
->rtc
.ctrl
& 3) == 3)
104 s
->rtc
.alm_sec
= qemu_timedate_diff(&s
->rtc
.alm
) - s
->rtc
.sec_offset
;
107 static void menelaus_rtc_hz(void *opaque
)
109 MenelausState
*s
= (MenelausState
*) opaque
;
114 timer_mod(s
->rtc
.hz_tm
, s
->rtc
.next
);
115 if ((s
->rtc
.ctrl
>> 3) & 3) { /* EVERY */
116 menelaus_rtc_update(s
);
117 if (((s
->rtc
.ctrl
>> 3) & 3) == 1 && !s
->rtc
.tm
.tm_sec
)
118 s
->status
|= 1 << 8; /* RTCTMR */
119 else if (((s
->rtc
.ctrl
>> 3) & 3) == 2 && !s
->rtc
.tm
.tm_min
)
120 s
->status
|= 1 << 8; /* RTCTMR */
121 else if (!s
->rtc
.tm
.tm_hour
)
122 s
->status
|= 1 << 8; /* RTCTMR */
124 s
->status
|= 1 << 8; /* RTCTMR */
125 if ((s
->rtc
.ctrl
>> 1) & 1) { /* RTC_AL_EN */
126 if (s
->rtc
.alm_sec
== 0)
127 s
->status
|= 1 << 9; /* RTCALM */
130 if (s
->rtc
.next_comp
<= 0) {
131 s
->rtc
.next
-= muldiv64((int16_t) s
->rtc
.comp
, 1000, 0x8000);
132 s
->rtc
.next_comp
= 3600;
137 static void menelaus_reset(I2CSlave
*i2c
)
139 MenelausState
*s
= TWL92230(i2c
);
143 s
->vcore
[0] = 0x0c; /* XXX: X-loader needs 0x8c? check! */
148 s
->dcdc
[0] = 0x33; /* Depends on wiring */
154 s
->ldo
[3] = 0x00; /* Depends on wiring */
155 s
->ldo
[4] = 0x03; /* Depends on wiring */
172 s
->mmc_ctrl
[0] = 0x03;
173 s
->mmc_ctrl
[1] = 0xc0;
174 s
->mmc_ctrl
[2] = 0x00;
175 s
->mmc_debounce
= 0x05;
178 menelaus_rtc_stop(s
);
180 s
->rtc
.comp
= 0x0000;
182 s
->rtc
.sec_offset
= 0;
183 s
->rtc
.next_comp
= 1800;
184 s
->rtc
.alm_sec
= 1800;
185 s
->rtc
.alm
.tm_sec
= 0x00;
186 s
->rtc
.alm
.tm_min
= 0x00;
187 s
->rtc
.alm
.tm_hour
= 0x00;
188 s
->rtc
.alm
.tm_mday
= 0x01;
189 s
->rtc
.alm
.tm_mon
= 0x00;
190 s
->rtc
.alm
.tm_year
= 2004;
194 static void menelaus_gpio_set(void *opaque
, int line
, int level
)
196 MenelausState
*s
= (MenelausState
*) opaque
;
199 /* No interrupt generated */
200 s
->inputs
&= ~(1 << line
);
201 s
->inputs
|= level
<< line
;
205 if (!s
->pwrbtn_state
&& level
) {
206 s
->status
|= 1 << 11; /* PSHBTN */
209 s
->pwrbtn_state
= level
;
212 #define MENELAUS_REV 0x01
213 #define MENELAUS_VCORE_CTRL1 0x02
214 #define MENELAUS_VCORE_CTRL2 0x03
215 #define MENELAUS_VCORE_CTRL3 0x04
216 #define MENELAUS_VCORE_CTRL4 0x05
217 #define MENELAUS_VCORE_CTRL5 0x06
218 #define MENELAUS_DCDC_CTRL1 0x07
219 #define MENELAUS_DCDC_CTRL2 0x08
220 #define MENELAUS_DCDC_CTRL3 0x09
221 #define MENELAUS_LDO_CTRL1 0x0a
222 #define MENELAUS_LDO_CTRL2 0x0b
223 #define MENELAUS_LDO_CTRL3 0x0c
224 #define MENELAUS_LDO_CTRL4 0x0d
225 #define MENELAUS_LDO_CTRL5 0x0e
226 #define MENELAUS_LDO_CTRL6 0x0f
227 #define MENELAUS_LDO_CTRL7 0x10
228 #define MENELAUS_LDO_CTRL8 0x11
229 #define MENELAUS_SLEEP_CTRL1 0x12
230 #define MENELAUS_SLEEP_CTRL2 0x13
231 #define MENELAUS_DEVICE_OFF 0x14
232 #define MENELAUS_OSC_CTRL 0x15
233 #define MENELAUS_DETECT_CTRL 0x16
234 #define MENELAUS_INT_MASK1 0x17
235 #define MENELAUS_INT_MASK2 0x18
236 #define MENELAUS_INT_STATUS1 0x19
237 #define MENELAUS_INT_STATUS2 0x1a
238 #define MENELAUS_INT_ACK1 0x1b
239 #define MENELAUS_INT_ACK2 0x1c
240 #define MENELAUS_GPIO_CTRL 0x1d
241 #define MENELAUS_GPIO_IN 0x1e
242 #define MENELAUS_GPIO_OUT 0x1f
243 #define MENELAUS_BBSMS 0x20
244 #define MENELAUS_RTC_CTRL 0x21
245 #define MENELAUS_RTC_UPDATE 0x22
246 #define MENELAUS_RTC_SEC 0x23
247 #define MENELAUS_RTC_MIN 0x24
248 #define MENELAUS_RTC_HR 0x25
249 #define MENELAUS_RTC_DAY 0x26
250 #define MENELAUS_RTC_MON 0x27
251 #define MENELAUS_RTC_YR 0x28
252 #define MENELAUS_RTC_WKDAY 0x29
253 #define MENELAUS_RTC_AL_SEC 0x2a
254 #define MENELAUS_RTC_AL_MIN 0x2b
255 #define MENELAUS_RTC_AL_HR 0x2c
256 #define MENELAUS_RTC_AL_DAY 0x2d
257 #define MENELAUS_RTC_AL_MON 0x2e
258 #define MENELAUS_RTC_AL_YR 0x2f
259 #define MENELAUS_RTC_COMP_MSB 0x30
260 #define MENELAUS_RTC_COMP_LSB 0x31
261 #define MENELAUS_S1_PULL_EN 0x32
262 #define MENELAUS_S1_PULL_DIR 0x33
263 #define MENELAUS_S2_PULL_EN 0x34
264 #define MENELAUS_S2_PULL_DIR 0x35
265 #define MENELAUS_MCT_CTRL1 0x36
266 #define MENELAUS_MCT_CTRL2 0x37
267 #define MENELAUS_MCT_CTRL3 0x38
268 #define MENELAUS_MCT_PIN_ST 0x39
269 #define MENELAUS_DEBOUNCE1 0x3a
271 static uint8_t menelaus_read(void *opaque
, uint8_t addr
)
273 MenelausState
*s
= (MenelausState
*) opaque
;
280 case MENELAUS_VCORE_CTRL5
: reg
++;
281 case MENELAUS_VCORE_CTRL4
: reg
++;
282 case MENELAUS_VCORE_CTRL3
: reg
++;
283 case MENELAUS_VCORE_CTRL2
: reg
++;
284 case MENELAUS_VCORE_CTRL1
:
285 return s
->vcore
[reg
];
287 case MENELAUS_DCDC_CTRL3
: reg
++;
288 case MENELAUS_DCDC_CTRL2
: reg
++;
289 case MENELAUS_DCDC_CTRL1
:
292 case MENELAUS_LDO_CTRL8
: reg
++;
293 case MENELAUS_LDO_CTRL7
: reg
++;
294 case MENELAUS_LDO_CTRL6
: reg
++;
295 case MENELAUS_LDO_CTRL5
: reg
++;
296 case MENELAUS_LDO_CTRL4
: reg
++;
297 case MENELAUS_LDO_CTRL3
: reg
++;
298 case MENELAUS_LDO_CTRL2
: reg
++;
299 case MENELAUS_LDO_CTRL1
:
302 case MENELAUS_SLEEP_CTRL2
: reg
++;
303 case MENELAUS_SLEEP_CTRL1
:
304 return s
->sleep
[reg
];
306 case MENELAUS_DEVICE_OFF
:
309 case MENELAUS_OSC_CTRL
:
310 return s
->osc
| (1 << 7); /* CLK32K_GOOD */
312 case MENELAUS_DETECT_CTRL
:
315 case MENELAUS_INT_MASK1
:
316 return (s
->mask
>> 0) & 0xff;
317 case MENELAUS_INT_MASK2
:
318 return (s
->mask
>> 8) & 0xff;
320 case MENELAUS_INT_STATUS1
:
321 return (s
->status
>> 0) & 0xff;
322 case MENELAUS_INT_STATUS2
:
323 return (s
->status
>> 8) & 0xff;
325 case MENELAUS_INT_ACK1
:
326 case MENELAUS_INT_ACK2
:
329 case MENELAUS_GPIO_CTRL
:
331 case MENELAUS_GPIO_IN
:
332 return s
->inputs
| (~s
->dir
& s
->outputs
);
333 case MENELAUS_GPIO_OUT
:
339 case MENELAUS_RTC_CTRL
:
341 case MENELAUS_RTC_UPDATE
:
343 case MENELAUS_RTC_SEC
:
344 menelaus_rtc_update(s
);
345 return to_bcd(s
->rtc
.tm
.tm_sec
);
346 case MENELAUS_RTC_MIN
:
347 menelaus_rtc_update(s
);
348 return to_bcd(s
->rtc
.tm
.tm_min
);
349 case MENELAUS_RTC_HR
:
350 menelaus_rtc_update(s
);
351 if ((s
->rtc
.ctrl
>> 2) & 1) /* MODE12_n24 */
352 return to_bcd((s
->rtc
.tm
.tm_hour
% 12) + 1) |
353 (!!(s
->rtc
.tm
.tm_hour
>= 12) << 7); /* PM_nAM */
355 return to_bcd(s
->rtc
.tm
.tm_hour
);
356 case MENELAUS_RTC_DAY
:
357 menelaus_rtc_update(s
);
358 return to_bcd(s
->rtc
.tm
.tm_mday
);
359 case MENELAUS_RTC_MON
:
360 menelaus_rtc_update(s
);
361 return to_bcd(s
->rtc
.tm
.tm_mon
+ 1);
362 case MENELAUS_RTC_YR
:
363 menelaus_rtc_update(s
);
364 return to_bcd(s
->rtc
.tm
.tm_year
- 2000);
365 case MENELAUS_RTC_WKDAY
:
366 menelaus_rtc_update(s
);
367 return to_bcd(s
->rtc
.tm
.tm_wday
);
368 case MENELAUS_RTC_AL_SEC
:
369 return to_bcd(s
->rtc
.alm
.tm_sec
);
370 case MENELAUS_RTC_AL_MIN
:
371 return to_bcd(s
->rtc
.alm
.tm_min
);
372 case MENELAUS_RTC_AL_HR
:
373 if ((s
->rtc
.ctrl
>> 2) & 1) /* MODE12_n24 */
374 return to_bcd((s
->rtc
.alm
.tm_hour
% 12) + 1) |
375 (!!(s
->rtc
.alm
.tm_hour
>= 12) << 7);/* AL_PM_nAM */
377 return to_bcd(s
->rtc
.alm
.tm_hour
);
378 case MENELAUS_RTC_AL_DAY
:
379 return to_bcd(s
->rtc
.alm
.tm_mday
);
380 case MENELAUS_RTC_AL_MON
:
381 return to_bcd(s
->rtc
.alm
.tm_mon
+ 1);
382 case MENELAUS_RTC_AL_YR
:
383 return to_bcd(s
->rtc
.alm
.tm_year
- 2000);
384 case MENELAUS_RTC_COMP_MSB
:
385 return (s
->rtc
.comp
>> 8) & 0xff;
386 case MENELAUS_RTC_COMP_LSB
:
387 return (s
->rtc
.comp
>> 0) & 0xff;
389 case MENELAUS_S1_PULL_EN
:
391 case MENELAUS_S1_PULL_DIR
:
393 case MENELAUS_S2_PULL_EN
:
395 case MENELAUS_S2_PULL_DIR
:
398 case MENELAUS_MCT_CTRL3
: reg
++;
399 case MENELAUS_MCT_CTRL2
: reg
++;
400 case MENELAUS_MCT_CTRL1
:
401 return s
->mmc_ctrl
[reg
];
402 case MENELAUS_MCT_PIN_ST
:
403 /* TODO: return the real Card Detect */
405 case MENELAUS_DEBOUNCE1
:
406 return s
->mmc_debounce
;
410 printf("%s: unknown register %02x\n", __func__
, addr
);
417 static void menelaus_write(void *opaque
, uint8_t addr
, uint8_t value
)
419 MenelausState
*s
= (MenelausState
*) opaque
;
425 case MENELAUS_VCORE_CTRL1
:
426 s
->vcore
[0] = (value
& 0xe) | MIN(value
& 0x1f, 0x12);
428 case MENELAUS_VCORE_CTRL2
:
431 case MENELAUS_VCORE_CTRL3
:
432 s
->vcore
[2] = MIN(value
& 0x1f, 0x12);
434 case MENELAUS_VCORE_CTRL4
:
435 s
->vcore
[3] = MIN(value
& 0x1f, 0x12);
437 case MENELAUS_VCORE_CTRL5
:
438 s
->vcore
[4] = value
& 3;
440 * auto set to 3 on M_Active, nRESWARM
441 * auto set to 0 on M_WaitOn, M_Backup
445 case MENELAUS_DCDC_CTRL1
:
446 s
->dcdc
[0] = value
& 0x3f;
448 case MENELAUS_DCDC_CTRL2
:
449 s
->dcdc
[1] = value
& 0x07;
451 * auto set to 3 on M_Active, nRESWARM
452 * auto set to 0 on M_WaitOn, M_Backup
455 case MENELAUS_DCDC_CTRL3
:
456 s
->dcdc
[2] = value
& 0x07;
459 case MENELAUS_LDO_CTRL1
:
462 case MENELAUS_LDO_CTRL2
:
463 s
->ldo
[1] = value
& 0x7f;
465 * auto set to 0x7e on M_WaitOn, M_Backup
468 case MENELAUS_LDO_CTRL3
:
469 s
->ldo
[2] = value
& 3;
471 * auto set to 3 on M_Active, nRESWARM
472 * auto set to 0 on M_WaitOn, M_Backup
475 case MENELAUS_LDO_CTRL4
:
476 s
->ldo
[3] = value
& 3;
478 * auto set to 3 on M_Active, nRESWARM
479 * auto set to 0 on M_WaitOn, M_Backup
482 case MENELAUS_LDO_CTRL5
:
483 s
->ldo
[4] = value
& 3;
485 * auto set to 3 on M_Active, nRESWARM
486 * auto set to 0 on M_WaitOn, M_Backup
489 case MENELAUS_LDO_CTRL6
:
490 s
->ldo
[5] = value
& 3;
492 case MENELAUS_LDO_CTRL7
:
493 s
->ldo
[6] = value
& 3;
495 case MENELAUS_LDO_CTRL8
:
496 s
->ldo
[7] = value
& 3;
499 case MENELAUS_SLEEP_CTRL2
: reg
++;
500 case MENELAUS_SLEEP_CTRL1
:
501 s
->sleep
[reg
] = value
;
504 case MENELAUS_DEVICE_OFF
:
506 menelaus_reset(I2C_SLAVE(s
));
510 case MENELAUS_OSC_CTRL
:
514 case MENELAUS_DETECT_CTRL
:
515 s
->detect
= value
& 0x7f;
518 case MENELAUS_INT_MASK1
:
520 s
->mask
|= value
<< 0;
523 case MENELAUS_INT_MASK2
:
525 s
->mask
|= value
<< 8;
529 case MENELAUS_INT_ACK1
:
530 s
->status
&= ~(((uint16_t) value
) << 0);
533 case MENELAUS_INT_ACK2
:
534 s
->status
&= ~(((uint16_t) value
) << 8);
538 case MENELAUS_GPIO_CTRL
:
539 for (line
= 0; line
< 3; line
++) {
540 if (((s
->dir
^ value
) >> line
) & 1) {
541 qemu_set_irq(s
->out
[line
],
542 ((s
->outputs
& ~s
->dir
) >> line
) & 1);
545 s
->dir
= value
& 0x67;
547 case MENELAUS_GPIO_OUT
:
548 for (line
= 0; line
< 3; line
++) {
549 if ((((s
->outputs
^ value
) & ~s
->dir
) >> line
) & 1) {
550 qemu_set_irq(s
->out
[line
], (s
->outputs
>> line
) & 1);
553 s
->outputs
= value
& 0x07;
560 case MENELAUS_RTC_CTRL
:
561 if ((s
->rtc
.ctrl
^ value
) & 1) { /* RTC_EN */
563 menelaus_rtc_start(s
);
565 menelaus_rtc_stop(s
);
567 s
->rtc
.ctrl
= value
& 0x1f;
568 menelaus_alm_update(s
);
570 case MENELAUS_RTC_UPDATE
:
571 menelaus_rtc_update(s
);
572 memcpy(&tm
, &s
->rtc
.tm
, sizeof(tm
));
573 switch (value
& 0xf) {
577 tm
.tm_sec
= s
->rtc
.new.tm_sec
;
580 tm
.tm_min
= s
->rtc
.new.tm_min
;
583 if (s
->rtc
.new.tm_hour
> 23)
585 tm
.tm_hour
= s
->rtc
.new.tm_hour
;
588 if (s
->rtc
.new.tm_mday
< 1)
590 /* TODO check range */
591 tm
.tm_mday
= s
->rtc
.new.tm_mday
;
594 if (s
->rtc
.new.tm_mon
< 0 || s
->rtc
.new.tm_mon
> 11)
596 tm
.tm_mon
= s
->rtc
.new.tm_mon
;
599 tm
.tm_year
= s
->rtc
.new.tm_year
;
602 /* TODO set .tm_mday instead */
603 tm
.tm_wday
= s
->rtc
.new.tm_wday
;
606 if (s
->rtc
.new.tm_hour
> 23)
608 if (s
->rtc
.new.tm_mday
< 1)
610 if (s
->rtc
.new.tm_mon
< 0 || s
->rtc
.new.tm_mon
> 11)
612 tm
.tm_sec
= s
->rtc
.new.tm_sec
;
613 tm
.tm_min
= s
->rtc
.new.tm_min
;
614 tm
.tm_hour
= s
->rtc
.new.tm_hour
;
615 tm
.tm_mday
= s
->rtc
.new.tm_mday
;
616 tm
.tm_mon
= s
->rtc
.new.tm_mon
;
617 tm
.tm_year
= s
->rtc
.new.tm_year
;
621 fprintf(stderr
, "%s: bad RTC_UPDATE value %02x\n",
623 s
->status
|= 1 << 10; /* RTCERR */
626 s
->rtc
.sec_offset
= qemu_timedate_diff(&tm
);
628 case MENELAUS_RTC_SEC
:
629 s
->rtc
.tm
.tm_sec
= from_bcd(value
& 0x7f);
631 case MENELAUS_RTC_MIN
:
632 s
->rtc
.tm
.tm_min
= from_bcd(value
& 0x7f);
634 case MENELAUS_RTC_HR
:
635 s
->rtc
.tm
.tm_hour
= (s
->rtc
.ctrl
& (1 << 2)) ? /* MODE12_n24 */
636 MIN(from_bcd(value
& 0x3f), 12) + ((value
>> 7) ? 11 : -1) :
637 from_bcd(value
& 0x3f);
639 case MENELAUS_RTC_DAY
:
640 s
->rtc
.tm
.tm_mday
= from_bcd(value
);
642 case MENELAUS_RTC_MON
:
643 s
->rtc
.tm
.tm_mon
= MAX(1, from_bcd(value
)) - 1;
645 case MENELAUS_RTC_YR
:
646 s
->rtc
.tm
.tm_year
= 2000 + from_bcd(value
);
648 case MENELAUS_RTC_WKDAY
:
649 s
->rtc
.tm
.tm_mday
= from_bcd(value
);
651 case MENELAUS_RTC_AL_SEC
:
652 s
->rtc
.alm
.tm_sec
= from_bcd(value
& 0x7f);
653 menelaus_alm_update(s
);
655 case MENELAUS_RTC_AL_MIN
:
656 s
->rtc
.alm
.tm_min
= from_bcd(value
& 0x7f);
657 menelaus_alm_update(s
);
659 case MENELAUS_RTC_AL_HR
:
660 s
->rtc
.alm
.tm_hour
= (s
->rtc
.ctrl
& (1 << 2)) ? /* MODE12_n24 */
661 MIN(from_bcd(value
& 0x3f), 12) + ((value
>> 7) ? 11 : -1) :
662 from_bcd(value
& 0x3f);
663 menelaus_alm_update(s
);
665 case MENELAUS_RTC_AL_DAY
:
666 s
->rtc
.alm
.tm_mday
= from_bcd(value
);
667 menelaus_alm_update(s
);
669 case MENELAUS_RTC_AL_MON
:
670 s
->rtc
.alm
.tm_mon
= MAX(1, from_bcd(value
)) - 1;
671 menelaus_alm_update(s
);
673 case MENELAUS_RTC_AL_YR
:
674 s
->rtc
.alm
.tm_year
= 2000 + from_bcd(value
);
675 menelaus_alm_update(s
);
677 case MENELAUS_RTC_COMP_MSB
:
679 s
->rtc
.comp
|= value
<< 8;
681 case MENELAUS_RTC_COMP_LSB
:
682 s
->rtc
.comp
&= 0xff << 8;
683 s
->rtc
.comp
|= value
;
686 case MENELAUS_S1_PULL_EN
:
689 case MENELAUS_S1_PULL_DIR
:
690 s
->pull
[1] = value
& 0x1f;
692 case MENELAUS_S2_PULL_EN
:
695 case MENELAUS_S2_PULL_DIR
:
696 s
->pull
[3] = value
& 0x1f;
699 case MENELAUS_MCT_CTRL1
:
700 s
->mmc_ctrl
[0] = value
& 0x7f;
702 case MENELAUS_MCT_CTRL2
:
703 s
->mmc_ctrl
[1] = value
;
704 /* TODO update Card Detect interrupts */
706 case MENELAUS_MCT_CTRL3
:
707 s
->mmc_ctrl
[2] = value
& 0xf;
709 case MENELAUS_DEBOUNCE1
:
710 s
->mmc_debounce
= value
& 0x3f;
715 printf("%s: unknown register %02x\n", __func__
, addr
);
720 static int menelaus_event(I2CSlave
*i2c
, enum i2c_event event
)
722 MenelausState
*s
= TWL92230(i2c
);
724 if (event
== I2C_START_SEND
)
730 static int menelaus_tx(I2CSlave
*i2c
, uint8_t data
)
732 MenelausState
*s
= TWL92230(i2c
);
734 /* Interpret register address byte */
739 menelaus_write(s
, s
->reg
++, data
);
744 static uint8_t menelaus_rx(I2CSlave
*i2c
)
746 MenelausState
*s
= TWL92230(i2c
);
748 return menelaus_read(s
, s
->reg
++);
751 /* Save restore 32 bit int as uint16_t
752 This is a Big hack, but it is how the old state did it.
753 Or we broke compatibility in the state, or we can't use struct tm
756 static int get_int32_as_uint16(QEMUFile
*f
, void *pv
, size_t size
,
757 const VMStateField
*field
)
760 *v
= qemu_get_be16(f
);
764 static int put_int32_as_uint16(QEMUFile
*f
, void *pv
, size_t size
,
765 const VMStateField
*field
, QJSON
*vmdesc
)
768 qemu_put_be16(f
, *v
);
773 static const VMStateInfo vmstate_hack_int32_as_uint16
= {
774 .name
= "int32_as_uint16",
775 .get
= get_int32_as_uint16
,
776 .put
= put_int32_as_uint16
,
779 #define VMSTATE_UINT16_HACK(_f, _s) \
780 VMSTATE_SINGLE(_f, _s, 0, vmstate_hack_int32_as_uint16, int32_t)
783 static const VMStateDescription vmstate_menelaus_tm
= {
784 .name
= "menelaus_tm",
786 .minimum_version_id
= 0,
787 .fields
= (VMStateField
[]) {
788 VMSTATE_UINT16_HACK(tm_sec
, struct tm
),
789 VMSTATE_UINT16_HACK(tm_min
, struct tm
),
790 VMSTATE_UINT16_HACK(tm_hour
, struct tm
),
791 VMSTATE_UINT16_HACK(tm_mday
, struct tm
),
792 VMSTATE_UINT16_HACK(tm_min
, struct tm
),
793 VMSTATE_UINT16_HACK(tm_year
, struct tm
),
794 VMSTATE_END_OF_LIST()
798 static int menelaus_pre_save(void *opaque
)
800 MenelausState
*s
= opaque
;
801 /* Should be <= 1000 */
802 s
->rtc_next_vmstate
= s
->rtc
.next
- qemu_clock_get_ms(rtc_clock
);
807 static int menelaus_post_load(void *opaque
, int version_id
)
809 MenelausState
*s
= opaque
;
811 if (s
->rtc
.ctrl
& 1) /* RTC_EN */
812 menelaus_rtc_stop(s
);
814 s
->rtc
.next
= s
->rtc_next_vmstate
;
816 menelaus_alm_update(s
);
818 if (s
->rtc
.ctrl
& 1) /* RTC_EN */
819 menelaus_rtc_start(s
);
823 static const VMStateDescription vmstate_menelaus
= {
826 .minimum_version_id
= 0,
827 .pre_save
= menelaus_pre_save
,
828 .post_load
= menelaus_post_load
,
829 .fields
= (VMStateField
[]) {
830 VMSTATE_INT32(firstbyte
, MenelausState
),
831 VMSTATE_UINT8(reg
, MenelausState
),
832 VMSTATE_UINT8_ARRAY(vcore
, MenelausState
, 5),
833 VMSTATE_UINT8_ARRAY(dcdc
, MenelausState
, 3),
834 VMSTATE_UINT8_ARRAY(ldo
, MenelausState
, 8),
835 VMSTATE_UINT8_ARRAY(sleep
, MenelausState
, 2),
836 VMSTATE_UINT8(osc
, MenelausState
),
837 VMSTATE_UINT8(detect
, MenelausState
),
838 VMSTATE_UINT16(mask
, MenelausState
),
839 VMSTATE_UINT16(status
, MenelausState
),
840 VMSTATE_UINT8(dir
, MenelausState
),
841 VMSTATE_UINT8(inputs
, MenelausState
),
842 VMSTATE_UINT8(outputs
, MenelausState
),
843 VMSTATE_UINT8(bbsms
, MenelausState
),
844 VMSTATE_UINT8_ARRAY(pull
, MenelausState
, 4),
845 VMSTATE_UINT8_ARRAY(mmc_ctrl
, MenelausState
, 3),
846 VMSTATE_UINT8(mmc_debounce
, MenelausState
),
847 VMSTATE_UINT8(rtc
.ctrl
, MenelausState
),
848 VMSTATE_UINT16(rtc
.comp
, MenelausState
),
849 VMSTATE_UINT16(rtc_next_vmstate
, MenelausState
),
850 VMSTATE_STRUCT(rtc
.new, MenelausState
, 0, vmstate_menelaus_tm
,
852 VMSTATE_STRUCT(rtc
.alm
, MenelausState
, 0, vmstate_menelaus_tm
,
854 VMSTATE_UINT8(pwrbtn_state
, MenelausState
),
855 VMSTATE_I2C_SLAVE(parent_obj
, MenelausState
),
856 VMSTATE_END_OF_LIST()
860 static void twl92230_realize(DeviceState
*dev
, Error
**errp
)
862 MenelausState
*s
= TWL92230(dev
);
864 s
->rtc
.hz_tm
= timer_new_ms(rtc_clock
, menelaus_rtc_hz
, s
);
865 /* Three output pins plus one interrupt pin. */
866 qdev_init_gpio_out(dev
, s
->out
, 4);
868 /* Three input pins plus one power-button pin. */
869 qdev_init_gpio_in(dev
, menelaus_gpio_set
, 4);
871 menelaus_reset(I2C_SLAVE(dev
));
874 static void twl92230_class_init(ObjectClass
*klass
, void *data
)
876 DeviceClass
*dc
= DEVICE_CLASS(klass
);
877 I2CSlaveClass
*sc
= I2C_SLAVE_CLASS(klass
);
879 dc
->realize
= twl92230_realize
;
880 sc
->event
= menelaus_event
;
881 sc
->recv
= menelaus_rx
;
882 sc
->send
= menelaus_tx
;
883 dc
->vmsd
= &vmstate_menelaus
;
886 static const TypeInfo twl92230_info
= {
887 .name
= TYPE_TWL92230
,
888 .parent
= TYPE_I2C_SLAVE
,
889 .instance_size
= sizeof(MenelausState
),
890 .class_init
= twl92230_class_init
,
893 static void twl92230_register_types(void)
895 type_register_static(&twl92230_info
);
898 type_init(twl92230_register_types
)