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/timer.h"
24 #include "hw/i2c/i2c.h"
26 #include "migration/qemu-file-types.h"
27 #include "migration/vmstate.h"
28 #include "sysemu/sysemu.h"
29 #include "sysemu/rtc.h"
31 #include "qemu/module.h"
32 #include "qom/object.h"
36 #define TYPE_TWL92230 "twl92230"
37 OBJECT_DECLARE_SIMPLE_TYPE(MenelausState
, TWL92230
)
39 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
;
279 case MENELAUS_VCORE_CTRL1
... MENELAUS_VCORE_CTRL5
:
280 return s
->vcore
[addr
- MENELAUS_VCORE_CTRL1
];
282 case MENELAUS_DCDC_CTRL1
... MENELAUS_DCDC_CTRL3
:
283 return s
->dcdc
[addr
- MENELAUS_DCDC_CTRL1
];
285 case MENELAUS_LDO_CTRL1
... MENELAUS_LDO_CTRL8
:
286 return s
->ldo
[addr
- MENELAUS_LDO_CTRL1
];
288 case MENELAUS_SLEEP_CTRL1
:
289 case MENELAUS_SLEEP_CTRL2
:
290 return s
->sleep
[addr
- MENELAUS_SLEEP_CTRL1
];
292 case MENELAUS_DEVICE_OFF
:
295 case MENELAUS_OSC_CTRL
:
296 return s
->osc
| (1 << 7); /* CLK32K_GOOD */
298 case MENELAUS_DETECT_CTRL
:
301 case MENELAUS_INT_MASK1
:
302 return (s
->mask
>> 0) & 0xff;
303 case MENELAUS_INT_MASK2
:
304 return (s
->mask
>> 8) & 0xff;
306 case MENELAUS_INT_STATUS1
:
307 return (s
->status
>> 0) & 0xff;
308 case MENELAUS_INT_STATUS2
:
309 return (s
->status
>> 8) & 0xff;
311 case MENELAUS_INT_ACK1
:
312 case MENELAUS_INT_ACK2
:
315 case MENELAUS_GPIO_CTRL
:
317 case MENELAUS_GPIO_IN
:
318 return s
->inputs
| (~s
->dir
& s
->outputs
);
319 case MENELAUS_GPIO_OUT
:
325 case MENELAUS_RTC_CTRL
:
327 case MENELAUS_RTC_UPDATE
:
329 case MENELAUS_RTC_SEC
:
330 menelaus_rtc_update(s
);
331 return to_bcd(s
->rtc
.tm
.tm_sec
);
332 case MENELAUS_RTC_MIN
:
333 menelaus_rtc_update(s
);
334 return to_bcd(s
->rtc
.tm
.tm_min
);
335 case MENELAUS_RTC_HR
:
336 menelaus_rtc_update(s
);
337 if ((s
->rtc
.ctrl
>> 2) & 1) /* MODE12_n24 */
338 return to_bcd((s
->rtc
.tm
.tm_hour
% 12) + 1) |
339 (!!(s
->rtc
.tm
.tm_hour
>= 12) << 7); /* PM_nAM */
341 return to_bcd(s
->rtc
.tm
.tm_hour
);
342 case MENELAUS_RTC_DAY
:
343 menelaus_rtc_update(s
);
344 return to_bcd(s
->rtc
.tm
.tm_mday
);
345 case MENELAUS_RTC_MON
:
346 menelaus_rtc_update(s
);
347 return to_bcd(s
->rtc
.tm
.tm_mon
+ 1);
348 case MENELAUS_RTC_YR
:
349 menelaus_rtc_update(s
);
350 return to_bcd(s
->rtc
.tm
.tm_year
- 2000);
351 case MENELAUS_RTC_WKDAY
:
352 menelaus_rtc_update(s
);
353 return to_bcd(s
->rtc
.tm
.tm_wday
);
354 case MENELAUS_RTC_AL_SEC
:
355 return to_bcd(s
->rtc
.alm
.tm_sec
);
356 case MENELAUS_RTC_AL_MIN
:
357 return to_bcd(s
->rtc
.alm
.tm_min
);
358 case MENELAUS_RTC_AL_HR
:
359 if ((s
->rtc
.ctrl
>> 2) & 1) /* MODE12_n24 */
360 return to_bcd((s
->rtc
.alm
.tm_hour
% 12) + 1) |
361 (!!(s
->rtc
.alm
.tm_hour
>= 12) << 7);/* AL_PM_nAM */
363 return to_bcd(s
->rtc
.alm
.tm_hour
);
364 case MENELAUS_RTC_AL_DAY
:
365 return to_bcd(s
->rtc
.alm
.tm_mday
);
366 case MENELAUS_RTC_AL_MON
:
367 return to_bcd(s
->rtc
.alm
.tm_mon
+ 1);
368 case MENELAUS_RTC_AL_YR
:
369 return to_bcd(s
->rtc
.alm
.tm_year
- 2000);
370 case MENELAUS_RTC_COMP_MSB
:
371 return (s
->rtc
.comp
>> 8) & 0xff;
372 case MENELAUS_RTC_COMP_LSB
:
373 return (s
->rtc
.comp
>> 0) & 0xff;
375 case MENELAUS_S1_PULL_EN
:
377 case MENELAUS_S1_PULL_DIR
:
379 case MENELAUS_S2_PULL_EN
:
381 case MENELAUS_S2_PULL_DIR
:
384 case MENELAUS_MCT_CTRL1
... MENELAUS_MCT_CTRL3
:
385 return s
->mmc_ctrl
[addr
- MENELAUS_MCT_CTRL1
];
386 case MENELAUS_MCT_PIN_ST
:
387 /* TODO: return the real Card Detect */
389 case MENELAUS_DEBOUNCE1
:
390 return s
->mmc_debounce
;
394 printf("%s: unknown register %02x\n", __func__
, addr
);
401 static void menelaus_write(void *opaque
, uint8_t addr
, uint8_t value
)
403 MenelausState
*s
= (MenelausState
*) opaque
;
408 case MENELAUS_VCORE_CTRL1
:
409 s
->vcore
[0] = (value
& 0xe) | MIN(value
& 0x1f, 0x12);
411 case MENELAUS_VCORE_CTRL2
:
414 case MENELAUS_VCORE_CTRL3
:
415 s
->vcore
[2] = MIN(value
& 0x1f, 0x12);
417 case MENELAUS_VCORE_CTRL4
:
418 s
->vcore
[3] = MIN(value
& 0x1f, 0x12);
420 case MENELAUS_VCORE_CTRL5
:
421 s
->vcore
[4] = value
& 3;
423 * auto set to 3 on M_Active, nRESWARM
424 * auto set to 0 on M_WaitOn, M_Backup
428 case MENELAUS_DCDC_CTRL1
:
429 s
->dcdc
[0] = value
& 0x3f;
431 case MENELAUS_DCDC_CTRL2
:
432 s
->dcdc
[1] = value
& 0x07;
434 * auto set to 3 on M_Active, nRESWARM
435 * auto set to 0 on M_WaitOn, M_Backup
438 case MENELAUS_DCDC_CTRL3
:
439 s
->dcdc
[2] = value
& 0x07;
442 case MENELAUS_LDO_CTRL1
:
445 case MENELAUS_LDO_CTRL2
:
446 s
->ldo
[1] = value
& 0x7f;
448 * auto set to 0x7e on M_WaitOn, M_Backup
451 case MENELAUS_LDO_CTRL3
:
452 s
->ldo
[2] = value
& 3;
454 * auto set to 3 on M_Active, nRESWARM
455 * auto set to 0 on M_WaitOn, M_Backup
458 case MENELAUS_LDO_CTRL4
:
459 s
->ldo
[3] = value
& 3;
461 * auto set to 3 on M_Active, nRESWARM
462 * auto set to 0 on M_WaitOn, M_Backup
465 case MENELAUS_LDO_CTRL5
:
466 s
->ldo
[4] = value
& 3;
468 * auto set to 3 on M_Active, nRESWARM
469 * auto set to 0 on M_WaitOn, M_Backup
472 case MENELAUS_LDO_CTRL6
:
473 s
->ldo
[5] = value
& 3;
475 case MENELAUS_LDO_CTRL7
:
476 s
->ldo
[6] = value
& 3;
478 case MENELAUS_LDO_CTRL8
:
479 s
->ldo
[7] = value
& 3;
482 case MENELAUS_SLEEP_CTRL1
:
483 case MENELAUS_SLEEP_CTRL2
:
484 s
->sleep
[addr
- MENELAUS_SLEEP_CTRL1
] = value
;
487 case MENELAUS_DEVICE_OFF
:
489 menelaus_reset(I2C_SLAVE(s
));
493 case MENELAUS_OSC_CTRL
:
497 case MENELAUS_DETECT_CTRL
:
498 s
->detect
= value
& 0x7f;
501 case MENELAUS_INT_MASK1
:
503 s
->mask
|= value
<< 0;
506 case MENELAUS_INT_MASK2
:
508 s
->mask
|= value
<< 8;
512 case MENELAUS_INT_ACK1
:
513 s
->status
&= ~(((uint16_t) value
) << 0);
516 case MENELAUS_INT_ACK2
:
517 s
->status
&= ~(((uint16_t) value
) << 8);
521 case MENELAUS_GPIO_CTRL
:
522 for (line
= 0; line
< 3; line
++) {
523 if (((s
->dir
^ value
) >> line
) & 1) {
524 qemu_set_irq(s
->out
[line
],
525 ((s
->outputs
& ~s
->dir
) >> line
) & 1);
528 s
->dir
= value
& 0x67;
530 case MENELAUS_GPIO_OUT
:
531 for (line
= 0; line
< 3; line
++) {
532 if ((((s
->outputs
^ value
) & ~s
->dir
) >> line
) & 1) {
533 qemu_set_irq(s
->out
[line
], (s
->outputs
>> line
) & 1);
536 s
->outputs
= value
& 0x07;
543 case MENELAUS_RTC_CTRL
:
544 if ((s
->rtc
.ctrl
^ value
) & 1) { /* RTC_EN */
546 menelaus_rtc_start(s
);
548 menelaus_rtc_stop(s
);
550 s
->rtc
.ctrl
= value
& 0x1f;
551 menelaus_alm_update(s
);
553 case MENELAUS_RTC_UPDATE
:
554 menelaus_rtc_update(s
);
555 memcpy(&tm
, &s
->rtc
.tm
, sizeof(tm
));
556 switch (value
& 0xf) {
560 tm
.tm_sec
= s
->rtc
.new.tm_sec
;
563 tm
.tm_min
= s
->rtc
.new.tm_min
;
566 if (s
->rtc
.new.tm_hour
> 23)
568 tm
.tm_hour
= s
->rtc
.new.tm_hour
;
571 if (s
->rtc
.new.tm_mday
< 1)
573 /* TODO check range */
574 tm
.tm_mday
= s
->rtc
.new.tm_mday
;
577 if (s
->rtc
.new.tm_mon
< 0 || s
->rtc
.new.tm_mon
> 11)
579 tm
.tm_mon
= s
->rtc
.new.tm_mon
;
582 tm
.tm_year
= s
->rtc
.new.tm_year
;
585 /* TODO set .tm_mday instead */
586 tm
.tm_wday
= s
->rtc
.new.tm_wday
;
589 if (s
->rtc
.new.tm_hour
> 23)
591 if (s
->rtc
.new.tm_mday
< 1)
593 if (s
->rtc
.new.tm_mon
< 0 || s
->rtc
.new.tm_mon
> 11)
595 tm
.tm_sec
= s
->rtc
.new.tm_sec
;
596 tm
.tm_min
= s
->rtc
.new.tm_min
;
597 tm
.tm_hour
= s
->rtc
.new.tm_hour
;
598 tm
.tm_mday
= s
->rtc
.new.tm_mday
;
599 tm
.tm_mon
= s
->rtc
.new.tm_mon
;
600 tm
.tm_year
= s
->rtc
.new.tm_year
;
604 fprintf(stderr
, "%s: bad RTC_UPDATE value %02x\n",
606 s
->status
|= 1 << 10; /* RTCERR */
609 s
->rtc
.sec_offset
= qemu_timedate_diff(&tm
);
611 case MENELAUS_RTC_SEC
:
612 s
->rtc
.tm
.tm_sec
= from_bcd(value
& 0x7f);
614 case MENELAUS_RTC_MIN
:
615 s
->rtc
.tm
.tm_min
= from_bcd(value
& 0x7f);
617 case MENELAUS_RTC_HR
:
618 s
->rtc
.tm
.tm_hour
= (s
->rtc
.ctrl
& (1 << 2)) ? /* MODE12_n24 */
619 MIN(from_bcd(value
& 0x3f), 12) + ((value
>> 7) ? 11 : -1) :
620 from_bcd(value
& 0x3f);
622 case MENELAUS_RTC_DAY
:
623 s
->rtc
.tm
.tm_mday
= from_bcd(value
);
625 case MENELAUS_RTC_MON
:
626 s
->rtc
.tm
.tm_mon
= MAX(1, from_bcd(value
)) - 1;
628 case MENELAUS_RTC_YR
:
629 s
->rtc
.tm
.tm_year
= 2000 + from_bcd(value
);
631 case MENELAUS_RTC_WKDAY
:
632 s
->rtc
.tm
.tm_mday
= from_bcd(value
);
634 case MENELAUS_RTC_AL_SEC
:
635 s
->rtc
.alm
.tm_sec
= from_bcd(value
& 0x7f);
636 menelaus_alm_update(s
);
638 case MENELAUS_RTC_AL_MIN
:
639 s
->rtc
.alm
.tm_min
= from_bcd(value
& 0x7f);
640 menelaus_alm_update(s
);
642 case MENELAUS_RTC_AL_HR
:
643 s
->rtc
.alm
.tm_hour
= (s
->rtc
.ctrl
& (1 << 2)) ? /* MODE12_n24 */
644 MIN(from_bcd(value
& 0x3f), 12) + ((value
>> 7) ? 11 : -1) :
645 from_bcd(value
& 0x3f);
646 menelaus_alm_update(s
);
648 case MENELAUS_RTC_AL_DAY
:
649 s
->rtc
.alm
.tm_mday
= from_bcd(value
);
650 menelaus_alm_update(s
);
652 case MENELAUS_RTC_AL_MON
:
653 s
->rtc
.alm
.tm_mon
= MAX(1, from_bcd(value
)) - 1;
654 menelaus_alm_update(s
);
656 case MENELAUS_RTC_AL_YR
:
657 s
->rtc
.alm
.tm_year
= 2000 + from_bcd(value
);
658 menelaus_alm_update(s
);
660 case MENELAUS_RTC_COMP_MSB
:
662 s
->rtc
.comp
|= value
<< 8;
664 case MENELAUS_RTC_COMP_LSB
:
665 s
->rtc
.comp
&= 0xff << 8;
666 s
->rtc
.comp
|= value
;
669 case MENELAUS_S1_PULL_EN
:
672 case MENELAUS_S1_PULL_DIR
:
673 s
->pull
[1] = value
& 0x1f;
675 case MENELAUS_S2_PULL_EN
:
678 case MENELAUS_S2_PULL_DIR
:
679 s
->pull
[3] = value
& 0x1f;
682 case MENELAUS_MCT_CTRL1
:
683 s
->mmc_ctrl
[0] = value
& 0x7f;
685 case MENELAUS_MCT_CTRL2
:
686 s
->mmc_ctrl
[1] = value
;
687 /* TODO update Card Detect interrupts */
689 case MENELAUS_MCT_CTRL3
:
690 s
->mmc_ctrl
[2] = value
& 0xf;
692 case MENELAUS_DEBOUNCE1
:
693 s
->mmc_debounce
= value
& 0x3f;
698 printf("%s: unknown register %02x\n", __func__
, addr
);
704 static int menelaus_event(I2CSlave
*i2c
, enum i2c_event event
)
706 MenelausState
*s
= TWL92230(i2c
);
708 if (event
== I2C_START_SEND
)
714 static int menelaus_tx(I2CSlave
*i2c
, uint8_t data
)
716 MenelausState
*s
= TWL92230(i2c
);
718 /* Interpret register address byte */
723 menelaus_write(s
, s
->reg
++, data
);
728 static uint8_t menelaus_rx(I2CSlave
*i2c
)
730 MenelausState
*s
= TWL92230(i2c
);
732 return menelaus_read(s
, s
->reg
++);
735 /* Save restore 32 bit int as uint16_t
736 This is a Big hack, but it is how the old state did it.
737 Or we broke compatibility in the state, or we can't use struct tm
740 static int get_int32_as_uint16(QEMUFile
*f
, void *pv
, size_t size
,
741 const VMStateField
*field
)
744 *v
= qemu_get_be16(f
);
748 static int put_int32_as_uint16(QEMUFile
*f
, void *pv
, size_t size
,
749 const VMStateField
*field
, JSONWriter
*vmdesc
)
752 qemu_put_be16(f
, *v
);
757 static const VMStateInfo vmstate_hack_int32_as_uint16
= {
758 .name
= "int32_as_uint16",
759 .get
= get_int32_as_uint16
,
760 .put
= put_int32_as_uint16
,
763 #define VMSTATE_UINT16_HACK(_f, _s) \
764 VMSTATE_SINGLE(_f, _s, 0, vmstate_hack_int32_as_uint16, int32_t)
767 static const VMStateDescription vmstate_menelaus_tm
= {
768 .name
= "menelaus_tm",
770 .minimum_version_id
= 0,
771 .fields
= (VMStateField
[]) {
772 VMSTATE_UINT16_HACK(tm_sec
, struct tm
),
773 VMSTATE_UINT16_HACK(tm_min
, struct tm
),
774 VMSTATE_UINT16_HACK(tm_hour
, struct tm
),
775 VMSTATE_UINT16_HACK(tm_mday
, struct tm
),
776 VMSTATE_UINT16_HACK(tm_min
, struct tm
),
777 VMSTATE_UINT16_HACK(tm_year
, struct tm
),
778 VMSTATE_END_OF_LIST()
782 static int menelaus_pre_save(void *opaque
)
784 MenelausState
*s
= opaque
;
785 /* Should be <= 1000 */
786 s
->rtc_next_vmstate
= s
->rtc
.next
- qemu_clock_get_ms(rtc_clock
);
791 static int menelaus_post_load(void *opaque
, int version_id
)
793 MenelausState
*s
= opaque
;
795 if (s
->rtc
.ctrl
& 1) /* RTC_EN */
796 menelaus_rtc_stop(s
);
798 s
->rtc
.next
= s
->rtc_next_vmstate
;
800 menelaus_alm_update(s
);
802 if (s
->rtc
.ctrl
& 1) /* RTC_EN */
803 menelaus_rtc_start(s
);
807 static const VMStateDescription vmstate_menelaus
= {
810 .minimum_version_id
= 0,
811 .pre_save
= menelaus_pre_save
,
812 .post_load
= menelaus_post_load
,
813 .fields
= (VMStateField
[]) {
814 VMSTATE_INT32(firstbyte
, MenelausState
),
815 VMSTATE_UINT8(reg
, MenelausState
),
816 VMSTATE_UINT8_ARRAY(vcore
, MenelausState
, 5),
817 VMSTATE_UINT8_ARRAY(dcdc
, MenelausState
, 3),
818 VMSTATE_UINT8_ARRAY(ldo
, MenelausState
, 8),
819 VMSTATE_UINT8_ARRAY(sleep
, MenelausState
, 2),
820 VMSTATE_UINT8(osc
, MenelausState
),
821 VMSTATE_UINT8(detect
, MenelausState
),
822 VMSTATE_UINT16(mask
, MenelausState
),
823 VMSTATE_UINT16(status
, MenelausState
),
824 VMSTATE_UINT8(dir
, MenelausState
),
825 VMSTATE_UINT8(inputs
, MenelausState
),
826 VMSTATE_UINT8(outputs
, MenelausState
),
827 VMSTATE_UINT8(bbsms
, MenelausState
),
828 VMSTATE_UINT8_ARRAY(pull
, MenelausState
, 4),
829 VMSTATE_UINT8_ARRAY(mmc_ctrl
, MenelausState
, 3),
830 VMSTATE_UINT8(mmc_debounce
, MenelausState
),
831 VMSTATE_UINT8(rtc
.ctrl
, MenelausState
),
832 VMSTATE_UINT16(rtc
.comp
, MenelausState
),
833 VMSTATE_UINT16(rtc_next_vmstate
, MenelausState
),
834 VMSTATE_STRUCT(rtc
.new, MenelausState
, 0, vmstate_menelaus_tm
,
836 VMSTATE_STRUCT(rtc
.alm
, MenelausState
, 0, vmstate_menelaus_tm
,
838 VMSTATE_UINT8(pwrbtn_state
, MenelausState
),
839 VMSTATE_I2C_SLAVE(parent_obj
, MenelausState
),
840 VMSTATE_END_OF_LIST()
844 static void twl92230_realize(DeviceState
*dev
, Error
**errp
)
846 MenelausState
*s
= TWL92230(dev
);
848 s
->rtc
.hz_tm
= timer_new_ms(rtc_clock
, menelaus_rtc_hz
, s
);
849 /* Three output pins plus one interrupt pin. */
850 qdev_init_gpio_out(dev
, s
->out
, 4);
852 /* Three input pins plus one power-button pin. */
853 qdev_init_gpio_in(dev
, menelaus_gpio_set
, 4);
855 menelaus_reset(I2C_SLAVE(dev
));
858 static void twl92230_class_init(ObjectClass
*klass
, void *data
)
860 DeviceClass
*dc
= DEVICE_CLASS(klass
);
861 I2CSlaveClass
*sc
= I2C_SLAVE_CLASS(klass
);
863 dc
->realize
= twl92230_realize
;
864 sc
->event
= menelaus_event
;
865 sc
->recv
= menelaus_rx
;
866 sc
->send
= menelaus_tx
;
867 dc
->vmsd
= &vmstate_menelaus
;
870 static const TypeInfo twl92230_info
= {
871 .name
= TYPE_TWL92230
,
872 .parent
= TYPE_I2C_SLAVE
,
873 .instance_size
= sizeof(MenelausState
),
874 .class_init
= twl92230_class_init
,
877 static void twl92230_register_types(void)
879 type_register_static(&twl92230_info
);
882 type_init(twl92230_register_types
)