Skip to content

Commit eff9ec9

Browse files
Marco Aurelio da CostaJean Delvare
authored andcommitted
i2c-algo-pca: Add PCA9665 support
Add support for the PCA9665 I2C controller. Signed-off-by: Wolfram Sang <w.sang@pengutronix.de> Signed-off-by: Jean Delvare <khali@linux-fr.org>
1 parent bac3e7c commit eff9ec9

5 files changed

Lines changed: 216 additions & 28 deletions

File tree

‎drivers/i2c/algos/i2c-algo-pca.c‎

Lines changed: 166 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ static int i2c_debug;
4646
#define pca_wait(adap) adap->wait_for_completion(adap->data)
4747
#define pca_reset(adap) adap->reset_chip(adap->data)
4848

49+
static void pca9665_reset(void *pd)
50+
{
51+
struct i2c_algo_pca_data *adap = pd;
52+
pca_outw(adap, I2C_PCA_INDPTR, I2C_PCA_IPRESET);
53+
pca_outw(adap, I2C_PCA_IND, 0xA5);
54+
pca_outw(adap, I2C_PCA_IND, 0x5A);
55+
}
56+
4957
/*
5058
* Generate a start condition on the i2c bus.
5159
*
@@ -333,27 +341,171 @@ static const struct i2c_algorithm pca_algo = {
333341
.functionality = pca_func,
334342
};
335343

336-
static int pca_init(struct i2c_adapter *adap)
344+
static unsigned int pca_probe_chip(struct i2c_adapter *adap)
337345
{
338-
static int freqs[] = {330,288,217,146,88,59,44,36};
339-
int clock;
340346
struct i2c_algo_pca_data *pca_data = adap->algo_data;
341-
342-
if (pca_data->i2c_clock > 7) {
343-
printk(KERN_WARNING "%s: Invalid I2C clock speed selected. Trying default.\n",
344-
adap->name);
345-
pca_data->i2c_clock = I2C_PCA_CON_59kHz;
347+
/* The trick here is to check if there is an indirect register
348+
* available. If there is one, we will read the value we first
349+
* wrote on I2C_PCA_IADR. Otherwise, we will read the last value
350+
* we wrote on I2C_PCA_ADR
351+
*/
352+
pca_outw(pca_data, I2C_PCA_INDPTR, I2C_PCA_IADR);
353+
pca_outw(pca_data, I2C_PCA_IND, 0xAA);
354+
pca_outw(pca_data, I2C_PCA_INDPTR, I2C_PCA_ITO);
355+
pca_outw(pca_data, I2C_PCA_IND, 0x00);
356+
pca_outw(pca_data, I2C_PCA_INDPTR, I2C_PCA_IADR);
357+
if (pca_inw(pca_data, I2C_PCA_IND) == 0xAA) {
358+
printk(KERN_INFO "%s: PCA9665 detected.\n", adap->name);
359+
return I2C_PCA_CHIP_9665;
360+
} else {
361+
printk(KERN_INFO "%s: PCA9564 detected.\n", adap->name);
362+
return I2C_PCA_CHIP_9564;
346363
}
364+
}
365+
366+
static int pca_init(struct i2c_adapter *adap)
367+
{
368+
struct i2c_algo_pca_data *pca_data = adap->algo_data;
347369

348370
adap->algo = &pca_algo;
349371

350-
pca_reset(pca_data);
372+
if (pca_probe_chip(adap) == I2C_PCA_CHIP_9564) {
373+
static int freqs[] = {330, 288, 217, 146, 88, 59, 44, 36};
374+
int clock;
375+
376+
if (pca_data->i2c_clock > 7) {
377+
switch (pca_data->i2c_clock) {
378+
case 330000:
379+
pca_data->i2c_clock = I2C_PCA_CON_330kHz;
380+
break;
381+
case 288000:
382+
pca_data->i2c_clock = I2C_PCA_CON_288kHz;
383+
break;
384+
case 217000:
385+
pca_data->i2c_clock = I2C_PCA_CON_217kHz;
386+
break;
387+
case 146000:
388+
pca_data->i2c_clock = I2C_PCA_CON_146kHz;
389+
break;
390+
case 88000:
391+
pca_data->i2c_clock = I2C_PCA_CON_88kHz;
392+
break;
393+
case 59000:
394+
pca_data->i2c_clock = I2C_PCA_CON_59kHz;
395+
break;
396+
case 44000:
397+
pca_data->i2c_clock = I2C_PCA_CON_44kHz;
398+
break;
399+
case 36000:
400+
pca_data->i2c_clock = I2C_PCA_CON_36kHz;
401+
break;
402+
default:
403+
printk(KERN_WARNING
404+
"%s: Invalid I2C clock speed selected."
405+
" Using default 59kHz.\n", adap->name);
406+
pca_data->i2c_clock = I2C_PCA_CON_59kHz;
407+
}
408+
} else {
409+
printk(KERN_WARNING "%s: "
410+
"Choosing the clock frequency based on "
411+
"index is deprecated."
412+
" Use the nominal frequency.\n", adap->name);
413+
}
414+
415+
pca_reset(pca_data);
416+
417+
clock = pca_clock(pca_data);
418+
printk(KERN_INFO "%s: Clock frequency is %dkHz\n",
419+
adap->name, freqs[clock]);
420+
421+
pca_set_con(pca_data, I2C_PCA_CON_ENSIO | clock);
422+
} else {
423+
int clock;
424+
int mode;
425+
int tlow, thi;
426+
/* Values can be found on PCA9665 datasheet section 7.3.2.6 */
427+
int min_tlow, min_thi;
428+
/* These values are the maximum raise and fall values allowed
429+
* by the I2C operation mode (Standard, Fast or Fast+)
430+
* They are used (added) below to calculate the clock dividers
431+
* of PCA9665. Note that they are slightly different of the
432+
* real maximum, to allow the change on mode exactly on the
433+
* maximum clock rate for each mode
434+
*/
435+
int raise_fall_time;
436+
437+
struct i2c_algo_pca_data *pca_data = adap->algo_data;
438+
439+
/* Ignore the reset function from the module,
440+
* we can use the parallel bus reset
441+
*/
442+
pca_data->reset_chip = pca9665_reset;
443+
444+
if (pca_data->i2c_clock > 1265800) {
445+
printk(KERN_WARNING "%s: I2C clock speed too high."
446+
" Using 1265.8kHz.\n", adap->name);
447+
pca_data->i2c_clock = 1265800;
448+
}
449+
450+
if (pca_data->i2c_clock < 60300) {
451+
printk(KERN_WARNING "%s: I2C clock speed too low."
452+
" Using 60.3kHz.\n", adap->name);
453+
pca_data->i2c_clock = 60300;
454+
}
455+
456+
/* To avoid integer overflow, use clock/100 for calculations */
457+
clock = pca_clock(pca_data) / 100;
458+
459+
if (pca_data->i2c_clock > 10000) {
460+
mode = I2C_PCA_MODE_TURBO;
461+
min_tlow = 14;
462+
min_thi = 5;
463+
raise_fall_time = 22; /* Raise 11e-8s, Fall 11e-8s */
464+
} else if (pca_data->i2c_clock > 4000) {
465+
mode = I2C_PCA_MODE_FASTP;
466+
min_tlow = 17;
467+
min_thi = 9;
468+
raise_fall_time = 22; /* Raise 11e-8s, Fall 11e-8s */
469+
} else if (pca_data->i2c_clock > 1000) {
470+
mode = I2C_PCA_MODE_FAST;
471+
min_tlow = 44;
472+
min_thi = 20;
473+
raise_fall_time = 58; /* Raise 29e-8s, Fall 29e-8s */
474+
} else {
475+
mode = I2C_PCA_MODE_STD;
476+
min_tlow = 157;
477+
min_thi = 134;
478+
raise_fall_time = 127; /* Raise 29e-8s, Fall 98e-8s */
479+
}
480+
481+
/* The minimum clock that respects the thi/tlow = 134/157 is
482+
* 64800 Hz. Below that, we have to fix the tlow to 255 and
483+
* calculate the thi factor.
484+
*/
485+
if (clock < 648) {
486+
tlow = 255;
487+
thi = 1000000 - clock * raise_fall_time;
488+
thi /= (I2C_PCA_OSC_PER * clock) - tlow;
489+
} else {
490+
tlow = (1000000 - clock * raise_fall_time) * min_tlow;
491+
tlow /= I2C_PCA_OSC_PER * clock * (min_thi + min_tlow);
492+
thi = tlow * min_thi / min_tlow;
493+
}
494+
495+
pca_reset(pca_data);
351496

352-
clock = pca_clock(pca_data);
353-
printk(KERN_INFO "%s: Clock frequency is %dkHz\n", adap->name,
354-
freqs[clock]);
497+
printk(KERN_INFO
498+
"%s: Clock frequency is %dHz\n", adap->name, clock * 100);
355499

356-
pca_set_con(pca_data, I2C_PCA_CON_ENSIO | clock);
500+
pca_outw(pca_data, I2C_PCA_INDPTR, I2C_PCA_IMODE);
501+
pca_outw(pca_data, I2C_PCA_IND, mode);
502+
pca_outw(pca_data, I2C_PCA_INDPTR, I2C_PCA_ISCLL);
503+
pca_outw(pca_data, I2C_PCA_IND, tlow);
504+
pca_outw(pca_data, I2C_PCA_INDPTR, I2C_PCA_ISCLH);
505+
pca_outw(pca_data, I2C_PCA_IND, thi);
506+
507+
pca_set_con(pca_data, I2C_PCA_CON_ENSIO);
508+
}
357509
udelay(500); /* 500 us for oscilator to stabilise */
358510

359511
return 0;
@@ -388,7 +540,7 @@ EXPORT_SYMBOL(i2c_pca_add_numbered_bus);
388540

389541
MODULE_AUTHOR("Ian Campbell <icampbell@arcom.com>, "
390542
"Wolfram Sang <w.sang@pengutronix.de>");
391-
MODULE_DESCRIPTION("I2C-Bus PCA9564 algorithm");
543+
MODULE_DESCRIPTION("I2C-Bus PCA9564/PCA9665 algorithm");
392544
MODULE_LICENSE("GPL");
393545

394546
module_param(i2c_debug, int, 0);

‎drivers/i2c/busses/Kconfig‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -617,12 +617,12 @@ config I2C_ELEKTOR
617617
will be called i2c-elektor.
618618

619619
config I2C_PCA_ISA
620-
tristate "PCA9564 on an ISA bus"
620+
tristate "PCA9564/PCA9665 on an ISA bus"
621621
depends on ISA
622622
select I2C_ALGOPCA
623623
default n
624624
help
625-
This driver supports ISA boards using the Philips PCA9564
625+
This driver supports ISA boards using the Philips PCA9564/PCA9665
626626
parallel bus to I2C bus controller.
627627

628628
This driver can also be built as a module. If so, the module
@@ -634,11 +634,11 @@ config I2C_PCA_ISA
634634
time). If unsure, say N.
635635

