aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/i2c
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
committerLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
commit1da177e4c3f41524e886b7f1b8a0c1fc7321cac2 (patch)
tree0bba044c4ce775e45a88a51686b5d9f90697ea9d /drivers/i2c
Linux-2.6.12-rc2v2.6.12-rc2
Initial git repository build. I'm not bothering with the full history, even though we have it. We can create a separate "historical" git archive of that later if we want to, and in the meantime it's about 3.2GB when imported into git - space that would just make the early git days unnecessarily complicated, when we don't have a lot of good infrastructure for it. Let it rip!
Diffstat (limited to 'drivers/i2c')
-rw-r--r--drivers/i2c/Kconfig77
-rw-r--r--drivers/i2c/Makefile15
-rw-r--r--drivers/i2c/algos/Kconfig70
-rw-r--r--drivers/i2c/algos/Makefile14
-rw-r--r--drivers/i2c/algos/i2c-algo-bit.c573
-rw-r--r--drivers/i2c/algos/i2c-algo-ite.c812
-rw-r--r--drivers/i2c/algos/i2c-algo-ite.h117
-rw-r--r--drivers/i2c/algos/i2c-algo-pca.c399
-rw-r--r--drivers/i2c/algos/i2c-algo-pca.h26
-rw-r--r--drivers/i2c/algos/i2c-algo-pcf.c507
-rw-r--r--drivers/i2c/algos/i2c-algo-pcf.h76
-rw-r--r--drivers/i2c/algos/i2c-algo-sgi.c189
-rw-r--r--drivers/i2c/algos/i2c-algo-sibyte.c222
-rw-r--r--drivers/i2c/busses/Kconfig499
-rw-r--r--drivers/i2c/busses/Makefile47
-rw-r--r--drivers/i2c/busses/i2c-ali1535.c543
-rw-r--r--drivers/i2c/busses/i2c-ali1563.c415
-rw-r--r--drivers/i2c/busses/i2c-ali15x3.c532
-rw-r--r--drivers/i2c/busses/i2c-amd756-s4882.c264
-rw-r--r--drivers/i2c/busses/i2c-amd756.c431
-rw-r--r--drivers/i2c/busses/i2c-amd8111.c415
-rw-r--r--drivers/i2c/busses/i2c-au1550.c435
-rw-r--r--drivers/i2c/busses/i2c-au1550.h32
-rw-r--r--drivers/i2c/busses/i2c-elektor.c295
-rw-r--r--drivers/i2c/busses/i2c-frodo.c86
-rw-r--r--drivers/i2c/busses/i2c-hydra.c183
-rw-r--r--drivers/i2c/busses/i2c-i801.c613
-rw-r--r--drivers/i2c/busses/i2c-i810.c260
-rw-r--r--drivers/i2c/busses/i2c-ibm_iic.c819
-rw-r--r--drivers/i2c/busses/i2c-ibm_iic.h124
-rw-r--r--drivers/i2c/busses/i2c-iop3xx.c554
-rw-r--r--drivers/i2c/busses/i2c-iop3xx.h107
-rw-r--r--drivers/i2c/busses/i2c-isa.c72
-rw-r--r--drivers/i2c/busses/i2c-ite.c282
-rw-r--r--drivers/i2c/busses/i2c-ixp2000.c171
-rw-r--r--drivers/i2c/busses/i2c-ixp4xx.c181
-rw-r--r--drivers/i2c/busses/i2c-keywest.c763
-rw-r--r--drivers/i2c/busses/i2c-keywest.h108
-rw-r--r--drivers/i2c/busses/i2c-mpc.c496
-rw-r--r--drivers/i2c/busses/i2c-mv64xxx.c598
-rw-r--r--drivers/i2c/busses/i2c-nforce2.c410
-rw-r--r--drivers/i2c/busses/i2c-parport-light.c175
-rw-r--r--drivers/i2c/busses/i2c-parport.c267
-rw-r--r--drivers/i2c/busses/i2c-parport.h94
-rw-r--r--drivers/i2c/busses/i2c-pca-isa.c184
-rw-r--r--drivers/i2c/busses/i2c-piix4.c490
-rw-r--r--drivers/i2c/busses/i2c-prosavage.c334
-rw-r--r--drivers/i2c/busses/i2c-rpx.c102
-rw-r--r--drivers/i2c/busses/i2c-s3c2410.c938
-rw-r--r--drivers/i2c/busses/i2c-savage4.c205
-rw-r--r--drivers/i2c/busses/i2c-sibyte.c71
-rw-r--r--drivers/i2c/busses/i2c-sis5595.c424
-rw-r--r--drivers/i2c/busses/i2c-sis630.c523
-rw-r--r--drivers/i2c/busses/i2c-sis96x.c358
-rw-r--r--drivers/i2c/busses/i2c-stub.c143
-rw-r--r--drivers/i2c/busses/i2c-via.c185
-rw-r--r--drivers/i2c/busses/i2c-viapro.c458
-rw-r--r--drivers/i2c/busses/i2c-voodoo3.c254
-rw-r--r--drivers/i2c/busses/scx200_acb.c557
-rw-r--r--drivers/i2c/busses/scx200_i2c.c131
-rw-r--r--drivers/i2c/chips/Kconfig443
-rw-r--r--drivers/i2c/chips/Makefile48
-rw-r--r--drivers/i2c/chips/adm1021.c411
-rw-r--r--drivers/i2c/chips/adm1025.c574
-rw-r--r--drivers/i2c/chips/adm1026.c1754
-rw-r--r--drivers/i2c/chips/adm1031.c977
-rw-r--r--drivers/i2c/chips/asb100.c1066
-rw-r--r--drivers/i2c/chips/ds1337.c402
-rw-r--r--drivers/i2c/chips/ds1621.c341
-rw-r--r--drivers/i2c/chips/eeprom.c264
-rw-r--r--drivers/i2c/chips/fscher.c692
-rw-r--r--drivers/i2c/chips/fscpos.c641
-rw-r--r--drivers/i2c/chips/gl518sm.c605
-rw-r--r--drivers/i2c/chips/gl520sm.c769
-rw-r--r--drivers/i2c/chips/isp1301_omap.c1658
-rw-r--r--drivers/i2c/chips/it87.c1208
-rw-r--r--drivers/i2c/chips/lm63.c581
-rw-r--r--drivers/i2c/chips/lm75.c297
-rw-r--r--drivers/i2c/chips/lm75.h49
-rw-r--r--drivers/i2c/chips/lm77.c421
-rw-r--r--drivers/i2c/chips/lm78.c796
-rw-r--r--drivers/i2c/chips/lm80.c602
-rw-r--r--drivers/i2c/chips/lm83.c412
-rw-r--r--drivers/i2c/chips/lm85.c1578
-rw-r--r--drivers/i2c/chips/lm87.c829
-rw-r--r--drivers/i2c/chips/lm90.c626
-rw-r--r--drivers/i2c/chips/lm92.c429
-rw-r--r--drivers/i2c/chips/m41t00.c246
-rw-r--r--drivers/i2c/chips/max1619.c373
-rw-r--r--drivers/i2c/chips/pc87360.c1349
-rw-r--r--drivers/i2c/chips/pcf8574.c229
-rw-r--r--drivers/i2c/chips/pcf8591.c316
-rw-r--r--drivers/i2c/chips/rtc8564.c394
-rw-r--r--drivers/i2c/chips/rtc8564.h78
-rw-r--r--drivers/i2c/chips/sis5595.c816
-rw-r--r--drivers/i2c/chips/smsc47b397.c352
-rw-r--r--drivers/i2c/chips/smsc47m1.c591
-rw-r--r--drivers/i2c/chips/via686a.c879
-rw-r--r--drivers/i2c/chips/w83627hf.c1511
-rw-r--r--drivers/i2c/chips/w83781d.c1664
-rw-r--r--drivers/i2c/chips/w83l785ts.c329
-rw-r--r--drivers/i2c/i2c-core.c1272
-rw-r--r--drivers/i2c/i2c-dev.c552
-rw-r--r--drivers/i2c/i2c-sensor-detect.c145
-rw-r--r--drivers/i2c/i2c-sensor-vid.c98
105 files changed, 48392 insertions, 0 deletions
diff --git a/drivers/i2c/Kconfig b/drivers/i2c/Kconfig
new file mode 100644
index 000000000000..24383afdda76
--- /dev/null
+++ b/drivers/i2c/Kconfig
@@ -0,0 +1,77 @@
1#
2# Character device configuration
3#
4
5menu "I2C support"
6
7config I2C
8 tristate "I2C support"
9 ---help---
10 I2C (pronounce: I-square-C) is a slow serial bus protocol used in
11 many micro controller applications and developed by Philips. SMBus,
12 or System Management Bus is a subset of the I2C protocol. More
13 information is contained in the directory <file:Documentation/i2c/>,
14 especially in the file called "summary" there.
15
16 Both I2C and SMBus are supported here. You will need this for
17 hardware sensors support, and also for Video For Linux support.
18
19 If you want I2C support, you should say Y here and also to the
20 specific driver for your bus adapter(s) below.
21
22 This I2C support can also be built as a module. If so, the module
23 will be called i2c-core.
24
25config I2C_CHARDEV
26 tristate "I2C device interface"
27 depends on I2C
28 help
29 Say Y here to use i2c-* device files, usually found in the /dev
30 directory on your system. They make it possible to have user-space
31 programs use the I2C bus. Information on how to do this is
32 contained in the file <file:Documentation/i2c/dev-interface>.
33
34 This support is also available as a module. If so, the module
35 will be called i2c-dev.
36
37source drivers/i2c/algos/Kconfig
38source drivers/i2c/busses/Kconfig
39source drivers/i2c/chips/Kconfig
40
41config I2C_DEBUG_CORE
42 bool "I2C Core debugging messages"
43 depends on I2C
44 help
45 Say Y here if you want the I2C core to produce a bunch of debug
46 messages to the system log. Select this if you are having a
47 problem with I2C support and want to see more of what is going on.
48
49config I2C_DEBUG_ALGO
50 bool "I2C Algorithm debugging messages"
51 depends on I2C
52 help
53 Say Y here if you want the I2C algorithm drivers to produce a bunch
54 of debug messages to the system log. Select this if you are having
55 a problem with I2C support and want to see more of what is going
56 on.
57
58config I2C_DEBUG_BUS
59 bool "I2C Bus debugging messages"
60 depends on I2C
61 help
62 Say Y here if you want the I2C bus drivers to produce a bunch of
63 debug messages to the system log. Select this if you are having
64 a problem with I2C support and want to see more of what is going
65 on.
66
67config I2C_DEBUG_CHIP
68 bool "I2C Chip debugging messages"
69 depends on I2C
70 help
71 Say Y here if you want the I2C chip drivers to produce a bunch of
72 debug messages to the system log. Select this if you are having
73 a problem with I2C support and want to see more of what is going
74 on.
75
76endmenu
77
diff --git a/drivers/i2c/Makefile b/drivers/i2c/Makefile
new file mode 100644
index 000000000000..cd170395a8c7
--- /dev/null
+++ b/drivers/i2c/Makefile
@@ -0,0 +1,15 @@
1#
2# Makefile for the i2c core.
3#
4
5obj-$(CONFIG_I2C) += i2c-core.o
6obj-$(CONFIG_I2C_CHARDEV) += i2c-dev.o
7obj-$(CONFIG_I2C_SENSOR) += i2c-sensor.o
8obj-y += busses/ chips/ algos/
9
10i2c-sensor-objs := i2c-sensor-detect.o i2c-sensor-vid.o
11
12
13ifeq ($(CONFIG_I2C_DEBUG_CORE),y)
14EXTRA_CFLAGS += -DDEBUG
15endif
diff --git a/drivers/i2c/algos/Kconfig b/drivers/i2c/algos/Kconfig
new file mode 100644
index 000000000000..30408015d231
--- /dev/null
+++ b/drivers/i2c/algos/Kconfig
@@ -0,0 +1,70 @@
1#
2# Character device configuration
3#
4
5menu "I2C Algorithms"
6 depends on I2C
7
8config I2C_ALGOBIT
9 tristate "I2C bit-banging interfaces"
10 depends on I2C
11 help
12 This allows you to use a range of I2C adapters called bit-banging
13 adapters. Say Y if you own an I2C adapter belonging to this class
14 and then say Y to the specific driver for you adapter below.
15
16 This support is also available as a module. If so, the module
17 will be called i2c-algo-bit.
18
19config I2C_ALGOPCF
20 tristate "I2C PCF 8584 interfaces"
21 depends on I2C
22 help
23 This allows you to use a range of I2C adapters called PCF adapters.
24 Say Y if you own an I2C adapter belonging to this class and then say
25 Y to the specific driver for you adapter below.
26
27 This support is also available as a module. If so, the module
28 will be called i2c-algo-pcf.
29
30config I2C_ALGOPCA
31 tristate "I2C PCA 9564 interfaces"
32 depends on I2C
33 help
34 This allows you to use a range of I2C adapters called PCA adapters.
35 Say Y if you own an I2C adapter belonging to this class and then say
36 Y to the specific driver for you adapter below.
37
38 This support is also available as a module. If so, the module
39 will be called i2c-algo-pca.
40
41config I2C_ALGOITE
42 tristate "ITE I2C Algorithm"
43 depends on MIPS_ITE8172 && I2C
44 help
45 This supports the use of the ITE8172 I2C interface found on some MIPS
46 systems. Say Y if you have one of these. You should also say Y for
47 the ITE I2C peripheral driver support below.
48
49 This support is also available as a module. If so, the module
50 will be called i2c-algo-ite.
51
52config I2C_ALGO8XX
53 tristate "MPC8xx CPM I2C interface"
54 depends on 8xx && I2C
55
56config I2C_ALGO_SIBYTE
57 tristate "SiByte SMBus interface"
58 depends on SIBYTE_SB1xxx_SOC && I2C
59 help
60 Supports the SiByte SOC on-chip I2C interfaces (2 channels).
61
62config I2C_ALGO_SGI
63 tristate "I2C SGI interfaces"
64 depends on I2C && (SGI_IP22 || SGI_IP32 || X86_VISWS)
65 help
66 Supports the SGI interfaces like the ones found on SGI Indy VINO
67 or SGI O2 MACE.
68
69endmenu
70
diff --git a/drivers/i2c/algos/Makefile b/drivers/i2c/algos/Makefile
new file mode 100644
index 000000000000..867fe1f67401
--- /dev/null
+++ b/drivers/i2c/algos/Makefile
@@ -0,0 +1,14 @@
1#
2# Makefile for the i2c algorithms
3#
4
5obj-$(CONFIG_I2C_ALGOBIT) += i2c-algo-bit.o
6obj-$(CONFIG_I2C_ALGOPCF) += i2c-algo-pcf.o
7obj-$(CONFIG_I2C_ALGOPCA) += i2c-algo-pca.o
8obj-$(CONFIG_I2C_ALGOITE) += i2c-algo-ite.o
9obj-$(CONFIG_I2C_ALGO_SIBYTE) += i2c-algo-sibyte.o
10obj-$(CONFIG_I2C_ALGO_SGI) += i2c-algo-sgi.o
11
12ifeq ($(CONFIG_I2C_DEBUG_ALGO),y)
13EXTRA_CFLAGS += -DDEBUG
14endif
diff --git a/drivers/i2c/algos/i2c-algo-bit.c b/drivers/i2c/algos/i2c-algo-bit.c
new file mode 100644
index 000000000000..fb5b732238ed
--- /dev/null
+++ b/drivers/i2c/algos/i2c-algo-bit.c
@@ -0,0 +1,573 @@
1/* ------------------------------------------------------------------------- */
2/* i2c-algo-bit.c i2c driver algorithms for bit-shift adapters */
3/* ------------------------------------------------------------------------- */
4/* Copyright (C) 1995-2000 Simon G. Vogl
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19/* ------------------------------------------------------------------------- */
20
21/* With some changes from Frodo Looijaard <frodol@dds.nl>, Kyösti Mälkki
22 <kmalkki@cc.hut.fi> and Jean Delvare <khali@linux-fr.org> */
23
24#include <linux/kernel.h>
25#include <linux/module.h>
26#include <linux/delay.h>
27#include <linux/slab.h>
28#include <linux/init.h>
29#include <linux/errno.h>
30#include <linux/sched.h>
31#include <linux/i2c.h>
32#include <linux/i2c-algo-bit.h>
33
34
35/* ----- global defines ----------------------------------------------- */
36#define DEB(x) if (i2c_debug>=1) x;
37#define DEB2(x) if (i2c_debug>=2) x;
38#define DEBSTAT(x) if (i2c_debug>=3) x; /* print several statistical values*/
39#define DEBPROTO(x) if (i2c_debug>=9) { x; }
40 /* debug the protocol by showing transferred bits */
41
42
43/* ----- global variables --------------------------------------------- */
44
45/* module parameters:
46 */
47static int i2c_debug;
48static int bit_test; /* see if the line-setting functions work */
49
50/* --- setting states on the bus with the right timing: --------------- */
51
52#define setsda(adap,val) adap->setsda(adap->data, val)
53#define setscl(adap,val) adap->setscl(adap->data, val)
54#define getsda(adap) adap->getsda(adap->data)
55#define getscl(adap) adap->getscl(adap->data)
56
57static inline void sdalo(struct i2c_algo_bit_data *adap)
58{
59 setsda(adap,0);
60 udelay(adap->udelay);
61}
62
63static inline void sdahi(struct i2c_algo_bit_data *adap)
64{
65 setsda(adap,1);
66 udelay(adap->udelay);
67}
68
69static inline void scllo(struct i2c_algo_bit_data *adap)
70{
71 setscl(adap,0);
72 udelay(adap->udelay);
73}
74
75/*
76 * Raise scl line, and do checking for delays. This is necessary for slower
77 * devices.
78 */
79static inline int sclhi(struct i2c_algo_bit_data *adap)
80{
81 unsigned long start;
82
83 setscl(adap,1);
84
85 /* Not all adapters have scl sense line... */
86 if (adap->getscl == NULL ) {
87 udelay(adap->udelay);
88 return 0;
89 }
90
91 start=jiffies;
92 while (! getscl(adap) ) {
93 /* the hw knows how to read the clock line,
94 * so we wait until it actually gets high.
95 * This is safer as some chips may hold it low
96 * while they are processing data internally.
97 */
98 if (time_after_eq(jiffies, start+adap->timeout)) {
99 return -ETIMEDOUT;
100 }
101 cond_resched();
102 }
103 DEBSTAT(printk(KERN_DEBUG "needed %ld jiffies\n", jiffies-start));
104 udelay(adap->udelay);
105 return 0;
106}
107
108
109/* --- other auxiliary functions -------------------------------------- */
110static void i2c_start(struct i2c_algo_bit_data *adap)
111{
112 /* assert: scl, sda are high */
113 DEBPROTO(printk("S "));
114 sdalo(adap);
115 scllo(adap);
116}
117
118static void i2c_repstart(struct i2c_algo_bit_data *adap)
119{
120 /* scl, sda may not be high */
121 DEBPROTO(printk(" Sr "));
122 setsda(adap,1);
123 sclhi(adap);
124 udelay(adap->udelay);
125
126 sdalo(adap);
127 scllo(adap);
128}
129
130
131static void i2c_stop(struct i2c_algo_bit_data *adap)
132{
133 DEBPROTO(printk("P\n"));
134 /* assert: scl is low */
135 sdalo(adap);
136 sclhi(adap);
137 sdahi(adap);
138}
139
140
141
142/* send a byte without start cond., look for arbitration,
143 check ackn. from slave */
144/* returns:
145 * 1 if the device acknowledged
146 * 0 if the device did not ack
147 * -ETIMEDOUT if an error occurred (while raising the scl line)
148 */
149static int i2c_outb(struct i2c_adapter *i2c_adap, char c)
150{
151 int i;
152 int sb;
153 int ack;
154 struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
155
156 /* assert: scl is low */
157 for ( i=7 ; i>=0 ; i-- ) {
158 sb = c & ( 1 << i );
159 setsda(adap,sb);
160 udelay(adap->udelay);
161 DEBPROTO(printk(KERN_DEBUG "%d",sb!=0));
162 if (sclhi(adap)<0) { /* timed out */
163 sdahi(adap); /* we don't want to block the net */
164 DEB2(printk(KERN_DEBUG " i2c_outb: 0x%02x, timeout at bit #%d\n", c&0xff, i));
165 return -ETIMEDOUT;
166 };
167 /* do arbitration here:
168 * if ( sb && ! getsda(adap) ) -> ouch! Get out of here.
169 */
170 setscl(adap, 0 );
171 udelay(adap->udelay);
172 }
173 sdahi(adap);
174 if (sclhi(adap)<0){ /* timeout */
175 DEB2(printk(KERN_DEBUG " i2c_outb: 0x%02x, timeout at ack\n", c&0xff));
176 return -ETIMEDOUT;
177 };
178 /* read ack: SDA should be pulled down by slave */
179 ack=getsda(adap); /* ack: sda is pulled low ->success. */
180 DEB2(printk(KERN_DEBUG " i2c_outb: 0x%02x , getsda() = %d\n", c & 0xff, ack));
181
182 DEBPROTO( printk(KERN_DEBUG "[%2.2x]",c&0xff) );
183 DEBPROTO(if (0==ack){ printk(KERN_DEBUG " A ");} else printk(KERN_DEBUG " NA ") );
184 scllo(adap);
185 return 0==ack; /* return 1 if device acked */
186 /* assert: scl is low (sda undef) */
187}
188
189
190static int i2c_inb(struct i2c_adapter *i2c_adap)
191{
192 /* read byte via i2c port, without start/stop sequence */
193 /* acknowledge is sent in i2c_read. */
194 int i;
195 unsigned char indata=0;
196 struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
197
198 /* assert: scl is low */
199 sdahi(adap);
200 for (i=0;i<8;i++) {
201 if (sclhi(adap)<0) { /* timeout */
202 DEB2(printk(KERN_DEBUG " i2c_inb: timeout at bit #%d\n", 7-i));
203 return -ETIMEDOUT;
204 };
205 indata *= 2;
206 if ( getsda(adap) )
207 indata |= 0x01;
208 scllo(adap);
209 }
210 /* assert: scl is low */
211 DEB2(printk(KERN_DEBUG "i2c_inb: 0x%02x\n", indata & 0xff));
212
213 DEBPROTO(printk(KERN_DEBUG " 0x%02x", indata & 0xff));
214 return (int) (indata & 0xff);
215}
216
217/*
218 * Sanity check for the adapter hardware - check the reaction of
219 * the bus lines only if it seems to be idle.
220 */
221static int test_bus(struct i2c_algo_bit_data *adap, char* name) {
222 int scl,sda;
223
224 if (adap->getscl==NULL)
225 printk(KERN_INFO "i2c-algo-bit.o: Testing SDA only, "
226 "SCL is not readable.\n");
227
228 sda=getsda(adap);
229 scl=(adap->getscl==NULL?1:getscl(adap));
230 printk(KERN_DEBUG "i2c-algo-bit.o: (0) scl=%d, sda=%d\n",scl,sda);
231 if (!scl || !sda ) {
232 printk(KERN_WARNING "i2c-algo-bit.o: %s seems to be busy.\n", name);
233 goto bailout;
234 }
235
236 sdalo(adap);
237 sda=getsda(adap);
238 scl=(adap->getscl==NULL?1:getscl(adap));
239 printk(KERN_DEBUG "i2c-algo-bit.o: (1) scl=%d, sda=%d\n",scl,sda);
240 if ( 0 != sda ) {
241 printk(KERN_WARNING "i2c-algo-bit.o: SDA stuck high!\n");
242 goto bailout;
243 }
244 if ( 0 == scl ) {
245 printk(KERN_WARNING "i2c-algo-bit.o: SCL unexpected low "
246 "while pulling SDA low!\n");
247 goto bailout;
248 }
249
250 sdahi(adap);
251 sda=getsda(adap);
252 scl=(adap->getscl==NULL?1:getscl(adap));
253 printk(KERN_DEBUG "i2c-algo-bit.o: (2) scl=%d, sda=%d\n",scl,sda);
254 if ( 0 == sda ) {
255 printk(KERN_WARNING "i2c-algo-bit.o: SDA stuck low!\n");
256 goto bailout;
257 }
258 if ( 0 == scl ) {
259 printk(KERN_WARNING "i2c-algo-bit.o: SCL unexpected low "
260 "while pulling SDA high!\n");
261 goto bailout;
262 }
263
264 scllo(adap);
265 sda=getsda(adap);
266 scl=(adap->getscl==NULL?0:getscl(adap));
267 printk(KERN_DEBUG "i2c-algo-bit.o: (3) scl=%d, sda=%d\n",scl,sda);
268 if ( 0 != scl ) {
269 printk(KERN_WARNING "i2c-algo-bit.o: SCL stuck high!\n");
270 goto bailout;
271 }
272 if ( 0 == sda ) {
273 printk(KERN_WARNING "i2c-algo-bit.o: SDA unexpected low "
274 "while pulling SCL low!\n");
275 goto bailout;
276 }
277
278 sclhi(adap);
279 sda=getsda(adap);
280 scl=(adap->getscl==NULL?1:getscl(adap));
281 printk(KERN_DEBUG "i2c-algo-bit.o: (4) scl=%d, sda=%d\n",scl,sda);
282 if ( 0 == scl ) {
283 printk(KERN_WARNING "i2c-algo-bit.o: SCL stuck low!\n");
284 goto bailout;
285 }
286 if ( 0 == sda ) {
287 printk(KERN_WARNING "i2c-algo-bit.o: SDA unexpected low "
288 "while pulling SCL high!\n");
289 goto bailout;
290 }
291 printk(KERN_INFO "i2c-algo-bit.o: %s passed test.\n",name);
292 return 0;
293bailout:
294 sdahi(adap);
295 sclhi(adap);
296 return -ENODEV;
297}
298
299/* ----- Utility functions
300 */
301
302/* try_address tries to contact a chip for a number of
303 * times before it gives up.
304 * return values:
305 * 1 chip answered
306 * 0 chip did not answer
307 * -x transmission error
308 */
309static inline int try_address(struct i2c_adapter *i2c_adap,
310 unsigned char addr, int retries)
311{
312 struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
313 int i,ret = -1;
314 for (i=0;i<=retries;i++) {
315 ret = i2c_outb(i2c_adap,addr);
316 if (ret==1)
317 break; /* success! */
318 i2c_stop(adap);
319 udelay(5/*adap->udelay*/);
320 if (i==retries) /* no success */
321 break;
322 i2c_start(adap);
323 udelay(adap->udelay);
324 }
325 DEB2(if (i)
326 printk(KERN_DEBUG "i2c-algo-bit.o: Used %d tries to %s client at 0x%02x : %s\n",
327 i+1, addr & 1 ? "read" : "write", addr>>1,
328 ret==1 ? "success" : ret==0 ? "no ack" : "failed, timeout?" )
329 );
330 return ret;
331}
332
333static int sendbytes(struct i2c_adapter *i2c_adap, struct i2c_msg *msg)
334{
335 struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
336 char c;
337 const char *temp = msg->buf;
338 int count = msg->len;
339 unsigned short nak_ok = msg->flags & I2C_M_IGNORE_NAK;
340 int retval;
341 int wrcount=0;
342
343 while (count > 0) {
344 c = *temp;
345 DEB2(dev_dbg(&i2c_adap->dev, "sendbytes: writing %2.2X\n", c&0xff));
346 retval = i2c_outb(i2c_adap,c);
347 if ((retval>0) || (nak_ok && (retval==0))) { /* ok or ignored NAK */
348 count--;
349 temp++;
350 wrcount++;
351 } else { /* arbitration or no acknowledge */
352 dev_err(&i2c_adap->dev, "sendbytes: error - bailout.\n");
353 i2c_stop(adap);
354 return (retval<0)? retval : -EFAULT;
355 /* got a better one ?? */
356 }
357#if 0
358 /* from asm/delay.h */
359 __delay(adap->mdelay * (loops_per_sec / 1000) );
360#endif
361 }
362 return wrcount;
363}
364
365static inline int readbytes(struct i2c_adapter *i2c_adap, struct i2c_msg *msg)
366{
367 int inval;
368 int rdcount=0; /* counts bytes read */
369 struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
370 char *temp = msg->buf;
371 int count = msg->len;
372
373 while (count > 0) {
374 inval = i2c_inb(i2c_adap);
375/*printk("%#02x ",inval); if ( ! (count % 16) ) printk("\n"); */
376 if (inval>=0) {
377 *temp = inval;
378 rdcount++;
379 } else { /* read timed out */
380 printk(KERN_ERR "i2c-algo-bit.o: readbytes: i2c_inb timed out.\n");
381 break;
382 }
383
384 temp++;
385 count--;
386
387 if (msg->flags & I2C_M_NO_RD_ACK)
388 continue;
389
390 if ( count > 0 ) { /* send ack */
391 sdalo(adap);
392 DEBPROTO(printk(" Am "));
393 } else {
394 sdahi(adap); /* neg. ack on last byte */
395 DEBPROTO(printk(" NAm "));
396 }
397 if (sclhi(adap)<0) { /* timeout */
398 sdahi(adap);
399 printk(KERN_ERR "i2c-algo-bit.o: readbytes: Timeout at ack\n");
400 return -ETIMEDOUT;
401 };
402 scllo(adap);
403 sdahi(adap);
404 }
405 return rdcount;
406}
407
408/* doAddress initiates the transfer by generating the start condition (in
409 * try_address) and transmits the address in the necessary format to handle
410 * reads, writes as well as 10bit-addresses.
411 * returns:
412 * 0 everything went okay, the chip ack'ed, or IGNORE_NAK flag was set
413 * -x an error occurred (like: -EREMOTEIO if the device did not answer, or
414 * -ETIMEDOUT, for example if the lines are stuck...)
415 */
416static inline int bit_doAddress(struct i2c_adapter *i2c_adap, struct i2c_msg *msg)
417{
418 unsigned short flags = msg->flags;
419 unsigned short nak_ok = msg->flags & I2C_M_IGNORE_NAK;
420 struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
421
422 unsigned char addr;
423 int ret, retries;
424
425 retries = nak_ok ? 0 : i2c_adap->retries;
426
427 if ( (flags & I2C_M_TEN) ) {
428 /* a ten bit address */
429 addr = 0xf0 | (( msg->addr >> 7) & 0x03);
430 DEB2(printk(KERN_DEBUG "addr0: %d\n",addr));
431 /* try extended address code...*/
432 ret = try_address(i2c_adap, addr, retries);
433 if ((ret != 1) && !nak_ok) {
434 printk(KERN_ERR "died at extended address code.\n");
435 return -EREMOTEIO;
436 }
437 /* the remaining 8 bit address */
438 ret = i2c_outb(i2c_adap,msg->addr & 0x7f);
439 if ((ret != 1) && !nak_ok) {
440 /* the chip did not ack / xmission error occurred */
441 printk(KERN_ERR "died at 2nd address code.\n");
442 return -EREMOTEIO;
443 }
444 if ( flags & I2C_M_RD ) {
445 i2c_repstart(adap);
446 /* okay, now switch into reading mode */
447 addr |= 0x01;
448 ret = try_address(i2c_adap, addr, retries);
449 if ((ret!=1) && !nak_ok) {
450 printk(KERN_ERR "died at extended address code.\n");
451 return -EREMOTEIO;
452 }
453 }
454 } else { /* normal 7bit address */
455 addr = ( msg->addr << 1 );
456 if (flags & I2C_M_RD )
457 addr |= 1;
458 if (flags & I2C_M_REV_DIR_ADDR )
459 addr ^= 1;
460 ret = try_address(i2c_adap, addr, retries);
461 if ((ret!=1) && !nak_ok)
462 return -EREMOTEIO;
463 }
464
465 return 0;
466}
467
468static int bit_xfer(struct i2c_adapter *i2c_adap,
469 struct i2c_msg msgs[], int num)
470{
471 struct i2c_msg *pmsg;
472 struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
473
474 int i,ret;
475 unsigned short nak_ok;
476
477 i2c_start(adap);
478 for (i=0;i<num;i++) {
479 pmsg = &msgs[i];
480 nak_ok = pmsg->flags & I2C_M_IGNORE_NAK;
481 if (!(pmsg->flags & I2C_M_NOSTART)) {
482 if (i) {
483 i2c_repstart(adap);
484 }
485 ret = bit_doAddress(i2c_adap, pmsg);
486 if ((ret != 0) && !nak_ok) {
487 DEB2(printk(KERN_DEBUG "i2c-algo-bit.o: NAK from device addr %2.2x msg #%d\n"
488 ,msgs[i].addr,i));
489 return (ret<0) ? ret : -EREMOTEIO;
490 }
491 }
492 if (pmsg->flags & I2C_M_RD ) {
493 /* read bytes into buffer*/
494 ret = readbytes(i2c_adap, pmsg);
495 DEB2(printk(KERN_DEBUG "i2c-algo-bit.o: read %d bytes.\n",ret));
496 if (ret < pmsg->len ) {
497 return (ret<0)? ret : -EREMOTEIO;
498 }
499 } else {
500 /* write bytes from buffer */
501 ret = sendbytes(i2c_adap, pmsg);
502 DEB2(printk(KERN_DEBUG "i2c-algo-bit.o: wrote %d bytes.\n",ret));
503 if (ret < pmsg->len ) {
504 return (ret<0) ? ret : -EREMOTEIO;
505 }
506 }
507 }
508 i2c_stop(adap);
509 return num;
510}
511
512static u32 bit_func(struct i2c_adapter *adap)
513{
514 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
515 I2C_FUNC_10BIT_ADDR | I2C_FUNC_PROTOCOL_MANGLING;
516}
517
518
519/* -----exported algorithm data: ------------------------------------- */
520
521static struct i2c_algorithm i2c_bit_algo = {
522 .name = "Bit-shift algorithm",
523 .id = I2C_ALGO_BIT,
524 .master_xfer = bit_xfer,
525 .functionality = bit_func,
526};
527
528/*
529 * registering functions to load algorithms at runtime
530 */
531int i2c_bit_add_bus(struct i2c_adapter *adap)
532{
533 struct i2c_algo_bit_data *bit_adap = adap->algo_data;
534
535 if (bit_test) {
536 int ret = test_bus(bit_adap, adap->name);
537 if (ret<0)
538 return -ENODEV;
539 }
540
541 DEB2(dev_dbg(&adap->dev, "hw routines registered.\n"));
542
543 /* register new adapter to i2c module... */
544
545 adap->id |= i2c_bit_algo.id;
546 adap->algo = &i2c_bit_algo;
547
548 adap->timeout = 100; /* default values, should */
549 adap->retries = 3; /* be replaced by defines */
550
551 i2c_add_adapter(adap);
552 return 0;
553}
554
555
556int i2c_bit_del_bus(struct i2c_adapter *adap)
557{
558 return i2c_del_adapter(adap);
559}
560
561EXPORT_SYMBOL(i2c_bit_add_bus);
562EXPORT_SYMBOL(i2c_bit_del_bus);
563
564MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
565MODULE_DESCRIPTION("I2C-Bus bit-banging algorithm");
566MODULE_LICENSE("GPL");
567
568module_param(bit_test, bool, 0);
569module_param(i2c_debug, int, S_IRUGO | S_IWUSR);
570
571MODULE_PARM_DESC(bit_test, "Test the lines of the bus to see if it is stuck");
572MODULE_PARM_DESC(i2c_debug,
573 "debug level - 0 off; 1 normal; 2,3 more verbose; 9 bit-protocol");
diff --git a/drivers/i2c/algos/i2c-algo-ite.c b/drivers/i2c/algos/i2c-algo-ite.c
new file mode 100644
index 000000000000..68e9e6832ca0
--- /dev/null
+++ b/drivers/i2c/algos/i2c-algo-ite.c
@@ -0,0 +1,812 @@
1/*
2 -------------------------------------------------------------------------
3 i2c-algo-ite.c i2c driver algorithms for ITE adapters
4
5 Hai-Pao Fan, MontaVista Software, Inc.
6 hpfan@mvista.com or source@mvista.com
7
8 Copyright 2000 MontaVista Software Inc.
9
10 ---------------------------------------------------------------------------
11 This file was highly leveraged from i2c-algo-pcf.c, which was created
12 by Simon G. Vogl and Hans Berglund:
13
14
15 Copyright (C) 1995-1997 Simon G. Vogl
16 1998-2000 Hans Berglund
17
18 This program is free software; you can redistribute it and/or modify
19 it under the terms of the GNU General Public License as published by
20 the Free Software Foundation; either version 2 of the License, or
21 (at your option) any later version.
22
23 This program is distributed in the hope that it will be useful,
24 but WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 GNU General Public License for more details.
27
28 You should have received a copy of the GNU General Public License
29 along with this program; if not, write to the Free Software
30 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
31/* ------------------------------------------------------------------------- */
32
33/* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi> and
34 Frodo Looijaard <frodol@dds.nl> ,and also from Martin Bailey
35 <mbailey@littlefeet-inc.com> */
36
37#include <linux/kernel.h>
38#include <linux/module.h>
39#include <linux/delay.h>
40#include <linux/slab.h>
41#include <linux/init.h>
42#include <asm/uaccess.h>
43#include <linux/ioport.h>
44#include <linux/errno.h>
45#include <linux/sched.h>
46
47#include <linux/i2c.h>
48#include <linux/i2c-algo-ite.h>
49#include "i2c-algo-ite.h"
50
51#define PM_DSR IT8172_PCI_IO_BASE + IT_PM_DSR
52#define PM_IBSR IT8172_PCI_IO_BASE + IT_PM_DSR + 0x04
53#define GPIO_CCR IT8172_PCI_IO_BASE + IT_GPCCR
54
55#define DEB2(x) if (i2c_debug>=2) x
56#define DEB3(x) if (i2c_debug>=3) x /* print several statistical values*/
57#define DEF_TIMEOUT 16
58
59
60/* module parameters:
61 */
62static int i2c_debug;
63static int iic_test; /* see if the line-setting functions work */
64
65/* --- setting states on the bus with the right timing: --------------- */
66
67#define get_clock(adap) adap->getclock(adap->data)
68#define iic_outw(adap, reg, val) adap->setiic(adap->data, reg, val)
69#define iic_inw(adap, reg) adap->getiic(adap->data, reg)
70
71
72/* --- other auxiliary functions -------------------------------------- */
73
74static void iic_start(struct i2c_algo_iic_data *adap)
75{
76 iic_outw(adap,ITE_I2CHCR,ITE_CMD);
77}
78
79static void iic_stop(struct i2c_algo_iic_data *adap)
80{
81 iic_outw(adap,ITE_I2CHCR,0);
82 iic_outw(adap,ITE_I2CHSR,ITE_I2CHSR_TDI);
83}
84
85static void iic_reset(struct i2c_algo_iic_data *adap)
86{
87 iic_outw(adap, PM_IBSR, iic_inw(adap, PM_IBSR) | 0x80);
88}
89
90
91static int wait_for_bb(struct i2c_algo_iic_data *adap)
92{
93 int timeout = DEF_TIMEOUT;
94 short status;
95
96 status = iic_inw(adap, ITE_I2CHSR);
97#ifndef STUB_I2C
98 while (timeout-- && (status & ITE_I2CHSR_HB)) {
99 udelay(1000); /* How much is this? */
100 status = iic_inw(adap, ITE_I2CHSR);
101 }
102#endif
103 if (timeout<=0) {
104 printk(KERN_ERR "Timeout, host is busy\n");
105 iic_reset(adap);
106 }
107 return(timeout<=0);
108}
109
110/* After we issue a transaction on the IIC bus, this function
111 * is called. It puts this process to sleep until we get an interrupt from
112 * from the controller telling us that the transaction we requested in complete.
113 */
114static int wait_for_pin(struct i2c_algo_iic_data *adap, short *status) {
115
116 int timeout = DEF_TIMEOUT;
117
118 timeout = wait_for_bb(adap);
119 if (timeout) {
120 DEB2(printk("Timeout waiting for host not busy\n");)
121 return -EIO;
122 }
123 timeout = DEF_TIMEOUT;
124
125 *status = iic_inw(adap, ITE_I2CHSR);
126#ifndef STUB_I2C
127 while (timeout-- && !(*status & ITE_I2CHSR_TDI)) {
128 adap->waitforpin();
129 *status = iic_inw(adap, ITE_I2CHSR);
130 }
131#endif
132 if (timeout <= 0)
133 return(-1);
134 else
135 return(0);
136}
137
138static int wait_for_fe(struct i2c_algo_iic_data *adap, short *status)
139{
140 int timeout = DEF_TIMEOUT;
141
142 *status = iic_inw(adap, ITE_I2CFSR);
143#ifndef STUB_I2C
144 while (timeout-- && (*status & ITE_I2CFSR_FE)) {
145 udelay(1000);
146 iic_inw(adap, ITE_I2CFSR);
147 }
148#endif
149 if (timeout <= 0)
150 return(-1);
151 else
152 return(0);
153}
154
155static int iic_init (struct i2c_algo_iic_data *adap)
156{
157 short i;
158
159 /* Clear bit 7 to set I2C to normal operation mode */
160 i=iic_inw(adap, PM_DSR)& 0xff7f;
161 iic_outw(adap, PM_DSR, i);
162
163 /* set IT_GPCCR port C bit 2&3 as function 2 */
164 i = iic_inw(adap, GPIO_CCR) & 0xfc0f;
165 iic_outw(adap,GPIO_CCR,i);
166
167 /* Clear slave address/sub-address */
168 iic_outw(adap,ITE_I2CSAR, 0);
169 iic_outw(adap,ITE_I2CSSAR, 0);
170
171 /* Set clock counter register */
172 iic_outw(adap,ITE_I2CCKCNT, get_clock(adap));
173
174 /* Set START/reSTART/STOP time registers */
175 iic_outw(adap,ITE_I2CSHDR, 0x0a);
176 iic_outw(adap,ITE_I2CRSUR, 0x0a);
177 iic_outw(adap,ITE_I2CPSUR, 0x0a);
178
179 /* Enable interrupts on completing the current transaction */
180 iic_outw(adap,ITE_I2CHCR, ITE_I2CHCR_IE | ITE_I2CHCR_HCE);
181
182 /* Clear transfer count */
183 iic_outw(adap,ITE_I2CFBCR, 0x0);
184
185 DEB2(printk("iic_init: Initialized IIC on ITE 0x%x\n",
186 iic_inw(adap, ITE_I2CHSR)));
187 return 0;
188}
189
190
191/*
192 * Sanity check for the adapter hardware - check the reaction of
193 * the bus lines only if it seems to be idle.
194 */
195static int test_bus(struct i2c_algo_iic_data *adap, char *name) {
196#if 0
197 int scl,sda;
198 sda=getsda(adap);
199 if (adap->getscl==NULL) {
200 printk("test_bus: Warning: Adapter can't read from clock line - skipping test.\n");
201 return 0;
202 }
203 scl=getscl(adap);
204 printk("test_bus: Adapter: %s scl: %d sda: %d -- testing...\n",
205 name,getscl(adap),getsda(adap));
206 if (!scl || !sda ) {
207 printk("test_bus: %s seems to be busy.\n",adap->name);
208 goto bailout;
209 }
210 sdalo(adap);
211 printk("test_bus:1 scl: %d sda: %d \n",getscl(adap),
212 getsda(adap));
213 if ( 0 != getsda(adap) ) {
214 printk("test_bus: %s SDA stuck high!\n",name);
215 sdahi(adap);
216 goto bailout;
217 }
218 if ( 0 == getscl(adap) ) {
219 printk("test_bus: %s SCL unexpected low while pulling SDA low!\n",
220 name);
221 goto bailout;
222 }
223 sdahi(adap);
224 printk("test_bus:2 scl: %d sda: %d \n",getscl(adap),
225 getsda(adap));
226 if ( 0 == getsda(adap) ) {
227 printk("test_bus: %s SDA stuck low!\n",name);
228 sdahi(adap);
229 goto bailout;
230 }
231 if ( 0 == getscl(adap) ) {
232 printk("test_bus: %s SCL unexpected low while SDA high!\n",
233 adap->name);
234 goto bailout;
235 }
236 scllo(adap);
237 printk("test_bus:3 scl: %d sda: %d \n",getscl(adap),
238 getsda(adap));
239 if ( 0 != getscl(adap) ) {
240
241 sclhi(adap);
242 goto bailout;
243 }
244 if ( 0 == getsda(adap) ) {
245 printk("test_bus: %s SDA unexpected low while pulling SCL low!\n",
246 name);
247 goto bailout;
248 }
249 sclhi(adap);
250 printk("test_bus:4 scl: %d sda: %d \n",getscl(adap),
251 getsda(adap));
252 if ( 0 == getscl(adap) ) {
253 printk("test_bus: %s SCL stuck low!\n",name);
254 sclhi(adap);
255 goto bailout;
256 }
257 if ( 0 == getsda(adap) ) {
258 printk("test_bus: %s SDA unexpected low while SCL high!\n",
259 name);
260 goto bailout;
261 }
262 printk("test_bus: %s passed test.\n",name);
263 return 0;
264bailout:
265 sdahi(adap);
266 sclhi(adap);
267 return -ENODEV;
268#endif
269 return (0);
270}
271
272/* ----- Utility functions
273 */
274
275
276/* Verify the device we want to talk to on the IIC bus really exists. */
277static inline int try_address(struct i2c_algo_iic_data *adap,
278 unsigned int addr, int retries)
279{
280 int i, ret = -1;
281 short status;
282
283 for (i=0;i<retries;i++) {
284 iic_outw(adap, ITE_I2CSAR, addr);
285 iic_start(adap);
286 if (wait_for_pin(adap, &status) == 0) {
287 if ((status & ITE_I2CHSR_DNE) == 0) {
288 iic_stop(adap);
289 iic_outw(adap, ITE_I2CFCR, ITE_I2CFCR_FLUSH);
290 ret=1;
291 break; /* success! */
292 }
293 }
294 iic_stop(adap);
295 udelay(adap->udelay);
296 }
297 DEB2(if (i) printk("try_address: needed %d retries for 0x%x\n",i,
298 addr));
299 return ret;
300}
301
302
303static int iic_sendbytes(struct i2c_adapter *i2c_adap,const char *buf,
304 int count)
305{
306 struct i2c_algo_iic_data *adap = i2c_adap->algo_data;
307 int wrcount=0, timeout;
308 short status;
309 int loops, remainder, i, j;
310 union {
311 char byte[2];
312 unsigned short word;
313 } tmp;
314
315 iic_outw(adap, ITE_I2CSSAR, (unsigned short)buf[wrcount++]);
316 count--;
317 if (count == 0)
318 return -EIO;
319
320 loops = count / 32; /* 32-byte FIFO */
321 remainder = count % 32;
322
323 if(loops) {
324 for(i=0; i<loops; i++) {
325
326 iic_outw(adap, ITE_I2CFBCR, 32);
327 for(j=0; j<32/2; j++) {
328 tmp.byte[1] = buf[wrcount++];
329 tmp.byte[0] = buf[wrcount++];
330 iic_outw(adap, ITE_I2CFDR, tmp.word);
331 }
332
333 /* status FIFO overrun */
334 iic_inw(adap, ITE_I2CFSR);
335 iic_inw(adap, ITE_I2CFBCR);
336
337 iic_outw(adap, ITE_I2CHCR, ITE_WRITE); /* Issue WRITE command */
338
339 /* Wait for transmission to complete */
340 timeout = wait_for_pin(adap, &status);
341 if(timeout) {
342 iic_stop(adap);
343 printk("iic_sendbytes: %s write timeout.\n", i2c_adap->name);
344 return -EREMOTEIO; /* got a better one ?? */
345 }
346 if (status & ITE_I2CHSR_DB) {
347 iic_stop(adap);
348 printk("iic_sendbytes: %s write error - no ack.\n", i2c_adap->name);
349 return -EREMOTEIO; /* got a better one ?? */
350 }
351 }
352 }
353 if(remainder) {
354 iic_outw(adap, ITE_I2CFBCR, remainder);
355 for(i=0; i<remainder/2; i++) {
356 tmp.byte[1] = buf[wrcount++];
357 tmp.byte[0] = buf[wrcount++];
358 iic_outw(adap, ITE_I2CFDR, tmp.word);
359 }
360
361 /* status FIFO overrun */
362 iic_inw(adap, ITE_I2CFSR);
363 iic_inw(adap, ITE_I2CFBCR);
364
365 iic_outw(adap, ITE_I2CHCR, ITE_WRITE); /* Issue WRITE command */
366
367 timeout = wait_for_pin(adap, &status);
368 if(timeout) {
369 iic_stop(adap);
370 printk("iic_sendbytes: %s write timeout.\n", i2c_adap->name);
371 return -EREMOTEIO; /* got a better one ?? */
372 }
373#ifndef STUB_I2C
374 if (status & ITE_I2CHSR_DB) {
375 iic_stop(adap);
376 printk("iic_sendbytes: %s write error - no ack.\n", i2c_adap->name);
377 return -EREMOTEIO; /* got a better one ?? */
378 }
379#endif
380 }
381 iic_stop(adap);
382 return wrcount;
383}
384
385
386static int iic_readbytes(struct i2c_adapter *i2c_adap, char *buf, int count,
387 int sread)
388{
389 int rdcount=0, i, timeout;
390 short status;
391 struct i2c_algo_iic_data *adap = i2c_adap->algo_data;
392 int loops, remainder, j;
393 union {
394 char byte[2];
395 unsigned short word;
396 } tmp;
397
398 loops = count / 32; /* 32-byte FIFO */
399 remainder = count % 32;
400
401 if(loops) {
402 for(i=0; i<loops; i++) {
403 iic_outw(adap, ITE_I2CFBCR, 32);
404 if (sread)
405 iic_outw(adap, ITE_I2CHCR, ITE_SREAD);
406 else
407 iic_outw(adap, ITE_I2CHCR, ITE_READ); /* Issue READ command */
408
409 timeout = wait_for_pin(adap, &status);
410 if(timeout) {
411 iic_stop(adap);
412 printk("iic_readbytes: %s read timeout.\n", i2c_adap->name);
413 return (-1);
414 }
415#ifndef STUB_I2C
416 if (status & ITE_I2CHSR_DB) {
417 iic_stop(adap);
418 printk("iic_readbytes: %s read error - no ack.\n", i2c_adap->name);
419 return (-1);
420 }
421#endif
422
423 timeout = wait_for_fe(adap, &status);
424 if(timeout) {
425 iic_stop(adap);
426 printk("iic_readbytes: %s FIFO is empty\n", i2c_adap->name);
427 return (-1);
428 }
429
430 for(j=0; j<32/2; j++) {
431 tmp.word = iic_inw(adap, ITE_I2CFDR);
432 buf[rdcount++] = tmp.byte[1];
433 buf[rdcount++] = tmp.byte[0];
434 }
435
436 /* status FIFO underrun */
437 iic_inw(adap, ITE_I2CFSR);
438
439 }
440 }
441
442
443 if(remainder) {
444 remainder=(remainder+1)/2 * 2;
445 iic_outw(adap, ITE_I2CFBCR, remainder);
446 if (sread)
447 iic_outw(adap, ITE_I2CHCR, ITE_SREAD);
448 else
449 iic_outw(adap, ITE_I2CHCR, ITE_READ); /* Issue READ command */
450
451 timeout = wait_for_pin(adap, &status);
452 if(timeout) {
453 iic_stop(adap);
454 printk("iic_readbytes: %s read timeout.\n", i2c_adap->name);
455 return (-1);
456 }
457#ifndef STUB_I2C
458 if (status & ITE_I2CHSR_DB) {
459 iic_stop(adap);
460 printk("iic_readbytes: %s read error - no ack.\n", i2c_adap->name);
461 return (-1);
462 }
463#endif
464 timeout = wait_for_fe(adap, &status);
465 if(timeout) {
466 iic_stop(adap);
467 printk("iic_readbytes: %s FIFO is empty\n", i2c_adap->name);
468 return (-1);
469 }
470
471 for(i=0; i<(remainder+1)/2; i++) {
472 tmp.word = iic_inw(adap, ITE_I2CFDR);
473 buf[rdcount++] = tmp.byte[1];
474 buf[rdcount++] = tmp.byte[0];
475 }
476
477 /* status FIFO underrun */
478 iic_inw(adap, ITE_I2CFSR);
479
480 }
481
482 iic_stop(adap);
483 return rdcount;
484}
485
486
487/* This function implements combined transactions. Combined
488 * transactions consist of combinations of reading and writing blocks of data.
489 * Each transfer (i.e. a read or a write) is separated by a repeated start
490 * condition.
491 */
492#if 0
493static int iic_combined_transaction(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs, int num)
494{
495 int i;
496 struct i2c_msg *pmsg;
497 int ret;
498
499 DEB2(printk("Beginning combined transaction\n"));
500
501 for(i=0; i<(num-1); i++) {
502 pmsg = &msgs[i];
503 if(pmsg->flags & I2C_M_RD) {
504 DEB2(printk(" This one is a read\n"));
505 ret = iic_readbytes(i2c_adap, pmsg->buf, pmsg->len, IIC_COMBINED_XFER);
506 }
507 else if(!(pmsg->flags & I2C_M_RD)) {
508 DEB2(printk("This one is a write\n"));
509 ret = iic_sendbytes(i2c_adap, pmsg->buf, pmsg->len, IIC_COMBINED_XFER);
510 }
511 }
512 /* Last read or write segment needs to be terminated with a stop */
513 pmsg = &msgs[i];
514
515 if(pmsg->flags & I2C_M_RD) {
516 DEB2(printk("Doing the last read\n"));
517 ret = iic_readbytes(i2c_adap, pmsg->buf, pmsg->len, IIC_SINGLE_XFER);
518 }
519 else if(!(pmsg->flags & I2C_M_RD)) {
520 DEB2(printk("Doing the last write\n"));
521 ret = iic_sendbytes(i2c_adap, pmsg->buf, pmsg->len, IIC_SINGLE_XFER);
522 }
523
524 return ret;
525}
526#endif
527
528
529/* Whenever we initiate a transaction, the first byte clocked
530 * onto the bus after the start condition is the address (7 bit) of the
531 * device we want to talk to. This function manipulates the address specified
532 * so that it makes sense to the hardware when written to the IIC peripheral.
533 *
534 * Note: 10 bit addresses are not supported in this driver, although they are
535 * supported by the hardware. This functionality needs to be implemented.
536 */
537static inline int iic_doAddress(struct i2c_algo_iic_data *adap,
538 struct i2c_msg *msg, int retries)
539{
540 unsigned short flags = msg->flags;
541 unsigned int addr;
542 int ret;
543
544/* Ten bit addresses not supported right now */
545 if ( (flags & I2C_M_TEN) ) {
546#if 0
547 addr = 0xf0 | (( msg->addr >> 7) & 0x03);
548 DEB2(printk("addr0: %d\n",addr));
549 ret = try_address(adap, addr, retries);
550 if (ret!=1) {
551 printk("iic_doAddress: died at extended address code.\n");
552 return -EREMOTEIO;
553 }
554 iic_outw(adap,msg->addr & 0x7f);
555 if (ret != 1) {
556 printk("iic_doAddress: died at 2nd address code.\n");
557 return -EREMOTEIO;
558 }
559 if ( flags & I2C_M_RD ) {
560 i2c_repstart(adap);
561 addr |= 0x01;
562 ret = try_address(adap, addr, retries);
563 if (ret!=1) {
564 printk("iic_doAddress: died at extended address code.\n");
565 return -EREMOTEIO;
566 }
567 }
568#endif
569 } else {
570
571 addr = ( msg->addr << 1 );
572
573#if 0
574 if (flags & I2C_M_RD )
575 addr |= 1;
576 if (flags & I2C_M_REV_DIR_ADDR )
577 addr ^= 1;
578#endif
579
580 if (iic_inw(adap, ITE_I2CSAR) != addr) {
581 iic_outw(adap, ITE_I2CSAR, addr);
582 ret = try_address(adap, addr, retries);
583 if (ret!=1) {
584 printk("iic_doAddress: died at address code.\n");
585 return -EREMOTEIO;
586 }
587 }
588
589 }
590
591 return 0;
592}
593
594
595/* Description: Prepares the controller for a transaction (clearing status
596 * registers, data buffers, etc), and then calls either iic_readbytes or
597 * iic_sendbytes to do the actual transaction.
598 *
599 * still to be done: Before we issue a transaction, we should
600 * verify that the bus is not busy or in some unknown state.
601 */
602static int iic_xfer(struct i2c_adapter *i2c_adap,
603 struct i2c_msg *msgs,
604 int num)
605{
606 struct i2c_algo_iic_data *adap = i2c_adap->algo_data;
607 struct i2c_msg *pmsg;
608 int i = 0;
609 int ret, timeout;
610
611 pmsg = &msgs[i];
612
613 if(!pmsg->len) {
614 DEB2(printk("iic_xfer: read/write length is 0\n");)
615 return -EIO;
616 }
617 if(!(pmsg->flags & I2C_M_RD) && (!(pmsg->len)%2) ) {
618 DEB2(printk("iic_xfer: write buffer length is not odd\n");)
619 return -EIO;
620 }
621
622 /* Wait for any pending transfers to complete */
623 timeout = wait_for_bb(adap);
624 if (timeout) {
625 DEB2(printk("iic_xfer: Timeout waiting for host not busy\n");)
626 return -EIO;
627 }
628
629 /* Flush FIFO */
630 iic_outw(adap, ITE_I2CFCR, ITE_I2CFCR_FLUSH);
631
632 /* Load address */
633 ret = iic_doAddress(adap, pmsg, i2c_adap->retries);
634 if (ret)
635 return -EIO;
636
637#if 0
638 /* Combined transaction (read and write) */
639 if(num > 1) {
640 DEB2(printk("iic_xfer: Call combined transaction\n"));
641 ret = iic_combined_transaction(i2c_adap, msgs, num);
642 }
643#endif
644
645 DEB3(printk("iic_xfer: Msg %d, addr=0x%x, flags=0x%x, len=%d\n",
646 i, msgs[i].addr, msgs[i].flags, msgs[i].len);)
647
648 if(pmsg->flags & I2C_M_RD) /* Read */
649 ret = iic_readbytes(i2c_adap, pmsg->buf, pmsg->len, 0);
650 else { /* Write */
651 udelay(1000);
652 ret = iic_sendbytes(i2c_adap, pmsg->buf, pmsg->len);
653 }
654
655 if (ret != pmsg->len)
656 DEB3(printk("iic_xfer: error or fail on read/write %d bytes.\n",ret));
657 else
658 DEB3(printk("iic_xfer: read/write %d bytes.\n",ret));
659
660 return ret;
661}
662
663
664/* Implements device specific ioctls. Higher level ioctls can
665 * be found in i2c-core.c and are typical of any i2c controller (specifying
666 * slave address, timeouts, etc). These ioctls take advantage of any hardware
667 * features built into the controller for which this algorithm-adapter set
668 * was written. These ioctls allow you to take control of the data and clock
669 * lines and set the either high or low,
670 * similar to a GPIO pin.
671 */
672static int algo_control(struct i2c_adapter *adapter,
673 unsigned int cmd, unsigned long arg)
674{
675
676 struct i2c_algo_iic_data *adap = adapter->algo_data;
677 struct i2c_iic_msg s_msg;
678 char *buf;
679 int ret;
680
681 if (cmd == I2C_SREAD) {
682 if(copy_from_user(&s_msg, (struct i2c_iic_msg *)arg,
683 sizeof(struct i2c_iic_msg)))
684 return -EFAULT;
685 buf = kmalloc(s_msg.len, GFP_KERNEL);
686 if (buf== NULL)
687 return -ENOMEM;
688
689 /* Flush FIFO */
690 iic_outw(adap, ITE_I2CFCR, ITE_I2CFCR_FLUSH);
691
692 /* Load address */
693 iic_outw(adap, ITE_I2CSAR,s_msg.addr<<1);
694 iic_outw(adap, ITE_I2CSSAR,s_msg.waddr & 0xff);
695
696 ret = iic_readbytes(adapter, buf, s_msg.len, 1);
697 if (ret>=0) {
698 if(copy_to_user( s_msg.buf, buf, s_msg.len) )
699 ret = -EFAULT;
700 }
701 kfree(buf);
702 }
703 return 0;
704}
705
706
707static u32 iic_func(struct i2c_adapter *adap)
708{
709 return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_10BIT_ADDR |
710 I2C_FUNC_PROTOCOL_MANGLING;
711}
712
713/* -----exported algorithm data: ------------------------------------- */
714
715static struct i2c_algorithm iic_algo = {
716 .name = "ITE IIC algorithm",
717 .id = I2C_ALGO_IIC,
718 .master_xfer = iic_xfer,
719 .algo_control = algo_control, /* ioctl */
720 .functionality = iic_func,
721};
722
723
724/*
725 * registering functions to load algorithms at runtime
726 */
727int i2c_iic_add_bus(struct i2c_adapter *adap)
728{
729 struct i2c_algo_iic_data *iic_adap = adap->algo_data;
730
731 if (iic_test) {
732 int ret = test_bus(iic_adap, adap->name);
733 if (ret<0)
734 return -ENODEV;
735 }
736
737 DEB2(printk("i2c-algo-ite: hw routines for %s registered.\n",
738 adap->name));
739
740 /* register new adapter to i2c module... */
741
742 adap->id |= iic_algo.id;
743 adap->algo = &iic_algo;
744
745 adap->timeout = 100; /* default values, should */
746 adap->retries = 3; /* be replaced by defines */
747 adap->flags = 0;
748
749 i2c_add_adapter(adap);
750 iic_init(iic_adap);
751
752 return 0;
753}
754
755
756int i2c_iic_del_bus(struct i2c_adapter *adap)
757{
758 int res;
759 if ((res = i2c_del_adapter(adap)) < 0)
760 return res;
761 DEB2(printk("i2c-algo-ite: adapter unregistered: %s\n",adap->name));
762
763 return 0;
764}
765
766
767int __init i2c_algo_iic_init (void)
768{
769 printk(KERN_INFO "ITE iic (i2c) algorithm module\n");
770 return 0;
771}
772
773
774void i2c_algo_iic_exit(void)
775{
776 return;
777}
778
779
780EXPORT_SYMBOL(i2c_iic_add_bus);
781EXPORT_SYMBOL(i2c_iic_del_bus);
782
783/* The MODULE_* macros resolve to nothing if MODULES is not defined
784 * when this file is compiled.
785 */
786MODULE_AUTHOR("MontaVista Software <www.mvista.com>");
787MODULE_DESCRIPTION("ITE iic algorithm");
788MODULE_LICENSE("GPL");
789
790module_param(iic_test, bool, 0);
791module_param(i2c_debug, int, S_IRUGO | S_IWUSR);
792
793MODULE_PARM_DESC(iic_test, "Test if the I2C bus is available");
794MODULE_PARM_DESC(i2c_debug,
795 "debug level - 0 off; 1 normal; 2,3 more verbose; 9 iic-protocol");
796
797
798/* This function resolves to init_module (the function invoked when a module
799 * is loaded via insmod) when this file is compiled with MODULES defined.
800 * Otherwise (i.e. if you want this driver statically linked to the kernel),
801 * a pointer to this function is stored in a table and called
802 * during the initialization of the kernel (in do_basic_setup in /init/main.c)
803 *
804 * All this functionality is complements of the macros defined in linux/init.h
805 */
806module_init(i2c_algo_iic_init);
807
808
809/* If MODULES is defined when this file is compiled, then this function will
810 * resolved to cleanup_module.
811 */
812module_exit(i2c_algo_iic_exit);
diff --git a/drivers/i2c/algos/i2c-algo-ite.h b/drivers/i2c/algos/i2c-algo-ite.h
new file mode 100644
index 000000000000..a8ca3c9b546a
--- /dev/null
+++ b/drivers/i2c/algos/i2c-algo-ite.h
@@ -0,0 +1,117 @@
1/*
2 --------------------------------------------------------------------
3 i2c-ite.h: Global defines for the I2C controller on board the
4 ITE MIPS processor.
5 --------------------------------------------------------------------
6 Hai-Pao Fan, MontaVista Software, Inc.
7 hpfan@mvista.com or source@mvista.com
8
9 Copyright 2001 MontaVista Software Inc.
10
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
15 *
16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
19 * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
22 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
23 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *
27 * You should have received a copy of the GNU General Public License along
28 * with this program; if not, write to the Free Software Foundation, Inc.,
29 * 675 Mass Ave, Cambridge, MA 02139, USA.
30
31 */
32
33#ifndef I2C_ITE_H
34#define I2C_ITE_H 1
35
36#include <asm/it8172/it8172.h>
37
38/* I2C Registers */
39#define ITE_I2CHCR IT8172_PCI_IO_BASE + IT_I2C_BASE + 0x30
40#define ITE_I2CHSR IT8172_PCI_IO_BASE + IT_I2C_BASE + 0x34
41#define ITE_I2CSAR IT8172_PCI_IO_BASE + IT_I2C_BASE + 0x38
42#define ITE_I2CSSAR IT8172_PCI_IO_BASE + IT_I2C_BASE + 0x3c
43#define ITE_I2CCKCNT IT8172_PCI_IO_BASE + IT_I2C_BASE + 0x48
44#define ITE_I2CSHDR IT8172_PCI_IO_BASE + IT_I2C_BASE + 0x4c
45#define ITE_I2CRSUR IT8172_PCI_IO_BASE + IT_I2C_BASE + 0x50
46#define ITE_I2CPSUR IT8172_PCI_IO_BASE + IT_I2C_BASE + 0x54
47
48#define ITE_I2CFDR IT8172_PCI_IO_BASE + IT_I2C_BASE + 0x70
49#define ITE_I2CFBCR IT8172_PCI_IO_BASE + IT_I2C_BASE + 0x74
50#define ITE_I2CFCR IT8172_PCI_IO_BASE + IT_I2C_BASE + 0x78
51#define ITE_I2CFSR IT8172_PCI_IO_BASE + IT_I2C_BASE + 0x7c
52
53
54/* Host Control Register ITE_I2CHCR */
55#define ITE_I2CHCR_HCE 0x01 /* Enable I2C Host Controller */
56#define ITE_I2CHCR_IE 0x02 /* Enable the interrupt after completing
57 the current transaction */
58#define ITE_I2CHCR_CP_W 0x00 /* bit2-4 000 - Write */
59#define ITE_I2CHCR_CP_R 0x08 /* 010 - Current address read */
60#define ITE_I2CHCR_CP_S 0x10 /* 100 - Sequential read */
61#define ITE_I2CHCR_ST 0x20 /* Initiates the I2C host controller to execute
62 the command and send the data programmed in
63 all required registers to I2C bus */
64#define ITE_CMD ITE_I2CHCR_HCE | ITE_I2CHCR_IE | ITE_I2CHCR_ST
65#define ITE_WRITE ITE_CMD | ITE_I2CHCR_CP_W
66#define ITE_READ ITE_CMD | ITE_I2CHCR_CP_R
67#define ITE_SREAD ITE_CMD | ITE_I2CHCR_CP_S
68
69/* Host Status Register ITE_I2CHSR */
70#define ITE_I2CHSR_DB 0x01 /* Device is busy, receives NACK response except
71 in the first and last bytes */
72#define ITE_I2CHSR_DNE 0x02 /* Target address on I2C bus does not exist */
73#define ITE_I2CHSR_TDI 0x04 /* R/W Transaction on I2C bus was completed */
74#define ITE_I2CHSR_HB 0x08 /* Host controller is processing transactions */
75#define ITE_I2CHSR_FER 0x10 /* Error occurs in the FIFO */
76
77/* Slave Address Register ITE_I2CSAR */
78#define ITE_I2CSAR_SA_MASK 0xfe /* Target I2C device address */
79#define ITE_I2CSAR_ASO 0x0100 /* Output 1/0 to I2CAS port when the
80 next slave address is addressed */
81
82/* Slave Sub-address Register ITE_I2CSSAR */
83#define ITE_I2CSSAR_SUBA_MASK 0xff /* Target I2C device sub-address */
84
85/* Clock Counter Register ITE_I2CCKCNT */
86#define ITE_I2CCKCNT_STOP 0x00 /* stop I2C clock */
87#define ITE_I2CCKCNT_HPCC_MASK 0x7f /* SCL high period counter */
88#define ITE_I2CCKCNT_LPCC_MASK 0x7f00 /* SCL low period counter */
89
90/* START Hold Time Register ITE_I2CSHDR */
91/* value is counted based on 16 MHz internal clock */
92#define ITE_I2CSHDR_FM 0x0a /* START condition at fast mode */
93#define ITE_I2CSHDR_SM 0x47 /* START contition at standard mode */
94
95/* (Repeated) START Setup Time Register ITE_I2CRSUR */
96/* value is counted based on 16 MHz internal clock */
97#define ITE_I2CRSUR_FM 0x0a /* repeated START condition at fast mode */
98#define ITE_I2CRSUR_SM 0x50 /* repeated START condition at standard mode */
99
100/* STOP setup Time Register ITE_I2CPSUR */
101
102/* FIFO Data Register ITE_I2CFDR */
103#define ITE_I2CFDR_MASK 0xff
104
105/* FIFO Byte Count Register ITE_I2CFBCR */
106#define ITE_I2CFBCR_MASK 0x3f
107
108/* FIFO Control Register ITE_I2CFCR */
109#define ITE_I2CFCR_FLUSH 0x01 /* Flush FIFO and reset the FIFO point
110 and I2CFSR */
111/* FIFO Status Register ITE_I2CFSR */
112#define ITE_I2CFSR_FO 0x01 /* FIFO is overrun when write */
113#define ITE_I2CFSR_FU 0x02 /* FIFO is underrun when read */
114#define ITE_I2CFSR_FF 0x04 /* FIFO is full when write */
115#define ITE_I2CFSR_FE 0x08 /* FIFO is empty when read */
116
117#endif /* I2C_ITE_H */
diff --git a/drivers/i2c/algos/i2c-algo-pca.c b/drivers/i2c/algos/i2c-algo-pca.c
new file mode 100644
index 000000000000..c3d912cbbbc3
--- /dev/null
+++ b/drivers/i2c/algos/i2c-algo-pca.c
@@ -0,0 +1,399 @@
1/*
2 * i2c-algo-pca.c i2c driver algorithms for PCA9564 adapters
3 * Copyright (C) 2004 Arcom Control Systems
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20#include <linux/kernel.h>
21#include <linux/module.h>
22#include <linux/moduleparam.h>
23#include <linux/delay.h>
24#include <linux/slab.h>
25#include <linux/init.h>
26#include <linux/errno.h>
27#include <linux/i2c.h>
28#include <linux/i2c-algo-pca.h>
29#include "i2c-algo-pca.h"
30
31#define DRIVER "i2c-algo-pca"
32
33#define DEB1(fmt, args...) do { if (i2c_debug>=1) printk(fmt, ## args); } while(0)
34#define DEB2(fmt, args...) do { if (i2c_debug>=2) printk(fmt, ## args); } while(0)
35#define DEB3(fmt, args...) do { if (i2c_debug>=3) printk(fmt, ## args); } while(0)
36
37static int i2c_debug=0;
38
39#define pca_outw(adap, reg, val) adap->write_byte(adap, reg, val)
40#define pca_inw(adap, reg) adap->read_byte(adap, reg)
41
42#define pca_status(adap) pca_inw(adap, I2C_PCA_STA)
43#define pca_clock(adap) adap->get_clock(adap)
44#define pca_own(adap) adap->get_own(adap)
45#define pca_set_con(adap, val) pca_outw(adap, I2C_PCA_CON, val)
46#define pca_get_con(adap) pca_inw(adap, I2C_PCA_CON)
47#define pca_wait(adap) adap->wait_for_interrupt(adap)
48
49/*
50 * Generate a start condition on the i2c bus.
51 *
52 * returns after the start condition has occured
53 */
54static void pca_start(struct i2c_algo_pca_data *adap)
55{
56 int sta = pca_get_con(adap);
57 DEB2("=== START\n");
58 sta |= I2C_PCA_CON_STA;
59 sta &= ~(I2C_PCA_CON_STO|I2C_PCA_CON_SI);
60 pca_set_con(adap, sta);
61 pca_wait(adap);
62}
63
64/*
65 * Generate a repeated start condition on the i2c bus
66 *
67 * return after the repeated start condition has occured
68 */
69static void pca_repeated_start(struct i2c_algo_pca_data *adap)
70{
71 int sta = pca_get_con(adap);
72 DEB2("=== REPEATED START\n");
73 sta |= I2C_PCA_CON_STA;
74 sta &= ~(I2C_PCA_CON_STO|I2C_PCA_CON_SI);
75 pca_set_con(adap, sta);
76 pca_wait(adap);
77}
78
79/*
80 * Generate a stop condition on the i2c bus
81 *
82 * returns after the stop condition has been generated
83 *
84 * STOPs do not generate an interrupt or set the SI flag, since the
85 * part returns the the idle state (0xf8). Hence we don't need to
86 * pca_wait here.
87 */
88static void pca_stop(struct i2c_algo_pca_data *adap)
89{
90 int sta = pca_get_con(adap);
91 DEB2("=== STOP\n");
92 sta |= I2C_PCA_CON_STO;
93 sta &= ~(I2C_PCA_CON_STA|I2C_PCA_CON_SI);
94 pca_set_con(adap, sta);
95}
96
97/*
98 * Send the slave address and R/W bit
99 *
100 * returns after the address has been sent
101 */
102static void pca_address(struct i2c_algo_pca_data *adap,
103 struct i2c_msg *msg)
104{
105 int sta = pca_get_con(adap);
106 int addr;
107
108 addr = ( (0x7f & msg->addr) << 1 );
109 if (msg->flags & I2C_M_RD )
110 addr |= 1;
111 DEB2("=== SLAVE ADDRESS %#04x+%c=%#04x\n",
112 msg->addr, msg->flags & I2C_M_RD ? 'R' : 'W', addr);
113
114 pca_outw(adap, I2C_PCA_DAT, addr);
115
116 sta &= ~(I2C_PCA_CON_STO|I2C_PCA_CON_STA|I2C_PCA_CON_SI);
117 pca_set_con(adap, sta);
118
119 pca_wait(adap);
120}
121
122/*
123 * Transmit a byte.
124 *
125 * Returns after the byte has been transmitted
126 */
127static void pca_tx_byte(struct i2c_algo_pca_data *adap,
128 __u8 b)
129{
130 int sta = pca_get_con(adap);
131 DEB2("=== WRITE %#04x\n", b);
132 pca_outw(adap, I2C_PCA_DAT, b);
133
134 sta &= ~(I2C_PCA_CON_STO|I2C_PCA_CON_STA|I2C_PCA_CON_SI);
135 pca_set_con(adap, sta);
136
137 pca_wait(adap);
138}
139
140/*
141 * Receive a byte
142 *
143 * returns immediately.
144 */
145static void pca_rx_byte(struct i2c_algo_pca_data *adap,
146 __u8 *b, int ack)
147{
148 *b = pca_inw(adap, I2C_PCA_DAT);
149 DEB2("=== READ %#04x %s\n", *b, ack ? "ACK" : "NACK");
150}
151
152/*
153 * Setup ACK or NACK for next received byte and wait for it to arrive.
154 *
155 * Returns after next byte has arrived.
156 */
157static void pca_rx_ack(struct i2c_algo_pca_data *adap,
158 int ack)
159{
160 int sta = pca_get_con(adap);
161
162 sta &= ~(I2C_PCA_CON_STO|I2C_PCA_CON_STA|I2C_PCA_CON_SI|I2C_PCA_CON_AA);
163
164 if ( ack )
165 sta |= I2C_PCA_CON_AA;
166
167 pca_set_con(adap, sta);
168 pca_wait(adap);
169}
170
171/*
172 * Reset the i2c bus / SIO
173 */
174static void pca_reset(struct i2c_algo_pca_data *adap)
175{
176 /* apparently only an external reset will do it. not a lot can be done */
177 printk(KERN_ERR DRIVER ": Haven't figured out how to do a reset yet\n");
178}
179
180static int pca_xfer(struct i2c_adapter *i2c_adap,
181 struct i2c_msg *msgs,
182 int num)
183{
184 struct i2c_algo_pca_data *adap = i2c_adap->algo_data;
185 struct i2c_msg *msg = NULL;
186 int curmsg;
187 int numbytes = 0;
188 int state;
189 int ret;
190
191 state = pca_status(adap);
192 if ( state != 0xF8 ) {
193 dev_dbg(&i2c_adap->dev, "bus is not idle. status is %#04x\n", state );
194 /* FIXME: what to do. Force stop ? */
195 return -EREMOTEIO;
196 }
197
198 DEB1("{{{ XFER %d messages\n", num);
199
200 if (i2c_debug>=2) {
201 for (curmsg = 0; curmsg < num; curmsg++) {
202 int addr, i;
203 msg = &msgs[curmsg];
204
205 addr = (0x7f & msg->addr) ;
206
207 if (msg->flags & I2C_M_RD )
208 printk(KERN_INFO " [%02d] RD %d bytes from %#02x [%#02x, ...]\n",
209 curmsg, msg->len, addr, (addr<<1) | 1);
210 else {
211 printk(KERN_INFO " [%02d] WR %d bytes to %#02x [%#02x%s",
212 curmsg, msg->len, addr, addr<<1,
213 msg->len == 0 ? "" : ", ");
214 for(i=0; i < msg->len; i++)
215 printk("%#04x%s", msg->buf[i], i == msg->len - 1 ? "" : ", ");
216 printk("]\n");
217 }
218 }
219 }
220
221 curmsg = 0;
222 ret = -EREMOTEIO;
223 while (curmsg < num) {
224 state = pca_status(adap);
225
226 DEB3("STATE is 0x%02x\n", state);
227 msg = &msgs[curmsg];
228
229 switch (state) {
230 case 0xf8: /* On reset or stop the bus is idle */
231 pca_start(adap);
232 break;
233
234 case 0x08: /* A START condition has been transmitted */
235 case 0x10: /* A repeated start condition has been transmitted */
236 pca_address(adap, msg);
237 break;
238
239 case 0x18: /* SLA+W has been transmitted; ACK has been received */
240 case 0x28: /* Data byte in I2CDAT has been transmitted; ACK has been received */
241 if (numbytes < msg->len) {
242 pca_tx_byte(adap, msg->buf[numbytes]);
243 numbytes++;
244 break;
245 }
246 curmsg++; numbytes = 0;
247 if (curmsg == num)
248 pca_stop(adap);
249 else
250 pca_repeated_start(adap);
251 break;
252
253 case 0x20: /* SLA+W has been transmitted; NOT ACK has been received */
254 DEB2("NOT ACK received after SLA+W\n");
255 pca_stop(adap);
256 goto out;
257
258 case 0x40: /* SLA+R has been transmitted; ACK has been received */
259 pca_rx_ack(adap, msg->len > 1);
260 break;
261
262 case 0x50: /* Data bytes has been received; ACK has been returned */
263 if (numbytes < msg->len) {
264 pca_rx_byte(adap, &msg->buf[numbytes], 1);
265 numbytes++;
266 pca_rx_ack(adap, numbytes < msg->len - 1);
267 break;
268 }
269 curmsg++; numbytes = 0;
270 if (curmsg == num)
271 pca_stop(adap);
272 else
273 pca_repeated_start(adap);
274 break;
275
276 case 0x48: /* SLA+R has been transmitted; NOT ACK has been received */
277 DEB2("NOT ACK received after SLA+R\n");
278 pca_stop(adap);
279 goto out;
280
281 case 0x30: /* Data byte in I2CDAT has been transmitted; NOT ACK has been received */
282 DEB2("NOT ACK received after data byte\n");
283 goto out;
284
285 case 0x38: /* Arbitration lost during SLA+W, SLA+R or data bytes */
286 DEB2("Arbitration lost\n");
287 goto out;
288
289 case 0x58: /* Data byte has been received; NOT ACK has been returned */
290 if ( numbytes == msg->len - 1 ) {
291 pca_rx_byte(adap, &msg->buf[numbytes], 0);
292 curmsg++; numbytes = 0;
293 if (curmsg == num)
294 pca_stop(adap);
295 else
296 pca_repeated_start(adap);
297 } else {
298 DEB2("NOT ACK sent after data byte received. "
299 "Not final byte. numbytes %d. len %d\n",
300 numbytes, msg->len);
301 pca_stop(adap);
302 goto out;
303 }
304 break;
305 case 0x70: /* Bus error - SDA stuck low */
306 DEB2("BUS ERROR - SDA Stuck low\n");
307 pca_reset(adap);
308 goto out;
309 case 0x90: /* Bus error - SCL stuck low */
310 DEB2("BUS ERROR - SCL Stuck low\n");
311 pca_reset(adap);
312 goto out;
313 case 0x00: /* Bus error during master or slave mode due to illegal START or STOP condition */
314 DEB2("BUS ERROR - Illegal START or STOP\n");
315 pca_reset(adap);
316 goto out;
317 default:
318 printk(KERN_ERR DRIVER ": unhandled SIO state 0x%02x\n", state);
319 break;
320 }
321
322 }
323
324 ret = curmsg;
325 out:
326 DEB1(KERN_CRIT "}}} transfered %d/%d messages. "
327 "status is %#04x. control is %#04x\n",
328 curmsg, num, pca_status(adap),
329 pca_get_con(adap));
330 return ret;
331}
332
333static u32 pca_func(struct i2c_adapter *adap)
334{
335 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
336}
337
338static int pca_init(struct i2c_algo_pca_data *adap)
339{
340 static int freqs[] = {330,288,217,146,88,59,44,36};
341 int own, clock;
342
343 own = pca_own(adap);
344 clock = pca_clock(adap);
345 DEB1(KERN_INFO DRIVER ": own address is %#04x\n", own);
346 DEB1(KERN_INFO DRIVER ": clock freqeuncy is %dkHz\n", freqs[clock]);
347
348 pca_outw(adap, I2C_PCA_ADR, own << 1);
349
350 pca_set_con(adap, I2C_PCA_CON_ENSIO | clock);
351 udelay(500); /* 500 µs for oscilator to stabilise */
352
353 return 0;
354}
355
356static struct i2c_algorithm pca_algo = {
357 .name = "PCA9564 algorithm",
358 .id = I2C_ALGO_PCA,
359 .master_xfer = pca_xfer,
360 .functionality = pca_func,
361};
362
363/*
364 * registering functions to load algorithms at runtime
365 */
366int i2c_pca_add_bus(struct i2c_adapter *adap)
367{
368 struct i2c_algo_pca_data *pca_adap = adap->algo_data;
369 int rval;
370
371 /* register new adapter to i2c module... */
372
373 adap->id |= pca_algo.id;
374 adap->algo = &pca_algo;
375
376 adap->timeout = 100; /* default values, should */
377 adap->retries = 3; /* be replaced by defines */
378
379 rval = pca_init(pca_adap);
380
381 if (!rval)
382 i2c_add_adapter(adap);
383
384 return rval;
385}
386
387int i2c_pca_del_bus(struct i2c_adapter *adap)
388{
389 return i2c_del_adapter(adap);
390}
391
392EXPORT_SYMBOL(i2c_pca_add_bus);
393EXPORT_SYMBOL(i2c_pca_del_bus);
394
395MODULE_AUTHOR("Ian Campbell <icampbell@arcom.com>");
396MODULE_DESCRIPTION("I2C-Bus PCA9564 algorithm");
397MODULE_LICENSE("GPL");
398
399module_param(i2c_debug, int, 0);
diff --git a/drivers/i2c/algos/i2c-algo-pca.h b/drivers/i2c/algos/i2c-algo-pca.h
new file mode 100644
index 000000000000..2fee07e05211
--- /dev/null
+++ b/drivers/i2c/algos/i2c-algo-pca.h
@@ -0,0 +1,26 @@
1#ifndef I2C_PCA9564_H
2#define I2C_PCA9564_H 1
3
4#define I2C_PCA_STA 0x00 /* STATUS Read Only */
5#define I2C_PCA_TO 0x00 /* TIMEOUT Write Only */
6#define I2C_PCA_DAT 0x01 /* DATA Read/Write */
7#define I2C_PCA_ADR 0x02 /* OWN ADR Read/Write */
8#define I2C_PCA_CON 0x03 /* CONTROL Read/Write */
9
10#define I2C_PCA_CON_AA 0x80 /* Assert Acknowledge */
11#define I2C_PCA_CON_ENSIO 0x40 /* Enable */
12#define I2C_PCA_CON_STA 0x20 /* Start */
13#define I2C_PCA_CON_STO 0x10 /* Stop */
14#define I2C_PCA_CON_SI 0x08 /* Serial Interrupt */
15#define I2C_PCA_CON_CR 0x07 /* Clock Rate (MASK) */
16
17#define I2C_PCA_CON_330kHz 0x00
18#define I2C_PCA_CON_288kHz 0x01
19#define I2C_PCA_CON_217kHz 0x02
20#define I2C_PCA_CON_146kHz 0x03
21#define I2C_PCA_CON_88kHz 0x04
22#define I2C_PCA_CON_59kHz 0x05
23#define I2C_PCA_CON_44kHz 0x06
24#define I2C_PCA_CON_36kHz 0x07
25
26#endif /* I2C_PCA9564_H */
diff --git a/drivers/i2c/algos/i2c-algo-pcf.c b/drivers/i2c/algos/i2c-algo-pcf.c
new file mode 100644
index 000000000000..8d087dac32af
--- /dev/null
+++ b/drivers/i2c/algos/i2c-algo-pcf.c
@@ -0,0 +1,507 @@
1/* ------------------------------------------------------------------------- */
2/* i2c-algo-pcf.c i2c driver algorithms for PCF8584 adapters */
3/* ------------------------------------------------------------------------- */
4/* Copyright (C) 1995-1997 Simon G. Vogl
5 1998-2000 Hans Berglund
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/* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi> and
23 Frodo Looijaard <frodol@dds.nl> ,and also from Martin Bailey
24 <mbailey@littlefeet-inc.com> */
25
26/* Partially rewriten by Oleg I. Vdovikin <vdovikin@jscc.ru> to handle multiple
27 messages, proper stop/repstart signaling during receive,
28 added detect code */
29
30#include <linux/kernel.h>
31#include <linux/module.h>
32#include <linux/delay.h>
33#include <linux/slab.h>
34#include <linux/init.h>
35#include <linux/errno.h>
36#include <linux/i2c.h>
37#include <linux/i2c-algo-pcf.h>
38#include "i2c-algo-pcf.h"
39
40
41#define DEB2(x) if (i2c_debug>=2) x
42#define DEB3(x) if (i2c_debug>=3) x /* print several statistical values*/
43#define DEBPROTO(x) if (i2c_debug>=9) x;
44 /* debug the protocol by showing transferred bits */
45#define DEF_TIMEOUT 16
46
47/* module parameters:
48 */
49static int i2c_debug;
50
51/* --- setting states on the bus with the right timing: --------------- */
52
53#define set_pcf(adap, ctl, val) adap->setpcf(adap->data, ctl, val)
54#define get_pcf(adap, ctl) adap->getpcf(adap->data, ctl)
55#define get_own(adap) adap->getown(adap->data)
56#define get_clock(adap) adap->getclock(adap->data)
57#define i2c_outb(adap, val) adap->setpcf(adap->data, 0, val)
58#define i2c_inb(adap) adap->getpcf(adap->data, 0)
59
60/* --- other auxiliary functions -------------------------------------- */
61
62static void i2c_start(struct i2c_algo_pcf_data *adap)
63{
64 DEBPROTO(printk("S "));
65 set_pcf(adap, 1, I2C_PCF_START);
66}
67
68static void i2c_repstart(struct i2c_algo_pcf_data *adap)
69{
70 DEBPROTO(printk(" Sr "));
71 set_pcf(adap, 1, I2C_PCF_REPSTART);
72}
73
74
75static void i2c_stop(struct i2c_algo_pcf_data *adap)
76{
77 DEBPROTO(printk("P\n"));
78 set_pcf(adap, 1, I2C_PCF_STOP);
79}
80
81static int wait_for_bb(struct i2c_algo_pcf_data *adap) {
82
83 int timeout = DEF_TIMEOUT;
84 int status;
85
86 status = get_pcf(adap, 1);
87#ifndef STUB_I2C
88 while (timeout-- && !(status & I2C_PCF_BB)) {
89 udelay(100); /* wait for 100 us */
90 status = get_pcf(adap, 1);
91 }
92#endif
93 if (timeout <= 0) {
94 printk(KERN_ERR "Timeout waiting for Bus Busy\n");
95 }
96
97 return (timeout<=0);
98}
99
100
101static int wait_for_pin(struct i2c_algo_pcf_data *adap, int *status) {
102
103 int timeout = DEF_TIMEOUT;
104
105 *status = get_pcf(adap, 1);
106#ifndef STUB_I2C
107 while (timeout-- && (*status & I2C_PCF_PIN)) {
108 adap->waitforpin();
109 *status = get_pcf(adap, 1);
110 }
111 if (*status & I2C_PCF_LAB) {
112 DEB2(printk(KERN_INFO
113 "i2c-algo-pcf.o: lost arbitration (CSR 0x%02x)\n",
114 *status));
115 /* Cleanup from LAB-- reset and enable ESO.
116 * This resets the PCF8584; since we've lost the bus, no
117 * further attempts should be made by callers to clean up
118 * (no i2c_stop() etc.)
119 */
120 set_pcf(adap, 1, I2C_PCF_PIN);
121 set_pcf(adap, 1, I2C_PCF_ESO);
122 /* TODO: we should pause for a time period sufficient for any
123 * running I2C transaction to complete-- the arbitration
124 * logic won't work properly until the next START is seen.
125 */
126 DEB2(printk(KERN_INFO
127 "i2c-algo-pcf.o: reset LAB condition (CSR 0x%02x)\n",
128 get_pcf(adap,1)));
129 return(-EINTR);
130 }
131#endif
132 if (timeout <= 0)
133 return(-1);
134 else
135 return(0);
136}
137
138/*
139 * This should perform the 'PCF8584 initialization sequence' as described
140 * in the Philips IC12 data book (1995, Aug 29).
141 * There should be a 30 clock cycle wait after reset, I assume this
142 * has been fulfilled.
143 * There should be a delay at the end equal to the longest I2C message
144 * to synchronize the BB-bit (in multimaster systems). How long is
145 * this? I assume 1 second is always long enough.
146 *
147 * vdovikin: added detect code for PCF8584
148 */
149static int pcf_init_8584 (struct i2c_algo_pcf_data *adap)
150{
151 unsigned char temp;
152
153 DEB3(printk(KERN_DEBUG "i2c-algo-pcf.o: PCF state 0x%02x\n", get_pcf(adap, 1)));
154
155 /* S1=0x80: S0 selected, serial interface off */
156 set_pcf(adap, 1, I2C_PCF_PIN);
157 /* check to see S1 now used as R/W ctrl -
158 PCF8584 does that when ESO is zero */
159 if (((temp = get_pcf(adap, 1)) & 0x7f) != (0)) {
160 DEB2(printk(KERN_ERR "i2c-algo-pcf.o: PCF detection failed -- can't select S0 (0x%02x).\n", temp));
161 return -ENXIO; /* definetly not PCF8584 */
162 }
163
164 /* load own address in S0, effective address is (own << 1) */
165 i2c_outb(adap, get_own(adap));
166 /* check it's really written */
167 if ((temp = i2c_inb(adap)) != get_own(adap)) {
168 DEB2(printk(KERN_ERR "i2c-algo-pcf.o: PCF detection failed -- can't set S0 (0x%02x).\n", temp));
169 return -ENXIO;
170 }
171
172 /* S1=0xA0, next byte in S2 */
173 set_pcf(adap, 1, I2C_PCF_PIN | I2C_PCF_ES1);
174 /* check to see S2 now selected */
175 if (((temp = get_pcf(adap, 1)) & 0x7f) != I2C_PCF_ES1) {
176 DEB2(printk(KERN_ERR "i2c-algo-pcf.o: PCF detection failed -- can't select S2 (0x%02x).\n", temp));
177 return -ENXIO;
178 }
179
180 /* load clock register S2 */
181 i2c_outb(adap, get_clock(adap));
182 /* check it's really written, the only 5 lowest bits does matter */
183 if (((temp = i2c_inb(adap)) & 0x1f) != get_clock(adap)) {
184 DEB2(printk(KERN_ERR "i2c-algo-pcf.o: PCF detection failed -- can't set S2 (0x%02x).\n", temp));
185 return -ENXIO;
186 }
187
188 /* Enable serial interface, idle, S0 selected */
189 set_pcf(adap, 1, I2C_PCF_IDLE);
190
191 /* check to see PCF is really idled and we can access status register */
192 if ((temp = get_pcf(adap, 1)) != (I2C_PCF_PIN | I2C_PCF_BB)) {
193 DEB2(printk(KERN_ERR "i2c-algo-pcf.o: PCF detection failed -- can't select S1` (0x%02x).\n", temp));
194 return -ENXIO;
195 }
196
197 printk(KERN_DEBUG "i2c-algo-pcf.o: deteted and initialized PCF8584.\n");
198
199 return 0;
200}
201
202
203/* ----- Utility functions
204 */
205
206static inline int try_address(struct i2c_algo_pcf_data *adap,
207 unsigned char addr, int retries)
208{
209 int i, status, ret = -1;
210 int wfp;
211 for (i=0;i<retries;i++) {
212 i2c_outb(adap, addr);
213 i2c_start(adap);
214 status = get_pcf(adap, 1);
215 if ((wfp = wait_for_pin(adap, &status)) >= 0) {
216 if ((status & I2C_PCF_LRB) == 0) {
217 i2c_stop(adap);
218 break; /* success! */
219 }
220 }
221 if (wfp == -EINTR) {
222 /* arbitration lost */
223 udelay(adap->udelay);
224 return -EINTR;
225 }
226 i2c_stop(adap);
227 udelay(adap->udelay);
228 }
229 DEB2(if (i) printk(KERN_DEBUG "i2c-algo-pcf.o: needed %d retries for %d\n",i,
230 addr));
231 return ret;
232}
233
234
235static int pcf_sendbytes(struct i2c_adapter *i2c_adap, const char *buf,
236 int count, int last)
237{
238 struct i2c_algo_pcf_data *adap = i2c_adap->algo_data;
239 int wrcount, status, timeout;
240
241 for (wrcount=0; wrcount<count; ++wrcount) {
242 DEB2(dev_dbg(&i2c_adap->dev, "i2c_write: writing %2.2X\n",
243 buf[wrcount]&0xff));
244 i2c_outb(adap, buf[wrcount]);
245 timeout = wait_for_pin(adap, &status);
246 if (timeout) {
247 if (timeout == -EINTR) {
248 /* arbitration lost */
249 return -EINTR;
250 }
251 i2c_stop(adap);
252 dev_err(&i2c_adap->dev, "i2c_write: error - timeout.\n");
253 return -EREMOTEIO; /* got a better one ?? */
254 }
255#ifndef STUB_I2C
256 if (status & I2C_PCF_LRB) {
257 i2c_stop(adap);
258 dev_err(&i2c_adap->dev, "i2c_write: error - no ack.\n");
259 return -EREMOTEIO; /* got a better one ?? */
260 }
261#endif
262 }
263 if (last) {
264 i2c_stop(adap);
265 }
266 else {
267 i2c_repstart(adap);
268 }
269
270 return (wrcount);
271}
272
273
274static int pcf_readbytes(struct i2c_adapter *i2c_adap, char *buf,
275 int count, int last)
276{
277 int i, status;
278 struct i2c_algo_pcf_data *adap = i2c_adap->algo_data;
279 int wfp;
280
281 /* increment number of bytes to read by one -- read dummy byte */
282 for (i = 0; i <= count; i++) {
283
284 if ((wfp = wait_for_pin(adap, &status))) {
285 if (wfp == -EINTR) {
286 /* arbitration lost */
287 return -EINTR;
288 }
289 i2c_stop(adap);
290 dev_err(&i2c_adap->dev, "pcf_readbytes timed out.\n");
291 return (-1);
292 }
293
294#ifndef STUB_I2C
295 if ((status & I2C_PCF_LRB) && (i != count)) {
296 i2c_stop(adap);
297 dev_err(&i2c_adap->dev, "i2c_read: i2c_inb, No ack.\n");
298 return (-1);
299 }
300#endif
301
302 if (i == count - 1) {
303 set_pcf(adap, 1, I2C_PCF_ESO);
304 } else
305 if (i == count) {
306 if (last) {
307 i2c_stop(adap);
308 } else {
309 i2c_repstart(adap);
310 }
311 };
312
313 if (i) {
314 buf[i - 1] = i2c_inb(adap);
315 } else {
316 i2c_inb(adap); /* dummy read */
317 }
318 }
319
320 return (i - 1);
321}
322
323
324static inline int pcf_doAddress(struct i2c_algo_pcf_data *adap,
325 struct i2c_msg *msg, int retries)
326{
327 unsigned short flags = msg->flags;
328 unsigned char addr;
329 int ret;
330 if ( (flags & I2C_M_TEN) ) {
331 /* a ten bit address */
332 addr = 0xf0 | (( msg->addr >> 7) & 0x03);
333 DEB2(printk(KERN_DEBUG "addr0: %d\n",addr));
334 /* try extended address code...*/
335 ret = try_address(adap, addr, retries);
336 if (ret!=1) {
337 printk(KERN_ERR "died at extended address code.\n");
338 return -EREMOTEIO;
339 }
340 /* the remaining 8 bit address */
341 i2c_outb(adap,msg->addr & 0x7f);
342/* Status check comes here */
343 if (ret != 1) {
344 printk(KERN_ERR "died at 2nd address code.\n");
345 return -EREMOTEIO;
346 }
347 if ( flags & I2C_M_RD ) {
348 i2c_repstart(adap);
349 /* okay, now switch into reading mode */
350 addr |= 0x01;
351 ret = try_address(adap, addr, retries);
352 if (ret!=1) {
353 printk(KERN_ERR "died at extended address code.\n");
354 return -EREMOTEIO;
355 }
356 }
357 } else { /* normal 7bit address */
358 addr = ( msg->addr << 1 );
359 if (flags & I2C_M_RD )
360 addr |= 1;
361 if (flags & I2C_M_REV_DIR_ADDR )
362 addr ^= 1;
363 i2c_outb(adap, addr);
364 }
365 return 0;
366}
367
368static int pcf_xfer(struct i2c_adapter *i2c_adap,
369 struct i2c_msg *msgs,
370 int num)
371{
372 struct i2c_algo_pcf_data *adap = i2c_adap->algo_data;
373 struct i2c_msg *pmsg;
374 int i;
375 int ret=0, timeout, status;
376
377
378 /* Check for bus busy */
379 timeout = wait_for_bb(adap);
380 if (timeout) {
381 DEB2(printk(KERN_ERR "i2c-algo-pcf.o: "
382 "Timeout waiting for BB in pcf_xfer\n");)
383 return -EIO;
384 }
385
386 for (i = 0;ret >= 0 && i < num; i++) {
387 pmsg = &msgs[i];
388
389 DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: Doing %s %d bytes to 0x%02x - %d of %d messages\n",
390 pmsg->flags & I2C_M_RD ? "read" : "write",
391 pmsg->len, pmsg->addr, i + 1, num);)
392
393 ret = pcf_doAddress(adap, pmsg, i2c_adap->retries);
394
395 /* Send START */
396 if (i == 0) {
397 i2c_start(adap);
398 }
399
400 /* Wait for PIN (pending interrupt NOT) */
401 timeout = wait_for_pin(adap, &status);
402 if (timeout) {
403 if (timeout == -EINTR) {
404 /* arbitration lost */
405 return (-EINTR);
406 }
407 i2c_stop(adap);
408 DEB2(printk(KERN_ERR "i2c-algo-pcf.o: Timeout waiting "
409 "for PIN(1) in pcf_xfer\n");)
410 return (-EREMOTEIO);
411 }
412
413#ifndef STUB_I2C
414 /* Check LRB (last rcvd bit - slave ack) */
415 if (status & I2C_PCF_LRB) {
416 i2c_stop(adap);
417 DEB2(printk(KERN_ERR "i2c-algo-pcf.o: No LRB(1) in pcf_xfer\n");)
418 return (-EREMOTEIO);
419 }
420#endif
421
422 DEB3(printk(KERN_DEBUG "i2c-algo-pcf.o: Msg %d, addr=0x%x, flags=0x%x, len=%d\n",
423 i, msgs[i].addr, msgs[i].flags, msgs[i].len);)
424
425 /* Read */
426 if (pmsg->flags & I2C_M_RD) {
427 /* read bytes into buffer*/
428 ret = pcf_readbytes(i2c_adap, pmsg->buf, pmsg->len,
429 (i + 1 == num));
430
431 if (ret != pmsg->len) {
432 DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: fail: "
433 "only read %d bytes.\n",ret));
434 } else {
435 DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: read %d bytes.\n",ret));
436 }
437 } else { /* Write */
438 ret = pcf_sendbytes(i2c_adap, pmsg->buf, pmsg->len,
439 (i + 1 == num));
440
441 if (ret != pmsg->len) {
442 DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: fail: "
443 "only wrote %d bytes.\n",ret));
444 } else {
445 DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: wrote %d bytes.\n",ret));
446 }
447 }
448 }
449
450 return (i);
451}
452
453static u32 pcf_func(struct i2c_adapter *adap)
454{
455 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
456 I2C_FUNC_10BIT_ADDR | I2C_FUNC_PROTOCOL_MANGLING;
457}
458
459/* -----exported algorithm data: ------------------------------------- */
460
461static struct i2c_algorithm pcf_algo = {
462 .name = "PCF8584 algorithm",
463 .id = I2C_ALGO_PCF,
464 .master_xfer = pcf_xfer,
465 .functionality = pcf_func,
466};
467
468/*
469 * registering functions to load algorithms at runtime
470 */
471int i2c_pcf_add_bus(struct i2c_adapter *adap)
472{
473 struct i2c_algo_pcf_data *pcf_adap = adap->algo_data;
474 int rval;
475
476 DEB2(dev_dbg(&adap->dev, "hw routines registered.\n"));
477
478 /* register new adapter to i2c module... */
479
480 adap->id |= pcf_algo.id;
481 adap->algo = &pcf_algo;
482
483 adap->timeout = 100; /* default values, should */
484 adap->retries = 3; /* be replaced by defines */
485
486 rval = pcf_init_8584(pcf_adap);
487 if (!rval)
488 i2c_add_adapter(adap);
489 return rval;
490}
491
492
493int i2c_pcf_del_bus(struct i2c_adapter *adap)
494{
495 return i2c_del_adapter(adap);
496}
497
498EXPORT_SYMBOL(i2c_pcf_add_bus);
499EXPORT_SYMBOL(i2c_pcf_del_bus);
500
501MODULE_AUTHOR("Hans Berglund <hb@spacetec.no>");
502MODULE_DESCRIPTION("I2C-Bus PCF8584 algorithm");
503MODULE_LICENSE("GPL");
504
505module_param(i2c_debug, int, S_IRUGO | S_IWUSR);
506MODULE_PARM_DESC(i2c_debug,
507 "debug level - 0 off; 1 normal; 2,3 more verbose; 9 pcf-protocol");
diff --git a/drivers/i2c/algos/i2c-algo-pcf.h b/drivers/i2c/algos/i2c-algo-pcf.h
new file mode 100644
index 000000000000..5263a9eeb8d7
--- /dev/null
+++ b/drivers/i2c/algos/i2c-algo-pcf.h
@@ -0,0 +1,76 @@
1/* -------------------------------------------------------------------- */
2/* i2c-pcf8584.h: PCF 8584 global defines */
3/* -------------------------------------------------------------------- */
4/* Copyright (C) 1996 Simon G. Vogl
5 1999 Hans Berglund
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/* With some changes from Frodo Looijaard <frodol@dds.nl> */
23
24#ifndef I2C_PCF8584_H
25#define I2C_PCF8584_H 1
26
27/* ----- Control register bits ---------------------------------------- */
28#define I2C_PCF_PIN 0x80
29#define I2C_PCF_ESO 0x40
30#define I2C_PCF_ES1 0x20
31#define I2C_PCF_ES2 0x10
32#define I2C_PCF_ENI 0x08
33#define I2C_PCF_STA 0x04
34#define I2C_PCF_STO 0x02
35#define I2C_PCF_ACK 0x01
36
37#define I2C_PCF_START (I2C_PCF_PIN | I2C_PCF_ESO | I2C_PCF_STA | I2C_PCF_ACK)
38#define I2C_PCF_STOP (I2C_PCF_PIN | I2C_PCF_ESO | I2C_PCF_STO | I2C_PCF_ACK)
39#define I2C_PCF_REPSTART ( I2C_PCF_ESO | I2C_PCF_STA | I2C_PCF_ACK)
40#define I2C_PCF_IDLE (I2C_PCF_PIN | I2C_PCF_ESO | I2C_PCF_ACK)
41
42/* ----- Status register bits ----------------------------------------- */
43/*#define I2C_PCF_PIN 0x80 as above*/
44
45#define I2C_PCF_INI 0x40 /* 1 if not initialized */
46#define I2C_PCF_STS 0x20
47#define I2C_PCF_BER 0x10
48#define I2C_PCF_AD0 0x08
49#define I2C_PCF_LRB 0x08
50#define I2C_PCF_AAS 0x04
51#define I2C_PCF_LAB 0x02
52#define I2C_PCF_BB 0x01
53
54/* ----- Chip clock frequencies --------------------------------------- */
55#define I2C_PCF_CLK3 0x00
56#define I2C_PCF_CLK443 0x10
57#define I2C_PCF_CLK6 0x14
58#define I2C_PCF_CLK 0x18
59#define I2C_PCF_CLK12 0x1c
60
61/* ----- transmission frequencies ------------------------------------- */
62#define I2C_PCF_TRNS90 0x00 /* 90 kHz */
63#define I2C_PCF_TRNS45 0x01 /* 45 kHz */
64#define I2C_PCF_TRNS11 0x02 /* 11 kHz */
65#define I2C_PCF_TRNS15 0x03 /* 1.5 kHz */
66
67
68/* ----- Access to internal registers according to ES1,ES2 ------------ */
69/* they are mapped to the data port ( a0 = 0 ) */
70/* available when ESO == 0 : */
71
72#define I2C_PCF_OWNADR 0
73#define I2C_PCF_INTREG I2C_PCF_ES2
74#define I2C_PCF_CLKREG I2C_PCF_ES1
75
76#endif /* I2C_PCF8584_H */
diff --git a/drivers/i2c/algos/i2c-algo-sgi.c b/drivers/i2c/algos/i2c-algo-sgi.c
new file mode 100644
index 000000000000..422721b241e5
--- /dev/null
+++ b/drivers/i2c/algos/i2c-algo-sgi.c
@@ -0,0 +1,189 @@
1/*
2 * i2c-algo-sgi.c: i2c driver algorithms for SGI adapters.
3 *
4 * This file is subject to the terms and conditions of the GNU General Public
5 * License version 2 as published by the Free Software Foundation.
6 *
7 * Copyright (C) 2003 Ladislav Michl <ladis@linux-mips.org>
8 */
9
10#include <linux/kernel.h>
11#include <linux/module.h>
12#include <linux/init.h>
13#include <linux/errno.h>
14#include <linux/delay.h>
15
16#include <linux/i2c.h>
17#include <linux/i2c-algo-sgi.h>
18
19
20#define SGI_I2C_FORCE_IDLE (0 << 0)
21#define SGI_I2C_NOT_IDLE (1 << 0)
22#define SGI_I2C_WRITE (0 << 1)
23#define SGI_I2C_READ (1 << 1)
24#define SGI_I2C_RELEASE_BUS (0 << 2)
25#define SGI_I2C_HOLD_BUS (1 << 2)
26#define SGI_I2C_XFER_DONE (0 << 4)
27#define SGI_I2C_XFER_BUSY (1 << 4)
28#define SGI_I2C_ACK (0 << 5)
29#define SGI_I2C_NACK (1 << 5)
30#define SGI_I2C_BUS_OK (0 << 7)
31#define SGI_I2C_BUS_ERR (1 << 7)
32
33#define get_control() adap->getctrl(adap->data)
34#define set_control(val) adap->setctrl(adap->data, val)
35#define read_data() adap->rdata(adap->data)
36#define write_data(val) adap->wdata(adap->data, val)
37
38
39static int wait_xfer_done(struct i2c_algo_sgi_data *adap)
40{
41 int i;
42
43 for (i = 0; i < adap->xfer_timeout; i++) {
44 if ((get_control() & SGI_I2C_XFER_BUSY) == 0)
45 return 0;
46 udelay(1);
47 }
48
49 return -ETIMEDOUT;
50}
51
52static int wait_ack(struct i2c_algo_sgi_data *adap)
53{
54 int i;
55
56 if (wait_xfer_done(adap))
57 return -ETIMEDOUT;
58 for (i = 0; i < adap->ack_timeout; i++) {
59 if ((get_control() & SGI_I2C_NACK) == 0)
60 return 0;
61 udelay(1);
62 }
63
64 return -ETIMEDOUT;
65}
66
67static int force_idle(struct i2c_algo_sgi_data *adap)
68{
69 int i;
70
71 set_control(SGI_I2C_FORCE_IDLE);
72 for (i = 0; i < adap->xfer_timeout; i++) {
73 if ((get_control() & SGI_I2C_NOT_IDLE) == 0)
74 goto out;
75 udelay(1);
76 }
77 return -ETIMEDOUT;
78out:
79 if (get_control() & SGI_I2C_BUS_ERR)
80 return -EIO;
81 return 0;
82}
83
84static int do_address(struct i2c_algo_sgi_data *adap, unsigned int addr,
85 int rd)
86{
87 if (rd)
88 set_control(SGI_I2C_NOT_IDLE);
89 /* Check if bus is idle, eventually force it to do so */
90 if (get_control() & SGI_I2C_NOT_IDLE)
91 if (force_idle(adap))
92 return -EIO;
93 /* Write out the i2c chip address and specify operation */
94 set_control(SGI_I2C_HOLD_BUS | SGI_I2C_WRITE | SGI_I2C_NOT_IDLE);
95 if (rd)
96 addr |= 1;
97 write_data(addr);
98 if (wait_ack(adap))
99 return -EIO;
100 return 0;
101}
102
103static int i2c_read(struct i2c_algo_sgi_data *adap, unsigned char *buf,
104 unsigned int len)
105{
106 int i;
107
108 set_control(SGI_I2C_HOLD_BUS | SGI_I2C_READ | SGI_I2C_NOT_IDLE);
109 for (i = 0; i < len; i++) {
110 if (wait_xfer_done(adap))
111 return -EIO;
112 buf[i] = read_data();
113 }
114 set_control(SGI_I2C_RELEASE_BUS | SGI_I2C_FORCE_IDLE);
115
116 return 0;
117
118}
119
120static int i2c_write(struct i2c_algo_sgi_data *adap, unsigned char *buf,
121 unsigned int len)
122{
123 int i;
124
125 /* We are already in write state */
126 for (i = 0; i < len; i++) {
127 write_data(buf[i]);
128 if (wait_ack(adap))
129 return -EIO;
130 }
131 return 0;
132}
133
134static int sgi_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs,
135 int num)
136{
137 struct i2c_algo_sgi_data *adap = i2c_adap->algo_data;
138 struct i2c_msg *p;
139 int i, err = 0;
140
141 for (i = 0; !err && i < num; i++) {
142 p = &msgs[i];
143 err = do_address(adap, p->addr, p->flags & I2C_M_RD);
144 if (err || !p->len)
145 continue;
146 if (p->flags & I2C_M_RD)
147 err = i2c_read(adap, p->buf, p->len);
148 else
149 err = i2c_write(adap, p->buf, p->len);
150 }
151
152 return err;
153}
154
155static u32 sgi_func(struct i2c_adapter *adap)
156{
157 return I2C_FUNC_SMBUS_EMUL;
158}
159
160static struct i2c_algorithm sgi_algo = {
161 .name = "SGI algorithm",
162 .id = I2C_ALGO_SGI,
163 .master_xfer = sgi_xfer,
164 .functionality = sgi_func,
165};
166
167/*
168 * registering functions to load algorithms at runtime
169 */
170int i2c_sgi_add_bus(struct i2c_adapter *adap)
171{
172 adap->id |= sgi_algo.id;
173 adap->algo = &sgi_algo;
174
175 return i2c_add_adapter(adap);
176}
177
178
179int i2c_sgi_del_bus(struct i2c_adapter *adap)
180{
181 return i2c_del_adapter(adap);
182}
183
184EXPORT_SYMBOL(i2c_sgi_add_bus);
185EXPORT_SYMBOL(i2c_sgi_del_bus);
186
187MODULE_AUTHOR("Ladislav Michl <ladis@linux-mips.org>");
188MODULE_DESCRIPTION("I2C-Bus SGI algorithm");
189MODULE_LICENSE("GPL");
diff --git a/drivers/i2c/algos/i2c-algo-sibyte.c b/drivers/i2c/algos/i2c-algo-sibyte.c
new file mode 100644
index 000000000000..35789bb7126a
--- /dev/null
+++ b/drivers/i2c/algos/i2c-algo-sibyte.c
@@ -0,0 +1,222 @@
1/* ------------------------------------------------------------------------- */
2/* i2c-algo-sibyte.c i2c driver algorithms for bit-shift adapters */
3/* ------------------------------------------------------------------------- */
4/* Copyright (C) 2001,2002,2003 Broadcom Corporation
5 Copyright (C) 1995-2000 Simon G. Vogl
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/* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi> and even
23 Frodo Looijaard <frodol@dds.nl>. */
24
25/* Ported for SiByte SOCs by Broadcom Corporation. */
26
27#include <linux/config.h>
28#include <linux/kernel.h>
29#include <linux/module.h>
30#include <linux/init.h>
31
32#include <asm/io.h>
33#include <asm/sibyte/sb1250_regs.h>
34#include <asm/sibyte/sb1250_smbus.h>
35
36#include <linux/i2c.h>
37#include <linux/i2c-algo-sibyte.h>
38
39/* ----- global defines ----------------------------------------------- */
40#define SMB_CSR(a,r) ((long)(a->reg_base + r))
41
42/* ----- global variables --------------------------------------------- */
43
44/* module parameters:
45 */
46static int bit_scan=0; /* have a look at what's hanging 'round */
47
48
49static int smbus_xfer(struct i2c_adapter *i2c_adap, u16 addr,
50 unsigned short flags, char read_write,
51 u8 command, int size, union i2c_smbus_data * data)
52{
53 struct i2c_algo_sibyte_data *adap = i2c_adap->algo_data;
54 int data_bytes = 0;
55 int error;
56
57 while (csr_in32(SMB_CSR(adap, R_SMB_STATUS)) & M_SMB_BUSY)
58 ;
59
60 switch (size) {
61 case I2C_SMBUS_QUICK:
62 csr_out32((V_SMB_ADDR(addr) | (read_write == I2C_SMBUS_READ ? M_SMB_QDATA : 0) |
63 V_SMB_TT_QUICKCMD), SMB_CSR(adap, R_SMB_START));
64 break;
65 case I2C_SMBUS_BYTE:
66 if (read_write == I2C_SMBUS_READ) {
67 csr_out32((V_SMB_ADDR(addr) | V_SMB_TT_RD1BYTE),
68 SMB_CSR(adap, R_SMB_START));
69 data_bytes = 1;
70 } else {
71 csr_out32(V_SMB_CMD(command), SMB_CSR(adap, R_SMB_CMD));
72 csr_out32((V_SMB_ADDR(addr) | V_SMB_TT_WR1BYTE),
73 SMB_CSR(adap, R_SMB_START));
74 }
75 break;
76 case I2C_SMBUS_BYTE_DATA:
77 csr_out32(V_SMB_CMD(command), SMB_CSR(adap, R_SMB_CMD));
78 if (read_write == I2C_SMBUS_READ) {
79 csr_out32((V_SMB_ADDR(addr) | V_SMB_TT_CMD_RD1BYTE),
80 SMB_CSR(adap, R_SMB_START));
81 data_bytes = 1;
82 } else {
83 csr_out32(V_SMB_LB(data->byte), SMB_CSR(adap, R_SMB_DATA));
84 csr_out32((V_SMB_ADDR(addr) | V_SMB_TT_WR2BYTE),
85 SMB_CSR(adap, R_SMB_START));
86 }
87 break;
88 case I2C_SMBUS_WORD_DATA:
89 csr_out32(V_SMB_CMD(command), SMB_CSR(adap, R_SMB_CMD));
90 if (read_write == I2C_SMBUS_READ) {
91 csr_out32((V_SMB_ADDR(addr) | V_SMB_TT_CMD_RD2BYTE),
92 SMB_CSR(adap, R_SMB_START));
93 data_bytes = 2;
94 } else {
95 csr_out32(V_SMB_LB(data->word & 0xff), SMB_CSR(adap, R_SMB_DATA));
96 csr_out32(V_SMB_MB(data->word >> 8), SMB_CSR(adap, R_SMB_DATA));
97 csr_out32((V_SMB_ADDR(addr) | V_SMB_TT_WR2BYTE),
98 SMB_CSR(adap, R_SMB_START));
99 }
100 break;
101 default:
102 return -1; /* XXXKW better error code? */
103 }
104
105 while (csr_in32(SMB_CSR(adap, R_SMB_STATUS)) & M_SMB_BUSY)
106 ;
107
108 error = csr_in32(SMB_CSR(adap, R_SMB_STATUS));
109 if (error & M_SMB_ERROR) {
110 /* Clear error bit by writing a 1 */
111 csr_out32(M_SMB_ERROR, SMB_CSR(adap, R_SMB_STATUS));
112 return -1; /* XXXKW better error code? */
113 }
114
115 if (data_bytes == 1)
116 data->byte = csr_in32(SMB_CSR(adap, R_SMB_DATA)) & 0xff;
117 if (data_bytes == 2)
118 data->word = csr_in32(SMB_CSR(adap, R_SMB_DATA)) & 0xffff;
119
120 return 0;
121}
122
123static int algo_control(struct i2c_adapter *adapter,
124 unsigned int cmd, unsigned long arg)
125{
126 return 0;
127}
128
129static u32 bit_func(struct i2c_adapter *adap)
130{
131 return (I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
132 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA);
133}
134
135
136/* -----exported algorithm data: ------------------------------------- */
137
138static struct i2c_algorithm i2c_sibyte_algo = {
139 .name = "SiByte algorithm",
140 .id = I2C_ALGO_SIBYTE,
141 .smbus_xfer = smbus_xfer,
142 .algo_control = algo_control, /* ioctl */
143 .functionality = bit_func,
144};
145
146/*
147 * registering functions to load algorithms at runtime
148 */
149int i2c_sibyte_add_bus(struct i2c_adapter *i2c_adap, int speed)
150{
151 int i;
152 struct i2c_algo_sibyte_data *adap = i2c_adap->algo_data;
153
154 /* register new adapter to i2c module... */
155
156 i2c_adap->id |= i2c_sibyte_algo.id;
157 i2c_adap->algo = &i2c_sibyte_algo;
158
159 /* Set the frequency to 100 kHz */
160 csr_out32(speed, SMB_CSR(adap,R_SMB_FREQ));
161 csr_out32(0, SMB_CSR(adap,R_SMB_CONTROL));
162
163 /* scan bus */
164 if (bit_scan) {
165 union i2c_smbus_data data;
166 int rc;
167 printk(KERN_INFO " i2c-algo-sibyte.o: scanning bus %s.\n",
168 i2c_adap->name);
169 for (i = 0x00; i < 0x7f; i++) {
170 /* XXXKW is this a realistic probe? */
171 rc = smbus_xfer(i2c_adap, i, 0, I2C_SMBUS_READ, 0,
172 I2C_SMBUS_BYTE_DATA, &data);
173 if (!rc) {
174 printk("(%02x)",i);
175 } else
176 printk(".");
177 }
178 printk("\n");
179 }
180
181 i2c_add_adapter(i2c_adap);
182
183 return 0;
184}
185
186
187int i2c_sibyte_del_bus(struct i2c_adapter *adap)
188{
189 int res;
190
191 if ((res = i2c_del_adapter(adap)) < 0)
192 return res;
193
194 return 0;
195}
196
197int __init i2c_algo_sibyte_init (void)
198{
199 printk("i2c-algo-sibyte.o: i2c SiByte algorithm module\n");
200 return 0;
201}
202
203
204EXPORT_SYMBOL(i2c_sibyte_add_bus);
205EXPORT_SYMBOL(i2c_sibyte_del_bus);
206
207#ifdef MODULE
208MODULE_AUTHOR("Kip Walker, Broadcom Corp.");
209MODULE_DESCRIPTION("SiByte I2C-Bus algorithm");
210MODULE_PARM(bit_scan, "i");
211MODULE_PARM_DESC(bit_scan, "Scan for active chips on the bus");
212MODULE_LICENSE("GPL");
213
214int init_module(void)
215{
216 return i2c_algo_sibyte_init();
217}
218
219void cleanup_module(void)
220{
221}
222#endif
diff --git a/drivers/i2c/busses/Kconfig b/drivers/i2c/busses/Kconfig
new file mode 100644
index 000000000000..edf8051da3b4
--- /dev/null
+++ b/drivers/i2c/busses/Kconfig
@@ -0,0 +1,499 @@
1#
2# Sensor device configuration
3#
4
5menu "I2C Hardware Bus support"
6 depends on I2C
7
8config I2C_ALI1535
9 tristate "ALI 1535"
10 depends on I2C && PCI && EXPERIMENTAL
11 help
12 If you say yes to this option, support will be included for the SMB
13 Host controller on Acer Labs Inc. (ALI) M1535 South Bridges. The SMB
14 controller is part of the 7101 device, which is an ACPI-compliant
15 Power Management Unit (PMU).
16
17 This driver can also be built as a module. If so, the module
18 will be called i2c-ali1535.
19
20config I2C_ALI1563
21 tristate "ALI 1563"
22 depends on I2C && PCI && EXPERIMENTAL
23 help
24 If you say yes to this option, support will be included for the SMB
25 Host controller on Acer Labs Inc. (ALI) M1563 South Bridges. The SMB
26 controller is part of the 7101 device, which is an ACPI-compliant
27 Power Management Unit (PMU).
28
29 This driver can also be built as a module. If so, the module
30 will be called i2c-ali1563.
31
32config I2C_ALI15X3
33 tristate "ALI 15x3"
34 depends on I2C && PCI && EXPERIMENTAL
35 help
36 If you say yes to this option, support will be included for the
37 Acer Labs Inc. (ALI) M1514 and M1543 motherboard I2C interfaces.
38
39 This driver can also be built as a module. If so, the module
40 will be called i2c-ali15x3.
41
42config I2C_AMD756
43 tristate "AMD 756/766/768/8111 and nVidia nForce"
44 depends on I2C && PCI && EXPERIMENTAL
45 help
46 If you say yes to this option, support will be included for the AMD
47 756/766/768 mainboard I2C interfaces. The driver also includes
48 support for the first (SMBus 1.0) I2C interface of the AMD 8111 and
49 the nVidia nForce I2C interface.
50
51 This driver can also be built as a module. If so, the module
52 will be called i2c-amd756.
53
54config I2C_AMD756_S4882
55 tristate "SMBus multiplexing on the Tyan S4882"
56 depends on I2C_AMD756 && EXPERIMENTAL
57 help
58 Enabling this option will add specific SMBus support for the Tyan
59 S4882 motherboard. On this 4-CPU board, the SMBus is multiplexed
60 over 8 different channels, where the various memory module EEPROMs
61 and temperature sensors live. Saying yes here will give you access
62 to these in addition to the trunk.
63
64 This driver can also be built as a module. If so, the module
65 will be called i2c-amd756-s4882.
66
67config I2C_AMD8111
68 tristate "AMD 8111"
69 depends on I2C && PCI && EXPERIMENTAL
70 help
71 If you say yes to this option, support will be included for the
72 second (SMBus 2.0) AMD 8111 mainboard I2C interface.
73
74 This driver can also be built as a module. If so, the module
75 will be called i2c-amd8111.
76
77config I2C_AU1550
78 tristate "Au1550 SMBus interface"
79 depends on I2C && SOC_AU1550
80 help
81 If you say yes to this option, support will be included for the
82 Au1550 SMBus interface.
83
84 This driver can also be built as a module. If so, the module
85 will be called i2c-au1550.
86
87config I2C_ELEKTOR
88 tristate "Elektor ISA card"
89 depends on I2C && ISA && BROKEN_ON_SMP
90 select I2C_ALGOPCF
91 help
92 This supports the PCF8584 ISA bus I2C adapter. Say Y if you own
93 such an adapter.
94
95 This support is also available as a module. If so, the module
96 will be called i2c-elektor.
97
98config I2C_HYDRA
99 tristate "CHRP Apple Hydra Mac I/O I2C interface"
100 depends on I2C && PCI && PPC_CHRP && EXPERIMENTAL
101 select I2C_ALGOBIT
102 help
103 This supports the use of the I2C interface in the Apple Hydra Mac
104 I/O chip on some CHRP machines (e.g. the LongTrail). Say Y if you
105 have such a machine.
106
107 This support is also available as a module. If so, the module
108 will be called i2c-hydra.
109
110config I2C_I801
111 tristate "Intel 82801 (ICH)"
112 depends on I2C && PCI && EXPERIMENTAL
113 help
114 If you say yes to this option, support will be included for the Intel
115 801 family of mainboard I2C interfaces. Specifically, the following
116 versions of the chipset are supported:
117 82801AA
118 82801AB
119 82801BA
120 82801CA/CAM
121 82801DB
122 82801EB/ER (ICH5/ICH5R)
123 6300ESB
124 ICH6
125 ICH7
126
127 This driver can also be built as a module. If so, the module
128 will be called i2c-i801.
129
130config I2C_I810
131 tristate "Intel 810/815"
132 depends on I2C && PCI && EXPERIMENTAL
133 select I2C_ALGOBIT
134 help
135 If you say yes to this option, support will be included for the Intel
136 810/815 family of mainboard I2C interfaces. Specifically, the
137 following versions of the chipset is supported:
138 i810AA
139 i810AB
140 i810E
141 i815
142
143 This driver can also be built as a module. If so, the module
144 will be called i2c-i810.
145
146config I2C_PIIX4
147 tristate "Intel PIIX4"
148 depends on I2C && PCI
149 help
150 If you say yes to this option, support will be included for the Intel
151 PIIX4 family of mainboard I2C interfaces. Specifically, the following
152 versions of the chipset are supported:
153 Intel PIIX4
154 Intel 440MX
155 Serverworks OSB4
156 Serverworks CSB5
157 Serverworks CSB6
158 SMSC Victory66
159
160 This driver can also be built as a module. If so, the module
161 will be called i2c-piix4.
162
163config I2C_IBM_IIC
164 tristate "IBM PPC 4xx on-chip I2C interface"
165 depends on IBM_OCP && I2C
166 help
167 Say Y here if you want to use IIC peripheral found on
168 embedded IBM PPC 4xx based systems.
169
170 This driver can also be built as a module. If so, the module
171 will be called i2c-ibm_iic.
172
173config I2C_IOP3XX
174 tristate "Intel IOP3xx and IXP4xx on-chip I2C interface"
175 depends on (ARCH_IOP3XX || ARCH_IXP4XX) && I2C
176 help
177 Say Y here if you want to use the IIC bus controller on
178 the Intel IOP3xx I/O Processors or IXP4xx Network Processors.
179
180 This driver can also be built as a module. If so, the module
181 will be called i2c-iop3xx.
182
183config I2C_ISA
184 tristate "ISA Bus support"
185 depends on I2C && EXPERIMENTAL
186 help
187 If you say yes to this option, support will be included for i2c
188 interfaces that are on the ISA bus.
189
190 This driver can also be built as a module. If so, the module
191 will be called i2c-isa.
192
193config I2C_ITE
194 tristate "ITE I2C Adapter"
195 depends on I2C && MIPS_ITE8172
196 select I2C_ALGOITE
197 help
198 This supports the ITE8172 I2C peripheral found on some MIPS
199 systems. Say Y if you have one of these. You should also say Y for
200 the ITE I2C driver algorithm support above.
201
202 This support is also available as a module. If so, the module
203 will be called i2c-ite.
204
205config I2C_IXP4XX
206 tristate "IXP4xx GPIO-Based I2C Interface"
207 depends on I2C && ARCH_IXP4XX
208 select I2C_ALGOBIT
209 help
210 Say Y here if you have an Intel IXP4xx(420,421,422,425) based
211 system and are using GPIO lines for an I2C bus.
212
213 This support is also available as a module. If so, the module
214 will be called i2c-ixp4xx.
215
216config I2C_IXP2000
217 tristate "IXP2000 GPIO-Based I2C Interface"
218 depends on I2C && ARCH_IXP2000
219 select I2C_ALGOBIT
220 help
221 Say Y here if you have an Intel IXP2000(2400, 2800, 2850) based
222 system and are using GPIO lines for an I2C bus.
223
224 This support is also available as a module. If so, the module
225 will be called i2c-ixp2000.
226
227config I2C_KEYWEST
228 tristate "Powermac Keywest I2C interface"
229 depends on I2C && PPC_PMAC
230 help
231 This supports the use of the I2C interface in the combo-I/O
232 chip on recent Apple machines. Say Y if you have such a machine.
233
234 This support is also available as a module. If so, the module
235 will be called i2c-keywest.
236
237config I2C_MPC
238 tristate "MPC107/824x/85xx/52xx"
239 depends on I2C && PPC
240 help
241 If you say yes to this option, support will be included for the
242 built-in I2C interface on the MPC107/Tsi107/MPC8240/MPC8245 and
243 MPC85xx family processors. The driver may also work on 52xx
244 family processors, though interrupts are known not to work.
245
246 This driver can also be built as a module. If so, the module
247 will be called i2c-mpc.
248
249config I2C_NFORCE2
250 tristate "Nvidia Nforce2"
251 depends on I2C && PCI && EXPERIMENTAL
252 help
253 If you say yes to this option, support will be included for the Nvidia
254 Nforce2 family of mainboard I2C interfaces.
255 This driver also supports the nForce3 Pro 150 MCP.
256
257 This driver can also be built as a module. If so, the module
258 will be called i2c-nforce2.
259
260config I2C_PARPORT
261 tristate "Parallel port adapter"
262 depends on I2C && PARPORT
263 select I2C_ALGOBIT
264 help
265 This supports parallel port I2C adapters such as the ones made by
266 Philips or Velleman, Analog Devices evaluation boards, and more.
267 Basically any adapter using the parallel port as an I2C bus with
268 no extra chipset is supported by this driver, or could be.
269
270 This driver is a replacement for (and was inspired by) an older
271 driver named i2c-philips-par. The new driver supports more devices,
272 and makes it easier to add support for new devices.
273
274 Another driver exists, named i2c-parport-light, which doesn't depend
275 on the parport driver. This is meant for embedded systems. Don't say
276 Y here if you intend to say Y or M there.
277
278 This support is also available as a module. If so, the module
279 will be called i2c-parport.
280
281config I2C_PARPORT_LIGHT
282 tristate "Parallel port adapter (light)"
283 depends on I2C
284 select I2C_ALGOBIT
285 help
286 This supports parallel port I2C adapters such as the ones made by
287 Philips or Velleman, Analog Devices evaluation boards, and more.
288 Basically any adapter using the parallel port as an I2C bus with
289 no extra chipset is supported by this driver, or could be.
290
291 This driver is a light version of i2c-parport. It doesn't depend
292 on the parport driver, and uses direct I/O access instead. This
293 might be prefered on embedded systems where wasting memory for
294 the clean but heavy parport handling is not an option. The
295 drawback is a reduced portability and the impossibility to
296 dasiy-chain other parallel port devices.
297
298 Don't say Y here if you said Y or M to i2c-parport. Saying M to
299 both is possible but both modules should not be loaded at the same
300 time.
301
302 This support is also available as a module. If so, the module
303 will be called i2c-parport-light.
304
305config I2C_PROSAVAGE
306 tristate "S3/VIA (Pro)Savage"
307 depends on I2C && PCI && EXPERIMENTAL
308 select I2C_ALGOBIT
309 help
310 If you say yes to this option, support will be included for the
311 I2C bus and DDC bus of the S3VIA embedded Savage4 and ProSavage8
312 graphics processors.
313 chipsets supported:
314 S3/VIA KM266/VT8375 aka ProSavage8
315 S3/VIA KM133/VT8365 aka Savage4
316
317 This support is also available as a module. If so, the module
318 will be called i2c-prosavage.
319
320config I2C_RPXLITE
321 tristate "Embedded Planet RPX Lite/Classic support"
322 depends on (RPXLITE || RPXCLASSIC) && I2C
323 select I2C_ALGO8XX
324
325config I2C_S3C2410
326 tristate "S3C2410 I2C Driver"
327 depends on I2C && ARCH_S3C2410
328 help
329 Say Y here to include support for I2C controller in the
330 Samsung S3C2410 based System-on-Chip devices.
331
332config I2C_SAVAGE4
333 tristate "S3 Savage 4"
334 depends on I2C && PCI && EXPERIMENTAL
335 select I2C_ALGOBIT
336 help
337 If you say yes to this option, support will be included for the
338 S3 Savage 4 I2C interface.
339
340 This driver can also be built as a module. If so, the module
341 will be called i2c-savage4.
342
343config I2C_SIBYTE
344 tristate "SiByte SMBus interface"
345 depends on SIBYTE_SB1xxx_SOC && I2C
346 help
347 Supports the SiByte SOC on-chip I2C interfaces (2 channels).
348
349config SCx200_I2C
350 tristate "NatSemi SCx200 I2C using GPIO pins"
351 depends on SCx200_GPIO && I2C
352 select I2C_ALGOBIT
353 help
354 Enable the use of two GPIO pins of a SCx200 processor as an I2C bus.
355
356 If you don't know what to do here, say N.
357
358 This support is also available as a module. If so, the module
359 will be called scx200_i2c.
360
361config SCx200_I2C_SCL
362 int "GPIO pin used for SCL"
363 depends on SCx200_I2C
364 default "12"
365 help
366 Enter the GPIO pin number used for the SCL signal. This value can
367 also be specified with a module parameter.
368
369config SCx200_I2C_SDA
370 int "GPIO pin used for SDA"
371 depends on SCx200_I2C
372 default "13"
373 help
374 Enter the GPIO pin number used for the SSA signal. This value can
375 also be specified with a module parameter.
376
377config SCx200_ACB
378 tristate "NatSemi SCx200 ACCESS.bus"
379 depends on I2C && PCI
380 help
381 Enable the use of the ACCESS.bus controllers of a SCx200 processor.
382
383 If you don't know what to do here, say N.
384
385 This support is also available as a module. If so, the module
386 will be called scx200_acb.
387
388config I2C_SIS5595
389 tristate "SiS 5595"
390 depends on I2C && PCI && EXPERIMENTAL
391 help
392 If you say yes to this option, support will be included for the
393 SiS5595 SMBus (a subset of I2C) interface.
394
395 This driver can also be built as a module. If so, the module
396 will be called i2c-sis5595.
397
398config I2C_SIS630
399 tristate "SiS 630/730"
400 depends on I2C && PCI && EXPERIMENTAL
401 help
402 If you say yes to this option, support will be included for the
403 SiS630 and SiS730 SMBus (a subset of I2C) interface.
404
405 This driver can also be built as a module. If so, the module
406 will be called i2c-sis630.
407
408config I2C_SIS96X
409 tristate "SiS 96x"
410 depends on I2C && PCI && EXPERIMENTAL
411 help
412 If you say yes to this option, support will be included for the SiS
413 96x SMBus (a subset of I2C) interfaces. Specifically, the following
414 chipsets are supported:
415 645/961
416 645DX/961
417 645DX/962
418 648/961
419 650/961
420 735
421
422 This driver can also be built as a module. If so, the module
423 will be called i2c-sis96x.
424
425config I2C_STUB
426 tristate "I2C/SMBus Test Stub"
427 depends on I2C && EXPERIMENTAL && 'm'
428 default 'n'
429 help
430 This module may be useful to developers of SMBus client drivers,
431 especially for certain kinds of sensor chips.
432
433 If you do build this module, be sure to read the notes and warnings
434 in <file:Documentation/i2c/i2c-stub>.
435
436 If you don't know what to do here, definitely say N.
437
438config I2C_VIA
439 tristate "VIA 82C586B"
440 depends on I2C && PCI && EXPERIMENTAL
441 select I2C_ALGOBIT
442 help
443 If you say yes to this option, support will be included for the VIA
444 82C586B I2C interface
445
446 This driver can also be built as a module. If so, the module
447 will be called i2c-via.
448
449config I2C_VIAPRO
450 tristate "VIA 82C596/82C686/823x"
451 depends on I2C && PCI && EXPERIMENTAL
452 help
453 If you say yes to this option, support will be included for the VIA
454 82C596/82C686/823x I2C interfaces. Specifically, the following
455 chipsets are supported:
456 82C596A/B
457 82C686A/B
458 8231
459 8233
460 8233A
461 8235
462 8237
463
464 This driver can also be built as a module. If so, the module
465 will be called i2c-viapro.
466
467config I2C_VOODOO3
468 tristate "Voodoo 3"
469 depends on I2C && PCI && EXPERIMENTAL
470 select I2C_ALGOBIT
471 help
472 If you say yes to this option, support will be included for the
473 Voodoo 3 I2C interface.
474
475 This driver can also be built as a module. If so, the module
476 will be called i2c-voodoo3.
477
478config I2C_PCA_ISA
479 tristate "PCA9564 on an ISA bus"
480 depends on I2C
481 select I2C_ALGOPCA
482 help
483 This driver supports ISA boards using the Philips PCA 9564
484 Parallel bus to I2C bus controller
485
486 This driver can also be built as a module. If so, the module
487 will be called i2c-pca-isa.
488
489config I2C_MV64XXX
490 tristate "Marvell mv64xxx I2C Controller"
491 depends on I2C && MV64X60 && EXPERIMENTAL
492 help
493 If you say yes to this option, support will be included for the
494 built-in I2C interface on the Marvell 64xxx line of host bridges.
495
496 This driver can also be built as a module. If so, the module
497 will be called i2c-mv64xxx.
498
499endmenu
diff --git a/drivers/i2c/busses/Makefile b/drivers/i2c/busses/Makefile
new file mode 100644
index 000000000000..42d6d814da72
--- /dev/null
+++ b/drivers/i2c/busses/Makefile
@@ -0,0 +1,47 @@
1#
2# Makefile for the i2c bus drivers.
3#
4
5obj-$(CONFIG_I2C_ALI1535) += i2c-ali1535.o
6obj-$(CONFIG_I2C_ALI1563) += i2c-ali1563.o
7obj-$(CONFIG_I2C_ALI15X3) += i2c-ali15x3.o
8obj-$(CONFIG_I2C_AMD756) += i2c-amd756.o
9obj-$(CONFIG_I2C_AMD756_S4882) += i2c-amd756-s4882.o
10obj-$(CONFIG_I2C_AMD8111) += i2c-amd8111.o
11obj-$(CONFIG_I2C_AU1550) += i2c-au1550.o
12obj-$(CONFIG_I2C_ELEKTOR) += i2c-elektor.o
13obj-$(CONFIG_I2C_HYDRA) += i2c-hydra.o
14obj-$(CONFIG_I2C_I801) += i2c-i801.o
15obj-$(CONFIG_I2C_I810) += i2c-i810.o
16obj-$(CONFIG_I2C_IBM_IIC) += i2c-ibm_iic.o
17obj-$(CONFIG_I2C_IOP3XX) += i2c-iop3xx.o
18obj-$(CONFIG_I2C_ISA) += i2c-isa.o
19obj-$(CONFIG_I2C_ITE) += i2c-ite.o
20obj-$(CONFIG_I2C_IXP2000) += i2c-ixp2000.o
21obj-$(CONFIG_I2C_IXP4XX) += i2c-ixp4xx.o
22obj-$(CONFIG_I2C_KEYWEST) += i2c-keywest.o
23obj-$(CONFIG_I2C_MPC) += i2c-mpc.o
24obj-$(CONFIG_I2C_MV64XXX) += i2c-mv64xxx.o
25obj-$(CONFIG_I2C_NFORCE2) += i2c-nforce2.o
26obj-$(CONFIG_I2C_PARPORT) += i2c-parport.o
27obj-$(CONFIG_I2C_PARPORT_LIGHT) += i2c-parport-light.o
28obj-$(CONFIG_I2C_PCA_ISA) += i2c-pca-isa.o
29obj-$(CONFIG_I2C_PIIX4) += i2c-piix4.o
30obj-$(CONFIG_I2C_PROSAVAGE) += i2c-prosavage.o
31obj-$(CONFIG_I2C_RPXLITE) += i2c-rpx.o
32obj-$(CONFIG_I2C_S3C2410) += i2c-s3c2410.o
33obj-$(CONFIG_I2C_SAVAGE4) += i2c-savage4.o
34obj-$(CONFIG_I2C_SIBYTE) += i2c-sibyte.o
35obj-$(CONFIG_I2C_SIS5595) += i2c-sis5595.o
36obj-$(CONFIG_I2C_SIS630) += i2c-sis630.o
37obj-$(CONFIG_I2C_SIS96X) += i2c-sis96x.o
38obj-$(CONFIG_I2C_STUB) += i2c-stub.o
39obj-$(CONFIG_I2C_VIA) += i2c-via.o
40obj-$(CONFIG_I2C_VIAPRO) += i2c-viapro.o
41obj-$(CONFIG_I2C_VOODOO3) += i2c-voodoo3.o
42obj-$(CONFIG_SCx200_ACB) += scx200_acb.o
43obj-$(CONFIG_SCx200_I2C) += scx200_i2c.o
44
45ifeq ($(CONFIG_I2C_DEBUG_BUS),y)
46EXTRA_CFLAGS += -DDEBUG
47endif
diff --git a/drivers/i2c/busses/i2c-ali1535.c b/drivers/i2c/busses/i2c-ali1535.c
new file mode 100644
index 000000000000..b00cd4098221
--- /dev/null
+++ b/drivers/i2c/busses/i2c-ali1535.c
@@ -0,0 +1,543 @@
1/*
2 i2c-ali1535.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring
4 Copyright (c) 2000 Frodo Looijaard <frodol@dds.nl>,
5 Philip Edelbrock <phil@netroedge.com>,
6 Mark D. Studebaker <mdsxyz123@yahoo.com>,
7 Dan Eaton <dan.eaton@rocketlogix.com> and
8 Stephen Rousset<stephen.rousset@rocketlogix.com>
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23*/
24
25/*
26 This is the driver for the SMB Host controller on
27 Acer Labs Inc. (ALI) M1535 South Bridge.
28
29 The M1535 is a South bridge for portable systems.
30 It is very similar to the M15x3 South bridges also produced
31 by Acer Labs Inc. Some of the registers within the part
32 have moved and some have been redefined slightly. Additionally,
33 the sequencing of the SMBus transactions has been modified
34 to be more consistent with the sequencing recommended by
35 the manufacturer and observed through testing. These
36 changes are reflected in this driver and can be identified
37 by comparing this driver to the i2c-ali15x3 driver.
38 For an overview of these chips see http://www.acerlabs.com
39
40 The SMB controller is part of the 7101 device, which is an
41 ACPI-compliant Power Management Unit (PMU).
42
43 The whole 7101 device has to be enabled for the SMB to work.
44 You can't just enable the SMB alone.
45 The SMB and the ACPI have separate I/O spaces.
46 We make sure that the SMB is enabled. We leave the ACPI alone.
47
48 This driver controls the SMB Host only.
49
50 This driver does not use interrupts.
51*/
52
53
54/* Note: we assume there can only be one ALI1535, with one SMBus interface */
55
56#include <linux/config.h>
57#include <linux/module.h>
58#include <linux/pci.h>
59#include <linux/kernel.h>
60#include <linux/stddef.h>
61#include <linux/sched.h>
62#include <linux/delay.h>
63#include <linux/ioport.h>
64#include <linux/i2c.h>
65#include <linux/init.h>
66#include <asm/io.h>
67#include <asm/semaphore.h>
68
69
70/* ALI1535 SMBus address offsets */
71#define SMBHSTSTS (0 + ali1535_smba)
72#define SMBHSTTYP (1 + ali1535_smba)
73#define SMBHSTPORT (2 + ali1535_smba)
74#define SMBHSTCMD (7 + ali1535_smba)
75#define SMBHSTADD (3 + ali1535_smba)
76#define SMBHSTDAT0 (4 + ali1535_smba)
77#define SMBHSTDAT1 (5 + ali1535_smba)
78#define SMBBLKDAT (6 + ali1535_smba)
79
80/* PCI Address Constants */
81#define SMBCOM 0x004
82#define SMBREV 0x008
83#define SMBCFG 0x0D1
84#define SMBBA 0x0E2
85#define SMBHSTCFG 0x0F0
86#define SMBCLK 0x0F2
87
88/* Other settings */
89#define MAX_TIMEOUT 500 /* times 1/100 sec */
90#define ALI1535_SMB_IOSIZE 32
91
92#define ALI1535_SMB_DEFAULTBASE 0x8040
93
94/* ALI1535 address lock bits */
95#define ALI1535_LOCK 0x06 /* dwe */
96
97/* ALI1535 command constants */
98#define ALI1535_QUICK 0x00
99#define ALI1535_BYTE 0x10
100#define ALI1535_BYTE_DATA 0x20
101#define ALI1535_WORD_DATA 0x30
102#define ALI1535_BLOCK_DATA 0x40
103#define ALI1535_I2C_READ 0x60
104
105#define ALI1535_DEV10B_EN 0x80 /* Enable 10-bit addressing in */
106 /* I2C read */
107#define ALI1535_T_OUT 0x08 /* Time-out Command (write) */
108#define ALI1535_A_HIGH_BIT9 0x08 /* Bit 9 of 10-bit address in */
109 /* Alert-Response-Address */
110 /* (read) */
111#define ALI1535_KILL 0x04 /* Kill Command (write) */
112#define ALI1535_A_HIGH_BIT8 0x04 /* Bit 8 of 10-bit address in */
113 /* Alert-Response-Address */
114 /* (read) */
115
116#define ALI1535_D_HI_MASK 0x03 /* Mask for isolating bits 9-8 */
117 /* of 10-bit address in I2C */
118 /* Read Command */
119
120/* ALI1535 status register bits */
121#define ALI1535_STS_IDLE 0x04
122#define ALI1535_STS_BUSY 0x08 /* host busy */
123#define ALI1535_STS_DONE 0x10 /* transaction complete */
124#define ALI1535_STS_DEV 0x20 /* device error */
125#define ALI1535_STS_BUSERR 0x40 /* bus error */
126#define ALI1535_STS_FAIL 0x80 /* failed bus transaction */
127#define ALI1535_STS_ERR 0xE0 /* all the bad error bits */
128
129#define ALI1535_BLOCK_CLR 0x04 /* reset block data index */
130
131/* ALI1535 device address register bits */
132#define ALI1535_RD_ADDR 0x01 /* Read/Write Bit in Device */
133 /* Address field */
134 /* -> Write = 0 */
135 /* -> Read = 1 */
136#define ALI1535_SMBIO_EN 0x04 /* SMB I/O Space enable */
137
138
139static unsigned short ali1535_smba;
140static DECLARE_MUTEX(i2c_ali1535_sem);
141
142/* Detect whether a ALI1535 can be found, and initialize it, where necessary.
143 Note the differences between kernels with the old PCI BIOS interface and
144 newer kernels with the real PCI interface. In compat.h some things are
145 defined to make the transition easier. */
146static int ali1535_setup(struct pci_dev *dev)
147{
148 int retval = -ENODEV;
149 unsigned char temp;
150
151 /* Check the following things:
152 - SMB I/O address is initialized
153 - Device is enabled
154 - We can use the addresses
155 */
156
157 /* Determine the address of the SMBus area */
158 pci_read_config_word(dev, SMBBA, &ali1535_smba);
159 ali1535_smba &= (0xffff & ~(ALI1535_SMB_IOSIZE - 1));
160 if (ali1535_smba == 0) {
161 dev_warn(&dev->dev,
162 "ALI1535_smb region uninitialized - upgrade BIOS?\n");
163 goto exit;
164 }
165
166 if (!request_region(ali1535_smba, ALI1535_SMB_IOSIZE, "ali1535-smb")) {
167 dev_err(&dev->dev, "ALI1535_smb region 0x%x already in use!\n",
168 ali1535_smba);
169 goto exit;
170 }
171
172 /* check if whole device is enabled */
173 pci_read_config_byte(dev, SMBCFG, &temp);
174 if ((temp & ALI1535_SMBIO_EN) == 0) {
175 dev_err(&dev->dev, "SMB device not enabled - upgrade BIOS?\n");
176 goto exit_free;
177 }
178
179 /* Is SMB Host controller enabled? */
180 pci_read_config_byte(dev, SMBHSTCFG, &temp);
181 if ((temp & 1) == 0) {
182 dev_err(&dev->dev, "SMBus controller not enabled - upgrade BIOS?\n");
183 goto exit_free;
184 }
185
186 /* set SMB clock to 74KHz as recommended in data sheet */
187 pci_write_config_byte(dev, SMBCLK, 0x20);
188
189 /*
190 The interrupt routing for SMB is set up in register 0x77 in the
191 1533 ISA Bridge device, NOT in the 7101 device.
192 Don't bother with finding the 1533 device and reading the register.
193 if ((....... & 0x0F) == 1)
194 dev_dbg(&dev->dev, "ALI1535 using Interrupt 9 for SMBus.\n");
195 */
196 pci_read_config_byte(dev, SMBREV, &temp);
197 dev_dbg(&dev->dev, "SMBREV = 0x%X\n", temp);
198 dev_dbg(&dev->dev, "ALI1535_smba = 0x%X\n", ali1535_smba);
199
200 retval = 0;
201exit:
202 return retval;
203
204exit_free:
205 release_region(ali1535_smba, ALI1535_SMB_IOSIZE);
206 return retval;
207}
208
209static int ali1535_transaction(struct i2c_adapter *adap)
210{
211 int temp;
212 int result = 0;
213 int timeout = 0;
214
215 dev_dbg(&adap->dev, "Transaction (pre): STS=%02x, TYP=%02x, "
216 "CMD=%02x, ADD=%02x, DAT0=%02x, DAT1=%02x\n",
217 inb_p(SMBHSTSTS), inb_p(SMBHSTTYP), inb_p(SMBHSTCMD),
218 inb_p(SMBHSTADD), inb_p(SMBHSTDAT0), inb_p(SMBHSTDAT1));
219
220 /* get status */
221 temp = inb_p(SMBHSTSTS);
222
223 /* Make sure the SMBus host is ready to start transmitting */
224 /* Check the busy bit first */
225 if (temp & ALI1535_STS_BUSY) {
226 /* If the host controller is still busy, it may have timed out
227 * in the previous transaction, resulting in a "SMBus Timeout"
228 * printk. I've tried the following to reset a stuck busy bit.
229 * 1. Reset the controller with an KILL command. (this
230 * doesn't seem to clear the controller if an external
231 * device is hung)
232 * 2. Reset the controller and the other SMBus devices with a
233 * T_OUT command. (this clears the host busy bit if an
234 * external device is hung, but it comes back upon a new
235 * access to a device)
236 * 3. Disable and reenable the controller in SMBHSTCFG. Worst
237 * case, nothing seems to work except power reset.
238 */
239
240 /* Try resetting entire SMB bus, including other devices - This
241 * may not work either - it clears the BUSY bit but then the
242 * BUSY bit may come back on when you try and use the chip
243 * again. If that's the case you are stuck.
244 */
245 dev_info(&adap->dev,
246 "Resetting entire SMB Bus to clear busy condition (%02x)\n",
247 temp);
248 outb_p(ALI1535_T_OUT, SMBHSTTYP);
249 temp = inb_p(SMBHSTSTS);
250 }
251
252 /* now check the error bits and the busy bit */
253 if (temp & (ALI1535_STS_ERR | ALI1535_STS_BUSY)) {
254 /* do a clear-on-write */
255 outb_p(0xFF, SMBHSTSTS);
256 if ((temp = inb_p(SMBHSTSTS)) &
257 (ALI1535_STS_ERR | ALI1535_STS_BUSY)) {
258 /* This is probably going to be correctable only by a
259 * power reset as one of the bits now appears to be
260 * stuck */
261 /* This may be a bus or device with electrical problems. */
262 dev_err(&adap->dev,
263 "SMBus reset failed! (0x%02x) - controller or "
264 "device on bus is probably hung\n", temp);
265 return -1;
266 }
267 } else {
268 /* check and clear done bit */
269 if (temp & ALI1535_STS_DONE) {
270 outb_p(temp, SMBHSTSTS);
271 }
272 }
273
274 /* start the transaction by writing anything to the start register */
275 outb_p(0xFF, SMBHSTPORT);
276
277 /* We will always wait for a fraction of a second! */
278 timeout = 0;
279 do {
280 msleep(1);
281 temp = inb_p(SMBHSTSTS);
282 } while (((temp & ALI1535_STS_BUSY) && !(temp & ALI1535_STS_IDLE))
283 && (timeout++ < MAX_TIMEOUT));
284
285 /* If the SMBus is still busy, we give up */
286 if (timeout >= MAX_TIMEOUT) {
287 result = -1;
288 dev_err(&adap->dev, "SMBus Timeout!\n");
289 }
290
291 if (temp & ALI1535_STS_FAIL) {
292 result = -1;
293 dev_dbg(&adap->dev, "Error: Failed bus transaction\n");
294 }
295
296 /* Unfortunately the ALI SMB controller maps "no response" and "bus
297 * collision" into a single bit. No reponse is the usual case so don't
298 * do a printk. This means that bus collisions go unreported.
299 */
300 if (temp & ALI1535_STS_BUSERR) {
301 result = -1;
302 dev_dbg(&adap->dev,
303 "Error: no response or bus collision ADD=%02x\n",
304 inb_p(SMBHSTADD));
305 }
306
307 /* haven't ever seen this */
308 if (temp & ALI1535_STS_DEV) {
309 result = -1;
310 dev_err(&adap->dev, "Error: device error\n");
311 }
312
313 /* check to see if the "command complete" indication is set */
314 if (!(temp & ALI1535_STS_DONE)) {
315 result = -1;
316 dev_err(&adap->dev, "Error: command never completed\n");
317 }
318
319 dev_dbg(&adap->dev, "Transaction (post): STS=%02x, TYP=%02x, "
320 "CMD=%02x, ADD=%02x, DAT0=%02x, DAT1=%02x\n",
321 inb_p(SMBHSTSTS), inb_p(SMBHSTTYP), inb_p(SMBHSTCMD),
322 inb_p(SMBHSTADD), inb_p(SMBHSTDAT0), inb_p(SMBHSTDAT1));
323
324 /* take consequent actions for error conditions */
325 if (!(temp & ALI1535_STS_DONE)) {
326 /* issue "kill" to reset host controller */
327 outb_p(ALI1535_KILL,SMBHSTTYP);
328 outb_p(0xFF,SMBHSTSTS);
329 } else if (temp & ALI1535_STS_ERR) {
330 /* issue "timeout" to reset all devices on bus */
331 outb_p(ALI1535_T_OUT,SMBHSTTYP);
332 outb_p(0xFF,SMBHSTSTS);
333 }
334
335 return result;
336}
337
338/* Return -1 on error. */
339static s32 ali1535_access(struct i2c_adapter *adap, u16 addr,
340 unsigned short flags, char read_write, u8 command,
341 int size, union i2c_smbus_data *data)
342{
343 int i, len;
344 int temp;
345 int timeout;
346 s32 result = 0;
347
348 down(&i2c_ali1535_sem);
349 /* make sure SMBus is idle */
350 temp = inb_p(SMBHSTSTS);
351 for (timeout = 0;
352 (timeout < MAX_TIMEOUT) && !(temp & ALI1535_STS_IDLE);
353 timeout++) {
354 msleep(1);
355 temp = inb_p(SMBHSTSTS);
356 }
357 if (timeout >= MAX_TIMEOUT)
358 dev_warn(&adap->dev, "Idle wait Timeout! STS=0x%02x\n", temp);
359
360 /* clear status register (clear-on-write) */
361 outb_p(0xFF, SMBHSTSTS);
362
363 switch (size) {
364 case I2C_SMBUS_PROC_CALL:
365 dev_err(&adap->dev, "I2C_SMBUS_PROC_CALL not supported!\n");
366 result = -1;
367 goto EXIT;
368 case I2C_SMBUS_QUICK:
369 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
370 SMBHSTADD);
371 size = ALI1535_QUICK;
372 outb_p(size, SMBHSTTYP); /* output command */
373 break;
374 case I2C_SMBUS_BYTE:
375 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
376 SMBHSTADD);
377 size = ALI1535_BYTE;
378 outb_p(size, SMBHSTTYP); /* output command */
379 if (read_write == I2C_SMBUS_WRITE)
380 outb_p(command, SMBHSTCMD);
381 break;
382 case I2C_SMBUS_BYTE_DATA:
383 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
384 SMBHSTADD);
385 size = ALI1535_BYTE_DATA;
386 outb_p(size, SMBHSTTYP); /* output command */
387 outb_p(command, SMBHSTCMD);
388 if (read_write == I2C_SMBUS_WRITE)
389 outb_p(data->byte, SMBHSTDAT0);
390 break;
391 case I2C_SMBUS_WORD_DATA:
392 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
393 SMBHSTADD);
394 size = ALI1535_WORD_DATA;
395 outb_p(size, SMBHSTTYP); /* output command */
396 outb_p(command, SMBHSTCMD);
397 if (read_write == I2C_SMBUS_WRITE) {
398 outb_p(data->word & 0xff, SMBHSTDAT0);
399 outb_p((data->word & 0xff00) >> 8, SMBHSTDAT1);
400 }
401 break;
402 case I2C_SMBUS_BLOCK_DATA:
403 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
404 SMBHSTADD);
405 size = ALI1535_BLOCK_DATA;
406 outb_p(size, SMBHSTTYP); /* output command */
407 outb_p(command, SMBHSTCMD);
408 if (read_write == I2C_SMBUS_WRITE) {
409 len = data->block[0];
410 if (len < 0) {
411 len = 0;
412 data->block[0] = len;
413 }
414 if (len > 32) {
415 len = 32;
416 data->block[0] = len;
417 }
418 outb_p(len, SMBHSTDAT0);
419 /* Reset SMBBLKDAT */
420 outb_p(inb_p(SMBHSTTYP) | ALI1535_BLOCK_CLR, SMBHSTTYP);
421 for (i = 1; i <= len; i++)
422 outb_p(data->block[i], SMBBLKDAT);
423 }
424 break;
425 }
426
427 if (ali1535_transaction(adap)) {
428 /* Error in transaction */
429 result = -1;
430 goto EXIT;
431 }
432
433 if ((read_write == I2C_SMBUS_WRITE) || (size == ALI1535_QUICK)) {
434 result = 0;
435 goto EXIT;
436 }
437
438 switch (size) {
439 case ALI1535_BYTE: /* Result put in SMBHSTDAT0 */
440 data->byte = inb_p(SMBHSTDAT0);
441 break;
442 case ALI1535_BYTE_DATA:
443 data->byte = inb_p(SMBHSTDAT0);
444 break;
445 case ALI1535_WORD_DATA:
446 data->word = inb_p(SMBHSTDAT0) + (inb_p(SMBHSTDAT1) << 8);
447 break;
448 case ALI1535_BLOCK_DATA:
449 len = inb_p(SMBHSTDAT0);
450 if (len > 32)
451 len = 32;
452 data->block[0] = len;
453 /* Reset SMBBLKDAT */
454 outb_p(inb_p(SMBHSTTYP) | ALI1535_BLOCK_CLR, SMBHSTTYP);
455 for (i = 1; i <= data->block[0]; i++) {
456 data->block[i] = inb_p(SMBBLKDAT);
457 dev_dbg(&adap->dev, "Blk: len=%d, i=%d, data=%02x\n",
458 len, i, data->block[i]);
459 }
460 break;
461 }
462EXIT:
463 up(&i2c_ali1535_sem);
464 return result;
465}
466
467
468static u32 ali1535_func(struct i2c_adapter *adapter)
469{
470 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
471 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
472 I2C_FUNC_SMBUS_BLOCK_DATA;
473}
474
475static struct i2c_algorithm smbus_algorithm = {
476 .name = "Non-i2c SMBus adapter",
477 .id = I2C_ALGO_SMBUS,
478 .smbus_xfer = ali1535_access,
479 .functionality = ali1535_func,
480};
481
482static struct i2c_adapter ali1535_adapter = {
483 .owner = THIS_MODULE,
484 .class = I2C_CLASS_HWMON,
485 .algo = &smbus_algorithm,
486 .name = "unset",
487};
488
489static struct pci_device_id ali1535_ids[] = {
490 { PCI_DEVICE(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M7101) },
491 { },
492};
493
494MODULE_DEVICE_TABLE (pci, ali1535_ids);
495
496static int __devinit ali1535_probe(struct pci_dev *dev, const struct pci_device_id *id)
497{
498 if (ali1535_setup(dev)) {
499 dev_warn(&dev->dev,
500 "ALI1535 not detected, module not inserted.\n");
501 return -ENODEV;
502 }
503
504 /* set up the driverfs linkage to our parent device */
505 ali1535_adapter.dev.parent = &dev->dev;
506
507 snprintf(ali1535_adapter.name, I2C_NAME_SIZE,
508 "SMBus ALI1535 adapter at %04x", ali1535_smba);
509 return i2c_add_adapter(&ali1535_adapter);
510}
511
512static void __devexit ali1535_remove(struct pci_dev *dev)
513{
514 i2c_del_adapter(&ali1535_adapter);
515 release_region(ali1535_smba, ALI1535_SMB_IOSIZE);
516}
517
518static struct pci_driver ali1535_driver = {
519 .name = "ali1535_smbus",
520 .id_table = ali1535_ids,
521 .probe = ali1535_probe,
522 .remove = __devexit_p(ali1535_remove),
523};
524
525static int __init i2c_ali1535_init(void)
526{
527 return pci_register_driver(&ali1535_driver);
528}
529
530static void __exit i2c_ali1535_exit(void)
531{
532 pci_unregister_driver(&ali1535_driver);
533}
534
535MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, "
536 "Philip Edelbrock <phil@netroedge.com>, "
537 "Mark D. Studebaker <mdsxyz123@yahoo.com> "
538 "and Dan Eaton <dan.eaton@rocketlogix.com>");
539MODULE_DESCRIPTION("ALI1535 SMBus driver");
540MODULE_LICENSE("GPL");
541
542module_init(i2c_ali1535_init);
543module_exit(i2c_ali1535_exit);
diff --git a/drivers/i2c/busses/i2c-ali1563.c b/drivers/i2c/busses/i2c-ali1563.c
new file mode 100644
index 000000000000..35710818fe47
--- /dev/null
+++ b/drivers/i2c/busses/i2c-ali1563.c
@@ -0,0 +1,415 @@
1/**
2 * i2c-ali1563.c - i2c driver for the ALi 1563 Southbridge
3 *
4 * Copyright (C) 2004 Patrick Mochel
5 *
6 * The 1563 southbridge is deceptively similar to the 1533, with a
7 * few notable exceptions. One of those happens to be the fact they
8 * upgraded the i2c core to be 2.0 compliant, and happens to be almost
9 * identical to the i2c controller found in the Intel 801 south
10 * bridges.
11 *
12 * This driver is based on a mix of the 15x3, 1535, and i801 drivers,
13 * with a little help from the ALi 1563 spec.
14 *
15 * This file is released under the GPLv2
16 */
17
18#include <linux/module.h>
19#include <linux/delay.h>
20#include <linux/i2c.h>
21#include <linux/pci.h>
22#include <linux/init.h>
23
24#define ALI1563_MAX_TIMEOUT 500
25#define ALI1563_SMBBA 0x80
26#define ALI1563_SMB_IOEN 1
27#define ALI1563_SMB_HOSTEN 2
28#define ALI1563_SMB_IOSIZE 16
29
30#define SMB_HST_STS (ali1563_smba + 0)
31#define SMB_HST_CNTL1 (ali1563_smba + 1)
32#define SMB_HST_CNTL2 (ali1563_smba + 2)
33#define SMB_HST_CMD (ali1563_smba + 3)
34#define SMB_HST_ADD (ali1563_smba + 4)
35#define SMB_HST_DAT0 (ali1563_smba + 5)
36#define SMB_HST_DAT1 (ali1563_smba + 6)
37#define SMB_BLK_DAT (ali1563_smba + 7)
38
39#define HST_STS_BUSY 0x01
40#define HST_STS_INTR 0x02
41#define HST_STS_DEVERR 0x04
42#define HST_STS_BUSERR 0x08
43#define HST_STS_FAIL 0x10
44#define HST_STS_DONE 0x80
45#define HST_STS_BAD 0x1c
46
47
48#define HST_CNTL1_TIMEOUT 0x80
49#define HST_CNTL1_LAST 0x40
50
51#define HST_CNTL2_KILL 0x04
52#define HST_CNTL2_START 0x40
53#define HST_CNTL2_QUICK 0x00
54#define HST_CNTL2_BYTE 0x01
55#define HST_CNTL2_BYTE_DATA 0x02
56#define HST_CNTL2_WORD_DATA 0x03
57#define HST_CNTL2_BLOCK 0x05
58
59
60
61static unsigned short ali1563_smba;
62
63static int ali1563_transaction(struct i2c_adapter * a)
64{
65 u32 data;
66 int timeout;
67
68 dev_dbg(&a->dev, "Transaction (pre): STS=%02x, CNTL1=%02x, "
69 "CNTL2=%02x, CMD=%02x, ADD=%02x, DAT0=%02x, DAT1=%02x\n",
70 inb_p(SMB_HST_STS), inb_p(SMB_HST_CNTL1), inb_p(SMB_HST_CNTL2),
71 inb_p(SMB_HST_CMD), inb_p(SMB_HST_ADD), inb_p(SMB_HST_DAT0),
72 inb_p(SMB_HST_DAT1));
73
74 data = inb_p(SMB_HST_STS);
75 if (data & HST_STS_BAD) {
76 dev_warn(&a->dev,"ali1563: Trying to reset busy device\n");
77 outb_p(data | HST_STS_BAD,SMB_HST_STS);
78 data = inb_p(SMB_HST_STS);
79 if (data & HST_STS_BAD)
80 return -EBUSY;
81 }
82 outb_p(inb_p(SMB_HST_CNTL2) | HST_CNTL2_START, SMB_HST_CNTL2);
83
84 timeout = ALI1563_MAX_TIMEOUT;
85 do
86 msleep(1);
87 while (((data = inb_p(SMB_HST_STS)) & HST_STS_BUSY) && --timeout);
88
89 dev_dbg(&a->dev, "Transaction (post): STS=%02x, CNTL1=%02x, "
90 "CNTL2=%02x, CMD=%02x, ADD=%02x, DAT0=%02x, DAT1=%02x\n",
91 inb_p(SMB_HST_STS), inb_p(SMB_HST_CNTL1), inb_p(SMB_HST_CNTL2),
92 inb_p(SMB_HST_CMD), inb_p(SMB_HST_ADD), inb_p(SMB_HST_DAT0),
93 inb_p(SMB_HST_DAT1));
94
95 if (timeout && !(data & HST_STS_BAD))
96 return 0;
97 dev_warn(&a->dev, "SMBus Error: %s%s%s%s%s\n",
98 timeout ? "Timeout " : "",
99 data & HST_STS_FAIL ? "Transaction Failed " : "",
100 data & HST_STS_BUSERR ? "No response or Bus Collision " : "",
101 data & HST_STS_DEVERR ? "Device Error " : "",
102 !(data & HST_STS_DONE) ? "Transaction Never Finished " : "");
103
104 if (!(data & HST_STS_DONE))
105 /* Issue 'kill' to host controller */
106 outb_p(HST_CNTL2_KILL,SMB_HST_CNTL2);
107 else
108 /* Issue timeout to reset all devices on bus */
109 outb_p(HST_CNTL1_TIMEOUT,SMB_HST_CNTL1);
110 return -1;
111}
112
113static int ali1563_block_start(struct i2c_adapter * a)
114{
115 u32 data;
116 int timeout;
117
118 dev_dbg(&a->dev, "Block (pre): STS=%02x, CNTL1=%02x, "
119 "CNTL2=%02x, CMD=%02x, ADD=%02x, DAT0=%02x, DAT1=%02x\n",
120 inb_p(SMB_HST_STS), inb_p(SMB_HST_CNTL1), inb_p(SMB_HST_CNTL2),
121 inb_p(SMB_HST_CMD), inb_p(SMB_HST_ADD), inb_p(SMB_HST_DAT0),
122 inb_p(SMB_HST_DAT1));
123
124 data = inb_p(SMB_HST_STS);
125 if (data & HST_STS_BAD) {
126 dev_warn(&a->dev,"ali1563: Trying to reset busy device\n");
127 outb_p(data | HST_STS_BAD,SMB_HST_STS);
128 data = inb_p(SMB_HST_STS);
129 if (data & HST_STS_BAD)
130 return -EBUSY;
131 }
132
133 /* Clear byte-ready bit */
134 outb_p(data | HST_STS_DONE, SMB_HST_STS);
135
136 /* Start transaction and wait for byte-ready bit to be set */
137 outb_p(inb_p(SMB_HST_CNTL2) | HST_CNTL2_START, SMB_HST_CNTL2);
138
139 timeout = ALI1563_MAX_TIMEOUT;
140 do
141 msleep(1);
142 while (!((data = inb_p(SMB_HST_STS)) & HST_STS_DONE) && --timeout);
143
144 dev_dbg(&a->dev, "Block (post): STS=%02x, CNTL1=%02x, "
145 "CNTL2=%02x, CMD=%02x, ADD=%02x, DAT0=%02x, DAT1=%02x\n",
146 inb_p(SMB_HST_STS), inb_p(SMB_HST_CNTL1), inb_p(SMB_HST_CNTL2),
147 inb_p(SMB_HST_CMD), inb_p(SMB_HST_ADD), inb_p(SMB_HST_DAT0),
148 inb_p(SMB_HST_DAT1));
149
150 if (timeout && !(data & HST_STS_BAD))
151 return 0;
152 dev_warn(&a->dev, "SMBus Error: %s%s%s%s%s\n",
153 timeout ? "Timeout " : "",
154 data & HST_STS_FAIL ? "Transaction Failed " : "",
155 data & HST_STS_BUSERR ? "No response or Bus Collision " : "",
156 data & HST_STS_DEVERR ? "Device Error " : "",
157 !(data & HST_STS_DONE) ? "Transaction Never Finished " : "");
158 return -1;
159}
160
161static int ali1563_block(struct i2c_adapter * a, union i2c_smbus_data * data, u8 rw)
162{
163 int i, len;
164 int error = 0;
165
166 /* Do we need this? */
167 outb_p(HST_CNTL1_LAST,SMB_HST_CNTL1);
168
169 if (rw == I2C_SMBUS_WRITE) {
170 len = data->block[0];
171 if (len < 1)
172 len = 1;
173 else if (len > 32)
174 len = 32;
175 outb_p(len,SMB_HST_DAT0);
176 outb_p(data->block[1],SMB_BLK_DAT);
177 } else
178 len = 32;
179
180 outb_p(inb_p(SMB_HST_CNTL2) | HST_CNTL2_BLOCK, SMB_HST_CNTL2);
181
182 for (i = 0; i < len; i++) {
183 if (rw == I2C_SMBUS_WRITE) {
184 outb_p(data->block[i + 1], SMB_BLK_DAT);
185 if ((error = ali1563_block_start(a)))
186 break;
187 } else {
188 if ((error = ali1563_block_start(a)))
189 break;
190 if (i == 0) {
191 len = inb_p(SMB_HST_DAT0);
192 if (len < 1)
193 len = 1;
194 else if (len > 32)
195 len = 32;
196 }
197 data->block[i+1] = inb_p(SMB_BLK_DAT);
198 }
199 }
200 /* Do we need this? */
201 outb_p(HST_CNTL1_LAST,SMB_HST_CNTL1);
202 return error;
203}
204
205static s32 ali1563_access(struct i2c_adapter * a, u16 addr,
206 unsigned short flags, char rw, u8 cmd,
207 int size, union i2c_smbus_data * data)
208{
209 int error = 0;
210 int timeout;
211 u32 reg;
212
213 for (timeout = ALI1563_MAX_TIMEOUT; timeout; timeout--) {
214 if (!(reg = inb_p(SMB_HST_STS) & HST_STS_BUSY))
215 break;
216 }
217 if (!timeout)
218 dev_warn(&a->dev,"SMBus not idle. HST_STS = %02x\n",reg);
219 outb_p(0xff,SMB_HST_STS);
220
221 /* Map the size to what the chip understands */
222 switch (size) {
223 case I2C_SMBUS_PROC_CALL:
224 dev_err(&a->dev, "I2C_SMBUS_PROC_CALL not supported!\n");
225 error = -EINVAL;
226 break;
227 case I2C_SMBUS_QUICK:
228 size = HST_CNTL2_QUICK;
229 break;
230 case I2C_SMBUS_BYTE:
231 size = HST_CNTL2_BYTE;
232 break;
233 case I2C_SMBUS_BYTE_DATA:
234 size = HST_CNTL2_BYTE_DATA;
235 break;
236 case I2C_SMBUS_WORD_DATA:
237 size = HST_CNTL2_WORD_DATA;
238 break;
239 case I2C_SMBUS_BLOCK_DATA:
240 size = HST_CNTL2_BLOCK;
241 break;
242 }
243
244 outb_p(((addr & 0x7f) << 1) | (rw & 0x01), SMB_HST_ADD);
245 outb_p(inb_p(SMB_HST_CNTL2) | (size << 3), SMB_HST_CNTL2);
246
247 /* Write the command register */
248 switch(size) {
249 case HST_CNTL2_BYTE:
250 if (rw== I2C_SMBUS_WRITE)
251 outb_p(cmd, SMB_HST_CMD);
252 break;
253 case HST_CNTL2_BYTE_DATA:
254 outb_p(cmd, SMB_HST_CMD);
255 if (rw == I2C_SMBUS_WRITE)
256 outb_p(data->byte, SMB_HST_DAT0);
257 break;
258 case HST_CNTL2_WORD_DATA:
259 outb_p(cmd, SMB_HST_CMD);
260 if (rw == I2C_SMBUS_WRITE) {
261 outb_p(data->word & 0xff, SMB_HST_DAT0);
262 outb_p((data->word & 0xff00) >> 8, SMB_HST_DAT1);
263 }
264 break;
265 case HST_CNTL2_BLOCK:
266 outb_p(cmd, SMB_HST_CMD);
267 error = ali1563_block(a,data,rw);
268 goto Done;
269 }
270
271 if ((error = ali1563_transaction(a)))
272 goto Done;
273
274 if ((rw == I2C_SMBUS_WRITE) || (size == HST_CNTL2_QUICK))
275 goto Done;
276
277 switch (size) {
278 case HST_CNTL2_BYTE: /* Result put in SMBHSTDAT0 */
279 data->byte = inb_p(SMB_HST_DAT0);
280 break;
281 case HST_CNTL2_BYTE_DATA:
282 data->byte = inb_p(SMB_HST_DAT0);
283 break;
284 case HST_CNTL2_WORD_DATA:
285 data->word = inb_p(SMB_HST_DAT0) + (inb_p(SMB_HST_DAT1) << 8);
286 break;
287 }
288Done:
289 return error;
290}
291
292static u32 ali1563_func(struct i2c_adapter * a)
293{
294 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
295 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
296 I2C_FUNC_SMBUS_BLOCK_DATA;
297}
298
299
300static void ali1563_enable(struct pci_dev * dev)
301{
302 u16 ctrl;
303
304 pci_read_config_word(dev,ALI1563_SMBBA,&ctrl);
305 ctrl |= 0x7;
306 pci_write_config_word(dev,ALI1563_SMBBA,ctrl);
307}
308
309static int __devinit ali1563_setup(struct pci_dev * dev)
310{
311 u16 ctrl;
312
313 pci_read_config_word(dev,ALI1563_SMBBA,&ctrl);
314 printk("ali1563: SMBus control = %04x\n",ctrl);
315
316 /* Check if device is even enabled first */
317 if (!(ctrl & ALI1563_SMB_IOEN)) {
318 dev_warn(&dev->dev,"I/O space not enabled, trying manually\n");
319 ali1563_enable(dev);
320 }
321 if (!(ctrl & ALI1563_SMB_IOEN)) {
322 dev_warn(&dev->dev,"I/O space still not enabled, giving up\n");
323 goto Err;
324 }
325 if (!(ctrl & ALI1563_SMB_HOSTEN)) {
326 dev_warn(&dev->dev,"Host Controller not enabled\n");
327 goto Err;
328 }
329
330 /* SMB I/O Base in high 12 bits and must be aligned with the
331 * size of the I/O space. */
332 ali1563_smba = ctrl & ~(ALI1563_SMB_IOSIZE - 1);
333 if (!ali1563_smba) {
334 dev_warn(&dev->dev,"ali1563_smba Uninitialized\n");
335 goto Err;
336 }
337 if (!request_region(ali1563_smba,ALI1563_SMB_IOSIZE,"i2c-ali1563")) {
338 dev_warn(&dev->dev,"Could not allocate I/O space");
339 goto Err;
340 }
341
342 return 0;
343Err:
344 return -ENODEV;
345}
346
347static void ali1563_shutdown(struct pci_dev *dev)
348{
349 release_region(ali1563_smba,ALI1563_SMB_IOSIZE);
350}
351
352static struct i2c_algorithm ali1563_algorithm = {
353 .name = "Non-i2c SMBus adapter",
354 .id = I2C_ALGO_SMBUS,
355 .smbus_xfer = ali1563_access,
356 .functionality = ali1563_func,
357};
358
359static struct i2c_adapter ali1563_adapter = {
360 .owner = THIS_MODULE,
361 .class = I2C_CLASS_HWMON,
362 .algo = &ali1563_algorithm,
363};
364
365static int __devinit ali1563_probe(struct pci_dev * dev,
366 const struct pci_device_id * id_table)
367{
368 int error;
369
370 if ((error = ali1563_setup(dev)))
371 return error;
372 ali1563_adapter.dev.parent = &dev->dev;
373 sprintf(ali1563_adapter.name,"SMBus ALi 1563 Adapter @ %04x",
374 ali1563_smba);
375 if ((error = i2c_add_adapter(&ali1563_adapter)))
376 ali1563_shutdown(dev);
377 printk("%s: Returning %d\n",__FUNCTION__,error);
378 return error;
379}
380
381static void __devexit ali1563_remove(struct pci_dev * dev)
382{
383 i2c_del_adapter(&ali1563_adapter);
384 ali1563_shutdown(dev);
385}
386
387static struct pci_device_id __devinitdata ali1563_id_table[] = {
388 { PCI_DEVICE(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M1563) },
389 {},
390};
391
392MODULE_DEVICE_TABLE (pci, ali1563_id_table);
393
394static struct pci_driver ali1563_pci_driver = {
395 .name = "ali1563_i2c",
396 .id_table = ali1563_id_table,
397 .probe = ali1563_probe,
398 .remove = __devexit_p(ali1563_remove),
399};
400
401static int __init ali1563_init(void)
402{
403 return pci_register_driver(&ali1563_pci_driver);
404}
405
406module_init(ali1563_init);
407
408static void __exit ali1563_exit(void)
409{
410 pci_unregister_driver(&ali1563_pci_driver);
411}
412
413module_exit(ali1563_exit);
414
415MODULE_LICENSE("GPL");
diff --git a/drivers/i2c/busses/i2c-ali15x3.c b/drivers/i2c/busses/i2c-ali15x3.c
new file mode 100644
index 000000000000..5bd6a4a77c1e
--- /dev/null
+++ b/drivers/i2c/busses/i2c-ali15x3.c
@@ -0,0 +1,532 @@
1/*
2 ali15x3.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring
4 Copyright (c) 1999 Frodo Looijaard <frodol@dds.nl> and
5 Philip Edelbrock <phil@netroedge.com> and
6 Mark D. Studebaker <mdsxyz123@yahoo.com>
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; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21*/
22
23/*
24 This is the driver for the SMB Host controller on
25 Acer Labs Inc. (ALI) M1541 and M1543C South Bridges.
26
27 The M1543C is a South bridge for desktop systems.
28 The M1533 is a South bridge for portable systems.
29 They are part of the following ALI chipsets:
30 "Aladdin Pro 2": Includes the M1621 Slot 1 North bridge
31 with AGP and 100MHz CPU Front Side bus
32 "Aladdin V": Includes the M1541 Socket 7 North bridge
33 with AGP and 100MHz CPU Front Side bus
34 "Aladdin IV": Includes the M1541 Socket 7 North bridge
35 with host bus up to 83.3 MHz.
36 For an overview of these chips see http://www.acerlabs.com
37
38 The M1533/M1543C devices appear as FOUR separate devices
39 on the PCI bus. An output of lspci will show something similar
40 to the following:
41
42 00:02.0 USB Controller: Acer Laboratories Inc. M5237
43 00:03.0 Bridge: Acer Laboratories Inc. M7101
44 00:07.0 ISA bridge: Acer Laboratories Inc. M1533
45 00:0f.0 IDE interface: Acer Laboratories Inc. M5229
46
47 The SMB controller is part of the 7101 device, which is an
48 ACPI-compliant Power Management Unit (PMU).
49
50 The whole 7101 device has to be enabled for the SMB to work.
51 You can't just enable the SMB alone.
52 The SMB and the ACPI have separate I/O spaces.
53 We make sure that the SMB is enabled. We leave the ACPI alone.
54
55 This driver controls the SMB Host only.
56 The SMB Slave controller on the M15X3 is not enabled.
57
58 This driver does not use interrupts.
59*/
60
61/* Note: we assume there can only be one ALI15X3, with one SMBus interface */
62
63#include <linux/config.h>
64#include <linux/module.h>
65#include <linux/pci.h>
66#include <linux/kernel.h>
67#include <linux/stddef.h>
68#include <linux/sched.h>
69#include <linux/ioport.h>
70#include <linux/delay.h>
71#include <linux/i2c.h>
72#include <linux/init.h>
73#include <asm/io.h>
74
75/* ALI15X3 SMBus address offsets */
76#define SMBHSTSTS (0 + ali15x3_smba)
77#define SMBHSTCNT (1 + ali15x3_smba)
78#define SMBHSTSTART (2 + ali15x3_smba)
79#define SMBHSTCMD (7 + ali15x3_smba)
80#define SMBHSTADD (3 + ali15x3_smba)
81#define SMBHSTDAT0 (4 + ali15x3_smba)
82#define SMBHSTDAT1 (5 + ali15x3_smba)
83#define SMBBLKDAT (6 + ali15x3_smba)
84
85/* PCI Address Constants */
86#define SMBCOM 0x004
87#define SMBBA 0x014
88#define SMBATPC 0x05B /* used to unlock xxxBA registers */
89#define SMBHSTCFG 0x0E0
90#define SMBSLVC 0x0E1
91#define SMBCLK 0x0E2
92#define SMBREV 0x008
93
94/* Other settings */
95#define MAX_TIMEOUT 200 /* times 1/100 sec */
96#define ALI15X3_SMB_IOSIZE 32
97
98/* this is what the Award 1004 BIOS sets them to on a ASUS P5A MB.
99 We don't use these here. If the bases aren't set to some value we
100 tell user to upgrade BIOS and we fail.
101*/
102#define ALI15X3_SMB_DEFAULTBASE 0xE800
103
104/* ALI15X3 address lock bits */
105#define ALI15X3_LOCK 0x06
106
107/* ALI15X3 command constants */
108#define ALI15X3_ABORT 0x02
109#define ALI15X3_T_OUT 0x04
110#define ALI15X3_QUICK 0x00
111#define ALI15X3_BYTE 0x10
112#define ALI15X3_BYTE_DATA 0x20
113#define ALI15X3_WORD_DATA 0x30
114#define ALI15X3_BLOCK_DATA 0x40
115#define ALI15X3_BLOCK_CLR 0x80
116
117/* ALI15X3 status register bits */
118#define ALI15X3_STS_IDLE 0x04
119#define ALI15X3_STS_BUSY 0x08
120#define ALI15X3_STS_DONE 0x10
121#define ALI15X3_STS_DEV 0x20 /* device error */
122#define ALI15X3_STS_COLL 0x40 /* collision or no response */
123#define ALI15X3_STS_TERM 0x80 /* terminated by abort */
124#define ALI15X3_STS_ERR 0xE0 /* all the bad error bits */
125
126
127/* If force_addr is set to anything different from 0, we forcibly enable
128 the device at the given address. */
129static u16 force_addr = 0;
130module_param(force_addr, ushort, 0);
131MODULE_PARM_DESC(force_addr,
132 "Initialize the base address of the i2c controller");
133
134static unsigned short ali15x3_smba = 0;
135
136static int ali15x3_setup(struct pci_dev *ALI15X3_dev)
137{
138 u16 a;
139 unsigned char temp;
140
141 /* Check the following things:
142 - SMB I/O address is initialized
143 - Device is enabled
144 - We can use the addresses
145 */
146
147 /* Unlock the register.
148 The data sheet says that the address registers are read-only
149 if the lock bits are 1, but in fact the address registers
150 are zero unless you clear the lock bits.
151 */
152 pci_read_config_byte(ALI15X3_dev, SMBATPC, &temp);
153 if (temp & ALI15X3_LOCK) {
154 temp &= ~ALI15X3_LOCK;
155 pci_write_config_byte(ALI15X3_dev, SMBATPC, temp);
156 }
157
158 /* Determine the address of the SMBus area */
159 pci_read_config_word(ALI15X3_dev, SMBBA, &ali15x3_smba);
160 ali15x3_smba &= (0xffff & ~(ALI15X3_SMB_IOSIZE - 1));
161 if (ali15x3_smba == 0 && force_addr == 0) {
162 dev_err(&ALI15X3_dev->dev, "ALI15X3_smb region uninitialized "
163 "- upgrade BIOS or use force_addr=0xaddr\n");
164 return -ENODEV;
165 }
166
167 if(force_addr)
168 ali15x3_smba = force_addr & ~(ALI15X3_SMB_IOSIZE - 1);
169
170 if (!request_region(ali15x3_smba, ALI15X3_SMB_IOSIZE, "ali15x3-smb")) {
171 dev_err(&ALI15X3_dev->dev,
172 "ALI15X3_smb region 0x%x already in use!\n",
173 ali15x3_smba);
174 return -ENODEV;
175 }
176
177 if(force_addr) {
178 dev_info(&ALI15X3_dev->dev, "forcing ISA address 0x%04X\n",
179 ali15x3_smba);
180 if (PCIBIOS_SUCCESSFUL != pci_write_config_word(ALI15X3_dev,
181 SMBBA,
182 ali15x3_smba))
183 goto error;
184 if (PCIBIOS_SUCCESSFUL != pci_read_config_word(ALI15X3_dev,
185 SMBBA, &a))
186 goto error;
187 if ((a & ~(ALI15X3_SMB_IOSIZE - 1)) != ali15x3_smba) {
188 /* make sure it works */
189 dev_err(&ALI15X3_dev->dev,
190 "force address failed - not supported?\n");
191 goto error;
192 }
193 }
194 /* check if whole device is enabled */
195 pci_read_config_byte(ALI15X3_dev, SMBCOM, &temp);
196 if ((temp & 1) == 0) {
197 dev_info(&ALI15X3_dev->dev, "enabling SMBus device\n");
198 pci_write_config_byte(ALI15X3_dev, SMBCOM, temp | 0x01);
199 }
200
201 /* Is SMB Host controller enabled? */
202 pci_read_config_byte(ALI15X3_dev, SMBHSTCFG, &temp);
203 if ((temp & 1) == 0) {
204 dev_info(&ALI15X3_dev->dev, "enabling SMBus controller\n");
205 pci_write_config_byte(ALI15X3_dev, SMBHSTCFG, temp | 0x01);
206 }
207
208 /* set SMB clock to 74KHz as recommended in data sheet */
209 pci_write_config_byte(ALI15X3_dev, SMBCLK, 0x20);
210
211 /*
212 The interrupt routing for SMB is set up in register 0x77 in the
213 1533 ISA Bridge device, NOT in the 7101 device.
214 Don't bother with finding the 1533 device and reading the register.
215 if ((....... & 0x0F) == 1)
216 dev_dbg(&ALI15X3_dev->dev, "ALI15X3 using Interrupt 9 for SMBus.\n");
217 */
218 pci_read_config_byte(ALI15X3_dev, SMBREV, &temp);
219 dev_dbg(&ALI15X3_dev->dev, "SMBREV = 0x%X\n", temp);
220 dev_dbg(&ALI15X3_dev->dev, "iALI15X3_smba = 0x%X\n", ali15x3_smba);
221
222 return 0;
223error:
224 release_region(ali15x3_smba, ALI15X3_SMB_IOSIZE);
225 return -ENODEV;
226}
227
228/* Another internally used function */
229static int ali15x3_transaction(struct i2c_adapter *adap)
230{
231 int temp;
232 int result = 0;
233 int timeout = 0;
234
235 dev_dbg(&adap->dev, "Transaction (pre): STS=%02x, CNT=%02x, CMD=%02x, "
236 "ADD=%02x, DAT0=%02x, DAT1=%02x\n", inb_p(SMBHSTSTS),
237 inb_p(SMBHSTCNT), inb_p(SMBHSTCMD), inb_p(SMBHSTADD),
238 inb_p(SMBHSTDAT0), inb_p(SMBHSTDAT1));
239
240 /* get status */
241 temp = inb_p(SMBHSTSTS);
242
243 /* Make sure the SMBus host is ready to start transmitting */
244 /* Check the busy bit first */
245 if (temp & ALI15X3_STS_BUSY) {
246 /*
247 If the host controller is still busy, it may have timed out in the
248 previous transaction, resulting in a "SMBus Timeout" Dev.
249 I've tried the following to reset a stuck busy bit.
250 1. Reset the controller with an ABORT command.
251 (this doesn't seem to clear the controller if an external
252 device is hung)
253 2. Reset the controller and the other SMBus devices with a
254 T_OUT command. (this clears the host busy bit if an
255 external device is hung, but it comes back upon a new access
256 to a device)
257 3. Disable and reenable the controller in SMBHSTCFG
258 Worst case, nothing seems to work except power reset.
259 */
260 /* Abort - reset the host controller */
261 /*
262 Try resetting entire SMB bus, including other devices -
263 This may not work either - it clears the BUSY bit but
264 then the BUSY bit may come back on when you try and use the chip again.
265 If that's the case you are stuck.
266 */
267 dev_info(&adap->dev, "Resetting entire SMB Bus to "
268 "clear busy condition (%02x)\n", temp);
269 outb_p(ALI15X3_T_OUT, SMBHSTCNT);
270 temp = inb_p(SMBHSTSTS);
271 }
272
273 /* now check the error bits and the busy bit */
274 if (temp & (ALI15X3_STS_ERR | ALI15X3_STS_BUSY)) {
275 /* do a clear-on-write */
276 outb_p(0xFF, SMBHSTSTS);
277 if ((temp = inb_p(SMBHSTSTS)) &
278 (ALI15X3_STS_ERR | ALI15X3_STS_BUSY)) {
279 /* this is probably going to be correctable only by a power reset
280 as one of the bits now appears to be stuck */
281 /* This may be a bus or device with electrical problems. */
282 dev_err(&adap->dev, "SMBus reset failed! (0x%02x) - "
283 "controller or device on bus is probably hung\n",
284 temp);
285 return -1;
286 }
287 } else {
288 /* check and clear done bit */
289 if (temp & ALI15X3_STS_DONE) {
290 outb_p(temp, SMBHSTSTS);
291 }
292 }
293
294 /* start the transaction by writing anything to the start register */
295 outb_p(0xFF, SMBHSTSTART);
296
297 /* We will always wait for a fraction of a second! */
298 timeout = 0;
299 do {
300 msleep(1);
301 temp = inb_p(SMBHSTSTS);
302 } while ((!(temp & (ALI15X3_STS_ERR | ALI15X3_STS_DONE)))
303 && (timeout++ < MAX_TIMEOUT));
304
305 /* If the SMBus is still busy, we give up */
306 if (timeout >= MAX_TIMEOUT) {
307 result = -1;
308 dev_err(&adap->dev, "SMBus Timeout!\n");
309 }
310
311 if (temp & ALI15X3_STS_TERM) {
312 result = -1;
313 dev_dbg(&adap->dev, "Error: Failed bus transaction\n");
314 }
315
316 /*
317 Unfortunately the ALI SMB controller maps "no response" and "bus
318 collision" into a single bit. No reponse is the usual case so don't
319 do a printk.
320 This means that bus collisions go unreported.
321 */
322 if (temp & ALI15X3_STS_COLL) {
323 result = -1;
324 dev_dbg(&adap->dev,
325 "Error: no response or bus collision ADD=%02x\n",
326 inb_p(SMBHSTADD));
327 }
328
329 /* haven't ever seen this */
330 if (temp & ALI15X3_STS_DEV) {
331 result = -1;
332 dev_err(&adap->dev, "Error: device error\n");
333 }
334 dev_dbg(&adap->dev, "Transaction (post): STS=%02x, CNT=%02x, CMD=%02x, "
335 "ADD=%02x, DAT0=%02x, DAT1=%02x\n", inb_p(SMBHSTSTS),
336 inb_p(SMBHSTCNT), inb_p(SMBHSTCMD), inb_p(SMBHSTADD),
337 inb_p(SMBHSTDAT0), inb_p(SMBHSTDAT1));
338 return result;
339}
340
341/* Return -1 on error. */
342static s32 ali15x3_access(struct i2c_adapter * adap, u16 addr,
343 unsigned short flags, char read_write, u8 command,
344 int size, union i2c_smbus_data * data)
345{
346 int i, len;
347 int temp;
348 int timeout;
349
350 /* clear all the bits (clear-on-write) */
351 outb_p(0xFF, SMBHSTSTS);
352 /* make sure SMBus is idle */
353 temp = inb_p(SMBHSTSTS);
354 for (timeout = 0;
355 (timeout < MAX_TIMEOUT) && !(temp & ALI15X3_STS_IDLE);
356 timeout++) {
357 msleep(1);
358 temp = inb_p(SMBHSTSTS);
359 }
360 if (timeout >= MAX_TIMEOUT) {
361 dev_err(&adap->dev, "Idle wait Timeout! STS=0x%02x\n", temp);
362 }
363
364 switch (size) {
365 case I2C_SMBUS_PROC_CALL:
366 dev_err(&adap->dev, "I2C_SMBUS_PROC_CALL not supported!\n");
367 return -1;
368 case I2C_SMBUS_QUICK:
369 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
370 SMBHSTADD);
371 size = ALI15X3_QUICK;
372 break;
373 case I2C_SMBUS_BYTE:
374 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
375 SMBHSTADD);
376 if (read_write == I2C_SMBUS_WRITE)
377 outb_p(command, SMBHSTCMD);
378 size = ALI15X3_BYTE;
379 break;
380 case I2C_SMBUS_BYTE_DATA:
381 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
382 SMBHSTADD);
383 outb_p(command, SMBHSTCMD);
384 if (read_write == I2C_SMBUS_WRITE)
385 outb_p(data->byte, SMBHSTDAT0);
386 size = ALI15X3_BYTE_DATA;
387 break;
388 case I2C_SMBUS_WORD_DATA:
389 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
390 SMBHSTADD);
391 outb_p(command, SMBHSTCMD);
392 if (read_write == I2C_SMBUS_WRITE) {
393 outb_p(data->word & 0xff, SMBHSTDAT0);
394 outb_p((data->word & 0xff00) >> 8, SMBHSTDAT1);
395 }
396 size = ALI15X3_WORD_DATA;
397 break;
398 case I2C_SMBUS_BLOCK_DATA:
399 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
400 SMBHSTADD);
401 outb_p(command, SMBHSTCMD);
402 if (read_write == I2C_SMBUS_WRITE) {
403 len = data->block[0];
404 if (len < 0) {
405 len = 0;
406 data->block[0] = len;
407 }
408 if (len > 32) {
409 len = 32;
410 data->block[0] = len;
411 }
412 outb_p(len, SMBHSTDAT0);
413 /* Reset SMBBLKDAT */
414 outb_p(inb_p(SMBHSTCNT) | ALI15X3_BLOCK_CLR, SMBHSTCNT);
415 for (i = 1; i <= len; i++)
416 outb_p(data->block[i], SMBBLKDAT);
417 }
418 size = ALI15X3_BLOCK_DATA;
419 break;
420 }
421
422 outb_p(size, SMBHSTCNT); /* output command */
423
424 if (ali15x3_transaction(adap)) /* Error in transaction */
425 return -1;
426
427 if ((read_write == I2C_SMBUS_WRITE) || (size == ALI15X3_QUICK))
428 return 0;
429
430
431 switch (size) {
432 case ALI15X3_BYTE: /* Result put in SMBHSTDAT0 */
433 data->byte = inb_p(SMBHSTDAT0);
434 break;
435 case ALI15X3_BYTE_DATA:
436 data->byte = inb_p(SMBHSTDAT0);
437 break;
438 case ALI15X3_WORD_DATA:
439 data->word = inb_p(SMBHSTDAT0) + (inb_p(SMBHSTDAT1) << 8);
440 break;
441 case ALI15X3_BLOCK_DATA:
442 len = inb_p(SMBHSTDAT0);
443 if (len > 32)
444 len = 32;
445 data->block[0] = len;
446 /* Reset SMBBLKDAT */
447 outb_p(inb_p(SMBHSTCNT) | ALI15X3_BLOCK_CLR, SMBHSTCNT);
448 for (i = 1; i <= data->block[0]; i++) {
449 data->block[i] = inb_p(SMBBLKDAT);
450 dev_dbg(&adap->dev, "Blk: len=%d, i=%d, data=%02x\n",
451 len, i, data->block[i]);
452 }
453 break;
454 }
455 return 0;
456}
457
458static u32 ali15x3_func(struct i2c_adapter *adapter)
459{
460 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
461 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
462 I2C_FUNC_SMBUS_BLOCK_DATA;
463}
464
465static struct i2c_algorithm smbus_algorithm = {
466 .name = "Non-I2C SMBus adapter",
467 .id = I2C_ALGO_SMBUS,
468 .smbus_xfer = ali15x3_access,
469 .functionality = ali15x3_func,
470};
471
472static struct i2c_adapter ali15x3_adapter = {
473 .owner = THIS_MODULE,
474 .class = I2C_CLASS_HWMON,
475 .algo = &smbus_algorithm,
476 .name = "unset",
477};
478
479static struct pci_device_id ali15x3_ids[] = {
480 { PCI_DEVICE(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M7101) },
481 { 0, }
482};
483
484MODULE_DEVICE_TABLE (pci, ali15x3_ids);
485
486static int __devinit ali15x3_probe(struct pci_dev *dev, const struct pci_device_id *id)
487{
488 if (ali15x3_setup(dev)) {
489 dev_err(&dev->dev,
490 "ALI15X3 not detected, module not inserted.\n");
491 return -ENODEV;
492 }
493
494 /* set up the driverfs linkage to our parent device */
495 ali15x3_adapter.dev.parent = &dev->dev;
496
497 snprintf(ali15x3_adapter.name, I2C_NAME_SIZE,
498 "SMBus ALI15X3 adapter at %04x", ali15x3_smba);
499 return i2c_add_adapter(&ali15x3_adapter);
500}
501
502static void __devexit ali15x3_remove(struct pci_dev *dev)
503{
504 i2c_del_adapter(&ali15x3_adapter);
505 release_region(ali15x3_smba, ALI15X3_SMB_IOSIZE);
506}
507
508static struct pci_driver ali15x3_driver = {
509 .name = "ali15x3_smbus",
510 .id_table = ali15x3_ids,
511 .probe = ali15x3_probe,
512 .remove = __devexit_p(ali15x3_remove),
513};
514
515static int __init i2c_ali15x3_init(void)
516{
517 return pci_register_driver(&ali15x3_driver);
518}
519
520static void __exit i2c_ali15x3_exit(void)
521{
522 pci_unregister_driver(&ali15x3_driver);
523}
524
525MODULE_AUTHOR ("Frodo Looijaard <frodol@dds.nl>, "
526 "Philip Edelbrock <phil@netroedge.com>, "
527 "and Mark D. Studebaker <mdsxyz123@yahoo.com>");
528MODULE_DESCRIPTION("ALI15X3 SMBus driver");
529MODULE_LICENSE("GPL");
530
531module_init(i2c_ali15x3_init);
532module_exit(i2c_ali15x3_exit);
diff --git a/drivers/i2c/busses/i2c-amd756-s4882.c b/drivers/i2c/busses/i2c-amd756-s4882.c
new file mode 100644
index 000000000000..4e553e8c5cba
--- /dev/null
+++ b/drivers/i2c/busses/i2c-amd756-s4882.c
@@ -0,0 +1,264 @@
1/*
2 * i2c-amd756-s4882.c - i2c-amd756 extras for the Tyan S4882 motherboard
3 *
4 * Copyright (C) 2004 Jean Delvare <khali@linux-fr.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21/*
22 * We select the channels by sending commands to the Philips
23 * PCA9556 chip at I2C address 0x18. The main adapter is used for
24 * the non-multiplexed part of the bus, and 4 virtual adapters
25 * are defined for the multiplexed addresses: 0x50-0x53 (memory
26 * module EEPROM) located on channels 1-4, and 0x4c (LM63)
27 * located on multiplexed channels 0 and 5-7. We define one
28 * virtual adapter per CPU, which corresponds to two multiplexed
29 * channels:
30 * CPU0: virtual adapter 1, channels 1 and 0
31 * CPU1: virtual adapter 2, channels 2 and 5
32 * CPU2: virtual adapter 3, channels 3 and 6
33 * CPU3: virtual adapter 4, channels 4 and 7
34 */
35
36#include <linux/module.h>
37#include <linux/kernel.h>
38#include <linux/slab.h>
39#include <linux/init.h>
40#include <linux/i2c.h>
41
42extern struct i2c_adapter amd756_smbus;
43
44static struct i2c_adapter *s4882_adapter;
45static struct i2c_algorithm *s4882_algo;
46
47/* Wrapper access functions for multiplexed SMBus */
48static struct semaphore amd756_lock;
49
50static s32 amd756_access_virt0(struct i2c_adapter * adap, u16 addr,
51 unsigned short flags, char read_write,
52 u8 command, int size,
53 union i2c_smbus_data * data)
54{
55 int error;
56
57 /* We exclude the multiplexed addresses */
58 if (addr == 0x4c || (addr & 0xfc) == 0x50 || (addr & 0xfc) == 0x30
59 || addr == 0x18)
60 return -1;
61
62 down(&amd756_lock);
63
64 error = amd756_smbus.algo->smbus_xfer(adap, addr, flags, read_write,
65 command, size, data);
66
67 up(&amd756_lock);
68
69 return error;
70}
71
72/* We remember the last used channels combination so as to only switch
73 channels when it is really needed. This greatly reduces the SMBus
74 overhead, but also assumes that nobody will be writing to the PCA9556
75 in our back. */
76static u8 last_channels;
77
78static inline s32 amd756_access_channel(struct i2c_adapter * adap, u16 addr,
79 unsigned short flags, char read_write,
80 u8 command, int size,
81 union i2c_smbus_data * data,
82 u8 channels)
83{
84 int error;
85
86 /* We exclude the non-multiplexed addresses */
87 if (addr != 0x4c && (addr & 0xfc) != 0x50 && (addr & 0xfc) != 0x30)
88 return -1;
89
90 down(&amd756_lock);
91
92 if (last_channels != channels) {
93 union i2c_smbus_data mplxdata;
94 mplxdata.byte = channels;
95
96 error = amd756_smbus.algo->smbus_xfer(adap, 0x18, 0,
97 I2C_SMBUS_WRITE, 0x01,
98 I2C_SMBUS_BYTE_DATA,
99 &mplxdata);
100 if (error)
101 goto UNLOCK;
102 last_channels = channels;
103 }
104 error = amd756_smbus.algo->smbus_xfer(adap, addr, flags, read_write,
105 command, size, data);
106
107UNLOCK:
108 up(&amd756_lock);
109 return error;
110}
111
112static s32 amd756_access_virt1(struct i2c_adapter * adap, u16 addr,
113 unsigned short flags, char read_write,
114 u8 command, int size,
115 union i2c_smbus_data * data)
116{
117 /* CPU0: channels 1 and 0 enabled */
118 return amd756_access_channel(adap, addr, flags, read_write, command,
119 size, data, 0x03);
120}
121
122static s32 amd756_access_virt2(struct i2c_adapter * adap, u16 addr,
123 unsigned short flags, char read_write,
124 u8 command, int size,
125 union i2c_smbus_data * data)
126{
127 /* CPU1: channels 2 and 5 enabled */
128 return amd756_access_channel(adap, addr, flags, read_write, command,
129 size, data, 0x24);
130}
131
132static s32 amd756_access_virt3(struct i2c_adapter * adap, u16 addr,
133 unsigned short flags, char read_write,
134 u8 command, int size,
135 union i2c_smbus_data * data)
136{
137 /* CPU2: channels 3 and 6 enabled */
138 return amd756_access_channel(adap, addr, flags, read_write, command,
139 size, data, 0x48);
140}
141
142static s32 amd756_access_virt4(struct i2c_adapter * adap, u16 addr,
143 unsigned short flags, char read_write,
144 u8 command, int size,
145 union i2c_smbus_data * data)
146{
147 /* CPU3: channels 4 and 7 enabled */
148 return amd756_access_channel(adap, addr, flags, read_write, command,
149 size, data, 0x90);
150}
151
152static int __init amd756_s4882_init(void)
153{
154 int i, error;
155 union i2c_smbus_data ioconfig;
156
157 /* Unregister physical bus */
158 error = i2c_del_adapter(&amd756_smbus);
159 if (error) {
160 if (error == -EINVAL)
161 error = -ENODEV;
162 else
163 dev_err(&amd756_smbus.dev, "Physical bus removal "
164 "failed\n");
165 goto ERROR0;
166 }
167
168 printk(KERN_INFO "Enabling SMBus multiplexing for Tyan S4882\n");
169 init_MUTEX(&amd756_lock);
170
171 /* Define the 5 virtual adapters and algorithms structures */
172 if (!(s4882_adapter = kmalloc(5 * sizeof(struct i2c_adapter),
173 GFP_KERNEL))) {
174 error = -ENOMEM;
175 goto ERROR1;
176 }
177 if (!(s4882_algo = kmalloc(5 * sizeof(struct i2c_algorithm),
178 GFP_KERNEL))) {
179 error = -ENOMEM;
180 goto ERROR2;
181 }
182
183 /* Fill in the new structures */
184 s4882_algo[0] = *(amd756_smbus.algo);
185 s4882_algo[0].smbus_xfer = amd756_access_virt0;
186 s4882_adapter[0] = amd756_smbus;
187 s4882_adapter[0].algo = s4882_algo;
188 for (i = 1; i < 5; i++) {
189 s4882_algo[i] = *(amd756_smbus.algo);
190 s4882_adapter[i] = amd756_smbus;
191 sprintf(s4882_adapter[i].name,
192 "SMBus 8111 adapter (CPU%d)", i-1);
193 s4882_adapter[i].algo = s4882_algo+i;
194 }
195 s4882_algo[1].smbus_xfer = amd756_access_virt1;
196 s4882_algo[2].smbus_xfer = amd756_access_virt2;
197 s4882_algo[3].smbus_xfer = amd756_access_virt3;
198 s4882_algo[4].smbus_xfer = amd756_access_virt4;
199
200 /* Configure the PCA9556 multiplexer */
201 ioconfig.byte = 0x00; /* All I/O to output mode */
202 error = amd756_smbus.algo->smbus_xfer(&amd756_smbus, 0x18, 0,
203 I2C_SMBUS_WRITE, 0x03,
204 I2C_SMBUS_BYTE_DATA, &ioconfig);
205 if (error) {
206 dev_err(&amd756_smbus.dev, "PCA9556 configuration failed\n");
207 error = -EIO;
208 goto ERROR3;
209 }
210
211 /* Register virtual adapters */
212 for (i = 0; i < 5; i++) {
213 error = i2c_add_adapter(s4882_adapter+i);
214 if (error) {
215 dev_err(&amd756_smbus.dev,
216 "Virtual adapter %d registration "
217 "failed, module not inserted\n", i);
218 for (i--; i >= 0; i--)
219 i2c_del_adapter(s4882_adapter+i);
220 goto ERROR3;
221 }
222 }
223
224 return 0;
225
226ERROR3:
227 kfree(s4882_algo);
228 s4882_algo = NULL;
229ERROR2:
230 kfree(s4882_adapter);
231 s4882_adapter = NULL;
232ERROR1:
233 i2c_del_adapter(&amd756_smbus);
234ERROR0:
235 return error;
236}
237
238static void __exit amd756_s4882_exit(void)
239{
240 if (s4882_adapter) {
241 int i;
242
243 for (i = 0; i < 5; i++)
244 i2c_del_adapter(s4882_adapter+i);
245 kfree(s4882_adapter);
246 s4882_adapter = NULL;
247 }
248 if (s4882_algo) {
249 kfree(s4882_algo);
250 s4882_algo = NULL;
251 }
252
253 /* Restore physical bus */
254 if (i2c_add_adapter(&amd756_smbus))
255 dev_err(&amd756_smbus.dev, "Physical bus restoration "
256 "failed\n");
257}
258
259MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
260MODULE_DESCRIPTION("S4882 SMBus multiplexing");
261MODULE_LICENSE("GPL");
262
263module_init(amd756_s4882_init);
264module_exit(amd756_s4882_exit);
diff --git a/drivers/i2c/busses/i2c-amd756.c b/drivers/i2c/busses/i2c-amd756.c
new file mode 100644
index 000000000000..eca5ed3738b8
--- /dev/null
+++ b/drivers/i2c/busses/i2c-amd756.c
@@ -0,0 +1,431 @@
1/*
2 amd756.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring
4
5 Copyright (c) 1999-2002 Merlin Hughes <merlin@merlin.org>
6
7 Shamelessly ripped from i2c-piix4.c:
8
9 Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl> and
10 Philip Edelbrock <phil@netroedge.com>
11
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25*/
26
27/*
28 2002-04-08: Added nForce support. (Csaba Halasz)
29 2002-10-03: Fixed nForce PnP I/O port. (Michael Steil)
30 2002-12-28: Rewritten into something that resembles a Linux driver (hch)
31 2003-11-29: Added back AMD8111 removed by the previous rewrite.
32 (Philip Pokorny)
33*/
34
35/*
36 Supports AMD756, AMD766, AMD768, AMD8111 and nVidia nForce
37 Note: we assume there can only be one device, with one SMBus interface.
38*/
39
40#include <linux/config.h>
41#include <linux/module.h>
42#include <linux/pci.h>
43#include <linux/kernel.h>
44#include <linux/delay.h>
45#include <linux/stddef.h>
46#include <linux/sched.h>
47#include <linux/ioport.h>
48#include <linux/i2c.h>
49#include <linux/init.h>
50#include <asm/io.h>
51
52/* AMD756 SMBus address offsets */
53#define SMB_ADDR_OFFSET 0xE0
54#define SMB_IOSIZE 16
55#define SMB_GLOBAL_STATUS (0x0 + amd756_ioport)
56#define SMB_GLOBAL_ENABLE (0x2 + amd756_ioport)
57#define SMB_HOST_ADDRESS (0x4 + amd756_ioport)
58#define SMB_HOST_DATA (0x6 + amd756_ioport)
59#define SMB_HOST_COMMAND (0x8 + amd756_ioport)
60#define SMB_HOST_BLOCK_DATA (0x9 + amd756_ioport)
61#define SMB_HAS_DATA (0xA + amd756_ioport)
62#define SMB_HAS_DEVICE_ADDRESS (0xC + amd756_ioport)
63#define SMB_HAS_HOST_ADDRESS (0xE + amd756_ioport)
64#define SMB_SNOOP_ADDRESS (0xF + amd756_ioport)
65
66/* PCI Address Constants */
67
68/* address of I/O space */
69#define SMBBA 0x058 /* mh */
70#define SMBBANFORCE 0x014
71
72/* general configuration */
73#define SMBGCFG 0x041 /* mh */
74
75/* silicon revision code */
76#define SMBREV 0x008
77
78/* Other settings */
79#define MAX_TIMEOUT 500
80
81/* AMD756 constants */
82#define AMD756_QUICK 0x00
83#define AMD756_BYTE 0x01
84#define AMD756_BYTE_DATA 0x02
85#define AMD756_WORD_DATA 0x03
86#define AMD756_PROCESS_CALL 0x04
87#define AMD756_BLOCK_DATA 0x05
88
89
90static unsigned short amd756_ioport = 0;
91
92/*
93 SMBUS event = I/O 28-29 bit 11
94 see E0 for the status bits and enabled in E2
95
96*/
97#define GS_ABRT_STS (1 << 0)
98#define GS_COL_STS (1 << 1)
99#define GS_PRERR_STS (1 << 2)
100#define GS_HST_STS (1 << 3)
101#define GS_HCYC_STS (1 << 4)
102#define GS_TO_STS (1 << 5)
103#define GS_SMB_STS (1 << 11)
104
105#define GS_CLEAR_STS (GS_ABRT_STS | GS_COL_STS | GS_PRERR_STS | \
106 GS_HCYC_STS | GS_TO_STS )
107
108#define GE_CYC_TYPE_MASK (7)
109#define GE_HOST_STC (1 << 3)
110#define GE_ABORT (1 << 5)
111
112
113static int amd756_transaction(struct i2c_adapter *adap)
114{
115 int temp;
116 int result = 0;
117 int timeout = 0;
118
119 dev_dbg(&adap->dev, "Transaction (pre): GS=%04x, GE=%04x, ADD=%04x, "
120 "DAT=%04x\n", inw_p(SMB_GLOBAL_STATUS),
121 inw_p(SMB_GLOBAL_ENABLE), inw_p(SMB_HOST_ADDRESS),
122 inb_p(SMB_HOST_DATA));
123
124 /* Make sure the SMBus host is ready to start transmitting */
125 if ((temp = inw_p(SMB_GLOBAL_STATUS)) & (GS_HST_STS | GS_SMB_STS)) {
126 dev_dbg(&adap->dev, "SMBus busy (%04x). Waiting...\n", temp);
127 do {
128 msleep(1);
129 temp = inw_p(SMB_GLOBAL_STATUS);
130 } while ((temp & (GS_HST_STS | GS_SMB_STS)) &&
131 (timeout++ < MAX_TIMEOUT));
132 /* If the SMBus is still busy, we give up */
133 if (timeout >= MAX_TIMEOUT) {
134 dev_dbg(&adap->dev, "Busy wait timeout (%04x)\n", temp);
135 goto abort;
136 }
137 timeout = 0;
138 }
139
140 /* start the transaction by setting the start bit */
141 outw_p(inw(SMB_GLOBAL_ENABLE) | GE_HOST_STC, SMB_GLOBAL_ENABLE);
142
143 /* We will always wait for a fraction of a second! */
144 do {
145 msleep(1);
146 temp = inw_p(SMB_GLOBAL_STATUS);
147 } while ((temp & GS_HST_STS) && (timeout++ < MAX_TIMEOUT));
148
149 /* If the SMBus is still busy, we give up */
150 if (timeout >= MAX_TIMEOUT) {
151 dev_dbg(&adap->dev, "Completion timeout!\n");
152 goto abort;
153 }
154
155 if (temp & GS_PRERR_STS) {
156 result = -1;
157 dev_dbg(&adap->dev, "SMBus Protocol error (no response)!\n");
158 }
159
160 if (temp & GS_COL_STS) {
161 result = -1;
162 dev_warn(&adap->dev, "SMBus collision!\n");
163 }
164
165 if (temp & GS_TO_STS) {
166 result = -1;
167 dev_dbg(&adap->dev, "SMBus protocol timeout!\n");
168 }
169
170 if (temp & GS_HCYC_STS)
171 dev_dbg(&adap->dev, "SMBus protocol success!\n");
172
173 outw_p(GS_CLEAR_STS, SMB_GLOBAL_STATUS);
174
175#ifdef DEBUG
176 if (((temp = inw_p(SMB_GLOBAL_STATUS)) & GS_CLEAR_STS) != 0x00) {
177 dev_dbg(&adap->dev,
178 "Failed reset at end of transaction (%04x)\n", temp);
179 }
180#endif
181
182 dev_dbg(&adap->dev,
183 "Transaction (post): GS=%04x, GE=%04x, ADD=%04x, DAT=%04x\n",
184 inw_p(SMB_GLOBAL_STATUS), inw_p(SMB_GLOBAL_ENABLE),
185 inw_p(SMB_HOST_ADDRESS), inb_p(SMB_HOST_DATA));
186
187 return result;
188
189 abort:
190 dev_warn(&adap->dev, "Sending abort\n");
191 outw_p(inw(SMB_GLOBAL_ENABLE) | GE_ABORT, SMB_GLOBAL_ENABLE);
192 msleep(100);
193 outw_p(GS_CLEAR_STS, SMB_GLOBAL_STATUS);
194 return -1;
195}
196
197/* Return -1 on error. */
198static s32 amd756_access(struct i2c_adapter * adap, u16 addr,
199 unsigned short flags, char read_write,
200 u8 command, int size, union i2c_smbus_data * data)
201{
202 int i, len;
203
204 /** TODO: Should I supporte the 10-bit transfers? */
205 switch (size) {
206 case I2C_SMBUS_PROC_CALL:
207 dev_dbg(&adap->dev, "I2C_SMBUS_PROC_CALL not supported!\n");
208 /* TODO: Well... It is supported, I'm just not sure what to do here... */
209 return -1;
210 case I2C_SMBUS_QUICK:
211 outw_p(((addr & 0x7f) << 1) | (read_write & 0x01),
212 SMB_HOST_ADDRESS);
213 size = AMD756_QUICK;
214 break;
215 case I2C_SMBUS_BYTE:
216 outw_p(((addr & 0x7f) << 1) | (read_write & 0x01),
217 SMB_HOST_ADDRESS);
218 if (read_write == I2C_SMBUS_WRITE)
219 outb_p(command, SMB_HOST_DATA);
220 size = AMD756_BYTE;
221 break;
222 case I2C_SMBUS_BYTE_DATA:
223 outw_p(((addr & 0x7f) << 1) | (read_write & 0x01),
224 SMB_HOST_ADDRESS);
225 outb_p(command, SMB_HOST_COMMAND);
226 if (read_write == I2C_SMBUS_WRITE)
227 outw_p(data->byte, SMB_HOST_DATA);
228 size = AMD756_BYTE_DATA;
229 break;
230 case I2C_SMBUS_WORD_DATA:
231 outw_p(((addr & 0x7f) << 1) | (read_write & 0x01),
232 SMB_HOST_ADDRESS);
233 outb_p(command, SMB_HOST_COMMAND);
234 if (read_write == I2C_SMBUS_WRITE)
235 outw_p(data->word, SMB_HOST_DATA); /* TODO: endian???? */
236 size = AMD756_WORD_DATA;
237 break;
238 case I2C_SMBUS_BLOCK_DATA:
239 outw_p(((addr & 0x7f) << 1) | (read_write & 0x01),
240 SMB_HOST_ADDRESS);
241 outb_p(command, SMB_HOST_COMMAND);
242 if (read_write == I2C_SMBUS_WRITE) {
243 len = data->block[0];
244 if (len < 0)
245 len = 0;
246 if (len > 32)
247 len = 32;
248 outw_p(len, SMB_HOST_DATA);
249 /* i = inw_p(SMBHSTCNT); Reset SMBBLKDAT */
250 for (i = 1; i <= len; i++)
251 outb_p(data->block[i],
252 SMB_HOST_BLOCK_DATA);
253 }
254 size = AMD756_BLOCK_DATA;
255 break;
256 }
257
258 /* How about enabling interrupts... */
259 outw_p(size & GE_CYC_TYPE_MASK, SMB_GLOBAL_ENABLE);
260
261 if (amd756_transaction(adap)) /* Error in transaction */
262 return -1;
263
264 if ((read_write == I2C_SMBUS_WRITE) || (size == AMD756_QUICK))
265 return 0;
266
267
268 switch (size) {
269 case AMD756_BYTE:
270 data->byte = inw_p(SMB_HOST_DATA);
271 break;
272 case AMD756_BYTE_DATA:
273 data->byte = inw_p(SMB_HOST_DATA);
274 break;
275 case AMD756_WORD_DATA:
276 data->word = inw_p(SMB_HOST_DATA); /* TODO: endian???? */
277 break;
278 case AMD756_BLOCK_DATA:
279 data->block[0] = inw_p(SMB_HOST_DATA) & 0x3f;
280 if(data->block[0] > 32)
281 data->block[0] = 32;
282 /* i = inw_p(SMBHSTCNT); Reset SMBBLKDAT */
283 for (i = 1; i <= data->block[0]; i++)
284 data->block[i] = inb_p(SMB_HOST_BLOCK_DATA);
285 break;
286 }
287
288 return 0;
289}
290
291static u32 amd756_func(struct i2c_adapter *adapter)
292{
293 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
294 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
295 I2C_FUNC_SMBUS_BLOCK_DATA | I2C_FUNC_SMBUS_PROC_CALL;
296}
297
298static struct i2c_algorithm smbus_algorithm = {
299 .name = "Non-I2C SMBus adapter",
300 .id = I2C_ALGO_SMBUS,
301 .smbus_xfer = amd756_access,
302 .functionality = amd756_func,
303};
304
305struct i2c_adapter amd756_smbus = {
306 .owner = THIS_MODULE,
307 .class = I2C_CLASS_HWMON,
308 .algo = &smbus_algorithm,
309 .name = "unset",
310};
311
312enum chiptype { AMD756, AMD766, AMD768, NFORCE, AMD8111 };
313static const char* chipname[] = {
314 "AMD756", "AMD766", "AMD768",
315 "nVidia nForce", "AMD8111",
316};
317
318static struct pci_device_id amd756_ids[] = {
319 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_VIPER_740B),
320 .driver_data = AMD756 },
321 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_VIPER_7413),
322 .driver_data = AMD766 },
323 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_OPUS_7443),
324 .driver_data = AMD768 },
325 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8111_SMBUS),
326 .driver_data = AMD8111 },
327 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_SMBUS),
328 .driver_data = NFORCE },
329 { 0, }
330};
331
332MODULE_DEVICE_TABLE (pci, amd756_ids);
333
334static int __devinit amd756_probe(struct pci_dev *pdev,
335 const struct pci_device_id *id)
336{
337 int nforce = (id->driver_data == NFORCE);
338 int error;
339 u8 temp;
340
341 if (amd756_ioport) {
342 dev_err(&pdev->dev, "Only one device supported "
343 "(you have a strange motherboard, btw)\n");
344 return -ENODEV;
345 }
346
347 if (nforce) {
348 if (PCI_FUNC(pdev->devfn) != 1)
349 return -ENODEV;
350
351 pci_read_config_word(pdev, SMBBANFORCE, &amd756_ioport);
352 amd756_ioport &= 0xfffc;
353 } else { /* amd */
354 if (PCI_FUNC(pdev->devfn) != 3)
355 return -ENODEV;
356
357 pci_read_config_byte(pdev, SMBGCFG, &temp);
358 if ((temp & 128) == 0) {
359 dev_err(&pdev->dev,
360 "Error: SMBus controller I/O not enabled!\n");
361 return -ENODEV;
362 }
363
364 /* Determine the address of the SMBus areas */
365 /* Technically it is a dword but... */
366 pci_read_config_word(pdev, SMBBA, &amd756_ioport);
367 amd756_ioport &= 0xff00;
368 amd756_ioport += SMB_ADDR_OFFSET;
369 }
370
371 if (!request_region(amd756_ioport, SMB_IOSIZE, "amd756-smbus")) {
372 dev_err(&pdev->dev, "SMB region 0x%x already in use!\n",
373 amd756_ioport);
374 return -ENODEV;
375 }
376
377 pci_read_config_byte(pdev, SMBREV, &temp);
378 dev_dbg(&pdev->dev, "SMBREV = 0x%X\n", temp);
379 dev_dbg(&pdev->dev, "AMD756_smba = 0x%X\n", amd756_ioport);
380
381 /* set up the driverfs linkage to our parent device */
382 amd756_smbus.dev.parent = &pdev->dev;
383
384 sprintf(amd756_smbus.name, "SMBus %s adapter at %04x",
385 chipname[id->driver_data], amd756_ioport);
386
387 error = i2c_add_adapter(&amd756_smbus);
388 if (error) {
389 dev_err(&pdev->dev,
390 "Adapter registration failed, module not inserted\n");
391 goto out_err;
392 }
393
394 return 0;
395
396 out_err:
397 release_region(amd756_ioport, SMB_IOSIZE);
398 return error;
399}
400
401static void __devexit amd756_remove(struct pci_dev *dev)
402{
403 i2c_del_adapter(&amd756_smbus);
404 release_region(amd756_ioport, SMB_IOSIZE);
405}
406
407static struct pci_driver amd756_driver = {
408 .name = "amd756_smbus",
409 .id_table = amd756_ids,
410 .probe = amd756_probe,
411 .remove = __devexit_p(amd756_remove),
412};
413
414static int __init amd756_init(void)
415{
416 return pci_register_driver(&amd756_driver);
417}
418
419static void __exit amd756_exit(void)
420{
421 pci_unregister_driver(&amd756_driver);
422}
423
424MODULE_AUTHOR("Merlin Hughes <merlin@merlin.org>");
425MODULE_DESCRIPTION("AMD756/766/768/8111 and nVidia nForce SMBus driver");
426MODULE_LICENSE("GPL");
427
428EXPORT_SYMBOL(amd756_smbus);
429
430module_init(amd756_init)
431module_exit(amd756_exit)
diff --git a/drivers/i2c/busses/i2c-amd8111.c b/drivers/i2c/busses/i2c-amd8111.c
new file mode 100644
index 000000000000..af22b401a38b
--- /dev/null
+++ b/drivers/i2c/busses/i2c-amd8111.c
@@ -0,0 +1,415 @@
1/*
2 * SMBus 2.0 driver for AMD-8111 IO-Hub.
3 *
4 * Copyright (c) 2002 Vojtech Pavlik
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation version 2.
9 */
10
11#include <linux/config.h>
12#include <linux/module.h>
13#include <linux/pci.h>
14#include <linux/kernel.h>
15#include <linux/stddef.h>
16#include <linux/sched.h>
17#include <linux/ioport.h>
18#include <linux/init.h>
19#include <linux/i2c.h>
20#include <linux/delay.h>
21#include <asm/io.h>
22
23MODULE_LICENSE("GPL");
24MODULE_AUTHOR ("Vojtech Pavlik <vojtech@suse.cz>");
25MODULE_DESCRIPTION("AMD8111 SMBus 2.0 driver");
26
27struct amd_smbus {
28 struct pci_dev *dev;
29 struct i2c_adapter adapter;
30 int base;
31 int size;
32};
33
34/*
35 * AMD PCI control registers definitions.
36 */
37
38#define AMD_PCI_MISC 0x48
39
40#define AMD_PCI_MISC_SCI 0x04 /* deliver SCI */
41#define AMD_PCI_MISC_INT 0x02 /* deliver PCI IRQ */
42#define AMD_PCI_MISC_SPEEDUP 0x01 /* 16x clock speedup */
43
44/*
45 * ACPI 2.0 chapter 13 PCI interface definitions.
46 */
47
48#define AMD_EC_DATA 0x00 /* data register */
49#define AMD_EC_SC 0x04 /* status of controller */
50#define AMD_EC_CMD 0x04 /* command register */
51#define AMD_EC_ICR 0x08 /* interrupt control register */
52
53#define AMD_EC_SC_SMI 0x04 /* smi event pending */
54#define AMD_EC_SC_SCI 0x02 /* sci event pending */
55#define AMD_EC_SC_BURST 0x01 /* burst mode enabled */
56#define AMD_EC_SC_CMD 0x08 /* byte in data reg is command */
57#define AMD_EC_SC_IBF 0x02 /* data ready for embedded controller */
58#define AMD_EC_SC_OBF 0x01 /* data ready for host */
59
60#define AMD_EC_CMD_RD 0x80 /* read EC */
61#define AMD_EC_CMD_WR 0x81 /* write EC */
62#define AMD_EC_CMD_BE 0x82 /* enable burst mode */
63#define AMD_EC_CMD_BD 0x83 /* disable burst mode */
64#define AMD_EC_CMD_QR 0x84 /* query EC */
65
66/*
67 * ACPI 2.0 chapter 13 access of registers of the EC
68 */
69
70static unsigned int amd_ec_wait_write(struct amd_smbus *smbus)
71{
72 int timeout = 500;
73
74 while (timeout-- && (inb(smbus->base + AMD_EC_SC) & AMD_EC_SC_IBF))
75 udelay(1);
76
77 if (!timeout) {
78 dev_warn(&smbus->dev->dev, "Timeout while waiting for IBF to clear\n");
79 return -1;
80 }
81
82 return 0;
83}
84
85static unsigned int amd_ec_wait_read(struct amd_smbus *smbus)
86{
87 int timeout = 500;
88
89 while (timeout-- && (~inb(smbus->base + AMD_EC_SC) & AMD_EC_SC_OBF))
90 udelay(1);
91
92 if (!timeout) {
93 dev_warn(&smbus->dev->dev, "Timeout while waiting for OBF to set\n");
94 return -1;
95 }
96
97 return 0;
98}
99
100static unsigned int amd_ec_read(struct amd_smbus *smbus, unsigned char address, unsigned char *data)
101{
102 if (amd_ec_wait_write(smbus))
103 return -1;
104 outb(AMD_EC_CMD_RD, smbus->base + AMD_EC_CMD);
105
106 if (amd_ec_wait_write(smbus))
107 return -1;
108 outb(address, smbus->base + AMD_EC_DATA);
109
110 if (amd_ec_wait_read(smbus))
111 return -1;
112 *data = inb(smbus->base + AMD_EC_DATA);
113
114 return 0;
115}
116
117static unsigned int amd_ec_write(struct amd_smbus *smbus, unsigned char address, unsigned char data)
118{
119 if (amd_ec_wait_write(smbus))
120 return -1;
121 outb(AMD_EC_CMD_WR, smbus->base + AMD_EC_CMD);
122
123 if (amd_ec_wait_write(smbus))
124 return -1;
125 outb(address, smbus->base + AMD_EC_DATA);
126
127 if (amd_ec_wait_write(smbus))
128 return -1;
129 outb(data, smbus->base + AMD_EC_DATA);
130
131 return 0;
132}
133
134/*
135 * ACPI 2.0 chapter 13 SMBus 2.0 EC register model
136 */
137
138#define AMD_SMB_PRTCL 0x00 /* protocol, PEC */
139#define AMD_SMB_STS 0x01 /* status */
140#define AMD_SMB_ADDR 0x02 /* address */
141#define AMD_SMB_CMD 0x03 /* command */
142#define AMD_SMB_DATA 0x04 /* 32 data registers */
143#define AMD_SMB_BCNT 0x24 /* number of data bytes */
144#define AMD_SMB_ALRM_A 0x25 /* alarm address */
145#define AMD_SMB_ALRM_D 0x26 /* 2 bytes alarm data */
146
147#define AMD_SMB_STS_DONE 0x80
148#define AMD_SMB_STS_ALRM 0x40
149#define AMD_SMB_STS_RES 0x20
150#define AMD_SMB_STS_STATUS 0x1f
151
152#define AMD_SMB_STATUS_OK 0x00
153#define AMD_SMB_STATUS_FAIL 0x07
154#define AMD_SMB_STATUS_DNAK 0x10
155#define AMD_SMB_STATUS_DERR 0x11
156#define AMD_SMB_STATUS_CMD_DENY 0x12
157#define AMD_SMB_STATUS_UNKNOWN 0x13
158#define AMD_SMB_STATUS_ACC_DENY 0x17
159#define AMD_SMB_STATUS_TIMEOUT 0x18
160#define AMD_SMB_STATUS_NOTSUP 0x19
161#define AMD_SMB_STATUS_BUSY 0x1A
162#define AMD_SMB_STATUS_PEC 0x1F
163
164#define AMD_SMB_PRTCL_WRITE 0x00
165#define AMD_SMB_PRTCL_READ 0x01
166#define AMD_SMB_PRTCL_QUICK 0x02
167#define AMD_SMB_PRTCL_BYTE 0x04
168#define AMD_SMB_PRTCL_BYTE_DATA 0x06
169#define AMD_SMB_PRTCL_WORD_DATA 0x08
170#define AMD_SMB_PRTCL_BLOCK_DATA 0x0a
171#define AMD_SMB_PRTCL_PROC_CALL 0x0c
172#define AMD_SMB_PRTCL_BLOCK_PROC_CALL 0x0d
173#define AMD_SMB_PRTCL_I2C_BLOCK_DATA 0x4a
174#define AMD_SMB_PRTCL_PEC 0x80
175
176
177static s32 amd8111_access(struct i2c_adapter * adap, u16 addr, unsigned short flags,
178 char read_write, u8 command, int size, union i2c_smbus_data * data)
179{
180 struct amd_smbus *smbus = adap->algo_data;
181 unsigned char protocol, len, pec, temp[2];
182 int i;
183
184 protocol = (read_write == I2C_SMBUS_READ) ? AMD_SMB_PRTCL_READ : AMD_SMB_PRTCL_WRITE;
185 pec = (flags & I2C_CLIENT_PEC) ? AMD_SMB_PRTCL_PEC : 0;
186
187 switch (size) {
188
189 case I2C_SMBUS_QUICK:
190 protocol |= AMD_SMB_PRTCL_QUICK;
191 read_write = I2C_SMBUS_WRITE;
192 break;
193
194 case I2C_SMBUS_BYTE:
195 if (read_write == I2C_SMBUS_WRITE)
196 amd_ec_write(smbus, AMD_SMB_CMD, command);
197 protocol |= AMD_SMB_PRTCL_BYTE;
198 break;
199
200 case I2C_SMBUS_BYTE_DATA:
201 amd_ec_write(smbus, AMD_SMB_CMD, command);
202 if (read_write == I2C_SMBUS_WRITE)
203 amd_ec_write(smbus, AMD_SMB_DATA, data->byte);
204 protocol |= AMD_SMB_PRTCL_BYTE_DATA;
205 break;
206
207 case I2C_SMBUS_WORD_DATA:
208 amd_ec_write(smbus, AMD_SMB_CMD, command);
209 if (read_write == I2C_SMBUS_WRITE) {
210 amd_ec_write(smbus, AMD_SMB_DATA, data->word);
211 amd_ec_write(smbus, AMD_SMB_DATA + 1, data->word >> 8);
212 }
213 protocol |= AMD_SMB_PRTCL_WORD_DATA | pec;
214 break;
215
216 case I2C_SMBUS_BLOCK_DATA:
217 amd_ec_write(smbus, AMD_SMB_CMD, command);
218 if (read_write == I2C_SMBUS_WRITE) {
219 len = min_t(u8, data->block[0], 32);
220 amd_ec_write(smbus, AMD_SMB_BCNT, len);
221 for (i = 0; i < len; i++)
222 amd_ec_write(smbus, AMD_SMB_DATA + i, data->block[i + 1]);
223 }
224 protocol |= AMD_SMB_PRTCL_BLOCK_DATA | pec;
225 break;
226
227 case I2C_SMBUS_I2C_BLOCK_DATA:
228 len = min_t(u8, data->block[0], 32);
229 amd_ec_write(smbus, AMD_SMB_CMD, command);
230 amd_ec_write(smbus, AMD_SMB_BCNT, len);
231 if (read_write == I2C_SMBUS_WRITE)
232 for (i = 0; i < len; i++)
233 amd_ec_write(smbus, AMD_SMB_DATA + i, data->block[i + 1]);
234 protocol |= AMD_SMB_PRTCL_I2C_BLOCK_DATA;
235 break;
236
237 case I2C_SMBUS_PROC_CALL:
238 amd_ec_write(smbus, AMD_SMB_CMD, command);
239 amd_ec_write(smbus, AMD_SMB_DATA, data->word);
240 amd_ec_write(smbus, AMD_SMB_DATA + 1, data->word >> 8);
241 protocol = AMD_SMB_PRTCL_PROC_CALL | pec;
242 read_write = I2C_SMBUS_READ;
243 break;
244
245 case I2C_SMBUS_BLOCK_PROC_CALL:
246 protocol |= pec;
247 len = min_t(u8, data->block[0], 31);
248 amd_ec_write(smbus, AMD_SMB_CMD, command);
249 amd_ec_write(smbus, AMD_SMB_BCNT, len);
250 for (i = 0; i < len; i++)
251 amd_ec_write(smbus, AMD_SMB_DATA + i, data->block[i + 1]);
252 protocol = AMD_SMB_PRTCL_BLOCK_PROC_CALL | pec;
253 read_write = I2C_SMBUS_READ;
254 break;
255
256 case I2C_SMBUS_WORD_DATA_PEC:
257 case I2C_SMBUS_BLOCK_DATA_PEC:
258 case I2C_SMBUS_PROC_CALL_PEC:
259 case I2C_SMBUS_BLOCK_PROC_CALL_PEC:
260 dev_warn(&adap->dev, "Unexpected software PEC transaction %d\n.", size);
261 return -1;
262
263 default:
264 dev_warn(&adap->dev, "Unsupported transaction %d\n", size);
265 return -1;
266 }
267
268 amd_ec_write(smbus, AMD_SMB_ADDR, addr << 1);
269 amd_ec_write(smbus, AMD_SMB_PRTCL, protocol);
270
271 amd_ec_read(smbus, AMD_SMB_STS, temp + 0);
272
273 if (~temp[0] & AMD_SMB_STS_DONE) {
274 udelay(500);
275 amd_ec_read(smbus, AMD_SMB_STS, temp + 0);
276 }
277
278 if (~temp[0] & AMD_SMB_STS_DONE) {
279 msleep(1);
280 amd_ec_read(smbus, AMD_SMB_STS, temp + 0);
281 }
282
283 if ((~temp[0] & AMD_SMB_STS_DONE) || (temp[0] & AMD_SMB_STS_STATUS))
284 return -1;
285
286 if (read_write == I2C_SMBUS_WRITE)
287 return 0;
288
289 switch (size) {
290
291 case I2C_SMBUS_BYTE:
292 case I2C_SMBUS_BYTE_DATA:
293 amd_ec_read(smbus, AMD_SMB_DATA, &data->byte);
294 break;
295
296 case I2C_SMBUS_WORD_DATA:
297 case I2C_SMBUS_PROC_CALL:
298 amd_ec_read(smbus, AMD_SMB_DATA, temp + 0);
299 amd_ec_read(smbus, AMD_SMB_DATA + 1, temp + 1);
300 data->word = (temp[1] << 8) | temp[0];
301 break;
302
303 case I2C_SMBUS_BLOCK_DATA:
304 case I2C_SMBUS_BLOCK_PROC_CALL:
305 amd_ec_read(smbus, AMD_SMB_BCNT, &len);
306 len = min_t(u8, len, 32);
307 case I2C_SMBUS_I2C_BLOCK_DATA:
308 for (i = 0; i < len; i++)
309 amd_ec_read(smbus, AMD_SMB_DATA + i, data->block + i + 1);
310 data->block[0] = len;
311 break;
312 }
313
314 return 0;
315}
316
317
318static u32 amd8111_func(struct i2c_adapter *adapter)
319{
320 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE | I2C_FUNC_SMBUS_BYTE_DATA |
321 I2C_FUNC_SMBUS_WORD_DATA | I2C_FUNC_SMBUS_BLOCK_DATA |
322 I2C_FUNC_SMBUS_PROC_CALL | I2C_FUNC_SMBUS_BLOCK_PROC_CALL |
323 I2C_FUNC_SMBUS_I2C_BLOCK | I2C_FUNC_SMBUS_HWPEC_CALC;
324}
325
326static struct i2c_algorithm smbus_algorithm = {
327 .name = "Non-I2C SMBus 2.0 adapter",
328 .id = I2C_ALGO_SMBUS,
329 .smbus_xfer = amd8111_access,
330 .functionality = amd8111_func,
331};
332
333
334static struct pci_device_id amd8111_ids[] = {
335 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8111_SMBUS2) },
336 { 0, }
337};
338
339MODULE_DEVICE_TABLE (pci, amd8111_ids);
340
341static int __devinit amd8111_probe(struct pci_dev *dev, const struct pci_device_id *id)
342{
343 struct amd_smbus *smbus;
344 int error = -ENODEV;
345
346 if (~pci_resource_flags(dev, 0) & IORESOURCE_IO)
347 return -ENODEV;
348
349 smbus = kmalloc(sizeof(struct amd_smbus), GFP_KERNEL);
350 if (!smbus)
351 return -ENOMEM;
352 memset(smbus, 0, sizeof(struct amd_smbus));
353
354 smbus->dev = dev;
355 smbus->base = pci_resource_start(dev, 0);
356 smbus->size = pci_resource_len(dev, 0);
357
358 if (!request_region(smbus->base, smbus->size, "amd8111 SMBus 2.0"))
359 goto out_kfree;
360
361 smbus->adapter.owner = THIS_MODULE;
362 snprintf(smbus->adapter.name, I2C_NAME_SIZE,
363 "SMBus2 AMD8111 adapter at %04x", smbus->base);
364 smbus->adapter.class = I2C_CLASS_HWMON;
365 smbus->adapter.algo = &smbus_algorithm;
366 smbus->adapter.algo_data = smbus;
367
368 /* set up the driverfs linkage to our parent device */
369 smbus->adapter.dev.parent = &dev->dev;
370
371 error = i2c_add_adapter(&smbus->adapter);
372 if (error)
373 goto out_release_region;
374
375 pci_write_config_dword(smbus->dev, AMD_PCI_MISC, 0);
376 pci_set_drvdata(dev, smbus);
377 return 0;
378
379 out_release_region:
380 release_region(smbus->base, smbus->size);
381 out_kfree:
382 kfree(smbus);
383 return -1;
384}
385
386
387static void __devexit amd8111_remove(struct pci_dev *dev)
388{
389 struct amd_smbus *smbus = pci_get_drvdata(dev);
390
391 i2c_del_adapter(&smbus->adapter);
392 release_region(smbus->base, smbus->size);
393 kfree(smbus);
394}
395
396static struct pci_driver amd8111_driver = {
397 .name = "amd8111_smbus2",
398 .id_table = amd8111_ids,
399 .probe = amd8111_probe,
400 .remove = __devexit_p(amd8111_remove),
401};
402
403static int __init i2c_amd8111_init(void)
404{
405 return pci_register_driver(&amd8111_driver);
406}
407
408
409static void __exit i2c_amd8111_exit(void)
410{
411 pci_unregister_driver(&amd8111_driver);
412}
413
414module_init(i2c_amd8111_init);
415module_exit(i2c_amd8111_exit);
diff --git a/drivers/i2c/busses/i2c-au1550.c b/drivers/i2c/busses/i2c-au1550.c
new file mode 100644
index 000000000000..75831a20b0bd
--- /dev/null
+++ b/drivers/i2c/busses/i2c-au1550.c
@@ -0,0 +1,435 @@
1/*
2 * i2c-au1550.c: SMBus (i2c) adapter for Alchemy PSC interface
3 * Copyright (C) 2004 Embedded Edge, LLC <dan@embeddededge.com>
4 *
5 * 2.6 port by Matt Porter <mporter@kernel.crashing.org>
6 *
7 * The documentation describes this as an SMBus controller, but it doesn't
8 * understand any of the SMBus protocol in hardware. It's really an I2C
9 * controller that could emulate most of the SMBus in software.
10 *
11 * This is just a skeleton adapter to use with the Au1550 PSC
12 * algorithm. It was developed for the Pb1550, but will work with
13 * any Au1550 board that has a similar PSC configuration.
14 *
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 */
29
30#include <linux/config.h>
31#include <linux/delay.h>
32#include <linux/kernel.h>
33#include <linux/module.h>
34#include <linux/init.h>
35#include <linux/errno.h>
36#include <linux/i2c.h>
37
38#include <asm/mach-au1x00/au1000.h>
39#include <asm/mach-pb1x00/pb1550.h>
40#include <asm/mach-au1x00/au1xxx_psc.h>
41
42#include "i2c-au1550.h"
43
44static int
45wait_xfer_done(struct i2c_au1550_data *adap)
46{
47 u32 stat;
48 int i;
49 volatile psc_smb_t *sp;
50
51 sp = (volatile psc_smb_t *)(adap->psc_base);
52
53 /* Wait for Tx FIFO Underflow.
54 */
55 for (i = 0; i < adap->xfer_timeout; i++) {
56 stat = sp->psc_smbevnt;
57 au_sync();
58 if ((stat & PSC_SMBEVNT_TU) != 0) {
59 /* Clear it. */
60 sp->psc_smbevnt = PSC_SMBEVNT_TU;
61 au_sync();
62 return 0;
63 }
64 udelay(1);
65 }
66
67 return -ETIMEDOUT;
68}
69
70static int
71wait_ack(struct i2c_au1550_data *adap)
72{
73 u32 stat;
74 volatile psc_smb_t *sp;
75
76 if (wait_xfer_done(adap))
77 return -ETIMEDOUT;
78
79 sp = (volatile psc_smb_t *)(adap->psc_base);
80
81 stat = sp->psc_smbevnt;
82 au_sync();
83
84 if ((stat & (PSC_SMBEVNT_DN | PSC_SMBEVNT_AN | PSC_SMBEVNT_AL)) != 0)
85 return -ETIMEDOUT;
86
87 return 0;
88}
89
90static int
91wait_master_done(struct i2c_au1550_data *adap)
92{
93 u32 stat;
94 int i;
95 volatile psc_smb_t *sp;
96
97 sp = (volatile psc_smb_t *)(adap->psc_base);
98
99 /* Wait for Master Done.
100 */
101 for (i = 0; i < adap->xfer_timeout; i++) {
102 stat = sp->psc_smbevnt;
103 au_sync();
104 if ((stat & PSC_SMBEVNT_MD) != 0)
105 return 0;
106 udelay(1);
107 }
108
109 return -ETIMEDOUT;
110}
111
112static int
113do_address(struct i2c_au1550_data *adap, unsigned int addr, int rd)
114{
115 volatile psc_smb_t *sp;
116 u32 stat;
117
118 sp = (volatile psc_smb_t *)(adap->psc_base);
119
120 /* Reset the FIFOs, clear events.
121 */
122 sp->psc_smbpcr = PSC_SMBPCR_DC;
123 sp->psc_smbevnt = PSC_SMBEVNT_ALLCLR;
124 au_sync();
125 do {
126 stat = sp->psc_smbpcr;
127 au_sync();
128 } while ((stat & PSC_SMBPCR_DC) != 0);
129
130 /* Write out the i2c chip address and specify operation
131 */
132 addr <<= 1;
133 if (rd)
134 addr |= 1;
135
136 /* Put byte into fifo, start up master.
137 */
138 sp->psc_smbtxrx = addr;
139 au_sync();
140 sp->psc_smbpcr = PSC_SMBPCR_MS;
141 au_sync();
142 if (wait_ack(adap))
143 return -EIO;
144 return 0;
145}
146
147static u32
148wait_for_rx_byte(struct i2c_au1550_data *adap, u32 *ret_data)
149{
150 int j;
151 u32 data, stat;
152 volatile psc_smb_t *sp;
153
154 if (wait_xfer_done(adap))
155 return -EIO;
156
157 sp = (volatile psc_smb_t *)(adap->psc_base);
158
159 j = adap->xfer_timeout * 100;
160 do {
161 j--;
162 if (j <= 0)
163 return -EIO;
164
165 stat = sp->psc_smbstat;
166 au_sync();
167 if ((stat & PSC_SMBSTAT_RE) == 0)
168 j = 0;
169 else
170 udelay(1);
171 } while (j > 0);
172 data = sp->psc_smbtxrx;
173 au_sync();
174 *ret_data = data;
175
176 return 0;
177}
178
179static int
180i2c_read(struct i2c_au1550_data *adap, unsigned char *buf,
181 unsigned int len)
182{
183 int i;
184 u32 data;
185 volatile psc_smb_t *sp;
186
187 if (len == 0)
188 return 0;
189
190 /* A read is performed by stuffing the transmit fifo with
191 * zero bytes for timing, waiting for bytes to appear in the
192 * receive fifo, then reading the bytes.
193 */
194
195 sp = (volatile psc_smb_t *)(adap->psc_base);
196
197 i = 0;
198 while (i < (len-1)) {
199 sp->psc_smbtxrx = 0;
200 au_sync();
201 if (wait_for_rx_byte(adap, &data))
202 return -EIO;
203
204 buf[i] = data;
205 i++;
206 }
207
208 /* The last byte has to indicate transfer done.
209 */
210 sp->psc_smbtxrx = PSC_SMBTXRX_STP;
211 au_sync();
212 if (wait_master_done(adap))
213 return -EIO;
214
215 data = sp->psc_smbtxrx;
216 au_sync();
217 buf[i] = data;
218 return 0;
219}
220
221static int
222i2c_write(struct i2c_au1550_data *adap, unsigned char *buf,
223 unsigned int len)
224{
225 int i;
226 u32 data;
227 volatile psc_smb_t *sp;
228
229 if (len == 0)
230 return 0;
231
232 sp = (volatile psc_smb_t *)(adap->psc_base);
233
234 i = 0;
235 while (i < (len-1)) {
236 data = buf[i];
237 sp->psc_smbtxrx = data;
238 au_sync();
239 if (wait_ack(adap))
240 return -EIO;
241 i++;
242 }
243
244 /* The last byte has to indicate transfer done.
245 */
246 data = buf[i];
247 data |= PSC_SMBTXRX_STP;
248 sp->psc_smbtxrx = data;
249 au_sync();
250 if (wait_master_done(adap))
251 return -EIO;
252 return 0;
253}
254
255static int
256au1550_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs, int num)
257{
258 struct i2c_au1550_data *adap = i2c_adap->algo_data;
259 struct i2c_msg *p;
260 int i, err = 0;
261
262 for (i = 0; !err && i < num; i++) {
263 p = &msgs[i];
264 err = do_address(adap, p->addr, p->flags & I2C_M_RD);
265 if (err || !p->len)
266 continue;
267 if (p->flags & I2C_M_RD)
268 err = i2c_read(adap, p->buf, p->len);
269 else
270 err = i2c_write(adap, p->buf, p->len);
271 }
272
273 /* Return the number of messages processed, or the error code.
274 */
275 if (err == 0)
276 err = num;
277 return err;
278}
279
280static u32
281au1550_func(struct i2c_adapter *adap)
282{
283 return I2C_FUNC_I2C;
284}
285
286static struct i2c_algorithm au1550_algo = {
287 .name = "Au1550 algorithm",
288 .id = I2C_ALGO_AU1550,
289 .master_xfer = au1550_xfer,
290 .functionality = au1550_func,
291};
292
293/*
294 * registering functions to load algorithms at runtime
295 * Prior to calling us, the 50MHz clock frequency and routing
296 * must have been set up for the PSC indicated by the adapter.
297 */
298int
299i2c_au1550_add_bus(struct i2c_adapter *i2c_adap)
300{
301 struct i2c_au1550_data *adap = i2c_adap->algo_data;
302 volatile psc_smb_t *sp;
303 u32 stat;
304
305 i2c_adap->algo = &au1550_algo;
306
307 /* Now, set up the PSC for SMBus PIO mode.
308 */
309 sp = (volatile psc_smb_t *)(adap->psc_base);
310 sp->psc_ctrl = PSC_CTRL_DISABLE;
311 au_sync();
312 sp->psc_sel = PSC_SEL_PS_SMBUSMODE;
313 sp->psc_smbcfg = 0;
314 au_sync();
315 sp->psc_ctrl = PSC_CTRL_ENABLE;
316 au_sync();
317 do {
318 stat = sp->psc_smbstat;
319 au_sync();
320 } while ((stat & PSC_SMBSTAT_SR) == 0);
321
322 sp->psc_smbcfg = (PSC_SMBCFG_RT_FIFO8 | PSC_SMBCFG_TT_FIFO8 |
323 PSC_SMBCFG_DD_DISABLE);
324
325 /* Divide by 8 to get a 6.25 MHz clock. The later protocol
326 * timings are based on this clock.
327 */
328 sp->psc_smbcfg |= PSC_SMBCFG_SET_DIV(PSC_SMBCFG_DIV8);
329 sp->psc_smbmsk = PSC_SMBMSK_ALLMASK;
330 au_sync();
331
332 /* Set the protocol timer values. See Table 71 in the
333 * Au1550 Data Book for standard timing values.
334 */
335 sp->psc_smbtmr = PSC_SMBTMR_SET_TH(0) | PSC_SMBTMR_SET_PS(15) | \
336 PSC_SMBTMR_SET_PU(15) | PSC_SMBTMR_SET_SH(15) | \
337 PSC_SMBTMR_SET_SU(15) | PSC_SMBTMR_SET_CL(15) | \
338 PSC_SMBTMR_SET_CH(15);
339 au_sync();
340
341 sp->psc_smbcfg |= PSC_SMBCFG_DE_ENABLE;
342 do {
343 stat = sp->psc_smbstat;
344 au_sync();
345 } while ((stat & PSC_SMBSTAT_DR) == 0);
346
347 return i2c_add_adapter(i2c_adap);
348}
349
350
351int
352i2c_au1550_del_bus(struct i2c_adapter *adap)
353{
354 return i2c_del_adapter(adap);
355}
356
357static int
358pb1550_reg(struct i2c_client *client)
359{
360 return 0;
361}
362
363static int
364pb1550_unreg(struct i2c_client *client)
365{
366 return 0;
367}
368
369static struct i2c_au1550_data pb1550_i2c_info = {
370 SMBUS_PSC_BASE, 200, 200
371};
372
373static struct i2c_adapter pb1550_board_adapter = {
374 name: "pb1550 adapter",
375 id: I2C_HW_AU1550_PSC,
376 algo: NULL,
377 algo_data: &pb1550_i2c_info,
378 client_register: pb1550_reg,
379 client_unregister: pb1550_unreg,
380};
381
382/* BIG hack to support the control interface on the Wolfson WM8731
383 * audio codec on the Pb1550 board. We get an address and two data
384 * bytes to write, create an i2c message, and send it across the
385 * i2c transfer function. We do this here because we have access to
386 * the i2c adapter structure.
387 */
388static struct i2c_msg wm_i2c_msg; /* We don't want this stuff on the stack */
389static u8 i2cbuf[2];
390
391int
392pb1550_wm_codec_write(u8 addr, u8 reg, u8 val)
393{
394 wm_i2c_msg.addr = addr;
395 wm_i2c_msg.flags = 0;
396 wm_i2c_msg.buf = i2cbuf;
397 wm_i2c_msg.len = 2;
398 i2cbuf[0] = reg;
399 i2cbuf[1] = val;
400
401 return pb1550_board_adapter.algo->master_xfer(&pb1550_board_adapter, &wm_i2c_msg, 1);
402}
403
404static int __init
405i2c_au1550_init(void)
406{
407 printk(KERN_INFO "Au1550 I2C: ");
408
409 /* This is where we would set up a 50MHz clock source
410 * and routing. On the Pb1550, the SMBus is PSC2, which
411 * uses a shared clock with USB. This has been already
412 * configured by Yamon as a 48MHz clock, close enough
413 * for our work.
414 */
415 if (i2c_au1550_add_bus(&pb1550_board_adapter) < 0) {
416 printk("failed to initialize.\n");
417 return -ENODEV;
418 }
419
420 printk("initialized.\n");
421 return 0;
422}
423
424static void __exit
425i2c_au1550_exit(void)
426{
427 i2c_au1550_del_bus(&pb1550_board_adapter);
428}
429
430MODULE_AUTHOR("Dan Malek, Embedded Edge, LLC.");
431MODULE_DESCRIPTION("SMBus adapter Alchemy pb1550");
432MODULE_LICENSE("GPL");
433
434module_init (i2c_au1550_init);
435module_exit (i2c_au1550_exit);
diff --git a/drivers/i2c/busses/i2c-au1550.h b/drivers/i2c/busses/i2c-au1550.h
new file mode 100644
index 000000000000..fce15d161ae7
--- /dev/null
+++ b/drivers/i2c/busses/i2c-au1550.h
@@ -0,0 +1,32 @@
1/*
2 * Copyright (C) 2004 Embedded Edge, LLC <dan@embeddededge.com>
3 * 2.6 port by Matt Porter <mporter@kernel.crashing.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20#ifndef I2C_AU1550_H
21#define I2C_AU1550_H
22
23struct i2c_au1550_data {
24 u32 psc_base;
25 int xfer_timeout;
26 int ack_timeout;
27};
28
29int i2c_au1550_add_bus(struct i2c_adapter *);
30int i2c_au1550_del_bus(struct i2c_adapter *);
31
32#endif /* I2C_AU1550_H */
diff --git a/drivers/i2c/busses/i2c-elektor.c b/drivers/i2c/busses/i2c-elektor.c
new file mode 100644
index 000000000000..0a7720000a0c
--- /dev/null
+++ b/drivers/i2c/busses/i2c-elektor.c
@@ -0,0 +1,295 @@
1/* ------------------------------------------------------------------------- */
2/* i2c-elektor.c i2c-hw access for PCF8584 style isa bus adaptes */
3/* ------------------------------------------------------------------------- */
4/* Copyright (C) 1995-97 Simon G. Vogl
5 1998-99 Hans Berglund
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/* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi> and even
23 Frodo Looijaard <frodol@dds.nl> */
24
25/* Partialy rewriten by Oleg I. Vdovikin for mmapped support of
26 for Alpha Processor Inc. UP-2000(+) boards */
27
28#include <linux/config.h>
29#include <linux/kernel.h>
30#include <linux/ioport.h>
31#include <linux/module.h>
32#include <linux/delay.h>
33#include <linux/slab.h>
34#include <linux/init.h>
35#include <linux/interrupt.h>
36#include <linux/pci.h>
37#include <linux/wait.h>
38
39#include <linux/i2c.h>
40#include <linux/i2c-algo-pcf.h>
41
42#include <asm/io.h>
43#include <asm/irq.h>
44
45#include "../algos/i2c-algo-pcf.h"
46
47#define DEFAULT_BASE 0x330
48
49static int base;
50static int irq;
51static int clock = 0x1c;
52static int own = 0x55;
53static int mmapped;
54
55/* vdovikin: removed static struct i2c_pcf_isa gpi; code -
56 this module in real supports only one device, due to missing arguments
57 in some functions, called from the algo-pcf module. Sometimes it's
58 need to be rewriten - but for now just remove this for simpler reading */
59
60static wait_queue_head_t pcf_wait;
61static int pcf_pending;
62static spinlock_t lock;
63
64/* ----- local functions ---------------------------------------------- */
65
66static void pcf_isa_setbyte(void *data, int ctl, int val)
67{
68 int address = ctl ? (base + 1) : base;
69
70 /* enable irq if any specified for serial operation */
71 if (ctl && irq && (val & I2C_PCF_ESO)) {
72 val |= I2C_PCF_ENI;
73 }
74
75 pr_debug("i2c-elektor: Write 0x%X 0x%02X\n", address, val & 255);
76
77 switch (mmapped) {
78 case 0: /* regular I/O */
79 outb(val, address);
80 break;
81 case 2: /* double mapped I/O needed for UP2000 board,
82 I don't know why this... */
83 writeb(val, (void *)address);
84 /* fall */
85 case 1: /* memory mapped I/O */
86 writeb(val, (void *)address);
87 break;
88 }
89}
90
91static int pcf_isa_getbyte(void *data, int ctl)
92{
93 int address = ctl ? (base + 1) : base;
94 int val = mmapped ? readb((void *)address) : inb(address);
95
96 pr_debug("i2c-elektor: Read 0x%X 0x%02X\n", address, val);
97
98 return (val);
99}
100
101static int pcf_isa_getown(void *data)
102{
103 return (own);
104}
105
106
107static int pcf_isa_getclock(void *data)
108{
109 return (clock);
110}
111
112static void pcf_isa_waitforpin(void) {
113 DEFINE_WAIT(wait);
114 int timeout = 2;
115 unsigned long flags;
116
117 if (irq > 0) {
118 spin_lock_irqsave(&lock, flags);
119 if (pcf_pending == 0) {
120 spin_unlock_irqrestore(&lock, flags);
121 prepare_to_wait(&pcf_wait, &wait, TASK_INTERRUPTIBLE);
122 if (schedule_timeout(timeout*HZ)) {
123 spin_lock_irqsave(&lock, flags);
124 if (pcf_pending == 1) {
125 pcf_pending = 0;
126 }
127 spin_unlock_irqrestore(&lock, flags);
128 }
129 finish_wait(&pcf_wait, &wait);
130 } else {
131 pcf_pending = 0;
132 spin_unlock_irqrestore(&lock, flags);
133 }
134 } else {
135 udelay(100);
136 }
137}
138
139
140static irqreturn_t pcf_isa_handler(int this_irq, void *dev_id, struct pt_regs *regs) {
141 spin_lock(&lock);
142 pcf_pending = 1;
143 spin_unlock(&lock);
144 wake_up_interruptible(&pcf_wait);
145 return IRQ_HANDLED;
146}
147
148
149static int pcf_isa_init(void)
150{
151 spin_lock_init(&lock);
152 if (!mmapped) {
153 if (!request_region(base, 2, "i2c (isa bus adapter)")) {
154 printk(KERN_ERR
155 "i2c-elektor: requested I/O region (0x%X:2) "
156 "is in use.\n", base);
157 return -ENODEV;
158 }
159 }
160 if (irq > 0) {
161 if (request_irq(irq, pcf_isa_handler, 0, "PCF8584", NULL) < 0) {
162 printk(KERN_ERR "i2c-elektor: Request irq%d failed\n", irq);
163 irq = 0;
164 } else
165 enable_irq(irq);
166 }
167 return 0;
168}
169
170/* ------------------------------------------------------------------------
171 * Encapsulate the above functions in the correct operations structure.
172 * This is only done when more than one hardware adapter is supported.
173 */
174static struct i2c_algo_pcf_data pcf_isa_data = {
175 .setpcf = pcf_isa_setbyte,
176 .getpcf = pcf_isa_getbyte,
177 .getown = pcf_isa_getown,
178 .getclock = pcf_isa_getclock,
179 .waitforpin = pcf_isa_waitforpin,
180 .udelay = 10,
181 .mdelay = 10,
182 .timeout = 100,
183};
184
185static struct i2c_adapter pcf_isa_ops = {
186 .owner = THIS_MODULE,
187 .class = I2C_CLASS_HWMON,
188 .id = I2C_HW_P_ELEK,
189 .algo_data = &pcf_isa_data,
190 .name = "PCF8584 ISA adapter",
191};
192
193static int __init i2c_pcfisa_init(void)
194{
195#ifdef __alpha__
196 /* check to see we have memory mapped PCF8584 connected to the
197 Cypress cy82c693 PCI-ISA bridge as on UP2000 board */
198 if (base == 0) {
199 struct pci_dev *cy693_dev;
200
201 cy693_dev = pci_get_device(PCI_VENDOR_ID_CONTAQ,
202 PCI_DEVICE_ID_CONTAQ_82C693, NULL);
203 if (cy693_dev) {
204 char config;
205 /* yeap, we've found cypress, let's check config */
206 if (!pci_read_config_byte(cy693_dev, 0x47, &config)) {
207
208 pr_debug("i2c-elektor: found cy82c693, config register 0x47 = 0x%02x.\n", config);
209
210 /* UP2000 board has this register set to 0xe1,
211 but the most significant bit as seems can be
212 reset during the proper initialisation
213 sequence if guys from API decides to do that
214 (so, we can even enable Tsunami Pchip
215 window for the upper 1 Gb) */
216
217 /* so just check for ROMCS at 0xe0000,
218 ROMCS enabled for writes
219 and external XD Bus buffer in use. */
220 if ((config & 0x7f) == 0x61) {
221 /* seems to be UP2000 like board */
222 base = 0xe0000;
223 /* I don't know why we need to
224 write twice */
225 mmapped = 2;
226 /* UP2000 drives ISA with
227 8.25 MHz (PCI/4) clock
228 (this can be read from cypress) */
229 clock = I2C_PCF_CLK | I2C_PCF_TRNS90;
230 printk(KERN_INFO "i2c-elektor: found API UP2000 like board, will probe PCF8584 later.\n");
231 }
232 }
233 pci_dev_put(cy693_dev);
234 }
235 }
236#endif
237
238 /* sanity checks for mmapped I/O */
239 if (mmapped && base < 0xc8000) {
240 printk(KERN_ERR "i2c-elektor: incorrect base address (0x%0X) specified for mmapped I/O.\n", base);
241 return -ENODEV;
242 }
243
244 printk(KERN_INFO "i2c-elektor: i2c pcf8584-isa adapter driver\n");
245
246 if (base == 0) {
247 base = DEFAULT_BASE;
248 }
249
250 init_waitqueue_head(&pcf_wait);
251 if (pcf_isa_init())
252 return -ENODEV;
253 if (i2c_pcf_add_bus(&pcf_isa_ops) < 0)
254 goto fail;
255
256 printk(KERN_ERR "i2c-elektor: found device at %#x.\n", base);
257
258 return 0;
259
260 fail:
261 if (irq > 0) {
262 disable_irq(irq);
263 free_irq(irq, NULL);
264 }
265
266 if (!mmapped)
267 release_region(base , 2);
268 return -ENODEV;
269}
270
271static void i2c_pcfisa_exit(void)
272{
273 i2c_pcf_del_bus(&pcf_isa_ops);
274
275 if (irq > 0) {
276 disable_irq(irq);
277 free_irq(irq, NULL);
278 }
279
280 if (!mmapped)
281 release_region(base , 2);
282}
283
284MODULE_AUTHOR("Hans Berglund <hb@spacetec.no>");
285MODULE_DESCRIPTION("I2C-Bus adapter routines for PCF8584 ISA bus adapter");
286MODULE_LICENSE("GPL");
287
288module_param(base, int, 0);
289module_param(irq, int, 0);
290module_param(clock, int, 0);
291module_param(own, int, 0);
292module_param(mmapped, int, 0);
293
294module_init(i2c_pcfisa_init);
295module_exit(i2c_pcfisa_exit);
diff --git a/drivers/i2c/busses/i2c-frodo.c b/drivers/i2c/busses/i2c-frodo.c
new file mode 100644
index 000000000000..e093829a0bf7
--- /dev/null
+++ b/drivers/i2c/busses/i2c-frodo.c
@@ -0,0 +1,86 @@
1
2/*
3 * linux/drivers/i2c/i2c-frodo.c
4 *
5 * Author: Abraham van der Merwe <abraham@2d3d.co.za>
6 *
7 * An I2C adapter driver for the 2d3D, Inc. StrongARM SA-1110
8 * Development board (Frodo).
9 *
10 * This source code is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * version 2 as published by the Free Software Foundation.
13 */
14
15#include <linux/config.h>
16#include <linux/module.h>
17#include <linux/kernel.h>
18#include <linux/init.h>
19#include <linux/delay.h>
20#include <linux/i2c.h>
21#include <linux/i2c-algo-bit.h>
22#include <asm/hardware.h>
23
24
25static void frodo_setsda (void *data,int state)
26{
27 if (state)
28 FRODO_CPLD_I2C |= FRODO_I2C_SDA_OUT;
29 else
30 FRODO_CPLD_I2C &= ~FRODO_I2C_SDA_OUT;
31}
32
33static void frodo_setscl (void *data,int state)
34{
35 if (state)
36 FRODO_CPLD_I2C |= FRODO_I2C_SCL_OUT;
37 else
38 FRODO_CPLD_I2C &= ~FRODO_I2C_SCL_OUT;
39}
40
41static int frodo_getsda (void *data)
42{
43 return ((FRODO_CPLD_I2C & FRODO_I2C_SDA_IN) != 0);
44}
45
46static int frodo_getscl (void *data)
47{
48 return ((FRODO_CPLD_I2C & FRODO_I2C_SCL_IN) != 0);
49}
50
51static struct i2c_algo_bit_data bit_frodo_data = {
52 .setsda = frodo_setsda,
53 .setscl = frodo_setscl,
54 .getsda = frodo_getsda,
55 .getscl = frodo_getscl,
56 .udelay = 80,
57 .mdelay = 80,
58 .timeout = HZ
59};
60
61static struct i2c_adapter frodo_ops = {
62 .owner = THIS_MODULE,
63 .id = I2C_HW_B_FRODO,
64 .algo_data = &bit_frodo_data,
65 .dev = {
66 .name = "Frodo adapter driver",
67 },
68};
69
70static int __init i2c_frodo_init (void)
71{
72 return i2c_bit_add_bus(&frodo_ops);
73}
74
75static void __exit i2c_frodo_exit (void)
76{
77 i2c_bit_del_bus(&frodo_ops);
78}
79
80MODULE_AUTHOR ("Abraham van der Merwe <abraham@2d3d.co.za>");
81MODULE_DESCRIPTION ("I2C-Bus adapter routines for Frodo");
82MODULE_LICENSE ("GPL");
83
84module_init (i2c_frodo_init);
85module_exit (i2c_frodo_exit);
86
diff --git a/drivers/i2c/busses/i2c-hydra.c b/drivers/i2c/busses/i2c-hydra.c
new file mode 100644
index 000000000000..e0cb3b0f92fa
--- /dev/null
+++ b/drivers/i2c/busses/i2c-hydra.c
@@ -0,0 +1,183 @@
1/*
2 i2c-hydra.c - Part of lm_sensors, Linux kernel modules
3 for hardware monitoring
4
5 i2c Support for the Apple `Hydra' Mac I/O
6
7 Copyright (c) 1999-2004 Geert Uytterhoeven <geert@linux-m68k.org>
8
9 Based on i2c Support for Via Technologies 82C586B South Bridge
10 Copyright (c) 1998, 1999 Kyösti Mälkki <kmalkki@cc.hut.fi>
11
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25*/
26
27#include <linux/kernel.h>
28#include <linux/module.h>
29#include <linux/pci.h>
30#include <linux/types.h>
31#include <linux/i2c.h>
32#include <linux/i2c-algo-bit.h>
33#include <linux/init.h>
34#include <asm/io.h>
35#include <asm/hydra.h>
36
37
38#define HYDRA_CPD_PD0 0x00000001 /* CachePD lines */
39#define HYDRA_CPD_PD1 0x00000002
40#define HYDRA_CPD_PD2 0x00000004
41#define HYDRA_CPD_PD3 0x00000008
42
43#define HYDRA_SCLK HYDRA_CPD_PD0
44#define HYDRA_SDAT HYDRA_CPD_PD1
45#define HYDRA_SCLK_OE 0x00000010
46#define HYDRA_SDAT_OE 0x00000020
47
48static inline void pdregw(void *data, u32 val)
49{
50 struct Hydra *hydra = (struct Hydra *)data;
51 writel(val, &hydra->CachePD);
52}
53
54static inline u32 pdregr(void *data)
55{
56 struct Hydra *hydra = (struct Hydra *)data;
57 return readl(&hydra->CachePD);
58}
59
60static void hydra_bit_setscl(void *data, int state)
61{
62 u32 val = pdregr(data);
63 if (state)
64 val &= ~HYDRA_SCLK_OE;
65 else {
66 val &= ~HYDRA_SCLK;
67 val |= HYDRA_SCLK_OE;
68 }
69 pdregw(data, val);
70}
71
72static void hydra_bit_setsda(void *data, int state)
73{
74 u32 val = pdregr(data);
75 if (state)
76 val &= ~HYDRA_SDAT_OE;
77 else {
78 val &= ~HYDRA_SDAT;
79 val |= HYDRA_SDAT_OE;
80 }
81 pdregw(data, val);
82}
83
84static int hydra_bit_getscl(void *data)
85{
86 return (pdregr(data) & HYDRA_SCLK) != 0;
87}
88
89static int hydra_bit_getsda(void *data)
90{
91 return (pdregr(data) & HYDRA_SDAT) != 0;
92}
93
94/* ------------------------------------------------------------------------ */
95
96static struct i2c_algo_bit_data hydra_bit_data = {
97 .setsda = hydra_bit_setsda,
98 .setscl = hydra_bit_setscl,
99 .getsda = hydra_bit_getsda,
100 .getscl = hydra_bit_getscl,
101 .udelay = 5,
102 .mdelay = 5,
103 .timeout = HZ
104};
105
106static struct i2c_adapter hydra_adap = {
107 .owner = THIS_MODULE,
108 .name = "Hydra i2c",
109 .id = I2C_HW_B_HYDRA,
110 .algo_data = &hydra_bit_data,
111};
112
113static struct pci_device_id hydra_ids[] = {
114 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_HYDRA) },
115 { 0, }
116};
117
118MODULE_DEVICE_TABLE (pci, hydra_ids);
119
120static int __devinit hydra_probe(struct pci_dev *dev,
121 const struct pci_device_id *id)
122{
123 unsigned long base = pci_resource_start(dev, 0);
124 int res;
125
126 if (!request_mem_region(base+offsetof(struct Hydra, CachePD), 4,
127 hydra_adap.name))
128 return -EBUSY;
129
130 hydra_bit_data.data = ioremap(base, pci_resource_len(dev, 0));
131 if (hydra_bit_data.data == NULL) {
132 release_mem_region(base+offsetof(struct Hydra, CachePD), 4);
133 return -ENODEV;
134 }
135
136 pdregw(hydra_bit_data.data, 0); /* clear SCLK_OE and SDAT_OE */
137 hydra_adap.dev.parent = &dev->dev;
138 res = i2c_bit_add_bus(&hydra_adap);
139 if (res < 0) {
140 iounmap(hydra_bit_data.data);
141 release_mem_region(base+offsetof(struct Hydra, CachePD), 4);
142 return res;
143 }
144 return 0;
145}
146
147static void __devexit hydra_remove(struct pci_dev *dev)
148{
149 pdregw(hydra_bit_data.data, 0); /* clear SCLK_OE and SDAT_OE */
150 i2c_bit_del_bus(&hydra_adap);
151 iounmap(hydra_bit_data.data);
152 release_mem_region(pci_resource_start(dev, 0)+
153 offsetof(struct Hydra, CachePD), 4);
154}
155
156
157static struct pci_driver hydra_driver = {
158 .name = "hydra_smbus",
159 .id_table = hydra_ids,
160 .probe = hydra_probe,
161 .remove = __devexit_p(hydra_remove),
162};
163
164static int __init i2c_hydra_init(void)
165{
166 return pci_register_driver(&hydra_driver);
167}
168
169
170static void __exit i2c_hydra_exit(void)
171{
172 pci_unregister_driver(&hydra_driver);
173}
174
175
176
177MODULE_AUTHOR("Geert Uytterhoeven <geert@linux-m68k.org>");
178MODULE_DESCRIPTION("i2c for Apple Hydra Mac I/O");
179MODULE_LICENSE("GPL");
180
181module_init(i2c_hydra_init);
182module_exit(i2c_hydra_exit);
183
diff --git a/drivers/i2c/busses/i2c-i801.c b/drivers/i2c/busses/i2c-i801.c
new file mode 100644
index 000000000000..6ec8b21965e2
--- /dev/null
+++ b/drivers/i2c/busses/i2c-i801.c
@@ -0,0 +1,613 @@
1/*
2 i801.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring
4 Copyright (c) 1998 - 2002 Frodo Looijaard <frodol@dds.nl>,
5 Philip Edelbrock <phil@netroedge.com>, and Mark D. Studebaker
6 <mdsxyz123@yahoo.com>
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; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21*/
22
23/*
24 SUPPORTED DEVICES PCI ID
25 82801AA 2413
26 82801AB 2423
27 82801BA 2443
28 82801CA/CAM 2483
29 82801DB 24C3 (HW PEC supported, 32 byte buffer not supported)
30 82801EB 24D3 (HW PEC supported, 32 byte buffer not supported)
31 6300ESB 25A4
32 ICH6 266A
33 ICH7 27DA
34 This driver supports several versions of Intel's I/O Controller Hubs (ICH).
35 For SMBus support, they are similar to the PIIX4 and are part
36 of Intel's '810' and other chipsets.
37 See the doc/busses/i2c-i801 file for details.
38 I2C Block Read and Process Call are not supported.
39*/
40
41/* Note: we assume there can only be one I801, with one SMBus interface */
42
43#include <linux/config.h>
44#include <linux/module.h>
45#include <linux/pci.h>
46#include <linux/kernel.h>
47#include <linux/stddef.h>
48#include <linux/delay.h>
49#include <linux/sched.h>
50#include <linux/ioport.h>
51#include <linux/init.h>
52#include <linux/i2c.h>
53#include <asm/io.h>
54
55#ifdef I2C_FUNC_SMBUS_BLOCK_DATA_PEC
56#define HAVE_PEC
57#endif
58
59/* I801 SMBus address offsets */
60#define SMBHSTSTS (0 + i801_smba)
61#define SMBHSTCNT (2 + i801_smba)
62#define SMBHSTCMD (3 + i801_smba)
63#define SMBHSTADD (4 + i801_smba)
64#define SMBHSTDAT0 (5 + i801_smba)
65#define SMBHSTDAT1 (6 + i801_smba)
66#define SMBBLKDAT (7 + i801_smba)
67#define SMBPEC (8 + i801_smba) /* ICH4 only */
68#define SMBAUXSTS (12 + i801_smba) /* ICH4 only */
69#define SMBAUXCTL (13 + i801_smba) /* ICH4 only */
70
71/* PCI Address Constants */
72#define SMBBA 0x020
73#define SMBHSTCFG 0x040
74#define SMBREV 0x008
75
76/* Host configuration bits for SMBHSTCFG */
77#define SMBHSTCFG_HST_EN 1
78#define SMBHSTCFG_SMB_SMI_EN 2
79#define SMBHSTCFG_I2C_EN 4
80
81/* Other settings */
82#define MAX_TIMEOUT 100
83#define ENABLE_INT9 0 /* set to 0x01 to enable - untested */
84
85/* I801 command constants */
86#define I801_QUICK 0x00
87#define I801_BYTE 0x04
88#define I801_BYTE_DATA 0x08
89#define I801_WORD_DATA 0x0C
90#define I801_PROC_CALL 0x10 /* later chips only, unimplemented */
91#define I801_BLOCK_DATA 0x14
92#define I801_I2C_BLOCK_DATA 0x18 /* unimplemented */
93#define I801_BLOCK_LAST 0x34
94#define I801_I2C_BLOCK_LAST 0x38 /* unimplemented */
95#define I801_START 0x40
96#define I801_PEC_EN 0x80 /* ICH4 only */
97
98/* insmod parameters */
99
100/* If force_addr is set to anything different from 0, we forcibly enable
101 the I801 at the given address. VERY DANGEROUS! */
102static u16 force_addr;
103module_param(force_addr, ushort, 0);
104MODULE_PARM_DESC(force_addr,
105 "Forcibly enable the I801 at the given address. "
106 "EXTREMELY DANGEROUS!");
107
108static int i801_transaction(void);
109static int i801_block_transaction(union i2c_smbus_data *data,
110 char read_write, int command);
111
112static unsigned short i801_smba;
113static struct pci_dev *I801_dev;
114static int isich4;
115
116static int i801_setup(struct pci_dev *dev)
117{
118 int error_return = 0;
119 unsigned char temp;
120
121 /* Note: we keep on searching until we have found 'function 3' */
122 if(PCI_FUNC(dev->devfn) != 3)
123 return -ENODEV;
124
125 I801_dev = dev;
126 if ((dev->device == PCI_DEVICE_ID_INTEL_82801DB_3) ||
127 (dev->device == PCI_DEVICE_ID_INTEL_82801EB_3) ||
128 (dev->device == PCI_DEVICE_ID_INTEL_ESB_4))
129 isich4 = 1;
130 else
131 isich4 = 0;
132
133 /* Determine the address of the SMBus areas */
134 if (force_addr) {
135 i801_smba = force_addr & 0xfff0;
136 } else {
137 pci_read_config_word(I801_dev, SMBBA, &i801_smba);
138 i801_smba &= 0xfff0;
139 if(i801_smba == 0) {
140 dev_err(&dev->dev, "SMB base address uninitialized"
141 "- upgrade BIOS or use force_addr=0xaddr\n");
142 return -ENODEV;
143 }
144 }
145
146 if (!request_region(i801_smba, (isich4 ? 16 : 8), "i801-smbus")) {
147 dev_err(&dev->dev, "I801_smb region 0x%x already in use!\n",
148 i801_smba);
149 error_return = -EBUSY;
150 goto END;
151 }
152
153 pci_read_config_byte(I801_dev, SMBHSTCFG, &temp);
154 temp &= ~SMBHSTCFG_I2C_EN; /* SMBus timing */
155 pci_write_config_byte(I801_dev, SMBHSTCFG, temp);
156
157 /* If force_addr is set, we program the new address here. Just to make
158 sure, we disable the device first. */
159 if (force_addr) {
160 pci_write_config_byte(I801_dev, SMBHSTCFG, temp & 0xfe);
161 pci_write_config_word(I801_dev, SMBBA, i801_smba);
162 pci_write_config_byte(I801_dev, SMBHSTCFG, temp | 0x01);
163 dev_warn(&dev->dev, "WARNING: I801 SMBus interface set to "
164 "new address %04x!\n", i801_smba);
165 } else if ((temp & 1) == 0) {
166 pci_write_config_byte(I801_dev, SMBHSTCFG, temp | 1);
167 dev_warn(&dev->dev, "enabling SMBus device\n");
168 }
169
170 if (temp & 0x02)
171 dev_dbg(&dev->dev, "I801 using Interrupt SMI# for SMBus.\n");
172 else
173 dev_dbg(&dev->dev, "I801 using PCI Interrupt for SMBus.\n");
174
175 pci_read_config_byte(I801_dev, SMBREV, &temp);
176 dev_dbg(&dev->dev, "SMBREV = 0x%X\n", temp);
177 dev_dbg(&dev->dev, "I801_smba = 0x%X\n", i801_smba);
178
179END:
180 return error_return;
181}
182
183static int i801_transaction(void)
184{
185 int temp;
186 int result = 0;
187 int timeout = 0;
188
189 dev_dbg(&I801_dev->dev, "Transaction (pre): CNT=%02x, CMD=%02x,"
190 "ADD=%02x, DAT0=%02x, DAT1=%02x\n", inb_p(SMBHSTCNT),
191 inb_p(SMBHSTCMD), inb_p(SMBHSTADD), inb_p(SMBHSTDAT0),
192 inb_p(SMBHSTDAT1));
193
194 /* Make sure the SMBus host is ready to start transmitting */
195 /* 0x1f = Failed, Bus_Err, Dev_Err, Intr, Host_Busy */
196 if ((temp = (0x1f & inb_p(SMBHSTSTS))) != 0x00) {
197 dev_dbg(&I801_dev->dev, "SMBus busy (%02x). Resetting... \n",
198 temp);
199 outb_p(temp, SMBHSTSTS);
200 if ((temp = (0x1f & inb_p(SMBHSTSTS))) != 0x00) {
201 dev_dbg(&I801_dev->dev, "Failed! (%02x)\n", temp);
202 return -1;
203 } else {
204 dev_dbg(&I801_dev->dev, "Successfull!\n");
205 }
206 }
207
208 outb_p(inb(SMBHSTCNT) | I801_START, SMBHSTCNT);
209
210 /* We will always wait for a fraction of a second! */
211 do {
212 msleep(1);
213 temp = inb_p(SMBHSTSTS);
214 } while ((temp & 0x01) && (timeout++ < MAX_TIMEOUT));
215
216 /* If the SMBus is still busy, we give up */
217 if (timeout >= MAX_TIMEOUT) {
218 dev_dbg(&I801_dev->dev, "SMBus Timeout!\n");
219 result = -1;
220 }
221
222 if (temp & 0x10) {
223 result = -1;
224 dev_dbg(&I801_dev->dev, "Error: Failed bus transaction\n");
225 }
226
227 if (temp & 0x08) {
228 result = -1;
229 dev_err(&I801_dev->dev, "Bus collision! SMBus may be locked "
230 "until next hard reset. (sorry!)\n");
231 /* Clock stops and slave is stuck in mid-transmission */
232 }
233
234 if (temp & 0x04) {
235 result = -1;
236 dev_dbg(&I801_dev->dev, "Error: no response!\n");
237 }
238
239 if ((inb_p(SMBHSTSTS) & 0x1f) != 0x00)
240 outb_p(inb(SMBHSTSTS), SMBHSTSTS);
241
242 if ((temp = (0x1f & inb_p(SMBHSTSTS))) != 0x00) {
243 dev_dbg(&I801_dev->dev, "Failed reset at end of transaction"
244 "(%02x)\n", temp);
245 }
246 dev_dbg(&I801_dev->dev, "Transaction (post): CNT=%02x, CMD=%02x, "
247 "ADD=%02x, DAT0=%02x, DAT1=%02x\n", inb_p(SMBHSTCNT),
248 inb_p(SMBHSTCMD), inb_p(SMBHSTADD), inb_p(SMBHSTDAT0),
249 inb_p(SMBHSTDAT1));
250 return result;
251}
252
253/* All-inclusive block transaction function */
254static int i801_block_transaction(union i2c_smbus_data *data, char read_write,
255 int command)
256{
257 int i, len;
258 int smbcmd;
259 int temp;
260 int result = 0;
261 int timeout;
262 unsigned char hostc, errmask;
263
264 if (command == I2C_SMBUS_I2C_BLOCK_DATA) {
265 if (read_write == I2C_SMBUS_WRITE) {
266 /* set I2C_EN bit in configuration register */
267 pci_read_config_byte(I801_dev, SMBHSTCFG, &hostc);
268 pci_write_config_byte(I801_dev, SMBHSTCFG,
269 hostc | SMBHSTCFG_I2C_EN);
270 } else {
271 dev_err(&I801_dev->dev,
272 "I2C_SMBUS_I2C_BLOCK_READ not DB!\n");
273 return -1;
274 }
275 }
276
277 if (read_write == I2C_SMBUS_WRITE) {
278 len = data->block[0];
279 if (len < 1)
280 len = 1;
281 if (len > 32)
282 len = 32;
283 outb_p(len, SMBHSTDAT0);
284 outb_p(data->block[1], SMBBLKDAT);
285 } else {
286 len = 32; /* max for reads */
287 }
288
289 if(isich4 && command != I2C_SMBUS_I2C_BLOCK_DATA) {
290 /* set 32 byte buffer */
291 }
292
293 for (i = 1; i <= len; i++) {
294 if (i == len && read_write == I2C_SMBUS_READ)
295 smbcmd = I801_BLOCK_LAST;
296 else
297 smbcmd = I801_BLOCK_DATA;
298 outb_p(smbcmd | ENABLE_INT9, SMBHSTCNT);
299
300 dev_dbg(&I801_dev->dev, "Block (pre %d): CNT=%02x, CMD=%02x, "
301 "ADD=%02x, DAT0=%02x, BLKDAT=%02x\n", i,
302 inb_p(SMBHSTCNT), inb_p(SMBHSTCMD), inb_p(SMBHSTADD),
303 inb_p(SMBHSTDAT0), inb_p(SMBBLKDAT));
304
305 /* Make sure the SMBus host is ready to start transmitting */
306 temp = inb_p(SMBHSTSTS);
307 if (i == 1) {
308 /* Erronenous conditions before transaction:
309 * Byte_Done, Failed, Bus_Err, Dev_Err, Intr, Host_Busy */
310 errmask=0x9f;
311 } else {
312 /* Erronenous conditions during transaction:
313 * Failed, Bus_Err, Dev_Err, Intr */
314 errmask=0x1e;
315 }
316 if (temp & errmask) {
317 dev_dbg(&I801_dev->dev, "SMBus busy (%02x). "
318 "Resetting... \n", temp);
319 outb_p(temp, SMBHSTSTS);
320 if (((temp = inb_p(SMBHSTSTS)) & errmask) != 0x00) {
321 dev_err(&I801_dev->dev,
322 "Reset failed! (%02x)\n", temp);
323 result = -1;
324 goto END;
325 }
326 if (i != 1) {
327 /* if die in middle of block transaction, fail */
328 result = -1;
329 goto END;
330 }
331 }
332
333 if (i == 1)
334 outb_p(inb(SMBHSTCNT) | I801_START, SMBHSTCNT);
335
336 /* We will always wait for a fraction of a second! */
337 timeout = 0;
338 do {
339 temp = inb_p(SMBHSTSTS);
340 msleep(1);
341 }
342 while ((!(temp & 0x80))
343 && (timeout++ < MAX_TIMEOUT));
344
345 /* If the SMBus is still busy, we give up */
346 if (timeout >= MAX_TIMEOUT) {
347 result = -1;
348 dev_dbg(&I801_dev->dev, "SMBus Timeout!\n");
349 }
350
351 if (temp & 0x10) {
352 result = -1;
353 dev_dbg(&I801_dev->dev,
354 "Error: Failed bus transaction\n");
355 } else if (temp & 0x08) {
356 result = -1;
357 dev_err(&I801_dev->dev, "Bus collision!\n");
358 } else if (temp & 0x04) {
359 result = -1;
360 dev_dbg(&I801_dev->dev, "Error: no response!\n");
361 }
362
363 if (i == 1 && read_write == I2C_SMBUS_READ) {
364 len = inb_p(SMBHSTDAT0);
365 if (len < 1)
366 len = 1;
367 if (len > 32)
368 len = 32;
369 data->block[0] = len;
370 }
371
372 /* Retrieve/store value in SMBBLKDAT */
373 if (read_write == I2C_SMBUS_READ)
374 data->block[i] = inb_p(SMBBLKDAT);
375 if (read_write == I2C_SMBUS_WRITE && i+1 <= len)
376 outb_p(data->block[i+1], SMBBLKDAT);
377 if ((temp & 0x9e) != 0x00)
378 outb_p(temp, SMBHSTSTS); /* signals SMBBLKDAT ready */
379
380 if ((temp = (0x1e & inb_p(SMBHSTSTS))) != 0x00) {
381 dev_dbg(&I801_dev->dev,
382 "Bad status (%02x) at end of transaction\n",
383 temp);
384 }
385 dev_dbg(&I801_dev->dev, "Block (post %d): CNT=%02x, CMD=%02x, "
386 "ADD=%02x, DAT0=%02x, BLKDAT=%02x\n", i,
387 inb_p(SMBHSTCNT), inb_p(SMBHSTCMD), inb_p(SMBHSTADD),
388 inb_p(SMBHSTDAT0), inb_p(SMBBLKDAT));
389
390 if (result < 0)
391 goto END;
392 }
393
394#ifdef HAVE_PEC
395 if(isich4 && command == I2C_SMBUS_BLOCK_DATA_PEC) {
396 /* wait for INTR bit as advised by Intel */
397 timeout = 0;
398 do {
399 temp = inb_p(SMBHSTSTS);
400 msleep(1);
401 } while ((!(temp & 0x02))
402 && (timeout++ < MAX_TIMEOUT));
403
404 if (timeout >= MAX_TIMEOUT) {
405 dev_dbg(&I801_dev->dev, "PEC Timeout!\n");
406 }
407 outb_p(temp, SMBHSTSTS);
408 }
409#endif
410 result = 0;
411END:
412 if (command == I2C_SMBUS_I2C_BLOCK_DATA) {
413 /* restore saved configuration register value */
414 pci_write_config_byte(I801_dev, SMBHSTCFG, hostc);
415 }
416 return result;
417}
418
419/* Return -1 on error. */
420static s32 i801_access(struct i2c_adapter * adap, u16 addr,
421 unsigned short flags, char read_write, u8 command,
422 int size, union i2c_smbus_data * data)
423{
424 int hwpec = 0;
425 int block = 0;
426 int ret, xact = 0;
427
428#ifdef HAVE_PEC
429 if(isich4)
430 hwpec = (flags & I2C_CLIENT_PEC) != 0;
431#endif
432
433 switch (size) {
434 case I2C_SMBUS_QUICK:
435 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
436 SMBHSTADD);
437 xact = I801_QUICK;
438 break;
439 case I2C_SMBUS_BYTE:
440 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
441 SMBHSTADD);
442 if (read_write == I2C_SMBUS_WRITE)
443 outb_p(command, SMBHSTCMD);
444 xact = I801_BYTE;
445 break;
446 case I2C_SMBUS_BYTE_DATA:
447 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
448 SMBHSTADD);
449 outb_p(command, SMBHSTCMD);
450 if (read_write == I2C_SMBUS_WRITE)
451 outb_p(data->byte, SMBHSTDAT0);
452 xact = I801_BYTE_DATA;
453 break;
454 case I2C_SMBUS_WORD_DATA:
455 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
456 SMBHSTADD);
457 outb_p(command, SMBHSTCMD);
458 if (read_write == I2C_SMBUS_WRITE) {
459 outb_p(data->word & 0xff, SMBHSTDAT0);
460 outb_p((data->word & 0xff00) >> 8, SMBHSTDAT1);
461 }
462 xact = I801_WORD_DATA;
463 break;
464 case I2C_SMBUS_BLOCK_DATA:
465 case I2C_SMBUS_I2C_BLOCK_DATA:
466#ifdef HAVE_PEC
467 case I2C_SMBUS_BLOCK_DATA_PEC:
468 if(hwpec && size == I2C_SMBUS_BLOCK_DATA)
469 size = I2C_SMBUS_BLOCK_DATA_PEC;
470#endif
471 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
472 SMBHSTADD);
473 outb_p(command, SMBHSTCMD);
474 block = 1;
475 break;
476 case I2C_SMBUS_PROC_CALL:
477 default:
478 dev_err(&I801_dev->dev, "Unsupported transaction %d\n", size);
479 return -1;
480 }
481
482#ifdef HAVE_PEC
483 if(isich4 && hwpec) {
484 if(size != I2C_SMBUS_QUICK &&
485 size != I2C_SMBUS_I2C_BLOCK_DATA)
486 outb_p(1, SMBAUXCTL); /* enable HW PEC */
487 }
488#endif
489 if(block)
490 ret = i801_block_transaction(data, read_write, size);
491 else {
492 outb_p(xact | ENABLE_INT9, SMBHSTCNT);
493 ret = i801_transaction();
494 }
495
496#ifdef HAVE_PEC
497 if(isich4 && hwpec) {
498 if(size != I2C_SMBUS_QUICK &&
499 size != I2C_SMBUS_I2C_BLOCK_DATA)
500 outb_p(0, SMBAUXCTL);
501 }
502#endif
503
504 if(block)
505 return ret;
506 if(ret)
507 return -1;
508 if ((read_write == I2C_SMBUS_WRITE) || (xact == I801_QUICK))
509 return 0;
510
511 switch (xact & 0x7f) {
512 case I801_BYTE: /* Result put in SMBHSTDAT0 */
513 case I801_BYTE_DATA:
514 data->byte = inb_p(SMBHSTDAT0);
515 break;
516 case I801_WORD_DATA:
517 data->word = inb_p(SMBHSTDAT0) + (inb_p(SMBHSTDAT1) << 8);
518 break;
519 }
520 return 0;
521}
522
523
524static u32 i801_func(struct i2c_adapter *adapter)
525{
526 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
527 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
528 I2C_FUNC_SMBUS_BLOCK_DATA | I2C_FUNC_SMBUS_WRITE_I2C_BLOCK
529#ifdef HAVE_PEC
530 | (isich4 ? I2C_FUNC_SMBUS_BLOCK_DATA_PEC |
531 I2C_FUNC_SMBUS_HWPEC_CALC
532 : 0)
533#endif
534 ;
535}
536
537static struct i2c_algorithm smbus_algorithm = {
538 .name = "Non-I2C SMBus adapter",
539 .id = I2C_ALGO_SMBUS,
540 .smbus_xfer = i801_access,
541 .functionality = i801_func,
542};
543
544static struct i2c_adapter i801_adapter = {
545 .owner = THIS_MODULE,
546 .class = I2C_CLASS_HWMON,
547 .algo = &smbus_algorithm,
548 .name = "unset",
549};
550
551static struct pci_device_id i801_ids[] = {
552 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_3) },
553 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_3) },
554 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_2) },
555 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801CA_3) },
556 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801DB_3) },
557 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801EB_3) },
558 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ESB_4) },
559 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH6_16) },
560 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH7_17) },
561 { 0, }
562};
563
564MODULE_DEVICE_TABLE (pci, i801_ids);
565
566static int __devinit i801_probe(struct pci_dev *dev, const struct pci_device_id *id)
567{
568
569 if (i801_setup(dev)) {
570 dev_warn(&dev->dev,
571 "I801 not detected, module not inserted.\n");
572 return -ENODEV;
573 }
574
575 /* set up the driverfs linkage to our parent device */
576 i801_adapter.dev.parent = &dev->dev;
577
578 snprintf(i801_adapter.name, I2C_NAME_SIZE,
579 "SMBus I801 adapter at %04x", i801_smba);
580 return i2c_add_adapter(&i801_adapter);
581}
582
583static void __devexit i801_remove(struct pci_dev *dev)
584{
585 i2c_del_adapter(&i801_adapter);
586 release_region(i801_smba, (isich4 ? 16 : 8));
587}
588
589static struct pci_driver i801_driver = {
590 .name = "i801_smbus",
591 .id_table = i801_ids,
592 .probe = i801_probe,
593 .remove = __devexit_p(i801_remove),
594};
595
596static int __init i2c_i801_init(void)
597{
598 return pci_register_driver(&i801_driver);
599}
600
601static void __exit i2c_i801_exit(void)
602{
603 pci_unregister_driver(&i801_driver);
604}
605
606MODULE_AUTHOR ("Frodo Looijaard <frodol@dds.nl>, "
607 "Philip Edelbrock <phil@netroedge.com>, "
608 "and Mark D. Studebaker <mdsxyz123@yahoo.com>");
609MODULE_DESCRIPTION("I801 SMBus driver");
610MODULE_LICENSE("GPL");
611
612module_init(i2c_i801_init);
613module_exit(i2c_i801_exit);
diff --git a/drivers/i2c/busses/i2c-i810.c b/drivers/i2c/busses/i2c-i810.c
new file mode 100644
index 000000000000..ef358bd9c3da
--- /dev/null
+++ b/drivers/i2c/busses/i2c-i810.c
@@ -0,0 +1,260 @@
1/*
2 i2c-i810.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring
4 Copyright (c) 1998, 1999, 2000 Frodo Looijaard <frodol@dds.nl>,
5 Philip Edelbrock <phil@netroedge.com>,
6 Ralph Metzler <rjkm@thp.uni-koeln.de>, and
7 Mark D. Studebaker <mdsxyz123@yahoo.com>
8
9 Based on code written by Ralph Metzler <rjkm@thp.uni-koeln.de> and
10 Simon Vogl
11
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25*/
26/*
27 This interfaces to the I810/I815 to provide access to
28 the DDC Bus and the I2C Bus.
29
30 SUPPORTED DEVICES PCI ID
31 i810AA 7121
32 i810AB 7123
33 i810E 7125
34 i815 1132
35*/
36
37#include <linux/config.h>
38#include <linux/kernel.h>
39#include <linux/module.h>
40#include <linux/init.h>
41#include <linux/pci.h>
42#include <linux/i2c.h>
43#include <linux/i2c-algo-bit.h>
44#include <asm/io.h>
45
46/* GPIO register locations */
47#define I810_IOCONTROL_OFFSET 0x5000
48#define I810_HVSYNC 0x00 /* not used */
49#define I810_GPIOA 0x10
50#define I810_GPIOB 0x14
51
52/* bit locations in the registers */
53#define SCL_DIR_MASK 0x0001
54#define SCL_DIR 0x0002
55#define SCL_VAL_MASK 0x0004
56#define SCL_VAL_OUT 0x0008
57#define SCL_VAL_IN 0x0010
58#define SDA_DIR_MASK 0x0100
59#define SDA_DIR 0x0200
60#define SDA_VAL_MASK 0x0400
61#define SDA_VAL_OUT 0x0800
62#define SDA_VAL_IN 0x1000
63
64/* initialization states */
65#define INIT1 0x1
66#define INIT2 0x2
67#define INIT3 0x4
68
69/* delays */
70#define CYCLE_DELAY 10
71#define TIMEOUT (HZ / 2)
72
73static void __iomem *ioaddr;
74
75/* The i810 GPIO registers have individual masks for each bit
76 so we never have to read before writing. Nice. */
77
78static void bit_i810i2c_setscl(void *data, int val)
79{
80 writel((val ? SCL_VAL_OUT : 0) | SCL_DIR | SCL_DIR_MASK | SCL_VAL_MASK,
81 ioaddr + I810_GPIOB);
82 readl(ioaddr + I810_GPIOB); /* flush posted write */
83}
84
85static void bit_i810i2c_setsda(void *data, int val)
86{
87 writel((val ? SDA_VAL_OUT : 0) | SDA_DIR | SDA_DIR_MASK | SDA_VAL_MASK,
88 ioaddr + I810_GPIOB);
89 readl(ioaddr + I810_GPIOB); /* flush posted write */
90}
91
92/* The GPIO pins are open drain, so the pins could always remain outputs.
93 However, some chip versions don't latch the inputs unless they
94 are set as inputs.
95 We rely on the i2c-algo-bit routines to set the pins high before
96 reading the input from other chips. Following guidance in the 815
97 prog. ref. guide, we do a "dummy write" of 0 to the register before
98 reading which forces the input value to be latched. We presume this
99 applies to the 810 as well; shouldn't hurt anyway. This is necessary to get
100 i2c_algo_bit bit_test=1 to pass. */
101
102static int bit_i810i2c_getscl(void *data)
103{
104 writel(SCL_DIR_MASK, ioaddr + I810_GPIOB);
105 writel(0, ioaddr + I810_GPIOB);
106 return (0 != (readl(ioaddr + I810_GPIOB) & SCL_VAL_IN));
107}
108
109static int bit_i810i2c_getsda(void *data)
110{
111 writel(SDA_DIR_MASK, ioaddr + I810_GPIOB);
112 writel(0, ioaddr + I810_GPIOB);
113 return (0 != (readl(ioaddr + I810_GPIOB) & SDA_VAL_IN));
114}
115
116static void bit_i810ddc_setscl(void *data, int val)
117{
118 writel((val ? SCL_VAL_OUT : 0) | SCL_DIR | SCL_DIR_MASK | SCL_VAL_MASK,
119 ioaddr + I810_GPIOA);
120 readl(ioaddr + I810_GPIOA); /* flush posted write */
121}
122
123static void bit_i810ddc_setsda(void *data, int val)
124{
125 writel((val ? SDA_VAL_OUT : 0) | SDA_DIR | SDA_DIR_MASK | SDA_VAL_MASK,
126 ioaddr + I810_GPIOA);
127 readl(ioaddr + I810_GPIOA); /* flush posted write */
128}
129
130static int bit_i810ddc_getscl(void *data)
131{
132 writel(SCL_DIR_MASK, ioaddr + I810_GPIOA);
133 writel(0, ioaddr + I810_GPIOA);
134 return (0 != (readl(ioaddr + I810_GPIOA) & SCL_VAL_IN));
135}
136
137static int bit_i810ddc_getsda(void *data)
138{
139 writel(SDA_DIR_MASK, ioaddr + I810_GPIOA);
140 writel(0, ioaddr + I810_GPIOA);
141 return (0 != (readl(ioaddr + I810_GPIOA) & SDA_VAL_IN));
142}
143
144static int config_i810(struct pci_dev *dev)
145{
146 unsigned long cadr;
147
148 /* map I810 memory */
149 cadr = dev->resource[1].start;
150 cadr += I810_IOCONTROL_OFFSET;
151 cadr &= PCI_BASE_ADDRESS_MEM_MASK;
152 ioaddr = ioremap_nocache(cadr, 0x1000);
153 if (ioaddr) {
154 bit_i810i2c_setscl(NULL, 1);
155 bit_i810i2c_setsda(NULL, 1);
156 bit_i810ddc_setscl(NULL, 1);
157 bit_i810ddc_setsda(NULL, 1);
158 return 0;
159 }
160 return -ENODEV;
161}
162
163static struct i2c_algo_bit_data i810_i2c_bit_data = {
164 .setsda = bit_i810i2c_setsda,
165 .setscl = bit_i810i2c_setscl,
166 .getsda = bit_i810i2c_getsda,
167 .getscl = bit_i810i2c_getscl,
168 .udelay = CYCLE_DELAY,
169 .mdelay = CYCLE_DELAY,
170 .timeout = TIMEOUT,
171};
172
173static struct i2c_adapter i810_i2c_adapter = {
174 .owner = THIS_MODULE,
175 .name = "I810/I815 I2C Adapter",
176 .algo_data = &i810_i2c_bit_data,
177};
178
179static struct i2c_algo_bit_data i810_ddc_bit_data = {
180 .setsda = bit_i810ddc_setsda,
181 .setscl = bit_i810ddc_setscl,
182 .getsda = bit_i810ddc_getsda,
183 .getscl = bit_i810ddc_getscl,
184 .udelay = CYCLE_DELAY,
185 .mdelay = CYCLE_DELAY,
186 .timeout = TIMEOUT,
187};
188
189static struct i2c_adapter i810_ddc_adapter = {
190 .owner = THIS_MODULE,
191 .name = "I810/I815 DDC Adapter",
192 .algo_data = &i810_ddc_bit_data,
193};
194
195static struct pci_device_id i810_ids[] __devinitdata = {
196 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82810_IG1) },
197 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82810_IG3) },
198 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82810E_IG) },
199 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82815_CGC) },
200 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82845G_IG) },
201 { 0, },
202};
203
204MODULE_DEVICE_TABLE (pci, i810_ids);
205
206static int __devinit i810_probe(struct pci_dev *dev, const struct pci_device_id *id)
207{
208 int retval;
209
210 retval = config_i810(dev);
211 if (retval)
212 return retval;
213 dev_info(&dev->dev, "i810/i815 i2c device found.\n");
214
215 /* set up the sysfs linkage to our parent device */
216 i810_i2c_adapter.dev.parent = &dev->dev;
217 i810_ddc_adapter.dev.parent = &dev->dev;
218
219 retval = i2c_bit_add_bus(&i810_i2c_adapter);
220 if (retval)
221 return retval;
222 retval = i2c_bit_add_bus(&i810_ddc_adapter);
223 if (retval)
224 i2c_bit_del_bus(&i810_i2c_adapter);
225 return retval;
226}
227
228static void __devexit i810_remove(struct pci_dev *dev)
229{
230 i2c_bit_del_bus(&i810_ddc_adapter);
231 i2c_bit_del_bus(&i810_i2c_adapter);
232 iounmap(ioaddr);
233}
234
235static struct pci_driver i810_driver = {
236 .name = "i810_smbus",
237 .id_table = i810_ids,
238 .probe = i810_probe,
239 .remove = __devexit_p(i810_remove),
240};
241
242static int __init i2c_i810_init(void)
243{
244 return pci_register_driver(&i810_driver);
245}
246
247static void __exit i2c_i810_exit(void)
248{
249 pci_unregister_driver(&i810_driver);
250}
251
252MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, "
253 "Philip Edelbrock <phil@netroedge.com>, "
254 "Ralph Metzler <rjkm@thp.uni-koeln.de>, "
255 "and Mark D. Studebaker <mdsxyz123@yahoo.com>");
256MODULE_DESCRIPTION("I810/I815 I2C/DDC driver");
257MODULE_LICENSE("GPL");
258
259module_init(i2c_i810_init);
260module_exit(i2c_i810_exit);
diff --git a/drivers/i2c/busses/i2c-ibm_iic.c b/drivers/i2c/busses/i2c-ibm_iic.c
new file mode 100644
index 000000000000..bb885215c08d
--- /dev/null
+++ b/drivers/i2c/busses/i2c-ibm_iic.c
@@ -0,0 +1,819 @@
1/*
2 * drivers/i2c/i2c-ibm_iic.c
3 *
4 * Support for the IIC peripheral on IBM PPC 4xx
5 *
6 * Copyright (c) 2003, 2004 Zultys Technologies.
7 * Eugene Surovegin <eugene.surovegin@zultys.com> or <ebs@ebshome.net>
8 *
9 * Based on original work by
10 * Ian DaSilva <idasilva@mvista.com>
11 * Armin Kuster <akuster@mvista.com>
12 * Matt Porter <mporter@mvista.com>
13 *
14 * Copyright 2000-2003 MontaVista Software Inc.
15 *
16 * Original driver version was highly leveraged from i2c-elektor.c
17 *
18 * Copyright 1995-97 Simon G. Vogl
19 * 1998-99 Hans Berglund
20 *
21 * With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>
22 * and even Frodo Looijaard <frodol@dds.nl>
23 *
24 * This program is free software; you can redistribute it and/or modify it
25 * under the terms of the GNU General Public License as published by the
26 * Free Software Foundation; either version 2 of the License, or (at your
27 * option) any later version.
28 *
29 */
30
31#include <linux/config.h>
32#include <linux/module.h>
33#include <linux/kernel.h>
34#include <linux/ioport.h>
35#include <linux/delay.h>
36#include <linux/slab.h>
37#include <linux/init.h>
38#include <linux/interrupt.h>
39#include <asm/irq.h>
40#include <asm/io.h>
41#include <linux/i2c.h>
42#include <linux/i2c-id.h>
43#include <asm/ocp.h>
44#include <asm/ibm4xx.h>
45
46#include "i2c-ibm_iic.h"
47
48#define DRIVER_VERSION "2.1"
49
50MODULE_DESCRIPTION("IBM IIC driver v" DRIVER_VERSION);
51MODULE_LICENSE("GPL");
52
53static int iic_force_poll;
54module_param(iic_force_poll, bool, 0);
55MODULE_PARM_DESC(iic_force_poll, "Force polling mode");
56
57static int iic_force_fast;
58module_param(iic_force_fast, bool, 0);
59MODULE_PARM_DESC(iic_fast_poll, "Force fast mode (400 kHz)");
60
61#define DBG_LEVEL 0
62
63#ifdef DBG
64#undef DBG
65#endif
66
67#ifdef DBG2
68#undef DBG2
69#endif
70
71#if DBG_LEVEL > 0
72# define DBG(f,x...) printk(KERN_DEBUG "ibm-iic" f, ##x)
73#else
74# define DBG(f,x...) ((void)0)
75#endif
76#if DBG_LEVEL > 1
77# define DBG2(f,x...) DBG(f, ##x)
78#else
79# define DBG2(f,x...) ((void)0)
80#endif
81#if DBG_LEVEL > 2
82static void dump_iic_regs(const char* header, struct ibm_iic_private* dev)
83{
84 volatile struct iic_regs __iomem *iic = dev->vaddr;
85 printk(KERN_DEBUG "ibm-iic%d: %s\n", dev->idx, header);
86 printk(KERN_DEBUG " cntl = 0x%02x, mdcntl = 0x%02x\n"
87 KERN_DEBUG " sts = 0x%02x, extsts = 0x%02x\n"
88 KERN_DEBUG " clkdiv = 0x%02x, xfrcnt = 0x%02x\n"
89 KERN_DEBUG " xtcntlss = 0x%02x, directcntl = 0x%02x\n",
90 in_8(&iic->cntl), in_8(&iic->mdcntl), in_8(&iic->sts),
91 in_8(&iic->extsts), in_8(&iic->clkdiv), in_8(&iic->xfrcnt),
92 in_8(&iic->xtcntlss), in_8(&iic->directcntl));
93}
94# define DUMP_REGS(h,dev) dump_iic_regs((h),(dev))
95#else
96# define DUMP_REGS(h,dev) ((void)0)
97#endif
98
99/* Bus timings (in ns) for bit-banging */
100static struct i2c_timings {
101 unsigned int hd_sta;
102 unsigned int su_sto;
103 unsigned int low;
104 unsigned int high;
105 unsigned int buf;
106} timings [] = {
107/* Standard mode (100 KHz) */
108{
109 .hd_sta = 4000,
110 .su_sto = 4000,
111 .low = 4700,
112 .high = 4000,
113 .buf = 4700,
114},
115/* Fast mode (400 KHz) */
116{
117 .hd_sta = 600,
118 .su_sto = 600,
119 .low = 1300,
120 .high = 600,
121 .buf = 1300,
122}};
123
124/* Enable/disable interrupt generation */
125static inline void iic_interrupt_mode(struct ibm_iic_private* dev, int enable)
126{
127 out_8(&dev->vaddr->intmsk, enable ? INTRMSK_EIMTC : 0);
128}
129
130/*
131 * Initialize IIC interface.
132 */
133static void iic_dev_init(struct ibm_iic_private* dev)
134{
135 volatile struct iic_regs __iomem *iic = dev->vaddr;
136
137 DBG("%d: init\n", dev->idx);
138
139 /* Clear master address */
140 out_8(&iic->lmadr, 0);
141 out_8(&iic->hmadr, 0);
142
143 /* Clear slave address */
144 out_8(&iic->lsadr, 0);
145 out_8(&iic->hsadr, 0);
146
147 /* Clear status & extended status */
148 out_8(&iic->sts, STS_SCMP | STS_IRQA);
149 out_8(&iic->extsts, EXTSTS_IRQP | EXTSTS_IRQD | EXTSTS_LA
150 | EXTSTS_ICT | EXTSTS_XFRA);
151
152 /* Set clock divider */
153 out_8(&iic->clkdiv, dev->clckdiv);
154
155 /* Clear transfer count */
156 out_8(&iic->xfrcnt, 0);
157
158 /* Clear extended control and status */
159 out_8(&iic->xtcntlss, XTCNTLSS_SRC | XTCNTLSS_SRS | XTCNTLSS_SWC
160 | XTCNTLSS_SWS);
161
162 /* Clear control register */
163 out_8(&iic->cntl, 0);
164
165 /* Enable interrupts if possible */
166 iic_interrupt_mode(dev, dev->irq >= 0);
167
168 /* Set mode control */
169 out_8(&iic->mdcntl, MDCNTL_FMDB | MDCNTL_EINT | MDCNTL_EUBS
170 | (dev->fast_mode ? MDCNTL_FSM : 0));
171
172 DUMP_REGS("iic_init", dev);
173}
174
175/*
176 * Reset IIC interface
177 */
178static void iic_dev_reset(struct ibm_iic_private* dev)
179{
180 volatile struct iic_regs __iomem *iic = dev->vaddr;
181 int i;
182 u8 dc;
183
184 DBG("%d: soft reset\n", dev->idx);
185 DUMP_REGS("reset", dev);
186
187 /* Place chip in the reset state */
188 out_8(&iic->xtcntlss, XTCNTLSS_SRST);
189
190 /* Check if bus is free */
191 dc = in_8(&iic->directcntl);
192 if (!DIRCTNL_FREE(dc)){
193 DBG("%d: trying to regain bus control\n", dev->idx);
194
195 /* Try to set bus free state */
196 out_8(&iic->directcntl, DIRCNTL_SDAC | DIRCNTL_SCC);
197
198 /* Wait until we regain bus control */
199 for (i = 0; i < 100; ++i){
200 dc = in_8(&iic->directcntl);
201 if (DIRCTNL_FREE(dc))
202 break;
203
204 /* Toggle SCL line */
205 dc ^= DIRCNTL_SCC;
206 out_8(&iic->directcntl, dc);
207 udelay(10);
208 dc ^= DIRCNTL_SCC;
209 out_8(&iic->directcntl, dc);
210
211 /* be nice */
212 cond_resched();
213 }
214 }
215
216 /* Remove reset */
217 out_8(&iic->xtcntlss, 0);
218
219 /* Reinitialize interface */
220 iic_dev_init(dev);
221}
222
223/*
224 * Do 0-length transaction using bit-banging through IIC_DIRECTCNTL register.
225 */
226
227/* Wait for SCL and/or SDA to be high */
228static int iic_dc_wait(volatile struct iic_regs __iomem *iic, u8 mask)
229{
230 unsigned long x = jiffies + HZ / 28 + 2;
231 while ((in_8(&iic->directcntl) & mask) != mask){
232 if (unlikely(time_after(jiffies, x)))
233 return -1;
234 cond_resched();
235 }
236 return 0;
237}
238
239static int iic_smbus_quick(struct ibm_iic_private* dev, const struct i2c_msg* p)
240{
241 volatile struct iic_regs __iomem *iic = dev->vaddr;
242 const struct i2c_timings* t = &timings[dev->fast_mode ? 1 : 0];
243 u8 mask, v, sda;
244 int i, res;
245
246 /* Only 7-bit addresses are supported */
247 if (unlikely(p->flags & I2C_M_TEN)){
248 DBG("%d: smbus_quick - 10 bit addresses are not supported\n",
249 dev->idx);
250 return -EINVAL;
251 }
252
253 DBG("%d: smbus_quick(0x%02x)\n", dev->idx, p->addr);
254
255 /* Reset IIC interface */
256 out_8(&iic->xtcntlss, XTCNTLSS_SRST);
257
258 /* Wait for bus to become free */
259 out_8(&iic->directcntl, DIRCNTL_SDAC | DIRCNTL_SCC);
260 if (unlikely(iic_dc_wait(iic, DIRCNTL_MSDA | DIRCNTL_MSC)))
261 goto err;
262 ndelay(t->buf);
263
264 /* START */
265 out_8(&iic->directcntl, DIRCNTL_SCC);
266 sda = 0;
267 ndelay(t->hd_sta);
268
269 /* Send address */
270 v = (u8)((p->addr << 1) | ((p->flags & I2C_M_RD) ? 1 : 0));
271 for (i = 0, mask = 0x80; i < 8; ++i, mask >>= 1){
272 out_8(&iic->directcntl, sda);
273 ndelay(t->low / 2);
274 sda = (v & mask) ? DIRCNTL_SDAC : 0;
275 out_8(&iic->directcntl, sda);
276 ndelay(t->low / 2);
277
278 out_8(&iic->directcntl, DIRCNTL_SCC | sda);
279 if (unlikely(iic_dc_wait(iic, DIRCNTL_MSC)))
280 goto err;
281 ndelay(t->high);
282 }
283
284 /* ACK */
285 out_8(&iic->directcntl, sda);
286 ndelay(t->low / 2);
287 out_8(&iic->directcntl, DIRCNTL_SDAC);
288 ndelay(t->low / 2);
289 out_8(&iic->directcntl, DIRCNTL_SDAC | DIRCNTL_SCC);
290 if (unlikely(iic_dc_wait(iic, DIRCNTL_MSC)))
291 goto err;
292 res = (in_8(&iic->directcntl) & DIRCNTL_MSDA) ? -EREMOTEIO : 1;
293 ndelay(t->high);
294
295 /* STOP */
296 out_8(&iic->directcntl, 0);
297 ndelay(t->low);
298 out_8(&iic->directcntl, DIRCNTL_SCC);
299 if (unlikely(iic_dc_wait(iic, DIRCNTL_MSC)))
300 goto err;
301 ndelay(t->su_sto);
302 out_8(&iic->directcntl, DIRCNTL_SDAC | DIRCNTL_SCC);
303
304 ndelay(t->buf);
305
306 DBG("%d: smbus_quick -> %s\n", dev->idx, res ? "NACK" : "ACK");
307out:
308 /* Remove reset */
309 out_8(&iic->xtcntlss, 0);
310
311 /* Reinitialize interface */
312 iic_dev_init(dev);
313
314 return res;
315err:
316 DBG("%d: smbus_quick - bus is stuck\n", dev->idx);
317 res = -EREMOTEIO;
318 goto out;
319}
320
321/*
322 * IIC interrupt handler
323 */
324static irqreturn_t iic_handler(int irq, void *dev_id, struct pt_regs *regs)
325{
326 struct ibm_iic_private* dev = (struct ibm_iic_private*)dev_id;
327 volatile struct iic_regs __iomem *iic = dev->vaddr;
328
329 DBG2("%d: irq handler, STS = 0x%02x, EXTSTS = 0x%02x\n",
330 dev->idx, in_8(&iic->sts), in_8(&iic->extsts));
331
332 /* Acknowledge IRQ and wakeup iic_wait_for_tc */
333 out_8(&iic->sts, STS_IRQA | STS_SCMP);
334 wake_up_interruptible(&dev->wq);
335
336 return IRQ_HANDLED;
337}
338
339/*
340 * Get master transfer result and clear errors if any.
341 * Returns the number of actually transferred bytes or error (<0)
342 */
343static int iic_xfer_result(struct ibm_iic_private* dev)
344{
345 volatile struct iic_regs __iomem *iic = dev->vaddr;
346
347 if (unlikely(in_8(&iic->sts) & STS_ERR)){
348 DBG("%d: xfer error, EXTSTS = 0x%02x\n", dev->idx,
349 in_8(&iic->extsts));
350
351 /* Clear errors and possible pending IRQs */
352 out_8(&iic->extsts, EXTSTS_IRQP | EXTSTS_IRQD |
353 EXTSTS_LA | EXTSTS_ICT | EXTSTS_XFRA);
354
355 /* Flush master data buffer */
356 out_8(&iic->mdcntl, in_8(&iic->mdcntl) | MDCNTL_FMDB);
357
358 /* Is bus free?
359 * If error happened during combined xfer
360 * IIC interface is usually stuck in some strange
361 * state, the only way out - soft reset.
362 */
363 if ((in_8(&iic->extsts) & EXTSTS_BCS_MASK) != EXTSTS_BCS_FREE){
364 DBG("%d: bus is stuck, resetting\n", dev->idx);
365 iic_dev_reset(dev);
366 }
367 return -EREMOTEIO;
368 }
369 else
370 return in_8(&iic->xfrcnt) & XFRCNT_MTC_MASK;
371}
372
373/*
374 * Try to abort active transfer.
375 */
376static void iic_abort_xfer(struct ibm_iic_private* dev)
377{
378 volatile struct iic_regs __iomem *iic = dev->vaddr;
379 unsigned long x;
380
381 DBG("%d: iic_abort_xfer\n", dev->idx);
382
383 out_8(&iic->cntl, CNTL_HMT);
384
385 /*
386 * Wait for the abort command to complete.
387 * It's not worth to be optimized, just poll (timeout >= 1 tick)
388 */
389 x = jiffies + 2;
390 while ((in_8(&iic->extsts) & EXTSTS_BCS_MASK) != EXTSTS_BCS_FREE){
391 if (time_after(jiffies, x)){
392 DBG("%d: abort timeout, resetting...\n", dev->idx);
393 iic_dev_reset(dev);
394 return;
395 }
396 schedule();
397 }
398
399 /* Just to clear errors */
400 iic_xfer_result(dev);
401}
402
403/*
404 * Wait for master transfer to complete.
405 * It puts current process to sleep until we get interrupt or timeout expires.
406 * Returns the number of transferred bytes or error (<0)
407 */
408static int iic_wait_for_tc(struct ibm_iic_private* dev){
409
410 volatile struct iic_regs __iomem *iic = dev->vaddr;
411 int ret = 0;
412
413 if (dev->irq >= 0){
414 /* Interrupt mode */
415 ret = wait_event_interruptible_timeout(dev->wq,
416 !(in_8(&iic->sts) & STS_PT), dev->adap.timeout * HZ);
417
418 if (unlikely(ret < 0))
419 DBG("%d: wait interrupted\n", dev->idx);
420 else if (unlikely(in_8(&iic->sts) & STS_PT)){
421 DBG("%d: wait timeout\n", dev->idx);
422 ret = -ETIMEDOUT;
423 }
424 }
425 else {
426 /* Polling mode */
427 unsigned long x = jiffies + dev->adap.timeout * HZ;
428
429 while (in_8(&iic->sts) & STS_PT){
430 if (unlikely(time_after(jiffies, x))){
431 DBG("%d: poll timeout\n", dev->idx);
432 ret = -ETIMEDOUT;
433 break;
434 }
435
436 if (unlikely(signal_pending(current))){
437 DBG("%d: poll interrupted\n", dev->idx);
438 ret = -ERESTARTSYS;
439 break;
440 }
441 schedule();
442 }
443 }
444
445 if (unlikely(ret < 0))
446 iic_abort_xfer(dev);
447 else
448 ret = iic_xfer_result(dev);
449
450 DBG2("%d: iic_wait_for_tc -> %d\n", dev->idx, ret);
451
452 return ret;
453}
454
455/*
456 * Low level master transfer routine
457 */
458static int iic_xfer_bytes(struct ibm_iic_private* dev, struct i2c_msg* pm,
459 int combined_xfer)
460{
461 volatile struct iic_regs __iomem *iic = dev->vaddr;
462 char* buf = pm->buf;
463 int i, j, loops, ret = 0;
464 int len = pm->len;
465
466 u8 cntl = (in_8(&iic->cntl) & CNTL_AMD) | CNTL_PT;
467 if (pm->flags & I2C_M_RD)
468 cntl |= CNTL_RW;
469
470 loops = (len + 3) / 4;
471 for (i = 0; i < loops; ++i, len -= 4){
472 int count = len > 4 ? 4 : len;
473 u8 cmd = cntl | ((count - 1) << CNTL_TCT_SHIFT);
474
475 if (!(cntl & CNTL_RW))
476 for (j = 0; j < count; ++j)
477 out_8((void __iomem *)&iic->mdbuf, *buf++);
478
479 if (i < loops - 1)
480 cmd |= CNTL_CHT;
481 else if (combined_xfer)
482 cmd |= CNTL_RPST;
483
484 DBG2("%d: xfer_bytes, %d, CNTL = 0x%02x\n", dev->idx, count, cmd);
485
486 /* Start transfer */
487 out_8(&iic->cntl, cmd);
488
489 /* Wait for completion */
490 ret = iic_wait_for_tc(dev);
491
492 if (unlikely(ret < 0))
493 break;
494 else if (unlikely(ret != count)){
495 DBG("%d: xfer_bytes, requested %d, transfered %d\n",
496 dev->idx, count, ret);
497
498 /* If it's not a last part of xfer, abort it */
499 if (combined_xfer || (i < loops - 1))
500 iic_abort_xfer(dev);
501
502 ret = -EREMOTEIO;
503 break;
504 }
505
506 if (cntl & CNTL_RW)
507 for (j = 0; j < count; ++j)
508 *buf++ = in_8((void __iomem *)&iic->mdbuf);
509 }
510
511 return ret > 0 ? 0 : ret;
512}
513
514/*
515 * Set target slave address for master transfer
516 */
517static inline void iic_address(struct ibm_iic_private* dev, struct i2c_msg* msg)
518{
519 volatile struct iic_regs __iomem *iic = dev->vaddr;
520 u16 addr = msg->addr;
521
522 DBG2("%d: iic_address, 0x%03x (%d-bit)\n", dev->idx,
523 addr, msg->flags & I2C_M_TEN ? 10 : 7);
524
525 if (msg->flags & I2C_M_TEN){
526 out_8(&iic->cntl, CNTL_AMD);
527 out_8(&iic->lmadr, addr);
528 out_8(&iic->hmadr, 0xf0 | ((addr >> 7) & 0x06));
529 }
530 else {
531 out_8(&iic->cntl, 0);
532 out_8(&iic->lmadr, addr << 1);
533 }
534}
535
536static inline int iic_invalid_address(const struct i2c_msg* p)
537{
538 return (p->addr > 0x3ff) || (!(p->flags & I2C_M_TEN) && (p->addr > 0x7f));
539}
540
541static inline int iic_address_neq(const struct i2c_msg* p1,
542 const struct i2c_msg* p2)
543{
544 return (p1->addr != p2->addr)
545 || ((p1->flags & I2C_M_TEN) != (p2->flags & I2C_M_TEN));
546}
547
548/*
549 * Generic master transfer entrypoint.
550 * Returns the number of processed messages or error (<0)
551 */
552static int iic_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
553{
554 struct ibm_iic_private* dev = (struct ibm_iic_private*)(i2c_get_adapdata(adap));
555 volatile struct iic_regs __iomem *iic = dev->vaddr;
556 int i, ret = 0;
557
558 DBG2("%d: iic_xfer, %d msg(s)\n", dev->idx, num);
559
560 if (!num)
561 return 0;
562
563 /* Check the sanity of the passed messages.
564 * Uhh, generic i2c layer is more suitable place for such code...
565 */
566 if (unlikely(iic_invalid_address(&msgs[0]))){
567 DBG("%d: invalid address 0x%03x (%d-bit)\n", dev->idx,
568 msgs[0].addr, msgs[0].flags & I2C_M_TEN ? 10 : 7);
569 return -EINVAL;
570 }
571 for (i = 0; i < num; ++i){
572 if (unlikely(msgs[i].len <= 0)){
573 if (num == 1 && !msgs[0].len){
574 /* Special case for I2C_SMBUS_QUICK emulation.
575 * IBM IIC doesn't support 0-length transactions
576 * so we have to emulate them using bit-banging.
577 */
578 return iic_smbus_quick(dev, &msgs[0]);
579 }
580 DBG("%d: invalid len %d in msg[%d]\n", dev->idx,
581 msgs[i].len, i);
582 return -EINVAL;
583 }
584 if (unlikely(iic_address_neq(&msgs[0], &msgs[i]))){
585 DBG("%d: invalid addr in msg[%d]\n", dev->idx, i);
586 return -EINVAL;
587 }
588 }
589
590 /* Check bus state */
591 if (unlikely((in_8(&iic->extsts) & EXTSTS_BCS_MASK) != EXTSTS_BCS_FREE)){
592 DBG("%d: iic_xfer, bus is not free\n", dev->idx);
593
594 /* Usually it means something serious has happend.
595 * We *cannot* have unfinished previous transfer
596 * so it doesn't make any sense to try to stop it.
597 * Probably we were not able to recover from the
598 * previous error.
599 * The only *reasonable* thing I can think of here
600 * is soft reset. --ebs
601 */
602 iic_dev_reset(dev);
603
604 if ((in_8(&iic->extsts) & EXTSTS_BCS_MASK) != EXTSTS_BCS_FREE){
605 DBG("%d: iic_xfer, bus is still not free\n", dev->idx);
606 return -EREMOTEIO;
607 }
608 }
609 else {
610 /* Flush master data buffer (just in case) */
611 out_8(&iic->mdcntl, in_8(&iic->mdcntl) | MDCNTL_FMDB);
612 }
613
614 /* Load slave address */
615 iic_address(dev, &msgs[0]);
616
617 /* Do real transfer */
618 for (i = 0; i < num && !ret; ++i)
619 ret = iic_xfer_bytes(dev, &msgs[i], i < num - 1);
620
621 return ret < 0 ? ret : num;
622}
623
624static u32 iic_func(struct i2c_adapter *adap)
625{
626 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_10BIT_ADDR;
627}
628
629static struct i2c_algorithm iic_algo = {
630 .name = "IBM IIC algorithm",
631 .id = I2C_ALGO_OCP,
632 .master_xfer = iic_xfer,
633 .functionality = iic_func
634};
635
636/*
637 * Calculates IICx_CLCKDIV value for a specific OPB clock frequency
638 */
639static inline u8 iic_clckdiv(unsigned int opb)
640{
641 /* Compatibility kludge, should go away after all cards
642 * are fixed to fill correct value for opbfreq.
643 * Previous driver version used hardcoded divider value 4,
644 * it corresponds to OPB frequency from the range (40, 50] MHz
645 */
646 if (!opb){
647 printk(KERN_WARNING "ibm-iic: using compatibility value for OPB freq,"
648 " fix your board specific setup\n");
649 opb = 50000000;
650 }
651
652 /* Convert to MHz */
653 opb /= 1000000;
654
655 if (opb < 20 || opb > 150){
656 printk(KERN_CRIT "ibm-iic: invalid OPB clock frequency %u MHz\n",
657 opb);
658 opb = opb < 20 ? 20 : 150;
659 }
660 return (u8)((opb + 9) / 10 - 1);
661}
662
663/*
664 * Register single IIC interface
665 */
666static int __devinit iic_probe(struct ocp_device *ocp){
667
668 struct ibm_iic_private* dev;
669 struct i2c_adapter* adap;
670 struct ocp_func_iic_data* iic_data = ocp->def->additions;
671 int ret;
672
673 if (!iic_data)
674 printk(KERN_WARNING"ibm-iic%d: missing additional data!\n",
675 ocp->def->index);
676
677 if (!(dev = kmalloc(sizeof(*dev), GFP_KERNEL))){
678 printk(KERN_CRIT "ibm-iic%d: failed to allocate device data\n",
679 ocp->def->index);
680 return -ENOMEM;
681 }
682
683 memset(dev, 0, sizeof(*dev));
684 dev->idx = ocp->def->index;
685 ocp_set_drvdata(ocp, dev);
686
687 if (!(dev->vaddr = ioremap(ocp->def->paddr, sizeof(struct iic_regs)))){
688 printk(KERN_CRIT "ibm-iic%d: failed to ioremap device registers\n",
689 dev->idx);
690 ret = -ENXIO;
691 goto fail2;
692 }
693
694 init_waitqueue_head(&dev->wq);
695
696 dev->irq = iic_force_poll ? -1 : ocp->def->irq;
697 if (dev->irq >= 0){
698 /* Disable interrupts until we finish intialization,
699 assumes level-sensitive IRQ setup...
700 */
701 iic_interrupt_mode(dev, 0);
702 if (request_irq(dev->irq, iic_handler, 0, "IBM IIC", dev)){
703 printk(KERN_ERR "ibm-iic%d: request_irq %d failed\n",
704 dev->idx, dev->irq);
705 /* Fallback to the polling mode */
706 dev->irq = -1;
707 }
708 }
709
710 if (dev->irq < 0)
711 printk(KERN_WARNING "ibm-iic%d: using polling mode\n",
712 dev->idx);
713
714 /* Board specific settings */
715 dev->fast_mode = iic_force_fast ? 1 : (iic_data ? iic_data->fast_mode : 0);
716
717 /* clckdiv is the same for *all* IIC interfaces,
718 * but I'd rather make a copy than introduce another global. --ebs
719 */
720 dev->clckdiv = iic_clckdiv(ocp_sys_info.opb_bus_freq);
721 DBG("%d: clckdiv = %d\n", dev->idx, dev->clckdiv);
722
723 /* Initialize IIC interface */
724 iic_dev_init(dev);
725
726 /* Register it with i2c layer */
727 adap = &dev->adap;
728 strcpy(adap->name, "IBM IIC");
729 i2c_set_adapdata(adap, dev);
730 adap->id = I2C_HW_OCP | iic_algo.id;
731 adap->algo = &iic_algo;
732 adap->client_register = NULL;
733 adap->client_unregister = NULL;
734 adap->timeout = 1;
735 adap->retries = 1;
736
737 if ((ret = i2c_add_adapter(adap)) != 0){
738 printk(KERN_CRIT "ibm-iic%d: failed to register i2c adapter\n",
739 dev->idx);
740 goto fail;
741 }
742
743 printk(KERN_INFO "ibm-iic%d: using %s mode\n", dev->idx,
744 dev->fast_mode ? "fast (400 kHz)" : "standard (100 kHz)");
745
746 return 0;
747
748fail:
749 if (dev->irq >= 0){
750 iic_interrupt_mode(dev, 0);
751 free_irq(dev->irq, dev);
752 }
753
754 iounmap(dev->vaddr);
755fail2:
756 ocp_set_drvdata(ocp, NULL);
757 kfree(dev);
758 return ret;
759}
760
761/*
762 * Cleanup initialized IIC interface
763 */
764static void __devexit iic_remove(struct ocp_device *ocp)
765{
766 struct ibm_iic_private* dev = (struct ibm_iic_private*)ocp_get_drvdata(ocp);
767 BUG_ON(dev == NULL);
768 if (i2c_del_adapter(&dev->adap)){
769 printk(KERN_CRIT "ibm-iic%d: failed to delete i2c adapter :(\n",
770 dev->idx);
771 /* That's *very* bad, just shutdown IRQ ... */
772 if (dev->irq >= 0){
773 iic_interrupt_mode(dev, 0);
774 free_irq(dev->irq, dev);
775 dev->irq = -1;
776 }
777 } else {
778 if (dev->irq >= 0){
779 iic_interrupt_mode(dev, 0);
780 free_irq(dev->irq, dev);
781 }
782 iounmap(dev->vaddr);
783 kfree(dev);
784 }
785}
786
787static struct ocp_device_id ibm_iic_ids[] __devinitdata =
788{
789 { .vendor = OCP_VENDOR_IBM, .function = OCP_FUNC_IIC },
790 { .vendor = OCP_VENDOR_INVALID }
791};
792
793MODULE_DEVICE_TABLE(ocp, ibm_iic_ids);
794
795static struct ocp_driver ibm_iic_driver =
796{
797 .name = "iic",
798 .id_table = ibm_iic_ids,
799 .probe = iic_probe,
800 .remove = __devexit_p(iic_remove),
801#if defined(CONFIG_PM)
802 .suspend = NULL,
803 .resume = NULL,
804#endif
805};
806
807static int __init iic_init(void)
808{
809 printk(KERN_INFO "IBM IIC driver v" DRIVER_VERSION "\n");
810 return ocp_register_driver(&ibm_iic_driver);
811}
812
813static void __exit iic_exit(void)
814{
815 ocp_unregister_driver(&ibm_iic_driver);
816}
817
818module_init(iic_init);
819module_exit(iic_exit);
diff --git a/drivers/i2c/busses/i2c-ibm_iic.h b/drivers/i2c/busses/i2c-ibm_iic.h
new file mode 100644
index 000000000000..d819a955d890
--- /dev/null
+++ b/drivers/i2c/busses/i2c-ibm_iic.h
@@ -0,0 +1,124 @@
1/*
2 * drivers/i2c/i2c-ibm_iic.h
3 *
4 * Support for the IIC peripheral on IBM PPC 4xx
5 *
6 * Copyright (c) 2003 Zultys Technologies.
7 * Eugene Surovegin <eugene.surovegin@zultys.com> or <ebs@ebshome.net>
8 *
9 * Based on original work by
10 * Ian DaSilva <idasilva@mvista.com>
11 * Armin Kuster <akuster@mvista.com>
12 * Matt Porter <mporter@mvista.com>
13 *
14 * Copyright 2000-2003 MontaVista Software Inc.
15 *
16 * This program is free software; you can redistribute it and/or modify it
17 * under the terms of the GNU General Public License as published by the
18 * Free Software Foundation; either version 2 of the License, or (at your
19 * option) any later version.
20 *
21 */
22#ifndef __I2C_IBM_IIC_H_
23#define __I2C_IBM_IIC_H_
24
25#include <linux/config.h>
26#include <linux/i2c.h>
27
28struct iic_regs {
29 u16 mdbuf;
30 u16 sbbuf;
31 u8 lmadr;
32 u8 hmadr;
33 u8 cntl;
34 u8 mdcntl;
35 u8 sts;
36 u8 extsts;
37 u8 lsadr;
38 u8 hsadr;
39 u8 clkdiv;
40 u8 intmsk;
41 u8 xfrcnt;
42 u8 xtcntlss;
43 u8 directcntl;
44};
45
46struct ibm_iic_private {
47 struct i2c_adapter adap;
48 volatile struct iic_regs __iomem *vaddr;
49 wait_queue_head_t wq;
50 int idx;
51 int irq;
52 int fast_mode;
53 u8 clckdiv;
54};
55
56/* IICx_CNTL register */
57#define CNTL_HMT 0x80
58#define CNTL_AMD 0x40
59#define CNTL_TCT_MASK 0x30
60#define CNTL_TCT_SHIFT 4
61#define CNTL_RPST 0x08
62#define CNTL_CHT 0x04
63#define CNTL_RW 0x02
64#define CNTL_PT 0x01
65
66/* IICx_MDCNTL register */
67#define MDCNTL_FSDB 0x80
68#define MDCNTL_FMDB 0x40
69#define MDCNTL_EGC 0x20
70#define MDCNTL_FSM 0x10
71#define MDCNTL_ESM 0x08
72#define MDCNTL_EINT 0x04
73#define MDCNTL_EUBS 0x02
74#define MDCNTL_HSCL 0x01
75
76/* IICx_STS register */
77#define STS_SSS 0x80
78#define STS_SLPR 0x40
79#define STS_MDBS 0x20
80#define STS_MDBF 0x10
81#define STS_SCMP 0x08
82#define STS_ERR 0x04
83#define STS_IRQA 0x02
84#define STS_PT 0x01
85
86/* IICx_EXTSTS register */
87#define EXTSTS_IRQP 0x80
88#define EXTSTS_BCS_MASK 0x70
89#define EXTSTS_BCS_FREE 0x40
90#define EXTSTS_IRQD 0x08
91#define EXTSTS_LA 0x04
92#define EXTSTS_ICT 0x02
93#define EXTSTS_XFRA 0x01
94
95/* IICx_INTRMSK register */
96#define INTRMSK_EIRC 0x80
97#define INTRMSK_EIRS 0x40
98#define INTRMSK_EIWC 0x20
99#define INTRMSK_EIWS 0x10
100#define INTRMSK_EIHE 0x08
101#define INTRMSK_EIIC 0x04
102#define INTRMSK_EITA 0x02
103#define INTRMSK_EIMTC 0x01
104
105/* IICx_XFRCNT register */
106#define XFRCNT_MTC_MASK 0x07
107
108/* IICx_XTCNTLSS register */
109#define XTCNTLSS_SRC 0x80
110#define XTCNTLSS_SRS 0x40
111#define XTCNTLSS_SWC 0x20
112#define XTCNTLSS_SWS 0x10
113#define XTCNTLSS_SRST 0x01
114
115/* IICx_DIRECTCNTL register */
116#define DIRCNTL_SDAC 0x08
117#define DIRCNTL_SCC 0x04
118#define DIRCNTL_MSDA 0x02
119#define DIRCNTL_MSC 0x01
120
121/* Check if we really control the I2C bus and bus is free */
122#define DIRCTNL_FREE(v) (((v) & 0x0f) == 0x0f)
123
124#endif /* __I2C_IBM_IIC_H_ */
diff --git a/drivers/i2c/busses/i2c-iop3xx.c b/drivers/i2c/busses/i2c-iop3xx.c
new file mode 100644
index 000000000000..c961ba4cfb32
--- /dev/null
+++ b/drivers/i2c/busses/i2c-iop3xx.c
@@ -0,0 +1,554 @@
1/* ------------------------------------------------------------------------- */
2/* i2c-iop3xx.c i2c driver algorithms for Intel XScale IOP3xx & IXP46x */
3/* ------------------------------------------------------------------------- */
4/* Copyright (C) 2003 Peter Milne, D-TACQ Solutions Ltd
5 * <Peter dot Milne at D hyphen TACQ dot com>
6 *
7 * With acknowledgements to i2c-algo-ibm_ocp.c by
8 * Ian DaSilva, MontaVista Software, Inc. idasilva@mvista.com
9 *
10 * And i2c-algo-pcf.c, which was created by Simon G. Vogl and Hans Berglund:
11 *
12 * Copyright (C) 1995-1997 Simon G. Vogl, 1998-2000 Hans Berglund
13 *
14 * And which acknowledged Kyösti Mälkki <kmalkki@cc.hut.fi>,
15 * Frodo Looijaard <frodol@dds.nl>, Martin Bailey<mbailey@littlefeet-inc.com>
16 *
17 * Major cleanup by Deepak Saxena <dsaxena@plexity.net>, 01/2005:
18 *
19 * - Use driver model to pass per-chip info instead of hardcoding and #ifdefs
20 * - Use ioremap/__raw_readl/__raw_writel instead of direct dereference
21 * - Make it work with IXP46x chips
22 * - Cleanup function names, coding style, etc
23 *
24 * This program is free software; you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License as published by
26 * the Free Software Foundation, version 2.
27 */
28
29#include <linux/config.h>
30#include <linux/interrupt.h>
31#include <linux/kernel.h>
32#include <linux/module.h>
33#include <linux/delay.h>
34#include <linux/slab.h>
35#include <linux/init.h>
36#include <linux/errno.h>
37#include <linux/sched.h>
38#include <linux/device.h>
39#include <linux/i2c.h>
40
41#include <asm/io.h>
42
43#include "i2c-iop3xx.h"
44
45/* global unit counter */
46static int i2c_id = 0;
47
48static inline unsigned char
49iic_cook_addr(struct i2c_msg *msg)
50{
51 unsigned char addr;
52
53 addr = (msg->addr << 1);
54
55 if (msg->flags & I2C_M_RD)
56 addr |= 1;
57
58 /*
59 * Read or Write?
60 */
61 if (msg->flags & I2C_M_REV_DIR_ADDR)
62 addr ^= 1;
63
64 return addr;
65}
66
67static void
68iop3xx_i2c_reset(struct i2c_algo_iop3xx_data *iop3xx_adap)
69{
70 /* Follows devman 9.3 */
71 __raw_writel(IOP3XX_ICR_UNIT_RESET, iop3xx_adap->ioaddr + CR_OFFSET);
72 __raw_writel(IOP3XX_ISR_CLEARBITS, iop3xx_adap->ioaddr + SR_OFFSET);
73 __raw_writel(0, iop3xx_adap->ioaddr + CR_OFFSET);
74}
75
76static void
77iop3xx_i2c_set_slave_addr(struct i2c_algo_iop3xx_data *iop3xx_adap)
78{
79 __raw_writel(MYSAR, iop3xx_adap->ioaddr + SAR_OFFSET);
80}
81
82static void
83iop3xx_i2c_enable(struct i2c_algo_iop3xx_data *iop3xx_adap)
84{
85 u32 cr = IOP3XX_ICR_GCD | IOP3XX_ICR_SCLEN | IOP3XX_ICR_UE;
86
87 /*
88 * Everytime unit enable is asserted, GPOD needs to be cleared
89 * on IOP321 to avoid data corruption on the bus.
90 */
91#ifdef CONFIG_ARCH_IOP321
92#define IOP321_GPOD_I2C0 0x00c0 /* clear these bits to enable ch0 */
93#define IOP321_GPOD_I2C1 0x0030 /* clear these bits to enable ch1 */
94
95 *IOP321_GPOD &= (iop3xx_adap->id == 0) ? ~IOP321_GPOD_I2C0 :
96 ~IOP321_GPOD_I2C1;
97#endif
98 /* NB SR bits not same position as CR IE bits :-( */
99 iop3xx_adap->SR_enabled =
100 IOP3XX_ISR_ALD | IOP3XX_ISR_BERRD |
101 IOP3XX_ISR_RXFULL | IOP3XX_ISR_TXEMPTY;
102
103 cr |= IOP3XX_ICR_ALD_IE | IOP3XX_ICR_BERR_IE |
104 IOP3XX_ICR_RXFULL_IE | IOP3XX_ICR_TXEMPTY_IE;
105
106 __raw_writel(cr, iop3xx_adap->ioaddr + CR_OFFSET);
107}
108
109static void
110iop3xx_i2c_transaction_cleanup(struct i2c_algo_iop3xx_data *iop3xx_adap)
111{
112 unsigned long cr = __raw_readl(iop3xx_adap->ioaddr + CR_OFFSET);
113
114 cr &= ~(IOP3XX_ICR_MSTART | IOP3XX_ICR_TBYTE |
115 IOP3XX_ICR_MSTOP | IOP3XX_ICR_SCLEN);
116
117 __raw_writel(cr, iop3xx_adap->ioaddr + CR_OFFSET);
118}
119
120/*
121 * NB: the handler has to clear the source of the interrupt!
122 * Then it passes the SR flags of interest to BH via adap data
123 */
124static irqreturn_t
125iop3xx_i2c_irq_handler(int this_irq, void *dev_id, struct pt_regs *regs)
126{
127 struct i2c_algo_iop3xx_data *iop3xx_adap = dev_id;
128 u32 sr = __raw_readl(iop3xx_adap->ioaddr + SR_OFFSET);
129
130 if ((sr &= iop3xx_adap->SR_enabled)) {
131 __raw_writel(sr, iop3xx_adap->ioaddr + SR_OFFSET);
132 iop3xx_adap->SR_received |= sr;
133 wake_up_interruptible(&iop3xx_adap->waitq);
134 }
135 return IRQ_HANDLED;
136}
137
138/* check all error conditions, clear them , report most important */
139static int
140iop3xx_i2c_error(u32 sr)
141{
142 int rc = 0;
143
144 if ((sr & IOP3XX_ISR_BERRD)) {
145 if ( !rc ) rc = -I2C_ERR_BERR;
146 }
147 if ((sr & IOP3XX_ISR_ALD)) {
148 if ( !rc ) rc = -I2C_ERR_ALD;
149 }
150 return rc;
151}
152
153static inline u32
154iop3xx_i2c_get_srstat(struct i2c_algo_iop3xx_data *iop3xx_adap)
155{
156 unsigned long flags;
157 u32 sr;
158
159 spin_lock_irqsave(&iop3xx_adap->lock, flags);
160 sr = iop3xx_adap->SR_received;
161 iop3xx_adap->SR_received = 0;
162 spin_unlock_irqrestore(&iop3xx_adap->lock, flags);
163
164 return sr;
165}
166
167/*
168 * sleep until interrupted, then recover and analyse the SR
169 * saved by handler
170 */
171typedef int (* compare_func)(unsigned test, unsigned mask);
172/* returns 1 on correct comparison */
173
174static int
175iop3xx_i2c_wait_event(struct i2c_algo_iop3xx_data *iop3xx_adap,
176 unsigned flags, unsigned* status,
177 compare_func compare)
178{
179 unsigned sr = 0;
180 int interrupted;
181 int done;
182 int rc = 0;
183
184 do {
185 interrupted = wait_event_interruptible_timeout (
186 iop3xx_adap->waitq,
187 (done = compare( sr = iop3xx_i2c_get_srstat(iop3xx_adap) ,flags )),
188 1 * HZ;
189 );
190 if ((rc = iop3xx_i2c_error(sr)) < 0) {
191 *status = sr;
192 return rc;
193 } else if (!interrupted) {
194 *status = sr;
195 return -ETIMEDOUT;
196 }
197 } while(!done);
198
199 *status = sr;
200
201 return 0;
202}
203
204/*
205 * Concrete compare_funcs
206 */
207static int
208all_bits_clear(unsigned test, unsigned mask)
209{
210 return (test & mask) == 0;
211}
212
213static int
214any_bits_set(unsigned test, unsigned mask)
215{
216 return (test & mask) != 0;
217}
218
219static int
220iop3xx_i2c_wait_tx_done(struct i2c_algo_iop3xx_data *iop3xx_adap, int *status)
221{
222 return iop3xx_i2c_wait_event(
223 iop3xx_adap,
224 IOP3XX_ISR_TXEMPTY | IOP3XX_ISR_ALD | IOP3XX_ISR_BERRD,
225 status, any_bits_set);
226}
227
228static int
229iop3xx_i2c_wait_rx_done(struct i2c_algo_iop3xx_data *iop3xx_adap, int *status)
230{
231 return iop3xx_i2c_wait_event(
232 iop3xx_adap,
233 IOP3XX_ISR_RXFULL | IOP3XX_ISR_ALD | IOP3XX_ISR_BERRD,
234 status, any_bits_set);
235}
236
237static int
238iop3xx_i2c_wait_idle(struct i2c_algo_iop3xx_data *iop3xx_adap, int *status)
239{
240 return iop3xx_i2c_wait_event(
241 iop3xx_adap, IOP3XX_ISR_UNITBUSY, status, all_bits_clear);
242}
243
244static int
245iop3xx_i2c_send_target_addr(struct i2c_algo_iop3xx_data *iop3xx_adap,
246 struct i2c_msg* msg)
247{
248 unsigned long cr = __raw_readl(iop3xx_adap->ioaddr + CR_OFFSET);
249 int status;
250 int rc;
251
252 __raw_writel(iic_cook_addr(msg), iop3xx_adap->ioaddr + DBR_OFFSET);
253
254 cr &= ~(IOP3XX_ICR_MSTOP | IOP3XX_ICR_NACK);
255 cr |= IOP3XX_ICR_MSTART | IOP3XX_ICR_TBYTE;
256
257 __raw_writel(cr, iop3xx_adap->ioaddr + CR_OFFSET);
258 rc = iop3xx_i2c_wait_tx_done(iop3xx_adap, &status);
259
260 return rc;
261}
262
263static int
264iop3xx_i2c_write_byte(struct i2c_algo_iop3xx_data *iop3xx_adap, char byte,
265 int stop)
266{
267 unsigned long cr = __raw_readl(iop3xx_adap->ioaddr + CR_OFFSET);
268 int status;
269 int rc = 0;
270
271 __raw_writel(byte, iop3xx_adap->ioaddr + DBR_OFFSET);
272 cr &= ~IOP3XX_ICR_MSTART;
273 if (stop) {
274 cr |= IOP3XX_ICR_MSTOP;
275 } else {
276 cr &= ~IOP3XX_ICR_MSTOP;
277 }
278 cr |= IOP3XX_ICR_TBYTE;
279 __raw_writel(cr, iop3xx_adap->ioaddr + CR_OFFSET);
280 rc = iop3xx_i2c_wait_tx_done(iop3xx_adap, &status);
281
282 return rc;
283}
284
285static int
286iop3xx_i2c_read_byte(struct i2c_algo_iop3xx_data *iop3xx_adap, char* byte,
287 int stop)
288{
289 unsigned long cr = __raw_readl(iop3xx_adap->ioaddr + CR_OFFSET);
290 int status;
291 int rc = 0;
292
293 cr &= ~IOP3XX_ICR_MSTART;
294
295 if (stop) {
296 cr |= IOP3XX_ICR_MSTOP | IOP3XX_ICR_NACK;
297 } else {
298 cr &= ~(IOP3XX_ICR_MSTOP | IOP3XX_ICR_NACK);
299 }
300 cr |= IOP3XX_ICR_TBYTE;
301 __raw_writel(cr, iop3xx_adap->ioaddr + CR_OFFSET);
302
303 rc = iop3xx_i2c_wait_rx_done(iop3xx_adap, &status);
304
305 *byte = __raw_readl(iop3xx_adap->ioaddr + DBR_OFFSET);
306
307 return rc;
308}
309
310static int
311iop3xx_i2c_writebytes(struct i2c_adapter *i2c_adap, const char *buf, int count)
312{
313 struct i2c_algo_iop3xx_data *iop3xx_adap = i2c_adap->algo_data;
314 int ii;
315 int rc = 0;
316
317 for (ii = 0; rc == 0 && ii != count; ++ii)
318 rc = iop3xx_i2c_write_byte(iop3xx_adap, buf[ii], ii==count-1);
319 return rc;
320}
321
322static int
323iop3xx_i2c_readbytes(struct i2c_adapter *i2c_adap, char *buf, int count)
324{
325 struct i2c_algo_iop3xx_data *iop3xx_adap = i2c_adap->algo_data;
326 int ii;
327 int rc = 0;
328
329 for (ii = 0; rc == 0 && ii != count; ++ii)
330 rc = iop3xx_i2c_read_byte(iop3xx_adap, &buf[ii], ii==count-1);
331
332 return rc;
333}
334
335/*
336 * Description: This function implements combined transactions. Combined
337 * transactions consist of combinations of reading and writing blocks of data.
338 * FROM THE SAME ADDRESS
339 * Each transfer (i.e. a read or a write) is separated by a repeated start
340 * condition.
341 */
342static int
343iop3xx_i2c_handle_msg(struct i2c_adapter *i2c_adap, struct i2c_msg* pmsg)
344{
345 struct i2c_algo_iop3xx_data *iop3xx_adap = i2c_adap->algo_data;
346 int rc;
347
348 rc = iop3xx_i2c_send_target_addr(iop3xx_adap, pmsg);
349 if (rc < 0) {
350 return rc;
351 }
352
353 if ((pmsg->flags&I2C_M_RD)) {
354 return iop3xx_i2c_readbytes(i2c_adap, pmsg->buf, pmsg->len);
355 } else {
356 return iop3xx_i2c_writebytes(i2c_adap, pmsg->buf, pmsg->len);
357 }
358}
359
360/*
361 * master_xfer() - main read/write entry
362 */
363static int
364iop3xx_i2c_master_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs,
365 int num)
366{
367 struct i2c_algo_iop3xx_data *iop3xx_adap = i2c_adap->algo_data;
368 int im = 0;
369 int ret = 0;
370 int status;
371
372 iop3xx_i2c_wait_idle(iop3xx_adap, &status);
373 iop3xx_i2c_reset(iop3xx_adap);
374 iop3xx_i2c_enable(iop3xx_adap);
375
376 for (im = 0; ret == 0 && im != num; im++) {
377 ret = iop3xx_i2c_handle_msg(i2c_adap, &msgs[im]);
378 }
379
380 iop3xx_i2c_transaction_cleanup(iop3xx_adap);
381
382 if(ret)
383 return ret;
384
385 return im;
386}
387
388static int
389iop3xx_i2c_algo_control(struct i2c_adapter *adapter, unsigned int cmd,
390 unsigned long arg)
391{
392 return 0;
393}
394
395static u32
396iop3xx_i2c_func(struct i2c_adapter *adap)
397{
398 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
399}
400
401static struct i2c_algorithm iop3xx_i2c_algo = {
402 .name = "IOP3xx I2C algorithm",
403 .id = I2C_ALGO_IOP3XX,
404 .master_xfer = iop3xx_i2c_master_xfer,
405 .algo_control = iop3xx_i2c_algo_control,
406 .functionality = iop3xx_i2c_func,
407};
408
409static int
410iop3xx_i2c_remove(struct device *device)
411{
412 struct platform_device *pdev = to_platform_device(device);
413 struct i2c_adapter *padapter = dev_get_drvdata(&pdev->dev);
414 struct i2c_algo_iop3xx_data *adapter_data =
415 (struct i2c_algo_iop3xx_data *)padapter->algo_data;
416 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
417 unsigned long cr = __raw_readl(adapter_data->ioaddr + CR_OFFSET);
418
419 /*
420 * Disable the actual HW unit
421 */
422 cr &= ~(IOP3XX_ICR_ALD_IE | IOP3XX_ICR_BERR_IE |
423 IOP3XX_ICR_RXFULL_IE | IOP3XX_ICR_TXEMPTY_IE);
424 __raw_writel(cr, adapter_data->ioaddr + CR_OFFSET);
425
426 iounmap((void __iomem*)adapter_data->ioaddr);
427 release_mem_region(res->start, IOP3XX_I2C_IO_SIZE);
428 kfree(adapter_data);
429 kfree(padapter);
430
431 dev_set_drvdata(&pdev->dev, NULL);
432
433 return 0;
434}
435
436static int
437iop3xx_i2c_probe(struct device *dev)
438{
439 struct platform_device *pdev = to_platform_device(dev);
440 struct resource *res;
441 int ret;
442 struct i2c_adapter *new_adapter;
443 struct i2c_algo_iop3xx_data *adapter_data;
444
445 new_adapter = kmalloc(sizeof(struct i2c_adapter), GFP_KERNEL);
446 if (!new_adapter) {
447 ret = -ENOMEM;
448 goto out;
449 }
450 memset((void*)new_adapter, 0, sizeof(*new_adapter));
451
452 adapter_data = kmalloc(sizeof(struct i2c_algo_iop3xx_data), GFP_KERNEL);
453 if (!adapter_data) {
454 ret = -ENOMEM;
455 goto free_adapter;
456 }
457 memset((void*)adapter_data, 0, sizeof(*adapter_data));
458
459 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
460 if (!res) {
461 ret = -ENODEV;
462 goto free_both;
463 }
464
465 if (!request_mem_region(res->start, IOP3XX_I2C_IO_SIZE, pdev->name)) {
466 ret = -EBUSY;
467 goto free_both;
468 }
469
470 /* set the adapter enumeration # */
471 adapter_data->id = i2c_id++;
472
473 adapter_data->ioaddr = (u32)ioremap(res->start, IOP3XX_I2C_IO_SIZE);
474 if (!adapter_data->ioaddr) {
475 ret = -ENOMEM;
476 goto release_region;
477 }
478
479 res = request_irq(platform_get_irq(pdev, 0), iop3xx_i2c_irq_handler, 0,
480 pdev->name, adapter_data);
481 if (res) {
482 ret = -EIO;
483 goto unmap;
484 }
485
486 memcpy(new_adapter->name, pdev->name, strlen(pdev->name));
487 new_adapter->id = I2C_HW_IOP3XX;
488 new_adapter->owner = THIS_MODULE;
489 new_adapter->dev.parent = &pdev->dev;
490
491 /*
492 * Default values...should these come in from board code?
493 */
494 new_adapter->timeout = 100;
495 new_adapter->retries = 3;
496 new_adapter->algo = &iop3xx_i2c_algo;
497
498 init_waitqueue_head(&adapter_data->waitq);
499 spin_lock_init(&adapter_data->lock);
500
501 iop3xx_i2c_reset(adapter_data);
502 iop3xx_i2c_set_slave_addr(adapter_data);
503 iop3xx_i2c_enable(adapter_data);
504
505 dev_set_drvdata(&pdev->dev, new_adapter);
506 new_adapter->algo_data = adapter_data;
507
508 i2c_add_adapter(new_adapter);
509
510 return 0;
511
512unmap:
513 iounmap((void __iomem*)adapter_data->ioaddr);
514
515release_region:
516 release_mem_region(res->start, IOP3XX_I2C_IO_SIZE);
517
518free_both:
519 kfree(adapter_data);
520
521free_adapter:
522 kfree(new_adapter);
523
524out:
525 return ret;
526}
527
528
529static struct device_driver iop3xx_i2c_driver = {
530 .name = "IOP3xx-I2C",
531 .bus = &platform_bus_type,
532 .probe = iop3xx_i2c_probe,
533 .remove = iop3xx_i2c_remove
534};
535
536static int __init
537i2c_iop3xx_init (void)
538{
539 return driver_register(&iop3xx_i2c_driver);
540}
541
542static void __exit
543i2c_iop3xx_exit (void)
544{
545 driver_unregister(&iop3xx_i2c_driver);
546 return;
547}
548
549module_init (i2c_iop3xx_init);
550module_exit (i2c_iop3xx_exit);
551
552MODULE_AUTHOR("D-TACQ Solutions Ltd <www.d-tacq.com>");
553MODULE_DESCRIPTION("IOP3xx iic algorithm and driver");
554MODULE_LICENSE("GPL");
diff --git a/drivers/i2c/busses/i2c-iop3xx.h b/drivers/i2c/busses/i2c-iop3xx.h
new file mode 100644
index 000000000000..e46ebaea7b1e
--- /dev/null
+++ b/drivers/i2c/busses/i2c-iop3xx.h
@@ -0,0 +1,107 @@
1/* ------------------------------------------------------------------------- */
2/* i2c-iop3xx.h algorithm driver definitions private to i2c-iop3xx.c */
3/* ------------------------------------------------------------------------- */
4/* Copyright (C) 2003 Peter Milne, D-TACQ Solutions Ltd
5 * <Peter dot Milne at D hyphen TACQ dot com>
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, version 2.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19/* ------------------------------------------------------------------------- */
20
21
22#ifndef I2C_IOP3XX_H
23#define I2C_IOP3XX_H 1
24
25/*
26 * iop321 hardware bit definitions
27 */
28#define IOP3XX_ICR_FAST_MODE 0x8000 /* 1=400kBps, 0=100kBps */
29#define IOP3XX_ICR_UNIT_RESET 0x4000 /* 1=RESET */
30#define IOP3XX_ICR_SAD_IE 0x2000 /* 1=Slave Detect Interrupt Enable */
31#define IOP3XX_ICR_ALD_IE 0x1000 /* 1=Arb Loss Detect Interrupt Enable */
32#define IOP3XX_ICR_SSD_IE 0x0800 /* 1=Slave STOP Detect Interrupt Enable */
33#define IOP3XX_ICR_BERR_IE 0x0400 /* 1=Bus Error Interrupt Enable */
34#define IOP3XX_ICR_RXFULL_IE 0x0200 /* 1=Receive Full Interrupt Enable */
35#define IOP3XX_ICR_TXEMPTY_IE 0x0100 /* 1=Transmit Empty Interrupt Enable */
36#define IOP3XX_ICR_GCD 0x0080 /* 1=General Call Disable */
37/*
38 * IOP3XX_ICR_GCD: 1 disables response as slave. "This bit must be set
39 * when sending a master mode general call message from the I2C unit"
40 */
41#define IOP3XX_ICR_UE 0x0040 /* 1=Unit Enable */
42/*
43 * "NOTE: To avoid I2C bus integrity problems,
44 * the user needs to ensure that the GPIO Output Data Register -
45 * GPOD bits associated with an I2C port are cleared prior to setting
46 * the enable bit for that I2C serial port.
47 * The user prepares to enable I2C port 0 and
48 * I2C port 1 by clearing GPOD bits 7:6 and GPOD bits 5:4, respectively.
49 */
50#define IOP3XX_ICR_SCLEN 0x0020 /* 1=SCL enable for master mode */
51#define IOP3XX_ICR_MABORT 0x0010 /* 1=Send a STOP with no data
52 * NB TBYTE must be clear */
53#define IOP3XX_ICR_TBYTE 0x0008 /* 1=Send/Receive a byte. i2c clears */
54#define IOP3XX_ICR_NACK 0x0004 /* 1=reply with NACK */
55#define IOP3XX_ICR_MSTOP 0x0002 /* 1=send a STOP after next data byte */
56#define IOP3XX_ICR_MSTART 0x0001 /* 1=initiate a START */
57
58
59#define IOP3XX_ISR_BERRD 0x0400 /* 1=BUS ERROR Detected */
60#define IOP3XX_ISR_SAD 0x0200 /* 1=Slave ADdress Detected */
61#define IOP3XX_ISR_GCAD 0x0100 /* 1=General Call Address Detected */
62#define IOP3XX_ISR_RXFULL 0x0080 /* 1=Receive Full */
63#define IOP3XX_ISR_TXEMPTY 0x0040 /* 1=Transmit Empty */
64#define IOP3XX_ISR_ALD 0x0020 /* 1=Arbitration Loss Detected */
65#define IOP3XX_ISR_SSD 0x0010 /* 1=Slave STOP Detected */
66#define IOP3XX_ISR_BBUSY 0x0008 /* 1=Bus BUSY */
67#define IOP3XX_ISR_UNITBUSY 0x0004 /* 1=Unit Busy */
68#define IOP3XX_ISR_NACK 0x0002 /* 1=Unit Rx or Tx a NACK */
69#define IOP3XX_ISR_RXREAD 0x0001 /* 1=READ 0=WRITE (R/W bit of slave addr */
70
71#define IOP3XX_ISR_CLEARBITS 0x07f0
72
73#define IOP3XX_ISAR_SAMASK 0x007f
74
75#define IOP3XX_IDBR_MASK 0x00ff
76
77#define IOP3XX_IBMR_SCL 0x0002
78#define IOP3XX_IBMR_SDA 0x0001
79
80#define IOP3XX_GPOD_I2C0 0x00c0 /* clear these bits to enable ch0 */
81#define IOP3XX_GPOD_I2C1 0x0030 /* clear these bits to enable ch1 */
82
83#define MYSAR 0x02 /* SWAG a suitable slave address */
84
85#define I2C_ERR 321
86#define I2C_ERR_BERR (I2C_ERR+0)
87#define I2C_ERR_ALD (I2C_ERR+1)
88
89
90#define CR_OFFSET 0
91#define SR_OFFSET 0x4
92#define SAR_OFFSET 0x8
93#define DBR_OFFSET 0xc
94#define CCR_OFFSET 0x10
95#define BMR_OFFSET 0x14
96
97#define IOP3XX_I2C_IO_SIZE 0x18
98
99struct i2c_algo_iop3xx_data {
100 u32 ioaddr;
101 wait_queue_head_t waitq;
102 spinlock_t lock;
103 u32 SR_enabled, SR_received;
104 int id;
105};
106
107#endif /* I2C_IOP3XX_H */
diff --git a/drivers/i2c/busses/i2c-isa.c b/drivers/i2c/busses/i2c-isa.c
new file mode 100644
index 000000000000..0f54a2a0afa5
--- /dev/null
+++ b/drivers/i2c/busses/i2c-isa.c
@@ -0,0 +1,72 @@
1/*
2 i2c-isa.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring
4 Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19*/
20
21/* This implements an i2c algorithm/adapter for ISA bus. Not that this is
22 on first sight very useful; almost no functionality is preserved.
23 Except that it makes writing drivers for chips which can be on both
24 the SMBus and the ISA bus very much easier. See lm78.c for an example
25 of this. */
26
27#include <linux/config.h>
28#include <linux/init.h>
29#include <linux/module.h>
30#include <linux/kernel.h>
31#include <linux/errno.h>
32#include <linux/i2c.h>
33
34static u32 isa_func(struct i2c_adapter *adapter);
35
36/* This is the actual algorithm we define */
37static struct i2c_algorithm isa_algorithm = {
38 .name = "ISA bus algorithm",
39 .id = I2C_ALGO_ISA,
40 .functionality = isa_func,
41};
42
43/* There can only be one... */
44static struct i2c_adapter isa_adapter = {
45 .owner = THIS_MODULE,
46 .class = I2C_CLASS_HWMON,
47 .algo = &isa_algorithm,
48 .name = "ISA main adapter",
49};
50
51/* We can't do a thing... */
52static u32 isa_func(struct i2c_adapter *adapter)
53{
54 return 0;
55}
56
57static int __init i2c_isa_init(void)
58{
59 return i2c_add_adapter(&isa_adapter);
60}
61
62static void __exit i2c_isa_exit(void)
63{
64 i2c_del_adapter(&isa_adapter);
65}
66
67MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>");
68MODULE_DESCRIPTION("ISA bus access through i2c");
69MODULE_LICENSE("GPL");
70
71module_init(i2c_isa_init);
72module_exit(i2c_isa_exit);
diff --git a/drivers/i2c/busses/i2c-ite.c b/drivers/i2c/busses/i2c-ite.c
new file mode 100644
index 000000000000..702e3def1b81
--- /dev/null
+++ b/drivers/i2c/busses/i2c-ite.c
@@ -0,0 +1,282 @@
1/*
2 -------------------------------------------------------------------------
3 i2c-adap-ite.c i2c-hw access for the IIC peripheral on the ITE MIPS system
4 -------------------------------------------------------------------------
5 Hai-Pao Fan, MontaVista Software, Inc.
6 hpfan@mvista.com or source@mvista.com
7
8 Copyright 2001 MontaVista Software Inc.
9
10 ----------------------------------------------------------------------------
11 This file was highly leveraged from i2c-elektor.c, which was created
12 by Simon G. Vogl and Hans Berglund:
13
14
15 Copyright (C) 1995-97 Simon G. Vogl
16 1998-99 Hans Berglund
17
18 This program is free software; you can redistribute it and/or modify
19 it under the terms of the GNU General Public License as published by
20 the Free Software Foundation; either version 2 of the License, or
21 (at your option) any later version.
22
23 This program is distributed in the hope that it will be useful,
24 but WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 GNU General Public License for more details.
27
28 You should have received a copy of the GNU General Public License
29 along with this program; if not, write to the Free Software
30 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
31/* ------------------------------------------------------------------------- */
32
33/* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi> and even
34 Frodo Looijaard <frodol@dds.nl> */
35
36#include <linux/config.h>
37#include <linux/kernel.h>
38#include <linux/ioport.h>
39#include <linux/module.h>
40#include <linux/delay.h>
41#include <linux/slab.h>
42#include <linux/init.h>
43#include <linux/wait.h>
44#include <asm/irq.h>
45#include <asm/io.h>
46
47#include <linux/i2c.h>
48#include <linux/i2c-algo-ite.h>
49#include <linux/i2c-adap-ite.h>
50#include "../i2c-ite.h"
51
52#define DEFAULT_BASE 0x14014030
53#define ITE_IIC_IO_SIZE 0x40
54#define DEFAULT_IRQ 0
55#define DEFAULT_CLOCK 0x1b0e /* default 16MHz/(27+14) = 400KHz */
56#define DEFAULT_OWN 0x55
57
58static int base;
59static int irq;
60static int clock;
61static int own;
62
63static struct iic_ite gpi;
64static wait_queue_head_t iic_wait;
65static int iic_pending;
66static spinlock_t lock;
67
68/* ----- local functions ---------------------------------------------- */
69
70static void iic_ite_setiic(void *data, int ctl, short val)
71{
72 unsigned long j = jiffies + 10;
73
74 pr_debug(" Write 0x%02x to 0x%x\n",(unsigned short)val, ctl&0xff);
75#ifdef DEBUG
76 while (time_before(jiffies, j))
77 schedule();
78#endif
79 outw(val,ctl);
80}
81
82static short iic_ite_getiic(void *data, int ctl)
83{
84 short val;
85
86 val = inw(ctl);
87 pr_debug("Read 0x%02x from 0x%x\n",(unsigned short)val, ctl&0xff);
88 return (val);
89}
90
91/* Return our slave address. This is the address
92 * put on the I2C bus when another master on the bus wants to address us
93 * as a slave
94 */
95static int iic_ite_getown(void *data)
96{
97 return (gpi.iic_own);
98}
99
100
101static int iic_ite_getclock(void *data)
102{
103 return (gpi.iic_clock);
104}
105
106
107/* Put this process to sleep. We will wake up when the
108 * IIC controller interrupts.
109 */
110static void iic_ite_waitforpin(void) {
111 DEFINE_WAIT(wait);
112 int timeout = 2;
113 long flags;
114
115 /* If interrupts are enabled (which they are), then put the process to
116 * sleep. This process will be awakened by two events -- either the
117 * the IIC peripheral interrupts or the timeout expires.
118 * If interrupts are not enabled then delay for a reasonable amount
119 * of time and return.
120 */
121 if (gpi.iic_irq > 0) {
122 spin_lock_irqsave(&lock, flags);
123 if (iic_pending == 0) {
124 spin_unlock_irqrestore(&lock, flags);
125 prepare_to_wait(&iic_wait, &wait, TASK_INTERRUPTIBLE);
126 if (schedule_timeout(timeout*HZ)) {
127 spin_lock_irqsave(&lock, flags);
128 if (iic_pending == 1) {
129 iic_pending = 0;
130 }
131 spin_unlock_irqrestore(&lock, flags);
132 }
133 finish_wait(&iic_wait, &wait);
134 } else {
135 iic_pending = 0;
136 spin_unlock_irqrestore(&lock, flags);
137 }
138 } else {
139 udelay(100);
140 }
141}
142
143
144static irqreturn_t iic_ite_handler(int this_irq, void *dev_id,
145 struct pt_regs *regs)
146{
147 spin_lock(&lock);
148 iic_pending = 1;
149 spin_unlock(&lock);
150
151 wake_up_interruptible(&iic_wait);
152
153 return IRQ_HANDLED;
154}
155
156
157/* Lock the region of memory where I/O registers exist. Request our
158 * interrupt line and register its associated handler.
159 */
160static int iic_hw_resrc_init(void)
161{
162 if (!request_region(gpi.iic_base, ITE_IIC_IO_SIZE, "i2c"))
163 return -ENODEV;
164
165 if (gpi.iic_irq <= 0)
166 return 0;
167
168 if (request_irq(gpi.iic_irq, iic_ite_handler, 0, "ITE IIC", 0) < 0)
169 gpi.iic_irq = 0;
170 else
171 enable_irq(gpi.iic_irq);
172
173 return 0;
174}
175
176
177static void iic_ite_release(void)
178{
179 if (gpi.iic_irq > 0) {
180 disable_irq(gpi.iic_irq);
181 free_irq(gpi.iic_irq, 0);
182 }
183 release_region(gpi.iic_base , 2);
184}
185
186/* ------------------------------------------------------------------------
187 * Encapsulate the above functions in the correct operations structure.
188 * This is only done when more than one hardware adapter is supported.
189 */
190static struct i2c_algo_iic_data iic_ite_data = {
191 NULL,
192 iic_ite_setiic,
193 iic_ite_getiic,
194 iic_ite_getown,
195 iic_ite_getclock,
196 iic_ite_waitforpin,
197 80, 80, 100, /* waits, timeout */
198};
199
200static struct i2c_adapter iic_ite_ops = {
201 .owner = THIS_MODULE,
202 .id = I2C_HW_I_IIC,
203 .algo_data = &iic_ite_data,
204 .dev = {
205 .name = "ITE IIC adapter",
206 },
207};
208
209/* Called when the module is loaded. This function starts the
210 * cascade of calls up through the hierarchy of i2c modules (i.e. up to the
211 * algorithm layer and into to the core layer)
212 */
213static int __init iic_ite_init(void)
214{
215
216 struct iic_ite *piic = &gpi;
217
218 printk(KERN_INFO "Initialize ITE IIC adapter module\n");
219 if (base == 0)
220 piic->iic_base = DEFAULT_BASE;
221 else
222 piic->iic_base = base;
223
224 if (irq == 0)
225 piic->iic_irq = DEFAULT_IRQ;
226 else
227 piic->iic_irq = irq;
228
229 if (clock == 0)
230 piic->iic_clock = DEFAULT_CLOCK;
231 else
232 piic->iic_clock = clock;
233
234 if (own == 0)
235 piic->iic_own = DEFAULT_OWN;
236 else
237 piic->iic_own = own;
238
239 iic_ite_data.data = (void *)piic;
240 init_waitqueue_head(&iic_wait);
241 spin_lock_init(&lock);
242 if (iic_hw_resrc_init() == 0) {
243 if (i2c_iic_add_bus(&iic_ite_ops) < 0)
244 return -ENODEV;
245 } else {
246 return -ENODEV;
247 }
248 printk(KERN_INFO " found device at %#x irq %d.\n",
249 piic->iic_base, piic->iic_irq);
250 return 0;
251}
252
253
254static void iic_ite_exit(void)
255{
256 i2c_iic_del_bus(&iic_ite_ops);
257 iic_ite_release();
258}
259
260/* If modules is NOT defined when this file is compiled, then the MODULE_*
261 * macros will resolve to nothing
262 */
263MODULE_AUTHOR("MontaVista Software <www.mvista.com>");
264MODULE_DESCRIPTION("I2C-Bus adapter routines for ITE IIC bus adapter");
265MODULE_LICENSE("GPL");
266
267module_param(base, int, 0);
268module_param(irq, int, 0);
269module_param(clock, int, 0);
270module_param(own, int, 0);
271
272
273/* Called when module is loaded or when kernel is initialized.
274 * If MODULES is defined when this file is compiled, then this function will
275 * resolve to init_module (the function called when insmod is invoked for a
276 * module). Otherwise, this function is called early in the boot, when the
277 * kernel is intialized. Check out /include/init.h to see how this works.
278 */
279module_init(iic_ite_init);
280
281/* Resolves to module_cleanup when MODULES is defined. */
282module_exit(iic_ite_exit);
diff --git a/drivers/i2c/busses/i2c-ixp2000.c b/drivers/i2c/busses/i2c-ixp2000.c
new file mode 100644
index 000000000000..21cd54d02302
--- /dev/null
+++ b/drivers/i2c/busses/i2c-ixp2000.c
@@ -0,0 +1,171 @@
1/*
2 * drivers/i2c/busses/i2c-ixp2000.c
3 *
4 * I2C adapter for IXP2000 systems using GPIOs for I2C bus
5 *
6 * Author: Deepak Saxena <dsaxena@plexity.net>
7 * Based on IXDP2400 code by: Naeem M. Afzal <naeem.m.afzal@intel.com>
8 * Made generic by: Jeff Daly <jeffrey.daly@intel.com>
9 *
10 * Copyright (c) 2003-2004 MontaVista Software Inc.
11 *
12 * This file is licensed under the terms of the GNU General Public
13 * License version 2. This program is licensed "as is" without any
14 * warranty of any kind, whether express or implied.
15 *
16 * From Jeff Daly:
17 *
18 * I2C adapter driver for Intel IXDP2xxx platforms. This should work for any
19 * IXP2000 platform if it uses the HW GPIO in the same manner. Basically,
20 * SDA and SCL GPIOs have external pullups. Setting the respective GPIO to
21 * an input will make the signal a '1' via the pullup. Setting them to
22 * outputs will pull them down.
23 *
24 * The GPIOs are open drain signals and are used as configuration strap inputs
25 * during power-up so there's generally a buffer on the board that needs to be
26 * 'enabled' to drive the GPIOs.
27 */
28
29#include <linux/config.h>
30#ifdef CONFIG_I2C_DEBUG_BUS
31#define DEBUG 1
32#endif
33
34#include <linux/kernel.h>
35#include <linux/init.h>
36#include <linux/device.h>
37#include <linux/module.h>
38#include <linux/i2c.h>
39#include <linux/i2c-algo-bit.h>
40
41#include <asm/hardware.h> /* Pick up IXP42000-specific bits */
42
43static inline int ixp2000_scl_pin(void *data)
44{
45 return ((struct ixp2000_i2c_pins*)data)->scl_pin;
46}
47
48static inline int ixp2000_sda_pin(void *data)
49{
50 return ((struct ixp2000_i2c_pins*)data)->sda_pin;
51}
52
53
54static void ixp2000_bit_setscl(void *data, int val)
55{
56 int i = 5000;
57
58 if (val) {
59 gpio_line_config(ixp2000_scl_pin(data), GPIO_IN);
60 while(!gpio_line_get(ixp2000_scl_pin(data)) && i--);
61 } else {
62 gpio_line_config(ixp2000_scl_pin(data), GPIO_OUT);
63 }
64}
65
66static void ixp2000_bit_setsda(void *data, int val)
67{
68 if (val) {
69 gpio_line_config(ixp2000_sda_pin(data), GPIO_IN);
70 } else {
71 gpio_line_config(ixp2000_sda_pin(data), GPIO_OUT);
72 }
73}
74
75static int ixp2000_bit_getscl(void *data)
76{
77 return gpio_line_get(ixp2000_scl_pin(data));
78}
79
80static int ixp2000_bit_getsda(void *data)
81{
82 return gpio_line_get(ixp2000_sda_pin(data));
83}
84
85struct ixp2000_i2c_data {
86 struct ixp2000_i2c_pins *gpio_pins;
87 struct i2c_adapter adapter;
88 struct i2c_algo_bit_data algo_data;
89};
90
91static int ixp2000_i2c_remove(struct device *dev)
92{
93 struct platform_device *plat_dev = to_platform_device(dev);
94 struct ixp2000_i2c_data *drv_data = dev_get_drvdata(&plat_dev->dev);
95
96 dev_set_drvdata(&plat_dev->dev, NULL);
97
98 i2c_bit_del_bus(&drv_data->adapter);
99
100 kfree(drv_data);
101
102 return 0;
103}
104
105static int ixp2000_i2c_probe(struct device *dev)
106{
107 int err;
108 struct platform_device *plat_dev = to_platform_device(dev);
109 struct ixp2000_i2c_pins *gpio = plat_dev->dev.platform_data;
110 struct ixp2000_i2c_data *drv_data =
111 kmalloc(sizeof(struct ixp2000_i2c_data), GFP_KERNEL);
112
113 if (!drv_data)
114 return -ENOMEM;
115 memzero(drv_data, sizeof(*drv_data));
116 drv_data->gpio_pins = gpio;
117
118 drv_data->algo_data.data = gpio;
119 drv_data->algo_data.setsda = ixp2000_bit_setsda;
120 drv_data->algo_data.setscl = ixp2000_bit_setscl;
121 drv_data->algo_data.getsda = ixp2000_bit_getsda;
122 drv_data->algo_data.getscl = ixp2000_bit_getscl;
123 drv_data->algo_data.udelay = 6;
124 drv_data->algo_data.mdelay = 6;
125 drv_data->algo_data.timeout = 100;
126
127 drv_data->adapter.id = I2C_HW_B_IXP2000,
128 drv_data->adapter.algo_data = &drv_data->algo_data,
129
130 drv_data->adapter.dev.parent = &plat_dev->dev;
131
132 gpio_line_config(gpio->sda_pin, GPIO_IN);
133 gpio_line_config(gpio->scl_pin, GPIO_IN);
134 gpio_line_set(gpio->scl_pin, 0);
135 gpio_line_set(gpio->sda_pin, 0);
136
137 if ((err = i2c_bit_add_bus(&drv_data->adapter)) != 0) {
138 dev_err(dev, "Could not install, error %d\n", err);
139 kfree(drv_data);
140 return err;
141 }
142
143 dev_set_drvdata(&plat_dev->dev, drv_data);
144
145 return 0;
146}
147
148static struct device_driver ixp2000_i2c_driver = {
149 .name = "IXP2000-I2C",
150 .bus = &platform_bus_type,
151 .probe = ixp2000_i2c_probe,
152 .remove = ixp2000_i2c_remove,
153};
154
155static int __init ixp2000_i2c_init(void)
156{
157 return driver_register(&ixp2000_i2c_driver);
158}
159
160static void __exit ixp2000_i2c_exit(void)
161{
162 driver_unregister(&ixp2000_i2c_driver);
163}
164
165module_init(ixp2000_i2c_init);
166module_exit(ixp2000_i2c_exit);
167
168MODULE_AUTHOR ("Deepak Saxena <dsaxena@plexity.net>");
169MODULE_DESCRIPTION("IXP2000 GPIO-based I2C bus driver");
170MODULE_LICENSE("GPL");
171
diff --git a/drivers/i2c/busses/i2c-ixp4xx.c b/drivers/i2c/busses/i2c-ixp4xx.c
new file mode 100644
index 000000000000..8c55eafc3a09
--- /dev/null
+++ b/drivers/i2c/busses/i2c-ixp4xx.c
@@ -0,0 +1,181 @@
1/*
2 * drivers/i2c/i2c-adap-ixp4xx.c
3 *
4 * Intel's IXP4xx XScale NPU chipsets (IXP420, 421, 422, 425) do not have
5 * an on board I2C controller but provide 16 GPIO pins that are often
6 * used to create an I2C bus. This driver provides an i2c_adapter
7 * interface that plugs in under algo_bit and drives the GPIO pins
8 * as instructed by the alogorithm driver.
9 *
10 * Author: Deepak Saxena <dsaxena@plexity.net>
11 *
12 * Copyright (c) 2003-2004 MontaVista Software Inc.
13 *
14 * This file is licensed under the terms of the GNU General Public
15 * License version 2. This program is licensed "as is" without any
16 * warranty of any kind, whether express or implied.
17 *
18 * NOTE: Since different platforms will use different GPIO pins for
19 * I2C, this driver uses an IXP4xx-specific platform_data
20 * pointer to pass the GPIO numbers to the driver. This
21 * allows us to support all the different IXP4xx platforms
22 * w/o having to put #ifdefs in this driver.
23 *
24 * See arch/arm/mach-ixp4xx/ixdp425.c for an example of building a
25 * device list and filling in the ixp4xx_i2c_pins data structure
26 * that is passed as the platform_data to this driver.
27 */
28
29#include <linux/config.h>
30#ifdef CONFIG_I2C_DEBUG_BUS
31#define DEBUG 1
32#endif
33
34#include <linux/kernel.h>
35#include <linux/init.h>
36#include <linux/device.h>
37#include <linux/module.h>
38#include <linux/i2c.h>
39#include <linux/i2c-algo-bit.h>
40
41#include <asm/hardware.h> /* Pick up IXP4xx-specific bits */
42
43static inline int ixp4xx_scl_pin(void *data)
44{
45 return ((struct ixp4xx_i2c_pins*)data)->scl_pin;
46}
47
48static inline int ixp4xx_sda_pin(void *data)
49{
50 return ((struct ixp4xx_i2c_pins*)data)->sda_pin;
51}
52
53static void ixp4xx_bit_setscl(void *data, int val)
54{
55 gpio_line_set(ixp4xx_scl_pin(data), 0);
56 gpio_line_config(ixp4xx_scl_pin(data),
57 val ? IXP4XX_GPIO_IN : IXP4XX_GPIO_OUT );
58}
59
60static void ixp4xx_bit_setsda(void *data, int val)
61{
62 gpio_line_set(ixp4xx_sda_pin(data), 0);
63 gpio_line_config(ixp4xx_sda_pin(data),
64 val ? IXP4XX_GPIO_IN : IXP4XX_GPIO_OUT );
65}
66
67static int ixp4xx_bit_getscl(void *data)
68{
69 int scl;
70
71 gpio_line_config(ixp4xx_scl_pin(data), IXP4XX_GPIO_IN );
72 gpio_line_get(ixp4xx_scl_pin(data), &scl);
73
74 return scl;
75}
76
77static int ixp4xx_bit_getsda(void *data)
78{
79 int sda;
80
81 gpio_line_config(ixp4xx_sda_pin(data), IXP4XX_GPIO_IN );
82 gpio_line_get(ixp4xx_sda_pin(data), &sda);
83
84 return sda;
85}
86
87struct ixp4xx_i2c_data {
88 struct ixp4xx_i2c_pins *gpio_pins;
89 struct i2c_adapter adapter;
90 struct i2c_algo_bit_data algo_data;
91};
92
93static int ixp4xx_i2c_remove(struct device *dev)
94{
95 struct platform_device *plat_dev = to_platform_device(dev);
96 struct ixp4xx_i2c_data *drv_data = dev_get_drvdata(&plat_dev->dev);
97
98 dev_set_drvdata(&plat_dev->dev, NULL);
99
100 i2c_bit_del_bus(&drv_data->adapter);
101
102 kfree(drv_data);
103
104 return 0;
105}
106
107static int ixp4xx_i2c_probe(struct device *dev)
108{
109 int err;
110 struct platform_device *plat_dev = to_platform_device(dev);
111 struct ixp4xx_i2c_pins *gpio = plat_dev->dev.platform_data;
112 struct ixp4xx_i2c_data *drv_data =
113 kmalloc(sizeof(struct ixp4xx_i2c_data), GFP_KERNEL);
114
115 if(!drv_data)
116 return -ENOMEM;
117
118 memzero(drv_data, sizeof(struct ixp4xx_i2c_data));
119 drv_data->gpio_pins = gpio;
120
121 /*
122 * We could make a lot of these structures static, but
123 * certain platforms may have multiple GPIO-based I2C
124 * buses for various device domains, so we need per-device
125 * algo_data->data.
126 */
127 drv_data->algo_data.data = gpio;
128 drv_data->algo_data.setsda = ixp4xx_bit_setsda;
129 drv_data->algo_data.setscl = ixp4xx_bit_setscl;
130 drv_data->algo_data.getsda = ixp4xx_bit_getsda;
131 drv_data->algo_data.getscl = ixp4xx_bit_getscl;
132 drv_data->algo_data.udelay = 10;
133 drv_data->algo_data.mdelay = 10;
134 drv_data->algo_data.timeout = 100;
135
136 drv_data->adapter.id = I2C_HW_B_IXP4XX;
137 drv_data->adapter.algo_data = &drv_data->algo_data;
138
139 drv_data->adapter.dev.parent = &plat_dev->dev;
140
141 gpio_line_config(gpio->scl_pin, IXP4XX_GPIO_IN);
142 gpio_line_config(gpio->sda_pin, IXP4XX_GPIO_IN);
143 gpio_line_set(gpio->scl_pin, 0);
144 gpio_line_set(gpio->sda_pin, 0);
145
146 if ((err = i2c_bit_add_bus(&drv_data->adapter) != 0)) {
147 printk(KERN_ERR "ERROR: Could not install %s\n", dev->bus_id);
148
149 kfree(drv_data);
150 return err;
151 }
152
153 dev_set_drvdata(&plat_dev->dev, drv_data);
154
155 return 0;
156}
157
158static struct device_driver ixp4xx_i2c_driver = {
159 .name = "IXP4XX-I2C",
160 .bus = &platform_bus_type,
161 .probe = ixp4xx_i2c_probe,
162 .remove = ixp4xx_i2c_remove,
163};
164
165static int __init ixp4xx_i2c_init(void)
166{
167 return driver_register(&ixp4xx_i2c_driver);
168}
169
170static void __exit ixp4xx_i2c_exit(void)
171{
172 driver_unregister(&ixp4xx_i2c_driver);
173}
174
175module_init(ixp4xx_i2c_init);
176module_exit(ixp4xx_i2c_exit);
177
178MODULE_DESCRIPTION("GPIO-based I2C adapter for IXP4xx systems");
179MODULE_LICENSE("GPL");
180MODULE_AUTHOR("Deepak Saxena <dsaxena@plexity.net>");
181
diff --git a/drivers/i2c/busses/i2c-keywest.c b/drivers/i2c/busses/i2c-keywest.c
new file mode 100644
index 000000000000..dd0d4c463146
--- /dev/null
+++ b/drivers/i2c/busses/i2c-keywest.c
@@ -0,0 +1,763 @@
1/*
2 i2c Support for Apple Keywest I2C Bus Controller
3
4 Copyright (c) 2001 Benjamin Herrenschmidt <benh@kernel.crashing.org>
5
6 Original work by
7
8 Copyright (c) 2000 Philip Edelbrock <phil@stimpy.netroedge.com>
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23
24 Changes:
25
26 2001/12/13 BenH New implementation
27 2001/12/15 BenH Add support for "byte" and "quick"
28 transfers. Add i2c_xfer routine.
29 2003/09/21 BenH Rework state machine with Paulus help
30 2004/01/21 BenH Merge in Greg KH changes, polled mode is back
31 2004/02/05 BenH Merge 64 bits fixes from the g5 ppc64 tree
32
33 My understanding of the various modes supported by keywest are:
34
35 - Dumb mode : not implemented, probably direct tweaking of lines
36 - Standard mode : simple i2c transaction of type
37 S Addr R/W A Data A Data ... T
38 - Standard sub mode : combined 8 bit subaddr write with data read
39 S Addr R/W A SubAddr A Data A Data ... T
40 - Combined mode : Subaddress and Data sequences appended with no stop
41 S Addr R/W A SubAddr S Addr R/W A Data A Data ... T
42
43 Currently, this driver uses only Standard mode for i2c xfer, and
44 smbus byte & quick transfers ; and uses StandardSub mode for
45 other smbus transfers instead of combined as we need that for the
46 sound driver to be happy
47*/
48
49#include <linux/config.h>
50#include <linux/module.h>
51#include <linux/kernel.h>
52#include <linux/ioport.h>
53#include <linux/pci.h>
54#include <linux/types.h>
55#include <linux/delay.h>
56#include <linux/i2c.h>
57#include <linux/init.h>
58#include <linux/mm.h>
59#include <linux/timer.h>
60#include <linux/spinlock.h>
61#include <linux/completion.h>
62#include <linux/interrupt.h>
63
64#include <asm/io.h>
65#include <asm/prom.h>
66#include <asm/machdep.h>
67#include <asm/pmac_feature.h>
68#include <asm/pmac_low_i2c.h>
69
70#include "i2c-keywest.h"
71
72#undef POLLED_MODE
73
74/* Some debug macros */
75#define WRONG_STATE(name) do {\
76 pr_debug("KW: wrong state. Got %s, state: %s (isr: %02x)\n", \
77 name, __kw_state_names[iface->state], isr); \
78 } while(0)
79
80#ifdef DEBUG
81static const char *__kw_state_names[] = {
82 "state_idle",
83 "state_addr",
84 "state_read",
85 "state_write",
86 "state_stop",
87 "state_dead"
88};
89#endif /* DEBUG */
90
91static int probe;
92
93MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
94MODULE_DESCRIPTION("I2C driver for Apple's Keywest");
95MODULE_LICENSE("GPL");
96module_param(probe, bool, 0);
97
98#ifdef POLLED_MODE
99/* Don't schedule, the g5 fan controller is too
100 * timing sensitive
101 */
102static u8
103wait_interrupt(struct keywest_iface* iface)
104{
105 int i;
106 u8 isr;
107
108 for (i = 0; i < 200000; i++) {
109 isr = read_reg(reg_isr) & KW_I2C_IRQ_MASK;
110 if (isr != 0)
111 return isr;
112 udelay(10);
113 }
114 return isr;
115}
116#endif /* POLLED_MODE */
117
118static void
119do_stop(struct keywest_iface* iface, int result)
120{
121 write_reg(reg_control, KW_I2C_CTL_STOP);
122 iface->state = state_stop;
123 iface->result = result;
124}
125
126/* Main state machine for standard & standard sub mode */
127static void
128handle_interrupt(struct keywest_iface *iface, u8 isr)
129{
130 int ack;
131
132 if (isr == 0) {
133 if (iface->state != state_stop) {
134 pr_debug("KW: Timeout !\n");
135 do_stop(iface, -EIO);
136 }
137 if (iface->state == state_stop) {
138 ack = read_reg(reg_status);
139 if (!(ack & KW_I2C_STAT_BUSY)) {
140 iface->state = state_idle;
141 write_reg(reg_ier, 0x00);
142#ifndef POLLED_MODE
143 complete(&iface->complete);
144#endif /* POLLED_MODE */
145 }
146 }
147 return;
148 }
149
150 if (isr & KW_I2C_IRQ_ADDR) {
151 ack = read_reg(reg_status);
152 if (iface->state != state_addr) {
153 write_reg(reg_isr, KW_I2C_IRQ_ADDR);
154 WRONG_STATE("KW_I2C_IRQ_ADDR");
155 do_stop(iface, -EIO);
156 return;
157 }
158 if ((ack & KW_I2C_STAT_LAST_AAK) == 0) {
159 iface->state = state_stop;
160 iface->result = -ENODEV;
161 pr_debug("KW: NAK on address\n");
162 } else {
163 /* Handle rw "quick" mode */
164 if (iface->datalen == 0) {
165 do_stop(iface, 0);
166 } else if (iface->read_write == I2C_SMBUS_READ) {
167 iface->state = state_read;
168 if (iface->datalen > 1)
169 write_reg(reg_control, KW_I2C_CTL_AAK);
170 } else {
171 iface->state = state_write;
172 write_reg(reg_data, *(iface->data++));
173 iface->datalen--;
174 }
175 }
176 write_reg(reg_isr, KW_I2C_IRQ_ADDR);
177 }
178
179 if (isr & KW_I2C_IRQ_DATA) {
180 if (iface->state == state_read) {
181 *(iface->data++) = read_reg(reg_data);
182 write_reg(reg_isr, KW_I2C_IRQ_DATA);
183 iface->datalen--;
184 if (iface->datalen == 0)
185 iface->state = state_stop;
186 else if (iface->datalen == 1)
187 write_reg(reg_control, 0);
188 } else if (iface->state == state_write) {
189 /* Check ack status */
190 ack = read_reg(reg_status);
191 if ((ack & KW_I2C_STAT_LAST_AAK) == 0) {
192 pr_debug("KW: nack on data write (%x): %x\n",
193 iface->data[-1], ack);
194 do_stop(iface, -EIO);
195 } else if (iface->datalen) {
196 write_reg(reg_data, *(iface->data++));
197 iface->datalen--;
198 } else {
199 write_reg(reg_control, KW_I2C_CTL_STOP);
200 iface->state = state_stop;
201 iface->result = 0;
202 }
203 write_reg(reg_isr, KW_I2C_IRQ_DATA);
204 } else {
205 write_reg(reg_isr, KW_I2C_IRQ_DATA);
206 WRONG_STATE("KW_I2C_IRQ_DATA");
207 if (iface->state != state_stop)
208 do_stop(iface, -EIO);
209 }
210 }
211
212 if (isr & KW_I2C_IRQ_STOP) {
213 write_reg(reg_isr, KW_I2C_IRQ_STOP);
214 if (iface->state != state_stop) {
215 WRONG_STATE("KW_I2C_IRQ_STOP");
216 iface->result = -EIO;
217 }
218 iface->state = state_idle;
219 write_reg(reg_ier, 0x00);
220#ifndef POLLED_MODE
221 complete(&iface->complete);
222#endif /* POLLED_MODE */
223 }
224
225 if (isr & KW_I2C_IRQ_START)
226 write_reg(reg_isr, KW_I2C_IRQ_START);
227}
228
229#ifndef POLLED_MODE
230
231/* Interrupt handler */
232static irqreturn_t
233keywest_irq(int irq, void *dev_id, struct pt_regs *regs)
234{
235 struct keywest_iface *iface = (struct keywest_iface *)dev_id;
236 unsigned long flags;
237
238 spin_lock_irqsave(&iface->lock, flags);
239 del_timer(&iface->timeout_timer);
240 handle_interrupt(iface, read_reg(reg_isr));
241 if (iface->state != state_idle) {
242 iface->timeout_timer.expires = jiffies + POLL_TIMEOUT;
243 add_timer(&iface->timeout_timer);
244 }
245 spin_unlock_irqrestore(&iface->lock, flags);
246 return IRQ_HANDLED;
247}
248
249static void
250keywest_timeout(unsigned long data)
251{
252 struct keywest_iface *iface = (struct keywest_iface *)data;
253 unsigned long flags;
254
255 pr_debug("timeout !\n");
256 spin_lock_irqsave(&iface->lock, flags);
257 handle_interrupt(iface, read_reg(reg_isr));
258 if (iface->state != state_idle) {
259 iface->timeout_timer.expires = jiffies + POLL_TIMEOUT;
260 add_timer(&iface->timeout_timer);
261 }
262 spin_unlock_irqrestore(&iface->lock, flags);
263}
264
265#endif /* POLLED_MODE */
266
267/*
268 * SMBUS-type transfer entrypoint
269 */
270static s32
271keywest_smbus_xfer( struct i2c_adapter* adap,
272 u16 addr,
273 unsigned short flags,
274 char read_write,
275 u8 command,
276 int size,
277 union i2c_smbus_data* data)
278{
279 struct keywest_chan* chan = i2c_get_adapdata(adap);
280 struct keywest_iface* iface = chan->iface;
281 int len;
282 u8* buffer;
283 u16 cur_word;
284 int rc = 0;
285
286 if (iface->state == state_dead)
287 return -ENXIO;
288
289 /* Prepare datas & select mode */
290 iface->cur_mode &= ~KW_I2C_MODE_MODE_MASK;
291 switch (size) {
292 case I2C_SMBUS_QUICK:
293 len = 0;
294 buffer = NULL;
295 iface->cur_mode |= KW_I2C_MODE_STANDARD;
296 break;
297 case I2C_SMBUS_BYTE:
298 len = 1;
299 buffer = &data->byte;
300 iface->cur_mode |= KW_I2C_MODE_STANDARD;
301 break;
302 case I2C_SMBUS_BYTE_DATA:
303 len = 1;
304 buffer = &data->byte;
305 iface->cur_mode |= KW_I2C_MODE_STANDARDSUB;
306 break;
307 case I2C_SMBUS_WORD_DATA:
308 len = 2;
309 cur_word = cpu_to_le16(data->word);
310 buffer = (u8 *)&cur_word;
311 iface->cur_mode |= KW_I2C_MODE_STANDARDSUB;
312 break;
313 case I2C_SMBUS_BLOCK_DATA:
314 len = data->block[0];
315 buffer = &data->block[1];
316 iface->cur_mode |= KW_I2C_MODE_STANDARDSUB;
317 break;
318 default:
319 return -1;
320 }
321
322 /* Turn a standardsub read into a combined mode access */
323 if (read_write == I2C_SMBUS_READ
324 && (iface->cur_mode & KW_I2C_MODE_MODE_MASK) == KW_I2C_MODE_STANDARDSUB) {
325 iface->cur_mode &= ~KW_I2C_MODE_MODE_MASK;
326 iface->cur_mode |= KW_I2C_MODE_COMBINED;
327 }
328
329 /* Original driver had this limitation */
330 if (len > 32)
331 len = 32;
332
333 if (pmac_low_i2c_lock(iface->node))
334 return -ENXIO;
335
336 pr_debug("chan: %d, addr: 0x%x, transfer len: %d, read: %d\n",
337 chan->chan_no, addr, len, read_write == I2C_SMBUS_READ);
338
339 iface->data = buffer;
340 iface->datalen = len;
341 iface->state = state_addr;
342 iface->result = 0;
343 iface->read_write = read_write;
344
345 /* Setup channel & clear pending irqs */
346 write_reg(reg_isr, read_reg(reg_isr));
347 write_reg(reg_mode, iface->cur_mode | (chan->chan_no << 4));
348 write_reg(reg_status, 0);
349
350 /* Set up address and r/w bit */
351 write_reg(reg_addr,
352 (addr << 1) | ((read_write == I2C_SMBUS_READ) ? 0x01 : 0x00));
353
354 /* Set up the sub address */
355 if ((iface->cur_mode & KW_I2C_MODE_MODE_MASK) == KW_I2C_MODE_STANDARDSUB
356 || (iface->cur_mode & KW_I2C_MODE_MODE_MASK) == KW_I2C_MODE_COMBINED)
357 write_reg(reg_subaddr, command);
358
359#ifndef POLLED_MODE
360 /* Arm timeout */
361 iface->timeout_timer.expires = jiffies + POLL_TIMEOUT;
362 add_timer(&iface->timeout_timer);
363#endif
364
365 /* Start sending address & enable interrupt*/
366 write_reg(reg_control, KW_I2C_CTL_XADDR);
367 write_reg(reg_ier, KW_I2C_IRQ_MASK);
368
369#ifdef POLLED_MODE
370 pr_debug("using polled mode...\n");
371 /* State machine, to turn into an interrupt handler */
372 while(iface->state != state_idle) {
373 unsigned long flags;
374
375 u8 isr = wait_interrupt(iface);
376 spin_lock_irqsave(&iface->lock, flags);
377 handle_interrupt(iface, isr);
378 spin_unlock_irqrestore(&iface->lock, flags);
379 }
380#else /* POLLED_MODE */
381 pr_debug("using interrupt mode...\n");
382 wait_for_completion(&iface->complete);
383#endif /* POLLED_MODE */
384
385 rc = iface->result;
386 pr_debug("transfer done, result: %d\n", rc);
387
388 if (rc == 0 && size == I2C_SMBUS_WORD_DATA && read_write == I2C_SMBUS_READ)
389 data->word = le16_to_cpu(cur_word);
390
391 /* Release sem */
392 pmac_low_i2c_unlock(iface->node);
393
394 return rc;
395}
396
397/*
398 * Generic i2c master transfer entrypoint
399 */
400static int
401keywest_xfer( struct i2c_adapter *adap,
402 struct i2c_msg *msgs,
403 int num)
404{
405 struct keywest_chan* chan = i2c_get_adapdata(adap);
406 struct keywest_iface* iface = chan->iface;
407 struct i2c_msg *pmsg;
408 int i, completed;
409 int rc = 0;
410
411 if (iface->state == state_dead)
412 return -ENXIO;
413
414 if (pmac_low_i2c_lock(iface->node))
415 return -ENXIO;
416
417 /* Set adapter to standard mode */
418 iface->cur_mode &= ~KW_I2C_MODE_MODE_MASK;
419 iface->cur_mode |= KW_I2C_MODE_STANDARD;
420
421 completed = 0;
422 for (i = 0; rc >= 0 && i < num;) {
423 u8 addr;
424
425 pmsg = &msgs[i++];
426 addr = pmsg->addr;
427 if (pmsg->flags & I2C_M_TEN) {
428 printk(KERN_ERR "i2c-keywest: 10 bits addr not supported !\n");
429 rc = -EINVAL;
430 break;
431 }
432 pr_debug("xfer: chan: %d, doing %s %d bytes to 0x%02x - %d of %d messages\n",
433 chan->chan_no,
434 pmsg->flags & I2C_M_RD ? "read" : "write",
435 pmsg->len, addr, i, num);
436
437 /* Setup channel & clear pending irqs */
438 write_reg(reg_mode, iface->cur_mode | (chan->chan_no << 4));
439 write_reg(reg_isr, read_reg(reg_isr));
440 write_reg(reg_status, 0);
441
442 iface->data = pmsg->buf;
443 iface->datalen = pmsg->len;
444 iface->state = state_addr;
445 iface->result = 0;
446 if (pmsg->flags & I2C_M_RD)
447 iface->read_write = I2C_SMBUS_READ;
448 else
449 iface->read_write = I2C_SMBUS_WRITE;
450
451 /* Set up address and r/w bit */
452 if (pmsg->flags & I2C_M_REV_DIR_ADDR)
453 addr ^= 1;
454 write_reg(reg_addr,
455 (addr << 1) |
456 ((iface->read_write == I2C_SMBUS_READ) ? 0x01 : 0x00));
457
458#ifndef POLLED_MODE
459 /* Arm timeout */
460 iface->timeout_timer.expires = jiffies + POLL_TIMEOUT;
461 add_timer(&iface->timeout_timer);
462#endif
463
464 /* Start sending address & enable interrupt*/
465 write_reg(reg_ier, KW_I2C_IRQ_MASK);
466 write_reg(reg_control, KW_I2C_CTL_XADDR);
467
468#ifdef POLLED_MODE
469 pr_debug("using polled mode...\n");
470 /* State machine, to turn into an interrupt handler */
471 while(iface->state != state_idle) {
472 u8 isr = wait_interrupt(iface);
473 handle_interrupt(iface, isr);
474 }
475#else /* POLLED_MODE */
476 pr_debug("using interrupt mode...\n");
477 wait_for_completion(&iface->complete);
478#endif /* POLLED_MODE */
479
480 rc = iface->result;
481 if (rc == 0)
482 completed++;
483 pr_debug("transfer done, result: %d\n", rc);
484 }
485
486 /* Release sem */
487 pmac_low_i2c_unlock(iface->node);
488
489 return completed;
490}
491
492static u32
493keywest_func(struct i2c_adapter * adapter)
494{
495 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
496 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
497 I2C_FUNC_SMBUS_BLOCK_DATA;
498}
499
500/* For now, we only handle combined mode (smbus) */
501static struct i2c_algorithm keywest_algorithm = {
502 .name = "Keywest i2c",
503 .id = I2C_ALGO_SMBUS,
504 .smbus_xfer = keywest_smbus_xfer,
505 .master_xfer = keywest_xfer,
506 .functionality = keywest_func,
507};
508
509
510static int
511create_iface(struct device_node *np, struct device *dev)
512{
513 unsigned long steps;
514 unsigned bsteps, tsize, i, nchan, addroffset;
515 struct keywest_iface* iface;
516 u32 *psteps, *prate;
517 int rc;
518
519 if (pmac_low_i2c_lock(np))
520 return -ENODEV;
521
522 psteps = (u32 *)get_property(np, "AAPL,address-step", NULL);
523 steps = psteps ? (*psteps) : 0x10;
524
525 /* Hrm... maybe we can be smarter here */
526 for (bsteps = 0; (steps & 0x01) == 0; bsteps++)
527 steps >>= 1;
528
529 if (np->parent->name[0] == 'u') {
530 nchan = 2;
531 addroffset = 3;
532 } else {
533 addroffset = 0;
534 nchan = 1;
535 }
536
537 tsize = sizeof(struct keywest_iface) +
538 (sizeof(struct keywest_chan) + 4) * nchan;
539 iface = (struct keywest_iface *) kmalloc(tsize, GFP_KERNEL);
540 if (iface == NULL) {
541 printk(KERN_ERR "i2c-keywest: can't allocate inteface !\n");
542 pmac_low_i2c_unlock(np);
543 return -ENOMEM;
544 }
545 memset(iface, 0, tsize);
546 spin_lock_init(&iface->lock);
547 init_completion(&iface->complete);
548 iface->node = of_node_get(np);
549 iface->bsteps = bsteps;
550 iface->chan_count = nchan;
551 iface->state = state_idle;
552 iface->irq = np->intrs[0].line;
553 iface->channels = (struct keywest_chan *)
554 (((unsigned long)(iface + 1) + 3UL) & ~3UL);
555 iface->base = ioremap(np->addrs[0].address + addroffset,
556 np->addrs[0].size);
557 if (!iface->base) {
558 printk(KERN_ERR "i2c-keywest: can't map inteface !\n");
559 kfree(iface);
560 pmac_low_i2c_unlock(np);
561 return -ENOMEM;
562 }
563
564#ifndef POLLED_MODE
565 init_timer(&iface->timeout_timer);
566 iface->timeout_timer.function = keywest_timeout;
567 iface->timeout_timer.data = (unsigned long)iface;
568#endif
569
570 /* Select interface rate */
571 iface->cur_mode = KW_I2C_MODE_100KHZ;
572 prate = (u32 *)get_property(np, "AAPL,i2c-rate", NULL);
573 if (prate) switch(*prate) {
574 case 100:
575 iface->cur_mode = KW_I2C_MODE_100KHZ;
576 break;
577 case 50:
578 iface->cur_mode = KW_I2C_MODE_50KHZ;
579 break;
580 case 25:
581 iface->cur_mode = KW_I2C_MODE_25KHZ;
582 break;
583 default:
584 printk(KERN_WARNING "i2c-keywest: unknown rate %ldKhz, using 100KHz\n",
585 (long)*prate);
586 }
587
588 /* Select standard mode by default */
589 iface->cur_mode |= KW_I2C_MODE_STANDARD;
590
591 /* Write mode */
592 write_reg(reg_mode, iface->cur_mode);
593
594 /* Switch interrupts off & clear them*/
595 write_reg(reg_ier, 0x00);
596 write_reg(reg_isr, KW_I2C_IRQ_MASK);
597
598#ifndef POLLED_MODE
599 /* Request chip interrupt */
600 rc = request_irq(iface->irq, keywest_irq, SA_INTERRUPT, "keywest i2c", iface);
601 if (rc) {
602 printk(KERN_ERR "i2c-keywest: can't get IRQ %d !\n", iface->irq);
603 iounmap(iface->base);
604 kfree(iface);
605 pmac_low_i2c_unlock(np);
606 return -ENODEV;
607 }
608#endif /* POLLED_MODE */
609
610 pmac_low_i2c_unlock(np);
611 dev_set_drvdata(dev, iface);
612
613 for (i=0; i<nchan; i++) {
614 struct keywest_chan* chan = &iface->channels[i];
615 u8 addr;
616
617 sprintf(chan->adapter.name, "%s %d", np->parent->name, i);
618 chan->iface = iface;
619 chan->chan_no = i;
620 chan->adapter.id = I2C_ALGO_SMBUS;
621 chan->adapter.algo = &keywest_algorithm;
622 chan->adapter.algo_data = NULL;
623 chan->adapter.client_register = NULL;
624 chan->adapter.client_unregister = NULL;
625 i2c_set_adapdata(&chan->adapter, chan);
626 chan->adapter.dev.parent = dev;
627
628 rc = i2c_add_adapter(&chan->adapter);
629 if (rc) {
630 printk("i2c-keywest.c: Adapter %s registration failed\n",
631 chan->adapter.name);
632 i2c_set_adapdata(&chan->adapter, NULL);
633 }
634 if (probe) {
635 printk("Probe: ");
636 for (addr = 0x00; addr <= 0x7f; addr++) {
637 if (i2c_smbus_xfer(&chan->adapter,addr,
638 0,0,0,I2C_SMBUS_QUICK,NULL) >= 0)
639 printk("%02x ", addr);
640 }
641 printk("\n");
642 }
643 }
644
645 printk(KERN_INFO "Found KeyWest i2c on \"%s\", %d channel%s, stepping: %d bits\n",
646 np->parent->name, nchan, nchan > 1 ? "s" : "", bsteps);
647
648 return 0;
649}
650
651static int
652dispose_iface(struct device *dev)
653{
654 struct keywest_iface *iface = dev_get_drvdata(dev);
655 int i, rc;
656
657 /* Make sure we stop all activity */
658 if (pmac_low_i2c_lock(iface->node))
659 return -ENODEV;
660
661#ifndef POLLED_MODE
662 spin_lock_irq(&iface->lock);
663 while (iface->state != state_idle) {
664 spin_unlock_irq(&iface->lock);
665 msleep(100);
666 spin_lock_irq(&iface->lock);
667 }
668#endif /* POLLED_MODE */
669 iface->state = state_dead;
670#ifndef POLLED_MODE
671 spin_unlock_irq(&iface->lock);
672 free_irq(iface->irq, iface);
673#endif /* POLLED_MODE */
674
675 pmac_low_i2c_unlock(iface->node);
676
677 /* Release all channels */
678 for (i=0; i<iface->chan_count; i++) {
679 struct keywest_chan* chan = &iface->channels[i];
680 if (i2c_get_adapdata(&chan->adapter) == NULL)
681 continue;
682 rc = i2c_del_adapter(&chan->adapter);
683 i2c_set_adapdata(&chan->adapter, NULL);
684 /* We aren't that prepared to deal with this... */
685 if (rc)
686 printk("i2c-keywest.c: i2c_del_adapter failed, that's bad !\n");
687 }
688 iounmap(iface->base);
689 dev_set_drvdata(dev, NULL);
690 of_node_put(iface->node);
691 kfree(iface);
692
693 return 0;
694}
695
696static int
697create_iface_macio(struct macio_dev* dev, const struct of_match *match)
698{
699 return create_iface(dev->ofdev.node, &dev->ofdev.dev);
700}
701
702static int
703dispose_iface_macio(struct macio_dev* dev)
704{
705 return dispose_iface(&dev->ofdev.dev);
706}
707
708static int
709create_iface_of_platform(struct of_device* dev, const struct of_match *match)
710{
711 return create_iface(dev->node, &dev->dev);
712}
713
714static int
715dispose_iface_of_platform(struct of_device* dev)
716{
717 return dispose_iface(&dev->dev);
718}
719
720static struct of_match i2c_keywest_match[] =
721{
722 {
723 .name = OF_ANY_MATCH,
724 .type = "i2c",
725 .compatible = "keywest"
726 },
727 {},
728};
729
730static struct macio_driver i2c_keywest_macio_driver =
731{
732 .name = "i2c-keywest",
733 .match_table = i2c_keywest_match,
734 .probe = create_iface_macio,
735 .remove = dispose_iface_macio
736};
737
738static struct of_platform_driver i2c_keywest_of_platform_driver =
739{
740 .name = "i2c-keywest",
741 .match_table = i2c_keywest_match,
742 .probe = create_iface_of_platform,
743 .remove = dispose_iface_of_platform
744};
745
746static int __init
747i2c_keywest_init(void)
748{
749 of_register_driver(&i2c_keywest_of_platform_driver);
750 macio_register_driver(&i2c_keywest_macio_driver);
751
752 return 0;
753}
754
755static void __exit
756i2c_keywest_cleanup(void)
757{
758 of_unregister_driver(&i2c_keywest_of_platform_driver);
759 macio_unregister_driver(&i2c_keywest_macio_driver);
760}
761
762module_init(i2c_keywest_init);
763module_exit(i2c_keywest_cleanup);
diff --git a/drivers/i2c/busses/i2c-keywest.h b/drivers/i2c/busses/i2c-keywest.h
new file mode 100644
index 000000000000..c5022e1ca6ff
--- /dev/null
+++ b/drivers/i2c/busses/i2c-keywest.h
@@ -0,0 +1,108 @@
1#ifndef __I2C_KEYWEST_H__
2#define __I2C_KEYWEST_H__
3
4/* The Tumbler audio equalizer can be really slow sometimes */
5#define POLL_TIMEOUT (2*HZ)
6
7/* Register indices */
8typedef enum {
9 reg_mode = 0,
10 reg_control,
11 reg_status,
12 reg_isr,
13 reg_ier,
14 reg_addr,
15 reg_subaddr,
16 reg_data
17} reg_t;
18
19
20/* Mode register */
21#define KW_I2C_MODE_100KHZ 0x00
22#define KW_I2C_MODE_50KHZ 0x01
23#define KW_I2C_MODE_25KHZ 0x02
24#define KW_I2C_MODE_DUMB 0x00
25#define KW_I2C_MODE_STANDARD 0x04
26#define KW_I2C_MODE_STANDARDSUB 0x08
27#define KW_I2C_MODE_COMBINED 0x0C
28#define KW_I2C_MODE_MODE_MASK 0x0C
29#define KW_I2C_MODE_CHAN_MASK 0xF0
30
31/* Control register */
32#define KW_I2C_CTL_AAK 0x01
33#define KW_I2C_CTL_XADDR 0x02
34#define KW_I2C_CTL_STOP 0x04
35#define KW_I2C_CTL_START 0x08
36
37/* Status register */
38#define KW_I2C_STAT_BUSY 0x01
39#define KW_I2C_STAT_LAST_AAK 0x02
40#define KW_I2C_STAT_LAST_RW 0x04
41#define KW_I2C_STAT_SDA 0x08
42#define KW_I2C_STAT_SCL 0x10
43
44/* IER & ISR registers */
45#define KW_I2C_IRQ_DATA 0x01
46#define KW_I2C_IRQ_ADDR 0x02
47#define KW_I2C_IRQ_STOP 0x04
48#define KW_I2C_IRQ_START 0x08
49#define KW_I2C_IRQ_MASK 0x0F
50
51/* Physical interface */
52struct keywest_iface
53{
54 struct device_node *node;
55 void __iomem * base;
56 unsigned bsteps;
57 int irq;
58 spinlock_t lock;
59 struct keywest_chan *channels;
60 unsigned chan_count;
61 u8 cur_mode;
62 char read_write;
63 u8 *data;
64 unsigned datalen;
65 int state;
66 int result;
67 struct timer_list timeout_timer;
68 struct completion complete;
69};
70
71enum {
72 state_idle,
73 state_addr,
74 state_read,
75 state_write,
76 state_stop,
77 state_dead
78};
79
80/* Channel on an interface */
81struct keywest_chan
82{
83 struct i2c_adapter adapter;
84 struct keywest_iface* iface;
85 unsigned chan_no;
86};
87
88/* Register access */
89
90static inline u8 __read_reg(struct keywest_iface *iface, reg_t reg)
91{
92 return in_8(iface->base
93 + (((unsigned)reg) << iface->bsteps));
94}
95
96static inline void __write_reg(struct keywest_iface *iface, reg_t reg, u8 val)
97{
98 out_8(iface->base
99 + (((unsigned)reg) << iface->bsteps), val);
100 (void)__read_reg(iface, reg_subaddr);
101}
102
103#define write_reg(reg, val) __write_reg(iface, reg, val)
104#define read_reg(reg) __read_reg(iface, reg)
105
106
107
108#endif /* __I2C_KEYWEST_H__ */
diff --git a/drivers/i2c/busses/i2c-mpc.c b/drivers/i2c/busses/i2c-mpc.c
new file mode 100644
index 000000000000..75b8d867dae1
--- /dev/null
+++ b/drivers/i2c/busses/i2c-mpc.c
@@ -0,0 +1,496 @@
1/*
2 * (C) Copyright 2003-2004
3 * Humboldt Solutions Ltd, adrian@humboldt.co.uk.
4
5 * This is a combined i2c adapter and algorithm driver for the
6 * MPC107/Tsi107 PowerPC northbridge and processors that include
7 * the same I2C unit (8240, 8245, 85xx).
8 *
9 * Release 0.8
10 *
11 * This file is licensed under the terms of the GNU General Public
12 * License version 2. This program is licensed "as is" without any
13 * warranty of any kind, whether express or implied.
14 */
15
16#include <linux/config.h>
17#include <linux/kernel.h>
18#include <linux/module.h>
19#include <linux/sched.h>
20#include <linux/init.h>
21#include <linux/pci.h>
22#include <asm/io.h>
23#ifdef CONFIG_FSL_OCP
24#include <asm/ocp.h>
25#define FSL_I2C_DEV_SEPARATE_DFSRR FS_I2C_SEPARATE_DFSRR
26#define FSL_I2C_DEV_CLOCK_5200 FS_I2C_CLOCK_5200
27#else
28#include <linux/fsl_devices.h>
29#endif
30#include <linux/i2c.h>
31#include <linux/interrupt.h>
32#include <linux/delay.h>
33
34#define MPC_I2C_ADDR 0x00
35#define MPC_I2C_FDR 0x04
36#define MPC_I2C_CR 0x08
37#define MPC_I2C_SR 0x0c
38#define MPC_I2C_DR 0x10
39#define MPC_I2C_DFSRR 0x14
40#define MPC_I2C_REGION 0x20
41
42#define CCR_MEN 0x80
43#define CCR_MIEN 0x40
44#define CCR_MSTA 0x20
45#define CCR_MTX 0x10
46#define CCR_TXAK 0x08
47#define CCR_RSTA 0x04
48
49#define CSR_MCF 0x80
50#define CSR_MAAS 0x40
51#define CSR_MBB 0x20
52#define CSR_MAL 0x10
53#define CSR_SRW 0x04
54#define CSR_MIF 0x02
55#define CSR_RXAK 0x01
56
57struct mpc_i2c {
58 char *base;
59 u32 interrupt;
60 wait_queue_head_t queue;
61 struct i2c_adapter adap;
62 int irq;
63 u32 flags;
64};
65
66static __inline__ void writeccr(struct mpc_i2c *i2c, u32 x)
67{
68 writeb(x, i2c->base + MPC_I2C_CR);
69}
70
71static irqreturn_t mpc_i2c_isr(int irq, void *dev_id, struct pt_regs *regs)
72{
73 struct mpc_i2c *i2c = dev_id;
74 if (readb(i2c->base + MPC_I2C_SR) & CSR_MIF) {
75 /* Read again to allow register to stabilise */
76 i2c->interrupt = readb(i2c->base + MPC_I2C_SR);
77 writeb(0, i2c->base + MPC_I2C_SR);
78 wake_up_interruptible(&i2c->queue);
79 }
80 return IRQ_HANDLED;
81}
82
83static int i2c_wait(struct mpc_i2c *i2c, unsigned timeout, int writing)
84{
85 unsigned long orig_jiffies = jiffies;
86 u32 x;
87 int result = 0;
88
89 if (i2c->irq == 0)
90 {
91 while (!(readb(i2c->base + MPC_I2C_SR) & CSR_MIF)) {
92 schedule();
93 if (time_after(jiffies, orig_jiffies + timeout)) {
94 pr_debug("I2C: timeout\n");
95 result = -EIO;
96 break;
97 }
98 }
99 x = readb(i2c->base + MPC_I2C_SR);
100 writeb(0, i2c->base + MPC_I2C_SR);
101 } else {
102 /* Interrupt mode */
103 result = wait_event_interruptible_timeout(i2c->queue,
104 (i2c->interrupt & CSR_MIF), timeout * HZ);
105
106 if (unlikely(result < 0))
107 pr_debug("I2C: wait interrupted\n");
108 else if (unlikely(!(i2c->interrupt & CSR_MIF))) {
109 pr_debug("I2C: wait timeout\n");
110 result = -ETIMEDOUT;
111 }
112
113 x = i2c->interrupt;
114 i2c->interrupt = 0;
115 }
116
117 if (result < 0)
118 return result;
119
120 if (!(x & CSR_MCF)) {
121 pr_debug("I2C: unfinished\n");
122 return -EIO;
123 }
124
125 if (x & CSR_MAL) {
126 pr_debug("I2C: MAL\n");
127 return -EIO;
128 }
129
130 if (writing && (x & CSR_RXAK)) {
131 pr_debug("I2C: No RXAK\n");
132 /* generate stop */
133 writeccr(i2c, CCR_MEN);
134 return -EIO;
135 }
136 return 0;
137}
138
139static void mpc_i2c_setclock(struct mpc_i2c *i2c)
140{
141 /* Set clock and filters */
142 if (i2c->flags & FSL_I2C_DEV_SEPARATE_DFSRR) {
143 writeb(0x31, i2c->base + MPC_I2C_FDR);
144 writeb(0x10, i2c->base + MPC_I2C_DFSRR);
145 } else if (i2c->flags & FSL_I2C_DEV_CLOCK_5200)
146 writeb(0x3f, i2c->base + MPC_I2C_FDR);
147 else
148 writel(0x1031, i2c->base + MPC_I2C_FDR);
149}
150
151static void mpc_i2c_start(struct mpc_i2c *i2c)
152{
153 /* Clear arbitration */
154 writeb(0, i2c->base + MPC_I2C_SR);
155 /* Start with MEN */
156 writeccr(i2c, CCR_MEN);
157}
158
159static void mpc_i2c_stop(struct mpc_i2c *i2c)
160{
161 writeccr(i2c, CCR_MEN);
162}
163
164static int mpc_write(struct mpc_i2c *i2c, int target,
165 const u8 * data, int length, int restart)
166{
167 int i;
168 unsigned timeout = i2c->adap.timeout;
169 u32 flags = restart ? CCR_RSTA : 0;
170
171 /* Start with MEN */
172 if (!restart)
173 writeccr(i2c, CCR_MEN);
174 /* Start as master */
175 writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_MSTA | CCR_MTX | flags);
176 /* Write target byte */
177 writeb((target << 1), i2c->base + MPC_I2C_DR);
178
179 if (i2c_wait(i2c, timeout, 1) < 0)
180 return -1;
181
182 for (i = 0; i < length; i++) {
183 /* Write data byte */
184 writeb(data[i], i2c->base + MPC_I2C_DR);
185
186 if (i2c_wait(i2c, timeout, 1) < 0)
187 return -1;
188 }
189
190 return 0;
191}
192
193static int mpc_read(struct mpc_i2c *i2c, int target,
194 u8 * data, int length, int restart)
195{
196 unsigned timeout = i2c->adap.timeout;
197 int i;
198 u32 flags = restart ? CCR_RSTA : 0;
199
200 /* Start with MEN */
201 if (!restart)
202 writeccr(i2c, CCR_MEN);
203 /* Switch to read - restart */
204 writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_MSTA | CCR_MTX | flags);
205 /* Write target address byte - this time with the read flag set */
206 writeb((target << 1) | 1, i2c->base + MPC_I2C_DR);
207
208 if (i2c_wait(i2c, timeout, 1) < 0)
209 return -1;
210
211 if (length) {
212 if (length == 1)
213 writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_MSTA | CCR_TXAK);
214 else
215 writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_MSTA);
216 /* Dummy read */
217 readb(i2c->base + MPC_I2C_DR);
218 }
219
220 for (i = 0; i < length; i++) {
221 if (i2c_wait(i2c, timeout, 0) < 0)
222 return -1;
223
224 /* Generate txack on next to last byte */
225 if (i == length - 2)
226 writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_MSTA | CCR_TXAK);
227 /* Generate stop on last byte */
228 if (i == length - 1)
229 writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_TXAK);
230 data[i] = readb(i2c->base + MPC_I2C_DR);
231 }
232
233 return length;
234}
235
236static int mpc_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
237{
238 struct i2c_msg *pmsg;
239 int i;
240 int ret = 0;
241 unsigned long orig_jiffies = jiffies;
242 struct mpc_i2c *i2c = i2c_get_adapdata(adap);
243
244 mpc_i2c_start(i2c);
245
246 /* Allow bus up to 1s to become not busy */
247 while (readb(i2c->base + MPC_I2C_SR) & CSR_MBB) {
248 if (signal_pending(current)) {
249 pr_debug("I2C: Interrupted\n");
250 return -EINTR;
251 }
252 if (time_after(jiffies, orig_jiffies + HZ)) {
253 pr_debug("I2C: timeout\n");
254 return -EIO;
255 }
256 schedule();
257 }
258
259 for (i = 0; ret >= 0 && i < num; i++) {
260 pmsg = &msgs[i];
261 pr_debug("Doing %s %d bytes to 0x%02x - %d of %d messages\n",
262 pmsg->flags & I2C_M_RD ? "read" : "write",
263 pmsg->len, pmsg->addr, i + 1, num);
264 if (pmsg->flags & I2C_M_RD)
265 ret =
266 mpc_read(i2c, pmsg->addr, pmsg->buf, pmsg->len, i);
267 else
268 ret =
269 mpc_write(i2c, pmsg->addr, pmsg->buf, pmsg->len, i);
270 }
271 mpc_i2c_stop(i2c);
272 return (ret < 0) ? ret : num;
273}
274
275static u32 mpc_functionality(struct i2c_adapter *adap)
276{
277 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
278}
279
280static struct i2c_algorithm mpc_algo = {
281 .name = "MPC algorithm",
282 .id = I2C_ALGO_MPC107,
283 .master_xfer = mpc_xfer,
284 .functionality = mpc_functionality,
285};
286
287static struct i2c_adapter mpc_ops = {
288 .owner = THIS_MODULE,
289 .name = "MPC adapter",
290 .id = I2C_ALGO_MPC107 | I2C_HW_MPC107,
291 .algo = &mpc_algo,
292 .class = I2C_CLASS_HWMON,
293 .timeout = 1,
294 .retries = 1
295};
296
297#ifdef CONFIG_FSL_OCP
298static int __devinit mpc_i2c_probe(struct ocp_device *ocp)
299{
300 int result = 0;
301 struct mpc_i2c *i2c;
302
303 if (!(i2c = kmalloc(sizeof(*i2c), GFP_KERNEL))) {
304 return -ENOMEM;
305 }
306 memset(i2c, 0, sizeof(*i2c));
307
308 i2c->irq = ocp->def->irq;
309 i2c->flags = ((struct ocp_fs_i2c_data *)ocp->def->additions)->flags;
310 init_waitqueue_head(&i2c->queue);
311
312 if (!request_mem_region(ocp->def->paddr, MPC_I2C_REGION, "i2c-mpc")) {
313 printk(KERN_ERR "i2c-mpc - resource unavailable\n");
314 return -ENODEV;
315 }
316
317 i2c->base = ioremap(ocp->def->paddr, MPC_I2C_REGION);
318
319 if (!i2c->base) {
320 printk(KERN_ERR "i2c-mpc - failed to map controller\n");
321 result = -ENOMEM;
322 goto fail_map;
323 }
324
325 if (i2c->irq != OCP_IRQ_NA)
326 {
327 if ((result = request_irq(ocp->def->irq, mpc_i2c_isr,
328 0, "i2c-mpc", i2c)) < 0) {
329 printk(KERN_ERR
330 "i2c-mpc - failed to attach interrupt\n");
331 goto fail_irq;
332 }
333 } else
334 i2c->irq = 0;
335
336 i2c->adap = mpc_ops;
337 i2c_set_adapdata(&i2c->adap, i2c);
338
339 if ((result = i2c_add_adapter(&i2c->adap)) < 0) {
340 printk(KERN_ERR "i2c-mpc - failed to add adapter\n");
341 goto fail_add;
342 }
343
344 mpc_i2c_setclock(i2c);
345 ocp_set_drvdata(ocp, i2c);
346 return result;
347
348 fail_add:
349 if (ocp->def->irq != OCP_IRQ_NA)
350 free_irq(ocp->def->irq, 0);
351 fail_irq:
352 iounmap(i2c->base);
353 fail_map:
354 release_mem_region(ocp->def->paddr, MPC_I2C_REGION);
355 kfree(i2c);
356 return result;
357}
358static void __devexit mpc_i2c_remove(struct ocp_device *ocp)
359{
360 struct mpc_i2c *i2c = ocp_get_drvdata(ocp);
361 ocp_set_drvdata(ocp, NULL);
362 i2c_del_adapter(&i2c->adap);
363
364 if (ocp->def->irq != OCP_IRQ_NA)
365 free_irq(i2c->irq, i2c);
366 iounmap(i2c->base);
367 release_mem_region(ocp->def->paddr, MPC_I2C_REGION);
368 kfree(i2c);
369}
370
371static struct ocp_device_id mpc_iic_ids[] __devinitdata = {
372 {.vendor = OCP_VENDOR_FREESCALE,.function = OCP_FUNC_IIC},
373 {.vendor = OCP_VENDOR_INVALID}
374};
375
376MODULE_DEVICE_TABLE(ocp, mpc_iic_ids);
377
378static struct ocp_driver mpc_iic_driver = {
379 .name = "iic",
380 .id_table = mpc_iic_ids,
381 .probe = mpc_i2c_probe,
382 .remove = __devexit_p(mpc_i2c_remove)
383};
384
385static int __init iic_init(void)
386{
387 return ocp_register_driver(&mpc_iic_driver);
388}
389
390static void __exit iic_exit(void)
391{
392 ocp_unregister_driver(&mpc_iic_driver);
393}
394
395module_init(iic_init);
396module_exit(iic_exit);
397#else
398static int fsl_i2c_probe(struct device *device)
399{
400 int result = 0;
401 struct mpc_i2c *i2c;
402 struct platform_device *pdev = to_platform_device(device);
403 struct fsl_i2c_platform_data *pdata;
404 struct resource *r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
405
406 pdata = (struct fsl_i2c_platform_data *) pdev->dev.platform_data;
407
408 if (!(i2c = kmalloc(sizeof(*i2c), GFP_KERNEL))) {
409 return -ENOMEM;
410 }
411 memset(i2c, 0, sizeof(*i2c));
412
413 i2c->irq = platform_get_irq(pdev, 0);
414 i2c->flags = pdata->device_flags;
415 init_waitqueue_head(&i2c->queue);
416
417 i2c->base = ioremap((phys_addr_t)r->start, MPC_I2C_REGION);
418
419 if (!i2c->base) {
420 printk(KERN_ERR "i2c-mpc - failed to map controller\n");
421 result = -ENOMEM;
422 goto fail_map;
423 }
424
425 if (i2c->irq != 0)
426 if ((result = request_irq(i2c->irq, mpc_i2c_isr,
427 0, "fsl-i2c", i2c)) < 0) {
428 printk(KERN_ERR
429 "i2c-mpc - failed to attach interrupt\n");
430 goto fail_irq;
431 }
432
433 i2c->adap = mpc_ops;
434 i2c_set_adapdata(&i2c->adap, i2c);
435 i2c->adap.dev.parent = &pdev->dev;
436 if ((result = i2c_add_adapter(&i2c->adap)) < 0) {
437 printk(KERN_ERR "i2c-mpc - failed to add adapter\n");
438 goto fail_add;
439 }
440
441 mpc_i2c_setclock(i2c);
442 dev_set_drvdata(device, i2c);
443 return result;
444
445 fail_add:
446 if (i2c->irq != 0)
447 free_irq(i2c->irq, 0);
448 fail_irq:
449 iounmap(i2c->base);
450 fail_map:
451 kfree(i2c);
452 return result;
453};
454
455static int fsl_i2c_remove(struct device *device)
456{
457 struct mpc_i2c *i2c = dev_get_drvdata(device);
458
459 dev_set_drvdata(device, NULL);
460 i2c_del_adapter(&i2c->adap);
461
462 if (i2c->irq != 0)
463 free_irq(i2c->irq, i2c);
464
465 iounmap(i2c->base);
466 kfree(i2c);
467 return 0;
468};
469
470/* Structure for a device driver */
471static struct device_driver fsl_i2c_driver = {
472 .name = "fsl-i2c",
473 .bus = &platform_bus_type,
474 .probe = fsl_i2c_probe,
475 .remove = fsl_i2c_remove,
476};
477
478static int __init fsl_i2c_init(void)
479{
480 return driver_register(&fsl_i2c_driver);
481}
482
483static void __exit fsl_i2c_exit(void)
484{
485 driver_unregister(&fsl_i2c_driver);
486}
487
488module_init(fsl_i2c_init);
489module_exit(fsl_i2c_exit);
490
491#endif /* CONFIG_FSL_OCP */
492
493MODULE_AUTHOR("Adrian Cox <adrian@humboldt.co.uk>");
494MODULE_DESCRIPTION
495 ("I2C-Bus adapter for MPC107 bridge and MPC824x/85xx/52xx processors");
496MODULE_LICENSE("GPL");
diff --git a/drivers/i2c/busses/i2c-mv64xxx.c b/drivers/i2c/busses/i2c-mv64xxx.c
new file mode 100644
index 000000000000..5b852782d2f5
--- /dev/null
+++ b/drivers/i2c/busses/i2c-mv64xxx.c
@@ -0,0 +1,598 @@
1/*
2 * drivers/i2c/busses/i2c-mv64xxx.c
3 *
4 * Driver for the i2c controller on the Marvell line of host bridges for MIPS
5 * and PPC (e.g, gt642[46]0, mv643[46]0, mv644[46]0).
6 *
7 * Author: Mark A. Greer <mgreer@mvista.com>
8 *
9 * 2005 (c) MontaVista, Software, Inc. This file is licensed under
10 * the terms of the GNU General Public License version 2. This program
11 * is licensed "as is" without any warranty of any kind, whether express
12 * or implied.
13 */
14#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/spinlock.h>
17#include <linux/i2c.h>
18#include <linux/interrupt.h>
19#include <linux/mv643xx.h>
20#include <asm/io.h>
21
22/* Register defines */
23#define MV64XXX_I2C_REG_SLAVE_ADDR 0x00
24#define MV64XXX_I2C_REG_DATA 0x04
25#define MV64XXX_I2C_REG_CONTROL 0x08
26#define MV64XXX_I2C_REG_STATUS 0x0c
27#define MV64XXX_I2C_REG_BAUD 0x0c
28#define MV64XXX_I2C_REG_EXT_SLAVE_ADDR 0x10
29#define MV64XXX_I2C_REG_SOFT_RESET 0x1c
30
31#define MV64XXX_I2C_REG_CONTROL_ACK 0x00000004
32#define MV64XXX_I2C_REG_CONTROL_IFLG 0x00000008
33#define MV64XXX_I2C_REG_CONTROL_STOP 0x00000010
34#define MV64XXX_I2C_REG_CONTROL_START 0x00000020
35#define MV64XXX_I2C_REG_CONTROL_TWSIEN 0x00000040
36#define MV64XXX_I2C_REG_CONTROL_INTEN 0x00000080
37
38/* Ctlr status values */
39#define MV64XXX_I2C_STATUS_BUS_ERR 0x00
40#define MV64XXX_I2C_STATUS_MAST_START 0x08
41#define MV64XXX_I2C_STATUS_MAST_REPEAT_START 0x10
42#define MV64XXX_I2C_STATUS_MAST_WR_ADDR_ACK 0x18
43#define MV64XXX_I2C_STATUS_MAST_WR_ADDR_NO_ACK 0x20
44#define MV64XXX_I2C_STATUS_MAST_WR_ACK 0x28
45#define MV64XXX_I2C_STATUS_MAST_WR_NO_ACK 0x30
46#define MV64XXX_I2C_STATUS_MAST_LOST_ARB 0x38
47#define MV64XXX_I2C_STATUS_MAST_RD_ADDR_ACK 0x40
48#define MV64XXX_I2C_STATUS_MAST_RD_ADDR_NO_ACK 0x48
49#define MV64XXX_I2C_STATUS_MAST_RD_DATA_ACK 0x50
50#define MV64XXX_I2C_STATUS_MAST_RD_DATA_NO_ACK 0x58
51#define MV64XXX_I2C_STATUS_MAST_WR_ADDR_2_ACK 0xd0
52#define MV64XXX_I2C_STATUS_MAST_WR_ADDR_2_NO_ACK 0xd8
53#define MV64XXX_I2C_STATUS_MAST_RD_ADDR_2_ACK 0xe0
54#define MV64XXX_I2C_STATUS_MAST_RD_ADDR_2_NO_ACK 0xe8
55#define MV64XXX_I2C_STATUS_NO_STATUS 0xf8
56
57/* Driver states */
58enum {
59 MV64XXX_I2C_STATE_INVALID,
60 MV64XXX_I2C_STATE_IDLE,
61 MV64XXX_I2C_STATE_WAITING_FOR_START_COND,
62 MV64XXX_I2C_STATE_WAITING_FOR_ADDR_1_ACK,
63 MV64XXX_I2C_STATE_WAITING_FOR_ADDR_2_ACK,
64 MV64XXX_I2C_STATE_WAITING_FOR_SLAVE_ACK,
65 MV64XXX_I2C_STATE_WAITING_FOR_SLAVE_DATA,
66 MV64XXX_I2C_STATE_ABORTING,
67};
68
69/* Driver actions */
70enum {
71 MV64XXX_I2C_ACTION_INVALID,
72 MV64XXX_I2C_ACTION_CONTINUE,
73 MV64XXX_I2C_ACTION_SEND_START,
74 MV64XXX_I2C_ACTION_SEND_ADDR_1,
75 MV64XXX_I2C_ACTION_SEND_ADDR_2,
76 MV64XXX_I2C_ACTION_SEND_DATA,
77 MV64XXX_I2C_ACTION_RCV_DATA,
78 MV64XXX_I2C_ACTION_RCV_DATA_STOP,
79 MV64XXX_I2C_ACTION_SEND_STOP,
80};
81
82struct mv64xxx_i2c_data {
83 int irq;
84 u32 state;
85 u32 action;
86 u32 cntl_bits;
87 void __iomem *reg_base;
88 u32 reg_base_p;
89 u32 addr1;
90 u32 addr2;
91 u32 bytes_left;
92 u32 byte_posn;
93 u32 block;
94 int rc;
95 u32 freq_m;
96 u32 freq_n;
97 wait_queue_head_t waitq;
98 spinlock_t lock;
99 struct i2c_msg *msg;
100 struct i2c_adapter adapter;
101};
102
103/*
104 *****************************************************************************
105 *
106 * Finite State Machine & Interrupt Routines
107 *
108 *****************************************************************************
109 */
110static void
111mv64xxx_i2c_fsm(struct mv64xxx_i2c_data *drv_data, u32 status)
112{
113 /*
114 * If state is idle, then this is likely the remnants of an old
115 * operation that driver has given up on or the user has killed.
116 * If so, issue the stop condition and go to idle.
117 */
118 if (drv_data->state == MV64XXX_I2C_STATE_IDLE) {
119 drv_data->action = MV64XXX_I2C_ACTION_SEND_STOP;
120 return;
121 }
122
123 if (drv_data->state == MV64XXX_I2C_STATE_ABORTING) {
124 drv_data->action = MV64XXX_I2C_ACTION_SEND_STOP;
125 drv_data->state = MV64XXX_I2C_STATE_IDLE;
126 return;
127 }
128
129 /* The status from the ctlr [mostly] tells us what to do next */
130 switch (status) {
131 /* Start condition interrupt */
132 case MV64XXX_I2C_STATUS_MAST_START: /* 0x08 */
133 case MV64XXX_I2C_STATUS_MAST_REPEAT_START: /* 0x10 */
134 drv_data->action = MV64XXX_I2C_ACTION_SEND_ADDR_1;
135 drv_data->state = MV64XXX_I2C_STATE_WAITING_FOR_ADDR_1_ACK;
136 break;
137
138 /* Performing a write */
139 case MV64XXX_I2C_STATUS_MAST_WR_ADDR_ACK: /* 0x18 */
140 if (drv_data->msg->flags & I2C_M_TEN) {
141 drv_data->action = MV64XXX_I2C_ACTION_SEND_ADDR_2;
142 drv_data->state =
143 MV64XXX_I2C_STATE_WAITING_FOR_ADDR_2_ACK;
144 break;
145 }
146 /* FALLTHRU */
147 case MV64XXX_I2C_STATUS_MAST_WR_ADDR_2_ACK: /* 0xd0 */
148 case MV64XXX_I2C_STATUS_MAST_WR_ACK: /* 0x28 */
149 if (drv_data->bytes_left > 0) {
150 drv_data->action = MV64XXX_I2C_ACTION_SEND_DATA;
151 drv_data->state =
152 MV64XXX_I2C_STATE_WAITING_FOR_SLAVE_ACK;
153 drv_data->bytes_left--;
154 } else {
155 drv_data->action = MV64XXX_I2C_ACTION_SEND_STOP;
156 drv_data->state = MV64XXX_I2C_STATE_IDLE;
157 }
158 break;
159
160 /* Performing a read */
161 case MV64XXX_I2C_STATUS_MAST_RD_ADDR_ACK: /* 40 */
162 if (drv_data->msg->flags & I2C_M_TEN) {
163 drv_data->action = MV64XXX_I2C_ACTION_SEND_ADDR_2;
164 drv_data->state =
165 MV64XXX_I2C_STATE_WAITING_FOR_ADDR_2_ACK;
166 break;
167 }
168 /* FALLTHRU */
169 case MV64XXX_I2C_STATUS_MAST_RD_ADDR_2_ACK: /* 0xe0 */
170 if (drv_data->bytes_left == 0) {
171 drv_data->action = MV64XXX_I2C_ACTION_SEND_STOP;
172 drv_data->state = MV64XXX_I2C_STATE_IDLE;
173 break;
174 }
175 /* FALLTHRU */
176 case MV64XXX_I2C_STATUS_MAST_RD_DATA_ACK: /* 0x50 */
177 if (status != MV64XXX_I2C_STATUS_MAST_RD_DATA_ACK)
178 drv_data->action = MV64XXX_I2C_ACTION_CONTINUE;
179 else {
180 drv_data->action = MV64XXX_I2C_ACTION_RCV_DATA;
181 drv_data->bytes_left--;
182 }
183 drv_data->state = MV64XXX_I2C_STATE_WAITING_FOR_SLAVE_DATA;
184
185 if (drv_data->bytes_left == 1)
186 drv_data->cntl_bits &= ~MV64XXX_I2C_REG_CONTROL_ACK;
187 break;
188
189 case MV64XXX_I2C_STATUS_MAST_RD_DATA_NO_ACK: /* 0x58 */
190 drv_data->action = MV64XXX_I2C_ACTION_RCV_DATA_STOP;
191 drv_data->state = MV64XXX_I2C_STATE_IDLE;
192 break;
193
194 case MV64XXX_I2C_STATUS_MAST_WR_ADDR_NO_ACK: /* 0x20 */
195 case MV64XXX_I2C_STATUS_MAST_WR_NO_ACK: /* 30 */
196 case MV64XXX_I2C_STATUS_MAST_RD_ADDR_NO_ACK: /* 48 */
197 /* Doesn't seem to be a device at other end */
198 drv_data->action = MV64XXX_I2C_ACTION_SEND_STOP;
199 drv_data->state = MV64XXX_I2C_STATE_IDLE;
200 drv_data->rc = -ENODEV;
201 break;
202
203 default:
204 dev_err(&drv_data->adapter.dev,
205 "mv64xxx_i2c_fsm: Ctlr Error -- state: 0x%x, "
206 "status: 0x%x, addr: 0x%x, flags: 0x%x\n",
207 drv_data->state, status, drv_data->msg->addr,
208 drv_data->msg->flags);
209 drv_data->action = MV64XXX_I2C_ACTION_SEND_STOP;
210 drv_data->state = MV64XXX_I2C_STATE_IDLE;
211 drv_data->rc = -EIO;
212 }
213}
214
215static void
216mv64xxx_i2c_do_action(struct mv64xxx_i2c_data *drv_data)
217{
218 switch(drv_data->action) {
219 case MV64XXX_I2C_ACTION_CONTINUE:
220 writel(drv_data->cntl_bits,
221 drv_data->reg_base + MV64XXX_I2C_REG_CONTROL);
222 break;
223
224 case MV64XXX_I2C_ACTION_SEND_START:
225 writel(drv_data->cntl_bits | MV64XXX_I2C_REG_CONTROL_START,
226 drv_data->reg_base + MV64XXX_I2C_REG_CONTROL);
227 break;
228
229 case MV64XXX_I2C_ACTION_SEND_ADDR_1:
230 writel(drv_data->addr1,
231 drv_data->reg_base + MV64XXX_I2C_REG_DATA);
232 writel(drv_data->cntl_bits,
233 drv_data->reg_base + MV64XXX_I2C_REG_CONTROL);
234 break;
235
236 case MV64XXX_I2C_ACTION_SEND_ADDR_2:
237 writel(drv_data->addr2,
238 drv_data->reg_base + MV64XXX_I2C_REG_DATA);
239 writel(drv_data->cntl_bits,
240 drv_data->reg_base + MV64XXX_I2C_REG_CONTROL);
241 break;
242
243 case MV64XXX_I2C_ACTION_SEND_DATA:
244 writel(drv_data->msg->buf[drv_data->byte_posn++],
245 drv_data->reg_base + MV64XXX_I2C_REG_DATA);
246 writel(drv_data->cntl_bits,
247 drv_data->reg_base + MV64XXX_I2C_REG_CONTROL);
248 break;
249
250 case MV64XXX_I2C_ACTION_RCV_DATA:
251 drv_data->msg->buf[drv_data->byte_posn++] =
252 readl(drv_data->reg_base + MV64XXX_I2C_REG_DATA);
253 writel(drv_data->cntl_bits,
254 drv_data->reg_base + MV64XXX_I2C_REG_CONTROL);
255 break;
256
257 case MV64XXX_I2C_ACTION_RCV_DATA_STOP:
258 drv_data->msg->buf[drv_data->byte_posn++] =
259 readl(drv_data->reg_base + MV64XXX_I2C_REG_DATA);
260 drv_data->cntl_bits &= ~MV64XXX_I2C_REG_CONTROL_INTEN;
261 writel(drv_data->cntl_bits | MV64XXX_I2C_REG_CONTROL_STOP,
262 drv_data->reg_base + MV64XXX_I2C_REG_CONTROL);
263 drv_data->block = 0;
264 wake_up_interruptible(&drv_data->waitq);
265 break;
266
267 case MV64XXX_I2C_ACTION_INVALID:
268 default:
269 dev_err(&drv_data->adapter.dev,
270 "mv64xxx_i2c_do_action: Invalid action: %d\n",
271 drv_data->action);
272 drv_data->rc = -EIO;
273 /* FALLTHRU */
274 case MV64XXX_I2C_ACTION_SEND_STOP:
275 drv_data->cntl_bits &= ~MV64XXX_I2C_REG_CONTROL_INTEN;
276 writel(drv_data->cntl_bits | MV64XXX_I2C_REG_CONTROL_STOP,
277 drv_data->reg_base + MV64XXX_I2C_REG_CONTROL);
278 drv_data->block = 0;
279 wake_up_interruptible(&drv_data->waitq);
280 break;
281 }
282}
283
284static int
285mv64xxx_i2c_intr(int irq, void *dev_id, struct pt_regs *regs)
286{
287 struct mv64xxx_i2c_data *drv_data = dev_id;
288 unsigned long flags;
289 u32 status;
290 int rc = IRQ_NONE;
291
292 spin_lock_irqsave(&drv_data->lock, flags);
293 while (readl(drv_data->reg_base + MV64XXX_I2C_REG_CONTROL) &
294 MV64XXX_I2C_REG_CONTROL_IFLG) {
295 status = readl(drv_data->reg_base + MV64XXX_I2C_REG_STATUS);
296 mv64xxx_i2c_fsm(drv_data, status);
297 mv64xxx_i2c_do_action(drv_data);
298 rc = IRQ_HANDLED;
299 }
300 spin_unlock_irqrestore(&drv_data->lock, flags);
301
302 return rc;
303}
304
305/*
306 *****************************************************************************
307 *
308 * I2C Msg Execution Routines
309 *
310 *****************************************************************************
311 */
312static void
313mv64xxx_i2c_prepare_for_io(struct mv64xxx_i2c_data *drv_data,
314 struct i2c_msg *msg)
315{
316 u32 dir = 0;
317
318 drv_data->msg = msg;
319 drv_data->byte_posn = 0;
320 drv_data->bytes_left = msg->len;
321 drv_data->rc = 0;
322 drv_data->cntl_bits = MV64XXX_I2C_REG_CONTROL_ACK |
323 MV64XXX_I2C_REG_CONTROL_INTEN | MV64XXX_I2C_REG_CONTROL_TWSIEN;
324
325 if (msg->flags & I2C_M_RD)
326 dir = 1;
327
328 if (msg->flags & I2C_M_REV_DIR_ADDR)
329 dir ^= 1;
330
331 if (msg->flags & I2C_M_TEN) {
332 drv_data->addr1 = 0xf0 | (((u32)msg->addr & 0x300) >> 7) | dir;
333 drv_data->addr2 = (u32)msg->addr & 0xff;
334 } else {
335 drv_data->addr1 = ((u32)msg->addr & 0x7f) << 1 | dir;
336 drv_data->addr2 = 0;
337 }
338}
339
340static void
341mv64xxx_i2c_wait_for_completion(struct mv64xxx_i2c_data *drv_data)
342{
343 long time_left;
344 unsigned long flags;
345 char abort = 0;
346
347 time_left = wait_event_interruptible_timeout(drv_data->waitq,
348 !drv_data->block, msecs_to_jiffies(drv_data->adapter.timeout));
349
350 spin_lock_irqsave(&drv_data->lock, flags);
351 if (!time_left) { /* Timed out */
352 drv_data->rc = -ETIMEDOUT;
353 abort = 1;
354 } else if (time_left < 0) { /* Interrupted/Error */
355 drv_data->rc = time_left; /* errno value */
356 abort = 1;
357 }
358
359 if (abort && drv_data->block) {
360 drv_data->state = MV64XXX_I2C_STATE_ABORTING;
361 spin_unlock_irqrestore(&drv_data->lock, flags);
362
363 time_left = wait_event_timeout(drv_data->waitq,
364 !drv_data->block,
365 msecs_to_jiffies(drv_data->adapter.timeout));
366
367 if (time_left <= 0) {
368 drv_data->state = MV64XXX_I2C_STATE_IDLE;
369 dev_err(&drv_data->adapter.dev,
370 "mv64xxx: I2C bus locked\n");
371 }
372 } else
373 spin_unlock_irqrestore(&drv_data->lock, flags);
374}
375
376static int
377mv64xxx_i2c_execute_msg(struct mv64xxx_i2c_data *drv_data, struct i2c_msg *msg)
378{
379 unsigned long flags;
380
381 spin_lock_irqsave(&drv_data->lock, flags);
382 mv64xxx_i2c_prepare_for_io(drv_data, msg);
383
384 if (unlikely(msg->flags & I2C_M_NOSTART)) { /* Skip start/addr phases */
385 if (drv_data->msg->flags & I2C_M_RD) {
386 /* No action to do, wait for slave to send a byte */
387 drv_data->action = MV64XXX_I2C_ACTION_CONTINUE;
388 drv_data->state =
389 MV64XXX_I2C_STATE_WAITING_FOR_SLAVE_DATA;
390 } else {
391 drv_data->action = MV64XXX_I2C_ACTION_SEND_DATA;
392 drv_data->state =
393 MV64XXX_I2C_STATE_WAITING_FOR_SLAVE_ACK;
394 drv_data->bytes_left--;
395 }
396 } else {
397 drv_data->action = MV64XXX_I2C_ACTION_SEND_START;
398 drv_data->state = MV64XXX_I2C_STATE_WAITING_FOR_START_COND;
399 }
400
401 drv_data->block = 1;
402 mv64xxx_i2c_do_action(drv_data);
403 spin_unlock_irqrestore(&drv_data->lock, flags);
404
405 mv64xxx_i2c_wait_for_completion(drv_data);
406 return drv_data->rc;
407}
408
409/*
410 *****************************************************************************
411 *
412 * I2C Core Support Routines (Interface to higher level I2C code)
413 *
414 *****************************************************************************
415 */
416static u32
417mv64xxx_i2c_functionality(struct i2c_adapter *adap)
418{
419 return I2C_FUNC_I2C | I2C_FUNC_10BIT_ADDR | I2C_FUNC_SMBUS_EMUL;
420}
421
422static int
423mv64xxx_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
424{
425 struct mv64xxx_i2c_data *drv_data = i2c_get_adapdata(adap);
426 int i, rc = 0;
427
428 for (i=0; i<num; i++)
429 if ((rc = mv64xxx_i2c_execute_msg(drv_data, &msgs[i])) != 0)
430 break;
431
432 return rc;
433}
434
435static struct i2c_algorithm mv64xxx_i2c_algo = {
436 .name = MV64XXX_I2C_CTLR_NAME " algorithm",
437 .id = I2C_ALGO_MV64XXX,
438 .master_xfer = mv64xxx_i2c_xfer,
439 .functionality = mv64xxx_i2c_functionality,
440};
441
442/*
443 *****************************************************************************
444 *
445 * Driver Interface & Early Init Routines
446 *
447 *****************************************************************************
448 */
449static void __devinit
450mv64xxx_i2c_hw_init(struct mv64xxx_i2c_data *drv_data)
451{
452 writel(0, drv_data->reg_base + MV64XXX_I2C_REG_SOFT_RESET);
453 writel((((drv_data->freq_m & 0xf) << 3) | (drv_data->freq_n & 0x7)),
454 drv_data->reg_base + MV64XXX_I2C_REG_BAUD);
455 writel(0, drv_data->reg_base + MV64XXX_I2C_REG_SLAVE_ADDR);
456 writel(0, drv_data->reg_base + MV64XXX_I2C_REG_EXT_SLAVE_ADDR);
457 writel(MV64XXX_I2C_REG_CONTROL_TWSIEN | MV64XXX_I2C_REG_CONTROL_STOP,
458 drv_data->reg_base + MV64XXX_I2C_REG_CONTROL);
459 drv_data->state = MV64XXX_I2C_STATE_IDLE;
460}
461
462static int __devinit
463mv64xxx_i2c_map_regs(struct platform_device *pd,
464 struct mv64xxx_i2c_data *drv_data)
465{
466 struct resource *r;
467
468 if ((r = platform_get_resource(pd, IORESOURCE_MEM, 0)) &&
469 request_mem_region(r->start, MV64XXX_I2C_REG_BLOCK_SIZE,
470 drv_data->adapter.name)) {
471
472 drv_data->reg_base = ioremap(r->start,
473 MV64XXX_I2C_REG_BLOCK_SIZE);
474 drv_data->reg_base_p = r->start;
475 } else
476 return -ENOMEM;
477
478 return 0;
479}
480
481static void __devexit
482mv64xxx_i2c_unmap_regs(struct mv64xxx_i2c_data *drv_data)
483{
484 if (drv_data->reg_base) {
485 iounmap(drv_data->reg_base);
486 release_mem_region(drv_data->reg_base_p,
487 MV64XXX_I2C_REG_BLOCK_SIZE);
488 }
489
490 drv_data->reg_base = NULL;
491 drv_data->reg_base_p = 0;
492}
493
494static int __devinit
495mv64xxx_i2c_probe(struct device *dev)
496{
497 struct platform_device *pd = to_platform_device(dev);
498 struct mv64xxx_i2c_data *drv_data;
499 struct mv64xxx_i2c_pdata *pdata = dev->platform_data;
500 int rc;
501
502 if ((pd->id != 0) || !pdata)
503 return -ENODEV;
504
505 drv_data = kmalloc(sizeof(struct mv64xxx_i2c_data), GFP_KERNEL);
506
507 if (!drv_data)
508 return -ENOMEM;
509
510 memset(drv_data, 0, sizeof(struct mv64xxx_i2c_data));
511
512 if (mv64xxx_i2c_map_regs(pd, drv_data)) {
513 rc = -ENODEV;
514 goto exit_kfree;
515 }
516
517 strncpy(drv_data->adapter.name, MV64XXX_I2C_CTLR_NAME " adapter",
518 I2C_NAME_SIZE);
519
520 init_waitqueue_head(&drv_data->waitq);
521 spin_lock_init(&drv_data->lock);
522
523 drv_data->freq_m = pdata->freq_m;
524 drv_data->freq_n = pdata->freq_n;
525 drv_data->irq = platform_get_irq(pd, 0);
526 drv_data->adapter.id = I2C_ALGO_MV64XXX | I2C_HW_MV64XXX;
527 drv_data->adapter.algo = &mv64xxx_i2c_algo;
528 drv_data->adapter.owner = THIS_MODULE;
529 drv_data->adapter.class = I2C_CLASS_HWMON;
530 drv_data->adapter.timeout = pdata->timeout;
531 drv_data->adapter.retries = pdata->retries;
532 dev_set_drvdata(dev, drv_data);
533 i2c_set_adapdata(&drv_data->adapter, drv_data);
534
535 if (request_irq(drv_data->irq, mv64xxx_i2c_intr, 0,
536 MV64XXX_I2C_CTLR_NAME, drv_data)) {
537
538 dev_err(dev, "mv64xxx: Can't register intr handler "
539 "irq: %d\n", drv_data->irq);
540 rc = -EINVAL;
541 goto exit_unmap_regs;
542 } else if ((rc = i2c_add_adapter(&drv_data->adapter)) != 0) {
543 dev_err(dev, "mv64xxx: Can't add i2c adapter, rc: %d\n", -rc);
544 goto exit_free_irq;
545 }
546
547 mv64xxx_i2c_hw_init(drv_data);
548
549 return 0;
550
551 exit_free_irq:
552 free_irq(drv_data->irq, drv_data);
553 exit_unmap_regs:
554 mv64xxx_i2c_unmap_regs(drv_data);
555 exit_kfree:
556 kfree(drv_data);
557 return rc;
558}
559
560static int __devexit
561mv64xxx_i2c_remove(struct device *dev)
562{
563 struct mv64xxx_i2c_data *drv_data = dev_get_drvdata(dev);
564 int rc;
565
566 rc = i2c_del_adapter(&drv_data->adapter);
567 free_irq(drv_data->irq, drv_data);
568 mv64xxx_i2c_unmap_regs(drv_data);
569 kfree(drv_data);
570
571 return rc;
572}
573
574static struct device_driver mv64xxx_i2c_driver = {
575 .name = MV64XXX_I2C_CTLR_NAME,
576 .bus = &platform_bus_type,
577 .probe = mv64xxx_i2c_probe,
578 .remove = mv64xxx_i2c_remove,
579};
580
581static int __init
582mv64xxx_i2c_init(void)
583{
584 return driver_register(&mv64xxx_i2c_driver);
585}
586
587static void __exit
588mv64xxx_i2c_exit(void)
589{
590 driver_unregister(&mv64xxx_i2c_driver);
591}
592
593module_init(mv64xxx_i2c_init);
594module_exit(mv64xxx_i2c_exit);
595
596MODULE_AUTHOR("Mark A. Greer <mgreer@mvista.com>");
597MODULE_DESCRIPTION("Marvell mv64xxx host bridge i2c ctlr driver");
598MODULE_LICENSE("GPL");
diff --git a/drivers/i2c/busses/i2c-nforce2.c b/drivers/i2c/busses/i2c-nforce2.c
new file mode 100644
index 000000000000..6d13127c8c4e
--- /dev/null
+++ b/drivers/i2c/busses/i2c-nforce2.c
@@ -0,0 +1,410 @@
1/*
2 SMBus driver for nVidia nForce2 MCP
3
4 Added nForce3 Pro 150 Thomas Leibold <thomas@plx.com>,
5 Ported to 2.5 Patrick Dreker <patrick@dreker.de>,
6 Copyright (c) 2003 Hans-Frieder Vogt <hfvogt@arcor.de>,
7 Based on
8 SMBus 2.0 driver for AMD-8111 IO-Hub
9 Copyright (c) 2002 Vojtech Pavlik
10
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24*/
25
26/*
27 SUPPORTED DEVICES PCI ID
28 nForce2 MCP 0064
29 nForce2 Ultra 400 MCP 0084
30 nForce3 Pro150 MCP 00D4
31 nForce3 250Gb MCP 00E4
32 nForce4 MCP 0052
33
34 This driver supports the 2 SMBuses that are included in the MCP of the
35 nForce2/3/4 chipsets.
36*/
37
38/* Note: we assume there can only be one nForce2, with two SMBus interfaces */
39
40#include <linux/config.h>
41#include <linux/module.h>
42#include <linux/pci.h>
43#include <linux/kernel.h>
44#include <linux/stddef.h>
45#include <linux/sched.h>
46#include <linux/ioport.h>
47#include <linux/init.h>
48#include <linux/i2c.h>
49#include <linux/delay.h>
50#include <asm/io.h>
51
52MODULE_LICENSE("GPL");
53MODULE_AUTHOR ("Hans-Frieder Vogt <hfvogt@arcor.de>");
54MODULE_DESCRIPTION("nForce2 SMBus driver");
55
56
57struct nforce2_smbus {
58 struct pci_dev *dev;
59 struct i2c_adapter adapter;
60 int base;
61 int size;
62};
63
64
65/*
66 * nVidia nForce2 SMBus control register definitions
67 */
68#define NFORCE_PCI_SMB1 0x50
69#define NFORCE_PCI_SMB2 0x54
70
71
72/*
73 * ACPI 2.0 chapter 13 SMBus 2.0 EC register model
74 */
75#define NVIDIA_SMB_PRTCL (smbus->base + 0x00) /* protocol, PEC */
76#define NVIDIA_SMB_STS (smbus->base + 0x01) /* status */
77#define NVIDIA_SMB_ADDR (smbus->base + 0x02) /* address */
78#define NVIDIA_SMB_CMD (smbus->base + 0x03) /* command */
79#define NVIDIA_SMB_DATA (smbus->base + 0x04) /* 32 data registers */
80#define NVIDIA_SMB_BCNT (smbus->base + 0x24) /* number of data bytes */
81#define NVIDIA_SMB_ALRM_A (smbus->base + 0x25) /* alarm address */
82#define NVIDIA_SMB_ALRM_D (smbus->base + 0x26) /* 2 bytes alarm data */
83
84#define NVIDIA_SMB_STS_DONE 0x80
85#define NVIDIA_SMB_STS_ALRM 0x40
86#define NVIDIA_SMB_STS_RES 0x20
87#define NVIDIA_SMB_STS_STATUS 0x1f
88
89#define NVIDIA_SMB_PRTCL_WRITE 0x00
90#define NVIDIA_SMB_PRTCL_READ 0x01
91#define NVIDIA_SMB_PRTCL_QUICK 0x02
92#define NVIDIA_SMB_PRTCL_BYTE 0x04
93#define NVIDIA_SMB_PRTCL_BYTE_DATA 0x06
94#define NVIDIA_SMB_PRTCL_WORD_DATA 0x08
95#define NVIDIA_SMB_PRTCL_BLOCK_DATA 0x0a
96#define NVIDIA_SMB_PRTCL_PROC_CALL 0x0c
97#define NVIDIA_SMB_PRTCL_BLOCK_PROC_CALL 0x0d
98#define NVIDIA_SMB_PRTCL_I2C_BLOCK_DATA 0x4a
99#define NVIDIA_SMB_PRTCL_PEC 0x80
100
101
102/* Other settings */
103#define MAX_TIMEOUT 256
104
105
106
107static s32 nforce2_access(struct i2c_adapter *adap, u16 addr,
108 unsigned short flags, char read_write,
109 u8 command, int size, union i2c_smbus_data *data);
110static u32 nforce2_func(struct i2c_adapter *adapter);
111
112
113static struct i2c_algorithm smbus_algorithm = {
114 .name = "Non-I2C SMBus adapter",
115 .id = I2C_ALGO_SMBUS,
116 .smbus_xfer = nforce2_access,
117 .functionality = nforce2_func,
118};
119
120static struct i2c_adapter nforce2_adapter = {
121 .owner = THIS_MODULE,
122 .class = I2C_CLASS_HWMON,
123 .algo = &smbus_algorithm,
124 .name = "unset",
125};
126
127/* Return -1 on error. See smbus.h for more information */
128static s32 nforce2_access(struct i2c_adapter * adap, u16 addr,
129 unsigned short flags, char read_write,
130 u8 command, int size, union i2c_smbus_data * data)
131{
132 struct nforce2_smbus *smbus = adap->algo_data;
133 unsigned char protocol, pec, temp;
134 unsigned char len = 0; /* to keep the compiler quiet */
135 int timeout = 0;
136 int i;
137
138 protocol = (read_write == I2C_SMBUS_READ) ? NVIDIA_SMB_PRTCL_READ :
139 NVIDIA_SMB_PRTCL_WRITE;
140 pec = (flags & I2C_CLIENT_PEC) ? NVIDIA_SMB_PRTCL_PEC : 0;
141
142 switch (size) {
143
144 case I2C_SMBUS_QUICK:
145 protocol |= NVIDIA_SMB_PRTCL_QUICK;
146 read_write = I2C_SMBUS_WRITE;
147 break;
148
149 case I2C_SMBUS_BYTE:
150 if (read_write == I2C_SMBUS_WRITE)
151 outb_p(command, NVIDIA_SMB_CMD);
152 protocol |= NVIDIA_SMB_PRTCL_BYTE;
153 break;
154
155 case I2C_SMBUS_BYTE_DATA:
156 outb_p(command, NVIDIA_SMB_CMD);
157 if (read_write == I2C_SMBUS_WRITE)
158 outb_p(data->byte, NVIDIA_SMB_DATA);
159 protocol |= NVIDIA_SMB_PRTCL_BYTE_DATA;
160 break;
161
162 case I2C_SMBUS_WORD_DATA:
163 outb_p(command, NVIDIA_SMB_CMD);
164 if (read_write == I2C_SMBUS_WRITE) {
165 outb_p(data->word, NVIDIA_SMB_DATA);
166 outb_p(data->word >> 8, NVIDIA_SMB_DATA+1);
167 }
168 protocol |= NVIDIA_SMB_PRTCL_WORD_DATA | pec;
169 break;
170
171 case I2C_SMBUS_BLOCK_DATA:
172 outb_p(command, NVIDIA_SMB_CMD);
173 if (read_write == I2C_SMBUS_WRITE) {
174 len = min_t(u8, data->block[0], 32);
175 outb_p(len, NVIDIA_SMB_BCNT);
176 for (i = 0; i < len; i++)
177 outb_p(data->block[i + 1], NVIDIA_SMB_DATA+i);
178 }
179 protocol |= NVIDIA_SMB_PRTCL_BLOCK_DATA | pec;
180 break;
181
182 case I2C_SMBUS_I2C_BLOCK_DATA:
183 len = min_t(u8, data->block[0], 32);
184 outb_p(command, NVIDIA_SMB_CMD);
185 outb_p(len, NVIDIA_SMB_BCNT);
186 if (read_write == I2C_SMBUS_WRITE)
187 for (i = 0; i < len; i++)
188 outb_p(data->block[i + 1], NVIDIA_SMB_DATA+i);
189 protocol |= NVIDIA_SMB_PRTCL_I2C_BLOCK_DATA;
190 break;
191
192 case I2C_SMBUS_PROC_CALL:
193 dev_err(&adap->dev, "I2C_SMBUS_PROC_CALL not supported!\n");
194 return -1;
195 /*
196 outb_p(command, NVIDIA_SMB_CMD);
197 outb_p(data->word, NVIDIA_SMB_DATA);
198 outb_p(data->word >> 8, NVIDIA_SMB_DATA + 1);
199 protocol = NVIDIA_SMB_PRTCL_PROC_CALL | pec;
200 read_write = I2C_SMBUS_READ;
201 break;
202 */
203
204 case I2C_SMBUS_BLOCK_PROC_CALL:
205 dev_err(&adap->dev, "I2C_SMBUS_BLOCK_PROC_CALL not supported!\n");
206 return -1;
207 /*
208 protocol |= pec;
209 len = min_t(u8, data->block[0], 31);
210 outb_p(command, NVIDIA_SMB_CMD);
211 outb_p(len, NVIDIA_SMB_BCNT);
212 for (i = 0; i < len; i++)
213 outb_p(data->block[i + 1], NVIDIA_SMB_DATA + i);
214 protocol = NVIDIA_SMB_PRTCL_BLOCK_PROC_CALL | pec;
215 read_write = I2C_SMBUS_READ;
216 break;
217 */
218
219 case I2C_SMBUS_WORD_DATA_PEC:
220 case I2C_SMBUS_BLOCK_DATA_PEC:
221 case I2C_SMBUS_PROC_CALL_PEC:
222 case I2C_SMBUS_BLOCK_PROC_CALL_PEC:
223 dev_err(&adap->dev, "Unexpected software PEC transaction %d\n.", size);
224 return -1;
225
226 default:
227 dev_err(&adap->dev, "Unsupported transaction %d\n", size);
228 return -1;
229 }
230
231 outb_p((addr & 0x7f) << 1, NVIDIA_SMB_ADDR);
232 outb_p(protocol, NVIDIA_SMB_PRTCL);
233
234 temp = inb_p(NVIDIA_SMB_STS);
235
236#if 0
237 do {
238 i2c_do_pause(1);
239 temp = inb_p(NVIDIA_SMB_STS);
240 } while (((temp & NVIDIA_SMB_STS_DONE) == 0) && (timeout++ < MAX_TIMEOUT));
241#endif
242 if (~temp & NVIDIA_SMB_STS_DONE) {
243 udelay(500);
244 temp = inb_p(NVIDIA_SMB_STS);
245 }
246 if (~temp & NVIDIA_SMB_STS_DONE) {
247 msleep(10);
248 temp = inb_p(NVIDIA_SMB_STS);
249 }
250
251 if ((timeout >= MAX_TIMEOUT) || (~temp & NVIDIA_SMB_STS_DONE)
252 || (temp & NVIDIA_SMB_STS_STATUS))
253 return -1;
254
255 if (read_write == I2C_SMBUS_WRITE)
256 return 0;
257
258 switch (size) {
259
260 case I2C_SMBUS_BYTE:
261 case I2C_SMBUS_BYTE_DATA:
262 data->byte = inb_p(NVIDIA_SMB_DATA);
263 break;
264
265 case I2C_SMBUS_WORD_DATA:
266 /* case I2C_SMBUS_PROC_CALL: not supported */
267 data->word = inb_p(NVIDIA_SMB_DATA) | (inb_p(NVIDIA_SMB_DATA+1) << 8);
268 break;
269
270 case I2C_SMBUS_BLOCK_DATA:
271 /* case I2C_SMBUS_BLOCK_PROC_CALL: not supported */
272 len = inb_p(NVIDIA_SMB_BCNT);
273 len = min_t(u8, len, 32);
274 case I2C_SMBUS_I2C_BLOCK_DATA:
275 for (i = 0; i < len; i++)
276 data->block[i+1] = inb_p(NVIDIA_SMB_DATA + i);
277 data->block[0] = len;
278 break;
279 }
280
281 return 0;
282}
283
284
285static u32 nforce2_func(struct i2c_adapter *adapter)
286{
287 /* other functionality might be possible, but is not tested */
288 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
289 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA /* |
290 I2C_FUNC_SMBUS_BLOCK_DATA */;
291}
292
293
294static struct pci_device_id nforce2_ids[] = {
295 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE2_SMBUS) },
296 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE2S_SMBUS) },
297 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE3_SMBUS) },
298 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE3S_SMBUS) },
299 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE4_SMBUS) },
300 { 0 }
301};
302
303
304MODULE_DEVICE_TABLE (pci, nforce2_ids);
305
306
307static int __devinit nforce2_probe_smb (struct pci_dev *dev, int reg,
308 struct nforce2_smbus *smbus, char *name)
309{
310 u16 iobase;
311 int error;
312
313 if (pci_read_config_word(dev, reg, &iobase) != PCIBIOS_SUCCESSFUL) {
314 dev_err(&smbus->adapter.dev, "Error reading PCI config for %s\n", name);
315 return -1;
316 }
317 smbus->dev = dev;
318 smbus->base = iobase & 0xfffc;
319 smbus->size = 8;
320
321 if (!request_region(smbus->base, smbus->size, "nForce2 SMBus")) {
322 dev_err(&smbus->adapter.dev, "Error requesting region %02x .. %02X for %s\n",
323 smbus->base, smbus->base+smbus->size-1, name);
324 return -1;
325 }
326 smbus->adapter = nforce2_adapter;
327 smbus->adapter.algo_data = smbus;
328 smbus->adapter.dev.parent = &dev->dev;
329 snprintf(smbus->adapter.name, I2C_NAME_SIZE,
330 "SMBus nForce2 adapter at %04x", smbus->base);
331
332 error = i2c_add_adapter(&smbus->adapter);
333 if (error) {
334 dev_err(&smbus->adapter.dev, "Failed to register adapter.\n");
335 release_region(smbus->base, smbus->size);
336 return -1;
337 }
338 dev_info(&smbus->adapter.dev, "nForce2 SMBus adapter at %#x\n", smbus->base);
339 return 0;
340}
341
342
343static int __devinit nforce2_probe(struct pci_dev *dev, const struct pci_device_id *id)
344{
345 struct nforce2_smbus *smbuses;
346 int res1, res2;
347
348 /* we support 2 SMBus adapters */
349 if (!(smbuses = (void *)kmalloc(2*sizeof(struct nforce2_smbus),
350 GFP_KERNEL)))
351 return -ENOMEM;
352 memset (smbuses, 0, 2*sizeof(struct nforce2_smbus));
353 pci_set_drvdata(dev, smbuses);
354
355 /* SMBus adapter 1 */
356 res1 = nforce2_probe_smb (dev, NFORCE_PCI_SMB1, &smbuses[0], "SMB1");
357 if (res1 < 0) {
358 dev_err(&dev->dev, "Error probing SMB1.\n");
359 smbuses[0].base = 0; /* to have a check value */
360 }
361 res2 = nforce2_probe_smb (dev, NFORCE_PCI_SMB2, &smbuses[1], "SMB2");
362 if (res2 < 0) {
363 dev_err(&dev->dev, "Error probing SMB2.\n");
364 smbuses[1].base = 0; /* to have a check value */
365 }
366 if ((res1 < 0) && (res2 < 0)) {
367 /* we did not find even one of the SMBuses, so we give up */
368 kfree(smbuses);
369 return -ENODEV;
370 }
371
372 return 0;
373}
374
375
376static void __devexit nforce2_remove(struct pci_dev *dev)
377{
378 struct nforce2_smbus *smbuses = (void*) pci_get_drvdata(dev);
379
380 if (smbuses[0].base) {
381 i2c_del_adapter(&smbuses[0].adapter);
382 release_region(smbuses[0].base, smbuses[0].size);
383 }
384 if (smbuses[1].base) {
385 i2c_del_adapter(&smbuses[1].adapter);
386 release_region(smbuses[1].base, smbuses[1].size);
387 }
388 kfree(smbuses);
389}
390
391static struct pci_driver nforce2_driver = {
392 .name = "nForce2_smbus",
393 .id_table = nforce2_ids,
394 .probe = nforce2_probe,
395 .remove = __devexit_p(nforce2_remove),
396};
397
398static int __init nforce2_init(void)
399{
400 return pci_register_driver(&nforce2_driver);
401}
402
403static void __exit nforce2_exit(void)
404{
405 pci_unregister_driver(&nforce2_driver);
406}
407
408module_init(nforce2_init);
409module_exit(nforce2_exit);
410
diff --git a/drivers/i2c/busses/i2c-parport-light.c b/drivers/i2c/busses/i2c-parport-light.c
new file mode 100644
index 000000000000..cb5e722301d8
--- /dev/null
+++ b/drivers/i2c/busses/i2c-parport-light.c
@@ -0,0 +1,175 @@
1/* ------------------------------------------------------------------------ *
2 * i2c-parport.c I2C bus over parallel port *
3 * ------------------------------------------------------------------------ *
4 Copyright (C) 2003-2004 Jean Delvare <khali@linux-fr.org>
5
6 Based on older i2c-velleman.c driver
7 Copyright (C) 1995-2000 Simon G. Vogl
8 With some changes from:
9 Frodo Looijaard <frodol@dds.nl>
10 Kyösti Mälkki <kmalkki@cc.hut.fi>
11
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 * ------------------------------------------------------------------------ */
26
27#include <linux/config.h>
28#include <linux/kernel.h>
29#include <linux/module.h>
30#include <linux/init.h>
31#include <linux/ioport.h>
32#include <linux/i2c.h>
33#include <linux/i2c-algo-bit.h>
34#include <asm/io.h>
35#include "i2c-parport.h"
36
37#define DEFAULT_BASE 0x378
38
39static u16 base;
40module_param(base, ushort, 0);
41MODULE_PARM_DESC(base, "Base I/O address");
42
43/* ----- Low-level parallel port access ----------------------------------- */
44
45static inline void port_write(unsigned char p, unsigned char d)
46{
47 outb(d, base+p);
48}
49
50static inline unsigned char port_read(unsigned char p)
51{
52 return inb(base+p);
53}
54
55/* ----- Unified line operation functions --------------------------------- */
56
57static inline void line_set(int state, const struct lineop *op)
58{
59 u8 oldval = port_read(op->port);
60
61 /* Touch only the bit(s) needed */
62 if ((op->inverted && !state) || (!op->inverted && state))
63 port_write(op->port, oldval | op->val);
64 else
65 port_write(op->port, oldval & ~op->val);
66}
67
68static inline int line_get(const struct lineop *op)
69{
70 u8 oldval = port_read(op->port);
71
72 return ((op->inverted && (oldval & op->val) != op->val)
73 || (!op->inverted && (oldval & op->val) == op->val));
74}
75
76/* ----- I2C algorithm call-back functions and structures ----------------- */
77
78static void parport_setscl(void *data, int state)
79{
80 line_set(state, &adapter_parm[type].setscl);
81}
82
83static void parport_setsda(void *data, int state)
84{
85 line_set(state, &adapter_parm[type].setsda);
86}
87
88static int parport_getscl(void *data)
89{
90 return line_get(&adapter_parm[type].getscl);
91}
92
93static int parport_getsda(void *data)
94{
95 return line_get(&adapter_parm[type].getsda);
96}
97
98/* Encapsulate the functions above in the correct structure
99 Note that getscl will be set to NULL by the attaching code for adapters
100 that cannot read SCL back */
101static struct i2c_algo_bit_data parport_algo_data = {
102 .setsda = parport_setsda,
103 .setscl = parport_setscl,
104 .getsda = parport_getsda,
105 .getscl = parport_getscl,
106 .udelay = 50,
107 .mdelay = 50,
108 .timeout = HZ,
109};
110
111/* ----- I2c structure ---------------------------------------------------- */
112
113static struct i2c_adapter parport_adapter = {
114 .owner = THIS_MODULE,
115 .class = I2C_CLASS_HWMON,
116 .id = I2C_HW_B_LP,
117 .algo_data = &parport_algo_data,
118 .name = "Parallel port adapter (light)",
119};
120
121/* ----- Module loading, unloading and information ------------------------ */
122
123static int __init i2c_parport_init(void)
124{
125 int type_count;
126
127 type_count = sizeof(adapter_parm)/sizeof(struct adapter_parm);
128 if (type < 0 || type >= type_count) {
129 printk(KERN_WARNING "i2c-parport: invalid type (%d)\n", type);
130 type = 0;
131 }
132
133 if (base == 0) {
134 printk(KERN_INFO "i2c-parport: using default base 0x%x\n", DEFAULT_BASE);
135 base = DEFAULT_BASE;
136 }
137
138 if (!request_region(base, 3, "i2c-parport"))
139 return -ENODEV;
140
141 if (!adapter_parm[type].getscl.val)
142 parport_algo_data.getscl = NULL;
143
144 /* Reset hardware to a sane state (SCL and SDA high) */
145 parport_setsda(NULL, 1);
146 parport_setscl(NULL, 1);
147 /* Other init if needed (power on...) */
148 if (adapter_parm[type].init.val)
149 line_set(1, &adapter_parm[type].init);
150
151 if (i2c_bit_add_bus(&parport_adapter) < 0) {
152 printk(KERN_ERR "i2c-parport: Unable to register with I2C\n");
153 release_region(base, 3);
154 return -ENODEV;
155 }
156
157 return 0;
158}
159
160static void __exit i2c_parport_exit(void)
161{
162 /* Un-init if needed (power off...) */
163 if (adapter_parm[type].init.val)
164 line_set(0, &adapter_parm[type].init);
165
166 i2c_bit_del_bus(&parport_adapter);
167 release_region(base, 3);
168}
169
170MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
171MODULE_DESCRIPTION("I2C bus over parallel port (light)");
172MODULE_LICENSE("GPL");
173
174module_init(i2c_parport_init);
175module_exit(i2c_parport_exit);
diff --git a/drivers/i2c/busses/i2c-parport.c b/drivers/i2c/busses/i2c-parport.c
new file mode 100644
index 000000000000..e9560bab51c4
--- /dev/null
+++ b/drivers/i2c/busses/i2c-parport.c
@@ -0,0 +1,267 @@
1/* ------------------------------------------------------------------------ *
2 * i2c-parport.c I2C bus over parallel port *
3 * ------------------------------------------------------------------------ *
4 Copyright (C) 2003-2004 Jean Delvare <khali@linux-fr.org>
5
6 Based on older i2c-philips-par.c driver
7 Copyright (C) 1995-2000 Simon G. Vogl
8 With some changes from:
9 Frodo Looijaard <frodol@dds.nl>
10 Kyösti Mälkki <kmalkki@cc.hut.fi>
11
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 * ------------------------------------------------------------------------ */
26
27#include <linux/config.h>
28#include <linux/kernel.h>
29#include <linux/module.h>
30#include <linux/init.h>
31#include <linux/parport.h>
32#include <linux/i2c.h>
33#include <linux/i2c-algo-bit.h>
34#include "i2c-parport.h"
35
36/* ----- Device list ------------------------------------------------------ */
37
38struct i2c_par {
39 struct pardevice *pdev;
40 struct i2c_adapter adapter;
41 struct i2c_algo_bit_data algo_data;
42 struct i2c_par *next;
43};
44
45static struct i2c_par *adapter_list;
46
47/* ----- Low-level parallel port access ----------------------------------- */
48
49static void port_write_data(struct parport *p, unsigned char d)
50{
51 parport_write_data(p, d);
52}
53
54static void port_write_control(struct parport *p, unsigned char d)
55{
56 parport_write_control(p, d);
57}
58
59static unsigned char port_read_data(struct parport *p)
60{
61 return parport_read_data(p);
62}
63
64static unsigned char port_read_status(struct parport *p)
65{
66 return parport_read_status(p);
67}
68
69static unsigned char port_read_control(struct parport *p)
70{
71 return parport_read_control(p);
72}
73
74static void (*port_write[])(struct parport *, unsigned char) = {
75 port_write_data,
76 NULL,
77 port_write_control,
78};
79
80static unsigned char (*port_read[])(struct parport *) = {
81 port_read_data,
82 port_read_status,
83 port_read_control,
84};
85
86/* ----- Unified line operation functions --------------------------------- */
87
88static inline void line_set(struct parport *data, int state,
89 const struct lineop *op)
90{
91 u8 oldval = port_read[op->port](data);
92
93 /* Touch only the bit(s) needed */
94 if ((op->inverted && !state) || (!op->inverted && state))
95 port_write[op->port](data, oldval | op->val);
96 else
97 port_write[op->port](data, oldval & ~op->val);
98}
99
100static inline int line_get(struct parport *data,
101 const struct lineop *op)
102{
103 u8 oldval = port_read[op->port](data);
104
105 return ((op->inverted && (oldval & op->val) != op->val)
106 || (!op->inverted && (oldval & op->val) == op->val));
107}
108
109/* ----- I2C algorithm call-back functions and structures ----------------- */
110
111static void parport_setscl(void *data, int state)
112{
113 line_set((struct parport *) data, state, &adapter_parm[type].setscl);
114}
115
116static void parport_setsda(void *data, int state)
117{
118 line_set((struct parport *) data, state, &adapter_parm[type].setsda);
119}
120
121static int parport_getscl(void *data)
122{
123 return line_get((struct parport *) data, &adapter_parm[type].getscl);
124}
125
126static int parport_getsda(void *data)
127{
128 return line_get((struct parport *) data, &adapter_parm[type].getsda);
129}
130
131/* Encapsulate the functions above in the correct structure.
132 Note that this is only a template, from which the real structures are
133 copied. The attaching code will set getscl to NULL for adapters that
134 cannot read SCL back, and will also make the the data field point to
135 the parallel port structure. */
136static struct i2c_algo_bit_data parport_algo_data = {
137 .setsda = parport_setsda,
138 .setscl = parport_setscl,
139 .getsda = parport_getsda,
140 .getscl = parport_getscl,
141 .udelay = 60,
142 .mdelay = 60,
143 .timeout = HZ,
144};
145
146/* ----- I2c and parallel port call-back functions and structures --------- */
147
148static struct i2c_adapter parport_adapter = {
149 .owner = THIS_MODULE,
150 .class = I2C_CLASS_HWMON,
151 .id = I2C_HW_B_LP,
152 .name = "Parallel port adapter",
153};
154
155static void i2c_parport_attach (struct parport *port)
156{
157 struct i2c_par *adapter;
158
159 adapter = kmalloc(sizeof(struct i2c_par), GFP_KERNEL);
160 if (adapter == NULL) {
161 printk(KERN_ERR "i2c-parport: Failed to kmalloc\n");
162 return;
163 }
164 memset(adapter, 0x00, sizeof(struct i2c_par));
165
166 pr_debug("i2c-parport: attaching to %s\n", port->name);
167 adapter->pdev = parport_register_device(port, "i2c-parport",
168 NULL, NULL, NULL, PARPORT_FLAG_EXCL, NULL);
169 if (!adapter->pdev) {
170 printk(KERN_ERR "i2c-parport: Unable to register with parport\n");
171 goto ERROR0;
172 }
173
174 /* Fill the rest of the structure */
175 adapter->adapter = parport_adapter;
176 adapter->algo_data = parport_algo_data;
177 if (!adapter_parm[type].getscl.val)
178 adapter->algo_data.getscl = NULL;
179 adapter->algo_data.data = port;
180 adapter->adapter.algo_data = &adapter->algo_data;
181
182 if (parport_claim_or_block(adapter->pdev) < 0) {
183 printk(KERN_ERR "i2c-parport: Could not claim parallel port\n");
184 goto ERROR1;
185 }
186
187 /* Reset hardware to a sane state (SCL and SDA high) */
188 parport_setsda(port, 1);
189 parport_setscl(port, 1);
190 /* Other init if needed (power on...) */
191 if (adapter_parm[type].init.val)
192 line_set(port, 1, &adapter_parm[type].init);
193
194 parport_release(adapter->pdev);
195
196 if (i2c_bit_add_bus(&adapter->adapter) < 0) {
197 printk(KERN_ERR "i2c-parport: Unable to register with I2C\n");
198 goto ERROR1;
199 }
200
201 /* Add the new adapter to the list */
202 adapter->next = adapter_list;
203 adapter_list = adapter;
204 return;
205
206ERROR1:
207 parport_unregister_device(adapter->pdev);
208ERROR0:
209 kfree(adapter);
210}
211
212static void i2c_parport_detach (struct parport *port)
213{
214 struct i2c_par *adapter, *prev;
215
216 /* Walk the list */
217 for (prev = NULL, adapter = adapter_list; adapter;
218 prev = adapter, adapter = adapter->next) {
219 if (adapter->pdev->port == port) {
220 /* Un-init if needed (power off...) */
221 if (adapter_parm[type].init.val)
222 line_set(port, 0, &adapter_parm[type].init);
223
224 i2c_bit_del_bus(&adapter->adapter);
225 parport_unregister_device(adapter->pdev);
226 if (prev)
227 prev->next = adapter->next;
228 else
229 adapter_list = adapter->next;
230 kfree(adapter);
231 return;
232 }
233 }
234}
235
236static struct parport_driver i2c_driver = {
237 .name = "i2c-parport",
238 .attach = i2c_parport_attach,
239 .detach = i2c_parport_detach,
240};
241
242/* ----- Module loading, unloading and information ------------------------ */
243
244static int __init i2c_parport_init(void)
245{
246 int type_count;
247
248 type_count = sizeof(adapter_parm)/sizeof(struct adapter_parm);
249 if (type < 0 || type >= type_count) {
250 printk(KERN_WARNING "i2c-parport: invalid type (%d)\n", type);
251 type = 0;
252 }
253
254 return parport_register_driver(&i2c_driver);
255}
256
257static void __exit i2c_parport_exit(void)
258{
259 parport_unregister_driver(&i2c_driver);
260}
261
262MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
263MODULE_DESCRIPTION("I2C bus over parallel port");
264MODULE_LICENSE("GPL");
265
266module_init(i2c_parport_init);
267module_exit(i2c_parport_exit);
diff --git a/drivers/i2c/busses/i2c-parport.h b/drivers/i2c/busses/i2c-parport.h
new file mode 100644
index 000000000000..f63a53779281
--- /dev/null
+++ b/drivers/i2c/busses/i2c-parport.h
@@ -0,0 +1,94 @@
1/* ------------------------------------------------------------------------ *
2 * i2c-parport.h I2C bus over parallel port *
3 * ------------------------------------------------------------------------ *
4 Copyright (C) 2003-2004 Jean Delvare <khali@linux-fr.org>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 * ------------------------------------------------------------------------ */
20
21#ifdef DATA
22#undef DATA
23#endif
24
25#define DATA 0
26#define STAT 1
27#define CTRL 2
28
29struct lineop {
30 u8 val;
31 u8 port;
32 u8 inverted;
33};
34
35struct adapter_parm {
36 struct lineop setsda;
37 struct lineop setscl;
38 struct lineop getsda;
39 struct lineop getscl;
40 struct lineop init;
41};
42
43static struct adapter_parm adapter_parm[] = {
44 /* type 0: Philips adapter */
45 {
46 .setsda = { 0x80, DATA, 1 },
47 .setscl = { 0x08, CTRL, 0 },
48 .getsda = { 0x80, STAT, 0 },
49 .getscl = { 0x08, STAT, 0 },
50 },
51 /* type 1: home brew teletext adapter */
52 {
53 .setsda = { 0x02, DATA, 0 },
54 .setscl = { 0x01, DATA, 0 },
55 .getsda = { 0x80, STAT, 1 },
56 },
57 /* type 2: Velleman K8000 adapter */
58 {
59 .setsda = { 0x02, CTRL, 1 },
60 .setscl = { 0x08, CTRL, 1 },
61 .getsda = { 0x10, STAT, 0 },
62 },
63 /* type 3: ELV adapter */
64 {
65 .setsda = { 0x02, DATA, 1 },
66 .setscl = { 0x01, DATA, 1 },
67 .getsda = { 0x40, STAT, 1 },
68 .getscl = { 0x08, STAT, 1 },
69 },
70 /* type 4: ADM1032 evaluation board */
71 {
72 .setsda = { 0x02, DATA, 1 },
73 .setscl = { 0x01, DATA, 1 },
74 .getsda = { 0x10, STAT, 1 },
75 .init = { 0xf0, DATA, 0 },
76 },
77 /* type 5: ADM1025, ADM1030 and ADM1031 evaluation boards */
78 {
79 .setsda = { 0x02, DATA, 1 },
80 .setscl = { 0x01, DATA, 1 },
81 .getsda = { 0x10, STAT, 1 },
82 },
83};
84
85static int type;
86module_param(type, int, 0);
87MODULE_PARM_DESC(type,
88 "Type of adapter:\n"
89 " 0 = Philips adapter\n"
90 " 1 = home brew teletext adapter\n"
91 " 2 = Velleman K8000 adapter\n"
92 " 3 = ELV adapter\n"
93 " 4 = ADM1032 evaluation board\n"
94 " 5 = ADM1025, ADM1030 and ADM1031 evaluation boards\n");
diff --git a/drivers/i2c/busses/i2c-pca-isa.c b/drivers/i2c/busses/i2c-pca-isa.c
new file mode 100644
index 000000000000..9c611134db9c
--- /dev/null
+++ b/drivers/i2c/busses/i2c-pca-isa.c
@@ -0,0 +1,184 @@
1/*
2 * i2c-pca-isa.c driver for PCA9564 on ISA boards
3 * Copyright (C) 2004 Arcom Control Systems
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20#include <linux/config.h>
21#include <linux/kernel.h>
22#include <linux/ioport.h>
23#include <linux/module.h>
24#include <linux/moduleparam.h>
25#include <linux/delay.h>
26#include <linux/slab.h>
27#include <linux/init.h>
28#include <linux/interrupt.h>
29#include <linux/pci.h>
30#include <linux/wait.h>
31
32#include <linux/i2c.h>
33#include <linux/i2c-algo-pca.h>
34
35#include <asm/io.h>
36#include <asm/irq.h>
37
38#include "../algos/i2c-algo-pca.h"
39
40#define IO_SIZE 4
41
42#undef DEBUG_IO
43//#define DEBUG_IO
44
45static unsigned long base = 0x330;
46static int irq = 10;
47
48/* Data sheet recommends 59kHz for 100kHz operation due to variation
49 * in the actual clock rate */
50static int clock = I2C_PCA_CON_59kHz;
51
52static int own = 0x55;
53
54static wait_queue_head_t pca_wait;
55
56static int pca_isa_getown(struct i2c_algo_pca_data *adap)
57{
58 return (own);
59}
60
61static int pca_isa_getclock(struct i2c_algo_pca_data *adap)
62{
63 return (clock);
64}
65
66static void
67pca_isa_writebyte(struct i2c_algo_pca_data *adap, int reg, int val)
68{
69#ifdef DEBUG_IO
70 static char *names[] = { "T/O", "DAT", "ADR", "CON" };
71 printk("*** write %s at %#lx <= %#04x\n", names[reg], base+reg, val);
72#endif
73 outb(val, base+reg);
74}
75
76static int
77pca_isa_readbyte(struct i2c_algo_pca_data *adap, int reg)
78{
79 int res = inb(base+reg);
80#ifdef DEBUG_IO
81 {
82 static char *names[] = { "STA", "DAT", "ADR", "CON" };
83 printk("*** read %s => %#04x\n", names[reg], res);
84 }
85#endif
86 return res;
87}
88
89static int pca_isa_waitforinterrupt(struct i2c_algo_pca_data *adap)
90{
91 int ret = 0;
92
93 if (irq > -1) {
94 ret = wait_event_interruptible(pca_wait,
95 pca_isa_readbyte(adap, I2C_PCA_CON) & I2C_PCA_CON_SI);
96 } else {
97 while ((pca_isa_readbyte(adap, I2C_PCA_CON) & I2C_PCA_CON_SI) == 0)
98 udelay(100);
99 }
100 return ret;
101}
102
103static irqreturn_t pca_handler(int this_irq, void *dev_id, struct pt_regs *regs) {
104 wake_up_interruptible(&pca_wait);
105 return IRQ_HANDLED;
106}
107
108static struct i2c_algo_pca_data pca_isa_data = {
109 .get_own = pca_isa_getown,
110 .get_clock = pca_isa_getclock,
111 .write_byte = pca_isa_writebyte,
112 .read_byte = pca_isa_readbyte,
113 .wait_for_interrupt = pca_isa_waitforinterrupt,
114};
115
116static struct i2c_adapter pca_isa_ops = {
117 .owner = THIS_MODULE,
118 .id = I2C_HW_A_ISA,
119 .algo_data = &pca_isa_data,
120 .name = "PCA9564 ISA Adapter",
121};
122
123static int __init pca_isa_init(void)
124{
125
126 init_waitqueue_head(&pca_wait);
127
128 printk(KERN_INFO "i2c-pca-isa: i/o base %#08lx. irq %d\n", base, irq);
129
130 if (!request_region(base, IO_SIZE, "i2c-pca-isa")) {
131 printk(KERN_ERR "i2c-pca-isa: I/O address %#08lx is in use.\n", base);
132 goto out;
133 }
134
135 if (irq > -1) {
136 if (request_irq(irq, pca_handler, 0, "i2c-pca-isa", &pca_isa_ops) < 0) {
137 printk(KERN_ERR "i2c-pca-isa: Request irq%d failed\n", irq);
138 goto out_region;
139 }
140 }
141
142 if (i2c_pca_add_bus(&pca_isa_ops) < 0) {
143 printk(KERN_ERR "i2c-pca-isa: Failed to add i2c bus\n");
144 goto out_irq;
145 }
146
147 return 0;
148
149 out_irq:
150 if (irq > -1)
151 free_irq(irq, &pca_isa_ops);
152 out_region:
153 release_region(base, IO_SIZE);
154 out:
155 return -ENODEV;
156}
157
158static void pca_isa_exit(void)
159{
160 i2c_pca_del_bus(&pca_isa_ops);
161
162 if (irq > 0) {
163 disable_irq(irq);
164 free_irq(irq, &pca_isa_ops);
165 }
166 release_region(base, IO_SIZE);
167}
168
169MODULE_AUTHOR("Ian Campbell <icampbell@arcom.com>");
170MODULE_DESCRIPTION("ISA base PCA9564 driver");
171MODULE_LICENSE("GPL");
172
173module_param(base, ulong, 0);
174MODULE_PARM_DESC(base, "I/O base address");
175
176module_param(irq, int, 0);
177MODULE_PARM_DESC(irq, "IRQ");
178module_param(clock, int, 0);
179MODULE_PARM_DESC(clock, "Clock rate as described in table 1 of PCA9564 datasheet");
180
181module_param(own, int, 0); /* the driver can't do slave mode, so there's no real point in this */
182
183module_init(pca_isa_init);
184module_exit(pca_isa_exit);
diff --git a/drivers/i2c/busses/i2c-piix4.c b/drivers/i2c/busses/i2c-piix4.c
new file mode 100644
index 000000000000..646381b6b3bf
--- /dev/null
+++ b/drivers/i2c/busses/i2c-piix4.c
@@ -0,0 +1,490 @@
1/*
2 piix4.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring
4 Copyright (c) 1998 - 2002 Frodo Looijaard <frodol@dds.nl> and
5 Philip Edelbrock <phil@netroedge.com>
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/*
23 Supports:
24 Intel PIIX4, 440MX
25 Serverworks OSB4, CSB5, CSB6
26 SMSC Victory66
27
28 Note: we assume there can only be one device, with one SMBus interface.
29*/
30
31#include <linux/config.h>
32#include <linux/module.h>
33#include <linux/moduleparam.h>
34#include <linux/pci.h>
35#include <linux/kernel.h>
36#include <linux/delay.h>
37#include <linux/stddef.h>
38#include <linux/sched.h>
39#include <linux/ioport.h>
40#include <linux/i2c.h>
41#include <linux/init.h>
42#include <linux/apm_bios.h>
43#include <linux/dmi.h>
44#include <asm/io.h>
45
46
47struct sd {
48 const unsigned short mfr;
49 const unsigned short dev;
50 const unsigned char fn;
51 const char *name;
52};
53
54/* PIIX4 SMBus address offsets */
55#define SMBHSTSTS (0 + piix4_smba)
56#define SMBHSLVSTS (1 + piix4_smba)
57#define SMBHSTCNT (2 + piix4_smba)
58#define SMBHSTCMD (3 + piix4_smba)
59#define SMBHSTADD (4 + piix4_smba)
60#define SMBHSTDAT0 (5 + piix4_smba)
61#define SMBHSTDAT1 (6 + piix4_smba)
62#define SMBBLKDAT (7 + piix4_smba)
63#define SMBSLVCNT (8 + piix4_smba)
64#define SMBSHDWCMD (9 + piix4_smba)
65#define SMBSLVEVT (0xA + piix4_smba)
66#define SMBSLVDAT (0xC + piix4_smba)
67
68/* count for request_region */
69#define SMBIOSIZE 8
70
71/* PCI Address Constants */
72#define SMBBA 0x090
73#define SMBHSTCFG 0x0D2
74#define SMBSLVC 0x0D3
75#define SMBSHDW1 0x0D4
76#define SMBSHDW2 0x0D5
77#define SMBREV 0x0D6
78
79/* Other settings */
80#define MAX_TIMEOUT 500
81#define ENABLE_INT9 0
82
83/* PIIX4 constants */
84#define PIIX4_QUICK 0x00
85#define PIIX4_BYTE 0x04
86#define PIIX4_BYTE_DATA 0x08
87#define PIIX4_WORD_DATA 0x0C
88#define PIIX4_BLOCK_DATA 0x14
89
90/* insmod parameters */
91
92/* If force is set to anything different from 0, we forcibly enable the
93 PIIX4. DANGEROUS! */
94static int force = 0;
95module_param (force, int, 0);
96MODULE_PARM_DESC(force, "Forcibly enable the PIIX4. DANGEROUS!");
97
98/* If force_addr is set to anything different from 0, we forcibly enable
99 the PIIX4 at the given address. VERY DANGEROUS! */
100static int force_addr = 0;
101module_param (force_addr, int, 0);
102MODULE_PARM_DESC(force_addr,
103 "Forcibly enable the PIIX4 at the given address. "
104 "EXTREMELY DANGEROUS!");
105
106/* If fix_hstcfg is set to anything different from 0, we reset one of the
107 registers to be a valid value. */
108static int fix_hstcfg = 0;
109module_param (fix_hstcfg, int, 0);
110MODULE_PARM_DESC(fix_hstcfg,
111 "Fix config register. Needed on some boards (Force CPCI735).");
112
113static int piix4_transaction(void);
114
115static unsigned short piix4_smba = 0;
116static struct i2c_adapter piix4_adapter;
117
118static struct dmi_system_id __devinitdata piix4_dmi_table[] = {
119 {
120 .ident = "IBM",
121 .matches = { DMI_MATCH(DMI_SYS_VENDOR, "IBM"), },
122 },
123 { },
124};
125
126static int __devinit piix4_setup(struct pci_dev *PIIX4_dev,
127 const struct pci_device_id *id)
128{
129 unsigned char temp;
130
131 /* match up the function */
132 if (PCI_FUNC(PIIX4_dev->devfn) != id->driver_data)
133 return -ENODEV;
134
135 dev_info(&PIIX4_dev->dev, "Found %s device\n", pci_name(PIIX4_dev));
136
137 /* Don't access SMBus on IBM systems which get corrupted eeproms */
138 if (dmi_check_system(piix4_dmi_table) &&
139 PIIX4_dev->vendor == PCI_VENDOR_ID_INTEL) {
140 dev_err(&PIIX4_dev->dev, "IBM Laptop detected; this module "
141 "may corrupt your serial eeprom! Refusing to load "
142 "module!\n");
143 return -EPERM;
144 }
145
146 /* Determine the address of the SMBus areas */
147 if (force_addr) {
148 piix4_smba = force_addr & 0xfff0;
149 force = 0;
150 } else {
151 pci_read_config_word(PIIX4_dev, SMBBA, &piix4_smba);
152 piix4_smba &= 0xfff0;
153 if(piix4_smba == 0) {
154 dev_err(&PIIX4_dev->dev, "SMB base address "
155 "uninitialized - upgrade BIOS or use "
156 "force_addr=0xaddr\n");
157 return -ENODEV;
158 }
159 }
160
161 if (!request_region(piix4_smba, SMBIOSIZE, "piix4-smbus")) {
162 dev_err(&PIIX4_dev->dev, "SMB region 0x%x already in use!\n",
163 piix4_smba);
164 return -ENODEV;
165 }
166
167 pci_read_config_byte(PIIX4_dev, SMBHSTCFG, &temp);
168
169 /* Some BIOS will set up the chipset incorrectly and leave a register
170 in an undefined state (causing I2C to act very strangely). */
171 if (temp & 0x02) {
172 if (fix_hstcfg) {
173 dev_info(&PIIX4_dev->dev, "Working around buggy BIOS "
174 "(I2C)\n");
175 temp &= 0xfd;
176 pci_write_config_byte(PIIX4_dev, SMBHSTCFG, temp);
177 } else {
178 dev_info(&PIIX4_dev->dev, "Unusual config register "
179 "value\n");
180 dev_info(&PIIX4_dev->dev, "Try using fix_hstcfg=1 if "
181 "you experience problems\n");
182 }
183 }
184
185 /* If force_addr is set, we program the new address here. Just to make
186 sure, we disable the PIIX4 first. */
187 if (force_addr) {
188 pci_write_config_byte(PIIX4_dev, SMBHSTCFG, temp & 0xfe);
189 pci_write_config_word(PIIX4_dev, SMBBA, piix4_smba);
190 pci_write_config_byte(PIIX4_dev, SMBHSTCFG, temp | 0x01);
191 dev_info(&PIIX4_dev->dev, "WARNING: SMBus interface set to "
192 "new address %04x!\n", piix4_smba);
193 } else if ((temp & 1) == 0) {
194 if (force) {
195 /* This should never need to be done, but has been
196 * noted that many Dell machines have the SMBus
197 * interface on the PIIX4 disabled!? NOTE: This assumes
198 * I/O space and other allocations WERE done by the
199 * Bios! Don't complain if your hardware does weird
200 * things after enabling this. :') Check for Bios
201 * updates before resorting to this.
202 */
203 pci_write_config_byte(PIIX4_dev, SMBHSTCFG,
204 temp | 1);
205 dev_printk(KERN_NOTICE, &PIIX4_dev->dev,
206 "WARNING: SMBus interface has been "
207 "FORCEFULLY ENABLED!\n");
208 } else {
209 dev_err(&PIIX4_dev->dev,
210 "Host SMBus controller not enabled!\n");
211 release_region(piix4_smba, SMBIOSIZE);
212 piix4_smba = 0;
213 return -ENODEV;
214 }
215 }
216
217 if ((temp & 0x0E) == 8)
218 dev_dbg(&PIIX4_dev->dev, "Using Interrupt 9 for SMBus.\n");
219 else if ((temp & 0x0E) == 0)
220 dev_dbg(&PIIX4_dev->dev, "Using Interrupt SMI# for SMBus.\n");
221 else
222 dev_err(&PIIX4_dev->dev, "Illegal Interrupt configuration "
223 "(or code out of date)!\n");
224
225 pci_read_config_byte(PIIX4_dev, SMBREV, &temp);
226 dev_dbg(&PIIX4_dev->dev, "SMBREV = 0x%X\n", temp);
227 dev_dbg(&PIIX4_dev->dev, "SMBA = 0x%X\n", piix4_smba);
228
229 return 0;
230}
231
232/* Another internally used function */
233static int piix4_transaction(void)
234{
235 int temp;
236 int result = 0;
237 int timeout = 0;
238
239 dev_dbg(&piix4_adapter.dev, "Transaction (pre): CNT=%02x, CMD=%02x, "
240 "ADD=%02x, DAT0=%02x, DAT1=%02x\n", inb_p(SMBHSTCNT),
241 inb_p(SMBHSTCMD), inb_p(SMBHSTADD), inb_p(SMBHSTDAT0),
242 inb_p(SMBHSTDAT1));
243
244 /* Make sure the SMBus host is ready to start transmitting */
245 if ((temp = inb_p(SMBHSTSTS)) != 0x00) {
246 dev_dbg(&piix4_adapter.dev, "SMBus busy (%02x). "
247 "Resetting... \n", temp);
248 outb_p(temp, SMBHSTSTS);
249 if ((temp = inb_p(SMBHSTSTS)) != 0x00) {
250 dev_err(&piix4_adapter.dev, "Failed! (%02x)\n", temp);
251 return -1;
252 } else {
253 dev_dbg(&piix4_adapter.dev, "Successfull!\n");
254 }
255 }
256
257 /* start the transaction by setting bit 6 */
258 outb_p(inb(SMBHSTCNT) | 0x040, SMBHSTCNT);
259
260 /* We will always wait for a fraction of a second! (See PIIX4 docs errata) */
261 do {
262 msleep(1);
263 temp = inb_p(SMBHSTSTS);
264 } while ((temp & 0x01) && (timeout++ < MAX_TIMEOUT));
265
266 /* If the SMBus is still busy, we give up */
267 if (timeout >= MAX_TIMEOUT) {
268 dev_err(&piix4_adapter.dev, "SMBus Timeout!\n");
269 result = -1;
270 }
271
272 if (temp & 0x10) {
273 result = -1;
274 dev_err(&piix4_adapter.dev, "Error: Failed bus transaction\n");
275 }
276
277 if (temp & 0x08) {
278 result = -1;
279 dev_dbg(&piix4_adapter.dev, "Bus collision! SMBus may be "
280 "locked until next hard reset. (sorry!)\n");
281 /* Clock stops and slave is stuck in mid-transmission */
282 }
283
284 if (temp & 0x04) {
285 result = -1;
286 dev_dbg(&piix4_adapter.dev, "Error: no response!\n");
287 }
288
289 if (inb_p(SMBHSTSTS) != 0x00)
290 outb_p(inb(SMBHSTSTS), SMBHSTSTS);
291
292 if ((temp = inb_p(SMBHSTSTS)) != 0x00) {
293 dev_err(&piix4_adapter.dev, "Failed reset at end of "
294 "transaction (%02x)\n", temp);
295 }
296 dev_dbg(&piix4_adapter.dev, "Transaction (post): CNT=%02x, CMD=%02x, "
297 "ADD=%02x, DAT0=%02x, DAT1=%02x\n", inb_p(SMBHSTCNT),
298 inb_p(SMBHSTCMD), inb_p(SMBHSTADD), inb_p(SMBHSTDAT0),
299 inb_p(SMBHSTDAT1));
300 return result;
301}
302
303/* Return -1 on error. */
304static s32 piix4_access(struct i2c_adapter * adap, u16 addr,
305 unsigned short flags, char read_write,
306 u8 command, int size, union i2c_smbus_data * data)
307{
308 int i, len;
309
310 switch (size) {
311 case I2C_SMBUS_PROC_CALL:
312 dev_err(&adap->dev, "I2C_SMBUS_PROC_CALL not supported!\n");
313 return -1;
314 case I2C_SMBUS_QUICK:
315 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
316 SMBHSTADD);
317 size = PIIX4_QUICK;
318 break;
319 case I2C_SMBUS_BYTE:
320 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
321 SMBHSTADD);
322 if (read_write == I2C_SMBUS_WRITE)
323 outb_p(command, SMBHSTCMD);
324 size = PIIX4_BYTE;
325 break;
326 case I2C_SMBUS_BYTE_DATA:
327 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
328 SMBHSTADD);
329 outb_p(command, SMBHSTCMD);
330 if (read_write == I2C_SMBUS_WRITE)
331 outb_p(data->byte, SMBHSTDAT0);
332 size = PIIX4_BYTE_DATA;
333 break;
334 case I2C_SMBUS_WORD_DATA:
335 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
336 SMBHSTADD);
337 outb_p(command, SMBHSTCMD);
338 if (read_write == I2C_SMBUS_WRITE) {
339 outb_p(data->word & 0xff, SMBHSTDAT0);
340 outb_p((data->word & 0xff00) >> 8, SMBHSTDAT1);
341 }
342 size = PIIX4_WORD_DATA;
343 break;
344 case I2C_SMBUS_BLOCK_DATA:
345 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
346 SMBHSTADD);
347 outb_p(command, SMBHSTCMD);
348 if (read_write == I2C_SMBUS_WRITE) {
349 len = data->block[0];
350 if (len < 0)
351 len = 0;
352 if (len > 32)
353 len = 32;
354 outb_p(len, SMBHSTDAT0);
355 i = inb_p(SMBHSTCNT); /* Reset SMBBLKDAT */
356 for (i = 1; i <= len; i++)
357 outb_p(data->block[i], SMBBLKDAT);
358 }
359 size = PIIX4_BLOCK_DATA;
360 break;
361 }
362
363 outb_p((size & 0x1C) + (ENABLE_INT9 & 1), SMBHSTCNT);
364
365 if (piix4_transaction()) /* Error in transaction */
366 return -1;
367
368 if ((read_write == I2C_SMBUS_WRITE) || (size == PIIX4_QUICK))
369 return 0;
370
371
372 switch (size) {
373 case PIIX4_BYTE: /* Where is the result put? I assume here it is in
374 SMBHSTDAT0 but it might just as well be in the
375 SMBHSTCMD. No clue in the docs */
376
377 data->byte = inb_p(SMBHSTDAT0);
378 break;
379 case PIIX4_BYTE_DATA:
380 data->byte = inb_p(SMBHSTDAT0);
381 break;
382 case PIIX4_WORD_DATA:
383 data->word = inb_p(SMBHSTDAT0) + (inb_p(SMBHSTDAT1) << 8);
384 break;
385 case PIIX4_BLOCK_DATA:
386 data->block[0] = inb_p(SMBHSTDAT0);
387 i = inb_p(SMBHSTCNT); /* Reset SMBBLKDAT */
388 for (i = 1; i <= data->block[0]; i++)
389 data->block[i] = inb_p(SMBBLKDAT);
390 break;
391 }
392 return 0;
393}
394
395static u32 piix4_func(struct i2c_adapter *adapter)
396{
397 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
398 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
399 I2C_FUNC_SMBUS_BLOCK_DATA;
400}
401
402static struct i2c_algorithm smbus_algorithm = {
403 .name = "Non-I2C SMBus adapter",
404 .id = I2C_ALGO_SMBUS,
405 .smbus_xfer = piix4_access,
406 .functionality = piix4_func,
407};
408
409static struct i2c_adapter piix4_adapter = {
410 .owner = THIS_MODULE,
411 .class = I2C_CLASS_HWMON,
412 .algo = &smbus_algorithm,
413 .name = "unset",
414};
415
416static struct pci_device_id piix4_ids[] = {
417 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371AB_3),
418 .driver_data = 3 },
419 { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS, PCI_DEVICE_ID_SERVERWORKS_OSB4),
420 .driver_data = 0 },
421 { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS, PCI_DEVICE_ID_SERVERWORKS_CSB5),
422 .driver_data = 0 },
423 { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS, PCI_DEVICE_ID_SERVERWORKS_CSB6),
424 .driver_data = 0 },
425 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82443MX_3),
426 .driver_data = 3 },
427 { PCI_DEVICE(PCI_VENDOR_ID_EFAR, PCI_DEVICE_ID_EFAR_SLC90E66_3),
428 .driver_data = 0 },
429 { 0, }
430};
431
432MODULE_DEVICE_TABLE (pci, piix4_ids);
433
434static int __devinit piix4_probe(struct pci_dev *dev,
435 const struct pci_device_id *id)
436{
437 int retval;
438
439 retval = piix4_setup(dev, id);
440 if (retval)
441 return retval;
442
443 /* set up the driverfs linkage to our parent device */
444 piix4_adapter.dev.parent = &dev->dev;
445
446 snprintf(piix4_adapter.name, I2C_NAME_SIZE,
447 "SMBus PIIX4 adapter at %04x", piix4_smba);
448
449 if ((retval = i2c_add_adapter(&piix4_adapter))) {
450 dev_err(&dev->dev, "Couldn't register adapter!\n");
451 release_region(piix4_smba, SMBIOSIZE);
452 piix4_smba = 0;
453 }
454
455 return retval;
456}
457
458static void __devexit piix4_remove(struct pci_dev *dev)
459{
460 if (piix4_smba) {
461 i2c_del_adapter(&piix4_adapter);
462 release_region(piix4_smba, SMBIOSIZE);
463 piix4_smba = 0;
464 }
465}
466
467static struct pci_driver piix4_driver = {
468 .name = "piix4_smbus",
469 .id_table = piix4_ids,
470 .probe = piix4_probe,
471 .remove = __devexit_p(piix4_remove),
472};
473
474static int __init i2c_piix4_init(void)
475{
476 return pci_register_driver(&piix4_driver);
477}
478
479static void __exit i2c_piix4_exit(void)
480{
481 pci_unregister_driver(&piix4_driver);
482}
483
484MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and "
485 "Philip Edelbrock <phil@netroedge.com>");
486MODULE_DESCRIPTION("PIIX4 SMBus driver");
487MODULE_LICENSE("GPL");
488
489module_init(i2c_piix4_init);
490module_exit(i2c_piix4_exit);
diff --git a/drivers/i2c/busses/i2c-prosavage.c b/drivers/i2c/busses/i2c-prosavage.c
new file mode 100644
index 000000000000..13d66289933b
--- /dev/null
+++ b/drivers/i2c/busses/i2c-prosavage.c
@@ -0,0 +1,334 @@
1/*
2 * kernel/busses/i2c-prosavage.c
3 *
4 * i2c bus driver for S3/VIA 8365/8375 graphics processor.
5 * Copyright (c) 2003 Henk Vergonet <henk@god.dyndns.org>
6 * Based on code written by:
7 * Frodo Looijaard <frodol@dds.nl>,
8 * Philip Edelbrock <phil@netroedge.com>,
9 * Ralph Metzler <rjkm@thp.uni-koeln.de>, and
10 * Mark D. Studebaker <mdsxyz123@yahoo.com>
11 * Simon Vogl
12 * and others
13 *
14 * Please read the lm_sensors documentation for details on use.
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 *
30 */
31/* 18-05-2003 HVE - created
32 * 14-06-2003 HVE - adapted for lm_sensors2
33 * 17-06-2003 HVE - linux 2.5.xx compatible
34 * 18-06-2003 HVE - codingstyle
35 * 21-06-2003 HVE - compatibility lm_sensors2 and linux 2.5.xx
36 * codingstyle, mmio enabled
37 *
38 * This driver interfaces to the I2C bus of the VIA north bridge embedded
39 * ProSavage4/8 devices. Usefull for gaining access to the TV Encoder chips.
40 *
41 * Graphics cores:
42 * S3/VIA KM266/VT8375 aka ProSavage8
43 * S3/VIA KM133/VT8365 aka Savage4
44 *
45 * Two serial busses are implemented:
46 * SERIAL1 - I2C serial communications interface
47 * SERIAL2 - DDC2 monitor communications interface
48 *
49 * Tested on a FX41 mainboard, see http://www.shuttle.com
50 *
51 *
52 * TODO:
53 * - integration with prosavage framebuffer device
54 * (Additional documentation needed :(
55 */
56
57#include <linux/config.h>
58#include <linux/module.h>
59#include <linux/init.h>
60#include <linux/pci.h>
61#include <linux/i2c.h>
62#include <linux/i2c-algo-bit.h>
63#include <asm/io.h>
64
65/*
66 * driver configuration
67 */
68#define MAX_BUSSES 2
69
70struct s_i2c_bus {
71 void __iomem *mmvga;
72 int i2c_reg;
73 int adap_ok;
74 struct i2c_adapter adap;
75 struct i2c_algo_bit_data algo;
76};
77
78struct s_i2c_chip {
79 void __iomem *mmio;
80 struct s_i2c_bus i2c_bus[MAX_BUSSES];
81};
82
83
84/*
85 * i2c configuration
86 */
87#ifndef I2C_HW_B_S3VIA
88#define I2C_HW_B_S3VIA 0x18 /* S3VIA ProSavage adapter */
89#endif
90
91/* delays */
92#define CYCLE_DELAY 10
93#define TIMEOUT (HZ / 2)
94
95
96/*
97 * S3/VIA 8365/8375 registers
98 */
99#define VGA_CR_IX 0x3d4
100#define VGA_CR_DATA 0x3d5
101
102#define CR_SERIAL1 0xa0 /* I2C serial communications interface */
103#define MM_SERIAL1 0xff20
104#define CR_SERIAL2 0xb1 /* DDC2 monitor communications interface */
105
106/* based on vt8365 documentation */
107#define I2C_ENAB 0x10
108#define I2C_SCL_OUT 0x01
109#define I2C_SDA_OUT 0x02
110#define I2C_SCL_IN 0x04
111#define I2C_SDA_IN 0x08
112
113#define SET_CR_IX(p, val) writeb((val), (p)->mmvga + VGA_CR_IX)
114#define SET_CR_DATA(p, val) writeb((val), (p)->mmvga + VGA_CR_DATA)
115#define GET_CR_DATA(p) readb((p)->mmvga + VGA_CR_DATA)
116
117
118/*
119 * Serial bus line handling
120 *
121 * serial communications register as parameter in private data
122 *
123 * TODO: locks with other code sections accessing video registers?
124 */
125static void bit_s3via_setscl(void *bus, int val)
126{
127 struct s_i2c_bus *p = (struct s_i2c_bus *)bus;
128 unsigned int r;
129
130 SET_CR_IX(p, p->i2c_reg);
131 r = GET_CR_DATA(p);
132 r |= I2C_ENAB;
133 if (val) {
134 r |= I2C_SCL_OUT;
135 } else {
136 r &= ~I2C_SCL_OUT;
137 }
138 SET_CR_DATA(p, r);
139}
140
141static void bit_s3via_setsda(void *bus, int val)
142{
143 struct s_i2c_bus *p = (struct s_i2c_bus *)bus;
144 unsigned int r;
145
146 SET_CR_IX(p, p->i2c_reg);
147 r = GET_CR_DATA(p);
148 r |= I2C_ENAB;
149 if (val) {
150 r |= I2C_SDA_OUT;
151 } else {
152 r &= ~I2C_SDA_OUT;
153 }
154 SET_CR_DATA(p, r);
155}
156
157static int bit_s3via_getscl(void *bus)
158{
159 struct s_i2c_bus *p = (struct s_i2c_bus *)bus;
160
161 SET_CR_IX(p, p->i2c_reg);
162 return (0 != (GET_CR_DATA(p) & I2C_SCL_IN));
163}
164
165static int bit_s3via_getsda(void *bus)
166{
167 struct s_i2c_bus *p = (struct s_i2c_bus *)bus;
168
169 SET_CR_IX(p, p->i2c_reg);
170 return (0 != (GET_CR_DATA(p) & I2C_SDA_IN));
171}
172
173
174/*
175 * adapter initialisation
176 */
177static int i2c_register_bus(struct pci_dev *dev, struct s_i2c_bus *p, void __iomem *mmvga, u32 i2c_reg)
178{
179 int ret;
180 p->adap.owner = THIS_MODULE;
181 p->adap.id = I2C_HW_B_S3VIA;
182 p->adap.algo_data = &p->algo;
183 p->adap.dev.parent = &dev->dev;
184 p->algo.setsda = bit_s3via_setsda;
185 p->algo.setscl = bit_s3via_setscl;
186 p->algo.getsda = bit_s3via_getsda;
187 p->algo.getscl = bit_s3via_getscl;
188 p->algo.udelay = CYCLE_DELAY;
189 p->algo.mdelay = CYCLE_DELAY;
190 p->algo.timeout = TIMEOUT;
191 p->algo.data = p;
192 p->mmvga = mmvga;
193 p->i2c_reg = i2c_reg;
194
195 ret = i2c_bit_add_bus(&p->adap);
196 if (ret) {
197 return ret;
198 }
199
200 p->adap_ok = 1;
201 return 0;
202}
203
204
205/*
206 * Cleanup stuff
207 */
208static void prosavage_remove(struct pci_dev *dev)
209{
210 struct s_i2c_chip *chip;
211 int i, ret;
212
213 chip = (struct s_i2c_chip *)pci_get_drvdata(dev);
214
215 if (!chip) {
216 return;
217 }
218 for (i = MAX_BUSSES - 1; i >= 0; i--) {
219 if (chip->i2c_bus[i].adap_ok == 0)
220 continue;
221
222 ret = i2c_bit_del_bus(&chip->i2c_bus[i].adap);
223 if (ret) {
224 dev_err(&dev->dev, "%s not removed\n",
225 chip->i2c_bus[i].adap.name);
226 }
227 }
228 if (chip->mmio) {
229 iounmap(chip->mmio);
230 }
231 kfree(chip);
232}
233
234
235/*
236 * Detect chip and initialize it
237 */
238static int __devinit prosavage_probe(struct pci_dev *dev, const struct pci_device_id *id)
239{
240 int ret;
241 unsigned long base, len;
242 struct s_i2c_chip *chip;
243 struct s_i2c_bus *bus;
244
245 pci_set_drvdata(dev, kmalloc(sizeof(struct s_i2c_chip), GFP_KERNEL));
246 chip = (struct s_i2c_chip *)pci_get_drvdata(dev);
247 if (chip == NULL) {
248 return -ENOMEM;
249 }
250
251 memset(chip, 0, sizeof(struct s_i2c_chip));
252
253 base = dev->resource[0].start & PCI_BASE_ADDRESS_MEM_MASK;
254 len = dev->resource[0].end - base + 1;
255 chip->mmio = ioremap_nocache(base, len);
256
257 if (chip->mmio == NULL) {
258 dev_err(&dev->dev, "ioremap failed\n");
259 prosavage_remove(dev);
260 return -ENODEV;
261 }
262
263
264 /*
265 * Chip initialisation
266 */
267 /* Unlock Extended IO Space ??? */
268
269
270 /*
271 * i2c bus registration
272 */
273 bus = &chip->i2c_bus[0];
274 snprintf(bus->adap.name, sizeof(bus->adap.name),
275 "ProSavage I2C bus at %02x:%02x.%x",
276 dev->bus->number, PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn));
277 ret = i2c_register_bus(dev, bus, chip->mmio + 0x8000, CR_SERIAL1);
278 if (ret) {
279 goto err_adap;
280 }
281 /*
282 * ddc bus registration
283 */
284 bus = &chip->i2c_bus[1];
285 snprintf(bus->adap.name, sizeof(bus->adap.name),
286 "ProSavage DDC bus at %02x:%02x.%x",
287 dev->bus->number, PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn));
288 ret = i2c_register_bus(dev, bus, chip->mmio + 0x8000, CR_SERIAL2);
289 if (ret) {
290 goto err_adap;
291 }
292 return 0;
293err_adap:
294 dev_err(&dev->dev, "%s failed\n", bus->adap.name);
295 prosavage_remove(dev);
296 return ret;
297}
298
299
300/*
301 * Data for PCI driver interface
302 */
303static struct pci_device_id prosavage_pci_tbl[] = {
304 { PCI_DEVICE(PCI_VENDOR_ID_S3, PCI_DEVICE_ID_S3_SAVAGE4) },
305 { PCI_DEVICE(PCI_VENDOR_ID_S3, PCI_DEVICE_ID_S3_PROSAVAGE8) },
306 { 0, },
307};
308
309MODULE_DEVICE_TABLE (pci, prosavage_pci_tbl);
310
311static struct pci_driver prosavage_driver = {
312 .name = "prosavage_smbus",
313 .id_table = prosavage_pci_tbl,
314 .probe = prosavage_probe,
315 .remove = prosavage_remove,
316};
317
318static int __init i2c_prosavage_init(void)
319{
320 return pci_register_driver(&prosavage_driver);
321}
322
323static void __exit i2c_prosavage_exit(void)
324{
325 pci_unregister_driver(&prosavage_driver);
326}
327
328MODULE_DEVICE_TABLE(pci, prosavage_pci_tbl);
329MODULE_AUTHOR("Henk Vergonet");
330MODULE_DESCRIPTION("ProSavage VIA 8365/8375 smbus driver");
331MODULE_LICENSE("GPL");
332
333module_init (i2c_prosavage_init);
334module_exit (i2c_prosavage_exit);
diff --git a/drivers/i2c/busses/i2c-rpx.c b/drivers/i2c/busses/i2c-rpx.c
new file mode 100644
index 000000000000..9497b1b6852f
--- /dev/null
+++ b/drivers/i2c/busses/i2c-rpx.c
@@ -0,0 +1,102 @@
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/config.h>
15#include <linux/kernel.h>
16#include <linux/module.h>
17#include <linux/init.h>
18#include <linux/stddef.h>
19#include <linux/i2c.h>
20#include <linux/i2c-algo-8xx.h>
21#include <asm/mpc8xx.h>
22#include <asm/commproc.h>
23
24
25static void
26rpx_iic_init(struct i2c_algo_8xx_data *data)
27{
28 volatile cpm8xx_t *cp;
29 volatile immap_t *immap;
30
31 cp = cpmp; /* Get pointer to Communication Processor */
32 immap = (immap_t *)IMAP_ADDR; /* and to internal registers */
33
34 data->iip = (iic_t *)&cp->cp_dparam[PROFF_IIC];
35
36 /* Check for and use a microcode relocation patch.
37 */
38 if ((data->reloc = data->iip->iic_rpbase))
39 data->iip = (iic_t *)&cp->cp_dpmem[data->iip->iic_rpbase];
40
41 data->i2c = (i2c8xx_t *)&(immap->im_i2c);
42 data->cp = cp;
43
44 /* Initialize Port B IIC pins.
45 */
46 cp->cp_pbpar |= 0x00000030;
47 cp->cp_pbdir |= 0x00000030;
48 cp->cp_pbodr |= 0x00000030;
49
50 /* Allocate space for two transmit and two receive buffer
51 * descriptors in the DP ram.
52 */
53 data->dp_addr = cpm_dpalloc(sizeof(cbd_t) * 4, 8);
54
55 /* ptr to i2c area */
56 data->i2c = (i2c8xx_t *)&(((immap_t *)IMAP_ADDR)->im_i2c);
57}
58
59static int rpx_install_isr(int irq, void (*func)(void *, void *), void *data)
60{
61 /* install interrupt handler */
62 cpm_install_handler(irq, (void (*)(void *, struct pt_regs *)) func, data);
63
64 return 0;
65}
66
67static struct i2c_algo_8xx_data rpx_data = {
68 .setisr = rpx_install_isr
69};
70
71static struct i2c_adapter rpx_ops = {
72 .owner = THIS_MODULE,
73 .name = "m8xx",
74 .id = I2C_HW_MPC8XX_EPON,
75 .algo_data = &rpx_data,
76};
77
78int __init i2c_rpx_init(void)
79{
80 printk(KERN_INFO "i2c-rpx: i2c MPC8xx driver\n");
81
82 /* reset hardware to sane state */
83 rpx_iic_init(&rpx_data);
84
85 if (i2c_8xx_add_bus(&rpx_ops) < 0) {
86 printk(KERN_ERR "i2c-rpx: Unable to register with I2C\n");
87 return -ENODEV;
88 }
89
90 return 0;
91}
92
93void __exit i2c_rpx_exit(void)
94{
95 i2c_8xx_del_bus(&rpx_ops);
96}
97
98MODULE_AUTHOR("Dan Malek <dmalek@jlc.net>");
99MODULE_DESCRIPTION("I2C-Bus adapter routines for MPC8xx boards");
100
101module_init(i2c_rpx_init);
102module_exit(i2c_rpx_exit);
diff --git a/drivers/i2c/busses/i2c-s3c2410.c b/drivers/i2c/busses/i2c-s3c2410.c
new file mode 100644
index 000000000000..fcfa51c1436b
--- /dev/null
+++ b/drivers/i2c/busses/i2c-s3c2410.c
@@ -0,0 +1,938 @@
1/* linux/drivers/i2c/busses/i2c-s3c2410.c
2 *
3 * Copyright (C) 2004,2005 Simtec Electronics
4 * Ben Dooks <ben@simtec.co.uk>
5 *
6 * S3C2410 I2C Controller
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; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21*/
22
23#include <linux/kernel.h>
24#include <linux/module.h>
25
26#include <linux/i2c.h>
27#include <linux/i2c-id.h>
28#include <linux/init.h>
29#include <linux/time.h>
30#include <linux/interrupt.h>
31#include <linux/sched.h>
32#include <linux/delay.h>
33#include <linux/errno.h>
34#include <linux/err.h>
35#include <linux/device.h>
36
37#include <asm/hardware.h>
38#include <asm/irq.h>
39#include <asm/io.h>
40
41#include <asm/hardware/clock.h>
42#include <asm/arch/regs-gpio.h>
43#include <asm/arch/regs-iic.h>
44#include <asm/arch/iic.h>
45
46/* i2c controller state */
47
48enum s3c24xx_i2c_state {
49 STATE_IDLE,
50 STATE_START,
51 STATE_READ,
52 STATE_WRITE,
53 STATE_STOP
54};
55
56struct s3c24xx_i2c {
57 spinlock_t lock;
58 wait_queue_head_t wait;
59
60 struct i2c_msg *msg;
61 unsigned int msg_num;
62 unsigned int msg_idx;
63 unsigned int msg_ptr;
64
65 enum s3c24xx_i2c_state state;
66
67 void __iomem *regs;
68 struct clk *clk;
69 struct device *dev;
70 struct resource *irq;
71 struct resource *ioarea;
72 struct i2c_adapter adap;
73};
74
75/* default platform data to use if not supplied in the platform_device
76*/
77
78static struct s3c2410_platform_i2c s3c24xx_i2c_default_platform = {
79 .flags = 0,
80 .slave_addr = 0x10,
81 .bus_freq = 100*1000,
82 .max_freq = 400*1000,
83 .sda_delay = S3C2410_IICLC_SDA_DELAY5 | S3C2410_IICLC_FILTER_ON,
84};
85
86/* s3c24xx_i2c_is2440()
87 *
88 * return true is this is an s3c2440
89*/
90
91static inline int s3c24xx_i2c_is2440(struct s3c24xx_i2c *i2c)
92{
93 struct platform_device *pdev = to_platform_device(i2c->dev);
94
95 return !strcmp(pdev->name, "s3c2440-i2c");
96}
97
98
99/* s3c24xx_i2c_get_platformdata
100 *
101 * get the platform data associated with the given device, or return
102 * the default if there is none
103*/
104
105static inline struct s3c2410_platform_i2c *s3c24xx_i2c_get_platformdata(struct device *dev)
106{
107 if (dev->platform_data != NULL)
108 return (struct s3c2410_platform_i2c *)dev->platform_data;
109
110 return &s3c24xx_i2c_default_platform;
111}
112
113/* s3c24xx_i2c_master_complete
114 *
115 * complete the message and wake up the caller, using the given return code,
116 * or zero to mean ok.
117*/
118
119static inline void s3c24xx_i2c_master_complete(struct s3c24xx_i2c *i2c, int ret)
120{
121 dev_dbg(i2c->dev, "master_complete %d\n", ret);
122
123 i2c->msg_ptr = 0;
124 i2c->msg = NULL;
125 i2c->msg_idx ++;
126 i2c->msg_num = 0;
127 if (ret)
128 i2c->msg_idx = ret;
129
130 wake_up(&i2c->wait);
131}
132
133static inline void s3c24xx_i2c_disable_ack(struct s3c24xx_i2c *i2c)
134{
135 unsigned long tmp;
136
137 tmp = readl(i2c->regs + S3C2410_IICCON);
138 writel(tmp & ~S3C2410_IICCON_ACKEN, i2c->regs + S3C2410_IICCON);
139
140}
141
142static inline void s3c24xx_i2c_enable_ack(struct s3c24xx_i2c *i2c)
143{
144 unsigned long tmp;
145
146 tmp = readl(i2c->regs + S3C2410_IICCON);
147 writel(tmp | S3C2410_IICCON_ACKEN, i2c->regs + S3C2410_IICCON);
148
149}
150
151/* irq enable/disable functions */
152
153static inline void s3c24xx_i2c_disable_irq(struct s3c24xx_i2c *i2c)
154{
155 unsigned long tmp;
156
157 tmp = readl(i2c->regs + S3C2410_IICCON);
158 writel(tmp & ~S3C2410_IICCON_IRQEN, i2c->regs + S3C2410_IICCON);
159}
160
161static inline void s3c24xx_i2c_enable_irq(struct s3c24xx_i2c *i2c)
162{
163 unsigned long tmp;
164
165 tmp = readl(i2c->regs + S3C2410_IICCON);
166 writel(tmp | S3C2410_IICCON_IRQEN, i2c->regs + S3C2410_IICCON);
167}
168
169
170/* s3c24xx_i2c_message_start
171 *
172 * put the start of a message onto the bus
173*/
174
175static void s3c24xx_i2c_message_start(struct s3c24xx_i2c *i2c,
176 struct i2c_msg *msg)
177{
178 unsigned int addr = (msg->addr & 0x7f) << 1;
179 unsigned long stat;
180 unsigned long iiccon;
181
182 stat = 0;
183 stat |= S3C2410_IICSTAT_TXRXEN;
184
185 if (msg->flags & I2C_M_RD) {
186 stat |= S3C2410_IICSTAT_MASTER_RX;
187 addr |= 1;
188 } else
189 stat |= S3C2410_IICSTAT_MASTER_TX;
190
191 if (msg->flags & I2C_M_REV_DIR_ADDR)
192 addr ^= 1;
193
194 // todo - check for wether ack wanted or not
195 s3c24xx_i2c_enable_ack(i2c);
196
197 iiccon = readl(i2c->regs + S3C2410_IICCON);
198 writel(stat, i2c->regs + S3C2410_IICSTAT);
199
200 dev_dbg(i2c->dev, "START: %08lx to IICSTAT, %02x to DS\n", stat, addr);
201 writeb(addr, i2c->regs + S3C2410_IICDS);
202
203 // delay a bit and reset iiccon before setting start (per samsung)
204 udelay(1);
205 dev_dbg(i2c->dev, "iiccon, %08lx\n", iiccon);
206 writel(iiccon, i2c->regs + S3C2410_IICCON);
207
208 stat |= S3C2410_IICSTAT_START;
209 writel(stat, i2c->regs + S3C2410_IICSTAT);
210}
211
212static inline void s3c24xx_i2c_stop(struct s3c24xx_i2c *i2c, int ret)
213{
214 unsigned long iicstat = readl(i2c->regs + S3C2410_IICSTAT);
215
216 dev_dbg(i2c->dev, "STOP\n");
217
218 /* stop the transfer */
219 iicstat &= ~ S3C2410_IICSTAT_START;
220 writel(iicstat, i2c->regs + S3C2410_IICSTAT);
221
222 i2c->state = STATE_STOP;
223
224 s3c24xx_i2c_master_complete(i2c, ret);
225 s3c24xx_i2c_disable_irq(i2c);
226}
227
228/* helper functions to determine the current state in the set of
229 * messages we are sending */
230
231/* is_lastmsg()
232 *
233 * returns TRUE if the current message is the last in the set
234*/
235
236static inline int is_lastmsg(struct s3c24xx_i2c *i2c)
237{
238 return i2c->msg_idx >= (i2c->msg_num - 1);
239}
240
241/* is_msglast
242 *
243 * returns TRUE if we this is the last byte in the current message
244*/
245
246static inline int is_msglast(struct s3c24xx_i2c *i2c)
247{
248 return i2c->msg_ptr == i2c->msg->len-1;
249}
250
251/* is_msgend
252 *
253 * returns TRUE if we reached the end of the current message
254*/
255
256static inline int is_msgend(struct s3c24xx_i2c *i2c)
257{
258 return i2c->msg_ptr >= i2c->msg->len;
259}
260
261/* i2s_s3c_irq_nextbyte
262 *
263 * process an interrupt and work out what to do
264 */
265
266static int i2s_s3c_irq_nextbyte(struct s3c24xx_i2c *i2c, unsigned long iicstat)
267{
268 unsigned long tmp;
269 unsigned char byte;
270 int ret = 0;
271
272 switch (i2c->state) {
273
274 case STATE_IDLE:
275 dev_err(i2c->dev, "%s: called in STATE_IDLE\n", __FUNCTION__);
276 goto out;
277 break;
278
279 case STATE_STOP:
280 dev_err(i2c->dev, "%s: called in STATE_STOP\n", __FUNCTION__);
281 s3c24xx_i2c_disable_irq(i2c);
282 goto out_ack;
283
284 case STATE_START:
285 /* last thing we did was send a start condition on the
286 * bus, or started a new i2c message
287 */
288
289 if (iicstat & S3C2410_IICSTAT_LASTBIT &&
290 !(i2c->msg->flags & I2C_M_IGNORE_NAK)) {
291 /* ack was not received... */
292
293 dev_dbg(i2c->dev, "ack was not received\n");
294 s3c24xx_i2c_stop(i2c, -EREMOTEIO);
295 goto out_ack;
296 }
297
298 if (i2c->msg->flags & I2C_M_RD)
299 i2c->state = STATE_READ;
300 else
301 i2c->state = STATE_WRITE;
302
303 /* terminate the transfer if there is nothing to do
304 * (used by the i2c probe to find devices */
305
306 if (is_lastmsg(i2c) && i2c->msg->len == 0) {
307 s3c24xx_i2c_stop(i2c, 0);
308 goto out_ack;
309 }
310
311 if (i2c->state == STATE_READ)
312 goto prepare_read;
313
314 /* fall through to the write state, as we will need to
315 * send a byte as well */
316
317 case STATE_WRITE:
318 /* we are writing data to the device... check for the
319 * end of the message, and if so, work out what to do
320 */
321
322 retry_write:
323 if (!is_msgend(i2c)) {
324 byte = i2c->msg->buf[i2c->msg_ptr++];
325 writeb(byte, i2c->regs + S3C2410_IICDS);
326
327 } else if (!is_lastmsg(i2c)) {
328 /* we need to go to the next i2c message */
329
330 dev_dbg(i2c->dev, "WRITE: Next Message\n");
331
332 i2c->msg_ptr = 0;
333 i2c->msg_idx ++;
334 i2c->msg++;
335
336 /* check to see if we need to do another message */
337 if (i2c->msg->flags & I2C_M_NOSTART) {
338
339 if (i2c->msg->flags & I2C_M_RD) {
340 /* cannot do this, the controller
341 * forces us to send a new START
342 * when we change direction */
343
344 s3c24xx_i2c_stop(i2c, -EINVAL);
345 }
346
347 goto retry_write;
348 } else {
349
350 /* send the new start */
351 s3c24xx_i2c_message_start(i2c, i2c->msg);
352 i2c->state = STATE_START;
353 }
354
355 } else {
356 /* send stop */
357
358 s3c24xx_i2c_stop(i2c, 0);
359 }
360 break;
361
362 case STATE_READ:
363 /* we have a byte of data in the data register, do
364 * something with it, and then work out wether we are
365 * going to do any more read/write
366 */
367
368 if (!(i2c->msg->flags & I2C_M_IGNORE_NAK) &&
369 !(is_msglast(i2c) && is_lastmsg(i2c))) {
370
371 if (iicstat & S3C2410_IICSTAT_LASTBIT) {
372 dev_dbg(i2c->dev, "READ: No Ack\n");
373
374 s3c24xx_i2c_stop(i2c, -ECONNREFUSED);
375 goto out_ack;
376 }
377 }
378
379 byte = readb(i2c->regs + S3C2410_IICDS);
380 i2c->msg->buf[i2c->msg_ptr++] = byte;
381
382 prepare_read:
383 if (is_msglast(i2c)) {
384 /* last byte of buffer */
385
386 if (is_lastmsg(i2c))
387 s3c24xx_i2c_disable_ack(i2c);
388
389 } else if (is_msgend(i2c)) {
390 /* ok, we've read the entire buffer, see if there
391 * is anything else we need to do */
392
393 if (is_lastmsg(i2c)) {
394 /* last message, send stop and complete */
395 dev_dbg(i2c->dev, "READ: Send Stop\n");
396
397 s3c24xx_i2c_stop(i2c, 0);
398 } else {
399 /* go to the next transfer */
400 dev_dbg(i2c->dev, "READ: Next Transfer\n");
401
402 i2c->msg_ptr = 0;
403 i2c->msg_idx++;
404 i2c->msg++;
405 }
406 }
407
408 break;
409 }
410
411 /* acknowlegde the IRQ and get back on with the work */
412
413 out_ack:
414 tmp = readl(i2c->regs + S3C2410_IICCON);
415 tmp &= ~S3C2410_IICCON_IRQPEND;
416 writel(tmp, i2c->regs + S3C2410_IICCON);
417 out:
418 return ret;
419}
420
421/* s3c24xx_i2c_irq
422 *
423 * top level IRQ servicing routine
424*/
425
426static irqreturn_t s3c24xx_i2c_irq(int irqno, void *dev_id,
427 struct pt_regs *regs)
428{
429 struct s3c24xx_i2c *i2c = dev_id;
430 unsigned long status;
431 unsigned long tmp;
432
433 status = readl(i2c->regs + S3C2410_IICSTAT);
434
435 if (status & S3C2410_IICSTAT_ARBITR) {
436 // deal with arbitration loss
437 dev_err(i2c->dev, "deal with arbitration loss\n");
438 }
439
440 if (i2c->state == STATE_IDLE) {
441 dev_dbg(i2c->dev, "IRQ: error i2c->state == IDLE\n");
442
443 tmp = readl(i2c->regs + S3C2410_IICCON);
444 tmp &= ~S3C2410_IICCON_IRQPEND;
445 writel(tmp, i2c->regs + S3C2410_IICCON);
446 goto out;
447 }
448
449 /* pretty much this leaves us with the fact that we've
450 * transmitted or received whatever byte we last sent */
451
452 i2s_s3c_irq_nextbyte(i2c, status);
453
454 out:
455 return IRQ_HANDLED;
456}
457
458
459/* s3c24xx_i2c_set_master
460 *
461 * get the i2c bus for a master transaction
462*/
463
464static int s3c24xx_i2c_set_master(struct s3c24xx_i2c *i2c)
465{
466 unsigned long iicstat;
467 int timeout = 400;
468
469 while (timeout-- > 0) {
470 iicstat = readl(i2c->regs + S3C2410_IICSTAT);
471
472 if (!(iicstat & S3C2410_IICSTAT_BUSBUSY))
473 return 0;
474
475 msleep(1);
476 }
477
478 dev_dbg(i2c->dev, "timeout: GPEDAT is %08x\n",
479 __raw_readl(S3C2410_GPEDAT));
480
481 return -ETIMEDOUT;
482}
483
484/* s3c24xx_i2c_doxfer
485 *
486 * this starts an i2c transfer
487*/
488
489static int s3c24xx_i2c_doxfer(struct s3c24xx_i2c *i2c, struct i2c_msg *msgs, int num)
490{
491 unsigned long timeout;
492 int ret;
493
494 ret = s3c24xx_i2c_set_master(i2c);
495 if (ret != 0) {
496 dev_err(i2c->dev, "cannot get bus (error %d)\n", ret);
497 ret = -EAGAIN;
498 goto out;
499 }
500
501 spin_lock_irq(&i2c->lock);
502
503 i2c->msg = msgs;
504 i2c->msg_num = num;
505 i2c->msg_ptr = 0;
506 i2c->msg_idx = 0;
507 i2c->state = STATE_START;
508
509 s3c24xx_i2c_enable_irq(i2c);
510 s3c24xx_i2c_message_start(i2c, msgs);
511 spin_unlock_irq(&i2c->lock);
512
513 timeout = wait_event_timeout(i2c->wait, i2c->msg_num == 0, HZ * 5);
514
515 ret = i2c->msg_idx;
516
517 /* having these next two as dev_err() makes life very
518 * noisy when doing an i2cdetect */
519
520 if (timeout == 0)
521 dev_dbg(i2c->dev, "timeout\n");
522 else if (ret != num)
523 dev_dbg(i2c->dev, "incomplete xfer (%d)\n", ret);
524
525 /* ensure the stop has been through the bus */
526
527 msleep(1);
528
529 out:
530 return ret;
531}
532
533/* s3c24xx_i2c_xfer
534 *
535 * first port of call from the i2c bus code when an message needs
536 * transfering across the i2c bus.
537*/
538
539static int s3c24xx_i2c_xfer(struct i2c_adapter *adap,
540 struct i2c_msg *msgs, int num)
541{
542 struct s3c24xx_i2c *i2c = (struct s3c24xx_i2c *)adap->algo_data;
543 int retry;
544 int ret;
545
546 for (retry = 0; retry < adap->retries; retry++) {
547
548 ret = s3c24xx_i2c_doxfer(i2c, msgs, num);
549
550 if (ret != -EAGAIN)
551 return ret;
552
553 dev_dbg(i2c->dev, "Retrying transmission (%d)\n", retry);
554
555 udelay(100);
556 }
557
558 return -EREMOTEIO;
559}
560
561/* declare our i2c functionality */
562static u32 s3c24xx_i2c_func(struct i2c_adapter *adap)
563{
564 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_PROTOCOL_MANGLING;
565}
566
567/* i2c bus registration info */
568
569static struct i2c_algorithm s3c24xx_i2c_algorithm = {
570 .name = "S3C2410-I2C-Algorithm",
571 .master_xfer = s3c24xx_i2c_xfer,
572 .functionality = s3c24xx_i2c_func,
573};
574
575static struct s3c24xx_i2c s3c24xx_i2c = {
576 .lock = SPIN_LOCK_UNLOCKED,
577 .wait = __WAIT_QUEUE_HEAD_INITIALIZER(s3c24xx_i2c.wait),
578 .adap = {
579 .name = "s3c2410-i2c",
580 .owner = THIS_MODULE,
581 .algo = &s3c24xx_i2c_algorithm,
582 .retries = 2,
583 .class = I2C_CLASS_HWMON,
584 },
585};
586
587/* s3c24xx_i2c_calcdivisor
588 *
589 * return the divisor settings for a given frequency
590*/
591
592static int s3c24xx_i2c_calcdivisor(unsigned long clkin, unsigned int wanted,
593 unsigned int *div1, unsigned int *divs)
594{
595 unsigned int calc_divs = clkin / wanted;
596 unsigned int calc_div1;
597
598 if (calc_divs > (16*16))
599 calc_div1 = 512;
600 else
601 calc_div1 = 16;
602
603 calc_divs += calc_div1-1;
604 calc_divs /= calc_div1;
605
606 if (calc_divs == 0)
607 calc_divs = 1;
608 if (calc_divs > 17)
609 calc_divs = 17;
610
611 *divs = calc_divs;
612 *div1 = calc_div1;
613
614 return clkin / (calc_divs * calc_div1);
615}
616
617/* freq_acceptable
618 *
619 * test wether a frequency is within the acceptable range of error
620*/
621
622static inline int freq_acceptable(unsigned int freq, unsigned int wanted)
623{
624 int diff = freq - wanted;
625
626 return (diff >= -2 && diff <= 2);
627}
628
629/* s3c24xx_i2c_getdivisor
630 *
631 * work out a divisor for the user requested frequency setting,
632 * either by the requested frequency, or scanning the acceptable
633 * range of frequencies until something is found
634*/
635
636static int s3c24xx_i2c_getdivisor(struct s3c24xx_i2c *i2c,
637 struct s3c2410_platform_i2c *pdata,
638 unsigned long *iicon,
639 unsigned int *got)
640{
641 unsigned long clkin = clk_get_rate(i2c->clk);
642
643 unsigned int divs, div1;
644 int freq;
645 int start, end;
646
647 clkin /= 1000; /* clkin now in KHz */
648
649 dev_dbg(i2c->dev, "pdata %p, freq %lu %lu..%lu\n",
650 pdata, pdata->bus_freq, pdata->min_freq, pdata->max_freq);
651
652 if (pdata->bus_freq != 0) {
653 freq = s3c24xx_i2c_calcdivisor(clkin, pdata->bus_freq/1000,
654 &div1, &divs);
655 if (freq_acceptable(freq, pdata->bus_freq/1000))
656 goto found;
657 }
658
659 /* ok, we may have to search for something suitable... */
660
661 start = (pdata->max_freq == 0) ? pdata->bus_freq : pdata->max_freq;
662 end = pdata->min_freq;
663
664 start /= 1000;
665 end /= 1000;
666
667 /* search loop... */
668
669 for (; start > end; start--) {
670 freq = s3c24xx_i2c_calcdivisor(clkin, start, &div1, &divs);
671 if (freq_acceptable(freq, start))
672 goto found;
673 }
674
675 /* cannot find frequency spec */
676
677 return -EINVAL;
678
679 found:
680 *got = freq;
681 *iicon |= (divs-1);
682 *iicon |= (div1 == 512) ? S3C2410_IICCON_TXDIV_512 : 0;
683 return 0;
684}
685
686/* s3c24xx_i2c_init
687 *
688 * initialise the controller, set the IO lines and frequency
689*/
690
691static int s3c24xx_i2c_init(struct s3c24xx_i2c *i2c)
692{
693 unsigned long iicon = S3C2410_IICCON_IRQEN | S3C2410_IICCON_ACKEN;
694 struct s3c2410_platform_i2c *pdata;
695 unsigned int freq;
696
697 /* get the plafrom data */
698
699 pdata = s3c24xx_i2c_get_platformdata(i2c->adap.dev.parent);
700
701 /* inititalise the gpio */
702
703 s3c2410_gpio_cfgpin(S3C2410_GPE15, S3C2410_GPE15_IICSDA);
704 s3c2410_gpio_cfgpin(S3C2410_GPE14, S3C2410_GPE14_IICSCL);
705
706 /* write slave address */
707
708 writeb(pdata->slave_addr, i2c->regs + S3C2410_IICADD);
709
710 dev_info(i2c->dev, "slave address 0x%02x\n", pdata->slave_addr);
711
712 /* we need to work out the divisors for the clock... */
713
714 if (s3c24xx_i2c_getdivisor(i2c, pdata, &iicon, &freq) != 0) {
715 dev_err(i2c->dev, "cannot meet bus frequency required\n");
716 return -EINVAL;
717 }
718
719 /* todo - check that the i2c lines aren't being dragged anywhere */
720
721 dev_info(i2c->dev, "bus frequency set to %d KHz\n", freq);
722 dev_dbg(i2c->dev, "S3C2410_IICCON=0x%02lx\n", iicon);
723
724 writel(iicon, i2c->regs + S3C2410_IICCON);
725
726 /* check for s3c2440 i2c controller */
727
728 if (s3c24xx_i2c_is2440(i2c)) {
729 dev_dbg(i2c->dev, "S3C2440_IICLC=%08x\n", pdata->sda_delay);
730
731 writel(pdata->sda_delay, i2c->regs + S3C2440_IICLC);
732 }
733
734 return 0;
735}
736
737static void s3c24xx_i2c_free(struct s3c24xx_i2c *i2c)
738{
739 if (i2c->clk != NULL && !IS_ERR(i2c->clk)) {
740 clk_disable(i2c->clk);
741 clk_unuse(i2c->clk);
742 clk_put(i2c->clk);
743 i2c->clk = NULL;
744 }
745
746 if (i2c->regs != NULL) {
747 iounmap(i2c->regs);
748 i2c->regs = NULL;
749 }
750
751 if (i2c->ioarea != NULL) {
752 release_resource(i2c->ioarea);
753 kfree(i2c->ioarea);
754 i2c->ioarea = NULL;
755 }
756}
757
758/* s3c24xx_i2c_probe
759 *
760 * called by the bus driver when a suitable device is found
761*/
762
763static int s3c24xx_i2c_probe(struct device *dev)
764{
765 struct platform_device *pdev = to_platform_device(dev);
766 struct s3c24xx_i2c *i2c = &s3c24xx_i2c;
767 struct resource *res;
768 int ret;
769
770 /* find the clock and enable it */
771
772 i2c->dev = dev;
773 i2c->clk = clk_get(dev, "i2c");
774 if (IS_ERR(i2c->clk)) {
775 dev_err(dev, "cannot get clock\n");
776 ret = -ENOENT;
777 goto out;
778 }
779
780 dev_dbg(dev, "clock source %p\n", i2c->clk);
781
782 clk_use(i2c->clk);
783 clk_enable(i2c->clk);
784
785 /* map the registers */
786
787 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
788 if (res == NULL) {
789 dev_err(dev, "cannot find IO resource\n");
790 ret = -ENOENT;
791 goto out;
792 }
793
794 i2c->ioarea = request_mem_region(res->start, (res->end-res->start)+1,
795 pdev->name);
796
797 if (i2c->ioarea == NULL) {
798 dev_err(dev, "cannot request IO\n");
799 ret = -ENXIO;
800 goto out;
801 }
802
803 i2c->regs = ioremap(res->start, (res->end-res->start)+1);
804
805 if (i2c->regs == NULL) {
806 dev_err(dev, "cannot map IO\n");
807 ret = -ENXIO;
808 goto out;
809 }
810
811 dev_dbg(dev, "registers %p (%p, %p)\n", i2c->regs, i2c->ioarea, res);
812
813 /* setup info block for the i2c core */
814
815 i2c->adap.algo_data = i2c;
816 i2c->adap.dev.parent = dev;
817
818 /* initialise the i2c controller */
819
820 ret = s3c24xx_i2c_init(i2c);
821 if (ret != 0)
822 goto out;
823
824 /* find the IRQ for this unit (note, this relies on the init call to
825 * ensure no current IRQs pending
826 */
827
828 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
829 if (res == NULL) {
830 dev_err(dev, "cannot find IRQ\n");
831 ret = -ENOENT;
832 goto out;
833 }
834
835 ret = request_irq(res->start, s3c24xx_i2c_irq, SA_INTERRUPT,
836 pdev->name, i2c);
837
838 if (ret != 0) {
839 dev_err(dev, "cannot claim IRQ\n");
840 goto out;
841 }
842
843 i2c->irq = res;
844
845 dev_dbg(dev, "irq resource %p (%ld)\n", res, res->start);
846
847 ret = i2c_add_adapter(&i2c->adap);
848 if (ret < 0) {
849 dev_err(dev, "failed to add bus to i2c core\n");
850 goto out;
851 }
852
853 dev_set_drvdata(dev, i2c);
854
855 dev_info(dev, "%s: S3C I2C adapter\n", i2c->adap.dev.bus_id);
856
857 out:
858 if (ret < 0)
859 s3c24xx_i2c_free(i2c);
860
861 return ret;
862}
863
864/* s3c24xx_i2c_remove
865 *
866 * called when device is removed from the bus
867*/
868
869static int s3c24xx_i2c_remove(struct device *dev)
870{
871 struct s3c24xx_i2c *i2c = dev_get_drvdata(dev);
872
873 if (i2c != NULL) {
874 s3c24xx_i2c_free(i2c);
875 dev_set_drvdata(dev, NULL);
876 }
877
878 return 0;
879}
880
881#ifdef CONFIG_PM
882static int s3c24xx_i2c_resume(struct device *dev, u32 level)
883{
884 struct s3c24xx_i2c *i2c = dev_get_drvdata(dev);
885
886 if (i2c != NULL && level == RESUME_ENABLE) {
887 dev_dbg(dev, "resume: level %d\n", level);
888 s3c24xx_i2c_init(i2c);
889 }
890
891 return 0;
892}
893
894#else
895#define s3c24xx_i2c_resume NULL
896#endif
897
898/* device driver for platform bus bits */
899
900static struct device_driver s3c2410_i2c_driver = {
901 .name = "s3c2410-i2c",
902 .bus = &platform_bus_type,
903 .probe = s3c24xx_i2c_probe,
904 .remove = s3c24xx_i2c_remove,
905 .resume = s3c24xx_i2c_resume,
906};
907
908static struct device_driver s3c2440_i2c_driver = {
909 .name = "s3c2440-i2c",
910 .bus = &platform_bus_type,
911 .probe = s3c24xx_i2c_probe,
912 .remove = s3c24xx_i2c_remove,
913 .resume = s3c24xx_i2c_resume,
914};
915
916static int __init i2c_adap_s3c_init(void)
917{
918 int ret;
919
920 ret = driver_register(&s3c2410_i2c_driver);
921 if (ret == 0)
922 ret = driver_register(&s3c2440_i2c_driver);
923
924 return ret;
925}
926
927static void __exit i2c_adap_s3c_exit(void)
928{
929 driver_unregister(&s3c2410_i2c_driver);
930 driver_unregister(&s3c2440_i2c_driver);
931}
932
933module_init(i2c_adap_s3c_init);
934module_exit(i2c_adap_s3c_exit);
935
936MODULE_DESCRIPTION("S3C24XX I2C Bus driver");
937MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
938MODULE_LICENSE("GPL");
diff --git a/drivers/i2c/busses/i2c-savage4.c b/drivers/i2c/busses/i2c-savage4.c
new file mode 100644
index 000000000000..092d0323c6c6
--- /dev/null
+++ b/drivers/i2c/busses/i2c-savage4.c
@@ -0,0 +1,205 @@
1/*
2 i2c-savage4.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring
4 Copyright (C) 1998-2003 The LM Sensors Team
5 Alexander Wold <awold@bigfoot.com>
6 Mark D. Studebaker <mdsxyz123@yahoo.com>
7
8 Based on i2c-voodoo3.c.
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23*/
24
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
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*/
31
32#include <linux/config.h>
33#include <linux/kernel.h>
34#include <linux/module.h>
35#include <linux/init.h>
36#include <linux/pci.h>
37#include <linux/i2c.h>
38#include <linux/i2c-algo-bit.h>
39#include <asm/io.h>
40
41/* 3DFX defines */
42#define PCI_CHIP_SAVAGE3D 0x8A20
43#define PCI_CHIP_SAVAGE3D_MV 0x8A21
44#define PCI_CHIP_SAVAGE4 0x8A22
45#define PCI_CHIP_SAVAGE2000 0x9102
46#define PCI_CHIP_PROSAVAGE_PM 0x8A25
47#define PCI_CHIP_PROSAVAGE_KM 0x8A26
48#define PCI_CHIP_SAVAGE_MX_MV 0x8c10
49#define PCI_CHIP_SAVAGE_MX 0x8c11
50#define PCI_CHIP_SAVAGE_IX_MV 0x8c12
51#define PCI_CHIP_SAVAGE_IX 0x8c13
52
53#define REG 0xff20 /* Serial Port 1 Register */
54
55/* bit locations in the register */
56#define DDC_ENAB 0x00040000
57#define DDC_SCL_OUT 0x00080000
58#define DDC_SDA_OUT 0x00100000
59#define DDC_SCL_IN 0x00200000
60#define DDC_SDA_IN 0x00400000
61#define I2C_ENAB 0x00000020
62#define I2C_SCL_OUT 0x00000001
63#define I2C_SDA_OUT 0x00000002
64#define I2C_SCL_IN 0x00000008
65#define I2C_SDA_IN 0x00000010
66
67/* initialization states */
68#define INIT2 0x20
69#define INIT3 0x04
70
71/* delays */
72#define CYCLE_DELAY 10
73#define TIMEOUT (HZ / 2)
74
75
76static void __iomem *ioaddr;
77
78/* The sav GPIO registers don't have individual masks for each bit
79 so we always have to read before writing. */
80
81static void bit_savi2c_setscl(void *data, int val)
82{
83 unsigned int r;
84 r = readl(ioaddr + REG);
85 if(val)
86 r |= I2C_SCL_OUT;
87 else
88 r &= ~I2C_SCL_OUT;
89 writel(r, ioaddr + REG);
90 readl(ioaddr + REG); /* flush posted write */
91}
92
93static void bit_savi2c_setsda(void *data, int val)
94{
95 unsigned int r;
96 r = readl(ioaddr + REG);
97 if(val)
98 r |= I2C_SDA_OUT;
99 else
100 r &= ~I2C_SDA_OUT;
101 writel(r, ioaddr + REG);
102 readl(ioaddr + REG); /* flush posted write */
103}
104
105/* The GPIO pins are open drain, so the pins always remain outputs.
106 We rely on the i2c-algo-bit routines to set the pins high before
107 reading the input from other chips. */
108
109static int bit_savi2c_getscl(void *data)
110{
111 return (0 != (readl(ioaddr + REG) & I2C_SCL_IN));
112}
113
114static int bit_savi2c_getsda(void *data)
115{
116 return (0 != (readl(ioaddr + REG) & I2C_SDA_IN));
117}
118
119/* Configures the chip */
120
121static int config_s4(struct pci_dev *dev)
122{
123 unsigned long cadr;
124
125 /* map memory */
126 cadr = dev->resource[0].start;
127 cadr &= PCI_BASE_ADDRESS_MEM_MASK;
128 ioaddr = ioremap_nocache(cadr, 0x0080000);
129 if (ioaddr) {
130 /* writel(0x8160, ioaddr + REG2); */
131 writel(0x00000020, ioaddr + REG);
132 dev_info(&dev->dev, "Using Savage4 at %p\n", ioaddr);
133 return 0;
134 }
135 return -ENODEV;
136}
137
138static struct i2c_algo_bit_data sav_i2c_bit_data = {
139 .setsda = bit_savi2c_setsda,
140 .setscl = bit_savi2c_setscl,
141 .getsda = bit_savi2c_getsda,
142 .getscl = bit_savi2c_getscl,
143 .udelay = CYCLE_DELAY,
144 .mdelay = CYCLE_DELAY,
145 .timeout = TIMEOUT
146};
147
148static struct i2c_adapter savage4_i2c_adapter = {
149 .owner = THIS_MODULE,
150 .name = "I2C Savage4 adapter",
151 .algo_data = &sav_i2c_bit_data,
152};
153
154static struct pci_device_id savage4_ids[] __devinitdata = {
155 { PCI_DEVICE(PCI_VENDOR_ID_S3, PCI_CHIP_SAVAGE4) },
156 { PCI_DEVICE(PCI_VENDOR_ID_S3, PCI_CHIP_SAVAGE2000) },
157 { 0, }
158};
159
160MODULE_DEVICE_TABLE (pci, savage4_ids);
161
162static int __devinit savage4_probe(struct pci_dev *dev, const struct pci_device_id *id)
163{
164 int retval;
165
166 retval = config_s4(dev);
167 if (retval)
168 return retval;
169
170 /* set up the sysfs linkage to our parent device */
171 savage4_i2c_adapter.dev.parent = &dev->dev;
172
173 return i2c_bit_add_bus(&savage4_i2c_adapter);
174}
175
176static void __devexit savage4_remove(struct pci_dev *dev)
177{
178 i2c_bit_del_bus(&savage4_i2c_adapter);
179 iounmap(ioaddr);
180}
181
182static struct pci_driver savage4_driver = {
183 .name = "savage4_smbus",
184 .id_table = savage4_ids,
185 .probe = savage4_probe,
186 .remove = __devexit_p(savage4_remove),
187};
188
189static int __init i2c_savage4_init(void)
190{
191 return pci_register_driver(&savage4_driver);
192}
193
194static void __exit i2c_savage4_exit(void)
195{
196 pci_unregister_driver(&savage4_driver);
197}
198
199MODULE_AUTHOR("Alexander Wold <awold@bigfoot.com> "
200 "and Mark D. Studebaker <mdsxyz123@yahoo.com>");
201MODULE_DESCRIPTION("Savage4 I2C/SMBus driver");
202MODULE_LICENSE("GPL");
203
204module_init(i2c_savage4_init);
205module_exit(i2c_savage4_exit);
diff --git a/drivers/i2c/busses/i2c-sibyte.c b/drivers/i2c/busses/i2c-sibyte.c
new file mode 100644
index 000000000000..e5dd90bdb04a
--- /dev/null
+++ b/drivers/i2c/busses/i2c-sibyte.c
@@ -0,0 +1,71 @@
1/*
2 * Copyright (C) 2004 Steven J. Hill
3 * Copyright (C) 2001,2002,2003 Broadcom Corporation
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
20#include <linux/config.h>
21#include <linux/module.h>
22#include <linux/i2c-algo-sibyte.h>
23#include <asm/sibyte/sb1250_regs.h>
24#include <asm/sibyte/sb1250_smbus.h>
25
26static struct i2c_algo_sibyte_data sibyte_board_data[2] = {
27 { NULL, 0, (void *) (KSEG1+A_SMB_BASE(0)) },
28 { NULL, 1, (void *) (KSEG1+A_SMB_BASE(1)) }
29};
30
31static struct i2c_adapter sibyte_board_adapter[2] = {
32 {
33 .owner = THIS_MODULE,
34 .id = I2C_HW_SIBYTE,
35 .class = I2C_CLASS_HWMON,
36 .algo = NULL,
37 .algo_data = &sibyte_board_data[0],
38 .name = "SiByte SMBus 0",
39 },
40 {
41 .owner = THIS_MODULE,
42 .id = I2C_HW_SIBYTE,
43 .class = I2C_CLASS_HWMON,
44 .algo = NULL,
45 .algo_data = &sibyte_board_data[1],
46 .name = "SiByte SMBus 1",
47 },
48};
49
50static int __init i2c_sibyte_init(void)
51{
52 printk("i2c-swarm.o: i2c SMBus adapter module for SiByte board\n");
53 if (i2c_sibyte_add_bus(&sibyte_board_adapter[0], K_SMB_FREQ_100KHZ) < 0)
54 return -ENODEV;
55 if (i2c_sibyte_add_bus(&sibyte_board_adapter[1], K_SMB_FREQ_400KHZ) < 0)
56 return -ENODEV;
57 return 0;
58}
59
60static void __exit i2c_sibyte_exit(void)
61{
62 i2c_sibyte_del_bus(&sibyte_board_adapter[0]);
63 i2c_sibyte_del_bus(&sibyte_board_adapter[1]);
64}
65
66module_init(i2c_sibyte_init);
67module_exit(i2c_sibyte_exit);
68
69MODULE_AUTHOR("Kip Walker <kwalker@broadcom.com>, Steven J. Hill <sjhill@realitydiluted.com>");
70MODULE_DESCRIPTION("SMBus adapter routines for SiByte boards");
71MODULE_LICENSE("GPL");
diff --git a/drivers/i2c/busses/i2c-sis5595.c b/drivers/i2c/busses/i2c-sis5595.c
new file mode 100644
index 000000000000..425733b019b6
--- /dev/null
+++ b/drivers/i2c/busses/i2c-sis5595.c
@@ -0,0 +1,424 @@
1/*
2 sis5595.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring
4 Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl> and
5 Philip Edelbrock <phil@netroedge.com>
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/* Note: we assume there can only be one SIS5595 with one SMBus interface */
23
24/*
25 Note: all have mfr. ID 0x1039.
26 SUPPORTED PCI ID
27 5595 0008
28
29 Note: these chips contain a 0008 device which is incompatible with the
30 5595. We recognize these by the presence of the listed
31 "blacklist" PCI ID and refuse to load.
32
33 NOT SUPPORTED PCI ID BLACKLIST PCI ID
34 540 0008 0540
35 550 0008 0550
36 5513 0008 5511
37 5581 0008 5597
38 5582 0008 5597
39 5597 0008 5597
40 5598 0008 5597/5598
41 630 0008 0630
42 645 0008 0645
43 646 0008 0646
44 648 0008 0648
45 650 0008 0650
46 651 0008 0651
47 730 0008 0730
48 735 0008 0735
49 745 0008 0745
50 746 0008 0746
51*/
52
53/* TO DO:
54 * Add Block Transfers (ugly, but supported by the adapter)
55 * Add adapter resets
56 */
57
58#include <linux/config.h>
59#include <linux/kernel.h>
60#include <linux/module.h>
61#include <linux/delay.h>
62#include <linux/pci.h>
63#include <linux/ioport.h>
64#include <linux/init.h>
65#include <linux/i2c.h>
66#include <asm/io.h>
67
68static int blacklist[] = {
69 PCI_DEVICE_ID_SI_540,
70 PCI_DEVICE_ID_SI_550,
71 PCI_DEVICE_ID_SI_630,
72 PCI_DEVICE_ID_SI_645,
73 PCI_DEVICE_ID_SI_646,
74 PCI_DEVICE_ID_SI_648,
75 PCI_DEVICE_ID_SI_650,
76 PCI_DEVICE_ID_SI_651,
77 PCI_DEVICE_ID_SI_730,
78 PCI_DEVICE_ID_SI_735,
79 PCI_DEVICE_ID_SI_745,
80 PCI_DEVICE_ID_SI_746,
81 PCI_DEVICE_ID_SI_5511, /* 5513 chip has the 0008 device but that ID
82 shows up in other chips so we use the 5511
83 ID for recognition */
84 PCI_DEVICE_ID_SI_5597,
85 PCI_DEVICE_ID_SI_5598,
86 0, /* terminates the list */
87};
88
89/* Length of ISA address segment */
90#define SIS5595_EXTENT 8
91/* SIS5595 SMBus registers */
92#define SMB_STS_LO 0x00
93#define SMB_STS_HI 0x01
94#define SMB_CTL_LO 0x02
95#define SMB_CTL_HI 0x03
96#define SMB_ADDR 0x04
97#define SMB_CMD 0x05
98#define SMB_PCNT 0x06
99#define SMB_CNT 0x07
100#define SMB_BYTE 0x08
101#define SMB_DEV 0x10
102#define SMB_DB0 0x11
103#define SMB_DB1 0x12
104#define SMB_HAA 0x13
105
106/* PCI Address Constants */
107#define SMB_INDEX 0x38
108#define SMB_DAT 0x39
109#define SIS5595_ENABLE_REG 0x40
110#define ACPI_BASE 0x90
111
112/* Other settings */
113#define MAX_TIMEOUT 500
114
115/* SIS5595 constants */
116#define SIS5595_QUICK 0x00
117#define SIS5595_BYTE 0x02
118#define SIS5595_BYTE_DATA 0x04
119#define SIS5595_WORD_DATA 0x06
120#define SIS5595_PROC_CALL 0x08
121#define SIS5595_BLOCK_DATA 0x0A
122
123/* insmod parameters */
124
125/* If force_addr is set to anything different from 0, we forcibly enable
126 the device at the given address. */
127static u16 force_addr = 0;
128module_param(force_addr, ushort, 0);
129MODULE_PARM_DESC(force_addr, "Initialize the base address of the i2c controller");
130
131static unsigned short sis5595_base = 0;
132
133static u8 sis5595_read(u8 reg)
134{
135 outb(reg, sis5595_base + SMB_INDEX);
136 return inb(sis5595_base + SMB_DAT);
137}
138
139static void sis5595_write(u8 reg, u8 data)
140{
141 outb(reg, sis5595_base + SMB_INDEX);
142 outb(data, sis5595_base + SMB_DAT);
143}
144
145static int sis5595_setup(struct pci_dev *SIS5595_dev)
146{
147 u16 a;
148 u8 val;
149 int *i;
150 int retval = -ENODEV;
151
152 /* Look for imposters */
153 for (i = blacklist; *i != 0; i++) {
154 struct pci_dev *dev;
155 dev = pci_get_device(PCI_VENDOR_ID_SI, *i, NULL);
156 if (dev) {
157 dev_err(&SIS5595_dev->dev, "Looked for SIS5595 but found unsupported device %.4x\n", *i);
158 pci_dev_put(dev);
159 return -ENODEV;
160 }
161 }
162
163 /* Determine the address of the SMBus areas */
164 pci_read_config_word(SIS5595_dev, ACPI_BASE, &sis5595_base);
165 if (sis5595_base == 0 && force_addr == 0) {
166 dev_err(&SIS5595_dev->dev, "ACPI base address uninitialized - upgrade BIOS or use force_addr=0xaddr\n");
167 return -ENODEV;
168 }
169
170 if (force_addr)
171 sis5595_base = force_addr & ~(SIS5595_EXTENT - 1);
172 dev_dbg(&SIS5595_dev->dev, "ACPI Base address: %04x\n", sis5595_base);
173
174 /* NB: We grab just the two SMBus registers here, but this may still
175 * interfere with ACPI :-( */
176 if (!request_region(sis5595_base + SMB_INDEX, 2, "sis5595-smbus")) {
177 dev_err(&SIS5595_dev->dev, "SMBus registers 0x%04x-0x%04x already in use!\n",
178 sis5595_base + SMB_INDEX, sis5595_base + SMB_INDEX + 1);
179 return -ENODEV;
180 }
181
182 if (force_addr) {
183 dev_info(&SIS5595_dev->dev, "forcing ISA address 0x%04X\n", sis5595_base);
184 if (pci_write_config_word(SIS5595_dev, ACPI_BASE, sis5595_base)
185 != PCIBIOS_SUCCESSFUL)
186 goto error;
187 if (pci_read_config_word(SIS5595_dev, ACPI_BASE, &a)
188 != PCIBIOS_SUCCESSFUL)
189 goto error;
190 if ((a & ~(SIS5595_EXTENT - 1)) != sis5595_base) {
191 /* doesn't work for some chips! */
192 dev_err(&SIS5595_dev->dev, "force address failed - not supported?\n");
193 goto error;
194 }
195 }
196
197 if (pci_read_config_byte(SIS5595_dev, SIS5595_ENABLE_REG, &val)
198 != PCIBIOS_SUCCESSFUL)
199 goto error;
200 if ((val & 0x80) == 0) {
201 dev_info(&SIS5595_dev->dev, "enabling ACPI\n");
202 if (pci_write_config_byte(SIS5595_dev, SIS5595_ENABLE_REG, val | 0x80)
203 != PCIBIOS_SUCCESSFUL)
204 goto error;
205 if (pci_read_config_byte(SIS5595_dev, SIS5595_ENABLE_REG, &val)
206 != PCIBIOS_SUCCESSFUL)
207 goto error;
208 if ((val & 0x80) == 0) {
209 /* doesn't work for some chips? */
210 dev_err(&SIS5595_dev->dev, "ACPI enable failed - not supported?\n");
211 goto error;
212 }
213 }
214
215 /* Everything is happy */
216 return 0;
217
218error:
219 release_region(sis5595_base + SMB_INDEX, 2);
220 return retval;
221}
222
223static int sis5595_transaction(struct i2c_adapter *adap)
224{
225 int temp;
226 int result = 0;
227 int timeout = 0;
228
229 /* Make sure the SMBus host is ready to start transmitting */
230 temp = sis5595_read(SMB_STS_LO) + (sis5595_read(SMB_STS_HI) << 8);
231 if (temp != 0x00) {
232 dev_dbg(&adap->dev, "SMBus busy (%04x). Resetting... \n", temp);
233 sis5595_write(SMB_STS_LO, temp & 0xff);
234 sis5595_write(SMB_STS_HI, temp >> 8);
235 if ((temp = sis5595_read(SMB_STS_LO) + (sis5595_read(SMB_STS_HI) << 8)) != 0x00) {
236 dev_dbg(&adap->dev, "Failed! (%02x)\n", temp);
237 return -1;
238 } else {
239 dev_dbg(&adap->dev, "Successfull!\n");
240 }
241 }
242
243 /* start the transaction by setting bit 4 */
244 sis5595_write(SMB_CTL_LO, sis5595_read(SMB_CTL_LO) | 0x10);
245
246 /* We will always wait for a fraction of a second! */
247 do {
248 msleep(1);
249 temp = sis5595_read(SMB_STS_LO);
250 } while (!(temp & 0x40) && (timeout++ < MAX_TIMEOUT));
251
252 /* If the SMBus is still busy, we give up */
253 if (timeout >= MAX_TIMEOUT) {
254 dev_dbg(&adap->dev, "SMBus Timeout!\n");
255 result = -1;
256 }
257
258 if (temp & 0x10) {
259 dev_dbg(&adap->dev, "Error: Failed bus transaction\n");
260 result = -1;
261 }
262
263 if (temp & 0x20) {
264 dev_err(&adap->dev, "Bus collision! SMBus may be locked until "
265 "next hard reset (or not...)\n");
266 /* Clock stops and slave is stuck in mid-transmission */
267 result = -1;
268 }
269
270 temp = sis5595_read(SMB_STS_LO) + (sis5595_read(SMB_STS_HI) << 8);
271 if (temp != 0x00) {
272 sis5595_write(SMB_STS_LO, temp & 0xff);
273 sis5595_write(SMB_STS_HI, temp >> 8);
274 }
275
276 temp = sis5595_read(SMB_STS_LO) + (sis5595_read(SMB_STS_HI) << 8);
277 if (temp != 0x00)
278 dev_dbg(&adap->dev, "Failed reset at end of transaction (%02x)\n", temp);
279
280 return result;
281}
282
283/* Return -1 on error. */
284static s32 sis5595_access(struct i2c_adapter *adap, u16 addr,
285 unsigned short flags, char read_write,
286 u8 command, int size, union i2c_smbus_data *data)
287{
288 switch (size) {
289 case I2C_SMBUS_QUICK:
290 sis5595_write(SMB_ADDR, ((addr & 0x7f) << 1) | (read_write & 0x01));
291 size = SIS5595_QUICK;
292 break;
293 case I2C_SMBUS_BYTE:
294 sis5595_write(SMB_ADDR, ((addr & 0x7f) << 1) | (read_write & 0x01));
295 if (read_write == I2C_SMBUS_WRITE)
296 sis5595_write(SMB_CMD, command);
297 size = SIS5595_BYTE;
298 break;
299 case I2C_SMBUS_BYTE_DATA:
300 sis5595_write(SMB_ADDR, ((addr & 0x7f) << 1) | (read_write & 0x01));
301 sis5595_write(SMB_CMD, command);
302 if (read_write == I2C_SMBUS_WRITE)
303 sis5595_write(SMB_BYTE, data->byte);
304 size = SIS5595_BYTE_DATA;
305 break;
306 case I2C_SMBUS_PROC_CALL:
307 case I2C_SMBUS_WORD_DATA:
308 sis5595_write(SMB_ADDR, ((addr & 0x7f) << 1) | (read_write & 0x01));
309 sis5595_write(SMB_CMD, command);
310 if (read_write == I2C_SMBUS_WRITE) {
311 sis5595_write(SMB_BYTE, data->word & 0xff);
312 sis5595_write(SMB_BYTE + 1,
313 (data->word & 0xff00) >> 8);
314 }
315 size = (size == I2C_SMBUS_PROC_CALL) ? SIS5595_PROC_CALL : SIS5595_WORD_DATA;
316 break;
317/*
318 case I2C_SMBUS_BLOCK_DATA:
319 printk(KERN_WARNING "sis5595.o: Block data not yet implemented!\n");
320 return -1;
321 break;
322*/
323 default:
324 printk(KERN_WARNING "sis5595.o: Unsupported transaction %d\n", size);
325 return -1;
326 }
327
328 sis5595_write(SMB_CTL_LO, ((size & 0x0E)));
329
330 if (sis5595_transaction(adap))
331 return -1;
332
333 if ((size != SIS5595_PROC_CALL) &&
334 ((read_write == I2C_SMBUS_WRITE) || (size == SIS5595_QUICK)))
335 return 0;
336
337
338 switch (size) {
339 case SIS5595_BYTE: /* Where is the result put? I assume here it is in
340 SMB_DATA but it might just as well be in the
341 SMB_CMD. No clue in the docs */
342 case SIS5595_BYTE_DATA:
343 data->byte = sis5595_read(SMB_BYTE);
344 break;
345 case SIS5595_WORD_DATA:
346 case SIS5595_PROC_CALL:
347 data->word = sis5595_read(SMB_BYTE) + (sis5595_read(SMB_BYTE + 1) << 8);
348 break;
349 }
350 return 0;
351}
352
353static u32 sis5595_func(struct i2c_adapter *adapter)
354{
355 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
356 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
357 I2C_FUNC_SMBUS_PROC_CALL;
358}
359
360static struct i2c_algorithm smbus_algorithm = {
361 .name = "Non-I2C SMBus adapter",
362 .id = I2C_ALGO_SMBUS,
363 .smbus_xfer = sis5595_access,
364 .functionality = sis5595_func,
365};
366
367static struct i2c_adapter sis5595_adapter = {
368 .owner = THIS_MODULE,
369 .class = I2C_CLASS_HWMON,
370 .name = "unset",
371 .algo = &smbus_algorithm,
372};
373
374static struct pci_device_id sis5595_ids[] __devinitdata = {
375 { PCI_DEVICE(PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_503) },
376 { 0, }
377};
378
379MODULE_DEVICE_TABLE (pci, sis5595_ids);
380
381static int __devinit sis5595_probe(struct pci_dev *dev, const struct pci_device_id *id)
382{
383 if (sis5595_setup(dev)) {
384 dev_err(&dev->dev, "SIS5595 not detected, module not inserted.\n");
385 return -ENODEV;
386 }
387
388 /* set up the driverfs linkage to our parent device */
389 sis5595_adapter.dev.parent = &dev->dev;
390
391 sprintf(sis5595_adapter.name, "SMBus SIS5595 adapter at %04x",
392 sis5595_base + SMB_INDEX);
393 return i2c_add_adapter(&sis5595_adapter);
394}
395
396static void __devexit sis5595_remove(struct pci_dev *dev)
397{
398 i2c_del_adapter(&sis5595_adapter);
399 release_region(sis5595_base + SMB_INDEX, 2);
400}
401
402static struct pci_driver sis5595_driver = {
403 .name = "sis5595_smbus",
404 .id_table = sis5595_ids,
405 .probe = sis5595_probe,
406 .remove = __devexit_p(sis5595_remove),
407};
408
409static int __init i2c_sis5595_init(void)
410{
411 return pci_register_driver(&sis5595_driver);
412}
413
414static void __exit i2c_sis5595_exit(void)
415{
416 pci_unregister_driver(&sis5595_driver);
417}
418
419MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>");
420MODULE_DESCRIPTION("SIS5595 SMBus driver");
421MODULE_LICENSE("GPL");
422
423module_init(i2c_sis5595_init);
424module_exit(i2c_sis5595_exit);
diff --git a/drivers/i2c/busses/i2c-sis630.c b/drivers/i2c/busses/i2c-sis630.c
new file mode 100644
index 000000000000..58df63df1540
--- /dev/null
+++ b/drivers/i2c/busses/i2c-sis630.c
@@ -0,0 +1,523 @@
1/*
2 i2c-sis630.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring
4
5 Copyright (c) 2002,2003 Alexander Malysh <amalysh@web.de>
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/*
23 Changes:
24 24.08.2002
25 Fixed the typo in sis630_access (Thanks to Mark M. Hoffman)
26 Changed sis630_transaction.(Thanks to Mark M. Hoffman)
27 18.09.2002
28 Added SIS730 as supported.
29 21.09.2002
30 Added high_clock module option.If this option is set
31 used Host Master Clock 56KHz (default 14KHz).For now we save old Host
32 Master Clock and after transaction completed restore (otherwise
33 it's confuse BIOS and hung Machine).
34 24.09.2002
35 Fixed typo in sis630_access
36 Fixed logical error by restoring of Host Master Clock
37 31.07.2003
38 Added block data read/write support.
39*/
40
41/*
42 Status: beta
43
44 Supports:
45 SIS 630
46 SIS 730
47
48 Note: we assume there can only be one device, with one SMBus interface.
49*/
50
51#include <linux/config.h>
52#include <linux/kernel.h>
53#include <linux/module.h>
54#include <linux/delay.h>
55#include <linux/pci.h>
56#include <linux/ioport.h>
57#include <linux/init.h>
58#include <linux/i2c.h>
59#include <asm/io.h>
60
61/* SIS630 SMBus registers */
62#define SMB_STS 0x80 /* status */
63#define SMB_EN 0x81 /* status enable */
64#define SMB_CNT 0x82
65#define SMBHOST_CNT 0x83
66#define SMB_ADDR 0x84
67#define SMB_CMD 0x85
68#define SMB_PCOUNT 0x86 /* processed count */
69#define SMB_COUNT 0x87
70#define SMB_BYTE 0x88 /* ~0x8F data byte field */
71#define SMBDEV_ADDR 0x90
72#define SMB_DB0 0x91
73#define SMB_DB1 0x92
74#define SMB_SAA 0x93
75
76/* register count for request_region */
77#define SIS630_SMB_IOREGION 20
78
79/* PCI address constants */
80/* acpi base address register */
81#define SIS630_ACPI_BASE_REG 0x74
82/* bios control register */
83#define SIS630_BIOS_CTL_REG 0x40
84
85/* Other settings */
86#define MAX_TIMEOUT 500
87
88/* SIS630 constants */
89#define SIS630_QUICK 0x00
90#define SIS630_BYTE 0x01
91#define SIS630_BYTE_DATA 0x02
92#define SIS630_WORD_DATA 0x03
93#define SIS630_PCALL 0x04
94#define SIS630_BLOCK_DATA 0x05
95
96/* insmod parameters */
97static int high_clock;
98static int force;
99module_param(high_clock, bool, 0);
100MODULE_PARM_DESC(high_clock, "Set Host Master Clock to 56KHz (default 14KHz).");
101module_param(force, bool, 0);
102MODULE_PARM_DESC(force, "Forcibly enable the SIS630. DANGEROUS!");
103
104/* acpi base address */
105static unsigned short acpi_base = 0;
106
107/* supported chips */
108static int supported[] = {
109 PCI_DEVICE_ID_SI_630,
110 PCI_DEVICE_ID_SI_730,
111 0 /* terminates the list */
112};
113
114static inline u8 sis630_read(u8 reg)
115{
116 return inb(acpi_base + reg);
117}
118
119static inline void sis630_write(u8 reg, u8 data)
120{
121 outb(data, acpi_base + reg);
122}
123
124static int sis630_transaction_start(struct i2c_adapter *adap, int size, u8 *oldclock)
125{
126 int temp;
127
128 /* Make sure the SMBus host is ready to start transmitting. */
129 if ((temp = sis630_read(SMB_CNT) & 0x03) != 0x00) {
130 dev_dbg(&adap->dev, "SMBus busy (%02x).Resetting...\n",temp);
131 /* kill smbus transaction */
132 sis630_write(SMBHOST_CNT, 0x20);
133
134 if ((temp = sis630_read(SMB_CNT) & 0x03) != 0x00) {
135 dev_dbg(&adap->dev, "Failed! (%02x)\n", temp);
136 return -1;
137 } else {
138 dev_dbg(&adap->dev, "Successfull!\n");
139 }
140 }
141
142 /* save old clock, so we can prevent machine for hung */
143 *oldclock = sis630_read(SMB_CNT);
144
145 dev_dbg(&adap->dev, "saved clock 0x%02x\n", *oldclock);
146
147 /* disable timeout interrupt , set Host Master Clock to 56KHz if requested */
148 if (high_clock)
149 sis630_write(SMB_CNT, 0x20);
150 else
151 sis630_write(SMB_CNT, (*oldclock & ~0x40));
152
153 /* clear all sticky bits */
154 temp = sis630_read(SMB_STS);
155 sis630_write(SMB_STS, temp & 0x1e);
156
157 /* start the transaction by setting bit 4 and size */
158 sis630_write(SMBHOST_CNT,0x10 | (size & 0x07));
159
160 return 0;
161}
162
163static int sis630_transaction_wait(struct i2c_adapter *adap, int size)
164{
165 int temp, result = 0, timeout = 0;
166
167 /* We will always wait for a fraction of a second! */
168 do {
169 msleep(1);
170 temp = sis630_read(SMB_STS);
171 /* check if block transmitted */
172 if (size == SIS630_BLOCK_DATA && (temp & 0x10))
173 break;
174 } while (!(temp & 0x0e) && (timeout++ < MAX_TIMEOUT));
175
176 /* If the SMBus is still busy, we give up */
177 if (timeout >= MAX_TIMEOUT) {
178 dev_dbg(&adap->dev, "SMBus Timeout!\n");
179 result = -1;
180 }
181
182 if (temp & 0x02) {
183 dev_dbg(&adap->dev, "Error: Failed bus transaction\n");
184 result = -1;
185 }
186
187 if (temp & 0x04) {
188 dev_err(&adap->dev, "Bus collision!\n");
189 result = -1;
190 /*
191 TBD: Datasheet say:
192 the software should clear this bit and restart SMBUS operation.
193 Should we do it or user start request again?
194 */
195 }
196
197 return result;
198}
199
200static void sis630_transaction_end(struct i2c_adapter *adap, u8 oldclock)
201{
202 int temp = 0;
203
204 /* clear all status "sticky" bits */
205 sis630_write(SMB_STS, temp);
206
207 dev_dbg(&adap->dev, "SMB_CNT before clock restore 0x%02x\n", sis630_read(SMB_CNT));
208
209 /*
210 * restore old Host Master Clock if high_clock is set
211 * and oldclock was not 56KHz
212 */
213 if (high_clock && !(oldclock & 0x20))
214 sis630_write(SMB_CNT,(sis630_read(SMB_CNT) & ~0x20));
215
216 dev_dbg(&adap->dev, "SMB_CNT after clock restore 0x%02x\n", sis630_read(SMB_CNT));
217}
218
219static int sis630_transaction(struct i2c_adapter *adap, int size)
220{
221 int result = 0;
222 u8 oldclock = 0;
223
224 result = sis630_transaction_start(adap, size, &oldclock);
225 if (!result) {
226 result = sis630_transaction_wait(adap, size);
227 sis630_transaction_end(adap, oldclock);
228 }
229
230 return result;
231}
232
233static int sis630_block_data(struct i2c_adapter *adap, union i2c_smbus_data *data, int read_write)
234{
235 int i, len = 0, rc = 0;
236 u8 oldclock = 0;
237
238 if (read_write == I2C_SMBUS_WRITE) {
239 len = data->block[0];
240 if (len < 0)
241 len = 0;
242 else if (len > 32)
243 len = 32;
244 sis630_write(SMB_COUNT, len);
245 for (i=1; i <= len; i++) {
246 dev_dbg(&adap->dev, "set data 0x%02x\n", data->block[i]);
247 /* set data */
248 sis630_write(SMB_BYTE+(i-1)%8, data->block[i]);
249 if (i==8 || (len<8 && i==len)) {
250 dev_dbg(&adap->dev, "start trans len=%d i=%d\n",len ,i);
251 /* first transaction */
252 if (sis630_transaction_start(adap, SIS630_BLOCK_DATA, &oldclock))
253 return -1;
254 }
255 else if ((i-1)%8 == 7 || i==len) {
256 dev_dbg(&adap->dev, "trans_wait len=%d i=%d\n",len,i);
257 if (i>8) {
258 dev_dbg(&adap->dev, "clear smbary_sts len=%d i=%d\n",len,i);
259 /*
260 If this is not first transaction,
261 we must clear sticky bit.
262 clear SMBARY_STS
263 */
264 sis630_write(SMB_STS,0x10);
265 }
266 if (sis630_transaction_wait(adap, SIS630_BLOCK_DATA)) {
267 dev_dbg(&adap->dev, "trans_wait failed\n");
268 rc = -1;
269 break;
270 }
271 }
272 }
273 }
274 else {
275 /* read request */
276 data->block[0] = len = 0;
277 if (sis630_transaction_start(adap, SIS630_BLOCK_DATA, &oldclock)) {
278 return -1;
279 }
280 do {
281 if (sis630_transaction_wait(adap, SIS630_BLOCK_DATA)) {
282 dev_dbg(&adap->dev, "trans_wait failed\n");
283 rc = -1;
284 break;
285 }
286 /* if this first transaction then read byte count */
287 if (len == 0)
288 data->block[0] = sis630_read(SMB_COUNT);
289
290 /* just to be sure */
291 if (data->block[0] > 32)
292 data->block[0] = 32;
293
294 dev_dbg(&adap->dev, "block data read len=0x%x\n", data->block[0]);
295
296 for (i=0; i < 8 && len < data->block[0]; i++,len++) {
297 dev_dbg(&adap->dev, "read i=%d len=%d\n", i, len);
298 data->block[len+1] = sis630_read(SMB_BYTE+i);
299 }
300
301 dev_dbg(&adap->dev, "clear smbary_sts len=%d i=%d\n",len,i);
302
303 /* clear SMBARY_STS */
304 sis630_write(SMB_STS,0x10);
305 } while(len < data->block[0]);
306 }
307
308 sis630_transaction_end(adap, oldclock);
309
310 return rc;
311}
312
313/* Return -1 on error. */
314static s32 sis630_access(struct i2c_adapter *adap, u16 addr,
315 unsigned short flags, char read_write,
316 u8 command, int size, union i2c_smbus_data *data)
317{
318 switch (size) {
319 case I2C_SMBUS_QUICK:
320 sis630_write(SMB_ADDR, ((addr & 0x7f) << 1) | (read_write & 0x01));
321 size = SIS630_QUICK;
322 break;
323 case I2C_SMBUS_BYTE:
324 sis630_write(SMB_ADDR, ((addr & 0x7f) << 1) | (read_write & 0x01));
325 if (read_write == I2C_SMBUS_WRITE)
326 sis630_write(SMB_CMD, command);
327 size = SIS630_BYTE;
328 break;
329 case I2C_SMBUS_BYTE_DATA:
330 sis630_write(SMB_ADDR, ((addr & 0x7f) << 1) | (read_write & 0x01));
331 sis630_write(SMB_CMD, command);
332 if (read_write == I2C_SMBUS_WRITE)
333 sis630_write(SMB_BYTE, data->byte);
334 size = SIS630_BYTE_DATA;
335 break;
336 case I2C_SMBUS_PROC_CALL:
337 case I2C_SMBUS_WORD_DATA:
338 sis630_write(SMB_ADDR,((addr & 0x7f) << 1) | (read_write & 0x01));
339 sis630_write(SMB_CMD, command);
340 if (read_write == I2C_SMBUS_WRITE) {
341 sis630_write(SMB_BYTE, data->word & 0xff);
342 sis630_write(SMB_BYTE + 1,(data->word & 0xff00) >> 8);
343 }
344 size = (size == I2C_SMBUS_PROC_CALL ? SIS630_PCALL : SIS630_WORD_DATA);
345 break;
346 case I2C_SMBUS_BLOCK_DATA:
347 sis630_write(SMB_ADDR,((addr & 0x7f) << 1) | (read_write & 0x01));
348 sis630_write(SMB_CMD, command);
349 size = SIS630_BLOCK_DATA;
350 return sis630_block_data(adap, data, read_write);
351 default:
352 printk("Unsupported I2C size\n");
353 return -1;
354 break;
355 }
356
357 if (sis630_transaction(adap, size))
358 return -1;
359
360 if ((size != SIS630_PCALL) &&
361 ((read_write == I2C_SMBUS_WRITE) || (size == SIS630_QUICK))) {
362 return 0;
363 }
364
365 switch(size) {
366 case SIS630_BYTE:
367 case SIS630_BYTE_DATA:
368 data->byte = sis630_read(SMB_BYTE);
369 break;
370 case SIS630_PCALL:
371 case SIS630_WORD_DATA:
372 data->word = sis630_read(SMB_BYTE) + (sis630_read(SMB_BYTE + 1) << 8);
373 break;
374 default:
375 return -1;
376 break;
377 }
378
379 return 0;
380}
381
382static u32 sis630_func(struct i2c_adapter *adapter)
383{
384 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE | I2C_FUNC_SMBUS_BYTE_DATA |
385 I2C_FUNC_SMBUS_WORD_DATA | I2C_FUNC_SMBUS_PROC_CALL |
386 I2C_FUNC_SMBUS_BLOCK_DATA;
387}
388
389static int sis630_setup(struct pci_dev *sis630_dev)
390{
391 unsigned char b;
392 struct pci_dev *dummy = NULL;
393 int retval = -ENODEV, i;
394
395 /* check for supported SiS devices */
396 for (i=0; supported[i] > 0 ; i++) {
397 if ((dummy = pci_get_device(PCI_VENDOR_ID_SI, supported[i], dummy)))
398 break; /* found */
399 }
400
401 if (dummy) {
402 pci_dev_put(dummy);
403 }
404 else if (force) {
405 dev_err(&sis630_dev->dev, "WARNING: Can't detect SIS630 compatible device, but "
406 "loading because of force option enabled\n");
407 }
408 else {
409 return -ENODEV;
410 }
411
412 /*
413 Enable ACPI first , so we can accsess reg 74-75
414 in acpi io space and read acpi base addr
415 */
416 if (pci_read_config_byte(sis630_dev, SIS630_BIOS_CTL_REG,&b)) {
417 dev_err(&sis630_dev->dev, "Error: Can't read bios ctl reg\n");
418 goto exit;
419 }
420 /* if ACPI already enabled , do nothing */
421 if (!(b & 0x80) &&
422 pci_write_config_byte(sis630_dev, SIS630_BIOS_CTL_REG, b | 0x80)) {
423 dev_err(&sis630_dev->dev, "Error: Can't enable ACPI\n");
424 goto exit;
425 }
426
427 /* Determine the ACPI base address */
428 if (pci_read_config_word(sis630_dev,SIS630_ACPI_BASE_REG,&acpi_base)) {
429 dev_err(&sis630_dev->dev, "Error: Can't determine ACPI base address\n");
430 goto exit;
431 }
432
433 dev_dbg(&sis630_dev->dev, "ACPI base at 0x%04x\n", acpi_base);
434
435 /* Everything is happy, let's grab the memory and set things up. */
436 if (!request_region(acpi_base + SMB_STS, SIS630_SMB_IOREGION, "sis630-smbus")) {
437 dev_err(&sis630_dev->dev, "SMBus registers 0x%04x-0x%04x already "
438 "in use!\n", acpi_base + SMB_STS, acpi_base + SMB_SAA);
439 goto exit;
440 }
441
442 retval = 0;
443
444exit:
445 if (retval)
446 acpi_base = 0;
447 return retval;
448}
449
450
451static struct i2c_algorithm smbus_algorithm = {
452 .name = "Non-I2C SMBus adapter",
453 .id = I2C_ALGO_SMBUS,
454 .smbus_xfer = sis630_access,
455 .functionality = sis630_func,
456};
457
458static struct i2c_adapter sis630_adapter = {
459 .owner = THIS_MODULE,
460 .class = I2C_CLASS_HWMON,
461 .name = "unset",
462 .algo = &smbus_algorithm,
463};
464
465static struct pci_device_id sis630_ids[] __devinitdata = {
466 { PCI_DEVICE(PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_503) },
467 { PCI_DEVICE(PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_LPC) },
468 { 0, }
469};
470
471MODULE_DEVICE_TABLE (pci, sis630_ids);
472
473static int __devinit sis630_probe(struct pci_dev *dev, const struct pci_device_id *id)
474{
475 if (sis630_setup(dev)) {
476 dev_err(&dev->dev, "SIS630 comp. bus not detected, module not inserted.\n");
477 return -ENODEV;
478 }
479
480 /* set up the driverfs linkage to our parent device */
481 sis630_adapter.dev.parent = &dev->dev;
482
483 sprintf(sis630_adapter.name, "SMBus SIS630 adapter at %04x",
484 acpi_base + SMB_STS);
485
486 return i2c_add_adapter(&sis630_adapter);
487}
488
489static void __devexit sis630_remove(struct pci_dev *dev)
490{
491 if (acpi_base) {
492 i2c_del_adapter(&sis630_adapter);
493 release_region(acpi_base + SMB_STS, SIS630_SMB_IOREGION);
494 acpi_base = 0;
495 }
496}
497
498
499static struct pci_driver sis630_driver = {
500 .name = "sis630_smbus",
501 .id_table = sis630_ids,
502 .probe = sis630_probe,
503 .remove = __devexit_p(sis630_remove),
504};
505
506static int __init i2c_sis630_init(void)
507{
508 return pci_register_driver(&sis630_driver);
509}
510
511
512static void __exit i2c_sis630_exit(void)
513{
514 pci_unregister_driver(&sis630_driver);
515}
516
517
518MODULE_LICENSE("GPL");
519MODULE_AUTHOR("Alexander Malysh <amalysh@web.de>");
520MODULE_DESCRIPTION("SIS630 SMBus driver");
521
522module_init(i2c_sis630_init);
523module_exit(i2c_sis630_exit);
diff --git a/drivers/i2c/busses/i2c-sis96x.c b/drivers/i2c/busses/i2c-sis96x.c
new file mode 100644
index 000000000000..3cac6d43bce5
--- /dev/null
+++ b/drivers/i2c/busses/i2c-sis96x.c
@@ -0,0 +1,358 @@
1/*
2 sis96x.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring
4
5 Copyright (c) 2003 Mark M. Hoffman <mhoffman@lightlink.com>
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/*
23 This module must be considered BETA unless and until
24 the chipset manufacturer releases a datasheet.
25 The register definitions are based on the SiS630.
26
27 This module relies on quirk_sis_96x_smbus (drivers/pci/quirks.c)
28 for just about every machine for which users have reported.
29 If this module isn't detecting your 96x south bridge, have a
30 look there.
31
32 We assume there can only be one SiS96x with one SMBus interface.
33*/
34
35#include <linux/config.h>
36#include <linux/module.h>
37#include <linux/pci.h>
38#include <linux/kernel.h>
39#include <linux/delay.h>
40#include <linux/stddef.h>
41#include <linux/sched.h>
42#include <linux/ioport.h>
43#include <linux/i2c.h>
44#include <linux/init.h>
45#include <asm/io.h>
46
47/*
48 HISTORY:
49 2003-05-11 1.0.0 Updated from lm_sensors project for kernel 2.5
50 (was i2c-sis645.c from lm_sensors 2.7.0)
51*/
52#define SIS96x_VERSION "1.0.0"
53
54/* base address register in PCI config space */
55#define SIS96x_BAR 0x04
56
57/* SiS96x SMBus registers */
58#define SMB_STS 0x00
59#define SMB_EN 0x01
60#define SMB_CNT 0x02
61#define SMB_HOST_CNT 0x03
62#define SMB_ADDR 0x04
63#define SMB_CMD 0x05
64#define SMB_PCOUNT 0x06
65#define SMB_COUNT 0x07
66#define SMB_BYTE 0x08
67#define SMB_DEV_ADDR 0x10
68#define SMB_DB0 0x11
69#define SMB_DB1 0x12
70#define SMB_SAA 0x13
71
72/* register count for request_region */
73#define SMB_IOSIZE 0x20
74
75/* Other settings */
76#define MAX_TIMEOUT 500
77
78/* SiS96x SMBus constants */
79#define SIS96x_QUICK 0x00
80#define SIS96x_BYTE 0x01
81#define SIS96x_BYTE_DATA 0x02
82#define SIS96x_WORD_DATA 0x03
83#define SIS96x_PROC_CALL 0x04
84#define SIS96x_BLOCK_DATA 0x05
85
86static struct i2c_adapter sis96x_adapter;
87static u16 sis96x_smbus_base = 0;
88
89static inline u8 sis96x_read(u8 reg)
90{
91 return inb(sis96x_smbus_base + reg) ;
92}
93
94static inline void sis96x_write(u8 reg, u8 data)
95{
96 outb(data, sis96x_smbus_base + reg) ;
97}
98
99/* Execute a SMBus transaction.
100 int size is from SIS96x_QUICK to SIS96x_BLOCK_DATA
101 */
102static int sis96x_transaction(int size)
103{
104 int temp;
105 int result = 0;
106 int timeout = 0;
107
108 dev_dbg(&sis96x_adapter.dev, "SMBus transaction %d\n", size);
109
110 /* Make sure the SMBus host is ready to start transmitting */
111 if (((temp = sis96x_read(SMB_CNT)) & 0x03) != 0x00) {
112
113 dev_dbg(&sis96x_adapter.dev, "SMBus busy (0x%02x). "
114 "Resetting...\n", temp);
115
116 /* kill the transaction */
117 sis96x_write(SMB_HOST_CNT, 0x20);
118
119 /* check it again */
120 if (((temp = sis96x_read(SMB_CNT)) & 0x03) != 0x00) {
121 dev_dbg(&sis96x_adapter.dev, "Failed (0x%02x)\n", temp);
122 return -1;
123 } else {
124 dev_dbg(&sis96x_adapter.dev, "Successful\n");
125 }
126 }
127
128 /* Turn off timeout interrupts, set fast host clock */
129 sis96x_write(SMB_CNT, 0x20);
130
131 /* clear all (sticky) status flags */
132 temp = sis96x_read(SMB_STS);
133 sis96x_write(SMB_STS, temp & 0x1e);
134
135 /* start the transaction by setting bit 4 and size bits */
136 sis96x_write(SMB_HOST_CNT, 0x10 | (size & 0x07));
137
138 /* We will always wait for a fraction of a second! */
139 do {
140 msleep(1);
141 temp = sis96x_read(SMB_STS);
142 } while (!(temp & 0x0e) && (timeout++ < MAX_TIMEOUT));
143
144 /* If the SMBus is still busy, we give up */
145 if (timeout >= MAX_TIMEOUT) {
146 dev_dbg(&sis96x_adapter.dev, "SMBus Timeout! (0x%02x)\n", temp);
147 result = -1;
148 }
149
150 /* device error - probably missing ACK */
151 if (temp & 0x02) {
152 dev_dbg(&sis96x_adapter.dev, "Failed bus transaction!\n");
153 result = -1;
154 }
155
156 /* bus collision */
157 if (temp & 0x04) {
158 dev_dbg(&sis96x_adapter.dev, "Bus collision!\n");
159 result = -1;
160 }
161
162 /* Finish up by resetting the bus */
163 sis96x_write(SMB_STS, temp);
164 if ((temp = sis96x_read(SMB_STS))) {
165 dev_dbg(&sis96x_adapter.dev, "Failed reset at "
166 "end of transaction! (0x%02x)\n", temp);
167 }
168
169 return result;
170}
171
172/* Return -1 on error. */
173static s32 sis96x_access(struct i2c_adapter * adap, u16 addr,
174 unsigned short flags, char read_write,
175 u8 command, int size, union i2c_smbus_data * data)
176{
177
178 switch (size) {
179 case I2C_SMBUS_QUICK:
180 sis96x_write(SMB_ADDR, ((addr & 0x7f) << 1) | (read_write & 0x01));
181 size = SIS96x_QUICK;
182 break;
183
184 case I2C_SMBUS_BYTE:
185 sis96x_write(SMB_ADDR, ((addr & 0x7f) << 1) | (read_write & 0x01));
186 if (read_write == I2C_SMBUS_WRITE)
187 sis96x_write(SMB_CMD, command);
188 size = SIS96x_BYTE;
189 break;
190
191 case I2C_SMBUS_BYTE_DATA:
192 sis96x_write(SMB_ADDR, ((addr & 0x7f) << 1) | (read_write & 0x01));
193 sis96x_write(SMB_CMD, command);
194 if (read_write == I2C_SMBUS_WRITE)
195 sis96x_write(SMB_BYTE, data->byte);
196 size = SIS96x_BYTE_DATA;
197 break;
198
199 case I2C_SMBUS_PROC_CALL:
200 case I2C_SMBUS_WORD_DATA:
201 sis96x_write(SMB_ADDR, ((addr & 0x7f) << 1) | (read_write & 0x01));
202 sis96x_write(SMB_CMD, command);
203 if (read_write == I2C_SMBUS_WRITE) {
204 sis96x_write(SMB_BYTE, data->word & 0xff);
205 sis96x_write(SMB_BYTE + 1, (data->word & 0xff00) >> 8);
206 }
207 size = (size == I2C_SMBUS_PROC_CALL ?
208 SIS96x_PROC_CALL : SIS96x_WORD_DATA);
209 break;
210
211 case I2C_SMBUS_BLOCK_DATA:
212 /* TO DO: */
213 dev_info(&adap->dev, "SMBus block not implemented!\n");
214 return -1;
215 break;
216
217 default:
218 dev_info(&adap->dev, "Unsupported I2C size\n");
219 return -1;
220 break;
221 }
222
223 if (sis96x_transaction(size))
224 return -1;
225
226 if ((size != SIS96x_PROC_CALL) &&
227 ((read_write == I2C_SMBUS_WRITE) || (size == SIS96x_QUICK)))
228 return 0;
229
230 switch (size) {
231 case SIS96x_BYTE:
232 case SIS96x_BYTE_DATA:
233 data->byte = sis96x_read(SMB_BYTE);
234 break;
235
236 case SIS96x_WORD_DATA:
237 case SIS96x_PROC_CALL:
238 data->word = sis96x_read(SMB_BYTE) +
239 (sis96x_read(SMB_BYTE + 1) << 8);
240 break;
241 }
242 return 0;
243}
244
245static u32 sis96x_func(struct i2c_adapter *adapter)
246{
247 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
248 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
249 I2C_FUNC_SMBUS_PROC_CALL;
250}
251
252static struct i2c_algorithm smbus_algorithm = {
253 .name = "Non-I2C SMBus adapter",
254 .id = I2C_ALGO_SMBUS,
255 .smbus_xfer = sis96x_access,
256 .functionality = sis96x_func,
257};
258
259static struct i2c_adapter sis96x_adapter = {
260 .owner = THIS_MODULE,
261 .class = I2C_CLASS_HWMON,
262 .algo = &smbus_algorithm,
263 .name = "unset",
264};
265
266static struct pci_device_id sis96x_ids[] = {
267 { PCI_DEVICE(PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_SMBUS) },
268 { 0, }
269};
270
271MODULE_DEVICE_TABLE (pci, sis96x_ids);
272
273static int __devinit sis96x_probe(struct pci_dev *dev,
274 const struct pci_device_id *id)
275{
276 u16 ww = 0;
277 int retval;
278
279 if (sis96x_smbus_base) {
280 dev_err(&dev->dev, "Only one device supported.\n");
281 return -EBUSY;
282 }
283
284 pci_read_config_word(dev, PCI_CLASS_DEVICE, &ww);
285 if (PCI_CLASS_SERIAL_SMBUS != ww) {
286 dev_err(&dev->dev, "Unsupported device class 0x%04x!\n", ww);
287 return -ENODEV;
288 }
289
290 sis96x_smbus_base = pci_resource_start(dev, SIS96x_BAR);
291 if (!sis96x_smbus_base) {
292 dev_err(&dev->dev, "SiS96x SMBus base address "
293 "not initialized!\n");
294 return -EINVAL;
295 }
296 dev_info(&dev->dev, "SiS96x SMBus base address: 0x%04x\n",
297 sis96x_smbus_base);
298
299 /* Everything is happy, let's grab the memory and set things up. */
300 if (!request_region(sis96x_smbus_base, SMB_IOSIZE, "sis96x-smbus")) {
301 dev_err(&dev->dev, "SMBus registers 0x%04x-0x%04x "
302 "already in use!\n", sis96x_smbus_base,
303 sis96x_smbus_base + SMB_IOSIZE - 1);
304
305 sis96x_smbus_base = 0;
306 return -EINVAL;
307 }
308
309 /* set up the driverfs linkage to our parent device */
310 sis96x_adapter.dev.parent = &dev->dev;
311
312 snprintf(sis96x_adapter.name, I2C_NAME_SIZE,
313 "SiS96x SMBus adapter at 0x%04x", sis96x_smbus_base);
314
315 if ((retval = i2c_add_adapter(&sis96x_adapter))) {
316 dev_err(&dev->dev, "Couldn't register adapter!\n");
317 release_region(sis96x_smbus_base, SMB_IOSIZE);
318 sis96x_smbus_base = 0;
319 }
320
321 return retval;
322}
323
324static void __devexit sis96x_remove(struct pci_dev *dev)
325{
326 if (sis96x_smbus_base) {
327 i2c_del_adapter(&sis96x_adapter);
328 release_region(sis96x_smbus_base, SMB_IOSIZE);
329 sis96x_smbus_base = 0;
330 }
331}
332
333static struct pci_driver sis96x_driver = {
334 .name = "sis96x_smbus",
335 .id_table = sis96x_ids,
336 .probe = sis96x_probe,
337 .remove = __devexit_p(sis96x_remove),
338};
339
340static int __init i2c_sis96x_init(void)
341{
342 printk(KERN_INFO "i2c-sis96x version %s\n", SIS96x_VERSION);
343 return pci_register_driver(&sis96x_driver);
344}
345
346static void __exit i2c_sis96x_exit(void)
347{
348 pci_unregister_driver(&sis96x_driver);
349}
350
351MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
352MODULE_DESCRIPTION("SiS96x SMBus driver");
353MODULE_LICENSE("GPL");
354
355/* Register initialization functions using helper macros */
356module_init(i2c_sis96x_init);
357module_exit(i2c_sis96x_exit);
358
diff --git a/drivers/i2c/busses/i2c-stub.c b/drivers/i2c/busses/i2c-stub.c
new file mode 100644
index 000000000000..19c805ead4d8
--- /dev/null
+++ b/drivers/i2c/busses/i2c-stub.c
@@ -0,0 +1,143 @@
1/*
2 i2c-stub.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring
4
5 Copyright (c) 2004 Mark M. Hoffman <mhoffman@lightlink.com>
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#define DEBUG 1
23
24#include <linux/config.h>
25#include <linux/init.h>
26#include <linux/module.h>
27#include <linux/kernel.h>
28#include <linux/errno.h>
29#include <linux/i2c.h>
30
31static u8 stub_pointer;
32static u8 stub_bytes[256];
33static u16 stub_words[256];
34
35/* Return -1 on error. */
36static s32 stub_xfer(struct i2c_adapter * adap, u16 addr, unsigned short flags,
37 char read_write, u8 command, int size, union i2c_smbus_data * data)
38{
39 s32 ret;
40
41 switch (size) {
42
43 case I2C_SMBUS_QUICK:
44 dev_dbg(&adap->dev, "smbus quick - addr 0x%02x\n", addr);
45 ret = 0;
46 break;
47
48 case I2C_SMBUS_BYTE:
49 if (read_write == I2C_SMBUS_WRITE) {
50 stub_pointer = command;
51 dev_dbg(&adap->dev, "smbus byte - addr 0x%02x, "
52 "wrote 0x%02x.\n",
53 addr, command);
54 } else {
55 data->byte = stub_bytes[stub_pointer++];
56 dev_dbg(&adap->dev, "smbus byte - addr 0x%02x, "
57 "read 0x%02x.\n",
58 addr, data->byte);
59 }
60
61 ret = 0;
62 break;
63
64 case I2C_SMBUS_BYTE_DATA:
65 if (read_write == I2C_SMBUS_WRITE) {
66 stub_bytes[command] = data->byte;
67 dev_dbg(&adap->dev, "smbus byte data - addr 0x%02x, "
68 "wrote 0x%02x at 0x%02x.\n",
69 addr, data->byte, command);
70 } else {
71 data->byte = stub_bytes[command];
72 dev_dbg(&adap->dev, "smbus byte data - addr 0x%02x, "
73 "read 0x%02x at 0x%02x.\n",
74 addr, data->byte, command);
75 }
76 stub_pointer = command + 1;
77
78 ret = 0;
79 break;
80
81 case I2C_SMBUS_WORD_DATA:
82 if (read_write == I2C_SMBUS_WRITE) {
83 stub_words[command] = data->word;
84 dev_dbg(&adap->dev, "smbus word data - addr 0x%02x, "
85 "wrote 0x%04x at 0x%02x.\n",
86 addr, data->word, command);
87 } else {
88 data->word = stub_words[command];
89 dev_dbg(&adap->dev, "smbus word data - addr 0x%02x, "
90 "read 0x%04x at 0x%02x.\n",
91 addr, data->word, command);
92 }
93
94 ret = 0;
95 break;
96
97 default:
98 dev_dbg(&adap->dev, "Unsupported I2C/SMBus command\n");
99 ret = -1;
100 break;
101 } /* switch (size) */
102
103 return ret;
104}
105
106static u32 stub_func(struct i2c_adapter *adapter)
107{
108 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
109 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA;
110}
111
112static struct i2c_algorithm smbus_algorithm = {
113 .name = "Non-I2C SMBus adapter",
114 .id = I2C_ALGO_SMBUS,
115 .functionality = stub_func,
116 .smbus_xfer = stub_xfer,
117};
118
119static struct i2c_adapter stub_adapter = {
120 .owner = THIS_MODULE,
121 .class = I2C_CLASS_HWMON,
122 .algo = &smbus_algorithm,
123 .name = "SMBus stub driver",
124};
125
126static int __init i2c_stub_init(void)
127{
128 printk(KERN_INFO "i2c-stub loaded\n");
129 return i2c_add_adapter(&stub_adapter);
130}
131
132static void __exit i2c_stub_exit(void)
133{
134 i2c_del_adapter(&stub_adapter);
135}
136
137MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
138MODULE_DESCRIPTION("I2C stub driver");
139MODULE_LICENSE("GPL");
140
141module_init(i2c_stub_init);
142module_exit(i2c_stub_exit);
143
diff --git a/drivers/i2c/busses/i2c-via.c b/drivers/i2c/busses/i2c-via.c
new file mode 100644
index 000000000000..2cbc4cd22366
--- /dev/null
+++ b/drivers/i2c/busses/i2c-via.c
@@ -0,0 +1,185 @@
1/*
2 i2c-via.c - Part of lm_sensors, Linux kernel modules
3 for hardware monitoring
4
5 i2c Support for Via Technologies 82C586B South Bridge
6
7 Copyright (c) 1998, 1999 Kyösti Mälkki <kmalkki@cc.hut.fi>
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22*/
23
24#include <linux/config.h>
25#include <linux/kernel.h>
26#include <linux/module.h>
27#include <linux/pci.h>
28#include <linux/ioport.h>
29#include <linux/init.h>
30#include <linux/i2c.h>
31#include <linux/i2c-algo-bit.h>
32#include <asm/io.h>
33
34/* Power management registers */
35#define PM_CFG_REVID 0x08 /* silicon revision code */
36#define PM_CFG_IOBASE0 0x20
37#define PM_CFG_IOBASE1 0x48
38
39#define I2C_DIR (pm_io_base+0x40)
40#define I2C_OUT (pm_io_base+0x42)
41#define I2C_IN (pm_io_base+0x44)
42#define I2C_SCL 0x02 /* clock bit in DIR/OUT/IN register */
43#define I2C_SDA 0x04
44
45/* io-region reservation */
46#define IOSPACE 0x06
47#define IOTEXT "via-i2c"
48
49static u16 pm_io_base = 0;
50
51/*
52 It does not appear from the datasheet that the GPIO pins are
53 open drain. So a we set a low value by setting the direction to
54 output and a high value by setting the direction to input and
55 relying on the required I2C pullup. The data value is initialized
56 to 0 in via_init() and never changed.
57*/
58static void bit_via_setscl(void *data, int state)
59{
60 outb(state ? inb(I2C_DIR) & ~I2C_SCL : inb(I2C_DIR) | I2C_SCL, I2C_DIR);
61}
62
63static void bit_via_setsda(void *data, int state)
64{
65 outb(state ? inb(I2C_DIR) & ~I2C_SDA : inb(I2C_DIR) | I2C_SDA, I2C_DIR);
66}
67
68static int bit_via_getscl(void *data)
69{
70 return (0 != (inb(I2C_IN) & I2C_SCL));
71}
72
73static int bit_via_getsda(void *data)
74{
75 return (0 != (inb(I2C_IN) & I2C_SDA));
76}
77
78
79static struct i2c_algo_bit_data bit_data = {
80 .setsda = bit_via_setsda,
81 .setscl = bit_via_setscl,
82 .getsda = bit_via_getsda,
83 .getscl = bit_via_getscl,
84 .udelay = 5,
85 .mdelay = 5,
86 .timeout = HZ
87};
88
89static struct i2c_adapter vt586b_adapter = {
90 .owner = THIS_MODULE,
91 .class = I2C_CLASS_HWMON,
92 .name = "VIA i2c",
93 .algo_data = &bit_data,
94};
95
96
97static struct pci_device_id vt586b_ids[] __devinitdata = {
98 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_82C586_3) },
99 { 0, }
100};
101
102MODULE_DEVICE_TABLE (pci, vt586b_ids);
103
104static int __devinit vt586b_probe(struct pci_dev *dev, const struct pci_device_id *id)
105{
106 u16 base;
107 u8 rev;
108 int res;
109
110 if (pm_io_base) {
111 dev_err(&dev->dev, "i2c-via: Will only support one host\n");
112 return -ENODEV;
113 }
114
115 pci_read_config_byte(dev, PM_CFG_REVID, &rev);
116
117 switch (rev) {
118 case 0x00:
119 base = PM_CFG_IOBASE0;
120 break;
121 case 0x01:
122 case 0x10:
123 base = PM_CFG_IOBASE1;
124 break;
125
126 default:
127 base = PM_CFG_IOBASE1;
128 /* later revision */
129 }
130
131 pci_read_config_word(dev, base, &pm_io_base);
132 pm_io_base &= (0xff << 8);
133
134 if (!request_region(I2C_DIR, IOSPACE, IOTEXT)) {
135 dev_err(&dev->dev, "IO 0x%x-0x%x already in use\n", I2C_DIR, I2C_DIR + IOSPACE);
136 return -ENODEV;
137 }
138
139 outb(inb(I2C_DIR) & ~(I2C_SDA | I2C_SCL), I2C_DIR);
140 outb(inb(I2C_OUT) & ~(I2C_SDA | I2C_SCL), I2C_OUT);
141
142 /* set up the driverfs linkage to our parent device */
143 vt586b_adapter.dev.parent = &dev->dev;
144
145 res = i2c_bit_add_bus(&vt586b_adapter);
146 if ( res < 0 ) {
147 release_region(I2C_DIR, IOSPACE);
148 pm_io_base = 0;
149 return res;
150 }
151 return 0;
152}
153
154static void __devexit vt586b_remove(struct pci_dev *dev)
155{
156 i2c_bit_del_bus(&vt586b_adapter);
157 release_region(I2C_DIR, IOSPACE);
158 pm_io_base = 0;
159}
160
161
162static struct pci_driver vt586b_driver = {
163 .name = "vt586b_smbus",
164 .id_table = vt586b_ids,
165 .probe = vt586b_probe,
166 .remove = __devexit_p(vt586b_remove),
167};
168
169static int __init i2c_vt586b_init(void)
170{
171 return pci_register_driver(&vt586b_driver);
172}
173
174static void __exit i2c_vt586b_exit(void)
175{
176 pci_unregister_driver(&vt586b_driver);
177}
178
179
180MODULE_AUTHOR("Kyösti Mälkki <kmalkki@cc.hut.fi>");
181MODULE_DESCRIPTION("i2c for Via vt82c586b southbridge");
182MODULE_LICENSE("GPL");
183
184module_init(i2c_vt586b_init);
185module_exit(i2c_vt586b_exit);
diff --git a/drivers/i2c/busses/i2c-viapro.c b/drivers/i2c/busses/i2c-viapro.c
new file mode 100644
index 000000000000..0bb60a636e16
--- /dev/null
+++ b/drivers/i2c/busses/i2c-viapro.c
@@ -0,0 +1,458 @@
1/*
2 i2c-viapro.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring
4 Copyright (c) 1998 - 2002 Frodo Looijaard <frodol@dds.nl>,
5 Philip Edelbrock <phil@netroedge.com>, Kyösti Mälkki <kmalkki@cc.hut.fi>,
6 Mark D. Studebaker <mdsxyz123@yahoo.com>
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; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21*/
22
23/*
24 Supports Via devices:
25 82C596A/B (0x3050)
26 82C596B (0x3051)
27 82C686A/B
28 8231
29 8233
30 8233A (0x3147 and 0x3177)
31 8235
32 8237
33 Note: we assume there can only be one device, with one SMBus interface.
34*/
35
36#include <linux/config.h>
37#include <linux/module.h>
38#include <linux/delay.h>
39#include <linux/pci.h>
40#include <linux/kernel.h>
41#include <linux/stddef.h>
42#include <linux/sched.h>
43#include <linux/ioport.h>
44#include <linux/i2c.h>
45#include <linux/init.h>
46#include <asm/io.h>
47
48static struct pci_dev *vt596_pdev;
49
50#define SMBBA1 0x90
51#define SMBBA2 0x80
52#define SMBBA3 0xD0
53
54/* SMBus address offsets */
55static unsigned short vt596_smba;
56#define SMBHSTSTS (vt596_smba + 0)
57#define SMBHSLVSTS (vt596_smba + 1)
58#define SMBHSTCNT (vt596_smba + 2)
59#define SMBHSTCMD (vt596_smba + 3)
60#define SMBHSTADD (vt596_smba + 4)
61#define SMBHSTDAT0 (vt596_smba + 5)
62#define SMBHSTDAT1 (vt596_smba + 6)
63#define SMBBLKDAT (vt596_smba + 7)
64#define SMBSLVCNT (vt596_smba + 8)
65#define SMBSHDWCMD (vt596_smba + 9)
66#define SMBSLVEVT (vt596_smba + 0xA)
67#define SMBSLVDAT (vt596_smba + 0xC)
68
69/* PCI Address Constants */
70
71/* SMBus data in configuration space can be found in two places,
72 We try to select the better one*/
73
74static unsigned short smb_cf_hstcfg = 0xD2;
75
76#define SMBHSTCFG (smb_cf_hstcfg)
77#define SMBSLVC (smb_cf_hstcfg + 1)
78#define SMBSHDW1 (smb_cf_hstcfg + 2)
79#define SMBSHDW2 (smb_cf_hstcfg + 3)
80#define SMBREV (smb_cf_hstcfg + 4)
81
82/* Other settings */
83#define MAX_TIMEOUT 500
84#define ENABLE_INT9 0
85
86/* VT82C596 constants */
87#define VT596_QUICK 0x00
88#define VT596_BYTE 0x04
89#define VT596_BYTE_DATA 0x08
90#define VT596_WORD_DATA 0x0C
91#define VT596_BLOCK_DATA 0x14
92
93
94/* If force is set to anything different from 0, we forcibly enable the
95 VT596. DANGEROUS! */
96static int force;
97module_param(force, bool, 0);
98MODULE_PARM_DESC(force, "Forcibly enable the SMBus. DANGEROUS!");
99
100/* If force_addr is set to anything different from 0, we forcibly enable
101 the VT596 at the given address. VERY DANGEROUS! */
102static u16 force_addr;
103module_param(force_addr, ushort, 0);
104MODULE_PARM_DESC(force_addr,
105 "Forcibly enable the SMBus at the given address. "
106 "EXTREMELY DANGEROUS!");
107
108
109static struct i2c_adapter vt596_adapter;
110
111/* Another internally used function */
112static int vt596_transaction(void)
113{
114 int temp;
115 int result = 0;
116 int timeout = 0;
117
118 dev_dbg(&vt596_adapter.dev, "Transaction (pre): CNT=%02x, CMD=%02x, "
119 "ADD=%02x, DAT0=%02x, DAT1=%02x\n", inb_p(SMBHSTCNT),
120 inb_p(SMBHSTCMD), inb_p(SMBHSTADD), inb_p(SMBHSTDAT0),
121 inb_p(SMBHSTDAT1));
122
123 /* Make sure the SMBus host is ready to start transmitting */
124 if ((temp = inb_p(SMBHSTSTS)) & 0x1F) {
125 dev_dbg(&vt596_adapter.dev, "SMBus busy (0x%02x). "
126 "Resetting...\n", temp);
127
128 outb_p(temp, SMBHSTSTS);
129 if ((temp = inb_p(SMBHSTSTS)) & 0x1F) {
130 dev_dbg(&vt596_adapter.dev, "Failed! (0x%02x)\n", temp);
131
132 return -1;
133 } else {
134 dev_dbg(&vt596_adapter.dev, "Successfull!\n");
135 }
136 }
137
138 /* start the transaction by setting bit 6 */
139 outb_p(inb(SMBHSTCNT) | 0x040, SMBHSTCNT);
140
141 /* We will always wait for a fraction of a second!
142 I don't know if VIA needs this, Intel did */
143 do {
144 msleep(1);
145 temp = inb_p(SMBHSTSTS);
146 } while ((temp & 0x01) && (timeout++ < MAX_TIMEOUT));
147
148 /* If the SMBus is still busy, we give up */
149 if (timeout >= MAX_TIMEOUT) {
150 result = -1;
151 dev_dbg(&vt596_adapter.dev, "SMBus Timeout!\n");
152 }
153
154 if (temp & 0x10) {
155 result = -1;
156 dev_dbg(&vt596_adapter.dev, "Error: Failed bus transaction\n");
157 }
158
159 if (temp & 0x08) {
160 result = -1;
161 dev_info(&vt596_adapter.dev, "Bus collision! SMBus may be "
162 "locked until next hard\nreset. (sorry!)\n");
163 /* Clock stops and slave is stuck in mid-transmission */
164 }
165
166 if (temp & 0x04) {
167 result = -1;
168 dev_dbg(&vt596_adapter.dev, "Error: no response!\n");
169 }
170
171 if ((temp = inb_p(SMBHSTSTS)) & 0x1F) {
172 outb_p(temp, SMBHSTSTS);
173 if ((temp = inb_p(SMBHSTSTS)) & 0x1F) {
174 dev_warn(&vt596_adapter.dev, "Failed reset at end "
175 "of transaction (%02x)\n", temp);
176 }
177 }
178
179 dev_dbg(&vt596_adapter.dev, "Transaction (post): CNT=%02x, CMD=%02x, "
180 "ADD=%02x, DAT0=%02x, DAT1=%02x\n", inb_p(SMBHSTCNT),
181 inb_p(SMBHSTCMD), inb_p(SMBHSTADD), inb_p(SMBHSTDAT0),
182 inb_p(SMBHSTDAT1));
183
184 return result;
185}
186
187/* Return -1 on error. */
188static s32 vt596_access(struct i2c_adapter *adap, u16 addr,
189 unsigned short flags, char read_write, u8 command,
190 int size, union i2c_smbus_data *data)
191{
192 int i, len;
193
194 switch (size) {
195 case I2C_SMBUS_PROC_CALL:
196 dev_info(&vt596_adapter.dev,
197 "I2C_SMBUS_PROC_CALL not supported!\n");
198 return -1;
199 case I2C_SMBUS_QUICK:
200 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
201 SMBHSTADD);
202 size = VT596_QUICK;
203 break;
204 case I2C_SMBUS_BYTE:
205 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
206 SMBHSTADD);
207 if (read_write == I2C_SMBUS_WRITE)
208 outb_p(command, SMBHSTCMD);
209 size = VT596_BYTE;
210 break;
211 case I2C_SMBUS_BYTE_DATA:
212 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
213 SMBHSTADD);
214 outb_p(command, SMBHSTCMD);
215 if (read_write == I2C_SMBUS_WRITE)
216 outb_p(data->byte, SMBHSTDAT0);
217 size = VT596_BYTE_DATA;
218 break;
219 case I2C_SMBUS_WORD_DATA:
220 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
221 SMBHSTADD);
222 outb_p(command, SMBHSTCMD);
223 if (read_write == I2C_SMBUS_WRITE) {
224 outb_p(data->word & 0xff, SMBHSTDAT0);
225 outb_p((data->word & 0xff00) >> 8, SMBHSTDAT1);
226 }
227 size = VT596_WORD_DATA;
228 break;
229 case I2C_SMBUS_BLOCK_DATA:
230 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
231 SMBHSTADD);
232 outb_p(command, SMBHSTCMD);
233 if (read_write == I2C_SMBUS_WRITE) {
234 len = data->block[0];
235 if (len < 0)
236 len = 0;
237 if (len > I2C_SMBUS_BLOCK_MAX)
238 len = I2C_SMBUS_BLOCK_MAX;
239 outb_p(len, SMBHSTDAT0);
240 i = inb_p(SMBHSTCNT); /* Reset SMBBLKDAT */
241 for (i = 1; i <= len; i++)
242 outb_p(data->block[i], SMBBLKDAT);
243 }
244 size = VT596_BLOCK_DATA;
245 break;
246 }
247
248 outb_p((size & 0x1C) + (ENABLE_INT9 & 1), SMBHSTCNT);
249
250 if (vt596_transaction()) /* Error in transaction */
251 return -1;
252
253 if ((read_write == I2C_SMBUS_WRITE) || (size == VT596_QUICK))
254 return 0;
255
256 switch (size) {
257 case VT596_BYTE:
258 /* Where is the result put? I assume here it is in
259 * SMBHSTDAT0 but it might just as well be in the
260 * SMBHSTCMD. No clue in the docs
261 */
262 data->byte = inb_p(SMBHSTDAT0);
263 break;
264 case VT596_BYTE_DATA:
265 data->byte = inb_p(SMBHSTDAT0);
266 break;
267 case VT596_WORD_DATA:
268 data->word = inb_p(SMBHSTDAT0) + (inb_p(SMBHSTDAT1) << 8);
269 break;
270 case VT596_BLOCK_DATA:
271 data->block[0] = inb_p(SMBHSTDAT0);
272 if (data->block[0] > I2C_SMBUS_BLOCK_MAX)
273 data->block[0] = I2C_SMBUS_BLOCK_MAX;
274 i = inb_p(SMBHSTCNT); /* Reset SMBBLKDAT */
275 for (i = 1; i <= data->block[0]; i++)
276 data->block[i] = inb_p(SMBBLKDAT);
277 break;
278 }
279 return 0;
280}
281
282static u32 vt596_func(struct i2c_adapter *adapter)
283{
284 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
285 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
286 I2C_FUNC_SMBUS_BLOCK_DATA;
287}
288
289static struct i2c_algorithm smbus_algorithm = {
290 .name = "Non-I2C SMBus adapter",
291 .id = I2C_ALGO_SMBUS,
292 .smbus_xfer = vt596_access,
293 .functionality = vt596_func,
294};
295
296static struct i2c_adapter vt596_adapter = {
297 .owner = THIS_MODULE,
298 .class = I2C_CLASS_HWMON,
299 .algo = &smbus_algorithm,
300 .name = "unset",
301};
302
303static int __devinit vt596_probe(struct pci_dev *pdev,
304 const struct pci_device_id *id)
305{
306 unsigned char temp;
307 int error = -ENODEV;
308
309 /* Determine the address of the SMBus areas */
310 if (force_addr) {
311 vt596_smba = force_addr & 0xfff0;
312 force = 0;
313 goto found;
314 }
315
316 if ((pci_read_config_word(pdev, id->driver_data, &vt596_smba)) ||
317 !(vt596_smba & 0x1)) {
318 /* try 2nd address and config reg. for 596 */
319 if (id->device == PCI_DEVICE_ID_VIA_82C596_3 &&
320 !pci_read_config_word(pdev, SMBBA2, &vt596_smba) &&
321 (vt596_smba & 0x1)) {
322 smb_cf_hstcfg = 0x84;
323 } else {
324 /* no matches at all */
325 dev_err(&pdev->dev, "Cannot configure "
326 "SMBus I/O Base address\n");
327 return -ENODEV;
328 }
329 }
330
331 vt596_smba &= 0xfff0;
332 if (vt596_smba == 0) {
333 dev_err(&pdev->dev, "SMBus base address "
334 "uninitialized - upgrade BIOS or use "
335 "force_addr=0xaddr\n");
336 return -ENODEV;
337 }
338
339 found:
340 if (!request_region(vt596_smba, 8, "viapro-smbus")) {
341 dev_err(&pdev->dev, "SMBus region 0x%x already in use!\n",
342 vt596_smba);
343 return -ENODEV;
344 }
345
346 pci_read_config_byte(pdev, SMBHSTCFG, &temp);
347 /* If force_addr is set, we program the new address here. Just to make
348 sure, we disable the VT596 first. */
349 if (force_addr) {
350 pci_write_config_byte(pdev, SMBHSTCFG, temp & 0xfe);
351 pci_write_config_word(pdev, id->driver_data, vt596_smba);
352 pci_write_config_byte(pdev, SMBHSTCFG, temp | 0x01);
353 dev_warn(&pdev->dev, "WARNING: SMBus interface set to new "
354 "address 0x%04x!\n", vt596_smba);
355 } else if ((temp & 1) == 0) {
356 if (force) {
357 /* NOTE: This assumes I/O space and other allocations
358 * WERE done by the Bios! Don't complain if your
359 * hardware does weird things after enabling this.
360 * :') Check for Bios updates before resorting to
361 * this.
362 */
363 pci_write_config_byte(pdev, SMBHSTCFG, temp | 1);
364 dev_info(&pdev->dev, "Enabling SMBus device\n");
365 } else {
366 dev_err(&pdev->dev, "SMBUS: Error: Host SMBus "
367 "controller not enabled! - upgrade BIOS or "
368 "use force=1\n");
369 goto release_region;
370 }
371 }
372
373 if ((temp & 0x0E) == 8)
374 dev_dbg(&pdev->dev, "using Interrupt 9 for SMBus.\n");
375 else if ((temp & 0x0E) == 0)
376 dev_dbg(&pdev->dev, "using Interrupt SMI# for SMBus.\n");
377 else
378 dev_dbg(&pdev->dev, "Illegal Interrupt configuration "
379 "(or code out of date)!\n");
380
381 pci_read_config_byte(pdev, SMBREV, &temp);
382 dev_dbg(&pdev->dev, "SMBREV = 0x%X\n", temp);
383 dev_dbg(&pdev->dev, "VT596_smba = 0x%X\n", vt596_smba);
384
385 vt596_adapter.dev.parent = &pdev->dev;
386 snprintf(vt596_adapter.name, I2C_NAME_SIZE,
387 "SMBus Via Pro adapter at %04x", vt596_smba);
388
389 vt596_pdev = pci_dev_get(pdev);
390 if (i2c_add_adapter(&vt596_adapter)) {
391 pci_dev_put(vt596_pdev);
392 vt596_pdev = NULL;
393 }
394
395 /* Always return failure here. This is to allow other drivers to bind
396 * to this pci device. We don't really want to have control over the
397 * pci device, we only wanted to read as few register values from it.
398 */
399 return -ENODEV;
400
401 release_region:
402 release_region(vt596_smba, 8);
403 return error;
404}
405
406static struct pci_device_id vt596_ids[] = {
407 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_82C596_3),
408 .driver_data = SMBBA1 },
409 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_82C596B_3),
410 .driver_data = SMBBA1 },
411 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_82C686_4),
412 .driver_data = SMBBA1 },
413 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8233_0),
414 .driver_data = SMBBA3 },
415 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8233A),
416 .driver_data = SMBBA3 },
417 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8235),
418 .driver_data = SMBBA3 },
419 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8237),
420 .driver_data = SMBBA3 },
421 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8231_4),
422 .driver_data = SMBBA1 },
423 { 0, }
424};
425
426MODULE_DEVICE_TABLE (pci, vt596_ids);
427
428static struct pci_driver vt596_driver = {
429 .name = "vt596_smbus",
430 .id_table = vt596_ids,
431 .probe = vt596_probe,
432};
433
434static int __init i2c_vt596_init(void)
435{
436 return pci_register_driver(&vt596_driver);
437}
438
439
440static void __exit i2c_vt596_exit(void)
441{
442 pci_unregister_driver(&vt596_driver);
443 if (vt596_pdev != NULL) {
444 i2c_del_adapter(&vt596_adapter);
445 release_region(vt596_smba, 8);
446 pci_dev_put(vt596_pdev);
447 vt596_pdev = NULL;
448 }
449}
450
451MODULE_AUTHOR(
452 "Frodo Looijaard <frodol@dds.nl> and "
453 "Philip Edelbrock <phil@netroedge.com>");
454MODULE_DESCRIPTION("vt82c596 SMBus driver");
455MODULE_LICENSE("GPL");
456
457module_init(i2c_vt596_init);
458module_exit(i2c_vt596_exit);
diff --git a/drivers/i2c/busses/i2c-voodoo3.c b/drivers/i2c/busses/i2c-voodoo3.c
new file mode 100644
index 000000000000..3edf0e34155e
--- /dev/null
+++ b/drivers/i2c/busses/i2c-voodoo3.c
@@ -0,0 +1,254 @@
1/*
2 voodoo3.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring
4 Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl>,
5 Philip Edelbrock <phil@netroedge.com>,
6 Ralph Metzler <rjkm@thp.uni-koeln.de>, and
7 Mark D. Studebaker <mdsxyz123@yahoo.com>
8
9 Based on code written by Ralph Metzler <rjkm@thp.uni-koeln.de> and
10 Simon Vogl
11
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25*/
26
27/* This interfaces to the I2C bus of the Voodoo3 to gain access to
28 the BT869 and possibly other I2C devices. */
29
30#include <linux/config.h>
31#include <linux/kernel.h>
32#include <linux/module.h>
33#include <linux/init.h>
34#include <linux/pci.h>
35#include <linux/i2c.h>
36#include <linux/i2c-algo-bit.h>
37#include <asm/io.h>
38
39/* the only registers we use */
40#define REG 0x78
41#define REG2 0x70
42
43/* bit locations in the register */
44#define DDC_ENAB 0x00040000
45#define DDC_SCL_OUT 0x00080000
46#define DDC_SDA_OUT 0x00100000
47#define DDC_SCL_IN 0x00200000
48#define DDC_SDA_IN 0x00400000
49#define I2C_ENAB 0x00800000
50#define I2C_SCL_OUT 0x01000000
51#define I2C_SDA_OUT 0x02000000
52#define I2C_SCL_IN 0x04000000
53#define I2C_SDA_IN 0x08000000
54
55/* initialization states */
56#define INIT2 0x2
57#define INIT3 0x4
58
59/* delays */
60#define CYCLE_DELAY 10
61#define TIMEOUT (HZ / 2)
62
63
64static void __iomem *ioaddr;
65
66/* The voo GPIO registers don't have individual masks for each bit
67 so we always have to read before writing. */
68
69static void bit_vooi2c_setscl(void *data, int val)
70{
71 unsigned int r;
72 r = readl(ioaddr + REG);
73 if (val)
74 r |= I2C_SCL_OUT;
75 else
76 r &= ~I2C_SCL_OUT;
77 writel(r, ioaddr + REG);
78 readl(ioaddr + REG); /* flush posted write */
79}
80
81static void bit_vooi2c_setsda(void *data, int val)
82{
83 unsigned int r;
84 r = readl(ioaddr + REG);
85 if (val)
86 r |= I2C_SDA_OUT;
87 else
88 r &= ~I2C_SDA_OUT;
89 writel(r, ioaddr + REG);
90 readl(ioaddr + REG); /* flush posted write */
91}
92
93/* The GPIO pins are open drain, so the pins always remain outputs.
94 We rely on the i2c-algo-bit routines to set the pins high before
95 reading the input from other chips. */
96
97static int bit_vooi2c_getscl(void *data)
98{
99 return (0 != (readl(ioaddr + REG) & I2C_SCL_IN));
100}
101
102static int bit_vooi2c_getsda(void *data)
103{
104 return (0 != (readl(ioaddr + REG) & I2C_SDA_IN));
105}
106
107static void bit_vooddc_setscl(void *data, int val)
108{
109 unsigned int r;
110 r = readl(ioaddr + REG);
111 if (val)
112 r |= DDC_SCL_OUT;
113 else
114 r &= ~DDC_SCL_OUT;
115 writel(r, ioaddr + REG);
116 readl(ioaddr + REG); /* flush posted write */
117}
118
119static void bit_vooddc_setsda(void *data, int val)
120{
121 unsigned int r;
122 r = readl(ioaddr + REG);
123 if (val)
124 r |= DDC_SDA_OUT;
125 else
126 r &= ~DDC_SDA_OUT;
127 writel(r, ioaddr + REG);
128 readl(ioaddr + REG); /* flush posted write */
129}
130
131static int bit_vooddc_getscl(void *data)
132{
133 return (0 != (readl(ioaddr + REG) & DDC_SCL_IN));
134}
135
136static int bit_vooddc_getsda(void *data)
137{
138 return (0 != (readl(ioaddr + REG) & DDC_SDA_IN));
139}
140
141static int config_v3(struct pci_dev *dev)
142{
143 unsigned long cadr;
144
145 /* map Voodoo3 memory */
146 cadr = dev->resource[0].start;
147 cadr &= PCI_BASE_ADDRESS_MEM_MASK;
148 ioaddr = ioremap_nocache(cadr, 0x1000);
149 if (ioaddr) {
150 writel(0x8160, ioaddr + REG2);
151 writel(0xcffc0020, ioaddr + REG);
152 dev_info(&dev->dev, "Using Banshee/Voodoo3 I2C device at %p\n", ioaddr);
153 return 0;
154 }
155 return -ENODEV;
156}
157
158static struct i2c_algo_bit_data voo_i2c_bit_data = {
159 .setsda = bit_vooi2c_setsda,
160 .setscl = bit_vooi2c_setscl,
161 .getsda = bit_vooi2c_getsda,
162 .getscl = bit_vooi2c_getscl,
163 .udelay = CYCLE_DELAY,
164 .mdelay = CYCLE_DELAY,
165 .timeout = TIMEOUT
166};
167
168static struct i2c_adapter voodoo3_i2c_adapter = {
169 .owner = THIS_MODULE,
170 .class = I2C_CLASS_TV_ANALOG,
171 .name = "I2C Voodoo3/Banshee adapter",
172 .algo_data = &voo_i2c_bit_data,
173};
174
175static struct i2c_algo_bit_data voo_ddc_bit_data = {
176 .setsda = bit_vooddc_setsda,
177 .setscl = bit_vooddc_setscl,
178 .getsda = bit_vooddc_getsda,
179 .getscl = bit_vooddc_getscl,
180 .udelay = CYCLE_DELAY,
181 .mdelay = CYCLE_DELAY,
182 .timeout = TIMEOUT
183};
184
185static struct i2c_adapter voodoo3_ddc_adapter = {
186 .owner = THIS_MODULE,
187 .class = I2C_CLASS_DDC,
188 .name = "DDC Voodoo3/Banshee adapter",
189 .algo_data = &voo_ddc_bit_data,
190};
191
192static struct pci_device_id voodoo3_ids[] __devinitdata = {
193 { PCI_DEVICE(PCI_VENDOR_ID_3DFX, PCI_DEVICE_ID_3DFX_VOODOO3) },
194 { PCI_DEVICE(PCI_VENDOR_ID_3DFX, PCI_DEVICE_ID_3DFX_BANSHEE) },
195 { 0, }
196};
197
198MODULE_DEVICE_TABLE (pci, voodoo3_ids);
199
200static int __devinit voodoo3_probe(struct pci_dev *dev, const struct pci_device_id *id)
201{
202 int retval;
203
204 retval = config_v3(dev);
205 if (retval)
206 return retval;
207
208 /* set up the sysfs linkage to our parent device */
209 voodoo3_i2c_adapter.dev.parent = &dev->dev;
210 voodoo3_ddc_adapter.dev.parent = &dev->dev;
211
212 retval = i2c_bit_add_bus(&voodoo3_i2c_adapter);
213 if (retval)
214 return retval;
215 retval = i2c_bit_add_bus(&voodoo3_ddc_adapter);
216 if (retval)
217 i2c_bit_del_bus(&voodoo3_i2c_adapter);
218 return retval;
219}
220
221static void __devexit voodoo3_remove(struct pci_dev *dev)
222{
223 i2c_bit_del_bus(&voodoo3_i2c_adapter);
224 i2c_bit_del_bus(&voodoo3_ddc_adapter);
225 iounmap(ioaddr);
226}
227
228static struct pci_driver voodoo3_driver = {
229 .name = "voodoo3_smbus",
230 .id_table = voodoo3_ids,
231 .probe = voodoo3_probe,
232 .remove = __devexit_p(voodoo3_remove),
233};
234
235static int __init i2c_voodoo3_init(void)
236{
237 return pci_register_driver(&voodoo3_driver);
238}
239
240static void __exit i2c_voodoo3_exit(void)
241{
242 pci_unregister_driver(&voodoo3_driver);
243}
244
245
246MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, "
247 "Philip Edelbrock <phil@netroedge.com>, "
248 "Ralph Metzler <rjkm@thp.uni-koeln.de>, "
249 "and Mark D. Studebaker <mdsxyz123@yahoo.com>");
250MODULE_DESCRIPTION("Voodoo3 I2C/SMBus driver");
251MODULE_LICENSE("GPL");
252
253module_init(i2c_voodoo3_init);
254module_exit(i2c_voodoo3_exit);
diff --git a/drivers/i2c/busses/scx200_acb.c b/drivers/i2c/busses/scx200_acb.c
new file mode 100644
index 000000000000..1c4159a93623
--- /dev/null
+++ b/drivers/i2c/busses/scx200_acb.c
@@ -0,0 +1,557 @@
1/* linux/drivers/i2c/scx200_acb.c
2
3 Copyright (c) 2001,2002 Christer Weinigel <wingel@nano-system.com>
4
5 National Semiconductor SCx200 ACCESS.bus support
6
7 Based on i2c-keywest.c which is:
8 Copyright (c) 2001 Benjamin Herrenschmidt <benh@kernel.crashing.org>
9 Copyright (c) 2000 Philip Edelbrock <phil@stimpy.netroedge.com>
10
11 This program is free software; you can redistribute it and/or
12 modify it under the terms of the GNU General Public License as
13 published by the Free Software Foundation; either version 2 of the
14 License, or (at your option) any later version.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24
25*/
26
27#include <linux/config.h>
28#include <linux/module.h>
29#include <linux/errno.h>
30#include <linux/kernel.h>
31#include <linux/init.h>
32#include <linux/i2c.h>
33#include <linux/smp_lock.h>
34#include <linux/pci.h>
35#include <linux/delay.h>
36#include <asm/io.h>
37
38#include <linux/scx200.h>
39
40#define NAME "scx200_acb"
41
42MODULE_AUTHOR("Christer Weinigel <wingel@nano-system.com>");
43MODULE_DESCRIPTION("NatSemi SCx200 ACCESS.bus Driver");
44MODULE_LICENSE("GPL");
45
46#define MAX_DEVICES 4
47static int base[MAX_DEVICES] = { 0x820, 0x840 };
48module_param_array(base, int, NULL, 0);
49MODULE_PARM_DESC(base, "Base addresses for the ACCESS.bus controllers");
50
51#ifdef DEBUG
52#define DBG(x...) printk(KERN_DEBUG NAME ": " x)
53#else
54#define DBG(x...)
55#endif
56
57/* The hardware supports interrupt driven mode too, but I haven't
58 implemented that. */
59#define POLLED_MODE 1
60#define POLL_TIMEOUT (HZ)
61
62enum scx200_acb_state {
63 state_idle,
64 state_address,
65 state_command,
66 state_repeat_start,
67 state_quick,
68 state_read,
69 state_write,
70};
71
72static const char *scx200_acb_state_name[] = {
73 "idle",
74 "address",
75 "command",
76 "repeat_start",
77 "quick",
78 "read",
79 "write",
80};
81
82/* Physical interface */
83struct scx200_acb_iface
84{
85 struct scx200_acb_iface *next;
86 struct i2c_adapter adapter;
87 unsigned base;
88 struct semaphore sem;
89
90 /* State machine data */
91 enum scx200_acb_state state;
92 int result;
93 u8 address_byte;
94 u8 command;
95 u8 *ptr;
96 char needs_reset;
97 unsigned len;
98};
99
100/* Register Definitions */
101#define ACBSDA (iface->base + 0)
102#define ACBST (iface->base + 1)
103#define ACBST_SDAST 0x40 /* SDA Status */
104#define ACBST_BER 0x20
105#define ACBST_NEGACK 0x10 /* Negative Acknowledge */
106#define ACBST_STASTR 0x08 /* Stall After Start */
107#define ACBST_MASTER 0x02
108#define ACBCST (iface->base + 2)
109#define ACBCST_BB 0x02
110#define ACBCTL1 (iface->base + 3)
111#define ACBCTL1_STASTRE 0x80
112#define ACBCTL1_NMINTE 0x40
113#define ACBCTL1_ACK 0x10
114#define ACBCTL1_STOP 0x02
115#define ACBCTL1_START 0x01
116#define ACBADDR (iface->base + 4)
117#define ACBCTL2 (iface->base + 5)
118#define ACBCTL2_ENABLE 0x01
119
120/************************************************************************/
121
122static void scx200_acb_machine(struct scx200_acb_iface *iface, u8 status)
123{
124 const char *errmsg;
125
126 DBG("state %s, status = 0x%02x\n",
127 scx200_acb_state_name[iface->state], status);
128
129 if (status & ACBST_BER) {
130 errmsg = "bus error";
131 goto error;
132 }
133 if (!(status & ACBST_MASTER)) {
134 errmsg = "not master";
135 goto error;
136 }
137 if (status & ACBST_NEGACK)
138 goto negack;
139
140 switch (iface->state) {
141 case state_idle:
142 dev_warn(&iface->adapter.dev, "interrupt in idle state\n");
143 break;
144
145 case state_address:
146 /* Do a pointer write first */
147 outb(iface->address_byte & ~1, ACBSDA);
148
149 iface->state = state_command;
150 break;
151
152 case state_command:
153 outb(iface->command, ACBSDA);
154
155 if (iface->address_byte & 1)
156 iface->state = state_repeat_start;
157 else
158 iface->state = state_write;
159 break;
160
161 case state_repeat_start:
162 outb(inb(ACBCTL1) | ACBCTL1_START, ACBCTL1);
163 /* fallthrough */
164
165 case state_quick:
166 if (iface->address_byte & 1) {
167 if (iface->len == 1)
168 outb(inb(ACBCTL1) | ACBCTL1_ACK, ACBCTL1);
169 else
170 outb(inb(ACBCTL1) & ~ACBCTL1_ACK, ACBCTL1);
171 outb(iface->address_byte, ACBSDA);
172
173 iface->state = state_read;
174 } else {
175 outb(iface->address_byte, ACBSDA);
176
177 iface->state = state_write;
178 }
179 break;
180
181 case state_read:
182 /* Set ACK if receiving the last byte */
183 if (iface->len == 1)
184 outb(inb(ACBCTL1) | ACBCTL1_ACK, ACBCTL1);
185 else
186 outb(inb(ACBCTL1) & ~ACBCTL1_ACK, ACBCTL1);
187
188 *iface->ptr++ = inb(ACBSDA);
189 --iface->len;
190
191 if (iface->len == 0) {
192 iface->result = 0;
193 iface->state = state_idle;
194 outb(inb(ACBCTL1) | ACBCTL1_STOP, ACBCTL1);
195 }
196
197 break;
198
199 case state_write:
200 if (iface->len == 0) {
201 iface->result = 0;
202 iface->state = state_idle;
203 outb(inb(ACBCTL1) | ACBCTL1_STOP, ACBCTL1);
204 break;
205 }
206
207 outb(*iface->ptr++, ACBSDA);
208 --iface->len;
209
210 break;
211 }
212
213 return;
214
215 negack:
216 DBG("negative acknowledge in state %s\n",
217 scx200_acb_state_name[iface->state]);
218
219 iface->state = state_idle;
220 iface->result = -ENXIO;
221
222 outb(inb(ACBCTL1) | ACBCTL1_STOP, ACBCTL1);
223 outb(ACBST_STASTR | ACBST_NEGACK, ACBST);
224 return;
225
226 error:
227 dev_err(&iface->adapter.dev, "%s in state %s\n", errmsg,
228 scx200_acb_state_name[iface->state]);
229
230 iface->state = state_idle;
231 iface->result = -EIO;
232 iface->needs_reset = 1;
233}
234
235static void scx200_acb_timeout(struct scx200_acb_iface *iface)
236{
237 dev_err(&iface->adapter.dev, "timeout in state %s\n",
238 scx200_acb_state_name[iface->state]);
239
240 iface->state = state_idle;
241 iface->result = -EIO;
242 iface->needs_reset = 1;
243}
244
245#ifdef POLLED_MODE
246static void scx200_acb_poll(struct scx200_acb_iface *iface)
247{
248 u8 status = 0;
249 unsigned long timeout;
250
251 timeout = jiffies + POLL_TIMEOUT;
252 while (time_before(jiffies, timeout)) {
253 status = inb(ACBST);
254 if ((status & (ACBST_SDAST|ACBST_BER|ACBST_NEGACK)) != 0) {
255 scx200_acb_machine(iface, status);
256 return;
257 }
258 msleep(10);
259 }
260
261 scx200_acb_timeout(iface);
262}
263#endif /* POLLED_MODE */
264
265static void scx200_acb_reset(struct scx200_acb_iface *iface)
266{
267 /* Disable the ACCESS.bus device and Configure the SCL
268 frequency: 16 clock cycles */
269 outb(0x70, ACBCTL2);
270 /* Polling mode */
271 outb(0, ACBCTL1);
272 /* Disable slave address */
273 outb(0, ACBADDR);
274 /* Enable the ACCESS.bus device */
275 outb(inb(ACBCTL2) | ACBCTL2_ENABLE, ACBCTL2);
276 /* Free STALL after START */
277 outb(inb(ACBCTL1) & ~(ACBCTL1_STASTRE | ACBCTL1_NMINTE), ACBCTL1);
278 /* Send a STOP */
279 outb(inb(ACBCTL1) | ACBCTL1_STOP, ACBCTL1);
280 /* Clear BER, NEGACK and STASTR bits */
281 outb(ACBST_BER | ACBST_NEGACK | ACBST_STASTR, ACBST);
282 /* Clear BB bit */
283 outb(inb(ACBCST) | ACBCST_BB, ACBCST);
284}
285
286static s32 scx200_acb_smbus_xfer(struct i2c_adapter *adapter,
287 u16 address, unsigned short flags,
288 char rw, u8 command, int size,
289 union i2c_smbus_data *data)
290{
291 struct scx200_acb_iface *iface = i2c_get_adapdata(adapter);
292 int len;
293 u8 *buffer;
294 u16 cur_word;
295 int rc;
296
297 switch (size) {
298 case I2C_SMBUS_QUICK:
299 len = 0;
300 buffer = NULL;
301 break;
302 case I2C_SMBUS_BYTE:
303 if (rw == I2C_SMBUS_READ) {
304 len = 1;
305 buffer = &data->byte;
306 } else {
307 len = 1;
308 buffer = &command;
309 }
310 break;
311 case I2C_SMBUS_BYTE_DATA:
312 len = 1;
313 buffer = &data->byte;
314 break;
315 case I2C_SMBUS_WORD_DATA:
316 len = 2;
317 cur_word = cpu_to_le16(data->word);
318 buffer = (u8 *)&cur_word;
319 break;
320 case I2C_SMBUS_BLOCK_DATA:
321 len = data->block[0];
322 buffer = &data->block[1];
323 break;
324 default:
325 return -EINVAL;
326 }
327
328 DBG("size=%d, address=0x%x, command=0x%x, len=%d, read=%d\n",
329 size, address, command, len, rw == I2C_SMBUS_READ);
330
331 if (!len && rw == I2C_SMBUS_READ) {
332 dev_warn(&adapter->dev, "zero length read\n");
333 return -EINVAL;
334 }
335
336 if (len && !buffer) {
337 dev_warn(&adapter->dev, "nonzero length but no buffer\n");
338 return -EFAULT;
339 }
340
341 down(&iface->sem);
342
343 iface->address_byte = address<<1;
344 if (rw == I2C_SMBUS_READ)
345 iface->address_byte |= 1;
346 iface->command = command;
347 iface->ptr = buffer;
348 iface->len = len;
349 iface->result = -EINVAL;
350 iface->needs_reset = 0;
351
352 outb(inb(ACBCTL1) | ACBCTL1_START, ACBCTL1);
353
354 if (size == I2C_SMBUS_QUICK || size == I2C_SMBUS_BYTE)
355 iface->state = state_quick;
356 else
357 iface->state = state_address;
358
359#ifdef POLLED_MODE
360 while (iface->state != state_idle)
361 scx200_acb_poll(iface);
362#else /* POLLED_MODE */
363#error Interrupt driven mode not implemented
364#endif /* POLLED_MODE */
365
366 if (iface->needs_reset)
367 scx200_acb_reset(iface);
368
369 rc = iface->result;
370
371 up(&iface->sem);
372
373 if (rc == 0 && size == I2C_SMBUS_WORD_DATA && rw == I2C_SMBUS_READ)
374 data->word = le16_to_cpu(cur_word);
375
376#ifdef DEBUG
377 DBG(": transfer done, result: %d", rc);
378 if (buffer) {
379 int i;
380 printk(" data:");
381 for (i = 0; i < len; ++i)
382 printk(" %02x", buffer[i]);
383 }
384 printk("\n");
385#endif
386
387 return rc;
388}
389
390static u32 scx200_acb_func(struct i2c_adapter *adapter)
391{
392 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
393 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
394 I2C_FUNC_SMBUS_BLOCK_DATA;
395}
396
397/* For now, we only handle combined mode (smbus) */
398static struct i2c_algorithm scx200_acb_algorithm = {
399 .name = "NatSemi SCx200 ACCESS.bus",
400 .id = I2C_ALGO_SMBUS,
401 .smbus_xfer = scx200_acb_smbus_xfer,
402 .functionality = scx200_acb_func,
403};
404
405static struct scx200_acb_iface *scx200_acb_list;
406
407static int scx200_acb_probe(struct scx200_acb_iface *iface)
408{
409 u8 val;
410
411 /* Disable the ACCESS.bus device and Configure the SCL
412 frequency: 16 clock cycles */
413 outb(0x70, ACBCTL2);
414
415 if (inb(ACBCTL2) != 0x70) {
416 DBG("ACBCTL2 readback failed\n");
417 return -ENXIO;
418 }
419
420 outb(inb(ACBCTL1) | ACBCTL1_NMINTE, ACBCTL1);
421
422 val = inb(ACBCTL1);
423 if (val) {
424 DBG("disabled, but ACBCTL1=0x%02x\n", val);
425 return -ENXIO;
426 }
427
428 outb(inb(ACBCTL2) | ACBCTL2_ENABLE, ACBCTL2);
429
430 outb(inb(ACBCTL1) | ACBCTL1_NMINTE, ACBCTL1);
431
432 val = inb(ACBCTL1);
433 if ((val & ACBCTL1_NMINTE) != ACBCTL1_NMINTE) {
434 DBG("enabled, but NMINTE won't be set, ACBCTL1=0x%02x\n", val);
435 return -ENXIO;
436 }
437
438 return 0;
439}
440
441static int __init scx200_acb_create(int base, int index)
442{
443 struct scx200_acb_iface *iface;
444 struct i2c_adapter *adapter;
445 int rc = 0;
446 char description[64];
447
448 iface = kmalloc(sizeof(*iface), GFP_KERNEL);
449 if (!iface) {
450 printk(KERN_ERR NAME ": can't allocate memory\n");
451 rc = -ENOMEM;
452 goto errout;
453 }
454
455 memset(iface, 0, sizeof(*iface));
456 adapter = &iface->adapter;
457 i2c_set_adapdata(adapter, iface);
458 snprintf(adapter->name, I2C_NAME_SIZE, "SCx200 ACB%d", index);
459 adapter->owner = THIS_MODULE;
460 adapter->id = I2C_ALGO_SMBUS;
461 adapter->algo = &scx200_acb_algorithm;
462 adapter->class = I2C_CLASS_HWMON;
463
464 init_MUTEX(&iface->sem);
465
466 snprintf(description, sizeof(description), "NatSemi SCx200 ACCESS.bus [%s]", adapter->name);
467 if (request_region(base, 8, description) == 0) {
468 dev_err(&adapter->dev, "can't allocate io 0x%x-0x%x\n",
469 base, base + 8-1);
470 rc = -EBUSY;
471 goto errout;
472 }
473 iface->base = base;
474
475 rc = scx200_acb_probe(iface);
476 if (rc) {
477 dev_warn(&adapter->dev, "probe failed\n");
478 goto errout;
479 }
480
481 scx200_acb_reset(iface);
482
483 if (i2c_add_adapter(adapter) < 0) {
484 dev_err(&adapter->dev, "failed to register\n");
485 rc = -ENODEV;
486 goto errout;
487 }
488
489 lock_kernel();
490 iface->next = scx200_acb_list;
491 scx200_acb_list = iface;
492 unlock_kernel();
493
494 return 0;
495
496 errout:
497 if (iface) {
498 if (iface->base)
499 release_region(iface->base, 8);
500 kfree(iface);
501 }
502 return rc;
503}
504
505static struct pci_device_id scx200[] = {
506 { PCI_DEVICE(PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_SCx200_BRIDGE) },
507 { PCI_DEVICE(PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_SC1100_BRIDGE) },
508 { },
509};
510
511static int __init scx200_acb_init(void)
512{
513 int i;
514 int rc;
515
516 pr_debug(NAME ": NatSemi SCx200 ACCESS.bus Driver\n");
517
518 /* Verify that this really is a SCx200 processor */
519 if (pci_dev_present(scx200) == 0)
520 return -ENODEV;
521
522 rc = -ENXIO;
523 for (i = 0; i < MAX_DEVICES; ++i) {
524 if (base[i] > 0)
525 rc = scx200_acb_create(base[i], i);
526 }
527 if (scx200_acb_list)
528 return 0;
529 return rc;
530}
531
532static void __exit scx200_acb_cleanup(void)
533{
534 struct scx200_acb_iface *iface;
535 lock_kernel();
536 while ((iface = scx200_acb_list) != NULL) {
537 scx200_acb_list = iface->next;
538 unlock_kernel();
539
540 i2c_del_adapter(&iface->adapter);
541 release_region(iface->base, 8);
542 kfree(iface);
543 lock_kernel();
544 }
545 unlock_kernel();
546}
547
548module_init(scx200_acb_init);
549module_exit(scx200_acb_cleanup);
550
551/*
552 Local variables:
553 compile-command: "make -k -C ../.. SUBDIRS=drivers/i2c modules"
554 c-basic-offset: 8
555 End:
556*/
557
diff --git a/drivers/i2c/busses/scx200_i2c.c b/drivers/i2c/busses/scx200_i2c.c
new file mode 100644
index 000000000000..27fbfecc414f
--- /dev/null
+++ b/drivers/i2c/busses/scx200_i2c.c
@@ -0,0 +1,131 @@
1/* linux/drivers/i2c/scx200_i2c.c
2
3 Copyright (c) 2001,2002 Christer Weinigel <wingel@nano-system.com>
4
5 National Semiconductor SCx200 I2C bus on GPIO pins
6
7 Based on i2c-velleman.c Copyright (C) 1995-96, 2000 Simon G. Vogl
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22*/
23
24#include <linux/config.h>
25#include <linux/module.h>
26#include <linux/errno.h>
27#include <linux/kernel.h>
28#include <linux/init.h>
29#include <linux/i2c.h>
30#include <linux/i2c-algo-bit.h>
31#include <asm/io.h>
32
33#include <linux/scx200_gpio.h>
34
35#define NAME "scx200_i2c"
36
37MODULE_AUTHOR("Christer Weinigel <wingel@nano-system.com>");
38MODULE_DESCRIPTION("NatSemi SCx200 I2C Driver");
39MODULE_LICENSE("GPL");
40
41static int scl = CONFIG_SCx200_I2C_SCL;
42static int sda = CONFIG_SCx200_I2C_SDA;
43
44module_param(scl, int, 0);
45MODULE_PARM_DESC(scl, "GPIO line for SCL");
46module_param(sda, int, 0);
47MODULE_PARM_DESC(sda, "GPIO line for SDA");
48
49static void scx200_i2c_setscl(void *data, int state)
50{
51 scx200_gpio_set(scl, state);
52}
53
54static void scx200_i2c_setsda(void *data, int state)
55{
56 scx200_gpio_set(sda, state);
57}
58
59static int scx200_i2c_getscl(void *data)
60{
61 return scx200_gpio_get(scl);
62}
63
64static int scx200_i2c_getsda(void *data)
65{
66 return scx200_gpio_get(sda);
67}
68
69/* ------------------------------------------------------------------------
70 * Encapsulate the above functions in the correct operations structure.
71 * This is only done when more than one hardware adapter is supported.
72 */
73
74static struct i2c_algo_bit_data scx200_i2c_data = {
75 NULL,
76 scx200_i2c_setsda,
77 scx200_i2c_setscl,
78 scx200_i2c_getsda,
79 scx200_i2c_getscl,
80 10, 10, 100, /* waits, timeout */
81};
82
83static struct i2c_adapter scx200_i2c_ops = {
84 .owner = THIS_MODULE,
85 .algo_data = &scx200_i2c_data,
86 .name = "NatSemi SCx200 I2C",
87};
88
89static int scx200_i2c_init(void)
90{
91 pr_debug(NAME ": NatSemi SCx200 I2C Driver\n");
92
93 if (!scx200_gpio_present()) {
94 printk(KERN_ERR NAME ": no SCx200 gpio pins available\n");
95 return -ENODEV;
96 }
97
98 pr_debug(NAME ": SCL=GPIO%02u, SDA=GPIO%02u\n", scl, sda);
99
100 if (scl == -1 || sda == -1 || scl == sda) {
101 printk(KERN_ERR NAME ": scl and sda must be specified\n");
102 return -EINVAL;
103 }
104
105 /* Configure GPIOs as open collector outputs */
106 scx200_gpio_configure(scl, ~2, 5);
107 scx200_gpio_configure(sda, ~2, 5);
108
109 if (i2c_bit_add_bus(&scx200_i2c_ops) < 0) {
110 printk(KERN_ERR NAME ": adapter %s registration failed\n",
111 scx200_i2c_ops.name);
112 return -ENODEV;
113 }
114
115 return 0;
116}
117
118static void scx200_i2c_cleanup(void)
119{
120 i2c_bit_del_bus(&scx200_i2c_ops);
121}
122
123module_init(scx200_i2c_init);
124module_exit(scx200_i2c_cleanup);
125
126/*
127 Local variables:
128 compile-command: "make -k -C ../.. SUBDIRS=drivers/i2c modules"
129 c-basic-offset: 8
130 End:
131*/
diff --git a/drivers/i2c/chips/Kconfig b/drivers/i2c/chips/Kconfig
new file mode 100644
index 000000000000..74d23cfce2a3
--- /dev/null
+++ b/drivers/i2c/chips/Kconfig
@@ -0,0 +1,443 @@
1#
2# I2C Sensor device configuration
3#
4
5menu "Hardware Sensors Chip support"
6 depends on I2C
7
8config I2C_SENSOR
9 tristate
10 default n
11
12config SENSORS_ADM1021
13 tristate "Analog Devices ADM1021 and compatibles"
14 depends on I2C && EXPERIMENTAL
15 select I2C_SENSOR
16 help
17 If you say yes here you get support for Analog Devices ADM1021
18 and ADM1023 sensor chips and clones: Maxim MAX1617 and MAX1617A,
19 Genesys Logic GL523SM, National Semiconductor LM84, TI THMC10,
20 and the XEON processor built-in sensor.
21
22 This driver can also be built as a module. If so, the module
23 will be called adm1021.
24
25config SENSORS_ADM1025
26 tristate "Analog Devices ADM1025 and compatibles"
27 depends on I2C && EXPERIMENTAL
28 select I2C_SENSOR
29 help
30 If you say yes here you get support for Analog Devices ADM1025
31 and Philips NE1619 sensor chips.
32 This driver can also be built as a module. If so, the module
33 will be called adm1025.
34
35config SENSORS_ADM1026
36 tristate "Analog Devices ADM1026 and compatibles"
37 depends on I2C && EXPERIMENTAL
38 select I2C_SENSOR
39 help
40 If you say yes here you get support for Analog Devices ADM1026
41 This driver can also be built as a module. If so, the module
42 will be called adm1026.
43
44config SENSORS_ADM1031
45 tristate "Analog Devices ADM1031 and compatibles"
46 depends on I2C && EXPERIMENTAL
47 select I2C_SENSOR
48 help
49 If you say yes here you get support for Analog Devices ADM1031
50 and ADM1030 sensor chips.
51 This driver can also be built as a module. If so, the module
52 will be called adm1031.
53
54config SENSORS_ASB100
55 tristate "Asus ASB100 Bach"
56 depends on I2C && EXPERIMENTAL
57 select I2C_SENSOR
58 help
59 If you say yes here you get support for the ASB100 Bach sensor
60 chip found on some Asus mainboards.
61
62 This driver can also be built as a module. If so, the module
63 will be called asb100.
64
65config SENSORS_DS1621
66 tristate "Dallas Semiconductor DS1621 and DS1625"
67 depends on I2C && EXPERIMENTAL
68 select I2C_SENSOR
69 help
70 If you say yes here you get support for Dallas Semiconductor
71 DS1621 and DS1625 sensor chips.
72
73 This driver can also be built as a module. If so, the module
74 will be called ds1621.
75
76config SENSORS_FSCHER
77 tristate "FSC Hermes"
78 depends on I2C && EXPERIMENTAL
79 select I2C_SENSOR
80 help
81 If you say yes here you get support for Fujitsu Siemens
82 Computers Hermes sensor chips.
83
84 This driver can also be built as a module. If so, the module
85 will be called fscher.
86
87config SENSORS_FSCPOS
88 tristate "FSC Poseidon"
89 depends on I2C && EXPERIMENTAL
90 select I2C_SENSOR
91 help
92 If you say yes here you get support for Fujitsu Siemens
93 Computers Poseidon sensor chips.
94
95 This driver can also be built as a module. If so, the module
96 will be called fscpos.
97
98config SENSORS_GL518SM
99 tristate "Genesys Logic GL518SM"
100 depends on I2C && EXPERIMENTAL
101 select I2C_SENSOR
102 help
103 If you say yes here you get support for Genesys Logic GL518SM
104 sensor chips.
105
106 This driver can also be built as a module. If so, the module
107 will be called gl518sm.
108
109config SENSORS_GL520SM
110 tristate "Genesys Logic GL520SM"
111 depends on I2C && EXPERIMENTAL
112 select I2C_SENSOR
113 help
114 If you say yes here you get support for Genesys Logic GL520SM
115 sensor chips.
116
117 This driver can also be built as a module. If so, the module
118 will be called gl520sm.
119
120config SENSORS_IT87
121 tristate "ITE IT87xx and compatibles"
122 depends on I2C && EXPERIMENTAL
123 select I2C_SENSOR
124 help
125 If you say yes here you get support for ITE IT87xx sensor chips
126 and clones: SiS960.
127
128 This driver can also be built as a module. If so, the module
129 will be called it87.
130
131config SENSORS_LM63
132 tristate "National Semiconductor LM63"
133 depends on I2C && EXPERIMENTAL
134 select I2C_SENSOR
135 help
136 If you say yes here you get support for the National Semiconductor
137 LM63 remote diode digital temperature sensor with integrated fan
138 control. Such chips are found on the Tyan S4882 (Thunder K8QS Pro)
139 motherboard, among others.
140
141 This driver can also be built as a module. If so, the module
142 will be called lm63.
143
144config SENSORS_LM75
145 tristate "National Semiconductor LM75 and compatibles"
146 depends on I2C && EXPERIMENTAL
147 select I2C_SENSOR
148 help
149 If you say yes here you get support for National Semiconductor LM75
150 sensor chips and clones: Dallas Semiconductor DS75 and DS1775 (in
151 9-bit precision mode), and TelCom (now Microchip) TCN75.
152
153 The DS75 and DS1775 in 10- to 12-bit precision modes will require
154 a force module parameter. The driver will not handle the extra
155 precision anyhow.
156
157 This driver can also be built as a module. If so, the module
158 will be called lm75.
159
160config SENSORS_LM77
161 tristate "National Semiconductor LM77"
162 depends on I2C && EXPERIMENTAL
163 select I2C_SENSOR
164 help
165 If you say yes here you get support for National Semiconductor LM77
166 sensor chips.
167
168 This driver can also be built as a module. If so, the module
169 will be called lm77.
170
171config SENSORS_LM78
172 tristate "National Semiconductor LM78 and compatibles"
173 depends on I2C && EXPERIMENTAL
174 select I2C_SENSOR
175 help
176 If you say yes here you get support for National Semiconductor LM78,
177 LM78-J and LM79. This can also be built as a module which can be
178 inserted and removed while the kernel is running.
179
180 This driver can also be built as a module. If so, the module
181 will be called lm78.
182
183config SENSORS_LM80
184 tristate "National Semiconductor LM80"
185 depends on I2C && EXPERIMENTAL
186 select I2C_SENSOR
187 help
188 If you say yes here you get support for National Semiconductor
189 LM80 sensor chips.
190
191 This driver can also be built as a module. If so, the module
192 will be called lm80.
193
194config SENSORS_LM83
195 tristate "National Semiconductor LM83"
196 depends on I2C
197 select I2C_SENSOR
198 help
199 If you say yes here you get support for National Semiconductor
200 LM83 sensor chips.
201
202 This driver can also be built as a module. If so, the module
203 will be called lm83.
204
205config SENSORS_LM85
206 tristate "National Semiconductor LM85 and compatibles"
207 depends on I2C && EXPERIMENTAL
208 select I2C_SENSOR
209 help
210 If you say yes here you get support for National Semiconductor LM85
211 sensor chips and clones: ADT7463 and ADM1027.
212
213 This driver can also be built as a module. If so, the module
214 will be called lm85.
215
216config SENSORS_LM87
217 tristate "National Semiconductor LM87"
218 depends on I2C && EXPERIMENTAL
219 select I2C_SENSOR
220 help
221 If you say yes here you get support for National Semiconductor LM87
222 sensor chips.
223
224 This driver can also be built as a module. If so, the module
225 will be called lm87.
226
227config SENSORS_LM90
228 tristate "National Semiconductor LM90 and compatibles"
229 depends on I2C
230 select I2C_SENSOR
231 help
232 If you say yes here you get support for National Semiconductor LM90,
233 LM86, LM89 and LM99, Analog Devices ADM1032 and Maxim MAX6657 and
234 MAX6658 sensor chips.
235
236 The Analog Devices ADT7461 sensor chip is also supported, but only
237 if found in ADM1032 compatibility mode.
238
239 This driver can also be built as a module. If so, the module
240 will be called lm90.
241
242config SENSORS_LM92
243 tristate "National Semiconductor LM92 and compatibles"
244 depends on I2C && EXPERIMENTAL
245 select I2C_SENSOR
246 help
247 If you say yes here you get support for National Semiconductor LM92
248 and Maxim MAX6635 sensor chips.
249
250 This driver can also be built as a module. If so, the module
251 will be called lm92.
252
253config SENSORS_MAX1619
254 tristate "Maxim MAX1619 sensor chip"
255 depends on I2C && EXPERIMENTAL
256 select I2C_SENSOR
257 help
258 If you say yes here you get support for MAX1619 sensor chip.
259
260 This driver can also be built as a module. If so, the module
261 will be called max1619.
262
263config SENSORS_PC87360
264 tristate "National Semiconductor PC87360 family"
265 depends on I2C && EXPERIMENTAL
266 select I2C_SENSOR
267 select I2C_ISA
268 help
269 If you say yes here you get access to the hardware monitoring
270 functions of the National Semiconductor PC8736x Super-I/O chips.
271 The PC87360, PC87363 and PC87364 only have fan monitoring and
272 control. The PC87365 and PC87366 additionally have voltage and
273 temperature monitoring.
274
275 This driver can also be built as a module. If so, the module
276 will be called pc87360.
277
278config SENSORS_SMSC47B397
279 tristate "SMSC LPC47B397-NC"
280 depends on I2C && EXPERIMENTAL
281 select I2C_SENSOR
282 select I2C_ISA
283 help
284 If you say yes here you get support for the SMSC LPC47B397-NC
285 sensor chip.
286
287 This driver can also be built as a module. If so, the module
288 will be called smsc47b397.
289
290config SENSORS_SIS5595
291 tristate "Silicon Integrated Systems Corp. SiS5595"
292 depends on I2C && PCI && EXPERIMENTAL
293 select I2C_SENSOR
294 select I2C_ISA
295 help
296 If you say yes here you get support for the integrated sensors in
297 SiS5595 South Bridges.
298
299 This driver can also be built as a module. If so, the module
300 will be called sis5595.
301
302config SENSORS_SMSC47M1
303 tristate "SMSC LPC47M10x and compatibles"
304 depends on I2C && EXPERIMENTAL
305 select I2C_SENSOR
306 select I2C_ISA
307 help
308 If you say yes here you get support for the integrated fan
309 monitoring and control capabilities of the SMSC LPC47B27x,
310 LPC47M10x, LPC47M13x and LPC47M14x chips.
311
312 This driver can also be built as a module. If so, the module
313 will be called smsc47m1.
314
315config SENSORS_VIA686A
316 tristate "VIA686A"
317 depends on I2C && PCI && EXPERIMENTAL
318 select I2C_SENSOR
319 select I2C_ISA
320 help
321 If you say yes here you get support for the integrated sensors in
322 Via 686A/B South Bridges.
323
324 This driver can also be built as a module. If so, the module
325 will be called via686a.
326
327config SENSORS_W83781D
328 tristate "Winbond W83781D, W83782D, W83783S, W83627HF, Asus AS99127F"
329 depends on I2C && EXPERIMENTAL
330 select I2C_SENSOR
331 help
332 If you say yes here you get support for the Winbond W8378x series
333 of sensor chips: the W83781D, W83782D, W83783S and W83627HF,
334 and the similar Asus AS99127F.
335
336 This driver can also be built as a module. If so, the module
337 will be called w83781d.
338
339config SENSORS_W83L785TS
340 tristate "Winbond W83L785TS-S"
341 depends on I2C && EXPERIMENTAL
342 select I2C_SENSOR
343 help
344 If you say yes here you get support for the Winbond W83L785TS-S
345 sensor chip, which is used on the Asus A7N8X, among other
346 motherboards.
347
348 This driver can also be built as a module. If so, the module
349 will be called w83l785ts.
350
351config SENSORS_W83627HF
352 tristate "Winbond W83627HF, W83627THF, W83637HF, W83697HF"
353 depends on I2C && EXPERIMENTAL
354 select I2C_SENSOR
355 select I2C_ISA
356 help
357 If you say yes here you get support for the Winbond W836X7 series
358 of sensor chips: the W83627HF, W83627THF, W83637HF, and the W83697HF
359
360 This driver can also be built as a module. If so, the module
361 will be called w83627hf.
362
363endmenu
364
365menu "Other I2C Chip support"
366 depends on I2C
367
368config SENSORS_DS1337
369 tristate "Dallas Semiconductor DS1337 Real Time Clock"
370 depends on I2C && EXPERIMENTAL
371 select I2C_SENSOR
372 help
373 If you say yes here you get support for Dallas Semiconductor
374 DS1337 real-time clock chips.
375
376 This driver can also be built as a module. If so, the module
377 will be called ds1337.
378
379config SENSORS_EEPROM
380 tristate "EEPROM reader"
381 depends on I2C && EXPERIMENTAL
382 select I2C_SENSOR
383 help
384 If you say yes here you get read-only access to the EEPROM data
385 available on modern memory DIMMs and Sony Vaio laptops. Such
386 EEPROMs could theoretically be available on other devices as well.
387
388 This driver can also be built as a module. If so, the module
389 will be called eeprom.
390
391config SENSORS_PCF8574
392 tristate "Philips PCF8574 and PCF8574A"
393 depends on I2C && EXPERIMENTAL
394 select I2C_SENSOR
395 help
396 If you say yes here you get support for Philips PCF8574 and
397 PCF8574A chips.
398
399 This driver can also be built as a module. If so, the module
400 will be called pcf8574.
401
402config SENSORS_PCF8591
403 tristate "Philips PCF8591"
404 depends on I2C && EXPERIMENTAL
405 select I2C_SENSOR
406 help
407 If you say yes here you get support for Philips PCF8591 chips.
408
409 This driver can also be built as a module. If so, the module
410 will be called pcf8591.
411
412config SENSORS_RTC8564
413 tristate "Epson 8564 RTC chip"
414 depends on I2C && EXPERIMENTAL
415 select I2C_SENSOR
416 help
417 If you say yes here you get support for the Epson 8564 RTC chip.
418
419 This driver can also be built as a module. If so, the module
420 will be called i2c-rtc8564.
421
422config ISP1301_OMAP
423 tristate "Philips ISP1301 with OMAP OTG"
424 depends on I2C && ARCH_OMAP_OTG
425 help
426 If you say yes here you get support for the Philips ISP1301
427 USB-On-The-Go transceiver working with the OMAP OTG controller.
428 The ISP1301 is used in products including H2 and H3 development
429 boards for Texas Instruments OMAP processors.
430
431 This driver can also be built as a module. If so, the module
432 will be called isp1301_omap.
433
434config SENSORS_M41T00
435 tristate "ST M41T00 RTC chip"
436 depends on I2C && PPC32
437 help
438 If you say yes here you get support for the ST M41T00 RTC chip.
439
440 This driver can also be built as a module. If so, the module
441 will be called m41t00.
442
443endmenu
diff --git a/drivers/i2c/chips/Makefile b/drivers/i2c/chips/Makefile
new file mode 100644
index 000000000000..65599161a172
--- /dev/null
+++ b/drivers/i2c/chips/Makefile
@@ -0,0 +1,48 @@
1#
2# Makefile for the kernel hardware sensors chip drivers.
3#
4
5# asb100, then w83781d go first, as they can override other drivers' addresses.
6obj-$(CONFIG_SENSORS_ASB100) += asb100.o
7obj-$(CONFIG_SENSORS_W83627HF) += w83627hf.o
8obj-$(CONFIG_SENSORS_W83781D) += w83781d.o
9
10obj-$(CONFIG_SENSORS_ADM1021) += adm1021.o
11obj-$(CONFIG_SENSORS_ADM1025) += adm1025.o
12obj-$(CONFIG_SENSORS_ADM1026) += adm1026.o
13obj-$(CONFIG_SENSORS_ADM1031) += adm1031.o
14obj-$(CONFIG_SENSORS_DS1337) += ds1337.o
15obj-$(CONFIG_SENSORS_DS1621) += ds1621.o
16obj-$(CONFIG_SENSORS_EEPROM) += eeprom.o
17obj-$(CONFIG_SENSORS_FSCHER) += fscher.o
18obj-$(CONFIG_SENSORS_FSCPOS) += fscpos.o
19obj-$(CONFIG_SENSORS_GL518SM) += gl518sm.o
20obj-$(CONFIG_SENSORS_GL520SM) += gl520sm.o
21obj-$(CONFIG_SENSORS_IT87) += it87.o
22obj-$(CONFIG_SENSORS_LM63) += lm63.o
23obj-$(CONFIG_SENSORS_LM75) += lm75.o
24obj-$(CONFIG_SENSORS_LM77) += lm77.o
25obj-$(CONFIG_SENSORS_LM78) += lm78.o
26obj-$(CONFIG_SENSORS_LM80) += lm80.o
27obj-$(CONFIG_SENSORS_LM83) += lm83.o
28obj-$(CONFIG_SENSORS_LM85) += lm85.o
29obj-$(CONFIG_SENSORS_LM87) += lm87.o
30obj-$(CONFIG_SENSORS_LM90) += lm90.o
31obj-$(CONFIG_SENSORS_LM92) += lm92.o
32obj-$(CONFIG_SENSORS_MAX1619) += max1619.o
33obj-$(CONFIG_SENSORS_M41T00) += m41t00.o
34obj-$(CONFIG_SENSORS_PC87360) += pc87360.o
35obj-$(CONFIG_SENSORS_PCF8574) += pcf8574.o
36obj-$(CONFIG_SENSORS_PCF8591) += pcf8591.o
37obj-$(CONFIG_SENSORS_RTC8564) += rtc8564.o
38obj-$(CONFIG_SENSORS_SIS5595) += sis5595.o
39obj-$(CONFIG_SENSORS_SMSC47B397)+= smsc47b397.o
40obj-$(CONFIG_SENSORS_SMSC47M1) += smsc47m1.o
41obj-$(CONFIG_SENSORS_VIA686A) += via686a.o
42obj-$(CONFIG_SENSORS_W83L785TS) += w83l785ts.o
43obj-$(CONFIG_ISP1301_OMAP) += isp1301_omap.o
44
45ifeq ($(CONFIG_I2C_DEBUG_CHIP),y)
46EXTRA_CFLAGS += -DDEBUG
47endif
48
diff --git a/drivers/i2c/chips/adm1021.c b/drivers/i2c/chips/adm1021.c
new file mode 100644
index 000000000000..9c59a370b6d9
--- /dev/null
+++ b/drivers/i2c/chips/adm1021.c
@@ -0,0 +1,411 @@
1/*
2 adm1021.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring
4 Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl> and
5 Philip Edelbrock <phil@netroedge.com>
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/config.h>
23#include <linux/module.h>
24#include <linux/init.h>
25#include <linux/slab.h>
26#include <linux/jiffies.h>
27#include <linux/i2c.h>
28#include <linux/i2c-sensor.h>
29
30
31/* Addresses to scan */
32static unsigned short normal_i2c[] = { 0x18, 0x19, 0x1a,
33 0x29, 0x2a, 0x2b,
34 0x4c, 0x4d, 0x4e,
35 I2C_CLIENT_END };
36static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
37
38/* Insmod parameters */
39SENSORS_INSMOD_8(adm1021, adm1023, max1617, max1617a, thmc10, lm84, gl523sm, mc1066);
40
41/* adm1021 constants specified below */
42
43/* The adm1021 registers */
44/* Read-only */
45#define ADM1021_REG_TEMP 0x00
46#define ADM1021_REG_REMOTE_TEMP 0x01
47#define ADM1021_REG_STATUS 0x02
48#define ADM1021_REG_MAN_ID 0x0FE /* 0x41 = AMD, 0x49 = TI, 0x4D = Maxim, 0x23 = Genesys , 0x54 = Onsemi*/
49#define ADM1021_REG_DEV_ID 0x0FF /* ADM1021 = 0x0X, ADM1023 = 0x3X */
50#define ADM1021_REG_DIE_CODE 0x0FF /* MAX1617A */
51/* These use different addresses for reading/writing */
52#define ADM1021_REG_CONFIG_R 0x03
53#define ADM1021_REG_CONFIG_W 0x09
54#define ADM1021_REG_CONV_RATE_R 0x04
55#define ADM1021_REG_CONV_RATE_W 0x0A
56/* These are for the ADM1023's additional precision on the remote temp sensor */
57#define ADM1021_REG_REM_TEMP_PREC 0x010
58#define ADM1021_REG_REM_OFFSET 0x011
59#define ADM1021_REG_REM_OFFSET_PREC 0x012
60#define ADM1021_REG_REM_TOS_PREC 0x013
61#define ADM1021_REG_REM_THYST_PREC 0x014
62/* limits */
63#define ADM1021_REG_TOS_R 0x05
64#define ADM1021_REG_TOS_W 0x0B
65#define ADM1021_REG_REMOTE_TOS_R 0x07
66#define ADM1021_REG_REMOTE_TOS_W 0x0D
67#define ADM1021_REG_THYST_R 0x06
68#define ADM1021_REG_THYST_W 0x0C
69#define ADM1021_REG_REMOTE_THYST_R 0x08
70#define ADM1021_REG_REMOTE_THYST_W 0x0E
71/* write-only */
72#define ADM1021_REG_ONESHOT 0x0F
73
74
75/* Conversions. Rounding and limit checking is only done on the TO_REG
76 variants. Note that you should be a bit careful with which arguments
77 these macros are called: arguments may be evaluated more than once.
78 Fixing this is just not worth it. */
79/* Conversions note: 1021 uses normal integer signed-byte format*/
80#define TEMP_FROM_REG(val) (val > 127 ? (val-256)*1000 : val*1000)
81#define TEMP_TO_REG(val) (SENSORS_LIMIT((val < 0 ? (val/1000)+256 : val/1000),0,255))
82
83/* Initial values */
84
85/* Note: Even though I left the low and high limits named os and hyst,
86they don't quite work like a thermostat the way the LM75 does. I.e.,
87a lower temp than THYST actually triggers an alarm instead of
88clearing it. Weird, ey? --Phil */
89
90/* Each client has this additional data */
91struct adm1021_data {
92 struct i2c_client client;
93 enum chips type;
94
95 struct semaphore update_lock;
96 char valid; /* !=0 if following fields are valid */
97 unsigned long last_updated; /* In jiffies */
98
99 u8 temp_max; /* Register values */
100 u8 temp_hyst;
101 u8 temp_input;
102 u8 remote_temp_max;
103 u8 remote_temp_hyst;
104 u8 remote_temp_input;
105 u8 alarms;
106 /* special values for ADM1021 only */
107 u8 die_code;
108 /* Special values for ADM1023 only */
109 u8 remote_temp_prec;
110 u8 remote_temp_os_prec;
111 u8 remote_temp_hyst_prec;
112 u8 remote_temp_offset;
113 u8 remote_temp_offset_prec;
114};
115
116static int adm1021_attach_adapter(struct i2c_adapter *adapter);
117static int adm1021_detect(struct i2c_adapter *adapter, int address, int kind);
118static void adm1021_init_client(struct i2c_client *client);
119static int adm1021_detach_client(struct i2c_client *client);
120static int adm1021_read_value(struct i2c_client *client, u8 reg);
121static int adm1021_write_value(struct i2c_client *client, u8 reg,
122 u16 value);
123static struct adm1021_data *adm1021_update_device(struct device *dev);
124
125/* (amalysh) read only mode, otherwise any limit's writing confuse BIOS */
126static int read_only = 0;
127
128
129/* This is the driver that will be inserted */
130static struct i2c_driver adm1021_driver = {
131 .owner = THIS_MODULE,
132 .name = "adm1021",
133 .id = I2C_DRIVERID_ADM1021,
134 .flags = I2C_DF_NOTIFY,
135 .attach_adapter = adm1021_attach_adapter,
136 .detach_client = adm1021_detach_client,
137};
138
139#define show(value) \
140static ssize_t show_##value(struct device *dev, char *buf) \
141{ \
142 struct adm1021_data *data = adm1021_update_device(dev); \
143 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->value)); \
144}
145show(temp_max);
146show(temp_hyst);
147show(temp_input);
148show(remote_temp_max);
149show(remote_temp_hyst);
150show(remote_temp_input);
151
152#define show2(value) \
153static ssize_t show_##value(struct device *dev, char *buf) \
154{ \
155 struct adm1021_data *data = adm1021_update_device(dev); \
156 return sprintf(buf, "%d\n", data->value); \
157}
158show2(alarms);
159show2(die_code);
160
161#define set(value, reg) \
162static ssize_t set_##value(struct device *dev, const char *buf, size_t count) \
163{ \
164 struct i2c_client *client = to_i2c_client(dev); \
165 struct adm1021_data *data = i2c_get_clientdata(client); \
166 int temp = simple_strtoul(buf, NULL, 10); \
167 \
168 down(&data->update_lock); \
169 data->value = TEMP_TO_REG(temp); \
170 adm1021_write_value(client, reg, data->value); \
171 up(&data->update_lock); \
172 return count; \
173}
174set(temp_max, ADM1021_REG_TOS_W);
175set(temp_hyst, ADM1021_REG_THYST_W);
176set(remote_temp_max, ADM1021_REG_REMOTE_TOS_W);
177set(remote_temp_hyst, ADM1021_REG_REMOTE_THYST_W);
178
179static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max, set_temp_max);
180static DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp_hyst, set_temp_hyst);
181static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input, NULL);
182static DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_remote_temp_max, set_remote_temp_max);
183static DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_remote_temp_hyst, set_remote_temp_hyst);
184static DEVICE_ATTR(temp2_input, S_IRUGO, show_remote_temp_input, NULL);
185static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
186static DEVICE_ATTR(die_code, S_IRUGO, show_die_code, NULL);
187
188
189static int adm1021_attach_adapter(struct i2c_adapter *adapter)
190{
191 if (!(adapter->class & I2C_CLASS_HWMON))
192 return 0;
193 return i2c_detect(adapter, &addr_data, adm1021_detect);
194}
195
196static int adm1021_detect(struct i2c_adapter *adapter, int address, int kind)
197{
198 int i;
199 struct i2c_client *new_client;
200 struct adm1021_data *data;
201 int err = 0;
202 const char *type_name = "";
203
204 /* Make sure we aren't probing the ISA bus!! This is just a safety check
205 at this moment; i2c_detect really won't call us. */
206#ifdef DEBUG
207 if (i2c_is_isa_adapter(adapter)) {
208 dev_dbg(&adapter->dev, "adm1021_detect called for an ISA bus adapter?!?\n");
209 return 0;
210 }
211#endif
212
213 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
214 goto error0;
215
216 /* OK. For now, we presume we have a valid client. We now create the
217 client structure, even though we cannot fill it completely yet.
218 But it allows us to access adm1021_{read,write}_value. */
219
220 if (!(data = kmalloc(sizeof(struct adm1021_data), GFP_KERNEL))) {
221 err = -ENOMEM;
222 goto error0;
223 }
224 memset(data, 0, sizeof(struct adm1021_data));
225
226 new_client = &data->client;
227 i2c_set_clientdata(new_client, data);
228 new_client->addr = address;
229 new_client->adapter = adapter;
230 new_client->driver = &adm1021_driver;
231 new_client->flags = 0;
232
233 /* Now, we do the remaining detection. */
234 if (kind < 0) {
235 if ((adm1021_read_value(new_client, ADM1021_REG_STATUS) & 0x03) != 0x00
236 || (adm1021_read_value(new_client, ADM1021_REG_CONFIG_R) & 0x3F) != 0x00
237 || (adm1021_read_value(new_client, ADM1021_REG_CONV_RATE_R) & 0xF8) != 0x00) {
238 err = -ENODEV;
239 goto error1;
240 }
241 }
242
243 /* Determine the chip type. */
244 if (kind <= 0) {
245 i = adm1021_read_value(new_client, ADM1021_REG_MAN_ID);
246 if (i == 0x41)
247 if ((adm1021_read_value(new_client, ADM1021_REG_DEV_ID) & 0x0F0) == 0x030)
248 kind = adm1023;
249 else
250 kind = adm1021;
251 else if (i == 0x49)
252 kind = thmc10;
253 else if (i == 0x23)
254 kind = gl523sm;
255 else if ((i == 0x4d) &&
256 (adm1021_read_value(new_client, ADM1021_REG_DEV_ID) == 0x01))
257 kind = max1617a;
258 else if (i == 0x54)
259 kind = mc1066;
260 /* LM84 Mfr ID in a different place, and it has more unused bits */
261 else if (adm1021_read_value(new_client, ADM1021_REG_CONV_RATE_R) == 0x00
262 && (kind == 0 /* skip extra detection */
263 || ((adm1021_read_value(new_client, ADM1021_REG_CONFIG_R) & 0x7F) == 0x00
264 && (adm1021_read_value(new_client, ADM1021_REG_STATUS) & 0xAB) == 0x00)))
265 kind = lm84;
266 else
267 kind = max1617;
268 }
269
270 if (kind == max1617) {
271 type_name = "max1617";
272 } else if (kind == max1617a) {
273 type_name = "max1617a";
274 } else if (kind == adm1021) {
275 type_name = "adm1021";
276 } else if (kind == adm1023) {
277 type_name = "adm1023";
278 } else if (kind == thmc10) {
279 type_name = "thmc10";
280 } else if (kind == lm84) {
281 type_name = "lm84";
282 } else if (kind == gl523sm) {
283 type_name = "gl523sm";
284 } else if (kind == mc1066) {
285 type_name = "mc1066";
286 }
287
288 /* Fill in the remaining client fields and put it into the global list */
289 strlcpy(new_client->name, type_name, I2C_NAME_SIZE);
290 data->type = kind;
291 data->valid = 0;
292 init_MUTEX(&data->update_lock);
293
294 /* Tell the I2C layer a new client has arrived */
295 if ((err = i2c_attach_client(new_client)))
296 goto error1;
297
298 /* Initialize the ADM1021 chip */
299 if (kind != lm84)
300 adm1021_init_client(new_client);
301
302 /* Register sysfs hooks */
303 device_create_file(&new_client->dev, &dev_attr_temp1_max);
304 device_create_file(&new_client->dev, &dev_attr_temp1_min);
305 device_create_file(&new_client->dev, &dev_attr_temp1_input);
306 device_create_file(&new_client->dev, &dev_attr_temp2_max);
307 device_create_file(&new_client->dev, &dev_attr_temp2_min);
308 device_create_file(&new_client->dev, &dev_attr_temp2_input);
309 device_create_file(&new_client->dev, &dev_attr_alarms);
310 if (data->type == adm1021)
311 device_create_file(&new_client->dev, &dev_attr_die_code);
312
313 return 0;
314
315error1:
316 kfree(data);
317error0:
318 return err;
319}
320
321static void adm1021_init_client(struct i2c_client *client)
322{
323 /* Enable ADC and disable suspend mode */
324 adm1021_write_value(client, ADM1021_REG_CONFIG_W,
325 adm1021_read_value(client, ADM1021_REG_CONFIG_R) & 0xBF);
326 /* Set Conversion rate to 1/sec (this can be tinkered with) */
327 adm1021_write_value(client, ADM1021_REG_CONV_RATE_W, 0x04);
328}
329
330static int adm1021_detach_client(struct i2c_client *client)
331{
332 int err;
333
334 if ((err = i2c_detach_client(client))) {
335 dev_err(&client->dev, "Client deregistration failed, client not detached.\n");
336 return err;
337 }
338
339 kfree(i2c_get_clientdata(client));
340 return 0;
341}
342
343/* All registers are byte-sized */
344static int adm1021_read_value(struct i2c_client *client, u8 reg)
345{
346 return i2c_smbus_read_byte_data(client, reg);
347}
348
349static int adm1021_write_value(struct i2c_client *client, u8 reg, u16 value)
350{
351 if (!read_only)
352 return i2c_smbus_write_byte_data(client, reg, value);
353 return 0;
354}
355
356static struct adm1021_data *adm1021_update_device(struct device *dev)
357{
358 struct i2c_client *client = to_i2c_client(dev);
359 struct adm1021_data *data = i2c_get_clientdata(client);
360
361 down(&data->update_lock);
362
363 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
364 || !data->valid) {
365 dev_dbg(&client->dev, "Starting adm1021 update\n");
366
367 data->temp_input = adm1021_read_value(client, ADM1021_REG_TEMP);
368 data->temp_max = adm1021_read_value(client, ADM1021_REG_TOS_R);
369 data->temp_hyst = adm1021_read_value(client, ADM1021_REG_THYST_R);
370 data->remote_temp_input = adm1021_read_value(client, ADM1021_REG_REMOTE_TEMP);
371 data->remote_temp_max = adm1021_read_value(client, ADM1021_REG_REMOTE_TOS_R);
372 data->remote_temp_hyst = adm1021_read_value(client, ADM1021_REG_REMOTE_THYST_R);
373 data->alarms = adm1021_read_value(client, ADM1021_REG_STATUS) & 0x7c;
374 if (data->type == adm1021)
375 data->die_code = adm1021_read_value(client, ADM1021_REG_DIE_CODE);
376 if (data->type == adm1023) {
377 data->remote_temp_prec = adm1021_read_value(client, ADM1021_REG_REM_TEMP_PREC);
378 data->remote_temp_os_prec = adm1021_read_value(client, ADM1021_REG_REM_TOS_PREC);
379 data->remote_temp_hyst_prec = adm1021_read_value(client, ADM1021_REG_REM_THYST_PREC);
380 data->remote_temp_offset = adm1021_read_value(client, ADM1021_REG_REM_OFFSET);
381 data->remote_temp_offset_prec = adm1021_read_value(client, ADM1021_REG_REM_OFFSET_PREC);
382 }
383 data->last_updated = jiffies;
384 data->valid = 1;
385 }
386
387 up(&data->update_lock);
388
389 return data;
390}
391
392static int __init sensors_adm1021_init(void)
393{
394 return i2c_add_driver(&adm1021_driver);
395}
396
397static void __exit sensors_adm1021_exit(void)
398{
399 i2c_del_driver(&adm1021_driver);
400}
401
402MODULE_AUTHOR ("Frodo Looijaard <frodol@dds.nl> and "
403 "Philip Edelbrock <phil@netroedge.com>");
404MODULE_DESCRIPTION("adm1021 driver");
405MODULE_LICENSE("GPL");
406
407module_param(read_only, bool, 0);
408MODULE_PARM_DESC(read_only, "Don't set any values, read only mode");
409
410module_init(sensors_adm1021_init)
411module_exit(sensors_adm1021_exit)
diff --git a/drivers/i2c/chips/adm1025.c b/drivers/i2c/chips/adm1025.c
new file mode 100644
index 000000000000..e0771a3d05c9
--- /dev/null
+++ b/drivers/i2c/chips/adm1025.c
@@ -0,0 +1,574 @@
1/*
2 * adm1025.c
3 *
4 * Copyright (C) 2000 Chen-Yuan Wu <gwu@esoft.com>
5 * Copyright (C) 2003-2004 Jean Delvare <khali@linux-fr.org>
6 *
7 * The ADM1025 is a sensor chip made by Analog Devices. It reports up to 6
8 * voltages (including its own power source) and up to two temperatures
9 * (its own plus up to one external one). Voltages are scaled internally
10 * (which is not the common way) with ratios such that the nominal value
11 * of each voltage correspond to a register value of 192 (which means a
12 * resolution of about 0.5% of the nominal value). Temperature values are
13 * reported with a 1 deg resolution and a 3 deg accuracy. Complete
14 * datasheet can be obtained from Analog's website at:
15 * http://www.analog.com/Analog_Root/productPage/productHome/0,2121,ADM1025,00.html
16 *
17 * This driver also supports the ADM1025A, which differs from the ADM1025
18 * only in that it has "open-drain VID inputs while the ADM1025 has
19 * on-chip 100k pull-ups on the VID inputs". It doesn't make any
20 * difference for us.
21 *
22 * This driver also supports the NE1619, a sensor chip made by Philips.
23 * That chip is similar to the ADM1025A, with a few differences. The only
24 * difference that matters to us is that the NE1619 has only two possible
25 * addresses while the ADM1025A has a third one. Complete datasheet can be
26 * obtained from Philips's website at:
27 * http://www.semiconductors.philips.com/pip/NE1619DS.html
28 *
29 * Since the ADM1025 was the first chipset supported by this driver, most
30 * comments will refer to this chipset, but are actually general and
31 * concern all supported chipsets, unless mentioned otherwise.
32 *
33 * This program is free software; you can redistribute it and/or modify
34 * it under the terms of the GNU General Public License as published by
35 * the Free Software Foundation; either version 2 of the License, or
36 * (at your option) any later version.
37 *
38 * This program is distributed in the hope that it will be useful,
39 * but WITHOUT ANY WARRANTY; without even the implied warranty of
40 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
41 * GNU General Public License for more details.
42 *
43 * You should have received a copy of the GNU General Public License
44 * along with this program; if not, write to the Free Software
45 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
46 */
47
48#include <linux/config.h>
49#include <linux/module.h>
50#include <linux/init.h>
51#include <linux/slab.h>
52#include <linux/jiffies.h>
53#include <linux/i2c.h>
54#include <linux/i2c-sensor.h>
55#include <linux/i2c-vid.h>
56
57/*
58 * Addresses to scan
59 * ADM1025 and ADM1025A have three possible addresses: 0x2c, 0x2d and 0x2e.
60 * NE1619 has two possible addresses: 0x2c and 0x2d.
61 */
62
63static unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
64static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
65
66/*
67 * Insmod parameters
68 */
69
70SENSORS_INSMOD_2(adm1025, ne1619);
71
72/*
73 * The ADM1025 registers
74 */
75
76#define ADM1025_REG_MAN_ID 0x3E
77#define ADM1025_REG_CHIP_ID 0x3F
78#define ADM1025_REG_CONFIG 0x40
79#define ADM1025_REG_STATUS1 0x41
80#define ADM1025_REG_STATUS2 0x42
81#define ADM1025_REG_IN(nr) (0x20 + (nr))
82#define ADM1025_REG_IN_MAX(nr) (0x2B + (nr) * 2)
83#define ADM1025_REG_IN_MIN(nr) (0x2C + (nr) * 2)
84#define ADM1025_REG_TEMP(nr) (0x26 + (nr))
85#define ADM1025_REG_TEMP_HIGH(nr) (0x37 + (nr) * 2)
86#define ADM1025_REG_TEMP_LOW(nr) (0x38 + (nr) * 2)
87#define ADM1025_REG_VID 0x47
88#define ADM1025_REG_VID4 0x49
89
90/*
91 * Conversions and various macros
92 * The ADM1025 uses signed 8-bit values for temperatures.
93 */
94
95static int in_scale[6] = { 2500, 2250, 3300, 5000, 12000, 3300 };
96
97#define IN_FROM_REG(reg,scale) (((reg) * (scale) + 96) / 192)
98#define IN_TO_REG(val,scale) ((val) <= 0 ? 0 : \
99 (val) * 192 >= (scale) * 255 ? 255 : \
100 ((val) * 192 + (scale)/2) / (scale))
101
102#define TEMP_FROM_REG(reg) ((reg) * 1000)
103#define TEMP_TO_REG(val) ((val) <= -127500 ? -128 : \
104 (val) >= 126500 ? 127 : \
105 (((val) < 0 ? (val)-500 : (val)+500) / 1000))
106
107/*
108 * Functions declaration
109 */
110
111static int adm1025_attach_adapter(struct i2c_adapter *adapter);
112static int adm1025_detect(struct i2c_adapter *adapter, int address, int kind);
113static void adm1025_init_client(struct i2c_client *client);
114static int adm1025_detach_client(struct i2c_client *client);
115static struct adm1025_data *adm1025_update_device(struct device *dev);
116
117/*
118 * Driver data (common to all clients)
119 */
120
121static struct i2c_driver adm1025_driver = {
122 .owner = THIS_MODULE,
123 .name = "adm1025",
124 .id = I2C_DRIVERID_ADM1025,
125 .flags = I2C_DF_NOTIFY,
126 .attach_adapter = adm1025_attach_adapter,
127 .detach_client = adm1025_detach_client,
128};
129
130/*
131 * Client data (each client gets its own)
132 */
133
134struct adm1025_data {
135 struct i2c_client client;
136 struct semaphore update_lock;
137 char valid; /* zero until following fields are valid */
138 unsigned long last_updated; /* in jiffies */
139
140 u8 in[6]; /* register value */
141 u8 in_max[6]; /* register value */
142 u8 in_min[6]; /* register value */
143 s8 temp[2]; /* register value */
144 s8 temp_min[2]; /* register value */
145 s8 temp_max[2]; /* register value */
146 u16 alarms; /* register values, combined */
147 u8 vid; /* register values, combined */
148 u8 vrm;
149};
150
151/*
152 * Sysfs stuff
153 */
154
155#define show_in(offset) \
156static ssize_t show_in##offset(struct device *dev, char *buf) \
157{ \
158 struct adm1025_data *data = adm1025_update_device(dev); \
159 return sprintf(buf, "%u\n", IN_FROM_REG(data->in[offset], \
160 in_scale[offset])); \
161} \
162static ssize_t show_in##offset##_min(struct device *dev, char *buf) \
163{ \
164 struct adm1025_data *data = adm1025_update_device(dev); \
165 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[offset], \
166 in_scale[offset])); \
167} \
168static ssize_t show_in##offset##_max(struct device *dev, char *buf) \
169{ \
170 struct adm1025_data *data = adm1025_update_device(dev); \
171 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[offset], \
172 in_scale[offset])); \
173} \
174static DEVICE_ATTR(in##offset##_input, S_IRUGO, show_in##offset, NULL);
175show_in(0);
176show_in(1);
177show_in(2);
178show_in(3);
179show_in(4);
180show_in(5);
181
182#define show_temp(offset) \
183static ssize_t show_temp##offset(struct device *dev, char *buf) \
184{ \
185 struct adm1025_data *data = adm1025_update_device(dev); \
186 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[offset-1])); \
187} \
188static ssize_t show_temp##offset##_min(struct device *dev, char *buf) \
189{ \
190 struct adm1025_data *data = adm1025_update_device(dev); \
191 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[offset-1])); \
192} \
193static ssize_t show_temp##offset##_max(struct device *dev, char *buf) \
194{ \
195 struct adm1025_data *data = adm1025_update_device(dev); \
196 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[offset-1])); \
197}\
198static DEVICE_ATTR(temp##offset##_input, S_IRUGO, show_temp##offset, NULL);
199show_temp(1);
200show_temp(2);
201
202#define set_in(offset) \
203static ssize_t set_in##offset##_min(struct device *dev, const char *buf, \
204 size_t count) \
205{ \
206 struct i2c_client *client = to_i2c_client(dev); \
207 struct adm1025_data *data = i2c_get_clientdata(client); \
208 long val = simple_strtol(buf, NULL, 10); \
209 \
210 down(&data->update_lock); \
211 data->in_min[offset] = IN_TO_REG(val, in_scale[offset]); \
212 i2c_smbus_write_byte_data(client, ADM1025_REG_IN_MIN(offset), \
213 data->in_min[offset]); \
214 up(&data->update_lock); \
215 return count; \
216} \
217static ssize_t set_in##offset##_max(struct device *dev, const char *buf, \
218 size_t count) \
219{ \
220 struct i2c_client *client = to_i2c_client(dev); \
221 struct adm1025_data *data = i2c_get_clientdata(client); \
222 long val = simple_strtol(buf, NULL, 10); \
223 \
224 down(&data->update_lock); \
225 data->in_max[offset] = IN_TO_REG(val, in_scale[offset]); \
226 i2c_smbus_write_byte_data(client, ADM1025_REG_IN_MAX(offset), \
227 data->in_max[offset]); \
228 up(&data->update_lock); \
229 return count; \
230} \
231static DEVICE_ATTR(in##offset##_min, S_IWUSR | S_IRUGO, \
232 show_in##offset##_min, set_in##offset##_min); \
233static DEVICE_ATTR(in##offset##_max, S_IWUSR | S_IRUGO, \
234 show_in##offset##_max, set_in##offset##_max);
235set_in(0);
236set_in(1);
237set_in(2);
238set_in(3);
239set_in(4);
240set_in(5);
241
242#define set_temp(offset) \
243static ssize_t set_temp##offset##_min(struct device *dev, const char *buf, \
244 size_t count) \
245{ \
246 struct i2c_client *client = to_i2c_client(dev); \
247 struct adm1025_data *data = i2c_get_clientdata(client); \
248 long val = simple_strtol(buf, NULL, 10); \
249 \
250 down(&data->update_lock); \
251 data->temp_min[offset-1] = TEMP_TO_REG(val); \
252 i2c_smbus_write_byte_data(client, ADM1025_REG_TEMP_LOW(offset-1), \
253 data->temp_min[offset-1]); \
254 up(&data->update_lock); \
255 return count; \
256} \
257static ssize_t set_temp##offset##_max(struct device *dev, const char *buf, \
258 size_t count) \
259{ \
260 struct i2c_client *client = to_i2c_client(dev); \
261 struct adm1025_data *data = i2c_get_clientdata(client); \
262 long val = simple_strtol(buf, NULL, 10); \
263 \
264 down(&data->update_lock); \
265 data->temp_max[offset-1] = TEMP_TO_REG(val); \
266 i2c_smbus_write_byte_data(client, ADM1025_REG_TEMP_HIGH(offset-1), \
267 data->temp_max[offset-1]); \
268 up(&data->update_lock); \
269 return count; \
270} \
271static DEVICE_ATTR(temp##offset##_min, S_IWUSR | S_IRUGO, \
272 show_temp##offset##_min, set_temp##offset##_min); \
273static DEVICE_ATTR(temp##offset##_max, S_IWUSR | S_IRUGO, \
274 show_temp##offset##_max, set_temp##offset##_max);
275set_temp(1);
276set_temp(2);
277
278static ssize_t show_alarms(struct device *dev, char *buf)
279{
280 struct adm1025_data *data = adm1025_update_device(dev);
281 return sprintf(buf, "%u\n", data->alarms);
282}
283static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
284
285static ssize_t show_vid(struct device *dev, char *buf)
286{
287 struct adm1025_data *data = adm1025_update_device(dev);
288 return sprintf(buf, "%u\n", vid_from_reg(data->vid, data->vrm));
289}
290static DEVICE_ATTR(in1_ref, S_IRUGO, show_vid, NULL);
291
292static ssize_t show_vrm(struct device *dev, char *buf)
293{
294 struct adm1025_data *data = adm1025_update_device(dev);
295 return sprintf(buf, "%u\n", data->vrm);
296}
297static ssize_t set_vrm(struct device *dev, const char *buf, size_t count)
298{
299 struct i2c_client *client = to_i2c_client(dev);
300 struct adm1025_data *data = i2c_get_clientdata(client);
301 data->vrm = simple_strtoul(buf, NULL, 10);
302 return count;
303}
304static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm);
305
306/*
307 * Real code
308 */
309
310static int adm1025_attach_adapter(struct i2c_adapter *adapter)
311{
312 if (!(adapter->class & I2C_CLASS_HWMON))
313 return 0;
314 return i2c_detect(adapter, &addr_data, adm1025_detect);
315}
316
317/*
318 * The following function does more than just detection. If detection
319 * succeeds, it also registers the new chip.
320 */
321static int adm1025_detect(struct i2c_adapter *adapter, int address, int kind)
322{
323 struct i2c_client *new_client;
324 struct adm1025_data *data;
325 int err = 0;
326 const char *name = "";
327 u8 config;
328
329 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
330 goto exit;
331
332 if (!(data = kmalloc(sizeof(struct adm1025_data), GFP_KERNEL))) {
333 err = -ENOMEM;
334 goto exit;
335 }
336 memset(data, 0, sizeof(struct adm1025_data));
337
338 /* The common I2C client data is placed right before the
339 ADM1025-specific data. */
340 new_client = &data->client;
341 i2c_set_clientdata(new_client, data);
342 new_client->addr = address;
343 new_client->adapter = adapter;
344 new_client->driver = &adm1025_driver;
345 new_client->flags = 0;
346
347 /*
348 * Now we do the remaining detection. A negative kind means that
349 * the driver was loaded with no force parameter (default), so we
350 * must both detect and identify the chip. A zero kind means that
351 * the driver was loaded with the force parameter, the detection
352 * step shall be skipped. A positive kind means that the driver
353 * was loaded with the force parameter and a given kind of chip is
354 * requested, so both the detection and the identification steps
355 * are skipped.
356 */
357 config = i2c_smbus_read_byte_data(new_client, ADM1025_REG_CONFIG);
358 if (kind < 0) { /* detection */
359 if ((config & 0x80) != 0x00
360 || (i2c_smbus_read_byte_data(new_client,
361 ADM1025_REG_STATUS1) & 0xC0) != 0x00
362 || (i2c_smbus_read_byte_data(new_client,
363 ADM1025_REG_STATUS2) & 0xBC) != 0x00) {
364 dev_dbg(&adapter->dev,
365 "ADM1025 detection failed at 0x%02x.\n",
366 address);
367 goto exit_free;
368 }
369 }
370
371 if (kind <= 0) { /* identification */
372 u8 man_id, chip_id;
373
374 man_id = i2c_smbus_read_byte_data(new_client,
375 ADM1025_REG_MAN_ID);
376 chip_id = i2c_smbus_read_byte_data(new_client,
377 ADM1025_REG_CHIP_ID);
378
379 if (man_id == 0x41) { /* Analog Devices */
380 if ((chip_id & 0xF0) == 0x20) { /* ADM1025/ADM1025A */
381 kind = adm1025;
382 }
383 } else
384 if (man_id == 0xA1) { /* Philips */
385 if (address != 0x2E
386 && (chip_id & 0xF0) == 0x20) { /* NE1619 */
387 kind = ne1619;
388 }
389 }
390
391 if (kind <= 0) { /* identification failed */
392 dev_info(&adapter->dev,
393 "Unsupported chip (man_id=0x%02X, "
394 "chip_id=0x%02X).\n", man_id, chip_id);
395 goto exit_free;
396 }
397 }
398
399 if (kind == adm1025) {
400 name = "adm1025";
401 } else if (kind == ne1619) {
402 name = "ne1619";
403 }
404
405 /* We can fill in the remaining client fields */
406 strlcpy(new_client->name, name, I2C_NAME_SIZE);
407 data->valid = 0;
408 init_MUTEX(&data->update_lock);
409
410 /* Tell the I2C layer a new client has arrived */
411 if ((err = i2c_attach_client(new_client)))
412 goto exit_free;
413
414 /* Initialize the ADM1025 chip */
415 adm1025_init_client(new_client);
416
417 /* Register sysfs hooks */
418 device_create_file(&new_client->dev, &dev_attr_in0_input);
419 device_create_file(&new_client->dev, &dev_attr_in1_input);
420 device_create_file(&new_client->dev, &dev_attr_in2_input);
421 device_create_file(&new_client->dev, &dev_attr_in3_input);
422 device_create_file(&new_client->dev, &dev_attr_in5_input);
423 device_create_file(&new_client->dev, &dev_attr_in0_min);
424 device_create_file(&new_client->dev, &dev_attr_in1_min);
425 device_create_file(&new_client->dev, &dev_attr_in2_min);
426 device_create_file(&new_client->dev, &dev_attr_in3_min);
427 device_create_file(&new_client->dev, &dev_attr_in5_min);
428 device_create_file(&new_client->dev, &dev_attr_in0_max);
429 device_create_file(&new_client->dev, &dev_attr_in1_max);
430 device_create_file(&new_client->dev, &dev_attr_in2_max);
431 device_create_file(&new_client->dev, &dev_attr_in3_max);
432 device_create_file(&new_client->dev, &dev_attr_in5_max);
433 device_create_file(&new_client->dev, &dev_attr_temp1_input);
434 device_create_file(&new_client->dev, &dev_attr_temp2_input);
435 device_create_file(&new_client->dev, &dev_attr_temp1_min);
436 device_create_file(&new_client->dev, &dev_attr_temp2_min);
437 device_create_file(&new_client->dev, &dev_attr_temp1_max);
438 device_create_file(&new_client->dev, &dev_attr_temp2_max);
439 device_create_file(&new_client->dev, &dev_attr_alarms);
440 device_create_file(&new_client->dev, &dev_attr_in1_ref);
441 device_create_file(&new_client->dev, &dev_attr_vrm);
442
443 /* Pin 11 is either in4 (+12V) or VID4 */
444 if (!(config & 0x20)) {
445 device_create_file(&new_client->dev, &dev_attr_in4_input);
446 device_create_file(&new_client->dev, &dev_attr_in4_min);
447 device_create_file(&new_client->dev, &dev_attr_in4_max);
448 }
449
450 return 0;
451
452exit_free:
453 kfree(data);
454exit:
455 return err;
456}
457
458static void adm1025_init_client(struct i2c_client *client)
459{
460 u8 reg;
461 struct adm1025_data *data = i2c_get_clientdata(client);
462 int i;
463
464 data->vrm = i2c_which_vrm();
465
466 /*
467 * Set high limits
468 * Usually we avoid setting limits on driver init, but it happens
469 * that the ADM1025 comes with stupid default limits (all registers
470 * set to 0). In case the chip has not gone through any limit
471 * setting yet, we better set the high limits to the max so that
472 * no alarm triggers.
473 */
474 for (i=0; i<6; i++) {
475 reg = i2c_smbus_read_byte_data(client,
476 ADM1025_REG_IN_MAX(i));
477 if (reg == 0)
478 i2c_smbus_write_byte_data(client,
479 ADM1025_REG_IN_MAX(i),
480 0xFF);
481 }
482 for (i=0; i<2; i++) {
483 reg = i2c_smbus_read_byte_data(client,
484 ADM1025_REG_TEMP_HIGH(i));
485 if (reg == 0)
486 i2c_smbus_write_byte_data(client,
487 ADM1025_REG_TEMP_HIGH(i),
488 0x7F);
489 }
490
491 /*
492 * Start the conversions
493 */
494 reg = i2c_smbus_read_byte_data(client, ADM1025_REG_CONFIG);
495 if (!(reg & 0x01))
496 i2c_smbus_write_byte_data(client, ADM1025_REG_CONFIG,
497 (reg&0x7E)|0x01);
498}
499
500static int adm1025_detach_client(struct i2c_client *client)
501{
502 int err;
503
504 if ((err = i2c_detach_client(client))) {
505 dev_err(&client->dev, "Client deregistration failed, "
506 "client not detached.\n");
507 return err;
508 }
509
510 kfree(i2c_get_clientdata(client));
511 return 0;
512}
513
514static struct adm1025_data *adm1025_update_device(struct device *dev)
515{
516 struct i2c_client *client = to_i2c_client(dev);
517 struct adm1025_data *data = i2c_get_clientdata(client);
518
519 down(&data->update_lock);
520
521 if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
522 int i;
523
524 dev_dbg(&client->dev, "Updating data.\n");
525 for (i=0; i<6; i++) {
526 data->in[i] = i2c_smbus_read_byte_data(client,
527 ADM1025_REG_IN(i));
528 data->in_min[i] = i2c_smbus_read_byte_data(client,
529 ADM1025_REG_IN_MIN(i));
530 data->in_max[i] = i2c_smbus_read_byte_data(client,
531 ADM1025_REG_IN_MAX(i));
532 }
533 for (i=0; i<2; i++) {
534 data->temp[i] = i2c_smbus_read_byte_data(client,
535 ADM1025_REG_TEMP(i));
536 data->temp_min[i] = i2c_smbus_read_byte_data(client,
537 ADM1025_REG_TEMP_LOW(i));
538 data->temp_max[i] = i2c_smbus_read_byte_data(client,
539 ADM1025_REG_TEMP_HIGH(i));
540 }
541 data->alarms = i2c_smbus_read_byte_data(client,
542 ADM1025_REG_STATUS1)
543 | (i2c_smbus_read_byte_data(client,
544 ADM1025_REG_STATUS2) << 8);
545 data->vid = (i2c_smbus_read_byte_data(client,
546 ADM1025_REG_VID) & 0x0f)
547 | ((i2c_smbus_read_byte_data(client,
548 ADM1025_REG_VID4) & 0x01) << 4);
549
550 data->last_updated = jiffies;
551 data->valid = 1;
552 }
553
554 up(&data->update_lock);
555
556 return data;
557}
558
559static int __init sensors_adm1025_init(void)
560{
561 return i2c_add_driver(&adm1025_driver);
562}
563
564static void __exit sensors_adm1025_exit(void)
565{
566 i2c_del_driver(&adm1025_driver);
567}
568
569MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
570MODULE_DESCRIPTION("ADM1025 driver");
571MODULE_LICENSE("GPL");
572
573module_init(sensors_adm1025_init);
574module_exit(sensors_adm1025_exit);
diff --git a/drivers/i2c/chips/adm1026.c b/drivers/i2c/chips/adm1026.c
new file mode 100644
index 000000000000..39e2f4a900bf
--- /dev/null
+++ b/drivers/i2c/chips/adm1026.c
@@ -0,0 +1,1754 @@
1/*
2 adm1026.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring
4 Copyright (C) 2002, 2003 Philip Pokorny <ppokorny@penguincomputing.com>
5 Copyright (C) 2004 Justin Thiessen <jthiessen@penguincomputing.com>
6
7 Chip details at:
8
9 <http://www.analog.com/UploadedFiles/Data_Sheets/779263102ADM1026_a.pdf>
10
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24*/
25
26#include <linux/config.h>
27#include <linux/module.h>
28#include <linux/init.h>
29#include <linux/slab.h>
30#include <linux/jiffies.h>
31#include <linux/i2c.h>
32#include <linux/i2c-sensor.h>
33#include <linux/i2c-vid.h>
34
35/* Addresses to scan */
36static unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
37static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
38
39/* Insmod parameters */
40SENSORS_INSMOD_1(adm1026);
41
42static int gpio_input[17] = { -1, -1, -1, -1, -1, -1, -1, -1, -1,
43 -1, -1, -1, -1, -1, -1, -1, -1 };
44static int gpio_output[17] = { -1, -1, -1, -1, -1, -1, -1, -1, -1,
45 -1, -1, -1, -1, -1, -1, -1, -1 };
46static int gpio_inverted[17] = { -1, -1, -1, -1, -1, -1, -1, -1, -1,
47 -1, -1, -1, -1, -1, -1, -1, -1 };
48static int gpio_normal[17] = { -1, -1, -1, -1, -1, -1, -1, -1, -1,
49 -1, -1, -1, -1, -1, -1, -1, -1 };
50static int gpio_fan[8] = { -1, -1, -1, -1, -1, -1, -1, -1 };
51module_param_array(gpio_input,int,NULL,0);
52MODULE_PARM_DESC(gpio_input,"List of GPIO pins (0-16) to program as inputs");
53module_param_array(gpio_output,int,NULL,0);
54MODULE_PARM_DESC(gpio_output,"List of GPIO pins (0-16) to program as "
55 "outputs");
56module_param_array(gpio_inverted,int,NULL,0);
57MODULE_PARM_DESC(gpio_inverted,"List of GPIO pins (0-16) to program as "
58 "inverted");
59module_param_array(gpio_normal,int,NULL,0);
60MODULE_PARM_DESC(gpio_normal,"List of GPIO pins (0-16) to program as "
61 "normal/non-inverted");
62module_param_array(gpio_fan,int,NULL,0);
63MODULE_PARM_DESC(gpio_fan,"List of GPIO pins (0-7) to program as fan tachs");
64
65/* Many ADM1026 constants specified below */
66
67/* The ADM1026 registers */
68#define ADM1026_REG_CONFIG1 0x00
69#define CFG1_MONITOR 0x01
70#define CFG1_INT_ENABLE 0x02
71#define CFG1_INT_CLEAR 0x04
72#define CFG1_AIN8_9 0x08
73#define CFG1_THERM_HOT 0x10
74#define CFG1_DAC_AFC 0x20
75#define CFG1_PWM_AFC 0x40
76#define CFG1_RESET 0x80
77#define ADM1026_REG_CONFIG2 0x01
78/* CONFIG2 controls FAN0/GPIO0 through FAN7/GPIO7 */
79#define ADM1026_REG_CONFIG3 0x07
80#define CFG3_GPIO16_ENABLE 0x01
81#define CFG3_CI_CLEAR 0x02
82#define CFG3_VREF_250 0x04
83#define CFG3_GPIO16_DIR 0x40
84#define CFG3_GPIO16_POL 0x80
85#define ADM1026_REG_E2CONFIG 0x13
86#define E2CFG_READ 0x01
87#define E2CFG_WRITE 0x02
88#define E2CFG_ERASE 0x04
89#define E2CFG_ROM 0x08
90#define E2CFG_CLK_EXT 0x80
91
92/* There are 10 general analog inputs and 7 dedicated inputs
93 * They are:
94 * 0 - 9 = AIN0 - AIN9
95 * 10 = Vbat
96 * 11 = 3.3V Standby
97 * 12 = 3.3V Main
98 * 13 = +5V
99 * 14 = Vccp (CPU core voltage)
100 * 15 = +12V
101 * 16 = -12V
102 */
103static u16 ADM1026_REG_IN[] = {
104 0x30, 0x31, 0x32, 0x33, 0x34, 0x35,
105 0x36, 0x37, 0x27, 0x29, 0x26, 0x2a,
106 0x2b, 0x2c, 0x2d, 0x2e, 0x2f
107 };
108static u16 ADM1026_REG_IN_MIN[] = {
109 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d,
110 0x5e, 0x5f, 0x6d, 0x49, 0x6b, 0x4a,
111 0x4b, 0x4c, 0x4d, 0x4e, 0x4f
112 };
113static u16 ADM1026_REG_IN_MAX[] = {
114 0x50, 0x51, 0x52, 0x53, 0x54, 0x55,
115 0x56, 0x57, 0x6c, 0x41, 0x6a, 0x42,
116 0x43, 0x44, 0x45, 0x46, 0x47
117 };
118
119/* Temperatures are:
120 * 0 - Internal
121 * 1 - External 1
122 * 2 - External 2
123 */
124static u16 ADM1026_REG_TEMP[] = { 0x1f, 0x28, 0x29 };
125static u16 ADM1026_REG_TEMP_MIN[] = { 0x69, 0x48, 0x49 };
126static u16 ADM1026_REG_TEMP_MAX[] = { 0x68, 0x40, 0x41 };
127static u16 ADM1026_REG_TEMP_TMIN[] = { 0x10, 0x11, 0x12 };
128static u16 ADM1026_REG_TEMP_THERM[] = { 0x0d, 0x0e, 0x0f };
129static u16 ADM1026_REG_TEMP_OFFSET[] = { 0x1e, 0x6e, 0x6f };
130
131#define ADM1026_REG_FAN(nr) (0x38 + (nr))
132#define ADM1026_REG_FAN_MIN(nr) (0x60 + (nr))
133#define ADM1026_REG_FAN_DIV_0_3 0x02
134#define ADM1026_REG_FAN_DIV_4_7 0x03
135
136#define ADM1026_REG_DAC 0x04
137#define ADM1026_REG_PWM 0x05
138
139#define ADM1026_REG_GPIO_CFG_0_3 0x08
140#define ADM1026_REG_GPIO_CFG_4_7 0x09
141#define ADM1026_REG_GPIO_CFG_8_11 0x0a
142#define ADM1026_REG_GPIO_CFG_12_15 0x0b
143/* CFG_16 in REG_CFG3 */
144#define ADM1026_REG_GPIO_STATUS_0_7 0x24
145#define ADM1026_REG_GPIO_STATUS_8_15 0x25
146/* STATUS_16 in REG_STATUS4 */
147#define ADM1026_REG_GPIO_MASK_0_7 0x1c
148#define ADM1026_REG_GPIO_MASK_8_15 0x1d
149/* MASK_16 in REG_MASK4 */
150
151#define ADM1026_REG_COMPANY 0x16
152#define ADM1026_REG_VERSTEP 0x17
153/* These are the recognized values for the above regs */
154#define ADM1026_COMPANY_ANALOG_DEV 0x41
155#define ADM1026_VERSTEP_GENERIC 0x40
156#define ADM1026_VERSTEP_ADM1026 0x44
157
158#define ADM1026_REG_MASK1 0x18
159#define ADM1026_REG_MASK2 0x19
160#define ADM1026_REG_MASK3 0x1a
161#define ADM1026_REG_MASK4 0x1b
162
163#define ADM1026_REG_STATUS1 0x20
164#define ADM1026_REG_STATUS2 0x21
165#define ADM1026_REG_STATUS3 0x22
166#define ADM1026_REG_STATUS4 0x23
167
168#define ADM1026_FAN_ACTIVATION_TEMP_HYST -6
169#define ADM1026_FAN_CONTROL_TEMP_RANGE 20
170#define ADM1026_PWM_MAX 255
171
172/* Conversions. Rounding and limit checking is only done on the TO_REG
173 * variants. Note that you should be a bit careful with which arguments
174 * these macros are called: arguments may be evaluated more than once.
175 */
176
177/* IN are scaled acording to built-in resistors. These are the
178 * voltages corresponding to 3/4 of full scale (192 or 0xc0)
179 * NOTE: The -12V input needs an additional factor to account
180 * for the Vref pullup resistor.
181 * NEG12_OFFSET = SCALE * Vref / V-192 - Vref
182 * = 13875 * 2.50 / 1.875 - 2500
183 * = 16000
184 *
185 * The values in this table are based on Table II, page 15 of the
186 * datasheet.
187 */
188static int adm1026_scaling[] = { /* .001 Volts */
189 2250, 2250, 2250, 2250, 2250, 2250,
190 1875, 1875, 1875, 1875, 3000, 3330,
191 3330, 4995, 2250, 12000, 13875
192 };
193#define NEG12_OFFSET 16000
194#define SCALE(val,from,to) (((val)*(to) + ((from)/2))/(from))
195#define INS_TO_REG(n,val) (SENSORS_LIMIT(SCALE(val,adm1026_scaling[n],192),\
196 0,255))
197#define INS_FROM_REG(n,val) (SCALE(val,192,adm1026_scaling[n]))
198
199/* FAN speed is measured using 22.5kHz clock and counts for 2 pulses
200 * and we assume a 2 pulse-per-rev fan tach signal
201 * 22500 kHz * 60 (sec/min) * 2 (pulse) / 2 (pulse/rev) == 1350000
202 */
203#define FAN_TO_REG(val,div) ((val)<=0 ? 0xff : SENSORS_LIMIT(1350000/((val)*\
204 (div)),1,254))
205#define FAN_FROM_REG(val,div) ((val)==0?-1:(val)==0xff ? 0 : 1350000/((val)*\
206 (div)))
207#define DIV_FROM_REG(val) (1<<(val))
208#define DIV_TO_REG(val) ((val)>=8 ? 3 : (val)>=4 ? 2 : (val)>=2 ? 1 : 0)
209
210/* Temperature is reported in 1 degC increments */
211#define TEMP_TO_REG(val) (SENSORS_LIMIT(((val)+((val)<0 ? -500 : 500))/1000,\
212 -127,127))
213#define TEMP_FROM_REG(val) ((val) * 1000)
214#define OFFSET_TO_REG(val) (SENSORS_LIMIT(((val)+((val)<0 ? -500 : 500))/1000,\
215 -127,127))
216#define OFFSET_FROM_REG(val) ((val) * 1000)
217
218#define PWM_TO_REG(val) (SENSORS_LIMIT(val,0,255))
219#define PWM_FROM_REG(val) (val)
220
221#define PWM_MIN_TO_REG(val) ((val) & 0xf0)
222#define PWM_MIN_FROM_REG(val) (((val) & 0xf0) + ((val) >> 4))
223
224/* Analog output is a voltage, and scaled to millivolts. The datasheet
225 * indicates that the DAC could be used to drive the fans, but in our
226 * example board (Arima HDAMA) it isn't connected to the fans at all.
227 */
228#define DAC_TO_REG(val) (SENSORS_LIMIT(((((val)*255)+500)/2500),0,255))
229#define DAC_FROM_REG(val) (((val)*2500)/255)
230
231/* Typically used with systems using a v9.1 VRM spec ? */
232#define ADM1026_INIT_VRM 91
233
234/* Chip sampling rates
235 *
236 * Some sensors are not updated more frequently than once per second
237 * so it doesn't make sense to read them more often than that.
238 * We cache the results and return the saved data if the driver
239 * is called again before a second has elapsed.
240 *
241 * Also, there is significant configuration data for this chip
242 * So, we keep the config data up to date in the cache
243 * when it is written and only sample it once every 5 *minutes*
244 */
245#define ADM1026_DATA_INTERVAL (1 * HZ)
246#define ADM1026_CONFIG_INTERVAL (5 * 60 * HZ)
247
248/* We allow for multiple chips in a single system.
249 *
250 * For each registered ADM1026, we need to keep state information
251 * at client->data. The adm1026_data structure is dynamically
252 * allocated, when a new client structure is allocated. */
253
254struct pwm_data {
255 u8 pwm;
256 u8 enable;
257 u8 auto_pwm_min;
258};
259
260struct adm1026_data {
261 struct i2c_client client;
262 struct semaphore lock;
263 enum chips type;
264
265 struct semaphore update_lock;
266 int valid; /* !=0 if following fields are valid */
267 unsigned long last_reading; /* In jiffies */
268 unsigned long last_config; /* In jiffies */
269
270 u8 in[17]; /* Register value */
271 u8 in_max[17]; /* Register value */
272 u8 in_min[17]; /* Register value */
273 s8 temp[3]; /* Register value */
274 s8 temp_min[3]; /* Register value */
275 s8 temp_max[3]; /* Register value */
276 s8 temp_tmin[3]; /* Register value */
277 s8 temp_crit[3]; /* Register value */
278 s8 temp_offset[3]; /* Register value */
279 u8 fan[8]; /* Register value */
280 u8 fan_min[8]; /* Register value */
281 u8 fan_div[8]; /* Decoded value */
282 struct pwm_data pwm1; /* Pwm control values */
283 int vid; /* Decoded value */
284 u8 vrm; /* VRM version */
285 u8 analog_out; /* Register value (DAC) */
286 long alarms; /* Register encoding, combined */
287 long alarm_mask; /* Register encoding, combined */
288 long gpio; /* Register encoding, combined */
289 long gpio_mask; /* Register encoding, combined */
290 u8 gpio_config[17]; /* Decoded value */
291 u8 config1; /* Register value */
292 u8 config2; /* Register value */
293 u8 config3; /* Register value */
294};
295
296static int adm1026_attach_adapter(struct i2c_adapter *adapter);
297static int adm1026_detect(struct i2c_adapter *adapter, int address,
298 int kind);
299static int adm1026_detach_client(struct i2c_client *client);
300static int adm1026_read_value(struct i2c_client *client, u8 register);
301static int adm1026_write_value(struct i2c_client *client, u8 register,
302 int value);
303static void adm1026_print_gpio(struct i2c_client *client);
304static void adm1026_fixup_gpio(struct i2c_client *client);
305static struct adm1026_data *adm1026_update_device(struct device *dev);
306static void adm1026_init_client(struct i2c_client *client);
307
308
309static struct i2c_driver adm1026_driver = {
310 .owner = THIS_MODULE,
311 .name = "adm1026",
312 .flags = I2C_DF_NOTIFY,
313 .attach_adapter = adm1026_attach_adapter,
314 .detach_client = adm1026_detach_client,
315};
316
317int adm1026_attach_adapter(struct i2c_adapter *adapter)
318{
319 if (!(adapter->class & I2C_CLASS_HWMON)) {
320 return 0;
321 }
322 return i2c_detect(adapter, &addr_data, adm1026_detect);
323}
324
325int adm1026_detach_client(struct i2c_client *client)
326{
327 i2c_detach_client(client);
328 kfree(client);
329 return 0;
330}
331
332int adm1026_read_value(struct i2c_client *client, u8 reg)
333{
334 int res;
335
336 if (reg < 0x80) {
337 /* "RAM" locations */
338 res = i2c_smbus_read_byte_data(client, reg) & 0xff;
339 } else {
340 /* EEPROM, do nothing */
341 res = 0;
342 }
343 return res;
344}
345
346int adm1026_write_value(struct i2c_client *client, u8 reg, int value)
347{
348 int res;
349
350 if (reg < 0x80) {
351 /* "RAM" locations */
352 res = i2c_smbus_write_byte_data(client, reg, value);
353 } else {
354 /* EEPROM, do nothing */
355 res = 0;
356 }
357 return res;
358}
359
360void adm1026_init_client(struct i2c_client *client)
361{
362 int value, i;
363 struct adm1026_data *data = i2c_get_clientdata(client);
364
365 dev_dbg(&client->dev, "Initializing device\n");
366 /* Read chip config */
367 data->config1 = adm1026_read_value(client, ADM1026_REG_CONFIG1);
368 data->config2 = adm1026_read_value(client, ADM1026_REG_CONFIG2);
369 data->config3 = adm1026_read_value(client, ADM1026_REG_CONFIG3);
370
371 /* Inform user of chip config */
372 dev_dbg(&client->dev, "ADM1026_REG_CONFIG1 is: 0x%02x\n",
373 data->config1);
374 if ((data->config1 & CFG1_MONITOR) == 0) {
375 dev_dbg(&client->dev, "Monitoring not currently "
376 "enabled.\n");
377 }
378 if (data->config1 & CFG1_INT_ENABLE) {
379 dev_dbg(&client->dev, "SMBALERT interrupts are "
380 "enabled.\n");
381 }
382 if (data->config1 & CFG1_AIN8_9) {
383 dev_dbg(&client->dev, "in8 and in9 enabled. "
384 "temp3 disabled.\n");
385 } else {
386 dev_dbg(&client->dev, "temp3 enabled. in8 and "
387 "in9 disabled.\n");
388 }
389 if (data->config1 & CFG1_THERM_HOT) {
390 dev_dbg(&client->dev, "Automatic THERM, PWM, "
391 "and temp limits enabled.\n");
392 }
393
394 value = data->config3;
395 if (data->config3 & CFG3_GPIO16_ENABLE) {
396 dev_dbg(&client->dev, "GPIO16 enabled. THERM"
397 "pin disabled.\n");
398 } else {
399 dev_dbg(&client->dev, "THERM pin enabled. "
400 "GPIO16 disabled.\n");
401 }
402 if (data->config3 & CFG3_VREF_250) {
403 dev_dbg(&client->dev, "Vref is 2.50 Volts.\n");
404 } else {
405 dev_dbg(&client->dev, "Vref is 1.82 Volts.\n");
406 }
407 /* Read and pick apart the existing GPIO configuration */
408 value = 0;
409 for (i = 0;i <= 15;++i) {
410 if ((i & 0x03) == 0) {
411 value = adm1026_read_value(client,
412 ADM1026_REG_GPIO_CFG_0_3 + i/4);
413 }
414 data->gpio_config[i] = value & 0x03;
415 value >>= 2;
416 }
417 data->gpio_config[16] = (data->config3 >> 6) & 0x03;
418
419 /* ... and then print it */
420 adm1026_print_gpio(client);
421
422 /* If the user asks us to reprogram the GPIO config, then
423 * do it now.
424 */
425 if (gpio_input[0] != -1 || gpio_output[0] != -1
426 || gpio_inverted[0] != -1 || gpio_normal[0] != -1
427 || gpio_fan[0] != -1) {
428 adm1026_fixup_gpio(client);
429 }
430
431 /* WE INTENTIONALLY make no changes to the limits,
432 * offsets, pwms, fans and zones. If they were
433 * configured, we don't want to mess with them.
434 * If they weren't, the default is 100% PWM, no
435 * control and will suffice until 'sensors -s'
436 * can be run by the user. We DO set the default
437 * value for pwm1.auto_pwm_min to its maximum
438 * so that enabling automatic pwm fan control
439 * without first setting a value for pwm1.auto_pwm_min
440 * will not result in potentially dangerous fan speed decrease.
441 */
442 data->pwm1.auto_pwm_min=255;
443 /* Start monitoring */
444 value = adm1026_read_value(client, ADM1026_REG_CONFIG1);
445 /* Set MONITOR, clear interrupt acknowledge and s/w reset */
446 value = (value | CFG1_MONITOR) & (~CFG1_INT_CLEAR & ~CFG1_RESET);
447 dev_dbg(&client->dev, "Setting CONFIG to: 0x%02x\n", value);
448 data->config1 = value;
449 adm1026_write_value(client, ADM1026_REG_CONFIG1, value);
450
451 /* initialize fan_div[] to hardware defaults */
452 value = adm1026_read_value(client, ADM1026_REG_FAN_DIV_0_3) |
453 (adm1026_read_value(client, ADM1026_REG_FAN_DIV_4_7) << 8);
454 for (i = 0;i <= 7;++i) {
455 data->fan_div[i] = DIV_FROM_REG(value & 0x03);
456 value >>= 2;
457 }
458}
459
460void adm1026_print_gpio(struct i2c_client *client)
461{
462 struct adm1026_data *data = i2c_get_clientdata(client);
463 int i;
464
465 dev_dbg(&client->dev, "GPIO config is:");
466 for (i = 0;i <= 7;++i) {
467 if (data->config2 & (1 << i)) {
468 dev_dbg(&client->dev, "\t%sGP%s%d\n",
469 data->gpio_config[i] & 0x02 ? "" : "!",
470 data->gpio_config[i] & 0x01 ? "OUT" : "IN",
471 i);
472 } else {
473 dev_dbg(&client->dev, "\tFAN%d\n", i);
474 }
475 }
476 for (i = 8;i <= 15;++i) {
477 dev_dbg(&client->dev, "\t%sGP%s%d\n",
478 data->gpio_config[i] & 0x02 ? "" : "!",
479 data->gpio_config[i] & 0x01 ? "OUT" : "IN",
480 i);
481 }
482 if (data->config3 & CFG3_GPIO16_ENABLE) {
483 dev_dbg(&client->dev, "\t%sGP%s16\n",
484 data->gpio_config[16] & 0x02 ? "" : "!",
485 data->gpio_config[16] & 0x01 ? "OUT" : "IN");
486 } else {
487 /* GPIO16 is THERM */
488 dev_dbg(&client->dev, "\tTHERM\n");
489 }
490}
491
492void adm1026_fixup_gpio(struct i2c_client *client)
493{
494 struct adm1026_data *data = i2c_get_clientdata(client);
495 int i;
496 int value;
497
498 /* Make the changes requested. */
499 /* We may need to unlock/stop monitoring or soft-reset the
500 * chip before we can make changes. This hasn't been
501 * tested much. FIXME
502 */
503
504 /* Make outputs */
505 for (i = 0;i <= 16;++i) {
506 if (gpio_output[i] >= 0 && gpio_output[i] <= 16) {
507 data->gpio_config[gpio_output[i]] |= 0x01;
508 }
509 /* if GPIO0-7 is output, it isn't a FAN tach */
510 if (gpio_output[i] >= 0 && gpio_output[i] <= 7) {
511 data->config2 |= 1 << gpio_output[i];
512 }
513 }
514
515 /* Input overrides output */
516 for (i = 0;i <= 16;++i) {
517 if (gpio_input[i] >= 0 && gpio_input[i] <= 16) {
518 data->gpio_config[gpio_input[i]] &= ~ 0x01;
519 }
520 /* if GPIO0-7 is input, it isn't a FAN tach */
521 if (gpio_input[i] >= 0 && gpio_input[i] <= 7) {
522 data->config2 |= 1 << gpio_input[i];
523 }
524 }
525
526 /* Inverted */
527 for (i = 0;i <= 16;++i) {
528 if (gpio_inverted[i] >= 0 && gpio_inverted[i] <= 16) {
529 data->gpio_config[gpio_inverted[i]] &= ~ 0x02;
530 }
531 }
532
533 /* Normal overrides inverted */
534 for (i = 0;i <= 16;++i) {
535 if (gpio_normal[i] >= 0 && gpio_normal[i] <= 16) {
536 data->gpio_config[gpio_normal[i]] |= 0x02;
537 }
538 }
539
540 /* Fan overrides input and output */
541 for (i = 0;i <= 7;++i) {
542 if (gpio_fan[i] >= 0 && gpio_fan[i] <= 7) {
543 data->config2 &= ~(1 << gpio_fan[i]);
544 }
545 }
546
547 /* Write new configs to registers */
548 adm1026_write_value(client, ADM1026_REG_CONFIG2, data->config2);
549 data->config3 = (data->config3 & 0x3f)
550 | ((data->gpio_config[16] & 0x03) << 6);
551 adm1026_write_value(client, ADM1026_REG_CONFIG3, data->config3);
552 for (i = 15, value = 0;i >= 0;--i) {
553 value <<= 2;
554 value |= data->gpio_config[i] & 0x03;
555 if ((i & 0x03) == 0) {
556 adm1026_write_value(client,
557 ADM1026_REG_GPIO_CFG_0_3 + i/4,
558 value);
559 value = 0;
560 }
561 }
562
563 /* Print the new config */
564 adm1026_print_gpio(client);
565}
566
567
568static struct adm1026_data *adm1026_update_device(struct device *dev)
569{
570 struct i2c_client *client = to_i2c_client(dev);
571 struct adm1026_data *data = i2c_get_clientdata(client);
572 int i;
573 long value, alarms, gpio;
574
575 down(&data->update_lock);
576 if (!data->valid
577 || time_after(jiffies, data->last_reading + ADM1026_DATA_INTERVAL)) {
578 /* Things that change quickly */
579 dev_dbg(&client->dev,"Reading sensor values\n");
580 for (i = 0;i <= 16;++i) {
581 data->in[i] =
582 adm1026_read_value(client, ADM1026_REG_IN[i]);
583 }
584
585 for (i = 0;i <= 7;++i) {
586 data->fan[i] =
587 adm1026_read_value(client, ADM1026_REG_FAN(i));
588 }
589
590 for (i = 0;i <= 2;++i) {
591 /* NOTE: temp[] is s8 and we assume 2's complement
592 * "conversion" in the assignment */
593 data->temp[i] =
594 adm1026_read_value(client, ADM1026_REG_TEMP[i]);
595 }
596
597 data->pwm1.pwm = adm1026_read_value(client,
598 ADM1026_REG_PWM);
599 data->analog_out = adm1026_read_value(client,
600 ADM1026_REG_DAC);
601 /* GPIO16 is MSbit of alarms, move it to gpio */
602 alarms = adm1026_read_value(client, ADM1026_REG_STATUS4);
603 gpio = alarms & 0x80 ? 0x0100 : 0; /* GPIO16 */
604 alarms &= 0x7f;
605 alarms <<= 8;
606 alarms |= adm1026_read_value(client, ADM1026_REG_STATUS3);
607 alarms <<= 8;
608 alarms |= adm1026_read_value(client, ADM1026_REG_STATUS2);
609 alarms <<= 8;
610 alarms |= adm1026_read_value(client, ADM1026_REG_STATUS1);
611 data->alarms = alarms;
612
613 /* Read the GPIO values */
614 gpio |= adm1026_read_value(client,
615 ADM1026_REG_GPIO_STATUS_8_15);
616 gpio <<= 8;
617 gpio |= adm1026_read_value(client,
618 ADM1026_REG_GPIO_STATUS_0_7);
619 data->gpio = gpio;
620
621 data->last_reading = jiffies;
622 }; /* last_reading */
623
624 if (!data->valid ||
625 time_after(jiffies, data->last_config + ADM1026_CONFIG_INTERVAL)) {
626 /* Things that don't change often */
627 dev_dbg(&client->dev, "Reading config values\n");
628 for (i = 0;i <= 16;++i) {
629 data->in_min[i] = adm1026_read_value(client,
630 ADM1026_REG_IN_MIN[i]);
631 data->in_max[i] = adm1026_read_value(client,
632 ADM1026_REG_IN_MAX[i]);
633 }
634
635 value = adm1026_read_value(client, ADM1026_REG_FAN_DIV_0_3)
636 | (adm1026_read_value(client, ADM1026_REG_FAN_DIV_4_7)
637 << 8);
638 for (i = 0;i <= 7;++i) {
639 data->fan_min[i] = adm1026_read_value(client,
640 ADM1026_REG_FAN_MIN(i));
641 data->fan_div[i] = DIV_FROM_REG(value & 0x03);
642 value >>= 2;
643 }
644
645 for (i = 0; i <= 2; ++i) {
646 /* NOTE: temp_xxx[] are s8 and we assume 2's
647 * complement "conversion" in the assignment
648 */
649 data->temp_min[i] = adm1026_read_value(client,
650 ADM1026_REG_TEMP_MIN[i]);
651 data->temp_max[i] = adm1026_read_value(client,
652 ADM1026_REG_TEMP_MAX[i]);
653 data->temp_tmin[i] = adm1026_read_value(client,
654 ADM1026_REG_TEMP_TMIN[i]);
655 data->temp_crit[i] = adm1026_read_value(client,
656 ADM1026_REG_TEMP_THERM[i]);
657 data->temp_offset[i] = adm1026_read_value(client,
658 ADM1026_REG_TEMP_OFFSET[i]);
659 }
660
661 /* Read the STATUS/alarm masks */
662 alarms = adm1026_read_value(client, ADM1026_REG_MASK4);
663 gpio = alarms & 0x80 ? 0x0100 : 0; /* GPIO16 */
664 alarms = (alarms & 0x7f) << 8;
665 alarms |= adm1026_read_value(client, ADM1026_REG_MASK3);
666 alarms <<= 8;
667 alarms |= adm1026_read_value(client, ADM1026_REG_MASK2);
668 alarms <<= 8;
669 alarms |= adm1026_read_value(client, ADM1026_REG_MASK1);
670 data->alarm_mask = alarms;
671
672 /* Read the GPIO values */
673 gpio |= adm1026_read_value(client,
674 ADM1026_REG_GPIO_MASK_8_15);
675 gpio <<= 8;
676 gpio |= adm1026_read_value(client, ADM1026_REG_GPIO_MASK_0_7);
677 data->gpio_mask = gpio;
678
679 /* Read various values from CONFIG1 */
680 data->config1 = adm1026_read_value(client,
681 ADM1026_REG_CONFIG1);
682 if (data->config1 & CFG1_PWM_AFC) {
683 data->pwm1.enable = 2;
684 data->pwm1.auto_pwm_min =
685 PWM_MIN_FROM_REG(data->pwm1.pwm);
686 }
687 /* Read the GPIO config */
688 data->config2 = adm1026_read_value(client,
689 ADM1026_REG_CONFIG2);
690 data->config3 = adm1026_read_value(client,
691 ADM1026_REG_CONFIG3);
692 data->gpio_config[16] = (data->config3 >> 6) & 0x03;
693
694 value = 0;
695 for (i = 0;i <= 15;++i) {
696 if ((i & 0x03) == 0) {
697 value = adm1026_read_value(client,
698 ADM1026_REG_GPIO_CFG_0_3 + i/4);
699 }
700 data->gpio_config[i] = value & 0x03;
701 value >>= 2;
702 }
703
704 data->last_config = jiffies;
705 }; /* last_config */
706
707 dev_dbg(&client->dev, "Setting VID from GPIO11-15.\n");
708 data->vid = (data->gpio >> 11) & 0x1f;
709 data->valid = 1;
710 up(&data->update_lock);
711 return data;
712}
713
714static ssize_t show_in(struct device *dev, char *buf, int nr)
715{
716 struct adm1026_data *data = adm1026_update_device(dev);
717 return sprintf(buf,"%d\n", INS_FROM_REG(nr, data->in[nr]));
718}
719static ssize_t show_in_min(struct device *dev, char *buf, int nr)
720{
721 struct adm1026_data *data = adm1026_update_device(dev);
722 return sprintf(buf,"%d\n", INS_FROM_REG(nr, data->in_min[nr]));
723}
724static ssize_t set_in_min(struct device *dev, const char *buf,
725 size_t count, int nr)
726{
727 struct i2c_client *client = to_i2c_client(dev);
728 struct adm1026_data *data = i2c_get_clientdata(client);
729 int val = simple_strtol(buf, NULL, 10);
730
731 down(&data->update_lock);
732 data->in_min[nr] = INS_TO_REG(nr, val);
733 adm1026_write_value(client, ADM1026_REG_IN_MIN[nr], data->in_min[nr]);
734 up(&data->update_lock);
735 return count;
736}
737static ssize_t show_in_max(struct device *dev, char *buf, int nr)
738{
739 struct adm1026_data *data = adm1026_update_device(dev);
740 return sprintf(buf,"%d\n", INS_FROM_REG(nr, data->in_max[nr]));
741}
742static ssize_t set_in_max(struct device *dev, const char *buf,
743 size_t count, int nr)
744{
745 struct i2c_client *client = to_i2c_client(dev);
746 struct adm1026_data *data = i2c_get_clientdata(client);
747 int val = simple_strtol(buf, NULL, 10);
748
749 down(&data->update_lock);
750 data->in_max[nr] = INS_TO_REG(nr, val);
751 adm1026_write_value(client, ADM1026_REG_IN_MAX[nr], data->in_max[nr]);
752 up(&data->update_lock);
753 return count;
754}
755
756#define in_reg(offset) \
757static ssize_t show_in##offset (struct device *dev, char *buf) \
758{ \
759 return show_in(dev, buf, offset); \
760} \
761static ssize_t show_in##offset##_min (struct device *dev, char *buf) \
762{ \
763 return show_in_min(dev, buf, offset); \
764} \
765static ssize_t set_in##offset##_min (struct device *dev, \
766 const char *buf, size_t count) \
767{ \
768 return set_in_min(dev, buf, count, offset); \
769} \
770static ssize_t show_in##offset##_max (struct device *dev, char *buf) \
771{ \
772 return show_in_max(dev, buf, offset); \
773} \
774static ssize_t set_in##offset##_max (struct device *dev, \
775 const char *buf, size_t count) \
776{ \
777 return set_in_max(dev, buf, count, offset); \
778} \
779static DEVICE_ATTR(in##offset##_input, S_IRUGO, show_in##offset, NULL); \
780static DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
781 show_in##offset##_min, set_in##offset##_min); \
782static DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
783 show_in##offset##_max, set_in##offset##_max);
784
785
786in_reg(0);
787in_reg(1);
788in_reg(2);
789in_reg(3);
790in_reg(4);
791in_reg(5);
792in_reg(6);
793in_reg(7);
794in_reg(8);
795in_reg(9);
796in_reg(10);
797in_reg(11);
798in_reg(12);
799in_reg(13);
800in_reg(14);
801in_reg(15);
802
803static ssize_t show_in16(struct device *dev, char *buf)
804{
805 struct adm1026_data *data = adm1026_update_device(dev);
806 return sprintf(buf,"%d\n", INS_FROM_REG(16, data->in[16]) -
807 NEG12_OFFSET);
808}
809static ssize_t show_in16_min(struct device *dev, char *buf)
810{
811 struct adm1026_data *data = adm1026_update_device(dev);
812 return sprintf(buf,"%d\n", INS_FROM_REG(16, data->in_min[16])
813 - NEG12_OFFSET);
814}
815static ssize_t set_in16_min(struct device *dev, const char *buf, size_t count)
816{
817 struct i2c_client *client = to_i2c_client(dev);
818 struct adm1026_data *data = i2c_get_clientdata(client);
819 int val = simple_strtol(buf, NULL, 10);
820
821 down(&data->update_lock);
822 data->in_min[16] = INS_TO_REG(16, val + NEG12_OFFSET);
823 adm1026_write_value(client, ADM1026_REG_IN_MIN[16], data->in_min[16]);
824 up(&data->update_lock);
825 return count;
826}
827static ssize_t show_in16_max(struct device *dev, char *buf)
828{
829 struct adm1026_data *data = adm1026_update_device(dev);
830 return sprintf(buf,"%d\n", INS_FROM_REG(16, data->in_max[16])
831 - NEG12_OFFSET);
832}
833static ssize_t set_in16_max(struct device *dev, const char *buf, size_t count)
834{
835 struct i2c_client *client = to_i2c_client(dev);
836 struct adm1026_data *data = i2c_get_clientdata(client);
837 int val = simple_strtol(buf, NULL, 10);
838
839 down(&data->update_lock);
840 data->in_max[16] = INS_TO_REG(16, val+NEG12_OFFSET);
841 adm1026_write_value(client, ADM1026_REG_IN_MAX[16], data->in_max[16]);
842 up(&data->update_lock);
843 return count;
844}
845
846static DEVICE_ATTR(in16_input, S_IRUGO, show_in16, NULL);
847static DEVICE_ATTR(in16_min, S_IRUGO | S_IWUSR, show_in16_min, set_in16_min);
848static DEVICE_ATTR(in16_max, S_IRUGO | S_IWUSR, show_in16_max, set_in16_max);
849
850
851
852
853/* Now add fan read/write functions */
854
855static ssize_t show_fan(struct device *dev, char *buf, int nr)
856{
857 struct adm1026_data *data = adm1026_update_device(dev);
858 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan[nr],
859 data->fan_div[nr]));
860}
861static ssize_t show_fan_min(struct device *dev, char *buf, int nr)
862{
863 struct adm1026_data *data = adm1026_update_device(dev);
864 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan_min[nr],
865 data->fan_div[nr]));
866}
867static ssize_t set_fan_min(struct device *dev, const char *buf,
868 size_t count, int nr)
869{
870 struct i2c_client *client = to_i2c_client(dev);
871 struct adm1026_data *data = i2c_get_clientdata(client);
872 int val = simple_strtol(buf, NULL, 10);
873
874 down(&data->update_lock);
875 data->fan_min[nr] = FAN_TO_REG(val, data->fan_div[nr]);
876 adm1026_write_value(client, ADM1026_REG_FAN_MIN(nr),
877 data->fan_min[nr]);
878 up(&data->update_lock);
879 return count;
880}
881
882#define fan_offset(offset) \
883static ssize_t show_fan_##offset (struct device *dev, char *buf) \
884{ \
885 return show_fan(dev, buf, offset - 1); \
886} \
887static ssize_t show_fan_##offset##_min (struct device *dev, char *buf) \
888{ \
889 return show_fan_min(dev, buf, offset - 1); \
890} \
891static ssize_t set_fan_##offset##_min (struct device *dev, \
892 const char *buf, size_t count) \
893{ \
894 return set_fan_min(dev, buf, count, offset - 1); \
895} \
896static DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_fan_##offset, NULL); \
897static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
898 show_fan_##offset##_min, set_fan_##offset##_min);
899
900fan_offset(1);
901fan_offset(2);
902fan_offset(3);
903fan_offset(4);
904fan_offset(5);
905fan_offset(6);
906fan_offset(7);
907fan_offset(8);
908
909/* Adjust fan_min to account for new fan divisor */
910static void fixup_fan_min(struct device *dev, int fan, int old_div)
911{
912 struct i2c_client *client = to_i2c_client(dev);
913 struct adm1026_data *data = i2c_get_clientdata(client);
914 int new_min;
915 int new_div = data->fan_div[fan];
916
917 /* 0 and 0xff are special. Don't adjust them */
918 if (data->fan_min[fan] == 0 || data->fan_min[fan] == 0xff) {
919 return;
920 }
921
922 new_min = data->fan_min[fan] * old_div / new_div;
923 new_min = SENSORS_LIMIT(new_min, 1, 254);
924 data->fan_min[fan] = new_min;
925 adm1026_write_value(client, ADM1026_REG_FAN_MIN(fan), new_min);
926}
927
928/* Now add fan_div read/write functions */
929static ssize_t show_fan_div(struct device *dev, char *buf, int nr)
930{
931 struct adm1026_data *data = adm1026_update_device(dev);
932 return sprintf(buf,"%d\n", data->fan_div[nr]);
933}
934static ssize_t set_fan_div(struct device *dev, const char *buf,
935 size_t count, int nr)
936{
937 struct i2c_client *client = to_i2c_client(dev);
938 struct adm1026_data *data = i2c_get_clientdata(client);
939 int val,orig_div,new_div,shift;
940
941 val = simple_strtol(buf, NULL, 10);
942 new_div = DIV_TO_REG(val);
943 if (new_div == 0) {
944 return -EINVAL;
945 }
946 down(&data->update_lock);
947 orig_div = data->fan_div[nr];
948 data->fan_div[nr] = DIV_FROM_REG(new_div);
949
950 if (nr < 4) { /* 0 <= nr < 4 */
951 shift = 2 * nr;
952 adm1026_write_value(client, ADM1026_REG_FAN_DIV_0_3,
953 ((DIV_TO_REG(orig_div) & (~(0x03 << shift))) |
954 (new_div << shift)));
955 } else { /* 3 < nr < 8 */
956 shift = 2 * (nr - 4);
957 adm1026_write_value(client, ADM1026_REG_FAN_DIV_4_7,
958 ((DIV_TO_REG(orig_div) & (~(0x03 << (2 * shift)))) |
959 (new_div << shift)));
960 }
961
962 if (data->fan_div[nr] != orig_div) {
963 fixup_fan_min(dev,nr,orig_div);
964 }
965 up(&data->update_lock);
966 return count;
967}
968
969#define fan_offset_div(offset) \
970static ssize_t show_fan_##offset##_div (struct device *dev, char *buf) \
971{ \
972 return show_fan_div(dev, buf, offset - 1); \
973} \
974static ssize_t set_fan_##offset##_div (struct device *dev, \
975 const char *buf, size_t count) \
976{ \
977 return set_fan_div(dev, buf, count, offset - 1); \
978} \
979static DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
980 show_fan_##offset##_div, set_fan_##offset##_div);
981
982fan_offset_div(1);
983fan_offset_div(2);
984fan_offset_div(3);
985fan_offset_div(4);
986fan_offset_div(5);
987fan_offset_div(6);
988fan_offset_div(7);
989fan_offset_div(8);
990
991/* Temps */
992static ssize_t show_temp(struct device *dev, char *buf, int nr)
993{
994 struct adm1026_data *data = adm1026_update_device(dev);
995 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->temp[nr]));
996}
997static ssize_t show_temp_min(struct device *dev, char *buf, int nr)
998{
999 struct adm1026_data *data = adm1026_update_device(dev);
1000 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->temp_min[nr]));
1001}
1002static ssize_t set_temp_min(struct device *dev, const char *buf,
1003 size_t count, int nr)
1004{
1005 struct i2c_client *client = to_i2c_client(dev);
1006 struct adm1026_data *data = i2c_get_clientdata(client);
1007 int val = simple_strtol(buf, NULL, 10);
1008
1009 down(&data->update_lock);
1010 data->temp_min[nr] = TEMP_TO_REG(val);
1011 adm1026_write_value(client, ADM1026_REG_TEMP_MIN[nr],
1012 data->temp_min[nr]);
1013 up(&data->update_lock);
1014 return count;
1015}
1016static ssize_t show_temp_max(struct device *dev, char *buf, int nr)
1017{
1018 struct adm1026_data *data = adm1026_update_device(dev);
1019 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->temp_max[nr]));
1020}
1021static ssize_t set_temp_max(struct device *dev, const char *buf,
1022 size_t count, int nr)
1023{
1024 struct i2c_client *client = to_i2c_client(dev);
1025 struct adm1026_data *data = i2c_get_clientdata(client);
1026 int val = simple_strtol(buf, NULL, 10);
1027
1028 down(&data->update_lock);
1029 data->temp_max[nr] = TEMP_TO_REG(val);
1030 adm1026_write_value(client, ADM1026_REG_TEMP_MAX[nr],
1031 data->temp_max[nr]);
1032 up(&data->update_lock);
1033 return count;
1034}
1035#define temp_reg(offset) \
1036static ssize_t show_temp_##offset (struct device *dev, char *buf) \
1037{ \
1038 return show_temp(dev, buf, offset - 1); \
1039} \
1040static ssize_t show_temp_##offset##_min (struct device *dev, char *buf) \
1041{ \
1042 return show_temp_min(dev, buf, offset - 1); \
1043} \
1044static ssize_t show_temp_##offset##_max (struct device *dev, char *buf) \
1045{ \
1046 return show_temp_max(dev, buf, offset - 1); \
1047} \
1048static ssize_t set_temp_##offset##_min (struct device *dev, \
1049 const char *buf, size_t count) \
1050{ \
1051 return set_temp_min(dev, buf, count, offset - 1); \
1052} \
1053static ssize_t set_temp_##offset##_max (struct device *dev, \
1054 const char *buf, size_t count) \
1055{ \
1056 return set_temp_max(dev, buf, count, offset - 1); \
1057} \
1058static DEVICE_ATTR(temp##offset##_input, S_IRUGO, show_temp_##offset, NULL); \
1059static DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
1060 show_temp_##offset##_min, set_temp_##offset##_min); \
1061static DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
1062 show_temp_##offset##_max, set_temp_##offset##_max);
1063
1064
1065temp_reg(1);
1066temp_reg(2);
1067temp_reg(3);
1068
1069static ssize_t show_temp_offset(struct device *dev, char *buf, int nr)
1070{
1071 struct adm1026_data *data = adm1026_update_device(dev);
1072 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->temp_offset[nr]));
1073}
1074static ssize_t set_temp_offset(struct device *dev, const char *buf,
1075 size_t count, int nr)
1076{
1077 struct i2c_client *client = to_i2c_client(dev);
1078 struct adm1026_data *data = i2c_get_clientdata(client);
1079 int val = simple_strtol(buf, NULL, 10);
1080
1081 down(&data->update_lock);
1082 data->temp_offset[nr] = TEMP_TO_REG(val);
1083 adm1026_write_value(client, ADM1026_REG_TEMP_OFFSET[nr],
1084 data->temp_offset[nr]);
1085 up(&data->update_lock);
1086 return count;
1087}
1088
1089#define temp_offset_reg(offset) \
1090static ssize_t show_temp_##offset##_offset (struct device *dev, char *buf) \
1091{ \
1092 return show_temp_offset(dev, buf, offset - 1); \
1093} \
1094static ssize_t set_temp_##offset##_offset (struct device *dev, \
1095 const char *buf, size_t count) \
1096{ \
1097 return set_temp_offset(dev, buf, count, offset - 1); \
1098} \
1099static DEVICE_ATTR(temp##offset##_offset, S_IRUGO | S_IWUSR, \
1100 show_temp_##offset##_offset, set_temp_##offset##_offset);
1101
1102temp_offset_reg(1);
1103temp_offset_reg(2);
1104temp_offset_reg(3);
1105
1106static ssize_t show_temp_auto_point1_temp_hyst(struct device *dev, char *buf,
1107 int nr)
1108{
1109 struct adm1026_data *data = adm1026_update_device(dev);
1110 return sprintf(buf,"%d\n", TEMP_FROM_REG(
1111 ADM1026_FAN_ACTIVATION_TEMP_HYST + data->temp_tmin[nr]));
1112}
1113static ssize_t show_temp_auto_point2_temp(struct device *dev, char *buf,
1114 int nr)
1115{
1116 struct adm1026_data *data = adm1026_update_device(dev);
1117 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->temp_tmin[nr] +
1118 ADM1026_FAN_CONTROL_TEMP_RANGE));
1119}
1120static ssize_t show_temp_auto_point1_temp(struct device *dev, char *buf,
1121 int nr)
1122{
1123 struct adm1026_data *data = adm1026_update_device(dev);
1124 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->temp_tmin[nr]));
1125}
1126static ssize_t set_temp_auto_point1_temp(struct device *dev, const char *buf,
1127 size_t count, int nr)
1128{
1129 struct i2c_client *client = to_i2c_client(dev);
1130 struct adm1026_data *data = i2c_get_clientdata(client);
1131 int val = simple_strtol(buf, NULL, 10);
1132
1133 down(&data->update_lock);
1134 data->temp_tmin[nr] = TEMP_TO_REG(val);
1135 adm1026_write_value(client, ADM1026_REG_TEMP_TMIN[nr],
1136 data->temp_tmin[nr]);
1137 up(&data->update_lock);
1138 return count;
1139}
1140
1141#define temp_auto_point(offset) \
1142static ssize_t show_temp##offset##_auto_point1_temp (struct device *dev, \
1143 char *buf) \
1144{ \
1145 return show_temp_auto_point1_temp(dev, buf, offset - 1); \
1146} \
1147static ssize_t set_temp##offset##_auto_point1_temp (struct device *dev, \
1148 const char *buf, size_t count) \
1149{ \
1150 return set_temp_auto_point1_temp(dev, buf, count, offset - 1); \
1151} \
1152static ssize_t show_temp##offset##_auto_point1_temp_hyst (struct device \
1153 *dev, char *buf) \
1154{ \
1155 return show_temp_auto_point1_temp_hyst(dev, buf, offset - 1); \
1156} \
1157static ssize_t show_temp##offset##_auto_point2_temp (struct device *dev, \
1158 char *buf) \
1159{ \
1160 return show_temp_auto_point2_temp(dev, buf, offset - 1); \
1161} \
1162static DEVICE_ATTR(temp##offset##_auto_point1_temp, S_IRUGO | S_IWUSR, \
1163 show_temp##offset##_auto_point1_temp, \
1164 set_temp##offset##_auto_point1_temp); \
1165static DEVICE_ATTR(temp##offset##_auto_point1_temp_hyst, S_IRUGO, \
1166 show_temp##offset##_auto_point1_temp_hyst, NULL); \
1167static DEVICE_ATTR(temp##offset##_auto_point2_temp, S_IRUGO, \
1168 show_temp##offset##_auto_point2_temp, NULL);
1169
1170temp_auto_point(1);
1171temp_auto_point(2);
1172temp_auto_point(3);
1173
1174static ssize_t show_temp_crit_enable(struct device *dev, char *buf)
1175{
1176 struct adm1026_data *data = adm1026_update_device(dev);
1177 return sprintf(buf,"%d\n", (data->config1 & CFG1_THERM_HOT) >> 4);
1178}
1179static ssize_t set_temp_crit_enable(struct device *dev, const char *buf,
1180 size_t count)
1181{
1182 struct i2c_client *client = to_i2c_client(dev);
1183 struct adm1026_data *data = i2c_get_clientdata(client);
1184 int val = simple_strtol(buf, NULL, 10);
1185
1186 if ((val == 1) || (val==0)) {
1187 down(&data->update_lock);
1188 data->config1 = (data->config1 & ~CFG1_THERM_HOT) | (val << 4);
1189 adm1026_write_value(client, ADM1026_REG_CONFIG1,
1190 data->config1);
1191 up(&data->update_lock);
1192 }
1193 return count;
1194}
1195
1196static DEVICE_ATTR(temp1_crit_enable, S_IRUGO | S_IWUSR,
1197 show_temp_crit_enable, set_temp_crit_enable);
1198
1199static DEVICE_ATTR(temp2_crit_enable, S_IRUGO | S_IWUSR,
1200 show_temp_crit_enable, set_temp_crit_enable);
1201
1202static DEVICE_ATTR(temp3_crit_enable, S_IRUGO | S_IWUSR,
1203 show_temp_crit_enable, set_temp_crit_enable);
1204
1205
1206static ssize_t show_temp_crit(struct device *dev, char *buf, int nr)
1207{
1208 struct adm1026_data *data = adm1026_update_device(dev);
1209 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->temp_crit[nr]));
1210}
1211static ssize_t set_temp_crit(struct device *dev, const char *buf,
1212 size_t count, int nr)
1213{
1214 struct i2c_client *client = to_i2c_client(dev);
1215 struct adm1026_data *data = i2c_get_clientdata(client);
1216 int val = simple_strtol(buf, NULL, 10);
1217
1218 down(&data->update_lock);
1219 data->temp_crit[nr] = TEMP_TO_REG(val);
1220 adm1026_write_value(client, ADM1026_REG_TEMP_THERM[nr],
1221 data->temp_crit[nr]);
1222 up(&data->update_lock);
1223 return count;
1224}
1225
1226#define temp_crit_reg(offset) \
1227static ssize_t show_temp_##offset##_crit (struct device *dev, char *buf) \
1228{ \
1229 return show_temp_crit(dev, buf, offset - 1); \
1230} \
1231static ssize_t set_temp_##offset##_crit (struct device *dev, \
1232 const char *buf, size_t count) \
1233{ \
1234 return set_temp_crit(dev, buf, count, offset - 1); \
1235} \
1236static DEVICE_ATTR(temp##offset##_crit, S_IRUGO | S_IWUSR, \
1237 show_temp_##offset##_crit, set_temp_##offset##_crit);
1238
1239temp_crit_reg(1);
1240temp_crit_reg(2);
1241temp_crit_reg(3);
1242
1243static ssize_t show_analog_out_reg(struct device *dev, char *buf)
1244{
1245 struct adm1026_data *data = adm1026_update_device(dev);
1246 return sprintf(buf,"%d\n", DAC_FROM_REG(data->analog_out));
1247}
1248static ssize_t set_analog_out_reg(struct device *dev, const char *buf,
1249 size_t count)
1250{
1251 struct i2c_client *client = to_i2c_client(dev);
1252 struct adm1026_data *data = i2c_get_clientdata(client);
1253 int val = simple_strtol(buf, NULL, 10);
1254
1255 down(&data->update_lock);
1256 data->analog_out = DAC_TO_REG(val);
1257 adm1026_write_value(client, ADM1026_REG_DAC, data->analog_out);
1258 up(&data->update_lock);
1259 return count;
1260}
1261
1262static DEVICE_ATTR(analog_out, S_IRUGO | S_IWUSR, show_analog_out_reg,
1263 set_analog_out_reg);
1264
1265static ssize_t show_vid_reg(struct device *dev, char *buf)
1266{
1267 struct adm1026_data *data = adm1026_update_device(dev);
1268 return sprintf(buf,"%d\n", vid_from_reg(data->vid & 0x3f, data->vrm));
1269}
1270
1271static DEVICE_ATTR(vid, S_IRUGO, show_vid_reg, NULL);
1272
1273static ssize_t show_vrm_reg(struct device *dev, char *buf)
1274{
1275 struct adm1026_data *data = adm1026_update_device(dev);
1276 return sprintf(buf,"%d\n", data->vrm);
1277}
1278static ssize_t store_vrm_reg(struct device *dev, const char *buf,
1279 size_t count)
1280{
1281 struct i2c_client *client = to_i2c_client(dev);
1282 struct adm1026_data *data = i2c_get_clientdata(client);
1283
1284 data->vrm = simple_strtol(buf, NULL, 10);
1285 return count;
1286}
1287
1288static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
1289
1290static ssize_t show_alarms_reg(struct device *dev, char *buf)
1291{
1292 struct adm1026_data *data = adm1026_update_device(dev);
1293 return sprintf(buf, "%ld\n", (long) (data->alarms));
1294}
1295
1296static DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL);
1297
1298static ssize_t show_alarm_mask(struct device *dev, char *buf)
1299{
1300 struct adm1026_data *data = adm1026_update_device(dev);
1301 return sprintf(buf,"%ld\n", data->alarm_mask);
1302}
1303static ssize_t set_alarm_mask(struct device *dev, const char *buf,
1304 size_t count)
1305{
1306 struct i2c_client *client = to_i2c_client(dev);
1307 struct adm1026_data *data = i2c_get_clientdata(client);
1308 int val = simple_strtol(buf, NULL, 10);
1309 unsigned long mask;
1310
1311 down(&data->update_lock);
1312 data->alarm_mask = val & 0x7fffffff;
1313 mask = data->alarm_mask
1314 | (data->gpio_mask & 0x10000 ? 0x80000000 : 0);
1315 adm1026_write_value(client, ADM1026_REG_MASK1,
1316 mask & 0xff);
1317 mask >>= 8;
1318 adm1026_write_value(client, ADM1026_REG_MASK2,
1319 mask & 0xff);
1320 mask >>= 8;
1321 adm1026_write_value(client, ADM1026_REG_MASK3,
1322 mask & 0xff);
1323 mask >>= 8;
1324 adm1026_write_value(client, ADM1026_REG_MASK4,
1325 mask & 0xff);
1326 up(&data->update_lock);
1327 return count;
1328}
1329
1330static DEVICE_ATTR(alarm_mask, S_IRUGO | S_IWUSR, show_alarm_mask,
1331 set_alarm_mask);
1332
1333
1334static ssize_t show_gpio(struct device *dev, char *buf)
1335{
1336 struct adm1026_data *data = adm1026_update_device(dev);
1337 return sprintf(buf,"%ld\n", data->gpio);
1338}
1339static ssize_t set_gpio(struct device *dev, const char *buf,
1340 size_t count)
1341{
1342 struct i2c_client *client = to_i2c_client(dev);
1343 struct adm1026_data *data = i2c_get_clientdata(client);
1344 int val = simple_strtol(buf, NULL, 10);
1345 long gpio;
1346
1347 down(&data->update_lock);
1348 data->gpio = val & 0x1ffff;
1349 gpio = data->gpio;
1350 adm1026_write_value(client, ADM1026_REG_GPIO_STATUS_0_7,gpio & 0xff);
1351 gpio >>= 8;
1352 adm1026_write_value(client, ADM1026_REG_GPIO_STATUS_8_15,gpio & 0xff);
1353 gpio = ((gpio >> 1) & 0x80) | (data->alarms >> 24 & 0x7f);
1354 adm1026_write_value(client, ADM1026_REG_STATUS4,gpio & 0xff);
1355 up(&data->update_lock);
1356 return count;
1357}
1358
1359static DEVICE_ATTR(gpio, S_IRUGO | S_IWUSR, show_gpio, set_gpio);
1360
1361
1362static ssize_t show_gpio_mask(struct device *dev, char *buf)
1363{
1364 struct adm1026_data *data = adm1026_update_device(dev);
1365 return sprintf(buf,"%ld\n", data->gpio_mask);
1366}
1367static ssize_t set_gpio_mask(struct device *dev, const char *buf,
1368 size_t count)
1369{
1370 struct i2c_client *client = to_i2c_client(dev);
1371 struct adm1026_data *data = i2c_get_clientdata(client);
1372 int val = simple_strtol(buf, NULL, 10);
1373 long mask;
1374
1375 down(&data->update_lock);
1376 data->gpio_mask = val & 0x1ffff;
1377 mask = data->gpio_mask;
1378 adm1026_write_value(client, ADM1026_REG_GPIO_MASK_0_7,mask & 0xff);
1379 mask >>= 8;
1380 adm1026_write_value(client, ADM1026_REG_GPIO_MASK_8_15,mask & 0xff);
1381 mask = ((mask >> 1) & 0x80) | (data->alarm_mask >> 24 & 0x7f);
1382 adm1026_write_value(client, ADM1026_REG_MASK1,mask & 0xff);
1383 up(&data->update_lock);
1384 return count;
1385}
1386
1387static DEVICE_ATTR(gpio_mask, S_IRUGO | S_IWUSR, show_gpio_mask, set_gpio_mask);
1388
1389static ssize_t show_pwm_reg(struct device *dev, char *buf)
1390{
1391 struct adm1026_data *data = adm1026_update_device(dev);
1392 return sprintf(buf,"%d\n", PWM_FROM_REG(data->pwm1.pwm));
1393}
1394static ssize_t set_pwm_reg(struct device *dev, const char *buf,
1395 size_t count)
1396{
1397 struct i2c_client *client = to_i2c_client(dev);
1398 struct adm1026_data *data = i2c_get_clientdata(client);
1399
1400 if (data->pwm1.enable == 1) {
1401 int val = simple_strtol(buf, NULL, 10);
1402
1403 down(&data->update_lock);
1404 data->pwm1.pwm = PWM_TO_REG(val);
1405 adm1026_write_value(client, ADM1026_REG_PWM, data->pwm1.pwm);
1406 up(&data->update_lock);
1407 }
1408 return count;
1409}
1410static ssize_t show_auto_pwm_min(struct device *dev, char *buf)
1411{
1412 struct adm1026_data *data = adm1026_update_device(dev);
1413 return sprintf(buf,"%d\n", data->pwm1.auto_pwm_min);
1414}
1415static ssize_t set_auto_pwm_min(struct device *dev, const char *buf,
1416 size_t count)
1417{
1418 struct i2c_client *client = to_i2c_client(dev);
1419 struct adm1026_data *data = i2c_get_clientdata(client);
1420 int val = simple_strtol(buf, NULL, 10);
1421
1422 down(&data->update_lock);
1423 data->pwm1.auto_pwm_min = SENSORS_LIMIT(val,0,255);
1424 if (data->pwm1.enable == 2) { /* apply immediately */
1425 data->pwm1.pwm = PWM_TO_REG((data->pwm1.pwm & 0x0f) |
1426 PWM_MIN_TO_REG(data->pwm1.auto_pwm_min));
1427 adm1026_write_value(client, ADM1026_REG_PWM, data->pwm1.pwm);
1428 }
1429 up(&data->update_lock);
1430 return count;
1431}
1432static ssize_t show_auto_pwm_max(struct device *dev, char *buf)
1433{
1434 return sprintf(buf,"%d\n", ADM1026_PWM_MAX);
1435}
1436static ssize_t show_pwm_enable(struct device *dev, char *buf)
1437{
1438 struct adm1026_data *data = adm1026_update_device(dev);
1439 return sprintf(buf,"%d\n", data->pwm1.enable);
1440}
1441static ssize_t set_pwm_enable(struct device *dev, const char *buf,
1442 size_t count)
1443{
1444 struct i2c_client *client = to_i2c_client(dev);
1445 struct adm1026_data *data = i2c_get_clientdata(client);
1446 int val = simple_strtol(buf, NULL, 10);
1447 int old_enable;
1448
1449 if ((val >= 0) && (val < 3)) {
1450 down(&data->update_lock);
1451 old_enable = data->pwm1.enable;
1452 data->pwm1.enable = val;
1453 data->config1 = (data->config1 & ~CFG1_PWM_AFC)
1454 | ((val == 2) ? CFG1_PWM_AFC : 0);
1455 adm1026_write_value(client, ADM1026_REG_CONFIG1,
1456 data->config1);
1457 if (val == 2) { /* apply pwm1_auto_pwm_min to pwm1 */
1458 data->pwm1.pwm = PWM_TO_REG((data->pwm1.pwm & 0x0f) |
1459 PWM_MIN_TO_REG(data->pwm1.auto_pwm_min));
1460 adm1026_write_value(client, ADM1026_REG_PWM,
1461 data->pwm1.pwm);
1462 } else if (!((old_enable == 1) && (val == 1))) {
1463 /* set pwm to safe value */
1464 data->pwm1.pwm = 255;
1465 adm1026_write_value(client, ADM1026_REG_PWM,
1466 data->pwm1.pwm);
1467 }
1468 up(&data->update_lock);
1469 }
1470 return count;
1471}
1472
1473/* enable PWM fan control */
1474static DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm_reg, set_pwm_reg);
1475static DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, show_pwm_reg, set_pwm_reg);
1476static DEVICE_ATTR(pwm3, S_IRUGO | S_IWUSR, show_pwm_reg, set_pwm_reg);
1477static DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, show_pwm_enable,
1478 set_pwm_enable);
1479static DEVICE_ATTR(pwm2_enable, S_IRUGO | S_IWUSR, show_pwm_enable,
1480 set_pwm_enable);
1481static DEVICE_ATTR(pwm3_enable, S_IRUGO | S_IWUSR, show_pwm_enable,
1482 set_pwm_enable);
1483static DEVICE_ATTR(temp1_auto_point1_pwm, S_IRUGO | S_IWUSR,
1484 show_auto_pwm_min, set_auto_pwm_min);
1485static DEVICE_ATTR(temp2_auto_point1_pwm, S_IRUGO | S_IWUSR,
1486 show_auto_pwm_min, set_auto_pwm_min);
1487static DEVICE_ATTR(temp3_auto_point1_pwm, S_IRUGO | S_IWUSR,
1488 show_auto_pwm_min, set_auto_pwm_min);
1489
1490static DEVICE_ATTR(temp1_auto_point2_pwm, S_IRUGO, show_auto_pwm_max, NULL);
1491static DEVICE_ATTR(temp2_auto_point2_pwm, S_IRUGO, show_auto_pwm_max, NULL);
1492static DEVICE_ATTR(temp3_auto_point2_pwm, S_IRUGO, show_auto_pwm_max, NULL);
1493
1494int adm1026_detect(struct i2c_adapter *adapter, int address,
1495 int kind)
1496{
1497 int company, verstep;
1498 struct i2c_client *new_client;
1499 struct adm1026_data *data;
1500 int err = 0;
1501 const char *type_name = "";
1502
1503 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
1504 /* We need to be able to do byte I/O */
1505 goto exit;
1506 };
1507
1508 /* OK. For now, we presume we have a valid client. We now create the
1509 client structure, even though we cannot fill it completely yet.
1510 But it allows us to access adm1026_{read,write}_value. */
1511
1512 if (!(data = kmalloc(sizeof(struct adm1026_data), GFP_KERNEL))) {
1513 err = -ENOMEM;
1514 goto exit;
1515 }
1516
1517 memset(data, 0, sizeof(struct adm1026_data));
1518
1519 new_client = &data->client;
1520 i2c_set_clientdata(new_client, data);
1521 new_client->addr = address;
1522 new_client->adapter = adapter;
1523 new_client->driver = &adm1026_driver;
1524 new_client->flags = 0;
1525
1526 /* Now, we do the remaining detection. */
1527
1528 company = adm1026_read_value(new_client, ADM1026_REG_COMPANY);
1529 verstep = adm1026_read_value(new_client, ADM1026_REG_VERSTEP);
1530
1531 dev_dbg(&new_client->dev, "Detecting device at %d,0x%02x with"
1532 " COMPANY: 0x%02x and VERSTEP: 0x%02x\n",
1533 i2c_adapter_id(new_client->adapter), new_client->addr,
1534 company, verstep);
1535
1536 /* If auto-detecting, Determine the chip type. */
1537 if (kind <= 0) {
1538 dev_dbg(&new_client->dev, "Autodetecting device at %d,0x%02x "
1539 "...\n", i2c_adapter_id(adapter), address);
1540 if (company == ADM1026_COMPANY_ANALOG_DEV
1541 && verstep == ADM1026_VERSTEP_ADM1026) {
1542 kind = adm1026;
1543 } else if (company == ADM1026_COMPANY_ANALOG_DEV
1544 && (verstep & 0xf0) == ADM1026_VERSTEP_GENERIC) {
1545 dev_err(&adapter->dev, ": Unrecognized stepping "
1546 "0x%02x. Defaulting to ADM1026.\n", verstep);
1547 kind = adm1026;
1548 } else if ((verstep & 0xf0) == ADM1026_VERSTEP_GENERIC) {
1549 dev_err(&adapter->dev, ": Found version/stepping "
1550 "0x%02x. Assuming generic ADM1026.\n",
1551 verstep);
1552 kind = any_chip;
1553 } else {
1554 dev_dbg(&new_client->dev, ": Autodetection "
1555 "failed\n");
1556 /* Not an ADM1026 ... */
1557 if (kind == 0) { /* User used force=x,y */
1558 dev_err(&adapter->dev, "Generic ADM1026 not "
1559 "found at %d,0x%02x. Try "
1560 "force_adm1026.\n",
1561 i2c_adapter_id(adapter), address);
1562 }
1563 err = 0;
1564 goto exitfree;
1565 }
1566 }
1567
1568 /* Fill in the chip specific driver values */
1569 switch (kind) {
1570 case any_chip :
1571 type_name = "adm1026";
1572 break;
1573 case adm1026 :
1574 type_name = "adm1026";
1575 break;
1576 default :
1577 dev_err(&adapter->dev, ": Internal error, invalid "
1578 "kind (%d)!", kind);
1579 err = -EFAULT;
1580 goto exitfree;
1581 }
1582 strlcpy(new_client->name, type_name, I2C_NAME_SIZE);
1583
1584 /* Fill in the remaining client fields */
1585 data->type = kind;
1586 data->valid = 0;
1587 init_MUTEX(&data->update_lock);
1588
1589 /* Tell the I2C layer a new client has arrived */
1590 if ((err = i2c_attach_client(new_client)))
1591 goto exitfree;
1592
1593 /* Set the VRM version */
1594 data->vrm = i2c_which_vrm();
1595
1596 /* Initialize the ADM1026 chip */
1597 adm1026_init_client(new_client);
1598
1599 /* Register sysfs hooks */
1600 device_create_file(&new_client->dev, &dev_attr_in0_input);
1601 device_create_file(&new_client->dev, &dev_attr_in0_max);
1602 device_create_file(&new_client->dev, &dev_attr_in0_min);
1603 device_create_file(&new_client->dev, &dev_attr_in1_input);
1604 device_create_file(&new_client->dev, &dev_attr_in1_max);
1605 device_create_file(&new_client->dev, &dev_attr_in1_min);
1606 device_create_file(&new_client->dev, &dev_attr_in2_input);
1607 device_create_file(&new_client->dev, &dev_attr_in2_max);
1608 device_create_file(&new_client->dev, &dev_attr_in2_min);
1609 device_create_file(&new_client->dev, &dev_attr_in3_input);
1610 device_create_file(&new_client->dev, &dev_attr_in3_max);
1611 device_create_file(&new_client->dev, &dev_attr_in3_min);
1612 device_create_file(&new_client->dev, &dev_attr_in4_input);
1613 device_create_file(&new_client->dev, &dev_attr_in4_max);
1614 device_create_file(&new_client->dev, &dev_attr_in4_min);
1615 device_create_file(&new_client->dev, &dev_attr_in5_input);
1616 device_create_file(&new_client->dev, &dev_attr_in5_max);
1617 device_create_file(&new_client->dev, &dev_attr_in5_min);
1618 device_create_file(&new_client->dev, &dev_attr_in6_input);
1619 device_create_file(&new_client->dev, &dev_attr_in6_max);
1620 device_create_file(&new_client->dev, &dev_attr_in6_min);
1621 device_create_file(&new_client->dev, &dev_attr_in7_input);
1622 device_create_file(&new_client->dev, &dev_attr_in7_max);
1623 device_create_file(&new_client->dev, &dev_attr_in7_min);
1624 device_create_file(&new_client->dev, &dev_attr_in8_input);
1625 device_create_file(&new_client->dev, &dev_attr_in8_max);
1626 device_create_file(&new_client->dev, &dev_attr_in8_min);
1627 device_create_file(&new_client->dev, &dev_attr_in9_input);
1628 device_create_file(&new_client->dev, &dev_attr_in9_max);
1629 device_create_file(&new_client->dev, &dev_attr_in9_min);
1630 device_create_file(&new_client->dev, &dev_attr_in10_input);
1631 device_create_file(&new_client->dev, &dev_attr_in10_max);
1632 device_create_file(&new_client->dev, &dev_attr_in10_min);
1633 device_create_file(&new_client->dev, &dev_attr_in11_input);
1634 device_create_file(&new_client->dev, &dev_attr_in11_max);
1635 device_create_file(&new_client->dev, &dev_attr_in11_min);
1636 device_create_file(&new_client->dev, &dev_attr_in12_input);
1637 device_create_file(&new_client->dev, &dev_attr_in12_max);
1638 device_create_file(&new_client->dev, &dev_attr_in12_min);
1639 device_create_file(&new_client->dev, &dev_attr_in13_input);
1640 device_create_file(&new_client->dev, &dev_attr_in13_max);
1641 device_create_file(&new_client->dev, &dev_attr_in13_min);
1642 device_create_file(&new_client->dev, &dev_attr_in14_input);
1643 device_create_file(&new_client->dev, &dev_attr_in14_max);
1644 device_create_file(&new_client->dev, &dev_attr_in14_min);
1645 device_create_file(&new_client->dev, &dev_attr_in15_input);
1646 device_create_file(&new_client->dev, &dev_attr_in15_max);
1647 device_create_file(&new_client->dev, &dev_attr_in15_min);
1648 device_create_file(&new_client->dev, &dev_attr_in16_input);
1649 device_create_file(&new_client->dev, &dev_attr_in16_max);
1650 device_create_file(&new_client->dev, &dev_attr_in16_min);
1651 device_create_file(&new_client->dev, &dev_attr_fan1_input);
1652 device_create_file(&new_client->dev, &dev_attr_fan1_div);
1653 device_create_file(&new_client->dev, &dev_attr_fan1_min);
1654 device_create_file(&new_client->dev, &dev_attr_fan2_input);
1655 device_create_file(&new_client->dev, &dev_attr_fan2_div);
1656 device_create_file(&new_client->dev, &dev_attr_fan2_min);
1657 device_create_file(&new_client->dev, &dev_attr_fan3_input);
1658 device_create_file(&new_client->dev, &dev_attr_fan3_div);
1659 device_create_file(&new_client->dev, &dev_attr_fan3_min);
1660 device_create_file(&new_client->dev, &dev_attr_fan4_input);
1661 device_create_file(&new_client->dev, &dev_attr_fan4_div);
1662 device_create_file(&new_client->dev, &dev_attr_fan4_min);
1663 device_create_file(&new_client->dev, &dev_attr_fan5_input);
1664 device_create_file(&new_client->dev, &dev_attr_fan5_div);
1665 device_create_file(&new_client->dev, &dev_attr_fan5_min);
1666 device_create_file(&new_client->dev, &dev_attr_fan6_input);
1667 device_create_file(&new_client->dev, &dev_attr_fan6_div);
1668 device_create_file(&new_client->dev, &dev_attr_fan6_min);
1669 device_create_file(&new_client->dev, &dev_attr_fan7_input);
1670 device_create_file(&new_client->dev, &dev_attr_fan7_div);
1671 device_create_file(&new_client->dev, &dev_attr_fan7_min);
1672 device_create_file(&new_client->dev, &dev_attr_fan8_input);
1673 device_create_file(&new_client->dev, &dev_attr_fan8_div);
1674 device_create_file(&new_client->dev, &dev_attr_fan8_min);
1675 device_create_file(&new_client->dev, &dev_attr_temp1_input);
1676 device_create_file(&new_client->dev, &dev_attr_temp1_max);
1677 device_create_file(&new_client->dev, &dev_attr_temp1_min);
1678 device_create_file(&new_client->dev, &dev_attr_temp2_input);
1679 device_create_file(&new_client->dev, &dev_attr_temp2_max);
1680 device_create_file(&new_client->dev, &dev_attr_temp2_min);
1681 device_create_file(&new_client->dev, &dev_attr_temp3_input);
1682 device_create_file(&new_client->dev, &dev_attr_temp3_max);
1683 device_create_file(&new_client->dev, &dev_attr_temp3_min);
1684 device_create_file(&new_client->dev, &dev_attr_temp1_offset);
1685 device_create_file(&new_client->dev, &dev_attr_temp2_offset);
1686 device_create_file(&new_client->dev, &dev_attr_temp3_offset);
1687 device_create_file(&new_client->dev,
1688 &dev_attr_temp1_auto_point1_temp);
1689 device_create_file(&new_client->dev,
1690 &dev_attr_temp2_auto_point1_temp);
1691 device_create_file(&new_client->dev,
1692 &dev_attr_temp3_auto_point1_temp);
1693 device_create_file(&new_client->dev,
1694 &dev_attr_temp1_auto_point1_temp_hyst);
1695 device_create_file(&new_client->dev,
1696 &dev_attr_temp2_auto_point1_temp_hyst);
1697 device_create_file(&new_client->dev,
1698 &dev_attr_temp3_auto_point1_temp_hyst);
1699 device_create_file(&new_client->dev,
1700 &dev_attr_temp1_auto_point2_temp);
1701 device_create_file(&new_client->dev,
1702 &dev_attr_temp2_auto_point2_temp);
1703 device_create_file(&new_client->dev,
1704 &dev_attr_temp3_auto_point2_temp);
1705 device_create_file(&new_client->dev, &dev_attr_temp1_crit);
1706 device_create_file(&new_client->dev, &dev_attr_temp2_crit);
1707 device_create_file(&new_client->dev, &dev_attr_temp3_crit);
1708 device_create_file(&new_client->dev, &dev_attr_temp1_crit_enable);
1709 device_create_file(&new_client->dev, &dev_attr_temp2_crit_enable);
1710 device_create_file(&new_client->dev, &dev_attr_temp3_crit_enable);
1711 device_create_file(&new_client->dev, &dev_attr_vid);
1712 device_create_file(&new_client->dev, &dev_attr_vrm);
1713 device_create_file(&new_client->dev, &dev_attr_alarms);
1714 device_create_file(&new_client->dev, &dev_attr_alarm_mask);
1715 device_create_file(&new_client->dev, &dev_attr_gpio);
1716 device_create_file(&new_client->dev, &dev_attr_gpio_mask);
1717 device_create_file(&new_client->dev, &dev_attr_pwm1);
1718 device_create_file(&new_client->dev, &dev_attr_pwm2);
1719 device_create_file(&new_client->dev, &dev_attr_pwm3);
1720 device_create_file(&new_client->dev, &dev_attr_pwm1_enable);
1721 device_create_file(&new_client->dev, &dev_attr_pwm2_enable);
1722 device_create_file(&new_client->dev, &dev_attr_pwm3_enable);
1723 device_create_file(&new_client->dev, &dev_attr_temp1_auto_point1_pwm);
1724 device_create_file(&new_client->dev, &dev_attr_temp2_auto_point1_pwm);
1725 device_create_file(&new_client->dev, &dev_attr_temp3_auto_point1_pwm);
1726 device_create_file(&new_client->dev, &dev_attr_temp1_auto_point2_pwm);
1727 device_create_file(&new_client->dev, &dev_attr_temp2_auto_point2_pwm);
1728 device_create_file(&new_client->dev, &dev_attr_temp3_auto_point2_pwm);
1729 device_create_file(&new_client->dev, &dev_attr_analog_out);
1730 return 0;
1731
1732 /* Error out and cleanup code */
1733exitfree:
1734 kfree(new_client);
1735exit:
1736 return err;
1737}
1738static int __init sm_adm1026_init(void)
1739{
1740 return i2c_add_driver(&adm1026_driver);
1741}
1742
1743static void __exit sm_adm1026_exit(void)
1744{
1745 i2c_del_driver(&adm1026_driver);
1746}
1747
1748MODULE_LICENSE("GPL");
1749MODULE_AUTHOR("Philip Pokorny <ppokorny@penguincomputing.com>, "
1750 "Justin Thiessen <jthiessen@penguincomputing.com>");
1751MODULE_DESCRIPTION("ADM1026 driver");
1752
1753module_init(sm_adm1026_init);
1754module_exit(sm_adm1026_exit);
diff --git a/drivers/i2c/chips/adm1031.c b/drivers/i2c/chips/adm1031.c
new file mode 100644
index 000000000000..d4385a23f79a
--- /dev/null
+++ b/drivers/i2c/chips/adm1031.c
@@ -0,0 +1,977 @@
1/*
2 adm1031.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring
4 Based on lm75.c and lm85.c
5 Supports adm1030 / adm1031
6 Copyright (C) 2004 Alexandre d'Alton <alex@alexdalton.org>
7 Reworked by Jean Delvare <khali@linux-fr.org>
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22*/
23
24#include <linux/module.h>
25#include <linux/init.h>
26#include <linux/slab.h>
27#include <linux/jiffies.h>
28#include <linux/i2c.h>
29#include <linux/i2c-sensor.h>
30
31/* Following macros takes channel parameter starting from 0 to 2 */
32#define ADM1031_REG_FAN_SPEED(nr) (0x08 + (nr))
33#define ADM1031_REG_FAN_DIV(nr) (0x20 + (nr))
34#define ADM1031_REG_PWM (0x22)
35#define ADM1031_REG_FAN_MIN(nr) (0x10 + (nr))
36
37#define ADM1031_REG_TEMP_MAX(nr) (0x14 + 4*(nr))
38#define ADM1031_REG_TEMP_MIN(nr) (0x15 + 4*(nr))
39#define ADM1031_REG_TEMP_CRIT(nr) (0x16 + 4*(nr))
40
41#define ADM1031_REG_TEMP(nr) (0xa + (nr))
42#define ADM1031_REG_AUTO_TEMP(nr) (0x24 + (nr))
43
44#define ADM1031_REG_STATUS(nr) (0x2 + (nr))
45
46#define ADM1031_REG_CONF1 0x0
47#define ADM1031_REG_CONF2 0x1
48#define ADM1031_REG_EXT_TEMP 0x6
49
50#define ADM1031_CONF1_MONITOR_ENABLE 0x01 /* Monitoring enable */
51#define ADM1031_CONF1_PWM_INVERT 0x08 /* PWM Invert */
52#define ADM1031_CONF1_AUTO_MODE 0x80 /* Auto FAN */
53
54#define ADM1031_CONF2_PWM1_ENABLE 0x01
55#define ADM1031_CONF2_PWM2_ENABLE 0x02
56#define ADM1031_CONF2_TACH1_ENABLE 0x04
57#define ADM1031_CONF2_TACH2_ENABLE 0x08
58#define ADM1031_CONF2_TEMP_ENABLE(chan) (0x10 << (chan))
59
60/* Addresses to scan */
61static unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
62static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
63
64/* Insmod parameters */
65SENSORS_INSMOD_2(adm1030, adm1031);
66
67typedef u8 auto_chan_table_t[8][2];
68
69/* Each client has this additional data */
70struct adm1031_data {
71 struct i2c_client client;
72 struct semaphore update_lock;
73 int chip_type;
74 char valid; /* !=0 if following fields are valid */
75 unsigned long last_updated; /* In jiffies */
76 /* The chan_select_table contains the possible configurations for
77 * auto fan control.
78 */
79 auto_chan_table_t *chan_select_table;
80 u16 alarm;
81 u8 conf1;
82 u8 conf2;
83 u8 fan[2];
84 u8 fan_div[2];
85 u8 fan_min[2];
86 u8 pwm[2];
87 u8 old_pwm[2];
88 s8 temp[3];
89 u8 ext_temp[3];
90 u8 auto_temp[3];
91 u8 auto_temp_min[3];
92 u8 auto_temp_off[3];
93 u8 auto_temp_max[3];
94 s8 temp_min[3];
95 s8 temp_max[3];
96 s8 temp_crit[3];
97};
98
99static int adm1031_attach_adapter(struct i2c_adapter *adapter);
100static int adm1031_detect(struct i2c_adapter *adapter, int address, int kind);
101static void adm1031_init_client(struct i2c_client *client);
102static int adm1031_detach_client(struct i2c_client *client);
103static struct adm1031_data *adm1031_update_device(struct device *dev);
104
105/* This is the driver that will be inserted */
106static struct i2c_driver adm1031_driver = {
107 .owner = THIS_MODULE,
108 .name = "adm1031",
109 .flags = I2C_DF_NOTIFY,
110 .attach_adapter = adm1031_attach_adapter,
111 .detach_client = adm1031_detach_client,
112};
113
114static inline u8 adm1031_read_value(struct i2c_client *client, u8 reg)
115{
116 return i2c_smbus_read_byte_data(client, reg);
117}
118
119static inline int
120adm1031_write_value(struct i2c_client *client, u8 reg, unsigned int value)
121{
122 return i2c_smbus_write_byte_data(client, reg, value);
123}
124
125
126#define TEMP_TO_REG(val) (((val) < 0 ? ((val - 500) / 1000) : \
127 ((val + 500) / 1000)))
128
129#define TEMP_FROM_REG(val) ((val) * 1000)
130
131#define TEMP_FROM_REG_EXT(val, ext) (TEMP_FROM_REG(val) + (ext) * 125)
132
133#define FAN_FROM_REG(reg, div) ((reg) ? (11250 * 60) / ((reg) * (div)) : 0)
134
135static int FAN_TO_REG(int reg, int div)
136{
137 int tmp;
138 tmp = FAN_FROM_REG(SENSORS_LIMIT(reg, 0, 65535), div);
139 return tmp > 255 ? 255 : tmp;
140}
141
142#define FAN_DIV_FROM_REG(reg) (1<<(((reg)&0xc0)>>6))
143
144#define PWM_TO_REG(val) (SENSORS_LIMIT((val), 0, 255) >> 4)
145#define PWM_FROM_REG(val) ((val) << 4)
146
147#define FAN_CHAN_FROM_REG(reg) (((reg) >> 5) & 7)
148#define FAN_CHAN_TO_REG(val, reg) \
149 (((reg) & 0x1F) | (((val) << 5) & 0xe0))
150
151#define AUTO_TEMP_MIN_TO_REG(val, reg) \
152 ((((val)/500) & 0xf8)|((reg) & 0x7))
153#define AUTO_TEMP_RANGE_FROM_REG(reg) (5000 * (1<< ((reg)&0x7)))
154#define AUTO_TEMP_MIN_FROM_REG(reg) (1000 * ((((reg) >> 3) & 0x1f) << 2))
155
156#define AUTO_TEMP_MIN_FROM_REG_DEG(reg) ((((reg) >> 3) & 0x1f) << 2)
157
158#define AUTO_TEMP_OFF_FROM_REG(reg) \
159 (AUTO_TEMP_MIN_FROM_REG(reg) - 5000)
160
161#define AUTO_TEMP_MAX_FROM_REG(reg) \
162 (AUTO_TEMP_RANGE_FROM_REG(reg) + \
163 AUTO_TEMP_MIN_FROM_REG(reg))
164
165static int AUTO_TEMP_MAX_TO_REG(int val, int reg, int pwm)
166{
167 int ret;
168 int range = val - AUTO_TEMP_MIN_FROM_REG(reg);
169
170 range = ((val - AUTO_TEMP_MIN_FROM_REG(reg))*10)/(16 - pwm);
171 ret = ((reg & 0xf8) |
172 (range < 10000 ? 0 :
173 range < 20000 ? 1 :
174 range < 40000 ? 2 : range < 80000 ? 3 : 4));
175 return ret;
176}
177
178/* FAN auto control */
179#define GET_FAN_AUTO_BITFIELD(data, idx) \
180 (*(data)->chan_select_table)[FAN_CHAN_FROM_REG((data)->conf1)][idx%2]
181
182/* The tables below contains the possible values for the auto fan
183 * control bitfields. the index in the table is the register value.
184 * MSb is the auto fan control enable bit, so the four first entries
185 * in the table disables auto fan control when both bitfields are zero.
186 */
187static auto_chan_table_t auto_channel_select_table_adm1031 = {
188 {0, 0}, {0, 0}, {0, 0}, {0, 0},
189 {2 /*0b010 */ , 4 /*0b100 */ },
190 {2 /*0b010 */ , 2 /*0b010 */ },
191 {4 /*0b100 */ , 4 /*0b100 */ },
192 {7 /*0b111 */ , 7 /*0b111 */ },
193};
194
195static auto_chan_table_t auto_channel_select_table_adm1030 = {
196 {0, 0}, {0, 0}, {0, 0}, {0, 0},
197 {2 /*0b10 */ , 0},
198 {0xff /*invalid */ , 0},
199 {0xff /*invalid */ , 0},
200 {3 /*0b11 */ , 0},
201};
202
203/* That function checks if a bitfield is valid and returns the other bitfield
204 * nearest match if no exact match where found.
205 */
206static int
207get_fan_auto_nearest(struct adm1031_data *data,
208 int chan, u8 val, u8 reg, u8 * new_reg)
209{
210 int i;
211 int first_match = -1, exact_match = -1;
212 u8 other_reg_val =
213 (*data->chan_select_table)[FAN_CHAN_FROM_REG(reg)][chan ? 0 : 1];
214
215 if (val == 0) {
216 *new_reg = 0;
217 return 0;
218 }
219
220 for (i = 0; i < 8; i++) {
221 if ((val == (*data->chan_select_table)[i][chan]) &&
222 ((*data->chan_select_table)[i][chan ? 0 : 1] ==
223 other_reg_val)) {
224 /* We found an exact match */
225 exact_match = i;
226 break;
227 } else if (val == (*data->chan_select_table)[i][chan] &&
228 first_match == -1) {
229 /* Save the first match in case of an exact match has not been
230 * found
231 */
232 first_match = i;
233 }
234 }
235
236 if (exact_match >= 0) {
237 *new_reg = exact_match;
238 } else if (first_match >= 0) {
239 *new_reg = first_match;
240 } else {
241 return -EINVAL;
242 }
243 return 0;
244}
245
246static ssize_t show_fan_auto_channel(struct device *dev, char *buf, int nr)
247{
248 struct adm1031_data *data = adm1031_update_device(dev);
249 return sprintf(buf, "%d\n", GET_FAN_AUTO_BITFIELD(data, nr));
250}
251
252static ssize_t
253set_fan_auto_channel(struct device *dev, const char *buf, size_t count, int nr)
254{
255 struct i2c_client *client = to_i2c_client(dev);
256 struct adm1031_data *data = i2c_get_clientdata(client);
257 int val = simple_strtol(buf, NULL, 10);
258 u8 reg;
259 int ret;
260 u8 old_fan_mode;
261
262 old_fan_mode = data->conf1;
263
264 down(&data->update_lock);
265
266 if ((ret = get_fan_auto_nearest(data, nr, val, data->conf1, &reg))) {
267 up(&data->update_lock);
268 return ret;
269 }
270 if (((data->conf1 = FAN_CHAN_TO_REG(reg, data->conf1)) & ADM1031_CONF1_AUTO_MODE) ^
271 (old_fan_mode & ADM1031_CONF1_AUTO_MODE)) {
272 if (data->conf1 & ADM1031_CONF1_AUTO_MODE){
273 /* Switch to Auto Fan Mode
274 * Save PWM registers
275 * Set PWM registers to 33% Both */
276 data->old_pwm[0] = data->pwm[0];
277 data->old_pwm[1] = data->pwm[1];
278 adm1031_write_value(client, ADM1031_REG_PWM, 0x55);
279 } else {
280 /* Switch to Manual Mode */
281 data->pwm[0] = data->old_pwm[0];
282 data->pwm[1] = data->old_pwm[1];
283 /* Restore PWM registers */
284 adm1031_write_value(client, ADM1031_REG_PWM,
285 data->pwm[0] | (data->pwm[1] << 4));
286 }
287 }
288 data->conf1 = FAN_CHAN_TO_REG(reg, data->conf1);
289 adm1031_write_value(client, ADM1031_REG_CONF1, data->conf1);
290 up(&data->update_lock);
291 return count;
292}
293
294#define fan_auto_channel_offset(offset) \
295static ssize_t show_fan_auto_channel_##offset (struct device *dev, char *buf) \
296{ \
297 return show_fan_auto_channel(dev, buf, offset - 1); \
298} \
299static ssize_t set_fan_auto_channel_##offset (struct device *dev, \
300 const char *buf, size_t count) \
301{ \
302 return set_fan_auto_channel(dev, buf, count, offset - 1); \
303} \
304static DEVICE_ATTR(auto_fan##offset##_channel, S_IRUGO | S_IWUSR, \
305 show_fan_auto_channel_##offset, \
306 set_fan_auto_channel_##offset)
307
308fan_auto_channel_offset(1);
309fan_auto_channel_offset(2);
310
311/* Auto Temps */
312static ssize_t show_auto_temp_off(struct device *dev, char *buf, int nr)
313{
314 struct adm1031_data *data = adm1031_update_device(dev);
315 return sprintf(buf, "%d\n",
316 AUTO_TEMP_OFF_FROM_REG(data->auto_temp[nr]));
317}
318static ssize_t show_auto_temp_min(struct device *dev, char *buf, int nr)
319{
320 struct adm1031_data *data = adm1031_update_device(dev);
321 return sprintf(buf, "%d\n",
322 AUTO_TEMP_MIN_FROM_REG(data->auto_temp[nr]));
323}
324static ssize_t
325set_auto_temp_min(struct device *dev, const char *buf, size_t count, int nr)
326{
327 struct i2c_client *client = to_i2c_client(dev);
328 struct adm1031_data *data = i2c_get_clientdata(client);
329 int val = simple_strtol(buf, NULL, 10);
330
331 down(&data->update_lock);
332 data->auto_temp[nr] = AUTO_TEMP_MIN_TO_REG(val, data->auto_temp[nr]);
333 adm1031_write_value(client, ADM1031_REG_AUTO_TEMP(nr),
334 data->auto_temp[nr]);
335 up(&data->update_lock);
336 return count;
337}
338static ssize_t show_auto_temp_max(struct device *dev, char *buf, int nr)
339{
340 struct adm1031_data *data = adm1031_update_device(dev);
341 return sprintf(buf, "%d\n",
342 AUTO_TEMP_MAX_FROM_REG(data->auto_temp[nr]));
343}
344static ssize_t
345set_auto_temp_max(struct device *dev, const char *buf, size_t count, int nr)
346{
347 struct i2c_client *client = to_i2c_client(dev);
348 struct adm1031_data *data = i2c_get_clientdata(client);
349 int val = simple_strtol(buf, NULL, 10);
350
351 down(&data->update_lock);
352 data->temp_max[nr] = AUTO_TEMP_MAX_TO_REG(val, data->auto_temp[nr], data->pwm[nr]);
353 adm1031_write_value(client, ADM1031_REG_AUTO_TEMP(nr),
354 data->temp_max[nr]);
355 up(&data->update_lock);
356 return count;
357}
358
359#define auto_temp_reg(offset) \
360static ssize_t show_auto_temp_##offset##_off (struct device *dev, char *buf) \
361{ \
362 return show_auto_temp_off(dev, buf, offset - 1); \
363} \
364static ssize_t show_auto_temp_##offset##_min (struct device *dev, char *buf) \
365{ \
366 return show_auto_temp_min(dev, buf, offset - 1); \
367} \
368static ssize_t show_auto_temp_##offset##_max (struct device *dev, char *buf) \
369{ \
370 return show_auto_temp_max(dev, buf, offset - 1); \
371} \
372static ssize_t set_auto_temp_##offset##_min (struct device *dev, \
373 const char *buf, size_t count) \
374{ \
375 return set_auto_temp_min(dev, buf, count, offset - 1); \
376} \
377static ssize_t set_auto_temp_##offset##_max (struct device *dev, \
378 const char *buf, size_t count) \
379{ \
380 return set_auto_temp_max(dev, buf, count, offset - 1); \
381} \
382static DEVICE_ATTR(auto_temp##offset##_off, S_IRUGO, \
383 show_auto_temp_##offset##_off, NULL); \
384static DEVICE_ATTR(auto_temp##offset##_min, S_IRUGO | S_IWUSR, \
385 show_auto_temp_##offset##_min, set_auto_temp_##offset##_min);\
386static DEVICE_ATTR(auto_temp##offset##_max, S_IRUGO | S_IWUSR, \
387 show_auto_temp_##offset##_max, set_auto_temp_##offset##_max)
388
389auto_temp_reg(1);
390auto_temp_reg(2);
391auto_temp_reg(3);
392
393/* pwm */
394static ssize_t show_pwm(struct device *dev, char *buf, int nr)
395{
396 struct adm1031_data *data = adm1031_update_device(dev);
397 return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm[nr]));
398}
399static ssize_t
400set_pwm(struct device *dev, const char *buf, size_t count, int nr)
401{
402 struct i2c_client *client = to_i2c_client(dev);
403 struct adm1031_data *data = i2c_get_clientdata(client);
404 int val = simple_strtol(buf, NULL, 10);
405 int reg;
406
407 down(&data->update_lock);
408 if ((data->conf1 & ADM1031_CONF1_AUTO_MODE) &&
409 (((val>>4) & 0xf) != 5)) {
410 /* In automatic mode, the only PWM accepted is 33% */
411 up(&data->update_lock);
412 return -EINVAL;
413 }
414 data->pwm[nr] = PWM_TO_REG(val);
415 reg = adm1031_read_value(client, ADM1031_REG_PWM);
416 adm1031_write_value(client, ADM1031_REG_PWM,
417 nr ? ((data->pwm[nr] << 4) & 0xf0) | (reg & 0xf)
418 : (data->pwm[nr] & 0xf) | (reg & 0xf0));
419 up(&data->update_lock);
420 return count;
421}
422
423#define pwm_reg(offset) \
424static ssize_t show_pwm_##offset (struct device *dev, char *buf) \
425{ \
426 return show_pwm(dev, buf, offset - 1); \
427} \
428static ssize_t set_pwm_##offset (struct device *dev, \
429 const char *buf, size_t count) \
430{ \
431 return set_pwm(dev, buf, count, offset - 1); \
432} \
433static DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
434 show_pwm_##offset, set_pwm_##offset)
435
436pwm_reg(1);
437pwm_reg(2);
438
439/* Fans */
440
441/*
442 * That function checks the cases where the fan reading is not
443 * relevent. It is used to provide 0 as fan reading when the fan is
444 * not supposed to run
445 */
446static int trust_fan_readings(struct adm1031_data *data, int chan)
447{
448 int res = 0;
449
450 if (data->conf1 & ADM1031_CONF1_AUTO_MODE) {
451 switch (data->conf1 & 0x60) {
452 case 0x00: /* remote temp1 controls fan1 remote temp2 controls fan2 */
453 res = data->temp[chan+1] >=
454 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[chan+1]);
455 break;
456 case 0x20: /* remote temp1 controls both fans */
457 res =
458 data->temp[1] >=
459 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[1]);
460 break;
461 case 0x40: /* remote temp2 controls both fans */
462 res =
463 data->temp[2] >=
464 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[2]);
465 break;
466 case 0x60: /* max controls both fans */
467 res =
468 data->temp[0] >=
469 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[0])
470 || data->temp[1] >=
471 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[1])
472 || (data->chip_type == adm1031
473 && data->temp[2] >=
474 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[2]));
475 break;
476 }
477 } else {
478 res = data->pwm[chan] > 0;
479 }
480 return res;
481}
482
483
484static ssize_t show_fan(struct device *dev, char *buf, int nr)
485{
486 struct adm1031_data *data = adm1031_update_device(dev);
487 int value;
488
489 value = trust_fan_readings(data, nr) ? FAN_FROM_REG(data->fan[nr],
490 FAN_DIV_FROM_REG(data->fan_div[nr])) : 0;
491 return sprintf(buf, "%d\n", value);
492}
493
494static ssize_t show_fan_div(struct device *dev, char *buf, int nr)
495{
496 struct adm1031_data *data = adm1031_update_device(dev);
497 return sprintf(buf, "%d\n", FAN_DIV_FROM_REG(data->fan_div[nr]));
498}
499static ssize_t show_fan_min(struct device *dev, char *buf, int nr)
500{
501 struct adm1031_data *data = adm1031_update_device(dev);
502 return sprintf(buf, "%d\n",
503 FAN_FROM_REG(data->fan_min[nr],
504 FAN_DIV_FROM_REG(data->fan_div[nr])));
505}
506static ssize_t
507set_fan_min(struct device *dev, const char *buf, size_t count, int nr)
508{
509 struct i2c_client *client = to_i2c_client(dev);
510 struct adm1031_data *data = i2c_get_clientdata(client);
511 int val = simple_strtol(buf, NULL, 10);
512
513 down(&data->update_lock);
514 if (val) {
515 data->fan_min[nr] =
516 FAN_TO_REG(val, FAN_DIV_FROM_REG(data->fan_div[nr]));
517 } else {
518 data->fan_min[nr] = 0xff;
519 }
520 adm1031_write_value(client, ADM1031_REG_FAN_MIN(nr), data->fan_min[nr]);
521 up(&data->update_lock);
522 return count;
523}
524static ssize_t
525set_fan_div(struct device *dev, const char *buf, size_t count, int nr)
526{
527 struct i2c_client *client = to_i2c_client(dev);
528 struct adm1031_data *data = i2c_get_clientdata(client);
529 int val = simple_strtol(buf, NULL, 10);
530 u8 tmp;
531 int old_div;
532 int new_min;
533
534 tmp = val == 8 ? 0xc0 :
535 val == 4 ? 0x80 :
536 val == 2 ? 0x40 :
537 val == 1 ? 0x00 :
538 0xff;
539 if (tmp == 0xff)
540 return -EINVAL;
541
542 down(&data->update_lock);
543 old_div = FAN_DIV_FROM_REG(data->fan_div[nr]);
544 data->fan_div[nr] = (tmp & 0xC0) | (0x3f & data->fan_div[nr]);
545 new_min = data->fan_min[nr] * old_div /
546 FAN_DIV_FROM_REG(data->fan_div[nr]);
547 data->fan_min[nr] = new_min > 0xff ? 0xff : new_min;
548 data->fan[nr] = data->fan[nr] * old_div /
549 FAN_DIV_FROM_REG(data->fan_div[nr]);
550
551 adm1031_write_value(client, ADM1031_REG_FAN_DIV(nr),
552 data->fan_div[nr]);
553 adm1031_write_value(client, ADM1031_REG_FAN_MIN(nr),
554 data->fan_min[nr]);
555 up(&data->update_lock);
556 return count;
557}
558
559#define fan_offset(offset) \
560static ssize_t show_fan_##offset (struct device *dev, char *buf) \
561{ \
562 return show_fan(dev, buf, offset - 1); \
563} \
564static ssize_t show_fan_##offset##_min (struct device *dev, char *buf) \
565{ \
566 return show_fan_min(dev, buf, offset - 1); \
567} \
568static ssize_t show_fan_##offset##_div (struct device *dev, char *buf) \
569{ \
570 return show_fan_div(dev, buf, offset - 1); \
571} \
572static ssize_t set_fan_##offset##_min (struct device *dev, \
573 const char *buf, size_t count) \
574{ \
575 return set_fan_min(dev, buf, count, offset - 1); \
576} \
577static ssize_t set_fan_##offset##_div (struct device *dev, \
578 const char *buf, size_t count) \
579{ \
580 return set_fan_div(dev, buf, count, offset - 1); \
581} \
582static DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_fan_##offset, \
583 NULL); \
584static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
585 show_fan_##offset##_min, set_fan_##offset##_min); \
586static DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
587 show_fan_##offset##_div, set_fan_##offset##_div); \
588static DEVICE_ATTR(auto_fan##offset##_min_pwm, S_IRUGO | S_IWUSR, \
589 show_pwm_##offset, set_pwm_##offset)
590
591fan_offset(1);
592fan_offset(2);
593
594
595/* Temps */
596static ssize_t show_temp(struct device *dev, char *buf, int nr)
597{
598 struct adm1031_data *data = adm1031_update_device(dev);
599 int ext;
600 ext = nr == 0 ?
601 ((data->ext_temp[nr] >> 6) & 0x3) * 2 :
602 (((data->ext_temp[nr] >> ((nr - 1) * 3)) & 7));
603 return sprintf(buf, "%d\n", TEMP_FROM_REG_EXT(data->temp[nr], ext));
604}
605static ssize_t show_temp_min(struct device *dev, char *buf, int nr)
606{
607 struct adm1031_data *data = adm1031_update_device(dev);
608 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[nr]));
609}
610static ssize_t show_temp_max(struct device *dev, char *buf, int nr)
611{
612 struct adm1031_data *data = adm1031_update_device(dev);
613 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[nr]));
614}
615static ssize_t show_temp_crit(struct device *dev, char *buf, int nr)
616{
617 struct adm1031_data *data = adm1031_update_device(dev);
618 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit[nr]));
619}
620static ssize_t
621set_temp_min(struct device *dev, const char *buf, size_t count, int nr)
622{
623 struct i2c_client *client = to_i2c_client(dev);
624 struct adm1031_data *data = i2c_get_clientdata(client);
625 int val;
626
627 val = simple_strtol(buf, NULL, 10);
628 val = SENSORS_LIMIT(val, -55000, nr == 0 ? 127750 : 127875);
629 down(&data->update_lock);
630 data->temp_min[nr] = TEMP_TO_REG(val);
631 adm1031_write_value(client, ADM1031_REG_TEMP_MIN(nr),
632 data->temp_min[nr]);
633 up(&data->update_lock);
634 return count;
635}
636static ssize_t
637set_temp_max(struct device *dev, const char *buf, size_t count, int nr)
638{
639 struct i2c_client *client = to_i2c_client(dev);
640 struct adm1031_data *data = i2c_get_clientdata(client);
641 int val;
642
643 val = simple_strtol(buf, NULL, 10);
644 val = SENSORS_LIMIT(val, -55000, nr == 0 ? 127750 : 127875);
645 down(&data->update_lock);
646 data->temp_max[nr] = TEMP_TO_REG(val);
647 adm1031_write_value(client, ADM1031_REG_TEMP_MAX(nr),
648 data->temp_max[nr]);
649 up(&data->update_lock);
650 return count;
651}
652static ssize_t
653set_temp_crit(struct device *dev, const char *buf, size_t count, int nr)
654{
655 struct i2c_client *client = to_i2c_client(dev);
656 struct adm1031_data *data = i2c_get_clientdata(client);
657 int val;
658
659 val = simple_strtol(buf, NULL, 10);
660 val = SENSORS_LIMIT(val, -55000, nr == 0 ? 127750 : 127875);
661 down(&data->update_lock);
662 data->temp_crit[nr] = TEMP_TO_REG(val);
663 adm1031_write_value(client, ADM1031_REG_TEMP_CRIT(nr),
664 data->temp_crit[nr]);
665 up(&data->update_lock);
666 return count;
667}
668
669#define temp_reg(offset) \
670static ssize_t show_temp_##offset (struct device *dev, char *buf) \
671{ \
672 return show_temp(dev, buf, offset - 1); \
673} \
674static ssize_t show_temp_##offset##_min (struct device *dev, char *buf) \
675{ \
676 return show_temp_min(dev, buf, offset - 1); \
677} \
678static ssize_t show_temp_##offset##_max (struct device *dev, char *buf) \
679{ \
680 return show_temp_max(dev, buf, offset - 1); \
681} \
682static ssize_t show_temp_##offset##_crit (struct device *dev, char *buf) \
683{ \
684 return show_temp_crit(dev, buf, offset - 1); \
685} \
686static ssize_t set_temp_##offset##_min (struct device *dev, \
687 const char *buf, size_t count) \
688{ \
689 return set_temp_min(dev, buf, count, offset - 1); \
690} \
691static ssize_t set_temp_##offset##_max (struct device *dev, \
692 const char *buf, size_t count) \
693{ \
694 return set_temp_max(dev, buf, count, offset - 1); \
695} \
696static ssize_t set_temp_##offset##_crit (struct device *dev, \
697 const char *buf, size_t count) \
698{ \
699 return set_temp_crit(dev, buf, count, offset - 1); \
700} \
701static DEVICE_ATTR(temp##offset##_input, S_IRUGO, show_temp_##offset, \
702 NULL); \
703static DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
704 show_temp_##offset##_min, set_temp_##offset##_min); \
705static DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
706 show_temp_##offset##_max, set_temp_##offset##_max); \
707static DEVICE_ATTR(temp##offset##_crit, S_IRUGO | S_IWUSR, \
708 show_temp_##offset##_crit, set_temp_##offset##_crit)
709
710temp_reg(1);
711temp_reg(2);
712temp_reg(3);
713
714/* Alarms */
715static ssize_t show_alarms(struct device *dev, char *buf)
716{
717 struct adm1031_data *data = adm1031_update_device(dev);
718 return sprintf(buf, "%d\n", data->alarm);
719}
720
721static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
722
723
724static int adm1031_attach_adapter(struct i2c_adapter *adapter)
725{
726 if (!(adapter->class & I2C_CLASS_HWMON))
727 return 0;
728 return i2c_detect(adapter, &addr_data, adm1031_detect);
729}
730
731/* This function is called by i2c_detect */
732static int adm1031_detect(struct i2c_adapter *adapter, int address, int kind)
733{
734 struct i2c_client *new_client;
735 struct adm1031_data *data;
736 int err = 0;
737 const char *name = "";
738
739 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
740 goto exit;
741
742 if (!(data = kmalloc(sizeof(struct adm1031_data), GFP_KERNEL))) {
743 err = -ENOMEM;
744 goto exit;
745 }
746 memset(data, 0, sizeof(struct adm1031_data));
747
748 new_client = &data->client;
749 i2c_set_clientdata(new_client, data);
750 new_client->addr = address;
751 new_client->adapter = adapter;
752 new_client->driver = &adm1031_driver;
753 new_client->flags = 0;
754
755 if (kind < 0) {
756 int id, co;
757 id = i2c_smbus_read_byte_data(new_client, 0x3d);
758 co = i2c_smbus_read_byte_data(new_client, 0x3e);
759
760 if (!((id == 0x31 || id == 0x30) && co == 0x41))
761 goto exit_free;
762 kind = (id == 0x30) ? adm1030 : adm1031;
763 }
764
765 if (kind <= 0)
766 kind = adm1031;
767
768 /* Given the detected chip type, set the chip name and the
769 * auto fan control helper table. */
770 if (kind == adm1030) {
771 name = "adm1030";
772 data->chan_select_table = &auto_channel_select_table_adm1030;
773 } else if (kind == adm1031) {
774 name = "adm1031";
775 data->chan_select_table = &auto_channel_select_table_adm1031;
776 }
777 data->chip_type = kind;
778
779 strlcpy(new_client->name, name, I2C_NAME_SIZE);
780 data->valid = 0;
781 init_MUTEX(&data->update_lock);
782
783 /* Tell the I2C layer a new client has arrived */
784 if ((err = i2c_attach_client(new_client)))
785 goto exit_free;
786
787 /* Initialize the ADM1031 chip */
788 adm1031_init_client(new_client);
789
790 /* Register sysfs hooks */
791 device_create_file(&new_client->dev, &dev_attr_fan1_input);
792 device_create_file(&new_client->dev, &dev_attr_fan1_div);
793 device_create_file(&new_client->dev, &dev_attr_fan1_min);
794 device_create_file(&new_client->dev, &dev_attr_pwm1);
795 device_create_file(&new_client->dev, &dev_attr_auto_fan1_channel);
796 device_create_file(&new_client->dev, &dev_attr_temp1_input);
797 device_create_file(&new_client->dev, &dev_attr_temp1_min);
798 device_create_file(&new_client->dev, &dev_attr_temp1_max);
799 device_create_file(&new_client->dev, &dev_attr_temp1_crit);
800 device_create_file(&new_client->dev, &dev_attr_temp2_input);
801 device_create_file(&new_client->dev, &dev_attr_temp2_min);
802 device_create_file(&new_client->dev, &dev_attr_temp2_max);
803 device_create_file(&new_client->dev, &dev_attr_temp2_crit);
804
805 device_create_file(&new_client->dev, &dev_attr_auto_temp1_off);
806 device_create_file(&new_client->dev, &dev_attr_auto_temp1_min);
807 device_create_file(&new_client->dev, &dev_attr_auto_temp1_max);
808
809 device_create_file(&new_client->dev, &dev_attr_auto_temp2_off);
810 device_create_file(&new_client->dev, &dev_attr_auto_temp2_min);
811 device_create_file(&new_client->dev, &dev_attr_auto_temp2_max);
812
813 device_create_file(&new_client->dev, &dev_attr_auto_fan1_min_pwm);
814
815 device_create_file(&new_client->dev, &dev_attr_alarms);
816
817 if (kind == adm1031) {
818 device_create_file(&new_client->dev, &dev_attr_fan2_input);
819 device_create_file(&new_client->dev, &dev_attr_fan2_div);
820 device_create_file(&new_client->dev, &dev_attr_fan2_min);
821 device_create_file(&new_client->dev, &dev_attr_pwm2);
822 device_create_file(&new_client->dev,
823 &dev_attr_auto_fan2_channel);
824 device_create_file(&new_client->dev, &dev_attr_temp3_input);
825 device_create_file(&new_client->dev, &dev_attr_temp3_min);
826 device_create_file(&new_client->dev, &dev_attr_temp3_max);
827 device_create_file(&new_client->dev, &dev_attr_temp3_crit);
828 device_create_file(&new_client->dev, &dev_attr_auto_temp3_off);
829 device_create_file(&new_client->dev, &dev_attr_auto_temp3_min);
830 device_create_file(&new_client->dev, &dev_attr_auto_temp3_max);
831 device_create_file(&new_client->dev, &dev_attr_auto_fan2_min_pwm);
832 }
833
834 return 0;
835
836exit_free:
837 kfree(new_client);
838exit:
839 return err;
840}
841
842static int adm1031_detach_client(struct i2c_client *client)
843{
844 int ret;
845 if ((ret = i2c_detach_client(client)) != 0) {
846 return ret;
847 }
848 kfree(client);
849 return 0;
850}
851
852static void adm1031_init_client(struct i2c_client *client)
853{
854 unsigned int read_val;
855 unsigned int mask;
856 struct adm1031_data *data = i2c_get_clientdata(client);
857
858 mask = (ADM1031_CONF2_PWM1_ENABLE | ADM1031_CONF2_TACH1_ENABLE);
859 if (data->chip_type == adm1031) {
860 mask |= (ADM1031_CONF2_PWM2_ENABLE |
861 ADM1031_CONF2_TACH2_ENABLE);
862 }
863 /* Initialize the ADM1031 chip (enables fan speed reading ) */
864 read_val = adm1031_read_value(client, ADM1031_REG_CONF2);
865 if ((read_val | mask) != read_val) {
866 adm1031_write_value(client, ADM1031_REG_CONF2, read_val | mask);
867 }
868
869 read_val = adm1031_read_value(client, ADM1031_REG_CONF1);
870 if ((read_val | ADM1031_CONF1_MONITOR_ENABLE) != read_val) {
871 adm1031_write_value(client, ADM1031_REG_CONF1, read_val |
872 ADM1031_CONF1_MONITOR_ENABLE);
873 }
874
875}
876
877static struct adm1031_data *adm1031_update_device(struct device *dev)
878{
879 struct i2c_client *client = to_i2c_client(dev);
880 struct adm1031_data *data = i2c_get_clientdata(client);
881 int chan;
882
883 down(&data->update_lock);
884
885 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
886 || !data->valid) {
887
888 dev_dbg(&client->dev, "Starting adm1031 update\n");
889 for (chan = 0;
890 chan < ((data->chip_type == adm1031) ? 3 : 2); chan++) {
891 u8 oldh, newh;
892
893 oldh =
894 adm1031_read_value(client, ADM1031_REG_TEMP(chan));
895 data->ext_temp[chan] =
896 adm1031_read_value(client, ADM1031_REG_EXT_TEMP);
897 newh =
898 adm1031_read_value(client, ADM1031_REG_TEMP(chan));
899 if (newh != oldh) {
900 data->ext_temp[chan] =
901 adm1031_read_value(client,
902 ADM1031_REG_EXT_TEMP);
903#ifdef DEBUG
904 oldh =
905 adm1031_read_value(client,
906 ADM1031_REG_TEMP(chan));
907
908 /* oldh is actually newer */
909 if (newh != oldh)
910 dev_warn(&client->dev,
911 "Remote temperature may be "
912 "wrong.\n");
913#endif
914 }
915 data->temp[chan] = newh;
916
917 data->temp_min[chan] =
918 adm1031_read_value(client,
919 ADM1031_REG_TEMP_MIN(chan));
920 data->temp_max[chan] =
921 adm1031_read_value(client,
922 ADM1031_REG_TEMP_MAX(chan));
923 data->temp_crit[chan] =
924 adm1031_read_value(client,
925 ADM1031_REG_TEMP_CRIT(chan));
926 data->auto_temp[chan] =
927 adm1031_read_value(client,
928 ADM1031_REG_AUTO_TEMP(chan));
929
930 }
931
932 data->conf1 = adm1031_read_value(client, ADM1031_REG_CONF1);
933 data->conf2 = adm1031_read_value(client, ADM1031_REG_CONF2);
934
935 data->alarm = adm1031_read_value(client, ADM1031_REG_STATUS(0))
936 | (adm1031_read_value(client, ADM1031_REG_STATUS(1))
937 << 8);
938 if (data->chip_type == adm1030) {
939 data->alarm &= 0xc0ff;
940 }
941
942 for (chan=0; chan<(data->chip_type == adm1030 ? 1 : 2); chan++) {
943 data->fan_div[chan] =
944 adm1031_read_value(client, ADM1031_REG_FAN_DIV(chan));
945 data->fan_min[chan] =
946 adm1031_read_value(client, ADM1031_REG_FAN_MIN(chan));
947 data->fan[chan] =
948 adm1031_read_value(client, ADM1031_REG_FAN_SPEED(chan));
949 data->pwm[chan] =
950 0xf & (adm1031_read_value(client, ADM1031_REG_PWM) >>
951 (4*chan));
952 }
953 data->last_updated = jiffies;
954 data->valid = 1;
955 }
956
957 up(&data->update_lock);
958
959 return data;
960}
961
962static int __init sensors_adm1031_init(void)
963{
964 return i2c_add_driver(&adm1031_driver);
965}
966
967static void __exit sensors_adm1031_exit(void)
968{
969 i2c_del_driver(&adm1031_driver);
970}
971
972MODULE_AUTHOR("Alexandre d'Alton <alex@alexdalton.org>");
973MODULE_DESCRIPTION("ADM1031/ADM1030 driver");
974MODULE_LICENSE("GPL");
975
976module_init(sensors_adm1031_init);
977module_exit(sensors_adm1031_exit);
diff --git a/drivers/i2c/chips/asb100.c b/drivers/i2c/chips/asb100.c
new file mode 100644
index 000000000000..7f899002bc54
--- /dev/null
+++ b/drivers/i2c/chips/asb100.c
@@ -0,0 +1,1066 @@
1/*
2 asb100.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring
4
5 Copyright (C) 2004 Mark M. Hoffman <mhoffman@lightlink.com>
6
7 (derived from w83781d.c)
8
9 Copyright (C) 1998 - 2003 Frodo Looijaard <frodol@dds.nl>,
10 Philip Edelbrock <phil@netroedge.com>, and
11 Mark Studebaker <mdsxyz123@yahoo.com>
12
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
17
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26*/
27
28/*
29 This driver supports the hardware sensor chips: Asus ASB100 and
30 ASB100-A "BACH".
31
32 ASB100-A supports pwm1, while plain ASB100 does not. There is no known
33 way for the driver to tell which one is there.
34
35 Chip #vin #fanin #pwm #temp wchipid vendid i2c ISA
36 asb100 7 3 1 4 0x31 0x0694 yes no
37*/
38
39#include <linux/module.h>
40#include <linux/slab.h>
41#include <linux/i2c.h>
42#include <linux/i2c-sensor.h>
43#include <linux/i2c-vid.h>
44#include <linux/init.h>
45#include "lm75.h"
46
47/*
48 HISTORY:
49 2003-12-29 1.0.0 Ported from lm_sensors project for kernel 2.6
50*/
51#define ASB100_VERSION "1.0.0"
52
53/* I2C addresses to scan */
54static unsigned short normal_i2c[] = { 0x2d, I2C_CLIENT_END };
55
56/* ISA addresses to scan (none) */
57static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
58
59/* Insmod parameters */
60SENSORS_INSMOD_1(asb100);
61I2C_CLIENT_MODULE_PARM(force_subclients, "List of subclient addresses: "
62 "{bus, clientaddr, subclientaddr1, subclientaddr2}");
63
64/* Voltage IN registers 0-6 */
65#define ASB100_REG_IN(nr) (0x20 + (nr))
66#define ASB100_REG_IN_MAX(nr) (0x2b + (nr * 2))
67#define ASB100_REG_IN_MIN(nr) (0x2c + (nr * 2))
68
69/* FAN IN registers 1-3 */
70#define ASB100_REG_FAN(nr) (0x28 + (nr))
71#define ASB100_REG_FAN_MIN(nr) (0x3b + (nr))
72
73/* TEMPERATURE registers 1-4 */
74static const u16 asb100_reg_temp[] = {0, 0x27, 0x150, 0x250, 0x17};
75static const u16 asb100_reg_temp_max[] = {0, 0x39, 0x155, 0x255, 0x18};
76static const u16 asb100_reg_temp_hyst[] = {0, 0x3a, 0x153, 0x253, 0x19};
77
78#define ASB100_REG_TEMP(nr) (asb100_reg_temp[nr])
79#define ASB100_REG_TEMP_MAX(nr) (asb100_reg_temp_max[nr])
80#define ASB100_REG_TEMP_HYST(nr) (asb100_reg_temp_hyst[nr])
81
82#define ASB100_REG_TEMP2_CONFIG 0x0152
83#define ASB100_REG_TEMP3_CONFIG 0x0252
84
85
86#define ASB100_REG_CONFIG 0x40
87#define ASB100_REG_ALARM1 0x41
88#define ASB100_REG_ALARM2 0x42
89#define ASB100_REG_SMIM1 0x43
90#define ASB100_REG_SMIM2 0x44
91#define ASB100_REG_VID_FANDIV 0x47
92#define ASB100_REG_I2C_ADDR 0x48
93#define ASB100_REG_CHIPID 0x49
94#define ASB100_REG_I2C_SUBADDR 0x4a
95#define ASB100_REG_PIN 0x4b
96#define ASB100_REG_IRQ 0x4c
97#define ASB100_REG_BANK 0x4e
98#define ASB100_REG_CHIPMAN 0x4f
99
100#define ASB100_REG_WCHIPID 0x58
101
102/* bit 7 -> enable, bits 0-3 -> duty cycle */
103#define ASB100_REG_PWM1 0x59
104
105/* CONVERSIONS
106 Rounding and limit checking is only done on the TO_REG variants. */
107
108/* These constants are a guess, consistent w/ w83781d */
109#define ASB100_IN_MIN ( 0)
110#define ASB100_IN_MAX (4080)
111
112/* IN: 1/1000 V (0V to 4.08V)
113 REG: 16mV/bit */
114static u8 IN_TO_REG(unsigned val)
115{
116 unsigned nval = SENSORS_LIMIT(val, ASB100_IN_MIN, ASB100_IN_MAX);
117 return (nval + 8) / 16;
118}
119
120static unsigned IN_FROM_REG(u8 reg)
121{
122 return reg * 16;
123}
124
125static u8 FAN_TO_REG(long rpm, int div)
126{
127 if (rpm == -1)
128 return 0;
129 if (rpm == 0)
130 return 255;
131 rpm = SENSORS_LIMIT(rpm, 1, 1000000);
132 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
133}
134
135static int FAN_FROM_REG(u8 val, int div)
136{
137 return val==0 ? -1 : val==255 ? 0 : 1350000/(val*div);
138}
139
140/* These constants are a guess, consistent w/ w83781d */
141#define ASB100_TEMP_MIN (-128000)
142#define ASB100_TEMP_MAX ( 127000)
143
144/* TEMP: 0.001C/bit (-128C to +127C)
145 REG: 1C/bit, two's complement */
146static u8 TEMP_TO_REG(int temp)
147{
148 int ntemp = SENSORS_LIMIT(temp, ASB100_TEMP_MIN, ASB100_TEMP_MAX);
149 ntemp += (ntemp<0 ? -500 : 500);
150 return (u8)(ntemp / 1000);
151}
152
153static int TEMP_FROM_REG(u8 reg)
154{
155 return (s8)reg * 1000;
156}
157
158/* PWM: 0 - 255 per sensors documentation
159 REG: (6.25% duty cycle per bit) */
160static u8 ASB100_PWM_TO_REG(int pwm)
161{
162 pwm = SENSORS_LIMIT(pwm, 0, 255);
163 return (u8)(pwm / 16);
164}
165
166static int ASB100_PWM_FROM_REG(u8 reg)
167{
168 return reg * 16;
169}
170
171#define ALARMS_FROM_REG(val) (val)
172
173#define DIV_FROM_REG(val) (1 << (val))
174
175/* FAN DIV: 1, 2, 4, or 8 (defaults to 2)
176 REG: 0, 1, 2, or 3 (respectively) (defaults to 1) */
177static u8 DIV_TO_REG(long val)
178{
179 return val==8 ? 3 : val==4 ? 2 : val==1 ? 0 : 1;
180}
181
182/* For each registered client, we need to keep some data in memory. That
183 data is pointed to by client->data. The structure itself is
184 dynamically allocated, at the same time the client itself is allocated. */
185struct asb100_data {
186 struct i2c_client client;
187 struct semaphore lock;
188 enum chips type;
189
190 struct semaphore update_lock;
191 unsigned long last_updated; /* In jiffies */
192
193 /* array of 2 pointers to subclients */
194 struct i2c_client *lm75[2];
195
196 char valid; /* !=0 if following fields are valid */
197 u8 in[7]; /* Register value */
198 u8 in_max[7]; /* Register value */
199 u8 in_min[7]; /* Register value */
200 u8 fan[3]; /* Register value */
201 u8 fan_min[3]; /* Register value */
202 u16 temp[4]; /* Register value (0 and 3 are u8 only) */
203 u16 temp_max[4]; /* Register value (0 and 3 are u8 only) */
204 u16 temp_hyst[4]; /* Register value (0 and 3 are u8 only) */
205 u8 fan_div[3]; /* Register encoding, right justified */
206 u8 pwm; /* Register encoding */
207 u8 vid; /* Register encoding, combined */
208 u32 alarms; /* Register encoding, combined */
209 u8 vrm;
210};
211
212static int asb100_read_value(struct i2c_client *client, u16 reg);
213static void asb100_write_value(struct i2c_client *client, u16 reg, u16 val);
214
215static int asb100_attach_adapter(struct i2c_adapter *adapter);
216static int asb100_detect(struct i2c_adapter *adapter, int address, int kind);
217static int asb100_detach_client(struct i2c_client *client);
218static struct asb100_data *asb100_update_device(struct device *dev);
219static void asb100_init_client(struct i2c_client *client);
220
221static struct i2c_driver asb100_driver = {
222 .owner = THIS_MODULE,
223 .name = "asb100",
224 .id = I2C_DRIVERID_ASB100,
225 .flags = I2C_DF_NOTIFY,
226 .attach_adapter = asb100_attach_adapter,
227 .detach_client = asb100_detach_client,
228};
229
230/* 7 Voltages */
231#define show_in_reg(reg) \
232static ssize_t show_##reg (struct device *dev, char *buf, int nr) \
233{ \
234 struct asb100_data *data = asb100_update_device(dev); \
235 return sprintf(buf, "%d\n", IN_FROM_REG(data->reg[nr])); \
236}
237
238show_in_reg(in)
239show_in_reg(in_min)
240show_in_reg(in_max)
241
242#define set_in_reg(REG, reg) \
243static ssize_t set_in_##reg(struct device *dev, const char *buf, \
244 size_t count, int nr) \
245{ \
246 struct i2c_client *client = to_i2c_client(dev); \
247 struct asb100_data *data = i2c_get_clientdata(client); \
248 unsigned long val = simple_strtoul(buf, NULL, 10); \
249 \
250 down(&data->update_lock); \
251 data->in_##reg[nr] = IN_TO_REG(val); \
252 asb100_write_value(client, ASB100_REG_IN_##REG(nr), \
253 data->in_##reg[nr]); \
254 up(&data->update_lock); \
255 return count; \
256}
257
258set_in_reg(MIN, min)
259set_in_reg(MAX, max)
260
261#define sysfs_in(offset) \
262static ssize_t \
263 show_in##offset (struct device *dev, char *buf) \
264{ \
265 return show_in(dev, buf, offset); \
266} \
267static DEVICE_ATTR(in##offset##_input, S_IRUGO, \
268 show_in##offset, NULL); \
269static ssize_t \
270 show_in##offset##_min (struct device *dev, char *buf) \
271{ \
272 return show_in_min(dev, buf, offset); \
273} \
274static ssize_t \
275 show_in##offset##_max (struct device *dev, char *buf) \
276{ \
277 return show_in_max(dev, buf, offset); \
278} \
279static ssize_t set_in##offset##_min (struct device *dev, \
280 const char *buf, size_t count) \
281{ \
282 return set_in_min(dev, buf, count, offset); \
283} \
284static ssize_t set_in##offset##_max (struct device *dev, \
285 const char *buf, size_t count) \
286{ \
287 return set_in_max(dev, buf, count, offset); \
288} \
289static DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
290 show_in##offset##_min, set_in##offset##_min); \
291static DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
292 show_in##offset##_max, set_in##offset##_max);
293
294sysfs_in(0);
295sysfs_in(1);
296sysfs_in(2);
297sysfs_in(3);
298sysfs_in(4);
299sysfs_in(5);
300sysfs_in(6);
301
302#define device_create_file_in(client, offset) do { \
303 device_create_file(&client->dev, &dev_attr_in##offset##_input); \
304 device_create_file(&client->dev, &dev_attr_in##offset##_min); \
305 device_create_file(&client->dev, &dev_attr_in##offset##_max); \
306} while (0)
307
308/* 3 Fans */
309static ssize_t show_fan(struct device *dev, char *buf, int nr)
310{
311 struct asb100_data *data = asb100_update_device(dev);
312 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
313 DIV_FROM_REG(data->fan_div[nr])));
314}
315
316static ssize_t show_fan_min(struct device *dev, char *buf, int nr)
317{
318 struct asb100_data *data = asb100_update_device(dev);
319 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
320 DIV_FROM_REG(data->fan_div[nr])));
321}
322
323static ssize_t show_fan_div(struct device *dev, char *buf, int nr)
324{
325 struct asb100_data *data = asb100_update_device(dev);
326 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
327}
328
329static ssize_t set_fan_min(struct device *dev, const char *buf,
330 size_t count, int nr)
331{
332 struct i2c_client *client = to_i2c_client(dev);
333 struct asb100_data *data = i2c_get_clientdata(client);
334 u32 val = simple_strtoul(buf, NULL, 10);
335
336 down(&data->update_lock);
337 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
338 asb100_write_value(client, ASB100_REG_FAN_MIN(nr), data->fan_min[nr]);
339 up(&data->update_lock);
340 return count;
341}
342
343/* Note: we save and restore the fan minimum here, because its value is
344 determined in part by the fan divisor. This follows the principle of
345 least suprise; the user doesn't expect the fan minimum to change just
346 because the divisor changed. */
347static ssize_t set_fan_div(struct device *dev, const char *buf,
348 size_t count, int nr)
349{
350 struct i2c_client *client = to_i2c_client(dev);
351 struct asb100_data *data = i2c_get_clientdata(client);
352 unsigned long min;
353 unsigned long val = simple_strtoul(buf, NULL, 10);
354 int reg;
355
356 down(&data->update_lock);
357
358 min = FAN_FROM_REG(data->fan_min[nr],
359 DIV_FROM_REG(data->fan_div[nr]));
360 data->fan_div[nr] = DIV_TO_REG(val);
361
362 switch(nr) {
363 case 0: /* fan 1 */
364 reg = asb100_read_value(client, ASB100_REG_VID_FANDIV);
365 reg = (reg & 0xcf) | (data->fan_div[0] << 4);
366 asb100_write_value(client, ASB100_REG_VID_FANDIV, reg);
367 break;
368
369 case 1: /* fan 2 */
370 reg = asb100_read_value(client, ASB100_REG_VID_FANDIV);
371 reg = (reg & 0x3f) | (data->fan_div[1] << 6);
372 asb100_write_value(client, ASB100_REG_VID_FANDIV, reg);
373 break;
374
375 case 2: /* fan 3 */
376 reg = asb100_read_value(client, ASB100_REG_PIN);
377 reg = (reg & 0x3f) | (data->fan_div[2] << 6);
378 asb100_write_value(client, ASB100_REG_PIN, reg);
379 break;
380 }
381
382 data->fan_min[nr] =
383 FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
384 asb100_write_value(client, ASB100_REG_FAN_MIN(nr), data->fan_min[nr]);
385
386 up(&data->update_lock);
387
388 return count;
389}
390
391#define sysfs_fan(offset) \
392static ssize_t show_fan##offset(struct device *dev, char *buf) \
393{ \
394 return show_fan(dev, buf, offset - 1); \
395} \
396static ssize_t show_fan##offset##_min(struct device *dev, char *buf) \
397{ \
398 return show_fan_min(dev, buf, offset - 1); \
399} \
400static ssize_t show_fan##offset##_div(struct device *dev, char *buf) \
401{ \
402 return show_fan_div(dev, buf, offset - 1); \
403} \
404static ssize_t set_fan##offset##_min(struct device *dev, const char *buf, \
405 size_t count) \
406{ \
407 return set_fan_min(dev, buf, count, offset - 1); \
408} \
409static ssize_t set_fan##offset##_div(struct device *dev, const char *buf, \
410 size_t count) \
411{ \
412 return set_fan_div(dev, buf, count, offset - 1); \
413} \
414static DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
415 show_fan##offset, NULL); \
416static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
417 show_fan##offset##_min, set_fan##offset##_min); \
418static DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
419 show_fan##offset##_div, set_fan##offset##_div);
420
421sysfs_fan(1);
422sysfs_fan(2);
423sysfs_fan(3);
424
425#define device_create_file_fan(client, offset) do { \
426 device_create_file(&client->dev, &dev_attr_fan##offset##_input); \
427 device_create_file(&client->dev, &dev_attr_fan##offset##_min); \
428 device_create_file(&client->dev, &dev_attr_fan##offset##_div); \
429} while (0)
430
431/* 4 Temp. Sensors */
432static int sprintf_temp_from_reg(u16 reg, char *buf, int nr)
433{
434 int ret = 0;
435
436 switch (nr) {
437 case 1: case 2:
438 ret = sprintf(buf, "%d\n", LM75_TEMP_FROM_REG(reg));
439 break;
440 case 0: case 3: default:
441 ret = sprintf(buf, "%d\n", TEMP_FROM_REG(reg));
442 break;
443 }
444 return ret;
445}
446
447#define show_temp_reg(reg) \
448static ssize_t show_##reg(struct device *dev, char *buf, int nr) \
449{ \
450 struct asb100_data *data = asb100_update_device(dev); \
451 return sprintf_temp_from_reg(data->reg[nr], buf, nr); \
452}
453
454show_temp_reg(temp);
455show_temp_reg(temp_max);
456show_temp_reg(temp_hyst);
457
458#define set_temp_reg(REG, reg) \
459static ssize_t set_##reg(struct device *dev, const char *buf, \
460 size_t count, int nr) \
461{ \
462 struct i2c_client *client = to_i2c_client(dev); \
463 struct asb100_data *data = i2c_get_clientdata(client); \
464 unsigned long val = simple_strtoul(buf, NULL, 10); \
465 \
466 down(&data->update_lock); \
467 switch (nr) { \
468 case 1: case 2: \
469 data->reg[nr] = LM75_TEMP_TO_REG(val); \
470 break; \
471 case 0: case 3: default: \
472 data->reg[nr] = TEMP_TO_REG(val); \
473 break; \
474 } \
475 asb100_write_value(client, ASB100_REG_TEMP_##REG(nr+1), \
476 data->reg[nr]); \
477 up(&data->update_lock); \
478 return count; \
479}
480
481set_temp_reg(MAX, temp_max);
482set_temp_reg(HYST, temp_hyst);
483
484#define sysfs_temp(num) \
485static ssize_t show_temp##num(struct device *dev, char *buf) \
486{ \
487 return show_temp(dev, buf, num-1); \
488} \
489static DEVICE_ATTR(temp##num##_input, S_IRUGO, show_temp##num, NULL); \
490static ssize_t show_temp_max##num(struct device *dev, char *buf) \
491{ \
492 return show_temp_max(dev, buf, num-1); \
493} \
494static ssize_t set_temp_max##num(struct device *dev, const char *buf, \
495 size_t count) \
496{ \
497 return set_temp_max(dev, buf, count, num-1); \
498} \
499static DEVICE_ATTR(temp##num##_max, S_IRUGO | S_IWUSR, \
500 show_temp_max##num, set_temp_max##num); \
501static ssize_t show_temp_hyst##num(struct device *dev, char *buf) \
502{ \
503 return show_temp_hyst(dev, buf, num-1); \
504} \
505static ssize_t set_temp_hyst##num(struct device *dev, const char *buf, \
506 size_t count) \
507{ \
508 return set_temp_hyst(dev, buf, count, num-1); \
509} \
510static DEVICE_ATTR(temp##num##_max_hyst, S_IRUGO | S_IWUSR, \
511 show_temp_hyst##num, set_temp_hyst##num);
512
513sysfs_temp(1);
514sysfs_temp(2);
515sysfs_temp(3);
516sysfs_temp(4);
517
518/* VID */
519#define device_create_file_temp(client, num) do { \
520 device_create_file(&client->dev, &dev_attr_temp##num##_input); \
521 device_create_file(&client->dev, &dev_attr_temp##num##_max); \
522 device_create_file(&client->dev, &dev_attr_temp##num##_max_hyst); \
523} while (0)
524
525static ssize_t show_vid(struct device *dev, char *buf)
526{
527 struct asb100_data *data = asb100_update_device(dev);
528 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
529}
530
531static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
532#define device_create_file_vid(client) \
533device_create_file(&client->dev, &dev_attr_cpu0_vid)
534
535/* VRM */
536static ssize_t show_vrm(struct device *dev, char *buf)
537{
538 struct asb100_data *data = asb100_update_device(dev);
539 return sprintf(buf, "%d\n", data->vrm);
540}
541
542static ssize_t set_vrm(struct device *dev, const char *buf, size_t count)
543{
544 struct i2c_client *client = to_i2c_client(dev);
545 struct asb100_data *data = i2c_get_clientdata(client);
546 unsigned long val = simple_strtoul(buf, NULL, 10);
547 data->vrm = val;
548 return count;
549}
550
551/* Alarms */
552static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm);
553#define device_create_file_vrm(client) \
554device_create_file(&client->dev, &dev_attr_vrm);
555
556static ssize_t show_alarms(struct device *dev, char *buf)
557{
558 struct asb100_data *data = asb100_update_device(dev);
559 return sprintf(buf, "%d\n", ALARMS_FROM_REG(data->alarms));
560}
561
562static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
563#define device_create_file_alarms(client) \
564device_create_file(&client->dev, &dev_attr_alarms)
565
566/* 1 PWM */
567static ssize_t show_pwm1(struct device *dev, char *buf)
568{
569 struct asb100_data *data = asb100_update_device(dev);
570 return sprintf(buf, "%d\n", ASB100_PWM_FROM_REG(data->pwm & 0x0f));
571}
572
573static ssize_t set_pwm1(struct device *dev, const char *buf, size_t count)
574{
575 struct i2c_client *client = to_i2c_client(dev);
576 struct asb100_data *data = i2c_get_clientdata(client);
577 unsigned long val = simple_strtoul(buf, NULL, 10);
578
579 down(&data->update_lock);
580 data->pwm &= 0x80; /* keep the enable bit */
581 data->pwm |= (0x0f & ASB100_PWM_TO_REG(val));
582 asb100_write_value(client, ASB100_REG_PWM1, data->pwm);
583 up(&data->update_lock);
584 return count;
585}
586
587static ssize_t show_pwm_enable1(struct device *dev, char *buf)
588{
589 struct asb100_data *data = asb100_update_device(dev);
590 return sprintf(buf, "%d\n", (data->pwm & 0x80) ? 1 : 0);
591}
592
593static ssize_t set_pwm_enable1(struct device *dev, const char *buf,
594 size_t count)
595{
596 struct i2c_client *client = to_i2c_client(dev);
597 struct asb100_data *data = i2c_get_clientdata(client);
598 unsigned long val = simple_strtoul(buf, NULL, 10);
599
600 down(&data->update_lock);
601 data->pwm &= 0x0f; /* keep the duty cycle bits */
602 data->pwm |= (val ? 0x80 : 0x00);
603 asb100_write_value(client, ASB100_REG_PWM1, data->pwm);
604 up(&data->update_lock);
605 return count;
606}
607
608static DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm1, set_pwm1);
609static DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
610 show_pwm_enable1, set_pwm_enable1);
611#define device_create_file_pwm1(client) do { \
612 device_create_file(&new_client->dev, &dev_attr_pwm1); \
613 device_create_file(&new_client->dev, &dev_attr_pwm1_enable); \
614} while (0)
615
616/* This function is called when:
617 asb100_driver is inserted (when this module is loaded), for each
618 available adapter
619 when a new adapter is inserted (and asb100_driver is still present)
620 */
621static int asb100_attach_adapter(struct i2c_adapter *adapter)
622{
623 if (!(adapter->class & I2C_CLASS_HWMON))
624 return 0;
625 return i2c_detect(adapter, &addr_data, asb100_detect);
626}
627
628static int asb100_detect_subclients(struct i2c_adapter *adapter, int address,
629 int kind, struct i2c_client *new_client)
630{
631 int i, id, err;
632 struct asb100_data *data = i2c_get_clientdata(new_client);
633
634 data->lm75[0] = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
635 if (!(data->lm75[0])) {
636 err = -ENOMEM;
637 goto ERROR_SC_0;
638 }
639 memset(data->lm75[0], 0x00, sizeof(struct i2c_client));
640
641 data->lm75[1] = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
642 if (!(data->lm75[1])) {
643 err = -ENOMEM;
644 goto ERROR_SC_1;
645 }
646 memset(data->lm75[1], 0x00, sizeof(struct i2c_client));
647
648 id = i2c_adapter_id(adapter);
649
650 if (force_subclients[0] == id && force_subclients[1] == address) {
651 for (i = 2; i <= 3; i++) {
652 if (force_subclients[i] < 0x48 ||
653 force_subclients[i] > 0x4f) {
654 dev_err(&new_client->dev, "invalid subclient "
655 "address %d; must be 0x48-0x4f\n",
656 force_subclients[i]);
657 err = -ENODEV;
658 goto ERROR_SC_2;
659 }
660 }
661 asb100_write_value(new_client, ASB100_REG_I2C_SUBADDR,
662 (force_subclients[2] & 0x07) |
663 ((force_subclients[3] & 0x07) <<4));
664 data->lm75[0]->addr = force_subclients[2];
665 data->lm75[1]->addr = force_subclients[3];
666 } else {
667 int val = asb100_read_value(new_client, ASB100_REG_I2C_SUBADDR);
668 data->lm75[0]->addr = 0x48 + (val & 0x07);
669 data->lm75[1]->addr = 0x48 + ((val >> 4) & 0x07);
670 }
671
672 if(data->lm75[0]->addr == data->lm75[1]->addr) {
673 dev_err(&new_client->dev, "duplicate addresses 0x%x "
674 "for subclients\n", data->lm75[0]->addr);
675 err = -ENODEV;
676 goto ERROR_SC_2;
677 }
678
679 for (i = 0; i <= 1; i++) {
680 i2c_set_clientdata(data->lm75[i], NULL);
681 data->lm75[i]->adapter = adapter;
682 data->lm75[i]->driver = &asb100_driver;
683 data->lm75[i]->flags = 0;
684 strlcpy(data->lm75[i]->name, "asb100 subclient", I2C_NAME_SIZE);
685 }
686
687 if ((err = i2c_attach_client(data->lm75[0]))) {
688 dev_err(&new_client->dev, "subclient %d registration "
689 "at address 0x%x failed.\n", i, data->lm75[0]->addr);
690 goto ERROR_SC_2;
691 }
692
693 if ((err = i2c_attach_client(data->lm75[1]))) {
694 dev_err(&new_client->dev, "subclient %d registration "
695 "at address 0x%x failed.\n", i, data->lm75[1]->addr);
696 goto ERROR_SC_3;
697 }
698
699 return 0;
700
701/* Undo inits in case of errors */
702ERROR_SC_3:
703 i2c_detach_client(data->lm75[0]);
704ERROR_SC_2:
705 kfree(data->lm75[1]);
706ERROR_SC_1:
707 kfree(data->lm75[0]);
708ERROR_SC_0:
709 return err;
710}
711
712static int asb100_detect(struct i2c_adapter *adapter, int address, int kind)
713{
714 int err;
715 struct i2c_client *new_client;
716 struct asb100_data *data;
717
718 /* asb100 is SMBus only */
719 if (i2c_is_isa_adapter(adapter)) {
720 pr_debug("asb100.o: detect failed, "
721 "cannot attach to legacy adapter!\n");
722 err = -ENODEV;
723 goto ERROR0;
724 }
725
726 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
727 pr_debug("asb100.o: detect failed, "
728 "smbus byte data not supported!\n");
729 err = -ENODEV;
730 goto ERROR0;
731 }
732
733 /* OK. For now, we presume we have a valid client. We now create the
734 client structure, even though we cannot fill it completely yet.
735 But it allows us to access asb100_{read,write}_value. */
736
737 if (!(data = kmalloc(sizeof(struct asb100_data), GFP_KERNEL))) {
738 pr_debug("asb100.o: detect failed, kmalloc failed!\n");
739 err = -ENOMEM;
740 goto ERROR0;
741 }
742 memset(data, 0, sizeof(struct asb100_data));
743
744 new_client = &data->client;
745 init_MUTEX(&data->lock);
746 i2c_set_clientdata(new_client, data);
747 new_client->addr = address;
748 new_client->adapter = adapter;
749 new_client->driver = &asb100_driver;
750 new_client->flags = 0;
751
752 /* Now, we do the remaining detection. */
753
754 /* The chip may be stuck in some other bank than bank 0. This may
755 make reading other information impossible. Specify a force=... or
756 force_*=... parameter, and the chip will be reset to the right
757 bank. */
758 if (kind < 0) {
759
760 int val1 = asb100_read_value(new_client, ASB100_REG_BANK);
761 int val2 = asb100_read_value(new_client, ASB100_REG_CHIPMAN);
762
763 /* If we're in bank 0 */
764 if ( (!(val1 & 0x07)) &&
765 /* Check for ASB100 ID (low byte) */
766 ( ((!(val1 & 0x80)) && (val2 != 0x94)) ||
767 /* Check for ASB100 ID (high byte ) */
768 ((val1 & 0x80) && (val2 != 0x06)) ) ) {
769 pr_debug("asb100.o: detect failed, "
770 "bad chip id 0x%02x!\n", val2);
771 err = -ENODEV;
772 goto ERROR1;
773 }
774
775 } /* kind < 0 */
776
777 /* We have either had a force parameter, or we have already detected
778 Winbond. Put it now into bank 0 and Vendor ID High Byte */
779 asb100_write_value(new_client, ASB100_REG_BANK,
780 (asb100_read_value(new_client, ASB100_REG_BANK) & 0x78) | 0x80);
781
782 /* Determine the chip type. */
783 if (kind <= 0) {
784 int val1 = asb100_read_value(new_client, ASB100_REG_WCHIPID);
785 int val2 = asb100_read_value(new_client, ASB100_REG_CHIPMAN);
786
787 if ((val1 == 0x31) && (val2 == 0x06))
788 kind = asb100;
789 else {
790 if (kind == 0)
791 dev_warn(&new_client->dev, "ignoring "
792 "'force' parameter for unknown chip "
793 "at adapter %d, address 0x%02x.\n",
794 i2c_adapter_id(adapter), address);
795 err = -ENODEV;
796 goto ERROR1;
797 }
798 }
799
800 /* Fill in remaining client fields and put it into the global list */
801 strlcpy(new_client->name, "asb100", I2C_NAME_SIZE);
802 data->type = kind;
803
804 data->valid = 0;
805 init_MUTEX(&data->update_lock);
806
807 /* Tell the I2C layer a new client has arrived */
808 if ((err = i2c_attach_client(new_client)))
809 goto ERROR1;
810
811 /* Attach secondary lm75 clients */
812 if ((err = asb100_detect_subclients(adapter, address, kind,
813 new_client)))
814 goto ERROR2;
815
816 /* Initialize the chip */
817 asb100_init_client(new_client);
818
819 /* A few vars need to be filled upon startup */
820 data->fan_min[0] = asb100_read_value(new_client, ASB100_REG_FAN_MIN(0));
821 data->fan_min[1] = asb100_read_value(new_client, ASB100_REG_FAN_MIN(1));
822 data->fan_min[2] = asb100_read_value(new_client, ASB100_REG_FAN_MIN(2));
823
824 /* Register sysfs hooks */
825 device_create_file_in(new_client, 0);
826 device_create_file_in(new_client, 1);
827 device_create_file_in(new_client, 2);
828 device_create_file_in(new_client, 3);
829 device_create_file_in(new_client, 4);
830 device_create_file_in(new_client, 5);
831 device_create_file_in(new_client, 6);
832
833 device_create_file_fan(new_client, 1);
834 device_create_file_fan(new_client, 2);
835 device_create_file_fan(new_client, 3);
836
837 device_create_file_temp(new_client, 1);
838 device_create_file_temp(new_client, 2);
839 device_create_file_temp(new_client, 3);
840 device_create_file_temp(new_client, 4);
841
842 device_create_file_vid(new_client);
843 device_create_file_vrm(new_client);
844
845 device_create_file_alarms(new_client);
846
847 device_create_file_pwm1(new_client);
848
849 return 0;
850
851ERROR2:
852 i2c_detach_client(new_client);
853ERROR1:
854 kfree(data);
855ERROR0:
856 return err;
857}
858
859static int asb100_detach_client(struct i2c_client *client)
860{
861 int err;
862
863 if ((err = i2c_detach_client(client))) {
864 dev_err(&client->dev, "client deregistration failed; "
865 "client not detached.\n");
866 return err;
867 }
868
869 if (i2c_get_clientdata(client)==NULL) {
870 /* subclients */
871 kfree(client);
872 } else {
873 /* main client */
874 kfree(i2c_get_clientdata(client));
875 }
876
877 return 0;
878}
879
880/* The SMBus locks itself, usually, but nothing may access the chip between
881 bank switches. */
882static int asb100_read_value(struct i2c_client *client, u16 reg)
883{
884 struct asb100_data *data = i2c_get_clientdata(client);
885 struct i2c_client *cl;
886 int res, bank;
887
888 down(&data->lock);
889
890 bank = (reg >> 8) & 0x0f;
891 if (bank > 2)
892 /* switch banks */
893 i2c_smbus_write_byte_data(client, ASB100_REG_BANK, bank);
894
895 if (bank == 0 || bank > 2) {
896 res = i2c_smbus_read_byte_data(client, reg & 0xff);
897 } else {
898 /* switch to subclient */
899 cl = data->lm75[bank - 1];
900
901 /* convert from ISA to LM75 I2C addresses */
902 switch (reg & 0xff) {
903 case 0x50: /* TEMP */
904 res = swab16(i2c_smbus_read_word_data (cl, 0));
905 break;
906 case 0x52: /* CONFIG */
907 res = i2c_smbus_read_byte_data(cl, 1);
908 break;
909 case 0x53: /* HYST */
910 res = swab16(i2c_smbus_read_word_data (cl, 2));
911 break;
912 case 0x55: /* MAX */
913 default:
914 res = swab16(i2c_smbus_read_word_data (cl, 3));
915 break;
916 }
917 }
918
919 if (bank > 2)
920 i2c_smbus_write_byte_data(client, ASB100_REG_BANK, 0);
921
922 up(&data->lock);
923
924 return res;
925}
926
927static void asb100_write_value(struct i2c_client *client, u16 reg, u16 value)
928{
929 struct asb100_data *data = i2c_get_clientdata(client);
930 struct i2c_client *cl;
931 int bank;
932
933 down(&data->lock);
934
935 bank = (reg >> 8) & 0x0f;
936 if (bank > 2)
937 /* switch banks */
938 i2c_smbus_write_byte_data(client, ASB100_REG_BANK, bank);
939
940 if (bank == 0 || bank > 2) {
941 i2c_smbus_write_byte_data(client, reg & 0xff, value & 0xff);
942 } else {
943 /* switch to subclient */
944 cl = data->lm75[bank - 1];
945
946 /* convert from ISA to LM75 I2C addresses */
947 switch (reg & 0xff) {
948 case 0x52: /* CONFIG */
949 i2c_smbus_write_byte_data(cl, 1, value & 0xff);
950 break;
951 case 0x53: /* HYST */
952 i2c_smbus_write_word_data(cl, 2, swab16(value));
953 break;
954 case 0x55: /* MAX */
955 i2c_smbus_write_word_data(cl, 3, swab16(value));
956 break;
957 }
958 }
959
960 if (bank > 2)
961 i2c_smbus_write_byte_data(client, ASB100_REG_BANK, 0);
962
963 up(&data->lock);
964}
965
966static void asb100_init_client(struct i2c_client *client)
967{
968 struct asb100_data *data = i2c_get_clientdata(client);
969 int vid = 0;
970
971 vid = asb100_read_value(client, ASB100_REG_VID_FANDIV) & 0x0f;
972 vid |= (asb100_read_value(client, ASB100_REG_CHIPID) & 0x01) << 4;
973 data->vrm = i2c_which_vrm();
974 vid = vid_from_reg(vid, data->vrm);
975
976 /* Start monitoring */
977 asb100_write_value(client, ASB100_REG_CONFIG,
978 (asb100_read_value(client, ASB100_REG_CONFIG) & 0xf7) | 0x01);
979}
980
981static struct asb100_data *asb100_update_device(struct device *dev)
982{
983 struct i2c_client *client = to_i2c_client(dev);
984 struct asb100_data *data = i2c_get_clientdata(client);
985 int i;
986
987 down(&data->update_lock);
988
989 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
990 || !data->valid) {
991
992 dev_dbg(&client->dev, "starting device update...\n");
993
994 /* 7 voltage inputs */
995 for (i = 0; i < 7; i++) {
996 data->in[i] = asb100_read_value(client,
997 ASB100_REG_IN(i));
998 data->in_min[i] = asb100_read_value(client,
999 ASB100_REG_IN_MIN(i));
1000 data->in_max[i] = asb100_read_value(client,
1001 ASB100_REG_IN_MAX(i));
1002 }
1003
1004 /* 3 fan inputs */
1005 for (i = 0; i < 3; i++) {
1006 data->fan[i] = asb100_read_value(client,
1007 ASB100_REG_FAN(i));
1008 data->fan_min[i] = asb100_read_value(client,
1009 ASB100_REG_FAN_MIN(i));
1010 }
1011
1012 /* 4 temperature inputs */
1013 for (i = 1; i <= 4; i++) {
1014 data->temp[i-1] = asb100_read_value(client,
1015 ASB100_REG_TEMP(i));
1016 data->temp_max[i-1] = asb100_read_value(client,
1017 ASB100_REG_TEMP_MAX(i));
1018 data->temp_hyst[i-1] = asb100_read_value(client,
1019 ASB100_REG_TEMP_HYST(i));
1020 }
1021
1022 /* VID and fan divisors */
1023 i = asb100_read_value(client, ASB100_REG_VID_FANDIV);
1024 data->vid = i & 0x0f;
1025 data->vid |= (asb100_read_value(client,
1026 ASB100_REG_CHIPID) & 0x01) << 4;
1027 data->fan_div[0] = (i >> 4) & 0x03;
1028 data->fan_div[1] = (i >> 6) & 0x03;
1029 data->fan_div[2] = (asb100_read_value(client,
1030 ASB100_REG_PIN) >> 6) & 0x03;
1031
1032 /* PWM */
1033 data->pwm = asb100_read_value(client, ASB100_REG_PWM1);
1034
1035 /* alarms */
1036 data->alarms = asb100_read_value(client, ASB100_REG_ALARM1) +
1037 (asb100_read_value(client, ASB100_REG_ALARM2) << 8);
1038
1039 data->last_updated = jiffies;
1040 data->valid = 1;
1041
1042 dev_dbg(&client->dev, "... device update complete\n");
1043 }
1044
1045 up(&data->update_lock);
1046
1047 return data;
1048}
1049
1050static int __init asb100_init(void)
1051{
1052 return i2c_add_driver(&asb100_driver);
1053}
1054
1055static void __exit asb100_exit(void)
1056{
1057 i2c_del_driver(&asb100_driver);
1058}
1059
1060MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
1061MODULE_DESCRIPTION("ASB100 Bach driver");
1062MODULE_LICENSE("GPL");
1063
1064module_init(asb100_init);
1065module_exit(asb100_exit);
1066
diff --git a/drivers/i2c/chips/ds1337.c b/drivers/i2c/chips/ds1337.c
new file mode 100644
index 000000000000..07f16c3fb084
--- /dev/null
+++ b/drivers/i2c/chips/ds1337.c
@@ -0,0 +1,402 @@
1/*
2 * linux/drivers/i2c/chips/ds1337.c
3 *
4 * Copyright (C) 2005 James Chapman <jchapman@katalix.com>
5 *
6 * based on linux/drivers/acron/char/pcf8583.c
7 * Copyright (C) 2000 Russell King
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 *
13 * Driver for Dallas Semiconductor DS1337 real time clock chip
14 */
15
16#include <linux/config.h>
17#include <linux/module.h>
18#include <linux/init.h>
19#include <linux/slab.h>
20#include <linux/i2c.h>
21#include <linux/i2c-sensor.h>
22#include <linux/string.h>
23#include <linux/rtc.h> /* get the user-level API */
24#include <linux/bcd.h>
25#include <linux/list.h>
26
27/* Device registers */
28#define DS1337_REG_HOUR 2
29#define DS1337_REG_DAY 3
30#define DS1337_REG_DATE 4
31#define DS1337_REG_MONTH 5
32#define DS1337_REG_CONTROL 14
33#define DS1337_REG_STATUS 15
34
35/* FIXME - how do we export these interface constants? */
36#define DS1337_GET_DATE 0
37#define DS1337_SET_DATE 1
38
39/*
40 * Functions declaration
41 */
42static unsigned short normal_i2c[] = { 0x68, I2C_CLIENT_END };
43static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
44
45SENSORS_INSMOD_1(ds1337);
46
47static int ds1337_attach_adapter(struct i2c_adapter *adapter);
48static int ds1337_detect(struct i2c_adapter *adapter, int address, int kind);
49static void ds1337_init_client(struct i2c_client *client);
50static int ds1337_detach_client(struct i2c_client *client);
51static int ds1337_command(struct i2c_client *client, unsigned int cmd,
52 void *arg);
53
54/*
55 * Driver data (common to all clients)
56 */
57static struct i2c_driver ds1337_driver = {
58 .owner = THIS_MODULE,
59 .name = "ds1337",
60 .flags = I2C_DF_NOTIFY,
61 .attach_adapter = ds1337_attach_adapter,
62 .detach_client = ds1337_detach_client,
63 .command = ds1337_command,
64};
65
66/*
67 * Client data (each client gets its own)
68 */
69struct ds1337_data {
70 struct i2c_client client;
71 struct list_head list;
72 int id;
73};
74
75/*
76 * Internal variables
77 */
78static int ds1337_id;
79static LIST_HEAD(ds1337_clients);
80
81static inline int ds1337_read(struct i2c_client *client, u8 reg, u8 *value)
82{
83 s32 tmp = i2c_smbus_read_byte_data(client, reg);
84
85 if (tmp < 0)
86 return -EIO;
87
88 *value = tmp;
89
90 return 0;
91}
92
93/*
94 * Chip access functions
95 */
96static int ds1337_get_datetime(struct i2c_client *client, struct rtc_time *dt)
97{
98 struct ds1337_data *data = i2c_get_clientdata(client);
99 int result;
100 u8 buf[7];
101 u8 val;
102 struct i2c_msg msg[2];
103 u8 offs = 0;
104
105 if (!dt) {
106 dev_dbg(&client->adapter->dev, "%s: EINVAL: dt=NULL\n",
107 __FUNCTION__);
108
109 return -EINVAL;
110 }
111
112 msg[0].addr = client->addr;
113 msg[0].flags = 0;
114 msg[0].len = 1;
115 msg[0].buf = &offs;
116
117 msg[1].addr = client->addr;
118 msg[1].flags = I2C_M_RD;
119 msg[1].len = sizeof(buf);
120 msg[1].buf = &buf[0];
121
122 result = client->adapter->algo->master_xfer(client->adapter,
123 &msg[0], 2);
124
125 dev_dbg(&client->adapter->dev,
126 "%s: [%d] %02x %02x %02x %02x %02x %02x %02x\n",
127 __FUNCTION__, result, buf[0], buf[1], buf[2], buf[3],
128 buf[4], buf[5], buf[6]);
129
130 if (result >= 0) {
131 dt->tm_sec = BCD_TO_BIN(buf[0]);
132 dt->tm_min = BCD_TO_BIN(buf[1]);
133 val = buf[2] & 0x3f;
134 dt->tm_hour = BCD_TO_BIN(val);
135 dt->tm_wday = BCD_TO_BIN(buf[3]) - 1;
136 dt->tm_mday = BCD_TO_BIN(buf[4]);
137 val = buf[5] & 0x7f;
138 dt->tm_mon = BCD_TO_BIN(val);
139 dt->tm_year = 1900 + BCD_TO_BIN(buf[6]);
140 if (buf[5] & 0x80)
141 dt->tm_year += 100;
142
143 dev_dbg(&client->adapter->dev, "%s: secs=%d, mins=%d, "
144 "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
145 __FUNCTION__, dt->tm_sec, dt->tm_min,
146 dt->tm_hour, dt->tm_mday,
147 dt->tm_mon, dt->tm_year, dt->tm_wday);
148 } else {
149 dev_err(&client->adapter->dev, "ds1337[%d]: error reading "
150 "data! %d\n", data->id, result);
151 result = -EIO;
152 }
153
154 return result;
155}
156
157static int ds1337_set_datetime(struct i2c_client *client, struct rtc_time *dt)
158{
159 struct ds1337_data *data = i2c_get_clientdata(client);
160 int result;
161 u8 buf[8];
162 u8 val;
163 struct i2c_msg msg[1];
164
165 if (!dt) {
166 dev_dbg(&client->adapter->dev, "%s: EINVAL: dt=NULL\n",
167 __FUNCTION__);
168
169 return -EINVAL;
170 }
171
172 dev_dbg(&client->adapter->dev, "%s: secs=%d, mins=%d, hours=%d, "
173 "mday=%d, mon=%d, year=%d, wday=%d\n", __FUNCTION__,
174 dt->tm_sec, dt->tm_min, dt->tm_hour,
175 dt->tm_mday, dt->tm_mon, dt->tm_year, dt->tm_wday);
176
177 buf[0] = 0; /* reg offset */
178 buf[1] = BIN_TO_BCD(dt->tm_sec);
179 buf[2] = BIN_TO_BCD(dt->tm_min);
180 buf[3] = BIN_TO_BCD(dt->tm_hour) | (1 << 6);
181 buf[4] = BIN_TO_BCD(dt->tm_wday) + 1;
182 buf[5] = BIN_TO_BCD(dt->tm_mday);
183 buf[6] = BIN_TO_BCD(dt->tm_mon);
184 if (dt->tm_year >= 2000) {
185 val = dt->tm_year - 2000;
186 buf[6] |= (1 << 7);
187 } else {
188 val = dt->tm_year - 1900;
189 }
190 buf[7] = BIN_TO_BCD(val);
191
192 msg[0].addr = client->addr;
193 msg[0].flags = 0;
194 msg[0].len = sizeof(buf);
195 msg[0].buf = &buf[0];
196
197 result = client->adapter->algo->master_xfer(client->adapter,
198 &msg[0], 1);
199 if (result < 0) {
200 dev_err(&client->adapter->dev, "ds1337[%d]: error "
201 "writing data! %d\n", data->id, result);
202 result = -EIO;
203 } else {
204 result = 0;
205 }
206
207 return result;
208}
209
210static int ds1337_command(struct i2c_client *client, unsigned int cmd,
211 void *arg)
212{
213 dev_dbg(&client->adapter->dev, "%s: cmd=%d\n", __FUNCTION__, cmd);
214
215 switch (cmd) {
216 case DS1337_GET_DATE:
217 return ds1337_get_datetime(client, arg);
218
219 case DS1337_SET_DATE:
220 return ds1337_set_datetime(client, arg);
221
222 default:
223 return -EINVAL;
224 }
225}
226
227/*
228 * Public API for access to specific device. Useful for low-level
229 * RTC access from kernel code.
230 */
231int ds1337_do_command(int id, int cmd, void *arg)
232{
233 struct list_head *walk;
234 struct list_head *tmp;
235 struct ds1337_data *data;
236
237 list_for_each_safe(walk, tmp, &ds1337_clients) {
238 data = list_entry(walk, struct ds1337_data, list);
239 if (data->id == id)
240 return ds1337_command(&data->client, cmd, arg);
241 }
242
243 return -ENODEV;
244}
245
246static int ds1337_attach_adapter(struct i2c_adapter *adapter)
247{
248 return i2c_detect(adapter, &addr_data, ds1337_detect);
249}
250
251/*
252 * The following function does more than just detection. If detection
253 * succeeds, it also registers the new chip.
254 */
255static int ds1337_detect(struct i2c_adapter *adapter, int address, int kind)
256{
257 struct i2c_client *new_client;
258 struct ds1337_data *data;
259 int err = 0;
260 const char *name = "";
261
262 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
263 I2C_FUNC_I2C))
264 goto exit;
265
266 if (!(data = kmalloc(sizeof(struct ds1337_data), GFP_KERNEL))) {
267 err = -ENOMEM;
268 goto exit;
269 }
270 memset(data, 0, sizeof(struct ds1337_data));
271 INIT_LIST_HEAD(&data->list);
272
273 /* The common I2C client data is placed right before the
274 * DS1337-specific data.
275 */
276 new_client = &data->client;
277 i2c_set_clientdata(new_client, data);
278 new_client->addr = address;
279 new_client->adapter = adapter;
280 new_client->driver = &ds1337_driver;
281 new_client->flags = 0;
282
283 /*
284 * Now we do the remaining detection. A negative kind means that
285 * the driver was loaded with no force parameter (default), so we
286 * must both detect and identify the chip. A zero kind means that
287 * the driver was loaded with the force parameter, the detection
288 * step shall be skipped. A positive kind means that the driver
289 * was loaded with the force parameter and a given kind of chip is
290 * requested, so both the detection and the identification steps
291 * are skipped.
292 *
293 * For detection, we read registers that are most likely to cause
294 * detection failure, i.e. those that have more bits with fixed
295 * or reserved values.
296 */
297
298 /* Default to an DS1337 if forced */
299 if (kind == 0)
300 kind = ds1337;
301
302 if (kind < 0) { /* detection and identification */
303 u8 data;
304
305 /* Check that status register bits 6-2 are zero */
306 if ((ds1337_read(new_client, DS1337_REG_STATUS, &data) < 0) ||
307 (data & 0x7c))
308 goto exit_free;
309
310 /* Check for a valid day register value */
311 if ((ds1337_read(new_client, DS1337_REG_DAY, &data) < 0) ||
312 (data == 0) || (data & 0xf8))
313 goto exit_free;
314
315 /* Check for a valid date register value */
316 if ((ds1337_read(new_client, DS1337_REG_DATE, &data) < 0) ||
317 (data == 0) || (data & 0xc0) || ((data & 0x0f) > 9) ||
318 (data >= 0x32))
319 goto exit_free;
320
321 /* Check for a valid month register value */
322 if ((ds1337_read(new_client, DS1337_REG_MONTH, &data) < 0) ||
323 (data == 0) || (data & 0x60) || ((data & 0x0f) > 9) ||
324 ((data >= 0x13) && (data <= 0x19)))
325 goto exit_free;
326
327 /* Check that control register bits 6-5 are zero */
328 if ((ds1337_read(new_client, DS1337_REG_CONTROL, &data) < 0) ||
329 (data & 0x60))
330 goto exit_free;
331
332 kind = ds1337;
333 }
334
335 if (kind == ds1337)
336 name = "ds1337";
337
338 /* We can fill in the remaining client fields */
339 strlcpy(new_client->name, name, I2C_NAME_SIZE);
340
341 /* Tell the I2C layer a new client has arrived */
342 if ((err = i2c_attach_client(new_client)))
343 goto exit_free;
344
345 /* Initialize the DS1337 chip */
346 ds1337_init_client(new_client);
347
348 /* Add client to local list */
349 data->id = ds1337_id++;
350 list_add(&data->list, &ds1337_clients);
351
352 return 0;
353
354exit_free:
355 kfree(data);
356exit:
357 return err;
358}
359
360static void ds1337_init_client(struct i2c_client *client)
361{
362 s32 val;
363
364 /* Ensure that device is set in 24-hour mode */
365 val = i2c_smbus_read_byte_data(client, DS1337_REG_HOUR);
366 if ((val >= 0) && (val & (1 << 6)) == 0)
367 i2c_smbus_write_byte_data(client, DS1337_REG_HOUR,
368 val | (1 << 6));
369}
370
371static int ds1337_detach_client(struct i2c_client *client)
372{
373 int err;
374 struct ds1337_data *data = i2c_get_clientdata(client);
375
376 if ((err = i2c_detach_client(client))) {
377 dev_err(&client->dev, "Client deregistration failed, "
378 "client not detached.\n");
379 return err;
380 }
381
382 list_del(&data->list);
383 kfree(data);
384 return 0;
385}
386
387static int __init ds1337_init(void)
388{
389 return i2c_add_driver(&ds1337_driver);
390}
391
392static void __exit ds1337_exit(void)
393{
394 i2c_del_driver(&ds1337_driver);
395}
396
397MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
398MODULE_DESCRIPTION("DS1337 RTC driver");
399MODULE_LICENSE("GPL");
400
401module_init(ds1337_init);
402module_exit(ds1337_exit);
diff --git a/drivers/i2c/chips/ds1621.c b/drivers/i2c/chips/ds1621.c
new file mode 100644
index 000000000000..bb1fefb2162e
--- /dev/null
+++ b/drivers/i2c/chips/ds1621.c
@@ -0,0 +1,341 @@
1/*
2 ds1621.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring
4 Christian W. Zuckschwerdt <zany@triq.net> 2000-11-23
5 based on lm75.c by Frodo Looijaard <frodol@dds.nl>
6 Ported to Linux 2.6 by Aurelien Jarno <aurelien@aurel32.net> with
7 the help of Jean Delvare <khali@linux-fr.org>
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22*/
23
24#include <linux/module.h>
25#include <linux/init.h>
26#include <linux/slab.h>
27#include <linux/jiffies.h>
28#include <linux/i2c.h>
29#include <linux/i2c-sensor.h>
30#include "lm75.h"
31
32/* Addresses to scan */
33static unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b, 0x4c,
34 0x4d, 0x4e, 0x4f, I2C_CLIENT_END };
35static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
36
37/* Insmod parameters */
38SENSORS_INSMOD_1(ds1621);
39static int polarity = -1;
40module_param(polarity, int, 0);
41MODULE_PARM_DESC(polarity, "Output's polarity: 0 = active high, 1 = active low");
42
43/* Many DS1621 constants specified below */
44/* Config register used for detection */
45/* 7 6 5 4 3 2 1 0 */
46/* |Done|THF |TLF |NVB | X | X |POL |1SHOT| */
47#define DS1621_REG_CONFIG_NVB 0x10
48#define DS1621_REG_CONFIG_POLARITY 0x02
49#define DS1621_REG_CONFIG_1SHOT 0x01
50#define DS1621_REG_CONFIG_DONE 0x80
51
52/* The DS1621 registers */
53#define DS1621_REG_TEMP 0xAA /* word, RO */
54#define DS1621_REG_TEMP_MIN 0xA1 /* word, RW */
55#define DS1621_REG_TEMP_MAX 0xA2 /* word, RW */
56#define DS1621_REG_CONF 0xAC /* byte, RW */
57#define DS1621_COM_START 0xEE /* no data */
58#define DS1621_COM_STOP 0x22 /* no data */
59
60/* The DS1621 configuration register */
61#define DS1621_ALARM_TEMP_HIGH 0x40
62#define DS1621_ALARM_TEMP_LOW 0x20
63
64/* Conversions. Rounding and limit checking is only done on the TO_REG
65 variants. Note that you should be a bit careful with which arguments
66 these macros are called: arguments may be evaluated more than once.
67 Fixing this is just not worth it. */
68#define ALARMS_FROM_REG(val) ((val) & \
69 (DS1621_ALARM_TEMP_HIGH | DS1621_ALARM_TEMP_LOW))
70
71/* Each client has this additional data */
72struct ds1621_data {
73 struct i2c_client client;
74 struct semaphore update_lock;
75 char valid; /* !=0 if following fields are valid */
76 unsigned long last_updated; /* In jiffies */
77
78 u16 temp, temp_min, temp_max; /* Register values, word */
79 u8 conf; /* Register encoding, combined */
80};
81
82static int ds1621_attach_adapter(struct i2c_adapter *adapter);
83static int ds1621_detect(struct i2c_adapter *adapter, int address,
84 int kind);
85static void ds1621_init_client(struct i2c_client *client);
86static int ds1621_detach_client(struct i2c_client *client);
87static struct ds1621_data *ds1621_update_client(struct device *dev);
88
89/* This is the driver that will be inserted */
90static struct i2c_driver ds1621_driver = {
91 .owner = THIS_MODULE,
92 .name = "ds1621",
93 .id = I2C_DRIVERID_DS1621,
94 .flags = I2C_DF_NOTIFY,
95 .attach_adapter = ds1621_attach_adapter,
96 .detach_client = ds1621_detach_client,
97};
98
99/* All registers are word-sized, except for the configuration register.
100 DS1621 uses a high-byte first convention, which is exactly opposite to
101 the usual practice. */
102static int ds1621_read_value(struct i2c_client *client, u8 reg)
103{
104 if (reg == DS1621_REG_CONF)
105 return i2c_smbus_read_byte_data(client, reg);
106 else
107 return swab16(i2c_smbus_read_word_data(client, reg));
108}
109
110/* All registers are word-sized, except for the configuration register.
111 DS1621 uses a high-byte first convention, which is exactly opposite to
112 the usual practice. */
113static int ds1621_write_value(struct i2c_client *client, u8 reg, u16 value)
114{
115 if (reg == DS1621_REG_CONF)
116 return i2c_smbus_write_byte_data(client, reg, value);
117 else
118 return i2c_smbus_write_word_data(client, reg, swab16(value));
119}
120
121static void ds1621_init_client(struct i2c_client *client)
122{
123 int reg = ds1621_read_value(client, DS1621_REG_CONF);
124 /* switch to continous conversion mode */
125 reg &= ~ DS1621_REG_CONFIG_1SHOT;
126
127 /* setup output polarity */
128 if (polarity == 0)
129 reg &= ~DS1621_REG_CONFIG_POLARITY;
130 else if (polarity == 1)
131 reg |= DS1621_REG_CONFIG_POLARITY;
132
133 ds1621_write_value(client, DS1621_REG_CONF, reg);
134
135 /* start conversion */
136 i2c_smbus_write_byte(client, DS1621_COM_START);
137}
138
139#define show(value) \
140static ssize_t show_##value(struct device *dev, char *buf) \
141{ \
142 struct ds1621_data *data = ds1621_update_client(dev); \
143 return sprintf(buf, "%d\n", LM75_TEMP_FROM_REG(data->value)); \
144}
145
146show(temp);
147show(temp_min);
148show(temp_max);
149
150#define set_temp(suffix, value, reg) \
151static ssize_t set_temp_##suffix(struct device *dev, const char *buf, \
152 size_t count) \
153{ \
154 struct i2c_client *client = to_i2c_client(dev); \
155 struct ds1621_data *data = ds1621_update_client(dev); \
156 u16 val = LM75_TEMP_TO_REG(simple_strtoul(buf, NULL, 10)); \
157 \
158 down(&data->update_lock); \
159 data->value = val; \
160 ds1621_write_value(client, reg, data->value); \
161 up(&data->update_lock); \
162 return count; \
163}
164
165set_temp(min, temp_min, DS1621_REG_TEMP_MIN);
166set_temp(max, temp_max, DS1621_REG_TEMP_MAX);
167
168static ssize_t show_alarms(struct device *dev, char *buf)
169{
170 struct ds1621_data *data = ds1621_update_client(dev);
171 return sprintf(buf, "%d\n", ALARMS_FROM_REG(data->conf));
172}
173
174static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
175static DEVICE_ATTR(temp1_input, S_IRUGO , show_temp, NULL);
176static DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO , show_temp_min, set_temp_min);
177static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max, set_temp_max);
178
179
180static int ds1621_attach_adapter(struct i2c_adapter *adapter)
181{
182 return i2c_detect(adapter, &addr_data, ds1621_detect);
183}
184
185/* This function is called by i2c_detect */
186int ds1621_detect(struct i2c_adapter *adapter, int address,
187 int kind)
188{
189 int conf, temp;
190 struct i2c_client *new_client;
191 struct ds1621_data *data;
192 int err = 0;
193
194 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA
195 | I2C_FUNC_SMBUS_WORD_DATA
196 | I2C_FUNC_SMBUS_WRITE_BYTE))
197 goto exit;
198
199 /* OK. For now, we presume we have a valid client. We now create the
200 client structure, even though we cannot fill it completely yet.
201 But it allows us to access ds1621_{read,write}_value. */
202 if (!(data = kmalloc(sizeof(struct ds1621_data), GFP_KERNEL))) {
203 err = -ENOMEM;
204 goto exit;
205 }
206 memset(data, 0, sizeof(struct ds1621_data));
207
208 new_client = &data->client;
209 i2c_set_clientdata(new_client, data);
210 new_client->addr = address;
211 new_client->adapter = adapter;
212 new_client->driver = &ds1621_driver;
213 new_client->flags = 0;
214
215
216 /* Now, we do the remaining detection. It is lousy. */
217 if (kind < 0) {
218 /* The NVB bit should be low if no EEPROM write has been
219 requested during the latest 10ms, which is highly
220 improbable in our case. */
221 conf = ds1621_read_value(new_client, DS1621_REG_CONF);
222 if (conf & DS1621_REG_CONFIG_NVB)
223 goto exit_free;
224 /* The 7 lowest bits of a temperature should always be 0. */
225 temp = ds1621_read_value(new_client, DS1621_REG_TEMP);
226 if (temp & 0x007f)
227 goto exit_free;
228 temp = ds1621_read_value(new_client, DS1621_REG_TEMP_MIN);
229 if (temp & 0x007f)
230 goto exit_free;
231 temp = ds1621_read_value(new_client, DS1621_REG_TEMP_MAX);
232 if (temp & 0x007f)
233 goto exit_free;
234 }
235
236 /* Determine the chip type - only one kind supported! */
237 if (kind <= 0)
238 kind = ds1621;
239
240 /* Fill in remaining client fields and put it into the global list */
241 strlcpy(new_client->name, "ds1621", I2C_NAME_SIZE);
242 data->valid = 0;
243 init_MUTEX(&data->update_lock);
244
245 /* Tell the I2C layer a new client has arrived */
246 if ((err = i2c_attach_client(new_client)))
247 goto exit_free;
248
249 /* Initialize the DS1621 chip */
250 ds1621_init_client(new_client);
251
252 /* Register sysfs hooks */
253 device_create_file(&new_client->dev, &dev_attr_alarms);
254 device_create_file(&new_client->dev, &dev_attr_temp1_input);
255 device_create_file(&new_client->dev, &dev_attr_temp1_min);
256 device_create_file(&new_client->dev, &dev_attr_temp1_max);
257
258 return 0;
259
260/* OK, this is not exactly good programming practice, usually. But it is
261 very code-efficient in this case. */
262 exit_free:
263 kfree(data);
264 exit:
265 return err;
266}
267
268static int ds1621_detach_client(struct i2c_client *client)
269{
270 int err;
271
272 if ((err = i2c_detach_client(client))) {
273 dev_err(&client->dev, "Client deregistration failed, "
274 "client not detached.\n");
275 return err;
276 }
277
278 kfree(i2c_get_clientdata(client));
279
280 return 0;
281}
282
283
284static struct ds1621_data *ds1621_update_client(struct device *dev)
285{
286 struct i2c_client *client = to_i2c_client(dev);
287 struct ds1621_data *data = i2c_get_clientdata(client);
288 u8 new_conf;
289
290 down(&data->update_lock);
291
292 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
293 || !data->valid) {
294
295 dev_dbg(&client->dev, "Starting ds1621 update\n");
296
297 data->conf = ds1621_read_value(client, DS1621_REG_CONF);
298
299 data->temp = ds1621_read_value(client, DS1621_REG_TEMP);
300
301 data->temp_min = ds1621_read_value(client,
302 DS1621_REG_TEMP_MIN);
303 data->temp_max = ds1621_read_value(client,
304 DS1621_REG_TEMP_MAX);
305
306 /* reset alarms if neccessary */
307 new_conf = data->conf;
308 if (data->temp < data->temp_min)
309 new_conf &= ~DS1621_ALARM_TEMP_LOW;
310 if (data->temp > data->temp_max)
311 new_conf &= ~DS1621_ALARM_TEMP_HIGH;
312 if (data->conf != new_conf)
313 ds1621_write_value(client, DS1621_REG_CONF,
314 new_conf);
315
316 data->last_updated = jiffies;
317 data->valid = 1;
318 }
319
320 up(&data->update_lock);
321
322 return data;
323}
324
325static int __init ds1621_init(void)
326{
327 return i2c_add_driver(&ds1621_driver);
328}
329
330static void __exit ds1621_exit(void)
331{
332 i2c_del_driver(&ds1621_driver);
333}
334
335
336MODULE_AUTHOR("Christian W. Zuckschwerdt <zany@triq.net>");
337MODULE_DESCRIPTION("DS1621 driver");
338MODULE_LICENSE("GPL");
339
340module_init(ds1621_init);
341module_exit(ds1621_exit);
diff --git a/drivers/i2c/chips/eeprom.c b/drivers/i2c/chips/eeprom.c
new file mode 100644
index 000000000000..cbdfa2db6f7c
--- /dev/null
+++ b/drivers/i2c/chips/eeprom.c
@@ -0,0 +1,264 @@
1/*
2 eeprom.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring
4 Copyright (C) 1998, 1999 Frodo Looijaard <frodol@dds.nl> and
5 Philip Edelbrock <phil@netroedge.com>
6 Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
7 Copyright (C) 2003 IBM Corp.
8
9 2004-01-16 Jean Delvare <khali@linux-fr.org>
10 Divide the eeprom in 32-byte (arbitrary) slices. This significantly
11 speeds sensors up, as well as various scripts using the eeprom
12 module.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 2 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27*/
28
29#include <linux/config.h>
30#include <linux/kernel.h>
31#include <linux/init.h>
32#include <linux/module.h>
33#include <linux/slab.h>
34#include <linux/sched.h>
35#include <linux/jiffies.h>
36#include <linux/i2c.h>
37#include <linux/i2c-sensor.h>
38
39/* Addresses to scan */
40static unsigned short normal_i2c[] = { 0x50, 0x51, 0x52, 0x53, 0x54,
41 0x55, 0x56, 0x57, I2C_CLIENT_END };
42static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
43
44/* Insmod parameters */
45SENSORS_INSMOD_1(eeprom);
46
47
48/* Size of EEPROM in bytes */
49#define EEPROM_SIZE 256
50
51/* possible types of eeprom devices */
52enum eeprom_nature {
53 UNKNOWN,
54 VAIO,
55};
56
57/* Each client has this additional data */
58struct eeprom_data {
59 struct i2c_client client;
60 struct semaphore update_lock;
61 u8 valid; /* bitfield, bit!=0 if slice is valid */
62 unsigned long last_updated[8]; /* In jiffies, 8 slices */
63 u8 data[EEPROM_SIZE]; /* Register values */
64 enum eeprom_nature nature;
65};
66
67
68static int eeprom_attach_adapter(struct i2c_adapter *adapter);
69static int eeprom_detect(struct i2c_adapter *adapter, int address, int kind);
70static int eeprom_detach_client(struct i2c_client *client);
71
72/* This is the driver that will be inserted */
73static struct i2c_driver eeprom_driver = {
74 .owner = THIS_MODULE,
75 .name = "eeprom",
76 .id = I2C_DRIVERID_EEPROM,
77 .flags = I2C_DF_NOTIFY,
78 .attach_adapter = eeprom_attach_adapter,
79 .detach_client = eeprom_detach_client,
80};
81
82static void eeprom_update_client(struct i2c_client *client, u8 slice)
83{
84 struct eeprom_data *data = i2c_get_clientdata(client);
85 int i, j;
86
87 down(&data->update_lock);
88
89 if (!(data->valid & (1 << slice)) ||
90 time_after(jiffies, data->last_updated[slice] + 300 * HZ)) {
91 dev_dbg(&client->dev, "Starting eeprom update, slice %u\n", slice);
92
93 if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_I2C_BLOCK)) {
94 for (i = slice << 5; i < (slice + 1) << 5; i += I2C_SMBUS_I2C_BLOCK_MAX)
95 if (i2c_smbus_read_i2c_block_data(client, i, data->data + i) != I2C_SMBUS_I2C_BLOCK_MAX)
96 goto exit;
97 } else {
98 if (i2c_smbus_write_byte(client, slice << 5)) {
99 dev_dbg(&client->dev, "eeprom read start has failed!\n");
100 goto exit;
101 }
102 for (i = slice << 5; i < (slice + 1) << 5; i++) {
103 j = i2c_smbus_read_byte(client);
104 if (j < 0)
105 goto exit;
106 data->data[i] = (u8) j;
107 }
108 }
109 data->last_updated[slice] = jiffies;
110 data->valid |= (1 << slice);
111 }
112exit:
113 up(&data->update_lock);
114}
115
116static ssize_t eeprom_read(struct kobject *kobj, char *buf, loff_t off, size_t count)
117{
118 struct i2c_client *client = to_i2c_client(container_of(kobj, struct device, kobj));
119 struct eeprom_data *data = i2c_get_clientdata(client);
120 u8 slice;
121
122 if (off > EEPROM_SIZE)
123 return 0;
124 if (off + count > EEPROM_SIZE)
125 count = EEPROM_SIZE - off;
126
127 /* Only refresh slices which contain requested bytes */
128 for (slice = off >> 5; slice <= (off + count - 1) >> 5; slice++)
129 eeprom_update_client(client, slice);
130
131 /* Hide Vaio security settings to regular users (16 first bytes) */
132 if (data->nature == VAIO && off < 16 && !capable(CAP_SYS_ADMIN)) {
133 size_t in_row1 = 16 - off;
134 in_row1 = min(in_row1, count);
135 memset(buf, 0, in_row1);
136 if (count - in_row1 > 0)
137 memcpy(buf + in_row1, &data->data[16], count - in_row1);
138 } else {
139 memcpy(buf, &data->data[off], count);
140 }
141
142 return count;
143}
144
145static struct bin_attribute eeprom_attr = {
146 .attr = {
147 .name = "eeprom",
148 .mode = S_IRUGO,
149 .owner = THIS_MODULE,
150 },
151 .size = EEPROM_SIZE,
152 .read = eeprom_read,
153};
154
155static int eeprom_attach_adapter(struct i2c_adapter *adapter)
156{
157 return i2c_detect(adapter, &addr_data, eeprom_detect);
158}
159
160/* This function is called by i2c_detect */
161int eeprom_detect(struct i2c_adapter *adapter, int address, int kind)
162{
163 struct i2c_client *new_client;
164 struct eeprom_data *data;
165 int err = 0;
166
167 /* There are three ways we can read the EEPROM data:
168 (1) I2C block reads (faster, but unsupported by most adapters)
169 (2) Consecutive byte reads (100% overhead)
170 (3) Regular byte data reads (200% overhead)
171 The third method is not implemented by this driver because all
172 known adapters support at least the second. */
173 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_READ_BYTE_DATA
174 | I2C_FUNC_SMBUS_BYTE))
175 goto exit;
176
177 /* OK. For now, we presume we have a valid client. We now create the
178 client structure, even though we cannot fill it completely yet.
179 But it allows us to access eeprom_{read,write}_value. */
180 if (!(data = kmalloc(sizeof(struct eeprom_data), GFP_KERNEL))) {
181 err = -ENOMEM;
182 goto exit;
183 }
184 memset(data, 0, sizeof(struct eeprom_data));
185
186 new_client = &data->client;
187 memset(data->data, 0xff, EEPROM_SIZE);
188 i2c_set_clientdata(new_client, data);
189 new_client->addr = address;
190 new_client->adapter = adapter;
191 new_client->driver = &eeprom_driver;
192 new_client->flags = 0;
193
194 /* prevent 24RF08 corruption */
195 i2c_smbus_write_quick(new_client, 0);
196
197 /* Fill in the remaining client fields */
198 strlcpy(new_client->name, "eeprom", I2C_NAME_SIZE);
199 data->valid = 0;
200 init_MUTEX(&data->update_lock);
201 data->nature = UNKNOWN;
202
203 /* Tell the I2C layer a new client has arrived */
204 if ((err = i2c_attach_client(new_client)))
205 goto exit_kfree;
206
207 /* Detect the Vaio nature of EEPROMs.
208 We use the "PCG-" prefix as the signature. */
209 if (address == 0x57) {
210 if (i2c_smbus_read_byte_data(new_client, 0x80) == 'P'
211 && i2c_smbus_read_byte(new_client) == 'C'
212 && i2c_smbus_read_byte(new_client) == 'G'
213 && i2c_smbus_read_byte(new_client) == '-') {
214 dev_info(&new_client->dev, "Vaio EEPROM detected, "
215 "enabling password protection\n");
216 data->nature = VAIO;
217 }
218 }
219
220 /* create the sysfs eeprom file */
221 sysfs_create_bin_file(&new_client->dev.kobj, &eeprom_attr);
222
223 return 0;
224
225exit_kfree:
226 kfree(data);
227exit:
228 return err;
229}
230
231static int eeprom_detach_client(struct i2c_client *client)
232{
233 int err;
234
235 err = i2c_detach_client(client);
236 if (err) {
237 dev_err(&client->dev, "Client deregistration failed, client not detached.\n");
238 return err;
239 }
240
241 kfree(i2c_get_clientdata(client));
242
243 return 0;
244}
245
246static int __init eeprom_init(void)
247{
248 return i2c_add_driver(&eeprom_driver);
249}
250
251static void __exit eeprom_exit(void)
252{
253 i2c_del_driver(&eeprom_driver);
254}
255
256
257MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and "
258 "Philip Edelbrock <phil@netroedge.com> and "
259 "Greg Kroah-Hartman <greg@kroah.com>");
260MODULE_DESCRIPTION("I2C EEPROM driver");
261MODULE_LICENSE("GPL");
262
263module_init(eeprom_init);
264module_exit(eeprom_exit);
diff --git a/drivers/i2c/chips/fscher.c b/drivers/i2c/chips/fscher.c
new file mode 100644
index 000000000000..18e33ac59d0c
--- /dev/null
+++ b/drivers/i2c/chips/fscher.c
@@ -0,0 +1,692 @@
1/*
2 * fscher.c - Part of lm_sensors, Linux kernel modules for hardware
3 * monitoring
4 * Copyright (C) 2003, 2004 Reinhard Nissl <rnissl@gmx.de>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21/*
22 * fujitsu siemens hermes chip,
23 * module based on fscpos.c
24 * Copyright (C) 2000 Hermann Jung <hej@odn.de>
25 * Copyright (C) 1998, 1999 Frodo Looijaard <frodol@dds.nl>
26 * and Philip Edelbrock <phil@netroedge.com>
27 */
28
29#include <linux/config.h>
30#include <linux/module.h>
31#include <linux/init.h>
32#include <linux/slab.h>
33#include <linux/jiffies.h>
34#include <linux/i2c.h>
35#include <linux/i2c-sensor.h>
36
37/*
38 * Addresses to scan
39 */
40
41static unsigned short normal_i2c[] = { 0x73, I2C_CLIENT_END };
42static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
43
44/*
45 * Insmod parameters
46 */
47
48SENSORS_INSMOD_1(fscher);
49
50/*
51 * The FSCHER registers
52 */
53
54/* chip identification */
55#define FSCHER_REG_IDENT_0 0x00
56#define FSCHER_REG_IDENT_1 0x01
57#define FSCHER_REG_IDENT_2 0x02
58#define FSCHER_REG_REVISION 0x03
59
60/* global control and status */
61#define FSCHER_REG_EVENT_STATE 0x04
62#define FSCHER_REG_CONTROL 0x05
63
64/* watchdog */
65#define FSCHER_REG_WDOG_PRESET 0x28
66#define FSCHER_REG_WDOG_STATE 0x23
67#define FSCHER_REG_WDOG_CONTROL 0x21
68
69/* fan 0 */
70#define FSCHER_REG_FAN0_MIN 0x55
71#define FSCHER_REG_FAN0_ACT 0x0e
72#define FSCHER_REG_FAN0_STATE 0x0d
73#define FSCHER_REG_FAN0_RIPPLE 0x0f
74
75/* fan 1 */
76#define FSCHER_REG_FAN1_MIN 0x65
77#define FSCHER_REG_FAN1_ACT 0x6b
78#define FSCHER_REG_FAN1_STATE 0x62
79#define FSCHER_REG_FAN1_RIPPLE 0x6f
80
81/* fan 2 */
82#define FSCHER_REG_FAN2_MIN 0xb5
83#define FSCHER_REG_FAN2_ACT 0xbb
84#define FSCHER_REG_FAN2_STATE 0xb2
85#define FSCHER_REG_FAN2_RIPPLE 0xbf
86
87/* voltage supervision */
88#define FSCHER_REG_VOLT_12 0x45
89#define FSCHER_REG_VOLT_5 0x42
90#define FSCHER_REG_VOLT_BATT 0x48
91
92/* temperature 0 */
93#define FSCHER_REG_TEMP0_ACT 0x64
94#define FSCHER_REG_TEMP0_STATE 0x71
95
96/* temperature 1 */
97#define FSCHER_REG_TEMP1_ACT 0x32
98#define FSCHER_REG_TEMP1_STATE 0x81
99
100/* temperature 2 */
101#define FSCHER_REG_TEMP2_ACT 0x35
102#define FSCHER_REG_TEMP2_STATE 0x91
103
104/*
105 * Functions declaration
106 */
107
108static int fscher_attach_adapter(struct i2c_adapter *adapter);
109static int fscher_detect(struct i2c_adapter *adapter, int address, int kind);
110static int fscher_detach_client(struct i2c_client *client);
111static struct fscher_data *fscher_update_device(struct device *dev);
112static void fscher_init_client(struct i2c_client *client);
113
114static int fscher_read_value(struct i2c_client *client, u8 reg);
115static int fscher_write_value(struct i2c_client *client, u8 reg, u8 value);
116
117/*
118 * Driver data (common to all clients)
119 */
120
121static struct i2c_driver fscher_driver = {
122 .owner = THIS_MODULE,
123 .name = "fscher",
124 .id = I2C_DRIVERID_FSCHER,
125 .flags = I2C_DF_NOTIFY,
126 .attach_adapter = fscher_attach_adapter,
127 .detach_client = fscher_detach_client,
128};
129
130/*
131 * Client data (each client gets its own)
132 */
133
134struct fscher_data {
135 struct i2c_client client;
136 struct semaphore update_lock;
137 char valid; /* zero until following fields are valid */
138 unsigned long last_updated; /* in jiffies */
139
140 /* register values */
141 u8 revision; /* revision of chip */
142 u8 global_event; /* global event status */
143 u8 global_control; /* global control register */
144 u8 watchdog[3]; /* watchdog */
145 u8 volt[3]; /* 12, 5, battery voltage */
146 u8 temp_act[3]; /* temperature */
147 u8 temp_status[3]; /* status of sensor */
148 u8 fan_act[3]; /* fans revolutions per second */
149 u8 fan_status[3]; /* fan status */
150 u8 fan_min[3]; /* fan min value for rps */
151 u8 fan_ripple[3]; /* divider for rps */
152};
153
154/*
155 * Sysfs stuff
156 */
157
158#define sysfs_r(kind, sub, offset, reg) \
159static ssize_t show_##kind##sub (struct fscher_data *, char *, int); \
160static ssize_t show_##kind##offset##sub (struct device *, char *); \
161static ssize_t show_##kind##offset##sub (struct device *dev, char *buf) \
162{ \
163 struct fscher_data *data = fscher_update_device(dev); \
164 return show_##kind##sub(data, buf, (offset)); \
165}
166
167#define sysfs_w(kind, sub, offset, reg) \
168static ssize_t set_##kind##sub (struct i2c_client *, struct fscher_data *, const char *, size_t, int, int); \
169static ssize_t set_##kind##offset##sub (struct device *, const char *, size_t); \
170static ssize_t set_##kind##offset##sub (struct device *dev, const char *buf, size_t count) \
171{ \
172 struct i2c_client *client = to_i2c_client(dev); \
173 struct fscher_data *data = i2c_get_clientdata(client); \
174 return set_##kind##sub(client, data, buf, count, (offset), reg); \
175}
176
177#define sysfs_rw_n(kind, sub, offset, reg) \
178sysfs_r(kind, sub, offset, reg) \
179sysfs_w(kind, sub, offset, reg) \
180static DEVICE_ATTR(kind##offset##sub, S_IRUGO | S_IWUSR, show_##kind##offset##sub, set_##kind##offset##sub);
181
182#define sysfs_rw(kind, sub, reg) \
183sysfs_r(kind, sub, 0, reg) \
184sysfs_w(kind, sub, 0, reg) \
185static DEVICE_ATTR(kind##sub, S_IRUGO | S_IWUSR, show_##kind##0##sub, set_##kind##0##sub);
186
187#define sysfs_ro_n(kind, sub, offset, reg) \
188sysfs_r(kind, sub, offset, reg) \
189static DEVICE_ATTR(kind##offset##sub, S_IRUGO, show_##kind##offset##sub, NULL);
190
191#define sysfs_ro(kind, sub, reg) \
192sysfs_r(kind, sub, 0, reg) \
193static DEVICE_ATTR(kind, S_IRUGO, show_##kind##0##sub, NULL);
194
195#define sysfs_fan(offset, reg_status, reg_min, reg_ripple, reg_act) \
196sysfs_rw_n(pwm, , offset, reg_min) \
197sysfs_rw_n(fan, _status, offset, reg_status) \
198sysfs_rw_n(fan, _div , offset, reg_ripple) \
199sysfs_ro_n(fan, _input , offset, reg_act)
200
201#define sysfs_temp(offset, reg_status, reg_act) \
202sysfs_rw_n(temp, _status, offset, reg_status) \
203sysfs_ro_n(temp, _input , offset, reg_act)
204
205#define sysfs_in(offset, reg_act) \
206sysfs_ro_n(in, _input, offset, reg_act)
207
208#define sysfs_revision(reg_revision) \
209sysfs_ro(revision, , reg_revision)
210
211#define sysfs_alarms(reg_events) \
212sysfs_ro(alarms, , reg_events)
213
214#define sysfs_control(reg_control) \
215sysfs_rw(control, , reg_control)
216
217#define sysfs_watchdog(reg_control, reg_status, reg_preset) \
218sysfs_rw(watchdog, _control, reg_control) \
219sysfs_rw(watchdog, _status , reg_status) \
220sysfs_rw(watchdog, _preset , reg_preset)
221
222sysfs_fan(1, FSCHER_REG_FAN0_STATE, FSCHER_REG_FAN0_MIN,
223 FSCHER_REG_FAN0_RIPPLE, FSCHER_REG_FAN0_ACT)
224sysfs_fan(2, FSCHER_REG_FAN1_STATE, FSCHER_REG_FAN1_MIN,
225 FSCHER_REG_FAN1_RIPPLE, FSCHER_REG_FAN1_ACT)
226sysfs_fan(3, FSCHER_REG_FAN2_STATE, FSCHER_REG_FAN2_MIN,
227 FSCHER_REG_FAN2_RIPPLE, FSCHER_REG_FAN2_ACT)
228
229sysfs_temp(1, FSCHER_REG_TEMP0_STATE, FSCHER_REG_TEMP0_ACT)
230sysfs_temp(2, FSCHER_REG_TEMP1_STATE, FSCHER_REG_TEMP1_ACT)
231sysfs_temp(3, FSCHER_REG_TEMP2_STATE, FSCHER_REG_TEMP2_ACT)
232
233sysfs_in(0, FSCHER_REG_VOLT_12)
234sysfs_in(1, FSCHER_REG_VOLT_5)
235sysfs_in(2, FSCHER_REG_VOLT_BATT)
236
237sysfs_revision(FSCHER_REG_REVISION)
238sysfs_alarms(FSCHER_REG_EVENTS)
239sysfs_control(FSCHER_REG_CONTROL)
240sysfs_watchdog(FSCHER_REG_WDOG_CONTROL, FSCHER_REG_WDOG_STATE, FSCHER_REG_WDOG_PRESET)
241
242#define device_create_file_fan(client, offset) \
243do { \
244 device_create_file(&client->dev, &dev_attr_fan##offset##_status); \
245 device_create_file(&client->dev, &dev_attr_pwm##offset); \
246 device_create_file(&client->dev, &dev_attr_fan##offset##_div); \
247 device_create_file(&client->dev, &dev_attr_fan##offset##_input); \
248} while (0)
249
250#define device_create_file_temp(client, offset) \
251do { \
252 device_create_file(&client->dev, &dev_attr_temp##offset##_status); \
253 device_create_file(&client->dev, &dev_attr_temp##offset##_input); \
254} while (0)
255
256#define device_create_file_in(client, offset) \
257do { \
258 device_create_file(&client->dev, &dev_attr_in##offset##_input); \
259} while (0)
260
261#define device_create_file_revision(client) \
262do { \
263 device_create_file(&client->dev, &dev_attr_revision); \
264} while (0)
265
266#define device_create_file_alarms(client) \
267do { \
268 device_create_file(&client->dev, &dev_attr_alarms); \
269} while (0)
270
271#define device_create_file_control(client) \
272do { \
273 device_create_file(&client->dev, &dev_attr_control); \
274} while (0)
275
276#define device_create_file_watchdog(client) \
277do { \
278 device_create_file(&client->dev, &dev_attr_watchdog_status); \
279 device_create_file(&client->dev, &dev_attr_watchdog_control); \
280 device_create_file(&client->dev, &dev_attr_watchdog_preset); \
281} while (0)
282
283/*
284 * Real code
285 */
286
287static int fscher_attach_adapter(struct i2c_adapter *adapter)
288{
289 if (!(adapter->class & I2C_CLASS_HWMON))
290 return 0;
291 return i2c_detect(adapter, &addr_data, fscher_detect);
292}
293
294static int fscher_detect(struct i2c_adapter *adapter, int address, int kind)
295{
296 struct i2c_client *new_client;
297 struct fscher_data *data;
298 int err = 0;
299
300 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
301 goto exit;
302
303 /* OK. For now, we presume we have a valid client. We now create the
304 * client structure, even though we cannot fill it completely yet.
305 * But it allows us to access i2c_smbus_read_byte_data. */
306 if (!(data = kmalloc(sizeof(struct fscher_data), GFP_KERNEL))) {
307 err = -ENOMEM;
308 goto exit;
309 }
310 memset(data, 0, sizeof(struct fscher_data));
311
312 /* The common I2C client data is placed right before the
313 * Hermes-specific data. */
314 new_client = &data->client;
315 i2c_set_clientdata(new_client, data);
316 new_client->addr = address;
317 new_client->adapter = adapter;
318 new_client->driver = &fscher_driver;
319 new_client->flags = 0;
320
321 /* Do the remaining detection unless force or force_fscher parameter */
322 if (kind < 0) {
323 if ((i2c_smbus_read_byte_data(new_client,
324 FSCHER_REG_IDENT_0) != 0x48) /* 'H' */
325 || (i2c_smbus_read_byte_data(new_client,
326 FSCHER_REG_IDENT_1) != 0x45) /* 'E' */
327 || (i2c_smbus_read_byte_data(new_client,
328 FSCHER_REG_IDENT_2) != 0x52)) /* 'R' */
329 goto exit_free;
330 }
331
332 /* Fill in the remaining client fields and put it into the
333 * global list */
334 strlcpy(new_client->name, "fscher", I2C_NAME_SIZE);
335 data->valid = 0;
336 init_MUTEX(&data->update_lock);
337
338 /* Tell the I2C layer a new client has arrived */
339 if ((err = i2c_attach_client(new_client)))
340 goto exit_free;
341
342 fscher_init_client(new_client);
343
344 /* Register sysfs hooks */
345 device_create_file_revision(new_client);
346 device_create_file_alarms(new_client);
347 device_create_file_control(new_client);
348 device_create_file_watchdog(new_client);
349
350 device_create_file_in(new_client, 0);
351 device_create_file_in(new_client, 1);
352 device_create_file_in(new_client, 2);
353
354 device_create_file_fan(new_client, 1);
355 device_create_file_fan(new_client, 2);
356 device_create_file_fan(new_client, 3);
357
358 device_create_file_temp(new_client, 1);
359 device_create_file_temp(new_client, 2);
360 device_create_file_temp(new_client, 3);
361
362 return 0;
363
364exit_free:
365 kfree(data);
366exit:
367 return err;
368}
369
370static int fscher_detach_client(struct i2c_client *client)
371{
372 int err;
373
374 if ((err = i2c_detach_client(client))) {
375 dev_err(&client->dev, "Client deregistration failed, "
376 "client not detached.\n");
377 return err;
378 }
379
380 kfree(i2c_get_clientdata(client));
381 return 0;
382}
383
384static int fscher_read_value(struct i2c_client *client, u8 reg)
385{
386 dev_dbg(&client->dev, "read reg 0x%02x\n", reg);
387
388 return i2c_smbus_read_byte_data(client, reg);
389}
390
391static int fscher_write_value(struct i2c_client *client, u8 reg, u8 value)
392{
393 dev_dbg(&client->dev, "write reg 0x%02x, val 0x%02x\n",
394 reg, value);
395
396 return i2c_smbus_write_byte_data(client, reg, value);
397}
398
399/* Called when we have found a new FSC Hermes. */
400static void fscher_init_client(struct i2c_client *client)
401{
402 struct fscher_data *data = i2c_get_clientdata(client);
403
404 /* Read revision from chip */
405 data->revision = fscher_read_value(client, FSCHER_REG_REVISION);
406}
407
408static struct fscher_data *fscher_update_device(struct device *dev)
409{
410 struct i2c_client *client = to_i2c_client(dev);
411 struct fscher_data *data = i2c_get_clientdata(client);
412
413 down(&data->update_lock);
414
415 if (time_after(jiffies, data->last_updated + 2 * HZ) || !data->valid) {
416
417 dev_dbg(&client->dev, "Starting fscher update\n");
418
419 data->temp_act[0] = fscher_read_value(client, FSCHER_REG_TEMP0_ACT);
420 data->temp_act[1] = fscher_read_value(client, FSCHER_REG_TEMP1_ACT);
421 data->temp_act[2] = fscher_read_value(client, FSCHER_REG_TEMP2_ACT);
422 data->temp_status[0] = fscher_read_value(client, FSCHER_REG_TEMP0_STATE);
423 data->temp_status[1] = fscher_read_value(client, FSCHER_REG_TEMP1_STATE);
424 data->temp_status[2] = fscher_read_value(client, FSCHER_REG_TEMP2_STATE);
425
426 data->volt[0] = fscher_read_value(client, FSCHER_REG_VOLT_12);
427 data->volt[1] = fscher_read_value(client, FSCHER_REG_VOLT_5);
428 data->volt[2] = fscher_read_value(client, FSCHER_REG_VOLT_BATT);
429
430 data->fan_act[0] = fscher_read_value(client, FSCHER_REG_FAN0_ACT);
431 data->fan_act[1] = fscher_read_value(client, FSCHER_REG_FAN1_ACT);
432 data->fan_act[2] = fscher_read_value(client, FSCHER_REG_FAN2_ACT);
433 data->fan_status[0] = fscher_read_value(client, FSCHER_REG_FAN0_STATE);
434 data->fan_status[1] = fscher_read_value(client, FSCHER_REG_FAN1_STATE);
435 data->fan_status[2] = fscher_read_value(client, FSCHER_REG_FAN2_STATE);
436 data->fan_min[0] = fscher_read_value(client, FSCHER_REG_FAN0_MIN);
437 data->fan_min[1] = fscher_read_value(client, FSCHER_REG_FAN1_MIN);
438 data->fan_min[2] = fscher_read_value(client, FSCHER_REG_FAN2_MIN);
439 data->fan_ripple[0] = fscher_read_value(client, FSCHER_REG_FAN0_RIPPLE);
440 data->fan_ripple[1] = fscher_read_value(client, FSCHER_REG_FAN1_RIPPLE);
441 data->fan_ripple[2] = fscher_read_value(client, FSCHER_REG_FAN2_RIPPLE);
442
443 data->watchdog[0] = fscher_read_value(client, FSCHER_REG_WDOG_PRESET);
444 data->watchdog[1] = fscher_read_value(client, FSCHER_REG_WDOG_STATE);
445 data->watchdog[2] = fscher_read_value(client, FSCHER_REG_WDOG_CONTROL);
446
447 data->global_event = fscher_read_value(client, FSCHER_REG_EVENT_STATE);
448
449 data->last_updated = jiffies;
450 data->valid = 1;
451 }
452
453 up(&data->update_lock);
454
455 return data;
456}
457
458
459
460#define FAN_INDEX_FROM_NUM(nr) ((nr) - 1)
461
462static ssize_t set_fan_status(struct i2c_client *client, struct fscher_data *data,
463 const char *buf, size_t count, int nr, int reg)
464{
465 /* bits 0..1, 3..7 reserved => mask with 0x04 */
466 unsigned long v = simple_strtoul(buf, NULL, 10) & 0x04;
467
468 down(&data->update_lock);
469 data->fan_status[FAN_INDEX_FROM_NUM(nr)] &= ~v;
470 fscher_write_value(client, reg, v);
471 up(&data->update_lock);
472 return count;
473}
474
475static ssize_t show_fan_status(struct fscher_data *data, char *buf, int nr)
476{
477 /* bits 0..1, 3..7 reserved => mask with 0x04 */
478 return sprintf(buf, "%u\n", data->fan_status[FAN_INDEX_FROM_NUM(nr)] & 0x04);
479}
480
481static ssize_t set_pwm(struct i2c_client *client, struct fscher_data *data,
482 const char *buf, size_t count, int nr, int reg)
483{
484 unsigned long v = simple_strtoul(buf, NULL, 10);
485
486 down(&data->update_lock);
487 data->fan_min[FAN_INDEX_FROM_NUM(nr)] = v > 0xff ? 0xff : v;
488 fscher_write_value(client, reg, data->fan_min[FAN_INDEX_FROM_NUM(nr)]);
489 up(&data->update_lock);
490 return count;
491}
492
493static ssize_t show_pwm(struct fscher_data *data, char *buf, int nr)
494{
495 return sprintf(buf, "%u\n", data->fan_min[FAN_INDEX_FROM_NUM(nr)]);
496}
497
498static ssize_t set_fan_div(struct i2c_client *client, struct fscher_data *data,
499 const char *buf, size_t count, int nr, int reg)
500{
501 /* supported values: 2, 4, 8 */
502 unsigned long v = simple_strtoul(buf, NULL, 10);
503
504 switch (v) {
505 case 2: v = 1; break;
506 case 4: v = 2; break;
507 case 8: v = 3; break;
508 default:
509 dev_err(&client->dev, "fan_div value %ld not "
510 "supported. Choose one of 2, 4 or 8!\n", v);
511 return -EINVAL;
512 }
513
514 down(&data->update_lock);
515
516 /* bits 2..7 reserved => mask with 0x03 */
517 data->fan_ripple[FAN_INDEX_FROM_NUM(nr)] &= ~0x03;
518 data->fan_ripple[FAN_INDEX_FROM_NUM(nr)] |= v;
519
520 fscher_write_value(client, reg, data->fan_ripple[FAN_INDEX_FROM_NUM(nr)]);
521 up(&data->update_lock);
522 return count;
523}
524
525static ssize_t show_fan_div(struct fscher_data *data, char *buf, int nr)
526{
527 /* bits 2..7 reserved => mask with 0x03 */
528 return sprintf(buf, "%u\n", 1 << (data->fan_ripple[FAN_INDEX_FROM_NUM(nr)] & 0x03));
529}
530
531#define RPM_FROM_REG(val) (val*60)
532
533static ssize_t show_fan_input (struct fscher_data *data, char *buf, int nr)
534{
535 return sprintf(buf, "%u\n", RPM_FROM_REG(data->fan_act[FAN_INDEX_FROM_NUM(nr)]));
536}
537
538
539
540#define TEMP_INDEX_FROM_NUM(nr) ((nr) - 1)
541
542static ssize_t set_temp_status(struct i2c_client *client, struct fscher_data *data,
543 const char *buf, size_t count, int nr, int reg)
544{
545 /* bits 2..7 reserved, 0 read only => mask with 0x02 */
546 unsigned long v = simple_strtoul(buf, NULL, 10) & 0x02;
547
548 down(&data->update_lock);
549 data->temp_status[TEMP_INDEX_FROM_NUM(nr)] &= ~v;
550 fscher_write_value(client, reg, v);
551 up(&data->update_lock);
552 return count;
553}
554
555static ssize_t show_temp_status(struct fscher_data *data, char *buf, int nr)
556{
557 /* bits 2..7 reserved => mask with 0x03 */
558 return sprintf(buf, "%u\n", data->temp_status[TEMP_INDEX_FROM_NUM(nr)] & 0x03);
559}
560
561#define TEMP_FROM_REG(val) (((val) - 128) * 1000)
562
563static ssize_t show_temp_input(struct fscher_data *data, char *buf, int nr)
564{
565 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_act[TEMP_INDEX_FROM_NUM(nr)]));
566}
567
568/*
569 * The final conversion is specified in sensors.conf, as it depends on
570 * mainboard specific values. We export the registers contents as
571 * pseudo-hundredths-of-Volts (range 0V - 2.55V). Not that it makes much
572 * sense per se, but it minimizes the conversions count and keeps the
573 * values within a usual range.
574 */
575#define VOLT_FROM_REG(val) ((val) * 10)
576
577static ssize_t show_in_input(struct fscher_data *data, char *buf, int nr)
578{
579 return sprintf(buf, "%u\n", VOLT_FROM_REG(data->volt[nr]));
580}
581
582
583
584static ssize_t show_revision(struct fscher_data *data, char *buf, int nr)
585{
586 return sprintf(buf, "%u\n", data->revision);
587}
588
589
590
591static ssize_t show_alarms(struct fscher_data *data, char *buf, int nr)
592{
593 /* bits 2, 5..6 reserved => mask with 0x9b */
594 return sprintf(buf, "%u\n", data->global_event & 0x9b);
595}
596
597
598
599static ssize_t set_control(struct i2c_client *client, struct fscher_data *data,
600 const char *buf, size_t count, int nr, int reg)
601{
602 /* bits 1..7 reserved => mask with 0x01 */
603 unsigned long v = simple_strtoul(buf, NULL, 10) & 0x01;
604
605 down(&data->update_lock);
606 data->global_control &= ~v;
607 fscher_write_value(client, reg, v);
608 up(&data->update_lock);
609 return count;
610}
611
612static ssize_t show_control(struct fscher_data *data, char *buf, int nr)
613{
614 /* bits 1..7 reserved => mask with 0x01 */
615 return sprintf(buf, "%u\n", data->global_control & 0x01);
616}
617
618
619
620static ssize_t set_watchdog_control(struct i2c_client *client, struct
621 fscher_data *data, const char *buf, size_t count,
622 int nr, int reg)
623{
624 /* bits 0..3 reserved => mask with 0xf0 */
625 unsigned long v = simple_strtoul(buf, NULL, 10) & 0xf0;
626
627 down(&data->update_lock);
628 data->watchdog[2] &= ~0xf0;
629 data->watchdog[2] |= v;
630 fscher_write_value(client, reg, data->watchdog[2]);
631 up(&data->update_lock);
632 return count;
633}
634
635static ssize_t show_watchdog_control(struct fscher_data *data, char *buf, int nr)
636{
637 /* bits 0..3 reserved, bit 5 write only => mask with 0xd0 */
638 return sprintf(buf, "%u\n", data->watchdog[2] & 0xd0);
639}
640
641static ssize_t set_watchdog_status(struct i2c_client *client, struct fscher_data *data,
642 const char *buf, size_t count, int nr, int reg)
643{
644 /* bits 0, 2..7 reserved => mask with 0x02 */
645 unsigned long v = simple_strtoul(buf, NULL, 10) & 0x02;
646
647 down(&data->update_lock);
648 data->watchdog[1] &= ~v;
649 fscher_write_value(client, reg, v);
650 up(&data->update_lock);
651 return count;
652}
653
654static ssize_t show_watchdog_status(struct fscher_data *data, char *buf, int nr)
655{
656 /* bits 0, 2..7 reserved => mask with 0x02 */
657 return sprintf(buf, "%u\n", data->watchdog[1] & 0x02);
658}
659
660static ssize_t set_watchdog_preset(struct i2c_client *client, struct fscher_data *data,
661 const char *buf, size_t count, int nr, int reg)
662{
663 unsigned long v = simple_strtoul(buf, NULL, 10) & 0xff;
664
665 down(&data->update_lock);
666 data->watchdog[0] = v;
667 fscher_write_value(client, reg, data->watchdog[0]);
668 up(&data->update_lock);
669 return count;
670}
671
672static ssize_t show_watchdog_preset(struct fscher_data *data, char *buf, int nr)
673{
674 return sprintf(buf, "%u\n", data->watchdog[0]);
675}
676
677static int __init sensors_fscher_init(void)
678{
679 return i2c_add_driver(&fscher_driver);
680}
681
682static void __exit sensors_fscher_exit(void)
683{
684 i2c_del_driver(&fscher_driver);
685}
686
687MODULE_AUTHOR("Reinhard Nissl <rnissl@gmx.de>");
688MODULE_DESCRIPTION("FSC Hermes driver");
689MODULE_LICENSE("GPL");
690
691module_init(sensors_fscher_init);
692module_exit(sensors_fscher_exit);
diff --git a/drivers/i2c/chips/fscpos.c b/drivers/i2c/chips/fscpos.c
new file mode 100644
index 000000000000..2cac79145c75
--- /dev/null
+++ b/drivers/i2c/chips/fscpos.c
@@ -0,0 +1,641 @@
1/*
2 fscpos.c - Kernel module for hardware monitoring with FSC Poseidon chips
3 Copyright (C) 2004, 2005 Stefan Ott <stefan@desire.ch>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18*/
19
20/*
21 fujitsu siemens poseidon chip,
22 module based on the old fscpos module by Hermann Jung <hej@odn.de> and
23 the fscher module by Reinhard Nissl <rnissl@gmx.de>
24
25 original module based on lm80.c
26 Copyright (C) 1998, 1999 Frodo Looijaard <frodol@dds.nl>
27 and Philip Edelbrock <phil@netroedge.com>
28
29 Thanks to Jean Delvare for reviewing my code and suggesting a lot of
30 improvements.
31*/
32
33#include <linux/module.h>
34#include <linux/slab.h>
35#include <linux/i2c.h>
36#include <linux/i2c-sensor.h>
37#include <linux/init.h>
38
39/*
40 * Addresses to scan
41 */
42static unsigned short normal_i2c[] = { 0x73, I2C_CLIENT_END };
43static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
44
45/*
46 * Insmod parameters
47 */
48SENSORS_INSMOD_1(fscpos);
49
50/*
51 * The FSCPOS registers
52 */
53
54/* chip identification */
55#define FSCPOS_REG_IDENT_0 0x00
56#define FSCPOS_REG_IDENT_1 0x01
57#define FSCPOS_REG_IDENT_2 0x02
58#define FSCPOS_REG_REVISION 0x03
59
60/* global control and status */
61#define FSCPOS_REG_EVENT_STATE 0x04
62#define FSCPOS_REG_CONTROL 0x05
63
64/* watchdog */
65#define FSCPOS_REG_WDOG_PRESET 0x28
66#define FSCPOS_REG_WDOG_STATE 0x23
67#define FSCPOS_REG_WDOG_CONTROL 0x21
68
69/* voltages */
70#define FSCPOS_REG_VOLT_12 0x45
71#define FSCPOS_REG_VOLT_5 0x42
72#define FSCPOS_REG_VOLT_BATT 0x48
73
74/* fans - the chip does not support minimum speed for fan2 */
75static u8 FSCPOS_REG_PWM[] = { 0x55, 0x65 };
76static u8 FSCPOS_REG_FAN_ACT[] = { 0x0e, 0x6b, 0xab };
77static u8 FSCPOS_REG_FAN_STATE[] = { 0x0d, 0x62, 0xa2 };
78static u8 FSCPOS_REG_FAN_RIPPLE[] = { 0x0f, 0x6f, 0xaf };
79
80/* temperatures */
81static u8 FSCPOS_REG_TEMP_ACT[] = { 0x64, 0x32, 0x35 };
82static u8 FSCPOS_REG_TEMP_STATE[] = { 0x71, 0x81, 0x91 };
83
84/*
85 * Functions declaration
86 */
87static int fscpos_attach_adapter(struct i2c_adapter *adapter);
88static int fscpos_detect(struct i2c_adapter *adapter, int address, int kind);
89static int fscpos_detach_client(struct i2c_client *client);
90
91static int fscpos_read_value(struct i2c_client *client, u8 register);
92static int fscpos_write_value(struct i2c_client *client, u8 register, u8 value);
93static struct fscpos_data *fscpos_update_device(struct device *dev);
94static void fscpos_init_client(struct i2c_client *client);
95
96static void reset_fan_alarm(struct i2c_client *client, int nr);
97
98/*
99 * Driver data (common to all clients)
100 */
101static struct i2c_driver fscpos_driver = {
102 .owner = THIS_MODULE,
103 .name = "fscpos",
104 .id = I2C_DRIVERID_FSCPOS,
105 .flags = I2C_DF_NOTIFY,
106 .attach_adapter = fscpos_attach_adapter,
107 .detach_client = fscpos_detach_client,
108};
109
110/*
111 * Client data (each client gets its own)
112 */
113struct fscpos_data {
114 struct i2c_client client;
115 struct semaphore update_lock;
116 char valid; /* 0 until following fields are valid */
117 unsigned long last_updated; /* In jiffies */
118
119 /* register values */
120 u8 revision; /* revision of chip */
121 u8 global_event; /* global event status */
122 u8 global_control; /* global control register */
123 u8 wdog_control; /* watchdog control */
124 u8 wdog_state; /* watchdog status */
125 u8 wdog_preset; /* watchdog preset */
126 u8 volt[3]; /* 12, 5, battery current */
127 u8 temp_act[3]; /* temperature */
128 u8 temp_status[3]; /* status of sensor */
129 u8 fan_act[3]; /* fans revolutions per second */
130 u8 fan_status[3]; /* fan status */
131 u8 pwm[2]; /* fan min value for rps */
132 u8 fan_ripple[3]; /* divider for rps */
133};
134
135/* Temperature */
136#define TEMP_FROM_REG(val) (((val) - 128) * 1000)
137
138static ssize_t show_temp_input(struct fscpos_data *data, char *buf, int nr)
139{
140 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_act[nr - 1]));
141}
142
143static ssize_t show_temp_status(struct fscpos_data *data, char *buf, int nr)
144{
145 /* bits 2..7 reserved => mask with 0x03 */
146 return sprintf(buf, "%u\n", data->temp_status[nr - 1] & 0x03);
147}
148
149static ssize_t show_temp_reset(struct fscpos_data *data, char *buf, int nr)
150{
151 return sprintf(buf, "1\n");
152}
153
154static ssize_t set_temp_reset(struct i2c_client *client, struct fscpos_data
155 *data, const char *buf, size_t count, int nr, int reg)
156{
157 unsigned long v = simple_strtoul(buf, NULL, 10);
158 if (v != 1) {
159 dev_err(&client->dev, "temp_reset value %ld not supported. "
160 "Use 1 to reset the alarm!\n", v);
161 return -EINVAL;
162 }
163
164 dev_info(&client->dev, "You used the temp_reset feature which has not "
165 "been proplerly tested. Please report your "
166 "experience to the module author.\n");
167
168 /* Supported value: 2 (clears the status) */
169 fscpos_write_value(client, FSCPOS_REG_TEMP_STATE[nr], 2);
170 return count;
171}
172
173/* Fans */
174#define RPM_FROM_REG(val) ((val) * 60)
175
176static ssize_t show_fan_status(struct fscpos_data *data, char *buf, int nr)
177{
178 /* bits 0..1, 3..7 reserved => mask with 0x04 */
179 return sprintf(buf, "%u\n", data->fan_status[nr - 1] & 0x04);
180}
181
182static ssize_t show_fan_input(struct fscpos_data *data, char *buf, int nr)
183{
184 return sprintf(buf, "%u\n", RPM_FROM_REG(data->fan_act[nr - 1]));
185}
186
187static ssize_t show_fan_ripple(struct fscpos_data *data, char *buf, int nr)
188{
189 /* bits 2..7 reserved => mask with 0x03 */
190 return sprintf(buf, "%u\n", data->fan_ripple[nr - 1] & 0x03);
191}
192
193static ssize_t set_fan_ripple(struct i2c_client *client, struct fscpos_data
194 *data, const char *buf, size_t count, int nr, int reg)
195{
196 /* supported values: 2, 4, 8 */
197 unsigned long v = simple_strtoul(buf, NULL, 10);
198
199 switch (v) {
200 case 2: v = 1; break;
201 case 4: v = 2; break;
202 case 8: v = 3; break;
203 default:
204 dev_err(&client->dev, "fan_ripple value %ld not supported. "
205 "Must be one of 2, 4 or 8!\n", v);
206 return -EINVAL;
207 }
208
209 down(&data->update_lock);
210 /* bits 2..7 reserved => mask with 0x03 */
211 data->fan_ripple[nr - 1] &= ~0x03;
212 data->fan_ripple[nr - 1] |= v;
213
214 fscpos_write_value(client, reg, data->fan_ripple[nr - 1]);
215 up(&data->update_lock);
216 return count;
217}
218
219static ssize_t show_pwm(struct fscpos_data *data, char *buf, int nr)
220{
221 return sprintf(buf, "%u\n", data->pwm[nr - 1]);
222}
223
224static ssize_t set_pwm(struct i2c_client *client, struct fscpos_data *data,
225 const char *buf, size_t count, int nr, int reg)
226{
227 unsigned long v = simple_strtoul(buf, NULL, 10);
228
229 /* Range: 0..255 */
230 if (v < 0) v = 0;
231 if (v > 255) v = 255;
232
233 down(&data->update_lock);
234 data->pwm[nr - 1] = v;
235 fscpos_write_value(client, reg, data->pwm[nr - 1]);
236 up(&data->update_lock);
237 return count;
238}
239
240static void reset_fan_alarm(struct i2c_client *client, int nr)
241{
242 fscpos_write_value(client, FSCPOS_REG_FAN_STATE[nr], 4);
243}
244
245/* Volts */
246#define VOLT_FROM_REG(val, mult) ((val) * (mult) / 255)
247
248static ssize_t show_volt_12(struct device *dev, char *buf)
249{
250 struct fscpos_data *data = fscpos_update_device(dev);
251 return sprintf(buf, "%u\n", VOLT_FROM_REG(data->volt[0], 14200));
252}
253
254static ssize_t show_volt_5(struct device *dev, char *buf)
255{
256 struct fscpos_data *data = fscpos_update_device(dev);
257 return sprintf(buf, "%u\n", VOLT_FROM_REG(data->volt[1], 6600));
258}
259
260static ssize_t show_volt_batt(struct device *dev, char *buf)
261{
262 struct fscpos_data *data = fscpos_update_device(dev);
263 return sprintf(buf, "%u\n", VOLT_FROM_REG(data->volt[2], 3300));
264}
265
266/* Watchdog */
267static ssize_t show_wdog_control(struct fscpos_data *data, char *buf)
268{
269 /* bits 0..3 reserved, bit 6 write only => mask with 0xb0 */
270 return sprintf(buf, "%u\n", data->wdog_control & 0xb0);
271}
272
273static ssize_t set_wdog_control(struct i2c_client *client, struct fscpos_data
274 *data, const char *buf, size_t count, int reg)
275{
276 /* bits 0..3 reserved => mask with 0xf0 */
277 unsigned long v = simple_strtoul(buf, NULL, 10) & 0xf0;
278
279 down(&data->update_lock);
280 data->wdog_control &= ~0xf0;
281 data->wdog_control |= v;
282 fscpos_write_value(client, reg, data->wdog_control);
283 up(&data->update_lock);
284 return count;
285}
286
287static ssize_t show_wdog_state(struct fscpos_data *data, char *buf)
288{
289 /* bits 0, 2..7 reserved => mask with 0x02 */
290 return sprintf(buf, "%u\n", data->wdog_state & 0x02);
291}
292
293static ssize_t set_wdog_state(struct i2c_client *client, struct fscpos_data
294 *data, const char *buf, size_t count, int reg)
295{
296 unsigned long v = simple_strtoul(buf, NULL, 10) & 0x02;
297
298 /* Valid values: 2 (clear) */
299 if (v != 2) {
300 dev_err(&client->dev, "wdog_state value %ld not supported. "
301 "Must be 2 to clear the state!\n", v);
302 return -EINVAL;
303 }
304
305 down(&data->update_lock);
306 data->wdog_state &= ~v;
307 fscpos_write_value(client, reg, v);
308 up(&data->update_lock);
309 return count;
310}
311
312static ssize_t show_wdog_preset(struct fscpos_data *data, char *buf)
313{
314 return sprintf(buf, "%u\n", data->wdog_preset);
315}
316
317static ssize_t set_wdog_preset(struct i2c_client *client, struct fscpos_data
318 *data, const char *buf, size_t count, int reg)
319{
320 unsigned long v = simple_strtoul(buf, NULL, 10) & 0xff;
321
322 down(&data->update_lock);
323 data->wdog_preset = v;
324 fscpos_write_value(client, reg, data->wdog_preset);
325 up(&data->update_lock);
326 return count;
327}
328
329/* Event */
330static ssize_t show_event(struct device *dev, char *buf)
331{
332 /* bits 5..7 reserved => mask with 0x1f */
333 struct fscpos_data *data = fscpos_update_device(dev);
334 return sprintf(buf, "%u\n", data->global_event & 0x9b);
335}
336
337/*
338 * Sysfs stuff
339 */
340#define create_getter(kind, sub) \
341 static ssize_t sysfs_show_##kind##sub(struct device *dev, char *buf) \
342 { \
343 struct fscpos_data *data = fscpos_update_device(dev); \
344 return show_##kind##sub(data, buf); \
345 }
346
347#define create_getter_n(kind, offset, sub) \
348 static ssize_t sysfs_show_##kind##offset##sub(struct device *dev, char\
349 *buf) \
350 { \
351 struct fscpos_data *data = fscpos_update_device(dev); \
352 return show_##kind##sub(data, buf, offset); \
353 }
354
355#define create_setter(kind, sub, reg) \
356 static ssize_t sysfs_set_##kind##sub (struct device *dev, const char \
357 *buf, size_t count) \
358 { \
359 struct i2c_client *client = to_i2c_client(dev); \
360 struct fscpos_data *data = i2c_get_clientdata(client); \
361 return set_##kind##sub(client, data, buf, count, reg); \
362 }
363
364#define create_setter_n(kind, offset, sub, reg) \
365 static ssize_t sysfs_set_##kind##offset##sub (struct device *dev, \
366 const char *buf, size_t count) \
367 { \
368 struct i2c_client *client = to_i2c_client(dev); \
369 struct fscpos_data *data = i2c_get_clientdata(client); \
370 return set_##kind##sub(client, data, buf, count, offset, reg);\
371 }
372
373#define create_sysfs_device_ro(kind, sub, offset) \
374 static DEVICE_ATTR(kind##offset##sub, S_IRUGO, \
375 sysfs_show_##kind##offset##sub, NULL);
376
377#define create_sysfs_device_rw(kind, sub, offset) \
378 static DEVICE_ATTR(kind##offset##sub, S_IRUGO | S_IWUSR, \
379 sysfs_show_##kind##offset##sub, sysfs_set_##kind##offset##sub);
380
381#define sysfs_ro_n(kind, sub, offset) \
382 create_getter_n(kind, offset, sub); \
383 create_sysfs_device_ro(kind, sub, offset);
384
385#define sysfs_rw_n(kind, sub, offset, reg) \
386 create_getter_n(kind, offset, sub); \
387 create_setter_n(kind, offset, sub, reg); \
388 create_sysfs_device_rw(kind, sub, offset);
389
390#define sysfs_rw(kind, sub, reg) \
391 create_getter(kind, sub); \
392 create_setter(kind, sub, reg); \
393 create_sysfs_device_rw(kind, sub,);
394
395#define sysfs_fan_with_min(offset, reg_status, reg_ripple, reg_min) \
396 sysfs_fan(offset, reg_status, reg_ripple); \
397 sysfs_rw_n(pwm,, offset, reg_min);
398
399#define sysfs_fan(offset, reg_status, reg_ripple) \
400 sysfs_ro_n(fan, _input, offset); \
401 sysfs_ro_n(fan, _status, offset); \
402 sysfs_rw_n(fan, _ripple, offset, reg_ripple);
403
404#define sysfs_temp(offset, reg_status) \
405 sysfs_ro_n(temp, _input, offset); \
406 sysfs_ro_n(temp, _status, offset); \
407 sysfs_rw_n(temp, _reset, offset, reg_status);
408
409#define sysfs_watchdog(reg_wdog_preset, reg_wdog_state, reg_wdog_control) \
410 sysfs_rw(wdog, _control, reg_wdog_control); \
411 sysfs_rw(wdog, _preset, reg_wdog_preset); \
412 sysfs_rw(wdog, _state, reg_wdog_state);
413
414sysfs_fan_with_min(1, FSCPOS_REG_FAN_STATE[0], FSCPOS_REG_FAN_RIPPLE[0],
415 FSCPOS_REG_PWM[0]);
416sysfs_fan_with_min(2, FSCPOS_REG_FAN_STATE[1], FSCPOS_REG_FAN_RIPPLE[1],
417 FSCPOS_REG_PWM[1]);
418sysfs_fan(3, FSCPOS_REG_FAN_STATE[2], FSCPOS_REG_FAN_RIPPLE[2]);
419
420sysfs_temp(1, FSCPOS_REG_TEMP_STATE[0]);
421sysfs_temp(2, FSCPOS_REG_TEMP_STATE[1]);
422sysfs_temp(3, FSCPOS_REG_TEMP_STATE[2]);
423
424sysfs_watchdog(FSCPOS_REG_WDOG_PRESET, FSCPOS_REG_WDOG_STATE,
425 FSCPOS_REG_WDOG_CONTROL);
426
427static DEVICE_ATTR(event, S_IRUGO, show_event, NULL);
428static DEVICE_ATTR(in0_input, S_IRUGO, show_volt_12, NULL);
429static DEVICE_ATTR(in1_input, S_IRUGO, show_volt_5, NULL);
430static DEVICE_ATTR(in2_input, S_IRUGO, show_volt_batt, NULL);
431
432static int fscpos_attach_adapter(struct i2c_adapter *adapter)
433{
434 if (!(adapter->class & I2C_CLASS_HWMON))
435 return 0;
436 return i2c_detect(adapter, &addr_data, fscpos_detect);
437}
438
439int fscpos_detect(struct i2c_adapter *adapter, int address, int kind)
440{
441 struct i2c_client *new_client;
442 struct fscpos_data *data;
443 int err = 0;
444
445 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
446 goto exit;
447
448 /*
449 * OK. For now, we presume we have a valid client. We now create the
450 * client structure, even though we cannot fill it completely yet.
451 * But it allows us to access fscpos_{read,write}_value.
452 */
453
454 if (!(data = kmalloc(sizeof(struct fscpos_data), GFP_KERNEL))) {
455 err = -ENOMEM;
456 goto exit;
457 }
458 memset(data, 0, sizeof(struct fscpos_data));
459
460 new_client = &data->client;
461 i2c_set_clientdata(new_client, data);
462 new_client->addr = address;
463 new_client->adapter = adapter;
464 new_client->driver = &fscpos_driver;
465 new_client->flags = 0;
466
467 /* Do the remaining detection unless force or force_fscpos parameter */
468 if (kind < 0) {
469 if ((fscpos_read_value(new_client, FSCPOS_REG_IDENT_0)
470 != 0x50) /* 'P' */
471 || (fscpos_read_value(new_client, FSCPOS_REG_IDENT_1)
472 != 0x45) /* 'E' */
473 || (fscpos_read_value(new_client, FSCPOS_REG_IDENT_2)
474 != 0x47))/* 'G' */
475 {
476 dev_dbg(&new_client->dev, "fscpos detection failed\n");
477 goto exit_free;
478 }
479 }
480
481 /* Fill in the remaining client fields and put it in the global list */
482 strlcpy(new_client->name, "fscpos", I2C_NAME_SIZE);
483
484 data->valid = 0;
485 init_MUTEX(&data->update_lock);
486
487 /* Tell the I2C layer a new client has arrived */
488 if ((err = i2c_attach_client(new_client)))
489 goto exit_free;
490
491 /* Inizialize the fscpos chip */
492 fscpos_init_client(new_client);
493
494 /* Announce that the chip was found */
495 dev_info(&new_client->dev, "Found fscpos chip, rev %u\n", data->revision);
496
497 /* Register sysfs hooks */
498 device_create_file(&new_client->dev, &dev_attr_event);
499 device_create_file(&new_client->dev, &dev_attr_in0_input);
500 device_create_file(&new_client->dev, &dev_attr_in1_input);
501 device_create_file(&new_client->dev, &dev_attr_in2_input);
502 device_create_file(&new_client->dev, &dev_attr_wdog_control);
503 device_create_file(&new_client->dev, &dev_attr_wdog_preset);
504 device_create_file(&new_client->dev, &dev_attr_wdog_state);
505 device_create_file(&new_client->dev, &dev_attr_temp1_input);
506 device_create_file(&new_client->dev, &dev_attr_temp1_status);
507 device_create_file(&new_client->dev, &dev_attr_temp1_reset);
508 device_create_file(&new_client->dev, &dev_attr_temp2_input);
509 device_create_file(&new_client->dev, &dev_attr_temp2_status);
510 device_create_file(&new_client->dev, &dev_attr_temp2_reset);
511 device_create_file(&new_client->dev, &dev_attr_temp3_input);
512 device_create_file(&new_client->dev, &dev_attr_temp3_status);
513 device_create_file(&new_client->dev, &dev_attr_temp3_reset);
514 device_create_file(&new_client->dev, &dev_attr_fan1_input);
515 device_create_file(&new_client->dev, &dev_attr_fan1_status);
516 device_create_file(&new_client->dev, &dev_attr_fan1_ripple);
517 device_create_file(&new_client->dev, &dev_attr_pwm1);
518 device_create_file(&new_client->dev, &dev_attr_fan2_input);
519 device_create_file(&new_client->dev, &dev_attr_fan2_status);
520 device_create_file(&new_client->dev, &dev_attr_fan2_ripple);
521 device_create_file(&new_client->dev, &dev_attr_pwm2);
522 device_create_file(&new_client->dev, &dev_attr_fan3_input);
523 device_create_file(&new_client->dev, &dev_attr_fan3_status);
524 device_create_file(&new_client->dev, &dev_attr_fan3_ripple);
525
526 return 0;
527
528exit_free:
529 kfree(data);
530exit:
531 return err;
532}
533
534static int fscpos_detach_client(struct i2c_client *client)
535{
536 int err;
537
538 if ((err = i2c_detach_client(client))) {
539 dev_err(&client->dev, "Client deregistration failed, client"
540 " not detached.\n");
541 return err;
542 }
543 kfree(i2c_get_clientdata(client));
544 return 0;
545}
546
547static int fscpos_read_value(struct i2c_client *client, u8 reg)
548{
549 dev_dbg(&client->dev, "Read reg 0x%02x\n", reg);
550 return i2c_smbus_read_byte_data(client, reg);
551}
552
553static int fscpos_write_value(struct i2c_client *client, u8 reg, u8 value)
554{
555 dev_dbg(&client->dev, "Write reg 0x%02x, val 0x%02x\n", reg, value);
556 return i2c_smbus_write_byte_data(client, reg, value);
557}
558
559/* Called when we have found a new FSCPOS chip */
560static void fscpos_init_client(struct i2c_client *client)
561{
562 struct fscpos_data *data = i2c_get_clientdata(client);
563
564 /* read revision from chip */
565 data->revision = fscpos_read_value(client, FSCPOS_REG_REVISION);
566}
567
568static struct fscpos_data *fscpos_update_device(struct device *dev)
569{
570 struct i2c_client *client = to_i2c_client(dev);
571 struct fscpos_data *data = i2c_get_clientdata(client);
572
573 down(&data->update_lock);
574
575 if ((jiffies - data->last_updated > 2 * HZ) ||
576 (jiffies < data->last_updated) || !data->valid) {
577 int i;
578
579 dev_dbg(&client->dev, "Starting fscpos update\n");
580
581 for (i = 0; i < 3; i++) {
582 data->temp_act[i] = fscpos_read_value(client,
583 FSCPOS_REG_TEMP_ACT[i]);
584 data->temp_status[i] = fscpos_read_value(client,
585 FSCPOS_REG_TEMP_STATE[i]);
586 data->fan_act[i] = fscpos_read_value(client,
587 FSCPOS_REG_FAN_ACT[i]);
588 data->fan_status[i] = fscpos_read_value(client,
589 FSCPOS_REG_FAN_STATE[i]);
590 data->fan_ripple[i] = fscpos_read_value(client,
591 FSCPOS_REG_FAN_RIPPLE[i]);
592 if (i < 2) {
593 /* fan2_min is not supported by the chip */
594 data->pwm[i] = fscpos_read_value(client,
595 FSCPOS_REG_PWM[i]);
596 }
597 /* reset fan status if speed is back to > 0 */
598 if (data->fan_status[i] != 0 && data->fan_act[i] > 0) {
599 reset_fan_alarm(client, i);
600 }
601 }
602
603 data->volt[0] = fscpos_read_value(client, FSCPOS_REG_VOLT_12);
604 data->volt[1] = fscpos_read_value(client, FSCPOS_REG_VOLT_5);
605 data->volt[2] = fscpos_read_value(client, FSCPOS_REG_VOLT_BATT);
606
607 data->wdog_preset = fscpos_read_value(client,
608 FSCPOS_REG_WDOG_PRESET);
609 data->wdog_state = fscpos_read_value(client,
610 FSCPOS_REG_WDOG_STATE);
611 data->wdog_control = fscpos_read_value(client,
612 FSCPOS_REG_WDOG_CONTROL);
613
614 data->global_event = fscpos_read_value(client,
615 FSCPOS_REG_EVENT_STATE);
616
617 data->last_updated = jiffies;
618 data->valid = 1;
619 }
620 up(&data->update_lock);
621 return data;
622}
623
624static int __init sm_fscpos_init(void)
625{
626 return i2c_add_driver(&fscpos_driver);
627}
628
629static void __exit sm_fscpos_exit(void)
630{
631 i2c_del_driver(&fscpos_driver);
632}
633
634MODULE_AUTHOR("Stefan Ott <stefan@desire.ch> based on work from Hermann Jung "
635 "<hej@odn.de>, Frodo Looijaard <frodol@dds.nl>"
636 " and Philip Edelbrock <phil@netroedge.com>");
637MODULE_DESCRIPTION("fujitsu siemens poseidon chip driver");
638MODULE_LICENSE("GPL");
639
640module_init(sm_fscpos_init);
641module_exit(sm_fscpos_exit);
diff --git a/drivers/i2c/chips/gl518sm.c b/drivers/i2c/chips/gl518sm.c
new file mode 100644
index 000000000000..c82d6ce21205
--- /dev/null
+++ b/drivers/i2c/chips/gl518sm.c
@@ -0,0 +1,605 @@
1/*
2 * gl518sm.c - Part of lm_sensors, Linux kernel modules for hardware
3 * monitoring
4 * Copyright (C) 1998, 1999 Frodo Looijaard <frodol@dds.nl> and
5 * Kyosti Malkki <kmalkki@cc.hut.fi>
6 * Copyright (C) 2004 Hong-Gunn Chew <hglinux@gunnet.org> and
7 * Jean Delvare <khali@linux-fr.org>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
23 * Ported to Linux 2.6 by Hong-Gunn Chew with the help of Jean Delvare
24 * and advice of Greg Kroah-Hartman.
25 *
26 * Notes about the port:
27 * Release 0x00 of the GL518SM chipset doesn't support reading of in0,
28 * in1 nor in2. The original driver had an ugly workaround to get them
29 * anyway (changing limits and watching alarms trigger and wear off).
30 * We did not keep that part of the original driver in the Linux 2.6
31 * version, since it was making the driver significantly more complex
32 * with no real benefit.
33 *
34 * History:
35 * 2004-01-28 Original port. (Hong-Gunn Chew)
36 * 2004-01-31 Code review and approval. (Jean Delvare)
37 */
38
39#include <linux/config.h>
40#include <linux/module.h>
41#include <linux/init.h>
42#include <linux/slab.h>
43#include <linux/jiffies.h>
44#include <linux/i2c.h>
45#include <linux/i2c-sensor.h>
46
47/* Addresses to scan */
48static unsigned short normal_i2c[] = { 0x2c, 0x2d, I2C_CLIENT_END };
49static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
50
51/* Insmod parameters */
52SENSORS_INSMOD_2(gl518sm_r00, gl518sm_r80);
53
54/* Many GL518 constants specified below */
55
56/* The GL518 registers */
57#define GL518_REG_CHIP_ID 0x00
58#define GL518_REG_REVISION 0x01
59#define GL518_REG_VENDOR_ID 0x02
60#define GL518_REG_CONF 0x03
61#define GL518_REG_TEMP_IN 0x04
62#define GL518_REG_TEMP_MAX 0x05
63#define GL518_REG_TEMP_HYST 0x06
64#define GL518_REG_FAN_COUNT 0x07
65#define GL518_REG_FAN_LIMIT 0x08
66#define GL518_REG_VIN1_LIMIT 0x09
67#define GL518_REG_VIN2_LIMIT 0x0a
68#define GL518_REG_VIN3_LIMIT 0x0b
69#define GL518_REG_VDD_LIMIT 0x0c
70#define GL518_REG_VIN3 0x0d
71#define GL518_REG_MISC 0x0f
72#define GL518_REG_ALARM 0x10
73#define GL518_REG_MASK 0x11
74#define GL518_REG_INT 0x12
75#define GL518_REG_VIN2 0x13
76#define GL518_REG_VIN1 0x14
77#define GL518_REG_VDD 0x15
78
79
80/*
81 * Conversions. Rounding and limit checking is only done on the TO_REG
82 * variants. Note that you should be a bit careful with which arguments
83 * these macros are called: arguments may be evaluated more than once.
84 * Fixing this is just not worth it.
85 */
86
87#define RAW_FROM_REG(val) val
88
89#define BOOL_FROM_REG(val) ((val)?0:1)
90#define BOOL_TO_REG(val) ((val)?0:1)
91
92#define TEMP_TO_REG(val) (SENSORS_LIMIT(((((val)<0? \
93 (val)-500:(val)+500)/1000)+119),0,255))
94#define TEMP_FROM_REG(val) (((val) - 119) * 1000)
95
96static inline u8 FAN_TO_REG(long rpm, int div)
97{
98 long rpmdiv;
99 if (rpm == 0)
100 return 0;
101 rpmdiv = SENSORS_LIMIT(rpm, 1, 1920000) * div;
102 return SENSORS_LIMIT((960000 + rpmdiv / 2) / rpmdiv, 1, 255);
103}
104#define FAN_FROM_REG(val,div) ((val)==0 ? 0 : (960000/((val)*(div))))
105
106#define IN_TO_REG(val) (SENSORS_LIMIT((((val)+9)/19),0,255))
107#define IN_FROM_REG(val) ((val)*19)
108
109#define VDD_TO_REG(val) (SENSORS_LIMIT((((val)*4+47)/95),0,255))
110#define VDD_FROM_REG(val) (((val)*95+2)/4)
111
112#define DIV_TO_REG(val) ((val)==4?2:(val)==2?1:(val)==1?0:3)
113#define DIV_FROM_REG(val) (1 << (val))
114
115#define BEEP_MASK_TO_REG(val) ((val) & 0x7f & data->alarm_mask)
116#define BEEP_MASK_FROM_REG(val) ((val) & 0x7f)
117
118/* Each client has this additional data */
119struct gl518_data {
120 struct i2c_client client;
121 enum chips type;
122
123 struct semaphore update_lock;
124 char valid; /* !=0 if following fields are valid */
125 unsigned long last_updated; /* In jiffies */
126
127 u8 voltage_in[4]; /* Register values; [0] = VDD */
128 u8 voltage_min[4]; /* Register values; [0] = VDD */
129 u8 voltage_max[4]; /* Register values; [0] = VDD */
130 u8 iter_voltage_in[4]; /* Register values; [0] = VDD */
131 u8 fan_in[2];
132 u8 fan_min[2];
133 u8 fan_div[2]; /* Register encoding, shifted right */
134 u8 fan_auto1; /* Boolean */
135 u8 temp_in; /* Register values */
136 u8 temp_max; /* Register values */
137 u8 temp_hyst; /* Register values */
138 u8 alarms; /* Register value */
139 u8 alarm_mask; /* Register value */
140 u8 beep_mask; /* Register value */
141 u8 beep_enable; /* Boolean */
142};
143
144static int gl518_attach_adapter(struct i2c_adapter *adapter);
145static int gl518_detect(struct i2c_adapter *adapter, int address, int kind);
146static void gl518_init_client(struct i2c_client *client);
147static int gl518_detach_client(struct i2c_client *client);
148static int gl518_read_value(struct i2c_client *client, u8 reg);
149static int gl518_write_value(struct i2c_client *client, u8 reg, u16 value);
150static struct gl518_data *gl518_update_device(struct device *dev);
151
152/* This is the driver that will be inserted */
153static struct i2c_driver gl518_driver = {
154 .owner = THIS_MODULE,
155 .name = "gl518sm",
156 .id = I2C_DRIVERID_GL518,
157 .flags = I2C_DF_NOTIFY,
158 .attach_adapter = gl518_attach_adapter,
159 .detach_client = gl518_detach_client,
160};
161
162/*
163 * Sysfs stuff
164 */
165
166#define show(type, suffix, value) \
167static ssize_t show_##suffix(struct device *dev, char *buf) \
168{ \
169 struct gl518_data *data = gl518_update_device(dev); \
170 return sprintf(buf, "%d\n", type##_FROM_REG(data->value)); \
171}
172
173#define show_fan(suffix, value, index) \
174static ssize_t show_##suffix(struct device *dev, char *buf) \
175{ \
176 struct gl518_data *data = gl518_update_device(dev); \
177 return sprintf(buf, "%d\n", FAN_FROM_REG(data->value[index], \
178 DIV_FROM_REG(data->fan_div[index]))); \
179}
180
181show(TEMP, temp_input1, temp_in);
182show(TEMP, temp_max1, temp_max);
183show(TEMP, temp_hyst1, temp_hyst);
184show(BOOL, fan_auto1, fan_auto1);
185show_fan(fan_input1, fan_in, 0);
186show_fan(fan_input2, fan_in, 1);
187show_fan(fan_min1, fan_min, 0);
188show_fan(fan_min2, fan_min, 1);
189show(DIV, fan_div1, fan_div[0]);
190show(DIV, fan_div2, fan_div[1]);
191show(VDD, in_input0, voltage_in[0]);
192show(IN, in_input1, voltage_in[1]);
193show(IN, in_input2, voltage_in[2]);
194show(IN, in_input3, voltage_in[3]);
195show(VDD, in_min0, voltage_min[0]);
196show(IN, in_min1, voltage_min[1]);
197show(IN, in_min2, voltage_min[2]);
198show(IN, in_min3, voltage_min[3]);
199show(VDD, in_max0, voltage_max[0]);
200show(IN, in_max1, voltage_max[1]);
201show(IN, in_max2, voltage_max[2]);
202show(IN, in_max3, voltage_max[3]);
203show(RAW, alarms, alarms);
204show(BOOL, beep_enable, beep_enable);
205show(BEEP_MASK, beep_mask, beep_mask);
206
207#define set(type, suffix, value, reg) \
208static ssize_t set_##suffix(struct device *dev, const char *buf, \
209 size_t count) \
210{ \
211 struct i2c_client *client = to_i2c_client(dev); \
212 struct gl518_data *data = i2c_get_clientdata(client); \
213 long val = simple_strtol(buf, NULL, 10); \
214 \
215 down(&data->update_lock); \
216 data->value = type##_TO_REG(val); \
217 gl518_write_value(client, reg, data->value); \
218 up(&data->update_lock); \
219 return count; \
220}
221
222#define set_bits(type, suffix, value, reg, mask, shift) \
223static ssize_t set_##suffix(struct device *dev, const char *buf, \
224 size_t count) \
225{ \
226 struct i2c_client *client = to_i2c_client(dev); \
227 struct gl518_data *data = i2c_get_clientdata(client); \
228 int regvalue; \
229 unsigned long val = simple_strtoul(buf, NULL, 10); \
230 \
231 down(&data->update_lock); \
232 regvalue = gl518_read_value(client, reg); \
233 data->value = type##_TO_REG(val); \
234 regvalue = (regvalue & ~mask) | (data->value << shift); \
235 gl518_write_value(client, reg, regvalue); \
236 up(&data->update_lock); \
237 return count; \
238}
239
240#define set_low(type, suffix, value, reg) \
241 set_bits(type, suffix, value, reg, 0x00ff, 0)
242#define set_high(type, suffix, value, reg) \
243 set_bits(type, suffix, value, reg, 0xff00, 8)
244
245set(TEMP, temp_max1, temp_max, GL518_REG_TEMP_MAX);
246set(TEMP, temp_hyst1, temp_hyst, GL518_REG_TEMP_HYST);
247set_bits(BOOL, fan_auto1, fan_auto1, GL518_REG_MISC, 0x08, 3);
248set_bits(DIV, fan_div1, fan_div[0], GL518_REG_MISC, 0xc0, 6);
249set_bits(DIV, fan_div2, fan_div[1], GL518_REG_MISC, 0x30, 4);
250set_low(VDD, in_min0, voltage_min[0], GL518_REG_VDD_LIMIT);
251set_low(IN, in_min1, voltage_min[1], GL518_REG_VIN1_LIMIT);
252set_low(IN, in_min2, voltage_min[2], GL518_REG_VIN2_LIMIT);
253set_low(IN, in_min3, voltage_min[3], GL518_REG_VIN3_LIMIT);
254set_high(VDD, in_max0, voltage_max[0], GL518_REG_VDD_LIMIT);
255set_high(IN, in_max1, voltage_max[1], GL518_REG_VIN1_LIMIT);
256set_high(IN, in_max2, voltage_max[2], GL518_REG_VIN2_LIMIT);
257set_high(IN, in_max3, voltage_max[3], GL518_REG_VIN3_LIMIT);
258set_bits(BOOL, beep_enable, beep_enable, GL518_REG_CONF, 0x04, 2);
259set(BEEP_MASK, beep_mask, beep_mask, GL518_REG_ALARM);
260
261static ssize_t set_fan_min1(struct device *dev, const char *buf, size_t count)
262{
263 struct i2c_client *client = to_i2c_client(dev);
264 struct gl518_data *data = i2c_get_clientdata(client);
265 int regvalue;
266 unsigned long val = simple_strtoul(buf, NULL, 10);
267
268 down(&data->update_lock);
269 regvalue = gl518_read_value(client, GL518_REG_FAN_LIMIT);
270 data->fan_min[0] = FAN_TO_REG(val,
271 DIV_FROM_REG(data->fan_div[0]));
272 regvalue = (regvalue & 0x00ff) | (data->fan_min[0] << 8);
273 gl518_write_value(client, GL518_REG_FAN_LIMIT, regvalue);
274
275 data->beep_mask = gl518_read_value(client, GL518_REG_ALARM);
276 if (data->fan_min[0] == 0)
277 data->alarm_mask &= ~0x20;
278 else
279 data->alarm_mask |= 0x20;
280 data->beep_mask &= data->alarm_mask;
281 gl518_write_value(client, GL518_REG_ALARM, data->beep_mask);
282
283 up(&data->update_lock);
284 return count;
285}
286
287static ssize_t set_fan_min2(struct device *dev, const char *buf, size_t count)
288{
289 struct i2c_client *client = to_i2c_client(dev);
290 struct gl518_data *data = i2c_get_clientdata(client);
291 int regvalue;
292 unsigned long val = simple_strtoul(buf, NULL, 10);
293
294 down(&data->update_lock);
295 regvalue = gl518_read_value(client, GL518_REG_FAN_LIMIT);
296 data->fan_min[1] = FAN_TO_REG(val,
297 DIV_FROM_REG(data->fan_div[1]));
298 regvalue = (regvalue & 0xff00) | data->fan_min[1];
299 gl518_write_value(client, GL518_REG_FAN_LIMIT, regvalue);
300
301 data->beep_mask = gl518_read_value(client, GL518_REG_ALARM);
302 if (data->fan_min[1] == 0)
303 data->alarm_mask &= ~0x40;
304 else
305 data->alarm_mask |= 0x40;
306 data->beep_mask &= data->alarm_mask;
307 gl518_write_value(client, GL518_REG_ALARM, data->beep_mask);
308
309 up(&data->update_lock);
310 return count;
311}
312
313static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input1, NULL);
314static DEVICE_ATTR(temp1_max, S_IWUSR|S_IRUGO, show_temp_max1, set_temp_max1);
315static DEVICE_ATTR(temp1_max_hyst, S_IWUSR|S_IRUGO,
316 show_temp_hyst1, set_temp_hyst1);
317static DEVICE_ATTR(fan1_auto, S_IWUSR|S_IRUGO, show_fan_auto1, set_fan_auto1);
318static DEVICE_ATTR(fan1_input, S_IRUGO, show_fan_input1, NULL);
319static DEVICE_ATTR(fan2_input, S_IRUGO, show_fan_input2, NULL);
320static DEVICE_ATTR(fan1_min, S_IWUSR|S_IRUGO, show_fan_min1, set_fan_min1);
321static DEVICE_ATTR(fan2_min, S_IWUSR|S_IRUGO, show_fan_min2, set_fan_min2);
322static DEVICE_ATTR(fan1_div, S_IWUSR|S_IRUGO, show_fan_div1, set_fan_div1);
323static DEVICE_ATTR(fan2_div, S_IWUSR|S_IRUGO, show_fan_div2, set_fan_div2);
324static DEVICE_ATTR(in0_input, S_IRUGO, show_in_input0, NULL);
325static DEVICE_ATTR(in1_input, S_IRUGO, show_in_input1, NULL);
326static DEVICE_ATTR(in2_input, S_IRUGO, show_in_input2, NULL);
327static DEVICE_ATTR(in3_input, S_IRUGO, show_in_input3, NULL);
328static DEVICE_ATTR(in0_min, S_IWUSR|S_IRUGO, show_in_min0, set_in_min0);
329static DEVICE_ATTR(in1_min, S_IWUSR|S_IRUGO, show_in_min1, set_in_min1);
330static DEVICE_ATTR(in2_min, S_IWUSR|S_IRUGO, show_in_min2, set_in_min2);
331static DEVICE_ATTR(in3_min, S_IWUSR|S_IRUGO, show_in_min3, set_in_min3);
332static DEVICE_ATTR(in0_max, S_IWUSR|S_IRUGO, show_in_max0, set_in_max0);
333static DEVICE_ATTR(in1_max, S_IWUSR|S_IRUGO, show_in_max1, set_in_max1);
334static DEVICE_ATTR(in2_max, S_IWUSR|S_IRUGO, show_in_max2, set_in_max2);
335static DEVICE_ATTR(in3_max, S_IWUSR|S_IRUGO, show_in_max3, set_in_max3);
336static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
337static DEVICE_ATTR(beep_enable, S_IWUSR|S_IRUGO,
338 show_beep_enable, set_beep_enable);
339static DEVICE_ATTR(beep_mask, S_IWUSR|S_IRUGO,
340 show_beep_mask, set_beep_mask);
341
342/*
343 * Real code
344 */
345
346static int gl518_attach_adapter(struct i2c_adapter *adapter)
347{
348 if (!(adapter->class & I2C_CLASS_HWMON))
349 return 0;
350 return i2c_detect(adapter, &addr_data, gl518_detect);
351}
352
353static int gl518_detect(struct i2c_adapter *adapter, int address, int kind)
354{
355 int i;
356 struct i2c_client *new_client;
357 struct gl518_data *data;
358 int err = 0;
359
360 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
361 I2C_FUNC_SMBUS_WORD_DATA))
362 goto exit;
363
364 /* OK. For now, we presume we have a valid client. We now create the
365 client structure, even though we cannot fill it completely yet.
366 But it allows us to access gl518_{read,write}_value. */
367
368 if (!(data = kmalloc(sizeof(struct gl518_data), GFP_KERNEL))) {
369 err = -ENOMEM;
370 goto exit;
371 }
372 memset(data, 0, sizeof(struct gl518_data));
373
374 new_client = &data->client;
375 i2c_set_clientdata(new_client, data);
376
377 new_client->addr = address;
378 new_client->adapter = adapter;
379 new_client->driver = &gl518_driver;
380 new_client->flags = 0;
381
382 /* Now, we do the remaining detection. */
383
384 if (kind < 0) {
385 if ((gl518_read_value(new_client, GL518_REG_CHIP_ID) != 0x80)
386 || (gl518_read_value(new_client, GL518_REG_CONF) & 0x80))
387 goto exit_free;
388 }
389
390 /* Determine the chip type. */
391 if (kind <= 0) {
392 i = gl518_read_value(new_client, GL518_REG_REVISION);
393 if (i == 0x00) {
394 kind = gl518sm_r00;
395 } else if (i == 0x80) {
396 kind = gl518sm_r80;
397 } else {
398 if (kind <= 0)
399 dev_info(&adapter->dev,
400 "Ignoring 'force' parameter for unknown "
401 "chip at adapter %d, address 0x%02x\n",
402 i2c_adapter_id(adapter), address);
403 goto exit_free;
404 }
405 }
406
407 /* Fill in the remaining client fields */
408 strlcpy(new_client->name, "gl518sm", I2C_NAME_SIZE);
409 data->type = kind;
410 data->valid = 0;
411 init_MUTEX(&data->update_lock);
412
413 /* Tell the I2C layer a new client has arrived */
414 if ((err = i2c_attach_client(new_client)))
415 goto exit_free;
416
417 /* Initialize the GL518SM chip */
418 data->alarm_mask = 0xff;
419 data->voltage_in[0]=data->voltage_in[1]=data->voltage_in[2]=0;
420 gl518_init_client((struct i2c_client *) new_client);
421
422 /* Register sysfs hooks */
423 device_create_file(&new_client->dev, &dev_attr_in0_input);
424 device_create_file(&new_client->dev, &dev_attr_in1_input);
425 device_create_file(&new_client->dev, &dev_attr_in2_input);
426 device_create_file(&new_client->dev, &dev_attr_in3_input);
427 device_create_file(&new_client->dev, &dev_attr_in0_min);
428 device_create_file(&new_client->dev, &dev_attr_in1_min);
429 device_create_file(&new_client->dev, &dev_attr_in2_min);
430 device_create_file(&new_client->dev, &dev_attr_in3_min);
431 device_create_file(&new_client->dev, &dev_attr_in0_max);
432 device_create_file(&new_client->dev, &dev_attr_in1_max);
433 device_create_file(&new_client->dev, &dev_attr_in2_max);
434 device_create_file(&new_client->dev, &dev_attr_in3_max);
435 device_create_file(&new_client->dev, &dev_attr_fan1_auto);
436 device_create_file(&new_client->dev, &dev_attr_fan1_input);
437 device_create_file(&new_client->dev, &dev_attr_fan2_input);
438 device_create_file(&new_client->dev, &dev_attr_fan1_min);
439 device_create_file(&new_client->dev, &dev_attr_fan2_min);
440 device_create_file(&new_client->dev, &dev_attr_fan1_div);
441 device_create_file(&new_client->dev, &dev_attr_fan2_div);
442 device_create_file(&new_client->dev, &dev_attr_temp1_input);
443 device_create_file(&new_client->dev, &dev_attr_temp1_max);
444 device_create_file(&new_client->dev, &dev_attr_temp1_max_hyst);
445 device_create_file(&new_client->dev, &dev_attr_alarms);
446 device_create_file(&new_client->dev, &dev_attr_beep_enable);
447 device_create_file(&new_client->dev, &dev_attr_beep_mask);
448
449 return 0;
450
451/* OK, this is not exactly good programming practice, usually. But it is
452 very code-efficient in this case. */
453
454exit_free:
455 kfree(data);
456exit:
457 return err;
458}
459
460
461/* Called when we have found a new GL518SM.
462 Note that we preserve D4:NoFan2 and D2:beep_enable. */
463static void gl518_init_client(struct i2c_client *client)
464{
465 /* Make sure we leave D7:Reset untouched */
466 u8 regvalue = gl518_read_value(client, GL518_REG_CONF) & 0x7f;
467
468 /* Comparator mode (D3=0), standby mode (D6=0) */
469 gl518_write_value(client, GL518_REG_CONF, (regvalue &= 0x37));
470
471 /* Never interrupts */
472 gl518_write_value(client, GL518_REG_MASK, 0x00);
473
474 /* Clear status register (D5=1), start (D6=1) */
475 gl518_write_value(client, GL518_REG_CONF, 0x20 | regvalue);
476 gl518_write_value(client, GL518_REG_CONF, 0x40 | regvalue);
477}
478
479static int gl518_detach_client(struct i2c_client *client)
480{
481 int err;
482
483 if ((err = i2c_detach_client(client))) {
484 dev_err(&client->dev, "Client deregistration failed, "
485 "client not detached.\n");
486 return err;
487 }
488
489 kfree(i2c_get_clientdata(client));
490
491 return 0;
492}
493
494/* Registers 0x07 to 0x0c are word-sized, others are byte-sized
495 GL518 uses a high-byte first convention, which is exactly opposite to
496 the usual practice. */
497static int gl518_read_value(struct i2c_client *client, u8 reg)
498{
499 if ((reg >= 0x07) && (reg <= 0x0c))
500 return swab16(i2c_smbus_read_word_data(client, reg));
501 else
502 return i2c_smbus_read_byte_data(client, reg);
503}
504
505/* Registers 0x07 to 0x0c are word-sized, others are byte-sized
506 GL518 uses a high-byte first convention, which is exactly opposite to
507 the usual practice. */
508static int gl518_write_value(struct i2c_client *client, u8 reg, u16 value)
509{
510 if ((reg >= 0x07) && (reg <= 0x0c))
511 return i2c_smbus_write_word_data(client, reg, swab16(value));
512 else
513 return i2c_smbus_write_byte_data(client, reg, value);
514}
515
516static struct gl518_data *gl518_update_device(struct device *dev)
517{
518 struct i2c_client *client = to_i2c_client(dev);
519 struct gl518_data *data = i2c_get_clientdata(client);
520 int val;
521
522 down(&data->update_lock);
523
524 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
525 || !data->valid) {
526 dev_dbg(&client->dev, "Starting gl518 update\n");
527
528 data->alarms = gl518_read_value(client, GL518_REG_INT);
529 data->beep_mask = gl518_read_value(client, GL518_REG_ALARM);
530
531 val = gl518_read_value(client, GL518_REG_VDD_LIMIT);
532 data->voltage_min[0] = val & 0xff;
533 data->voltage_max[0] = (val >> 8) & 0xff;
534 val = gl518_read_value(client, GL518_REG_VIN1_LIMIT);
535 data->voltage_min[1] = val & 0xff;
536 data->voltage_max[1] = (val >> 8) & 0xff;
537 val = gl518_read_value(client, GL518_REG_VIN2_LIMIT);
538 data->voltage_min[2] = val & 0xff;
539 data->voltage_max[2] = (val >> 8) & 0xff;
540 val = gl518_read_value(client, GL518_REG_VIN3_LIMIT);
541 data->voltage_min[3] = val & 0xff;
542 data->voltage_max[3] = (val >> 8) & 0xff;
543
544 val = gl518_read_value(client, GL518_REG_FAN_COUNT);
545 data->fan_in[0] = (val >> 8) & 0xff;
546 data->fan_in[1] = val & 0xff;
547
548 val = gl518_read_value(client, GL518_REG_FAN_LIMIT);
549 data->fan_min[0] = (val >> 8) & 0xff;
550 data->fan_min[1] = val & 0xff;
551
552 data->temp_in = gl518_read_value(client, GL518_REG_TEMP_IN);
553 data->temp_max =
554 gl518_read_value(client, GL518_REG_TEMP_MAX);
555 data->temp_hyst =
556 gl518_read_value(client, GL518_REG_TEMP_HYST);
557
558 val = gl518_read_value(client, GL518_REG_MISC);
559 data->fan_div[0] = (val >> 6) & 0x03;
560 data->fan_div[1] = (val >> 4) & 0x03;
561 data->fan_auto1 = (val >> 3) & 0x01;
562
563 data->alarms &= data->alarm_mask;
564
565 val = gl518_read_value(client, GL518_REG_CONF);
566 data->beep_enable = (val >> 2) & 1;
567
568 if (data->type != gl518sm_r00) {
569 data->voltage_in[0] =
570 gl518_read_value(client, GL518_REG_VDD);
571 data->voltage_in[1] =
572 gl518_read_value(client, GL518_REG_VIN1);
573 data->voltage_in[2] =
574 gl518_read_value(client, GL518_REG_VIN2);
575 }
576 data->voltage_in[3] =
577 gl518_read_value(client, GL518_REG_VIN3);
578
579 data->last_updated = jiffies;
580 data->valid = 1;
581 }
582
583 up(&data->update_lock);
584
585 return data;
586}
587
588static int __init sensors_gl518sm_init(void)
589{
590 return i2c_add_driver(&gl518_driver);
591}
592
593static void __exit sensors_gl518sm_exit(void)
594{
595 i2c_del_driver(&gl518_driver);
596}
597
598MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, "
599 "Kyosti Malkki <kmalkki@cc.hut.fi> and "
600 "Hong-Gunn Chew <hglinux@gunnet.org>");
601MODULE_DESCRIPTION("GL518SM driver");
602MODULE_LICENSE("GPL");
603
604module_init(sensors_gl518sm_init);
605module_exit(sensors_gl518sm_exit);
diff --git a/drivers/i2c/chips/gl520sm.c b/drivers/i2c/chips/gl520sm.c
new file mode 100644
index 000000000000..3fd17e46ffc6
--- /dev/null
+++ b/drivers/i2c/chips/gl520sm.c
@@ -0,0 +1,769 @@
1/*
2 gl520sm.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring
4 Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl>,
5 Kyösti Mälkki <kmalkki@cc.hut.fi>
6 Copyright (c) 2005 Maarten Deprez <maartendeprez@users.sourceforge.net>
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; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21
22*/
23
24#include <linux/module.h>
25#include <linux/init.h>
26#include <linux/slab.h>
27#include <linux/i2c.h>
28#include <linux/i2c-sensor.h>
29#include <linux/i2c-vid.h>
30
31/* Type of the extra sensor */
32static unsigned short extra_sensor_type;
33module_param(extra_sensor_type, ushort, 0);
34MODULE_PARM_DESC(extra_sensor_type, "Type of extra sensor (0=autodetect, 1=temperature, 2=voltage)");
35
36/* Addresses to scan */
37static unsigned short normal_i2c[] = { 0x2c, 0x2d, I2C_CLIENT_END };
38static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
39
40/* Insmod parameters */
41SENSORS_INSMOD_1(gl520sm);
42
43/* Many GL520 constants specified below
44One of the inputs can be configured as either temp or voltage.
45That's why _TEMP2 and _IN4 access the same register
46*/
47
48/* The GL520 registers */
49#define GL520_REG_CHIP_ID 0x00
50#define GL520_REG_REVISION 0x01
51#define GL520_REG_CONF 0x03
52#define GL520_REG_MASK 0x11
53
54#define GL520_REG_VID_INPUT 0x02
55
56#define GL520_REG_IN0_INPUT 0x15
57#define GL520_REG_IN0_LIMIT 0x0c
58#define GL520_REG_IN0_MIN GL520_REG_IN0_LIMIT
59#define GL520_REG_IN0_MAX GL520_REG_IN0_LIMIT
60
61#define GL520_REG_IN1_INPUT 0x14
62#define GL520_REG_IN1_LIMIT 0x09
63#define GL520_REG_IN1_MIN GL520_REG_IN1_LIMIT
64#define GL520_REG_IN1_MAX GL520_REG_IN1_LIMIT
65
66#define GL520_REG_IN2_INPUT 0x13
67#define GL520_REG_IN2_LIMIT 0x0a
68#define GL520_REG_IN2_MIN GL520_REG_IN2_LIMIT
69#define GL520_REG_IN2_MAX GL520_REG_IN2_LIMIT
70
71#define GL520_REG_IN3_INPUT 0x0d
72#define GL520_REG_IN3_LIMIT 0x0b
73#define GL520_REG_IN3_MIN GL520_REG_IN3_LIMIT
74#define GL520_REG_IN3_MAX GL520_REG_IN3_LIMIT
75
76#define GL520_REG_IN4_INPUT 0x0e
77#define GL520_REG_IN4_MAX 0x17
78#define GL520_REG_IN4_MIN 0x18
79
80#define GL520_REG_TEMP1_INPUT 0x04
81#define GL520_REG_TEMP1_MAX 0x05
82#define GL520_REG_TEMP1_MAX_HYST 0x06
83
84#define GL520_REG_TEMP2_INPUT 0x0e
85#define GL520_REG_TEMP2_MAX 0x17
86#define GL520_REG_TEMP2_MAX_HYST 0x18
87
88#define GL520_REG_FAN_INPUT 0x07
89#define GL520_REG_FAN_MIN 0x08
90#define GL520_REG_FAN_DIV 0x0f
91#define GL520_REG_FAN_OFF GL520_REG_FAN_DIV
92
93#define GL520_REG_ALARMS 0x12
94#define GL520_REG_BEEP_MASK 0x10
95#define GL520_REG_BEEP_ENABLE GL520_REG_CONF
96
97/*
98 * Function declarations
99 */
100
101static int gl520_attach_adapter(struct i2c_adapter *adapter);
102static int gl520_detect(struct i2c_adapter *adapter, int address, int kind);
103static void gl520_init_client(struct i2c_client *client);
104static int gl520_detach_client(struct i2c_client *client);
105static int gl520_read_value(struct i2c_client *client, u8 reg);
106static int gl520_write_value(struct i2c_client *client, u8 reg, u16 value);
107static struct gl520_data *gl520_update_device(struct device *dev);
108
109/* Driver data */
110static struct i2c_driver gl520_driver = {
111 .owner = THIS_MODULE,
112 .name = "gl520sm",
113 .id = I2C_DRIVERID_GL520,
114 .flags = I2C_DF_NOTIFY,
115 .attach_adapter = gl520_attach_adapter,
116 .detach_client = gl520_detach_client,
117};
118
119/* Client data */
120struct gl520_data {
121 struct i2c_client client;
122 struct semaphore update_lock;
123 char valid; /* zero until the following fields are valid */
124 unsigned long last_updated; /* in jiffies */
125
126 u8 vid;
127 u8 vrm;
128 u8 in_input[5]; /* [0] = VVD */
129 u8 in_min[5]; /* [0] = VDD */
130 u8 in_max[5]; /* [0] = VDD */
131 u8 fan_input[2];
132 u8 fan_min[2];
133 u8 fan_div[2];
134 u8 fan_off;
135 u8 temp_input[2];
136 u8 temp_max[2];
137 u8 temp_max_hyst[2];
138 u8 alarms;
139 u8 beep_enable;
140 u8 beep_mask;
141 u8 alarm_mask;
142 u8 two_temps;
143};
144
145/*
146 * Sysfs stuff
147 */
148
149#define sysfs_r(type, n, item, reg) \
150static ssize_t get_##type##item (struct gl520_data *, char *, int); \
151static ssize_t get_##type##n##item (struct device *, char *); \
152static ssize_t get_##type##n##item (struct device *dev, char *buf) \
153{ \
154 struct gl520_data *data = gl520_update_device(dev); \
155 return get_##type##item(data, buf, (n)); \
156}
157
158#define sysfs_w(type, n, item, reg) \
159static ssize_t set_##type##item (struct i2c_client *, struct gl520_data *, const char *, size_t, int, int); \
160static ssize_t set_##type##n##item (struct device *, const char *, size_t); \
161static ssize_t set_##type##n##item (struct device *dev, const char *buf, size_t count) \
162{ \
163 struct i2c_client *client = to_i2c_client(dev); \
164 struct gl520_data *data = i2c_get_clientdata(client); \
165 return set_##type##item(client, data, buf, count, (n), reg); \
166}
167
168#define sysfs_rw_n(type, n, item, reg) \
169sysfs_r(type, n, item, reg) \
170sysfs_w(type, n, item, reg) \
171static DEVICE_ATTR(type##n##item, S_IRUGO | S_IWUSR, get_##type##n##item, set_##type##n##item);
172
173#define sysfs_ro_n(type, n, item, reg) \
174sysfs_r(type, n, item, reg) \
175static DEVICE_ATTR(type##n##item, S_IRUGO, get_##type##n##item, NULL);
176
177#define sysfs_rw(type, item, reg) \
178sysfs_r(type, 0, item, reg) \
179sysfs_w(type, 0, item, reg) \
180static DEVICE_ATTR(type##item, S_IRUGO | S_IWUSR, get_##type##0##item, set_##type##0##item);
181
182#define sysfs_ro(type, item, reg) \
183sysfs_r(type, 0, item, reg) \
184static DEVICE_ATTR(type##item, S_IRUGO, get_##type##0##item, NULL);
185
186
187#define sysfs_vid(n) \
188sysfs_ro_n(cpu, n, _vid, GL520_REG_VID_INPUT)
189
190#define device_create_file_vid(client, n) \
191device_create_file(&client->dev, &dev_attr_cpu##n##_vid)
192
193#define sysfs_in(n) \
194sysfs_ro_n(in, n, _input, GL520_REG_IN##n##INPUT) \
195sysfs_rw_n(in, n, _min, GL520_REG_IN##n##_MIN) \
196sysfs_rw_n(in, n, _max, GL520_REG_IN##n##_MAX) \
197
198#define device_create_file_in(client, n) \
199({device_create_file(&client->dev, &dev_attr_in##n##_input); \
200device_create_file(&client->dev, &dev_attr_in##n##_min); \
201device_create_file(&client->dev, &dev_attr_in##n##_max);})
202
203#define sysfs_fan(n) \
204sysfs_ro_n(fan, n, _input, GL520_REG_FAN_INPUT) \
205sysfs_rw_n(fan, n, _min, GL520_REG_FAN_MIN) \
206sysfs_rw_n(fan, n, _div, GL520_REG_FAN_DIV)
207
208#define device_create_file_fan(client, n) \
209({device_create_file(&client->dev, &dev_attr_fan##n##_input); \
210device_create_file(&client->dev, &dev_attr_fan##n##_min); \
211device_create_file(&client->dev, &dev_attr_fan##n##_div);})
212
213#define sysfs_fan_off(n) \
214sysfs_rw_n(fan, n, _off, GL520_REG_FAN_OFF) \
215
216#define device_create_file_fan_off(client, n) \
217device_create_file(&client->dev, &dev_attr_fan##n##_off)
218
219#define sysfs_temp(n) \
220sysfs_ro_n(temp, n, _input, GL520_REG_TEMP##n##_INPUT) \
221sysfs_rw_n(temp, n, _max, GL520_REG_TEMP##n##_MAX) \
222sysfs_rw_n(temp, n, _max_hyst, GL520_REG_TEMP##n##_MAX_HYST)
223
224#define device_create_file_temp(client, n) \
225({device_create_file(&client->dev, &dev_attr_temp##n##_input); \
226device_create_file(&client->dev, &dev_attr_temp##n##_max); \
227device_create_file(&client->dev, &dev_attr_temp##n##_max_hyst);})
228
229#define sysfs_alarms() \
230sysfs_ro(alarms, , GL520_REG_ALARMS) \
231sysfs_rw(beep_enable, , GL520_REG_BEEP_ENABLE) \
232sysfs_rw(beep_mask, , GL520_REG_BEEP_MASK)
233
234#define device_create_file_alarms(client) \
235({device_create_file(&client->dev, &dev_attr_alarms); \
236device_create_file(&client->dev, &dev_attr_beep_enable); \
237device_create_file(&client->dev, &dev_attr_beep_mask);})
238
239
240sysfs_vid(0)
241
242sysfs_in(0)
243sysfs_in(1)
244sysfs_in(2)
245sysfs_in(3)
246sysfs_in(4)
247
248sysfs_fan(1)
249sysfs_fan(2)
250sysfs_fan_off(1)
251
252sysfs_temp(1)
253sysfs_temp(2)
254
255sysfs_alarms()
256
257
258static ssize_t get_cpu_vid(struct gl520_data *data, char *buf, int n)
259{
260 return sprintf(buf, "%u\n", vid_from_reg(data->vid, data->vrm));
261}
262
263#define VDD_FROM_REG(val) (((val)*95+2)/4)
264#define VDD_TO_REG(val) (SENSORS_LIMIT((((val)*4+47)/95),0,255))
265
266#define IN_FROM_REG(val) ((val)*19)
267#define IN_TO_REG(val) (SENSORS_LIMIT((((val)+9)/19),0,255))
268
269static ssize_t get_in_input(struct gl520_data *data, char *buf, int n)
270{
271 u8 r = data->in_input[n];
272
273 if (n == 0)
274 return sprintf(buf, "%d\n", VDD_FROM_REG(r));
275 else
276 return sprintf(buf, "%d\n", IN_FROM_REG(r));
277}
278
279static ssize_t get_in_min(struct gl520_data *data, char *buf, int n)
280{
281 u8 r = data->in_min[n];
282
283 if (n == 0)
284 return sprintf(buf, "%d\n", VDD_FROM_REG(r));
285 else
286 return sprintf(buf, "%d\n", IN_FROM_REG(r));
287}
288
289static ssize_t get_in_max(struct gl520_data *data, char *buf, int n)
290{
291 u8 r = data->in_max[n];
292
293 if (n == 0)
294 return sprintf(buf, "%d\n", VDD_FROM_REG(r));
295 else
296 return sprintf(buf, "%d\n", IN_FROM_REG(r));
297}
298
299static ssize_t set_in_min(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
300{
301 long v = simple_strtol(buf, NULL, 10);
302 u8 r;
303
304 down(&data->update_lock);
305
306 if (n == 0)
307 r = VDD_TO_REG(v);
308 else
309 r = IN_TO_REG(v);
310
311 data->in_min[n] = r;
312
313 if (n < 4)
314 gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0xff) | r);
315 else
316 gl520_write_value(client, reg, r);
317
318 up(&data->update_lock);
319 return count;
320}
321
322static ssize_t set_in_max(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
323{
324 long v = simple_strtol(buf, NULL, 10);
325 u8 r;
326
327 if (n == 0)
328 r = VDD_TO_REG(v);
329 else
330 r = IN_TO_REG(v);
331
332 down(&data->update_lock);
333
334 data->in_max[n] = r;
335
336 if (n < 4)
337 gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0xff00) | (r << 8));
338 else
339 gl520_write_value(client, reg, r);
340
341 up(&data->update_lock);
342 return count;
343}
344
345#define DIV_FROM_REG(val) (1 << (val))
346#define FAN_FROM_REG(val,div) ((val)==0 ? 0 : (480000/((val) << (div))))
347#define FAN_TO_REG(val,div) ((val)<=0?0:SENSORS_LIMIT((480000 + ((val) << ((div)-1))) / ((val) << (div)), 1, 255));
348
349static ssize_t get_fan_input(struct gl520_data *data, char *buf, int n)
350{
351 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_input[n - 1], data->fan_div[n - 1]));
352}
353
354static ssize_t get_fan_min(struct gl520_data *data, char *buf, int n)
355{
356 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[n - 1], data->fan_div[n - 1]));
357}
358
359static ssize_t get_fan_div(struct gl520_data *data, char *buf, int n)
360{
361 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[n - 1]));
362}
363
364static ssize_t get_fan_off(struct gl520_data *data, char *buf, int n)
365{
366 return sprintf(buf, "%d\n", data->fan_off);
367}
368
369static ssize_t set_fan_min(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
370{
371 unsigned long v = simple_strtoul(buf, NULL, 10);
372 u8 r;
373
374 down(&data->update_lock);
375 r = FAN_TO_REG(v, data->fan_div[n - 1]);
376 data->fan_min[n - 1] = r;
377
378 if (n == 1)
379 gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0xff00) | (r << 8));
380 else
381 gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0xff) | r);
382
383 data->beep_mask = gl520_read_value(client, GL520_REG_BEEP_MASK);
384 if (data->fan_min[n - 1] == 0)
385 data->alarm_mask &= (n == 1) ? ~0x20 : ~0x40;
386 else
387 data->alarm_mask |= (n == 1) ? 0x20 : 0x40;
388 data->beep_mask &= data->alarm_mask;
389 gl520_write_value(client, GL520_REG_BEEP_MASK, data->beep_mask);
390
391 up(&data->update_lock);
392 return count;
393}
394
395static ssize_t set_fan_div(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
396{
397 unsigned long v = simple_strtoul(buf, NULL, 10);
398 u8 r;
399
400 switch (v) {
401 case 1: r = 0; break;
402 case 2: r = 1; break;
403 case 4: r = 2; break;
404 case 8: r = 3; break;
405 default:
406 dev_err(&client->dev, "fan_div value %ld not supported. Choose one of 1, 2, 4 or 8!\n", v);
407 return -EINVAL;
408 }
409
410 down(&data->update_lock);
411 data->fan_div[n - 1] = r;
412
413 if (n == 1)
414 gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0xc0) | (r << 6));
415 else
416 gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0x30) | (r << 4));
417
418 up(&data->update_lock);
419 return count;
420}
421
422static ssize_t set_fan_off(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
423{
424 u8 r = simple_strtoul(buf, NULL, 10)?1:0;
425
426 down(&data->update_lock);
427 data->fan_off = r;
428 gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0x0c) | (r << 2));
429 up(&data->update_lock);
430 return count;
431}
432
433#define TEMP_FROM_REG(val) (((val) - 130) * 1000)
434#define TEMP_TO_REG(val) (SENSORS_LIMIT(((((val)<0?(val)-500:(val)+500) / 1000)+130),0,255))
435
436static ssize_t get_temp_input(struct gl520_data *data, char *buf, int n)
437{
438 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_input[n - 1]));
439}
440
441static ssize_t get_temp_max(struct gl520_data *data, char *buf, int n)
442{
443 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[n - 1]));
444}
445
446static ssize_t get_temp_max_hyst(struct gl520_data *data, char *buf, int n)
447{
448 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max_hyst[n - 1]));
449}
450
451static ssize_t set_temp_max(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
452{
453 long v = simple_strtol(buf, NULL, 10);
454
455 down(&data->update_lock);
456 data->temp_max[n - 1] = TEMP_TO_REG(v);;
457 gl520_write_value(client, reg, data->temp_max[n - 1]);
458 up(&data->update_lock);
459 return count;
460}
461
462static ssize_t set_temp_max_hyst(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
463{
464 long v = simple_strtol(buf, NULL, 10);
465
466 down(&data->update_lock);
467 data->temp_max_hyst[n - 1] = TEMP_TO_REG(v);
468 gl520_write_value(client, reg, data->temp_max_hyst[n - 1]);
469 up(&data->update_lock);
470 return count;
471}
472
473static ssize_t get_alarms(struct gl520_data *data, char *buf, int n)
474{
475 return sprintf(buf, "%d\n", data->alarms);
476}
477
478static ssize_t get_beep_enable(struct gl520_data *data, char *buf, int n)
479{
480 return sprintf(buf, "%d\n", data->beep_enable);
481}
482
483static ssize_t get_beep_mask(struct gl520_data *data, char *buf, int n)
484{
485 return sprintf(buf, "%d\n", data->beep_mask);
486}
487
488static ssize_t set_beep_enable(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
489{
490 u8 r = simple_strtoul(buf, NULL, 10)?0:1;
491
492 down(&data->update_lock);
493 data->beep_enable = !r;
494 gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0x04) | (r << 2));
495 up(&data->update_lock);
496 return count;
497}
498
499static ssize_t set_beep_mask(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
500{
501 u8 r = simple_strtoul(buf, NULL, 10);
502
503 down(&data->update_lock);
504 r &= data->alarm_mask;
505 data->beep_mask = r;
506 gl520_write_value(client, reg, r);
507 up(&data->update_lock);
508 return count;
509}
510
511
512/*
513 * Real code
514 */
515
516static int gl520_attach_adapter(struct i2c_adapter *adapter)
517{
518 if (!(adapter->class & I2C_CLASS_HWMON))
519 return 0;
520 return i2c_detect(adapter, &addr_data, gl520_detect);
521}
522
523static int gl520_detect(struct i2c_adapter *adapter, int address, int kind)
524{
525 struct i2c_client *new_client;
526 struct gl520_data *data;
527 int err = 0;
528
529 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
530 I2C_FUNC_SMBUS_WORD_DATA))
531 goto exit;
532
533 /* OK. For now, we presume we have a valid client. We now create the
534 client structure, even though we cannot fill it completely yet.
535 But it allows us to access gl520_{read,write}_value. */
536
537 if (!(data = kmalloc(sizeof(struct gl520_data), GFP_KERNEL))) {
538 err = -ENOMEM;
539 goto exit;
540 }
541 memset(data, 0, sizeof(struct gl520_data));
542
543 new_client = &data->client;
544 i2c_set_clientdata(new_client, data);
545 new_client->addr = address;
546 new_client->adapter = adapter;
547 new_client->driver = &gl520_driver;
548 new_client->flags = 0;
549
550 /* Determine the chip type. */
551 if (kind < 0) {
552 if ((gl520_read_value(new_client, GL520_REG_CHIP_ID) != 0x20) ||
553 ((gl520_read_value(new_client, GL520_REG_REVISION) & 0x7f) != 0x00) ||
554 ((gl520_read_value(new_client, GL520_REG_CONF) & 0x80) != 0x00)) {
555 dev_dbg(&new_client->dev, "Unknown chip type, skipping\n");
556 goto exit_free;
557 }
558 }
559
560 /* Fill in the remaining client fields */
561 strlcpy(new_client->name, "gl520sm", I2C_NAME_SIZE);
562 data->valid = 0;
563 init_MUTEX(&data->update_lock);
564
565 /* Tell the I2C layer a new client has arrived */
566 if ((err = i2c_attach_client(new_client)))
567 goto exit_free;
568
569 /* Initialize the GL520SM chip */
570 gl520_init_client(new_client);
571
572 /* Register sysfs hooks */
573 device_create_file_vid(new_client, 0);
574
575 device_create_file_in(new_client, 0);
576 device_create_file_in(new_client, 1);
577 device_create_file_in(new_client, 2);
578 device_create_file_in(new_client, 3);
579 if (!data->two_temps)
580 device_create_file_in(new_client, 4);
581
582 device_create_file_fan(new_client, 1);
583 device_create_file_fan(new_client, 2);
584 device_create_file_fan_off(new_client, 1);
585
586 device_create_file_temp(new_client, 1);
587 if (data->two_temps)
588 device_create_file_temp(new_client, 2);
589
590 device_create_file_alarms(new_client);
591
592 return 0;
593
594exit_free:
595 kfree(data);
596exit:
597 return err;
598}
599
600
601/* Called when we have found a new GL520SM. */
602static void gl520_init_client(struct i2c_client *client)
603{
604 struct gl520_data *data = i2c_get_clientdata(client);
605 u8 oldconf, conf;
606
607 conf = oldconf = gl520_read_value(client, GL520_REG_CONF);
608
609 data->alarm_mask = 0xff;
610 data->vrm = i2c_which_vrm();
611
612 if (extra_sensor_type == 1)
613 conf &= ~0x10;
614 else if (extra_sensor_type == 2)
615 conf |= 0x10;
616 data->two_temps = !(conf & 0x10);
617
618 /* If IRQ# is disabled, we can safely force comparator mode */
619 if (!(conf & 0x20))
620 conf &= 0xf7;
621
622 /* Enable monitoring if needed */
623 conf |= 0x40;
624
625 if (conf != oldconf)
626 gl520_write_value(client, GL520_REG_CONF, conf);
627
628 gl520_update_device(&(client->dev));
629
630 if (data->fan_min[0] == 0)
631 data->alarm_mask &= ~0x20;
632 if (data->fan_min[1] == 0)
633 data->alarm_mask &= ~0x40;
634
635 data->beep_mask &= data->alarm_mask;
636 gl520_write_value(client, GL520_REG_BEEP_MASK, data->beep_mask);
637}
638
639static int gl520_detach_client(struct i2c_client *client)
640{
641 int err;
642
643 if ((err = i2c_detach_client(client))) {
644 dev_err(&client->dev, "Client deregistration failed, "
645 "client not detached.\n");
646 return err;
647 }
648
649 kfree(i2c_get_clientdata(client));
650 return 0;
651}
652
653
654/* Registers 0x07 to 0x0c are word-sized, others are byte-sized
655 GL520 uses a high-byte first convention */
656static int gl520_read_value(struct i2c_client *client, u8 reg)
657{
658 if ((reg >= 0x07) && (reg <= 0x0c))
659 return swab16(i2c_smbus_read_word_data(client, reg));
660 else
661 return i2c_smbus_read_byte_data(client, reg);
662}
663
664static int gl520_write_value(struct i2c_client *client, u8 reg, u16 value)
665{
666 if ((reg >= 0x07) && (reg <= 0x0c))
667 return i2c_smbus_write_word_data(client, reg, swab16(value));
668 else
669 return i2c_smbus_write_byte_data(client, reg, value);
670}
671
672
673static struct gl520_data *gl520_update_device(struct device *dev)
674{
675 struct i2c_client *client = to_i2c_client(dev);
676 struct gl520_data *data = i2c_get_clientdata(client);
677 int val;
678
679 down(&data->update_lock);
680
681 if ((jiffies - data->last_updated > 2 * HZ) ||
682 (jiffies < data->last_updated) || !data->valid) {
683
684 dev_dbg(&client->dev, "Starting gl520sm update\n");
685
686 data->alarms = gl520_read_value(client, GL520_REG_ALARMS);
687 data->beep_mask = gl520_read_value(client, GL520_REG_BEEP_MASK);
688 data->vid = gl520_read_value(client, GL520_REG_VID_INPUT) & 0x1f;
689
690 val = gl520_read_value(client, GL520_REG_IN0_LIMIT);
691 data->in_min[0] = val & 0xff;
692 data->in_max[0] = (val >> 8) & 0xff;
693 val = gl520_read_value(client, GL520_REG_IN1_LIMIT);
694 data->in_min[1] = val & 0xff;
695 data->in_max[1] = (val >> 8) & 0xff;
696 val = gl520_read_value(client, GL520_REG_IN2_LIMIT);
697 data->in_min[2] = val & 0xff;
698 data->in_max[2] = (val >> 8) & 0xff;
699 val = gl520_read_value(client, GL520_REG_IN3_LIMIT);
700 data->in_min[3] = val & 0xff;
701 data->in_max[3] = (val >> 8) & 0xff;
702
703 val = gl520_read_value(client, GL520_REG_FAN_INPUT);
704 data->fan_input[0] = (val >> 8) & 0xff;
705 data->fan_input[1] = val & 0xff;
706
707 val = gl520_read_value(client, GL520_REG_FAN_MIN);
708 data->fan_min[0] = (val >> 8) & 0xff;
709 data->fan_min[1] = val & 0xff;
710
711 data->temp_input[0] = gl520_read_value(client, GL520_REG_TEMP1_INPUT);
712 data->temp_max[0] = gl520_read_value(client, GL520_REG_TEMP1_MAX);
713 data->temp_max_hyst[0] = gl520_read_value(client, GL520_REG_TEMP1_MAX_HYST);
714
715 val = gl520_read_value(client, GL520_REG_FAN_DIV);
716 data->fan_div[0] = (val >> 6) & 0x03;
717 data->fan_div[1] = (val >> 4) & 0x03;
718 data->fan_off = (val >> 2) & 0x01;
719
720 data->alarms &= data->alarm_mask;
721
722 val = gl520_read_value(client, GL520_REG_CONF);
723 data->beep_enable = !((val >> 2) & 1);
724
725 data->in_input[0] = gl520_read_value(client, GL520_REG_IN0_INPUT);
726 data->in_input[1] = gl520_read_value(client, GL520_REG_IN1_INPUT);
727 data->in_input[2] = gl520_read_value(client, GL520_REG_IN2_INPUT);
728 data->in_input[3] = gl520_read_value(client, GL520_REG_IN3_INPUT);
729
730 /* Temp1 and Vin4 are the same input */
731 if (data->two_temps) {
732 data->temp_input[1] = gl520_read_value(client, GL520_REG_TEMP2_INPUT);
733 data->temp_max[1] = gl520_read_value(client, GL520_REG_TEMP2_MAX);
734 data->temp_max_hyst[1] = gl520_read_value(client, GL520_REG_TEMP2_MAX_HYST);
735 } else {
736 data->in_input[4] = gl520_read_value(client, GL520_REG_IN4_INPUT);
737 data->in_min[4] = gl520_read_value(client, GL520_REG_IN4_MIN);
738 data->in_max[4] = gl520_read_value(client, GL520_REG_IN4_MAX);
739 }
740
741 data->last_updated = jiffies;
742 data->valid = 1;
743 }
744
745 up(&data->update_lock);
746
747 return data;
748}
749
750
751static int __init sensors_gl520sm_init(void)
752{
753 return i2c_add_driver(&gl520_driver);
754}
755
756static void __exit sensors_gl520sm_exit(void)
757{
758 i2c_del_driver(&gl520_driver);
759}
760
761
762MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, "
763 "Kyösti Mälkki <kmalkki@cc.hut.fi>, "
764 "Maarten Deprez <maartendeprez@users.sourceforge.net>");
765MODULE_DESCRIPTION("GL520SM driver");
766MODULE_LICENSE("GPL");
767
768module_init(sensors_gl520sm_init);
769module_exit(sensors_gl520sm_exit);
diff --git a/drivers/i2c/chips/isp1301_omap.c b/drivers/i2c/chips/isp1301_omap.c
new file mode 100644
index 000000000000..7f29a8aff165
--- /dev/null
+++ b/drivers/i2c/chips/isp1301_omap.c
@@ -0,0 +1,1658 @@
1/*
2 * isp1301_omap - ISP 1301 USB transceiver, talking to OMAP OTG controller
3 *
4 * Copyright (C) 2004 Texas Instruments
5 * Copyright (C) 2004 David Brownell
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#undef DEBUG
22#undef VERBOSE
23
24#include <linux/config.h>
25#include <linux/kernel.h>
26#include <linux/module.h>
27#include <linux/init.h>
28#include <linux/slab.h>
29#include <linux/interrupt.h>
30#include <linux/device.h>
31#include <linux/usb_ch9.h>
32#include <linux/usb_gadget.h>
33#include <linux/usb.h>
34#include <linux/usb_otg.h>
35#include <linux/i2c.h>
36#include <linux/workqueue.h>
37
38#include <asm/irq.h>
39#include <asm/arch/usb.h>
40
41
42#ifndef DEBUG
43#undef VERBOSE
44#endif
45
46
47#define DRIVER_VERSION "24 August 2004"
48#define DRIVER_NAME (isp1301_driver.name)
49
50MODULE_DESCRIPTION("ISP1301 USB OTG Transceiver Driver");
51MODULE_LICENSE("GPL");
52
53struct isp1301 {
54 struct otg_transceiver otg;
55 struct i2c_client client;
56 void (*i2c_release)(struct device *dev);
57
58 int irq;
59
60 u32 last_otg_ctrl;
61 unsigned working:1;
62
63 struct timer_list timer;
64
65 /* use keventd context to change the state for us */
66 struct work_struct work;
67
68 unsigned long todo;
69# define WORK_UPDATE_ISP 0 /* update ISP from OTG */
70# define WORK_UPDATE_OTG 1 /* update OTG from ISP */
71# define WORK_HOST_RESUME 4 /* resume host */
72# define WORK_TIMER 6 /* timer fired */
73# define WORK_STOP 7 /* don't resubmit */
74};
75
76
77/* bits in OTG_CTRL_REG */
78
79#define OTG_XCEIV_OUTPUTS \
80 (OTG_ASESSVLD|OTG_BSESSEND|OTG_BSESSVLD|OTG_VBUSVLD|OTG_ID)
81#define OTG_XCEIV_INPUTS \
82 (OTG_PULLDOWN|OTG_PULLUP|OTG_DRV_VBUS|OTG_PD_VBUS|OTG_PU_VBUS|OTG_PU_ID)
83#define OTG_CTRL_BITS \
84 (OTG_A_BUSREQ|OTG_A_SETB_HNPEN|OTG_B_BUSREQ|OTG_B_HNPEN|OTG_BUSDROP)
85 /* and OTG_PULLUP is sometimes written */
86
87#define OTG_CTRL_MASK (OTG_DRIVER_SEL| \
88 OTG_XCEIV_OUTPUTS|OTG_XCEIV_INPUTS| \
89 OTG_CTRL_BITS)
90
91
92/*-------------------------------------------------------------------------*/
93
94#ifdef CONFIG_MACH_OMAP_H2
95
96/* board-specific PM hooks */
97
98#include <asm/arch/gpio.h>
99#include <asm/arch/mux.h>
100#include <asm/mach-types.h>
101
102
103#if defined(CONFIG_TPS65010) || defined(CONFIG_TPS65010_MODULE)
104
105#include <asm/arch/tps65010.h>
106
107#else
108
109static inline int tps65010_set_vbus_draw(unsigned mA)
110{
111 pr_debug("tps65010: draw %d mA (STUB)\n", mA);
112 return 0;
113}
114
115#endif
116
117static void enable_vbus_draw(struct isp1301 *isp, unsigned mA)
118{
119 int status = tps65010_set_vbus_draw(mA);
120 if (status < 0)
121 pr_debug(" VBUS %d mA error %d\n", mA, status);
122}
123
124static void enable_vbus_source(struct isp1301 *isp)
125{
126 /* this board won't supply more than 8mA vbus power.
127 * some boards can switch a 100ma "unit load" (or more).
128 */
129}
130
131
132/* products will deliver OTG messages with LEDs, GUI, etc */
133static inline void notresponding(struct isp1301 *isp)
134{
135 printk(KERN_NOTICE "OTG device not responding.\n");
136}
137
138
139#endif
140
141/*-------------------------------------------------------------------------*/
142
143/* only two addresses possible */
144#define ISP_BASE 0x2c
145static unsigned short normal_i2c[] = {
146 ISP_BASE, ISP_BASE + 1,
147 I2C_CLIENT_END };
148static unsigned short normal_i2c_range[] = { I2C_CLIENT_END };
149
150I2C_CLIENT_INSMOD;
151
152static struct i2c_driver isp1301_driver;
153
154/* smbus apis are used for portability */
155
156static inline u8
157isp1301_get_u8(struct isp1301 *isp, u8 reg)
158{
159 return i2c_smbus_read_byte_data(&isp->client, reg + 0);
160}
161
162static inline int
163isp1301_get_u16(struct isp1301 *isp, u8 reg)
164{
165 return i2c_smbus_read_word_data(&isp->client, reg);
166}
167
168static inline int
169isp1301_set_bits(struct isp1301 *isp, u8 reg, u8 bits)
170{
171 return i2c_smbus_write_byte_data(&isp->client, reg + 0, bits);
172}
173
174static inline int
175isp1301_clear_bits(struct isp1301 *isp, u8 reg, u8 bits)
176{
177 return i2c_smbus_write_byte_data(&isp->client, reg + 1, bits);
178}
179
180/*-------------------------------------------------------------------------*/
181
182/* identification */
183#define ISP1301_VENDOR_ID 0x00 /* u16 read */
184#define ISP1301_PRODUCT_ID 0x02 /* u16 read */
185#define ISP1301_BCD_DEVICE 0x14 /* u16 read */
186
187#define I2C_VENDOR_ID_PHILIPS 0x04cc
188#define I2C_PRODUCT_ID_PHILIPS_1301 0x1301
189
190/* operational registers */
191#define ISP1301_MODE_CONTROL_1 0x04 /* u8 read, set, +1 clear */
192# define MC1_SPEED_REG (1 << 0)
193# define MC1_SUSPEND_REG (1 << 1)
194# define MC1_DAT_SE0 (1 << 2)
195# define MC1_TRANSPARENT (1 << 3)
196# define MC1_BDIS_ACON_EN (1 << 4)
197# define MC1_OE_INT_EN (1 << 5)
198# define MC1_UART_EN (1 << 6)
199# define MC1_MASK 0x7f
200#define ISP1301_MODE_CONTROL_2 0x12 /* u8 read, set, +1 clear */
201# define MC2_GLOBAL_PWR_DN (1 << 0)
202# define MC2_SPD_SUSP_CTRL (1 << 1)
203# define MC2_BI_DI (1 << 2)
204# define MC2_TRANSP_BDIR0 (1 << 3)
205# define MC2_TRANSP_BDIR1 (1 << 4)
206# define MC2_AUDIO_EN (1 << 5)
207# define MC2_PSW_EN (1 << 6)
208# define MC2_EN2V7 (1 << 7)
209#define ISP1301_OTG_CONTROL_1 0x06 /* u8 read, set, +1 clear */
210# define OTG1_DP_PULLUP (1 << 0)
211# define OTG1_DM_PULLUP (1 << 1)
212# define OTG1_DP_PULLDOWN (1 << 2)
213# define OTG1_DM_PULLDOWN (1 << 3)
214# define OTG1_ID_PULLDOWN (1 << 4)
215# define OTG1_VBUS_DRV (1 << 5)
216# define OTG1_VBUS_DISCHRG (1 << 6)
217# define OTG1_VBUS_CHRG (1 << 7)
218#define ISP1301_OTG_STATUS 0x10 /* u8 readonly */
219# define OTG_B_SESS_END (1 << 6)
220# define OTG_B_SESS_VLD (1 << 7)
221
222#define ISP1301_INTERRUPT_SOURCE 0x08 /* u8 read */
223#define ISP1301_INTERRUPT_LATCH 0x0A /* u8 read, set, +1 clear */
224
225#define ISP1301_INTERRUPT_FALLING 0x0C /* u8 read, set, +1 clear */
226#define ISP1301_INTERRUPT_RISING 0x0E /* u8 read, set, +1 clear */
227
228/* same bitfields in all interrupt registers */
229# define INTR_VBUS_VLD (1 << 0)
230# define INTR_SESS_VLD (1 << 1)
231# define INTR_DP_HI (1 << 2)
232# define INTR_ID_GND (1 << 3)
233# define INTR_DM_HI (1 << 4)
234# define INTR_ID_FLOAT (1 << 5)
235# define INTR_BDIS_ACON (1 << 6)
236# define INTR_CR_INT (1 << 7)
237
238/*-------------------------------------------------------------------------*/
239
240static const char *state_string(enum usb_otg_state state)
241{
242 switch (state) {
243 case OTG_STATE_A_IDLE: return "a_idle";
244 case OTG_STATE_A_WAIT_VRISE: return "a_wait_vrise";
245 case OTG_STATE_A_WAIT_BCON: return "a_wait_bcon";
246 case OTG_STATE_A_HOST: return "a_host";
247 case OTG_STATE_A_SUSPEND: return "a_suspend";
248 case OTG_STATE_A_PERIPHERAL: return "a_peripheral";
249 case OTG_STATE_A_WAIT_VFALL: return "a_wait_vfall";
250 case OTG_STATE_A_VBUS_ERR: return "a_vbus_err";
251 case OTG_STATE_B_IDLE: return "b_idle";
252 case OTG_STATE_B_SRP_INIT: return "b_srp_init";
253 case OTG_STATE_B_PERIPHERAL: return "b_peripheral";
254 case OTG_STATE_B_WAIT_ACON: return "b_wait_acon";
255 case OTG_STATE_B_HOST: return "b_host";
256 default: return "UNDEFINED";
257 }
258}
259
260static inline const char *state_name(struct isp1301 *isp)
261{
262 return state_string(isp->otg.state);
263}
264
265#ifdef VERBOSE
266#define dev_vdbg dev_dbg
267#else
268#define dev_vdbg(dev, fmt, arg...) do{}while(0)
269#endif
270
271/*-------------------------------------------------------------------------*/
272
273/* NOTE: some of this ISP1301 setup is specific to H2 boards;
274 * not everything is guarded by board-specific checks, or even using
275 * omap_usb_config data to deduce MC1_DAT_SE0 and MC2_BI_DI.
276 *
277 * ALSO: this currently doesn't use ISP1301 low-power modes
278 * while OTG is running.
279 */
280
281static void power_down(struct isp1301 *isp)
282{
283 isp->otg.state = OTG_STATE_UNDEFINED;
284
285 // isp1301_set_bits(isp, ISP1301_MODE_CONTROL_2, MC2_GLOBAL_PWR_DN);
286 isp1301_set_bits(isp, ISP1301_MODE_CONTROL_1, MC1_SUSPEND_REG);
287
288 isp1301_clear_bits(isp, ISP1301_OTG_CONTROL_1, OTG1_ID_PULLDOWN);
289 isp1301_clear_bits(isp, ISP1301_MODE_CONTROL_1, MC1_DAT_SE0);
290}
291
292static void power_up(struct isp1301 *isp)
293{
294 // isp1301_clear_bits(isp, ISP1301_MODE_CONTROL_2, MC2_GLOBAL_PWR_DN);
295 isp1301_clear_bits(isp, ISP1301_MODE_CONTROL_1, MC1_SUSPEND_REG);
296
297 /* do this only when cpu is driving transceiver,
298 * so host won't see a low speed device...
299 */
300 isp1301_set_bits(isp, ISP1301_MODE_CONTROL_1, MC1_DAT_SE0);
301}
302
303#define NO_HOST_SUSPEND
304
305static int host_suspend(struct isp1301 *isp)
306{
307#ifdef NO_HOST_SUSPEND
308 return 0;
309#else
310 struct device *dev;
311
312 if (!isp->otg.host)
313 return -ENODEV;
314
315 /* Currently ASSUMES only the OTG port matters;
316 * other ports could be active...
317 */
318 dev = isp->otg.host->controller;
319 return dev->driver->suspend(dev, 3, 0);
320#endif
321}
322
323static int host_resume(struct isp1301 *isp)
324{
325#ifdef NO_HOST_SUSPEND
326 return 0;
327#else
328 struct device *dev;
329
330 if (!isp->otg.host)
331 return -ENODEV;
332
333 dev = isp->otg.host->controller;
334 return dev->driver->resume(dev, 0);
335#endif
336}
337
338static int gadget_suspend(struct isp1301 *isp)
339{
340 isp->otg.gadget->b_hnp_enable = 0;
341 isp->otg.gadget->a_hnp_support = 0;
342 isp->otg.gadget->a_alt_hnp_support = 0;
343 return usb_gadget_vbus_disconnect(isp->otg.gadget);
344}
345
346/*-------------------------------------------------------------------------*/
347
348#define TIMER_MINUTES 10
349#define TIMER_JIFFIES (TIMER_MINUTES * 60 * HZ)
350
351/* Almost all our I2C messaging comes from a work queue's task context.
352 * NOTE: guaranteeing certain response times might mean we shouldn't
353 * share keventd's work queue; a realtime task might be safest.
354 */
355void
356isp1301_defer_work(struct isp1301 *isp, int work)
357{
358 int status;
359
360 if (isp && !test_and_set_bit(work, &isp->todo)) {
361 (void) get_device(&isp->client.dev);
362 status = schedule_work(&isp->work);
363 if (!status && !isp->working)
364 dev_vdbg(&isp->client.dev,
365 "work item %d may be lost\n", work);
366 }
367}
368
369/* called from irq handlers */
370static void a_idle(struct isp1301 *isp, const char *tag)
371{
372 if (isp->otg.state == OTG_STATE_A_IDLE)
373 return;
374
375 isp->otg.default_a = 1;
376 if (isp->otg.host) {
377 isp->otg.host->is_b_host = 0;
378 host_suspend(isp);
379 }
380 if (isp->otg.gadget) {
381 isp->otg.gadget->is_a_peripheral = 1;
382 gadget_suspend(isp);
383 }
384 isp->otg.state = OTG_STATE_A_IDLE;
385 isp->last_otg_ctrl = OTG_CTRL_REG = OTG_CTRL_REG & OTG_XCEIV_OUTPUTS;
386 pr_debug(" --> %s/%s\n", state_name(isp), tag);
387}
388
389/* called from irq handlers */
390static void b_idle(struct isp1301 *isp, const char *tag)
391{
392 if (isp->otg.state == OTG_STATE_B_IDLE)
393 return;
394
395 isp->otg.default_a = 0;
396 if (isp->otg.host) {
397 isp->otg.host->is_b_host = 1;
398 host_suspend(isp);
399 }
400 if (isp->otg.gadget) {
401 isp->otg.gadget->is_a_peripheral = 0;
402 gadget_suspend(isp);
403 }
404 isp->otg.state = OTG_STATE_B_IDLE;
405 isp->last_otg_ctrl = OTG_CTRL_REG = OTG_CTRL_REG & OTG_XCEIV_OUTPUTS;
406 pr_debug(" --> %s/%s\n", state_name(isp), tag);
407}
408
409static void
410dump_regs(struct isp1301 *isp, const char *label)
411{
412#ifdef DEBUG
413 u8 ctrl = isp1301_get_u8(isp, ISP1301_OTG_CONTROL_1);
414 u8 status = isp1301_get_u8(isp, ISP1301_OTG_STATUS);
415 u8 src = isp1301_get_u8(isp, ISP1301_INTERRUPT_SOURCE);
416
417 pr_debug("otg: %06x, %s %s, otg/%02x stat/%02x.%02x\n",
418 OTG_CTRL_REG, label, state_name(isp),
419 ctrl, status, src);
420 /* mode control and irq enables don't change much */
421#endif
422}
423
424/*-------------------------------------------------------------------------*/
425
426#ifdef CONFIG_USB_OTG
427
428/*
429 * The OMAP OTG controller handles most of the OTG state transitions.
430 *
431 * We translate isp1301 outputs (mostly voltage comparator status) into
432 * OTG inputs; OTG outputs (mostly pullup/pulldown controls) and HNP state
433 * flags into isp1301 inputs ... and infer state transitions.
434 */
435
436#ifdef VERBOSE
437
438static void check_state(struct isp1301 *isp, const char *tag)
439{
440 enum usb_otg_state state = OTG_STATE_UNDEFINED;
441 u8 fsm = OTG_TEST_REG & 0x0ff;
442 unsigned extra = 0;
443
444 switch (fsm) {
445
446 /* default-b */
447 case 0x0:
448 state = OTG_STATE_B_IDLE;
449 break;
450 case 0x3:
451 case 0x7:
452 extra = 1;
453 case 0x1:
454 state = OTG_STATE_B_PERIPHERAL;
455 break;
456 case 0x11:
457 state = OTG_STATE_B_SRP_INIT;
458 break;
459
460 /* extra dual-role default-b states */
461 case 0x12:
462 case 0x13:
463 case 0x16:
464 extra = 1;
465 case 0x17:
466 state = OTG_STATE_B_WAIT_ACON;
467 break;
468 case 0x34:
469 state = OTG_STATE_B_HOST;
470 break;
471
472 /* default-a */
473 case 0x36:
474 state = OTG_STATE_A_IDLE;
475 break;
476 case 0x3c:
477 state = OTG_STATE_A_WAIT_VFALL;
478 break;
479 case 0x7d:
480 state = OTG_STATE_A_VBUS_ERR;
481 break;
482 case 0x9e:
483 case 0x9f:
484 extra = 1;
485 case 0x89:
486 state = OTG_STATE_A_PERIPHERAL;
487 break;
488 case 0xb7:
489 state = OTG_STATE_A_WAIT_VRISE;
490 break;
491 case 0xb8:
492 state = OTG_STATE_A_WAIT_BCON;
493 break;
494 case 0xb9:
495 state = OTG_STATE_A_HOST;
496 break;
497 case 0xba:
498 state = OTG_STATE_A_SUSPEND;
499 break;
500 default:
501 break;
502 }
503 if (isp->otg.state == state && !extra)
504 return;
505 pr_debug("otg: %s FSM %s/%02x, %s, %06x\n", tag,
506 state_string(state), fsm, state_name(isp), OTG_CTRL_REG);
507}
508
509#else
510
511static inline void check_state(struct isp1301 *isp, const char *tag) { }
512
513#endif
514
515/* outputs from ISP1301_INTERRUPT_SOURCE */
516static void update_otg1(struct isp1301 *isp, u8 int_src)
517{
518 u32 otg_ctrl;
519
520 otg_ctrl = OTG_CTRL_REG
521 & OTG_CTRL_MASK
522 & ~OTG_XCEIV_INPUTS
523 & ~(OTG_ID|OTG_ASESSVLD|OTG_VBUSVLD);
524 if (int_src & INTR_SESS_VLD)
525 otg_ctrl |= OTG_ASESSVLD;
526 else if (isp->otg.state == OTG_STATE_A_WAIT_VFALL) {
527 a_idle(isp, "vfall");
528 otg_ctrl &= ~OTG_CTRL_BITS;
529 }
530 if (int_src & INTR_VBUS_VLD)
531 otg_ctrl |= OTG_VBUSVLD;
532 if (int_src & INTR_ID_GND) { /* default-A */
533 if (isp->otg.state == OTG_STATE_B_IDLE
534 || isp->otg.state == OTG_STATE_UNDEFINED) {
535 a_idle(isp, "init");
536 return;
537 }
538 } else { /* default-B */
539 otg_ctrl |= OTG_ID;
540 if (isp->otg.state == OTG_STATE_A_IDLE
541 || isp->otg.state == OTG_STATE_UNDEFINED) {
542 b_idle(isp, "init");
543 return;
544 }
545 }
546 OTG_CTRL_REG = otg_ctrl;
547}
548
549/* outputs from ISP1301_OTG_STATUS */
550static void update_otg2(struct isp1301 *isp, u8 otg_status)
551{
552 u32 otg_ctrl;
553
554 otg_ctrl = OTG_CTRL_REG
555 & OTG_CTRL_MASK
556 & ~OTG_XCEIV_INPUTS
557 & ~(OTG_BSESSVLD|OTG_BSESSEND);
558 if (otg_status & OTG_B_SESS_VLD)
559 otg_ctrl |= OTG_BSESSVLD;
560 else if (otg_status & OTG_B_SESS_END)
561 otg_ctrl |= OTG_BSESSEND;
562 OTG_CTRL_REG = otg_ctrl;
563}
564
565/* inputs going to ISP1301 */
566static void otg_update_isp(struct isp1301 *isp)
567{
568 u32 otg_ctrl, otg_change;
569 u8 set = OTG1_DM_PULLDOWN, clr = OTG1_DM_PULLUP;
570
571 otg_ctrl = OTG_CTRL_REG;
572 otg_change = otg_ctrl ^ isp->last_otg_ctrl;
573 isp->last_otg_ctrl = otg_ctrl;
574 otg_ctrl = otg_ctrl & OTG_XCEIV_INPUTS;
575
576 switch (isp->otg.state) {
577 case OTG_STATE_B_IDLE:
578 case OTG_STATE_B_PERIPHERAL:
579 case OTG_STATE_B_SRP_INIT:
580 if (!(otg_ctrl & OTG_PULLUP)) {
581 // if (otg_ctrl & OTG_B_HNPEN) {
582 if (isp->otg.gadget->b_hnp_enable) {
583 isp->otg.state = OTG_STATE_B_WAIT_ACON;
584 pr_debug(" --> b_wait_acon\n");
585 }
586 goto pulldown;
587 }
588pullup:
589 set |= OTG1_DP_PULLUP;
590 clr |= OTG1_DP_PULLDOWN;
591 break;
592 case OTG_STATE_A_SUSPEND:
593 case OTG_STATE_A_PERIPHERAL:
594 if (otg_ctrl & OTG_PULLUP)
595 goto pullup;
596 /* FALLTHROUGH */
597 // case OTG_STATE_B_WAIT_ACON:
598 default:
599pulldown:
600 set |= OTG1_DP_PULLDOWN;
601 clr |= OTG1_DP_PULLUP;
602 break;
603 }
604
605# define toggle(OTG,ISP) do { \
606 if (otg_ctrl & OTG) set |= ISP; \
607 else clr |= ISP; \
608 } while (0)
609
610 if (!(isp->otg.host))
611 otg_ctrl &= ~OTG_DRV_VBUS;
612
613 switch (isp->otg.state) {
614 case OTG_STATE_A_SUSPEND:
615 if (otg_ctrl & OTG_DRV_VBUS) {
616 set |= OTG1_VBUS_DRV;
617 break;
618 }
619 /* HNP failed for some reason (A_AIDL_BDIS timeout) */
620 notresponding(isp);
621
622 /* FALLTHROUGH */
623 case OTG_STATE_A_VBUS_ERR:
624 isp->otg.state = OTG_STATE_A_WAIT_VFALL;
625 pr_debug(" --> a_wait_vfall\n");
626 /* FALLTHROUGH */
627 case OTG_STATE_A_WAIT_VFALL:
628 /* FIXME usbcore thinks port power is still on ... */
629 clr |= OTG1_VBUS_DRV;
630 break;
631 case OTG_STATE_A_IDLE:
632 if (otg_ctrl & OTG_DRV_VBUS) {
633 isp->otg.state = OTG_STATE_A_WAIT_VRISE;
634 pr_debug(" --> a_wait_vrise\n");
635 }
636 /* FALLTHROUGH */
637 default:
638 toggle(OTG_DRV_VBUS, OTG1_VBUS_DRV);
639 }
640
641 toggle(OTG_PU_VBUS, OTG1_VBUS_CHRG);
642 toggle(OTG_PD_VBUS, OTG1_VBUS_DISCHRG);
643
644# undef toggle
645
646 isp1301_set_bits(isp, ISP1301_OTG_CONTROL_1, set);
647 isp1301_clear_bits(isp, ISP1301_OTG_CONTROL_1, clr);
648
649 /* HNP switch to host or peripheral; and SRP */
650 if (otg_change & OTG_PULLUP) {
651 switch (isp->otg.state) {
652 case OTG_STATE_B_IDLE:
653 if (clr & OTG1_DP_PULLUP)
654 break;
655 isp->otg.state = OTG_STATE_B_PERIPHERAL;
656 pr_debug(" --> b_peripheral\n");
657 break;
658 case OTG_STATE_A_SUSPEND:
659 if (clr & OTG1_DP_PULLUP)
660 break;
661 isp->otg.state = OTG_STATE_A_PERIPHERAL;
662 pr_debug(" --> a_peripheral\n");
663 break;
664 default:
665 break;
666 }
667 OTG_CTRL_REG |= OTG_PULLUP;
668 }
669
670 check_state(isp, __FUNCTION__);
671 dump_regs(isp, "otg->isp1301");
672}
673
674static irqreturn_t omap_otg_irq(int irq, void *_isp, struct pt_regs *regs)
675{
676 u16 otg_irq = OTG_IRQ_SRC_REG;
677 u32 otg_ctrl;
678 int ret = IRQ_NONE;
679 struct isp1301 *isp = _isp;
680
681 /* update ISP1301 transciever from OTG controller */
682 if (otg_irq & OPRT_CHG) {
683 OTG_IRQ_SRC_REG = OPRT_CHG;
684 isp1301_defer_work(isp, WORK_UPDATE_ISP);
685 ret = IRQ_HANDLED;
686
687 /* SRP to become b_peripheral failed */
688 } else if (otg_irq & B_SRP_TMROUT) {
689 pr_debug("otg: B_SRP_TIMEOUT, %06x\n", OTG_CTRL_REG);
690 notresponding(isp);
691
692 /* gadget drivers that care should monitor all kinds of
693 * remote wakeup (SRP, normal) using their own timer
694 * to give "check cable and A-device" messages.
695 */
696 if (isp->otg.state == OTG_STATE_B_SRP_INIT)
697 b_idle(isp, "srp_timeout");
698
699 OTG_IRQ_SRC_REG = B_SRP_TMROUT;
700 ret = IRQ_HANDLED;
701
702 /* HNP to become b_host failed */
703 } else if (otg_irq & B_HNP_FAIL) {
704 pr_debug("otg: %s B_HNP_FAIL, %06x\n",
705 state_name(isp), OTG_CTRL_REG);
706 notresponding(isp);
707
708 otg_ctrl = OTG_CTRL_REG;
709 otg_ctrl |= OTG_BUSDROP;
710 otg_ctrl &= OTG_CTRL_MASK & ~OTG_XCEIV_INPUTS;
711 OTG_CTRL_REG = otg_ctrl;
712
713 /* subset of b_peripheral()... */
714 isp->otg.state = OTG_STATE_B_PERIPHERAL;
715 pr_debug(" --> b_peripheral\n");
716
717 OTG_IRQ_SRC_REG = B_HNP_FAIL;
718 ret = IRQ_HANDLED;
719
720 /* detect SRP from B-device ... */
721 } else if (otg_irq & A_SRP_DETECT) {
722 pr_debug("otg: %s SRP_DETECT, %06x\n",
723 state_name(isp), OTG_CTRL_REG);
724
725 isp1301_defer_work(isp, WORK_UPDATE_OTG);
726 switch (isp->otg.state) {
727 case OTG_STATE_A_IDLE:
728 if (!isp->otg.host)
729 break;
730 isp1301_defer_work(isp, WORK_HOST_RESUME);
731 otg_ctrl = OTG_CTRL_REG;
732 otg_ctrl |= OTG_A_BUSREQ;
733 otg_ctrl &= ~(OTG_BUSDROP|OTG_B_BUSREQ)
734 & ~OTG_XCEIV_INPUTS
735 & OTG_CTRL_MASK;
736 OTG_CTRL_REG = otg_ctrl;
737 break;
738 default:
739 break;
740 }
741
742 OTG_IRQ_SRC_REG = A_SRP_DETECT;
743 ret = IRQ_HANDLED;
744
745 /* timer expired: T(a_wait_bcon) and maybe T(a_wait_vrise)
746 * we don't track them separately
747 */
748 } else if (otg_irq & A_REQ_TMROUT) {
749 otg_ctrl = OTG_CTRL_REG;
750 pr_info("otg: BCON_TMOUT from %s, %06x\n",
751 state_name(isp), otg_ctrl);
752 notresponding(isp);
753
754 otg_ctrl |= OTG_BUSDROP;
755 otg_ctrl &= ~OTG_A_BUSREQ & OTG_CTRL_MASK & ~OTG_XCEIV_INPUTS;
756 OTG_CTRL_REG = otg_ctrl;
757 isp->otg.state = OTG_STATE_A_WAIT_VFALL;
758
759 OTG_IRQ_SRC_REG = A_REQ_TMROUT;
760 ret = IRQ_HANDLED;
761
762 /* A-supplied voltage fell too low; overcurrent */
763 } else if (otg_irq & A_VBUS_ERR) {
764 otg_ctrl = OTG_CTRL_REG;
765 printk(KERN_ERR "otg: %s, VBUS_ERR %04x ctrl %06x\n",
766 state_name(isp), otg_irq, otg_ctrl);
767
768 otg_ctrl |= OTG_BUSDROP;
769 otg_ctrl &= ~OTG_A_BUSREQ & OTG_CTRL_MASK & ~OTG_XCEIV_INPUTS;
770 OTG_CTRL_REG = otg_ctrl;
771 isp->otg.state = OTG_STATE_A_VBUS_ERR;
772
773 OTG_IRQ_SRC_REG = A_VBUS_ERR;
774 ret = IRQ_HANDLED;
775
776 /* switch driver; the transciever code activates it,
777 * ungating the udc clock or resuming OHCI.
778 */
779 } else if (otg_irq & DRIVER_SWITCH) {
780 int kick = 0;
781
782 otg_ctrl = OTG_CTRL_REG;
783 printk(KERN_NOTICE "otg: %s, SWITCH to %s, ctrl %06x\n",
784 state_name(isp),
785 (otg_ctrl & OTG_DRIVER_SEL)
786 ? "gadget" : "host",
787 otg_ctrl);
788 isp1301_defer_work(isp, WORK_UPDATE_ISP);
789
790 /* role is peripheral */
791 if (otg_ctrl & OTG_DRIVER_SEL) {
792 switch (isp->otg.state) {
793 case OTG_STATE_A_IDLE:
794 b_idle(isp, __FUNCTION__);
795 break;
796 default:
797 break;
798 }
799 isp1301_defer_work(isp, WORK_UPDATE_ISP);
800
801 /* role is host */
802 } else {
803 if (!(otg_ctrl & OTG_ID)) {
804 otg_ctrl &= OTG_CTRL_MASK & ~OTG_XCEIV_INPUTS;
805 OTG_CTRL_REG = otg_ctrl | OTG_A_BUSREQ;
806 }
807
808 if (isp->otg.host) {
809 switch (isp->otg.state) {
810 case OTG_STATE_B_WAIT_ACON:
811 isp->otg.state = OTG_STATE_B_HOST;
812 pr_debug(" --> b_host\n");
813 kick = 1;
814 break;
815 case OTG_STATE_A_WAIT_BCON:
816 isp->otg.state = OTG_STATE_A_HOST;
817 pr_debug(" --> a_host\n");
818 break;
819 case OTG_STATE_A_PERIPHERAL:
820 isp->otg.state = OTG_STATE_A_WAIT_BCON;
821 pr_debug(" --> a_wait_bcon\n");
822 break;
823 default:
824 break;
825 }
826 isp1301_defer_work(isp, WORK_HOST_RESUME);
827 }
828 }
829
830 OTG_IRQ_SRC_REG = DRIVER_SWITCH;
831 ret = IRQ_HANDLED;
832
833 if (kick)
834 usb_bus_start_enum(isp->otg.host,
835 isp->otg.host->otg_port);
836 }
837
838 check_state(isp, __FUNCTION__);
839 return ret;
840}
841
842static struct platform_device *otg_dev;
843
844static int otg_init(struct isp1301 *isp)
845{
846 if (!otg_dev)
847 return -ENODEV;
848
849 dump_regs(isp, __FUNCTION__);
850 /* some of these values are board-specific... */
851 OTG_SYSCON_2_REG |= OTG_EN
852 /* for B-device: */
853 | SRP_GPDATA /* 9msec Bdev D+ pulse */
854 | SRP_GPDVBUS /* discharge after VBUS pulse */
855 // | (3 << 24) /* 2msec VBUS pulse */
856 /* for A-device: */
857 | (0 << 20) /* 200ms nominal A_WAIT_VRISE timer */
858 | SRP_DPW /* detect 167+ns SRP pulses */
859 | SRP_DATA | SRP_VBUS /* accept both kinds of SRP pulse */
860 ;
861
862 update_otg1(isp, isp1301_get_u8(isp, ISP1301_INTERRUPT_SOURCE));
863 update_otg2(isp, isp1301_get_u8(isp, ISP1301_OTG_STATUS));
864
865 check_state(isp, __FUNCTION__);
866 pr_debug("otg: %s, %s %06x\n",
867 state_name(isp), __FUNCTION__, OTG_CTRL_REG);
868
869 OTG_IRQ_EN_REG = DRIVER_SWITCH | OPRT_CHG
870 | B_SRP_TMROUT | B_HNP_FAIL
871 | A_VBUS_ERR | A_SRP_DETECT | A_REQ_TMROUT;
872 OTG_SYSCON_2_REG |= OTG_EN;
873
874 return 0;
875}
876
877static int otg_probe(struct device *dev)
878{
879 // struct omap_usb_config *config = dev->platform_data;
880
881 otg_dev = to_platform_device(dev);
882 return 0;
883}
884
885static int otg_remove(struct device *dev)
886{
887 otg_dev = 0;
888 return 0;
889}
890
891struct device_driver omap_otg_driver = {
892 .name = "omap_otg",
893 .bus = &platform_bus_type,
894 .probe = otg_probe,
895 .remove = otg_remove,
896};
897
898static int otg_bind(struct isp1301 *isp)
899{
900 int status;
901
902 if (otg_dev)
903 return -EBUSY;
904
905 status = driver_register(&omap_otg_driver);
906 if (status < 0)
907 return status;
908
909 if (otg_dev)
910 status = request_irq(otg_dev->resource[1].start, omap_otg_irq,
911 SA_INTERRUPT, DRIVER_NAME, isp);
912 else
913 status = -ENODEV;
914
915 if (status < 0)
916 driver_unregister(&omap_otg_driver);
917 return status;
918}
919
920static void otg_unbind(struct isp1301 *isp)
921{
922 if (!otg_dev)
923 return;
924 free_irq(otg_dev->resource[1].start, isp);
925}
926
927#else
928
929/* OTG controller isn't clocked */
930
931#endif /* CONFIG_USB_OTG */
932
933/*-------------------------------------------------------------------------*/
934
935static void b_peripheral(struct isp1301 *isp)
936{
937 OTG_CTRL_REG = OTG_CTRL_REG & OTG_XCEIV_OUTPUTS;
938 usb_gadget_vbus_connect(isp->otg.gadget);
939
940#ifdef CONFIG_USB_OTG
941 enable_vbus_draw(isp, 8);
942 otg_update_isp(isp);
943#else
944 enable_vbus_draw(isp, 100);
945 /* UDC driver just set OTG_BSESSVLD */
946 isp1301_set_bits(isp, ISP1301_OTG_CONTROL_1, OTG1_DP_PULLUP);
947 isp1301_clear_bits(isp, ISP1301_OTG_CONTROL_1, OTG1_DP_PULLDOWN);
948 isp->otg.state = OTG_STATE_B_PERIPHERAL;
949 pr_debug(" --> b_peripheral\n");
950 dump_regs(isp, "2periph");
951#endif
952}
953
954static void isp_update_otg(struct isp1301 *isp, u8 stat)
955{
956 u8 isp_stat, isp_bstat;
957 enum usb_otg_state state = isp->otg.state;
958
959 if (stat & INTR_BDIS_ACON)
960 pr_debug("OTG: BDIS_ACON, %s\n", state_name(isp));
961
962 /* start certain state transitions right away */
963 isp_stat = isp1301_get_u8(isp, ISP1301_INTERRUPT_SOURCE);
964 if (isp_stat & INTR_ID_GND) {
965 if (isp->otg.default_a) {
966 switch (state) {
967 case OTG_STATE_B_IDLE:
968 a_idle(isp, "idle");
969 /* FALLTHROUGH */
970 case OTG_STATE_A_IDLE:
971 enable_vbus_source(isp);
972 /* FALLTHROUGH */
973 case OTG_STATE_A_WAIT_VRISE:
974 /* we skip over OTG_STATE_A_WAIT_BCON, since
975 * the HC will transition to A_HOST (or
976 * A_SUSPEND!) without our noticing except
977 * when HNP is used.
978 */
979 if (isp_stat & INTR_VBUS_VLD)
980 isp->otg.state = OTG_STATE_A_HOST;
981 break;
982 case OTG_STATE_A_WAIT_VFALL:
983 if (!(isp_stat & INTR_SESS_VLD))
984 a_idle(isp, "vfell");
985 break;
986 default:
987 if (!(isp_stat & INTR_VBUS_VLD))
988 isp->otg.state = OTG_STATE_A_VBUS_ERR;
989 break;
990 }
991 isp_bstat = isp1301_get_u8(isp, ISP1301_OTG_STATUS);
992 } else {
993 switch (state) {
994 case OTG_STATE_B_PERIPHERAL:
995 case OTG_STATE_B_HOST:
996 case OTG_STATE_B_WAIT_ACON:
997 usb_gadget_vbus_disconnect(isp->otg.gadget);
998 break;
999 default:
1000 break;
1001 }
1002 if (state != OTG_STATE_A_IDLE)
1003 a_idle(isp, "id");
1004 if (isp->otg.host && state == OTG_STATE_A_IDLE)
1005 isp1301_defer_work(isp, WORK_HOST_RESUME);
1006 isp_bstat = 0;
1007 }
1008 } else {
1009 /* if user unplugged mini-A end of cable,
1010 * don't bypass A_WAIT_VFALL.
1011 */
1012 if (isp->otg.default_a) {
1013 switch (state) {
1014 default:
1015 isp->otg.state = OTG_STATE_A_WAIT_VFALL;
1016 break;
1017 case OTG_STATE_A_WAIT_VFALL:
1018 state = OTG_STATE_A_IDLE;
1019 /* khubd may take a while to notice and
1020 * handle this disconnect, so don't go
1021 * to B_IDLE quite yet.
1022 */
1023 break;
1024 case OTG_STATE_A_IDLE:
1025 host_suspend(isp);
1026 isp1301_clear_bits(isp, ISP1301_MODE_CONTROL_1,
1027 MC1_BDIS_ACON_EN);
1028 isp->otg.state = OTG_STATE_B_IDLE;
1029 OTG_CTRL_REG &= OTG_CTRL_REG & OTG_CTRL_MASK
1030 & ~OTG_CTRL_BITS;
1031 break;
1032 case OTG_STATE_B_IDLE:
1033 break;
1034 }
1035 }
1036 isp_bstat = isp1301_get_u8(isp, ISP1301_OTG_STATUS);
1037
1038 switch (isp->otg.state) {
1039 case OTG_STATE_B_PERIPHERAL:
1040 case OTG_STATE_B_WAIT_ACON:
1041 case OTG_STATE_B_HOST:
1042 if (likely(isp_bstat & OTG_B_SESS_VLD))
1043 break;
1044 enable_vbus_draw(isp, 0);
1045#ifndef CONFIG_USB_OTG
1046 /* UDC driver will clear OTG_BSESSVLD */
1047 isp1301_set_bits(isp, ISP1301_OTG_CONTROL_1,
1048 OTG1_DP_PULLDOWN);
1049 isp1301_clear_bits(isp, ISP1301_OTG_CONTROL_1,
1050 OTG1_DP_PULLUP);
1051 dump_regs(isp, __FUNCTION__);
1052#endif
1053 /* FALLTHROUGH */
1054 case OTG_STATE_B_SRP_INIT:
1055 b_idle(isp, __FUNCTION__);
1056 OTG_CTRL_REG &= OTG_CTRL_REG & OTG_XCEIV_OUTPUTS;
1057 /* FALLTHROUGH */
1058 case OTG_STATE_B_IDLE:
1059 if (isp->otg.gadget && (isp_bstat & OTG_B_SESS_VLD)) {
1060#ifdef CONFIG_USB_OTG
1061 update_otg1(isp, isp_stat);
1062 update_otg2(isp, isp_bstat);
1063#endif
1064 b_peripheral(isp);
1065 } else if (!(isp_stat & (INTR_VBUS_VLD|INTR_SESS_VLD)))
1066 isp_bstat |= OTG_B_SESS_END;
1067 break;
1068 case OTG_STATE_A_WAIT_VFALL:
1069 break;
1070 default:
1071 pr_debug("otg: unsupported b-device %s\n",
1072 state_name(isp));
1073 break;
1074 }
1075 }
1076
1077 if (state != isp->otg.state)
1078 pr_debug(" isp, %s -> %s\n",
1079 state_string(state), state_name(isp));
1080
1081#ifdef CONFIG_USB_OTG
1082 /* update the OTG controller state to match the isp1301; may
1083 * trigger OPRT_CHG irqs for changes going to the isp1301.
1084 */
1085 update_otg1(isp, isp_stat);
1086 update_otg2(isp, isp_bstat);
1087 check_state(isp, __FUNCTION__);
1088#endif
1089
1090 dump_regs(isp, "isp1301->otg");
1091}
1092
1093/*-------------------------------------------------------------------------*/
1094
1095static u8 isp1301_clear_latch(struct isp1301 *isp)
1096{
1097 u8 latch = isp1301_get_u8(isp, ISP1301_INTERRUPT_LATCH);
1098 isp1301_clear_bits(isp, ISP1301_INTERRUPT_LATCH, latch);
1099 return latch;
1100}
1101
1102static void
1103isp1301_work(void *data)
1104{
1105 struct isp1301 *isp = data;
1106 int stop;
1107
1108 /* implicit lock: we're the only task using this device */
1109 isp->working = 1;
1110 do {
1111 stop = test_bit(WORK_STOP, &isp->todo);
1112
1113#ifdef CONFIG_USB_OTG
1114 /* transfer state from otg engine to isp1301 */
1115 if (test_and_clear_bit(WORK_UPDATE_ISP, &isp->todo)) {
1116 otg_update_isp(isp);
1117 put_device(&isp->client.dev);
1118 }
1119#endif
1120 /* transfer state from isp1301 to otg engine */
1121 if (test_and_clear_bit(WORK_UPDATE_OTG, &isp->todo)) {
1122 u8 stat = isp1301_clear_latch(isp);
1123
1124 isp_update_otg(isp, stat);
1125 put_device(&isp->client.dev);
1126 }
1127
1128 if (test_and_clear_bit(WORK_HOST_RESUME, &isp->todo)) {
1129 u32 otg_ctrl;
1130
1131 /*
1132 * skip A_WAIT_VRISE; hc transitions invisibly
1133 * skip A_WAIT_BCON; same.
1134 */
1135 switch (isp->otg.state) {
1136 case OTG_STATE_A_WAIT_BCON:
1137 case OTG_STATE_A_WAIT_VRISE:
1138 isp->otg.state = OTG_STATE_A_HOST;
1139 pr_debug(" --> a_host\n");
1140 otg_ctrl = OTG_CTRL_REG;
1141 otg_ctrl |= OTG_A_BUSREQ;
1142 otg_ctrl &= ~(OTG_BUSDROP|OTG_B_BUSREQ)
1143 & OTG_CTRL_MASK;
1144 OTG_CTRL_REG = otg_ctrl;
1145 break;
1146 case OTG_STATE_B_WAIT_ACON:
1147 isp->otg.state = OTG_STATE_B_HOST;
1148 pr_debug(" --> b_host (acon)\n");
1149 break;
1150 case OTG_STATE_B_HOST:
1151 case OTG_STATE_B_IDLE:
1152 case OTG_STATE_A_IDLE:
1153 break;
1154 default:
1155 pr_debug(" host resume in %s\n",
1156 state_name(isp));
1157 }
1158 host_resume(isp);
1159 // mdelay(10);
1160 put_device(&isp->client.dev);
1161 }
1162
1163 if (test_and_clear_bit(WORK_TIMER, &isp->todo)) {
1164#ifdef VERBOSE
1165 dump_regs(isp, "timer");
1166 if (!stop)
1167 mod_timer(&isp->timer, jiffies + TIMER_JIFFIES);
1168#endif
1169 put_device(&isp->client.dev);
1170 }
1171
1172 if (isp->todo)
1173 dev_vdbg(&isp->client.dev,
1174 "work done, todo = 0x%lx\n",
1175 isp->todo);
1176 if (stop) {
1177 dev_dbg(&isp->client.dev, "stop\n");
1178 break;
1179 }
1180 } while (isp->todo);
1181 isp->working = 0;
1182}
1183
1184static irqreturn_t isp1301_irq(int irq, void *isp, struct pt_regs *regs)
1185{
1186 isp1301_defer_work(isp, WORK_UPDATE_OTG);
1187 return IRQ_HANDLED;
1188}
1189
1190static void isp1301_timer(unsigned long _isp)
1191{
1192 isp1301_defer_work((void *)_isp, WORK_TIMER);
1193}
1194
1195/*-------------------------------------------------------------------------*/
1196
1197static void isp1301_release(struct device *dev)
1198{
1199 struct isp1301 *isp;
1200
1201 isp = container_of(dev, struct isp1301, client.dev);
1202
1203 /* ugly -- i2c hijacks our memory hook to wait_for_completion() */
1204 if (isp->i2c_release)
1205 isp->i2c_release(dev);
1206 kfree (isp);
1207}
1208
1209static struct isp1301 *the_transceiver;
1210
1211static int isp1301_detach_client(struct i2c_client *i2c)
1212{
1213 struct isp1301 *isp;
1214
1215 isp = container_of(i2c, struct isp1301, client);
1216
1217 isp1301_clear_bits(isp, ISP1301_INTERRUPT_FALLING, ~0);
1218 isp1301_clear_bits(isp, ISP1301_INTERRUPT_RISING, ~0);
1219 free_irq(isp->irq, isp);
1220#ifdef CONFIG_USB_OTG
1221 otg_unbind(isp);
1222#endif
1223 if (machine_is_omap_h2())
1224 omap_free_gpio(2);
1225
1226 isp->timer.data = 0;
1227 set_bit(WORK_STOP, &isp->todo);
1228 del_timer_sync(&isp->timer);
1229 flush_scheduled_work();
1230
1231 put_device(&i2c->dev);
1232 the_transceiver = 0;
1233
1234 return i2c_detach_client(i2c);
1235}
1236
1237/*-------------------------------------------------------------------------*/
1238
1239/* NOTE: three modes are possible here, only one of which
1240 * will be standards-conformant on any given system:
1241 *
1242 * - OTG mode (dual-role), required if there's a Mini-AB connector
1243 * - HOST mode, for when there's one or more A (host) connectors
1244 * - DEVICE mode, for when there's a B/Mini-B (device) connector
1245 *
1246 * As a rule, you won't have an isp1301 chip unless it's there to
1247 * support the OTG mode. Other modes help testing USB controllers
1248 * in isolation from (full) OTG support, or maybe so later board
1249 * revisions can help to support those feature.
1250 */
1251
1252#ifdef CONFIG_USB_OTG
1253
1254static int isp1301_otg_enable(struct isp1301 *isp)
1255{
1256 power_up(isp);
1257 otg_init(isp);
1258
1259 /* NOTE: since we don't change this, this provides
1260 * a few more interrupts than are strictly needed.
1261 */
1262 isp1301_set_bits(isp, ISP1301_INTERRUPT_RISING,
1263 INTR_VBUS_VLD | INTR_SESS_VLD | INTR_ID_GND);
1264 isp1301_set_bits(isp, ISP1301_INTERRUPT_FALLING,
1265 INTR_VBUS_VLD | INTR_SESS_VLD | INTR_ID_GND);
1266
1267 dev_info(&isp->client.dev, "ready for dual-role USB ...\n");
1268
1269 return 0;
1270}
1271
1272#endif
1273
1274/* add or disable the host device+driver */
1275static int
1276isp1301_set_host(struct otg_transceiver *otg, struct usb_bus *host)
1277{
1278 struct isp1301 *isp = container_of(otg, struct isp1301, otg);
1279
1280 if (!otg || isp != the_transceiver)
1281 return -ENODEV;
1282
1283 if (!host) {
1284 OTG_IRQ_EN_REG = 0;
1285 power_down(isp);
1286 isp->otg.host = 0;
1287 return 0;
1288 }
1289
1290#ifdef CONFIG_USB_OTG
1291 isp->otg.host = host;
1292 dev_dbg(&isp->client.dev, "registered host\n");
1293 host_suspend(isp);
1294 if (isp->otg.gadget)
1295 return isp1301_otg_enable(isp);
1296 return 0;
1297
1298#elif !defined(CONFIG_USB_GADGET_OMAP)
1299 // FIXME update its refcount
1300 isp->otg.host = host;
1301
1302 power_up(isp);
1303
1304 if (machine_is_omap_h2())
1305 isp1301_set_bits(isp, ISP1301_MODE_CONTROL_1, MC1_DAT_SE0);
1306
1307 dev_info(&isp->client.dev, "A-Host sessions ok\n");
1308 isp1301_set_bits(isp, ISP1301_INTERRUPT_RISING,
1309 INTR_ID_GND);
1310 isp1301_set_bits(isp, ISP1301_INTERRUPT_FALLING,
1311 INTR_ID_GND);
1312
1313 /* If this has a Mini-AB connector, this mode is highly
1314 * nonstandard ... but can be handy for testing, especially with
1315 * the Mini-A end of an OTG cable. (Or something nonstandard
1316 * like MiniB-to-StandardB, maybe built with a gender mender.)
1317 */
1318 isp1301_set_bits(isp, ISP1301_OTG_CONTROL_1, OTG1_VBUS_DRV);
1319
1320 dump_regs(isp, __FUNCTION__);
1321
1322 return 0;
1323
1324#else
1325 dev_dbg(&isp->client.dev, "host sessions not allowed\n");
1326 return -EINVAL;
1327#endif
1328
1329}
1330
1331static int
1332isp1301_set_peripheral(struct otg_transceiver *otg, struct usb_gadget *gadget)
1333{
1334 struct isp1301 *isp = container_of(otg, struct isp1301, otg);
1335
1336 if (!otg || isp != the_transceiver)
1337 return -ENODEV;
1338
1339 if (!gadget) {
1340 OTG_IRQ_EN_REG = 0;
1341 if (!isp->otg.default_a)
1342 enable_vbus_draw(isp, 0);
1343 usb_gadget_vbus_disconnect(isp->otg.gadget);
1344 isp->otg.gadget = 0;
1345 power_down(isp);
1346 return 0;
1347 }
1348
1349#ifdef CONFIG_USB_OTG
1350 isp->otg.gadget = gadget;
1351 dev_dbg(&isp->client.dev, "registered gadget\n");
1352 /* gadget driver may be suspended until vbus_connect () */
1353 if (isp->otg.host)
1354 return isp1301_otg_enable(isp);
1355 return 0;
1356
1357#elif !defined(CONFIG_USB_OHCI_HCD) && !defined(CONFIG_USB_OHCI_HCD_MODULE)
1358 isp->otg.gadget = gadget;
1359 // FIXME update its refcount
1360
1361 OTG_CTRL_REG = (OTG_CTRL_REG & OTG_CTRL_MASK
1362 & ~(OTG_XCEIV_OUTPUTS|OTG_CTRL_BITS))
1363 | OTG_ID;
1364 power_up(isp);
1365 isp->otg.state = OTG_STATE_B_IDLE;
1366
1367 if (machine_is_omap_h2())
1368 isp1301_set_bits(isp, ISP1301_MODE_CONTROL_1, MC1_DAT_SE0);
1369
1370 isp1301_set_bits(isp, ISP1301_INTERRUPT_RISING,
1371 INTR_SESS_VLD);
1372 isp1301_set_bits(isp, ISP1301_INTERRUPT_FALLING,
1373 INTR_VBUS_VLD);
1374 dev_info(&isp->client.dev, "B-Peripheral sessions ok\n");
1375 dump_regs(isp, __FUNCTION__);
1376
1377 /* If this has a Mini-AB connector, this mode is highly
1378 * nonstandard ... but can be handy for testing, so long
1379 * as you don't plug a Mini-A cable into the jack.
1380 */
1381 if (isp1301_get_u8(isp, ISP1301_INTERRUPT_SOURCE) & INTR_VBUS_VLD)
1382 b_peripheral(isp);
1383
1384 return 0;
1385
1386#else
1387 dev_dbg(&isp->client.dev, "peripheral sessions not allowed\n");
1388 return -EINVAL;
1389#endif
1390}
1391
1392
1393/*-------------------------------------------------------------------------*/
1394
1395static int
1396isp1301_set_power(struct otg_transceiver *dev, unsigned mA)
1397{
1398 if (!the_transceiver)
1399 return -ENODEV;
1400 if (dev->state == OTG_STATE_B_PERIPHERAL)
1401 enable_vbus_draw(the_transceiver, mA);
1402 return 0;
1403}
1404
1405static int
1406isp1301_start_srp(struct otg_transceiver *dev)
1407{
1408 struct isp1301 *isp = container_of(dev, struct isp1301, otg);
1409 u32 otg_ctrl;
1410
1411 if (!dev || isp != the_transceiver
1412 || isp->otg.state != OTG_STATE_B_IDLE)
1413 return -ENODEV;
1414
1415 otg_ctrl = OTG_CTRL_REG;
1416 if (!(otg_ctrl & OTG_BSESSEND))
1417 return -EINVAL;
1418
1419 otg_ctrl |= OTG_B_BUSREQ;
1420 otg_ctrl &= ~OTG_A_BUSREQ & OTG_CTRL_MASK;
1421 OTG_CTRL_REG = otg_ctrl;
1422 isp->otg.state = OTG_STATE_B_SRP_INIT;
1423
1424 pr_debug("otg: SRP, %s ... %06x\n", state_name(isp), OTG_CTRL_REG);
1425#ifdef CONFIG_USB_OTG
1426 check_state(isp, __FUNCTION__);
1427#endif
1428 return 0;
1429}
1430
1431static int
1432isp1301_start_hnp(struct otg_transceiver *dev)
1433{
1434#ifdef CONFIG_USB_OTG
1435 struct isp1301 *isp = container_of(dev, struct isp1301, otg);
1436
1437 if (!dev || isp != the_transceiver)
1438 return -ENODEV;
1439 if (isp->otg.default_a && (isp->otg.host == NULL
1440 || !isp->otg.host->b_hnp_enable))
1441 return -ENOTCONN;
1442 if (!isp->otg.default_a && (isp->otg.gadget == NULL
1443 || !isp->otg.gadget->b_hnp_enable))
1444 return -ENOTCONN;
1445
1446 /* We want hardware to manage most HNP protocol timings.
1447 * So do this part as early as possible...
1448 */
1449 switch (isp->otg.state) {
1450 case OTG_STATE_B_HOST:
1451 isp->otg.state = OTG_STATE_B_PERIPHERAL;
1452 /* caller will suspend next */
1453 break;
1454 case OTG_STATE_A_HOST:
1455#if 0
1456 /* autoconnect mode avoids irq latency bugs */
1457 isp1301_set_bits(isp, ISP1301_MODE_CONTROL_1,
1458 MC1_BDIS_ACON_EN);
1459#endif
1460 /* caller must suspend then clear A_BUSREQ */
1461 usb_gadget_vbus_connect(isp->otg.gadget);
1462 OTG_CTRL_REG |= OTG_A_SETB_HNPEN;
1463
1464 break;
1465 case OTG_STATE_A_PERIPHERAL:
1466 /* initiated by B-Host suspend */
1467 break;
1468 default:
1469 return -EILSEQ;
1470 }
1471 pr_debug("otg: HNP %s, %06x ...\n",
1472 state_name(isp), OTG_CTRL_REG);
1473 check_state(isp, __FUNCTION__);
1474 return 0;
1475#else
1476 /* srp-only */
1477 return -EINVAL;
1478#endif
1479}
1480
1481/*-------------------------------------------------------------------------*/
1482
1483/* no error returns, they'd just make bus scanning stop */
1484static int isp1301_probe(struct i2c_adapter *bus, int address, int kind)
1485{
1486 int status;
1487 struct isp1301 *isp;
1488 struct i2c_client *i2c;
1489
1490 if (the_transceiver)
1491 return 0;
1492
1493 isp = kcalloc(1, sizeof *isp, GFP_KERNEL);
1494 if (!isp)
1495 return 0;
1496
1497 INIT_WORK(&isp->work, isp1301_work, isp);
1498 init_timer(&isp->timer);
1499 isp->timer.function = isp1301_timer;
1500 isp->timer.data = (unsigned long) isp;
1501
1502 isp->irq = -1;
1503 isp->client.addr = address;
1504 i2c_set_clientdata(&isp->client, isp);
1505 isp->client.adapter = bus;
1506 isp->client.driver = &isp1301_driver;
1507 strlcpy(isp->client.name, DRIVER_NAME, I2C_NAME_SIZE);
1508 i2c = &isp->client;
1509
1510 /* if this is a true probe, verify the chip ... */
1511 if (kind < 0) {
1512 status = isp1301_get_u16(isp, ISP1301_VENDOR_ID);
1513 if (status != I2C_VENDOR_ID_PHILIPS) {
1514 dev_dbg(&bus->dev, "addr %d not philips id: %d\n",
1515 address, status);
1516 goto fail1;
1517 }
1518 status = isp1301_get_u16(isp, ISP1301_PRODUCT_ID);
1519 if (status != I2C_PRODUCT_ID_PHILIPS_1301) {
1520 dev_dbg(&bus->dev, "%d not isp1301, %d\n",
1521 address, status);
1522 goto fail1;
1523 }
1524 }
1525
1526 status = i2c_attach_client(i2c);
1527 if (status < 0) {
1528 dev_dbg(&bus->dev, "can't attach %s to device %d, err %d\n",
1529 DRIVER_NAME, address, status);
1530fail1:
1531 kfree(isp);
1532 return 0;
1533 }
1534 isp->i2c_release = i2c->dev.release;
1535 i2c->dev.release = isp1301_release;
1536
1537 /* initial development used chiprev 2.00 */
1538 status = i2c_smbus_read_word_data(i2c, ISP1301_BCD_DEVICE);
1539 dev_info(&i2c->dev, "chiprev %x.%02x, driver " DRIVER_VERSION "\n",
1540 status >> 8, status & 0xff);
1541
1542 /* make like power-on reset */
1543 isp1301_clear_bits(isp, ISP1301_MODE_CONTROL_1, MC1_MASK);
1544
1545 isp1301_set_bits(isp, ISP1301_MODE_CONTROL_2, MC2_BI_DI);
1546 isp1301_clear_bits(isp, ISP1301_MODE_CONTROL_2, ~MC2_BI_DI);
1547
1548 isp1301_set_bits(isp, ISP1301_OTG_CONTROL_1,
1549 OTG1_DM_PULLDOWN | OTG1_DP_PULLDOWN);
1550 isp1301_clear_bits(isp, ISP1301_OTG_CONTROL_1,
1551 ~(OTG1_DM_PULLDOWN | OTG1_DP_PULLDOWN));
1552
1553 isp1301_clear_bits(isp, ISP1301_INTERRUPT_LATCH, ~0);
1554 isp1301_clear_bits(isp, ISP1301_INTERRUPT_FALLING, ~0);
1555 isp1301_clear_bits(isp, ISP1301_INTERRUPT_RISING, ~0);
1556
1557#ifdef CONFIG_USB_OTG
1558 status = otg_bind(isp);
1559 if (status < 0) {
1560 dev_dbg(&i2c->dev, "can't bind OTG\n");
1561 goto fail2;
1562 }
1563#endif
1564
1565 if (machine_is_omap_h2()) {
1566 /* full speed signaling by default */
1567 isp1301_set_bits(isp, ISP1301_MODE_CONTROL_1,
1568 MC1_SPEED_REG);
1569 isp1301_set_bits(isp, ISP1301_MODE_CONTROL_2,
1570 MC2_SPD_SUSP_CTRL);
1571
1572 /* IRQ wired at M14 */
1573 omap_cfg_reg(M14_1510_GPIO2);
1574 isp->irq = OMAP_GPIO_IRQ(2);
1575 omap_request_gpio(2);
1576 omap_set_gpio_direction(2, 1);
1577 omap_set_gpio_edge_ctrl(2, OMAP_GPIO_FALLING_EDGE);
1578 }
1579
1580 status = request_irq(isp->irq, isp1301_irq,
1581 SA_SAMPLE_RANDOM, DRIVER_NAME, isp);
1582 if (status < 0) {
1583 dev_dbg(&i2c->dev, "can't get IRQ %d, err %d\n",
1584 isp->irq, status);
1585#ifdef CONFIG_USB_OTG
1586fail2:
1587#endif
1588 i2c_detach_client(i2c);
1589 goto fail1;
1590 }
1591
1592 isp->otg.dev = &isp->client.dev;
1593 isp->otg.label = DRIVER_NAME;
1594
1595 isp->otg.set_host = isp1301_set_host,
1596 isp->otg.set_peripheral = isp1301_set_peripheral,
1597 isp->otg.set_power = isp1301_set_power,
1598 isp->otg.start_srp = isp1301_start_srp,
1599 isp->otg.start_hnp = isp1301_start_hnp,
1600
1601 enable_vbus_draw(isp, 0);
1602 power_down(isp);
1603 the_transceiver = isp;
1604
1605#ifdef CONFIG_USB_OTG
1606 update_otg1(isp, isp1301_get_u8(isp, ISP1301_INTERRUPT_SOURCE));
1607 update_otg2(isp, isp1301_get_u8(isp, ISP1301_OTG_STATUS));
1608#endif
1609
1610 dump_regs(isp, __FUNCTION__);
1611
1612#ifdef VERBOSE
1613 mod_timer(&isp->timer, jiffies + TIMER_JIFFIES);
1614 dev_dbg(&i2c->dev, "scheduled timer, %d min\n", TIMER_MINUTES);
1615#endif
1616
1617 status = otg_set_transceiver(&isp->otg);
1618 if (status < 0)
1619 dev_err(&i2c->dev, "can't register transceiver, %d\n",
1620 status);
1621
1622 return 0;
1623}
1624
1625static int isp1301_scan_bus(struct i2c_adapter *bus)
1626{
1627 if (!i2c_check_functionality(bus, I2C_FUNC_SMBUS_BYTE_DATA
1628 | I2C_FUNC_SMBUS_READ_WORD_DATA))
1629 return -EINVAL;
1630 return i2c_probe(bus, &addr_data, isp1301_probe);
1631}
1632
1633static struct i2c_driver isp1301_driver = {
1634 .owner = THIS_MODULE,
1635 .name = "isp1301_omap",
1636 .id = 1301, /* FIXME "official", i2c-ids.h */
1637 .class = I2C_CLASS_HWMON,
1638 .flags = I2C_DF_NOTIFY,
1639 .attach_adapter = isp1301_scan_bus,
1640 .detach_client = isp1301_detach_client,
1641};
1642
1643/*-------------------------------------------------------------------------*/
1644
1645static int __init isp_init(void)
1646{
1647 return i2c_add_driver(&isp1301_driver);
1648}
1649module_init(isp_init);
1650
1651static void __exit isp_exit(void)
1652{
1653 if (the_transceiver)
1654 otg_set_transceiver(0);
1655 i2c_del_driver(&isp1301_driver);
1656}
1657module_exit(isp_exit);
1658
diff --git a/drivers/i2c/chips/it87.c b/drivers/i2c/chips/it87.c
new file mode 100644
index 000000000000..3d484a7aff12
--- /dev/null
+++ b/drivers/i2c/chips/it87.c
@@ -0,0 +1,1208 @@
1/*
2 it87.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring.
4
5 Supports: IT8705F Super I/O chip w/LPC interface & SMBus
6 IT8712F Super I/O chip w/LPC interface & SMBus
7 Sis950 A clone of the IT8705F
8
9 Copyright (C) 2001 Chris Gauthron <chrisg@0-in.com>
10 Largely inspired by lm78.c of the same package
11
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25*/
26
27/*
28 djg@pdp8.net David Gesswein 7/18/01
29 Modified to fix bug with not all alarms enabled.
30 Added ability to read battery voltage and select temperature sensor
31 type at module load time.
32*/
33
34#include <linux/config.h>
35#include <linux/module.h>
36#include <linux/init.h>
37#include <linux/slab.h>
38#include <linux/jiffies.h>
39#include <linux/i2c.h>
40#include <linux/i2c-sensor.h>
41#include <linux/i2c-vid.h>
42#include <asm/io.h>
43
44
45/* Addresses to scan */
46static unsigned short normal_i2c[] = { 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d,
47 0x2e, 0x2f, I2C_CLIENT_END };
48static unsigned int normal_isa[] = { 0x0290, I2C_CLIENT_ISA_END };
49
50/* Insmod parameters */
51SENSORS_INSMOD_2(it87, it8712);
52
53#define REG 0x2e /* The register to read/write */
54#define DEV 0x07 /* Register: Logical device select */
55#define VAL 0x2f /* The value to read/write */
56#define PME 0x04 /* The device with the fan registers in it */
57#define DEVID 0x20 /* Register: Device ID */
58#define DEVREV 0x22 /* Register: Device Revision */
59
60static inline int
61superio_inb(int reg)
62{
63 outb(reg, REG);
64 return inb(VAL);
65}
66
67static int superio_inw(int reg)
68{
69 int val;
70 outb(reg++, REG);
71 val = inb(VAL) << 8;
72 outb(reg, REG);
73 val |= inb(VAL);
74 return val;
75}
76
77static inline void
78superio_select(void)
79{
80 outb(DEV, REG);
81 outb(PME, VAL);
82}
83
84static inline void
85superio_enter(void)
86{
87 outb(0x87, REG);
88 outb(0x01, REG);
89 outb(0x55, REG);
90 outb(0x55, REG);
91}
92
93static inline void
94superio_exit(void)
95{
96 outb(0x02, REG);
97 outb(0x02, VAL);
98}
99
100#define IT8712F_DEVID 0x8712
101#define IT8705F_DEVID 0x8705
102#define IT87_ACT_REG 0x30
103#define IT87_BASE_REG 0x60
104
105/* Update battery voltage after every reading if true */
106static int update_vbat;
107
108/* Not all BIOSes properly configure the PWM registers */
109static int fix_pwm_polarity;
110
111/* Chip Type */
112
113static u16 chip_type;
114
115/* Many IT87 constants specified below */
116
117/* Length of ISA address segment */
118#define IT87_EXTENT 8
119
120/* Where are the ISA address/data registers relative to the base address */
121#define IT87_ADDR_REG_OFFSET 5
122#define IT87_DATA_REG_OFFSET 6
123
124/*----- The IT87 registers -----*/
125
126#define IT87_REG_CONFIG 0x00
127
128#define IT87_REG_ALARM1 0x01
129#define IT87_REG_ALARM2 0x02
130#define IT87_REG_ALARM3 0x03
131
132#define IT87_REG_VID 0x0a
133#define IT87_REG_FAN_DIV 0x0b
134
135/* Monitors: 9 voltage (0 to 7, battery), 3 temp (1 to 3), 3 fan (1 to 3) */
136
137#define IT87_REG_FAN(nr) (0x0d + (nr))
138#define IT87_REG_FAN_MIN(nr) (0x10 + (nr))
139#define IT87_REG_FAN_MAIN_CTRL 0x13
140#define IT87_REG_FAN_CTL 0x14
141#define IT87_REG_PWM(nr) (0x15 + (nr))
142
143#define IT87_REG_VIN(nr) (0x20 + (nr))
144#define IT87_REG_TEMP(nr) (0x29 + (nr))
145
146#define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2)
147#define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2)
148#define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2)
149#define IT87_REG_TEMP_LOW(nr) (0x41 + (nr) * 2)
150
151#define IT87_REG_I2C_ADDR 0x48
152
153#define IT87_REG_VIN_ENABLE 0x50
154#define IT87_REG_TEMP_ENABLE 0x51
155
156#define IT87_REG_CHIPID 0x58
157
158#define IN_TO_REG(val) (SENSORS_LIMIT((((val) + 8)/16),0,255))
159#define IN_FROM_REG(val) ((val) * 16)
160
161static inline u8 FAN_TO_REG(long rpm, int div)
162{
163 if (rpm == 0)
164 return 255;
165 rpm = SENSORS_LIMIT(rpm, 1, 1000000);
166 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1,
167 254);
168}
169
170#define FAN_FROM_REG(val,div) ((val)==0?-1:(val)==255?0:1350000/((val)*(div)))
171
172#define TEMP_TO_REG(val) (SENSORS_LIMIT(((val)<0?(((val)-500)/1000):\
173 ((val)+500)/1000),-128,127))
174#define TEMP_FROM_REG(val) (((val)>0x80?(val)-0x100:(val))*1000)
175
176#define ALARMS_FROM_REG(val) (val)
177
178#define PWM_TO_REG(val) ((val) >> 1)
179#define PWM_FROM_REG(val) (((val)&0x7f) << 1)
180
181static int DIV_TO_REG(int val)
182{
183 int answer = 0;
184 while ((val >>= 1) != 0)
185 answer++;
186 return answer;
187}
188#define DIV_FROM_REG(val) (1 << (val))
189
190
191/* For each registered IT87, we need to keep some data in memory. That
192 data is pointed to by it87_list[NR]->data. The structure itself is
193 dynamically allocated, at the same time when a new it87 client is
194 allocated. */
195struct it87_data {
196 struct i2c_client client;
197 struct semaphore lock;
198 enum chips type;
199
200 struct semaphore update_lock;
201 char valid; /* !=0 if following fields are valid */
202 unsigned long last_updated; /* In jiffies */
203
204 u8 in[9]; /* Register value */
205 u8 in_max[9]; /* Register value */
206 u8 in_min[9]; /* Register value */
207 u8 fan[3]; /* Register value */
208 u8 fan_min[3]; /* Register value */
209 u8 temp[3]; /* Register value */
210 u8 temp_high[3]; /* Register value */
211 u8 temp_low[3]; /* Register value */
212 u8 sensor; /* Register value */
213 u8 fan_div[3]; /* Register encoding, shifted right */
214 u8 vid; /* Register encoding, combined */
215 int vrm;
216 u32 alarms; /* Register encoding, combined */
217 u8 fan_main_ctrl; /* Register value */
218 u8 manual_pwm_ctl[3]; /* manual PWM value set by user */
219};
220
221
222static int it87_attach_adapter(struct i2c_adapter *adapter);
223static int it87_find(int *address);
224static int it87_detect(struct i2c_adapter *adapter, int address, int kind);
225static int it87_detach_client(struct i2c_client *client);
226
227static int it87_read_value(struct i2c_client *client, u8 register);
228static int it87_write_value(struct i2c_client *client, u8 register,
229 u8 value);
230static struct it87_data *it87_update_device(struct device *dev);
231static int it87_check_pwm(struct i2c_client *client);
232static void it87_init_client(struct i2c_client *client, struct it87_data *data);
233
234
235static struct i2c_driver it87_driver = {
236 .owner = THIS_MODULE,
237 .name = "it87",
238 .id = I2C_DRIVERID_IT87,
239 .flags = I2C_DF_NOTIFY,
240 .attach_adapter = it87_attach_adapter,
241 .detach_client = it87_detach_client,
242};
243
244static ssize_t show_in(struct device *dev, char *buf, int nr)
245{
246 struct it87_data *data = it87_update_device(dev);
247 return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr]));
248}
249
250static ssize_t show_in_min(struct device *dev, char *buf, int nr)
251{
252 struct it87_data *data = it87_update_device(dev);
253 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr]));
254}
255
256static ssize_t show_in_max(struct device *dev, char *buf, int nr)
257{
258 struct it87_data *data = it87_update_device(dev);
259 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr]));
260}
261
262static ssize_t set_in_min(struct device *dev, const char *buf,
263 size_t count, int nr)
264{
265 struct i2c_client *client = to_i2c_client(dev);
266 struct it87_data *data = i2c_get_clientdata(client);
267 unsigned long val = simple_strtoul(buf, NULL, 10);
268
269 down(&data->update_lock);
270 data->in_min[nr] = IN_TO_REG(val);
271 it87_write_value(client, IT87_REG_VIN_MIN(nr),
272 data->in_min[nr]);
273 up(&data->update_lock);
274 return count;
275}
276static ssize_t set_in_max(struct device *dev, const char *buf,
277 size_t count, int nr)
278{
279 struct i2c_client *client = to_i2c_client(dev);
280 struct it87_data *data = i2c_get_clientdata(client);
281 unsigned long val = simple_strtoul(buf, NULL, 10);
282
283 down(&data->update_lock);
284 data->in_max[nr] = IN_TO_REG(val);
285 it87_write_value(client, IT87_REG_VIN_MAX(nr),
286 data->in_max[nr]);
287 up(&data->update_lock);
288 return count;
289}
290
291#define show_in_offset(offset) \
292static ssize_t \
293 show_in##offset (struct device *dev, char *buf) \
294{ \
295 return show_in(dev, buf, offset); \
296} \
297static DEVICE_ATTR(in##offset##_input, S_IRUGO, show_in##offset, NULL);
298
299#define limit_in_offset(offset) \
300static ssize_t \
301 show_in##offset##_min (struct device *dev, char *buf) \
302{ \
303 return show_in_min(dev, buf, offset); \
304} \
305static ssize_t \
306 show_in##offset##_max (struct device *dev, char *buf) \
307{ \
308 return show_in_max(dev, buf, offset); \
309} \
310static ssize_t set_in##offset##_min (struct device *dev, \
311 const char *buf, size_t count) \
312{ \
313 return set_in_min(dev, buf, count, offset); \
314} \
315static ssize_t set_in##offset##_max (struct device *dev, \
316 const char *buf, size_t count) \
317{ \
318 return set_in_max(dev, buf, count, offset); \
319} \
320static DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
321 show_in##offset##_min, set_in##offset##_min); \
322static DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
323 show_in##offset##_max, set_in##offset##_max);
324
325show_in_offset(0);
326limit_in_offset(0);
327show_in_offset(1);
328limit_in_offset(1);
329show_in_offset(2);
330limit_in_offset(2);
331show_in_offset(3);
332limit_in_offset(3);
333show_in_offset(4);
334limit_in_offset(4);
335show_in_offset(5);
336limit_in_offset(5);
337show_in_offset(6);
338limit_in_offset(6);
339show_in_offset(7);
340limit_in_offset(7);
341show_in_offset(8);
342
343/* 3 temperatures */
344static ssize_t show_temp(struct device *dev, char *buf, int nr)
345{
346 struct it87_data *data = it87_update_device(dev);
347 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
348}
349static ssize_t show_temp_max(struct device *dev, char *buf, int nr)
350{
351 struct it87_data *data = it87_update_device(dev);
352 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[nr]));
353}
354static ssize_t show_temp_min(struct device *dev, char *buf, int nr)
355{
356 struct it87_data *data = it87_update_device(dev);
357 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_low[nr]));
358}
359static ssize_t set_temp_max(struct device *dev, const char *buf,
360 size_t count, int nr)
361{
362 struct i2c_client *client = to_i2c_client(dev);
363 struct it87_data *data = i2c_get_clientdata(client);
364 int val = simple_strtol(buf, NULL, 10);
365
366 down(&data->update_lock);
367 data->temp_high[nr] = TEMP_TO_REG(val);
368 it87_write_value(client, IT87_REG_TEMP_HIGH(nr), data->temp_high[nr]);
369 up(&data->update_lock);
370 return count;
371}
372static ssize_t set_temp_min(struct device *dev, const char *buf,
373 size_t count, int nr)
374{
375 struct i2c_client *client = to_i2c_client(dev);
376 struct it87_data *data = i2c_get_clientdata(client);
377 int val = simple_strtol(buf, NULL, 10);
378
379 down(&data->update_lock);
380 data->temp_low[nr] = TEMP_TO_REG(val);
381 it87_write_value(client, IT87_REG_TEMP_LOW(nr), data->temp_low[nr]);
382 up(&data->update_lock);
383 return count;
384}
385#define show_temp_offset(offset) \
386static ssize_t show_temp_##offset (struct device *dev, char *buf) \
387{ \
388 return show_temp(dev, buf, offset - 1); \
389} \
390static ssize_t \
391show_temp_##offset##_max (struct device *dev, char *buf) \
392{ \
393 return show_temp_max(dev, buf, offset - 1); \
394} \
395static ssize_t \
396show_temp_##offset##_min (struct device *dev, char *buf) \
397{ \
398 return show_temp_min(dev, buf, offset - 1); \
399} \
400static ssize_t set_temp_##offset##_max (struct device *dev, \
401 const char *buf, size_t count) \
402{ \
403 return set_temp_max(dev, buf, count, offset - 1); \
404} \
405static ssize_t set_temp_##offset##_min (struct device *dev, \
406 const char *buf, size_t count) \
407{ \
408 return set_temp_min(dev, buf, count, offset - 1); \
409} \
410static DEVICE_ATTR(temp##offset##_input, S_IRUGO, show_temp_##offset, NULL); \
411static DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
412 show_temp_##offset##_max, set_temp_##offset##_max); \
413static DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
414 show_temp_##offset##_min, set_temp_##offset##_min);
415
416show_temp_offset(1);
417show_temp_offset(2);
418show_temp_offset(3);
419
420static ssize_t show_sensor(struct device *dev, char *buf, int nr)
421{
422 struct it87_data *data = it87_update_device(dev);
423 u8 reg = data->sensor; /* In case the value is updated while we use it */
424
425 if (reg & (1 << nr))
426 return sprintf(buf, "3\n"); /* thermal diode */
427 if (reg & (8 << nr))
428 return sprintf(buf, "2\n"); /* thermistor */
429 return sprintf(buf, "0\n"); /* disabled */
430}
431static ssize_t set_sensor(struct device *dev, const char *buf,
432 size_t count, int nr)
433{
434 struct i2c_client *client = to_i2c_client(dev);
435 struct it87_data *data = i2c_get_clientdata(client);
436 int val = simple_strtol(buf, NULL, 10);
437
438 down(&data->update_lock);
439
440 data->sensor &= ~(1 << nr);
441 data->sensor &= ~(8 << nr);
442 /* 3 = thermal diode; 2 = thermistor; 0 = disabled */
443 if (val == 3)
444 data->sensor |= 1 << nr;
445 else if (val == 2)
446 data->sensor |= 8 << nr;
447 else if (val != 0) {
448 up(&data->update_lock);
449 return -EINVAL;
450 }
451 it87_write_value(client, IT87_REG_TEMP_ENABLE, data->sensor);
452 up(&data->update_lock);
453 return count;
454}
455#define show_sensor_offset(offset) \
456static ssize_t show_sensor_##offset (struct device *dev, char *buf) \
457{ \
458 return show_sensor(dev, buf, offset - 1); \
459} \
460static ssize_t set_sensor_##offset (struct device *dev, \
461 const char *buf, size_t count) \
462{ \
463 return set_sensor(dev, buf, count, offset - 1); \
464} \
465static DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR, \
466 show_sensor_##offset, set_sensor_##offset);
467
468show_sensor_offset(1);
469show_sensor_offset(2);
470show_sensor_offset(3);
471
472/* 3 Fans */
473static ssize_t show_fan(struct device *dev, char *buf, int nr)
474{
475 struct it87_data *data = it87_update_device(dev);
476 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan[nr],
477 DIV_FROM_REG(data->fan_div[nr])));
478}
479static ssize_t show_fan_min(struct device *dev, char *buf, int nr)
480{
481 struct it87_data *data = it87_update_device(dev);
482 return sprintf(buf,"%d\n",
483 FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr])));
484}
485static ssize_t show_fan_div(struct device *dev, char *buf, int nr)
486{
487 struct it87_data *data = it87_update_device(dev);
488 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
489}
490static ssize_t show_pwm_enable(struct device *dev, char *buf, int nr)
491{
492 struct it87_data *data = it87_update_device(dev);
493 return sprintf(buf,"%d\n", (data->fan_main_ctrl & (1 << nr)) ? 1 : 0);
494}
495static ssize_t show_pwm(struct device *dev, char *buf, int nr)
496{
497 struct it87_data *data = it87_update_device(dev);
498 return sprintf(buf,"%d\n", data->manual_pwm_ctl[nr]);
499}
500static ssize_t set_fan_min(struct device *dev, const char *buf,
501 size_t count, int nr)
502{
503 struct i2c_client *client = to_i2c_client(dev);
504 struct it87_data *data = i2c_get_clientdata(client);
505 int val = simple_strtol(buf, NULL, 10);
506
507 down(&data->update_lock);
508 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
509 it87_write_value(client, IT87_REG_FAN_MIN(nr), data->fan_min[nr]);
510 up(&data->update_lock);
511 return count;
512}
513static ssize_t set_fan_div(struct device *dev, const char *buf,
514 size_t count, int nr)
515{
516 struct i2c_client *client = to_i2c_client(dev);
517 struct it87_data *data = i2c_get_clientdata(client);
518 int val = simple_strtol(buf, NULL, 10);
519 int i, min[3];
520 u8 old;
521
522 down(&data->update_lock);
523 old = it87_read_value(client, IT87_REG_FAN_DIV);
524
525 for (i = 0; i < 3; i++)
526 min[i] = FAN_FROM_REG(data->fan_min[i], DIV_FROM_REG(data->fan_div[i]));
527
528 switch (nr) {
529 case 0:
530 case 1:
531 data->fan_div[nr] = DIV_TO_REG(val);
532 break;
533 case 2:
534 if (val < 8)
535 data->fan_div[nr] = 1;
536 else
537 data->fan_div[nr] = 3;
538 }
539 val = old & 0x80;
540 val |= (data->fan_div[0] & 0x07);
541 val |= (data->fan_div[1] & 0x07) << 3;
542 if (data->fan_div[2] == 3)
543 val |= 0x1 << 6;
544 it87_write_value(client, IT87_REG_FAN_DIV, val);
545
546 for (i = 0; i < 3; i++) {
547 data->fan_min[i]=FAN_TO_REG(min[i], DIV_FROM_REG(data->fan_div[i]));
548 it87_write_value(client, IT87_REG_FAN_MIN(i), data->fan_min[i]);
549 }
550 up(&data->update_lock);
551 return count;
552}
553static ssize_t set_pwm_enable(struct device *dev, const char *buf,
554 size_t count, int nr)
555{
556 struct i2c_client *client = to_i2c_client(dev);
557 struct it87_data *data = i2c_get_clientdata(client);
558 int val = simple_strtol(buf, NULL, 10);
559
560 down(&data->update_lock);
561
562 if (val == 0) {
563 int tmp;
564 /* make sure the fan is on when in on/off mode */
565 tmp = it87_read_value(client, IT87_REG_FAN_CTL);
566 it87_write_value(client, IT87_REG_FAN_CTL, tmp | (1 << nr));
567 /* set on/off mode */
568 data->fan_main_ctrl &= ~(1 << nr);
569 it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
570 } else if (val == 1) {
571 /* set SmartGuardian mode */
572 data->fan_main_ctrl |= (1 << nr);
573 it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
574 /* set saved pwm value, clear FAN_CTLX PWM mode bit */
575 it87_write_value(client, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr]));
576 } else {
577 up(&data->update_lock);
578 return -EINVAL;
579 }
580
581 up(&data->update_lock);
582 return count;
583}
584static ssize_t set_pwm(struct device *dev, const char *buf,
585 size_t count, int nr)
586{
587 struct i2c_client *client = to_i2c_client(dev);
588 struct it87_data *data = i2c_get_clientdata(client);
589 int val = simple_strtol(buf, NULL, 10);
590
591 if (val < 0 || val > 255)
592 return -EINVAL;
593
594 down(&data->update_lock);
595 data->manual_pwm_ctl[nr] = val;
596 if (data->fan_main_ctrl & (1 << nr))
597 it87_write_value(client, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr]));
598 up(&data->update_lock);
599 return count;
600}
601
602#define show_fan_offset(offset) \
603static ssize_t show_fan_##offset (struct device *dev, char *buf) \
604{ \
605 return show_fan(dev, buf, offset - 1); \
606} \
607static ssize_t show_fan_##offset##_min (struct device *dev, char *buf) \
608{ \
609 return show_fan_min(dev, buf, offset - 1); \
610} \
611static ssize_t show_fan_##offset##_div (struct device *dev, char *buf) \
612{ \
613 return show_fan_div(dev, buf, offset - 1); \
614} \
615static ssize_t set_fan_##offset##_min (struct device *dev, \
616 const char *buf, size_t count) \
617{ \
618 return set_fan_min(dev, buf, count, offset - 1); \
619} \
620static ssize_t set_fan_##offset##_div (struct device *dev, \
621 const char *buf, size_t count) \
622{ \
623 return set_fan_div(dev, buf, count, offset - 1); \
624} \
625static DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_fan_##offset, NULL); \
626static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
627 show_fan_##offset##_min, set_fan_##offset##_min); \
628static DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
629 show_fan_##offset##_div, set_fan_##offset##_div);
630
631show_fan_offset(1);
632show_fan_offset(2);
633show_fan_offset(3);
634
635#define show_pwm_offset(offset) \
636static ssize_t show_pwm##offset##_enable (struct device *dev, \
637 char *buf) \
638{ \
639 return show_pwm_enable(dev, buf, offset - 1); \
640} \
641static ssize_t show_pwm##offset (struct device *dev, char *buf) \
642{ \
643 return show_pwm(dev, buf, offset - 1); \
644} \
645static ssize_t set_pwm##offset##_enable (struct device *dev, \
646 const char *buf, size_t count) \
647{ \
648 return set_pwm_enable(dev, buf, count, offset - 1); \
649} \
650static ssize_t set_pwm##offset (struct device *dev, \
651 const char *buf, size_t count) \
652{ \
653 return set_pwm(dev, buf, count, offset - 1); \
654} \
655static DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
656 show_pwm##offset##_enable, \
657 set_pwm##offset##_enable); \
658static DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
659 show_pwm##offset , set_pwm##offset );
660
661show_pwm_offset(1);
662show_pwm_offset(2);
663show_pwm_offset(3);
664
665/* Alarms */
666static ssize_t show_alarms(struct device *dev, char *buf)
667{
668 struct it87_data *data = it87_update_device(dev);
669 return sprintf(buf,"%d\n", ALARMS_FROM_REG(data->alarms));
670}
671static DEVICE_ATTR(alarms, S_IRUGO | S_IWUSR, show_alarms, NULL);
672
673static ssize_t
674show_vrm_reg(struct device *dev, char *buf)
675{
676 struct it87_data *data = it87_update_device(dev);
677 return sprintf(buf, "%ld\n", (long) data->vrm);
678}
679static ssize_t
680store_vrm_reg(struct device *dev, const char *buf, size_t count)
681{
682 struct i2c_client *client = to_i2c_client(dev);
683 struct it87_data *data = i2c_get_clientdata(client);
684 u32 val;
685
686 val = simple_strtoul(buf, NULL, 10);
687 data->vrm = val;
688
689 return count;
690}
691static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
692#define device_create_file_vrm(client) \
693device_create_file(&client->dev, &dev_attr_vrm)
694
695static ssize_t
696show_vid_reg(struct device *dev, char *buf)
697{
698 struct it87_data *data = it87_update_device(dev);
699 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
700}
701static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
702#define device_create_file_vid(client) \
703device_create_file(&client->dev, &dev_attr_cpu0_vid)
704
705/* This function is called when:
706 * it87_driver is inserted (when this module is loaded), for each
707 available adapter
708 * when a new adapter is inserted (and it87_driver is still present) */
709static int it87_attach_adapter(struct i2c_adapter *adapter)
710{
711 if (!(adapter->class & I2C_CLASS_HWMON))
712 return 0;
713 return i2c_detect(adapter, &addr_data, it87_detect);
714}
715
716/* SuperIO detection - will change normal_isa[0] if a chip is found */
717static int it87_find(int *address)
718{
719 int err = -ENODEV;
720
721 superio_enter();
722 chip_type = superio_inw(DEVID);
723 if (chip_type != IT8712F_DEVID
724 && chip_type != IT8705F_DEVID)
725 goto exit;
726
727 superio_select();
728 if (!(superio_inb(IT87_ACT_REG) & 0x01)) {
729 pr_info("it87: Device not activated, skipping\n");
730 goto exit;
731 }
732
733 *address = superio_inw(IT87_BASE_REG) & ~(IT87_EXTENT - 1);
734 if (*address == 0) {
735 pr_info("it87: Base address not set, skipping\n");
736 goto exit;
737 }
738
739 err = 0;
740 pr_info("it87: Found IT%04xF chip at 0x%x, revision %d\n",
741 chip_type, *address, superio_inb(DEVREV) & 0x0f);
742
743exit:
744 superio_exit();
745 return err;
746}
747
748/* This function is called by i2c_detect */
749int it87_detect(struct i2c_adapter *adapter, int address, int kind)
750{
751 int i;
752 struct i2c_client *new_client;
753 struct it87_data *data;
754 int err = 0;
755 const char *name = "";
756 int is_isa = i2c_is_isa_adapter(adapter);
757 int enable_pwm_interface;
758
759 if (!is_isa &&
760 !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
761 goto ERROR0;
762
763 /* Reserve the ISA region */
764 if (is_isa)
765 if (!request_region(address, IT87_EXTENT, it87_driver.name))
766 goto ERROR0;
767
768 /* Probe whether there is anything available on this address. Already
769 done for SMBus and Super-I/O clients */
770 if (kind < 0) {
771 if (is_isa && !chip_type) {
772#define REALLY_SLOW_IO
773 /* We need the timeouts for at least some IT87-like chips. But only
774 if we read 'undefined' registers. */
775 i = inb_p(address + 1);
776 if (inb_p(address + 2) != i
777 || inb_p(address + 3) != i
778 || inb_p(address + 7) != i) {
779 err = -ENODEV;
780 goto ERROR1;
781 }
782#undef REALLY_SLOW_IO
783
784 /* Let's just hope nothing breaks here */
785 i = inb_p(address + 5) & 0x7f;
786 outb_p(~i & 0x7f, address + 5);
787 if ((inb_p(address + 5) & 0x7f) != (~i & 0x7f)) {
788 outb_p(i, address + 5);
789 err = -ENODEV;
790 goto ERROR1;
791 }
792 }
793 }
794
795 /* OK. For now, we presume we have a valid client. We now create the
796 client structure, even though we cannot fill it completely yet.
797 But it allows us to access it87_{read,write}_value. */
798
799 if (!(data = kmalloc(sizeof(struct it87_data), GFP_KERNEL))) {
800 err = -ENOMEM;
801 goto ERROR1;
802 }
803 memset(data, 0, sizeof(struct it87_data));
804
805 new_client = &data->client;
806 if (is_isa)
807 init_MUTEX(&data->lock);
808 i2c_set_clientdata(new_client, data);
809 new_client->addr = address;
810 new_client->adapter = adapter;
811 new_client->driver = &it87_driver;
812 new_client->flags = 0;
813
814 /* Now, we do the remaining detection. */
815
816 if (kind < 0) {
817 if ((it87_read_value(new_client, IT87_REG_CONFIG) & 0x80)
818 || (!is_isa
819 && it87_read_value(new_client, IT87_REG_I2C_ADDR) != address)) {
820 err = -ENODEV;
821 goto ERROR2;
822 }
823 }
824
825 /* Determine the chip type. */
826 if (kind <= 0) {
827 i = it87_read_value(new_client, IT87_REG_CHIPID);
828 if (i == 0x90) {
829 kind = it87;
830 if ((is_isa) && (chip_type == IT8712F_DEVID))
831 kind = it8712;
832 }
833 else {
834 if (kind == 0)
835 dev_info(&adapter->dev,
836 "Ignoring 'force' parameter for unknown chip at "
837 "adapter %d, address 0x%02x\n",
838 i2c_adapter_id(adapter), address);
839 err = -ENODEV;
840 goto ERROR2;
841 }
842 }
843
844 if (kind == it87) {
845 name = "it87";
846 } else if (kind == it8712) {
847 name = "it8712";
848 }
849
850 /* Fill in the remaining client fields and put it into the global list */
851 strlcpy(new_client->name, name, I2C_NAME_SIZE);
852 data->type = kind;
853 data->valid = 0;
854 init_MUTEX(&data->update_lock);
855
856 /* Tell the I2C layer a new client has arrived */
857 if ((err = i2c_attach_client(new_client)))
858 goto ERROR2;
859
860 /* Check PWM configuration */
861 enable_pwm_interface = it87_check_pwm(new_client);
862
863 /* Initialize the IT87 chip */
864 it87_init_client(new_client, data);
865
866 /* Register sysfs hooks */
867 device_create_file(&new_client->dev, &dev_attr_in0_input);
868 device_create_file(&new_client->dev, &dev_attr_in1_input);
869 device_create_file(&new_client->dev, &dev_attr_in2_input);
870 device_create_file(&new_client->dev, &dev_attr_in3_input);
871 device_create_file(&new_client->dev, &dev_attr_in4_input);
872 device_create_file(&new_client->dev, &dev_attr_in5_input);
873 device_create_file(&new_client->dev, &dev_attr_in6_input);
874 device_create_file(&new_client->dev, &dev_attr_in7_input);
875 device_create_file(&new_client->dev, &dev_attr_in8_input);
876 device_create_file(&new_client->dev, &dev_attr_in0_min);
877 device_create_file(&new_client->dev, &dev_attr_in1_min);
878 device_create_file(&new_client->dev, &dev_attr_in2_min);
879 device_create_file(&new_client->dev, &dev_attr_in3_min);
880 device_create_file(&new_client->dev, &dev_attr_in4_min);
881 device_create_file(&new_client->dev, &dev_attr_in5_min);
882 device_create_file(&new_client->dev, &dev_attr_in6_min);
883 device_create_file(&new_client->dev, &dev_attr_in7_min);
884 device_create_file(&new_client->dev, &dev_attr_in0_max);
885 device_create_file(&new_client->dev, &dev_attr_in1_max);
886 device_create_file(&new_client->dev, &dev_attr_in2_max);
887 device_create_file(&new_client->dev, &dev_attr_in3_max);
888 device_create_file(&new_client->dev, &dev_attr_in4_max);
889 device_create_file(&new_client->dev, &dev_attr_in5_max);
890 device_create_file(&new_client->dev, &dev_attr_in6_max);
891 device_create_file(&new_client->dev, &dev_attr_in7_max);
892 device_create_file(&new_client->dev, &dev_attr_temp1_input);
893 device_create_file(&new_client->dev, &dev_attr_temp2_input);
894 device_create_file(&new_client->dev, &dev_attr_temp3_input);
895 device_create_file(&new_client->dev, &dev_attr_temp1_max);
896 device_create_file(&new_client->dev, &dev_attr_temp2_max);
897 device_create_file(&new_client->dev, &dev_attr_temp3_max);
898 device_create_file(&new_client->dev, &dev_attr_temp1_min);
899 device_create_file(&new_client->dev, &dev_attr_temp2_min);
900 device_create_file(&new_client->dev, &dev_attr_temp3_min);
901 device_create_file(&new_client->dev, &dev_attr_temp1_type);
902 device_create_file(&new_client->dev, &dev_attr_temp2_type);
903 device_create_file(&new_client->dev, &dev_attr_temp3_type);
904 device_create_file(&new_client->dev, &dev_attr_fan1_input);
905 device_create_file(&new_client->dev, &dev_attr_fan2_input);
906 device_create_file(&new_client->dev, &dev_attr_fan3_input);
907 device_create_file(&new_client->dev, &dev_attr_fan1_min);
908 device_create_file(&new_client->dev, &dev_attr_fan2_min);
909 device_create_file(&new_client->dev, &dev_attr_fan3_min);
910 device_create_file(&new_client->dev, &dev_attr_fan1_div);
911 device_create_file(&new_client->dev, &dev_attr_fan2_div);
912 device_create_file(&new_client->dev, &dev_attr_fan3_div);
913 device_create_file(&new_client->dev, &dev_attr_alarms);
914 if (enable_pwm_interface) {
915 device_create_file(&new_client->dev, &dev_attr_pwm1_enable);
916 device_create_file(&new_client->dev, &dev_attr_pwm2_enable);
917 device_create_file(&new_client->dev, &dev_attr_pwm3_enable);
918 device_create_file(&new_client->dev, &dev_attr_pwm1);
919 device_create_file(&new_client->dev, &dev_attr_pwm2);
920 device_create_file(&new_client->dev, &dev_attr_pwm3);
921 }
922
923 if (data->type == it8712) {
924 data->vrm = i2c_which_vrm();
925 device_create_file_vrm(new_client);
926 device_create_file_vid(new_client);
927 }
928
929 return 0;
930
931ERROR2:
932 kfree(data);
933ERROR1:
934 if (is_isa)
935 release_region(address, IT87_EXTENT);
936ERROR0:
937 return err;
938}
939
940static int it87_detach_client(struct i2c_client *client)
941{
942 int err;
943
944 if ((err = i2c_detach_client(client))) {
945 dev_err(&client->dev,
946 "Client deregistration failed, client not detached.\n");
947 return err;
948 }
949
950 if(i2c_is_isa_client(client))
951 release_region(client->addr, IT87_EXTENT);
952 kfree(i2c_get_clientdata(client));
953
954 return 0;
955}
956
957/* The SMBus locks itself, but ISA access must be locked explicitely!
958 We don't want to lock the whole ISA bus, so we lock each client
959 separately.
960 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
961 would slow down the IT87 access and should not be necessary. */
962static int it87_read_value(struct i2c_client *client, u8 reg)
963{
964 struct it87_data *data = i2c_get_clientdata(client);
965
966 int res;
967 if (i2c_is_isa_client(client)) {
968 down(&data->lock);
969 outb_p(reg, client->addr + IT87_ADDR_REG_OFFSET);
970 res = inb_p(client->addr + IT87_DATA_REG_OFFSET);
971 up(&data->lock);
972 return res;
973 } else
974 return i2c_smbus_read_byte_data(client, reg);
975}
976
977/* The SMBus locks itself, but ISA access muse be locked explicitely!
978 We don't want to lock the whole ISA bus, so we lock each client
979 separately.
980 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
981 would slow down the IT87 access and should not be necessary. */
982static int it87_write_value(struct i2c_client *client, u8 reg, u8 value)
983{
984 struct it87_data *data = i2c_get_clientdata(client);
985
986 if (i2c_is_isa_client(client)) {
987 down(&data->lock);
988 outb_p(reg, client->addr + IT87_ADDR_REG_OFFSET);
989 outb_p(value, client->addr + IT87_DATA_REG_OFFSET);
990 up(&data->lock);
991 return 0;
992 } else
993 return i2c_smbus_write_byte_data(client, reg, value);
994}
995
996/* Return 1 if and only if the PWM interface is safe to use */
997static int it87_check_pwm(struct i2c_client *client)
998{
999 /* Some BIOSes fail to correctly configure the IT87 fans. All fans off
1000 * and polarity set to active low is sign that this is the case so we
1001 * disable pwm control to protect the user. */
1002 int tmp = it87_read_value(client, IT87_REG_FAN_CTL);
1003 if ((tmp & 0x87) == 0) {
1004 if (fix_pwm_polarity) {
1005 /* The user asks us to attempt a chip reconfiguration.
1006 * This means switching to active high polarity and
1007 * inverting all fan speed values. */
1008 int i;
1009 u8 pwm[3];
1010
1011 for (i = 0; i < 3; i++)
1012 pwm[i] = it87_read_value(client,
1013 IT87_REG_PWM(i));
1014
1015 /* If any fan is in automatic pwm mode, the polarity
1016 * might be correct, as suspicious as it seems, so we
1017 * better don't change anything (but still disable the
1018 * PWM interface). */
1019 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
1020 dev_info(&client->dev, "Reconfiguring PWM to "
1021 "active high polarity\n");
1022 it87_write_value(client, IT87_REG_FAN_CTL,
1023 tmp | 0x87);
1024 for (i = 0; i < 3; i++)
1025 it87_write_value(client,
1026 IT87_REG_PWM(i),
1027 0x7f & ~pwm[i]);
1028 return 1;
1029 }
1030
1031 dev_info(&client->dev, "PWM configuration is "
1032 "too broken to be fixed\n");
1033 }
1034
1035 dev_info(&client->dev, "Detected broken BIOS "
1036 "defaults, disabling PWM interface\n");
1037 return 0;
1038 } else if (fix_pwm_polarity) {
1039 dev_info(&client->dev, "PWM configuration looks "
1040 "sane, won't touch\n");
1041 }
1042
1043 return 1;
1044}
1045
1046/* Called when we have found a new IT87. */
1047static void it87_init_client(struct i2c_client *client, struct it87_data *data)
1048{
1049 int tmp, i;
1050
1051 /* initialize to sane defaults:
1052 * - if the chip is in manual pwm mode, this will be overwritten with
1053 * the actual settings on the chip (so in this case, initialization
1054 * is not needed)
1055 * - if in automatic or on/off mode, we could switch to manual mode,
1056 * read the registers and set manual_pwm_ctl accordingly, but currently
1057 * this is not implemented, so we initialize to something sane */
1058 for (i = 0; i < 3; i++) {
1059 data->manual_pwm_ctl[i] = 0xff;
1060 }
1061
1062 /* Check if temperature channnels are reset manually or by some reason */
1063 tmp = it87_read_value(client, IT87_REG_TEMP_ENABLE);
1064 if ((tmp & 0x3f) == 0) {
1065 /* Temp1,Temp3=thermistor; Temp2=thermal diode */
1066 tmp = (tmp & 0xc0) | 0x2a;
1067 it87_write_value(client, IT87_REG_TEMP_ENABLE, tmp);
1068 }
1069 data->sensor = tmp;
1070
1071 /* Check if voltage monitors are reset manually or by some reason */
1072 tmp = it87_read_value(client, IT87_REG_VIN_ENABLE);
1073 if ((tmp & 0xff) == 0) {
1074 /* Enable all voltage monitors */
1075 it87_write_value(client, IT87_REG_VIN_ENABLE, 0xff);
1076 }
1077
1078 /* Check if tachometers are reset manually or by some reason */
1079 data->fan_main_ctrl = it87_read_value(client, IT87_REG_FAN_MAIN_CTRL);
1080 if ((data->fan_main_ctrl & 0x70) == 0) {
1081 /* Enable all fan tachometers */
1082 data->fan_main_ctrl |= 0x70;
1083 it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
1084 }
1085
1086 /* Set current fan mode registers and the default settings for the
1087 * other mode registers */
1088 for (i = 0; i < 3; i++) {
1089 if (data->fan_main_ctrl & (1 << i)) {
1090 /* pwm mode */
1091 tmp = it87_read_value(client, IT87_REG_PWM(i));
1092 if (tmp & 0x80) {
1093 /* automatic pwm - not yet implemented, but
1094 * leave the settings made by the BIOS alone
1095 * until a change is requested via the sysfs
1096 * interface */
1097 } else {
1098 /* manual pwm */
1099 data->manual_pwm_ctl[i] = PWM_FROM_REG(tmp);
1100 }
1101 }
1102 }
1103
1104 /* Start monitoring */
1105 it87_write_value(client, IT87_REG_CONFIG,
1106 (it87_read_value(client, IT87_REG_CONFIG) & 0x36)
1107 | (update_vbat ? 0x41 : 0x01));
1108}
1109
1110static struct it87_data *it87_update_device(struct device *dev)
1111{
1112 struct i2c_client *client = to_i2c_client(dev);
1113 struct it87_data *data = i2c_get_clientdata(client);
1114 int i;
1115
1116 down(&data->update_lock);
1117
1118 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
1119 || !data->valid) {
1120
1121 if (update_vbat) {
1122 /* Cleared after each update, so reenable. Value
1123 returned by this read will be previous value */
1124 it87_write_value(client, IT87_REG_CONFIG,
1125 it87_read_value(client, IT87_REG_CONFIG) | 0x40);
1126 }
1127 for (i = 0; i <= 7; i++) {
1128 data->in[i] =
1129 it87_read_value(client, IT87_REG_VIN(i));
1130 data->in_min[i] =
1131 it87_read_value(client, IT87_REG_VIN_MIN(i));
1132 data->in_max[i] =
1133 it87_read_value(client, IT87_REG_VIN_MAX(i));
1134 }
1135 data->in[8] =
1136 it87_read_value(client, IT87_REG_VIN(8));
1137 /* Temperature sensor doesn't have limit registers, set
1138 to min and max value */
1139 data->in_min[8] = 0;
1140 data->in_max[8] = 255;
1141
1142 for (i = 0; i < 3; i++) {
1143 data->fan[i] =
1144 it87_read_value(client, IT87_REG_FAN(i));
1145 data->fan_min[i] =
1146 it87_read_value(client, IT87_REG_FAN_MIN(i));
1147 }
1148 for (i = 0; i < 3; i++) {
1149 data->temp[i] =
1150 it87_read_value(client, IT87_REG_TEMP(i));
1151 data->temp_high[i] =
1152 it87_read_value(client, IT87_REG_TEMP_HIGH(i));
1153 data->temp_low[i] =
1154 it87_read_value(client, IT87_REG_TEMP_LOW(i));
1155 }
1156
1157 i = it87_read_value(client, IT87_REG_FAN_DIV);
1158 data->fan_div[0] = i & 0x07;
1159 data->fan_div[1] = (i >> 3) & 0x07;
1160 data->fan_div[2] = (i & 0x40) ? 3 : 1;
1161
1162 data->alarms =
1163 it87_read_value(client, IT87_REG_ALARM1) |
1164 (it87_read_value(client, IT87_REG_ALARM2) << 8) |
1165 (it87_read_value(client, IT87_REG_ALARM3) << 16);
1166 data->fan_main_ctrl = it87_read_value(client, IT87_REG_FAN_MAIN_CTRL);
1167
1168 data->sensor = it87_read_value(client, IT87_REG_TEMP_ENABLE);
1169 /* The 8705 does not have VID capability */
1170 if (data->type == it8712) {
1171 data->vid = it87_read_value(client, IT87_REG_VID);
1172 data->vid &= 0x1f;
1173 }
1174 data->last_updated = jiffies;
1175 data->valid = 1;
1176 }
1177
1178 up(&data->update_lock);
1179
1180 return data;
1181}
1182
1183static int __init sm_it87_init(void)
1184{
1185 int addr;
1186
1187 if (!it87_find(&addr)) {
1188 normal_isa[0] = addr;
1189 }
1190 return i2c_add_driver(&it87_driver);
1191}
1192
1193static void __exit sm_it87_exit(void)
1194{
1195 i2c_del_driver(&it87_driver);
1196}
1197
1198
1199MODULE_AUTHOR("Chris Gauthron <chrisg@0-in.com>");
1200MODULE_DESCRIPTION("IT8705F, IT8712F, Sis950 driver");
1201module_param(update_vbat, bool, 0);
1202MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
1203module_param(fix_pwm_polarity, bool, 0);
1204MODULE_PARM_DESC(fix_pwm_polarity, "Force PWM polarity to active high (DANGEROUS)");
1205MODULE_LICENSE("GPL");
1206
1207module_init(sm_it87_init);
1208module_exit(sm_it87_exit);
diff --git a/drivers/i2c/chips/lm63.c b/drivers/i2c/chips/lm63.c
new file mode 100644
index 000000000000..14cc5af03739
--- /dev/null
+++ b/drivers/i2c/chips/lm63.c
@@ -0,0 +1,581 @@
1/*
2 * lm63.c - driver for the National Semiconductor LM63 temperature sensor
3 * with integrated fan control
4 * Copyright (C) 2004 Jean Delvare <khali@linux-fr.org>
5 * Based on the lm90 driver.
6 *
7 * The LM63 is a sensor chip made by National Semiconductor. It measures
8 * two temperatures (its own and one external one) and the speed of one
9 * fan, those speed it can additionally control. Complete datasheet can be
10 * obtained from National's website at:
11 * http://www.national.com/pf/LM/LM63.html
12 *
13 * The LM63 is basically an LM86 with fan speed monitoring and control
14 * capabilities added. It misses some of the LM86 features though:
15 * - No low limit for local temperature.
16 * - No critical limit for local temperature.
17 * - Critical limit for remote temperature can be changed only once. We
18 * will consider that the critical limit is read-only.
19 *
20 * The datasheet isn't very clear about what the tachometer reading is.
21 * I had a explanation from National Semiconductor though. The two lower
22 * bits of the read value have to be masked out. The value is still 16 bit
23 * in width.
24 *
25 * This program is free software; you can redistribute it and/or modify
26 * it under the terms of the GNU General Public License as published by
27 * the Free Software Foundation; either version 2 of the License, or
28 * (at your option) any later version.
29 *
30 * This program is distributed in the hope that it will be useful,
31 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 * GNU General Public License for more details.
34 *
35 * You should have received a copy of the GNU General Public License
36 * along with this program; if not, write to the Free Software
37 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
38 */
39
40#include <linux/config.h>
41#include <linux/module.h>
42#include <linux/init.h>
43#include <linux/slab.h>
44#include <linux/jiffies.h>
45#include <linux/i2c.h>
46#include <linux/i2c-sensor.h>
47
48/*
49 * Addresses to scan
50 * Address is fully defined internally and cannot be changed.
51 */
52
53static unsigned short normal_i2c[] = { 0x4c, I2C_CLIENT_END };
54static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
55
56/*
57 * Insmod parameters
58 */
59
60SENSORS_INSMOD_1(lm63);
61
62/*
63 * The LM63 registers
64 */
65
66#define LM63_REG_CONFIG1 0x03
67#define LM63_REG_CONFIG2 0xBF
68#define LM63_REG_CONFIG_FAN 0x4A
69
70#define LM63_REG_TACH_COUNT_MSB 0x47
71#define LM63_REG_TACH_COUNT_LSB 0x46
72#define LM63_REG_TACH_LIMIT_MSB 0x49
73#define LM63_REG_TACH_LIMIT_LSB 0x48
74
75#define LM63_REG_PWM_VALUE 0x4C
76#define LM63_REG_PWM_FREQ 0x4D
77
78#define LM63_REG_LOCAL_TEMP 0x00
79#define LM63_REG_LOCAL_HIGH 0x05
80
81#define LM63_REG_REMOTE_TEMP_MSB 0x01
82#define LM63_REG_REMOTE_TEMP_LSB 0x10
83#define LM63_REG_REMOTE_OFFSET_MSB 0x11
84#define LM63_REG_REMOTE_OFFSET_LSB 0x12
85#define LM63_REG_REMOTE_HIGH_MSB 0x07
86#define LM63_REG_REMOTE_HIGH_LSB 0x13
87#define LM63_REG_REMOTE_LOW_MSB 0x08
88#define LM63_REG_REMOTE_LOW_LSB 0x14
89#define LM63_REG_REMOTE_TCRIT 0x19
90#define LM63_REG_REMOTE_TCRIT_HYST 0x21
91
92#define LM63_REG_ALERT_STATUS 0x02
93#define LM63_REG_ALERT_MASK 0x16
94
95#define LM63_REG_MAN_ID 0xFE
96#define LM63_REG_CHIP_ID 0xFF
97
98/*
99 * Conversions and various macros
100 * For tachometer counts, the LM63 uses 16-bit values.
101 * For local temperature and high limit, remote critical limit and hysteresis
102 * value, it uses signed 8-bit values with LSB = 1 degree Celcius.
103 * For remote temperature, low and high limits, it uses signed 11-bit values
104 * with LSB = 0.125 degree Celcius, left-justified in 16-bit registers.
105 */
106
107#define FAN_FROM_REG(reg) ((reg) == 0xFFFC || (reg) == 0 ? 0 : \
108 5400000 / (reg))
109#define FAN_TO_REG(val) ((val) <= 82 ? 0xFFFC : \
110 (5400000 / (val)) & 0xFFFC)
111#define TEMP8_FROM_REG(reg) ((reg) * 1000)
112#define TEMP8_TO_REG(val) ((val) <= -128000 ? -128 : \
113 (val) >= 127000 ? 127 : \
114 (val) < 0 ? ((val) - 500) / 1000 : \
115 ((val) + 500) / 1000)
116#define TEMP11_FROM_REG(reg) ((reg) / 32 * 125)
117#define TEMP11_TO_REG(val) ((val) <= -128000 ? 0x8000 : \
118 (val) >= 127875 ? 0x7FE0 : \
119 (val) < 0 ? ((val) - 62) / 125 * 32 : \
120 ((val) + 62) / 125 * 32)
121#define HYST_TO_REG(val) ((val) <= 0 ? 0 : \
122 (val) >= 127000 ? 127 : \
123 ((val) + 500) / 1000)
124
125/*
126 * Functions declaration
127 */
128
129static int lm63_attach_adapter(struct i2c_adapter *adapter);
130static int lm63_detach_client(struct i2c_client *client);
131
132static struct lm63_data *lm63_update_device(struct device *dev);
133
134static int lm63_detect(struct i2c_adapter *adapter, int address, int kind);
135static void lm63_init_client(struct i2c_client *client);
136
137/*
138 * Driver data (common to all clients)
139 */
140
141static struct i2c_driver lm63_driver = {
142 .owner = THIS_MODULE,
143 .name = "lm63",
144 .flags = I2C_DF_NOTIFY,
145 .attach_adapter = lm63_attach_adapter,
146 .detach_client = lm63_detach_client,
147};
148
149/*
150 * Client data (each client gets its own)
151 */
152
153struct lm63_data {
154 struct i2c_client client;
155 struct semaphore update_lock;
156 char valid; /* zero until following fields are valid */
157 unsigned long last_updated; /* in jiffies */
158
159 /* registers values */
160 u8 config, config_fan;
161 u16 fan1_input;
162 u16 fan1_low;
163 u8 pwm1_freq;
164 u8 pwm1_value;
165 s8 temp1_input;
166 s8 temp1_high;
167 s16 temp2_input;
168 s16 temp2_high;
169 s16 temp2_low;
170 s8 temp2_crit;
171 u8 temp2_crit_hyst;
172 u8 alarms;
173};
174
175/*
176 * Sysfs callback functions and files
177 */
178
179#define show_fan(value) \
180static ssize_t show_##value(struct device *dev, char *buf) \
181{ \
182 struct lm63_data *data = lm63_update_device(dev); \
183 return sprintf(buf, "%d\n", FAN_FROM_REG(data->value)); \
184}
185show_fan(fan1_input);
186show_fan(fan1_low);
187
188static ssize_t set_fan1_low(struct device *dev, const char *buf,
189 size_t count)
190{
191 struct i2c_client *client = to_i2c_client(dev);
192 struct lm63_data *data = i2c_get_clientdata(client);
193 unsigned long val = simple_strtoul(buf, NULL, 10);
194
195 down(&data->update_lock);
196 data->fan1_low = FAN_TO_REG(val);
197 i2c_smbus_write_byte_data(client, LM63_REG_TACH_LIMIT_LSB,
198 data->fan1_low & 0xFF);
199 i2c_smbus_write_byte_data(client, LM63_REG_TACH_LIMIT_MSB,
200 data->fan1_low >> 8);
201 up(&data->update_lock);
202 return count;
203}
204
205static ssize_t show_pwm1(struct device *dev, char *buf)
206{
207 struct lm63_data *data = lm63_update_device(dev);
208 return sprintf(buf, "%d\n", data->pwm1_value >= 2 * data->pwm1_freq ?
209 255 : (data->pwm1_value * 255 + data->pwm1_freq) /
210 (2 * data->pwm1_freq));
211}
212
213static ssize_t set_pwm1(struct device *dev, const char *buf, size_t count)
214{
215 struct i2c_client *client = to_i2c_client(dev);
216 struct lm63_data *data = i2c_get_clientdata(client);
217 unsigned long val;
218
219 if (!(data->config_fan & 0x20)) /* register is read-only */
220 return -EPERM;
221
222 val = simple_strtoul(buf, NULL, 10);
223 down(&data->update_lock);
224 data->pwm1_value = val <= 0 ? 0 :
225 val >= 255 ? 2 * data->pwm1_freq :
226 (val * data->pwm1_freq * 2 + 127) / 255;
227 i2c_smbus_write_byte_data(client, LM63_REG_PWM_VALUE, data->pwm1_value);
228 up(&data->update_lock);
229 return count;
230}
231
232static ssize_t show_pwm1_enable(struct device *dev, char *buf)
233{
234 struct lm63_data *data = lm63_update_device(dev);
235 return sprintf(buf, "%d\n", data->config_fan & 0x20 ? 1 : 2);
236}
237
238#define show_temp8(value) \
239static ssize_t show_##value(struct device *dev, char *buf) \
240{ \
241 struct lm63_data *data = lm63_update_device(dev); \
242 return sprintf(buf, "%d\n", TEMP8_FROM_REG(data->value)); \
243}
244#define show_temp11(value) \
245static ssize_t show_##value(struct device *dev, char *buf) \
246{ \
247 struct lm63_data *data = lm63_update_device(dev); \
248 return sprintf(buf, "%d\n", TEMP11_FROM_REG(data->value)); \
249}
250show_temp8(temp1_input);
251show_temp8(temp1_high);
252show_temp11(temp2_input);
253show_temp11(temp2_high);
254show_temp11(temp2_low);
255show_temp8(temp2_crit);
256
257#define set_temp8(value, reg) \
258static ssize_t set_##value(struct device *dev, const char *buf, \
259 size_t count) \
260{ \
261 struct i2c_client *client = to_i2c_client(dev); \
262 struct lm63_data *data = i2c_get_clientdata(client); \
263 long val = simple_strtol(buf, NULL, 10); \
264 \
265 down(&data->update_lock); \
266 data->value = TEMP8_TO_REG(val); \
267 i2c_smbus_write_byte_data(client, reg, data->value); \
268 up(&data->update_lock); \
269 return count; \
270}
271#define set_temp11(value, reg_msb, reg_lsb) \
272static ssize_t set_##value(struct device *dev, const char *buf, \
273 size_t count) \
274{ \
275 struct i2c_client *client = to_i2c_client(dev); \
276 struct lm63_data *data = i2c_get_clientdata(client); \
277 long val = simple_strtol(buf, NULL, 10); \
278 \
279 down(&data->update_lock); \
280 data->value = TEMP11_TO_REG(val); \
281 i2c_smbus_write_byte_data(client, reg_msb, data->value >> 8); \
282 i2c_smbus_write_byte_data(client, reg_lsb, data->value & 0xff); \
283 up(&data->update_lock); \
284 return count; \
285}
286set_temp8(temp1_high, LM63_REG_LOCAL_HIGH);
287set_temp11(temp2_high, LM63_REG_REMOTE_HIGH_MSB, LM63_REG_REMOTE_HIGH_LSB);
288set_temp11(temp2_low, LM63_REG_REMOTE_LOW_MSB, LM63_REG_REMOTE_LOW_LSB);
289
290/* Hysteresis register holds a relative value, while we want to present
291 an absolute to user-space */
292static ssize_t show_temp2_crit_hyst(struct device *dev, char *buf)
293{
294 struct lm63_data *data = lm63_update_device(dev);
295 return sprintf(buf, "%d\n", TEMP8_FROM_REG(data->temp2_crit)
296 - TEMP8_FROM_REG(data->temp2_crit_hyst));
297}
298
299/* And now the other way around, user-space provides an absolute
300 hysteresis value and we have to store a relative one */
301static ssize_t set_temp2_crit_hyst(struct device *dev, const char *buf,
302 size_t count)
303{
304 struct i2c_client *client = to_i2c_client(dev);
305 struct lm63_data *data = i2c_get_clientdata(client);
306 long val = simple_strtol(buf, NULL, 10);
307 long hyst;
308
309 down(&data->update_lock);
310 hyst = TEMP8_FROM_REG(data->temp2_crit) - val;
311 i2c_smbus_write_byte_data(client, LM63_REG_REMOTE_TCRIT_HYST,
312 HYST_TO_REG(hyst));
313 up(&data->update_lock);
314 return count;
315}
316
317static ssize_t show_alarms(struct device *dev, char *buf)
318{
319 struct lm63_data *data = lm63_update_device(dev);
320 return sprintf(buf, "%u\n", data->alarms);
321}
322
323static DEVICE_ATTR(fan1_input, S_IRUGO, show_fan1_input, NULL);
324static DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan1_low,
325 set_fan1_low);
326
327static DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm1, set_pwm1);
328static DEVICE_ATTR(pwm1_enable, S_IRUGO, show_pwm1_enable, NULL);
329
330static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp1_input, NULL);
331static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp1_high,
332 set_temp1_high);
333
334static DEVICE_ATTR(temp2_input, S_IRUGO, show_temp2_input, NULL);
335static DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp2_low,
336 set_temp2_low);
337static DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp2_high,
338 set_temp2_high);
339static DEVICE_ATTR(temp2_crit, S_IRUGO, show_temp2_crit, NULL);
340static DEVICE_ATTR(temp2_crit_hyst, S_IWUSR | S_IRUGO, show_temp2_crit_hyst,
341 set_temp2_crit_hyst);
342
343static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
344
345/*
346 * Real code
347 */
348
349static int lm63_attach_adapter(struct i2c_adapter *adapter)
350{
351 if (!(adapter->class & I2C_CLASS_HWMON))
352 return 0;
353 return i2c_detect(adapter, &addr_data, lm63_detect);
354}
355
356/*
357 * The following function does more than just detection. If detection
358 * succeeds, it also registers the new chip.
359 */
360static int lm63_detect(struct i2c_adapter *adapter, int address, int kind)
361{
362 struct i2c_client *new_client;
363 struct lm63_data *data;
364 int err = 0;
365
366 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
367 goto exit;
368
369 if (!(data = kmalloc(sizeof(struct lm63_data), GFP_KERNEL))) {
370 err = -ENOMEM;
371 goto exit;
372 }
373 memset(data, 0, sizeof(struct lm63_data));
374
375 /* The common I2C client data is placed right before the
376 LM63-specific data. */
377 new_client = &data->client;
378 i2c_set_clientdata(new_client, data);
379 new_client->addr = address;
380 new_client->adapter = adapter;
381 new_client->driver = &lm63_driver;
382 new_client->flags = 0;
383
384 /* Default to an LM63 if forced */
385 if (kind == 0)
386 kind = lm63;
387
388 if (kind < 0) { /* must identify */
389 u8 man_id, chip_id, reg_config1, reg_config2;
390 u8 reg_alert_status, reg_alert_mask;
391
392 man_id = i2c_smbus_read_byte_data(new_client,
393 LM63_REG_MAN_ID);
394 chip_id = i2c_smbus_read_byte_data(new_client,
395 LM63_REG_CHIP_ID);
396 reg_config1 = i2c_smbus_read_byte_data(new_client,
397 LM63_REG_CONFIG1);
398 reg_config2 = i2c_smbus_read_byte_data(new_client,
399 LM63_REG_CONFIG2);
400 reg_alert_status = i2c_smbus_read_byte_data(new_client,
401 LM63_REG_ALERT_STATUS);
402 reg_alert_mask = i2c_smbus_read_byte_data(new_client,
403 LM63_REG_ALERT_MASK);
404
405 if (man_id == 0x01 /* National Semiconductor */
406 && chip_id == 0x41 /* LM63 */
407 && (reg_config1 & 0x18) == 0x00
408 && (reg_config2 & 0xF8) == 0x00
409 && (reg_alert_status & 0x20) == 0x00
410 && (reg_alert_mask & 0xA4) == 0xA4) {
411 kind = lm63;
412 } else { /* failed */
413 dev_dbg(&adapter->dev, "Unsupported chip "
414 "(man_id=0x%02X, chip_id=0x%02X).\n",
415 man_id, chip_id);
416 goto exit_free;
417 }
418 }
419
420 strlcpy(new_client->name, "lm63", I2C_NAME_SIZE);
421 data->valid = 0;
422 init_MUTEX(&data->update_lock);
423
424 /* Tell the I2C layer a new client has arrived */
425 if ((err = i2c_attach_client(new_client)))
426 goto exit_free;
427
428 /* Initialize the LM63 chip */
429 lm63_init_client(new_client);
430
431 /* Register sysfs hooks */
432 if (data->config & 0x04) { /* tachometer enabled */
433 device_create_file(&new_client->dev, &dev_attr_fan1_input);
434 device_create_file(&new_client->dev, &dev_attr_fan1_min);
435 }
436 device_create_file(&new_client->dev, &dev_attr_pwm1);
437 device_create_file(&new_client->dev, &dev_attr_pwm1_enable);
438 device_create_file(&new_client->dev, &dev_attr_temp1_input);
439 device_create_file(&new_client->dev, &dev_attr_temp2_input);
440 device_create_file(&new_client->dev, &dev_attr_temp2_min);
441 device_create_file(&new_client->dev, &dev_attr_temp1_max);
442 device_create_file(&new_client->dev, &dev_attr_temp2_max);
443 device_create_file(&new_client->dev, &dev_attr_temp2_crit);
444 device_create_file(&new_client->dev, &dev_attr_temp2_crit_hyst);
445 device_create_file(&new_client->dev, &dev_attr_alarms);
446
447 return 0;
448
449exit_free:
450 kfree(data);
451exit:
452 return err;
453}
454
455/* Idealy we shouldn't have to initialize anything, since the BIOS
456 should have taken care of everything */
457static void lm63_init_client(struct i2c_client *client)
458{
459 struct lm63_data *data = i2c_get_clientdata(client);
460
461 data->config = i2c_smbus_read_byte_data(client, LM63_REG_CONFIG1);
462 data->config_fan = i2c_smbus_read_byte_data(client,
463 LM63_REG_CONFIG_FAN);
464
465 /* Start converting if needed */
466 if (data->config & 0x40) { /* standby */
467 dev_dbg(&client->dev, "Switching to operational mode");
468 data->config &= 0xA7;
469 i2c_smbus_write_byte_data(client, LM63_REG_CONFIG1,
470 data->config);
471 }
472
473 /* We may need pwm1_freq before ever updating the client data */
474 data->pwm1_freq = i2c_smbus_read_byte_data(client, LM63_REG_PWM_FREQ);
475 if (data->pwm1_freq == 0)
476 data->pwm1_freq = 1;
477
478 /* Show some debug info about the LM63 configuration */
479 dev_dbg(&client->dev, "Alert/tach pin configured for %s\n",
480 (data->config & 0x04) ? "tachometer input" :
481 "alert output");
482 dev_dbg(&client->dev, "PWM clock %s kHz, output frequency %u Hz\n",
483 (data->config_fan & 0x08) ? "1.4" : "360",
484 ((data->config_fan & 0x08) ? 700 : 180000) / data->pwm1_freq);
485 dev_dbg(&client->dev, "PWM output active %s, %s mode\n",
486 (data->config_fan & 0x10) ? "low" : "high",
487 (data->config_fan & 0x20) ? "manual" : "auto");
488}
489
490static int lm63_detach_client(struct i2c_client *client)
491{
492 int err;
493
494 if ((err = i2c_detach_client(client))) {
495 dev_err(&client->dev, "Client deregistration failed, "
496 "client not detached\n");
497 return err;
498 }
499
500 kfree(i2c_get_clientdata(client));
501 return 0;
502}
503
504static struct lm63_data *lm63_update_device(struct device *dev)
505{
506 struct i2c_client *client = to_i2c_client(dev);
507 struct lm63_data *data = i2c_get_clientdata(client);
508
509 down(&data->update_lock);
510
511 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
512 if (data->config & 0x04) { /* tachometer enabled */
513 /* order matters for fan1_input */
514 data->fan1_input = i2c_smbus_read_byte_data(client,
515 LM63_REG_TACH_COUNT_LSB) & 0xFC;
516 data->fan1_input |= i2c_smbus_read_byte_data(client,
517 LM63_REG_TACH_COUNT_MSB) << 8;
518 data->fan1_low = (i2c_smbus_read_byte_data(client,
519 LM63_REG_TACH_LIMIT_LSB) & 0xFC)
520 | (i2c_smbus_read_byte_data(client,
521 LM63_REG_TACH_LIMIT_MSB) << 8);
522 }
523
524 data->pwm1_freq = i2c_smbus_read_byte_data(client,
525 LM63_REG_PWM_FREQ);
526 if (data->pwm1_freq == 0)
527 data->pwm1_freq = 1;
528 data->pwm1_value = i2c_smbus_read_byte_data(client,
529 LM63_REG_PWM_VALUE);
530
531 data->temp1_input = i2c_smbus_read_byte_data(client,
532 LM63_REG_LOCAL_TEMP);
533 data->temp1_high = i2c_smbus_read_byte_data(client,
534 LM63_REG_LOCAL_HIGH);
535
536 /* order matters for temp2_input */
537 data->temp2_input = i2c_smbus_read_byte_data(client,
538 LM63_REG_REMOTE_TEMP_MSB) << 8;
539 data->temp2_input |= i2c_smbus_read_byte_data(client,
540 LM63_REG_REMOTE_TEMP_LSB);
541 data->temp2_high = (i2c_smbus_read_byte_data(client,
542 LM63_REG_REMOTE_HIGH_MSB) << 8)
543 | i2c_smbus_read_byte_data(client,
544 LM63_REG_REMOTE_HIGH_LSB);
545 data->temp2_low = (i2c_smbus_read_byte_data(client,
546 LM63_REG_REMOTE_LOW_MSB) << 8)
547 | i2c_smbus_read_byte_data(client,
548 LM63_REG_REMOTE_LOW_LSB);
549 data->temp2_crit = i2c_smbus_read_byte_data(client,
550 LM63_REG_REMOTE_TCRIT);
551 data->temp2_crit_hyst = i2c_smbus_read_byte_data(client,
552 LM63_REG_REMOTE_TCRIT_HYST);
553
554 data->alarms = i2c_smbus_read_byte_data(client,
555 LM63_REG_ALERT_STATUS) & 0x7F;
556
557 data->last_updated = jiffies;
558 data->valid = 1;
559 }
560
561 up(&data->update_lock);
562
563 return data;
564}
565
566static int __init sensors_lm63_init(void)
567{
568 return i2c_add_driver(&lm63_driver);
569}
570
571static void __exit sensors_lm63_exit(void)
572{
573 i2c_del_driver(&lm63_driver);
574}
575
576MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
577MODULE_DESCRIPTION("LM63 driver");
578MODULE_LICENSE("GPL");
579
580module_init(sensors_lm63_init);
581module_exit(sensors_lm63_exit);
diff --git a/drivers/i2c/chips/lm75.c b/drivers/i2c/chips/lm75.c
new file mode 100644
index 000000000000..0e86cc893981
--- /dev/null
+++ b/drivers/i2c/chips/lm75.c
@@ -0,0 +1,297 @@
1/*
2 lm75.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring
4 Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19*/
20
21#include <linux/config.h>
22#include <linux/module.h>
23#include <linux/init.h>
24#include <linux/slab.h>
25#include <linux/jiffies.h>
26#include <linux/i2c.h>
27#include <linux/i2c-sensor.h>
28#include "lm75.h"
29
30
31/* Addresses to scan */
32static unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b, 0x4c,
33 0x4d, 0x4e, 0x4f, I2C_CLIENT_END };
34static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
35
36/* Insmod parameters */
37SENSORS_INSMOD_1(lm75);
38
39/* Many LM75 constants specified below */
40
41/* The LM75 registers */
42#define LM75_REG_TEMP 0x00
43#define LM75_REG_CONF 0x01
44#define LM75_REG_TEMP_HYST 0x02
45#define LM75_REG_TEMP_OS 0x03
46
47/* Each client has this additional data */
48struct lm75_data {
49 struct i2c_client client;
50 struct semaphore update_lock;
51 char valid; /* !=0 if following fields are valid */
52 unsigned long last_updated; /* In jiffies */
53 u16 temp_input; /* Register values */
54 u16 temp_max;
55 u16 temp_hyst;
56};
57
58static int lm75_attach_adapter(struct i2c_adapter *adapter);
59static int lm75_detect(struct i2c_adapter *adapter, int address, int kind);
60static void lm75_init_client(struct i2c_client *client);
61static int lm75_detach_client(struct i2c_client *client);
62static int lm75_read_value(struct i2c_client *client, u8 reg);
63static int lm75_write_value(struct i2c_client *client, u8 reg, u16 value);
64static struct lm75_data *lm75_update_device(struct device *dev);
65
66
67/* This is the driver that will be inserted */
68static struct i2c_driver lm75_driver = {
69 .owner = THIS_MODULE,
70 .name = "lm75",
71 .id = I2C_DRIVERID_LM75,
72 .flags = I2C_DF_NOTIFY,
73 .attach_adapter = lm75_attach_adapter,
74 .detach_client = lm75_detach_client,
75};
76
77#define show(value) \
78static ssize_t show_##value(struct device *dev, char *buf) \
79{ \
80 struct lm75_data *data = lm75_update_device(dev); \
81 return sprintf(buf, "%d\n", LM75_TEMP_FROM_REG(data->value)); \
82}
83show(temp_max);
84show(temp_hyst);
85show(temp_input);
86
87#define set(value, reg) \
88static ssize_t set_##value(struct device *dev, const char *buf, size_t count) \
89{ \
90 struct i2c_client *client = to_i2c_client(dev); \
91 struct lm75_data *data = i2c_get_clientdata(client); \
92 int temp = simple_strtoul(buf, NULL, 10); \
93 \
94 down(&data->update_lock); \
95 data->value = LM75_TEMP_TO_REG(temp); \
96 lm75_write_value(client, reg, data->value); \
97 up(&data->update_lock); \
98 return count; \
99}
100set(temp_max, LM75_REG_TEMP_OS);
101set(temp_hyst, LM75_REG_TEMP_HYST);
102
103static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max, set_temp_max);
104static DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO, show_temp_hyst, set_temp_hyst);
105static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input, NULL);
106
107static int lm75_attach_adapter(struct i2c_adapter *adapter)
108{
109 if (!(adapter->class & I2C_CLASS_HWMON))
110 return 0;
111 return i2c_detect(adapter, &addr_data, lm75_detect);
112}
113
114/* This function is called by i2c_detect */
115static int lm75_detect(struct i2c_adapter *adapter, int address, int kind)
116{
117 int i;
118 struct i2c_client *new_client;
119 struct lm75_data *data;
120 int err = 0;
121 const char *name = "";
122
123 /* Make sure we aren't probing the ISA bus!! This is just a safety check
124 at this moment; i2c_detect really won't call us. */
125#ifdef DEBUG
126 if (i2c_is_isa_adapter(adapter)) {
127 dev_dbg(&adapter->dev,
128 "lm75_detect called for an ISA bus adapter?!?\n");
129 goto exit;
130 }
131#endif
132
133 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
134 I2C_FUNC_SMBUS_WORD_DATA))
135 goto exit;
136
137 /* OK. For now, we presume we have a valid client. We now create the
138 client structure, even though we cannot fill it completely yet.
139 But it allows us to access lm75_{read,write}_value. */
140 if (!(data = kmalloc(sizeof(struct lm75_data), GFP_KERNEL))) {
141 err = -ENOMEM;
142 goto exit;
143 }
144 memset(data, 0, sizeof(struct lm75_data));
145
146 new_client = &data->client;
147 i2c_set_clientdata(new_client, data);
148 new_client->addr = address;
149 new_client->adapter = adapter;
150 new_client->driver = &lm75_driver;
151 new_client->flags = 0;
152
153 /* Now, we do the remaining detection. There is no identification-
154 dedicated register so we have to rely on several tricks:
155 unused bits, registers cycling over 8-address boundaries,
156 addresses 0x04-0x07 returning the last read value.
157 The cycling+unused addresses combination is not tested,
158 since it would significantly slow the detection down and would
159 hardly add any value. */
160 if (kind < 0) {
161 int cur, conf, hyst, os;
162
163 /* Unused addresses */
164 cur = i2c_smbus_read_word_data(new_client, 0);
165 conf = i2c_smbus_read_byte_data(new_client, 1);
166 hyst = i2c_smbus_read_word_data(new_client, 2);
167 if (i2c_smbus_read_word_data(new_client, 4) != hyst
168 || i2c_smbus_read_word_data(new_client, 5) != hyst
169 || i2c_smbus_read_word_data(new_client, 6) != hyst
170 || i2c_smbus_read_word_data(new_client, 7) != hyst)
171 goto exit_free;
172 os = i2c_smbus_read_word_data(new_client, 3);
173 if (i2c_smbus_read_word_data(new_client, 4) != os
174 || i2c_smbus_read_word_data(new_client, 5) != os
175 || i2c_smbus_read_word_data(new_client, 6) != os
176 || i2c_smbus_read_word_data(new_client, 7) != os)
177 goto exit_free;
178
179 /* Unused bits */
180 if (conf & 0xe0)
181 goto exit_free;
182
183 /* Addresses cycling */
184 for (i = 8; i < 0xff; i += 8)
185 if (i2c_smbus_read_byte_data(new_client, i + 1) != conf
186 || i2c_smbus_read_word_data(new_client, i + 2) != hyst
187 || i2c_smbus_read_word_data(new_client, i + 3) != os)
188 goto exit_free;
189 }
190
191 /* Determine the chip type - only one kind supported! */
192 if (kind <= 0)
193 kind = lm75;
194
195 if (kind == lm75) {
196 name = "lm75";
197 }
198
199 /* Fill in the remaining client fields and put it into the global list */
200 strlcpy(new_client->name, name, I2C_NAME_SIZE);
201 data->valid = 0;
202 init_MUTEX(&data->update_lock);
203
204 /* Tell the I2C layer a new client has arrived */
205 if ((err = i2c_attach_client(new_client)))
206 goto exit_free;
207
208 /* Initialize the LM75 chip */
209 lm75_init_client(new_client);
210
211 /* Register sysfs hooks */
212 device_create_file(&new_client->dev, &dev_attr_temp1_max);
213 device_create_file(&new_client->dev, &dev_attr_temp1_max_hyst);
214 device_create_file(&new_client->dev, &dev_attr_temp1_input);
215
216 return 0;
217
218exit_free:
219 kfree(data);
220exit:
221 return err;
222}
223
224static int lm75_detach_client(struct i2c_client *client)
225{
226 i2c_detach_client(client);
227 kfree(i2c_get_clientdata(client));
228 return 0;
229}
230
231/* All registers are word-sized, except for the configuration register.
232 LM75 uses a high-byte first convention, which is exactly opposite to
233 the usual practice. */
234static int lm75_read_value(struct i2c_client *client, u8 reg)
235{
236 if (reg == LM75_REG_CONF)
237 return i2c_smbus_read_byte_data(client, reg);
238 else
239 return swab16(i2c_smbus_read_word_data(client, reg));
240}
241
242/* All registers are word-sized, except for the configuration register.
243 LM75 uses a high-byte first convention, which is exactly opposite to
244 the usual practice. */
245static int lm75_write_value(struct i2c_client *client, u8 reg, u16 value)
246{
247 if (reg == LM75_REG_CONF)
248 return i2c_smbus_write_byte_data(client, reg, value);
249 else
250 return i2c_smbus_write_word_data(client, reg, swab16(value));
251}
252
253static void lm75_init_client(struct i2c_client *client)
254{
255 /* Initialize the LM75 chip */
256 lm75_write_value(client, LM75_REG_CONF, 0);
257}
258
259static struct lm75_data *lm75_update_device(struct device *dev)
260{
261 struct i2c_client *client = to_i2c_client(dev);
262 struct lm75_data *data = i2c_get_clientdata(client);
263
264 down(&data->update_lock);
265
266 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
267 || !data->valid) {
268 dev_dbg(&client->dev, "Starting lm75 update\n");
269
270 data->temp_input = lm75_read_value(client, LM75_REG_TEMP);
271 data->temp_max = lm75_read_value(client, LM75_REG_TEMP_OS);
272 data->temp_hyst = lm75_read_value(client, LM75_REG_TEMP_HYST);
273 data->last_updated = jiffies;
274 data->valid = 1;
275 }
276
277 up(&data->update_lock);
278
279 return data;
280}
281
282static int __init sensors_lm75_init(void)
283{
284 return i2c_add_driver(&lm75_driver);
285}
286
287static void __exit sensors_lm75_exit(void)
288{
289 i2c_del_driver(&lm75_driver);
290}
291
292MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>");
293MODULE_DESCRIPTION("LM75 driver");
294MODULE_LICENSE("GPL");
295
296module_init(sensors_lm75_init);
297module_exit(sensors_lm75_exit);
diff --git a/drivers/i2c/chips/lm75.h b/drivers/i2c/chips/lm75.h
new file mode 100644
index 000000000000..63e3f2fb4c21
--- /dev/null
+++ b/drivers/i2c/chips/lm75.h
@@ -0,0 +1,49 @@
1/*
2 lm75.h - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring
4 Copyright (c) 2003 Mark M. Hoffman <mhoffman@lightlink.com>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19*/
20
21/*
22 This file contains common code for encoding/decoding LM75 type
23 temperature readings, which are emulated by many of the chips
24 we support. As the user is unlikely to load more than one driver
25 which contains this code, we don't worry about the wasted space.
26*/
27
28#include <linux/i2c-sensor.h>
29
30/* straight from the datasheet */
31#define LM75_TEMP_MIN (-55000)
32#define LM75_TEMP_MAX 125000
33
34/* TEMP: 0.001C/bit (-55C to +125C)
35 REG: (0.5C/bit, two's complement) << 7 */
36static inline u16 LM75_TEMP_TO_REG(int temp)
37{
38 int ntemp = SENSORS_LIMIT(temp, LM75_TEMP_MIN, LM75_TEMP_MAX);
39 ntemp += (ntemp<0 ? -250 : 250);
40 return (u16)((ntemp / 500) << 7);
41}
42
43static inline int LM75_TEMP_FROM_REG(u16 reg)
44{
45 /* use integer division instead of equivalent right shift to
46 guarantee arithmetic shift and preserve the sign */
47 return ((s16)reg / 128) * 500;
48}
49
diff --git a/drivers/i2c/chips/lm77.c b/drivers/i2c/chips/lm77.c
new file mode 100644
index 000000000000..f56b7a37de75
--- /dev/null
+++ b/drivers/i2c/chips/lm77.c
@@ -0,0 +1,421 @@
1/*
2 lm77.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring
4
5 Copyright (c) 2004 Andras BALI <drewie@freemail.hu>
6
7 Heavily based on lm75.c by Frodo Looijaard <frodol@dds.nl>. The LM77
8 is a temperature sensor and thermal window comparator with 0.5 deg
9 resolution made by National Semiconductor. Complete datasheet can be
10 obtained at their site:
11 http://www.national.com/pf/LM/LM77.html
12
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
17
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26*/
27
28#include <linux/config.h>
29#include <linux/module.h>
30#include <linux/init.h>
31#include <linux/slab.h>
32#include <linux/jiffies.h>
33#include <linux/i2c.h>
34#include <linux/i2c-sensor.h>
35
36
37/* Addresses to scan */
38static unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b, I2C_CLIENT_END };
39static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
40
41/* Insmod parameters */
42SENSORS_INSMOD_1(lm77);
43
44/* The LM77 registers */
45#define LM77_REG_TEMP 0x00
46#define LM77_REG_CONF 0x01
47#define LM77_REG_TEMP_HYST 0x02
48#define LM77_REG_TEMP_CRIT 0x03
49#define LM77_REG_TEMP_MIN 0x04
50#define LM77_REG_TEMP_MAX 0x05
51
52/* Each client has this additional data */
53struct lm77_data {
54 struct i2c_client client;
55 struct semaphore update_lock;
56 char valid;
57 unsigned long last_updated; /* In jiffies */
58 int temp_input; /* Temperatures */
59 int temp_crit;
60 int temp_min;
61 int temp_max;
62 int temp_hyst;
63 u8 alarms;
64};
65
66static int lm77_attach_adapter(struct i2c_adapter *adapter);
67static int lm77_detect(struct i2c_adapter *adapter, int address, int kind);
68static void lm77_init_client(struct i2c_client *client);
69static int lm77_detach_client(struct i2c_client *client);
70static u16 lm77_read_value(struct i2c_client *client, u8 reg);
71static int lm77_write_value(struct i2c_client *client, u8 reg, u16 value);
72
73static struct lm77_data *lm77_update_device(struct device *dev);
74
75
76/* This is the driver that will be inserted */
77static struct i2c_driver lm77_driver = {
78 .owner = THIS_MODULE,
79 .name = "lm77",
80 .flags = I2C_DF_NOTIFY,
81 .attach_adapter = lm77_attach_adapter,
82 .detach_client = lm77_detach_client,
83};
84
85/* straight from the datasheet */
86#define LM77_TEMP_MIN (-55000)
87#define LM77_TEMP_MAX 125000
88
89/* In the temperature registers, the low 3 bits are not part of the
90 temperature values; they are the status bits. */
91static inline u16 LM77_TEMP_TO_REG(int temp)
92{
93 int ntemp = SENSORS_LIMIT(temp, LM77_TEMP_MIN, LM77_TEMP_MAX);
94 return (u16)((ntemp / 500) * 8);
95}
96
97static inline int LM77_TEMP_FROM_REG(u16 reg)
98{
99 return ((int)reg / 8) * 500;
100}
101
102/* sysfs stuff */
103
104/* read routines for temperature limits */
105#define show(value) \
106static ssize_t show_##value(struct device *dev, char *buf) \
107{ \
108 struct lm77_data *data = lm77_update_device(dev); \
109 return sprintf(buf, "%d\n", data->value); \
110}
111
112show(temp_input);
113show(temp_crit);
114show(temp_min);
115show(temp_max);
116show(alarms);
117
118/* read routines for hysteresis values */
119static ssize_t show_temp_crit_hyst(struct device *dev, char *buf)
120{
121 struct lm77_data *data = lm77_update_device(dev);
122 return sprintf(buf, "%d\n", data->temp_crit - data->temp_hyst);
123}
124static ssize_t show_temp_min_hyst(struct device *dev, char *buf)
125{
126 struct lm77_data *data = lm77_update_device(dev);
127 return sprintf(buf, "%d\n", data->temp_min + data->temp_hyst);
128}
129static ssize_t show_temp_max_hyst(struct device *dev, char *buf)
130{
131 struct lm77_data *data = lm77_update_device(dev);
132 return sprintf(buf, "%d\n", data->temp_max - data->temp_hyst);
133}
134
135/* write routines */
136#define set(value, reg) \
137static ssize_t set_##value(struct device *dev, const char *buf, size_t count) \
138{ \
139 struct i2c_client *client = to_i2c_client(dev); \
140 struct lm77_data *data = i2c_get_clientdata(client); \
141 long val = simple_strtoul(buf, NULL, 10); \
142 \
143 down(&data->update_lock); \
144 data->value = val; \
145 lm77_write_value(client, reg, LM77_TEMP_TO_REG(data->value)); \
146 up(&data->update_lock); \
147 return count; \
148}
149
150set(temp_min, LM77_REG_TEMP_MIN);
151set(temp_max, LM77_REG_TEMP_MAX);
152
153/* hysteresis is stored as a relative value on the chip, so it has to be
154 converted first */
155static ssize_t set_temp_crit_hyst(struct device *dev, const char *buf, size_t count)
156{
157 struct i2c_client *client = to_i2c_client(dev);
158 struct lm77_data *data = i2c_get_clientdata(client);
159 unsigned long val = simple_strtoul(buf, NULL, 10);
160
161 down(&data->update_lock);
162 data->temp_hyst = data->temp_crit - val;
163 lm77_write_value(client, LM77_REG_TEMP_HYST,
164 LM77_TEMP_TO_REG(data->temp_hyst));
165 up(&data->update_lock);
166 return count;
167}
168
169/* preserve hysteresis when setting T_crit */
170static ssize_t set_temp_crit(struct device *dev, const char *buf, size_t count)
171{
172 struct i2c_client *client = to_i2c_client(dev);
173 struct lm77_data *data = i2c_get_clientdata(client);
174 long val = simple_strtoul(buf, NULL, 10);
175 int oldcrithyst;
176
177 down(&data->update_lock);
178 oldcrithyst = data->temp_crit - data->temp_hyst;
179 data->temp_crit = val;
180 data->temp_hyst = data->temp_crit - oldcrithyst;
181 lm77_write_value(client, LM77_REG_TEMP_CRIT,
182 LM77_TEMP_TO_REG(data->temp_crit));
183 lm77_write_value(client, LM77_REG_TEMP_HYST,
184 LM77_TEMP_TO_REG(data->temp_hyst));
185 up(&data->update_lock);
186 return count;
187}
188
189static DEVICE_ATTR(temp1_input, S_IRUGO,
190 show_temp_input, NULL);
191static DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO,
192 show_temp_crit, set_temp_crit);
193static DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO,
194 show_temp_min, set_temp_min);
195static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO,
196 show_temp_max, set_temp_max);
197
198static DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO,
199 show_temp_crit_hyst, set_temp_crit_hyst);
200static DEVICE_ATTR(temp1_min_hyst, S_IRUGO,
201 show_temp_min_hyst, NULL);
202static DEVICE_ATTR(temp1_max_hyst, S_IRUGO,
203 show_temp_max_hyst, NULL);
204
205static DEVICE_ATTR(alarms, S_IRUGO,
206 show_alarms, NULL);
207
208static int lm77_attach_adapter(struct i2c_adapter *adapter)
209{
210 if (!(adapter->class & I2C_CLASS_HWMON))
211 return 0;
212 return i2c_detect(adapter, &addr_data, lm77_detect);
213}
214
215/* This function is called by i2c_detect */
216static int lm77_detect(struct i2c_adapter *adapter, int address, int kind)
217{
218 struct i2c_client *new_client;
219 struct lm77_data *data;
220 int err = 0;
221 const char *name = "";
222
223 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
224 I2C_FUNC_SMBUS_WORD_DATA))
225 goto exit;
226
227 /* OK. For now, we presume we have a valid client. We now create the
228 client structure, even though we cannot fill it completely yet.
229 But it allows us to access lm77_{read,write}_value. */
230 if (!(data = kmalloc(sizeof(struct lm77_data), GFP_KERNEL))) {
231 err = -ENOMEM;
232 goto exit;
233 }
234 memset(data, 0, sizeof(struct lm77_data));
235
236 new_client = &data->client;
237 i2c_set_clientdata(new_client, data);
238 new_client->addr = address;
239 new_client->adapter = adapter;
240 new_client->driver = &lm77_driver;
241 new_client->flags = 0;
242
243 /* Here comes the remaining detection. Since the LM77 has no
244 register dedicated to identification, we have to rely on the
245 following tricks:
246
247 1. the high 4 bits represent the sign and thus they should
248 always be the same
249 2. the high 3 bits are unused in the configuration register
250 3. addresses 0x06 and 0x07 return the last read value
251 4. registers cycling over 8-address boundaries
252
253 Word-sized registers are high-byte first. */
254 if (kind < 0) {
255 int i, cur, conf, hyst, crit, min, max;
256
257 /* addresses cycling */
258 cur = i2c_smbus_read_word_data(new_client, 0);
259 conf = i2c_smbus_read_byte_data(new_client, 1);
260 hyst = i2c_smbus_read_word_data(new_client, 2);
261 crit = i2c_smbus_read_word_data(new_client, 3);
262 min = i2c_smbus_read_word_data(new_client, 4);
263 max = i2c_smbus_read_word_data(new_client, 5);
264 for (i = 8; i <= 0xff; i += 8)
265 if (i2c_smbus_read_byte_data(new_client, i + 1) != conf
266 || i2c_smbus_read_word_data(new_client, i + 2) != hyst
267 || i2c_smbus_read_word_data(new_client, i + 3) != crit
268 || i2c_smbus_read_word_data(new_client, i + 4) != min
269 || i2c_smbus_read_word_data(new_client, i + 5) != max)
270 goto exit_free;
271
272 /* sign bits */
273 if (((cur & 0x00f0) != 0xf0 && (cur & 0x00f0) != 0x0)
274 || ((hyst & 0x00f0) != 0xf0 && (hyst & 0x00f0) != 0x0)
275 || ((crit & 0x00f0) != 0xf0 && (crit & 0x00f0) != 0x0)
276 || ((min & 0x00f0) != 0xf0 && (min & 0x00f0) != 0x0)
277 || ((max & 0x00f0) != 0xf0 && (max & 0x00f0) != 0x0))
278 goto exit_free;
279
280 /* unused bits */
281 if (conf & 0xe0)
282 goto exit_free;
283
284 /* 0x06 and 0x07 return the last read value */
285 cur = i2c_smbus_read_word_data(new_client, 0);
286 if (i2c_smbus_read_word_data(new_client, 6) != cur
287 || i2c_smbus_read_word_data(new_client, 7) != cur)
288 goto exit_free;
289 hyst = i2c_smbus_read_word_data(new_client, 2);
290 if (i2c_smbus_read_word_data(new_client, 6) != hyst
291 || i2c_smbus_read_word_data(new_client, 7) != hyst)
292 goto exit_free;
293 min = i2c_smbus_read_word_data(new_client, 4);
294 if (i2c_smbus_read_word_data(new_client, 6) != min
295 || i2c_smbus_read_word_data(new_client, 7) != min)
296 goto exit_free;
297
298 }
299
300 /* Determine the chip type - only one kind supported! */
301 if (kind <= 0)
302 kind = lm77;
303
304 if (kind == lm77) {
305 name = "lm77";
306 }
307
308 /* Fill in the remaining client fields and put it into the global list */
309 strlcpy(new_client->name, name, I2C_NAME_SIZE);
310 data->valid = 0;
311 init_MUTEX(&data->update_lock);
312
313 /* Tell the I2C layer a new client has arrived */
314 if ((err = i2c_attach_client(new_client)))
315 goto exit_free;
316
317 /* Initialize the LM77 chip */
318 lm77_init_client(new_client);
319
320 /* Register sysfs hooks */
321 device_create_file(&new_client->dev, &dev_attr_temp1_input);
322 device_create_file(&new_client->dev, &dev_attr_temp1_crit);
323 device_create_file(&new_client->dev, &dev_attr_temp1_min);
324 device_create_file(&new_client->dev, &dev_attr_temp1_max);
325 device_create_file(&new_client->dev, &dev_attr_temp1_crit_hyst);
326 device_create_file(&new_client->dev, &dev_attr_temp1_min_hyst);
327 device_create_file(&new_client->dev, &dev_attr_temp1_max_hyst);
328 device_create_file(&new_client->dev, &dev_attr_alarms);
329 return 0;
330
331exit_free:
332 kfree(data);
333exit:
334 return err;
335}
336
337static int lm77_detach_client(struct i2c_client *client)
338{
339 i2c_detach_client(client);
340 kfree(i2c_get_clientdata(client));
341 return 0;
342}
343
344/* All registers are word-sized, except for the configuration register.
345 The LM77 uses the high-byte first convention. */
346static u16 lm77_read_value(struct i2c_client *client, u8 reg)
347{
348 if (reg == LM77_REG_CONF)
349 return i2c_smbus_read_byte_data(client, reg);
350 else
351 return swab16(i2c_smbus_read_word_data(client, reg));
352}
353
354static int lm77_write_value(struct i2c_client *client, u8 reg, u16 value)
355{
356 if (reg == LM77_REG_CONF)
357 return i2c_smbus_write_byte_data(client, reg, value);
358 else
359 return i2c_smbus_write_word_data(client, reg, swab16(value));
360}
361
362static void lm77_init_client(struct i2c_client *client)
363{
364 /* Initialize the LM77 chip - turn off shutdown mode */
365 int conf = lm77_read_value(client, LM77_REG_CONF);
366 if (conf & 1)
367 lm77_write_value(client, LM77_REG_CONF, conf & 0xfe);
368}
369
370static struct lm77_data *lm77_update_device(struct device *dev)
371{
372 struct i2c_client *client = to_i2c_client(dev);
373 struct lm77_data *data = i2c_get_clientdata(client);
374
375 down(&data->update_lock);
376
377 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
378 || !data->valid) {
379 dev_dbg(&client->dev, "Starting lm77 update\n");
380 data->temp_input =
381 LM77_TEMP_FROM_REG(lm77_read_value(client,
382 LM77_REG_TEMP));
383 data->temp_hyst =
384 LM77_TEMP_FROM_REG(lm77_read_value(client,
385 LM77_REG_TEMP_HYST));
386 data->temp_crit =
387 LM77_TEMP_FROM_REG(lm77_read_value(client,
388 LM77_REG_TEMP_CRIT));
389 data->temp_min =
390 LM77_TEMP_FROM_REG(lm77_read_value(client,
391 LM77_REG_TEMP_MIN));
392 data->temp_max =
393 LM77_TEMP_FROM_REG(lm77_read_value(client,
394 LM77_REG_TEMP_MAX));
395 data->alarms =
396 lm77_read_value(client, LM77_REG_TEMP) & 0x0007;
397 data->last_updated = jiffies;
398 data->valid = 1;
399 }
400
401 up(&data->update_lock);
402
403 return data;
404}
405
406static int __init sensors_lm77_init(void)
407{
408 return i2c_add_driver(&lm77_driver);
409}
410
411static void __exit sensors_lm77_exit(void)
412{
413 i2c_del_driver(&lm77_driver);
414}
415
416MODULE_AUTHOR("Andras BALI <drewie@freemail.hu>");
417MODULE_DESCRIPTION("LM77 driver");
418MODULE_LICENSE("GPL");
419
420module_init(sensors_lm77_init);
421module_exit(sensors_lm77_exit);
diff --git a/drivers/i2c/chips/lm78.c b/drivers/i2c/chips/lm78.c
new file mode 100644
index 000000000000..6d52d14eb31c
--- /dev/null
+++ b/drivers/i2c/chips/lm78.c
@@ -0,0 +1,796 @@
1/*
2 lm78.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring
4 Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19*/
20
21#include <linux/config.h>
22#include <linux/module.h>
23#include <linux/init.h>
24#include <linux/slab.h>
25#include <linux/jiffies.h>
26#include <linux/i2c.h>
27#include <linux/i2c-sensor.h>
28#include <asm/io.h>
29
30/* Addresses to scan */
31static unsigned short normal_i2c[] = { 0x20, 0x21, 0x22, 0x23, 0x24,
32 0x25, 0x26, 0x27, 0x28, 0x29,
33 0x2a, 0x2b, 0x2c, 0x2d, 0x2e,
34 0x2f, I2C_CLIENT_END };
35static unsigned int normal_isa[] = { 0x0290, I2C_CLIENT_ISA_END };
36
37/* Insmod parameters */
38SENSORS_INSMOD_3(lm78, lm78j, lm79);
39
40/* Many LM78 constants specified below */
41
42/* Length of ISA address segment */
43#define LM78_EXTENT 8
44
45/* Where are the ISA address/data registers relative to the base address */
46#define LM78_ADDR_REG_OFFSET 5
47#define LM78_DATA_REG_OFFSET 6
48
49/* The LM78 registers */
50#define LM78_REG_IN_MAX(nr) (0x2b + (nr) * 2)
51#define LM78_REG_IN_MIN(nr) (0x2c + (nr) * 2)
52#define LM78_REG_IN(nr) (0x20 + (nr))
53
54#define LM78_REG_FAN_MIN(nr) (0x3b + (nr))
55#define LM78_REG_FAN(nr) (0x28 + (nr))
56
57#define LM78_REG_TEMP 0x27
58#define LM78_REG_TEMP_OVER 0x39
59#define LM78_REG_TEMP_HYST 0x3a
60
61#define LM78_REG_ALARM1 0x41
62#define LM78_REG_ALARM2 0x42
63
64#define LM78_REG_VID_FANDIV 0x47
65
66#define LM78_REG_CONFIG 0x40
67#define LM78_REG_CHIPID 0x49
68#define LM78_REG_I2C_ADDR 0x48
69
70
71/* Conversions. Rounding and limit checking is only done on the TO_REG
72 variants. */
73
74/* IN: mV, (0V to 4.08V)
75 REG: 16mV/bit */
76static inline u8 IN_TO_REG(unsigned long val)
77{
78 unsigned long nval = SENSORS_LIMIT(val, 0, 4080);
79 return (nval + 8) / 16;
80}
81#define IN_FROM_REG(val) ((val) * 16)
82
83static inline u8 FAN_TO_REG(long rpm, int div)
84{
85 if (rpm <= 0)
86 return 255;
87 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
88}
89
90static inline int FAN_FROM_REG(u8 val, int div)
91{
92 return val==0 ? -1 : val==255 ? 0 : 1350000/(val*div);
93}
94
95/* TEMP: mC (-128C to +127C)
96 REG: 1C/bit, two's complement */
97static inline s8 TEMP_TO_REG(int val)
98{
99 int nval = SENSORS_LIMIT(val, -128000, 127000) ;
100 return nval<0 ? (nval-500)/1000 : (nval+500)/1000;
101}
102
103static inline int TEMP_FROM_REG(s8 val)
104{
105 return val * 1000;
106}
107
108/* VID: mV
109 REG: (see doc/vid) */
110static inline int VID_FROM_REG(u8 val)
111{
112 return val==0x1f ? 0 : val>=0x10 ? 5100-val*100 : 2050-val*50;
113}
114
115#define DIV_FROM_REG(val) (1 << (val))
116
117/* There are some complications in a module like this. First off, LM78 chips
118 may be both present on the SMBus and the ISA bus, and we have to handle
119 those cases separately at some places. Second, there might be several
120 LM78 chips available (well, actually, that is probably never done; but
121 it is a clean illustration of how to handle a case like that). Finally,
122 a specific chip may be attached to *both* ISA and SMBus, and we would
123 not like to detect it double. Fortunately, in the case of the LM78 at
124 least, a register tells us what SMBus address we are on, so that helps
125 a bit - except if there could be more than one SMBus. Groan. No solution
126 for this yet. */
127
128/* This module may seem overly long and complicated. In fact, it is not so
129 bad. Quite a lot of bookkeeping is done. A real driver can often cut
130 some corners. */
131
132/* For each registered LM78, we need to keep some data in memory. That
133 data is pointed to by lm78_list[NR]->data. The structure itself is
134 dynamically allocated, at the same time when a new lm78 client is
135 allocated. */
136struct lm78_data {
137 struct i2c_client client;
138 struct semaphore lock;
139 enum chips type;
140
141 struct semaphore update_lock;
142 char valid; /* !=0 if following fields are valid */
143 unsigned long last_updated; /* In jiffies */
144
145 u8 in[7]; /* Register value */
146 u8 in_max[7]; /* Register value */
147 u8 in_min[7]; /* Register value */
148 u8 fan[3]; /* Register value */
149 u8 fan_min[3]; /* Register value */
150 s8 temp; /* Register value */
151 s8 temp_over; /* Register value */
152 s8 temp_hyst; /* Register value */
153 u8 fan_div[3]; /* Register encoding, shifted right */
154 u8 vid; /* Register encoding, combined */
155 u16 alarms; /* Register encoding, combined */
156};
157
158
159static int lm78_attach_adapter(struct i2c_adapter *adapter);
160static int lm78_detect(struct i2c_adapter *adapter, int address, int kind);
161static int lm78_detach_client(struct i2c_client *client);
162
163static int lm78_read_value(struct i2c_client *client, u8 register);
164static int lm78_write_value(struct i2c_client *client, u8 register, u8 value);
165static struct lm78_data *lm78_update_device(struct device *dev);
166static void lm78_init_client(struct i2c_client *client);
167
168
169static struct i2c_driver lm78_driver = {
170 .owner = THIS_MODULE,
171 .name = "lm78",
172 .id = I2C_DRIVERID_LM78,
173 .flags = I2C_DF_NOTIFY,
174 .attach_adapter = lm78_attach_adapter,
175 .detach_client = lm78_detach_client,
176};
177
178/* 7 Voltages */
179static ssize_t show_in(struct device *dev, char *buf, int nr)
180{
181 struct lm78_data *data = lm78_update_device(dev);
182 return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr]));
183}
184
185static ssize_t show_in_min(struct device *dev, char *buf, int nr)
186{
187 struct lm78_data *data = lm78_update_device(dev);
188 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr]));
189}
190
191static ssize_t show_in_max(struct device *dev, char *buf, int nr)
192{
193 struct lm78_data *data = lm78_update_device(dev);
194 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr]));
195}
196
197static ssize_t set_in_min(struct device *dev, const char *buf,
198 size_t count, int nr)
199{
200 struct i2c_client *client = to_i2c_client(dev);
201 struct lm78_data *data = i2c_get_clientdata(client);
202 unsigned long val = simple_strtoul(buf, NULL, 10);
203
204 down(&data->update_lock);
205 data->in_min[nr] = IN_TO_REG(val);
206 lm78_write_value(client, LM78_REG_IN_MIN(nr), data->in_min[nr]);
207 up(&data->update_lock);
208 return count;
209}
210
211static ssize_t set_in_max(struct device *dev, const char *buf,
212 size_t count, int nr)
213{
214 struct i2c_client *client = to_i2c_client(dev);
215 struct lm78_data *data = i2c_get_clientdata(client);
216 unsigned long val = simple_strtoul(buf, NULL, 10);
217
218 down(&data->update_lock);
219 data->in_max[nr] = IN_TO_REG(val);
220 lm78_write_value(client, LM78_REG_IN_MAX(nr), data->in_max[nr]);
221 up(&data->update_lock);
222 return count;
223}
224
225#define show_in_offset(offset) \
226static ssize_t \
227 show_in##offset (struct device *dev, char *buf) \
228{ \
229 return show_in(dev, buf, offset); \
230} \
231static DEVICE_ATTR(in##offset##_input, S_IRUGO, \
232 show_in##offset, NULL); \
233static ssize_t \
234 show_in##offset##_min (struct device *dev, char *buf) \
235{ \
236 return show_in_min(dev, buf, offset); \
237} \
238static ssize_t \
239 show_in##offset##_max (struct device *dev, char *buf) \
240{ \
241 return show_in_max(dev, buf, offset); \
242} \
243static ssize_t set_in##offset##_min (struct device *dev, \
244 const char *buf, size_t count) \
245{ \
246 return set_in_min(dev, buf, count, offset); \
247} \
248static ssize_t set_in##offset##_max (struct device *dev, \
249 const char *buf, size_t count) \
250{ \
251 return set_in_max(dev, buf, count, offset); \
252} \
253static DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
254 show_in##offset##_min, set_in##offset##_min); \
255static DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
256 show_in##offset##_max, set_in##offset##_max);
257
258show_in_offset(0);
259show_in_offset(1);
260show_in_offset(2);
261show_in_offset(3);
262show_in_offset(4);
263show_in_offset(5);
264show_in_offset(6);
265
266/* Temperature */
267static ssize_t show_temp(struct device *dev, char *buf)
268{
269 struct lm78_data *data = lm78_update_device(dev);
270 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp));
271}
272
273static ssize_t show_temp_over(struct device *dev, char *buf)
274{
275 struct lm78_data *data = lm78_update_device(dev);
276 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_over));
277}
278
279static ssize_t set_temp_over(struct device *dev, const char *buf, size_t count)
280{
281 struct i2c_client *client = to_i2c_client(dev);
282 struct lm78_data *data = i2c_get_clientdata(client);
283 long val = simple_strtol(buf, NULL, 10);
284
285 down(&data->update_lock);
286 data->temp_over = TEMP_TO_REG(val);
287 lm78_write_value(client, LM78_REG_TEMP_OVER, data->temp_over);
288 up(&data->update_lock);
289 return count;
290}
291
292static ssize_t show_temp_hyst(struct device *dev, char *buf)
293{
294 struct lm78_data *data = lm78_update_device(dev);
295 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_hyst));
296}
297
298static ssize_t set_temp_hyst(struct device *dev, const char *buf, size_t count)
299{
300 struct i2c_client *client = to_i2c_client(dev);
301 struct lm78_data *data = i2c_get_clientdata(client);
302 long val = simple_strtol(buf, NULL, 10);
303
304 down(&data->update_lock);
305 data->temp_hyst = TEMP_TO_REG(val);
306 lm78_write_value(client, LM78_REG_TEMP_HYST, data->temp_hyst);
307 up(&data->update_lock);
308 return count;
309}
310
311static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL);
312static DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR,
313 show_temp_over, set_temp_over);
314static DEVICE_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR,
315 show_temp_hyst, set_temp_hyst);
316
317/* 3 Fans */
318static ssize_t show_fan(struct device *dev, char *buf, int nr)
319{
320 struct lm78_data *data = lm78_update_device(dev);
321 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
322 DIV_FROM_REG(data->fan_div[nr])) );
323}
324
325static ssize_t show_fan_min(struct device *dev, char *buf, int nr)
326{
327 struct lm78_data *data = lm78_update_device(dev);
328 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan_min[nr],
329 DIV_FROM_REG(data->fan_div[nr])) );
330}
331
332static ssize_t set_fan_min(struct device *dev, const char *buf,
333 size_t count, int nr)
334{
335 struct i2c_client *client = to_i2c_client(dev);
336 struct lm78_data *data = i2c_get_clientdata(client);
337 unsigned long val = simple_strtoul(buf, NULL, 10);
338
339 down(&data->update_lock);
340 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
341 lm78_write_value(client, LM78_REG_FAN_MIN(nr), data->fan_min[nr]);
342 up(&data->update_lock);
343 return count;
344}
345
346static ssize_t show_fan_div(struct device *dev, char *buf, int nr)
347{
348 struct lm78_data *data = lm78_update_device(dev);
349 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]) );
350}
351
352/* Note: we save and restore the fan minimum here, because its value is
353 determined in part by the fan divisor. This follows the principle of
354 least suprise; the user doesn't expect the fan minimum to change just
355 because the divisor changed. */
356static ssize_t set_fan_div(struct device *dev, const char *buf,
357 size_t count, int nr)
358{
359 struct i2c_client *client = to_i2c_client(dev);
360 struct lm78_data *data = i2c_get_clientdata(client);
361 unsigned long val = simple_strtoul(buf, NULL, 10);
362 unsigned long min;
363 u8 reg;
364
365 down(&data->update_lock);
366 min = FAN_FROM_REG(data->fan_min[nr],
367 DIV_FROM_REG(data->fan_div[nr]));
368
369 switch (val) {
370 case 1: data->fan_div[nr] = 0; break;
371 case 2: data->fan_div[nr] = 1; break;
372 case 4: data->fan_div[nr] = 2; break;
373 case 8: data->fan_div[nr] = 3; break;
374 default:
375 dev_err(&client->dev, "fan_div value %ld not "
376 "supported. Choose one of 1, 2, 4 or 8!\n", val);
377 up(&data->update_lock);
378 return -EINVAL;
379 }
380
381 reg = lm78_read_value(client, LM78_REG_VID_FANDIV);
382 switch (nr) {
383 case 0:
384 reg = (reg & 0xcf) | (data->fan_div[nr] << 4);
385 break;
386 case 1:
387 reg = (reg & 0x3f) | (data->fan_div[nr] << 6);
388 break;
389 }
390 lm78_write_value(client, LM78_REG_VID_FANDIV, reg);
391
392 data->fan_min[nr] =
393 FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
394 lm78_write_value(client, LM78_REG_FAN_MIN(nr), data->fan_min[nr]);
395 up(&data->update_lock);
396
397 return count;
398}
399
400#define show_fan_offset(offset) \
401static ssize_t show_fan_##offset (struct device *dev, char *buf) \
402{ \
403 return show_fan(dev, buf, offset - 1); \
404} \
405static ssize_t show_fan_##offset##_min (struct device *dev, char *buf) \
406{ \
407 return show_fan_min(dev, buf, offset - 1); \
408} \
409static ssize_t show_fan_##offset##_div (struct device *dev, char *buf) \
410{ \
411 return show_fan_div(dev, buf, offset - 1); \
412} \
413static ssize_t set_fan_##offset##_min (struct device *dev, \
414 const char *buf, size_t count) \
415{ \
416 return set_fan_min(dev, buf, count, offset - 1); \
417} \
418static DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_fan_##offset, NULL);\
419static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
420 show_fan_##offset##_min, set_fan_##offset##_min);
421
422static ssize_t set_fan_1_div(struct device *dev, const char *buf,
423 size_t count)
424{
425 return set_fan_div(dev, buf, count, 0) ;
426}
427
428static ssize_t set_fan_2_div(struct device *dev, const char *buf,
429 size_t count)
430{
431 return set_fan_div(dev, buf, count, 1) ;
432}
433
434show_fan_offset(1);
435show_fan_offset(2);
436show_fan_offset(3);
437
438/* Fan 3 divisor is locked in H/W */
439static DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR,
440 show_fan_1_div, set_fan_1_div);
441static DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR,
442 show_fan_2_div, set_fan_2_div);
443static DEVICE_ATTR(fan3_div, S_IRUGO, show_fan_3_div, NULL);
444
445/* VID */
446static ssize_t show_vid(struct device *dev, char *buf)
447{
448 struct lm78_data *data = lm78_update_device(dev);
449 return sprintf(buf, "%d\n", VID_FROM_REG(data->vid));
450}
451static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
452
453/* Alarms */
454static ssize_t show_alarms(struct device *dev, char *buf)
455{
456 struct lm78_data *data = lm78_update_device(dev);
457 return sprintf(buf, "%u\n", data->alarms);
458}
459static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
460
461/* This function is called when:
462 * lm78_driver is inserted (when this module is loaded), for each
463 available adapter
464 * when a new adapter is inserted (and lm78_driver is still present) */
465static int lm78_attach_adapter(struct i2c_adapter *adapter)
466{
467 if (!(adapter->class & I2C_CLASS_HWMON))
468 return 0;
469 return i2c_detect(adapter, &addr_data, lm78_detect);
470}
471
472/* This function is called by i2c_detect */
473int lm78_detect(struct i2c_adapter *adapter, int address, int kind)
474{
475 int i, err;
476 struct i2c_client *new_client;
477 struct lm78_data *data;
478 const char *client_name = "";
479 int is_isa = i2c_is_isa_adapter(adapter);
480
481 if (!is_isa &&
482 !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
483 err = -ENODEV;
484 goto ERROR0;
485 }
486
487 /* Reserve the ISA region */
488 if (is_isa)
489 if (!request_region(address, LM78_EXTENT, lm78_driver.name)) {
490 err = -EBUSY;
491 goto ERROR0;
492 }
493
494 /* Probe whether there is anything available on this address. Already
495 done for SMBus clients */
496 if (kind < 0) {
497 if (is_isa) {
498
499#define REALLY_SLOW_IO
500 /* We need the timeouts for at least some LM78-like
501 chips. But only if we read 'undefined' registers. */
502 i = inb_p(address + 1);
503 if (inb_p(address + 2) != i) {
504 err = -ENODEV;
505 goto ERROR1;
506 }
507 if (inb_p(address + 3) != i) {
508 err = -ENODEV;
509 goto ERROR1;
510 }
511 if (inb_p(address + 7) != i) {
512 err = -ENODEV;
513 goto ERROR1;
514 }
515#undef REALLY_SLOW_IO
516
517 /* Let's just hope nothing breaks here */
518 i = inb_p(address + 5) & 0x7f;
519 outb_p(~i & 0x7f, address + 5);
520 if ((inb_p(address + 5) & 0x7f) != (~i & 0x7f)) {
521 outb_p(i, address + 5);
522 err = -ENODEV;
523 goto ERROR1;
524 }
525 }
526 }
527
528 /* OK. For now, we presume we have a valid client. We now create the
529 client structure, even though we cannot fill it completely yet.
530 But it allows us to access lm78_{read,write}_value. */
531
532 if (!(data = kmalloc(sizeof(struct lm78_data), GFP_KERNEL))) {
533 err = -ENOMEM;
534 goto ERROR1;
535 }
536 memset(data, 0, sizeof(struct lm78_data));
537
538 new_client = &data->client;
539 if (is_isa)
540 init_MUTEX(&data->lock);
541 i2c_set_clientdata(new_client, data);
542 new_client->addr = address;
543 new_client->adapter = adapter;
544 new_client->driver = &lm78_driver;
545 new_client->flags = 0;
546
547 /* Now, we do the remaining detection. */
548 if (kind < 0) {
549 if (lm78_read_value(new_client, LM78_REG_CONFIG) & 0x80) {
550 err = -ENODEV;
551 goto ERROR2;
552 }
553 if (!is_isa && (lm78_read_value(
554 new_client, LM78_REG_I2C_ADDR) != address)) {
555 err = -ENODEV;
556 goto ERROR2;
557 }
558 }
559
560 /* Determine the chip type. */
561 if (kind <= 0) {
562 i = lm78_read_value(new_client, LM78_REG_CHIPID);
563 if (i == 0x00 || i == 0x20)
564 kind = lm78;
565 else if (i == 0x40)
566 kind = lm78j;
567 else if ((i & 0xfe) == 0xc0)
568 kind = lm79;
569 else {
570 if (kind == 0)
571 dev_warn(&adapter->dev, "Ignoring 'force' "
572 "parameter for unknown chip at "
573 "adapter %d, address 0x%02x\n",
574 i2c_adapter_id(adapter), address);
575 err = -ENODEV;
576 goto ERROR2;
577 }
578 }
579
580 if (kind == lm78) {
581 client_name = "lm78";
582 } else if (kind == lm78j) {
583 client_name = "lm78-j";
584 } else if (kind == lm79) {
585 client_name = "lm79";
586 }
587
588 /* Fill in the remaining client fields and put into the global list */
589 strlcpy(new_client->name, client_name, I2C_NAME_SIZE);
590 data->type = kind;
591
592 data->valid = 0;
593 init_MUTEX(&data->update_lock);
594
595 /* Tell the I2C layer a new client has arrived */
596 if ((err = i2c_attach_client(new_client)))
597 goto ERROR2;
598
599 /* Initialize the LM78 chip */
600 lm78_init_client(new_client);
601
602 /* A few vars need to be filled upon startup */
603 for (i = 0; i < 3; i++) {
604 data->fan_min[i] = lm78_read_value(new_client,
605 LM78_REG_FAN_MIN(i));
606 }
607
608 /* Register sysfs hooks */
609 device_create_file(&new_client->dev, &dev_attr_in0_input);
610 device_create_file(&new_client->dev, &dev_attr_in0_min);
611 device_create_file(&new_client->dev, &dev_attr_in0_max);
612 device_create_file(&new_client->dev, &dev_attr_in1_input);
613 device_create_file(&new_client->dev, &dev_attr_in1_min);
614 device_create_file(&new_client->dev, &dev_attr_in1_max);
615 device_create_file(&new_client->dev, &dev_attr_in2_input);
616 device_create_file(&new_client->dev, &dev_attr_in2_min);
617 device_create_file(&new_client->dev, &dev_attr_in2_max);
618 device_create_file(&new_client->dev, &dev_attr_in3_input);
619 device_create_file(&new_client->dev, &dev_attr_in3_min);
620 device_create_file(&new_client->dev, &dev_attr_in3_max);
621 device_create_file(&new_client->dev, &dev_attr_in4_input);
622 device_create_file(&new_client->dev, &dev_attr_in4_min);
623 device_create_file(&new_client->dev, &dev_attr_in4_max);
624 device_create_file(&new_client->dev, &dev_attr_in5_input);
625 device_create_file(&new_client->dev, &dev_attr_in5_min);
626 device_create_file(&new_client->dev, &dev_attr_in5_max);
627 device_create_file(&new_client->dev, &dev_attr_in6_input);
628 device_create_file(&new_client->dev, &dev_attr_in6_min);
629 device_create_file(&new_client->dev, &dev_attr_in6_max);
630 device_create_file(&new_client->dev, &dev_attr_temp1_input);
631 device_create_file(&new_client->dev, &dev_attr_temp1_max);
632 device_create_file(&new_client->dev, &dev_attr_temp1_max_hyst);
633 device_create_file(&new_client->dev, &dev_attr_fan1_input);
634 device_create_file(&new_client->dev, &dev_attr_fan1_min);
635 device_create_file(&new_client->dev, &dev_attr_fan1_div);
636 device_create_file(&new_client->dev, &dev_attr_fan2_input);
637 device_create_file(&new_client->dev, &dev_attr_fan2_min);
638 device_create_file(&new_client->dev, &dev_attr_fan2_div);
639 device_create_file(&new_client->dev, &dev_attr_fan3_input);
640 device_create_file(&new_client->dev, &dev_attr_fan3_min);
641 device_create_file(&new_client->dev, &dev_attr_fan3_div);
642 device_create_file(&new_client->dev, &dev_attr_alarms);
643 device_create_file(&new_client->dev, &dev_attr_cpu0_vid);
644
645 return 0;
646
647ERROR2:
648 kfree(data);
649ERROR1:
650 if (is_isa)
651 release_region(address, LM78_EXTENT);
652ERROR0:
653 return err;
654}
655
656static int lm78_detach_client(struct i2c_client *client)
657{
658 int err;
659
660 if ((err = i2c_detach_client(client))) {
661 dev_err(&client->dev,
662 "Client deregistration failed, client not detached.\n");
663 return err;
664 }
665
666 if(i2c_is_isa_client(client))
667 release_region(client->addr, LM78_EXTENT);
668
669 kfree(i2c_get_clientdata(client));
670
671 return 0;
672}
673
674/* The SMBus locks itself, but ISA access must be locked explicitely!
675 We don't want to lock the whole ISA bus, so we lock each client
676 separately.
677 We ignore the LM78 BUSY flag at this moment - it could lead to deadlocks,
678 would slow down the LM78 access and should not be necessary. */
679static int lm78_read_value(struct i2c_client *client, u8 reg)
680{
681 int res;
682 if (i2c_is_isa_client(client)) {
683 struct lm78_data *data = i2c_get_clientdata(client);
684 down(&data->lock);
685 outb_p(reg, client->addr + LM78_ADDR_REG_OFFSET);
686 res = inb_p(client->addr + LM78_DATA_REG_OFFSET);
687 up(&data->lock);
688 return res;
689 } else
690 return i2c_smbus_read_byte_data(client, reg);
691}
692
693/* The SMBus locks itself, but ISA access muse be locked explicitely!
694 We don't want to lock the whole ISA bus, so we lock each client
695 separately.
696 We ignore the LM78 BUSY flag at this moment - it could lead to deadlocks,
697 would slow down the LM78 access and should not be necessary.
698 There are some ugly typecasts here, but the good new is - they should
699 nowhere else be necessary! */
700static int lm78_write_value(struct i2c_client *client, u8 reg, u8 value)
701{
702 if (i2c_is_isa_client(client)) {
703 struct lm78_data *data = i2c_get_clientdata(client);
704 down(&data->lock);
705 outb_p(reg, client->addr + LM78_ADDR_REG_OFFSET);
706 outb_p(value, client->addr + LM78_DATA_REG_OFFSET);
707 up(&data->lock);
708 return 0;
709 } else
710 return i2c_smbus_write_byte_data(client, reg, value);
711}
712
713/* Called when we have found a new LM78. It should set limits, etc. */
714static void lm78_init_client(struct i2c_client *client)
715{
716 u8 config = lm78_read_value(client, LM78_REG_CONFIG);
717
718 /* Start monitoring */
719 if (!(config & 0x01))
720 lm78_write_value(client, LM78_REG_CONFIG,
721 (config & 0xf7) | 0x01);
722}
723
724static struct lm78_data *lm78_update_device(struct device *dev)
725{
726 struct i2c_client *client = to_i2c_client(dev);
727 struct lm78_data *data = i2c_get_clientdata(client);
728 int i;
729
730 down(&data->update_lock);
731
732 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
733 || !data->valid) {
734
735 dev_dbg(&client->dev, "Starting lm78 update\n");
736
737 for (i = 0; i <= 6; i++) {
738 data->in[i] =
739 lm78_read_value(client, LM78_REG_IN(i));
740 data->in_min[i] =
741 lm78_read_value(client, LM78_REG_IN_MIN(i));
742 data->in_max[i] =
743 lm78_read_value(client, LM78_REG_IN_MAX(i));
744 }
745 for (i = 0; i < 3; i++) {
746 data->fan[i] =
747 lm78_read_value(client, LM78_REG_FAN(i));
748 data->fan_min[i] =
749 lm78_read_value(client, LM78_REG_FAN_MIN(i));
750 }
751 data->temp = lm78_read_value(client, LM78_REG_TEMP);
752 data->temp_over =
753 lm78_read_value(client, LM78_REG_TEMP_OVER);
754 data->temp_hyst =
755 lm78_read_value(client, LM78_REG_TEMP_HYST);
756 i = lm78_read_value(client, LM78_REG_VID_FANDIV);
757 data->vid = i & 0x0f;
758 if (data->type == lm79)
759 data->vid |=
760 (lm78_read_value(client, LM78_REG_CHIPID) &
761 0x01) << 4;
762 else
763 data->vid |= 0x10;
764 data->fan_div[0] = (i >> 4) & 0x03;
765 data->fan_div[1] = i >> 6;
766 data->alarms = lm78_read_value(client, LM78_REG_ALARM1) +
767 (lm78_read_value(client, LM78_REG_ALARM2) << 8);
768 data->last_updated = jiffies;
769 data->valid = 1;
770
771 data->fan_div[2] = 1;
772 }
773
774 up(&data->update_lock);
775
776 return data;
777}
778
779static int __init sm_lm78_init(void)
780{
781 return i2c_add_driver(&lm78_driver);
782}
783
784static void __exit sm_lm78_exit(void)
785{
786 i2c_del_driver(&lm78_driver);
787}
788
789
790
791MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>");
792MODULE_DESCRIPTION("LM78, LM78-J and LM79 driver");
793MODULE_LICENSE("GPL");
794
795module_init(sm_lm78_init);
796module_exit(sm_lm78_exit);
diff --git a/drivers/i2c/chips/lm80.c b/drivers/i2c/chips/lm80.c
new file mode 100644
index 000000000000..a72f431971bb
--- /dev/null
+++ b/drivers/i2c/chips/lm80.c
@@ -0,0 +1,602 @@
1/*
2 * lm80.c - From lm_sensors, Linux kernel modules for hardware
3 * monitoring
4 * Copyright (C) 1998, 1999 Frodo Looijaard <frodol@dds.nl>
5 * and Philip Edelbrock <phil@netroedge.com>
6 *
7 * Ported to Linux 2.6 by Tiago Sousa <mirage@kaotik.org>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24#include <linux/config.h>
25#include <linux/module.h>
26#include <linux/init.h>
27#include <linux/slab.h>
28#include <linux/jiffies.h>
29#include <linux/i2c.h>
30#include <linux/i2c-sensor.h>
31
32/* Addresses to scan */
33static unsigned short normal_i2c[] = { 0x28, 0x29, 0x2a, 0x2b, 0x2c,
34 0x2d, 0x2e, 0x2f, I2C_CLIENT_END };
35static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
36
37/* Insmod parameters */
38SENSORS_INSMOD_1(lm80);
39
40/* Many LM80 constants specified below */
41
42/* The LM80 registers */
43#define LM80_REG_IN_MAX(nr) (0x2a + (nr) * 2)
44#define LM80_REG_IN_MIN(nr) (0x2b + (nr) * 2)
45#define LM80_REG_IN(nr) (0x20 + (nr))
46
47#define LM80_REG_FAN1 0x28
48#define LM80_REG_FAN2 0x29
49#define LM80_REG_FAN_MIN(nr) (0x3b + (nr))
50
51#define LM80_REG_TEMP 0x27
52#define LM80_REG_TEMP_HOT_MAX 0x38
53#define LM80_REG_TEMP_HOT_HYST 0x39
54#define LM80_REG_TEMP_OS_MAX 0x3a
55#define LM80_REG_TEMP_OS_HYST 0x3b
56
57#define LM80_REG_CONFIG 0x00
58#define LM80_REG_ALARM1 0x01
59#define LM80_REG_ALARM2 0x02
60#define LM80_REG_MASK1 0x03
61#define LM80_REG_MASK2 0x04
62#define LM80_REG_FANDIV 0x05
63#define LM80_REG_RES 0x06
64
65
66/* Conversions. Rounding and limit checking is only done on the TO_REG
67 variants. Note that you should be a bit careful with which arguments
68 these macros are called: arguments may be evaluated more than once.
69 Fixing this is just not worth it. */
70
71#define IN_TO_REG(val) (SENSORS_LIMIT(((val)+5)/10,0,255))
72#define IN_FROM_REG(val) ((val)*10)
73
74static inline unsigned char FAN_TO_REG(unsigned rpm, unsigned div)
75{
76 if (rpm == 0)
77 return 255;
78 rpm = SENSORS_LIMIT(rpm, 1, 1000000);
79 return SENSORS_LIMIT((1350000 + rpm*div / 2) / (rpm*div), 1, 254);
80}
81
82#define FAN_FROM_REG(val,div) ((val)==0?-1:\
83 (val)==255?0:1350000/((div)*(val)))
84
85static inline long TEMP_FROM_REG(u16 temp)
86{
87 long res;
88
89 temp >>= 4;
90 if (temp < 0x0800)
91 res = 625 * (long) temp;
92 else
93 res = ((long) temp - 0x01000) * 625;
94
95 return res / 10;
96}
97
98#define TEMP_LIMIT_FROM_REG(val) (((val)>0x80?(val)-0x100:(val))*1000)
99
100#define TEMP_LIMIT_TO_REG(val) SENSORS_LIMIT((val)<0?\
101 ((val)-500)/1000:((val)+500)/1000,0,255)
102
103#define DIV_FROM_REG(val) (1 << (val))
104
105/*
106 * Client data (each client gets its own)
107 */
108
109struct lm80_data {
110 struct i2c_client client;
111 struct semaphore update_lock;
112 char valid; /* !=0 if following fields are valid */
113 unsigned long last_updated; /* In jiffies */
114
115 u8 in[7]; /* Register value */
116 u8 in_max[7]; /* Register value */
117 u8 in_min[7]; /* Register value */
118 u8 fan[2]; /* Register value */
119 u8 fan_min[2]; /* Register value */
120 u8 fan_div[2]; /* Register encoding, shifted right */
121 u16 temp; /* Register values, shifted right */
122 u8 temp_hot_max; /* Register value */
123 u8 temp_hot_hyst; /* Register value */
124 u8 temp_os_max; /* Register value */
125 u8 temp_os_hyst; /* Register value */
126 u16 alarms; /* Register encoding, combined */
127};
128
129/*
130 * Functions declaration
131 */
132
133static int lm80_attach_adapter(struct i2c_adapter *adapter);
134static int lm80_detect(struct i2c_adapter *adapter, int address, int kind);
135static void lm80_init_client(struct i2c_client *client);
136static int lm80_detach_client(struct i2c_client *client);
137static struct lm80_data *lm80_update_device(struct device *dev);
138static int lm80_read_value(struct i2c_client *client, u8 reg);
139static int lm80_write_value(struct i2c_client *client, u8 reg, u8 value);
140
141/*
142 * Driver data (common to all clients)
143 */
144
145static struct i2c_driver lm80_driver = {
146 .owner = THIS_MODULE,
147 .name = "lm80",
148 .id = I2C_DRIVERID_LM80,
149 .flags = I2C_DF_NOTIFY,
150 .attach_adapter = lm80_attach_adapter,
151 .detach_client = lm80_detach_client,
152};
153
154/*
155 * Sysfs stuff
156 */
157
158#define show_in(suffix, value) \
159static ssize_t show_in_##suffix(struct device *dev, char *buf) \
160{ \
161 struct lm80_data *data = lm80_update_device(dev); \
162 return sprintf(buf, "%d\n", IN_FROM_REG(data->value)); \
163}
164show_in(min0, in_min[0]);
165show_in(min1, in_min[1]);
166show_in(min2, in_min[2]);
167show_in(min3, in_min[3]);
168show_in(min4, in_min[4]);
169show_in(min5, in_min[5]);
170show_in(min6, in_min[6]);
171show_in(max0, in_max[0]);
172show_in(max1, in_max[1]);
173show_in(max2, in_max[2]);
174show_in(max3, in_max[3]);
175show_in(max4, in_max[4]);
176show_in(max5, in_max[5]);
177show_in(max6, in_max[6]);
178show_in(input0, in[0]);
179show_in(input1, in[1]);
180show_in(input2, in[2]);
181show_in(input3, in[3]);
182show_in(input4, in[4]);
183show_in(input5, in[5]);
184show_in(input6, in[6]);
185
186#define set_in(suffix, value, reg) \
187static ssize_t set_in_##suffix(struct device *dev, const char *buf, \
188 size_t count) \
189{ \
190 struct i2c_client *client = to_i2c_client(dev); \
191 struct lm80_data *data = i2c_get_clientdata(client); \
192 long val = simple_strtol(buf, NULL, 10); \
193 \
194 down(&data->update_lock);\
195 data->value = IN_TO_REG(val); \
196 lm80_write_value(client, reg, data->value); \
197 up(&data->update_lock);\
198 return count; \
199}
200set_in(min0, in_min[0], LM80_REG_IN_MIN(0));
201set_in(min1, in_min[1], LM80_REG_IN_MIN(1));
202set_in(min2, in_min[2], LM80_REG_IN_MIN(2));
203set_in(min3, in_min[3], LM80_REG_IN_MIN(3));
204set_in(min4, in_min[4], LM80_REG_IN_MIN(4));
205set_in(min5, in_min[5], LM80_REG_IN_MIN(5));
206set_in(min6, in_min[6], LM80_REG_IN_MIN(6));
207set_in(max0, in_max[0], LM80_REG_IN_MAX(0));
208set_in(max1, in_max[1], LM80_REG_IN_MAX(1));
209set_in(max2, in_max[2], LM80_REG_IN_MAX(2));
210set_in(max3, in_max[3], LM80_REG_IN_MAX(3));
211set_in(max4, in_max[4], LM80_REG_IN_MAX(4));
212set_in(max5, in_max[5], LM80_REG_IN_MAX(5));
213set_in(max6, in_max[6], LM80_REG_IN_MAX(6));
214
215#define show_fan(suffix, value, div) \
216static ssize_t show_fan_##suffix(struct device *dev, char *buf) \
217{ \
218 struct lm80_data *data = lm80_update_device(dev); \
219 return sprintf(buf, "%d\n", FAN_FROM_REG(data->value, \
220 DIV_FROM_REG(data->div))); \
221}
222show_fan(min1, fan_min[0], fan_div[0]);
223show_fan(min2, fan_min[1], fan_div[1]);
224show_fan(input1, fan[0], fan_div[0]);
225show_fan(input2, fan[1], fan_div[1]);
226
227#define show_fan_div(suffix, value) \
228static ssize_t show_fan_div##suffix(struct device *dev, char *buf) \
229{ \
230 struct lm80_data *data = lm80_update_device(dev); \
231 return sprintf(buf, "%d\n", DIV_FROM_REG(data->value)); \
232}
233show_fan_div(1, fan_div[0]);
234show_fan_div(2, fan_div[1]);
235
236#define set_fan(suffix, value, reg, div) \
237static ssize_t set_fan_##suffix(struct device *dev, const char *buf, \
238 size_t count) \
239{ \
240 struct i2c_client *client = to_i2c_client(dev); \
241 struct lm80_data *data = i2c_get_clientdata(client); \
242 long val = simple_strtoul(buf, NULL, 10); \
243 \
244 down(&data->update_lock);\
245 data->value = FAN_TO_REG(val, DIV_FROM_REG(data->div)); \
246 lm80_write_value(client, reg, data->value); \
247 up(&data->update_lock);\
248 return count; \
249}
250set_fan(min1, fan_min[0], LM80_REG_FAN_MIN(1), fan_div[0]);
251set_fan(min2, fan_min[1], LM80_REG_FAN_MIN(2), fan_div[1]);
252
253/* Note: we save and restore the fan minimum here, because its value is
254 determined in part by the fan divisor. This follows the principle of
255 least suprise; the user doesn't expect the fan minimum to change just
256 because the divisor changed. */
257static ssize_t set_fan_div(struct device *dev, const char *buf,
258 size_t count, int nr)
259{
260 struct i2c_client *client = to_i2c_client(dev);
261 struct lm80_data *data = i2c_get_clientdata(client);
262 unsigned long min, val = simple_strtoul(buf, NULL, 10);
263 u8 reg;
264
265 /* Save fan_min */
266 down(&data->update_lock);
267 min = FAN_FROM_REG(data->fan_min[nr],
268 DIV_FROM_REG(data->fan_div[nr]));
269
270 switch (val) {
271 case 1: data->fan_div[nr] = 0; break;
272 case 2: data->fan_div[nr] = 1; break;
273 case 4: data->fan_div[nr] = 2; break;
274 case 8: data->fan_div[nr] = 3; break;
275 default:
276 dev_err(&client->dev, "fan_div value %ld not "
277 "supported. Choose one of 1, 2, 4 or 8!\n", val);
278 up(&data->update_lock);
279 return -EINVAL;
280 }
281
282 reg = (lm80_read_value(client, LM80_REG_FANDIV) & ~(3 << (2 * (nr + 1))))
283 | (data->fan_div[nr] << (2 * (nr + 1)));
284 lm80_write_value(client, LM80_REG_FANDIV, reg);
285
286 /* Restore fan_min */
287 data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
288 lm80_write_value(client, LM80_REG_FAN_MIN(nr + 1), data->fan_min[nr]);
289 up(&data->update_lock);
290
291 return count;
292}
293
294#define set_fan_div(number) \
295static ssize_t set_fan_div##number(struct device *dev, const char *buf, \
296 size_t count) \
297{ \
298 return set_fan_div(dev, buf, count, number - 1); \
299}
300set_fan_div(1);
301set_fan_div(2);
302
303static ssize_t show_temp_input1(struct device *dev, char *buf)
304{
305 struct lm80_data *data = lm80_update_device(dev);
306 return sprintf(buf, "%ld\n", TEMP_FROM_REG(data->temp));
307}
308
309#define show_temp(suffix, value) \
310static ssize_t show_temp_##suffix(struct device *dev, char *buf) \
311{ \
312 struct lm80_data *data = lm80_update_device(dev); \
313 return sprintf(buf, "%d\n", TEMP_LIMIT_FROM_REG(data->value)); \
314}
315show_temp(hot_max, temp_hot_max);
316show_temp(hot_hyst, temp_hot_hyst);
317show_temp(os_max, temp_os_max);
318show_temp(os_hyst, temp_os_hyst);
319
320#define set_temp(suffix, value, reg) \
321static ssize_t set_temp_##suffix(struct device *dev, const char *buf, \
322 size_t count) \
323{ \
324 struct i2c_client *client = to_i2c_client(dev); \
325 struct lm80_data *data = i2c_get_clientdata(client); \
326 long val = simple_strtoul(buf, NULL, 10); \
327 \
328 down(&data->update_lock); \
329 data->value = TEMP_LIMIT_TO_REG(val); \
330 lm80_write_value(client, reg, data->value); \
331 up(&data->update_lock); \
332 return count; \
333}
334set_temp(hot_max, temp_hot_max, LM80_REG_TEMP_HOT_MAX);
335set_temp(hot_hyst, temp_hot_hyst, LM80_REG_TEMP_HOT_HYST);
336set_temp(os_max, temp_os_max, LM80_REG_TEMP_OS_MAX);
337set_temp(os_hyst, temp_os_hyst, LM80_REG_TEMP_OS_HYST);
338
339static ssize_t show_alarms(struct device *dev, char *buf)
340{
341 struct lm80_data *data = lm80_update_device(dev);
342 return sprintf(buf, "%u\n", data->alarms);
343}
344
345static DEVICE_ATTR(in0_min, S_IWUSR | S_IRUGO, show_in_min0, set_in_min0);
346static DEVICE_ATTR(in1_min, S_IWUSR | S_IRUGO, show_in_min1, set_in_min1);
347static DEVICE_ATTR(in2_min, S_IWUSR | S_IRUGO, show_in_min2, set_in_min2);
348static DEVICE_ATTR(in3_min, S_IWUSR | S_IRUGO, show_in_min3, set_in_min3);
349static DEVICE_ATTR(in4_min, S_IWUSR | S_IRUGO, show_in_min4, set_in_min4);
350static DEVICE_ATTR(in5_min, S_IWUSR | S_IRUGO, show_in_min5, set_in_min5);
351static DEVICE_ATTR(in6_min, S_IWUSR | S_IRUGO, show_in_min6, set_in_min6);
352static DEVICE_ATTR(in0_max, S_IWUSR | S_IRUGO, show_in_max0, set_in_max0);
353static DEVICE_ATTR(in1_max, S_IWUSR | S_IRUGO, show_in_max1, set_in_max1);
354static DEVICE_ATTR(in2_max, S_IWUSR | S_IRUGO, show_in_max2, set_in_max2);
355static DEVICE_ATTR(in3_max, S_IWUSR | S_IRUGO, show_in_max3, set_in_max3);
356static DEVICE_ATTR(in4_max, S_IWUSR | S_IRUGO, show_in_max4, set_in_max4);
357static DEVICE_ATTR(in5_max, S_IWUSR | S_IRUGO, show_in_max5, set_in_max5);
358static DEVICE_ATTR(in6_max, S_IWUSR | S_IRUGO, show_in_max6, set_in_max6);
359static DEVICE_ATTR(in0_input, S_IRUGO, show_in_input0, NULL);
360static DEVICE_ATTR(in1_input, S_IRUGO, show_in_input1, NULL);
361static DEVICE_ATTR(in2_input, S_IRUGO, show_in_input2, NULL);
362static DEVICE_ATTR(in3_input, S_IRUGO, show_in_input3, NULL);
363static DEVICE_ATTR(in4_input, S_IRUGO, show_in_input4, NULL);
364static DEVICE_ATTR(in5_input, S_IRUGO, show_in_input5, NULL);
365static DEVICE_ATTR(in6_input, S_IRUGO, show_in_input6, NULL);
366static DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan_min1,
367 set_fan_min1);
368static DEVICE_ATTR(fan2_min, S_IWUSR | S_IRUGO, show_fan_min2,
369 set_fan_min2);
370static DEVICE_ATTR(fan1_input, S_IRUGO, show_fan_input1, NULL);
371static DEVICE_ATTR(fan2_input, S_IRUGO, show_fan_input2, NULL);
372static DEVICE_ATTR(fan1_div, S_IWUSR | S_IRUGO, show_fan_div1, set_fan_div1);
373static DEVICE_ATTR(fan2_div, S_IWUSR | S_IRUGO, show_fan_div2, set_fan_div2);
374static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input1, NULL);
375static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_hot_max,
376 set_temp_hot_max);
377static DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO, show_temp_hot_hyst,
378 set_temp_hot_hyst);
379static DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_temp_os_max,
380 set_temp_os_max);
381static DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, show_temp_os_hyst,
382 set_temp_os_hyst);
383static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
384
385/*
386 * Real code
387 */
388
389static int lm80_attach_adapter(struct i2c_adapter *adapter)
390{
391 if (!(adapter->class & I2C_CLASS_HWMON))
392 return 0;
393 return i2c_detect(adapter, &addr_data, lm80_detect);
394}
395
396int lm80_detect(struct i2c_adapter *adapter, int address, int kind)
397{
398 int i, cur;
399 struct i2c_client *new_client;
400 struct lm80_data *data;
401 int err = 0;
402 const char *name;
403
404 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
405 goto exit;
406
407 /* OK. For now, we presume we have a valid client. We now create the
408 client structure, even though we cannot fill it completely yet.
409 But it allows us to access lm80_{read,write}_value. */
410 if (!(data = kmalloc(sizeof(struct lm80_data), GFP_KERNEL))) {
411 err = -ENOMEM;
412 goto exit;
413 }
414 memset(data, 0, sizeof(struct lm80_data));
415
416 new_client = &data->client;
417 i2c_set_clientdata(new_client, data);
418 new_client->addr = address;
419 new_client->adapter = adapter;
420 new_client->driver = &lm80_driver;
421 new_client->flags = 0;
422
423 /* Now, we do the remaining detection. It is lousy. */
424 if (lm80_read_value(new_client, LM80_REG_ALARM2) & 0xc0)
425 goto error_free;
426 for (i = 0x2a; i <= 0x3d; i++) {
427 cur = i2c_smbus_read_byte_data(new_client, i);
428 if ((i2c_smbus_read_byte_data(new_client, i + 0x40) != cur)
429 || (i2c_smbus_read_byte_data(new_client, i + 0x80) != cur)
430 || (i2c_smbus_read_byte_data(new_client, i + 0xc0) != cur))
431 goto error_free;
432 }
433
434 /* Determine the chip type - only one kind supported! */
435 kind = lm80;
436 name = "lm80";
437
438 /* Fill in the remaining client fields and put it into the global list */
439 strlcpy(new_client->name, name, I2C_NAME_SIZE);
440 data->valid = 0;
441 init_MUTEX(&data->update_lock);
442
443 /* Tell the I2C layer a new client has arrived */
444 if ((err = i2c_attach_client(new_client)))
445 goto error_free;
446
447 /* Initialize the LM80 chip */
448 lm80_init_client(new_client);
449
450 /* A few vars need to be filled upon startup */
451 data->fan_min[0] = lm80_read_value(new_client, LM80_REG_FAN_MIN(1));
452 data->fan_min[1] = lm80_read_value(new_client, LM80_REG_FAN_MIN(2));
453
454 /* Register sysfs hooks */
455 device_create_file(&new_client->dev, &dev_attr_in0_min);
456 device_create_file(&new_client->dev, &dev_attr_in1_min);
457 device_create_file(&new_client->dev, &dev_attr_in2_min);
458 device_create_file(&new_client->dev, &dev_attr_in3_min);
459 device_create_file(&new_client->dev, &dev_attr_in4_min);
460 device_create_file(&new_client->dev, &dev_attr_in5_min);
461 device_create_file(&new_client->dev, &dev_attr_in6_min);
462 device_create_file(&new_client->dev, &dev_attr_in0_max);
463 device_create_file(&new_client->dev, &dev_attr_in1_max);
464 device_create_file(&new_client->dev, &dev_attr_in2_max);
465 device_create_file(&new_client->dev, &dev_attr_in3_max);
466 device_create_file(&new_client->dev, &dev_attr_in4_max);
467 device_create_file(&new_client->dev, &dev_attr_in5_max);
468 device_create_file(&new_client->dev, &dev_attr_in6_max);
469 device_create_file(&new_client->dev, &dev_attr_in0_input);
470 device_create_file(&new_client->dev, &dev_attr_in1_input);
471 device_create_file(&new_client->dev, &dev_attr_in2_input);
472 device_create_file(&new_client->dev, &dev_attr_in3_input);
473 device_create_file(&new_client->dev, &dev_attr_in4_input);
474 device_create_file(&new_client->dev, &dev_attr_in5_input);
475 device_create_file(&new_client->dev, &dev_attr_in6_input);
476 device_create_file(&new_client->dev, &dev_attr_fan1_min);
477 device_create_file(&new_client->dev, &dev_attr_fan2_min);
478 device_create_file(&new_client->dev, &dev_attr_fan1_input);
479 device_create_file(&new_client->dev, &dev_attr_fan2_input);
480 device_create_file(&new_client->dev, &dev_attr_fan1_div);
481 device_create_file(&new_client->dev, &dev_attr_fan2_div);
482 device_create_file(&new_client->dev, &dev_attr_temp1_input);
483 device_create_file(&new_client->dev, &dev_attr_temp1_max);
484 device_create_file(&new_client->dev, &dev_attr_temp1_max_hyst);
485 device_create_file(&new_client->dev, &dev_attr_temp1_crit);
486 device_create_file(&new_client->dev, &dev_attr_temp1_crit_hyst);
487 device_create_file(&new_client->dev, &dev_attr_alarms);
488
489 return 0;
490
491error_free:
492 kfree(data);
493exit:
494 return err;
495}
496
497static int lm80_detach_client(struct i2c_client *client)
498{
499 int err;
500
501 if ((err = i2c_detach_client(client))) {
502 dev_err(&client->dev, "Client deregistration failed, "
503 "client not detached.\n");
504 return err;
505 }
506
507 kfree(i2c_get_clientdata(client));
508 return 0;
509}
510
511static int lm80_read_value(struct i2c_client *client, u8 reg)
512{
513 return i2c_smbus_read_byte_data(client, reg);
514}
515
516static int lm80_write_value(struct i2c_client *client, u8 reg, u8 value)
517{
518 return i2c_smbus_write_byte_data(client, reg, value);
519}
520
521/* Called when we have found a new LM80. */
522static void lm80_init_client(struct i2c_client *client)
523{
524 /* Reset all except Watchdog values and last conversion values
525 This sets fan-divs to 2, among others. This makes most other
526 initializations unnecessary */
527 lm80_write_value(client, LM80_REG_CONFIG, 0x80);
528 /* Set 11-bit temperature resolution */
529 lm80_write_value(client, LM80_REG_RES, 0x08);
530
531 /* Start monitoring */
532 lm80_write_value(client, LM80_REG_CONFIG, 0x01);
533}
534
535static struct lm80_data *lm80_update_device(struct device *dev)
536{
537 struct i2c_client *client = to_i2c_client(dev);
538 struct lm80_data *data = i2c_get_clientdata(client);
539 int i;
540
541 down(&data->update_lock);
542
543 if (time_after(jiffies, data->last_updated + 2 * HZ) || !data->valid) {
544 dev_dbg(&client->dev, "Starting lm80 update\n");
545 for (i = 0; i <= 6; i++) {
546 data->in[i] =
547 lm80_read_value(client, LM80_REG_IN(i));
548 data->in_min[i] =
549 lm80_read_value(client, LM80_REG_IN_MIN(i));
550 data->in_max[i] =
551 lm80_read_value(client, LM80_REG_IN_MAX(i));
552 }
553 data->fan[0] = lm80_read_value(client, LM80_REG_FAN1);
554 data->fan_min[0] =
555 lm80_read_value(client, LM80_REG_FAN_MIN(1));
556 data->fan[1] = lm80_read_value(client, LM80_REG_FAN2);
557 data->fan_min[1] =
558 lm80_read_value(client, LM80_REG_FAN_MIN(2));
559
560 data->temp =
561 (lm80_read_value(client, LM80_REG_TEMP) << 8) |
562 (lm80_read_value(client, LM80_REG_RES) & 0xf0);
563 data->temp_os_max =
564 lm80_read_value(client, LM80_REG_TEMP_OS_MAX);
565 data->temp_os_hyst =
566 lm80_read_value(client, LM80_REG_TEMP_OS_HYST);
567 data->temp_hot_max =
568 lm80_read_value(client, LM80_REG_TEMP_HOT_MAX);
569 data->temp_hot_hyst =
570 lm80_read_value(client, LM80_REG_TEMP_HOT_HYST);
571
572 i = lm80_read_value(client, LM80_REG_FANDIV);
573 data->fan_div[0] = (i >> 2) & 0x03;
574 data->fan_div[1] = (i >> 4) & 0x03;
575 data->alarms = lm80_read_value(client, LM80_REG_ALARM1) +
576 (lm80_read_value(client, LM80_REG_ALARM2) << 8);
577 data->last_updated = jiffies;
578 data->valid = 1;
579 }
580
581 up(&data->update_lock);
582
583 return data;
584}
585
586static int __init sensors_lm80_init(void)
587{
588 return i2c_add_driver(&lm80_driver);
589}
590
591static void __exit sensors_lm80_exit(void)
592{
593 i2c_del_driver(&lm80_driver);
594}
595
596MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and "
597 "Philip Edelbrock <phil@netroedge.com>");
598MODULE_DESCRIPTION("LM80 driver");
599MODULE_LICENSE("GPL");
600
601module_init(sensors_lm80_init);
602module_exit(sensors_lm80_exit);
diff --git a/drivers/i2c/chips/lm83.c b/drivers/i2c/chips/lm83.c
new file mode 100644
index 000000000000..3dafe60766ad
--- /dev/null
+++ b/drivers/i2c/chips/lm83.c
@@ -0,0 +1,412 @@
1/*
2 * lm83.c - Part of lm_sensors, Linux kernel modules for hardware
3 * monitoring
4 * Copyright (C) 2003 Jean Delvare <khali@linux-fr.org>
5 *
6 * Heavily inspired from the lm78, lm75 and adm1021 drivers. The LM83 is
7 * a sensor chip made by National Semiconductor. It reports up to four
8 * temperatures (its own plus up to three external ones) with a 1 deg
9 * resolution and a 3-4 deg accuracy. Complete datasheet can be obtained
10 * from National's website at:
11 * http://www.national.com/pf/LM/LM83.html
12 * Since the datasheet omits to give the chip stepping code, I give it
13 * here: 0x03 (at register 0xff).
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 */
29
30#include <linux/config.h>
31#include <linux/module.h>
32#include <linux/init.h>
33#include <linux/slab.h>
34#include <linux/jiffies.h>
35#include <linux/i2c.h>
36#include <linux/i2c-sensor.h>
37
38/*
39 * Addresses to scan
40 * Address is selected using 2 three-level pins, resulting in 9 possible
41 * addresses.
42 */
43
44static unsigned short normal_i2c[] = { 0x18, 0x19, 0x1a,
45 0x29, 0x2a, 0x2b,
46 0x4c, 0x4d, 0x4e,
47 I2C_CLIENT_END };
48static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
49
50/*
51 * Insmod parameters
52 */
53
54SENSORS_INSMOD_1(lm83);
55
56/*
57 * The LM83 registers
58 * Manufacturer ID is 0x01 for National Semiconductor.
59 */
60
61#define LM83_REG_R_MAN_ID 0xFE
62#define LM83_REG_R_CHIP_ID 0xFF
63#define LM83_REG_R_CONFIG 0x03
64#define LM83_REG_W_CONFIG 0x09
65#define LM83_REG_R_STATUS1 0x02
66#define LM83_REG_R_STATUS2 0x35
67#define LM83_REG_R_LOCAL_TEMP 0x00
68#define LM83_REG_R_LOCAL_HIGH 0x05
69#define LM83_REG_W_LOCAL_HIGH 0x0B
70#define LM83_REG_R_REMOTE1_TEMP 0x30
71#define LM83_REG_R_REMOTE1_HIGH 0x38
72#define LM83_REG_W_REMOTE1_HIGH 0x50
73#define LM83_REG_R_REMOTE2_TEMP 0x01
74#define LM83_REG_R_REMOTE2_HIGH 0x07
75#define LM83_REG_W_REMOTE2_HIGH 0x0D
76#define LM83_REG_R_REMOTE3_TEMP 0x31
77#define LM83_REG_R_REMOTE3_HIGH 0x3A
78#define LM83_REG_W_REMOTE3_HIGH 0x52
79#define LM83_REG_R_TCRIT 0x42
80#define LM83_REG_W_TCRIT 0x5A
81
82/*
83 * Conversions and various macros
84 * The LM83 uses signed 8-bit values with LSB = 1 degree Celcius.
85 */
86
87#define TEMP_FROM_REG(val) ((val) * 1000)
88#define TEMP_TO_REG(val) ((val) <= -128000 ? -128 : \
89 (val) >= 127000 ? 127 : \
90 (val) < 0 ? ((val) - 500) / 1000 : \
91 ((val) + 500) / 1000)
92
93static const u8 LM83_REG_R_TEMP[] = {
94 LM83_REG_R_LOCAL_TEMP,
95 LM83_REG_R_REMOTE1_TEMP,
96 LM83_REG_R_REMOTE2_TEMP,
97 LM83_REG_R_REMOTE3_TEMP
98};
99
100static const u8 LM83_REG_R_HIGH[] = {
101 LM83_REG_R_LOCAL_HIGH,
102 LM83_REG_R_REMOTE1_HIGH,
103 LM83_REG_R_REMOTE2_HIGH,
104 LM83_REG_R_REMOTE3_HIGH
105};
106
107static const u8 LM83_REG_W_HIGH[] = {
108 LM83_REG_W_LOCAL_HIGH,
109 LM83_REG_W_REMOTE1_HIGH,
110 LM83_REG_W_REMOTE2_HIGH,
111 LM83_REG_W_REMOTE3_HIGH
112};
113
114/*
115 * Functions declaration
116 */
117
118static int lm83_attach_adapter(struct i2c_adapter *adapter);
119static int lm83_detect(struct i2c_adapter *adapter, int address, int kind);
120static int lm83_detach_client(struct i2c_client *client);
121static struct lm83_data *lm83_update_device(struct device *dev);
122
123/*
124 * Driver data (common to all clients)
125 */
126
127static struct i2c_driver lm83_driver = {
128 .owner = THIS_MODULE,
129 .name = "lm83",
130 .id = I2C_DRIVERID_LM83,
131 .flags = I2C_DF_NOTIFY,
132 .attach_adapter = lm83_attach_adapter,
133 .detach_client = lm83_detach_client,
134};
135
136/*
137 * Client data (each client gets its own)
138 */
139
140struct lm83_data {
141 struct i2c_client client;
142 struct semaphore update_lock;
143 char valid; /* zero until following fields are valid */
144 unsigned long last_updated; /* in jiffies */
145
146 /* registers values */
147 s8 temp_input[4];
148 s8 temp_high[4];
149 s8 temp_crit;
150 u16 alarms; /* bitvector, combined */
151};
152
153/*
154 * Sysfs stuff
155 */
156
157#define show_temp(suffix, value) \
158static ssize_t show_temp_##suffix(struct device *dev, char *buf) \
159{ \
160 struct lm83_data *data = lm83_update_device(dev); \
161 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->value)); \
162}
163show_temp(input1, temp_input[0]);
164show_temp(input2, temp_input[1]);
165show_temp(input3, temp_input[2]);
166show_temp(input4, temp_input[3]);
167show_temp(high1, temp_high[0]);
168show_temp(high2, temp_high[1]);
169show_temp(high3, temp_high[2]);
170show_temp(high4, temp_high[3]);
171show_temp(crit, temp_crit);
172
173#define set_temp(suffix, value, reg) \
174static ssize_t set_temp_##suffix(struct device *dev, const char *buf, \
175 size_t count) \
176{ \
177 struct i2c_client *client = to_i2c_client(dev); \
178 struct lm83_data *data = i2c_get_clientdata(client); \
179 long val = simple_strtol(buf, NULL, 10); \
180 \
181 down(&data->update_lock); \
182 data->value = TEMP_TO_REG(val); \
183 i2c_smbus_write_byte_data(client, reg, data->value); \
184 up(&data->update_lock); \
185 return count; \
186}
187set_temp(high1, temp_high[0], LM83_REG_W_LOCAL_HIGH);
188set_temp(high2, temp_high[1], LM83_REG_W_REMOTE1_HIGH);
189set_temp(high3, temp_high[2], LM83_REG_W_REMOTE2_HIGH);
190set_temp(high4, temp_high[3], LM83_REG_W_REMOTE3_HIGH);
191set_temp(crit, temp_crit, LM83_REG_W_TCRIT);
192
193static ssize_t show_alarms(struct device *dev, char *buf)
194{
195 struct lm83_data *data = lm83_update_device(dev);
196 return sprintf(buf, "%d\n", data->alarms);
197}
198
199static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input1, NULL);
200static DEVICE_ATTR(temp2_input, S_IRUGO, show_temp_input2, NULL);
201static DEVICE_ATTR(temp3_input, S_IRUGO, show_temp_input3, NULL);
202static DEVICE_ATTR(temp4_input, S_IRUGO, show_temp_input4, NULL);
203static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_high1,
204 set_temp_high1);
205static DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp_high2,
206 set_temp_high2);
207static DEVICE_ATTR(temp3_max, S_IWUSR | S_IRUGO, show_temp_high3,
208 set_temp_high3);
209static DEVICE_ATTR(temp4_max, S_IWUSR | S_IRUGO, show_temp_high4,
210 set_temp_high4);
211static DEVICE_ATTR(temp1_crit, S_IRUGO, show_temp_crit, NULL);
212static DEVICE_ATTR(temp2_crit, S_IRUGO, show_temp_crit, NULL);
213static DEVICE_ATTR(temp3_crit, S_IWUSR | S_IRUGO, show_temp_crit,
214 set_temp_crit);
215static DEVICE_ATTR(temp4_crit, S_IRUGO, show_temp_crit, NULL);
216static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
217
218/*
219 * Real code
220 */
221
222static int lm83_attach_adapter(struct i2c_adapter *adapter)
223{
224 if (!(adapter->class & I2C_CLASS_HWMON))
225 return 0;
226 return i2c_detect(adapter, &addr_data, lm83_detect);
227}
228
229/*
230 * The following function does more than just detection. If detection
231 * succeeds, it also registers the new chip.
232 */
233static int lm83_detect(struct i2c_adapter *adapter, int address, int kind)
234{
235 struct i2c_client *new_client;
236 struct lm83_data *data;
237 int err = 0;
238 const char *name = "";
239
240 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
241 goto exit;
242
243 if (!(data = kmalloc(sizeof(struct lm83_data), GFP_KERNEL))) {
244 err = -ENOMEM;
245 goto exit;
246 }
247 memset(data, 0, sizeof(struct lm83_data));
248
249 /* The common I2C client data is placed right after the
250 * LM83-specific data. */
251 new_client = &data->client;
252 i2c_set_clientdata(new_client, data);
253 new_client->addr = address;
254 new_client->adapter = adapter;
255 new_client->driver = &lm83_driver;
256 new_client->flags = 0;
257
258 /* Now we do the detection and identification. A negative kind
259 * means that the driver was loaded with no force parameter
260 * (default), so we must both detect and identify the chip
261 * (actually there is only one possible kind of chip for now, LM83).
262 * A zero kind means that the driver was loaded with the force
263 * parameter, the detection step shall be skipped. A positive kind
264 * means that the driver was loaded with the force parameter and a
265 * given kind of chip is requested, so both the detection and the
266 * identification steps are skipped. */
267
268 /* Default to an LM83 if forced */
269 if (kind == 0)
270 kind = lm83;
271
272 if (kind < 0) { /* detection */
273 if (((i2c_smbus_read_byte_data(new_client, LM83_REG_R_STATUS1)
274 & 0xA8) != 0x00) ||
275 ((i2c_smbus_read_byte_data(new_client, LM83_REG_R_STATUS2)
276 & 0x48) != 0x00) ||
277 ((i2c_smbus_read_byte_data(new_client, LM83_REG_R_CONFIG)
278 & 0x41) != 0x00)) {
279 dev_dbg(&adapter->dev,
280 "LM83 detection failed at 0x%02x.\n", address);
281 goto exit_free;
282 }
283 }
284
285 if (kind <= 0) { /* identification */
286 u8 man_id, chip_id;
287
288 man_id = i2c_smbus_read_byte_data(new_client,
289 LM83_REG_R_MAN_ID);
290 chip_id = i2c_smbus_read_byte_data(new_client,
291 LM83_REG_R_CHIP_ID);
292
293 if (man_id == 0x01) { /* National Semiconductor */
294 if (chip_id == 0x03) {
295 kind = lm83;
296 }
297 }
298
299 if (kind <= 0) { /* identification failed */
300 dev_info(&adapter->dev,
301 "Unsupported chip (man_id=0x%02X, "
302 "chip_id=0x%02X).\n", man_id, chip_id);
303 goto exit_free;
304 }
305 }
306
307 if (kind == lm83) {
308 name = "lm83";
309 }
310
311 /* We can fill in the remaining client fields */
312 strlcpy(new_client->name, name, I2C_NAME_SIZE);
313 data->valid = 0;
314 init_MUTEX(&data->update_lock);
315
316 /* Tell the I2C layer a new client has arrived */
317 if ((err = i2c_attach_client(new_client)))
318 goto exit_free;
319
320 /*
321 * Initialize the LM83 chip
322 * (Nothing to do for this one.)
323 */
324
325 /* Register sysfs hooks */
326 device_create_file(&new_client->dev, &dev_attr_temp1_input);
327 device_create_file(&new_client->dev, &dev_attr_temp2_input);
328 device_create_file(&new_client->dev, &dev_attr_temp3_input);
329 device_create_file(&new_client->dev, &dev_attr_temp4_input);
330 device_create_file(&new_client->dev, &dev_attr_temp1_max);
331 device_create_file(&new_client->dev, &dev_attr_temp2_max);
332 device_create_file(&new_client->dev, &dev_attr_temp3_max);
333 device_create_file(&new_client->dev, &dev_attr_temp4_max);
334 device_create_file(&new_client->dev, &dev_attr_temp1_crit);
335 device_create_file(&new_client->dev, &dev_attr_temp2_crit);
336 device_create_file(&new_client->dev, &dev_attr_temp3_crit);
337 device_create_file(&new_client->dev, &dev_attr_temp4_crit);
338 device_create_file(&new_client->dev, &dev_attr_alarms);
339
340 return 0;
341
342exit_free:
343 kfree(data);
344exit:
345 return err;
346}
347
348static int lm83_detach_client(struct i2c_client *client)
349{
350 int err;
351
352 if ((err = i2c_detach_client(client))) {
353 dev_err(&client->dev,
354 "Client deregistration failed, client not detached.\n");
355 return err;
356 }
357
358 kfree(i2c_get_clientdata(client));
359 return 0;
360}
361
362static struct lm83_data *lm83_update_device(struct device *dev)
363{
364 struct i2c_client *client = to_i2c_client(dev);
365 struct lm83_data *data = i2c_get_clientdata(client);
366
367 down(&data->update_lock);
368
369 if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
370 int nr;
371
372 dev_dbg(&client->dev, "Updating lm83 data.\n");
373 for (nr = 0; nr < 4 ; nr++) {
374 data->temp_input[nr] =
375 i2c_smbus_read_byte_data(client,
376 LM83_REG_R_TEMP[nr]);
377 data->temp_high[nr] =
378 i2c_smbus_read_byte_data(client,
379 LM83_REG_R_HIGH[nr]);
380 }
381 data->temp_crit =
382 i2c_smbus_read_byte_data(client, LM83_REG_R_TCRIT);
383 data->alarms =
384 i2c_smbus_read_byte_data(client, LM83_REG_R_STATUS1)
385 + (i2c_smbus_read_byte_data(client, LM83_REG_R_STATUS2)
386 << 8);
387
388 data->last_updated = jiffies;
389 data->valid = 1;
390 }
391
392 up(&data->update_lock);
393
394 return data;
395}
396
397static int __init sensors_lm83_init(void)
398{
399 return i2c_add_driver(&lm83_driver);
400}
401
402static void __exit sensors_lm83_exit(void)
403{
404 i2c_del_driver(&lm83_driver);
405}
406
407MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
408MODULE_DESCRIPTION("LM83 driver");
409MODULE_LICENSE("GPL");
410
411module_init(sensors_lm83_init);
412module_exit(sensors_lm83_exit);
diff --git a/drivers/i2c/chips/lm85.c b/drivers/i2c/chips/lm85.c
new file mode 100644
index 000000000000..b1a0dc5f6b34
--- /dev/null
+++ b/drivers/i2c/chips/lm85.c
@@ -0,0 +1,1578 @@
1/*
2 lm85.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring
4 Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl>
5 Copyright (c) 2002, 2003 Philip Pokorny <ppokorny@penguincomputing.com>
6 Copyright (c) 2003 Margit Schubert-While <margitsw@t-online.de>
7 Copyright (c) 2004 Justin Thiessen <jthiessen@penguincomputing.com>
8
9 Chip details at <http://www.national.com/ds/LM/LM85.pdf>
10
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24*/
25
26#include <linux/config.h>
27#include <linux/module.h>
28#include <linux/init.h>
29#include <linux/slab.h>
30#include <linux/jiffies.h>
31#include <linux/i2c.h>
32#include <linux/i2c-sensor.h>
33#include <linux/i2c-vid.h>
34
35/* Addresses to scan */
36static unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
37static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
38
39/* Insmod parameters */
40SENSORS_INSMOD_6(lm85b, lm85c, adm1027, adt7463, emc6d100, emc6d102);
41
42/* The LM85 registers */
43
44#define LM85_REG_IN(nr) (0x20 + (nr))
45#define LM85_REG_IN_MIN(nr) (0x44 + (nr) * 2)
46#define LM85_REG_IN_MAX(nr) (0x45 + (nr) * 2)
47
48#define LM85_REG_TEMP(nr) (0x25 + (nr))
49#define LM85_REG_TEMP_MIN(nr) (0x4e + (nr) * 2)
50#define LM85_REG_TEMP_MAX(nr) (0x4f + (nr) * 2)
51
52/* Fan speeds are LSB, MSB (2 bytes) */
53#define LM85_REG_FAN(nr) (0x28 + (nr) *2)
54#define LM85_REG_FAN_MIN(nr) (0x54 + (nr) *2)
55
56#define LM85_REG_PWM(nr) (0x30 + (nr))
57
58#define ADT7463_REG_OPPOINT(nr) (0x33 + (nr))
59
60#define ADT7463_REG_TMIN_CTL1 0x36
61#define ADT7463_REG_TMIN_CTL2 0x37
62
63#define LM85_REG_DEVICE 0x3d
64#define LM85_REG_COMPANY 0x3e
65#define LM85_REG_VERSTEP 0x3f
66/* These are the recognized values for the above regs */
67#define LM85_DEVICE_ADX 0x27
68#define LM85_COMPANY_NATIONAL 0x01
69#define LM85_COMPANY_ANALOG_DEV 0x41
70#define LM85_COMPANY_SMSC 0x5c
71#define LM85_VERSTEP_VMASK 0xf0
72#define LM85_VERSTEP_GENERIC 0x60
73#define LM85_VERSTEP_LM85C 0x60
74#define LM85_VERSTEP_LM85B 0x62
75#define LM85_VERSTEP_ADM1027 0x60
76#define LM85_VERSTEP_ADT7463 0x62
77#define LM85_VERSTEP_ADT7463C 0x6A
78#define LM85_VERSTEP_EMC6D100_A0 0x60
79#define LM85_VERSTEP_EMC6D100_A1 0x61
80#define LM85_VERSTEP_EMC6D102 0x65
81
82#define LM85_REG_CONFIG 0x40
83
84#define LM85_REG_ALARM1 0x41
85#define LM85_REG_ALARM2 0x42
86
87#define LM85_REG_VID 0x43
88
89/* Automated FAN control */
90#define LM85_REG_AFAN_CONFIG(nr) (0x5c + (nr))
91#define LM85_REG_AFAN_RANGE(nr) (0x5f + (nr))
92#define LM85_REG_AFAN_SPIKE1 0x62
93#define LM85_REG_AFAN_SPIKE2 0x63
94#define LM85_REG_AFAN_MINPWM(nr) (0x64 + (nr))
95#define LM85_REG_AFAN_LIMIT(nr) (0x67 + (nr))
96#define LM85_REG_AFAN_CRITICAL(nr) (0x6a + (nr))
97#define LM85_REG_AFAN_HYST1 0x6d
98#define LM85_REG_AFAN_HYST2 0x6e
99
100#define LM85_REG_TACH_MODE 0x74
101#define LM85_REG_SPINUP_CTL 0x75
102
103#define ADM1027_REG_TEMP_OFFSET(nr) (0x70 + (nr))
104#define ADM1027_REG_CONFIG2 0x73
105#define ADM1027_REG_INTMASK1 0x74
106#define ADM1027_REG_INTMASK2 0x75
107#define ADM1027_REG_EXTEND_ADC1 0x76
108#define ADM1027_REG_EXTEND_ADC2 0x77
109#define ADM1027_REG_CONFIG3 0x78
110#define ADM1027_REG_FAN_PPR 0x7b
111
112#define ADT7463_REG_THERM 0x79
113#define ADT7463_REG_THERM_LIMIT 0x7A
114
115#define EMC6D100_REG_ALARM3 0x7d
116/* IN5, IN6 and IN7 */
117#define EMC6D100_REG_IN(nr) (0x70 + ((nr)-5))
118#define EMC6D100_REG_IN_MIN(nr) (0x73 + ((nr)-5) * 2)
119#define EMC6D100_REG_IN_MAX(nr) (0x74 + ((nr)-5) * 2)
120#define EMC6D102_REG_EXTEND_ADC1 0x85
121#define EMC6D102_REG_EXTEND_ADC2 0x86
122#define EMC6D102_REG_EXTEND_ADC3 0x87
123#define EMC6D102_REG_EXTEND_ADC4 0x88
124
125#define LM85_ALARM_IN0 0x0001
126#define LM85_ALARM_IN1 0x0002
127#define LM85_ALARM_IN2 0x0004
128#define LM85_ALARM_IN3 0x0008
129#define LM85_ALARM_TEMP1 0x0010
130#define LM85_ALARM_TEMP2 0x0020
131#define LM85_ALARM_TEMP3 0x0040
132#define LM85_ALARM_ALARM2 0x0080
133#define LM85_ALARM_IN4 0x0100
134#define LM85_ALARM_RESERVED 0x0200
135#define LM85_ALARM_FAN1 0x0400
136#define LM85_ALARM_FAN2 0x0800
137#define LM85_ALARM_FAN3 0x1000
138#define LM85_ALARM_FAN4 0x2000
139#define LM85_ALARM_TEMP1_FAULT 0x4000
140#define LM85_ALARM_TEMP3_FAULT 0x8000
141
142
143/* Conversions. Rounding and limit checking is only done on the TO_REG
144 variants. Note that you should be a bit careful with which arguments
145 these macros are called: arguments may be evaluated more than once.
146 */
147
148/* IN are scaled acording to built-in resistors */
149static int lm85_scaling[] = { /* .001 Volts */
150 2500, 2250, 3300, 5000, 12000,
151 3300, 1500, 1800 /*EMC6D100*/
152 };
153#define SCALE(val,from,to) (((val)*(to) + ((from)/2))/(from))
154
155#define INS_TO_REG(n,val) \
156 SENSORS_LIMIT(SCALE(val,lm85_scaling[n],192),0,255)
157
158#define INSEXT_FROM_REG(n,val,ext,scale) \
159 SCALE((val)*(scale) + (ext),192*(scale),lm85_scaling[n])
160
161#define INS_FROM_REG(n,val) INSEXT_FROM_REG(n,val,0,1)
162
163/* FAN speed is measured using 90kHz clock */
164#define FAN_TO_REG(val) (SENSORS_LIMIT( (val)<=0?0: 5400000/(val),0,65534))
165#define FAN_FROM_REG(val) ((val)==0?-1:(val)==0xffff?0:5400000/(val))
166
167/* Temperature is reported in .001 degC increments */
168#define TEMP_TO_REG(val) \
169 SENSORS_LIMIT(SCALE(val,1000,1),-127,127)
170#define TEMPEXT_FROM_REG(val,ext,scale) \
171 SCALE((val)*scale + (ext),scale,1000)
172#define TEMP_FROM_REG(val) \
173 TEMPEXT_FROM_REG(val,0,1)
174
175#define PWM_TO_REG(val) (SENSORS_LIMIT(val,0,255))
176#define PWM_FROM_REG(val) (val)
177
178
179/* ZONEs have the following parameters:
180 * Limit (low) temp, 1. degC
181 * Hysteresis (below limit), 1. degC (0-15)
182 * Range of speed control, .1 degC (2-80)
183 * Critical (high) temp, 1. degC
184 *
185 * FAN PWMs have the following parameters:
186 * Reference Zone, 1, 2, 3, etc.
187 * Spinup time, .05 sec
188 * PWM value at limit/low temp, 1 count
189 * PWM Frequency, 1. Hz
190 * PWM is Min or OFF below limit, flag
191 * Invert PWM output, flag
192 *
193 * Some chips filter the temp, others the fan.
194 * Filter constant (or disabled) .1 seconds
195 */
196
197/* These are the zone temperature range encodings in .001 degree C */
198static int lm85_range_map[] = {
199 2000, 2500, 3300, 4000, 5000, 6600,
200 8000, 10000, 13300, 16000, 20000, 26600,
201 32000, 40000, 53300, 80000
202 };
203static int RANGE_TO_REG( int range )
204{
205 int i;
206
207 if ( range < lm85_range_map[0] ) {
208 return 0 ;
209 } else if ( range > lm85_range_map[15] ) {
210 return 15 ;
211 } else { /* find closest match */
212 for ( i = 14 ; i >= 0 ; --i ) {
213 if ( range > lm85_range_map[i] ) { /* range bracketed */
214 if ((lm85_range_map[i+1] - range) <
215 (range - lm85_range_map[i])) {
216 i++;
217 break;
218 }
219 break;
220 }
221 }
222 }
223 return( i & 0x0f );
224}
225#define RANGE_FROM_REG(val) (lm85_range_map[(val)&0x0f])
226
227/* These are the Acoustic Enhancement, or Temperature smoothing encodings
228 * NOTE: The enable/disable bit is INCLUDED in these encodings as the
229 * MSB (bit 3, value 8). If the enable bit is 0, the encoded value
230 * is ignored, or set to 0.
231 */
232/* These are the PWM frequency encodings */
233static int lm85_freq_map[] = { /* .1 Hz */
234 100, 150, 230, 300, 380, 470, 620, 940
235 };
236static int FREQ_TO_REG( int freq )
237{
238 int i;
239
240 if( freq >= lm85_freq_map[7] ) { return 7 ; }
241 for( i = 0 ; i < 7 ; ++i )
242 if( freq <= lm85_freq_map[i] )
243 break ;
244 return( i & 0x07 );
245}
246#define FREQ_FROM_REG(val) (lm85_freq_map[(val)&0x07])
247
248/* Since we can't use strings, I'm abusing these numbers
249 * to stand in for the following meanings:
250 * 1 -- PWM responds to Zone 1
251 * 2 -- PWM responds to Zone 2
252 * 3 -- PWM responds to Zone 3
253 * 23 -- PWM responds to the higher temp of Zone 2 or 3
254 * 123 -- PWM responds to highest of Zone 1, 2, or 3
255 * 0 -- PWM is always at 0% (ie, off)
256 * -1 -- PWM is always at 100%
257 * -2 -- PWM responds to manual control
258 */
259
260static int lm85_zone_map[] = { 1, 2, 3, -1, 0, 23, 123, -2 };
261#define ZONE_FROM_REG(val) (lm85_zone_map[((val)>>5)&0x07])
262
263static int ZONE_TO_REG( int zone )
264{
265 int i;
266
267 for( i = 0 ; i <= 7 ; ++i )
268 if( zone == lm85_zone_map[i] )
269 break ;
270 if( i > 7 ) /* Not found. */
271 i = 3; /* Always 100% */
272 return( (i & 0x07)<<5 );
273}
274
275#define HYST_TO_REG(val) (SENSORS_LIMIT(((val)+500)/1000,0,15))
276#define HYST_FROM_REG(val) ((val)*1000)
277
278#define OFFSET_TO_REG(val) (SENSORS_LIMIT((val)/25,-127,127))
279#define OFFSET_FROM_REG(val) ((val)*25)
280
281#define PPR_MASK(fan) (0x03<<(fan *2))
282#define PPR_TO_REG(val,fan) (SENSORS_LIMIT((val)-1,0,3)<<(fan *2))
283#define PPR_FROM_REG(val,fan) ((((val)>>(fan * 2))&0x03)+1)
284
285/* i2c-vid.h defines vid_from_reg() */
286#define VID_FROM_REG(val,vrm) (vid_from_reg((val),(vrm)))
287
288#define ALARMS_FROM_REG(val) (val)
289
290/* Unlike some other drivers we DO NOT set initial limits. Use
291 * the config file to set limits. Some users have reported
292 * motherboards shutting down when we set limits in a previous
293 * version of the driver.
294 */
295
296/* Chip sampling rates
297 *
298 * Some sensors are not updated more frequently than once per second
299 * so it doesn't make sense to read them more often than that.
300 * We cache the results and return the saved data if the driver
301 * is called again before a second has elapsed.
302 *
303 * Also, there is significant configuration data for this chip
304 * given the automatic PWM fan control that is possible. There
305 * are about 47 bytes of config data to only 22 bytes of actual
306 * readings. So, we keep the config data up to date in the cache
307 * when it is written and only sample it once every 1 *minute*
308 */
309#define LM85_DATA_INTERVAL (HZ + HZ / 2)
310#define LM85_CONFIG_INTERVAL (1 * 60 * HZ)
311
312/* For each registered LM85, we need to keep some data in memory. That
313 data is pointed to by lm85_list[NR]->data. The structure itself is
314 dynamically allocated, at the same time when a new lm85 client is
315 allocated. */
316
317/* LM85 can automatically adjust fan speeds based on temperature
318 * This structure encapsulates an entire Zone config. There are
319 * three zones (one for each temperature input) on the lm85
320 */
321struct lm85_zone {
322 s8 limit; /* Low temp limit */
323 u8 hyst; /* Low limit hysteresis. (0-15) */
324 u8 range; /* Temp range, encoded */
325 s8 critical; /* "All fans ON" temp limit */
326 u8 off_desired; /* Actual "off" temperature specified. Preserved
327 * to prevent "drift" as other autofan control
328 * values change.
329 */
330 u8 max_desired; /* Actual "max" temperature specified. Preserved
331 * to prevent "drift" as other autofan control
332 * values change.
333 */
334};
335
336struct lm85_autofan {
337 u8 config; /* Register value */
338 u8 freq; /* PWM frequency, encoded */
339 u8 min_pwm; /* Minimum PWM value, encoded */
340 u8 min_off; /* Min PWM or OFF below "limit", flag */
341};
342
343struct lm85_data {
344 struct i2c_client client;
345 struct semaphore lock;
346 enum chips type;
347
348 struct semaphore update_lock;
349 int valid; /* !=0 if following fields are valid */
350 unsigned long last_reading; /* In jiffies */
351 unsigned long last_config; /* In jiffies */
352
353 u8 in[8]; /* Register value */
354 u8 in_max[8]; /* Register value */
355 u8 in_min[8]; /* Register value */
356 s8 temp[3]; /* Register value */
357 s8 temp_min[3]; /* Register value */
358 s8 temp_max[3]; /* Register value */
359 s8 temp_offset[3]; /* Register value */
360 u16 fan[4]; /* Register value */
361 u16 fan_min[4]; /* Register value */
362 u8 pwm[3]; /* Register value */
363 u8 spinup_ctl; /* Register encoding, combined */
364 u8 tach_mode; /* Register encoding, combined */
365 u8 temp_ext[3]; /* Decoded values */
366 u8 in_ext[8]; /* Decoded values */
367 u8 adc_scale; /* ADC Extended bits scaling factor */
368 u8 fan_ppr; /* Register value */
369 u8 smooth[3]; /* Register encoding */
370 u8 vid; /* Register value */
371 u8 vrm; /* VRM version */
372 u8 syncpwm3; /* Saved PWM3 for TACH 2,3,4 config */
373 u8 oppoint[3]; /* Register value */
374 u16 tmin_ctl; /* Register value */
375 unsigned long therm_total; /* Cummulative therm count */
376 u8 therm_limit; /* Register value */
377 u32 alarms; /* Register encoding, combined */
378 struct lm85_autofan autofan[3];
379 struct lm85_zone zone[3];
380};
381
382static int lm85_attach_adapter(struct i2c_adapter *adapter);
383static int lm85_detect(struct i2c_adapter *adapter, int address,
384 int kind);
385static int lm85_detach_client(struct i2c_client *client);
386
387static int lm85_read_value(struct i2c_client *client, u8 register);
388static int lm85_write_value(struct i2c_client *client, u8 register, int value);
389static struct lm85_data *lm85_update_device(struct device *dev);
390static void lm85_init_client(struct i2c_client *client);
391
392
393static struct i2c_driver lm85_driver = {
394 .owner = THIS_MODULE,
395 .name = "lm85",
396 .id = I2C_DRIVERID_LM85,
397 .flags = I2C_DF_NOTIFY,
398 .attach_adapter = lm85_attach_adapter,
399 .detach_client = lm85_detach_client,
400};
401
402
403/* 4 Fans */
404static ssize_t show_fan(struct device *dev, char *buf, int nr)
405{
406 struct lm85_data *data = lm85_update_device(dev);
407 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan[nr]) );
408}
409static ssize_t show_fan_min(struct device *dev, char *buf, int nr)
410{
411 struct lm85_data *data = lm85_update_device(dev);
412 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan_min[nr]) );
413}
414static ssize_t set_fan_min(struct device *dev, const char *buf,
415 size_t count, int nr)
416{
417 struct i2c_client *client = to_i2c_client(dev);
418 struct lm85_data *data = i2c_get_clientdata(client);
419 long val = simple_strtol(buf, NULL, 10);
420
421 down(&data->update_lock);
422 data->fan_min[nr] = FAN_TO_REG(val);
423 lm85_write_value(client, LM85_REG_FAN_MIN(nr), data->fan_min[nr]);
424 up(&data->update_lock);
425 return count;
426}
427
428#define show_fan_offset(offset) \
429static ssize_t show_fan_##offset (struct device *dev, char *buf) \
430{ \
431 return show_fan(dev, buf, offset - 1); \
432} \
433static ssize_t show_fan_##offset##_min (struct device *dev, char *buf) \
434{ \
435 return show_fan_min(dev, buf, offset - 1); \
436} \
437static ssize_t set_fan_##offset##_min (struct device *dev, \
438 const char *buf, size_t count) \
439{ \
440 return set_fan_min(dev, buf, count, offset - 1); \
441} \
442static DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_fan_##offset, \
443 NULL); \
444static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
445 show_fan_##offset##_min, set_fan_##offset##_min);
446
447show_fan_offset(1);
448show_fan_offset(2);
449show_fan_offset(3);
450show_fan_offset(4);
451
452/* vid, vrm, alarms */
453
454static ssize_t show_vid_reg(struct device *dev, char *buf)
455{
456 struct lm85_data *data = lm85_update_device(dev);
457 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
458}
459
460static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
461
462static ssize_t show_vrm_reg(struct device *dev, char *buf)
463{
464 struct lm85_data *data = lm85_update_device(dev);
465 return sprintf(buf, "%ld\n", (long) data->vrm);
466}
467
468static ssize_t store_vrm_reg(struct device *dev, const char *buf, size_t count)
469{
470 struct i2c_client *client = to_i2c_client(dev);
471 struct lm85_data *data = i2c_get_clientdata(client);
472 u32 val;
473
474 val = simple_strtoul(buf, NULL, 10);
475 data->vrm = val;
476 return count;
477}
478
479static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
480
481static ssize_t show_alarms_reg(struct device *dev, char *buf)
482{
483 struct lm85_data *data = lm85_update_device(dev);
484 return sprintf(buf, "%ld\n", (long) ALARMS_FROM_REG(data->alarms));
485}
486
487static DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL);
488
489/* pwm */
490
491static ssize_t show_pwm(struct device *dev, char *buf, int nr)
492{
493 struct lm85_data *data = lm85_update_device(dev);
494 return sprintf(buf,"%d\n", PWM_FROM_REG(data->pwm[nr]) );
495}
496static ssize_t set_pwm(struct device *dev, const char *buf,
497 size_t count, int nr)
498{
499 struct i2c_client *client = to_i2c_client(dev);
500 struct lm85_data *data = i2c_get_clientdata(client);
501 long val = simple_strtol(buf, NULL, 10);
502
503 down(&data->update_lock);
504 data->pwm[nr] = PWM_TO_REG(val);
505 lm85_write_value(client, LM85_REG_PWM(nr), data->pwm[nr]);
506 up(&data->update_lock);
507 return count;
508}
509static ssize_t show_pwm_enable(struct device *dev, char *buf, int nr)
510{
511 struct lm85_data *data = lm85_update_device(dev);
512 int pwm_zone;
513
514 pwm_zone = ZONE_FROM_REG(data->autofan[nr].config);
515 return sprintf(buf,"%d\n", (pwm_zone != 0 && pwm_zone != -1) );
516}
517
518#define show_pwm_reg(offset) \
519static ssize_t show_pwm_##offset (struct device *dev, char *buf) \
520{ \
521 return show_pwm(dev, buf, offset - 1); \
522} \
523static ssize_t set_pwm_##offset (struct device *dev, \
524 const char *buf, size_t count) \
525{ \
526 return set_pwm(dev, buf, count, offset - 1); \
527} \
528static ssize_t show_pwm_enable##offset (struct device *dev, char *buf) \
529{ \
530 return show_pwm_enable(dev, buf, offset - 1); \
531} \
532static DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
533 show_pwm_##offset, set_pwm_##offset); \
534static DEVICE_ATTR(pwm##offset##_enable, S_IRUGO, \
535 show_pwm_enable##offset, NULL);
536
537show_pwm_reg(1);
538show_pwm_reg(2);
539show_pwm_reg(3);
540
541/* Voltages */
542
543static ssize_t show_in(struct device *dev, char *buf, int nr)
544{
545 struct lm85_data *data = lm85_update_device(dev);
546 return sprintf( buf, "%d\n", INSEXT_FROM_REG(nr,
547 data->in[nr],
548 data->in_ext[nr],
549 data->adc_scale) );
550}
551static ssize_t show_in_min(struct device *dev, char *buf, int nr)
552{
553 struct lm85_data *data = lm85_update_device(dev);
554 return sprintf(buf,"%d\n", INS_FROM_REG(nr, data->in_min[nr]) );
555}
556static ssize_t set_in_min(struct device *dev, const char *buf,
557 size_t count, int nr)
558{
559 struct i2c_client *client = to_i2c_client(dev);
560 struct lm85_data *data = i2c_get_clientdata(client);
561 long val = simple_strtol(buf, NULL, 10);
562
563 down(&data->update_lock);
564 data->in_min[nr] = INS_TO_REG(nr, val);
565 lm85_write_value(client, LM85_REG_IN_MIN(nr), data->in_min[nr]);
566 up(&data->update_lock);
567 return count;
568}
569static ssize_t show_in_max(struct device *dev, char *buf, int nr)
570{
571 struct lm85_data *data = lm85_update_device(dev);
572 return sprintf(buf,"%d\n", INS_FROM_REG(nr, data->in_max[nr]) );
573}
574static ssize_t set_in_max(struct device *dev, const char *buf,
575 size_t count, int nr)
576{
577 struct i2c_client *client = to_i2c_client(dev);
578 struct lm85_data *data = i2c_get_clientdata(client);
579 long val = simple_strtol(buf, NULL, 10);
580
581 down(&data->update_lock);
582 data->in_max[nr] = INS_TO_REG(nr, val);
583 lm85_write_value(client, LM85_REG_IN_MAX(nr), data->in_max[nr]);
584 up(&data->update_lock);
585 return count;
586}
587#define show_in_reg(offset) \
588static ssize_t show_in_##offset (struct device *dev, char *buf) \
589{ \
590 return show_in(dev, buf, offset); \
591} \
592static ssize_t show_in_##offset##_min (struct device *dev, char *buf) \
593{ \
594 return show_in_min(dev, buf, offset); \
595} \
596static ssize_t show_in_##offset##_max (struct device *dev, char *buf) \
597{ \
598 return show_in_max(dev, buf, offset); \
599} \
600static ssize_t set_in_##offset##_min (struct device *dev, \
601 const char *buf, size_t count) \
602{ \
603 return set_in_min(dev, buf, count, offset); \
604} \
605static ssize_t set_in_##offset##_max (struct device *dev, \
606 const char *buf, size_t count) \
607{ \
608 return set_in_max(dev, buf, count, offset); \
609} \
610static DEVICE_ATTR(in##offset##_input, S_IRUGO, show_in_##offset, \
611 NULL); \
612static DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
613 show_in_##offset##_min, set_in_##offset##_min); \
614static DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
615 show_in_##offset##_max, set_in_##offset##_max);
616
617show_in_reg(0);
618show_in_reg(1);
619show_in_reg(2);
620show_in_reg(3);
621show_in_reg(4);
622
623/* Temps */
624
625static ssize_t show_temp(struct device *dev, char *buf, int nr)
626{
627 struct lm85_data *data = lm85_update_device(dev);
628 return sprintf(buf,"%d\n", TEMPEXT_FROM_REG(data->temp[nr],
629 data->temp_ext[nr],
630 data->adc_scale) );
631}
632static ssize_t show_temp_min(struct device *dev, char *buf, int nr)
633{
634 struct lm85_data *data = lm85_update_device(dev);
635 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->temp_min[nr]) );
636}
637static ssize_t set_temp_min(struct device *dev, const char *buf,
638 size_t count, int nr)
639{
640 struct i2c_client *client = to_i2c_client(dev);
641 struct lm85_data *data = i2c_get_clientdata(client);
642 long val = simple_strtol(buf, NULL, 10);
643
644 down(&data->update_lock);
645 data->temp_min[nr] = TEMP_TO_REG(val);
646 lm85_write_value(client, LM85_REG_TEMP_MIN(nr), data->temp_min[nr]);
647 up(&data->update_lock);
648 return count;
649}
650static ssize_t show_temp_max(struct device *dev, char *buf, int nr)
651{
652 struct lm85_data *data = lm85_update_device(dev);
653 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->temp_max[nr]) );
654}
655static ssize_t set_temp_max(struct device *dev, const char *buf,
656 size_t count, int nr)
657{
658 struct i2c_client *client = to_i2c_client(dev);
659 struct lm85_data *data = i2c_get_clientdata(client);
660 long val = simple_strtol(buf, NULL, 10);
661
662 down(&data->update_lock);
663 data->temp_max[nr] = TEMP_TO_REG(val);
664 lm85_write_value(client, LM85_REG_TEMP_MAX(nr), data->temp_max[nr]);
665 up(&data->update_lock);
666 return count;
667}
668#define show_temp_reg(offset) \
669static ssize_t show_temp_##offset (struct device *dev, char *buf) \
670{ \
671 return show_temp(dev, buf, offset - 1); \
672} \
673static ssize_t show_temp_##offset##_min (struct device *dev, char *buf) \
674{ \
675 return show_temp_min(dev, buf, offset - 1); \
676} \
677static ssize_t show_temp_##offset##_max (struct device *dev, char *buf) \
678{ \
679 return show_temp_max(dev, buf, offset - 1); \
680} \
681static ssize_t set_temp_##offset##_min (struct device *dev, \
682 const char *buf, size_t count) \
683{ \
684 return set_temp_min(dev, buf, count, offset - 1); \
685} \
686static ssize_t set_temp_##offset##_max (struct device *dev, \
687 const char *buf, size_t count) \
688{ \
689 return set_temp_max(dev, buf, count, offset - 1); \
690} \
691static DEVICE_ATTR(temp##offset##_input, S_IRUGO, show_temp_##offset, \
692 NULL); \
693static DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
694 show_temp_##offset##_min, set_temp_##offset##_min); \
695static DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
696 show_temp_##offset##_max, set_temp_##offset##_max);
697
698show_temp_reg(1);
699show_temp_reg(2);
700show_temp_reg(3);
701
702
703/* Automatic PWM control */
704
705static ssize_t show_pwm_auto_channels(struct device *dev, char *buf, int nr)
706{
707 struct lm85_data *data = lm85_update_device(dev);
708 return sprintf(buf,"%d\n", ZONE_FROM_REG(data->autofan[nr].config));
709}
710static ssize_t set_pwm_auto_channels(struct device *dev, const char *buf,
711 size_t count, int nr)
712{
713 struct i2c_client *client = to_i2c_client(dev);
714 struct lm85_data *data = i2c_get_clientdata(client);
715 long val = simple_strtol(buf, NULL, 10);
716
717 down(&data->update_lock);
718 data->autofan[nr].config = (data->autofan[nr].config & (~0xe0))
719 | ZONE_TO_REG(val) ;
720 lm85_write_value(client, LM85_REG_AFAN_CONFIG(nr),
721 data->autofan[nr].config);
722 up(&data->update_lock);
723 return count;
724}
725static ssize_t show_pwm_auto_pwm_min(struct device *dev, char *buf, int nr)
726{
727 struct lm85_data *data = lm85_update_device(dev);
728 return sprintf(buf,"%d\n", PWM_FROM_REG(data->autofan[nr].min_pwm));
729}
730static ssize_t set_pwm_auto_pwm_min(struct device *dev, const char *buf,
731 size_t count, int nr)
732{
733 struct i2c_client *client = to_i2c_client(dev);
734 struct lm85_data *data = i2c_get_clientdata(client);
735 long val = simple_strtol(buf, NULL, 10);
736
737 down(&data->update_lock);
738 data->autofan[nr].min_pwm = PWM_TO_REG(val);
739 lm85_write_value(client, LM85_REG_AFAN_MINPWM(nr),
740 data->autofan[nr].min_pwm);
741 up(&data->update_lock);
742 return count;
743}
744static ssize_t show_pwm_auto_pwm_minctl(struct device *dev, char *buf, int nr)
745{
746 struct lm85_data *data = lm85_update_device(dev);
747 return sprintf(buf,"%d\n", data->autofan[nr].min_off);
748}
749static ssize_t set_pwm_auto_pwm_minctl(struct device *dev, const char *buf,
750 size_t count, int nr)
751{
752 struct i2c_client *client = to_i2c_client(dev);
753 struct lm85_data *data = i2c_get_clientdata(client);
754 long val = simple_strtol(buf, NULL, 10);
755
756 down(&data->update_lock);
757 data->autofan[nr].min_off = val;
758 lm85_write_value(client, LM85_REG_AFAN_SPIKE1, data->smooth[0]
759 | data->syncpwm3
760 | (data->autofan[0].min_off ? 0x20 : 0)
761 | (data->autofan[1].min_off ? 0x40 : 0)
762 | (data->autofan[2].min_off ? 0x80 : 0)
763 );
764 up(&data->update_lock);
765 return count;
766}
767static ssize_t show_pwm_auto_pwm_freq(struct device *dev, char *buf, int nr)
768{
769 struct lm85_data *data = lm85_update_device(dev);
770 return sprintf(buf,"%d\n", FREQ_FROM_REG(data->autofan[nr].freq));
771}
772static ssize_t set_pwm_auto_pwm_freq(struct device *dev, const char *buf,
773 size_t count, int nr)
774{
775 struct i2c_client *client = to_i2c_client(dev);
776 struct lm85_data *data = i2c_get_clientdata(client);
777 long val = simple_strtol(buf, NULL, 10);
778
779 down(&data->update_lock);
780 data->autofan[nr].freq = FREQ_TO_REG(val);
781 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
782 (data->zone[nr].range << 4)
783 | data->autofan[nr].freq
784 );
785 up(&data->update_lock);
786 return count;
787}
788#define pwm_auto(offset) \
789static ssize_t show_pwm##offset##_auto_channels (struct device *dev, \
790 char *buf) \
791{ \
792 return show_pwm_auto_channels(dev, buf, offset - 1); \
793} \
794static ssize_t set_pwm##offset##_auto_channels (struct device *dev, \
795 const char *buf, size_t count) \
796{ \
797 return set_pwm_auto_channels(dev, buf, count, offset - 1); \
798} \
799static ssize_t show_pwm##offset##_auto_pwm_min (struct device *dev, \
800 char *buf) \
801{ \
802 return show_pwm_auto_pwm_min(dev, buf, offset - 1); \
803} \
804static ssize_t set_pwm##offset##_auto_pwm_min (struct device *dev, \
805 const char *buf, size_t count) \
806{ \
807 return set_pwm_auto_pwm_min(dev, buf, count, offset - 1); \
808} \
809static ssize_t show_pwm##offset##_auto_pwm_minctl (struct device *dev, \
810 char *buf) \
811{ \
812 return show_pwm_auto_pwm_minctl(dev, buf, offset - 1); \
813} \
814static ssize_t set_pwm##offset##_auto_pwm_minctl (struct device *dev, \
815 const char *buf, size_t count) \
816{ \
817 return set_pwm_auto_pwm_minctl(dev, buf, count, offset - 1); \
818} \
819static ssize_t show_pwm##offset##_auto_pwm_freq (struct device *dev, \
820 char *buf) \
821{ \
822 return show_pwm_auto_pwm_freq(dev, buf, offset - 1); \
823} \
824static ssize_t set_pwm##offset##_auto_pwm_freq(struct device *dev, \
825 const char *buf, size_t count) \
826{ \
827 return set_pwm_auto_pwm_freq(dev, buf, count, offset - 1); \
828} \
829static DEVICE_ATTR(pwm##offset##_auto_channels, S_IRUGO | S_IWUSR, \
830 show_pwm##offset##_auto_channels, \
831 set_pwm##offset##_auto_channels); \
832static DEVICE_ATTR(pwm##offset##_auto_pwm_min, S_IRUGO | S_IWUSR, \
833 show_pwm##offset##_auto_pwm_min, \
834 set_pwm##offset##_auto_pwm_min); \
835static DEVICE_ATTR(pwm##offset##_auto_pwm_minctl, S_IRUGO | S_IWUSR, \
836 show_pwm##offset##_auto_pwm_minctl, \
837 set_pwm##offset##_auto_pwm_minctl); \
838static DEVICE_ATTR(pwm##offset##_auto_pwm_freq, S_IRUGO | S_IWUSR, \
839 show_pwm##offset##_auto_pwm_freq, \
840 set_pwm##offset##_auto_pwm_freq);
841pwm_auto(1);
842pwm_auto(2);
843pwm_auto(3);
844
845/* Temperature settings for automatic PWM control */
846
847static ssize_t show_temp_auto_temp_off(struct device *dev, char *buf, int nr)
848{
849 struct lm85_data *data = lm85_update_device(dev);
850 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->zone[nr].limit) -
851 HYST_FROM_REG(data->zone[nr].hyst));
852}
853static ssize_t set_temp_auto_temp_off(struct device *dev, const char *buf,
854 size_t count, int nr)
855{
856 struct i2c_client *client = to_i2c_client(dev);
857 struct lm85_data *data = i2c_get_clientdata(client);
858 int min;
859 long val = simple_strtol(buf, NULL, 10);
860
861 down(&data->update_lock);
862 min = TEMP_FROM_REG(data->zone[nr].limit);
863 data->zone[nr].off_desired = TEMP_TO_REG(val);
864 data->zone[nr].hyst = HYST_TO_REG(min - val);
865 if ( nr == 0 || nr == 1 ) {
866 lm85_write_value(client, LM85_REG_AFAN_HYST1,
867 (data->zone[0].hyst << 4)
868 | data->zone[1].hyst
869 );
870 } else {
871 lm85_write_value(client, LM85_REG_AFAN_HYST2,
872 (data->zone[2].hyst << 4)
873 );
874 }
875 up(&data->update_lock);
876 return count;
877}
878static ssize_t show_temp_auto_temp_min(struct device *dev, char *buf, int nr)
879{
880 struct lm85_data *data = lm85_update_device(dev);
881 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->zone[nr].limit) );
882}
883static ssize_t set_temp_auto_temp_min(struct device *dev, const char *buf,
884 size_t count, int nr)
885{
886 struct i2c_client *client = to_i2c_client(dev);
887 struct lm85_data *data = i2c_get_clientdata(client);
888 long val = simple_strtol(buf, NULL, 10);
889
890 down(&data->update_lock);
891 data->zone[nr].limit = TEMP_TO_REG(val);
892 lm85_write_value(client, LM85_REG_AFAN_LIMIT(nr),
893 data->zone[nr].limit);
894
895/* Update temp_auto_max and temp_auto_range */
896 data->zone[nr].range = RANGE_TO_REG(
897 TEMP_FROM_REG(data->zone[nr].max_desired) -
898 TEMP_FROM_REG(data->zone[nr].limit));
899 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
900 ((data->zone[nr].range & 0x0f) << 4)
901 | (data->autofan[nr].freq & 0x07));
902
903/* Update temp_auto_hyst and temp_auto_off */
904 data->zone[nr].hyst = HYST_TO_REG(TEMP_FROM_REG(
905 data->zone[nr].limit) - TEMP_FROM_REG(
906 data->zone[nr].off_desired));
907 if ( nr == 0 || nr == 1 ) {
908 lm85_write_value(client, LM85_REG_AFAN_HYST1,
909 (data->zone[0].hyst << 4)
910 | data->zone[1].hyst
911 );
912 } else {
913 lm85_write_value(client, LM85_REG_AFAN_HYST2,
914 (data->zone[2].hyst << 4)
915 );
916 }
917 up(&data->update_lock);
918 return count;
919}
920static ssize_t show_temp_auto_temp_max(struct device *dev, char *buf, int nr)
921{
922 struct lm85_data *data = lm85_update_device(dev);
923 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->zone[nr].limit) +
924 RANGE_FROM_REG(data->zone[nr].range));
925}
926static ssize_t set_temp_auto_temp_max(struct device *dev, const char *buf,
927 size_t count, int nr)
928{
929 struct i2c_client *client = to_i2c_client(dev);
930 struct lm85_data *data = i2c_get_clientdata(client);
931 int min;
932 long val = simple_strtol(buf, NULL, 10);
933
934 down(&data->update_lock);
935 min = TEMP_FROM_REG(data->zone[nr].limit);
936 data->zone[nr].max_desired = TEMP_TO_REG(val);
937 data->zone[nr].range = RANGE_TO_REG(
938 val - min);
939 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
940 ((data->zone[nr].range & 0x0f) << 4)
941 | (data->autofan[nr].freq & 0x07));
942 up(&data->update_lock);
943 return count;
944}
945static ssize_t show_temp_auto_temp_crit(struct device *dev, char *buf, int nr)
946{
947 struct lm85_data *data = lm85_update_device(dev);
948 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->zone[nr].critical));
949}
950static ssize_t set_temp_auto_temp_crit(struct device *dev, const char *buf,
951 size_t count, int nr)
952{
953 struct i2c_client *client = to_i2c_client(dev);
954 struct lm85_data *data = i2c_get_clientdata(client);
955 long val = simple_strtol(buf, NULL, 10);
956
957 down(&data->update_lock);
958 data->zone[nr].critical = TEMP_TO_REG(val);
959 lm85_write_value(client, LM85_REG_AFAN_CRITICAL(nr),
960 data->zone[nr].critical);
961 up(&data->update_lock);
962 return count;
963}
964#define temp_auto(offset) \
965static ssize_t show_temp##offset##_auto_temp_off (struct device *dev, \
966 char *buf) \
967{ \
968 return show_temp_auto_temp_off(dev, buf, offset - 1); \
969} \
970static ssize_t set_temp##offset##_auto_temp_off (struct device *dev, \
971 const char *buf, size_t count) \
972{ \
973 return set_temp_auto_temp_off(dev, buf, count, offset - 1); \
974} \
975static ssize_t show_temp##offset##_auto_temp_min (struct device *dev, \
976 char *buf) \
977{ \
978 return show_temp_auto_temp_min(dev, buf, offset - 1); \
979} \
980static ssize_t set_temp##offset##_auto_temp_min (struct device *dev, \
981 const char *buf, size_t count) \
982{ \
983 return set_temp_auto_temp_min(dev, buf, count, offset - 1); \
984} \
985static ssize_t show_temp##offset##_auto_temp_max (struct device *dev, \
986 char *buf) \
987{ \
988 return show_temp_auto_temp_max(dev, buf, offset - 1); \
989} \
990static ssize_t set_temp##offset##_auto_temp_max (struct device *dev, \
991 const char *buf, size_t count) \
992{ \
993 return set_temp_auto_temp_max(dev, buf, count, offset - 1); \
994} \
995static ssize_t show_temp##offset##_auto_temp_crit (struct device *dev, \
996 char *buf) \
997{ \
998 return show_temp_auto_temp_crit(dev, buf, offset - 1); \
999} \
1000static ssize_t set_temp##offset##_auto_temp_crit (struct device *dev, \
1001 const char *buf, size_t count) \
1002{ \
1003 return set_temp_auto_temp_crit(dev, buf, count, offset - 1); \
1004} \
1005static DEVICE_ATTR(temp##offset##_auto_temp_off, S_IRUGO | S_IWUSR, \
1006 show_temp##offset##_auto_temp_off, \
1007 set_temp##offset##_auto_temp_off); \
1008static DEVICE_ATTR(temp##offset##_auto_temp_min, S_IRUGO | S_IWUSR, \
1009 show_temp##offset##_auto_temp_min, \
1010 set_temp##offset##_auto_temp_min); \
1011static DEVICE_ATTR(temp##offset##_auto_temp_max, S_IRUGO | S_IWUSR, \
1012 show_temp##offset##_auto_temp_max, \
1013 set_temp##offset##_auto_temp_max); \
1014static DEVICE_ATTR(temp##offset##_auto_temp_crit, S_IRUGO | S_IWUSR, \
1015 show_temp##offset##_auto_temp_crit, \
1016 set_temp##offset##_auto_temp_crit);
1017temp_auto(1);
1018temp_auto(2);
1019temp_auto(3);
1020
1021int lm85_attach_adapter(struct i2c_adapter *adapter)
1022{
1023 if (!(adapter->class & I2C_CLASS_HWMON))
1024 return 0;
1025 return i2c_detect(adapter, &addr_data, lm85_detect);
1026}
1027
1028int lm85_detect(struct i2c_adapter *adapter, int address,
1029 int kind)
1030{
1031 int company, verstep ;
1032 struct i2c_client *new_client = NULL;
1033 struct lm85_data *data;
1034 int err = 0;
1035 const char *type_name = "";
1036
1037 if (i2c_is_isa_adapter(adapter)) {
1038 /* This chip has no ISA interface */
1039 goto ERROR0 ;
1040 };
1041
1042 if (!i2c_check_functionality(adapter,
1043 I2C_FUNC_SMBUS_BYTE_DATA)) {
1044 /* We need to be able to do byte I/O */
1045 goto ERROR0 ;
1046 };
1047
1048 /* OK. For now, we presume we have a valid client. We now create the
1049 client structure, even though we cannot fill it completely yet.
1050 But it allows us to access lm85_{read,write}_value. */
1051
1052 if (!(data = kmalloc(sizeof(struct lm85_data), GFP_KERNEL))) {
1053 err = -ENOMEM;
1054 goto ERROR0;
1055 }
1056 memset(data, 0, sizeof(struct lm85_data));
1057
1058 new_client = &data->client;
1059 i2c_set_clientdata(new_client, data);
1060 new_client->addr = address;
1061 new_client->adapter = adapter;
1062 new_client->driver = &lm85_driver;
1063 new_client->flags = 0;
1064
1065 /* Now, we do the remaining detection. */
1066
1067 company = lm85_read_value(new_client, LM85_REG_COMPANY);
1068 verstep = lm85_read_value(new_client, LM85_REG_VERSTEP);
1069
1070 dev_dbg(&adapter->dev, "Detecting device at %d,0x%02x with"
1071 " COMPANY: 0x%02x and VERSTEP: 0x%02x\n",
1072 i2c_adapter_id(new_client->adapter), new_client->addr,
1073 company, verstep);
1074
1075 /* If auto-detecting, Determine the chip type. */
1076 if (kind <= 0) {
1077 dev_dbg(&adapter->dev, "Autodetecting device at %d,0x%02x ...\n",
1078 i2c_adapter_id(adapter), address );
1079 if( company == LM85_COMPANY_NATIONAL
1080 && verstep == LM85_VERSTEP_LM85C ) {
1081 kind = lm85c ;
1082 } else if( company == LM85_COMPANY_NATIONAL
1083 && verstep == LM85_VERSTEP_LM85B ) {
1084 kind = lm85b ;
1085 } else if( company == LM85_COMPANY_NATIONAL
1086 && (verstep & LM85_VERSTEP_VMASK) == LM85_VERSTEP_GENERIC ) {
1087 dev_err(&adapter->dev, "Unrecognized version/stepping 0x%02x"
1088 " Defaulting to LM85.\n", verstep);
1089 kind = any_chip ;
1090 } else if( company == LM85_COMPANY_ANALOG_DEV
1091 && verstep == LM85_VERSTEP_ADM1027 ) {
1092 kind = adm1027 ;
1093 } else if( company == LM85_COMPANY_ANALOG_DEV
1094 && (verstep == LM85_VERSTEP_ADT7463
1095 || verstep == LM85_VERSTEP_ADT7463C) ) {
1096 kind = adt7463 ;
1097 } else if( company == LM85_COMPANY_ANALOG_DEV
1098 && (verstep & LM85_VERSTEP_VMASK) == LM85_VERSTEP_GENERIC ) {
1099 dev_err(&adapter->dev, "Unrecognized version/stepping 0x%02x"
1100 " Defaulting to Generic LM85.\n", verstep );
1101 kind = any_chip ;
1102 } else if( company == LM85_COMPANY_SMSC
1103 && (verstep == LM85_VERSTEP_EMC6D100_A0
1104 || verstep == LM85_VERSTEP_EMC6D100_A1) ) {
1105 /* Unfortunately, we can't tell a '100 from a '101
1106 * from the registers. Since a '101 is a '100
1107 * in a package with fewer pins and therefore no
1108 * 3.3V, 1.5V or 1.8V inputs, perhaps if those
1109 * inputs read 0, then it's a '101.
1110 */
1111 kind = emc6d100 ;
1112 } else if( company == LM85_COMPANY_SMSC
1113 && verstep == LM85_VERSTEP_EMC6D102) {
1114 kind = emc6d102 ;
1115 } else if( company == LM85_COMPANY_SMSC
1116 && (verstep & LM85_VERSTEP_VMASK) == LM85_VERSTEP_GENERIC) {
1117 dev_err(&adapter->dev, "lm85: Detected SMSC chip\n");
1118 dev_err(&adapter->dev, "lm85: Unrecognized version/stepping 0x%02x"
1119 " Defaulting to Generic LM85.\n", verstep );
1120 kind = any_chip ;
1121 } else if( kind == any_chip
1122 && (verstep & LM85_VERSTEP_VMASK) == LM85_VERSTEP_GENERIC) {
1123 dev_err(&adapter->dev, "Generic LM85 Version 6 detected\n");
1124 /* Leave kind as "any_chip" */
1125 } else {
1126 dev_dbg(&adapter->dev, "Autodetection failed\n");
1127 /* Not an LM85 ... */
1128 if( kind == any_chip ) { /* User used force=x,y */
1129 dev_err(&adapter->dev, "Generic LM85 Version 6 not"
1130 " found at %d,0x%02x. Try force_lm85c.\n",
1131 i2c_adapter_id(adapter), address );
1132 }
1133 err = 0 ;
1134 goto ERROR1;
1135 }
1136 }
1137
1138 /* Fill in the chip specific driver values */
1139 if ( kind == any_chip ) {
1140 type_name = "lm85";
1141 } else if ( kind == lm85b ) {
1142 type_name = "lm85b";
1143 } else if ( kind == lm85c ) {
1144 type_name = "lm85c";
1145 } else if ( kind == adm1027 ) {
1146 type_name = "adm1027";
1147 } else if ( kind == adt7463 ) {
1148 type_name = "adt7463";
1149 } else if ( kind == emc6d100){
1150 type_name = "emc6d100";
1151 } else if ( kind == emc6d102 ) {
1152 type_name = "emc6d102";
1153 }
1154 strlcpy(new_client->name, type_name, I2C_NAME_SIZE);
1155
1156 /* Fill in the remaining client fields */
1157 data->type = kind;
1158 data->valid = 0;
1159 init_MUTEX(&data->update_lock);
1160
1161 /* Tell the I2C layer a new client has arrived */
1162 if ((err = i2c_attach_client(new_client)))
1163 goto ERROR1;
1164
1165 /* Set the VRM version */
1166 data->vrm = i2c_which_vrm();
1167
1168 /* Initialize the LM85 chip */
1169 lm85_init_client(new_client);
1170
1171 /* Register sysfs hooks */
1172 device_create_file(&new_client->dev, &dev_attr_fan1_input);
1173 device_create_file(&new_client->dev, &dev_attr_fan2_input);
1174 device_create_file(&new_client->dev, &dev_attr_fan3_input);
1175 device_create_file(&new_client->dev, &dev_attr_fan4_input);
1176 device_create_file(&new_client->dev, &dev_attr_fan1_min);
1177 device_create_file(&new_client->dev, &dev_attr_fan2_min);
1178 device_create_file(&new_client->dev, &dev_attr_fan3_min);
1179 device_create_file(&new_client->dev, &dev_attr_fan4_min);
1180 device_create_file(&new_client->dev, &dev_attr_pwm1);
1181 device_create_file(&new_client->dev, &dev_attr_pwm2);
1182 device_create_file(&new_client->dev, &dev_attr_pwm3);
1183 device_create_file(&new_client->dev, &dev_attr_pwm1_enable);
1184 device_create_file(&new_client->dev, &dev_attr_pwm2_enable);
1185 device_create_file(&new_client->dev, &dev_attr_pwm3_enable);
1186 device_create_file(&new_client->dev, &dev_attr_in0_input);
1187 device_create_file(&new_client->dev, &dev_attr_in1_input);
1188 device_create_file(&new_client->dev, &dev_attr_in2_input);
1189 device_create_file(&new_client->dev, &dev_attr_in3_input);
1190 device_create_file(&new_client->dev, &dev_attr_in4_input);
1191 device_create_file(&new_client->dev, &dev_attr_in0_min);
1192 device_create_file(&new_client->dev, &dev_attr_in1_min);
1193 device_create_file(&new_client->dev, &dev_attr_in2_min);
1194 device_create_file(&new_client->dev, &dev_attr_in3_min);
1195 device_create_file(&new_client->dev, &dev_attr_in4_min);
1196 device_create_file(&new_client->dev, &dev_attr_in0_max);
1197 device_create_file(&new_client->dev, &dev_attr_in1_max);
1198 device_create_file(&new_client->dev, &dev_attr_in2_max);
1199 device_create_file(&new_client->dev, &dev_attr_in3_max);
1200 device_create_file(&new_client->dev, &dev_attr_in4_max);
1201 device_create_file(&new_client->dev, &dev_attr_temp1_input);
1202 device_create_file(&new_client->dev, &dev_attr_temp2_input);
1203 device_create_file(&new_client->dev, &dev_attr_temp3_input);
1204 device_create_file(&new_client->dev, &dev_attr_temp1_min);
1205 device_create_file(&new_client->dev, &dev_attr_temp2_min);
1206 device_create_file(&new_client->dev, &dev_attr_temp3_min);
1207 device_create_file(&new_client->dev, &dev_attr_temp1_max);
1208 device_create_file(&new_client->dev, &dev_attr_temp2_max);
1209 device_create_file(&new_client->dev, &dev_attr_temp3_max);
1210 device_create_file(&new_client->dev, &dev_attr_vrm);
1211 device_create_file(&new_client->dev, &dev_attr_cpu0_vid);
1212 device_create_file(&new_client->dev, &dev_attr_alarms);
1213 device_create_file(&new_client->dev, &dev_attr_pwm1_auto_channels);
1214 device_create_file(&new_client->dev, &dev_attr_pwm2_auto_channels);
1215 device_create_file(&new_client->dev, &dev_attr_pwm3_auto_channels);
1216 device_create_file(&new_client->dev, &dev_attr_pwm1_auto_pwm_min);
1217 device_create_file(&new_client->dev, &dev_attr_pwm2_auto_pwm_min);
1218 device_create_file(&new_client->dev, &dev_attr_pwm3_auto_pwm_min);
1219 device_create_file(&new_client->dev, &dev_attr_pwm1_auto_pwm_minctl);
1220 device_create_file(&new_client->dev, &dev_attr_pwm2_auto_pwm_minctl);
1221 device_create_file(&new_client->dev, &dev_attr_pwm3_auto_pwm_minctl);
1222 device_create_file(&new_client->dev, &dev_attr_pwm1_auto_pwm_freq);
1223 device_create_file(&new_client->dev, &dev_attr_pwm2_auto_pwm_freq);
1224 device_create_file(&new_client->dev, &dev_attr_pwm3_auto_pwm_freq);
1225 device_create_file(&new_client->dev, &dev_attr_temp1_auto_temp_off);
1226 device_create_file(&new_client->dev, &dev_attr_temp2_auto_temp_off);
1227 device_create_file(&new_client->dev, &dev_attr_temp3_auto_temp_off);
1228 device_create_file(&new_client->dev, &dev_attr_temp1_auto_temp_min);
1229 device_create_file(&new_client->dev, &dev_attr_temp2_auto_temp_min);
1230 device_create_file(&new_client->dev, &dev_attr_temp3_auto_temp_min);
1231 device_create_file(&new_client->dev, &dev_attr_temp1_auto_temp_max);
1232 device_create_file(&new_client->dev, &dev_attr_temp2_auto_temp_max);
1233 device_create_file(&new_client->dev, &dev_attr_temp3_auto_temp_max);
1234 device_create_file(&new_client->dev, &dev_attr_temp1_auto_temp_crit);
1235 device_create_file(&new_client->dev, &dev_attr_temp2_auto_temp_crit);
1236 device_create_file(&new_client->dev, &dev_attr_temp3_auto_temp_crit);
1237
1238 return 0;
1239
1240 /* Error out and cleanup code */
1241 ERROR1:
1242 kfree(data);
1243 ERROR0:
1244 return err;
1245}
1246
1247int lm85_detach_client(struct i2c_client *client)
1248{
1249 i2c_detach_client(client);
1250 kfree(i2c_get_clientdata(client));
1251 return 0;
1252}
1253
1254
1255int lm85_read_value(struct i2c_client *client, u8 reg)
1256{
1257 int res;
1258
1259 /* What size location is it? */
1260 switch( reg ) {
1261 case LM85_REG_FAN(0) : /* Read WORD data */
1262 case LM85_REG_FAN(1) :
1263 case LM85_REG_FAN(2) :
1264 case LM85_REG_FAN(3) :
1265 case LM85_REG_FAN_MIN(0) :
1266 case LM85_REG_FAN_MIN(1) :
1267 case LM85_REG_FAN_MIN(2) :
1268 case LM85_REG_FAN_MIN(3) :
1269 case LM85_REG_ALARM1 : /* Read both bytes at once */
1270 res = i2c_smbus_read_byte_data(client, reg) & 0xff ;
1271 res |= i2c_smbus_read_byte_data(client, reg+1) << 8 ;
1272 break ;
1273 case ADT7463_REG_TMIN_CTL1 : /* Read WORD MSB, LSB */
1274 res = i2c_smbus_read_byte_data(client, reg) << 8 ;
1275 res |= i2c_smbus_read_byte_data(client, reg+1) & 0xff ;
1276 break ;
1277 default: /* Read BYTE data */
1278 res = i2c_smbus_read_byte_data(client, reg);
1279 break ;
1280 }
1281
1282 return res ;
1283}
1284
1285int lm85_write_value(struct i2c_client *client, u8 reg, int value)
1286{
1287 int res ;
1288
1289 switch( reg ) {
1290 case LM85_REG_FAN(0) : /* Write WORD data */
1291 case LM85_REG_FAN(1) :
1292 case LM85_REG_FAN(2) :
1293 case LM85_REG_FAN(3) :
1294 case LM85_REG_FAN_MIN(0) :
1295 case LM85_REG_FAN_MIN(1) :
1296 case LM85_REG_FAN_MIN(2) :
1297 case LM85_REG_FAN_MIN(3) :
1298 /* NOTE: ALARM is read only, so not included here */
1299 res = i2c_smbus_write_byte_data(client, reg, value & 0xff) ;
1300 res |= i2c_smbus_write_byte_data(client, reg+1, (value>>8) & 0xff) ;
1301 break ;
1302 case ADT7463_REG_TMIN_CTL1 : /* Write WORD MSB, LSB */
1303 res = i2c_smbus_write_byte_data(client, reg, (value>>8) & 0xff);
1304 res |= i2c_smbus_write_byte_data(client, reg+1, value & 0xff) ;
1305 break ;
1306 default: /* Write BYTE data */
1307 res = i2c_smbus_write_byte_data(client, reg, value);
1308 break ;
1309 }
1310
1311 return res ;
1312}
1313
1314void lm85_init_client(struct i2c_client *client)
1315{
1316 int value;
1317 struct lm85_data *data = i2c_get_clientdata(client);
1318
1319 dev_dbg(&client->dev, "Initializing device\n");
1320
1321 /* Warn if part was not "READY" */
1322 value = lm85_read_value(client, LM85_REG_CONFIG);
1323 dev_dbg(&client->dev, "LM85_REG_CONFIG is: 0x%02x\n", value);
1324 if( value & 0x02 ) {
1325 dev_err(&client->dev, "Client (%d,0x%02x) config is locked.\n",
1326 i2c_adapter_id(client->adapter), client->addr );
1327 };
1328 if( ! (value & 0x04) ) {
1329 dev_err(&client->dev, "Client (%d,0x%02x) is not ready.\n",
1330 i2c_adapter_id(client->adapter), client->addr );
1331 };
1332 if( value & 0x10
1333 && ( data->type == adm1027
1334 || data->type == adt7463 ) ) {
1335 dev_err(&client->dev, "Client (%d,0x%02x) VxI mode is set. "
1336 "Please report this to the lm85 maintainer.\n",
1337 i2c_adapter_id(client->adapter), client->addr );
1338 };
1339
1340 /* WE INTENTIONALLY make no changes to the limits,
1341 * offsets, pwms, fans and zones. If they were
1342 * configured, we don't want to mess with them.
1343 * If they weren't, the default is 100% PWM, no
1344 * control and will suffice until 'sensors -s'
1345 * can be run by the user.
1346 */
1347
1348 /* Start monitoring */
1349 value = lm85_read_value(client, LM85_REG_CONFIG);
1350 /* Try to clear LOCK, Set START, save everything else */
1351 value = (value & ~ 0x02) | 0x01 ;
1352 dev_dbg(&client->dev, "Setting CONFIG to: 0x%02x\n", value);
1353 lm85_write_value(client, LM85_REG_CONFIG, value);
1354}
1355
1356static struct lm85_data *lm85_update_device(struct device *dev)
1357{
1358 struct i2c_client *client = to_i2c_client(dev);
1359 struct lm85_data *data = i2c_get_clientdata(client);
1360 int i;
1361
1362 down(&data->update_lock);
1363
1364 if ( !data->valid ||
1365 time_after(jiffies, data->last_reading + LM85_DATA_INTERVAL) ) {
1366 /* Things that change quickly */
1367 dev_dbg(&client->dev, "Reading sensor values\n");
1368
1369 /* Have to read extended bits first to "freeze" the
1370 * more significant bits that are read later.
1371 */
1372 if ( (data->type == adm1027) || (data->type == adt7463) ) {
1373 int ext1 = lm85_read_value(client,
1374 ADM1027_REG_EXTEND_ADC1);
1375 int ext2 = lm85_read_value(client,
1376 ADM1027_REG_EXTEND_ADC2);
1377 int val = (ext1 << 8) + ext2;
1378
1379 for(i = 0; i <= 4; i++)
1380 data->in_ext[i] = (val>>(i * 2))&0x03;
1381
1382 for(i = 0; i <= 2; i++)
1383 data->temp_ext[i] = (val>>((i + 5) * 2))&0x03;
1384 }
1385
1386 /* adc_scale is 2^(number of LSBs). There are 4 extra bits in
1387 the emc6d102 and 2 in the adt7463 and adm1027. In all
1388 other chips ext is always 0 and the value of scale is
1389 irrelevant. So it is left in 4*/
1390 data->adc_scale = (data->type == emc6d102 ) ? 16 : 4;
1391
1392 for (i = 0; i <= 4; ++i) {
1393 data->in[i] =
1394 lm85_read_value(client, LM85_REG_IN(i));
1395 }
1396
1397 for (i = 0; i <= 3; ++i) {
1398 data->fan[i] =
1399 lm85_read_value(client, LM85_REG_FAN(i));
1400 }
1401
1402 for (i = 0; i <= 2; ++i) {
1403 data->temp[i] =
1404 lm85_read_value(client, LM85_REG_TEMP(i));
1405 }
1406
1407 for (i = 0; i <= 2; ++i) {
1408 data->pwm[i] =
1409 lm85_read_value(client, LM85_REG_PWM(i));
1410 }
1411
1412 data->alarms = lm85_read_value(client, LM85_REG_ALARM1);
1413
1414 if ( data->type == adt7463 ) {
1415 if( data->therm_total < ULONG_MAX - 256 ) {
1416 data->therm_total +=
1417 lm85_read_value(client, ADT7463_REG_THERM );
1418 }
1419 } else if ( data->type == emc6d100 ) {
1420 /* Three more voltage sensors */
1421 for (i = 5; i <= 7; ++i) {
1422 data->in[i] =
1423 lm85_read_value(client, EMC6D100_REG_IN(i));
1424 }
1425 /* More alarm bits */
1426 data->alarms |=
1427 lm85_read_value(client, EMC6D100_REG_ALARM3) << 16;
1428 } else if (data->type == emc6d102 ) {
1429 /* Have to read LSB bits after the MSB ones because
1430 the reading of the MSB bits has frozen the
1431 LSBs (backward from the ADM1027).
1432 */
1433 int ext1 = lm85_read_value(client,
1434 EMC6D102_REG_EXTEND_ADC1);
1435 int ext2 = lm85_read_value(client,
1436 EMC6D102_REG_EXTEND_ADC2);
1437 int ext3 = lm85_read_value(client,
1438 EMC6D102_REG_EXTEND_ADC3);
1439 int ext4 = lm85_read_value(client,
1440 EMC6D102_REG_EXTEND_ADC4);
1441 data->in_ext[0] = ext3 & 0x0f;
1442 data->in_ext[1] = ext4 & 0x0f;
1443 data->in_ext[2] = (ext4 >> 4) & 0x0f;
1444 data->in_ext[3] = (ext3 >> 4) & 0x0f;
1445 data->in_ext[4] = (ext2 >> 4) & 0x0f;
1446
1447 data->temp_ext[0] = ext1 & 0x0f;
1448 data->temp_ext[1] = ext2 & 0x0f;
1449 data->temp_ext[2] = (ext1 >> 4) & 0x0f;
1450 }
1451
1452 data->last_reading = jiffies ;
1453 }; /* last_reading */
1454
1455 if ( !data->valid ||
1456 time_after(jiffies, data->last_config + LM85_CONFIG_INTERVAL) ) {
1457 /* Things that don't change often */
1458 dev_dbg(&client->dev, "Reading config values\n");
1459
1460 for (i = 0; i <= 4; ++i) {
1461 data->in_min[i] =
1462 lm85_read_value(client, LM85_REG_IN_MIN(i));
1463 data->in_max[i] =
1464 lm85_read_value(client, LM85_REG_IN_MAX(i));
1465 }
1466
1467 if ( data->type == emc6d100 ) {
1468 for (i = 5; i <= 7; ++i) {
1469 data->in_min[i] =
1470 lm85_read_value(client, EMC6D100_REG_IN_MIN(i));
1471 data->in_max[i] =
1472 lm85_read_value(client, EMC6D100_REG_IN_MAX(i));
1473 }
1474 }
1475
1476 for (i = 0; i <= 3; ++i) {
1477 data->fan_min[i] =
1478 lm85_read_value(client, LM85_REG_FAN_MIN(i));
1479 }
1480
1481 for (i = 0; i <= 2; ++i) {
1482 data->temp_min[i] =
1483 lm85_read_value(client, LM85_REG_TEMP_MIN(i));
1484 data->temp_max[i] =
1485 lm85_read_value(client, LM85_REG_TEMP_MAX(i));
1486 }
1487
1488 data->vid = lm85_read_value(client, LM85_REG_VID);
1489
1490 for (i = 0; i <= 2; ++i) {
1491 int val ;
1492 data->autofan[i].config =
1493 lm85_read_value(client, LM85_REG_AFAN_CONFIG(i));
1494 val = lm85_read_value(client, LM85_REG_AFAN_RANGE(i));
1495 data->autofan[i].freq = val & 0x07 ;
1496 data->zone[i].range = (val >> 4) & 0x0f ;
1497 data->autofan[i].min_pwm =
1498 lm85_read_value(client, LM85_REG_AFAN_MINPWM(i));
1499 data->zone[i].limit =
1500 lm85_read_value(client, LM85_REG_AFAN_LIMIT(i));
1501 data->zone[i].critical =
1502 lm85_read_value(client, LM85_REG_AFAN_CRITICAL(i));
1503 }
1504
1505 i = lm85_read_value(client, LM85_REG_AFAN_SPIKE1);
1506 data->smooth[0] = i & 0x0f ;
1507 data->syncpwm3 = i & 0x10 ; /* Save PWM3 config */
1508 data->autofan[0].min_off = (i & 0x20) != 0 ;
1509 data->autofan[1].min_off = (i & 0x40) != 0 ;
1510 data->autofan[2].min_off = (i & 0x80) != 0 ;
1511 i = lm85_read_value(client, LM85_REG_AFAN_SPIKE2);
1512 data->smooth[1] = (i>>4) & 0x0f ;
1513 data->smooth[2] = i & 0x0f ;
1514
1515 i = lm85_read_value(client, LM85_REG_AFAN_HYST1);
1516 data->zone[0].hyst = (i>>4) & 0x0f ;
1517 data->zone[1].hyst = i & 0x0f ;
1518
1519 i = lm85_read_value(client, LM85_REG_AFAN_HYST2);
1520 data->zone[2].hyst = (i>>4) & 0x0f ;
1521
1522 if ( (data->type == lm85b) || (data->type == lm85c) ) {
1523 data->tach_mode = lm85_read_value(client,
1524 LM85_REG_TACH_MODE );
1525 data->spinup_ctl = lm85_read_value(client,
1526 LM85_REG_SPINUP_CTL );
1527 } else if ( (data->type == adt7463) || (data->type == adm1027) ) {
1528 if ( data->type == adt7463 ) {
1529 for (i = 0; i <= 2; ++i) {
1530 data->oppoint[i] = lm85_read_value(client,
1531 ADT7463_REG_OPPOINT(i) );
1532 }
1533 data->tmin_ctl = lm85_read_value(client,
1534 ADT7463_REG_TMIN_CTL1 );
1535 data->therm_limit = lm85_read_value(client,
1536 ADT7463_REG_THERM_LIMIT );
1537 }
1538 for (i = 0; i <= 2; ++i) {
1539 data->temp_offset[i] = lm85_read_value(client,
1540 ADM1027_REG_TEMP_OFFSET(i) );
1541 }
1542 data->tach_mode = lm85_read_value(client,
1543 ADM1027_REG_CONFIG3 );
1544 data->fan_ppr = lm85_read_value(client,
1545 ADM1027_REG_FAN_PPR );
1546 }
1547
1548 data->last_config = jiffies;
1549 }; /* last_config */
1550
1551 data->valid = 1;
1552
1553 up(&data->update_lock);
1554
1555 return data;
1556}
1557
1558
1559static int __init sm_lm85_init(void)
1560{
1561 return i2c_add_driver(&lm85_driver);
1562}
1563
1564static void __exit sm_lm85_exit(void)
1565{
1566 i2c_del_driver(&lm85_driver);
1567}
1568
1569/* Thanks to Richard Barrington for adding the LM85 to sensors-detect.
1570 * Thanks to Margit Schubert-While <margitsw@t-online.de> for help with
1571 * post 2.7.0 CVS changes.
1572 */
1573MODULE_LICENSE("GPL");
1574MODULE_AUTHOR("Philip Pokorny <ppokorny@penguincomputing.com>, Margit Schubert-While <margitsw@t-online.de>, Justin Thiessen <jthiessen@penguincomputing.com");
1575MODULE_DESCRIPTION("LM85-B, LM85-C driver");
1576
1577module_init(sm_lm85_init);
1578module_exit(sm_lm85_exit);
diff --git a/drivers/i2c/chips/lm87.c b/drivers/i2c/chips/lm87.c
new file mode 100644
index 000000000000..98cabd665063
--- /dev/null
+++ b/drivers/i2c/chips/lm87.c
@@ -0,0 +1,829 @@
1/*
2 * lm87.c
3 *
4 * Copyright (C) 2000 Frodo Looijaard <frodol@dds.nl>
5 * Philip Edelbrock <phil@netroedge.com>
6 * Stephen Rousset <stephen.rousset@rocketlogix.com>
7 * Dan Eaton <dan.eaton@rocketlogix.com>
8 * Copyright (C) 2004 Jean Delvare <khali@linux-fr.org>
9 *
10 * Original port to Linux 2.6 by Jeff Oliver.
11 *
12 * The LM87 is a sensor chip made by National Semiconductor. It monitors up
13 * to 8 voltages (including its own power source), up to three temperatures
14 * (its own plus up to two external ones) and up to two fans. The default
15 * configuration is 6 voltages, two temperatures and two fans (see below).
16 * Voltages are scaled internally with ratios such that the nominal value of
17 * each voltage correspond to a register value of 192 (which means a
18 * resolution of about 0.5% of the nominal value). Temperature values are
19 * reported with a 1 deg resolution and a 3-4 deg accuracy. Complete
20 * datasheet can be obtained from National's website at:
21 * http://www.national.com/pf/LM/LM87.html
22 *
23 * Some functions share pins, so not all functions are available at the same
24 * time. Which are depends on the hardware setup. This driver assumes that
25 * the BIOS configured the chip correctly. In that respect, it differs from
26 * the original driver (from lm_sensors for Linux 2.4), which would force the
27 * LM87 to an arbitrary, compile-time chosen mode, regardless of the actual
28 * chipset wiring.
29 * For reference, here is the list of exclusive functions:
30 * - in0+in5 (default) or temp3
31 * - fan1 (default) or in6
32 * - fan2 (default) or in7
33 * - VID lines (default) or IRQ lines (not handled by this driver)
34 *
35 * The LM87 additionally features an analog output, supposedly usable to
36 * control the speed of a fan. All new chips use pulse width modulation
37 * instead. The LM87 is the only hardware monitoring chipset I know of
38 * which uses amplitude modulation. Be careful when using this feature.
39 *
40 * This program is free software; you can redistribute it and/or modify
41 * it under the terms of the GNU General Public License as published by
42 * the Free Software Foundation; either version 2 of the License, or
43 * (at your option) any later version.
44 *
45 * This program is distributed in the hope that it will be useful,
46 * but WITHOUT ANY WARRANTY; without even the implied warranty of
47 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
48 * GNU General Public License for more details.
49 *
50 * You should have received a copy of the GNU General Public License
51 * along with this program; if not, write to the Free Software
52 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
53 */
54
55#include <linux/config.h>
56#include <linux/module.h>
57#include <linux/init.h>
58#include <linux/slab.h>
59#include <linux/jiffies.h>
60#include <linux/i2c.h>
61#include <linux/i2c-sensor.h>
62#include <linux/i2c-vid.h>
63
64/*
65 * Addresses to scan
66 * LM87 has three possible addresses: 0x2c, 0x2d and 0x2e.
67 */
68
69static unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
70static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
71
72/*
73 * Insmod parameters
74 */
75
76SENSORS_INSMOD_1(lm87);
77
78/*
79 * The LM87 registers
80 */
81
82/* nr in 0..5 */
83#define LM87_REG_IN(nr) (0x20 + (nr))
84#define LM87_REG_IN_MAX(nr) (0x2B + (nr) * 2)
85#define LM87_REG_IN_MIN(nr) (0x2C + (nr) * 2)
86/* nr in 0..1 */
87#define LM87_REG_AIN(nr) (0x28 + (nr))
88#define LM87_REG_AIN_MIN(nr) (0x1A + (nr))
89#define LM87_REG_AIN_MAX(nr) (0x3B + (nr))
90
91static u8 LM87_REG_TEMP[3] = { 0x27, 0x26, 0x20 };
92static u8 LM87_REG_TEMP_HIGH[3] = { 0x39, 0x37, 0x2B };
93static u8 LM87_REG_TEMP_LOW[3] = { 0x3A, 0x38, 0x2C };
94
95#define LM87_REG_TEMP_HW_INT_LOCK 0x13
96#define LM87_REG_TEMP_HW_EXT_LOCK 0x14
97#define LM87_REG_TEMP_HW_INT 0x17
98#define LM87_REG_TEMP_HW_EXT 0x18
99
100/* nr in 0..1 */
101#define LM87_REG_FAN(nr) (0x28 + (nr))
102#define LM87_REG_FAN_MIN(nr) (0x3B + (nr))
103#define LM87_REG_AOUT 0x19
104
105#define LM87_REG_CONFIG 0x40
106#define LM87_REG_CHANNEL_MODE 0x16
107#define LM87_REG_VID_FAN_DIV 0x47
108#define LM87_REG_VID4 0x49
109
110#define LM87_REG_ALARMS1 0x41
111#define LM87_REG_ALARMS2 0x42
112
113#define LM87_REG_COMPANY_ID 0x3E
114#define LM87_REG_REVISION 0x3F
115
116/*
117 * Conversions and various macros
118 * The LM87 uses signed 8-bit values for temperatures.
119 */
120
121#define IN_FROM_REG(reg,scale) (((reg) * (scale) + 96) / 192)
122#define IN_TO_REG(val,scale) ((val) <= 0 ? 0 : \
123 (val) * 192 >= (scale) * 255 ? 255 : \
124 ((val) * 192 + (scale)/2) / (scale))
125
126#define TEMP_FROM_REG(reg) ((reg) * 1000)
127#define TEMP_TO_REG(val) ((val) <= -127500 ? -128 : \
128 (val) >= 126500 ? 127 : \
129 (((val) < 0 ? (val)-500 : (val)+500) / 1000))
130
131#define FAN_FROM_REG(reg,div) ((reg) == 255 || (reg) == 0 ? 0 : \
132 1350000 + (reg)*(div) / 2) / ((reg)*(div))
133#define FAN_TO_REG(val,div) ((val)*(div) * 255 <= 1350000 ? 255 : \
134 (1350000 + (val)*(div) / 2) / ((val)*(div)))
135
136#define FAN_DIV_FROM_REG(reg) (1 << (reg))
137
138/* analog out is 9.80mV/LSB */
139#define AOUT_FROM_REG(reg) (((reg) * 98 + 5) / 10)
140#define AOUT_TO_REG(val) ((val) <= 0 ? 0 : \
141 (val) >= 2500 ? 255 : \
142 ((val) * 10 + 49) / 98)
143
144/* nr in 0..1 */
145#define CHAN_NO_FAN(nr) (1 << (nr))
146#define CHAN_TEMP3 (1 << 2)
147#define CHAN_VCC_5V (1 << 3)
148#define CHAN_NO_VID (1 << 8)
149
150/*
151 * Functions declaration
152 */
153
154static int lm87_attach_adapter(struct i2c_adapter *adapter);
155static int lm87_detect(struct i2c_adapter *adapter, int address, int kind);
156static void lm87_init_client(struct i2c_client *client);
157static int lm87_detach_client(struct i2c_client *client);
158static struct lm87_data *lm87_update_device(struct device *dev);
159
160/*
161 * Driver data (common to all clients)
162 */
163
164static struct i2c_driver lm87_driver = {
165 .owner = THIS_MODULE,
166 .name = "lm87",
167 .id = I2C_DRIVERID_LM87,
168 .flags = I2C_DF_NOTIFY,
169 .attach_adapter = lm87_attach_adapter,
170 .detach_client = lm87_detach_client,
171};
172
173/*
174 * Client data (each client gets its own)
175 */
176
177struct lm87_data {
178 struct i2c_client client;
179 struct semaphore update_lock;
180 char valid; /* zero until following fields are valid */
181 unsigned long last_updated; /* In jiffies */
182
183 u8 channel; /* register value */
184
185 u8 in[8]; /* register value */
186 u8 in_max[8]; /* register value */
187 u8 in_min[8]; /* register value */
188 u16 in_scale[8];
189
190 s8 temp[3]; /* register value */
191 s8 temp_high[3]; /* register value */
192 s8 temp_low[3]; /* register value */
193 s8 temp_crit_int; /* min of two register values */
194 s8 temp_crit_ext; /* min of two register values */
195
196 u8 fan[2]; /* register value */
197 u8 fan_min[2]; /* register value */
198 u8 fan_div[2]; /* register value, shifted right */
199 u8 aout; /* register value */
200
201 u16 alarms; /* register values, combined */
202 u8 vid; /* register values, combined */
203 u8 vrm;
204};
205
206/*
207 * Sysfs stuff
208 */
209
210static inline int lm87_read_value(struct i2c_client *client, u8 reg)
211{
212 return i2c_smbus_read_byte_data(client, reg);
213}
214
215static inline int lm87_write_value(struct i2c_client *client, u8 reg, u8 value)
216{
217 return i2c_smbus_write_byte_data(client, reg, value);
218}
219
220#define show_in(offset) \
221static ssize_t show_in##offset##_input(struct device *dev, char *buf) \
222{ \
223 struct lm87_data *data = lm87_update_device(dev); \
224 return sprintf(buf, "%u\n", IN_FROM_REG(data->in[offset], \
225 data->in_scale[offset])); \
226} \
227static ssize_t show_in##offset##_min(struct device *dev, char *buf) \
228{ \
229 struct lm87_data *data = lm87_update_device(dev); \
230 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[offset], \
231 data->in_scale[offset])); \
232} \
233static ssize_t show_in##offset##_max(struct device *dev, char *buf) \
234{ \
235 struct lm87_data *data = lm87_update_device(dev); \
236 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[offset], \
237 data->in_scale[offset])); \
238} \
239static DEVICE_ATTR(in##offset##_input, S_IRUGO, \
240 show_in##offset##_input, NULL);
241show_in(0);
242show_in(1);
243show_in(2);
244show_in(3);
245show_in(4);
246show_in(5);
247show_in(6);
248show_in(7);
249
250static void set_in_min(struct device *dev, const char *buf, int nr)
251{
252 struct i2c_client *client = to_i2c_client(dev);
253 struct lm87_data *data = i2c_get_clientdata(client);
254 long val = simple_strtol(buf, NULL, 10);
255
256 down(&data->update_lock);
257 data->in_min[nr] = IN_TO_REG(val, data->in_scale[nr]);
258 lm87_write_value(client, nr<6 ? LM87_REG_IN_MIN(nr) :
259 LM87_REG_AIN_MIN(nr-6), data->in_min[nr]);
260 up(&data->update_lock);
261}
262
263static void set_in_max(struct device *dev, const char *buf, int nr)
264{
265 struct i2c_client *client = to_i2c_client(dev);
266 struct lm87_data *data = i2c_get_clientdata(client);
267 long val = simple_strtol(buf, NULL, 10);
268
269 down(&data->update_lock);
270 data->in_max[nr] = IN_TO_REG(val, data->in_scale[nr]);
271 lm87_write_value(client, nr<6 ? LM87_REG_IN_MAX(nr) :
272 LM87_REG_AIN_MAX(nr-6), data->in_max[nr]);
273 up(&data->update_lock);
274}
275
276#define set_in(offset) \
277static ssize_t set_in##offset##_min(struct device *dev, \
278 const char *buf, size_t count) \
279{ \
280 set_in_min(dev, buf, offset); \
281 return count; \
282} \
283static ssize_t set_in##offset##_max(struct device *dev, \
284 const char *buf, size_t count) \
285{ \
286 set_in_max(dev, buf, offset); \
287 return count; \
288} \
289static DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
290 show_in##offset##_min, set_in##offset##_min); \
291static DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
292 show_in##offset##_max, set_in##offset##_max);
293set_in(0);
294set_in(1);
295set_in(2);
296set_in(3);
297set_in(4);
298set_in(5);
299set_in(6);
300set_in(7);
301
302#define show_temp(offset) \
303static ssize_t show_temp##offset##_input(struct device *dev, char *buf) \
304{ \
305 struct lm87_data *data = lm87_update_device(dev); \
306 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[offset-1])); \
307} \
308static ssize_t show_temp##offset##_low(struct device *dev, char *buf) \
309{ \
310 struct lm87_data *data = lm87_update_device(dev); \
311 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_low[offset-1])); \
312} \
313static ssize_t show_temp##offset##_high(struct device *dev, char *buf) \
314{ \
315 struct lm87_data *data = lm87_update_device(dev); \
316 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[offset-1])); \
317}\
318static DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
319 show_temp##offset##_input, NULL);
320show_temp(1);
321show_temp(2);
322show_temp(3);
323
324static void set_temp_low(struct device *dev, const char *buf, int nr)
325{
326 struct i2c_client *client = to_i2c_client(dev);
327 struct lm87_data *data = i2c_get_clientdata(client);
328 long val = simple_strtol(buf, NULL, 10);
329
330 down(&data->update_lock);
331 data->temp_low[nr] = TEMP_TO_REG(val);
332 lm87_write_value(client, LM87_REG_TEMP_LOW[nr], data->temp_low[nr]);
333 up(&data->update_lock);
334}
335
336static void set_temp_high(struct device *dev, const char *buf, int nr)
337{
338 struct i2c_client *client = to_i2c_client(dev);
339 struct lm87_data *data = i2c_get_clientdata(client);
340 long val = simple_strtol(buf, NULL, 10);
341
342 down(&data->update_lock);
343 data->temp_high[nr] = TEMP_TO_REG(val);
344 lm87_write_value(client, LM87_REG_TEMP_HIGH[nr], data->temp_high[nr]);
345 up(&data->update_lock);
346}
347
348#define set_temp(offset) \
349static ssize_t set_temp##offset##_low(struct device *dev, \
350 const char *buf, size_t count) \
351{ \
352 set_temp_low(dev, buf, offset-1); \
353 return count; \
354} \
355static ssize_t set_temp##offset##_high(struct device *dev, \
356 const char *buf, size_t count) \
357{ \
358 set_temp_high(dev, buf, offset-1); \
359 return count; \
360} \
361static DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
362 show_temp##offset##_high, set_temp##offset##_high); \
363static DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
364 show_temp##offset##_low, set_temp##offset##_low);
365set_temp(1);
366set_temp(2);
367set_temp(3);
368
369static ssize_t show_temp_crit_int(struct device *dev, char *buf)
370{
371 struct lm87_data *data = lm87_update_device(dev);
372 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit_int));
373}
374
375static ssize_t show_temp_crit_ext(struct device *dev, char *buf)
376{
377 struct lm87_data *data = lm87_update_device(dev);
378 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit_ext));
379}
380
381static DEVICE_ATTR(temp1_crit, S_IRUGO, show_temp_crit_int, NULL);
382static DEVICE_ATTR(temp2_crit, S_IRUGO, show_temp_crit_ext, NULL);
383static DEVICE_ATTR(temp3_crit, S_IRUGO, show_temp_crit_ext, NULL);
384
385#define show_fan(offset) \
386static ssize_t show_fan##offset##_input(struct device *dev, char *buf) \
387{ \
388 struct lm87_data *data = lm87_update_device(dev); \
389 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[offset-1], \
390 FAN_DIV_FROM_REG(data->fan_div[offset-1]))); \
391} \
392static ssize_t show_fan##offset##_min(struct device *dev, char *buf) \
393{ \
394 struct lm87_data *data = lm87_update_device(dev); \
395 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[offset-1], \
396 FAN_DIV_FROM_REG(data->fan_div[offset-1]))); \
397} \
398static ssize_t show_fan##offset##_div(struct device *dev, char *buf) \
399{ \
400 struct lm87_data *data = lm87_update_device(dev); \
401 return sprintf(buf, "%d\n", FAN_DIV_FROM_REG(data->fan_div[offset-1])); \
402} \
403static DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
404 show_fan##offset##_input, NULL);
405show_fan(1);
406show_fan(2);
407
408static void set_fan_min(struct device *dev, const char *buf, int nr)
409{
410 struct i2c_client *client = to_i2c_client(dev);
411 struct lm87_data *data = i2c_get_clientdata(client);
412 long val = simple_strtol(buf, NULL, 10);
413
414 down(&data->update_lock);
415 data->fan_min[nr] = FAN_TO_REG(val,
416 FAN_DIV_FROM_REG(data->fan_div[nr]));
417 lm87_write_value(client, LM87_REG_FAN_MIN(nr), data->fan_min[nr]);
418 up(&data->update_lock);
419}
420
421/* Note: we save and restore the fan minimum here, because its value is
422 determined in part by the fan clock divider. This follows the principle
423 of least suprise; the user doesn't expect the fan minimum to change just
424 because the divider changed. */
425static ssize_t set_fan_div(struct device *dev, const char *buf,
426 size_t count, int nr)
427{
428 struct i2c_client *client = to_i2c_client(dev);
429 struct lm87_data *data = i2c_get_clientdata(client);
430 long val = simple_strtol(buf, NULL, 10);
431 unsigned long min;
432 u8 reg;
433
434 down(&data->update_lock);
435 min = FAN_FROM_REG(data->fan_min[nr],
436 FAN_DIV_FROM_REG(data->fan_div[nr]));
437
438 switch (val) {
439 case 1: data->fan_div[nr] = 0; break;
440 case 2: data->fan_div[nr] = 1; break;
441 case 4: data->fan_div[nr] = 2; break;
442 case 8: data->fan_div[nr] = 3; break;
443 default:
444 up(&data->update_lock);
445 return -EINVAL;
446 }
447
448 reg = lm87_read_value(client, LM87_REG_VID_FAN_DIV);
449 switch (nr) {
450 case 0:
451 reg = (reg & 0xCF) | (data->fan_div[0] << 4);
452 break;
453 case 1:
454 reg = (reg & 0x3F) | (data->fan_div[1] << 6);
455 break;
456 }
457 lm87_write_value(client, LM87_REG_VID_FAN_DIV, reg);
458
459 data->fan_min[nr] = FAN_TO_REG(min, val);
460 lm87_write_value(client, LM87_REG_FAN_MIN(nr),
461 data->fan_min[nr]);
462 up(&data->update_lock);
463
464 return count;
465}
466
467#define set_fan(offset) \
468static ssize_t set_fan##offset##_min(struct device *dev, const char *buf, \
469 size_t count) \
470{ \
471 set_fan_min(dev, buf, offset-1); \
472 return count; \
473} \
474static ssize_t set_fan##offset##_div(struct device *dev, const char *buf, \
475 size_t count) \
476{ \
477 return set_fan_div(dev, buf, count, offset-1); \
478} \
479static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
480 show_fan##offset##_min, set_fan##offset##_min); \
481static DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
482 show_fan##offset##_div, set_fan##offset##_div);
483set_fan(1);
484set_fan(2);
485
486static ssize_t show_alarms(struct device *dev, char *buf)
487{
488 struct lm87_data *data = lm87_update_device(dev);
489 return sprintf(buf, "%d\n", data->alarms);
490}
491static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
492
493static ssize_t show_vid(struct device *dev, char *buf)
494{
495 struct lm87_data *data = lm87_update_device(dev);
496 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
497}
498static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
499
500static ssize_t show_vrm(struct device *dev, char *buf)
501{
502 struct lm87_data *data = lm87_update_device(dev);
503 return sprintf(buf, "%d\n", data->vrm);
504}
505static ssize_t set_vrm(struct device *dev, const char *buf, size_t count)
506{
507 struct i2c_client *client = to_i2c_client(dev);
508 struct lm87_data *data = i2c_get_clientdata(client);
509 data->vrm = simple_strtoul(buf, NULL, 10);
510 return count;
511}
512static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm);
513
514static ssize_t show_aout(struct device *dev, char *buf)
515{
516 struct lm87_data *data = lm87_update_device(dev);
517 return sprintf(buf, "%d\n", AOUT_FROM_REG(data->aout));
518}
519static ssize_t set_aout(struct device *dev, const char *buf, size_t count)
520{
521 struct i2c_client *client = to_i2c_client(dev);
522 struct lm87_data *data = i2c_get_clientdata(client);
523 long val = simple_strtol(buf, NULL, 10);
524
525 down(&data->update_lock);
526 data->aout = AOUT_TO_REG(val);
527 lm87_write_value(client, LM87_REG_AOUT, data->aout);
528 up(&data->update_lock);
529 return count;
530}
531static DEVICE_ATTR(aout_output, S_IRUGO | S_IWUSR, show_aout, set_aout);
532
533/*
534 * Real code
535 */
536
537static int lm87_attach_adapter(struct i2c_adapter *adapter)
538{
539 if (!(adapter->class & I2C_CLASS_HWMON))
540 return 0;
541 return i2c_detect(adapter, &addr_data, lm87_detect);
542}
543
544/*
545 * The following function does more than just detection. If detection
546 * succeeds, it also registers the new chip.
547 */
548static int lm87_detect(struct i2c_adapter *adapter, int address, int kind)
549{
550 struct i2c_client *new_client;
551 struct lm87_data *data;
552 int err = 0;
553
554 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
555 goto exit;
556
557 if (!(data = kmalloc(sizeof(struct lm87_data), GFP_KERNEL))) {
558 err = -ENOMEM;
559 goto exit;
560 }
561 memset(data, 0, sizeof(struct lm87_data));
562
563 /* The common I2C client data is placed right before the
564 LM87-specific data. */
565 new_client = &data->client;
566 i2c_set_clientdata(new_client, data);
567 new_client->addr = address;
568 new_client->adapter = adapter;
569 new_client->driver = &lm87_driver;
570 new_client->flags = 0;
571
572 /* Default to an LM87 if forced */
573 if (kind == 0)
574 kind = lm87;
575
576 /* Now, we do the remaining detection. */
577 if (kind < 0) {
578 u8 rev = lm87_read_value(new_client, LM87_REG_REVISION);
579
580 if (rev < 0x01 || rev > 0x08
581 || (lm87_read_value(new_client, LM87_REG_CONFIG) & 0x80)
582 || lm87_read_value(new_client, LM87_REG_COMPANY_ID) != 0x02) {
583 dev_dbg(&adapter->dev,
584 "LM87 detection failed at 0x%02x.\n",
585 address);
586 goto exit_free;
587 }
588 }
589
590 /* We can fill in the remaining client fields */
591 strlcpy(new_client->name, "lm87", I2C_NAME_SIZE);
592 data->valid = 0;
593 init_MUTEX(&data->update_lock);
594
595 /* Tell the I2C layer a new client has arrived */
596 if ((err = i2c_attach_client(new_client)))
597 goto exit_free;
598
599 /* Initialize the LM87 chip */
600 lm87_init_client(new_client);
601
602 data->in_scale[0] = 2500;
603 data->in_scale[1] = 2700;
604 data->in_scale[2] = (data->channel & CHAN_VCC_5V) ? 5000 : 3300;
605 data->in_scale[3] = 5000;
606 data->in_scale[4] = 12000;
607 data->in_scale[5] = 2700;
608 data->in_scale[6] = 1875;
609 data->in_scale[7] = 1875;
610
611 /* Register sysfs hooks */
612 device_create_file(&new_client->dev, &dev_attr_in1_input);
613 device_create_file(&new_client->dev, &dev_attr_in1_min);
614 device_create_file(&new_client->dev, &dev_attr_in1_max);
615 device_create_file(&new_client->dev, &dev_attr_in2_input);
616 device_create_file(&new_client->dev, &dev_attr_in2_min);
617 device_create_file(&new_client->dev, &dev_attr_in2_max);
618 device_create_file(&new_client->dev, &dev_attr_in3_input);
619 device_create_file(&new_client->dev, &dev_attr_in3_min);
620 device_create_file(&new_client->dev, &dev_attr_in3_max);
621 device_create_file(&new_client->dev, &dev_attr_in4_input);
622 device_create_file(&new_client->dev, &dev_attr_in4_min);
623 device_create_file(&new_client->dev, &dev_attr_in4_max);
624
625 if (data->channel & CHAN_NO_FAN(0)) {
626 device_create_file(&new_client->dev, &dev_attr_in6_input);
627 device_create_file(&new_client->dev, &dev_attr_in6_min);
628 device_create_file(&new_client->dev, &dev_attr_in6_max);
629 } else {
630 device_create_file(&new_client->dev, &dev_attr_fan1_input);
631 device_create_file(&new_client->dev, &dev_attr_fan1_min);
632 device_create_file(&new_client->dev, &dev_attr_fan1_div);
633 }
634 if (data->channel & CHAN_NO_FAN(1)) {
635 device_create_file(&new_client->dev, &dev_attr_in7_input);
636 device_create_file(&new_client->dev, &dev_attr_in7_min);
637 device_create_file(&new_client->dev, &dev_attr_in7_max);
638 } else {
639 device_create_file(&new_client->dev, &dev_attr_fan2_input);
640 device_create_file(&new_client->dev, &dev_attr_fan2_min);
641 device_create_file(&new_client->dev, &dev_attr_fan2_div);
642 }
643
644 device_create_file(&new_client->dev, &dev_attr_temp1_input);
645 device_create_file(&new_client->dev, &dev_attr_temp1_max);
646 device_create_file(&new_client->dev, &dev_attr_temp1_min);
647 device_create_file(&new_client->dev, &dev_attr_temp1_crit);
648 device_create_file(&new_client->dev, &dev_attr_temp2_input);
649 device_create_file(&new_client->dev, &dev_attr_temp2_max);
650 device_create_file(&new_client->dev, &dev_attr_temp2_min);
651 device_create_file(&new_client->dev, &dev_attr_temp2_crit);
652
653 if (data->channel & CHAN_TEMP3) {
654 device_create_file(&new_client->dev, &dev_attr_temp3_input);
655 device_create_file(&new_client->dev, &dev_attr_temp3_max);
656 device_create_file(&new_client->dev, &dev_attr_temp3_min);
657 device_create_file(&new_client->dev, &dev_attr_temp3_crit);
658 } else {
659 device_create_file(&new_client->dev, &dev_attr_in0_input);
660 device_create_file(&new_client->dev, &dev_attr_in0_min);
661 device_create_file(&new_client->dev, &dev_attr_in0_max);
662 device_create_file(&new_client->dev, &dev_attr_in5_input);
663 device_create_file(&new_client->dev, &dev_attr_in5_min);
664 device_create_file(&new_client->dev, &dev_attr_in5_max);
665 }
666
667 if (!(data->channel & CHAN_NO_VID)) {
668 device_create_file(&new_client->dev, &dev_attr_cpu0_vid);
669 device_create_file(&new_client->dev, &dev_attr_vrm);
670 }
671
672 device_create_file(&new_client->dev, &dev_attr_alarms);
673 device_create_file(&new_client->dev, &dev_attr_aout_output);
674
675 return 0;
676
677exit_free:
678 kfree(data);
679exit:
680 return err;
681}
682
683static void lm87_init_client(struct i2c_client *client)
684{
685 struct lm87_data *data = i2c_get_clientdata(client);
686 u8 config;
687
688 data->channel = lm87_read_value(client, LM87_REG_CHANNEL_MODE);
689 data->vrm = i2c_which_vrm();
690
691 config = lm87_read_value(client, LM87_REG_CONFIG);
692 if (!(config & 0x01)) {
693 int i;
694
695 /* Limits are left uninitialized after power-up */
696 for (i = 1; i < 6; i++) {
697 lm87_write_value(client, LM87_REG_IN_MIN(i), 0x00);
698 lm87_write_value(client, LM87_REG_IN_MAX(i), 0xFF);
699 }
700 for (i = 0; i < 2; i++) {
701 lm87_write_value(client, LM87_REG_TEMP_HIGH[i], 0x7F);
702 lm87_write_value(client, LM87_REG_TEMP_LOW[i], 0x00);
703 lm87_write_value(client, LM87_REG_AIN_MIN(i), 0x00);
704 lm87_write_value(client, LM87_REG_AIN_MAX(i), 0xFF);
705 }
706 if (data->channel & CHAN_TEMP3) {
707 lm87_write_value(client, LM87_REG_TEMP_HIGH[2], 0x7F);
708 lm87_write_value(client, LM87_REG_TEMP_LOW[2], 0x00);
709 } else {
710 lm87_write_value(client, LM87_REG_IN_MIN(0), 0x00);
711 lm87_write_value(client, LM87_REG_IN_MAX(0), 0xFF);
712 }
713 }
714 if ((config & 0x81) != 0x01) {
715 /* Start monitoring */
716 lm87_write_value(client, LM87_REG_CONFIG,
717 (config & 0xF7) | 0x01);
718 }
719}
720
721static int lm87_detach_client(struct i2c_client *client)
722{
723 int err;
724
725 if ((err = i2c_detach_client(client))) {
726 dev_err(&client->dev, "Client deregistration failed, "
727 "client not detached.\n");
728 return err;
729 }
730
731 kfree(i2c_get_clientdata(client));
732 return 0;
733}
734
735static struct lm87_data *lm87_update_device(struct device *dev)
736{
737 struct i2c_client *client = to_i2c_client(dev);
738 struct lm87_data *data = i2c_get_clientdata(client);
739
740 down(&data->update_lock);
741
742 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
743 int i, j;
744
745 dev_dbg(&client->dev, "Updating data.\n");
746
747 i = (data->channel & CHAN_TEMP3) ? 1 : 0;
748 j = (data->channel & CHAN_TEMP3) ? 5 : 6;
749 for (; i < j; i++) {
750 data->in[i] = lm87_read_value(client,
751 LM87_REG_IN(i));
752 data->in_min[i] = lm87_read_value(client,
753 LM87_REG_IN_MIN(i));
754 data->in_max[i] = lm87_read_value(client,
755 LM87_REG_IN_MAX(i));
756 }
757
758 for (i = 0; i < 2; i++) {
759 if (data->channel & CHAN_NO_FAN(i)) {
760 data->in[6+i] = lm87_read_value(client,
761 LM87_REG_AIN(i));
762 data->in_max[6+i] = lm87_read_value(client,
763 LM87_REG_AIN_MAX(i));
764 data->in_min[6+i] = lm87_read_value(client,
765 LM87_REG_AIN_MIN(i));
766
767 } else {
768 data->fan[i] = lm87_read_value(client,
769 LM87_REG_FAN(i));
770 data->fan_min[i] = lm87_read_value(client,
771 LM87_REG_FAN_MIN(i));
772 }
773 }
774
775 j = (data->channel & CHAN_TEMP3) ? 3 : 2;
776 for (i = 0 ; i < j; i++) {
777 data->temp[i] = lm87_read_value(client,
778 LM87_REG_TEMP[i]);
779 data->temp_high[i] = lm87_read_value(client,
780 LM87_REG_TEMP_HIGH[i]);
781 data->temp_low[i] = lm87_read_value(client,
782 LM87_REG_TEMP_LOW[i]);
783 }
784
785 i = lm87_read_value(client, LM87_REG_TEMP_HW_INT_LOCK);
786 j = lm87_read_value(client, LM87_REG_TEMP_HW_INT);
787 data->temp_crit_int = min(i, j);
788
789 i = lm87_read_value(client, LM87_REG_TEMP_HW_EXT_LOCK);
790 j = lm87_read_value(client, LM87_REG_TEMP_HW_EXT);
791 data->temp_crit_ext = min(i, j);
792
793 i = lm87_read_value(client, LM87_REG_VID_FAN_DIV);
794 data->fan_div[0] = (i >> 4) & 0x03;
795 data->fan_div[1] = (i >> 6) & 0x03;
796 data->vid = (i & 0x0F)
797 | (lm87_read_value(client, LM87_REG_VID4) & 0x01)
798 << 4;
799
800 data->alarms = lm87_read_value(client, LM87_REG_ALARMS1)
801 | (lm87_read_value(client, LM87_REG_ALARMS2)
802 << 8);
803 data->aout = lm87_read_value(client, LM87_REG_AOUT);
804
805 data->last_updated = jiffies;
806 data->valid = 1;
807 }
808
809 up(&data->update_lock);
810
811 return data;
812}
813
814static int __init sensors_lm87_init(void)
815{
816 return i2c_add_driver(&lm87_driver);
817}
818
819static void __exit sensors_lm87_exit(void)
820{
821 i2c_del_driver(&lm87_driver);
822}
823
824MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org> and others");
825MODULE_DESCRIPTION("LM87 driver");
826MODULE_LICENSE("GPL");
827
828module_init(sensors_lm87_init);
829module_exit(sensors_lm87_exit);
diff --git a/drivers/i2c/chips/lm90.c b/drivers/i2c/chips/lm90.c
new file mode 100644
index 000000000000..2c00ff83babc
--- /dev/null
+++ b/drivers/i2c/chips/lm90.c
@@ -0,0 +1,626 @@
1/*
2 * lm90.c - Part of lm_sensors, Linux kernel modules for hardware
3 * monitoring
4 * Copyright (C) 2003-2004 Jean Delvare <khali@linux-fr.org>
5 *
6 * Based on the lm83 driver. The LM90 is a sensor chip made by National
7 * Semiconductor. It reports up to two temperatures (its own plus up to
8 * one external one) with a 0.125 deg resolution (1 deg for local
9 * temperature) and a 3-4 deg accuracy. Complete datasheet can be
10 * obtained from National's website at:
11 * http://www.national.com/pf/LM/LM90.html
12 *
13 * This driver also supports the LM89 and LM99, two other sensor chips
14 * made by National Semiconductor. Both have an increased remote
15 * temperature measurement accuracy (1 degree), and the LM99
16 * additionally shifts remote temperatures (measured and limits) by 16
17 * degrees, which allows for higher temperatures measurement. The
18 * driver doesn't handle it since it can be done easily in user-space.
19 * Complete datasheets can be obtained from National's website at:
20 * http://www.national.com/pf/LM/LM89.html
21 * http://www.national.com/pf/LM/LM99.html
22 * Note that there is no way to differenciate between both chips.
23 *
24 * This driver also supports the LM86, another sensor chip made by
25 * National Semiconductor. It is exactly similar to the LM90 except it
26 * has a higher accuracy.
27 * Complete datasheet can be obtained from National's website at:
28 * http://www.national.com/pf/LM/LM86.html
29 *
30 * This driver also supports the ADM1032, a sensor chip made by Analog
31 * Devices. That chip is similar to the LM90, with a few differences
32 * that are not handled by this driver. Complete datasheet can be
33 * obtained from Analog's website at:
34 * http://products.analog.com/products/info.asp?product=ADM1032
35 * Among others, it has a higher accuracy than the LM90, much like the
36 * LM86 does.
37 *
38 * This driver also supports the MAX6657, MAX6658 and MAX6659 sensor
39 * chips made by Maxim. These chips are similar to the LM86. Complete
40 * datasheet can be obtained at Maxim's website at:
41 * http://www.maxim-ic.com/quick_view2.cfm/qv_pk/2578
42 * Note that there is no easy way to differenciate between the three
43 * variants. The extra address and features of the MAX6659 are not
44 * supported by this driver.
45 *
46 * This driver also supports the ADT7461 chip from Analog Devices but
47 * only in its "compatability mode". If an ADT7461 chip is found but
48 * is configured in non-compatible mode (where its temperature
49 * register values are decoded differently) it is ignored by this
50 * driver. Complete datasheet can be obtained from Analog's website
51 * at:
52 * http://products.analog.com/products/info.asp?product=ADT7461
53 *
54 * Since the LM90 was the first chipset supported by this driver, most
55 * comments will refer to this chipset, but are actually general and
56 * concern all supported chipsets, unless mentioned otherwise.
57 *
58 * This program is free software; you can redistribute it and/or modify
59 * it under the terms of the GNU General Public License as published by
60 * the Free Software Foundation; either version 2 of the License, or
61 * (at your option) any later version.
62 *
63 * This program is distributed in the hope that it will be useful,
64 * but WITHOUT ANY WARRANTY; without even the implied warranty of
65 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
66 * GNU General Public License for more details.
67 *
68 * You should have received a copy of the GNU General Public License
69 * along with this program; if not, write to the Free Software
70 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
71 */
72
73#include <linux/config.h>
74#include <linux/module.h>
75#include <linux/init.h>
76#include <linux/slab.h>
77#include <linux/jiffies.h>
78#include <linux/i2c.h>
79#include <linux/i2c-sensor.h>
80
81/*
82 * Addresses to scan
83 * Address is fully defined internally and cannot be changed except for
84 * MAX6659.
85 * LM86, LM89, LM90, LM99, ADM1032, MAX6657 and MAX6658 have address 0x4c.
86 * LM89-1, and LM99-1 have address 0x4d.
87 * MAX6659 can have address 0x4c, 0x4d or 0x4e (unsupported).
88 * ADT7461 always has address 0x4c.
89 */
90
91static unsigned short normal_i2c[] = { 0x4c, 0x4d, I2C_CLIENT_END };
92static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
93
94/*
95 * Insmod parameters
96 */
97
98SENSORS_INSMOD_6(lm90, adm1032, lm99, lm86, max6657, adt7461);
99
100/*
101 * The LM90 registers
102 */
103
104#define LM90_REG_R_MAN_ID 0xFE
105#define LM90_REG_R_CHIP_ID 0xFF
106#define LM90_REG_R_CONFIG1 0x03
107#define LM90_REG_W_CONFIG1 0x09
108#define LM90_REG_R_CONFIG2 0xBF
109#define LM90_REG_W_CONFIG2 0xBF
110#define LM90_REG_R_CONVRATE 0x04
111#define LM90_REG_W_CONVRATE 0x0A
112#define LM90_REG_R_STATUS 0x02
113#define LM90_REG_R_LOCAL_TEMP 0x00
114#define LM90_REG_R_LOCAL_HIGH 0x05
115#define LM90_REG_W_LOCAL_HIGH 0x0B
116#define LM90_REG_R_LOCAL_LOW 0x06
117#define LM90_REG_W_LOCAL_LOW 0x0C
118#define LM90_REG_R_LOCAL_CRIT 0x20
119#define LM90_REG_W_LOCAL_CRIT 0x20
120#define LM90_REG_R_REMOTE_TEMPH 0x01
121#define LM90_REG_R_REMOTE_TEMPL 0x10
122#define LM90_REG_R_REMOTE_OFFSH 0x11
123#define LM90_REG_W_REMOTE_OFFSH 0x11
124#define LM90_REG_R_REMOTE_OFFSL 0x12
125#define LM90_REG_W_REMOTE_OFFSL 0x12
126#define LM90_REG_R_REMOTE_HIGHH 0x07
127#define LM90_REG_W_REMOTE_HIGHH 0x0D
128#define LM90_REG_R_REMOTE_HIGHL 0x13
129#define LM90_REG_W_REMOTE_HIGHL 0x13
130#define LM90_REG_R_REMOTE_LOWH 0x08
131#define LM90_REG_W_REMOTE_LOWH 0x0E
132#define LM90_REG_R_REMOTE_LOWL 0x14
133#define LM90_REG_W_REMOTE_LOWL 0x14
134#define LM90_REG_R_REMOTE_CRIT 0x19
135#define LM90_REG_W_REMOTE_CRIT 0x19
136#define LM90_REG_R_TCRIT_HYST 0x21
137#define LM90_REG_W_TCRIT_HYST 0x21
138
139/*
140 * Conversions and various macros
141 * For local temperatures and limits, critical limits and the hysteresis
142 * value, the LM90 uses signed 8-bit values with LSB = 1 degree Celcius.
143 * For remote temperatures and limits, it uses signed 11-bit values with
144 * LSB = 0.125 degree Celcius, left-justified in 16-bit registers.
145 */
146
147#define TEMP1_FROM_REG(val) ((val) * 1000)
148#define TEMP1_TO_REG(val) ((val) <= -128000 ? -128 : \
149 (val) >= 127000 ? 127 : \
150 (val) < 0 ? ((val) - 500) / 1000 : \
151 ((val) + 500) / 1000)
152#define TEMP2_FROM_REG(val) ((val) / 32 * 125)
153#define TEMP2_TO_REG(val) ((val) <= -128000 ? 0x8000 : \
154 (val) >= 127875 ? 0x7FE0 : \
155 (val) < 0 ? ((val) - 62) / 125 * 32 : \
156 ((val) + 62) / 125 * 32)
157#define HYST_TO_REG(val) ((val) <= 0 ? 0 : (val) >= 30500 ? 31 : \
158 ((val) + 500) / 1000)
159
160/*
161 * ADT7461 is almost identical to LM90 except that attempts to write
162 * values that are outside the range 0 < temp < 127 are treated as
163 * the boundary value.
164 */
165
166#define TEMP1_TO_REG_ADT7461(val) ((val) <= 0 ? 0 : \
167 (val) >= 127000 ? 127 : \
168 ((val) + 500) / 1000)
169#define TEMP2_TO_REG_ADT7461(val) ((val) <= 0 ? 0 : \
170 (val) >= 127750 ? 0x7FC0 : \
171 ((val) + 125) / 250 * 64)
172
173/*
174 * Functions declaration
175 */
176
177static int lm90_attach_adapter(struct i2c_adapter *adapter);
178static int lm90_detect(struct i2c_adapter *adapter, int address,
179 int kind);
180static void lm90_init_client(struct i2c_client *client);
181static int lm90_detach_client(struct i2c_client *client);
182static struct lm90_data *lm90_update_device(struct device *dev);
183
184/*
185 * Driver data (common to all clients)
186 */
187
188static struct i2c_driver lm90_driver = {
189 .owner = THIS_MODULE,
190 .name = "lm90",
191 .id = I2C_DRIVERID_LM90,
192 .flags = I2C_DF_NOTIFY,
193 .attach_adapter = lm90_attach_adapter,
194 .detach_client = lm90_detach_client,
195};
196
197/*
198 * Client data (each client gets its own)
199 */
200
201struct lm90_data {
202 struct i2c_client client;
203 struct semaphore update_lock;
204 char valid; /* zero until following fields are valid */
205 unsigned long last_updated; /* in jiffies */
206 int kind;
207
208 /* registers values */
209 s8 temp_input1, temp_low1, temp_high1; /* local */
210 s16 temp_input2, temp_low2, temp_high2; /* remote, combined */
211 s8 temp_crit1, temp_crit2;
212 u8 temp_hyst;
213 u8 alarms; /* bitvector */
214};
215
216/*
217 * Sysfs stuff
218 */
219
220#define show_temp(value, converter) \
221static ssize_t show_##value(struct device *dev, char *buf) \
222{ \
223 struct lm90_data *data = lm90_update_device(dev); \
224 return sprintf(buf, "%d\n", converter(data->value)); \
225}
226show_temp(temp_input1, TEMP1_FROM_REG);
227show_temp(temp_input2, TEMP2_FROM_REG);
228show_temp(temp_low1, TEMP1_FROM_REG);
229show_temp(temp_low2, TEMP2_FROM_REG);
230show_temp(temp_high1, TEMP1_FROM_REG);
231show_temp(temp_high2, TEMP2_FROM_REG);
232show_temp(temp_crit1, TEMP1_FROM_REG);
233show_temp(temp_crit2, TEMP1_FROM_REG);
234
235#define set_temp1(value, reg) \
236static ssize_t set_##value(struct device *dev, const char *buf, \
237 size_t count) \
238{ \
239 struct i2c_client *client = to_i2c_client(dev); \
240 struct lm90_data *data = i2c_get_clientdata(client); \
241 long val = simple_strtol(buf, NULL, 10); \
242 \
243 down(&data->update_lock); \
244 if (data->kind == adt7461) \
245 data->value = TEMP1_TO_REG_ADT7461(val); \
246 else \
247 data->value = TEMP1_TO_REG(val); \
248 i2c_smbus_write_byte_data(client, reg, data->value); \
249 up(&data->update_lock); \
250 return count; \
251}
252#define set_temp2(value, regh, regl) \
253static ssize_t set_##value(struct device *dev, const char *buf, \
254 size_t count) \
255{ \
256 struct i2c_client *client = to_i2c_client(dev); \
257 struct lm90_data *data = i2c_get_clientdata(client); \
258 long val = simple_strtol(buf, NULL, 10); \
259 \
260 down(&data->update_lock); \
261 if (data->kind == adt7461) \
262 data->value = TEMP2_TO_REG_ADT7461(val); \
263 else \
264 data->value = TEMP2_TO_REG(val); \
265 i2c_smbus_write_byte_data(client, regh, data->value >> 8); \
266 i2c_smbus_write_byte_data(client, regl, data->value & 0xff); \
267 up(&data->update_lock); \
268 return count; \
269}
270set_temp1(temp_low1, LM90_REG_W_LOCAL_LOW);
271set_temp2(temp_low2, LM90_REG_W_REMOTE_LOWH, LM90_REG_W_REMOTE_LOWL);
272set_temp1(temp_high1, LM90_REG_W_LOCAL_HIGH);
273set_temp2(temp_high2, LM90_REG_W_REMOTE_HIGHH, LM90_REG_W_REMOTE_HIGHL);
274set_temp1(temp_crit1, LM90_REG_W_LOCAL_CRIT);
275set_temp1(temp_crit2, LM90_REG_W_REMOTE_CRIT);
276
277#define show_temp_hyst(value, basereg) \
278static ssize_t show_##value(struct device *dev, char *buf) \
279{ \
280 struct lm90_data *data = lm90_update_device(dev); \
281 return sprintf(buf, "%d\n", TEMP1_FROM_REG(data->basereg) \
282 - TEMP1_FROM_REG(data->temp_hyst)); \
283}
284show_temp_hyst(temp_hyst1, temp_crit1);
285show_temp_hyst(temp_hyst2, temp_crit2);
286
287static ssize_t set_temp_hyst1(struct device *dev, const char *buf,
288 size_t count)
289{
290 struct i2c_client *client = to_i2c_client(dev);
291 struct lm90_data *data = i2c_get_clientdata(client);
292 long val = simple_strtol(buf, NULL, 10);
293 long hyst;
294
295 down(&data->update_lock);
296 hyst = TEMP1_FROM_REG(data->temp_crit1) - val;
297 i2c_smbus_write_byte_data(client, LM90_REG_W_TCRIT_HYST,
298 HYST_TO_REG(hyst));
299 up(&data->update_lock);
300 return count;
301}
302
303static ssize_t show_alarms(struct device *dev, char *buf)
304{
305 struct lm90_data *data = lm90_update_device(dev);
306 return sprintf(buf, "%d\n", data->alarms);
307}
308
309static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input1, NULL);
310static DEVICE_ATTR(temp2_input, S_IRUGO, show_temp_input2, NULL);
311static DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp_low1,
312 set_temp_low1);
313static DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp_low2,
314 set_temp_low2);
315static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_high1,
316 set_temp_high1);
317static DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp_high2,
318 set_temp_high2);
319static DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_temp_crit1,
320 set_temp_crit1);
321static DEVICE_ATTR(temp2_crit, S_IWUSR | S_IRUGO, show_temp_crit2,
322 set_temp_crit2);
323static DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, show_temp_hyst1,
324 set_temp_hyst1);
325static DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, show_temp_hyst2, NULL);
326static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
327
328/*
329 * Real code
330 */
331
332static int lm90_attach_adapter(struct i2c_adapter *adapter)
333{
334 if (!(adapter->class & I2C_CLASS_HWMON))
335 return 0;
336 return i2c_detect(adapter, &addr_data, lm90_detect);
337}
338
339/*
340 * The following function does more than just detection. If detection
341 * succeeds, it also registers the new chip.
342 */
343static int lm90_detect(struct i2c_adapter *adapter, int address, int kind)
344{
345 struct i2c_client *new_client;
346 struct lm90_data *data;
347 int err = 0;
348 const char *name = "";
349
350 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
351 goto exit;
352
353 if (!(data = kmalloc(sizeof(struct lm90_data), GFP_KERNEL))) {
354 err = -ENOMEM;
355 goto exit;
356 }
357 memset(data, 0, sizeof(struct lm90_data));
358
359 /* The common I2C client data is placed right before the
360 LM90-specific data. */
361 new_client = &data->client;
362 i2c_set_clientdata(new_client, data);
363 new_client->addr = address;
364 new_client->adapter = adapter;
365 new_client->driver = &lm90_driver;
366 new_client->flags = 0;
367
368 /*
369 * Now we do the remaining detection. A negative kind means that
370 * the driver was loaded with no force parameter (default), so we
371 * must both detect and identify the chip. A zero kind means that
372 * the driver was loaded with the force parameter, the detection
373 * step shall be skipped. A positive kind means that the driver
374 * was loaded with the force parameter and a given kind of chip is
375 * requested, so both the detection and the identification steps
376 * are skipped.
377 */
378
379 /* Default to an LM90 if forced */
380 if (kind == 0)
381 kind = lm90;
382
383 if (kind < 0) { /* detection and identification */
384 u8 man_id, chip_id, reg_config1, reg_convrate;
385
386 man_id = i2c_smbus_read_byte_data(new_client,
387 LM90_REG_R_MAN_ID);
388 chip_id = i2c_smbus_read_byte_data(new_client,
389 LM90_REG_R_CHIP_ID);
390 reg_config1 = i2c_smbus_read_byte_data(new_client,
391 LM90_REG_R_CONFIG1);
392 reg_convrate = i2c_smbus_read_byte_data(new_client,
393 LM90_REG_R_CONVRATE);
394
395 if (man_id == 0x01) { /* National Semiconductor */
396 u8 reg_config2;
397
398 reg_config2 = i2c_smbus_read_byte_data(new_client,
399 LM90_REG_R_CONFIG2);
400
401 if ((reg_config1 & 0x2A) == 0x00
402 && (reg_config2 & 0xF8) == 0x00
403 && reg_convrate <= 0x09) {
404 if (address == 0x4C
405 && (chip_id & 0xF0) == 0x20) { /* LM90 */
406 kind = lm90;
407 } else
408 if ((chip_id & 0xF0) == 0x30) { /* LM89/LM99 */
409 kind = lm99;
410 } else
411 if (address == 0x4C
412 && (chip_id & 0xF0) == 0x10) { /* LM86 */
413 kind = lm86;
414 }
415 }
416 } else
417 if (man_id == 0x41) { /* Analog Devices */
418 if (address == 0x4C
419 && (chip_id & 0xF0) == 0x40 /* ADM1032 */
420 && (reg_config1 & 0x3F) == 0x00
421 && reg_convrate <= 0x0A) {
422 kind = adm1032;
423 } else
424 if (address == 0x4c
425 && chip_id == 0x51 /* ADT7461 */
426 && (reg_config1 & 0x1F) == 0x00 /* check compat mode */
427 && reg_convrate <= 0x0A) {
428 kind = adt7461;
429 }
430 } else
431 if (man_id == 0x4D) { /* Maxim */
432 /*
433 * The Maxim variants do NOT have a chip_id register.
434 * Reading from that address will return the last read
435 * value, which in our case is those of the man_id
436 * register. Likewise, the config1 register seems to
437 * lack a low nibble, so the value will be those of the
438 * previous read, so in our case those of the man_id
439 * register.
440 */
441 if (chip_id == man_id
442 && (reg_config1 & 0x1F) == (man_id & 0x0F)
443 && reg_convrate <= 0x09) {
444 kind = max6657;
445 }
446 }
447
448 if (kind <= 0) { /* identification failed */
449 dev_info(&adapter->dev,
450 "Unsupported chip (man_id=0x%02X, "
451 "chip_id=0x%02X).\n", man_id, chip_id);
452 goto exit_free;
453 }
454 }
455
456 if (kind == lm90) {
457 name = "lm90";
458 } else if (kind == adm1032) {
459 name = "adm1032";
460 } else if (kind == lm99) {
461 name = "lm99";
462 } else if (kind == lm86) {
463 name = "lm86";
464 } else if (kind == max6657) {
465 name = "max6657";
466 } else if (kind == adt7461) {
467 name = "adt7461";
468 }
469
470 /* We can fill in the remaining client fields */
471 strlcpy(new_client->name, name, I2C_NAME_SIZE);
472 data->valid = 0;
473 data->kind = kind;
474 init_MUTEX(&data->update_lock);
475
476 /* Tell the I2C layer a new client has arrived */
477 if ((err = i2c_attach_client(new_client)))
478 goto exit_free;
479
480 /* Initialize the LM90 chip */
481 lm90_init_client(new_client);
482
483 /* Register sysfs hooks */
484 device_create_file(&new_client->dev, &dev_attr_temp1_input);
485 device_create_file(&new_client->dev, &dev_attr_temp2_input);
486 device_create_file(&new_client->dev, &dev_attr_temp1_min);
487 device_create_file(&new_client->dev, &dev_attr_temp2_min);
488 device_create_file(&new_client->dev, &dev_attr_temp1_max);
489 device_create_file(&new_client->dev, &dev_attr_temp2_max);
490 device_create_file(&new_client->dev, &dev_attr_temp1_crit);
491 device_create_file(&new_client->dev, &dev_attr_temp2_crit);
492 device_create_file(&new_client->dev, &dev_attr_temp1_crit_hyst);
493 device_create_file(&new_client->dev, &dev_attr_temp2_crit_hyst);
494 device_create_file(&new_client->dev, &dev_attr_alarms);
495
496 return 0;
497
498exit_free:
499 kfree(data);
500exit:
501 return err;
502}
503
504static void lm90_init_client(struct i2c_client *client)
505{
506 u8 config;
507
508 /*
509 * Start the conversions.
510 */
511 i2c_smbus_write_byte_data(client, LM90_REG_W_CONVRATE,
512 5); /* 2 Hz */
513 config = i2c_smbus_read_byte_data(client, LM90_REG_R_CONFIG1);
514 if (config & 0x40)
515 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
516 config & 0xBF); /* run */
517}
518
519static int lm90_detach_client(struct i2c_client *client)
520{
521 int err;
522
523 if ((err = i2c_detach_client(client))) {
524 dev_err(&client->dev, "Client deregistration failed, "
525 "client not detached.\n");
526 return err;
527 }
528
529 kfree(i2c_get_clientdata(client));
530 return 0;
531}
532
533static struct lm90_data *lm90_update_device(struct device *dev)
534{
535 struct i2c_client *client = to_i2c_client(dev);
536 struct lm90_data *data = i2c_get_clientdata(client);
537
538 down(&data->update_lock);
539
540 if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
541 u8 oldh, newh;
542
543 dev_dbg(&client->dev, "Updating lm90 data.\n");
544 data->temp_input1 = i2c_smbus_read_byte_data(client,
545 LM90_REG_R_LOCAL_TEMP);
546 data->temp_high1 = i2c_smbus_read_byte_data(client,
547 LM90_REG_R_LOCAL_HIGH);
548 data->temp_low1 = i2c_smbus_read_byte_data(client,
549 LM90_REG_R_LOCAL_LOW);
550 data->temp_crit1 = i2c_smbus_read_byte_data(client,
551 LM90_REG_R_LOCAL_CRIT);
552 data->temp_crit2 = i2c_smbus_read_byte_data(client,
553 LM90_REG_R_REMOTE_CRIT);
554 data->temp_hyst = i2c_smbus_read_byte_data(client,
555 LM90_REG_R_TCRIT_HYST);
556
557 /*
558 * There is a trick here. We have to read two registers to
559 * have the remote sensor temperature, but we have to beware
560 * a conversion could occur inbetween the readings. The
561 * datasheet says we should either use the one-shot
562 * conversion register, which we don't want to do (disables
563 * hardware monitoring) or monitor the busy bit, which is
564 * impossible (we can't read the values and monitor that bit
565 * at the exact same time). So the solution used here is to
566 * read the high byte once, then the low byte, then the high
567 * byte again. If the new high byte matches the old one,
568 * then we have a valid reading. Else we have to read the low
569 * byte again, and now we believe we have a correct reading.
570 */
571 oldh = i2c_smbus_read_byte_data(client,
572 LM90_REG_R_REMOTE_TEMPH);
573 data->temp_input2 = i2c_smbus_read_byte_data(client,
574 LM90_REG_R_REMOTE_TEMPL);
575 newh = i2c_smbus_read_byte_data(client,
576 LM90_REG_R_REMOTE_TEMPH);
577 if (newh != oldh) {
578 data->temp_input2 = i2c_smbus_read_byte_data(client,
579 LM90_REG_R_REMOTE_TEMPL);
580#ifdef DEBUG
581 oldh = i2c_smbus_read_byte_data(client,
582 LM90_REG_R_REMOTE_TEMPH);
583 /* oldh is actually newer */
584 if (newh != oldh)
585 dev_warn(&client->dev, "Remote temperature may be "
586 "wrong.\n");
587#endif
588 }
589 data->temp_input2 |= (newh << 8);
590
591 data->temp_high2 = (i2c_smbus_read_byte_data(client,
592 LM90_REG_R_REMOTE_HIGHH) << 8) +
593 i2c_smbus_read_byte_data(client,
594 LM90_REG_R_REMOTE_HIGHL);
595 data->temp_low2 = (i2c_smbus_read_byte_data(client,
596 LM90_REG_R_REMOTE_LOWH) << 8) +
597 i2c_smbus_read_byte_data(client,
598 LM90_REG_R_REMOTE_LOWL);
599 data->alarms = i2c_smbus_read_byte_data(client,
600 LM90_REG_R_STATUS);
601
602 data->last_updated = jiffies;
603 data->valid = 1;
604 }
605
606 up(&data->update_lock);
607
608 return data;
609}
610
611static int __init sensors_lm90_init(void)
612{
613 return i2c_add_driver(&lm90_driver);
614}
615
616static void __exit sensors_lm90_exit(void)
617{
618 i2c_del_driver(&lm90_driver);
619}
620
621MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
622MODULE_DESCRIPTION("LM90/ADM1032 driver");
623MODULE_LICENSE("GPL");
624
625module_init(sensors_lm90_init);
626module_exit(sensors_lm90_exit);
diff --git a/drivers/i2c/chips/lm92.c b/drivers/i2c/chips/lm92.c
new file mode 100644
index 000000000000..fe6e83d70a72
--- /dev/null
+++ b/drivers/i2c/chips/lm92.c
@@ -0,0 +1,429 @@
1/*
2 * lm92 - Hardware monitoring driver
3 * Copyright (C) 2005 Jean Delvare <khali@linux-fr.org>
4 *
5 * Based on the lm90 driver, with some ideas taken from the lm_sensors
6 * lm92 driver as well.
7 *
8 * The LM92 is a sensor chip made by National Semiconductor. It reports
9 * its own temperature with a 0.0625 deg resolution and a 0.33 deg
10 * accuracy. Complete datasheet can be obtained from National's website
11 * at:
12 * http://www.national.com/pf/LM/LM92.html
13 *
14 * This driver also supports the MAX6635 sensor chip made by Maxim.
15 * This chip is compatible with the LM92, but has a lesser accuracy
16 * (1.0 deg). Complete datasheet can be obtained from Maxim's website
17 * at:
18 * http://www.maxim-ic.com/quick_view2.cfm/qv_pk/3074
19 *
20 * Since the LM92 was the first chipset supported by this driver, most
21 * comments will refer to this chipset, but are actually general and
22 * concern all supported chipsets, unless mentioned otherwise.
23 *
24 * Support could easily be added for the National Semiconductor LM76
25 * and Maxim MAX6633 and MAX6634 chips, which are mostly compatible
26 * with the LM92.
27 *
28 * This program is free software; you can redistribute it and/or modify
29 * it under the terms of the GNU General Public License as published by
30 * the Free Software Foundation; either version 2 of the License, or
31 * (at your option) any later version.
32 *
33 * This program is distributed in the hope that it will be useful,
34 * but WITHOUT ANY WARRANTY; without even the implied warranty of
35 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36 * GNU General Public License for more details.
37 *
38 * You should have received a copy of the GNU General Public License
39 * along with this program; if not, write to the Free Software
40 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
41 */
42
43#include <linux/module.h>
44#include <linux/init.h>
45#include <linux/slab.h>
46#include <linux/i2c.h>
47#include <linux/i2c-sensor.h>
48
49
50/* The LM92 and MAX6635 have 2 two-state pins for address selection,
51 resulting in 4 possible addresses. */
52static unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b,
53 I2C_CLIENT_END };
54static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
55
56/* Insmod parameters */
57SENSORS_INSMOD_1(lm92);
58
59/* The LM92 registers */
60#define LM92_REG_CONFIG 0x01 /* 8-bit, RW */
61#define LM92_REG_TEMP 0x00 /* 16-bit, RO */
62#define LM92_REG_TEMP_HYST 0x02 /* 16-bit, RW */
63#define LM92_REG_TEMP_CRIT 0x03 /* 16-bit, RW */
64#define LM92_REG_TEMP_LOW 0x04 /* 16-bit, RW */
65#define LM92_REG_TEMP_HIGH 0x05 /* 16-bit, RW */
66#define LM92_REG_MAN_ID 0x07 /* 16-bit, RO, LM92 only */
67
68/* The LM92 uses signed 13-bit values with LSB = 0.0625 degree Celsius,
69 left-justified in 16-bit registers. No rounding is done, with such
70 a resolution it's just not worth it. Note that the MAX6635 doesn't
71 make use of the 4 lower bits for limits (i.e. effective resolution
72 for limits is 1 degree Celsius). */
73static inline int TEMP_FROM_REG(s16 reg)
74{
75 return reg / 8 * 625 / 10;
76}
77
78static inline s16 TEMP_TO_REG(int val)
79{
80 if (val <= -60000)
81 return -60000 * 10 / 625 * 8;
82 if (val >= 160000)
83 return 160000 * 10 / 625 * 8;
84 return val * 10 / 625 * 8;
85}
86
87/* Alarm flags are stored in the 3 LSB of the temperature register */
88static inline u8 ALARMS_FROM_REG(s16 reg)
89{
90 return reg & 0x0007;
91}
92
93/* Driver data (common to all clients) */
94static struct i2c_driver lm92_driver;
95
96/* Client data (each client gets its own) */
97struct lm92_data {
98 struct i2c_client client;
99 struct semaphore update_lock;
100 char valid; /* zero until following fields are valid */
101 unsigned long last_updated; /* in jiffies */
102
103 /* registers values */
104 s16 temp1_input, temp1_crit, temp1_min, temp1_max, temp1_hyst;
105};
106
107
108/*
109 * Sysfs attributes and callback functions
110 */
111
112static struct lm92_data *lm92_update_device(struct device *dev)
113{
114 struct i2c_client *client = to_i2c_client(dev);
115 struct lm92_data *data = i2c_get_clientdata(client);
116
117 down(&data->update_lock);
118
119 if (time_after(jiffies, data->last_updated + HZ)
120 || !data->valid) {
121 dev_dbg(&client->dev, "Updating lm92 data\n");
122 data->temp1_input = swab16(i2c_smbus_read_word_data(client,
123 LM92_REG_TEMP));
124 data->temp1_hyst = swab16(i2c_smbus_read_word_data(client,
125 LM92_REG_TEMP_HYST));
126 data->temp1_crit = swab16(i2c_smbus_read_word_data(client,
127 LM92_REG_TEMP_CRIT));
128 data->temp1_min = swab16(i2c_smbus_read_word_data(client,
129 LM92_REG_TEMP_LOW));
130 data->temp1_max = swab16(i2c_smbus_read_word_data(client,
131 LM92_REG_TEMP_HIGH));
132
133 data->last_updated = jiffies;
134 data->valid = 1;
135 }
136
137 up(&data->update_lock);
138
139 return data;
140}
141
142#define show_temp(value) \
143static ssize_t show_##value(struct device *dev, char *buf) \
144{ \
145 struct lm92_data *data = lm92_update_device(dev); \
146 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->value)); \
147}
148show_temp(temp1_input);
149show_temp(temp1_crit);
150show_temp(temp1_min);
151show_temp(temp1_max);
152
153#define set_temp(value, reg) \
154static ssize_t set_##value(struct device *dev, const char *buf, \
155 size_t count) \
156{ \
157 struct i2c_client *client = to_i2c_client(dev); \
158 struct lm92_data *data = i2c_get_clientdata(client); \
159 long val = simple_strtol(buf, NULL, 10); \
160 \
161 down(&data->update_lock); \
162 data->value = TEMP_TO_REG(val); \
163 i2c_smbus_write_word_data(client, reg, swab16(data->value)); \
164 up(&data->update_lock); \
165 return count; \
166}
167set_temp(temp1_crit, LM92_REG_TEMP_CRIT);
168set_temp(temp1_min, LM92_REG_TEMP_LOW);
169set_temp(temp1_max, LM92_REG_TEMP_HIGH);
170
171static ssize_t show_temp1_crit_hyst(struct device *dev, char *buf)
172{
173 struct lm92_data *data = lm92_update_device(dev);
174 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp1_crit)
175 - TEMP_FROM_REG(data->temp1_hyst));
176}
177static ssize_t show_temp1_max_hyst(struct device *dev, char *buf)
178{
179 struct lm92_data *data = lm92_update_device(dev);
180 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp1_max)
181 - TEMP_FROM_REG(data->temp1_hyst));
182}
183static ssize_t show_temp1_min_hyst(struct device *dev, char *buf)
184{
185 struct lm92_data *data = lm92_update_device(dev);
186 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp1_min)
187 + TEMP_FROM_REG(data->temp1_hyst));
188}
189
190static ssize_t set_temp1_crit_hyst(struct device *dev, const char *buf,
191 size_t count)
192{
193 struct i2c_client *client = to_i2c_client(dev);
194 struct lm92_data *data = i2c_get_clientdata(client);
195 long val = simple_strtol(buf, NULL, 10);
196
197 down(&data->update_lock);
198 data->temp1_hyst = TEMP_FROM_REG(data->temp1_crit) - val;
199 i2c_smbus_write_word_data(client, LM92_REG_TEMP_HYST,
200 swab16(TEMP_TO_REG(data->temp1_hyst)));
201 up(&data->update_lock);
202 return count;
203}
204
205static ssize_t show_alarms(struct device *dev, char *buf)
206{
207 struct lm92_data *data = lm92_update_device(dev);
208 return sprintf(buf, "%d\n", ALARMS_FROM_REG(data->temp1_input));
209}
210
211static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp1_input, NULL);
212static DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_temp1_crit,
213 set_temp1_crit);
214static DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, show_temp1_crit_hyst,
215 set_temp1_crit_hyst);
216static DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp1_min,
217 set_temp1_min);
218static DEVICE_ATTR(temp1_min_hyst, S_IRUGO, show_temp1_min_hyst, NULL);
219static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp1_max,
220 set_temp1_max);
221static DEVICE_ATTR(temp1_max_hyst, S_IRUGO, show_temp1_max_hyst, NULL);
222static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
223
224
225/*
226 * Detection and registration
227 */
228
229static void lm92_init_client(struct i2c_client *client)
230{
231 u8 config;
232
233 /* Start the conversions if needed */
234 config = i2c_smbus_read_byte_data(client, LM92_REG_CONFIG);
235 if (config & 0x01)
236 i2c_smbus_write_byte_data(client, LM92_REG_CONFIG,
237 config & 0xFE);
238}
239
240/* The MAX6635 has no identification register, so we have to use tricks
241 to identify it reliably. This is somewhat slow.
242 Note that we do NOT rely on the 2 MSB of the configuration register
243 always reading 0, as suggested by the datasheet, because it was once
244 reported not to be true. */
245static int max6635_check(struct i2c_client *client)
246{
247 u16 temp_low, temp_high, temp_hyst, temp_crit;
248 u8 conf;
249 int i;
250
251 /* No manufacturer ID register, so a read from this address will
252 always return the last read value. */
253 temp_low = i2c_smbus_read_word_data(client, LM92_REG_TEMP_LOW);
254 if (i2c_smbus_read_word_data(client, LM92_REG_MAN_ID) != temp_low)
255 return 0;
256 temp_high = i2c_smbus_read_word_data(client, LM92_REG_TEMP_HIGH);
257 if (i2c_smbus_read_word_data(client, LM92_REG_MAN_ID) != temp_high)
258 return 0;
259
260 /* Limits are stored as integer values (signed, 9-bit). */
261 if ((temp_low & 0x7f00) || (temp_high & 0x7f00))
262 return 0;
263 temp_hyst = i2c_smbus_read_word_data(client, LM92_REG_TEMP_HYST);
264 temp_crit = i2c_smbus_read_word_data(client, LM92_REG_TEMP_CRIT);
265 if ((temp_hyst & 0x7f00) || (temp_crit & 0x7f00))
266 return 0;
267
268 /* Registers addresses were found to cycle over 16-byte boundaries.
269 We don't test all registers with all offsets so as to save some
270 reads and time, but this should still be sufficient to dismiss
271 non-MAX6635 chips. */
272 conf = i2c_smbus_read_byte_data(client, LM92_REG_CONFIG);
273 for (i=16; i<96; i*=2) {
274 if (temp_hyst != i2c_smbus_read_word_data(client,
275 LM92_REG_TEMP_HYST + i - 16)
276 || temp_crit != i2c_smbus_read_word_data(client,
277 LM92_REG_TEMP_CRIT + i)
278 || temp_low != i2c_smbus_read_word_data(client,
279 LM92_REG_TEMP_LOW + i + 16)
280 || temp_high != i2c_smbus_read_word_data(client,
281 LM92_REG_TEMP_HIGH + i + 32)
282 || conf != i2c_smbus_read_byte_data(client,
283 LM92_REG_CONFIG + i))
284 return 0;
285 }
286
287 return 1;
288}
289
290/* The following function does more than just detection. If detection
291 succeeds, it also registers the new chip. */
292static int lm92_detect(struct i2c_adapter *adapter, int address, int kind)
293{
294 struct i2c_client *new_client;
295 struct lm92_data *data;
296 int err = 0;
297 char *name;
298
299 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA
300 | I2C_FUNC_SMBUS_WORD_DATA))
301 goto exit;
302
303 if (!(data = kmalloc(sizeof(struct lm92_data), GFP_KERNEL))) {
304 err = -ENOMEM;
305 goto exit;
306 }
307 memset(data, 0, sizeof(struct lm92_data));
308
309 /* Fill in enough client fields so that we can read from the chip,
310 which is required for identication */
311 new_client = &data->client;
312 i2c_set_clientdata(new_client, data);
313 new_client->addr = address;
314 new_client->adapter = adapter;
315 new_client->driver = &lm92_driver;
316 new_client->flags = 0;
317
318 /* A negative kind means that the driver was loaded with no force
319 parameter (default), so we must identify the chip. */
320 if (kind < 0) {
321 u8 config = i2c_smbus_read_byte_data(new_client,
322 LM92_REG_CONFIG);
323 u16 man_id = i2c_smbus_read_word_data(new_client,
324 LM92_REG_MAN_ID);
325
326 if ((config & 0xe0) == 0x00
327 && man_id == 0x0180) {
328 pr_info("lm92: Found National Semiconductor LM92 chip\n");
329 kind = lm92;
330 } else
331 if (max6635_check(new_client)) {
332 pr_info("lm92: Found Maxim MAX6635 chip\n");
333 kind = lm92; /* No separate prefix */
334 }
335 else
336 goto exit_free;
337 } else
338 if (kind == 0) /* Default to an LM92 if forced */
339 kind = lm92;
340
341 /* Give it the proper name */
342 if (kind == lm92) {
343 name = "lm92";
344 } else { /* Supposedly cannot happen */
345 dev_dbg(&new_client->dev, "Kind out of range?\n");
346 goto exit_free;
347 }
348
349 /* Fill in the remaining client fields */
350 strlcpy(new_client->name, name, I2C_NAME_SIZE);
351 data->valid = 0;
352 init_MUTEX(&data->update_lock);
353
354 /* Tell the i2c subsystem a new client has arrived */
355 if ((err = i2c_attach_client(new_client)))
356 goto exit_free;
357
358 /* Initialize the chipset */
359 lm92_init_client(new_client);
360
361 /* Register sysfs hooks */
362 device_create_file(&new_client->dev, &dev_attr_temp1_input);
363 device_create_file(&new_client->dev, &dev_attr_temp1_crit);
364 device_create_file(&new_client->dev, &dev_attr_temp1_crit_hyst);
365 device_create_file(&new_client->dev, &dev_attr_temp1_min);
366 device_create_file(&new_client->dev, &dev_attr_temp1_min_hyst);
367 device_create_file(&new_client->dev, &dev_attr_temp1_max);
368 device_create_file(&new_client->dev, &dev_attr_temp1_max_hyst);
369 device_create_file(&new_client->dev, &dev_attr_alarms);
370
371 return 0;
372
373exit_free:
374 kfree(data);
375exit:
376 return err;
377}
378
379static int lm92_attach_adapter(struct i2c_adapter *adapter)
380{
381 if (!(adapter->class & I2C_CLASS_HWMON))
382 return 0;
383 return i2c_detect(adapter, &addr_data, lm92_detect);
384}
385
386static int lm92_detach_client(struct i2c_client *client)
387{
388 int err;
389
390 if ((err = i2c_detach_client(client))) {
391 dev_err(&client->dev, "Client deregistration failed, "
392 "client not detached.\n");
393 return err;
394 }
395
396 kfree(i2c_get_clientdata(client));
397 return 0;
398}
399
400
401/*
402 * Module and driver stuff
403 */
404
405static struct i2c_driver lm92_driver = {
406 .owner = THIS_MODULE,
407 .name = "lm92",
408 .id = I2C_DRIVERID_LM92,
409 .flags = I2C_DF_NOTIFY,
410 .attach_adapter = lm92_attach_adapter,
411 .detach_client = lm92_detach_client,
412};
413
414static int __init sensors_lm92_init(void)
415{
416 return i2c_add_driver(&lm92_driver);
417}
418
419static void __exit sensors_lm92_exit(void)
420{
421 i2c_del_driver(&lm92_driver);
422}
423
424MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
425MODULE_DESCRIPTION("LM92/MAX6635 driver");
426MODULE_LICENSE("GPL");
427
428module_init(sensors_lm92_init);
429module_exit(sensors_lm92_exit);
diff --git a/drivers/i2c/chips/m41t00.c b/drivers/i2c/chips/m41t00.c
new file mode 100644
index 000000000000..e771566dffa8
--- /dev/null
+++ b/drivers/i2c/chips/m41t00.c
@@ -0,0 +1,246 @@
1/*
2 * drivers/i2c/chips/m41t00.c
3 *
4 * I2C client/driver for the ST M41T00 Real-Time Clock chip.
5 *
6 * Author: Mark A. Greer <mgreer@mvista.com>
7 *
8 * 2005 (c) MontaVista Software, Inc. This file is licensed under
9 * the terms of the GNU General Public License version 2. This program
10 * is licensed "as is" without any warranty of any kind, whether express
11 * or implied.
12 */
13/*
14 * This i2c client/driver wedges between the drivers/char/genrtc.c RTC
15 * interface and the SMBus interface of the i2c subsystem.
16 * It would be more efficient to use i2c msgs/i2c_transfer directly but, as
17 * recommened in .../Documentation/i2c/writing-clients section
18 * "Sending and receiving", using SMBus level communication is preferred.
19 */
20
21#include <linux/kernel.h>
22#include <linux/module.h>
23#include <linux/interrupt.h>
24#include <linux/i2c.h>
25#include <linux/rtc.h>
26#include <linux/bcd.h>
27
28#include <asm/time.h>
29#include <asm/rtc.h>
30
31#define M41T00_DRV_NAME "m41t00"
32
33static DECLARE_MUTEX(m41t00_mutex);
34
35static struct i2c_driver m41t00_driver;
36static struct i2c_client *save_client;
37
38static unsigned short ignore[] = { I2C_CLIENT_END };
39static unsigned short normal_addr[] = { 0x68, I2C_CLIENT_END };
40
41static struct i2c_client_address_data addr_data = {
42 .normal_i2c = normal_addr,
43 .normal_i2c_range = ignore,
44 .probe = ignore,
45 .probe_range = ignore,
46 .ignore = ignore,
47 .ignore_range = ignore,
48 .force = ignore,
49};
50
51ulong
52m41t00_get_rtc_time(void)
53{
54 s32 sec, min, hour, day, mon, year;
55 s32 sec1, min1, hour1, day1, mon1, year1;
56 ulong limit = 10;
57
58 sec = min = hour = day = mon = year = 0;
59 sec1 = min1 = hour1 = day1 = mon1 = year1 = 0;
60
61 down(&m41t00_mutex);
62 do {
63 if (((sec = i2c_smbus_read_byte_data(save_client, 0)) >= 0)
64 && ((min = i2c_smbus_read_byte_data(save_client, 1))
65 >= 0)
66 && ((hour = i2c_smbus_read_byte_data(save_client, 2))
67 >= 0)
68 && ((day = i2c_smbus_read_byte_data(save_client, 4))
69 >= 0)
70 && ((mon = i2c_smbus_read_byte_data(save_client, 5))
71 >= 0)
72 && ((year = i2c_smbus_read_byte_data(save_client, 6))
73 >= 0)
74 && ((sec == sec1) && (min == min1) && (hour == hour1)
75 && (day == day1) && (mon == mon1)
76 && (year == year1)))
77
78 break;
79
80 sec1 = sec;
81 min1 = min;
82 hour1 = hour;
83 day1 = day;
84 mon1 = mon;
85 year1 = year;
86 } while (--limit > 0);
87 up(&m41t00_mutex);
88
89 if (limit == 0) {
90 dev_warn(&save_client->dev,
91 "m41t00: can't read rtc chip\n");
92 sec = min = hour = day = mon = year = 0;
93 }
94
95 sec &= 0x7f;
96 min &= 0x7f;
97 hour &= 0x3f;
98 day &= 0x3f;
99 mon &= 0x1f;
100 year &= 0xff;
101
102 BCD_TO_BIN(sec);
103 BCD_TO_BIN(min);
104 BCD_TO_BIN(hour);
105 BCD_TO_BIN(day);
106 BCD_TO_BIN(mon);
107 BCD_TO_BIN(year);
108
109 year += 1900;
110 if (year < 1970)
111 year += 100;
112
113 return mktime(year, mon, day, hour, min, sec);
114}
115
116static void
117m41t00_set_tlet(ulong arg)
118{
119 struct rtc_time tm;
120 ulong nowtime = *(ulong *)arg;
121
122 to_tm(nowtime, &tm);
123 tm.tm_year = (tm.tm_year - 1900) % 100;
124
125 BIN_TO_BCD(tm.tm_sec);
126 BIN_TO_BCD(tm.tm_min);
127 BIN_TO_BCD(tm.tm_hour);
128 BIN_TO_BCD(tm.tm_mon);
129 BIN_TO_BCD(tm.tm_mday);
130 BIN_TO_BCD(tm.tm_year);
131
132 down(&m41t00_mutex);
133 if ((i2c_smbus_write_byte_data(save_client, 0, tm.tm_sec & 0x7f) < 0)
134 || (i2c_smbus_write_byte_data(save_client, 1, tm.tm_min & 0x7f)
135 < 0)
136 || (i2c_smbus_write_byte_data(save_client, 2, tm.tm_hour & 0x7f)
137 < 0)
138 || (i2c_smbus_write_byte_data(save_client, 4, tm.tm_mday & 0x7f)
139 < 0)
140 || (i2c_smbus_write_byte_data(save_client, 5, tm.tm_mon & 0x7f)
141 < 0)
142 || (i2c_smbus_write_byte_data(save_client, 6, tm.tm_year & 0x7f)
143 < 0))
144
145 dev_warn(&save_client->dev,"m41t00: can't write to rtc chip\n");
146
147 up(&m41t00_mutex);
148 return;
149}
150
151ulong new_time;
152
153DECLARE_TASKLET_DISABLED(m41t00_tasklet, m41t00_set_tlet, (ulong)&new_time);
154
155int
156m41t00_set_rtc_time(ulong nowtime)
157{
158 new_time = nowtime;
159
160 if (in_interrupt())
161 tasklet_schedule(&m41t00_tasklet);
162 else
163 m41t00_set_tlet((ulong)&new_time);
164
165 return 0;
166}
167
168/*
169 *****************************************************************************
170 *
171 * Driver Interface
172 *
173 *****************************************************************************
174 */
175static int
176m41t00_probe(struct i2c_adapter *adap, int addr, int kind)
177{
178 struct i2c_client *client;
179 int rc;
180
181 client = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
182 if (!client)
183 return -ENOMEM;
184
185 memset(client, 0, sizeof(struct i2c_client));
186 strncpy(client->name, M41T00_DRV_NAME, I2C_NAME_SIZE);
187 client->flags = I2C_DF_NOTIFY;
188 client->addr = addr;
189 client->adapter = adap;
190 client->driver = &m41t00_driver;
191
192 if ((rc = i2c_attach_client(client)) != 0) {
193 kfree(client);
194 return rc;
195 }
196
197 save_client = client;
198 return 0;
199}
200
201static int
202m41t00_attach(struct i2c_adapter *adap)
203{
204 return i2c_probe(adap, &addr_data, m41t00_probe);
205}
206
207static int
208m41t00_detach(struct i2c_client *client)
209{
210 int rc;
211
212 if ((rc = i2c_detach_client(client)) == 0) {
213 kfree(i2c_get_clientdata(client));
214 tasklet_kill(&m41t00_tasklet);
215 }
216 return rc;
217}
218
219static struct i2c_driver m41t00_driver = {
220 .owner = THIS_MODULE,
221 .name = M41T00_DRV_NAME,
222 .id = I2C_DRIVERID_STM41T00,
223 .flags = I2C_DF_NOTIFY,
224 .attach_adapter = m41t00_attach,
225 .detach_client = m41t00_detach,
226};
227
228static int __init
229m41t00_init(void)
230{
231 return i2c_add_driver(&m41t00_driver);
232}
233
234static void __exit
235m41t00_exit(void)
236{
237 i2c_del_driver(&m41t00_driver);
238 return;
239}
240
241module_init(m41t00_init);
242module_exit(m41t00_exit);
243
244MODULE_AUTHOR("Mark A. Greer <mgreer@mvista.com>");
245MODULE_DESCRIPTION("ST Microelectronics M41T00 RTC I2C Client Driver");
246MODULE_LICENSE("GPL");
diff --git a/drivers/i2c/chips/max1619.c b/drivers/i2c/chips/max1619.c
new file mode 100644
index 000000000000..5afa961a5e10
--- /dev/null
+++ b/drivers/i2c/chips/max1619.c
@@ -0,0 +1,373 @@
1/*
2 * max1619.c - Part of lm_sensors, Linux kernel modules for hardware
3 * monitoring
4 * Copyright (C) 2003-2004 Alexey Fisher <fishor@mail.ru>
5 * Jean Delvare <khali@linux-fr.org>
6 *
7 * Based on the lm90 driver. The MAX1619 is a sensor chip made by Maxim.
8 * It reports up to two temperatures (its own plus up to
9 * one external one). Complete datasheet can be
10 * obtained from Maxim's website at:
11 * http://pdfserv.maxim-ic.com/en/ds/MAX1619.pdf
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 */
27
28
29#include <linux/config.h>
30#include <linux/module.h>
31#include <linux/init.h>
32#include <linux/slab.h>
33#include <linux/jiffies.h>
34#include <linux/i2c.h>
35#include <linux/i2c-sensor.h>
36
37
38static unsigned short normal_i2c[] = { 0x18, 0x19, 0x1a,
39 0x29, 0x2a, 0x2b,
40 0x4c, 0x4d, 0x4e,
41 I2C_CLIENT_END };
42static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
43
44/*
45 * Insmod parameters
46 */
47
48SENSORS_INSMOD_1(max1619);
49
50/*
51 * The MAX1619 registers
52 */
53
54#define MAX1619_REG_R_MAN_ID 0xFE
55#define MAX1619_REG_R_CHIP_ID 0xFF
56#define MAX1619_REG_R_CONFIG 0x03
57#define MAX1619_REG_W_CONFIG 0x09
58#define MAX1619_REG_R_CONVRATE 0x04
59#define MAX1619_REG_W_CONVRATE 0x0A
60#define MAX1619_REG_R_STATUS 0x02
61#define MAX1619_REG_R_LOCAL_TEMP 0x00
62#define MAX1619_REG_R_REMOTE_TEMP 0x01
63#define MAX1619_REG_R_REMOTE_HIGH 0x07
64#define MAX1619_REG_W_REMOTE_HIGH 0x0D
65#define MAX1619_REG_R_REMOTE_LOW 0x08
66#define MAX1619_REG_W_REMOTE_LOW 0x0E
67#define MAX1619_REG_R_REMOTE_CRIT 0x10
68#define MAX1619_REG_W_REMOTE_CRIT 0x12
69#define MAX1619_REG_R_TCRIT_HYST 0x11
70#define MAX1619_REG_W_TCRIT_HYST 0x13
71
72/*
73 * Conversions and various macros
74 */
75
76#define TEMP_FROM_REG(val) ((val & 0x80 ? val-0x100 : val) * 1000)
77#define TEMP_TO_REG(val) ((val < 0 ? val+0x100*1000 : val) / 1000)
78
79/*
80 * Functions declaration
81 */
82
83static int max1619_attach_adapter(struct i2c_adapter *adapter);
84static int max1619_detect(struct i2c_adapter *adapter, int address,
85 int kind);
86static void max1619_init_client(struct i2c_client *client);
87static int max1619_detach_client(struct i2c_client *client);
88static struct max1619_data *max1619_update_device(struct device *dev);
89
90/*
91 * Driver data (common to all clients)
92 */
93
94static struct i2c_driver max1619_driver = {
95 .owner = THIS_MODULE,
96 .name = "max1619",
97 .flags = I2C_DF_NOTIFY,
98 .attach_adapter = max1619_attach_adapter,
99 .detach_client = max1619_detach_client,
100};
101
102/*
103 * Client data (each client gets its own)
104 */
105
106struct max1619_data {
107 struct i2c_client client;
108 struct semaphore update_lock;
109 char valid; /* zero until following fields are valid */
110 unsigned long last_updated; /* in jiffies */
111
112 /* registers values */
113 u8 temp_input1; /* local */
114 u8 temp_input2, temp_low2, temp_high2; /* remote */
115 u8 temp_crit2;
116 u8 temp_hyst2;
117 u8 alarms;
118};
119
120/*
121 * Sysfs stuff
122 */
123
124#define show_temp(value) \
125static ssize_t show_##value(struct device *dev, char *buf) \
126{ \
127 struct max1619_data *data = max1619_update_device(dev); \
128 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->value)); \
129}
130show_temp(temp_input1);
131show_temp(temp_input2);
132show_temp(temp_low2);
133show_temp(temp_high2);
134show_temp(temp_crit2);
135show_temp(temp_hyst2);
136
137#define set_temp2(value, reg) \
138static ssize_t set_##value(struct device *dev, const char *buf, \
139 size_t count) \
140{ \
141 struct i2c_client *client = to_i2c_client(dev); \
142 struct max1619_data *data = i2c_get_clientdata(client); \
143 long val = simple_strtol(buf, NULL, 10); \
144 \
145 down(&data->update_lock); \
146 data->value = TEMP_TO_REG(val); \
147 i2c_smbus_write_byte_data(client, reg, data->value); \
148 up(&data->update_lock); \
149 return count; \
150}
151
152set_temp2(temp_low2, MAX1619_REG_W_REMOTE_LOW);
153set_temp2(temp_high2, MAX1619_REG_W_REMOTE_HIGH);
154set_temp2(temp_crit2, MAX1619_REG_W_REMOTE_CRIT);
155set_temp2(temp_hyst2, MAX1619_REG_W_TCRIT_HYST);
156
157static ssize_t show_alarms(struct device *dev, char *buf)
158{
159 struct max1619_data *data = max1619_update_device(dev);
160 return sprintf(buf, "%d\n", data->alarms);
161}
162
163static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input1, NULL);
164static DEVICE_ATTR(temp2_input, S_IRUGO, show_temp_input2, NULL);
165static DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp_low2,
166 set_temp_low2);
167static DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp_high2,
168 set_temp_high2);
169static DEVICE_ATTR(temp2_crit, S_IWUSR | S_IRUGO, show_temp_crit2,
170 set_temp_crit2);
171static DEVICE_ATTR(temp2_crit_hyst, S_IWUSR | S_IRUGO, show_temp_hyst2,
172 set_temp_hyst2);
173static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
174
175/*
176 * Real code
177 */
178
179static int max1619_attach_adapter(struct i2c_adapter *adapter)
180{
181 if (!(adapter->class & I2C_CLASS_HWMON))
182 return 0;
183 return i2c_detect(adapter, &addr_data, max1619_detect);
184}
185
186/*
187 * The following function does more than just detection. If detection
188 * succeeds, it also registers the new chip.
189 */
190static int max1619_detect(struct i2c_adapter *adapter, int address, int kind)
191{
192 struct i2c_client *new_client;
193 struct max1619_data *data;
194 int err = 0;
195 const char *name = "";
196 u8 reg_config=0, reg_convrate=0, reg_status=0;
197 u8 man_id, chip_id;
198 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
199 goto exit;
200
201 if (!(data = kmalloc(sizeof(struct max1619_data), GFP_KERNEL))) {
202 err = -ENOMEM;
203 goto exit;
204 }
205 memset(data, 0, sizeof(struct max1619_data));
206
207 /* The common I2C client data is placed right before the
208 MAX1619-specific data. */
209 new_client = &data->client;
210 i2c_set_clientdata(new_client, data);
211 new_client->addr = address;
212 new_client->adapter = adapter;
213 new_client->driver = &max1619_driver;
214 new_client->flags = 0;
215
216 /*
217 * Now we do the remaining detection. A negative kind means that
218 * the driver was loaded with no force parameter (default), so we
219 * must both detect and identify the chip. A zero kind means that
220 * the driver was loaded with the force parameter, the detection
221 * step shall be skipped. A positive kind means that the driver
222 * was loaded with the force parameter and a given kind of chip is
223 * requested, so both the detection and the identification steps
224 * are skipped.
225 */
226 if (kind < 0) { /* detection */
227 reg_config = i2c_smbus_read_byte_data(new_client,
228 MAX1619_REG_R_CONFIG);
229 reg_convrate = i2c_smbus_read_byte_data(new_client,
230 MAX1619_REG_R_CONVRATE);
231 reg_status = i2c_smbus_read_byte_data(new_client,
232 MAX1619_REG_R_STATUS);
233 if ((reg_config & 0x03) != 0x00
234 || reg_convrate > 0x07 || (reg_status & 0x61 ) !=0x00) {
235 dev_dbg(&adapter->dev,
236 "MAX1619 detection failed at 0x%02x.\n",
237 address);
238 goto exit_free;
239 }
240 }
241
242 if (kind <= 0) { /* identification */
243
244 man_id = i2c_smbus_read_byte_data(new_client,
245 MAX1619_REG_R_MAN_ID);
246 chip_id = i2c_smbus_read_byte_data(new_client,
247 MAX1619_REG_R_CHIP_ID);
248
249 if ((man_id == 0x4D) && (chip_id == 0x04)){
250 kind = max1619;
251 }
252 }
253
254 if (kind <= 0) { /* identification failed */
255 dev_info(&adapter->dev,
256 "Unsupported chip (man_id=0x%02X, "
257 "chip_id=0x%02X).\n", man_id, chip_id);
258 goto exit_free;
259 }
260
261
262 if (kind == max1619){
263 name = "max1619";
264 }
265
266 /* We can fill in the remaining client fields */
267 strlcpy(new_client->name, name, I2C_NAME_SIZE);
268 data->valid = 0;
269 init_MUTEX(&data->update_lock);
270
271 /* Tell the I2C layer a new client has arrived */
272 if ((err = i2c_attach_client(new_client)))
273 goto exit_free;
274
275 /* Initialize the MAX1619 chip */
276 max1619_init_client(new_client);
277
278 /* Register sysfs hooks */
279 device_create_file(&new_client->dev, &dev_attr_temp1_input);
280 device_create_file(&new_client->dev, &dev_attr_temp2_input);
281 device_create_file(&new_client->dev, &dev_attr_temp2_min);
282 device_create_file(&new_client->dev, &dev_attr_temp2_max);
283 device_create_file(&new_client->dev, &dev_attr_temp2_crit);
284 device_create_file(&new_client->dev, &dev_attr_temp2_crit_hyst);
285 device_create_file(&new_client->dev, &dev_attr_alarms);
286
287 return 0;
288
289exit_free:
290 kfree(data);
291exit:
292 return err;
293}
294
295static void max1619_init_client(struct i2c_client *client)
296{
297 u8 config;
298
299 /*
300 * Start the conversions.
301 */
302 i2c_smbus_write_byte_data(client, MAX1619_REG_W_CONVRATE,
303 5); /* 2 Hz */
304 config = i2c_smbus_read_byte_data(client, MAX1619_REG_R_CONFIG);
305 if (config & 0x40)
306 i2c_smbus_write_byte_data(client, MAX1619_REG_W_CONFIG,
307 config & 0xBF); /* run */
308}
309
310static int max1619_detach_client(struct i2c_client *client)
311{
312 int err;
313
314 if ((err = i2c_detach_client(client))) {
315 dev_err(&client->dev, "Client deregistration failed, "
316 "client not detached.\n");
317 return err;
318 }
319
320 kfree(i2c_get_clientdata(client));
321 return 0;
322}
323
324static struct max1619_data *max1619_update_device(struct device *dev)
325{
326 struct i2c_client *client = to_i2c_client(dev);
327 struct max1619_data *data = i2c_get_clientdata(client);
328
329 down(&data->update_lock);
330
331 if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
332 dev_dbg(&client->dev, "Updating max1619 data.\n");
333 data->temp_input1 = i2c_smbus_read_byte_data(client,
334 MAX1619_REG_R_LOCAL_TEMP);
335 data->temp_input2 = i2c_smbus_read_byte_data(client,
336 MAX1619_REG_R_REMOTE_TEMP);
337 data->temp_high2 = i2c_smbus_read_byte_data(client,
338 MAX1619_REG_R_REMOTE_HIGH);
339 data->temp_low2 = i2c_smbus_read_byte_data(client,
340 MAX1619_REG_R_REMOTE_LOW);
341 data->temp_crit2 = i2c_smbus_read_byte_data(client,
342 MAX1619_REG_R_REMOTE_CRIT);
343 data->temp_hyst2 = i2c_smbus_read_byte_data(client,
344 MAX1619_REG_R_TCRIT_HYST);
345 data->alarms = i2c_smbus_read_byte_data(client,
346 MAX1619_REG_R_STATUS);
347
348 data->last_updated = jiffies;
349 data->valid = 1;
350 }
351
352 up(&data->update_lock);
353
354 return data;
355}
356
357static int __init sensors_max1619_init(void)
358{
359 return i2c_add_driver(&max1619_driver);
360}
361
362static void __exit sensors_max1619_exit(void)
363{
364 i2c_del_driver(&max1619_driver);
365}
366
367MODULE_AUTHOR("Alexey Fisher <fishor@mail.ru> and"
368 "Jean Delvare <khali@linux-fr.org>");
369MODULE_DESCRIPTION("MAX1619 sensor driver");
370MODULE_LICENSE("GPL");
371
372module_init(sensors_max1619_init);
373module_exit(sensors_max1619_exit);
diff --git a/drivers/i2c/chips/pc87360.c b/drivers/i2c/chips/pc87360.c
new file mode 100644
index 000000000000..6d94c36c9218
--- /dev/null
+++ b/drivers/i2c/chips/pc87360.c
@@ -0,0 +1,1349 @@
1/*
2 * pc87360.c - Part of lm_sensors, Linux kernel modules
3 * for hardware monitoring
4 * Copyright (C) 2004 Jean Delvare <khali@linux-fr.org>
5 *
6 * Copied from smsc47m1.c:
7 * Copyright (C) 2002 Mark D. Studebaker <mdsxyz123@yahoo.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
23 * Supports the following chips:
24 *
25 * Chip #vin #fan #pwm #temp devid
26 * PC87360 - 2 2 - 0xE1
27 * PC87363 - 2 2 - 0xE8
28 * PC87364 - 3 3 - 0xE4
29 * PC87365 11 3 3 2 0xE5
30 * PC87366 11 3 3 3-4 0xE9
31 *
32 * This driver assumes that no more than one chip is present, and one of
33 * the standard Super-I/O addresses is used (0x2E/0x2F or 0x4E/0x4F).
34 */
35
36#include <linux/config.h>
37#include <linux/module.h>
38#include <linux/init.h>
39#include <linux/slab.h>
40#include <linux/jiffies.h>
41#include <linux/i2c.h>
42#include <linux/i2c-sensor.h>
43#include <linux/i2c-vid.h>
44#include <asm/io.h>
45
46static unsigned short normal_i2c[] = { I2C_CLIENT_END };
47static unsigned int normal_isa[] = { 0, I2C_CLIENT_ISA_END };
48static struct i2c_force_data forces[] = {{ NULL }};
49static u8 devid;
50static unsigned int extra_isa[3];
51static u8 confreg[4];
52
53enum chips { any_chip, pc87360, pc87363, pc87364, pc87365, pc87366 };
54static struct i2c_address_data addr_data = {
55 .normal_i2c = normal_i2c,
56 .normal_isa = normal_isa,
57 .forces = forces,
58};
59
60static int init = 1;
61module_param(init, int, 0);
62MODULE_PARM_DESC(init,
63 "Chip initialization level:\n"
64 " 0: None\n"
65 "*1: Forcibly enable internal voltage and temperature channels, except in9\n"
66 " 2: Forcibly enable all voltage and temperature channels, except in9\n"
67 " 3: Forcibly enable all voltage and temperature channels, including in9");
68
69/*
70 * Super-I/O registers and operations
71 */
72
73#define DEV 0x07 /* Register: Logical device select */
74#define DEVID 0x20 /* Register: Device ID */
75#define ACT 0x30 /* Register: Device activation */
76#define BASE 0x60 /* Register: Base address */
77
78#define FSCM 0x09 /* Logical device: fans */
79#define VLM 0x0d /* Logical device: voltages */
80#define TMS 0x0e /* Logical device: temperatures */
81static const u8 logdev[3] = { FSCM, VLM, TMS };
82
83#define LD_FAN 0
84#define LD_IN 1
85#define LD_TEMP 2
86
87static inline void superio_outb(int sioaddr, int reg, int val)
88{
89 outb(reg, sioaddr);
90 outb(val, sioaddr+1);
91}
92
93static inline int superio_inb(int sioaddr, int reg)
94{
95 outb(reg, sioaddr);
96 return inb(sioaddr+1);
97}
98
99static inline void superio_exit(int sioaddr)
100{
101 outb(0x02, sioaddr);
102 outb(0x02, sioaddr+1);
103}
104
105/*
106 * Logical devices
107 */
108
109#define PC87360_EXTENT 0x10
110#define PC87365_REG_BANK 0x09
111#define NO_BANK 0xff
112
113/*
114 * Fan registers and conversions
115 */
116
117/* nr has to be 0 or 1 (PC87360/87363) or 2 (PC87364/87365/87366) */
118#define PC87360_REG_PRESCALE(nr) (0x00 + 2 * (nr))
119#define PC87360_REG_PWM(nr) (0x01 + 2 * (nr))
120#define PC87360_REG_FAN_MIN(nr) (0x06 + 3 * (nr))
121#define PC87360_REG_FAN(nr) (0x07 + 3 * (nr))
122#define PC87360_REG_FAN_STATUS(nr) (0x08 + 3 * (nr))
123
124#define FAN_FROM_REG(val,div) ((val) == 0 ? 0: \
125 480000 / ((val)*(div)))
126#define FAN_TO_REG(val,div) ((val) <= 100 ? 0 : \
127 480000 / ((val)*(div)))
128#define FAN_DIV_FROM_REG(val) (1 << ((val >> 5) & 0x03))
129#define FAN_STATUS_FROM_REG(val) ((val) & 0x07)
130
131#define FAN_CONFIG_MONITOR(val,nr) (((val) >> (2 + nr * 3)) & 1)
132#define FAN_CONFIG_CONTROL(val,nr) (((val) >> (3 + nr * 3)) & 1)
133#define FAN_CONFIG_INVERT(val,nr) (((val) >> (4 + nr * 3)) & 1)
134
135#define PWM_FROM_REG(val,inv) ((inv) ? 255 - (val) : (val))
136static inline u8 PWM_TO_REG(int val, int inv)
137{
138 if (inv)
139 val = 255 - val;
140 if (val < 0)
141 return 0;
142 if (val > 255)
143 return 255;
144 return val;
145}
146
147/*
148 * Voltage registers and conversions
149 */
150
151#define PC87365_REG_IN_CONVRATE 0x07
152#define PC87365_REG_IN_CONFIG 0x08
153#define PC87365_REG_IN 0x0B
154#define PC87365_REG_IN_MIN 0x0D
155#define PC87365_REG_IN_MAX 0x0C
156#define PC87365_REG_IN_STATUS 0x0A
157#define PC87365_REG_IN_ALARMS1 0x00
158#define PC87365_REG_IN_ALARMS2 0x01
159#define PC87365_REG_VID 0x06
160
161#define IN_FROM_REG(val,ref) (((val) * (ref) + 128) / 256)
162#define IN_TO_REG(val,ref) ((val) < 0 ? 0 : \
163 (val)*256 >= (ref)*255 ? 255: \
164 ((val) * 256 + (ref)/2) / (ref))
165
166/*
167 * Temperature registers and conversions
168 */
169
170#define PC87365_REG_TEMP_CONFIG 0x08
171#define PC87365_REG_TEMP 0x0B
172#define PC87365_REG_TEMP_MIN 0x0D
173#define PC87365_REG_TEMP_MAX 0x0C
174#define PC87365_REG_TEMP_CRIT 0x0E
175#define PC87365_REG_TEMP_STATUS 0x0A
176#define PC87365_REG_TEMP_ALARMS 0x00
177
178#define TEMP_FROM_REG(val) ((val) * 1000)
179#define TEMP_TO_REG(val) ((val) < -55000 ? -55 : \
180 (val) > 127000 ? 127 : \
181 (val) < 0 ? ((val) - 500) / 1000 : \
182 ((val) + 500) / 1000)
183
184/*
185 * Client data (each client gets its own)
186 */
187
188struct pc87360_data {
189 struct i2c_client client;
190 struct semaphore lock;
191 struct semaphore update_lock;
192 char valid; /* !=0 if following fields are valid */
193 unsigned long last_updated; /* In jiffies */
194
195 int address[3];
196
197 u8 fannr, innr, tempnr;
198
199 u8 fan[3]; /* Register value */
200 u8 fan_min[3]; /* Register value */
201 u8 fan_status[3]; /* Register value */
202 u8 pwm[3]; /* Register value */
203 u16 fan_conf; /* Configuration register values, combined */
204
205 u16 in_vref; /* 1 mV/bit */
206 u8 in[14]; /* Register value */
207 u8 in_min[14]; /* Register value */
208 u8 in_max[14]; /* Register value */
209 u8 in_crit[3]; /* Register value */
210 u8 in_status[14]; /* Register value */
211 u16 in_alarms; /* Register values, combined, masked */
212 u8 vid_conf; /* Configuration register value */
213 u8 vrm;
214 u8 vid; /* Register value */
215
216 s8 temp[3]; /* Register value */
217 s8 temp_min[3]; /* Register value */
218 s8 temp_max[3]; /* Register value */
219 s8 temp_crit[3]; /* Register value */
220 u8 temp_status[3]; /* Register value */
221 u8 temp_alarms; /* Register value, masked */
222};
223
224/*
225 * Functions declaration
226 */
227
228static int pc87360_attach_adapter(struct i2c_adapter *adapter);
229static int pc87360_detect(struct i2c_adapter *adapter, int address, int kind);
230static int pc87360_detach_client(struct i2c_client *client);
231
232static int pc87360_read_value(struct pc87360_data *data, u8 ldi, u8 bank,
233 u8 reg);
234static void pc87360_write_value(struct pc87360_data *data, u8 ldi, u8 bank,
235 u8 reg, u8 value);
236static void pc87360_init_client(struct i2c_client *client, int use_thermistors);
237static struct pc87360_data *pc87360_update_device(struct device *dev);
238
239/*
240 * Driver data (common to all clients)
241 */
242
243static struct i2c_driver pc87360_driver = {
244 .owner = THIS_MODULE,
245 .name = "pc87360",
246 .flags = I2C_DF_NOTIFY,
247 .attach_adapter = pc87360_attach_adapter,
248 .detach_client = pc87360_detach_client,
249};
250
251/*
252 * Sysfs stuff
253 */
254
255static ssize_t set_fan_min(struct device *dev, const char *buf,
256 size_t count, int nr)
257{
258 struct i2c_client *client = to_i2c_client(dev);
259 struct pc87360_data *data = i2c_get_clientdata(client);
260 long fan_min = simple_strtol(buf, NULL, 10);
261
262 down(&data->update_lock);
263 fan_min = FAN_TO_REG(fan_min, FAN_DIV_FROM_REG(data->fan_status[nr]));
264
265 /* If it wouldn't fit, change clock divisor */
266 while (fan_min > 255
267 && (data->fan_status[nr] & 0x60) != 0x60) {
268 fan_min >>= 1;
269 data->fan[nr] >>= 1;
270 data->fan_status[nr] += 0x20;
271 }
272 data->fan_min[nr] = fan_min > 255 ? 255 : fan_min;
273 pc87360_write_value(data, LD_FAN, NO_BANK, PC87360_REG_FAN_MIN(nr),
274 data->fan_min[nr]);
275
276 /* Write new divider, preserve alarm bits */
277 pc87360_write_value(data, LD_FAN, NO_BANK, PC87360_REG_FAN_STATUS(nr),
278 data->fan_status[nr] & 0xF9);
279 up(&data->update_lock);
280
281 return count;
282}
283
284#define show_and_set_fan(offset) \
285static ssize_t show_fan##offset##_input(struct device *dev, char *buf) \
286{ \
287 struct pc87360_data *data = pc87360_update_device(dev); \
288 return sprintf(buf, "%u\n", FAN_FROM_REG(data->fan[offset-1], \
289 FAN_DIV_FROM_REG(data->fan_status[offset-1]))); \
290} \
291static ssize_t show_fan##offset##_min(struct device *dev, char *buf) \
292{ \
293 struct pc87360_data *data = pc87360_update_device(dev); \
294 return sprintf(buf, "%u\n", FAN_FROM_REG(data->fan_min[offset-1], \
295 FAN_DIV_FROM_REG(data->fan_status[offset-1]))); \
296} \
297static ssize_t show_fan##offset##_div(struct device *dev, char *buf) \
298{ \
299 struct pc87360_data *data = pc87360_update_device(dev); \
300 return sprintf(buf, "%u\n", \
301 FAN_DIV_FROM_REG(data->fan_status[offset-1])); \
302} \
303static ssize_t show_fan##offset##_status(struct device *dev, char *buf) \
304{ \
305 struct pc87360_data *data = pc87360_update_device(dev); \
306 return sprintf(buf, "%u\n", \
307 FAN_STATUS_FROM_REG(data->fan_status[offset-1])); \
308} \
309static ssize_t set_fan##offset##_min(struct device *dev, const char *buf, \
310 size_t count) \
311{ \
312 return set_fan_min(dev, buf, count, offset-1); \
313} \
314static DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
315 show_fan##offset##_input, NULL); \
316static DEVICE_ATTR(fan##offset##_min, S_IWUSR | S_IRUGO, \
317 show_fan##offset##_min, set_fan##offset##_min); \
318static DEVICE_ATTR(fan##offset##_div, S_IRUGO, \
319 show_fan##offset##_div, NULL); \
320static DEVICE_ATTR(fan##offset##_status, S_IRUGO, \
321 show_fan##offset##_status, NULL);
322show_and_set_fan(1)
323show_and_set_fan(2)
324show_and_set_fan(3)
325
326#define show_and_set_pwm(offset) \
327static ssize_t show_pwm##offset(struct device *dev, char *buf) \
328{ \
329 struct pc87360_data *data = pc87360_update_device(dev); \
330 return sprintf(buf, "%u\n", \
331 PWM_FROM_REG(data->pwm[offset-1], \
332 FAN_CONFIG_INVERT(data->fan_conf, \
333 offset-1))); \
334} \
335static ssize_t set_pwm##offset(struct device *dev, const char *buf, \
336 size_t count) \
337{ \
338 struct i2c_client *client = to_i2c_client(dev); \
339 struct pc87360_data *data = i2c_get_clientdata(client); \
340 long val = simple_strtol(buf, NULL, 10); \
341 \
342 down(&data->update_lock); \
343 data->pwm[offset-1] = PWM_TO_REG(val, \
344 FAN_CONFIG_INVERT(data->fan_conf, offset-1)); \
345 pc87360_write_value(data, LD_FAN, NO_BANK, PC87360_REG_PWM(offset-1), \
346 data->pwm[offset-1]); \
347 up(&data->update_lock); \
348 return count; \
349} \
350static DEVICE_ATTR(pwm##offset, S_IWUSR | S_IRUGO, \
351 show_pwm##offset, set_pwm##offset);
352show_and_set_pwm(1)
353show_and_set_pwm(2)
354show_and_set_pwm(3)
355
356#define show_and_set_in(offset) \
357static ssize_t show_in##offset##_input(struct device *dev, char *buf) \
358{ \
359 struct pc87360_data *data = pc87360_update_device(dev); \
360 return sprintf(buf, "%u\n", IN_FROM_REG(data->in[offset], \
361 data->in_vref)); \
362} \
363static ssize_t show_in##offset##_min(struct device *dev, char *buf) \
364{ \
365 struct pc87360_data *data = pc87360_update_device(dev); \
366 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[offset], \
367 data->in_vref)); \
368} \
369static ssize_t show_in##offset##_max(struct device *dev, char *buf) \
370{ \
371 struct pc87360_data *data = pc87360_update_device(dev); \
372 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[offset], \
373 data->in_vref)); \
374} \
375static ssize_t show_in##offset##_status(struct device *dev, char *buf) \
376{ \
377 struct pc87360_data *data = pc87360_update_device(dev); \
378 return sprintf(buf, "%u\n", data->in_status[offset]); \
379} \
380static ssize_t set_in##offset##_min(struct device *dev, const char *buf, \
381 size_t count) \
382{ \
383 struct i2c_client *client = to_i2c_client(dev); \
384 struct pc87360_data *data = i2c_get_clientdata(client); \
385 long val = simple_strtol(buf, NULL, 10); \
386 \
387 down(&data->update_lock); \
388 data->in_min[offset] = IN_TO_REG(val, data->in_vref); \
389 pc87360_write_value(data, LD_IN, offset, PC87365_REG_IN_MIN, \
390 data->in_min[offset]); \
391 up(&data->update_lock); \
392 return count; \
393} \
394static ssize_t set_in##offset##_max(struct device *dev, const char *buf, \
395 size_t count) \
396{ \
397 struct i2c_client *client = to_i2c_client(dev); \
398 struct pc87360_data *data = i2c_get_clientdata(client); \
399 long val = simple_strtol(buf, NULL, 10); \
400 \
401 down(&data->update_lock); \
402 data->in_max[offset] = IN_TO_REG(val, \
403 data->in_vref); \
404 pc87360_write_value(data, LD_IN, offset, PC87365_REG_IN_MAX, \
405 data->in_max[offset]); \
406 up(&data->update_lock); \
407 return count; \
408} \
409static DEVICE_ATTR(in##offset##_input, S_IRUGO, \
410 show_in##offset##_input, NULL); \
411static DEVICE_ATTR(in##offset##_min, S_IWUSR | S_IRUGO, \
412 show_in##offset##_min, set_in##offset##_min); \
413static DEVICE_ATTR(in##offset##_max, S_IWUSR | S_IRUGO, \
414 show_in##offset##_max, set_in##offset##_max); \
415static DEVICE_ATTR(in##offset##_status, S_IRUGO, \
416 show_in##offset##_status, NULL);
417show_and_set_in(0)
418show_and_set_in(1)
419show_and_set_in(2)
420show_and_set_in(3)
421show_and_set_in(4)
422show_and_set_in(5)
423show_and_set_in(6)
424show_and_set_in(7)
425show_and_set_in(8)
426show_and_set_in(9)
427show_and_set_in(10)
428
429#define show_and_set_therm(offset) \
430static ssize_t show_temp##offset##_input(struct device *dev, char *buf) \
431{ \
432 struct pc87360_data *data = pc87360_update_device(dev); \
433 return sprintf(buf, "%u\n", IN_FROM_REG(data->in[offset+7], \
434 data->in_vref)); \
435} \
436static ssize_t show_temp##offset##_min(struct device *dev, char *buf) \
437{ \
438 struct pc87360_data *data = pc87360_update_device(dev); \
439 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[offset+7], \
440 data->in_vref)); \
441} \
442static ssize_t show_temp##offset##_max(struct device *dev, char *buf) \
443{ \
444 struct pc87360_data *data = pc87360_update_device(dev); \
445 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[offset+7], \
446 data->in_vref)); \
447} \
448static ssize_t show_temp##offset##_crit(struct device *dev, char *buf) \
449{ \
450 struct pc87360_data *data = pc87360_update_device(dev); \
451 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_crit[offset-4], \
452 data->in_vref)); \
453} \
454static ssize_t show_temp##offset##_status(struct device *dev, char *buf) \
455{ \
456 struct pc87360_data *data = pc87360_update_device(dev); \
457 return sprintf(buf, "%u\n", data->in_status[offset+7]); \
458} \
459static ssize_t set_temp##offset##_min(struct device *dev, const char *buf, \
460 size_t count) \
461{ \
462 struct i2c_client *client = to_i2c_client(dev); \
463 struct pc87360_data *data = i2c_get_clientdata(client); \
464 long val = simple_strtol(buf, NULL, 10); \
465 \
466 down(&data->update_lock); \
467 data->in_min[offset+7] = IN_TO_REG(val, data->in_vref); \
468 pc87360_write_value(data, LD_IN, offset+7, PC87365_REG_TEMP_MIN, \
469 data->in_min[offset+7]); \
470 up(&data->update_lock); \
471 return count; \
472} \
473static ssize_t set_temp##offset##_max(struct device *dev, const char *buf, \
474 size_t count) \
475{ \
476 struct i2c_client *client = to_i2c_client(dev); \
477 struct pc87360_data *data = i2c_get_clientdata(client); \
478 long val = simple_strtol(buf, NULL, 10); \
479 \
480 down(&data->update_lock); \
481 data->in_max[offset+7] = IN_TO_REG(val, data->in_vref); \
482 pc87360_write_value(data, LD_IN, offset+7, PC87365_REG_TEMP_MAX, \
483 data->in_max[offset+7]); \
484 up(&data->update_lock); \
485 return count; \
486} \
487static ssize_t set_temp##offset##_crit(struct device *dev, const char *buf, \
488 size_t count) \
489{ \
490 struct i2c_client *client = to_i2c_client(dev); \
491 struct pc87360_data *data = i2c_get_clientdata(client); \
492 long val = simple_strtol(buf, NULL, 10); \
493 \
494 down(&data->update_lock); \
495 data->in_crit[offset-4] = IN_TO_REG(val, data->in_vref); \
496 pc87360_write_value(data, LD_IN, offset+7, PC87365_REG_TEMP_CRIT, \
497 data->in_crit[offset-4]); \
498 up(&data->update_lock); \
499 return count; \
500} \
501static DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
502 show_temp##offset##_input, NULL); \
503static DEVICE_ATTR(temp##offset##_min, S_IWUSR | S_IRUGO, \
504 show_temp##offset##_min, set_temp##offset##_min); \
505static DEVICE_ATTR(temp##offset##_max, S_IWUSR | S_IRUGO, \
506 show_temp##offset##_max, set_temp##offset##_max); \
507static DEVICE_ATTR(temp##offset##_crit, S_IWUSR | S_IRUGO, \
508 show_temp##offset##_crit, set_temp##offset##_crit); \
509static DEVICE_ATTR(temp##offset##_status, S_IRUGO, \
510 show_temp##offset##_status, NULL);
511show_and_set_therm(4)
512show_and_set_therm(5)
513show_and_set_therm(6)
514
515static ssize_t show_vid(struct device *dev, char *buf)
516{
517 struct pc87360_data *data = pc87360_update_device(dev);
518 return sprintf(buf, "%u\n", vid_from_reg(data->vid, data->vrm));
519}
520static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
521
522static ssize_t show_vrm(struct device *dev, char *buf)
523{
524 struct pc87360_data *data = pc87360_update_device(dev);
525 return sprintf(buf, "%u\n", data->vrm);
526}
527static ssize_t set_vrm(struct device *dev, const char *buf, size_t count)
528{
529 struct i2c_client *client = to_i2c_client(dev);
530 struct pc87360_data *data = i2c_get_clientdata(client);
531 data->vrm = simple_strtoul(buf, NULL, 10);
532 return count;
533}
534static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm);
535
536static ssize_t show_in_alarms(struct device *dev, char *buf)
537{
538 struct pc87360_data *data = pc87360_update_device(dev);
539 return sprintf(buf, "%u\n", data->in_alarms);
540}
541static DEVICE_ATTR(alarms_in, S_IRUGO, show_in_alarms, NULL);
542
543#define show_and_set_temp(offset) \
544static ssize_t show_temp##offset##_input(struct device *dev, char *buf) \
545{ \
546 struct pc87360_data *data = pc87360_update_device(dev); \
547 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[offset-1])); \
548} \
549static ssize_t show_temp##offset##_min(struct device *dev, char *buf) \
550{ \
551 struct pc87360_data *data = pc87360_update_device(dev); \
552 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[offset-1])); \
553} \
554static ssize_t show_temp##offset##_max(struct device *dev, char *buf) \
555{ \
556 struct pc87360_data *data = pc87360_update_device(dev); \
557 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[offset-1])); \
558}\
559static ssize_t show_temp##offset##_crit(struct device *dev, char *buf) \
560{ \
561 struct pc87360_data *data = pc87360_update_device(dev); \
562 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit[offset-1])); \
563}\
564static ssize_t show_temp##offset##_status(struct device *dev, char *buf) \
565{ \
566 struct pc87360_data *data = pc87360_update_device(dev); \
567 return sprintf(buf, "%d\n", data->temp_status[offset-1]); \
568}\
569static ssize_t set_temp##offset##_min(struct device *dev, const char *buf, \
570 size_t count) \
571{ \
572 struct i2c_client *client = to_i2c_client(dev); \
573 struct pc87360_data *data = i2c_get_clientdata(client); \
574 long val = simple_strtol(buf, NULL, 10); \
575 \
576 down(&data->update_lock); \
577 data->temp_min[offset-1] = TEMP_TO_REG(val); \
578 pc87360_write_value(data, LD_TEMP, offset-1, PC87365_REG_TEMP_MIN, \
579 data->temp_min[offset-1]); \
580 up(&data->update_lock); \
581 return count; \
582} \
583static ssize_t set_temp##offset##_max(struct device *dev, const char *buf, \
584 size_t count) \
585{ \
586 struct i2c_client *client = to_i2c_client(dev); \
587 struct pc87360_data *data = i2c_get_clientdata(client); \
588 long val = simple_strtol(buf, NULL, 10); \
589 \
590 down(&data->update_lock); \
591 data->temp_max[offset-1] = TEMP_TO_REG(val); \
592 pc87360_write_value(data, LD_TEMP, offset-1, PC87365_REG_TEMP_MAX, \
593 data->temp_max[offset-1]); \
594 up(&data->update_lock); \
595 return count; \
596} \
597static ssize_t set_temp##offset##_crit(struct device *dev, const char *buf, \
598 size_t count) \
599{ \
600 struct i2c_client *client = to_i2c_client(dev); \
601 struct pc87360_data *data = i2c_get_clientdata(client); \
602 long val = simple_strtol(buf, NULL, 10); \
603 \
604 down(&data->update_lock); \
605 data->temp_crit[offset-1] = TEMP_TO_REG(val); \
606 pc87360_write_value(data, LD_TEMP, offset-1, PC87365_REG_TEMP_CRIT, \
607 data->temp_crit[offset-1]); \
608 up(&data->update_lock); \
609 return count; \
610} \
611static DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
612 show_temp##offset##_input, NULL); \
613static DEVICE_ATTR(temp##offset##_min, S_IWUSR | S_IRUGO, \
614 show_temp##offset##_min, set_temp##offset##_min); \
615static DEVICE_ATTR(temp##offset##_max, S_IWUSR | S_IRUGO, \
616 show_temp##offset##_max, set_temp##offset##_max); \
617static DEVICE_ATTR(temp##offset##_crit, S_IWUSR | S_IRUGO, \
618 show_temp##offset##_crit, set_temp##offset##_crit); \
619static DEVICE_ATTR(temp##offset##_status, S_IRUGO, \
620 show_temp##offset##_status, NULL);
621show_and_set_temp(1)
622show_and_set_temp(2)
623show_and_set_temp(3)
624
625static ssize_t show_temp_alarms(struct device *dev, char *buf)
626{
627 struct pc87360_data *data = pc87360_update_device(dev);
628 return sprintf(buf, "%u\n", data->temp_alarms);
629}
630static DEVICE_ATTR(alarms_temp, S_IRUGO, show_temp_alarms, NULL);
631
632/*
633 * Device detection, registration and update
634 */
635
636static int pc87360_attach_adapter(struct i2c_adapter *adapter)
637{
638 return i2c_detect(adapter, &addr_data, pc87360_detect);
639}
640
641static int pc87360_find(int sioaddr, u8 *devid, int *address)
642{
643 u16 val;
644 int i;
645 int nrdev; /* logical device count */
646
647 /* No superio_enter */
648
649 /* Identify device */
650 val = superio_inb(sioaddr, DEVID);
651 switch (val) {
652 case 0xE1: /* PC87360 */
653 case 0xE8: /* PC87363 */
654 case 0xE4: /* PC87364 */
655 nrdev = 1;
656 break;
657 case 0xE5: /* PC87365 */
658 case 0xE9: /* PC87366 */
659 nrdev = 3;
660 break;
661 default:
662 superio_exit(sioaddr);
663 return -ENODEV;
664 }
665 /* Remember the device id */
666 *devid = val;
667
668 for (i = 0; i < nrdev; i++) {
669 /* select logical device */
670 superio_outb(sioaddr, DEV, logdev[i]);
671
672 val = superio_inb(sioaddr, ACT);
673 if (!(val & 0x01)) {
674 printk(KERN_INFO "pc87360: Device 0x%02x not "
675 "activated\n", logdev[i]);
676 continue;
677 }
678
679 val = (superio_inb(sioaddr, BASE) << 8)
680 | superio_inb(sioaddr, BASE + 1);
681 if (!val) {
682 printk(KERN_INFO "pc87360: Base address not set for "
683 "device 0x%02x\n", logdev[i]);
684 continue;
685 }
686
687 address[i] = val;
688
689 if (i==0) { /* Fans */
690 confreg[0] = superio_inb(sioaddr, 0xF0);
691 confreg[1] = superio_inb(sioaddr, 0xF1);
692
693#ifdef DEBUG
694 printk(KERN_DEBUG "pc87360: Fan 1: mon=%d "
695 "ctrl=%d inv=%d\n", (confreg[0]>>2)&1,
696 (confreg[0]>>3)&1, (confreg[0]>>4)&1);
697 printk(KERN_DEBUG "pc87360: Fan 2: mon=%d "
698 "ctrl=%d inv=%d\n", (confreg[0]>>5)&1,
699 (confreg[0]>>6)&1, (confreg[0]>>7)&1);
700 printk(KERN_DEBUG "pc87360: Fan 3: mon=%d "
701 "ctrl=%d inv=%d\n", confreg[1]&1,
702 (confreg[1]>>1)&1, (confreg[1]>>2)&1);
703#endif
704 } else if (i==1) { /* Voltages */
705 /* Are we using thermistors? */
706 if (*devid == 0xE9) { /* PC87366 */
707 /* These registers are not logical-device
708 specific, just that we won't need them if
709 we don't use the VLM device */
710 confreg[2] = superio_inb(sioaddr, 0x2B);
711 confreg[3] = superio_inb(sioaddr, 0x25);
712
713 if (confreg[2] & 0x40) {
714 printk(KERN_INFO "pc87360: Using "
715 "thermistors for temperature "
716 "monitoring\n");
717 }
718 if (confreg[3] & 0xE0) {
719 printk(KERN_INFO "pc87360: VID "
720 "inputs routed (mode %u)\n",
721 confreg[3] >> 5);
722 }
723 }
724 }
725 }
726
727 superio_exit(sioaddr);
728 return 0;
729}
730
731/* We don't really care about the address.
732 Read from extra_isa instead. */
733int pc87360_detect(struct i2c_adapter *adapter, int address, int kind)
734{
735 int i;
736 struct i2c_client *new_client;
737 struct pc87360_data *data;
738 int err = 0;
739 const char *name = "pc87360";
740 int use_thermistors = 0;
741
742 if (!i2c_is_isa_adapter(adapter))
743 return -ENODEV;
744
745 if (!(data = kmalloc(sizeof(struct pc87360_data), GFP_KERNEL)))
746 return -ENOMEM;
747 memset(data, 0x00, sizeof(struct pc87360_data));
748
749 new_client = &data->client;
750 i2c_set_clientdata(new_client, data);
751 new_client->addr = address;
752 init_MUTEX(&data->lock);
753 new_client->adapter = adapter;
754 new_client->driver = &pc87360_driver;
755 new_client->flags = 0;
756
757 data->fannr = 2;
758 data->innr = 0;
759 data->tempnr = 0;
760
761 switch (devid) {
762 case 0xe8:
763 name = "pc87363";
764 break;
765 case 0xe4:
766 name = "pc87364";
767 data->fannr = 3;
768 break;
769 case 0xe5:
770 name = "pc87365";
771 data->fannr = extra_isa[0] ? 3 : 0;
772 data->innr = extra_isa[1] ? 11 : 0;
773 data->tempnr = extra_isa[2] ? 2 : 0;
774 break;
775 case 0xe9:
776 name = "pc87366";
777 data->fannr = extra_isa[0] ? 3 : 0;
778 data->innr = extra_isa[1] ? 14 : 0;
779 data->tempnr = extra_isa[2] ? 3 : 0;
780 break;
781 }
782
783 strcpy(new_client->name, name);
784 data->valid = 0;
785 init_MUTEX(&data->update_lock);
786
787 for (i = 0; i < 3; i++) {
788 if (((data->address[i] = extra_isa[i]))
789 && !request_region(extra_isa[i], PC87360_EXTENT,
790 pc87360_driver.name)) {
791 dev_err(&new_client->dev, "Region 0x%x-0x%x already "
792 "in use!\n", extra_isa[i],
793 extra_isa[i]+PC87360_EXTENT-1);
794 for (i--; i >= 0; i--)
795 release_region(extra_isa[i], PC87360_EXTENT);
796 err = -EBUSY;
797 goto ERROR1;
798 }
799 }
800
801 /* Retrieve the fans configuration from Super-I/O space */
802 if (data->fannr)
803 data->fan_conf = confreg[0] | (confreg[1] << 8);
804
805 if ((err = i2c_attach_client(new_client)))
806 goto ERROR2;
807
808 /* Use the correct reference voltage
809 Unless both the VLM and the TMS logical devices agree to
810 use an external Vref, the internal one is used. */
811 if (data->innr) {
812 i = pc87360_read_value(data, LD_IN, NO_BANK,
813 PC87365_REG_IN_CONFIG);
814 if (data->tempnr) {
815 i &= pc87360_read_value(data, LD_TEMP, NO_BANK,
816 PC87365_REG_TEMP_CONFIG);
817 }
818 data->in_vref = (i&0x02) ? 3025 : 2966;
819 dev_dbg(&new_client->dev, "Using %s reference voltage\n",
820 (i&0x02) ? "external" : "internal");
821
822 data->vid_conf = confreg[3];
823 data->vrm = 90;
824 }
825
826 /* Fan clock dividers may be needed before any data is read */
827 for (i = 0; i < data->fannr; i++) {
828 if (FAN_CONFIG_MONITOR(data->fan_conf, i))
829 data->fan_status[i] = pc87360_read_value(data,
830 LD_FAN, NO_BANK,
831 PC87360_REG_FAN_STATUS(i));
832 }
833
834 if (init > 0) {
835 if (devid == 0xe9 && data->address[1]) /* PC87366 */
836 use_thermistors = confreg[2] & 0x40;
837
838 pc87360_init_client(new_client, use_thermistors);
839 }
840
841 /* Register sysfs hooks */
842 if (data->innr) {
843 device_create_file(&new_client->dev, &dev_attr_in0_input);
844 device_create_file(&new_client->dev, &dev_attr_in1_input);
845 device_create_file(&new_client->dev, &dev_attr_in2_input);
846 device_create_file(&new_client->dev, &dev_attr_in3_input);
847 device_create_file(&new_client->dev, &dev_attr_in4_input);
848 device_create_file(&new_client->dev, &dev_attr_in5_input);
849 device_create_file(&new_client->dev, &dev_attr_in6_input);
850 device_create_file(&new_client->dev, &dev_attr_in7_input);
851 device_create_file(&new_client->dev, &dev_attr_in8_input);
852 device_create_file(&new_client->dev, &dev_attr_in9_input);
853 device_create_file(&new_client->dev, &dev_attr_in10_input);
854 device_create_file(&new_client->dev, &dev_attr_in0_min);
855 device_create_file(&new_client->dev, &dev_attr_in1_min);
856 device_create_file(&new_client->dev, &dev_attr_in2_min);
857 device_create_file(&new_client->dev, &dev_attr_in3_min);
858 device_create_file(&new_client->dev, &dev_attr_in4_min);
859 device_create_file(&new_client->dev, &dev_attr_in5_min);
860 device_create_file(&new_client->dev, &dev_attr_in6_min);
861 device_create_file(&new_client->dev, &dev_attr_in7_min);
862 device_create_file(&new_client->dev, &dev_attr_in8_min);
863 device_create_file(&new_client->dev, &dev_attr_in9_min);
864 device_create_file(&new_client->dev, &dev_attr_in10_min);
865 device_create_file(&new_client->dev, &dev_attr_in0_max);
866 device_create_file(&new_client->dev, &dev_attr_in1_max);
867 device_create_file(&new_client->dev, &dev_attr_in2_max);
868 device_create_file(&new_client->dev, &dev_attr_in3_max);
869 device_create_file(&new_client->dev, &dev_attr_in4_max);
870 device_create_file(&new_client->dev, &dev_attr_in5_max);
871 device_create_file(&new_client->dev, &dev_attr_in6_max);
872 device_create_file(&new_client->dev, &dev_attr_in7_max);
873 device_create_file(&new_client->dev, &dev_attr_in8_max);
874 device_create_file(&new_client->dev, &dev_attr_in9_max);
875 device_create_file(&new_client->dev, &dev_attr_in10_max);
876 device_create_file(&new_client->dev, &dev_attr_in0_status);
877 device_create_file(&new_client->dev, &dev_attr_in1_status);
878 device_create_file(&new_client->dev, &dev_attr_in2_status);
879 device_create_file(&new_client->dev, &dev_attr_in3_status);
880 device_create_file(&new_client->dev, &dev_attr_in4_status);
881 device_create_file(&new_client->dev, &dev_attr_in5_status);
882 device_create_file(&new_client->dev, &dev_attr_in6_status);
883 device_create_file(&new_client->dev, &dev_attr_in7_status);
884 device_create_file(&new_client->dev, &dev_attr_in8_status);
885 device_create_file(&new_client->dev, &dev_attr_in9_status);
886 device_create_file(&new_client->dev, &dev_attr_in10_status);
887
888 device_create_file(&new_client->dev, &dev_attr_cpu0_vid);
889 device_create_file(&new_client->dev, &dev_attr_vrm);
890 device_create_file(&new_client->dev, &dev_attr_alarms_in);
891 }
892
893 if (data->tempnr) {
894 device_create_file(&new_client->dev, &dev_attr_temp1_input);
895 device_create_file(&new_client->dev, &dev_attr_temp2_input);
896 device_create_file(&new_client->dev, &dev_attr_temp1_min);
897 device_create_file(&new_client->dev, &dev_attr_temp2_min);
898 device_create_file(&new_client->dev, &dev_attr_temp1_max);
899 device_create_file(&new_client->dev, &dev_attr_temp2_max);
900 device_create_file(&new_client->dev, &dev_attr_temp1_crit);
901 device_create_file(&new_client->dev, &dev_attr_temp2_crit);
902 device_create_file(&new_client->dev, &dev_attr_temp1_status);
903 device_create_file(&new_client->dev, &dev_attr_temp2_status);
904
905 device_create_file(&new_client->dev, &dev_attr_alarms_temp);
906 }
907 if (data->tempnr == 3) {
908 device_create_file(&new_client->dev, &dev_attr_temp3_input);
909 device_create_file(&new_client->dev, &dev_attr_temp3_min);
910 device_create_file(&new_client->dev, &dev_attr_temp3_max);
911 device_create_file(&new_client->dev, &dev_attr_temp3_crit);
912 device_create_file(&new_client->dev, &dev_attr_temp3_status);
913 }
914 if (data->innr == 14) {
915 device_create_file(&new_client->dev, &dev_attr_temp4_input);
916 device_create_file(&new_client->dev, &dev_attr_temp5_input);
917 device_create_file(&new_client->dev, &dev_attr_temp6_input);
918 device_create_file(&new_client->dev, &dev_attr_temp4_min);
919 device_create_file(&new_client->dev, &dev_attr_temp5_min);
920 device_create_file(&new_client->dev, &dev_attr_temp6_min);
921 device_create_file(&new_client->dev, &dev_attr_temp4_max);
922 device_create_file(&new_client->dev, &dev_attr_temp5_max);
923 device_create_file(&new_client->dev, &dev_attr_temp6_max);
924 device_create_file(&new_client->dev, &dev_attr_temp4_crit);
925 device_create_file(&new_client->dev, &dev_attr_temp5_crit);
926 device_create_file(&new_client->dev, &dev_attr_temp6_crit);
927 device_create_file(&new_client->dev, &dev_attr_temp4_status);
928 device_create_file(&new_client->dev, &dev_attr_temp5_status);
929 device_create_file(&new_client->dev, &dev_attr_temp6_status);
930 }
931
932 if (data->fannr) {
933 if (FAN_CONFIG_MONITOR(data->fan_conf, 0)) {
934 device_create_file(&new_client->dev,
935 &dev_attr_fan1_input);
936 device_create_file(&new_client->dev,
937 &dev_attr_fan1_min);
938 device_create_file(&new_client->dev,
939 &dev_attr_fan1_div);
940 device_create_file(&new_client->dev,
941 &dev_attr_fan1_status);
942 }
943
944 if (FAN_CONFIG_MONITOR(data->fan_conf, 1)) {
945 device_create_file(&new_client->dev,
946 &dev_attr_fan2_input);
947 device_create_file(&new_client->dev,
948 &dev_attr_fan2_min);
949 device_create_file(&new_client->dev,
950 &dev_attr_fan2_div);
951 device_create_file(&new_client->dev,
952 &dev_attr_fan2_status);
953 }
954
955 if (FAN_CONFIG_CONTROL(data->fan_conf, 0))
956 device_create_file(&new_client->dev, &dev_attr_pwm1);
957 if (FAN_CONFIG_CONTROL(data->fan_conf, 1))
958 device_create_file(&new_client->dev, &dev_attr_pwm2);
959 }
960 if (data->fannr == 3) {
961 if (FAN_CONFIG_MONITOR(data->fan_conf, 2)) {
962 device_create_file(&new_client->dev,
963 &dev_attr_fan3_input);
964 device_create_file(&new_client->dev,
965 &dev_attr_fan3_min);
966 device_create_file(&new_client->dev,
967 &dev_attr_fan3_div);
968 device_create_file(&new_client->dev,
969 &dev_attr_fan3_status);
970 }
971
972 if (FAN_CONFIG_CONTROL(data->fan_conf, 2))
973 device_create_file(&new_client->dev, &dev_attr_pwm3);
974 }
975
976 return 0;
977
978ERROR2:
979 for (i = 0; i < 3; i++) {
980 if (data->address[i]) {
981 release_region(data->address[i], PC87360_EXTENT);
982 }
983 }
984ERROR1:
985 kfree(data);
986 return err;
987}
988
989static int pc87360_detach_client(struct i2c_client *client)
990{
991 struct pc87360_data *data = i2c_get_clientdata(client);
992 int i;
993
994 if ((i = i2c_detach_client(client))) {
995 dev_err(&client->dev, "Client deregistration failed, "
996 "client not detached.\n");
997 return i;
998 }
999
1000 for (i = 0; i < 3; i++) {
1001 if (data->address[i]) {
1002 release_region(data->address[i], PC87360_EXTENT);
1003 }
1004 }
1005 kfree(data);
1006
1007 return 0;
1008}
1009
1010/* ldi is the logical device index
1011 bank is for voltages and temperatures only */
1012static int pc87360_read_value(struct pc87360_data *data, u8 ldi, u8 bank,
1013 u8 reg)
1014{
1015 int res;
1016
1017 down(&(data->lock));
1018 if (bank != NO_BANK)
1019 outb_p(bank, data->address[ldi] + PC87365_REG_BANK);
1020 res = inb_p(data->address[ldi] + reg);
1021 up(&(data->lock));
1022
1023 return res;
1024}
1025
1026static void pc87360_write_value(struct pc87360_data *data, u8 ldi, u8 bank,
1027 u8 reg, u8 value)
1028{
1029 down(&(data->lock));
1030 if (bank != NO_BANK)
1031 outb_p(bank, data->address[ldi] + PC87365_REG_BANK);
1032 outb_p(value, data->address[ldi] + reg);
1033 up(&(data->lock));
1034}
1035
1036static void pc87360_init_client(struct i2c_client *client, int use_thermistors)
1037{
1038 struct pc87360_data *data = i2c_get_clientdata(client);
1039 int i, nr;
1040 const u8 init_in[14] = { 2, 2, 2, 2, 2, 2, 2, 1, 1, 3, 1, 2, 2, 2 };
1041 const u8 init_temp[3] = { 2, 2, 1 };
1042 u8 reg;
1043
1044 if (init >= 2 && data->innr) {
1045 reg = pc87360_read_value(data, LD_IN, NO_BANK,
1046 PC87365_REG_IN_CONVRATE);
1047 dev_info(&client->dev, "VLM conversion set to"
1048 "1s period, 160us delay\n");
1049 pc87360_write_value(data, LD_IN, NO_BANK,
1050 PC87365_REG_IN_CONVRATE,
1051 (reg & 0xC0) | 0x11);
1052 }
1053
1054 nr = data->innr < 11 ? data->innr : 11;
1055 for (i=0; i<nr; i++) {
1056 if (init >= init_in[i]) {
1057 /* Forcibly enable voltage channel */
1058 reg = pc87360_read_value(data, LD_IN, i,
1059 PC87365_REG_IN_STATUS);
1060 if (!(reg & 0x01)) {
1061 dev_dbg(&client->dev, "Forcibly "
1062 "enabling in%d\n", i);
1063 pc87360_write_value(data, LD_IN, i,
1064 PC87365_REG_IN_STATUS,
1065 (reg & 0x68) | 0x87);
1066 }
1067 }
1068 }
1069
1070 /* We can't blindly trust the Super-I/O space configuration bit,
1071 most BIOS won't set it properly */
1072 for (i=11; i<data->innr; i++) {
1073 reg = pc87360_read_value(data, LD_IN, i,
1074 PC87365_REG_TEMP_STATUS);
1075 use_thermistors = use_thermistors || (reg & 0x01);
1076 }
1077
1078 i = use_thermistors ? 2 : 0;
1079 for (; i<data->tempnr; i++) {
1080 if (init >= init_temp[i]) {
1081 /* Forcibly enable temperature channel */
1082 reg = pc87360_read_value(data, LD_TEMP, i,
1083 PC87365_REG_TEMP_STATUS);
1084 if (!(reg & 0x01)) {
1085 dev_dbg(&client->dev, "Forcibly "
1086 "enabling temp%d\n", i+1);
1087 pc87360_write_value(data, LD_TEMP, i,
1088 PC87365_REG_TEMP_STATUS,
1089 0xCF);
1090 }
1091 }
1092 }
1093
1094 if (use_thermistors) {
1095 for (i=11; i<data->innr; i++) {
1096 if (init >= init_in[i]) {
1097 /* The pin may already be used by thermal
1098 diodes */
1099 reg = pc87360_read_value(data, LD_TEMP,
1100 (i-11)/2, PC87365_REG_TEMP_STATUS);
1101 if (reg & 0x01) {
1102 dev_dbg(&client->dev, "Skipping "
1103 "temp%d, pin already in use "
1104 "by temp%d\n", i-7, (i-11)/2);
1105 continue;
1106 }
1107
1108 /* Forcibly enable thermistor channel */
1109 reg = pc87360_read_value(data, LD_IN, i,
1110 PC87365_REG_IN_STATUS);
1111 if (!(reg & 0x01)) {
1112 dev_dbg(&client->dev, "Forcibly "
1113 "enabling temp%d\n", i-7);
1114 pc87360_write_value(data, LD_IN, i,
1115 PC87365_REG_TEMP_STATUS,
1116 (reg & 0x60) | 0x8F);
1117 }
1118 }
1119 }
1120 }
1121
1122 if (data->innr) {
1123 reg = pc87360_read_value(data, LD_IN, NO_BANK,
1124 PC87365_REG_IN_CONFIG);
1125 if (reg & 0x01) {
1126 dev_dbg(&client->dev, "Forcibly "
1127 "enabling monitoring (VLM)\n");
1128 pc87360_write_value(data, LD_IN, NO_BANK,
1129 PC87365_REG_IN_CONFIG,
1130 reg & 0xFE);
1131 }
1132 }
1133
1134 if (data->tempnr) {
1135 reg = pc87360_read_value(data, LD_TEMP, NO_BANK,
1136 PC87365_REG_TEMP_CONFIG);
1137 if (reg & 0x01) {
1138 dev_dbg(&client->dev, "Forcibly enabling "
1139 "monitoring (TMS)\n");
1140 pc87360_write_value(data, LD_TEMP, NO_BANK,
1141 PC87365_REG_TEMP_CONFIG,
1142 reg & 0xFE);
1143 }
1144
1145 if (init >= 2) {
1146 /* Chip config as documented by National Semi. */
1147 pc87360_write_value(data, LD_TEMP, 0xF, 0xA, 0x08);
1148 /* We voluntarily omit the bank here, in case the
1149 sequence itself matters. It shouldn't be a problem,
1150 since nobody else is supposed to access the
1151 device at that point. */
1152 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xB, 0x04);
1153 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xC, 0x35);
1154 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xD, 0x05);
1155 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xE, 0x05);
1156 }
1157 }
1158}
1159
1160static void pc87360_autodiv(struct i2c_client *client, int nr)
1161{
1162 struct pc87360_data *data = i2c_get_clientdata(client);
1163 u8 old_min = data->fan_min[nr];
1164
1165 /* Increase clock divider if needed and possible */
1166 if ((data->fan_status[nr] & 0x04) /* overflow flag */
1167 || (data->fan[nr] >= 224)) { /* next to overflow */
1168 if ((data->fan_status[nr] & 0x60) != 0x60) {
1169 data->fan_status[nr] += 0x20;
1170 data->fan_min[nr] >>= 1;
1171 data->fan[nr] >>= 1;
1172 dev_dbg(&client->dev, "Increasing "
1173 "clock divider to %d for fan %d\n",
1174 FAN_DIV_FROM_REG(data->fan_status[nr]), nr+1);
1175 }
1176 } else {
1177 /* Decrease clock divider if possible */
1178 while (!(data->fan_min[nr] & 0x80) /* min "nails" divider */
1179 && data->fan[nr] < 85 /* bad accuracy */
1180 && (data->fan_status[nr] & 0x60) != 0x00) {
1181 data->fan_status[nr] -= 0x20;
1182 data->fan_min[nr] <<= 1;
1183 data->fan[nr] <<= 1;
1184 dev_dbg(&client->dev, "Decreasing "
1185 "clock divider to %d for fan %d\n",
1186 FAN_DIV_FROM_REG(data->fan_status[nr]),
1187 nr+1);
1188 }
1189 }
1190
1191 /* Write new fan min if it changed */
1192 if (old_min != data->fan_min[nr]) {
1193 pc87360_write_value(data, LD_FAN, NO_BANK,
1194 PC87360_REG_FAN_MIN(nr),
1195 data->fan_min[nr]);
1196 }
1197}
1198
1199static struct pc87360_data *pc87360_update_device(struct device *dev)
1200{
1201 struct i2c_client *client = to_i2c_client(dev);
1202 struct pc87360_data *data = i2c_get_clientdata(client);
1203 u8 i;
1204
1205 down(&data->update_lock);
1206
1207 if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
1208 dev_dbg(&client->dev, "Data update\n");
1209
1210 /* Fans */
1211 for (i = 0; i < data->fannr; i++) {
1212 if (FAN_CONFIG_MONITOR(data->fan_conf, i)) {
1213 data->fan_status[i] =
1214 pc87360_read_value(data, LD_FAN,
1215 NO_BANK, PC87360_REG_FAN_STATUS(i));
1216 data->fan[i] = pc87360_read_value(data, LD_FAN,
1217 NO_BANK, PC87360_REG_FAN(i));
1218 data->fan_min[i] = pc87360_read_value(data,
1219 LD_FAN, NO_BANK,
1220 PC87360_REG_FAN_MIN(i));
1221 /* Change clock divider if needed */
1222 pc87360_autodiv(client, i);
1223 /* Clear bits and write new divider */
1224 pc87360_write_value(data, LD_FAN, NO_BANK,
1225 PC87360_REG_FAN_STATUS(i),
1226 data->fan_status[i]);
1227 }
1228 if (FAN_CONFIG_CONTROL(data->fan_conf, i))
1229 data->pwm[i] = pc87360_read_value(data, LD_FAN,
1230 NO_BANK, PC87360_REG_PWM(i));
1231 }
1232
1233 /* Voltages */
1234 for (i = 0; i < data->innr; i++) {
1235 data->in_status[i] = pc87360_read_value(data, LD_IN, i,
1236 PC87365_REG_IN_STATUS);
1237 /* Clear bits */
1238 pc87360_write_value(data, LD_IN, i,
1239 PC87365_REG_IN_STATUS,
1240 data->in_status[i]);
1241 if ((data->in_status[i] & 0x81) == 0x81) {
1242 data->in[i] = pc87360_read_value(data, LD_IN,
1243 i, PC87365_REG_IN);
1244 }
1245 if (data->in_status[i] & 0x01) {
1246 data->in_min[i] = pc87360_read_value(data,
1247 LD_IN, i,
1248 PC87365_REG_IN_MIN);
1249 data->in_max[i] = pc87360_read_value(data,
1250 LD_IN, i,
1251 PC87365_REG_IN_MAX);
1252 if (i >= 11)
1253 data->in_crit[i-11] =
1254 pc87360_read_value(data, LD_IN,
1255 i, PC87365_REG_TEMP_CRIT);
1256 }
1257 }
1258 if (data->innr) {
1259 data->in_alarms = pc87360_read_value(data, LD_IN,
1260 NO_BANK, PC87365_REG_IN_ALARMS1)
1261 | ((pc87360_read_value(data, LD_IN,
1262 NO_BANK, PC87365_REG_IN_ALARMS2)
1263 & 0x07) << 8);
1264 data->vid = (data->vid_conf & 0xE0) ?
1265 pc87360_read_value(data, LD_IN,
1266 NO_BANK, PC87365_REG_VID) : 0x1F;
1267 }
1268
1269 /* Temperatures */
1270 for (i = 0; i < data->tempnr; i++) {
1271 data->temp_status[i] = pc87360_read_value(data,
1272 LD_TEMP, i,
1273 PC87365_REG_TEMP_STATUS);
1274 /* Clear bits */
1275 pc87360_write_value(data, LD_TEMP, i,
1276 PC87365_REG_TEMP_STATUS,
1277 data->temp_status[i]);
1278 if ((data->temp_status[i] & 0x81) == 0x81) {
1279 data->temp[i] = pc87360_read_value(data,
1280 LD_TEMP, i,
1281 PC87365_REG_TEMP);
1282 }
1283 if (data->temp_status[i] & 0x01) {
1284 data->temp_min[i] = pc87360_read_value(data,
1285 LD_TEMP, i,
1286 PC87365_REG_TEMP_MIN);
1287 data->temp_max[i] = pc87360_read_value(data,
1288 LD_TEMP, i,
1289 PC87365_REG_TEMP_MAX);
1290 data->temp_crit[i] = pc87360_read_value(data,
1291 LD_TEMP, i,
1292 PC87365_REG_TEMP_CRIT);
1293 }
1294 }
1295 if (data->tempnr) {
1296 data->temp_alarms = pc87360_read_value(data, LD_TEMP,
1297 NO_BANK, PC87365_REG_TEMP_ALARMS)
1298 & 0x3F;
1299 }
1300
1301 data->last_updated = jiffies;
1302 data->valid = 1;
1303 }
1304
1305 up(&data->update_lock);
1306
1307 return data;
1308}
1309
1310static int __init pc87360_init(void)
1311{
1312 int i;
1313
1314 if (pc87360_find(0x2e, &devid, extra_isa)
1315 && pc87360_find(0x4e, &devid, extra_isa)) {
1316 printk(KERN_WARNING "pc87360: PC8736x not detected, "
1317 "module not inserted.\n");
1318 return -ENODEV;
1319 }
1320
1321 /* Arbitrarily pick one of the addresses */
1322 for (i = 0; i < 3; i++) {
1323 if (extra_isa[i] != 0x0000) {
1324 normal_isa[0] = extra_isa[i];
1325 break;
1326 }
1327 }
1328
1329 if (normal_isa[0] == 0x0000) {
1330 printk(KERN_WARNING "pc87360: No active logical device, "
1331 "module not inserted.\n");
1332 return -ENODEV;
1333 }
1334
1335 return i2c_add_driver(&pc87360_driver);
1336}
1337
1338static void __exit pc87360_exit(void)
1339{
1340 i2c_del_driver(&pc87360_driver);
1341}
1342
1343
1344MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
1345MODULE_DESCRIPTION("PC8736x hardware monitor");
1346MODULE_LICENSE("GPL");
1347
1348module_init(pc87360_init);
1349module_exit(pc87360_exit);
diff --git a/drivers/i2c/chips/pcf8574.c b/drivers/i2c/chips/pcf8574.c
new file mode 100644
index 000000000000..48b4e22eaffe
--- /dev/null
+++ b/drivers/i2c/chips/pcf8574.c
@@ -0,0 +1,229 @@
1/*
2 pcf8574.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring
4 Copyright (c) 2000 Frodo Looijaard <frodol@dds.nl>,
5 Philip Edelbrock <phil@netroedge.com>,
6 Dan Eaton <dan.eaton@rocketlogix.com>
7 Ported to Linux 2.6 by Aurelien Jarno <aurel32@debian.org> with
8 the help of Jean Delvare <khali@linux-fr.org>
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23*/
24
25/* A few notes about the PCF8574:
26
27* The PCF8574 is an 8-bit I/O expander for the I2C bus produced by
28 Philips Semiconductors. It is designed to provide a byte I2C
29 interface to up to 8 separate devices.
30
31* The PCF8574 appears as a very simple SMBus device which can be
32 read from or written to with SMBUS byte read/write accesses.
33
34 --Dan
35
36*/
37
38#include <linux/module.h>
39#include <linux/init.h>
40#include <linux/slab.h>
41#include <linux/i2c.h>
42#include <linux/i2c-sensor.h>
43
44/* Addresses to scan */
45static unsigned short normal_i2c[] = { 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
46 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
47 I2C_CLIENT_END };
48static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
49
50/* Insmod parameters */
51SENSORS_INSMOD_2(pcf8574, pcf8574a);
52
53/* Initial values */
54#define PCF8574_INIT 255 /* All outputs on (input mode) */
55
56/* Each client has this additional data */
57struct pcf8574_data {
58 struct i2c_client client;
59
60 u8 read, write; /* Register values */
61};
62
63static int pcf8574_attach_adapter(struct i2c_adapter *adapter);
64static int pcf8574_detect(struct i2c_adapter *adapter, int address, int kind);
65static int pcf8574_detach_client(struct i2c_client *client);
66static void pcf8574_init_client(struct i2c_client *client);
67
68/* This is the driver that will be inserted */
69static struct i2c_driver pcf8574_driver = {
70 .owner = THIS_MODULE,
71 .name = "pcf8574",
72 .id = I2C_DRIVERID_PCF8574,
73 .flags = I2C_DF_NOTIFY,
74 .attach_adapter = pcf8574_attach_adapter,
75 .detach_client = pcf8574_detach_client,
76};
77
78/* following are the sysfs callback functions */
79static ssize_t show_read(struct device *dev, char *buf)
80{
81 struct i2c_client *client = to_i2c_client(dev);
82 struct pcf8574_data *data = i2c_get_clientdata(client);
83 data->read = i2c_smbus_read_byte(client);
84 return sprintf(buf, "%u\n", data->read);
85}
86
87static DEVICE_ATTR(read, S_IRUGO, show_read, NULL);
88
89static ssize_t show_write(struct device *dev, char *buf)
90{
91 struct pcf8574_data *data = i2c_get_clientdata(to_i2c_client(dev));
92 return sprintf(buf, "%u\n", data->write);
93}
94
95static ssize_t set_write(struct device *dev, const char *buf,
96 size_t count)
97{
98 struct i2c_client *client = to_i2c_client(dev);
99 struct pcf8574_data *data = i2c_get_clientdata(client);
100 unsigned long val = simple_strtoul(buf, NULL, 10);
101
102 if (val > 0xff)
103 return -EINVAL;
104
105 data->write = val;
106 i2c_smbus_write_byte(client, data->write);
107 return count;
108}
109
110static DEVICE_ATTR(write, S_IWUSR | S_IRUGO, show_write, set_write);
111
112/*
113 * Real code
114 */
115
116static int pcf8574_attach_adapter(struct i2c_adapter *adapter)
117{
118 return i2c_detect(adapter, &addr_data, pcf8574_detect);
119}
120
121/* This function is called by i2c_detect */
122int pcf8574_detect(struct i2c_adapter *adapter, int address, int kind)
123{
124 struct i2c_client *new_client;
125 struct pcf8574_data *data;
126 int err = 0;
127 const char *client_name = "";
128
129 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
130 goto exit;
131
132 /* OK. For now, we presume we have a valid client. We now create the
133 client structure, even though we cannot fill it completely yet. */
134 if (!(data = kmalloc(sizeof(struct pcf8574_data), GFP_KERNEL))) {
135 err = -ENOMEM;
136 goto exit;
137 }
138 memset(data, 0, sizeof(struct pcf8574_data));
139
140 new_client = &data->client;
141 i2c_set_clientdata(new_client, data);
142 new_client->addr = address;
143 new_client->adapter = adapter;
144 new_client->driver = &pcf8574_driver;
145 new_client->flags = 0;
146
147 /* Now, we would do the remaining detection. But the PCF8574 is plainly
148 impossible to detect! Stupid chip. */
149
150 /* Determine the chip type */
151 if (kind <= 0) {
152 if (address >= 0x38 && address <= 0x3f)
153 kind = pcf8574a;
154 else
155 kind = pcf8574;
156 }
157
158 if (kind == pcf8574a)
159 client_name = "pcf8574a";
160 else
161 client_name = "pcf8574";
162
163 /* Fill in the remaining client fields and put it into the global list */
164 strlcpy(new_client->name, client_name, I2C_NAME_SIZE);
165
166 /* Tell the I2C layer a new client has arrived */
167 if ((err = i2c_attach_client(new_client)))
168 goto exit_free;
169
170 /* Initialize the PCF8574 chip */
171 pcf8574_init_client(new_client);
172
173 /* Register sysfs hooks */
174 device_create_file(&new_client->dev, &dev_attr_read);
175 device_create_file(&new_client->dev, &dev_attr_write);
176 return 0;
177
178/* OK, this is not exactly good programming practice, usually. But it is
179 very code-efficient in this case. */
180
181 exit_free:
182 kfree(data);
183 exit:
184 return err;
185}
186
187static int pcf8574_detach_client(struct i2c_client *client)
188{
189 int err;
190
191 if ((err = i2c_detach_client(client))) {
192 dev_err(&client->dev,
193 "Client deregistration failed, client not detached.\n");
194 return err;
195 }
196
197 kfree(i2c_get_clientdata(client));
198 return 0;
199}
200
201/* Called when we have found a new PCF8574. */
202static void pcf8574_init_client(struct i2c_client *client)
203{
204 struct pcf8574_data *data = i2c_get_clientdata(client);
205 data->write = PCF8574_INIT;
206 i2c_smbus_write_byte(client, data->write);
207}
208
209static int __init pcf8574_init(void)
210{
211 return i2c_add_driver(&pcf8574_driver);
212}
213
214static void __exit pcf8574_exit(void)
215{
216 i2c_del_driver(&pcf8574_driver);
217}
218
219
220MODULE_AUTHOR
221 ("Frodo Looijaard <frodol@dds.nl>, "
222 "Philip Edelbrock <phil@netroedge.com>, "
223 "Dan Eaton <dan.eaton@rocketlogix.com> "
224 "and Aurelien Jarno <aurelien@aurel32.net>");
225MODULE_DESCRIPTION("PCF8574 driver");
226MODULE_LICENSE("GPL");
227
228module_init(pcf8574_init);
229module_exit(pcf8574_exit);
diff --git a/drivers/i2c/chips/pcf8591.c b/drivers/i2c/chips/pcf8591.c
new file mode 100644
index 000000000000..b6b927d8b372
--- /dev/null
+++ b/drivers/i2c/chips/pcf8591.c
@@ -0,0 +1,316 @@
1/*
2 pcf8591.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring
4 Copyright (C) 2001-2004 Aurelien Jarno <aurelien@aurel32.net>
5 Ported to Linux 2.6 by Aurelien Jarno <aurelien@aurel32.net> with
6 the help of 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; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21*/
22
23#include <linux/module.h>
24#include <linux/init.h>
25#include <linux/slab.h>
26#include <linux/i2c.h>
27#include <linux/i2c-sensor.h>
28
29/* Addresses to scan */
30static unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b, 0x4c,
31 0x4d, 0x4e, 0x4f, I2C_CLIENT_END };
32static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
33
34/* Insmod parameters */
35SENSORS_INSMOD_1(pcf8591);
36
37static int input_mode;
38module_param(input_mode, int, 0);
39MODULE_PARM_DESC(input_mode,
40 "Analog input mode:\n"
41 " 0 = four single ended inputs\n"
42 " 1 = three differential inputs\n"
43 " 2 = single ended and differential mixed\n"
44 " 3 = two differential inputs\n");
45
46/* The PCF8591 control byte
47 7 6 5 4 3 2 1 0
48 | 0 |AOEF| AIP | 0 |AINC| AICH | */
49
50/* Analog Output Enable Flag (analog output active if 1) */
51#define PCF8591_CONTROL_AOEF 0x40
52
53/* Analog Input Programming
54 0x00 = four single ended inputs
55 0x10 = three differential inputs
56 0x20 = single ended and differential mixed
57 0x30 = two differential inputs */
58#define PCF8591_CONTROL_AIP_MASK 0x30
59
60/* Autoincrement Flag (switch on if 1) */
61#define PCF8591_CONTROL_AINC 0x04
62
63/* Channel selection
64 0x00 = channel 0
65 0x01 = channel 1
66 0x02 = channel 2
67 0x03 = channel 3 */
68#define PCF8591_CONTROL_AICH_MASK 0x03
69
70/* Initial values */
71#define PCF8591_INIT_CONTROL ((input_mode << 4) | PCF8591_CONTROL_AOEF)
72#define PCF8591_INIT_AOUT 0 /* DAC out = 0 */
73
74/* Conversions */
75#define REG_TO_SIGNED(reg) (((reg) & 0x80)?((reg) - 256):(reg))
76
77struct pcf8591_data {
78 struct i2c_client client;
79 struct semaphore update_lock;
80
81 u8 control;
82 u8 aout;
83};
84
85static int pcf8591_attach_adapter(struct i2c_adapter *adapter);
86static int pcf8591_detect(struct i2c_adapter *adapter, int address, int kind);
87static int pcf8591_detach_client(struct i2c_client *client);
88static void pcf8591_init_client(struct i2c_client *client);
89static int pcf8591_read_channel(struct device *dev, int channel);
90
91/* This is the driver that will be inserted */
92static struct i2c_driver pcf8591_driver = {
93 .owner = THIS_MODULE,
94 .name = "pcf8591",
95 .id = I2C_DRIVERID_PCF8591,
96 .flags = I2C_DF_NOTIFY,
97 .attach_adapter = pcf8591_attach_adapter,
98 .detach_client = pcf8591_detach_client,
99};
100
101/* following are the sysfs callback functions */
102#define show_in_channel(channel) \
103static ssize_t show_in##channel##_input(struct device *dev, char *buf) \
104{ \
105 return sprintf(buf, "%d\n", pcf8591_read_channel(dev, channel));\
106} \
107static DEVICE_ATTR(in##channel##_input, S_IRUGO, \
108 show_in##channel##_input, NULL);
109
110show_in_channel(0);
111show_in_channel(1);
112show_in_channel(2);
113show_in_channel(3);
114
115static ssize_t show_out0_ouput(struct device *dev, char *buf)
116{
117 struct pcf8591_data *data = i2c_get_clientdata(to_i2c_client(dev));
118 return sprintf(buf, "%d\n", data->aout * 10);
119}
120
121static ssize_t set_out0_output(struct device *dev, const char *buf, size_t count)
122{
123 unsigned int value;
124 struct i2c_client *client = to_i2c_client(dev);
125 struct pcf8591_data *data = i2c_get_clientdata(client);
126 if ((value = (simple_strtoul(buf, NULL, 10) + 5) / 10) <= 255) {
127 data->aout = value;
128 i2c_smbus_write_byte_data(client, data->control, data->aout);
129 return count;
130 }
131 return -EINVAL;
132}
133
134static DEVICE_ATTR(out0_output, S_IWUSR | S_IRUGO,
135 show_out0_ouput, set_out0_output);
136
137static ssize_t show_out0_enable(struct device *dev, char *buf)
138{
139 struct pcf8591_data *data = i2c_get_clientdata(to_i2c_client(dev));
140 return sprintf(buf, "%u\n", !(!(data->control & PCF8591_CONTROL_AOEF)));
141}
142
143static ssize_t set_out0_enable(struct device *dev, const char *buf, size_t count)
144{
145 struct i2c_client *client = to_i2c_client(dev);
146 struct pcf8591_data *data = i2c_get_clientdata(client);
147 unsigned long val = simple_strtoul(buf, NULL, 10);
148
149 down(&data->update_lock);
150 if (val)
151 data->control |= PCF8591_CONTROL_AOEF;
152 else
153 data->control &= ~PCF8591_CONTROL_AOEF;
154 i2c_smbus_write_byte(client, data->control);
155 up(&data->update_lock);
156 return count;
157}
158
159static DEVICE_ATTR(out0_enable, S_IWUSR | S_IRUGO,
160 show_out0_enable, set_out0_enable);
161
162/*
163 * Real code
164 */
165static int pcf8591_attach_adapter(struct i2c_adapter *adapter)
166{
167 return i2c_detect(adapter, &addr_data, pcf8591_detect);
168}
169
170/* This function is called by i2c_detect */
171int pcf8591_detect(struct i2c_adapter *adapter, int address, int kind)
172{
173 struct i2c_client *new_client;
174 struct pcf8591_data *data;
175 int err = 0;
176
177 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE
178 | I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
179 goto exit;
180
181 /* OK. For now, we presume we have a valid client. We now create the
182 client structure, even though we cannot fill it completely yet. */
183 if (!(data = kmalloc(sizeof(struct pcf8591_data), GFP_KERNEL))) {
184 err = -ENOMEM;
185 goto exit;
186 }
187 memset(data, 0, sizeof(struct pcf8591_data));
188
189 new_client = &data->client;
190 i2c_set_clientdata(new_client, data);
191 new_client->addr = address;
192 new_client->adapter = adapter;
193 new_client->driver = &pcf8591_driver;
194 new_client->flags = 0;
195
196 /* Now, we would do the remaining detection. But the PCF8591 is plainly
197 impossible to detect! Stupid chip. */
198
199 /* Determine the chip type - only one kind supported! */
200 if (kind <= 0)
201 kind = pcf8591;
202
203 /* Fill in the remaining client fields and put it into the global
204 list */
205 strlcpy(new_client->name, "pcf8591", I2C_NAME_SIZE);
206 init_MUTEX(&data->update_lock);
207
208 /* Tell the I2C layer a new client has arrived */
209 if ((err = i2c_attach_client(new_client)))
210 goto exit_kfree;
211
212 /* Initialize the PCF8591 chip */
213 pcf8591_init_client(new_client);
214
215 /* Register sysfs hooks */
216 device_create_file(&new_client->dev, &dev_attr_out0_enable);
217 device_create_file(&new_client->dev, &dev_attr_out0_output);
218 device_create_file(&new_client->dev, &dev_attr_in0_input);
219 device_create_file(&new_client->dev, &dev_attr_in1_input);
220
221 /* Register input2 if not in "two differential inputs" mode */
222 if (input_mode != 3 )
223 device_create_file(&new_client->dev, &dev_attr_in2_input);
224
225 /* Register input3 only in "four single ended inputs" mode */
226 if (input_mode == 0)
227 device_create_file(&new_client->dev, &dev_attr_in3_input);
228
229 return 0;
230
231 /* OK, this is not exactly good programming practice, usually. But it is
232 very code-efficient in this case. */
233
234exit_kfree:
235 kfree(data);
236exit:
237 return err;
238}
239
240static int pcf8591_detach_client(struct i2c_client *client)
241{
242 int err;
243
244 if ((err = i2c_detach_client(client))) {
245 dev_err(&client->dev,
246 "Client deregistration failed, client not detached.\n");
247 return err;
248 }
249
250 kfree(i2c_get_clientdata(client));
251 return 0;
252}
253
254/* Called when we have found a new PCF8591. */
255static void pcf8591_init_client(struct i2c_client *client)
256{
257 struct pcf8591_data *data = i2c_get_clientdata(client);
258 data->control = PCF8591_INIT_CONTROL;
259 data->aout = PCF8591_INIT_AOUT;
260
261 i2c_smbus_write_byte_data(client, data->control, data->aout);
262
263 /* The first byte transmitted contains the conversion code of the
264 previous read cycle. FLUSH IT! */
265 i2c_smbus_read_byte(client);
266}
267
268static int pcf8591_read_channel(struct device *dev, int channel)
269{
270 u8 value;
271 struct i2c_client *client = to_i2c_client(dev);
272 struct pcf8591_data *data = i2c_get_clientdata(client);
273
274 down(&data->update_lock);
275
276 if ((data->control & PCF8591_CONTROL_AICH_MASK) != channel) {
277 data->control = (data->control & ~PCF8591_CONTROL_AICH_MASK)
278 | channel;
279 i2c_smbus_write_byte(client, data->control);
280
281 /* The first byte transmitted contains the conversion code of
282 the previous read cycle. FLUSH IT! */
283 i2c_smbus_read_byte(client);
284 }
285 value = i2c_smbus_read_byte(client);
286
287 up(&data->update_lock);
288
289 if ((channel == 2 && input_mode == 2) ||
290 (channel != 3 && (input_mode == 1 || input_mode == 3)))
291 return (10 * REG_TO_SIGNED(value));
292 else
293 return (10 * value);
294}
295
296static int __init pcf8591_init(void)
297{
298 if (input_mode < 0 || input_mode > 3) {
299 printk(KERN_WARNING "pcf8591: invalid input_mode (%d)\n",
300 input_mode);
301 input_mode = 0;
302 }
303 return i2c_add_driver(&pcf8591_driver);
304}
305
306static void __exit pcf8591_exit(void)
307{
308 i2c_del_driver(&pcf8591_driver);
309}
310
311MODULE_AUTHOR("Aurelien Jarno <aurelien@aurel32.net>");
312MODULE_DESCRIPTION("PCF8591 driver");
313MODULE_LICENSE("GPL");
314
315module_init(pcf8591_init);
316module_exit(pcf8591_exit);
diff --git a/drivers/i2c/chips/rtc8564.c b/drivers/i2c/chips/rtc8564.c
new file mode 100644
index 000000000000..5a9deddb626b
--- /dev/null
+++ b/drivers/i2c/chips/rtc8564.c
@@ -0,0 +1,394 @@
1/*
2 * linux/drivers/i2c/chips/rtc8564.c
3 *
4 * Copyright (C) 2002-2004 Stefan Eletzhofer
5 *
6 * based on linux/drivers/acron/char/pcf8583.c
7 * Copyright (C) 2000 Russell King
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 *
13 * Driver for system3's EPSON RTC 8564 chip
14 */
15#include <linux/module.h>
16#include <linux/kernel.h>
17#include <linux/i2c.h>
18#include <linux/slab.h>
19#include <linux/string.h>
20#include <linux/rtc.h> /* get the user-level API */
21#include <linux/init.h>
22#include <linux/init.h>
23
24#include "rtc8564.h"
25
26#ifdef DEBUG
27# define _DBG(x, fmt, args...) do{ if (debug>=x) printk(KERN_DEBUG"%s: " fmt "\n", __FUNCTION__, ##args); } while(0);
28#else
29# define _DBG(x, fmt, args...) do { } while(0);
30#endif
31
32#define _DBGRTCTM(x, rtctm) if (debug>=x) printk("%s: secs=%d, mins=%d, hours=%d, mday=%d, " \
33 "mon=%d, year=%d, wday=%d VL=%d\n", __FUNCTION__, \
34 (rtctm).secs, (rtctm).mins, (rtctm).hours, (rtctm).mday, \
35 (rtctm).mon, (rtctm).year, (rtctm).wday, (rtctm).vl);
36
37struct rtc8564_data {
38 struct i2c_client client;
39 u16 ctrl;
40};
41
42static inline u8 _rtc8564_ctrl1(struct i2c_client *client)
43{
44 struct rtc8564_data *data = i2c_get_clientdata(client);
45 return data->ctrl & 0xff;
46}
47static inline u8 _rtc8564_ctrl2(struct i2c_client *client)
48{
49 struct rtc8564_data *data = i2c_get_clientdata(client);
50 return (data->ctrl & 0xff00) >> 8;
51}
52
53#define CTRL1(c) _rtc8564_ctrl1(c)
54#define CTRL2(c) _rtc8564_ctrl2(c)
55
56#define BCD_TO_BIN(val) (((val)&15) + ((val)>>4)*10)
57#define BIN_TO_BCD(val) ((((val)/10)<<4) + (val)%10)
58
59static int debug;;
60module_param(debug, int, S_IRUGO | S_IWUSR);
61
62static struct i2c_driver rtc8564_driver;
63
64static unsigned short ignore[] = { I2C_CLIENT_END };
65static unsigned short normal_addr[] = { 0x51, I2C_CLIENT_END };
66
67static struct i2c_client_address_data addr_data = {
68 .normal_i2c = normal_addr,
69 .normal_i2c_range = ignore,
70 .probe = ignore,
71 .probe_range = ignore,
72 .ignore = ignore,
73 .ignore_range = ignore,
74 .force = ignore,
75};
76
77static int rtc8564_read_mem(struct i2c_client *client, struct mem *mem);
78static int rtc8564_write_mem(struct i2c_client *client, struct mem *mem);
79
80static int rtc8564_read(struct i2c_client *client, unsigned char adr,
81 unsigned char *buf, unsigned char len)
82{
83 int ret = -EIO;
84 unsigned char addr[1] = { adr };
85 struct i2c_msg msgs[2] = {
86 {client->addr, 0, 1, addr},
87 {client->addr, I2C_M_RD, len, buf}
88 };
89
90 _DBG(1, "client=%p, adr=%d, buf=%p, len=%d", client, adr, buf, len);
91
92 if (!buf) {
93 ret = -EINVAL;
94 goto done;
95 }
96
97 ret = i2c_transfer(client->adapter, msgs, 2);
98 if (ret == 2) {
99 ret = 0;
100 }
101
102done:
103 return ret;
104}
105
106static int rtc8564_write(struct i2c_client *client, unsigned char adr,
107 unsigned char *data, unsigned char len)
108{
109 int ret = 0;
110 unsigned char _data[16];
111 struct i2c_msg wr;
112 int i;
113
114 if (!data || len > 15) {
115 ret = -EINVAL;
116 goto done;
117 }
118
119 _DBG(1, "client=%p, adr=%d, buf=%p, len=%d", client, adr, data, len);
120
121 _data[0] = adr;
122 for (i = 0; i < len; i++) {
123 _data[i + 1] = data[i];
124 _DBG(5, "data[%d] = 0x%02x (%d)", i, data[i], data[i]);
125 }
126
127 wr.addr = client->addr;
128 wr.flags = 0;
129 wr.len = len + 1;
130 wr.buf = _data;
131
132 ret = i2c_transfer(client->adapter, &wr, 1);
133 if (ret == 1) {
134 ret = 0;
135 }
136
137done:
138 return ret;
139}
140
141static int rtc8564_attach(struct i2c_adapter *adap, int addr, int kind)
142{
143 int ret;
144 struct i2c_client *new_client;
145 struct rtc8564_data *d;
146 unsigned char data[10];
147 unsigned char ad[1] = { 0 };
148 struct i2c_msg ctrl_wr[1] = {
149 {addr, 0, 2, data}
150 };
151 struct i2c_msg ctrl_rd[2] = {
152 {addr, 0, 1, ad},
153 {addr, I2C_M_RD, 2, data}
154 };
155
156 d = kmalloc(sizeof(struct rtc8564_data), GFP_KERNEL);
157 if (!d) {
158 ret = -ENOMEM;
159 goto done;
160 }
161 memset(d, 0, sizeof(struct rtc8564_data));
162 new_client = &d->client;
163
164 strlcpy(new_client->name, "RTC8564", I2C_NAME_SIZE);
165 i2c_set_clientdata(new_client, d);
166 new_client->flags = I2C_CLIENT_ALLOW_USE | I2C_DF_NOTIFY;
167 new_client->addr = addr;
168 new_client->adapter = adap;
169 new_client->driver = &rtc8564_driver;
170
171 _DBG(1, "client=%p", new_client);
172
173 /* init ctrl1 reg */
174 data[0] = 0;
175 data[1] = 0;
176 ret = i2c_transfer(new_client->adapter, ctrl_wr, 1);
177 if (ret != 1) {
178 printk(KERN_INFO "rtc8564: cant init ctrl1\n");
179 ret = -ENODEV;
180 goto done;
181 }
182
183 /* read back ctrl1 and ctrl2 */
184 ret = i2c_transfer(new_client->adapter, ctrl_rd, 2);
185 if (ret != 2) {
186 printk(KERN_INFO "rtc8564: cant read ctrl\n");
187 ret = -ENODEV;
188 goto done;
189 }
190
191 d->ctrl = data[0] | (data[1] << 8);
192
193 _DBG(1, "RTC8564_REG_CTRL1=%02x, RTC8564_REG_CTRL2=%02x",
194 data[0], data[1]);
195
196 ret = i2c_attach_client(new_client);
197done:
198 if (ret) {
199 kfree(d);
200 }
201 return ret;
202}
203
204static int rtc8564_probe(struct i2c_adapter *adap)
205{
206 return i2c_probe(adap, &addr_data, rtc8564_attach);
207}
208
209static int rtc8564_detach(struct i2c_client *client)
210{
211 i2c_detach_client(client);
212 kfree(i2c_get_clientdata(client));
213 return 0;
214}
215
216static int rtc8564_get_datetime(struct i2c_client *client, struct rtc_tm *dt)
217{
218 int ret = -EIO;
219 unsigned char buf[15];
220
221 _DBG(1, "client=%p, dt=%p", client, dt);
222
223 if (!dt)
224 return -EINVAL;
225
226 memset(buf, 0, sizeof(buf));
227
228 ret = rtc8564_read(client, 0, buf, 15);
229 if (ret)
230 return ret;
231
232 /* century stored in minute alarm reg */
233 dt->year = BCD_TO_BIN(buf[RTC8564_REG_YEAR]);
234 dt->year += 100 * BCD_TO_BIN(buf[RTC8564_REG_AL_MIN] & 0x3f);
235 dt->mday = BCD_TO_BIN(buf[RTC8564_REG_DAY] & 0x3f);
236 dt->wday = BCD_TO_BIN(buf[RTC8564_REG_WDAY] & 7);
237 dt->mon = BCD_TO_BIN(buf[RTC8564_REG_MON_CENT] & 0x1f);
238
239 dt->secs = BCD_TO_BIN(buf[RTC8564_REG_SEC] & 0x7f);
240 dt->vl = (buf[RTC8564_REG_SEC] & 0x80) == 0x80;
241 dt->mins = BCD_TO_BIN(buf[RTC8564_REG_MIN] & 0x7f);
242 dt->hours = BCD_TO_BIN(buf[RTC8564_REG_HR] & 0x3f);
243
244 _DBGRTCTM(2, *dt);
245
246 return 0;
247}
248
249static int
250rtc8564_set_datetime(struct i2c_client *client, struct rtc_tm *dt, int datetoo)
251{
252 int ret, len = 5;
253 unsigned char buf[15];
254
255 _DBG(1, "client=%p, dt=%p", client, dt);
256
257 if (!dt)
258 return -EINVAL;
259
260 _DBGRTCTM(2, *dt);
261
262 buf[RTC8564_REG_CTRL1] = CTRL1(client) | RTC8564_CTRL1_STOP;
263 buf[RTC8564_REG_CTRL2] = CTRL2(client);
264 buf[RTC8564_REG_SEC] = BIN_TO_BCD(dt->secs);
265 buf[RTC8564_REG_MIN] = BIN_TO_BCD(dt->mins);
266 buf[RTC8564_REG_HR] = BIN_TO_BCD(dt->hours);
267
268 if (datetoo) {
269 len += 5;
270 buf[RTC8564_REG_DAY] = BIN_TO_BCD(dt->mday);
271 buf[RTC8564_REG_WDAY] = BIN_TO_BCD(dt->wday);
272 buf[RTC8564_REG_MON_CENT] = BIN_TO_BCD(dt->mon) & 0x1f;
273 /* century stored in minute alarm reg */
274 buf[RTC8564_REG_YEAR] = BIN_TO_BCD(dt->year % 100);
275 buf[RTC8564_REG_AL_MIN] = BIN_TO_BCD(dt->year / 100);
276 }
277
278 ret = rtc8564_write(client, 0, buf, len);
279 if (ret) {
280 _DBG(1, "error writing data! %d", ret);
281 }
282
283 buf[RTC8564_REG_CTRL1] = CTRL1(client);
284 ret = rtc8564_write(client, 0, buf, 1);
285 if (ret) {
286 _DBG(1, "error writing data! %d", ret);
287 }
288
289 return ret;
290}
291
292static int rtc8564_get_ctrl(struct i2c_client *client, unsigned int *ctrl)
293{
294 struct rtc8564_data *data = i2c_get_clientdata(client);
295
296 if (!ctrl)
297 return -1;
298
299 *ctrl = data->ctrl;
300 return 0;
301}
302
303static int rtc8564_set_ctrl(struct i2c_client *client, unsigned int *ctrl)
304{
305 struct rtc8564_data *data = i2c_get_clientdata(client);
306 unsigned char buf[2];
307
308 if (!ctrl)
309 return -1;
310
311 buf[0] = *ctrl & 0xff;
312 buf[1] = (*ctrl & 0xff00) >> 8;
313 data->ctrl = *ctrl;
314
315 return rtc8564_write(client, 0, buf, 2);
316}
317
318static int rtc8564_read_mem(struct i2c_client *client, struct mem *mem)
319{
320
321 if (!mem)
322 return -EINVAL;
323
324 return rtc8564_read(client, mem->loc, mem->data, mem->nr);
325}
326
327static int rtc8564_write_mem(struct i2c_client *client, struct mem *mem)
328{
329
330 if (!mem)
331 return -EINVAL;
332
333 return rtc8564_write(client, mem->loc, mem->data, mem->nr);
334}
335
336static int
337rtc8564_command(struct i2c_client *client, unsigned int cmd, void *arg)
338{
339
340 _DBG(1, "cmd=%d", cmd);
341
342 switch (cmd) {
343 case RTC_GETDATETIME:
344 return rtc8564_get_datetime(client, arg);
345
346 case RTC_SETTIME:
347 return rtc8564_set_datetime(client, arg, 0);
348
349 case RTC_SETDATETIME:
350 return rtc8564_set_datetime(client, arg, 1);
351
352 case RTC_GETCTRL:
353 return rtc8564_get_ctrl(client, arg);
354
355 case RTC_SETCTRL:
356 return rtc8564_set_ctrl(client, arg);
357
358 case MEM_READ:
359 return rtc8564_read_mem(client, arg);
360
361 case MEM_WRITE:
362 return rtc8564_write_mem(client, arg);
363
364 default:
365 return -EINVAL;
366 }
367}
368
369static struct i2c_driver rtc8564_driver = {
370 .owner = THIS_MODULE,
371 .name = "RTC8564",
372 .id = I2C_DRIVERID_RTC8564,
373 .flags = I2C_DF_NOTIFY,
374 .attach_adapter = rtc8564_probe,
375 .detach_client = rtc8564_detach,
376 .command = rtc8564_command
377};
378
379static __init int rtc8564_init(void)
380{
381 return i2c_add_driver(&rtc8564_driver);
382}
383
384static __exit void rtc8564_exit(void)
385{
386 i2c_del_driver(&rtc8564_driver);
387}
388
389MODULE_AUTHOR("Stefan Eletzhofer <Stefan.Eletzhofer@eletztrick.de>");
390MODULE_DESCRIPTION("EPSON RTC8564 Driver");
391MODULE_LICENSE("GPL");
392
393module_init(rtc8564_init);
394module_exit(rtc8564_exit);
diff --git a/drivers/i2c/chips/rtc8564.h b/drivers/i2c/chips/rtc8564.h
new file mode 100644
index 000000000000..e5342d10b8fa
--- /dev/null
+++ b/drivers/i2c/chips/rtc8564.h
@@ -0,0 +1,78 @@
1/*
2 * linux/drivers/i2c/chips/rtc8564.h
3 *
4 * Copyright (C) 2002-2004 Stefan Eletzhofer
5 *
6 * based on linux/drivers/acron/char/pcf8583.h
7 * Copyright (C) 2000 Russell King
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13struct rtc_tm {
14 unsigned char secs;
15 unsigned char mins;
16 unsigned char hours;
17 unsigned char mday;
18 unsigned char mon;
19 unsigned short year; /* xxxx 4 digits :) */
20 unsigned char wday;
21 unsigned char vl;
22};
23
24struct mem {
25 unsigned int loc;
26 unsigned int nr;
27 unsigned char *data;
28};
29
30#define RTC_GETDATETIME 0
31#define RTC_SETTIME 1
32#define RTC_SETDATETIME 2
33#define RTC_GETCTRL 3
34#define RTC_SETCTRL 4
35#define MEM_READ 5
36#define MEM_WRITE 6
37
38#define RTC8564_REG_CTRL1 0x0 /* T 0 S 0 | T 0 0 0 */
39#define RTC8564_REG_CTRL2 0x1 /* 0 0 0 TI/TP | AF TF AIE TIE */
40#define RTC8564_REG_SEC 0x2 /* VL 4 2 1 | 8 4 2 1 */
41#define RTC8564_REG_MIN 0x3 /* x 4 2 1 | 8 4 2 1 */
42#define RTC8564_REG_HR 0x4 /* x x 2 1 | 8 4 2 1 */
43#define RTC8564_REG_DAY 0x5 /* x x 2 1 | 8 4 2 1 */
44#define RTC8564_REG_WDAY 0x6 /* x x x x | x 4 2 1 */
45#define RTC8564_REG_MON_CENT 0x7 /* C x x 1 | 8 4 2 1 */
46#define RTC8564_REG_YEAR 0x8 /* 8 4 2 1 | 8 4 2 1 */
47#define RTC8564_REG_AL_MIN 0x9 /* AE 4 2 1 | 8 4 2 1 */
48#define RTC8564_REG_AL_HR 0xa /* AE 4 2 1 | 8 4 2 1 */
49#define RTC8564_REG_AL_DAY 0xb /* AE x 2 1 | 8 4 2 1 */
50#define RTC8564_REG_AL_WDAY 0xc /* AE x x x | x 4 2 1 */
51#define RTC8564_REG_CLKOUT 0xd /* FE x x x | x x FD1 FD0 */
52#define RTC8564_REG_TCTL 0xe /* TE x x x | x x FD1 FD0 */
53#define RTC8564_REG_TIMER 0xf /* 8 bit binary */
54
55/* Control reg */
56#define RTC8564_CTRL1_TEST1 (1<<3)
57#define RTC8564_CTRL1_STOP (1<<5)
58#define RTC8564_CTRL1_TEST2 (1<<7)
59
60#define RTC8564_CTRL2_TIE (1<<0)
61#define RTC8564_CTRL2_AIE (1<<1)
62#define RTC8564_CTRL2_TF (1<<2)
63#define RTC8564_CTRL2_AF (1<<3)
64#define RTC8564_CTRL2_TI_TP (1<<4)
65
66/* CLKOUT frequencies */
67#define RTC8564_FD_32768HZ (0x0)
68#define RTC8564_FD_1024HZ (0x1)
69#define RTC8564_FD_32 (0x2)
70#define RTC8564_FD_1HZ (0x3)
71
72/* Timer CTRL */
73#define RTC8564_TD_4096HZ (0x0)
74#define RTC8564_TD_64HZ (0x1)
75#define RTC8564_TD_1HZ (0x2)
76#define RTC8564_TD_1_60HZ (0x3)
77
78#define I2C_DRIVERID_RTC8564 0xf000
diff --git a/drivers/i2c/chips/sis5595.c b/drivers/i2c/chips/sis5595.c
new file mode 100644
index 000000000000..7ea84532df32
--- /dev/null
+++ b/drivers/i2c/chips/sis5595.c
@@ -0,0 +1,816 @@
1/*
2 sis5595.c - Part of lm_sensors, Linux kernel modules
3 for hardware monitoring
4
5 Copyright (C) 1998 - 2001 Frodo Looijaard <frodol@dds.nl>,
6 Kyösti Mälkki <kmalkki@cc.hut.fi>, and
7 Mark D. Studebaker <mdsxyz123@yahoo.com>
8 Ported to Linux 2.6 by Aurelien Jarno <aurelien@aurel32.net> with
9 the help of Jean Delvare <khali@linux-fr.org>
10
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24*/
25
26/*
27 SiS southbridge has a LM78-like chip integrated on the same IC.
28 This driver is a customized copy of lm78.c
29
30 Supports following revisions:
31 Version PCI ID PCI Revision
32 1 1039/0008 AF or less
33 2 1039/0008 B0 or greater
34
35 Note: these chips contain a 0008 device which is incompatible with the
36 5595. We recognize these by the presence of the listed
37 "blacklist" PCI ID and refuse to load.
38
39 NOT SUPPORTED PCI ID BLACKLIST PCI ID
40 540 0008 0540
41 550 0008 0550
42 5513 0008 5511
43 5581 0008 5597
44 5582 0008 5597
45 5597 0008 5597
46 5598 0008 5597/5598
47 630 0008 0630
48 645 0008 0645
49 730 0008 0730
50 735 0008 0735
51*/
52
53#include <linux/module.h>
54#include <linux/slab.h>
55#include <linux/ioport.h>
56#include <linux/pci.h>
57#include <linux/i2c.h>
58#include <linux/i2c-sensor.h>
59#include <linux/init.h>
60#include <asm/io.h>
61
62
63/* If force_addr is set to anything different from 0, we forcibly enable
64 the device at the given address. */
65static u16 force_addr;
66module_param(force_addr, ushort, 0);
67MODULE_PARM_DESC(force_addr,
68 "Initialize the base address of the sensors");
69
70/* Addresses to scan.
71 Note that we can't determine the ISA address until we have initialized
72 our module */
73static unsigned short normal_i2c[] = { I2C_CLIENT_END };
74static unsigned int normal_isa[] = { 0x0000, I2C_CLIENT_ISA_END };
75
76/* Insmod parameters */
77SENSORS_INSMOD_1(sis5595);
78
79/* Many SIS5595 constants specified below */
80
81/* Length of ISA address segment */
82#define SIS5595_EXTENT 8
83/* PCI Config Registers */
84#define SIS5595_REVISION_REG 0x08
85#define SIS5595_BASE_REG 0x68
86#define SIS5595_PIN_REG 0x7A
87#define SIS5595_ENABLE_REG 0x7B
88
89/* Where are the ISA address/data registers relative to the base address */
90#define SIS5595_ADDR_REG_OFFSET 5
91#define SIS5595_DATA_REG_OFFSET 6
92
93/* The SIS5595 registers */
94#define SIS5595_REG_IN_MAX(nr) (0x2b + (nr) * 2)
95#define SIS5595_REG_IN_MIN(nr) (0x2c + (nr) * 2)
96#define SIS5595_REG_IN(nr) (0x20 + (nr))
97
98#define SIS5595_REG_FAN_MIN(nr) (0x3b + (nr))
99#define SIS5595_REG_FAN(nr) (0x28 + (nr))
100
101/* On the first version of the chip, the temp registers are separate.
102 On the second version,
103 TEMP pin is shared with IN4, configured in PCI register 0x7A.
104 The registers are the same as well.
105 OVER and HYST are really MAX and MIN. */
106
107#define REV2MIN 0xb0
108#define SIS5595_REG_TEMP (( data->revision) >= REV2MIN) ? \
109 SIS5595_REG_IN(4) : 0x27
110#define SIS5595_REG_TEMP_OVER (( data->revision) >= REV2MIN) ? \
111 SIS5595_REG_IN_MAX(4) : 0x39
112#define SIS5595_REG_TEMP_HYST (( data->revision) >= REV2MIN) ? \
113 SIS5595_REG_IN_MIN(4) : 0x3a
114
115#define SIS5595_REG_CONFIG 0x40
116#define SIS5595_REG_ALARM1 0x41
117#define SIS5595_REG_ALARM2 0x42
118#define SIS5595_REG_FANDIV 0x47
119
120/* Conversions. Limit checking is only done on the TO_REG
121 variants. */
122
123/* IN: mV, (0V to 4.08V)
124 REG: 16mV/bit */
125static inline u8 IN_TO_REG(unsigned long val)
126{
127 unsigned long nval = SENSORS_LIMIT(val, 0, 4080);
128 return (nval + 8) / 16;
129}
130#define IN_FROM_REG(val) ((val) * 16)
131
132static inline u8 FAN_TO_REG(long rpm, int div)
133{
134 if (rpm <= 0)
135 return 255;
136 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
137}
138
139static inline int FAN_FROM_REG(u8 val, int div)
140{
141 return val==0 ? -1 : val==255 ? 0 : 1350000/(val*div);
142}
143
144/* TEMP: mC (-54.12C to +157.53C)
145 REG: 0.83C/bit + 52.12, two's complement */
146static inline int TEMP_FROM_REG(s8 val)
147{
148 return val * 830 + 52120;
149}
150static inline s8 TEMP_TO_REG(int val)
151{
152 int nval = SENSORS_LIMIT(val, -54120, 157530) ;
153 return nval<0 ? (nval-5212-415)/830 : (nval-5212+415)/830;
154}
155
156/* FAN DIV: 1, 2, 4, or 8 (defaults to 2)
157 REG: 0, 1, 2, or 3 (respectively) (defaults to 1) */
158static inline u8 DIV_TO_REG(int val)
159{
160 return val==8 ? 3 : val==4 ? 2 : val==1 ? 0 : 1;
161}
162#define DIV_FROM_REG(val) (1 << (val))
163
164/* For the SIS5595, we need to keep some data in memory. That
165 data is pointed to by sis5595_list[NR]->data. The structure itself is
166 dynamically allocated, at the time when the new sis5595 client is
167 allocated. */
168struct sis5595_data {
169 struct i2c_client client;
170 struct semaphore lock;
171
172 struct semaphore update_lock;
173 char valid; /* !=0 if following fields are valid */
174 unsigned long last_updated; /* In jiffies */
175 char maxins; /* == 3 if temp enabled, otherwise == 4 */
176 u8 revision; /* Reg. value */
177
178 u8 in[5]; /* Register value */
179 u8 in_max[5]; /* Register value */
180 u8 in_min[5]; /* Register value */
181 u8 fan[2]; /* Register value */
182 u8 fan_min[2]; /* Register value */
183 s8 temp; /* Register value */
184 s8 temp_over; /* Register value */
185 s8 temp_hyst; /* Register value */
186 u8 fan_div[2]; /* Register encoding, shifted right */
187 u16 alarms; /* Register encoding, combined */
188};
189
190static struct pci_dev *s_bridge; /* pointer to the (only) sis5595 */
191
192static int sis5595_attach_adapter(struct i2c_adapter *adapter);
193static int sis5595_detect(struct i2c_adapter *adapter, int address, int kind);
194static int sis5595_detach_client(struct i2c_client *client);
195
196static int sis5595_read_value(struct i2c_client *client, u8 register);
197static int sis5595_write_value(struct i2c_client *client, u8 register, u8 value);
198static struct sis5595_data *sis5595_update_device(struct device *dev);
199static void sis5595_init_client(struct i2c_client *client);
200
201static struct i2c_driver sis5595_driver = {
202 .owner = THIS_MODULE,
203 .name = "sis5595",
204 .id = I2C_DRIVERID_SIS5595,
205 .flags = I2C_DF_NOTIFY,
206 .attach_adapter = sis5595_attach_adapter,
207 .detach_client = sis5595_detach_client,
208};
209
210/* 4 Voltages */
211static ssize_t show_in(struct device *dev, char *buf, int nr)
212{
213 struct sis5595_data *data = sis5595_update_device(dev);
214 return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr]));
215}
216
217static ssize_t show_in_min(struct device *dev, char *buf, int nr)
218{
219 struct sis5595_data *data = sis5595_update_device(dev);
220 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr]));
221}
222
223static ssize_t show_in_max(struct device *dev, char *buf, int nr)
224{
225 struct sis5595_data *data = sis5595_update_device(dev);
226 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr]));
227}
228
229static ssize_t set_in_min(struct device *dev, const char *buf,
230 size_t count, int nr)
231{
232 struct i2c_client *client = to_i2c_client(dev);
233 struct sis5595_data *data = i2c_get_clientdata(client);
234 unsigned long val = simple_strtoul(buf, NULL, 10);
235
236 down(&data->update_lock);
237 data->in_min[nr] = IN_TO_REG(val);
238 sis5595_write_value(client, SIS5595_REG_IN_MIN(nr), data->in_min[nr]);
239 up(&data->update_lock);
240 return count;
241}
242
243static ssize_t set_in_max(struct device *dev, const char *buf,
244 size_t count, int nr)
245{
246 struct i2c_client *client = to_i2c_client(dev);
247 struct sis5595_data *data = i2c_get_clientdata(client);
248 unsigned long val = simple_strtoul(buf, NULL, 10);
249
250 down(&data->update_lock);
251 data->in_max[nr] = IN_TO_REG(val);
252 sis5595_write_value(client, SIS5595_REG_IN_MAX(nr), data->in_max[nr]);
253 up(&data->update_lock);
254 return count;
255}
256
257#define show_in_offset(offset) \
258static ssize_t \
259 show_in##offset (struct device *dev, char *buf) \
260{ \
261 return show_in(dev, buf, offset); \
262} \
263static DEVICE_ATTR(in##offset##_input, S_IRUGO, \
264 show_in##offset, NULL); \
265static ssize_t \
266 show_in##offset##_min (struct device *dev, char *buf) \
267{ \
268 return show_in_min(dev, buf, offset); \
269} \
270static ssize_t \
271 show_in##offset##_max (struct device *dev, char *buf) \
272{ \
273 return show_in_max(dev, buf, offset); \
274} \
275static ssize_t set_in##offset##_min (struct device *dev, \
276 const char *buf, size_t count) \
277{ \
278 return set_in_min(dev, buf, count, offset); \
279} \
280static ssize_t set_in##offset##_max (struct device *dev, \
281 const char *buf, size_t count) \
282{ \
283 return set_in_max(dev, buf, count, offset); \
284} \
285static DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
286 show_in##offset##_min, set_in##offset##_min); \
287static DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
288 show_in##offset##_max, set_in##offset##_max);
289
290show_in_offset(0);
291show_in_offset(1);
292show_in_offset(2);
293show_in_offset(3);
294show_in_offset(4);
295
296/* Temperature */
297static ssize_t show_temp(struct device *dev, char *buf)
298{
299 struct sis5595_data *data = sis5595_update_device(dev);
300 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp));
301}
302
303static ssize_t show_temp_over(struct device *dev, char *buf)
304{
305 struct sis5595_data *data = sis5595_update_device(dev);
306 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_over));
307}
308
309static ssize_t set_temp_over(struct device *dev, const char *buf, size_t count)
310{
311 struct i2c_client *client = to_i2c_client(dev);
312 struct sis5595_data *data = i2c_get_clientdata(client);
313 long val = simple_strtol(buf, NULL, 10);
314
315 down(&data->update_lock);
316 data->temp_over = TEMP_TO_REG(val);
317 sis5595_write_value(client, SIS5595_REG_TEMP_OVER, data->temp_over);
318 up(&data->update_lock);
319 return count;
320}
321
322static ssize_t show_temp_hyst(struct device *dev, char *buf)
323{
324 struct sis5595_data *data = sis5595_update_device(dev);
325 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_hyst));
326}
327
328static ssize_t set_temp_hyst(struct device *dev, const char *buf, size_t count)
329{
330 struct i2c_client *client = to_i2c_client(dev);
331 struct sis5595_data *data = i2c_get_clientdata(client);
332 long val = simple_strtol(buf, NULL, 10);
333
334 down(&data->update_lock);
335 data->temp_hyst = TEMP_TO_REG(val);
336 sis5595_write_value(client, SIS5595_REG_TEMP_HYST, data->temp_hyst);
337 up(&data->update_lock);
338 return count;
339}
340
341static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL);
342static DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR,
343 show_temp_over, set_temp_over);
344static DEVICE_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR,
345 show_temp_hyst, set_temp_hyst);
346
347/* 2 Fans */
348static ssize_t show_fan(struct device *dev, char *buf, int nr)
349{
350 struct sis5595_data *data = sis5595_update_device(dev);
351 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
352 DIV_FROM_REG(data->fan_div[nr])) );
353}
354
355static ssize_t show_fan_min(struct device *dev, char *buf, int nr)
356{
357 struct sis5595_data *data = sis5595_update_device(dev);
358 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan_min[nr],
359 DIV_FROM_REG(data->fan_div[nr])) );
360}
361
362static ssize_t set_fan_min(struct device *dev, const char *buf,
363 size_t count, int nr)
364{
365 struct i2c_client *client = to_i2c_client(dev);
366 struct sis5595_data *data = i2c_get_clientdata(client);
367 unsigned long val = simple_strtoul(buf, NULL, 10);
368
369 down(&data->update_lock);
370 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
371 sis5595_write_value(client, SIS5595_REG_FAN_MIN(nr), data->fan_min[nr]);
372 up(&data->update_lock);
373 return count;
374}
375
376static ssize_t show_fan_div(struct device *dev, char *buf, int nr)
377{
378 struct sis5595_data *data = sis5595_update_device(dev);
379 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]) );
380}
381
382/* Note: we save and restore the fan minimum here, because its value is
383 determined in part by the fan divisor. This follows the principle of
384 least suprise; the user doesn't expect the fan minimum to change just
385 because the divisor changed. */
386static ssize_t set_fan_div(struct device *dev, const char *buf,
387 size_t count, int nr)
388{
389 struct i2c_client *client = to_i2c_client(dev);
390 struct sis5595_data *data = i2c_get_clientdata(client);
391 unsigned long min;
392 unsigned long val = simple_strtoul(buf, NULL, 10);
393 int reg;
394
395 down(&data->update_lock);
396 min = FAN_FROM_REG(data->fan_min[nr],
397 DIV_FROM_REG(data->fan_div[nr]));
398 reg = sis5595_read_value(client, SIS5595_REG_FANDIV);
399
400 switch (val) {
401 case 1: data->fan_div[nr] = 0; break;
402 case 2: data->fan_div[nr] = 1; break;
403 case 4: data->fan_div[nr] = 2; break;
404 case 8: data->fan_div[nr] = 3; break;
405 default:
406 dev_err(&client->dev, "fan_div value %ld not "
407 "supported. Choose one of 1, 2, 4 or 8!\n", val);
408 up(&data->update_lock);
409 return -EINVAL;
410 }
411
412 switch (nr) {
413 case 0:
414 reg = (reg & 0xcf) | (data->fan_div[nr] << 4);
415 break;
416 case 1:
417 reg = (reg & 0x3f) | (data->fan_div[nr] << 6);
418 break;
419 }
420 sis5595_write_value(client, SIS5595_REG_FANDIV, reg);
421 data->fan_min[nr] =
422 FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
423 sis5595_write_value(client, SIS5595_REG_FAN_MIN(nr), data->fan_min[nr]);
424 up(&data->update_lock);
425 return count;
426}
427
428#define show_fan_offset(offset) \
429static ssize_t show_fan_##offset (struct device *dev, char *buf) \
430{ \
431 return show_fan(dev, buf, offset - 1); \
432} \
433static ssize_t show_fan_##offset##_min (struct device *dev, char *buf) \
434{ \
435 return show_fan_min(dev, buf, offset - 1); \
436} \
437static ssize_t show_fan_##offset##_div (struct device *dev, char *buf) \
438{ \
439 return show_fan_div(dev, buf, offset - 1); \
440} \
441static ssize_t set_fan_##offset##_min (struct device *dev, \
442 const char *buf, size_t count) \
443{ \
444 return set_fan_min(dev, buf, count, offset - 1); \
445} \
446static DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_fan_##offset, NULL);\
447static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
448 show_fan_##offset##_min, set_fan_##offset##_min);
449
450show_fan_offset(1);
451show_fan_offset(2);
452
453static ssize_t set_fan_1_div(struct device *dev, const char *buf,
454 size_t count)
455{
456 return set_fan_div(dev, buf, count, 0) ;
457}
458
459static ssize_t set_fan_2_div(struct device *dev, const char *buf,
460 size_t count)
461{
462 return set_fan_div(dev, buf, count, 1) ;
463}
464static DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR,
465 show_fan_1_div, set_fan_1_div);
466static DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR,
467 show_fan_2_div, set_fan_2_div);
468
469/* Alarms */
470static ssize_t show_alarms(struct device *dev, char *buf)
471{
472 struct sis5595_data *data = sis5595_update_device(dev);
473 return sprintf(buf, "%d\n", data->alarms);
474}
475static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
476
477/* This is called when the module is loaded */
478static int sis5595_attach_adapter(struct i2c_adapter *adapter)
479{
480 if (!(adapter->class & I2C_CLASS_HWMON))
481 return 0;
482 return i2c_detect(adapter, &addr_data, sis5595_detect);
483}
484
485int sis5595_detect(struct i2c_adapter *adapter, int address, int kind)
486{
487 int err = 0;
488 int i;
489 struct i2c_client *new_client;
490 struct sis5595_data *data;
491 char val;
492 u16 a;
493
494 /* Make sure we are probing the ISA bus!! */
495 if (!i2c_is_isa_adapter(adapter))
496 goto exit;
497
498 if (force_addr)
499 address = force_addr & ~(SIS5595_EXTENT - 1);
500 /* Reserve the ISA region */
501 if (!request_region(address, SIS5595_EXTENT, sis5595_driver.name)) {
502 err = -EBUSY;
503 goto exit;
504 }
505 if (force_addr) {
506 dev_warn(&adapter->dev, "forcing ISA address 0x%04X\n", address);
507 if (PCIBIOS_SUCCESSFUL !=
508 pci_write_config_word(s_bridge, SIS5595_BASE_REG, address))
509 goto exit_release;
510 if (PCIBIOS_SUCCESSFUL !=
511 pci_read_config_word(s_bridge, SIS5595_BASE_REG, &a))
512 goto exit_release;
513 if ((a & ~(SIS5595_EXTENT - 1)) != address)
514 /* doesn't work for some chips? */
515 goto exit_release;
516 }
517
518 if (PCIBIOS_SUCCESSFUL !=
519 pci_read_config_byte(s_bridge, SIS5595_ENABLE_REG, &val)) {
520 goto exit_release;
521 }
522 if ((val & 0x80) == 0) {
523 if (PCIBIOS_SUCCESSFUL !=
524 pci_write_config_byte(s_bridge, SIS5595_ENABLE_REG,
525 val | 0x80))
526 goto exit_release;
527 if (PCIBIOS_SUCCESSFUL !=
528 pci_read_config_byte(s_bridge, SIS5595_ENABLE_REG, &val))
529 goto exit_release;
530 if ((val & 0x80) == 0)
531 /* doesn't work for some chips! */
532 goto exit_release;
533 }
534
535 if (!(data = kmalloc(sizeof(struct sis5595_data), GFP_KERNEL))) {
536 err = -ENOMEM;
537 goto exit_release;
538 }
539 memset(data, 0, sizeof(struct sis5595_data));
540
541 new_client = &data->client;
542 new_client->addr = address;
543 init_MUTEX(&data->lock);
544 i2c_set_clientdata(new_client, data);
545 new_client->adapter = adapter;
546 new_client->driver = &sis5595_driver;
547 new_client->flags = 0;
548
549 /* Check revision and pin registers to determine whether 4 or 5 voltages */
550 pci_read_config_byte(s_bridge, SIS5595_REVISION_REG, &(data->revision));
551 /* 4 voltages, 1 temp */
552 data->maxins = 3;
553 if (data->revision >= REV2MIN) {
554 pci_read_config_byte(s_bridge, SIS5595_PIN_REG, &val);
555 if (!(val & 0x80))
556 /* 5 voltages, no temps */
557 data->maxins = 4;
558 }
559
560 /* Fill in the remaining client fields and put it into the global list */
561 strlcpy(new_client->name, "sis5595", I2C_NAME_SIZE);
562
563 data->valid = 0;
564 init_MUTEX(&data->update_lock);
565
566 /* Tell the I2C layer a new client has arrived */
567 if ((err = i2c_attach_client(new_client)))
568 goto exit_free;
569
570 /* Initialize the SIS5595 chip */
571 sis5595_init_client(new_client);
572
573 /* A few vars need to be filled upon startup */
574 for (i = 0; i < 2; i++) {
575 data->fan_min[i] = sis5595_read_value(new_client,
576 SIS5595_REG_FAN_MIN(i));
577 }
578
579 /* Register sysfs hooks */
580 device_create_file(&new_client->dev, &dev_attr_in0_input);
581 device_create_file(&new_client->dev, &dev_attr_in0_min);
582 device_create_file(&new_client->dev, &dev_attr_in0_max);
583 device_create_file(&new_client->dev, &dev_attr_in1_input);
584 device_create_file(&new_client->dev, &dev_attr_in1_min);
585 device_create_file(&new_client->dev, &dev_attr_in1_max);
586 device_create_file(&new_client->dev, &dev_attr_in2_input);
587 device_create_file(&new_client->dev, &dev_attr_in2_min);
588 device_create_file(&new_client->dev, &dev_attr_in2_max);
589 device_create_file(&new_client->dev, &dev_attr_in3_input);
590 device_create_file(&new_client->dev, &dev_attr_in3_min);
591 device_create_file(&new_client->dev, &dev_attr_in3_max);
592 if (data->maxins == 4) {
593 device_create_file(&new_client->dev, &dev_attr_in4_input);
594 device_create_file(&new_client->dev, &dev_attr_in4_min);
595 device_create_file(&new_client->dev, &dev_attr_in4_max);
596 }
597 device_create_file(&new_client->dev, &dev_attr_fan1_input);
598 device_create_file(&new_client->dev, &dev_attr_fan1_min);
599 device_create_file(&new_client->dev, &dev_attr_fan1_div);
600 device_create_file(&new_client->dev, &dev_attr_fan2_input);
601 device_create_file(&new_client->dev, &dev_attr_fan2_min);
602 device_create_file(&new_client->dev, &dev_attr_fan2_div);
603 device_create_file(&new_client->dev, &dev_attr_alarms);
604 if (data->maxins == 3) {
605 device_create_file(&new_client->dev, &dev_attr_temp1_input);
606 device_create_file(&new_client->dev, &dev_attr_temp1_max);
607 device_create_file(&new_client->dev, &dev_attr_temp1_max_hyst);
608 }
609 return 0;
610
611exit_free:
612 kfree(data);
613exit_release:
614 release_region(address, SIS5595_EXTENT);
615exit:
616 return err;
617}
618
619static int sis5595_detach_client(struct i2c_client *client)
620{
621 int err;
622
623 if ((err = i2c_detach_client(client))) {
624 dev_err(&client->dev,
625 "Client deregistration failed, client not detached.\n");
626 return err;
627 }
628
629 if (i2c_is_isa_client(client))
630 release_region(client->addr, SIS5595_EXTENT);
631
632 kfree(i2c_get_clientdata(client));
633
634 return 0;
635}
636
637
638/* ISA access must be locked explicitly. */
639static int sis5595_read_value(struct i2c_client *client, u8 reg)
640{
641 int res;
642
643 struct sis5595_data *data = i2c_get_clientdata(client);
644 down(&data->lock);
645 outb_p(reg, client->addr + SIS5595_ADDR_REG_OFFSET);
646 res = inb_p(client->addr + SIS5595_DATA_REG_OFFSET);
647 up(&data->lock);
648 return res;
649}
650
651static int sis5595_write_value(struct i2c_client *client, u8 reg, u8 value)
652{
653 struct sis5595_data *data = i2c_get_clientdata(client);
654 down(&data->lock);
655 outb_p(reg, client->addr + SIS5595_ADDR_REG_OFFSET);
656 outb_p(value, client->addr + SIS5595_DATA_REG_OFFSET);
657 up(&data->lock);
658 return 0;
659}
660
661/* Called when we have found a new SIS5595. */
662static void sis5595_init_client(struct i2c_client *client)
663{
664 u8 config = sis5595_read_value(client, SIS5595_REG_CONFIG);
665 if (!(config & 0x01))
666 sis5595_write_value(client, SIS5595_REG_CONFIG,
667 (config & 0xf7) | 0x01);
668}
669
670static struct sis5595_data *sis5595_update_device(struct device *dev)
671{
672 struct i2c_client *client = to_i2c_client(dev);
673 struct sis5595_data *data = i2c_get_clientdata(client);
674 int i;
675
676 down(&data->update_lock);
677
678 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
679 || !data->valid) {
680
681 for (i = 0; i <= data->maxins; i++) {
682 data->in[i] =
683 sis5595_read_value(client, SIS5595_REG_IN(i));
684 data->in_min[i] =
685 sis5595_read_value(client,
686 SIS5595_REG_IN_MIN(i));
687 data->in_max[i] =
688 sis5595_read_value(client,
689 SIS5595_REG_IN_MAX(i));
690 }
691 for (i = 0; i < 2; i++) {
692 data->fan[i] =
693 sis5595_read_value(client, SIS5595_REG_FAN(i));
694 data->fan_min[i] =
695 sis5595_read_value(client,
696 SIS5595_REG_FAN_MIN(i));
697 }
698 if (data->maxins == 3) {
699 data->temp =
700 sis5595_read_value(client, SIS5595_REG_TEMP);
701 data->temp_over =
702 sis5595_read_value(client, SIS5595_REG_TEMP_OVER);
703 data->temp_hyst =
704 sis5595_read_value(client, SIS5595_REG_TEMP_HYST);
705 }
706 i = sis5595_read_value(client, SIS5595_REG_FANDIV);
707 data->fan_div[0] = (i >> 4) & 0x03;
708 data->fan_div[1] = i >> 6;
709 data->alarms =
710 sis5595_read_value(client, SIS5595_REG_ALARM1) |
711 (sis5595_read_value(client, SIS5595_REG_ALARM2) << 8);
712 data->last_updated = jiffies;
713 data->valid = 1;
714 }
715
716 up(&data->update_lock);
717
718 return data;
719}
720
721static struct pci_device_id sis5595_pci_ids[] = {
722 { PCI_DEVICE(PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_503) },
723 { 0, }
724};
725
726MODULE_DEVICE_TABLE(pci, sis5595_pci_ids);
727
728static int blacklist[] __devinitdata = {
729 PCI_DEVICE_ID_SI_540,
730 PCI_DEVICE_ID_SI_550,
731 PCI_DEVICE_ID_SI_630,
732 PCI_DEVICE_ID_SI_645,
733 PCI_DEVICE_ID_SI_730,
734 PCI_DEVICE_ID_SI_735,
735 PCI_DEVICE_ID_SI_5511, /* 5513 chip has the 0008 device but
736 that ID shows up in other chips so we
737 use the 5511 ID for recognition */
738 PCI_DEVICE_ID_SI_5597,
739 PCI_DEVICE_ID_SI_5598,
740 0 };
741
742static int __devinit sis5595_pci_probe(struct pci_dev *dev,
743 const struct pci_device_id *id)
744{
745 u16 val;
746 int *i;
747 int addr = 0;
748
749 for (i = blacklist; *i != 0; i++) {
750 struct pci_dev *dev;
751 dev = pci_get_device(PCI_VENDOR_ID_SI, *i, NULL);
752 if (dev) {
753 dev_err(&dev->dev, "Looked for SIS5595 but found unsupported device %.4x\n", *i);
754 pci_dev_put(dev);
755 return -ENODEV;
756 }
757 }
758
759 if (PCIBIOS_SUCCESSFUL !=
760 pci_read_config_word(dev, SIS5595_BASE_REG, &val))
761 return -ENODEV;
762
763 addr = val & ~(SIS5595_EXTENT - 1);
764 if (addr == 0 && force_addr == 0) {
765 dev_err(&dev->dev, "Base address not set - upgrade BIOS or use force_addr=0xaddr\n");
766 return -ENODEV;
767 }
768 if (force_addr)
769 addr = force_addr; /* so detect will get called */
770
771 if (!addr) {
772 dev_err(&dev->dev,"No SiS 5595 sensors found.\n");
773 return -ENODEV;
774 }
775 normal_isa[0] = addr;
776
777 s_bridge = pci_dev_get(dev);
778 if (i2c_add_driver(&sis5595_driver)) {
779 pci_dev_put(s_bridge);
780 s_bridge = NULL;
781 }
782
783 /* Always return failure here. This is to allow other drivers to bind
784 * to this pci device. We don't really want to have control over the
785 * pci device, we only wanted to read as few register values from it.
786 */
787 return -ENODEV;
788}
789
790static struct pci_driver sis5595_pci_driver = {
791 .name = "sis5595",
792 .id_table = sis5595_pci_ids,
793 .probe = sis5595_pci_probe,
794};
795
796static int __init sm_sis5595_init(void)
797{
798 return pci_register_driver(&sis5595_pci_driver);
799}
800
801static void __exit sm_sis5595_exit(void)
802{
803 pci_unregister_driver(&sis5595_pci_driver);
804 if (s_bridge != NULL) {
805 i2c_del_driver(&sis5595_driver);
806 pci_dev_put(s_bridge);
807 s_bridge = NULL;
808 }
809}
810
811MODULE_AUTHOR("Aurelien Jarno <aurelien@aurel32.net>");
812MODULE_DESCRIPTION("SiS 5595 Sensor device");
813MODULE_LICENSE("GPL");
814
815module_init(sm_sis5595_init);
816module_exit(sm_sis5595_exit);
diff --git a/drivers/i2c/chips/smsc47b397.c b/drivers/i2c/chips/smsc47b397.c
new file mode 100644
index 000000000000..1119c76791d9
--- /dev/null
+++ b/drivers/i2c/chips/smsc47b397.c
@@ -0,0 +1,352 @@
1/*
2 smsc47b397.c - Part of lm_sensors, Linux kernel modules
3 for hardware monitoring
4
5 Supports the SMSC LPC47B397-NC Super-I/O chip.
6
7 Author/Maintainer: Mark M. Hoffman <mhoffman@lightlink.com>
8 Copyright (C) 2004 Utilitek Systems, Inc.
9
10 derived in part from smsc47m1.c:
11 Copyright (C) 2002 Mark D. Studebaker <mdsxyz123@yahoo.com>
12 Copyright (C) 2004 Jean Delvare <khali@linux-fr.org>
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 2 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27*/
28
29#include <linux/module.h>
30#include <linux/slab.h>
31#include <linux/ioport.h>
32#include <linux/jiffies.h>
33#include <linux/i2c.h>
34#include <linux/i2c-sensor.h>
35#include <linux/init.h>
36#include <asm/io.h>
37
38static unsigned short normal_i2c[] = { I2C_CLIENT_END };
39/* Address is autodetected, there is no default value */
40static unsigned int normal_isa[] = { 0x0000, I2C_CLIENT_ISA_END };
41static struct i2c_force_data forces[] = {{NULL}};
42
43enum chips { any_chip, smsc47b397 };
44static struct i2c_address_data addr_data = {
45 .normal_i2c = normal_i2c,
46 .normal_isa = normal_isa,
47 .probe = normal_i2c, /* cheat */
48 .ignore = normal_i2c, /* cheat */
49 .forces = forces,
50};
51
52/* Super-I/0 registers and commands */
53
54#define REG 0x2e /* The register to read/write */
55#define VAL 0x2f /* The value to read/write */
56
57static inline void superio_outb(int reg, int val)
58{
59 outb(reg, REG);
60 outb(val, VAL);
61}
62
63static inline int superio_inb(int reg)
64{
65 outb(reg, REG);
66 return inb(VAL);
67}
68
69/* select superio logical device */
70static inline void superio_select(int ld)
71{
72 superio_outb(0x07, ld);
73}
74
75static inline void superio_enter(void)
76{
77 outb(0x55, REG);
78}
79
80static inline void superio_exit(void)
81{
82 outb(0xAA, REG);
83}
84
85#define SUPERIO_REG_DEVID 0x20
86#define SUPERIO_REG_DEVREV 0x21
87#define SUPERIO_REG_BASE_MSB 0x60
88#define SUPERIO_REG_BASE_LSB 0x61
89#define SUPERIO_REG_LD8 0x08
90
91#define SMSC_EXTENT 0x02
92
93/* 0 <= nr <= 3 */
94static u8 smsc47b397_reg_temp[] = {0x25, 0x26, 0x27, 0x80};
95#define SMSC47B397_REG_TEMP(nr) (smsc47b397_reg_temp[(nr)])
96
97/* 0 <= nr <= 3 */
98#define SMSC47B397_REG_FAN_LSB(nr) (0x28 + 2 * (nr))
99#define SMSC47B397_REG_FAN_MSB(nr) (0x29 + 2 * (nr))
100
101struct smsc47b397_data {
102 struct i2c_client client;
103 struct semaphore lock;
104
105 struct semaphore update_lock;
106 unsigned long last_updated; /* in jiffies */
107 int valid;
108
109 /* register values */
110 u16 fan[4];
111 u8 temp[4];
112};
113
114static int smsc47b397_read_value(struct i2c_client *client, u8 reg)
115{
116 struct smsc47b397_data *data = i2c_get_clientdata(client);
117 int res;
118
119 down(&data->lock);
120 outb(reg, client->addr);
121 res = inb_p(client->addr + 1);
122 up(&data->lock);
123 return res;
124}
125
126static struct smsc47b397_data *smsc47b397_update_device(struct device *dev)
127{
128 struct i2c_client *client = to_i2c_client(dev);
129 struct smsc47b397_data *data = i2c_get_clientdata(client);
130 int i;
131
132 down(&data->update_lock);
133
134 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
135 dev_dbg(&client->dev, "starting device update...\n");
136
137 /* 4 temperature inputs, 4 fan inputs */
138 for (i = 0; i < 4; i++) {
139 data->temp[i] = smsc47b397_read_value(client,
140 SMSC47B397_REG_TEMP(i));
141
142 /* must read LSB first */
143 data->fan[i] = smsc47b397_read_value(client,
144 SMSC47B397_REG_FAN_LSB(i));
145 data->fan[i] |= smsc47b397_read_value(client,
146 SMSC47B397_REG_FAN_MSB(i)) << 8;
147 }
148
149 data->last_updated = jiffies;
150 data->valid = 1;
151
152 dev_dbg(&client->dev, "... device update complete\n");
153 }
154
155 up(&data->update_lock);
156
157 return data;
158}
159
160/* TEMP: 0.001C/bit (-128C to +127C)
161 REG: 1C/bit, two's complement */
162static int temp_from_reg(u8 reg)
163{
164 return (s8)reg * 1000;
165}
166
167/* 0 <= nr <= 3 */
168static ssize_t show_temp(struct device *dev, char *buf, int nr)
169{
170 struct smsc47b397_data *data = smsc47b397_update_device(dev);
171 return sprintf(buf, "%d\n", temp_from_reg(data->temp[nr]));
172}
173
174#define sysfs_temp(num) \
175static ssize_t show_temp##num(struct device *dev, char *buf) \
176{ \
177 return show_temp(dev, buf, num-1); \
178} \
179static DEVICE_ATTR(temp##num##_input, S_IRUGO, show_temp##num, NULL)
180
181sysfs_temp(1);
182sysfs_temp(2);
183sysfs_temp(3);
184sysfs_temp(4);
185
186#define device_create_file_temp(client, num) \
187 device_create_file(&client->dev, &dev_attr_temp##num##_input)
188
189/* FAN: 1 RPM/bit
190 REG: count of 90kHz pulses / revolution */
191static int fan_from_reg(u16 reg)
192{
193 return 90000 * 60 / reg;
194}
195
196/* 0 <= nr <= 3 */
197static ssize_t show_fan(struct device *dev, char *buf, int nr)
198{
199 struct smsc47b397_data *data = smsc47b397_update_device(dev);
200 return sprintf(buf, "%d\n", fan_from_reg(data->fan[nr]));
201}
202
203#define sysfs_fan(num) \
204static ssize_t show_fan##num(struct device *dev, char *buf) \
205{ \
206 return show_fan(dev, buf, num-1); \
207} \
208static DEVICE_ATTR(fan##num##_input, S_IRUGO, show_fan##num, NULL)
209
210sysfs_fan(1);
211sysfs_fan(2);
212sysfs_fan(3);
213sysfs_fan(4);
214
215#define device_create_file_fan(client, num) \
216 device_create_file(&client->dev, &dev_attr_fan##num##_input)
217
218static int smsc47b397_detect(struct i2c_adapter *adapter, int addr, int kind);
219
220static int smsc47b397_attach_adapter(struct i2c_adapter *adapter)
221{
222 if (!(adapter->class & I2C_CLASS_HWMON))
223 return 0;
224 return i2c_detect(adapter, &addr_data, smsc47b397_detect);
225}
226
227static int smsc47b397_detach_client(struct i2c_client *client)
228{
229 int err;
230
231 if ((err = i2c_detach_client(client))) {
232 dev_err(&client->dev, "Client deregistration failed, "
233 "client not detached.\n");
234 return err;
235 }
236
237 release_region(client->addr, SMSC_EXTENT);
238 kfree(i2c_get_clientdata(client));
239
240 return 0;
241}
242
243static struct i2c_driver smsc47b397_driver = {
244 .owner = THIS_MODULE,
245 .name = "smsc47b397",
246 .id = I2C_DRIVERID_SMSC47B397,
247 .flags = I2C_DF_NOTIFY,
248 .attach_adapter = smsc47b397_attach_adapter,
249 .detach_client = smsc47b397_detach_client,
250};
251
252static int smsc47b397_detect(struct i2c_adapter *adapter, int addr, int kind)
253{
254 struct i2c_client *new_client;
255 struct smsc47b397_data *data;
256 int err = 0;
257
258 if (!i2c_is_isa_adapter(adapter)) {
259 return 0;
260 }
261
262 if (!request_region(addr, SMSC_EXTENT, smsc47b397_driver.name)) {
263 dev_err(&adapter->dev, "Region 0x%x already in use!\n", addr);
264 return -EBUSY;
265 }
266
267 if (!(data = kmalloc(sizeof(struct smsc47b397_data), GFP_KERNEL))) {
268 err = -ENOMEM;
269 goto error_release;
270 }
271 memset(data, 0x00, sizeof(struct smsc47b397_data));
272
273 new_client = &data->client;
274 i2c_set_clientdata(new_client, data);
275 new_client->addr = addr;
276 init_MUTEX(&data->lock);
277 new_client->adapter = adapter;
278 new_client->driver = &smsc47b397_driver;
279 new_client->flags = 0;
280
281 strlcpy(new_client->name, "smsc47b397", I2C_NAME_SIZE);
282
283 init_MUTEX(&data->update_lock);
284
285 if ((err = i2c_attach_client(new_client)))
286 goto error_free;
287
288 device_create_file_temp(new_client, 1);
289 device_create_file_temp(new_client, 2);
290 device_create_file_temp(new_client, 3);
291 device_create_file_temp(new_client, 4);
292
293 device_create_file_fan(new_client, 1);
294 device_create_file_fan(new_client, 2);
295 device_create_file_fan(new_client, 3);
296 device_create_file_fan(new_client, 4);
297
298 return 0;
299
300error_free:
301 kfree(new_client);
302error_release:
303 release_region(addr, SMSC_EXTENT);
304 return err;
305}
306
307static int __init smsc47b397_find(unsigned int *addr)
308{
309 u8 id, rev;
310
311 superio_enter();
312 id = superio_inb(SUPERIO_REG_DEVID);
313
314 if (id != 0x6f) {
315 superio_exit();
316 return -ENODEV;
317 }
318
319 rev = superio_inb(SUPERIO_REG_DEVREV);
320
321 superio_select(SUPERIO_REG_LD8);
322 *addr = (superio_inb(SUPERIO_REG_BASE_MSB) << 8)
323 | superio_inb(SUPERIO_REG_BASE_LSB);
324
325 printk(KERN_INFO "smsc47b397: found SMSC LPC47B397-NC "
326 "(base address 0x%04x, revision %u)\n", *addr, rev);
327
328 superio_exit();
329 return 0;
330}
331
332static int __init smsc47b397_init(void)
333{
334 int ret;
335
336 if ((ret = smsc47b397_find(normal_isa)))
337 return ret;
338
339 return i2c_add_driver(&smsc47b397_driver);
340}
341
342static void __exit smsc47b397_exit(void)
343{
344 i2c_del_driver(&smsc47b397_driver);
345}
346
347MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
348MODULE_DESCRIPTION("SMSC LPC47B397 driver");
349MODULE_LICENSE("GPL");
350
351module_init(smsc47b397_init);
352module_exit(smsc47b397_exit);
diff --git a/drivers/i2c/chips/smsc47m1.c b/drivers/i2c/chips/smsc47m1.c
new file mode 100644
index 000000000000..0e12ca369413
--- /dev/null
+++ b/drivers/i2c/chips/smsc47m1.c
@@ -0,0 +1,591 @@
1/*
2 smsc47m1.c - Part of lm_sensors, Linux kernel modules
3 for hardware monitoring
4
5 Supports the SMSC LPC47B27x, LPC47M10x, LPC47M13x and LPC47M14x
6 Super-I/O chips.
7
8 Copyright (C) 2002 Mark D. Studebaker <mdsxyz123@yahoo.com>
9 Copyright (C) 2004 Jean Delvare <khali@linux-fr.org>
10 Ported to Linux 2.6 by Gabriele Gorla <gorlik@yahoo.com>
11 and Jean Delvare
12
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
17
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26*/
27
28#include <linux/module.h>
29#include <linux/slab.h>
30#include <linux/ioport.h>
31#include <linux/jiffies.h>
32#include <linux/i2c.h>
33#include <linux/i2c-sensor.h>
34#include <linux/init.h>
35#include <asm/io.h>
36
37static unsigned short normal_i2c[] = { I2C_CLIENT_END };
38/* Address is autodetected, there is no default value */
39static unsigned int normal_isa[] = { 0x0000, I2C_CLIENT_ISA_END };
40static struct i2c_force_data forces[] = {{NULL}};
41
42enum chips { any_chip, smsc47m1 };
43static struct i2c_address_data addr_data = {
44 .normal_i2c = normal_i2c,
45 .normal_isa = normal_isa,
46 .forces = forces,
47};
48
49/* Super-I/0 registers and commands */
50
51#define REG 0x2e /* The register to read/write */
52#define VAL 0x2f /* The value to read/write */
53
54static inline void
55superio_outb(int reg, int val)
56{
57 outb(reg, REG);
58 outb(val, VAL);
59}
60
61static inline int
62superio_inb(int reg)
63{
64 outb(reg, REG);
65 return inb(VAL);
66}
67
68/* logical device for fans is 0x0A */
69#define superio_select() superio_outb(0x07, 0x0A)
70
71static inline void
72superio_enter(void)
73{
74 outb(0x55, REG);
75}
76
77static inline void
78superio_exit(void)
79{
80 outb(0xAA, REG);
81}
82
83#define SUPERIO_REG_ACT 0x30
84#define SUPERIO_REG_BASE 0x60
85#define SUPERIO_REG_DEVID 0x20
86
87/* Logical device registers */
88
89#define SMSC_EXTENT 0x80
90
91/* nr is 0 or 1 in the macros below */
92#define SMSC47M1_REG_ALARM 0x04
93#define SMSC47M1_REG_TPIN(nr) (0x34 - (nr))
94#define SMSC47M1_REG_PPIN(nr) (0x36 - (nr))
95#define SMSC47M1_REG_PWM(nr) (0x56 + (nr))
96#define SMSC47M1_REG_FANDIV 0x58
97#define SMSC47M1_REG_FAN(nr) (0x59 + (nr))
98#define SMSC47M1_REG_FAN_PRELOAD(nr) (0x5B + (nr))
99
100#define MIN_FROM_REG(reg,div) ((reg)>=192 ? 0 : \
101 983040/((192-(reg))*(div)))
102#define FAN_FROM_REG(reg,div,preload) ((reg)<=(preload) || (reg)==255 ? 0 : \
103 983040/(((reg)-(preload))*(div)))
104#define DIV_FROM_REG(reg) (1 << (reg))
105#define PWM_FROM_REG(reg) (((reg) & 0x7E) << 1)
106#define PWM_EN_FROM_REG(reg) ((~(reg)) & 0x01)
107#define PWM_TO_REG(reg) (((reg) >> 1) & 0x7E)
108
109struct smsc47m1_data {
110 struct i2c_client client;
111 struct semaphore lock;
112
113 struct semaphore update_lock;
114 unsigned long last_updated; /* In jiffies */
115
116 u8 fan[2]; /* Register value */
117 u8 fan_preload[2]; /* Register value */
118 u8 fan_div[2]; /* Register encoding, shifted right */
119 u8 alarms; /* Register encoding */
120 u8 pwm[2]; /* Register value (bit 7 is enable) */
121};
122
123
124static int smsc47m1_attach_adapter(struct i2c_adapter *adapter);
125static int smsc47m1_find(int *address);
126static int smsc47m1_detect(struct i2c_adapter *adapter, int address, int kind);
127static int smsc47m1_detach_client(struct i2c_client *client);
128
129static int smsc47m1_read_value(struct i2c_client *client, u8 reg);
130static void smsc47m1_write_value(struct i2c_client *client, u8 reg, u8 value);
131
132static struct smsc47m1_data *smsc47m1_update_device(struct device *dev,
133 int init);
134
135
136static struct i2c_driver smsc47m1_driver = {
137 .owner = THIS_MODULE,
138 .name = "smsc47m1",
139 .id = I2C_DRIVERID_SMSC47M1,
140 .flags = I2C_DF_NOTIFY,
141 .attach_adapter = smsc47m1_attach_adapter,
142 .detach_client = smsc47m1_detach_client,
143};
144
145/* nr is 0 or 1 in the callback functions below */
146
147static ssize_t get_fan(struct device *dev, char *buf, int nr)
148{
149 struct smsc47m1_data *data = smsc47m1_update_device(dev, 0);
150 /* This chip (stupidly) stops monitoring fan speed if PWM is
151 enabled and duty cycle is 0%. This is fine if the monitoring
152 and control concern the same fan, but troublesome if they are
153 not (which could as well happen). */
154 int rpm = (data->pwm[nr] & 0x7F) == 0x00 ? 0 :
155 FAN_FROM_REG(data->fan[nr],
156 DIV_FROM_REG(data->fan_div[nr]),
157 data->fan_preload[nr]);
158 return sprintf(buf, "%d\n", rpm);
159}
160
161static ssize_t get_fan_min(struct device *dev, char *buf, int nr)
162{
163 struct smsc47m1_data *data = smsc47m1_update_device(dev, 0);
164 int rpm = MIN_FROM_REG(data->fan_preload[nr],
165 DIV_FROM_REG(data->fan_div[nr]));
166 return sprintf(buf, "%d\n", rpm);
167}
168
169static ssize_t get_fan_div(struct device *dev, char *buf, int nr)
170{
171 struct smsc47m1_data *data = smsc47m1_update_device(dev, 0);
172 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
173}
174
175static ssize_t get_pwm(struct device *dev, char *buf, int nr)
176{
177 struct smsc47m1_data *data = smsc47m1_update_device(dev, 0);
178 return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm[nr]));
179}
180
181static ssize_t get_pwm_en(struct device *dev, char *buf, int nr)
182{
183 struct smsc47m1_data *data = smsc47m1_update_device(dev, 0);
184 return sprintf(buf, "%d\n", PWM_EN_FROM_REG(data->pwm[nr]));
185}
186
187static ssize_t get_alarms(struct device *dev, char *buf)
188{
189 struct smsc47m1_data *data = smsc47m1_update_device(dev, 0);
190 return sprintf(buf, "%d\n", data->alarms);
191}
192
193static ssize_t set_fan_min(struct device *dev, const char *buf,
194 size_t count, int nr)
195{
196 struct i2c_client *client = to_i2c_client(dev);
197 struct smsc47m1_data *data = i2c_get_clientdata(client);
198 long rpmdiv, val = simple_strtol(buf, NULL, 10);
199
200 down(&data->update_lock);
201 rpmdiv = val * DIV_FROM_REG(data->fan_div[nr]);
202
203 if (983040 > 192 * rpmdiv || 2 * rpmdiv > 983040) {
204 up(&data->update_lock);
205 return -EINVAL;
206 }
207
208 data->fan_preload[nr] = 192 - ((983040 + rpmdiv / 2) / rpmdiv);
209 smsc47m1_write_value(client, SMSC47M1_REG_FAN_PRELOAD(nr),
210 data->fan_preload[nr]);
211 up(&data->update_lock);
212
213 return count;
214}
215
216/* Note: we save and restore the fan minimum here, because its value is
217 determined in part by the fan clock divider. This follows the principle
218 of least suprise; the user doesn't expect the fan minimum to change just
219 because the divider changed. */
220static ssize_t set_fan_div(struct device *dev, const char *buf,
221 size_t count, int nr)
222{
223 struct i2c_client *client = to_i2c_client(dev);
224 struct smsc47m1_data *data = i2c_get_clientdata(client);
225
226 long new_div = simple_strtol(buf, NULL, 10), tmp;
227 u8 old_div = DIV_FROM_REG(data->fan_div[nr]);
228
229 if (new_div == old_div) /* No change */
230 return count;
231
232 down(&data->update_lock);
233 switch (new_div) {
234 case 1: data->fan_div[nr] = 0; break;
235 case 2: data->fan_div[nr] = 1; break;
236 case 4: data->fan_div[nr] = 2; break;
237 case 8: data->fan_div[nr] = 3; break;
238 default:
239 up(&data->update_lock);
240 return -EINVAL;
241 }
242
243 tmp = smsc47m1_read_value(client, SMSC47M1_REG_FANDIV) & 0x0F;
244 tmp |= (data->fan_div[0] << 4) | (data->fan_div[1] << 6);
245 smsc47m1_write_value(client, SMSC47M1_REG_FANDIV, tmp);
246
247 /* Preserve fan min */
248 tmp = 192 - (old_div * (192 - data->fan_preload[nr])
249 + new_div / 2) / new_div;
250 data->fan_preload[nr] = SENSORS_LIMIT(tmp, 0, 191);
251 smsc47m1_write_value(client, SMSC47M1_REG_FAN_PRELOAD(nr),
252 data->fan_preload[nr]);
253 up(&data->update_lock);
254
255 return count;
256}
257
258static ssize_t set_pwm(struct device *dev, const char *buf,
259 size_t count, int nr)
260{
261 struct i2c_client *client = to_i2c_client(dev);
262 struct smsc47m1_data *data = i2c_get_clientdata(client);
263
264 long val = simple_strtol(buf, NULL, 10);
265
266 if (val < 0 || val > 255)
267 return -EINVAL;
268
269 down(&data->update_lock);
270 data->pwm[nr] &= 0x81; /* Preserve additional bits */
271 data->pwm[nr] |= PWM_TO_REG(val);
272 smsc47m1_write_value(client, SMSC47M1_REG_PWM(nr),
273 data->pwm[nr]);
274 up(&data->update_lock);
275
276 return count;
277}
278
279static ssize_t set_pwm_en(struct device *dev, const char *buf,
280 size_t count, int nr)
281{
282 struct i2c_client *client = to_i2c_client(dev);
283 struct smsc47m1_data *data = i2c_get_clientdata(client);
284
285 long val = simple_strtol(buf, NULL, 10);
286
287 if (val != 0 && val != 1)
288 return -EINVAL;
289
290 down(&data->update_lock);
291 data->pwm[nr] &= 0xFE; /* preserve the other bits */
292 data->pwm[nr] |= !val;
293 smsc47m1_write_value(client, SMSC47M1_REG_PWM(nr),
294 data->pwm[nr]);
295 up(&data->update_lock);
296
297 return count;
298}
299
300#define fan_present(offset) \
301static ssize_t get_fan##offset (struct device *dev, char *buf) \
302{ \
303 return get_fan(dev, buf, offset - 1); \
304} \
305static ssize_t get_fan##offset##_min (struct device *dev, char *buf) \
306{ \
307 return get_fan_min(dev, buf, offset - 1); \
308} \
309static ssize_t set_fan##offset##_min (struct device *dev, \
310 const char *buf, size_t count) \
311{ \
312 return set_fan_min(dev, buf, count, offset - 1); \
313} \
314static ssize_t get_fan##offset##_div (struct device *dev, char *buf) \
315{ \
316 return get_fan_div(dev, buf, offset - 1); \
317} \
318static ssize_t set_fan##offset##_div (struct device *dev, \
319 const char *buf, size_t count) \
320{ \
321 return set_fan_div(dev, buf, count, offset - 1); \
322} \
323static ssize_t get_pwm##offset (struct device *dev, char *buf) \
324{ \
325 return get_pwm(dev, buf, offset - 1); \
326} \
327static ssize_t set_pwm##offset (struct device *dev, \
328 const char *buf, size_t count) \
329{ \
330 return set_pwm(dev, buf, count, offset - 1); \
331} \
332static ssize_t get_pwm##offset##_en (struct device *dev, char *buf) \
333{ \
334 return get_pwm_en(dev, buf, offset - 1); \
335} \
336static ssize_t set_pwm##offset##_en (struct device *dev, \
337 const char *buf, size_t count) \
338{ \
339 return set_pwm_en(dev, buf, count, offset - 1); \
340} \
341static DEVICE_ATTR(fan##offset##_input, S_IRUGO, get_fan##offset, \
342 NULL); \
343static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
344 get_fan##offset##_min, set_fan##offset##_min); \
345static DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
346 get_fan##offset##_div, set_fan##offset##_div); \
347static DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
348 get_pwm##offset, set_pwm##offset); \
349static DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
350 get_pwm##offset##_en, set_pwm##offset##_en);
351
352fan_present(1);
353fan_present(2);
354
355static DEVICE_ATTR(alarms, S_IRUGO, get_alarms, NULL);
356
357static int smsc47m1_attach_adapter(struct i2c_adapter *adapter)
358{
359 if (!(adapter->class & I2C_CLASS_HWMON))
360 return 0;
361 return i2c_detect(adapter, &addr_data, smsc47m1_detect);
362}
363
364static int smsc47m1_find(int *address)
365{
366 u8 val;
367
368 superio_enter();
369 val = superio_inb(SUPERIO_REG_DEVID);
370
371 /*
372 * SMSC LPC47M10x/LPC47M13x (device id 0x59), LPC47M14x (device id
373 * 0x5F) and LPC47B27x (device id 0x51) have fan control.
374 * The LPC47M15x and LPC47M192 chips "with hardware monitoring block"
375 * can do much more besides (device id 0x60, unsupported).
376 */
377 if (val == 0x51)
378 printk(KERN_INFO "smsc47m1: Found SMSC47B27x\n");
379 else if (val == 0x59)
380 printk(KERN_INFO "smsc47m1: Found SMSC47M10x/SMSC47M13x\n");
381 else if (val == 0x5F)
382 printk(KERN_INFO "smsc47m1: Found SMSC47M14x\n");
383 else {
384 superio_exit();
385 return -ENODEV;
386 }
387
388 superio_select();
389 *address = (superio_inb(SUPERIO_REG_BASE) << 8)
390 | superio_inb(SUPERIO_REG_BASE + 1);
391 val = superio_inb(SUPERIO_REG_ACT);
392 if (*address == 0 || (val & 0x01) == 0) {
393 printk(KERN_INFO "smsc47m1: Device is disabled, will not use\n");
394 superio_exit();
395 return -ENODEV;
396 }
397
398 superio_exit();
399 return 0;
400}
401
402static int smsc47m1_detect(struct i2c_adapter *adapter, int address, int kind)
403{
404 struct i2c_client *new_client;
405 struct smsc47m1_data *data;
406 int err = 0;
407 int fan1, fan2, pwm1, pwm2;
408
409 if (!i2c_is_isa_adapter(adapter)) {
410 return 0;
411 }
412
413 if (!request_region(address, SMSC_EXTENT, smsc47m1_driver.name)) {
414 dev_err(&adapter->dev, "Region 0x%x already in use!\n", address);
415 return -EBUSY;
416 }
417
418 if (!(data = kmalloc(sizeof(struct smsc47m1_data), GFP_KERNEL))) {
419 err = -ENOMEM;
420 goto error_release;
421 }
422 memset(data, 0x00, sizeof(struct smsc47m1_data));
423
424 new_client = &data->client;
425 i2c_set_clientdata(new_client, data);
426 new_client->addr = address;
427 init_MUTEX(&data->lock);
428 new_client->adapter = adapter;
429 new_client->driver = &smsc47m1_driver;
430 new_client->flags = 0;
431
432 strlcpy(new_client->name, "smsc47m1", I2C_NAME_SIZE);
433 init_MUTEX(&data->update_lock);
434
435 /* If no function is properly configured, there's no point in
436 actually registering the chip. */
437 fan1 = (smsc47m1_read_value(new_client, SMSC47M1_REG_TPIN(0)) & 0x05)
438 == 0x05;
439 fan2 = (smsc47m1_read_value(new_client, SMSC47M1_REG_TPIN(1)) & 0x05)
440 == 0x05;
441 pwm1 = (smsc47m1_read_value(new_client, SMSC47M1_REG_PPIN(0)) & 0x05)
442 == 0x04;
443 pwm2 = (smsc47m1_read_value(new_client, SMSC47M1_REG_PPIN(1)) & 0x05)
444 == 0x04;
445 if (!(fan1 || fan2 || pwm1 || pwm2)) {
446 dev_warn(&new_client->dev, "Device is not configured, will not use\n");
447 err = -ENODEV;
448 goto error_free;
449 }
450
451 if ((err = i2c_attach_client(new_client)))
452 goto error_free;
453
454 /* Some values (fan min, clock dividers, pwm registers) may be
455 needed before any update is triggered, so we better read them
456 at least once here. We don't usually do it that way, but in
457 this particular case, manually reading 5 registers out of 8
458 doesn't make much sense and we're better using the existing
459 function. */
460 smsc47m1_update_device(&new_client->dev, 1);
461
462 if (fan1) {
463 device_create_file(&new_client->dev, &dev_attr_fan1_input);
464 device_create_file(&new_client->dev, &dev_attr_fan1_min);
465 device_create_file(&new_client->dev, &dev_attr_fan1_div);
466 } else
467 dev_dbg(&new_client->dev, "Fan 1 not enabled by hardware, "
468 "skipping\n");
469
470 if (fan2) {
471 device_create_file(&new_client->dev, &dev_attr_fan2_input);
472 device_create_file(&new_client->dev, &dev_attr_fan2_min);
473 device_create_file(&new_client->dev, &dev_attr_fan2_div);
474 } else
475 dev_dbg(&new_client->dev, "Fan 2 not enabled by hardware, "
476 "skipping\n");
477
478 if (pwm1) {
479 device_create_file(&new_client->dev, &dev_attr_pwm1);
480 device_create_file(&new_client->dev, &dev_attr_pwm1_enable);
481 } else
482 dev_dbg(&new_client->dev, "PWM 1 not enabled by hardware, "
483 "skipping\n");
484 if (pwm2) {
485 device_create_file(&new_client->dev, &dev_attr_pwm2);
486 device_create_file(&new_client->dev, &dev_attr_pwm2_enable);
487 } else
488 dev_dbg(&new_client->dev, "PWM 2 not enabled by hardware, "
489 "skipping\n");
490
491 device_create_file(&new_client->dev, &dev_attr_alarms);
492
493 return 0;
494
495error_free:
496 kfree(new_client);
497error_release:
498 release_region(address, SMSC_EXTENT);
499 return err;
500}
501
502static int smsc47m1_detach_client(struct i2c_client *client)
503{
504 int err;
505
506 if ((err = i2c_detach_client(client))) {
507 dev_err(&client->dev, "Client deregistration failed, "
508 "client not detached.\n");
509 return err;
510 }
511
512 release_region(client->addr, SMSC_EXTENT);
513 kfree(i2c_get_clientdata(client));
514
515 return 0;
516}
517
518static int smsc47m1_read_value(struct i2c_client *client, u8 reg)
519{
520 int res;
521
522 down(&((struct smsc47m1_data *) i2c_get_clientdata(client))->lock);
523 res = inb_p(client->addr + reg);
524 up(&((struct smsc47m1_data *) i2c_get_clientdata(client))->lock);
525 return res;
526}
527
528static void smsc47m1_write_value(struct i2c_client *client, u8 reg, u8 value)
529{
530 down(&((struct smsc47m1_data *) i2c_get_clientdata(client))->lock);
531 outb_p(value, client->addr + reg);
532 up(&((struct smsc47m1_data *) i2c_get_clientdata(client))->lock);
533}
534
535static struct smsc47m1_data *smsc47m1_update_device(struct device *dev,
536 int init)
537{
538 struct i2c_client *client = to_i2c_client(dev);
539 struct smsc47m1_data *data = i2c_get_clientdata(client);
540
541 down(&data->update_lock);
542
543 if (time_after(jiffies, data->last_updated + HZ + HZ / 2) || init) {
544 int i;
545
546 for (i = 0; i < 2; i++) {
547 data->fan[i] = smsc47m1_read_value(client,
548 SMSC47M1_REG_FAN(i));
549 data->fan_preload[i] = smsc47m1_read_value(client,
550 SMSC47M1_REG_FAN_PRELOAD(i));
551 data->pwm[i] = smsc47m1_read_value(client,
552 SMSC47M1_REG_PWM(i));
553 }
554
555 i = smsc47m1_read_value(client, SMSC47M1_REG_FANDIV);
556 data->fan_div[0] = (i >> 4) & 0x03;
557 data->fan_div[1] = i >> 6;
558
559 data->alarms = smsc47m1_read_value(client,
560 SMSC47M1_REG_ALARM) >> 6;
561 /* Clear alarms if needed */
562 if (data->alarms)
563 smsc47m1_write_value(client, SMSC47M1_REG_ALARM, 0xC0);
564
565 data->last_updated = jiffies;
566 }
567
568 up(&data->update_lock);
569 return data;
570}
571
572static int __init sm_smsc47m1_init(void)
573{
574 if (smsc47m1_find(normal_isa)) {
575 return -ENODEV;
576 }
577
578 return i2c_add_driver(&smsc47m1_driver);
579}
580
581static void __exit sm_smsc47m1_exit(void)
582{
583 i2c_del_driver(&smsc47m1_driver);
584}
585
586MODULE_AUTHOR("Mark D. Studebaker <mdsxyz123@yahoo.com>");
587MODULE_DESCRIPTION("SMSC LPC47M1xx fan sensors driver");
588MODULE_LICENSE("GPL");
589
590module_init(sm_smsc47m1_init);
591module_exit(sm_smsc47m1_exit);
diff --git a/drivers/i2c/chips/via686a.c b/drivers/i2c/chips/via686a.c
new file mode 100644
index 000000000000..9b948f4531f5
--- /dev/null
+++ b/drivers/i2c/chips/via686a.c
@@ -0,0 +1,879 @@
1/*
2 via686a.c - Part of lm_sensors, Linux kernel modules
3 for hardware monitoring
4
5 Copyright (c) 1998 - 2002 Frodo Looijaard <frodol@dds.nl>,
6 Kyösti Mälkki <kmalkki@cc.hut.fi>,
7 Mark Studebaker <mdsxyz123@yahoo.com>,
8 and Bob Dougherty <bobd@stanford.edu>
9 (Some conversion-factor data were contributed by Jonathan Teh Soon Yew
10 <j.teh@iname.com> and Alex van Kaam <darkside@chello.nl>.)
11
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25*/
26
27/*
28 Supports the Via VT82C686A, VT82C686B south bridges.
29 Reports all as a 686A.
30 Warning - only supports a single device.
31*/
32
33#include <linux/config.h>
34#include <linux/module.h>
35#include <linux/slab.h>
36#include <linux/pci.h>
37#include <linux/delay.h>
38#include <linux/jiffies.h>
39#include <linux/i2c.h>
40#include <linux/i2c-sensor.h>
41#include <linux/init.h>
42#include <asm/io.h>
43
44
45/* If force_addr is set to anything different from 0, we forcibly enable
46 the device at the given address. */
47static unsigned short force_addr = 0;
48module_param(force_addr, ushort, 0);
49MODULE_PARM_DESC(force_addr,
50 "Initialize the base address of the sensors");
51
52/* Addresses to scan.
53 Note that we can't determine the ISA address until we have initialized
54 our module */
55static unsigned short normal_i2c[] = { I2C_CLIENT_END };
56static unsigned int normal_isa[] = { 0x0000, I2C_CLIENT_ISA_END };
57
58/* Insmod parameters */
59SENSORS_INSMOD_1(via686a);
60
61/*
62 The Via 686a southbridge has a LM78-like chip integrated on the same IC.
63 This driver is a customized copy of lm78.c
64*/
65
66/* Many VIA686A constants specified below */
67
68/* Length of ISA address segment */
69#define VIA686A_EXTENT 0x80
70#define VIA686A_BASE_REG 0x70
71#define VIA686A_ENABLE_REG 0x74
72
73/* The VIA686A registers */
74/* ins numbered 0-4 */
75#define VIA686A_REG_IN_MAX(nr) (0x2b + ((nr) * 2))
76#define VIA686A_REG_IN_MIN(nr) (0x2c + ((nr) * 2))
77#define VIA686A_REG_IN(nr) (0x22 + (nr))
78
79/* fans numbered 1-2 */
80#define VIA686A_REG_FAN_MIN(nr) (0x3a + (nr))
81#define VIA686A_REG_FAN(nr) (0x28 + (nr))
82
83/* the following values are as speced by VIA: */
84static const u8 regtemp[] = { 0x20, 0x21, 0x1f };
85static const u8 regover[] = { 0x39, 0x3d, 0x1d };
86static const u8 reghyst[] = { 0x3a, 0x3e, 0x1e };
87
88/* temps numbered 1-3 */
89#define VIA686A_REG_TEMP(nr) (regtemp[nr])
90#define VIA686A_REG_TEMP_OVER(nr) (regover[nr])
91#define VIA686A_REG_TEMP_HYST(nr) (reghyst[nr])
92#define VIA686A_REG_TEMP_LOW1 0x4b // bits 7-6
93#define VIA686A_REG_TEMP_LOW23 0x49 // 2 = bits 5-4, 3 = bits 7-6
94
95#define VIA686A_REG_ALARM1 0x41
96#define VIA686A_REG_ALARM2 0x42
97#define VIA686A_REG_FANDIV 0x47
98#define VIA686A_REG_CONFIG 0x40
99/* The following register sets temp interrupt mode (bits 1-0 for temp1,
100 3-2 for temp2, 5-4 for temp3). Modes are:
101 00 interrupt stays as long as value is out-of-range
102 01 interrupt is cleared once register is read (default)
103 10 comparator mode- like 00, but ignores hysteresis
104 11 same as 00 */
105#define VIA686A_REG_TEMP_MODE 0x4b
106/* We'll just assume that you want to set all 3 simultaneously: */
107#define VIA686A_TEMP_MODE_MASK 0x3F
108#define VIA686A_TEMP_MODE_CONTINUOUS (0x00)
109
110/* Conversions. Limit checking is only done on the TO_REG
111 variants.
112
113********* VOLTAGE CONVERSIONS (Bob Dougherty) ********
114 From HWMon.cpp (Copyright 1998-2000 Jonathan Teh Soon Yew):
115 voltagefactor[0]=1.25/2628; (2628/1.25=2102.4) // Vccp
116 voltagefactor[1]=1.25/2628; (2628/1.25=2102.4) // +2.5V
117 voltagefactor[2]=1.67/2628; (2628/1.67=1573.7) // +3.3V
118 voltagefactor[3]=2.6/2628; (2628/2.60=1010.8) // +5V
119 voltagefactor[4]=6.3/2628; (2628/6.30=417.14) // +12V
120 in[i]=(data[i+2]*25.0+133)*voltagefactor[i];
121 That is:
122 volts = (25*regVal+133)*factor
123 regVal = (volts/factor-133)/25
124 (These conversions were contributed by Jonathan Teh Soon Yew
125 <j.teh@iname.com>) */
126static inline u8 IN_TO_REG(long val, int inNum)
127{
128 /* To avoid floating point, we multiply constants by 10 (100 for +12V).
129 Rounding is done (120500 is actually 133000 - 12500).
130 Remember that val is expressed in 0.001V/bit, which is why we divide
131 by an additional 10000 (100000 for +12V): 1000 for val and 10 (100)
132 for the constants. */
133 if (inNum <= 1)
134 return (u8)
135 SENSORS_LIMIT((val * 21024 - 1205000) / 250000, 0, 255);
136 else if (inNum == 2)
137 return (u8)
138 SENSORS_LIMIT((val * 15737 - 1205000) / 250000, 0, 255);
139 else if (inNum == 3)
140 return (u8)
141 SENSORS_LIMIT((val * 10108 - 1205000) / 250000, 0, 255);
142 else
143 return (u8)
144 SENSORS_LIMIT((val * 41714 - 12050000) / 2500000, 0, 255);
145}
146
147static inline long IN_FROM_REG(u8 val, int inNum)
148{
149 /* To avoid floating point, we multiply constants by 10 (100 for +12V).
150 We also multiply them by 1000 because we want 0.001V/bit for the
151 output value. Rounding is done. */
152 if (inNum <= 1)
153 return (long) ((250000 * val + 1330000 + 21024 / 2) / 21024);
154 else if (inNum == 2)
155 return (long) ((250000 * val + 1330000 + 15737 / 2) / 15737);
156 else if (inNum == 3)
157 return (long) ((250000 * val + 1330000 + 10108 / 2) / 10108);
158 else
159 return (long) ((2500000 * val + 13300000 + 41714 / 2) / 41714);
160}
161
162/********* FAN RPM CONVERSIONS ********/
163/* Higher register values = slower fans (the fan's strobe gates a counter).
164 But this chip saturates back at 0, not at 255 like all the other chips.
165 So, 0 means 0 RPM */
166static inline u8 FAN_TO_REG(long rpm, int div)
167{
168 if (rpm == 0)
169 return 0;
170 rpm = SENSORS_LIMIT(rpm, 1, 1000000);
171 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1, 255);
172}
173
174#define FAN_FROM_REG(val,div) ((val)==0?0:(val)==255?0:1350000/((val)*(div)))
175
176/******** TEMP CONVERSIONS (Bob Dougherty) *********/
177/* linear fits from HWMon.cpp (Copyright 1998-2000 Jonathan Teh Soon Yew)
178 if(temp<169)
179 return double(temp)*0.427-32.08;
180 else if(temp>=169 && temp<=202)
181 return double(temp)*0.582-58.16;
182 else
183 return double(temp)*0.924-127.33;
184
185 A fifth-order polynomial fits the unofficial data (provided by Alex van
186 Kaam <darkside@chello.nl>) a bit better. It also give more reasonable
187 numbers on my machine (ie. they agree with what my BIOS tells me).
188 Here's the fifth-order fit to the 8-bit data:
189 temp = 1.625093e-10*val^5 - 1.001632e-07*val^4 + 2.457653e-05*val^3 -
190 2.967619e-03*val^2 + 2.175144e-01*val - 7.090067e+0.
191
192 (2000-10-25- RFD: thanks to Uwe Andersen <uandersen@mayah.com> for
193 finding my typos in this formula!)
194
195 Alas, none of the elegant function-fit solutions will work because we
196 aren't allowed to use floating point in the kernel and doing it with
197 integers doesn't rpovide enough precision. So we'll do boring old
198 look-up table stuff. The unofficial data (see below) have effectively
199 7-bit resolution (they are rounded to the nearest degree). I'm assuming
200 that the transfer function of the device is monotonic and smooth, so a
201 smooth function fit to the data will allow us to get better precision.
202 I used the 5th-order poly fit described above and solved for
203 VIA register values 0-255. I *10 before rounding, so we get tenth-degree
204 precision. (I could have done all 1024 values for our 10-bit readings,
205 but the function is very linear in the useful range (0-80 deg C), so
206 we'll just use linear interpolation for 10-bit readings.) So, tempLUT
207 is the temp at via register values 0-255: */
208static const long tempLUT[] =
209 { -709, -688, -667, -646, -627, -607, -589, -570, -553, -536, -519,
210 -503, -487, -471, -456, -442, -428, -414, -400, -387, -375,
211 -362, -350, -339, -327, -316, -305, -295, -285, -275, -265,
212 -255, -246, -237, -229, -220, -212, -204, -196, -188, -180,
213 -173, -166, -159, -152, -145, -139, -132, -126, -120, -114,
214 -108, -102, -96, -91, -85, -80, -74, -69, -64, -59, -54, -49,
215 -44, -39, -34, -29, -25, -20, -15, -11, -6, -2, 3, 7, 12, 16,
216 20, 25, 29, 33, 37, 42, 46, 50, 54, 59, 63, 67, 71, 75, 79, 84,
217 88, 92, 96, 100, 104, 109, 113, 117, 121, 125, 130, 134, 138,
218 142, 146, 151, 155, 159, 163, 168, 172, 176, 181, 185, 189,
219 193, 198, 202, 206, 211, 215, 219, 224, 228, 232, 237, 241,
220 245, 250, 254, 259, 263, 267, 272, 276, 281, 285, 290, 294,
221 299, 303, 307, 312, 316, 321, 325, 330, 334, 339, 344, 348,
222 353, 357, 362, 366, 371, 376, 380, 385, 390, 395, 399, 404,
223 409, 414, 419, 423, 428, 433, 438, 443, 449, 454, 459, 464,
224 469, 475, 480, 486, 491, 497, 502, 508, 514, 520, 526, 532,
225 538, 544, 551, 557, 564, 571, 578, 584, 592, 599, 606, 614,
226 621, 629, 637, 645, 654, 662, 671, 680, 689, 698, 708, 718,
227 728, 738, 749, 759, 770, 782, 793, 805, 818, 830, 843, 856,
228 870, 883, 898, 912, 927, 943, 958, 975, 991, 1008, 1026, 1044,
229 1062, 1081, 1101, 1121, 1141, 1162, 1184, 1206, 1229, 1252,
230 1276, 1301, 1326, 1352, 1378, 1406, 1434, 1462
231};
232
233/* the original LUT values from Alex van Kaam <darkside@chello.nl>
234 (for via register values 12-240):
235{-50,-49,-47,-45,-43,-41,-39,-38,-37,-35,-34,-33,-32,-31,
236-30,-29,-28,-27,-26,-25,-24,-24,-23,-22,-21,-20,-20,-19,-18,-17,-17,-16,-15,
237-15,-14,-14,-13,-12,-12,-11,-11,-10,-9,-9,-8,-8,-7,-7,-6,-6,-5,-5,-4,-4,-3,
238-3,-2,-2,-1,-1,0,0,1,1,1,3,3,3,4,4,4,5,5,5,6,6,7,7,8,8,9,9,9,10,10,11,11,12,
23912,12,13,13,13,14,14,15,15,16,16,16,17,17,18,18,19,19,20,20,21,21,21,22,22,
24022,23,23,24,24,25,25,26,26,26,27,27,27,28,28,29,29,30,30,30,31,31,32,32,33,
24133,34,34,35,35,35,36,36,37,37,38,38,39,39,40,40,41,41,42,42,43,43,44,44,45,
24245,46,46,47,48,48,49,49,50,51,51,52,52,53,53,54,55,55,56,57,57,58,59,59,60,
24361,62,62,63,64,65,66,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,83,84,
24485,86,88,89,91,92,94,96,97,99,101,103,105,107,109,110};
245
246
247 Here's the reverse LUT. I got it by doing a 6-th order poly fit (needed
248 an extra term for a good fit to these inverse data!) and then
249 solving for each temp value from -50 to 110 (the useable range for
250 this chip). Here's the fit:
251 viaRegVal = -1.160370e-10*val^6 +3.193693e-08*val^5 - 1.464447e-06*val^4
252 - 2.525453e-04*val^3 + 1.424593e-02*val^2 + 2.148941e+00*val +7.275808e+01)
253 Note that n=161: */
254static const u8 viaLUT[] =
255 { 12, 12, 13, 14, 14, 15, 16, 16, 17, 18, 18, 19, 20, 20, 21, 22, 23,
256 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 35, 36, 37, 39, 40,
257 41, 43, 45, 46, 48, 49, 51, 53, 55, 57, 59, 60, 62, 64, 66,
258 69, 71, 73, 75, 77, 79, 82, 84, 86, 88, 91, 93, 95, 98, 100,
259 103, 105, 107, 110, 112, 115, 117, 119, 122, 124, 126, 129,
260 131, 134, 136, 138, 140, 143, 145, 147, 150, 152, 154, 156,
261 158, 160, 162, 164, 166, 168, 170, 172, 174, 176, 178, 180,
262 182, 183, 185, 187, 188, 190, 192, 193, 195, 196, 198, 199,
263 200, 202, 203, 205, 206, 207, 208, 209, 210, 211, 212, 213,
264 214, 215, 216, 217, 218, 219, 220, 221, 222, 222, 223, 224,
265 225, 226, 226, 227, 228, 228, 229, 230, 230, 231, 232, 232,
266 233, 233, 234, 235, 235, 236, 236, 237, 237, 238, 238, 239,
267 239, 240
268};
269
270/* Converting temps to (8-bit) hyst and over registers
271 No interpolation here.
272 The +50 is because the temps start at -50 */
273static inline u8 TEMP_TO_REG(long val)
274{
275 return viaLUT[val <= -50000 ? 0 : val >= 110000 ? 160 :
276 (val < 0 ? val - 500 : val + 500) / 1000 + 50];
277}
278
279/* for 8-bit temperature hyst and over registers */
280#define TEMP_FROM_REG(val) (tempLUT[(val)] * 100)
281
282/* for 10-bit temperature readings */
283static inline long TEMP_FROM_REG10(u16 val)
284{
285 u16 eightBits = val >> 2;
286 u16 twoBits = val & 3;
287
288 /* no interpolation for these */
289 if (twoBits == 0 || eightBits == 255)
290 return TEMP_FROM_REG(eightBits);
291
292 /* do some linear interpolation */
293 return (tempLUT[eightBits] * (4 - twoBits) +
294 tempLUT[eightBits + 1] * twoBits) * 25;
295}
296
297#define ALARMS_FROM_REG(val) (val)
298
299#define DIV_FROM_REG(val) (1 << (val))
300#define DIV_TO_REG(val) ((val)==8?3:(val)==4?2:(val)==1?0:1)
301
302/* For the VIA686A, we need to keep some data in memory.
303 The structure is dynamically allocated, at the same time when a new
304 via686a client is allocated. */
305struct via686a_data {
306 struct i2c_client client;
307 struct semaphore update_lock;
308 char valid; /* !=0 if following fields are valid */
309 unsigned long last_updated; /* In jiffies */
310
311 u8 in[5]; /* Register value */
312 u8 in_max[5]; /* Register value */
313 u8 in_min[5]; /* Register value */
314 u8 fan[2]; /* Register value */
315 u8 fan_min[2]; /* Register value */
316 u16 temp[3]; /* Register value 10 bit */
317 u8 temp_over[3]; /* Register value */
318 u8 temp_hyst[3]; /* Register value */
319 u8 fan_div[2]; /* Register encoding, shifted right */
320 u16 alarms; /* Register encoding, combined */
321};
322
323static struct pci_dev *s_bridge; /* pointer to the (only) via686a */
324
325static int via686a_attach_adapter(struct i2c_adapter *adapter);
326static int via686a_detect(struct i2c_adapter *adapter, int address, int kind);
327static int via686a_detach_client(struct i2c_client *client);
328
329static inline int via686a_read_value(struct i2c_client *client, u8 reg)
330{
331 return (inb_p(client->addr + reg));
332}
333
334static inline void via686a_write_value(struct i2c_client *client, u8 reg,
335 u8 value)
336{
337 outb_p(value, client->addr + reg);
338}
339
340static struct via686a_data *via686a_update_device(struct device *dev);
341static void via686a_init_client(struct i2c_client *client);
342
343/* following are the sysfs callback functions */
344
345/* 7 voltage sensors */
346static ssize_t show_in(struct device *dev, char *buf, int nr) {
347 struct via686a_data *data = via686a_update_device(dev);
348 return sprintf(buf, "%ld\n", IN_FROM_REG(data->in[nr], nr));
349}
350
351static ssize_t show_in_min(struct device *dev, char *buf, int nr) {
352 struct via686a_data *data = via686a_update_device(dev);
353 return sprintf(buf, "%ld\n", IN_FROM_REG(data->in_min[nr], nr));
354}
355
356static ssize_t show_in_max(struct device *dev, char *buf, int nr) {
357 struct via686a_data *data = via686a_update_device(dev);
358 return sprintf(buf, "%ld\n", IN_FROM_REG(data->in_max[nr], nr));
359}
360
361static ssize_t set_in_min(struct device *dev, const char *buf,
362 size_t count, int nr) {
363 struct i2c_client *client = to_i2c_client(dev);
364 struct via686a_data *data = i2c_get_clientdata(client);
365 unsigned long val = simple_strtoul(buf, NULL, 10);
366
367 down(&data->update_lock);
368 data->in_min[nr] = IN_TO_REG(val,nr);
369 via686a_write_value(client, VIA686A_REG_IN_MIN(nr),
370 data->in_min[nr]);
371 up(&data->update_lock);
372 return count;
373}
374static ssize_t set_in_max(struct device *dev, const char *buf,
375 size_t count, int nr) {
376 struct i2c_client *client = to_i2c_client(dev);
377 struct via686a_data *data = i2c_get_clientdata(client);
378 unsigned long val = simple_strtoul(buf, NULL, 10);
379
380 down(&data->update_lock);
381 data->in_max[nr] = IN_TO_REG(val,nr);
382 via686a_write_value(client, VIA686A_REG_IN_MAX(nr),
383 data->in_max[nr]);
384 up(&data->update_lock);
385 return count;
386}
387#define show_in_offset(offset) \
388static ssize_t \
389 show_in##offset (struct device *dev, char *buf) \
390{ \
391 return show_in(dev, buf, offset); \
392} \
393static ssize_t \
394 show_in##offset##_min (struct device *dev, char *buf) \
395{ \
396 return show_in_min(dev, buf, offset); \
397} \
398static ssize_t \
399 show_in##offset##_max (struct device *dev, char *buf) \
400{ \
401 return show_in_max(dev, buf, offset); \
402} \
403static ssize_t set_in##offset##_min (struct device *dev, \
404 const char *buf, size_t count) \
405{ \
406 return set_in_min(dev, buf, count, offset); \
407} \
408static ssize_t set_in##offset##_max (struct device *dev, \
409 const char *buf, size_t count) \
410{ \
411 return set_in_max(dev, buf, count, offset); \
412} \
413static DEVICE_ATTR(in##offset##_input, S_IRUGO, show_in##offset, NULL);\
414static DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
415 show_in##offset##_min, set_in##offset##_min); \
416static DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
417 show_in##offset##_max, set_in##offset##_max);
418
419show_in_offset(0);
420show_in_offset(1);
421show_in_offset(2);
422show_in_offset(3);
423show_in_offset(4);
424
425/* 3 temperatures */
426static ssize_t show_temp(struct device *dev, char *buf, int nr) {
427 struct via686a_data *data = via686a_update_device(dev);
428 return sprintf(buf, "%ld\n", TEMP_FROM_REG10(data->temp[nr]));
429}
430static ssize_t show_temp_over(struct device *dev, char *buf, int nr) {
431 struct via686a_data *data = via686a_update_device(dev);
432 return sprintf(buf, "%ld\n", TEMP_FROM_REG(data->temp_over[nr]));
433}
434static ssize_t show_temp_hyst(struct device *dev, char *buf, int nr) {
435 struct via686a_data *data = via686a_update_device(dev);
436 return sprintf(buf, "%ld\n", TEMP_FROM_REG(data->temp_hyst[nr]));
437}
438static ssize_t set_temp_over(struct device *dev, const char *buf,
439 size_t count, int nr) {
440 struct i2c_client *client = to_i2c_client(dev);
441 struct via686a_data *data = i2c_get_clientdata(client);
442 int val = simple_strtol(buf, NULL, 10);
443
444 down(&data->update_lock);
445 data->temp_over[nr] = TEMP_TO_REG(val);
446 via686a_write_value(client, VIA686A_REG_TEMP_OVER(nr), data->temp_over[nr]);
447 up(&data->update_lock);
448 return count;
449}
450static ssize_t set_temp_hyst(struct device *dev, const char *buf,
451 size_t count, int nr) {
452 struct i2c_client *client = to_i2c_client(dev);
453 struct via686a_data *data = i2c_get_clientdata(client);
454 int val = simple_strtol(buf, NULL, 10);
455
456 down(&data->update_lock);
457 data->temp_hyst[nr] = TEMP_TO_REG(val);
458 via686a_write_value(client, VIA686A_REG_TEMP_HYST(nr), data->temp_hyst[nr]);
459 up(&data->update_lock);
460 return count;
461}
462#define show_temp_offset(offset) \
463static ssize_t show_temp_##offset (struct device *dev, char *buf) \
464{ \
465 return show_temp(dev, buf, offset - 1); \
466} \
467static ssize_t \
468show_temp_##offset##_over (struct device *dev, char *buf) \
469{ \
470 return show_temp_over(dev, buf, offset - 1); \
471} \
472static ssize_t \
473show_temp_##offset##_hyst (struct device *dev, char *buf) \
474{ \
475 return show_temp_hyst(dev, buf, offset - 1); \
476} \
477static ssize_t set_temp_##offset##_over (struct device *dev, \
478 const char *buf, size_t count) \
479{ \
480 return set_temp_over(dev, buf, count, offset - 1); \
481} \
482static ssize_t set_temp_##offset##_hyst (struct device *dev, \
483 const char *buf, size_t count) \
484{ \
485 return set_temp_hyst(dev, buf, count, offset - 1); \
486} \
487static DEVICE_ATTR(temp##offset##_input, S_IRUGO, show_temp_##offset, NULL);\
488static DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
489 show_temp_##offset##_over, set_temp_##offset##_over); \
490static DEVICE_ATTR(temp##offset##_max_hyst, S_IRUGO | S_IWUSR, \
491 show_temp_##offset##_hyst, set_temp_##offset##_hyst);
492
493show_temp_offset(1);
494show_temp_offset(2);
495show_temp_offset(3);
496
497/* 2 Fans */
498static ssize_t show_fan(struct device *dev, char *buf, int nr) {
499 struct via686a_data *data = via686a_update_device(dev);
500 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan[nr],
501 DIV_FROM_REG(data->fan_div[nr])) );
502}
503static ssize_t show_fan_min(struct device *dev, char *buf, int nr) {
504 struct via686a_data *data = via686a_update_device(dev);
505 return sprintf(buf,"%d\n",
506 FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr])) );
507}
508static ssize_t show_fan_div(struct device *dev, char *buf, int nr) {
509 struct via686a_data *data = via686a_update_device(dev);
510 return sprintf(buf,"%d\n", DIV_FROM_REG(data->fan_div[nr]) );
511}
512static ssize_t set_fan_min(struct device *dev, const char *buf,
513 size_t count, int nr) {
514 struct i2c_client *client = to_i2c_client(dev);
515 struct via686a_data *data = i2c_get_clientdata(client);
516 int val = simple_strtol(buf, NULL, 10);
517
518 down(&data->update_lock);
519 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
520 via686a_write_value(client, VIA686A_REG_FAN_MIN(nr+1), data->fan_min[nr]);
521 up(&data->update_lock);
522 return count;
523}
524static ssize_t set_fan_div(struct device *dev, const char *buf,
525 size_t count, int nr) {
526 struct i2c_client *client = to_i2c_client(dev);
527 struct via686a_data *data = i2c_get_clientdata(client);
528 int val = simple_strtol(buf, NULL, 10);
529 int old;
530
531 down(&data->update_lock);
532 old = via686a_read_value(client, VIA686A_REG_FANDIV);
533 data->fan_div[nr] = DIV_TO_REG(val);
534 old = (old & 0x0f) | (data->fan_div[1] << 6) | (data->fan_div[0] << 4);
535 via686a_write_value(client, VIA686A_REG_FANDIV, old);
536 up(&data->update_lock);
537 return count;
538}
539
540#define show_fan_offset(offset) \
541static ssize_t show_fan_##offset (struct device *dev, char *buf) \
542{ \
543 return show_fan(dev, buf, offset - 1); \
544} \
545static ssize_t show_fan_##offset##_min (struct device *dev, char *buf) \
546{ \
547 return show_fan_min(dev, buf, offset - 1); \
548} \
549static ssize_t show_fan_##offset##_div (struct device *dev, char *buf) \
550{ \
551 return show_fan_div(dev, buf, offset - 1); \
552} \
553static ssize_t set_fan_##offset##_min (struct device *dev, \
554 const char *buf, size_t count) \
555{ \
556 return set_fan_min(dev, buf, count, offset - 1); \
557} \
558static ssize_t set_fan_##offset##_div (struct device *dev, \
559 const char *buf, size_t count) \
560{ \
561 return set_fan_div(dev, buf, count, offset - 1); \
562} \
563static DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_fan_##offset, NULL);\
564static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
565 show_fan_##offset##_min, set_fan_##offset##_min); \
566static DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
567 show_fan_##offset##_div, set_fan_##offset##_div);
568
569show_fan_offset(1);
570show_fan_offset(2);
571
572/* Alarms */
573static ssize_t show_alarms(struct device *dev, char *buf) {
574 struct via686a_data *data = via686a_update_device(dev);
575 return sprintf(buf,"%d\n", ALARMS_FROM_REG(data->alarms));
576}
577static DEVICE_ATTR(alarms, S_IRUGO | S_IWUSR, show_alarms, NULL);
578
579/* The driver. I choose to use type i2c_driver, as at is identical to both
580 smbus_driver and isa_driver, and clients could be of either kind */
581static struct i2c_driver via686a_driver = {
582 .owner = THIS_MODULE,
583 .name = "via686a",
584 .id = I2C_DRIVERID_VIA686A,
585 .flags = I2C_DF_NOTIFY,
586 .attach_adapter = via686a_attach_adapter,
587 .detach_client = via686a_detach_client,
588};
589
590
591/* This is called when the module is loaded */
592static int via686a_attach_adapter(struct i2c_adapter *adapter)
593{
594 if (!(adapter->class & I2C_CLASS_HWMON))
595 return 0;
596 return i2c_detect(adapter, &addr_data, via686a_detect);
597}
598
599static int via686a_detect(struct i2c_adapter *adapter, int address, int kind)
600{
601 struct i2c_client *new_client;
602 struct via686a_data *data;
603 int err = 0;
604 const char client_name[] = "via686a";
605 u16 val;
606
607 /* Make sure we are probing the ISA bus!! */
608 if (!i2c_is_isa_adapter(adapter)) {
609 dev_err(&adapter->dev,
610 "via686a_detect called for an I2C bus adapter?!?\n");
611 return 0;
612 }
613
614 /* 8231 requires multiple of 256, we enforce that on 686 as well */
615 if(force_addr)
616 address = force_addr & 0xFF00;
617
618 if(force_addr) {
619 dev_warn(&adapter->dev,"forcing ISA address 0x%04X\n", address);
620 if (PCIBIOS_SUCCESSFUL !=
621 pci_write_config_word(s_bridge, VIA686A_BASE_REG, address))
622 return -ENODEV;
623 }
624 if (PCIBIOS_SUCCESSFUL !=
625 pci_read_config_word(s_bridge, VIA686A_ENABLE_REG, &val))
626 return -ENODEV;
627 if (!(val & 0x0001)) {
628 dev_warn(&adapter->dev,"enabling sensors\n");
629 if (PCIBIOS_SUCCESSFUL !=
630 pci_write_config_word(s_bridge, VIA686A_ENABLE_REG,
631 val | 0x0001))
632 return -ENODEV;
633 }
634
635 /* Reserve the ISA region */
636 if (!request_region(address, VIA686A_EXTENT, via686a_driver.name)) {
637 dev_err(&adapter->dev,"region 0x%x already in use!\n",
638 address);
639 return -ENODEV;
640 }
641
642 if (!(data = kmalloc(sizeof(struct via686a_data), GFP_KERNEL))) {
643 err = -ENOMEM;
644 goto ERROR0;
645 }
646 memset(data, 0, sizeof(struct via686a_data));
647
648 new_client = &data->client;
649 i2c_set_clientdata(new_client, data);
650 new_client->addr = address;
651 new_client->adapter = adapter;
652 new_client->driver = &via686a_driver;
653 new_client->flags = 0;
654 new_client->dev.parent = &adapter->dev;
655
656 /* Fill in the remaining client fields and put into the global list */
657 snprintf(new_client->name, I2C_NAME_SIZE, client_name);
658
659 data->valid = 0;
660 init_MUTEX(&data->update_lock);
661 /* Tell the I2C layer a new client has arrived */
662 if ((err = i2c_attach_client(new_client)))
663 goto ERROR3;
664
665 /* Initialize the VIA686A chip */
666 via686a_init_client(new_client);
667
668 /* Register sysfs hooks */
669 device_create_file(&new_client->dev, &dev_attr_in0_input);
670 device_create_file(&new_client->dev, &dev_attr_in1_input);
671 device_create_file(&new_client->dev, &dev_attr_in2_input);
672 device_create_file(&new_client->dev, &dev_attr_in3_input);
673 device_create_file(&new_client->dev, &dev_attr_in4_input);
674 device_create_file(&new_client->dev, &dev_attr_in0_min);
675 device_create_file(&new_client->dev, &dev_attr_in1_min);
676 device_create_file(&new_client->dev, &dev_attr_in2_min);
677 device_create_file(&new_client->dev, &dev_attr_in3_min);
678 device_create_file(&new_client->dev, &dev_attr_in4_min);
679 device_create_file(&new_client->dev, &dev_attr_in0_max);
680 device_create_file(&new_client->dev, &dev_attr_in1_max);
681 device_create_file(&new_client->dev, &dev_attr_in2_max);
682 device_create_file(&new_client->dev, &dev_attr_in3_max);
683 device_create_file(&new_client->dev, &dev_attr_in4_max);
684 device_create_file(&new_client->dev, &dev_attr_temp1_input);
685 device_create_file(&new_client->dev, &dev_attr_temp2_input);
686 device_create_file(&new_client->dev, &dev_attr_temp3_input);
687 device_create_file(&new_client->dev, &dev_attr_temp1_max);
688 device_create_file(&new_client->dev, &dev_attr_temp2_max);
689 device_create_file(&new_client->dev, &dev_attr_temp3_max);
690 device_create_file(&new_client->dev, &dev_attr_temp1_max_hyst);
691 device_create_file(&new_client->dev, &dev_attr_temp2_max_hyst);
692 device_create_file(&new_client->dev, &dev_attr_temp3_max_hyst);
693 device_create_file(&new_client->dev, &dev_attr_fan1_input);
694 device_create_file(&new_client->dev, &dev_attr_fan2_input);
695 device_create_file(&new_client->dev, &dev_attr_fan1_min);
696 device_create_file(&new_client->dev, &dev_attr_fan2_min);
697 device_create_file(&new_client->dev, &dev_attr_fan1_div);
698 device_create_file(&new_client->dev, &dev_attr_fan2_div);
699 device_create_file(&new_client->dev, &dev_attr_alarms);
700
701 return 0;
702
703 ERROR3:
704 kfree(data);
705 ERROR0:
706 release_region(address, VIA686A_EXTENT);
707 return err;
708}
709
710static int via686a_detach_client(struct i2c_client *client)
711{
712 int err;
713
714 if ((err = i2c_detach_client(client))) {
715 dev_err(&client->dev,
716 "Client deregistration failed, client not detached.\n");
717 return err;
718 }
719
720 release_region(client->addr, VIA686A_EXTENT);
721 kfree(i2c_get_clientdata(client));
722
723 return 0;
724}
725
726/* Called when we have found a new VIA686A. Set limits, etc. */
727static void via686a_init_client(struct i2c_client *client)
728{
729 u8 reg;
730
731 /* Start monitoring */
732 reg = via686a_read_value(client, VIA686A_REG_CONFIG);
733 via686a_write_value(client, VIA686A_REG_CONFIG, (reg|0x01)&0x7F);
734
735 /* Configure temp interrupt mode for continuous-interrupt operation */
736 via686a_write_value(client, VIA686A_REG_TEMP_MODE,
737 via686a_read_value(client, VIA686A_REG_TEMP_MODE) &
738 !(VIA686A_TEMP_MODE_MASK | VIA686A_TEMP_MODE_CONTINUOUS));
739}
740
741static struct via686a_data *via686a_update_device(struct device *dev)
742{
743 struct i2c_client *client = to_i2c_client(dev);
744 struct via686a_data *data = i2c_get_clientdata(client);
745 int i;
746
747 down(&data->update_lock);
748
749 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
750 || !data->valid) {
751 for (i = 0; i <= 4; i++) {
752 data->in[i] =
753 via686a_read_value(client, VIA686A_REG_IN(i));
754 data->in_min[i] = via686a_read_value(client,
755 VIA686A_REG_IN_MIN
756 (i));
757 data->in_max[i] =
758 via686a_read_value(client, VIA686A_REG_IN_MAX(i));
759 }
760 for (i = 1; i <= 2; i++) {
761 data->fan[i - 1] =
762 via686a_read_value(client, VIA686A_REG_FAN(i));
763 data->fan_min[i - 1] = via686a_read_value(client,
764 VIA686A_REG_FAN_MIN(i));
765 }
766 for (i = 0; i <= 2; i++) {
767 data->temp[i] = via686a_read_value(client,
768 VIA686A_REG_TEMP(i)) << 2;
769 data->temp_over[i] =
770 via686a_read_value(client,
771 VIA686A_REG_TEMP_OVER(i));
772 data->temp_hyst[i] =
773 via686a_read_value(client,
774 VIA686A_REG_TEMP_HYST(i));
775 }
776 /* add in lower 2 bits
777 temp1 uses bits 7-6 of VIA686A_REG_TEMP_LOW1
778 temp2 uses bits 5-4 of VIA686A_REG_TEMP_LOW23
779 temp3 uses bits 7-6 of VIA686A_REG_TEMP_LOW23
780 */
781 data->temp[0] |= (via686a_read_value(client,
782 VIA686A_REG_TEMP_LOW1)
783 & 0xc0) >> 6;
784 data->temp[1] |=
785 (via686a_read_value(client, VIA686A_REG_TEMP_LOW23) &
786 0x30) >> 4;
787 data->temp[2] |=
788 (via686a_read_value(client, VIA686A_REG_TEMP_LOW23) &
789 0xc0) >> 6;
790
791 i = via686a_read_value(client, VIA686A_REG_FANDIV);
792 data->fan_div[0] = (i >> 4) & 0x03;
793 data->fan_div[1] = i >> 6;
794 data->alarms =
795 via686a_read_value(client,
796 VIA686A_REG_ALARM1) |
797 (via686a_read_value(client, VIA686A_REG_ALARM2) << 8);
798 data->last_updated = jiffies;
799 data->valid = 1;
800 }
801
802 up(&data->update_lock);
803
804 return data;
805}
806
807static struct pci_device_id via686a_pci_ids[] = {
808 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_82C686_4) },
809 { 0, }
810};
811
812MODULE_DEVICE_TABLE(pci, via686a_pci_ids);
813
814static int __devinit via686a_pci_probe(struct pci_dev *dev,
815 const struct pci_device_id *id)
816{
817 u16 val;
818 int addr = 0;
819
820 if (PCIBIOS_SUCCESSFUL !=
821 pci_read_config_word(dev, VIA686A_BASE_REG, &val))
822 return -ENODEV;
823
824 addr = val & ~(VIA686A_EXTENT - 1);
825 if (addr == 0 && force_addr == 0) {
826 dev_err(&dev->dev,"base address not set - upgrade BIOS or use force_addr=0xaddr\n");
827 return -ENODEV;
828 }
829 if (force_addr)
830 addr = force_addr; /* so detect will get called */
831
832 if (!addr) {
833 dev_err(&dev->dev,"No Via 686A sensors found.\n");
834 return -ENODEV;
835 }
836 normal_isa[0] = addr;
837
838 s_bridge = pci_dev_get(dev);
839 if (i2c_add_driver(&via686a_driver)) {
840 pci_dev_put(s_bridge);
841 s_bridge = NULL;
842 }
843
844 /* Always return failure here. This is to allow other drivers to bind
845 * to this pci device. We don't really want to have control over the
846 * pci device, we only wanted to read as few register values from it.
847 */
848 return -ENODEV;
849}
850
851static struct pci_driver via686a_pci_driver = {
852 .name = "via686a",
853 .id_table = via686a_pci_ids,
854 .probe = via686a_pci_probe,
855};
856
857static int __init sm_via686a_init(void)
858{
859 return pci_register_driver(&via686a_pci_driver);
860}
861
862static void __exit sm_via686a_exit(void)
863{
864 pci_unregister_driver(&via686a_pci_driver);
865 if (s_bridge != NULL) {
866 i2c_del_driver(&via686a_driver);
867 pci_dev_put(s_bridge);
868 s_bridge = NULL;
869 }
870}
871
872MODULE_AUTHOR("Kyösti Mälkki <kmalkki@cc.hut.fi>, "
873 "Mark Studebaker <mdsxyz123@yahoo.com> "
874 "and Bob Dougherty <bobd@stanford.edu>");
875MODULE_DESCRIPTION("VIA 686A Sensor device");
876MODULE_LICENSE("GPL");
877
878module_init(sm_via686a_init);
879module_exit(sm_via686a_exit);
diff --git a/drivers/i2c/chips/w83627hf.c b/drivers/i2c/chips/w83627hf.c
new file mode 100644
index 000000000000..b1da5ed696d3
--- /dev/null
+++ b/drivers/i2c/chips/w83627hf.c
@@ -0,0 +1,1511 @@
1/*
2 w83627hf.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring
4 Copyright (c) 1998 - 2003 Frodo Looijaard <frodol@dds.nl>,
5 Philip Edelbrock <phil@netroedge.com>,
6 and Mark Studebaker <mdsxyz123@yahoo.com>
7 Ported to 2.6 by Bernhard C. Schrenk <clemy@clemy.org>
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22*/
23
24/*
25 Supports following chips:
26
27 Chip #vin #fanin #pwm #temp wchipid vendid i2c ISA
28 w83627hf 9 3 2 3 0x20 0x5ca3 no yes(LPC)
29 w83627thf 7 3 3 3 0x90 0x5ca3 no yes(LPC)
30 w83637hf 7 3 3 3 0x80 0x5ca3 no yes(LPC)
31 w83697hf 8 2 2 2 0x60 0x5ca3 no yes(LPC)
32
33 For other winbond chips, and for i2c support in the above chips,
34 use w83781d.c.
35
36 Note: automatic ("cruise") fan control for 697, 637 & 627thf not
37 supported yet.
38*/
39
40#include <linux/module.h>
41#include <linux/init.h>
42#include <linux/slab.h>
43#include <linux/jiffies.h>
44#include <linux/i2c.h>
45#include <linux/i2c-sensor.h>
46#include <linux/i2c-vid.h>
47#include <asm/io.h>
48#include "lm75.h"
49
50static u16 force_addr;
51module_param(force_addr, ushort, 0);
52MODULE_PARM_DESC(force_addr,
53 "Initialize the base address of the sensors");
54static u8 force_i2c = 0x1f;
55module_param(force_i2c, byte, 0);
56MODULE_PARM_DESC(force_i2c,
57 "Initialize the i2c address of the sensors");
58
59/* Addresses to scan */
60static unsigned short normal_i2c[] = { I2C_CLIENT_END };
61static unsigned int normal_isa[] = { 0, I2C_CLIENT_ISA_END };
62
63/* Insmod parameters */
64SENSORS_INSMOD_4(w83627hf, w83627thf, w83697hf, w83637hf);
65
66static int init = 1;
67module_param(init, bool, 0);
68MODULE_PARM_DESC(init, "Set to zero to bypass chip initialization");
69
70/* modified from kernel/include/traps.c */
71static int REG; /* The register to read/write */
72#define DEV 0x07 /* Register: Logical device select */
73static int VAL; /* The value to read/write */
74
75/* logical device numbers for superio_select (below) */
76#define W83627HF_LD_FDC 0x00
77#define W83627HF_LD_PRT 0x01
78#define W83627HF_LD_UART1 0x02
79#define W83627HF_LD_UART2 0x03
80#define W83627HF_LD_KBC 0x05
81#define W83627HF_LD_CIR 0x06 /* w83627hf only */
82#define W83627HF_LD_GAME 0x07
83#define W83627HF_LD_MIDI 0x07
84#define W83627HF_LD_GPIO1 0x07
85#define W83627HF_LD_GPIO5 0x07 /* w83627thf only */
86#define W83627HF_LD_GPIO2 0x08
87#define W83627HF_LD_GPIO3 0x09
88#define W83627HF_LD_GPIO4 0x09 /* w83627thf only */
89#define W83627HF_LD_ACPI 0x0a
90#define W83627HF_LD_HWM 0x0b
91
92#define DEVID 0x20 /* Register: Device ID */
93
94#define W83627THF_GPIO5_EN 0x30 /* w83627thf only */
95#define W83627THF_GPIO5_IOSR 0xf3 /* w83627thf only */
96#define W83627THF_GPIO5_DR 0xf4 /* w83627thf only */
97
98static inline void
99superio_outb(int reg, int val)
100{
101 outb(reg, REG);
102 outb(val, VAL);
103}
104
105static inline int
106superio_inb(int reg)
107{
108 outb(reg, REG);
109 return inb(VAL);
110}
111
112static inline void
113superio_select(int ld)
114{
115 outb(DEV, REG);
116 outb(ld, VAL);
117}
118
119static inline void
120superio_enter(void)
121{
122 outb(0x87, REG);
123 outb(0x87, REG);
124}
125
126static inline void
127superio_exit(void)
128{
129 outb(0xAA, REG);
130}
131
132#define W627_DEVID 0x52
133#define W627THF_DEVID 0x82
134#define W697_DEVID 0x60
135#define W637_DEVID 0x70
136#define WINB_ACT_REG 0x30
137#define WINB_BASE_REG 0x60
138/* Constants specified below */
139
140/* Length of ISA address segment */
141#define WINB_EXTENT 8
142
143/* Where are the ISA address/data registers relative to the base address */
144#define W83781D_ADDR_REG_OFFSET 5
145#define W83781D_DATA_REG_OFFSET 6
146
147/* The W83781D registers */
148/* The W83782D registers for nr=7,8 are in bank 5 */
149#define W83781D_REG_IN_MAX(nr) ((nr < 7) ? (0x2b + (nr) * 2) : \
150 (0x554 + (((nr) - 7) * 2)))
151#define W83781D_REG_IN_MIN(nr) ((nr < 7) ? (0x2c + (nr) * 2) : \
152 (0x555 + (((nr) - 7) * 2)))
153#define W83781D_REG_IN(nr) ((nr < 7) ? (0x20 + (nr)) : \
154 (0x550 + (nr) - 7))
155
156#define W83781D_REG_FAN_MIN(nr) (0x3a + (nr))
157#define W83781D_REG_FAN(nr) (0x27 + (nr))
158
159#define W83781D_REG_TEMP2_CONFIG 0x152
160#define W83781D_REG_TEMP3_CONFIG 0x252
161#define W83781D_REG_TEMP(nr) ((nr == 3) ? (0x0250) : \
162 ((nr == 2) ? (0x0150) : \
163 (0x27)))
164#define W83781D_REG_TEMP_HYST(nr) ((nr == 3) ? (0x253) : \
165 ((nr == 2) ? (0x153) : \
166 (0x3A)))
167#define W83781D_REG_TEMP_OVER(nr) ((nr == 3) ? (0x255) : \
168 ((nr == 2) ? (0x155) : \
169 (0x39)))
170
171#define W83781D_REG_BANK 0x4E
172
173#define W83781D_REG_CONFIG 0x40
174#define W83781D_REG_ALARM1 0x41
175#define W83781D_REG_ALARM2 0x42
176#define W83781D_REG_ALARM3 0x450
177
178#define W83781D_REG_IRQ 0x4C
179#define W83781D_REG_BEEP_CONFIG 0x4D
180#define W83781D_REG_BEEP_INTS1 0x56
181#define W83781D_REG_BEEP_INTS2 0x57
182#define W83781D_REG_BEEP_INTS3 0x453
183
184#define W83781D_REG_VID_FANDIV 0x47
185
186#define W83781D_REG_CHIPID 0x49
187#define W83781D_REG_WCHIPID 0x58
188#define W83781D_REG_CHIPMAN 0x4F
189#define W83781D_REG_PIN 0x4B
190
191#define W83781D_REG_VBAT 0x5D
192
193#define W83627HF_REG_PWM1 0x5A
194#define W83627HF_REG_PWM2 0x5B
195#define W83627HF_REG_PWMCLK12 0x5C
196
197#define W83627THF_REG_PWM1 0x01 /* 697HF and 637HF too */
198#define W83627THF_REG_PWM2 0x03 /* 697HF and 637HF too */
199#define W83627THF_REG_PWM3 0x11 /* 637HF too */
200
201#define W83627THF_REG_VRM_OVT_CFG 0x18 /* 637HF too */
202
203static const u8 regpwm_627hf[] = { W83627HF_REG_PWM1, W83627HF_REG_PWM2 };
204static const u8 regpwm[] = { W83627THF_REG_PWM1, W83627THF_REG_PWM2,
205 W83627THF_REG_PWM3 };
206#define W836X7HF_REG_PWM(type, nr) (((type) == w83627hf) ? \
207 regpwm_627hf[(nr) - 1] : regpwm[(nr) - 1])
208
209#define W83781D_REG_I2C_ADDR 0x48
210#define W83781D_REG_I2C_SUBADDR 0x4A
211
212/* Sensor selection */
213#define W83781D_REG_SCFG1 0x5D
214static const u8 BIT_SCFG1[] = { 0x02, 0x04, 0x08 };
215#define W83781D_REG_SCFG2 0x59
216static const u8 BIT_SCFG2[] = { 0x10, 0x20, 0x40 };
217#define W83781D_DEFAULT_BETA 3435
218
219/* Conversions. Limit checking is only done on the TO_REG
220 variants. Note that you should be a bit careful with which arguments
221 these macros are called: arguments may be evaluated more than once.
222 Fixing this is just not worth it. */
223#define IN_TO_REG(val) (SENSORS_LIMIT((((val) + 8)/16),0,255))
224#define IN_FROM_REG(val) ((val) * 16)
225
226static inline u8 FAN_TO_REG(long rpm, int div)
227{
228 if (rpm == 0)
229 return 255;
230 rpm = SENSORS_LIMIT(rpm, 1, 1000000);
231 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1,
232 254);
233}
234
235#define TEMP_MIN (-128000)
236#define TEMP_MAX ( 127000)
237
238/* TEMP: 0.001C/bit (-128C to +127C)
239 REG: 1C/bit, two's complement */
240static u8 TEMP_TO_REG(int temp)
241{
242 int ntemp = SENSORS_LIMIT(temp, TEMP_MIN, TEMP_MAX);
243 ntemp += (ntemp<0 ? -500 : 500);
244 return (u8)(ntemp / 1000);
245}
246
247static int TEMP_FROM_REG(u8 reg)
248{
249 return (s8)reg * 1000;
250}
251
252#define FAN_FROM_REG(val,div) ((val)==0?-1:(val)==255?0:1350000/((val)*(div)))
253
254#define PWM_TO_REG(val) (SENSORS_LIMIT((val),0,255))
255
256#define BEEP_MASK_FROM_REG(val) (val)
257#define BEEP_MASK_TO_REG(val) ((val) & 0xffffff)
258#define BEEP_ENABLE_TO_REG(val) ((val)?1:0)
259#define BEEP_ENABLE_FROM_REG(val) ((val)?1:0)
260
261#define DIV_FROM_REG(val) (1 << (val))
262
263static inline u8 DIV_TO_REG(long val)
264{
265 int i;
266 val = SENSORS_LIMIT(val, 1, 128) >> 1;
267 for (i = 0; i < 6; i++) {
268 if (val == 0)
269 break;
270 val >>= 1;
271 }
272 return ((u8) i);
273}
274
275/* For each registered chip, we need to keep some data in memory. That
276 data is pointed to by w83627hf_list[NR]->data. The structure itself is
277 dynamically allocated, at the same time when a new client is allocated. */
278struct w83627hf_data {
279 struct i2c_client client;
280 struct semaphore lock;
281 enum chips type;
282
283 struct semaphore update_lock;
284 char valid; /* !=0 if following fields are valid */
285 unsigned long last_updated; /* In jiffies */
286
287 struct i2c_client *lm75; /* for secondary I2C addresses */
288 /* pointer to array of 2 subclients */
289
290 u8 in[9]; /* Register value */
291 u8 in_max[9]; /* Register value */
292 u8 in_min[9]; /* Register value */
293 u8 fan[3]; /* Register value */
294 u8 fan_min[3]; /* Register value */
295 u8 temp;
296 u8 temp_max; /* Register value */
297 u8 temp_max_hyst; /* Register value */
298 u16 temp_add[2]; /* Register value */
299 u16 temp_max_add[2]; /* Register value */
300 u16 temp_max_hyst_add[2]; /* Register value */
301 u8 fan_div[3]; /* Register encoding, shifted right */
302 u8 vid; /* Register encoding, combined */
303 u32 alarms; /* Register encoding, combined */
304 u32 beep_mask; /* Register encoding, combined */
305 u8 beep_enable; /* Boolean */
306 u8 pwm[3]; /* Register value */
307 u16 sens[3]; /* 782D/783S only.
308 1 = pentium diode; 2 = 3904 diode;
309 3000-5000 = thermistor beta.
310 Default = 3435.
311 Other Betas unimplemented */
312 u8 vrm;
313 u8 vrm_ovt; /* Register value, 627thf & 637hf only */
314};
315
316
317static int w83627hf_attach_adapter(struct i2c_adapter *adapter);
318static int w83627hf_detect(struct i2c_adapter *adapter, int address,
319 int kind);
320static int w83627hf_detach_client(struct i2c_client *client);
321
322static int w83627hf_read_value(struct i2c_client *client, u16 register);
323static int w83627hf_write_value(struct i2c_client *client, u16 register,
324 u16 value);
325static struct w83627hf_data *w83627hf_update_device(struct device *dev);
326static void w83627hf_init_client(struct i2c_client *client);
327
328static struct i2c_driver w83627hf_driver = {
329 .owner = THIS_MODULE,
330 .name = "w83627hf",
331 .id = I2C_DRIVERID_W83627HF,
332 .flags = I2C_DF_NOTIFY,
333 .attach_adapter = w83627hf_attach_adapter,
334 .detach_client = w83627hf_detach_client,
335};
336
337/* following are the sysfs callback functions */
338#define show_in_reg(reg) \
339static ssize_t show_##reg (struct device *dev, char *buf, int nr) \
340{ \
341 struct w83627hf_data *data = w83627hf_update_device(dev); \
342 return sprintf(buf,"%ld\n", (long)IN_FROM_REG(data->reg[nr])); \
343}
344show_in_reg(in)
345show_in_reg(in_min)
346show_in_reg(in_max)
347
348#define store_in_reg(REG, reg) \
349static ssize_t \
350store_in_##reg (struct device *dev, const char *buf, size_t count, int nr) \
351{ \
352 struct i2c_client *client = to_i2c_client(dev); \
353 struct w83627hf_data *data = i2c_get_clientdata(client); \
354 u32 val; \
355 \
356 val = simple_strtoul(buf, NULL, 10); \
357 \
358 down(&data->update_lock); \
359 data->in_##reg[nr] = IN_TO_REG(val); \
360 w83627hf_write_value(client, W83781D_REG_IN_##REG(nr), \
361 data->in_##reg[nr]); \
362 \
363 up(&data->update_lock); \
364 return count; \
365}
366store_in_reg(MIN, min)
367store_in_reg(MAX, max)
368
369#define sysfs_in_offset(offset) \
370static ssize_t \
371show_regs_in_##offset (struct device *dev, char *buf) \
372{ \
373 return show_in(dev, buf, offset); \
374} \
375static DEVICE_ATTR(in##offset##_input, S_IRUGO, show_regs_in_##offset, NULL);
376
377#define sysfs_in_reg_offset(reg, offset) \
378static ssize_t show_regs_in_##reg##offset (struct device *dev, char *buf) \
379{ \
380 return show_in_##reg (dev, buf, offset); \
381} \
382static ssize_t \
383store_regs_in_##reg##offset (struct device *dev, \
384 const char *buf, size_t count) \
385{ \
386 return store_in_##reg (dev, buf, count, offset); \
387} \
388static DEVICE_ATTR(in##offset##_##reg, S_IRUGO| S_IWUSR, \
389 show_regs_in_##reg##offset, store_regs_in_##reg##offset);
390
391#define sysfs_in_offsets(offset) \
392sysfs_in_offset(offset) \
393sysfs_in_reg_offset(min, offset) \
394sysfs_in_reg_offset(max, offset)
395
396sysfs_in_offsets(1);
397sysfs_in_offsets(2);
398sysfs_in_offsets(3);
399sysfs_in_offsets(4);
400sysfs_in_offsets(5);
401sysfs_in_offsets(6);
402sysfs_in_offsets(7);
403sysfs_in_offsets(8);
404
405/* use a different set of functions for in0 */
406static ssize_t show_in_0(struct w83627hf_data *data, char *buf, u8 reg)
407{
408 long in0;
409
410 if ((data->vrm_ovt & 0x01) &&
411 (w83627thf == data->type || w83637hf == data->type))
412
413 /* use VRM9 calculation */
414 in0 = (long)((reg * 488 + 70000 + 50) / 100);
415 else
416 /* use VRM8 (standard) calculation */
417 in0 = (long)IN_FROM_REG(reg);
418
419 return sprintf(buf,"%ld\n", in0);
420}
421
422static ssize_t show_regs_in_0(struct device *dev, char *buf)
423{
424 struct w83627hf_data *data = w83627hf_update_device(dev);
425 return show_in_0(data, buf, data->in[0]);
426}
427
428static ssize_t show_regs_in_min0(struct device *dev, char *buf)
429{
430 struct w83627hf_data *data = w83627hf_update_device(dev);
431 return show_in_0(data, buf, data->in_min[0]);
432}
433
434static ssize_t show_regs_in_max0(struct device *dev, char *buf)
435{
436 struct w83627hf_data *data = w83627hf_update_device(dev);
437 return show_in_0(data, buf, data->in_max[0]);
438}
439
440static ssize_t store_regs_in_min0(struct device *dev,
441 const char *buf, size_t count)
442{
443 struct i2c_client *client = to_i2c_client(dev);
444 struct w83627hf_data *data = i2c_get_clientdata(client);
445 u32 val;
446
447 val = simple_strtoul(buf, NULL, 10);
448
449 down(&data->update_lock);
450
451 if ((data->vrm_ovt & 0x01) &&
452 (w83627thf == data->type || w83637hf == data->type))
453
454 /* use VRM9 calculation */
455 data->in_min[0] = (u8)(((val * 100) - 70000 + 244) / 488);
456 else
457 /* use VRM8 (standard) calculation */
458 data->in_min[0] = IN_TO_REG(val);
459
460 w83627hf_write_value(client, W83781D_REG_IN_MIN(0), data->in_min[0]);
461 up(&data->update_lock);
462 return count;
463}
464
465static ssize_t store_regs_in_max0(struct device *dev,
466 const char *buf, size_t count)
467{
468 struct i2c_client *client = to_i2c_client(dev);
469 struct w83627hf_data *data = i2c_get_clientdata(client);
470 u32 val;
471
472 val = simple_strtoul(buf, NULL, 10);
473
474 down(&data->update_lock);
475
476 if ((data->vrm_ovt & 0x01) &&
477 (w83627thf == data->type || w83637hf == data->type))
478
479 /* use VRM9 calculation */
480 data->in_max[0] = (u8)(((val * 100) - 70000 + 244) / 488);
481 else
482 /* use VRM8 (standard) calculation */
483 data->in_max[0] = IN_TO_REG(val);
484
485 w83627hf_write_value(client, W83781D_REG_IN_MAX(0), data->in_max[0]);
486 up(&data->update_lock);
487 return count;
488}
489
490static DEVICE_ATTR(in0_input, S_IRUGO, show_regs_in_0, NULL);
491static DEVICE_ATTR(in0_min, S_IRUGO | S_IWUSR,
492 show_regs_in_min0, store_regs_in_min0);
493static DEVICE_ATTR(in0_max, S_IRUGO | S_IWUSR,
494 show_regs_in_max0, store_regs_in_max0);
495
496#define device_create_file_in(client, offset) \
497do { \
498device_create_file(&client->dev, &dev_attr_in##offset##_input); \
499device_create_file(&client->dev, &dev_attr_in##offset##_min); \
500device_create_file(&client->dev, &dev_attr_in##offset##_max); \
501} while (0)
502
503#define show_fan_reg(reg) \
504static ssize_t show_##reg (struct device *dev, char *buf, int nr) \
505{ \
506 struct w83627hf_data *data = w83627hf_update_device(dev); \
507 return sprintf(buf,"%ld\n", \
508 FAN_FROM_REG(data->reg[nr-1], \
509 (long)DIV_FROM_REG(data->fan_div[nr-1]))); \
510}
511show_fan_reg(fan);
512show_fan_reg(fan_min);
513
514static ssize_t
515store_fan_min(struct device *dev, const char *buf, size_t count, int nr)
516{
517 struct i2c_client *client = to_i2c_client(dev);
518 struct w83627hf_data *data = i2c_get_clientdata(client);
519 u32 val;
520
521 val = simple_strtoul(buf, NULL, 10);
522
523 down(&data->update_lock);
524 data->fan_min[nr - 1] =
525 FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr - 1]));
526 w83627hf_write_value(client, W83781D_REG_FAN_MIN(nr),
527 data->fan_min[nr - 1]);
528
529 up(&data->update_lock);
530 return count;
531}
532
533#define sysfs_fan_offset(offset) \
534static ssize_t show_regs_fan_##offset (struct device *dev, char *buf) \
535{ \
536 return show_fan(dev, buf, offset); \
537} \
538static DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_regs_fan_##offset, NULL);
539
540#define sysfs_fan_min_offset(offset) \
541static ssize_t show_regs_fan_min##offset (struct device *dev, char *buf) \
542{ \
543 return show_fan_min(dev, buf, offset); \
544} \
545static ssize_t \
546store_regs_fan_min##offset (struct device *dev, const char *buf, size_t count) \
547{ \
548 return store_fan_min(dev, buf, count, offset); \
549} \
550static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
551 show_regs_fan_min##offset, store_regs_fan_min##offset);
552
553sysfs_fan_offset(1);
554sysfs_fan_min_offset(1);
555sysfs_fan_offset(2);
556sysfs_fan_min_offset(2);
557sysfs_fan_offset(3);
558sysfs_fan_min_offset(3);
559
560#define device_create_file_fan(client, offset) \
561do { \
562device_create_file(&client->dev, &dev_attr_fan##offset##_input); \
563device_create_file(&client->dev, &dev_attr_fan##offset##_min); \
564} while (0)
565
566#define show_temp_reg(reg) \
567static ssize_t show_##reg (struct device *dev, char *buf, int nr) \
568{ \
569 struct w83627hf_data *data = w83627hf_update_device(dev); \
570 if (nr >= 2) { /* TEMP2 and TEMP3 */ \
571 return sprintf(buf,"%ld\n", \
572 (long)LM75_TEMP_FROM_REG(data->reg##_add[nr-2])); \
573 } else { /* TEMP1 */ \
574 return sprintf(buf,"%ld\n", (long)TEMP_FROM_REG(data->reg)); \
575 } \
576}
577show_temp_reg(temp);
578show_temp_reg(temp_max);
579show_temp_reg(temp_max_hyst);
580
581#define store_temp_reg(REG, reg) \
582static ssize_t \
583store_temp_##reg (struct device *dev, const char *buf, size_t count, int nr) \
584{ \
585 struct i2c_client *client = to_i2c_client(dev); \
586 struct w83627hf_data *data = i2c_get_clientdata(client); \
587 u32 val; \
588 \
589 val = simple_strtoul(buf, NULL, 10); \
590 \
591 down(&data->update_lock); \
592 \
593 if (nr >= 2) { /* TEMP2 and TEMP3 */ \
594 data->temp_##reg##_add[nr-2] = LM75_TEMP_TO_REG(val); \
595 w83627hf_write_value(client, W83781D_REG_TEMP_##REG(nr), \
596 data->temp_##reg##_add[nr-2]); \
597 } else { /* TEMP1 */ \
598 data->temp_##reg = TEMP_TO_REG(val); \
599 w83627hf_write_value(client, W83781D_REG_TEMP_##REG(nr), \
600 data->temp_##reg); \
601 } \
602 \
603 up(&data->update_lock); \
604 return count; \
605}
606store_temp_reg(OVER, max);
607store_temp_reg(HYST, max_hyst);
608
609#define sysfs_temp_offset(offset) \
610static ssize_t \
611show_regs_temp_##offset (struct device *dev, char *buf) \
612{ \
613 return show_temp(dev, buf, offset); \
614} \
615static DEVICE_ATTR(temp##offset##_input, S_IRUGO, show_regs_temp_##offset, NULL);
616
617#define sysfs_temp_reg_offset(reg, offset) \
618static ssize_t show_regs_temp_##reg##offset (struct device *dev, char *buf) \
619{ \
620 return show_temp_##reg (dev, buf, offset); \
621} \
622static ssize_t \
623store_regs_temp_##reg##offset (struct device *dev, \
624 const char *buf, size_t count) \
625{ \
626 return store_temp_##reg (dev, buf, count, offset); \
627} \
628static DEVICE_ATTR(temp##offset##_##reg, S_IRUGO| S_IWUSR, \
629 show_regs_temp_##reg##offset, store_regs_temp_##reg##offset);
630
631#define sysfs_temp_offsets(offset) \
632sysfs_temp_offset(offset) \
633sysfs_temp_reg_offset(max, offset) \
634sysfs_temp_reg_offset(max_hyst, offset)
635
636sysfs_temp_offsets(1);
637sysfs_temp_offsets(2);
638sysfs_temp_offsets(3);
639
640#define device_create_file_temp(client, offset) \
641do { \
642device_create_file(&client->dev, &dev_attr_temp##offset##_input); \
643device_create_file(&client->dev, &dev_attr_temp##offset##_max); \
644device_create_file(&client->dev, &dev_attr_temp##offset##_max_hyst); \
645} while (0)
646
647static ssize_t
648show_vid_reg(struct device *dev, char *buf)
649{
650 struct w83627hf_data *data = w83627hf_update_device(dev);
651 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
652}
653static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
654#define device_create_file_vid(client) \
655device_create_file(&client->dev, &dev_attr_cpu0_vid)
656
657static ssize_t
658show_vrm_reg(struct device *dev, char *buf)
659{
660 struct w83627hf_data *data = w83627hf_update_device(dev);
661 return sprintf(buf, "%ld\n", (long) data->vrm);
662}
663static ssize_t
664store_vrm_reg(struct device *dev, const char *buf, size_t count)
665{
666 struct i2c_client *client = to_i2c_client(dev);
667 struct w83627hf_data *data = i2c_get_clientdata(client);
668 u32 val;
669
670 val = simple_strtoul(buf, NULL, 10);
671 data->vrm = val;
672
673 return count;
674}
675static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
676#define device_create_file_vrm(client) \
677device_create_file(&client->dev, &dev_attr_vrm)
678
679static ssize_t
680show_alarms_reg(struct device *dev, char *buf)
681{
682 struct w83627hf_data *data = w83627hf_update_device(dev);
683 return sprintf(buf, "%ld\n", (long) data->alarms);
684}
685static DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL);
686#define device_create_file_alarms(client) \
687device_create_file(&client->dev, &dev_attr_alarms)
688
689#define show_beep_reg(REG, reg) \
690static ssize_t show_beep_##reg (struct device *dev, char *buf) \
691{ \
692 struct w83627hf_data *data = w83627hf_update_device(dev); \
693 return sprintf(buf,"%ld\n", \
694 (long)BEEP_##REG##_FROM_REG(data->beep_##reg)); \
695}
696show_beep_reg(ENABLE, enable)
697show_beep_reg(MASK, mask)
698
699#define BEEP_ENABLE 0 /* Store beep_enable */
700#define BEEP_MASK 1 /* Store beep_mask */
701
702static ssize_t
703store_beep_reg(struct device *dev, const char *buf, size_t count,
704 int update_mask)
705{
706 struct i2c_client *client = to_i2c_client(dev);
707 struct w83627hf_data *data = i2c_get_clientdata(client);
708 u32 val, val2;
709
710 val = simple_strtoul(buf, NULL, 10);
711
712 down(&data->update_lock);
713
714 if (update_mask == BEEP_MASK) { /* We are storing beep_mask */
715 data->beep_mask = BEEP_MASK_TO_REG(val);
716 w83627hf_write_value(client, W83781D_REG_BEEP_INTS1,
717 data->beep_mask & 0xff);
718 w83627hf_write_value(client, W83781D_REG_BEEP_INTS3,
719 ((data->beep_mask) >> 16) & 0xff);
720 val2 = (data->beep_mask >> 8) & 0x7f;
721 } else { /* We are storing beep_enable */
722 val2 =
723 w83627hf_read_value(client, W83781D_REG_BEEP_INTS2) & 0x7f;
724 data->beep_enable = BEEP_ENABLE_TO_REG(val);
725 }
726
727 w83627hf_write_value(client, W83781D_REG_BEEP_INTS2,
728 val2 | data->beep_enable << 7);
729
730 up(&data->update_lock);
731 return count;
732}
733
734#define sysfs_beep(REG, reg) \
735static ssize_t show_regs_beep_##reg (struct device *dev, char *buf) \
736{ \
737 return show_beep_##reg(dev, buf); \
738} \
739static ssize_t \
740store_regs_beep_##reg (struct device *dev, const char *buf, size_t count) \
741{ \
742 return store_beep_reg(dev, buf, count, BEEP_##REG); \
743} \
744static DEVICE_ATTR(beep_##reg, S_IRUGO | S_IWUSR, \
745 show_regs_beep_##reg, store_regs_beep_##reg);
746
747sysfs_beep(ENABLE, enable);
748sysfs_beep(MASK, mask);
749
750#define device_create_file_beep(client) \
751do { \
752device_create_file(&client->dev, &dev_attr_beep_enable); \
753device_create_file(&client->dev, &dev_attr_beep_mask); \
754} while (0)
755
756static ssize_t
757show_fan_div_reg(struct device *dev, char *buf, int nr)
758{
759 struct w83627hf_data *data = w83627hf_update_device(dev);
760 return sprintf(buf, "%ld\n",
761 (long) DIV_FROM_REG(data->fan_div[nr - 1]));
762}
763
764/* Note: we save and restore the fan minimum here, because its value is
765 determined in part by the fan divisor. This follows the principle of
766 least suprise; the user doesn't expect the fan minimum to change just
767 because the divisor changed. */
768static ssize_t
769store_fan_div_reg(struct device *dev, const char *buf, size_t count, int nr)
770{
771 struct i2c_client *client = to_i2c_client(dev);
772 struct w83627hf_data *data = i2c_get_clientdata(client);
773 unsigned long min;
774 u8 reg;
775 unsigned long val = simple_strtoul(buf, NULL, 10);
776
777 down(&data->update_lock);
778
779 /* Save fan_min */
780 min = FAN_FROM_REG(data->fan_min[nr],
781 DIV_FROM_REG(data->fan_div[nr]));
782
783 data->fan_div[nr] = DIV_TO_REG(val);
784
785 reg = (w83627hf_read_value(client, nr==2 ? W83781D_REG_PIN : W83781D_REG_VID_FANDIV)
786 & (nr==0 ? 0xcf : 0x3f))
787 | ((data->fan_div[nr] & 0x03) << (nr==0 ? 4 : 6));
788 w83627hf_write_value(client, nr==2 ? W83781D_REG_PIN : W83781D_REG_VID_FANDIV, reg);
789
790 reg = (w83627hf_read_value(client, W83781D_REG_VBAT)
791 & ~(1 << (5 + nr)))
792 | ((data->fan_div[nr] & 0x04) << (3 + nr));
793 w83627hf_write_value(client, W83781D_REG_VBAT, reg);
794
795 /* Restore fan_min */
796 data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
797 w83627hf_write_value(client, W83781D_REG_FAN_MIN(nr+1), data->fan_min[nr]);
798
799 up(&data->update_lock);
800 return count;
801}
802
803#define sysfs_fan_div(offset) \
804static ssize_t show_regs_fan_div_##offset (struct device *dev, char *buf) \
805{ \
806 return show_fan_div_reg(dev, buf, offset); \
807} \
808static ssize_t \
809store_regs_fan_div_##offset (struct device *dev, \
810 const char *buf, size_t count) \
811{ \
812 return store_fan_div_reg(dev, buf, count, offset - 1); \
813} \
814static DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
815 show_regs_fan_div_##offset, store_regs_fan_div_##offset);
816
817sysfs_fan_div(1);
818sysfs_fan_div(2);
819sysfs_fan_div(3);
820
821#define device_create_file_fan_div(client, offset) \
822do { \
823device_create_file(&client->dev, &dev_attr_fan##offset##_div); \
824} while (0)
825
826static ssize_t
827show_pwm_reg(struct device *dev, char *buf, int nr)
828{
829 struct w83627hf_data *data = w83627hf_update_device(dev);
830 return sprintf(buf, "%ld\n", (long) data->pwm[nr - 1]);
831}
832
833static ssize_t
834store_pwm_reg(struct device *dev, const char *buf, size_t count, int nr)
835{
836 struct i2c_client *client = to_i2c_client(dev);
837 struct w83627hf_data *data = i2c_get_clientdata(client);
838 u32 val;
839
840 val = simple_strtoul(buf, NULL, 10);
841
842 down(&data->update_lock);
843
844 if (data->type == w83627thf) {
845 /* bits 0-3 are reserved in 627THF */
846 data->pwm[nr - 1] = PWM_TO_REG(val) & 0xf0;
847 w83627hf_write_value(client,
848 W836X7HF_REG_PWM(data->type, nr),
849 data->pwm[nr - 1] |
850 (w83627hf_read_value(client,
851 W836X7HF_REG_PWM(data->type, nr)) & 0x0f));
852 } else {
853 data->pwm[nr - 1] = PWM_TO_REG(val);
854 w83627hf_write_value(client,
855 W836X7HF_REG_PWM(data->type, nr),
856 data->pwm[nr - 1]);
857 }
858
859 up(&data->update_lock);
860 return count;
861}
862
863#define sysfs_pwm(offset) \
864static ssize_t show_regs_pwm_##offset (struct device *dev, char *buf) \
865{ \
866 return show_pwm_reg(dev, buf, offset); \
867} \
868static ssize_t \
869store_regs_pwm_##offset (struct device *dev, const char *buf, size_t count) \
870{ \
871 return store_pwm_reg(dev, buf, count, offset); \
872} \
873static DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
874 show_regs_pwm_##offset, store_regs_pwm_##offset);
875
876sysfs_pwm(1);
877sysfs_pwm(2);
878sysfs_pwm(3);
879
880#define device_create_file_pwm(client, offset) \
881do { \
882device_create_file(&client->dev, &dev_attr_pwm##offset); \
883} while (0)
884
885static ssize_t
886show_sensor_reg(struct device *dev, char *buf, int nr)
887{
888 struct w83627hf_data *data = w83627hf_update_device(dev);
889 return sprintf(buf, "%ld\n", (long) data->sens[nr - 1]);
890}
891
892static ssize_t
893store_sensor_reg(struct device *dev, const char *buf, size_t count, int nr)
894{
895 struct i2c_client *client = to_i2c_client(dev);
896 struct w83627hf_data *data = i2c_get_clientdata(client);
897 u32 val, tmp;
898
899 val = simple_strtoul(buf, NULL, 10);
900
901 down(&data->update_lock);
902
903 switch (val) {
904 case 1: /* PII/Celeron diode */
905 tmp = w83627hf_read_value(client, W83781D_REG_SCFG1);
906 w83627hf_write_value(client, W83781D_REG_SCFG1,
907 tmp | BIT_SCFG1[nr - 1]);
908 tmp = w83627hf_read_value(client, W83781D_REG_SCFG2);
909 w83627hf_write_value(client, W83781D_REG_SCFG2,
910 tmp | BIT_SCFG2[nr - 1]);
911 data->sens[nr - 1] = val;
912 break;
913 case 2: /* 3904 */
914 tmp = w83627hf_read_value(client, W83781D_REG_SCFG1);
915 w83627hf_write_value(client, W83781D_REG_SCFG1,
916 tmp | BIT_SCFG1[nr - 1]);
917 tmp = w83627hf_read_value(client, W83781D_REG_SCFG2);
918 w83627hf_write_value(client, W83781D_REG_SCFG2,
919 tmp & ~BIT_SCFG2[nr - 1]);
920 data->sens[nr - 1] = val;
921 break;
922 case W83781D_DEFAULT_BETA: /* thermistor */
923 tmp = w83627hf_read_value(client, W83781D_REG_SCFG1);
924 w83627hf_write_value(client, W83781D_REG_SCFG1,
925 tmp & ~BIT_SCFG1[nr - 1]);
926 data->sens[nr - 1] = val;
927 break;
928 default:
929 dev_err(&client->dev,
930 "Invalid sensor type %ld; must be 1, 2, or %d\n",
931 (long) val, W83781D_DEFAULT_BETA);
932 break;
933 }
934
935 up(&data->update_lock);
936 return count;
937}
938
939#define sysfs_sensor(offset) \
940static ssize_t show_regs_sensor_##offset (struct device *dev, char *buf) \
941{ \
942 return show_sensor_reg(dev, buf, offset); \
943} \
944static ssize_t \
945store_regs_sensor_##offset (struct device *dev, const char *buf, size_t count) \
946{ \
947 return store_sensor_reg(dev, buf, count, offset); \
948} \
949static DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR, \
950 show_regs_sensor_##offset, store_regs_sensor_##offset);
951
952sysfs_sensor(1);
953sysfs_sensor(2);
954sysfs_sensor(3);
955
956#define device_create_file_sensor(client, offset) \
957do { \
958device_create_file(&client->dev, &dev_attr_temp##offset##_type); \
959} while (0)
960
961
962/* This function is called when:
963 * w83627hf_driver is inserted (when this module is loaded), for each
964 available adapter
965 * when a new adapter is inserted (and w83627hf_driver is still present) */
966static int w83627hf_attach_adapter(struct i2c_adapter *adapter)
967{
968 return i2c_detect(adapter, &addr_data, w83627hf_detect);
969}
970
971static int w83627hf_find(int sioaddr, int *address)
972{
973 u16 val;
974
975 REG = sioaddr;
976 VAL = sioaddr + 1;
977
978 superio_enter();
979 val= superio_inb(DEVID);
980 if(val != W627_DEVID &&
981 val != W627THF_DEVID &&
982 val != W697_DEVID &&
983 val != W637_DEVID) {
984 superio_exit();
985 return -ENODEV;
986 }
987
988 superio_select(W83627HF_LD_HWM);
989 val = (superio_inb(WINB_BASE_REG) << 8) |
990 superio_inb(WINB_BASE_REG + 1);
991 *address = val & ~(WINB_EXTENT - 1);
992 if (*address == 0 && force_addr == 0) {
993 superio_exit();
994 return -ENODEV;
995 }
996 if (force_addr)
997 *address = force_addr; /* so detect will get called */
998
999 superio_exit();
1000 return 0;
1001}
1002
1003int w83627hf_detect(struct i2c_adapter *adapter, int address,
1004 int kind)
1005{
1006 int val;
1007 struct i2c_client *new_client;
1008 struct w83627hf_data *data;
1009 int err = 0;
1010 const char *client_name = "";
1011
1012 if (!i2c_is_isa_adapter(adapter)) {
1013 err = -ENODEV;
1014 goto ERROR0;
1015 }
1016
1017 if(force_addr)
1018 address = force_addr & ~(WINB_EXTENT - 1);
1019
1020 if (!request_region(address, WINB_EXTENT, w83627hf_driver.name)) {
1021 err = -EBUSY;
1022 goto ERROR0;
1023 }
1024
1025 if(force_addr) {
1026 printk("w83627hf.o: forcing ISA address 0x%04X\n", address);
1027 superio_enter();
1028 superio_select(W83627HF_LD_HWM);
1029 superio_outb(WINB_BASE_REG, address >> 8);
1030 superio_outb(WINB_BASE_REG+1, address & 0xff);
1031 superio_exit();
1032 }
1033
1034 superio_enter();
1035 val= superio_inb(DEVID);
1036 if(val == W627_DEVID)
1037 kind = w83627hf;
1038 else if(val == W697_DEVID)
1039 kind = w83697hf;
1040 else if(val == W627THF_DEVID)
1041 kind = w83627thf;
1042 else if(val == W637_DEVID)
1043 kind = w83637hf;
1044 else {
1045 dev_info(&adapter->dev,
1046 "Unsupported chip (dev_id=0x%02X).\n", val);
1047 goto ERROR1;
1048 }
1049
1050 superio_select(W83627HF_LD_HWM);
1051 if((val = 0x01 & superio_inb(WINB_ACT_REG)) == 0)
1052 superio_outb(WINB_ACT_REG, 1);
1053 superio_exit();
1054
1055 /* OK. For now, we presume we have a valid client. We now create the
1056 client structure, even though we cannot fill it completely yet.
1057 But it allows us to access w83627hf_{read,write}_value. */
1058
1059 if (!(data = kmalloc(sizeof(struct w83627hf_data), GFP_KERNEL))) {
1060 err = -ENOMEM;
1061 goto ERROR1;
1062 }
1063 memset(data, 0, sizeof(struct w83627hf_data));
1064
1065 new_client = &data->client;
1066 i2c_set_clientdata(new_client, data);
1067 new_client->addr = address;
1068 init_MUTEX(&data->lock);
1069 new_client->adapter = adapter;
1070 new_client->driver = &w83627hf_driver;
1071 new_client->flags = 0;
1072
1073
1074 if (kind == w83627hf) {
1075 client_name = "w83627hf";
1076 } else if (kind == w83627thf) {
1077 client_name = "w83627thf";
1078 } else if (kind == w83697hf) {
1079 client_name = "w83697hf";
1080 } else if (kind == w83637hf) {
1081 client_name = "w83637hf";
1082 }
1083
1084 /* Fill in the remaining client fields and put into the global list */
1085 strlcpy(new_client->name, client_name, I2C_NAME_SIZE);
1086 data->type = kind;
1087 data->valid = 0;
1088 init_MUTEX(&data->update_lock);
1089
1090 /* Tell the I2C layer a new client has arrived */
1091 if ((err = i2c_attach_client(new_client)))
1092 goto ERROR2;
1093
1094 data->lm75 = NULL;
1095
1096 /* Initialize the chip */
1097 w83627hf_init_client(new_client);
1098
1099 /* A few vars need to be filled upon startup */
1100 data->fan_min[0] = w83627hf_read_value(new_client, W83781D_REG_FAN_MIN(1));
1101 data->fan_min[1] = w83627hf_read_value(new_client, W83781D_REG_FAN_MIN(2));
1102 data->fan_min[2] = w83627hf_read_value(new_client, W83781D_REG_FAN_MIN(3));
1103
1104 /* Register sysfs hooks */
1105 device_create_file_in(new_client, 0);
1106 if (kind != w83697hf)
1107 device_create_file_in(new_client, 1);
1108 device_create_file_in(new_client, 2);
1109 device_create_file_in(new_client, 3);
1110 device_create_file_in(new_client, 4);
1111 if (kind != w83627thf && kind != w83637hf) {
1112 device_create_file_in(new_client, 5);
1113 device_create_file_in(new_client, 6);
1114 }
1115 device_create_file_in(new_client, 7);
1116 device_create_file_in(new_client, 8);
1117
1118 device_create_file_fan(new_client, 1);
1119 device_create_file_fan(new_client, 2);
1120 if (kind != w83697hf)
1121 device_create_file_fan(new_client, 3);
1122
1123 device_create_file_temp(new_client, 1);
1124 device_create_file_temp(new_client, 2);
1125 if (kind != w83697hf)
1126 device_create_file_temp(new_client, 3);
1127
1128 if (kind != w83697hf)
1129 device_create_file_vid(new_client);
1130
1131 if (kind != w83697hf)
1132 device_create_file_vrm(new_client);
1133
1134 device_create_file_fan_div(new_client, 1);
1135 device_create_file_fan_div(new_client, 2);
1136 if (kind != w83697hf)
1137 device_create_file_fan_div(new_client, 3);
1138
1139 device_create_file_alarms(new_client);
1140
1141 device_create_file_beep(new_client);
1142
1143 device_create_file_pwm(new_client, 1);
1144 device_create_file_pwm(new_client, 2);
1145 if (kind == w83627thf || kind == w83637hf)
1146 device_create_file_pwm(new_client, 3);
1147
1148 device_create_file_sensor(new_client, 1);
1149 device_create_file_sensor(new_client, 2);
1150 if (kind != w83697hf)
1151 device_create_file_sensor(new_client, 3);
1152
1153 return 0;
1154
1155 ERROR2:
1156 kfree(data);
1157 ERROR1:
1158 release_region(address, WINB_EXTENT);
1159 ERROR0:
1160 return err;
1161}
1162
1163static int w83627hf_detach_client(struct i2c_client *client)
1164{
1165 int err;
1166
1167 if ((err = i2c_detach_client(client))) {
1168 dev_err(&client->dev,
1169 "Client deregistration failed, client not detached.\n");
1170 return err;
1171 }
1172
1173 release_region(client->addr, WINB_EXTENT);
1174 kfree(i2c_get_clientdata(client));
1175
1176 return 0;
1177}
1178
1179
1180/*
1181 ISA access must always be locked explicitly!
1182 We ignore the W83781D BUSY flag at this moment - it could lead to deadlocks,
1183 would slow down the W83781D access and should not be necessary.
1184 There are some ugly typecasts here, but the good news is - they should
1185 nowhere else be necessary! */
1186static int w83627hf_read_value(struct i2c_client *client, u16 reg)
1187{
1188 struct w83627hf_data *data = i2c_get_clientdata(client);
1189 int res, word_sized;
1190
1191 down(&data->lock);
1192 word_sized = (((reg & 0xff00) == 0x100)
1193 || ((reg & 0xff00) == 0x200))
1194 && (((reg & 0x00ff) == 0x50)
1195 || ((reg & 0x00ff) == 0x53)
1196 || ((reg & 0x00ff) == 0x55));
1197 if (reg & 0xff00) {
1198 outb_p(W83781D_REG_BANK,
1199 client->addr + W83781D_ADDR_REG_OFFSET);
1200 outb_p(reg >> 8,
1201 client->addr + W83781D_DATA_REG_OFFSET);
1202 }
1203 outb_p(reg & 0xff, client->addr + W83781D_ADDR_REG_OFFSET);
1204 res = inb_p(client->addr + W83781D_DATA_REG_OFFSET);
1205 if (word_sized) {
1206 outb_p((reg & 0xff) + 1,
1207 client->addr + W83781D_ADDR_REG_OFFSET);
1208 res =
1209 (res << 8) + inb_p(client->addr +
1210 W83781D_DATA_REG_OFFSET);
1211 }
1212 if (reg & 0xff00) {
1213 outb_p(W83781D_REG_BANK,
1214 client->addr + W83781D_ADDR_REG_OFFSET);
1215 outb_p(0, client->addr + W83781D_DATA_REG_OFFSET);
1216 }
1217 up(&data->lock);
1218 return res;
1219}
1220
1221static int w83627thf_read_gpio5(struct i2c_client *client)
1222{
1223 int res = 0xff, sel;
1224
1225 superio_enter();
1226 superio_select(W83627HF_LD_GPIO5);
1227
1228 /* Make sure these GPIO pins are enabled */
1229 if (!(superio_inb(W83627THF_GPIO5_EN) & (1<<3))) {
1230 dev_dbg(&client->dev, "GPIO5 disabled, no VID function\n");
1231 goto exit;
1232 }
1233
1234 /* Make sure the pins are configured for input
1235 There must be at least five (VRM 9), and possibly 6 (VRM 10) */
1236 sel = superio_inb(W83627THF_GPIO5_IOSR);
1237 if ((sel & 0x1f) != 0x1f) {
1238 dev_dbg(&client->dev, "GPIO5 not configured for VID "
1239 "function\n");
1240 goto exit;
1241 }
1242
1243 dev_info(&client->dev, "Reading VID from GPIO5\n");
1244 res = superio_inb(W83627THF_GPIO5_DR) & sel;
1245
1246exit:
1247 superio_exit();
1248 return res;
1249}
1250
1251static int w83627hf_write_value(struct i2c_client *client, u16 reg, u16 value)
1252{
1253 struct w83627hf_data *data = i2c_get_clientdata(client);
1254 int word_sized;
1255
1256 down(&data->lock);
1257 word_sized = (((reg & 0xff00) == 0x100)
1258 || ((reg & 0xff00) == 0x200))
1259 && (((reg & 0x00ff) == 0x53)
1260 || ((reg & 0x00ff) == 0x55));
1261 if (reg & 0xff00) {
1262 outb_p(W83781D_REG_BANK,
1263 client->addr + W83781D_ADDR_REG_OFFSET);
1264 outb_p(reg >> 8,
1265 client->addr + W83781D_DATA_REG_OFFSET);
1266 }
1267 outb_p(reg & 0xff, client->addr + W83781D_ADDR_REG_OFFSET);
1268 if (word_sized) {
1269 outb_p(value >> 8,
1270 client->addr + W83781D_DATA_REG_OFFSET);
1271 outb_p((reg & 0xff) + 1,
1272 client->addr + W83781D_ADDR_REG_OFFSET);
1273 }
1274 outb_p(value & 0xff,
1275 client->addr + W83781D_DATA_REG_OFFSET);
1276 if (reg & 0xff00) {
1277 outb_p(W83781D_REG_BANK,
1278 client->addr + W83781D_ADDR_REG_OFFSET);
1279 outb_p(0, client->addr + W83781D_DATA_REG_OFFSET);
1280 }
1281 up(&data->lock);
1282 return 0;
1283}
1284
1285/* Called when we have found a new W83781D. It should set limits, etc. */
1286static void w83627hf_init_client(struct i2c_client *client)
1287{
1288 struct w83627hf_data *data = i2c_get_clientdata(client);
1289 int i;
1290 int type = data->type;
1291 u8 tmp;
1292
1293 if(init) {
1294 /* save this register */
1295 i = w83627hf_read_value(client, W83781D_REG_BEEP_CONFIG);
1296 /* Reset all except Watchdog values and last conversion values
1297 This sets fan-divs to 2, among others */
1298 w83627hf_write_value(client, W83781D_REG_CONFIG, 0x80);
1299 /* Restore the register and disable power-on abnormal beep.
1300 This saves FAN 1/2/3 input/output values set by BIOS. */
1301 w83627hf_write_value(client, W83781D_REG_BEEP_CONFIG, i | 0x80);
1302 /* Disable master beep-enable (reset turns it on).
1303 Individual beeps should be reset to off but for some reason
1304 disabling this bit helps some people not get beeped */
1305 w83627hf_write_value(client, W83781D_REG_BEEP_INTS2, 0);
1306 }
1307
1308 /* Minimize conflicts with other winbond i2c-only clients... */
1309 /* disable i2c subclients... how to disable main i2c client?? */
1310 /* force i2c address to relatively uncommon address */
1311 w83627hf_write_value(client, W83781D_REG_I2C_SUBADDR, 0x89);
1312 w83627hf_write_value(client, W83781D_REG_I2C_ADDR, force_i2c);
1313
1314 /* Read VID only once */
1315 if (w83627hf == data->type || w83637hf == data->type) {
1316 int lo = w83627hf_read_value(client, W83781D_REG_VID_FANDIV);
1317 int hi = w83627hf_read_value(client, W83781D_REG_CHIPID);
1318 data->vid = (lo & 0x0f) | ((hi & 0x01) << 4);
1319 } else if (w83627thf == data->type) {
1320 data->vid = w83627thf_read_gpio5(client) & 0x3f;
1321 }
1322
1323 /* Read VRM & OVT Config only once */
1324 if (w83627thf == data->type || w83637hf == data->type) {
1325 data->vrm_ovt =
1326 w83627hf_read_value(client, W83627THF_REG_VRM_OVT_CFG);
1327 data->vrm = (data->vrm_ovt & 0x01) ? 90 : 82;
1328 } else {
1329 /* Convert VID to voltage based on default VRM */
1330 data->vrm = i2c_which_vrm();
1331 }
1332
1333 tmp = w83627hf_read_value(client, W83781D_REG_SCFG1);
1334 for (i = 1; i <= 3; i++) {
1335 if (!(tmp & BIT_SCFG1[i - 1])) {
1336 data->sens[i - 1] = W83781D_DEFAULT_BETA;
1337 } else {
1338 if (w83627hf_read_value
1339 (client,
1340 W83781D_REG_SCFG2) & BIT_SCFG2[i - 1])
1341 data->sens[i - 1] = 1;
1342 else
1343 data->sens[i - 1] = 2;
1344 }
1345 if ((type == w83697hf) && (i == 2))
1346 break;
1347 }
1348
1349 if(init) {
1350 /* Enable temp2 */
1351 tmp = w83627hf_read_value(client, W83781D_REG_TEMP2_CONFIG);
1352 if (tmp & 0x01) {
1353 dev_warn(&client->dev, "Enabling temp2, readings "
1354 "might not make sense\n");
1355 w83627hf_write_value(client, W83781D_REG_TEMP2_CONFIG,
1356 tmp & 0xfe);
1357 }
1358
1359 /* Enable temp3 */
1360 if (type != w83697hf) {
1361 tmp = w83627hf_read_value(client,
1362 W83781D_REG_TEMP3_CONFIG);
1363 if (tmp & 0x01) {
1364 dev_warn(&client->dev, "Enabling temp3, "
1365 "readings might not make sense\n");
1366 w83627hf_write_value(client,
1367 W83781D_REG_TEMP3_CONFIG, tmp & 0xfe);
1368 }
1369 }
1370
1371 if (type == w83627hf) {
1372 /* enable PWM2 control (can't hurt since PWM reg
1373 should have been reset to 0xff) */
1374 w83627hf_write_value(client, W83627HF_REG_PWMCLK12,
1375 0x19);
1376 }
1377 /* enable comparator mode for temp2 and temp3 so
1378 alarm indication will work correctly */
1379 i = w83627hf_read_value(client, W83781D_REG_IRQ);
1380 if (!(i & 0x40))
1381 w83627hf_write_value(client, W83781D_REG_IRQ,
1382 i | 0x40);
1383 }
1384
1385 /* Start monitoring */
1386 w83627hf_write_value(client, W83781D_REG_CONFIG,
1387 (w83627hf_read_value(client,
1388 W83781D_REG_CONFIG) & 0xf7)
1389 | 0x01);
1390}
1391
1392static struct w83627hf_data *w83627hf_update_device(struct device *dev)
1393{
1394 struct i2c_client *client = to_i2c_client(dev);
1395 struct w83627hf_data *data = i2c_get_clientdata(client);
1396 int i;
1397
1398 down(&data->update_lock);
1399
1400 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
1401 || !data->valid) {
1402 for (i = 0; i <= 8; i++) {
1403 /* skip missing sensors */
1404 if (((data->type == w83697hf) && (i == 1)) ||
1405 ((data->type == w83627thf || data->type == w83637hf)
1406 && (i == 4 || i == 5)))
1407 continue;
1408 data->in[i] =
1409 w83627hf_read_value(client, W83781D_REG_IN(i));
1410 data->in_min[i] =
1411 w83627hf_read_value(client,
1412 W83781D_REG_IN_MIN(i));
1413 data->in_max[i] =
1414 w83627hf_read_value(client,
1415 W83781D_REG_IN_MAX(i));
1416 }
1417 for (i = 1; i <= 3; i++) {
1418 data->fan[i - 1] =
1419 w83627hf_read_value(client, W83781D_REG_FAN(i));
1420 data->fan_min[i - 1] =
1421 w83627hf_read_value(client,
1422 W83781D_REG_FAN_MIN(i));
1423 }
1424 for (i = 1; i <= 3; i++) {
1425 u8 tmp = w83627hf_read_value(client,
1426 W836X7HF_REG_PWM(data->type, i));
1427 /* bits 0-3 are reserved in 627THF */
1428 if (data->type == w83627thf)
1429 tmp &= 0xf0;
1430 data->pwm[i - 1] = tmp;
1431 if(i == 2 &&
1432 (data->type == w83627hf || data->type == w83697hf))
1433 break;
1434 }
1435
1436 data->temp = w83627hf_read_value(client, W83781D_REG_TEMP(1));
1437 data->temp_max =
1438 w83627hf_read_value(client, W83781D_REG_TEMP_OVER(1));
1439 data->temp_max_hyst =
1440 w83627hf_read_value(client, W83781D_REG_TEMP_HYST(1));
1441 data->temp_add[0] =
1442 w83627hf_read_value(client, W83781D_REG_TEMP(2));
1443 data->temp_max_add[0] =
1444 w83627hf_read_value(client, W83781D_REG_TEMP_OVER(2));
1445 data->temp_max_hyst_add[0] =
1446 w83627hf_read_value(client, W83781D_REG_TEMP_HYST(2));
1447 if (data->type != w83697hf) {
1448 data->temp_add[1] =
1449 w83627hf_read_value(client, W83781D_REG_TEMP(3));
1450 data->temp_max_add[1] =
1451 w83627hf_read_value(client, W83781D_REG_TEMP_OVER(3));
1452 data->temp_max_hyst_add[1] =
1453 w83627hf_read_value(client, W83781D_REG_TEMP_HYST(3));
1454 }
1455
1456 i = w83627hf_read_value(client, W83781D_REG_VID_FANDIV);
1457 data->fan_div[0] = (i >> 4) & 0x03;
1458 data->fan_div[1] = (i >> 6) & 0x03;
1459 if (data->type != w83697hf) {
1460 data->fan_div[2] = (w83627hf_read_value(client,
1461 W83781D_REG_PIN) >> 6) & 0x03;
1462 }
1463 i = w83627hf_read_value(client, W83781D_REG_VBAT);
1464 data->fan_div[0] |= (i >> 3) & 0x04;
1465 data->fan_div[1] |= (i >> 4) & 0x04;
1466 if (data->type != w83697hf)
1467 data->fan_div[2] |= (i >> 5) & 0x04;
1468 data->alarms =
1469 w83627hf_read_value(client, W83781D_REG_ALARM1) |
1470 (w83627hf_read_value(client, W83781D_REG_ALARM2) << 8) |
1471 (w83627hf_read_value(client, W83781D_REG_ALARM3) << 16);
1472 i = w83627hf_read_value(client, W83781D_REG_BEEP_INTS2);
1473 data->beep_enable = i >> 7;
1474 data->beep_mask = ((i & 0x7f) << 8) |
1475 w83627hf_read_value(client, W83781D_REG_BEEP_INTS1) |
1476 w83627hf_read_value(client, W83781D_REG_BEEP_INTS3) << 16;
1477 data->last_updated = jiffies;
1478 data->valid = 1;
1479 }
1480
1481 up(&data->update_lock);
1482
1483 return data;
1484}
1485
1486static int __init sensors_w83627hf_init(void)
1487{
1488 int addr;
1489
1490 if (w83627hf_find(0x2e, &addr)
1491 && w83627hf_find(0x4e, &addr)) {
1492 return -ENODEV;
1493 }
1494 normal_isa[0] = addr;
1495
1496 return i2c_add_driver(&w83627hf_driver);
1497}
1498
1499static void __exit sensors_w83627hf_exit(void)
1500{
1501 i2c_del_driver(&w83627hf_driver);
1502}
1503
1504MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, "
1505 "Philip Edelbrock <phil@netroedge.com>, "
1506 "and Mark Studebaker <mdsxyz123@yahoo.com>");
1507MODULE_DESCRIPTION("W83627HF driver");
1508MODULE_LICENSE("GPL");
1509
1510module_init(sensors_w83627hf_init);
1511module_exit(sensors_w83627hf_exit);
diff --git a/drivers/i2c/chips/w83781d.c b/drivers/i2c/chips/w83781d.c
new file mode 100644
index 000000000000..4954e465c419
--- /dev/null
+++ b/drivers/i2c/chips/w83781d.c
@@ -0,0 +1,1664 @@
1/*
2 w83781d.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring
4 Copyright (c) 1998 - 2001 Frodo Looijaard <frodol@dds.nl>,
5 Philip Edelbrock <phil@netroedge.com>,
6 and Mark Studebaker <mdsxyz123@yahoo.com>
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; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21*/
22
23/*
24 Supports following chips:
25
26 Chip #vin #fanin #pwm #temp wchipid vendid i2c ISA
27 as99127f 7 3 0 3 0x31 0x12c3 yes no
28 as99127f rev.2 (type_name = as99127f) 0x31 0x5ca3 yes no
29 w83781d 7 3 0 3 0x10-1 0x5ca3 yes yes
30 w83627hf 9 3 2 3 0x21 0x5ca3 yes yes(LPC)
31 w83627thf 9 3 2 3 0x90 0x5ca3 no yes(LPC)
32 w83782d 9 3 2-4 3 0x30 0x5ca3 yes yes
33 w83783s 5-6 3 2 1-2 0x40 0x5ca3 yes no
34 w83697hf 8 2 2 2 0x60 0x5ca3 no yes(LPC)
35
36*/
37
38#include <linux/config.h>
39#include <linux/module.h>
40#include <linux/init.h>
41#include <linux/slab.h>
42#include <linux/jiffies.h>
43#include <linux/i2c.h>
44#include <linux/i2c-sensor.h>
45#include <linux/i2c-vid.h>
46#include <asm/io.h>
47#include "lm75.h"
48
49/* Addresses to scan */
50static unsigned short normal_i2c[] = { 0x20, 0x21, 0x22, 0x23, 0x24, 0x25,
51 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b,
52 0x2c, 0x2d, 0x2e, 0x2f, I2C_CLIENT_END };
53static unsigned int normal_isa[] = { 0x0290, I2C_CLIENT_ISA_END };
54
55/* Insmod parameters */
56SENSORS_INSMOD_6(w83781d, w83782d, w83783s, w83627hf, as99127f, w83697hf);
57I2C_CLIENT_MODULE_PARM(force_subclients, "List of subclient addresses: "
58 "{bus, clientaddr, subclientaddr1, subclientaddr2}");
59
60static int init = 1;
61module_param(init, bool, 0);
62MODULE_PARM_DESC(init, "Set to zero to bypass chip initialization");
63
64/* Constants specified below */
65
66/* Length of ISA address segment */
67#define W83781D_EXTENT 8
68
69/* Where are the ISA address/data registers relative to the base address */
70#define W83781D_ADDR_REG_OFFSET 5
71#define W83781D_DATA_REG_OFFSET 6
72
73/* The W83781D registers */
74/* The W83782D registers for nr=7,8 are in bank 5 */
75#define W83781D_REG_IN_MAX(nr) ((nr < 7) ? (0x2b + (nr) * 2) : \
76 (0x554 + (((nr) - 7) * 2)))
77#define W83781D_REG_IN_MIN(nr) ((nr < 7) ? (0x2c + (nr) * 2) : \
78 (0x555 + (((nr) - 7) * 2)))
79#define W83781D_REG_IN(nr) ((nr < 7) ? (0x20 + (nr)) : \
80 (0x550 + (nr) - 7))
81
82#define W83781D_REG_FAN_MIN(nr) (0x3a + (nr))
83#define W83781D_REG_FAN(nr) (0x27 + (nr))
84
85#define W83781D_REG_BANK 0x4E
86#define W83781D_REG_TEMP2_CONFIG 0x152
87#define W83781D_REG_TEMP3_CONFIG 0x252
88#define W83781D_REG_TEMP(nr) ((nr == 3) ? (0x0250) : \
89 ((nr == 2) ? (0x0150) : \
90 (0x27)))
91#define W83781D_REG_TEMP_HYST(nr) ((nr == 3) ? (0x253) : \
92 ((nr == 2) ? (0x153) : \
93 (0x3A)))
94#define W83781D_REG_TEMP_OVER(nr) ((nr == 3) ? (0x255) : \
95 ((nr == 2) ? (0x155) : \
96 (0x39)))
97
98#define W83781D_REG_CONFIG 0x40
99#define W83781D_REG_ALARM1 0x41
100#define W83781D_REG_ALARM2 0x42
101#define W83781D_REG_ALARM3 0x450 /* not on W83781D */
102
103#define W83781D_REG_IRQ 0x4C
104#define W83781D_REG_BEEP_CONFIG 0x4D
105#define W83781D_REG_BEEP_INTS1 0x56
106#define W83781D_REG_BEEP_INTS2 0x57
107#define W83781D_REG_BEEP_INTS3 0x453 /* not on W83781D */
108
109#define W83781D_REG_VID_FANDIV 0x47
110
111#define W83781D_REG_CHIPID 0x49
112#define W83781D_REG_WCHIPID 0x58
113#define W83781D_REG_CHIPMAN 0x4F
114#define W83781D_REG_PIN 0x4B
115
116/* 782D/783S only */
117#define W83781D_REG_VBAT 0x5D
118
119/* PWM 782D (1-4) and 783S (1-2) only */
120#define W83781D_REG_PWM1 0x5B /* 782d and 783s/627hf datasheets disagree */
121 /* on which is which; */
122#define W83781D_REG_PWM2 0x5A /* We follow the 782d convention here, */
123 /* However 782d is probably wrong. */
124#define W83781D_REG_PWM3 0x5E
125#define W83781D_REG_PWM4 0x5F
126#define W83781D_REG_PWMCLK12 0x5C
127#define W83781D_REG_PWMCLK34 0x45C
128static const u8 regpwm[] = { W83781D_REG_PWM1, W83781D_REG_PWM2,
129 W83781D_REG_PWM3, W83781D_REG_PWM4
130};
131
132#define W83781D_REG_PWM(nr) (regpwm[(nr) - 1])
133
134#define W83781D_REG_I2C_ADDR 0x48
135#define W83781D_REG_I2C_SUBADDR 0x4A
136
137/* The following are undocumented in the data sheets however we
138 received the information in an email from Winbond tech support */
139/* Sensor selection - not on 781d */
140#define W83781D_REG_SCFG1 0x5D
141static const u8 BIT_SCFG1[] = { 0x02, 0x04, 0x08 };
142
143#define W83781D_REG_SCFG2 0x59
144static const u8 BIT_SCFG2[] = { 0x10, 0x20, 0x40 };
145
146#define W83781D_DEFAULT_BETA 3435
147
148/* RT Table registers */
149#define W83781D_REG_RT_IDX 0x50
150#define W83781D_REG_RT_VAL 0x51
151
152/* Conversions. Rounding and limit checking is only done on the TO_REG
153 variants. Note that you should be a bit careful with which arguments
154 these macros are called: arguments may be evaluated more than once.
155 Fixing this is just not worth it. */
156#define IN_TO_REG(val) (SENSORS_LIMIT((((val) * 10 + 8)/16),0,255))
157#define IN_FROM_REG(val) (((val) * 16) / 10)
158
159static inline u8
160FAN_TO_REG(long rpm, int div)
161{
162 if (rpm == 0)
163 return 255;
164 rpm = SENSORS_LIMIT(rpm, 1, 1000000);
165 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
166}
167
168#define FAN_FROM_REG(val,div) ((val) == 0 ? -1 : \
169 ((val) == 255 ? 0 : \
170 1350000 / ((val) * (div))))
171
172#define TEMP_TO_REG(val) (SENSORS_LIMIT(((val) < 0 ? (val)+0x100*1000 \
173 : (val)) / 1000, 0, 0xff))
174#define TEMP_FROM_REG(val) (((val) & 0x80 ? (val)-0x100 : (val)) * 1000)
175
176#define ALARMS_FROM_REG(val) (val)
177#define PWM_FROM_REG(val) (val)
178#define PWM_TO_REG(val) (SENSORS_LIMIT((val),0,255))
179#define BEEP_MASK_FROM_REG(val,type) ((type) == as99127f ? \
180 (val) ^ 0x7fff : (val))
181#define BEEP_MASK_TO_REG(val,type) ((type) == as99127f ? \
182 (~(val)) & 0x7fff : (val) & 0xffffff)
183
184#define BEEP_ENABLE_TO_REG(val) ((val) ? 1 : 0)
185#define BEEP_ENABLE_FROM_REG(val) ((val) ? 1 : 0)
186
187#define DIV_FROM_REG(val) (1 << (val))
188
189static inline u8
190DIV_TO_REG(long val, enum chips type)
191{
192 int i;
193 val = SENSORS_LIMIT(val, 1,
194 ((type == w83781d
195 || type == as99127f) ? 8 : 128)) >> 1;
196 for (i = 0; i < 6; i++) {
197 if (val == 0)
198 break;
199 val >>= 1;
200 }
201 return ((u8) i);
202}
203
204/* There are some complications in a module like this. First off, W83781D chips
205 may be both present on the SMBus and the ISA bus, and we have to handle
206 those cases separately at some places. Second, there might be several
207 W83781D chips available (well, actually, that is probably never done; but
208 it is a clean illustration of how to handle a case like that). Finally,
209 a specific chip may be attached to *both* ISA and SMBus, and we would
210 not like to detect it double. Fortunately, in the case of the W83781D at
211 least, a register tells us what SMBus address we are on, so that helps
212 a bit - except if there could be more than one SMBus. Groan. No solution
213 for this yet. */
214
215/* This module may seem overly long and complicated. In fact, it is not so
216 bad. Quite a lot of bookkeeping is done. A real driver can often cut
217 some corners. */
218
219/* For each registered W83781D, we need to keep some data in memory. That
220 data is pointed to by w83781d_list[NR]->data. The structure itself is
221 dynamically allocated, at the same time when a new w83781d client is
222 allocated. */
223struct w83781d_data {
224 struct i2c_client client;
225 struct semaphore lock;
226 enum chips type;
227
228 struct semaphore update_lock;
229 char valid; /* !=0 if following fields are valid */
230 unsigned long last_updated; /* In jiffies */
231
232 struct i2c_client *lm75[2]; /* for secondary I2C addresses */
233 /* array of 2 pointers to subclients */
234
235 u8 in[9]; /* Register value - 8 & 9 for 782D only */
236 u8 in_max[9]; /* Register value - 8 & 9 for 782D only */
237 u8 in_min[9]; /* Register value - 8 & 9 for 782D only */
238 u8 fan[3]; /* Register value */
239 u8 fan_min[3]; /* Register value */
240 u8 temp;
241 u8 temp_max; /* Register value */
242 u8 temp_max_hyst; /* Register value */
243 u16 temp_add[2]; /* Register value */
244 u16 temp_max_add[2]; /* Register value */
245 u16 temp_max_hyst_add[2]; /* Register value */
246 u8 fan_div[3]; /* Register encoding, shifted right */
247 u8 vid; /* Register encoding, combined */
248 u32 alarms; /* Register encoding, combined */
249 u32 beep_mask; /* Register encoding, combined */
250 u8 beep_enable; /* Boolean */
251 u8 pwm[4]; /* Register value */
252 u8 pwmenable[4]; /* Boolean */
253 u16 sens[3]; /* 782D/783S only.
254 1 = pentium diode; 2 = 3904 diode;
255 3000-5000 = thermistor beta.
256 Default = 3435.
257 Other Betas unimplemented */
258 u8 vrm;
259};
260
261static int w83781d_attach_adapter(struct i2c_adapter *adapter);
262static int w83781d_detect(struct i2c_adapter *adapter, int address, int kind);
263static int w83781d_detach_client(struct i2c_client *client);
264
265static int w83781d_read_value(struct i2c_client *client, u16 register);
266static int w83781d_write_value(struct i2c_client *client, u16 register,
267 u16 value);
268static struct w83781d_data *w83781d_update_device(struct device *dev);
269static void w83781d_init_client(struct i2c_client *client);
270
271static struct i2c_driver w83781d_driver = {
272 .owner = THIS_MODULE,
273 .name = "w83781d",
274 .id = I2C_DRIVERID_W83781D,
275 .flags = I2C_DF_NOTIFY,
276 .attach_adapter = w83781d_attach_adapter,
277 .detach_client = w83781d_detach_client,
278};
279
280/* following are the sysfs callback functions */
281#define show_in_reg(reg) \
282static ssize_t show_##reg (struct device *dev, char *buf, int nr) \
283{ \
284 struct w83781d_data *data = w83781d_update_device(dev); \
285 return sprintf(buf,"%ld\n", (long)IN_FROM_REG(data->reg[nr] * 10)); \
286}
287show_in_reg(in);
288show_in_reg(in_min);
289show_in_reg(in_max);
290
291#define store_in_reg(REG, reg) \
292static ssize_t store_in_##reg (struct device *dev, const char *buf, size_t count, int nr) \
293{ \
294 struct i2c_client *client = to_i2c_client(dev); \
295 struct w83781d_data *data = i2c_get_clientdata(client); \
296 u32 val; \
297 \
298 val = simple_strtoul(buf, NULL, 10) / 10; \
299 \
300 down(&data->update_lock); \
301 data->in_##reg[nr] = IN_TO_REG(val); \
302 w83781d_write_value(client, W83781D_REG_IN_##REG(nr), data->in_##reg[nr]); \
303 \
304 up(&data->update_lock); \
305 return count; \
306}
307store_in_reg(MIN, min);
308store_in_reg(MAX, max);
309
310#define sysfs_in_offset(offset) \
311static ssize_t \
312show_regs_in_##offset (struct device *dev, char *buf) \
313{ \
314 return show_in(dev, buf, offset); \
315} \
316static DEVICE_ATTR(in##offset##_input, S_IRUGO, show_regs_in_##offset, NULL);
317
318#define sysfs_in_reg_offset(reg, offset) \
319static ssize_t show_regs_in_##reg##offset (struct device *dev, char *buf) \
320{ \
321 return show_in_##reg (dev, buf, offset); \
322} \
323static ssize_t store_regs_in_##reg##offset (struct device *dev, const char *buf, size_t count) \
324{ \
325 return store_in_##reg (dev, buf, count, offset); \
326} \
327static DEVICE_ATTR(in##offset##_##reg, S_IRUGO| S_IWUSR, show_regs_in_##reg##offset, store_regs_in_##reg##offset);
328
329#define sysfs_in_offsets(offset) \
330sysfs_in_offset(offset); \
331sysfs_in_reg_offset(min, offset); \
332sysfs_in_reg_offset(max, offset);
333
334sysfs_in_offsets(0);
335sysfs_in_offsets(1);
336sysfs_in_offsets(2);
337sysfs_in_offsets(3);
338sysfs_in_offsets(4);
339sysfs_in_offsets(5);
340sysfs_in_offsets(6);
341sysfs_in_offsets(7);
342sysfs_in_offsets(8);
343
344#define device_create_file_in(client, offset) \
345do { \
346device_create_file(&client->dev, &dev_attr_in##offset##_input); \
347device_create_file(&client->dev, &dev_attr_in##offset##_min); \
348device_create_file(&client->dev, &dev_attr_in##offset##_max); \
349} while (0)
350
351#define show_fan_reg(reg) \
352static ssize_t show_##reg (struct device *dev, char *buf, int nr) \
353{ \
354 struct w83781d_data *data = w83781d_update_device(dev); \
355 return sprintf(buf,"%ld\n", \
356 FAN_FROM_REG(data->reg[nr-1], (long)DIV_FROM_REG(data->fan_div[nr-1]))); \
357}
358show_fan_reg(fan);
359show_fan_reg(fan_min);
360
361static ssize_t
362store_fan_min(struct device *dev, const char *buf, size_t count, int nr)
363{
364 struct i2c_client *client = to_i2c_client(dev);
365 struct w83781d_data *data = i2c_get_clientdata(client);
366 u32 val;
367
368 val = simple_strtoul(buf, NULL, 10);
369
370 down(&data->update_lock);
371 data->fan_min[nr - 1] =
372 FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr - 1]));
373 w83781d_write_value(client, W83781D_REG_FAN_MIN(nr),
374 data->fan_min[nr - 1]);
375
376 up(&data->update_lock);
377 return count;
378}
379
380#define sysfs_fan_offset(offset) \
381static ssize_t show_regs_fan_##offset (struct device *dev, char *buf) \
382{ \
383 return show_fan(dev, buf, offset); \
384} \
385static DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_regs_fan_##offset, NULL);
386
387#define sysfs_fan_min_offset(offset) \
388static ssize_t show_regs_fan_min##offset (struct device *dev, char *buf) \
389{ \
390 return show_fan_min(dev, buf, offset); \
391} \
392static ssize_t store_regs_fan_min##offset (struct device *dev, const char *buf, size_t count) \
393{ \
394 return store_fan_min(dev, buf, count, offset); \
395} \
396static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, show_regs_fan_min##offset, store_regs_fan_min##offset);
397
398sysfs_fan_offset(1);
399sysfs_fan_min_offset(1);
400sysfs_fan_offset(2);
401sysfs_fan_min_offset(2);
402sysfs_fan_offset(3);
403sysfs_fan_min_offset(3);
404
405#define device_create_file_fan(client, offset) \
406do { \
407device_create_file(&client->dev, &dev_attr_fan##offset##_input); \
408device_create_file(&client->dev, &dev_attr_fan##offset##_min); \
409} while (0)
410
411#define show_temp_reg(reg) \
412static ssize_t show_##reg (struct device *dev, char *buf, int nr) \
413{ \
414 struct w83781d_data *data = w83781d_update_device(dev); \
415 if (nr >= 2) { /* TEMP2 and TEMP3 */ \
416 return sprintf(buf,"%d\n", \
417 LM75_TEMP_FROM_REG(data->reg##_add[nr-2])); \
418 } else { /* TEMP1 */ \
419 return sprintf(buf,"%ld\n", (long)TEMP_FROM_REG(data->reg)); \
420 } \
421}
422show_temp_reg(temp);
423show_temp_reg(temp_max);
424show_temp_reg(temp_max_hyst);
425
426#define store_temp_reg(REG, reg) \
427static ssize_t store_temp_##reg (struct device *dev, const char *buf, size_t count, int nr) \
428{ \
429 struct i2c_client *client = to_i2c_client(dev); \
430 struct w83781d_data *data = i2c_get_clientdata(client); \
431 s32 val; \
432 \
433 val = simple_strtol(buf, NULL, 10); \
434 \
435 down(&data->update_lock); \
436 \
437 if (nr >= 2) { /* TEMP2 and TEMP3 */ \
438 data->temp_##reg##_add[nr-2] = LM75_TEMP_TO_REG(val); \
439 w83781d_write_value(client, W83781D_REG_TEMP_##REG(nr), \
440 data->temp_##reg##_add[nr-2]); \
441 } else { /* TEMP1 */ \
442 data->temp_##reg = TEMP_TO_REG(val); \
443 w83781d_write_value(client, W83781D_REG_TEMP_##REG(nr), \
444 data->temp_##reg); \
445 } \
446 \
447 up(&data->update_lock); \
448 return count; \
449}
450store_temp_reg(OVER, max);
451store_temp_reg(HYST, max_hyst);
452
453#define sysfs_temp_offset(offset) \
454static ssize_t \
455show_regs_temp_##offset (struct device *dev, char *buf) \
456{ \
457 return show_temp(dev, buf, offset); \
458} \
459static DEVICE_ATTR(temp##offset##_input, S_IRUGO, show_regs_temp_##offset, NULL);
460
461#define sysfs_temp_reg_offset(reg, offset) \
462static ssize_t show_regs_temp_##reg##offset (struct device *dev, char *buf) \
463{ \
464 return show_temp_##reg (dev, buf, offset); \
465} \
466static ssize_t store_regs_temp_##reg##offset (struct device *dev, const char *buf, size_t count) \
467{ \
468 return store_temp_##reg (dev, buf, count, offset); \
469} \
470static DEVICE_ATTR(temp##offset##_##reg, S_IRUGO| S_IWUSR, show_regs_temp_##reg##offset, store_regs_temp_##reg##offset);
471
472#define sysfs_temp_offsets(offset) \
473sysfs_temp_offset(offset); \
474sysfs_temp_reg_offset(max, offset); \
475sysfs_temp_reg_offset(max_hyst, offset);
476
477sysfs_temp_offsets(1);
478sysfs_temp_offsets(2);
479sysfs_temp_offsets(3);
480
481#define device_create_file_temp(client, offset) \
482do { \
483device_create_file(&client->dev, &dev_attr_temp##offset##_input); \
484device_create_file(&client->dev, &dev_attr_temp##offset##_max); \
485device_create_file(&client->dev, &dev_attr_temp##offset##_max_hyst); \
486} while (0)
487
488static ssize_t
489show_vid_reg(struct device *dev, char *buf)
490{
491 struct w83781d_data *data = w83781d_update_device(dev);
492 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
493}
494
495static
496DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
497#define device_create_file_vid(client) \
498device_create_file(&client->dev, &dev_attr_cpu0_vid);
499static ssize_t
500show_vrm_reg(struct device *dev, char *buf)
501{
502 struct w83781d_data *data = w83781d_update_device(dev);
503 return sprintf(buf, "%ld\n", (long) data->vrm);
504}
505
506static ssize_t
507store_vrm_reg(struct device *dev, const char *buf, size_t count)
508{
509 struct i2c_client *client = to_i2c_client(dev);
510 struct w83781d_data *data = i2c_get_clientdata(client);
511 u32 val;
512
513 val = simple_strtoul(buf, NULL, 10);
514 data->vrm = val;
515
516 return count;
517}
518
519static
520DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
521#define device_create_file_vrm(client) \
522device_create_file(&client->dev, &dev_attr_vrm);
523static ssize_t
524show_alarms_reg(struct device *dev, char *buf)
525{
526 struct w83781d_data *data = w83781d_update_device(dev);
527 return sprintf(buf, "%ld\n", (long) ALARMS_FROM_REG(data->alarms));
528}
529
530static
531DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL);
532#define device_create_file_alarms(client) \
533device_create_file(&client->dev, &dev_attr_alarms);
534static ssize_t show_beep_mask (struct device *dev, char *buf)
535{
536 struct w83781d_data *data = w83781d_update_device(dev);
537 return sprintf(buf, "%ld\n",
538 (long)BEEP_MASK_FROM_REG(data->beep_mask, data->type));
539}
540static ssize_t show_beep_enable (struct device *dev, char *buf)
541{
542 struct w83781d_data *data = w83781d_update_device(dev);
543 return sprintf(buf, "%ld\n",
544 (long)BEEP_ENABLE_FROM_REG(data->beep_enable));
545}
546
547#define BEEP_ENABLE 0 /* Store beep_enable */
548#define BEEP_MASK 1 /* Store beep_mask */
549
550static ssize_t
551store_beep_reg(struct device *dev, const char *buf, size_t count,
552 int update_mask)
553{
554 struct i2c_client *client = to_i2c_client(dev);
555 struct w83781d_data *data = i2c_get_clientdata(client);
556 u32 val, val2;
557
558 val = simple_strtoul(buf, NULL, 10);
559
560 down(&data->update_lock);
561
562 if (update_mask == BEEP_MASK) { /* We are storing beep_mask */
563 data->beep_mask = BEEP_MASK_TO_REG(val, data->type);
564 w83781d_write_value(client, W83781D_REG_BEEP_INTS1,
565 data->beep_mask & 0xff);
566
567 if ((data->type != w83781d) && (data->type != as99127f)) {
568 w83781d_write_value(client, W83781D_REG_BEEP_INTS3,
569 ((data->beep_mask) >> 16) & 0xff);
570 }
571
572 val2 = (data->beep_mask >> 8) & 0x7f;
573 } else { /* We are storing beep_enable */
574 val2 = w83781d_read_value(client, W83781D_REG_BEEP_INTS2) & 0x7f;
575 data->beep_enable = BEEP_ENABLE_TO_REG(val);
576 }
577
578 w83781d_write_value(client, W83781D_REG_BEEP_INTS2,
579 val2 | data->beep_enable << 7);
580
581 up(&data->update_lock);
582 return count;
583}
584
585#define sysfs_beep(REG, reg) \
586static ssize_t show_regs_beep_##reg (struct device *dev, char *buf) \
587{ \
588 return show_beep_##reg(dev, buf); \
589} \
590static ssize_t store_regs_beep_##reg (struct device *dev, const char *buf, size_t count) \
591{ \
592 return store_beep_reg(dev, buf, count, BEEP_##REG); \
593} \
594static DEVICE_ATTR(beep_##reg, S_IRUGO | S_IWUSR, show_regs_beep_##reg, store_regs_beep_##reg);
595
596sysfs_beep(ENABLE, enable);
597sysfs_beep(MASK, mask);
598
599#define device_create_file_beep(client) \
600do { \
601device_create_file(&client->dev, &dev_attr_beep_enable); \
602device_create_file(&client->dev, &dev_attr_beep_mask); \
603} while (0)
604
605static ssize_t
606show_fan_div_reg(struct device *dev, char *buf, int nr)
607{
608 struct w83781d_data *data = w83781d_update_device(dev);
609 return sprintf(buf, "%ld\n",
610 (long) DIV_FROM_REG(data->fan_div[nr - 1]));
611}
612
613/* Note: we save and restore the fan minimum here, because its value is
614 determined in part by the fan divisor. This follows the principle of
615 least suprise; the user doesn't expect the fan minimum to change just
616 because the divisor changed. */
617static ssize_t
618store_fan_div_reg(struct device *dev, const char *buf, size_t count, int nr)
619{
620 struct i2c_client *client = to_i2c_client(dev);
621 struct w83781d_data *data = i2c_get_clientdata(client);
622 unsigned long min;
623 u8 reg;
624 unsigned long val = simple_strtoul(buf, NULL, 10);
625
626 down(&data->update_lock);
627
628 /* Save fan_min */
629 min = FAN_FROM_REG(data->fan_min[nr],
630 DIV_FROM_REG(data->fan_div[nr]));
631
632 data->fan_div[nr] = DIV_TO_REG(val, data->type);
633
634 reg = (w83781d_read_value(client, nr==2 ? W83781D_REG_PIN : W83781D_REG_VID_FANDIV)
635 & (nr==0 ? 0xcf : 0x3f))
636 | ((data->fan_div[nr] & 0x03) << (nr==0 ? 4 : 6));
637 w83781d_write_value(client, nr==2 ? W83781D_REG_PIN : W83781D_REG_VID_FANDIV, reg);
638
639 /* w83781d and as99127f don't have extended divisor bits */
640 if (data->type != w83781d && data->type != as99127f) {
641 reg = (w83781d_read_value(client, W83781D_REG_VBAT)
642 & ~(1 << (5 + nr)))
643 | ((data->fan_div[nr] & 0x04) << (3 + nr));
644 w83781d_write_value(client, W83781D_REG_VBAT, reg);
645 }
646
647 /* Restore fan_min */
648 data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
649 w83781d_write_value(client, W83781D_REG_FAN_MIN(nr+1), data->fan_min[nr]);
650
651 up(&data->update_lock);
652 return count;
653}
654
655#define sysfs_fan_div(offset) \
656static ssize_t show_regs_fan_div_##offset (struct device *dev, char *buf) \
657{ \
658 return show_fan_div_reg(dev, buf, offset); \
659} \
660static ssize_t store_regs_fan_div_##offset (struct device *dev, const char *buf, size_t count) \
661{ \
662 return store_fan_div_reg(dev, buf, count, offset - 1); \
663} \
664static DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, show_regs_fan_div_##offset, store_regs_fan_div_##offset);
665
666sysfs_fan_div(1);
667sysfs_fan_div(2);
668sysfs_fan_div(3);
669
670#define device_create_file_fan_div(client, offset) \
671do { \
672device_create_file(&client->dev, &dev_attr_fan##offset##_div); \
673} while (0)
674
675static ssize_t
676show_pwm_reg(struct device *dev, char *buf, int nr)
677{
678 struct w83781d_data *data = w83781d_update_device(dev);
679 return sprintf(buf, "%ld\n", (long) PWM_FROM_REG(data->pwm[nr - 1]));
680}
681
682static ssize_t
683show_pwmenable_reg(struct device *dev, char *buf, int nr)
684{
685 struct w83781d_data *data = w83781d_update_device(dev);
686 return sprintf(buf, "%ld\n", (long) data->pwmenable[nr - 1]);
687}
688
689static ssize_t
690store_pwm_reg(struct device *dev, const char *buf, size_t count, int nr)
691{
692 struct i2c_client *client = to_i2c_client(dev);
693 struct w83781d_data *data = i2c_get_clientdata(client);
694 u32 val;
695
696 val = simple_strtoul(buf, NULL, 10);
697
698 down(&data->update_lock);
699 data->pwm[nr - 1] = PWM_TO_REG(val);
700 w83781d_write_value(client, W83781D_REG_PWM(nr), data->pwm[nr - 1]);
701 up(&data->update_lock);
702 return count;
703}
704
705static ssize_t
706store_pwmenable_reg(struct device *dev, const char *buf, size_t count, int nr)
707{
708 struct i2c_client *client = to_i2c_client(dev);
709 struct w83781d_data *data = i2c_get_clientdata(client);
710 u32 val, reg;
711
712 val = simple_strtoul(buf, NULL, 10);
713
714 down(&data->update_lock);
715
716 switch (val) {
717 case 0:
718 case 1:
719 reg = w83781d_read_value(client, W83781D_REG_PWMCLK12);
720 w83781d_write_value(client, W83781D_REG_PWMCLK12,
721 (reg & 0xf7) | (val << 3));
722
723 reg = w83781d_read_value(client, W83781D_REG_BEEP_CONFIG);
724 w83781d_write_value(client, W83781D_REG_BEEP_CONFIG,
725 (reg & 0xef) | (!val << 4));
726
727 data->pwmenable[nr - 1] = val;
728 break;
729
730 default:
731 up(&data->update_lock);
732 return -EINVAL;
733 }
734
735 up(&data->update_lock);
736 return count;
737}
738
739#define sysfs_pwm(offset) \
740static ssize_t show_regs_pwm_##offset (struct device *dev, char *buf) \
741{ \
742 return show_pwm_reg(dev, buf, offset); \
743} \
744static ssize_t store_regs_pwm_##offset (struct device *dev, \
745 const char *buf, size_t count) \
746{ \
747 return store_pwm_reg(dev, buf, count, offset); \
748} \
749static DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
750 show_regs_pwm_##offset, store_regs_pwm_##offset);
751
752#define sysfs_pwmenable(offset) \
753static ssize_t show_regs_pwmenable_##offset (struct device *dev, char *buf) \
754{ \
755 return show_pwmenable_reg(dev, buf, offset); \
756} \
757static ssize_t store_regs_pwmenable_##offset (struct device *dev, \
758 const char *buf, size_t count) \
759{ \
760 return store_pwmenable_reg(dev, buf, count, offset); \
761} \
762static DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
763 show_regs_pwmenable_##offset, store_regs_pwmenable_##offset);
764
765sysfs_pwm(1);
766sysfs_pwm(2);
767sysfs_pwmenable(2); /* only PWM2 can be enabled/disabled */
768sysfs_pwm(3);
769sysfs_pwm(4);
770
771#define device_create_file_pwm(client, offset) \
772do { \
773device_create_file(&client->dev, &dev_attr_pwm##offset); \
774} while (0)
775
776#define device_create_file_pwmenable(client, offset) \
777do { \
778device_create_file(&client->dev, &dev_attr_pwm##offset##_enable); \
779} while (0)
780
781static ssize_t
782show_sensor_reg(struct device *dev, char *buf, int nr)
783{
784 struct w83781d_data *data = w83781d_update_device(dev);
785 return sprintf(buf, "%ld\n", (long) data->sens[nr - 1]);
786}
787
788static ssize_t
789store_sensor_reg(struct device *dev, const char *buf, size_t count, int nr)
790{
791 struct i2c_client *client = to_i2c_client(dev);
792 struct w83781d_data *data = i2c_get_clientdata(client);
793 u32 val, tmp;
794
795 val = simple_strtoul(buf, NULL, 10);
796
797 down(&data->update_lock);
798
799 switch (val) {
800 case 1: /* PII/Celeron diode */
801 tmp = w83781d_read_value(client, W83781D_REG_SCFG1);
802 w83781d_write_value(client, W83781D_REG_SCFG1,
803 tmp | BIT_SCFG1[nr - 1]);
804 tmp = w83781d_read_value(client, W83781D_REG_SCFG2);
805 w83781d_write_value(client, W83781D_REG_SCFG2,
806 tmp | BIT_SCFG2[nr - 1]);
807 data->sens[nr - 1] = val;
808 break;
809 case 2: /* 3904 */
810 tmp = w83781d_read_value(client, W83781D_REG_SCFG1);
811 w83781d_write_value(client, W83781D_REG_SCFG1,
812 tmp | BIT_SCFG1[nr - 1]);
813 tmp = w83781d_read_value(client, W83781D_REG_SCFG2);
814 w83781d_write_value(client, W83781D_REG_SCFG2,
815 tmp & ~BIT_SCFG2[nr - 1]);
816 data->sens[nr - 1] = val;
817 break;
818 case W83781D_DEFAULT_BETA: /* thermistor */
819 tmp = w83781d_read_value(client, W83781D_REG_SCFG1);
820 w83781d_write_value(client, W83781D_REG_SCFG1,
821 tmp & ~BIT_SCFG1[nr - 1]);
822 data->sens[nr - 1] = val;
823 break;
824 default:
825 dev_err(dev, "Invalid sensor type %ld; must be 1, 2, or %d\n",
826 (long) val, W83781D_DEFAULT_BETA);
827 break;
828 }
829
830 up(&data->update_lock);
831 return count;
832}
833
834#define sysfs_sensor(offset) \
835static ssize_t show_regs_sensor_##offset (struct device *dev, char *buf) \
836{ \
837 return show_sensor_reg(dev, buf, offset); \
838} \
839static ssize_t store_regs_sensor_##offset (struct device *dev, const char *buf, size_t count) \
840{ \
841 return store_sensor_reg(dev, buf, count, offset); \
842} \
843static DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR, show_regs_sensor_##offset, store_regs_sensor_##offset);
844
845sysfs_sensor(1);
846sysfs_sensor(2);
847sysfs_sensor(3);
848
849#define device_create_file_sensor(client, offset) \
850do { \
851device_create_file(&client->dev, &dev_attr_temp##offset##_type); \
852} while (0)
853
854/* This function is called when:
855 * w83781d_driver is inserted (when this module is loaded), for each
856 available adapter
857 * when a new adapter is inserted (and w83781d_driver is still present) */
858static int
859w83781d_attach_adapter(struct i2c_adapter *adapter)
860{
861 if (!(adapter->class & I2C_CLASS_HWMON))
862 return 0;
863 return i2c_detect(adapter, &addr_data, w83781d_detect);
864}
865
866/* Assumes that adapter is of I2C, not ISA variety.
867 * OTHERWISE DON'T CALL THIS
868 */
869static int
870w83781d_detect_subclients(struct i2c_adapter *adapter, int address, int kind,
871 struct i2c_client *new_client)
872{
873 int i, val1 = 0, id;
874 int err;
875 const char *client_name = "";
876 struct w83781d_data *data = i2c_get_clientdata(new_client);
877
878 data->lm75[0] = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
879 if (!(data->lm75[0])) {
880 err = -ENOMEM;
881 goto ERROR_SC_0;
882 }
883 memset(data->lm75[0], 0x00, sizeof (struct i2c_client));
884
885 id = i2c_adapter_id(adapter);
886
887 if (force_subclients[0] == id && force_subclients[1] == address) {
888 for (i = 2; i <= 3; i++) {
889 if (force_subclients[i] < 0x48 ||
890 force_subclients[i] > 0x4f) {
891 dev_err(&new_client->dev, "Invalid subclient "
892 "address %d; must be 0x48-0x4f\n",
893 force_subclients[i]);
894 err = -EINVAL;
895 goto ERROR_SC_1;
896 }
897 }
898 w83781d_write_value(new_client, W83781D_REG_I2C_SUBADDR,
899 (force_subclients[2] & 0x07) |
900 ((force_subclients[3] & 0x07) << 4));
901 data->lm75[0]->addr = force_subclients[2];
902 } else {
903 val1 = w83781d_read_value(new_client, W83781D_REG_I2C_SUBADDR);
904 data->lm75[0]->addr = 0x48 + (val1 & 0x07);
905 }
906
907 if (kind != w83783s) {
908
909 data->lm75[1] = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
910 if (!(data->lm75[1])) {
911 err = -ENOMEM;
912 goto ERROR_SC_1;
913 }
914 memset(data->lm75[1], 0x0, sizeof(struct i2c_client));
915
916 if (force_subclients[0] == id &&
917 force_subclients[1] == address) {
918 data->lm75[1]->addr = force_subclients[3];
919 } else {
920 data->lm75[1]->addr = 0x48 + ((val1 >> 4) & 0x07);
921 }
922 if (data->lm75[0]->addr == data->lm75[1]->addr) {
923 dev_err(&new_client->dev,
924 "Duplicate addresses 0x%x for subclients.\n",
925 data->lm75[0]->addr);
926 err = -EBUSY;
927 goto ERROR_SC_2;
928 }
929 }
930
931 if (kind == w83781d)
932 client_name = "w83781d subclient";
933 else if (kind == w83782d)
934 client_name = "w83782d subclient";
935 else if (kind == w83783s)
936 client_name = "w83783s subclient";
937 else if (kind == w83627hf)
938 client_name = "w83627hf subclient";
939 else if (kind == as99127f)
940 client_name = "as99127f subclient";
941
942 for (i = 0; i <= 1; i++) {
943 /* store all data in w83781d */
944 i2c_set_clientdata(data->lm75[i], NULL);
945 data->lm75[i]->adapter = adapter;
946 data->lm75[i]->driver = &w83781d_driver;
947 data->lm75[i]->flags = 0;
948 strlcpy(data->lm75[i]->name, client_name,
949 I2C_NAME_SIZE);
950 if ((err = i2c_attach_client(data->lm75[i]))) {
951 dev_err(&new_client->dev, "Subclient %d "
952 "registration at address 0x%x "
953 "failed.\n", i, data->lm75[i]->addr);
954 if (i == 1)
955 goto ERROR_SC_3;
956 goto ERROR_SC_2;
957 }
958 if (kind == w83783s)
959 break;
960 }
961
962 return 0;
963
964/* Undo inits in case of errors */
965ERROR_SC_3:
966 i2c_detach_client(data->lm75[0]);
967ERROR_SC_2:
968 if (NULL != data->lm75[1])
969 kfree(data->lm75[1]);
970ERROR_SC_1:
971 if (NULL != data->lm75[0])
972 kfree(data->lm75[0]);
973ERROR_SC_0:
974 return err;
975}
976
977static int
978w83781d_detect(struct i2c_adapter *adapter, int address, int kind)
979{
980 int i = 0, val1 = 0, val2;
981 struct i2c_client *new_client;
982 struct w83781d_data *data;
983 int err;
984 const char *client_name = "";
985 int is_isa = i2c_is_isa_adapter(adapter);
986 enum vendor { winbond, asus } vendid;
987
988 if (!is_isa
989 && !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
990 err = -EINVAL;
991 goto ERROR0;
992 }
993
994 /* Prevent users from forcing a kind for a bus it isn't supposed
995 to possibly be on */
996 if (is_isa && (kind == as99127f || kind == w83783s)) {
997 dev_err(&adapter->dev,
998 "Cannot force I2C-only chip for ISA address 0x%02x.\n",
999 address);
1000 err = -EINVAL;
1001 goto ERROR0;
1002 }
1003 if (!is_isa && kind == w83697hf) {
1004 dev_err(&adapter->dev,
1005 "Cannot force ISA-only chip for I2C address 0x%02x.\n",
1006 address);
1007 err = -EINVAL;
1008 goto ERROR0;
1009 }
1010
1011 if (is_isa)
1012 if (!request_region(address, W83781D_EXTENT,
1013 w83781d_driver.name)) {
1014 dev_dbg(&adapter->dev, "Request of region "
1015 "0x%x-0x%x for w83781d failed\n", address,
1016 address + W83781D_EXTENT - 1);
1017 err = -EBUSY;
1018 goto ERROR0;
1019 }
1020
1021 /* Probe whether there is anything available on this address. Already
1022 done for SMBus clients */
1023 if (kind < 0) {
1024 if (is_isa) {
1025
1026#define REALLY_SLOW_IO
1027 /* We need the timeouts for at least some LM78-like
1028 chips. But only if we read 'undefined' registers. */
1029 i = inb_p(address + 1);
1030 if (inb_p(address + 2) != i
1031 || inb_p(address + 3) != i
1032 || inb_p(address + 7) != i) {
1033 dev_dbg(&adapter->dev, "Detection of w83781d "
1034 "chip failed at step 1\n");
1035 err = -ENODEV;
1036 goto ERROR1;
1037 }
1038#undef REALLY_SLOW_IO
1039
1040 /* Let's just hope nothing breaks here */
1041 i = inb_p(address + 5) & 0x7f;
1042 outb_p(~i & 0x7f, address + 5);
1043 val2 = inb_p(address + 5) & 0x7f;
1044 if (val2 != (~i & 0x7f)) {
1045 outb_p(i, address + 5);
1046 dev_dbg(&adapter->dev, "Detection of w83781d "
1047 "chip failed at step 2 (0x%x != "
1048 "0x%x at 0x%x)\n", val2, ~i & 0x7f,
1049 address + 5);
1050 err = -ENODEV;
1051 goto ERROR1;
1052 }
1053 }
1054 }
1055
1056 /* OK. For now, we presume we have a valid client. We now create the
1057 client structure, even though we cannot fill it completely yet.
1058 But it allows us to access w83781d_{read,write}_value. */
1059
1060 if (!(data = kmalloc(sizeof(struct w83781d_data), GFP_KERNEL))) {
1061 err = -ENOMEM;
1062 goto ERROR1;
1063 }
1064 memset(data, 0, sizeof(struct w83781d_data));
1065
1066 new_client = &data->client;
1067 i2c_set_clientdata(new_client, data);
1068 new_client->addr = address;
1069 init_MUTEX(&data->lock);
1070 new_client->adapter = adapter;
1071 new_client->driver = &w83781d_driver;
1072 new_client->flags = 0;
1073
1074 /* Now, we do the remaining detection. */
1075
1076 /* The w8378?d may be stuck in some other bank than bank 0. This may
1077 make reading other information impossible. Specify a force=... or
1078 force_*=... parameter, and the Winbond will be reset to the right
1079 bank. */
1080 if (kind < 0) {
1081 if (w83781d_read_value(new_client, W83781D_REG_CONFIG) & 0x80) {
1082 dev_dbg(&new_client->dev, "Detection failed at step "
1083 "3\n");
1084 err = -ENODEV;
1085 goto ERROR2;
1086 }
1087 val1 = w83781d_read_value(new_client, W83781D_REG_BANK);
1088 val2 = w83781d_read_value(new_client, W83781D_REG_CHIPMAN);
1089 /* Check for Winbond or Asus ID if in bank 0 */
1090 if ((!(val1 & 0x07)) &&
1091 (((!(val1 & 0x80)) && (val2 != 0xa3) && (val2 != 0xc3))
1092 || ((val1 & 0x80) && (val2 != 0x5c) && (val2 != 0x12)))) {
1093 dev_dbg(&new_client->dev, "Detection failed at step "
1094 "4\n");
1095 err = -ENODEV;
1096 goto ERROR2;
1097 }
1098 /* If Winbond SMBus, check address at 0x48.
1099 Asus doesn't support, except for as99127f rev.2 */
1100 if ((!is_isa) && (((!(val1 & 0x80)) && (val2 == 0xa3)) ||
1101 ((val1 & 0x80) && (val2 == 0x5c)))) {
1102 if (w83781d_read_value
1103 (new_client, W83781D_REG_I2C_ADDR) != address) {
1104 dev_dbg(&new_client->dev, "Detection failed "
1105 "at step 5\n");
1106 err = -ENODEV;
1107 goto ERROR2;
1108 }
1109 }
1110 }
1111
1112 /* We have either had a force parameter, or we have already detected the
1113 Winbond. Put it now into bank 0 and Vendor ID High Byte */
1114 w83781d_write_value(new_client, W83781D_REG_BANK,
1115 (w83781d_read_value(new_client,
1116 W83781D_REG_BANK) & 0x78) |
1117 0x80);
1118
1119 /* Determine the chip type. */
1120 if (kind <= 0) {
1121 /* get vendor ID */
1122 val2 = w83781d_read_value(new_client, W83781D_REG_CHIPMAN);
1123 if (val2 == 0x5c)
1124 vendid = winbond;
1125 else if (val2 == 0x12)
1126 vendid = asus;
1127 else {
1128 dev_dbg(&new_client->dev, "Chip was made by neither "
1129 "Winbond nor Asus?\n");
1130 err = -ENODEV;
1131 goto ERROR2;
1132 }
1133
1134 val1 = w83781d_read_value(new_client, W83781D_REG_WCHIPID);
1135 if ((val1 == 0x10 || val1 == 0x11) && vendid == winbond)
1136 kind = w83781d;
1137 else if (val1 == 0x30 && vendid == winbond)
1138 kind = w83782d;
1139 else if (val1 == 0x40 && vendid == winbond && !is_isa
1140 && address == 0x2d)
1141 kind = w83783s;
1142 else if ((val1 == 0x21 || val1 == 0x90) && vendid == winbond)
1143 kind = w83627hf;
1144 else if (val1 == 0x31 && !is_isa && address >= 0x28)
1145 kind = as99127f;
1146 else if (val1 == 0x60 && vendid == winbond && is_isa)
1147 kind = w83697hf;
1148 else {
1149 if (kind == 0)
1150 dev_warn(&new_client->dev, "Ignoring 'force' "
1151 "parameter for unknown chip at "
1152 "adapter %d, address 0x%02x\n",
1153 i2c_adapter_id(adapter), address);
1154 err = -EINVAL;
1155 goto ERROR2;
1156 }
1157 }
1158
1159 if (kind == w83781d) {
1160 client_name = "w83781d";
1161 } else if (kind == w83782d) {
1162 client_name = "w83782d";
1163 } else if (kind == w83783s) {
1164 client_name = "w83783s";
1165 } else if (kind == w83627hf) {
1166 if (val1 == 0x90)
1167 client_name = "w83627thf";
1168 else
1169 client_name = "w83627hf";
1170 } else if (kind == as99127f) {
1171 client_name = "as99127f";
1172 } else if (kind == w83697hf) {
1173 client_name = "w83697hf";
1174 }
1175
1176 /* Fill in the remaining client fields and put into the global list */
1177 strlcpy(new_client->name, client_name, I2C_NAME_SIZE);
1178 data->type = kind;
1179
1180 data->valid = 0;
1181 init_MUTEX(&data->update_lock);
1182
1183 /* Tell the I2C layer a new client has arrived */
1184 if ((err = i2c_attach_client(new_client)))
1185 goto ERROR2;
1186
1187 /* attach secondary i2c lm75-like clients */
1188 if (!is_isa) {
1189 if ((err = w83781d_detect_subclients(adapter, address,
1190 kind, new_client)))
1191 goto ERROR3;
1192 } else {
1193 data->lm75[0] = NULL;
1194 data->lm75[1] = NULL;
1195 }
1196
1197 /* Initialize the chip */
1198 w83781d_init_client(new_client);
1199
1200 /* A few vars need to be filled upon startup */
1201 for (i = 1; i <= 3; i++) {
1202 data->fan_min[i - 1] = w83781d_read_value(new_client,
1203 W83781D_REG_FAN_MIN(i));
1204 }
1205 if (kind != w83781d && kind != as99127f)
1206 for (i = 0; i < 4; i++)
1207 data->pwmenable[i] = 1;
1208
1209 /* Register sysfs hooks */
1210 device_create_file_in(new_client, 0);
1211 if (kind != w83783s && kind != w83697hf)
1212 device_create_file_in(new_client, 1);
1213 device_create_file_in(new_client, 2);
1214 device_create_file_in(new_client, 3);
1215 device_create_file_in(new_client, 4);
1216 device_create_file_in(new_client, 5);
1217 device_create_file_in(new_client, 6);
1218 if (kind != as99127f && kind != w83781d && kind != w83783s) {
1219 device_create_file_in(new_client, 7);
1220 device_create_file_in(new_client, 8);
1221 }
1222
1223 device_create_file_fan(new_client, 1);
1224 device_create_file_fan(new_client, 2);
1225 if (kind != w83697hf)
1226 device_create_file_fan(new_client, 3);
1227
1228 device_create_file_temp(new_client, 1);
1229 device_create_file_temp(new_client, 2);
1230 if (kind != w83783s && kind != w83697hf)
1231 device_create_file_temp(new_client, 3);
1232
1233 if (kind != w83697hf)
1234 device_create_file_vid(new_client);
1235
1236 if (kind != w83697hf)
1237 device_create_file_vrm(new_client);
1238
1239 device_create_file_fan_div(new_client, 1);
1240 device_create_file_fan_div(new_client, 2);
1241 if (kind != w83697hf)
1242 device_create_file_fan_div(new_client, 3);
1243
1244 device_create_file_alarms(new_client);
1245
1246 device_create_file_beep(new_client);
1247
1248 if (kind != w83781d && kind != as99127f) {
1249 device_create_file_pwm(new_client, 1);
1250 device_create_file_pwm(new_client, 2);
1251 device_create_file_pwmenable(new_client, 2);
1252 }
1253 if (kind == w83782d && !is_isa) {
1254 device_create_file_pwm(new_client, 3);
1255 device_create_file_pwm(new_client, 4);
1256 }
1257
1258 if (kind != as99127f && kind != w83781d) {
1259 device_create_file_sensor(new_client, 1);
1260 device_create_file_sensor(new_client, 2);
1261 if (kind != w83783s && kind != w83697hf)
1262 device_create_file_sensor(new_client, 3);
1263 }
1264
1265 return 0;
1266
1267ERROR3:
1268 i2c_detach_client(new_client);
1269ERROR2:
1270 kfree(data);
1271ERROR1:
1272 if (is_isa)
1273 release_region(address, W83781D_EXTENT);
1274ERROR0:
1275 return err;
1276}
1277
1278static int
1279w83781d_detach_client(struct i2c_client *client)
1280{
1281 int err;
1282
1283 if (i2c_is_isa_client(client))
1284 release_region(client->addr, W83781D_EXTENT);
1285
1286 if ((err = i2c_detach_client(client))) {
1287 dev_err(&client->dev,
1288 "Client deregistration failed, client not detached.\n");
1289 return err;
1290 }
1291
1292 if (i2c_get_clientdata(client)==NULL) {
1293 /* subclients */
1294 kfree(client);
1295 } else {
1296 /* main client */
1297 kfree(i2c_get_clientdata(client));
1298 }
1299
1300 return 0;
1301}
1302
1303/* The SMBus locks itself, usually, but nothing may access the Winbond between
1304 bank switches. ISA access must always be locked explicitly!
1305 We ignore the W83781D BUSY flag at this moment - it could lead to deadlocks,
1306 would slow down the W83781D access and should not be necessary.
1307 There are some ugly typecasts here, but the good news is - they should
1308 nowhere else be necessary! */
1309static int
1310w83781d_read_value(struct i2c_client *client, u16 reg)
1311{
1312 struct w83781d_data *data = i2c_get_clientdata(client);
1313 int res, word_sized, bank;
1314 struct i2c_client *cl;
1315
1316 down(&data->lock);
1317 if (i2c_is_isa_client(client)) {
1318 word_sized = (((reg & 0xff00) == 0x100)
1319 || ((reg & 0xff00) == 0x200))
1320 && (((reg & 0x00ff) == 0x50)
1321 || ((reg & 0x00ff) == 0x53)
1322 || ((reg & 0x00ff) == 0x55));
1323 if (reg & 0xff00) {
1324 outb_p(W83781D_REG_BANK,
1325 client->addr + W83781D_ADDR_REG_OFFSET);
1326 outb_p(reg >> 8,
1327 client->addr + W83781D_DATA_REG_OFFSET);
1328 }
1329 outb_p(reg & 0xff, client->addr + W83781D_ADDR_REG_OFFSET);
1330 res = inb_p(client->addr + W83781D_DATA_REG_OFFSET);
1331 if (word_sized) {
1332 outb_p((reg & 0xff) + 1,
1333 client->addr + W83781D_ADDR_REG_OFFSET);
1334 res =
1335 (res << 8) + inb_p(client->addr +
1336 W83781D_DATA_REG_OFFSET);
1337 }
1338 if (reg & 0xff00) {
1339 outb_p(W83781D_REG_BANK,
1340 client->addr + W83781D_ADDR_REG_OFFSET);
1341 outb_p(0, client->addr + W83781D_DATA_REG_OFFSET);
1342 }
1343 } else {
1344 bank = (reg >> 8) & 0x0f;
1345 if (bank > 2)
1346 /* switch banks */
1347 i2c_smbus_write_byte_data(client, W83781D_REG_BANK,
1348 bank);
1349 if (bank == 0 || bank > 2) {
1350 res = i2c_smbus_read_byte_data(client, reg & 0xff);
1351 } else {
1352 /* switch to subclient */
1353 cl = data->lm75[bank - 1];
1354 /* convert from ISA to LM75 I2C addresses */
1355 switch (reg & 0xff) {
1356 case 0x50: /* TEMP */
1357 res = swab16(i2c_smbus_read_word_data(cl, 0));
1358 break;
1359 case 0x52: /* CONFIG */
1360 res = i2c_smbus_read_byte_data(cl, 1);
1361 break;
1362 case 0x53: /* HYST */
1363 res = swab16(i2c_smbus_read_word_data(cl, 2));
1364 break;
1365 case 0x55: /* OVER */
1366 default:
1367 res = swab16(i2c_smbus_read_word_data(cl, 3));
1368 break;
1369 }
1370 }
1371 if (bank > 2)
1372 i2c_smbus_write_byte_data(client, W83781D_REG_BANK, 0);
1373 }
1374 up(&data->lock);
1375 return res;
1376}
1377
1378static int
1379w83781d_write_value(struct i2c_client *client, u16 reg, u16 value)
1380{
1381 struct w83781d_data *data = i2c_get_clientdata(client);
1382 int word_sized, bank;
1383 struct i2c_client *cl;
1384
1385 down(&data->lock);
1386 if (i2c_is_isa_client(client)) {
1387 word_sized = (((reg & 0xff00) == 0x100)
1388 || ((reg & 0xff00) == 0x200))
1389 && (((reg & 0x00ff) == 0x53)
1390 || ((reg & 0x00ff) == 0x55));
1391 if (reg & 0xff00) {
1392 outb_p(W83781D_REG_BANK,
1393 client->addr + W83781D_ADDR_REG_OFFSET);
1394 outb_p(reg >> 8,
1395 client->addr + W83781D_DATA_REG_OFFSET);
1396 }
1397 outb_p(reg & 0xff, client->addr + W83781D_ADDR_REG_OFFSET);
1398 if (word_sized) {
1399 outb_p(value >> 8,
1400 client->addr + W83781D_DATA_REG_OFFSET);
1401 outb_p((reg & 0xff) + 1,
1402 client->addr + W83781D_ADDR_REG_OFFSET);
1403 }
1404 outb_p(value & 0xff, client->addr + W83781D_DATA_REG_OFFSET);
1405 if (reg & 0xff00) {
1406 outb_p(W83781D_REG_BANK,
1407 client->addr + W83781D_ADDR_REG_OFFSET);
1408 outb_p(0, client->addr + W83781D_DATA_REG_OFFSET);
1409 }
1410 } else {
1411 bank = (reg >> 8) & 0x0f;
1412 if (bank > 2)
1413 /* switch banks */
1414 i2c_smbus_write_byte_data(client, W83781D_REG_BANK,
1415 bank);
1416 if (bank == 0 || bank > 2) {
1417 i2c_smbus_write_byte_data(client, reg & 0xff,
1418 value & 0xff);
1419 } else {
1420 /* switch to subclient */
1421 cl = data->lm75[bank - 1];
1422 /* convert from ISA to LM75 I2C addresses */
1423 switch (reg & 0xff) {
1424 case 0x52: /* CONFIG */
1425 i2c_smbus_write_byte_data(cl, 1, value & 0xff);
1426 break;
1427 case 0x53: /* HYST */
1428 i2c_smbus_write_word_data(cl, 2, swab16(value));
1429 break;
1430 case 0x55: /* OVER */
1431 i2c_smbus_write_word_data(cl, 3, swab16(value));
1432 break;
1433 }
1434 }
1435 if (bank > 2)
1436 i2c_smbus_write_byte_data(client, W83781D_REG_BANK, 0);
1437 }
1438 up(&data->lock);
1439 return 0;
1440}
1441
1442/* Called when we have found a new W83781D. It should set limits, etc. */
1443static void
1444w83781d_init_client(struct i2c_client *client)
1445{
1446 struct w83781d_data *data = i2c_get_clientdata(client);
1447 int i, p;
1448 int type = data->type;
1449 u8 tmp;
1450
1451 if (init && type != as99127f) { /* this resets registers we don't have
1452 documentation for on the as99127f */
1453 /* save these registers */
1454 i = w83781d_read_value(client, W83781D_REG_BEEP_CONFIG);
1455 p = w83781d_read_value(client, W83781D_REG_PWMCLK12);
1456 /* Reset all except Watchdog values and last conversion values
1457 This sets fan-divs to 2, among others */
1458 w83781d_write_value(client, W83781D_REG_CONFIG, 0x80);
1459 /* Restore the registers and disable power-on abnormal beep.
1460 This saves FAN 1/2/3 input/output values set by BIOS. */
1461 w83781d_write_value(client, W83781D_REG_BEEP_CONFIG, i | 0x80);
1462 w83781d_write_value(client, W83781D_REG_PWMCLK12, p);
1463 /* Disable master beep-enable (reset turns it on).
1464 Individual beep_mask should be reset to off but for some reason
1465 disabling this bit helps some people not get beeped */
1466 w83781d_write_value(client, W83781D_REG_BEEP_INTS2, 0);
1467 }
1468
1469 data->vrm = i2c_which_vrm();
1470
1471 if ((type != w83781d) && (type != as99127f)) {
1472 tmp = w83781d_read_value(client, W83781D_REG_SCFG1);
1473 for (i = 1; i <= 3; i++) {
1474 if (!(tmp & BIT_SCFG1[i - 1])) {
1475 data->sens[i - 1] = W83781D_DEFAULT_BETA;
1476 } else {
1477 if (w83781d_read_value
1478 (client,
1479 W83781D_REG_SCFG2) & BIT_SCFG2[i - 1])
1480 data->sens[i - 1] = 1;
1481 else
1482 data->sens[i - 1] = 2;
1483 }
1484 if ((type == w83783s || type == w83697hf) && (i == 2))
1485 break;
1486 }
1487 }
1488
1489 if (init && type != as99127f) {
1490 /* Enable temp2 */
1491 tmp = w83781d_read_value(client, W83781D_REG_TEMP2_CONFIG);
1492 if (tmp & 0x01) {
1493 dev_warn(&client->dev, "Enabling temp2, readings "
1494 "might not make sense\n");
1495 w83781d_write_value(client, W83781D_REG_TEMP2_CONFIG,
1496 tmp & 0xfe);
1497 }
1498
1499 /* Enable temp3 */
1500 if (type != w83783s && type != w83697hf) {
1501 tmp = w83781d_read_value(client,
1502 W83781D_REG_TEMP3_CONFIG);
1503 if (tmp & 0x01) {
1504 dev_warn(&client->dev, "Enabling temp3, "
1505 "readings might not make sense\n");
1506 w83781d_write_value(client,
1507 W83781D_REG_TEMP3_CONFIG, tmp & 0xfe);
1508 }
1509 }
1510
1511 if (type != w83781d) {
1512 /* enable comparator mode for temp2 and temp3 so
1513 alarm indication will work correctly */
1514 i = w83781d_read_value(client, W83781D_REG_IRQ);
1515 if (!(i & 0x40))
1516 w83781d_write_value(client, W83781D_REG_IRQ,
1517 i | 0x40);
1518 }
1519 }
1520
1521 /* Start monitoring */
1522 w83781d_write_value(client, W83781D_REG_CONFIG,
1523 (w83781d_read_value(client,
1524 W83781D_REG_CONFIG) & 0xf7)
1525 | 0x01);
1526}
1527
1528static struct w83781d_data *w83781d_update_device(struct device *dev)
1529{
1530 struct i2c_client *client = to_i2c_client(dev);
1531 struct w83781d_data *data = i2c_get_clientdata(client);
1532 int i;
1533
1534 down(&data->update_lock);
1535
1536 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
1537 || !data->valid) {
1538 dev_dbg(dev, "Starting device update\n");
1539
1540 for (i = 0; i <= 8; i++) {
1541 if ((data->type == w83783s || data->type == w83697hf)
1542 && (i == 1))
1543 continue; /* 783S has no in1 */
1544 data->in[i] =
1545 w83781d_read_value(client, W83781D_REG_IN(i));
1546 data->in_min[i] =
1547 w83781d_read_value(client, W83781D_REG_IN_MIN(i));
1548 data->in_max[i] =
1549 w83781d_read_value(client, W83781D_REG_IN_MAX(i));
1550 if ((data->type != w83782d) && (data->type != w83697hf)
1551 && (data->type != w83627hf) && (i == 6))
1552 break;
1553 }
1554 for (i = 1; i <= 3; i++) {
1555 data->fan[i - 1] =
1556 w83781d_read_value(client, W83781D_REG_FAN(i));
1557 data->fan_min[i - 1] =
1558 w83781d_read_value(client, W83781D_REG_FAN_MIN(i));
1559 }
1560 if (data->type != w83781d && data->type != as99127f) {
1561 for (i = 1; i <= 4; i++) {
1562 data->pwm[i - 1] =
1563 w83781d_read_value(client,
1564 W83781D_REG_PWM(i));
1565 if ((data->type != w83782d
1566 || i2c_is_isa_client(client))
1567 && i == 2)
1568 break;
1569 }
1570 /* Only PWM2 can be disabled */
1571 data->pwmenable[1] = (w83781d_read_value(client,
1572 W83781D_REG_PWMCLK12) & 0x08) >> 3;
1573 }
1574
1575 data->temp = w83781d_read_value(client, W83781D_REG_TEMP(1));
1576 data->temp_max =
1577 w83781d_read_value(client, W83781D_REG_TEMP_OVER(1));
1578 data->temp_max_hyst =
1579 w83781d_read_value(client, W83781D_REG_TEMP_HYST(1));
1580 data->temp_add[0] =
1581 w83781d_read_value(client, W83781D_REG_TEMP(2));
1582 data->temp_max_add[0] =
1583 w83781d_read_value(client, W83781D_REG_TEMP_OVER(2));
1584 data->temp_max_hyst_add[0] =
1585 w83781d_read_value(client, W83781D_REG_TEMP_HYST(2));
1586 if (data->type != w83783s && data->type != w83697hf) {
1587 data->temp_add[1] =
1588 w83781d_read_value(client, W83781D_REG_TEMP(3));
1589 data->temp_max_add[1] =
1590 w83781d_read_value(client,
1591 W83781D_REG_TEMP_OVER(3));
1592 data->temp_max_hyst_add[1] =
1593 w83781d_read_value(client,
1594 W83781D_REG_TEMP_HYST(3));
1595 }
1596 i = w83781d_read_value(client, W83781D_REG_VID_FANDIV);
1597 if (data->type != w83697hf) {
1598 data->vid = i & 0x0f;
1599 data->vid |=
1600 (w83781d_read_value(client, W83781D_REG_CHIPID) &
1601 0x01)
1602 << 4;
1603 }
1604 data->fan_div[0] = (i >> 4) & 0x03;
1605 data->fan_div[1] = (i >> 6) & 0x03;
1606 if (data->type != w83697hf) {
1607 data->fan_div[2] = (w83781d_read_value(client,
1608 W83781D_REG_PIN)
1609 >> 6) & 0x03;
1610 }
1611 if ((data->type != w83781d) && (data->type != as99127f)) {
1612 i = w83781d_read_value(client, W83781D_REG_VBAT);
1613 data->fan_div[0] |= (i >> 3) & 0x04;
1614 data->fan_div[1] |= (i >> 4) & 0x04;
1615 if (data->type != w83697hf)
1616 data->fan_div[2] |= (i >> 5) & 0x04;
1617 }
1618 data->alarms =
1619 w83781d_read_value(client,
1620 W83781D_REG_ALARM1) +
1621 (w83781d_read_value(client, W83781D_REG_ALARM2) << 8);
1622 if ((data->type == w83782d) || (data->type == w83627hf)) {
1623 data->alarms |=
1624 w83781d_read_value(client,
1625 W83781D_REG_ALARM3) << 16;
1626 }
1627 i = w83781d_read_value(client, W83781D_REG_BEEP_INTS2);
1628 data->beep_enable = i >> 7;
1629 data->beep_mask = ((i & 0x7f) << 8) +
1630 w83781d_read_value(client, W83781D_REG_BEEP_INTS1);
1631 if ((data->type != w83781d) && (data->type != as99127f)) {
1632 data->beep_mask |=
1633 w83781d_read_value(client,
1634 W83781D_REG_BEEP_INTS3) << 16;
1635 }
1636 data->last_updated = jiffies;
1637 data->valid = 1;
1638 }
1639
1640 up(&data->update_lock);
1641
1642 return data;
1643}
1644
1645static int __init
1646sensors_w83781d_init(void)
1647{
1648 return i2c_add_driver(&w83781d_driver);
1649}
1650
1651static void __exit
1652sensors_w83781d_exit(void)
1653{
1654 i2c_del_driver(&w83781d_driver);
1655}
1656
1657MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, "
1658 "Philip Edelbrock <phil@netroedge.com>, "
1659 "and Mark Studebaker <mdsxyz123@yahoo.com>");
1660MODULE_DESCRIPTION("W83781D driver");
1661MODULE_LICENSE("GPL");
1662
1663module_init(sensors_w83781d_init);
1664module_exit(sensors_w83781d_exit);
diff --git a/drivers/i2c/chips/w83l785ts.c b/drivers/i2c/chips/w83l785ts.c
new file mode 100644
index 000000000000..59bbc5881fa6
--- /dev/null
+++ b/drivers/i2c/chips/w83l785ts.c
@@ -0,0 +1,329 @@
1/*
2 * w83l785ts.c - Part of lm_sensors, Linux kernel modules for hardware
3 * monitoring
4 * Copyright (C) 2003-2004 Jean Delvare <khali@linux-fr.org>
5 *
6 * Inspired from the lm83 driver. The W83L785TS-S is a sensor chip made
7 * by Winbond. It reports a single external temperature with a 1 deg
8 * resolution and a 3 deg accuracy. Datasheet can be obtained from
9 * Winbond's website at:
10 * http://www.winbond-usa.com/products/winbond_products/pdfs/PCIC/W83L785TS-S.pdf
11 *
12 * Ported to Linux 2.6 by Wolfgang Ziegler <nuppla@gmx.at> and Jean Delvare
13 * <khali@linux-fr.org>.
14 *
15 * Thanks to James Bolt <james@evilpenguin.com> for benchmarking the read
16 * error handling mechanism.
17 *
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
22 *
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
31 */
32
33#include <linux/config.h>
34#include <linux/module.h>
35#include <linux/delay.h>
36#include <linux/init.h>
37#include <linux/slab.h>
38#include <linux/jiffies.h>
39#include <linux/i2c.h>
40#include <linux/i2c-sensor.h>
41
42/* How many retries on register read error */
43#define MAX_RETRIES 5
44
45/*
46 * Address to scan
47 * Address is fully defined internally and cannot be changed.
48 */
49
50static unsigned short normal_i2c[] = { 0x2e, I2C_CLIENT_END };
51static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
52
53/*
54 * Insmod parameters
55 */
56
57SENSORS_INSMOD_1(w83l785ts);
58
59/*
60 * The W83L785TS-S registers
61 * Manufacturer ID is 0x5CA3 for Winbond.
62 */
63
64#define W83L785TS_REG_MAN_ID1 0x4D
65#define W83L785TS_REG_MAN_ID2 0x4C
66#define W83L785TS_REG_CHIP_ID 0x4E
67#define W83L785TS_REG_CONFIG 0x40
68#define W83L785TS_REG_TYPE 0x52
69#define W83L785TS_REG_TEMP 0x27
70#define W83L785TS_REG_TEMP_OVER 0x53 /* not sure about this one */
71
72/*
73 * Conversions
74 * The W83L785TS-S uses signed 8-bit values.
75 */
76
77#define TEMP_FROM_REG(val) ((val & 0x80 ? val-0x100 : val) * 1000)
78
79/*
80 * Functions declaration
81 */
82
83static int w83l785ts_attach_adapter(struct i2c_adapter *adapter);
84static int w83l785ts_detect(struct i2c_adapter *adapter, int address,
85 int kind);
86static int w83l785ts_detach_client(struct i2c_client *client);
87static u8 w83l785ts_read_value(struct i2c_client *client, u8 reg, u8 defval);
88static struct w83l785ts_data *w83l785ts_update_device(struct device *dev);
89
90/*
91 * Driver data (common to all clients)
92 */
93
94static struct i2c_driver w83l785ts_driver = {
95 .owner = THIS_MODULE,
96 .name = "w83l785ts",
97 .id = I2C_DRIVERID_W83L785TS,
98 .flags = I2C_DF_NOTIFY,
99 .attach_adapter = w83l785ts_attach_adapter,
100 .detach_client = w83l785ts_detach_client,
101};
102
103/*
104 * Client data (each client gets its own)
105 */
106
107struct w83l785ts_data {
108 struct i2c_client client;
109 struct semaphore update_lock;
110 char valid; /* zero until following fields are valid */
111 unsigned long last_updated; /* in jiffies */
112
113 /* registers values */
114 u8 temp, temp_over;
115};
116
117/*
118 * Sysfs stuff
119 */
120
121static ssize_t show_temp(struct device *dev, char *buf)
122{
123 struct w83l785ts_data *data = w83l785ts_update_device(dev);
124 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp));
125}
126
127static ssize_t show_temp_over(struct device *dev, char *buf)
128{
129 struct w83l785ts_data *data = w83l785ts_update_device(dev);
130 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_over));
131}
132
133static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL);
134static DEVICE_ATTR(temp1_max, S_IRUGO, show_temp_over, NULL);
135
136/*
137 * Real code
138 */
139
140static int w83l785ts_attach_adapter(struct i2c_adapter *adapter)
141{
142 if (!(adapter->class & I2C_CLASS_HWMON))
143 return 0;
144 return i2c_detect(adapter, &addr_data, w83l785ts_detect);
145}
146
147/*
148 * The following function does more than just detection. If detection
149 * succeeds, it also registers the new chip.
150 */
151static int w83l785ts_detect(struct i2c_adapter *adapter, int address, int kind)
152{
153 struct i2c_client *new_client;
154 struct w83l785ts_data *data;
155 int err = 0;
156
157
158 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
159 goto exit;
160
161 if (!(data = kmalloc(sizeof(struct w83l785ts_data), GFP_KERNEL))) {
162 err = -ENOMEM;
163 goto exit;
164 }
165 memset(data, 0, sizeof(struct w83l785ts_data));
166
167
168 /* The common I2C client data is placed right before the
169 * W83L785TS-specific data. */
170 new_client = &data->client;
171 i2c_set_clientdata(new_client, data);
172 new_client->addr = address;
173 new_client->adapter = adapter;
174 new_client->driver = &w83l785ts_driver;
175 new_client->flags = 0;
176
177 /*
178 * Now we do the remaining detection. A negative kind means that
179 * the driver was loaded with no force parameter (default), so we
180 * must both detect and identify the chip (actually there is only
181 * one possible kind of chip for now, W83L785TS-S). A zero kind means
182 * that the driver was loaded with the force parameter, the detection
183 * step shall be skipped. A positive kind means that the driver
184 * was loaded with the force parameter and a given kind of chip is
185 * requested, so both the detection and the identification steps
186 * are skipped.
187 */
188 if (kind < 0) { /* detection */
189 if (((w83l785ts_read_value(new_client,
190 W83L785TS_REG_CONFIG, 0) & 0x80) != 0x00)
191 || ((w83l785ts_read_value(new_client,
192 W83L785TS_REG_TYPE, 0) & 0xFC) != 0x00)) {
193 dev_dbg(&adapter->dev,
194 "W83L785TS-S detection failed at 0x%02x.\n",
195 address);
196 goto exit_free;
197 }
198 }
199
200 if (kind <= 0) { /* identification */
201 u16 man_id;
202 u8 chip_id;
203
204 man_id = (w83l785ts_read_value(new_client,
205 W83L785TS_REG_MAN_ID1, 0) << 8) +
206 w83l785ts_read_value(new_client,
207 W83L785TS_REG_MAN_ID2, 0);
208 chip_id = w83l785ts_read_value(new_client,
209 W83L785TS_REG_CHIP_ID, 0);
210
211 if (man_id == 0x5CA3) { /* Winbond */
212 if (chip_id == 0x70) { /* W83L785TS-S */
213 kind = w83l785ts;
214 }
215 }
216
217 if (kind <= 0) { /* identification failed */
218 dev_info(&adapter->dev,
219 "Unsupported chip (man_id=0x%04X, "
220 "chip_id=0x%02X).\n", man_id, chip_id);
221 goto exit_free;
222 }
223 }
224
225 /* We can fill in the remaining client fields. */
226 strlcpy(new_client->name, "w83l785ts", I2C_NAME_SIZE);
227 data->valid = 0;
228 init_MUTEX(&data->update_lock);
229
230 /* Default values in case the first read fails (unlikely). */
231 data->temp_over = data->temp = 0;
232
233 /* Tell the I2C layer a new client has arrived. */
234 if ((err = i2c_attach_client(new_client)))
235 goto exit_free;
236
237 /*
238 * Initialize the W83L785TS chip
239 * Nothing yet, assume it is already started.
240 */
241
242 /* Register sysfs hooks */
243 device_create_file(&new_client->dev, &dev_attr_temp1_input);
244 device_create_file(&new_client->dev, &dev_attr_temp1_max);
245
246 return 0;
247
248exit_free:
249 kfree(data);
250exit:
251 return err;
252}
253
254static int w83l785ts_detach_client(struct i2c_client *client)
255{
256 int err;
257
258 if ((err = i2c_detach_client(client))) {
259 dev_err(&client->dev, "Client deregistration failed, "
260 "client not detached.\n");
261 return err;
262 }
263
264 kfree(i2c_get_clientdata(client));
265 return 0;
266}
267
268static u8 w83l785ts_read_value(struct i2c_client *client, u8 reg, u8 defval)
269{
270 int value, i;
271
272 /* Frequent read errors have been reported on Asus boards, so we
273 * retry on read errors. If it still fails (unlikely), return the
274 * default value requested by the caller. */
275 for (i = 1; i <= MAX_RETRIES; i++) {
276 value = i2c_smbus_read_byte_data(client, reg);
277 if (value >= 0) {
278 dev_dbg(&client->dev, "Read 0x%02x from register "
279 "0x%02x.\n", value, reg);
280 return value;
281 }
282 dev_dbg(&client->dev, "Read failed, will retry in %d.\n", i);
283 msleep(i);
284 }
285
286 dev_err(&client->dev, "Couldn't read value from register 0x%02x. "
287 "Please report.\n", reg);
288 return defval;
289}
290
291static struct w83l785ts_data *w83l785ts_update_device(struct device *dev)
292{
293 struct i2c_client *client = to_i2c_client(dev);
294 struct w83l785ts_data *data = i2c_get_clientdata(client);
295
296 down(&data->update_lock);
297
298 if (!data->valid || time_after(jiffies, data->last_updated + HZ * 2)) {
299 dev_dbg(&client->dev, "Updating w83l785ts data.\n");
300 data->temp = w83l785ts_read_value(client,
301 W83L785TS_REG_TEMP, data->temp);
302 data->temp_over = w83l785ts_read_value(client,
303 W83L785TS_REG_TEMP_OVER, data->temp_over);
304
305 data->last_updated = jiffies;
306 data->valid = 1;
307 }
308
309 up(&data->update_lock);
310
311 return data;
312}
313
314static int __init sensors_w83l785ts_init(void)
315{
316 return i2c_add_driver(&w83l785ts_driver);
317}
318
319static void __exit sensors_w83l785ts_exit(void)
320{
321 i2c_del_driver(&w83l785ts_driver);
322}
323
324MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
325MODULE_DESCRIPTION("W83L785TS-S driver");
326MODULE_LICENSE("GPL");
327
328module_init(sensors_w83l785ts_init);
329module_exit(sensors_w83l785ts_exit);
diff --git a/drivers/i2c/i2c-core.c b/drivers/i2c/i2c-core.c
new file mode 100644
index 000000000000..9011627d7eb0
--- /dev/null
+++ b/drivers/i2c/i2c-core.c
@@ -0,0 +1,1272 @@
1/* i2c-core.c - a device driver for the iic-bus interface */
2/* ------------------------------------------------------------------------- */
3/* Copyright (C) 1995-99 Simon G. Vogl
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
18/* ------------------------------------------------------------------------- */
19
20/* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>.
21 All SMBus-related things are written by Frodo Looijaard <frodol@dds.nl>
22 SMBus 2.0 support by Mark Studebaker <mdsxyz123@yahoo.com> */
23
24#include <linux/config.h>
25#include <linux/module.h>
26#include <linux/kernel.h>
27#include <linux/errno.h>
28#include <linux/slab.h>
29#include <linux/i2c.h>
30#include <linux/init.h>
31#include <linux/idr.h>
32#include <linux/seq_file.h>
33#include <asm/uaccess.h>
34
35
36static LIST_HEAD(adapters);
37static LIST_HEAD(drivers);
38static DECLARE_MUTEX(core_lists);
39static DEFINE_IDR(i2c_adapter_idr);
40
41/* match always succeeds, as we want the probe() to tell if we really accept this match */
42static int i2c_device_match(struct device *dev, struct device_driver *drv)
43{
44 return 1;
45}
46
47static int i2c_bus_suspend(struct device * dev, pm_message_t state)
48{
49 int rc = 0;
50
51 if (dev->driver && dev->driver->suspend)
52 rc = dev->driver->suspend(dev,state,0);
53 return rc;
54}
55
56static int i2c_bus_resume(struct device * dev)
57{
58 int rc = 0;
59
60 if (dev->driver && dev->driver->resume)
61 rc = dev->driver->resume(dev,0);
62 return rc;
63}
64
65static struct bus_type i2c_bus_type = {
66 .name = "i2c",
67 .match = i2c_device_match,
68 .suspend = i2c_bus_suspend,
69 .resume = i2c_bus_resume,
70};
71
72static int i2c_device_probe(struct device *dev)
73{
74 return -ENODEV;
75}
76
77static int i2c_device_remove(struct device *dev)
78{
79 return 0;
80}
81
82static void i2c_adapter_dev_release(struct device *dev)
83{
84 struct i2c_adapter *adap = dev_to_i2c_adapter(dev);
85 complete(&adap->dev_released);
86}
87
88static struct device_driver i2c_adapter_driver = {
89 .name = "i2c_adapter",
90 .bus = &i2c_bus_type,
91 .probe = i2c_device_probe,
92 .remove = i2c_device_remove,
93};
94
95static void i2c_adapter_class_dev_release(struct class_device *dev)
96{
97 struct i2c_adapter *adap = class_dev_to_i2c_adapter(dev);
98 complete(&adap->class_dev_released);
99}
100
101static struct class i2c_adapter_class = {
102 .name = "i2c-adapter",
103 .release = &i2c_adapter_class_dev_release,
104};
105
106static ssize_t show_adapter_name(struct device *dev, char *buf)
107{
108 struct i2c_adapter *adap = dev_to_i2c_adapter(dev);
109 return sprintf(buf, "%s\n", adap->name);
110}
111static DEVICE_ATTR(name, S_IRUGO, show_adapter_name, NULL);
112
113
114static void i2c_client_release(struct device *dev)
115{
116 struct i2c_client *client = to_i2c_client(dev);
117 complete(&client->released);
118}
119
120static ssize_t show_client_name(struct device *dev, char *buf)
121{
122 struct i2c_client *client = to_i2c_client(dev);
123 return sprintf(buf, "%s\n", client->name);
124}
125
126/*
127 * We can't use the DEVICE_ATTR() macro here as we want the same filename for a
128 * different type of a device. So beware if the DEVICE_ATTR() macro ever
129 * changes, this definition will also have to change.
130 */
131static struct device_attribute dev_attr_client_name = {
132 .attr = {.name = "name", .mode = S_IRUGO, .owner = THIS_MODULE },
133 .show = &show_client_name,
134};
135
136
137/* ---------------------------------------------------
138 * registering functions
139 * ---------------------------------------------------
140 */
141
142/* -----
143 * i2c_add_adapter is called from within the algorithm layer,
144 * when a new hw adapter registers. A new device is register to be
145 * available for clients.
146 */
147int i2c_add_adapter(struct i2c_adapter *adap)
148{
149 int id, res = 0;
150 struct list_head *item;
151 struct i2c_driver *driver;
152
153 down(&core_lists);
154
155 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0) {
156 res = -ENOMEM;
157 goto out_unlock;
158 }
159
160 res = idr_get_new(&i2c_adapter_idr, NULL, &id);
161 if (res < 0) {
162 if (res == -EAGAIN)
163 res = -ENOMEM;
164 goto out_unlock;
165 }
166
167 adap->nr = id & MAX_ID_MASK;
168 init_MUTEX(&adap->bus_lock);
169 init_MUTEX(&adap->clist_lock);
170 list_add_tail(&adap->list,&adapters);
171 INIT_LIST_HEAD(&adap->clients);
172
173 /* Add the adapter to the driver core.
174 * If the parent pointer is not set up,
175 * we add this adapter to the host bus.
176 */
177 if (adap->dev.parent == NULL)
178 adap->dev.parent = &platform_bus;
179 sprintf(adap->dev.bus_id, "i2c-%d", adap->nr);
180 adap->dev.driver = &i2c_adapter_driver;
181 adap->dev.release = &i2c_adapter_dev_release;
182 device_register(&adap->dev);
183 device_create_file(&adap->dev, &dev_attr_name);
184
185 /* Add this adapter to the i2c_adapter class */
186 memset(&adap->class_dev, 0x00, sizeof(struct class_device));
187 adap->class_dev.dev = &adap->dev;
188 adap->class_dev.class = &i2c_adapter_class;
189 strlcpy(adap->class_dev.class_id, adap->dev.bus_id, BUS_ID_SIZE);
190 class_device_register(&adap->class_dev);
191
192 /* inform drivers of new adapters */
193 list_for_each(item,&drivers) {
194 driver = list_entry(item, struct i2c_driver, list);
195 if (driver->flags & I2C_DF_NOTIFY)
196 /* We ignore the return code; if it fails, too bad */
197 driver->attach_adapter(adap);
198 }
199
200 dev_dbg(&adap->dev, "registered as adapter #%d\n", adap->nr);
201
202out_unlock:
203 up(&core_lists);
204 return res;
205}
206
207
208int i2c_del_adapter(struct i2c_adapter *adap)
209{
210 struct list_head *item, *_n;
211 struct i2c_adapter *adap_from_list;
212 struct i2c_driver *driver;
213 struct i2c_client *client;
214 int res = 0;
215
216 down(&core_lists);
217
218 /* First make sure that this adapter was ever added */
219 list_for_each_entry(adap_from_list, &adapters, list) {
220 if (adap_from_list == adap)
221 break;
222 }
223 if (adap_from_list != adap) {
224 pr_debug("I2C: Attempting to delete an unregistered "
225 "adapter\n");
226 res = -EINVAL;
227 goto out_unlock;
228 }
229
230 list_for_each(item,&drivers) {
231 driver = list_entry(item, struct i2c_driver, list);
232 if (driver->detach_adapter)
233 if ((res = driver->detach_adapter(adap))) {
234 dev_warn(&adap->dev, "can't detach adapter "
235 "while detaching driver %s: driver not "
236 "detached!", driver->name);
237 goto out_unlock;
238 }
239 }
240
241 /* detach any active clients. This must be done first, because
242 * it can fail; in which case we give upp. */
243 list_for_each_safe(item, _n, &adap->clients) {
244 client = list_entry(item, struct i2c_client, list);
245
246 /* detaching devices is unconditional of the set notify
247 * flag, as _all_ clients that reside on the adapter
248 * must be deleted, as this would cause invalid states.
249 */
250 if ((res=client->driver->detach_client(client))) {
251 dev_err(&adap->dev, "adapter not "
252 "unregistered, because client at "
253 "address %02x can't be detached. ",
254 client->addr);
255 goto out_unlock;
256 }
257 }
258
259 /* clean up the sysfs representation */
260 init_completion(&adap->dev_released);
261 init_completion(&adap->class_dev_released);
262 class_device_unregister(&adap->class_dev);
263 device_remove_file(&adap->dev, &dev_attr_name);
264 device_unregister(&adap->dev);
265 list_del(&adap->list);
266
267 /* wait for sysfs to drop all references */
268 wait_for_completion(&adap->dev_released);
269 wait_for_completion(&adap->class_dev_released);
270
271 /* free dynamically allocated bus id */
272 idr_remove(&i2c_adapter_idr, adap->nr);
273
274 dev_dbg(&adap->dev, "adapter unregistered\n");
275
276 out_unlock:
277 up(&core_lists);
278 return res;
279}
280
281
282/* -----
283 * What follows is the "upwards" interface: commands for talking to clients,
284 * which implement the functions to access the physical information of the
285 * chips.
286 */
287
288int i2c_add_driver(struct i2c_driver *driver)
289{
290 struct list_head *item;
291 struct i2c_adapter *adapter;
292 int res = 0;
293
294 down(&core_lists);
295
296 /* add the driver to the list of i2c drivers in the driver core */
297 driver->driver.name = driver->name;
298 driver->driver.bus = &i2c_bus_type;
299 driver->driver.probe = i2c_device_probe;
300 driver->driver.remove = i2c_device_remove;
301
302 res = driver_register(&driver->driver);
303 if (res)
304 goto out_unlock;
305
306 list_add_tail(&driver->list,&drivers);
307 pr_debug("i2c-core: driver %s registered.\n", driver->name);
308
309 /* now look for instances of driver on our adapters */
310 if (driver->flags & I2C_DF_NOTIFY) {
311 list_for_each(item,&adapters) {
312 adapter = list_entry(item, struct i2c_adapter, list);
313 driver->attach_adapter(adapter);
314 }
315 }
316
317 out_unlock:
318 up(&core_lists);
319 return res;
320}
321
322int i2c_del_driver(struct i2c_driver *driver)
323{
324 struct list_head *item1, *item2, *_n;
325 struct i2c_client *client;
326 struct i2c_adapter *adap;
327
328 int res = 0;
329
330 down(&core_lists);
331
332 /* Have a look at each adapter, if clients of this driver are still
333 * attached. If so, detach them to be able to kill the driver
334 * afterwards.
335 */
336 pr_debug("i2c-core: unregister_driver - looking for clients.\n");
337 /* removing clients does not depend on the notify flag, else
338 * invalid operation might (will!) result, when using stale client
339 * pointers.
340 */
341 list_for_each(item1,&adapters) {
342 adap = list_entry(item1, struct i2c_adapter, list);
343 dev_dbg(&adap->dev, "examining adapter\n");
344 if (driver->detach_adapter) {
345 if ((res = driver->detach_adapter(adap))) {
346 dev_warn(&adap->dev, "while unregistering "
347 "dummy driver %s, adapter could "
348 "not be detached properly; driver "
349 "not unloaded!",driver->name);
350 goto out_unlock;
351 }
352 } else {
353 list_for_each_safe(item2, _n, &adap->clients) {
354 client = list_entry(item2, struct i2c_client, list);
355 if (client->driver != driver)
356 continue;
357 pr_debug("i2c-core.o: detaching client %s:\n", client->name);
358 if ((res = driver->detach_client(client))) {
359 dev_err(&adap->dev, "while "
360 "unregistering driver "
361 "`%s', the client at "
362 "address %02x of "
363 "adapter could not "
364 "be detached; driver "
365 "not unloaded!",
366 driver->name,
367 client->addr);
368 goto out_unlock;
369 }
370 }
371 }
372 }
373
374 driver_unregister(&driver->driver);
375 list_del(&driver->list);
376 pr_debug("i2c-core: driver unregistered: %s\n", driver->name);
377
378 out_unlock:
379 up(&core_lists);
380 return 0;
381}
382
383static int __i2c_check_addr(struct i2c_adapter *adapter, unsigned int addr)
384{
385 struct list_head *item;
386 struct i2c_client *client;
387
388 list_for_each(item,&adapter->clients) {
389 client = list_entry(item, struct i2c_client, list);
390 if (client->addr == addr)
391 return -EBUSY;
392 }
393 return 0;
394}
395
396int i2c_check_addr(struct i2c_adapter *adapter, int addr)
397{
398 int rval;
399
400 down(&adapter->clist_lock);
401 rval = __i2c_check_addr(adapter, addr);
402 up(&adapter->clist_lock);
403
404 return rval;
405}
406
407int i2c_attach_client(struct i2c_client *client)
408{
409 struct i2c_adapter *adapter = client->adapter;
410
411 down(&adapter->clist_lock);
412 if (__i2c_check_addr(client->adapter, client->addr)) {
413 up(&adapter->clist_lock);
414 return -EBUSY;
415 }
416 list_add_tail(&client->list,&adapter->clients);
417 up(&adapter->clist_lock);
418
419 if (adapter->client_register) {
420 if (adapter->client_register(client)) {
421 dev_warn(&adapter->dev, "warning: client_register "
422 "seems to have failed for client %02x\n",
423 client->addr);
424 }
425 }
426
427 dev_dbg(&adapter->dev, "client [%s] registered to adapter\n",
428 client->name);
429
430 if (client->flags & I2C_CLIENT_ALLOW_USE)
431 client->usage_count = 0;
432
433 client->dev.parent = &client->adapter->dev;
434 client->dev.driver = &client->driver->driver;
435 client->dev.bus = &i2c_bus_type;
436 client->dev.release = &i2c_client_release;
437
438 snprintf(&client->dev.bus_id[0], sizeof(client->dev.bus_id),
439 "%d-%04x", i2c_adapter_id(adapter), client->addr);
440 pr_debug("registering %s\n", client->dev.bus_id);
441 device_register(&client->dev);
442 device_create_file(&client->dev, &dev_attr_client_name);
443
444 return 0;
445}
446
447
448int i2c_detach_client(struct i2c_client *client)
449{
450 struct i2c_adapter *adapter = client->adapter;
451 int res = 0;
452
453 if ((client->flags & I2C_CLIENT_ALLOW_USE) && (client->usage_count > 0))
454 return -EBUSY;
455
456 if (adapter->client_unregister) {
457 res = adapter->client_unregister(client);
458 if (res) {
459 dev_err(&client->dev,
460 "client_unregister [%s] failed, "
461 "client not detached", client->name);
462 goto out;
463 }
464 }
465
466 down(&adapter->clist_lock);
467 list_del(&client->list);
468 init_completion(&client->released);
469 device_remove_file(&client->dev, &dev_attr_client_name);
470 device_unregister(&client->dev);
471 up(&adapter->clist_lock);
472 wait_for_completion(&client->released);
473
474 out:
475 return res;
476}
477
478static int i2c_inc_use_client(struct i2c_client *client)
479{
480
481 if (!try_module_get(client->driver->owner))
482 return -ENODEV;
483 if (!try_module_get(client->adapter->owner)) {
484 module_put(client->driver->owner);
485 return -ENODEV;
486 }
487
488 return 0;
489}
490
491static void i2c_dec_use_client(struct i2c_client *client)
492{
493 module_put(client->driver->owner);
494 module_put(client->adapter->owner);
495}
496
497int i2c_use_client(struct i2c_client *client)
498{
499 int ret;
500
501 ret = i2c_inc_use_client(client);
502 if (ret)
503 return ret;
504
505 if (client->flags & I2C_CLIENT_ALLOW_USE) {
506 if (client->flags & I2C_CLIENT_ALLOW_MULTIPLE_USE)
507 client->usage_count++;
508 else if (client->usage_count > 0)
509 goto busy;
510 else
511 client->usage_count++;
512 }
513
514 return 0;
515 busy:
516 i2c_dec_use_client(client);
517 return -EBUSY;
518}
519
520int i2c_release_client(struct i2c_client *client)
521{
522 if(client->flags & I2C_CLIENT_ALLOW_USE) {
523 if(client->usage_count>0)
524 client->usage_count--;
525 else {
526 pr_debug("i2c-core: %s used one too many times\n",
527 __FUNCTION__);
528 return -EPERM;
529 }
530 }
531
532 i2c_dec_use_client(client);
533
534 return 0;
535}
536
537void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
538{
539 struct list_head *item;
540 struct i2c_client *client;
541
542 down(&adap->clist_lock);
543 list_for_each(item,&adap->clients) {
544 client = list_entry(item, struct i2c_client, list);
545 if (!try_module_get(client->driver->owner))
546 continue;
547 if (NULL != client->driver->command) {
548 up(&adap->clist_lock);
549 client->driver->command(client,cmd,arg);
550 down(&adap->clist_lock);
551 }
552 module_put(client->driver->owner);
553 }
554 up(&adap->clist_lock);
555}
556
557static int __init i2c_init(void)
558{
559 int retval;
560
561 retval = bus_register(&i2c_bus_type);
562 if (retval)
563 return retval;
564 retval = driver_register(&i2c_adapter_driver);
565 if (retval)
566 return retval;
567 return class_register(&i2c_adapter_class);
568}
569
570static void __exit i2c_exit(void)
571{
572 class_unregister(&i2c_adapter_class);
573 driver_unregister(&i2c_adapter_driver);
574 bus_unregister(&i2c_bus_type);
575}
576
577subsys_initcall(i2c_init);
578module_exit(i2c_exit);
579
580/* ----------------------------------------------------
581 * the functional interface to the i2c busses.
582 * ----------------------------------------------------
583 */
584
585int i2c_transfer(struct i2c_adapter * adap, struct i2c_msg *msgs, int num)
586{
587 int ret;
588
589 if (adap->algo->master_xfer) {
590#ifdef DEBUG
591 for (ret = 0; ret < num; ret++) {
592 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
593 "len=%d\n", ret, msgs[ret].flags & I2C_M_RD ?
594 'R' : 'W', msgs[ret].addr, msgs[ret].len);
595 }
596#endif
597
598 down(&adap->bus_lock);
599 ret = adap->algo->master_xfer(adap,msgs,num);
600 up(&adap->bus_lock);
601
602 return ret;
603 } else {
604 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
605 return -ENOSYS;
606 }
607}
608
609int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
610{
611 int ret;
612 struct i2c_adapter *adap=client->adapter;
613 struct i2c_msg msg;
614
615 if (client->adapter->algo->master_xfer) {
616 msg.addr = client->addr;
617 msg.flags = client->flags & I2C_M_TEN;
618 msg.len = count;
619 msg.buf = (char *)buf;
620
621 dev_dbg(&client->adapter->dev, "master_send: writing %d bytes.\n",
622 count);
623
624 down(&adap->bus_lock);
625 ret = adap->algo->master_xfer(adap,&msg,1);
626 up(&adap->bus_lock);
627
628 /* if everything went ok (i.e. 1 msg transmitted), return #bytes
629 * transmitted, else error code.
630 */
631 return (ret == 1 )? count : ret;
632 } else {
633 dev_err(&client->adapter->dev, "I2C level transfers not supported\n");
634 return -ENOSYS;
635 }
636}
637
638int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
639{
640 struct i2c_adapter *adap=client->adapter;
641 struct i2c_msg msg;
642 int ret;
643 if (client->adapter->algo->master_xfer) {
644 msg.addr = client->addr;
645 msg.flags = client->flags & I2C_M_TEN;
646 msg.flags |= I2C_M_RD;
647 msg.len = count;
648 msg.buf = buf;
649
650 dev_dbg(&client->adapter->dev, "master_recv: reading %d bytes.\n",
651 count);
652
653 down(&adap->bus_lock);
654 ret = adap->algo->master_xfer(adap,&msg,1);
655 up(&adap->bus_lock);
656
657 dev_dbg(&client->adapter->dev, "master_recv: return:%d (count:%d, addr:0x%02x)\n",
658 ret, count, client->addr);
659
660 /* if everything went ok (i.e. 1 msg transmitted), return #bytes
661 * transmitted, else error code.
662 */
663 return (ret == 1 )? count : ret;
664 } else {
665 dev_err(&client->adapter->dev, "I2C level transfers not supported\n");
666 return -ENOSYS;
667 }
668}
669
670
671int i2c_control(struct i2c_client *client,
672 unsigned int cmd, unsigned long arg)
673{
674 int ret = 0;
675 struct i2c_adapter *adap = client->adapter;
676
677 dev_dbg(&client->adapter->dev, "i2c ioctl, cmd: 0x%x, arg: %#lx\n", cmd, arg);
678 switch (cmd) {
679 case I2C_RETRIES:
680 adap->retries = arg;
681 break;
682 case I2C_TIMEOUT:
683 adap->timeout = arg;
684 break;
685 default:
686 if (adap->algo->algo_control!=NULL)
687 ret = adap->algo->algo_control(adap,cmd,arg);
688 }
689 return ret;
690}
691
692/* ----------------------------------------------------
693 * the i2c address scanning function
694 * Will not work for 10-bit addresses!
695 * ----------------------------------------------------
696 */
697int i2c_probe(struct i2c_adapter *adapter,
698 struct i2c_client_address_data *address_data,
699 int (*found_proc) (struct i2c_adapter *, int, int))
700{
701 int addr,i,found,err;
702 int adap_id = i2c_adapter_id(adapter);
703
704 /* Forget it if we can't probe using SMBUS_QUICK */
705 if (! i2c_check_functionality(adapter,I2C_FUNC_SMBUS_QUICK))
706 return -1;
707
708 for (addr = 0x00; addr <= 0x7f; addr++) {
709
710 /* Skip if already in use */
711 if (i2c_check_addr(adapter,addr))
712 continue;
713
714 /* If it is in one of the force entries, we don't do any detection
715 at all */
716 found = 0;
717
718 for (i = 0; !found && (address_data->force[i] != I2C_CLIENT_END); i += 2) {
719 if (((adap_id == address_data->force[i]) ||
720 (address_data->force[i] == ANY_I2C_BUS)) &&
721 (addr == address_data->force[i+1])) {
722 dev_dbg(&adapter->dev, "found force parameter for adapter %d, addr %04x\n",
723 adap_id, addr);
724 if ((err = found_proc(adapter,addr,0)))
725 return err;
726 found = 1;
727 }
728 }
729 if (found)
730 continue;
731
732 /* If this address is in one of the ignores, we can forget about
733 it right now */
734 for (i = 0;
735 !found && (address_data->ignore[i] != I2C_CLIENT_END);
736 i += 2) {
737 if (((adap_id == address_data->ignore[i]) ||
738 ((address_data->ignore[i] == ANY_I2C_BUS))) &&
739 (addr == address_data->ignore[i+1])) {
740 dev_dbg(&adapter->dev, "found ignore parameter for adapter %d, "
741 "addr %04x\n", adap_id ,addr);
742 found = 1;
743 }
744 }
745 for (i = 0;
746 !found && (address_data->ignore_range[i] != I2C_CLIENT_END);
747 i += 3) {
748 if (((adap_id == address_data->ignore_range[i]) ||
749 ((address_data->ignore_range[i]==ANY_I2C_BUS))) &&
750 (addr >= address_data->ignore_range[i+1]) &&
751 (addr <= address_data->ignore_range[i+2])) {
752 dev_dbg(&adapter->dev, "found ignore_range parameter for adapter %d, "
753 "addr %04x\n", adap_id,addr);
754 found = 1;
755 }
756 }
757 if (found)
758 continue;
759
760 /* Now, we will do a detection, but only if it is in the normal or
761 probe entries */
762 for (i = 0;
763 !found && (address_data->normal_i2c[i] != I2C_CLIENT_END);
764 i += 1) {
765 if (addr == address_data->normal_i2c[i]) {
766 found = 1;
767 dev_dbg(&adapter->dev, "found normal i2c entry for adapter %d, "
768 "addr %02x\n", adap_id, addr);
769 }
770 }
771
772 for (i = 0;
773 !found && (address_data->normal_i2c_range[i] != I2C_CLIENT_END);
774 i += 2) {
775 if ((addr >= address_data->normal_i2c_range[i]) &&
776 (addr <= address_data->normal_i2c_range[i+1])) {
777 found = 1;
778 dev_dbg(&adapter->dev, "found normal i2c_range entry for adapter %d, "
779 "addr %04x\n", adap_id,addr);
780 }
781 }
782
783 for (i = 0;
784 !found && (address_data->probe[i] != I2C_CLIENT_END);
785 i += 2) {
786 if (((adap_id == address_data->probe[i]) ||
787 ((address_data->probe[i] == ANY_I2C_BUS))) &&
788 (addr == address_data->probe[i+1])) {
789 found = 1;
790 dev_dbg(&adapter->dev, "found probe parameter for adapter %d, "
791 "addr %04x\n", adap_id,addr);
792 }
793 }
794 for (i = 0;
795 !found && (address_data->probe_range[i] != I2C_CLIENT_END);
796 i += 3) {
797 if (((adap_id == address_data->probe_range[i]) ||
798 (address_data->probe_range[i] == ANY_I2C_BUS)) &&
799 (addr >= address_data->probe_range[i+1]) &&
800 (addr <= address_data->probe_range[i+2])) {
801 found = 1;
802 dev_dbg(&adapter->dev, "found probe_range parameter for adapter %d, "
803 "addr %04x\n", adap_id,addr);
804 }
805 }
806 if (!found)
807 continue;
808
809 /* OK, so we really should examine this address. First check
810 whether there is some client here at all! */
811 if (i2c_smbus_xfer(adapter,addr,0,0,0,I2C_SMBUS_QUICK,NULL) >= 0)
812 if ((err = found_proc(adapter,addr,-1)))
813 return err;
814 }
815 return 0;
816}
817
818/*
819 * return id number for a specific adapter
820 */
821int i2c_adapter_id(struct i2c_adapter *adap)
822{
823 return adap->nr;
824}
825
826struct i2c_adapter* i2c_get_adapter(int id)
827{
828 struct list_head *item;
829 struct i2c_adapter *adapter;
830
831 down(&core_lists);
832 list_for_each(item,&adapters) {
833 adapter = list_entry(item, struct i2c_adapter, list);
834 if (id == adapter->nr &&
835 try_module_get(adapter->owner)) {
836 up(&core_lists);
837 return adapter;
838 }
839 }
840 up(&core_lists);
841 return NULL;
842}
843
844void i2c_put_adapter(struct i2c_adapter *adap)
845{
846 module_put(adap->owner);
847}
848
849/* The SMBus parts */
850
851#define POLY (0x1070U << 3)
852static u8
853crc8(u16 data)
854{
855 int i;
856
857 for(i = 0; i < 8; i++) {
858 if (data & 0x8000)
859 data = data ^ POLY;
860 data = data << 1;
861 }
862 return (u8)(data >> 8);
863}
864
865/* CRC over count bytes in the first array plus the bytes in the rest
866 array if it is non-null. rest[0] is the (length of rest) - 1
867 and is included. */
868static u8 i2c_smbus_partial_pec(u8 crc, int count, u8 *first, u8 *rest)
869{
870 int i;
871
872 for(i = 0; i < count; i++)
873 crc = crc8((crc ^ first[i]) << 8);
874 if(rest != NULL)
875 for(i = 0; i <= rest[0]; i++)
876 crc = crc8((crc ^ rest[i]) << 8);
877 return crc;
878}
879
880static u8 i2c_smbus_pec(int count, u8 *first, u8 *rest)
881{
882 return i2c_smbus_partial_pec(0, count, first, rest);
883}
884
885/* Returns new "size" (transaction type)
886 Note that we convert byte to byte_data and byte_data to word_data
887 rather than invent new xxx_PEC transactions. */
888static int i2c_smbus_add_pec(u16 addr, u8 command, int size,
889 union i2c_smbus_data *data)
890{
891 u8 buf[3];
892
893 buf[0] = addr << 1;
894 buf[1] = command;
895 switch(size) {
896 case I2C_SMBUS_BYTE:
897 data->byte = i2c_smbus_pec(2, buf, NULL);
898 size = I2C_SMBUS_BYTE_DATA;
899 break;
900 case I2C_SMBUS_BYTE_DATA:
901 buf[2] = data->byte;
902 data->word = buf[2] ||
903 (i2c_smbus_pec(3, buf, NULL) << 8);
904 size = I2C_SMBUS_WORD_DATA;
905 break;
906 case I2C_SMBUS_WORD_DATA:
907 /* unsupported */
908 break;
909 case I2C_SMBUS_BLOCK_DATA:
910 data->block[data->block[0] + 1] =
911 i2c_smbus_pec(2, buf, data->block);
912 size = I2C_SMBUS_BLOCK_DATA_PEC;
913 break;
914 }
915 return size;
916}
917
918static int i2c_smbus_check_pec(u16 addr, u8 command, int size, u8 partial,
919 union i2c_smbus_data *data)
920{
921 u8 buf[3], rpec, cpec;
922
923 buf[1] = command;
924 switch(size) {
925 case I2C_SMBUS_BYTE_DATA:
926 buf[0] = (addr << 1) | 1;
927 cpec = i2c_smbus_pec(2, buf, NULL);
928 rpec = data->byte;
929 break;
930 case I2C_SMBUS_WORD_DATA:
931 buf[0] = (addr << 1) | 1;
932 buf[2] = data->word & 0xff;
933 cpec = i2c_smbus_pec(3, buf, NULL);
934 rpec = data->word >> 8;
935 break;
936 case I2C_SMBUS_WORD_DATA_PEC:
937 /* unsupported */
938 cpec = rpec = 0;
939 break;
940 case I2C_SMBUS_PROC_CALL_PEC:
941 /* unsupported */
942 cpec = rpec = 0;
943 break;
944 case I2C_SMBUS_BLOCK_DATA_PEC:
945 buf[0] = (addr << 1);
946 buf[2] = (addr << 1) | 1;
947 cpec = i2c_smbus_pec(3, buf, data->block);
948 rpec = data->block[data->block[0] + 1];
949 break;
950 case I2C_SMBUS_BLOCK_PROC_CALL_PEC:
951 buf[0] = (addr << 1) | 1;
952 rpec = i2c_smbus_partial_pec(partial, 1,
953 buf, data->block);
954 cpec = data->block[data->block[0] + 1];
955 break;
956 default:
957 cpec = rpec = 0;
958 break;
959 }
960 if (rpec != cpec) {
961 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
962 rpec, cpec);
963 return -1;
964 }
965 return 0;
966}
967
968s32 i2c_smbus_write_quick(struct i2c_client *client, u8 value)
969{
970 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
971 value,0,I2C_SMBUS_QUICK,NULL);
972}
973
974s32 i2c_smbus_read_byte(struct i2c_client *client)
975{
976 union i2c_smbus_data data;
977 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
978 I2C_SMBUS_READ,0,I2C_SMBUS_BYTE, &data))
979 return -1;
980 else
981 return 0x0FF & data.byte;
982}
983
984s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
985{
986 union i2c_smbus_data data; /* only for PEC */
987 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
988 I2C_SMBUS_WRITE,value, I2C_SMBUS_BYTE,&data);
989}
990
991s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
992{
993 union i2c_smbus_data data;
994 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
995 I2C_SMBUS_READ,command, I2C_SMBUS_BYTE_DATA,&data))
996 return -1;
997 else
998 return 0x0FF & data.byte;
999}
1000
1001s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
1002{
1003 union i2c_smbus_data data;
1004 data.byte = value;
1005 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1006 I2C_SMBUS_WRITE,command,
1007 I2C_SMBUS_BYTE_DATA,&data);
1008}
1009
1010s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
1011{
1012 union i2c_smbus_data data;
1013 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1014 I2C_SMBUS_READ,command, I2C_SMBUS_WORD_DATA, &data))
1015 return -1;
1016 else
1017 return 0x0FFFF & data.word;
1018}
1019
1020s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
1021{
1022 union i2c_smbus_data data;
1023 data.word = value;
1024 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1025 I2C_SMBUS_WRITE,command,
1026 I2C_SMBUS_WORD_DATA,&data);
1027}
1028
1029s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
1030 u8 length, u8 *values)
1031{
1032 union i2c_smbus_data data;
1033 int i;
1034 if (length > I2C_SMBUS_BLOCK_MAX)
1035 length = I2C_SMBUS_BLOCK_MAX;
1036 for (i = 1; i <= length; i++)
1037 data.block[i] = values[i-1];
1038 data.block[0] = length;
1039 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1040 I2C_SMBUS_WRITE,command,
1041 I2C_SMBUS_BLOCK_DATA,&data);
1042}
1043
1044/* Returns the number of read bytes */
1045s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command, u8 *values)
1046{
1047 union i2c_smbus_data data;
1048 int i;
1049 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1050 I2C_SMBUS_READ,command,
1051 I2C_SMBUS_I2C_BLOCK_DATA,&data))
1052 return -1;
1053 else {
1054 for (i = 1; i <= data.block[0]; i++)
1055 values[i-1] = data.block[i];
1056 return data.block[0];
1057 }
1058}
1059
1060/* Simulate a SMBus command using the i2c protocol
1061 No checking of parameters is done! */
1062static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
1063 unsigned short flags,
1064 char read_write, u8 command, int size,
1065 union i2c_smbus_data * data)
1066{
1067 /* So we need to generate a series of msgs. In the case of writing, we
1068 need to use only one message; when reading, we need two. We initialize
1069 most things with sane defaults, to keep the code below somewhat
1070 simpler. */
1071 unsigned char msgbuf0[34];
1072 unsigned char msgbuf1[34];
1073 int num = read_write == I2C_SMBUS_READ?2:1;
1074 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
1075 { addr, flags | I2C_M_RD, 0, msgbuf1 }
1076 };
1077 int i;
1078
1079 msgbuf0[0] = command;
1080 switch(size) {
1081 case I2C_SMBUS_QUICK:
1082 msg[0].len = 0;
1083 /* Special case: The read/write field is used as data */
1084 msg[0].flags = flags | (read_write==I2C_SMBUS_READ)?I2C_M_RD:0;
1085 num = 1;
1086 break;
1087 case I2C_SMBUS_BYTE:
1088 if (read_write == I2C_SMBUS_READ) {
1089 /* Special case: only a read! */
1090 msg[0].flags = I2C_M_RD | flags;
1091 num = 1;
1092 }
1093 break;
1094 case I2C_SMBUS_BYTE_DATA:
1095 if (read_write == I2C_SMBUS_READ)
1096 msg[1].len = 1;
1097 else {
1098 msg[0].len = 2;
1099 msgbuf0[1] = data->byte;
1100 }
1101 break;
1102 case I2C_SMBUS_WORD_DATA:
1103 if (read_write == I2C_SMBUS_READ)
1104 msg[1].len = 2;
1105 else {
1106 msg[0].len=3;
1107 msgbuf0[1] = data->word & 0xff;
1108 msgbuf0[2] = (data->word >> 8) & 0xff;
1109 }
1110 break;
1111 case I2C_SMBUS_PROC_CALL:
1112 num = 2; /* Special case */
1113 read_write = I2C_SMBUS_READ;
1114 msg[0].len = 3;
1115 msg[1].len = 2;
1116 msgbuf0[1] = data->word & 0xff;
1117 msgbuf0[2] = (data->word >> 8) & 0xff;
1118 break;
1119 case I2C_SMBUS_BLOCK_DATA:
1120 case I2C_SMBUS_BLOCK_DATA_PEC:
1121 if (read_write == I2C_SMBUS_READ) {
1122 dev_err(&adapter->dev, "Block read not supported "
1123 "under I2C emulation!\n");
1124 return -1;
1125 } else {
1126 msg[0].len = data->block[0] + 2;
1127 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
1128 dev_err(&adapter->dev, "smbus_access called with "
1129 "invalid block write size (%d)\n",
1130 data->block[0]);
1131 return -1;
1132 }
1133 if(size == I2C_SMBUS_BLOCK_DATA_PEC)
1134 (msg[0].len)++;
1135 for (i = 1; i <= msg[0].len; i++)
1136 msgbuf0[i] = data->block[i-1];
1137 }
1138 break;
1139 case I2C_SMBUS_BLOCK_PROC_CALL:
1140 case I2C_SMBUS_BLOCK_PROC_CALL_PEC:
1141 dev_dbg(&adapter->dev, "Block process call not supported "
1142 "under I2C emulation!\n");
1143 return -1;
1144 case I2C_SMBUS_I2C_BLOCK_DATA:
1145 if (read_write == I2C_SMBUS_READ) {
1146 msg[1].len = I2C_SMBUS_I2C_BLOCK_MAX;
1147 } else {
1148 msg[0].len = data->block[0] + 1;
1149 if (msg[0].len > I2C_SMBUS_I2C_BLOCK_MAX + 1) {
1150 dev_err(&adapter->dev, "i2c_smbus_xfer_emulated called with "
1151 "invalid block write size (%d)\n",
1152 data->block[0]);
1153 return -1;
1154 }
1155 for (i = 1; i <= data->block[0]; i++)
1156 msgbuf0[i] = data->block[i];
1157 }
1158 break;
1159 default:
1160 dev_err(&adapter->dev, "smbus_access called with invalid size (%d)\n",
1161 size);
1162 return -1;
1163 }
1164
1165 if (i2c_transfer(adapter, msg, num) < 0)
1166 return -1;
1167
1168 if (read_write == I2C_SMBUS_READ)
1169 switch(size) {
1170 case I2C_SMBUS_BYTE:
1171 data->byte = msgbuf0[0];
1172 break;
1173 case I2C_SMBUS_BYTE_DATA:
1174 data->byte = msgbuf1[0];
1175 break;
1176 case I2C_SMBUS_WORD_DATA:
1177 case I2C_SMBUS_PROC_CALL:
1178 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1179 break;
1180 case I2C_SMBUS_I2C_BLOCK_DATA:
1181 /* fixed at 32 for now */
1182 data->block[0] = I2C_SMBUS_I2C_BLOCK_MAX;
1183 for (i = 0; i < I2C_SMBUS_I2C_BLOCK_MAX; i++)
1184 data->block[i+1] = msgbuf1[i];
1185 break;
1186 }
1187 return 0;
1188}
1189
1190
1191s32 i2c_smbus_xfer(struct i2c_adapter * adapter, u16 addr, unsigned short flags,
1192 char read_write, u8 command, int size,
1193 union i2c_smbus_data * data)
1194{
1195 s32 res;
1196 int swpec = 0;
1197 u8 partial = 0;
1198
1199 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
1200 if((flags & I2C_CLIENT_PEC) &&
1201 !(i2c_check_functionality(adapter, I2C_FUNC_SMBUS_HWPEC_CALC))) {
1202 swpec = 1;
1203 if(read_write == I2C_SMBUS_READ &&
1204 size == I2C_SMBUS_BLOCK_DATA)
1205 size = I2C_SMBUS_BLOCK_DATA_PEC;
1206 else if(size == I2C_SMBUS_PROC_CALL)
1207 size = I2C_SMBUS_PROC_CALL_PEC;
1208 else if(size == I2C_SMBUS_BLOCK_PROC_CALL) {
1209 i2c_smbus_add_pec(addr, command,
1210 I2C_SMBUS_BLOCK_DATA, data);
1211 partial = data->block[data->block[0] + 1];
1212 size = I2C_SMBUS_BLOCK_PROC_CALL_PEC;
1213 } else if(read_write == I2C_SMBUS_WRITE &&
1214 size != I2C_SMBUS_QUICK &&
1215 size != I2C_SMBUS_I2C_BLOCK_DATA)
1216 size = i2c_smbus_add_pec(addr, command, size, data);
1217 }
1218
1219 if (adapter->algo->smbus_xfer) {
1220 down(&adapter->bus_lock);
1221 res = adapter->algo->smbus_xfer(adapter,addr,flags,read_write,
1222 command,size,data);
1223 up(&adapter->bus_lock);
1224 } else
1225 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
1226 command,size,data);
1227
1228 if(res >= 0 && swpec &&
1229 size != I2C_SMBUS_QUICK && size != I2C_SMBUS_I2C_BLOCK_DATA &&
1230 (read_write == I2C_SMBUS_READ || size == I2C_SMBUS_PROC_CALL_PEC ||
1231 size == I2C_SMBUS_BLOCK_PROC_CALL_PEC)) {
1232 if(i2c_smbus_check_pec(addr, command, size, partial, data))
1233 return -1;
1234 }
1235 return res;
1236}
1237
1238
1239EXPORT_SYMBOL(i2c_add_adapter);
1240EXPORT_SYMBOL(i2c_del_adapter);
1241EXPORT_SYMBOL(i2c_add_driver);
1242EXPORT_SYMBOL(i2c_del_driver);
1243EXPORT_SYMBOL(i2c_attach_client);
1244EXPORT_SYMBOL(i2c_detach_client);
1245EXPORT_SYMBOL(i2c_use_client);
1246EXPORT_SYMBOL(i2c_release_client);
1247EXPORT_SYMBOL(i2c_clients_command);
1248EXPORT_SYMBOL(i2c_check_addr);
1249
1250EXPORT_SYMBOL(i2c_master_send);
1251EXPORT_SYMBOL(i2c_master_recv);
1252EXPORT_SYMBOL(i2c_control);
1253EXPORT_SYMBOL(i2c_transfer);
1254EXPORT_SYMBOL(i2c_adapter_id);
1255EXPORT_SYMBOL(i2c_get_adapter);
1256EXPORT_SYMBOL(i2c_put_adapter);
1257EXPORT_SYMBOL(i2c_probe);
1258
1259EXPORT_SYMBOL(i2c_smbus_xfer);
1260EXPORT_SYMBOL(i2c_smbus_write_quick);
1261EXPORT_SYMBOL(i2c_smbus_read_byte);
1262EXPORT_SYMBOL(i2c_smbus_write_byte);
1263EXPORT_SYMBOL(i2c_smbus_read_byte_data);
1264EXPORT_SYMBOL(i2c_smbus_write_byte_data);
1265EXPORT_SYMBOL(i2c_smbus_read_word_data);
1266EXPORT_SYMBOL(i2c_smbus_write_word_data);
1267EXPORT_SYMBOL(i2c_smbus_write_block_data);
1268EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
1269
1270MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1271MODULE_DESCRIPTION("I2C-Bus main module");
1272MODULE_LICENSE("GPL");
diff --git a/drivers/i2c/i2c-dev.c b/drivers/i2c/i2c-dev.c
new file mode 100644
index 000000000000..86c4d0149e82
--- /dev/null
+++ b/drivers/i2c/i2c-dev.c
@@ -0,0 +1,552 @@
1/*
2 i2c-dev.c - i2c-bus driver, char device interface
3
4 Copyright (C) 1995-97 Simon G. Vogl
5 Copyright (C) 1998-99 Frodo Looijaard <frodol@dds.nl>
6 Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
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; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21*/
22
23/* Note that this is a complete rewrite of Simon Vogl's i2c-dev module.
24 But I have used so much of his original code and ideas that it seems
25 only fair to recognize him as co-author -- Frodo */
26
27/* The I2C_RDWR ioctl code is written by Kolja Waschk <waschk@telos.de> */
28
29/* The devfs code is contributed by Philipp Matthias Hahn
30 <pmhahn@titan.lahn.de> */
31
32#include <linux/config.h>
33#include <linux/kernel.h>
34#include <linux/module.h>
35#include <linux/fs.h>
36#include <linux/slab.h>
37#include <linux/smp_lock.h>
38#include <linux/devfs_fs_kernel.h>
39#include <linux/init.h>
40#include <linux/i2c.h>
41#include <linux/i2c-dev.h>
42#include <asm/uaccess.h>
43
44static struct i2c_client i2cdev_client_template;
45
46struct i2c_dev {
47 int minor;
48 struct i2c_adapter *adap;
49 struct class_device class_dev;
50 struct completion released; /* FIXME, we need a class_device_unregister() */
51};
52#define to_i2c_dev(d) container_of(d, struct i2c_dev, class_dev)
53
54#define I2C_MINORS 256
55static struct i2c_dev *i2c_dev_array[I2C_MINORS];
56static DEFINE_SPINLOCK(i2c_dev_array_lock);
57
58static struct i2c_dev *i2c_dev_get_by_minor(unsigned index)
59{
60 struct i2c_dev *i2c_dev;
61
62 spin_lock(&i2c_dev_array_lock);
63 i2c_dev = i2c_dev_array[index];
64 spin_unlock(&i2c_dev_array_lock);
65 return i2c_dev;
66}
67
68static struct i2c_dev *i2c_dev_get_by_adapter(struct i2c_adapter *adap)
69{
70 struct i2c_dev *i2c_dev = NULL;
71
72 spin_lock(&i2c_dev_array_lock);
73 if ((i2c_dev_array[adap->nr]) &&
74 (i2c_dev_array[adap->nr]->adap == adap))
75 i2c_dev = i2c_dev_array[adap->nr];
76 spin_unlock(&i2c_dev_array_lock);
77 return i2c_dev;
78}
79
80static struct i2c_dev *get_free_i2c_dev(struct i2c_adapter *adap)
81{
82 struct i2c_dev *i2c_dev;
83
84 i2c_dev = kmalloc(sizeof(*i2c_dev), GFP_KERNEL);
85 if (!i2c_dev)
86 return ERR_PTR(-ENOMEM);
87 memset(i2c_dev, 0x00, sizeof(*i2c_dev));
88
89 spin_lock(&i2c_dev_array_lock);
90 if (i2c_dev_array[adap->nr]) {
91 spin_unlock(&i2c_dev_array_lock);
92 dev_err(&adap->dev, "i2c-dev already has a device assigned to this adapter\n");
93 goto error;
94 }
95 i2c_dev->minor = adap->nr;
96 i2c_dev_array[adap->nr] = i2c_dev;
97 spin_unlock(&i2c_dev_array_lock);
98 return i2c_dev;
99error:
100 kfree(i2c_dev);
101 return ERR_PTR(-ENODEV);
102}
103
104static void return_i2c_dev(struct i2c_dev *i2c_dev)
105{
106 spin_lock(&i2c_dev_array_lock);
107 i2c_dev_array[i2c_dev->minor] = NULL;
108 spin_unlock(&i2c_dev_array_lock);
109}
110
111static ssize_t show_adapter_name(struct class_device *class_dev, char *buf)
112{
113 struct i2c_dev *i2c_dev = to_i2c_dev(class_dev);
114 return sprintf(buf, "%s\n", i2c_dev->adap->name);
115}
116static CLASS_DEVICE_ATTR(name, S_IRUGO, show_adapter_name, NULL);
117
118static ssize_t i2cdev_read (struct file *file, char __user *buf, size_t count,
119 loff_t *offset)
120{
121 char *tmp;
122 int ret;
123
124 struct i2c_client *client = (struct i2c_client *)file->private_data;
125
126 if (count > 8192)
127 count = 8192;
128
129 tmp = kmalloc(count,GFP_KERNEL);
130 if (tmp==NULL)
131 return -ENOMEM;
132
133 pr_debug("i2c-dev: i2c-%d reading %zd bytes.\n",
134 iminor(file->f_dentry->d_inode), count);
135
136 ret = i2c_master_recv(client,tmp,count);
137 if (ret >= 0)
138 ret = copy_to_user(buf,tmp,count)?-EFAULT:ret;
139 kfree(tmp);
140 return ret;
141}
142
143static ssize_t i2cdev_write (struct file *file, const char __user *buf, size_t count,
144 loff_t *offset)
145{
146 int ret;
147 char *tmp;
148 struct i2c_client *client = (struct i2c_client *)file->private_data;
149
150 if (count > 8192)
151 count = 8192;
152
153 tmp = kmalloc(count,GFP_KERNEL);
154 if (tmp==NULL)
155 return -ENOMEM;
156 if (copy_from_user(tmp,buf,count)) {
157 kfree(tmp);
158 return -EFAULT;
159 }
160
161 pr_debug("i2c-dev: i2c-%d writing %zd bytes.\n",
162 iminor(file->f_dentry->d_inode), count);
163
164 ret = i2c_master_send(client,tmp,count);
165 kfree(tmp);
166 return ret;
167}
168
169static int i2cdev_ioctl(struct inode *inode, struct file *file,
170 unsigned int cmd, unsigned long arg)
171{
172 struct i2c_client *client = (struct i2c_client *)file->private_data;
173 struct i2c_rdwr_ioctl_data rdwr_arg;
174 struct i2c_smbus_ioctl_data data_arg;
175 union i2c_smbus_data temp;
176 struct i2c_msg *rdwr_pa;
177 u8 __user **data_ptrs;
178 int i,datasize,res;
179 unsigned long funcs;
180
181 dev_dbg(&client->adapter->dev, "i2c-%d ioctl, cmd: 0x%x, arg: %lx.\n",
182 iminor(inode),cmd, arg);
183
184 switch ( cmd ) {
185 case I2C_SLAVE:
186 case I2C_SLAVE_FORCE:
187 if ((arg > 0x3ff) ||
188 (((client->flags & I2C_M_TEN) == 0) && arg > 0x7f))
189 return -EINVAL;
190 if ((cmd == I2C_SLAVE) && i2c_check_addr(client->adapter,arg))
191 return -EBUSY;
192 client->addr = arg;
193 return 0;
194 case I2C_TENBIT:
195 if (arg)
196 client->flags |= I2C_M_TEN;
197 else
198 client->flags &= ~I2C_M_TEN;
199 return 0;
200 case I2C_PEC:
201 if (arg)
202 client->flags |= I2C_CLIENT_PEC;
203 else
204 client->flags &= ~I2C_CLIENT_PEC;
205 return 0;
206 case I2C_FUNCS:
207 funcs = i2c_get_functionality(client->adapter);
208 return (copy_to_user((unsigned long __user *)arg, &funcs,
209 sizeof(unsigned long)))?-EFAULT:0;
210
211 case I2C_RDWR:
212 if (copy_from_user(&rdwr_arg,
213 (struct i2c_rdwr_ioctl_data __user *)arg,
214 sizeof(rdwr_arg)))
215 return -EFAULT;
216
217 /* Put an arbritrary limit on the number of messages that can
218 * be sent at once */
219 if (rdwr_arg.nmsgs > I2C_RDRW_IOCTL_MAX_MSGS)
220 return -EINVAL;
221
222 rdwr_pa = (struct i2c_msg *)
223 kmalloc(rdwr_arg.nmsgs * sizeof(struct i2c_msg),
224 GFP_KERNEL);
225
226 if (rdwr_pa == NULL) return -ENOMEM;
227
228 if (copy_from_user(rdwr_pa, rdwr_arg.msgs,
229 rdwr_arg.nmsgs * sizeof(struct i2c_msg))) {
230 kfree(rdwr_pa);
231 return -EFAULT;
232 }
233
234 data_ptrs = kmalloc(rdwr_arg.nmsgs * sizeof(u8 __user *), GFP_KERNEL);
235 if (data_ptrs == NULL) {
236 kfree(rdwr_pa);
237 return -ENOMEM;
238 }
239
240 res = 0;
241 for( i=0; i<rdwr_arg.nmsgs; i++ ) {
242 /* Limit the size of the message to a sane amount */
243 if (rdwr_pa[i].len > 8192) {
244 res = -EINVAL;
245 break;
246 }
247 data_ptrs[i] = (u8 __user *)rdwr_pa[i].buf;
248 rdwr_pa[i].buf = kmalloc(rdwr_pa[i].len, GFP_KERNEL);
249 if(rdwr_pa[i].buf == NULL) {
250 res = -ENOMEM;
251 break;
252 }
253 if(copy_from_user(rdwr_pa[i].buf,
254 data_ptrs[i],
255 rdwr_pa[i].len)) {
256 ++i; /* Needs to be kfreed too */
257 res = -EFAULT;
258 break;
259 }
260 }
261 if (res < 0) {
262 int j;
263 for (j = 0; j < i; ++j)
264 kfree(rdwr_pa[j].buf);
265 kfree(data_ptrs);
266 kfree(rdwr_pa);
267 return res;
268 }
269
270 res = i2c_transfer(client->adapter,
271 rdwr_pa,
272 rdwr_arg.nmsgs);
273 while(i-- > 0) {
274 if( res>=0 && (rdwr_pa[i].flags & I2C_M_RD)) {
275 if(copy_to_user(
276 data_ptrs[i],
277 rdwr_pa[i].buf,
278 rdwr_pa[i].len)) {
279 res = -EFAULT;
280 }
281 }
282 kfree(rdwr_pa[i].buf);
283 }
284 kfree(data_ptrs);
285 kfree(rdwr_pa);
286 return res;
287
288 case I2C_SMBUS:
289 if (copy_from_user(&data_arg,
290 (struct i2c_smbus_ioctl_data __user *) arg,
291 sizeof(struct i2c_smbus_ioctl_data)))
292 return -EFAULT;
293 if ((data_arg.size != I2C_SMBUS_BYTE) &&
294 (data_arg.size != I2C_SMBUS_QUICK) &&
295 (data_arg.size != I2C_SMBUS_BYTE_DATA) &&
296 (data_arg.size != I2C_SMBUS_WORD_DATA) &&
297 (data_arg.size != I2C_SMBUS_PROC_CALL) &&
298 (data_arg.size != I2C_SMBUS_BLOCK_DATA) &&
299 (data_arg.size != I2C_SMBUS_I2C_BLOCK_DATA) &&
300 (data_arg.size != I2C_SMBUS_BLOCK_PROC_CALL)) {
301 dev_dbg(&client->adapter->dev,
302 "size out of range (%x) in ioctl I2C_SMBUS.\n",
303 data_arg.size);
304 return -EINVAL;
305 }
306 /* Note that I2C_SMBUS_READ and I2C_SMBUS_WRITE are 0 and 1,
307 so the check is valid if size==I2C_SMBUS_QUICK too. */
308 if ((data_arg.read_write != I2C_SMBUS_READ) &&
309 (data_arg.read_write != I2C_SMBUS_WRITE)) {
310 dev_dbg(&client->adapter->dev,
311 "read_write out of range (%x) in ioctl I2C_SMBUS.\n",
312 data_arg.read_write);
313 return -EINVAL;
314 }
315
316 /* Note that command values are always valid! */
317
318 if ((data_arg.size == I2C_SMBUS_QUICK) ||
319 ((data_arg.size == I2C_SMBUS_BYTE) &&
320 (data_arg.read_write == I2C_SMBUS_WRITE)))
321 /* These are special: we do not use data */
322 return i2c_smbus_xfer(client->adapter, client->addr,
323 client->flags,
324 data_arg.read_write,
325 data_arg.command,
326 data_arg.size, NULL);
327
328 if (data_arg.data == NULL) {
329 dev_dbg(&client->adapter->dev,
330 "data is NULL pointer in ioctl I2C_SMBUS.\n");
331 return -EINVAL;
332 }
333
334 if ((data_arg.size == I2C_SMBUS_BYTE_DATA) ||
335 (data_arg.size == I2C_SMBUS_BYTE))
336 datasize = sizeof(data_arg.data->byte);
337 else if ((data_arg.size == I2C_SMBUS_WORD_DATA) ||
338 (data_arg.size == I2C_SMBUS_PROC_CALL))
339 datasize = sizeof(data_arg.data->word);
340 else /* size == smbus block, i2c block, or block proc. call */
341 datasize = sizeof(data_arg.data->block);
342
343 if ((data_arg.size == I2C_SMBUS_PROC_CALL) ||
344 (data_arg.size == I2C_SMBUS_BLOCK_PROC_CALL) ||
345 (data_arg.read_write == I2C_SMBUS_WRITE)) {
346 if (copy_from_user(&temp, data_arg.data, datasize))
347 return -EFAULT;
348 }
349 res = i2c_smbus_xfer(client->adapter,client->addr,client->flags,
350 data_arg.read_write,
351 data_arg.command,data_arg.size,&temp);
352 if (! res && ((data_arg.size == I2C_SMBUS_PROC_CALL) ||
353 (data_arg.size == I2C_SMBUS_BLOCK_PROC_CALL) ||
354 (data_arg.read_write == I2C_SMBUS_READ))) {
355 if (copy_to_user(data_arg.data, &temp, datasize))
356 return -EFAULT;
357 }
358 return res;
359
360 default:
361 return i2c_control(client,cmd,arg);
362 }
363 return 0;
364}
365
366static int i2cdev_open(struct inode *inode, struct file *file)
367{
368 unsigned int minor = iminor(inode);
369 struct i2c_client *client;
370 struct i2c_adapter *adap;
371 struct i2c_dev *i2c_dev;
372
373 i2c_dev = i2c_dev_get_by_minor(minor);
374 if (!i2c_dev)
375 return -ENODEV;
376
377 adap = i2c_get_adapter(i2c_dev->adap->nr);
378 if (!adap)
379 return -ENODEV;
380
381 client = kmalloc(sizeof(*client), GFP_KERNEL);
382 if (!client) {
383 i2c_put_adapter(adap);
384 return -ENOMEM;
385 }
386 memcpy(client, &i2cdev_client_template, sizeof(*client));
387
388 /* registered with adapter, passed as client to user */
389 client->adapter = adap;
390 file->private_data = client;
391
392 return 0;
393}
394
395static int i2cdev_release(struct inode *inode, struct file *file)
396{
397 struct i2c_client *client = file->private_data;
398
399 i2c_put_adapter(client->adapter);
400 kfree(client);
401 file->private_data = NULL;
402
403 return 0;
404}
405
406static struct file_operations i2cdev_fops = {
407 .owner = THIS_MODULE,
408 .llseek = no_llseek,
409 .read = i2cdev_read,
410 .write = i2cdev_write,
411 .ioctl = i2cdev_ioctl,
412 .open = i2cdev_open,
413 .release = i2cdev_release,
414};
415
416static void release_i2c_dev(struct class_device *dev)
417{
418 struct i2c_dev *i2c_dev = to_i2c_dev(dev);
419 complete(&i2c_dev->released);
420}
421
422static struct class i2c_dev_class = {
423 .name = "i2c-dev",
424 .release = &release_i2c_dev,
425};
426
427static int i2cdev_attach_adapter(struct i2c_adapter *adap)
428{
429 struct i2c_dev *i2c_dev;
430 int retval;
431
432 i2c_dev = get_free_i2c_dev(adap);
433 if (IS_ERR(i2c_dev))
434 return PTR_ERR(i2c_dev);
435
436 devfs_mk_cdev(MKDEV(I2C_MAJOR, i2c_dev->minor),
437 S_IFCHR|S_IRUSR|S_IWUSR, "i2c/%d", i2c_dev->minor);
438 dev_dbg(&adap->dev, "Registered as minor %d\n", i2c_dev->minor);
439
440 /* register this i2c device with the driver core */
441 i2c_dev->adap = adap;
442 if (adap->dev.parent == &platform_bus)
443 i2c_dev->class_dev.dev = &adap->dev;
444 else
445 i2c_dev->class_dev.dev = adap->dev.parent;
446 i2c_dev->class_dev.class = &i2c_dev_class;
447 i2c_dev->class_dev.devt = MKDEV(I2C_MAJOR, i2c_dev->minor);
448 snprintf(i2c_dev->class_dev.class_id, BUS_ID_SIZE, "i2c-%d", i2c_dev->minor);
449 retval = class_device_register(&i2c_dev->class_dev);
450 if (retval)
451 goto error;
452 class_device_create_file(&i2c_dev->class_dev, &class_device_attr_name);
453 return 0;
454error:
455 return_i2c_dev(i2c_dev);
456 kfree(i2c_dev);
457 return retval;
458}
459
460static int i2cdev_detach_adapter(struct i2c_adapter *adap)
461{
462 struct i2c_dev *i2c_dev;
463
464 i2c_dev = i2c_dev_get_by_adapter(adap);
465 if (!i2c_dev)
466 return -ENODEV;
467
468 init_completion(&i2c_dev->released);
469 devfs_remove("i2c/%d", i2c_dev->minor);
470 return_i2c_dev(i2c_dev);
471 class_device_unregister(&i2c_dev->class_dev);
472 wait_for_completion(&i2c_dev->released);
473 kfree(i2c_dev);
474
475 dev_dbg(&adap->dev, "Adapter unregistered\n");
476 return 0;
477}
478
479static int i2cdev_detach_client(struct i2c_client *client)
480{
481 return 0;
482}
483
484static int i2cdev_command(struct i2c_client *client, unsigned int cmd,
485 void *arg)
486{
487 return -1;
488}
489
490static struct i2c_driver i2cdev_driver = {
491 .owner = THIS_MODULE,
492 .name = "dev_driver",
493 .id = I2C_DRIVERID_I2CDEV,
494 .flags = I2C_DF_NOTIFY,
495 .attach_adapter = i2cdev_attach_adapter,
496 .detach_adapter = i2cdev_detach_adapter,
497 .detach_client = i2cdev_detach_client,
498 .command = i2cdev_command,
499};
500
501static struct i2c_client i2cdev_client_template = {
502 .name = "I2C /dev entry",
503 .addr = -1,
504 .driver = &i2cdev_driver,
505};
506
507static int __init i2c_dev_init(void)
508{
509 int res;
510
511 printk(KERN_INFO "i2c /dev entries driver\n");
512
513 res = register_chrdev(I2C_MAJOR, "i2c", &i2cdev_fops);
514 if (res)
515 goto out;
516
517 res = class_register(&i2c_dev_class);
518 if (res)
519 goto out_unreg_chrdev;
520
521 res = i2c_add_driver(&i2cdev_driver);
522 if (res)
523 goto out_unreg_class;
524
525 devfs_mk_dir("i2c");
526
527 return 0;
528
529out_unreg_class:
530 class_unregister(&i2c_dev_class);
531out_unreg_chrdev:
532 unregister_chrdev(I2C_MAJOR, "i2c");
533out:
534 printk(KERN_ERR "%s: Driver Initialisation failed\n", __FILE__);
535 return res;
536}
537
538static void __exit i2c_dev_exit(void)
539{
540 i2c_del_driver(&i2cdev_driver);
541 class_unregister(&i2c_dev_class);
542 devfs_remove("i2c");
543 unregister_chrdev(I2C_MAJOR,"i2c");
544}
545
546MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and "
547 "Simon G. Vogl <simon@tk.uni-linz.ac.at>");
548MODULE_DESCRIPTION("I2C /dev entries driver");
549MODULE_LICENSE("GPL");
550
551module_init(i2c_dev_init);
552module_exit(i2c_dev_exit);
diff --git a/drivers/i2c/i2c-sensor-detect.c b/drivers/i2c/i2c-sensor-detect.c
new file mode 100644
index 000000000000..f99a8161a9f1
--- /dev/null
+++ b/drivers/i2c/i2c-sensor-detect.c
@@ -0,0 +1,145 @@
1/*
2 i2c-sensor-detect.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring
4 Copyright (c) 1998 - 2001 Frodo Looijaard <frodol@dds.nl> and
5 Mark D. Studebaker <mdsxyz123@yahoo.com>
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/kernel.h>
24#include <linux/i2c.h>
25#include <linux/i2c-sensor.h>
26
27static unsigned short empty[] = {I2C_CLIENT_END};
28static unsigned int empty_isa[] = {I2C_CLIENT_ISA_END};
29
30/* Very inefficient for ISA detects, and won't work for 10-bit addresses! */
31int i2c_detect(struct i2c_adapter *adapter,
32 struct i2c_address_data *address_data,
33 int (*found_proc) (struct i2c_adapter *, int, int))
34{
35 int addr, i, found, j, err;
36 struct i2c_force_data *this_force;
37 int is_isa = i2c_is_isa_adapter(adapter);
38 int adapter_id =
39 is_isa ? ANY_I2C_ISA_BUS : i2c_adapter_id(adapter);
40 unsigned short *normal_i2c;
41 unsigned int *normal_isa;
42 unsigned short *probe;
43 unsigned short *ignore;
44
45 /* Forget it if we can't probe using SMBUS_QUICK */
46 if ((!is_isa) &&
47 !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK))
48 return -1;
49
50 /* Use default "empty" list if the adapter doesn't specify any */
51 normal_i2c = probe = ignore = empty;
52 normal_isa = empty_isa;
53 if (address_data->normal_i2c)
54 normal_i2c = address_data->normal_i2c;
55 if (address_data->normal_isa)
56 normal_isa = address_data->normal_isa;
57 if (address_data->probe)
58 probe = address_data->probe;
59 if (address_data->ignore)
60 ignore = address_data->ignore;
61
62 for (addr = 0x00; addr <= (is_isa ? 0xffff : 0x7f); addr++) {
63 if (!is_isa && i2c_check_addr(adapter, addr))
64 continue;
65
66 /* If it is in one of the force entries, we don't do any
67 detection at all */
68 found = 0;
69 for (i = 0; !found && (this_force = address_data->forces + i, this_force->force); i++) {
70 for (j = 0; !found && (this_force->force[j] != I2C_CLIENT_END); j += 2) {
71 if ( ((adapter_id == this_force->force[j]) ||
72 ((this_force->force[j] == ANY_I2C_BUS) && !is_isa)) &&
73 (addr == this_force->force[j + 1]) ) {
74 dev_dbg(&adapter->dev, "found force parameter for adapter %d, addr %04x\n", adapter_id, addr);
75 if ((err = found_proc(adapter, addr, this_force->kind)))
76 return err;
77 found = 1;
78 }
79 }
80 }
81 if (found)
82 continue;
83
84 /* If this address is in one of the ignores, we can forget about it
85 right now */
86 for (i = 0; !found && (ignore[i] != I2C_CLIENT_END); i += 2) {
87 if ( ((adapter_id == ignore[i]) ||
88 ((ignore[i] == ANY_I2C_BUS) &&
89 !is_isa)) &&
90 (addr == ignore[i + 1])) {
91 dev_dbg(&adapter->dev, "found ignore parameter for adapter %d, addr %04x\n", adapter_id, addr);
92 found = 1;
93 }
94 }
95 if (found)
96 continue;
97
98 /* Now, we will do a detection, but only if it is in the normal or
99 probe entries */
100 if (is_isa) {
101 for (i = 0; !found && (normal_isa[i] != I2C_CLIENT_ISA_END); i += 1) {
102 if (addr == normal_isa[i]) {
103 dev_dbg(&adapter->dev, "found normal isa entry for adapter %d, addr %04x\n", adapter_id, addr);
104 found = 1;
105 }
106 }
107 } else {
108 for (i = 0; !found && (normal_i2c[i] != I2C_CLIENT_END); i += 1) {
109 if (addr == normal_i2c[i]) {
110 found = 1;
111 dev_dbg(&adapter->dev, "found normal i2c entry for adapter %d, addr %02x\n", adapter_id, addr);
112 }
113 }
114 }
115
116 for (i = 0;
117 !found && (probe[i] != I2C_CLIENT_END);
118 i += 2) {
119 if (((adapter_id == probe[i]) ||
120 ((probe[i] == ANY_I2C_BUS) && !is_isa))
121 && (addr == probe[i + 1])) {
122 dev_dbg(&adapter->dev, "found probe parameter for adapter %d, addr %04x\n", adapter_id, addr);
123 found = 1;
124 }
125 }
126 if (!found)
127 continue;
128
129 /* OK, so we really should examine this address. First check
130 whether there is some client here at all! */
131 if (is_isa ||
132 (i2c_smbus_xfer (adapter, addr, 0, 0, 0, I2C_SMBUS_QUICK, NULL) >= 0))
133 if ((err = found_proc(adapter, addr, -1)))
134 return err;
135 }
136 return 0;
137}
138
139EXPORT_SYMBOL(i2c_detect);
140
141MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, "
142 "Rudolf Marek <r.marek@sh.cvut.cz>");
143
144MODULE_DESCRIPTION("i2c-sensor driver");
145MODULE_LICENSE("GPL");
diff --git a/drivers/i2c/i2c-sensor-vid.c b/drivers/i2c/i2c-sensor-vid.c
new file mode 100644
index 000000000000..922e22f054bb
--- /dev/null
+++ b/drivers/i2c/i2c-sensor-vid.c
@@ -0,0 +1,98 @@
1/*
2 i2c-sensor-vid.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring
4
5 Copyright (c) 2004 Rudolf Marek <r.marek@sh.cvut.cz>
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/config.h>
23#include <linux/module.h>
24#include <linux/kernel.h>
25
26struct vrm_model {
27 u8 vendor;
28 u8 eff_family;
29 u8 eff_model;
30 int vrm_type;
31};
32
33#define ANY 0xFF
34
35#ifdef CONFIG_X86
36
37static struct vrm_model vrm_models[] = {
38 {X86_VENDOR_AMD, 0x6, ANY, 90}, /* Athlon Duron etc */
39 {X86_VENDOR_AMD, 0xF, ANY, 24}, /* Athlon 64, Opteron */
40 {X86_VENDOR_INTEL, 0x6, 0x9, 85}, /* 0.13um too */
41 {X86_VENDOR_INTEL, 0x6, 0xB, 85}, /* 0xB Tualatin */
42 {X86_VENDOR_INTEL, 0x6, ANY, 82}, /* any P6 */
43 {X86_VENDOR_INTEL, 0x7, ANY, 0}, /* Itanium */
44 {X86_VENDOR_INTEL, 0xF, 0x3, 100}, /* P4 Prescott */
45 {X86_VENDOR_INTEL, 0xF, ANY, 90}, /* P4 before Prescott */
46 {X86_VENDOR_INTEL, 0x10,ANY, 0}, /* Itanium 2 */
47 {X86_VENDOR_UNKNOWN, ANY, ANY, 0} /* stop here */
48 };
49
50static int find_vrm(u8 eff_family, u8 eff_model, u8 vendor)
51{
52 int i = 0;
53
54 while (vrm_models[i].vendor!=X86_VENDOR_UNKNOWN) {
55 if (vrm_models[i].vendor==vendor)
56 if ((vrm_models[i].eff_family==eff_family)&& \
57 ((vrm_models[i].eff_model==eff_model)|| \
58 (vrm_models[i].eff_model==ANY)))
59 return vrm_models[i].vrm_type;
60 i++;
61 }
62
63 return 0;
64}
65
66int i2c_which_vrm(void)
67{
68 struct cpuinfo_x86 *c = cpu_data;
69 u32 eax;
70 u8 eff_family, eff_model;
71 int vrm_ret;
72
73 if (c->x86 < 6) return 0; /* any CPU with familly lower than 6
74 dont have VID and/or CPUID */
75 eax = cpuid_eax(1);
76 eff_family = ((eax & 0x00000F00)>>8);
77 eff_model = ((eax & 0x000000F0)>>4);
78 if (eff_family == 0xF) { /* use extended model & family */
79 eff_family += ((eax & 0x00F00000)>>20);
80 eff_model += ((eax & 0x000F0000)>>16)<<4;
81 }
82 vrm_ret = find_vrm(eff_family,eff_model,c->x86_vendor);
83 if (vrm_ret == 0)
84 printk(KERN_INFO "i2c-sensor.o: Unknown VRM version of your"
85 " x86 CPU\n");
86 return vrm_ret;
87}
88
89/* and now for something completely different for Non-x86 world*/
90#else
91int i2c_which_vrm(void)
92{
93 printk(KERN_INFO "i2c-sensor.o: Unknown VRM version of your CPU\n");
94 return 0;
95}
96#endif
97
98EXPORT_SYMBOL(i2c_which_vrm);