summaryrefslogtreecommitdiffstats
path: root/firmware/target/arm/s5l8702/ipod6g/pmu-6g.c
blob: 10b16d6c4de612efdd9243468ab0d5076d62435b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id: pmu-nano2g.c 27752 2010-08-08 10:49:32Z bertrik $
 *
 * Copyright © 2008 Rafaël Carré
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

#include "config.h"
#include "kernel.h"
#include "thread.h"

#include "pmu-target.h"
#include "adc-target.h"
#include "i2c-s5l8702.h"
#include "gpio-s5l8702.h"


int pmu_read_multiple(int address, int count, unsigned char* buffer)
{
    return i2c_read(0, 0xe6, address, count, buffer);
}

int pmu_write_multiple(int address, int count, unsigned char* buffer)
{
    return i2c_write(0, 0xe6, address, count, buffer);
}

unsigned char pmu_read(int address)
{
    unsigned char tmp;

    pmu_read_multiple(address, 1, &tmp);

    return tmp;
}

int pmu_write(int address, unsigned char val)
{
    return pmu_write_multiple(address, 1, &val);
}

void pmu_ldo_on_in_standby(unsigned int ldo, int onoff)
{
    if (ldo < 4)
    {
        unsigned char newval = pmu_read(0x3B) & ~(1 << (2 * ldo));
        if (onoff) newval |= 1 << (2 * ldo);
        pmu_write(0x3B, newval);
    }
    else if (ldo < 8)
    {
        unsigned char newval = pmu_read(0x3C) & ~(1 << (2 * (ldo - 4)));
        if (onoff) newval |= 1 << (2 * (ldo - 4));
        pmu_write(0x3C, newval);
    }
}

void pmu_ldo_set_voltage(unsigned int ldo, unsigned char voltage)
{
    if (ldo > 6) return;
    pmu_write(0x2d + (ldo << 1), voltage);
}

void pmu_hdd_power(bool on)
{
    pmu_write(0x1b, on ? 1 : 0);
}

void pmu_ldo_power_on(unsigned int ldo)
{
    if (ldo > 6) return;
    pmu_write(0x2e + (ldo << 1), 1);
}

void pmu_ldo_power_off(unsigned int ldo)
{
    if (ldo > 6) return;
    pmu_write(0x2e + (ldo << 1), 0);
}

void pmu_set_wake_condition(unsigned char condition)
{
    pmu_write(0xd, condition);
}

void pmu_enter_standby(void)
{
    pmu_write(0xc, 1);
}

#ifdef HAVE_ADJUSTABLE_CPU_FREQ
void pmu_set_cpu_voltage(bool high)
{
    pmu_write(PCF5063X_REG_DOWN1OUT,
            high ? 0x13 /*1100mV*/ : 0xf /*1000mV*/);
}
#endif

#if (CONFIG_RTC == RTC_NANO2G)
void pmu_read_rtc(unsigned char* buffer)
{
    pmu_read_multiple(0x59, 7, buffer);
}

void pmu_write_rtc(unsigned char* buffer)
{
    pmu_write_multiple(0x59, 7, buffer);
}
#endif

/*
 * ADC
 */
#define ADC_FULL_SCALE          2000
#define ADC_FULL_SCALE_VISA     2400
#define ADC_SUBTR_OFFSET        2250

static struct mutex pmu_adc_mutex;

/* converts raw 8/10-bit value to millivolts */
unsigned short pmu_adc_raw2mv(
        const struct pmu_adc_channel *ch, unsigned short raw)
{
    int full_scale = ADC_FULL_SCALE;
    int offset = 0;

    switch (ch->adcc1 & PCF5063X_ADCC1_ADCMUX_MASK)
    {
        case PCF5063X_ADCC1_MUX_BATSNS_RES:
        case PCF5063X_ADCC1_MUX_ADCIN2_RES:
            full_scale *= ((ch->adcc1 & PCF5063X_ADCC3_RES_DIV_MASK) ==
                                        PCF5063X_ADCC3_RES_DIV_TWO) ? 2 : 3;
            break;
        case PCF5063X_ADCC1_MUX_BATSNS_SUBTR:
        case PCF5063X_ADCC1_MUX_ADCIN2_SUBTR:
            offset = ADC_SUBTR_OFFSET;
            break;
        case PCF5063X_ADCC1_MUX_BATTEMP:
            if (ch->adcc2 & PCF5063X_ADCC2_RATIO_BATTEMP)
                full_scale = ADC_FULL_SCALE_VISA;
            break;
        case PCF5063X_ADCC1_MUX_ADCIN1:
            if (ch->adcc2 & PCF5063X_ADCC2_RATIO_ADCIN1)
                full_scale = ADC_FULL_SCALE_VISA;
            break;
    }

    int nrb = ((ch->adcc1 & PCF5063X_ADCC1_RES_MASK) ==
                                PCF5063X_ADCC1_RES_8BIT) ? 8 : 10;
    return (raw * full_scale / ((1<<nrb)-1)) + offset;
}

