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 * SPDX-License-Identifier: GPL-2.0+
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)
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 /*-----------------------------------------------------------------------
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,
71 int i2c_probe(uchar chip)
75 pca_write_reg(PCA_CON, PCA_CON_STA | PCA_CON_ENSIO);
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))
86 pca_write_reg(PCA_CON, PCA_CON_STO | PCA_CON_ENSIO);
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
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)
107 pca_write_reg(PCA_CON, PCA_CON_ENSIO | PCA_CON_STA);
110 pca_write_reg(PCA_CON, PCA_CON_ENSIO);
112 pca_write_reg(PCA_DAT, (chip << 1));
114 pca_write_reg(PCA_CON, PCA_CON_ENSIO);
117 pca_write_reg(PCA_DAT, addr);
119 pca_write_reg(PCA_CON, PCA_CON_ENSIO);
122 pca_write_reg(PCA_CON, PCA_CON_ENSIO | PCA_CON_STO);
126 pca_write_reg(PCA_CON, PCA_CON_ENSIO | PCA_CON_STA);
128 pca_write_reg(PCA_CON, PCA_CON_ENSIO);
130 pca_write_reg(PCA_DAT, (chip << 1) | 1);
133 for (i = 0; i < len; ++i) {
135 pca_write_reg(PCA_CON, PCA_CON_ENSIO);
137 pca_write_reg(PCA_CON, PCA_CON_ENSIO | PCA_CON_AA);
140 buffer[i] = pca_read_reg(PCA_DAT);
144 pca_write_reg(PCA_CON, PCA_CON_ENSIO | PCA_CON_STO);
149 int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len)
153 pca_write_reg(PCA_CON, PCA_CON_ENSIO | PCA_CON_STA);
155 pca_write_reg(PCA_CON, PCA_CON_ENSIO);
157 pca_write_reg(PCA_DAT, chip << 1);
159 pca_write_reg(PCA_CON, PCA_CON_ENSIO);
162 pca_write_reg(PCA_DAT, addr);
164 pca_write_reg(PCA_CON, PCA_CON_ENSIO);
167 for (i = 0; i < len; ++i) {
168 pca_write_reg(PCA_DAT, buffer[i]);
170 pca_write_reg(PCA_CON, PCA_CON_ENSIO);
173 pca_write_reg(PCA_CON, PCA_CON_STO | PCA_CON_ENSIO);