2 * File: drivers/i2c/pca9564.c
3 * Based on: drivers/i2c/s3c44b0_i2c.c
7 * Description: PCA9564 i2c bridge driver
10 * Copyright 2009 CJSC "NII STT", http://www.niistt.ru/
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, see the file COPYING, or write
26 * to the Free Software Foundation, Inc.,
27 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
35 #define PCA_STA (CONFIG_PCA9564_BASE + 0)
36 #define PCA_TO (CONFIG_PCA9564_BASE + 0)
37 #define PCA_DAT (CONFIG_PCA9564_BASE + (1 << 2))
38 #define PCA_ADR (CONFIG_PCA9564_BASE + (2 << 2))
39 #define PCA_CON (CONFIG_PCA9564_BASE + (3 << 2))
41 static unsigned char pca_read_reg(unsigned int reg)
43 return readb((void *)reg);
46 static void pca_write_reg(unsigned int reg, unsigned char value)
48 writeb(value, (void *)reg);
51 static int pca_wait_busy(void)
53 unsigned int timeout = 10000;
55 while (!(pca_read_reg(PCA_CON) & PCA_CON_SI) && --timeout)
59 debug("I2C timeout!\n");
61 debug("CON = 0x%02x, STA = 0x%02x\n", pca_read_reg(PCA_CON),
62 pca_read_reg(PCA_STA));
64 return timeout ? 0 : 1;
67 /*=====================================================================*/
68 /* Public Functions */
69 /*=====================================================================*/
71 /*-----------------------------------------------------------------------
74 void i2c_init(int speed, int slaveaddr)
76 pca_write_reg(PCA_CON, PCA_CON_ENSIO | speed);
80 * Probe the given I2C chip address. Returns 0 if a chip responded,
84 int i2c_probe(uchar chip)
88 pca_write_reg(PCA_CON, PCA_CON_STA | PCA_CON_ENSIO);
91 pca_write_reg(PCA_CON, PCA_CON_STA | PCA_CON_ENSIO);
93 pca_write_reg(PCA_DAT, (chip << 1) | 1);
94 res = pca_wait_busy();
96 if ((res == 0) && (pca_read_reg(PCA_STA) == 0x48))
99 pca_write_reg(PCA_CON, PCA_CON_STO | PCA_CON_ENSIO);
105 * Read/Write interface:
106 * chip: I2C chip address, range 0..127
107 * addr: Memory (register) address within the chip
108 * alen: Number of bytes to use for addr (typically 1, 2 for larger
109 * memories, 0 for register type devices with only one
111 * buffer: Where to read/write the data
112 * len: How many bytes to read/write
114 * Returns: 0 on success, not 0 on failure
116 int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len)
120 pca_write_reg(PCA_CON, PCA_CON_ENSIO | PCA_CON_STA);
123 pca_write_reg(PCA_CON, PCA_CON_ENSIO);
125 pca_write_reg(PCA_DAT, (chip << 1));
127 pca_write_reg(PCA_CON, PCA_CON_ENSIO);
130 pca_write_reg(PCA_DAT, addr);
132 pca_write_reg(PCA_CON, PCA_CON_ENSIO);
135 pca_write_reg(PCA_CON, PCA_CON_ENSIO | PCA_CON_STO);
139 pca_write_reg(PCA_CON, PCA_CON_ENSIO | PCA_CON_STA);
141 pca_write_reg(PCA_CON, PCA_CON_ENSIO);
143 pca_write_reg(PCA_DAT, (chip << 1) | 1);
146 for (i = 0; i < len; ++i) {
148 pca_write_reg(PCA_CON, PCA_CON_ENSIO);
150 pca_write_reg(PCA_CON, PCA_CON_ENSIO | PCA_CON_AA);
153 buffer[i] = pca_read_reg(PCA_DAT);
157 pca_write_reg(PCA_CON, PCA_CON_ENSIO | PCA_CON_STO);
162 int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len)
166 pca_write_reg(PCA_CON, PCA_CON_ENSIO | PCA_CON_STA);
168 pca_write_reg(PCA_CON, PCA_CON_ENSIO);
170 pca_write_reg(PCA_DAT, chip << 1);
172 pca_write_reg(PCA_CON, PCA_CON_ENSIO);
175 pca_write_reg(PCA_DAT, addr);
177 pca_write_reg(PCA_CON, PCA_CON_ENSIO);
180 for (i = 0; i < len; ++i) {
181 pca_write_reg(PCA_DAT, buffer[i]);
183 pca_write_reg(PCA_CON, PCA_CON_ENSIO);
186 pca_write_reg(PCA_CON, PCA_CON_STO | PCA_CON_ENSIO);