aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--drivers/hwmon/adm1031.c126
1 files changed, 62 insertions, 64 deletions
diff --git a/drivers/hwmon/adm1031.c b/drivers/hwmon/adm1031.c
index 558d83b25b68..b3e358c65d02 100644
--- a/drivers/hwmon/adm1031.c
+++ b/drivers/hwmon/adm1031.c
@@ -5,7 +5,7 @@
5 Supports adm1030 / adm1031 5 Supports adm1030 / adm1031
6 Copyright (C) 2004 Alexandre d'Alton <alex@alexdalton.org> 6 Copyright (C) 2004 Alexandre d'Alton <alex@alexdalton.org>
7 Reworked by Jean Delvare <khali@linux-fr.org> 7 Reworked by Jean Delvare <khali@linux-fr.org>
8 8
9 This program is free software; you can redistribute it and/or modify 9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by 10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or 11 the Free Software Foundation; either version 2 of the License, or
@@ -32,22 +32,22 @@
32 32
33/* Following macros takes channel parameter starting from 0 to 2 */ 33/* Following macros takes channel parameter starting from 0 to 2 */
34#define ADM1031_REG_FAN_SPEED(nr) (0x08 + (nr)) 34#define ADM1031_REG_FAN_SPEED(nr) (0x08 + (nr))
35#define ADM1031_REG_FAN_DIV(nr) (0x20 + (nr)) 35#define ADM1031_REG_FAN_DIV(nr) (0x20 + (nr))
36#define ADM1031_REG_PWM (0x22) 36#define ADM1031_REG_PWM (0x22)
37#define ADM1031_REG_FAN_MIN(nr) (0x10 + (nr)) 37#define ADM1031_REG_FAN_MIN(nr) (0x10 + (nr))
38 38
39#define ADM1031_REG_TEMP_MAX(nr) (0x14 + 4*(nr)) 39#define ADM1031_REG_TEMP_MAX(nr) (0x14 + 4 * (nr))
40#define ADM1031_REG_TEMP_MIN(nr) (0x15 + 4*(nr)) 40#define ADM1031_REG_TEMP_MIN(nr) (0x15 + 4 * (nr))
41#define ADM1031_REG_TEMP_CRIT(nr) (0x16 + 4*(nr)) 41#define ADM1031_REG_TEMP_CRIT(nr) (0x16 + 4 * (nr))
42 42
43#define ADM1031_REG_TEMP(nr) (0xa + (nr)) 43#define ADM1031_REG_TEMP(nr) (0x0a + (nr))
44#define ADM1031_REG_AUTO_TEMP(nr) (0x24 + (nr)) 44#define ADM1031_REG_AUTO_TEMP(nr) (0x24 + (nr))
45 45
46#define ADM1031_REG_STATUS(nr) (0x2 + (nr)) 46#define ADM1031_REG_STATUS(nr) (0x2 + (nr))
47 47
48#define ADM1031_REG_CONF1 0x0 48#define ADM1031_REG_CONF1 0x00
49#define ADM1031_REG_CONF2 0x1 49#define ADM1031_REG_CONF2 0x01
50#define ADM1031_REG_EXT_TEMP 0x6 50#define ADM1031_REG_EXT_TEMP 0x06
51 51
52#define ADM1031_CONF1_MONITOR_ENABLE 0x01 /* Monitoring enable */ 52#define ADM1031_CONF1_MONITOR_ENABLE 0x01 /* Monitoring enable */
53#define ADM1031_CONF1_PWM_INVERT 0x08 /* PWM Invert */ 53#define ADM1031_CONF1_PWM_INVERT 0x08 /* PWM Invert */
@@ -78,7 +78,7 @@ struct adm1031_data {
78 /* The chan_select_table contains the possible configurations for 78 /* The chan_select_table contains the possible configurations for
79 * auto fan control. 79 * auto fan control.
80 */ 80 */
81 auto_chan_table_t *chan_select_table; 81 const auto_chan_table_t *chan_select_table;
82 u16 alarm; 82 u16 alarm;
83 u8 conf1; 83 u8 conf1;
84 u8 conf2; 84 u8 conf2;
@@ -181,25 +181,25 @@ static int AUTO_TEMP_MAX_TO_REG(int val, int reg, int pwm)
181#define GET_FAN_AUTO_BITFIELD(data, idx) \ 181#define GET_FAN_AUTO_BITFIELD(data, idx) \
182 (*(data)->chan_select_table)[FAN_CHAN_FROM_REG((data)->conf1)][idx%2] 182 (*(data)->chan_select_table)[FAN_CHAN_FROM_REG((data)->conf1)][idx%2]
183 183
184/* The tables below contains the possible values for the auto fan 184/* The tables below contains the possible values for the auto fan
185 * control bitfields. the index in the table is the register value. 185 * control bitfields. the index in the table is the register value.
186 * MSb is the auto fan control enable bit, so the four first entries 186 * MSb is the auto fan control enable bit, so the four first entries
187 * in the table disables auto fan control when both bitfields are zero. 187 * in the table disables auto fan control when both bitfields are zero.
188 */ 188 */
189static auto_chan_table_t auto_channel_select_table_adm1031 = { 189static const auto_chan_table_t auto_channel_select_table_adm1031 = {
190 {0, 0}, {0, 0}, {0, 0}, {0, 0}, 190 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
191 {2 /*0b010 */ , 4 /*0b100 */ }, 191 { 2 /* 0b010 */ , 4 /* 0b100 */ },
192 {2 /*0b010 */ , 2 /*0b010 */ }, 192 { 2 /* 0b010 */ , 2 /* 0b010 */ },
193 {4 /*0b100 */ , 4 /*0b100 */ }, 193 { 4 /* 0b100 */ , 4 /* 0b100 */ },
194 {7 /*0b111 */ , 7 /*0b111 */ }, 194 { 7 /* 0b111 */ , 7 /* 0b111 */ },
195}; 195};
196 196
197static auto_chan_table_t auto_channel_select_table_adm1030 = { 197static const auto_chan_table_t auto_channel_select_table_adm1030 = {
198 {0, 0}, {0, 0}, {0, 0}, {0, 0}, 198 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
199 {2 /*0b10 */ , 0}, 199 { 2 /* 0b10 */ , 0 },
200 {0xff /*invalid */ , 0}, 200 { 0xff /* invalid */ , 0 },
201 {0xff /*invalid */ , 0}, 201 { 0xff /* invalid */ , 0 },
202 {3 /*0b11 */ , 0}, 202 { 3 /* 0b11 */ , 0 },
203}; 203};
204 204
205/* That function checks if a bitfield is valid and returns the other bitfield 205/* That function checks if a bitfield is valid and returns the other bitfield
@@ -228,8 +228,8 @@ get_fan_auto_nearest(struct adm1031_data *data,
228 break; 228 break;
229 } else if (val == (*data->chan_select_table)[i][chan] && 229 } else if (val == (*data->chan_select_table)[i][chan] &&
230 first_match == -1) { 230 first_match == -1) {
231 /* Save the first match in case of an exact match has not been 231 /* Save the first match in case of an exact match has
232 * found 232 * not been found
233 */ 233 */
234 first_match = i; 234 first_match = i;
235 } 235 }
@@ -264,16 +264,17 @@ set_fan_auto_channel(struct device *dev, const char *buf, size_t count, int nr)
264 old_fan_mode = data->conf1; 264 old_fan_mode = data->conf1;
265 265
266 mutex_lock(&data->update_lock); 266 mutex_lock(&data->update_lock);
267 267
268 if ((ret = get_fan_auto_nearest(data, nr, val, data->conf1, &reg))) { 268 if ((ret = get_fan_auto_nearest(data, nr, val, data->conf1, &reg))) {
269 mutex_unlock(&data->update_lock); 269 mutex_unlock(&data->update_lock);
270 return ret; 270 return ret;
271 } 271 }
272 if (((data->conf1 = FAN_CHAN_TO_REG(reg, data->conf1)) & ADM1031_CONF1_AUTO_MODE) ^ 272 data->conf1 = FAN_CHAN_TO_REG(reg, data->conf1);
273 if ((data->conf1 & ADM1031_CONF1_AUTO_MODE) ^
273 (old_fan_mode & ADM1031_CONF1_AUTO_MODE)) { 274 (old_fan_mode & ADM1031_CONF1_AUTO_MODE)) {
274 if (data->conf1 & ADM1031_CONF1_AUTO_MODE){ 275 if (data->conf1 & ADM1031_CONF1_AUTO_MODE){
275 /* Switch to Auto Fan Mode 276 /* Switch to Auto Fan Mode
276 * Save PWM registers 277 * Save PWM registers
277 * Set PWM registers to 33% Both */ 278 * Set PWM registers to 33% Both */
278 data->old_pwm[0] = data->pwm[0]; 279 data->old_pwm[0] = data->pwm[0];
279 data->old_pwm[1] = data->pwm[1]; 280 data->old_pwm[1] = data->pwm[1];
@@ -283,7 +284,7 @@ set_fan_auto_channel(struct device *dev, const char *buf, size_t count, int nr)
283 data->pwm[0] = data->old_pwm[0]; 284 data->pwm[0] = data->old_pwm[0];
284 data->pwm[1] = data->old_pwm[1]; 285 data->pwm[1] = data->old_pwm[1];
285 /* Restore PWM registers */ 286 /* Restore PWM registers */
286 adm1031_write_value(client, ADM1031_REG_PWM, 287 adm1031_write_value(client, ADM1031_REG_PWM,
287 data->pwm[0] | (data->pwm[1] << 4)); 288 data->pwm[0] | (data->pwm[1] << 4));
288 } 289 }
289 } 290 }
@@ -314,7 +315,7 @@ fan_auto_channel_offset(2);
314static ssize_t show_auto_temp_off(struct device *dev, char *buf, int nr) 315static ssize_t show_auto_temp_off(struct device *dev, char *buf, int nr)
315{ 316{
316 struct adm1031_data *data = adm1031_update_device(dev); 317 struct adm1031_data *data = adm1031_update_device(dev);
317 return sprintf(buf, "%d\n", 318 return sprintf(buf, "%d\n",
318 AUTO_TEMP_OFF_FROM_REG(data->auto_temp[nr])); 319 AUTO_TEMP_OFF_FROM_REG(data->auto_temp[nr]));
319} 320}
320static ssize_t show_auto_temp_min(struct device *dev, char *buf, int nr) 321static ssize_t show_auto_temp_min(struct device *dev, char *buf, int nr)
@@ -407,7 +408,7 @@ set_pwm(struct device *dev, const char *buf, size_t count, int nr)
407 int reg; 408 int reg;
408 409
409 mutex_lock(&data->update_lock); 410 mutex_lock(&data->update_lock);
410 if ((data->conf1 & ADM1031_CONF1_AUTO_MODE) && 411 if ((data->conf1 & ADM1031_CONF1_AUTO_MODE) &&
411 (((val>>4) & 0xf) != 5)) { 412 (((val>>4) & 0xf) != 5)) {
412 /* In automatic mode, the only PWM accepted is 33% */ 413 /* In automatic mode, the only PWM accepted is 33% */
413 mutex_unlock(&data->update_lock); 414 mutex_unlock(&data->update_lock);
@@ -471,7 +472,7 @@ static int trust_fan_readings(struct adm1031_data *data, int chan)
471 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[0]) 472 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[0])
472 || data->temp[1] >= 473 || data->temp[1] >=
473 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[1]) 474 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[1])
474 || (data->chip_type == adm1031 475 || (data->chip_type == adm1031
475 && data->temp[2] >= 476 && data->temp[2] >=
476 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[2])); 477 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[2]));
477 break; 478 break;
@@ -514,7 +515,7 @@ set_fan_min(struct device *dev, const char *buf, size_t count, int nr)
514 515
515 mutex_lock(&data->update_lock); 516 mutex_lock(&data->update_lock);
516 if (val) { 517 if (val) {
517 data->fan_min[nr] = 518 data->fan_min[nr] =
518 FAN_TO_REG(val, FAN_DIV_FROM_REG(data->fan_div[nr])); 519 FAN_TO_REG(val, FAN_DIV_FROM_REG(data->fan_div[nr]));
519 } else { 520 } else {
520 data->fan_min[nr] = 0xff; 521 data->fan_min[nr] = 0xff;
@@ -535,12 +536,12 @@ set_fan_div(struct device *dev, const char *buf, size_t count, int nr)
535 536
536 tmp = val == 8 ? 0xc0 : 537 tmp = val == 8 ? 0xc0 :
537 val == 4 ? 0x80 : 538 val == 4 ? 0x80 :
538 val == 2 ? 0x40 : 539 val == 2 ? 0x40 :
539 val == 1 ? 0x00 : 540 val == 1 ? 0x00 :
540 0xff; 541 0xff;
541 if (tmp == 0xff) 542 if (tmp == 0xff)
542 return -EINVAL; 543 return -EINVAL;
543 544
544 mutex_lock(&data->update_lock); 545 mutex_lock(&data->update_lock);
545 /* Get fresh readings */ 546 /* Get fresh readings */
546 data->fan_div[nr] = adm1031_read_value(client, 547 data->fan_div[nr] = adm1031_read_value(client,
@@ -550,14 +551,13 @@ set_fan_div(struct device *dev, const char *buf, size_t count, int nr)
550 551
551 /* Write the new clock divider and fan min */ 552 /* Write the new clock divider and fan min */
552 old_div = FAN_DIV_FROM_REG(data->fan_div[nr]); 553 old_div = FAN_DIV_FROM_REG(data->fan_div[nr]);
553 data->fan_div[nr] = (tmp & 0xC0) | (0x3f & data->fan_div[nr]); 554 data->fan_div[nr] = tmp | (0x3f & data->fan_div[nr]);
554 new_min = data->fan_min[nr] * old_div / 555 new_min = data->fan_min[nr] * old_div / val;
555 FAN_DIV_FROM_REG(data->fan_div[nr]);
556 data->fan_min[nr] = new_min > 0xff ? 0xff : new_min; 556 data->fan_min[nr] = new_min > 0xff ? 0xff : new_min;
557 557
558 adm1031_write_value(client, ADM1031_REG_FAN_DIV(nr), 558 adm1031_write_value(client, ADM1031_REG_FAN_DIV(nr),
559 data->fan_div[nr]); 559 data->fan_div[nr]);
560 adm1031_write_value(client, ADM1031_REG_FAN_MIN(nr), 560 adm1031_write_value(client, ADM1031_REG_FAN_MIN(nr),
561 data->fan_min[nr]); 561 data->fan_min[nr]);
562 562
563 /* Invalidate the cache: fan speed is no longer valid */ 563 /* Invalidate the cache: fan speed is no longer valid */
@@ -796,7 +796,7 @@ static const struct attribute_group adm1031_group_opt = {
796/* This function is called by i2c_probe */ 796/* This function is called by i2c_probe */
797static int adm1031_detect(struct i2c_adapter *adapter, int address, int kind) 797static int adm1031_detect(struct i2c_adapter *adapter, int address, int kind)
798{ 798{
799 struct i2c_client *new_client; 799 struct i2c_client *client;
800 struct adm1031_data *data; 800 struct adm1031_data *data;
801 int err = 0; 801 int err = 0;
802 const char *name = ""; 802 const char *name = "";
@@ -809,17 +809,16 @@ static int adm1031_detect(struct i2c_adapter *adapter, int address, int kind)
809 goto exit; 809 goto exit;
810 } 810 }
811 811
812 new_client = &data->client; 812 client = &data->client;
813 i2c_set_clientdata(new_client, data); 813 i2c_set_clientdata(client, data);
814 new_client->addr = address; 814 client->addr = address;
815 new_client->adapter = adapter; 815 client->adapter = adapter;
816 new_client->driver = &adm1031_driver; 816 client->driver = &adm1031_driver;
817 new_client->flags = 0;
818 817
819 if (kind < 0) { 818 if (kind < 0) {
820 int id, co; 819 int id, co;
821 id = i2c_smbus_read_byte_data(new_client, 0x3d); 820 id = i2c_smbus_read_byte_data(client, 0x3d);
822 co = i2c_smbus_read_byte_data(new_client, 0x3e); 821 co = i2c_smbus_read_byte_data(client, 0x3e);
823 822
824 if (!((id == 0x31 || id == 0x30) && co == 0x41)) 823 if (!((id == 0x31 || id == 0x30) && co == 0x41))
825 goto exit_free; 824 goto exit_free;
@@ -840,28 +839,27 @@ static int adm1031_detect(struct i2c_adapter *adapter, int address, int kind)
840 } 839 }
841 data->chip_type = kind; 840 data->chip_type = kind;
842 841
843 strlcpy(new_client->name, name, I2C_NAME_SIZE); 842 strlcpy(client->name, name, I2C_NAME_SIZE);
844 data->valid = 0;
845 mutex_init(&data->update_lock); 843 mutex_init(&data->update_lock);
846 844
847 /* Tell the I2C layer a new client has arrived */ 845 /* Tell the I2C layer a new client has arrived */
848 if ((err = i2c_attach_client(new_client))) 846 if ((err = i2c_attach_client(client)))
849 goto exit_free; 847 goto exit_free;
850 848
851 /* Initialize the ADM1031 chip */ 849 /* Initialize the ADM1031 chip */
852 adm1031_init_client(new_client); 850 adm1031_init_client(client);
853 851
854 /* Register sysfs hooks */ 852 /* Register sysfs hooks */
855 if ((err = sysfs_create_group(&new_client->dev.kobj, &adm1031_group))) 853 if ((err = sysfs_create_group(&client->dev.kobj, &adm1031_group)))
856 goto exit_detach; 854 goto exit_detach;
857 855
858 if (kind == adm1031) { 856 if (kind == adm1031) {
859 if ((err = sysfs_create_group(&new_client->dev.kobj, 857 if ((err = sysfs_create_group(&client->dev.kobj,
860 &adm1031_group_opt))) 858 &adm1031_group_opt)))
861 goto exit_remove; 859 goto exit_remove;
862 } 860 }
863 861
864 data->hwmon_dev = hwmon_device_register(&new_client->dev); 862 data->hwmon_dev = hwmon_device_register(&client->dev);
865 if (IS_ERR(data->hwmon_dev)) { 863 if (IS_ERR(data->hwmon_dev)) {
866 err = PTR_ERR(data->hwmon_dev); 864 err = PTR_ERR(data->hwmon_dev);
867 goto exit_remove; 865 goto exit_remove;
@@ -870,10 +868,10 @@ static int adm1031_detect(struct i2c_adapter *adapter, int address, int kind)
870 return 0; 868 return 0;
871 869
872exit_remove: 870exit_remove:
873 sysfs_remove_group(&new_client->dev.kobj, &adm1031_group); 871 sysfs_remove_group(&client->dev.kobj, &adm1031_group);
874 sysfs_remove_group(&new_client->dev.kobj, &adm1031_group_opt); 872 sysfs_remove_group(&client->dev.kobj, &adm1031_group_opt);
875exit_detach: 873exit_detach:
876 i2c_detach_client(new_client); 874 i2c_detach_client(client);
877exit_free: 875exit_free:
878 kfree(data); 876 kfree(data);
879exit: 877exit:
@@ -905,7 +903,7 @@ static void adm1031_init_client(struct i2c_client *client)
905 if (data->chip_type == adm1031) { 903 if (data->chip_type == adm1031) {
906 mask |= (ADM1031_CONF2_PWM2_ENABLE | 904 mask |= (ADM1031_CONF2_PWM2_ENABLE |
907 ADM1031_CONF2_TACH2_ENABLE); 905 ADM1031_CONF2_TACH2_ENABLE);
908 } 906 }
909 /* Initialize the ADM1031 chip (enables fan speed reading ) */ 907 /* Initialize the ADM1031 chip (enables fan speed reading ) */
910 read_val = adm1031_read_value(client, ADM1031_REG_CONF2); 908 read_val = adm1031_read_value(client, ADM1031_REG_CONF2);
911 if ((read_val | mask) != read_val) { 909 if ((read_val | mask) != read_val) {
@@ -984,7 +982,7 @@ static struct adm1031_data *adm1031_update_device(struct device *dev)
984 if (data->chip_type == adm1030) { 982 if (data->chip_type == adm1030) {
985 data->alarm &= 0xc0ff; 983 data->alarm &= 0xc0ff;
986 } 984 }
987 985
988 for (chan=0; chan<(data->chip_type == adm1030 ? 1 : 2); chan++) { 986 for (chan=0; chan<(data->chip_type == adm1030 ? 1 : 2); chan++) {
989 data->fan_div[chan] = 987 data->fan_div[chan] =
990 adm1031_read_value(client, ADM1031_REG_FAN_DIV(chan)); 988 adm1031_read_value(client, ADM1031_REG_FAN_DIV(chan));
@@ -993,7 +991,7 @@ static struct adm1031_data *adm1031_update_device(struct device *dev)
993 data->fan[chan] = 991 data->fan[chan] =
994 adm1031_read_value(client, ADM1031_REG_FAN_SPEED(chan)); 992 adm1031_read_value(client, ADM1031_REG_FAN_SPEED(chan));
995 data->pwm[chan] = 993 data->pwm[chan] =
996 0xf & (adm1031_read_value(client, ADM1031_REG_PWM) >> 994 0xf & (adm1031_read_value(client, ADM1031_REG_PWM) >>
997 (4*chan)); 995 (4*chan));
998 } 996 }
999 data->last_updated = jiffies; 997 data->last_updated = jiffies;