/* returns raw value, 8 or 10-bit resolution */
unsigned short pmu_read_adc(const struct pmu_adc_channel *ch)
{
    mutex_lock(&pmu_adc_mutex);

    pmu_write(PCF5063X_REG_ADCC3, ch->adcc3);
    if (ch->bias_dly)
        sleep(ch->bias_dly);
    uint8_t buf[2] = { ch->adcc2, ch->adcc1 | PCF5063X_ADCC1_ADCSTART };
    pmu_write_multiple(PCF5063X_REG_ADCC2, 2, buf);

    int adcs3 = 0;
    while (!(adcs3 & PCF5063X_ADCS3_ADCRDY))
    {
        yield();
        adcs3 = pmu_read(PCF5063X_REG_ADCS3);
    }

    int raw = pmu_read(PCF5063X_REG_ADCS1);
    if ((ch->adcc1 & PCF5063X_ADCC1_RES_MASK) == PCF5063X_ADCC1_RES_10BIT)
        raw = (raw << 2) | (adcs3 & PCF5063X_ADCS3_ADCDAT1L_MASK);

    mutex_unlock(&pmu_adc_mutex);
    return raw;
}

/*
 * eINT
 */
#define Q_EINT  0

static char pmu_thread_stack[DEFAULT_STACK_SIZE/2];
static struct event_queue pmu_queue;
static unsigned char ints_msk[6];

static void pmu_eint_isr(struct eint_handler*);

static struct eint_handler pmu_eint =
{
    .gpio_n = GPIO_EINT_PMU,
    .type   = EIC_INTTYPE_LEVEL,
    .level  = EIC_INTLEVEL_LOW,
    .isr    = pmu_eint_isr,
};

static int pmu_input_holdswitch;

int pmu_holdswitch_locked(void)
{
   return pmu_input_holdswitch;
}

#ifdef IPOD_ACCESSORY_PROTOCOL
static int pmu_input_accessory;

int pmu_accessory_present(void)
{
   return pmu_input_accessory;
}
#endif

#if CONFIG_CHARGING
static int pmu_input_firewire;

int pmu_firewire_present(void)
{
   return pmu_input_firewire;
}

static void pmu_read_inputs_mbcs(void)
{
    pmu_input_firewire = !!(pmu_read(PCF5063X_REG_MBCS1)
                                & PCF5063X_MBCS1_ADAPTPRES);
}
#endif

static void pmu_read_inputs_gpio(void)
{
    pmu_input_holdswitch = !(pmu_read(PCF50635_REG_GPIOSTAT)
                                & PCF50635_GPIOSTAT_GPIO2);
}

static void pmu_read_inputs_ooc(void)
{
    unsigned char oocstat = pmu_read(PCF5063X_REG_OOCSTAT);
    if (oocstat & PCF5063X_OOCSTAT_EXTON2)
        usb_insert_int();
    else
        usb_remove_int();
#ifdef IPOD_ACCESSORY_PROTOCOL
    pmu_input_accessory = !(oocstat & PCF5063X_OOCSTAT_EXTON3);
#endif
}

static void pmu_eint_isr(struct eint_handler *h)
{
     eint_unregister(h);
     queue_post(&pmu_queue, Q_EINT, 0);
}

