adt7475.c (99b8c83a7720da66c9054aa06f5c13c30555c17d) adt7475.c (9ed5bc24fa2b4ebe52b5aec3318babe2aa31795a)
1/*
2 * adt7475 - Thermal sensor driver for the ADT7475 chip and derivatives
3 * Copyright (C) 2007-2008, Advanced Micro Devices, Inc.
4 * Copyright (C) 2008 Jordan Crouse <jordan@cosmicpenguin.net>
5 * Copyright (C) 2008 Hans de Goede <hdegoede@redhat.com>
6 * Copyright (C) 2009 Jean Delvare <khali@linux-fr.org>
7 *
8 * Derived from the lm83 driver by Jean Delvare

--- 18 unchanged lines hidden (view full) ---

27#define MIN 1
28#define MAX 2
29#define CONTROL 3
30#define OFFSET 3
31#define AUTOMIN 4
32#define THERM 5
33#define HYSTERSIS 6
34
1/*
2 * adt7475 - Thermal sensor driver for the ADT7475 chip and derivatives
3 * Copyright (C) 2007-2008, Advanced Micro Devices, Inc.
4 * Copyright (C) 2008 Jordan Crouse <jordan@cosmicpenguin.net>
5 * Copyright (C) 2008 Hans de Goede <hdegoede@redhat.com>
6 * Copyright (C) 2009 Jean Delvare <khali@linux-fr.org>
7 *
8 * Derived from the lm83 driver by Jean Delvare

--- 18 unchanged lines hidden (view full) ---

27#define MIN 1
28#define MAX 2
29#define CONTROL 3
30#define OFFSET 3
31#define AUTOMIN 4
32#define THERM 5
33#define HYSTERSIS 6
34
35/* These are unique identifiers for the sysfs functions - unlike the
36 numbers above, these are not also indexes into an array
37*/
35/*
36 * These are unique identifiers for the sysfs functions - unlike the
37 * numbers above, these are not also indexes into an array
38 */
38
39#define ALARM 9
40#define FAULT 10
41
42/* 7475 Common Registers */
43
44#define REG_DEVREV2 0x12 /* ADT7490 only */
45

--- 237 unchanged lines hidden (view full) ---

283}
284
285static void adt7475_write_word(struct i2c_client *client, int reg, u16 val)
286{
287 i2c_smbus_write_byte_data(client, reg + 1, val >> 8);
288 i2c_smbus_write_byte_data(client, reg, val & 0xFF);
289}
290
39
40#define ALARM 9
41#define FAULT 10
42
43/* 7475 Common Registers */
44
45#define REG_DEVREV2 0x12 /* ADT7490 only */
46

--- 237 unchanged lines hidden (view full) ---

