19431996fSJuerg Haefliger /* 2549edb83SJuerg Haefliger * dme1737.c - Driver for the SMSC DME1737, Asus A8000, SMSC SCH311x and 3549edb83SJuerg Haefliger * SCH5027 Super-I/O chips integrated hardware monitoring features. 4549edb83SJuerg Haefliger * Copyright (c) 2007, 2008 Juerg Haefliger <juergh@gmail.com> 59431996fSJuerg Haefliger * 6e95c237dSJuerg Haefliger * This driver is an I2C/ISA hybrid, meaning that it uses the I2C bus to access 7549edb83SJuerg Haefliger * the chip registers if a DME1737, A8000, or SCH5027 is found and the ISA bus 8549edb83SJuerg Haefliger * if a SCH311x chip is found. Both types of chips have very similar hardware 9e95c237dSJuerg Haefliger * monitoring capabilities but differ in the way they can be accessed. 109431996fSJuerg Haefliger * 119431996fSJuerg Haefliger * This program is free software; you can redistribute it and/or modify 129431996fSJuerg Haefliger * it under the terms of the GNU General Public License as published by 139431996fSJuerg Haefliger * the Free Software Foundation; either version 2 of the License, or 149431996fSJuerg Haefliger * (at your option) any later version. 159431996fSJuerg Haefliger * 169431996fSJuerg Haefliger * This program is distributed in the hope that it will be useful, 179431996fSJuerg Haefliger * but WITHOUT ANY WARRANTY; without even the implied warranty of 189431996fSJuerg Haefliger * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 199431996fSJuerg Haefliger * GNU General Public License for more details. 209431996fSJuerg Haefliger * 219431996fSJuerg Haefliger * You should have received a copy of the GNU General Public License 229431996fSJuerg Haefliger * along with this program; if not, write to the Free Software 239431996fSJuerg Haefliger * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 249431996fSJuerg Haefliger */ 259431996fSJuerg Haefliger 269431996fSJuerg Haefliger #include <linux/module.h> 279431996fSJuerg Haefliger #include <linux/init.h> 289431996fSJuerg Haefliger #include <linux/slab.h> 299431996fSJuerg Haefliger #include <linux/jiffies.h> 309431996fSJuerg Haefliger #include <linux/i2c.h> 31e95c237dSJuerg Haefliger #include <linux/platform_device.h> 329431996fSJuerg Haefliger #include <linux/hwmon.h> 339431996fSJuerg Haefliger #include <linux/hwmon-sysfs.h> 349431996fSJuerg Haefliger #include <linux/hwmon-vid.h> 359431996fSJuerg Haefliger #include <linux/err.h> 369431996fSJuerg Haefliger #include <linux/mutex.h> 37b9acb64aSJean Delvare #include <linux/acpi.h> 386055fae8SH Hartley Sweeten #include <linux/io.h> 399431996fSJuerg Haefliger 40e95c237dSJuerg Haefliger /* ISA device, if found */ 41e95c237dSJuerg Haefliger static struct platform_device *pdev; 42e95c237dSJuerg Haefliger 439431996fSJuerg Haefliger /* Module load parameters */ 449431996fSJuerg Haefliger static int force_start; 459431996fSJuerg Haefliger module_param(force_start, bool, 0); 469431996fSJuerg Haefliger MODULE_PARM_DESC(force_start, "Force the chip to start monitoring inputs"); 479431996fSJuerg Haefliger 4867b671bcSJean Delvare static unsigned short force_id; 4967b671bcSJean Delvare module_param(force_id, ushort, 0); 5067b671bcSJean Delvare MODULE_PARM_DESC(force_id, "Override the detected device ID"); 5167b671bcSJean Delvare 5292430b6fSJuerg Haefliger static int probe_all_addr; 5392430b6fSJuerg Haefliger module_param(probe_all_addr, bool, 0); 5492430b6fSJuerg Haefliger MODULE_PARM_DESC(probe_all_addr, "Include probing of non-standard LPC " 5592430b6fSJuerg Haefliger "addresses"); 5692430b6fSJuerg Haefliger 579431996fSJuerg Haefliger /* Addresses to scan */ 5825e9c86dSMark M. Hoffman static const unsigned short normal_i2c[] = {0x2c, 0x2d, 0x2e, I2C_CLIENT_END}; 599431996fSJuerg Haefliger 609431996fSJuerg Haefliger /* Insmod parameters */ 61549edb83SJuerg Haefliger I2C_CLIENT_INSMOD_2(dme1737, sch5027); 62549edb83SJuerg Haefliger 63549edb83SJuerg Haefliger /* ISA chip types */ 64549edb83SJuerg Haefliger enum isa_chips { sch311x = sch5027 + 1 }; 659431996fSJuerg Haefliger 669431996fSJuerg Haefliger /* --------------------------------------------------------------------- 679431996fSJuerg Haefliger * Registers 689431996fSJuerg Haefliger * 699431996fSJuerg Haefliger * The sensors are defined as follows: 709431996fSJuerg Haefliger * 719431996fSJuerg Haefliger * Voltages Temperatures 729431996fSJuerg Haefliger * -------- ------------ 739431996fSJuerg Haefliger * in0 +5VTR (+5V stdby) temp1 Remote diode 1 749431996fSJuerg Haefliger * in1 Vccp (proc core) temp2 Internal temp 759431996fSJuerg Haefliger * in2 VCC (internal +3.3V) temp3 Remote diode 2 769431996fSJuerg Haefliger * in3 +5V 779431996fSJuerg Haefliger * in4 +12V 789431996fSJuerg Haefliger * in5 VTR (+3.3V stby) 799431996fSJuerg Haefliger * in6 Vbat 809431996fSJuerg Haefliger * 819431996fSJuerg Haefliger * --------------------------------------------------------------------- */ 829431996fSJuerg Haefliger 839431996fSJuerg Haefliger /* Voltages (in) numbered 0-6 (ix) */ 849431996fSJuerg Haefliger #define DME1737_REG_IN(ix) ((ix) < 5 ? 0x20 + (ix) \ 859431996fSJuerg Haefliger : 0x94 + (ix)) 869431996fSJuerg Haefliger #define DME1737_REG_IN_MIN(ix) ((ix) < 5 ? 0x44 + (ix) * 2 \ 879431996fSJuerg Haefliger : 0x91 + (ix) * 2) 889431996fSJuerg Haefliger #define DME1737_REG_IN_MAX(ix) ((ix) < 5 ? 0x45 + (ix) * 2 \ 899431996fSJuerg Haefliger : 0x92 + (ix) * 2) 909431996fSJuerg Haefliger 919431996fSJuerg Haefliger /* Temperatures (temp) numbered 0-2 (ix) */ 929431996fSJuerg Haefliger #define DME1737_REG_TEMP(ix) (0x25 + (ix)) 939431996fSJuerg Haefliger #define DME1737_REG_TEMP_MIN(ix) (0x4e + (ix) * 2) 949431996fSJuerg Haefliger #define DME1737_REG_TEMP_MAX(ix) (0x4f + (ix) * 2) 959431996fSJuerg Haefliger #define DME1737_REG_TEMP_OFFSET(ix) ((ix) == 0 ? 0x1f \ 969431996fSJuerg Haefliger : 0x1c + (ix)) 979431996fSJuerg Haefliger 989431996fSJuerg Haefliger /* Voltage and temperature LSBs 999431996fSJuerg Haefliger * The LSBs (4 bits each) are stored in 5 registers with the following layouts: 1009431996fSJuerg Haefliger * IN_TEMP_LSB(0) = [in5, in6] 1019431996fSJuerg Haefliger * IN_TEMP_LSB(1) = [temp3, temp1] 1029431996fSJuerg Haefliger * IN_TEMP_LSB(2) = [in4, temp2] 1039431996fSJuerg Haefliger * IN_TEMP_LSB(3) = [in3, in0] 1049431996fSJuerg Haefliger * IN_TEMP_LSB(4) = [in2, in1] */ 1059431996fSJuerg Haefliger #define DME1737_REG_IN_TEMP_LSB(ix) (0x84 + (ix)) 1069431996fSJuerg Haefliger static const u8 DME1737_REG_IN_LSB[] = {3, 4, 4, 3, 2, 0, 0}; 1079431996fSJuerg Haefliger static const u8 DME1737_REG_IN_LSB_SHL[] = {4, 4, 0, 0, 0, 0, 4}; 1089431996fSJuerg Haefliger static const u8 DME1737_REG_TEMP_LSB[] = {1, 2, 1}; 1099431996fSJuerg Haefliger static const u8 DME1737_REG_TEMP_LSB_SHL[] = {4, 4, 0}; 1109431996fSJuerg Haefliger 1119431996fSJuerg Haefliger /* Fans numbered 0-5 (ix) */ 1129431996fSJuerg Haefliger #define DME1737_REG_FAN(ix) ((ix) < 4 ? 0x28 + (ix) * 2 \ 1139431996fSJuerg Haefliger : 0xa1 + (ix) * 2) 1149431996fSJuerg Haefliger #define DME1737_REG_FAN_MIN(ix) ((ix) < 4 ? 0x54 + (ix) * 2 \ 1159431996fSJuerg Haefliger : 0xa5 + (ix) * 2) 1169431996fSJuerg Haefliger #define DME1737_REG_FAN_OPT(ix) ((ix) < 4 ? 0x90 + (ix) \ 1179431996fSJuerg Haefliger : 0xb2 + (ix)) 1189431996fSJuerg Haefliger #define DME1737_REG_FAN_MAX(ix) (0xb4 + (ix)) /* only for fan[4-5] */ 1199431996fSJuerg Haefliger 1209431996fSJuerg Haefliger /* PWMs numbered 0-2, 4-5 (ix) */ 1219431996fSJuerg Haefliger #define DME1737_REG_PWM(ix) ((ix) < 3 ? 0x30 + (ix) \ 1229431996fSJuerg Haefliger : 0xa1 + (ix)) 1239431996fSJuerg Haefliger #define DME1737_REG_PWM_CONFIG(ix) (0x5c + (ix)) /* only for pwm[0-2] */ 1249431996fSJuerg Haefliger #define DME1737_REG_PWM_MIN(ix) (0x64 + (ix)) /* only for pwm[0-2] */ 1259431996fSJuerg Haefliger #define DME1737_REG_PWM_FREQ(ix) ((ix) < 3 ? 0x5f + (ix) \ 1269431996fSJuerg Haefliger : 0xa3 + (ix)) 1279431996fSJuerg Haefliger /* The layout of the ramp rate registers is different from the other pwm 1289431996fSJuerg Haefliger * registers. The bits for the 3 PWMs are stored in 2 registers: 1299431996fSJuerg Haefliger * PWM_RR(0) = [OFF3, OFF2, OFF1, RES, RR1E, RR1-2, RR1-1, RR1-0] 1309431996fSJuerg Haefliger * PWM_RR(1) = [RR2E, RR2-2, RR2-1, RR2-0, RR3E, RR3-2, RR3-1, RR3-0] */ 1319431996fSJuerg Haefliger #define DME1737_REG_PWM_RR(ix) (0x62 + (ix)) /* only for pwm[0-2] */ 1329431996fSJuerg Haefliger 1339431996fSJuerg Haefliger /* Thermal zones 0-2 */ 1349431996fSJuerg Haefliger #define DME1737_REG_ZONE_LOW(ix) (0x67 + (ix)) 1359431996fSJuerg Haefliger #define DME1737_REG_ZONE_ABS(ix) (0x6a + (ix)) 1369431996fSJuerg Haefliger /* The layout of the hysteresis registers is different from the other zone 1379431996fSJuerg Haefliger * registers. The bits for the 3 zones are stored in 2 registers: 1389431996fSJuerg Haefliger * ZONE_HYST(0) = [H1-3, H1-2, H1-1, H1-0, H2-3, H2-2, H2-1, H2-0] 1399431996fSJuerg Haefliger * ZONE_HYST(1) = [H3-3, H3-2, H3-1, H3-0, RES, RES, RES, RES] */ 1409431996fSJuerg Haefliger #define DME1737_REG_ZONE_HYST(ix) (0x6d + (ix)) 1419431996fSJuerg Haefliger 1429431996fSJuerg Haefliger /* Alarm registers and bit mapping 1439431996fSJuerg Haefliger * The 3 8-bit alarm registers will be concatenated to a single 32-bit 1449431996fSJuerg Haefliger * alarm value [0, ALARM3, ALARM2, ALARM1]. */ 1459431996fSJuerg Haefliger #define DME1737_REG_ALARM1 0x41 1469431996fSJuerg Haefliger #define DME1737_REG_ALARM2 0x42 1479431996fSJuerg Haefliger #define DME1737_REG_ALARM3 0x83 1489431996fSJuerg Haefliger static const u8 DME1737_BIT_ALARM_IN[] = {0, 1, 2, 3, 8, 16, 17}; 1499431996fSJuerg Haefliger static const u8 DME1737_BIT_ALARM_TEMP[] = {4, 5, 6}; 1509431996fSJuerg Haefliger static const u8 DME1737_BIT_ALARM_FAN[] = {10, 11, 12, 13, 22, 23}; 1519431996fSJuerg Haefliger 1529431996fSJuerg Haefliger /* Miscellaneous registers */ 153e95c237dSJuerg Haefliger #define DME1737_REG_DEVICE 0x3d 1549431996fSJuerg Haefliger #define DME1737_REG_COMPANY 0x3e 1559431996fSJuerg Haefliger #define DME1737_REG_VERSTEP 0x3f 1569431996fSJuerg Haefliger #define DME1737_REG_CONFIG 0x40 1579431996fSJuerg Haefliger #define DME1737_REG_CONFIG2 0x7f 1589431996fSJuerg Haefliger #define DME1737_REG_VID 0x43 1599431996fSJuerg Haefliger #define DME1737_REG_TACH_PWM 0x81 1609431996fSJuerg Haefliger 1619431996fSJuerg Haefliger /* --------------------------------------------------------------------- 1629431996fSJuerg Haefliger * Misc defines 1639431996fSJuerg Haefliger * --------------------------------------------------------------------- */ 1649431996fSJuerg Haefliger 1659431996fSJuerg Haefliger /* Chip identification */ 1669431996fSJuerg Haefliger #define DME1737_COMPANY_SMSC 0x5c 1679431996fSJuerg Haefliger #define DME1737_VERSTEP 0x88 1689431996fSJuerg Haefliger #define DME1737_VERSTEP_MASK 0xf8 169e95c237dSJuerg Haefliger #define SCH311X_DEVICE 0x8c 170549edb83SJuerg Haefliger #define SCH5027_VERSTEP 0x69 171e95c237dSJuerg Haefliger 172e95c237dSJuerg Haefliger /* Length of ISA address segment */ 173e95c237dSJuerg Haefliger #define DME1737_EXTENT 2 1749431996fSJuerg Haefliger 1759431996fSJuerg Haefliger /* --------------------------------------------------------------------- 1769431996fSJuerg Haefliger * Data structures and manipulation thereof 1779431996fSJuerg Haefliger * --------------------------------------------------------------------- */ 1789431996fSJuerg Haefliger 1799431996fSJuerg Haefliger struct dme1737_data { 180dbc2bc25SJean Delvare struct i2c_client *client; /* for I2C devices only */ 1811beeffe4STony Jones struct device *hwmon_dev; 182dbc2bc25SJean Delvare const char *name; 183dbc2bc25SJean Delvare unsigned int addr; /* for ISA devices only */ 1849431996fSJuerg Haefliger 1859431996fSJuerg Haefliger struct mutex update_lock; 1869431996fSJuerg Haefliger int valid; /* !=0 if following fields are valid */ 1879431996fSJuerg Haefliger unsigned long last_update; /* in jiffies */ 1889431996fSJuerg Haefliger unsigned long last_vbat; /* in jiffies */ 189f994fb23SJuerg Haefliger enum chips type; 190549edb83SJuerg Haefliger const int *in_nominal; /* pointer to IN_NOMINAL array */ 1919431996fSJuerg Haefliger 1929431996fSJuerg Haefliger u8 vid; 1939431996fSJuerg Haefliger u8 pwm_rr_en; 1949431996fSJuerg Haefliger u8 has_pwm; 1959431996fSJuerg Haefliger u8 has_fan; 1969431996fSJuerg Haefliger 1979431996fSJuerg Haefliger /* Register values */ 1989431996fSJuerg Haefliger u16 in[7]; 1999431996fSJuerg Haefliger u8 in_min[7]; 2009431996fSJuerg Haefliger u8 in_max[7]; 2019431996fSJuerg Haefliger s16 temp[3]; 2029431996fSJuerg Haefliger s8 temp_min[3]; 2039431996fSJuerg Haefliger s8 temp_max[3]; 2049431996fSJuerg Haefliger s8 temp_offset[3]; 2059431996fSJuerg Haefliger u8 config; 2069431996fSJuerg Haefliger u8 config2; 2079431996fSJuerg Haefliger u8 vrm; 2089431996fSJuerg Haefliger u16 fan[6]; 2099431996fSJuerg Haefliger u16 fan_min[6]; 2109431996fSJuerg Haefliger u8 fan_max[2]; 2119431996fSJuerg Haefliger u8 fan_opt[6]; 2129431996fSJuerg Haefliger u8 pwm[6]; 2139431996fSJuerg Haefliger u8 pwm_min[3]; 2149431996fSJuerg Haefliger u8 pwm_config[3]; 2159431996fSJuerg Haefliger u8 pwm_acz[3]; 2169431996fSJuerg Haefliger u8 pwm_freq[6]; 2179431996fSJuerg Haefliger u8 pwm_rr[2]; 2189431996fSJuerg Haefliger u8 zone_low[3]; 2199431996fSJuerg Haefliger u8 zone_abs[3]; 2209431996fSJuerg Haefliger u8 zone_hyst[2]; 2219431996fSJuerg Haefliger u32 alarms; 2229431996fSJuerg Haefliger }; 2239431996fSJuerg Haefliger 2249431996fSJuerg Haefliger /* Nominal voltage values */ 225f994fb23SJuerg Haefliger static const int IN_NOMINAL_DME1737[] = {5000, 2250, 3300, 5000, 12000, 3300, 226f994fb23SJuerg Haefliger 3300}; 227f994fb23SJuerg Haefliger static const int IN_NOMINAL_SCH311x[] = {2500, 1500, 3300, 5000, 12000, 3300, 228f994fb23SJuerg Haefliger 3300}; 229549edb83SJuerg Haefliger static const int IN_NOMINAL_SCH5027[] = {5000, 2250, 3300, 1125, 1125, 3300, 230549edb83SJuerg Haefliger 3300}; 231549edb83SJuerg Haefliger #define IN_NOMINAL(type) ((type) == sch311x ? IN_NOMINAL_SCH311x : \ 232549edb83SJuerg Haefliger (type) == sch5027 ? IN_NOMINAL_SCH5027 : \ 233549edb83SJuerg Haefliger IN_NOMINAL_DME1737) 2349431996fSJuerg Haefliger 2359431996fSJuerg Haefliger /* Voltage input 2369431996fSJuerg Haefliger * Voltage inputs have 16 bits resolution, limit values have 8 bits 2379431996fSJuerg Haefliger * resolution. */ 238549edb83SJuerg Haefliger static inline int IN_FROM_REG(int reg, int nominal, int res) 2399431996fSJuerg Haefliger { 240549edb83SJuerg Haefliger return (reg * nominal + (3 << (res - 3))) / (3 << (res - 2)); 2419431996fSJuerg Haefliger } 2429431996fSJuerg Haefliger 243549edb83SJuerg Haefliger static inline int IN_TO_REG(int val, int nominal) 2449431996fSJuerg Haefliger { 245549edb83SJuerg Haefliger return SENSORS_LIMIT((val * 192 + nominal / 2) / nominal, 0, 255); 2469431996fSJuerg Haefliger } 2479431996fSJuerg Haefliger 2489431996fSJuerg Haefliger /* Temperature input 2499431996fSJuerg Haefliger * The register values represent temperatures in 2's complement notation from 2509431996fSJuerg Haefliger * -127 degrees C to +127 degrees C. Temp inputs have 16 bits resolution, limit 2519431996fSJuerg Haefliger * values have 8 bits resolution. */ 2529431996fSJuerg Haefliger static inline int TEMP_FROM_REG(int reg, int res) 2539431996fSJuerg Haefliger { 2549431996fSJuerg Haefliger return (reg * 1000) >> (res - 8); 2559431996fSJuerg Haefliger } 2569431996fSJuerg Haefliger 2579431996fSJuerg Haefliger static inline int TEMP_TO_REG(int val) 2589431996fSJuerg Haefliger { 2599431996fSJuerg Haefliger return SENSORS_LIMIT((val < 0 ? val - 500 : val + 500) / 1000, 2609431996fSJuerg Haefliger -128, 127); 2619431996fSJuerg Haefliger } 2629431996fSJuerg Haefliger 2639431996fSJuerg Haefliger /* Temperature range */ 2649431996fSJuerg Haefliger static const int TEMP_RANGE[] = {2000, 2500, 3333, 4000, 5000, 6666, 8000, 2659431996fSJuerg Haefliger 10000, 13333, 16000, 20000, 26666, 32000, 2669431996fSJuerg Haefliger 40000, 53333, 80000}; 2679431996fSJuerg Haefliger 2689431996fSJuerg Haefliger static inline int TEMP_RANGE_FROM_REG(int reg) 2699431996fSJuerg Haefliger { 2709431996fSJuerg Haefliger return TEMP_RANGE[(reg >> 4) & 0x0f]; 2719431996fSJuerg Haefliger } 2729431996fSJuerg Haefliger 2739431996fSJuerg Haefliger static int TEMP_RANGE_TO_REG(int val, int reg) 2749431996fSJuerg Haefliger { 2759431996fSJuerg Haefliger int i; 2769431996fSJuerg Haefliger 2779431996fSJuerg Haefliger for (i = 15; i > 0; i--) { 2789431996fSJuerg Haefliger if (val > (TEMP_RANGE[i] + TEMP_RANGE[i - 1] + 1) / 2) { 2799431996fSJuerg Haefliger break; 2809431996fSJuerg Haefliger } 2819431996fSJuerg Haefliger } 2829431996fSJuerg Haefliger 2839431996fSJuerg Haefliger return (reg & 0x0f) | (i << 4); 2849431996fSJuerg Haefliger } 2859431996fSJuerg Haefliger 2869431996fSJuerg Haefliger /* Temperature hysteresis 2879431996fSJuerg Haefliger * Register layout: 2889431996fSJuerg Haefliger * reg[0] = [H1-3, H1-2, H1-1, H1-0, H2-3, H2-2, H2-1, H2-0] 2899431996fSJuerg Haefliger * reg[1] = [H3-3, H3-2, H3-1, H3-0, xxxx, xxxx, xxxx, xxxx] */ 2909431996fSJuerg Haefliger static inline int TEMP_HYST_FROM_REG(int reg, int ix) 2919431996fSJuerg Haefliger { 2929431996fSJuerg Haefliger return (((ix == 1) ? reg : reg >> 4) & 0x0f) * 1000; 2939431996fSJuerg Haefliger } 2949431996fSJuerg Haefliger 2959431996fSJuerg Haefliger static inline int TEMP_HYST_TO_REG(int val, int ix, int reg) 2969431996fSJuerg Haefliger { 2979431996fSJuerg Haefliger int hyst = SENSORS_LIMIT((val + 500) / 1000, 0, 15); 2989431996fSJuerg Haefliger 2999431996fSJuerg Haefliger return (ix == 1) ? (reg & 0xf0) | hyst : (reg & 0x0f) | (hyst << 4); 3009431996fSJuerg Haefliger } 3019431996fSJuerg Haefliger 3029431996fSJuerg Haefliger /* Fan input RPM */ 3039431996fSJuerg Haefliger static inline int FAN_FROM_REG(int reg, int tpc) 3049431996fSJuerg Haefliger { 305ff8421f7SJuerg Haefliger if (tpc) { 306ff8421f7SJuerg Haefliger return tpc * reg; 307ff8421f7SJuerg Haefliger } else { 308ff8421f7SJuerg Haefliger return (reg == 0 || reg == 0xffff) ? 0 : 90000 * 60 / reg; 309ff8421f7SJuerg Haefliger } 3109431996fSJuerg Haefliger } 3119431996fSJuerg Haefliger 3129431996fSJuerg Haefliger static inline int FAN_TO_REG(int val, int tpc) 3139431996fSJuerg Haefliger { 314ff8421f7SJuerg Haefliger if (tpc) { 315ff8421f7SJuerg Haefliger return SENSORS_LIMIT(val / tpc, 0, 0xffff); 316ff8421f7SJuerg Haefliger } else { 317ff8421f7SJuerg Haefliger return (val <= 0) ? 0xffff : 318ff8421f7SJuerg Haefliger SENSORS_LIMIT(90000 * 60 / val, 0, 0xfffe); 319ff8421f7SJuerg Haefliger } 3209431996fSJuerg Haefliger } 3219431996fSJuerg Haefliger 3229431996fSJuerg Haefliger /* Fan TPC (tach pulse count) 3239431996fSJuerg Haefliger * Converts a register value to a TPC multiplier or returns 0 if the tachometer 3249431996fSJuerg Haefliger * is configured in legacy (non-tpc) mode */ 3259431996fSJuerg Haefliger static inline int FAN_TPC_FROM_REG(int reg) 3269431996fSJuerg Haefliger { 3279431996fSJuerg Haefliger return (reg & 0x20) ? 0 : 60 >> (reg & 0x03); 3289431996fSJuerg Haefliger } 3299431996fSJuerg Haefliger 3309431996fSJuerg Haefliger /* Fan type 3319431996fSJuerg Haefliger * The type of a fan is expressed in number of pulses-per-revolution that it 3329431996fSJuerg Haefliger * emits */ 3339431996fSJuerg Haefliger static inline int FAN_TYPE_FROM_REG(int reg) 3349431996fSJuerg Haefliger { 3359431996fSJuerg Haefliger int edge = (reg >> 1) & 0x03; 3369431996fSJuerg Haefliger 3379431996fSJuerg Haefliger return (edge > 0) ? 1 << (edge - 1) : 0; 3389431996fSJuerg Haefliger } 3399431996fSJuerg Haefliger 3409431996fSJuerg Haefliger static inline int FAN_TYPE_TO_REG(int val, int reg) 3419431996fSJuerg Haefliger { 3429431996fSJuerg Haefliger int edge = (val == 4) ? 3 : val; 3439431996fSJuerg Haefliger 3449431996fSJuerg Haefliger return (reg & 0xf9) | (edge << 1); 3459431996fSJuerg Haefliger } 3469431996fSJuerg Haefliger 3479431996fSJuerg Haefliger /* Fan max RPM */ 3489431996fSJuerg Haefliger static const int FAN_MAX[] = {0x54, 0x38, 0x2a, 0x21, 0x1c, 0x18, 0x15, 0x12, 3499431996fSJuerg Haefliger 0x11, 0x0f, 0x0e}; 3509431996fSJuerg Haefliger 3519431996fSJuerg Haefliger static int FAN_MAX_FROM_REG(int reg) 3529431996fSJuerg Haefliger { 3539431996fSJuerg Haefliger int i; 3549431996fSJuerg Haefliger 3559431996fSJuerg Haefliger for (i = 10; i > 0; i--) { 3569431996fSJuerg Haefliger if (reg == FAN_MAX[i]) { 3579431996fSJuerg Haefliger break; 3589431996fSJuerg Haefliger } 3599431996fSJuerg Haefliger } 3609431996fSJuerg Haefliger 3619431996fSJuerg Haefliger return 1000 + i * 500; 3629431996fSJuerg Haefliger } 3639431996fSJuerg Haefliger 3649431996fSJuerg Haefliger static int FAN_MAX_TO_REG(int val) 3659431996fSJuerg Haefliger { 3669431996fSJuerg Haefliger int i; 3679431996fSJuerg Haefliger 3689431996fSJuerg Haefliger for (i = 10; i > 0; i--) { 3699431996fSJuerg Haefliger if (val > (1000 + (i - 1) * 500)) { 3709431996fSJuerg Haefliger break; 3719431996fSJuerg Haefliger } 3729431996fSJuerg Haefliger } 3739431996fSJuerg Haefliger 3749431996fSJuerg Haefliger return FAN_MAX[i]; 3759431996fSJuerg Haefliger } 3769431996fSJuerg Haefliger 3779431996fSJuerg Haefliger /* PWM enable 3789431996fSJuerg Haefliger * Register to enable mapping: 3799431996fSJuerg Haefliger * 000: 2 fan on zone 1 auto 3809431996fSJuerg Haefliger * 001: 2 fan on zone 2 auto 3819431996fSJuerg Haefliger * 010: 2 fan on zone 3 auto 3829431996fSJuerg Haefliger * 011: 0 fan full on 3839431996fSJuerg Haefliger * 100: -1 fan disabled 3849431996fSJuerg Haefliger * 101: 2 fan on hottest of zones 2,3 auto 3859431996fSJuerg Haefliger * 110: 2 fan on hottest of zones 1,2,3 auto 3869431996fSJuerg Haefliger * 111: 1 fan in manual mode */ 3879431996fSJuerg Haefliger static inline int PWM_EN_FROM_REG(int reg) 3889431996fSJuerg Haefliger { 3899431996fSJuerg Haefliger static const int en[] = {2, 2, 2, 0, -1, 2, 2, 1}; 3909431996fSJuerg Haefliger 3919431996fSJuerg Haefliger return en[(reg >> 5) & 0x07]; 3929431996fSJuerg Haefliger } 3939431996fSJuerg Haefliger 3949431996fSJuerg Haefliger static inline int PWM_EN_TO_REG(int val, int reg) 3959431996fSJuerg Haefliger { 3969431996fSJuerg Haefliger int en = (val == 1) ? 7 : 3; 3979431996fSJuerg Haefliger 3989431996fSJuerg Haefliger return (reg & 0x1f) | ((en & 0x07) << 5); 3999431996fSJuerg Haefliger } 4009431996fSJuerg Haefliger 4019431996fSJuerg Haefliger /* PWM auto channels zone 4029431996fSJuerg Haefliger * Register to auto channels zone mapping (ACZ is a bitfield with bit x 4039431996fSJuerg Haefliger * corresponding to zone x+1): 4049431996fSJuerg Haefliger * 000: 001 fan on zone 1 auto 4059431996fSJuerg Haefliger * 001: 010 fan on zone 2 auto 4069431996fSJuerg Haefliger * 010: 100 fan on zone 3 auto 4079431996fSJuerg Haefliger * 011: 000 fan full on 4089431996fSJuerg Haefliger * 100: 000 fan disabled 4099431996fSJuerg Haefliger * 101: 110 fan on hottest of zones 2,3 auto 4109431996fSJuerg Haefliger * 110: 111 fan on hottest of zones 1,2,3 auto 4119431996fSJuerg Haefliger * 111: 000 fan in manual mode */ 4129431996fSJuerg Haefliger static inline int PWM_ACZ_FROM_REG(int reg) 4139431996fSJuerg Haefliger { 4149431996fSJuerg Haefliger static const int acz[] = {1, 2, 4, 0, 0, 6, 7, 0}; 4159431996fSJuerg Haefliger 4169431996fSJuerg Haefliger return acz[(reg >> 5) & 0x07]; 4179431996fSJuerg Haefliger } 4189431996fSJuerg Haefliger 4199431996fSJuerg Haefliger static inline int PWM_ACZ_TO_REG(int val, int reg) 4209431996fSJuerg Haefliger { 4219431996fSJuerg Haefliger int acz = (val == 4) ? 2 : val - 1; 4229431996fSJuerg Haefliger 4239431996fSJuerg Haefliger return (reg & 0x1f) | ((acz & 0x07) << 5); 4249431996fSJuerg Haefliger } 4259431996fSJuerg Haefliger 4269431996fSJuerg Haefliger /* PWM frequency */ 4279431996fSJuerg Haefliger static const int PWM_FREQ[] = {11, 15, 22, 29, 35, 44, 59, 88, 4289431996fSJuerg Haefliger 15000, 20000, 30000, 25000, 0, 0, 0, 0}; 4299431996fSJuerg Haefliger 4309431996fSJuerg Haefliger static inline int PWM_FREQ_FROM_REG(int reg) 4319431996fSJuerg Haefliger { 4329431996fSJuerg Haefliger return PWM_FREQ[reg & 0x0f]; 4339431996fSJuerg Haefliger } 4349431996fSJuerg Haefliger 4359431996fSJuerg Haefliger static int PWM_FREQ_TO_REG(int val, int reg) 4369431996fSJuerg Haefliger { 4379431996fSJuerg Haefliger int i; 4389431996fSJuerg Haefliger 4399431996fSJuerg Haefliger /* the first two cases are special - stupid chip design! */ 4409431996fSJuerg Haefliger if (val > 27500) { 4419431996fSJuerg Haefliger i = 10; 4429431996fSJuerg Haefliger } else if (val > 22500) { 4439431996fSJuerg Haefliger i = 11; 4449431996fSJuerg Haefliger } else { 4459431996fSJuerg Haefliger for (i = 9; i > 0; i--) { 4469431996fSJuerg Haefliger if (val > (PWM_FREQ[i] + PWM_FREQ[i - 1] + 1) / 2) { 4479431996fSJuerg Haefliger break; 4489431996fSJuerg Haefliger } 4499431996fSJuerg Haefliger } 4509431996fSJuerg Haefliger } 4519431996fSJuerg Haefliger 4529431996fSJuerg Haefliger return (reg & 0xf0) | i; 4539431996fSJuerg Haefliger } 4549431996fSJuerg Haefliger 4559431996fSJuerg Haefliger /* PWM ramp rate 4569431996fSJuerg Haefliger * Register layout: 4579431996fSJuerg Haefliger * reg[0] = [OFF3, OFF2, OFF1, RES, RR1-E, RR1-2, RR1-1, RR1-0] 4589431996fSJuerg Haefliger * reg[1] = [RR2-E, RR2-2, RR2-1, RR2-0, RR3-E, RR3-2, RR3-1, RR3-0] */ 4599431996fSJuerg Haefliger static const u8 PWM_RR[] = {206, 104, 69, 41, 26, 18, 10, 5}; 4609431996fSJuerg Haefliger 4619431996fSJuerg Haefliger static inline int PWM_RR_FROM_REG(int reg, int ix) 4629431996fSJuerg Haefliger { 4639431996fSJuerg Haefliger int rr = (ix == 1) ? reg >> 4 : reg; 4649431996fSJuerg Haefliger 4659431996fSJuerg Haefliger return (rr & 0x08) ? PWM_RR[rr & 0x07] : 0; 4669431996fSJuerg Haefliger } 4679431996fSJuerg Haefliger 4689431996fSJuerg Haefliger static int PWM_RR_TO_REG(int val, int ix, int reg) 4699431996fSJuerg Haefliger { 4709431996fSJuerg Haefliger int i; 4719431996fSJuerg Haefliger 4729431996fSJuerg Haefliger for (i = 0; i < 7; i++) { 4739431996fSJuerg Haefliger if (val > (PWM_RR[i] + PWM_RR[i + 1] + 1) / 2) { 4749431996fSJuerg Haefliger break; 4759431996fSJuerg Haefliger } 4769431996fSJuerg Haefliger } 4779431996fSJuerg Haefliger 4789431996fSJuerg Haefliger return (ix == 1) ? (reg & 0x8f) | (i << 4) : (reg & 0xf8) | i; 4799431996fSJuerg Haefliger } 4809431996fSJuerg Haefliger 4819431996fSJuerg Haefliger /* PWM ramp rate enable */ 4829431996fSJuerg Haefliger static inline int PWM_RR_EN_FROM_REG(int reg, int ix) 4839431996fSJuerg Haefliger { 4849431996fSJuerg Haefliger return PWM_RR_FROM_REG(reg, ix) ? 1 : 0; 4859431996fSJuerg Haefliger } 4869431996fSJuerg Haefliger 4879431996fSJuerg Haefliger static inline int PWM_RR_EN_TO_REG(int val, int ix, int reg) 4889431996fSJuerg Haefliger { 4899431996fSJuerg Haefliger int en = (ix == 1) ? 0x80 : 0x08; 4909431996fSJuerg Haefliger 4919431996fSJuerg Haefliger return val ? reg | en : reg & ~en; 4929431996fSJuerg Haefliger } 4939431996fSJuerg Haefliger 4949431996fSJuerg Haefliger /* PWM min/off 4959431996fSJuerg Haefliger * The PWM min/off bits are part of the PMW ramp rate register 0 (see above for 4969431996fSJuerg Haefliger * the register layout). */ 4979431996fSJuerg Haefliger static inline int PWM_OFF_FROM_REG(int reg, int ix) 4989431996fSJuerg Haefliger { 4999431996fSJuerg Haefliger return (reg >> (ix + 5)) & 0x01; 5009431996fSJuerg Haefliger } 5019431996fSJuerg Haefliger 5029431996fSJuerg Haefliger static inline int PWM_OFF_TO_REG(int val, int ix, int reg) 5039431996fSJuerg Haefliger { 5049431996fSJuerg Haefliger return (reg & ~(1 << (ix + 5))) | ((val & 0x01) << (ix + 5)); 5059431996fSJuerg Haefliger } 5069431996fSJuerg Haefliger 5079431996fSJuerg Haefliger /* --------------------------------------------------------------------- 5089431996fSJuerg Haefliger * Device I/O access 509e95c237dSJuerg Haefliger * 510e95c237dSJuerg Haefliger * ISA access is performed through an index/data register pair and needs to 511e95c237dSJuerg Haefliger * be protected by a mutex during runtime (not required for initialization). 512e95c237dSJuerg Haefliger * We use data->update_lock for this and need to ensure that we acquire it 513e95c237dSJuerg Haefliger * before calling dme1737_read or dme1737_write. 5149431996fSJuerg Haefliger * --------------------------------------------------------------------- */ 5159431996fSJuerg Haefliger 516dbc2bc25SJean Delvare static u8 dme1737_read(const struct dme1737_data *data, u8 reg) 5179431996fSJuerg Haefliger { 518dbc2bc25SJean Delvare struct i2c_client *client = data->client; 519e95c237dSJuerg Haefliger s32 val; 520e95c237dSJuerg Haefliger 521dbc2bc25SJean Delvare if (client) { /* I2C device */ 522e95c237dSJuerg Haefliger val = i2c_smbus_read_byte_data(client, reg); 5239431996fSJuerg Haefliger 5249431996fSJuerg Haefliger if (val < 0) { 525e95c237dSJuerg Haefliger dev_warn(&client->dev, "Read from register " 526e95c237dSJuerg Haefliger "0x%02x failed! Please report to the driver " 527e95c237dSJuerg Haefliger "maintainer.\n", reg); 528e95c237dSJuerg Haefliger } 529e95c237dSJuerg Haefliger } else { /* ISA device */ 530dbc2bc25SJean Delvare outb(reg, data->addr); 531dbc2bc25SJean Delvare val = inb(data->addr + 1); 5329431996fSJuerg Haefliger } 5339431996fSJuerg Haefliger 5349431996fSJuerg Haefliger return val; 5359431996fSJuerg Haefliger } 5369431996fSJuerg Haefliger 537dbc2bc25SJean Delvare static s32 dme1737_write(const struct dme1737_data *data, u8 reg, u8 val) 5389431996fSJuerg Haefliger { 539dbc2bc25SJean Delvare struct i2c_client *client = data->client; 540e95c237dSJuerg Haefliger s32 res = 0; 541e95c237dSJuerg Haefliger 542dbc2bc25SJean Delvare if (client) { /* I2C device */ 543e95c237dSJuerg Haefliger res = i2c_smbus_write_byte_data(client, reg, val); 5449431996fSJuerg Haefliger 5459431996fSJuerg Haefliger if (res < 0) { 546e95c237dSJuerg Haefliger dev_warn(&client->dev, "Write to register " 547e95c237dSJuerg Haefliger "0x%02x failed! Please report to the driver " 548e95c237dSJuerg Haefliger "maintainer.\n", reg); 549e95c237dSJuerg Haefliger } 550e95c237dSJuerg Haefliger } else { /* ISA device */ 551dbc2bc25SJean Delvare outb(reg, data->addr); 552dbc2bc25SJean Delvare outb(val, data->addr + 1); 5539431996fSJuerg Haefliger } 5549431996fSJuerg Haefliger 5559431996fSJuerg Haefliger return res; 5569431996fSJuerg Haefliger } 5579431996fSJuerg Haefliger 5589431996fSJuerg Haefliger static struct dme1737_data *dme1737_update_device(struct device *dev) 5599431996fSJuerg Haefliger { 560b237eb25SJuerg Haefliger struct dme1737_data *data = dev_get_drvdata(dev); 5619431996fSJuerg Haefliger int ix; 5629431996fSJuerg Haefliger u8 lsb[5]; 5639431996fSJuerg Haefliger 5649431996fSJuerg Haefliger mutex_lock(&data->update_lock); 5659431996fSJuerg Haefliger 5669431996fSJuerg Haefliger /* Enable a Vbat monitoring cycle every 10 mins */ 5679431996fSJuerg Haefliger if (time_after(jiffies, data->last_vbat + 600 * HZ) || !data->valid) { 568dbc2bc25SJean Delvare dme1737_write(data, DME1737_REG_CONFIG, dme1737_read(data, 5699431996fSJuerg Haefliger DME1737_REG_CONFIG) | 0x10); 5709431996fSJuerg Haefliger data->last_vbat = jiffies; 5719431996fSJuerg Haefliger } 5729431996fSJuerg Haefliger 5739431996fSJuerg Haefliger /* Sample register contents every 1 sec */ 5749431996fSJuerg Haefliger if (time_after(jiffies, data->last_update + HZ) || !data->valid) { 5759d091446SJean Delvare if (data->type == dme1737) { 576dbc2bc25SJean Delvare data->vid = dme1737_read(data, DME1737_REG_VID) & 577549edb83SJuerg Haefliger 0x3f; 578549edb83SJuerg Haefliger } 5799431996fSJuerg Haefliger 5809431996fSJuerg Haefliger /* In (voltage) registers */ 5819431996fSJuerg Haefliger for (ix = 0; ix < ARRAY_SIZE(data->in); ix++) { 5829431996fSJuerg Haefliger /* Voltage inputs are stored as 16 bit values even 5839431996fSJuerg Haefliger * though they have only 12 bits resolution. This is 5849431996fSJuerg Haefliger * to make it consistent with the temp inputs. */ 585dbc2bc25SJean Delvare data->in[ix] = dme1737_read(data, 5869431996fSJuerg Haefliger DME1737_REG_IN(ix)) << 8; 587dbc2bc25SJean Delvare data->in_min[ix] = dme1737_read(data, 5889431996fSJuerg Haefliger DME1737_REG_IN_MIN(ix)); 589dbc2bc25SJean Delvare data->in_max[ix] = dme1737_read(data, 5909431996fSJuerg Haefliger DME1737_REG_IN_MAX(ix)); 5919431996fSJuerg Haefliger } 5929431996fSJuerg Haefliger 5939431996fSJuerg Haefliger /* Temp registers */ 5949431996fSJuerg Haefliger for (ix = 0; ix < ARRAY_SIZE(data->temp); ix++) { 5959431996fSJuerg Haefliger /* Temp inputs are stored as 16 bit values even 5969431996fSJuerg Haefliger * though they have only 12 bits resolution. This is 5979431996fSJuerg Haefliger * to take advantage of implicit conversions between 5989431996fSJuerg Haefliger * register values (2's complement) and temp values 5999431996fSJuerg Haefliger * (signed decimal). */ 600dbc2bc25SJean Delvare data->temp[ix] = dme1737_read(data, 6019431996fSJuerg Haefliger DME1737_REG_TEMP(ix)) << 8; 602dbc2bc25SJean Delvare data->temp_min[ix] = dme1737_read(data, 6039431996fSJuerg Haefliger DME1737_REG_TEMP_MIN(ix)); 604dbc2bc25SJean Delvare data->temp_max[ix] = dme1737_read(data, 6059431996fSJuerg Haefliger DME1737_REG_TEMP_MAX(ix)); 606549edb83SJuerg Haefliger if (data->type != sch5027) { 607dbc2bc25SJean Delvare data->temp_offset[ix] = dme1737_read(data, 6089431996fSJuerg Haefliger DME1737_REG_TEMP_OFFSET(ix)); 6099431996fSJuerg Haefliger } 610549edb83SJuerg Haefliger } 6119431996fSJuerg Haefliger 6129431996fSJuerg Haefliger /* In and temp LSB registers 6139431996fSJuerg Haefliger * The LSBs are latched when the MSBs are read, so the order in 6149431996fSJuerg Haefliger * which the registers are read (MSB first, then LSB) is 6159431996fSJuerg Haefliger * important! */ 6169431996fSJuerg Haefliger for (ix = 0; ix < ARRAY_SIZE(lsb); ix++) { 617dbc2bc25SJean Delvare lsb[ix] = dme1737_read(data, 6189431996fSJuerg Haefliger DME1737_REG_IN_TEMP_LSB(ix)); 6199431996fSJuerg Haefliger } 6209431996fSJuerg Haefliger for (ix = 0; ix < ARRAY_SIZE(data->in); ix++) { 6219431996fSJuerg Haefliger data->in[ix] |= (lsb[DME1737_REG_IN_LSB[ix]] << 6229431996fSJuerg Haefliger DME1737_REG_IN_LSB_SHL[ix]) & 0xf0; 6239431996fSJuerg Haefliger } 6249431996fSJuerg Haefliger for (ix = 0; ix < ARRAY_SIZE(data->temp); ix++) { 6259431996fSJuerg Haefliger data->temp[ix] |= (lsb[DME1737_REG_TEMP_LSB[ix]] << 6269431996fSJuerg Haefliger DME1737_REG_TEMP_LSB_SHL[ix]) & 0xf0; 6279431996fSJuerg Haefliger } 6289431996fSJuerg Haefliger 6299431996fSJuerg Haefliger /* Fan registers */ 6309431996fSJuerg Haefliger for (ix = 0; ix < ARRAY_SIZE(data->fan); ix++) { 6319431996fSJuerg Haefliger /* Skip reading registers if optional fans are not 6329431996fSJuerg Haefliger * present */ 6339431996fSJuerg Haefliger if (!(data->has_fan & (1 << ix))) { 6349431996fSJuerg Haefliger continue; 6359431996fSJuerg Haefliger } 636dbc2bc25SJean Delvare data->fan[ix] = dme1737_read(data, 6379431996fSJuerg Haefliger DME1737_REG_FAN(ix)); 638dbc2bc25SJean Delvare data->fan[ix] |= dme1737_read(data, 6399431996fSJuerg Haefliger DME1737_REG_FAN(ix) + 1) << 8; 640dbc2bc25SJean Delvare data->fan_min[ix] = dme1737_read(data, 6419431996fSJuerg Haefliger DME1737_REG_FAN_MIN(ix)); 642dbc2bc25SJean Delvare data->fan_min[ix] |= dme1737_read(data, 6439431996fSJuerg Haefliger DME1737_REG_FAN_MIN(ix) + 1) << 8; 644dbc2bc25SJean Delvare data->fan_opt[ix] = dme1737_read(data, 6459431996fSJuerg Haefliger DME1737_REG_FAN_OPT(ix)); 6469431996fSJuerg Haefliger /* fan_max exists only for fan[5-6] */ 6479431996fSJuerg Haefliger if (ix > 3) { 648dbc2bc25SJean Delvare data->fan_max[ix - 4] = dme1737_read(data, 6499431996fSJuerg Haefliger DME1737_REG_FAN_MAX(ix)); 6509431996fSJuerg Haefliger } 6519431996fSJuerg Haefliger } 6529431996fSJuerg Haefliger 6539431996fSJuerg Haefliger /* PWM registers */ 6549431996fSJuerg Haefliger for (ix = 0; ix < ARRAY_SIZE(data->pwm); ix++) { 6559431996fSJuerg Haefliger /* Skip reading registers if optional PWMs are not 6569431996fSJuerg Haefliger * present */ 6579431996fSJuerg Haefliger if (!(data->has_pwm & (1 << ix))) { 6589431996fSJuerg Haefliger continue; 6599431996fSJuerg Haefliger } 660dbc2bc25SJean Delvare data->pwm[ix] = dme1737_read(data, 6619431996fSJuerg Haefliger DME1737_REG_PWM(ix)); 662dbc2bc25SJean Delvare data->pwm_freq[ix] = dme1737_read(data, 6639431996fSJuerg Haefliger DME1737_REG_PWM_FREQ(ix)); 6649431996fSJuerg Haefliger /* pwm_config and pwm_min exist only for pwm[1-3] */ 6659431996fSJuerg Haefliger if (ix < 3) { 666dbc2bc25SJean Delvare data->pwm_config[ix] = dme1737_read(data, 6679431996fSJuerg Haefliger DME1737_REG_PWM_CONFIG(ix)); 668dbc2bc25SJean Delvare data->pwm_min[ix] = dme1737_read(data, 6699431996fSJuerg Haefliger DME1737_REG_PWM_MIN(ix)); 6709431996fSJuerg Haefliger } 6719431996fSJuerg Haefliger } 6729431996fSJuerg Haefliger for (ix = 0; ix < ARRAY_SIZE(data->pwm_rr); ix++) { 673dbc2bc25SJean Delvare data->pwm_rr[ix] = dme1737_read(data, 6749431996fSJuerg Haefliger DME1737_REG_PWM_RR(ix)); 6759431996fSJuerg Haefliger } 6769431996fSJuerg Haefliger 6779431996fSJuerg Haefliger /* Thermal zone registers */ 6789431996fSJuerg Haefliger for (ix = 0; ix < ARRAY_SIZE(data->zone_low); ix++) { 679dbc2bc25SJean Delvare data->zone_low[ix] = dme1737_read(data, 6809431996fSJuerg Haefliger DME1737_REG_ZONE_LOW(ix)); 681dbc2bc25SJean Delvare data->zone_abs[ix] = dme1737_read(data, 6829431996fSJuerg Haefliger DME1737_REG_ZONE_ABS(ix)); 6839431996fSJuerg Haefliger } 684549edb83SJuerg Haefliger if (data->type != sch5027) { 6859431996fSJuerg Haefliger for (ix = 0; ix < ARRAY_SIZE(data->zone_hyst); ix++) { 686dbc2bc25SJean Delvare data->zone_hyst[ix] = dme1737_read(data, 6879431996fSJuerg Haefliger DME1737_REG_ZONE_HYST(ix)); 6889431996fSJuerg Haefliger } 689549edb83SJuerg Haefliger } 6909431996fSJuerg Haefliger 6919431996fSJuerg Haefliger /* Alarm registers */ 692dbc2bc25SJean Delvare data->alarms = dme1737_read(data, 6939431996fSJuerg Haefliger DME1737_REG_ALARM1); 6949431996fSJuerg Haefliger /* Bit 7 tells us if the other alarm registers are non-zero and 6959431996fSJuerg Haefliger * therefore also need to be read */ 6969431996fSJuerg Haefliger if (data->alarms & 0x80) { 697dbc2bc25SJean Delvare data->alarms |= dme1737_read(data, 6989431996fSJuerg Haefliger DME1737_REG_ALARM2) << 8; 699dbc2bc25SJean Delvare data->alarms |= dme1737_read(data, 7009431996fSJuerg Haefliger DME1737_REG_ALARM3) << 16; 7019431996fSJuerg Haefliger } 7029431996fSJuerg Haefliger 703e95c237dSJuerg Haefliger /* The ISA chips require explicit clearing of alarm bits. 704e95c237dSJuerg Haefliger * Don't worry, an alarm will come back if the condition 705e95c237dSJuerg Haefliger * that causes it still exists */ 706dbc2bc25SJean Delvare if (!data->client) { 707e95c237dSJuerg Haefliger if (data->alarms & 0xff0000) { 708dbc2bc25SJean Delvare dme1737_write(data, DME1737_REG_ALARM3, 709e95c237dSJuerg Haefliger 0xff); 710e95c237dSJuerg Haefliger } 711e95c237dSJuerg Haefliger if (data->alarms & 0xff00) { 712dbc2bc25SJean Delvare dme1737_write(data, DME1737_REG_ALARM2, 713e95c237dSJuerg Haefliger 0xff); 714e95c237dSJuerg Haefliger } 715e95c237dSJuerg Haefliger if (data->alarms & 0xff) { 716dbc2bc25SJean Delvare dme1737_write(data, DME1737_REG_ALARM1, 717e95c237dSJuerg Haefliger 0xff); 718e95c237dSJuerg Haefliger } 719e95c237dSJuerg Haefliger } 720e95c237dSJuerg Haefliger 7219431996fSJuerg Haefliger data->last_update = jiffies; 7229431996fSJuerg Haefliger data->valid = 1; 7239431996fSJuerg Haefliger } 7249431996fSJuerg Haefliger 7259431996fSJuerg Haefliger mutex_unlock(&data->update_lock); 7269431996fSJuerg Haefliger 7279431996fSJuerg Haefliger return data; 7289431996fSJuerg Haefliger } 7299431996fSJuerg Haefliger 7309431996fSJuerg Haefliger /* --------------------------------------------------------------------- 7319431996fSJuerg Haefliger * Voltage sysfs attributes 7329431996fSJuerg Haefliger * ix = [0-5] 7339431996fSJuerg Haefliger * --------------------------------------------------------------------- */ 7349431996fSJuerg Haefliger 7359431996fSJuerg Haefliger #define SYS_IN_INPUT 0 7369431996fSJuerg Haefliger #define SYS_IN_MIN 1 7379431996fSJuerg Haefliger #define SYS_IN_MAX 2 7389431996fSJuerg Haefliger #define SYS_IN_ALARM 3 7399431996fSJuerg Haefliger 7409431996fSJuerg Haefliger static ssize_t show_in(struct device *dev, struct device_attribute *attr, 7419431996fSJuerg Haefliger char *buf) 7429431996fSJuerg Haefliger { 7439431996fSJuerg Haefliger struct dme1737_data *data = dme1737_update_device(dev); 7449431996fSJuerg Haefliger struct sensor_device_attribute_2 7459431996fSJuerg Haefliger *sensor_attr_2 = to_sensor_dev_attr_2(attr); 7469431996fSJuerg Haefliger int ix = sensor_attr_2->index; 7479431996fSJuerg Haefliger int fn = sensor_attr_2->nr; 7489431996fSJuerg Haefliger int res; 7499431996fSJuerg Haefliger 7509431996fSJuerg Haefliger switch (fn) { 7519431996fSJuerg Haefliger case SYS_IN_INPUT: 752549edb83SJuerg Haefliger res = IN_FROM_REG(data->in[ix], data->in_nominal[ix], 16); 7539431996fSJuerg Haefliger break; 7549431996fSJuerg Haefliger case SYS_IN_MIN: 755549edb83SJuerg Haefliger res = IN_FROM_REG(data->in_min[ix], data->in_nominal[ix], 8); 7569431996fSJuerg Haefliger break; 7579431996fSJuerg Haefliger case SYS_IN_MAX: 758549edb83SJuerg Haefliger res = IN_FROM_REG(data->in_max[ix], data->in_nominal[ix], 8); 7599431996fSJuerg Haefliger break; 7609431996fSJuerg Haefliger case SYS_IN_ALARM: 7619431996fSJuerg Haefliger res = (data->alarms >> DME1737_BIT_ALARM_IN[ix]) & 0x01; 7629431996fSJuerg Haefliger break; 7639431996fSJuerg Haefliger default: 7649431996fSJuerg Haefliger res = 0; 765b237eb25SJuerg Haefliger dev_dbg(dev, "Unknown function %d.\n", fn); 7669431996fSJuerg Haefliger } 7679431996fSJuerg Haefliger 7689431996fSJuerg Haefliger return sprintf(buf, "%d\n", res); 7699431996fSJuerg Haefliger } 7709431996fSJuerg Haefliger 7719431996fSJuerg Haefliger static ssize_t set_in(struct device *dev, struct device_attribute *attr, 7729431996fSJuerg Haefliger const char *buf, size_t count) 7739431996fSJuerg Haefliger { 774b237eb25SJuerg Haefliger struct dme1737_data *data = dev_get_drvdata(dev); 7759431996fSJuerg Haefliger struct sensor_device_attribute_2 7769431996fSJuerg Haefliger *sensor_attr_2 = to_sensor_dev_attr_2(attr); 7779431996fSJuerg Haefliger int ix = sensor_attr_2->index; 7789431996fSJuerg Haefliger int fn = sensor_attr_2->nr; 7799431996fSJuerg Haefliger long val = simple_strtol(buf, NULL, 10); 7809431996fSJuerg Haefliger 7819431996fSJuerg Haefliger mutex_lock(&data->update_lock); 7829431996fSJuerg Haefliger switch (fn) { 7839431996fSJuerg Haefliger case SYS_IN_MIN: 784549edb83SJuerg Haefliger data->in_min[ix] = IN_TO_REG(val, data->in_nominal[ix]); 785dbc2bc25SJean Delvare dme1737_write(data, DME1737_REG_IN_MIN(ix), 7869431996fSJuerg Haefliger data->in_min[ix]); 7879431996fSJuerg Haefliger break; 7889431996fSJuerg Haefliger case SYS_IN_MAX: 789549edb83SJuerg Haefliger data->in_max[ix] = IN_TO_REG(val, data->in_nominal[ix]); 790dbc2bc25SJean Delvare dme1737_write(data, DME1737_REG_IN_MAX(ix), 7919431996fSJuerg Haefliger data->in_max[ix]); 7929431996fSJuerg Haefliger break; 7939431996fSJuerg Haefliger default: 794b237eb25SJuerg Haefliger dev_dbg(dev, "Unknown function %d.\n", fn); 7959431996fSJuerg Haefliger } 7969431996fSJuerg Haefliger mutex_unlock(&data->update_lock); 7979431996fSJuerg Haefliger 7989431996fSJuerg Haefliger return count; 7999431996fSJuerg Haefliger } 8009431996fSJuerg Haefliger 8019431996fSJuerg Haefliger /* --------------------------------------------------------------------- 8029431996fSJuerg Haefliger * Temperature sysfs attributes 8039431996fSJuerg Haefliger * ix = [0-2] 8049431996fSJuerg Haefliger * --------------------------------------------------------------------- */ 8059431996fSJuerg Haefliger 8069431996fSJuerg Haefliger #define SYS_TEMP_INPUT 0 8079431996fSJuerg Haefliger #define SYS_TEMP_MIN 1 8089431996fSJuerg Haefliger #define SYS_TEMP_MAX 2 8099431996fSJuerg Haefliger #define SYS_TEMP_OFFSET 3 8109431996fSJuerg Haefliger #define SYS_TEMP_ALARM 4 8119431996fSJuerg Haefliger #define SYS_TEMP_FAULT 5 8129431996fSJuerg Haefliger 8139431996fSJuerg Haefliger static ssize_t show_temp(struct device *dev, struct device_attribute *attr, 8149431996fSJuerg Haefliger char *buf) 8159431996fSJuerg Haefliger { 8169431996fSJuerg Haefliger struct dme1737_data *data = dme1737_update_device(dev); 8179431996fSJuerg Haefliger struct sensor_device_attribute_2 8189431996fSJuerg Haefliger *sensor_attr_2 = to_sensor_dev_attr_2(attr); 8199431996fSJuerg Haefliger int ix = sensor_attr_2->index; 8209431996fSJuerg Haefliger int fn = sensor_attr_2->nr; 8219431996fSJuerg Haefliger int res; 8229431996fSJuerg Haefliger 8239431996fSJuerg Haefliger switch (fn) { 8249431996fSJuerg Haefliger case SYS_TEMP_INPUT: 8259431996fSJuerg Haefliger res = TEMP_FROM_REG(data->temp[ix], 16); 8269431996fSJuerg Haefliger break; 8279431996fSJuerg Haefliger case SYS_TEMP_MIN: 8289431996fSJuerg Haefliger res = TEMP_FROM_REG(data->temp_min[ix], 8); 8299431996fSJuerg Haefliger break; 8309431996fSJuerg Haefliger case SYS_TEMP_MAX: 8319431996fSJuerg Haefliger res = TEMP_FROM_REG(data->temp_max[ix], 8); 8329431996fSJuerg Haefliger break; 8339431996fSJuerg Haefliger case SYS_TEMP_OFFSET: 8349431996fSJuerg Haefliger res = TEMP_FROM_REG(data->temp_offset[ix], 8); 8359431996fSJuerg Haefliger break; 8369431996fSJuerg Haefliger case SYS_TEMP_ALARM: 8379431996fSJuerg Haefliger res = (data->alarms >> DME1737_BIT_ALARM_TEMP[ix]) & 0x01; 8389431996fSJuerg Haefliger break; 8399431996fSJuerg Haefliger case SYS_TEMP_FAULT: 840c0f31403SJuerg Haefliger res = (((u16)data->temp[ix] & 0xff00) == 0x8000); 8419431996fSJuerg Haefliger break; 8429431996fSJuerg Haefliger default: 8439431996fSJuerg Haefliger res = 0; 844b237eb25SJuerg Haefliger dev_dbg(dev, "Unknown function %d.\n", fn); 8459431996fSJuerg Haefliger } 8469431996fSJuerg Haefliger 8479431996fSJuerg Haefliger return sprintf(buf, "%d\n", res); 8489431996fSJuerg Haefliger } 8499431996fSJuerg Haefliger 8509431996fSJuerg Haefliger static ssize_t set_temp(struct device *dev, struct device_attribute *attr, 8519431996fSJuerg Haefliger const char *buf, size_t count) 8529431996fSJuerg Haefliger { 853b237eb25SJuerg Haefliger struct dme1737_data *data = dev_get_drvdata(dev); 8549431996fSJuerg Haefliger struct sensor_device_attribute_2 8559431996fSJuerg Haefliger *sensor_attr_2 = to_sensor_dev_attr_2(attr); 8569431996fSJuerg Haefliger int ix = sensor_attr_2->index; 8579431996fSJuerg Haefliger int fn = sensor_attr_2->nr; 8589431996fSJuerg Haefliger long val = simple_strtol(buf, NULL, 10); 8599431996fSJuerg Haefliger 8609431996fSJuerg Haefliger mutex_lock(&data->update_lock); 8619431996fSJuerg Haefliger switch (fn) { 8629431996fSJuerg Haefliger case SYS_TEMP_MIN: 8639431996fSJuerg Haefliger data->temp_min[ix] = TEMP_TO_REG(val); 864dbc2bc25SJean Delvare dme1737_write(data, DME1737_REG_TEMP_MIN(ix), 8659431996fSJuerg Haefliger data->temp_min[ix]); 8669431996fSJuerg Haefliger break; 8679431996fSJuerg Haefliger case SYS_TEMP_MAX: 8689431996fSJuerg Haefliger data->temp_max[ix] = TEMP_TO_REG(val); 869dbc2bc25SJean Delvare dme1737_write(data, DME1737_REG_TEMP_MAX(ix), 8709431996fSJuerg Haefliger data->temp_max[ix]); 8719431996fSJuerg Haefliger break; 8729431996fSJuerg Haefliger case SYS_TEMP_OFFSET: 8739431996fSJuerg Haefliger data->temp_offset[ix] = TEMP_TO_REG(val); 874dbc2bc25SJean Delvare dme1737_write(data, DME1737_REG_TEMP_OFFSET(ix), 8759431996fSJuerg Haefliger data->temp_offset[ix]); 8769431996fSJuerg Haefliger break; 8779431996fSJuerg Haefliger default: 878b237eb25SJuerg Haefliger dev_dbg(dev, "Unknown function %d.\n", fn); 8799431996fSJuerg Haefliger } 8809431996fSJuerg Haefliger mutex_unlock(&data->update_lock); 8819431996fSJuerg Haefliger 8829431996fSJuerg Haefliger return count; 8839431996fSJuerg Haefliger } 8849431996fSJuerg Haefliger 8859431996fSJuerg Haefliger /* --------------------------------------------------------------------- 8869431996fSJuerg Haefliger * Zone sysfs attributes 8879431996fSJuerg Haefliger * ix = [0-2] 8889431996fSJuerg Haefliger * --------------------------------------------------------------------- */ 8899431996fSJuerg Haefliger 8909431996fSJuerg Haefliger #define SYS_ZONE_AUTO_CHANNELS_TEMP 0 8919431996fSJuerg Haefliger #define SYS_ZONE_AUTO_POINT1_TEMP_HYST 1 8929431996fSJuerg Haefliger #define SYS_ZONE_AUTO_POINT1_TEMP 2 8939431996fSJuerg Haefliger #define SYS_ZONE_AUTO_POINT2_TEMP 3 8949431996fSJuerg Haefliger #define SYS_ZONE_AUTO_POINT3_TEMP 4 8959431996fSJuerg Haefliger 8969431996fSJuerg Haefliger static ssize_t show_zone(struct device *dev, struct device_attribute *attr, 8979431996fSJuerg Haefliger char *buf) 8989431996fSJuerg Haefliger { 8999431996fSJuerg Haefliger struct dme1737_data *data = dme1737_update_device(dev); 9009431996fSJuerg Haefliger struct sensor_device_attribute_2 9019431996fSJuerg Haefliger *sensor_attr_2 = to_sensor_dev_attr_2(attr); 9029431996fSJuerg Haefliger int ix = sensor_attr_2->index; 9039431996fSJuerg Haefliger int fn = sensor_attr_2->nr; 9049431996fSJuerg Haefliger int res; 9059431996fSJuerg Haefliger 9069431996fSJuerg Haefliger switch (fn) { 9079431996fSJuerg Haefliger case SYS_ZONE_AUTO_CHANNELS_TEMP: 9089431996fSJuerg Haefliger /* check config2 for non-standard temp-to-zone mapping */ 9099431996fSJuerg Haefliger if ((ix == 1) && (data->config2 & 0x02)) { 9109431996fSJuerg Haefliger res = 4; 9119431996fSJuerg Haefliger } else { 9129431996fSJuerg Haefliger res = 1 << ix; 9139431996fSJuerg Haefliger } 9149431996fSJuerg Haefliger break; 9159431996fSJuerg Haefliger case SYS_ZONE_AUTO_POINT1_TEMP_HYST: 9169431996fSJuerg Haefliger res = TEMP_FROM_REG(data->zone_low[ix], 8) - 9179431996fSJuerg Haefliger TEMP_HYST_FROM_REG(data->zone_hyst[ix == 2], ix); 9189431996fSJuerg Haefliger break; 9199431996fSJuerg Haefliger case SYS_ZONE_AUTO_POINT1_TEMP: 9209431996fSJuerg Haefliger res = TEMP_FROM_REG(data->zone_low[ix], 8); 9219431996fSJuerg Haefliger break; 9229431996fSJuerg Haefliger case SYS_ZONE_AUTO_POINT2_TEMP: 9239431996fSJuerg Haefliger /* pwm_freq holds the temp range bits in the upper nibble */ 9249431996fSJuerg Haefliger res = TEMP_FROM_REG(data->zone_low[ix], 8) + 9259431996fSJuerg Haefliger TEMP_RANGE_FROM_REG(data->pwm_freq[ix]); 9269431996fSJuerg Haefliger break; 9279431996fSJuerg Haefliger case SYS_ZONE_AUTO_POINT3_TEMP: 9289431996fSJuerg Haefliger res = TEMP_FROM_REG(data->zone_abs[ix], 8); 9299431996fSJuerg Haefliger break; 9309431996fSJuerg Haefliger default: 9319431996fSJuerg Haefliger res = 0; 932b237eb25SJuerg Haefliger dev_dbg(dev, "Unknown function %d.\n", fn); 9339431996fSJuerg Haefliger } 9349431996fSJuerg Haefliger 9359431996fSJuerg Haefliger return sprintf(buf, "%d\n", res); 9369431996fSJuerg Haefliger } 9379431996fSJuerg Haefliger 9389431996fSJuerg Haefliger static ssize_t set_zone(struct device *dev, struct device_attribute *attr, 9399431996fSJuerg Haefliger const char *buf, size_t count) 9409431996fSJuerg Haefliger { 941b237eb25SJuerg Haefliger struct dme1737_data *data = dev_get_drvdata(dev); 9429431996fSJuerg Haefliger struct sensor_device_attribute_2 9439431996fSJuerg Haefliger *sensor_attr_2 = to_sensor_dev_attr_2(attr); 9449431996fSJuerg Haefliger int ix = sensor_attr_2->index; 9459431996fSJuerg Haefliger int fn = sensor_attr_2->nr; 9469431996fSJuerg Haefliger long val = simple_strtol(buf, NULL, 10); 9479431996fSJuerg Haefliger 9489431996fSJuerg Haefliger mutex_lock(&data->update_lock); 9499431996fSJuerg Haefliger switch (fn) { 9509431996fSJuerg Haefliger case SYS_ZONE_AUTO_POINT1_TEMP_HYST: 9519431996fSJuerg Haefliger /* Refresh the cache */ 952dbc2bc25SJean Delvare data->zone_low[ix] = dme1737_read(data, 9539431996fSJuerg Haefliger DME1737_REG_ZONE_LOW(ix)); 9549431996fSJuerg Haefliger /* Modify the temp hyst value */ 9559431996fSJuerg Haefliger data->zone_hyst[ix == 2] = TEMP_HYST_TO_REG( 9569431996fSJuerg Haefliger TEMP_FROM_REG(data->zone_low[ix], 8) - 957dbc2bc25SJean Delvare val, ix, dme1737_read(data, 9589431996fSJuerg Haefliger DME1737_REG_ZONE_HYST(ix == 2))); 959dbc2bc25SJean Delvare dme1737_write(data, DME1737_REG_ZONE_HYST(ix == 2), 9609431996fSJuerg Haefliger data->zone_hyst[ix == 2]); 9619431996fSJuerg Haefliger break; 9629431996fSJuerg Haefliger case SYS_ZONE_AUTO_POINT1_TEMP: 9639431996fSJuerg Haefliger data->zone_low[ix] = TEMP_TO_REG(val); 964dbc2bc25SJean Delvare dme1737_write(data, DME1737_REG_ZONE_LOW(ix), 9659431996fSJuerg Haefliger data->zone_low[ix]); 9669431996fSJuerg Haefliger break; 9679431996fSJuerg Haefliger case SYS_ZONE_AUTO_POINT2_TEMP: 9689431996fSJuerg Haefliger /* Refresh the cache */ 969dbc2bc25SJean Delvare data->zone_low[ix] = dme1737_read(data, 9709431996fSJuerg Haefliger DME1737_REG_ZONE_LOW(ix)); 9719431996fSJuerg Haefliger /* Modify the temp range value (which is stored in the upper 9729431996fSJuerg Haefliger * nibble of the pwm_freq register) */ 9739431996fSJuerg Haefliger data->pwm_freq[ix] = TEMP_RANGE_TO_REG(val - 9749431996fSJuerg Haefliger TEMP_FROM_REG(data->zone_low[ix], 8), 975dbc2bc25SJean Delvare dme1737_read(data, 9769431996fSJuerg Haefliger DME1737_REG_PWM_FREQ(ix))); 977dbc2bc25SJean Delvare dme1737_write(data, DME1737_REG_PWM_FREQ(ix), 9789431996fSJuerg Haefliger data->pwm_freq[ix]); 9799431996fSJuerg Haefliger break; 9809431996fSJuerg Haefliger case SYS_ZONE_AUTO_POINT3_TEMP: 9819431996fSJuerg Haefliger data->zone_abs[ix] = TEMP_TO_REG(val); 982dbc2bc25SJean Delvare dme1737_write(data, DME1737_REG_ZONE_ABS(ix), 9839431996fSJuerg Haefliger data->zone_abs[ix]); 9849431996fSJuerg Haefliger break; 9859431996fSJuerg Haefliger default: 986b237eb25SJuerg Haefliger dev_dbg(dev, "Unknown function %d.\n", fn); 9879431996fSJuerg Haefliger } 9889431996fSJuerg Haefliger mutex_unlock(&data->update_lock); 9899431996fSJuerg Haefliger 9909431996fSJuerg Haefliger return count; 9919431996fSJuerg Haefliger } 9929431996fSJuerg Haefliger 9939431996fSJuerg Haefliger /* --------------------------------------------------------------------- 9949431996fSJuerg Haefliger * Fan sysfs attributes 9959431996fSJuerg Haefliger * ix = [0-5] 9969431996fSJuerg Haefliger * --------------------------------------------------------------------- */ 9979431996fSJuerg Haefliger 9989431996fSJuerg Haefliger #define SYS_FAN_INPUT 0 9999431996fSJuerg Haefliger #define SYS_FAN_MIN 1 10009431996fSJuerg Haefliger #define SYS_FAN_MAX 2 10019431996fSJuerg Haefliger #define SYS_FAN_ALARM 3 10029431996fSJuerg Haefliger #define SYS_FAN_TYPE 4 10039431996fSJuerg Haefliger 10049431996fSJuerg Haefliger static ssize_t show_fan(struct device *dev, struct device_attribute *attr, 10059431996fSJuerg Haefliger char *buf) 10069431996fSJuerg Haefliger { 10079431996fSJuerg Haefliger struct dme1737_data *data = dme1737_update_device(dev); 10089431996fSJuerg Haefliger struct sensor_device_attribute_2 10099431996fSJuerg Haefliger *sensor_attr_2 = to_sensor_dev_attr_2(attr); 10109431996fSJuerg Haefliger int ix = sensor_attr_2->index; 10119431996fSJuerg Haefliger int fn = sensor_attr_2->nr; 10129431996fSJuerg Haefliger int res; 10139431996fSJuerg Haefliger 10149431996fSJuerg Haefliger switch (fn) { 10159431996fSJuerg Haefliger case SYS_FAN_INPUT: 10169431996fSJuerg Haefliger res = FAN_FROM_REG(data->fan[ix], 10179431996fSJuerg Haefliger ix < 4 ? 0 : 10189431996fSJuerg Haefliger FAN_TPC_FROM_REG(data->fan_opt[ix])); 10199431996fSJuerg Haefliger break; 10209431996fSJuerg Haefliger case SYS_FAN_MIN: 10219431996fSJuerg Haefliger res = FAN_FROM_REG(data->fan_min[ix], 10229431996fSJuerg Haefliger ix < 4 ? 0 : 10239431996fSJuerg Haefliger FAN_TPC_FROM_REG(data->fan_opt[ix])); 10249431996fSJuerg Haefliger break; 10259431996fSJuerg Haefliger case SYS_FAN_MAX: 10269431996fSJuerg Haefliger /* only valid for fan[5-6] */ 10279431996fSJuerg Haefliger res = FAN_MAX_FROM_REG(data->fan_max[ix - 4]); 10289431996fSJuerg Haefliger break; 10299431996fSJuerg Haefliger case SYS_FAN_ALARM: 10309431996fSJuerg Haefliger res = (data->alarms >> DME1737_BIT_ALARM_FAN[ix]) & 0x01; 10319431996fSJuerg Haefliger break; 10329431996fSJuerg Haefliger case SYS_FAN_TYPE: 10339431996fSJuerg Haefliger /* only valid for fan[1-4] */ 10349431996fSJuerg Haefliger res = FAN_TYPE_FROM_REG(data->fan_opt[ix]); 10359431996fSJuerg Haefliger break; 10369431996fSJuerg Haefliger default: 10379431996fSJuerg Haefliger res = 0; 1038b237eb25SJuerg Haefliger dev_dbg(dev, "Unknown function %d.\n", fn); 10399431996fSJuerg Haefliger } 10409431996fSJuerg Haefliger 10419431996fSJuerg Haefliger return sprintf(buf, "%d\n", res); 10429431996fSJuerg Haefliger } 10439431996fSJuerg Haefliger 10449431996fSJuerg Haefliger static ssize_t set_fan(struct device *dev, struct device_attribute *attr, 10459431996fSJuerg Haefliger const char *buf, size_t count) 10469431996fSJuerg Haefliger { 1047b237eb25SJuerg Haefliger struct dme1737_data *data = dev_get_drvdata(dev); 10489431996fSJuerg Haefliger struct sensor_device_attribute_2 10499431996fSJuerg Haefliger *sensor_attr_2 = to_sensor_dev_attr_2(attr); 10509431996fSJuerg Haefliger int ix = sensor_attr_2->index; 10519431996fSJuerg Haefliger int fn = sensor_attr_2->nr; 10529431996fSJuerg Haefliger long val = simple_strtol(buf, NULL, 10); 10539431996fSJuerg Haefliger 10549431996fSJuerg Haefliger mutex_lock(&data->update_lock); 10559431996fSJuerg Haefliger switch (fn) { 10569431996fSJuerg Haefliger case SYS_FAN_MIN: 10579431996fSJuerg Haefliger if (ix < 4) { 10589431996fSJuerg Haefliger data->fan_min[ix] = FAN_TO_REG(val, 0); 10599431996fSJuerg Haefliger } else { 10609431996fSJuerg Haefliger /* Refresh the cache */ 1061dbc2bc25SJean Delvare data->fan_opt[ix] = dme1737_read(data, 10629431996fSJuerg Haefliger DME1737_REG_FAN_OPT(ix)); 10639431996fSJuerg Haefliger /* Modify the fan min value */ 10649431996fSJuerg Haefliger data->fan_min[ix] = FAN_TO_REG(val, 10659431996fSJuerg Haefliger FAN_TPC_FROM_REG(data->fan_opt[ix])); 10669431996fSJuerg Haefliger } 1067dbc2bc25SJean Delvare dme1737_write(data, DME1737_REG_FAN_MIN(ix), 10689431996fSJuerg Haefliger data->fan_min[ix] & 0xff); 1069dbc2bc25SJean Delvare dme1737_write(data, DME1737_REG_FAN_MIN(ix) + 1, 10709431996fSJuerg Haefliger data->fan_min[ix] >> 8); 10719431996fSJuerg Haefliger break; 10729431996fSJuerg Haefliger case SYS_FAN_MAX: 10739431996fSJuerg Haefliger /* Only valid for fan[5-6] */ 10749431996fSJuerg Haefliger data->fan_max[ix - 4] = FAN_MAX_TO_REG(val); 1075dbc2bc25SJean Delvare dme1737_write(data, DME1737_REG_FAN_MAX(ix), 10769431996fSJuerg Haefliger data->fan_max[ix - 4]); 10779431996fSJuerg Haefliger break; 10789431996fSJuerg Haefliger case SYS_FAN_TYPE: 10799431996fSJuerg Haefliger /* Only valid for fan[1-4] */ 10809431996fSJuerg Haefliger if (!(val == 1 || val == 2 || val == 4)) { 10819431996fSJuerg Haefliger count = -EINVAL; 1082e95c237dSJuerg Haefliger dev_warn(dev, "Fan type value %ld not " 10839431996fSJuerg Haefliger "supported. Choose one of 1, 2, or 4.\n", 10849431996fSJuerg Haefliger val); 10859431996fSJuerg Haefliger goto exit; 10869431996fSJuerg Haefliger } 1087dbc2bc25SJean Delvare data->fan_opt[ix] = FAN_TYPE_TO_REG(val, dme1737_read(data, 10889431996fSJuerg Haefliger DME1737_REG_FAN_OPT(ix))); 1089dbc2bc25SJean Delvare dme1737_write(data, DME1737_REG_FAN_OPT(ix), 10909431996fSJuerg Haefliger data->fan_opt[ix]); 10919431996fSJuerg Haefliger break; 10929431996fSJuerg Haefliger default: 1093b237eb25SJuerg Haefliger dev_dbg(dev, "Unknown function %d.\n", fn); 10949431996fSJuerg Haefliger } 10959431996fSJuerg Haefliger exit: 10969431996fSJuerg Haefliger mutex_unlock(&data->update_lock); 10979431996fSJuerg Haefliger 10989431996fSJuerg Haefliger return count; 10999431996fSJuerg Haefliger } 11009431996fSJuerg Haefliger 11019431996fSJuerg Haefliger /* --------------------------------------------------------------------- 11029431996fSJuerg Haefliger * PWM sysfs attributes 11039431996fSJuerg Haefliger * ix = [0-4] 11049431996fSJuerg Haefliger * --------------------------------------------------------------------- */ 11059431996fSJuerg Haefliger 11069431996fSJuerg Haefliger #define SYS_PWM 0 11079431996fSJuerg Haefliger #define SYS_PWM_FREQ 1 11089431996fSJuerg Haefliger #define SYS_PWM_ENABLE 2 11099431996fSJuerg Haefliger #define SYS_PWM_RAMP_RATE 3 11109431996fSJuerg Haefliger #define SYS_PWM_AUTO_CHANNELS_ZONE 4 11119431996fSJuerg Haefliger #define SYS_PWM_AUTO_PWM_MIN 5 11129431996fSJuerg Haefliger #define SYS_PWM_AUTO_POINT1_PWM 6 11139431996fSJuerg Haefliger #define SYS_PWM_AUTO_POINT2_PWM 7 11149431996fSJuerg Haefliger 11159431996fSJuerg Haefliger static ssize_t show_pwm(struct device *dev, struct device_attribute *attr, 11169431996fSJuerg Haefliger char *buf) 11179431996fSJuerg Haefliger { 11189431996fSJuerg Haefliger struct dme1737_data *data = dme1737_update_device(dev); 11199431996fSJuerg Haefliger struct sensor_device_attribute_2 11209431996fSJuerg Haefliger *sensor_attr_2 = to_sensor_dev_attr_2(attr); 11219431996fSJuerg Haefliger int ix = sensor_attr_2->index; 11229431996fSJuerg Haefliger int fn = sensor_attr_2->nr; 11239431996fSJuerg Haefliger int res; 11249431996fSJuerg Haefliger 11259431996fSJuerg Haefliger switch (fn) { 11269431996fSJuerg Haefliger case SYS_PWM: 11279431996fSJuerg Haefliger if (PWM_EN_FROM_REG(data->pwm_config[ix]) == 0) { 11289431996fSJuerg Haefliger res = 255; 11299431996fSJuerg Haefliger } else { 11309431996fSJuerg Haefliger res = data->pwm[ix]; 11319431996fSJuerg Haefliger } 11329431996fSJuerg Haefliger break; 11339431996fSJuerg Haefliger case SYS_PWM_FREQ: 11349431996fSJuerg Haefliger res = PWM_FREQ_FROM_REG(data->pwm_freq[ix]); 11359431996fSJuerg Haefliger break; 11369431996fSJuerg Haefliger case SYS_PWM_ENABLE: 1137912e837aSRoel Kluin if (ix >= 3) { 11389431996fSJuerg Haefliger res = 1; /* pwm[5-6] hard-wired to manual mode */ 11399431996fSJuerg Haefliger } else { 11409431996fSJuerg Haefliger res = PWM_EN_FROM_REG(data->pwm_config[ix]); 11419431996fSJuerg Haefliger } 11429431996fSJuerg Haefliger break; 11439431996fSJuerg Haefliger case SYS_PWM_RAMP_RATE: 11449431996fSJuerg Haefliger /* Only valid for pwm[1-3] */ 11459431996fSJuerg Haefliger res = PWM_RR_FROM_REG(data->pwm_rr[ix > 0], ix); 11469431996fSJuerg Haefliger break; 11479431996fSJuerg Haefliger case SYS_PWM_AUTO_CHANNELS_ZONE: 11489431996fSJuerg Haefliger /* Only valid for pwm[1-3] */ 11499431996fSJuerg Haefliger if (PWM_EN_FROM_REG(data->pwm_config[ix]) == 2) { 11509431996fSJuerg Haefliger res = PWM_ACZ_FROM_REG(data->pwm_config[ix]); 11519431996fSJuerg Haefliger } else { 11529431996fSJuerg Haefliger res = data->pwm_acz[ix]; 11539431996fSJuerg Haefliger } 11549431996fSJuerg Haefliger break; 11559431996fSJuerg Haefliger case SYS_PWM_AUTO_PWM_MIN: 11569431996fSJuerg Haefliger /* Only valid for pwm[1-3] */ 11579431996fSJuerg Haefliger if (PWM_OFF_FROM_REG(data->pwm_rr[0], ix)) { 11589431996fSJuerg Haefliger res = data->pwm_min[ix]; 11599431996fSJuerg Haefliger } else { 11609431996fSJuerg Haefliger res = 0; 11619431996fSJuerg Haefliger } 11629431996fSJuerg Haefliger break; 11639431996fSJuerg Haefliger case SYS_PWM_AUTO_POINT1_PWM: 11649431996fSJuerg Haefliger /* Only valid for pwm[1-3] */ 11659431996fSJuerg Haefliger res = data->pwm_min[ix]; 11669431996fSJuerg Haefliger break; 11679431996fSJuerg Haefliger case SYS_PWM_AUTO_POINT2_PWM: 11689431996fSJuerg Haefliger /* Only valid for pwm[1-3] */ 11699431996fSJuerg Haefliger res = 255; /* hard-wired */ 11709431996fSJuerg Haefliger break; 11719431996fSJuerg Haefliger default: 11729431996fSJuerg Haefliger res = 0; 1173b237eb25SJuerg Haefliger dev_dbg(dev, "Unknown function %d.\n", fn); 11749431996fSJuerg Haefliger } 11759431996fSJuerg Haefliger 11769431996fSJuerg Haefliger return sprintf(buf, "%d\n", res); 11779431996fSJuerg Haefliger } 11789431996fSJuerg Haefliger 117973ce48f6SJuerg Haefliger static struct attribute *dme1737_pwm_chmod_attr[]; 1180b237eb25SJuerg Haefliger static void dme1737_chmod_file(struct device*, struct attribute*, mode_t); 11819431996fSJuerg Haefliger 11829431996fSJuerg Haefliger static ssize_t set_pwm(struct device *dev, struct device_attribute *attr, 11839431996fSJuerg Haefliger const char *buf, size_t count) 11849431996fSJuerg Haefliger { 1185b237eb25SJuerg Haefliger struct dme1737_data *data = dev_get_drvdata(dev); 11869431996fSJuerg Haefliger struct sensor_device_attribute_2 11879431996fSJuerg Haefliger *sensor_attr_2 = to_sensor_dev_attr_2(attr); 11889431996fSJuerg Haefliger int ix = sensor_attr_2->index; 11899431996fSJuerg Haefliger int fn = sensor_attr_2->nr; 11909431996fSJuerg Haefliger long val = simple_strtol(buf, NULL, 10); 11919431996fSJuerg Haefliger 11929431996fSJuerg Haefliger mutex_lock(&data->update_lock); 11939431996fSJuerg Haefliger switch (fn) { 11949431996fSJuerg Haefliger case SYS_PWM: 11959431996fSJuerg Haefliger data->pwm[ix] = SENSORS_LIMIT(val, 0, 255); 1196dbc2bc25SJean Delvare dme1737_write(data, DME1737_REG_PWM(ix), data->pwm[ix]); 11979431996fSJuerg Haefliger break; 11989431996fSJuerg Haefliger case SYS_PWM_FREQ: 1199dbc2bc25SJean Delvare data->pwm_freq[ix] = PWM_FREQ_TO_REG(val, dme1737_read(data, 12009431996fSJuerg Haefliger DME1737_REG_PWM_FREQ(ix))); 1201dbc2bc25SJean Delvare dme1737_write(data, DME1737_REG_PWM_FREQ(ix), 12029431996fSJuerg Haefliger data->pwm_freq[ix]); 12039431996fSJuerg Haefliger break; 12049431996fSJuerg Haefliger case SYS_PWM_ENABLE: 12059431996fSJuerg Haefliger /* Only valid for pwm[1-3] */ 12069431996fSJuerg Haefliger if (val < 0 || val > 2) { 12079431996fSJuerg Haefliger count = -EINVAL; 1208e95c237dSJuerg Haefliger dev_warn(dev, "PWM enable %ld not " 12099431996fSJuerg Haefliger "supported. Choose one of 0, 1, or 2.\n", 12109431996fSJuerg Haefliger val); 12119431996fSJuerg Haefliger goto exit; 12129431996fSJuerg Haefliger } 12139431996fSJuerg Haefliger /* Refresh the cache */ 1214dbc2bc25SJean Delvare data->pwm_config[ix] = dme1737_read(data, 12159431996fSJuerg Haefliger DME1737_REG_PWM_CONFIG(ix)); 12169431996fSJuerg Haefliger if (val == PWM_EN_FROM_REG(data->pwm_config[ix])) { 12179431996fSJuerg Haefliger /* Bail out if no change */ 12189431996fSJuerg Haefliger goto exit; 12199431996fSJuerg Haefliger } 12209431996fSJuerg Haefliger /* Do some housekeeping if we are currently in auto mode */ 12219431996fSJuerg Haefliger if (PWM_EN_FROM_REG(data->pwm_config[ix]) == 2) { 12229431996fSJuerg Haefliger /* Save the current zone channel assignment */ 12239431996fSJuerg Haefliger data->pwm_acz[ix] = PWM_ACZ_FROM_REG( 12249431996fSJuerg Haefliger data->pwm_config[ix]); 12259431996fSJuerg Haefliger /* Save the current ramp rate state and disable it */ 1226dbc2bc25SJean Delvare data->pwm_rr[ix > 0] = dme1737_read(data, 12279431996fSJuerg Haefliger DME1737_REG_PWM_RR(ix > 0)); 12289431996fSJuerg Haefliger data->pwm_rr_en &= ~(1 << ix); 12299431996fSJuerg Haefliger if (PWM_RR_EN_FROM_REG(data->pwm_rr[ix > 0], ix)) { 12309431996fSJuerg Haefliger data->pwm_rr_en |= (1 << ix); 12319431996fSJuerg Haefliger data->pwm_rr[ix > 0] = PWM_RR_EN_TO_REG(0, ix, 12329431996fSJuerg Haefliger data->pwm_rr[ix > 0]); 1233dbc2bc25SJean Delvare dme1737_write(data, 12349431996fSJuerg Haefliger DME1737_REG_PWM_RR(ix > 0), 12359431996fSJuerg Haefliger data->pwm_rr[ix > 0]); 12369431996fSJuerg Haefliger } 12379431996fSJuerg Haefliger } 12389431996fSJuerg Haefliger /* Set the new PWM mode */ 12399431996fSJuerg Haefliger switch (val) { 12409431996fSJuerg Haefliger case 0: 12419431996fSJuerg Haefliger /* Change permissions of pwm[ix] to read-only */ 124273ce48f6SJuerg Haefliger dme1737_chmod_file(dev, dme1737_pwm_chmod_attr[ix], 12439431996fSJuerg Haefliger S_IRUGO); 12449431996fSJuerg Haefliger /* Turn fan fully on */ 12459431996fSJuerg Haefliger data->pwm_config[ix] = PWM_EN_TO_REG(0, 12469431996fSJuerg Haefliger data->pwm_config[ix]); 1247dbc2bc25SJean Delvare dme1737_write(data, DME1737_REG_PWM_CONFIG(ix), 12489431996fSJuerg Haefliger data->pwm_config[ix]); 12499431996fSJuerg Haefliger break; 12509431996fSJuerg Haefliger case 1: 12519431996fSJuerg Haefliger /* Turn on manual mode */ 12529431996fSJuerg Haefliger data->pwm_config[ix] = PWM_EN_TO_REG(1, 12539431996fSJuerg Haefliger data->pwm_config[ix]); 1254dbc2bc25SJean Delvare dme1737_write(data, DME1737_REG_PWM_CONFIG(ix), 12559431996fSJuerg Haefliger data->pwm_config[ix]); 12569431996fSJuerg Haefliger /* Change permissions of pwm[ix] to read-writeable */ 125773ce48f6SJuerg Haefliger dme1737_chmod_file(dev, dme1737_pwm_chmod_attr[ix], 12589431996fSJuerg Haefliger S_IRUGO | S_IWUSR); 12599431996fSJuerg Haefliger break; 12609431996fSJuerg Haefliger case 2: 12619431996fSJuerg Haefliger /* Change permissions of pwm[ix] to read-only */ 126273ce48f6SJuerg Haefliger dme1737_chmod_file(dev, dme1737_pwm_chmod_attr[ix], 12639431996fSJuerg Haefliger S_IRUGO); 12649431996fSJuerg Haefliger /* Turn on auto mode using the saved zone channel 12659431996fSJuerg Haefliger * assignment */ 12669431996fSJuerg Haefliger data->pwm_config[ix] = PWM_ACZ_TO_REG( 12679431996fSJuerg Haefliger data->pwm_acz[ix], 12689431996fSJuerg Haefliger data->pwm_config[ix]); 1269dbc2bc25SJean Delvare dme1737_write(data, DME1737_REG_PWM_CONFIG(ix), 12709431996fSJuerg Haefliger data->pwm_config[ix]); 12719431996fSJuerg Haefliger /* Enable PWM ramp rate if previously enabled */ 12729431996fSJuerg Haefliger if (data->pwm_rr_en & (1 << ix)) { 12739431996fSJuerg Haefliger data->pwm_rr[ix > 0] = PWM_RR_EN_TO_REG(1, ix, 1274dbc2bc25SJean Delvare dme1737_read(data, 12759431996fSJuerg Haefliger DME1737_REG_PWM_RR(ix > 0))); 1276dbc2bc25SJean Delvare dme1737_write(data, 12779431996fSJuerg Haefliger DME1737_REG_PWM_RR(ix > 0), 12789431996fSJuerg Haefliger data->pwm_rr[ix > 0]); 12799431996fSJuerg Haefliger } 12809431996fSJuerg Haefliger break; 12819431996fSJuerg Haefliger } 12829431996fSJuerg Haefliger break; 12839431996fSJuerg Haefliger case SYS_PWM_RAMP_RATE: 12849431996fSJuerg Haefliger /* Only valid for pwm[1-3] */ 12859431996fSJuerg Haefliger /* Refresh the cache */ 1286dbc2bc25SJean Delvare data->pwm_config[ix] = dme1737_read(data, 12879431996fSJuerg Haefliger DME1737_REG_PWM_CONFIG(ix)); 1288dbc2bc25SJean Delvare data->pwm_rr[ix > 0] = dme1737_read(data, 12899431996fSJuerg Haefliger DME1737_REG_PWM_RR(ix > 0)); 12909431996fSJuerg Haefliger /* Set the ramp rate value */ 12919431996fSJuerg Haefliger if (val > 0) { 12929431996fSJuerg Haefliger data->pwm_rr[ix > 0] = PWM_RR_TO_REG(val, ix, 12939431996fSJuerg Haefliger data->pwm_rr[ix > 0]); 12949431996fSJuerg Haefliger } 12959431996fSJuerg Haefliger /* Enable/disable the feature only if the associated PWM 12969431996fSJuerg Haefliger * output is in automatic mode. */ 12979431996fSJuerg Haefliger if (PWM_EN_FROM_REG(data->pwm_config[ix]) == 2) { 12989431996fSJuerg Haefliger data->pwm_rr[ix > 0] = PWM_RR_EN_TO_REG(val > 0, ix, 12999431996fSJuerg Haefliger data->pwm_rr[ix > 0]); 13009431996fSJuerg Haefliger } 1301dbc2bc25SJean Delvare dme1737_write(data, DME1737_REG_PWM_RR(ix > 0), 13029431996fSJuerg Haefliger data->pwm_rr[ix > 0]); 13039431996fSJuerg Haefliger break; 13049431996fSJuerg Haefliger case SYS_PWM_AUTO_CHANNELS_ZONE: 13059431996fSJuerg Haefliger /* Only valid for pwm[1-3] */ 13069431996fSJuerg Haefliger if (!(val == 1 || val == 2 || val == 4 || 13079431996fSJuerg Haefliger val == 6 || val == 7)) { 13089431996fSJuerg Haefliger count = -EINVAL; 1309e95c237dSJuerg Haefliger dev_warn(dev, "PWM auto channels zone %ld " 13109431996fSJuerg Haefliger "not supported. Choose one of 1, 2, 4, 6, " 13119431996fSJuerg Haefliger "or 7.\n", val); 13129431996fSJuerg Haefliger goto exit; 13139431996fSJuerg Haefliger } 13149431996fSJuerg Haefliger /* Refresh the cache */ 1315dbc2bc25SJean Delvare data->pwm_config[ix] = dme1737_read(data, 13169431996fSJuerg Haefliger DME1737_REG_PWM_CONFIG(ix)); 13179431996fSJuerg Haefliger if (PWM_EN_FROM_REG(data->pwm_config[ix]) == 2) { 13189431996fSJuerg Haefliger /* PWM is already in auto mode so update the temp 13199431996fSJuerg Haefliger * channel assignment */ 13209431996fSJuerg Haefliger data->pwm_config[ix] = PWM_ACZ_TO_REG(val, 13219431996fSJuerg Haefliger data->pwm_config[ix]); 1322dbc2bc25SJean Delvare dme1737_write(data, DME1737_REG_PWM_CONFIG(ix), 13239431996fSJuerg Haefliger data->pwm_config[ix]); 13249431996fSJuerg Haefliger } else { 13259431996fSJuerg Haefliger /* PWM is not in auto mode so we save the temp 13269431996fSJuerg Haefliger * channel assignment for later use */ 13279431996fSJuerg Haefliger data->pwm_acz[ix] = val; 13289431996fSJuerg Haefliger } 13299431996fSJuerg Haefliger break; 13309431996fSJuerg Haefliger case SYS_PWM_AUTO_PWM_MIN: 13319431996fSJuerg Haefliger /* Only valid for pwm[1-3] */ 13329431996fSJuerg Haefliger /* Refresh the cache */ 1333dbc2bc25SJean Delvare data->pwm_min[ix] = dme1737_read(data, 13349431996fSJuerg Haefliger DME1737_REG_PWM_MIN(ix)); 13359431996fSJuerg Haefliger /* There are only 2 values supported for the auto_pwm_min 13369431996fSJuerg Haefliger * value: 0 or auto_point1_pwm. So if the temperature drops 13379431996fSJuerg Haefliger * below the auto_point1_temp_hyst value, the fan either turns 13389431996fSJuerg Haefliger * off or runs at auto_point1_pwm duty-cycle. */ 13399431996fSJuerg Haefliger if (val > ((data->pwm_min[ix] + 1) / 2)) { 13409431996fSJuerg Haefliger data->pwm_rr[0] = PWM_OFF_TO_REG(1, ix, 1341dbc2bc25SJean Delvare dme1737_read(data, 13429431996fSJuerg Haefliger DME1737_REG_PWM_RR(0))); 13439431996fSJuerg Haefliger } else { 13449431996fSJuerg Haefliger data->pwm_rr[0] = PWM_OFF_TO_REG(0, ix, 1345dbc2bc25SJean Delvare dme1737_read(data, 13469431996fSJuerg Haefliger DME1737_REG_PWM_RR(0))); 13479431996fSJuerg Haefliger } 1348dbc2bc25SJean Delvare dme1737_write(data, DME1737_REG_PWM_RR(0), 13499431996fSJuerg Haefliger data->pwm_rr[0]); 13509431996fSJuerg Haefliger break; 13519431996fSJuerg Haefliger case SYS_PWM_AUTO_POINT1_PWM: 13529431996fSJuerg Haefliger /* Only valid for pwm[1-3] */ 13539431996fSJuerg Haefliger data->pwm_min[ix] = SENSORS_LIMIT(val, 0, 255); 1354dbc2bc25SJean Delvare dme1737_write(data, DME1737_REG_PWM_MIN(ix), 13559431996fSJuerg Haefliger data->pwm_min[ix]); 13569431996fSJuerg Haefliger break; 13579431996fSJuerg Haefliger default: 1358b237eb25SJuerg Haefliger dev_dbg(dev, "Unknown function %d.\n", fn); 13599431996fSJuerg Haefliger } 13609431996fSJuerg Haefliger exit: 13619431996fSJuerg Haefliger mutex_unlock(&data->update_lock); 13629431996fSJuerg Haefliger 13639431996fSJuerg Haefliger return count; 13649431996fSJuerg Haefliger } 13659431996fSJuerg Haefliger 13669431996fSJuerg Haefliger /* --------------------------------------------------------------------- 13679431996fSJuerg Haefliger * Miscellaneous sysfs attributes 13689431996fSJuerg Haefliger * --------------------------------------------------------------------- */ 13699431996fSJuerg Haefliger 13709431996fSJuerg Haefliger static ssize_t show_vrm(struct device *dev, struct device_attribute *attr, 13719431996fSJuerg Haefliger char *buf) 13729431996fSJuerg Haefliger { 13739431996fSJuerg Haefliger struct i2c_client *client = to_i2c_client(dev); 13749431996fSJuerg Haefliger struct dme1737_data *data = i2c_get_clientdata(client); 13759431996fSJuerg Haefliger 13769431996fSJuerg Haefliger return sprintf(buf, "%d\n", data->vrm); 13779431996fSJuerg Haefliger } 13789431996fSJuerg Haefliger 13799431996fSJuerg Haefliger static ssize_t set_vrm(struct device *dev, struct device_attribute *attr, 13809431996fSJuerg Haefliger const char *buf, size_t count) 13819431996fSJuerg Haefliger { 1382b237eb25SJuerg Haefliger struct dme1737_data *data = dev_get_drvdata(dev); 13839431996fSJuerg Haefliger long val = simple_strtol(buf, NULL, 10); 13849431996fSJuerg Haefliger 13859431996fSJuerg Haefliger data->vrm = val; 13869431996fSJuerg Haefliger return count; 13879431996fSJuerg Haefliger } 13889431996fSJuerg Haefliger 13899431996fSJuerg Haefliger static ssize_t show_vid(struct device *dev, struct device_attribute *attr, 13909431996fSJuerg Haefliger char *buf) 13919431996fSJuerg Haefliger { 13929431996fSJuerg Haefliger struct dme1737_data *data = dme1737_update_device(dev); 13939431996fSJuerg Haefliger 13949431996fSJuerg Haefliger return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm)); 13959431996fSJuerg Haefliger } 13969431996fSJuerg Haefliger 1397e95c237dSJuerg Haefliger static ssize_t show_name(struct device *dev, struct device_attribute *attr, 1398e95c237dSJuerg Haefliger char *buf) 1399e95c237dSJuerg Haefliger { 1400e95c237dSJuerg Haefliger struct dme1737_data *data = dev_get_drvdata(dev); 1401e95c237dSJuerg Haefliger 1402dbc2bc25SJean Delvare return sprintf(buf, "%s\n", data->name); 1403e95c237dSJuerg Haefliger } 1404e95c237dSJuerg Haefliger 14059431996fSJuerg Haefliger /* --------------------------------------------------------------------- 14069431996fSJuerg Haefliger * Sysfs device attribute defines and structs 14079431996fSJuerg Haefliger * --------------------------------------------------------------------- */ 14089431996fSJuerg Haefliger 14099431996fSJuerg Haefliger /* Voltages 0-6 */ 14109431996fSJuerg Haefliger 14119431996fSJuerg Haefliger #define SENSOR_DEVICE_ATTR_IN(ix) \ 14129431996fSJuerg Haefliger static SENSOR_DEVICE_ATTR_2(in##ix##_input, S_IRUGO, \ 14139431996fSJuerg Haefliger show_in, NULL, SYS_IN_INPUT, ix); \ 14149431996fSJuerg Haefliger static SENSOR_DEVICE_ATTR_2(in##ix##_min, S_IRUGO | S_IWUSR, \ 14159431996fSJuerg Haefliger show_in, set_in, SYS_IN_MIN, ix); \ 14169431996fSJuerg Haefliger static SENSOR_DEVICE_ATTR_2(in##ix##_max, S_IRUGO | S_IWUSR, \ 14179431996fSJuerg Haefliger show_in, set_in, SYS_IN_MAX, ix); \ 14189431996fSJuerg Haefliger static SENSOR_DEVICE_ATTR_2(in##ix##_alarm, S_IRUGO, \ 14199431996fSJuerg Haefliger show_in, NULL, SYS_IN_ALARM, ix) 14209431996fSJuerg Haefliger 14219431996fSJuerg Haefliger SENSOR_DEVICE_ATTR_IN(0); 14229431996fSJuerg Haefliger SENSOR_DEVICE_ATTR_IN(1); 14239431996fSJuerg Haefliger SENSOR_DEVICE_ATTR_IN(2); 14249431996fSJuerg Haefliger SENSOR_DEVICE_ATTR_IN(3); 14259431996fSJuerg Haefliger SENSOR_DEVICE_ATTR_IN(4); 14269431996fSJuerg Haefliger SENSOR_DEVICE_ATTR_IN(5); 14279431996fSJuerg Haefliger SENSOR_DEVICE_ATTR_IN(6); 14289431996fSJuerg Haefliger 14299431996fSJuerg Haefliger /* Temperatures 1-3 */ 14309431996fSJuerg Haefliger 14319431996fSJuerg Haefliger #define SENSOR_DEVICE_ATTR_TEMP(ix) \ 14329431996fSJuerg Haefliger static SENSOR_DEVICE_ATTR_2(temp##ix##_input, S_IRUGO, \ 14339431996fSJuerg Haefliger show_temp, NULL, SYS_TEMP_INPUT, ix-1); \ 14349431996fSJuerg Haefliger static SENSOR_DEVICE_ATTR_2(temp##ix##_min, S_IRUGO | S_IWUSR, \ 14359431996fSJuerg Haefliger show_temp, set_temp, SYS_TEMP_MIN, ix-1); \ 14369431996fSJuerg Haefliger static SENSOR_DEVICE_ATTR_2(temp##ix##_max, S_IRUGO | S_IWUSR, \ 14379431996fSJuerg Haefliger show_temp, set_temp, SYS_TEMP_MAX, ix-1); \ 14389431996fSJuerg Haefliger static SENSOR_DEVICE_ATTR_2(temp##ix##_offset, S_IRUGO, \ 14399431996fSJuerg Haefliger show_temp, set_temp, SYS_TEMP_OFFSET, ix-1); \ 14409431996fSJuerg Haefliger static SENSOR_DEVICE_ATTR_2(temp##ix##_alarm, S_IRUGO, \ 14419431996fSJuerg Haefliger show_temp, NULL, SYS_TEMP_ALARM, ix-1); \ 14429431996fSJuerg Haefliger static SENSOR_DEVICE_ATTR_2(temp##ix##_fault, S_IRUGO, \ 14439431996fSJuerg Haefliger show_temp, NULL, SYS_TEMP_FAULT, ix-1) 14449431996fSJuerg Haefliger 14459431996fSJuerg Haefliger SENSOR_DEVICE_ATTR_TEMP(1); 14469431996fSJuerg Haefliger SENSOR_DEVICE_ATTR_TEMP(2); 14479431996fSJuerg Haefliger SENSOR_DEVICE_ATTR_TEMP(3); 14489431996fSJuerg Haefliger 14499431996fSJuerg Haefliger /* Zones 1-3 */ 14509431996fSJuerg Haefliger 14519431996fSJuerg Haefliger #define SENSOR_DEVICE_ATTR_ZONE(ix) \ 14529431996fSJuerg Haefliger static SENSOR_DEVICE_ATTR_2(zone##ix##_auto_channels_temp, S_IRUGO, \ 14539431996fSJuerg Haefliger show_zone, NULL, SYS_ZONE_AUTO_CHANNELS_TEMP, ix-1); \ 14549431996fSJuerg Haefliger static SENSOR_DEVICE_ATTR_2(zone##ix##_auto_point1_temp_hyst, S_IRUGO, \ 14559431996fSJuerg Haefliger show_zone, set_zone, SYS_ZONE_AUTO_POINT1_TEMP_HYST, ix-1); \ 14569431996fSJuerg Haefliger static SENSOR_DEVICE_ATTR_2(zone##ix##_auto_point1_temp, S_IRUGO, \ 14579431996fSJuerg Haefliger show_zone, set_zone, SYS_ZONE_AUTO_POINT1_TEMP, ix-1); \ 14589431996fSJuerg Haefliger static SENSOR_DEVICE_ATTR_2(zone##ix##_auto_point2_temp, S_IRUGO, \ 14599431996fSJuerg Haefliger show_zone, set_zone, SYS_ZONE_AUTO_POINT2_TEMP, ix-1); \ 14609431996fSJuerg Haefliger static SENSOR_DEVICE_ATTR_2(zone##ix##_auto_point3_temp, S_IRUGO, \ 14619431996fSJuerg Haefliger show_zone, set_zone, SYS_ZONE_AUTO_POINT3_TEMP, ix-1) 14629431996fSJuerg Haefliger 14639431996fSJuerg Haefliger SENSOR_DEVICE_ATTR_ZONE(1); 14649431996fSJuerg Haefliger SENSOR_DEVICE_ATTR_ZONE(2); 14659431996fSJuerg Haefliger SENSOR_DEVICE_ATTR_ZONE(3); 14669431996fSJuerg Haefliger 14679431996fSJuerg Haefliger /* Fans 1-4 */ 14689431996fSJuerg Haefliger 14699431996fSJuerg Haefliger #define SENSOR_DEVICE_ATTR_FAN_1TO4(ix) \ 14709431996fSJuerg Haefliger static SENSOR_DEVICE_ATTR_2(fan##ix##_input, S_IRUGO, \ 14719431996fSJuerg Haefliger show_fan, NULL, SYS_FAN_INPUT, ix-1); \ 14729431996fSJuerg Haefliger static SENSOR_DEVICE_ATTR_2(fan##ix##_min, S_IRUGO | S_IWUSR, \ 14739431996fSJuerg Haefliger show_fan, set_fan, SYS_FAN_MIN, ix-1); \ 14749431996fSJuerg Haefliger static SENSOR_DEVICE_ATTR_2(fan##ix##_alarm, S_IRUGO, \ 14759431996fSJuerg Haefliger show_fan, NULL, SYS_FAN_ALARM, ix-1); \ 14769431996fSJuerg Haefliger static SENSOR_DEVICE_ATTR_2(fan##ix##_type, S_IRUGO | S_IWUSR, \ 14779431996fSJuerg Haefliger show_fan, set_fan, SYS_FAN_TYPE, ix-1) 14789431996fSJuerg Haefliger 14799431996fSJuerg Haefliger SENSOR_DEVICE_ATTR_FAN_1TO4(1); 14809431996fSJuerg Haefliger SENSOR_DEVICE_ATTR_FAN_1TO4(2); 14819431996fSJuerg Haefliger SENSOR_DEVICE_ATTR_FAN_1TO4(3); 14829431996fSJuerg Haefliger SENSOR_DEVICE_ATTR_FAN_1TO4(4); 14839431996fSJuerg Haefliger 14849431996fSJuerg Haefliger /* Fans 5-6 */ 14859431996fSJuerg Haefliger 14869431996fSJuerg Haefliger #define SENSOR_DEVICE_ATTR_FAN_5TO6(ix) \ 14879431996fSJuerg Haefliger static SENSOR_DEVICE_ATTR_2(fan##ix##_input, S_IRUGO, \ 14889431996fSJuerg Haefliger show_fan, NULL, SYS_FAN_INPUT, ix-1); \ 14899431996fSJuerg Haefliger static SENSOR_DEVICE_ATTR_2(fan##ix##_min, S_IRUGO | S_IWUSR, \ 14909431996fSJuerg Haefliger show_fan, set_fan, SYS_FAN_MIN, ix-1); \ 14919431996fSJuerg Haefliger static SENSOR_DEVICE_ATTR_2(fan##ix##_alarm, S_IRUGO, \ 14929431996fSJuerg Haefliger show_fan, NULL, SYS_FAN_ALARM, ix-1); \ 14939431996fSJuerg Haefliger static SENSOR_DEVICE_ATTR_2(fan##ix##_max, S_IRUGO | S_IWUSR, \ 14949431996fSJuerg Haefliger show_fan, set_fan, SYS_FAN_MAX, ix-1) 14959431996fSJuerg Haefliger 14969431996fSJuerg Haefliger SENSOR_DEVICE_ATTR_FAN_5TO6(5); 14979431996fSJuerg Haefliger SENSOR_DEVICE_ATTR_FAN_5TO6(6); 14989431996fSJuerg Haefliger 14999431996fSJuerg Haefliger /* PWMs 1-3 */ 15009431996fSJuerg Haefliger 15019431996fSJuerg Haefliger #define SENSOR_DEVICE_ATTR_PWM_1TO3(ix) \ 15029431996fSJuerg Haefliger static SENSOR_DEVICE_ATTR_2(pwm##ix, S_IRUGO, \ 15039431996fSJuerg Haefliger show_pwm, set_pwm, SYS_PWM, ix-1); \ 15049431996fSJuerg Haefliger static SENSOR_DEVICE_ATTR_2(pwm##ix##_freq, S_IRUGO, \ 15059431996fSJuerg Haefliger show_pwm, set_pwm, SYS_PWM_FREQ, ix-1); \ 15069431996fSJuerg Haefliger static SENSOR_DEVICE_ATTR_2(pwm##ix##_enable, S_IRUGO, \ 15079431996fSJuerg Haefliger show_pwm, set_pwm, SYS_PWM_ENABLE, ix-1); \ 15089431996fSJuerg Haefliger static SENSOR_DEVICE_ATTR_2(pwm##ix##_ramp_rate, S_IRUGO, \ 15099431996fSJuerg Haefliger show_pwm, set_pwm, SYS_PWM_RAMP_RATE, ix-1); \ 15109431996fSJuerg Haefliger static SENSOR_DEVICE_ATTR_2(pwm##ix##_auto_channels_zone, S_IRUGO, \ 15119431996fSJuerg Haefliger show_pwm, set_pwm, SYS_PWM_AUTO_CHANNELS_ZONE, ix-1); \ 15129431996fSJuerg Haefliger static SENSOR_DEVICE_ATTR_2(pwm##ix##_auto_pwm_min, S_IRUGO, \ 15139431996fSJuerg Haefliger show_pwm, set_pwm, SYS_PWM_AUTO_PWM_MIN, ix-1); \ 15149431996fSJuerg Haefliger static SENSOR_DEVICE_ATTR_2(pwm##ix##_auto_point1_pwm, S_IRUGO, \ 15159431996fSJuerg Haefliger show_pwm, set_pwm, SYS_PWM_AUTO_POINT1_PWM, ix-1); \ 15169431996fSJuerg Haefliger static SENSOR_DEVICE_ATTR_2(pwm##ix##_auto_point2_pwm, S_IRUGO, \ 15179431996fSJuerg Haefliger show_pwm, NULL, SYS_PWM_AUTO_POINT2_PWM, ix-1) 15189431996fSJuerg Haefliger 15199431996fSJuerg Haefliger SENSOR_DEVICE_ATTR_PWM_1TO3(1); 15209431996fSJuerg Haefliger SENSOR_DEVICE_ATTR_PWM_1TO3(2); 15219431996fSJuerg Haefliger SENSOR_DEVICE_ATTR_PWM_1TO3(3); 15229431996fSJuerg Haefliger 15239431996fSJuerg Haefliger /* PWMs 5-6 */ 15249431996fSJuerg Haefliger 15259431996fSJuerg Haefliger #define SENSOR_DEVICE_ATTR_PWM_5TO6(ix) \ 15269b257714SJuerg Haefliger static SENSOR_DEVICE_ATTR_2(pwm##ix, S_IRUGO, \ 15279431996fSJuerg Haefliger show_pwm, set_pwm, SYS_PWM, ix-1); \ 15289b257714SJuerg Haefliger static SENSOR_DEVICE_ATTR_2(pwm##ix##_freq, S_IRUGO, \ 15299431996fSJuerg Haefliger show_pwm, set_pwm, SYS_PWM_FREQ, ix-1); \ 15309431996fSJuerg Haefliger static SENSOR_DEVICE_ATTR_2(pwm##ix##_enable, S_IRUGO, \ 15319431996fSJuerg Haefliger show_pwm, NULL, SYS_PWM_ENABLE, ix-1) 15329431996fSJuerg Haefliger 15339431996fSJuerg Haefliger SENSOR_DEVICE_ATTR_PWM_5TO6(5); 15349431996fSJuerg Haefliger SENSOR_DEVICE_ATTR_PWM_5TO6(6); 15359431996fSJuerg Haefliger 15369431996fSJuerg Haefliger /* Misc */ 15379431996fSJuerg Haefliger 15389431996fSJuerg Haefliger static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm); 15399431996fSJuerg Haefliger static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL); 1540e95c237dSJuerg Haefliger static DEVICE_ATTR(name, S_IRUGO, show_name, NULL); /* for ISA devices */ 15419431996fSJuerg Haefliger 15429431996fSJuerg Haefliger /* This struct holds all the attributes that are always present and need to be 15439431996fSJuerg Haefliger * created unconditionally. The attributes that need modification of their 15449431996fSJuerg Haefliger * permissions are created read-only and write permissions are added or removed 15459431996fSJuerg Haefliger * on the fly when required */ 15469431996fSJuerg Haefliger static struct attribute *dme1737_attr[] ={ 15479431996fSJuerg Haefliger /* Voltages */ 15489b257714SJuerg Haefliger &sensor_dev_attr_in0_input.dev_attr.attr, 15499b257714SJuerg Haefliger &sensor_dev_attr_in0_min.dev_attr.attr, 15509b257714SJuerg Haefliger &sensor_dev_attr_in0_max.dev_attr.attr, 15519b257714SJuerg Haefliger &sensor_dev_attr_in0_alarm.dev_attr.attr, 15529b257714SJuerg Haefliger &sensor_dev_attr_in1_input.dev_attr.attr, 15539b257714SJuerg Haefliger &sensor_dev_attr_in1_min.dev_attr.attr, 15549b257714SJuerg Haefliger &sensor_dev_attr_in1_max.dev_attr.attr, 15559b257714SJuerg Haefliger &sensor_dev_attr_in1_alarm.dev_attr.attr, 15569b257714SJuerg Haefliger &sensor_dev_attr_in2_input.dev_attr.attr, 15579b257714SJuerg Haefliger &sensor_dev_attr_in2_min.dev_attr.attr, 15589b257714SJuerg Haefliger &sensor_dev_attr_in2_max.dev_attr.attr, 15599b257714SJuerg Haefliger &sensor_dev_attr_in2_alarm.dev_attr.attr, 15609b257714SJuerg Haefliger &sensor_dev_attr_in3_input.dev_attr.attr, 15619b257714SJuerg Haefliger &sensor_dev_attr_in3_min.dev_attr.attr, 15629b257714SJuerg Haefliger &sensor_dev_attr_in3_max.dev_attr.attr, 15639b257714SJuerg Haefliger &sensor_dev_attr_in3_alarm.dev_attr.attr, 15649b257714SJuerg Haefliger &sensor_dev_attr_in4_input.dev_attr.attr, 15659b257714SJuerg Haefliger &sensor_dev_attr_in4_min.dev_attr.attr, 15669b257714SJuerg Haefliger &sensor_dev_attr_in4_max.dev_attr.attr, 15679b257714SJuerg Haefliger &sensor_dev_attr_in4_alarm.dev_attr.attr, 15689b257714SJuerg Haefliger &sensor_dev_attr_in5_input.dev_attr.attr, 15699b257714SJuerg Haefliger &sensor_dev_attr_in5_min.dev_attr.attr, 15709b257714SJuerg Haefliger &sensor_dev_attr_in5_max.dev_attr.attr, 15719b257714SJuerg Haefliger &sensor_dev_attr_in5_alarm.dev_attr.attr, 15729b257714SJuerg Haefliger &sensor_dev_attr_in6_input.dev_attr.attr, 15739b257714SJuerg Haefliger &sensor_dev_attr_in6_min.dev_attr.attr, 15749b257714SJuerg Haefliger &sensor_dev_attr_in6_max.dev_attr.attr, 15759b257714SJuerg Haefliger &sensor_dev_attr_in6_alarm.dev_attr.attr, 15769431996fSJuerg Haefliger /* Temperatures */ 15779b257714SJuerg Haefliger &sensor_dev_attr_temp1_input.dev_attr.attr, 15789b257714SJuerg Haefliger &sensor_dev_attr_temp1_min.dev_attr.attr, 15799b257714SJuerg Haefliger &sensor_dev_attr_temp1_max.dev_attr.attr, 15809b257714SJuerg Haefliger &sensor_dev_attr_temp1_alarm.dev_attr.attr, 15819b257714SJuerg Haefliger &sensor_dev_attr_temp1_fault.dev_attr.attr, 15829b257714SJuerg Haefliger &sensor_dev_attr_temp2_input.dev_attr.attr, 15839b257714SJuerg Haefliger &sensor_dev_attr_temp2_min.dev_attr.attr, 15849b257714SJuerg Haefliger &sensor_dev_attr_temp2_max.dev_attr.attr, 15859b257714SJuerg Haefliger &sensor_dev_attr_temp2_alarm.dev_attr.attr, 15869b257714SJuerg Haefliger &sensor_dev_attr_temp2_fault.dev_attr.attr, 15879b257714SJuerg Haefliger &sensor_dev_attr_temp3_input.dev_attr.attr, 15889b257714SJuerg Haefliger &sensor_dev_attr_temp3_min.dev_attr.attr, 15899b257714SJuerg Haefliger &sensor_dev_attr_temp3_max.dev_attr.attr, 15909b257714SJuerg Haefliger &sensor_dev_attr_temp3_alarm.dev_attr.attr, 15919b257714SJuerg Haefliger &sensor_dev_attr_temp3_fault.dev_attr.attr, 15929431996fSJuerg Haefliger /* Zones */ 15939b257714SJuerg Haefliger &sensor_dev_attr_zone1_auto_point1_temp.dev_attr.attr, 15949b257714SJuerg Haefliger &sensor_dev_attr_zone1_auto_point2_temp.dev_attr.attr, 15959b257714SJuerg Haefliger &sensor_dev_attr_zone1_auto_point3_temp.dev_attr.attr, 15969b257714SJuerg Haefliger &sensor_dev_attr_zone1_auto_channels_temp.dev_attr.attr, 15979b257714SJuerg Haefliger &sensor_dev_attr_zone2_auto_point1_temp.dev_attr.attr, 15989b257714SJuerg Haefliger &sensor_dev_attr_zone2_auto_point2_temp.dev_attr.attr, 15999b257714SJuerg Haefliger &sensor_dev_attr_zone2_auto_point3_temp.dev_attr.attr, 16009b257714SJuerg Haefliger &sensor_dev_attr_zone2_auto_channels_temp.dev_attr.attr, 16019b257714SJuerg Haefliger &sensor_dev_attr_zone3_auto_point1_temp.dev_attr.attr, 16029b257714SJuerg Haefliger &sensor_dev_attr_zone3_auto_point2_temp.dev_attr.attr, 16039b257714SJuerg Haefliger &sensor_dev_attr_zone3_auto_point3_temp.dev_attr.attr, 16049b257714SJuerg Haefliger &sensor_dev_attr_zone3_auto_channels_temp.dev_attr.attr, 1605549edb83SJuerg Haefliger NULL 1606549edb83SJuerg Haefliger }; 1607549edb83SJuerg Haefliger 1608549edb83SJuerg Haefliger static const struct attribute_group dme1737_group = { 1609549edb83SJuerg Haefliger .attrs = dme1737_attr, 1610549edb83SJuerg Haefliger }; 1611549edb83SJuerg Haefliger 1612549edb83SJuerg Haefliger /* The following struct holds misc attributes, which are not available in all 1613549edb83SJuerg Haefliger * chips. Their creation depends on the chip type which is determined during 1614549edb83SJuerg Haefliger * module load. */ 1615549edb83SJuerg Haefliger static struct attribute *dme1737_misc_attr[] = { 1616549edb83SJuerg Haefliger /* Temperatures */ 1617549edb83SJuerg Haefliger &sensor_dev_attr_temp1_offset.dev_attr.attr, 1618549edb83SJuerg Haefliger &sensor_dev_attr_temp2_offset.dev_attr.attr, 1619549edb83SJuerg Haefliger &sensor_dev_attr_temp3_offset.dev_attr.attr, 1620549edb83SJuerg Haefliger /* Zones */ 1621549edb83SJuerg Haefliger &sensor_dev_attr_zone1_auto_point1_temp_hyst.dev_attr.attr, 1622549edb83SJuerg Haefliger &sensor_dev_attr_zone2_auto_point1_temp_hyst.dev_attr.attr, 1623549edb83SJuerg Haefliger &sensor_dev_attr_zone3_auto_point1_temp_hyst.dev_attr.attr, 16249431996fSJuerg Haefliger NULL 16259431996fSJuerg Haefliger }; 16269431996fSJuerg Haefliger 1627549edb83SJuerg Haefliger static const struct attribute_group dme1737_misc_group = { 1628549edb83SJuerg Haefliger .attrs = dme1737_misc_attr, 16299431996fSJuerg Haefliger }; 16309431996fSJuerg Haefliger 16319d091446SJean Delvare /* The following struct holds VID-related attributes. Their creation 16329d091446SJean Delvare depends on the chip type which is determined during module load. */ 16339d091446SJean Delvare static struct attribute *dme1737_vid_attr[] = { 16349d091446SJean Delvare &dev_attr_vrm.attr, 16359d091446SJean Delvare &dev_attr_cpu0_vid.attr, 16369d091446SJean Delvare NULL 16379d091446SJean Delvare }; 16389d091446SJean Delvare 16399d091446SJean Delvare static const struct attribute_group dme1737_vid_group = { 16409d091446SJean Delvare .attrs = dme1737_vid_attr, 16419d091446SJean Delvare }; 16429d091446SJean Delvare 16439431996fSJuerg Haefliger /* The following structs hold the PWM attributes, some of which are optional. 16449431996fSJuerg Haefliger * Their creation depends on the chip configuration which is determined during 16459431996fSJuerg Haefliger * module load. */ 164673ce48f6SJuerg Haefliger static struct attribute *dme1737_pwm1_attr[] = { 16479b257714SJuerg Haefliger &sensor_dev_attr_pwm1.dev_attr.attr, 16489b257714SJuerg Haefliger &sensor_dev_attr_pwm1_freq.dev_attr.attr, 16499b257714SJuerg Haefliger &sensor_dev_attr_pwm1_enable.dev_attr.attr, 16509b257714SJuerg Haefliger &sensor_dev_attr_pwm1_ramp_rate.dev_attr.attr, 16519b257714SJuerg Haefliger &sensor_dev_attr_pwm1_auto_channels_zone.dev_attr.attr, 16529b257714SJuerg Haefliger &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr, 16539b257714SJuerg Haefliger &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr, 16549431996fSJuerg Haefliger NULL 16559431996fSJuerg Haefliger }; 165673ce48f6SJuerg Haefliger static struct attribute *dme1737_pwm2_attr[] = { 16579b257714SJuerg Haefliger &sensor_dev_attr_pwm2.dev_attr.attr, 16589b257714SJuerg Haefliger &sensor_dev_attr_pwm2_freq.dev_attr.attr, 16599b257714SJuerg Haefliger &sensor_dev_attr_pwm2_enable.dev_attr.attr, 16609b257714SJuerg Haefliger &sensor_dev_attr_pwm2_ramp_rate.dev_attr.attr, 16619b257714SJuerg Haefliger &sensor_dev_attr_pwm2_auto_channels_zone.dev_attr.attr, 16629b257714SJuerg Haefliger &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr, 16639b257714SJuerg Haefliger &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr, 16649431996fSJuerg Haefliger NULL 16659431996fSJuerg Haefliger }; 166673ce48f6SJuerg Haefliger static struct attribute *dme1737_pwm3_attr[] = { 16679b257714SJuerg Haefliger &sensor_dev_attr_pwm3.dev_attr.attr, 16689b257714SJuerg Haefliger &sensor_dev_attr_pwm3_freq.dev_attr.attr, 16699b257714SJuerg Haefliger &sensor_dev_attr_pwm3_enable.dev_attr.attr, 16709b257714SJuerg Haefliger &sensor_dev_attr_pwm3_ramp_rate.dev_attr.attr, 16719b257714SJuerg Haefliger &sensor_dev_attr_pwm3_auto_channels_zone.dev_attr.attr, 16729b257714SJuerg Haefliger &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr, 16739b257714SJuerg Haefliger &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr, 16749431996fSJuerg Haefliger NULL 16759431996fSJuerg Haefliger }; 167673ce48f6SJuerg Haefliger static struct attribute *dme1737_pwm5_attr[] = { 16779b257714SJuerg Haefliger &sensor_dev_attr_pwm5.dev_attr.attr, 16789b257714SJuerg Haefliger &sensor_dev_attr_pwm5_freq.dev_attr.attr, 16799b257714SJuerg Haefliger &sensor_dev_attr_pwm5_enable.dev_attr.attr, 16809431996fSJuerg Haefliger NULL 16819431996fSJuerg Haefliger }; 168273ce48f6SJuerg Haefliger static struct attribute *dme1737_pwm6_attr[] = { 16839b257714SJuerg Haefliger &sensor_dev_attr_pwm6.dev_attr.attr, 16849b257714SJuerg Haefliger &sensor_dev_attr_pwm6_freq.dev_attr.attr, 16859b257714SJuerg Haefliger &sensor_dev_attr_pwm6_enable.dev_attr.attr, 16869431996fSJuerg Haefliger NULL 16879431996fSJuerg Haefliger }; 16889431996fSJuerg Haefliger 16899431996fSJuerg Haefliger static const struct attribute_group dme1737_pwm_group[] = { 169073ce48f6SJuerg Haefliger { .attrs = dme1737_pwm1_attr }, 169173ce48f6SJuerg Haefliger { .attrs = dme1737_pwm2_attr }, 169273ce48f6SJuerg Haefliger { .attrs = dme1737_pwm3_attr }, 16939431996fSJuerg Haefliger { .attrs = NULL }, 169473ce48f6SJuerg Haefliger { .attrs = dme1737_pwm5_attr }, 169573ce48f6SJuerg Haefliger { .attrs = dme1737_pwm6_attr }, 16969431996fSJuerg Haefliger }; 16979431996fSJuerg Haefliger 1698549edb83SJuerg Haefliger /* The following struct holds misc PWM attributes, which are not available in 1699549edb83SJuerg Haefliger * all chips. Their creation depends on the chip type which is determined 1700549edb83SJuerg Haefliger * during module load. */ 1701549edb83SJuerg Haefliger static struct attribute *dme1737_pwm_misc_attr[] = { 1702549edb83SJuerg Haefliger &sensor_dev_attr_pwm1_auto_pwm_min.dev_attr.attr, 1703549edb83SJuerg Haefliger &sensor_dev_attr_pwm2_auto_pwm_min.dev_attr.attr, 1704549edb83SJuerg Haefliger &sensor_dev_attr_pwm3_auto_pwm_min.dev_attr.attr, 1705549edb83SJuerg Haefliger }; 1706549edb83SJuerg Haefliger 17079431996fSJuerg Haefliger /* The following structs hold the fan attributes, some of which are optional. 17089431996fSJuerg Haefliger * Their creation depends on the chip configuration which is determined during 17099431996fSJuerg Haefliger * module load. */ 171073ce48f6SJuerg Haefliger static struct attribute *dme1737_fan1_attr[] = { 17119b257714SJuerg Haefliger &sensor_dev_attr_fan1_input.dev_attr.attr, 17129b257714SJuerg Haefliger &sensor_dev_attr_fan1_min.dev_attr.attr, 17139b257714SJuerg Haefliger &sensor_dev_attr_fan1_alarm.dev_attr.attr, 17149b257714SJuerg Haefliger &sensor_dev_attr_fan1_type.dev_attr.attr, 17159431996fSJuerg Haefliger NULL 17169431996fSJuerg Haefliger }; 171773ce48f6SJuerg Haefliger static struct attribute *dme1737_fan2_attr[] = { 17189b257714SJuerg Haefliger &sensor_dev_attr_fan2_input.dev_attr.attr, 17199b257714SJuerg Haefliger &sensor_dev_attr_fan2_min.dev_attr.attr, 17209b257714SJuerg Haefliger &sensor_dev_attr_fan2_alarm.dev_attr.attr, 17219b257714SJuerg Haefliger &sensor_dev_attr_fan2_type.dev_attr.attr, 17229431996fSJuerg Haefliger NULL 17239431996fSJuerg Haefliger }; 172473ce48f6SJuerg Haefliger static struct attribute *dme1737_fan3_attr[] = { 17259b257714SJuerg Haefliger &sensor_dev_attr_fan3_input.dev_attr.attr, 17269b257714SJuerg Haefliger &sensor_dev_attr_fan3_min.dev_attr.attr, 17279b257714SJuerg Haefliger &sensor_dev_attr_fan3_alarm.dev_attr.attr, 17289b257714SJuerg Haefliger &sensor_dev_attr_fan3_type.dev_attr.attr, 17299431996fSJuerg Haefliger NULL 17309431996fSJuerg Haefliger }; 173173ce48f6SJuerg Haefliger static struct attribute *dme1737_fan4_attr[] = { 17329b257714SJuerg Haefliger &sensor_dev_attr_fan4_input.dev_attr.attr, 17339b257714SJuerg Haefliger &sensor_dev_attr_fan4_min.dev_attr.attr, 17349b257714SJuerg Haefliger &sensor_dev_attr_fan4_alarm.dev_attr.attr, 17359b257714SJuerg Haefliger &sensor_dev_attr_fan4_type.dev_attr.attr, 17369431996fSJuerg Haefliger NULL 17379431996fSJuerg Haefliger }; 173873ce48f6SJuerg Haefliger static struct attribute *dme1737_fan5_attr[] = { 17399b257714SJuerg Haefliger &sensor_dev_attr_fan5_input.dev_attr.attr, 17409b257714SJuerg Haefliger &sensor_dev_attr_fan5_min.dev_attr.attr, 17419b257714SJuerg Haefliger &sensor_dev_attr_fan5_alarm.dev_attr.attr, 17429b257714SJuerg Haefliger &sensor_dev_attr_fan5_max.dev_attr.attr, 17439431996fSJuerg Haefliger NULL 17449431996fSJuerg Haefliger }; 174573ce48f6SJuerg Haefliger static struct attribute *dme1737_fan6_attr[] = { 17469b257714SJuerg Haefliger &sensor_dev_attr_fan6_input.dev_attr.attr, 17479b257714SJuerg Haefliger &sensor_dev_attr_fan6_min.dev_attr.attr, 17489b257714SJuerg Haefliger &sensor_dev_attr_fan6_alarm.dev_attr.attr, 17499b257714SJuerg Haefliger &sensor_dev_attr_fan6_max.dev_attr.attr, 17509431996fSJuerg Haefliger NULL 17519431996fSJuerg Haefliger }; 17529431996fSJuerg Haefliger 17539431996fSJuerg Haefliger static const struct attribute_group dme1737_fan_group[] = { 175473ce48f6SJuerg Haefliger { .attrs = dme1737_fan1_attr }, 175573ce48f6SJuerg Haefliger { .attrs = dme1737_fan2_attr }, 175673ce48f6SJuerg Haefliger { .attrs = dme1737_fan3_attr }, 175773ce48f6SJuerg Haefliger { .attrs = dme1737_fan4_attr }, 175873ce48f6SJuerg Haefliger { .attrs = dme1737_fan5_attr }, 175973ce48f6SJuerg Haefliger { .attrs = dme1737_fan6_attr }, 17609431996fSJuerg Haefliger }; 17619431996fSJuerg Haefliger 1762549edb83SJuerg Haefliger /* The permissions of the following zone attributes are changed to read- 17639431996fSJuerg Haefliger * writeable if the chip is *not* locked. Otherwise they stay read-only. */ 1764549edb83SJuerg Haefliger static struct attribute *dme1737_zone_chmod_attr[] = { 17659b257714SJuerg Haefliger &sensor_dev_attr_zone1_auto_point1_temp.dev_attr.attr, 17669b257714SJuerg Haefliger &sensor_dev_attr_zone1_auto_point2_temp.dev_attr.attr, 17679b257714SJuerg Haefliger &sensor_dev_attr_zone1_auto_point3_temp.dev_attr.attr, 17689b257714SJuerg Haefliger &sensor_dev_attr_zone2_auto_point1_temp.dev_attr.attr, 17699b257714SJuerg Haefliger &sensor_dev_attr_zone2_auto_point2_temp.dev_attr.attr, 17709b257714SJuerg Haefliger &sensor_dev_attr_zone2_auto_point3_temp.dev_attr.attr, 17719b257714SJuerg Haefliger &sensor_dev_attr_zone3_auto_point1_temp.dev_attr.attr, 17729b257714SJuerg Haefliger &sensor_dev_attr_zone3_auto_point2_temp.dev_attr.attr, 17739b257714SJuerg Haefliger &sensor_dev_attr_zone3_auto_point3_temp.dev_attr.attr, 17749431996fSJuerg Haefliger NULL 17759431996fSJuerg Haefliger }; 17769431996fSJuerg Haefliger 1777549edb83SJuerg Haefliger static const struct attribute_group dme1737_zone_chmod_group = { 1778549edb83SJuerg Haefliger .attrs = dme1737_zone_chmod_attr, 17799431996fSJuerg Haefliger }; 17809431996fSJuerg Haefliger 17819431996fSJuerg Haefliger /* The permissions of the following PWM attributes are changed to read- 17829431996fSJuerg Haefliger * writeable if the chip is *not* locked and the respective PWM is available. 17839431996fSJuerg Haefliger * Otherwise they stay read-only. */ 178473ce48f6SJuerg Haefliger static struct attribute *dme1737_pwm1_chmod_attr[] = { 17859b257714SJuerg Haefliger &sensor_dev_attr_pwm1_freq.dev_attr.attr, 17869b257714SJuerg Haefliger &sensor_dev_attr_pwm1_enable.dev_attr.attr, 17879b257714SJuerg Haefliger &sensor_dev_attr_pwm1_ramp_rate.dev_attr.attr, 17889b257714SJuerg Haefliger &sensor_dev_attr_pwm1_auto_channels_zone.dev_attr.attr, 17899b257714SJuerg Haefliger &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr, 17909431996fSJuerg Haefliger NULL 17919431996fSJuerg Haefliger }; 179273ce48f6SJuerg Haefliger static struct attribute *dme1737_pwm2_chmod_attr[] = { 17939b257714SJuerg Haefliger &sensor_dev_attr_pwm2_freq.dev_attr.attr, 17949b257714SJuerg Haefliger &sensor_dev_attr_pwm2_enable.dev_attr.attr, 17959b257714SJuerg Haefliger &sensor_dev_attr_pwm2_ramp_rate.dev_attr.attr, 17969b257714SJuerg Haefliger &sensor_dev_attr_pwm2_auto_channels_zone.dev_attr.attr, 17979b257714SJuerg Haefliger &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr, 17989431996fSJuerg Haefliger NULL 17999431996fSJuerg Haefliger }; 180073ce48f6SJuerg Haefliger static struct attribute *dme1737_pwm3_chmod_attr[] = { 18019b257714SJuerg Haefliger &sensor_dev_attr_pwm3_freq.dev_attr.attr, 18029b257714SJuerg Haefliger &sensor_dev_attr_pwm3_enable.dev_attr.attr, 18039b257714SJuerg Haefliger &sensor_dev_attr_pwm3_ramp_rate.dev_attr.attr, 18049b257714SJuerg Haefliger &sensor_dev_attr_pwm3_auto_channels_zone.dev_attr.attr, 18059b257714SJuerg Haefliger &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr, 18069431996fSJuerg Haefliger NULL 18079431996fSJuerg Haefliger }; 180873ce48f6SJuerg Haefliger static struct attribute *dme1737_pwm5_chmod_attr[] = { 18099b257714SJuerg Haefliger &sensor_dev_attr_pwm5.dev_attr.attr, 18109b257714SJuerg Haefliger &sensor_dev_attr_pwm5_freq.dev_attr.attr, 18119431996fSJuerg Haefliger NULL 18129431996fSJuerg Haefliger }; 181373ce48f6SJuerg Haefliger static struct attribute *dme1737_pwm6_chmod_attr[] = { 18149b257714SJuerg Haefliger &sensor_dev_attr_pwm6.dev_attr.attr, 18159b257714SJuerg Haefliger &sensor_dev_attr_pwm6_freq.dev_attr.attr, 18169431996fSJuerg Haefliger NULL 18179431996fSJuerg Haefliger }; 18189431996fSJuerg Haefliger 181973ce48f6SJuerg Haefliger static const struct attribute_group dme1737_pwm_chmod_group[] = { 182073ce48f6SJuerg Haefliger { .attrs = dme1737_pwm1_chmod_attr }, 182173ce48f6SJuerg Haefliger { .attrs = dme1737_pwm2_chmod_attr }, 182273ce48f6SJuerg Haefliger { .attrs = dme1737_pwm3_chmod_attr }, 18239431996fSJuerg Haefliger { .attrs = NULL }, 182473ce48f6SJuerg Haefliger { .attrs = dme1737_pwm5_chmod_attr }, 182573ce48f6SJuerg Haefliger { .attrs = dme1737_pwm6_chmod_attr }, 18269431996fSJuerg Haefliger }; 18279431996fSJuerg Haefliger 18289431996fSJuerg Haefliger /* Pwm[1-3] are read-writeable if the associated pwm is in manual mode and the 18299431996fSJuerg Haefliger * chip is not locked. Otherwise they are read-only. */ 183073ce48f6SJuerg Haefliger static struct attribute *dme1737_pwm_chmod_attr[] = { 18319431996fSJuerg Haefliger &sensor_dev_attr_pwm1.dev_attr.attr, 18329431996fSJuerg Haefliger &sensor_dev_attr_pwm2.dev_attr.attr, 18339431996fSJuerg Haefliger &sensor_dev_attr_pwm3.dev_attr.attr, 18349431996fSJuerg Haefliger }; 18359431996fSJuerg Haefliger 18369431996fSJuerg Haefliger /* --------------------------------------------------------------------- 18379431996fSJuerg Haefliger * Super-IO functions 18389431996fSJuerg Haefliger * --------------------------------------------------------------------- */ 18399431996fSJuerg Haefliger 1840b237eb25SJuerg Haefliger static inline void dme1737_sio_enter(int sio_cip) 1841b237eb25SJuerg Haefliger { 1842b237eb25SJuerg Haefliger outb(0x55, sio_cip); 1843b237eb25SJuerg Haefliger } 1844b237eb25SJuerg Haefliger 1845b237eb25SJuerg Haefliger static inline void dme1737_sio_exit(int sio_cip) 1846b237eb25SJuerg Haefliger { 1847b237eb25SJuerg Haefliger outb(0xaa, sio_cip); 1848b237eb25SJuerg Haefliger } 1849b237eb25SJuerg Haefliger 18509431996fSJuerg Haefliger static inline int dme1737_sio_inb(int sio_cip, int reg) 18519431996fSJuerg Haefliger { 18529431996fSJuerg Haefliger outb(reg, sio_cip); 18539431996fSJuerg Haefliger return inb(sio_cip + 1); 18549431996fSJuerg Haefliger } 18559431996fSJuerg Haefliger 18569431996fSJuerg Haefliger static inline void dme1737_sio_outb(int sio_cip, int reg, int val) 18579431996fSJuerg Haefliger { 18589431996fSJuerg Haefliger outb(reg, sio_cip); 18599431996fSJuerg Haefliger outb(val, sio_cip + 1); 18609431996fSJuerg Haefliger } 18619431996fSJuerg Haefliger 18629431996fSJuerg Haefliger /* --------------------------------------------------------------------- 1863e95c237dSJuerg Haefliger * Device initialization 18649431996fSJuerg Haefliger * --------------------------------------------------------------------- */ 18659431996fSJuerg Haefliger 186667e2f328SJuerg Haefliger static int dme1737_i2c_get_features(int, struct dme1737_data*); 18679431996fSJuerg Haefliger 1868b237eb25SJuerg Haefliger static void dme1737_chmod_file(struct device *dev, 18699431996fSJuerg Haefliger struct attribute *attr, mode_t mode) 18709431996fSJuerg Haefliger { 1871b237eb25SJuerg Haefliger if (sysfs_chmod_file(&dev->kobj, attr, mode)) { 1872b237eb25SJuerg Haefliger dev_warn(dev, "Failed to change permissions of %s.\n", 18739431996fSJuerg Haefliger attr->name); 18749431996fSJuerg Haefliger } 18759431996fSJuerg Haefliger } 18769431996fSJuerg Haefliger 1877b237eb25SJuerg Haefliger static void dme1737_chmod_group(struct device *dev, 18789431996fSJuerg Haefliger const struct attribute_group *group, 18799431996fSJuerg Haefliger mode_t mode) 18809431996fSJuerg Haefliger { 18819431996fSJuerg Haefliger struct attribute **attr; 18829431996fSJuerg Haefliger 18839431996fSJuerg Haefliger for (attr = group->attrs; *attr; attr++) { 1884b237eb25SJuerg Haefliger dme1737_chmod_file(dev, *attr, mode); 18859431996fSJuerg Haefliger } 18869431996fSJuerg Haefliger } 18879431996fSJuerg Haefliger 1888b237eb25SJuerg Haefliger static void dme1737_remove_files(struct device *dev) 18899431996fSJuerg Haefliger { 1890b237eb25SJuerg Haefliger struct dme1737_data *data = dev_get_drvdata(dev); 1891b237eb25SJuerg Haefliger int ix; 1892b237eb25SJuerg Haefliger 1893b237eb25SJuerg Haefliger for (ix = 0; ix < ARRAY_SIZE(dme1737_fan_group); ix++) { 1894b237eb25SJuerg Haefliger if (data->has_fan & (1 << ix)) { 1895b237eb25SJuerg Haefliger sysfs_remove_group(&dev->kobj, 1896b237eb25SJuerg Haefliger &dme1737_fan_group[ix]); 1897b237eb25SJuerg Haefliger } 1898b237eb25SJuerg Haefliger } 1899b237eb25SJuerg Haefliger 1900b237eb25SJuerg Haefliger for (ix = 0; ix < ARRAY_SIZE(dme1737_pwm_group); ix++) { 1901b237eb25SJuerg Haefliger if (data->has_pwm & (1 << ix)) { 1902b237eb25SJuerg Haefliger sysfs_remove_group(&dev->kobj, 1903b237eb25SJuerg Haefliger &dme1737_pwm_group[ix]); 1904549edb83SJuerg Haefliger if (data->type != sch5027 && ix < 3) { 1905549edb83SJuerg Haefliger sysfs_remove_file(&dev->kobj, 1906549edb83SJuerg Haefliger dme1737_pwm_misc_attr[ix]); 1907b237eb25SJuerg Haefliger } 1908b237eb25SJuerg Haefliger } 1909549edb83SJuerg Haefliger } 1910549edb83SJuerg Haefliger 1911549edb83SJuerg Haefliger if (data->type != sch5027) { 1912549edb83SJuerg Haefliger sysfs_remove_group(&dev->kobj, &dme1737_misc_group); 1913549edb83SJuerg Haefliger } 19149d091446SJean Delvare if (data->type == dme1737) { 19159d091446SJean Delvare sysfs_remove_group(&dev->kobj, &dme1737_vid_group); 19169d091446SJean Delvare } 1917b237eb25SJuerg Haefliger 1918b237eb25SJuerg Haefliger sysfs_remove_group(&dev->kobj, &dme1737_group); 1919e95c237dSJuerg Haefliger 1920dbc2bc25SJean Delvare if (!data->client) { 1921e95c237dSJuerg Haefliger sysfs_remove_file(&dev->kobj, &dev_attr_name.attr); 1922e95c237dSJuerg Haefliger } 1923b237eb25SJuerg Haefliger } 1924b237eb25SJuerg Haefliger 1925b237eb25SJuerg Haefliger static int dme1737_create_files(struct device *dev) 1926b237eb25SJuerg Haefliger { 1927b237eb25SJuerg Haefliger struct dme1737_data *data = dev_get_drvdata(dev); 1928b237eb25SJuerg Haefliger int err, ix; 1929b237eb25SJuerg Haefliger 1930e95c237dSJuerg Haefliger /* Create a name attribute for ISA devices */ 1931dbc2bc25SJean Delvare if (!data->client && 1932e95c237dSJuerg Haefliger (err = sysfs_create_file(&dev->kobj, &dev_attr_name.attr))) { 1933e95c237dSJuerg Haefliger goto exit; 1934e95c237dSJuerg Haefliger } 1935e95c237dSJuerg Haefliger 1936b237eb25SJuerg Haefliger /* Create standard sysfs attributes */ 1937b237eb25SJuerg Haefliger if ((err = sysfs_create_group(&dev->kobj, &dme1737_group))) { 1938e95c237dSJuerg Haefliger goto exit_remove; 1939b237eb25SJuerg Haefliger } 1940b237eb25SJuerg Haefliger 1941549edb83SJuerg Haefliger /* Create misc sysfs attributes */ 1942549edb83SJuerg Haefliger if ((data->type != sch5027) && 1943549edb83SJuerg Haefliger (err = sysfs_create_group(&dev->kobj, 1944549edb83SJuerg Haefliger &dme1737_misc_group))) { 1945549edb83SJuerg Haefliger goto exit_remove; 1946549edb83SJuerg Haefliger } 1947549edb83SJuerg Haefliger 19489d091446SJean Delvare /* Create VID-related sysfs attributes */ 19499d091446SJean Delvare if ((data->type == dme1737) && 19509d091446SJean Delvare (err = sysfs_create_group(&dev->kobj, 19519d091446SJean Delvare &dme1737_vid_group))) { 19529d091446SJean Delvare goto exit_remove; 19539d091446SJean Delvare } 19549d091446SJean Delvare 1955b237eb25SJuerg Haefliger /* Create fan sysfs attributes */ 1956b237eb25SJuerg Haefliger for (ix = 0; ix < ARRAY_SIZE(dme1737_fan_group); ix++) { 1957b237eb25SJuerg Haefliger if (data->has_fan & (1 << ix)) { 1958b237eb25SJuerg Haefliger if ((err = sysfs_create_group(&dev->kobj, 1959b237eb25SJuerg Haefliger &dme1737_fan_group[ix]))) { 1960b237eb25SJuerg Haefliger goto exit_remove; 1961b237eb25SJuerg Haefliger } 1962b237eb25SJuerg Haefliger } 1963b237eb25SJuerg Haefliger } 1964b237eb25SJuerg Haefliger 1965b237eb25SJuerg Haefliger /* Create PWM sysfs attributes */ 1966b237eb25SJuerg Haefliger for (ix = 0; ix < ARRAY_SIZE(dme1737_pwm_group); ix++) { 1967b237eb25SJuerg Haefliger if (data->has_pwm & (1 << ix)) { 1968b237eb25SJuerg Haefliger if ((err = sysfs_create_group(&dev->kobj, 1969b237eb25SJuerg Haefliger &dme1737_pwm_group[ix]))) { 1970b237eb25SJuerg Haefliger goto exit_remove; 1971b237eb25SJuerg Haefliger } 1972549edb83SJuerg Haefliger if (data->type != sch5027 && ix < 3 && 1973549edb83SJuerg Haefliger (err = sysfs_create_file(&dev->kobj, 1974549edb83SJuerg Haefliger dme1737_pwm_misc_attr[ix]))) { 1975549edb83SJuerg Haefliger goto exit_remove; 1976549edb83SJuerg Haefliger } 1977b237eb25SJuerg Haefliger } 1978b237eb25SJuerg Haefliger } 1979b237eb25SJuerg Haefliger 1980b237eb25SJuerg Haefliger /* Inform if the device is locked. Otherwise change the permissions of 1981b237eb25SJuerg Haefliger * selected attributes from read-only to read-writeable. */ 1982b237eb25SJuerg Haefliger if (data->config & 0x02) { 1983b237eb25SJuerg Haefliger dev_info(dev, "Device is locked. Some attributes " 1984b237eb25SJuerg Haefliger "will be read-only.\n"); 1985b237eb25SJuerg Haefliger } else { 1986549edb83SJuerg Haefliger /* Change permissions of zone sysfs attributes */ 1987549edb83SJuerg Haefliger dme1737_chmod_group(dev, &dme1737_zone_chmod_group, 1988b237eb25SJuerg Haefliger S_IRUGO | S_IWUSR); 1989b237eb25SJuerg Haefliger 1990549edb83SJuerg Haefliger /* Change permissions of misc sysfs attributes */ 1991549edb83SJuerg Haefliger if (data->type != sch5027) { 1992549edb83SJuerg Haefliger dme1737_chmod_group(dev, &dme1737_misc_group, 1993549edb83SJuerg Haefliger S_IRUGO | S_IWUSR); 1994549edb83SJuerg Haefliger } 1995549edb83SJuerg Haefliger 199673ce48f6SJuerg Haefliger /* Change permissions of PWM sysfs attributes */ 199773ce48f6SJuerg Haefliger for (ix = 0; ix < ARRAY_SIZE(dme1737_pwm_chmod_group); ix++) { 1998b237eb25SJuerg Haefliger if (data->has_pwm & (1 << ix)) { 1999b237eb25SJuerg Haefliger dme1737_chmod_group(dev, 200073ce48f6SJuerg Haefliger &dme1737_pwm_chmod_group[ix], 2001b237eb25SJuerg Haefliger S_IRUGO | S_IWUSR); 2002549edb83SJuerg Haefliger if (data->type != sch5027 && ix < 3) { 2003549edb83SJuerg Haefliger dme1737_chmod_file(dev, 2004549edb83SJuerg Haefliger dme1737_pwm_misc_attr[ix], 2005549edb83SJuerg Haefliger S_IRUGO | S_IWUSR); 2006549edb83SJuerg Haefliger } 2007b237eb25SJuerg Haefliger } 2008b237eb25SJuerg Haefliger } 2009b237eb25SJuerg Haefliger 2010b237eb25SJuerg Haefliger /* Change permissions of pwm[1-3] if in manual mode */ 2011b237eb25SJuerg Haefliger for (ix = 0; ix < 3; ix++) { 2012b237eb25SJuerg Haefliger if ((data->has_pwm & (1 << ix)) && 2013b237eb25SJuerg Haefliger (PWM_EN_FROM_REG(data->pwm_config[ix]) == 1)) { 2014b237eb25SJuerg Haefliger dme1737_chmod_file(dev, 201573ce48f6SJuerg Haefliger dme1737_pwm_chmod_attr[ix], 2016b237eb25SJuerg Haefliger S_IRUGO | S_IWUSR); 2017b237eb25SJuerg Haefliger } 2018b237eb25SJuerg Haefliger } 2019b237eb25SJuerg Haefliger } 2020b237eb25SJuerg Haefliger 2021b237eb25SJuerg Haefliger return 0; 2022b237eb25SJuerg Haefliger 2023b237eb25SJuerg Haefliger exit_remove: 2024b237eb25SJuerg Haefliger dme1737_remove_files(dev); 2025b237eb25SJuerg Haefliger exit: 2026b237eb25SJuerg Haefliger return err; 2027b237eb25SJuerg Haefliger } 2028b237eb25SJuerg Haefliger 2029b237eb25SJuerg Haefliger static int dme1737_init_device(struct device *dev) 2030b237eb25SJuerg Haefliger { 2031b237eb25SJuerg Haefliger struct dme1737_data *data = dev_get_drvdata(dev); 2032dbc2bc25SJean Delvare struct i2c_client *client = data->client; 20339431996fSJuerg Haefliger int ix; 20349431996fSJuerg Haefliger u8 reg; 20359431996fSJuerg Haefliger 2036549edb83SJuerg Haefliger /* Point to the right nominal voltages array */ 2037549edb83SJuerg Haefliger data->in_nominal = IN_NOMINAL(data->type); 2038549edb83SJuerg Haefliger 2039dbc2bc25SJean Delvare data->config = dme1737_read(data, DME1737_REG_CONFIG); 20409431996fSJuerg Haefliger /* Inform if part is not monitoring/started */ 20419431996fSJuerg Haefliger if (!(data->config & 0x01)) { 20429431996fSJuerg Haefliger if (!force_start) { 2043b237eb25SJuerg Haefliger dev_err(dev, "Device is not monitoring. " 20449431996fSJuerg Haefliger "Use the force_start load parameter to " 20459431996fSJuerg Haefliger "override.\n"); 20469431996fSJuerg Haefliger return -EFAULT; 20479431996fSJuerg Haefliger } 20489431996fSJuerg Haefliger 20499431996fSJuerg Haefliger /* Force monitoring */ 20509431996fSJuerg Haefliger data->config |= 0x01; 2051dbc2bc25SJean Delvare dme1737_write(data, DME1737_REG_CONFIG, data->config); 20529431996fSJuerg Haefliger } 20539431996fSJuerg Haefliger /* Inform if part is not ready */ 20549431996fSJuerg Haefliger if (!(data->config & 0x04)) { 2055b237eb25SJuerg Haefliger dev_err(dev, "Device is not ready.\n"); 20569431996fSJuerg Haefliger return -EFAULT; 20579431996fSJuerg Haefliger } 20589431996fSJuerg Haefliger 2059e95c237dSJuerg Haefliger /* Determine which optional fan and pwm features are enabled/present */ 2060dbc2bc25SJean Delvare if (client) { /* I2C chip */ 2061dbc2bc25SJean Delvare data->config2 = dme1737_read(data, DME1737_REG_CONFIG2); 20629431996fSJuerg Haefliger /* Check if optional fan3 input is enabled */ 20639431996fSJuerg Haefliger if (data->config2 & 0x04) { 20649431996fSJuerg Haefliger data->has_fan |= (1 << 2); 20659431996fSJuerg Haefliger } 20669431996fSJuerg Haefliger 20679431996fSJuerg Haefliger /* Fan4 and pwm3 are only available if the client's I2C address 2068e95c237dSJuerg Haefliger * is the default 0x2e. Otherwise the I/Os associated with 2069e95c237dSJuerg Haefliger * these functions are used for addr enable/select. */ 2070dbc2bc25SJean Delvare if (client->addr == 0x2e) { 20719431996fSJuerg Haefliger data->has_fan |= (1 << 3); 20729431996fSJuerg Haefliger data->has_pwm |= (1 << 2); 20739431996fSJuerg Haefliger } 20749431996fSJuerg Haefliger 2075e95c237dSJuerg Haefliger /* Determine which of the optional fan[5-6] and pwm[5-6] 2076e95c237dSJuerg Haefliger * features are enabled. For this, we need to query the runtime 2077e95c237dSJuerg Haefliger * registers through the Super-IO LPC interface. Try both 2078e95c237dSJuerg Haefliger * config ports 0x2e and 0x4e. */ 2079b237eb25SJuerg Haefliger if (dme1737_i2c_get_features(0x2e, data) && 2080b237eb25SJuerg Haefliger dme1737_i2c_get_features(0x4e, data)) { 2081b237eb25SJuerg Haefliger dev_warn(dev, "Failed to query Super-IO for optional " 20829431996fSJuerg Haefliger "features.\n"); 20839431996fSJuerg Haefliger } 2084e95c237dSJuerg Haefliger } else { /* ISA chip */ 2085e95c237dSJuerg Haefliger /* Fan3 and pwm3 are always available. Fan[4-5] and pwm[5-6] 2086e95c237dSJuerg Haefliger * don't exist in the ISA chip. */ 2087e95c237dSJuerg Haefliger data->has_fan |= (1 << 2); 2088e95c237dSJuerg Haefliger data->has_pwm |= (1 << 2); 2089e95c237dSJuerg Haefliger } 20909431996fSJuerg Haefliger 20919431996fSJuerg Haefliger /* Fan1, fan2, pwm1, and pwm2 are always present */ 20929431996fSJuerg Haefliger data->has_fan |= 0x03; 20939431996fSJuerg Haefliger data->has_pwm |= 0x03; 20949431996fSJuerg Haefliger 2095b237eb25SJuerg Haefliger dev_info(dev, "Optional features: pwm3=%s, pwm5=%s, pwm6=%s, " 20969431996fSJuerg Haefliger "fan3=%s, fan4=%s, fan5=%s, fan6=%s.\n", 20979431996fSJuerg Haefliger (data->has_pwm & (1 << 2)) ? "yes" : "no", 20989431996fSJuerg Haefliger (data->has_pwm & (1 << 4)) ? "yes" : "no", 20999431996fSJuerg Haefliger (data->has_pwm & (1 << 5)) ? "yes" : "no", 21009431996fSJuerg Haefliger (data->has_fan & (1 << 2)) ? "yes" : "no", 21019431996fSJuerg Haefliger (data->has_fan & (1 << 3)) ? "yes" : "no", 21029431996fSJuerg Haefliger (data->has_fan & (1 << 4)) ? "yes" : "no", 21039431996fSJuerg Haefliger (data->has_fan & (1 << 5)) ? "yes" : "no"); 21049431996fSJuerg Haefliger 2105dbc2bc25SJean Delvare reg = dme1737_read(data, DME1737_REG_TACH_PWM); 21069431996fSJuerg Haefliger /* Inform if fan-to-pwm mapping differs from the default */ 2107dbc2bc25SJean Delvare if (client && reg != 0xa4) { /* I2C chip */ 2108b237eb25SJuerg Haefliger dev_warn(dev, "Non-standard fan to pwm mapping: " 21099431996fSJuerg Haefliger "fan1->pwm%d, fan2->pwm%d, fan3->pwm%d, " 21109431996fSJuerg Haefliger "fan4->pwm%d. Please report to the driver " 21119431996fSJuerg Haefliger "maintainer.\n", 21129431996fSJuerg Haefliger (reg & 0x03) + 1, ((reg >> 2) & 0x03) + 1, 21139431996fSJuerg Haefliger ((reg >> 4) & 0x03) + 1, ((reg >> 6) & 0x03) + 1); 2114dbc2bc25SJean Delvare } else if (!client && reg != 0x24) { /* ISA chip */ 2115e95c237dSJuerg Haefliger dev_warn(dev, "Non-standard fan to pwm mapping: " 2116e95c237dSJuerg Haefliger "fan1->pwm%d, fan2->pwm%d, fan3->pwm%d. " 2117e95c237dSJuerg Haefliger "Please report to the driver maintainer.\n", 2118e95c237dSJuerg Haefliger (reg & 0x03) + 1, ((reg >> 2) & 0x03) + 1, 2119e95c237dSJuerg Haefliger ((reg >> 4) & 0x03) + 1); 21209431996fSJuerg Haefliger } 21219431996fSJuerg Haefliger 21229431996fSJuerg Haefliger /* Switch pwm[1-3] to manual mode if they are currently disabled and 21239431996fSJuerg Haefliger * set the duty-cycles to 0% (which is identical to the PWMs being 21249431996fSJuerg Haefliger * disabled). */ 21259431996fSJuerg Haefliger if (!(data->config & 0x02)) { 21269431996fSJuerg Haefliger for (ix = 0; ix < 3; ix++) { 2127dbc2bc25SJean Delvare data->pwm_config[ix] = dme1737_read(data, 21289431996fSJuerg Haefliger DME1737_REG_PWM_CONFIG(ix)); 21299431996fSJuerg Haefliger if ((data->has_pwm & (1 << ix)) && 21309431996fSJuerg Haefliger (PWM_EN_FROM_REG(data->pwm_config[ix]) == -1)) { 2131b237eb25SJuerg Haefliger dev_info(dev, "Switching pwm%d to " 21329431996fSJuerg Haefliger "manual mode.\n", ix + 1); 21339431996fSJuerg Haefliger data->pwm_config[ix] = PWM_EN_TO_REG(1, 21349431996fSJuerg Haefliger data->pwm_config[ix]); 2135dbc2bc25SJean Delvare dme1737_write(data, DME1737_REG_PWM(ix), 0); 2136dbc2bc25SJean Delvare dme1737_write(data, 21379431996fSJuerg Haefliger DME1737_REG_PWM_CONFIG(ix), 21389431996fSJuerg Haefliger data->pwm_config[ix]); 21399431996fSJuerg Haefliger } 21409431996fSJuerg Haefliger } 21419431996fSJuerg Haefliger } 21429431996fSJuerg Haefliger 21439431996fSJuerg Haefliger /* Initialize the default PWM auto channels zone (acz) assignments */ 21449431996fSJuerg Haefliger data->pwm_acz[0] = 1; /* pwm1 -> zone1 */ 21459431996fSJuerg Haefliger data->pwm_acz[1] = 2; /* pwm2 -> zone2 */ 21469431996fSJuerg Haefliger data->pwm_acz[2] = 4; /* pwm3 -> zone3 */ 21479431996fSJuerg Haefliger 21489431996fSJuerg Haefliger /* Set VRM */ 21499d091446SJean Delvare if (data->type == dme1737) { 21509431996fSJuerg Haefliger data->vrm = vid_which_vrm(); 2151549edb83SJuerg Haefliger } 21529431996fSJuerg Haefliger 21539431996fSJuerg Haefliger return 0; 21549431996fSJuerg Haefliger } 21559431996fSJuerg Haefliger 215667e2f328SJuerg Haefliger /* --------------------------------------------------------------------- 215767e2f328SJuerg Haefliger * I2C device detection and registration 215867e2f328SJuerg Haefliger * --------------------------------------------------------------------- */ 215967e2f328SJuerg Haefliger 216067e2f328SJuerg Haefliger static struct i2c_driver dme1737_i2c_driver; 216167e2f328SJuerg Haefliger 216267e2f328SJuerg Haefliger static int dme1737_i2c_get_features(int sio_cip, struct dme1737_data *data) 216367e2f328SJuerg Haefliger { 21649431996fSJuerg Haefliger int err = 0, reg; 21659431996fSJuerg Haefliger u16 addr; 21669431996fSJuerg Haefliger 216767e2f328SJuerg Haefliger dme1737_sio_enter(sio_cip); 21689431996fSJuerg Haefliger 21699431996fSJuerg Haefliger /* Check device ID 2170549edb83SJuerg Haefliger * The DME1737 can return either 0x78 or 0x77 as its device ID. 2171549edb83SJuerg Haefliger * The SCH5027 returns 0x89 as its device ID. */ 2172345a2224SJuerg Haefliger reg = force_id ? force_id : dme1737_sio_inb(sio_cip, 0x20); 2173549edb83SJuerg Haefliger if (!(reg == 0x77 || reg == 0x78 || reg == 0x89)) { 21749431996fSJuerg Haefliger err = -ENODEV; 21759431996fSJuerg Haefliger goto exit; 21769431996fSJuerg Haefliger } 21779431996fSJuerg Haefliger 21789431996fSJuerg Haefliger /* Select logical device A (runtime registers) */ 21799431996fSJuerg Haefliger dme1737_sio_outb(sio_cip, 0x07, 0x0a); 21809431996fSJuerg Haefliger 21819431996fSJuerg Haefliger /* Get the base address of the runtime registers */ 21829431996fSJuerg Haefliger if (!(addr = (dme1737_sio_inb(sio_cip, 0x60) << 8) | 21839431996fSJuerg Haefliger dme1737_sio_inb(sio_cip, 0x61))) { 21849431996fSJuerg Haefliger err = -ENODEV; 21859431996fSJuerg Haefliger goto exit; 21869431996fSJuerg Haefliger } 21879431996fSJuerg Haefliger 21889431996fSJuerg Haefliger /* Read the runtime registers to determine which optional features 21899431996fSJuerg Haefliger * are enabled and available. Bits [3:2] of registers 0x43-0x46 are set 21909431996fSJuerg Haefliger * to '10' if the respective feature is enabled. */ 21919431996fSJuerg Haefliger if ((inb(addr + 0x43) & 0x0c) == 0x08) { /* fan6 */ 21929431996fSJuerg Haefliger data->has_fan |= (1 << 5); 21939431996fSJuerg Haefliger } 21949431996fSJuerg Haefliger if ((inb(addr + 0x44) & 0x0c) == 0x08) { /* pwm6 */ 21959431996fSJuerg Haefliger data->has_pwm |= (1 << 5); 21969431996fSJuerg Haefliger } 21979431996fSJuerg Haefliger if ((inb(addr + 0x45) & 0x0c) == 0x08) { /* fan5 */ 21989431996fSJuerg Haefliger data->has_fan |= (1 << 4); 21999431996fSJuerg Haefliger } 22009431996fSJuerg Haefliger if ((inb(addr + 0x46) & 0x0c) == 0x08) { /* pwm5 */ 22019431996fSJuerg Haefliger data->has_pwm |= (1 << 4); 22029431996fSJuerg Haefliger } 22039431996fSJuerg Haefliger 22049431996fSJuerg Haefliger exit: 220567e2f328SJuerg Haefliger dme1737_sio_exit(sio_cip); 22069431996fSJuerg Haefliger 22079431996fSJuerg Haefliger return err; 22089431996fSJuerg Haefliger } 22099431996fSJuerg Haefliger 221067a37308SJean Delvare /* Return 0 if detection is successful, -ENODEV otherwise */ 2211*310ec792SJean Delvare static int dme1737_i2c_detect(struct i2c_client *client, 221267a37308SJean Delvare struct i2c_board_info *info) 22139431996fSJuerg Haefliger { 221467a37308SJean Delvare struct i2c_adapter *adapter = client->adapter; 221567a37308SJean Delvare struct device *dev = &adapter->dev; 22169431996fSJuerg Haefliger u8 company, verstep = 0; 22179431996fSJuerg Haefliger const char *name; 22189431996fSJuerg Haefliger 22199431996fSJuerg Haefliger if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) { 222067a37308SJean Delvare return -ENODEV; 22219431996fSJuerg Haefliger } 22229431996fSJuerg Haefliger 222367a37308SJean Delvare company = i2c_smbus_read_byte_data(client, DME1737_REG_COMPANY); 222467a37308SJean Delvare verstep = i2c_smbus_read_byte_data(client, DME1737_REG_VERSTEP); 22259431996fSJuerg Haefliger 2226549edb83SJuerg Haefliger if (company == DME1737_COMPANY_SMSC && 2227549edb83SJuerg Haefliger verstep == SCH5027_VERSTEP) { 222852df6440SJean Delvare name = "sch5027"; 222952df6440SJean Delvare 223052df6440SJean Delvare } else if (company == DME1737_COMPANY_SMSC && 223152df6440SJean Delvare (verstep & DME1737_VERSTEP_MASK) == DME1737_VERSTEP) { 223252df6440SJean Delvare name = "dme1737"; 2233549edb83SJuerg Haefliger } else { 223467a37308SJean Delvare return -ENODEV; 22359431996fSJuerg Haefliger } 22369431996fSJuerg Haefliger 2237549edb83SJuerg Haefliger dev_info(dev, "Found a %s chip at 0x%02x (rev 0x%02x).\n", 223852df6440SJean Delvare verstep == SCH5027_VERSTEP ? "SCH5027" : "DME1737", 223952df6440SJean Delvare client->addr, verstep); 224067a37308SJean Delvare strlcpy(info->type, name, I2C_NAME_SIZE); 224167a37308SJean Delvare 224267a37308SJean Delvare return 0; 224367a37308SJean Delvare } 224467a37308SJean Delvare 224567a37308SJean Delvare static int dme1737_i2c_probe(struct i2c_client *client, 224667a37308SJean Delvare const struct i2c_device_id *id) 224767a37308SJean Delvare { 224867a37308SJean Delvare struct dme1737_data *data; 224967a37308SJean Delvare struct device *dev = &client->dev; 225067a37308SJean Delvare int err; 225167a37308SJean Delvare 225267a37308SJean Delvare data = kzalloc(sizeof(struct dme1737_data), GFP_KERNEL); 225367a37308SJean Delvare if (!data) { 225467a37308SJean Delvare err = -ENOMEM; 225567a37308SJean Delvare goto exit; 225667a37308SJean Delvare } 225767a37308SJean Delvare 225867a37308SJean Delvare i2c_set_clientdata(client, data); 225967a37308SJean Delvare data->type = id->driver_data; 226067a37308SJean Delvare data->client = client; 226167a37308SJean Delvare data->name = client->name; 226267a37308SJean Delvare mutex_init(&data->update_lock); 2263b237eb25SJuerg Haefliger 22649431996fSJuerg Haefliger /* Initialize the DME1737 chip */ 2265b237eb25SJuerg Haefliger if ((err = dme1737_init_device(dev))) { 2266b237eb25SJuerg Haefliger dev_err(dev, "Failed to initialize device.\n"); 226767a37308SJean Delvare goto exit_kfree; 22689431996fSJuerg Haefliger } 22699431996fSJuerg Haefliger 2270b237eb25SJuerg Haefliger /* Create sysfs files */ 2271b237eb25SJuerg Haefliger if ((err = dme1737_create_files(dev))) { 2272b237eb25SJuerg Haefliger dev_err(dev, "Failed to create sysfs files.\n"); 227367a37308SJean Delvare goto exit_kfree; 22749431996fSJuerg Haefliger } 22759431996fSJuerg Haefliger 22769431996fSJuerg Haefliger /* Register device */ 227762ee3e10SMark M. Hoffman data->hwmon_dev = hwmon_device_register(dev); 22781beeffe4STony Jones if (IS_ERR(data->hwmon_dev)) { 2279b237eb25SJuerg Haefliger dev_err(dev, "Failed to register device.\n"); 22801beeffe4STony Jones err = PTR_ERR(data->hwmon_dev); 22819431996fSJuerg Haefliger goto exit_remove; 22829431996fSJuerg Haefliger } 22839431996fSJuerg Haefliger 22849431996fSJuerg Haefliger return 0; 22859431996fSJuerg Haefliger 22869431996fSJuerg Haefliger exit_remove: 2287b237eb25SJuerg Haefliger dme1737_remove_files(dev); 22889431996fSJuerg Haefliger exit_kfree: 22899431996fSJuerg Haefliger kfree(data); 22909431996fSJuerg Haefliger exit: 22919431996fSJuerg Haefliger return err; 22929431996fSJuerg Haefliger } 22939431996fSJuerg Haefliger 229467a37308SJean Delvare static int dme1737_i2c_remove(struct i2c_client *client) 22959431996fSJuerg Haefliger { 22969431996fSJuerg Haefliger struct dme1737_data *data = i2c_get_clientdata(client); 22979431996fSJuerg Haefliger 22981beeffe4STony Jones hwmon_device_unregister(data->hwmon_dev); 2299b237eb25SJuerg Haefliger dme1737_remove_files(&client->dev); 23009431996fSJuerg Haefliger 23019431996fSJuerg Haefliger kfree(data); 23029431996fSJuerg Haefliger return 0; 23039431996fSJuerg Haefliger } 23049431996fSJuerg Haefliger 230567a37308SJean Delvare static const struct i2c_device_id dme1737_id[] = { 230667a37308SJean Delvare { "dme1737", dme1737 }, 230767a37308SJean Delvare { "sch5027", sch5027 }, 230867a37308SJean Delvare { } 230967a37308SJean Delvare }; 231067a37308SJean Delvare MODULE_DEVICE_TABLE(i2c, dme1737_id); 231167a37308SJean Delvare 2312b237eb25SJuerg Haefliger static struct i2c_driver dme1737_i2c_driver = { 231367a37308SJean Delvare .class = I2C_CLASS_HWMON, 23149431996fSJuerg Haefliger .driver = { 23159431996fSJuerg Haefliger .name = "dme1737", 23169431996fSJuerg Haefliger }, 231767a37308SJean Delvare .probe = dme1737_i2c_probe, 231867a37308SJean Delvare .remove = dme1737_i2c_remove, 231967a37308SJean Delvare .id_table = dme1737_id, 232067a37308SJean Delvare .detect = dme1737_i2c_detect, 232167a37308SJean Delvare .address_data = &addr_data, 23229431996fSJuerg Haefliger }; 23239431996fSJuerg Haefliger 232467e2f328SJuerg Haefliger /* --------------------------------------------------------------------- 2325e95c237dSJuerg Haefliger * ISA device detection and registration 2326e95c237dSJuerg Haefliger * --------------------------------------------------------------------- */ 2327e95c237dSJuerg Haefliger 2328e95c237dSJuerg Haefliger static int __init dme1737_isa_detect(int sio_cip, unsigned short *addr) 2329e95c237dSJuerg Haefliger { 2330e95c237dSJuerg Haefliger int err = 0, reg; 2331e95c237dSJuerg Haefliger unsigned short base_addr; 2332e95c237dSJuerg Haefliger 2333e95c237dSJuerg Haefliger dme1737_sio_enter(sio_cip); 2334e95c237dSJuerg Haefliger 2335e95c237dSJuerg Haefliger /* Check device ID 2336e95c237dSJuerg Haefliger * We currently know about SCH3112 (0x7c), SCH3114 (0x7d), and 2337e95c237dSJuerg Haefliger * SCH3116 (0x7f). */ 233867b671bcSJean Delvare reg = force_id ? force_id : dme1737_sio_inb(sio_cip, 0x20); 2339e95c237dSJuerg Haefliger if (!(reg == 0x7c || reg == 0x7d || reg == 0x7f)) { 2340e95c237dSJuerg Haefliger err = -ENODEV; 2341e95c237dSJuerg Haefliger goto exit; 2342e95c237dSJuerg Haefliger } 2343e95c237dSJuerg Haefliger 2344e95c237dSJuerg Haefliger /* Select logical device A (runtime registers) */ 2345e95c237dSJuerg Haefliger dme1737_sio_outb(sio_cip, 0x07, 0x0a); 2346e95c237dSJuerg Haefliger 2347e95c237dSJuerg Haefliger /* Get the base address of the runtime registers */ 2348e95c237dSJuerg Haefliger if (!(base_addr = (dme1737_sio_inb(sio_cip, 0x60) << 8) | 2349e95c237dSJuerg Haefliger dme1737_sio_inb(sio_cip, 0x61))) { 2350e95c237dSJuerg Haefliger printk(KERN_ERR "dme1737: Base address not set.\n"); 2351e95c237dSJuerg Haefliger err = -ENODEV; 2352e95c237dSJuerg Haefliger goto exit; 2353e95c237dSJuerg Haefliger } 2354e95c237dSJuerg Haefliger 2355e95c237dSJuerg Haefliger /* Access to the hwmon registers is through an index/data register 2356e95c237dSJuerg Haefliger * pair located at offset 0x70/0x71. */ 2357e95c237dSJuerg Haefliger *addr = base_addr + 0x70; 2358e95c237dSJuerg Haefliger 2359e95c237dSJuerg Haefliger exit: 2360e95c237dSJuerg Haefliger dme1737_sio_exit(sio_cip); 2361e95c237dSJuerg Haefliger return err; 2362e95c237dSJuerg Haefliger } 2363e95c237dSJuerg Haefliger 2364e95c237dSJuerg Haefliger static int __init dme1737_isa_device_add(unsigned short addr) 2365e95c237dSJuerg Haefliger { 2366e95c237dSJuerg Haefliger struct resource res = { 2367e95c237dSJuerg Haefliger .start = addr, 2368e95c237dSJuerg Haefliger .end = addr + DME1737_EXTENT - 1, 2369e95c237dSJuerg Haefliger .name = "dme1737", 2370e95c237dSJuerg Haefliger .flags = IORESOURCE_IO, 2371e95c237dSJuerg Haefliger }; 2372e95c237dSJuerg Haefliger int err; 2373e95c237dSJuerg Haefliger 2374b9acb64aSJean Delvare err = acpi_check_resource_conflict(&res); 2375b9acb64aSJean Delvare if (err) 2376b9acb64aSJean Delvare goto exit; 2377b9acb64aSJean Delvare 2378e95c237dSJuerg Haefliger if (!(pdev = platform_device_alloc("dme1737", addr))) { 2379e95c237dSJuerg Haefliger printk(KERN_ERR "dme1737: Failed to allocate device.\n"); 2380e95c237dSJuerg Haefliger err = -ENOMEM; 2381e95c237dSJuerg Haefliger goto exit; 2382e95c237dSJuerg Haefliger } 2383e95c237dSJuerg Haefliger 2384e95c237dSJuerg Haefliger if ((err = platform_device_add_resources(pdev, &res, 1))) { 2385e95c237dSJuerg Haefliger printk(KERN_ERR "dme1737: Failed to add device resource " 2386e95c237dSJuerg Haefliger "(err = %d).\n", err); 2387e95c237dSJuerg Haefliger goto exit_device_put; 2388e95c237dSJuerg Haefliger } 2389e95c237dSJuerg Haefliger 2390e95c237dSJuerg Haefliger if ((err = platform_device_add(pdev))) { 2391e95c237dSJuerg Haefliger printk(KERN_ERR "dme1737: Failed to add device (err = %d).\n", 2392e95c237dSJuerg Haefliger err); 2393e95c237dSJuerg Haefliger goto exit_device_put; 2394e95c237dSJuerg Haefliger } 2395e95c237dSJuerg Haefliger 2396e95c237dSJuerg Haefliger return 0; 2397e95c237dSJuerg Haefliger 2398e95c237dSJuerg Haefliger exit_device_put: 2399e95c237dSJuerg Haefliger platform_device_put(pdev); 2400e95c237dSJuerg Haefliger pdev = NULL; 2401e95c237dSJuerg Haefliger exit: 2402e95c237dSJuerg Haefliger return err; 2403e95c237dSJuerg Haefliger } 2404e95c237dSJuerg Haefliger 2405e95c237dSJuerg Haefliger static int __devinit dme1737_isa_probe(struct platform_device *pdev) 2406e95c237dSJuerg Haefliger { 2407e95c237dSJuerg Haefliger u8 company, device; 2408e95c237dSJuerg Haefliger struct resource *res; 2409e95c237dSJuerg Haefliger struct dme1737_data *data; 2410e95c237dSJuerg Haefliger struct device *dev = &pdev->dev; 2411e95c237dSJuerg Haefliger int err; 2412e95c237dSJuerg Haefliger 2413e95c237dSJuerg Haefliger res = platform_get_resource(pdev, IORESOURCE_IO, 0); 2414e95c237dSJuerg Haefliger if (!request_region(res->start, DME1737_EXTENT, "dme1737")) { 2415e95c237dSJuerg Haefliger dev_err(dev, "Failed to request region 0x%04x-0x%04x.\n", 2416e95c237dSJuerg Haefliger (unsigned short)res->start, 2417e95c237dSJuerg Haefliger (unsigned short)res->start + DME1737_EXTENT - 1); 2418e95c237dSJuerg Haefliger err = -EBUSY; 2419e95c237dSJuerg Haefliger goto exit; 2420e95c237dSJuerg Haefliger } 2421e95c237dSJuerg Haefliger 2422e95c237dSJuerg Haefliger if (!(data = kzalloc(sizeof(struct dme1737_data), GFP_KERNEL))) { 2423e95c237dSJuerg Haefliger err = -ENOMEM; 2424e95c237dSJuerg Haefliger goto exit_release_region; 2425e95c237dSJuerg Haefliger } 2426e95c237dSJuerg Haefliger 2427dbc2bc25SJean Delvare data->addr = res->start; 2428e95c237dSJuerg Haefliger platform_set_drvdata(pdev, data); 2429e95c237dSJuerg Haefliger 243055d68d75SJuerg Haefliger /* Skip chip detection if module is loaded with force_id parameter */ 243155d68d75SJuerg Haefliger if (!force_id) { 2432dbc2bc25SJean Delvare company = dme1737_read(data, DME1737_REG_COMPANY); 2433dbc2bc25SJean Delvare device = dme1737_read(data, DME1737_REG_DEVICE); 2434e95c237dSJuerg Haefliger 2435e95c237dSJuerg Haefliger if (!((company == DME1737_COMPANY_SMSC) && 2436e95c237dSJuerg Haefliger (device == SCH311X_DEVICE))) { 2437e95c237dSJuerg Haefliger err = -ENODEV; 2438e95c237dSJuerg Haefliger goto exit_kfree; 2439e95c237dSJuerg Haefliger } 244055d68d75SJuerg Haefliger } 2441549edb83SJuerg Haefliger data->type = sch311x; 2442e95c237dSJuerg Haefliger 2443e95c237dSJuerg Haefliger /* Fill in the remaining client fields and initialize the mutex */ 2444dbc2bc25SJean Delvare data->name = "sch311x"; 2445e95c237dSJuerg Haefliger mutex_init(&data->update_lock); 2446e95c237dSJuerg Haefliger 2447dbc2bc25SJean Delvare dev_info(dev, "Found a SCH311x chip at 0x%04x\n", data->addr); 2448e95c237dSJuerg Haefliger 2449e95c237dSJuerg Haefliger /* Initialize the chip */ 2450e95c237dSJuerg Haefliger if ((err = dme1737_init_device(dev))) { 2451e95c237dSJuerg Haefliger dev_err(dev, "Failed to initialize device.\n"); 2452e95c237dSJuerg Haefliger goto exit_kfree; 2453e95c237dSJuerg Haefliger } 2454e95c237dSJuerg Haefliger 2455e95c237dSJuerg Haefliger /* Create sysfs files */ 2456e95c237dSJuerg Haefliger if ((err = dme1737_create_files(dev))) { 2457e95c237dSJuerg Haefliger dev_err(dev, "Failed to create sysfs files.\n"); 2458e95c237dSJuerg Haefliger goto exit_kfree; 2459e95c237dSJuerg Haefliger } 2460e95c237dSJuerg Haefliger 2461e95c237dSJuerg Haefliger /* Register device */ 2462e95c237dSJuerg Haefliger data->hwmon_dev = hwmon_device_register(dev); 2463e95c237dSJuerg Haefliger if (IS_ERR(data->hwmon_dev)) { 2464e95c237dSJuerg Haefliger dev_err(dev, "Failed to register device.\n"); 2465e95c237dSJuerg Haefliger err = PTR_ERR(data->hwmon_dev); 2466e95c237dSJuerg Haefliger goto exit_remove_files; 2467e95c237dSJuerg Haefliger } 2468e95c237dSJuerg Haefliger 2469e95c237dSJuerg Haefliger return 0; 2470e95c237dSJuerg Haefliger 2471e95c237dSJuerg Haefliger exit_remove_files: 2472e95c237dSJuerg Haefliger dme1737_remove_files(dev); 2473e95c237dSJuerg Haefliger exit_kfree: 2474e95c237dSJuerg Haefliger platform_set_drvdata(pdev, NULL); 2475e95c237dSJuerg Haefliger kfree(data); 2476e95c237dSJuerg Haefliger exit_release_region: 2477e95c237dSJuerg Haefliger release_region(res->start, DME1737_EXTENT); 2478e95c237dSJuerg Haefliger exit: 2479e95c237dSJuerg Haefliger return err; 2480e95c237dSJuerg Haefliger } 2481e95c237dSJuerg Haefliger 2482e95c237dSJuerg Haefliger static int __devexit dme1737_isa_remove(struct platform_device *pdev) 2483e95c237dSJuerg Haefliger { 2484e95c237dSJuerg Haefliger struct dme1737_data *data = platform_get_drvdata(pdev); 2485e95c237dSJuerg Haefliger 2486e95c237dSJuerg Haefliger hwmon_device_unregister(data->hwmon_dev); 2487e95c237dSJuerg Haefliger dme1737_remove_files(&pdev->dev); 2488dbc2bc25SJean Delvare release_region(data->addr, DME1737_EXTENT); 2489e95c237dSJuerg Haefliger platform_set_drvdata(pdev, NULL); 2490e95c237dSJuerg Haefliger kfree(data); 2491e95c237dSJuerg Haefliger 2492e95c237dSJuerg Haefliger return 0; 2493e95c237dSJuerg Haefliger } 2494e95c237dSJuerg Haefliger 2495e95c237dSJuerg Haefliger static struct platform_driver dme1737_isa_driver = { 2496e95c237dSJuerg Haefliger .driver = { 2497e95c237dSJuerg Haefliger .owner = THIS_MODULE, 2498e95c237dSJuerg Haefliger .name = "dme1737", 2499e95c237dSJuerg Haefliger }, 2500e95c237dSJuerg Haefliger .probe = dme1737_isa_probe, 2501e95c237dSJuerg Haefliger .remove = __devexit_p(dme1737_isa_remove), 2502e95c237dSJuerg Haefliger }; 2503e95c237dSJuerg Haefliger 2504e95c237dSJuerg Haefliger /* --------------------------------------------------------------------- 250567e2f328SJuerg Haefliger * Module initialization and cleanup 250667e2f328SJuerg Haefliger * --------------------------------------------------------------------- */ 250767e2f328SJuerg Haefliger 25089431996fSJuerg Haefliger static int __init dme1737_init(void) 25099431996fSJuerg Haefliger { 2510e95c237dSJuerg Haefliger int err; 2511e95c237dSJuerg Haefliger unsigned short addr; 2512e95c237dSJuerg Haefliger 2513e95c237dSJuerg Haefliger if ((err = i2c_add_driver(&dme1737_i2c_driver))) { 2514e95c237dSJuerg Haefliger goto exit; 2515e95c237dSJuerg Haefliger } 2516e95c237dSJuerg Haefliger 2517e95c237dSJuerg Haefliger if (dme1737_isa_detect(0x2e, &addr) && 251892430b6fSJuerg Haefliger dme1737_isa_detect(0x4e, &addr) && 251992430b6fSJuerg Haefliger (!probe_all_addr || 252092430b6fSJuerg Haefliger (dme1737_isa_detect(0x162e, &addr) && 252192430b6fSJuerg Haefliger dme1737_isa_detect(0x164e, &addr)))) { 2522e95c237dSJuerg Haefliger /* Return 0 if we didn't find an ISA device */ 2523e95c237dSJuerg Haefliger return 0; 2524e95c237dSJuerg Haefliger } 2525e95c237dSJuerg Haefliger 2526e95c237dSJuerg Haefliger if ((err = platform_driver_register(&dme1737_isa_driver))) { 2527e95c237dSJuerg Haefliger goto exit_del_i2c_driver; 2528e95c237dSJuerg Haefliger } 2529e95c237dSJuerg Haefliger 2530e95c237dSJuerg Haefliger /* Sets global pdev as a side effect */ 2531e95c237dSJuerg Haefliger if ((err = dme1737_isa_device_add(addr))) { 2532e95c237dSJuerg Haefliger goto exit_del_isa_driver; 2533e95c237dSJuerg Haefliger } 2534e95c237dSJuerg Haefliger 2535e95c237dSJuerg Haefliger return 0; 2536e95c237dSJuerg Haefliger 2537e95c237dSJuerg Haefliger exit_del_isa_driver: 2538e95c237dSJuerg Haefliger platform_driver_unregister(&dme1737_isa_driver); 2539e95c237dSJuerg Haefliger exit_del_i2c_driver: 2540e95c237dSJuerg Haefliger i2c_del_driver(&dme1737_i2c_driver); 2541e95c237dSJuerg Haefliger exit: 2542e95c237dSJuerg Haefliger return err; 25439431996fSJuerg Haefliger } 25449431996fSJuerg Haefliger 25459431996fSJuerg Haefliger static void __exit dme1737_exit(void) 25469431996fSJuerg Haefliger { 2547e95c237dSJuerg Haefliger if (pdev) { 2548e95c237dSJuerg Haefliger platform_device_unregister(pdev); 2549e95c237dSJuerg Haefliger platform_driver_unregister(&dme1737_isa_driver); 2550e95c237dSJuerg Haefliger } 2551e95c237dSJuerg Haefliger 2552b237eb25SJuerg Haefliger i2c_del_driver(&dme1737_i2c_driver); 25539431996fSJuerg Haefliger } 25549431996fSJuerg Haefliger 25559431996fSJuerg Haefliger MODULE_AUTHOR("Juerg Haefliger <juergh@gmail.com>"); 25569431996fSJuerg Haefliger MODULE_DESCRIPTION("DME1737 sensors"); 25579431996fSJuerg Haefliger MODULE_LICENSE("GPL"); 25589431996fSJuerg Haefliger 25599431996fSJuerg Haefliger module_init(dme1737_init); 25609431996fSJuerg Haefliger module_exit(dme1737_exit); 2561