static void NORETURN_ATTR pmu_thread(void)
{
    struct queue_event ev;
    unsigned char ints[6];

    while (true)
    {
        queue_wait_w_tmo(&pmu_queue, &ev, TIMEOUT_BLOCK);
        switch (ev.id)
        {
            case Q_EINT:
                /* read (clear) PMU interrupts, this will also
                   raise the PMU IRQ pin */
                pmu_read_multiple(PCF5063X_REG_INT1, 2, ints);
                ints[5] = pmu_read(PCF50635_REG_INT6);

#if CONFIG_CHARGING
                if (ints[0] & ~ints_msk[0]) pmu_read_inputs_mbcs();
#endif
                if (ints[1] & ~ints_msk[1]) pmu_read_inputs_ooc();
                if (ints[5] & ~ints_msk[5]) pmu_read_inputs_gpio();

                eint_register(&pmu_eint);
                break;

            case SYS_TIMEOUT:
                break;
        }
    }
}

/* main init */
void pmu_init(void)
{
    mutex_init(&pmu_adc_mutex);
    queue_init(&pmu_queue, false);

    create_thread(pmu_thread,
            pmu_thread_stack, sizeof(pmu_thread_stack), 0,
            "PMU" IF_PRIO(, PRIORITY_SYSTEM) IF_COP(, CPU));

    /* configure PMU interrutps */
    for (int i = 0; i < 6; i++)
        ints_msk[i] = 0xff;

#if CONFIG_CHARGING
    ints_msk[0] &= ~PCF5063X_INT1_ADPINS &    /* FireWire */
                   ~PCF5063X_INT1_ADPREM;
#endif
    ints_msk[1] &= ~PCF5063X_INT2_EXTON2R &   /* USB */
                   ~PCF5063X_INT2_EXTON2F;
#ifdef IPOD_ACCESSORY_PROTOCOL
    ints_msk[1] &= ~PCF5063X_INT2_EXTON3R &   /* Accessory */
                   ~PCF5063X_INT2_EXTON3F;
#endif
    ints_msk[5] &= ~PCF50635_INT6_GPIO2;      /* Holdswitch */

    pmu_write_multiple(PCF5063X_REG_INT1M, 5, ints_msk);
    pmu_write(PCF50635_REG_INT6M, ints_msk[5]);

    /* clear all */
    unsigned char ints[5];
    pmu_read_multiple(PCF5063X_REG_INT1, 5, ints);
    pmu_read(PCF50635_REG_INT6);

    /* get initial values */
#if CONFIG_CHARGING
    pmu_read_inputs_mbcs();
#endif
    pmu_read_inputs_ooc();
    pmu_read_inputs_gpio();

    eint_register(&pmu_eint);
}

/*
 * preinit
 */
int pmu_rd_multiple(int address, int count, unsigned char* buffer)
{
    return i2c_rd(0, 0xe6, address, count, buffer);
}

int pmu_wr_multiple(int address, int count, unsigned char* buffer)
{
    return i2c_wr(0, 0xe6, address, count, buffer);
}

unsigned char pmu_rd(int address)
{
    unsigned char val;
    pmu_rd_multiple(address, 1, &val);
    return val;
}

int pmu_wr(int address, unsigned char val)
{
    return pmu_wr_multiple(address, 1, &val);
}

