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> 63d849981SJean Delvare * Copyright (C) 2009 Jean Delvare <khali@linux-fr.org> 73d849981SJean Delvare * 81c301fc5SJordan Crouse * Derived from the lm83 driver by Jean Delvare 91c301fc5SJordan Crouse * 101c301fc5SJordan Crouse * This program is free software; you can redistribute it and/or modify 111c301fc5SJordan Crouse * it under the terms of the GNU General Public License version 2 as 121c301fc5SJordan Crouse * published by the Free Software Foundation. 131c301fc5SJordan Crouse */ 141c301fc5SJordan Crouse 151c301fc5SJordan Crouse #include <linux/module.h> 161c301fc5SJordan Crouse #include <linux/init.h> 171c301fc5SJordan Crouse #include <linux/slab.h> 181c301fc5SJordan Crouse #include <linux/i2c.h> 191c301fc5SJordan Crouse #include <linux/hwmon.h> 201c301fc5SJordan Crouse #include <linux/hwmon-sysfs.h> 2154fe4671SJean Delvare #include <linux/hwmon-vid.h> 221c301fc5SJordan Crouse #include <linux/err.h> 231c301fc5SJordan Crouse 241c301fc5SJordan Crouse /* Indexes for the sysfs hooks */ 251c301fc5SJordan Crouse 261c301fc5SJordan Crouse #define INPUT 0 271c301fc5SJordan Crouse #define MIN 1 281c301fc5SJordan Crouse #define MAX 2 291c301fc5SJordan Crouse #define CONTROL 3 301c301fc5SJordan Crouse #define OFFSET 3 311c301fc5SJordan Crouse #define AUTOMIN 4 321c301fc5SJordan Crouse #define THERM 5 331c301fc5SJordan Crouse #define HYSTERSIS 6 341c301fc5SJordan Crouse 351c301fc5SJordan Crouse /* These are unique identifiers for the sysfs functions - unlike the 361c301fc5SJordan Crouse numbers above, these are not also indexes into an array 371c301fc5SJordan Crouse */ 381c301fc5SJordan Crouse 391c301fc5SJordan Crouse #define ALARM 9 401c301fc5SJordan Crouse #define FAULT 10 411c301fc5SJordan Crouse 421c301fc5SJordan Crouse /* 7475 Common Registers */ 431c301fc5SJordan Crouse 44d07ca4adSJean Delvare #define REG_DEVREV2 0x12 /* ADT7490 only */ 45d07ca4adSJean Delvare 463d849981SJean Delvare #define REG_VTT 0x1E /* ADT7490 only */ 473d849981SJean Delvare #define REG_EXTEND3 0x1F /* ADT7490 only */ 483d849981SJean Delvare 49cffb9dd0SJean Delvare #define REG_VOLTAGE_BASE 0x20 501c301fc5SJordan Crouse #define REG_TEMP_BASE 0x25 511c301fc5SJordan Crouse #define REG_TACH_BASE 0x28 521c301fc5SJordan Crouse #define REG_PWM_BASE 0x30 531c301fc5SJordan Crouse #define REG_PWM_MAX_BASE 0x38 541c301fc5SJordan Crouse 551c301fc5SJordan Crouse #define REG_DEVID 0x3D 561c301fc5SJordan Crouse #define REG_VENDID 0x3E 57d656b6fdSJean Delvare #define REG_DEVID2 0x3F 581c301fc5SJordan Crouse 591c301fc5SJordan Crouse #define REG_STATUS1 0x41 601c301fc5SJordan Crouse #define REG_STATUS2 0x42 611c301fc5SJordan Crouse 62d8d2ee07SJean Delvare #define REG_VID 0x43 /* ADT7476 only */ 63d8d2ee07SJean Delvare 64cffb9dd0SJean Delvare #define REG_VOLTAGE_MIN_BASE 0x44 65cffb9dd0SJean Delvare #define REG_VOLTAGE_MAX_BASE 0x45 661c301fc5SJordan Crouse 671c301fc5SJordan Crouse #define REG_TEMP_MIN_BASE 0x4E 681c301fc5SJordan Crouse #define REG_TEMP_MAX_BASE 0x4F 691c301fc5SJordan Crouse 701c301fc5SJordan Crouse #define REG_TACH_MIN_BASE 0x54 711c301fc5SJordan Crouse 721c301fc5SJordan Crouse #define REG_PWM_CONFIG_BASE 0x5C 731c301fc5SJordan Crouse 741c301fc5SJordan Crouse #define REG_TEMP_TRANGE_BASE 0x5F 751c301fc5SJordan Crouse 761c301fc5SJordan Crouse #define REG_PWM_MIN_BASE 0x64 771c301fc5SJordan Crouse 781c301fc5SJordan Crouse #define REG_TEMP_TMIN_BASE 0x67 791c301fc5SJordan Crouse #define REG_TEMP_THERM_BASE 0x6A 801c301fc5SJordan Crouse 811c301fc5SJordan Crouse #define REG_REMOTE1_HYSTERSIS 0x6D 821c301fc5SJordan Crouse #define REG_REMOTE2_HYSTERSIS 0x6E 831c301fc5SJordan Crouse 841c301fc5SJordan Crouse #define REG_TEMP_OFFSET_BASE 0x70 851c301fc5SJordan Crouse 86ebfaf1fbSJean Delvare #define REG_CONFIG2 0x73 87ebfaf1fbSJean Delvare 881c301fc5SJordan Crouse #define REG_EXTEND1 0x76 891c301fc5SJordan Crouse #define REG_EXTEND2 0x77 90378933c9SJean Delvare 91378933c9SJean Delvare #define REG_CONFIG3 0x78 921c301fc5SJordan Crouse #define REG_CONFIG5 0x7C 93f99318b2SJean Delvare #define REG_CONFIG4 0x7D 94f99318b2SJean Delvare 953d849981SJean Delvare #define REG_STATUS4 0x81 /* ADT7490 only */ 963d849981SJean Delvare 973d849981SJean Delvare #define REG_VTT_MIN 0x84 /* ADT7490 only */ 983d849981SJean Delvare #define REG_VTT_MAX 0x86 /* ADT7490 only */ 993d849981SJean Delvare 100d8d2ee07SJean Delvare #define VID_VIDSEL 0x80 /* ADT7476 only */ 101d8d2ee07SJean Delvare 102ebfaf1fbSJean Delvare #define CONFIG2_ATTN 0x20 103ebfaf1fbSJean Delvare 104378933c9SJean Delvare #define CONFIG3_SMBALERT 0x01 105378933c9SJean Delvare #define CONFIG3_THERM 0x02 106378933c9SJean Delvare 107378933c9SJean Delvare #define CONFIG4_PINFUNC 0x03 108f99318b2SJean Delvare #define CONFIG4_MAXDUTY 0x08 109ebfaf1fbSJean Delvare #define CONFIG4_ATTN_IN10 0x30 110ebfaf1fbSJean Delvare #define CONFIG4_ATTN_IN43 0xC0 1111c301fc5SJordan Crouse 1121c301fc5SJordan Crouse #define CONFIG5_TWOSCOMP 0x01 1131c301fc5SJordan Crouse #define CONFIG5_TEMPOFFSET 0x02 11454fe4671SJean Delvare #define CONFIG5_VIDGPIO 0x10 /* ADT7476 only */ 1151c301fc5SJordan Crouse 1161c301fc5SJordan Crouse /* ADT7475 Settings */ 1171c301fc5SJordan Crouse 118cffb9dd0SJean Delvare #define ADT7475_VOLTAGE_COUNT 5 /* Not counting Vtt */ 1191c301fc5SJordan Crouse #define ADT7475_TEMP_COUNT 3 1201c301fc5SJordan Crouse #define ADT7475_TACH_COUNT 4 1211c301fc5SJordan Crouse #define ADT7475_PWM_COUNT 3 1221c301fc5SJordan Crouse 1231c301fc5SJordan Crouse /* Macro to read the registers */ 1241c301fc5SJordan Crouse 1251c301fc5SJordan Crouse #define adt7475_read(reg) i2c_smbus_read_byte_data(client, (reg)) 1261c301fc5SJordan Crouse 1271c301fc5SJordan Crouse /* Macros to easily index the registers */ 1281c301fc5SJordan Crouse 1291c301fc5SJordan Crouse #define TACH_REG(idx) (REG_TACH_BASE + ((idx) * 2)) 1301c301fc5SJordan Crouse #define TACH_MIN_REG(idx) (REG_TACH_MIN_BASE + ((idx) * 2)) 1311c301fc5SJordan Crouse 1321c301fc5SJordan Crouse #define PWM_REG(idx) (REG_PWM_BASE + (idx)) 1331c301fc5SJordan Crouse #define PWM_MAX_REG(idx) (REG_PWM_MAX_BASE + (idx)) 1341c301fc5SJordan Crouse #define PWM_MIN_REG(idx) (REG_PWM_MIN_BASE + (idx)) 1351c301fc5SJordan Crouse #define PWM_CONFIG_REG(idx) (REG_PWM_CONFIG_BASE + (idx)) 1361c301fc5SJordan Crouse 1371c301fc5SJordan Crouse #define VOLTAGE_REG(idx) (REG_VOLTAGE_BASE + (idx)) 1381c301fc5SJordan Crouse #define VOLTAGE_MIN_REG(idx) (REG_VOLTAGE_MIN_BASE + ((idx) * 2)) 1391c301fc5SJordan Crouse #define VOLTAGE_MAX_REG(idx) (REG_VOLTAGE_MAX_BASE + ((idx) * 2)) 1401c301fc5SJordan Crouse 1411c301fc5SJordan Crouse #define TEMP_REG(idx) (REG_TEMP_BASE + (idx)) 1421c301fc5SJordan Crouse #define TEMP_MIN_REG(idx) (REG_TEMP_MIN_BASE + ((idx) * 2)) 1431c301fc5SJordan Crouse #define TEMP_MAX_REG(idx) (REG_TEMP_MAX_BASE + ((idx) * 2)) 1441c301fc5SJordan Crouse #define TEMP_TMIN_REG(idx) (REG_TEMP_TMIN_BASE + (idx)) 1451c301fc5SJordan Crouse #define TEMP_THERM_REG(idx) (REG_TEMP_THERM_BASE + (idx)) 1461c301fc5SJordan Crouse #define TEMP_OFFSET_REG(idx) (REG_TEMP_OFFSET_BASE + (idx)) 1471c301fc5SJordan Crouse #define TEMP_TRANGE_REG(idx) (REG_TEMP_TRANGE_BASE + (idx)) 1481c301fc5SJordan Crouse 149918ee91cSJean Delvare static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END }; 1501c301fc5SJordan Crouse 151e5e9f44cSJean Delvare enum chips { adt7473, adt7475, adt7476, adt7490 }; 1521c301fc5SJordan Crouse 1531c301fc5SJordan Crouse static const struct i2c_device_id adt7475_id[] = { 154b180d050SJean Delvare { "adt7473", adt7473 }, 1551c301fc5SJordan Crouse { "adt7475", adt7475 }, 156d8d2ee07SJean Delvare { "adt7476", adt7476 }, 1573d849981SJean Delvare { "adt7490", adt7490 }, 1581c301fc5SJordan Crouse { } 1591c301fc5SJordan Crouse }; 1601c301fc5SJordan Crouse MODULE_DEVICE_TABLE(i2c, adt7475_id); 1611c301fc5SJordan Crouse 1621c301fc5SJordan Crouse struct adt7475_data { 1631c301fc5SJordan Crouse struct device *hwmon_dev; 1641c301fc5SJordan Crouse struct mutex lock; 1651c301fc5SJordan Crouse 1661c301fc5SJordan Crouse unsigned long measure_updated; 1671c301fc5SJordan Crouse unsigned long limits_updated; 1681c301fc5SJordan Crouse char valid; 1691c301fc5SJordan Crouse 170f99318b2SJean Delvare u8 config4; 1711c301fc5SJordan Crouse u8 config5; 172cffb9dd0SJean Delvare u8 has_voltage; 173ebfaf1fbSJean Delvare u8 bypass_attn; /* Bypass voltage attenuator */ 174378933c9SJean Delvare u8 has_pwm2:1; 175378933c9SJean Delvare u8 has_fan4:1; 17654fe4671SJean Delvare u8 has_vid:1; 1773d849981SJean Delvare u32 alarms; 178cffb9dd0SJean Delvare u16 voltage[3][6]; 1791c301fc5SJordan Crouse u16 temp[7][3]; 1801c301fc5SJordan Crouse u16 tach[2][4]; 1811c301fc5SJordan Crouse u8 pwm[4][3]; 1821c301fc5SJordan Crouse u8 range[3]; 1831c301fc5SJordan Crouse u8 pwmctl[3]; 1841c301fc5SJordan Crouse u8 pwmchan[3]; 18554fe4671SJean Delvare 18654fe4671SJean Delvare u8 vid; 18754fe4671SJean Delvare u8 vrm; 1881c301fc5SJordan Crouse }; 1891c301fc5SJordan Crouse 1901c301fc5SJordan Crouse static struct i2c_driver adt7475_driver; 1911c301fc5SJordan Crouse static struct adt7475_data *adt7475_update_device(struct device *dev); 1921c301fc5SJordan Crouse static void adt7475_read_hystersis(struct i2c_client *client); 1931c301fc5SJordan Crouse static void adt7475_read_pwm(struct i2c_client *client, int index); 1941c301fc5SJordan Crouse 1951c301fc5SJordan Crouse /* Given a temp value, convert it to register value */ 1961c301fc5SJordan Crouse 1971c301fc5SJordan Crouse static inline u16 temp2reg(struct adt7475_data *data, long val) 1981c301fc5SJordan Crouse { 1991c301fc5SJordan Crouse u16 ret; 2001c301fc5SJordan Crouse 2011c301fc5SJordan Crouse if (!(data->config5 & CONFIG5_TWOSCOMP)) { 2021c301fc5SJordan Crouse val = SENSORS_LIMIT(val, -64000, 191000); 2031c301fc5SJordan Crouse ret = (val + 64500) / 1000; 2041c301fc5SJordan Crouse } else { 2051c301fc5SJordan Crouse val = SENSORS_LIMIT(val, -128000, 127000); 2061c301fc5SJordan Crouse if (val < -500) 2071c301fc5SJordan Crouse ret = (256500 + val) / 1000; 2081c301fc5SJordan Crouse else 2091c301fc5SJordan Crouse ret = (val + 500) / 1000; 2101c301fc5SJordan Crouse } 2111c301fc5SJordan Crouse 2121c301fc5SJordan Crouse return ret << 2; 2131c301fc5SJordan Crouse } 2141c301fc5SJordan Crouse 2151c301fc5SJordan Crouse /* Given a register value, convert it to a real temp value */ 2161c301fc5SJordan Crouse 2171c301fc5SJordan Crouse static inline int reg2temp(struct adt7475_data *data, u16 reg) 2181c301fc5SJordan Crouse { 2191c301fc5SJordan Crouse if (data->config5 & CONFIG5_TWOSCOMP) { 2201c301fc5SJordan Crouse if (reg >= 512) 2211c301fc5SJordan Crouse return (reg - 1024) * 250; 2221c301fc5SJordan Crouse else 2231c301fc5SJordan Crouse return reg * 250; 2241c301fc5SJordan Crouse } else 2251c301fc5SJordan Crouse return (reg - 256) * 250; 2261c301fc5SJordan Crouse } 2271c301fc5SJordan Crouse 2281c301fc5SJordan Crouse static inline int tach2rpm(u16 tach) 2291c301fc5SJordan Crouse { 2301c301fc5SJordan Crouse if (tach == 0 || tach == 0xFFFF) 2311c301fc5SJordan Crouse return 0; 2321c301fc5SJordan Crouse 2331c301fc5SJordan Crouse return (90000 * 60) / tach; 2341c301fc5SJordan Crouse } 2351c301fc5SJordan Crouse 2361c301fc5SJordan Crouse static inline u16 rpm2tach(unsigned long rpm) 2371c301fc5SJordan Crouse { 2381c301fc5SJordan Crouse if (rpm == 0) 2391c301fc5SJordan Crouse return 0; 2401c301fc5SJordan Crouse 2411c301fc5SJordan Crouse return SENSORS_LIMIT((90000 * 60) / rpm, 1, 0xFFFF); 2421c301fc5SJordan Crouse } 2431c301fc5SJordan Crouse 244cffb9dd0SJean Delvare /* Scaling factors for voltage inputs, taken from the ADT7490 datasheet */ 245cffb9dd0SJean Delvare static const int adt7473_in_scaling[ADT7475_VOLTAGE_COUNT + 1][2] = { 246cffb9dd0SJean Delvare { 45, 94 }, /* +2.5V */ 247cffb9dd0SJean Delvare { 175, 525 }, /* Vccp */ 248cffb9dd0SJean Delvare { 68, 71 }, /* Vcc */ 249cffb9dd0SJean Delvare { 93, 47 }, /* +5V */ 250cffb9dd0SJean Delvare { 120, 20 }, /* +12V */ 251cffb9dd0SJean Delvare { 45, 45 }, /* Vtt */ 252cffb9dd0SJean Delvare }; 253cffb9dd0SJean Delvare 254ebfaf1fbSJean Delvare static inline int reg2volt(int channel, u16 reg, u8 bypass_attn) 2551c301fc5SJordan Crouse { 256cffb9dd0SJean Delvare const int *r = adt7473_in_scaling[channel]; 257cffb9dd0SJean Delvare 258ebfaf1fbSJean Delvare if (bypass_attn & (1 << channel)) 259ebfaf1fbSJean Delvare return DIV_ROUND_CLOSEST(reg * 2250, 1024); 260cffb9dd0SJean Delvare return DIV_ROUND_CLOSEST(reg * (r[0] + r[1]) * 2250, r[1] * 1024); 2611c301fc5SJordan Crouse } 2621c301fc5SJordan Crouse 263ebfaf1fbSJean Delvare static inline u16 volt2reg(int channel, long volt, u8 bypass_attn) 2641c301fc5SJordan Crouse { 265cffb9dd0SJean Delvare const int *r = adt7473_in_scaling[channel]; 266cffb9dd0SJean Delvare long reg; 2671c301fc5SJordan Crouse 268ebfaf1fbSJean Delvare if (bypass_attn & (1 << channel)) 269ebfaf1fbSJean Delvare reg = (volt * 1024) / 2250; 270ebfaf1fbSJean Delvare else 271cffb9dd0SJean Delvare reg = (volt * r[1] * 1024) / ((r[0] + r[1]) * 2250); 272cffb9dd0SJean Delvare return SENSORS_LIMIT(reg, 0, 1023) & (0xff << 2); 2731c301fc5SJordan Crouse } 2741c301fc5SJordan Crouse 2751c301fc5SJordan Crouse static u16 adt7475_read_word(struct i2c_client *client, int reg) 2761c301fc5SJordan Crouse { 2771c301fc5SJordan Crouse u16 val; 2781c301fc5SJordan Crouse 2791c301fc5SJordan Crouse val = i2c_smbus_read_byte_data(client, reg); 2801c301fc5SJordan Crouse val |= (i2c_smbus_read_byte_data(client, reg + 1) << 8); 2811c301fc5SJordan Crouse 2821c301fc5SJordan Crouse return val; 2831c301fc5SJordan Crouse } 2841c301fc5SJordan Crouse 2851c301fc5SJordan Crouse static void adt7475_write_word(struct i2c_client *client, int reg, u16 val) 2861c301fc5SJordan Crouse { 2871c301fc5SJordan Crouse i2c_smbus_write_byte_data(client, reg + 1, val >> 8); 2881c301fc5SJordan Crouse i2c_smbus_write_byte_data(client, reg, val & 0xFF); 2891c301fc5SJordan Crouse } 2901c301fc5SJordan Crouse 2911c301fc5SJordan Crouse /* Find the nearest value in a table - used for pwm frequency and 2921c301fc5SJordan Crouse auto temp range */ 2931c301fc5SJordan Crouse static int find_nearest(long val, const int *array, int size) 2941c301fc5SJordan Crouse { 2951c301fc5SJordan Crouse int i; 2961c301fc5SJordan Crouse 2971c301fc5SJordan Crouse if (val < array[0]) 2981c301fc5SJordan Crouse return 0; 2991c301fc5SJordan Crouse 3001c301fc5SJordan Crouse if (val > array[size - 1]) 3011c301fc5SJordan Crouse return size - 1; 3021c301fc5SJordan Crouse 3031c301fc5SJordan Crouse for (i = 0; i < size - 1; i++) { 3041c301fc5SJordan Crouse int a, b; 3051c301fc5SJordan Crouse 3061c301fc5SJordan Crouse if (val > array[i + 1]) 3071c301fc5SJordan Crouse continue; 3081c301fc5SJordan Crouse 3091c301fc5SJordan Crouse a = val - array[i]; 3101c301fc5SJordan Crouse b = array[i + 1] - val; 3111c301fc5SJordan Crouse 3121c301fc5SJordan Crouse return (a <= b) ? i : i + 1; 3131c301fc5SJordan Crouse } 3141c301fc5SJordan Crouse 3151c301fc5SJordan Crouse return 0; 3161c301fc5SJordan Crouse } 3171c301fc5SJordan Crouse 3181c301fc5SJordan Crouse static ssize_t show_voltage(struct device *dev, struct device_attribute *attr, 3191c301fc5SJordan Crouse char *buf) 3201c301fc5SJordan Crouse { 3211c301fc5SJordan Crouse struct adt7475_data *data = adt7475_update_device(dev); 3221c301fc5SJordan Crouse struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); 3231c301fc5SJordan Crouse unsigned short val; 3241c301fc5SJordan Crouse 3251c301fc5SJordan Crouse switch (sattr->nr) { 3261c301fc5SJordan Crouse case ALARM: 3271c301fc5SJordan Crouse return sprintf(buf, "%d\n", 328cffb9dd0SJean Delvare (data->alarms >> sattr->index) & 1); 3291c301fc5SJordan Crouse default: 3301c301fc5SJordan Crouse val = data->voltage[sattr->nr][sattr->index]; 331ebfaf1fbSJean Delvare return sprintf(buf, "%d\n", 332ebfaf1fbSJean Delvare reg2volt(sattr->index, val, data->bypass_attn)); 3331c301fc5SJordan Crouse } 3341c301fc5SJordan Crouse } 3351c301fc5SJordan Crouse 3361c301fc5SJordan Crouse static ssize_t set_voltage(struct device *dev, struct device_attribute *attr, 3371c301fc5SJordan Crouse const char *buf, size_t count) 3381c301fc5SJordan Crouse { 3391c301fc5SJordan Crouse 3401c301fc5SJordan Crouse struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); 3411c301fc5SJordan Crouse struct i2c_client *client = to_i2c_client(dev); 3421c301fc5SJordan Crouse struct adt7475_data *data = i2c_get_clientdata(client); 3431c301fc5SJordan Crouse unsigned char reg; 3441c301fc5SJordan Crouse long val; 3451c301fc5SJordan Crouse 346179c4fdbSFrans Meulenbroeks if (kstrtol(buf, 10, &val)) 3471c301fc5SJordan Crouse return -EINVAL; 3481c301fc5SJordan Crouse 3491c301fc5SJordan Crouse mutex_lock(&data->lock); 3501c301fc5SJordan Crouse 351ebfaf1fbSJean Delvare data->voltage[sattr->nr][sattr->index] = 352ebfaf1fbSJean Delvare volt2reg(sattr->index, val, data->bypass_attn); 3531c301fc5SJordan Crouse 3543d849981SJean Delvare if (sattr->index < ADT7475_VOLTAGE_COUNT) { 3551c301fc5SJordan Crouse if (sattr->nr == MIN) 3561c301fc5SJordan Crouse reg = VOLTAGE_MIN_REG(sattr->index); 3571c301fc5SJordan Crouse else 3581c301fc5SJordan Crouse reg = VOLTAGE_MAX_REG(sattr->index); 3593d849981SJean Delvare } else { 3603d849981SJean Delvare if (sattr->nr == MIN) 3613d849981SJean Delvare reg = REG_VTT_MIN; 3623d849981SJean Delvare else 3633d849981SJean Delvare reg = REG_VTT_MAX; 3643d849981SJean Delvare } 3651c301fc5SJordan Crouse 3661c301fc5SJordan Crouse i2c_smbus_write_byte_data(client, reg, 3671c301fc5SJordan Crouse data->voltage[sattr->nr][sattr->index] >> 2); 3681c301fc5SJordan Crouse mutex_unlock(&data->lock); 3691c301fc5SJordan Crouse 3701c301fc5SJordan Crouse return count; 3711c301fc5SJordan Crouse } 3721c301fc5SJordan Crouse 3731c301fc5SJordan Crouse static ssize_t show_temp(struct device *dev, struct device_attribute *attr, 3741c301fc5SJordan Crouse char *buf) 3751c301fc5SJordan Crouse { 3761c301fc5SJordan Crouse struct adt7475_data *data = adt7475_update_device(dev); 3771c301fc5SJordan Crouse struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); 3781c301fc5SJordan Crouse int out; 3791c301fc5SJordan Crouse 3801c301fc5SJordan Crouse switch (sattr->nr) { 3811c301fc5SJordan Crouse case HYSTERSIS: 3821c301fc5SJordan Crouse mutex_lock(&data->lock); 3831c301fc5SJordan Crouse out = data->temp[sattr->nr][sattr->index]; 3841c301fc5SJordan Crouse if (sattr->index != 1) 3851c301fc5SJordan Crouse out = (out >> 4) & 0xF; 3861c301fc5SJordan Crouse else 3871c301fc5SJordan Crouse out = (out & 0xF); 3881c301fc5SJordan Crouse /* Show the value as an absolute number tied to 3891c301fc5SJordan Crouse * THERM */ 3901c301fc5SJordan Crouse out = reg2temp(data, data->temp[THERM][sattr->index]) - 3911c301fc5SJordan Crouse out * 1000; 3921c301fc5SJordan Crouse mutex_unlock(&data->lock); 3931c301fc5SJordan Crouse break; 3941c301fc5SJordan Crouse 3951c301fc5SJordan Crouse case OFFSET: 3961c301fc5SJordan Crouse /* Offset is always 2's complement, regardless of the 3971c301fc5SJordan Crouse * setting in CONFIG5 */ 3981c301fc5SJordan Crouse mutex_lock(&data->lock); 3991c301fc5SJordan Crouse out = (s8)data->temp[sattr->nr][sattr->index]; 4001c301fc5SJordan Crouse if (data->config5 & CONFIG5_TEMPOFFSET) 4011c301fc5SJordan Crouse out *= 1000; 4021c301fc5SJordan Crouse else 4031c301fc5SJordan Crouse out *= 500; 4041c301fc5SJordan Crouse mutex_unlock(&data->lock); 4051c301fc5SJordan Crouse break; 4061c301fc5SJordan Crouse 4071c301fc5SJordan Crouse case ALARM: 4081c301fc5SJordan Crouse out = (data->alarms >> (sattr->index + 4)) & 1; 4091c301fc5SJordan Crouse break; 4101c301fc5SJordan Crouse 4111c301fc5SJordan Crouse case FAULT: 4121c301fc5SJordan Crouse /* Note - only for remote1 and remote2 */ 413cf312e07SJean Delvare out = !!(data->alarms & (sattr->index ? 0x8000 : 0x4000)); 4141c301fc5SJordan Crouse break; 4151c301fc5SJordan Crouse 4161c301fc5SJordan Crouse default: 4171c301fc5SJordan Crouse /* All other temp values are in the configured format */ 4181c301fc5SJordan Crouse out = reg2temp(data, data->temp[sattr->nr][sattr->index]); 4191c301fc5SJordan Crouse } 4201c301fc5SJordan Crouse 4211c301fc5SJordan Crouse return sprintf(buf, "%d\n", out); 4221c301fc5SJordan Crouse } 4231c301fc5SJordan Crouse 4241c301fc5SJordan Crouse static ssize_t set_temp(struct device *dev, struct device_attribute *attr, 4251c301fc5SJordan Crouse const char *buf, size_t count) 4261c301fc5SJordan Crouse { 4271c301fc5SJordan Crouse struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); 4281c301fc5SJordan Crouse struct i2c_client *client = to_i2c_client(dev); 4291c301fc5SJordan Crouse struct adt7475_data *data = i2c_get_clientdata(client); 4301c301fc5SJordan Crouse unsigned char reg = 0; 4311c301fc5SJordan Crouse u8 out; 4321c301fc5SJordan Crouse int temp; 4331c301fc5SJordan Crouse long val; 4341c301fc5SJordan Crouse 435179c4fdbSFrans Meulenbroeks if (kstrtol(buf, 10, &val)) 4361c301fc5SJordan Crouse return -EINVAL; 4371c301fc5SJordan Crouse 4381c301fc5SJordan Crouse mutex_lock(&data->lock); 4391c301fc5SJordan Crouse 4401c301fc5SJordan Crouse /* We need the config register in all cases for temp <-> reg conv. */ 4411c301fc5SJordan Crouse data->config5 = adt7475_read(REG_CONFIG5); 4421c301fc5SJordan Crouse 4431c301fc5SJordan Crouse switch (sattr->nr) { 4441c301fc5SJordan Crouse case OFFSET: 4451c301fc5SJordan Crouse if (data->config5 & CONFIG5_TEMPOFFSET) { 4461c301fc5SJordan Crouse val = SENSORS_LIMIT(val, -63000, 127000); 4471c301fc5SJordan Crouse out = data->temp[OFFSET][sattr->index] = val / 1000; 4481c301fc5SJordan Crouse } else { 4491c301fc5SJordan Crouse val = SENSORS_LIMIT(val, -63000, 64000); 4501c301fc5SJordan Crouse out = data->temp[OFFSET][sattr->index] = val / 500; 4511c301fc5SJordan Crouse } 4521c301fc5SJordan Crouse break; 4531c301fc5SJordan Crouse 4541c301fc5SJordan Crouse case HYSTERSIS: 4551c301fc5SJordan Crouse /* The value will be given as an absolute value, turn it 4561c301fc5SJordan Crouse into an offset based on THERM */ 4571c301fc5SJordan Crouse 4581c301fc5SJordan Crouse /* Read fresh THERM and HYSTERSIS values from the chip */ 4591c301fc5SJordan Crouse data->temp[THERM][sattr->index] = 4601c301fc5SJordan Crouse adt7475_read(TEMP_THERM_REG(sattr->index)) << 2; 4611c301fc5SJordan Crouse adt7475_read_hystersis(client); 4621c301fc5SJordan Crouse 4631c301fc5SJordan Crouse temp = reg2temp(data, data->temp[THERM][sattr->index]); 4641c301fc5SJordan Crouse val = SENSORS_LIMIT(val, temp - 15000, temp); 4651c301fc5SJordan Crouse val = (temp - val) / 1000; 4661c301fc5SJordan Crouse 4671c301fc5SJordan Crouse if (sattr->index != 1) { 4681c301fc5SJordan Crouse data->temp[HYSTERSIS][sattr->index] &= 0xF0; 4691c301fc5SJordan Crouse data->temp[HYSTERSIS][sattr->index] |= (val & 0xF) << 4; 4701c301fc5SJordan Crouse } else { 4711c301fc5SJordan Crouse data->temp[HYSTERSIS][sattr->index] &= 0x0F; 4721c301fc5SJordan Crouse data->temp[HYSTERSIS][sattr->index] |= (val & 0xF); 4731c301fc5SJordan Crouse } 4741c301fc5SJordan Crouse 4751c301fc5SJordan Crouse out = data->temp[HYSTERSIS][sattr->index]; 4761c301fc5SJordan Crouse break; 4771c301fc5SJordan Crouse 4781c301fc5SJordan Crouse default: 4791c301fc5SJordan Crouse data->temp[sattr->nr][sattr->index] = temp2reg(data, val); 4801c301fc5SJordan Crouse 4811c301fc5SJordan Crouse /* We maintain an extra 2 digits of precision for simplicity 4821c301fc5SJordan Crouse * - shift those back off before writing the value */ 4831c301fc5SJordan Crouse out = (u8) (data->temp[sattr->nr][sattr->index] >> 2); 4841c301fc5SJordan Crouse } 4851c301fc5SJordan Crouse 4861c301fc5SJordan Crouse switch (sattr->nr) { 4871c301fc5SJordan Crouse case MIN: 4881c301fc5SJordan Crouse reg = TEMP_MIN_REG(sattr->index); 4891c301fc5SJordan Crouse break; 4901c301fc5SJordan Crouse case MAX: 4911c301fc5SJordan Crouse reg = TEMP_MAX_REG(sattr->index); 4921c301fc5SJordan Crouse break; 4931c301fc5SJordan Crouse case OFFSET: 4941c301fc5SJordan Crouse reg = TEMP_OFFSET_REG(sattr->index); 4951c301fc5SJordan Crouse break; 4961c301fc5SJordan Crouse case AUTOMIN: 4971c301fc5SJordan Crouse reg = TEMP_TMIN_REG(sattr->index); 4981c301fc5SJordan Crouse break; 4991c301fc5SJordan Crouse case THERM: 5001c301fc5SJordan Crouse reg = TEMP_THERM_REG(sattr->index); 5011c301fc5SJordan Crouse break; 5021c301fc5SJordan Crouse case HYSTERSIS: 5031c301fc5SJordan Crouse if (sattr->index != 2) 5041c301fc5SJordan Crouse reg = REG_REMOTE1_HYSTERSIS; 5051c301fc5SJordan Crouse else 5061c301fc5SJordan Crouse reg = REG_REMOTE2_HYSTERSIS; 5071c301fc5SJordan Crouse 5081c301fc5SJordan Crouse break; 5091c301fc5SJordan Crouse } 5101c301fc5SJordan Crouse 5111c301fc5SJordan Crouse i2c_smbus_write_byte_data(client, reg, out); 5121c301fc5SJordan Crouse 5131c301fc5SJordan Crouse mutex_unlock(&data->lock); 5141c301fc5SJordan Crouse return count; 5151c301fc5SJordan Crouse } 5161c301fc5SJordan Crouse 5171c301fc5SJordan Crouse /* Table of autorange values - the user will write the value in millidegrees, 5181c301fc5SJordan Crouse and we'll convert it */ 5191c301fc5SJordan Crouse static const int autorange_table[] = { 5201c301fc5SJordan Crouse 2000, 2500, 3330, 4000, 5000, 6670, 8000, 5211c301fc5SJordan Crouse 10000, 13330, 16000, 20000, 26670, 32000, 40000, 5221c301fc5SJordan Crouse 53330, 80000 5231c301fc5SJordan Crouse }; 5241c301fc5SJordan Crouse 5251c301fc5SJordan Crouse static ssize_t show_point2(struct device *dev, struct device_attribute *attr, 5261c301fc5SJordan Crouse char *buf) 5271c301fc5SJordan Crouse { 5281c301fc5SJordan Crouse struct adt7475_data *data = adt7475_update_device(dev); 5291c301fc5SJordan Crouse struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); 5301c301fc5SJordan Crouse int out, val; 5311c301fc5SJordan Crouse 5321c301fc5SJordan Crouse mutex_lock(&data->lock); 5331c301fc5SJordan Crouse out = (data->range[sattr->index] >> 4) & 0x0F; 5341c301fc5SJordan Crouse val = reg2temp(data, data->temp[AUTOMIN][sattr->index]); 5351c301fc5SJordan Crouse mutex_unlock(&data->lock); 5361c301fc5SJordan Crouse 5371c301fc5SJordan Crouse return sprintf(buf, "%d\n", val + autorange_table[out]); 5381c301fc5SJordan Crouse } 5391c301fc5SJordan Crouse 5401c301fc5SJordan Crouse static ssize_t set_point2(struct device *dev, struct device_attribute *attr, 5411c301fc5SJordan Crouse const char *buf, size_t count) 5421c301fc5SJordan Crouse { 5431c301fc5SJordan Crouse struct i2c_client *client = to_i2c_client(dev); 5441c301fc5SJordan Crouse struct adt7475_data *data = i2c_get_clientdata(client); 5451c301fc5SJordan Crouse struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); 5461c301fc5SJordan Crouse int temp; 5471c301fc5SJordan Crouse long val; 5481c301fc5SJordan Crouse 549179c4fdbSFrans Meulenbroeks if (kstrtol(buf, 10, &val)) 5501c301fc5SJordan Crouse return -EINVAL; 5511c301fc5SJordan Crouse 5521c301fc5SJordan Crouse mutex_lock(&data->lock); 5531c301fc5SJordan Crouse 5541c301fc5SJordan Crouse /* Get a fresh copy of the needed registers */ 5551c301fc5SJordan Crouse data->config5 = adt7475_read(REG_CONFIG5); 5561c301fc5SJordan Crouse data->temp[AUTOMIN][sattr->index] = 5571c301fc5SJordan Crouse adt7475_read(TEMP_TMIN_REG(sattr->index)) << 2; 5581c301fc5SJordan Crouse data->range[sattr->index] = 5591c301fc5SJordan Crouse adt7475_read(TEMP_TRANGE_REG(sattr->index)); 5601c301fc5SJordan Crouse 5611c301fc5SJordan Crouse /* The user will write an absolute value, so subtract the start point 5621c301fc5SJordan Crouse to figure the range */ 5631c301fc5SJordan Crouse temp = reg2temp(data, data->temp[AUTOMIN][sattr->index]); 5641c301fc5SJordan Crouse val = SENSORS_LIMIT(val, temp + autorange_table[0], 5651c301fc5SJordan Crouse temp + autorange_table[ARRAY_SIZE(autorange_table) - 1]); 5661c301fc5SJordan Crouse val -= temp; 5671c301fc5SJordan Crouse 5681c301fc5SJordan Crouse /* Find the nearest table entry to what the user wrote */ 5691c301fc5SJordan Crouse val = find_nearest(val, autorange_table, ARRAY_SIZE(autorange_table)); 5701c301fc5SJordan Crouse 5711c301fc5SJordan Crouse data->range[sattr->index] &= ~0xF0; 5721c301fc5SJordan Crouse data->range[sattr->index] |= val << 4; 5731c301fc5SJordan Crouse 5741c301fc5SJordan Crouse i2c_smbus_write_byte_data(client, TEMP_TRANGE_REG(sattr->index), 5751c301fc5SJordan Crouse data->range[sattr->index]); 5761c301fc5SJordan Crouse 5771c301fc5SJordan Crouse mutex_unlock(&data->lock); 5781c301fc5SJordan Crouse return count; 5791c301fc5SJordan Crouse } 5801c301fc5SJordan Crouse 5811c301fc5SJordan Crouse static ssize_t show_tach(struct device *dev, struct device_attribute *attr, 5821c301fc5SJordan Crouse char *buf) 5831c301fc5SJordan Crouse { 5841c301fc5SJordan Crouse struct adt7475_data *data = adt7475_update_device(dev); 5851c301fc5SJordan Crouse struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); 5861c301fc5SJordan Crouse int out; 5871c301fc5SJordan Crouse 5881c301fc5SJordan Crouse if (sattr->nr == ALARM) 5891c301fc5SJordan Crouse out = (data->alarms >> (sattr->index + 10)) & 1; 5901c301fc5SJordan Crouse else 5911c301fc5SJordan Crouse out = tach2rpm(data->tach[sattr->nr][sattr->index]); 5921c301fc5SJordan Crouse 5931c301fc5SJordan Crouse return sprintf(buf, "%d\n", out); 5941c301fc5SJordan Crouse } 5951c301fc5SJordan Crouse 5961c301fc5SJordan Crouse static ssize_t set_tach(struct device *dev, struct device_attribute *attr, 5971c301fc5SJordan Crouse const char *buf, size_t count) 5981c301fc5SJordan Crouse { 5991c301fc5SJordan Crouse 6001c301fc5SJordan Crouse struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); 6011c301fc5SJordan Crouse struct i2c_client *client = to_i2c_client(dev); 6021c301fc5SJordan Crouse struct adt7475_data *data = i2c_get_clientdata(client); 6031c301fc5SJordan Crouse unsigned long val; 6041c301fc5SJordan Crouse 605179c4fdbSFrans Meulenbroeks if (kstrtoul(buf, 10, &val)) 6061c301fc5SJordan Crouse return -EINVAL; 6071c301fc5SJordan Crouse 6081c301fc5SJordan Crouse mutex_lock(&data->lock); 6091c301fc5SJordan Crouse 6101c301fc5SJordan Crouse data->tach[MIN][sattr->index] = rpm2tach(val); 6111c301fc5SJordan Crouse 6121c301fc5SJordan Crouse adt7475_write_word(client, TACH_MIN_REG(sattr->index), 6131c301fc5SJordan Crouse data->tach[MIN][sattr->index]); 6141c301fc5SJordan Crouse 6151c301fc5SJordan Crouse mutex_unlock(&data->lock); 6161c301fc5SJordan Crouse return count; 6171c301fc5SJordan Crouse } 6181c301fc5SJordan Crouse 6191c301fc5SJordan Crouse static ssize_t show_pwm(struct device *dev, struct device_attribute *attr, 6201c301fc5SJordan Crouse char *buf) 6211c301fc5SJordan Crouse { 6221c301fc5SJordan Crouse struct adt7475_data *data = adt7475_update_device(dev); 6231c301fc5SJordan Crouse struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); 6241c301fc5SJordan Crouse 6251c301fc5SJordan Crouse return sprintf(buf, "%d\n", data->pwm[sattr->nr][sattr->index]); 6261c301fc5SJordan Crouse } 6271c301fc5SJordan Crouse 6281c301fc5SJordan Crouse static ssize_t show_pwmchan(struct device *dev, struct device_attribute *attr, 6291c301fc5SJordan Crouse char *buf) 6301c301fc5SJordan Crouse { 6311c301fc5SJordan Crouse struct adt7475_data *data = adt7475_update_device(dev); 6321c301fc5SJordan Crouse struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); 6331c301fc5SJordan Crouse 6341c301fc5SJordan Crouse return sprintf(buf, "%d\n", data->pwmchan[sattr->index]); 6351c301fc5SJordan Crouse } 6361c301fc5SJordan Crouse 6371c301fc5SJordan Crouse static ssize_t show_pwmctrl(struct device *dev, struct device_attribute *attr, 6381c301fc5SJordan Crouse char *buf) 6391c301fc5SJordan Crouse { 6401c301fc5SJordan Crouse struct adt7475_data *data = adt7475_update_device(dev); 6411c301fc5SJordan Crouse struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); 6421c301fc5SJordan Crouse 6431c301fc5SJordan Crouse return sprintf(buf, "%d\n", data->pwmctl[sattr->index]); 6441c301fc5SJordan Crouse } 6451c301fc5SJordan Crouse 6461c301fc5SJordan Crouse static ssize_t set_pwm(struct device *dev, struct device_attribute *attr, 6471c301fc5SJordan Crouse const char *buf, size_t count) 6481c301fc5SJordan Crouse { 6491c301fc5SJordan Crouse 6501c301fc5SJordan Crouse struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); 6511c301fc5SJordan Crouse struct i2c_client *client = to_i2c_client(dev); 6521c301fc5SJordan Crouse struct adt7475_data *data = i2c_get_clientdata(client); 6531c301fc5SJordan Crouse unsigned char reg = 0; 6541c301fc5SJordan Crouse long val; 6551c301fc5SJordan Crouse 656179c4fdbSFrans Meulenbroeks if (kstrtol(buf, 10, &val)) 6571c301fc5SJordan Crouse return -EINVAL; 6581c301fc5SJordan Crouse 6591c301fc5SJordan Crouse mutex_lock(&data->lock); 6601c301fc5SJordan Crouse 6611c301fc5SJordan Crouse switch (sattr->nr) { 6621c301fc5SJordan Crouse case INPUT: 6631c301fc5SJordan Crouse /* Get a fresh value for CONTROL */ 6641c301fc5SJordan Crouse data->pwm[CONTROL][sattr->index] = 6651c301fc5SJordan Crouse adt7475_read(PWM_CONFIG_REG(sattr->index)); 6661c301fc5SJordan Crouse 6671c301fc5SJordan Crouse /* If we are not in manual mode, then we shouldn't allow 6681c301fc5SJordan Crouse * the user to set the pwm speed */ 6691c301fc5SJordan Crouse if (((data->pwm[CONTROL][sattr->index] >> 5) & 7) != 7) { 6701c301fc5SJordan Crouse mutex_unlock(&data->lock); 6711c301fc5SJordan Crouse return count; 6721c301fc5SJordan Crouse } 6731c301fc5SJordan Crouse 6741c301fc5SJordan Crouse reg = PWM_REG(sattr->index); 6751c301fc5SJordan Crouse break; 6761c301fc5SJordan Crouse 6771c301fc5SJordan Crouse case MIN: 6781c301fc5SJordan Crouse reg = PWM_MIN_REG(sattr->index); 6791c301fc5SJordan Crouse break; 6801c301fc5SJordan Crouse 6811c301fc5SJordan Crouse case MAX: 6821c301fc5SJordan Crouse reg = PWM_MAX_REG(sattr->index); 6831c301fc5SJordan Crouse break; 6841c301fc5SJordan Crouse } 6851c301fc5SJordan Crouse 6861c301fc5SJordan Crouse data->pwm[sattr->nr][sattr->index] = SENSORS_LIMIT(val, 0, 0xFF); 6871c301fc5SJordan Crouse i2c_smbus_write_byte_data(client, reg, 6881c301fc5SJordan Crouse data->pwm[sattr->nr][sattr->index]); 6891c301fc5SJordan Crouse 6901c301fc5SJordan Crouse mutex_unlock(&data->lock); 6911c301fc5SJordan Crouse 6921c301fc5SJordan Crouse return count; 6931c301fc5SJordan Crouse } 6941c301fc5SJordan Crouse 6951c301fc5SJordan Crouse /* Called by set_pwmctrl and set_pwmchan */ 6961c301fc5SJordan Crouse 6971c301fc5SJordan Crouse static int hw_set_pwm(struct i2c_client *client, int index, 6981c301fc5SJordan Crouse unsigned int pwmctl, unsigned int pwmchan) 6991c301fc5SJordan Crouse { 7001c301fc5SJordan Crouse struct adt7475_data *data = i2c_get_clientdata(client); 7011c301fc5SJordan Crouse long val = 0; 7021c301fc5SJordan Crouse 7031c301fc5SJordan Crouse switch (pwmctl) { 7041c301fc5SJordan Crouse case 0: 7051c301fc5SJordan Crouse val = 0x03; /* Run at full speed */ 7061c301fc5SJordan Crouse break; 7071c301fc5SJordan Crouse case 1: 7081c301fc5SJordan Crouse val = 0x07; /* Manual mode */ 7091c301fc5SJordan Crouse break; 7101c301fc5SJordan Crouse case 2: 7111c301fc5SJordan Crouse switch (pwmchan) { 7121c301fc5SJordan Crouse case 1: 7131c301fc5SJordan Crouse /* Remote1 controls PWM */ 7141c301fc5SJordan Crouse val = 0x00; 7151c301fc5SJordan Crouse break; 7161c301fc5SJordan Crouse case 2: 7171c301fc5SJordan Crouse /* local controls PWM */ 7181c301fc5SJordan Crouse val = 0x01; 7191c301fc5SJordan Crouse break; 7201c301fc5SJordan Crouse case 4: 7211c301fc5SJordan Crouse /* remote2 controls PWM */ 7221c301fc5SJordan Crouse val = 0x02; 7231c301fc5SJordan Crouse break; 7241c301fc5SJordan Crouse case 6: 7251c301fc5SJordan Crouse /* local/remote2 control PWM */ 7261c301fc5SJordan Crouse val = 0x05; 7271c301fc5SJordan Crouse break; 7281c301fc5SJordan Crouse case 7: 7291c301fc5SJordan Crouse /* All three control PWM */ 7301c301fc5SJordan Crouse val = 0x06; 7311c301fc5SJordan Crouse break; 7321c301fc5SJordan Crouse default: 7331c301fc5SJordan Crouse return -EINVAL; 7341c301fc5SJordan Crouse } 7351c301fc5SJordan Crouse break; 7361c301fc5SJordan Crouse default: 7371c301fc5SJordan Crouse return -EINVAL; 7381c301fc5SJordan Crouse } 7391c301fc5SJordan Crouse 7401c301fc5SJordan Crouse data->pwmctl[index] = pwmctl; 7411c301fc5SJordan Crouse data->pwmchan[index] = pwmchan; 7421c301fc5SJordan Crouse 7431c301fc5SJordan Crouse data->pwm[CONTROL][index] &= ~0xE0; 7441c301fc5SJordan Crouse data->pwm[CONTROL][index] |= (val & 7) << 5; 7451c301fc5SJordan Crouse 7461c301fc5SJordan Crouse i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index), 7471c301fc5SJordan Crouse data->pwm[CONTROL][index]); 7481c301fc5SJordan Crouse 7491c301fc5SJordan Crouse return 0; 7501c301fc5SJordan Crouse } 7511c301fc5SJordan Crouse 7521c301fc5SJordan Crouse static ssize_t set_pwmchan(struct device *dev, struct device_attribute *attr, 7531c301fc5SJordan Crouse const char *buf, size_t count) 7541c301fc5SJordan Crouse { 7551c301fc5SJordan Crouse struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); 7561c301fc5SJordan Crouse struct i2c_client *client = to_i2c_client(dev); 7571c301fc5SJordan Crouse struct adt7475_data *data = i2c_get_clientdata(client); 7581c301fc5SJordan Crouse int r; 7591c301fc5SJordan Crouse long val; 7601c301fc5SJordan Crouse 761179c4fdbSFrans Meulenbroeks if (kstrtol(buf, 10, &val)) 7621c301fc5SJordan Crouse return -EINVAL; 7631c301fc5SJordan Crouse 7641c301fc5SJordan Crouse mutex_lock(&data->lock); 7651c301fc5SJordan Crouse /* Read Modify Write PWM values */ 7661c301fc5SJordan Crouse adt7475_read_pwm(client, sattr->index); 7671c301fc5SJordan Crouse r = hw_set_pwm(client, sattr->index, data->pwmctl[sattr->index], val); 7681c301fc5SJordan Crouse if (r) 7691c301fc5SJordan Crouse count = r; 7701c301fc5SJordan Crouse mutex_unlock(&data->lock); 7711c301fc5SJordan Crouse 7721c301fc5SJordan Crouse return count; 7731c301fc5SJordan Crouse } 7741c301fc5SJordan Crouse 7751c301fc5SJordan Crouse static ssize_t set_pwmctrl(struct device *dev, struct device_attribute *attr, 7761c301fc5SJordan Crouse const char *buf, size_t count) 7771c301fc5SJordan Crouse { 7781c301fc5SJordan Crouse struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); 7791c301fc5SJordan Crouse struct i2c_client *client = to_i2c_client(dev); 7801c301fc5SJordan Crouse struct adt7475_data *data = i2c_get_clientdata(client); 7811c301fc5SJordan Crouse int r; 7821c301fc5SJordan Crouse long val; 7831c301fc5SJordan Crouse 784179c4fdbSFrans Meulenbroeks if (kstrtol(buf, 10, &val)) 7851c301fc5SJordan Crouse return -EINVAL; 7861c301fc5SJordan Crouse 7871c301fc5SJordan Crouse mutex_lock(&data->lock); 7881c301fc5SJordan Crouse /* Read Modify Write PWM values */ 7891c301fc5SJordan Crouse adt7475_read_pwm(client, sattr->index); 7901c301fc5SJordan Crouse r = hw_set_pwm(client, sattr->index, val, data->pwmchan[sattr->index]); 7911c301fc5SJordan Crouse if (r) 7921c301fc5SJordan Crouse count = r; 7931c301fc5SJordan Crouse mutex_unlock(&data->lock); 7941c301fc5SJordan Crouse 7951c301fc5SJordan Crouse return count; 7961c301fc5SJordan Crouse } 7971c301fc5SJordan Crouse 7981c301fc5SJordan Crouse /* List of frequencies for the PWM */ 7991c301fc5SJordan Crouse static const int pwmfreq_table[] = { 8001c301fc5SJordan Crouse 11, 14, 22, 29, 35, 44, 58, 88 8011c301fc5SJordan Crouse }; 8021c301fc5SJordan Crouse 8031c301fc5SJordan Crouse static ssize_t show_pwmfreq(struct device *dev, struct device_attribute *attr, 8041c301fc5SJordan Crouse char *buf) 8051c301fc5SJordan Crouse { 8061c301fc5SJordan Crouse struct adt7475_data *data = adt7475_update_device(dev); 8071c301fc5SJordan Crouse struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); 8081c301fc5SJordan Crouse 8091c301fc5SJordan Crouse return sprintf(buf, "%d\n", 8101c301fc5SJordan Crouse pwmfreq_table[data->range[sattr->index] & 7]); 8111c301fc5SJordan Crouse } 8121c301fc5SJordan Crouse 8131c301fc5SJordan Crouse static ssize_t set_pwmfreq(struct device *dev, struct device_attribute *attr, 8141c301fc5SJordan Crouse const char *buf, size_t count) 8151c301fc5SJordan Crouse { 8161c301fc5SJordan Crouse struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); 8171c301fc5SJordan Crouse struct i2c_client *client = to_i2c_client(dev); 8181c301fc5SJordan Crouse struct adt7475_data *data = i2c_get_clientdata(client); 8191c301fc5SJordan Crouse int out; 8201c301fc5SJordan Crouse long val; 8211c301fc5SJordan Crouse 822179c4fdbSFrans Meulenbroeks if (kstrtol(buf, 10, &val)) 8231c301fc5SJordan Crouse return -EINVAL; 8241c301fc5SJordan Crouse 8251c301fc5SJordan Crouse out = find_nearest(val, pwmfreq_table, ARRAY_SIZE(pwmfreq_table)); 8261c301fc5SJordan Crouse 8271c301fc5SJordan Crouse mutex_lock(&data->lock); 8281c301fc5SJordan Crouse 8291c301fc5SJordan Crouse data->range[sattr->index] = 8301c301fc5SJordan Crouse adt7475_read(TEMP_TRANGE_REG(sattr->index)); 8311c301fc5SJordan Crouse data->range[sattr->index] &= ~7; 8321c301fc5SJordan Crouse data->range[sattr->index] |= out; 8331c301fc5SJordan Crouse 8341c301fc5SJordan Crouse i2c_smbus_write_byte_data(client, TEMP_TRANGE_REG(sattr->index), 8351c301fc5SJordan Crouse data->range[sattr->index]); 8361c301fc5SJordan Crouse 8371c301fc5SJordan Crouse mutex_unlock(&data->lock); 8381c301fc5SJordan Crouse return count; 8391c301fc5SJordan Crouse } 8401c301fc5SJordan Crouse 841f99318b2SJean Delvare static ssize_t show_pwm_at_crit(struct device *dev, 842f99318b2SJean Delvare struct device_attribute *devattr, char *buf) 843f99318b2SJean Delvare { 844f99318b2SJean Delvare struct adt7475_data *data = adt7475_update_device(dev); 845f99318b2SJean Delvare return sprintf(buf, "%d\n", !!(data->config4 & CONFIG4_MAXDUTY)); 846f99318b2SJean Delvare } 847f99318b2SJean Delvare 848f99318b2SJean Delvare static ssize_t set_pwm_at_crit(struct device *dev, 849f99318b2SJean Delvare struct device_attribute *devattr, 850f99318b2SJean Delvare const char *buf, size_t count) 851f99318b2SJean Delvare { 852f99318b2SJean Delvare struct i2c_client *client = to_i2c_client(dev); 853f99318b2SJean Delvare struct adt7475_data *data = i2c_get_clientdata(client); 854f99318b2SJean Delvare long val; 855f99318b2SJean Delvare 856179c4fdbSFrans Meulenbroeks if (kstrtol(buf, 10, &val)) 857f99318b2SJean Delvare return -EINVAL; 858f99318b2SJean Delvare if (val != 0 && val != 1) 859f99318b2SJean Delvare return -EINVAL; 860f99318b2SJean Delvare 861f99318b2SJean Delvare mutex_lock(&data->lock); 862f99318b2SJean Delvare data->config4 = i2c_smbus_read_byte_data(client, REG_CONFIG4); 863f99318b2SJean Delvare if (val) 864f99318b2SJean Delvare data->config4 |= CONFIG4_MAXDUTY; 865f99318b2SJean Delvare else 866f99318b2SJean Delvare data->config4 &= ~CONFIG4_MAXDUTY; 867f99318b2SJean Delvare i2c_smbus_write_byte_data(client, REG_CONFIG4, data->config4); 868f99318b2SJean Delvare mutex_unlock(&data->lock); 869f99318b2SJean Delvare 870f99318b2SJean Delvare return count; 871f99318b2SJean Delvare } 872f99318b2SJean Delvare 87354fe4671SJean Delvare static ssize_t show_vrm(struct device *dev, struct device_attribute *devattr, 87454fe4671SJean Delvare char *buf) 87554fe4671SJean Delvare { 87654fe4671SJean Delvare struct adt7475_data *data = dev_get_drvdata(dev); 87754fe4671SJean Delvare return sprintf(buf, "%d\n", (int)data->vrm); 87854fe4671SJean Delvare } 87954fe4671SJean Delvare 88054fe4671SJean Delvare static ssize_t set_vrm(struct device *dev, struct device_attribute *devattr, 88154fe4671SJean Delvare const char *buf, size_t count) 88254fe4671SJean Delvare { 88354fe4671SJean Delvare struct adt7475_data *data = dev_get_drvdata(dev); 88454fe4671SJean Delvare long val; 88554fe4671SJean Delvare 886179c4fdbSFrans Meulenbroeks if (kstrtol(buf, 10, &val)) 88754fe4671SJean Delvare return -EINVAL; 88854fe4671SJean Delvare if (val < 0 || val > 255) 88954fe4671SJean Delvare return -EINVAL; 89054fe4671SJean Delvare data->vrm = val; 89154fe4671SJean Delvare 89254fe4671SJean Delvare return count; 89354fe4671SJean Delvare } 89454fe4671SJean Delvare 89554fe4671SJean Delvare static ssize_t show_vid(struct device *dev, struct device_attribute *devattr, 89654fe4671SJean Delvare char *buf) 89754fe4671SJean Delvare { 89854fe4671SJean Delvare struct adt7475_data *data = adt7475_update_device(dev); 89954fe4671SJean Delvare return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm)); 90054fe4671SJean Delvare } 90154fe4671SJean Delvare 9023d849981SJean Delvare static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_voltage, NULL, INPUT, 0); 9033d849981SJean Delvare static SENSOR_DEVICE_ATTR_2(in0_max, S_IRUGO | S_IWUSR, show_voltage, 9043d849981SJean Delvare set_voltage, MAX, 0); 9053d849981SJean Delvare static SENSOR_DEVICE_ATTR_2(in0_min, S_IRUGO | S_IWUSR, show_voltage, 9063d849981SJean Delvare set_voltage, MIN, 0); 9073d849981SJean Delvare static SENSOR_DEVICE_ATTR_2(in0_alarm, S_IRUGO, show_voltage, NULL, ALARM, 0); 908cffb9dd0SJean Delvare static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_voltage, NULL, INPUT, 1); 9091c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(in1_max, S_IRUGO | S_IWUSR, show_voltage, 9101c301fc5SJordan Crouse set_voltage, MAX, 1); 911cffb9dd0SJean Delvare static SENSOR_DEVICE_ATTR_2(in1_min, S_IRUGO | S_IWUSR, show_voltage, 9121c301fc5SJordan Crouse set_voltage, MIN, 1); 913cffb9dd0SJean Delvare static SENSOR_DEVICE_ATTR_2(in1_alarm, S_IRUGO, show_voltage, NULL, ALARM, 1); 914cffb9dd0SJean Delvare static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_voltage, NULL, INPUT, 2); 915cffb9dd0SJean Delvare static SENSOR_DEVICE_ATTR_2(in2_max, S_IRUGO | S_IWUSR, show_voltage, 916cffb9dd0SJean Delvare set_voltage, MAX, 2); 917cffb9dd0SJean Delvare static SENSOR_DEVICE_ATTR_2(in2_min, S_IRUGO | S_IWUSR, show_voltage, 918cffb9dd0SJean Delvare set_voltage, MIN, 2); 919cffb9dd0SJean Delvare static SENSOR_DEVICE_ATTR_2(in2_alarm, S_IRUGO, show_voltage, NULL, ALARM, 2); 9203d849981SJean Delvare static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_voltage, NULL, INPUT, 3); 9213d849981SJean Delvare static SENSOR_DEVICE_ATTR_2(in3_max, S_IRUGO | S_IWUSR, show_voltage, 9223d849981SJean Delvare set_voltage, MAX, 3); 9233d849981SJean Delvare static SENSOR_DEVICE_ATTR_2(in3_min, S_IRUGO | S_IWUSR, show_voltage, 9243d849981SJean Delvare set_voltage, MIN, 3); 9253d849981SJean Delvare static SENSOR_DEVICE_ATTR_2(in3_alarm, S_IRUGO, show_voltage, NULL, ALARM, 3); 9263d849981SJean Delvare static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_voltage, NULL, INPUT, 4); 9273d849981SJean Delvare static SENSOR_DEVICE_ATTR_2(in4_max, S_IRUGO | S_IWUSR, show_voltage, 9283d849981SJean Delvare set_voltage, MAX, 4); 9293d849981SJean Delvare static SENSOR_DEVICE_ATTR_2(in4_min, S_IRUGO | S_IWUSR, show_voltage, 9303d849981SJean Delvare set_voltage, MIN, 4); 9313d849981SJean Delvare static SENSOR_DEVICE_ATTR_2(in4_alarm, S_IRUGO, show_voltage, NULL, ALARM, 8); 9323d849981SJean Delvare static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_voltage, NULL, INPUT, 5); 9333d849981SJean Delvare static SENSOR_DEVICE_ATTR_2(in5_max, S_IRUGO | S_IWUSR, show_voltage, 9343d849981SJean Delvare set_voltage, MAX, 5); 9353d849981SJean Delvare static SENSOR_DEVICE_ATTR_2(in5_min, S_IRUGO | S_IWUSR, show_voltage, 9363d849981SJean Delvare set_voltage, MIN, 5); 9373d849981SJean Delvare static SENSOR_DEVICE_ATTR_2(in5_alarm, S_IRUGO, show_voltage, NULL, ALARM, 31); 9381c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, INPUT, 0); 9391c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp1_alarm, S_IRUGO, show_temp, NULL, ALARM, 0); 9401c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp1_fault, S_IRUGO, show_temp, NULL, FAULT, 0); 9411c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, set_temp, 9421c301fc5SJordan Crouse MAX, 0); 9431c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp1_min, S_IRUGO | S_IWUSR, show_temp, set_temp, 9441c301fc5SJordan Crouse MIN, 0); 9451c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp1_offset, S_IRUGO | S_IWUSR, show_temp, 9461c301fc5SJordan Crouse set_temp, OFFSET, 0); 9471c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp1_auto_point1_temp, S_IRUGO | S_IWUSR, 9481c301fc5SJordan Crouse show_temp, set_temp, AUTOMIN, 0); 9491c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp1_auto_point2_temp, S_IRUGO | S_IWUSR, 9501c301fc5SJordan Crouse show_point2, set_point2, 0, 0); 9511c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp1_crit, S_IRUGO | S_IWUSR, show_temp, set_temp, 9521c301fc5SJordan Crouse THERM, 0); 9531c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp1_crit_hyst, S_IRUGO | S_IWUSR, show_temp, 9541c301fc5SJordan Crouse set_temp, HYSTERSIS, 0); 9551c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, INPUT, 1); 9561c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp2_alarm, S_IRUGO, show_temp, NULL, ALARM, 1); 9571c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, set_temp, 9581c301fc5SJordan Crouse MAX, 1); 9591c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp2_min, S_IRUGO | S_IWUSR, show_temp, set_temp, 9601c301fc5SJordan Crouse MIN, 1); 9611c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IRUGO | S_IWUSR, show_temp, 9621c301fc5SJordan Crouse set_temp, OFFSET, 1); 9631c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp2_auto_point1_temp, S_IRUGO | S_IWUSR, 9641c301fc5SJordan Crouse show_temp, set_temp, AUTOMIN, 1); 9651c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp2_auto_point2_temp, S_IRUGO | S_IWUSR, 9661c301fc5SJordan Crouse show_point2, set_point2, 0, 1); 9671c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp2_crit, S_IRUGO | S_IWUSR, show_temp, set_temp, 9681c301fc5SJordan Crouse THERM, 1); 9691c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp2_crit_hyst, S_IRUGO | S_IWUSR, show_temp, 9701c301fc5SJordan Crouse set_temp, HYSTERSIS, 1); 9711c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, INPUT, 2); 9721c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp3_alarm, S_IRUGO, show_temp, NULL, ALARM, 2); 9731c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp3_fault, S_IRUGO, show_temp, NULL, FAULT, 2); 9741c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, set_temp, 9751c301fc5SJordan Crouse MAX, 2); 9761c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp3_min, S_IRUGO | S_IWUSR, show_temp, set_temp, 9771c301fc5SJordan Crouse MIN, 2); 9781c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp3_offset, S_IRUGO | S_IWUSR, show_temp, 9791c301fc5SJordan Crouse set_temp, OFFSET, 2); 9801c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp3_auto_point1_temp, S_IRUGO | S_IWUSR, 9811c301fc5SJordan Crouse show_temp, set_temp, AUTOMIN, 2); 9821c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp3_auto_point2_temp, S_IRUGO | S_IWUSR, 9831c301fc5SJordan Crouse show_point2, set_point2, 0, 2); 9841c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp3_crit, S_IRUGO | S_IWUSR, show_temp, set_temp, 9851c301fc5SJordan Crouse THERM, 2); 9861c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp3_crit_hyst, S_IRUGO | S_IWUSR, show_temp, 9871c301fc5SJordan Crouse set_temp, HYSTERSIS, 2); 9881c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(fan1_input, S_IRUGO, show_tach, NULL, INPUT, 0); 9891c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(fan1_min, S_IRUGO | S_IWUSR, show_tach, set_tach, 9901c301fc5SJordan Crouse MIN, 0); 9911c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(fan1_alarm, S_IRUGO, show_tach, NULL, ALARM, 0); 9921c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(fan2_input, S_IRUGO, show_tach, NULL, INPUT, 1); 9931c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(fan2_min, S_IRUGO | S_IWUSR, show_tach, set_tach, 9941c301fc5SJordan Crouse MIN, 1); 9951c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(fan2_alarm, S_IRUGO, show_tach, NULL, ALARM, 1); 9961c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(fan3_input, S_IRUGO, show_tach, NULL, INPUT, 2); 9971c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(fan3_min, S_IRUGO | S_IWUSR, show_tach, set_tach, 9981c301fc5SJordan Crouse MIN, 2); 9991c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(fan3_alarm, S_IRUGO, show_tach, NULL, ALARM, 2); 10001c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(fan4_input, S_IRUGO, show_tach, NULL, INPUT, 3); 10011c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(fan4_min, S_IRUGO | S_IWUSR, show_tach, set_tach, 10021c301fc5SJordan Crouse MIN, 3); 10031c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(fan4_alarm, S_IRUGO, show_tach, NULL, ALARM, 3); 10041c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT, 10051c301fc5SJordan Crouse 0); 10061c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm1_freq, S_IRUGO | S_IWUSR, show_pwmfreq, 10071c301fc5SJordan Crouse set_pwmfreq, INPUT, 0); 10081c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm1_enable, S_IRUGO | S_IWUSR, show_pwmctrl, 10091c301fc5SJordan Crouse set_pwmctrl, INPUT, 0); 101084d2a314SJean Delvare static SENSOR_DEVICE_ATTR_2(pwm1_auto_channels_temp, S_IRUGO | S_IWUSR, 10111c301fc5SJordan Crouse show_pwmchan, set_pwmchan, INPUT, 0); 10121c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm, 10131c301fc5SJordan Crouse set_pwm, MIN, 0); 10141c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm, 10151c301fc5SJordan Crouse set_pwm, MAX, 0); 10161c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm2, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT, 10171c301fc5SJordan Crouse 1); 10181c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm2_freq, S_IRUGO | S_IWUSR, show_pwmfreq, 10191c301fc5SJordan Crouse set_pwmfreq, INPUT, 1); 10201c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm2_enable, S_IRUGO | S_IWUSR, show_pwmctrl, 10211c301fc5SJordan Crouse set_pwmctrl, INPUT, 1); 102284d2a314SJean Delvare static SENSOR_DEVICE_ATTR_2(pwm2_auto_channels_temp, S_IRUGO | S_IWUSR, 10231c301fc5SJordan Crouse show_pwmchan, set_pwmchan, INPUT, 1); 10241c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm, 10251c301fc5SJordan Crouse set_pwm, MIN, 1); 10261c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm, 10271c301fc5SJordan Crouse set_pwm, MAX, 1); 10281c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm3, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT, 10291c301fc5SJordan Crouse 2); 10301c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm3_freq, S_IRUGO | S_IWUSR, show_pwmfreq, 10311c301fc5SJordan Crouse set_pwmfreq, INPUT, 2); 10321c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm3_enable, S_IRUGO | S_IWUSR, show_pwmctrl, 10331c301fc5SJordan Crouse set_pwmctrl, INPUT, 2); 103484d2a314SJean Delvare static SENSOR_DEVICE_ATTR_2(pwm3_auto_channels_temp, S_IRUGO | S_IWUSR, 10351c301fc5SJordan Crouse show_pwmchan, set_pwmchan, INPUT, 2); 10361c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm, 10371c301fc5SJordan Crouse set_pwm, MIN, 2); 10381c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm, 10391c301fc5SJordan Crouse set_pwm, MAX, 2); 10401c301fc5SJordan Crouse 1041f99318b2SJean Delvare /* Non-standard name, might need revisiting */ 1042f99318b2SJean Delvare static DEVICE_ATTR(pwm_use_point2_pwm_at_crit, S_IWUSR | S_IRUGO, 1043f99318b2SJean Delvare show_pwm_at_crit, set_pwm_at_crit); 1044f99318b2SJean Delvare 104554fe4671SJean Delvare static DEVICE_ATTR(vrm, S_IWUSR | S_IRUGO, show_vrm, set_vrm); 104654fe4671SJean Delvare static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL); 104754fe4671SJean Delvare 10481c301fc5SJordan Crouse static struct attribute *adt7475_attrs[] = { 10491c301fc5SJordan Crouse &sensor_dev_attr_in1_input.dev_attr.attr, 10501c301fc5SJordan Crouse &sensor_dev_attr_in1_max.dev_attr.attr, 10511c301fc5SJordan Crouse &sensor_dev_attr_in1_min.dev_attr.attr, 10521c301fc5SJordan Crouse &sensor_dev_attr_in1_alarm.dev_attr.attr, 10531c301fc5SJordan Crouse &sensor_dev_attr_in2_input.dev_attr.attr, 10541c301fc5SJordan Crouse &sensor_dev_attr_in2_max.dev_attr.attr, 10551c301fc5SJordan Crouse &sensor_dev_attr_in2_min.dev_attr.attr, 10561c301fc5SJordan Crouse &sensor_dev_attr_in2_alarm.dev_attr.attr, 10571c301fc5SJordan Crouse &sensor_dev_attr_temp1_input.dev_attr.attr, 10581c301fc5SJordan Crouse &sensor_dev_attr_temp1_alarm.dev_attr.attr, 10591c301fc5SJordan Crouse &sensor_dev_attr_temp1_fault.dev_attr.attr, 10601c301fc5SJordan Crouse &sensor_dev_attr_temp1_max.dev_attr.attr, 10611c301fc5SJordan Crouse &sensor_dev_attr_temp1_min.dev_attr.attr, 10621c301fc5SJordan Crouse &sensor_dev_attr_temp1_offset.dev_attr.attr, 10631c301fc5SJordan Crouse &sensor_dev_attr_temp1_auto_point1_temp.dev_attr.attr, 10641c301fc5SJordan Crouse &sensor_dev_attr_temp1_auto_point2_temp.dev_attr.attr, 10651c301fc5SJordan Crouse &sensor_dev_attr_temp1_crit.dev_attr.attr, 10661c301fc5SJordan Crouse &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr, 10671c301fc5SJordan Crouse &sensor_dev_attr_temp2_input.dev_attr.attr, 10681c301fc5SJordan Crouse &sensor_dev_attr_temp2_alarm.dev_attr.attr, 10691c301fc5SJordan Crouse &sensor_dev_attr_temp2_max.dev_attr.attr, 10701c301fc5SJordan Crouse &sensor_dev_attr_temp2_min.dev_attr.attr, 10711c301fc5SJordan Crouse &sensor_dev_attr_temp2_offset.dev_attr.attr, 10721c301fc5SJordan Crouse &sensor_dev_attr_temp2_auto_point1_temp.dev_attr.attr, 10731c301fc5SJordan Crouse &sensor_dev_attr_temp2_auto_point2_temp.dev_attr.attr, 10741c301fc5SJordan Crouse &sensor_dev_attr_temp2_crit.dev_attr.attr, 10751c301fc5SJordan Crouse &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr, 10761c301fc5SJordan Crouse &sensor_dev_attr_temp3_input.dev_attr.attr, 10771c301fc5SJordan Crouse &sensor_dev_attr_temp3_fault.dev_attr.attr, 10781c301fc5SJordan Crouse &sensor_dev_attr_temp3_alarm.dev_attr.attr, 10791c301fc5SJordan Crouse &sensor_dev_attr_temp3_max.dev_attr.attr, 10801c301fc5SJordan Crouse &sensor_dev_attr_temp3_min.dev_attr.attr, 10811c301fc5SJordan Crouse &sensor_dev_attr_temp3_offset.dev_attr.attr, 10821c301fc5SJordan Crouse &sensor_dev_attr_temp3_auto_point1_temp.dev_attr.attr, 10831c301fc5SJordan Crouse &sensor_dev_attr_temp3_auto_point2_temp.dev_attr.attr, 10841c301fc5SJordan Crouse &sensor_dev_attr_temp3_crit.dev_attr.attr, 10851c301fc5SJordan Crouse &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr, 10861c301fc5SJordan Crouse &sensor_dev_attr_fan1_input.dev_attr.attr, 10871c301fc5SJordan Crouse &sensor_dev_attr_fan1_min.dev_attr.attr, 10881c301fc5SJordan Crouse &sensor_dev_attr_fan1_alarm.dev_attr.attr, 10891c301fc5SJordan Crouse &sensor_dev_attr_fan2_input.dev_attr.attr, 10901c301fc5SJordan Crouse &sensor_dev_attr_fan2_min.dev_attr.attr, 10911c301fc5SJordan Crouse &sensor_dev_attr_fan2_alarm.dev_attr.attr, 10921c301fc5SJordan Crouse &sensor_dev_attr_fan3_input.dev_attr.attr, 10931c301fc5SJordan Crouse &sensor_dev_attr_fan3_min.dev_attr.attr, 10941c301fc5SJordan Crouse &sensor_dev_attr_fan3_alarm.dev_attr.attr, 10951c301fc5SJordan Crouse &sensor_dev_attr_pwm1.dev_attr.attr, 10961c301fc5SJordan Crouse &sensor_dev_attr_pwm1_freq.dev_attr.attr, 10971c301fc5SJordan Crouse &sensor_dev_attr_pwm1_enable.dev_attr.attr, 109884d2a314SJean Delvare &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr, 10991c301fc5SJordan Crouse &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr, 11001c301fc5SJordan Crouse &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr, 11011c301fc5SJordan Crouse &sensor_dev_attr_pwm3.dev_attr.attr, 11021c301fc5SJordan Crouse &sensor_dev_attr_pwm3_freq.dev_attr.attr, 11031c301fc5SJordan Crouse &sensor_dev_attr_pwm3_enable.dev_attr.attr, 110484d2a314SJean Delvare &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr, 11051c301fc5SJordan Crouse &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr, 11061c301fc5SJordan Crouse &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr, 1107f99318b2SJean Delvare &dev_attr_pwm_use_point2_pwm_at_crit.attr, 11081c301fc5SJordan Crouse NULL, 11091c301fc5SJordan Crouse }; 11101c301fc5SJordan Crouse 1111378933c9SJean Delvare static struct attribute *fan4_attrs[] = { 1112378933c9SJean Delvare &sensor_dev_attr_fan4_input.dev_attr.attr, 1113378933c9SJean Delvare &sensor_dev_attr_fan4_min.dev_attr.attr, 1114378933c9SJean Delvare &sensor_dev_attr_fan4_alarm.dev_attr.attr, 1115378933c9SJean Delvare NULL 1116378933c9SJean Delvare }; 1117378933c9SJean Delvare 1118378933c9SJean Delvare static struct attribute *pwm2_attrs[] = { 1119378933c9SJean Delvare &sensor_dev_attr_pwm2.dev_attr.attr, 1120378933c9SJean Delvare &sensor_dev_attr_pwm2_freq.dev_attr.attr, 1121378933c9SJean Delvare &sensor_dev_attr_pwm2_enable.dev_attr.attr, 1122378933c9SJean Delvare &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr, 1123378933c9SJean Delvare &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr, 1124378933c9SJean Delvare &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr, 1125378933c9SJean Delvare NULL 1126378933c9SJean Delvare }; 1127378933c9SJean Delvare 1128378933c9SJean Delvare static struct attribute *in0_attrs[] = { 11293d849981SJean Delvare &sensor_dev_attr_in0_input.dev_attr.attr, 11303d849981SJean Delvare &sensor_dev_attr_in0_max.dev_attr.attr, 11313d849981SJean Delvare &sensor_dev_attr_in0_min.dev_attr.attr, 11323d849981SJean Delvare &sensor_dev_attr_in0_alarm.dev_attr.attr, 1133378933c9SJean Delvare NULL 1134378933c9SJean Delvare }; 1135378933c9SJean Delvare 1136d8d2ee07SJean Delvare static struct attribute *in3_attrs[] = { 11373d849981SJean Delvare &sensor_dev_attr_in3_input.dev_attr.attr, 11383d849981SJean Delvare &sensor_dev_attr_in3_max.dev_attr.attr, 11393d849981SJean Delvare &sensor_dev_attr_in3_min.dev_attr.attr, 11403d849981SJean Delvare &sensor_dev_attr_in3_alarm.dev_attr.attr, 1141d8d2ee07SJean Delvare NULL 1142d8d2ee07SJean Delvare }; 1143d8d2ee07SJean Delvare 1144d8d2ee07SJean Delvare static struct attribute *in4_attrs[] = { 11453d849981SJean Delvare &sensor_dev_attr_in4_input.dev_attr.attr, 11463d849981SJean Delvare &sensor_dev_attr_in4_max.dev_attr.attr, 11473d849981SJean Delvare &sensor_dev_attr_in4_min.dev_attr.attr, 11483d849981SJean Delvare &sensor_dev_attr_in4_alarm.dev_attr.attr, 1149d8d2ee07SJean Delvare NULL 1150d8d2ee07SJean Delvare }; 1151d8d2ee07SJean Delvare 1152d8d2ee07SJean Delvare static struct attribute *in5_attrs[] = { 11533d849981SJean Delvare &sensor_dev_attr_in5_input.dev_attr.attr, 11543d849981SJean Delvare &sensor_dev_attr_in5_max.dev_attr.attr, 11553d849981SJean Delvare &sensor_dev_attr_in5_min.dev_attr.attr, 11563d849981SJean Delvare &sensor_dev_attr_in5_alarm.dev_attr.attr, 11573d849981SJean Delvare NULL 11583d849981SJean Delvare }; 11593d849981SJean Delvare 116054fe4671SJean Delvare static struct attribute *vid_attrs[] = { 116154fe4671SJean Delvare &dev_attr_cpu0_vid.attr, 116254fe4671SJean Delvare &dev_attr_vrm.attr, 116354fe4671SJean Delvare NULL 116454fe4671SJean Delvare }; 116554fe4671SJean Delvare 116654ecb9e3SJean Delvare static struct attribute_group adt7475_attr_group = { .attrs = adt7475_attrs }; 1167378933c9SJean Delvare static struct attribute_group fan4_attr_group = { .attrs = fan4_attrs }; 1168378933c9SJean Delvare static struct attribute_group pwm2_attr_group = { .attrs = pwm2_attrs }; 1169378933c9SJean Delvare static struct attribute_group in0_attr_group = { .attrs = in0_attrs }; 1170d8d2ee07SJean Delvare static struct attribute_group in3_attr_group = { .attrs = in3_attrs }; 1171d8d2ee07SJean Delvare static struct attribute_group in4_attr_group = { .attrs = in4_attrs }; 1172d8d2ee07SJean Delvare static struct attribute_group in5_attr_group = { .attrs = in5_attrs }; 117354fe4671SJean Delvare static struct attribute_group vid_attr_group = { .attrs = vid_attrs }; 11741c301fc5SJordan Crouse 1175310ec792SJean Delvare static int adt7475_detect(struct i2c_client *client, 11761c301fc5SJordan Crouse struct i2c_board_info *info) 11771c301fc5SJordan Crouse { 11781c301fc5SJordan Crouse struct i2c_adapter *adapter = client->adapter; 1179d656b6fdSJean Delvare int vendid, devid, devid2; 1180b180d050SJean Delvare const char *name; 11811c301fc5SJordan Crouse 11821c301fc5SJordan Crouse if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) 11831c301fc5SJordan Crouse return -ENODEV; 11841c301fc5SJordan Crouse 1185b180d050SJean Delvare vendid = adt7475_read(REG_VENDID); 1186d656b6fdSJean Delvare devid2 = adt7475_read(REG_DEVID2); 1187d656b6fdSJean Delvare if (vendid != 0x41 || /* Analog Devices */ 1188d656b6fdSJean Delvare (devid2 & 0xf8) != 0x68) 1189d656b6fdSJean Delvare return -ENODEV; 1190b180d050SJean Delvare 1191d656b6fdSJean Delvare devid = adt7475_read(REG_DEVID); 1192d656b6fdSJean Delvare if (devid == 0x73) 1193b180d050SJean Delvare name = "adt7473"; 1194d656b6fdSJean Delvare else if (devid == 0x75 && client->addr == 0x2e) 1195b180d050SJean Delvare name = "adt7475"; 1196d8d2ee07SJean Delvare else if (devid == 0x76) 1197d8d2ee07SJean Delvare name = "adt7476"; 11983d849981SJean Delvare else if ((devid2 & 0xfc) == 0x6c) 11993d849981SJean Delvare name = "adt7490"; 1200b180d050SJean Delvare else { 1201b180d050SJean Delvare dev_dbg(&adapter->dev, 1202d8d2ee07SJean Delvare "Couldn't detect an ADT7473/75/76/90 part at " 1203b180d050SJean Delvare "0x%02x\n", (unsigned int)client->addr); 12041c301fc5SJordan Crouse return -ENODEV; 12051c301fc5SJordan Crouse } 12061c301fc5SJordan Crouse 1207b180d050SJean Delvare strlcpy(info->type, name, I2C_NAME_SIZE); 12081c301fc5SJordan Crouse 12091c301fc5SJordan Crouse return 0; 12101c301fc5SJordan Crouse } 12111c301fc5SJordan Crouse 12120f14480bSJean Delvare static void adt7475_remove_files(struct i2c_client *client, 12130f14480bSJean Delvare struct adt7475_data *data) 12140f14480bSJean Delvare { 12150f14480bSJean Delvare sysfs_remove_group(&client->dev.kobj, &adt7475_attr_group); 1216378933c9SJean Delvare if (data->has_fan4) 1217378933c9SJean Delvare sysfs_remove_group(&client->dev.kobj, &fan4_attr_group); 1218378933c9SJean Delvare if (data->has_pwm2) 1219378933c9SJean Delvare sysfs_remove_group(&client->dev.kobj, &pwm2_attr_group); 1220378933c9SJean Delvare if (data->has_voltage & (1 << 0)) 1221378933c9SJean Delvare sysfs_remove_group(&client->dev.kobj, &in0_attr_group); 1222d8d2ee07SJean Delvare if (data->has_voltage & (1 << 3)) 1223d8d2ee07SJean Delvare sysfs_remove_group(&client->dev.kobj, &in3_attr_group); 1224d8d2ee07SJean Delvare if (data->has_voltage & (1 << 4)) 1225d8d2ee07SJean Delvare sysfs_remove_group(&client->dev.kobj, &in4_attr_group); 1226d8d2ee07SJean Delvare if (data->has_voltage & (1 << 5)) 1227d8d2ee07SJean Delvare sysfs_remove_group(&client->dev.kobj, &in5_attr_group); 122854fe4671SJean Delvare if (data->has_vid) 122954fe4671SJean Delvare sysfs_remove_group(&client->dev.kobj, &vid_attr_group); 12300f14480bSJean Delvare } 12310f14480bSJean Delvare 12321c301fc5SJordan Crouse static int adt7475_probe(struct i2c_client *client, 12331c301fc5SJordan Crouse const struct i2c_device_id *id) 12341c301fc5SJordan Crouse { 1235*99b8c83aSFrans Meulenbroeks static const char * const names[] = { 1236d07ca4adSJean Delvare [adt7473] = "ADT7473", 1237d07ca4adSJean Delvare [adt7475] = "ADT7475", 1238d8d2ee07SJean Delvare [adt7476] = "ADT7476", 1239d07ca4adSJean Delvare [adt7490] = "ADT7490", 1240d07ca4adSJean Delvare }; 1241d07ca4adSJean Delvare 12421c301fc5SJordan Crouse struct adt7475_data *data; 1243378933c9SJean Delvare int i, ret = 0, revision; 1244ebfaf1fbSJean Delvare u8 config2, config3; 12451c301fc5SJordan Crouse 12461c301fc5SJordan Crouse data = kzalloc(sizeof(*data), GFP_KERNEL); 12471c301fc5SJordan Crouse if (data == NULL) 12481c301fc5SJordan Crouse return -ENOMEM; 12491c301fc5SJordan Crouse 12501c301fc5SJordan Crouse mutex_init(&data->lock); 12511c301fc5SJordan Crouse i2c_set_clientdata(client, data); 12521c301fc5SJordan Crouse 1253cffb9dd0SJean Delvare /* Initialize device-specific values */ 1254cffb9dd0SJean Delvare switch (id->driver_data) { 1255d8d2ee07SJean Delvare case adt7476: 1256d8d2ee07SJean Delvare data->has_voltage = 0x0e; /* in1 to in3 */ 1257d8d2ee07SJean Delvare revision = adt7475_read(REG_DEVID2) & 0x07; 1258d8d2ee07SJean Delvare break; 12593d849981SJean Delvare case adt7490: 1260378933c9SJean Delvare data->has_voltage = 0x3e; /* in1 to in5 */ 1261378933c9SJean Delvare revision = adt7475_read(REG_DEVID2) & 0x03; 1262d07ca4adSJean Delvare if (revision == 0x03) 1263d07ca4adSJean Delvare revision += adt7475_read(REG_DEVREV2); 12643d849981SJean Delvare break; 1265cffb9dd0SJean Delvare default: 1266cffb9dd0SJean Delvare data->has_voltage = 0x06; /* in1, in2 */ 1267378933c9SJean Delvare revision = adt7475_read(REG_DEVID2) & 0x07; 1268378933c9SJean Delvare } 1269378933c9SJean Delvare 1270378933c9SJean Delvare config3 = adt7475_read(REG_CONFIG3); 1271378933c9SJean Delvare /* Pin PWM2 may alternatively be used for ALERT output */ 1272378933c9SJean Delvare if (!(config3 & CONFIG3_SMBALERT)) 1273378933c9SJean Delvare data->has_pwm2 = 1; 1274378933c9SJean Delvare /* Meaning of this bit is inverted for the ADT7473-1 */ 1275378933c9SJean Delvare if (id->driver_data == adt7473 && revision >= 1) 1276378933c9SJean Delvare data->has_pwm2 = !data->has_pwm2; 1277378933c9SJean Delvare 1278378933c9SJean Delvare data->config4 = adt7475_read(REG_CONFIG4); 1279378933c9SJean Delvare /* Pin TACH4 may alternatively be used for THERM */ 1280378933c9SJean Delvare if ((data->config4 & CONFIG4_PINFUNC) == 0x0) 1281378933c9SJean Delvare data->has_fan4 = 1; 1282378933c9SJean Delvare 1283d8d2ee07SJean Delvare /* THERM configuration is more complex on the ADT7476 and ADT7490, 1284d8d2ee07SJean Delvare because 2 different pins (TACH4 and +2.5 Vin) can be used for 1285d8d2ee07SJean Delvare this function */ 1286378933c9SJean Delvare if (id->driver_data == adt7490) { 1287378933c9SJean Delvare if ((data->config4 & CONFIG4_PINFUNC) == 0x1 && 1288378933c9SJean Delvare !(config3 & CONFIG3_THERM)) 1289378933c9SJean Delvare data->has_fan4 = 1; 1290d8d2ee07SJean Delvare } 1291d8d2ee07SJean Delvare if (id->driver_data == adt7476 || id->driver_data == adt7490) { 1292378933c9SJean Delvare if (!(config3 & CONFIG3_THERM) || 1293378933c9SJean Delvare (data->config4 & CONFIG4_PINFUNC) == 0x1) 1294378933c9SJean Delvare data->has_voltage |= (1 << 0); /* in0 */ 1295cffb9dd0SJean Delvare } 1296cffb9dd0SJean Delvare 129754fe4671SJean Delvare /* On the ADT7476, the +12V input pin may instead be used as VID5, 129854fe4671SJean Delvare and VID pins may alternatively be used as GPIO */ 1299d8d2ee07SJean Delvare if (id->driver_data == adt7476) { 1300d8d2ee07SJean Delvare u8 vid = adt7475_read(REG_VID); 1301d8d2ee07SJean Delvare if (!(vid & VID_VIDSEL)) 1302d8d2ee07SJean Delvare data->has_voltage |= (1 << 4); /* in4 */ 130354fe4671SJean Delvare 130454fe4671SJean Delvare data->has_vid = !(adt7475_read(REG_CONFIG5) & CONFIG5_VIDGPIO); 1305d8d2ee07SJean Delvare } 1306d8d2ee07SJean Delvare 1307ebfaf1fbSJean Delvare /* Voltage attenuators can be bypassed, globally or individually */ 1308ebfaf1fbSJean Delvare config2 = adt7475_read(REG_CONFIG2); 1309ebfaf1fbSJean Delvare if (config2 & CONFIG2_ATTN) { 1310ebfaf1fbSJean Delvare data->bypass_attn = (0x3 << 3) | 0x3; 1311ebfaf1fbSJean Delvare } else { 1312ebfaf1fbSJean Delvare data->bypass_attn = ((data->config4 & CONFIG4_ATTN_IN10) >> 4) | 1313ebfaf1fbSJean Delvare ((data->config4 & CONFIG4_ATTN_IN43) >> 3); 1314ebfaf1fbSJean Delvare } 1315ebfaf1fbSJean Delvare data->bypass_attn &= data->has_voltage; 1316ebfaf1fbSJean Delvare 13171c301fc5SJordan Crouse /* Call adt7475_read_pwm for all pwm's as this will reprogram any 13181c301fc5SJordan Crouse pwm's which are disabled to manual mode with 0% duty cycle */ 13191c301fc5SJordan Crouse for (i = 0; i < ADT7475_PWM_COUNT; i++) 13201c301fc5SJordan Crouse adt7475_read_pwm(client, i); 13211c301fc5SJordan Crouse 13221c301fc5SJordan Crouse ret = sysfs_create_group(&client->dev.kobj, &adt7475_attr_group); 13231c301fc5SJordan Crouse if (ret) 13241c301fc5SJordan Crouse goto efree; 13251c301fc5SJordan Crouse 1326378933c9SJean Delvare /* Features that can be disabled individually */ 1327378933c9SJean Delvare if (data->has_fan4) { 1328378933c9SJean Delvare ret = sysfs_create_group(&client->dev.kobj, &fan4_attr_group); 1329378933c9SJean Delvare if (ret) 1330378933c9SJean Delvare goto eremove; 1331378933c9SJean Delvare } 1332378933c9SJean Delvare if (data->has_pwm2) { 1333378933c9SJean Delvare ret = sysfs_create_group(&client->dev.kobj, &pwm2_attr_group); 1334378933c9SJean Delvare if (ret) 1335378933c9SJean Delvare goto eremove; 1336378933c9SJean Delvare } 1337378933c9SJean Delvare if (data->has_voltage & (1 << 0)) { 1338378933c9SJean Delvare ret = sysfs_create_group(&client->dev.kobj, &in0_attr_group); 1339378933c9SJean Delvare if (ret) 1340378933c9SJean Delvare goto eremove; 1341378933c9SJean Delvare } 1342d8d2ee07SJean Delvare if (data->has_voltage & (1 << 3)) { 1343d8d2ee07SJean Delvare ret = sysfs_create_group(&client->dev.kobj, &in3_attr_group); 1344d8d2ee07SJean Delvare if (ret) 1345d8d2ee07SJean Delvare goto eremove; 1346d8d2ee07SJean Delvare } 1347d8d2ee07SJean Delvare if (data->has_voltage & (1 << 4)) { 1348d8d2ee07SJean Delvare ret = sysfs_create_group(&client->dev.kobj, &in4_attr_group); 1349d8d2ee07SJean Delvare if (ret) 1350d8d2ee07SJean Delvare goto eremove; 1351d8d2ee07SJean Delvare } 1352d8d2ee07SJean Delvare if (data->has_voltage & (1 << 5)) { 1353d8d2ee07SJean Delvare ret = sysfs_create_group(&client->dev.kobj, &in5_attr_group); 1354d8d2ee07SJean Delvare if (ret) 1355d8d2ee07SJean Delvare goto eremove; 1356d8d2ee07SJean Delvare } 135754fe4671SJean Delvare if (data->has_vid) { 135854fe4671SJean Delvare data->vrm = vid_which_vrm(); 135954fe4671SJean Delvare ret = sysfs_create_group(&client->dev.kobj, &vid_attr_group); 136054fe4671SJean Delvare if (ret) 136154fe4671SJean Delvare goto eremove; 136254fe4671SJean Delvare } 1363378933c9SJean Delvare 13641c301fc5SJordan Crouse data->hwmon_dev = hwmon_device_register(&client->dev); 13651c301fc5SJordan Crouse if (IS_ERR(data->hwmon_dev)) { 13661c301fc5SJordan Crouse ret = PTR_ERR(data->hwmon_dev); 13671c301fc5SJordan Crouse goto eremove; 13681c301fc5SJordan Crouse } 13691c301fc5SJordan Crouse 1370d07ca4adSJean Delvare dev_info(&client->dev, "%s device, revision %d\n", 1371d07ca4adSJean Delvare names[id->driver_data], revision); 1372d8d2ee07SJean Delvare if ((data->has_voltage & 0x11) || data->has_fan4 || data->has_pwm2) 137354fe4671SJean Delvare dev_info(&client->dev, "Optional features:%s%s%s%s%s\n", 1374d07ca4adSJean Delvare (data->has_voltage & (1 << 0)) ? " in0" : "", 1375d8d2ee07SJean Delvare (data->has_voltage & (1 << 4)) ? " in4" : "", 1376d07ca4adSJean Delvare data->has_fan4 ? " fan4" : "", 137754fe4671SJean Delvare data->has_pwm2 ? " pwm2" : "", 137854fe4671SJean Delvare data->has_vid ? " vid" : ""); 1379ebfaf1fbSJean Delvare if (data->bypass_attn) 1380ebfaf1fbSJean Delvare dev_info(&client->dev, "Bypassing attenuators on:%s%s%s%s\n", 1381ebfaf1fbSJean Delvare (data->bypass_attn & (1 << 0)) ? " in0" : "", 1382ebfaf1fbSJean Delvare (data->bypass_attn & (1 << 1)) ? " in1" : "", 1383ebfaf1fbSJean Delvare (data->bypass_attn & (1 << 3)) ? " in3" : "", 1384ebfaf1fbSJean Delvare (data->bypass_attn & (1 << 4)) ? " in4" : ""); 1385d07ca4adSJean Delvare 13861c301fc5SJordan Crouse return 0; 13871c301fc5SJordan Crouse 13881c301fc5SJordan Crouse eremove: 13890f14480bSJean Delvare adt7475_remove_files(client, data); 13901c301fc5SJordan Crouse efree: 13911c301fc5SJordan Crouse kfree(data); 13921c301fc5SJordan Crouse return ret; 13931c301fc5SJordan Crouse } 13941c301fc5SJordan Crouse 13951c301fc5SJordan Crouse static int adt7475_remove(struct i2c_client *client) 13961c301fc5SJordan Crouse { 13971c301fc5SJordan Crouse struct adt7475_data *data = i2c_get_clientdata(client); 13981c301fc5SJordan Crouse 13991c301fc5SJordan Crouse hwmon_device_unregister(data->hwmon_dev); 14000f14480bSJean Delvare adt7475_remove_files(client, data); 14011c301fc5SJordan Crouse kfree(data); 14021c301fc5SJordan Crouse 14031c301fc5SJordan Crouse return 0; 14041c301fc5SJordan Crouse } 14051c301fc5SJordan Crouse 14061c301fc5SJordan Crouse static struct i2c_driver adt7475_driver = { 14071c301fc5SJordan Crouse .class = I2C_CLASS_HWMON, 14081c301fc5SJordan Crouse .driver = { 14091c301fc5SJordan Crouse .name = "adt7475", 14101c301fc5SJordan Crouse }, 14111c301fc5SJordan Crouse .probe = adt7475_probe, 14121c301fc5SJordan Crouse .remove = adt7475_remove, 14131c301fc5SJordan Crouse .id_table = adt7475_id, 14141c301fc5SJordan Crouse .detect = adt7475_detect, 1415c3813d6aSJean Delvare .address_list = normal_i2c, 14161c301fc5SJordan Crouse }; 14171c301fc5SJordan Crouse 14181c301fc5SJordan Crouse static void adt7475_read_hystersis(struct i2c_client *client) 14191c301fc5SJordan Crouse { 14201c301fc5SJordan Crouse struct adt7475_data *data = i2c_get_clientdata(client); 14211c301fc5SJordan Crouse 14221c301fc5SJordan Crouse data->temp[HYSTERSIS][0] = (u16) adt7475_read(REG_REMOTE1_HYSTERSIS); 14231c301fc5SJordan Crouse data->temp[HYSTERSIS][1] = data->temp[HYSTERSIS][0]; 14241c301fc5SJordan Crouse data->temp[HYSTERSIS][2] = (u16) adt7475_read(REG_REMOTE2_HYSTERSIS); 14251c301fc5SJordan Crouse } 14261c301fc5SJordan Crouse 14271c301fc5SJordan Crouse static void adt7475_read_pwm(struct i2c_client *client, int index) 14281c301fc5SJordan Crouse { 14291c301fc5SJordan Crouse struct adt7475_data *data = i2c_get_clientdata(client); 14301c301fc5SJordan Crouse unsigned int v; 14311c301fc5SJordan Crouse 14321c301fc5SJordan Crouse data->pwm[CONTROL][index] = adt7475_read(PWM_CONFIG_REG(index)); 14331c301fc5SJordan Crouse 14341c301fc5SJordan Crouse /* Figure out the internal value for pwmctrl and pwmchan 14351c301fc5SJordan Crouse based on the current settings */ 14361c301fc5SJordan Crouse v = (data->pwm[CONTROL][index] >> 5) & 7; 14371c301fc5SJordan Crouse 14381c301fc5SJordan Crouse if (v == 3) 14391c301fc5SJordan Crouse data->pwmctl[index] = 0; 14401c301fc5SJordan Crouse else if (v == 7) 14411c301fc5SJordan Crouse data->pwmctl[index] = 1; 14421c301fc5SJordan Crouse else if (v == 4) { 14431c301fc5SJordan Crouse /* The fan is disabled - we don't want to 14441c301fc5SJordan Crouse support that, so change to manual mode and 14451c301fc5SJordan Crouse set the duty cycle to 0 instead 14461c301fc5SJordan Crouse */ 14471c301fc5SJordan Crouse data->pwm[INPUT][index] = 0; 14481c301fc5SJordan Crouse data->pwm[CONTROL][index] &= ~0xE0; 14491c301fc5SJordan Crouse data->pwm[CONTROL][index] |= (7 << 5); 14501c301fc5SJordan Crouse 14511c301fc5SJordan Crouse i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index), 14521c301fc5SJordan Crouse data->pwm[INPUT][index]); 14531c301fc5SJordan Crouse 14541c301fc5SJordan Crouse i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index), 14551c301fc5SJordan Crouse data->pwm[CONTROL][index]); 14561c301fc5SJordan Crouse 14571c301fc5SJordan Crouse data->pwmctl[index] = 1; 14581c301fc5SJordan Crouse } else { 14591c301fc5SJordan Crouse data->pwmctl[index] = 2; 14601c301fc5SJordan Crouse 14611c301fc5SJordan Crouse switch (v) { 14621c301fc5SJordan Crouse case 0: 14631c301fc5SJordan Crouse data->pwmchan[index] = 1; 14641c301fc5SJordan Crouse break; 14651c301fc5SJordan Crouse case 1: 14661c301fc5SJordan Crouse data->pwmchan[index] = 2; 14671c301fc5SJordan Crouse break; 14681c301fc5SJordan Crouse case 2: 14691c301fc5SJordan Crouse data->pwmchan[index] = 4; 14701c301fc5SJordan Crouse break; 14711c301fc5SJordan Crouse case 5: 14721c301fc5SJordan Crouse data->pwmchan[index] = 6; 14731c301fc5SJordan Crouse break; 14741c301fc5SJordan Crouse case 6: 14751c301fc5SJordan Crouse data->pwmchan[index] = 7; 14761c301fc5SJordan Crouse break; 14771c301fc5SJordan Crouse } 14781c301fc5SJordan Crouse } 14791c301fc5SJordan Crouse } 14801c301fc5SJordan Crouse 14811c301fc5SJordan Crouse static struct adt7475_data *adt7475_update_device(struct device *dev) 14821c301fc5SJordan Crouse { 14831c301fc5SJordan Crouse struct i2c_client *client = to_i2c_client(dev); 14841c301fc5SJordan Crouse struct adt7475_data *data = i2c_get_clientdata(client); 14853d849981SJean Delvare u16 ext; 14861c301fc5SJordan Crouse int i; 14871c301fc5SJordan Crouse 14881c301fc5SJordan Crouse mutex_lock(&data->lock); 14891c301fc5SJordan Crouse 14901c301fc5SJordan Crouse /* Measurement values update every 2 seconds */ 14911c301fc5SJordan Crouse if (time_after(jiffies, data->measure_updated + HZ * 2) || 14921c301fc5SJordan Crouse !data->valid) { 14931c301fc5SJordan Crouse data->alarms = adt7475_read(REG_STATUS2) << 8; 14941c301fc5SJordan Crouse data->alarms |= adt7475_read(REG_STATUS1); 14951c301fc5SJordan Crouse 14963d849981SJean Delvare ext = (adt7475_read(REG_EXTEND2) << 8) | 14973d849981SJean Delvare adt7475_read(REG_EXTEND1); 1498cffb9dd0SJean Delvare for (i = 0; i < ADT7475_VOLTAGE_COUNT; i++) { 1499cffb9dd0SJean Delvare if (!(data->has_voltage & (1 << i))) 1500cffb9dd0SJean Delvare continue; 15011c301fc5SJordan Crouse data->voltage[INPUT][i] = 15021c301fc5SJordan Crouse (adt7475_read(VOLTAGE_REG(i)) << 2) | 1503cffb9dd0SJean Delvare ((ext >> (i * 2)) & 3); 1504cffb9dd0SJean Delvare } 15051c301fc5SJordan Crouse 15061c301fc5SJordan Crouse for (i = 0; i < ADT7475_TEMP_COUNT; i++) 15071c301fc5SJordan Crouse data->temp[INPUT][i] = 15081c301fc5SJordan Crouse (adt7475_read(TEMP_REG(i)) << 2) | 15093d849981SJean Delvare ((ext >> ((i + 5) * 2)) & 3); 15103d849981SJean Delvare 15113d849981SJean Delvare if (data->has_voltage & (1 << 5)) { 15123d849981SJean Delvare data->alarms |= adt7475_read(REG_STATUS4) << 24; 15133d849981SJean Delvare ext = adt7475_read(REG_EXTEND3); 15143d849981SJean Delvare data->voltage[INPUT][5] = adt7475_read(REG_VTT) << 2 | 15153d849981SJean Delvare ((ext >> 4) & 3); 15163d849981SJean Delvare } 15171c301fc5SJordan Crouse 1518378933c9SJean Delvare for (i = 0; i < ADT7475_TACH_COUNT; i++) { 1519378933c9SJean Delvare if (i == 3 && !data->has_fan4) 1520378933c9SJean Delvare continue; 15211c301fc5SJordan Crouse data->tach[INPUT][i] = 15221c301fc5SJordan Crouse adt7475_read_word(client, TACH_REG(i)); 1523378933c9SJean Delvare } 15241c301fc5SJordan Crouse 15251c301fc5SJordan Crouse /* Updated by hw when in auto mode */ 1526378933c9SJean Delvare for (i = 0; i < ADT7475_PWM_COUNT; i++) { 1527378933c9SJean Delvare if (i == 1 && !data->has_pwm2) 1528378933c9SJean Delvare continue; 15291c301fc5SJordan Crouse data->pwm[INPUT][i] = adt7475_read(PWM_REG(i)); 1530378933c9SJean Delvare } 15311c301fc5SJordan Crouse 153254fe4671SJean Delvare if (data->has_vid) 153354fe4671SJean Delvare data->vid = adt7475_read(REG_VID) & 0x3f; 153454fe4671SJean Delvare 15351c301fc5SJordan Crouse data->measure_updated = jiffies; 15361c301fc5SJordan Crouse } 15371c301fc5SJordan Crouse 15381c301fc5SJordan Crouse /* Limits and settings, should never change update every 60 seconds */ 153956e35eeeSJean Delvare if (time_after(jiffies, data->limits_updated + HZ * 60) || 15401c301fc5SJordan Crouse !data->valid) { 1541f99318b2SJean Delvare data->config4 = adt7475_read(REG_CONFIG4); 15421c301fc5SJordan Crouse data->config5 = adt7475_read(REG_CONFIG5); 15431c301fc5SJordan Crouse 15441c301fc5SJordan Crouse for (i = 0; i < ADT7475_VOLTAGE_COUNT; i++) { 1545cffb9dd0SJean Delvare if (!(data->has_voltage & (1 << i))) 1546cffb9dd0SJean Delvare continue; 15471c301fc5SJordan Crouse /* Adjust values so they match the input precision */ 15481c301fc5SJordan Crouse data->voltage[MIN][i] = 15491c301fc5SJordan Crouse adt7475_read(VOLTAGE_MIN_REG(i)) << 2; 15501c301fc5SJordan Crouse data->voltage[MAX][i] = 15511c301fc5SJordan Crouse adt7475_read(VOLTAGE_MAX_REG(i)) << 2; 15521c301fc5SJordan Crouse } 15531c301fc5SJordan Crouse 15543d849981SJean Delvare if (data->has_voltage & (1 << 5)) { 15553d849981SJean Delvare data->voltage[MIN][5] = adt7475_read(REG_VTT_MIN) << 2; 15563d849981SJean Delvare data->voltage[MAX][5] = adt7475_read(REG_VTT_MAX) << 2; 15573d849981SJean Delvare } 15583d849981SJean Delvare 15591c301fc5SJordan Crouse for (i = 0; i < ADT7475_TEMP_COUNT; i++) { 15601c301fc5SJordan Crouse /* Adjust values so they match the input precision */ 15611c301fc5SJordan Crouse data->temp[MIN][i] = 15621c301fc5SJordan Crouse adt7475_read(TEMP_MIN_REG(i)) << 2; 15631c301fc5SJordan Crouse data->temp[MAX][i] = 15641c301fc5SJordan Crouse adt7475_read(TEMP_MAX_REG(i)) << 2; 15651c301fc5SJordan Crouse data->temp[AUTOMIN][i] = 15661c301fc5SJordan Crouse adt7475_read(TEMP_TMIN_REG(i)) << 2; 15671c301fc5SJordan Crouse data->temp[THERM][i] = 15681c301fc5SJordan Crouse adt7475_read(TEMP_THERM_REG(i)) << 2; 15691c301fc5SJordan Crouse data->temp[OFFSET][i] = 15701c301fc5SJordan Crouse adt7475_read(TEMP_OFFSET_REG(i)); 15711c301fc5SJordan Crouse } 15721c301fc5SJordan Crouse adt7475_read_hystersis(client); 15731c301fc5SJordan Crouse 1574378933c9SJean Delvare for (i = 0; i < ADT7475_TACH_COUNT; i++) { 1575378933c9SJean Delvare if (i == 3 && !data->has_fan4) 1576378933c9SJean Delvare continue; 15771c301fc5SJordan Crouse data->tach[MIN][i] = 15781c301fc5SJordan Crouse adt7475_read_word(client, TACH_MIN_REG(i)); 1579378933c9SJean Delvare } 15801c301fc5SJordan Crouse 15811c301fc5SJordan Crouse for (i = 0; i < ADT7475_PWM_COUNT; i++) { 1582378933c9SJean Delvare if (i == 1 && !data->has_pwm2) 1583378933c9SJean Delvare continue; 15841c301fc5SJordan Crouse data->pwm[MAX][i] = adt7475_read(PWM_MAX_REG(i)); 15851c301fc5SJordan Crouse data->pwm[MIN][i] = adt7475_read(PWM_MIN_REG(i)); 15861c301fc5SJordan Crouse /* Set the channel and control information */ 15871c301fc5SJordan Crouse adt7475_read_pwm(client, i); 15881c301fc5SJordan Crouse } 15891c301fc5SJordan Crouse 15901c301fc5SJordan Crouse data->range[0] = adt7475_read(TEMP_TRANGE_REG(0)); 15911c301fc5SJordan Crouse data->range[1] = adt7475_read(TEMP_TRANGE_REG(1)); 15921c301fc5SJordan Crouse data->range[2] = adt7475_read(TEMP_TRANGE_REG(2)); 15931c301fc5SJordan Crouse 15941c301fc5SJordan Crouse data->limits_updated = jiffies; 15951c301fc5SJordan Crouse data->valid = 1; 15961c301fc5SJordan Crouse } 15971c301fc5SJordan Crouse 15981c301fc5SJordan Crouse mutex_unlock(&data->lock); 15991c301fc5SJordan Crouse 16001c301fc5SJordan Crouse return data; 16011c301fc5SJordan Crouse } 16021c301fc5SJordan Crouse 16031c301fc5SJordan Crouse static int __init sensors_adt7475_init(void) 16041c301fc5SJordan Crouse { 16051c301fc5SJordan Crouse return i2c_add_driver(&adt7475_driver); 16061c301fc5SJordan Crouse } 16071c301fc5SJordan Crouse 16081c301fc5SJordan Crouse static void __exit sensors_adt7475_exit(void) 16091c301fc5SJordan Crouse { 16101c301fc5SJordan Crouse i2c_del_driver(&adt7475_driver); 16111c301fc5SJordan Crouse } 16121c301fc5SJordan Crouse 16131c301fc5SJordan Crouse MODULE_AUTHOR("Advanced Micro Devices, Inc"); 16141c301fc5SJordan Crouse MODULE_DESCRIPTION("adt7475 driver"); 16151c301fc5SJordan Crouse MODULE_LICENSE("GPL"); 16161c301fc5SJordan Crouse 16171c301fc5SJordan Crouse module_init(sensors_adt7475_init); 16181c301fc5SJordan Crouse module_exit(sensors_adt7475_exit); 1619