backlight: Rename the corgi backlight driver to generic
[linux-2.6-block.git] / drivers / i2c / algos / i2c-algo-pca.c
CommitLineData
1da177e4 1/*
3d438291 2 * i2c-algo-pca.c i2c driver algorithms for PCA9564 adapters
1da177e4 3 * Copyright (C) 2004 Arcom Control Systems
c01b0831 4 * Copyright (C) 2008 Pengutronix
1da177e4
LT
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21#include <linux/kernel.h>
22#include <linux/module.h>
23#include <linux/moduleparam.h>
24#include <linux/delay.h>
1da177e4
LT
25#include <linux/init.h>
26#include <linux/errno.h>
27#include <linux/i2c.h>
28#include <linux/i2c-algo-pca.h>
1da177e4
LT
29
30#define DEB1(fmt, args...) do { if (i2c_debug>=1) printk(fmt, ## args); } while(0)
31#define DEB2(fmt, args...) do { if (i2c_debug>=2) printk(fmt, ## args); } while(0)
32#define DEB3(fmt, args...) do { if (i2c_debug>=3) printk(fmt, ## args); } while(0)
33
60507095 34static int i2c_debug;
1da177e4 35
c01b0831
WS
36#define pca_outw(adap, reg, val) adap->write_byte(adap->data, reg, val)
37#define pca_inw(adap, reg) adap->read_byte(adap->data, reg)
1da177e4
LT
38
39#define pca_status(adap) pca_inw(adap, I2C_PCA_STA)
c01b0831 40#define pca_clock(adap) adap->i2c_clock
1da177e4
LT
41#define pca_set_con(adap, val) pca_outw(adap, I2C_PCA_CON, val)
42#define pca_get_con(adap) pca_inw(adap, I2C_PCA_CON)
c01b0831
WS
43#define pca_wait(adap) adap->wait_for_completion(adap->data)
44#define pca_reset(adap) adap->reset_chip(adap->data)
1da177e4
LT
45
46/*
47 * Generate a start condition on the i2c bus.
48 *
44bbe87e 49 * returns after the start condition has occurred
1da177e4
LT
50 */
51static void pca_start(struct i2c_algo_pca_data *adap)
52{
53 int sta = pca_get_con(adap);
54 DEB2("=== START\n");
55 sta |= I2C_PCA_CON_STA;
56 sta &= ~(I2C_PCA_CON_STO|I2C_PCA_CON_SI);
57 pca_set_con(adap, sta);
58 pca_wait(adap);
59}
60
61/*
46b615f4 62 * Generate a repeated start condition on the i2c bus
1da177e4 63 *
44bbe87e 64 * return after the repeated start condition has occurred
1da177e4
LT
65 */
66static void pca_repeated_start(struct i2c_algo_pca_data *adap)
67{
68 int sta = pca_get_con(adap);
69 DEB2("=== REPEATED START\n");
70 sta |= I2C_PCA_CON_STA;
71 sta &= ~(I2C_PCA_CON_STO|I2C_PCA_CON_SI);
72 pca_set_con(adap, sta);
73 pca_wait(adap);
74}
75
76/*
77 * Generate a stop condition on the i2c bus
78 *
79 * returns after the stop condition has been generated
80 *
81 * STOPs do not generate an interrupt or set the SI flag, since the
46b615f4 82 * part returns the idle state (0xf8). Hence we don't need to
1da177e4
LT
83 * pca_wait here.
84 */
85static void pca_stop(struct i2c_algo_pca_data *adap)
86{
87 int sta = pca_get_con(adap);
88 DEB2("=== STOP\n");
89 sta |= I2C_PCA_CON_STO;
90 sta &= ~(I2C_PCA_CON_STA|I2C_PCA_CON_SI);
91 pca_set_con(adap, sta);
92}
93
94/*
95 * Send the slave address and R/W bit
96 *
97 * returns after the address has been sent
98 */
3d438291 99static void pca_address(struct i2c_algo_pca_data *adap,
1da177e4
LT
100 struct i2c_msg *msg)
101{
102 int sta = pca_get_con(adap);
103 int addr;
104
105 addr = ( (0x7f & msg->addr) << 1 );
106 if (msg->flags & I2C_M_RD )
107 addr |= 1;
3d438291 108 DEB2("=== SLAVE ADDRESS %#04x+%c=%#04x\n",
1da177e4 109 msg->addr, msg->flags & I2C_M_RD ? 'R' : 'W', addr);
3d438291 110
1da177e4
LT
111 pca_outw(adap, I2C_PCA_DAT, addr);
112
113 sta &= ~(I2C_PCA_CON_STO|I2C_PCA_CON_STA|I2C_PCA_CON_SI);
114 pca_set_con(adap, sta);
115
116 pca_wait(adap);
117}
118
119/*
120 * Transmit a byte.
121 *
122 * Returns after the byte has been transmitted
123 */
3d438291 124static void pca_tx_byte(struct i2c_algo_pca_data *adap,
1da177e4
LT
125 __u8 b)
126{
127 int sta = pca_get_con(adap);
128 DEB2("=== WRITE %#04x\n", b);
129 pca_outw(adap, I2C_PCA_DAT, b);
130
131 sta &= ~(I2C_PCA_CON_STO|I2C_PCA_CON_STA|I2C_PCA_CON_SI);
132 pca_set_con(adap, sta);
133
134 pca_wait(adap);
135}
136
137/*
138 * Receive a byte
139 *
140 * returns immediately.
141 */
3d438291 142static void pca_rx_byte(struct i2c_algo_pca_data *adap,
1da177e4
LT
143 __u8 *b, int ack)
144{
145 *b = pca_inw(adap, I2C_PCA_DAT);
146 DEB2("=== READ %#04x %s\n", *b, ack ? "ACK" : "NACK");
147}
148
3d438291 149/*
1da177e4
LT
150 * Setup ACK or NACK for next received byte and wait for it to arrive.
151 *
152 * Returns after next byte has arrived.
153 */
3d438291 154static void pca_rx_ack(struct i2c_algo_pca_data *adap,
1da177e4
LT
155 int ack)
156{
157 int sta = pca_get_con(adap);
158
159 sta &= ~(I2C_PCA_CON_STO|I2C_PCA_CON_STA|I2C_PCA_CON_SI|I2C_PCA_CON_AA);
160
161 if ( ack )
162 sta |= I2C_PCA_CON_AA;
163
164 pca_set_con(adap, sta);
165 pca_wait(adap);
166}
167
1da177e4
LT
168static int pca_xfer(struct i2c_adapter *i2c_adap,
169 struct i2c_msg *msgs,
170 int num)
171{
172 struct i2c_algo_pca_data *adap = i2c_adap->algo_data;
173 struct i2c_msg *msg = NULL;
174 int curmsg;
175 int numbytes = 0;
176 int state;
177 int ret;
c01b0831 178 int timeout = i2c_adap->timeout;
1da177e4 179
48edcb65
IC
180 while ((state = pca_status(adap)) != 0xf8 && timeout--) {
181 msleep(10);
182 }
183 if (state != 0xf8) {
184 dev_dbg(&i2c_adap->dev, "bus is not idle. status is %#04x\n", state);
c80ebe79 185 return -EAGAIN;
1da177e4
LT
186 }
187
188 DEB1("{{{ XFER %d messages\n", num);
189
190 if (i2c_debug>=2) {
191 for (curmsg = 0; curmsg < num; curmsg++) {
192 int addr, i;
193 msg = &msgs[curmsg];
3d438291 194
1da177e4 195 addr = (0x7f & msg->addr) ;
3d438291 196
1da177e4 197 if (msg->flags & I2C_M_RD )
3d438291 198 printk(KERN_INFO " [%02d] RD %d bytes from %#02x [%#02x, ...]\n",
1da177e4
LT
199 curmsg, msg->len, addr, (addr<<1) | 1);
200 else {
3d438291 201 printk(KERN_INFO " [%02d] WR %d bytes to %#02x [%#02x%s",
1da177e4
LT
202 curmsg, msg->len, addr, addr<<1,
203 msg->len == 0 ? "" : ", ");
204 for(i=0; i < msg->len; i++)
205 printk("%#04x%s", msg->buf[i], i == msg->len - 1 ? "" : ", ");
206 printk("]\n");
207 }
208 }
209 }
210
211 curmsg = 0;
212 ret = -EREMOTEIO;
213 while (curmsg < num) {
214 state = pca_status(adap);
215
216 DEB3("STATE is 0x%02x\n", state);
217 msg = &msgs[curmsg];
218
219 switch (state) {
220 case 0xf8: /* On reset or stop the bus is idle */
221 pca_start(adap);
222 break;
223
224 case 0x08: /* A START condition has been transmitted */
225 case 0x10: /* A repeated start condition has been transmitted */
226 pca_address(adap, msg);
227 break;
3d438291 228
1da177e4
LT
229 case 0x18: /* SLA+W has been transmitted; ACK has been received */
230 case 0x28: /* Data byte in I2CDAT has been transmitted; ACK has been received */
231 if (numbytes < msg->len) {
232 pca_tx_byte(adap, msg->buf[numbytes]);
233 numbytes++;
234 break;
235 }
236 curmsg++; numbytes = 0;
237 if (curmsg == num)
238 pca_stop(adap);
239 else
240 pca_repeated_start(adap);
241 break;
242
243 case 0x20: /* SLA+W has been transmitted; NOT ACK has been received */
244 DEB2("NOT ACK received after SLA+W\n");
245 pca_stop(adap);
246 goto out;
247
248 case 0x40: /* SLA+R has been transmitted; ACK has been received */
249 pca_rx_ack(adap, msg->len > 1);
250 break;
251
252 case 0x50: /* Data bytes has been received; ACK has been returned */
253 if (numbytes < msg->len) {
254 pca_rx_byte(adap, &msg->buf[numbytes], 1);
255 numbytes++;
256 pca_rx_ack(adap, numbytes < msg->len - 1);
257 break;
258 }
259 curmsg++; numbytes = 0;
260 if (curmsg == num)
261 pca_stop(adap);
262 else
263 pca_repeated_start(adap);
264 break;
265
266 case 0x48: /* SLA+R has been transmitted; NOT ACK has been received */
267 DEB2("NOT ACK received after SLA+R\n");
268 pca_stop(adap);
269 goto out;
270
271 case 0x30: /* Data byte in I2CDAT has been transmitted; NOT ACK has been received */
272 DEB2("NOT ACK received after data byte\n");
273 goto out;
274
275 case 0x38: /* Arbitration lost during SLA+W, SLA+R or data bytes */
276 DEB2("Arbitration lost\n");
277 goto out;
3d438291 278
1da177e4
LT
279 case 0x58: /* Data byte has been received; NOT ACK has been returned */
280 if ( numbytes == msg->len - 1 ) {
281 pca_rx_byte(adap, &msg->buf[numbytes], 0);
282 curmsg++; numbytes = 0;
283 if (curmsg == num)
284 pca_stop(adap);
285 else
286 pca_repeated_start(adap);
287 } else {
288 DEB2("NOT ACK sent after data byte received. "
289 "Not final byte. numbytes %d. len %d\n",
290 numbytes, msg->len);
291 pca_stop(adap);
292 goto out;
293 }
294 break;
295 case 0x70: /* Bus error - SDA stuck low */
296 DEB2("BUS ERROR - SDA Stuck low\n");
297 pca_reset(adap);
298 goto out;
299 case 0x90: /* Bus error - SCL stuck low */
300 DEB2("BUS ERROR - SCL Stuck low\n");
301 pca_reset(adap);
302 goto out;
303 case 0x00: /* Bus error during master or slave mode due to illegal START or STOP condition */
304 DEB2("BUS ERROR - Illegal START or STOP\n");
305 pca_reset(adap);
306 goto out;
307 default:
c01b0831 308 dev_err(&i2c_adap->dev, "unhandled SIO state 0x%02x\n", state);
1da177e4
LT
309 break;
310 }
3d438291 311
1da177e4
LT
312 }
313
314 ret = curmsg;
315 out:
316 DEB1(KERN_CRIT "}}} transfered %d/%d messages. "
3d438291 317 "status is %#04x. control is %#04x\n",
1da177e4
LT
318 curmsg, num, pca_status(adap),
319 pca_get_con(adap));
320 return ret;
321}
322
323static u32 pca_func(struct i2c_adapter *adap)
324{
325 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
326}
327
c01b0831
WS
328static const struct i2c_algorithm pca_algo = {
329 .master_xfer = pca_xfer,
330 .functionality = pca_func,
331};
332
333static int pca_init(struct i2c_adapter *adap)
1da177e4
LT
334{
335 static int freqs[] = {330,288,217,146,88,59,44,36};
c01b0831
WS
336 int clock;
337 struct i2c_algo_pca_data *pca_data = adap->algo_data;
338
339 if (pca_data->i2c_clock > 7) {
340 printk(KERN_WARNING "%s: Invalid I2C clock speed selected. Trying default.\n",
341 adap->name);
342 pca_data->i2c_clock = I2C_PCA_CON_59kHz;
343 }
344
345 adap->algo = &pca_algo;
1da177e4 346
c01b0831 347 pca_reset(pca_data);
1da177e4 348
c01b0831
WS
349 clock = pca_clock(pca_data);
350 DEB1(KERN_INFO "%s: Clock frequency is %dkHz\n", adap->name, freqs[clock]);
1da177e4 351
c01b0831 352 pca_set_con(pca_data, I2C_PCA_CON_ENSIO | clock);
3d438291 353 udelay(500); /* 500 us for oscilator to stabilise */
1da177e4
LT
354
355 return 0;
356}
357
3d438291
WS
358/*
359 * registering functions to load algorithms at runtime
1da177e4
LT
360 */
361int i2c_pca_add_bus(struct i2c_adapter *adap)
362{
1da177e4
LT
363 int rval;
364
c01b0831
WS
365 rval = pca_init(adap);
366 if (rval)
367 return rval;
1da177e4 368
c01b0831
WS
369 return i2c_add_adapter(adap);
370}
371EXPORT_SYMBOL(i2c_pca_add_bus);
1da177e4 372
c01b0831
WS
373int i2c_pca_add_numbered_bus(struct i2c_adapter *adap)
374{
375 int rval;
1da177e4 376
c01b0831
WS
377 rval = pca_init(adap);
378 if (rval)
379 return rval;
1da177e4 380
c01b0831 381 return i2c_add_numbered_adapter(adap);
1da177e4 382}
c01b0831 383EXPORT_SYMBOL(i2c_pca_add_numbered_bus);
1da177e4 384
c01b0831
WS
385MODULE_AUTHOR("Ian Campbell <icampbell@arcom.com>, "
386 "Wolfram Sang <w.sang@pengutronix.de>");
1da177e4
LT
387MODULE_DESCRIPTION("I2C-Bus PCA9564 algorithm");
388MODULE_LICENSE("GPL");
389
390module_param(i2c_debug, int, 0);