Commit 89fac11cb3e7c5860c425dba14845c09ccede39d

Authored by Darrick J. Wong
Committed by Linus Torvalds
1 parent 2f22d5dff6

adt7470: make automatic fan control really work

It turns out that the adt7470's automatic fan control algorithm only works
when the temperature sensors get updated.  This in turn happens only when
someone tells the chip to read its temperature sensors.  Regrettably, this
means that we have to drive the chip periodically.

Signed-off-by: Darrick J. Wong <djwong@us.ibm.com>
Cc: Jean Delvare <khali@linux-fr.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

Showing 2 changed files with 142 additions and 33 deletions Side-by-side Diff

Documentation/hwmon/adt7470
... ... @@ -31,15 +31,11 @@
31 31 limit values. The ADT7470 will signal an ALARM if any measured value exceeds
32 32 either limit.
33 33  
34   -The ADT7470 DOES NOT sample all inputs continuously. A single pin on the
35   -ADT7470 is connected to a multitude of thermal diodes, but the chip must be
36   -instructed explicitly to read the multitude of diodes. If you want to use
37   -automatic fan control mode, you must manually read any of the temperature
38   -sensors or the fan control algorithm will not run. The chip WILL NOT DO THIS
39   -AUTOMATICALLY; this must be done from userspace. This may be a bug in the chip
40   -design, given that many other AD chips take care of this. The driver will not
41   -read the registers more often than once every 5 seconds. Further,
42   -configuration data is only read once per minute.
  34 +The ADT7470 samples all inputs continuously. A kernel thread is started up for
  35 +the purpose of periodically querying the temperature sensors, thus allowing the
  36 +automatic fan pwm control to set the fan speed. The driver will not read the
  37 +registers more often than once every 5 seconds. Further, configuration data is
  38 +only read once per minute.
43 39  
44 40 Special Features
45 41 ----------------
... ... @@ -72,6 +68,7 @@
72 68 Notes
73 69 -----
74 70  
75   -As stated above, the temperature inputs must be read periodically from
76   -userspace in order for the automatic pwm algorithm to run.
  71 +The temperature inputs no longer need to be read periodically from userspace in
  72 +order for the automatic pwm algorithm to run. This was the case for earlier
  73 +versions of the driver.
drivers/hwmon/adt7470.c
... ... @@ -28,6 +28,7 @@
28 28 #include <linux/mutex.h>
29 29 #include <linux/delay.h>
30 30 #include <linux/log2.h>
  31 +#include <linux/kthread.h>
31 32  
32 33 /* Addresses to scan */
33 34 static const unsigned short normal_i2c[] = { 0x2C, 0x2E, 0x2F, I2C_CLIENT_END };
... ... @@ -132,6 +133,9 @@
132 133 /* Wait at least 200ms per sensor for 10 sensors */
133 134 #define TEMP_COLLECTION_TIME 2000
134 135  
  136 +/* auto update thing won't fire more than every 2s */
  137 +#define AUTO_UPDATE_INTERVAL 2000
  138 +
135 139 /* datasheet says to divide this number by the fan reading to get fan rpm */
136 140 #define FAN_PERIOD_TO_RPM(x) ((90000 * 60) / (x))
137 141 #define FAN_RPM_TO_PERIOD FAN_PERIOD_TO_RPM
... ... @@ -148,6 +152,7 @@
148 152 unsigned long limits_last_updated; /* In jiffies */
149 153  
150 154 int num_temp_sensors; /* -1 = probe */
  155 + int temperatures_probed;
151 156  
152 157 s8 temp[ADT7470_TEMP_COUNT];
153 158 s8 temp_min[ADT7470_TEMP_COUNT];
... ... @@ -164,6 +169,10 @@
164 169 u8 pwm_min[ADT7470_PWM_COUNT];
165 170 s8 pwm_tmin[ADT7470_PWM_COUNT];
166 171 u8 pwm_auto_temp[ADT7470_PWM_COUNT];
  172 +
  173 + struct task_struct *auto_update;
  174 + struct completion auto_update_stop;
  175 + unsigned int auto_update_interval;
167 176 };
168 177  
169 178 static int adt7470_probe(struct i2c_client *client,
170 179  
171 180  
172 181  
... ... @@ -221,20 +230,14 @@
221 230 }
222 231 }
223 232  
224   -static struct adt7470_data *adt7470_update_device(struct device *dev)
  233 +/* Probe for temperature sensors. Assumes lock is held */
  234 +static int adt7470_read_temperatures(struct i2c_client *client,
  235 + struct adt7470_data *data)
