aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/i2c/chips
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/i2c/chips')
-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
41 files changed, 27600 insertions, 0 deletions
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);