1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2006 by Linus Nielsen Feltzing
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
27 /* Define the approprate bits for SDA and SCL being the only difference in
28 config between each player. */
29 #if defined(IRIVER_H300_SERIES)
30 #define SDA_BITNUM 13 /* LRCK3/GPIO45 */
31 #define SCL_BITNUM 12 /* SWE/GPIO12 */
32 #elif defined(IAUDIO_X5) || defined(IAUDIO_M5)
33 #define SDA_BITNUM 12 /* SDA1/RXD1/GPIO44 */
34 #define SCL_BITNUM 10 /* SCL1/TXD1/GPIO10 */
38 #define SDA_GPIO_READ GPIO1_READ /* MBAR2 + 0x0b0 */
39 #define SDA_GPIO_OUT GPIO1_OUT /* MBAR2 + 0x0b4 */
40 #define SDA_GPIO_ENABLE GPIO1_ENABLE /* MBAR2 + 0x0b8 */
41 #define SDA_GPIO_FUNCTION GPIO1_FUNCTION /* MBAR2 + 0x0bc */
44 #define SCL_GPIO_READ GPIO_READ /* MBAR2 + 0x000 */
45 #define SCL_GPIO_OUT GPIO_OUT /* MBAR2 + 0x004 */
46 #define SCL_GPIO_ENABLE GPIO_ENABLE /* MBAR2 + 0x008 */
47 #define SCL_GPIO_FUNCTION GPIO_FUNCTION /* MBAR2 + 0x00c */
49 #define PCF50606_ADDR 0x10
50 #define SCL_BIT (1ul << SCL_BITNUM)
51 #define SDA_BIT (1ul << SDA_BITNUM)
53 #define SDA ( SDA_BIT & SDA_GPIO_READ)
54 #define SDA_LO_OUT or_l( SDA_BIT, &SDA_GPIO_ENABLE)
55 #define SDA_HI_IN and_l(~SDA_BIT, &SDA_GPIO_ENABLE)
57 #define SCL ( SCL_BIT & SCL_GPIO_READ)
58 #define SCL_LO_OUT or_l( SCL_BIT, &SCL_GPIO_ENABLE)
59 #define SCL_HI_IN and_l(~SCL_BIT, &SCL_GPIO_ENABLE); while(!SCL);
63 "move.l %[dly],%%d0 \n" \
67 : : [dly]"d"(i2c_delay) : "d0" );
69 void pcf50606_i2c_init(void)
72 or_l(SDA_BIT
, &SDA_GPIO_FUNCTION
);
73 or_l(SCL_BIT
, &SCL_GPIO_FUNCTION
);
74 and_l(~SDA_BIT
, &SDA_GPIO_OUT
);
75 and_l(~SCL_BIT
, &SCL_GPIO_OUT
);
76 and_l(~SDA_BIT
, &SDA_GPIO_ENABLE
);
77 and_l(~SCL_BIT
, &SCL_GPIO_ENABLE
);
80 static int i2c_delay IDATA_ATTR
= 44;
82 void pcf50606_i2c_recalc_delay(int cpu_clock
)
84 i2c_delay
= MAX(cpu_clock
/ (400000*2*3) - 7, 1);
87 inline void pcf50606_i2c_start(void)
91 "not.l %[sdab] \n" /* SDA_HI_IN */
92 "and.l %[sdab],(8,%[sdard]) \n"
95 "not.l %[sclb] \n" /* SCL_HI_IN */
96 "and.l %[sclb],(8,%[sclrd]) \n"
99 "move.l (%[sclrd]),%%d0 \n"
100 "btst.l %[sclbnum], %%d0 \n"
103 "move.l %[dly],%%d0 \n" /* DELAY */
108 "or.l %[sdab],(8,%[sdard]) \n" /* SDA_LO_OUT */
110 "move.l %[dly],%%d0 \n" /* DELAY */
115 "or.l %[sclb],(8,%[sclrd]) \n" /* SCL_LO_OUT */
118 [sclrd
] "a"(&SCL_GPIO_READ
),
120 [sclbnum
] "i"(SCL_BITNUM
),
121 [sdard
] "a"(&SDA_GPIO_READ
),
137 inline void pcf50606_i2c_stop(void)
141 "or.l %[sdab],(8,%[sdard]) \n" /* SDA_LO_OUT */
143 "not.l %[sclb] \n" /* SCL_HI_IN */
144 "and.l %[sclb],(8,%[sclrd]) \n"
147 "move.l (%[sclrd]),%%d0 \n"
148 "btst.l %[sclbnum],%%d0 \n"
151 "move.l %[dly],%%d0 \n" /* DELAY */
156 "not.l %[sdab] \n" /* SDA_HI_IN */
157 "and.l %[sdab],(8,%[sdard]) \n"
161 [sclrd
] "a"(&SCL_GPIO_READ
),
163 [sclbnum
] "i"(SCL_BITNUM
),
164 [sdard
] "a"(&SDA_GPIO_READ
),
178 inline void pcf50606_i2c_ack(bool ack
)
182 "tst.b %[ack] \n" /* if (!ack) */
185 "not.l %[sdab] \n" /* SDA_HI_IN */
186 "and.l %[sdab],(8,%[sdard]) \n"
188 ".word 0x51fb \n" /* trapf.l : else */
190 "or.l %[sdab],(8,%[sdard]) \n" /* SDA_LO_OUT */
192 "not.l %[sclb] \n" /* SCL_HI_IN */
193 "and.l %[sclb],(8,%[sclrd]) \n"
196 "move.l (%[sclrd]),%%d0 \n"
197 "btst.l %[sclbnum],%%d0 \n"
200 "move.l %[dly],%%d0 \n" /* DELAY */
205 "or.l %[sclb],(8,%[sclrd]) \n" /* SCL_LO_OUT */
208 [sclrd
] "a"(&SCL_GPIO_READ
),
210 [sclbnum
] "i"(SCL_BITNUM
),
211 [sdard
] "a"(&SDA_GPIO_READ
),
213 [dly
] "d"(i2c_delay
),
231 inline bool pcf50606_i2c_getack(void)
237 "not.l %[sdab] \n" /* SDA_HI_IN */
238 "and.l %[sdab],(8,%[sdard]) \n"
241 "move.l %[dly],%%d0 \n" /* DELAY */
246 "not.l %[sclb] \n" /* SCL_HI_IN */
247 "and.l %[sclb],(8,%[sclrd]) \n"
250 "move.l (%[sclrd]),%%d0 \n"
251 "btst.l %[sclbnum],%%d0 \n"
254 "move.l (%[sdard]),%%d0 \n" /* ret = !SDA */
255 "btst.l %[sdabnum],%%d0 \n"
258 "or.l %[sclb],(8,%[sclrd]) \n" /* SCL_LO_OUT */
260 "move.l %[dly],%%d0 \n" /* DELAY */
267 [sclrd
] "a"(&SCL_GPIO_READ
),
269 [sclbnum
] "i"(SCL_BITNUM
),
270 [sdard
] "a"(&SDA_GPIO_READ
),
272 [sdabnum
] "i"(SDA_BITNUM
),
290 void pcf50606_i2c_outb(unsigned char byte
)
294 "moveq.l #24,%%d0 \n" /* byte <<= 24 */
295 "lsl.l %%d0,%[byte] \n"
296 "moveq.l #8,%%d1 \n" /* i = 8 */
299 "lsl.l #1,%[byte] \n" /* if ((byte <<= 1) carry) */
302 "not.l %[sdab] \n" /* SDA_HI_IN */
303 "and.l %[sdab],(8,%[sdard]) \n"
305 ".word 0x51fb \n" /* trapf.l; else */
307 "or.l %[sdab],(8,%[sdard]) \n" /* SDA_LO_OUT */
309 "move.l %[dly],%%d0 \n" /* DELAY */
314 "not.l %[sclb] \n" /* SCL_HI_IN */
315 "and.l %[sclb],(8,%[sclrd]) \n"
318 "move.l (%[sclrd]),%%d0 \n"
319 "btst.l %[sclbnum],%%d0 \n"
322 "move.l %[dly],%%d0 \n" /* DELAY */
327 "or.l %[sclb],(8,%[sclrd]) \n" /* SCL_LO_OUT */
329 "subq.l #1,%%d1 \n" /* i-- */
330 "bne.s 2b \n" /* while (i != 0) */
334 [sclrd
] "a"(&SCL_GPIO_READ
),
336 [sclbnum
] "i"(SCL_BITNUM
),
337 [sdard
] "a"(&SDA_GPIO_READ
),
346 /* clock out each bit, MSB first */
347 for ( i
=0x80; i
; i
>>=1 )
361 unsigned char pcf50606_i2c_inb(bool ack
)
363 unsigned char byte
= 0;
367 "not.l %[sdab] \n" /* SDA_HI_IN */
368 "and.l %[sdab],(8,%[sdard]) \n"
371 "moveq.l #8,%%d1 \n" /* i = 8 */
372 "clr.l %[byte] \n" /* byte = 0 */
375 "not.l %[sclb] \n" /* SCL_HI_IN */
376 "and.l %[sclb],(8,%[sclrd]) \n"
379 "move.l (%[sclrd]),%%d0 \n"
380 "btst.l %[sclbnum],%%d0 \n"
383 "move.l %[dly],%%d0 \n" /* DELAY */
388 "lsl.l #1,%[byte] \n" /* byte <<= 1 */
389 "move.l (%[sdard]),%%d0 \n" /* if (SDA) */
390 "btst.l %[sdabnum],%%d0 \n"
392 "addq.l #1,%[byte] \n" /* byte++ */
395 "or.l %[sclb],(8,%[sclrd]) \n" /* SCL_LO_OUT */
397 "move.l %[dly],%%d0 \n" /* DELAY */
402 "subq.l #1,%%d1 \n" /* i-- */
403 "bne.s 2b \n" /* while (i != 0) */
407 [sclrd
] "a"(&SCL_GPIO_READ
),
409 [sclbnum
] "i"(SCL_BITNUM
),
410 [sdard
] "a"(&SDA_GPIO_READ
),
412 [sdabnum
] "i"(SDA_BITNUM
),
420 /* clock in each bit, MSB first */
422 for ( i
=0x80; i
; i
>>=1 )
433 pcf50606_i2c_ack(ack
);
438 int pcf50606_i2c_write(int address
, const unsigned char* buf
, int count
)
442 pcf50606_i2c_start();
443 pcf50606_i2c_outb(address
& 0xfe);
444 if (pcf50606_i2c_getack())
446 for (i
=0; i
<count
; i
++)
448 pcf50606_i2c_outb(buf
[i
]);
449 if (!pcf50606_i2c_getack())
458 logf("pcf50606_i2c_write() - no ack\n");
464 int pcf50606_read_multiple(int address
, unsigned char* buf
, int count
)
468 unsigned char obuf
[1];
472 /* send read command */
473 if (pcf50606_i2c_write(PCF50606_ADDR
, obuf
, 1) >= 0)
475 pcf50606_i2c_start();
476 pcf50606_i2c_outb(0x11);
477 if (pcf50606_i2c_getack())
479 for(i
= 0;i
< count
-1;i
++)
480 buf
[i
] = pcf50606_i2c_inb(true);
482 buf
[i
] = pcf50606_i2c_inb(false);
495 int pcf50606_read(int address
)
500 ret
= pcf50606_read_multiple(address
, &c
, 1);
507 int pcf50606_write_multiple(int address
, const unsigned char* buf
, int count
)
509 unsigned char obuf
[1];
515 /* send write command */
516 if (pcf50606_i2c_write(PCF50606_ADDR
, obuf
, 1) >= 0)
518 for (i
=0; i
<count
; i
++)
520 pcf50606_i2c_outb(buf
[i
]);
521 if (!pcf50606_i2c_getack())
537 int pcf50606_write(int address
, unsigned char val
)
539 return pcf50606_write_multiple(address
, &val
, 1);