aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/i2c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/i2c')
-rw-r--r--drivers/i2c/algos/Kconfig4
-rw-r--r--drivers/i2c/busses/Kconfig33
-rw-r--r--drivers/i2c/busses/Makefile3
-rw-r--r--drivers/i2c/busses/i2c-acorn.c2
-rw-r--r--drivers/i2c/busses/i2c-gpio.c12
-rw-r--r--drivers/i2c/busses/i2c-i801.c249
-rw-r--r--drivers/i2c/busses/i2c-iop3xx.c3
-rw-r--r--drivers/i2c/busses/i2c-mpc.c26
-rw-r--r--drivers/i2c/busses/i2c-mv64xxx.c3
-rw-r--r--drivers/i2c/busses/i2c-nforce2.c44
-rw-r--r--drivers/i2c/busses/i2c-piix4.c4
-rw-r--r--drivers/i2c/busses/i2c-pmcmsp.c653
-rw-r--r--drivers/i2c/busses/i2c-powermac.c3
-rw-r--r--drivers/i2c/busses/i2c-pxa.c9
-rw-r--r--drivers/i2c/busses/i2c-rpx.c101
-rw-r--r--drivers/i2c/busses/i2c-savage4.c21
-rw-r--r--drivers/i2c/busses/i2c-sis5595.c27
-rw-r--r--drivers/i2c/busses/i2c-taos-evm.c330
-rw-r--r--drivers/i2c/busses/i2c-viapro.c5
-rw-r--r--drivers/i2c/busses/scx200_acb.c16
-rw-r--r--drivers/i2c/chips/Kconfig45
-rw-r--r--drivers/i2c/chips/Makefile3
-rw-r--r--drivers/i2c/chips/ds1682.c259
-rw-r--r--drivers/i2c/chips/eeprom.c10
-rw-r--r--drivers/i2c/chips/max6875.c7
-rw-r--r--drivers/i2c/chips/menelaus.c1281
-rw-r--r--drivers/i2c/chips/tsl2550.c460
-rw-r--r--drivers/i2c/i2c-core.c25
-rw-r--r--drivers/i2c/i2c-dev.c9
29 files changed, 3386 insertions, 261 deletions
diff --git a/drivers/i2c/algos/Kconfig b/drivers/i2c/algos/Kconfig
index 58899078810b..014dfa575be7 100644
--- a/drivers/i2c/algos/Kconfig
+++ b/drivers/i2c/algos/Kconfig
@@ -34,10 +34,6 @@ config I2C_ALGOPCA
34 This support is also available as a module. If so, the module 34 This support is also available as a module. If so, the module
35 will be called i2c-algo-pca. 35 will be called i2c-algo-pca.
36 36
37config I2C_ALGO8XX
38 tristate "MPC8xx CPM I2C interface"
39 depends on 8xx
40
41config I2C_ALGO_SGI 37config I2C_ALGO_SGI
42 tristate "I2C SGI interfaces" 38 tristate "I2C SGI interfaces"
43 depends on SGI_IP22 || SGI_IP32 || X86_VISWS 39 depends on SGI_IP22 || SGI_IP32 || X86_VISWS
diff --git a/drivers/i2c/busses/Kconfig b/drivers/i2c/busses/Kconfig
index c63bfa68e144..da1647869f91 100644
--- a/drivers/i2c/busses/Kconfig
+++ b/drivers/i2c/busses/Kconfig
@@ -207,6 +207,7 @@ config I2C_PIIX4
207 ATI IXP300 207 ATI IXP300
208 ATI IXP400 208 ATI IXP400
209 ATI SB600 209 ATI SB600
210 ATI SB700
210 Serverworks OSB4 211 Serverworks OSB4
211 Serverworks CSB5 212 Serverworks CSB5
212 Serverworks CSB6 213 Serverworks CSB6
@@ -387,11 +388,6 @@ config I2C_PROSAVAGE
387 This support is also available as a module. If so, the module 388 This support is also available as a module. If so, the module
388 will be called i2c-prosavage. 389 will be called i2c-prosavage.
389 390
390config I2C_RPXLITE
391 tristate "Embedded Planet RPX Lite/Classic support"
392 depends on RPXLITE || RPXCLASSIC
393 select I2C_ALGO8XX
394
395config I2C_S3C2410 391config I2C_S3C2410
396 tristate "S3C2410 I2C Driver" 392 tristate "S3C2410 I2C Driver"
397 depends on ARCH_S3C2410 393 depends on ARCH_S3C2410
@@ -509,6 +505,22 @@ config I2C_SIS96X
509 This driver can also be built as a module. If so, the module 505 This driver can also be built as a module. If so, the module
510 will be called i2c-sis96x. 506 will be called i2c-sis96x.
511 507
508config I2C_TAOS_EVM
509 tristate "TAOS evaluation module"
510 depends on EXPERIMENTAL
511 select SERIO
512 select SERIO_SERPORT
513 default n
514 help
515 This supports TAOS evaluation modules on serial port. In order to
516 use this driver, you will need the inputattach tool, which is part
517 of the input-utils package.
518
519 If unsure, say N.
520
521 This support is also available as a module. If so, the module
522 will be called i2c-taos-evm.
523
512config I2C_STUB 524config I2C_STUB
513 tristate "I2C/SMBus Test Stub" 525 tristate "I2C/SMBus Test Stub"
514 depends on EXPERIMENTAL && m 526 depends on EXPERIMENTAL && m
@@ -545,7 +557,7 @@ config I2C_VERSATILE
545 will be called i2c-versatile. 557 will be called i2c-versatile.
546 558
547config I2C_ACORN 559config I2C_ACORN
548 bool "Acorn IOC/IOMD I2C bus support" 560 tristate "Acorn IOC/IOMD I2C bus support"
549 depends on ARCH_ACORN 561 depends on ARCH_ACORN
550 default y 562 default y
551 select I2C_ALGOBIT 563 select I2C_ALGOBIT
@@ -632,4 +644,13 @@ config I2C_PNX
632 This driver can also be built as a module. If so, the module 644 This driver can also be built as a module. If so, the module
633 will be called i2c-pnx. 645 will be called i2c-pnx.
634 646
647config I2C_PMCMSP
648 tristate "PMC MSP I2C TWI Controller"
649 depends on PMC_MSP
650 help
651 This driver supports the PMC TWI controller on MSP devices.
652
653 This driver can also be built as module. If so, the module
654 will be called i2c-pmcmsp.
655
635endmenu 656endmenu
diff --git a/drivers/i2c/busses/Makefile b/drivers/i2c/busses/Makefile
index b6a8037f1feb..5b752e4e1918 100644
--- a/drivers/i2c/busses/Makefile
+++ b/drivers/i2c/busses/Makefile
@@ -31,10 +31,10 @@ obj-$(CONFIG_I2C_PARPORT_LIGHT) += i2c-parport-light.o
31obj-$(CONFIG_I2C_PASEMI) += i2c-pasemi.o 31obj-$(CONFIG_I2C_PASEMI) += i2c-pasemi.o
32obj-$(CONFIG_I2C_PCA_ISA) += i2c-pca-isa.o 32obj-$(CONFIG_I2C_PCA_ISA) += i2c-pca-isa.o
33obj-$(CONFIG_I2C_PIIX4) += i2c-piix4.o 33obj-$(CONFIG_I2C_PIIX4) += i2c-piix4.o
34obj-$(CONFIG_I2C_PMCMSP) += i2c-pmcmsp.o
34obj-$(CONFIG_I2C_PNX) += i2c-pnx.o 35obj-$(CONFIG_I2C_PNX) += i2c-pnx.o
35obj-$(CONFIG_I2C_PROSAVAGE) += i2c-prosavage.o 36obj-$(CONFIG_I2C_PROSAVAGE) += i2c-prosavage.o
36obj-$(CONFIG_I2C_PXA) += i2c-pxa.o 37obj-$(CONFIG_I2C_PXA) += i2c-pxa.o
37obj-$(CONFIG_I2C_RPXLITE) += i2c-rpx.o
38obj-$(CONFIG_I2C_S3C2410) += i2c-s3c2410.o 38obj-$(CONFIG_I2C_S3C2410) += i2c-s3c2410.o
39obj-$(CONFIG_I2C_SAVAGE4) += i2c-savage4.o 39obj-$(CONFIG_I2C_SAVAGE4) += i2c-savage4.o
40obj-$(CONFIG_I2C_SIBYTE) += i2c-sibyte.o 40obj-$(CONFIG_I2C_SIBYTE) += i2c-sibyte.o
@@ -43,6 +43,7 @@ obj-$(CONFIG_I2C_SIS5595) += i2c-sis5595.o
43obj-$(CONFIG_I2C_SIS630) += i2c-sis630.o 43obj-$(CONFIG_I2C_SIS630) += i2c-sis630.o
44obj-$(CONFIG_I2C_SIS96X) += i2c-sis96x.o 44obj-$(CONFIG_I2C_SIS96X) += i2c-sis96x.o
45obj-$(CONFIG_I2C_STUB) += i2c-stub.o 45obj-$(CONFIG_I2C_STUB) += i2c-stub.o
46obj-$(CONFIG_I2C_TAOS_EVM) += i2c-taos-evm.o
46obj-$(CONFIG_I2C_TINY_USB) += i2c-tiny-usb.o 47obj-$(CONFIG_I2C_TINY_USB) += i2c-tiny-usb.o
47obj-$(CONFIG_I2C_VERSATILE) += i2c-versatile.o 48obj-$(CONFIG_I2C_VERSATILE) += i2c-versatile.o
48obj-$(CONFIG_I2C_ACORN) += i2c-acorn.o 49obj-$(CONFIG_I2C_ACORN) += i2c-acorn.o
diff --git a/drivers/i2c/busses/i2c-acorn.c b/drivers/i2c/busses/i2c-acorn.c
index 09bd7f40b90c..7c2be3558a24 100644
--- a/drivers/i2c/busses/i2c-acorn.c
+++ b/drivers/i2c/busses/i2c-acorn.c
@@ -94,4 +94,4 @@ static int __init i2c_ioc_init(void)
94 return i2c_bit_add_bus(&ioc_ops); 94 return i2c_bit_add_bus(&ioc_ops);
95} 95}
96 96
97__initcall(i2c_ioc_init); 97module_init(i2c_ioc_init);
diff --git a/drivers/i2c/busses/i2c-gpio.c b/drivers/i2c/busses/i2c-gpio.c
index a7dd54654a9a..025f19423faf 100644
--- a/drivers/i2c/busses/i2c-gpio.c
+++ b/drivers/i2c/busses/i2c-gpio.c
@@ -63,14 +63,14 @@ static void i2c_gpio_setscl_val(void *data, int state)
63 gpio_set_value(pdata->scl_pin, state); 63 gpio_set_value(pdata->scl_pin, state);
64} 64}
65 65
66int i2c_gpio_getsda(void *data) 66static int i2c_gpio_getsda(void *data)
67{ 67{
68 struct i2c_gpio_platform_data *pdata = data; 68 struct i2c_gpio_platform_data *pdata = data;
69 69
70 return gpio_get_value(pdata->sda_pin); 70 return gpio_get_value(pdata->sda_pin);
71} 71}
72 72
73int i2c_gpio_getscl(void *data) 73static int i2c_gpio_getscl(void *data)
74{ 74{
75 struct i2c_gpio_platform_data *pdata = data; 75 struct i2c_gpio_platform_data *pdata = data;
76 76
@@ -142,7 +142,13 @@ static int __init i2c_gpio_probe(struct platform_device *pdev)
142 adap->algo_data = bit_data; 142 adap->algo_data = bit_data;
143 adap->dev.parent = &pdev->dev; 143 adap->dev.parent = &pdev->dev;
144 144
145 ret = i2c_bit_add_bus(adap); 145 /*
146 * If "dev->id" is negative we consider it as zero.
147 * The reason to do so is to avoid sysfs names that only make
148 * sense when there are multiple adapters.
149 */
150 adap->nr = pdev->id >= 0 ? pdev->id : 0;
151 ret = i2c_bit_add_numbered_bus(adap);
146 if (ret) 152 if (ret)
147 goto err_add_bus; 153 goto err_add_bus;
148 154
diff --git a/drivers/i2c/busses/i2c-i801.c b/drivers/i2c/busses/i2c-i801.c
index 611b57192c96..8f5c686123b8 100644
--- a/drivers/i2c/busses/i2c-i801.c
+++ b/drivers/i2c/busses/i2c-i801.c
@@ -22,12 +22,12 @@
22 22
23/* 23/*
24 SUPPORTED DEVICES PCI ID 24 SUPPORTED DEVICES PCI ID
25 82801AA 2413 25 82801AA 2413
26 82801AB 2423 26 82801AB 2423
27 82801BA 2443 27 82801BA 2443
28 82801CA/CAM 2483 28 82801CA/CAM 2483
29 82801DB 24C3 (HW PEC supported, 32 byte buffer not supported) 29 82801DB 24C3 (HW PEC supported)
30 82801EB 24D3 (HW PEC supported, 32 byte buffer not supported) 30 82801EB 24D3 (HW PEC supported)
31 6300ESB 25A4 31 6300ESB 25A4
32 ICH6 266A 32 ICH6 266A
33 ICH7 27DA 33 ICH7 27DA
@@ -74,6 +74,13 @@
74#define SMBHSTCFG_SMB_SMI_EN 2 74#define SMBHSTCFG_SMB_SMI_EN 2
75#define SMBHSTCFG_I2C_EN 4 75#define SMBHSTCFG_I2C_EN 4
76 76
77/* Auxillary control register bits, ICH4+ only */
78#define SMBAUXCTL_CRC 1
79#define SMBAUXCTL_E32B 2
80
81/* kill bit for SMBHSTCNT */
82#define SMBHSTCNT_KILL 2
83
77/* Other settings */ 84/* Other settings */
78#define MAX_TIMEOUT 100 85#define MAX_TIMEOUT 100
79#define ENABLE_INT9 0 /* set to 0x01 to enable - untested */ 86#define ENABLE_INT9 0 /* set to 0x01 to enable - untested */
@@ -91,10 +98,15 @@
91#define I801_START 0x40 98#define I801_START 0x40
92#define I801_PEC_EN 0x80 /* ICH4 only */ 99#define I801_PEC_EN 0x80 /* ICH4 only */
93 100
94 101/* I801 Hosts Status register bits */
95static int i801_transaction(void); 102#define SMBHSTSTS_BYTE_DONE 0x80
96static int i801_block_transaction(union i2c_smbus_data *data, char read_write, 103#define SMBHSTSTS_INUSE_STS 0x40
97 int command, int hwpec); 104#define SMBHSTSTS_SMBALERT_STS 0x20
105#define SMBHSTSTS_FAILED 0x10
106#define SMBHSTSTS_BUS_ERR 0x08
107#define SMBHSTSTS_DEV_ERR 0x04
108#define SMBHSTSTS_INTR 0x02
109#define SMBHSTSTS_HOST_BUSY 0x01
98 110
99static unsigned long i801_smba; 111static unsigned long i801_smba;
100static unsigned char i801_original_hstcfg; 112static unsigned char i801_original_hstcfg;
@@ -102,7 +114,7 @@ static struct pci_driver i801_driver;
102static struct pci_dev *I801_dev; 114static struct pci_dev *I801_dev;
103static int isich4; 115static int isich4;
104 116
105static int i801_transaction(void) 117static int i801_transaction(int xact)
106{ 118{
107 int temp; 119 int temp;
108 int result = 0; 120 int result = 0;
@@ -127,33 +139,40 @@ static int i801_transaction(void)
127 } 139 }
128 } 140 }
129 141
130 outb_p(inb(SMBHSTCNT) | I801_START, SMBHSTCNT); 142 /* the current contents of SMBHSTCNT can be overwritten, since PEC,
143 * INTREN, SMBSCMD are passed in xact */
144 outb_p(xact | I801_START, SMBHSTCNT);
131 145
132 /* We will always wait for a fraction of a second! */ 146 /* We will always wait for a fraction of a second! */
133 do { 147 do {
134 msleep(1); 148 msleep(1);
135 temp = inb_p(SMBHSTSTS); 149 temp = inb_p(SMBHSTSTS);
136 } while ((temp & 0x01) && (timeout++ < MAX_TIMEOUT)); 150 } while ((temp & SMBHSTSTS_HOST_BUSY) && (timeout++ < MAX_TIMEOUT));
137 151
138 /* If the SMBus is still busy, we give up */ 152 /* If the SMBus is still busy, we give up */
139 if (timeout >= MAX_TIMEOUT) { 153 if (timeout >= MAX_TIMEOUT) {
140 dev_dbg(&I801_dev->dev, "SMBus Timeout!\n"); 154 dev_dbg(&I801_dev->dev, "SMBus Timeout!\n");
141 result = -1; 155 result = -1;
156 /* try to stop the current command */
157 dev_dbg(&I801_dev->dev, "Terminating the current operation\n");
158 outb_p(inb_p(SMBHSTCNT) | SMBHSTCNT_KILL, SMBHSTCNT);
159 msleep(1);
160 outb_p(inb_p(SMBHSTCNT) & (~SMBHSTCNT_KILL), SMBHSTCNT);
142 } 161 }
143 162
144 if (temp & 0x10) { 163 if (temp & SMBHSTSTS_FAILED) {
145 result = -1; 164 result = -1;
146 dev_dbg(&I801_dev->dev, "Error: Failed bus transaction\n"); 165 dev_dbg(&I801_dev->dev, "Error: Failed bus transaction\n");
147 } 166 }
148 167
149 if (temp & 0x08) { 168 if (temp & SMBHSTSTS_BUS_ERR) {
150 result = -1; 169 result = -1;
151 dev_err(&I801_dev->dev, "Bus collision! SMBus may be locked " 170 dev_err(&I801_dev->dev, "Bus collision! SMBus may be locked "
152 "until next hard reset. (sorry!)\n"); 171 "until next hard reset. (sorry!)\n");
153 /* Clock stops and slave is stuck in mid-transmission */ 172 /* Clock stops and slave is stuck in mid-transmission */
154 } 173 }
155 174
156 if (temp & 0x04) { 175 if (temp & SMBHSTSTS_DEV_ERR) {
157 result = -1; 176 result = -1;
158 dev_dbg(&I801_dev->dev, "Error: no response!\n"); 177 dev_dbg(&I801_dev->dev, "Error: no response!\n");
159 } 178 }
@@ -172,44 +191,70 @@ static int i801_transaction(void)
172 return result; 191 return result;
173} 192}
174 193
175/* All-inclusive block transaction function */ 194/* wait for INTR bit as advised by Intel */
176static int i801_block_transaction(union i2c_smbus_data *data, char read_write, 195static void i801_wait_hwpec(void)
177 int command, int hwpec) 196{
197 int timeout = 0;
198 int temp;
199
200 do {
201 msleep(1);
202 temp = inb_p(SMBHSTSTS);
203 } while ((!(temp & SMBHSTSTS_INTR))
204 && (timeout++ < MAX_TIMEOUT));
205
206 if (timeout >= MAX_TIMEOUT) {
207 dev_dbg(&I801_dev->dev, "PEC Timeout!\n");
208 }
209 outb_p(temp, SMBHSTSTS);
210}
211
212static int i801_block_transaction_by_block(union i2c_smbus_data *data,
213 char read_write, int hwpec)
214{
215 int i, len;
216
217 inb_p(SMBHSTCNT); /* reset the data buffer index */
218
219 /* Use 32-byte buffer to process this transaction */
220 if (read_write == I2C_SMBUS_WRITE) {
221 len = data->block[0];
222 outb_p(len, SMBHSTDAT0);
223 for (i = 0; i < len; i++)
224 outb_p(data->block[i+1], SMBBLKDAT);
225 }
226
227 if (i801_transaction(I801_BLOCK_DATA | ENABLE_INT9 |
228 I801_PEC_EN * hwpec))
229 return -1;
230
231 if (read_write == I2C_SMBUS_READ) {
232 len = inb_p(SMBHSTDAT0);
233 if (len < 1 || len > I2C_SMBUS_BLOCK_MAX)
234 return -1;
235
236 data->block[0] = len;
237 for (i = 0; i < len; i++)
238 data->block[i + 1] = inb_p(SMBBLKDAT);
239 }
240 return 0;
241}
242
243static int i801_block_transaction_byte_by_byte(union i2c_smbus_data *data,
244 char read_write, int hwpec)
178{ 245{
179 int i, len; 246 int i, len;
180 int smbcmd; 247 int smbcmd;
181 int temp; 248 int temp;
182 int result = 0; 249 int result = 0;
183 int timeout; 250 int timeout;
184 unsigned char hostc, errmask; 251 unsigned char errmask;
185 252
186 if (command == I2C_SMBUS_I2C_BLOCK_DATA) { 253 len = data->block[0];
187 if (read_write == I2C_SMBUS_WRITE) {
188 /* set I2C_EN bit in configuration register */
189 pci_read_config_byte(I801_dev, SMBHSTCFG, &hostc);
190 pci_write_config_byte(I801_dev, SMBHSTCFG,
191 hostc | SMBHSTCFG_I2C_EN);
192 } else {
193 dev_err(&I801_dev->dev,
194 "I2C_SMBUS_I2C_BLOCK_READ not DB!\n");
195 return -1;
196 }
197 }
198 254
199 if (read_write == I2C_SMBUS_WRITE) { 255 if (read_write == I2C_SMBUS_WRITE) {
200 len = data->block[0];
201 if (len < 1)
202 len = 1;
203 if (len > 32)
204 len = 32;
205 outb_p(len, SMBHSTDAT0); 256 outb_p(len, SMBHSTDAT0);
206 outb_p(data->block[1], SMBBLKDAT); 257 outb_p(data->block[1], SMBBLKDAT);
207 } else {
208 len = 32; /* max for reads */
209 }
210
211 if(isich4 && command != I2C_SMBUS_I2C_BLOCK_DATA) {
212 /* set 32 byte buffer */
213 } 258 }
214 259
215 for (i = 1; i <= len; i++) { 260 for (i = 1; i <= len; i++) {
@@ -227,13 +272,13 @@ static int i801_block_transaction(union i2c_smbus_data *data, char read_write,
227 /* Make sure the SMBus host is ready to start transmitting */ 272 /* Make sure the SMBus host is ready to start transmitting */
228 temp = inb_p(SMBHSTSTS); 273 temp = inb_p(SMBHSTSTS);
229 if (i == 1) { 274 if (i == 1) {
230 /* Erronenous conditions before transaction: 275 /* Erronenous conditions before transaction:
231 * Byte_Done, Failed, Bus_Err, Dev_Err, Intr, Host_Busy */ 276 * Byte_Done, Failed, Bus_Err, Dev_Err, Intr, Host_Busy */
232 errmask=0x9f; 277 errmask = 0x9f;
233 } else { 278 } else {
234 /* Erronenous conditions during transaction: 279 /* Erronenous conditions during transaction:
235 * Failed, Bus_Err, Dev_Err, Intr */ 280 * Failed, Bus_Err, Dev_Err, Intr */
236 errmask=0x1e; 281 errmask = 0x1e;
237 } 282 }
238 if (temp & errmask) { 283 if (temp & errmask) {
239 dev_dbg(&I801_dev->dev, "SMBus busy (%02x). " 284 dev_dbg(&I801_dev->dev, "SMBus busy (%02x). "
@@ -242,14 +287,11 @@ static int i801_block_transaction(union i2c_smbus_data *data, char read_write,
242 if (((temp = inb_p(SMBHSTSTS)) & errmask) != 0x00) { 287 if (((temp = inb_p(SMBHSTSTS)) & errmask) != 0x00) {
243 dev_err(&I801_dev->dev, 288 dev_err(&I801_dev->dev,
244 "Reset failed! (%02x)\n", temp); 289 "Reset failed! (%02x)\n", temp);
245 result = -1; 290 return -1;
246 goto END;
247 } 291 }
248 if (i != 1) { 292 if (i != 1)
249 /* if die in middle of block transaction, fail */ 293 /* if die in middle of block transaction, fail */
250 result = -1; 294 return -1;
251 goto END;
252 }
253 } 295 }
254 296
255 if (i == 1) 297 if (i == 1)
@@ -261,33 +303,38 @@ static int i801_block_transaction(union i2c_smbus_data *data, char read_write,
261 msleep(1); 303 msleep(1);
262 temp = inb_p(SMBHSTSTS); 304 temp = inb_p(SMBHSTSTS);
263 } 305 }
264 while ((!(temp & 0x80)) 306 while ((!(temp & SMBHSTSTS_BYTE_DONE))
265 && (timeout++ < MAX_TIMEOUT)); 307 && (timeout++ < MAX_TIMEOUT));
266 308
267 /* If the SMBus is still busy, we give up */ 309 /* If the SMBus is still busy, we give up */
268 if (timeout >= MAX_TIMEOUT) { 310 if (timeout >= MAX_TIMEOUT) {
311 /* try to stop the current command */
312 dev_dbg(&I801_dev->dev, "Terminating the current "
313 "operation\n");
314 outb_p(inb_p(SMBHSTCNT) | SMBHSTCNT_KILL, SMBHSTCNT);
315 msleep(1);
316 outb_p(inb_p(SMBHSTCNT) & (~SMBHSTCNT_KILL),
317 SMBHSTCNT);
269 result = -1; 318 result = -1;
270 dev_dbg(&I801_dev->dev, "SMBus Timeout!\n"); 319 dev_dbg(&I801_dev->dev, "SMBus Timeout!\n");
271 } 320 }
272 321
273 if (temp & 0x10) { 322 if (temp & SMBHSTSTS_FAILED) {
274 result = -1; 323 result = -1;
275 dev_dbg(&I801_dev->dev, 324 dev_dbg(&I801_dev->dev,
276 "Error: Failed bus transaction\n"); 325 "Error: Failed bus transaction\n");
277 } else if (temp & 0x08) { 326 } else if (temp & SMBHSTSTS_BUS_ERR) {
278 result = -1; 327 result = -1;
279 dev_err(&I801_dev->dev, "Bus collision!\n"); 328 dev_err(&I801_dev->dev, "Bus collision!\n");
280 } else if (temp & 0x04) { 329 } else if (temp & SMBHSTSTS_DEV_ERR) {
281 result = -1; 330 result = -1;
282 dev_dbg(&I801_dev->dev, "Error: no response!\n"); 331 dev_dbg(&I801_dev->dev, "Error: no response!\n");
283 } 332 }
284 333
285 if (i == 1 && read_write == I2C_SMBUS_READ) { 334 if (i == 1 && read_write == I2C_SMBUS_READ) {
286 len = inb_p(SMBHSTDAT0); 335 len = inb_p(SMBHSTDAT0);
287 if (len < 1) 336 if (len < 1 || len > I2C_SMBUS_BLOCK_MAX)
288 len = 1; 337 return -1;
289 if (len > 32)
290 len = 32;
291 data->block[0] = len; 338 data->block[0] = len;
292 } 339 }
293 340
@@ -310,25 +357,58 @@ static int i801_block_transaction(union i2c_smbus_data *data, char read_write,
310 inb_p(SMBHSTDAT0), inb_p(SMBBLKDAT)); 357 inb_p(SMBHSTDAT0), inb_p(SMBBLKDAT));
311 358
312 if (result < 0) 359 if (result < 0)
313 goto END; 360 return result;
314 } 361 }
362 return result;
363}
315 364
316 if (hwpec) { 365static int i801_set_block_buffer_mode(void)
317 /* wait for INTR bit as advised by Intel */ 366{
318 timeout = 0; 367 outb_p(inb_p(SMBAUXCTL) | SMBAUXCTL_E32B, SMBAUXCTL);
319 do { 368 if ((inb_p(SMBAUXCTL) & SMBAUXCTL_E32B) == 0)
320 msleep(1); 369 return -1;
321 temp = inb_p(SMBHSTSTS); 370 return 0;
322 } while ((!(temp & 0x02)) 371}
323 && (timeout++ < MAX_TIMEOUT));
324 372
325 if (timeout >= MAX_TIMEOUT) { 373/* Block transaction function */
326 dev_dbg(&I801_dev->dev, "PEC Timeout!\n"); 374static int i801_block_transaction(union i2c_smbus_data *data, char read_write,
375 int command, int hwpec)
376{
377 int result = 0;
378 unsigned char hostc;
379
380 if (command == I2C_SMBUS_I2C_BLOCK_DATA) {
381 if (read_write == I2C_SMBUS_WRITE) {
382 /* set I2C_EN bit in configuration register */
383 pci_read_config_byte(I801_dev, SMBHSTCFG, &hostc);
384 pci_write_config_byte(I801_dev, SMBHSTCFG,
385 hostc | SMBHSTCFG_I2C_EN);
386 } else {
387 dev_err(&I801_dev->dev,
388 "I2C_SMBUS_I2C_BLOCK_READ not DB!\n");
389 return -1;
327 } 390 }
328 outb_p(temp, SMBHSTSTS);
329 } 391 }
330 result = 0; 392
331END: 393 if (read_write == I2C_SMBUS_WRITE) {
394 if (data->block[0] < 1)
395 data->block[0] = 1;
396 if (data->block[0] > I2C_SMBUS_BLOCK_MAX)
397 data->block[0] = I2C_SMBUS_BLOCK_MAX;
398 } else {
399 data->block[0] = 32; /* max for reads */
400 }
401
402 if (isich4 && i801_set_block_buffer_mode() == 0 )
403 result = i801_block_transaction_by_block(data, read_write,
404 hwpec);
405 else
406 result = i801_block_transaction_byte_by_byte(data, read_write,
407 hwpec);
408
409 if (result == 0 && hwpec)
410 i801_wait_hwpec();
411
332 if (command == I2C_SMBUS_I2C_BLOCK_DATA) { 412 if (command == I2C_SMBUS_I2C_BLOCK_DATA) {
333 /* restore saved configuration register value */ 413 /* restore saved configuration register value */
334 pci_write_config_byte(I801_dev, SMBHSTCFG, hostc); 414 pci_write_config_byte(I801_dev, SMBHSTCFG, hostc);
@@ -393,19 +473,22 @@ static s32 i801_access(struct i2c_adapter * adap, u16 addr,
393 return -1; 473 return -1;
394 } 474 }
395 475
396 outb_p(hwpec, SMBAUXCTL); /* enable/disable hardware PEC */ 476 if (hwpec) /* enable/disable hardware PEC */
477 outb_p(inb_p(SMBAUXCTL) | SMBAUXCTL_CRC, SMBAUXCTL);
478 else
479 outb_p(inb_p(SMBAUXCTL) & (~SMBAUXCTL_CRC), SMBAUXCTL);
397 480
398 if(block) 481 if(block)
399 ret = i801_block_transaction(data, read_write, size, hwpec); 482 ret = i801_block_transaction(data, read_write, size, hwpec);
400 else { 483 else
401 outb_p(xact | ENABLE_INT9, SMBHSTCNT); 484 ret = i801_transaction(xact | ENABLE_INT9);
402 ret = i801_transaction();
403 }
404 485
405 /* Some BIOSes don't like it when PEC is enabled at reboot or resume 486 /* Some BIOSes don't like it when PEC is enabled at reboot or resume
406 time, so we forcibly disable it after every transaction. */ 487 time, so we forcibly disable it after every transaction. Turn off
488 E32B for the same reason. */
407 if (hwpec) 489 if (hwpec)
408 outb_p(0, SMBAUXCTL); 490 outb_p(inb_p(SMBAUXCTL) & ~(SMBAUXCTL_CRC | SMBAUXCTL_E32B),
491 SMBAUXCTL);
409 492
410 if(block) 493 if(block)
411 return ret; 494 return ret;
diff --git a/drivers/i2c/busses/i2c-iop3xx.c b/drivers/i2c/busses/i2c-iop3xx.c
index 90e2d9350c1b..440342bc62e1 100644
--- a/drivers/i2c/busses/i2c-iop3xx.c
+++ b/drivers/i2c/busses/i2c-iop3xx.c
@@ -491,6 +491,7 @@ iop3xx_i2c_probe(struct platform_device *pdev)
491 new_adapter->id = I2C_HW_IOP3XX; 491 new_adapter->id = I2C_HW_IOP3XX;
492 new_adapter->owner = THIS_MODULE; 492 new_adapter->owner = THIS_MODULE;
493 new_adapter->dev.parent = &pdev->dev; 493 new_adapter->dev.parent = &pdev->dev;
494 new_adapter->nr = pdev->id;
494 495
495 /* 496 /*
496 * Default values...should these come in from board code? 497 * Default values...should these come in from board code?
@@ -508,7 +509,7 @@ iop3xx_i2c_probe(struct platform_device *pdev)
508 platform_set_drvdata(pdev, new_adapter); 509 platform_set_drvdata(pdev, new_adapter);
509 new_adapter->algo_data = adapter_data; 510 new_adapter->algo_data = adapter_data;
510 511
511 i2c_add_adapter(new_adapter); 512 i2c_add_numbered_adapter(new_adapter);
512 513
513 return 0; 514 return 0;
514 515
diff --git a/drivers/i2c/busses/i2c-mpc.c b/drivers/i2c/busses/i2c-mpc.c
index c6b6898592b1..851c3ed513d0 100644
--- a/drivers/i2c/busses/i2c-mpc.c
+++ b/drivers/i2c/busses/i2c-mpc.c
@@ -74,6 +74,25 @@ static irqreturn_t mpc_i2c_isr(int irq, void *dev_id)
74 return IRQ_HANDLED; 74 return IRQ_HANDLED;
75} 75}
76 76
77/* Sometimes 9th clock pulse isn't generated, and slave doesn't release
78 * the bus, because it wants to send ACK.
79 * Following sequence of enabling/disabling and sending start/stop generates
80 * the pulse, so it's all OK.
81 */
82static void mpc_i2c_fixup(struct mpc_i2c *i2c)
83{
84 writeccr(i2c, 0);
85 udelay(30);
86 writeccr(i2c, CCR_MEN);
87 udelay(30);
88 writeccr(i2c, CCR_MSTA | CCR_MTX);
89 udelay(30);
90 writeccr(i2c, CCR_MSTA | CCR_MTX | CCR_MEN);
91 udelay(30);
92 writeccr(i2c, CCR_MEN);
93 udelay(30);
94}
95
77static int i2c_wait(struct mpc_i2c *i2c, unsigned timeout, int writing) 96static int i2c_wait(struct mpc_i2c *i2c, unsigned timeout, int writing)
78{ 97{
79 unsigned long orig_jiffies = jiffies; 98 unsigned long orig_jiffies = jiffies;
@@ -153,6 +172,7 @@ static void mpc_i2c_start(struct mpc_i2c *i2c)
153static void mpc_i2c_stop(struct mpc_i2c *i2c) 172static void mpc_i2c_stop(struct mpc_i2c *i2c)
154{ 173{
155 writeccr(i2c, CCR_MEN); 174 writeccr(i2c, CCR_MEN);
175 writeccr(i2c, 0);
156} 176}
157 177
158static int mpc_write(struct mpc_i2c *i2c, int target, 178static int mpc_write(struct mpc_i2c *i2c, int target,
@@ -245,6 +265,9 @@ static int mpc_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
245 } 265 }
246 if (time_after(jiffies, orig_jiffies + HZ)) { 266 if (time_after(jiffies, orig_jiffies + HZ)) {
247 pr_debug("I2C: timeout\n"); 267 pr_debug("I2C: timeout\n");
268 if (readb(i2c->base + MPC_I2C_SR) ==
269 (CSR_MCF | CSR_MBB | CSR_RXAK))
270 mpc_i2c_fixup(i2c);
248 return -EIO; 271 return -EIO;
249 } 272 }
250 schedule(); 273 schedule();
@@ -327,9 +350,10 @@ static int fsl_i2c_probe(struct platform_device *pdev)
327 platform_set_drvdata(pdev, i2c); 350 platform_set_drvdata(pdev, i2c);
328 351
329 i2c->adap = mpc_ops; 352 i2c->adap = mpc_ops;
353 i2c->adap.nr = pdev->id;
330 i2c_set_adapdata(&i2c->adap, i2c); 354 i2c_set_adapdata(&i2c->adap, i2c);
331 i2c->adap.dev.parent = &pdev->dev; 355 i2c->adap.dev.parent = &pdev->dev;
332 if ((result = i2c_add_adapter(&i2c->adap)) < 0) { 356 if ((result = i2c_add_numbered_adapter(&i2c->adap)) < 0) {
333 printk(KERN_ERR "i2c-mpc - failed to add adapter\n"); 357 printk(KERN_ERR "i2c-mpc - failed to add adapter\n");
334 goto fail_add; 358 goto fail_add;
335 } 359 }
diff --git a/drivers/i2c/busses/i2c-mv64xxx.c b/drivers/i2c/busses/i2c-mv64xxx.c
index a55b3335d1be..251154ae5d97 100644
--- a/drivers/i2c/busses/i2c-mv64xxx.c
+++ b/drivers/i2c/busses/i2c-mv64xxx.c
@@ -527,6 +527,7 @@ mv64xxx_i2c_probe(struct platform_device *pd)
527 drv_data->adapter.class = I2C_CLASS_HWMON; 527 drv_data->adapter.class = I2C_CLASS_HWMON;
528 drv_data->adapter.timeout = pdata->timeout; 528 drv_data->adapter.timeout = pdata->timeout;
529 drv_data->adapter.retries = pdata->retries; 529 drv_data->adapter.retries = pdata->retries;
530 drv_data->adapter.nr = pd->id;
530 platform_set_drvdata(pd, drv_data); 531 platform_set_drvdata(pd, drv_data);
531 i2c_set_adapdata(&drv_data->adapter, drv_data); 532 i2c_set_adapdata(&drv_data->adapter, drv_data);
532 533
@@ -539,7 +540,7 @@ mv64xxx_i2c_probe(struct platform_device *pd)
539 drv_data->irq); 540 drv_data->irq);
540 rc = -EINVAL; 541 rc = -EINVAL;
541 goto exit_unmap_regs; 542 goto exit_unmap_regs;
542 } else if ((rc = i2c_add_adapter(&drv_data->adapter)) != 0) { 543 } else if ((rc = i2c_add_numbered_adapter(&drv_data->adapter)) != 0) {
543 dev_err(&drv_data->adapter.dev, 544 dev_err(&drv_data->adapter.dev,
544 "mv64xxx: Can't add i2c adapter, rc: %d\n", -rc); 545 "mv64xxx: Can't add i2c adapter, rc: %d\n", -rc);
545 goto exit_free_irq; 546 goto exit_free_irq;
diff --git a/drivers/i2c/busses/i2c-nforce2.c b/drivers/i2c/busses/i2c-nforce2.c
index 3cd0d63e7b50..c48140f782d0 100644
--- a/drivers/i2c/busses/i2c-nforce2.c
+++ b/drivers/i2c/busses/i2c-nforce2.c
@@ -61,6 +61,7 @@ struct nforce2_smbus {
61 struct i2c_adapter adapter; 61 struct i2c_adapter adapter;
62 int base; 62 int base;
63 int size; 63 int size;
64 int blockops;
64}; 65};
65 66
66 67
@@ -80,6 +81,8 @@ struct nforce2_smbus {
80#define NVIDIA_SMB_ADDR (smbus->base + 0x02) /* address */ 81#define NVIDIA_SMB_ADDR (smbus->base + 0x02) /* address */
81#define NVIDIA_SMB_CMD (smbus->base + 0x03) /* command */ 82#define NVIDIA_SMB_CMD (smbus->base + 0x03) /* command */
82#define NVIDIA_SMB_DATA (smbus->base + 0x04) /* 32 data registers */ 83#define NVIDIA_SMB_DATA (smbus->base + 0x04) /* 32 data registers */
84#define NVIDIA_SMB_BCNT (smbus->base + 0x24) /* number of data
85 bytes */
83 86
84#define NVIDIA_SMB_STS_DONE 0x80 87#define NVIDIA_SMB_STS_DONE 0x80
85#define NVIDIA_SMB_STS_ALRM 0x40 88#define NVIDIA_SMB_STS_ALRM 0x40
@@ -92,6 +95,7 @@ struct nforce2_smbus {
92#define NVIDIA_SMB_PRTCL_BYTE 0x04 95#define NVIDIA_SMB_PRTCL_BYTE 0x04
93#define NVIDIA_SMB_PRTCL_BYTE_DATA 0x06 96#define NVIDIA_SMB_PRTCL_BYTE_DATA 0x06
94#define NVIDIA_SMB_PRTCL_WORD_DATA 0x08 97#define NVIDIA_SMB_PRTCL_WORD_DATA 0x08
98#define NVIDIA_SMB_PRTCL_BLOCK_DATA 0x0a
95#define NVIDIA_SMB_PRTCL_PEC 0x80 99#define NVIDIA_SMB_PRTCL_PEC 0x80
96 100
97static struct pci_driver nforce2_driver; 101static struct pci_driver nforce2_driver;
@@ -103,6 +107,8 @@ static s32 nforce2_access(struct i2c_adapter * adap, u16 addr,
103{ 107{
104 struct nforce2_smbus *smbus = adap->algo_data; 108 struct nforce2_smbus *smbus = adap->algo_data;
105 unsigned char protocol, pec, temp; 109 unsigned char protocol, pec, temp;
110 u8 len;
111 int i;
106 112
107 protocol = (read_write == I2C_SMBUS_READ) ? NVIDIA_SMB_PRTCL_READ : 113 protocol = (read_write == I2C_SMBUS_READ) ? NVIDIA_SMB_PRTCL_READ :
108 NVIDIA_SMB_PRTCL_WRITE; 114 NVIDIA_SMB_PRTCL_WRITE;
@@ -137,6 +143,25 @@ static s32 nforce2_access(struct i2c_adapter * adap, u16 addr,
137 protocol |= NVIDIA_SMB_PRTCL_WORD_DATA | pec; 143 protocol |= NVIDIA_SMB_PRTCL_WORD_DATA | pec;
138 break; 144 break;
139 145
146 case I2C_SMBUS_BLOCK_DATA:
147 outb_p(command, NVIDIA_SMB_CMD);
148 if (read_write == I2C_SMBUS_WRITE) {
149 len = data->block[0];
150 if ((len == 0) || (len > I2C_SMBUS_BLOCK_MAX)) {
151 dev_err(&adap->dev,
152 "Transaction failed "
153 "(requested block size: %d)\n",
154 len);
155 return -1;
156 }
157 outb_p(len, NVIDIA_SMB_BCNT);
158 for (i = 0; i < I2C_SMBUS_BLOCK_MAX; i++)
159 outb_p(data->block[i + 1],
160 NVIDIA_SMB_DATA+i);
161 }
162 protocol |= NVIDIA_SMB_PRTCL_BLOCK_DATA | pec;
163 break;
164
140 default: 165 default:
141 dev_err(&adap->dev, "Unsupported transaction %d\n", size); 166 dev_err(&adap->dev, "Unsupported transaction %d\n", size);
142 return -1; 167 return -1;
@@ -174,6 +199,14 @@ static s32 nforce2_access(struct i2c_adapter * adap, u16 addr,
174 case I2C_SMBUS_WORD_DATA: 199 case I2C_SMBUS_WORD_DATA:
175 data->word = inb_p(NVIDIA_SMB_DATA) | (inb_p(NVIDIA_SMB_DATA+1) << 8); 200 data->word = inb_p(NVIDIA_SMB_DATA) | (inb_p(NVIDIA_SMB_DATA+1) << 8);
176 break; 201 break;
202
203 case I2C_SMBUS_BLOCK_DATA:
204 len = inb_p(NVIDIA_SMB_BCNT);
205 len = min_t(u8, len, I2C_SMBUS_BLOCK_MAX);
206 for (i = 0; i < len; i++)
207 data->block[i+1] = inb_p(NVIDIA_SMB_DATA + i);
208 data->block[0] = len;
209 break;
177 } 210 }
178 211
179 return 0; 212 return 0;
@@ -184,7 +217,9 @@ static u32 nforce2_func(struct i2c_adapter *adapter)
184{ 217{
185 /* other functionality might be possible, but is not tested */ 218 /* other functionality might be possible, but is not tested */
186 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE | 219 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
187 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA; 220 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
221 (((struct nforce2_smbus*)adapter->algo_data)->blockops ?
222 I2C_FUNC_SMBUS_BLOCK_DATA : 0);
188} 223}
189 224
190static struct i2c_algorithm smbus_algorithm = { 225static struct i2c_algorithm smbus_algorithm = {
@@ -268,6 +303,13 @@ static int __devinit nforce2_probe(struct pci_dev *dev, const struct pci_device_
268 return -ENOMEM; 303 return -ENOMEM;
269 pci_set_drvdata(dev, smbuses); 304 pci_set_drvdata(dev, smbuses);
270 305
306 switch(dev->device) {
307 case PCI_DEVICE_ID_NVIDIA_NFORCE_MCP51_SMBUS:
308 case PCI_DEVICE_ID_NVIDIA_NFORCE_MCP55_SMBUS:
309 smbuses[0].blockops = 1;
310 smbuses[1].blockops = 1;
311 }
312
271 /* SMBus adapter 1 */ 313 /* SMBus adapter 1 */
272 res1 = nforce2_probe_smb(dev, 4, NFORCE_PCI_SMB1, &smbuses[0], "SMB1"); 314 res1 = nforce2_probe_smb(dev, 4, NFORCE_PCI_SMB1, &smbuses[0], "SMB1");
273 if (res1 < 0) { 315 if (res1 < 0) {
diff --git a/drivers/i2c/busses/i2c-piix4.c b/drivers/i2c/busses/i2c-piix4.c
index 5a52bf5e3fb0..debc76cd2161 100644
--- a/drivers/i2c/busses/i2c-piix4.c
+++ b/drivers/i2c/busses/i2c-piix4.c
@@ -23,7 +23,7 @@
23 Supports: 23 Supports:
24 Intel PIIX4, 440MX 24 Intel PIIX4, 440MX
25 Serverworks OSB4, CSB5, CSB6, HT-1000 25 Serverworks OSB4, CSB5, CSB6, HT-1000
26 ATI IXP200, IXP300, IXP400, SB600 26 ATI IXP200, IXP300, IXP400, SB600, SB700
27 SMSC Victory66 27 SMSC Victory66
28 28
29 Note: we assume there can only be one device, with one SMBus interface. 29 Note: we assume there can only be one device, with one SMBus interface.
@@ -399,6 +399,8 @@ static struct pci_device_id piix4_ids[] = {
399 .driver_data = 0 }, 399 .driver_data = 0 },
400 { PCI_DEVICE(PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_IXP600_SMBUS), 400 { PCI_DEVICE(PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_IXP600_SMBUS),
401 .driver_data = 0 }, 401 .driver_data = 0 },
402 { PCI_DEVICE(PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_IXP700_SMBUS),
403 .driver_data = 0 },
402 { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS, PCI_DEVICE_ID_SERVERWORKS_OSB4), 404 { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS, PCI_DEVICE_ID_SERVERWORKS_OSB4),
403 .driver_data = 0 }, 405 .driver_data = 0 },
404 { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS, PCI_DEVICE_ID_SERVERWORKS_CSB5), 406 { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS, PCI_DEVICE_ID_SERVERWORKS_CSB5),
diff --git a/drivers/i2c/busses/i2c-pmcmsp.c b/drivers/i2c/busses/i2c-pmcmsp.c
new file mode 100644
index 000000000000..17cecf1ea797
--- /dev/null
+++ b/drivers/i2c/busses/i2c-pmcmsp.c
@@ -0,0 +1,653 @@
1/*
2 * Specific bus support for PMC-TWI compliant implementation on MSP71xx.
3 *
4 * Copyright 2005-2007 PMC-Sierra, Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version.
10 *
11 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
12 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
14 * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
15 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
16 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
17 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
18 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
19 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
20 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
21 *
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 675 Mass Ave, Cambridge, MA 02139, USA.
25 */
26
27#include <linux/kernel.h>
28#include <linux/module.h>
29#include <linux/init.h>
30#include <linux/platform_device.h>
31#include <linux/i2c.h>
32#include <linux/interrupt.h>
33#include <linux/completion.h>
34#include <linux/mutex.h>
35#include <linux/delay.h>
36#include <asm/io.h>
37
38#define DRV_NAME "pmcmsptwi"
39
40#define MSP_TWI_SF_CLK_REG_OFFSET 0x00
41#define MSP_TWI_HS_CLK_REG_OFFSET 0x04
42#define MSP_TWI_CFG_REG_OFFSET 0x08
43#define MSP_TWI_CMD_REG_OFFSET 0x0c
44#define MSP_TWI_ADD_REG_OFFSET 0x10
45#define MSP_TWI_DAT_0_REG_OFFSET 0x14
46#define MSP_TWI_DAT_1_REG_OFFSET 0x18
47#define MSP_TWI_INT_STS_REG_OFFSET 0x1c
48#define MSP_TWI_INT_MSK_REG_OFFSET 0x20
49#define MSP_TWI_BUSY_REG_OFFSET 0x24
50
51#define MSP_TWI_INT_STS_DONE (1 << 0)
52#define MSP_TWI_INT_STS_LOST_ARBITRATION (1 << 1)
53#define MSP_TWI_INT_STS_NO_RESPONSE (1 << 2)
54#define MSP_TWI_INT_STS_DATA_COLLISION (1 << 3)
55#define MSP_TWI_INT_STS_BUSY (1 << 4)
56#define MSP_TWI_INT_STS_ALL 0x1f
57
58#define MSP_MAX_BYTES_PER_RW 8
59#define MSP_MAX_POLL 5
60#define MSP_POLL_DELAY 10
61#define MSP_IRQ_TIMEOUT (MSP_MAX_POLL * MSP_POLL_DELAY)
62
63/* IO Operation macros */
64#define pmcmsptwi_readl __raw_readl
65#define pmcmsptwi_writel __raw_writel
66
67/* TWI command type */
68enum pmcmsptwi_cmd_type {
69 MSP_TWI_CMD_WRITE = 0, /* Write only */
70 MSP_TWI_CMD_READ = 1, /* Read only */
71 MSP_TWI_CMD_WRITE_READ = 2, /* Write then Read */
72};
73
74/* The possible results of the xferCmd */
75enum pmcmsptwi_xfer_result {
76 MSP_TWI_XFER_OK = 0,
77 MSP_TWI_XFER_TIMEOUT,
78 MSP_TWI_XFER_BUSY,
79 MSP_TWI_XFER_DATA_COLLISION,
80 MSP_TWI_XFER_NO_RESPONSE,
81 MSP_TWI_XFER_LOST_ARBITRATION,
82};
83
84/* Corresponds to a PMCTWI clock configuration register */
85struct pmcmsptwi_clock {
86 u8 filter; /* Bits 15:12, default = 0x03 */
87 u16 clock; /* Bits 9:0, default = 0x001f */
88};
89
90struct pmcmsptwi_clockcfg {
91 struct pmcmsptwi_clock standard; /* The standard/fast clock config */
92 struct pmcmsptwi_clock highspeed; /* The highspeed clock config */
93};
94
95/* Corresponds to the main TWI configuration register */
96struct pmcmsptwi_cfg {
97 u8 arbf; /* Bits 15:12, default=0x03 */
98 u8 nak; /* Bits 11:8, default=0x03 */
99 u8 add10; /* Bit 7, default=0x00 */
100 u8 mst_code; /* Bits 6:4, default=0x00 */
101 u8 arb; /* Bit 1, default=0x01 */
102 u8 highspeed; /* Bit 0, default=0x00 */
103};
104
105/* A single pmctwi command to issue */
106struct pmcmsptwi_cmd {
107 u16 addr; /* The slave address (7 or 10 bits) */
108 enum pmcmsptwi_cmd_type type; /* The command type */
109 u8 write_len; /* Number of bytes in the write buffer */
110 u8 read_len; /* Number of bytes in the read buffer */
111 u8 *write_data; /* Buffer of characters to send */
112 u8 *read_data; /* Buffer to fill with incoming data */
113};
114
115/* The private data */
116struct pmcmsptwi_data {
117 void __iomem *iobase; /* iomapped base for IO */
118 int irq; /* IRQ to use (0 disables) */
119 struct completion wait; /* Completion for xfer */
120 struct mutex lock; /* Used for threadsafeness */
121 enum pmcmsptwi_xfer_result last_result; /* result of last xfer */
122};
123
124/* The default settings */
125const static struct pmcmsptwi_clockcfg pmcmsptwi_defclockcfg = {
126 .standard = {
127 .filter = 0x3,
128 .clock = 0x1f,
129 },
130 .highspeed = {
131 .filter = 0x3,
132 .clock = 0x1f,
133 },
134};
135
136const static struct pmcmsptwi_cfg pmcmsptwi_defcfg = {
137 .arbf = 0x03,
138 .nak = 0x03,
139 .add10 = 0x00,
140 .mst_code = 0x00,
141 .arb = 0x01,
142 .highspeed = 0x00,
143};
144
145static struct pmcmsptwi_data pmcmsptwi_data;
146
147static struct i2c_adapter pmcmsptwi_adapter;
148
149/* inline helper functions */
150static inline u32 pmcmsptwi_clock_to_reg(
151 const struct pmcmsptwi_clock *clock)
152{
153 return ((clock->filter & 0xf) << 12) | (clock->clock & 0x03ff);
154}
155
156static inline void pmcmsptwi_reg_to_clock(
157 u32 reg, struct pmcmsptwi_clock *clock)
158{
159 clock->filter = (reg >> 12) & 0xf;
160 clock->clock = reg & 0x03ff;
161}
162
163static inline u32 pmcmsptwi_cfg_to_reg(const struct pmcmsptwi_cfg *cfg)
164{
165 return ((cfg->arbf & 0xf) << 12) |
166 ((cfg->nak & 0xf) << 8) |
167 ((cfg->add10 & 0x1) << 7) |
168 ((cfg->mst_code & 0x7) << 4) |
169 ((cfg->arb & 0x1) << 1) |
170 (cfg->highspeed & 0x1);
171}
172
173static inline void pmcmsptwi_reg_to_cfg(u32 reg, struct pmcmsptwi_cfg *cfg)
174{
175 cfg->arbf = (reg >> 12) & 0xf;
176 cfg->nak = (reg >> 8) & 0xf;
177 cfg->add10 = (reg >> 7) & 0x1;
178 cfg->mst_code = (reg >> 4) & 0x7;
179 cfg->arb = (reg >> 1) & 0x1;
180 cfg->highspeed = reg & 0x1;
181}
182
183/*
184 * Sets the current clock configuration
185 */
186static void pmcmsptwi_set_clock_config(const struct pmcmsptwi_clockcfg *cfg,
187 struct pmcmsptwi_data *data)
188{
189 mutex_lock(&data->lock);
190 pmcmsptwi_writel(pmcmsptwi_clock_to_reg(&cfg->standard),
191 data->iobase + MSP_TWI_SF_CLK_REG_OFFSET);
192 pmcmsptwi_writel(pmcmsptwi_clock_to_reg(&cfg->highspeed),
193 data->iobase + MSP_TWI_HS_CLK_REG_OFFSET);
194 mutex_unlock(&data->lock);
195}
196
197/*
198 * Gets the current TWI bus configuration
199 */
200static void pmcmsptwi_get_twi_config(struct pmcmsptwi_cfg *cfg,
201 struct pmcmsptwi_data *data)
202{
203 mutex_lock(&data->lock);
204 pmcmsptwi_reg_to_cfg(pmcmsptwi_readl(
205 data->iobase + MSP_TWI_CFG_REG_OFFSET), cfg);
206 mutex_unlock(&data->lock);
207}
208
209/*
210 * Sets the current TWI bus configuration
211 */
212static void pmcmsptwi_set_twi_config(const struct pmcmsptwi_cfg *cfg,
213 struct pmcmsptwi_data *data)
214{
215 mutex_lock(&data->lock);
216 pmcmsptwi_writel(pmcmsptwi_cfg_to_reg(cfg),
217 data->iobase + MSP_TWI_CFG_REG_OFFSET);
218 mutex_unlock(&data->lock);
219}
220
221/*
222 * Parses the 'int_sts' register and returns a well-defined error code
223 */
224static enum pmcmsptwi_xfer_result pmcmsptwi_get_result(u32 reg)
225{
226 if (reg & MSP_TWI_INT_STS_LOST_ARBITRATION) {
227 dev_dbg(&pmcmsptwi_adapter.dev,
228 "Result: Lost arbitration\n");
229 return MSP_TWI_XFER_LOST_ARBITRATION;
230 } else if (reg & MSP_TWI_INT_STS_NO_RESPONSE) {
231 dev_dbg(&pmcmsptwi_adapter.dev,
232 "Result: No response\n");
233 return MSP_TWI_XFER_NO_RESPONSE;
234 } else if (reg & MSP_TWI_INT_STS_DATA_COLLISION) {
235 dev_dbg(&pmcmsptwi_adapter.dev,
236 "Result: Data collision\n");
237 return MSP_TWI_XFER_DATA_COLLISION;
238 } else if (reg & MSP_TWI_INT_STS_BUSY) {
239 dev_dbg(&pmcmsptwi_adapter.dev,
240 "Result: Bus busy\n");
241 return MSP_TWI_XFER_BUSY;
242 }
243
244 dev_dbg(&pmcmsptwi_adapter.dev, "Result: Operation succeeded\n");
245 return MSP_TWI_XFER_OK;
246}
247
248/*
249 * In interrupt mode, handle the interrupt.
250 * NOTE: Assumes data->lock is held.
251 */
252static irqreturn_t pmcmsptwi_interrupt(int irq, void *ptr)
253{
254 struct pmcmsptwi_data *data = ptr;
255
256 u32 reason = pmcmsptwi_readl(data->iobase +
257 MSP_TWI_INT_STS_REG_OFFSET);
258 pmcmsptwi_writel(reason, data->iobase + MSP_TWI_INT_STS_REG_OFFSET);
259
260 dev_dbg(&pmcmsptwi_adapter.dev, "Got interrupt 0x%08x\n", reason);
261 if (!(reason & MSP_TWI_INT_STS_DONE))
262 return IRQ_NONE;
263
264 data->last_result = pmcmsptwi_get_result(reason);
265 complete(&data->wait);
266
267 return IRQ_HANDLED;
268}
269
270/*
271 * Probe for and register the device and return 0 if there is one.
272 */
273static int __devinit pmcmsptwi_probe(struct platform_device *pldev)
274{
275 struct resource *res;
276 int rc = -ENODEV;
277
278 /* get the static platform resources */
279 res = platform_get_resource(pldev, IORESOURCE_MEM, 0);
280 if (!res) {
281 dev_err(&pldev->dev, "IOMEM resource not found\n");
282 goto ret_err;
283 }
284
285 /* reserve the memory region */
286 if (!request_mem_region(res->start, res->end - res->start + 1,
287 pldev->name)) {
288 dev_err(&pldev->dev,
289 "Unable to get memory/io address region 0x%08x\n",
290 res->start);
291 rc = -EBUSY;
292 goto ret_err;
293 }
294
295 /* remap the memory */
296 pmcmsptwi_data.iobase = ioremap_nocache(res->start,
297 res->end - res->start + 1);
298 if (!pmcmsptwi_data.iobase) {
299 dev_err(&pldev->dev,
300 "Unable to ioremap address 0x%08x\n", res->start);
301 rc = -EIO;
302 goto ret_unreserve;
303 }
304
305 /* request the irq */
306 pmcmsptwi_data.irq = platform_get_irq(pldev, 0);
307 if (pmcmsptwi_data.irq) {
308 rc = request_irq(pmcmsptwi_data.irq, &pmcmsptwi_interrupt,
309 IRQF_SHARED | IRQF_DISABLED | IRQF_SAMPLE_RANDOM,
310 pldev->name, &pmcmsptwi_data);
311 if (rc == 0) {
312 /*
313 * Enable 'DONE' interrupt only.
314 *
315 * If you enable all interrupts, you will get one on
316 * error and another when the operation completes.
317 * This way you only have to handle one interrupt,
318 * but you can still check all result flags.
319 */
320 pmcmsptwi_writel(MSP_TWI_INT_STS_DONE,
321 pmcmsptwi_data.iobase +
322 MSP_TWI_INT_MSK_REG_OFFSET);
323 } else {
324 dev_warn(&pldev->dev,
325 "Could not assign TWI IRQ handler "
326 "to irq %d (continuing with poll)\n",
327 pmcmsptwi_data.irq);
328 pmcmsptwi_data.irq = 0;
329 }
330 }
331
332 init_completion(&pmcmsptwi_data.wait);
333 mutex_init(&pmcmsptwi_data.lock);
334
335 pmcmsptwi_set_clock_config(&pmcmsptwi_defclockcfg, &pmcmsptwi_data);
336 pmcmsptwi_set_twi_config(&pmcmsptwi_defcfg, &pmcmsptwi_data);
337
338 printk(KERN_INFO DRV_NAME ": Registering MSP71xx I2C adapter\n");
339
340 pmcmsptwi_adapter.dev.parent = &pldev->dev;
341 platform_set_drvdata(pldev, &pmcmsptwi_adapter);
342 i2c_set_adapdata(&pmcmsptwi_adapter, &pmcmsptwi_data);
343
344 rc = i2c_add_adapter(&pmcmsptwi_adapter);
345 if (rc) {
346 dev_err(&pldev->dev, "Unable to register I2C adapter\n");
347 goto ret_unmap;
348 }
349
350 return 0;
351
352ret_unmap:
353 platform_set_drvdata(pldev, NULL);
354 if (pmcmsptwi_data.irq) {
355 pmcmsptwi_writel(0,
356 pmcmsptwi_data.iobase + MSP_TWI_INT_MSK_REG_OFFSET);
357 free_irq(pmcmsptwi_data.irq, &pmcmsptwi_data);
358 }
359
360 iounmap(pmcmsptwi_data.iobase);
361
362ret_unreserve:
363 release_mem_region(res->start, res->end - res->start + 1);
364
365ret_err:
366 return rc;
367}
368
369/*
370 * Release the device and return 0 if there is one.
371 */
372static int __devexit pmcmsptwi_remove(struct platform_device *pldev)
373{
374 struct resource *res;
375
376 i2c_del_adapter(&pmcmsptwi_adapter);
377
378 platform_set_drvdata(pldev, NULL);
379 if (pmcmsptwi_data.irq) {
380 pmcmsptwi_writel(0,
381 pmcmsptwi_data.iobase + MSP_TWI_INT_MSK_REG_OFFSET);
382 free_irq(pmcmsptwi_data.irq, &pmcmsptwi_data);
383 }
384
385 iounmap(pmcmsptwi_data.iobase);
386
387 res = platform_get_resource(pldev, IORESOURCE_MEM, 0);
388 release_mem_region(res->start, res->end - res->start + 1);
389
390 return 0;
391}
392
393/*
394 * Polls the 'busy' register until the command is complete.
395 * NOTE: Assumes data->lock is held.
396 */
397static void pmcmsptwi_poll_complete(struct pmcmsptwi_data *data)
398{
399 int i;
400
401 for (i = 0; i < MSP_MAX_POLL; i++) {
402 u32 val = pmcmsptwi_readl(data->iobase +
403 MSP_TWI_BUSY_REG_OFFSET);
404 if (val == 0) {
405 u32 reason = pmcmsptwi_readl(data->iobase +
406 MSP_TWI_INT_STS_REG_OFFSET);
407 pmcmsptwi_writel(reason, data->iobase +
408 MSP_TWI_INT_STS_REG_OFFSET);
409 data->last_result = pmcmsptwi_get_result(reason);
410 return;
411 }
412 udelay(MSP_POLL_DELAY);
413 }
414
415 dev_dbg(&pmcmsptwi_adapter.dev, "Result: Poll timeout\n");
416 data->last_result = MSP_TWI_XFER_TIMEOUT;
417}
418
419/*
420 * Do the transfer (low level):
421 * May use interrupt-driven or polling, depending on if an IRQ is
422 * presently registered.
423 * NOTE: Assumes data->lock is held.
424 */
425static enum pmcmsptwi_xfer_result pmcmsptwi_do_xfer(
426 u32 reg, struct pmcmsptwi_data *data)
427{
428 dev_dbg(&pmcmsptwi_adapter.dev, "Writing cmd reg 0x%08x\n", reg);
429 pmcmsptwi_writel(reg, data->iobase + MSP_TWI_CMD_REG_OFFSET);
430 if (data->irq) {
431 unsigned long timeleft = wait_for_completion_timeout(
432 &data->wait, MSP_IRQ_TIMEOUT);
433 if (timeleft == 0) {
434 dev_dbg(&pmcmsptwi_adapter.dev,
435 "Result: IRQ timeout\n");
436 complete(&data->wait);
437 data->last_result = MSP_TWI_XFER_TIMEOUT;
438 }
439 } else
440 pmcmsptwi_poll_complete(data);
441
442 return data->last_result;
443}
444
445/*
446 * Helper routine, converts 'pmctwi_cmd' struct to register format
447 */
448static inline u32 pmcmsptwi_cmd_to_reg(const struct pmcmsptwi_cmd *cmd)
449{
450 return ((cmd->type & 0x3) << 8) |
451 (((cmd->write_len - 1) & 0x7) << 4) |
452 ((cmd->read_len - 1) & 0x7);
453}
454
455/*
456 * Do the transfer (high level)
457 */
458static enum pmcmsptwi_xfer_result pmcmsptwi_xfer_cmd(
459 struct pmcmsptwi_cmd *cmd,
460 struct pmcmsptwi_data *data)
461{
462 enum pmcmsptwi_xfer_result retval;
463
464 if ((cmd->type == MSP_TWI_CMD_WRITE && cmd->write_len == 0) ||
465 (cmd->type == MSP_TWI_CMD_READ && cmd->read_len == 0) ||
466 (cmd->type == MSP_TWI_CMD_WRITE_READ &&
467 (cmd->read_len == 0 || cmd->write_len == 0))) {
468 dev_err(&pmcmsptwi_adapter.dev,
469 "%s: Cannot transfer less than 1 byte\n",
470 __FUNCTION__);
471 return -EINVAL;
472 }
473
474 if (cmd->read_len > MSP_MAX_BYTES_PER_RW ||
475 cmd->write_len > MSP_MAX_BYTES_PER_RW) {
476 dev_err(&pmcmsptwi_adapter.dev,
477 "%s: Cannot transfer more than %d bytes\n",
478 __FUNCTION__, MSP_MAX_BYTES_PER_RW);
479 return -EINVAL;
480 }
481
482 mutex_lock(&data->lock);
483 dev_dbg(&pmcmsptwi_adapter.dev,
484 "Setting address to 0x%04x\n", cmd->addr);
485 pmcmsptwi_writel(cmd->addr, data->iobase + MSP_TWI_ADD_REG_OFFSET);
486
487 if (cmd->type == MSP_TWI_CMD_WRITE ||
488 cmd->type == MSP_TWI_CMD_WRITE_READ) {
489 __be64 tmp = cpu_to_be64p((u64 *)cmd->write_data);
490 tmp >>= (MSP_MAX_BYTES_PER_RW - cmd->write_len) * 8;
491 dev_dbg(&pmcmsptwi_adapter.dev, "Writing 0x%016llx\n", tmp);
492 pmcmsptwi_writel(tmp & 0x00000000ffffffffLL,
493 data->iobase + MSP_TWI_DAT_0_REG_OFFSET);
494 if (cmd->write_len > 4)
495 pmcmsptwi_writel(tmp >> 32,
496 data->iobase + MSP_TWI_DAT_1_REG_OFFSET);
497 }
498
499 retval = pmcmsptwi_do_xfer(pmcmsptwi_cmd_to_reg(cmd), data);
500 if (retval != MSP_TWI_XFER_OK)
501 goto xfer_err;
502
503 if (cmd->type == MSP_TWI_CMD_READ ||
504 cmd->type == MSP_TWI_CMD_WRITE_READ) {
505 int i;
506 u64 rmsk = ~(0xffffffffffffffffLL << (cmd->read_len * 8));
507 u64 tmp = (u64)pmcmsptwi_readl(data->iobase +
508 MSP_TWI_DAT_0_REG_OFFSET);
509 if (cmd->read_len > 4)
510 tmp |= (u64)pmcmsptwi_readl(data->iobase +
511 MSP_TWI_DAT_1_REG_OFFSET) << 32;
512 tmp &= rmsk;
513 dev_dbg(&pmcmsptwi_adapter.dev, "Read 0x%016llx\n", tmp);
514
515 for (i = 0; i < cmd->read_len; i++)
516 cmd->read_data[i] = tmp >> i;
517 }
518
519xfer_err:
520 mutex_unlock(&data->lock);
521
522 return retval;
523}
524
525/* -- Algorithm functions -- */
526
527/*
528 * Sends an i2c command out on the adapter
529 */
530static int pmcmsptwi_master_xfer(struct i2c_adapter *adap,
531 struct i2c_msg *msg, int num)
532{
533 struct pmcmsptwi_data *data = i2c_get_adapdata(adap);
534 struct pmcmsptwi_cmd cmd;
535 struct pmcmsptwi_cfg oldcfg, newcfg;
536 int ret;
537
538 if (num > 2) {
539 dev_dbg(&adap->dev, "%d messages unsupported\n", num);
540 return -EINVAL;
541 } else if (num == 2) {
542 /* Check for a dual write-then-read command */
543 struct i2c_msg *nextmsg = msg + 1;
544 if (!(msg->flags & I2C_M_RD) &&
545 (nextmsg->flags & I2C_M_RD) &&
546 msg->addr == nextmsg->addr) {
547 cmd.type = MSP_TWI_CMD_WRITE_READ;
548 cmd.write_len = msg->len;
549 cmd.write_data = msg->buf;
550 cmd.read_len = nextmsg->len;
551 cmd.read_data = nextmsg->buf;
552 } else {
553 dev_dbg(&adap->dev,
554 "Non write-read dual messages unsupported\n");
555 return -EINVAL;
556 }
557 } else if (msg->flags & I2C_M_RD) {
558 cmd.type = MSP_TWI_CMD_READ;
559 cmd.read_len = msg->len;
560 cmd.read_data = msg->buf;
561 cmd.write_len = 0;
562 cmd.write_data = NULL;
563 } else {
564 cmd.type = MSP_TWI_CMD_WRITE;
565 cmd.read_len = 0;
566 cmd.read_data = NULL;
567 cmd.write_len = msg->len;
568 cmd.write_data = msg->buf;
569 }
570
571 if (msg->len == 0) {
572 dev_err(&adap->dev, "Zero-byte messages unsupported\n");
573 return -EINVAL;
574 }
575
576 cmd.addr = msg->addr;
577
578 if (msg->flags & I2C_M_TEN) {
579 pmcmsptwi_get_twi_config(&newcfg, data);
580 memcpy(&oldcfg, &newcfg, sizeof(oldcfg));
581
582 /* Set the special 10-bit address flag */
583 newcfg.add10 = 1;
584
585 pmcmsptwi_set_twi_config(&newcfg, data);
586 }
587
588 /* Execute the command */
589 ret = pmcmsptwi_xfer_cmd(&cmd, data);
590
591 if (msg->flags & I2C_M_TEN)
592 pmcmsptwi_set_twi_config(&oldcfg, data);
593
594 dev_dbg(&adap->dev, "I2C %s of %d bytes ",
595 (msg->flags & I2C_M_RD) ? "read" : "write", msg->len);
596 if (ret != MSP_TWI_XFER_OK) {
597 /*
598 * TODO: We could potentially loop and retry in the case
599 * of MSP_TWI_XFER_TIMEOUT.
600 */
601 dev_dbg(&adap->dev, "failed\n");
602 return -1;
603 }
604
605 dev_dbg(&adap->dev, "succeeded\n");
606 return 0;
607}
608
609static u32 pmcmsptwi_i2c_func(struct i2c_adapter *adapter)
610{
611 return I2C_FUNC_I2C | I2C_FUNC_10BIT_ADDR |
612 I2C_FUNC_SMBUS_BYTE | I2C_FUNC_SMBUS_BYTE_DATA |
613 I2C_FUNC_SMBUS_WORD_DATA | I2C_FUNC_SMBUS_PROC_CALL;
614}
615
616/* -- Initialization -- */
617
618static struct i2c_algorithm pmcmsptwi_algo = {
619 .master_xfer = pmcmsptwi_master_xfer,
620 .functionality = pmcmsptwi_i2c_func,
621};
622
623static struct i2c_adapter pmcmsptwi_adapter = {
624 .owner = THIS_MODULE,
625 .class = I2C_CLASS_HWMON,
626 .algo = &pmcmsptwi_algo,
627 .name = DRV_NAME,
628};
629
630static struct platform_driver pmcmsptwi_driver = {
631 .probe = pmcmsptwi_probe,
632 .remove = __devexit_p(pmcmsptwi_remove),
633 .driver = {
634 .name = DRV_NAME,
635 .owner = THIS_MODULE,
636 },
637};
638
639static int __init pmcmsptwi_init(void)
640{
641 return platform_driver_register(&pmcmsptwi_driver);
642}
643
644static void __exit pmcmsptwi_exit(void)
645{
646 platform_driver_unregister(&pmcmsptwi_driver);
647}
648
649MODULE_DESCRIPTION("PMC MSP TWI/SMBus/I2C driver");
650MODULE_LICENSE("GPL");
651
652module_init(pmcmsptwi_init);
653module_exit(pmcmsptwi_exit);
diff --git a/drivers/i2c/busses/i2c-powermac.c b/drivers/i2c/busses/i2c-powermac.c
index 1425d2245c82..0ab4f2627c26 100644
--- a/drivers/i2c/busses/i2c-powermac.c
+++ b/drivers/i2c/busses/i2c-powermac.c
@@ -121,8 +121,7 @@ static s32 i2c_powermac_smbus_xfer( struct i2c_adapter* adap,
121 if (rc) 121 if (rc)
122 goto bail; 122 goto bail;
123 rc = pmac_i2c_xfer(bus, addrdir, 1, command, 123 rc = pmac_i2c_xfer(bus, addrdir, 1, command,
124 read ? data->block : &data->block[1], 124 &data->block[1], data->block[0]);
125 data->block[0]);
126 break; 125 break;
127 126
128 default: 127 default:
diff --git a/drivers/i2c/busses/i2c-pxa.c b/drivers/i2c/busses/i2c-pxa.c
index 28e7b91a4553..9d6b790d4321 100644
--- a/drivers/i2c/busses/i2c-pxa.c
+++ b/drivers/i2c/busses/i2c-pxa.c
@@ -921,7 +921,14 @@ static int i2c_pxa_probe(struct platform_device *dev)
921 i2c->adap.class = plat->class; 921 i2c->adap.class = plat->class;
922 } 922 }
923 923
924 ret = i2c_add_adapter(&i2c->adap); 924 /*
925 * If "dev->id" is negative we consider it as zero.
926 * The reason to do so is to avoid sysfs names that only make
927 * sense when there are multiple adapters.
928 */
929 i2c->adap.nr = dev->id >= 0 ? dev->id : 0;
930
931 ret = i2c_add_numbered_adapter(&i2c->adap);
925 if (ret < 0) { 932 if (ret < 0) {
926 printk(KERN_INFO "I2C: Failed to add bus\n"); 933 printk(KERN_INFO "I2C: Failed to add bus\n");
927 goto eadapt; 934 goto eadapt;
diff --git a/drivers/i2c/busses/i2c-rpx.c b/drivers/i2c/busses/i2c-rpx.c
deleted file mode 100644
index 8764df06f51d..000000000000
--- a/drivers/i2c/busses/i2c-rpx.c
+++ /dev/null
@@ -1,101 +0,0 @@
1/*
2 * Embedded Planet RPX Lite MPC8xx CPM I2C interface.
3 * Copyright (c) 1999 Dan Malek (dmalek@jlc.net).
4 *
5 * moved into proper i2c interface;
6 * Brad Parker (brad@heeltoe.com)
7 *
8 * RPX lite specific parts of the i2c interface
9 * Update: There actually isn't anything RPXLite-specific about this module.
10 * This should work for most any 8xx board. The console messages have been
11 * changed to eliminate RPXLite references.
12 */
13
14#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/init.h>
17#include <linux/stddef.h>
18#include <linux/i2c.h>
19#include <linux/i2c-algo-8xx.h>
20#include <asm/mpc8xx.h>
21#include <asm/commproc.h>
22
23
24static void
25rpx_iic_init(struct i2c_algo_8xx_data *data)
26{
27 volatile cpm8xx_t *cp;
28 volatile immap_t *immap;
29
30 cp = cpmp; /* Get pointer to Communication Processor */
31 immap = (immap_t *)IMAP_ADDR; /* and to internal registers */
32
33 data->iip = (iic_t *)&cp->cp_dparam[PROFF_IIC];
34
35 /* Check for and use a microcode relocation patch.
36 */
37 if ((data->reloc = data->iip->iic_rpbase))
38 data->iip = (iic_t *)&cp->cp_dpmem[data->iip->iic_rpbase];
39
40 data->i2c = (i2c8xx_t *)&(immap->im_i2c);
41 data->cp = cp;
42
43 /* Initialize Port B IIC pins.
44 */
45 cp->cp_pbpar |= 0x00000030;
46 cp->cp_pbdir |= 0x00000030;
47 cp->cp_pbodr |= 0x00000030;
48
49 /* Allocate space for two transmit and two receive buffer
50 * descriptors in the DP ram.
51 */
52 data->dp_addr = cpm_dpalloc(sizeof(cbd_t) * 4, 8);
53
54 /* ptr to i2c area */
55 data->i2c = (i2c8xx_t *)&(((immap_t *)IMAP_ADDR)->im_i2c);
56}
57
58static int rpx_install_isr(int irq, void (*func)(void *), void *data)
59{
60 /* install interrupt handler */
61 cpm_install_handler(irq, func, data);
62
63 return 0;
64}
65
66static struct i2c_algo_8xx_data rpx_data = {
67 .setisr = rpx_install_isr
68};
69
70static struct i2c_adapter rpx_ops = {
71 .owner = THIS_MODULE,
72 .name = "m8xx",
73 .id = I2C_HW_MPC8XX_EPON,
74 .algo_data = &rpx_data,
75};
76
77int __init i2c_rpx_init(void)
78{
79 printk(KERN_INFO "i2c-rpx: i2c MPC8xx driver\n");
80
81 /* reset hardware to sane state */
82 rpx_iic_init(&rpx_data);
83
84 if (i2c_8xx_add_bus(&rpx_ops) < 0) {
85 printk(KERN_ERR "i2c-rpx: Unable to register with I2C\n");
86 return -ENODEV;
87 }
88
89 return 0;
90}
91
92void __exit i2c_rpx_exit(void)
93{
94 i2c_8xx_del_bus(&rpx_ops);
95}
96
97MODULE_AUTHOR("Dan Malek <dmalek@jlc.net>");
98MODULE_DESCRIPTION("I2C-Bus adapter routines for MPC8xx boards");
99
100module_init(i2c_rpx_init);
101module_exit(i2c_rpx_exit);
diff --git a/drivers/i2c/busses/i2c-savage4.c b/drivers/i2c/busses/i2c-savage4.c
index b7fb65c30112..8adf4abaa035 100644
--- a/drivers/i2c/busses/i2c-savage4.c
+++ b/drivers/i2c/busses/i2c-savage4.c
@@ -25,8 +25,6 @@
25/* This interfaces to the I2C bus of the Savage4 to gain access to 25/* This interfaces to the I2C bus of the Savage4 to gain access to
26 the BT869 and possibly other I2C devices. The DDC bus is not 26 the BT869 and possibly other I2C devices. The DDC bus is not
27 yet supported because its register is not memory-mapped. 27 yet supported because its register is not memory-mapped.
28 However we leave the DDC code here, commented out, to make
29 it easier to add later.
30*/ 28*/
31 29
32#include <linux/kernel.h> 30#include <linux/kernel.h>
@@ -37,36 +35,19 @@
37#include <linux/i2c-algo-bit.h> 35#include <linux/i2c-algo-bit.h>
38#include <asm/io.h> 36#include <asm/io.h>
39 37
40/* 3DFX defines */ 38/* device IDs */
41#define PCI_CHIP_SAVAGE3D 0x8A20
42#define PCI_CHIP_SAVAGE3D_MV 0x8A21
43#define PCI_CHIP_SAVAGE4 0x8A22 39#define PCI_CHIP_SAVAGE4 0x8A22
44#define PCI_CHIP_SAVAGE2000 0x9102 40#define PCI_CHIP_SAVAGE2000 0x9102
45#define PCI_CHIP_PROSAVAGE_PM 0x8A25
46#define PCI_CHIP_PROSAVAGE_KM 0x8A26
47#define PCI_CHIP_SAVAGE_MX_MV 0x8c10
48#define PCI_CHIP_SAVAGE_MX 0x8c11
49#define PCI_CHIP_SAVAGE_IX_MV 0x8c12
50#define PCI_CHIP_SAVAGE_IX 0x8c13
51 41
52#define REG 0xff20 /* Serial Port 1 Register */ 42#define REG 0xff20 /* Serial Port 1 Register */
53 43
54/* bit locations in the register */ 44/* bit locations in the register */
55#define DDC_ENAB 0x00040000
56#define DDC_SCL_OUT 0x00080000
57#define DDC_SDA_OUT 0x00100000
58#define DDC_SCL_IN 0x00200000
59#define DDC_SDA_IN 0x00400000
60#define I2C_ENAB 0x00000020 45#define I2C_ENAB 0x00000020
61#define I2C_SCL_OUT 0x00000001 46#define I2C_SCL_OUT 0x00000001
62#define I2C_SDA_OUT 0x00000002 47#define I2C_SDA_OUT 0x00000002
63#define I2C_SCL_IN 0x00000008 48#define I2C_SCL_IN 0x00000008
64#define I2C_SDA_IN 0x00000010 49#define I2C_SDA_IN 0x00000010
65 50
66/* initialization states */
67#define INIT2 0x20
68#define INIT3 0x04
69
70/* delays */ 51/* delays */
71#define CYCLE_DELAY 10 52#define CYCLE_DELAY 10
72#define TIMEOUT (HZ / 2) 53#define TIMEOUT (HZ / 2)
diff --git a/drivers/i2c/busses/i2c-sis5595.c b/drivers/i2c/busses/i2c-sis5595.c
index a6feed449dbe..283769cecee2 100644
--- a/drivers/i2c/busses/i2c-sis5595.c
+++ b/drivers/i2c/busses/i2c-sis5595.c
@@ -129,6 +129,7 @@ MODULE_PARM_DESC(force_addr, "Initialize the base address of the i2c controller"
129 129
130static struct pci_driver sis5595_driver; 130static struct pci_driver sis5595_driver;
131static unsigned short sis5595_base; 131static unsigned short sis5595_base;
132static struct pci_dev *sis5595_pdev;
132 133
133static u8 sis5595_read(u8 reg) 134static u8 sis5595_read(u8 reg)
134{ 135{
@@ -379,6 +380,8 @@ MODULE_DEVICE_TABLE (pci, sis5595_ids);
379 380
380static int __devinit sis5595_probe(struct pci_dev *dev, const struct pci_device_id *id) 381static int __devinit sis5595_probe(struct pci_dev *dev, const struct pci_device_id *id)
381{ 382{
383 int err;
384
382 if (sis5595_setup(dev)) { 385 if (sis5595_setup(dev)) {
383 dev_err(&dev->dev, "SIS5595 not detected, module not inserted.\n"); 386 dev_err(&dev->dev, "SIS5595 not detected, module not inserted.\n");
384 return -ENODEV; 387 return -ENODEV;
@@ -389,20 +392,24 @@ static int __devinit sis5595_probe(struct pci_dev *dev, const struct pci_device_
389 392
390 sprintf(sis5595_adapter.name, "SMBus SIS5595 adapter at %04x", 393 sprintf(sis5595_adapter.name, "SMBus SIS5595 adapter at %04x",
391 sis5595_base + SMB_INDEX); 394 sis5595_base + SMB_INDEX);
392 return i2c_add_adapter(&sis5595_adapter); 395 err = i2c_add_adapter(&sis5595_adapter);
393} 396 if (err) {
397 release_region(sis5595_base + SMB_INDEX, 2);
398 return err;
399 }
394 400
395static void __devexit sis5595_remove(struct pci_dev *dev) 401 /* Always return failure here. This is to allow other drivers to bind
396{ 402 * to this pci device. We don't really want to have control over the
397 i2c_del_adapter(&sis5595_adapter); 403 * pci device, we only wanted to read as few register values from it.
398 release_region(sis5595_base + SMB_INDEX, 2); 404 */
405 sis5595_pdev = pci_dev_get(dev);
406 return -ENODEV;
399} 407}
400 408
401static struct pci_driver sis5595_driver = { 409static struct pci_driver sis5595_driver = {
402 .name = "sis5595_smbus", 410 .name = "sis5595_smbus",
403 .id_table = sis5595_ids, 411 .id_table = sis5595_ids,
404 .probe = sis5595_probe, 412 .probe = sis5595_probe,
405 .remove = __devexit_p(sis5595_remove),
406}; 413};
407 414
408static int __init i2c_sis5595_init(void) 415static int __init i2c_sis5595_init(void)
@@ -413,6 +420,12 @@ static int __init i2c_sis5595_init(void)
413static void __exit i2c_sis5595_exit(void) 420static void __exit i2c_sis5595_exit(void)
414{ 421{
415 pci_unregister_driver(&sis5595_driver); 422 pci_unregister_driver(&sis5595_driver);
423 if (sis5595_pdev) {
424 i2c_del_adapter(&sis5595_adapter);
425 release_region(sis5595_base + SMB_INDEX, 2);
426 pci_dev_put(sis5595_pdev);
427 sis5595_pdev = NULL;
428 }
416} 429}
417 430
418MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>"); 431MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>");
diff --git a/drivers/i2c/busses/i2c-taos-evm.c b/drivers/i2c/busses/i2c-taos-evm.c
new file mode 100644
index 000000000000..1b0cfd5472fd
--- /dev/null
+++ b/drivers/i2c/busses/i2c-taos-evm.c
@@ -0,0 +1,330 @@
1/*
2 * Driver for the TAOS evaluation modules
3 * These devices include an I2C master which can be controlled over the
4 * serial port.
5 *
6 * Copyright (C) 2007 Jean Delvare <khali@linux-fr.org>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22#include <linux/delay.h>
23#include <linux/module.h>
24#include <linux/slab.h>
25#include <linux/interrupt.h>
26#include <linux/input.h>
27#include <linux/serio.h>
28#include <linux/init.h>
29#include <linux/i2c.h>
30
31#define TAOS_BUFFER_SIZE 63
32
33#define TAOS_STATE_INIT 0
34#define TAOS_STATE_IDLE 1
35#define TAOS_STATE_SEND 2
36#define TAOS_STATE_RECV 3
37
38#define TAOS_CMD_RESET 0x12
39
40static DECLARE_WAIT_QUEUE_HEAD(wq);
41
42struct taos_data {
43 struct i2c_adapter adapter;
44 struct i2c_client *client;
45 int state;
46 u8 addr; /* last used address */
47 unsigned char buffer[TAOS_BUFFER_SIZE];
48 unsigned int pos; /* position inside the buffer */
49};
50
51/* TAOS TSL2550 EVM */
52static struct i2c_board_info tsl2550_info = {
53 I2C_BOARD_INFO("tsl2550", 0x39),
54 .type = "tsl2550",
55};
56
57/* Instantiate i2c devices based on the adapter name */
58static struct i2c_client *taos_instantiate_device(struct i2c_adapter *adapter)
59{
60 if (!strncmp(adapter->name, "TAOS TSL2550 EVM", 16)) {
61 dev_info(&adapter->dev, "Instantiating device %s at 0x%02x\n",
62 tsl2550_info.driver_name, tsl2550_info.addr);
63 return i2c_new_device(adapter, &tsl2550_info);
64 }
65
66 return NULL;
67}
68
69static int taos_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
70 unsigned short flags, char read_write, u8 command,
71 int size, union i2c_smbus_data *data)
72{
73 struct serio *serio = adapter->algo_data;
74 struct taos_data *taos = serio_get_drvdata(serio);
75 char *p;
76
77 /* Encode our transaction. "@" is for the device address, "$" for the
78 SMBus command and "#" for the data. */
79 p = taos->buffer;
80
81 /* The device remembers the last used address, no need to send it
82 again if it's the same */
83 if (addr != taos->addr)
84 p += sprintf(p, "@%02X", addr);
85
86 switch (size) {
87 case I2C_SMBUS_BYTE:
88 if (read_write == I2C_SMBUS_WRITE)
89 sprintf(p, "$#%02X", command);
90 else
91 sprintf(p, "$");
92 break;
93 case I2C_SMBUS_BYTE_DATA:
94 if (read_write == I2C_SMBUS_WRITE)
95 sprintf(p, "$%02X#%02X", command, data->byte);
96 else
97 sprintf(p, "$%02X", command);
98 break;
99 default:
100 dev_dbg(&adapter->dev, "Unsupported transaction size %d\n",
101 size);
102 return -EINVAL;
103 }
104
105 /* Send the transaction to the TAOS EVM */
106 dev_dbg(&adapter->dev, "Command buffer: %s\n", taos->buffer);
107 taos->pos = 0;
108 taos->state = TAOS_STATE_SEND;
109 serio_write(serio, taos->buffer[0]);
110 wait_event_interruptible_timeout(wq, taos->state == TAOS_STATE_IDLE,
111 msecs_to_jiffies(250));
112 if (taos->state != TAOS_STATE_IDLE) {
113 dev_err(&adapter->dev, "Transaction failed "
114 "(state=%d, pos=%d)\n", taos->state, taos->pos);
115 taos->addr = 0;
116 return -EIO;
117 }
118 taos->addr = addr;
119
120 /* Start the transaction and read the answer */
121 taos->pos = 0;
122 taos->state = TAOS_STATE_RECV;
123 serio_write(serio, read_write == I2C_SMBUS_WRITE ? '>' : '<');
124 wait_event_interruptible_timeout(wq, taos->state == TAOS_STATE_IDLE,
125 msecs_to_jiffies(150));
126 if (taos->state != TAOS_STATE_IDLE
127 || taos->pos != 6) {
128 dev_err(&adapter->dev, "Transaction timeout (pos=%d)\n",
129 taos->pos);
130 return -EIO;
131 }
132 dev_dbg(&adapter->dev, "Answer buffer: %s\n", taos->buffer);
133
134 /* Interpret the returned string */
135 p = taos->buffer + 2;
136 p[3] = '\0';
137 if (!strcmp(p, "NAK"))
138 return -ENODEV;
139
140 if (read_write == I2C_SMBUS_WRITE) {
141 if (!strcmp(p, "ACK"))
142 return 0;
143 } else {
144 if (p[0] == 'x') {
145 data->byte = simple_strtol(p + 1, NULL, 16);
146 return 0;
147 }
148 }
149
150 return -EIO;
151}
152
153static u32 taos_smbus_func(struct i2c_adapter *adapter)
154{
155 return I2C_FUNC_SMBUS_BYTE | I2C_FUNC_SMBUS_BYTE_DATA;
156}
157
158static const struct i2c_algorithm taos_algorithm = {
159 .smbus_xfer = taos_smbus_xfer,
160 .functionality = taos_smbus_func,
161};
162
163static irqreturn_t taos_interrupt(struct serio *serio, unsigned char data,
164 unsigned int flags)
165{
166 struct taos_data *taos = serio_get_drvdata(serio);
167
168 switch (taos->state) {
169 case TAOS_STATE_INIT:
170 taos->buffer[taos->pos++] = data;
171 if (data == ':'
172 || taos->pos == TAOS_BUFFER_SIZE - 1) {
173 taos->buffer[taos->pos] = '\0';
174 taos->state = TAOS_STATE_IDLE;
175 wake_up_interruptible(&wq);
176 }
177 break;
178 case TAOS_STATE_SEND:
179 if (taos->buffer[++taos->pos])
180 serio_write(serio, taos->buffer[taos->pos]);
181 else {
182 taos->state = TAOS_STATE_IDLE;
183 wake_up_interruptible(&wq);
184 }
185 break;
186 case TAOS_STATE_RECV:
187 taos->buffer[taos->pos++] = data;
188 if (data == ']') {
189 taos->buffer[taos->pos] = '\0';
190 taos->state = TAOS_STATE_IDLE;
191 wake_up_interruptible(&wq);
192 }
193 break;
194 }
195
196 return IRQ_HANDLED;
197}
198
199/* Extract the adapter name from the buffer received after reset.
200 The buffer is modified and a pointer inside the buffer is returned. */
201static char *taos_adapter_name(char *buffer)
202{
203 char *start, *end;
204
205 start = strstr(buffer, "TAOS ");
206 if (!start)
207 return NULL;
208
209 end = strchr(start, '\r');
210 if (!end)
211 return NULL;
212 *end = '\0';
213
214 return start;
215}
216
217static int taos_connect(struct serio *serio, struct serio_driver *drv)
218{
219 struct taos_data *taos;
220 struct i2c_adapter *adapter;
221 char *name;
222 int err;
223
224 taos = kzalloc(sizeof(struct taos_data), GFP_KERNEL);
225 if (!taos) {
226 err = -ENOMEM;
227 goto exit;
228 }
229 taos->state = TAOS_STATE_INIT;
230 serio_set_drvdata(serio, taos);
231
232 err = serio_open(serio, drv);
233 if (err)
234 goto exit_kfree;
235
236 adapter = &taos->adapter;
237 adapter->owner = THIS_MODULE;
238 adapter->algo = &taos_algorithm;
239 adapter->algo_data = serio;
240 adapter->dev.parent = &serio->dev;
241
242 /* Reset the TAOS evaluation module to identify it */
243 serio_write(serio, TAOS_CMD_RESET);
244 wait_event_interruptible_timeout(wq, taos->state == TAOS_STATE_IDLE,
245 msecs_to_jiffies(2000));
246
247 if (taos->state != TAOS_STATE_IDLE) {
248 err = -ENODEV;
249 dev_dbg(&serio->dev, "TAOS EVM reset failed (state=%d, "
250 "pos=%d)\n", taos->state, taos->pos);
251 goto exit_close;
252 }
253
254 name = taos_adapter_name(taos->buffer);
255 if (!name) {
256 err = -ENODEV;
257 dev_err(&serio->dev, "TAOS EVM identification failed\n");
258 goto exit_close;
259 }
260 strlcpy(adapter->name, name, sizeof(adapter->name));
261
262 err = i2c_add_adapter(adapter);
263 if (err)
264 goto exit_close;
265 dev_dbg(&serio->dev, "Connected to TAOS EVM\n");
266
267 taos->client = taos_instantiate_device(adapter);
268 return 0;
269
270 exit_close:
271 serio_close(serio);
272 exit_kfree:
273 serio_set_drvdata(serio, NULL);
274 kfree(taos);
275 exit:
276 return err;
277}
278
279static void taos_disconnect(struct serio *serio)
280{
281 struct taos_data *taos = serio_get_drvdata(serio);
282
283 if (taos->client)
284 i2c_unregister_device(taos->client);
285 i2c_del_adapter(&taos->adapter);
286 serio_close(serio);
287 serio_set_drvdata(serio, NULL);
288 kfree(taos);
289
290 dev_dbg(&serio->dev, "Disconnected from TAOS EVM\n");
291}
292
293static struct serio_device_id taos_serio_ids[] = {
294 {
295 .type = SERIO_RS232,
296 .proto = SERIO_TAOSEVM,
297 .id = SERIO_ANY,
298 .extra = SERIO_ANY,
299 },
300 { 0 }
301};
302MODULE_DEVICE_TABLE(serio, taos_serio_ids);
303
304static struct serio_driver taos_drv = {
305 .driver = {
306 .name = "taos-evm",
307 },
308 .description = "TAOS evaluation module driver",
309 .id_table = taos_serio_ids,
310 .connect = taos_connect,
311 .disconnect = taos_disconnect,
312 .interrupt = taos_interrupt,
313};
314
315static int __init taos_init(void)
316{
317 return serio_register_driver(&taos_drv);
318}
319
320static void __exit taos_exit(void)
321{
322 serio_unregister_driver(&taos_drv);
323}
324
325MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
326MODULE_DESCRIPTION("TAOS evaluation module driver");
327MODULE_LICENSE("GPL");
328
329module_init(taos_init);
330module_exit(taos_exit);
diff --git a/drivers/i2c/busses/i2c-viapro.c b/drivers/i2c/busses/i2c-viapro.c
index 7a2bc06304fc..edc275002f80 100644
--- a/drivers/i2c/busses/i2c-viapro.c
+++ b/drivers/i2c/busses/i2c-viapro.c
@@ -235,7 +235,7 @@ static s32 vt596_access(struct i2c_adapter *adap, u16 addr,
235 if (!(vt596_features & FEATURE_I2CBLOCK)) 235 if (!(vt596_features & FEATURE_I2CBLOCK))
236 goto exit_unsupported; 236 goto exit_unsupported;
237 if (read_write == I2C_SMBUS_READ) 237 if (read_write == I2C_SMBUS_READ)
238 outb_p(I2C_SMBUS_BLOCK_MAX, SMBHSTDAT0); 238 outb_p(data->block[0], SMBHSTDAT0);
239 /* Fall through */ 239 /* Fall through */
240 case I2C_SMBUS_BLOCK_DATA: 240 case I2C_SMBUS_BLOCK_DATA:
241 outb_p(command, SMBHSTCMD); 241 outb_p(command, SMBHSTCMD);
@@ -397,8 +397,7 @@ found:
397 case PCI_DEVICE_ID_VIA_82C686_4: 397 case PCI_DEVICE_ID_VIA_82C686_4:
398 /* The VT82C686B (rev 0x40) does support I2C block 398 /* The VT82C686B (rev 0x40) does support I2C block
399 transactions, but the VT82C686A (rev 0x30) doesn't */ 399 transactions, but the VT82C686A (rev 0x30) doesn't */
400 if (!pci_read_config_byte(pdev, PCI_REVISION_ID, &temp) 400 if (pdev->revision >= 0x40)
401 && temp >= 0x40)
402 vt596_features |= FEATURE_I2CBLOCK; 401 vt596_features |= FEATURE_I2CBLOCK;
403 break; 402 break;
404 } 403 }
diff --git a/drivers/i2c/busses/scx200_acb.c b/drivers/i2c/busses/scx200_acb.c
index 0d6bd4f7b7fa..e6c4a2b762ec 100644
--- a/drivers/i2c/busses/scx200_acb.c
+++ b/drivers/i2c/busses/scx200_acb.c
@@ -310,8 +310,6 @@ static s32 scx200_acb_smbus_xfer(struct i2c_adapter *adapter,
310 break; 310 break;
311 311
312 case I2C_SMBUS_I2C_BLOCK_DATA: 312 case I2C_SMBUS_I2C_BLOCK_DATA:
313 if (rw == I2C_SMBUS_READ)
314 data->block[0] = I2C_SMBUS_BLOCK_MAX; /* For now */
315 len = data->block[0]; 313 len = data->block[0];
316 if (len == 0 || len > I2C_SMBUS_BLOCK_MAX) 314 if (len == 0 || len > I2C_SMBUS_BLOCK_MAX)
317 return -EINVAL; 315 return -EINVAL;
@@ -388,7 +386,7 @@ static const struct i2c_algorithm scx200_acb_algorithm = {
388}; 386};
389 387
390static struct scx200_acb_iface *scx200_acb_list; 388static struct scx200_acb_iface *scx200_acb_list;
391static DECLARE_MUTEX(scx200_acb_list_mutex); 389static DEFINE_MUTEX(scx200_acb_list_mutex);
392 390
393static __init int scx200_acb_probe(struct scx200_acb_iface *iface) 391static __init int scx200_acb_probe(struct scx200_acb_iface *iface)
394{ 392{
@@ -472,10 +470,10 @@ static int __init scx200_acb_create(struct scx200_acb_iface *iface)
472 return -ENODEV; 470 return -ENODEV;
473 } 471 }
474 472
475 down(&scx200_acb_list_mutex); 473 mutex_lock(&scx200_acb_list_mutex);
476 iface->next = scx200_acb_list; 474 iface->next = scx200_acb_list;
477 scx200_acb_list = iface; 475 scx200_acb_list = iface;
478 up(&scx200_acb_list_mutex); 476 mutex_unlock(&scx200_acb_list_mutex);
479 477
480 return 0; 478 return 0;
481} 479}
@@ -633,10 +631,10 @@ static void __exit scx200_acb_cleanup(void)
633{ 631{
634 struct scx200_acb_iface *iface; 632 struct scx200_acb_iface *iface;
635 633
636 down(&scx200_acb_list_mutex); 634 mutex_lock(&scx200_acb_list_mutex);
637 while ((iface = scx200_acb_list) != NULL) { 635 while ((iface = scx200_acb_list) != NULL) {
638 scx200_acb_list = iface->next; 636 scx200_acb_list = iface->next;
639 up(&scx200_acb_list_mutex); 637 mutex_unlock(&scx200_acb_list_mutex);
640 638
641 i2c_del_adapter(&iface->adapter); 639 i2c_del_adapter(&iface->adapter);
642 640
@@ -648,9 +646,9 @@ static void __exit scx200_acb_cleanup(void)
648 release_region(iface->base, 8); 646 release_region(iface->base, 8);
649 647
650 kfree(iface); 648 kfree(iface);
651 down(&scx200_acb_list_mutex); 649 mutex_lock(&scx200_acb_list_mutex);
652 } 650 }
653 up(&scx200_acb_list_mutex); 651 mutex_unlock(&scx200_acb_list_mutex);
654} 652}
655 653
656module_init(scx200_acb_init); 654module_init(scx200_acb_init);
diff --git a/drivers/i2c/chips/Kconfig b/drivers/i2c/chips/Kconfig
index ea085a006ead..2e1c24f671cf 100644
--- a/drivers/i2c/chips/Kconfig
+++ b/drivers/i2c/chips/Kconfig
@@ -5,7 +5,7 @@
5menu "Miscellaneous I2C Chip support" 5menu "Miscellaneous I2C Chip support"
6 6
7config SENSORS_DS1337 7config SENSORS_DS1337
8 tristate "Dallas Semiconductor DS1337 and DS1339 Real Time Clock" 8 tristate "Dallas DS1337 and DS1339 Real Time Clock (DEPRECATED)"
9 depends on EXPERIMENTAL 9 depends on EXPERIMENTAL
10 help 10 help
11 If you say yes here you get support for Dallas Semiconductor 11 If you say yes here you get support for Dallas Semiconductor
@@ -14,8 +14,11 @@ config SENSORS_DS1337
14 This driver can also be built as a module. If so, the module 14 This driver can also be built as a module. If so, the module
15 will be called ds1337. 15 will be called ds1337.
16 16
17 This driver is deprecated and will be dropped soon. Use
18 rtc-ds1307 instead.
19
17config SENSORS_DS1374 20config SENSORS_DS1374
18 tristate "Maxim/Dallas Semiconductor DS1374 Real Time Clock" 21 tristate "Dallas DS1374 Real Time Clock (DEPRECATED)"
19 depends on EXPERIMENTAL 22 depends on EXPERIMENTAL
20 help 23 help
21 If you say yes here you get support for Dallas Semiconductor 24 If you say yes here you get support for Dallas Semiconductor
@@ -24,6 +27,19 @@ config SENSORS_DS1374
24 This driver can also be built as a module. If so, the module 27 This driver can also be built as a module. If so, the module
25 will be called ds1374. 28 will be called ds1374.
26 29
30 This driver is deprecated and will be dropped soon. Use
31 rtc-ds1374 instead.
32
33config DS1682
34 tristate "Dallas DS1682 Total Elapsed Time Recorder with Alarm"
35 depends on EXPERIMENTAL
36 help
37 If you say yes here you get support for Dallas Semiconductor
38 DS1682 Total Elapsed Time Recorder.
39
40 This driver can also be built as a module. If so, the module
41 will be called ds1682.
42
27config SENSORS_EEPROM 43config SENSORS_EEPROM
28 tristate "EEPROM reader" 44 tristate "EEPROM reader"
29 depends on EXPERIMENTAL 45 depends on EXPERIMENTAL
@@ -101,7 +117,7 @@ config TPS65010
101 will be called tps65010. 117 will be called tps65010.
102 118
103config SENSORS_M41T00 119config SENSORS_M41T00
104 tristate "ST M41T00 RTC chip" 120 tristate "ST M41T00 RTC chip (DEPRECATED)"
105 depends on PPC32 121 depends on PPC32
106 help 122 help
107 If you say yes here you get support for the ST M41T00 RTC chip. 123 If you say yes here you get support for the ST M41T00 RTC chip.
@@ -109,6 +125,9 @@ config SENSORS_M41T00
109 This driver can also be built as a module. If so, the module 125 This driver can also be built as a module. If so, the module
110 will be called m41t00. 126 will be called m41t00.
111 127
128 This driver is deprecated and will be dropped soon. Use
129 rtc-ds1307 or rtc-m41t80 instead.
130
112config SENSORS_MAX6875 131config SENSORS_MAX6875
113 tristate "Maxim MAX6875 Power supply supervisor" 132 tristate "Maxim MAX6875 Power supply supervisor"
114 depends on EXPERIMENTAL 133 depends on EXPERIMENTAL
@@ -124,4 +143,24 @@ config SENSORS_MAX6875
124 This driver can also be built as a module. If so, the module 143 This driver can also be built as a module. If so, the module
125 will be called max6875. 144 will be called max6875.
126 145
146config SENSORS_TSL2550
147 tristate "Taos TSL2550 ambient light sensor"
148 depends on EXPERIMENTAL
149 help
150 If you say yes here you get support for the Taos TSL2550
151 ambient light sensor.
152
153 This driver can also be built as a module. If so, the module
154 will be called tsl2550.
155
156config MENELAUS
157 bool "TWL92330/Menelaus PM chip"
158 depends on I2C=y && ARCH_OMAP24XX
159 help
160 If you say yes here you get support for the Texas Instruments
161 TWL92330/Menelaus Power Management chip. This include voltage
162 regulators, Dual slot memory card tranceivers, real-time clock
163 and other features that are often used in portable devices like
164 cell phones and PDAs.
165
127endmenu 166endmenu
diff --git a/drivers/i2c/chips/Makefile b/drivers/i2c/chips/Makefile
index 779868ef2e26..ca924e105959 100644
--- a/drivers/i2c/chips/Makefile
+++ b/drivers/i2c/chips/Makefile
@@ -4,6 +4,7 @@
4 4
5obj-$(CONFIG_SENSORS_DS1337) += ds1337.o 5obj-$(CONFIG_SENSORS_DS1337) += ds1337.o
6obj-$(CONFIG_SENSORS_DS1374) += ds1374.o 6obj-$(CONFIG_SENSORS_DS1374) += ds1374.o
7obj-$(CONFIG_DS1682) += ds1682.o
7obj-$(CONFIG_SENSORS_EEPROM) += eeprom.o 8obj-$(CONFIG_SENSORS_EEPROM) += eeprom.o
8obj-$(CONFIG_SENSORS_MAX6875) += max6875.o 9obj-$(CONFIG_SENSORS_MAX6875) += max6875.o
9obj-$(CONFIG_SENSORS_M41T00) += m41t00.o 10obj-$(CONFIG_SENSORS_M41T00) += m41t00.o
@@ -12,6 +13,8 @@ obj-$(CONFIG_SENSORS_PCF8574) += pcf8574.o
12obj-$(CONFIG_SENSORS_PCF8591) += pcf8591.o 13obj-$(CONFIG_SENSORS_PCF8591) += pcf8591.o
13obj-$(CONFIG_ISP1301_OMAP) += isp1301_omap.o 14obj-$(CONFIG_ISP1301_OMAP) += isp1301_omap.o
14obj-$(CONFIG_TPS65010) += tps65010.o 15obj-$(CONFIG_TPS65010) += tps65010.o
16obj-$(CONFIG_MENELAUS) += menelaus.o
17obj-$(CONFIG_SENSORS_TSL2550) += tsl2550.o
15 18
16ifeq ($(CONFIG_I2C_DEBUG_CHIP),y) 19ifeq ($(CONFIG_I2C_DEBUG_CHIP),y)
17EXTRA_CFLAGS += -DDEBUG 20EXTRA_CFLAGS += -DDEBUG
diff --git a/drivers/i2c/chips/ds1682.c b/drivers/i2c/chips/ds1682.c
new file mode 100644
index 000000000000..5879f0f25495
--- /dev/null
+++ b/drivers/i2c/chips/ds1682.c
@@ -0,0 +1,259 @@
1/*
2 * Dallas Semiconductor DS1682 Elapsed Time Recorder device driver
3 *
4 * Written by: Grant Likely <grant.likely@secretlab.ca>
5 *
6 * Copyright (C) 2007 Secret Lab Technologies Ltd.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12
13/*
14 * The DS1682 elapsed timer recorder is a simple device that implements
15 * one elapsed time counter, one event counter, an alarm signal and 10
16 * bytes of general purpose EEPROM.
17 *
18 * This driver provides access to the DS1682 counters and user data via
19 * the sysfs. The following attributes are added to the device node:
20 * elapsed_time (u32): Total elapsed event time in ms resolution
21 * alarm_time (u32): When elapsed time exceeds the value in alarm_time,
22 * then the alarm pin is asserted.
23 * event_count (u16): number of times the event pin has gone low.
24 * eeprom (u8[10]): general purpose EEPROM
25 *
26 * Counter registers and user data are both read/write unless the device
27 * has been write protected. This driver does not support turning off write
28 * protection. Once write protection is turned on, it is impossible to
29 * turn it off again, so I have left the feature out of this driver to avoid
30 * accidental enabling, but it is trivial to add write protect support.
31 *
32 */
33
34#include <linux/module.h>
35#include <linux/init.h>
36#include <linux/slab.h>
37#include <linux/i2c.h>
38#include <linux/string.h>
39#include <linux/list.h>
40#include <linux/sysfs.h>
41#include <linux/ctype.h>
42#include <linux/hwmon-sysfs.h>
43
44/* Device registers */
45#define DS1682_REG_CONFIG 0x00
46#define DS1682_REG_ALARM 0x01
47#define DS1682_REG_ELAPSED 0x05
48#define DS1682_REG_EVT_CNTR 0x09
49#define DS1682_REG_EEPROM 0x0b
50#define DS1682_REG_RESET 0x1d
51#define DS1682_REG_WRITE_DISABLE 0x1e
52#define DS1682_REG_WRITE_MEM_DISABLE 0x1f
53
54#define DS1682_EEPROM_SIZE 10
55
56/*
57 * Generic counter attributes
58 */
59static ssize_t ds1682_show(struct device *dev, struct device_attribute *attr,
60 char *buf)
61{
62 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
63 struct i2c_client *client = to_i2c_client(dev);
64 __le32 val = 0;
65 int rc;
66
67 dev_dbg(dev, "ds1682_show() called on %s\n", attr->attr.name);
68
69 /* Read the register */
70 rc = i2c_smbus_read_i2c_block_data(client, sattr->index, sattr->nr,
71 (u8 *) & val);
72 if (rc < 0)
73 return -EIO;
74
75 /* Special case: the 32 bit regs are time values with 1/4s
76 * resolution, scale them up to milliseconds */
77 if (sattr->nr == 4)
78 return sprintf(buf, "%llu\n", ((u64) le32_to_cpu(val)) * 250);
79
80 /* Format the output string and return # of bytes */
81 return sprintf(buf, "%li\n", (long)le32_to_cpu(val));
82}
83
84static ssize_t ds1682_store(struct device *dev, struct device_attribute *attr,
85 const char *buf, size_t count)
86{
87 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
88 struct i2c_client *client = to_i2c_client(dev);
89 char *endp;
90 u64 val;
91 __le32 val_le;
92 int rc;
93
94 dev_dbg(dev, "ds1682_store() called on %s\n", attr->attr.name);
95
96 /* Decode input */
97 val = simple_strtoull(buf, &endp, 0);
98 if (buf == endp) {
99 dev_dbg(dev, "input string not a number\n");
100 return -EINVAL;
101 }
102
103 /* Special case: the 32 bit regs are time values with 1/4s
104 * resolution, scale input down to quarter-seconds */
105 if (sattr->nr == 4)
106 do_div(val, 250);
107
108 /* write out the value */
109 val_le = cpu_to_le32(val);
110 rc = i2c_smbus_write_i2c_block_data(client, sattr->index, sattr->nr,
111 (u8 *) & val_le);
112 if (rc < 0) {
113 dev_err(dev, "register write failed; reg=0x%x, size=%i\n",
114 sattr->index, sattr->nr);
115 return -EIO;
116 }
117
118 return count;
119}
120
121/*
122 * Simple register attributes
123 */
124static SENSOR_DEVICE_ATTR_2(elapsed_time, S_IRUGO | S_IWUSR, ds1682_show,
125 ds1682_store, 4, DS1682_REG_ELAPSED);
126static SENSOR_DEVICE_ATTR_2(alarm_time, S_IRUGO | S_IWUSR, ds1682_show,
127 ds1682_store, 4, DS1682_REG_ALARM);
128static SENSOR_DEVICE_ATTR_2(event_count, S_IRUGO | S_IWUSR, ds1682_show,
129 ds1682_store, 2, DS1682_REG_EVT_CNTR);
130
131static const struct attribute_group ds1682_group = {
132 .attrs = (struct attribute *[]) {
133 &sensor_dev_attr_elapsed_time.dev_attr.attr,
134 &sensor_dev_attr_alarm_time.dev_attr.attr,
135 &sensor_dev_attr_event_count.dev_attr.attr,
136 NULL,
137 },
138};
139
140/*
141 * User data attribute
142 */
143static ssize_t ds1682_eeprom_read(struct kobject *kobj, struct bin_attribute *attr,
144 char *buf, loff_t off, size_t count)
145{
146 struct i2c_client *client = kobj_to_i2c_client(kobj);
147 int rc;
148
149 dev_dbg(&client->dev, "ds1682_eeprom_read(p=%p, off=%lli, c=%zi)\n",
150 buf, off, count);
151
152 if (off >= DS1682_EEPROM_SIZE)
153 return 0;
154
155 if (off + count > DS1682_EEPROM_SIZE)
156 count = DS1682_EEPROM_SIZE - off;
157
158 rc = i2c_smbus_read_i2c_block_data(client, DS1682_REG_EEPROM + off,
159 count, buf);
160 if (rc < 0)
161 return -EIO;
162
163 return count;
164}
165
166static ssize_t ds1682_eeprom_write(struct kobject *kobj, struct bin_attribute *attr,
167 char *buf, loff_t off, size_t count)
168{
169 struct i2c_client *client = kobj_to_i2c_client(kobj);
170
171 dev_dbg(&client->dev, "ds1682_eeprom_write(p=%p, off=%lli, c=%zi)\n",
172 buf, off, count);
173
174 if (off >= DS1682_EEPROM_SIZE)
175 return -ENOSPC;
176
177 if (off + count > DS1682_EEPROM_SIZE)
178 count = DS1682_EEPROM_SIZE - off;
179
180 /* Write out to the device */
181 if (i2c_smbus_write_i2c_block_data(client, DS1682_REG_EEPROM + off,
182 count, buf) < 0)
183 return -EIO;
184
185 return count;
186}
187
188static struct bin_attribute ds1682_eeprom_attr = {
189 .attr = {
190 .name = "eeprom",
191 .mode = S_IRUGO | S_IWUSR,
192 .owner = THIS_MODULE,
193 },
194 .size = DS1682_EEPROM_SIZE,
195 .read = ds1682_eeprom_read,
196 .write = ds1682_eeprom_write,
197};
198
199/*
200 * Called when a ds1682 device is matched with this driver
201 */
202static int ds1682_probe(struct i2c_client *client)
203{
204 int rc;
205
206 if (!i2c_check_functionality(client->adapter,
207 I2C_FUNC_SMBUS_I2C_BLOCK)) {
208 dev_err(&client->dev, "i2c bus does not support the ds1682\n");
209 rc = -ENODEV;
210 goto exit;
211 }
212
213 rc = sysfs_create_group(&client->dev.kobj, &ds1682_group);
214 if (rc)
215 goto exit;
216
217 rc = sysfs_create_bin_file(&client->dev.kobj, &ds1682_eeprom_attr);
218 if (rc)
219 goto exit_bin_attr;
220
221 return 0;
222
223 exit_bin_attr:
224 sysfs_remove_group(&client->dev.kobj, &ds1682_group);
225 exit:
226 return rc;
227}
228
229static int ds1682_remove(struct i2c_client *client)
230{
231 sysfs_remove_bin_file(&client->dev.kobj, &ds1682_eeprom_attr);
232 sysfs_remove_group(&client->dev.kobj, &ds1682_group);
233 return 0;
234}
235
236static struct i2c_driver ds1682_driver = {
237 .driver = {
238 .name = "ds1682",
239 },
240 .probe = ds1682_probe,
241 .remove = ds1682_remove,
242};
243
244static int __init ds1682_init(void)
245{
246 return i2c_add_driver(&ds1682_driver);
247}
248
249static void __exit ds1682_exit(void)
250{
251 i2c_del_driver(&ds1682_driver);
252}
253
254MODULE_AUTHOR("Grant Likely <grant.likely@secretlab.ca>");
255MODULE_DESCRIPTION("DS1682 Elapsed Time Indicator driver");
256MODULE_LICENSE("GPL");
257
258module_init(ds1682_init);
259module_exit(ds1682_exit);
diff --git a/drivers/i2c/chips/eeprom.c b/drivers/i2c/chips/eeprom.c
index bfce13c8f1ff..d3da1fb05b9b 100644
--- a/drivers/i2c/chips/eeprom.c
+++ b/drivers/i2c/chips/eeprom.c
@@ -88,8 +88,10 @@ static void eeprom_update_client(struct i2c_client *client, u8 slice)
88 dev_dbg(&client->dev, "Starting eeprom update, slice %u\n", slice); 88 dev_dbg(&client->dev, "Starting eeprom update, slice %u\n", slice);
89 89
90 if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_I2C_BLOCK)) { 90 if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_I2C_BLOCK)) {
91 for (i = slice << 5; i < (slice + 1) << 5; i += I2C_SMBUS_BLOCK_MAX) 91 for (i = slice << 5; i < (slice + 1) << 5; i += 32)
92 if (i2c_smbus_read_i2c_block_data(client, i, data->data + i) != I2C_SMBUS_BLOCK_MAX) 92 if (i2c_smbus_read_i2c_block_data(client, i,
93 32, data->data + i)
94 != 32)
93 goto exit; 95 goto exit;
94 } else { 96 } else {
95 if (i2c_smbus_write_byte(client, slice << 5)) { 97 if (i2c_smbus_write_byte(client, slice << 5)) {
@@ -110,7 +112,8 @@ exit:
110 mutex_unlock(&data->update_lock); 112 mutex_unlock(&data->update_lock);
111} 113}
112 114
113static ssize_t eeprom_read(struct kobject *kobj, char *buf, loff_t off, size_t count) 115static ssize_t eeprom_read(struct kobject *kobj, struct bin_attribute *bin_attr,
116 char *buf, loff_t off, size_t count)
114{ 117{
115 struct i2c_client *client = to_i2c_client(container_of(kobj, struct device, kobj)); 118 struct i2c_client *client = to_i2c_client(container_of(kobj, struct device, kobj));
116 struct eeprom_data *data = i2c_get_clientdata(client); 119 struct eeprom_data *data = i2c_get_clientdata(client);
@@ -143,7 +146,6 @@ static struct bin_attribute eeprom_attr = {
143 .attr = { 146 .attr = {
144 .name = "eeprom", 147 .name = "eeprom",
145 .mode = S_IRUGO, 148 .mode = S_IRUGO,
146 .owner = THIS_MODULE,
147 }, 149 },
148 .size = EEPROM_SIZE, 150 .size = EEPROM_SIZE,
149 .read = eeprom_read, 151 .read = eeprom_read,
diff --git a/drivers/i2c/chips/max6875.c b/drivers/i2c/chips/max6875.c
index 76645c142977..64692f666372 100644
--- a/drivers/i2c/chips/max6875.c
+++ b/drivers/i2c/chips/max6875.c
@@ -106,6 +106,7 @@ static void max6875_update_slice(struct i2c_client *client, int slice)
106 I2C_FUNC_SMBUS_READ_I2C_BLOCK)) { 106 I2C_FUNC_SMBUS_READ_I2C_BLOCK)) {
107 if (i2c_smbus_read_i2c_block_data(client, 107 if (i2c_smbus_read_i2c_block_data(client,
108 MAX6875_CMD_BLK_READ, 108 MAX6875_CMD_BLK_READ,
109 SLICE_SIZE,
109 buf) != SLICE_SIZE) { 110 buf) != SLICE_SIZE) {
110 goto exit_up; 111 goto exit_up;
111 } 112 }
@@ -125,8 +126,9 @@ exit_up:
125 mutex_unlock(&data->update_lock); 126 mutex_unlock(&data->update_lock);
126} 127}
127 128
128static ssize_t max6875_read(struct kobject *kobj, char *buf, loff_t off, 129static ssize_t max6875_read(struct kobject *kobj,
129 size_t count) 130 struct bin_attribute *bin_attr,
131 char *buf, loff_t off, size_t count)
130{ 132{
131 struct i2c_client *client = kobj_to_i2c_client(kobj); 133 struct i2c_client *client = kobj_to_i2c_client(kobj);
132 struct max6875_data *data = i2c_get_clientdata(client); 134 struct max6875_data *data = i2c_get_clientdata(client);
@@ -152,7 +154,6 @@ static struct bin_attribute user_eeprom_attr = {
152 .attr = { 154 .attr = {
153 .name = "eeprom", 155 .name = "eeprom",
154 .mode = S_IRUGO, 156 .mode = S_IRUGO,
155 .owner = THIS_MODULE,
156 }, 157 },
157 .size = USER_EEPROM_SIZE, 158 .size = USER_EEPROM_SIZE,
158 .read = max6875_read, 159 .read = max6875_read,
diff --git a/drivers/i2c/chips/menelaus.c b/drivers/i2c/chips/menelaus.c
new file mode 100644
index 000000000000..48a7e2f0bdd3
--- /dev/null
+++ b/drivers/i2c/chips/menelaus.c
@@ -0,0 +1,1281 @@
1#define DEBUG
2/*
3 * Copyright (C) 2004 Texas Instruments, Inc.
4 *
5 * Some parts based tps65010.c:
6 * Copyright (C) 2004 Texas Instruments and
7 * Copyright (C) 2004-2005 David Brownell
8 *
9 * Some parts based on tlv320aic24.c:
10 * Copyright (C) by Kai Svahn <kai.svahn@nokia.com>
11 *
12 * Changes for interrupt handling and clean-up by
13 * Tony Lindgren <tony@atomide.com> and Imre Deak <imre.deak@nokia.com>
14 * Cleanup and generalized support for voltage setting by
15 * Juha Yrjola
16 * Added support for controlling VCORE and regulator sleep states,
17 * Amit Kucheria <amit.kucheria@nokia.com>
18 * Copyright (C) 2005, 2006 Nokia Corporation
19 *
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; either version 2 of the License, or
23 * (at your option) any later version.
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
29 *
30 * You should have received a copy of the GNU General Public License
31 * along with this program; if not, write to the Free Software
32 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33 */
34
35#include <linux/module.h>
36#include <linux/i2c.h>
37#include <linux/interrupt.h>
38#include <linux/sched.h>
39#include <linux/mutex.h>
40#include <linux/workqueue.h>
41#include <linux/delay.h>
42#include <linux/rtc.h>
43#include <linux/bcd.h>
44
45#include <asm/mach-types.h>
46#include <asm/mach/irq.h>
47
48#include <asm/arch/gpio.h>
49#include <asm/arch/menelaus.h>
50
51#define DRIVER_NAME "menelaus"
52
53#define pr_err(fmt, arg...) printk(KERN_ERR DRIVER_NAME ": ", ## arg);
54
55#define MENELAUS_I2C_ADDRESS 0x72
56
57#define MENELAUS_REV 0x01
58#define MENELAUS_VCORE_CTRL1 0x02
59#define MENELAUS_VCORE_CTRL2 0x03
60#define MENELAUS_VCORE_CTRL3 0x04
61#define MENELAUS_VCORE_CTRL4 0x05
62#define MENELAUS_VCORE_CTRL5 0x06
63#define MENELAUS_DCDC_CTRL1 0x07
64#define MENELAUS_DCDC_CTRL2 0x08
65#define MENELAUS_DCDC_CTRL3 0x09
66#define MENELAUS_LDO_CTRL1 0x0A
67#define MENELAUS_LDO_CTRL2 0x0B
68#define MENELAUS_LDO_CTRL3 0x0C
69#define MENELAUS_LDO_CTRL4 0x0D
70#define MENELAUS_LDO_CTRL5 0x0E
71#define MENELAUS_LDO_CTRL6 0x0F
72#define MENELAUS_LDO_CTRL7 0x10
73#define MENELAUS_LDO_CTRL8 0x11
74#define MENELAUS_SLEEP_CTRL1 0x12
75#define MENELAUS_SLEEP_CTRL2 0x13
76#define MENELAUS_DEVICE_OFF 0x14
77#define MENELAUS_OSC_CTRL 0x15
78#define MENELAUS_DETECT_CTRL 0x16
79#define MENELAUS_INT_MASK1 0x17
80#define MENELAUS_INT_MASK2 0x18
81#define MENELAUS_INT_STATUS1 0x19
82#define MENELAUS_INT_STATUS2 0x1A
83#define MENELAUS_INT_ACK1 0x1B
84#define MENELAUS_INT_ACK2 0x1C
85#define MENELAUS_GPIO_CTRL 0x1D
86#define MENELAUS_GPIO_IN 0x1E
87#define MENELAUS_GPIO_OUT 0x1F
88#define MENELAUS_BBSMS 0x20
89#define MENELAUS_RTC_CTRL 0x21
90#define MENELAUS_RTC_UPDATE 0x22
91#define MENELAUS_RTC_SEC 0x23
92#define MENELAUS_RTC_MIN 0x24
93#define MENELAUS_RTC_HR 0x25
94#define MENELAUS_RTC_DAY 0x26
95#define MENELAUS_RTC_MON 0x27
96#define MENELAUS_RTC_YR 0x28
97#define MENELAUS_RTC_WKDAY 0x29
98#define MENELAUS_RTC_AL_SEC 0x2A
99#define MENELAUS_RTC_AL_MIN 0x2B
100#define MENELAUS_RTC_AL_HR 0x2C
101#define MENELAUS_RTC_AL_DAY 0x2D
102#define MENELAUS_RTC_AL_MON 0x2E
103#define MENELAUS_RTC_AL_YR 0x2F
104#define MENELAUS_RTC_COMP_MSB 0x30
105#define MENELAUS_RTC_COMP_LSB 0x31
106#define MENELAUS_S1_PULL_EN 0x32
107#define MENELAUS_S1_PULL_DIR 0x33
108#define MENELAUS_S2_PULL_EN 0x34
109#define MENELAUS_S2_PULL_DIR 0x35
110#define MENELAUS_MCT_CTRL1 0x36
111#define MENELAUS_MCT_CTRL2 0x37
112#define MENELAUS_MCT_CTRL3 0x38
113#define MENELAUS_MCT_PIN_ST 0x39
114#define MENELAUS_DEBOUNCE1 0x3A
115
116#define IH_MENELAUS_IRQS 12
117#define MENELAUS_MMC_S1CD_IRQ 0 /* MMC slot 1 card change */
118#define MENELAUS_MMC_S2CD_IRQ 1 /* MMC slot 2 card change */
119#define MENELAUS_MMC_S1D1_IRQ 2 /* MMC DAT1 low in slot 1 */
120#define MENELAUS_MMC_S2D1_IRQ 3 /* MMC DAT1 low in slot 2 */
121#define MENELAUS_LOWBAT_IRQ 4 /* Low battery */
122#define MENELAUS_HOTDIE_IRQ 5 /* Hot die detect */
123#define MENELAUS_UVLO_IRQ 6 /* UVLO detect */
124#define MENELAUS_TSHUT_IRQ 7 /* Thermal shutdown */
125#define MENELAUS_RTCTMR_IRQ 8 /* RTC timer */
126#define MENELAUS_RTCALM_IRQ 9 /* RTC alarm */
127#define MENELAUS_RTCERR_IRQ 10 /* RTC error */
128#define MENELAUS_PSHBTN_IRQ 11 /* Push button */
129#define MENELAUS_RESERVED12_IRQ 12 /* Reserved */
130#define MENELAUS_RESERVED13_IRQ 13 /* Reserved */
131#define MENELAUS_RESERVED14_IRQ 14 /* Reserved */
132#define MENELAUS_RESERVED15_IRQ 15 /* Reserved */
133
134static void menelaus_work(struct work_struct *_menelaus);
135
136struct menelaus_chip {
137 struct mutex lock;
138 struct i2c_client *client;
139 struct work_struct work;
140#ifdef CONFIG_RTC_DRV_TWL92330
141 struct rtc_device *rtc;
142 u8 rtc_control;
143 unsigned uie:1;
144#endif
145 unsigned vcore_hw_mode:1;
146 u8 mask1, mask2;
147 void (*handlers[16])(struct menelaus_chip *);
148 void (*mmc_callback)(void *data, u8 mask);
149 void *mmc_callback_data;
150};
151
152static struct menelaus_chip *the_menelaus;
153
154static int menelaus_write_reg(int reg, u8 value)
155{
156 int val = i2c_smbus_write_byte_data(the_menelaus->client, reg, value);
157
158 if (val < 0) {
159 pr_err("write error");
160 return val;
161 }
162
163 return 0;
164}
165
166static int menelaus_read_reg(int reg)
167{
168 int val = i2c_smbus_read_byte_data(the_menelaus->client, reg);
169
170 if (val < 0)
171 pr_err("read error");
172
173 return val;
174}
175
176static int menelaus_enable_irq(int irq)
177{
178 if (irq > 7) {
179 irq -= 8;
180 the_menelaus->mask2 &= ~(1 << irq);
181 return menelaus_write_reg(MENELAUS_INT_MASK2,
182 the_menelaus->mask2);
183 } else {
184 the_menelaus->mask1 &= ~(1 << irq);
185 return menelaus_write_reg(MENELAUS_INT_MASK1,
186 the_menelaus->mask1);
187 }
188}
189
190static int menelaus_disable_irq(int irq)
191{
192 if (irq > 7) {
193 irq -= 8;
194 the_menelaus->mask2 |= (1 << irq);
195 return menelaus_write_reg(MENELAUS_INT_MASK2,
196 the_menelaus->mask2);
197 } else {
198 the_menelaus->mask1 |= (1 << irq);
199 return menelaus_write_reg(MENELAUS_INT_MASK1,
200 the_menelaus->mask1);
201 }
202}
203
204static int menelaus_ack_irq(int irq)
205{
206 if (irq > 7)
207 return menelaus_write_reg(MENELAUS_INT_ACK2, 1 << (irq - 8));
208 else
209 return menelaus_write_reg(MENELAUS_INT_ACK1, 1 << irq);
210}
211
212/* Adds a handler for an interrupt. Does not run in interrupt context */
213static int menelaus_add_irq_work(int irq,
214 void (*handler)(struct menelaus_chip *))
215{
216 int ret = 0;
217
218 mutex_lock(&the_menelaus->lock);
219 the_menelaus->handlers[irq] = handler;
220 ret = menelaus_enable_irq(irq);
221 mutex_unlock(&the_menelaus->lock);
222
223 return ret;
224}
225
226/* Removes handler for an interrupt */
227static int menelaus_remove_irq_work(int irq)
228{
229 int ret = 0;
230
231 mutex_lock(&the_menelaus->lock);
232 ret = menelaus_disable_irq(irq);
233 the_menelaus->handlers[irq] = NULL;
234 mutex_unlock(&the_menelaus->lock);
235
236 return ret;
237}
238
239/*
240 * Gets scheduled when a card detect interrupt happens. Note that in some cases
241 * this line is wired to card cover switch rather than the card detect switch
242 * in each slot. In this case the cards are not seen by menelaus.
243 * FIXME: Add handling for D1 too
244 */
245static void menelaus_mmc_cd_work(struct menelaus_chip *menelaus_hw)
246{
247 int reg;
248 unsigned char card_mask = 0;
249
250 reg = menelaus_read_reg(MENELAUS_MCT_PIN_ST);
251 if (reg < 0)
252 return;
253
254 if (!(reg & 0x1))
255 card_mask |= (1 << 0);
256
257 if (!(reg & 0x2))
258 card_mask |= (1 << 1);
259
260 if (menelaus_hw->mmc_callback)
261 menelaus_hw->mmc_callback(menelaus_hw->mmc_callback_data,
262 card_mask);
263}
264
265/*
266 * Toggles the MMC slots between open-drain and push-pull mode.
267 */
268int menelaus_set_mmc_opendrain(int slot, int enable)
269{
270 int ret, val;
271
272 if (slot != 1 && slot != 2)
273 return -EINVAL;
274 mutex_lock(&the_menelaus->lock);
275 ret = menelaus_read_reg(MENELAUS_MCT_CTRL1);
276 if (ret < 0) {
277 mutex_unlock(&the_menelaus->lock);
278 return ret;
279 }
280 val = ret;
281 if (slot == 1) {
282 if (enable)
283 val |= 1 << 2;
284 else
285 val &= ~(1 << 2);
286 } else {
287 if (enable)
288 val |= 1 << 3;
289 else
290 val &= ~(1 << 3);
291 }
292 ret = menelaus_write_reg(MENELAUS_MCT_CTRL1, val);
293 mutex_unlock(&the_menelaus->lock);
294
295 return ret;
296}
297EXPORT_SYMBOL(menelaus_set_mmc_opendrain);
298
299int menelaus_set_slot_sel(int enable)
300{
301 int ret;
302
303 mutex_lock(&the_menelaus->lock);
304 ret = menelaus_read_reg(MENELAUS_GPIO_CTRL);
305 if (ret < 0)
306 goto out;
307 ret |= 0x02;
308 if (enable)
309 ret |= 1 << 5;
310 else
311 ret &= ~(1 << 5);
312 ret = menelaus_write_reg(MENELAUS_GPIO_CTRL, ret);
313out:
314 mutex_unlock(&the_menelaus->lock);
315 return ret;
316}
317EXPORT_SYMBOL(menelaus_set_slot_sel);
318
319int menelaus_set_mmc_slot(int slot, int enable, int power, int cd_en)
320{
321 int ret, val;
322
323 if (slot != 1 && slot != 2)
324 return -EINVAL;
325 if (power >= 3)
326 return -EINVAL;
327
328 mutex_lock(&the_menelaus->lock);
329
330 ret = menelaus_read_reg(MENELAUS_MCT_CTRL2);
331 if (ret < 0)
332 goto out;
333 val = ret;
334 if (slot == 1) {
335 if (cd_en)
336 val |= (1 << 4) | (1 << 6);
337 else
338 val &= ~((1 << 4) | (1 << 6));
339 } else {
340 if (cd_en)
341 val |= (1 << 5) | (1 << 7);
342 else
343 val &= ~((1 << 5) | (1 << 7));
344 }
345 ret = menelaus_write_reg(MENELAUS_MCT_CTRL2, val);
346 if (ret < 0)
347 goto out;
348
349 ret = menelaus_read_reg(MENELAUS_MCT_CTRL3);
350 if (ret < 0)
351 goto out;
352 val = ret;
353 if (slot == 1) {
354 if (enable)
355 val |= 1 << 0;
356 else
357 val &= ~(1 << 0);
358 } else {
359 int b;
360
361 if (enable)
362 ret |= 1 << 1;
363 else
364 ret &= ~(1 << 1);
365 b = menelaus_read_reg(MENELAUS_MCT_CTRL2);
366 b &= ~0x03;
367 b |= power;
368 ret = menelaus_write_reg(MENELAUS_MCT_CTRL2, b);
369 if (ret < 0)
370 goto out;
371 }
372 /* Disable autonomous shutdown */
373 val &= ~(0x03 << 2);
374 ret = menelaus_write_reg(MENELAUS_MCT_CTRL3, val);
375out:
376 mutex_unlock(&the_menelaus->lock);
377 return ret;
378}
379EXPORT_SYMBOL(menelaus_set_mmc_slot);
380
381int menelaus_register_mmc_callback(void (*callback)(void *data, u8 card_mask),
382 void *data)
383{
384 int ret = 0;
385
386 the_menelaus->mmc_callback_data = data;
387 the_menelaus->mmc_callback = callback;
388 ret = menelaus_add_irq_work(MENELAUS_MMC_S1CD_IRQ,
389 menelaus_mmc_cd_work);
390 if (ret < 0)
391 return ret;
392 ret = menelaus_add_irq_work(MENELAUS_MMC_S2CD_IRQ,
393 menelaus_mmc_cd_work);
394 if (ret < 0)
395 return ret;
396 ret = menelaus_add_irq_work(MENELAUS_MMC_S1D1_IRQ,
397 menelaus_mmc_cd_work);
398 if (ret < 0)
399 return ret;
400 ret = menelaus_add_irq_work(MENELAUS_MMC_S2D1_IRQ,
401 menelaus_mmc_cd_work);
402
403 return ret;
404}
405EXPORT_SYMBOL(menelaus_register_mmc_callback);
406
407void menelaus_unregister_mmc_callback(void)
408{
409 menelaus_remove_irq_work(MENELAUS_MMC_S1CD_IRQ);
410 menelaus_remove_irq_work(MENELAUS_MMC_S2CD_IRQ);
411 menelaus_remove_irq_work(MENELAUS_MMC_S1D1_IRQ);
412 menelaus_remove_irq_work(MENELAUS_MMC_S2D1_IRQ);
413
414 the_menelaus->mmc_callback = NULL;
415 the_menelaus->mmc_callback_data = 0;
416}
417EXPORT_SYMBOL(menelaus_unregister_mmc_callback);
418
419struct menelaus_vtg {
420 const char *name;
421 u8 vtg_reg;
422 u8 vtg_shift;
423 u8 vtg_bits;
424 u8 mode_reg;
425};
426
427struct menelaus_vtg_value {
428 u16 vtg;
429 u16 val;
430};
431
432static int menelaus_set_voltage(const struct menelaus_vtg *vtg, int mV,
433 int vtg_val, int mode)
434{
435 int val, ret;
436 struct i2c_client *c = the_menelaus->client;
437
438 mutex_lock(&the_menelaus->lock);
439 if (vtg == 0)
440 goto set_voltage;
441
442 ret = menelaus_read_reg(vtg->vtg_reg);
443 if (ret < 0)
444 goto out;
445 val = ret & ~(((1 << vtg->vtg_bits) - 1) << vtg->vtg_shift);
446 val |= vtg_val << vtg->vtg_shift;
447
448 dev_dbg(&c->dev, "Setting voltage '%s'"
449 "to %d mV (reg 0x%02x, val 0x%02x)\n",
450 vtg->name, mV, vtg->vtg_reg, val);
451
452 ret = menelaus_write_reg(vtg->vtg_reg, val);
453 if (ret < 0)
454 goto out;
455set_voltage:
456 ret = menelaus_write_reg(vtg->mode_reg, mode);
457out:
458 mutex_unlock(&the_menelaus->lock);
459 if (ret == 0) {
460 /* Wait for voltage to stabilize */
461 msleep(1);
462 }
463 return ret;
464}
465
466static int menelaus_get_vtg_value(int vtg, const struct menelaus_vtg_value *tbl,
467 int n)
468{
469 int i;
470
471 for (i = 0; i < n; i++, tbl++)
472 if (tbl->vtg == vtg)
473 return tbl->val;
474 return -EINVAL;
475}
476
477/*
478 * Vcore can be programmed in two ways:
479 * SW-controlled: Required voltage is programmed into VCORE_CTRL1
480 * HW-controlled: Required range (roof-floor) is programmed into VCORE_CTRL3
481 * and VCORE_CTRL4
482 *
483 * Call correct 'set' function accordingly
484 */
485
486static const struct menelaus_vtg_value vcore_values[] = {
487 { 1000, 0 },
488 { 1025, 1 },
489 { 1050, 2 },
490 { 1075, 3 },
491 { 1100, 4 },
492 { 1125, 5 },
493 { 1150, 6 },
494 { 1175, 7 },
495 { 1200, 8 },
496 { 1225, 9 },
497 { 1250, 10 },
498 { 1275, 11 },
499 { 1300, 12 },
500 { 1325, 13 },
501 { 1350, 14 },
502 { 1375, 15 },
503 { 1400, 16 },
504 { 1425, 17 },
505 { 1450, 18 },
506};
507
508int menelaus_set_vcore_sw(unsigned int mV)
509{
510 int val, ret;
511 struct i2c_client *c = the_menelaus->client;
512
513 val = menelaus_get_vtg_value(mV, vcore_values,
514 ARRAY_SIZE(vcore_values));
515 if (val < 0)
516 return -EINVAL;
517
518 dev_dbg(&c->dev, "Setting VCORE to %d mV (val 0x%02x)\n", mV, val);
519
520 /* Set SW mode and the voltage in one go. */
521 mutex_lock(&the_menelaus->lock);
522 ret = menelaus_write_reg(MENELAUS_VCORE_CTRL1, val);
523 if (ret == 0)
524 the_menelaus->vcore_hw_mode = 0;
525 mutex_unlock(&the_menelaus->lock);
526 msleep(1);
527
528 return ret;
529}
530
531int menelaus_set_vcore_hw(unsigned int roof_mV, unsigned int floor_mV)
532{
533 int fval, rval, val, ret;
534 struct i2c_client *c = the_menelaus->client;
535
536 rval = menelaus_get_vtg_value(roof_mV, vcore_values,
537 ARRAY_SIZE(vcore_values));
538 if (rval < 0)
539 return -EINVAL;
540 fval = menelaus_get_vtg_value(floor_mV, vcore_values,
541 ARRAY_SIZE(vcore_values));
542 if (fval < 0)
543 return -EINVAL;
544
545 dev_dbg(&c->dev, "Setting VCORE FLOOR to %d mV and ROOF to %d mV\n",
546 floor_mV, roof_mV);
547
548 mutex_lock(&the_menelaus->lock);
549 ret = menelaus_write_reg(MENELAUS_VCORE_CTRL3, fval);
550 if (ret < 0)
551 goto out;
552 ret = menelaus_write_reg(MENELAUS_VCORE_CTRL4, rval);
553 if (ret < 0)
554 goto out;
555 if (!the_menelaus->vcore_hw_mode) {
556 val = menelaus_read_reg(MENELAUS_VCORE_CTRL1);
557 /* HW mode, turn OFF byte comparator */
558 val |= ((1 << 7) | (1 << 5));
559 ret = menelaus_write_reg(MENELAUS_VCORE_CTRL1, val);
560 the_menelaus->vcore_hw_mode = 1;
561 }
562 msleep(1);
563out:
564 mutex_unlock(&the_menelaus->lock);
565 return ret;
566}
567
568static const struct menelaus_vtg vmem_vtg = {
569 .name = "VMEM",
570 .vtg_reg = MENELAUS_LDO_CTRL1,
571 .vtg_shift = 0,
572 .vtg_bits = 2,
573 .mode_reg = MENELAUS_LDO_CTRL3,
574};
575
576static const struct menelaus_vtg_value vmem_values[] = {
577 { 1500, 0 },
578 { 1800, 1 },
579 { 1900, 2 },
580 { 2500, 3 },
581};
582
583int menelaus_set_vmem(unsigned int mV)
584{
585 int val;
586
587 if (mV == 0)
588 return menelaus_set_voltage(&vmem_vtg, 0, 0, 0);
589
590 val = menelaus_get_vtg_value(mV, vmem_values, ARRAY_SIZE(vmem_values));
591 if (val < 0)
592 return -EINVAL;
593 return menelaus_set_voltage(&vmem_vtg, mV, val, 0x02);
594}
595EXPORT_SYMBOL(menelaus_set_vmem);
596
597static const struct menelaus_vtg vio_vtg = {
598 .name = "VIO",
599 .vtg_reg = MENELAUS_LDO_CTRL1,
600 .vtg_shift = 2,
601 .vtg_bits = 2,
602 .mode_reg = MENELAUS_LDO_CTRL4,
603};
604
605static const struct menelaus_vtg_value vio_values[] = {
606 { 1500, 0 },
607 { 1800, 1 },
608 { 2500, 2 },
609 { 2800, 3 },
610};
611
612int menelaus_set_vio(unsigned int mV)
613{
614 int val;
615
616 if (mV == 0)
617 return menelaus_set_voltage(&vio_vtg, 0, 0, 0);
618
619 val = menelaus_get_vtg_value(mV, vio_values, ARRAY_SIZE(vio_values));
620 if (val < 0)
621 return -EINVAL;
622 return menelaus_set_voltage(&vio_vtg, mV, val, 0x02);
623}
624EXPORT_SYMBOL(menelaus_set_vio);
625
626static const struct menelaus_vtg_value vdcdc_values[] = {
627 { 1500, 0 },
628 { 1800, 1 },
629 { 2000, 2 },
630 { 2200, 3 },
631 { 2400, 4 },
632 { 2800, 5 },
633 { 3000, 6 },
634 { 3300, 7 },
635};
636
637static const struct menelaus_vtg vdcdc2_vtg = {
638 .name = "VDCDC2",
639 .vtg_reg = MENELAUS_DCDC_CTRL1,
640 .vtg_shift = 0,
641 .vtg_bits = 3,
642 .mode_reg = MENELAUS_DCDC_CTRL2,
643};
644
645static const struct menelaus_vtg vdcdc3_vtg = {
646 .name = "VDCDC3",
647 .vtg_reg = MENELAUS_DCDC_CTRL1,
648 .vtg_shift = 3,
649 .vtg_bits = 3,
650 .mode_reg = MENELAUS_DCDC_CTRL3,
651};
652
653int menelaus_set_vdcdc(int dcdc, unsigned int mV)
654{
655 const struct menelaus_vtg *vtg;
656 int val;
657
658 if (dcdc != 2 && dcdc != 3)
659 return -EINVAL;
660 if (dcdc == 2)
661 vtg = &vdcdc2_vtg;
662 else
663 vtg = &vdcdc3_vtg;
664
665 if (mV == 0)
666 return menelaus_set_voltage(vtg, 0, 0, 0);
667
668 val = menelaus_get_vtg_value(mV, vdcdc_values,
669 ARRAY_SIZE(vdcdc_values));
670 if (val < 0)
671 return -EINVAL;
672 return menelaus_set_voltage(vtg, mV, val, 0x03);
673}
674
675static const struct menelaus_vtg_value vmmc_values[] = {
676 { 1850, 0 },
677 { 2800, 1 },
678 { 3000, 2 },
679 { 3100, 3 },
680};
681
682static const struct menelaus_vtg vmmc_vtg = {
683 .name = "VMMC",
684 .vtg_reg = MENELAUS_LDO_CTRL1,
685 .vtg_shift = 6,
686 .vtg_bits = 2,
687 .mode_reg = MENELAUS_LDO_CTRL7,
688};
689
690int menelaus_set_vmmc(unsigned int mV)
691{
692 int val;
693
694 if (mV == 0)
695 return menelaus_set_voltage(&vmmc_vtg, 0, 0, 0);
696
697 val = menelaus_get_vtg_value(mV, vmmc_values, ARRAY_SIZE(vmmc_values));
698 if (val < 0)
699 return -EINVAL;
700 return menelaus_set_voltage(&vmmc_vtg, mV, val, 0x02);
701}
702EXPORT_SYMBOL(menelaus_set_vmmc);
703
704
705static const struct menelaus_vtg_value vaux_values[] = {
706 { 1500, 0 },
707 { 1800, 1 },
708 { 2500, 2 },
709 { 2800, 3 },
710};
711
712static const struct menelaus_vtg vaux_vtg = {
713 .name = "VAUX",
714 .vtg_reg = MENELAUS_LDO_CTRL1,
715 .vtg_shift = 4,
716 .vtg_bits = 2,
717 .mode_reg = MENELAUS_LDO_CTRL6,
718};
719
720int menelaus_set_vaux(unsigned int mV)
721{
722 int val;
723
724 if (mV == 0)
725 return menelaus_set_voltage(&vaux_vtg, 0, 0, 0);
726
727 val = menelaus_get_vtg_value(mV, vaux_values, ARRAY_SIZE(vaux_values));
728 if (val < 0)
729 return -EINVAL;
730 return menelaus_set_voltage(&vaux_vtg, mV, val, 0x02);
731}
732EXPORT_SYMBOL(menelaus_set_vaux);
733
734int menelaus_get_slot_pin_states(void)
735{
736 return menelaus_read_reg(MENELAUS_MCT_PIN_ST);
737}
738EXPORT_SYMBOL(menelaus_get_slot_pin_states);
739
740int menelaus_set_regulator_sleep(int enable, u32 val)
741{
742 int t, ret;
743 struct i2c_client *c = the_menelaus->client;
744
745 mutex_lock(&the_menelaus->lock);
746 ret = menelaus_write_reg(MENELAUS_SLEEP_CTRL2, val);
747 if (ret < 0)
748 goto out;
749
750 dev_dbg(&c->dev, "regulator sleep configuration: %02x\n", val);
751
752 ret = menelaus_read_reg(MENELAUS_GPIO_CTRL);
753 if (ret < 0)
754 goto out;
755 t = ((1 << 6) | 0x04);
756 if (enable)
757 ret |= t;
758 else
759 ret &= ~t;
760 ret = menelaus_write_reg(MENELAUS_GPIO_CTRL, ret);
761out:
762 mutex_unlock(&the_menelaus->lock);
763 return ret;
764}
765
766/*-----------------------------------------------------------------------*/
767
768/* Handles Menelaus interrupts. Does not run in interrupt context */
769static void menelaus_work(struct work_struct *_menelaus)
770{
771 struct menelaus_chip *menelaus =
772 container_of(_menelaus, struct menelaus_chip, work);
773 void (*handler)(struct menelaus_chip *menelaus);
774
775 while (1) {
776 unsigned isr;
777
778 isr = (menelaus_read_reg(MENELAUS_INT_STATUS2)
779 & ~menelaus->mask2) << 8;
780 isr |= menelaus_read_reg(MENELAUS_INT_STATUS1)
781 & ~menelaus->mask1;
782 if (!isr)
783 break;
784
785 while (isr) {
786 int irq = fls(isr) - 1;
787 isr &= ~(1 << irq);
788
789 mutex_lock(&menelaus->lock);
790 menelaus_disable_irq(irq);
791 menelaus_ack_irq(irq);
792 handler = menelaus->handlers[irq];
793 if (handler)
794 handler(menelaus);
795 menelaus_enable_irq(irq);
796 mutex_unlock(&menelaus->lock);
797 }
798 }
799 enable_irq(menelaus->client->irq);
800}
801
802/*
803 * We cannot use I2C in interrupt context, so we just schedule work.
804 */
805static irqreturn_t menelaus_irq(int irq, void *_menelaus)
806{
807 struct menelaus_chip *menelaus = _menelaus;
808
809 disable_irq_nosync(irq);
810 (void)schedule_work(&menelaus->work);
811
812 return IRQ_HANDLED;
813}
814
815/*-----------------------------------------------------------------------*/
816
817/*
818 * The RTC needs to be set once, then it runs on backup battery power.
819 * It supports alarms, including system wake alarms (from some modes);
820 * and 1/second IRQs if requested.
821 */
822#ifdef CONFIG_RTC_DRV_TWL92330
823
824#define RTC_CTRL_RTC_EN (1 << 0)
825#define RTC_CTRL_AL_EN (1 << 1)
826#define RTC_CTRL_MODE12 (1 << 2)
827#define RTC_CTRL_EVERY_MASK (3 << 3)
828#define RTC_CTRL_EVERY_SEC (0 << 3)
829#define RTC_CTRL_EVERY_MIN (1 << 3)
830#define RTC_CTRL_EVERY_HR (2 << 3)
831#define RTC_CTRL_EVERY_DAY (3 << 3)
832
833#define RTC_UPDATE_EVERY 0x08
834
835#define RTC_HR_PM (1 << 7)
836
837static void menelaus_to_time(char *regs, struct rtc_time *t)
838{
839 t->tm_sec = BCD2BIN(regs[0]);
840 t->tm_min = BCD2BIN(regs[1]);
841 if (the_menelaus->rtc_control & RTC_CTRL_MODE12) {
842 t->tm_hour = BCD2BIN(regs[2] & 0x1f) - 1;
843 if (regs[2] & RTC_HR_PM)
844 t->tm_hour += 12;
845 } else
846 t->tm_hour = BCD2BIN(regs[2] & 0x3f);
847 t->tm_mday = BCD2BIN(regs[3]);
848 t->tm_mon = BCD2BIN(regs[4]) - 1;
849 t->tm_year = BCD2BIN(regs[5]) + 100;
850}
851
852static int time_to_menelaus(struct rtc_time *t, int regnum)
853{
854 int hour, status;
855
856 status = menelaus_write_reg(regnum++, BIN2BCD(t->tm_sec));
857 if (status < 0)
858 goto fail;
859
860 status = menelaus_write_reg(regnum++, BIN2BCD(t->tm_min));
861 if (status < 0)
862 goto fail;
863
864 if (the_menelaus->rtc_control & RTC_CTRL_MODE12) {
865 hour = t->tm_hour + 1;
866 if (hour > 12)
867 hour = RTC_HR_PM | BIN2BCD(hour - 12);
868 else
869 hour = BIN2BCD(hour);
870 } else
871 hour = BIN2BCD(t->tm_hour);
872 status = menelaus_write_reg(regnum++, hour);
873 if (status < 0)
874 goto fail;
875
876 status = menelaus_write_reg(regnum++, BIN2BCD(t->tm_mday));
877 if (status < 0)
878 goto fail;
879
880 status = menelaus_write_reg(regnum++, BIN2BCD(t->tm_mon + 1));
881 if (status < 0)
882 goto fail;
883
884 status = menelaus_write_reg(regnum++, BIN2BCD(t->tm_year - 100));
885 if (status < 0)
886 goto fail;
887
888 return 0;
889fail:
890 dev_err(&the_menelaus->client->dev, "rtc write reg %02x, err %d\n",
891 --regnum, status);
892 return status;
893}
894
895static int menelaus_read_time(struct device *dev, struct rtc_time *t)
896{
897 struct i2c_msg msg[2];
898 char regs[7];
899 int status;
900
901 /* block read date and time registers */
902 regs[0] = MENELAUS_RTC_SEC;
903
904 msg[0].addr = MENELAUS_I2C_ADDRESS;
905 msg[0].flags = 0;
906 msg[0].len = 1;
907 msg[0].buf = regs;
908
909 msg[1].addr = MENELAUS_I2C_ADDRESS;
910 msg[1].flags = I2C_M_RD;
911 msg[1].len = sizeof(regs);
912 msg[1].buf = regs;
913
914 status = i2c_transfer(the_menelaus->client->adapter, msg, 2);
915 if (status != 2) {
916 dev_err(dev, "%s error %d\n", "read", status);
917 return -EIO;
918 }
919
920 menelaus_to_time(regs, t);
921 t->tm_wday = BCD2BIN(regs[6]);
922
923 return 0;
924}
925
926static int menelaus_set_time(struct device *dev, struct rtc_time *t)
927{
928 int status;
929
930 /* write date and time registers */
931 status = time_to_menelaus(t, MENELAUS_RTC_SEC);
932 if (status < 0)
933 return status;
934 status = menelaus_write_reg(MENELAUS_RTC_WKDAY, BIN2BCD(t->tm_wday));
935 if (status < 0) {
936 dev_err(&the_menelaus->client->dev, "rtc write reg %02x",
937 "err %d\n", MENELAUS_RTC_WKDAY, status);
938 return status;
939 }
940
941 /* now commit the write */
942 status = menelaus_write_reg(MENELAUS_RTC_UPDATE, RTC_UPDATE_EVERY);
943 if (status < 0)
944 dev_err(&the_menelaus->client->dev, "rtc commit time, err %d\n",
945 status);
946
947 return 0;
948}
949
950static int menelaus_read_alarm(struct device *dev, struct rtc_wkalrm *w)
951{
952 struct i2c_msg msg[2];
953 char regs[6];
954 int status;
955
956 /* block read alarm registers */
957 regs[0] = MENELAUS_RTC_AL_SEC;
958
959 msg[0].addr = MENELAUS_I2C_ADDRESS;
960 msg[0].flags = 0;
961 msg[0].len = 1;
962 msg[0].buf = regs;
963
964 msg[1].addr = MENELAUS_I2C_ADDRESS;
965 msg[1].flags = I2C_M_RD;
966 msg[1].len = sizeof(regs);
967 msg[1].buf = regs;
968
969 status = i2c_transfer(the_menelaus->client->adapter, msg, 2);
970 if (status != 2) {
971 dev_err(dev, "%s error %d\n", "alarm read", status);
972 return -EIO;
973 }
974
975 menelaus_to_time(regs, &w->time);
976
977 w->enabled = !!(the_menelaus->rtc_control & RTC_CTRL_AL_EN);
978
979 /* NOTE we *could* check if actually pending... */
980 w->pending = 0;
981
982 return 0;
983}
984
985static int menelaus_set_alarm(struct device *dev, struct rtc_wkalrm *w)
986{
987 int status;
988
989 if (the_menelaus->client->irq <= 0 && w->enabled)
990 return -ENODEV;
991
992 /* clear previous alarm enable */
993 if (the_menelaus->rtc_control & RTC_CTRL_AL_EN) {
994 the_menelaus->rtc_control &= ~RTC_CTRL_AL_EN;
995 status = menelaus_write_reg(MENELAUS_RTC_CTRL,
996 the_menelaus->rtc_control);
997 if (status < 0)
998 return status;
999 }
1000
1001 /* write alarm registers */
1002 status = time_to_menelaus(&w->time, MENELAUS_RTC_AL_SEC);
1003 if (status < 0)
1004 return status;
1005
1006 /* enable alarm if requested */
1007 if (w->enabled) {
1008 the_menelaus->rtc_control |= RTC_CTRL_AL_EN;
1009 status = menelaus_write_reg(MENELAUS_RTC_CTRL,
1010 the_menelaus->rtc_control);
1011 }
1012
1013 return status;
1014}
1015
1016#ifdef CONFIG_RTC_INTF_DEV
1017
1018static void menelaus_rtc_update_work(struct menelaus_chip *m)
1019{
1020 /* report 1/sec update */
1021 local_irq_disable();
1022 rtc_update_irq(m->rtc, 1, RTC_IRQF | RTC_UF);
1023 local_irq_enable();
1024}
1025
1026static int menelaus_ioctl(struct device *dev, unsigned cmd, unsigned long arg)
1027{
1028 int status;
1029
1030 if (the_menelaus->client->irq <= 0)
1031 return -ENOIOCTLCMD;
1032
1033 switch (cmd) {
1034 /* alarm IRQ */
1035 case RTC_AIE_ON:
1036 if (the_menelaus->rtc_control & RTC_CTRL_AL_EN)
1037 return 0;
1038 the_menelaus->rtc_control |= RTC_CTRL_AL_EN;
1039 break;
1040 case RTC_AIE_OFF:
1041 if (!(the_menelaus->rtc_control & RTC_CTRL_AL_EN))
1042 return 0;
1043 the_menelaus->rtc_control &= ~RTC_CTRL_AL_EN;
1044 break;
1045 /* 1/second "update" IRQ */
1046 case RTC_UIE_ON:
1047 if (the_menelaus->uie)
1048 return 0;
1049 status = menelaus_remove_irq_work(MENELAUS_RTCTMR_IRQ);
1050 status = menelaus_add_irq_work(MENELAUS_RTCTMR_IRQ,
1051 menelaus_rtc_update_work);
1052 if (status == 0)
1053 the_menelaus->uie = 1;
1054 return status;
1055 case RTC_UIE_OFF:
1056 if (!the_menelaus->uie)
1057 return 0;
1058 status = menelaus_remove_irq_work(MENELAUS_RTCTMR_IRQ);
1059 if (status == 0)
1060 the_menelaus->uie = 0;
1061 return status;
1062 default:
1063 return -ENOIOCTLCMD;
1064 }
1065 return menelaus_write_reg(MENELAUS_RTC_CTRL, the_menelaus->rtc_control);
1066}
1067
1068#else
1069#define menelaus_ioctl NULL
1070#endif
1071
1072/* REVISIT no compensation register support ... */
1073
1074static const struct rtc_class_ops menelaus_rtc_ops = {
1075 .ioctl = menelaus_ioctl,
1076 .read_time = menelaus_read_time,
1077 .set_time = menelaus_set_time,
1078 .read_alarm = menelaus_read_alarm,
1079 .set_alarm = menelaus_set_alarm,
1080};
1081
1082static void menelaus_rtc_alarm_work(struct menelaus_chip *m)
1083{
1084 /* report alarm */
1085 local_irq_disable();
1086 rtc_update_irq(m->rtc, 1, RTC_IRQF | RTC_AF);
1087 local_irq_enable();
1088
1089 /* then disable it; alarms are oneshot */
1090 the_menelaus->rtc_control &= ~RTC_CTRL_AL_EN;
1091 menelaus_write_reg(MENELAUS_RTC_CTRL, the_menelaus->rtc_control);
1092}
1093
1094static inline void menelaus_rtc_init(struct menelaus_chip *m)
1095{
1096 int alarm = (m->client->irq > 0);
1097
1098 /* assume 32KDETEN pin is pulled high */
1099 if (!(menelaus_read_reg(MENELAUS_OSC_CTRL) & 0x80)) {
1100 dev_dbg(&m->client->dev, "no 32k oscillator\n");
1101 return;
1102 }
1103
1104 /* support RTC alarm; it can issue wakeups */
1105 if (alarm) {
1106 if (menelaus_add_irq_work(MENELAUS_RTCALM_IRQ,
1107 menelaus_rtc_alarm_work) < 0) {
1108 dev_err(&m->client->dev, "can't handle RTC alarm\n");
1109 return;
1110 }
1111 device_init_wakeup(&m->client->dev, 1);
1112 }
1113
1114 /* be sure RTC is enabled; allow 1/sec irqs; leave 12hr mode alone */
1115 m->rtc_control = menelaus_read_reg(MENELAUS_RTC_CTRL);
1116 if (!(m->rtc_control & RTC_CTRL_RTC_EN)
1117 || (m->rtc_control & RTC_CTRL_AL_EN)
1118 || (m->rtc_control & RTC_CTRL_EVERY_MASK)) {
1119 if (!(m->rtc_control & RTC_CTRL_RTC_EN)) {
1120 dev_warn(&m->client->dev, "rtc clock needs setting\n");
1121 m->rtc_control |= RTC_CTRL_RTC_EN;
1122 }
1123 m->rtc_control &= ~RTC_CTRL_EVERY_MASK;
1124 m->rtc_control &= ~RTC_CTRL_AL_EN;
1125 menelaus_write_reg(MENELAUS_RTC_CTRL, m->rtc_control);
1126 }
1127
1128 m->rtc = rtc_device_register(DRIVER_NAME,
1129 &m->client->dev,
1130 &menelaus_rtc_ops, THIS_MODULE);
1131 if (IS_ERR(m->rtc)) {
1132 if (alarm) {
1133 menelaus_remove_irq_work(MENELAUS_RTCALM_IRQ);
1134 device_init_wakeup(&m->client->dev, 0);
1135 }
1136 dev_err(&m->client->dev, "can't register RTC: %d\n",
1137 (int) PTR_ERR(m->rtc));
1138 the_menelaus->rtc = NULL;
1139 }
1140}
1141
1142#else
1143
1144static inline void menelaus_rtc_init(struct menelaus_chip *m)
1145{
1146 /* nothing */
1147}
1148
1149#endif
1150
1151/*-----------------------------------------------------------------------*/
1152
1153static struct i2c_driver menelaus_i2c_driver;
1154
1155static int menelaus_probe(struct i2c_client *client)
1156{
1157 struct menelaus_chip *menelaus;
1158 int rev = 0, val;
1159 int err = 0;
1160 struct menelaus_platform_data *menelaus_pdata =
1161 client->dev.platform_data;
1162
1163 if (the_menelaus) {
1164 dev_dbg(&client->dev, "only one %s for now\n",
1165 DRIVER_NAME);
1166 return -ENODEV;
1167 }
1168
1169 menelaus = kzalloc(sizeof *menelaus, GFP_KERNEL);
1170 if (!menelaus)
1171 return -ENOMEM;
1172
1173 i2c_set_clientdata(client, menelaus);
1174
1175 the_menelaus = menelaus;
1176 menelaus->client = client;
1177
1178 /* If a true probe check the device */
1179 rev = menelaus_read_reg(MENELAUS_REV);
1180 if (rev < 0) {
1181 pr_err("device not found");
1182 err = -ENODEV;
1183 goto fail1;
1184 }
1185
1186 /* Ack and disable all Menelaus interrupts */
1187 menelaus_write_reg(MENELAUS_INT_ACK1, 0xff);
1188 menelaus_write_reg(MENELAUS_INT_ACK2, 0xff);
1189 menelaus_write_reg(MENELAUS_INT_MASK1, 0xff);
1190 menelaus_write_reg(MENELAUS_INT_MASK2, 0xff);
1191 menelaus->mask1 = 0xff;
1192 menelaus->mask2 = 0xff;
1193
1194 /* Set output buffer strengths */
1195 menelaus_write_reg(MENELAUS_MCT_CTRL1, 0x73);
1196
1197 if (client->irq > 0) {
1198 err = request_irq(client->irq, menelaus_irq, IRQF_DISABLED,
1199 DRIVER_NAME, menelaus);
1200 if (err) {
1201 dev_dbg(&client->dev, "can't get IRQ %d, err %d",
1202 client->irq, err);
1203 goto fail1;
1204 }
1205 }
1206
1207 mutex_init(&menelaus->lock);
1208 INIT_WORK(&menelaus->work, menelaus_work);
1209
1210 pr_info("Menelaus rev %d.%d\n", rev >> 4, rev & 0x0f);
1211
1212 val = menelaus_read_reg(MENELAUS_VCORE_CTRL1);
1213 if (val < 0)
1214 goto fail2;
1215 if (val & (1 << 7))
1216 menelaus->vcore_hw_mode = 1;
1217 else
1218 menelaus->vcore_hw_mode = 0;
1219
1220 if (menelaus_pdata != NULL && menelaus_pdata->late_init != NULL) {
1221 err = menelaus_pdata->late_init(&client->dev);
1222 if (err < 0)
1223 goto fail2;
1224 }
1225
1226 menelaus_rtc_init(menelaus);
1227
1228 return 0;
1229fail2:
1230 free_irq(client->irq, menelaus);
1231 flush_scheduled_work();
1232fail1:
1233 kfree(menelaus);
1234 return err;
1235}
1236
1237static int __exit menelaus_remove(struct i2c_client *client)
1238{
1239 struct menelaus_chip *menelaus = i2c_get_clientdata(client);
1240
1241 free_irq(client->irq, menelaus);
1242 kfree(menelaus);
1243 i2c_set_clientdata(client, NULL);
1244 the_menelaus = NULL;
1245 return 0;
1246}
1247
1248static struct i2c_driver menelaus_i2c_driver = {
1249 .driver = {
1250 .name = DRIVER_NAME,
1251 },
1252 .probe = menelaus_probe,
1253 .remove = __exit_p(menelaus_remove),
1254};
1255
1256static int __init menelaus_init(void)
1257{
1258 int res;
1259
1260 res = i2c_add_driver(&menelaus_i2c_driver);
1261 if (res < 0) {
1262 pr_err("driver registration failed\n");
1263 return res;
1264 }
1265
1266 return 0;
1267}
1268
1269static void __exit menelaus_exit(void)
1270{
1271 i2c_del_driver(&menelaus_i2c_driver);
1272
1273 /* FIXME: Shutdown menelaus parts that can be shut down */
1274}
1275
1276MODULE_AUTHOR("Texas Instruments, Inc. (and others)");
1277MODULE_DESCRIPTION("I2C interface for Menelaus.");
1278MODULE_LICENSE("GPL");
1279
1280module_init(menelaus_init);
1281module_exit(menelaus_exit);
diff --git a/drivers/i2c/chips/tsl2550.c b/drivers/i2c/chips/tsl2550.c
new file mode 100644
index 000000000000..3de4b19ba08f
--- /dev/null
+++ b/drivers/i2c/chips/tsl2550.c
@@ -0,0 +1,460 @@
1/*
2 * tsl2550.c - Linux kernel modules for ambient light sensor
3 *
4 * Copyright (C) 2007 Rodolfo Giometti <giometti@linux.it>
5 * Copyright (C) 2007 Eurotech S.p.A. <info@eurotech.it>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22#include <linux/module.h>
23#include <linux/init.h>
24#include <linux/slab.h>
25#include <linux/i2c.h>
26#include <linux/mutex.h>
27#include <linux/delay.h>
28
29#define TSL2550_DRV_NAME "tsl2550"
30#define DRIVER_VERSION "1.1.1"
31
32/*
33 * Defines
34 */
35
36#define TSL2550_POWER_DOWN 0x00
37#define TSL2550_POWER_UP 0x03
38#define TSL2550_STANDARD_RANGE 0x18
39#define TSL2550_EXTENDED_RANGE 0x1d
40#define TSL2550_READ_ADC0 0x43
41#define TSL2550_READ_ADC1 0x83
42
43/*
44 * Structs
45 */
46
47struct tsl2550_data {
48 struct i2c_client *client;
49 struct mutex update_lock;
50
51 unsigned int power_state : 1;
52 unsigned int operating_mode : 1;
53};
54
55/*
56 * Global data
57 */
58
59static const u8 TSL2550_MODE_RANGE[2] = {
60 TSL2550_STANDARD_RANGE, TSL2550_EXTENDED_RANGE,
61};
62
63/*
64 * Management functions
65 */
66
67static int tsl2550_set_operating_mode(struct i2c_client *client, int mode)
68{
69 struct tsl2550_data *data = i2c_get_clientdata(client);
70
71 int ret = i2c_smbus_write_byte(client, TSL2550_MODE_RANGE[mode]);
72
73 data->operating_mode = mode;
74
75 return ret;
76}
77
78static int tsl2550_set_power_state(struct i2c_client *client, int state)
79{
80 struct tsl2550_data *data = i2c_get_clientdata(client);
81 int ret;
82
83 if (state == 0)
84 ret = i2c_smbus_write_byte(client, TSL2550_POWER_DOWN);
85 else {
86 ret = i2c_smbus_write_byte(client, TSL2550_POWER_UP);
87
88 /* On power up we should reset operating mode also... */
89 tsl2550_set_operating_mode(client, data->operating_mode);
90 }
91
92 data->power_state = state;
93
94 return ret;
95}
96
97static int tsl2550_get_adc_value(struct i2c_client *client, u8 cmd)
98{
99 unsigned long end;
100 int loop = 0, ret = 0;
101
102 /*
103 * Read ADC channel waiting at most 400ms (see data sheet for further
104 * info).
105 * To avoid long busy wait we spin for few milliseconds then
106 * start sleeping.
107 */
108 end = jiffies + msecs_to_jiffies(400);
109 while (time_before(jiffies, end)) {
110 i2c_smbus_write_byte(client, cmd);
111
112 if (loop++ < 5)
113 mdelay(1);
114 else
115 msleep(1);
116
117 ret = i2c_smbus_read_byte(client);
118 if (ret < 0)
119 return ret;
120 else if (ret & 0x0080)
121 break;
122 }
123 if (!(ret & 0x80))
124 return -EIO;
125 return ret & 0x7f; /* remove the "valid" bit */
126}
127
128/*
129 * LUX calculation
130 */
131
132#define TSL2550_MAX_LUX 1846
133
134static const u8 ratio_lut[] = {
135 100, 100, 100, 100, 100, 100, 100, 100,
136 100, 100, 100, 100, 100, 100, 99, 99,
137 99, 99, 99, 99, 99, 99, 99, 99,
138 99, 99, 99, 98, 98, 98, 98, 98,
139 98, 98, 97, 97, 97, 97, 97, 96,
140 96, 96, 96, 95, 95, 95, 94, 94,
141 93, 93, 93, 92, 92, 91, 91, 90,
142 89, 89, 88, 87, 87, 86, 85, 84,
143 83, 82, 81, 80, 79, 78, 77, 75,
144 74, 73, 71, 69, 68, 66, 64, 62,
145 60, 58, 56, 54, 52, 49, 47, 44,
146 42, 41, 40, 40, 39, 39, 38, 38,
147 37, 37, 37, 36, 36, 36, 35, 35,
148 35, 35, 34, 34, 34, 34, 33, 33,
149 33, 33, 32, 32, 32, 32, 32, 31,
150 31, 31, 31, 31, 30, 30, 30, 30,
151 30,
152};
153
154static const u16 count_lut[] = {
155 0, 1, 2, 3, 4, 5, 6, 7,
156 8, 9, 10, 11, 12, 13, 14, 15,
157 16, 18, 20, 22, 24, 26, 28, 30,
158 32, 34, 36, 38, 40, 42, 44, 46,
159 49, 53, 57, 61, 65, 69, 73, 77,
160 81, 85, 89, 93, 97, 101, 105, 109,
161 115, 123, 131, 139, 147, 155, 163, 171,
162 179, 187, 195, 203, 211, 219, 227, 235,
163 247, 263, 279, 295, 311, 327, 343, 359,
164 375, 391, 407, 423, 439, 455, 471, 487,
165 511, 543, 575, 607, 639, 671, 703, 735,
166 767, 799, 831, 863, 895, 927, 959, 991,
167 1039, 1103, 1167, 1231, 1295, 1359, 1423, 1487,
168 1551, 1615, 1679, 1743, 1807, 1871, 1935, 1999,
169 2095, 2223, 2351, 2479, 2607, 2735, 2863, 2991,
170 3119, 3247, 3375, 3503, 3631, 3759, 3887, 4015,
171};
172
173/*
174 * This function is described into Taos TSL2550 Designer's Notebook
175 * pages 2, 3.
176 */
177static int tsl2550_calculate_lux(u8 ch0, u8 ch1)
178{
179 unsigned int lux;
180
181 /* Look up count from channel values */
182 u16 c0 = count_lut[ch0];
183 u16 c1 = count_lut[ch1];
184
185 /*
186 * Calculate ratio.
187 * Note: the "128" is a scaling factor
188 */
189 u8 r = 128;
190
191 /* Avoid division by 0 and count 1 cannot be greater than count 0 */
192 if (c0 && (c1 <= c0))
193 r = c1 * 128 / c0;
194 else
195 return -1;
196
197 /* Calculate LUX */
198 lux = ((c0 - c1) * ratio_lut[r]) / 256;
199
200 /* LUX range check */
201 return lux > TSL2550_MAX_LUX ? TSL2550_MAX_LUX : lux;
202}
203
204/*
205 * SysFS support
206 */
207
208static ssize_t tsl2550_show_power_state(struct device *dev,
209 struct device_attribute *attr, char *buf)
210{
211 struct tsl2550_data *data = i2c_get_clientdata(to_i2c_client(dev));
212
213 return sprintf(buf, "%u\n", data->power_state);
214}
215
216static ssize_t tsl2550_store_power_state(struct device *dev,
217 struct device_attribute *attr, const char *buf, size_t count)
218{
219 struct i2c_client *client = to_i2c_client(dev);
220 struct tsl2550_data *data = i2c_get_clientdata(client);
221 unsigned long val = simple_strtoul(buf, NULL, 10);
222 int ret;
223
224 if (val < 0 || val > 1)
225 return -EINVAL;
226
227 mutex_lock(&data->update_lock);
228 ret = tsl2550_set_power_state(client, val);
229 mutex_unlock(&data->update_lock);
230
231 if (ret < 0)
232 return ret;
233
234 return count;
235}
236
237static DEVICE_ATTR(power_state, S_IWUSR | S_IRUGO,
238 tsl2550_show_power_state, tsl2550_store_power_state);
239
240static ssize_t tsl2550_show_operating_mode(struct device *dev,
241 struct device_attribute *attr, char *buf)
242{
243 struct tsl2550_data *data = i2c_get_clientdata(to_i2c_client(dev));
244
245 return sprintf(buf, "%u\n", data->operating_mode);
246}
247
248static ssize_t tsl2550_store_operating_mode(struct device *dev,
249 struct device_attribute *attr, const char *buf, size_t count)
250{
251 struct i2c_client *client = to_i2c_client(dev);
252 struct tsl2550_data *data = i2c_get_clientdata(client);
253 unsigned long val = simple_strtoul(buf, NULL, 10);
254 int ret;
255
256 if (val < 0 || val > 1)
257 return -EINVAL;
258
259 if (data->power_state == 0)
260 return -EBUSY;
261
262 mutex_lock(&data->update_lock);
263 ret = tsl2550_set_operating_mode(client, val);
264 mutex_unlock(&data->update_lock);
265
266 if (ret < 0)
267 return ret;
268
269 return count;
270}
271
272static DEVICE_ATTR(operating_mode, S_IWUSR | S_IRUGO,
273 tsl2550_show_operating_mode, tsl2550_store_operating_mode);
274
275static ssize_t __tsl2550_show_lux(struct i2c_client *client, char *buf)
276{
277 u8 ch0, ch1;
278 int ret;
279
280 ret = tsl2550_get_adc_value(client, TSL2550_READ_ADC0);
281 if (ret < 0)
282 return ret;
283 ch0 = ret;
284
285 mdelay(1);
286
287 ret = tsl2550_get_adc_value(client, TSL2550_READ_ADC1);
288 if (ret < 0)
289 return ret;
290 ch1 = ret;
291
292 /* Do the job */
293 ret = tsl2550_calculate_lux(ch0, ch1);
294 if (ret < 0)
295 return ret;
296
297 return sprintf(buf, "%d\n", ret);
298}
299
300static ssize_t tsl2550_show_lux1_input(struct device *dev,
301 struct device_attribute *attr, char *buf)
302{
303 struct i2c_client *client = to_i2c_client(dev);
304 struct tsl2550_data *data = i2c_get_clientdata(client);
305 int ret;
306
307 /* No LUX data if not operational */
308 if (!data->power_state)
309 return -EBUSY;
310
311 mutex_lock(&data->update_lock);
312 ret = __tsl2550_show_lux(client, buf);
313 mutex_unlock(&data->update_lock);
314
315 return ret;
316}
317
318static DEVICE_ATTR(lux1_input, S_IRUGO,
319 tsl2550_show_lux1_input, NULL);
320
321static struct attribute *tsl2550_attributes[] = {
322 &dev_attr_power_state.attr,
323 &dev_attr_operating_mode.attr,
324 &dev_attr_lux1_input.attr,
325 NULL
326};
327
328static const struct attribute_group tsl2550_attr_group = {
329 .attrs = tsl2550_attributes,
330};
331
332/*
333 * Initialization function
334 */
335
336static int tsl2550_init_client(struct i2c_client *client)
337{
338 struct tsl2550_data *data = i2c_get_clientdata(client);
339 int err;
340
341 /*
342 * Probe the chip. To do so we try to power up the device and then to
343 * read back the 0x03 code
344 */
345 err = i2c_smbus_write_byte(client, TSL2550_POWER_UP);
346 if (err < 0)
347 return err;
348 mdelay(1);
349 if (i2c_smbus_read_byte(client) != TSL2550_POWER_UP)
350 return -ENODEV;
351 data->power_state = 1;
352
353 /* Set the default operating mode */
354 err = i2c_smbus_write_byte(client,
355 TSL2550_MODE_RANGE[data->operating_mode]);
356 if (err < 0)
357 return err;
358
359 return 0;
360}
361
362/*
363 * I2C init/probing/exit functions
364 */
365
366static struct i2c_driver tsl2550_driver;
367static int __devinit tsl2550_probe(struct i2c_client *client)
368{
369 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
370 struct tsl2550_data *data;
371 int *opmode, err = 0;
372
373 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE)) {
374 err = -EIO;
375 goto exit;
376 }
377
378 data = kzalloc(sizeof(struct tsl2550_data), GFP_KERNEL);
379 if (!data) {
380 err = -ENOMEM;
381 goto exit;
382 }
383 data->client = client;
384 i2c_set_clientdata(client, data);
385
386 /* Check platform data */
387 opmode = client->dev.platform_data;
388 if (opmode) {
389 if (*opmode < 0 || *opmode > 1) {
390 dev_err(&client->dev, "invalid operating_mode (%d)\n",
391 *opmode);
392 err = -EINVAL;
393 goto exit_kfree;
394 }
395 data->operating_mode = *opmode;
396 } else
397 data->operating_mode = 0; /* default mode is standard */
398 dev_info(&client->dev, "%s operating mode\n",
399 data->operating_mode ? "extended" : "standard");
400
401 mutex_init(&data->update_lock);
402
403 /* Initialize the TSL2550 chip */
404 err = tsl2550_init_client(client);
405 if (err)
406 goto exit_kfree;
407
408 /* Register sysfs hooks */
409 err = sysfs_create_group(&client->dev.kobj, &tsl2550_attr_group);
410 if (err)
411 goto exit_kfree;
412
413 dev_info(&client->dev, "support ver. %s enabled\n", DRIVER_VERSION);
414
415 return 0;
416
417exit_kfree:
418 kfree(data);
419exit:
420 return err;
421}
422
423static int __devexit tsl2550_remove(struct i2c_client *client)
424{
425 sysfs_remove_group(&client->dev.kobj, &tsl2550_attr_group);
426
427 /* Power down the device */
428 tsl2550_set_power_state(client, 0);
429
430 kfree(i2c_get_clientdata(client));
431
432 return 0;
433}
434
435static struct i2c_driver tsl2550_driver = {
436 .driver = {
437 .name = TSL2550_DRV_NAME,
438 .owner = THIS_MODULE,
439 },
440 .probe = tsl2550_probe,
441 .remove = __devexit_p(tsl2550_remove),
442};
443
444static int __init tsl2550_init(void)
445{
446 return i2c_add_driver(&tsl2550_driver);
447}
448
449static void __exit tsl2550_exit(void)
450{
451 i2c_del_driver(&tsl2550_driver);
452}
453
454MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
455MODULE_DESCRIPTION("TSL2550 ambient light sensor driver");
456MODULE_LICENSE("GPL");
457MODULE_VERSION(DRIVER_VERSION);
458
459module_init(tsl2550_init);
460module_exit(tsl2550_exit);
diff --git a/drivers/i2c/i2c-core.c b/drivers/i2c/i2c-core.c
index 931f34592be9..d663e6960d93 100644
--- a/drivers/i2c/i2c-core.c
+++ b/drivers/i2c/i2c-core.c
@@ -207,6 +207,7 @@ EXPORT_SYMBOL_GPL(i2c_bus_type);
207 * i2c_new_device - instantiate an i2c device for use with a new style driver 207 * i2c_new_device - instantiate an i2c device for use with a new style driver
208 * @adap: the adapter managing the device 208 * @adap: the adapter managing the device
209 * @info: describes one I2C device; bus_num is ignored 209 * @info: describes one I2C device; bus_num is ignored
210 * Context: can sleep
210 * 211 *
211 * Create a device to work with a new style i2c driver, where binding is 212 * Create a device to work with a new style i2c driver, where binding is
212 * handled through driver model probe()/remove() methods. This call is not 213 * handled through driver model probe()/remove() methods. This call is not
@@ -255,6 +256,7 @@ EXPORT_SYMBOL_GPL(i2c_new_device);
255/** 256/**
256 * i2c_unregister_device - reverse effect of i2c_new_device() 257 * i2c_unregister_device - reverse effect of i2c_new_device()
257 * @client: value returned from i2c_new_device() 258 * @client: value returned from i2c_new_device()
259 * Context: can sleep
258 */ 260 */
259void i2c_unregister_device(struct i2c_client *client) 261void i2c_unregister_device(struct i2c_client *client)
260{ 262{
@@ -377,6 +379,7 @@ out_list:
377/** 379/**
378 * i2c_add_adapter - declare i2c adapter, use dynamic bus number 380 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
379 * @adapter: the adapter to add 381 * @adapter: the adapter to add
382 * Context: can sleep
380 * 383 *
381 * This routine is used to declare an I2C adapter when its bus number 384 * This routine is used to declare an I2C adapter when its bus number
382 * doesn't matter. Examples: for I2C adapters dynamically added by 385 * doesn't matter. Examples: for I2C adapters dynamically added by
@@ -414,6 +417,7 @@ EXPORT_SYMBOL(i2c_add_adapter);
414/** 417/**
415 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number 418 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
416 * @adap: the adapter to register (with adap->nr initialized) 419 * @adap: the adapter to register (with adap->nr initialized)
420 * Context: can sleep
417 * 421 *
418 * This routine is used to declare an I2C adapter when its bus number 422 * This routine is used to declare an I2C adapter when its bus number
419 * matters. Example: for I2C adapters from system-on-chip CPUs, or 423 * matters. Example: for I2C adapters from system-on-chip CPUs, or
@@ -461,6 +465,14 @@ retry:
461} 465}
462EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter); 466EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
463 467
468/**
469 * i2c_del_adapter - unregister I2C adapter
470 * @adap: the adapter being unregistered
471 * Context: can sleep
472 *
473 * This unregisters an I2C adapter which was previously registered
474 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
475 */
464int i2c_del_adapter(struct i2c_adapter *adap) 476int i2c_del_adapter(struct i2c_adapter *adap)
465{ 477{
466 struct list_head *item, *_n; 478 struct list_head *item, *_n;
@@ -596,6 +608,7 @@ EXPORT_SYMBOL(i2c_register_driver);
596/** 608/**
597 * i2c_del_driver - unregister I2C driver 609 * i2c_del_driver - unregister I2C driver
598 * @driver: the driver being unregistered 610 * @driver: the driver being unregistered
611 * Context: can sleep
599 */ 612 */
600void i2c_del_driver(struct i2c_driver *driver) 613void i2c_del_driver(struct i2c_driver *driver)
601{ 614{
@@ -1329,10 +1342,14 @@ s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
1329EXPORT_SYMBOL(i2c_smbus_write_block_data); 1342EXPORT_SYMBOL(i2c_smbus_write_block_data);
1330 1343
1331/* Returns the number of read bytes */ 1344/* Returns the number of read bytes */
1332s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command, u8 *values) 1345s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command,
1346 u8 length, u8 *values)
1333{ 1347{
1334 union i2c_smbus_data data; 1348 union i2c_smbus_data data;
1335 1349
1350 if (length > I2C_SMBUS_BLOCK_MAX)
1351 length = I2C_SMBUS_BLOCK_MAX;
1352 data.block[0] = length;
1336 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags, 1353 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1337 I2C_SMBUS_READ,command, 1354 I2C_SMBUS_READ,command,
1338 I2C_SMBUS_I2C_BLOCK_DATA,&data)) 1355 I2C_SMBUS_I2C_BLOCK_DATA,&data))
@@ -1453,7 +1470,7 @@ static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
1453 break; 1470 break;
1454 case I2C_SMBUS_I2C_BLOCK_DATA: 1471 case I2C_SMBUS_I2C_BLOCK_DATA:
1455 if (read_write == I2C_SMBUS_READ) { 1472 if (read_write == I2C_SMBUS_READ) {
1456 msg[1].len = I2C_SMBUS_BLOCK_MAX; 1473 msg[1].len = data->block[0];
1457 } else { 1474 } else {
1458 msg[0].len = data->block[0] + 1; 1475 msg[0].len = data->block[0] + 1;
1459 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) { 1476 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
@@ -1509,9 +1526,7 @@ static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
1509 data->word = msgbuf1[0] | (msgbuf1[1] << 8); 1526 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1510 break; 1527 break;
1511 case I2C_SMBUS_I2C_BLOCK_DATA: 1528 case I2C_SMBUS_I2C_BLOCK_DATA:
1512 /* fixed at 32 for now */ 1529 for (i = 0; i < data->block[0]; i++)
1513 data->block[0] = I2C_SMBUS_BLOCK_MAX;
1514 for (i = 0; i < I2C_SMBUS_BLOCK_MAX; i++)
1515 data->block[i+1] = msgbuf1[i]; 1530 data->block[i+1] = msgbuf1[i];
1516 break; 1531 break;
1517 case I2C_SMBUS_BLOCK_DATA: 1532 case I2C_SMBUS_BLOCK_DATA:
diff --git a/drivers/i2c/i2c-dev.c b/drivers/i2c/i2c-dev.c
index e7a709710592..64eee9551b22 100644
--- a/drivers/i2c/i2c-dev.c
+++ b/drivers/i2c/i2c-dev.c
@@ -283,6 +283,7 @@ static int i2cdev_ioctl(struct inode *inode, struct file *file,
283 (data_arg.size != I2C_SMBUS_WORD_DATA) && 283 (data_arg.size != I2C_SMBUS_WORD_DATA) &&
284 (data_arg.size != I2C_SMBUS_PROC_CALL) && 284 (data_arg.size != I2C_SMBUS_PROC_CALL) &&
285 (data_arg.size != I2C_SMBUS_BLOCK_DATA) && 285 (data_arg.size != I2C_SMBUS_BLOCK_DATA) &&
286 (data_arg.size != I2C_SMBUS_I2C_BLOCK_BROKEN) &&
286 (data_arg.size != I2C_SMBUS_I2C_BLOCK_DATA) && 287 (data_arg.size != I2C_SMBUS_I2C_BLOCK_DATA) &&
287 (data_arg.size != I2C_SMBUS_BLOCK_PROC_CALL)) { 288 (data_arg.size != I2C_SMBUS_BLOCK_PROC_CALL)) {
288 dev_dbg(&client->adapter->dev, 289 dev_dbg(&client->adapter->dev,
@@ -329,10 +330,18 @@ static int i2cdev_ioctl(struct inode *inode, struct file *file,
329 330
330 if ((data_arg.size == I2C_SMBUS_PROC_CALL) || 331 if ((data_arg.size == I2C_SMBUS_PROC_CALL) ||
331 (data_arg.size == I2C_SMBUS_BLOCK_PROC_CALL) || 332 (data_arg.size == I2C_SMBUS_BLOCK_PROC_CALL) ||
333 (data_arg.size == I2C_SMBUS_I2C_BLOCK_DATA) ||
332 (data_arg.read_write == I2C_SMBUS_WRITE)) { 334 (data_arg.read_write == I2C_SMBUS_WRITE)) {
333 if (copy_from_user(&temp, data_arg.data, datasize)) 335 if (copy_from_user(&temp, data_arg.data, datasize))
334 return -EFAULT; 336 return -EFAULT;
335 } 337 }
338 if (data_arg.size == I2C_SMBUS_I2C_BLOCK_BROKEN) {
339 /* Convert old I2C block commands to the new
340 convention. This preserves binary compatibility. */
341 data_arg.size = I2C_SMBUS_I2C_BLOCK_DATA;
342 if (data_arg.read_write == I2C_SMBUS_READ)
343 temp.block[0] = I2C_SMBUS_BLOCK_MAX;
344 }
336 res = i2c_smbus_xfer(client->adapter,client->addr,client->flags, 345 res = i2c_smbus_xfer(client->adapter,client->addr,client->flags,
337 data_arg.read_write, 346 data_arg.read_write,
338 data_arg.command,data_arg.size,&temp); 347 data_arg.command,data_arg.size,&temp);