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"
24 #include "qemu/timer.h"
25 #include "hw/i2c/i2c.h"
26 #include "sysemu/sysemu.h"
27 #include "ui/console.h"
32 #define TYPE_TWL92230 "twl92230"
33 #define TWL92230(obj) OBJECT_CHECK(MenelausState, (obj), TYPE_TWL92230)
35 typedef struct MenelausState
{
68 uint16_t rtc_next_vmstate
;
73 static inline void menelaus_update(MenelausState
*s
)
75 qemu_set_irq(s
->out
[3], s
->status
& ~s
->mask
);
78 static inline void menelaus_rtc_start(MenelausState
*s
)
80 s
->rtc
.next
+= qemu_clock_get_ms(rtc_clock
);
81 timer_mod(s
->rtc
.hz_tm
, s
->rtc
.next
);
84 static inline void menelaus_rtc_stop(MenelausState
*s
)
86 timer_del(s
->rtc
.hz_tm
);
87 s
->rtc
.next
-= qemu_clock_get_ms(rtc_clock
);
92 static void menelaus_rtc_update(MenelausState
*s
)
94 qemu_get_timedate(&s
->rtc
.tm
, s
->rtc
.sec_offset
);
97 static void menelaus_alm_update(MenelausState
*s
)
99 if ((s
->rtc
.ctrl
& 3) == 3)
100 s
->rtc
.alm_sec
= qemu_timedate_diff(&s
->rtc
.alm
) - s
->rtc
.sec_offset
;
103 static void menelaus_rtc_hz(void *opaque
)
105 MenelausState
*s
= (MenelausState
*) opaque
;
110 timer_mod(s
->rtc
.hz_tm
, s
->rtc
.next
);
111 if ((s
->rtc
.ctrl
>> 3) & 3) { /* EVERY */
112 menelaus_rtc_update(s
);
113 if (((s
->rtc
.ctrl
>> 3) & 3) == 1 && !s
->rtc
.tm
.tm_sec
)
114 s
->status
|= 1 << 8; /* RTCTMR */
115 else if (((s
->rtc
.ctrl
>> 3) & 3) == 2 && !s
->rtc
.tm
.tm_min
)
116 s
->status
|= 1 << 8; /* RTCTMR */
117 else if (!s
->rtc
.tm
.tm_hour
)
118 s
->status
|= 1 << 8; /* RTCTMR */
120 s
->status
|= 1 << 8; /* RTCTMR */
121 if ((s
->rtc
.ctrl
>> 1) & 1) { /* RTC_AL_EN */
122 if (s
->rtc
.alm_sec
== 0)
123 s
->status
|= 1 << 9; /* RTCALM */
126 if (s
->rtc
.next_comp
<= 0) {
127 s
->rtc
.next
-= muldiv64((int16_t) s
->rtc
.comp
, 1000, 0x8000);
128 s
->rtc
.next_comp
= 3600;
133 static void menelaus_reset(I2CSlave
*i2c
)
135 MenelausState
*s
= TWL92230(i2c
);
139 s
->vcore
[0] = 0x0c; /* XXX: X-loader needs 0x8c? check! */
144 s
->dcdc
[0] = 0x33; /* Depends on wiring */
150 s
->ldo
[3] = 0x00; /* Depends on wiring */
151 s
->ldo
[4] = 0x03; /* Depends on wiring */
168 s
->mmc_ctrl
[0] = 0x03;
169 s
->mmc_ctrl
[1] = 0xc0;
170 s
->mmc_ctrl
[2] = 0x00;
171 s
->mmc_debounce
= 0x05;
174 menelaus_rtc_stop(s
);
176 s
->rtc
.comp
= 0x0000;
178 s
->rtc
.sec_offset
= 0;
179 s
->rtc
.next_comp
= 1800;
180 s
->rtc
.alm_sec
= 1800;
181 s
->rtc
.alm
.tm_sec
= 0x00;
182 s
->rtc
.alm
.tm_min
= 0x00;
183 s
->rtc
.alm
.tm_hour
= 0x00;
184 s
->rtc
.alm
.tm_mday
= 0x01;
185 s
->rtc
.alm
.tm_mon
= 0x00;
186 s
->rtc
.alm
.tm_year
= 2004;
190 static void menelaus_gpio_set(void *opaque
, int line
, int level
)
192 MenelausState
*s
= (MenelausState
*) opaque
;
195 /* No interrupt generated */
196 s
->inputs
&= ~(1 << line
);
197 s
->inputs
|= level
<< line
;
201 if (!s
->pwrbtn_state
&& level
) {
202 s
->status
|= 1 << 11; /* PSHBTN */
205 s
->pwrbtn_state
= level
;
208 #define MENELAUS_REV 0x01
209 #define MENELAUS_VCORE_CTRL1 0x02
210 #define MENELAUS_VCORE_CTRL2 0x03
211 #define MENELAUS_VCORE_CTRL3 0x04
212 #define MENELAUS_VCORE_CTRL4 0x05
213 #define MENELAUS_VCORE_CTRL5 0x06
214 #define MENELAUS_DCDC_CTRL1 0x07
215 #define MENELAUS_DCDC_CTRL2 0x08
216 #define MENELAUS_DCDC_CTRL3 0x09
217 #define MENELAUS_LDO_CTRL1 0x0a
218 #define MENELAUS_LDO_CTRL2 0x0b
219 #define MENELAUS_LDO_CTRL3 0x0c
220 #define MENELAUS_LDO_CTRL4 0x0d
221 #define MENELAUS_LDO_CTRL5 0x0e
222 #define MENELAUS_LDO_CTRL6 0x0f
223 #define MENELAUS_LDO_CTRL7 0x10
224 #define MENELAUS_LDO_CTRL8 0x11
225 #define MENELAUS_SLEEP_CTRL1 0x12
226 #define MENELAUS_SLEEP_CTRL2 0x13
227 #define MENELAUS_DEVICE_OFF 0x14
228 #define MENELAUS_OSC_CTRL 0x15
229 #define MENELAUS_DETECT_CTRL 0x16
230 #define MENELAUS_INT_MASK1 0x17
231 #define MENELAUS_INT_MASK2 0x18
232 #define MENELAUS_INT_STATUS1 0x19
233 #define MENELAUS_INT_STATUS2 0x1a
234 #define MENELAUS_INT_ACK1 0x1b
235 #define MENELAUS_INT_ACK2 0x1c
236 #define MENELAUS_GPIO_CTRL 0x1d
237 #define MENELAUS_GPIO_IN 0x1e
238 #define MENELAUS_GPIO_OUT 0x1f
239 #define MENELAUS_BBSMS 0x20
240 #define MENELAUS_RTC_CTRL 0x21
241 #define MENELAUS_RTC_UPDATE 0x22
242 #define MENELAUS_RTC_SEC 0x23
243 #define MENELAUS_RTC_MIN 0x24
244 #define MENELAUS_RTC_HR 0x25
245 #define MENELAUS_RTC_DAY 0x26
246 #define MENELAUS_RTC_MON 0x27
247 #define MENELAUS_RTC_YR 0x28
248 #define MENELAUS_RTC_WKDAY 0x29
249 #define MENELAUS_RTC_AL_SEC 0x2a
250 #define MENELAUS_RTC_AL_MIN 0x2b
251 #define MENELAUS_RTC_AL_HR 0x2c
252 #define MENELAUS_RTC_AL_DAY 0x2d
253 #define MENELAUS_RTC_AL_MON 0x2e
254 #define MENELAUS_RTC_AL_YR 0x2f
255 #define MENELAUS_RTC_COMP_MSB 0x30
256 #define MENELAUS_RTC_COMP_LSB 0x31
257 #define MENELAUS_S1_PULL_EN 0x32
258 #define MENELAUS_S1_PULL_DIR 0x33
259 #define MENELAUS_S2_PULL_EN 0x34
260 #define MENELAUS_S2_PULL_DIR 0x35
261 #define MENELAUS_MCT_CTRL1 0x36
262 #define MENELAUS_MCT_CTRL2 0x37
263 #define MENELAUS_MCT_CTRL3 0x38
264 #define MENELAUS_MCT_PIN_ST 0x39
265 #define MENELAUS_DEBOUNCE1 0x3a
267 static uint8_t menelaus_read(void *opaque
, uint8_t addr
)
269 MenelausState
*s
= (MenelausState
*) opaque
;
276 case MENELAUS_VCORE_CTRL5
: reg
++;
277 case MENELAUS_VCORE_CTRL4
: reg
++;
278 case MENELAUS_VCORE_CTRL3
: reg
++;
279 case MENELAUS_VCORE_CTRL2
: reg
++;
280 case MENELAUS_VCORE_CTRL1
:
281 return s
->vcore
[reg
];
283 case MENELAUS_DCDC_CTRL3
: reg
++;
284 case MENELAUS_DCDC_CTRL2
: reg
++;
285 case MENELAUS_DCDC_CTRL1
:
288 case MENELAUS_LDO_CTRL8
: reg
++;
289 case MENELAUS_LDO_CTRL7
: reg
++;
290 case MENELAUS_LDO_CTRL6
: reg
++;
291 case MENELAUS_LDO_CTRL5
: reg
++;
292 case MENELAUS_LDO_CTRL4
: reg
++;
293 case MENELAUS_LDO_CTRL3
: reg
++;
294 case MENELAUS_LDO_CTRL2
: reg
++;
295 case MENELAUS_LDO_CTRL1
:
298 case MENELAUS_SLEEP_CTRL2
: reg
++;
299 case MENELAUS_SLEEP_CTRL1
:
300 return s
->sleep
[reg
];
302 case MENELAUS_DEVICE_OFF
:
305 case MENELAUS_OSC_CTRL
:
306 return s
->osc
| (1 << 7); /* CLK32K_GOOD */
308 case MENELAUS_DETECT_CTRL
:
311 case MENELAUS_INT_MASK1
:
312 return (s
->mask
>> 0) & 0xff;
313 case MENELAUS_INT_MASK2
:
314 return (s
->mask
>> 8) & 0xff;
316 case MENELAUS_INT_STATUS1
:
317 return (s
->status
>> 0) & 0xff;
318 case MENELAUS_INT_STATUS2
:
319 return (s
->status
>> 8) & 0xff;
321 case MENELAUS_INT_ACK1
:
322 case MENELAUS_INT_ACK2
:
325 case MENELAUS_GPIO_CTRL
:
327 case MENELAUS_GPIO_IN
:
328 return s
->inputs
| (~s
->dir
& s
->outputs
);
329 case MENELAUS_GPIO_OUT
:
335 case MENELAUS_RTC_CTRL
:
337 case MENELAUS_RTC_UPDATE
:
339 case MENELAUS_RTC_SEC
:
340 menelaus_rtc_update(s
);
341 return to_bcd(s
->rtc
.tm
.tm_sec
);
342 case MENELAUS_RTC_MIN
:
343 menelaus_rtc_update(s
);
344 return to_bcd(s
->rtc
.tm
.tm_min
);
345 case MENELAUS_RTC_HR
:
346 menelaus_rtc_update(s
);
347 if ((s
->rtc
.ctrl
>> 2) & 1) /* MODE12_n24 */
348 return to_bcd((s
->rtc
.tm
.tm_hour
% 12) + 1) |
349 (!!(s
->rtc
.tm
.tm_hour
>= 12) << 7); /* PM_nAM */
351 return to_bcd(s
->rtc
.tm
.tm_hour
);
352 case MENELAUS_RTC_DAY
:
353 menelaus_rtc_update(s
);
354 return to_bcd(s
->rtc
.tm
.tm_mday
);
355 case MENELAUS_RTC_MON
:
356 menelaus_rtc_update(s
);
357 return to_bcd(s
->rtc
.tm
.tm_mon
+ 1);
358 case MENELAUS_RTC_YR
:
359 menelaus_rtc_update(s
);
360 return to_bcd(s
->rtc
.tm
.tm_year
- 2000);
361 case MENELAUS_RTC_WKDAY
:
362 menelaus_rtc_update(s
);
363 return to_bcd(s
->rtc
.tm
.tm_wday
);
364 case MENELAUS_RTC_AL_SEC
:
365 return to_bcd(s
->rtc
.alm
.tm_sec
);
366 case MENELAUS_RTC_AL_MIN
:
367 return to_bcd(s
->rtc
.alm
.tm_min
);
368 case MENELAUS_RTC_AL_HR
:
369 if ((s
->rtc
.ctrl
>> 2) & 1) /* MODE12_n24 */
370 return to_bcd((s
->rtc
.alm
.tm_hour
% 12) + 1) |
371 (!!(s
->rtc
.alm
.tm_hour
>= 12) << 7);/* AL_PM_nAM */
373 return to_bcd(s
->rtc
.alm
.tm_hour
);
374 case MENELAUS_RTC_AL_DAY
:
375 return to_bcd(s
->rtc
.alm
.tm_mday
);
376 case MENELAUS_RTC_AL_MON
:
377 return to_bcd(s
->rtc
.alm
.tm_mon
+ 1);
378 case MENELAUS_RTC_AL_YR
:
379 return to_bcd(s
->rtc
.alm
.tm_year
- 2000);
380 case MENELAUS_RTC_COMP_MSB
:
381 return (s
->rtc
.comp
>> 8) & 0xff;
382 case MENELAUS_RTC_COMP_LSB
:
383 return (s
->rtc
.comp
>> 0) & 0xff;
385 case MENELAUS_S1_PULL_EN
:
387 case MENELAUS_S1_PULL_DIR
:
389 case MENELAUS_S2_PULL_EN
:
391 case MENELAUS_S2_PULL_DIR
:
394 case MENELAUS_MCT_CTRL3
: reg
++;
395 case MENELAUS_MCT_CTRL2
: reg
++;
396 case MENELAUS_MCT_CTRL1
:
397 return s
->mmc_ctrl
[reg
];
398 case MENELAUS_MCT_PIN_ST
:
399 /* TODO: return the real Card Detect */
401 case MENELAUS_DEBOUNCE1
:
402 return s
->mmc_debounce
;
406 printf("%s: unknown register %02x\n", __FUNCTION__
, addr
);
413 static void menelaus_write(void *opaque
, uint8_t addr
, uint8_t value
)
415 MenelausState
*s
= (MenelausState
*) opaque
;
421 case MENELAUS_VCORE_CTRL1
:
422 s
->vcore
[0] = (value
& 0xe) | MIN(value
& 0x1f, 0x12);
424 case MENELAUS_VCORE_CTRL2
:
427 case MENELAUS_VCORE_CTRL3
:
428 s
->vcore
[2] = MIN(value
& 0x1f, 0x12);
430 case MENELAUS_VCORE_CTRL4
:
431 s
->vcore
[3] = MIN(value
& 0x1f, 0x12);
433 case MENELAUS_VCORE_CTRL5
:
434 s
->vcore
[4] = value
& 3;
436 * auto set to 3 on M_Active, nRESWARM
437 * auto set to 0 on M_WaitOn, M_Backup
441 case MENELAUS_DCDC_CTRL1
:
442 s
->dcdc
[0] = value
& 0x3f;
444 case MENELAUS_DCDC_CTRL2
:
445 s
->dcdc
[1] = value
& 0x07;
447 * auto set to 3 on M_Active, nRESWARM
448 * auto set to 0 on M_WaitOn, M_Backup
451 case MENELAUS_DCDC_CTRL3
:
452 s
->dcdc
[2] = value
& 0x07;
455 case MENELAUS_LDO_CTRL1
:
458 case MENELAUS_LDO_CTRL2
:
459 s
->ldo
[1] = value
& 0x7f;
461 * auto set to 0x7e on M_WaitOn, M_Backup
464 case MENELAUS_LDO_CTRL3
:
465 s
->ldo
[2] = value
& 3;
467 * auto set to 3 on M_Active, nRESWARM
468 * auto set to 0 on M_WaitOn, M_Backup
471 case MENELAUS_LDO_CTRL4
:
472 s
->ldo
[3] = value
& 3;
474 * auto set to 3 on M_Active, nRESWARM
475 * auto set to 0 on M_WaitOn, M_Backup
478 case MENELAUS_LDO_CTRL5
:
479 s
->ldo
[4] = value
& 3;
481 * auto set to 3 on M_Active, nRESWARM
482 * auto set to 0 on M_WaitOn, M_Backup
485 case MENELAUS_LDO_CTRL6
:
486 s
->ldo
[5] = value
& 3;
488 case MENELAUS_LDO_CTRL7
:
489 s
->ldo
[6] = value
& 3;
491 case MENELAUS_LDO_CTRL8
:
492 s
->ldo
[7] = value
& 3;
495 case MENELAUS_SLEEP_CTRL2
: reg
++;
496 case MENELAUS_SLEEP_CTRL1
:
497 s
->sleep
[reg
] = value
;
500 case MENELAUS_DEVICE_OFF
:
502 menelaus_reset(I2C_SLAVE(s
));
506 case MENELAUS_OSC_CTRL
:
510 case MENELAUS_DETECT_CTRL
:
511 s
->detect
= value
& 0x7f;
514 case MENELAUS_INT_MASK1
:
516 s
->mask
|= value
<< 0;
519 case MENELAUS_INT_MASK2
:
521 s
->mask
|= value
<< 8;
525 case MENELAUS_INT_ACK1
:
526 s
->status
&= ~(((uint16_t) value
) << 0);
529 case MENELAUS_INT_ACK2
:
530 s
->status
&= ~(((uint16_t) value
) << 8);
534 case MENELAUS_GPIO_CTRL
:
535 for (line
= 0; line
< 3; line
++) {
536 if (((s
->dir
^ value
) >> line
) & 1) {
537 qemu_set_irq(s
->out
[line
],
538 ((s
->outputs
& ~s
->dir
) >> line
) & 1);
541 s
->dir
= value
& 0x67;
543 case MENELAUS_GPIO_OUT
:
544 for (line
= 0; line
< 3; line
++) {
545 if ((((s
->outputs
^ value
) & ~s
->dir
) >> line
) & 1) {
546 qemu_set_irq(s
->out
[line
], (s
->outputs
>> line
) & 1);
549 s
->outputs
= value
& 0x07;
556 case MENELAUS_RTC_CTRL
:
557 if ((s
->rtc
.ctrl
^ value
) & 1) { /* RTC_EN */
559 menelaus_rtc_start(s
);
561 menelaus_rtc_stop(s
);
563 s
->rtc
.ctrl
= value
& 0x1f;
564 menelaus_alm_update(s
);
566 case MENELAUS_RTC_UPDATE
:
567 menelaus_rtc_update(s
);
568 memcpy(&tm
, &s
->rtc
.tm
, sizeof(tm
));
569 switch (value
& 0xf) {
573 tm
.tm_sec
= s
->rtc
.new.tm_sec
;
576 tm
.tm_min
= s
->rtc
.new.tm_min
;
579 if (s
->rtc
.new.tm_hour
> 23)
581 tm
.tm_hour
= s
->rtc
.new.tm_hour
;
584 if (s
->rtc
.new.tm_mday
< 1)
586 /* TODO check range */
587 tm
.tm_mday
= s
->rtc
.new.tm_mday
;
590 if (s
->rtc
.new.tm_mon
< 0 || s
->rtc
.new.tm_mon
> 11)
592 tm
.tm_mon
= s
->rtc
.new.tm_mon
;
595 tm
.tm_year
= s
->rtc
.new.tm_year
;
598 /* TODO set .tm_mday instead */
599 tm
.tm_wday
= s
->rtc
.new.tm_wday
;
602 if (s
->rtc
.new.tm_hour
> 23)
604 if (s
->rtc
.new.tm_mday
< 1)
606 if (s
->rtc
.new.tm_mon
< 0 || s
->rtc
.new.tm_mon
> 11)
608 tm
.tm_sec
= s
->rtc
.new.tm_sec
;
609 tm
.tm_min
= s
->rtc
.new.tm_min
;
610 tm
.tm_hour
= s
->rtc
.new.tm_hour
;
611 tm
.tm_mday
= s
->rtc
.new.tm_mday
;
612 tm
.tm_mon
= s
->rtc
.new.tm_mon
;
613 tm
.tm_year
= s
->rtc
.new.tm_year
;
617 fprintf(stderr
, "%s: bad RTC_UPDATE value %02x\n",
618 __FUNCTION__
, value
);
619 s
->status
|= 1 << 10; /* RTCERR */
622 s
->rtc
.sec_offset
= qemu_timedate_diff(&tm
);
624 case MENELAUS_RTC_SEC
:
625 s
->rtc
.tm
.tm_sec
= from_bcd(value
& 0x7f);
627 case MENELAUS_RTC_MIN
:
628 s
->rtc
.tm
.tm_min
= from_bcd(value
& 0x7f);
630 case MENELAUS_RTC_HR
:
631 s
->rtc
.tm
.tm_hour
= (s
->rtc
.ctrl
& (1 << 2)) ? /* MODE12_n24 */
632 MIN(from_bcd(value
& 0x3f), 12) + ((value
>> 7) ? 11 : -1) :
633 from_bcd(value
& 0x3f);
635 case MENELAUS_RTC_DAY
:
636 s
->rtc
.tm
.tm_mday
= from_bcd(value
);
638 case MENELAUS_RTC_MON
:
639 s
->rtc
.tm
.tm_mon
= MAX(1, from_bcd(value
)) - 1;
641 case MENELAUS_RTC_YR
:
642 s
->rtc
.tm
.tm_year
= 2000 + from_bcd(value
);
644 case MENELAUS_RTC_WKDAY
:
645 s
->rtc
.tm
.tm_mday
= from_bcd(value
);
647 case MENELAUS_RTC_AL_SEC
:
648 s
->rtc
.alm
.tm_sec
= from_bcd(value
& 0x7f);
649 menelaus_alm_update(s
);
651 case MENELAUS_RTC_AL_MIN
:
652 s
->rtc
.alm
.tm_min
= from_bcd(value
& 0x7f);
653 menelaus_alm_update(s
);
655 case MENELAUS_RTC_AL_HR
:
656 s
->rtc
.alm
.tm_hour
= (s
->rtc
.ctrl
& (1 << 2)) ? /* MODE12_n24 */
657 MIN(from_bcd(value
& 0x3f), 12) + ((value
>> 7) ? 11 : -1) :
658 from_bcd(value
& 0x3f);
659 menelaus_alm_update(s
);
661 case MENELAUS_RTC_AL_DAY
:
662 s
->rtc
.alm
.tm_mday
= from_bcd(value
);
663 menelaus_alm_update(s
);
665 case MENELAUS_RTC_AL_MON
:
666 s
->rtc
.alm
.tm_mon
= MAX(1, from_bcd(value
)) - 1;
667 menelaus_alm_update(s
);
669 case MENELAUS_RTC_AL_YR
:
670 s
->rtc
.alm
.tm_year
= 2000 + from_bcd(value
);
671 menelaus_alm_update(s
);
673 case MENELAUS_RTC_COMP_MSB
:
675 s
->rtc
.comp
|= value
<< 8;
677 case MENELAUS_RTC_COMP_LSB
:
678 s
->rtc
.comp
&= 0xff << 8;
679 s
->rtc
.comp
|= value
;
682 case MENELAUS_S1_PULL_EN
:
685 case MENELAUS_S1_PULL_DIR
:
686 s
->pull
[1] = value
& 0x1f;
688 case MENELAUS_S2_PULL_EN
:
691 case MENELAUS_S2_PULL_DIR
:
692 s
->pull
[3] = value
& 0x1f;
695 case MENELAUS_MCT_CTRL1
:
696 s
->mmc_ctrl
[0] = value
& 0x7f;
698 case MENELAUS_MCT_CTRL2
:
699 s
->mmc_ctrl
[1] = value
;
700 /* TODO update Card Detect interrupts */
702 case MENELAUS_MCT_CTRL3
:
703 s
->mmc_ctrl
[2] = value
& 0xf;
705 case MENELAUS_DEBOUNCE1
:
706 s
->mmc_debounce
= value
& 0x3f;
711 printf("%s: unknown register %02x\n", __FUNCTION__
, addr
);
716 static int menelaus_event(I2CSlave
*i2c
, enum i2c_event event
)
718 MenelausState
*s
= TWL92230(i2c
);
720 if (event
== I2C_START_SEND
)
726 static int menelaus_tx(I2CSlave
*i2c
, uint8_t data
)
728 MenelausState
*s
= TWL92230(i2c
);
730 /* Interpret register address byte */
735 menelaus_write(s
, s
->reg
++, data
);
740 static int menelaus_rx(I2CSlave
*i2c
)
742 MenelausState
*s
= TWL92230(i2c
);
744 return menelaus_read(s
, s
->reg
++);
747 /* Save restore 32 bit int as uint16_t
748 This is a Big hack, but it is how the old state did it.
749 Or we broke compatibility in the state, or we can't use struct tm
752 static int get_int32_as_uint16(QEMUFile
*f
, void *pv
, size_t size
,
756 *v
= qemu_get_be16(f
);
760 static int put_int32_as_uint16(QEMUFile
*f
, void *pv
, size_t size
,
761 VMStateField
*field
, QJSON
*vmdesc
)
764 qemu_put_be16(f
, *v
);
769 static const VMStateInfo vmstate_hack_int32_as_uint16
= {
770 .name
= "int32_as_uint16",
771 .get
= get_int32_as_uint16
,
772 .put
= put_int32_as_uint16
,
775 #define VMSTATE_UINT16_HACK(_f, _s) \
776 VMSTATE_SINGLE(_f, _s, 0, vmstate_hack_int32_as_uint16, int32_t)
779 static const VMStateDescription vmstate_menelaus_tm
= {
780 .name
= "menelaus_tm",
782 .minimum_version_id
= 0,
783 .fields
= (VMStateField
[]) {
784 VMSTATE_UINT16_HACK(tm_sec
, struct tm
),
785 VMSTATE_UINT16_HACK(tm_min
, struct tm
),
786 VMSTATE_UINT16_HACK(tm_hour
, struct tm
),
787 VMSTATE_UINT16_HACK(tm_mday
, struct tm
),
788 VMSTATE_UINT16_HACK(tm_min
, struct tm
),
789 VMSTATE_UINT16_HACK(tm_year
, struct tm
),
790 VMSTATE_END_OF_LIST()
794 static void menelaus_pre_save(void *opaque
)
796 MenelausState
*s
= opaque
;
797 /* Should be <= 1000 */
798 s
->rtc_next_vmstate
= s
->rtc
.next
- qemu_clock_get_ms(rtc_clock
);
801 static int menelaus_post_load(void *opaque
, int version_id
)
803 MenelausState
*s
= opaque
;
805 if (s
->rtc
.ctrl
& 1) /* RTC_EN */
806 menelaus_rtc_stop(s
);
808 s
->rtc
.next
= s
->rtc_next_vmstate
;
810 menelaus_alm_update(s
);
812 if (s
->rtc
.ctrl
& 1) /* RTC_EN */
813 menelaus_rtc_start(s
);
817 static const VMStateDescription vmstate_menelaus
= {
820 .minimum_version_id
= 0,
821 .pre_save
= menelaus_pre_save
,
822 .post_load
= menelaus_post_load
,
823 .fields
= (VMStateField
[]) {
824 VMSTATE_INT32(firstbyte
, MenelausState
),
825 VMSTATE_UINT8(reg
, MenelausState
),
826 VMSTATE_UINT8_ARRAY(vcore
, MenelausState
, 5),
827 VMSTATE_UINT8_ARRAY(dcdc
, MenelausState
, 3),
828 VMSTATE_UINT8_ARRAY(ldo
, MenelausState
, 8),
829 VMSTATE_UINT8_ARRAY(sleep
, MenelausState
, 2),
830 VMSTATE_UINT8(osc
, MenelausState
),
831 VMSTATE_UINT8(detect
, MenelausState
),
832 VMSTATE_UINT16(mask
, MenelausState
),
833 VMSTATE_UINT16(status
, MenelausState
),
834 VMSTATE_UINT8(dir
, MenelausState
),
835 VMSTATE_UINT8(inputs
, MenelausState
),
836 VMSTATE_UINT8(outputs
, MenelausState
),
837 VMSTATE_UINT8(bbsms
, MenelausState
),
838 VMSTATE_UINT8_ARRAY(pull
, MenelausState
, 4),
839 VMSTATE_UINT8_ARRAY(mmc_ctrl
, MenelausState
, 3),
840 VMSTATE_UINT8(mmc_debounce
, MenelausState
),
841 VMSTATE_UINT8(rtc
.ctrl
, MenelausState
),
842 VMSTATE_UINT16(rtc
.comp
, MenelausState
),
843 VMSTATE_UINT16(rtc_next_vmstate
, MenelausState
),
844 VMSTATE_STRUCT(rtc
.new, MenelausState
, 0, vmstate_menelaus_tm
,
846 VMSTATE_STRUCT(rtc
.alm
, MenelausState
, 0, vmstate_menelaus_tm
,
848 VMSTATE_UINT8(pwrbtn_state
, MenelausState
),
849 VMSTATE_I2C_SLAVE(parent_obj
, MenelausState
),
850 VMSTATE_END_OF_LIST()
854 static int twl92230_init(I2CSlave
*i2c
)
856 DeviceState
*dev
= DEVICE(i2c
);
857 MenelausState
*s
= TWL92230(i2c
);
859 s
->rtc
.hz_tm
= timer_new_ms(rtc_clock
, menelaus_rtc_hz
, s
);
860 /* Three output pins plus one interrupt pin. */
861 qdev_init_gpio_out(dev
, s
->out
, 4);
863 /* Three input pins plus one power-button pin. */
864 qdev_init_gpio_in(dev
, menelaus_gpio_set
, 4);
871 static void twl92230_class_init(ObjectClass
*klass
, void *data
)
873 DeviceClass
*dc
= DEVICE_CLASS(klass
);
874 I2CSlaveClass
*sc
= I2C_SLAVE_CLASS(klass
);
876 sc
->init
= twl92230_init
;
877 sc
->event
= menelaus_event
;
878 sc
->recv
= menelaus_rx
;
879 sc
->send
= menelaus_tx
;
880 dc
->vmsd
= &vmstate_menelaus
;
883 static const TypeInfo twl92230_info
= {
884 .name
= TYPE_TWL92230
,
885 .parent
= TYPE_I2C_SLAVE
,
886 .instance_size
= sizeof(MenelausState
),
887 .class_init
= twl92230_class_init
,
890 static void twl92230_register_types(void)
892 type_register_static(&twl92230_info
);
895 type_init(twl92230_register_types
)