11c301fc5SJordan Crouse /* 21c301fc5SJordan Crouse * adt7475 - Thermal sensor driver for the ADT7475 chip and derivatives 31c301fc5SJordan Crouse * Copyright (C) 2007-2008, Advanced Micro Devices, Inc. 41c301fc5SJordan Crouse * Copyright (C) 2008 Jordan Crouse <jordan@cosmicpenguin.net> 51c301fc5SJordan Crouse * Copyright (C) 2008 Hans de Goede <hdegoede@redhat.com> 61c301fc5SJordan Crouse 71c301fc5SJordan Crouse * Derived from the lm83 driver by Jean Delvare 81c301fc5SJordan Crouse * 91c301fc5SJordan Crouse * This program is free software; you can redistribute it and/or modify 101c301fc5SJordan Crouse * it under the terms of the GNU General Public License version 2 as 111c301fc5SJordan Crouse * published by the Free Software Foundation. 121c301fc5SJordan Crouse */ 131c301fc5SJordan Crouse 141c301fc5SJordan Crouse #include <linux/module.h> 151c301fc5SJordan Crouse #include <linux/init.h> 161c301fc5SJordan Crouse #include <linux/slab.h> 171c301fc5SJordan Crouse #include <linux/i2c.h> 181c301fc5SJordan Crouse #include <linux/hwmon.h> 191c301fc5SJordan Crouse #include <linux/hwmon-sysfs.h> 201c301fc5SJordan Crouse #include <linux/err.h> 211c301fc5SJordan Crouse 221c301fc5SJordan Crouse /* Indexes for the sysfs hooks */ 231c301fc5SJordan Crouse 241c301fc5SJordan Crouse #define INPUT 0 251c301fc5SJordan Crouse #define MIN 1 261c301fc5SJordan Crouse #define MAX 2 271c301fc5SJordan Crouse #define CONTROL 3 281c301fc5SJordan Crouse #define OFFSET 3 291c301fc5SJordan Crouse #define AUTOMIN 4 301c301fc5SJordan Crouse #define THERM 5 311c301fc5SJordan Crouse #define HYSTERSIS 6 321c301fc5SJordan Crouse 331c301fc5SJordan Crouse /* These are unique identifiers for the sysfs functions - unlike the 341c301fc5SJordan Crouse numbers above, these are not also indexes into an array 351c301fc5SJordan Crouse */ 361c301fc5SJordan Crouse 371c301fc5SJordan Crouse #define ALARM 9 381c301fc5SJordan Crouse #define FAULT 10 391c301fc5SJordan Crouse 401c301fc5SJordan Crouse /* 7475 Common Registers */ 411c301fc5SJordan Crouse 421c301fc5SJordan Crouse #define REG_VOLTAGE_BASE 0x21 431c301fc5SJordan Crouse #define REG_TEMP_BASE 0x25 441c301fc5SJordan Crouse #define REG_TACH_BASE 0x28 451c301fc5SJordan Crouse #define REG_PWM_BASE 0x30 461c301fc5SJordan Crouse #define REG_PWM_MAX_BASE 0x38 471c301fc5SJordan Crouse 481c301fc5SJordan Crouse #define REG_DEVID 0x3D 491c301fc5SJordan Crouse #define REG_VENDID 0x3E 501c301fc5SJordan Crouse 511c301fc5SJordan Crouse #define REG_STATUS1 0x41 521c301fc5SJordan Crouse #define REG_STATUS2 0x42 531c301fc5SJordan Crouse 541c301fc5SJordan Crouse #define REG_VOLTAGE_MIN_BASE 0x46 551c301fc5SJordan Crouse #define REG_VOLTAGE_MAX_BASE 0x47 561c301fc5SJordan Crouse 571c301fc5SJordan Crouse #define REG_TEMP_MIN_BASE 0x4E 581c301fc5SJordan Crouse #define REG_TEMP_MAX_BASE 0x4F 591c301fc5SJordan Crouse 601c301fc5SJordan Crouse #define REG_TACH_MIN_BASE 0x54 611c301fc5SJordan Crouse 621c301fc5SJordan Crouse #define REG_PWM_CONFIG_BASE 0x5C 631c301fc5SJordan Crouse 641c301fc5SJordan Crouse #define REG_TEMP_TRANGE_BASE 0x5F 651c301fc5SJordan Crouse 661c301fc5SJordan Crouse #define REG_PWM_MIN_BASE 0x64 671c301fc5SJordan Crouse 681c301fc5SJordan Crouse #define REG_TEMP_TMIN_BASE 0x67 691c301fc5SJordan Crouse #define REG_TEMP_THERM_BASE 0x6A 701c301fc5SJordan Crouse 711c301fc5SJordan Crouse #define REG_REMOTE1_HYSTERSIS 0x6D 721c301fc5SJordan Crouse #define REG_REMOTE2_HYSTERSIS 0x6E 731c301fc5SJordan Crouse 741c301fc5SJordan Crouse #define REG_TEMP_OFFSET_BASE 0x70 751c301fc5SJordan Crouse 761c301fc5SJordan Crouse #define REG_EXTEND1 0x76 771c301fc5SJordan Crouse #define REG_EXTEND2 0x77 781c301fc5SJordan Crouse #define REG_CONFIG5 0x7C 791c301fc5SJordan Crouse 801c301fc5SJordan Crouse #define CONFIG5_TWOSCOMP 0x01 811c301fc5SJordan Crouse #define CONFIG5_TEMPOFFSET 0x02 821c301fc5SJordan Crouse 831c301fc5SJordan Crouse /* ADT7475 Settings */ 841c301fc5SJordan Crouse 851c301fc5SJordan Crouse #define ADT7475_VOLTAGE_COUNT 2 861c301fc5SJordan Crouse #define ADT7475_TEMP_COUNT 3 871c301fc5SJordan Crouse #define ADT7475_TACH_COUNT 4 881c301fc5SJordan Crouse #define ADT7475_PWM_COUNT 3 891c301fc5SJordan Crouse 901c301fc5SJordan Crouse /* Macro to read the registers */ 911c301fc5SJordan Crouse 921c301fc5SJordan Crouse #define adt7475_read(reg) i2c_smbus_read_byte_data(client, (reg)) 931c301fc5SJordan Crouse 941c301fc5SJordan Crouse /* Macros to easily index the registers */ 951c301fc5SJordan Crouse 961c301fc5SJordan Crouse #define TACH_REG(idx) (REG_TACH_BASE + ((idx) * 2)) 971c301fc5SJordan Crouse #define TACH_MIN_REG(idx) (REG_TACH_MIN_BASE + ((idx) * 2)) 981c301fc5SJordan Crouse 991c301fc5SJordan Crouse #define PWM_REG(idx) (REG_PWM_BASE + (idx)) 1001c301fc5SJordan Crouse #define PWM_MAX_REG(idx) (REG_PWM_MAX_BASE + (idx)) 1011c301fc5SJordan Crouse #define PWM_MIN_REG(idx) (REG_PWM_MIN_BASE + (idx)) 1021c301fc5SJordan Crouse #define PWM_CONFIG_REG(idx) (REG_PWM_CONFIG_BASE + (idx)) 1031c301fc5SJordan Crouse 1041c301fc5SJordan Crouse #define VOLTAGE_REG(idx) (REG_VOLTAGE_BASE + (idx)) 1051c301fc5SJordan Crouse #define VOLTAGE_MIN_REG(idx) (REG_VOLTAGE_MIN_BASE + ((idx) * 2)) 1061c301fc5SJordan Crouse #define VOLTAGE_MAX_REG(idx) (REG_VOLTAGE_MAX_BASE + ((idx) * 2)) 1071c301fc5SJordan Crouse 1081c301fc5SJordan Crouse #define TEMP_REG(idx) (REG_TEMP_BASE + (idx)) 1091c301fc5SJordan Crouse #define TEMP_MIN_REG(idx) (REG_TEMP_MIN_BASE + ((idx) * 2)) 1101c301fc5SJordan Crouse #define TEMP_MAX_REG(idx) (REG_TEMP_MAX_BASE + ((idx) * 2)) 1111c301fc5SJordan Crouse #define TEMP_TMIN_REG(idx) (REG_TEMP_TMIN_BASE + (idx)) 1121c301fc5SJordan Crouse #define TEMP_THERM_REG(idx) (REG_TEMP_THERM_BASE + (idx)) 1131c301fc5SJordan Crouse #define TEMP_OFFSET_REG(idx) (REG_TEMP_OFFSET_BASE + (idx)) 1141c301fc5SJordan Crouse #define TEMP_TRANGE_REG(idx) (REG_TEMP_TRANGE_BASE + (idx)) 1151c301fc5SJordan Crouse 1161c301fc5SJordan Crouse static unsigned short normal_i2c[] = { 0x2e, I2C_CLIENT_END }; 1171c301fc5SJordan Crouse 1181c301fc5SJordan Crouse I2C_CLIENT_INSMOD_1(adt7475); 1191c301fc5SJordan Crouse 1201c301fc5SJordan Crouse static const struct i2c_device_id adt7475_id[] = { 1211c301fc5SJordan Crouse { "adt7475", adt7475 }, 1221c301fc5SJordan Crouse { } 1231c301fc5SJordan Crouse }; 1241c301fc5SJordan Crouse MODULE_DEVICE_TABLE(i2c, adt7475_id); 1251c301fc5SJordan Crouse 1261c301fc5SJordan Crouse struct adt7475_data { 1271c301fc5SJordan Crouse struct device *hwmon_dev; 1281c301fc5SJordan Crouse struct mutex lock; 1291c301fc5SJordan Crouse 1301c301fc5SJordan Crouse unsigned long measure_updated; 1311c301fc5SJordan Crouse unsigned long limits_updated; 1321c301fc5SJordan Crouse char valid; 1331c301fc5SJordan Crouse 1341c301fc5SJordan Crouse u8 config5; 1351c301fc5SJordan Crouse u16 alarms; 1361c301fc5SJordan Crouse u16 voltage[3][3]; 1371c301fc5SJordan Crouse u16 temp[7][3]; 1381c301fc5SJordan Crouse u16 tach[2][4]; 1391c301fc5SJordan Crouse u8 pwm[4][3]; 1401c301fc5SJordan Crouse u8 range[3]; 1411c301fc5SJordan Crouse u8 pwmctl[3]; 1421c301fc5SJordan Crouse u8 pwmchan[3]; 1431c301fc5SJordan Crouse }; 1441c301fc5SJordan Crouse 1451c301fc5SJordan Crouse static struct i2c_driver adt7475_driver; 1461c301fc5SJordan Crouse static struct adt7475_data *adt7475_update_device(struct device *dev); 1471c301fc5SJordan Crouse static void adt7475_read_hystersis(struct i2c_client *client); 1481c301fc5SJordan Crouse static void adt7475_read_pwm(struct i2c_client *client, int index); 1491c301fc5SJordan Crouse 1501c301fc5SJordan Crouse /* Given a temp value, convert it to register value */ 1511c301fc5SJordan Crouse 1521c301fc5SJordan Crouse static inline u16 temp2reg(struct adt7475_data *data, long val) 1531c301fc5SJordan Crouse { 1541c301fc5SJordan Crouse u16 ret; 1551c301fc5SJordan Crouse 1561c301fc5SJordan Crouse if (!(data->config5 & CONFIG5_TWOSCOMP)) { 1571c301fc5SJordan Crouse val = SENSORS_LIMIT(val, -64000, 191000); 1581c301fc5SJordan Crouse ret = (val + 64500) / 1000; 1591c301fc5SJordan Crouse } else { 1601c301fc5SJordan Crouse val = SENSORS_LIMIT(val, -128000, 127000); 1611c301fc5SJordan Crouse if (val < -500) 1621c301fc5SJordan Crouse ret = (256500 + val) / 1000; 1631c301fc5SJordan Crouse else 1641c301fc5SJordan Crouse ret = (val + 500) / 1000; 1651c301fc5SJordan Crouse } 1661c301fc5SJordan Crouse 1671c301fc5SJordan Crouse return ret << 2; 1681c301fc5SJordan Crouse } 1691c301fc5SJordan Crouse 1701c301fc5SJordan Crouse /* Given a register value, convert it to a real temp value */ 1711c301fc5SJordan Crouse 1721c301fc5SJordan Crouse static inline int reg2temp(struct adt7475_data *data, u16 reg) 1731c301fc5SJordan Crouse { 1741c301fc5SJordan Crouse if (data->config5 & CONFIG5_TWOSCOMP) { 1751c301fc5SJordan Crouse if (reg >= 512) 1761c301fc5SJordan Crouse return (reg - 1024) * 250; 1771c301fc5SJordan Crouse else 1781c301fc5SJordan Crouse return reg * 250; 1791c301fc5SJordan Crouse } else 1801c301fc5SJordan Crouse return (reg - 256) * 250; 1811c301fc5SJordan Crouse } 1821c301fc5SJordan Crouse 1831c301fc5SJordan Crouse static inline int tach2rpm(u16 tach) 1841c301fc5SJordan Crouse { 1851c301fc5SJordan Crouse if (tach == 0 || tach == 0xFFFF) 1861c301fc5SJordan Crouse return 0; 1871c301fc5SJordan Crouse 1881c301fc5SJordan Crouse return (90000 * 60) / tach; 1891c301fc5SJordan Crouse } 1901c301fc5SJordan Crouse 1911c301fc5SJordan Crouse static inline u16 rpm2tach(unsigned long rpm) 1921c301fc5SJordan Crouse { 1931c301fc5SJordan Crouse if (rpm == 0) 1941c301fc5SJordan Crouse return 0; 1951c301fc5SJordan Crouse 1961c301fc5SJordan Crouse return SENSORS_LIMIT((90000 * 60) / rpm, 1, 0xFFFF); 1971c301fc5SJordan Crouse } 1981c301fc5SJordan Crouse 1991c301fc5SJordan Crouse static inline int reg2vcc(u16 reg) 2001c301fc5SJordan Crouse { 2011c301fc5SJordan Crouse return (4296 * reg) / 1000; 2021c301fc5SJordan Crouse } 2031c301fc5SJordan Crouse 2041c301fc5SJordan Crouse static inline int reg2vccp(u16 reg) 2051c301fc5SJordan Crouse { 2061c301fc5SJordan Crouse return (2929 * reg) / 1000; 2071c301fc5SJordan Crouse } 2081c301fc5SJordan Crouse 2091c301fc5SJordan Crouse static inline u16 vcc2reg(long vcc) 2101c301fc5SJordan Crouse { 2111c301fc5SJordan Crouse vcc = SENSORS_LIMIT(vcc, 0, 4396); 2121c301fc5SJordan Crouse return (vcc * 1000) / 4296; 2131c301fc5SJordan Crouse } 2141c301fc5SJordan Crouse 2151c301fc5SJordan Crouse static inline u16 vccp2reg(long vcc) 2161c301fc5SJordan Crouse { 2171c301fc5SJordan Crouse vcc = SENSORS_LIMIT(vcc, 0, 2998); 2181c301fc5SJordan Crouse return (vcc * 1000) / 2929; 2191c301fc5SJordan Crouse } 2201c301fc5SJordan Crouse 2211c301fc5SJordan Crouse static u16 adt7475_read_word(struct i2c_client *client, int reg) 2221c301fc5SJordan Crouse { 2231c301fc5SJordan Crouse u16 val; 2241c301fc5SJordan Crouse 2251c301fc5SJordan Crouse val = i2c_smbus_read_byte_data(client, reg); 2261c301fc5SJordan Crouse val |= (i2c_smbus_read_byte_data(client, reg + 1) << 8); 2271c301fc5SJordan Crouse 2281c301fc5SJordan Crouse return val; 2291c301fc5SJordan Crouse } 2301c301fc5SJordan Crouse 2311c301fc5SJordan Crouse static void adt7475_write_word(struct i2c_client *client, int reg, u16 val) 2321c301fc5SJordan Crouse { 2331c301fc5SJordan Crouse i2c_smbus_write_byte_data(client, reg + 1, val >> 8); 2341c301fc5SJordan Crouse i2c_smbus_write_byte_data(client, reg, val & 0xFF); 2351c301fc5SJordan Crouse } 2361c301fc5SJordan Crouse 2371c301fc5SJordan Crouse /* Find the nearest value in a table - used for pwm frequency and 2381c301fc5SJordan Crouse auto temp range */ 2391c301fc5SJordan Crouse static int find_nearest(long val, const int *array, int size) 2401c301fc5SJordan Crouse { 2411c301fc5SJordan Crouse int i; 2421c301fc5SJordan Crouse 2431c301fc5SJordan Crouse if (val < array[0]) 2441c301fc5SJordan Crouse return 0; 2451c301fc5SJordan Crouse 2461c301fc5SJordan Crouse if (val > array[size - 1]) 2471c301fc5SJordan Crouse return size - 1; 2481c301fc5SJordan Crouse 2491c301fc5SJordan Crouse for (i = 0; i < size - 1; i++) { 2501c301fc5SJordan Crouse int a, b; 2511c301fc5SJordan Crouse 2521c301fc5SJordan Crouse if (val > array[i + 1]) 2531c301fc5SJordan Crouse continue; 2541c301fc5SJordan Crouse 2551c301fc5SJordan Crouse a = val - array[i]; 2561c301fc5SJordan Crouse b = array[i + 1] - val; 2571c301fc5SJordan Crouse 2581c301fc5SJordan Crouse return (a <= b) ? i : i + 1; 2591c301fc5SJordan Crouse } 2601c301fc5SJordan Crouse 2611c301fc5SJordan Crouse return 0; 2621c301fc5SJordan Crouse } 2631c301fc5SJordan Crouse 2641c301fc5SJordan Crouse static ssize_t show_voltage(struct device *dev, struct device_attribute *attr, 2651c301fc5SJordan Crouse char *buf) 2661c301fc5SJordan Crouse { 2671c301fc5SJordan Crouse struct adt7475_data *data = adt7475_update_device(dev); 2681c301fc5SJordan Crouse struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); 2691c301fc5SJordan Crouse unsigned short val; 2701c301fc5SJordan Crouse 2711c301fc5SJordan Crouse switch (sattr->nr) { 2721c301fc5SJordan Crouse case ALARM: 2731c301fc5SJordan Crouse return sprintf(buf, "%d\n", 2741c301fc5SJordan Crouse (data->alarms >> (sattr->index + 1)) & 1); 2751c301fc5SJordan Crouse default: 2761c301fc5SJordan Crouse val = data->voltage[sattr->nr][sattr->index]; 2771c301fc5SJordan Crouse return sprintf(buf, "%d\n", 2781c301fc5SJordan Crouse sattr->index == 2791c301fc5SJordan Crouse 0 ? reg2vccp(val) : reg2vcc(val)); 2801c301fc5SJordan Crouse } 2811c301fc5SJordan Crouse } 2821c301fc5SJordan Crouse 2831c301fc5SJordan Crouse static ssize_t set_voltage(struct device *dev, struct device_attribute *attr, 2841c301fc5SJordan Crouse const char *buf, size_t count) 2851c301fc5SJordan Crouse { 2861c301fc5SJordan Crouse 2871c301fc5SJordan Crouse struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); 2881c301fc5SJordan Crouse struct i2c_client *client = to_i2c_client(dev); 2891c301fc5SJordan Crouse struct adt7475_data *data = i2c_get_clientdata(client); 2901c301fc5SJordan Crouse unsigned char reg; 2911c301fc5SJordan Crouse long val; 2921c301fc5SJordan Crouse 2931c301fc5SJordan Crouse if (strict_strtol(buf, 10, &val)) 2941c301fc5SJordan Crouse return -EINVAL; 2951c301fc5SJordan Crouse 2961c301fc5SJordan Crouse mutex_lock(&data->lock); 2971c301fc5SJordan Crouse 2981c301fc5SJordan Crouse data->voltage[sattr->nr][sattr->index] = 2991c301fc5SJordan Crouse sattr->index ? vcc2reg(val) : vccp2reg(val); 3001c301fc5SJordan Crouse 3011c301fc5SJordan Crouse if (sattr->nr == MIN) 3021c301fc5SJordan Crouse reg = VOLTAGE_MIN_REG(sattr->index); 3031c301fc5SJordan Crouse else 3041c301fc5SJordan Crouse reg = VOLTAGE_MAX_REG(sattr->index); 3051c301fc5SJordan Crouse 3061c301fc5SJordan Crouse i2c_smbus_write_byte_data(client, reg, 3071c301fc5SJordan Crouse data->voltage[sattr->nr][sattr->index] >> 2); 3081c301fc5SJordan Crouse mutex_unlock(&data->lock); 3091c301fc5SJordan Crouse 3101c301fc5SJordan Crouse return count; 3111c301fc5SJordan Crouse } 3121c301fc5SJordan Crouse 3131c301fc5SJordan Crouse static ssize_t show_temp(struct device *dev, struct device_attribute *attr, 3141c301fc5SJordan Crouse char *buf) 3151c301fc5SJordan Crouse { 3161c301fc5SJordan Crouse struct adt7475_data *data = adt7475_update_device(dev); 3171c301fc5SJordan Crouse struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); 3181c301fc5SJordan Crouse int out; 3191c301fc5SJordan Crouse 3201c301fc5SJordan Crouse switch (sattr->nr) { 3211c301fc5SJordan Crouse case HYSTERSIS: 3221c301fc5SJordan Crouse mutex_lock(&data->lock); 3231c301fc5SJordan Crouse out = data->temp[sattr->nr][sattr->index]; 3241c301fc5SJordan Crouse if (sattr->index != 1) 3251c301fc5SJordan Crouse out = (out >> 4) & 0xF; 3261c301fc5SJordan Crouse else 3271c301fc5SJordan Crouse out = (out & 0xF); 3281c301fc5SJordan Crouse /* Show the value as an absolute number tied to 3291c301fc5SJordan Crouse * THERM */ 3301c301fc5SJordan Crouse out = reg2temp(data, data->temp[THERM][sattr->index]) - 3311c301fc5SJordan Crouse out * 1000; 3321c301fc5SJordan Crouse mutex_unlock(&data->lock); 3331c301fc5SJordan Crouse break; 3341c301fc5SJordan Crouse 3351c301fc5SJordan Crouse case OFFSET: 3361c301fc5SJordan Crouse /* Offset is always 2's complement, regardless of the 3371c301fc5SJordan Crouse * setting in CONFIG5 */ 3381c301fc5SJordan Crouse mutex_lock(&data->lock); 3391c301fc5SJordan Crouse out = (s8)data->temp[sattr->nr][sattr->index]; 3401c301fc5SJordan Crouse if (data->config5 & CONFIG5_TEMPOFFSET) 3411c301fc5SJordan Crouse out *= 1000; 3421c301fc5SJordan Crouse else 3431c301fc5SJordan Crouse out *= 500; 3441c301fc5SJordan Crouse mutex_unlock(&data->lock); 3451c301fc5SJordan Crouse break; 3461c301fc5SJordan Crouse 3471c301fc5SJordan Crouse case ALARM: 3481c301fc5SJordan Crouse out = (data->alarms >> (sattr->index + 4)) & 1; 3491c301fc5SJordan Crouse break; 3501c301fc5SJordan Crouse 3511c301fc5SJordan Crouse case FAULT: 3521c301fc5SJordan Crouse /* Note - only for remote1 and remote2 */ 353*cf312e07SJean Delvare out = !!(data->alarms & (sattr->index ? 0x8000 : 0x4000)); 3541c301fc5SJordan Crouse break; 3551c301fc5SJordan Crouse 3561c301fc5SJordan Crouse default: 3571c301fc5SJordan Crouse /* All other temp values are in the configured format */ 3581c301fc5SJordan Crouse out = reg2temp(data, data->temp[sattr->nr][sattr->index]); 3591c301fc5SJordan Crouse } 3601c301fc5SJordan Crouse 3611c301fc5SJordan Crouse return sprintf(buf, "%d\n", out); 3621c301fc5SJordan Crouse } 3631c301fc5SJordan Crouse 3641c301fc5SJordan Crouse static ssize_t set_temp(struct device *dev, struct device_attribute *attr, 3651c301fc5SJordan Crouse const char *buf, size_t count) 3661c301fc5SJordan Crouse { 3671c301fc5SJordan Crouse struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); 3681c301fc5SJordan Crouse struct i2c_client *client = to_i2c_client(dev); 3691c301fc5SJordan Crouse struct adt7475_data *data = i2c_get_clientdata(client); 3701c301fc5SJordan Crouse unsigned char reg = 0; 3711c301fc5SJordan Crouse u8 out; 3721c301fc5SJordan Crouse int temp; 3731c301fc5SJordan Crouse long val; 3741c301fc5SJordan Crouse 3751c301fc5SJordan Crouse if (strict_strtol(buf, 10, &val)) 3761c301fc5SJordan Crouse return -EINVAL; 3771c301fc5SJordan Crouse 3781c301fc5SJordan Crouse mutex_lock(&data->lock); 3791c301fc5SJordan Crouse 3801c301fc5SJordan Crouse /* We need the config register in all cases for temp <-> reg conv. */ 3811c301fc5SJordan Crouse data->config5 = adt7475_read(REG_CONFIG5); 3821c301fc5SJordan Crouse 3831c301fc5SJordan Crouse switch (sattr->nr) { 3841c301fc5SJordan Crouse case OFFSET: 3851c301fc5SJordan Crouse if (data->config5 & CONFIG5_TEMPOFFSET) { 3861c301fc5SJordan Crouse val = SENSORS_LIMIT(val, -63000, 127000); 3871c301fc5SJordan Crouse out = data->temp[OFFSET][sattr->index] = val / 1000; 3881c301fc5SJordan Crouse } else { 3891c301fc5SJordan Crouse val = SENSORS_LIMIT(val, -63000, 64000); 3901c301fc5SJordan Crouse out = data->temp[OFFSET][sattr->index] = val / 500; 3911c301fc5SJordan Crouse } 3921c301fc5SJordan Crouse break; 3931c301fc5SJordan Crouse 3941c301fc5SJordan Crouse case HYSTERSIS: 3951c301fc5SJordan Crouse /* The value will be given as an absolute value, turn it 3961c301fc5SJordan Crouse into an offset based on THERM */ 3971c301fc5SJordan Crouse 3981c301fc5SJordan Crouse /* Read fresh THERM and HYSTERSIS values from the chip */ 3991c301fc5SJordan Crouse data->temp[THERM][sattr->index] = 4001c301fc5SJordan Crouse adt7475_read(TEMP_THERM_REG(sattr->index)) << 2; 4011c301fc5SJordan Crouse adt7475_read_hystersis(client); 4021c301fc5SJordan Crouse 4031c301fc5SJordan Crouse temp = reg2temp(data, data->temp[THERM][sattr->index]); 4041c301fc5SJordan Crouse val = SENSORS_LIMIT(val, temp - 15000, temp); 4051c301fc5SJordan Crouse val = (temp - val) / 1000; 4061c301fc5SJordan Crouse 4071c301fc5SJordan Crouse if (sattr->index != 1) { 4081c301fc5SJordan Crouse data->temp[HYSTERSIS][sattr->index] &= 0xF0; 4091c301fc5SJordan Crouse data->temp[HYSTERSIS][sattr->index] |= (val & 0xF) << 4; 4101c301fc5SJordan Crouse } else { 4111c301fc5SJordan Crouse data->temp[HYSTERSIS][sattr->index] &= 0x0F; 4121c301fc5SJordan Crouse data->temp[HYSTERSIS][sattr->index] |= (val & 0xF); 4131c301fc5SJordan Crouse } 4141c301fc5SJordan Crouse 4151c301fc5SJordan Crouse out = data->temp[HYSTERSIS][sattr->index]; 4161c301fc5SJordan Crouse break; 4171c301fc5SJordan Crouse 4181c301fc5SJordan Crouse default: 4191c301fc5SJordan Crouse data->temp[sattr->nr][sattr->index] = temp2reg(data, val); 4201c301fc5SJordan Crouse 4211c301fc5SJordan Crouse /* We maintain an extra 2 digits of precision for simplicity 4221c301fc5SJordan Crouse * - shift those back off before writing the value */ 4231c301fc5SJordan Crouse out = (u8) (data->temp[sattr->nr][sattr->index] >> 2); 4241c301fc5SJordan Crouse } 4251c301fc5SJordan Crouse 4261c301fc5SJordan Crouse switch (sattr->nr) { 4271c301fc5SJordan Crouse case MIN: 4281c301fc5SJordan Crouse reg = TEMP_MIN_REG(sattr->index); 4291c301fc5SJordan Crouse break; 4301c301fc5SJordan Crouse case MAX: 4311c301fc5SJordan Crouse reg = TEMP_MAX_REG(sattr->index); 4321c301fc5SJordan Crouse break; 4331c301fc5SJordan Crouse case OFFSET: 4341c301fc5SJordan Crouse reg = TEMP_OFFSET_REG(sattr->index); 4351c301fc5SJordan Crouse break; 4361c301fc5SJordan Crouse case AUTOMIN: 4371c301fc5SJordan Crouse reg = TEMP_TMIN_REG(sattr->index); 4381c301fc5SJordan Crouse break; 4391c301fc5SJordan Crouse case THERM: 4401c301fc5SJordan Crouse reg = TEMP_THERM_REG(sattr->index); 4411c301fc5SJordan Crouse break; 4421c301fc5SJordan Crouse case HYSTERSIS: 4431c301fc5SJordan Crouse if (sattr->index != 2) 4441c301fc5SJordan Crouse reg = REG_REMOTE1_HYSTERSIS; 4451c301fc5SJordan Crouse else 4461c301fc5SJordan Crouse reg = REG_REMOTE2_HYSTERSIS; 4471c301fc5SJordan Crouse 4481c301fc5SJordan Crouse break; 4491c301fc5SJordan Crouse } 4501c301fc5SJordan Crouse 4511c301fc5SJordan Crouse i2c_smbus_write_byte_data(client, reg, out); 4521c301fc5SJordan Crouse 4531c301fc5SJordan Crouse mutex_unlock(&data->lock); 4541c301fc5SJordan Crouse return count; 4551c301fc5SJordan Crouse } 4561c301fc5SJordan Crouse 4571c301fc5SJordan Crouse /* Table of autorange values - the user will write the value in millidegrees, 4581c301fc5SJordan Crouse and we'll convert it */ 4591c301fc5SJordan Crouse static const int autorange_table[] = { 4601c301fc5SJordan Crouse 2000, 2500, 3330, 4000, 5000, 6670, 8000, 4611c301fc5SJordan Crouse 10000, 13330, 16000, 20000, 26670, 32000, 40000, 4621c301fc5SJordan Crouse 53330, 80000 4631c301fc5SJordan Crouse }; 4641c301fc5SJordan Crouse 4651c301fc5SJordan Crouse static ssize_t show_point2(struct device *dev, struct device_attribute *attr, 4661c301fc5SJordan Crouse char *buf) 4671c301fc5SJordan Crouse { 4681c301fc5SJordan Crouse struct adt7475_data *data = adt7475_update_device(dev); 4691c301fc5SJordan Crouse struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); 4701c301fc5SJordan Crouse int out, val; 4711c301fc5SJordan Crouse 4721c301fc5SJordan Crouse mutex_lock(&data->lock); 4731c301fc5SJordan Crouse out = (data->range[sattr->index] >> 4) & 0x0F; 4741c301fc5SJordan Crouse val = reg2temp(data, data->temp[AUTOMIN][sattr->index]); 4751c301fc5SJordan Crouse mutex_unlock(&data->lock); 4761c301fc5SJordan Crouse 4771c301fc5SJordan Crouse return sprintf(buf, "%d\n", val + autorange_table[out]); 4781c301fc5SJordan Crouse } 4791c301fc5SJordan Crouse 4801c301fc5SJordan Crouse static ssize_t set_point2(struct device *dev, struct device_attribute *attr, 4811c301fc5SJordan Crouse const char *buf, size_t count) 4821c301fc5SJordan Crouse { 4831c301fc5SJordan Crouse struct i2c_client *client = to_i2c_client(dev); 4841c301fc5SJordan Crouse struct adt7475_data *data = i2c_get_clientdata(client); 4851c301fc5SJordan Crouse struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); 4861c301fc5SJordan Crouse int temp; 4871c301fc5SJordan Crouse long val; 4881c301fc5SJordan Crouse 4891c301fc5SJordan Crouse if (strict_strtol(buf, 10, &val)) 4901c301fc5SJordan Crouse return -EINVAL; 4911c301fc5SJordan Crouse 4921c301fc5SJordan Crouse mutex_lock(&data->lock); 4931c301fc5SJordan Crouse 4941c301fc5SJordan Crouse /* Get a fresh copy of the needed registers */ 4951c301fc5SJordan Crouse data->config5 = adt7475_read(REG_CONFIG5); 4961c301fc5SJordan Crouse data->temp[AUTOMIN][sattr->index] = 4971c301fc5SJordan Crouse adt7475_read(TEMP_TMIN_REG(sattr->index)) << 2; 4981c301fc5SJordan Crouse data->range[sattr->index] = 4991c301fc5SJordan Crouse adt7475_read(TEMP_TRANGE_REG(sattr->index)); 5001c301fc5SJordan Crouse 5011c301fc5SJordan Crouse /* The user will write an absolute value, so subtract the start point 5021c301fc5SJordan Crouse to figure the range */ 5031c301fc5SJordan Crouse temp = reg2temp(data, data->temp[AUTOMIN][sattr->index]); 5041c301fc5SJordan Crouse val = SENSORS_LIMIT(val, temp + autorange_table[0], 5051c301fc5SJordan Crouse temp + autorange_table[ARRAY_SIZE(autorange_table) - 1]); 5061c301fc5SJordan Crouse val -= temp; 5071c301fc5SJordan Crouse 5081c301fc5SJordan Crouse /* Find the nearest table entry to what the user wrote */ 5091c301fc5SJordan Crouse val = find_nearest(val, autorange_table, ARRAY_SIZE(autorange_table)); 5101c301fc5SJordan Crouse 5111c301fc5SJordan Crouse data->range[sattr->index] &= ~0xF0; 5121c301fc5SJordan Crouse data->range[sattr->index] |= val << 4; 5131c301fc5SJordan Crouse 5141c301fc5SJordan Crouse i2c_smbus_write_byte_data(client, TEMP_TRANGE_REG(sattr->index), 5151c301fc5SJordan Crouse data->range[sattr->index]); 5161c301fc5SJordan Crouse 5171c301fc5SJordan Crouse mutex_unlock(&data->lock); 5181c301fc5SJordan Crouse return count; 5191c301fc5SJordan Crouse } 5201c301fc5SJordan Crouse 5211c301fc5SJordan Crouse static ssize_t show_tach(struct device *dev, struct device_attribute *attr, 5221c301fc5SJordan Crouse char *buf) 5231c301fc5SJordan Crouse { 5241c301fc5SJordan Crouse struct adt7475_data *data = adt7475_update_device(dev); 5251c301fc5SJordan Crouse struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); 5261c301fc5SJordan Crouse int out; 5271c301fc5SJordan Crouse 5281c301fc5SJordan Crouse if (sattr->nr == ALARM) 5291c301fc5SJordan Crouse out = (data->alarms >> (sattr->index + 10)) & 1; 5301c301fc5SJordan Crouse else 5311c301fc5SJordan Crouse out = tach2rpm(data->tach[sattr->nr][sattr->index]); 5321c301fc5SJordan Crouse 5331c301fc5SJordan Crouse return sprintf(buf, "%d\n", out); 5341c301fc5SJordan Crouse } 5351c301fc5SJordan Crouse 5361c301fc5SJordan Crouse static ssize_t set_tach(struct device *dev, struct device_attribute *attr, 5371c301fc5SJordan Crouse const char *buf, size_t count) 5381c301fc5SJordan Crouse { 5391c301fc5SJordan Crouse 5401c301fc5SJordan Crouse struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); 5411c301fc5SJordan Crouse struct i2c_client *client = to_i2c_client(dev); 5421c301fc5SJordan Crouse struct adt7475_data *data = i2c_get_clientdata(client); 5431c301fc5SJordan Crouse unsigned long val; 5441c301fc5SJordan Crouse 5451c301fc5SJordan Crouse if (strict_strtoul(buf, 10, &val)) 5461c301fc5SJordan Crouse return -EINVAL; 5471c301fc5SJordan Crouse 5481c301fc5SJordan Crouse mutex_lock(&data->lock); 5491c301fc5SJordan Crouse 5501c301fc5SJordan Crouse data->tach[MIN][sattr->index] = rpm2tach(val); 5511c301fc5SJordan Crouse 5521c301fc5SJordan Crouse adt7475_write_word(client, TACH_MIN_REG(sattr->index), 5531c301fc5SJordan Crouse data->tach[MIN][sattr->index]); 5541c301fc5SJordan Crouse 5551c301fc5SJordan Crouse mutex_unlock(&data->lock); 5561c301fc5SJordan Crouse return count; 5571c301fc5SJordan Crouse } 5581c301fc5SJordan Crouse 5591c301fc5SJordan Crouse static ssize_t show_pwm(struct device *dev, struct device_attribute *attr, 5601c301fc5SJordan Crouse char *buf) 5611c301fc5SJordan Crouse { 5621c301fc5SJordan Crouse struct adt7475_data *data = adt7475_update_device(dev); 5631c301fc5SJordan Crouse struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); 5641c301fc5SJordan Crouse 5651c301fc5SJordan Crouse return sprintf(buf, "%d\n", data->pwm[sattr->nr][sattr->index]); 5661c301fc5SJordan Crouse } 5671c301fc5SJordan Crouse 5681c301fc5SJordan Crouse static ssize_t show_pwmchan(struct device *dev, struct device_attribute *attr, 5691c301fc5SJordan Crouse char *buf) 5701c301fc5SJordan Crouse { 5711c301fc5SJordan Crouse struct adt7475_data *data = adt7475_update_device(dev); 5721c301fc5SJordan Crouse struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); 5731c301fc5SJordan Crouse 5741c301fc5SJordan Crouse return sprintf(buf, "%d\n", data->pwmchan[sattr->index]); 5751c301fc5SJordan Crouse } 5761c301fc5SJordan Crouse 5771c301fc5SJordan Crouse static ssize_t show_pwmctrl(struct device *dev, struct device_attribute *attr, 5781c301fc5SJordan Crouse char *buf) 5791c301fc5SJordan Crouse { 5801c301fc5SJordan Crouse struct adt7475_data *data = adt7475_update_device(dev); 5811c301fc5SJordan Crouse struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); 5821c301fc5SJordan Crouse 5831c301fc5SJordan Crouse return sprintf(buf, "%d\n", data->pwmctl[sattr->index]); 5841c301fc5SJordan Crouse } 5851c301fc5SJordan Crouse 5861c301fc5SJordan Crouse static ssize_t set_pwm(struct device *dev, struct device_attribute *attr, 5871c301fc5SJordan Crouse const char *buf, size_t count) 5881c301fc5SJordan Crouse { 5891c301fc5SJordan Crouse 5901c301fc5SJordan Crouse struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); 5911c301fc5SJordan Crouse struct i2c_client *client = to_i2c_client(dev); 5921c301fc5SJordan Crouse struct adt7475_data *data = i2c_get_clientdata(client); 5931c301fc5SJordan Crouse unsigned char reg = 0; 5941c301fc5SJordan Crouse long val; 5951c301fc5SJordan Crouse 5961c301fc5SJordan Crouse if (strict_strtol(buf, 10, &val)) 5971c301fc5SJordan Crouse return -EINVAL; 5981c301fc5SJordan Crouse 5991c301fc5SJordan Crouse mutex_lock(&data->lock); 6001c301fc5SJordan Crouse 6011c301fc5SJordan Crouse switch (sattr->nr) { 6021c301fc5SJordan Crouse case INPUT: 6031c301fc5SJordan Crouse /* Get a fresh value for CONTROL */ 6041c301fc5SJordan Crouse data->pwm[CONTROL][sattr->index] = 6051c301fc5SJordan Crouse adt7475_read(PWM_CONFIG_REG(sattr->index)); 6061c301fc5SJordan Crouse 6071c301fc5SJordan Crouse /* If we are not in manual mode, then we shouldn't allow 6081c301fc5SJordan Crouse * the user to set the pwm speed */ 6091c301fc5SJordan Crouse if (((data->pwm[CONTROL][sattr->index] >> 5) & 7) != 7) { 6101c301fc5SJordan Crouse mutex_unlock(&data->lock); 6111c301fc5SJordan Crouse return count; 6121c301fc5SJordan Crouse } 6131c301fc5SJordan Crouse 6141c301fc5SJordan Crouse reg = PWM_REG(sattr->index); 6151c301fc5SJordan Crouse break; 6161c301fc5SJordan Crouse 6171c301fc5SJordan Crouse case MIN: 6181c301fc5SJordan Crouse reg = PWM_MIN_REG(sattr->index); 6191c301fc5SJordan Crouse break; 6201c301fc5SJordan Crouse 6211c301fc5SJordan Crouse case MAX: 6221c301fc5SJordan Crouse reg = PWM_MAX_REG(sattr->index); 6231c301fc5SJordan Crouse break; 6241c301fc5SJordan Crouse } 6251c301fc5SJordan Crouse 6261c301fc5SJordan Crouse data->pwm[sattr->nr][sattr->index] = SENSORS_LIMIT(val, 0, 0xFF); 6271c301fc5SJordan Crouse i2c_smbus_write_byte_data(client, reg, 6281c301fc5SJordan Crouse data->pwm[sattr->nr][sattr->index]); 6291c301fc5SJordan Crouse 6301c301fc5SJordan Crouse mutex_unlock(&data->lock); 6311c301fc5SJordan Crouse 6321c301fc5SJordan Crouse return count; 6331c301fc5SJordan Crouse } 6341c301fc5SJordan Crouse 6351c301fc5SJordan Crouse /* Called by set_pwmctrl and set_pwmchan */ 6361c301fc5SJordan Crouse 6371c301fc5SJordan Crouse static int hw_set_pwm(struct i2c_client *client, int index, 6381c301fc5SJordan Crouse unsigned int pwmctl, unsigned int pwmchan) 6391c301fc5SJordan Crouse { 6401c301fc5SJordan Crouse struct adt7475_data *data = i2c_get_clientdata(client); 6411c301fc5SJordan Crouse long val = 0; 6421c301fc5SJordan Crouse 6431c301fc5SJordan Crouse switch (pwmctl) { 6441c301fc5SJordan Crouse case 0: 6451c301fc5SJordan Crouse val = 0x03; /* Run at full speed */ 6461c301fc5SJordan Crouse break; 6471c301fc5SJordan Crouse case 1: 6481c301fc5SJordan Crouse val = 0x07; /* Manual mode */ 6491c301fc5SJordan Crouse break; 6501c301fc5SJordan Crouse case 2: 6511c301fc5SJordan Crouse switch (pwmchan) { 6521c301fc5SJordan Crouse case 1: 6531c301fc5SJordan Crouse /* Remote1 controls PWM */ 6541c301fc5SJordan Crouse val = 0x00; 6551c301fc5SJordan Crouse break; 6561c301fc5SJordan Crouse case 2: 6571c301fc5SJordan Crouse /* local controls PWM */ 6581c301fc5SJordan Crouse val = 0x01; 6591c301fc5SJordan Crouse break; 6601c301fc5SJordan Crouse case 4: 6611c301fc5SJordan Crouse /* remote2 controls PWM */ 6621c301fc5SJordan Crouse val = 0x02; 6631c301fc5SJordan Crouse break; 6641c301fc5SJordan Crouse case 6: 6651c301fc5SJordan Crouse /* local/remote2 control PWM */ 6661c301fc5SJordan Crouse val = 0x05; 6671c301fc5SJordan Crouse break; 6681c301fc5SJordan Crouse case 7: 6691c301fc5SJordan Crouse /* All three control PWM */ 6701c301fc5SJordan Crouse val = 0x06; 6711c301fc5SJordan Crouse break; 6721c301fc5SJordan Crouse default: 6731c301fc5SJordan Crouse return -EINVAL; 6741c301fc5SJordan Crouse } 6751c301fc5SJordan Crouse break; 6761c301fc5SJordan Crouse default: 6771c301fc5SJordan Crouse return -EINVAL; 6781c301fc5SJordan Crouse } 6791c301fc5SJordan Crouse 6801c301fc5SJordan Crouse data->pwmctl[index] = pwmctl; 6811c301fc5SJordan Crouse data->pwmchan[index] = pwmchan; 6821c301fc5SJordan Crouse 6831c301fc5SJordan Crouse data->pwm[CONTROL][index] &= ~0xE0; 6841c301fc5SJordan Crouse data->pwm[CONTROL][index] |= (val & 7) << 5; 6851c301fc5SJordan Crouse 6861c301fc5SJordan Crouse i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index), 6871c301fc5SJordan Crouse data->pwm[CONTROL][index]); 6881c301fc5SJordan Crouse 6891c301fc5SJordan Crouse return 0; 6901c301fc5SJordan Crouse } 6911c301fc5SJordan Crouse 6921c301fc5SJordan Crouse static ssize_t set_pwmchan(struct device *dev, struct device_attribute *attr, 6931c301fc5SJordan Crouse const char *buf, size_t count) 6941c301fc5SJordan Crouse { 6951c301fc5SJordan Crouse struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); 6961c301fc5SJordan Crouse struct i2c_client *client = to_i2c_client(dev); 6971c301fc5SJordan Crouse struct adt7475_data *data = i2c_get_clientdata(client); 6981c301fc5SJordan Crouse int r; 6991c301fc5SJordan Crouse long val; 7001c301fc5SJordan Crouse 7011c301fc5SJordan Crouse if (strict_strtol(buf, 10, &val)) 7021c301fc5SJordan Crouse return -EINVAL; 7031c301fc5SJordan Crouse 7041c301fc5SJordan Crouse mutex_lock(&data->lock); 7051c301fc5SJordan Crouse /* Read Modify Write PWM values */ 7061c301fc5SJordan Crouse adt7475_read_pwm(client, sattr->index); 7071c301fc5SJordan Crouse r = hw_set_pwm(client, sattr->index, data->pwmctl[sattr->index], val); 7081c301fc5SJordan Crouse if (r) 7091c301fc5SJordan Crouse count = r; 7101c301fc5SJordan Crouse mutex_unlock(&data->lock); 7111c301fc5SJordan Crouse 7121c301fc5SJordan Crouse return count; 7131c301fc5SJordan Crouse } 7141c301fc5SJordan Crouse 7151c301fc5SJordan Crouse static ssize_t set_pwmctrl(struct device *dev, struct device_attribute *attr, 7161c301fc5SJordan Crouse const char *buf, size_t count) 7171c301fc5SJordan Crouse { 7181c301fc5SJordan Crouse struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); 7191c301fc5SJordan Crouse struct i2c_client *client = to_i2c_client(dev); 7201c301fc5SJordan Crouse struct adt7475_data *data = i2c_get_clientdata(client); 7211c301fc5SJordan Crouse int r; 7221c301fc5SJordan Crouse long val; 7231c301fc5SJordan Crouse 7241c301fc5SJordan Crouse if (strict_strtol(buf, 10, &val)) 7251c301fc5SJordan Crouse return -EINVAL; 7261c301fc5SJordan Crouse 7271c301fc5SJordan Crouse mutex_lock(&data->lock); 7281c301fc5SJordan Crouse /* Read Modify Write PWM values */ 7291c301fc5SJordan Crouse adt7475_read_pwm(client, sattr->index); 7301c301fc5SJordan Crouse r = hw_set_pwm(client, sattr->index, val, data->pwmchan[sattr->index]); 7311c301fc5SJordan Crouse if (r) 7321c301fc5SJordan Crouse count = r; 7331c301fc5SJordan Crouse mutex_unlock(&data->lock); 7341c301fc5SJordan Crouse 7351c301fc5SJordan Crouse return count; 7361c301fc5SJordan Crouse } 7371c301fc5SJordan Crouse 7381c301fc5SJordan Crouse /* List of frequencies for the PWM */ 7391c301fc5SJordan Crouse static const int pwmfreq_table[] = { 7401c301fc5SJordan Crouse 11, 14, 22, 29, 35, 44, 58, 88 7411c301fc5SJordan Crouse }; 7421c301fc5SJordan Crouse 7431c301fc5SJordan Crouse static ssize_t show_pwmfreq(struct device *dev, struct device_attribute *attr, 7441c301fc5SJordan Crouse char *buf) 7451c301fc5SJordan Crouse { 7461c301fc5SJordan Crouse struct adt7475_data *data = adt7475_update_device(dev); 7471c301fc5SJordan Crouse struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); 7481c301fc5SJordan Crouse 7491c301fc5SJordan Crouse return sprintf(buf, "%d\n", 7501c301fc5SJordan Crouse pwmfreq_table[data->range[sattr->index] & 7]); 7511c301fc5SJordan Crouse } 7521c301fc5SJordan Crouse 7531c301fc5SJordan Crouse static ssize_t set_pwmfreq(struct device *dev, struct device_attribute *attr, 7541c301fc5SJordan Crouse const char *buf, size_t count) 7551c301fc5SJordan Crouse { 7561c301fc5SJordan Crouse struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); 7571c301fc5SJordan Crouse struct i2c_client *client = to_i2c_client(dev); 7581c301fc5SJordan Crouse struct adt7475_data *data = i2c_get_clientdata(client); 7591c301fc5SJordan Crouse int out; 7601c301fc5SJordan Crouse long val; 7611c301fc5SJordan Crouse 7621c301fc5SJordan Crouse if (strict_strtol(buf, 10, &val)) 7631c301fc5SJordan Crouse return -EINVAL; 7641c301fc5SJordan Crouse 7651c301fc5SJordan Crouse out = find_nearest(val, pwmfreq_table, ARRAY_SIZE(pwmfreq_table)); 7661c301fc5SJordan Crouse 7671c301fc5SJordan Crouse mutex_lock(&data->lock); 7681c301fc5SJordan Crouse 7691c301fc5SJordan Crouse data->range[sattr->index] = 7701c301fc5SJordan Crouse adt7475_read(TEMP_TRANGE_REG(sattr->index)); 7711c301fc5SJordan Crouse data->range[sattr->index] &= ~7; 7721c301fc5SJordan Crouse data->range[sattr->index] |= out; 7731c301fc5SJordan Crouse 7741c301fc5SJordan Crouse i2c_smbus_write_byte_data(client, TEMP_TRANGE_REG(sattr->index), 7751c301fc5SJordan Crouse data->range[sattr->index]); 7761c301fc5SJordan Crouse 7771c301fc5SJordan Crouse mutex_unlock(&data->lock); 7781c301fc5SJordan Crouse return count; 7791c301fc5SJordan Crouse } 7801c301fc5SJordan Crouse 7811c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_voltage, NULL, INPUT, 0); 7821c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(in1_max, S_IRUGO | S_IWUSR, show_voltage, 7831c301fc5SJordan Crouse set_voltage, MAX, 0); 7841c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(in1_min, S_IRUGO | S_IWUSR, show_voltage, 7851c301fc5SJordan Crouse set_voltage, MIN, 0); 7861c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(in1_alarm, S_IRUGO, show_voltage, NULL, ALARM, 0); 7871c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_voltage, NULL, INPUT, 1); 7881c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(in2_max, S_IRUGO | S_IWUSR, show_voltage, 7891c301fc5SJordan Crouse set_voltage, MAX, 1); 7901c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(in2_min, S_IRUGO | S_IWUSR, show_voltage, 7911c301fc5SJordan Crouse set_voltage, MIN, 1); 7921c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(in2_alarm, S_IRUGO, show_voltage, NULL, ALARM, 1); 7931c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, INPUT, 0); 7941c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp1_alarm, S_IRUGO, show_temp, NULL, ALARM, 0); 7951c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp1_fault, S_IRUGO, show_temp, NULL, FAULT, 0); 7961c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, set_temp, 7971c301fc5SJordan Crouse MAX, 0); 7981c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp1_min, S_IRUGO | S_IWUSR, show_temp, set_temp, 7991c301fc5SJordan Crouse MIN, 0); 8001c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp1_offset, S_IRUGO | S_IWUSR, show_temp, 8011c301fc5SJordan Crouse set_temp, OFFSET, 0); 8021c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp1_auto_point1_temp, S_IRUGO | S_IWUSR, 8031c301fc5SJordan Crouse show_temp, set_temp, AUTOMIN, 0); 8041c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp1_auto_point2_temp, S_IRUGO | S_IWUSR, 8051c301fc5SJordan Crouse show_point2, set_point2, 0, 0); 8061c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp1_crit, S_IRUGO | S_IWUSR, show_temp, set_temp, 8071c301fc5SJordan Crouse THERM, 0); 8081c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp1_crit_hyst, S_IRUGO | S_IWUSR, show_temp, 8091c301fc5SJordan Crouse set_temp, HYSTERSIS, 0); 8101c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, INPUT, 1); 8111c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp2_alarm, S_IRUGO, show_temp, NULL, ALARM, 1); 8121c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, set_temp, 8131c301fc5SJordan Crouse MAX, 1); 8141c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp2_min, S_IRUGO | S_IWUSR, show_temp, set_temp, 8151c301fc5SJordan Crouse MIN, 1); 8161c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IRUGO | S_IWUSR, show_temp, 8171c301fc5SJordan Crouse set_temp, OFFSET, 1); 8181c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp2_auto_point1_temp, S_IRUGO | S_IWUSR, 8191c301fc5SJordan Crouse show_temp, set_temp, AUTOMIN, 1); 8201c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp2_auto_point2_temp, S_IRUGO | S_IWUSR, 8211c301fc5SJordan Crouse show_point2, set_point2, 0, 1); 8221c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp2_crit, S_IRUGO | S_IWUSR, show_temp, set_temp, 8231c301fc5SJordan Crouse THERM, 1); 8241c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp2_crit_hyst, S_IRUGO | S_IWUSR, show_temp, 8251c301fc5SJordan Crouse set_temp, HYSTERSIS, 1); 8261c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, INPUT, 2); 8271c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp3_alarm, S_IRUGO, show_temp, NULL, ALARM, 2); 8281c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp3_fault, S_IRUGO, show_temp, NULL, FAULT, 2); 8291c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, set_temp, 8301c301fc5SJordan Crouse MAX, 2); 8311c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp3_min, S_IRUGO | S_IWUSR, show_temp, set_temp, 8321c301fc5SJordan Crouse MIN, 2); 8331c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp3_offset, S_IRUGO | S_IWUSR, show_temp, 8341c301fc5SJordan Crouse set_temp, OFFSET, 2); 8351c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp3_auto_point1_temp, S_IRUGO | S_IWUSR, 8361c301fc5SJordan Crouse show_temp, set_temp, AUTOMIN, 2); 8371c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp3_auto_point2_temp, S_IRUGO | S_IWUSR, 8381c301fc5SJordan Crouse show_point2, set_point2, 0, 2); 8391c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp3_crit, S_IRUGO | S_IWUSR, show_temp, set_temp, 8401c301fc5SJordan Crouse THERM, 2); 8411c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp3_crit_hyst, S_IRUGO | S_IWUSR, show_temp, 8421c301fc5SJordan Crouse set_temp, HYSTERSIS, 2); 8431c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(fan1_input, S_IRUGO, show_tach, NULL, INPUT, 0); 8441c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(fan1_min, S_IRUGO | S_IWUSR, show_tach, set_tach, 8451c301fc5SJordan Crouse MIN, 0); 8461c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(fan1_alarm, S_IRUGO, show_tach, NULL, ALARM, 0); 8471c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(fan2_input, S_IRUGO, show_tach, NULL, INPUT, 1); 8481c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(fan2_min, S_IRUGO | S_IWUSR, show_tach, set_tach, 8491c301fc5SJordan Crouse MIN, 1); 8501c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(fan2_alarm, S_IRUGO, show_tach, NULL, ALARM, 1); 8511c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(fan3_input, S_IRUGO, show_tach, NULL, INPUT, 2); 8521c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(fan3_min, S_IRUGO | S_IWUSR, show_tach, set_tach, 8531c301fc5SJordan Crouse MIN, 2); 8541c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(fan3_alarm, S_IRUGO, show_tach, NULL, ALARM, 2); 8551c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(fan4_input, S_IRUGO, show_tach, NULL, INPUT, 3); 8561c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(fan4_min, S_IRUGO | S_IWUSR, show_tach, set_tach, 8571c301fc5SJordan Crouse MIN, 3); 8581c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(fan4_alarm, S_IRUGO, show_tach, NULL, ALARM, 3); 8591c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT, 8601c301fc5SJordan Crouse 0); 8611c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm1_freq, S_IRUGO | S_IWUSR, show_pwmfreq, 8621c301fc5SJordan Crouse set_pwmfreq, INPUT, 0); 8631c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm1_enable, S_IRUGO | S_IWUSR, show_pwmctrl, 8641c301fc5SJordan Crouse set_pwmctrl, INPUT, 0); 8651c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm1_auto_channel_temp, S_IRUGO | S_IWUSR, 8661c301fc5SJordan Crouse show_pwmchan, set_pwmchan, INPUT, 0); 8671c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm, 8681c301fc5SJordan Crouse set_pwm, MIN, 0); 8691c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm, 8701c301fc5SJordan Crouse set_pwm, MAX, 0); 8711c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm2, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT, 8721c301fc5SJordan Crouse 1); 8731c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm2_freq, S_IRUGO | S_IWUSR, show_pwmfreq, 8741c301fc5SJordan Crouse set_pwmfreq, INPUT, 1); 8751c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm2_enable, S_IRUGO | S_IWUSR, show_pwmctrl, 8761c301fc5SJordan Crouse set_pwmctrl, INPUT, 1); 8771c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm2_auto_channel_temp, S_IRUGO | S_IWUSR, 8781c301fc5SJordan Crouse show_pwmchan, set_pwmchan, INPUT, 1); 8791c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm, 8801c301fc5SJordan Crouse set_pwm, MIN, 1); 8811c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm, 8821c301fc5SJordan Crouse set_pwm, MAX, 1); 8831c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm3, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT, 8841c301fc5SJordan Crouse 2); 8851c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm3_freq, S_IRUGO | S_IWUSR, show_pwmfreq, 8861c301fc5SJordan Crouse set_pwmfreq, INPUT, 2); 8871c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm3_enable, S_IRUGO | S_IWUSR, show_pwmctrl, 8881c301fc5SJordan Crouse set_pwmctrl, INPUT, 2); 8891c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm3_auto_channel_temp, S_IRUGO | S_IWUSR, 8901c301fc5SJordan Crouse show_pwmchan, set_pwmchan, INPUT, 2); 8911c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm, 8921c301fc5SJordan Crouse set_pwm, MIN, 2); 8931c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm, 8941c301fc5SJordan Crouse set_pwm, MAX, 2); 8951c301fc5SJordan Crouse 8961c301fc5SJordan Crouse static struct attribute *adt7475_attrs[] = { 8971c301fc5SJordan Crouse &sensor_dev_attr_in1_input.dev_attr.attr, 8981c301fc5SJordan Crouse &sensor_dev_attr_in1_max.dev_attr.attr, 8991c301fc5SJordan Crouse &sensor_dev_attr_in1_min.dev_attr.attr, 9001c301fc5SJordan Crouse &sensor_dev_attr_in1_alarm.dev_attr.attr, 9011c301fc5SJordan Crouse &sensor_dev_attr_in2_input.dev_attr.attr, 9021c301fc5SJordan Crouse &sensor_dev_attr_in2_max.dev_attr.attr, 9031c301fc5SJordan Crouse &sensor_dev_attr_in2_min.dev_attr.attr, 9041c301fc5SJordan Crouse &sensor_dev_attr_in2_alarm.dev_attr.attr, 9051c301fc5SJordan Crouse &sensor_dev_attr_temp1_input.dev_attr.attr, 9061c301fc5SJordan Crouse &sensor_dev_attr_temp1_alarm.dev_attr.attr, 9071c301fc5SJordan Crouse &sensor_dev_attr_temp1_fault.dev_attr.attr, 9081c301fc5SJordan Crouse &sensor_dev_attr_temp1_max.dev_attr.attr, 9091c301fc5SJordan Crouse &sensor_dev_attr_temp1_min.dev_attr.attr, 9101c301fc5SJordan Crouse &sensor_dev_attr_temp1_offset.dev_attr.attr, 9111c301fc5SJordan Crouse &sensor_dev_attr_temp1_auto_point1_temp.dev_attr.attr, 9121c301fc5SJordan Crouse &sensor_dev_attr_temp1_auto_point2_temp.dev_attr.attr, 9131c301fc5SJordan Crouse &sensor_dev_attr_temp1_crit.dev_attr.attr, 9141c301fc5SJordan Crouse &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr, 9151c301fc5SJordan Crouse &sensor_dev_attr_temp2_input.dev_attr.attr, 9161c301fc5SJordan Crouse &sensor_dev_attr_temp2_alarm.dev_attr.attr, 9171c301fc5SJordan Crouse &sensor_dev_attr_temp2_max.dev_attr.attr, 9181c301fc5SJordan Crouse &sensor_dev_attr_temp2_min.dev_attr.attr, 9191c301fc5SJordan Crouse &sensor_dev_attr_temp2_offset.dev_attr.attr, 9201c301fc5SJordan Crouse &sensor_dev_attr_temp2_auto_point1_temp.dev_attr.attr, 9211c301fc5SJordan Crouse &sensor_dev_attr_temp2_auto_point2_temp.dev_attr.attr, 9221c301fc5SJordan Crouse &sensor_dev_attr_temp2_crit.dev_attr.attr, 9231c301fc5SJordan Crouse &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr, 9241c301fc5SJordan Crouse &sensor_dev_attr_temp3_input.dev_attr.attr, 9251c301fc5SJordan Crouse &sensor_dev_attr_temp3_fault.dev_attr.attr, 9261c301fc5SJordan Crouse &sensor_dev_attr_temp3_alarm.dev_attr.attr, 9271c301fc5SJordan Crouse &sensor_dev_attr_temp3_max.dev_attr.attr, 9281c301fc5SJordan Crouse &sensor_dev_attr_temp3_min.dev_attr.attr, 9291c301fc5SJordan Crouse &sensor_dev_attr_temp3_offset.dev_attr.attr, 9301c301fc5SJordan Crouse &sensor_dev_attr_temp3_auto_point1_temp.dev_attr.attr, 9311c301fc5SJordan Crouse &sensor_dev_attr_temp3_auto_point2_temp.dev_attr.attr, 9321c301fc5SJordan Crouse &sensor_dev_attr_temp3_crit.dev_attr.attr, 9331c301fc5SJordan Crouse &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr, 9341c301fc5SJordan Crouse &sensor_dev_attr_fan1_input.dev_attr.attr, 9351c301fc5SJordan Crouse &sensor_dev_attr_fan1_min.dev_attr.attr, 9361c301fc5SJordan Crouse &sensor_dev_attr_fan1_alarm.dev_attr.attr, 9371c301fc5SJordan Crouse &sensor_dev_attr_fan2_input.dev_attr.attr, 9381c301fc5SJordan Crouse &sensor_dev_attr_fan2_min.dev_attr.attr, 9391c301fc5SJordan Crouse &sensor_dev_attr_fan2_alarm.dev_attr.attr, 9401c301fc5SJordan Crouse &sensor_dev_attr_fan3_input.dev_attr.attr, 9411c301fc5SJordan Crouse &sensor_dev_attr_fan3_min.dev_attr.attr, 9421c301fc5SJordan Crouse &sensor_dev_attr_fan3_alarm.dev_attr.attr, 9431c301fc5SJordan Crouse &sensor_dev_attr_fan4_input.dev_attr.attr, 9441c301fc5SJordan Crouse &sensor_dev_attr_fan4_min.dev_attr.attr, 9451c301fc5SJordan Crouse &sensor_dev_attr_fan4_alarm.dev_attr.attr, 9461c301fc5SJordan Crouse &sensor_dev_attr_pwm1.dev_attr.attr, 9471c301fc5SJordan Crouse &sensor_dev_attr_pwm1_freq.dev_attr.attr, 9481c301fc5SJordan Crouse &sensor_dev_attr_pwm1_enable.dev_attr.attr, 9491c301fc5SJordan Crouse &sensor_dev_attr_pwm1_auto_channel_temp.dev_attr.attr, 9501c301fc5SJordan Crouse &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr, 9511c301fc5SJordan Crouse &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr, 9521c301fc5SJordan Crouse &sensor_dev_attr_pwm2.dev_attr.attr, 9531c301fc5SJordan Crouse &sensor_dev_attr_pwm2_freq.dev_attr.attr, 9541c301fc5SJordan Crouse &sensor_dev_attr_pwm2_enable.dev_attr.attr, 9551c301fc5SJordan Crouse &sensor_dev_attr_pwm2_auto_channel_temp.dev_attr.attr, 9561c301fc5SJordan Crouse &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr, 9571c301fc5SJordan Crouse &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr, 9581c301fc5SJordan Crouse &sensor_dev_attr_pwm3.dev_attr.attr, 9591c301fc5SJordan Crouse &sensor_dev_attr_pwm3_freq.dev_attr.attr, 9601c301fc5SJordan Crouse &sensor_dev_attr_pwm3_enable.dev_attr.attr, 9611c301fc5SJordan Crouse &sensor_dev_attr_pwm3_auto_channel_temp.dev_attr.attr, 9621c301fc5SJordan Crouse &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr, 9631c301fc5SJordan Crouse &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr, 9641c301fc5SJordan Crouse NULL, 9651c301fc5SJordan Crouse }; 9661c301fc5SJordan Crouse 9671c301fc5SJordan Crouse struct attribute_group adt7475_attr_group = { .attrs = adt7475_attrs }; 9681c301fc5SJordan Crouse 9691c301fc5SJordan Crouse static int adt7475_detect(struct i2c_client *client, int kind, 9701c301fc5SJordan Crouse struct i2c_board_info *info) 9711c301fc5SJordan Crouse { 9721c301fc5SJordan Crouse struct i2c_adapter *adapter = client->adapter; 9731c301fc5SJordan Crouse 9741c301fc5SJordan Crouse if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) 9751c301fc5SJordan Crouse return -ENODEV; 9761c301fc5SJordan Crouse 9771c301fc5SJordan Crouse if (kind <= 0) { 9781c301fc5SJordan Crouse if (adt7475_read(REG_VENDID) != 0x41 || 9791c301fc5SJordan Crouse adt7475_read(REG_DEVID) != 0x75) { 9801c301fc5SJordan Crouse dev_err(&adapter->dev, 9811c301fc5SJordan Crouse "Couldn't detect a adt7475 part at 0x%02x\n", 9821c301fc5SJordan Crouse (unsigned int)client->addr); 9831c301fc5SJordan Crouse return -ENODEV; 9841c301fc5SJordan Crouse } 9851c301fc5SJordan Crouse } 9861c301fc5SJordan Crouse 9871c301fc5SJordan Crouse strlcpy(info->type, adt7475_id[0].name, I2C_NAME_SIZE); 9881c301fc5SJordan Crouse 9891c301fc5SJordan Crouse return 0; 9901c301fc5SJordan Crouse } 9911c301fc5SJordan Crouse 9921c301fc5SJordan Crouse static int adt7475_probe(struct i2c_client *client, 9931c301fc5SJordan Crouse const struct i2c_device_id *id) 9941c301fc5SJordan Crouse { 9951c301fc5SJordan Crouse struct adt7475_data *data; 9961c301fc5SJordan Crouse int i, ret = 0; 9971c301fc5SJordan Crouse 9981c301fc5SJordan Crouse data = kzalloc(sizeof(*data), GFP_KERNEL); 9991c301fc5SJordan Crouse if (data == NULL) 10001c301fc5SJordan Crouse return -ENOMEM; 10011c301fc5SJordan Crouse 10021c301fc5SJordan Crouse mutex_init(&data->lock); 10031c301fc5SJordan Crouse i2c_set_clientdata(client, data); 10041c301fc5SJordan Crouse 10051c301fc5SJordan Crouse /* Call adt7475_read_pwm for all pwm's as this will reprogram any 10061c301fc5SJordan Crouse pwm's which are disabled to manual mode with 0% duty cycle */ 10071c301fc5SJordan Crouse for (i = 0; i < ADT7475_PWM_COUNT; i++) 10081c301fc5SJordan Crouse adt7475_read_pwm(client, i); 10091c301fc5SJordan Crouse 10101c301fc5SJordan Crouse ret = sysfs_create_group(&client->dev.kobj, &adt7475_attr_group); 10111c301fc5SJordan Crouse if (ret) 10121c301fc5SJordan Crouse goto efree; 10131c301fc5SJordan Crouse 10141c301fc5SJordan Crouse data->hwmon_dev = hwmon_device_register(&client->dev); 10151c301fc5SJordan Crouse if (IS_ERR(data->hwmon_dev)) { 10161c301fc5SJordan Crouse ret = PTR_ERR(data->hwmon_dev); 10171c301fc5SJordan Crouse goto eremove; 10181c301fc5SJordan Crouse } 10191c301fc5SJordan Crouse 10201c301fc5SJordan Crouse return 0; 10211c301fc5SJordan Crouse 10221c301fc5SJordan Crouse eremove: 10231c301fc5SJordan Crouse sysfs_remove_group(&client->dev.kobj, &adt7475_attr_group); 10241c301fc5SJordan Crouse efree: 10251c301fc5SJordan Crouse kfree(data); 10261c301fc5SJordan Crouse return ret; 10271c301fc5SJordan Crouse } 10281c301fc5SJordan Crouse 10291c301fc5SJordan Crouse static int adt7475_remove(struct i2c_client *client) 10301c301fc5SJordan Crouse { 10311c301fc5SJordan Crouse struct adt7475_data *data = i2c_get_clientdata(client); 10321c301fc5SJordan Crouse 10331c301fc5SJordan Crouse hwmon_device_unregister(data->hwmon_dev); 10341c301fc5SJordan Crouse sysfs_remove_group(&client->dev.kobj, &adt7475_attr_group); 10351c301fc5SJordan Crouse kfree(data); 10361c301fc5SJordan Crouse 10371c301fc5SJordan Crouse return 0; 10381c301fc5SJordan Crouse } 10391c301fc5SJordan Crouse 10401c301fc5SJordan Crouse static struct i2c_driver adt7475_driver = { 10411c301fc5SJordan Crouse .class = I2C_CLASS_HWMON, 10421c301fc5SJordan Crouse .driver = { 10431c301fc5SJordan Crouse .name = "adt7475", 10441c301fc5SJordan Crouse }, 10451c301fc5SJordan Crouse .probe = adt7475_probe, 10461c301fc5SJordan Crouse .remove = adt7475_remove, 10471c301fc5SJordan Crouse .id_table = adt7475_id, 10481c301fc5SJordan Crouse .detect = adt7475_detect, 10491c301fc5SJordan Crouse .address_data = &addr_data, 10501c301fc5SJordan Crouse }; 10511c301fc5SJordan Crouse 10521c301fc5SJordan Crouse static void adt7475_read_hystersis(struct i2c_client *client) 10531c301fc5SJordan Crouse { 10541c301fc5SJordan Crouse struct adt7475_data *data = i2c_get_clientdata(client); 10551c301fc5SJordan Crouse 10561c301fc5SJordan Crouse data->temp[HYSTERSIS][0] = (u16) adt7475_read(REG_REMOTE1_HYSTERSIS); 10571c301fc5SJordan Crouse data->temp[HYSTERSIS][1] = data->temp[HYSTERSIS][0]; 10581c301fc5SJordan Crouse data->temp[HYSTERSIS][2] = (u16) adt7475_read(REG_REMOTE2_HYSTERSIS); 10591c301fc5SJordan Crouse } 10601c301fc5SJordan Crouse 10611c301fc5SJordan Crouse static void adt7475_read_pwm(struct i2c_client *client, int index) 10621c301fc5SJordan Crouse { 10631c301fc5SJordan Crouse struct adt7475_data *data = i2c_get_clientdata(client); 10641c301fc5SJordan Crouse unsigned int v; 10651c301fc5SJordan Crouse 10661c301fc5SJordan Crouse data->pwm[CONTROL][index] = adt7475_read(PWM_CONFIG_REG(index)); 10671c301fc5SJordan Crouse 10681c301fc5SJordan Crouse /* Figure out the internal value for pwmctrl and pwmchan 10691c301fc5SJordan Crouse based on the current settings */ 10701c301fc5SJordan Crouse v = (data->pwm[CONTROL][index] >> 5) & 7; 10711c301fc5SJordan Crouse 10721c301fc5SJordan Crouse if (v == 3) 10731c301fc5SJordan Crouse data->pwmctl[index] = 0; 10741c301fc5SJordan Crouse else if (v == 7) 10751c301fc5SJordan Crouse data->pwmctl[index] = 1; 10761c301fc5SJordan Crouse else if (v == 4) { 10771c301fc5SJordan Crouse /* The fan is disabled - we don't want to 10781c301fc5SJordan Crouse support that, so change to manual mode and 10791c301fc5SJordan Crouse set the duty cycle to 0 instead 10801c301fc5SJordan Crouse */ 10811c301fc5SJordan Crouse data->pwm[INPUT][index] = 0; 10821c301fc5SJordan Crouse data->pwm[CONTROL][index] &= ~0xE0; 10831c301fc5SJordan Crouse data->pwm[CONTROL][index] |= (7 << 5); 10841c301fc5SJordan Crouse 10851c301fc5SJordan Crouse i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index), 10861c301fc5SJordan Crouse data->pwm[INPUT][index]); 10871c301fc5SJordan Crouse 10881c301fc5SJordan Crouse i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index), 10891c301fc5SJordan Crouse data->pwm[CONTROL][index]); 10901c301fc5SJordan Crouse 10911c301fc5SJordan Crouse data->pwmctl[index] = 1; 10921c301fc5SJordan Crouse } else { 10931c301fc5SJordan Crouse data->pwmctl[index] = 2; 10941c301fc5SJordan Crouse 10951c301fc5SJordan Crouse switch (v) { 10961c301fc5SJordan Crouse case 0: 10971c301fc5SJordan Crouse data->pwmchan[index] = 1; 10981c301fc5SJordan Crouse break; 10991c301fc5SJordan Crouse case 1: 11001c301fc5SJordan Crouse data->pwmchan[index] = 2; 11011c301fc5SJordan Crouse break; 11021c301fc5SJordan Crouse case 2: 11031c301fc5SJordan Crouse data->pwmchan[index] = 4; 11041c301fc5SJordan Crouse break; 11051c301fc5SJordan Crouse case 5: 11061c301fc5SJordan Crouse data->pwmchan[index] = 6; 11071c301fc5SJordan Crouse break; 11081c301fc5SJordan Crouse case 6: 11091c301fc5SJordan Crouse data->pwmchan[index] = 7; 11101c301fc5SJordan Crouse break; 11111c301fc5SJordan Crouse } 11121c301fc5SJordan Crouse } 11131c301fc5SJordan Crouse } 11141c301fc5SJordan Crouse 11151c301fc5SJordan Crouse static struct adt7475_data *adt7475_update_device(struct device *dev) 11161c301fc5SJordan Crouse { 11171c301fc5SJordan Crouse struct i2c_client *client = to_i2c_client(dev); 11181c301fc5SJordan Crouse struct adt7475_data *data = i2c_get_clientdata(client); 11191c301fc5SJordan Crouse u8 ext; 11201c301fc5SJordan Crouse int i; 11211c301fc5SJordan Crouse 11221c301fc5SJordan Crouse mutex_lock(&data->lock); 11231c301fc5SJordan Crouse 11241c301fc5SJordan Crouse /* Measurement values update every 2 seconds */ 11251c301fc5SJordan Crouse if (time_after(jiffies, data->measure_updated + HZ * 2) || 11261c301fc5SJordan Crouse !data->valid) { 11271c301fc5SJordan Crouse data->alarms = adt7475_read(REG_STATUS2) << 8; 11281c301fc5SJordan Crouse data->alarms |= adt7475_read(REG_STATUS1); 11291c301fc5SJordan Crouse 11301c301fc5SJordan Crouse ext = adt7475_read(REG_EXTEND1); 11311c301fc5SJordan Crouse for (i = 0; i < ADT7475_VOLTAGE_COUNT; i++) 11321c301fc5SJordan Crouse data->voltage[INPUT][i] = 11331c301fc5SJordan Crouse (adt7475_read(VOLTAGE_REG(i)) << 2) | 11341c301fc5SJordan Crouse ((ext >> ((i + 1) * 2)) & 3); 11351c301fc5SJordan Crouse 11361c301fc5SJordan Crouse ext = adt7475_read(REG_EXTEND2); 11371c301fc5SJordan Crouse for (i = 0; i < ADT7475_TEMP_COUNT; i++) 11381c301fc5SJordan Crouse data->temp[INPUT][i] = 11391c301fc5SJordan Crouse (adt7475_read(TEMP_REG(i)) << 2) | 11401c301fc5SJordan Crouse ((ext >> ((i + 1) * 2)) & 3); 11411c301fc5SJordan Crouse 11421c301fc5SJordan Crouse for (i = 0; i < ADT7475_TACH_COUNT; i++) 11431c301fc5SJordan Crouse data->tach[INPUT][i] = 11441c301fc5SJordan Crouse adt7475_read_word(client, TACH_REG(i)); 11451c301fc5SJordan Crouse 11461c301fc5SJordan Crouse /* Updated by hw when in auto mode */ 11471c301fc5SJordan Crouse for (i = 0; i < ADT7475_PWM_COUNT; i++) 11481c301fc5SJordan Crouse data->pwm[INPUT][i] = adt7475_read(PWM_REG(i)); 11491c301fc5SJordan Crouse 11501c301fc5SJordan Crouse data->measure_updated = jiffies; 11511c301fc5SJordan Crouse } 11521c301fc5SJordan Crouse 11531c301fc5SJordan Crouse /* Limits and settings, should never change update every 60 seconds */ 11541c301fc5SJordan Crouse if (time_after(jiffies, data->limits_updated + HZ * 2) || 11551c301fc5SJordan Crouse !data->valid) { 11561c301fc5SJordan Crouse data->config5 = adt7475_read(REG_CONFIG5); 11571c301fc5SJordan Crouse 11581c301fc5SJordan Crouse for (i = 0; i < ADT7475_VOLTAGE_COUNT; i++) { 11591c301fc5SJordan Crouse /* Adjust values so they match the input precision */ 11601c301fc5SJordan Crouse data->voltage[MIN][i] = 11611c301fc5SJordan Crouse adt7475_read(VOLTAGE_MIN_REG(i)) << 2; 11621c301fc5SJordan Crouse data->voltage[MAX][i] = 11631c301fc5SJordan Crouse adt7475_read(VOLTAGE_MAX_REG(i)) << 2; 11641c301fc5SJordan Crouse } 11651c301fc5SJordan Crouse 11661c301fc5SJordan Crouse for (i = 0; i < ADT7475_TEMP_COUNT; i++) { 11671c301fc5SJordan Crouse /* Adjust values so they match the input precision */ 11681c301fc5SJordan Crouse data->temp[MIN][i] = 11691c301fc5SJordan Crouse adt7475_read(TEMP_MIN_REG(i)) << 2; 11701c301fc5SJordan Crouse data->temp[MAX][i] = 11711c301fc5SJordan Crouse adt7475_read(TEMP_MAX_REG(i)) << 2; 11721c301fc5SJordan Crouse data->temp[AUTOMIN][i] = 11731c301fc5SJordan Crouse adt7475_read(TEMP_TMIN_REG(i)) << 2; 11741c301fc5SJordan Crouse data->temp[THERM][i] = 11751c301fc5SJordan Crouse adt7475_read(TEMP_THERM_REG(i)) << 2; 11761c301fc5SJordan Crouse data->temp[OFFSET][i] = 11771c301fc5SJordan Crouse adt7475_read(TEMP_OFFSET_REG(i)); 11781c301fc5SJordan Crouse } 11791c301fc5SJordan Crouse adt7475_read_hystersis(client); 11801c301fc5SJordan Crouse 11811c301fc5SJordan Crouse for (i = 0; i < ADT7475_TACH_COUNT; i++) 11821c301fc5SJordan Crouse data->tach[MIN][i] = 11831c301fc5SJordan Crouse adt7475_read_word(client, TACH_MIN_REG(i)); 11841c301fc5SJordan Crouse 11851c301fc5SJordan Crouse for (i = 0; i < ADT7475_PWM_COUNT; i++) { 11861c301fc5SJordan Crouse data->pwm[MAX][i] = adt7475_read(PWM_MAX_REG(i)); 11871c301fc5SJordan Crouse data->pwm[MIN][i] = adt7475_read(PWM_MIN_REG(i)); 11881c301fc5SJordan Crouse /* Set the channel and control information */ 11891c301fc5SJordan Crouse adt7475_read_pwm(client, i); 11901c301fc5SJordan Crouse } 11911c301fc5SJordan Crouse 11921c301fc5SJordan Crouse data->range[0] = adt7475_read(TEMP_TRANGE_REG(0)); 11931c301fc5SJordan Crouse data->range[1] = adt7475_read(TEMP_TRANGE_REG(1)); 11941c301fc5SJordan Crouse data->range[2] = adt7475_read(TEMP_TRANGE_REG(2)); 11951c301fc5SJordan Crouse 11961c301fc5SJordan Crouse data->limits_updated = jiffies; 11971c301fc5SJordan Crouse data->valid = 1; 11981c301fc5SJordan Crouse } 11991c301fc5SJordan Crouse 12001c301fc5SJordan Crouse mutex_unlock(&data->lock); 12011c301fc5SJordan Crouse 12021c301fc5SJordan Crouse return data; 12031c301fc5SJordan Crouse } 12041c301fc5SJordan Crouse 12051c301fc5SJordan Crouse static int __init sensors_adt7475_init(void) 12061c301fc5SJordan Crouse { 12071c301fc5SJordan Crouse return i2c_add_driver(&adt7475_driver); 12081c301fc5SJordan Crouse } 12091c301fc5SJordan Crouse 12101c301fc5SJordan Crouse static void __exit sensors_adt7475_exit(void) 12111c301fc5SJordan Crouse { 12121c301fc5SJordan Crouse i2c_del_driver(&adt7475_driver); 12131c301fc5SJordan Crouse } 12141c301fc5SJordan Crouse 12151c301fc5SJordan Crouse MODULE_AUTHOR("Advanced Micro Devices, Inc"); 12161c301fc5SJordan Crouse MODULE_DESCRIPTION("adt7475 driver"); 12171c301fc5SJordan Crouse MODULE_LICENSE("GPL"); 12181c301fc5SJordan Crouse 12191c301fc5SJordan Crouse module_init(sensors_adt7475_init); 12201c301fc5SJordan Crouse module_exit(sensors_adt7475_exit); 1221