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"
25 #include "qemu/timer.h"
26 #include "hw/i2c/i2c.h"
27 #include "sysemu/sysemu.h"
28 #include "ui/console.h"
30 #include "qemu/module.h"
34 #define TYPE_TWL92230 "twl92230"
35 #define TWL92230(obj) OBJECT_CHECK(MenelausState, (obj), TYPE_TWL92230)
37 typedef struct MenelausState
{
70 uint16_t rtc_next_vmstate
;
75 static inline void menelaus_update(MenelausState
*s
)
77 qemu_set_irq(s
->out
[3], s
->status
& ~s
->mask
);
80 static inline void menelaus_rtc_start(MenelausState
*s
)
82 s
->rtc
.next
+= qemu_clock_get_ms(rtc_clock
);
83 timer_mod(s
->rtc
.hz_tm
, s
->rtc
.next
);
86 static inline void menelaus_rtc_stop(MenelausState
*s
)
88 timer_del(s
->rtc
.hz_tm
);
89 s
->rtc
.next
-= qemu_clock_get_ms(rtc_clock
);
94 static void menelaus_rtc_update(MenelausState
*s
)
96 qemu_get_timedate(&s
->rtc
.tm
, s
->rtc
.sec_offset
);
99 static void menelaus_alm_update(MenelausState
*s
)
101 if ((s
->rtc
.ctrl
& 3) == 3)
102 s
->rtc
.alm_sec
= qemu_timedate_diff(&s
->rtc
.alm
) - s
->rtc
.sec_offset
;
105 static void menelaus_rtc_hz(void *opaque
)
107 MenelausState
*s
= (MenelausState
*) opaque
;
112 timer_mod(s
->rtc
.hz_tm
, s
->rtc
.next
);
113 if ((s
->rtc
.ctrl
>> 3) & 3) { /* EVERY */
114 menelaus_rtc_update(s
);
115 if (((s
->rtc
.ctrl
>> 3) & 3) == 1 && !s
->rtc
.tm
.tm_sec
)
116 s
->status
|= 1 << 8; /* RTCTMR */
117 else if (((s
->rtc
.ctrl
>> 3) & 3) == 2 && !s
->rtc
.tm
.tm_min
)
118 s
->status
|= 1 << 8; /* RTCTMR */
119 else if (!s
->rtc
.tm
.tm_hour
)
120 s
->status
|= 1 << 8; /* RTCTMR */
122 s
->status
|= 1 << 8; /* RTCTMR */
123 if ((s
->rtc
.ctrl
>> 1) & 1) { /* RTC_AL_EN */
124 if (s
->rtc
.alm_sec
== 0)
125 s
->status
|= 1 << 9; /* RTCALM */
128 if (s
->rtc
.next_comp
<= 0) {
129 s
->rtc
.next
-= muldiv64((int16_t) s
->rtc
.comp
, 1000, 0x8000);
130 s
->rtc
.next_comp
= 3600;
135 static void menelaus_reset(I2CSlave
*i2c
)
137 MenelausState
*s
= TWL92230(i2c
);
141 s
->vcore
[0] = 0x0c; /* XXX: X-loader needs 0x8c? check! */
146 s
->dcdc
[0] = 0x33; /* Depends on wiring */
152 s
->ldo
[3] = 0x00; /* Depends on wiring */
153 s
->ldo
[4] = 0x03; /* Depends on wiring */
170 s
->mmc_ctrl
[0] = 0x03;
171 s
->mmc_ctrl
[1] = 0xc0;
172 s
->mmc_ctrl
[2] = 0x00;
173 s
->mmc_debounce
= 0x05;
176 menelaus_rtc_stop(s
);
178 s
->rtc
.comp
= 0x0000;
180 s
->rtc
.sec_offset
= 0;
181 s
->rtc
.next_comp
= 1800;
182 s
->rtc
.alm_sec
= 1800;
183 s
->rtc
.alm
.tm_sec
= 0x00;
184 s
->rtc
.alm
.tm_min
= 0x00;
185 s
->rtc
.alm
.tm_hour
= 0x00;
186 s
->rtc
.alm
.tm_mday
= 0x01;
187 s
->rtc
.alm
.tm_mon
= 0x00;
188 s
->rtc
.alm
.tm_year
= 2004;
192 static void menelaus_gpio_set(void *opaque
, int line
, int level
)
194 MenelausState
*s
= (MenelausState
*) opaque
;
197 /* No interrupt generated */
198 s
->inputs
&= ~(1 << line
);
199 s
->inputs
|= level
<< line
;
203 if (!s
->pwrbtn_state
&& level
) {
204 s
->status
|= 1 << 11; /* PSHBTN */
207 s
->pwrbtn_state
= level
;
210 #define MENELAUS_REV 0x01
211 #define MENELAUS_VCORE_CTRL1 0x02
212 #define MENELAUS_VCORE_CTRL2 0x03
213 #define MENELAUS_VCORE_CTRL3 0x04
214 #define MENELAUS_VCORE_CTRL4 0x05
215 #define MENELAUS_VCORE_CTRL5 0x06
216 #define MENELAUS_DCDC_CTRL1 0x07
217 #define MENELAUS_DCDC_CTRL2 0x08
218 #define MENELAUS_DCDC_CTRL3 0x09
219 #define MENELAUS_LDO_CTRL1 0x0a
220 #define MENELAUS_LDO_CTRL2 0x0b
221 #define MENELAUS_LDO_CTRL3 0x0c
222 #define MENELAUS_LDO_CTRL4 0x0d
223 #define MENELAUS_LDO_CTRL5 0x0e
224 #define MENELAUS_LDO_CTRL6 0x0f
225 #define MENELAUS_LDO_CTRL7 0x10
226 #define MENELAUS_LDO_CTRL8 0x11
227 #define MENELAUS_SLEEP_CTRL1 0x12
228 #define MENELAUS_SLEEP_CTRL2 0x13
229 #define MENELAUS_DEVICE_OFF 0x14
230 #define MENELAUS_OSC_CTRL 0x15
231 #define MENELAUS_DETECT_CTRL 0x16
232 #define MENELAUS_INT_MASK1 0x17
233 #define MENELAUS_INT_MASK2 0x18
234 #define MENELAUS_INT_STATUS1 0x19
235 #define MENELAUS_INT_STATUS2 0x1a
236 #define MENELAUS_INT_ACK1 0x1b
237 #define MENELAUS_INT_ACK2 0x1c
238 #define MENELAUS_GPIO_CTRL 0x1d
239 #define MENELAUS_GPIO_IN 0x1e
240 #define MENELAUS_GPIO_OUT 0x1f
241 #define MENELAUS_BBSMS 0x20
242 #define MENELAUS_RTC_CTRL 0x21
243 #define MENELAUS_RTC_UPDATE 0x22
244 #define MENELAUS_RTC_SEC 0x23
245 #define MENELAUS_RTC_MIN 0x24
246 #define MENELAUS_RTC_HR 0x25
247 #define MENELAUS_RTC_DAY 0x26
248 #define MENELAUS_RTC_MON 0x27
249 #define MENELAUS_RTC_YR 0x28
250 #define MENELAUS_RTC_WKDAY 0x29
251 #define MENELAUS_RTC_AL_SEC 0x2a
252 #define MENELAUS_RTC_AL_MIN 0x2b
253 #define MENELAUS_RTC_AL_HR 0x2c
254 #define MENELAUS_RTC_AL_DAY 0x2d
255 #define MENELAUS_RTC_AL_MON 0x2e
256 #define MENELAUS_RTC_AL_YR 0x2f
257 #define MENELAUS_RTC_COMP_MSB 0x30
258 #define MENELAUS_RTC_COMP_LSB 0x31
259 #define MENELAUS_S1_PULL_EN 0x32
260 #define MENELAUS_S1_PULL_DIR 0x33
261 #define MENELAUS_S2_PULL_EN 0x34
262 #define MENELAUS_S2_PULL_DIR 0x35
263 #define MENELAUS_MCT_CTRL1 0x36
264 #define MENELAUS_MCT_CTRL2 0x37
265 #define MENELAUS_MCT_CTRL3 0x38
266 #define MENELAUS_MCT_PIN_ST 0x39
267 #define MENELAUS_DEBOUNCE1 0x3a
269 static uint8_t menelaus_read(void *opaque
, uint8_t addr
)
271 MenelausState
*s
= (MenelausState
*) opaque
;
278 case MENELAUS_VCORE_CTRL5
: reg
++;
279 case MENELAUS_VCORE_CTRL4
: reg
++;
280 case MENELAUS_VCORE_CTRL3
: reg
++;
281 case MENELAUS_VCORE_CTRL2
: reg
++;
282 case MENELAUS_VCORE_CTRL1
:
283 return s
->vcore
[reg
];
285 case MENELAUS_DCDC_CTRL3
: reg
++;
286 case MENELAUS_DCDC_CTRL2
: reg
++;
287 case MENELAUS_DCDC_CTRL1
:
290 case MENELAUS_LDO_CTRL8
: reg
++;
291 case MENELAUS_LDO_CTRL7
: reg
++;
292 case MENELAUS_LDO_CTRL6
: reg
++;
293 case MENELAUS_LDO_CTRL5
: reg
++;
294 case MENELAUS_LDO_CTRL4
: reg
++;
295 case MENELAUS_LDO_CTRL3
: reg
++;
296 case MENELAUS_LDO_CTRL2
: reg
++;
297 case MENELAUS_LDO_CTRL1
:
300 case MENELAUS_SLEEP_CTRL2
: reg
++;
301 case MENELAUS_SLEEP_CTRL1
:
302 return s
->sleep
[reg
];
304 case MENELAUS_DEVICE_OFF
:
307 case MENELAUS_OSC_CTRL
:
308 return s
->osc
| (1 << 7); /* CLK32K_GOOD */
310 case MENELAUS_DETECT_CTRL
:
313 case MENELAUS_INT_MASK1
:
314 return (s
->mask
>> 0) & 0xff;
315 case MENELAUS_INT_MASK2
:
316 return (s
->mask
>> 8) & 0xff;
318 case MENELAUS_INT_STATUS1
:
319 return (s
->status
>> 0) & 0xff;
320 case MENELAUS_INT_STATUS2
:
321 return (s
->status
>> 8) & 0xff;
323 case MENELAUS_INT_ACK1
:
324 case MENELAUS_INT_ACK2
:
327 case MENELAUS_GPIO_CTRL
:
329 case MENELAUS_GPIO_IN
:
330 return s
->inputs
| (~s
->dir
& s
->outputs
);
331 case MENELAUS_GPIO_OUT
:
337 case MENELAUS_RTC_CTRL
:
339 case MENELAUS_RTC_UPDATE
:
341 case MENELAUS_RTC_SEC
:
342 menelaus_rtc_update(s
);
343 return to_bcd(s
->rtc
.tm
.tm_sec
);
344 case MENELAUS_RTC_MIN
:
345 menelaus_rtc_update(s
);
346 return to_bcd(s
->rtc
.tm
.tm_min
);
347 case MENELAUS_RTC_HR
:
348 menelaus_rtc_update(s
);
349 if ((s
->rtc
.ctrl
>> 2) & 1) /* MODE12_n24 */
350 return to_bcd((s
->rtc
.tm
.tm_hour
% 12) + 1) |
351 (!!(s
->rtc
.tm
.tm_hour
>= 12) << 7); /* PM_nAM */
353 return to_bcd(s
->rtc
.tm
.tm_hour
);
354 case MENELAUS_RTC_DAY
:
355 menelaus_rtc_update(s
);
356 return to_bcd(s
->rtc
.tm
.tm_mday
);
357 case MENELAUS_RTC_MON
:
358 menelaus_rtc_update(s
);
359 return to_bcd(s
->rtc
.tm
.tm_mon
+ 1);
360 case MENELAUS_RTC_YR
:
361 menelaus_rtc_update(s
);
362 return to_bcd(s
->rtc
.tm
.tm_year
- 2000);
363 case MENELAUS_RTC_WKDAY
:
364 menelaus_rtc_update(s
);
365 return to_bcd(s
->rtc
.tm
.tm_wday
);
366 case MENELAUS_RTC_AL_SEC
:
367 return to_bcd(s
->rtc
.alm
.tm_sec
);
368 case MENELAUS_RTC_AL_MIN
:
369 return to_bcd(s
->rtc
.alm
.tm_min
);
370 case MENELAUS_RTC_AL_HR
:
371 if ((s
->rtc
.ctrl
>> 2) & 1) /* MODE12_n24 */
372 return to_bcd((s
->rtc
.alm
.tm_hour
% 12) + 1) |
373 (!!(s
->rtc
.alm
.tm_hour
>= 12) << 7);/* AL_PM_nAM */
375 return to_bcd(s
->rtc
.alm
.tm_hour
);
376 case MENELAUS_RTC_AL_DAY
:
377 return to_bcd(s
->rtc
.alm
.tm_mday
);
378 case MENELAUS_RTC_AL_MON
:
379 return to_bcd(s
->rtc
.alm
.tm_mon
+ 1);
380 case MENELAUS_RTC_AL_YR
:
381 return to_bcd(s
->rtc
.alm
.tm_year
- 2000);
382 case MENELAUS_RTC_COMP_MSB
:
383 return (s
->rtc
.comp
>> 8) & 0xff;
384 case MENELAUS_RTC_COMP_LSB
:
385 return (s
->rtc
.comp
>> 0) & 0xff;
387 case MENELAUS_S1_PULL_EN
:
389 case MENELAUS_S1_PULL_DIR
:
391 case MENELAUS_S2_PULL_EN
:
393 case MENELAUS_S2_PULL_DIR
:
396 case MENELAUS_MCT_CTRL3
: reg
++;
397 case MENELAUS_MCT_CTRL2
: reg
++;
398 case MENELAUS_MCT_CTRL1
:
399 return s
->mmc_ctrl
[reg
];
400 case MENELAUS_MCT_PIN_ST
:
401 /* TODO: return the real Card Detect */
403 case MENELAUS_DEBOUNCE1
:
404 return s
->mmc_debounce
;
408 printf("%s: unknown register %02x\n", __func__
, addr
);
415 static void menelaus_write(void *opaque
, uint8_t addr
, uint8_t value
)
417 MenelausState
*s
= (MenelausState
*) opaque
;
423 case MENELAUS_VCORE_CTRL1
:
424 s
->vcore
[0] = (value
& 0xe) | MIN(value
& 0x1f, 0x12);
426 case MENELAUS_VCORE_CTRL2
:
429 case MENELAUS_VCORE_CTRL3
:
430 s
->vcore
[2] = MIN(value
& 0x1f, 0x12);
432 case MENELAUS_VCORE_CTRL4
:
433 s
->vcore
[3] = MIN(value
& 0x1f, 0x12);
435 case MENELAUS_VCORE_CTRL5
:
436 s
->vcore
[4] = value
& 3;
438 * auto set to 3 on M_Active, nRESWARM
439 * auto set to 0 on M_WaitOn, M_Backup
443 case MENELAUS_DCDC_CTRL1
:
444 s
->dcdc
[0] = value
& 0x3f;
446 case MENELAUS_DCDC_CTRL2
:
447 s
->dcdc
[1] = value
& 0x07;
449 * auto set to 3 on M_Active, nRESWARM
450 * auto set to 0 on M_WaitOn, M_Backup
453 case MENELAUS_DCDC_CTRL3
:
454 s
->dcdc
[2] = value
& 0x07;
457 case MENELAUS_LDO_CTRL1
:
460 case MENELAUS_LDO_CTRL2
:
461 s
->ldo
[1] = value
& 0x7f;
463 * auto set to 0x7e on M_WaitOn, M_Backup
466 case MENELAUS_LDO_CTRL3
:
467 s
->ldo
[2] = value
& 3;
469 * auto set to 3 on M_Active, nRESWARM
470 * auto set to 0 on M_WaitOn, M_Backup
473 case MENELAUS_LDO_CTRL4
:
474 s
->ldo
[3] = value
& 3;
476 * auto set to 3 on M_Active, nRESWARM
477 * auto set to 0 on M_WaitOn, M_Backup
480 case MENELAUS_LDO_CTRL5
:
481 s
->ldo
[4] = value
& 3;
483 * auto set to 3 on M_Active, nRESWARM
484 * auto set to 0 on M_WaitOn, M_Backup
487 case MENELAUS_LDO_CTRL6
:
488 s
->ldo
[5] = value
& 3;
490 case MENELAUS_LDO_CTRL7
:
491 s
->ldo
[6] = value
& 3;
493 case MENELAUS_LDO_CTRL8
:
494 s
->ldo
[7] = value
& 3;
497 case MENELAUS_SLEEP_CTRL2
: reg
++;
498 case MENELAUS_SLEEP_CTRL1
:
499 s
->sleep
[reg
] = value
;
502 case MENELAUS_DEVICE_OFF
:
504 menelaus_reset(I2C_SLAVE(s
));
508 case MENELAUS_OSC_CTRL
:
512 case MENELAUS_DETECT_CTRL
:
513 s
->detect
= value
& 0x7f;
516 case MENELAUS_INT_MASK1
:
518 s
->mask
|= value
<< 0;
521 case MENELAUS_INT_MASK2
:
523 s
->mask
|= value
<< 8;
527 case MENELAUS_INT_ACK1
:
528 s
->status
&= ~(((uint16_t) value
) << 0);
531 case MENELAUS_INT_ACK2
:
532 s
->status
&= ~(((uint16_t) value
) << 8);
536 case MENELAUS_GPIO_CTRL
:
537 for (line
= 0; line
< 3; line
++) {
538 if (((s
->dir
^ value
) >> line
) & 1) {
539 qemu_set_irq(s
->out
[line
],
540 ((s
->outputs
& ~s
->dir
) >> line
) & 1);
543 s
->dir
= value
& 0x67;
545 case MENELAUS_GPIO_OUT
:
546 for (line
= 0; line
< 3; line
++) {
547 if ((((s
->outputs
^ value
) & ~s
->dir
) >> line
) & 1) {
548 qemu_set_irq(s
->out
[line
], (s
->outputs
>> line
) & 1);
551 s
->outputs
= value
& 0x07;
558 case MENELAUS_RTC_CTRL
:
559 if ((s
->rtc
.ctrl
^ value
) & 1) { /* RTC_EN */
561 menelaus_rtc_start(s
);
563 menelaus_rtc_stop(s
);
565 s
->rtc
.ctrl
= value
& 0x1f;
566 menelaus_alm_update(s
);
568 case MENELAUS_RTC_UPDATE
:
569 menelaus_rtc_update(s
);
570 memcpy(&tm
, &s
->rtc
.tm
, sizeof(tm
));
571 switch (value
& 0xf) {
575 tm
.tm_sec
= s
->rtc
.new.tm_sec
;
578 tm
.tm_min
= s
->rtc
.new.tm_min
;
581 if (s
->rtc
.new.tm_hour
> 23)
583 tm
.tm_hour
= s
->rtc
.new.tm_hour
;
586 if (s
->rtc
.new.tm_mday
< 1)
588 /* TODO check range */
589 tm
.tm_mday
= s
->rtc
.new.tm_mday
;
592 if (s
->rtc
.new.tm_mon
< 0 || s
->rtc
.new.tm_mon
> 11)
594 tm
.tm_mon
= s
->rtc
.new.tm_mon
;
597 tm
.tm_year
= s
->rtc
.new.tm_year
;
600 /* TODO set .tm_mday instead */
601 tm
.tm_wday
= s
->rtc
.new.tm_wday
;
604 if (s
->rtc
.new.tm_hour
> 23)
606 if (s
->rtc
.new.tm_mday
< 1)
608 if (s
->rtc
.new.tm_mon
< 0 || s
->rtc
.new.tm_mon
> 11)
610 tm
.tm_sec
= s
->rtc
.new.tm_sec
;
611 tm
.tm_min
= s
->rtc
.new.tm_min
;
612 tm
.tm_hour
= s
->rtc
.new.tm_hour
;
613 tm
.tm_mday
= s
->rtc
.new.tm_mday
;
614 tm
.tm_mon
= s
->rtc
.new.tm_mon
;
615 tm
.tm_year
= s
->rtc
.new.tm_year
;
619 fprintf(stderr
, "%s: bad RTC_UPDATE value %02x\n",
621 s
->status
|= 1 << 10; /* RTCERR */
624 s
->rtc
.sec_offset
= qemu_timedate_diff(&tm
);
626 case MENELAUS_RTC_SEC
:
627 s
->rtc
.tm
.tm_sec
= from_bcd(value
& 0x7f);
629 case MENELAUS_RTC_MIN
:
630 s
->rtc
.tm
.tm_min
= from_bcd(value
& 0x7f);
632 case MENELAUS_RTC_HR
:
633 s
->rtc
.tm
.tm_hour
= (s
->rtc
.ctrl
& (1 << 2)) ? /* MODE12_n24 */
634 MIN(from_bcd(value
& 0x3f), 12) + ((value
>> 7) ? 11 : -1) :
635 from_bcd(value
& 0x3f);
637 case MENELAUS_RTC_DAY
:
638 s
->rtc
.tm
.tm_mday
= from_bcd(value
);
640 case MENELAUS_RTC_MON
:
641 s
->rtc
.tm
.tm_mon
= MAX(1, from_bcd(value
)) - 1;
643 case MENELAUS_RTC_YR
:
644 s
->rtc
.tm
.tm_year
= 2000 + from_bcd(value
);
646 case MENELAUS_RTC_WKDAY
:
647 s
->rtc
.tm
.tm_mday
= from_bcd(value
);
649 case MENELAUS_RTC_AL_SEC
:
650 s
->rtc
.alm
.tm_sec
= from_bcd(value
& 0x7f);
651 menelaus_alm_update(s
);
653 case MENELAUS_RTC_AL_MIN
:
654 s
->rtc
.alm
.tm_min
= from_bcd(value
& 0x7f);
655 menelaus_alm_update(s
);
657 case MENELAUS_RTC_AL_HR
:
658 s
->rtc
.alm
.tm_hour
= (s
->rtc
.ctrl
& (1 << 2)) ? /* MODE12_n24 */
659 MIN(from_bcd(value
& 0x3f), 12) + ((value
>> 7) ? 11 : -1) :
660 from_bcd(value
& 0x3f);
661 menelaus_alm_update(s
);
663 case MENELAUS_RTC_AL_DAY
:
664 s
->rtc
.alm
.tm_mday
= from_bcd(value
);
665 menelaus_alm_update(s
);
667 case MENELAUS_RTC_AL_MON
:
668 s
->rtc
.alm
.tm_mon
= MAX(1, from_bcd(value
)) - 1;
669 menelaus_alm_update(s
);
671 case MENELAUS_RTC_AL_YR
:
672 s
->rtc
.alm
.tm_year
= 2000 + from_bcd(value
);
673 menelaus_alm_update(s
);
675 case MENELAUS_RTC_COMP_MSB
:
677 s
->rtc
.comp
|= value
<< 8;
679 case MENELAUS_RTC_COMP_LSB
:
680 s
->rtc
.comp
&= 0xff << 8;
681 s
->rtc
.comp
|= value
;
684 case MENELAUS_S1_PULL_EN
:
687 case MENELAUS_S1_PULL_DIR
:
688 s
->pull
[1] = value
& 0x1f;
690 case MENELAUS_S2_PULL_EN
:
693 case MENELAUS_S2_PULL_DIR
:
694 s
->pull
[3] = value
& 0x1f;
697 case MENELAUS_MCT_CTRL1
:
698 s
->mmc_ctrl
[0] = value
& 0x7f;
700 case MENELAUS_MCT_CTRL2
:
701 s
->mmc_ctrl
[1] = value
;
702 /* TODO update Card Detect interrupts */
704 case MENELAUS_MCT_CTRL3
:
705 s
->mmc_ctrl
[2] = value
& 0xf;
707 case MENELAUS_DEBOUNCE1
:
708 s
->mmc_debounce
= value
& 0x3f;
713 printf("%s: unknown register %02x\n", __func__
, addr
);
718 static int menelaus_event(I2CSlave
*i2c
, enum i2c_event event
)
720 MenelausState
*s
= TWL92230(i2c
);
722 if (event
== I2C_START_SEND
)
728 static int menelaus_tx(I2CSlave
*i2c
, uint8_t data
)
730 MenelausState
*s
= TWL92230(i2c
);
732 /* Interpret register address byte */
737 menelaus_write(s
, s
->reg
++, data
);
742 static uint8_t menelaus_rx(I2CSlave
*i2c
)
744 MenelausState
*s
= TWL92230(i2c
);
746 return menelaus_read(s
, s
->reg
++);
749 /* Save restore 32 bit int as uint16_t
750 This is a Big hack, but it is how the old state did it.
751 Or we broke compatibility in the state, or we can't use struct tm
754 static int get_int32_as_uint16(QEMUFile
*f
, void *pv
, size_t size
,
755 const VMStateField
*field
)
758 *v
= qemu_get_be16(f
);
762 static int put_int32_as_uint16(QEMUFile
*f
, void *pv
, size_t size
,
763 const VMStateField
*field
, QJSON
*vmdesc
)
766 qemu_put_be16(f
, *v
);
771 static const VMStateInfo vmstate_hack_int32_as_uint16
= {
772 .name
= "int32_as_uint16",
773 .get
= get_int32_as_uint16
,
774 .put
= put_int32_as_uint16
,
777 #define VMSTATE_UINT16_HACK(_f, _s) \
778 VMSTATE_SINGLE(_f, _s, 0, vmstate_hack_int32_as_uint16, int32_t)
781 static const VMStateDescription vmstate_menelaus_tm
= {
782 .name
= "menelaus_tm",
784 .minimum_version_id
= 0,
785 .fields
= (VMStateField
[]) {
786 VMSTATE_UINT16_HACK(tm_sec
, struct tm
),
787 VMSTATE_UINT16_HACK(tm_min
, struct tm
),
788 VMSTATE_UINT16_HACK(tm_hour
, struct tm
),
789 VMSTATE_UINT16_HACK(tm_mday
, struct tm
),
790 VMSTATE_UINT16_HACK(tm_min
, struct tm
),
791 VMSTATE_UINT16_HACK(tm_year
, struct tm
),
792 VMSTATE_END_OF_LIST()
796 static int menelaus_pre_save(void *opaque
)
798 MenelausState
*s
= opaque
;
799 /* Should be <= 1000 */
800 s
->rtc_next_vmstate
= s
->rtc
.next
- qemu_clock_get_ms(rtc_clock
);
805 static int menelaus_post_load(void *opaque
, int version_id
)
807 MenelausState
*s
= opaque
;
809 if (s
->rtc
.ctrl
& 1) /* RTC_EN */
810 menelaus_rtc_stop(s
);
812 s
->rtc
.next
= s
->rtc_next_vmstate
;
814 menelaus_alm_update(s
);
816 if (s
->rtc
.ctrl
& 1) /* RTC_EN */
817 menelaus_rtc_start(s
);
821 static const VMStateDescription vmstate_menelaus
= {
824 .minimum_version_id
= 0,
825 .pre_save
= menelaus_pre_save
,
826 .post_load
= menelaus_post_load
,
827 .fields
= (VMStateField
[]) {
828 VMSTATE_INT32(firstbyte
, MenelausState
),
829 VMSTATE_UINT8(reg
, MenelausState
),
830 VMSTATE_UINT8_ARRAY(vcore
, MenelausState
, 5),
831 VMSTATE_UINT8_ARRAY(dcdc
, MenelausState
, 3),
832 VMSTATE_UINT8_ARRAY(ldo
, MenelausState
, 8),
833 VMSTATE_UINT8_ARRAY(sleep
, MenelausState
, 2),
834 VMSTATE_UINT8(osc
, MenelausState
),
835 VMSTATE_UINT8(detect
, MenelausState
),
836 VMSTATE_UINT16(mask
, MenelausState
),
837 VMSTATE_UINT16(status
, MenelausState
),
838 VMSTATE_UINT8(dir
, MenelausState
),
839 VMSTATE_UINT8(inputs
, MenelausState
),
840 VMSTATE_UINT8(outputs
, MenelausState
),
841 VMSTATE_UINT8(bbsms
, MenelausState
),
842 VMSTATE_UINT8_ARRAY(pull
, MenelausState
, 4),
843 VMSTATE_UINT8_ARRAY(mmc_ctrl
, MenelausState
, 3),
844 VMSTATE_UINT8(mmc_debounce
, MenelausState
),
845 VMSTATE_UINT8(rtc
.ctrl
, MenelausState
),
846 VMSTATE_UINT16(rtc
.comp
, MenelausState
),
847 VMSTATE_UINT16(rtc_next_vmstate
, MenelausState
),
848 VMSTATE_STRUCT(rtc
.new, MenelausState
, 0, vmstate_menelaus_tm
,
850 VMSTATE_STRUCT(rtc
.alm
, MenelausState
, 0, vmstate_menelaus_tm
,
852 VMSTATE_UINT8(pwrbtn_state
, MenelausState
),
853 VMSTATE_I2C_SLAVE(parent_obj
, MenelausState
),
854 VMSTATE_END_OF_LIST()
858 static void twl92230_realize(DeviceState
*dev
, Error
**errp
)
860 MenelausState
*s
= TWL92230(dev
);
862 s
->rtc
.hz_tm
= timer_new_ms(rtc_clock
, menelaus_rtc_hz
, s
);
863 /* Three output pins plus one interrupt pin. */
864 qdev_init_gpio_out(dev
, s
->out
, 4);
866 /* Three input pins plus one power-button pin. */
867 qdev_init_gpio_in(dev
, menelaus_gpio_set
, 4);
869 menelaus_reset(I2C_SLAVE(dev
));
872 static void twl92230_class_init(ObjectClass
*klass
, void *data
)
874 DeviceClass
*dc
= DEVICE_CLASS(klass
);
875 I2CSlaveClass
*sc
= I2C_SLAVE_CLASS(klass
);
877 dc
->realize
= twl92230_realize
;
878 sc
->event
= menelaus_event
;
879 sc
->recv
= menelaus_rx
;
880 sc
->send
= menelaus_tx
;
881 dc
->vmsd
= &vmstate_menelaus
;
884 static const TypeInfo twl92230_info
= {
885 .name
= TYPE_TWL92230
,
886 .parent
= TYPE_I2C_SLAVE
,
887 .instance_size
= sizeof(MenelausState
),
888 .class_init
= twl92230_class_init
,
891 static void twl92230_register_types(void)
893 type_register_static(&twl92230_info
);
896 type_init(twl92230_register_types
)