284}
285
286static void adt7475_write_word(struct i2c_client *client, int reg, u16 val)
287{
288 i2c_smbus_write_byte_data(client, reg + 1, val >> 8);
289 i2c_smbus_write_byte_data(client, reg, val & 0xFF);
290}
291
291/* Find the nearest value in a table - used for pwm frequency and
292 auto temp range */
292/*
293 * Find the nearest value in a table - used for pwm frequency and
294 * auto temp range
295 */
293static int find_nearest(long val, const int *array, int size)
294{
295 int i;
296
297 if (val < array[0])
298 return 0;
299
300 if (val > array[size - 1])

--- 79 unchanged lines hidden (view full) ---

380 switch (sattr->nr) {
381 case HYSTERSIS:
382 mutex_lock(&data->lock);
383 out = data->temp[sattr->nr][sattr->index];
384 if (sattr->index != 1)
385 out = (out >> 4) & 0xF;
386 else
387 out = (out & 0xF);
296static int find_nearest(long val, const int *array, int size)
297{
298 int i;
299
300 if (val < array[0])
301 return 0;
302
303 if (val > array[size - 1])

--- 79 unchanged lines hidden (view full) ---

383 switch (sattr->nr) {
384 case HYSTERSIS:
385 mutex_lock(&data->lock);
386 out = data->temp[sattr->nr][sattr->index];
387 if (sattr->index != 1)
388 out = (out >> 4) & 0xF;
389 else
390 out = (out & 0xF);
388 /* Show the value as an absolute number tied to
389 * THERM */
391 /*
392 * Show the value as an absolute number tied to
393 * THERM
394 */
390 out = reg2temp(data, data->temp[THERM][sattr->index]) -
391 out * 1000;
392 mutex_unlock(&data->lock);
393 break;
394
395 case OFFSET:
395 out = reg2temp(data, data->temp[THERM][sattr->index]) -
396 out * 1000;
397 mutex_unlock(&data->lock);
398 break;
399
400 case OFFSET:
396 /* Offset is always 2's complement, regardless of the
397 * setting in CONFIG5 */
401 /*
402 * Offset is always 2's complement, regardless of the
403 * setting in CONFIG5
404 */
398 mutex_lock(&data->lock);
399 out = (s8)data->temp[sattr->nr][sattr->index];
400 if (data->config5 & CONFIG5_TEMPOFFSET)
401 out *= 1000;
402 else
403 out *= 500;
404 mutex_unlock(&data->lock);
405 break;

--- 41 unchanged lines hidden (view full) ---

447 out = data->temp[OFFSET][sattr->index] = val / 1000;
448 } else {
449 val = SENSORS_LIMIT(val, -63000, 64000);
450 out = data->temp[OFFSET][sattr->index] = val / 500;
451 }
452 break;
453
454 case HYSTERSIS:
405 mutex_lock(&data->lock);
406 out = (s8)data->temp[sattr->nr][sattr->index];
407 if (data->config5 & CONFIG5_TEMPOFFSET)
408 out *= 1000;
409 else
410 out *= 500;
411 mutex_unlock(&data->lock);
412 break;

--- 41 unchanged lines hidden (view full) ---

454 out = data->temp[OFFSET][sattr->index] = val / 1000;
455 } else {
456 val = SENSORS_LIMIT(val, -63000, 64000);
457 out = data->temp[OFFSET][sattr->index] = val / 500;
458 }
459 break;
460
461 case HYSTERSIS:
455 /* The value will be given as an absolute value, turn it
456 into an offset based on THERM */
462 /*
463 * The value will be given as an absolute value, turn it
464 * into an offset based on THERM
465 */
457
458 /* Read fresh THERM and HYSTERSIS values from the chip */
459 data->temp[THERM][sattr->index] =
460 adt7475_read(TEMP_THERM_REG(sattr->index)) << 2;
461 adt7475_read_hystersis(client);
462
463 temp = reg2temp(data, data->temp[THERM][sattr->index]);
464 val = SENSORS_LIMIT(val, temp - 15000, temp);

--- 8 unchanged lines hidden (view full) ---

473 }
474
475 out = data->temp[HYSTERSIS][sattr->index];
476 break;
477
478 default:
479 data->temp[sattr->nr][sattr->index] = temp2reg(data, val);
480
466
467 /* Read fresh THERM and HYSTERSIS values from the chip */
468 data->temp[THERM][sattr->index] =
469 adt7475_read(TEMP_THERM_REG(sattr->index)) << 2;
470 adt7475_read_hystersis(client);
471
472 temp = reg2temp(data, data->temp[THERM][sattr->index]);
473 val = SENSORS_LIMIT(val, temp - 15000, temp);

--- 8 unchanged lines hidden (view full) ---

482 }
483
484 out = data->temp[HYSTERSIS][sattr->index];
485 break;
486
487 default:
488 data->temp[sattr->nr][sattr->index] = temp2reg(data, val);
489
481 /* We maintain an extra 2 digits of precision for simplicity
482 * - shift those back off before writing the value */
490 /*
491 * We maintain an extra 2 digits of precision for simplicity
492 * - shift those back off before writing the value
493 */
483 out = (u8) (data->temp[sattr->nr][sattr->index] >> 2);
484 }
485
486 switch (sattr->nr) {
487 case MIN:
488 reg = TEMP_MIN_REG(sattr->index);
489 break;
490 case MAX:

--- 18 unchanged lines hidden (view full) ---

509 }
510
511 i2c_smbus_write_byte_data(client, reg, out);
512
513 mutex_unlock(&data->lock);
514 return count;
515}
516
494 out = (u8) (data->temp[sattr->nr][sattr->index] >> 2);
495 }
496
497 switch (sattr->nr) {
498 case MIN:
499 reg = TEMP_MIN_REG(sattr->index);
500 break;
501 case MAX:

--- 18 unchanged lines hidden (view full) ---

520 }
521
522 i2c_smbus_write_byte_data(client, reg, out);
523
524 mutex_unlock(&data->lock);
525 return count;
526}
527
517/* Table of autorange values - the user will write the value in millidegrees,
518 and we'll convert it */
528/*
529 * Table of autorange values - the user will write the value in millidegrees,
530 * and we'll convert it
531 */
519static const int autorange_table[] = {
520 2000, 2500, 3330, 4000, 5000, 6670, 8000,
521 10000, 13330, 16000, 20000, 26670, 32000, 40000,
522 53330, 80000
523};
524
525static ssize_t show_point2(struct device *dev, struct device_attribute *attr,
526 char *buf)