636636
config I2C_PCA_PLATFORM
637-
tristate "PCA9564 as platform device"
637+
tristate "PCA9564/PCA9665 as platform device"
638638
select I2C_ALGOPCA
639639
default n
640640
help
641-
This driver supports a memory mapped Philips PCA9564
641+
This driver supports a memory mapped Philips PCA9564/PCA9665
642642
parallel bus to I2C bus controller.
643643

644644
This driver can also be built as a module. If so, the module

‎drivers/i2c/busses/i2c-pca-isa.c‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ static int irq = -1;
4141

4242
/* Data sheet recommends 59kHz for 100kHz operation due to variation
4343
* in the actual clock rate */
44-
static int clock = I2C_PCA_CON_59kHz;
44+
static int clock = 59000;
4545

4646
static wait_queue_head_t pca_wait;
4747

@@ -103,7 +103,7 @@ static struct i2c_algo_pca_data pca_isa_data = {
103103
static struct i2c_adapter pca_isa_ops = {
104104
.owner = THIS_MODULE,
105105
.algo_data = &pca_isa_data,
106-
.name = "PCA9564 ISA Adapter",
106+
.name = "PCA9564/PCA9665 ISA Adapter",
107107
.timeout = 100,
108108
};
109109

@@ -196,7 +196,7 @@ static void __exit pca_isa_exit(void)
196196
}
197197

