aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/hwmon
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/hwmon')
-rw-r--r--drivers/hwmon/Kconfig45
-rw-r--r--drivers/hwmon/Makefile3
-rw-r--r--drivers/hwmon/adcxx.c15
-rw-r--r--drivers/hwmon/adt7411.c366
-rw-r--r--drivers/hwmon/adt7473.c1180
-rw-r--r--drivers/hwmon/asc7621.c1255
-rw-r--r--drivers/hwmon/fschmd.c15
-rw-r--r--drivers/hwmon/g760a.c2
-rw-r--r--drivers/hwmon/it87.c939
-rw-r--r--drivers/hwmon/lm90.c89
-rw-r--r--drivers/hwmon/tmp401.c7
-rw-r--r--drivers/hwmon/tmp421.c24
-rw-r--r--drivers/hwmon/vt8231.c3
-rw-r--r--drivers/hwmon/w83793.c482
14 files changed, 2934 insertions, 1491 deletions
diff --git a/drivers/hwmon/Kconfig b/drivers/hwmon/Kconfig
index 68cf87749a42..e4595e6147b4 100644
--- a/drivers/hwmon/Kconfig
+++ b/drivers/hwmon/Kconfig
@@ -170,6 +170,16 @@ config SENSORS_ADM9240
170 This driver can also be built as a module. If so, the module 170 This driver can also be built as a module. If so, the module
171 will be called adm9240. 171 will be called adm9240.
172 172
173config SENSORS_ADT7411
174 tristate "Analog Devices ADT7411"
175 depends on I2C && EXPERIMENTAL
176 help
177 If you say yes here you get support for the Analog Devices
178 ADT7411 voltage and temperature monitoring chip.
179
180 This driver can also be built as a module. If so, the module
181 will be called adt7411.
182
173config SENSORS_ADT7462 183config SENSORS_ADT7462
174 tristate "Analog Devices ADT7462" 184 tristate "Analog Devices ADT7462"
175 depends on I2C && EXPERIMENTAL 185 depends on I2C && EXPERIMENTAL
@@ -190,20 +200,6 @@ config SENSORS_ADT7470
190 This driver can also be built as a module. If so, the module 200 This driver can also be built as a module. If so, the module
191 will be called adt7470. 201 will be called adt7470.
192 202
193config SENSORS_ADT7473
194 tristate "Analog Devices ADT7473 (DEPRECATED)"
195 depends on I2C && EXPERIMENTAL
196 select SENSORS_ADT7475
197 help
198 If you say yes here you get support for the Analog Devices
199 ADT7473 temperature monitoring chips.
200
201 This driver is deprecated, you should use the adt7475 driver
202 instead.
203
204 This driver can also be built as a module. If so, the module
205 will be called adt7473.
206
207config SENSORS_ADT7475 203config SENSORS_ADT7475
208 tristate "Analog Devices ADT7473, ADT7475, ADT7476 and ADT7490" 204 tristate "Analog Devices ADT7473, ADT7475, ADT7476 and ADT7490"
209 depends on I2C && EXPERIMENTAL 205 depends on I2C && EXPERIMENTAL
@@ -216,6 +212,19 @@ config SENSORS_ADT7475
216 This driver can also be build as a module. If so, the module 212 This driver can also be build as a module. If so, the module
217 will be called adt7475. 213 will be called adt7475.
218 214
215config SENSORS_ASC7621
216 tristate "Andigilog aSC7621"
217 depends on HWMON && I2C
218 help
219 If you say yes here you get support for the aSC7621
220 family of SMBus sensors chip found on most Intel X48, X38, 975,
221 965 and 945 desktop boards. Currently supported chips:
222 aSC7621
223 aSC7621a
224
225 This driver can also be built as a module. If so, the module
226 will be called asc7621.
227
219config SENSORS_K8TEMP 228config SENSORS_K8TEMP
220 tristate "AMD Athlon64/FX or Opteron temperature sensor" 229 tristate "AMD Athlon64/FX or Opteron temperature sensor"
221 depends on X86 && PCI && EXPERIMENTAL 230 depends on X86 && PCI && EXPERIMENTAL
@@ -563,9 +572,10 @@ config SENSORS_LM90
563 depends on I2C 572 depends on I2C
564 help 573 help
565 If you say yes here you get support for National Semiconductor LM90, 574 If you say yes here you get support for National Semiconductor LM90,
566 LM86, LM89 and LM99, Analog Devices ADM1032 and ADT7461, and Maxim 575 LM86, LM89 and LM99, Analog Devices ADM1032 and ADT7461, Maxim
567 MAX6646, MAX6647, MAX6648, MAX6649, MAX6657, MAX6658, MAX6659, 576 MAX6646, MAX6647, MAX6648, MAX6649, MAX6657, MAX6658, MAX6659,
568 MAX6680, MAX6681 and MAX6692 sensor chips. 577 MAX6680, MAX6681 and MAX6692, and Winbond/Nuvoton W83L771AWG/ASG
578 sensor chips.
569 579
570 This driver can also be built as a module. If so, the module 580 This driver can also be built as a module. If so, the module
571 will be called lm90. 581 will be called lm90.
@@ -909,7 +919,8 @@ config SENSORS_W83793
909 select HWMON_VID 919 select HWMON_VID
910 help 920 help
911 If you say yes here you get support for the Winbond W83793 921 If you say yes here you get support for the Winbond W83793
912 hardware monitoring chip. 922 hardware monitoring chip, including support for the integrated
923 watchdog.
913 924
914 This driver can also be built as a module. If so, the module 925 This driver can also be built as a module. If so, the module
915 will be called w83793. 926 will be called w83793.
diff --git a/drivers/hwmon/Makefile b/drivers/hwmon/Makefile
index 4bc215c0953f..4aa1a3d112ad 100644
--- a/drivers/hwmon/Makefile
+++ b/drivers/hwmon/Makefile
@@ -29,12 +29,13 @@ obj-$(CONFIG_SENSORS_ADM1029) += adm1029.o
29obj-$(CONFIG_SENSORS_ADM1031) += adm1031.o 29obj-$(CONFIG_SENSORS_ADM1031) += adm1031.o
30obj-$(CONFIG_SENSORS_ADM9240) += adm9240.o 30obj-$(CONFIG_SENSORS_ADM9240) += adm9240.o
31obj-$(CONFIG_SENSORS_ADS7828) += ads7828.o 31obj-$(CONFIG_SENSORS_ADS7828) += ads7828.o
32obj-$(CONFIG_SENSORS_ADT7411) += adt7411.o
32obj-$(CONFIG_SENSORS_ADT7462) += adt7462.o 33obj-$(CONFIG_SENSORS_ADT7462) += adt7462.o
33obj-$(CONFIG_SENSORS_ADT7470) += adt7470.o 34obj-$(CONFIG_SENSORS_ADT7470) += adt7470.o
34obj-$(CONFIG_SENSORS_ADT7473) += adt7473.o
35obj-$(CONFIG_SENSORS_ADT7475) += adt7475.o 35obj-$(CONFIG_SENSORS_ADT7475) += adt7475.o
36obj-$(CONFIG_SENSORS_APPLESMC) += applesmc.o 36obj-$(CONFIG_SENSORS_APPLESMC) += applesmc.o
37obj-$(CONFIG_SENSORS_AMS) += ams/ 37obj-$(CONFIG_SENSORS_AMS) += ams/
38obj-$(CONFIG_SENSORS_ASC7621) += asc7621.o
38obj-$(CONFIG_SENSORS_ATXP1) += atxp1.o 39obj-$(CONFIG_SENSORS_ATXP1) += atxp1.o
39obj-$(CONFIG_SENSORS_CORETEMP) += coretemp.o 40obj-$(CONFIG_SENSORS_CORETEMP) += coretemp.o
40obj-$(CONFIG_SENSORS_DME1737) += dme1737.o 41obj-$(CONFIG_SENSORS_DME1737) += dme1737.o
diff --git a/drivers/hwmon/adcxx.c b/drivers/hwmon/adcxx.c
index 5e9e095f1136..74d9c5195e44 100644
--- a/drivers/hwmon/adcxx.c
+++ b/drivers/hwmon/adcxx.c
@@ -62,18 +62,23 @@ static ssize_t adcxx_read(struct device *dev,
62 struct spi_device *spi = to_spi_device(dev); 62 struct spi_device *spi = to_spi_device(dev);
63 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 63 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
64 struct adcxx *adc = dev_get_drvdata(&spi->dev); 64 struct adcxx *adc = dev_get_drvdata(&spi->dev);
65 u8 tx_buf[2] = { attr->index << 3 }; /* other bits are don't care */ 65 u8 tx_buf[2];
66 u8 rx_buf[2]; 66 u8 rx_buf[2];
67 int status; 67 int status;
68 int value; 68 u32 value;
69 69
70 if (mutex_lock_interruptible(&adc->lock)) 70 if (mutex_lock_interruptible(&adc->lock))
71 return -ERESTARTSYS; 71 return -ERESTARTSYS;
72 72
73 status = spi_write_then_read(spi, tx_buf, sizeof(tx_buf), 73 if (adc->channels == 1) {
74 rx_buf, sizeof(rx_buf)); 74 status = spi_read(spi, rx_buf, sizeof(rx_buf));
75 } else {
76 tx_buf[0] = attr->index << 3; /* other bits are don't care */
77 status = spi_write_then_read(spi, tx_buf, sizeof(tx_buf),
78 rx_buf, sizeof(rx_buf));
79 }
75 if (status < 0) { 80 if (status < 0) {
76 dev_warn(dev, "spi_write_then_read failed with status %d\n", 81 dev_warn(dev, "SPI synch. transfer failed with status %d\n",
77 status); 82 status);
78 goto out; 83 goto out;
79 } 84 }
diff --git a/drivers/hwmon/adt7411.c b/drivers/hwmon/adt7411.c
new file mode 100644
index 000000000000..3471884e42d2
--- /dev/null
+++ b/drivers/hwmon/adt7411.c
@@ -0,0 +1,366 @@
1/*
2 * Driver for the ADT7411 (I2C/SPI 8 channel 10 bit ADC & temperature-sensor)
3 *
4 * Copyright (C) 2008, 2010 Pengutronix
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 version 2 as
8 * published by the Free Software Foundation.
9 *
10 * TODO: SPI, support for external temperature sensor
11 * use power-down mode for suspend?, interrupt handling?
12 */
13
14#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/init.h>
17#include <linux/err.h>
18#include <linux/delay.h>
19#include <linux/mutex.h>
20#include <linux/jiffies.h>
21#include <linux/i2c.h>
22#include <linux/hwmon.h>
23#include <linux/hwmon-sysfs.h>
24
25#define ADT7411_REG_INT_TEMP_VDD_LSB 0x03
26#define ADT7411_REG_EXT_TEMP_AIN14_LSB 0x04
27#define ADT7411_REG_VDD_MSB 0x06
28#define ADT7411_REG_INT_TEMP_MSB 0x07
29#define ADT7411_REG_EXT_TEMP_AIN1_MSB 0x08
30
31#define ADT7411_REG_CFG1 0x18
32#define ADT7411_CFG1_START_MONITOR (1 << 0)
33
34#define ADT7411_REG_CFG2 0x19
35#define ADT7411_CFG2_DISABLE_AVG (1 << 5)
36
37#define ADT7411_REG_CFG3 0x1a
38#define ADT7411_CFG3_ADC_CLK_225 (1 << 0)
39#define ADT7411_CFG3_REF_VDD (1 << 4)
40
41#define ADT7411_REG_DEVICE_ID 0x4d
42#define ADT7411_REG_MANUFACTURER_ID 0x4e
43
44#define ADT7411_DEVICE_ID 0x2
45#define ADT7411_MANUFACTURER_ID 0x41
46
47static const unsigned short normal_i2c[] = { 0x48, 0x4a, 0x4b, I2C_CLIENT_END };
48
49struct adt7411_data {
50 struct mutex device_lock; /* for "atomic" device accesses */
51 struct mutex update_lock;
52 unsigned long next_update;
53 int vref_cached;
54 struct device *hwmon_dev;
55};
56
57/*
58 * When reading a register containing (up to 4) lsb, all associated
59 * msb-registers get locked by the hardware. After _one_ of those msb is read,
60 * _all_ are unlocked. In order to use this locking correctly, reading lsb/msb
61 * is protected here with a mutex, too.
62 */
63static int adt7411_read_10_bit(struct i2c_client *client, u8 lsb_reg,
64 u8 msb_reg, u8 lsb_shift)
65{
66 struct adt7411_data *data = i2c_get_clientdata(client);
67 int val, tmp;
68
69 mutex_lock(&data->device_lock);
70
71 val = i2c_smbus_read_byte_data(client, lsb_reg);
72 if (val < 0)
73 goto exit_unlock;
74
75 tmp = (val >> lsb_shift) & 3;
76 val = i2c_smbus_read_byte_data(client, msb_reg);
77
78 if (val >= 0)
79 val = (val << 2) | tmp;
80
81 exit_unlock:
82 mutex_unlock(&data->device_lock);
83
84 return val;
85}
86
87static int adt7411_modify_bit(struct i2c_client *client, u8 reg, u8 bit,
88 bool flag)
89{
90 struct adt7411_data *data = i2c_get_clientdata(client);
91 int ret, val;
92
93 mutex_lock(&data->device_lock);
94
95 ret = i2c_smbus_read_byte_data(client, reg);
96 if (ret < 0)
97 goto exit_unlock;
98
99 if (flag)
100 val = ret | bit;
101 else
102 val = ret & ~bit;
103
104 ret = i2c_smbus_write_byte_data(client, reg, val);
105
106 exit_unlock:
107 mutex_unlock(&data->device_lock);
108 return ret;
109}
110
111static ssize_t adt7411_show_vdd(struct device *dev,
112 struct device_attribute *attr, char *buf)
113{
114 struct i2c_client *client = to_i2c_client(dev);
115 int ret = adt7411_read_10_bit(client, ADT7411_REG_INT_TEMP_VDD_LSB,
116 ADT7411_REG_VDD_MSB, 2);
117
118 return ret < 0 ? ret : sprintf(buf, "%u\n", ret * 7000 / 1024);
119}
120
121static ssize_t adt7411_show_temp(struct device *dev,
122 struct device_attribute *attr, char *buf)
123{
124 struct i2c_client *client = to_i2c_client(dev);
125 int val = adt7411_read_10_bit(client, ADT7411_REG_INT_TEMP_VDD_LSB,
126 ADT7411_REG_INT_TEMP_MSB, 0);
127
128 if (val < 0)
129 return val;
130
131 val = val & 0x200 ? val - 0x400 : val; /* 10 bit signed */
132
133 return sprintf(buf, "%d\n", val * 250);
134}
135
136static ssize_t adt7411_show_input(struct device *dev,
137 struct device_attribute *attr, char *buf)
138{
139 int nr = to_sensor_dev_attr(attr)->index;
140 struct i2c_client *client = to_i2c_client(dev);
141 struct adt7411_data *data = i2c_get_clientdata(client);
142 int val;
143 u8 lsb_reg, lsb_shift;
144
145 mutex_lock(&data->update_lock);
146 if (time_after_eq(jiffies, data->next_update)) {
147 val = i2c_smbus_read_byte_data(client, ADT7411_REG_CFG3);
148 if (val < 0)
149 goto exit_unlock;
150
151 if (val & ADT7411_CFG3_REF_VDD) {
152 val = adt7411_read_10_bit(client,
153 ADT7411_REG_INT_TEMP_VDD_LSB,
154 ADT7411_REG_VDD_MSB, 2);
155 if (val < 0)
156 goto exit_unlock;
157
158 data->vref_cached = val * 7000 / 1024;
159 } else {
160 data->vref_cached = 2250;
161 }
162
163 data->next_update = jiffies + HZ;
164 }
165
166 lsb_reg = ADT7411_REG_EXT_TEMP_AIN14_LSB + (nr >> 2);
167 lsb_shift = 2 * (nr & 0x03);
168 val = adt7411_read_10_bit(client, lsb_reg,
169 ADT7411_REG_EXT_TEMP_AIN1_MSB + nr, lsb_shift);
170 if (val < 0)
171 goto exit_unlock;
172
173 val = sprintf(buf, "%u\n", val * data->vref_cached / 1024);
174 exit_unlock:
175 mutex_unlock(&data->update_lock);
176 return val;
177}
178
179static ssize_t adt7411_show_bit(struct device *dev,
180 struct device_attribute *attr, char *buf)
181{
182 struct sensor_device_attribute_2 *attr2 = to_sensor_dev_attr_2(attr);
183 struct i2c_client *client = to_i2c_client(dev);
184 int ret = i2c_smbus_read_byte_data(client, attr2->index);
185
186 return ret < 0 ? ret : sprintf(buf, "%u\n", !!(ret & attr2->nr));
187}
188
189static ssize_t adt7411_set_bit(struct device *dev,
190 struct device_attribute *attr, const char *buf,
191 size_t count)
192{
193 struct sensor_device_attribute_2 *s_attr2 = to_sensor_dev_attr_2(attr);
194 struct i2c_client *client = to_i2c_client(dev);
195 struct adt7411_data *data = i2c_get_clientdata(client);
196 int ret;
197 unsigned long flag;
198
199 ret = strict_strtoul(buf, 0, &flag);
200 if (ret || flag > 1)
201 return -EINVAL;
202
203 ret = adt7411_modify_bit(client, s_attr2->index, s_attr2->nr, flag);
204
205 /* force update */
206 mutex_lock(&data->update_lock);
207 data->next_update = jiffies;
208 mutex_unlock(&data->update_lock);
209
210 return ret < 0 ? ret : count;
211}
212
213#define ADT7411_BIT_ATTR(__name, __reg, __bit) \
214 SENSOR_DEVICE_ATTR_2(__name, S_IRUGO | S_IWUSR, adt7411_show_bit, \
215 adt7411_set_bit, __bit, __reg)
216
217static DEVICE_ATTR(temp1_input, S_IRUGO, adt7411_show_temp, NULL);
218static DEVICE_ATTR(in0_input, S_IRUGO, adt7411_show_vdd, NULL);
219static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, adt7411_show_input, NULL, 0);
220static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, adt7411_show_input, NULL, 1);
221static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, adt7411_show_input, NULL, 2);
222static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, adt7411_show_input, NULL, 3);
223static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, adt7411_show_input, NULL, 4);
224static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, adt7411_show_input, NULL, 5);
225static SENSOR_DEVICE_ATTR(in7_input, S_IRUGO, adt7411_show_input, NULL, 6);
226static SENSOR_DEVICE_ATTR(in8_input, S_IRUGO, adt7411_show_input, NULL, 7);
227static ADT7411_BIT_ATTR(no_average, ADT7411_REG_CFG2, ADT7411_CFG2_DISABLE_AVG);
228static ADT7411_BIT_ATTR(fast_sampling, ADT7411_REG_CFG3, ADT7411_CFG3_ADC_CLK_225);
229static ADT7411_BIT_ATTR(adc_ref_vdd, ADT7411_REG_CFG3, ADT7411_CFG3_REF_VDD);
230
231static struct attribute *adt7411_attrs[] = {
232 &dev_attr_temp1_input.attr,
233 &dev_attr_in0_input.attr,
234 &sensor_dev_attr_in1_input.dev_attr.attr,
235 &sensor_dev_attr_in2_input.dev_attr.attr,
236 &sensor_dev_attr_in3_input.dev_attr.attr,
237 &sensor_dev_attr_in4_input.dev_attr.attr,
238 &sensor_dev_attr_in5_input.dev_attr.attr,
239 &sensor_dev_attr_in6_input.dev_attr.attr,
240 &sensor_dev_attr_in7_input.dev_attr.attr,
241 &sensor_dev_attr_in8_input.dev_attr.attr,
242 &sensor_dev_attr_no_average.dev_attr.attr,
243 &sensor_dev_attr_fast_sampling.dev_attr.attr,
244 &sensor_dev_attr_adc_ref_vdd.dev_attr.attr,
245 NULL
246};
247
248static const struct attribute_group adt7411_attr_grp = {
249 .attrs = adt7411_attrs,
250};
251
252static int adt7411_detect(struct i2c_client *client,
253 struct i2c_board_info *info)
254{
255 int val;
256
257 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
258 return -ENODEV;
259
260 val = i2c_smbus_read_byte_data(client, ADT7411_REG_MANUFACTURER_ID);
261 if (val < 0 || val != ADT7411_MANUFACTURER_ID) {
262 dev_dbg(&client->dev, "Wrong manufacturer ID. Got %d, "
263 "expected %d\n", val, ADT7411_MANUFACTURER_ID);
264 return -ENODEV;
265 }
266
267 val = i2c_smbus_read_byte_data(client, ADT7411_REG_DEVICE_ID);
268 if (val < 0 || val != ADT7411_DEVICE_ID) {
269 dev_dbg(&client->dev, "Wrong device ID. Got %d, "
270 "expected %d\n", val, ADT7411_DEVICE_ID);
271 return -ENODEV;
272 }
273
274 strlcpy(info->type, "adt7411", I2C_NAME_SIZE);
275
276 return 0;
277}
278
279static int __devinit adt7411_probe(struct i2c_client *client,
280 const struct i2c_device_id *id)
281{
282 struct adt7411_data *data;
283 int ret;
284
285 data = kzalloc(sizeof(*data), GFP_KERNEL);
286 if (!data)
287 return -ENOMEM;
288
289 i2c_set_clientdata(client, data);
290 mutex_init(&data->device_lock);
291 mutex_init(&data->update_lock);
292
293 ret = adt7411_modify_bit(client, ADT7411_REG_CFG1,
294 ADT7411_CFG1_START_MONITOR, 1);
295 if (ret < 0)
296 goto exit_free;
297
298 /* force update on first occasion */
299 data->next_update = jiffies;
300
301 ret = sysfs_create_group(&client->dev.kobj, &adt7411_attr_grp);
302 if (ret)
303 goto exit_free;
304
305 data->hwmon_dev = hwmon_device_register(&client->dev);
306 if (IS_ERR(data->hwmon_dev)) {
307 ret = PTR_ERR(data->hwmon_dev);
308 goto exit_remove;
309 }
310
311 dev_info(&client->dev, "successfully registered\n");
312
313 return 0;
314
315 exit_remove:
316 sysfs_remove_group(&client->dev.kobj, &adt7411_attr_grp);
317 exit_free:
318 i2c_set_clientdata(client, NULL);
319 kfree(data);
320 return ret;
321}
322
323static int __devexit adt7411_remove(struct i2c_client *client)
324{
325 struct adt7411_data *data = i2c_get_clientdata(client);
326
327 hwmon_device_unregister(data->hwmon_dev);
328 sysfs_remove_group(&client->dev.kobj, &adt7411_attr_grp);
329 i2c_set_clientdata(client, NULL);
330 kfree(data);
331 return 0;
332}
333
334static const struct i2c_device_id adt7411_id[] = {
335 { "adt7411", 0 },
336 { }
337};
338
339static struct i2c_driver adt7411_driver = {
340 .driver = {
341 .name = "adt7411",
342 },
343 .probe = adt7411_probe,
344 .remove = __devexit_p(adt7411_remove),
345 .id_table = adt7411_id,
346 .detect = adt7411_detect,
347 .address_list = normal_i2c,
348 .class = I2C_CLASS_HWMON,
349};
350
351static int __init sensors_adt7411_init(void)
352{
353 return i2c_add_driver(&adt7411_driver);
354}
355module_init(sensors_adt7411_init)
356
357static void __exit sensors_adt7411_exit(void)
358{
359 i2c_del_driver(&adt7411_driver);
360}
361module_exit(sensors_adt7411_exit)
362
363MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de> and "
364 "Wolfram Sang <w.sang@pengutronix.de>");
365MODULE_DESCRIPTION("ADT7411 driver");
366MODULE_LICENSE("GPL v2");
diff --git a/drivers/hwmon/adt7473.c b/drivers/hwmon/adt7473.c
deleted file mode 100644
index 434576f61c84..000000000000
--- a/drivers/hwmon/adt7473.c
+++ /dev/null
@@ -1,1180 +0,0 @@
1/*
2 * A hwmon driver for the Analog Devices ADT7473
3 * Copyright (C) 2007 IBM
4 *
5 * Author: Darrick J. Wong <djwong@us.ibm.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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22#include <linux/module.h>
23#include <linux/jiffies.h>
24#include <linux/i2c.h>
25#include <linux/hwmon.h>
26#include <linux/hwmon-sysfs.h>
27#include <linux/err.h>
28#include <linux/mutex.h>
29#include <linux/delay.h>
30#include <linux/log2.h>
31
32/* Addresses to scan */
33static const unsigned short normal_i2c[] = { 0x2C, 0x2D, 0x2E, I2C_CLIENT_END };
34
35/* ADT7473 registers */
36#define ADT7473_REG_BASE_ADDR 0x20
37
38#define ADT7473_REG_VOLT_BASE_ADDR 0x21
39#define ADT7473_REG_VOLT_MIN_BASE_ADDR 0x46
40
41#define ADT7473_REG_TEMP_BASE_ADDR 0x25
42#define ADT7473_REG_TEMP_LIMITS_BASE_ADDR 0x4E
43#define ADT7473_REG_TEMP_TMIN_BASE_ADDR 0x67
44#define ADT7473_REG_TEMP_TMAX_BASE_ADDR 0x6A
45
46#define ADT7473_REG_FAN_BASE_ADDR 0x28
47#define ADT7473_REG_FAN_MIN_BASE_ADDR 0x54
48
49#define ADT7473_REG_PWM_BASE_ADDR 0x30
50#define ADT7473_REG_PWM_MIN_BASE_ADDR 0x64
51#define ADT7473_REG_PWM_MAX_BASE_ADDR 0x38
52#define ADT7473_REG_PWM_BHVR_BASE_ADDR 0x5C
53#define ADT7473_PWM_BHVR_MASK 0xE0
54#define ADT7473_PWM_BHVR_SHIFT 5
55
56#define ADT7473_REG_CFG1 0x40
57#define ADT7473_CFG1_START 0x01
58#define ADT7473_CFG1_READY 0x04
59#define ADT7473_REG_CFG2 0x73
60#define ADT7473_REG_CFG3 0x78
61#define ADT7473_REG_CFG4 0x7D
62#define ADT7473_CFG4_MAX_DUTY_AT_OVT 0x08
63#define ADT7473_REG_CFG5 0x7C
64#define ADT7473_CFG5_TEMP_TWOS 0x01
65#define ADT7473_CFG5_TEMP_OFFSET 0x02
66
67#define ADT7473_REG_DEVICE 0x3D
68#define ADT7473_VENDOR 0x41
69#define ADT7473_REG_VENDOR 0x3E
70#define ADT7473_DEVICE 0x73
71#define ADT7473_REG_REVISION 0x3F
72#define ADT7473_REV_68 0x68
73#define ADT7473_REV_69 0x69
74
75#define ADT7473_REG_ALARM1 0x41
76#define ADT7473_VCCP_ALARM 0x02
77#define ADT7473_VCC_ALARM 0x04
78#define ADT7473_R1T_ALARM 0x10
79#define ADT7473_LT_ALARM 0x20
80#define ADT7473_R2T_ALARM 0x40
81#define ADT7473_OOL 0x80
82#define ADT7473_REG_ALARM2 0x42
83#define ADT7473_OVT_ALARM 0x02
84#define ADT7473_FAN1_ALARM 0x04
85#define ADT7473_FAN2_ALARM 0x08
86#define ADT7473_FAN3_ALARM 0x10
87#define ADT7473_FAN4_ALARM 0x20
88#define ADT7473_R1T_SHORT 0x40
89#define ADT7473_R2T_SHORT 0x80
90
91#define ALARM2(x) ((x) << 8)
92
93#define ADT7473_VOLT_COUNT 2
94#define ADT7473_REG_VOLT(x) (ADT7473_REG_VOLT_BASE_ADDR + (x))
95#define ADT7473_REG_VOLT_MIN(x) (ADT7473_REG_VOLT_MIN_BASE_ADDR + ((x) * 2))
96#define ADT7473_REG_VOLT_MAX(x) (ADT7473_REG_VOLT_MIN_BASE_ADDR + \
97 ((x) * 2) + 1)
98
99#define ADT7473_TEMP_COUNT 3
100#define ADT7473_REG_TEMP(x) (ADT7473_REG_TEMP_BASE_ADDR + (x))
101#define ADT7473_REG_TEMP_MIN(x) (ADT7473_REG_TEMP_LIMITS_BASE_ADDR + ((x) * 2))
102#define ADT7473_REG_TEMP_MAX(x) (ADT7473_REG_TEMP_LIMITS_BASE_ADDR + \
103 ((x) * 2) + 1)
104#define ADT7473_REG_TEMP_TMIN(x) (ADT7473_REG_TEMP_TMIN_BASE_ADDR + (x))
105#define ADT7473_REG_TEMP_TMAX(x) (ADT7473_REG_TEMP_TMAX_BASE_ADDR + (x))
106
107#define ADT7473_FAN_COUNT 4
108#define ADT7473_REG_FAN(x) (ADT7473_REG_FAN_BASE_ADDR + ((x) * 2))
109#define ADT7473_REG_FAN_MIN(x) (ADT7473_REG_FAN_MIN_BASE_ADDR + ((x) * 2))
110
111#define ADT7473_PWM_COUNT 3
112#define ADT7473_REG_PWM(x) (ADT7473_REG_PWM_BASE_ADDR + (x))
113#define ADT7473_REG_PWM_MAX(x) (ADT7473_REG_PWM_MAX_BASE_ADDR + (x))
114#define ADT7473_REG_PWM_MIN(x) (ADT7473_REG_PWM_MIN_BASE_ADDR + (x))
115#define ADT7473_REG_PWM_BHVR(x) (ADT7473_REG_PWM_BHVR_BASE_ADDR + (x))
116
117/* How often do we reread sensors values? (In jiffies) */
118#define SENSOR_REFRESH_INTERVAL (2 * HZ)
119
120/* How often do we reread sensor limit values? (In jiffies) */
121#define LIMIT_REFRESH_INTERVAL (60 * HZ)
122
123/* datasheet says to divide this number by the fan reading to get fan rpm */
124#define FAN_PERIOD_TO_RPM(x) ((90000 * 60) / (x))
125#define FAN_RPM_TO_PERIOD FAN_PERIOD_TO_RPM
126#define FAN_PERIOD_INVALID 65535
127#define FAN_DATA_VALID(x) ((x) && (x) != FAN_PERIOD_INVALID)
128
129struct adt7473_data {
130 struct device *hwmon_dev;
131 struct attribute_group attrs;
132 struct mutex lock;
133 char sensors_valid;
134 char limits_valid;
135 unsigned long sensors_last_updated; /* In jiffies */
136 unsigned long limits_last_updated; /* In jiffies */
137
138 u8 volt[ADT7473_VOLT_COUNT];
139 s8 volt_min[ADT7473_VOLT_COUNT];
140 s8 volt_max[ADT7473_VOLT_COUNT];
141
142 s8 temp[ADT7473_TEMP_COUNT];
143 s8 temp_min[ADT7473_TEMP_COUNT];
144 s8 temp_max[ADT7473_TEMP_COUNT];
145 s8 temp_tmin[ADT7473_TEMP_COUNT];
146 /* This is called the !THERM limit in the datasheet */
147 s8 temp_tmax[ADT7473_TEMP_COUNT];
148
149 u16 fan[ADT7473_FAN_COUNT];
150 u16 fan_min[ADT7473_FAN_COUNT];
151
152 u8 pwm[ADT7473_PWM_COUNT];
153 u8 pwm_max[ADT7473_PWM_COUNT];
154 u8 pwm_min[ADT7473_PWM_COUNT];
155 u8 pwm_behavior[ADT7473_PWM_COUNT];
156
157 u8 temp_twos_complement;
158 u8 temp_offset;
159
160 u16 alarm;
161 u8 max_duty_at_overheat;
162};
163
164static int adt7473_probe(struct i2c_client *client,
165 const struct i2c_device_id *id);
166static int adt7473_detect(struct i2c_client *client,
167 struct i2c_board_info *info);
168static int adt7473_remove(struct i2c_client *client);
169
170static const struct i2c_device_id adt7473_id[] = {
171 { "adt7473", 0 },
172 { }
173};
174
175static struct i2c_driver adt7473_driver = {
176 .class = I2C_CLASS_HWMON,
177 .driver = {
178 .name = "adt7473",
179 },
180 .probe = adt7473_probe,
181 .remove = adt7473_remove,
182 .id_table = adt7473_id,
183 .detect = adt7473_detect,
184 .address_list = normal_i2c,
185};
186
187/*
188 * 16-bit registers on the ADT7473 are low-byte first. The data sheet says
189 * that the low byte must be read before the high byte.
190 */
191static inline int adt7473_read_word_data(struct i2c_client *client, u8 reg)
192{
193 u16 foo;
194 foo = i2c_smbus_read_byte_data(client, reg);
195 foo |= ((u16)i2c_smbus_read_byte_data(client, reg + 1) << 8);
196 return foo;
197}
198
199static inline int adt7473_write_word_data(struct i2c_client *client, u8 reg,
200 u16 value)
201{
202 return i2c_smbus_write_byte_data(client, reg, value & 0xFF)
203 && i2c_smbus_write_byte_data(client, reg + 1, value >> 8);
204}
205
206static void adt7473_init_client(struct i2c_client *client)
207{
208 int reg = i2c_smbus_read_byte_data(client, ADT7473_REG_CFG1);
209
210 if (!(reg & ADT7473_CFG1_READY)) {
211 dev_err(&client->dev, "Chip not ready.\n");
212 } else {
213 /* start monitoring */
214 i2c_smbus_write_byte_data(client, ADT7473_REG_CFG1,
215 reg | ADT7473_CFG1_START);
216 }
217}
218
219static struct adt7473_data *adt7473_update_device(struct device *dev)
220{
221 struct i2c_client *client = to_i2c_client(dev);
222 struct adt7473_data *data = i2c_get_clientdata(client);
223 unsigned long local_jiffies = jiffies;
224 u8 cfg;
225 int i;
226
227 mutex_lock(&data->lock);
228 if (time_before(local_jiffies, data->sensors_last_updated +
229 SENSOR_REFRESH_INTERVAL)
230 && data->sensors_valid)
231 goto no_sensor_update;
232
233 for (i = 0; i < ADT7473_VOLT_COUNT; i++)
234 data->volt[i] = i2c_smbus_read_byte_data(client,
235 ADT7473_REG_VOLT(i));
236
237 /* Determine temperature encoding */
238 cfg = i2c_smbus_read_byte_data(client, ADT7473_REG_CFG5);
239 data->temp_twos_complement = (cfg & ADT7473_CFG5_TEMP_TWOS);
240
241 /*
242 * What does this do? it implies a variable temperature sensor
243 * offset, but the datasheet doesn't say anything about this bit
244 * and other parts of the datasheet imply that "offset64" mode
245 * means that you shift temp values by -64 if the above bit was set.
246 */
247 data->temp_offset = (cfg & ADT7473_CFG5_TEMP_OFFSET);
248
249 for (i = 0; i < ADT7473_TEMP_COUNT; i++)
250 data->temp[i] = i2c_smbus_read_byte_data(client,
251 ADT7473_REG_TEMP(i));
252
253 for (i = 0; i < ADT7473_FAN_COUNT; i++)
254 data->fan[i] = adt7473_read_word_data(client,
255 ADT7473_REG_FAN(i));
256
257 for (i = 0; i < ADT7473_PWM_COUNT; i++)
258 data->pwm[i] = i2c_smbus_read_byte_data(client,
259 ADT7473_REG_PWM(i));
260
261 data->alarm = i2c_smbus_read_byte_data(client, ADT7473_REG_ALARM1);
262 if (data->alarm & ADT7473_OOL)
263 data->alarm |= ALARM2(i2c_smbus_read_byte_data(client,
264 ADT7473_REG_ALARM2));
265
266 data->sensors_last_updated = local_jiffies;
267 data->sensors_valid = 1;
268
269no_sensor_update:
270 if (time_before(local_jiffies, data->limits_last_updated +
271 LIMIT_REFRESH_INTERVAL)
272 && data->limits_valid)
273 goto out;
274
275 for (i = 0; i < ADT7473_VOLT_COUNT; i++) {
276 data->volt_min[i] = i2c_smbus_read_byte_data(client,
277 ADT7473_REG_VOLT_MIN(i));
278 data->volt_max[i] = i2c_smbus_read_byte_data(client,
279 ADT7473_REG_VOLT_MAX(i));
280 }
281
282 for (i = 0; i < ADT7473_TEMP_COUNT; i++) {
283 data->temp_min[i] = i2c_smbus_read_byte_data(client,
284 ADT7473_REG_TEMP_MIN(i));
285 data->temp_max[i] = i2c_smbus_read_byte_data(client,
286 ADT7473_REG_TEMP_MAX(i));
287 data->temp_tmin[i] = i2c_smbus_read_byte_data(client,
288 ADT7473_REG_TEMP_TMIN(i));
289 data->temp_tmax[i] = i2c_smbus_read_byte_data(client,
290 ADT7473_REG_TEMP_TMAX(i));
291 }
292
293 for (i = 0; i < ADT7473_FAN_COUNT; i++)
294 data->fan_min[i] = adt7473_read_word_data(client,
295 ADT7473_REG_FAN_MIN(i));
296
297 for (i = 0; i < ADT7473_PWM_COUNT; i++) {
298 data->pwm_max[i] = i2c_smbus_read_byte_data(client,
299 ADT7473_REG_PWM_MAX(i));
300 data->pwm_min[i] = i2c_smbus_read_byte_data(client,
301 ADT7473_REG_PWM_MIN(i));
302 data->pwm_behavior[i] = i2c_smbus_read_byte_data(client,
303 ADT7473_REG_PWM_BHVR(i));
304 }
305
306 i = i2c_smbus_read_byte_data(client, ADT7473_REG_CFG4);
307 data->max_duty_at_overheat = !!(i & ADT7473_CFG4_MAX_DUTY_AT_OVT);
308
309 data->limits_last_updated = local_jiffies;
310 data->limits_valid = 1;
311
312out:
313 mutex_unlock(&data->lock);
314 return data;
315}
316
317/*
318 * Conversions
319 */
320
321/* IN are scaled acording to built-in resistors */
322static const int adt7473_scaling[] = { /* .001 Volts */
323 2250, 3300
324};
325#define SCALE(val, from, to) (((val) * (to) + ((from) / 2)) / (from))
326
327static int decode_volt(int volt_index, u8 raw)
328{
329 return SCALE(raw, 192, adt7473_scaling[volt_index]);
330}
331
332static u8 encode_volt(int volt_index, int cooked)
333{
334 int raw = SCALE(cooked, adt7473_scaling[volt_index], 192);
335 return SENSORS_LIMIT(raw, 0, 255);
336}
337
338static ssize_t show_volt_min(struct device *dev,
339 struct device_attribute *devattr,
340 char *buf)
341{
342 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
343 struct adt7473_data *data = adt7473_update_device(dev);
344 return sprintf(buf, "%d\n",
345 decode_volt(attr->index, data->volt_min[attr->index]));
346}
347
348static ssize_t set_volt_min(struct device *dev,
349 struct device_attribute *devattr,
350 const char *buf,
351 size_t count)
352{
353 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
354 struct i2c_client *client = to_i2c_client(dev);
355 struct adt7473_data *data = i2c_get_clientdata(client);
356 long volt;
357
358 if (strict_strtol(buf, 10, &volt))
359 return -EINVAL;
360
361 volt = encode_volt(attr->index, volt);
362
363 mutex_lock(&data->lock);
364 data->volt_min[attr->index] = volt;
365 i2c_smbus_write_byte_data(client, ADT7473_REG_VOLT_MIN(attr->index),
366 volt);
367 mutex_unlock(&data->lock);
368
369 return count;
370}
371
372static ssize_t show_volt_max(struct device *dev,
373 struct device_attribute *devattr,
374 char *buf)
375{
376 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
377 struct adt7473_data *data = adt7473_update_device(dev);
378 return sprintf(buf, "%d\n",
379 decode_volt(attr->index, data->volt_max[attr->index]));
380}
381
382static ssize_t set_volt_max(struct device *dev,
383 struct device_attribute *devattr,
384 const char *buf,
385 size_t count)
386{
387 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
388 struct i2c_client *client = to_i2c_client(dev);
389 struct adt7473_data *data = i2c_get_clientdata(client);
390 long volt;
391
392 if (strict_strtol(buf, 10, &volt))
393 return -EINVAL;
394
395 volt = encode_volt(attr->index, volt);
396
397 mutex_lock(&data->lock);
398 data->volt_max[attr->index] = volt;
399 i2c_smbus_write_byte_data(client, ADT7473_REG_VOLT_MAX(attr->index),
400 volt);
401 mutex_unlock(&data->lock);
402
403 return count;
404}
405
406static ssize_t show_volt(struct device *dev, struct device_attribute *devattr,
407 char *buf)
408{
409 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
410 struct adt7473_data *data = adt7473_update_device(dev);
411
412 return sprintf(buf, "%d\n",
413 decode_volt(attr->index, data->volt[attr->index]));
414}
415
416/*
417 * This chip can report temperature data either as a two's complement
418 * number in the range -128 to 127, or as an unsigned number that must
419 * be offset by 64.
420 */
421static int decode_temp(u8 twos_complement, u8 raw)
422{
423 return twos_complement ? (s8)raw : raw - 64;
424}
425
426static u8 encode_temp(u8 twos_complement, int cooked)
427{
428 u8 ret = twos_complement ? cooked & 0xFF : cooked + 64;
429 return SENSORS_LIMIT(ret, 0, 255);
430}
431
432static ssize_t show_temp_min(struct device *dev,
433 struct device_attribute *devattr,
434 char *buf)
435{
436 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
437 struct adt7473_data *data = adt7473_update_device(dev);
438 return sprintf(buf, "%d\n", 1000 * decode_temp(
439 data->temp_twos_complement,
440 data->temp_min[attr->index]));
441}
442
443static ssize_t set_temp_min(struct device *dev,
444 struct device_attribute *devattr,
445 const char *buf,
446 size_t count)
447{
448 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
449 struct i2c_client *client = to_i2c_client(dev);
450 struct adt7473_data *data = i2c_get_clientdata(client);
451 long temp;
452
453 if (strict_strtol(buf, 10, &temp))
454 return -EINVAL;
455
456 temp = DIV_ROUND_CLOSEST(temp, 1000);
457 temp = encode_temp(data->temp_twos_complement, temp);
458
459 mutex_lock(&data->lock);
460 data->temp_min[attr->index] = temp;
461 i2c_smbus_write_byte_data(client, ADT7473_REG_TEMP_MIN(attr->index),
462 temp);
463 mutex_unlock(&data->lock);
464
465 return count;
466}
467
468static ssize_t show_temp_max(struct device *dev,
469 struct device_attribute *devattr,
470 char *buf)
471{
472 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
473 struct adt7473_data *data = adt7473_update_device(dev);
474 return sprintf(buf, "%d\n", 1000 * decode_temp(
475 data->temp_twos_complement,
476 data->temp_max[attr->index]));
477}
478
479static ssize_t set_temp_max(struct device *dev,
480 struct device_attribute *devattr,
481 const char *buf,
482 size_t count)
483{
484 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
485 struct i2c_client *client = to_i2c_client(dev);
486 struct adt7473_data *data = i2c_get_clientdata(client);
487 long temp;
488
489 if (strict_strtol(buf, 10, &temp))
490 return -EINVAL;
491
492 temp = DIV_ROUND_CLOSEST(temp, 1000);
493 temp = encode_temp(data->temp_twos_complement, temp);
494
495 mutex_lock(&data->lock);
496 data->temp_max[attr->index] = temp;
497 i2c_smbus_write_byte_data(client, ADT7473_REG_TEMP_MAX(attr->index),
498 temp);
499 mutex_unlock(&data->lock);
500
501 return count;
502}
503
504static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
505 char *buf)
506{
507 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
508 struct adt7473_data *data = adt7473_update_device(dev);
509 return sprintf(buf, "%d\n", 1000 * decode_temp(
510 data->temp_twos_complement,
511 data->temp[attr->index]));
512}
513
514static ssize_t show_fan_min(struct device *dev,
515 struct device_attribute *devattr,
516 char *buf)
517{
518 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
519 struct adt7473_data *data = adt7473_update_device(dev);
520
521 if (FAN_DATA_VALID(data->fan_min[attr->index]))
522 return sprintf(buf, "%d\n",
523 FAN_PERIOD_TO_RPM(data->fan_min[attr->index]));
524 else
525 return sprintf(buf, "0\n");
526}
527
528static ssize_t set_fan_min(struct device *dev,
529 struct device_attribute *devattr,
530 const char *buf, size_t count)
531{
532 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
533 struct i2c_client *client = to_i2c_client(dev);
534 struct adt7473_data *data = i2c_get_clientdata(client);
535 long temp;
536
537 if (strict_strtol(buf, 10, &temp) || !temp)
538 return -EINVAL;
539
540 temp = FAN_RPM_TO_PERIOD(temp);
541 temp = SENSORS_LIMIT(temp, 1, 65534);
542
543 mutex_lock(&data->lock);
544 data->fan_min[attr->index] = temp;
545 adt7473_write_word_data(client, ADT7473_REG_FAN_MIN(attr->index), temp);
546 mutex_unlock(&data->lock);
547
548 return count;
549}
550
551static ssize_t show_fan(struct device *dev, struct device_attribute *devattr,
552 char *buf)
553{
554 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
555 struct adt7473_data *data = adt7473_update_device(dev);
556
557 if (FAN_DATA_VALID(data->fan[attr->index]))
558 return sprintf(buf, "%d\n",
559 FAN_PERIOD_TO_RPM(data->fan[attr->index]));
560 else
561 return sprintf(buf, "0\n");
562}
563
564static ssize_t show_max_duty_at_crit(struct device *dev,
565 struct device_attribute *devattr,
566 char *buf)
567{
568 struct adt7473_data *data = adt7473_update_device(dev);
569 return sprintf(buf, "%d\n", data->max_duty_at_overheat);
570}
571
572static ssize_t set_max_duty_at_crit(struct device *dev,
573 struct device_attribute *devattr,
574 const char *buf,
575 size_t count)
576{
577 u8 reg;
578 struct i2c_client *client = to_i2c_client(dev);
579 struct adt7473_data *data = i2c_get_clientdata(client);
580 long temp;
581
582 if (strict_strtol(buf, 10, &temp))
583 return -EINVAL;
584
585 mutex_lock(&data->lock);
586 data->max_duty_at_overheat = !!temp;
587 reg = i2c_smbus_read_byte_data(client, ADT7473_REG_CFG4);
588 if (temp)
589 reg |= ADT7473_CFG4_MAX_DUTY_AT_OVT;
590 else
591 reg &= ~ADT7473_CFG4_MAX_DUTY_AT_OVT;
592 i2c_smbus_write_byte_data(client, ADT7473_REG_CFG4, reg);
593 mutex_unlock(&data->lock);
594
595 return count;
596}
597
598static ssize_t show_pwm(struct device *dev, struct device_attribute *devattr,
599 char *buf)
600{
601 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
602 struct adt7473_data *data = adt7473_update_device(dev);
603 return sprintf(buf, "%d\n", data->pwm[attr->index]);
604}
605
606static ssize_t set_pwm(struct device *dev, struct device_attribute *devattr,
607 const char *buf, size_t count)
608{
609 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
610 struct i2c_client *client = to_i2c_client(dev);
611 struct adt7473_data *data = i2c_get_clientdata(client);
612 long temp;
613
614 if (strict_strtol(buf, 10, &temp))
615 return -EINVAL;
616
617 temp = SENSORS_LIMIT(temp, 0, 255);
618
619 mutex_lock(&data->lock);
620 data->pwm[attr->index] = temp;
621 i2c_smbus_write_byte_data(client, ADT7473_REG_PWM(attr->index), temp);
622 mutex_unlock(&data->lock);
623
624 return count;
625}
626
627static ssize_t show_pwm_max(struct device *dev,
628 struct device_attribute *devattr,
629 char *buf)
630{
631 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
632 struct adt7473_data *data = adt7473_update_device(dev);
633 return sprintf(buf, "%d\n", data->pwm_max[attr->index]);
634}
635
636static ssize_t set_pwm_max(struct device *dev,
637 struct device_attribute *devattr,
638 const char *buf,
639 size_t count)
640{
641 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
642 struct i2c_client *client = to_i2c_client(dev);
643 struct adt7473_data *data = i2c_get_clientdata(client);
644 long temp;
645
646 if (strict_strtol(buf, 10, &temp))
647 return -EINVAL;
648
649 temp = SENSORS_LIMIT(temp, 0, 255);
650
651 mutex_lock(&data->lock);
652 data->pwm_max[attr->index] = temp;
653 i2c_smbus_write_byte_data(client, ADT7473_REG_PWM_MAX(attr->index),
654 temp);
655 mutex_unlock(&data->lock);
656
657 return count;
658}
659
660static ssize_t show_pwm_min(struct device *dev,
661 struct device_attribute *devattr,
662 char *buf)
663{
664 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
665 struct adt7473_data *data = adt7473_update_device(dev);
666 return sprintf(buf, "%d\n", data->pwm_min[attr->index]);
667}
668
669static ssize_t set_pwm_min(struct device *dev,
670 struct device_attribute *devattr,
671 const char *buf,
672 size_t count)
673{
674 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
675 struct i2c_client *client = to_i2c_client(dev);
676 struct adt7473_data *data = i2c_get_clientdata(client);
677 long temp;
678
679 if (strict_strtol(buf, 10, &temp))
680 return -EINVAL;
681
682 temp = SENSORS_LIMIT(temp, 0, 255);
683
684 mutex_lock(&data->lock);
685 data->pwm_min[attr->index] = temp;
686 i2c_smbus_write_byte_data(client, ADT7473_REG_PWM_MIN(attr->index),
687 temp);
688 mutex_unlock(&data->lock);
689
690 return count;
691}
692
693static ssize_t show_temp_tmax(struct device *dev,
694 struct device_attribute *devattr,
695 char *buf)
696{
697 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
698 struct adt7473_data *data = adt7473_update_device(dev);
699 return sprintf(buf, "%d\n", 1000 * decode_temp(
700 data->temp_twos_complement,
701 data->temp_tmax[attr->index]));
702}
703
704static ssize_t set_temp_tmax(struct device *dev,
705 struct device_attribute *devattr,
706 const char *buf,
707 size_t count)
708{
709 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
710 struct i2c_client *client = to_i2c_client(dev);
711 struct adt7473_data *data = i2c_get_clientdata(client);
712 long temp;
713
714 if (strict_strtol(buf, 10, &temp))
715 return -EINVAL;
716
717 temp = DIV_ROUND_CLOSEST(temp, 1000);
718 temp = encode_temp(data->temp_twos_complement, temp);
719
720 mutex_lock(&data->lock);
721 data->temp_tmax[attr->index] = temp;
722 i2c_smbus_write_byte_data(client, ADT7473_REG_TEMP_TMAX(attr->index),
723 temp);
724 mutex_unlock(&data->lock);
725
726 return count;
727}
728
729static ssize_t show_temp_tmin(struct device *dev,
730 struct device_attribute *devattr,
731 char *buf)
732{
733 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
734 struct adt7473_data *data = adt7473_update_device(dev);
735 return sprintf(buf, "%d\n", 1000 * decode_temp(
736 data->temp_twos_complement,
737 data->temp_tmin[attr->index]));
738}
739
740static ssize_t set_temp_tmin(struct device *dev,
741 struct device_attribute *devattr,
742 const char *buf,
743 size_t count)
744{
745 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
746 struct i2c_client *client = to_i2c_client(dev);
747 struct adt7473_data *data = i2c_get_clientdata(client);
748 long temp;
749
750 if (strict_strtol(buf, 10, &temp))
751 return -EINVAL;
752
753 temp = DIV_ROUND_CLOSEST(temp, 1000);
754 temp = encode_temp(data->temp_twos_complement, temp);
755
756 mutex_lock(&data->lock);
757 data->temp_tmin[attr->index] = temp;
758 i2c_smbus_write_byte_data(client, ADT7473_REG_TEMP_TMIN(attr->index),
759 temp);
760 mutex_unlock(&data->lock);
761
762 return count;
763}
764
765static ssize_t show_pwm_enable(struct device *dev,
766 struct device_attribute *devattr,
767 char *buf)
768{
769 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
770 struct adt7473_data *data = adt7473_update_device(dev);
771
772 switch (data->pwm_behavior[attr->index] >> ADT7473_PWM_BHVR_SHIFT) {
773 case 3:
774 return sprintf(buf, "0\n");
775 case 7:
776 return sprintf(buf, "1\n");
777 default:
778 return sprintf(buf, "2\n");
779 }
780}
781
782static ssize_t set_pwm_enable(struct device *dev,
783 struct device_attribute *devattr,
784 const char *buf,
785 size_t count)
786{
787 u8 reg;
788 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
789 struct i2c_client *client = to_i2c_client(dev);
790 struct adt7473_data *data = i2c_get_clientdata(client);
791 long temp;
792
793 if (strict_strtol(buf, 10, &temp))
794 return -EINVAL;
795
796 switch (temp) {
797 case 0:
798 temp = 3;
799 break;
800 case 1:
801 temp = 7;
802 break;
803 case 2:
804 /* Enter automatic mode with fans off */
805 temp = 4;
806 break;
807 default:
808 return -EINVAL;
809 }
810
811 mutex_lock(&data->lock);
812 reg = i2c_smbus_read_byte_data(client,
813 ADT7473_REG_PWM_BHVR(attr->index));
814 reg = (temp << ADT7473_PWM_BHVR_SHIFT) |
815 (reg & ~ADT7473_PWM_BHVR_MASK);
816 i2c_smbus_write_byte_data(client, ADT7473_REG_PWM_BHVR(attr->index),
817 reg);
818 data->pwm_behavior[attr->index] = reg;
819 mutex_unlock(&data->lock);
820
821 return count;
822}
823
824static ssize_t show_pwm_auto_temp(struct device *dev,
825 struct device_attribute *devattr,
826 char *buf)
827{
828 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
829 struct adt7473_data *data = adt7473_update_device(dev);
830 int bhvr = data->pwm_behavior[attr->index] >> ADT7473_PWM_BHVR_SHIFT;
831
832 switch (bhvr) {
833 case 3:
834 case 4:
835 case 7:
836 return sprintf(buf, "0\n");
837 case 0:
838 case 1:
839 case 5:
840 case 6:
841 return sprintf(buf, "%d\n", bhvr + 1);
842 case 2:
843 return sprintf(buf, "4\n");
844 }
845 /* shouldn't ever get here */
846 BUG();
847}
848
849static ssize_t set_pwm_auto_temp(struct device *dev,
850 struct device_attribute *devattr,
851 const char *buf,
852 size_t count)
853{
854 u8 reg;
855 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
856 struct i2c_client *client = to_i2c_client(dev);
857 struct adt7473_data *data = i2c_get_clientdata(client);
858 long temp;
859
860 if (strict_strtol(buf, 10, &temp))
861 return -EINVAL;
862
863 switch (temp) {
864 case 1:
865 case 2:
866 case 6:
867 case 7:
868 temp--;
869 break;
870 case 0:
871 temp = 4;
872 break;
873 default:
874 return -EINVAL;
875 }
876
877 mutex_lock(&data->lock);
878 reg = i2c_smbus_read_byte_data(client,
879 ADT7473_REG_PWM_BHVR(attr->index));
880 reg = (temp << ADT7473_PWM_BHVR_SHIFT) |
881 (reg & ~ADT7473_PWM_BHVR_MASK);
882 i2c_smbus_write_byte_data(client, ADT7473_REG_PWM_BHVR(attr->index),
883 reg);
884 data->pwm_behavior[attr->index] = reg;
885 mutex_unlock(&data->lock);
886
887 return count;
888}
889
890static ssize_t show_alarm(struct device *dev,
891 struct device_attribute *devattr,
892 char *buf)
893{
894 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
895 struct adt7473_data *data = adt7473_update_device(dev);
896
897 if (data->alarm & attr->index)
898 return sprintf(buf, "1\n");
899 else
900 return sprintf(buf, "0\n");
901}
902
903
904static SENSOR_DEVICE_ATTR(in1_max, S_IWUSR | S_IRUGO, show_volt_max,
905 set_volt_max, 0);
906static SENSOR_DEVICE_ATTR(in2_max, S_IWUSR | S_IRUGO, show_volt_max,
907 set_volt_max, 1);
908
909static SENSOR_DEVICE_ATTR(in1_min, S_IWUSR | S_IRUGO, show_volt_min,
910 set_volt_min, 0);
911static SENSOR_DEVICE_ATTR(in2_min, S_IWUSR | S_IRUGO, show_volt_min,
912 set_volt_min, 1);
913
914static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_volt, NULL, 0);
915static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_volt, NULL, 1);
916
917static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL,
918 ADT7473_VCCP_ALARM);
919static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL,
920 ADT7473_VCC_ALARM);
921
922static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max,
923 set_temp_max, 0);
924static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp_max,
925 set_temp_max, 1);
926static SENSOR_DEVICE_ATTR(temp3_max, S_IWUSR | S_IRUGO, show_temp_max,
927 set_temp_max, 2);
928
929static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp_min,
930 set_temp_min, 0);
931static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp_min,
932 set_temp_min, 1);
933static SENSOR_DEVICE_ATTR(temp3_min, S_IWUSR | S_IRUGO, show_temp_min,
934 set_temp_min, 2);
935
936static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
937static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
938static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
939
940static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL,
941 ADT7473_R1T_ALARM | ALARM2(ADT7473_R1T_SHORT));
942static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL,
943 ADT7473_LT_ALARM);
944static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL,
945 ADT7473_R2T_ALARM | ALARM2(ADT7473_R2T_SHORT));
946
947static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan_min,
948 set_fan_min, 0);
949static SENSOR_DEVICE_ATTR(fan2_min, S_IWUSR | S_IRUGO, show_fan_min,
950 set_fan_min, 1);
951static SENSOR_DEVICE_ATTR(fan3_min, S_IWUSR | S_IRUGO, show_fan_min,
952 set_fan_min, 2);
953static SENSOR_DEVICE_ATTR(fan4_min, S_IWUSR | S_IRUGO, show_fan_min,
954 set_fan_min, 3);
955
956static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
957static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
958static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2);
959static SENSOR_DEVICE_ATTR(fan4_input, S_IRUGO, show_fan, NULL, 3);
960
961static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL,
962 ALARM2(ADT7473_FAN1_ALARM));
963static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL,
964 ALARM2(ADT7473_FAN2_ALARM));
965static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL,
966 ALARM2(ADT7473_FAN3_ALARM));
967static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL,
968 ALARM2(ADT7473_FAN4_ALARM));
969
970static SENSOR_DEVICE_ATTR(pwm_use_point2_pwm_at_crit, S_IWUSR | S_IRUGO,
971 show_max_duty_at_crit, set_max_duty_at_crit, 0);
972
973static SENSOR_DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 0);
974static SENSOR_DEVICE_ATTR(pwm2, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 1);
975static SENSOR_DEVICE_ATTR(pwm3, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 2);
976
977static SENSOR_DEVICE_ATTR(pwm1_auto_point1_pwm, S_IWUSR | S_IRUGO,
978 show_pwm_min, set_pwm_min, 0);
979static SENSOR_DEVICE_ATTR(pwm2_auto_point1_pwm, S_IWUSR | S_IRUGO,
980 show_pwm_min, set_pwm_min, 1);
981static SENSOR_DEVICE_ATTR(pwm3_auto_point1_pwm, S_IWUSR | S_IRUGO,
982 show_pwm_min, set_pwm_min, 2);
983
984static SENSOR_DEVICE_ATTR(pwm1_auto_point2_pwm, S_IWUSR | S_IRUGO,
985 show_pwm_max, set_pwm_max, 0);
986static SENSOR_DEVICE_ATTR(pwm2_auto_point2_pwm, S_IWUSR | S_IRUGO,
987 show_pwm_max, set_pwm_max, 1);
988static SENSOR_DEVICE_ATTR(pwm3_auto_point2_pwm, S_IWUSR | S_IRUGO,
989 show_pwm_max, set_pwm_max, 2);
990
991static SENSOR_DEVICE_ATTR(temp1_auto_point1_temp, S_IWUSR | S_IRUGO,
992 show_temp_tmin, set_temp_tmin, 0);
993static SENSOR_DEVICE_ATTR(temp2_auto_point1_temp, S_IWUSR | S_IRUGO,
994 show_temp_tmin, set_temp_tmin, 1);
995static SENSOR_DEVICE_ATTR(temp3_auto_point1_temp, S_IWUSR | S_IRUGO,
996 show_temp_tmin, set_temp_tmin, 2);
997
998static SENSOR_DEVICE_ATTR(temp1_auto_point2_temp, S_IWUSR | S_IRUGO,
999 show_temp_tmax, set_temp_tmax, 0);
1000static SENSOR_DEVICE_ATTR(temp2_auto_point2_temp, S_IWUSR | S_IRUGO,
1001 show_temp_tmax, set_temp_tmax, 1);
1002static SENSOR_DEVICE_ATTR(temp3_auto_point2_temp, S_IWUSR | S_IRUGO,
1003 show_temp_tmax, set_temp_tmax, 2);
1004
1005static SENSOR_DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
1006 set_pwm_enable, 0);
1007static SENSOR_DEVICE_ATTR(pwm2_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
1008 set_pwm_enable, 1);
1009static SENSOR_DEVICE_ATTR(pwm3_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
1010 set_pwm_enable, 2);
1011
1012static SENSOR_DEVICE_ATTR(pwm1_auto_channels_temp, S_IWUSR | S_IRUGO,
1013 show_pwm_auto_temp, set_pwm_auto_temp, 0);
1014static SENSOR_DEVICE_ATTR(pwm2_auto_channels_temp, S_IWUSR | S_IRUGO,
1015 show_pwm_auto_temp, set_pwm_auto_temp, 1);
1016static SENSOR_DEVICE_ATTR(pwm3_auto_channels_temp, S_IWUSR | S_IRUGO,
1017 show_pwm_auto_temp, set_pwm_auto_temp, 2);
1018
1019static struct attribute *adt7473_attr[] =
1020{
1021 &sensor_dev_attr_in1_max.dev_attr.attr,
1022 &sensor_dev_attr_in2_max.dev_attr.attr,
1023 &sensor_dev_attr_in1_min.dev_attr.attr,
1024 &sensor_dev_attr_in2_min.dev_attr.attr,
1025 &sensor_dev_attr_in1_input.dev_attr.attr,
1026 &sensor_dev_attr_in2_input.dev_attr.attr,
1027 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1028 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1029
1030 &sensor_dev_attr_temp1_max.dev_attr.attr,
1031 &sensor_dev_attr_temp2_max.dev_attr.attr,
1032 &sensor_dev_attr_temp3_max.dev_attr.attr,
1033 &sensor_dev_attr_temp1_min.dev_attr.attr,
1034 &sensor_dev_attr_temp2_min.dev_attr.attr,
1035 &sensor_dev_attr_temp3_min.dev_attr.attr,
1036 &sensor_dev_attr_temp1_input.dev_attr.attr,
1037 &sensor_dev_attr_temp2_input.dev_attr.attr,
1038 &sensor_dev_attr_temp3_input.dev_attr.attr,
1039 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1040 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1041 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1042 &sensor_dev_attr_temp1_auto_point1_temp.dev_attr.attr,
1043 &sensor_dev_attr_temp2_auto_point1_temp.dev_attr.attr,
1044 &sensor_dev_attr_temp3_auto_point1_temp.dev_attr.attr,
1045 &sensor_dev_attr_temp1_auto_point2_temp.dev_attr.attr,
1046 &sensor_dev_attr_temp2_auto_point2_temp.dev_attr.attr,
1047 &sensor_dev_attr_temp3_auto_point2_temp.dev_attr.attr,
1048
1049 &sensor_dev_attr_fan1_min.dev_attr.attr,
1050 &sensor_dev_attr_fan2_min.dev_attr.attr,
1051 &sensor_dev_attr_fan3_min.dev_attr.attr,
1052 &sensor_dev_attr_fan4_min.dev_attr.attr,
1053 &sensor_dev_attr_fan1_input.dev_attr.attr,
1054 &sensor_dev_attr_fan2_input.dev_attr.attr,
1055 &sensor_dev_attr_fan3_input.dev_attr.attr,
1056 &sensor_dev_attr_fan4_input.dev_attr.attr,
1057 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1058 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1059 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1060 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1061
1062 &sensor_dev_attr_pwm_use_point2_pwm_at_crit.dev_attr.attr,
1063
1064 &sensor_dev_attr_pwm1.dev_attr.attr,
1065 &sensor_dev_attr_pwm2.dev_attr.attr,
1066 &sensor_dev_attr_pwm3.dev_attr.attr,
1067 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1068 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1069 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1070 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
1071 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
1072 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
1073
1074 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1075 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1076 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1077 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
1078 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
1079 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
1080
1081 NULL
1082};
1083
1084/* Return 0 if detection is successful, -ENODEV otherwise */
1085static int adt7473_detect(struct i2c_client *client,
1086 struct i2c_board_info *info)
1087{
1088 struct i2c_adapter *adapter = client->adapter;
1089 int vendor, device, revision;
1090
1091 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1092 return -ENODEV;
1093
1094 vendor = i2c_smbus_read_byte_data(client, ADT7473_REG_VENDOR);
1095 if (vendor != ADT7473_VENDOR)
1096 return -ENODEV;
1097
1098 device = i2c_smbus_read_byte_data(client, ADT7473_REG_DEVICE);
1099 if (device != ADT7473_DEVICE)
1100 return -ENODEV;
1101
1102 revision = i2c_smbus_read_byte_data(client, ADT7473_REG_REVISION);
1103 if (revision != ADT7473_REV_68 && revision != ADT7473_REV_69)
1104 return -ENODEV;
1105
1106 strlcpy(info->type, "adt7473", I2C_NAME_SIZE);
1107
1108 return 0;
1109}
1110
1111static int adt7473_probe(struct i2c_client *client,
1112 const struct i2c_device_id *id)
1113{
1114 struct adt7473_data *data;
1115 int err;
1116
1117 data = kzalloc(sizeof(struct adt7473_data), GFP_KERNEL);
1118 if (!data) {
1119 err = -ENOMEM;
1120 goto exit;
1121 }
1122
1123 i2c_set_clientdata(client, data);
1124 mutex_init(&data->lock);
1125
1126 dev_info(&client->dev, "%s chip found\n", client->name);
1127
1128 /* Initialize the ADT7473 chip */
1129 adt7473_init_client(client);
1130
1131 /* Register sysfs hooks */
1132 data->attrs.attrs = adt7473_attr;
1133 err = sysfs_create_group(&client->dev.kobj, &data->attrs);
1134 if (err)
1135 goto exit_free;
1136
1137 data->hwmon_dev = hwmon_device_register(&client->dev);
1138 if (IS_ERR(data->hwmon_dev)) {
1139 err = PTR_ERR(data->hwmon_dev);
1140 goto exit_remove;
1141 }
1142
1143 return 0;
1144
1145exit_remove:
1146 sysfs_remove_group(&client->dev.kobj, &data->attrs);
1147exit_free:
1148 kfree(data);
1149exit:
1150 return err;
1151}
1152
1153static int adt7473_remove(struct i2c_client *client)
1154{
1155 struct adt7473_data *data = i2c_get_clientdata(client);
1156
1157 hwmon_device_unregister(data->hwmon_dev);
1158 sysfs_remove_group(&client->dev.kobj, &data->attrs);
1159 kfree(data);
1160 return 0;
1161}
1162
1163static int __init adt7473_init(void)
1164{
1165 pr_notice("The adt7473 driver is deprecated, please use the adt7475 "
1166 "driver instead\n");
1167 return i2c_add_driver(&adt7473_driver);
1168}
1169
1170static void __exit adt7473_exit(void)
1171{
1172 i2c_del_driver(&adt7473_driver);
1173}
1174
1175MODULE_AUTHOR("Darrick J. Wong <djwong@us.ibm.com>");
1176MODULE_DESCRIPTION("ADT7473 driver");
1177MODULE_LICENSE("GPL");
1178
1179module_init(adt7473_init);
1180module_exit(adt7473_exit);
diff --git a/drivers/hwmon/asc7621.c b/drivers/hwmon/asc7621.c
new file mode 100644
index 000000000000..7f948105d8ad
--- /dev/null
+++ b/drivers/hwmon/asc7621.c
@@ -0,0 +1,1255 @@
1/*
2 * asc7621.c - Part of lm_sensors, Linux kernel modules for hardware monitoring
3 * Copyright (c) 2007, 2010 George Joseph <george.joseph@fairview5.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20#include <linux/module.h>
21#include <linux/init.h>
22#include <linux/slab.h>
23#include <linux/jiffies.h>
24#include <linux/i2c.h>
25#include <linux/hwmon.h>
26#include <linux/hwmon-sysfs.h>
27#include <linux/err.h>
28#include <linux/mutex.h>
29
30/* Addresses to scan */
31static unsigned short normal_i2c[] = {
32 0x2c, 0x2d, 0x2e, I2C_CLIENT_END
33};
34
35enum asc7621_type {
36 asc7621,
37 asc7621a
38};
39
40#define INTERVAL_HIGH (HZ + HZ / 2)
41#define INTERVAL_LOW (1 * 60 * HZ)
42#define PRI_NONE 0
43#define PRI_LOW 1
44#define PRI_HIGH 2
45#define FIRST_CHIP asc7621
46#define LAST_CHIP asc7621a
47
48struct asc7621_chip {
49 char *name;
50 enum asc7621_type chip_type;
51 u8 company_reg;
52 u8 company_id;
53 u8 verstep_reg;
54 u8 verstep_id;
55 unsigned short *addresses;
56};
57
58static struct asc7621_chip asc7621_chips[] = {
59 {
60 .name = "asc7621",
61 .chip_type = asc7621,
62 .company_reg = 0x3e,
63 .company_id = 0x61,
64 .verstep_reg = 0x3f,
65 .verstep_id = 0x6c,
66 .addresses = normal_i2c,
67 },
68 {
69 .name = "asc7621a",
70 .chip_type = asc7621a,
71 .company_reg = 0x3e,
72 .company_id = 0x61,
73 .verstep_reg = 0x3f,
74 .verstep_id = 0x6d,
75 .addresses = normal_i2c,
76 },
77};
78
79/*
80 * Defines the highest register to be used, not the count.
81 * The actual count will probably be smaller because of gaps
82 * in the implementation (unused register locations).
83 * This define will safely set the array size of both the parameter
84 * and data arrays.
85 * This comes from the data sheet register description table.
86 */
87#define LAST_REGISTER 0xff
88
89struct asc7621_data {
90 struct i2c_client client;
91 struct device *class_dev;
92 struct mutex update_lock;
93 int valid; /* !=0 if following fields are valid */
94 unsigned long last_high_reading; /* In jiffies */
95 unsigned long last_low_reading; /* In jiffies */
96 /*
97 * Registers we care about occupy the corresponding index
98 * in the array. Registers we don't care about are left
99 * at 0.
100 */
101 u8 reg[LAST_REGISTER + 1];
102};
103
104/*
105 * Macro to get the parent asc7621_param structure
106 * from a sensor_device_attribute passed into the
107 * show/store functions.
108 */
109#define to_asc7621_param(_sda) \
110 container_of(_sda, struct asc7621_param, sda)
111
112/*
113 * Each parameter to be retrieved needs an asc7621_param structure
114 * allocated. It contains the sensor_device_attribute structure
115 * and the control info needed to retrieve the value from the register map.
116 */
117struct asc7621_param {
118 struct sensor_device_attribute sda;
119 u8 priority;
120 u8 msb[3];
121 u8 lsb[3];
122 u8 mask[3];
123 u8 shift[3];
124};
125
126/*
127 * This is the map that ultimately indicates whether we'll be
128 * retrieving a register value or not, and at what frequency.
129 */
130static u8 asc7621_register_priorities[255];
131
132static struct asc7621_data *asc7621_update_device(struct device *dev);
133
134static inline u8 read_byte(struct i2c_client *client, u8 reg)
135{
136 int res = i2c_smbus_read_byte_data(client, reg);
137 if (res < 0) {
138 dev_err(&client->dev,
139 "Unable to read from register 0x%02x.\n", reg);
140 return 0;
141 };
142 return res & 0xff;
143}
144
145static inline int write_byte(struct i2c_client *client, u8 reg, u8 data)
146{
147 int res = i2c_smbus_write_byte_data(client, reg, data);
148 if (res < 0) {
149 dev_err(&client->dev,
150 "Unable to write value 0x%02x to register 0x%02x.\n",
151 data, reg);
152 };
153 return res;
154}
155
156/*
157 * Data Handlers
158 * Each function handles the formatting, storage
159 * and retrieval of like parameters.
160 */
161
162#define SETUP_SHOW_data_param(d, a) \
163 struct sensor_device_attribute *sda = to_sensor_dev_attr(a); \
164 struct asc7621_data *data = asc7621_update_device(d); \
165 struct asc7621_param *param = to_asc7621_param(sda)
166
167#define SETUP_STORE_data_param(d, a) \
168 struct sensor_device_attribute *sda = to_sensor_dev_attr(a); \
169 struct i2c_client *client = to_i2c_client(d); \
170 struct asc7621_data *data = i2c_get_clientdata(client); \
171 struct asc7621_param *param = to_asc7621_param(sda)
172
173/*
174 * u8 is just what it sounds like...an unsigned byte with no
175 * special formatting.
176 */
177static ssize_t show_u8(struct device *dev, struct device_attribute *attr,
178 char *buf)
179{
180 SETUP_SHOW_data_param(dev, attr);
181
182 return sprintf(buf, "%u\n", data->reg[param->msb[0]]);
183}
184
185static ssize_t store_u8(struct device *dev, struct device_attribute *attr,
186 const char *buf, size_t count)
187{
188 SETUP_STORE_data_param(dev, attr);
189 long reqval;
190
191 if (strict_strtol(buf, 10, &reqval))
192 return -EINVAL;
193
194 reqval = SENSORS_LIMIT(reqval, 0, 255);
195
196 mutex_lock(&data->update_lock);
197 data->reg[param->msb[0]] = reqval;
198 write_byte(client, param->msb[0], reqval);
199 mutex_unlock(&data->update_lock);
200 return count;
201}
202
203/*
204 * Many of the config values occupy only a few bits of a register.
205 */
206static ssize_t show_bitmask(struct device *dev,
207 struct device_attribute *attr, char *buf)
208{
209 SETUP_SHOW_data_param(dev, attr);
210
211 return sprintf(buf, "%u\n",
212 (data->reg[param->msb[0]] >> param->
213 shift[0]) & param->mask[0]);
214}
215
216static ssize_t store_bitmask(struct device *dev,
217 struct device_attribute *attr,
218 const char *buf, size_t count)
219{
220 SETUP_STORE_data_param(dev, attr);
221 long reqval;
222 u8 currval;
223
224 if (strict_strtol(buf, 10, &reqval))
225 return -EINVAL;
226
227 reqval = SENSORS_LIMIT(reqval, 0, param->mask[0]);
228
229 reqval = (reqval & param->mask[0]) << param->shift[0];
230
231 mutex_lock(&data->update_lock);
232 currval = read_byte(client, param->msb[0]);
233 reqval |= (currval & ~(param->mask[0] << param->shift[0]));
234 data->reg[param->msb[0]] = reqval;
235 write_byte(client, param->msb[0], reqval);
236 mutex_unlock(&data->update_lock);
237 return count;
238}
239
240/*
241 * 16 bit fan rpm values
242 * reported by the device as the number of 11.111us periods (90khz)
243 * between full fan rotations. Therefore...
244 * RPM = (90000 * 60) / register value
245 */
246static ssize_t show_fan16(struct device *dev,
247 struct device_attribute *attr, char *buf)
248{
249 SETUP_SHOW_data_param(dev, attr);
250 u16 regval;
251
252 mutex_lock(&data->update_lock);
253 regval = (data->reg[param->msb[0]] << 8) | data->reg[param->lsb[0]];
254 mutex_unlock(&data->update_lock);
255
256 return sprintf(buf, "%u\n",
257 (regval == 0 ? -1 : (regval) ==
258 0xffff ? 0 : 5400000 / regval));
259}
260
261static ssize_t store_fan16(struct device *dev,
262 struct device_attribute *attr, const char *buf,
263 size_t count)
264{
265 SETUP_STORE_data_param(dev, attr);
266 long reqval;
267
268 if (strict_strtol(buf, 10, &reqval))
269 return -EINVAL;
270
271 reqval =
272 (SENSORS_LIMIT((reqval) <= 0 ? 0 : 5400000 / (reqval), 0, 65534));
273
274 mutex_lock(&data->update_lock);
275 data->reg[param->msb[0]] = (reqval >> 8) & 0xff;
276 data->reg[param->lsb[0]] = reqval & 0xff;
277 write_byte(client, param->msb[0], data->reg[param->msb[0]]);
278 write_byte(client, param->lsb[0], data->reg[param->lsb[0]]);
279 mutex_unlock(&data->update_lock);
280
281 return count;
282}
283
284/*
285 * Voltages are scaled in the device so that the nominal voltage
286 * is 3/4ths of the 0-255 range (i.e. 192).
287 * If all voltages are 'normal' then all voltage registers will
288 * read 0xC0. This doesn't help us if we don't have a point of refernce.
289 * The data sheet however provides us with the full scale value for each
290 * which is stored in in_scaling. The sda->index parameter value provides
291 * the index into in_scaling.
292 *
293 * NOTE: The chip expects the first 2 inputs be 2.5 and 2.25 volts
294 * respectively. That doesn't mean that's what the motherboard provides. :)
295 */
296
297static int asc7621_in_scaling[] = {
298 3320, 3000, 4380, 6640, 16000
299};
300
301static ssize_t show_in10(struct device *dev, struct device_attribute *attr,
302 char *buf)
303{
304 SETUP_SHOW_data_param(dev, attr);
305 u16 regval;
306 u8 nr = sda->index;
307
308 mutex_lock(&data->update_lock);
309 regval = (data->reg[param->msb[0]] * asc7621_in_scaling[nr]) / 256;
310
311 /* The LSB value is a 2-bit scaling of the MSB's LSbit value.
312 * I.E. If the maximim voltage for this input is 6640 millivolts then
313 * a MSB register value of 0 = 0mv and 255 = 6640mv.
314 * A 1 step change therefore represents 25.9mv (6640 / 256).
315 * The extra 2-bits therefore represent increments of 6.48mv.
316 */
317 regval += ((asc7621_in_scaling[nr] / 256) / 4) *
318 (data->reg[param->lsb[0]] >> 6);
319
320 mutex_unlock(&data->update_lock);
321
322 return sprintf(buf, "%u\n", regval);
323}
324
325/* 8 bit voltage values (the mins and maxs) */
326static ssize_t show_in8(struct device *dev, struct device_attribute *attr,
327 char *buf)
328{
329 SETUP_SHOW_data_param(dev, attr);
330 u8 nr = sda->index;
331
332 return sprintf(buf, "%u\n",
333 ((data->reg[param->msb[0]] *
334 asc7621_in_scaling[nr]) / 256));
335}
336
337static ssize_t store_in8(struct device *dev, struct device_attribute *attr,
338 const char *buf, size_t count)
339{
340 SETUP_STORE_data_param(dev, attr);
341 long reqval;
342 u8 nr = sda->index;
343
344 if (strict_strtol(buf, 10, &reqval))
345 return -EINVAL;
346
347 reqval = SENSORS_LIMIT(reqval, 0, asc7621_in_scaling[nr]);
348
349 reqval = (reqval * 255 + 128) / asc7621_in_scaling[nr];
350
351 mutex_lock(&data->update_lock);
352 data->reg[param->msb[0]] = reqval;
353 write_byte(client, param->msb[0], reqval);
354 mutex_unlock(&data->update_lock);
355
356 return count;
357}
358
359static ssize_t show_temp8(struct device *dev,
360 struct device_attribute *attr, char *buf)
361{
362 SETUP_SHOW_data_param(dev, attr);
363
364 return sprintf(buf, "%d\n", ((s8) data->reg[param->msb[0]]) * 1000);
365}
366
367static ssize_t store_temp8(struct device *dev,
368 struct device_attribute *attr, const char *buf,
369 size_t count)
370{
371 SETUP_STORE_data_param(dev, attr);
372 long reqval;
373 s8 temp;
374
375 if (strict_strtol(buf, 10, &reqval))
376 return -EINVAL;
377
378 reqval = SENSORS_LIMIT(reqval, -127000, 127000);
379
380 temp = reqval / 1000;
381
382 mutex_lock(&data->update_lock);
383 data->reg[param->msb[0]] = temp;
384 write_byte(client, param->msb[0], temp);
385 mutex_unlock(&data->update_lock);
386 return count;
387}
388
389/*
390 * Temperatures that occupy 2 bytes always have the whole
391 * number of degrees in the MSB with some part of the LSB
392 * indicating fractional degrees.
393 */
394
395/* mmmmmmmm.llxxxxxx */
396static ssize_t show_temp10(struct device *dev,
397 struct device_attribute *attr, char *buf)
398{
399 SETUP_SHOW_data_param(dev, attr);
400 u8 msb, lsb;
401 int temp;
402
403 mutex_lock(&data->update_lock);
404 msb = data->reg[param->msb[0]];
405 lsb = (data->reg[param->lsb[0]] >> 6) & 0x03;
406 temp = (((s8) msb) * 1000) + (lsb * 250);
407 mutex_unlock(&data->update_lock);
408
409 return sprintf(buf, "%d\n", temp);
410}
411
412/* mmmmmm.ll */
413static ssize_t show_temp62(struct device *dev,
414 struct device_attribute *attr, char *buf)
415{
416 SETUP_SHOW_data_param(dev, attr);
417 u8 regval = data->reg[param->msb[0]];
418 int temp = ((s8) (regval & 0xfc) * 1000) + ((regval & 0x03) * 250);
419
420 return sprintf(buf, "%d\n", temp);
421}
422
423static ssize_t store_temp62(struct device *dev,
424 struct device_attribute *attr, const char *buf,
425 size_t count)
426{
427 SETUP_STORE_data_param(dev, attr);
428 long reqval, i, f;
429 s8 temp;
430
431 if (strict_strtol(buf, 10, &reqval))
432 return -EINVAL;
433
434 reqval = SENSORS_LIMIT(reqval, -32000, 31750);
435 i = reqval / 1000;
436 f = reqval - (i * 1000);
437 temp = i << 2;
438 temp |= f / 250;
439
440 mutex_lock(&data->update_lock);
441 data->reg[param->msb[0]] = temp;
442 write_byte(client, param->msb[0], temp);
443 mutex_unlock(&data->update_lock);
444 return count;
445}
446
447/*
448 * The aSC7621 doesn't provide an "auto_point2". Instead, you
449 * specify the auto_point1 and a range. To keep with the sysfs
450 * hwmon specs, we synthesize the auto_point_2 from them.
451 */
452
453static u32 asc7621_range_map[] = {
454 2000, 2500, 3330, 4000, 5000, 6670, 8000, 10000,
455 13330, 16000, 20000, 26670, 32000, 40000, 53330, 80000,
456};
457
458static ssize_t show_ap2_temp(struct device *dev,
459 struct device_attribute *attr, char *buf)
460{
461 SETUP_SHOW_data_param(dev, attr);
462 long auto_point1;
463 u8 regval;
464 int temp;
465
466 mutex_lock(&data->update_lock);
467 auto_point1 = ((s8) data->reg[param->msb[1]]) * 1000;
468 regval =
469 ((data->reg[param->msb[0]] >> param->shift[0]) & param->mask[0]);
470 temp = auto_point1 + asc7621_range_map[SENSORS_LIMIT(regval, 0, 15)];
471 mutex_unlock(&data->update_lock);
472
473 return sprintf(buf, "%d\n", temp);
474
475}
476
477static ssize_t store_ap2_temp(struct device *dev,
478 struct device_attribute *attr,
479 const char *buf, size_t count)
480{
481 SETUP_STORE_data_param(dev, attr);
482 long reqval, auto_point1;
483 int i;
484 u8 currval, newval = 0;
485
486 if (strict_strtol(buf, 10, &reqval))
487 return -EINVAL;
488
489 mutex_lock(&data->update_lock);
490 auto_point1 = data->reg[param->msb[1]] * 1000;
491 reqval = SENSORS_LIMIT(reqval, auto_point1 + 2000, auto_point1 + 80000);
492
493 for (i = ARRAY_SIZE(asc7621_range_map) - 1; i >= 0; i--) {
494 if (reqval >= auto_point1 + asc7621_range_map[i]) {
495 newval = i;
496 break;
497 }
498 }
499
500 newval = (newval & param->mask[0]) << param->shift[0];
501 currval = read_byte(client, param->msb[0]);
502 newval |= (currval & ~(param->mask[0] << param->shift[0]));
503 data->reg[param->msb[0]] = newval;
504 write_byte(client, param->msb[0], newval);
505 mutex_unlock(&data->update_lock);
506 return count;
507}
508
509static ssize_t show_pwm_ac(struct device *dev,
510 struct device_attribute *attr, char *buf)
511{
512 SETUP_SHOW_data_param(dev, attr);
513 u8 config, altbit, regval;
514 u8 map[] = {
515 0x01, 0x02, 0x04, 0x1f, 0x00, 0x06, 0x07, 0x10,
516 0x08, 0x0f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f
517 };
518
519 mutex_lock(&data->update_lock);
520 config = (data->reg[param->msb[0]] >> param->shift[0]) & param->mask[0];
521 altbit = (data->reg[param->msb[1]] >> param->shift[1]) & param->mask[1];
522 regval = config | (altbit << 3);
523 mutex_unlock(&data->update_lock);
524
525 return sprintf(buf, "%u\n", map[SENSORS_LIMIT(regval, 0, 15)]);
526}
527
528static ssize_t store_pwm_ac(struct device *dev,
529 struct device_attribute *attr,
530 const char *buf, size_t count)
531{
532 SETUP_STORE_data_param(dev, attr);
533 unsigned long reqval;
534 u8 currval, config, altbit, newval;
535 u16 map[] = {
536 0x04, 0x00, 0x01, 0xff, 0x02, 0xff, 0x05, 0x06,
537 0x08, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x0f,
538 0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
539 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x03,
540 };
541
542 if (strict_strtoul(buf, 10, &reqval))
543 return -EINVAL;
544
545 if (reqval > 31)
546 return -EINVAL;
547
548 reqval = map[reqval];
549 if (reqval == 0xff)
550 return -EINVAL;
551
552 config = reqval & 0x07;
553 altbit = (reqval >> 3) & 0x01;
554
555 config = (config & param->mask[0]) << param->shift[0];
556 altbit = (altbit & param->mask[1]) << param->shift[1];
557
558 mutex_lock(&data->update_lock);
559 currval = read_byte(client, param->msb[0]);
560 newval = config | (currval & ~(param->mask[0] << param->shift[0]));
561 newval = altbit | (newval & ~(param->mask[1] << param->shift[1]));
562 data->reg[param->msb[0]] = newval;
563 write_byte(client, param->msb[0], newval);
564 mutex_unlock(&data->update_lock);
565 return count;
566}
567
568static ssize_t show_pwm_enable(struct device *dev,
569 struct device_attribute *attr, char *buf)
570{
571 SETUP_SHOW_data_param(dev, attr);
572 u8 config, altbit, minoff, val, newval;
573
574 mutex_lock(&data->update_lock);
575 config = (data->reg[param->msb[0]] >> param->shift[0]) & param->mask[0];
576 altbit = (data->reg[param->msb[1]] >> param->shift[1]) & param->mask[1];
577 minoff = (data->reg[param->msb[2]] >> param->shift[2]) & param->mask[2];
578 mutex_unlock(&data->update_lock);
579
580 val = config | (altbit << 3);
581 newval = 0;
582
583 if (val == 3 || val >= 10)
584 newval = 255;
585 else if (val == 4)
586 newval = 0;
587 else if (val == 7)
588 newval = 1;
589 else if (minoff == 1)
590 newval = 2;
591 else
592 newval = 3;
593
594 return sprintf(buf, "%u\n", newval);
595}
596
597static ssize_t store_pwm_enable(struct device *dev,
598 struct device_attribute *attr,
599 const char *buf, size_t count)
600{
601 SETUP_STORE_data_param(dev, attr);
602 long reqval;
603 u8 currval, config, altbit, newval, minoff = 255;
604
605 if (strict_strtol(buf, 10, &reqval))
606 return -EINVAL;
607
608 switch (reqval) {
609 case 0:
610 newval = 0x04;
611 break;
612 case 1:
613 newval = 0x07;
614 break;
615 case 2:
616 newval = 0x00;
617 minoff = 1;
618 break;
619 case 3:
620 newval = 0x00;
621 minoff = 0;
622 break;
623 case 255:
624 newval = 0x03;
625 break;
626 default:
627 return -EINVAL;
628 }
629
630 config = newval & 0x07;
631 altbit = (newval >> 3) & 0x01;
632
633 mutex_lock(&data->update_lock);
634 config = (config & param->mask[0]) << param->shift[0];
635 altbit = (altbit & param->mask[1]) << param->shift[1];
636 currval = read_byte(client, param->msb[0]);
637 newval = config | (currval & ~(param->mask[0] << param->shift[0]));
638 newval = altbit | (newval & ~(param->mask[1] << param->shift[1]));
639 data->reg[param->msb[0]] = newval;
640 write_byte(client, param->msb[0], newval);
641 if (minoff < 255) {
642 minoff = (minoff & param->mask[2]) << param->shift[2];
643 currval = read_byte(client, param->msb[2]);
644 newval =
645 minoff | (currval & ~(param->mask[2] << param->shift[2]));
646 data->reg[param->msb[2]] = newval;
647 write_byte(client, param->msb[2], newval);
648 }
649 mutex_unlock(&data->update_lock);
650 return count;
651}
652
653static u32 asc7621_pwm_freq_map[] = {
654 10, 15, 23, 30, 38, 47, 62, 94,
655 23000, 24000, 25000, 26000, 27000, 28000, 29000, 30000
656};
657
658static ssize_t show_pwm_freq(struct device *dev,
659 struct device_attribute *attr, char *buf)
660{
661 SETUP_SHOW_data_param(dev, attr);
662 u8 regval =
663 (data->reg[param->msb[0]] >> param->shift[0]) & param->mask[0];
664
665 regval = SENSORS_LIMIT(regval, 0, 15);
666
667 return sprintf(buf, "%u\n", asc7621_pwm_freq_map[regval]);
668}
669
670static ssize_t store_pwm_freq(struct device *dev,
671 struct device_attribute *attr,
672 const char *buf, size_t count)
673{
674 SETUP_STORE_data_param(dev, attr);
675 unsigned long reqval;
676 u8 currval, newval = 255;
677 int i;
678
679 if (strict_strtoul(buf, 10, &reqval))
680 return -EINVAL;
681
682 for (i = 0; i < ARRAY_SIZE(asc7621_pwm_freq_map); i++) {
683 if (reqval == asc7621_pwm_freq_map[i]) {
684 newval = i;
685 break;
686 }
687 }
688 if (newval == 255)
689 return -EINVAL;
690
691 newval = (newval & param->mask[0]) << param->shift[0];
692
693 mutex_lock(&data->update_lock);
694 currval = read_byte(client, param->msb[0]);
695 newval |= (currval & ~(param->mask[0] << param->shift[0]));
696 data->reg[param->msb[0]] = newval;
697 write_byte(client, param->msb[0], newval);
698 mutex_unlock(&data->update_lock);
699 return count;
700}
701
702static u32 asc7621_pwm_auto_spinup_map[] = {
703 0, 100, 250, 400, 700, 1000, 2000, 4000
704};
705
706static ssize_t show_pwm_ast(struct device *dev,
707 struct device_attribute *attr, char *buf)
708{
709 SETUP_SHOW_data_param(dev, attr);
710 u8 regval =
711 (data->reg[param->msb[0]] >> param->shift[0]) & param->mask[0];
712
713 regval = SENSORS_LIMIT(regval, 0, 7);
714
715 return sprintf(buf, "%u\n", asc7621_pwm_auto_spinup_map[regval]);
716
717}
718
719static ssize_t store_pwm_ast(struct device *dev,
720 struct device_attribute *attr,
721 const char *buf, size_t count)
722{
723 SETUP_STORE_data_param(dev, attr);
724 long reqval;
725 u8 currval, newval = 255;
726 u32 i;
727
728 if (strict_strtol(buf, 10, &reqval))
729 return -EINVAL;
730
731 for (i = 0; i < ARRAY_SIZE(asc7621_pwm_auto_spinup_map); i++) {
732 if (reqval == asc7621_pwm_auto_spinup_map[i]) {
733 newval = i;
734 break;
735 }
736 }
737 if (newval == 255)
738 return -EINVAL;
739
740 newval = (newval & param->mask[0]) << param->shift[0];
741
742 mutex_lock(&data->update_lock);
743 currval = read_byte(client, param->msb[0]);
744 newval |= (currval & ~(param->mask[0] << param->shift[0]));
745 data->reg[param->msb[0]] = newval;
746 write_byte(client, param->msb[0], newval);
747 mutex_unlock(&data->update_lock);
748 return count;
749}
750
751static u32 asc7621_temp_smoothing_time_map[] = {
752 35000, 17600, 11800, 7000, 4400, 3000, 1600, 800
753};
754
755static ssize_t show_temp_st(struct device *dev,
756 struct device_attribute *attr, char *buf)
757{
758 SETUP_SHOW_data_param(dev, attr);
759 u8 regval =
760 (data->reg[param->msb[0]] >> param->shift[0]) & param->mask[0];
761 regval = SENSORS_LIMIT(regval, 0, 7);
762
763 return sprintf(buf, "%u\n", asc7621_temp_smoothing_time_map[regval]);
764}
765
766static ssize_t store_temp_st(struct device *dev,
767 struct device_attribute *attr,
768 const char *buf, size_t count)
769{
770 SETUP_STORE_data_param(dev, attr);
771 long reqval;
772 u8 currval, newval = 255;
773 u32 i;
774
775 if (strict_strtol(buf, 10, &reqval))
776 return -EINVAL;
777
778 for (i = 0; i < ARRAY_SIZE(asc7621_temp_smoothing_time_map); i++) {
779 if (reqval == asc7621_temp_smoothing_time_map[i]) {
780 newval = i;
781 break;
782 }
783 }
784
785 if (newval == 255)
786 return -EINVAL;
787
788 newval = (newval & param->mask[0]) << param->shift[0];
789
790 mutex_lock(&data->update_lock);
791 currval = read_byte(client, param->msb[0]);
792 newval |= (currval & ~(param->mask[0] << param->shift[0]));
793 data->reg[param->msb[0]] = newval;
794 write_byte(client, param->msb[0], newval);
795 mutex_unlock(&data->update_lock);
796 return count;
797}
798
799/*
800 * End of data handlers
801 *
802 * These defines do nothing more than make the table easier
803 * to read when wrapped at column 80.
804 */
805
806/*
807 * Creates a variable length array inititalizer.
808 * VAA(1,3,5,7) would produce {1,3,5,7}
809 */
810#define VAA(args...) {args}
811
812#define PREAD(name, n, pri, rm, rl, m, s, r) \
813 {.sda = SENSOR_ATTR(name, S_IRUGO, show_##r, NULL, n), \
814 .priority = pri, .msb[0] = rm, .lsb[0] = rl, .mask[0] = m, \
815 .shift[0] = s,}
816
817#define PWRITE(name, n, pri, rm, rl, m, s, r) \
818 {.sda = SENSOR_ATTR(name, S_IRUGO | S_IWUSR, show_##r, store_##r, n), \
819 .priority = pri, .msb[0] = rm, .lsb[0] = rl, .mask[0] = m, \
820 .shift[0] = s,}
821
822/*
823 * PWRITEM assumes that the initializers for the .msb, .lsb, .mask and .shift
824 * were created using the VAA macro.
825 */
826#define PWRITEM(name, n, pri, rm, rl, m, s, r) \
827 {.sda = SENSOR_ATTR(name, S_IRUGO | S_IWUSR, show_##r, store_##r, n), \
828 .priority = pri, .msb = rm, .lsb = rl, .mask = m, .shift = s,}
829
830static struct asc7621_param asc7621_params[] = {
831 PREAD(in0_input, 0, PRI_HIGH, 0x20, 0x13, 0, 0, in10),
832 PREAD(in1_input, 1, PRI_HIGH, 0x21, 0x18, 0, 0, in10),
833 PREAD(in2_input, 2, PRI_HIGH, 0x22, 0x11, 0, 0, in10),
834 PREAD(in3_input, 3, PRI_HIGH, 0x23, 0x12, 0, 0, in10),
835 PREAD(in4_input, 4, PRI_HIGH, 0x24, 0x14, 0, 0, in10),
836
837 PWRITE(in0_min, 0, PRI_LOW, 0x44, 0, 0, 0, in8),
838 PWRITE(in1_min, 1, PRI_LOW, 0x46, 0, 0, 0, in8),
839 PWRITE(in2_min, 2, PRI_LOW, 0x48, 0, 0, 0, in8),
840 PWRITE(in3_min, 3, PRI_LOW, 0x4a, 0, 0, 0, in8),
841 PWRITE(in4_min, 4, PRI_LOW, 0x4c, 0, 0, 0, in8),
842
843 PWRITE(in0_max, 0, PRI_LOW, 0x45, 0, 0, 0, in8),
844 PWRITE(in1_max, 1, PRI_LOW, 0x47, 0, 0, 0, in8),
845 PWRITE(in2_max, 2, PRI_LOW, 0x49, 0, 0, 0, in8),
846 PWRITE(in3_max, 3, PRI_LOW, 0x4b, 0, 0, 0, in8),
847 PWRITE(in4_max, 4, PRI_LOW, 0x4d, 0, 0, 0, in8),
848
849 PREAD(in0_alarm, 0, PRI_LOW, 0x41, 0, 0x01, 0, bitmask),
850 PREAD(in1_alarm, 1, PRI_LOW, 0x41, 0, 0x01, 1, bitmask),
851 PREAD(in2_alarm, 2, PRI_LOW, 0x41, 0, 0x01, 2, bitmask),
852 PREAD(in3_alarm, 3, PRI_LOW, 0x41, 0, 0x01, 3, bitmask),
853 PREAD(in4_alarm, 4, PRI_LOW, 0x42, 0, 0x01, 0, bitmask),
854
855 PREAD(fan1_input, 0, PRI_HIGH, 0x29, 0x28, 0, 0, fan16),
856 PREAD(fan2_input, 1, PRI_HIGH, 0x2b, 0x2a, 0, 0, fan16),
857 PREAD(fan3_input, 2, PRI_HIGH, 0x2d, 0x2c, 0, 0, fan16),
858 PREAD(fan4_input, 3, PRI_HIGH, 0x2f, 0x2e, 0, 0, fan16),
859
860 PWRITE(fan1_min, 0, PRI_LOW, 0x55, 0x54, 0, 0, fan16),
861 PWRITE(fan2_min, 1, PRI_LOW, 0x57, 0x56, 0, 0, fan16),
862 PWRITE(fan3_min, 2, PRI_LOW, 0x59, 0x58, 0, 0, fan16),
863 PWRITE(fan4_min, 3, PRI_LOW, 0x5b, 0x5a, 0, 0, fan16),
864
865 PREAD(fan1_alarm, 0, PRI_LOW, 0x42, 0, 0x01, 0, bitmask),
866 PREAD(fan2_alarm, 1, PRI_LOW, 0x42, 0, 0x01, 1, bitmask),
867 PREAD(fan3_alarm, 2, PRI_LOW, 0x42, 0, 0x01, 2, bitmask),
868 PREAD(fan4_alarm, 3, PRI_LOW, 0x42, 0, 0x01, 3, bitmask),
869
870 PREAD(temp1_input, 0, PRI_HIGH, 0x25, 0x10, 0, 0, temp10),
871 PREAD(temp2_input, 1, PRI_HIGH, 0x26, 0x15, 0, 0, temp10),
872 PREAD(temp3_input, 2, PRI_HIGH, 0x27, 0x16, 0, 0, temp10),
873 PREAD(temp4_input, 3, PRI_HIGH, 0x33, 0x17, 0, 0, temp10),
874 PREAD(temp5_input, 4, PRI_HIGH, 0xf7, 0xf6, 0, 0, temp10),
875 PREAD(temp6_input, 5, PRI_HIGH, 0xf9, 0xf8, 0, 0, temp10),
876 PREAD(temp7_input, 6, PRI_HIGH, 0xfb, 0xfa, 0, 0, temp10),
877 PREAD(temp8_input, 7, PRI_HIGH, 0xfd, 0xfc, 0, 0, temp10),
878
879 PWRITE(temp1_min, 0, PRI_LOW, 0x4e, 0, 0, 0, temp8),
880 PWRITE(temp2_min, 1, PRI_LOW, 0x50, 0, 0, 0, temp8),
881 PWRITE(temp3_min, 2, PRI_LOW, 0x52, 0, 0, 0, temp8),
882 PWRITE(temp4_min, 3, PRI_LOW, 0x34, 0, 0, 0, temp8),
883
884 PWRITE(temp1_max, 0, PRI_LOW, 0x4f, 0, 0, 0, temp8),
885 PWRITE(temp2_max, 1, PRI_LOW, 0x51, 0, 0, 0, temp8),
886 PWRITE(temp3_max, 2, PRI_LOW, 0x53, 0, 0, 0, temp8),
887 PWRITE(temp4_max, 3, PRI_LOW, 0x35, 0, 0, 0, temp8),
888
889 PREAD(temp1_alarm, 0, PRI_LOW, 0x41, 0, 0x01, 4, bitmask),
890 PREAD(temp2_alarm, 1, PRI_LOW, 0x41, 0, 0x01, 5, bitmask),
891 PREAD(temp3_alarm, 2, PRI_LOW, 0x41, 0, 0x01, 6, bitmask),
892 PREAD(temp4_alarm, 3, PRI_LOW, 0x43, 0, 0x01, 0, bitmask),
893
894 PWRITE(temp1_source, 0, PRI_LOW, 0x02, 0, 0x07, 4, bitmask),
895 PWRITE(temp2_source, 1, PRI_LOW, 0x02, 0, 0x07, 0, bitmask),
896 PWRITE(temp3_source, 2, PRI_LOW, 0x03, 0, 0x07, 4, bitmask),
897 PWRITE(temp4_source, 3, PRI_LOW, 0x03, 0, 0x07, 0, bitmask),
898
899 PWRITE(temp1_smoothing_enable, 0, PRI_LOW, 0x62, 0, 0x01, 3, bitmask),
900 PWRITE(temp2_smoothing_enable, 1, PRI_LOW, 0x63, 0, 0x01, 7, bitmask),
901 PWRITE(temp3_smoothing_enable, 2, PRI_LOW, 0x64, 0, 0x01, 3, bitmask),
902 PWRITE(temp4_smoothing_enable, 3, PRI_LOW, 0x3c, 0, 0x01, 3, bitmask),
903
904 PWRITE(temp1_smoothing_time, 0, PRI_LOW, 0x62, 0, 0x07, 0, temp_st),
905 PWRITE(temp2_smoothing_time, 1, PRI_LOW, 0x63, 0, 0x07, 4, temp_st),
906 PWRITE(temp3_smoothing_time, 2, PRI_LOW, 0x63, 0, 0x07, 0, temp_st),
907 PWRITE(temp4_smoothing_time, 3, PRI_LOW, 0x3c, 0, 0x07, 0, temp_st),
908
909 PWRITE(temp1_auto_point1_temp_hyst, 0, PRI_LOW, 0x6d, 0, 0x0f, 4,
910 bitmask),
911 PWRITE(temp2_auto_point1_temp_hyst, 1, PRI_LOW, 0x6d, 0, 0x0f, 0,
912 bitmask),
913 PWRITE(temp3_auto_point1_temp_hyst, 2, PRI_LOW, 0x6e, 0, 0x0f, 4,
914 bitmask),
915 PWRITE(temp4_auto_point1_temp_hyst, 3, PRI_LOW, 0x6e, 0, 0x0f, 0,
916 bitmask),
917
918 PREAD(temp1_auto_point2_temp_hyst, 0, PRI_LOW, 0x6d, 0, 0x0f, 4,
919 bitmask),
920 PREAD(temp2_auto_point2_temp_hyst, 1, PRI_LOW, 0x6d, 0, 0x0f, 0,
921 bitmask),
922 PREAD(temp3_auto_point2_temp_hyst, 2, PRI_LOW, 0x6e, 0, 0x0f, 4,
923 bitmask),
924 PREAD(temp4_auto_point2_temp_hyst, 3, PRI_LOW, 0x6e, 0, 0x0f, 0,
925 bitmask),
926
927 PWRITE(temp1_auto_point1_temp, 0, PRI_LOW, 0x67, 0, 0, 0, temp8),
928 PWRITE(temp2_auto_point1_temp, 1, PRI_LOW, 0x68, 0, 0, 0, temp8),
929 PWRITE(temp3_auto_point1_temp, 2, PRI_LOW, 0x69, 0, 0, 0, temp8),
930 PWRITE(temp4_auto_point1_temp, 3, PRI_LOW, 0x3b, 0, 0, 0, temp8),
931
932 PWRITEM(temp1_auto_point2_temp, 0, PRI_LOW, VAA(0x5f, 0x67), VAA(0),
933 VAA(0x0f), VAA(4), ap2_temp),
934 PWRITEM(temp2_auto_point2_temp, 1, PRI_LOW, VAA(0x60, 0x68), VAA(0),
935 VAA(0x0f), VAA(4), ap2_temp),
936 PWRITEM(temp3_auto_point2_temp, 2, PRI_LOW, VAA(0x61, 0x69), VAA(0),
937 VAA(0x0f), VAA(4), ap2_temp),
938 PWRITEM(temp4_auto_point2_temp, 3, PRI_LOW, VAA(0x3c, 0x3b), VAA(0),
939 VAA(0x0f), VAA(4), ap2_temp),
940
941 PWRITE(temp1_crit, 0, PRI_LOW, 0x6a, 0, 0, 0, temp8),
942 PWRITE(temp2_crit, 1, PRI_LOW, 0x6b, 0, 0, 0, temp8),
943 PWRITE(temp3_crit, 2, PRI_LOW, 0x6c, 0, 0, 0, temp8),
944 PWRITE(temp4_crit, 3, PRI_LOW, 0x3d, 0, 0, 0, temp8),
945
946 PWRITE(temp5_enable, 4, PRI_LOW, 0x0e, 0, 0x01, 0, bitmask),
947 PWRITE(temp6_enable, 5, PRI_LOW, 0x0e, 0, 0x01, 1, bitmask),
948 PWRITE(temp7_enable, 6, PRI_LOW, 0x0e, 0, 0x01, 2, bitmask),
949 PWRITE(temp8_enable, 7, PRI_LOW, 0x0e, 0, 0x01, 3, bitmask),
950
951 PWRITE(remote1_offset, 0, PRI_LOW, 0x1c, 0, 0, 0, temp62),
952 PWRITE(remote2_offset, 1, PRI_LOW, 0x1d, 0, 0, 0, temp62),
953
954 PWRITE(pwm1, 0, PRI_HIGH, 0x30, 0, 0, 0, u8),
955 PWRITE(pwm2, 1, PRI_HIGH, 0x31, 0, 0, 0, u8),
956 PWRITE(pwm3, 2, PRI_HIGH, 0x32, 0, 0, 0, u8),
957
958 PWRITE(pwm1_invert, 0, PRI_LOW, 0x5c, 0, 0x01, 4, bitmask),
959 PWRITE(pwm2_invert, 1, PRI_LOW, 0x5d, 0, 0x01, 4, bitmask),
960 PWRITE(pwm3_invert, 2, PRI_LOW, 0x5e, 0, 0x01, 4, bitmask),
961
962 PWRITEM(pwm1_enable, 0, PRI_LOW, VAA(0x5c, 0x5c, 0x62), VAA(0, 0, 0),
963 VAA(0x07, 0x01, 0x01), VAA(5, 3, 5), pwm_enable),
964 PWRITEM(pwm2_enable, 1, PRI_LOW, VAA(0x5d, 0x5d, 0x62), VAA(0, 0, 0),
965 VAA(0x07, 0x01, 0x01), VAA(5, 3, 6), pwm_enable),
966 PWRITEM(pwm3_enable, 2, PRI_LOW, VAA(0x5e, 0x5e, 0x62), VAA(0, 0, 0),
967 VAA(0x07, 0x01, 0x01), VAA(5, 3, 7), pwm_enable),
968
969 PWRITEM(pwm1_auto_channels, 0, PRI_LOW, VAA(0x5c, 0x5c), VAA(0, 0),
970 VAA(0x07, 0x01), VAA(5, 3), pwm_ac),
971 PWRITEM(pwm2_auto_channels, 1, PRI_LOW, VAA(0x5d, 0x5d), VAA(0, 0),
972 VAA(0x07, 0x01), VAA(5, 3), pwm_ac),
973 PWRITEM(pwm3_auto_channels, 2, PRI_LOW, VAA(0x5e, 0x5e), VAA(0, 0),
974 VAA(0x07, 0x01), VAA(5, 3), pwm_ac),
975
976 PWRITE(pwm1_auto_point1_pwm, 0, PRI_LOW, 0x64, 0, 0, 0, u8),
977 PWRITE(pwm2_auto_point1_pwm, 1, PRI_LOW, 0x65, 0, 0, 0, u8),
978 PWRITE(pwm3_auto_point1_pwm, 2, PRI_LOW, 0x66, 0, 0, 0, u8),
979
980 PWRITE(pwm1_auto_point2_pwm, 0, PRI_LOW, 0x38, 0, 0, 0, u8),
981 PWRITE(pwm2_auto_point2_pwm, 1, PRI_LOW, 0x39, 0, 0, 0, u8),
982 PWRITE(pwm3_auto_point2_pwm, 2, PRI_LOW, 0x3a, 0, 0, 0, u8),
983
984 PWRITE(pwm1_freq, 0, PRI_LOW, 0x5f, 0, 0x0f, 0, pwm_freq),
985 PWRITE(pwm2_freq, 1, PRI_LOW, 0x60, 0, 0x0f, 0, pwm_freq),
986 PWRITE(pwm3_freq, 2, PRI_LOW, 0x61, 0, 0x0f, 0, pwm_freq),
987
988 PREAD(pwm1_auto_zone_assigned, 0, PRI_LOW, 0, 0, 0x03, 2, bitmask),
989 PREAD(pwm2_auto_zone_assigned, 1, PRI_LOW, 0, 0, 0x03, 4, bitmask),
990 PREAD(pwm3_auto_zone_assigned, 2, PRI_LOW, 0, 0, 0x03, 6, bitmask),
991
992 PWRITE(pwm1_auto_spinup_time, 0, PRI_LOW, 0x5c, 0, 0x07, 0, pwm_ast),
993 PWRITE(pwm2_auto_spinup_time, 1, PRI_LOW, 0x5d, 0, 0x07, 0, pwm_ast),
994 PWRITE(pwm3_auto_spinup_time, 2, PRI_LOW, 0x5e, 0, 0x07, 0, pwm_ast),
995
996 PWRITE(peci_enable, 0, PRI_LOW, 0x40, 0, 0x01, 4, bitmask),
997 PWRITE(peci_avg, 0, PRI_LOW, 0x36, 0, 0x07, 0, bitmask),
998 PWRITE(peci_domain, 0, PRI_LOW, 0x36, 0, 0x01, 3, bitmask),
999 PWRITE(peci_legacy, 0, PRI_LOW, 0x36, 0, 0x01, 4, bitmask),
1000 PWRITE(peci_diode, 0, PRI_LOW, 0x0e, 0, 0x07, 4, bitmask),
1001 PWRITE(peci_4domain, 0, PRI_LOW, 0x0e, 0, 0x01, 4, bitmask),
1002
1003};
1004
1005static struct asc7621_data *asc7621_update_device(struct device *dev)
1006{
1007 struct i2c_client *client = to_i2c_client(dev);
1008 struct asc7621_data *data = i2c_get_clientdata(client);
1009 int i;
1010
1011/*
1012 * The asc7621 chips guarantee consistent reads of multi-byte values
1013 * regardless of the order of the reads. No special logic is needed
1014 * so we can just read the registers in whatever order they appear
1015 * in the asc7621_params array.
1016 */
1017
1018 mutex_lock(&data->update_lock);
1019
1020 /* Read all the high priority registers */
1021
1022 if (!data->valid ||
1023 time_after(jiffies, data->last_high_reading + INTERVAL_HIGH)) {
1024
1025 for (i = 0; i < ARRAY_SIZE(asc7621_register_priorities); i++) {
1026 if (asc7621_register_priorities[i] == PRI_HIGH) {
1027 data->reg[i] =
1028 i2c_smbus_read_byte_data(client, i) & 0xff;
1029 }
1030 }
1031 data->last_high_reading = jiffies;
1032 }; /* last_reading */
1033
1034 /* Read all the low priority registers. */
1035
1036 if (!data->valid ||
1037 time_after(jiffies, data->last_low_reading + INTERVAL_LOW)) {
1038
1039 for (i = 0; i < ARRAY_SIZE(asc7621_params); i++) {
1040 if (asc7621_register_priorities[i] == PRI_LOW) {
1041 data->reg[i] =
1042 i2c_smbus_read_byte_data(client, i) & 0xff;
1043 }
1044 }
1045 data->last_low_reading = jiffies;
1046 }; /* last_reading */
1047
1048 data->valid = 1;
1049
1050 mutex_unlock(&data->update_lock);
1051
1052 return data;
1053}
1054
1055/*
1056 * Standard detection and initialization below
1057 *
1058 * Helper function that checks if an address is valid
1059 * for a particular chip.
1060 */
1061
1062static inline int valid_address_for_chip(int chip_type, int address)
1063{
1064 int i;
1065
1066 for (i = 0; asc7621_chips[chip_type].addresses[i] != I2C_CLIENT_END;
1067 i++) {
1068 if (asc7621_chips[chip_type].addresses[i] == address)
1069 return 1;
1070 }
1071 return 0;
1072}
1073
1074static void asc7621_init_client(struct i2c_client *client)
1075{
1076 int value;
1077
1078 /* Warn if part was not "READY" */
1079
1080 value = read_byte(client, 0x40);
1081
1082 if (value & 0x02) {
1083 dev_err(&client->dev,
1084 "Client (%d,0x%02x) config is locked.\n",
1085 i2c_adapter_id(client->adapter), client->addr);
1086 };
1087 if (!(value & 0x04)) {
1088 dev_err(&client->dev, "Client (%d,0x%02x) is not ready.\n",
1089 i2c_adapter_id(client->adapter), client->addr);
1090 };
1091
1092/*
1093 * Start monitoring
1094 *
1095 * Try to clear LOCK, Set START, save everything else
1096 */
1097 value = (value & ~0x02) | 0x01;
1098 write_byte(client, 0x40, value & 0xff);
1099
1100}
1101
1102static int
1103asc7621_probe(struct i2c_client *client, const struct i2c_device_id *id)
1104{
1105 struct asc7621_data *data;
1106 int i, err;
1107
1108 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1109 return -EIO;
1110
1111 data = kzalloc(sizeof(struct asc7621_data), GFP_KERNEL);
1112 if (data == NULL)
1113 return -ENOMEM;
1114
1115 i2c_set_clientdata(client, data);
1116 data->valid = 0;
1117 mutex_init(&data->update_lock);
1118
1119 /* Initialize the asc7621 chip */
1120 asc7621_init_client(client);
1121
1122 /* Create the sysfs entries */
1123 for (i = 0; i < ARRAY_SIZE(asc7621_params); i++) {
1124 err =
1125 device_create_file(&client->dev,
1126 &(asc7621_params[i].sda.dev_attr));
1127 if (err)
1128 goto exit_remove;
1129 }
1130
1131 data->class_dev = hwmon_device_register(&client->dev);
1132 if (IS_ERR(data->class_dev)) {
1133 err = PTR_ERR(data->class_dev);
1134 goto exit_remove;
1135 }
1136
1137 return 0;
1138
1139exit_remove:
1140 for (i = 0; i < ARRAY_SIZE(asc7621_params); i++) {
1141 device_remove_file(&client->dev,
1142 &(asc7621_params[i].sda.dev_attr));
1143 }
1144
1145 i2c_set_clientdata(client, NULL);
1146 kfree(data);
1147 return err;
1148}
1149
1150static int asc7621_detect(struct i2c_client *client,
1151 struct i2c_board_info *info)
1152{
1153 struct i2c_adapter *adapter = client->adapter;
1154 int company, verstep, chip_index;
1155 struct device *dev;
1156
1157 dev = &client->dev;
1158
1159 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1160 return -ENODEV;
1161
1162 for (chip_index = FIRST_CHIP; chip_index <= LAST_CHIP; chip_index++) {
1163
1164 if (!valid_address_for_chip(chip_index, client->addr))
1165 continue;
1166
1167 company = read_byte(client,
1168 asc7621_chips[chip_index].company_reg);
1169 verstep = read_byte(client,
1170 asc7621_chips[chip_index].verstep_reg);
1171
1172 if (company == asc7621_chips[chip_index].company_id &&
1173 verstep == asc7621_chips[chip_index].verstep_id) {
1174 strlcpy(client->name, asc7621_chips[chip_index].name,
1175 I2C_NAME_SIZE);
1176 strlcpy(info->type, asc7621_chips[chip_index].name,
1177 I2C_NAME_SIZE);
1178
1179 dev_info(&adapter->dev, "Matched %s\n",
1180 asc7621_chips[chip_index].name);
1181 return 0;
1182 }
1183 }
1184
1185 return -ENODEV;
1186}
1187
1188static int asc7621_remove(struct i2c_client *client)
1189{
1190 struct asc7621_data *data = i2c_get_clientdata(client);
1191 int i;
1192
1193 hwmon_device_unregister(data->class_dev);
1194
1195 for (i = 0; i < ARRAY_SIZE(asc7621_params); i++) {
1196 device_remove_file(&client->dev,
1197 &(asc7621_params[i].sda.dev_attr));
1198 }
1199
1200 i2c_set_clientdata(client, NULL);
1201 kfree(data);
1202 return 0;
1203}
1204
1205static const struct i2c_device_id asc7621_id[] = {
1206 {"asc7621", asc7621},
1207 {"asc7621a", asc7621a},
1208 {},
1209};
1210
1211MODULE_DEVICE_TABLE(i2c, asc7621_id);
1212
1213static struct i2c_driver asc7621_driver = {
1214 .class = I2C_CLASS_HWMON,
1215 .driver = {
1216 .name = "asc7621",
1217 },
1218 .probe = asc7621_probe,
1219 .remove = asc7621_remove,
1220 .id_table = asc7621_id,
1221 .detect = asc7621_detect,
1222 .address_list = normal_i2c,
1223};
1224
1225static int __init sm_asc7621_init(void)
1226{
1227 int i, j;
1228/*
1229 * Collect all the registers needed into a single array.
1230 * This way, if a register isn't actually used for anything,
1231 * we don't retrieve it.
1232 */
1233
1234 for (i = 0; i < ARRAY_SIZE(asc7621_params); i++) {
1235 for (j = 0; j < ARRAY_SIZE(asc7621_params[i].msb); j++)
1236 asc7621_register_priorities[asc7621_params[i].msb[j]] =
1237 asc7621_params[i].priority;
1238 for (j = 0; j < ARRAY_SIZE(asc7621_params[i].lsb); j++)
1239 asc7621_register_priorities[asc7621_params[i].lsb[j]] =
1240 asc7621_params[i].priority;
1241 }
1242 return i2c_add_driver(&asc7621_driver);
1243}
1244
1245static void __exit sm_asc7621_exit(void)
1246{
1247 i2c_del_driver(&asc7621_driver);
1248}
1249
1250MODULE_LICENSE("GPL");
1251MODULE_AUTHOR("George Joseph");
1252MODULE_DESCRIPTION("Andigilog aSC7621 and aSC7621a driver");
1253
1254module_init(sm_asc7621_init);
1255module_exit(sm_asc7621_exit);
diff --git a/drivers/hwmon/fschmd.c b/drivers/hwmon/fschmd.c
index fa0728232e71..0627f7a5b9b8 100644
--- a/drivers/hwmon/fschmd.c
+++ b/drivers/hwmon/fschmd.c
@@ -267,7 +267,7 @@ struct fschmd_data {
267 struct list_head list; /* member of the watchdog_data_list */ 267 struct list_head list; /* member of the watchdog_data_list */
268 struct kref kref; 268 struct kref kref;
269 struct miscdevice watchdog_miscdev; 269 struct miscdevice watchdog_miscdev;
270 int kind; 270 enum chips kind;
271 unsigned long watchdog_is_open; 271 unsigned long watchdog_is_open;
272 char watchdog_expect_close; 272 char watchdog_expect_close;
273 char watchdog_name[10]; /* must be unique to avoid sysfs conflict */ 273 char watchdog_name[10]; /* must be unique to avoid sysfs conflict */
@@ -325,8 +325,7 @@ static ssize_t show_in_value(struct device *dev,
325 int index = to_sensor_dev_attr(devattr)->index; 325 int index = to_sensor_dev_attr(devattr)->index;
326 struct fschmd_data *data = fschmd_update_device(dev); 326 struct fschmd_data *data = fschmd_update_device(dev);
327 327
328 /* fscher / fschrc - 1 as data->kind is an array index, not a chips */ 328 if (data->kind == fscher || data->kind >= fschrc)
329 if (data->kind == (fscher - 1) || data->kind >= (fschrc - 1))
330 return sprintf(buf, "%d\n", (data->volt[index] * dmi_vref * 329 return sprintf(buf, "%d\n", (data->volt[index] * dmi_vref *
331 dmi_mult[index]) / 255 + dmi_offset[index]); 330 dmi_mult[index]) / 255 + dmi_offset[index]);
332 else 331 else
@@ -492,7 +491,7 @@ static ssize_t show_pwm_auto_point1_pwm(struct device *dev,
492 int val = data->fan_min[index]; 491 int val = data->fan_min[index];
493 492
494 /* 0 = allow turning off (except on the syl), 1-255 = 50-100% */ 493 /* 0 = allow turning off (except on the syl), 1-255 = 50-100% */
495 if (val || data->kind == fscsyl - 1) 494 if (val || data->kind == fscsyl)
496 val = val / 2 + 128; 495 val = val / 2 + 128;
497 496
498 return sprintf(buf, "%d\n", val); 497 return sprintf(buf, "%d\n", val);
@@ -506,7 +505,7 @@ static ssize_t store_pwm_auto_point1_pwm(struct device *dev,
506 unsigned long v = simple_strtoul(buf, NULL, 10); 505 unsigned long v = simple_strtoul(buf, NULL, 10);
507 506
508 /* reg: 0 = allow turning off (except on the syl), 1-255 = 50-100% */ 507 /* reg: 0 = allow turning off (except on the syl), 1-255 = 50-100% */
509 if (v || data->kind == fscsyl - 1) { 508 if (v || data->kind == fscsyl) {
510 v = SENSORS_LIMIT(v, 128, 255); 509 v = SENSORS_LIMIT(v, 128, 255);
511 v = (v - 128) * 2 + 1; 510 v = (v - 128) * 2 + 1;
512 } 511 }
@@ -1037,7 +1036,7 @@ static int fschmd_detect(struct i2c_client *client,
1037 else 1036 else
1038 return -ENODEV; 1037 return -ENODEV;
1039 1038
1040 strlcpy(info->type, fschmd_id[kind - 1].name, I2C_NAME_SIZE); 1039 strlcpy(info->type, fschmd_id[kind].name, I2C_NAME_SIZE);
1041 1040
1042 return 0; 1041 return 0;
1043} 1042}
@@ -1065,6 +1064,7 @@ static int fschmd_probe(struct i2c_client *client,
1065 (where the client is found through a data ptr instead of the 1064 (where the client is found through a data ptr instead of the
1066 otherway around) */ 1065 otherway around) */
1067 data->client = client; 1066 data->client = client;
1067 data->kind = kind;
1068 1068
1069 if (kind == fscpos) { 1069 if (kind == fscpos) {
1070 /* The Poseidon has hardwired temp limits, fill these 1070 /* The Poseidon has hardwired temp limits, fill these
@@ -1085,9 +1085,6 @@ static int fschmd_probe(struct i2c_client *client,
1085 } 1085 }
1086 } 1086 }
1087 1087
1088 /* i2c kind goes from 1-6, we want from 0-5 to address arrays */
1089 data->kind = kind - 1;
1090
1091 /* Read in some never changing registers */ 1088 /* Read in some never changing registers */
1092 data->revision = i2c_smbus_read_byte_data(client, FSCHMD_REG_REVISION); 1089 data->revision = i2c_smbus_read_byte_data(client, FSCHMD_REG_REVISION);
1093 data->global_control = i2c_smbus_read_byte_data(client, 1090 data->global_control = i2c_smbus_read_byte_data(client,
diff --git a/drivers/hwmon/g760a.c b/drivers/hwmon/g760a.c
index 19c01a49f6be..09ea12e0a551 100644
--- a/drivers/hwmon/g760a.c
+++ b/drivers/hwmon/g760a.c
@@ -68,7 +68,7 @@ struct g760a_data {
68#define PWM_FROM_CNT(cnt) (0xff-(cnt)) 68#define PWM_FROM_CNT(cnt) (0xff-(cnt))
69#define PWM_TO_CNT(pwm) (0xff-(pwm)) 69#define PWM_TO_CNT(pwm) (0xff-(pwm))
70 70
71unsigned int rpm_from_cnt(u8 val, u32 clk, u16 div) 71static inline unsigned int rpm_from_cnt(u8 val, u32 clk, u16 div)
72{ 72{
73 return ((val == 0x00) ? 0 : ((clk*30)/(val*div))); 73 return ((val == 0x00) ? 0 : ((clk*30)/(val*div)));
74} 74}
diff --git a/drivers/hwmon/it87.c b/drivers/hwmon/it87.c
index 0ffe84d190bb..1002befd87d5 100644
--- a/drivers/hwmon/it87.c
+++ b/drivers/hwmon/it87.c
@@ -1,40 +1,40 @@
1/* 1/*
2 it87.c - Part of lm_sensors, Linux kernel modules for hardware 2 * it87.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring. 3 * monitoring.
4 4 *
5 The IT8705F is an LPC-based Super I/O part that contains UARTs, a 5 * The IT8705F is an LPC-based Super I/O part that contains UARTs, a
6 parallel port, an IR port, a MIDI port, a floppy controller, etc., in 6 * parallel port, an IR port, a MIDI port, a floppy controller, etc., in
7 addition to an Environment Controller (Enhanced Hardware Monitor and 7 * addition to an Environment Controller (Enhanced Hardware Monitor and
8 Fan Controller) 8 * Fan Controller)
9 9 *
10 This driver supports only the Environment Controller in the IT8705F and 10 * This driver supports only the Environment Controller in the IT8705F and
11 similar parts. The other devices are supported by different drivers. 11 * similar parts. The other devices are supported by different drivers.
12 12 *
13 Supports: IT8705F Super I/O chip w/LPC interface 13 * Supports: IT8705F Super I/O chip w/LPC interface
14 IT8712F Super I/O chip w/LPC interface 14 * IT8712F Super I/O chip w/LPC interface
15 IT8716F Super I/O chip w/LPC interface 15 * IT8716F Super I/O chip w/LPC interface
16 IT8718F Super I/O chip w/LPC interface 16 * IT8718F Super I/O chip w/LPC interface
17 IT8720F Super I/O chip w/LPC interface 17 * IT8720F Super I/O chip w/LPC interface
18 IT8726F Super I/O chip w/LPC interface 18 * IT8726F Super I/O chip w/LPC interface
19 Sis950 A clone of the IT8705F 19 * Sis950 A clone of the IT8705F
20 20 *
21 Copyright (C) 2001 Chris Gauthron 21 * Copyright (C) 2001 Chris Gauthron
22 Copyright (C) 2005-2007 Jean Delvare <khali@linux-fr.org> 22 * Copyright (C) 2005-2010 Jean Delvare <khali@linux-fr.org>
23 23 *
24 This program is free software; you can redistribute it and/or modify 24 * This program is free software; you can redistribute it and/or modify
25 it under the terms of the GNU General Public License as published by 25 * it under the terms of the GNU General Public License as published by
26 the Free Software Foundation; either version 2 of the License, or 26 * the Free Software Foundation; either version 2 of the License, or
27 (at your option) any later version. 27 * (at your option) any later version.
28 28 *
29 This program is distributed in the hope that it will be useful, 29 * This program is distributed in the hope that it will be useful,
30 but WITHOUT ANY WARRANTY; without even the implied warranty of 30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 GNU General Public License for more details. 32 * GNU General Public License for more details.
33 33 *
34 You should have received a copy of the GNU General Public License 34 * You should have received a copy of the GNU General Public License
35 along with this program; if not, write to the Free Software 35 * along with this program; if not, write to the Free Software
36 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 36 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
37*/ 37 */
38 38
39#include <linux/module.h> 39#include <linux/module.h>
40#include <linux/init.h> 40#include <linux/init.h>
@@ -128,6 +128,7 @@ superio_exit(void)
128#define IT87_SIO_GPIO5_REG 0x29 128#define IT87_SIO_GPIO5_REG 0x29
129#define IT87_SIO_PINX2_REG 0x2c /* Pin selection */ 129#define IT87_SIO_PINX2_REG 0x2c /* Pin selection */
130#define IT87_SIO_VID_REG 0xfc /* VID value */ 130#define IT87_SIO_VID_REG 0xfc /* VID value */
131#define IT87_SIO_BEEP_PIN_REG 0xf6 /* Beep pin mapping */
131 132
132/* Update battery voltage after every reading if true */ 133/* Update battery voltage after every reading if true */
133static int update_vbat; 134static int update_vbat;
@@ -187,9 +188,13 @@ static const u8 IT87_REG_FANX_MIN[] = { 0x1b, 0x1c, 0x1d, 0x85, 0x87 };
187 188
188#define IT87_REG_VIN_ENABLE 0x50 189#define IT87_REG_VIN_ENABLE 0x50
189#define IT87_REG_TEMP_ENABLE 0x51 190#define IT87_REG_TEMP_ENABLE 0x51
191#define IT87_REG_BEEP_ENABLE 0x5c
190 192
191#define IT87_REG_CHIPID 0x58 193#define IT87_REG_CHIPID 0x58
192 194
195#define IT87_REG_AUTO_TEMP(nr, i) (0x60 + (nr) * 8 + (i))
196#define IT87_REG_AUTO_PWM(nr, i) (0x65 + (nr) * 8 + (i))
197
193#define IN_TO_REG(val) (SENSORS_LIMIT((((val) + 8)/16),0,255)) 198#define IN_TO_REG(val) (SENSORS_LIMIT((((val) + 8)/16),0,255))
194#define IN_FROM_REG(val) ((val) * 16) 199#define IN_FROM_REG(val) ((val) * 16)
195 200
@@ -246,6 +251,7 @@ struct it87_sio_data {
246 /* Values read from Super-I/O config space */ 251 /* Values read from Super-I/O config space */
247 u8 revision; 252 u8 revision;
248 u8 vid_value; 253 u8 vid_value;
254 u8 beep_pin;
249 /* Features skipped based on config or DMI */ 255 /* Features skipped based on config or DMI */
250 u8 skip_vid; 256 u8 skip_vid;
251 u8 skip_fan; 257 u8 skip_fan;
@@ -279,9 +285,21 @@ struct it87_data {
279 u8 vid; /* Register encoding, combined */ 285 u8 vid; /* Register encoding, combined */
280 u8 vrm; 286 u8 vrm;
281 u32 alarms; /* Register encoding, combined */ 287 u32 alarms; /* Register encoding, combined */
288 u8 beeps; /* Register encoding */
282 u8 fan_main_ctrl; /* Register value */ 289 u8 fan_main_ctrl; /* Register value */
283 u8 fan_ctl; /* Register value */ 290 u8 fan_ctl; /* Register value */
284 u8 manual_pwm_ctl[3]; /* manual PWM value set by user */ 291
292 /* The following 3 arrays correspond to the same registers. The
293 * meaning of bits 6-0 depends on the value of bit 7, and we want
294 * to preserve settings on mode changes, so we have to track all
295 * values separately. */
296 u8 pwm_ctrl[3]; /* Register value */
297 u8 pwm_duty[3]; /* Manual PWM value set by user (bit 6-0) */
298 u8 pwm_temp_map[3]; /* PWM to temp. chan. mapping (bits 1-0) */
299
300 /* Automatic fan speed control registers */
301 u8 auto_pwm[3][4]; /* [nr][3] is hard-coded */
302 s8 auto_temp[3][5]; /* [nr][0] is point1_temp_hyst */
285}; 303};
286 304
287static inline int has_16bit_fans(const struct it87_data *data) 305static inline int has_16bit_fans(const struct it87_data *data)
@@ -296,6 +314,15 @@ static inline int has_16bit_fans(const struct it87_data *data)
296 || data->type == it8720; 314 || data->type == it8720;
297} 315}
298 316
317static inline int has_old_autopwm(const struct it87_data *data)
318{
319 /* The old automatic fan speed control interface is implemented
320 by IT8705F chips up to revision F and IT8712F chips up to
321 revision G. */
322 return (data->type == it87 && data->revision < 0x03)
323 || (data->type == it8712 && data->revision < 0x08);
324}
325
299static int it87_probe(struct platform_device *pdev); 326static int it87_probe(struct platform_device *pdev);
300static int __devexit it87_remove(struct platform_device *pdev); 327static int __devexit it87_remove(struct platform_device *pdev);
301 328
@@ -352,7 +379,10 @@ static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
352 int nr = sensor_attr->index; 379 int nr = sensor_attr->index;
353 380
354 struct it87_data *data = dev_get_drvdata(dev); 381 struct it87_data *data = dev_get_drvdata(dev);
355 unsigned long val = simple_strtoul(buf, NULL, 10); 382 unsigned long val;
383
384 if (strict_strtoul(buf, 10, &val) < 0)
385 return -EINVAL;
356 386
357 mutex_lock(&data->update_lock); 387 mutex_lock(&data->update_lock);
358 data->in_min[nr] = IN_TO_REG(val); 388 data->in_min[nr] = IN_TO_REG(val);
@@ -368,7 +398,10 @@ static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
368 int nr = sensor_attr->index; 398 int nr = sensor_attr->index;
369 399
370 struct it87_data *data = dev_get_drvdata(dev); 400 struct it87_data *data = dev_get_drvdata(dev);
371 unsigned long val = simple_strtoul(buf, NULL, 10); 401 unsigned long val;
402
403 if (strict_strtoul(buf, 10, &val) < 0)
404 return -EINVAL;
372 405
373 mutex_lock(&data->update_lock); 406 mutex_lock(&data->update_lock);
374 data->in_max[nr] = IN_TO_REG(val); 407 data->in_max[nr] = IN_TO_REG(val);
@@ -441,7 +474,10 @@ static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
441 int nr = sensor_attr->index; 474 int nr = sensor_attr->index;
442 475
443 struct it87_data *data = dev_get_drvdata(dev); 476 struct it87_data *data = dev_get_drvdata(dev);
444 int val = simple_strtol(buf, NULL, 10); 477 long val;
478
479 if (strict_strtol(buf, 10, &val) < 0)
480 return -EINVAL;
445 481
446 mutex_lock(&data->update_lock); 482 mutex_lock(&data->update_lock);
447 data->temp_high[nr] = TEMP_TO_REG(val); 483 data->temp_high[nr] = TEMP_TO_REG(val);
@@ -456,7 +492,10 @@ static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
456 int nr = sensor_attr->index; 492 int nr = sensor_attr->index;
457 493
458 struct it87_data *data = dev_get_drvdata(dev); 494 struct it87_data *data = dev_get_drvdata(dev);
459 int val = simple_strtol(buf, NULL, 10); 495 long val;
496
497 if (strict_strtol(buf, 10, &val) < 0)
498 return -EINVAL;
460 499
461 mutex_lock(&data->update_lock); 500 mutex_lock(&data->update_lock);
462 data->temp_low[nr] = TEMP_TO_REG(val); 501 data->temp_low[nr] = TEMP_TO_REG(val);
@@ -483,8 +522,9 @@ static ssize_t show_sensor(struct device *dev, struct device_attribute *attr,
483 int nr = sensor_attr->index; 522 int nr = sensor_attr->index;
484 523
485 struct it87_data *data = it87_update_device(dev); 524 struct it87_data *data = it87_update_device(dev);
486 u8 reg = data->sensor; /* In case the value is updated while we use it */ 525 u8 reg = data->sensor; /* In case the value is updated while
487 526 we use it */
527
488 if (reg & (1 << nr)) 528 if (reg & (1 << nr))
489 return sprintf(buf, "3\n"); /* thermal diode */ 529 return sprintf(buf, "3\n"); /* thermal diode */
490 if (reg & (8 << nr)) 530 if (reg & (8 << nr))
@@ -498,7 +538,10 @@ static ssize_t set_sensor(struct device *dev, struct device_attribute *attr,
498 int nr = sensor_attr->index; 538 int nr = sensor_attr->index;
499 539
500 struct it87_data *data = dev_get_drvdata(dev); 540 struct it87_data *data = dev_get_drvdata(dev);
501 int val = simple_strtol(buf, NULL, 10); 541 long val;
542
543 if (strict_strtol(buf, 10, &val) < 0)
544 return -EINVAL;
502 545
503 mutex_lock(&data->update_lock); 546 mutex_lock(&data->update_lock);
504 547
@@ -511,9 +554,9 @@ static ssize_t set_sensor(struct device *dev, struct device_attribute *attr,
511 } 554 }
512 /* 3 = thermal diode; 4 = thermistor; 0 = disabled */ 555 /* 3 = thermal diode; 4 = thermistor; 0 = disabled */
513 if (val == 3) 556 if (val == 3)
514 data->sensor |= 1 << nr; 557 data->sensor |= 1 << nr;
515 else if (val == 4) 558 else if (val == 4)
516 data->sensor |= 8 << nr; 559 data->sensor |= 8 << nr;
517 else if (val != 0) { 560 else if (val != 0) {
518 mutex_unlock(&data->update_lock); 561 mutex_unlock(&data->update_lock);
519 return -EINVAL; 562 return -EINVAL;
@@ -531,6 +574,19 @@ show_sensor_offset(2);
531show_sensor_offset(3); 574show_sensor_offset(3);
532 575
533/* 3 Fans */ 576/* 3 Fans */
577
578static int pwm_mode(const struct it87_data *data, int nr)
579{
580 int ctrl = data->fan_main_ctrl & (1 << nr);
581
582 if (ctrl == 0) /* Full speed */
583 return 0;
584 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
585 return 2;
586 else /* Manual mode */
587 return 1;
588}
589
534static ssize_t show_fan(struct device *dev, struct device_attribute *attr, 590static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
535 char *buf) 591 char *buf)
536{ 592{
@@ -538,7 +594,7 @@ static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
538 int nr = sensor_attr->index; 594 int nr = sensor_attr->index;
539 595
540 struct it87_data *data = it87_update_device(dev); 596 struct it87_data *data = it87_update_device(dev);
541 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan[nr], 597 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
542 DIV_FROM_REG(data->fan_div[nr]))); 598 DIV_FROM_REG(data->fan_div[nr])));
543} 599}
544static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr, 600static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
@@ -548,8 +604,8 @@ static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
548 int nr = sensor_attr->index; 604 int nr = sensor_attr->index;
549 605
550 struct it87_data *data = it87_update_device(dev); 606 struct it87_data *data = it87_update_device(dev);
551 return sprintf(buf,"%d\n", 607 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
552 FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr]))); 608 DIV_FROM_REG(data->fan_div[nr])));
553} 609}
554static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr, 610static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
555 char *buf) 611 char *buf)
@@ -560,14 +616,14 @@ static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
560 struct it87_data *data = it87_update_device(dev); 616 struct it87_data *data = it87_update_device(dev);
561 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr])); 617 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
562} 618}
563static ssize_t show_pwm_enable(struct device *dev, struct device_attribute *attr, 619static ssize_t show_pwm_enable(struct device *dev,
564 char *buf) 620 struct device_attribute *attr, char *buf)
565{ 621{
566 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); 622 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
567 int nr = sensor_attr->index; 623 int nr = sensor_attr->index;
568 624
569 struct it87_data *data = it87_update_device(dev); 625 struct it87_data *data = it87_update_device(dev);
570 return sprintf(buf,"%d\n", (data->fan_main_ctrl & (1 << nr)) ? 1 : 0); 626 return sprintf(buf, "%d\n", pwm_mode(data, nr));
571} 627}
572static ssize_t show_pwm(struct device *dev, struct device_attribute *attr, 628static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
573 char *buf) 629 char *buf)
@@ -576,7 +632,7 @@ static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
576 int nr = sensor_attr->index; 632 int nr = sensor_attr->index;
577 633
578 struct it87_data *data = it87_update_device(dev); 634 struct it87_data *data = it87_update_device(dev);
579 return sprintf(buf,"%d\n", data->manual_pwm_ctl[nr]); 635 return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm_duty[nr]));
580} 636}
581static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr, 637static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr,
582 char *buf) 638 char *buf)
@@ -593,15 +649,24 @@ static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
593 int nr = sensor_attr->index; 649 int nr = sensor_attr->index;
594 650
595 struct it87_data *data = dev_get_drvdata(dev); 651 struct it87_data *data = dev_get_drvdata(dev);
596 int val = simple_strtol(buf, NULL, 10); 652 long val;
597 u8 reg; 653 u8 reg;
598 654
655 if (strict_strtol(buf, 10, &val) < 0)
656 return -EINVAL;
657
599 mutex_lock(&data->update_lock); 658 mutex_lock(&data->update_lock);
600 reg = it87_read_value(data, IT87_REG_FAN_DIV); 659 reg = it87_read_value(data, IT87_REG_FAN_DIV);
601 switch (nr) { 660 switch (nr) {
602 case 0: data->fan_div[nr] = reg & 0x07; break; 661 case 0:
603 case 1: data->fan_div[nr] = (reg >> 3) & 0x07; break; 662 data->fan_div[nr] = reg & 0x07;
604 case 2: data->fan_div[nr] = (reg & 0x40) ? 3 : 1; break; 663 break;
664 case 1:
665 data->fan_div[nr] = (reg >> 3) & 0x07;
666 break;
667 case 2:
668 data->fan_div[nr] = (reg & 0x40) ? 3 : 1;
669 break;
605 } 670 }
606 671
607 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr])); 672 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
@@ -616,10 +681,13 @@ static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
616 int nr = sensor_attr->index; 681 int nr = sensor_attr->index;
617 682
618 struct it87_data *data = dev_get_drvdata(dev); 683 struct it87_data *data = dev_get_drvdata(dev);
619 unsigned long val = simple_strtoul(buf, NULL, 10); 684 unsigned long val;
620 int min; 685 int min;
621 u8 old; 686 u8 old;
622 687
688 if (strict_strtoul(buf, 10, &val) < 0)
689 return -EINVAL;
690
623 mutex_lock(&data->update_lock); 691 mutex_lock(&data->update_lock);
624 old = it87_read_value(data, IT87_REG_FAN_DIV); 692 old = it87_read_value(data, IT87_REG_FAN_DIV);
625 693
@@ -651,6 +719,32 @@ static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
651 mutex_unlock(&data->update_lock); 719 mutex_unlock(&data->update_lock);
652 return count; 720 return count;
653} 721}
722
723/* Returns 0 if OK, -EINVAL otherwise */
724static int check_trip_points(struct device *dev, int nr)
725{
726 const struct it87_data *data = dev_get_drvdata(dev);
727 int i, err = 0;
728
729 if (has_old_autopwm(data)) {
730 for (i = 0; i < 3; i++) {
731 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
732 err = -EINVAL;
733 }
734 for (i = 0; i < 2; i++) {
735 if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
736 err = -EINVAL;
737 }
738 }
739
740 if (err) {
741 dev_err(dev, "Inconsistent trip points, not switching to "
742 "automatic mode\n");
743 dev_err(dev, "Adjust the trip points and try again\n");
744 }
745 return err;
746}
747
654static ssize_t set_pwm_enable(struct device *dev, 748static ssize_t set_pwm_enable(struct device *dev,
655 struct device_attribute *attr, const char *buf, size_t count) 749 struct device_attribute *attr, const char *buf, size_t count)
656{ 750{
@@ -658,7 +752,16 @@ static ssize_t set_pwm_enable(struct device *dev,
658 int nr = sensor_attr->index; 752 int nr = sensor_attr->index;
659 753
660 struct it87_data *data = dev_get_drvdata(dev); 754 struct it87_data *data = dev_get_drvdata(dev);
661 int val = simple_strtol(buf, NULL, 10); 755 long val;
756
757 if (strict_strtol(buf, 10, &val) < 0 || val < 0 || val > 2)
758 return -EINVAL;
759
760 /* Check trip points before switching to automatic mode */
761 if (val == 2) {
762 if (check_trip_points(dev, nr) < 0)
763 return -EINVAL;
764 }
662 765
663 mutex_lock(&data->update_lock); 766 mutex_lock(&data->update_lock);
664 767
@@ -669,16 +772,18 @@ static ssize_t set_pwm_enable(struct device *dev,
669 it87_write_value(data, IT87_REG_FAN_CTL, tmp | (1 << nr)); 772 it87_write_value(data, IT87_REG_FAN_CTL, tmp | (1 << nr));
670 /* set on/off mode */ 773 /* set on/off mode */
671 data->fan_main_ctrl &= ~(1 << nr); 774 data->fan_main_ctrl &= ~(1 << nr);
672 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl); 775 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
673 } else if (val == 1) { 776 data->fan_main_ctrl);
777 } else {
778 if (val == 1) /* Manual mode */
779 data->pwm_ctrl[nr] = data->pwm_duty[nr];
780 else /* Automatic mode */
781 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
782 it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]);
674 /* set SmartGuardian mode */ 783 /* set SmartGuardian mode */
675 data->fan_main_ctrl |= (1 << nr); 784 data->fan_main_ctrl |= (1 << nr);
676 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl); 785 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
677 /* set saved pwm value, clear FAN_CTLX PWM mode bit */ 786 data->fan_main_ctrl);
678 it87_write_value(data, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr]));
679 } else {
680 mutex_unlock(&data->update_lock);
681 return -EINVAL;
682 } 787 }
683 788
684 mutex_unlock(&data->update_lock); 789 mutex_unlock(&data->update_lock);
@@ -691,15 +796,19 @@ static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
691 int nr = sensor_attr->index; 796 int nr = sensor_attr->index;
692 797
693 struct it87_data *data = dev_get_drvdata(dev); 798 struct it87_data *data = dev_get_drvdata(dev);
694 int val = simple_strtol(buf, NULL, 10); 799 long val;
695 800
696 if (val < 0 || val > 255) 801 if (strict_strtol(buf, 10, &val) < 0 || val < 0 || val > 255)
697 return -EINVAL; 802 return -EINVAL;
698 803
699 mutex_lock(&data->update_lock); 804 mutex_lock(&data->update_lock);
700 data->manual_pwm_ctl[nr] = val; 805 data->pwm_duty[nr] = PWM_TO_REG(val);
701 if (data->fan_main_ctrl & (1 << nr)) 806 /* If we are in manual mode, write the duty cycle immediately;
702 it87_write_value(data, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr])); 807 * otherwise, just store it for later use. */
808 if (!(data->pwm_ctrl[nr] & 0x80)) {
809 data->pwm_ctrl[nr] = data->pwm_duty[nr];
810 it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]);
811 }
703 mutex_unlock(&data->update_lock); 812 mutex_unlock(&data->update_lock);
704 return count; 813 return count;
705} 814}
@@ -707,9 +816,12 @@ static ssize_t set_pwm_freq(struct device *dev,
707 struct device_attribute *attr, const char *buf, size_t count) 816 struct device_attribute *attr, const char *buf, size_t count)
708{ 817{
709 struct it87_data *data = dev_get_drvdata(dev); 818 struct it87_data *data = dev_get_drvdata(dev);
710 unsigned long val = simple_strtoul(buf, NULL, 10); 819 unsigned long val;
711 int i; 820 int i;
712 821
822 if (strict_strtoul(buf, 10, &val) < 0)
823 return -EINVAL;
824
713 /* Search for the nearest available frequency */ 825 /* Search for the nearest available frequency */
714 for (i = 0; i < 7; i++) { 826 for (i = 0; i < 7; i++) {
715 if (val > (pwm_freq[i] + pwm_freq[i+1]) / 2) 827 if (val > (pwm_freq[i] + pwm_freq[i+1]) / 2)
@@ -724,6 +836,132 @@ static ssize_t set_pwm_freq(struct device *dev,
724 836
725 return count; 837 return count;
726} 838}
839static ssize_t show_pwm_temp_map(struct device *dev,
840 struct device_attribute *attr, char *buf)
841{
842 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
843 int nr = sensor_attr->index;
844
845 struct it87_data *data = it87_update_device(dev);
846 int map;
847
848 if (data->pwm_temp_map[nr] < 3)
849 map = 1 << data->pwm_temp_map[nr];
850 else
851 map = 0; /* Should never happen */
852 return sprintf(buf, "%d\n", map);
853}
854static ssize_t set_pwm_temp_map(struct device *dev,
855 struct device_attribute *attr, const char *buf, size_t count)
856{
857 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
858 int nr = sensor_attr->index;
859
860 struct it87_data *data = dev_get_drvdata(dev);
861 long val;
862 u8 reg;
863
864 /* This check can go away if we ever support automatic fan speed
865 control on newer chips. */
866 if (!has_old_autopwm(data)) {
867 dev_notice(dev, "Mapping change disabled for safety reasons\n");
868 return -EINVAL;
869 }
870
871 if (strict_strtol(buf, 10, &val) < 0)
872 return -EINVAL;
873
874 switch (val) {
875 case (1 << 0):
876 reg = 0x00;
877 break;
878 case (1 << 1):
879 reg = 0x01;
880 break;
881 case (1 << 2):
882 reg = 0x02;
883 break;
884 default:
885 return -EINVAL;
886 }
887
888 mutex_lock(&data->update_lock);
889 data->pwm_temp_map[nr] = reg;
890 /* If we are in automatic mode, write the temp mapping immediately;
891 * otherwise, just store it for later use. */
892 if (data->pwm_ctrl[nr] & 0x80) {
893 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
894 it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]);
895 }
896 mutex_unlock(&data->update_lock);
897 return count;
898}
899
900static ssize_t show_auto_pwm(struct device *dev,
901 struct device_attribute *attr, char *buf)
902{
903 struct it87_data *data = it87_update_device(dev);
904 struct sensor_device_attribute_2 *sensor_attr =
905 to_sensor_dev_attr_2(attr);
906 int nr = sensor_attr->nr;
907 int point = sensor_attr->index;
908
909 return sprintf(buf, "%d\n", PWM_FROM_REG(data->auto_pwm[nr][point]));
910}
911
912static ssize_t set_auto_pwm(struct device *dev,
913 struct device_attribute *attr, const char *buf, size_t count)
914{
915 struct it87_data *data = dev_get_drvdata(dev);
916 struct sensor_device_attribute_2 *sensor_attr =
917 to_sensor_dev_attr_2(attr);
918 int nr = sensor_attr->nr;
919 int point = sensor_attr->index;
920 long val;
921
922 if (strict_strtol(buf, 10, &val) < 0 || val < 0 || val > 255)
923 return -EINVAL;
924
925 mutex_lock(&data->update_lock);
926 data->auto_pwm[nr][point] = PWM_TO_REG(val);
927 it87_write_value(data, IT87_REG_AUTO_PWM(nr, point),
928 data->auto_pwm[nr][point]);
929 mutex_unlock(&data->update_lock);
930 return count;
931}
932
933static ssize_t show_auto_temp(struct device *dev,
934 struct device_attribute *attr, char *buf)
935{
936 struct it87_data *data = it87_update_device(dev);
937 struct sensor_device_attribute_2 *sensor_attr =
938 to_sensor_dev_attr_2(attr);
939 int nr = sensor_attr->nr;
940 int point = sensor_attr->index;
941
942 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->auto_temp[nr][point]));
943}
944
945static ssize_t set_auto_temp(struct device *dev,
946 struct device_attribute *attr, const char *buf, size_t count)
947{
948 struct it87_data *data = dev_get_drvdata(dev);
949 struct sensor_device_attribute_2 *sensor_attr =
950 to_sensor_dev_attr_2(attr);
951 int nr = sensor_attr->nr;
952 int point = sensor_attr->index;
953 long val;
954
955 if (strict_strtol(buf, 10, &val) < 0 || val < -128000 || val > 127000)
956 return -EINVAL;
957
958 mutex_lock(&data->update_lock);
959 data->auto_temp[nr][point] = TEMP_TO_REG(val);
960 it87_write_value(data, IT87_REG_AUTO_TEMP(nr, point),
961 data->auto_temp[nr][point]);
962 mutex_unlock(&data->update_lock);
963 return count;
964}
727 965
728#define show_fan_offset(offset) \ 966#define show_fan_offset(offset) \
729static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \ 967static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
@@ -744,7 +982,36 @@ static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
744 show_pwm, set_pwm, offset - 1); \ 982 show_pwm, set_pwm, offset - 1); \
745static DEVICE_ATTR(pwm##offset##_freq, \ 983static DEVICE_ATTR(pwm##offset##_freq, \
746 (offset == 1 ? S_IRUGO | S_IWUSR : S_IRUGO), \ 984 (offset == 1 ? S_IRUGO | S_IWUSR : S_IRUGO), \
747 show_pwm_freq, (offset == 1 ? set_pwm_freq : NULL)); 985 show_pwm_freq, (offset == 1 ? set_pwm_freq : NULL)); \
986static SENSOR_DEVICE_ATTR(pwm##offset##_auto_channels_temp, \
987 S_IRUGO | S_IWUSR, show_pwm_temp_map, set_pwm_temp_map, \
988 offset - 1); \
989static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point1_pwm, \
990 S_IRUGO | S_IWUSR, show_auto_pwm, set_auto_pwm, \
991 offset - 1, 0); \
992static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point2_pwm, \
993 S_IRUGO | S_IWUSR, show_auto_pwm, set_auto_pwm, \
994 offset - 1, 1); \
995static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point3_pwm, \
996 S_IRUGO | S_IWUSR, show_auto_pwm, set_auto_pwm, \
997 offset - 1, 2); \
998static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point4_pwm, \
999 S_IRUGO, show_auto_pwm, NULL, offset - 1, 3); \
1000static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point1_temp, \
1001 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1002 offset - 1, 1); \
1003static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point1_temp_hyst, \
1004 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1005 offset - 1, 0); \
1006static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point2_temp, \
1007 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1008 offset - 1, 2); \
1009static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point3_temp, \
1010 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1011 offset - 1, 3); \
1012static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point4_temp, \
1013 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1014 offset - 1, 4);
748 1015
749show_pwm_offset(1); 1016show_pwm_offset(1);
750show_pwm_offset(2); 1017show_pwm_offset(2);
@@ -775,7 +1042,10 @@ static ssize_t set_fan16_min(struct device *dev, struct device_attribute *attr,
775 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); 1042 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
776 int nr = sensor_attr->index; 1043 int nr = sensor_attr->index;
777 struct it87_data *data = dev_get_drvdata(dev); 1044 struct it87_data *data = dev_get_drvdata(dev);
778 int val = simple_strtol(buf, NULL, 10); 1045 long val;
1046
1047 if (strict_strtol(buf, 10, &val) < 0)
1048 return -EINVAL;
779 1049
780 mutex_lock(&data->update_lock); 1050 mutex_lock(&data->update_lock);
781 data->fan_min[nr] = FAN16_TO_REG(val); 1051 data->fan_min[nr] = FAN16_TO_REG(val);
@@ -805,7 +1075,8 @@ show_fan16_offset(4);
805show_fan16_offset(5); 1075show_fan16_offset(5);
806 1076
807/* Alarms */ 1077/* Alarms */
808static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf) 1078static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
1079 char *buf)
809{ 1080{
810 struct it87_data *data = it87_update_device(dev); 1081 struct it87_data *data = it87_update_device(dev);
811 return sprintf(buf, "%u\n", data->alarms); 1082 return sprintf(buf, "%u\n", data->alarms);
@@ -836,27 +1107,78 @@ static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 16);
836static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 17); 1107static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 17);
837static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 18); 1108static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 18);
838 1109
839static ssize_t 1110static ssize_t show_beep(struct device *dev, struct device_attribute *attr,
840show_vrm_reg(struct device *dev, struct device_attribute *attr, char *buf) 1111 char *buf)
1112{
1113 int bitnr = to_sensor_dev_attr(attr)->index;
1114 struct it87_data *data = it87_update_device(dev);
1115 return sprintf(buf, "%u\n", (data->beeps >> bitnr) & 1);
1116}
1117static ssize_t set_beep(struct device *dev, struct device_attribute *attr,
1118 const char *buf, size_t count)
1119{
1120 int bitnr = to_sensor_dev_attr(attr)->index;
1121 struct it87_data *data = dev_get_drvdata(dev);
1122 long val;
1123
1124 if (strict_strtol(buf, 10, &val) < 0
1125 || (val != 0 && val != 1))
1126 return -EINVAL;
1127
1128 mutex_lock(&data->update_lock);
1129 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
1130 if (val)
1131 data->beeps |= (1 << bitnr);
1132 else
1133 data->beeps &= ~(1 << bitnr);
1134 it87_write_value(data, IT87_REG_BEEP_ENABLE, data->beeps);
1135 mutex_unlock(&data->update_lock);
1136 return count;
1137}
1138
1139static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO | S_IWUSR,
1140 show_beep, set_beep, 1);
1141static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO, show_beep, NULL, 1);
1142static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO, show_beep, NULL, 1);
1143static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO, show_beep, NULL, 1);
1144static SENSOR_DEVICE_ATTR(in4_beep, S_IRUGO, show_beep, NULL, 1);
1145static SENSOR_DEVICE_ATTR(in5_beep, S_IRUGO, show_beep, NULL, 1);
1146static SENSOR_DEVICE_ATTR(in6_beep, S_IRUGO, show_beep, NULL, 1);
1147static SENSOR_DEVICE_ATTR(in7_beep, S_IRUGO, show_beep, NULL, 1);
1148/* fanX_beep writability is set later */
1149static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO, show_beep, set_beep, 0);
1150static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO, show_beep, set_beep, 0);
1151static SENSOR_DEVICE_ATTR(fan3_beep, S_IRUGO, show_beep, set_beep, 0);
1152static SENSOR_DEVICE_ATTR(fan4_beep, S_IRUGO, show_beep, set_beep, 0);
1153static SENSOR_DEVICE_ATTR(fan5_beep, S_IRUGO, show_beep, set_beep, 0);
1154static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO | S_IWUSR,
1155 show_beep, set_beep, 2);
1156static SENSOR_DEVICE_ATTR(temp2_beep, S_IRUGO, show_beep, NULL, 2);
1157static SENSOR_DEVICE_ATTR(temp3_beep, S_IRUGO, show_beep, NULL, 2);
1158
1159static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
1160 char *buf)
841{ 1161{
842 struct it87_data *data = dev_get_drvdata(dev); 1162 struct it87_data *data = dev_get_drvdata(dev);
843 return sprintf(buf, "%u\n", data->vrm); 1163 return sprintf(buf, "%u\n", data->vrm);
844} 1164}
845static ssize_t 1165static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
846store_vrm_reg(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) 1166 const char *buf, size_t count)
847{ 1167{
848 struct it87_data *data = dev_get_drvdata(dev); 1168 struct it87_data *data = dev_get_drvdata(dev);
849 u32 val; 1169 unsigned long val;
1170
1171 if (strict_strtoul(buf, 10, &val) < 0)
1172 return -EINVAL;
850 1173
851 val = simple_strtoul(buf, NULL, 10);
852 data->vrm = val; 1174 data->vrm = val;
853 1175
854 return count; 1176 return count;
855} 1177}
856static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg); 1178static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
857 1179
858static ssize_t 1180static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
859show_vid_reg(struct device *dev, struct device_attribute *attr, char *buf) 1181 char *buf)
860{ 1182{
861 struct it87_data *data = it87_update_device(dev); 1183 struct it87_data *data = it87_update_device(dev);
862 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm)); 1184 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
@@ -931,51 +1253,176 @@ static const struct attribute_group it87_group = {
931 .attrs = it87_attributes, 1253 .attrs = it87_attributes,
932}; 1254};
933 1255
934static struct attribute *it87_attributes_opt[] = { 1256static struct attribute *it87_attributes_beep[] = {
1257 &sensor_dev_attr_in0_beep.dev_attr.attr,
1258 &sensor_dev_attr_in1_beep.dev_attr.attr,
1259 &sensor_dev_attr_in2_beep.dev_attr.attr,
1260 &sensor_dev_attr_in3_beep.dev_attr.attr,
1261 &sensor_dev_attr_in4_beep.dev_attr.attr,
1262 &sensor_dev_attr_in5_beep.dev_attr.attr,
1263 &sensor_dev_attr_in6_beep.dev_attr.attr,
1264 &sensor_dev_attr_in7_beep.dev_attr.attr,
1265
1266 &sensor_dev_attr_temp1_beep.dev_attr.attr,
1267 &sensor_dev_attr_temp2_beep.dev_attr.attr,
1268 &sensor_dev_attr_temp3_beep.dev_attr.attr,
1269 NULL
1270};
1271
1272static const struct attribute_group it87_group_beep = {
1273 .attrs = it87_attributes_beep,
1274};
1275
1276static struct attribute *it87_attributes_fan16[5][3+1] = { {
935 &sensor_dev_attr_fan1_input16.dev_attr.attr, 1277 &sensor_dev_attr_fan1_input16.dev_attr.attr,
936 &sensor_dev_attr_fan1_min16.dev_attr.attr, 1278 &sensor_dev_attr_fan1_min16.dev_attr.attr,
1279 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1280 NULL
1281}, {
937 &sensor_dev_attr_fan2_input16.dev_attr.attr, 1282 &sensor_dev_attr_fan2_input16.dev_attr.attr,
938 &sensor_dev_attr_fan2_min16.dev_attr.attr, 1283 &sensor_dev_attr_fan2_min16.dev_attr.attr,
1284 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1285 NULL
1286}, {
939 &sensor_dev_attr_fan3_input16.dev_attr.attr, 1287 &sensor_dev_attr_fan3_input16.dev_attr.attr,
940 &sensor_dev_attr_fan3_min16.dev_attr.attr, 1288 &sensor_dev_attr_fan3_min16.dev_attr.attr,
1289 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1290 NULL
1291}, {
941 &sensor_dev_attr_fan4_input16.dev_attr.attr, 1292 &sensor_dev_attr_fan4_input16.dev_attr.attr,
942 &sensor_dev_attr_fan4_min16.dev_attr.attr, 1293 &sensor_dev_attr_fan4_min16.dev_attr.attr,
1294 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1295 NULL
1296}, {
943 &sensor_dev_attr_fan5_input16.dev_attr.attr, 1297 &sensor_dev_attr_fan5_input16.dev_attr.attr,
944 &sensor_dev_attr_fan5_min16.dev_attr.attr, 1298 &sensor_dev_attr_fan5_min16.dev_attr.attr,
1299 &sensor_dev_attr_fan5_alarm.dev_attr.attr,
1300 NULL
1301} };
1302
1303static const struct attribute_group it87_group_fan16[5] = {
1304 { .attrs = it87_attributes_fan16[0] },
1305 { .attrs = it87_attributes_fan16[1] },
1306 { .attrs = it87_attributes_fan16[2] },
1307 { .attrs = it87_attributes_fan16[3] },
1308 { .attrs = it87_attributes_fan16[4] },
1309};
945 1310
1311static struct attribute *it87_attributes_fan[3][4+1] = { {
946 &sensor_dev_attr_fan1_input.dev_attr.attr, 1312 &sensor_dev_attr_fan1_input.dev_attr.attr,
947 &sensor_dev_attr_fan1_min.dev_attr.attr, 1313 &sensor_dev_attr_fan1_min.dev_attr.attr,
948 &sensor_dev_attr_fan1_div.dev_attr.attr, 1314 &sensor_dev_attr_fan1_div.dev_attr.attr,
1315 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1316 NULL
1317}, {
949 &sensor_dev_attr_fan2_input.dev_attr.attr, 1318 &sensor_dev_attr_fan2_input.dev_attr.attr,
950 &sensor_dev_attr_fan2_min.dev_attr.attr, 1319 &sensor_dev_attr_fan2_min.dev_attr.attr,
951 &sensor_dev_attr_fan2_div.dev_attr.attr, 1320 &sensor_dev_attr_fan2_div.dev_attr.attr,
1321 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1322 NULL
1323}, {
952 &sensor_dev_attr_fan3_input.dev_attr.attr, 1324 &sensor_dev_attr_fan3_input.dev_attr.attr,
953 &sensor_dev_attr_fan3_min.dev_attr.attr, 1325 &sensor_dev_attr_fan3_min.dev_attr.attr,
954 &sensor_dev_attr_fan3_div.dev_attr.attr, 1326 &sensor_dev_attr_fan3_div.dev_attr.attr,
955
956 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
957 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
958 &sensor_dev_attr_fan3_alarm.dev_attr.attr, 1327 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
959 &sensor_dev_attr_fan4_alarm.dev_attr.attr, 1328 NULL
960 &sensor_dev_attr_fan5_alarm.dev_attr.attr, 1329} };
1330
1331static const struct attribute_group it87_group_fan[3] = {
1332 { .attrs = it87_attributes_fan[0] },
1333 { .attrs = it87_attributes_fan[1] },
1334 { .attrs = it87_attributes_fan[2] },
1335};
1336
1337static const struct attribute_group *
1338it87_get_fan_group(const struct it87_data *data)
1339{
1340 return has_16bit_fans(data) ? it87_group_fan16 : it87_group_fan;
1341}
961 1342
1343static struct attribute *it87_attributes_pwm[3][4+1] = { {
962 &sensor_dev_attr_pwm1_enable.dev_attr.attr, 1344 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
963 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
964 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
965 &sensor_dev_attr_pwm1.dev_attr.attr, 1345 &sensor_dev_attr_pwm1.dev_attr.attr,
966 &sensor_dev_attr_pwm2.dev_attr.attr,
967 &sensor_dev_attr_pwm3.dev_attr.attr,
968 &dev_attr_pwm1_freq.attr, 1346 &dev_attr_pwm1_freq.attr,
1347 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
1348 NULL
1349}, {
1350 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1351 &sensor_dev_attr_pwm2.dev_attr.attr,
969 &dev_attr_pwm2_freq.attr, 1352 &dev_attr_pwm2_freq.attr,
1353 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
1354 NULL
1355}, {
1356 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1357 &sensor_dev_attr_pwm3.dev_attr.attr,
970 &dev_attr_pwm3_freq.attr, 1358 &dev_attr_pwm3_freq.attr,
1359 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
1360 NULL
1361} };
1362
1363static const struct attribute_group it87_group_pwm[3] = {
1364 { .attrs = it87_attributes_pwm[0] },
1365 { .attrs = it87_attributes_pwm[1] },
1366 { .attrs = it87_attributes_pwm[2] },
1367};
971 1368
1369static struct attribute *it87_attributes_autopwm[3][9+1] = { {
1370 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1371 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
1372 &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
1373 &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
1374 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
1375 &sensor_dev_attr_pwm1_auto_point1_temp_hyst.dev_attr.attr,
1376 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
1377 &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
1378 &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
1379 NULL
1380}, {
1381 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1382 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
1383 &sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr,
1384 &sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr,
1385 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
1386 &sensor_dev_attr_pwm2_auto_point1_temp_hyst.dev_attr.attr,
1387 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
1388 &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
1389 &sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr,
1390 NULL
1391}, {
1392 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1393 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
1394 &sensor_dev_attr_pwm3_auto_point3_pwm.dev_attr.attr,
1395 &sensor_dev_attr_pwm3_auto_point4_pwm.dev_attr.attr,
1396 &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
1397 &sensor_dev_attr_pwm3_auto_point1_temp_hyst.dev_attr.attr,
1398 &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
1399 &sensor_dev_attr_pwm3_auto_point3_temp.dev_attr.attr,
1400 &sensor_dev_attr_pwm3_auto_point4_temp.dev_attr.attr,
1401 NULL
1402} };
1403
1404static const struct attribute_group it87_group_autopwm[3] = {
1405 { .attrs = it87_attributes_autopwm[0] },
1406 { .attrs = it87_attributes_autopwm[1] },
1407 { .attrs = it87_attributes_autopwm[2] },
1408};
1409
1410static struct attribute *it87_attributes_fan_beep[] = {
1411 &sensor_dev_attr_fan1_beep.dev_attr.attr,
1412 &sensor_dev_attr_fan2_beep.dev_attr.attr,
1413 &sensor_dev_attr_fan3_beep.dev_attr.attr,
1414 &sensor_dev_attr_fan4_beep.dev_attr.attr,
1415 &sensor_dev_attr_fan5_beep.dev_attr.attr,
1416};
1417
1418static struct attribute *it87_attributes_vid[] = {
972 &dev_attr_vrm.attr, 1419 &dev_attr_vrm.attr,
973 &dev_attr_cpu0_vid.attr, 1420 &dev_attr_cpu0_vid.attr,
974 NULL 1421 NULL
975}; 1422};
976 1423
977static const struct attribute_group it87_group_opt = { 1424static const struct attribute_group it87_group_vid = {
978 .attrs = it87_attributes_opt, 1425 .attrs = it87_attributes_vid,
979}; 1426};
980 1427
981/* SuperIO detection - will change isa_address if a chip is found */ 1428/* SuperIO detection - will change isa_address if a chip is found */
@@ -1035,6 +1482,10 @@ static int __init it87_find(unsigned short *address,
1035 if (sio_data->type == it87) { 1482 if (sio_data->type == it87) {
1036 /* The IT8705F doesn't have VID pins at all */ 1483 /* The IT8705F doesn't have VID pins at all */
1037 sio_data->skip_vid = 1; 1484 sio_data->skip_vid = 1;
1485
1486 /* The IT8705F has a different LD number for GPIO */
1487 superio_select(5);
1488 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
1038 } else { 1489 } else {
1039 int reg; 1490 int reg;
1040 1491
@@ -1068,7 +1519,11 @@ static int __init it87_find(unsigned short *address,
1068 pr_info("it87: in3 is VCC (+5V)\n"); 1519 pr_info("it87: in3 is VCC (+5V)\n");
1069 if (reg & (1 << 1)) 1520 if (reg & (1 << 1))
1070 pr_info("it87: in7 is VCCH (+5V Stand-By)\n"); 1521 pr_info("it87: in7 is VCCH (+5V Stand-By)\n");
1522
1523 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
1071 } 1524 }
1525 if (sio_data->beep_pin)
1526 pr_info("it87: Beeping is supported\n");
1072 1527
1073 /* Disable specific features based on DMI strings */ 1528 /* Disable specific features based on DMI strings */
1074 board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR); 1529 board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
@@ -1093,14 +1548,46 @@ exit:
1093 return err; 1548 return err;
1094} 1549}
1095 1550
1551static void it87_remove_files(struct device *dev)
1552{
1553 struct it87_data *data = platform_get_drvdata(pdev);
1554 struct it87_sio_data *sio_data = dev->platform_data;
1555 const struct attribute_group *fan_group = it87_get_fan_group(data);
1556 int i;
1557
1558 sysfs_remove_group(&dev->kobj, &it87_group);
1559 if (sio_data->beep_pin)
1560 sysfs_remove_group(&dev->kobj, &it87_group_beep);
1561 for (i = 0; i < 5; i++) {
1562 if (!(data->has_fan & (1 << i)))
1563 continue;
1564 sysfs_remove_group(&dev->kobj, &fan_group[i]);
1565 if (sio_data->beep_pin)
1566 sysfs_remove_file(&dev->kobj,
1567 it87_attributes_fan_beep[i]);
1568 }
1569 for (i = 0; i < 3; i++) {
1570 if (sio_data->skip_pwm & (1 << 0))
1571 continue;
1572 sysfs_remove_group(&dev->kobj, &it87_group_pwm[i]);
1573 if (has_old_autopwm(data))
1574 sysfs_remove_group(&dev->kobj,
1575 &it87_group_autopwm[i]);
1576 }
1577 if (!sio_data->skip_vid)
1578 sysfs_remove_group(&dev->kobj, &it87_group_vid);
1579}
1580
1096static int __devinit it87_probe(struct platform_device *pdev) 1581static int __devinit it87_probe(struct platform_device *pdev)
1097{ 1582{
1098 struct it87_data *data; 1583 struct it87_data *data;
1099 struct resource *res; 1584 struct resource *res;
1100 struct device *dev = &pdev->dev; 1585 struct device *dev = &pdev->dev;
1101 struct it87_sio_data *sio_data = dev->platform_data; 1586 struct it87_sio_data *sio_data = dev->platform_data;
1102 int err = 0; 1587 const struct attribute_group *fan_group;
1588 int err = 0, i;
1103 int enable_pwm_interface; 1589 int enable_pwm_interface;
1590 int fan_beep_need_rw;
1104 static const char *names[] = { 1591 static const char *names[] = {
1105 "it87", 1592 "it87",
1106 "it8712", 1593 "it8712",
@@ -1118,7 +1605,8 @@ static int __devinit it87_probe(struct platform_device *pdev)
1118 goto ERROR0; 1605 goto ERROR0;
1119 } 1606 }
1120 1607
1121 if (!(data = kzalloc(sizeof(struct it87_data), GFP_KERNEL))) { 1608 data = kzalloc(sizeof(struct it87_data), GFP_KERNEL);
1609 if (!data) {
1122 err = -ENOMEM; 1610 err = -ENOMEM;
1123 goto ERROR1; 1611 goto ERROR1;
1124 } 1612 }
@@ -1146,120 +1634,60 @@ static int __devinit it87_probe(struct platform_device *pdev)
1146 it87_init_device(pdev); 1634 it87_init_device(pdev);
1147 1635
1148 /* Register sysfs hooks */ 1636 /* Register sysfs hooks */
1149 if ((err = sysfs_create_group(&dev->kobj, &it87_group))) 1637 err = sysfs_create_group(&dev->kobj, &it87_group);
1638 if (err)
1150 goto ERROR2; 1639 goto ERROR2;
1151 1640
1641 if (sio_data->beep_pin) {
1642 err = sysfs_create_group(&dev->kobj, &it87_group_beep);
1643 if (err)
1644 goto ERROR4;
1645 }
1646
1152 /* Do not create fan files for disabled fans */ 1647 /* Do not create fan files for disabled fans */
1153 if (has_16bit_fans(data)) { 1648 fan_group = it87_get_fan_group(data);
1154 /* 16-bit tachometers */ 1649 fan_beep_need_rw = 1;
1155 if (data->has_fan & (1 << 0)) { 1650 for (i = 0; i < 5; i++) {
1156 if ((err = device_create_file(dev, 1651 if (!(data->has_fan & (1 << i)))
1157 &sensor_dev_attr_fan1_input16.dev_attr)) 1652 continue;
1158 || (err = device_create_file(dev, 1653 err = sysfs_create_group(&dev->kobj, &fan_group[i]);
1159 &sensor_dev_attr_fan1_min16.dev_attr)) 1654 if (err)
1160 || (err = device_create_file(dev, 1655 goto ERROR4;
1161 &sensor_dev_attr_fan1_alarm.dev_attr))) 1656
1162 goto ERROR4; 1657 if (sio_data->beep_pin) {
1163 } 1658 err = sysfs_create_file(&dev->kobj,
1164 if (data->has_fan & (1 << 1)) { 1659 it87_attributes_fan_beep[i]);
1165 if ((err = device_create_file(dev, 1660 if (err)
1166 &sensor_dev_attr_fan2_input16.dev_attr))
1167 || (err = device_create_file(dev,
1168 &sensor_dev_attr_fan2_min16.dev_attr))
1169 || (err = device_create_file(dev,
1170 &sensor_dev_attr_fan2_alarm.dev_attr)))
1171 goto ERROR4;
1172 }
1173 if (data->has_fan & (1 << 2)) {
1174 if ((err = device_create_file(dev,
1175 &sensor_dev_attr_fan3_input16.dev_attr))
1176 || (err = device_create_file(dev,
1177 &sensor_dev_attr_fan3_min16.dev_attr))
1178 || (err = device_create_file(dev,
1179 &sensor_dev_attr_fan3_alarm.dev_attr)))
1180 goto ERROR4;
1181 }
1182 if (data->has_fan & (1 << 3)) {
1183 if ((err = device_create_file(dev,
1184 &sensor_dev_attr_fan4_input16.dev_attr))
1185 || (err = device_create_file(dev,
1186 &sensor_dev_attr_fan4_min16.dev_attr))
1187 || (err = device_create_file(dev,
1188 &sensor_dev_attr_fan4_alarm.dev_attr)))
1189 goto ERROR4;
1190 }
1191 if (data->has_fan & (1 << 4)) {
1192 if ((err = device_create_file(dev,
1193 &sensor_dev_attr_fan5_input16.dev_attr))
1194 || (err = device_create_file(dev,
1195 &sensor_dev_attr_fan5_min16.dev_attr))
1196 || (err = device_create_file(dev,
1197 &sensor_dev_attr_fan5_alarm.dev_attr)))
1198 goto ERROR4;
1199 }
1200 } else {
1201 /* 8-bit tachometers with clock divider */
1202 if (data->has_fan & (1 << 0)) {
1203 if ((err = device_create_file(dev,
1204 &sensor_dev_attr_fan1_input.dev_attr))
1205 || (err = device_create_file(dev,
1206 &sensor_dev_attr_fan1_min.dev_attr))
1207 || (err = device_create_file(dev,
1208 &sensor_dev_attr_fan1_div.dev_attr))
1209 || (err = device_create_file(dev,
1210 &sensor_dev_attr_fan1_alarm.dev_attr)))
1211 goto ERROR4;
1212 }
1213 if (data->has_fan & (1 << 1)) {
1214 if ((err = device_create_file(dev,
1215 &sensor_dev_attr_fan2_input.dev_attr))
1216 || (err = device_create_file(dev,
1217 &sensor_dev_attr_fan2_min.dev_attr))
1218 || (err = device_create_file(dev,
1219 &sensor_dev_attr_fan2_div.dev_attr))
1220 || (err = device_create_file(dev,
1221 &sensor_dev_attr_fan2_alarm.dev_attr)))
1222 goto ERROR4;
1223 }
1224 if (data->has_fan & (1 << 2)) {
1225 if ((err = device_create_file(dev,
1226 &sensor_dev_attr_fan3_input.dev_attr))
1227 || (err = device_create_file(dev,
1228 &sensor_dev_attr_fan3_min.dev_attr))
1229 || (err = device_create_file(dev,
1230 &sensor_dev_attr_fan3_div.dev_attr))
1231 || (err = device_create_file(dev,
1232 &sensor_dev_attr_fan3_alarm.dev_attr)))
1233 goto ERROR4; 1661 goto ERROR4;
1662 if (!fan_beep_need_rw)
1663 continue;
1664
1665 /* As we have a single beep enable bit for all fans,
1666 * only the first enabled fan has a writable attribute
1667 * for it. */
1668 if (sysfs_chmod_file(&dev->kobj,
1669 it87_attributes_fan_beep[i],
1670 S_IRUGO | S_IWUSR))
1671 dev_dbg(dev, "chmod +w fan%d_beep failed\n",
1672 i + 1);
1673 fan_beep_need_rw = 0;
1234 } 1674 }
1235 } 1675 }
1236 1676
1237 if (enable_pwm_interface) { 1677 if (enable_pwm_interface) {
1238 if (!(sio_data->skip_pwm & (1 << 0))) { 1678 for (i = 0; i < 3; i++) {
1239 if ((err = device_create_file(dev, 1679 if (sio_data->skip_pwm & (1 << i))
1240 &sensor_dev_attr_pwm1_enable.dev_attr)) 1680 continue;
1241 || (err = device_create_file(dev, 1681 err = sysfs_create_group(&dev->kobj,
1242 &sensor_dev_attr_pwm1.dev_attr)) 1682 &it87_group_pwm[i]);
1243 || (err = device_create_file(dev, 1683 if (err)
1244 &dev_attr_pwm1_freq)))
1245 goto ERROR4;
1246 }
1247 if (!(sio_data->skip_pwm & (1 << 1))) {
1248 if ((err = device_create_file(dev,
1249 &sensor_dev_attr_pwm2_enable.dev_attr))
1250 || (err = device_create_file(dev,
1251 &sensor_dev_attr_pwm2.dev_attr))
1252 || (err = device_create_file(dev,
1253 &dev_attr_pwm2_freq)))
1254 goto ERROR4; 1684 goto ERROR4;
1255 } 1685
1256 if (!(sio_data->skip_pwm & (1 << 2))) { 1686 if (!has_old_autopwm(data))
1257 if ((err = device_create_file(dev, 1687 continue;
1258 &sensor_dev_attr_pwm3_enable.dev_attr)) 1688 err = sysfs_create_group(&dev->kobj,
1259 || (err = device_create_file(dev, 1689 &it87_group_autopwm[i]);
1260 &sensor_dev_attr_pwm3.dev_attr)) 1690 if (err)
1261 || (err = device_create_file(dev,
1262 &dev_attr_pwm3_freq)))
1263 goto ERROR4; 1691 goto ERROR4;
1264 } 1692 }
1265 } 1693 }
@@ -1268,10 +1696,8 @@ static int __devinit it87_probe(struct platform_device *pdev)
1268 data->vrm = vid_which_vrm(); 1696 data->vrm = vid_which_vrm();
1269 /* VID reading from Super-I/O config space if available */ 1697 /* VID reading from Super-I/O config space if available */
1270 data->vid = sio_data->vid_value; 1698 data->vid = sio_data->vid_value;
1271 if ((err = device_create_file(dev, 1699 err = sysfs_create_group(&dev->kobj, &it87_group_vid);
1272 &dev_attr_vrm)) 1700 if (err)
1273 || (err = device_create_file(dev,
1274 &dev_attr_cpu0_vid)))
1275 goto ERROR4; 1701 goto ERROR4;
1276 } 1702 }
1277 1703
@@ -1284,8 +1710,7 @@ static int __devinit it87_probe(struct platform_device *pdev)
1284 return 0; 1710 return 0;
1285 1711
1286ERROR4: 1712ERROR4:
1287 sysfs_remove_group(&dev->kobj, &it87_group); 1713 it87_remove_files(dev);
1288 sysfs_remove_group(&dev->kobj, &it87_group_opt);
1289ERROR2: 1714ERROR2:
1290 platform_set_drvdata(pdev, NULL); 1715 platform_set_drvdata(pdev, NULL);
1291 kfree(data); 1716 kfree(data);
@@ -1300,8 +1725,7 @@ static int __devexit it87_remove(struct platform_device *pdev)
1300 struct it87_data *data = platform_get_drvdata(pdev); 1725 struct it87_data *data = platform_get_drvdata(pdev);
1301 1726
1302 hwmon_device_unregister(data->hwmon_dev); 1727 hwmon_device_unregister(data->hwmon_dev);
1303 sysfs_remove_group(&pdev->dev.kobj, &it87_group); 1728 it87_remove_files(&pdev->dev);
1304 sysfs_remove_group(&pdev->dev.kobj, &it87_group_opt);
1305 1729
1306 release_region(data->addr, IT87_EC_EXTENT); 1730 release_region(data->addr, IT87_EC_EXTENT);
1307 platform_set_drvdata(pdev, NULL); 1731 platform_set_drvdata(pdev, NULL);
@@ -1387,15 +1811,18 @@ static void __devinit it87_init_device(struct platform_device *pdev)
1387 int tmp, i; 1811 int tmp, i;
1388 u8 mask; 1812 u8 mask;
1389 1813
1390 /* initialize to sane defaults: 1814 /* For each PWM channel:
1391 * - if the chip is in manual pwm mode, this will be overwritten with 1815 * - If it is in automatic mode, setting to manual mode should set
1392 * the actual settings on the chip (so in this case, initialization 1816 * the fan to full speed by default.
1393 * is not needed) 1817 * - If it is in manual mode, we need a mapping to temperature
1394 * - if in automatic or on/off mode, we could switch to manual mode, 1818 * channels to use when later setting to automatic mode later.
1395 * read the registers and set manual_pwm_ctl accordingly, but currently 1819 * Use a 1:1 mapping by default (we are clueless.)
1396 * this is not implemented, so we initialize to something sane */ 1820 * In both cases, the value can (and should) be changed by the user
1821 * prior to switching to a different mode. */
1397 for (i = 0; i < 3; i++) { 1822 for (i = 0; i < 3; i++) {
1398 data->manual_pwm_ctl[i] = 0xff; 1823 data->pwm_temp_map[i] = i;
1824 data->pwm_duty[i] = 0x7f; /* Full speed */
1825 data->auto_pwm[i][3] = 0x7f; /* Full speed, hard-coded */
1399 } 1826 }
1400 1827
1401 /* Some chips seem to have default value 0xff for all limit 1828 /* Some chips seem to have default value 0xff for all limit
@@ -1436,7 +1863,8 @@ static void __devinit it87_init_device(struct platform_device *pdev)
1436 if ((data->fan_main_ctrl & mask) == 0) { 1863 if ((data->fan_main_ctrl & mask) == 0) {
1437 /* Enable all fan tachometers */ 1864 /* Enable all fan tachometers */
1438 data->fan_main_ctrl |= mask; 1865 data->fan_main_ctrl |= mask;
1439 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl); 1866 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
1867 data->fan_main_ctrl);
1440 } 1868 }
1441 data->has_fan = (data->fan_main_ctrl >> 4) & 0x07; 1869 data->has_fan = (data->fan_main_ctrl >> 4) & 0x07;
1442 1870
@@ -1461,30 +1889,32 @@ static void __devinit it87_init_device(struct platform_device *pdev)
1461 /* Fan input pins may be used for alternative functions */ 1889 /* Fan input pins may be used for alternative functions */
1462 data->has_fan &= ~sio_data->skip_fan; 1890 data->has_fan &= ~sio_data->skip_fan;
1463 1891
1464 /* Set current fan mode registers and the default settings for the
1465 * other mode registers */
1466 for (i = 0; i < 3; i++) {
1467 if (data->fan_main_ctrl & (1 << i)) {
1468 /* pwm mode */
1469 tmp = it87_read_value(data, IT87_REG_PWM(i));
1470 if (tmp & 0x80) {
1471 /* automatic pwm - not yet implemented, but
1472 * leave the settings made by the BIOS alone
1473 * until a change is requested via the sysfs
1474 * interface */
1475 } else {
1476 /* manual pwm */
1477 data->manual_pwm_ctl[i] = PWM_FROM_REG(tmp);
1478 }
1479 }
1480 }
1481
1482 /* Start monitoring */ 1892 /* Start monitoring */
1483 it87_write_value(data, IT87_REG_CONFIG, 1893 it87_write_value(data, IT87_REG_CONFIG,
1484 (it87_read_value(data, IT87_REG_CONFIG) & 0x36) 1894 (it87_read_value(data, IT87_REG_CONFIG) & 0x36)
1485 | (update_vbat ? 0x41 : 0x01)); 1895 | (update_vbat ? 0x41 : 0x01));
1486} 1896}
1487 1897
1898static void it87_update_pwm_ctrl(struct it87_data *data, int nr)
1899{
1900 data->pwm_ctrl[nr] = it87_read_value(data, IT87_REG_PWM(nr));
1901 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
1902 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
1903 else /* Manual mode */
1904 data->pwm_duty[nr] = data->pwm_ctrl[nr] & 0x7f;
1905
1906 if (has_old_autopwm(data)) {
1907 int i;
1908
1909 for (i = 0; i < 5 ; i++)
1910 data->auto_temp[nr][i] = it87_read_value(data,
1911 IT87_REG_AUTO_TEMP(nr, i));
1912 for (i = 0; i < 3 ; i++)
1913 data->auto_pwm[nr][i] = it87_read_value(data,
1914 IT87_REG_AUTO_PWM(nr, i));
1915 }
1916}
1917
1488static struct it87_data *it87_update_device(struct device *dev) 1918static struct it87_data *it87_update_device(struct device *dev)
1489{ 1919{
1490 struct it87_data *data = dev_get_drvdata(dev); 1920 struct it87_data *data = dev_get_drvdata(dev);
@@ -1494,24 +1924,22 @@ static struct it87_data *it87_update_device(struct device *dev)
1494 1924
1495 if (time_after(jiffies, data->last_updated + HZ + HZ / 2) 1925 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
1496 || !data->valid) { 1926 || !data->valid) {
1497
1498 if (update_vbat) { 1927 if (update_vbat) {
1499 /* Cleared after each update, so reenable. Value 1928 /* Cleared after each update, so reenable. Value
1500 returned by this read will be previous value */ 1929 returned by this read will be previous value */
1501 it87_write_value(data, IT87_REG_CONFIG, 1930 it87_write_value(data, IT87_REG_CONFIG,
1502 it87_read_value(data, IT87_REG_CONFIG) | 0x40); 1931 it87_read_value(data, IT87_REG_CONFIG) | 0x40);
1503 } 1932 }
1504 for (i = 0; i <= 7; i++) { 1933 for (i = 0; i <= 7; i++) {
1505 data->in[i] = 1934 data->in[i] =
1506 it87_read_value(data, IT87_REG_VIN(i)); 1935 it87_read_value(data, IT87_REG_VIN(i));
1507 data->in_min[i] = 1936 data->in_min[i] =
1508 it87_read_value(data, IT87_REG_VIN_MIN(i)); 1937 it87_read_value(data, IT87_REG_VIN_MIN(i));
1509 data->in_max[i] = 1938 data->in_max[i] =
1510 it87_read_value(data, IT87_REG_VIN_MAX(i)); 1939 it87_read_value(data, IT87_REG_VIN_MAX(i));
1511 } 1940 }
1512 /* in8 (battery) has no limit registers */ 1941 /* in8 (battery) has no limit registers */
1513 data->in[8] = 1942 data->in[8] = it87_read_value(data, IT87_REG_VIN(8));
1514 it87_read_value(data, IT87_REG_VIN(8));
1515 1943
1516 for (i = 0; i < 5; i++) { 1944 for (i = 0; i < 5; i++) {
1517 /* Skip disabled fans */ 1945 /* Skip disabled fans */
@@ -1519,7 +1947,7 @@ static struct it87_data *it87_update_device(struct device *dev)
1519 continue; 1947 continue;
1520 1948
1521 data->fan_min[i] = 1949 data->fan_min[i] =
1522 it87_read_value(data, IT87_REG_FAN_MIN[i]); 1950 it87_read_value(data, IT87_REG_FAN_MIN[i]);
1523 data->fan[i] = it87_read_value(data, 1951 data->fan[i] = it87_read_value(data,
1524 IT87_REG_FAN[i]); 1952 IT87_REG_FAN[i]);
1525 /* Add high byte if in 16-bit mode */ 1953 /* Add high byte if in 16-bit mode */
@@ -1532,11 +1960,11 @@ static struct it87_data *it87_update_device(struct device *dev)
1532 } 1960 }
1533 for (i = 0; i < 3; i++) { 1961 for (i = 0; i < 3; i++) {
1534 data->temp[i] = 1962 data->temp[i] =
1535 it87_read_value(data, IT87_REG_TEMP(i)); 1963 it87_read_value(data, IT87_REG_TEMP(i));
1536 data->temp_high[i] = 1964 data->temp_high[i] =
1537 it87_read_value(data, IT87_REG_TEMP_HIGH(i)); 1965 it87_read_value(data, IT87_REG_TEMP_HIGH(i));
1538 data->temp_low[i] = 1966 data->temp_low[i] =
1539 it87_read_value(data, IT87_REG_TEMP_LOW(i)); 1967 it87_read_value(data, IT87_REG_TEMP_LOW(i));
1540 } 1968 }
1541 1969
1542 /* Newer chips don't have clock dividers */ 1970 /* Newer chips don't have clock dividers */
@@ -1551,9 +1979,13 @@ static struct it87_data *it87_update_device(struct device *dev)
1551 it87_read_value(data, IT87_REG_ALARM1) | 1979 it87_read_value(data, IT87_REG_ALARM1) |
1552 (it87_read_value(data, IT87_REG_ALARM2) << 8) | 1980 (it87_read_value(data, IT87_REG_ALARM2) << 8) |
1553 (it87_read_value(data, IT87_REG_ALARM3) << 16); 1981 (it87_read_value(data, IT87_REG_ALARM3) << 16);
1982 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
1983
1554 data->fan_main_ctrl = it87_read_value(data, 1984 data->fan_main_ctrl = it87_read_value(data,
1555 IT87_REG_FAN_MAIN_CTRL); 1985 IT87_REG_FAN_MAIN_CTRL);
1556 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL); 1986 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL);
1987 for (i = 0; i < 3; i++)
1988 it87_update_pwm_ctrl(data, i);
1557 1989
1558 data->sensor = it87_read_value(data, IT87_REG_TEMP_ENABLE); 1990 data->sensor = it87_read_value(data, IT87_REG_TEMP_ENABLE);
1559 /* The 8705 does not have VID capability. 1991 /* The 8705 does not have VID capability.
@@ -1628,7 +2060,7 @@ exit:
1628static int __init sm_it87_init(void) 2060static int __init sm_it87_init(void)
1629{ 2061{
1630 int err; 2062 int err;
1631 unsigned short isa_address=0; 2063 unsigned short isa_address = 0;
1632 struct it87_sio_data sio_data; 2064 struct it87_sio_data sio_data;
1633 2065
1634 memset(&sio_data, 0, sizeof(struct it87_sio_data)); 2066 memset(&sio_data, 0, sizeof(struct it87_sio_data));
@@ -1640,7 +2072,7 @@ static int __init sm_it87_init(void)
1640 return err; 2072 return err;
1641 2073
1642 err = it87_device_add(isa_address, &sio_data); 2074 err = it87_device_add(isa_address, &sio_data);
1643 if (err){ 2075 if (err) {
1644 platform_driver_unregister(&it87_driver); 2076 platform_driver_unregister(&it87_driver);
1645 return err; 2077 return err;
1646 } 2078 }
@@ -1661,7 +2093,8 @@ MODULE_DESCRIPTION("IT8705F/8712F/8716F/8718F/8720F/8726F, SiS950 driver");
1661module_param(update_vbat, bool, 0); 2093module_param(update_vbat, bool, 0);
1662MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value"); 2094MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
1663module_param(fix_pwm_polarity, bool, 0); 2095module_param(fix_pwm_polarity, bool, 0);
1664MODULE_PARM_DESC(fix_pwm_polarity, "Force PWM polarity to active high (DANGEROUS)"); 2096MODULE_PARM_DESC(fix_pwm_polarity,
2097 "Force PWM polarity to active high (DANGEROUS)");
1665MODULE_LICENSE("GPL"); 2098MODULE_LICENSE("GPL");
1666 2099
1667module_init(sm_it87_init); 2100module_init(sm_it87_init);
diff --git a/drivers/hwmon/lm90.c b/drivers/hwmon/lm90.c
index 7c9bdc167426..7cc2708871ab 100644
--- a/drivers/hwmon/lm90.c
+++ b/drivers/hwmon/lm90.c
@@ -1,7 +1,7 @@
1/* 1/*
2 * lm90.c - Part of lm_sensors, Linux kernel modules for hardware 2 * lm90.c - Part of lm_sensors, Linux kernel modules for hardware
3 * monitoring 3 * monitoring
4 * Copyright (C) 2003-2009 Jean Delvare <khali@linux-fr.org> 4 * Copyright (C) 2003-2010 Jean Delvare <khali@linux-fr.org>
5 * 5 *
6 * Based on the lm83 driver. The LM90 is a sensor chip made by National 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 7 * Semiconductor. It reports up to two temperatures (its own plus up to
@@ -93,7 +93,8 @@
93static const unsigned short normal_i2c[] = { 93static const unsigned short normal_i2c[] = {
94 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x4c, 0x4d, 0x4e, I2C_CLIENT_END }; 94 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x4c, 0x4d, 0x4e, I2C_CLIENT_END };
95 95
96enum chips { lm90, adm1032, lm99, lm86, max6657, adt7461, max6680, max6646 }; 96enum chips { lm90, adm1032, lm99, lm86, max6657, adt7461, max6680, max6646,
97 w83l771 };
97 98
98/* 99/*
99 * The LM90 registers 100 * The LM90 registers
@@ -151,6 +152,7 @@ static int lm90_detect(struct i2c_client *client, struct i2c_board_info *info);
151static int lm90_probe(struct i2c_client *client, 152static int lm90_probe(struct i2c_client *client,
152 const struct i2c_device_id *id); 153 const struct i2c_device_id *id);
153static void lm90_init_client(struct i2c_client *client); 154static void lm90_init_client(struct i2c_client *client);
155static void lm90_alert(struct i2c_client *client, unsigned int flag);
154static int lm90_remove(struct i2c_client *client); 156static int lm90_remove(struct i2c_client *client);
155static struct lm90_data *lm90_update_device(struct device *dev); 157static struct lm90_data *lm90_update_device(struct device *dev);
156 158
@@ -173,6 +175,7 @@ static const struct i2c_device_id lm90_id[] = {
173 { "max6659", max6657 }, 175 { "max6659", max6657 },
174 { "max6680", max6680 }, 176 { "max6680", max6680 },
175 { "max6681", max6680 }, 177 { "max6681", max6680 },
178 { "w83l771", w83l771 },
176 { } 179 { }
177}; 180};
178MODULE_DEVICE_TABLE(i2c, lm90_id); 181MODULE_DEVICE_TABLE(i2c, lm90_id);
@@ -184,6 +187,7 @@ static struct i2c_driver lm90_driver = {
184 }, 187 },
185 .probe = lm90_probe, 188 .probe = lm90_probe,
186 .remove = lm90_remove, 189 .remove = lm90_remove,
190 .alert = lm90_alert,
187 .id_table = lm90_id, 191 .id_table = lm90_id,
188 .detect = lm90_detect, 192 .detect = lm90_detect,
189 .address_list = normal_i2c, 193 .address_list = normal_i2c,
@@ -201,6 +205,9 @@ struct lm90_data {
201 int kind; 205 int kind;
202 int flags; 206 int flags;
203 207
208 u8 config_orig; /* Original configuration register value */
209 u8 alert_alarms; /* Which alarm bits trigger ALERT# */
210
204 /* registers values */ 211 /* registers values */
205 s8 temp8[4]; /* 0: local low limit 212 s8 temp8[4]; /* 0: local low limit
206 1: local high limit 213 1: local high limit
@@ -758,6 +765,14 @@ static int lm90_detect(struct i2c_client *new_client,
758 && reg_convrate <= 0x07) { 765 && reg_convrate <= 0x07) {
759 name = "max6646"; 766 name = "max6646";
760 } 767 }
768 } else
769 if (address == 0x4C
770 && man_id == 0x5C) { /* Winbond/Nuvoton */
771 if ((chip_id & 0xFE) == 0x10 /* W83L771AWG/ASG */
772 && (reg_config1 & 0x2A) == 0x00
773 && reg_convrate <= 0x08) {
774 name = "w83l771";
775 }
761 } 776 }
762 777
763 if (!name) { /* identification failed */ 778 if (!name) { /* identification failed */
@@ -794,6 +809,19 @@ static int lm90_probe(struct i2c_client *new_client,
794 new_client->flags &= ~I2C_CLIENT_PEC; 809 new_client->flags &= ~I2C_CLIENT_PEC;
795 } 810 }
796 811
812 /* Different devices have different alarm bits triggering the
813 * ALERT# output */
814 switch (data->kind) {
815 case lm90:
816 case lm99:
817 case lm86:
818 data->alert_alarms = 0x7b;
819 break;
820 default:
821 data->alert_alarms = 0x7c;
822 break;
823 }
824
797 /* Initialize the LM90 chip */ 825 /* Initialize the LM90 chip */
798 lm90_init_client(new_client); 826 lm90_init_client(new_client);
799 827
@@ -830,7 +858,7 @@ exit:
830 858
831static void lm90_init_client(struct i2c_client *client) 859static void lm90_init_client(struct i2c_client *client)
832{ 860{
833 u8 config, config_orig; 861 u8 config;
834 struct lm90_data *data = i2c_get_clientdata(client); 862 struct lm90_data *data = i2c_get_clientdata(client);
835 863
836 /* 864 /*
@@ -842,7 +870,7 @@ static void lm90_init_client(struct i2c_client *client)
842 dev_warn(&client->dev, "Initialization failed!\n"); 870 dev_warn(&client->dev, "Initialization failed!\n");
843 return; 871 return;
844 } 872 }
845 config_orig = config; 873 data->config_orig = config;
846 874
847 /* Check Temperature Range Select */ 875 /* Check Temperature Range Select */
848 if (data->kind == adt7461) { 876 if (data->kind == adt7461) {
@@ -860,7 +888,7 @@ static void lm90_init_client(struct i2c_client *client)
860 } 888 }
861 889
862 config &= 0xBF; /* run */ 890 config &= 0xBF; /* run */
863 if (config != config_orig) /* Only write if changed */ 891 if (config != data->config_orig) /* Only write if changed */
864 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1, config); 892 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1, config);
865} 893}
866 894
@@ -875,10 +903,46 @@ static int lm90_remove(struct i2c_client *client)
875 device_remove_file(&client->dev, 903 device_remove_file(&client->dev,
876 &sensor_dev_attr_temp2_offset.dev_attr); 904 &sensor_dev_attr_temp2_offset.dev_attr);
877 905
906 /* Restore initial configuration */
907 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
908 data->config_orig);
909
878 kfree(data); 910 kfree(data);
879 return 0; 911 return 0;
880} 912}
881 913
914static void lm90_alert(struct i2c_client *client, unsigned int flag)
915{
916 struct lm90_data *data = i2c_get_clientdata(client);
917 u8 config, alarms;
918
919 lm90_read_reg(client, LM90_REG_R_STATUS, &alarms);
920 if ((alarms & 0x7f) == 0) {
921 dev_info(&client->dev, "Everything OK\n");
922 } else {
923 if (alarms & 0x61)
924 dev_warn(&client->dev,
925 "temp%d out of range, please check!\n", 1);
926 if (alarms & 0x1a)
927 dev_warn(&client->dev,
928 "temp%d out of range, please check!\n", 2);
929 if (alarms & 0x04)
930 dev_warn(&client->dev,
931 "temp%d diode open, please check!\n", 2);
932
933 /* Disable ALERT# output, because these chips don't implement
934 SMBus alert correctly; they should only hold the alert line
935 low briefly. */
936 if ((data->kind == adm1032 || data->kind == adt7461)
937 && (alarms & data->alert_alarms)) {
938 dev_dbg(&client->dev, "Disabling ALERT#\n");
939 lm90_read_reg(client, LM90_REG_R_CONFIG1, &config);
940 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
941 config | 0x80);
942 }
943 }
944}
945
882static int lm90_read16(struct i2c_client *client, u8 regh, u8 regl, u16 *value) 946static int lm90_read16(struct i2c_client *client, u8 regh, u8 regl, u16 *value)
883{ 947{
884 int err; 948 int err;
@@ -966,6 +1030,21 @@ static struct lm90_data *lm90_update_device(struct device *dev)
966 } 1030 }
967 lm90_read_reg(client, LM90_REG_R_STATUS, &data->alarms); 1031 lm90_read_reg(client, LM90_REG_R_STATUS, &data->alarms);
968 1032
1033 /* Re-enable ALERT# output if it was originally enabled and
1034 * relevant alarms are all clear */
1035 if ((data->config_orig & 0x80) == 0
1036 && (data->alarms & data->alert_alarms) == 0) {
1037 u8 config;
1038
1039 lm90_read_reg(client, LM90_REG_R_CONFIG1, &config);
1040 if (config & 0x80) {
1041 dev_dbg(&client->dev, "Re-enabling ALERT#\n");
1042 i2c_smbus_write_byte_data(client,
1043 LM90_REG_W_CONFIG1,
1044 config & ~0x80);
1045 }
1046 }
1047
969 data->last_updated = jiffies; 1048 data->last_updated = jiffies;
970 data->valid = 1; 1049 data->valid = 1;
971 } 1050 }
diff --git a/drivers/hwmon/tmp401.c b/drivers/hwmon/tmp401.c
index a13b30e8d8d8..d14a1af9f550 100644
--- a/drivers/hwmon/tmp401.c
+++ b/drivers/hwmon/tmp401.c
@@ -134,7 +134,7 @@ struct tmp401_data {
134 struct mutex update_lock; 134 struct mutex update_lock;
135 char valid; /* zero until following fields are valid */ 135 char valid; /* zero until following fields are valid */
136 unsigned long last_updated; /* in jiffies */ 136 unsigned long last_updated; /* in jiffies */
137 int kind; 137 enum chips kind;
138 138
139 /* register values */ 139 /* register values */
140 u8 status; 140 u8 status;
@@ -524,7 +524,7 @@ static int tmp401_detect(struct i2c_client *client,
524 if (reg > 15) 524 if (reg > 15)
525 return -ENODEV; 525 return -ENODEV;
526 526
527 strlcpy(info->type, tmp401_id[kind - 1].name, I2C_NAME_SIZE); 527 strlcpy(info->type, tmp401_id[kind].name, I2C_NAME_SIZE);
528 528
529 return 0; 529 return 0;
530} 530}
@@ -572,8 +572,7 @@ static int tmp401_probe(struct i2c_client *client,
572 goto exit_remove; 572 goto exit_remove;
573 } 573 }
574 574
575 dev_info(&client->dev, "Detected TI %s chip\n", 575 dev_info(&client->dev, "Detected TI %s chip\n", names[data->kind]);
576 names[data->kind - 1]);
577 576
578 return 0; 577 return 0;
579 578
diff --git a/drivers/hwmon/tmp421.c b/drivers/hwmon/tmp421.c
index 4f7c051e2d7b..738c472ece27 100644
--- a/drivers/hwmon/tmp421.c
+++ b/drivers/hwmon/tmp421.c
@@ -61,9 +61,9 @@ static const u8 TMP421_TEMP_LSB[4] = { 0x10, 0x11, 0x12, 0x13 };
61#define TMP423_DEVICE_ID 0x23 61#define TMP423_DEVICE_ID 0x23
62 62
63static const struct i2c_device_id tmp421_id[] = { 63static const struct i2c_device_id tmp421_id[] = {
64 { "tmp421", tmp421 }, 64 { "tmp421", 2 },
65 { "tmp422", tmp422 }, 65 { "tmp422", 3 },
66 { "tmp423", tmp423 }, 66 { "tmp423", 4 },
67 { } 67 { }
68}; 68};
69MODULE_DEVICE_TABLE(i2c, tmp421_id); 69MODULE_DEVICE_TABLE(i2c, tmp421_id);
@@ -73,21 +73,23 @@ struct tmp421_data {
73 struct mutex update_lock; 73 struct mutex update_lock;
74 char valid; 74 char valid;
75 unsigned long last_updated; 75 unsigned long last_updated;
76 int kind; 76 int channels;
77 u8 config; 77 u8 config;
78 s16 temp[4]; 78 s16 temp[4];
79}; 79};
80 80
81static int temp_from_s16(s16 reg) 81static int temp_from_s16(s16 reg)
82{ 82{
83 int temp = reg; 83 /* Mask out status bits */
84 int temp = reg & ~0xf;
84 85
85 return (temp * 1000 + 128) / 256; 86 return (temp * 1000 + 128) / 256;
86} 87}
87 88
88static int temp_from_u16(u16 reg) 89static int temp_from_u16(u16 reg)
89{ 90{
90 int temp = reg; 91 /* Mask out status bits */
92 int temp = reg & ~0xf;
91 93
92 /* Add offset for extended temperature range. */ 94 /* Add offset for extended temperature range. */
93 temp -= 64 * 256; 95 temp -= 64 * 256;
@@ -107,7 +109,7 @@ static struct tmp421_data *tmp421_update_device(struct device *dev)
107 data->config = i2c_smbus_read_byte_data(client, 109 data->config = i2c_smbus_read_byte_data(client,
108 TMP421_CONFIG_REG_1); 110 TMP421_CONFIG_REG_1);
109 111
110 for (i = 0; i <= data->kind; i++) { 112 for (i = 0; i < data->channels; i++) {
111 data->temp[i] = i2c_smbus_read_byte_data(client, 113 data->temp[i] = i2c_smbus_read_byte_data(client,
112 TMP421_TEMP_MSB[i]) << 8; 114 TMP421_TEMP_MSB[i]) << 8;
113 data->temp[i] |= i2c_smbus_read_byte_data(client, 115 data->temp[i] |= i2c_smbus_read_byte_data(client,
@@ -166,7 +168,7 @@ static mode_t tmp421_is_visible(struct kobject *kobj, struct attribute *a,
166 devattr = container_of(a, struct device_attribute, attr); 168 devattr = container_of(a, struct device_attribute, attr);
167 index = to_sensor_dev_attr(devattr)->index; 169 index = to_sensor_dev_attr(devattr)->index;
168 170
169 if (data->kind > index) 171 if (index < data->channels)
170 return a->mode; 172 return a->mode;
171 173
172 return 0; 174 return 0;
@@ -252,9 +254,9 @@ static int tmp421_detect(struct i2c_client *client,
252 return -ENODEV; 254 return -ENODEV;
253 } 255 }
254 256
255 strlcpy(info->type, tmp421_id[kind - 1].name, I2C_NAME_SIZE); 257 strlcpy(info->type, tmp421_id[kind].name, I2C_NAME_SIZE);
256 dev_info(&adapter->dev, "Detected TI %s chip at 0x%02x\n", 258 dev_info(&adapter->dev, "Detected TI %s chip at 0x%02x\n",
257 names[kind - 1], client->addr); 259 names[kind], client->addr);
258 260
259 return 0; 261 return 0;
260} 262}
@@ -271,7 +273,7 @@ static int tmp421_probe(struct i2c_client *client,
271 273
272 i2c_set_clientdata(client, data); 274 i2c_set_clientdata(client, data);
273 mutex_init(&data->update_lock); 275 mutex_init(&data->update_lock);
274 data->kind = id->driver_data; 276 data->channels = id->driver_data;
275 277
276 err = tmp421_init_client(client); 278 err = tmp421_init_client(client);
277 if (err) 279 if (err)
diff --git a/drivers/hwmon/vt8231.c b/drivers/hwmon/vt8231.c
index d47b4c9949c2..e6078c9f0e27 100644
--- a/drivers/hwmon/vt8231.c
+++ b/drivers/hwmon/vt8231.c
@@ -948,8 +948,7 @@ static int __devinit vt8231_pci_probe(struct pci_dev *dev,
948 948
949 address = val & ~(VT8231_EXTENT - 1); 949 address = val & ~(VT8231_EXTENT - 1);
950 if (address == 0) { 950 if (address == 0) {
951 dev_err(&dev->dev, "base address not set -\ 951 dev_err(&dev->dev, "base address not set - upgrade BIOS or use force_addr=0xaddr\n");
952 upgrade BIOS or use force_addr=0xaddr\n");
953 return -ENODEV; 952 return -ENODEV;
954 } 953 }
955 954
diff --git a/drivers/hwmon/w83793.c b/drivers/hwmon/w83793.c
index 9a2022b67495..9de81a4c15a2 100644
--- a/drivers/hwmon/w83793.c
+++ b/drivers/hwmon/w83793.c
@@ -3,6 +3,10 @@
3 Copyright (C) 2006 Winbond Electronics Corp. 3 Copyright (C) 2006 Winbond Electronics Corp.
4 Yuan Mu 4 Yuan Mu
5 Rudolf Marek <r.marek@assembler.cz> 5 Rudolf Marek <r.marek@assembler.cz>
6 Copyright (C) 2009-2010 Sven Anders <anders@anduras.de>, ANDURAS AG.
7 Watchdog driver part
8 (Based partially on fschmd driver,
9 Copyright 2007-2008 by Hans de Goede)
6 10
7 This program is free software; you can redistribute it and/or modify 11 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 12 it under the terms of the GNU General Public License as published by
@@ -35,6 +39,16 @@
35#include <linux/hwmon-sysfs.h> 39#include <linux/hwmon-sysfs.h>
36#include <linux/err.h> 40#include <linux/err.h>
37#include <linux/mutex.h> 41#include <linux/mutex.h>
42#include <linux/fs.h>
43#include <linux/watchdog.h>
44#include <linux/miscdevice.h>
45#include <linux/uaccess.h>
46#include <linux/kref.h>
47#include <linux/notifier.h>
48#include <linux/reboot.h>
49
50/* Default values */
51#define WATCHDOG_TIMEOUT 2 /* 2 minute default timeout */
38 52
39/* Addresses to scan */ 53/* Addresses to scan */
40static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, 0x2f, 54static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, 0x2f,
@@ -51,6 +65,18 @@ static int reset;
51module_param(reset, bool, 0); 65module_param(reset, bool, 0);
52MODULE_PARM_DESC(reset, "Set to 1 to reset chip, not recommended"); 66MODULE_PARM_DESC(reset, "Set to 1 to reset chip, not recommended");
53 67
68static int timeout = WATCHDOG_TIMEOUT; /* default timeout in minutes */
69module_param(timeout, int, 0);
70MODULE_PARM_DESC(timeout,
71 "Watchdog timeout in minutes. 2<= timeout <=255 (default="
72 __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
73
74static int nowayout = WATCHDOG_NOWAYOUT;
75module_param(nowayout, int, 0);
76MODULE_PARM_DESC(nowayout,
77 "Watchdog cannot be stopped once started (default="
78 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
79
54/* 80/*
55 Address 0x00, 0x0d, 0x0e, 0x0f in all three banks are reserved 81 Address 0x00, 0x0d, 0x0e, 0x0f in all three banks are reserved
56 as ID, Bank Select registers 82 as ID, Bank Select registers
@@ -72,6 +98,11 @@ MODULE_PARM_DESC(reset, "Set to 1 to reset chip, not recommended");
72#define W83793_REG_VID_LATCHB 0x08 98#define W83793_REG_VID_LATCHB 0x08
73#define W83793_REG_VID_CTRL 0x59 99#define W83793_REG_VID_CTRL 0x59
74 100
101#define W83793_REG_WDT_LOCK 0x01
102#define W83793_REG_WDT_ENABLE 0x02
103#define W83793_REG_WDT_STATUS 0x03
104#define W83793_REG_WDT_TIMEOUT 0x04
105
75static u16 W83793_REG_TEMP_MODE[2] = { 0x5e, 0x5f }; 106static u16 W83793_REG_TEMP_MODE[2] = { 0x5e, 0x5f };
76 107
77#define TEMP_READ 0 108#define TEMP_READ 0
@@ -223,8 +254,37 @@ struct w83793_data {
223 u8 tolerance[3]; /* Temp tolerance(Smart Fan I/II) */ 254 u8 tolerance[3]; /* Temp tolerance(Smart Fan I/II) */
224 u8 sf2_pwm[6][7]; /* Smart FanII: Fan duty cycle */ 255 u8 sf2_pwm[6][7]; /* Smart FanII: Fan duty cycle */
225 u8 sf2_temp[6][7]; /* Smart FanII: Temp level point */ 256 u8 sf2_temp[6][7]; /* Smart FanII: Temp level point */
257
258 /* watchdog */
259 struct i2c_client *client;
260 struct mutex watchdog_lock;
261 struct list_head list; /* member of the watchdog_data_list */
262 struct kref kref;
263 struct miscdevice watchdog_miscdev;
264 unsigned long watchdog_is_open;
265 char watchdog_expect_close;
266 char watchdog_name[10]; /* must be unique to avoid sysfs conflict */
267 unsigned int watchdog_caused_reboot;
268 int watchdog_timeout; /* watchdog timeout in minutes */
226}; 269};
227 270
271/* Somewhat ugly :( global data pointer list with all devices, so that
272 we can find our device data as when using misc_register. There is no
273 other method to get to one's device data from the open file-op and
274 for usage in the reboot notifier callback. */
275static LIST_HEAD(watchdog_data_list);
276
277/* Note this lock not only protect list access, but also data.kref access */
278static DEFINE_MUTEX(watchdog_data_mutex);
279
280/* Release our data struct when we're detached from the i2c client *and* all
281 references to our watchdog device are released */
282static void w83793_release_resources(struct kref *ref)
283{
284 struct w83793_data *data = container_of(ref, struct w83793_data, kref);
285 kfree(data);
286}
287
228static u8 w83793_read_value(struct i2c_client *client, u16 reg); 288static u8 w83793_read_value(struct i2c_client *client, u16 reg);
229static int w83793_write_value(struct i2c_client *client, u16 reg, u8 value); 289static int w83793_write_value(struct i2c_client *client, u16 reg, u8 value);
230static int w83793_probe(struct i2c_client *client, 290static int w83793_probe(struct i2c_client *client,
@@ -1063,14 +1123,349 @@ static void w83793_init_client(struct i2c_client *client)
1063 /* Start monitoring */ 1123 /* Start monitoring */
1064 w83793_write_value(client, W83793_REG_CONFIG, 1124 w83793_write_value(client, W83793_REG_CONFIG,
1065 w83793_read_value(client, W83793_REG_CONFIG) | 0x01); 1125 w83793_read_value(client, W83793_REG_CONFIG) | 0x01);
1126}
1127
1128/*
1129 * Watchdog routines
1130 */
1131
1132static int watchdog_set_timeout(struct w83793_data *data, int timeout)
1133{
1134 int ret, mtimeout;
1135
1136 mtimeout = DIV_ROUND_UP(timeout, 60);
1137
1138 if (mtimeout > 255)
1139 return -EINVAL;
1140
1141 mutex_lock(&data->watchdog_lock);
1142 if (!data->client) {
1143 ret = -ENODEV;
1144 goto leave;
1145 }
1146
1147 data->watchdog_timeout = mtimeout;
1148
1149 /* Set Timeout value (in Minutes) */
1150 w83793_write_value(data->client, W83793_REG_WDT_TIMEOUT,
1151 data->watchdog_timeout);
1152
1153 ret = mtimeout * 60;
1154
1155leave:
1156 mutex_unlock(&data->watchdog_lock);
1157 return ret;
1158}
1159
1160static int watchdog_get_timeout(struct w83793_data *data)
1161{
1162 int timeout;
1163
1164 mutex_lock(&data->watchdog_lock);
1165 timeout = data->watchdog_timeout * 60;
1166 mutex_unlock(&data->watchdog_lock);
1167
1168 return timeout;
1169}
1170
1171static int watchdog_trigger(struct w83793_data *data)
1172{
1173 int ret = 0;
1174
1175 mutex_lock(&data->watchdog_lock);
1176 if (!data->client) {
1177 ret = -ENODEV;
1178 goto leave;
1179 }
1180
1181 /* Set Timeout value (in Minutes) */
1182 w83793_write_value(data->client, W83793_REG_WDT_TIMEOUT,
1183 data->watchdog_timeout);
1184
1185leave:
1186 mutex_unlock(&data->watchdog_lock);
1187 return ret;
1188}
1189
1190static int watchdog_enable(struct w83793_data *data)
1191{
1192 int ret = 0;
1193
1194 mutex_lock(&data->watchdog_lock);
1195 if (!data->client) {
1196 ret = -ENODEV;
1197 goto leave;
1198 }
1199
1200 /* Set initial timeout */
1201 w83793_write_value(data->client, W83793_REG_WDT_TIMEOUT,
1202 data->watchdog_timeout);
1203
1204 /* Enable Soft Watchdog */
1205 w83793_write_value(data->client, W83793_REG_WDT_LOCK, 0x55);
1206
1207leave:
1208 mutex_unlock(&data->watchdog_lock);
1209 return ret;
1210}
1211
1212static int watchdog_disable(struct w83793_data *data)
1213{
1214 int ret = 0;
1215
1216 mutex_lock(&data->watchdog_lock);
1217 if (!data->client) {
1218 ret = -ENODEV;
1219 goto leave;
1220 }
1221
1222 /* Disable Soft Watchdog */
1223 w83793_write_value(data->client, W83793_REG_WDT_LOCK, 0xAA);
1224
1225leave:
1226 mutex_unlock(&data->watchdog_lock);
1227 return ret;
1228}
1229
1230static int watchdog_open(struct inode *inode, struct file *filp)
1231{
1232 struct w83793_data *pos, *data = NULL;
1233 int watchdog_is_open;
1234
1235 /* We get called from drivers/char/misc.c with misc_mtx hold, and we
1236 call misc_register() from w83793_probe() with watchdog_data_mutex
1237 hold, as misc_register() takes the misc_mtx lock, this is a possible
1238 deadlock, so we use mutex_trylock here. */
1239 if (!mutex_trylock(&watchdog_data_mutex))
1240 return -ERESTARTSYS;
1241 list_for_each_entry(pos, &watchdog_data_list, list) {
1242 if (pos->watchdog_miscdev.minor == iminor(inode)) {
1243 data = pos;
1244 break;
1245 }
1246 }
1247
1248 /* Check, if device is already open */
1249 watchdog_is_open = test_and_set_bit(0, &data->watchdog_is_open);
1250
1251 /* Increase data reference counter (if not already done).
1252 Note we can never not have found data, so we don't check for this */
1253 if (!watchdog_is_open)
1254 kref_get(&data->kref);
1255
1256 mutex_unlock(&watchdog_data_mutex);
1257
1258 /* Check, if device is already open and possibly issue error */
1259 if (watchdog_is_open)
1260 return -EBUSY;
1261
1262 /* Enable Soft Watchdog */
1263 watchdog_enable(data);
1264
1265 /* Store pointer to data into filp's private data */
1266 filp->private_data = data;
1267
1268 return nonseekable_open(inode, filp);
1269}
1270
1271static int watchdog_close(struct inode *inode, struct file *filp)
1272{
1273 struct w83793_data *data = filp->private_data;
1066 1274
1275 if (data->watchdog_expect_close) {
1276 watchdog_disable(data);
1277 data->watchdog_expect_close = 0;
1278 } else {
1279 watchdog_trigger(data);
1280 dev_crit(&data->client->dev,
1281 "unexpected close, not stopping watchdog!\n");
1282 }
1283
1284 clear_bit(0, &data->watchdog_is_open);
1285
1286 /* Decrease data reference counter */
1287 mutex_lock(&watchdog_data_mutex);
1288 kref_put(&data->kref, w83793_release_resources);
1289 mutex_unlock(&watchdog_data_mutex);
1290
1291 return 0;
1292}
1293
1294static ssize_t watchdog_write(struct file *filp, const char __user *buf,
1295 size_t count, loff_t *offset)
1296{
1297 size_t ret;
1298 struct w83793_data *data = filp->private_data;
1299
1300 if (count) {
1301 if (!nowayout) {
1302 size_t i;
1303
1304 /* Clear it in case it was set with a previous write */
1305 data->watchdog_expect_close = 0;
1306
1307 for (i = 0; i != count; i++) {
1308 char c;
1309 if (get_user(c, buf + i))
1310 return -EFAULT;
1311 if (c == 'V')
1312 data->watchdog_expect_close = 1;
1313 }
1314 }
1315 ret = watchdog_trigger(data);
1316 if (ret < 0)
1317 return ret;
1318 }
1319 return count;
1320}
1321
1322static int watchdog_ioctl(struct inode *inode, struct file *filp,
1323 unsigned int cmd, unsigned long arg)
1324{
1325 static struct watchdog_info ident = {
1326 .options = WDIOF_KEEPALIVEPING |
1327 WDIOF_SETTIMEOUT |
1328 WDIOF_CARDRESET,
1329 .identity = "w83793 watchdog"
1330 };
1331
1332 int val, ret = 0;
1333 struct w83793_data *data = filp->private_data;
1334
1335 switch (cmd) {
1336 case WDIOC_GETSUPPORT:
1337 if (!nowayout)
1338 ident.options |= WDIOF_MAGICCLOSE;
1339 if (copy_to_user((void __user *)arg, &ident, sizeof(ident)))
1340 ret = -EFAULT;
1341 break;
1342
1343 case WDIOC_GETSTATUS:
1344 val = data->watchdog_caused_reboot ? WDIOF_CARDRESET : 0;
1345 ret = put_user(val, (int __user *)arg);
1346 break;
1347
1348 case WDIOC_GETBOOTSTATUS:
1349 ret = put_user(0, (int __user *)arg);
1350 break;
1351
1352 case WDIOC_KEEPALIVE:
1353 ret = watchdog_trigger(data);
1354 break;
1355
1356 case WDIOC_GETTIMEOUT:
1357 val = watchdog_get_timeout(data);
1358 ret = put_user(val, (int __user *)arg);
1359 break;
1360
1361 case WDIOC_SETTIMEOUT:
1362 if (get_user(val, (int __user *)arg)) {
1363 ret = -EFAULT;
1364 break;
1365 }
1366 ret = watchdog_set_timeout(data, val);
1367 if (ret > 0)
1368 ret = put_user(ret, (int __user *)arg);
1369 break;
1370
1371 case WDIOC_SETOPTIONS:
1372 if (get_user(val, (int __user *)arg)) {
1373 ret = -EFAULT;
1374 break;
1375 }
1376
1377 if (val & WDIOS_DISABLECARD)
1378 ret = watchdog_disable(data);
1379 else if (val & WDIOS_ENABLECARD)
1380 ret = watchdog_enable(data);
1381 else
1382 ret = -EINVAL;
1383
1384 break;
1385 default:
1386 ret = -ENOTTY;
1387 }
1388
1389 return ret;
1390}
1391
1392static const struct file_operations watchdog_fops = {
1393 .owner = THIS_MODULE,
1394 .llseek = no_llseek,
1395 .open = watchdog_open,
1396 .release = watchdog_close,
1397 .write = watchdog_write,
1398 .ioctl = watchdog_ioctl,
1399};
1400
1401/*
1402 * Notifier for system down
1403 */
1404
1405static int watchdog_notify_sys(struct notifier_block *this, unsigned long code,
1406 void *unused)
1407{
1408 struct w83793_data *data = NULL;
1409
1410 if (code == SYS_DOWN || code == SYS_HALT) {
1411
1412 /* Disable each registered watchdog */
1413 mutex_lock(&watchdog_data_mutex);
1414 list_for_each_entry(data, &watchdog_data_list, list) {
1415 if (data->watchdog_miscdev.minor)
1416 watchdog_disable(data);
1417 }
1418 mutex_unlock(&watchdog_data_mutex);
1419 }
1420
1421 return NOTIFY_DONE;
1067} 1422}
1068 1423
1424/*
1425 * The WDT needs to learn about soft shutdowns in order to
1426 * turn the timebomb registers off.
1427 */
1428
1429static struct notifier_block watchdog_notifier = {
1430 .notifier_call = watchdog_notify_sys,
1431};
1432
1433/*
1434 * Init / remove routines
1435 */
1436
1069static int w83793_remove(struct i2c_client *client) 1437static int w83793_remove(struct i2c_client *client)
1070{ 1438{
1071 struct w83793_data *data = i2c_get_clientdata(client); 1439 struct w83793_data *data = i2c_get_clientdata(client);
1072 struct device *dev = &client->dev; 1440 struct device *dev = &client->dev;
1073 int i; 1441 int i, tmp;
1442
1443 /* Unregister the watchdog (if registered) */
1444 if (data->watchdog_miscdev.minor) {
1445 misc_deregister(&data->watchdog_miscdev);
1446
1447 if (data->watchdog_is_open) {
1448 dev_warn(&client->dev,
1449 "i2c client detached with watchdog open! "
1450 "Stopping watchdog.\n");
1451 watchdog_disable(data);
1452 }
1453
1454 mutex_lock(&watchdog_data_mutex);
1455 list_del(&data->list);
1456 mutex_unlock(&watchdog_data_mutex);
1457
1458 /* Tell the watchdog code the client is gone */
1459 mutex_lock(&data->watchdog_lock);
1460 data->client = NULL;
1461 mutex_unlock(&data->watchdog_lock);
1462 }
1463
1464 /* Reset Configuration Register to Disable Watch Dog Registers */
1465 tmp = w83793_read_value(client, W83793_REG_CONFIG);
1466 w83793_write_value(client, W83793_REG_CONFIG, tmp & ~0x04);
1467
1468 unregister_reboot_notifier(&watchdog_notifier);
1074 1469
1075 hwmon_device_unregister(data->hwmon_dev); 1470 hwmon_device_unregister(data->hwmon_dev);
1076 1471
@@ -1099,7 +1494,10 @@ static int w83793_remove(struct i2c_client *client)
1099 if (data->lm75[1] != NULL) 1494 if (data->lm75[1] != NULL)
1100 i2c_unregister_device(data->lm75[1]); 1495 i2c_unregister_device(data->lm75[1]);
1101 1496
1102 kfree(data); 1497 /* Decrease data reference counter */
1498 mutex_lock(&watchdog_data_mutex);
1499 kref_put(&data->kref, w83793_release_resources);
1500 mutex_unlock(&watchdog_data_mutex);
1103 1501
1104 return 0; 1502 return 0;
1105} 1503}
@@ -1203,6 +1601,7 @@ static int w83793_probe(struct i2c_client *client,
1203 const struct i2c_device_id *id) 1601 const struct i2c_device_id *id)
1204{ 1602{
1205 struct device *dev = &client->dev; 1603 struct device *dev = &client->dev;
1604 const int watchdog_minors[] = { WATCHDOG_MINOR, 212, 213, 214, 215 };
1206 struct w83793_data *data; 1605 struct w83793_data *data;
1207 int i, tmp, val, err; 1606 int i, tmp, val, err;
1208 int files_fan = ARRAY_SIZE(w83793_left_fan) / 7; 1607 int files_fan = ARRAY_SIZE(w83793_left_fan) / 7;
@@ -1218,6 +1617,14 @@ static int w83793_probe(struct i2c_client *client,
1218 i2c_set_clientdata(client, data); 1617 i2c_set_clientdata(client, data);
1219 data->bank = i2c_smbus_read_byte_data(client, W83793_REG_BANKSEL); 1618 data->bank = i2c_smbus_read_byte_data(client, W83793_REG_BANKSEL);
1220 mutex_init(&data->update_lock); 1619 mutex_init(&data->update_lock);
1620 mutex_init(&data->watchdog_lock);
1621 INIT_LIST_HEAD(&data->list);
1622 kref_init(&data->kref);
1623
1624 /* Store client pointer in our data struct for watchdog usage
1625 (where the client is found through a data ptr instead of the
1626 otherway around) */
1627 data->client = client;
1221 1628
1222 err = w83793_detect_subclients(client); 1629 err = w83793_detect_subclients(client);
1223 if (err) 1630 if (err)
@@ -1380,8 +1787,77 @@ static int w83793_probe(struct i2c_client *client,
1380 goto exit_remove; 1787 goto exit_remove;
1381 } 1788 }
1382 1789
1790 /* Watchdog initialization */
1791
1792 /* Register boot notifier */
1793 err = register_reboot_notifier(&watchdog_notifier);
1794 if (err != 0) {
1795 dev_err(&client->dev,
1796 "cannot register reboot notifier (err=%d)\n", err);
1797 goto exit_devunreg;
1798 }
1799
1800 /* Enable Watchdog registers.
1801 Set Configuration Register to Enable Watch Dog Registers
1802 (Bit 2) = XXXX, X1XX. */
1803 tmp = w83793_read_value(client, W83793_REG_CONFIG);
1804 w83793_write_value(client, W83793_REG_CONFIG, tmp | 0x04);
1805
1806 /* Set the default watchdog timeout */
1807 data->watchdog_timeout = timeout;
1808
1809 /* Check, if last reboot was caused by watchdog */
1810 data->watchdog_caused_reboot =
1811 w83793_read_value(data->client, W83793_REG_WDT_STATUS) & 0x01;
1812
1813 /* Disable Soft Watchdog during initialiation */
1814 watchdog_disable(data);
1815
1816 /* We take the data_mutex lock early so that watchdog_open() cannot
1817 run when misc_register() has completed, but we've not yet added
1818 our data to the watchdog_data_list (and set the default timeout) */
1819 mutex_lock(&watchdog_data_mutex);
1820 for (i = 0; i < ARRAY_SIZE(watchdog_minors); i++) {
1821 /* Register our watchdog part */
1822 snprintf(data->watchdog_name, sizeof(data->watchdog_name),
1823 "watchdog%c", (i == 0) ? '\0' : ('0' + i));
1824 data->watchdog_miscdev.name = data->watchdog_name;
1825 data->watchdog_miscdev.fops = &watchdog_fops;
1826 data->watchdog_miscdev.minor = watchdog_minors[i];
1827
1828 err = misc_register(&data->watchdog_miscdev);
1829 if (err == -EBUSY)
1830 continue;
1831 if (err) {
1832 data->watchdog_miscdev.minor = 0;
1833 dev_err(&client->dev,
1834 "Registering watchdog chardev: %d\n", err);
1835 break;
1836 }
1837
1838 list_add(&data->list, &watchdog_data_list);
1839
1840 dev_info(&client->dev,
1841 "Registered watchdog chardev major 10, minor: %d\n",
1842 watchdog_minors[i]);
1843 break;
1844 }
1845 if (i == ARRAY_SIZE(watchdog_minors)) {
1846 data->watchdog_miscdev.minor = 0;
1847 dev_warn(&client->dev, "Couldn't register watchdog chardev "
1848 "(due to no free minor)\n");
1849 }
1850
1851 mutex_unlock(&watchdog_data_mutex);
1852
1383 return 0; 1853 return 0;
1384 1854
1855 /* Unregister hwmon device */
1856
1857exit_devunreg:
1858
1859 hwmon_device_unregister(data->hwmon_dev);
1860
1385 /* Unregister sysfs hooks */ 1861 /* Unregister sysfs hooks */
1386 1862
1387exit_remove: 1863exit_remove:
@@ -1628,7 +2104,7 @@ static void __exit sensors_w83793_exit(void)
1628 i2c_del_driver(&w83793_driver); 2104 i2c_del_driver(&w83793_driver);
1629} 2105}
1630 2106
1631MODULE_AUTHOR("Yuan Mu"); 2107MODULE_AUTHOR("Yuan Mu, Sven Anders");
1632MODULE_DESCRIPTION("w83793 driver"); 2108MODULE_DESCRIPTION("w83793 driver");
1633MODULE_LICENSE("GPL"); 2109MODULE_LICENSE("GPL");
1634 2110