void pmu_preinit(void)
{
    static const char init_data[] =
    {
        /* reset OOC shutdown register */
        PCF5063X_REG_OOCSHDWN,  0x0,

        /* LDO_UNK1: 3000 mV, enabled */
        PCF5063X_REG_LDO1OUT,   0x15,
        PCF5063X_REG_LDO1ENA,   0x1,

        /* LDO_UNK2: 3000 mV, enabled */
        PCF5063X_REG_LDO2OUT,   0x15,
        PCF5063X_REG_LDO2ENA,   0x1,

        /* LDO_LCD: 3000 mV, enabled */
        PCF5063X_REG_LDO3OUT,   0x15,
        PCF5063X_REG_LDO3ENA,   0x1,

        /* LDO_CODEC: 1800 mV, enabled */
        PCF5063X_REG_LDO4OUT,   0x9,
        PCF5063X_REG_LDO4ENA,   0x1,

        /* LDO_UNK5: 3000 mV, disabled */
        PCF5063X_REG_LDO5OUT,   0x15,
        PCF5063X_REG_LDO5ENA,   0x0,

        /* LDO_CWHEEL: 3000 mV, ON when GPIO2 High */
        PCF5063X_REG_LDO6OUT,   0x15,
        PCF5063X_REG_LDO6ENA,   0x4,

        /* LDO_ACCY: 3300 mV, disabled */
        PCF5063X_REG_HCLDOOUT,  0x18,
        PCF5063X_REG_HCLDOENA,  0x0,

        /* LDO_CWHEEL is ON in STANDBY state,
           LDO_CWHEEL and MEMLDO are ON in UNKNOWN state (TBC) */
        PCF5063X_REG_STBYCTL1,  0x0,
        PCF5063X_REG_STBYCTL2,  0x8c,

        /* GPIO1,2 = input, GPIO3 = output High (NoPower default) */
        PCF5063X_REG_GPIOCTL,   0x3,
        PCF5063X_REG_GPIO1CFG,  0x0,
        PCF5063X_REG_GPIO2CFG,  0x0,
        PCF5063X_REG_GPIO3CFG,  0x7,

        /* DOWN2 converter (SDRAM): 1800 mV, enabled,
           startup current limit = 15mA*0x10 (TBC) */
        PCF5063X_REG_DOWN2OUT,  0x2f,
        PCF5063X_REG_DOWN2ENA,  0x1,
        PCF5063X_REG_DOWN2CTL,  0x0,
        PCF5063X_REG_DOWN2MXC,  0x10,

        /* MEMLDO: 1800 mV, enabled */
        PCF5063X_REG_MEMLDOOUT, 0x9,
        PCF5063X_REG_MEMLDOENA, 0x1,

        /* AUTOLDO (HDD): 3400 mV, disabled,
           limit = 1000 mA (40mA*0x19), limit always active */
        PCF5063X_REG_AUTOOUT,   0x6f,
#ifdef BOOTLOADER
        PCF5063X_REG_AUTOENA,   0x0,
#endif
        PCF5063X_REG_AUTOCTL,   0x0,
        PCF5063X_REG_AUTOMXC,   0x59,

        /* Vsysok = 3100 mV */
        PCF5063X_REG_SVMCTL,    0x8,

        /* Reserved */
        0x58,                   0x0,

        /* Mask all PMU interrupts */
        PCF5063X_REG_INT1M,     0xff,
        PCF5063X_REG_INT2M,     0xff,
        PCF5063X_REG_INT3M,     0xff,
        PCF5063X_REG_INT4M,     0xff,
        PCF5063X_REG_INT5M,     0xff,
        PCF50635_REG_INT6M,     0xff,

        /* Wakeup on rising edge for EXTON1 and EXTON2,
           wakeup on falling edge for EXTON3 and !ONKEY,
           wakeup on RTC alarm, wakeup on adapter insert,
           Vbat status has no effect in state machine */
        PCF5063X_REG_OOCWAKE,   0xdf,
        PCF5063X_REG_OOCTIM1,   0xaa,
        PCF5063X_REG_OOCTIM2,   0x4a,
        PCF5063X_REG_OOCMODE,   0x5,
        PCF5063X_REG_OOCCTL,    0x27,

        /* GPO selection = LED external NFET drive signal */
        PCF5063X_REG_GPOCFG,    0x1,
        /* LED converter OFF, overvoltage protection enabled,
           OCP limit is 500 mA, led_dimstep = 16*0x6/32768 */
#ifdef BOOTLOADER
        PCF5063X_REG_LEDENA,    0x0,
#endif
        PCF5063X_REG_LEDCTL,    0x5,
        PCF5063X_REG_LEDDIM,    0x6,

        /* end marker */
        0
    };

    const char* ptr;
    for (ptr = init_data; *ptr != 0; ptr += 2)
        pmu_wr(ptr[0], ptr[1]);

    /* clear PMU interrupts */
    unsigned char rd_buf[5];
    pmu_rd_multiple(PCF5063X_REG_INT1, 5, rd_buf);
    pmu_rd(PCF50635_REG_INT6);
}

#ifdef BOOTLOADER
bool pmu_is_hibernated(void)
{
    /* OF sets GPIO3 to low when SDRAM is hibernated */
    bool gpio3out, coldboot;
    /* get (previously) configured output selection for GPIO3 */
    gpio3out = (pmu_rd(PCF5063X_REG_GPIO3CFG) & 7);
    /* coldboot: when set, device has been in NoPower state */
    coldboot = (pmu_rd(PCF5063X_REG_OOCSHDWN) & PCF5063X_OOCSHDWN_COLDBOOT);
    return !coldboot && !gpio3out;
}
#endif