--- 26 unchanged lines hidden (view full) ---

553
554 /* Get a fresh copy of the needed registers */
555 data->config5 = adt7475_read(REG_CONFIG5);
556 data->temp[AUTOMIN][sattr->index] =
557 adt7475_read(TEMP_TMIN_REG(sattr->index)) << 2;
558 data->range[sattr->index] =
559 adt7475_read(TEMP_TRANGE_REG(sattr->index));
560
532static const int autorange_table[] = {
533 2000, 2500, 3330, 4000, 5000, 6670, 8000,
534 10000, 13330, 16000, 20000, 26670, 32000, 40000,
535 53330, 80000
536};
537
538static ssize_t show_point2(struct device *dev, struct device_attribute *attr,
539 char *buf)

--- 26 unchanged lines hidden (view full) ---

566
567 /* Get a fresh copy of the needed registers */
568 data->config5 = adt7475_read(REG_CONFIG5);
569 data->temp[AUTOMIN][sattr->index] =
570 adt7475_read(TEMP_TMIN_REG(sattr->index)) << 2;
571 data->range[sattr->index] =
572 adt7475_read(TEMP_TRANGE_REG(sattr->index));
573
561 /* The user will write an absolute value, so subtract the start point
562 to figure the range */
574 /*
575 * The user will write an absolute value, so subtract the start point
576 * to figure the range
577 */
563 temp = reg2temp(data, data->temp[AUTOMIN][sattr->index]);
564 val = SENSORS_LIMIT(val, temp + autorange_table[0],
565 temp + autorange_table[ARRAY_SIZE(autorange_table) - 1]);
566 val -= temp;
567
568 /* Find the nearest table entry to what the user wrote */
569 val = find_nearest(val, autorange_table, ARRAY_SIZE(autorange_table));
570

--- 88 unchanged lines hidden (view full) ---

