aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/i2c/algos/i2c-algo-pca.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/i2c/algos/i2c-algo-pca.c')
-rw-r--r--drivers/i2c/algos/i2c-algo-pca.c250
1 files changed, 207 insertions, 43 deletions
diff --git a/drivers/i2c/algos/i2c-algo-pca.c b/drivers/i2c/algos/i2c-algo-pca.c
index d50b329a3c94..f68e5f8e23ee 100644
--- a/drivers/i2c/algos/i2c-algo-pca.c
+++ b/drivers/i2c/algos/i2c-algo-pca.c
@@ -22,14 +22,18 @@
22#include <linux/module.h> 22#include <linux/module.h>
23#include <linux/moduleparam.h> 23#include <linux/moduleparam.h>
24#include <linux/delay.h> 24#include <linux/delay.h>
25#include <linux/jiffies.h>
25#include <linux/init.h> 26#include <linux/init.h>
26#include <linux/errno.h> 27#include <linux/errno.h>
27#include <linux/i2c.h> 28#include <linux/i2c.h>
28#include <linux/i2c-algo-pca.h> 29#include <linux/i2c-algo-pca.h>
29 30
30#define DEB1(fmt, args...) do { if (i2c_debug>=1) printk(fmt, ## args); } while(0) 31#define DEB1(fmt, args...) do { if (i2c_debug >= 1) \
31#define DEB2(fmt, args...) do { if (i2c_debug>=2) printk(fmt, ## args); } while(0) 32 printk(KERN_DEBUG fmt, ## args); } while (0)
32#define DEB3(fmt, args...) do { if (i2c_debug>=3) printk(fmt, ## args); } while(0) 33#define DEB2(fmt, args...) do { if (i2c_debug >= 2) \
34 printk(KERN_DEBUG fmt, ## args); } while (0)
35#define DEB3(fmt, args...) do { if (i2c_debug >= 3) \
36 printk(KERN_DEBUG fmt, ## args); } while (0)
33 37
34static int i2c_debug; 38static int i2c_debug;
35 39
@@ -43,19 +47,27 @@ static int i2c_debug;
43#define pca_wait(adap) adap->wait_for_completion(adap->data) 47#define pca_wait(adap) adap->wait_for_completion(adap->data)
44#define pca_reset(adap) adap->reset_chip(adap->data) 48#define pca_reset(adap) adap->reset_chip(adap->data)
45 49
50static void pca9665_reset(void *pd)
51{
52 struct i2c_algo_pca_data *adap = pd;
53 pca_outw(adap, I2C_PCA_INDPTR, I2C_PCA_IPRESET);
54 pca_outw(adap, I2C_PCA_IND, 0xA5);
55 pca_outw(adap, I2C_PCA_IND, 0x5A);
56}
57
46/* 58/*
47 * Generate a start condition on the i2c bus. 59 * Generate a start condition on the i2c bus.
48 * 60 *
49 * returns after the start condition has occurred 61 * returns after the start condition has occurred
50 */ 62 */
51static void pca_start(struct i2c_algo_pca_data *adap) 63static int pca_start(struct i2c_algo_pca_data *adap)
52{ 64{
53 int sta = pca_get_con(adap); 65 int sta = pca_get_con(adap);
54 DEB2("=== START\n"); 66 DEB2("=== START\n");
55 sta |= I2C_PCA_CON_STA; 67 sta |= I2C_PCA_CON_STA;
56 sta &= ~(I2C_PCA_CON_STO|I2C_PCA_CON_SI); 68 sta &= ~(I2C_PCA_CON_STO|I2C_PCA_CON_SI);
57 pca_set_con(adap, sta); 69 pca_set_con(adap, sta);
58 pca_wait(adap); 70 return pca_wait(adap);
59} 71}
60 72
61/* 73/*
@@ -63,14 +75,14 @@ static void pca_start(struct i2c_algo_pca_data *adap)
63 * 75 *
64 * return after the repeated start condition has occurred 76 * return after the repeated start condition has occurred
65 */ 77 */
66static void pca_repeated_start(struct i2c_algo_pca_data *adap) 78static int pca_repeated_start(struct i2c_algo_pca_data *adap)
67{ 79{
68 int sta = pca_get_con(adap); 80 int sta = pca_get_con(adap);
69 DEB2("=== REPEATED START\n"); 81 DEB2("=== REPEATED START\n");
70 sta |= I2C_PCA_CON_STA; 82 sta |= I2C_PCA_CON_STA;
71 sta &= ~(I2C_PCA_CON_STO|I2C_PCA_CON_SI); 83 sta &= ~(I2C_PCA_CON_STO|I2C_PCA_CON_SI);
72 pca_set_con(adap, sta); 84 pca_set_con(adap, sta);
73 pca_wait(adap); 85 return pca_wait(adap);
74} 86}
75 87
76/* 88/*
@@ -96,7 +108,7 @@ static void pca_stop(struct i2c_algo_pca_data *adap)
96 * 108 *
97 * returns after the address has been sent 109 * returns after the address has been sent
98 */ 110 */
99static void pca_address(struct i2c_algo_pca_data *adap, 111static int pca_address(struct i2c_algo_pca_data *adap,
100 struct i2c_msg *msg) 112 struct i2c_msg *msg)
101{ 113{
102 int sta = pca_get_con(adap); 114 int sta = pca_get_con(adap);
@@ -113,7 +125,7 @@ static void pca_address(struct i2c_algo_pca_data *adap,
113 sta &= ~(I2C_PCA_CON_STO|I2C_PCA_CON_STA|I2C_PCA_CON_SI); 125 sta &= ~(I2C_PCA_CON_STO|I2C_PCA_CON_STA|I2C_PCA_CON_SI);
114 pca_set_con(adap, sta); 126 pca_set_con(adap, sta);
115 127
116 pca_wait(adap); 128 return pca_wait(adap);
117} 129}
118 130
119/* 131/*
@@ -121,7 +133,7 @@ static void pca_address(struct i2c_algo_pca_data *adap,
121 * 133 *
122 * Returns after the byte has been transmitted 134 * Returns after the byte has been transmitted
123 */ 135 */
124static void pca_tx_byte(struct i2c_algo_pca_data *adap, 136static int pca_tx_byte(struct i2c_algo_pca_data *adap,
125 __u8 b) 137 __u8 b)
126{ 138{
127 int sta = pca_get_con(adap); 139 int sta = pca_get_con(adap);
@@ -131,7 +143,7 @@ static void pca_tx_byte(struct i2c_algo_pca_data *adap,
131 sta &= ~(I2C_PCA_CON_STO|I2C_PCA_CON_STA|I2C_PCA_CON_SI); 143 sta &= ~(I2C_PCA_CON_STO|I2C_PCA_CON_STA|I2C_PCA_CON_SI);
132 pca_set_con(adap, sta); 144 pca_set_con(adap, sta);
133 145
134 pca_wait(adap); 146 return pca_wait(adap);
135} 147}
136 148
137/* 149/*
@@ -151,7 +163,7 @@ static void pca_rx_byte(struct i2c_algo_pca_data *adap,
151 * 163 *
152 * Returns after next byte has arrived. 164 * Returns after next byte has arrived.
153 */ 165 */
154static void pca_rx_ack(struct i2c_algo_pca_data *adap, 166static int pca_rx_ack(struct i2c_algo_pca_data *adap,
155 int ack) 167 int ack)
156{ 168{
157 int sta = pca_get_con(adap); 169 int sta = pca_get_con(adap);
@@ -162,7 +174,7 @@ static void pca_rx_ack(struct i2c_algo_pca_data *adap,
162 sta |= I2C_PCA_CON_AA; 174 sta |= I2C_PCA_CON_AA;
163 175
164 pca_set_con(adap, sta); 176 pca_set_con(adap, sta);
165 pca_wait(adap); 177 return pca_wait(adap);
166} 178}
167 179
168static int pca_xfer(struct i2c_adapter *i2c_adap, 180static int pca_xfer(struct i2c_adapter *i2c_adap,
@@ -175,14 +187,17 @@ static int pca_xfer(struct i2c_adapter *i2c_adap,
175 int numbytes = 0; 187 int numbytes = 0;
176 int state; 188 int state;
177 int ret; 189 int ret;
178 int timeout = i2c_adap->timeout; 190 int completed = 1;
179 191 unsigned long timeout = jiffies + i2c_adap->timeout;
180 while ((state = pca_status(adap)) != 0xf8 && timeout--) { 192
181 msleep(10); 193 while (pca_status(adap) != 0xf8) {
182 } 194 if (time_before(jiffies, timeout)) {
183 if (state != 0xf8) { 195 msleep(10);
184 dev_dbg(&i2c_adap->dev, "bus is not idle. status is %#04x\n", state); 196 } else {
185 return -EAGAIN; 197 dev_dbg(&i2c_adap->dev, "bus is not idle. status is "
198 "%#04x\n", state);
199 return -EAGAIN;
200 }
186 } 201 }
187 202
188 DEB1("{{{ XFER %d messages\n", num); 203 DEB1("{{{ XFER %d messages\n", num);
@@ -218,18 +233,19 @@ static int pca_xfer(struct i2c_adapter *i2c_adap,
218 233
219 switch (state) { 234 switch (state) {
220 case 0xf8: /* On reset or stop the bus is idle */ 235 case 0xf8: /* On reset or stop the bus is idle */
221 pca_start(adap); 236 completed = pca_start(adap);
222 break; 237 break;
223 238
224 case 0x08: /* A START condition has been transmitted */ 239 case 0x08: /* A START condition has been transmitted */
225 case 0x10: /* A repeated start condition has been transmitted */ 240 case 0x10: /* A repeated start condition has been transmitted */
226 pca_address(adap, msg); 241 completed = pca_address(adap, msg);
227 break; 242 break;
228 243
229 case 0x18: /* SLA+W has been transmitted; ACK has been received */ 244 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 */ 245 case 0x28: /* Data byte in I2CDAT has been transmitted; ACK has been received */
231 if (numbytes < msg->len) { 246 if (numbytes < msg->len) {
232 pca_tx_byte(adap, msg->buf[numbytes]); 247 completed = pca_tx_byte(adap,
248 msg->buf[numbytes]);
233 numbytes++; 249 numbytes++;
234 break; 250 break;
235 } 251 }
@@ -237,7 +253,7 @@ static int pca_xfer(struct i2c_adapter *i2c_adap,
237 if (curmsg == num) 253 if (curmsg == num)
238 pca_stop(adap); 254 pca_stop(adap);
239 else 255 else
240 pca_repeated_start(adap); 256 completed = pca_repeated_start(adap);
241 break; 257 break;
242 258
243 case 0x20: /* SLA+W has been transmitted; NOT ACK has been received */ 259 case 0x20: /* SLA+W has been transmitted; NOT ACK has been received */
@@ -246,21 +262,22 @@ static int pca_xfer(struct i2c_adapter *i2c_adap,
246 goto out; 262 goto out;
247 263
248 case 0x40: /* SLA+R has been transmitted; ACK has been received */ 264 case 0x40: /* SLA+R has been transmitted; ACK has been received */
249 pca_rx_ack(adap, msg->len > 1); 265 completed = pca_rx_ack(adap, msg->len > 1);
250 break; 266 break;
251 267
252 case 0x50: /* Data bytes has been received; ACK has been returned */ 268 case 0x50: /* Data bytes has been received; ACK has been returned */
253 if (numbytes < msg->len) { 269 if (numbytes < msg->len) {
254 pca_rx_byte(adap, &msg->buf[numbytes], 1); 270 pca_rx_byte(adap, &msg->buf[numbytes], 1);
255 numbytes++; 271 numbytes++;
256 pca_rx_ack(adap, numbytes < msg->len - 1); 272 completed = pca_rx_ack(adap,
273 numbytes < msg->len - 1);
257 break; 274 break;
258 } 275 }
259 curmsg++; numbytes = 0; 276 curmsg++; numbytes = 0;
260 if (curmsg == num) 277 if (curmsg == num)
261 pca_stop(adap); 278 pca_stop(adap);
262 else 279 else
263 pca_repeated_start(adap); 280 completed = pca_repeated_start(adap);
264 break; 281 break;
265 282
266 case 0x48: /* SLA+R has been transmitted; NOT ACK has been received */ 283 case 0x48: /* SLA+R has been transmitted; NOT ACK has been received */
@@ -283,7 +300,7 @@ static int pca_xfer(struct i2c_adapter *i2c_adap,
283 if (curmsg == num) 300 if (curmsg == num)
284 pca_stop(adap); 301 pca_stop(adap);
285 else 302 else
286 pca_repeated_start(adap); 303 completed = pca_repeated_start(adap);
287 } else { 304 } else {
288 DEB2("NOT ACK sent after data byte received. " 305 DEB2("NOT ACK sent after data byte received. "
289 "Not final byte. numbytes %d. len %d\n", 306 "Not final byte. numbytes %d. len %d\n",
@@ -309,11 +326,13 @@ static int pca_xfer(struct i2c_adapter *i2c_adap,
309 break; 326 break;
310 } 327 }
311 328
329 if (!completed)
330 goto out;
312 } 331 }
313 332
314 ret = curmsg; 333 ret = curmsg;
315 out: 334 out:
316 DEB1(KERN_CRIT "}}} transfered %d/%d messages. " 335 DEB1("}}} transfered %d/%d messages. "
317 "status is %#04x. control is %#04x\n", 336 "status is %#04x. control is %#04x\n",
318 curmsg, num, pca_status(adap), 337 curmsg, num, pca_status(adap),
319 pca_get_con(adap)); 338 pca_get_con(adap));
@@ -330,26 +349,171 @@ static const struct i2c_algorithm pca_algo = {
330 .functionality = pca_func, 349 .functionality = pca_func,
331}; 350};
332 351
333static int pca_init(struct i2c_adapter *adap) 352static unsigned int pca_probe_chip(struct i2c_adapter *adap)
334{ 353{
335 static int freqs[] = {330,288,217,146,88,59,44,36};
336 int clock;
337 struct i2c_algo_pca_data *pca_data = adap->algo_data; 354 struct i2c_algo_pca_data *pca_data = adap->algo_data;
338 355 /* The trick here is to check if there is an indirect register
339 if (pca_data->i2c_clock > 7) { 356 * available. If there is one, we will read the value we first
340 printk(KERN_WARNING "%s: Invalid I2C clock speed selected. Trying default.\n", 357 * wrote on I2C_PCA_IADR. Otherwise, we will read the last value
341 adap->name); 358 * we wrote on I2C_PCA_ADR
342 pca_data->i2c_clock = I2C_PCA_CON_59kHz; 359 */
360 pca_outw(pca_data, I2C_PCA_INDPTR, I2C_PCA_IADR);
361 pca_outw(pca_data, I2C_PCA_IND, 0xAA);
362 pca_outw(pca_data, I2C_PCA_INDPTR, I2C_PCA_ITO);
363 pca_outw(pca_data, I2C_PCA_IND, 0x00);
364 pca_outw(pca_data, I2C_PCA_INDPTR, I2C_PCA_IADR);
365 if (pca_inw(pca_data, I2C_PCA_IND) == 0xAA) {
366 printk(KERN_INFO "%s: PCA9665 detected.\n", adap->name);
367 return I2C_PCA_CHIP_9665;
368 } else {
369 printk(KERN_INFO "%s: PCA9564 detected.\n", adap->name);
370 return I2C_PCA_CHIP_9564;
343 } 371 }
372}
373
374static int pca_init(struct i2c_adapter *adap)
375{
376 struct i2c_algo_pca_data *pca_data = adap->algo_data;
344 377
345 adap->algo = &pca_algo; 378 adap->algo = &pca_algo;
346 379
347 pca_reset(pca_data); 380 if (pca_probe_chip(adap) == I2C_PCA_CHIP_9564) {
381 static int freqs[] = {330, 288, 217, 146, 88, 59, 44, 36};
382 int clock;
383
384 if (pca_data->i2c_clock > 7) {
385 switch (pca_data->i2c_clock) {
386 case 330000:
387 pca_data->i2c_clock = I2C_PCA_CON_330kHz;
388 break;
389 case 288000:
390 pca_data->i2c_clock = I2C_PCA_CON_288kHz;
391 break;
392 case 217000:
393 pca_data->i2c_clock = I2C_PCA_CON_217kHz;
394 break;
395 case 146000:
396 pca_data->i2c_clock = I2C_PCA_CON_146kHz;
397 break;
398 case 88000:
399 pca_data->i2c_clock = I2C_PCA_CON_88kHz;
400 break;
401 case 59000:
402 pca_data->i2c_clock = I2C_PCA_CON_59kHz;
403 break;
404 case 44000:
405 pca_data->i2c_clock = I2C_PCA_CON_44kHz;
406 break;
407 case 36000:
408 pca_data->i2c_clock = I2C_PCA_CON_36kHz;
409 break;
410 default:
411 printk(KERN_WARNING
412 "%s: Invalid I2C clock speed selected."
413 " Using default 59kHz.\n", adap->name);
414 pca_data->i2c_clock = I2C_PCA_CON_59kHz;
415 }
416 } else {
417 printk(KERN_WARNING "%s: "
418 "Choosing the clock frequency based on "
419 "index is deprecated."
420 " Use the nominal frequency.\n", adap->name);
421 }
422
423 pca_reset(pca_data);
424
425 clock = pca_clock(pca_data);
426 printk(KERN_INFO "%s: Clock frequency is %dkHz\n",
427 adap->name, freqs[clock]);
428
429 pca_set_con(pca_data, I2C_PCA_CON_ENSIO | clock);
430 } else {
431 int clock;
432 int mode;
433 int tlow, thi;
434 /* Values can be found on PCA9665 datasheet section 7.3.2.6 */
435 int min_tlow, min_thi;
436 /* These values are the maximum raise and fall values allowed
437 * by the I2C operation mode (Standard, Fast or Fast+)
438 * They are used (added) below to calculate the clock dividers
439 * of PCA9665. Note that they are slightly different of the
440 * real maximum, to allow the change on mode exactly on the
441 * maximum clock rate for each mode
442 */
443 int raise_fall_time;
444
445 struct i2c_algo_pca_data *pca_data = adap->algo_data;
446
447 /* Ignore the reset function from the module,
448 * we can use the parallel bus reset
449 */
450 pca_data->reset_chip = pca9665_reset;
451
452 if (pca_data->i2c_clock > 1265800) {
453 printk(KERN_WARNING "%s: I2C clock speed too high."
454 " Using 1265.8kHz.\n", adap->name);
455 pca_data->i2c_clock = 1265800;
456 }
457
458 if (pca_data->i2c_clock < 60300) {
459 printk(KERN_WARNING "%s: I2C clock speed too low."
460 " Using 60.3kHz.\n", adap->name);
461 pca_data->i2c_clock = 60300;
462 }
348 463
349 clock = pca_clock(pca_data); 464 /* To avoid integer overflow, use clock/100 for calculations */
350 DEB1(KERN_INFO "%s: Clock frequency is %dkHz\n", adap->name, freqs[clock]); 465 clock = pca_clock(pca_data) / 100;
466
467 if (pca_data->i2c_clock > 10000) {
468 mode = I2C_PCA_MODE_TURBO;
469 min_tlow = 14;
470 min_thi = 5;
471 raise_fall_time = 22; /* Raise 11e-8s, Fall 11e-8s */
472 } else if (pca_data->i2c_clock > 4000) {
473 mode = I2C_PCA_MODE_FASTP;
474 min_tlow = 17;
475 min_thi = 9;
476 raise_fall_time = 22; /* Raise 11e-8s, Fall 11e-8s */
477 } else if (pca_data->i2c_clock > 1000) {
478 mode = I2C_PCA_MODE_FAST;
479 min_tlow = 44;
480 min_thi = 20;
481 raise_fall_time = 58; /* Raise 29e-8s, Fall 29e-8s */
482 } else {
483 mode = I2C_PCA_MODE_STD;
484 min_tlow = 157;
485 min_thi = 134;
486 raise_fall_time = 127; /* Raise 29e-8s, Fall 98e-8s */
487 }
488
489 /* The minimum clock that respects the thi/tlow = 134/157 is
490 * 64800 Hz. Below that, we have to fix the tlow to 255 and
491 * calculate the thi factor.
492 */
493 if (clock < 648) {
494 tlow = 255;
495 thi = 1000000 - clock * raise_fall_time;
496 thi /= (I2C_PCA_OSC_PER * clock) - tlow;
497 } else {
498 tlow = (1000000 - clock * raise_fall_time) * min_tlow;
499 tlow /= I2C_PCA_OSC_PER * clock * (min_thi + min_tlow);
500 thi = tlow * min_thi / min_tlow;
501 }
351 502
352 pca_set_con(pca_data, I2C_PCA_CON_ENSIO | clock); 503 pca_reset(pca_data);
504
505 printk(KERN_INFO
506 "%s: Clock frequency is %dHz\n", adap->name, clock * 100);
507
508 pca_outw(pca_data, I2C_PCA_INDPTR, I2C_PCA_IMODE);
509 pca_outw(pca_data, I2C_PCA_IND, mode);
510 pca_outw(pca_data, I2C_PCA_INDPTR, I2C_PCA_ISCLL);
511 pca_outw(pca_data, I2C_PCA_IND, tlow);
512 pca_outw(pca_data, I2C_PCA_INDPTR, I2C_PCA_ISCLH);
513 pca_outw(pca_data, I2C_PCA_IND, thi);
514
515 pca_set_con(pca_data, I2C_PCA_CON_ENSIO);
516 }
353 udelay(500); /* 500 us for oscilator to stabilise */ 517 udelay(500); /* 500 us for oscilator to stabilise */
354 518
355 return 0; 519 return 0;
@@ -384,7 +548,7 @@ EXPORT_SYMBOL(i2c_pca_add_numbered_bus);
384 548
385MODULE_AUTHOR("Ian Campbell <icampbell@arcom.com>, " 549MODULE_AUTHOR("Ian Campbell <icampbell@arcom.com>, "
386 "Wolfram Sang <w.sang@pengutronix.de>"); 550 "Wolfram Sang <w.sang@pengutronix.de>");
387MODULE_DESCRIPTION("I2C-Bus PCA9564 algorithm"); 551MODULE_DESCRIPTION("I2C-Bus PCA9564/PCA9665 algorithm");
388MODULE_LICENSE("GPL"); 552MODULE_LICENSE("GPL");
389 553
390module_param(i2c_debug, int, 0); 554module_param(i2c_debug, int, 0);