225 236 {
226   - struct i2c_client *client = to_i2c_client(dev);
227   - struct adt7470_data *data = i2c_get_clientdata(client);
228   - unsigned long local_jiffies = jiffies;
229   - u8 cfg, pwm[4], pwm_cfg[2];
  237 + unsigned long res;
230 238 int i;
  239 + u8 cfg, pwm[4], pwm_cfg[2];
231 240  
232   - mutex_lock(&data->lock);
233   - if (time_before(local_jiffies, data->sensors_last_updated +
234   - SENSOR_REFRESH_INTERVAL)
235   - && data->sensors_valid)
236   - goto no_sensor_update;
237   -
238 241 /* save pwm[1-4] config register */
239 242 pwm_cfg[0] = i2c_smbus_read_byte_data(client, ADT7470_REG_PWM_CFG(0));
240 243 pwm_cfg[1] = i2c_smbus_read_byte_data(client, ADT7470_REG_PWM_CFG(2));
... ... @@ -259,9 +262,9 @@
259 262 i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, cfg);
260 263  
261 264 /* Delay is 200ms * number of temp sensors. */
262   - msleep((data->num_temp_sensors >= 0 ?
263   - data->num_temp_sensors * 200 :
264   - TEMP_COLLECTION_TIME));
  265 + res = msleep_interruptible((data->num_temp_sensors >= 0 ?
  266 + data->num_temp_sensors * 200 :
  267 + TEMP_COLLECTION_TIME));
265 268  
266 269 /* done reading temperature sensors */
267 270 cfg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG);
268 271  
269 272  
270 273  
... ... @@ -272,15 +275,81 @@
272 275 i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_CFG(0), pwm_cfg[0]);
273 276 i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_CFG(2), pwm_cfg[1]);
274 277  
275   - for (i = 0; i < ADT7470_TEMP_COUNT; i++)
  278 + if (res) {
  279 + printk(KERN_ERR "ha ha, interrupted");
  280 + return -EAGAIN;
  281 + }
  282 +
  283 + /* Only count fans if we have to */
  284 + if (data->num_temp_sensors >= 0)
  285 + return 0;
  286 +
  287 + for (i = 0; i < ADT7470_TEMP_COUNT; i++) {
276 288 data->temp[i] = i2c_smbus_read_byte_data(client,
277 289 ADT7470_TEMP_REG(i));
  290 + if (data->temp[i])
  291 + data->num_temp_sensors = i + 1;
  292 + }
  293 + data->temperatures_probed = 1;
  294 + return 0;
  295 +}
