aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/hwmon/lm78.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/hwmon/lm78.c')
-rw-r--r--drivers/hwmon/lm78.c206
1 files changed, 132 insertions, 74 deletions
diff --git a/drivers/hwmon/lm78.c b/drivers/hwmon/lm78.c
index 6df0b4681710..f6bc414e1e91 100644
--- a/drivers/hwmon/lm78.c
+++ b/drivers/hwmon/lm78.c
@@ -1,23 +1,23 @@
1/* 1/*
2 lm78.c - Part of lm_sensors, Linux kernel modules for hardware 2 * lm78.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring 3 * monitoring
4 Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl> 4 * Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl>
5 Copyright (c) 2007, 2011 Jean Delvare <khali@linux-fr.org> 5 * Copyright (c) 2007, 2011 Jean Delvare <khali@linux-fr.org>
6 6 *
7 This program is free software; you can redistribute it and/or modify 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 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 9 * the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version. 10 * (at your option) any later version.
11 11 *
12 This program is distributed in the hope that it will be useful, 12 * This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details. 15 * GNU General Public License for more details.
16 16 *
17 You should have received a copy of the GNU General Public License 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 18 * along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20*/ 20 */
21 21
22#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 22#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23 23
@@ -74,11 +74,15 @@ enum chips { lm78, lm79 };
74#define LM78_REG_I2C_ADDR 0x48 74#define LM78_REG_I2C_ADDR 0x48
75 75
76 76
77/* Conversions. Rounding and limit checking is only done on the TO_REG 77/*
78 variants. */ 78 * Conversions. Rounding and limit checking is only done on the TO_REG
79 * variants.
80 */
79 81
80/* IN: mV, (0V to 4.08V) 82/*
81 REG: 16mV/bit */ 83 * IN: mV (0V to 4.08V)
84 * REG: 16mV/bit
85 */
82static inline u8 IN_TO_REG(unsigned long val) 86static inline u8 IN_TO_REG(unsigned long val)
83{ 87{
84 unsigned long nval = SENSORS_LIMIT(val, 0, 4080); 88 unsigned long nval = SENSORS_LIMIT(val, 0, 4080);
@@ -95,15 +99,17 @@ static inline u8 FAN_TO_REG(long rpm, int div)
95 99
96static inline int FAN_FROM_REG(u8 val, int div) 100static inline int FAN_FROM_REG(u8 val, int div)
97{ 101{
98 return val==0 ? -1 : val==255 ? 0 : 1350000/(val*div); 102 return val == 0 ? -1 : val == 255 ? 0 : 1350000 / (val * div);
99} 103}
100 104
101/* TEMP: mC (-128C to +127C) 105/*
102 REG: 1C/bit, two's complement */ 106 * TEMP: mC (-128C to +127C)
107 * REG: 1C/bit, two's complement
108 */
103static inline s8 TEMP_TO_REG(int val) 109static inline s8 TEMP_TO_REG(int val)
104{ 110{
105 int nval = SENSORS_LIMIT(val, -128000, 127000) ; 111 int nval = SENSORS_LIMIT(val, -128000, 127000) ;
106 return nval<0 ? (nval-500)/1000 : (nval+500)/1000; 112 return nval < 0 ? (nval - 500) / 1000 : (nval + 500) / 1000;
107} 113}
108 114
109static inline int TEMP_FROM_REG(s8 val) 115static inline int TEMP_FROM_REG(s8 val)
@@ -177,8 +183,13 @@ static ssize_t set_in_min(struct device *dev, struct device_attribute *da,
177{ 183{
178 struct sensor_device_attribute *attr = to_sensor_dev_attr(da); 184 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
179 struct lm78_data *data = dev_get_drvdata(dev); 185 struct lm78_data *data = dev_get_drvdata(dev);
180 unsigned long val = simple_strtoul(buf, NULL, 10);
181 int nr = attr->index; 186 int nr = attr->index;
187 unsigned long val;
188 int err;
189
190 err = kstrtoul(buf, 10, &val);
191 if (err)
192 return err;
182 193
183 mutex_lock(&data->update_lock); 194 mutex_lock(&data->update_lock);
184 data->in_min[nr] = IN_TO_REG(val); 195 data->in_min[nr] = IN_TO_REG(val);
@@ -192,8 +203,13 @@ static ssize_t set_in_max(struct device *dev, struct device_attribute *da,
192{ 203{
193 struct sensor_device_attribute *attr = to_sensor_dev_attr(da); 204 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
194 struct lm78_data *data = dev_get_drvdata(dev); 205 struct lm78_data *data = dev_get_drvdata(dev);
195 unsigned long val = simple_strtoul(buf, NULL, 10);
196 int nr = attr->index; 206 int nr = attr->index;
207 unsigned long val;
208 int err;
209
210 err = kstrtoul(buf, 10, &val);
211 if (err)
212 return err;
197 213
198 mutex_lock(&data->update_lock); 214 mutex_lock(&data->update_lock);
199 data->in_max[nr] = IN_TO_REG(val); 215 data->in_max[nr] = IN_TO_REG(val);
@@ -201,7 +217,7 @@ static ssize_t set_in_max(struct device *dev, struct device_attribute *da,
201 mutex_unlock(&data->update_lock); 217 mutex_unlock(&data->update_lock);
202 return count; 218 return count;
203} 219}
204 220
205#define show_in_offset(offset) \ 221#define show_in_offset(offset) \
206static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \ 222static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
207 show_in, NULL, offset); \ 223 show_in, NULL, offset); \
@@ -237,7 +253,12 @@ static ssize_t set_temp_over(struct device *dev, struct device_attribute *da,
237 const char *buf, size_t count) 253 const char *buf, size_t count)
238{ 254{
239 struct lm78_data *data = dev_get_drvdata(dev); 255 struct lm78_data *data = dev_get_drvdata(dev);
240 long val = simple_strtol(buf, NULL, 10); 256 long val;
257 int err;
258
259 err = kstrtol(buf, 10, &val);
260 if (err)
261 return err;
241 262
242 mutex_lock(&data->update_lock); 263 mutex_lock(&data->update_lock);
243 data->temp_over = TEMP_TO_REG(val); 264 data->temp_over = TEMP_TO_REG(val);
@@ -257,7 +278,12 @@ static ssize_t set_temp_hyst(struct device *dev, struct device_attribute *da,
257 const char *buf, size_t count) 278 const char *buf, size_t count)
258{ 279{
259 struct lm78_data *data = dev_get_drvdata(dev); 280 struct lm78_data *data = dev_get_drvdata(dev);
260 long val = simple_strtol(buf, NULL, 10); 281 long val;
282 int err;
283
284 err = kstrtol(buf, 10, &val);
285 if (err)
286 return err;
261 287
262 mutex_lock(&data->update_lock); 288 mutex_lock(&data->update_lock);
263 data->temp_hyst = TEMP_TO_REG(val); 289 data->temp_hyst = TEMP_TO_REG(val);
@@ -280,7 +306,7 @@ static ssize_t show_fan(struct device *dev, struct device_attribute *da,
280 struct lm78_data *data = lm78_update_device(dev); 306 struct lm78_data *data = lm78_update_device(dev);
281 int nr = attr->index; 307 int nr = attr->index;
282 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr], 308 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
283 DIV_FROM_REG(data->fan_div[nr])) ); 309 DIV_FROM_REG(data->fan_div[nr])));
284} 310}
285 311
286static ssize_t show_fan_min(struct device *dev, struct device_attribute *da, 312static ssize_t show_fan_min(struct device *dev, struct device_attribute *da,
@@ -289,8 +315,8 @@ static ssize_t show_fan_min(struct device *dev, struct device_attribute *da,
289 struct sensor_device_attribute *attr = to_sensor_dev_attr(da); 315 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
290 struct lm78_data *data = lm78_update_device(dev); 316 struct lm78_data *data = lm78_update_device(dev);
291 int nr = attr->index; 317 int nr = attr->index;
292 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan_min[nr], 318 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
293 DIV_FROM_REG(data->fan_div[nr])) ); 319 DIV_FROM_REG(data->fan_div[nr])));
294} 320}
295 321
296static ssize_t set_fan_min(struct device *dev, struct device_attribute *da, 322static ssize_t set_fan_min(struct device *dev, struct device_attribute *da,
@@ -299,7 +325,12 @@ static ssize_t set_fan_min(struct device *dev, struct device_attribute *da,
299 struct sensor_device_attribute *attr = to_sensor_dev_attr(da); 325 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
300 struct lm78_data *data = dev_get_drvdata(dev); 326 struct lm78_data *data = dev_get_drvdata(dev);
301 int nr = attr->index; 327 int nr = attr->index;
302 unsigned long val = simple_strtoul(buf, NULL, 10); 328 unsigned long val;
329 int err;
330
331 err = kstrtoul(buf, 10, &val);
332 if (err)
333 return err;
303 334
304 mutex_lock(&data->update_lock); 335 mutex_lock(&data->update_lock);
305 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr])); 336 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
@@ -316,29 +347,44 @@ static ssize_t show_fan_div(struct device *dev, struct device_attribute *da,
316 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[attr->index])); 347 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[attr->index]));
317} 348}
318 349
319/* Note: we save and restore the fan minimum here, because its value is 350/*
320 determined in part by the fan divisor. This follows the principle of 351 * Note: we save and restore the fan minimum here, because its value is
321 least surprise; the user doesn't expect the fan minimum to change just 352 * determined in part by the fan divisor. This follows the principle of
322 because the divisor changed. */ 353 * least surprise; the user doesn't expect the fan minimum to change just
354 * because the divisor changed.
355 */
323static ssize_t set_fan_div(struct device *dev, struct device_attribute *da, 356static ssize_t set_fan_div(struct device *dev, struct device_attribute *da,
324 const char *buf, size_t count) 357 const char *buf, size_t count)
325{ 358{
326 struct sensor_device_attribute *attr = to_sensor_dev_attr(da); 359 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
327 struct lm78_data *data = dev_get_drvdata(dev); 360 struct lm78_data *data = dev_get_drvdata(dev);
328 int nr = attr->index; 361 int nr = attr->index;
329 unsigned long val = simple_strtoul(buf, NULL, 10);
330 unsigned long min; 362 unsigned long min;
331 u8 reg; 363 u8 reg;
364 unsigned long val;
365 int err;
366
367 err = kstrtoul(buf, 10, &val);
368 if (err)
369 return err;
332 370
333 mutex_lock(&data->update_lock); 371 mutex_lock(&data->update_lock);
334 min = FAN_FROM_REG(data->fan_min[nr], 372 min = FAN_FROM_REG(data->fan_min[nr],
335 DIV_FROM_REG(data->fan_div[nr])); 373 DIV_FROM_REG(data->fan_div[nr]));
336 374
337 switch (val) { 375 switch (val) {
338 case 1: data->fan_div[nr] = 0; break; 376 case 1:
339 case 2: data->fan_div[nr] = 1; break; 377 data->fan_div[nr] = 0;
340 case 4: data->fan_div[nr] = 2; break; 378 break;
341 case 8: data->fan_div[nr] = 3; break; 379 case 2:
380 data->fan_div[nr] = 1;
381 break;
382 case 4:
383 data->fan_div[nr] = 2;
384 break;
385 case 8:
386 data->fan_div[nr] = 3;
387 break;
342 default: 388 default:
343 dev_err(dev, "fan_div value %ld not " 389 dev_err(dev, "fan_div value %ld not "
344 "supported. Choose one of 1, 2, 4 or 8!\n", val); 390 "supported. Choose one of 1, 2, 4 or 8!\n", val);
@@ -484,8 +530,10 @@ static struct platform_device *pdev;
484 530
485static unsigned short isa_address = 0x290; 531static unsigned short isa_address = 0x290;
486 532
487/* I2C devices get this name attribute automatically, but for ISA devices 533/*
488 we must create it by ourselves. */ 534 * I2C devices get this name attribute automatically, but for ISA devices
535 * we must create it by ourselves.
536 */
489static ssize_t show_name(struct device *dev, struct device_attribute 537static ssize_t show_name(struct device *dev, struct device_attribute
490 *devattr, char *buf) 538 *devattr, char *buf)
491{ 539{
@@ -515,8 +563,10 @@ static int lm78_alias_detect(struct i2c_client *client, u8 chipid)
515 if ((lm78_read_value(isa, LM78_REG_CHIPID) & 0xfe) != (chipid & 0xfe)) 563 if ((lm78_read_value(isa, LM78_REG_CHIPID) & 0xfe) != (chipid & 0xfe))
516 return 0; /* Chip type doesn't match */ 564 return 0; /* Chip type doesn't match */
517 565
518 /* We compare all the limit registers, the config register and the 566 /*
519 * interrupt mask registers */ 567 * We compare all the limit registers, the config register and the
568 * interrupt mask registers
569 */
520 for (i = 0x2b; i <= 0x3d; i++) { 570 for (i = 0x2b; i <= 0x3d; i++) {
521 if (lm78_read_value(isa, i) != 571 if (lm78_read_value(isa, i) !=
522 i2c_smbus_read_byte_data(client, i)) 572 i2c_smbus_read_byte_data(client, i))
@@ -558,9 +608,11 @@ static int lm78_i2c_detect(struct i2c_client *client,
558 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) 608 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
559 return -ENODEV; 609 return -ENODEV;
560 610
561 /* We block updates of the ISA device to minimize the risk of 611 /*
562 concurrent access to the same LM78 chip through different 612 * We block updates of the ISA device to minimize the risk of
563 interfaces. */ 613 * concurrent access to the same LM78 chip through different
614 * interfaces.
615 */
564 if (isa) 616 if (isa)
565 mutex_lock(&isa->update_lock); 617 mutex_lock(&isa->update_lock);
566 618
@@ -669,11 +721,13 @@ static struct i2c_driver lm78_driver = {
669 .address_list = normal_i2c, 721 .address_list = normal_i2c,
670}; 722};
671 723
672/* The SMBus locks itself, but ISA access must be locked explicitly! 724/*
673 We don't want to lock the whole ISA bus, so we lock each client 725 * The SMBus locks itself, but ISA access must be locked explicitly!
674 separately. 726 * We don't want to lock the whole ISA bus, so we lock each client
675 We ignore the LM78 BUSY flag at this moment - it could lead to deadlocks, 727 * separately.
676 would slow down the LM78 access and should not be necessary. */ 728 * We ignore the LM78 BUSY flag at this moment - it could lead to deadlocks,
729 * would slow down the LM78 access and should not be necessary.
730 */
677static int lm78_read_value(struct lm78_data *data, u8 reg) 731static int lm78_read_value(struct lm78_data *data, u8 reg)
678{ 732{
679 struct i2c_client *client = data->client; 733 struct i2c_client *client = data->client;
@@ -691,13 +745,6 @@ static int lm78_read_value(struct lm78_data *data, u8 reg)
691 return i2c_smbus_read_byte_data(client, reg); 745 return i2c_smbus_read_byte_data(client, reg);
692} 746}
693 747
694/* The SMBus locks itself, but ISA access muse be locked explicitly!
695 We don't want to lock the whole ISA bus, so we lock each client
696 separately.
697 We ignore the LM78 BUSY flag at this moment - it could lead to deadlocks,
698 would slow down the LM78 access and should not be necessary.
699 There are some ugly typecasts here, but the good new is - they should
700 nowhere else be necessary! */
701static int lm78_write_value(struct lm78_data *data, u8 reg, u8 value) 748static int lm78_write_value(struct lm78_data *data, u8 reg, u8 value)
702{ 749{
703 struct i2c_client *client = data->client; 750 struct i2c_client *client = data->client;
@@ -823,8 +870,11 @@ static int __devinit lm78_isa_probe(struct platform_device *pdev)
823 lm78_init_device(data); 870 lm78_init_device(data);
824 871
825 /* Register sysfs hooks */ 872 /* Register sysfs hooks */
826 if ((err = sysfs_create_group(&pdev->dev.kobj, &lm78_group)) 873 err = sysfs_create_group(&pdev->dev.kobj, &lm78_group);
827 || (err = device_create_file(&pdev->dev, &dev_attr_name))) 874 if (err)
875 goto exit_remove_files;
876 err = device_create_file(&pdev->dev, &dev_attr_name);
877 if (err)
828 goto exit_remove_files; 878 goto exit_remove_files;
829 879
830 data->hwmon_dev = hwmon_device_register(&pdev->dev); 880 data->hwmon_dev = hwmon_device_register(&pdev->dev);
@@ -876,9 +926,11 @@ static int __init lm78_isa_found(unsigned short address)
876 int val, save, found = 0; 926 int val, save, found = 0;
877 int port; 927 int port;
878 928
879 /* Some boards declare base+0 to base+7 as a PNP device, some base+4 929 /*
930 * Some boards declare base+0 to base+7 as a PNP device, some base+4
880 * to base+7 and some base+5 to base+6. So we better request each port 931 * to base+7 and some base+5 to base+6. So we better request each port
881 * individually for the probing phase. */ 932 * individually for the probing phase.
933 */
882 for (port = address; port < address + LM78_EXTENT; port++) { 934 for (port = address; port < address + LM78_EXTENT; port++) {
883 if (!request_region(port, 1, "lm78")) { 935 if (!request_region(port, 1, "lm78")) {
884 pr_debug("Failed to request port 0x%x\n", port); 936 pr_debug("Failed to request port 0x%x\n", port);
@@ -887,8 +939,10 @@ static int __init lm78_isa_found(unsigned short address)
887 } 939 }
888 940
889#define REALLY_SLOW_IO 941#define REALLY_SLOW_IO
890 /* We need the timeouts for at least some LM78-like 942 /*
891 chips. But only if we read 'undefined' registers. */ 943 * We need the timeouts for at least some LM78-like
944 * chips. But only if we read 'undefined' registers.
945 */
892 val = inb_p(address + 1); 946 val = inb_p(address + 1);
893 if (inb_p(address + 2) != val 947 if (inb_p(address + 2) != val
894 || inb_p(address + 3) != val 948 || inb_p(address + 3) != val
@@ -896,8 +950,10 @@ static int __init lm78_isa_found(unsigned short address)
896 goto release; 950 goto release;
897#undef REALLY_SLOW_IO 951#undef REALLY_SLOW_IO
898 952
899 /* We should be able to change the 7 LSB of the address port. The 953 /*
900 MSB (busy flag) should be clear initially, set after the write. */ 954 * We should be able to change the 7 LSB of the address port. The
955 * MSB (busy flag) should be clear initially, set after the write.
956 */
901 save = inb_p(address + LM78_ADDR_REG_OFFSET); 957 save = inb_p(address + LM78_ADDR_REG_OFFSET);
902 if (save & 0x80) 958 if (save & 0x80)
903 goto release; 959 goto release;
@@ -1036,8 +1092,10 @@ static int __init sm_lm78_init(void)
1036{ 1092{
1037 int res; 1093 int res;
1038 1094
1039 /* We register the ISA device first, so that we can skip the 1095 /*
1040 * registration of an I2C interface to the same device. */ 1096 * We register the ISA device first, so that we can skip the
1097 * registration of an I2C interface to the same device.
1098 */
1041 res = lm78_isa_register(); 1099 res = lm78_isa_register();
1042 if (res) 1100 if (res)
1043 goto exit; 1101 goto exit;