198198
MODULE_AUTHOR("Ian Campbell <icampbell@arcom.com>");
199-
MODULE_DESCRIPTION("ISA base PCA9564 driver");
199+
MODULE_DESCRIPTION("ISA base PCA9564/PCA9665 driver");
200200
MODULE_LICENSE("GPL");
201201

202202
module_param(base, ulong, 0);
@@ -205,7 +205,13 @@ MODULE_PARM_DESC(base, "I/O base address");
205205
module_param(irq, int, 0);
206206
MODULE_PARM_DESC(irq, "IRQ");
207207
module_param(clock, int, 0);
208-
MODULE_PARM_DESC(clock, "Clock rate as described in table 1 of PCA9564 datasheet");
208+
MODULE_PARM_DESC(clock, "Clock rate in hertz.\n\t\t"
209+
"For PCA9564: 330000,288000,217000,146000,"
210+
"88000,59000,44000,36000\n"
211+
"\t\tFor PCA9665:\tStandard: 60300 - 100099\n"
212+
"\t\t\t\tFast: 100100 - 400099\n"
213+
"\t\t\t\tFast+: 400100 - 10000099\n"
214+
"\t\t\t\tTurbo: Up to 1265800");
209215

210216
module_init(pca_isa_init);
211217
module_exit(pca_isa_exit);

‎drivers/i2c/busses/i2c-pca-platform.c‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,9 @@ static int __devinit i2c_pca_pf_probe(struct platform_device *pdev)
172172

