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/>.
23 #include "qemu/timer.h"
24 #include "hw/i2c/i2c.h"
25 #include "sysemu/sysemu.h"
26 #include "ui/console.h"
30 #define TYPE_TWL92230 "twl92230"
31 #define TWL92230(obj) OBJECT_CHECK(MenelausState, (obj), TYPE_TWL92230)
33 typedef struct MenelausState
{
66 uint16_t rtc_next_vmstate
;
71 static inline void menelaus_update(MenelausState
*s
)
73 qemu_set_irq(s
->out
[3], s
->status
& ~s
->mask
);
76 static inline void menelaus_rtc_start(MenelausState
*s
)
78 s
->rtc
.next
+= qemu_clock_get_ms(rtc_clock
);
79 timer_mod(s
->rtc
.hz_tm
, s
->rtc
.next
);
82 static inline void menelaus_rtc_stop(MenelausState
*s
)
84 timer_del(s
->rtc
.hz_tm
);
85 s
->rtc
.next
-= qemu_clock_get_ms(rtc_clock
);
90 static void menelaus_rtc_update(MenelausState
*s
)
92 qemu_get_timedate(&s
->rtc
.tm
, s
->rtc
.sec_offset
);
95 static void menelaus_alm_update(MenelausState
*s
)
97 if ((s
->rtc
.ctrl
& 3) == 3)
98 s
->rtc
.alm_sec
= qemu_timedate_diff(&s
->rtc
.alm
) - s
->rtc
.sec_offset
;
101 static void menelaus_rtc_hz(void *opaque
)
103 MenelausState
*s
= (MenelausState
*) opaque
;
108 timer_mod(s
->rtc
.hz_tm
, s
->rtc
.next
);
109 if ((s
->rtc
.ctrl
>> 3) & 3) { /* EVERY */
110 menelaus_rtc_update(s
);
111 if (((s
->rtc
.ctrl
>> 3) & 3) == 1 && !s
->rtc
.tm
.tm_sec
)
112 s
->status
|= 1 << 8; /* RTCTMR */
113 else if (((s
->rtc
.ctrl
>> 3) & 3) == 2 && !s
->rtc
.tm
.tm_min
)
114 s
->status
|= 1 << 8; /* RTCTMR */
115 else if (!s
->rtc
.tm
.tm_hour
)
116 s
->status
|= 1 << 8; /* RTCTMR */
118 s
->status
|= 1 << 8; /* RTCTMR */
119 if ((s
->rtc
.ctrl
>> 1) & 1) { /* RTC_AL_EN */
120 if (s
->rtc
.alm_sec
== 0)
121 s
->status
|= 1 << 9; /* RTCALM */
124 if (s
->rtc
.next_comp
<= 0) {
125 s
->rtc
.next
-= muldiv64((int16_t) s
->rtc
.comp
, 1000, 0x8000);
126 s
->rtc
.next_comp
= 3600;
131 static void menelaus_reset(I2CSlave
*i2c
)
133 MenelausState
*s
= TWL92230(i2c
);
137 s
->vcore
[0] = 0x0c; /* XXX: X-loader needs 0x8c? check! */
142 s
->dcdc
[0] = 0x33; /* Depends on wiring */
148 s
->ldo
[3] = 0x00; /* Depends on wiring */
149 s
->ldo
[4] = 0x03; /* Depends on wiring */
166 s
->mmc_ctrl
[0] = 0x03;
167 s
->mmc_ctrl
[1] = 0xc0;
168 s
->mmc_ctrl
[2] = 0x00;
169 s
->mmc_debounce
= 0x05;
172 menelaus_rtc_stop(s
);
174 s
->rtc
.comp
= 0x0000;
176 s
->rtc
.sec_offset
= 0;
177 s
->rtc
.next_comp
= 1800;
178 s
->rtc
.alm_sec
= 1800;
179 s
->rtc
.alm
.tm_sec
= 0x00;
180 s
->rtc
.alm
.tm_min
= 0x00;
181 s
->rtc
.alm
.tm_hour
= 0x00;
182 s
->rtc
.alm
.tm_mday
= 0x01;
183 s
->rtc
.alm
.tm_mon
= 0x00;
184 s
->rtc
.alm
.tm_year
= 2004;
188 static void menelaus_gpio_set(void *opaque
, int line
, int level
)
190 MenelausState
*s
= (MenelausState
*) opaque
;
193 /* No interrupt generated */
194 s
->inputs
&= ~(1 << line
);
195 s
->inputs
|= level
<< line
;
199 if (!s
->pwrbtn_state
&& level
) {
200 s
->status
|= 1 << 11; /* PSHBTN */
203 s
->pwrbtn_state
= level
;
206 #define MENELAUS_REV 0x01
207 #define MENELAUS_VCORE_CTRL1 0x02
208 #define MENELAUS_VCORE_CTRL2 0x03
209 #define MENELAUS_VCORE_CTRL3 0x04
210 #define MENELAUS_VCORE_CTRL4 0x05
211 #define MENELAUS_VCORE_CTRL5 0x06
212 #define MENELAUS_DCDC_CTRL1 0x07
213 #define MENELAUS_DCDC_CTRL2 0x08
214 #define MENELAUS_DCDC_CTRL3 0x09
215 #define MENELAUS_LDO_CTRL1 0x0a
216 #define MENELAUS_LDO_CTRL2 0x0b
217 #define MENELAUS_LDO_CTRL3 0x0c
218 #define MENELAUS_LDO_CTRL4 0x0d
219 #define MENELAUS_LDO_CTRL5 0x0e
220 #define MENELAUS_LDO_CTRL6 0x0f
221 #define MENELAUS_LDO_CTRL7 0x10
222 #define MENELAUS_LDO_CTRL8 0x11
223 #define MENELAUS_SLEEP_CTRL1 0x12
224 #define MENELAUS_SLEEP_CTRL2 0x13
225 #define MENELAUS_DEVICE_OFF 0x14
226 #define MENELAUS_OSC_CTRL 0x15
227 #define MENELAUS_DETECT_CTRL 0x16
228 #define MENELAUS_INT_MASK1 0x17
229 #define MENELAUS_INT_MASK2 0x18
230 #define MENELAUS_INT_STATUS1 0x19
231 #define MENELAUS_INT_STATUS2 0x1a
232 #define MENELAUS_INT_ACK1 0x1b
233 #define MENELAUS_INT_ACK2 0x1c
234 #define MENELAUS_GPIO_CTRL 0x1d
235 #define MENELAUS_GPIO_IN 0x1e
236 #define MENELAUS_GPIO_OUT 0x1f
237 #define MENELAUS_BBSMS 0x20
238 #define MENELAUS_RTC_CTRL 0x21
239 #define MENELAUS_RTC_UPDATE 0x22
240 #define MENELAUS_RTC_SEC 0x23
241 #define MENELAUS_RTC_MIN 0x24
242 #define MENELAUS_RTC_HR 0x25
243 #define MENELAUS_RTC_DAY 0x26
244 #define MENELAUS_RTC_MON 0x27
245 #define MENELAUS_RTC_YR 0x28
246 #define MENELAUS_RTC_WKDAY 0x29
247 #define MENELAUS_RTC_AL_SEC 0x2a
248 #define MENELAUS_RTC_AL_MIN 0x2b
249 #define MENELAUS_RTC_AL_HR 0x2c
250 #define MENELAUS_RTC_AL_DAY 0x2d
251 #define MENELAUS_RTC_AL_MON 0x2e
252 #define MENELAUS_RTC_AL_YR 0x2f
253 #define MENELAUS_RTC_COMP_MSB 0x30
254 #define MENELAUS_RTC_COMP_LSB 0x31
255 #define MENELAUS_S1_PULL_EN 0x32
256 #define MENELAUS_S1_PULL_DIR 0x33
257 #define MENELAUS_S2_PULL_EN 0x34
258 #define MENELAUS_S2_PULL_DIR 0x35
259 #define MENELAUS_MCT_CTRL1 0x36
260 #define MENELAUS_MCT_CTRL2 0x37
261 #define MENELAUS_MCT_CTRL3 0x38
262 #define MENELAUS_MCT_PIN_ST 0x39
263 #define MENELAUS_DEBOUNCE1 0x3a
265 static uint8_t menelaus_read(void *opaque
, uint8_t addr
)
267 MenelausState
*s
= (MenelausState
*) opaque
;
274 case MENELAUS_VCORE_CTRL5
: reg
++;
275 case MENELAUS_VCORE_CTRL4
: reg
++;
276 case MENELAUS_VCORE_CTRL3
: reg
++;
277 case MENELAUS_VCORE_CTRL2
: reg
++;
278 case MENELAUS_VCORE_CTRL1
:
279 return s
->vcore
[reg
];
281 case MENELAUS_DCDC_CTRL3
: reg
++;
282 case MENELAUS_DCDC_CTRL2
: reg
++;
283 case MENELAUS_DCDC_CTRL1
:
286 case MENELAUS_LDO_CTRL8
: reg
++;
287 case MENELAUS_LDO_CTRL7
: reg
++;
288 case MENELAUS_LDO_CTRL6
: reg
++;
289 case MENELAUS_LDO_CTRL5
: reg
++;
290 case MENELAUS_LDO_CTRL4
: reg
++;
291 case MENELAUS_LDO_CTRL3
: reg
++;
292 case MENELAUS_LDO_CTRL2
: reg
++;
293 case MENELAUS_LDO_CTRL1
:
296 case MENELAUS_SLEEP_CTRL2
: reg
++;
297 case MENELAUS_SLEEP_CTRL1
:
298 return s
->sleep
[reg
];
300 case MENELAUS_DEVICE_OFF
:
303 case MENELAUS_OSC_CTRL
:
304 return s
->osc
| (1 << 7); /* CLK32K_GOOD */
306 case MENELAUS_DETECT_CTRL
:
309 case MENELAUS_INT_MASK1
:
310 return (s
->mask
>> 0) & 0xff;
311 case MENELAUS_INT_MASK2
:
312 return (s
->mask
>> 8) & 0xff;
314 case MENELAUS_INT_STATUS1
:
315 return (s
->status
>> 0) & 0xff;
316 case MENELAUS_INT_STATUS2
:
317 return (s
->status
>> 8) & 0xff;
319 case MENELAUS_INT_ACK1
:
320 case MENELAUS_INT_ACK2
:
323 case MENELAUS_GPIO_CTRL
:
325 case MENELAUS_GPIO_IN
:
326 return s
->inputs
| (~s
->dir
& s
->outputs
);
327 case MENELAUS_GPIO_OUT
:
333 case MENELAUS_RTC_CTRL
:
335 case MENELAUS_RTC_UPDATE
:
337 case MENELAUS_RTC_SEC
:
338 menelaus_rtc_update(s
);
339 return to_bcd(s
->rtc
.tm
.tm_sec
);
340 case MENELAUS_RTC_MIN
:
341 menelaus_rtc_update(s
);
342 return to_bcd(s
->rtc
.tm
.tm_min
);
343 case MENELAUS_RTC_HR
:
344 menelaus_rtc_update(s
);
345 if ((s
->rtc
.ctrl
>> 2) & 1) /* MODE12_n24 */
346 return to_bcd((s
->rtc
.tm
.tm_hour
% 12) + 1) |
347 (!!(s
->rtc
.tm
.tm_hour
>= 12) << 7); /* PM_nAM */
349 return to_bcd(s
->rtc
.tm
.tm_hour
);
350 case MENELAUS_RTC_DAY
:
351 menelaus_rtc_update(s
);
352 return to_bcd(s
->rtc
.tm
.tm_mday
);
353 case MENELAUS_RTC_MON
:
354 menelaus_rtc_update(s
);
355 return to_bcd(s
->rtc
.tm
.tm_mon
+ 1);
356 case MENELAUS_RTC_YR
:
357 menelaus_rtc_update(s
);
358 return to_bcd(s
->rtc
.tm
.tm_year
- 2000);
359 case MENELAUS_RTC_WKDAY
:
360 menelaus_rtc_update(s
);
361 return to_bcd(s
->rtc
.tm
.tm_wday
);
362 case MENELAUS_RTC_AL_SEC
:
363 return to_bcd(s
->rtc
.alm
.tm_sec
);
364 case MENELAUS_RTC_AL_MIN
:
365 return to_bcd(s
->rtc
.alm
.tm_min
);
366 case MENELAUS_RTC_AL_HR
:
367 if ((s
->rtc
.ctrl
>> 2) & 1) /* MODE12_n24 */
368 return to_bcd((s
->rtc
.alm
.tm_hour
% 12) + 1) |
369 (!!(s
->rtc
.alm
.tm_hour
>= 12) << 7);/* AL_PM_nAM */
371 return to_bcd(s
->rtc
.alm
.tm_hour
);
372 case MENELAUS_RTC_AL_DAY
:
373 return to_bcd(s
->rtc
.alm
.tm_mday
);
374 case MENELAUS_RTC_AL_MON
:
375 return to_bcd(s
->rtc
.alm
.tm_mon
+ 1);
376 case MENELAUS_RTC_AL_YR
:
377 return to_bcd(s
->rtc
.alm
.tm_year
- 2000);
378 case MENELAUS_RTC_COMP_MSB
:
379 return (s
->rtc
.comp
>> 8) & 0xff;
380 case MENELAUS_RTC_COMP_LSB
:
381 return (s
->rtc
.comp
>> 0) & 0xff;
383 case MENELAUS_S1_PULL_EN
:
385 case MENELAUS_S1_PULL_DIR
:
387 case MENELAUS_S2_PULL_EN
:
389 case MENELAUS_S2_PULL_DIR
:
392 case MENELAUS_MCT_CTRL3
: reg
++;
393 case MENELAUS_MCT_CTRL2
: reg
++;
394 case MENELAUS_MCT_CTRL1
:
395 return s
->mmc_ctrl
[reg
];
396 case MENELAUS_MCT_PIN_ST
:
397 /* TODO: return the real Card Detect */
399 case MENELAUS_DEBOUNCE1
:
400 return s
->mmc_debounce
;
404 printf("%s: unknown register %02x\n", __FUNCTION__
, addr
);
411 static void menelaus_write(void *opaque
, uint8_t addr
, uint8_t value
)
413 MenelausState
*s
= (MenelausState
*) opaque
;
419 case MENELAUS_VCORE_CTRL1
:
420 s
->vcore
[0] = (value
& 0xe) | MIN(value
& 0x1f, 0x12);
422 case MENELAUS_VCORE_CTRL2
:
425 case MENELAUS_VCORE_CTRL3
:
426 s
->vcore
[2] = MIN(value
& 0x1f, 0x12);
428 case MENELAUS_VCORE_CTRL4
:
429 s
->vcore
[3] = MIN(value
& 0x1f, 0x12);
431 case MENELAUS_VCORE_CTRL5
:
432 s
->vcore
[4] = value
& 3;
434 * auto set to 3 on M_Active, nRESWARM
435 * auto set to 0 on M_WaitOn, M_Backup
439 case MENELAUS_DCDC_CTRL1
:
440 s
->dcdc
[0] = value
& 0x3f;
442 case MENELAUS_DCDC_CTRL2
:
443 s
->dcdc
[1] = value
& 0x07;
445 * auto set to 3 on M_Active, nRESWARM
446 * auto set to 0 on M_WaitOn, M_Backup
449 case MENELAUS_DCDC_CTRL3
:
450 s
->dcdc
[2] = value
& 0x07;
453 case MENELAUS_LDO_CTRL1
:
456 case MENELAUS_LDO_CTRL2
:
457 s
->ldo
[1] = value
& 0x7f;
459 * auto set to 0x7e on M_WaitOn, M_Backup
462 case MENELAUS_LDO_CTRL3
:
463 s
->ldo
[2] = value
& 3;
465 * auto set to 3 on M_Active, nRESWARM
466 * auto set to 0 on M_WaitOn, M_Backup
469 case MENELAUS_LDO_CTRL4
:
470 s
->ldo
[3] = value
& 3;
472 * auto set to 3 on M_Active, nRESWARM
473 * auto set to 0 on M_WaitOn, M_Backup
476 case MENELAUS_LDO_CTRL5
:
477 s
->ldo
[4] = value
& 3;
479 * auto set to 3 on M_Active, nRESWARM
480 * auto set to 0 on M_WaitOn, M_Backup
483 case MENELAUS_LDO_CTRL6
:
484 s
->ldo
[5] = value
& 3;
486 case MENELAUS_LDO_CTRL7
:
487 s
->ldo
[6] = value
& 3;
489 case MENELAUS_LDO_CTRL8
:
490 s
->ldo
[7] = value
& 3;
493 case MENELAUS_SLEEP_CTRL2
: reg
++;
494 case MENELAUS_SLEEP_CTRL1
:
495 s
->sleep
[reg
] = value
;
498 case MENELAUS_DEVICE_OFF
:
500 menelaus_reset(I2C_SLAVE(s
));
504 case MENELAUS_OSC_CTRL
:
508 case MENELAUS_DETECT_CTRL
:
509 s
->detect
= value
& 0x7f;
512 case MENELAUS_INT_MASK1
:
514 s
->mask
|= value
<< 0;
517 case MENELAUS_INT_MASK2
:
519 s
->mask
|= value
<< 8;
523 case MENELAUS_INT_ACK1
:
524 s
->status
&= ~(((uint16_t) value
) << 0);
527 case MENELAUS_INT_ACK2
:
528 s
->status
&= ~(((uint16_t) value
) << 8);
532 case MENELAUS_GPIO_CTRL
:
533 for (line
= 0; line
< 3; line
++) {
534 if (((s
->dir
^ value
) >> line
) & 1) {
535 qemu_set_irq(s
->out
[line
],
536 ((s
->outputs
& ~s
->dir
) >> line
) & 1);
539 s
->dir
= value
& 0x67;
541 case MENELAUS_GPIO_OUT
:
542 for (line
= 0; line
< 3; line
++) {
543 if ((((s
->outputs
^ value
) & ~s
->dir
) >> line
) & 1) {
544 qemu_set_irq(s
->out
[line
], (s
->outputs
>> line
) & 1);
547 s
->outputs
= value
& 0x07;
554 case MENELAUS_RTC_CTRL
:
555 if ((s
->rtc
.ctrl
^ value
) & 1) { /* RTC_EN */
557 menelaus_rtc_start(s
);
559 menelaus_rtc_stop(s
);
561 s
->rtc
.ctrl
= value
& 0x1f;
562 menelaus_alm_update(s
);
564 case MENELAUS_RTC_UPDATE
:
565 menelaus_rtc_update(s
);
566 memcpy(&tm
, &s
->rtc
.tm
, sizeof(tm
));
567 switch (value
& 0xf) {
571 tm
.tm_sec
= s
->rtc
.new.tm_sec
;
574 tm
.tm_min
= s
->rtc
.new.tm_min
;
577 if (s
->rtc
.new.tm_hour
> 23)
579 tm
.tm_hour
= s
->rtc
.new.tm_hour
;
582 if (s
->rtc
.new.tm_mday
< 1)
584 /* TODO check range */
585 tm
.tm_mday
= s
->rtc
.new.tm_mday
;
588 if (s
->rtc
.new.tm_mon
< 0 || s
->rtc
.new.tm_mon
> 11)
590 tm
.tm_mon
= s
->rtc
.new.tm_mon
;
593 tm
.tm_year
= s
->rtc
.new.tm_year
;
596 /* TODO set .tm_mday instead */
597 tm
.tm_wday
= s
->rtc
.new.tm_wday
;
600 if (s
->rtc
.new.tm_hour
> 23)
602 if (s
->rtc
.new.tm_mday
< 1)
604 if (s
->rtc
.new.tm_mon
< 0 || s
->rtc
.new.tm_mon
> 11)
606 tm
.tm_sec
= s
->rtc
.new.tm_sec
;
607 tm
.tm_min
= s
->rtc
.new.tm_min
;
608 tm
.tm_hour
= s
->rtc
.new.tm_hour
;
609 tm
.tm_mday
= s
->rtc
.new.tm_mday
;
610 tm
.tm_mon
= s
->rtc
.new.tm_mon
;
611 tm
.tm_year
= s
->rtc
.new.tm_year
;
615 fprintf(stderr
, "%s: bad RTC_UPDATE value %02x\n",
616 __FUNCTION__
, value
);
617 s
->status
|= 1 << 10; /* RTCERR */
620 s
->rtc
.sec_offset
= qemu_timedate_diff(&tm
);
622 case MENELAUS_RTC_SEC
:
623 s
->rtc
.tm
.tm_sec
= from_bcd(value
& 0x7f);
625 case MENELAUS_RTC_MIN
:
626 s
->rtc
.tm
.tm_min
= from_bcd(value
& 0x7f);
628 case MENELAUS_RTC_HR
:
629 s
->rtc
.tm
.tm_hour
= (s
->rtc
.ctrl
& (1 << 2)) ? /* MODE12_n24 */
630 MIN(from_bcd(value
& 0x3f), 12) + ((value
>> 7) ? 11 : -1) :
631 from_bcd(value
& 0x3f);
633 case MENELAUS_RTC_DAY
:
634 s
->rtc
.tm
.tm_mday
= from_bcd(value
);
636 case MENELAUS_RTC_MON
:
637 s
->rtc
.tm
.tm_mon
= MAX(1, from_bcd(value
)) - 1;
639 case MENELAUS_RTC_YR
:
640 s
->rtc
.tm
.tm_year
= 2000 + from_bcd(value
);
642 case MENELAUS_RTC_WKDAY
:
643 s
->rtc
.tm
.tm_mday
= from_bcd(value
);
645 case MENELAUS_RTC_AL_SEC
:
646 s
->rtc
.alm
.tm_sec
= from_bcd(value
& 0x7f);
647 menelaus_alm_update(s
);
649 case MENELAUS_RTC_AL_MIN
:
650 s
->rtc
.alm
.tm_min
= from_bcd(value
& 0x7f);
651 menelaus_alm_update(s
);
653 case MENELAUS_RTC_AL_HR
:
654 s
->rtc
.alm
.tm_hour
= (s
->rtc
.ctrl
& (1 << 2)) ? /* MODE12_n24 */
655 MIN(from_bcd(value
& 0x3f), 12) + ((value
>> 7) ? 11 : -1) :
656 from_bcd(value
& 0x3f);
657 menelaus_alm_update(s
);
659 case MENELAUS_RTC_AL_DAY
:
660 s
->rtc
.alm
.tm_mday
= from_bcd(value
);
661 menelaus_alm_update(s
);
663 case MENELAUS_RTC_AL_MON
:
664 s
->rtc
.alm
.tm_mon
= MAX(1, from_bcd(value
)) - 1;
665 menelaus_alm_update(s
);
667 case MENELAUS_RTC_AL_YR
:
668 s
->rtc
.alm
.tm_year
= 2000 + from_bcd(value
);
669 menelaus_alm_update(s
);
671 case MENELAUS_RTC_COMP_MSB
:
673 s
->rtc
.comp
|= value
<< 8;
675 case MENELAUS_RTC_COMP_LSB
:
676 s
->rtc
.comp
&= 0xff << 8;
677 s
->rtc
.comp
|= value
;
680 case MENELAUS_S1_PULL_EN
:
683 case MENELAUS_S1_PULL_DIR
:
684 s
->pull
[1] = value
& 0x1f;
686 case MENELAUS_S2_PULL_EN
:
689 case MENELAUS_S2_PULL_DIR
:
690 s
->pull
[3] = value
& 0x1f;
693 case MENELAUS_MCT_CTRL1
:
694 s
->mmc_ctrl
[0] = value
& 0x7f;
696 case MENELAUS_MCT_CTRL2
:
697 s
->mmc_ctrl
[1] = value
;
698 /* TODO update Card Detect interrupts */
700 case MENELAUS_MCT_CTRL3
:
701 s
->mmc_ctrl
[2] = value
& 0xf;
703 case MENELAUS_DEBOUNCE1
:
704 s
->mmc_debounce
= value
& 0x3f;
709 printf("%s: unknown register %02x\n", __FUNCTION__
, addr
);
714 static void menelaus_event(I2CSlave
*i2c
, enum i2c_event event
)
716 MenelausState
*s
= TWL92230(i2c
);
718 if (event
== I2C_START_SEND
)
722 static int menelaus_tx(I2CSlave
*i2c
, uint8_t data
)
724 MenelausState
*s
= TWL92230(i2c
);
726 /* Interpret register address byte */
731 menelaus_write(s
, s
->reg
++, data
);
736 static int menelaus_rx(I2CSlave
*i2c
)
738 MenelausState
*s
= TWL92230(i2c
);
740 return menelaus_read(s
, s
->reg
++);
743 /* Save restore 32 bit int as uint16_t
744 This is a Big hack, but it is how the old state did it.
745 Or we broke compatibility in the state, or we can't use struct tm
748 static int get_int32_as_uint16(QEMUFile
*f
, void *pv
, size_t size
)
751 *v
= qemu_get_be16(f
);
755 static void put_int32_as_uint16(QEMUFile
*f
, void *pv
, size_t size
)
758 qemu_put_be16(f
, *v
);
761 static const VMStateInfo vmstate_hack_int32_as_uint16
= {
762 .name
= "int32_as_uint16",
763 .get
= get_int32_as_uint16
,
764 .put
= put_int32_as_uint16
,
767 #define VMSTATE_UINT16_HACK(_f, _s) \
768 VMSTATE_SINGLE(_f, _s, 0, vmstate_hack_int32_as_uint16, int32_t)
771 static const VMStateDescription vmstate_menelaus_tm
= {
772 .name
= "menelaus_tm",
774 .minimum_version_id
= 0,
775 .minimum_version_id_old
= 0,
776 .fields
= (VMStateField
[]) {
777 VMSTATE_UINT16_HACK(tm_sec
, struct tm
),
778 VMSTATE_UINT16_HACK(tm_min
, struct tm
),
779 VMSTATE_UINT16_HACK(tm_hour
, struct tm
),
780 VMSTATE_UINT16_HACK(tm_mday
, struct tm
),
781 VMSTATE_UINT16_HACK(tm_min
, struct tm
),
782 VMSTATE_UINT16_HACK(tm_year
, struct tm
),
783 VMSTATE_END_OF_LIST()
787 static void menelaus_pre_save(void *opaque
)
789 MenelausState
*s
= opaque
;
790 /* Should be <= 1000 */
791 s
->rtc_next_vmstate
= s
->rtc
.next
- qemu_clock_get_ms(rtc_clock
);
794 static int menelaus_post_load(void *opaque
, int version_id
)
796 MenelausState
*s
= opaque
;
798 if (s
->rtc
.ctrl
& 1) /* RTC_EN */
799 menelaus_rtc_stop(s
);
801 s
->rtc
.next
= s
->rtc_next_vmstate
;
803 menelaus_alm_update(s
);
805 if (s
->rtc
.ctrl
& 1) /* RTC_EN */
806 menelaus_rtc_start(s
);
810 static const VMStateDescription vmstate_menelaus
= {
813 .minimum_version_id
= 0,
814 .minimum_version_id_old
= 0,
815 .pre_save
= menelaus_pre_save
,
816 .post_load
= menelaus_post_load
,
817 .fields
= (VMStateField
[]) {
818 VMSTATE_INT32(firstbyte
, MenelausState
),
819 VMSTATE_UINT8(reg
, MenelausState
),
820 VMSTATE_UINT8_ARRAY(vcore
, MenelausState
, 5),
821 VMSTATE_UINT8_ARRAY(dcdc
, MenelausState
, 3),
822 VMSTATE_UINT8_ARRAY(ldo
, MenelausState
, 8),
823 VMSTATE_UINT8_ARRAY(sleep
, MenelausState
, 2),
824 VMSTATE_UINT8(osc
, MenelausState
),
825 VMSTATE_UINT8(detect
, MenelausState
),
826 VMSTATE_UINT16(mask
, MenelausState
),
827 VMSTATE_UINT16(status
, MenelausState
),
828 VMSTATE_UINT8(dir
, MenelausState
),
829 VMSTATE_UINT8(inputs
, MenelausState
),
830 VMSTATE_UINT8(outputs
, MenelausState
),
831 VMSTATE_UINT8(bbsms
, MenelausState
),
832 VMSTATE_UINT8_ARRAY(pull
, MenelausState
, 4),
833 VMSTATE_UINT8_ARRAY(mmc_ctrl
, MenelausState
, 3),
834 VMSTATE_UINT8(mmc_debounce
, MenelausState
),
835 VMSTATE_UINT8(rtc
.ctrl
, MenelausState
),
836 VMSTATE_UINT16(rtc
.comp
, MenelausState
),
837 VMSTATE_UINT16(rtc_next_vmstate
, MenelausState
),
838 VMSTATE_STRUCT(rtc
.new, MenelausState
, 0, vmstate_menelaus_tm
,
840 VMSTATE_STRUCT(rtc
.alm
, MenelausState
, 0, vmstate_menelaus_tm
,
842 VMSTATE_UINT8(pwrbtn_state
, MenelausState
),
843 VMSTATE_I2C_SLAVE(parent_obj
, MenelausState
),
844 VMSTATE_END_OF_LIST()
848 static int twl92230_init(I2CSlave
*i2c
)
850 DeviceState
*dev
= DEVICE(i2c
);
851 MenelausState
*s
= TWL92230(i2c
);
853 s
->rtc
.hz_tm
= timer_new_ms(rtc_clock
, menelaus_rtc_hz
, s
);
854 /* Three output pins plus one interrupt pin. */
855 qdev_init_gpio_out(dev
, s
->out
, 4);
857 /* Three input pins plus one power-button pin. */
858 qdev_init_gpio_in(dev
, menelaus_gpio_set
, 4);
865 static void twl92230_class_init(ObjectClass
*klass
, void *data
)
867 DeviceClass
*dc
= DEVICE_CLASS(klass
);
868 I2CSlaveClass
*sc
= I2C_SLAVE_CLASS(klass
);
870 sc
->init
= twl92230_init
;
871 sc
->event
= menelaus_event
;
872 sc
->recv
= menelaus_rx
;
873 sc
->send
= menelaus_tx
;
874 dc
->vmsd
= &vmstate_menelaus
;
877 static const TypeInfo twl92230_info
= {
878 .name
= TYPE_TWL92230
,
879 .parent
= TYPE_I2C_SLAVE
,
880 .instance_size
= sizeof(MenelausState
),
881 .class_init
= twl92230_class_init
,
884 static void twl92230_register_types(void)
886 type_register_static(&twl92230_info
);
889 type_init(twl92230_register_types
)