659 mutex_lock(&data->lock);
660
661 switch (sattr->nr) {
662 case INPUT:
663 /* Get a fresh value for CONTROL */
664 data->pwm[CONTROL][sattr->index] =
665 adt7475_read(PWM_CONFIG_REG(sattr->index));
666
578 temp = reg2temp(data, data->temp[AUTOMIN][sattr->index]);
579 val = SENSORS_LIMIT(val, temp + autorange_table[0],
580 temp + autorange_table[ARRAY_SIZE(autorange_table) - 1]);
581 val -= temp;
582
583 /* Find the nearest table entry to what the user wrote */
584 val = find_nearest(val, autorange_table, ARRAY_SIZE(autorange_table));
585

--- 88 unchanged lines hidden (view full) ---

674 mutex_lock(&data->lock);
675
676 switch (sattr->nr) {
677 case INPUT:
678 /* Get a fresh value for CONTROL */
679 data->pwm[CONTROL][sattr->index] =
680 adt7475_read(PWM_CONFIG_REG(sattr->index));
681
667 /* If we are not in manual mode, then we shouldn't allow
668 * the user to set the pwm speed */
682 /*
683 * If we are not in manual mode, then we shouldn't allow
684 * the user to set the pwm speed
685 */
669 if (((data->pwm[CONTROL][sattr->index] >> 5) & 7) != 7) {
670 mutex_unlock(&data->lock);
671 return count;
672 }
673
674 reg = PWM_REG(sattr->index);
675 break;
676

--- 598 unchanged lines hidden (view full) ---

1275 if (id->driver_data == adt7473 && revision >= 1)
1276 data->has_pwm2 = !data->has_pwm2;
1277
1278 data->config4 = adt7475_read(REG_CONFIG4);
1279 /* Pin TACH4 may alternatively be used for THERM */
1280 if ((data->config4 & CONFIG4_PINFUNC) == 0x0)
1281 data->has_fan4 = 1;
1282
686 if (((data->pwm[CONTROL][sattr->index] >> 5) & 7) != 7) {
687 mutex_unlock(&data->lock);
688 return count;
689 }
690
691 reg = PWM_REG(sattr->index);
692 break;
693

--- 598 unchanged lines hidden (view full) ---

1292 if (id->driver_data == adt7473 && revision >= 1)
1293 data->has_pwm2 = !data->has_pwm2;
1294
1295 data->config4 = adt7475_read(REG_CONFIG4);
1296 /* Pin TACH4 may alternatively be used for THERM */
1297 if ((data->config4 & CONFIG4_PINFUNC) == 0x0)
1298 data->has_fan4 = 1;
1299
1283 /* THERM configuration is more complex on the ADT7476 and ADT7490,
1284 because 2 different pins (TACH4 and +2.5 Vin) can be used for
1285 this function */
1300 /*
1301 * THERM configuration is more complex on the ADT7476 and ADT7490,
1302 * because 2 different pins (TACH4 and +2.5 Vin) can be used for
1303 * this function
1304 */
1286 if (id->driver_data == adt7490) {
1287 if ((data->config4 & CONFIG4_PINFUNC) == 0x1 &&
1288 !(config3 & CONFIG3_THERM))
1289 data->has_fan4 = 1;
1290 }
1291 if (id->driver_data == adt7476 || id->driver_data == adt7490) {
1292 if (!(config3 & CONFIG3_THERM) ||
1293 (data->config4 & CONFIG4_PINFUNC) == 0x1)
1294 data->has_voltage |= (1 << 0); /* in0 */
1295 }
1296
1305 if (id->driver_data == adt7490) {
1306 if ((data->config4 & CONFIG4_PINFUNC) == 0x1 &&
1307 !(config3 & CONFIG3_THERM))
1308 data->has_fan4 = 1;
1309 }
1310 if (id->driver_data == adt7476 || id->driver_data == adt7490) {
1311 if (!(config3 & CONFIG3_THERM) ||
1312 (data->config4 & CONFIG4_PINFUNC) == 0x1)
1313 data->has_voltage |= (1 << 0); /* in0 */
1314 }
1315
1297 /* On the ADT7476, the +12V input pin may instead be used as VID5,
1298 and VID pins may alternatively be used as GPIO */
1316 /*
1317 * On the ADT7476, the +12V input pin may instead be used as VID5,
1318 * and VID pins may alternatively be used as GPIO
1319 */
1299 if (id->driver_data == adt7476) {
1300 u8 vid = adt7475_read(REG_VID);
1301 if (!(vid & VID_VIDSEL))
1302 data->has_voltage |= (1 << 4); /* in4 */
1303
1304 data->has_vid = !(adt7475_read(REG_CONFIG5) & CONFIG5_VIDGPIO);
1305 }
1306
1307 /* Voltage attenuators can be bypassed, globally or individually */
1308 config2 = adt7475_read(REG_CONFIG2);
1309 if (config2 & CONFIG2_ATTN) {
1310 data->bypass_attn = (0x3 << 3) | 0x3;
1311 } else {
1312 data->bypass_attn = ((data->config4 & CONFIG4_ATTN_IN10) >> 4) |
1313 ((data->config4 & CONFIG4_ATTN_IN43) >> 3);
1314 }
1315 data->bypass_attn &= data->has_voltage;
1316
1320 if (id->driver_data == adt7476) {
1321 u8 vid = adt7475_read(REG_VID);
1322 if (!(vid & VID_VIDSEL))
1323 data->has_voltage |= (1 << 4); /* in4 */
1324
1325 data->has_vid = !(adt7475_read(REG_CONFIG5) & CONFIG5_VIDGPIO);
1326 }
1327
1328 /* Voltage attenuators can be bypassed, globally or individually */
1329 config2 = adt7475_read(REG_CONFIG2);
1330 if (config2 & CONFIG2_ATTN) {
1331 data->bypass_attn = (0x3 << 3) | 0x3;
1332 } else {
1333 data->bypass_attn = ((data->config4 & CONFIG4_ATTN_IN10) >> 4) |
1334 ((data->config4 & CONFIG4_ATTN_IN43) >> 3);
1335 }
1336 data->bypass_attn &= data->has_voltage;
1337
1317 /* Call adt7475_read_pwm for all pwm's as this will reprogram any
1318 pwm's which are disabled to manual mode with 0% duty cycle */
1338 /*
1339 * Call adt7475_read_pwm for all pwm's as this will reprogram any
1340 * pwm's which are disabled to manual mode with 0% duty cycle
1341 */
1319 for (i = 0; i < ADT7475_PWM_COUNT; i++)
1320 adt7475_read_pwm(client, i);
1321
1322 ret = sysfs_create_group(&client->dev.kobj, &adt7475_attr_group);
1323 if (ret)
1324 goto efree;
1325
1326 /* Features that can be disabled individually */

--- 99 unchanged lines hidden (view full) ---

1426
1427static void adt7475_read_pwm(struct i2c_client *client, int index)
1428{
1429 struct adt7475_data *data = i2c_get_clientdata(client);
1430 unsigned int v;
1431
1432 data->pwm[CONTROL][index] = adt7475_read(PWM_CONFIG_REG(index));
1433
1342 for (i = 0; i < ADT7475_PWM_COUNT; i++)
1343 adt7475_read_pwm(client, i);
1344
1345 ret = sysfs_create_group(&client->dev.kobj, &adt7475_attr_group);
1346 if (ret)
1347 goto efree;
1348
1349 /* Features that can be disabled individually */

--- 99 unchanged lines hidden (view full) ---

1449
1450static void adt7475_read_pwm(struct i2c_client *client, int index)
1451{
1452 struct adt7475_data *data = i2c_get_clientdata(client);
1453 unsigned int v;
1454
1455 data->pwm[CONTROL][index] = adt7475_read(PWM_CONFIG_REG(index));
1456
1434 /* Figure out the internal value for pwmctrl and pwmchan
1435 based on the current settings */
1457 /*
1458 * Figure out the internal value for pwmctrl and pwmchan
1459 * based on the current settings
1460 */
1436 v = (data->pwm[CONTROL][index] >> 5) & 7;
1437
1438 if (v == 3)
1439 data->pwmctl[index] = 0;
1440 else if (v == 7)
1441 data->pwmctl[index] = 1;
1442 else if (v == 4) {
1461 v = (data->pwm[CONTROL][index] >> 5) & 7;
1462
1463 if (v == 3)
1464 data->pwmctl[index] = 0;
1465 else if (v == 7)
1466 data->pwmctl[index] = 1;
1467 else if (v == 4) {
1443 /* The fan is disabled - we don't want to
1444 support that, so change to manual mode and
1445 set the duty cycle to 0 instead
1446 */
1468 /*
1469 * The fan is disabled - we don't want to
1470 * support that, so change to manual mode and
1471 * set the duty cycle to 0 instead
1472 */
1447 data->pwm[INPUT][index] = 0;
1448 data->pwm[CONTROL][index] &= ~0xE0;
1449 data->pwm[CONTROL][index] |= (7 << 5);
1450
1451 i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
1452 data->pwm[INPUT][index]);
1453
1454 i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),

--- 164 unchanged lines hidden ---
1473 data->pwm[INPUT][index] = 0;
1474 data->pwm[CONTROL][index] &= ~0xE0;
1475 data->pwm[CONTROL][index] |= (7 << 5);
1476
1477 i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
1478 data->pwm[INPUT][index]);
1479
1480 i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),

--- 164 unchanged lines hidden ---