278 296  
279   - /* Figure out the number of temp sensors */
280   - if (data->num_temp_sensors < 0)
  297 +static int adt7470_update_thread(void *p)
  298 +{
  299 + struct i2c_client *client = p;
  300 + struct adt7470_data *data = i2c_get_clientdata(client);
  301 +
  302 + while (!kthread_should_stop()) {
  303 + mutex_lock(&data->lock);
  304 + adt7470_read_temperatures(client, data);
  305 + mutex_unlock(&data->lock);
  306 + if (kthread_should_stop())
  307 + break;
  308 + msleep_interruptible(data->auto_update_interval);
  309 + }
  310 +
  311 + complete_all(&data->auto_update_stop);
  312 + return 0;
  313 +}
  314 +
  315 +static struct adt7470_data *adt7470_update_device(struct device *dev)
  316 +{
  317 + struct i2c_client *client = to_i2c_client(dev);
  318 + struct adt7470_data *data = i2c_get_clientdata(client);
  319 + unsigned long local_jiffies = jiffies;
  320 + u8 cfg;
  321 + int i;
  322 + int need_sensors = 1;
  323 + int need_limits = 1;
  324 +
  325 + /*
  326 + * Figure out if we need to update the shadow registers.
  327 + * Lockless means that we may occasionally report out of
  328 + * date data.
  329 + */
  330 + if (time_before(local_jiffies, data->sensors_last_updated +
  331 + SENSOR_REFRESH_INTERVAL) &&
  332 + data->sensors_valid)
  333 + need_sensors = 0;
  334 +
  335 + if (time_before(local_jiffies, data->limits_last_updated +
  336 + LIMIT_REFRESH_INTERVAL) &&
  337 + data->limits_valid)
  338 + need_limits = 0;
  339 +
  340 + if (!need_sensors && !need_limits)
  341 + return data;
  342 +
  343 + mutex_lock(&data->lock);
  344 + if (!need_sensors)
  345 + goto no_sensor_update;
  346 +
  347 + if (!data->temperatures_probed)
  348 + adt7470_read_temperatures(client, data);
  349 + else
281 350 for (i = 0; i < ADT7470_TEMP_COUNT; i++)
282   - if (data->temp[i])
283   - data->num_temp_sensors = i + 1;
  351 + data->temp[i] = i2c_smbus_read_byte_data(client,
  352 + ADT7470_TEMP_REG(i));
284 353  
285 354 for (i = 0; i < ADT7470_FAN_COUNT; i++)
286 355 data->fan[i] = adt7470_read_word_data(client,
... ... @@ -329,9 +398,7 @@
329 398 data->sensors_valid = 1;
330 399  
331 400 no_sensor_update:
332   - if (time_before(local_jiffies, data->limits_last_updated +
333   - LIMIT_REFRESH_INTERVAL)
334   - && data->limits_valid)
  401 + if (!need_limits)
335 402 goto out;
336 403  
337 404 for (i = 0; i < ADT7470_TEMP_COUNT; i++) {
... ... @@ -365,6 +432,35 @@
365 432 return data;
366 433 }
367 434  
  435 +static ssize_t show_auto_update_interval(struct device *dev,
  436 + struct device_attribute *devattr,
  437 + char *buf)
  438 +{
  439 + struct adt7470_data *data = adt7470_update_device(dev);
  440 + return sprintf(buf, "%d\n", data->auto_update_interval);
  441 +}
  442 +
  443 +static ssize_t set_auto_update_interval(struct device *dev,
  444 + struct device_attribute *devattr,
  445 + const char *buf,
  446 + size_t count)
  447 +{
  448 + struct i2c_client *client = to_i2c_client(dev);
  449 + struct adt7470_data *data = i2c_get_clientdata(client);
  450 + long temp;
  451 +
  452 + if (strict_strtol(buf, 10, &temp))
  453 + return -EINVAL;
  454 +
  455 + temp = SENSORS_LIMIT(temp, 0, 60000);
  456 +
  457 + mutex_lock(&data->lock);
  458 + data->auto_update_interval = temp;
  459 + mutex_unlock(&data->lock);
  460 +
  461 + return count;
  462 +}
  463 +
368 464 static ssize_t show_num_temp_sensors(struct device *dev,
369 465 struct device_attribute *devattr,
370 466 char *buf)
... ... @@ -389,6 +485,8 @@
389 485  
390 486 mutex_lock(&data->lock);
391 487 data->num_temp_sensors = temp;
  488 + if (temp < 0)
  489 + data->temperatures_probed = 0;
392 490 mutex_unlock(&data->lock);
393 491  
394 492 return count;
... ... @@ -862,6 +960,8 @@
862 960 static DEVICE_ATTR(alarm_mask, S_IRUGO, show_alarm_mask, NULL);
863 961 static DEVICE_ATTR(num_temp_sensors, S_IWUSR | S_IRUGO, show_num_temp_sensors,
864 962 set_num_temp_sensors);
  963 +static DEVICE_ATTR(auto_update_interval, S_IWUSR | S_IRUGO,
  964 + show_auto_update_interval, set_auto_update_interval);
865 965  
866 966 static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max,
867 967 set_temp_max, 0);
... ... @@ -1035,6 +1135,7 @@
1035 1135 {
1036 1136 &dev_attr_alarm_mask.attr,
1037 1137 &dev_attr_num_temp_sensors.attr,
  1138 + &dev_attr_auto_update_interval.attr,
1038 1139 &sensor_dev_attr_temp1_max.dev_attr.attr,
1039 1140 &sensor_dev_attr_temp2_max.dev_attr.attr,
1040 1141 &sensor_dev_attr_temp3_max.dev_attr.attr,
... ... @@ -1168,6 +1269,7 @@
1168 1269 }
1169 1270  
1170 1271 data->num_temp_sensors = -1;
  1272 + data->auto_update_interval = AUTO_UPDATE_INTERVAL;
1171 1273  
1172 1274 i2c_set_clientdata(client, data);
1173 1275 mutex_init(&data->lock);
1174 1276  
... ... @@ -1188,8 +1290,16 @@
1188 1290 goto exit_remove;
1189 1291 }
1190 1292  
  1293 + init_completion(&data->auto_update_stop);
  1294 + data->auto_update = kthread_run(adt7470_update_thread, client,
  1295 + dev_name(data->hwmon_dev));
  1296 + if (IS_ERR(data->auto_update))
  1297 + goto exit_unregister;
  1298 +
1191 1299 return 0;
1192 1300  
  1301 +exit_unregister:
  1302 + hwmon_device_unregister(data->hwmon_dev);
1193 1303 exit_remove:
1194 1304 sysfs_remove_group(&client->dev.kobj, &data->attrs);
1195 1305 exit_free:
... ... @@ -1202,6 +1312,8 @@
1202 1312 {
1203 1313 struct adt7470_data *data = i2c_get_clientdata(client);
1204 1314  
  1315 + kthread_stop(data->auto_update);
  1316 + wait_for_completion(&data->auto_update_stop);
1205 1317 hwmon_device_unregister(data->hwmon_dev);
1206 1318 sysfs_remove_group(&client->dev.kobj, &data->attrs);
1207 1319 kfree(data);