summaryrefslogtreecommitdiffstats
path: root/firmware/drivers/pcf50606.c
blob: 51cd812346abb464dc06851640cb426d631edfe6 (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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2005 by Linus Nielsen Feltzing
 *
 * All files in this archive are subject to the GNU General Public License.
 * See the file COPYING in the source tree root for full license agreement.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
#include "config.h"
#include "cpu.h"
#include <stdbool.h>
#include "kernel.h"
#include "system.h"
#include "hwcompat.h"
#include "logf.h"
#include "string.h"

/* TODO: merge all bit-banged I2C into a generic I2C driver */


#define SDA_LO     and_l(~0x00002000, &GPIO1_OUT)
#define SDA_HI      or_l( 0x00002000, &GPIO1_OUT)
#define SDA_INPUT  and_l(~0x00002000, &GPIO1_ENABLE)
#define SDA_OUTPUT  or_l( 0x00002000, &GPIO1_ENABLE)
#define SDA             ( 0x00002000 & GPIO1_READ)

/* SCL is GPIO, 3 */
#define SCL_INPUT  and_l(~0x00001000, &GPIO_ENABLE)
#define SCL_OUTPUT  or_l( 0x00001000, &GPIO_ENABLE)
#define SCL_LO     and_l(~0x00001000, &GPIO_OUT)
#define SCL_HI      SCL_INPUT;while(!SCL){};or_l(0x1000, &GPIO_OUT);SCL_OUTPUT
#define SCL             ( 0x00001000 & GPIO_READ)

/* delay loop to achieve 400kHz at 120MHz CPU frequency */
#define DELAY    do { int _x; for(_x=0;_x<22;_x++);} while(0)

static void pcf50606_i2c_start(void)
{
    SDA_OUTPUT;
    SCL_OUTPUT;
    SDA_HI;
    SCL_HI;
    DELAY;
    SDA_LO;
    DELAY;
    SCL_LO;
}

static void pcf50606_i2c_stop(void)
{
   SDA_LO;
   SCL_HI;
   DELAY;
   SDA_HI;
}


static void pcf50606_i2c_ack(bool ack)
{
    SCL_LO;      /* Set the clock low */
    if(ack)
        SDA_LO;
    else
        SDA_HI;

    SCL_HI;

    DELAY;
    SCL_OUTPUT;
    SCL_LO;
}

static int pcf50606_i2c_getack(void)
{
    int ret = 1;

    SDA_INPUT;   /* And set to input */
    DELAY;
    SCL_HI;

    if (SDA)
        /* ack failed */
        ret = 0;
    
    SCL_OUTPUT;
    SCL_LO;
    SDA_HI;
    SDA_OUTPUT;
    DELAY;

    return ret;
}

static void pcf50606_i2c_outb(unsigned char byte)
{
   int i;

   /* clock out each bit, MSB first */
   for ( i=0x80; i; i>>=1 ) {
      if ( i & byte )
      {
         SDA_HI;
      }
      else
      {
         SDA_LO;
      }
      DELAY;
      SCL_HI;
      DELAY;
      SCL_LO;
   }

   SDA_HI;
}

static unsigned char pcf50606_i2c_inb(bool ack)
{
   int i;
   unsigned char byte = 0;

   /* clock in each bit, MSB first */
   for ( i=0x80; i; i>>=1 ) {
       SDA_INPUT;   /* And set to input */
       SCL_HI;
       DELAY;
       if ( SDA )
           byte |= i;
       SCL_LO;
       DELAY;
       SDA_OUTPUT;
   }

   pcf50606_i2c_ack(ack);
   
   return byte;
}

int pcf50606_i2c_write(int address, const unsigned char* buf, int count)
{
    int i,x=0;

    pcf50606_i2c_start();
    pcf50606_i2c_outb(address & 0xfe);
    if (pcf50606_i2c_getack())
    {
        for (i=0; i<count; i++)
        {
            pcf50606_i2c_outb(buf[i]);
            if (!pcf50606_i2c_getack())
            {
                x=-2;
                break;
            }
        }
    }
    else
    {
        logf("pcf50606_i2c_write() - no ack\n");
        x=-1;
    }
    return x;
}

int pcf50606_read_multiple(int address, unsigned char* buf, int count)
{
    int i=0;
    int ret = 0;
    unsigned char obuf[1];

    obuf[0] = address;

    /* send read command */
    if (pcf50606_i2c_write(0x10, obuf, 1) >= 0)
    {
        pcf50606_i2c_start();
        pcf50606_i2c_outb(0x11);
        if (pcf50606_i2c_getack())
        {
            for(i = 0;i < count-1;i++)
                buf[i] = pcf50606_i2c_inb(true);
            
            buf[i] = pcf50606_i2c_inb(false);
        }
        else
        {
            ret = -1;
        }
    }

    pcf50606_i2c_stop();

    return ret;
}

int pcf50606_read(int address)
{
    int ret;
    unsigned char c;

    ret = pcf50606_read_multiple(address, &c, 1);
    if(ret >= 0)
        return c;
    else
        return ret;
}

int pcf50606_write_multiple(int address, const unsigned char* buf, int count)
{
    unsigned char obuf[1];
    int i;
    int ret = 0;

    obuf[0] = address;
    
    /* send write command */
    if (pcf50606_i2c_write(0x10, obuf, 1) >= 0)
    {
        for (i=0; i<count; i++)
        {
            pcf50606_i2c_outb(buf[i]);
            if (!pcf50606_i2c_getack())
            {
                ret = -2;
                break;
            }
        }
    }
    else
    {
        ret = -1;
    }

    pcf50606_i2c_stop();
    return ret;
}

int pcf50606_write(int address, unsigned char val)
{
    return pcf50606_write_multiple(address, &val, 1);
}


/* These voltages were determined by measuring the output of the PCF50606
   on a running H300, and verified by disassembling the original firmware */
static void set_voltages(void)
{
    static const unsigned char buf[5] =
        {
            0xf4,   /* IOREGC = 2.9V, ON in all states */
            0xef,   /* D1REGC = 2.4V, ON in all states */
            0x18,   /* D2REGC = 3.3V, OFF in all states */
            0xf0,   /* D3REGC = 2.5V, ON in all states */
            0xef,   /* LPREGC1 = 2.4V, ON in all states */
        };
    
    pcf50606_write_multiple(0x23, buf, 5);
}

void pcf50606_init(void)
{
    /* Bit banged I2C */
    or_l(0x00002000, &GPIO1_OUT);
    or_l(0x00001000, &GPIO_OUT);
    or_l(0x00002000, &GPIO1_ENABLE);
    or_l(0x00001000, &GPIO_ENABLE);
    or_l(0x00002000, &GPIO1_FUNCTION);
    or_l(0x00001000, &GPIO_FUNCTION);

    set_voltages();

    pcf50606_write(0x08, 0x60); /* Wake on USB and charger insertion */
    pcf50606_write(0x09, 0x05); /* USB and ON key debounce: 14ms */
    pcf50606_write(0x29, 0x1C); /* Disable the unused MBC module */
    
    pcf50606_write(0x35, 0x13); /* Backlight PWM = 512Hz 50/50 */
    pcf50606_write(0x3a, 0x3b); /* PWM output on GPOOD1 */
}