blob: 313288dd112b0e8c3e6f1c553019697ec0346785 [file] [log] [blame]
Valentin Yakovenkov08ea5502009-10-26 18:49:06 -04001/*
2 * File: drivers/i2c/pca9564.c
3 * Based on: drivers/i2c/s3c44b0_i2c.c
4 * Author:
5 *
6 * Created: 2009-06-23
7 * Description: PCA9564 i2c bridge driver
8 *
9 * Modified:
10 * Copyright 2009 CJSC "NII STT", http://www.niistt.ru/
11 *
12 * Bugs:
13 *
Wolfgang Denk1a459662013-07-08 09:37:19 +020014 * SPDX-License-Identifier: GPL-2.0+
Valentin Yakovenkov08ea5502009-10-26 18:49:06 -040015 */
16
17#include <common.h>
18#include <i2c.h>
19#include <pca9564.h>
20#include <asm/io.h>
21
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))
27
28static unsigned char pca_read_reg(unsigned int reg)
29{
30 return readb((void *)reg);
31}
32
33static void pca_write_reg(unsigned int reg, unsigned char value)
34{
35 writeb(value, (void *)reg);
36}
37
38static int pca_wait_busy(void)
39{
40 unsigned int timeout = 10000;
41
42 while (!(pca_read_reg(PCA_CON) & PCA_CON_SI) && --timeout)
43 udelay(1);
44
45 if (timeout == 0)
46 debug("I2C timeout!\n");
47
48 debug("CON = 0x%02x, STA = 0x%02x\n", pca_read_reg(PCA_CON),
49 pca_read_reg(PCA_STA));
50
51 return timeout ? 0 : 1;
52}
53
54/*=====================================================================*/
55/* Public Functions */
56/*=====================================================================*/
57
58/*-----------------------------------------------------------------------
59 * Initialization
60 */
61void i2c_init(int speed, int slaveaddr)
62{
63 pca_write_reg(PCA_CON, PCA_CON_ENSIO | speed);
64}
65
66/*
67 * Probe the given I2C chip address. Returns 0 if a chip responded,
68 * not 0 on failure.
69 */
70
71int i2c_probe(uchar chip)
72{
73 unsigned char res;
74
75 pca_write_reg(PCA_CON, PCA_CON_STA | PCA_CON_ENSIO);
76 pca_wait_busy();
77
78 pca_write_reg(PCA_CON, PCA_CON_STA | PCA_CON_ENSIO);
79
80 pca_write_reg(PCA_DAT, (chip << 1) | 1);
81 res = pca_wait_busy();
82
83 if ((res == 0) && (pca_read_reg(PCA_STA) == 0x48))
84 res = 1;
85
86 pca_write_reg(PCA_CON, PCA_CON_STO | PCA_CON_ENSIO);
87
88 return res;
89}
90
91/*
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
100 *
101 * Returns: 0 on success, not 0 on failure
102 */
103int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len)
104{
105 int i;
106
107 pca_write_reg(PCA_CON, PCA_CON_ENSIO | PCA_CON_STA);
108 pca_wait_busy();
109
110 pca_write_reg(PCA_CON, PCA_CON_ENSIO);
111
112 pca_write_reg(PCA_DAT, (chip << 1));
113 pca_wait_busy();
114 pca_write_reg(PCA_CON, PCA_CON_ENSIO);
115
116 if (alen > 0) {
117 pca_write_reg(PCA_DAT, addr);
118 pca_wait_busy();
119 pca_write_reg(PCA_CON, PCA_CON_ENSIO);
120 }
121
122 pca_write_reg(PCA_CON, PCA_CON_ENSIO | PCA_CON_STO);
123
124 udelay(500);
125
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);
129
130 pca_write_reg(PCA_DAT, (chip << 1) | 1);
131 pca_wait_busy();
132
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);
138
139 pca_wait_busy();
140 buffer[i] = pca_read_reg(PCA_DAT);
141
142 }
143
144 pca_write_reg(PCA_CON, PCA_CON_ENSIO | PCA_CON_STO);
145
146 return 0;
147}
148
149int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len)
150{
151 int i;
152
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);
156
157 pca_write_reg(PCA_DAT, chip << 1);
158 pca_wait_busy();
159 pca_write_reg(PCA_CON, PCA_CON_ENSIO);
160
161 if (alen > 0) {
162 pca_write_reg(PCA_DAT, addr);
163 pca_wait_busy();
164 pca_write_reg(PCA_CON, PCA_CON_ENSIO);
165 }
166
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);
171 }
172
173 pca_write_reg(PCA_CON, PCA_CON_STO | PCA_CON_ENSIO);
174
175 return 0;
176}