173173
i2c->adap.nr = pdev->id >= 0 ? pdev->id : 0;
174174
i2c->adap.owner = THIS_MODULE;
175-
snprintf(i2c->adap.name, sizeof(i2c->adap.name), "PCA9564 at 0x%08lx",
176-
(unsigned long) res->start);
175+
snprintf(i2c->adap.name, sizeof(i2c->adap.name),
176+
"PCA9564/PCA9665 at 0x%08lx",
177+
(unsigned long) res->start);
177178
i2c->adap.algo_data = &i2c->algo_data;
178179
i2c->adap.dev.parent = &pdev->dev;
179180
i2c->adap.timeout = platform_data->timeout;
@@ -246,7 +247,7 @@ static int __devinit i2c_pca_pf_probe(struct platform_device *pdev)
246247
e_alloc:
247248
release_mem_region(res->start, res_len(res));
248249
e_print:
249-
printk(KERN_ERR "Registering PCA9564 FAILED! (%d)\n", ret);
250+
printk(KERN_ERR "Registering PCA9564/PCA9665 FAILED! (%d)\n", ret);
250251
return ret;
251252
}
252253

@@ -290,7 +291,7 @@ static void __exit i2c_pca_pf_exit(void)
290291
}
291292

292293
MODULE_AUTHOR("Wolfram Sang <w.sang@pengutronix.de>");
293-
MODULE_DESCRIPTION("I2C-PCA9564 platform driver");
294+
MODULE_DESCRIPTION("I2C-PCA9564/PCA9665 platform driver");
294295
MODULE_LICENSE("GPL");
295296

296297
module_init(i2c_pca_pf_init);

‎include/linux/i2c-algo-pca.h‎

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
#ifndef _LINUX_I2C_ALGO_PCA_H
22
#define _LINUX_I2C_ALGO_PCA_H
33

4-
/* Clock speeds for the bus */
4+
/* Chips known to the pca algo */
5+
#define I2C_PCA_CHIP_9564 0x00
6+
#define I2C_PCA_CHIP_9665 0x01
7+
8+
/* Internal period for PCA9665 oscilator */
9+
#define I2C_PCA_OSC_PER 3 /* e10-8s */
10+
11+
/* Clock speeds for the bus for PCA9564*/
512
#define I2C_PCA_CON_330kHz 0x00
613
#define I2C_PCA_CON_288kHz 0x01
714
#define I2C_PCA_CON_217kHz 0x02
@@ -18,6 +25,26 @@
1825
#define I2C_PCA_ADR 0x02 /* OWN ADR Read/Write */
1926
#define I2C_PCA_CON 0x03 /* CONTROL Read/Write */
2027

28+
/* PCA9665 registers */
29+
#define I2C_PCA_INDPTR 0x00 /* INDIRECT Pointer Write Only */
30+
#define I2C_PCA_IND 0x02 /* INDIRECT Read/Write */
31+
32+
/* PCA9665 indirect registers */
33+
#define I2C_PCA_ICOUNT 0x00 /* Byte Count for buffered mode */
34+
#define I2C_PCA_IADR 0x01 /* OWN ADR */
35+
#define I2C_PCA_ISCLL 0x02 /* SCL LOW period */
36+
#define I2C_PCA_ISCLH 0x03 /* SCL HIGH period */
37+
#define I2C_PCA_ITO 0x04 /* TIMEOUT */
38+
#define I2C_PCA_IPRESET 0x05 /* Parallel bus reset */
39+
#define I2C_PCA_IMODE 0x06 /* I2C Bus mode */
40+
41+
/* PCA9665 I2C bus mode */
42+
#define I2C_PCA_MODE_STD 0x00 /* Standard mode */
43+
#define I2C_PCA_MODE_FAST 0x01 /* Fast mode */
44+
#define I2C_PCA_MODE_FASTP 0x02 /* Fast Plus mode */
45+
#define I2C_PCA_MODE_TURBO 0x03 /* Turbo mode */
46+
47+
2148
#define I2C_PCA_CON_AA 0x80 /* Assert Acknowledge */
2249
#define I2C_PCA_CON_ENSIO 0x40 /* Enable */
2350
#define I2C_PCA_CON_STA 0x20 /* Start */
@@ -31,7 +58,9 @@ struct i2c_algo_pca_data {
3158
int (*read_byte) (void *data, int reg);
3259
int (*wait_for_completion) (void *data);
3360
void (*reset_chip) (void *data);
34-
/* i2c_clock values are defined in linux/i2c-algo-pca.h */
61+
/* For PCA9564, use one of the predefined frequencies:
62+
* 330000, 288000, 217000, 146000, 88000, 59000, 44000, 36000
63+
* For PCA9665, use the frequency you want here. */
3564
unsigned int i2c_clock;
3665
};
3766

0 commit comments

Comments
 (0)