arm: vf610: fix double iomux configuration for vf610twr board
[u-boot/qq2440-u-boot.git] / drivers / i2c / pca9564_i2c.c
blob313288dd112b0e8c3e6f1c553019697ec0346785
1 /*
2 * File: drivers/i2c/pca9564.c
3 * Based on: drivers/i2c/s3c44b0_i2c.c
4 * Author:
6 * Created: 2009-06-23
7 * Description: PCA9564 i2c bridge driver
9 * Modified:
10 * Copyright 2009 CJSC "NII STT", http://www.niistt.ru/
12 * Bugs:
14 * SPDX-License-Identifier: GPL-2.0+
17 #include <common.h>
18 #include <i2c.h>
19 #include <pca9564.h>
20 #include <asm/io.h>
22 #define PCA_STA (CONFIG_PCA9564_BASE + 0)
23 #define PCA_TO (CONFIG_PCA9564_BASE + 0)
24 #define PCA_DAT (CONFIG_PCA9564_BASE + (1 << 2))
25 #define PCA_ADR (CONFIG_PCA9564_BASE + (2 << 2))
26 #define PCA_CON (CONFIG_PCA9564_BASE + (3 << 2))
28 static unsigned char pca_read_reg(unsigned int reg)
30 return readb((void *)reg);
33 static void pca_write_reg(unsigned int reg, unsigned char value)
35 writeb(value, (void *)reg);
38 static int pca_wait_busy(void)
40 unsigned int timeout = 10000;
42 while (!(pca_read_reg(PCA_CON) & PCA_CON_SI) && --timeout)
43 udelay(1);
45 if (timeout == 0)
46 debug("I2C timeout!\n");
48 debug("CON = 0x%02x, STA = 0x%02x\n", pca_read_reg(PCA_CON),
49 pca_read_reg(PCA_STA));
51 return timeout ? 0 : 1;
54 /*=====================================================================*/
55 /* Public Functions */
56 /*=====================================================================*/
58 /*-----------------------------------------------------------------------
59 * Initialization
61 void i2c_init(int speed, int slaveaddr)
63 pca_write_reg(PCA_CON, PCA_CON_ENSIO | speed);
67 * Probe the given I2C chip address. Returns 0 if a chip responded,
68 * not 0 on failure.
71 int i2c_probe(uchar chip)
73 unsigned char res;
75 pca_write_reg(PCA_CON, PCA_CON_STA | PCA_CON_ENSIO);
76 pca_wait_busy();
78 pca_write_reg(PCA_CON, PCA_CON_STA | PCA_CON_ENSIO);
80 pca_write_reg(PCA_DAT, (chip << 1) | 1);
81 res = pca_wait_busy();
83 if ((res == 0) && (pca_read_reg(PCA_STA) == 0x48))
84 res = 1;
86 pca_write_reg(PCA_CON, PCA_CON_STO | PCA_CON_ENSIO);
88 return res;
92 * Read/Write interface:
93 * chip: I2C chip address, range 0..127
94 * addr: Memory (register) address within the chip
95 * alen: Number of bytes to use for addr (typically 1, 2 for larger
96 * memories, 0 for register type devices with only one
97 * register)
98 * buffer: Where to read/write the data
99 * len: How many bytes to read/write
101 * Returns: 0 on success, not 0 on failure
103 int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len)
105 int i;
107 pca_write_reg(PCA_CON, PCA_CON_ENSIO | PCA_CON_STA);
108 pca_wait_busy();
110 pca_write_reg(PCA_CON, PCA_CON_ENSIO);
112 pca_write_reg(PCA_DAT, (chip << 1));
113 pca_wait_busy();
114 pca_write_reg(PCA_CON, PCA_CON_ENSIO);
116 if (alen > 0) {
117 pca_write_reg(PCA_DAT, addr);
118 pca_wait_busy();
119 pca_write_reg(PCA_CON, PCA_CON_ENSIO);
122 pca_write_reg(PCA_CON, PCA_CON_ENSIO | PCA_CON_STO);
124 udelay(500);
126 pca_write_reg(PCA_CON, PCA_CON_ENSIO | PCA_CON_STA);
127 pca_wait_busy();
128 pca_write_reg(PCA_CON, PCA_CON_ENSIO);
130 pca_write_reg(PCA_DAT, (chip << 1) | 1);
131 pca_wait_busy();
133 for (i = 0; i < len; ++i) {
134 if (i == len - 1)
135 pca_write_reg(PCA_CON, PCA_CON_ENSIO);
136 else
137 pca_write_reg(PCA_CON, PCA_CON_ENSIO | PCA_CON_AA);
139 pca_wait_busy();
140 buffer[i] = pca_read_reg(PCA_DAT);
144 pca_write_reg(PCA_CON, PCA_CON_ENSIO | PCA_CON_STO);
146 return 0;
149 int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len)
151 int i;
153 pca_write_reg(PCA_CON, PCA_CON_ENSIO | PCA_CON_STA);
154 pca_wait_busy();
155 pca_write_reg(PCA_CON, PCA_CON_ENSIO);
157 pca_write_reg(PCA_DAT, chip << 1);
158 pca_wait_busy();
159 pca_write_reg(PCA_CON, PCA_CON_ENSIO);
161 if (alen > 0) {
162 pca_write_reg(PCA_DAT, addr);
163 pca_wait_busy();
164 pca_write_reg(PCA_CON, PCA_CON_ENSIO);
167 for (i = 0; i < len; ++i) {
168 pca_write_reg(PCA_DAT, buffer[i]);
169 pca_wait_busy();
170 pca_write_reg(PCA_CON, PCA_CON_ENSIO);
173 pca_write_reg(PCA_CON, PCA_CON_STO | PCA_CON_ENSIO);
175 return 0;