Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1 | /* |
Juerg Haefliger | e95c237 | 2007-10-07 21:27:35 -0700 | [diff] [blame] | 2 | * dme1737.c - Driver for the SMSC DME1737, Asus A8000, and SMSC SCH311x |
| 3 | * Super-I/O chips integrated hardware monitoring features. |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 4 | * Copyright (c) 2007 Juerg Haefliger <juergh@gmail.com> |
| 5 | * |
Juerg Haefliger | e95c237 | 2007-10-07 21:27:35 -0700 | [diff] [blame] | 6 | * This driver is an I2C/ISA hybrid, meaning that it uses the I2C bus to access |
| 7 | * the chip registers if a DME1737 (or A8000) is found and the ISA bus if a |
| 8 | * SCH311x chip is found. Both types of chips have very similar hardware |
| 9 | * monitoring capabilities but differ in the way they can be accessed. |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 2 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License |
| 22 | * along with this program; if not, write to the Free Software |
| 23 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 24 | */ |
| 25 | |
| 26 | #include <linux/module.h> |
| 27 | #include <linux/init.h> |
| 28 | #include <linux/slab.h> |
| 29 | #include <linux/jiffies.h> |
| 30 | #include <linux/i2c.h> |
Juerg Haefliger | e95c237 | 2007-10-07 21:27:35 -0700 | [diff] [blame] | 31 | #include <linux/platform_device.h> |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 32 | #include <linux/hwmon.h> |
| 33 | #include <linux/hwmon-sysfs.h> |
| 34 | #include <linux/hwmon-vid.h> |
| 35 | #include <linux/err.h> |
| 36 | #include <linux/mutex.h> |
| 37 | #include <asm/io.h> |
| 38 | |
Juerg Haefliger | e95c237 | 2007-10-07 21:27:35 -0700 | [diff] [blame] | 39 | /* ISA device, if found */ |
| 40 | static struct platform_device *pdev; |
| 41 | |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 42 | /* Module load parameters */ |
| 43 | static int force_start; |
| 44 | module_param(force_start, bool, 0); |
| 45 | MODULE_PARM_DESC(force_start, "Force the chip to start monitoring inputs"); |
| 46 | |
Jean Delvare | 67b671b | 2007-12-06 23:13:42 +0100 | [diff] [blame] | 47 | static unsigned short force_id; |
| 48 | module_param(force_id, ushort, 0); |
| 49 | MODULE_PARM_DESC(force_id, "Override the detected device ID"); |
| 50 | |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 51 | /* Addresses to scan */ |
Mark M. Hoffman | 25e9c86 | 2008-02-17 22:28:03 -0500 | [diff] [blame] | 52 | static const unsigned short normal_i2c[] = {0x2c, 0x2d, 0x2e, I2C_CLIENT_END}; |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 53 | |
| 54 | /* Insmod parameters */ |
| 55 | I2C_CLIENT_INSMOD_1(dme1737); |
| 56 | |
| 57 | /* --------------------------------------------------------------------- |
| 58 | * Registers |
| 59 | * |
| 60 | * The sensors are defined as follows: |
| 61 | * |
| 62 | * Voltages Temperatures |
| 63 | * -------- ------------ |
| 64 | * in0 +5VTR (+5V stdby) temp1 Remote diode 1 |
| 65 | * in1 Vccp (proc core) temp2 Internal temp |
| 66 | * in2 VCC (internal +3.3V) temp3 Remote diode 2 |
| 67 | * in3 +5V |
| 68 | * in4 +12V |
| 69 | * in5 VTR (+3.3V stby) |
| 70 | * in6 Vbat |
| 71 | * |
| 72 | * --------------------------------------------------------------------- */ |
| 73 | |
| 74 | /* Voltages (in) numbered 0-6 (ix) */ |
| 75 | #define DME1737_REG_IN(ix) ((ix) < 5 ? 0x20 + (ix) \ |
| 76 | : 0x94 + (ix)) |
| 77 | #define DME1737_REG_IN_MIN(ix) ((ix) < 5 ? 0x44 + (ix) * 2 \ |
| 78 | : 0x91 + (ix) * 2) |
| 79 | #define DME1737_REG_IN_MAX(ix) ((ix) < 5 ? 0x45 + (ix) * 2 \ |
| 80 | : 0x92 + (ix) * 2) |
| 81 | |
| 82 | /* Temperatures (temp) numbered 0-2 (ix) */ |
| 83 | #define DME1737_REG_TEMP(ix) (0x25 + (ix)) |
| 84 | #define DME1737_REG_TEMP_MIN(ix) (0x4e + (ix) * 2) |
| 85 | #define DME1737_REG_TEMP_MAX(ix) (0x4f + (ix) * 2) |
| 86 | #define DME1737_REG_TEMP_OFFSET(ix) ((ix) == 0 ? 0x1f \ |
| 87 | : 0x1c + (ix)) |
| 88 | |
| 89 | /* Voltage and temperature LSBs |
| 90 | * The LSBs (4 bits each) are stored in 5 registers with the following layouts: |
| 91 | * IN_TEMP_LSB(0) = [in5, in6] |
| 92 | * IN_TEMP_LSB(1) = [temp3, temp1] |
| 93 | * IN_TEMP_LSB(2) = [in4, temp2] |
| 94 | * IN_TEMP_LSB(3) = [in3, in0] |
| 95 | * IN_TEMP_LSB(4) = [in2, in1] */ |
| 96 | #define DME1737_REG_IN_TEMP_LSB(ix) (0x84 + (ix)) |
| 97 | static const u8 DME1737_REG_IN_LSB[] = {3, 4, 4, 3, 2, 0, 0}; |
| 98 | static const u8 DME1737_REG_IN_LSB_SHL[] = {4, 4, 0, 0, 0, 0, 4}; |
| 99 | static const u8 DME1737_REG_TEMP_LSB[] = {1, 2, 1}; |
| 100 | static const u8 DME1737_REG_TEMP_LSB_SHL[] = {4, 4, 0}; |
| 101 | |
| 102 | /* Fans numbered 0-5 (ix) */ |
| 103 | #define DME1737_REG_FAN(ix) ((ix) < 4 ? 0x28 + (ix) * 2 \ |
| 104 | : 0xa1 + (ix) * 2) |
| 105 | #define DME1737_REG_FAN_MIN(ix) ((ix) < 4 ? 0x54 + (ix) * 2 \ |
| 106 | : 0xa5 + (ix) * 2) |
| 107 | #define DME1737_REG_FAN_OPT(ix) ((ix) < 4 ? 0x90 + (ix) \ |
| 108 | : 0xb2 + (ix)) |
| 109 | #define DME1737_REG_FAN_MAX(ix) (0xb4 + (ix)) /* only for fan[4-5] */ |
| 110 | |
| 111 | /* PWMs numbered 0-2, 4-5 (ix) */ |
| 112 | #define DME1737_REG_PWM(ix) ((ix) < 3 ? 0x30 + (ix) \ |
| 113 | : 0xa1 + (ix)) |
| 114 | #define DME1737_REG_PWM_CONFIG(ix) (0x5c + (ix)) /* only for pwm[0-2] */ |
| 115 | #define DME1737_REG_PWM_MIN(ix) (0x64 + (ix)) /* only for pwm[0-2] */ |
| 116 | #define DME1737_REG_PWM_FREQ(ix) ((ix) < 3 ? 0x5f + (ix) \ |
| 117 | : 0xa3 + (ix)) |
| 118 | /* The layout of the ramp rate registers is different from the other pwm |
| 119 | * registers. The bits for the 3 PWMs are stored in 2 registers: |
| 120 | * PWM_RR(0) = [OFF3, OFF2, OFF1, RES, RR1E, RR1-2, RR1-1, RR1-0] |
| 121 | * PWM_RR(1) = [RR2E, RR2-2, RR2-1, RR2-0, RR3E, RR3-2, RR3-1, RR3-0] */ |
| 122 | #define DME1737_REG_PWM_RR(ix) (0x62 + (ix)) /* only for pwm[0-2] */ |
| 123 | |
| 124 | /* Thermal zones 0-2 */ |
| 125 | #define DME1737_REG_ZONE_LOW(ix) (0x67 + (ix)) |
| 126 | #define DME1737_REG_ZONE_ABS(ix) (0x6a + (ix)) |
| 127 | /* The layout of the hysteresis registers is different from the other zone |
| 128 | * registers. The bits for the 3 zones are stored in 2 registers: |
| 129 | * ZONE_HYST(0) = [H1-3, H1-2, H1-1, H1-0, H2-3, H2-2, H2-1, H2-0] |
| 130 | * ZONE_HYST(1) = [H3-3, H3-2, H3-1, H3-0, RES, RES, RES, RES] */ |
| 131 | #define DME1737_REG_ZONE_HYST(ix) (0x6d + (ix)) |
| 132 | |
| 133 | /* Alarm registers and bit mapping |
| 134 | * The 3 8-bit alarm registers will be concatenated to a single 32-bit |
| 135 | * alarm value [0, ALARM3, ALARM2, ALARM1]. */ |
| 136 | #define DME1737_REG_ALARM1 0x41 |
| 137 | #define DME1737_REG_ALARM2 0x42 |
| 138 | #define DME1737_REG_ALARM3 0x83 |
| 139 | static const u8 DME1737_BIT_ALARM_IN[] = {0, 1, 2, 3, 8, 16, 17}; |
| 140 | static const u8 DME1737_BIT_ALARM_TEMP[] = {4, 5, 6}; |
| 141 | static const u8 DME1737_BIT_ALARM_FAN[] = {10, 11, 12, 13, 22, 23}; |
| 142 | |
| 143 | /* Miscellaneous registers */ |
Juerg Haefliger | e95c237 | 2007-10-07 21:27:35 -0700 | [diff] [blame] | 144 | #define DME1737_REG_DEVICE 0x3d |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 145 | #define DME1737_REG_COMPANY 0x3e |
| 146 | #define DME1737_REG_VERSTEP 0x3f |
| 147 | #define DME1737_REG_CONFIG 0x40 |
| 148 | #define DME1737_REG_CONFIG2 0x7f |
| 149 | #define DME1737_REG_VID 0x43 |
| 150 | #define DME1737_REG_TACH_PWM 0x81 |
| 151 | |
| 152 | /* --------------------------------------------------------------------- |
| 153 | * Misc defines |
| 154 | * --------------------------------------------------------------------- */ |
| 155 | |
| 156 | /* Chip identification */ |
| 157 | #define DME1737_COMPANY_SMSC 0x5c |
| 158 | #define DME1737_VERSTEP 0x88 |
| 159 | #define DME1737_VERSTEP_MASK 0xf8 |
Juerg Haefliger | e95c237 | 2007-10-07 21:27:35 -0700 | [diff] [blame] | 160 | #define SCH311X_DEVICE 0x8c |
| 161 | |
| 162 | /* Length of ISA address segment */ |
| 163 | #define DME1737_EXTENT 2 |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 164 | |
| 165 | /* --------------------------------------------------------------------- |
| 166 | * Data structures and manipulation thereof |
| 167 | * --------------------------------------------------------------------- */ |
| 168 | |
Juerg Haefliger | e95c237 | 2007-10-07 21:27:35 -0700 | [diff] [blame] | 169 | /* For ISA chips, we abuse the i2c_client addr and name fields. We also use |
| 170 | the driver field to differentiate between I2C and ISA chips. */ |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 171 | struct dme1737_data { |
| 172 | struct i2c_client client; |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 173 | struct device *hwmon_dev; |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 174 | |
| 175 | struct mutex update_lock; |
| 176 | int valid; /* !=0 if following fields are valid */ |
| 177 | unsigned long last_update; /* in jiffies */ |
| 178 | unsigned long last_vbat; /* in jiffies */ |
| 179 | |
| 180 | u8 vid; |
| 181 | u8 pwm_rr_en; |
| 182 | u8 has_pwm; |
| 183 | u8 has_fan; |
| 184 | |
| 185 | /* Register values */ |
| 186 | u16 in[7]; |
| 187 | u8 in_min[7]; |
| 188 | u8 in_max[7]; |
| 189 | s16 temp[3]; |
| 190 | s8 temp_min[3]; |
| 191 | s8 temp_max[3]; |
| 192 | s8 temp_offset[3]; |
| 193 | u8 config; |
| 194 | u8 config2; |
| 195 | u8 vrm; |
| 196 | u16 fan[6]; |
| 197 | u16 fan_min[6]; |
| 198 | u8 fan_max[2]; |
| 199 | u8 fan_opt[6]; |
| 200 | u8 pwm[6]; |
| 201 | u8 pwm_min[3]; |
| 202 | u8 pwm_config[3]; |
| 203 | u8 pwm_acz[3]; |
| 204 | u8 pwm_freq[6]; |
| 205 | u8 pwm_rr[2]; |
| 206 | u8 zone_low[3]; |
| 207 | u8 zone_abs[3]; |
| 208 | u8 zone_hyst[2]; |
| 209 | u32 alarms; |
| 210 | }; |
| 211 | |
| 212 | /* Nominal voltage values */ |
| 213 | static const int IN_NOMINAL[] = {5000, 2250, 3300, 5000, 12000, 3300, 3300}; |
| 214 | |
| 215 | /* Voltage input |
| 216 | * Voltage inputs have 16 bits resolution, limit values have 8 bits |
| 217 | * resolution. */ |
| 218 | static inline int IN_FROM_REG(int reg, int ix, int res) |
| 219 | { |
| 220 | return (reg * IN_NOMINAL[ix] + (3 << (res - 3))) / (3 << (res - 2)); |
| 221 | } |
| 222 | |
| 223 | static inline int IN_TO_REG(int val, int ix) |
| 224 | { |
| 225 | return SENSORS_LIMIT((val * 192 + IN_NOMINAL[ix] / 2) / |
| 226 | IN_NOMINAL[ix], 0, 255); |
| 227 | } |
| 228 | |
| 229 | /* Temperature input |
| 230 | * The register values represent temperatures in 2's complement notation from |
| 231 | * -127 degrees C to +127 degrees C. Temp inputs have 16 bits resolution, limit |
| 232 | * values have 8 bits resolution. */ |
| 233 | static inline int TEMP_FROM_REG(int reg, int res) |
| 234 | { |
| 235 | return (reg * 1000) >> (res - 8); |
| 236 | } |
| 237 | |
| 238 | static inline int TEMP_TO_REG(int val) |
| 239 | { |
| 240 | return SENSORS_LIMIT((val < 0 ? val - 500 : val + 500) / 1000, |
| 241 | -128, 127); |
| 242 | } |
| 243 | |
| 244 | /* Temperature range */ |
| 245 | static const int TEMP_RANGE[] = {2000, 2500, 3333, 4000, 5000, 6666, 8000, |
| 246 | 10000, 13333, 16000, 20000, 26666, 32000, |
| 247 | 40000, 53333, 80000}; |
| 248 | |
| 249 | static inline int TEMP_RANGE_FROM_REG(int reg) |
| 250 | { |
| 251 | return TEMP_RANGE[(reg >> 4) & 0x0f]; |
| 252 | } |
| 253 | |
| 254 | static int TEMP_RANGE_TO_REG(int val, int reg) |
| 255 | { |
| 256 | int i; |
| 257 | |
| 258 | for (i = 15; i > 0; i--) { |
| 259 | if (val > (TEMP_RANGE[i] + TEMP_RANGE[i - 1] + 1) / 2) { |
| 260 | break; |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | return (reg & 0x0f) | (i << 4); |
| 265 | } |
| 266 | |
| 267 | /* Temperature hysteresis |
| 268 | * Register layout: |
| 269 | * reg[0] = [H1-3, H1-2, H1-1, H1-0, H2-3, H2-2, H2-1, H2-0] |
| 270 | * reg[1] = [H3-3, H3-2, H3-1, H3-0, xxxx, xxxx, xxxx, xxxx] */ |
| 271 | static inline int TEMP_HYST_FROM_REG(int reg, int ix) |
| 272 | { |
| 273 | return (((ix == 1) ? reg : reg >> 4) & 0x0f) * 1000; |
| 274 | } |
| 275 | |
| 276 | static inline int TEMP_HYST_TO_REG(int val, int ix, int reg) |
| 277 | { |
| 278 | int hyst = SENSORS_LIMIT((val + 500) / 1000, 0, 15); |
| 279 | |
| 280 | return (ix == 1) ? (reg & 0xf0) | hyst : (reg & 0x0f) | (hyst << 4); |
| 281 | } |
| 282 | |
| 283 | /* Fan input RPM */ |
| 284 | static inline int FAN_FROM_REG(int reg, int tpc) |
| 285 | { |
Juerg Haefliger | ff8421f | 2008-01-27 16:39:46 -0800 | [diff] [blame] | 286 | if (tpc) { |
| 287 | return tpc * reg; |
| 288 | } else { |
| 289 | return (reg == 0 || reg == 0xffff) ? 0 : 90000 * 60 / reg; |
| 290 | } |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | static inline int FAN_TO_REG(int val, int tpc) |
| 294 | { |
Juerg Haefliger | ff8421f | 2008-01-27 16:39:46 -0800 | [diff] [blame] | 295 | if (tpc) { |
| 296 | return SENSORS_LIMIT(val / tpc, 0, 0xffff); |
| 297 | } else { |
| 298 | return (val <= 0) ? 0xffff : |
| 299 | SENSORS_LIMIT(90000 * 60 / val, 0, 0xfffe); |
| 300 | } |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | /* Fan TPC (tach pulse count) |
| 304 | * Converts a register value to a TPC multiplier or returns 0 if the tachometer |
| 305 | * is configured in legacy (non-tpc) mode */ |
| 306 | static inline int FAN_TPC_FROM_REG(int reg) |
| 307 | { |
| 308 | return (reg & 0x20) ? 0 : 60 >> (reg & 0x03); |
| 309 | } |
| 310 | |
| 311 | /* Fan type |
| 312 | * The type of a fan is expressed in number of pulses-per-revolution that it |
| 313 | * emits */ |
| 314 | static inline int FAN_TYPE_FROM_REG(int reg) |
| 315 | { |
| 316 | int edge = (reg >> 1) & 0x03; |
| 317 | |
| 318 | return (edge > 0) ? 1 << (edge - 1) : 0; |
| 319 | } |
| 320 | |
| 321 | static inline int FAN_TYPE_TO_REG(int val, int reg) |
| 322 | { |
| 323 | int edge = (val == 4) ? 3 : val; |
| 324 | |
| 325 | return (reg & 0xf9) | (edge << 1); |
| 326 | } |
| 327 | |
| 328 | /* Fan max RPM */ |
| 329 | static const int FAN_MAX[] = {0x54, 0x38, 0x2a, 0x21, 0x1c, 0x18, 0x15, 0x12, |
| 330 | 0x11, 0x0f, 0x0e}; |
| 331 | |
| 332 | static int FAN_MAX_FROM_REG(int reg) |
| 333 | { |
| 334 | int i; |
| 335 | |
| 336 | for (i = 10; i > 0; i--) { |
| 337 | if (reg == FAN_MAX[i]) { |
| 338 | break; |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | return 1000 + i * 500; |
| 343 | } |
| 344 | |
| 345 | static int FAN_MAX_TO_REG(int val) |
| 346 | { |
| 347 | int i; |
| 348 | |
| 349 | for (i = 10; i > 0; i--) { |
| 350 | if (val > (1000 + (i - 1) * 500)) { |
| 351 | break; |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | return FAN_MAX[i]; |
| 356 | } |
| 357 | |
| 358 | /* PWM enable |
| 359 | * Register to enable mapping: |
| 360 | * 000: 2 fan on zone 1 auto |
| 361 | * 001: 2 fan on zone 2 auto |
| 362 | * 010: 2 fan on zone 3 auto |
| 363 | * 011: 0 fan full on |
| 364 | * 100: -1 fan disabled |
| 365 | * 101: 2 fan on hottest of zones 2,3 auto |
| 366 | * 110: 2 fan on hottest of zones 1,2,3 auto |
| 367 | * 111: 1 fan in manual mode */ |
| 368 | static inline int PWM_EN_FROM_REG(int reg) |
| 369 | { |
| 370 | static const int en[] = {2, 2, 2, 0, -1, 2, 2, 1}; |
| 371 | |
| 372 | return en[(reg >> 5) & 0x07]; |
| 373 | } |
| 374 | |
| 375 | static inline int PWM_EN_TO_REG(int val, int reg) |
| 376 | { |
| 377 | int en = (val == 1) ? 7 : 3; |
| 378 | |
| 379 | return (reg & 0x1f) | ((en & 0x07) << 5); |
| 380 | } |
| 381 | |
| 382 | /* PWM auto channels zone |
| 383 | * Register to auto channels zone mapping (ACZ is a bitfield with bit x |
| 384 | * corresponding to zone x+1): |
| 385 | * 000: 001 fan on zone 1 auto |
| 386 | * 001: 010 fan on zone 2 auto |
| 387 | * 010: 100 fan on zone 3 auto |
| 388 | * 011: 000 fan full on |
| 389 | * 100: 000 fan disabled |
| 390 | * 101: 110 fan on hottest of zones 2,3 auto |
| 391 | * 110: 111 fan on hottest of zones 1,2,3 auto |
| 392 | * 111: 000 fan in manual mode */ |
| 393 | static inline int PWM_ACZ_FROM_REG(int reg) |
| 394 | { |
| 395 | static const int acz[] = {1, 2, 4, 0, 0, 6, 7, 0}; |
| 396 | |
| 397 | return acz[(reg >> 5) & 0x07]; |
| 398 | } |
| 399 | |
| 400 | static inline int PWM_ACZ_TO_REG(int val, int reg) |
| 401 | { |
| 402 | int acz = (val == 4) ? 2 : val - 1; |
| 403 | |
| 404 | return (reg & 0x1f) | ((acz & 0x07) << 5); |
| 405 | } |
| 406 | |
| 407 | /* PWM frequency */ |
| 408 | static const int PWM_FREQ[] = {11, 15, 22, 29, 35, 44, 59, 88, |
| 409 | 15000, 20000, 30000, 25000, 0, 0, 0, 0}; |
| 410 | |
| 411 | static inline int PWM_FREQ_FROM_REG(int reg) |
| 412 | { |
| 413 | return PWM_FREQ[reg & 0x0f]; |
| 414 | } |
| 415 | |
| 416 | static int PWM_FREQ_TO_REG(int val, int reg) |
| 417 | { |
| 418 | int i; |
| 419 | |
| 420 | /* the first two cases are special - stupid chip design! */ |
| 421 | if (val > 27500) { |
| 422 | i = 10; |
| 423 | } else if (val > 22500) { |
| 424 | i = 11; |
| 425 | } else { |
| 426 | for (i = 9; i > 0; i--) { |
| 427 | if (val > (PWM_FREQ[i] + PWM_FREQ[i - 1] + 1) / 2) { |
| 428 | break; |
| 429 | } |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | return (reg & 0xf0) | i; |
| 434 | } |
| 435 | |
| 436 | /* PWM ramp rate |
| 437 | * Register layout: |
| 438 | * reg[0] = [OFF3, OFF2, OFF1, RES, RR1-E, RR1-2, RR1-1, RR1-0] |
| 439 | * reg[1] = [RR2-E, RR2-2, RR2-1, RR2-0, RR3-E, RR3-2, RR3-1, RR3-0] */ |
| 440 | static const u8 PWM_RR[] = {206, 104, 69, 41, 26, 18, 10, 5}; |
| 441 | |
| 442 | static inline int PWM_RR_FROM_REG(int reg, int ix) |
| 443 | { |
| 444 | int rr = (ix == 1) ? reg >> 4 : reg; |
| 445 | |
| 446 | return (rr & 0x08) ? PWM_RR[rr & 0x07] : 0; |
| 447 | } |
| 448 | |
| 449 | static int PWM_RR_TO_REG(int val, int ix, int reg) |
| 450 | { |
| 451 | int i; |
| 452 | |
| 453 | for (i = 0; i < 7; i++) { |
| 454 | if (val > (PWM_RR[i] + PWM_RR[i + 1] + 1) / 2) { |
| 455 | break; |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | return (ix == 1) ? (reg & 0x8f) | (i << 4) : (reg & 0xf8) | i; |
| 460 | } |
| 461 | |
| 462 | /* PWM ramp rate enable */ |
| 463 | static inline int PWM_RR_EN_FROM_REG(int reg, int ix) |
| 464 | { |
| 465 | return PWM_RR_FROM_REG(reg, ix) ? 1 : 0; |
| 466 | } |
| 467 | |
| 468 | static inline int PWM_RR_EN_TO_REG(int val, int ix, int reg) |
| 469 | { |
| 470 | int en = (ix == 1) ? 0x80 : 0x08; |
| 471 | |
| 472 | return val ? reg | en : reg & ~en; |
| 473 | } |
| 474 | |
| 475 | /* PWM min/off |
| 476 | * The PWM min/off bits are part of the PMW ramp rate register 0 (see above for |
| 477 | * the register layout). */ |
| 478 | static inline int PWM_OFF_FROM_REG(int reg, int ix) |
| 479 | { |
| 480 | return (reg >> (ix + 5)) & 0x01; |
| 481 | } |
| 482 | |
| 483 | static inline int PWM_OFF_TO_REG(int val, int ix, int reg) |
| 484 | { |
| 485 | return (reg & ~(1 << (ix + 5))) | ((val & 0x01) << (ix + 5)); |
| 486 | } |
| 487 | |
| 488 | /* --------------------------------------------------------------------- |
| 489 | * Device I/O access |
Juerg Haefliger | e95c237 | 2007-10-07 21:27:35 -0700 | [diff] [blame] | 490 | * |
| 491 | * ISA access is performed through an index/data register pair and needs to |
| 492 | * be protected by a mutex during runtime (not required for initialization). |
| 493 | * We use data->update_lock for this and need to ensure that we acquire it |
| 494 | * before calling dme1737_read or dme1737_write. |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 495 | * --------------------------------------------------------------------- */ |
| 496 | |
| 497 | static u8 dme1737_read(struct i2c_client *client, u8 reg) |
| 498 | { |
Juerg Haefliger | e95c237 | 2007-10-07 21:27:35 -0700 | [diff] [blame] | 499 | s32 val; |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 500 | |
Juerg Haefliger | e95c237 | 2007-10-07 21:27:35 -0700 | [diff] [blame] | 501 | if (client->driver) { /* I2C device */ |
| 502 | val = i2c_smbus_read_byte_data(client, reg); |
| 503 | |
| 504 | if (val < 0) { |
| 505 | dev_warn(&client->dev, "Read from register " |
| 506 | "0x%02x failed! Please report to the driver " |
| 507 | "maintainer.\n", reg); |
| 508 | } |
| 509 | } else { /* ISA device */ |
| 510 | outb(reg, client->addr); |
| 511 | val = inb(client->addr + 1); |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | return val; |
| 515 | } |
| 516 | |
Juerg Haefliger | e95c237 | 2007-10-07 21:27:35 -0700 | [diff] [blame] | 517 | static s32 dme1737_write(struct i2c_client *client, u8 reg, u8 val) |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 518 | { |
Juerg Haefliger | e95c237 | 2007-10-07 21:27:35 -0700 | [diff] [blame] | 519 | s32 res = 0; |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 520 | |
Juerg Haefliger | e95c237 | 2007-10-07 21:27:35 -0700 | [diff] [blame] | 521 | if (client->driver) { /* I2C device */ |
| 522 | res = i2c_smbus_write_byte_data(client, reg, val); |
| 523 | |
| 524 | if (res < 0) { |
| 525 | dev_warn(&client->dev, "Write to register " |
| 526 | "0x%02x failed! Please report to the driver " |
| 527 | "maintainer.\n", reg); |
| 528 | } |
| 529 | } else { /* ISA device */ |
| 530 | outb(reg, client->addr); |
| 531 | outb(val, client->addr + 1); |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | return res; |
| 535 | } |
| 536 | |
| 537 | static struct dme1737_data *dme1737_update_device(struct device *dev) |
| 538 | { |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 539 | struct dme1737_data *data = dev_get_drvdata(dev); |
| 540 | struct i2c_client *client = &data->client; |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 541 | int ix; |
| 542 | u8 lsb[5]; |
| 543 | |
| 544 | mutex_lock(&data->update_lock); |
| 545 | |
| 546 | /* Enable a Vbat monitoring cycle every 10 mins */ |
| 547 | if (time_after(jiffies, data->last_vbat + 600 * HZ) || !data->valid) { |
| 548 | dme1737_write(client, DME1737_REG_CONFIG, dme1737_read(client, |
| 549 | DME1737_REG_CONFIG) | 0x10); |
| 550 | data->last_vbat = jiffies; |
| 551 | } |
| 552 | |
| 553 | /* Sample register contents every 1 sec */ |
| 554 | if (time_after(jiffies, data->last_update + HZ) || !data->valid) { |
| 555 | data->vid = dme1737_read(client, DME1737_REG_VID) & 0x3f; |
| 556 | |
| 557 | /* In (voltage) registers */ |
| 558 | for (ix = 0; ix < ARRAY_SIZE(data->in); ix++) { |
| 559 | /* Voltage inputs are stored as 16 bit values even |
| 560 | * though they have only 12 bits resolution. This is |
| 561 | * to make it consistent with the temp inputs. */ |
| 562 | data->in[ix] = dme1737_read(client, |
| 563 | DME1737_REG_IN(ix)) << 8; |
| 564 | data->in_min[ix] = dme1737_read(client, |
| 565 | DME1737_REG_IN_MIN(ix)); |
| 566 | data->in_max[ix] = dme1737_read(client, |
| 567 | DME1737_REG_IN_MAX(ix)); |
| 568 | } |
| 569 | |
| 570 | /* Temp registers */ |
| 571 | for (ix = 0; ix < ARRAY_SIZE(data->temp); ix++) { |
| 572 | /* Temp inputs are stored as 16 bit values even |
| 573 | * though they have only 12 bits resolution. This is |
| 574 | * to take advantage of implicit conversions between |
| 575 | * register values (2's complement) and temp values |
| 576 | * (signed decimal). */ |
| 577 | data->temp[ix] = dme1737_read(client, |
| 578 | DME1737_REG_TEMP(ix)) << 8; |
| 579 | data->temp_min[ix] = dme1737_read(client, |
| 580 | DME1737_REG_TEMP_MIN(ix)); |
| 581 | data->temp_max[ix] = dme1737_read(client, |
| 582 | DME1737_REG_TEMP_MAX(ix)); |
| 583 | data->temp_offset[ix] = dme1737_read(client, |
| 584 | DME1737_REG_TEMP_OFFSET(ix)); |
| 585 | } |
| 586 | |
| 587 | /* In and temp LSB registers |
| 588 | * The LSBs are latched when the MSBs are read, so the order in |
| 589 | * which the registers are read (MSB first, then LSB) is |
| 590 | * important! */ |
| 591 | for (ix = 0; ix < ARRAY_SIZE(lsb); ix++) { |
| 592 | lsb[ix] = dme1737_read(client, |
| 593 | DME1737_REG_IN_TEMP_LSB(ix)); |
| 594 | } |
| 595 | for (ix = 0; ix < ARRAY_SIZE(data->in); ix++) { |
| 596 | data->in[ix] |= (lsb[DME1737_REG_IN_LSB[ix]] << |
| 597 | DME1737_REG_IN_LSB_SHL[ix]) & 0xf0; |
| 598 | } |
| 599 | for (ix = 0; ix < ARRAY_SIZE(data->temp); ix++) { |
| 600 | data->temp[ix] |= (lsb[DME1737_REG_TEMP_LSB[ix]] << |
| 601 | DME1737_REG_TEMP_LSB_SHL[ix]) & 0xf0; |
| 602 | } |
| 603 | |
| 604 | /* Fan registers */ |
| 605 | for (ix = 0; ix < ARRAY_SIZE(data->fan); ix++) { |
| 606 | /* Skip reading registers if optional fans are not |
| 607 | * present */ |
| 608 | if (!(data->has_fan & (1 << ix))) { |
| 609 | continue; |
| 610 | } |
| 611 | data->fan[ix] = dme1737_read(client, |
| 612 | DME1737_REG_FAN(ix)); |
| 613 | data->fan[ix] |= dme1737_read(client, |
| 614 | DME1737_REG_FAN(ix) + 1) << 8; |
| 615 | data->fan_min[ix] = dme1737_read(client, |
| 616 | DME1737_REG_FAN_MIN(ix)); |
| 617 | data->fan_min[ix] |= dme1737_read(client, |
| 618 | DME1737_REG_FAN_MIN(ix) + 1) << 8; |
| 619 | data->fan_opt[ix] = dme1737_read(client, |
| 620 | DME1737_REG_FAN_OPT(ix)); |
| 621 | /* fan_max exists only for fan[5-6] */ |
| 622 | if (ix > 3) { |
| 623 | data->fan_max[ix - 4] = dme1737_read(client, |
| 624 | DME1737_REG_FAN_MAX(ix)); |
| 625 | } |
| 626 | } |
| 627 | |
| 628 | /* PWM registers */ |
| 629 | for (ix = 0; ix < ARRAY_SIZE(data->pwm); ix++) { |
| 630 | /* Skip reading registers if optional PWMs are not |
| 631 | * present */ |
| 632 | if (!(data->has_pwm & (1 << ix))) { |
| 633 | continue; |
| 634 | } |
| 635 | data->pwm[ix] = dme1737_read(client, |
| 636 | DME1737_REG_PWM(ix)); |
| 637 | data->pwm_freq[ix] = dme1737_read(client, |
| 638 | DME1737_REG_PWM_FREQ(ix)); |
| 639 | /* pwm_config and pwm_min exist only for pwm[1-3] */ |
| 640 | if (ix < 3) { |
| 641 | data->pwm_config[ix] = dme1737_read(client, |
| 642 | DME1737_REG_PWM_CONFIG(ix)); |
| 643 | data->pwm_min[ix] = dme1737_read(client, |
| 644 | DME1737_REG_PWM_MIN(ix)); |
| 645 | } |
| 646 | } |
| 647 | for (ix = 0; ix < ARRAY_SIZE(data->pwm_rr); ix++) { |
| 648 | data->pwm_rr[ix] = dme1737_read(client, |
| 649 | DME1737_REG_PWM_RR(ix)); |
| 650 | } |
| 651 | |
| 652 | /* Thermal zone registers */ |
| 653 | for (ix = 0; ix < ARRAY_SIZE(data->zone_low); ix++) { |
| 654 | data->zone_low[ix] = dme1737_read(client, |
| 655 | DME1737_REG_ZONE_LOW(ix)); |
| 656 | data->zone_abs[ix] = dme1737_read(client, |
| 657 | DME1737_REG_ZONE_ABS(ix)); |
| 658 | } |
| 659 | for (ix = 0; ix < ARRAY_SIZE(data->zone_hyst); ix++) { |
| 660 | data->zone_hyst[ix] = dme1737_read(client, |
| 661 | DME1737_REG_ZONE_HYST(ix)); |
| 662 | } |
| 663 | |
| 664 | /* Alarm registers */ |
| 665 | data->alarms = dme1737_read(client, |
| 666 | DME1737_REG_ALARM1); |
| 667 | /* Bit 7 tells us if the other alarm registers are non-zero and |
| 668 | * therefore also need to be read */ |
| 669 | if (data->alarms & 0x80) { |
| 670 | data->alarms |= dme1737_read(client, |
| 671 | DME1737_REG_ALARM2) << 8; |
| 672 | data->alarms |= dme1737_read(client, |
| 673 | DME1737_REG_ALARM3) << 16; |
| 674 | } |
| 675 | |
Juerg Haefliger | e95c237 | 2007-10-07 21:27:35 -0700 | [diff] [blame] | 676 | /* The ISA chips require explicit clearing of alarm bits. |
| 677 | * Don't worry, an alarm will come back if the condition |
| 678 | * that causes it still exists */ |
| 679 | if (!client->driver) { |
| 680 | if (data->alarms & 0xff0000) { |
| 681 | dme1737_write(client, DME1737_REG_ALARM3, |
| 682 | 0xff); |
| 683 | } |
| 684 | if (data->alarms & 0xff00) { |
| 685 | dme1737_write(client, DME1737_REG_ALARM2, |
| 686 | 0xff); |
| 687 | } |
| 688 | if (data->alarms & 0xff) { |
| 689 | dme1737_write(client, DME1737_REG_ALARM1, |
| 690 | 0xff); |
| 691 | } |
| 692 | } |
| 693 | |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 694 | data->last_update = jiffies; |
| 695 | data->valid = 1; |
| 696 | } |
| 697 | |
| 698 | mutex_unlock(&data->update_lock); |
| 699 | |
| 700 | return data; |
| 701 | } |
| 702 | |
| 703 | /* --------------------------------------------------------------------- |
| 704 | * Voltage sysfs attributes |
| 705 | * ix = [0-5] |
| 706 | * --------------------------------------------------------------------- */ |
| 707 | |
| 708 | #define SYS_IN_INPUT 0 |
| 709 | #define SYS_IN_MIN 1 |
| 710 | #define SYS_IN_MAX 2 |
| 711 | #define SYS_IN_ALARM 3 |
| 712 | |
| 713 | static ssize_t show_in(struct device *dev, struct device_attribute *attr, |
| 714 | char *buf) |
| 715 | { |
| 716 | struct dme1737_data *data = dme1737_update_device(dev); |
| 717 | struct sensor_device_attribute_2 |
| 718 | *sensor_attr_2 = to_sensor_dev_attr_2(attr); |
| 719 | int ix = sensor_attr_2->index; |
| 720 | int fn = sensor_attr_2->nr; |
| 721 | int res; |
| 722 | |
| 723 | switch (fn) { |
| 724 | case SYS_IN_INPUT: |
| 725 | res = IN_FROM_REG(data->in[ix], ix, 16); |
| 726 | break; |
| 727 | case SYS_IN_MIN: |
| 728 | res = IN_FROM_REG(data->in_min[ix], ix, 8); |
| 729 | break; |
| 730 | case SYS_IN_MAX: |
| 731 | res = IN_FROM_REG(data->in_max[ix], ix, 8); |
| 732 | break; |
| 733 | case SYS_IN_ALARM: |
| 734 | res = (data->alarms >> DME1737_BIT_ALARM_IN[ix]) & 0x01; |
| 735 | break; |
| 736 | default: |
| 737 | res = 0; |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 738 | dev_dbg(dev, "Unknown function %d.\n", fn); |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | return sprintf(buf, "%d\n", res); |
| 742 | } |
| 743 | |
| 744 | static ssize_t set_in(struct device *dev, struct device_attribute *attr, |
| 745 | const char *buf, size_t count) |
| 746 | { |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 747 | struct dme1737_data *data = dev_get_drvdata(dev); |
| 748 | struct i2c_client *client = &data->client; |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 749 | struct sensor_device_attribute_2 |
| 750 | *sensor_attr_2 = to_sensor_dev_attr_2(attr); |
| 751 | int ix = sensor_attr_2->index; |
| 752 | int fn = sensor_attr_2->nr; |
| 753 | long val = simple_strtol(buf, NULL, 10); |
| 754 | |
| 755 | mutex_lock(&data->update_lock); |
| 756 | switch (fn) { |
| 757 | case SYS_IN_MIN: |
| 758 | data->in_min[ix] = IN_TO_REG(val, ix); |
| 759 | dme1737_write(client, DME1737_REG_IN_MIN(ix), |
| 760 | data->in_min[ix]); |
| 761 | break; |
| 762 | case SYS_IN_MAX: |
| 763 | data->in_max[ix] = IN_TO_REG(val, ix); |
| 764 | dme1737_write(client, DME1737_REG_IN_MAX(ix), |
| 765 | data->in_max[ix]); |
| 766 | break; |
| 767 | default: |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 768 | dev_dbg(dev, "Unknown function %d.\n", fn); |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 769 | } |
| 770 | mutex_unlock(&data->update_lock); |
| 771 | |
| 772 | return count; |
| 773 | } |
| 774 | |
| 775 | /* --------------------------------------------------------------------- |
| 776 | * Temperature sysfs attributes |
| 777 | * ix = [0-2] |
| 778 | * --------------------------------------------------------------------- */ |
| 779 | |
| 780 | #define SYS_TEMP_INPUT 0 |
| 781 | #define SYS_TEMP_MIN 1 |
| 782 | #define SYS_TEMP_MAX 2 |
| 783 | #define SYS_TEMP_OFFSET 3 |
| 784 | #define SYS_TEMP_ALARM 4 |
| 785 | #define SYS_TEMP_FAULT 5 |
| 786 | |
| 787 | static ssize_t show_temp(struct device *dev, struct device_attribute *attr, |
| 788 | char *buf) |
| 789 | { |
| 790 | struct dme1737_data *data = dme1737_update_device(dev); |
| 791 | struct sensor_device_attribute_2 |
| 792 | *sensor_attr_2 = to_sensor_dev_attr_2(attr); |
| 793 | int ix = sensor_attr_2->index; |
| 794 | int fn = sensor_attr_2->nr; |
| 795 | int res; |
| 796 | |
| 797 | switch (fn) { |
| 798 | case SYS_TEMP_INPUT: |
| 799 | res = TEMP_FROM_REG(data->temp[ix], 16); |
| 800 | break; |
| 801 | case SYS_TEMP_MIN: |
| 802 | res = TEMP_FROM_REG(data->temp_min[ix], 8); |
| 803 | break; |
| 804 | case SYS_TEMP_MAX: |
| 805 | res = TEMP_FROM_REG(data->temp_max[ix], 8); |
| 806 | break; |
| 807 | case SYS_TEMP_OFFSET: |
| 808 | res = TEMP_FROM_REG(data->temp_offset[ix], 8); |
| 809 | break; |
| 810 | case SYS_TEMP_ALARM: |
| 811 | res = (data->alarms >> DME1737_BIT_ALARM_TEMP[ix]) & 0x01; |
| 812 | break; |
| 813 | case SYS_TEMP_FAULT: |
Juerg Haefliger | c0f3140 | 2007-07-20 14:16:47 -0700 | [diff] [blame] | 814 | res = (((u16)data->temp[ix] & 0xff00) == 0x8000); |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 815 | break; |
| 816 | default: |
| 817 | res = 0; |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 818 | dev_dbg(dev, "Unknown function %d.\n", fn); |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 819 | } |
| 820 | |
| 821 | return sprintf(buf, "%d\n", res); |
| 822 | } |
| 823 | |
| 824 | static ssize_t set_temp(struct device *dev, struct device_attribute *attr, |
| 825 | const char *buf, size_t count) |
| 826 | { |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 827 | struct dme1737_data *data = dev_get_drvdata(dev); |
| 828 | struct i2c_client *client = &data->client; |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 829 | struct sensor_device_attribute_2 |
| 830 | *sensor_attr_2 = to_sensor_dev_attr_2(attr); |
| 831 | int ix = sensor_attr_2->index; |
| 832 | int fn = sensor_attr_2->nr; |
| 833 | long val = simple_strtol(buf, NULL, 10); |
| 834 | |
| 835 | mutex_lock(&data->update_lock); |
| 836 | switch (fn) { |
| 837 | case SYS_TEMP_MIN: |
| 838 | data->temp_min[ix] = TEMP_TO_REG(val); |
| 839 | dme1737_write(client, DME1737_REG_TEMP_MIN(ix), |
| 840 | data->temp_min[ix]); |
| 841 | break; |
| 842 | case SYS_TEMP_MAX: |
| 843 | data->temp_max[ix] = TEMP_TO_REG(val); |
| 844 | dme1737_write(client, DME1737_REG_TEMP_MAX(ix), |
| 845 | data->temp_max[ix]); |
| 846 | break; |
| 847 | case SYS_TEMP_OFFSET: |
| 848 | data->temp_offset[ix] = TEMP_TO_REG(val); |
| 849 | dme1737_write(client, DME1737_REG_TEMP_OFFSET(ix), |
| 850 | data->temp_offset[ix]); |
| 851 | break; |
| 852 | default: |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 853 | dev_dbg(dev, "Unknown function %d.\n", fn); |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 854 | } |
| 855 | mutex_unlock(&data->update_lock); |
| 856 | |
| 857 | return count; |
| 858 | } |
| 859 | |
| 860 | /* --------------------------------------------------------------------- |
| 861 | * Zone sysfs attributes |
| 862 | * ix = [0-2] |
| 863 | * --------------------------------------------------------------------- */ |
| 864 | |
| 865 | #define SYS_ZONE_AUTO_CHANNELS_TEMP 0 |
| 866 | #define SYS_ZONE_AUTO_POINT1_TEMP_HYST 1 |
| 867 | #define SYS_ZONE_AUTO_POINT1_TEMP 2 |
| 868 | #define SYS_ZONE_AUTO_POINT2_TEMP 3 |
| 869 | #define SYS_ZONE_AUTO_POINT3_TEMP 4 |
| 870 | |
| 871 | static ssize_t show_zone(struct device *dev, struct device_attribute *attr, |
| 872 | char *buf) |
| 873 | { |
| 874 | struct dme1737_data *data = dme1737_update_device(dev); |
| 875 | struct sensor_device_attribute_2 |
| 876 | *sensor_attr_2 = to_sensor_dev_attr_2(attr); |
| 877 | int ix = sensor_attr_2->index; |
| 878 | int fn = sensor_attr_2->nr; |
| 879 | int res; |
| 880 | |
| 881 | switch (fn) { |
| 882 | case SYS_ZONE_AUTO_CHANNELS_TEMP: |
| 883 | /* check config2 for non-standard temp-to-zone mapping */ |
| 884 | if ((ix == 1) && (data->config2 & 0x02)) { |
| 885 | res = 4; |
| 886 | } else { |
| 887 | res = 1 << ix; |
| 888 | } |
| 889 | break; |
| 890 | case SYS_ZONE_AUTO_POINT1_TEMP_HYST: |
| 891 | res = TEMP_FROM_REG(data->zone_low[ix], 8) - |
| 892 | TEMP_HYST_FROM_REG(data->zone_hyst[ix == 2], ix); |
| 893 | break; |
| 894 | case SYS_ZONE_AUTO_POINT1_TEMP: |
| 895 | res = TEMP_FROM_REG(data->zone_low[ix], 8); |
| 896 | break; |
| 897 | case SYS_ZONE_AUTO_POINT2_TEMP: |
| 898 | /* pwm_freq holds the temp range bits in the upper nibble */ |
| 899 | res = TEMP_FROM_REG(data->zone_low[ix], 8) + |
| 900 | TEMP_RANGE_FROM_REG(data->pwm_freq[ix]); |
| 901 | break; |
| 902 | case SYS_ZONE_AUTO_POINT3_TEMP: |
| 903 | res = TEMP_FROM_REG(data->zone_abs[ix], 8); |
| 904 | break; |
| 905 | default: |
| 906 | res = 0; |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 907 | dev_dbg(dev, "Unknown function %d.\n", fn); |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 908 | } |
| 909 | |
| 910 | return sprintf(buf, "%d\n", res); |
| 911 | } |
| 912 | |
| 913 | static ssize_t set_zone(struct device *dev, struct device_attribute *attr, |
| 914 | const char *buf, size_t count) |
| 915 | { |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 916 | struct dme1737_data *data = dev_get_drvdata(dev); |
| 917 | struct i2c_client *client = &data->client; |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 918 | struct sensor_device_attribute_2 |
| 919 | *sensor_attr_2 = to_sensor_dev_attr_2(attr); |
| 920 | int ix = sensor_attr_2->index; |
| 921 | int fn = sensor_attr_2->nr; |
| 922 | long val = simple_strtol(buf, NULL, 10); |
| 923 | |
| 924 | mutex_lock(&data->update_lock); |
| 925 | switch (fn) { |
| 926 | case SYS_ZONE_AUTO_POINT1_TEMP_HYST: |
| 927 | /* Refresh the cache */ |
| 928 | data->zone_low[ix] = dme1737_read(client, |
| 929 | DME1737_REG_ZONE_LOW(ix)); |
| 930 | /* Modify the temp hyst value */ |
| 931 | data->zone_hyst[ix == 2] = TEMP_HYST_TO_REG( |
| 932 | TEMP_FROM_REG(data->zone_low[ix], 8) - |
| 933 | val, ix, dme1737_read(client, |
| 934 | DME1737_REG_ZONE_HYST(ix == 2))); |
| 935 | dme1737_write(client, DME1737_REG_ZONE_HYST(ix == 2), |
| 936 | data->zone_hyst[ix == 2]); |
| 937 | break; |
| 938 | case SYS_ZONE_AUTO_POINT1_TEMP: |
| 939 | data->zone_low[ix] = TEMP_TO_REG(val); |
| 940 | dme1737_write(client, DME1737_REG_ZONE_LOW(ix), |
| 941 | data->zone_low[ix]); |
| 942 | break; |
| 943 | case SYS_ZONE_AUTO_POINT2_TEMP: |
| 944 | /* Refresh the cache */ |
| 945 | data->zone_low[ix] = dme1737_read(client, |
| 946 | DME1737_REG_ZONE_LOW(ix)); |
| 947 | /* Modify the temp range value (which is stored in the upper |
| 948 | * nibble of the pwm_freq register) */ |
| 949 | data->pwm_freq[ix] = TEMP_RANGE_TO_REG(val - |
| 950 | TEMP_FROM_REG(data->zone_low[ix], 8), |
| 951 | dme1737_read(client, |
| 952 | DME1737_REG_PWM_FREQ(ix))); |
| 953 | dme1737_write(client, DME1737_REG_PWM_FREQ(ix), |
| 954 | data->pwm_freq[ix]); |
| 955 | break; |
| 956 | case SYS_ZONE_AUTO_POINT3_TEMP: |
| 957 | data->zone_abs[ix] = TEMP_TO_REG(val); |
| 958 | dme1737_write(client, DME1737_REG_ZONE_ABS(ix), |
| 959 | data->zone_abs[ix]); |
| 960 | break; |
| 961 | default: |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 962 | dev_dbg(dev, "Unknown function %d.\n", fn); |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 963 | } |
| 964 | mutex_unlock(&data->update_lock); |
| 965 | |
| 966 | return count; |
| 967 | } |
| 968 | |
| 969 | /* --------------------------------------------------------------------- |
| 970 | * Fan sysfs attributes |
| 971 | * ix = [0-5] |
| 972 | * --------------------------------------------------------------------- */ |
| 973 | |
| 974 | #define SYS_FAN_INPUT 0 |
| 975 | #define SYS_FAN_MIN 1 |
| 976 | #define SYS_FAN_MAX 2 |
| 977 | #define SYS_FAN_ALARM 3 |
| 978 | #define SYS_FAN_TYPE 4 |
| 979 | |
| 980 | static ssize_t show_fan(struct device *dev, struct device_attribute *attr, |
| 981 | char *buf) |
| 982 | { |
| 983 | struct dme1737_data *data = dme1737_update_device(dev); |
| 984 | struct sensor_device_attribute_2 |
| 985 | *sensor_attr_2 = to_sensor_dev_attr_2(attr); |
| 986 | int ix = sensor_attr_2->index; |
| 987 | int fn = sensor_attr_2->nr; |
| 988 | int res; |
| 989 | |
| 990 | switch (fn) { |
| 991 | case SYS_FAN_INPUT: |
| 992 | res = FAN_FROM_REG(data->fan[ix], |
| 993 | ix < 4 ? 0 : |
| 994 | FAN_TPC_FROM_REG(data->fan_opt[ix])); |
| 995 | break; |
| 996 | case SYS_FAN_MIN: |
| 997 | res = FAN_FROM_REG(data->fan_min[ix], |
| 998 | ix < 4 ? 0 : |
| 999 | FAN_TPC_FROM_REG(data->fan_opt[ix])); |
| 1000 | break; |
| 1001 | case SYS_FAN_MAX: |
| 1002 | /* only valid for fan[5-6] */ |
| 1003 | res = FAN_MAX_FROM_REG(data->fan_max[ix - 4]); |
| 1004 | break; |
| 1005 | case SYS_FAN_ALARM: |
| 1006 | res = (data->alarms >> DME1737_BIT_ALARM_FAN[ix]) & 0x01; |
| 1007 | break; |
| 1008 | case SYS_FAN_TYPE: |
| 1009 | /* only valid for fan[1-4] */ |
| 1010 | res = FAN_TYPE_FROM_REG(data->fan_opt[ix]); |
| 1011 | break; |
| 1012 | default: |
| 1013 | res = 0; |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 1014 | dev_dbg(dev, "Unknown function %d.\n", fn); |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1015 | } |
| 1016 | |
| 1017 | return sprintf(buf, "%d\n", res); |
| 1018 | } |
| 1019 | |
| 1020 | static ssize_t set_fan(struct device *dev, struct device_attribute *attr, |
| 1021 | const char *buf, size_t count) |
| 1022 | { |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 1023 | struct dme1737_data *data = dev_get_drvdata(dev); |
| 1024 | struct i2c_client *client = &data->client; |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1025 | struct sensor_device_attribute_2 |
| 1026 | *sensor_attr_2 = to_sensor_dev_attr_2(attr); |
| 1027 | int ix = sensor_attr_2->index; |
| 1028 | int fn = sensor_attr_2->nr; |
| 1029 | long val = simple_strtol(buf, NULL, 10); |
| 1030 | |
| 1031 | mutex_lock(&data->update_lock); |
| 1032 | switch (fn) { |
| 1033 | case SYS_FAN_MIN: |
| 1034 | if (ix < 4) { |
| 1035 | data->fan_min[ix] = FAN_TO_REG(val, 0); |
| 1036 | } else { |
| 1037 | /* Refresh the cache */ |
| 1038 | data->fan_opt[ix] = dme1737_read(client, |
| 1039 | DME1737_REG_FAN_OPT(ix)); |
| 1040 | /* Modify the fan min value */ |
| 1041 | data->fan_min[ix] = FAN_TO_REG(val, |
| 1042 | FAN_TPC_FROM_REG(data->fan_opt[ix])); |
| 1043 | } |
| 1044 | dme1737_write(client, DME1737_REG_FAN_MIN(ix), |
| 1045 | data->fan_min[ix] & 0xff); |
| 1046 | dme1737_write(client, DME1737_REG_FAN_MIN(ix) + 1, |
| 1047 | data->fan_min[ix] >> 8); |
| 1048 | break; |
| 1049 | case SYS_FAN_MAX: |
| 1050 | /* Only valid for fan[5-6] */ |
| 1051 | data->fan_max[ix - 4] = FAN_MAX_TO_REG(val); |
| 1052 | dme1737_write(client, DME1737_REG_FAN_MAX(ix), |
| 1053 | data->fan_max[ix - 4]); |
| 1054 | break; |
| 1055 | case SYS_FAN_TYPE: |
| 1056 | /* Only valid for fan[1-4] */ |
| 1057 | if (!(val == 1 || val == 2 || val == 4)) { |
| 1058 | count = -EINVAL; |
Juerg Haefliger | e95c237 | 2007-10-07 21:27:35 -0700 | [diff] [blame] | 1059 | dev_warn(dev, "Fan type value %ld not " |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1060 | "supported. Choose one of 1, 2, or 4.\n", |
| 1061 | val); |
| 1062 | goto exit; |
| 1063 | } |
| 1064 | data->fan_opt[ix] = FAN_TYPE_TO_REG(val, dme1737_read(client, |
| 1065 | DME1737_REG_FAN_OPT(ix))); |
| 1066 | dme1737_write(client, DME1737_REG_FAN_OPT(ix), |
| 1067 | data->fan_opt[ix]); |
| 1068 | break; |
| 1069 | default: |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 1070 | dev_dbg(dev, "Unknown function %d.\n", fn); |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1071 | } |
| 1072 | exit: |
| 1073 | mutex_unlock(&data->update_lock); |
| 1074 | |
| 1075 | return count; |
| 1076 | } |
| 1077 | |
| 1078 | /* --------------------------------------------------------------------- |
| 1079 | * PWM sysfs attributes |
| 1080 | * ix = [0-4] |
| 1081 | * --------------------------------------------------------------------- */ |
| 1082 | |
| 1083 | #define SYS_PWM 0 |
| 1084 | #define SYS_PWM_FREQ 1 |
| 1085 | #define SYS_PWM_ENABLE 2 |
| 1086 | #define SYS_PWM_RAMP_RATE 3 |
| 1087 | #define SYS_PWM_AUTO_CHANNELS_ZONE 4 |
| 1088 | #define SYS_PWM_AUTO_PWM_MIN 5 |
| 1089 | #define SYS_PWM_AUTO_POINT1_PWM 6 |
| 1090 | #define SYS_PWM_AUTO_POINT2_PWM 7 |
| 1091 | |
| 1092 | static ssize_t show_pwm(struct device *dev, struct device_attribute *attr, |
| 1093 | char *buf) |
| 1094 | { |
| 1095 | struct dme1737_data *data = dme1737_update_device(dev); |
| 1096 | struct sensor_device_attribute_2 |
| 1097 | *sensor_attr_2 = to_sensor_dev_attr_2(attr); |
| 1098 | int ix = sensor_attr_2->index; |
| 1099 | int fn = sensor_attr_2->nr; |
| 1100 | int res; |
| 1101 | |
| 1102 | switch (fn) { |
| 1103 | case SYS_PWM: |
| 1104 | if (PWM_EN_FROM_REG(data->pwm_config[ix]) == 0) { |
| 1105 | res = 255; |
| 1106 | } else { |
| 1107 | res = data->pwm[ix]; |
| 1108 | } |
| 1109 | break; |
| 1110 | case SYS_PWM_FREQ: |
| 1111 | res = PWM_FREQ_FROM_REG(data->pwm_freq[ix]); |
| 1112 | break; |
| 1113 | case SYS_PWM_ENABLE: |
| 1114 | if (ix > 3) { |
| 1115 | res = 1; /* pwm[5-6] hard-wired to manual mode */ |
| 1116 | } else { |
| 1117 | res = PWM_EN_FROM_REG(data->pwm_config[ix]); |
| 1118 | } |
| 1119 | break; |
| 1120 | case SYS_PWM_RAMP_RATE: |
| 1121 | /* Only valid for pwm[1-3] */ |
| 1122 | res = PWM_RR_FROM_REG(data->pwm_rr[ix > 0], ix); |
| 1123 | break; |
| 1124 | case SYS_PWM_AUTO_CHANNELS_ZONE: |
| 1125 | /* Only valid for pwm[1-3] */ |
| 1126 | if (PWM_EN_FROM_REG(data->pwm_config[ix]) == 2) { |
| 1127 | res = PWM_ACZ_FROM_REG(data->pwm_config[ix]); |
| 1128 | } else { |
| 1129 | res = data->pwm_acz[ix]; |
| 1130 | } |
| 1131 | break; |
| 1132 | case SYS_PWM_AUTO_PWM_MIN: |
| 1133 | /* Only valid for pwm[1-3] */ |
| 1134 | if (PWM_OFF_FROM_REG(data->pwm_rr[0], ix)) { |
| 1135 | res = data->pwm_min[ix]; |
| 1136 | } else { |
| 1137 | res = 0; |
| 1138 | } |
| 1139 | break; |
| 1140 | case SYS_PWM_AUTO_POINT1_PWM: |
| 1141 | /* Only valid for pwm[1-3] */ |
| 1142 | res = data->pwm_min[ix]; |
| 1143 | break; |
| 1144 | case SYS_PWM_AUTO_POINT2_PWM: |
| 1145 | /* Only valid for pwm[1-3] */ |
| 1146 | res = 255; /* hard-wired */ |
| 1147 | break; |
| 1148 | default: |
| 1149 | res = 0; |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 1150 | dev_dbg(dev, "Unknown function %d.\n", fn); |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1151 | } |
| 1152 | |
| 1153 | return sprintf(buf, "%d\n", res); |
| 1154 | } |
| 1155 | |
| 1156 | static struct attribute *dme1737_attr_pwm[]; |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 1157 | static void dme1737_chmod_file(struct device*, struct attribute*, mode_t); |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1158 | |
| 1159 | static ssize_t set_pwm(struct device *dev, struct device_attribute *attr, |
| 1160 | const char *buf, size_t count) |
| 1161 | { |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 1162 | struct dme1737_data *data = dev_get_drvdata(dev); |
| 1163 | struct i2c_client *client = &data->client; |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1164 | struct sensor_device_attribute_2 |
| 1165 | *sensor_attr_2 = to_sensor_dev_attr_2(attr); |
| 1166 | int ix = sensor_attr_2->index; |
| 1167 | int fn = sensor_attr_2->nr; |
| 1168 | long val = simple_strtol(buf, NULL, 10); |
| 1169 | |
| 1170 | mutex_lock(&data->update_lock); |
| 1171 | switch (fn) { |
| 1172 | case SYS_PWM: |
| 1173 | data->pwm[ix] = SENSORS_LIMIT(val, 0, 255); |
| 1174 | dme1737_write(client, DME1737_REG_PWM(ix), data->pwm[ix]); |
| 1175 | break; |
| 1176 | case SYS_PWM_FREQ: |
| 1177 | data->pwm_freq[ix] = PWM_FREQ_TO_REG(val, dme1737_read(client, |
| 1178 | DME1737_REG_PWM_FREQ(ix))); |
| 1179 | dme1737_write(client, DME1737_REG_PWM_FREQ(ix), |
| 1180 | data->pwm_freq[ix]); |
| 1181 | break; |
| 1182 | case SYS_PWM_ENABLE: |
| 1183 | /* Only valid for pwm[1-3] */ |
| 1184 | if (val < 0 || val > 2) { |
| 1185 | count = -EINVAL; |
Juerg Haefliger | e95c237 | 2007-10-07 21:27:35 -0700 | [diff] [blame] | 1186 | dev_warn(dev, "PWM enable %ld not " |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1187 | "supported. Choose one of 0, 1, or 2.\n", |
| 1188 | val); |
| 1189 | goto exit; |
| 1190 | } |
| 1191 | /* Refresh the cache */ |
| 1192 | data->pwm_config[ix] = dme1737_read(client, |
| 1193 | DME1737_REG_PWM_CONFIG(ix)); |
| 1194 | if (val == PWM_EN_FROM_REG(data->pwm_config[ix])) { |
| 1195 | /* Bail out if no change */ |
| 1196 | goto exit; |
| 1197 | } |
| 1198 | /* Do some housekeeping if we are currently in auto mode */ |
| 1199 | if (PWM_EN_FROM_REG(data->pwm_config[ix]) == 2) { |
| 1200 | /* Save the current zone channel assignment */ |
| 1201 | data->pwm_acz[ix] = PWM_ACZ_FROM_REG( |
| 1202 | data->pwm_config[ix]); |
| 1203 | /* Save the current ramp rate state and disable it */ |
| 1204 | data->pwm_rr[ix > 0] = dme1737_read(client, |
| 1205 | DME1737_REG_PWM_RR(ix > 0)); |
| 1206 | data->pwm_rr_en &= ~(1 << ix); |
| 1207 | if (PWM_RR_EN_FROM_REG(data->pwm_rr[ix > 0], ix)) { |
| 1208 | data->pwm_rr_en |= (1 << ix); |
| 1209 | data->pwm_rr[ix > 0] = PWM_RR_EN_TO_REG(0, ix, |
| 1210 | data->pwm_rr[ix > 0]); |
| 1211 | dme1737_write(client, |
| 1212 | DME1737_REG_PWM_RR(ix > 0), |
| 1213 | data->pwm_rr[ix > 0]); |
| 1214 | } |
| 1215 | } |
| 1216 | /* Set the new PWM mode */ |
| 1217 | switch (val) { |
| 1218 | case 0: |
| 1219 | /* Change permissions of pwm[ix] to read-only */ |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 1220 | dme1737_chmod_file(dev, dme1737_attr_pwm[ix], |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1221 | S_IRUGO); |
| 1222 | /* Turn fan fully on */ |
| 1223 | data->pwm_config[ix] = PWM_EN_TO_REG(0, |
| 1224 | data->pwm_config[ix]); |
| 1225 | dme1737_write(client, DME1737_REG_PWM_CONFIG(ix), |
| 1226 | data->pwm_config[ix]); |
| 1227 | break; |
| 1228 | case 1: |
| 1229 | /* Turn on manual mode */ |
| 1230 | data->pwm_config[ix] = PWM_EN_TO_REG(1, |
| 1231 | data->pwm_config[ix]); |
| 1232 | dme1737_write(client, DME1737_REG_PWM_CONFIG(ix), |
| 1233 | data->pwm_config[ix]); |
| 1234 | /* Change permissions of pwm[ix] to read-writeable */ |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 1235 | dme1737_chmod_file(dev, dme1737_attr_pwm[ix], |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1236 | S_IRUGO | S_IWUSR); |
| 1237 | break; |
| 1238 | case 2: |
| 1239 | /* Change permissions of pwm[ix] to read-only */ |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 1240 | dme1737_chmod_file(dev, dme1737_attr_pwm[ix], |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1241 | S_IRUGO); |
| 1242 | /* Turn on auto mode using the saved zone channel |
| 1243 | * assignment */ |
| 1244 | data->pwm_config[ix] = PWM_ACZ_TO_REG( |
| 1245 | data->pwm_acz[ix], |
| 1246 | data->pwm_config[ix]); |
| 1247 | dme1737_write(client, DME1737_REG_PWM_CONFIG(ix), |
| 1248 | data->pwm_config[ix]); |
| 1249 | /* Enable PWM ramp rate if previously enabled */ |
| 1250 | if (data->pwm_rr_en & (1 << ix)) { |
| 1251 | data->pwm_rr[ix > 0] = PWM_RR_EN_TO_REG(1, ix, |
| 1252 | dme1737_read(client, |
| 1253 | DME1737_REG_PWM_RR(ix > 0))); |
| 1254 | dme1737_write(client, |
| 1255 | DME1737_REG_PWM_RR(ix > 0), |
| 1256 | data->pwm_rr[ix > 0]); |
| 1257 | } |
| 1258 | break; |
| 1259 | } |
| 1260 | break; |
| 1261 | case SYS_PWM_RAMP_RATE: |
| 1262 | /* Only valid for pwm[1-3] */ |
| 1263 | /* Refresh the cache */ |
| 1264 | data->pwm_config[ix] = dme1737_read(client, |
| 1265 | DME1737_REG_PWM_CONFIG(ix)); |
| 1266 | data->pwm_rr[ix > 0] = dme1737_read(client, |
| 1267 | DME1737_REG_PWM_RR(ix > 0)); |
| 1268 | /* Set the ramp rate value */ |
| 1269 | if (val > 0) { |
| 1270 | data->pwm_rr[ix > 0] = PWM_RR_TO_REG(val, ix, |
| 1271 | data->pwm_rr[ix > 0]); |
| 1272 | } |
| 1273 | /* Enable/disable the feature only if the associated PWM |
| 1274 | * output is in automatic mode. */ |
| 1275 | if (PWM_EN_FROM_REG(data->pwm_config[ix]) == 2) { |
| 1276 | data->pwm_rr[ix > 0] = PWM_RR_EN_TO_REG(val > 0, ix, |
| 1277 | data->pwm_rr[ix > 0]); |
| 1278 | } |
| 1279 | dme1737_write(client, DME1737_REG_PWM_RR(ix > 0), |
| 1280 | data->pwm_rr[ix > 0]); |
| 1281 | break; |
| 1282 | case SYS_PWM_AUTO_CHANNELS_ZONE: |
| 1283 | /* Only valid for pwm[1-3] */ |
| 1284 | if (!(val == 1 || val == 2 || val == 4 || |
| 1285 | val == 6 || val == 7)) { |
| 1286 | count = -EINVAL; |
Juerg Haefliger | e95c237 | 2007-10-07 21:27:35 -0700 | [diff] [blame] | 1287 | dev_warn(dev, "PWM auto channels zone %ld " |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1288 | "not supported. Choose one of 1, 2, 4, 6, " |
| 1289 | "or 7.\n", val); |
| 1290 | goto exit; |
| 1291 | } |
| 1292 | /* Refresh the cache */ |
| 1293 | data->pwm_config[ix] = dme1737_read(client, |
| 1294 | DME1737_REG_PWM_CONFIG(ix)); |
| 1295 | if (PWM_EN_FROM_REG(data->pwm_config[ix]) == 2) { |
| 1296 | /* PWM is already in auto mode so update the temp |
| 1297 | * channel assignment */ |
| 1298 | data->pwm_config[ix] = PWM_ACZ_TO_REG(val, |
| 1299 | data->pwm_config[ix]); |
| 1300 | dme1737_write(client, DME1737_REG_PWM_CONFIG(ix), |
| 1301 | data->pwm_config[ix]); |
| 1302 | } else { |
| 1303 | /* PWM is not in auto mode so we save the temp |
| 1304 | * channel assignment for later use */ |
| 1305 | data->pwm_acz[ix] = val; |
| 1306 | } |
| 1307 | break; |
| 1308 | case SYS_PWM_AUTO_PWM_MIN: |
| 1309 | /* Only valid for pwm[1-3] */ |
| 1310 | /* Refresh the cache */ |
| 1311 | data->pwm_min[ix] = dme1737_read(client, |
| 1312 | DME1737_REG_PWM_MIN(ix)); |
| 1313 | /* There are only 2 values supported for the auto_pwm_min |
| 1314 | * value: 0 or auto_point1_pwm. So if the temperature drops |
| 1315 | * below the auto_point1_temp_hyst value, the fan either turns |
| 1316 | * off or runs at auto_point1_pwm duty-cycle. */ |
| 1317 | if (val > ((data->pwm_min[ix] + 1) / 2)) { |
| 1318 | data->pwm_rr[0] = PWM_OFF_TO_REG(1, ix, |
| 1319 | dme1737_read(client, |
| 1320 | DME1737_REG_PWM_RR(0))); |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1321 | } else { |
| 1322 | data->pwm_rr[0] = PWM_OFF_TO_REG(0, ix, |
| 1323 | dme1737_read(client, |
| 1324 | DME1737_REG_PWM_RR(0))); |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1325 | } |
| 1326 | dme1737_write(client, DME1737_REG_PWM_RR(0), |
| 1327 | data->pwm_rr[0]); |
| 1328 | break; |
| 1329 | case SYS_PWM_AUTO_POINT1_PWM: |
| 1330 | /* Only valid for pwm[1-3] */ |
| 1331 | data->pwm_min[ix] = SENSORS_LIMIT(val, 0, 255); |
| 1332 | dme1737_write(client, DME1737_REG_PWM_MIN(ix), |
| 1333 | data->pwm_min[ix]); |
| 1334 | break; |
| 1335 | default: |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 1336 | dev_dbg(dev, "Unknown function %d.\n", fn); |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1337 | } |
| 1338 | exit: |
| 1339 | mutex_unlock(&data->update_lock); |
| 1340 | |
| 1341 | return count; |
| 1342 | } |
| 1343 | |
| 1344 | /* --------------------------------------------------------------------- |
| 1345 | * Miscellaneous sysfs attributes |
| 1346 | * --------------------------------------------------------------------- */ |
| 1347 | |
| 1348 | static ssize_t show_vrm(struct device *dev, struct device_attribute *attr, |
| 1349 | char *buf) |
| 1350 | { |
| 1351 | struct i2c_client *client = to_i2c_client(dev); |
| 1352 | struct dme1737_data *data = i2c_get_clientdata(client); |
| 1353 | |
| 1354 | return sprintf(buf, "%d\n", data->vrm); |
| 1355 | } |
| 1356 | |
| 1357 | static ssize_t set_vrm(struct device *dev, struct device_attribute *attr, |
| 1358 | const char *buf, size_t count) |
| 1359 | { |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 1360 | struct dme1737_data *data = dev_get_drvdata(dev); |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1361 | long val = simple_strtol(buf, NULL, 10); |
| 1362 | |
| 1363 | data->vrm = val; |
| 1364 | return count; |
| 1365 | } |
| 1366 | |
| 1367 | static ssize_t show_vid(struct device *dev, struct device_attribute *attr, |
| 1368 | char *buf) |
| 1369 | { |
| 1370 | struct dme1737_data *data = dme1737_update_device(dev); |
| 1371 | |
| 1372 | return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm)); |
| 1373 | } |
| 1374 | |
Juerg Haefliger | e95c237 | 2007-10-07 21:27:35 -0700 | [diff] [blame] | 1375 | static ssize_t show_name(struct device *dev, struct device_attribute *attr, |
| 1376 | char *buf) |
| 1377 | { |
| 1378 | struct dme1737_data *data = dev_get_drvdata(dev); |
| 1379 | |
| 1380 | return sprintf(buf, "%s\n", data->client.name); |
| 1381 | } |
| 1382 | |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1383 | /* --------------------------------------------------------------------- |
| 1384 | * Sysfs device attribute defines and structs |
| 1385 | * --------------------------------------------------------------------- */ |
| 1386 | |
| 1387 | /* Voltages 0-6 */ |
| 1388 | |
| 1389 | #define SENSOR_DEVICE_ATTR_IN(ix) \ |
| 1390 | static SENSOR_DEVICE_ATTR_2(in##ix##_input, S_IRUGO, \ |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 1391 | show_in, NULL, SYS_IN_INPUT, ix); \ |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1392 | static SENSOR_DEVICE_ATTR_2(in##ix##_min, S_IRUGO | S_IWUSR, \ |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 1393 | show_in, set_in, SYS_IN_MIN, ix); \ |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1394 | static SENSOR_DEVICE_ATTR_2(in##ix##_max, S_IRUGO | S_IWUSR, \ |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 1395 | show_in, set_in, SYS_IN_MAX, ix); \ |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1396 | static SENSOR_DEVICE_ATTR_2(in##ix##_alarm, S_IRUGO, \ |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 1397 | show_in, NULL, SYS_IN_ALARM, ix) |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1398 | |
| 1399 | SENSOR_DEVICE_ATTR_IN(0); |
| 1400 | SENSOR_DEVICE_ATTR_IN(1); |
| 1401 | SENSOR_DEVICE_ATTR_IN(2); |
| 1402 | SENSOR_DEVICE_ATTR_IN(3); |
| 1403 | SENSOR_DEVICE_ATTR_IN(4); |
| 1404 | SENSOR_DEVICE_ATTR_IN(5); |
| 1405 | SENSOR_DEVICE_ATTR_IN(6); |
| 1406 | |
| 1407 | /* Temperatures 1-3 */ |
| 1408 | |
| 1409 | #define SENSOR_DEVICE_ATTR_TEMP(ix) \ |
| 1410 | static SENSOR_DEVICE_ATTR_2(temp##ix##_input, S_IRUGO, \ |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 1411 | show_temp, NULL, SYS_TEMP_INPUT, ix-1); \ |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1412 | static SENSOR_DEVICE_ATTR_2(temp##ix##_min, S_IRUGO | S_IWUSR, \ |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 1413 | show_temp, set_temp, SYS_TEMP_MIN, ix-1); \ |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1414 | static SENSOR_DEVICE_ATTR_2(temp##ix##_max, S_IRUGO | S_IWUSR, \ |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 1415 | show_temp, set_temp, SYS_TEMP_MAX, ix-1); \ |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1416 | static SENSOR_DEVICE_ATTR_2(temp##ix##_offset, S_IRUGO, \ |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 1417 | show_temp, set_temp, SYS_TEMP_OFFSET, ix-1); \ |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1418 | static SENSOR_DEVICE_ATTR_2(temp##ix##_alarm, S_IRUGO, \ |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 1419 | show_temp, NULL, SYS_TEMP_ALARM, ix-1); \ |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1420 | static SENSOR_DEVICE_ATTR_2(temp##ix##_fault, S_IRUGO, \ |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 1421 | show_temp, NULL, SYS_TEMP_FAULT, ix-1) |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1422 | |
| 1423 | SENSOR_DEVICE_ATTR_TEMP(1); |
| 1424 | SENSOR_DEVICE_ATTR_TEMP(2); |
| 1425 | SENSOR_DEVICE_ATTR_TEMP(3); |
| 1426 | |
| 1427 | /* Zones 1-3 */ |
| 1428 | |
| 1429 | #define SENSOR_DEVICE_ATTR_ZONE(ix) \ |
| 1430 | static SENSOR_DEVICE_ATTR_2(zone##ix##_auto_channels_temp, S_IRUGO, \ |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 1431 | show_zone, NULL, SYS_ZONE_AUTO_CHANNELS_TEMP, ix-1); \ |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1432 | static SENSOR_DEVICE_ATTR_2(zone##ix##_auto_point1_temp_hyst, S_IRUGO, \ |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 1433 | show_zone, set_zone, SYS_ZONE_AUTO_POINT1_TEMP_HYST, ix-1); \ |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1434 | static SENSOR_DEVICE_ATTR_2(zone##ix##_auto_point1_temp, S_IRUGO, \ |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 1435 | show_zone, set_zone, SYS_ZONE_AUTO_POINT1_TEMP, ix-1); \ |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1436 | static SENSOR_DEVICE_ATTR_2(zone##ix##_auto_point2_temp, S_IRUGO, \ |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 1437 | show_zone, set_zone, SYS_ZONE_AUTO_POINT2_TEMP, ix-1); \ |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1438 | static SENSOR_DEVICE_ATTR_2(zone##ix##_auto_point3_temp, S_IRUGO, \ |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 1439 | show_zone, set_zone, SYS_ZONE_AUTO_POINT3_TEMP, ix-1) |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1440 | |
| 1441 | SENSOR_DEVICE_ATTR_ZONE(1); |
| 1442 | SENSOR_DEVICE_ATTR_ZONE(2); |
| 1443 | SENSOR_DEVICE_ATTR_ZONE(3); |
| 1444 | |
| 1445 | /* Fans 1-4 */ |
| 1446 | |
| 1447 | #define SENSOR_DEVICE_ATTR_FAN_1TO4(ix) \ |
| 1448 | static SENSOR_DEVICE_ATTR_2(fan##ix##_input, S_IRUGO, \ |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 1449 | show_fan, NULL, SYS_FAN_INPUT, ix-1); \ |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1450 | static SENSOR_DEVICE_ATTR_2(fan##ix##_min, S_IRUGO | S_IWUSR, \ |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 1451 | show_fan, set_fan, SYS_FAN_MIN, ix-1); \ |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1452 | static SENSOR_DEVICE_ATTR_2(fan##ix##_alarm, S_IRUGO, \ |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 1453 | show_fan, NULL, SYS_FAN_ALARM, ix-1); \ |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1454 | static SENSOR_DEVICE_ATTR_2(fan##ix##_type, S_IRUGO | S_IWUSR, \ |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 1455 | show_fan, set_fan, SYS_FAN_TYPE, ix-1) |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1456 | |
| 1457 | SENSOR_DEVICE_ATTR_FAN_1TO4(1); |
| 1458 | SENSOR_DEVICE_ATTR_FAN_1TO4(2); |
| 1459 | SENSOR_DEVICE_ATTR_FAN_1TO4(3); |
| 1460 | SENSOR_DEVICE_ATTR_FAN_1TO4(4); |
| 1461 | |
| 1462 | /* Fans 5-6 */ |
| 1463 | |
| 1464 | #define SENSOR_DEVICE_ATTR_FAN_5TO6(ix) \ |
| 1465 | static SENSOR_DEVICE_ATTR_2(fan##ix##_input, S_IRUGO, \ |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 1466 | show_fan, NULL, SYS_FAN_INPUT, ix-1); \ |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1467 | static SENSOR_DEVICE_ATTR_2(fan##ix##_min, S_IRUGO | S_IWUSR, \ |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 1468 | show_fan, set_fan, SYS_FAN_MIN, ix-1); \ |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1469 | static SENSOR_DEVICE_ATTR_2(fan##ix##_alarm, S_IRUGO, \ |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 1470 | show_fan, NULL, SYS_FAN_ALARM, ix-1); \ |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1471 | static SENSOR_DEVICE_ATTR_2(fan##ix##_max, S_IRUGO | S_IWUSR, \ |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 1472 | show_fan, set_fan, SYS_FAN_MAX, ix-1) |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1473 | |
| 1474 | SENSOR_DEVICE_ATTR_FAN_5TO6(5); |
| 1475 | SENSOR_DEVICE_ATTR_FAN_5TO6(6); |
| 1476 | |
| 1477 | /* PWMs 1-3 */ |
| 1478 | |
| 1479 | #define SENSOR_DEVICE_ATTR_PWM_1TO3(ix) \ |
| 1480 | static SENSOR_DEVICE_ATTR_2(pwm##ix, S_IRUGO, \ |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 1481 | show_pwm, set_pwm, SYS_PWM, ix-1); \ |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1482 | static SENSOR_DEVICE_ATTR_2(pwm##ix##_freq, S_IRUGO, \ |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 1483 | show_pwm, set_pwm, SYS_PWM_FREQ, ix-1); \ |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1484 | static SENSOR_DEVICE_ATTR_2(pwm##ix##_enable, S_IRUGO, \ |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 1485 | show_pwm, set_pwm, SYS_PWM_ENABLE, ix-1); \ |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1486 | static SENSOR_DEVICE_ATTR_2(pwm##ix##_ramp_rate, S_IRUGO, \ |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 1487 | show_pwm, set_pwm, SYS_PWM_RAMP_RATE, ix-1); \ |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1488 | static SENSOR_DEVICE_ATTR_2(pwm##ix##_auto_channels_zone, S_IRUGO, \ |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 1489 | show_pwm, set_pwm, SYS_PWM_AUTO_CHANNELS_ZONE, ix-1); \ |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1490 | static SENSOR_DEVICE_ATTR_2(pwm##ix##_auto_pwm_min, S_IRUGO, \ |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 1491 | show_pwm, set_pwm, SYS_PWM_AUTO_PWM_MIN, ix-1); \ |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1492 | static SENSOR_DEVICE_ATTR_2(pwm##ix##_auto_point1_pwm, S_IRUGO, \ |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 1493 | show_pwm, set_pwm, SYS_PWM_AUTO_POINT1_PWM, ix-1); \ |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1494 | static SENSOR_DEVICE_ATTR_2(pwm##ix##_auto_point2_pwm, S_IRUGO, \ |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 1495 | show_pwm, NULL, SYS_PWM_AUTO_POINT2_PWM, ix-1) |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1496 | |
| 1497 | SENSOR_DEVICE_ATTR_PWM_1TO3(1); |
| 1498 | SENSOR_DEVICE_ATTR_PWM_1TO3(2); |
| 1499 | SENSOR_DEVICE_ATTR_PWM_1TO3(3); |
| 1500 | |
| 1501 | /* PWMs 5-6 */ |
| 1502 | |
| 1503 | #define SENSOR_DEVICE_ATTR_PWM_5TO6(ix) \ |
Juerg Haefliger | 9b25771 | 2008-03-25 21:49:02 -0700 | [diff] [blame^] | 1504 | static SENSOR_DEVICE_ATTR_2(pwm##ix, S_IRUGO, \ |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 1505 | show_pwm, set_pwm, SYS_PWM, ix-1); \ |
Juerg Haefliger | 9b25771 | 2008-03-25 21:49:02 -0700 | [diff] [blame^] | 1506 | static SENSOR_DEVICE_ATTR_2(pwm##ix##_freq, S_IRUGO, \ |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 1507 | show_pwm, set_pwm, SYS_PWM_FREQ, ix-1); \ |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1508 | static SENSOR_DEVICE_ATTR_2(pwm##ix##_enable, S_IRUGO, \ |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 1509 | show_pwm, NULL, SYS_PWM_ENABLE, ix-1) |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1510 | |
| 1511 | SENSOR_DEVICE_ATTR_PWM_5TO6(5); |
| 1512 | SENSOR_DEVICE_ATTR_PWM_5TO6(6); |
| 1513 | |
| 1514 | /* Misc */ |
| 1515 | |
| 1516 | static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm); |
| 1517 | static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL); |
Juerg Haefliger | e95c237 | 2007-10-07 21:27:35 -0700 | [diff] [blame] | 1518 | static DEVICE_ATTR(name, S_IRUGO, show_name, NULL); /* for ISA devices */ |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1519 | |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1520 | /* This struct holds all the attributes that are always present and need to be |
| 1521 | * created unconditionally. The attributes that need modification of their |
| 1522 | * permissions are created read-only and write permissions are added or removed |
| 1523 | * on the fly when required */ |
| 1524 | static struct attribute *dme1737_attr[] ={ |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 1525 | /* Voltages */ |
Juerg Haefliger | 9b25771 | 2008-03-25 21:49:02 -0700 | [diff] [blame^] | 1526 | &sensor_dev_attr_in0_input.dev_attr.attr, |
| 1527 | &sensor_dev_attr_in0_min.dev_attr.attr, |
| 1528 | &sensor_dev_attr_in0_max.dev_attr.attr, |
| 1529 | &sensor_dev_attr_in0_alarm.dev_attr.attr, |
| 1530 | &sensor_dev_attr_in1_input.dev_attr.attr, |
| 1531 | &sensor_dev_attr_in1_min.dev_attr.attr, |
| 1532 | &sensor_dev_attr_in1_max.dev_attr.attr, |
| 1533 | &sensor_dev_attr_in1_alarm.dev_attr.attr, |
| 1534 | &sensor_dev_attr_in2_input.dev_attr.attr, |
| 1535 | &sensor_dev_attr_in2_min.dev_attr.attr, |
| 1536 | &sensor_dev_attr_in2_max.dev_attr.attr, |
| 1537 | &sensor_dev_attr_in2_alarm.dev_attr.attr, |
| 1538 | &sensor_dev_attr_in3_input.dev_attr.attr, |
| 1539 | &sensor_dev_attr_in3_min.dev_attr.attr, |
| 1540 | &sensor_dev_attr_in3_max.dev_attr.attr, |
| 1541 | &sensor_dev_attr_in3_alarm.dev_attr.attr, |
| 1542 | &sensor_dev_attr_in4_input.dev_attr.attr, |
| 1543 | &sensor_dev_attr_in4_min.dev_attr.attr, |
| 1544 | &sensor_dev_attr_in4_max.dev_attr.attr, |
| 1545 | &sensor_dev_attr_in4_alarm.dev_attr.attr, |
| 1546 | &sensor_dev_attr_in5_input.dev_attr.attr, |
| 1547 | &sensor_dev_attr_in5_min.dev_attr.attr, |
| 1548 | &sensor_dev_attr_in5_max.dev_attr.attr, |
| 1549 | &sensor_dev_attr_in5_alarm.dev_attr.attr, |
| 1550 | &sensor_dev_attr_in6_input.dev_attr.attr, |
| 1551 | &sensor_dev_attr_in6_min.dev_attr.attr, |
| 1552 | &sensor_dev_attr_in6_max.dev_attr.attr, |
| 1553 | &sensor_dev_attr_in6_alarm.dev_attr.attr, |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 1554 | /* Temperatures */ |
Juerg Haefliger | 9b25771 | 2008-03-25 21:49:02 -0700 | [diff] [blame^] | 1555 | &sensor_dev_attr_temp1_input.dev_attr.attr, |
| 1556 | &sensor_dev_attr_temp1_min.dev_attr.attr, |
| 1557 | &sensor_dev_attr_temp1_max.dev_attr.attr, |
| 1558 | &sensor_dev_attr_temp1_alarm.dev_attr.attr, |
| 1559 | &sensor_dev_attr_temp1_fault.dev_attr.attr, |
| 1560 | &sensor_dev_attr_temp1_offset.dev_attr.attr, |
| 1561 | &sensor_dev_attr_temp2_input.dev_attr.attr, |
| 1562 | &sensor_dev_attr_temp2_min.dev_attr.attr, |
| 1563 | &sensor_dev_attr_temp2_max.dev_attr.attr, |
| 1564 | &sensor_dev_attr_temp2_alarm.dev_attr.attr, |
| 1565 | &sensor_dev_attr_temp2_fault.dev_attr.attr, |
| 1566 | &sensor_dev_attr_temp2_offset.dev_attr.attr, |
| 1567 | &sensor_dev_attr_temp3_input.dev_attr.attr, |
| 1568 | &sensor_dev_attr_temp3_min.dev_attr.attr, |
| 1569 | &sensor_dev_attr_temp3_max.dev_attr.attr, |
| 1570 | &sensor_dev_attr_temp3_alarm.dev_attr.attr, |
| 1571 | &sensor_dev_attr_temp3_fault.dev_attr.attr, |
| 1572 | &sensor_dev_attr_temp3_offset.dev_attr.attr, |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 1573 | /* Zones */ |
Juerg Haefliger | 9b25771 | 2008-03-25 21:49:02 -0700 | [diff] [blame^] | 1574 | &sensor_dev_attr_zone1_auto_point1_temp_hyst.dev_attr.attr, |
| 1575 | &sensor_dev_attr_zone1_auto_point1_temp.dev_attr.attr, |
| 1576 | &sensor_dev_attr_zone1_auto_point2_temp.dev_attr.attr, |
| 1577 | &sensor_dev_attr_zone1_auto_point3_temp.dev_attr.attr, |
| 1578 | &sensor_dev_attr_zone1_auto_channels_temp.dev_attr.attr, |
| 1579 | &sensor_dev_attr_zone2_auto_point1_temp_hyst.dev_attr.attr, |
| 1580 | &sensor_dev_attr_zone2_auto_point1_temp.dev_attr.attr, |
| 1581 | &sensor_dev_attr_zone2_auto_point2_temp.dev_attr.attr, |
| 1582 | &sensor_dev_attr_zone2_auto_point3_temp.dev_attr.attr, |
| 1583 | &sensor_dev_attr_zone2_auto_channels_temp.dev_attr.attr, |
| 1584 | &sensor_dev_attr_zone3_auto_point1_temp_hyst.dev_attr.attr, |
| 1585 | &sensor_dev_attr_zone3_auto_point1_temp.dev_attr.attr, |
| 1586 | &sensor_dev_attr_zone3_auto_point2_temp.dev_attr.attr, |
| 1587 | &sensor_dev_attr_zone3_auto_point3_temp.dev_attr.attr, |
| 1588 | &sensor_dev_attr_zone3_auto_channels_temp.dev_attr.attr, |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 1589 | /* Misc */ |
| 1590 | &dev_attr_vrm.attr, |
| 1591 | &dev_attr_cpu0_vid.attr, |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1592 | NULL |
| 1593 | }; |
| 1594 | |
| 1595 | static const struct attribute_group dme1737_group = { |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 1596 | .attrs = dme1737_attr, |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1597 | }; |
| 1598 | |
| 1599 | /* The following structs hold the PWM attributes, some of which are optional. |
| 1600 | * Their creation depends on the chip configuration which is determined during |
| 1601 | * module load. */ |
| 1602 | static struct attribute *dme1737_attr_pwm1[] = { |
Juerg Haefliger | 9b25771 | 2008-03-25 21:49:02 -0700 | [diff] [blame^] | 1603 | &sensor_dev_attr_pwm1.dev_attr.attr, |
| 1604 | &sensor_dev_attr_pwm1_freq.dev_attr.attr, |
| 1605 | &sensor_dev_attr_pwm1_enable.dev_attr.attr, |
| 1606 | &sensor_dev_attr_pwm1_ramp_rate.dev_attr.attr, |
| 1607 | &sensor_dev_attr_pwm1_auto_channels_zone.dev_attr.attr, |
| 1608 | &sensor_dev_attr_pwm1_auto_pwm_min.dev_attr.attr, |
| 1609 | &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr, |
| 1610 | &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr, |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1611 | NULL |
| 1612 | }; |
| 1613 | static struct attribute *dme1737_attr_pwm2[] = { |
Juerg Haefliger | 9b25771 | 2008-03-25 21:49:02 -0700 | [diff] [blame^] | 1614 | &sensor_dev_attr_pwm2.dev_attr.attr, |
| 1615 | &sensor_dev_attr_pwm2_freq.dev_attr.attr, |
| 1616 | &sensor_dev_attr_pwm2_enable.dev_attr.attr, |
| 1617 | &sensor_dev_attr_pwm2_ramp_rate.dev_attr.attr, |
| 1618 | &sensor_dev_attr_pwm2_auto_channels_zone.dev_attr.attr, |
| 1619 | &sensor_dev_attr_pwm2_auto_pwm_min.dev_attr.attr, |
| 1620 | &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr, |
| 1621 | &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr, |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1622 | NULL |
| 1623 | }; |
| 1624 | static struct attribute *dme1737_attr_pwm3[] = { |
Juerg Haefliger | 9b25771 | 2008-03-25 21:49:02 -0700 | [diff] [blame^] | 1625 | &sensor_dev_attr_pwm3.dev_attr.attr, |
| 1626 | &sensor_dev_attr_pwm3_freq.dev_attr.attr, |
| 1627 | &sensor_dev_attr_pwm3_enable.dev_attr.attr, |
| 1628 | &sensor_dev_attr_pwm3_ramp_rate.dev_attr.attr, |
| 1629 | &sensor_dev_attr_pwm3_auto_channels_zone.dev_attr.attr, |
| 1630 | &sensor_dev_attr_pwm3_auto_pwm_min.dev_attr.attr, |
| 1631 | &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr, |
| 1632 | &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr, |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1633 | NULL |
| 1634 | }; |
| 1635 | static struct attribute *dme1737_attr_pwm5[] = { |
Juerg Haefliger | 9b25771 | 2008-03-25 21:49:02 -0700 | [diff] [blame^] | 1636 | &sensor_dev_attr_pwm5.dev_attr.attr, |
| 1637 | &sensor_dev_attr_pwm5_freq.dev_attr.attr, |
| 1638 | &sensor_dev_attr_pwm5_enable.dev_attr.attr, |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1639 | NULL |
| 1640 | }; |
| 1641 | static struct attribute *dme1737_attr_pwm6[] = { |
Juerg Haefliger | 9b25771 | 2008-03-25 21:49:02 -0700 | [diff] [blame^] | 1642 | &sensor_dev_attr_pwm6.dev_attr.attr, |
| 1643 | &sensor_dev_attr_pwm6_freq.dev_attr.attr, |
| 1644 | &sensor_dev_attr_pwm6_enable.dev_attr.attr, |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1645 | NULL |
| 1646 | }; |
| 1647 | |
| 1648 | static const struct attribute_group dme1737_pwm_group[] = { |
| 1649 | { .attrs = dme1737_attr_pwm1 }, |
| 1650 | { .attrs = dme1737_attr_pwm2 }, |
| 1651 | { .attrs = dme1737_attr_pwm3 }, |
| 1652 | { .attrs = NULL }, |
| 1653 | { .attrs = dme1737_attr_pwm5 }, |
| 1654 | { .attrs = dme1737_attr_pwm6 }, |
| 1655 | }; |
| 1656 | |
| 1657 | /* The following structs hold the fan attributes, some of which are optional. |
| 1658 | * Their creation depends on the chip configuration which is determined during |
| 1659 | * module load. */ |
| 1660 | static struct attribute *dme1737_attr_fan1[] = { |
Juerg Haefliger | 9b25771 | 2008-03-25 21:49:02 -0700 | [diff] [blame^] | 1661 | &sensor_dev_attr_fan1_input.dev_attr.attr, |
| 1662 | &sensor_dev_attr_fan1_min.dev_attr.attr, |
| 1663 | &sensor_dev_attr_fan1_alarm.dev_attr.attr, |
| 1664 | &sensor_dev_attr_fan1_type.dev_attr.attr, |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1665 | NULL |
| 1666 | }; |
| 1667 | static struct attribute *dme1737_attr_fan2[] = { |
Juerg Haefliger | 9b25771 | 2008-03-25 21:49:02 -0700 | [diff] [blame^] | 1668 | &sensor_dev_attr_fan2_input.dev_attr.attr, |
| 1669 | &sensor_dev_attr_fan2_min.dev_attr.attr, |
| 1670 | &sensor_dev_attr_fan2_alarm.dev_attr.attr, |
| 1671 | &sensor_dev_attr_fan2_type.dev_attr.attr, |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1672 | NULL |
| 1673 | }; |
| 1674 | static struct attribute *dme1737_attr_fan3[] = { |
Juerg Haefliger | 9b25771 | 2008-03-25 21:49:02 -0700 | [diff] [blame^] | 1675 | &sensor_dev_attr_fan3_input.dev_attr.attr, |
| 1676 | &sensor_dev_attr_fan3_min.dev_attr.attr, |
| 1677 | &sensor_dev_attr_fan3_alarm.dev_attr.attr, |
| 1678 | &sensor_dev_attr_fan3_type.dev_attr.attr, |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1679 | NULL |
| 1680 | }; |
| 1681 | static struct attribute *dme1737_attr_fan4[] = { |
Juerg Haefliger | 9b25771 | 2008-03-25 21:49:02 -0700 | [diff] [blame^] | 1682 | &sensor_dev_attr_fan4_input.dev_attr.attr, |
| 1683 | &sensor_dev_attr_fan4_min.dev_attr.attr, |
| 1684 | &sensor_dev_attr_fan4_alarm.dev_attr.attr, |
| 1685 | &sensor_dev_attr_fan4_type.dev_attr.attr, |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1686 | NULL |
| 1687 | }; |
| 1688 | static struct attribute *dme1737_attr_fan5[] = { |
Juerg Haefliger | 9b25771 | 2008-03-25 21:49:02 -0700 | [diff] [blame^] | 1689 | &sensor_dev_attr_fan5_input.dev_attr.attr, |
| 1690 | &sensor_dev_attr_fan5_min.dev_attr.attr, |
| 1691 | &sensor_dev_attr_fan5_alarm.dev_attr.attr, |
| 1692 | &sensor_dev_attr_fan5_max.dev_attr.attr, |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1693 | NULL |
| 1694 | }; |
| 1695 | static struct attribute *dme1737_attr_fan6[] = { |
Juerg Haefliger | 9b25771 | 2008-03-25 21:49:02 -0700 | [diff] [blame^] | 1696 | &sensor_dev_attr_fan6_input.dev_attr.attr, |
| 1697 | &sensor_dev_attr_fan6_min.dev_attr.attr, |
| 1698 | &sensor_dev_attr_fan6_alarm.dev_attr.attr, |
| 1699 | &sensor_dev_attr_fan6_max.dev_attr.attr, |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1700 | NULL |
| 1701 | }; |
| 1702 | |
| 1703 | static const struct attribute_group dme1737_fan_group[] = { |
| 1704 | { .attrs = dme1737_attr_fan1 }, |
| 1705 | { .attrs = dme1737_attr_fan2 }, |
| 1706 | { .attrs = dme1737_attr_fan3 }, |
| 1707 | { .attrs = dme1737_attr_fan4 }, |
| 1708 | { .attrs = dme1737_attr_fan5 }, |
| 1709 | { .attrs = dme1737_attr_fan6 }, |
| 1710 | }; |
| 1711 | |
| 1712 | /* The permissions of all of the following attributes are changed to read- |
| 1713 | * writeable if the chip is *not* locked. Otherwise they stay read-only. */ |
| 1714 | static struct attribute *dme1737_attr_lock[] = { |
| 1715 | /* Temperatures */ |
Juerg Haefliger | 9b25771 | 2008-03-25 21:49:02 -0700 | [diff] [blame^] | 1716 | &sensor_dev_attr_temp1_offset.dev_attr.attr, |
| 1717 | &sensor_dev_attr_temp2_offset.dev_attr.attr, |
| 1718 | &sensor_dev_attr_temp3_offset.dev_attr.attr, |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1719 | /* Zones */ |
Juerg Haefliger | 9b25771 | 2008-03-25 21:49:02 -0700 | [diff] [blame^] | 1720 | &sensor_dev_attr_zone1_auto_point1_temp_hyst.dev_attr.attr, |
| 1721 | &sensor_dev_attr_zone1_auto_point1_temp.dev_attr.attr, |
| 1722 | &sensor_dev_attr_zone1_auto_point2_temp.dev_attr.attr, |
| 1723 | &sensor_dev_attr_zone1_auto_point3_temp.dev_attr.attr, |
| 1724 | &sensor_dev_attr_zone2_auto_point1_temp_hyst.dev_attr.attr, |
| 1725 | &sensor_dev_attr_zone2_auto_point1_temp.dev_attr.attr, |
| 1726 | &sensor_dev_attr_zone2_auto_point2_temp.dev_attr.attr, |
| 1727 | &sensor_dev_attr_zone2_auto_point3_temp.dev_attr.attr, |
| 1728 | &sensor_dev_attr_zone3_auto_point1_temp_hyst.dev_attr.attr, |
| 1729 | &sensor_dev_attr_zone3_auto_point1_temp.dev_attr.attr, |
| 1730 | &sensor_dev_attr_zone3_auto_point2_temp.dev_attr.attr, |
| 1731 | &sensor_dev_attr_zone3_auto_point3_temp.dev_attr.attr, |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1732 | NULL |
| 1733 | }; |
| 1734 | |
| 1735 | static const struct attribute_group dme1737_lock_group = { |
| 1736 | .attrs = dme1737_attr_lock, |
| 1737 | }; |
| 1738 | |
| 1739 | /* The permissions of the following PWM attributes are changed to read- |
| 1740 | * writeable if the chip is *not* locked and the respective PWM is available. |
| 1741 | * Otherwise they stay read-only. */ |
| 1742 | static struct attribute *dme1737_attr_pwm1_lock[] = { |
Juerg Haefliger | 9b25771 | 2008-03-25 21:49:02 -0700 | [diff] [blame^] | 1743 | &sensor_dev_attr_pwm1_freq.dev_attr.attr, |
| 1744 | &sensor_dev_attr_pwm1_enable.dev_attr.attr, |
| 1745 | &sensor_dev_attr_pwm1_ramp_rate.dev_attr.attr, |
| 1746 | &sensor_dev_attr_pwm1_auto_channels_zone.dev_attr.attr, |
| 1747 | &sensor_dev_attr_pwm1_auto_pwm_min.dev_attr.attr, |
| 1748 | &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr, |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1749 | NULL |
| 1750 | }; |
| 1751 | static struct attribute *dme1737_attr_pwm2_lock[] = { |
Juerg Haefliger | 9b25771 | 2008-03-25 21:49:02 -0700 | [diff] [blame^] | 1752 | &sensor_dev_attr_pwm2_freq.dev_attr.attr, |
| 1753 | &sensor_dev_attr_pwm2_enable.dev_attr.attr, |
| 1754 | &sensor_dev_attr_pwm2_ramp_rate.dev_attr.attr, |
| 1755 | &sensor_dev_attr_pwm2_auto_channels_zone.dev_attr.attr, |
| 1756 | &sensor_dev_attr_pwm2_auto_pwm_min.dev_attr.attr, |
| 1757 | &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr, |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1758 | NULL |
| 1759 | }; |
| 1760 | static struct attribute *dme1737_attr_pwm3_lock[] = { |
Juerg Haefliger | 9b25771 | 2008-03-25 21:49:02 -0700 | [diff] [blame^] | 1761 | &sensor_dev_attr_pwm3_freq.dev_attr.attr, |
| 1762 | &sensor_dev_attr_pwm3_enable.dev_attr.attr, |
| 1763 | &sensor_dev_attr_pwm3_ramp_rate.dev_attr.attr, |
| 1764 | &sensor_dev_attr_pwm3_auto_channels_zone.dev_attr.attr, |
| 1765 | &sensor_dev_attr_pwm3_auto_pwm_min.dev_attr.attr, |
| 1766 | &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr, |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1767 | NULL |
| 1768 | }; |
| 1769 | static struct attribute *dme1737_attr_pwm5_lock[] = { |
Juerg Haefliger | 9b25771 | 2008-03-25 21:49:02 -0700 | [diff] [blame^] | 1770 | &sensor_dev_attr_pwm5.dev_attr.attr, |
| 1771 | &sensor_dev_attr_pwm5_freq.dev_attr.attr, |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1772 | NULL |
| 1773 | }; |
| 1774 | static struct attribute *dme1737_attr_pwm6_lock[] = { |
Juerg Haefliger | 9b25771 | 2008-03-25 21:49:02 -0700 | [diff] [blame^] | 1775 | &sensor_dev_attr_pwm6.dev_attr.attr, |
| 1776 | &sensor_dev_attr_pwm6_freq.dev_attr.attr, |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1777 | NULL |
| 1778 | }; |
| 1779 | |
| 1780 | static const struct attribute_group dme1737_pwm_lock_group[] = { |
| 1781 | { .attrs = dme1737_attr_pwm1_lock }, |
| 1782 | { .attrs = dme1737_attr_pwm2_lock }, |
| 1783 | { .attrs = dme1737_attr_pwm3_lock }, |
| 1784 | { .attrs = NULL }, |
| 1785 | { .attrs = dme1737_attr_pwm5_lock }, |
| 1786 | { .attrs = dme1737_attr_pwm6_lock }, |
| 1787 | }; |
| 1788 | |
| 1789 | /* Pwm[1-3] are read-writeable if the associated pwm is in manual mode and the |
| 1790 | * chip is not locked. Otherwise they are read-only. */ |
| 1791 | static struct attribute *dme1737_attr_pwm[] = { |
| 1792 | &sensor_dev_attr_pwm1.dev_attr.attr, |
| 1793 | &sensor_dev_attr_pwm2.dev_attr.attr, |
| 1794 | &sensor_dev_attr_pwm3.dev_attr.attr, |
| 1795 | }; |
| 1796 | |
| 1797 | /* --------------------------------------------------------------------- |
| 1798 | * Super-IO functions |
| 1799 | * --------------------------------------------------------------------- */ |
| 1800 | |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 1801 | static inline void dme1737_sio_enter(int sio_cip) |
| 1802 | { |
| 1803 | outb(0x55, sio_cip); |
| 1804 | } |
| 1805 | |
| 1806 | static inline void dme1737_sio_exit(int sio_cip) |
| 1807 | { |
| 1808 | outb(0xaa, sio_cip); |
| 1809 | } |
| 1810 | |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1811 | static inline int dme1737_sio_inb(int sio_cip, int reg) |
| 1812 | { |
| 1813 | outb(reg, sio_cip); |
| 1814 | return inb(sio_cip + 1); |
| 1815 | } |
| 1816 | |
| 1817 | static inline void dme1737_sio_outb(int sio_cip, int reg, int val) |
| 1818 | { |
| 1819 | outb(reg, sio_cip); |
| 1820 | outb(val, sio_cip + 1); |
| 1821 | } |
| 1822 | |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1823 | /* --------------------------------------------------------------------- |
Juerg Haefliger | e95c237 | 2007-10-07 21:27:35 -0700 | [diff] [blame] | 1824 | * Device initialization |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1825 | * --------------------------------------------------------------------- */ |
| 1826 | |
Juerg Haefliger | 67e2f32 | 2007-10-01 21:20:28 -0700 | [diff] [blame] | 1827 | static int dme1737_i2c_get_features(int, struct dme1737_data*); |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1828 | |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 1829 | static void dme1737_chmod_file(struct device *dev, |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1830 | struct attribute *attr, mode_t mode) |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1831 | { |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 1832 | if (sysfs_chmod_file(&dev->kobj, attr, mode)) { |
| 1833 | dev_warn(dev, "Failed to change permissions of %s.\n", |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1834 | attr->name); |
| 1835 | } |
| 1836 | } |
| 1837 | |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 1838 | static void dme1737_chmod_group(struct device *dev, |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1839 | const struct attribute_group *group, |
| 1840 | mode_t mode) |
| 1841 | { |
| 1842 | struct attribute **attr; |
| 1843 | |
| 1844 | for (attr = group->attrs; *attr; attr++) { |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 1845 | dme1737_chmod_file(dev, *attr, mode); |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1846 | } |
| 1847 | } |
| 1848 | |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 1849 | static void dme1737_remove_files(struct device *dev) |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1850 | { |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 1851 | struct dme1737_data *data = dev_get_drvdata(dev); |
| 1852 | int ix; |
| 1853 | |
| 1854 | for (ix = 0; ix < ARRAY_SIZE(dme1737_fan_group); ix++) { |
| 1855 | if (data->has_fan & (1 << ix)) { |
| 1856 | sysfs_remove_group(&dev->kobj, |
| 1857 | &dme1737_fan_group[ix]); |
| 1858 | } |
| 1859 | } |
| 1860 | |
| 1861 | for (ix = 0; ix < ARRAY_SIZE(dme1737_pwm_group); ix++) { |
| 1862 | if (data->has_pwm & (1 << ix)) { |
| 1863 | sysfs_remove_group(&dev->kobj, |
| 1864 | &dme1737_pwm_group[ix]); |
| 1865 | } |
| 1866 | } |
| 1867 | |
| 1868 | sysfs_remove_group(&dev->kobj, &dme1737_group); |
Juerg Haefliger | e95c237 | 2007-10-07 21:27:35 -0700 | [diff] [blame] | 1869 | |
| 1870 | if (!data->client.driver) { |
| 1871 | sysfs_remove_file(&dev->kobj, &dev_attr_name.attr); |
| 1872 | } |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 1873 | } |
| 1874 | |
| 1875 | static int dme1737_create_files(struct device *dev) |
| 1876 | { |
| 1877 | struct dme1737_data *data = dev_get_drvdata(dev); |
| 1878 | int err, ix; |
| 1879 | |
Juerg Haefliger | e95c237 | 2007-10-07 21:27:35 -0700 | [diff] [blame] | 1880 | /* Create a name attribute for ISA devices */ |
| 1881 | if (!data->client.driver && |
| 1882 | (err = sysfs_create_file(&dev->kobj, &dev_attr_name.attr))) { |
| 1883 | goto exit; |
| 1884 | } |
| 1885 | |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 1886 | /* Create standard sysfs attributes */ |
| 1887 | if ((err = sysfs_create_group(&dev->kobj, &dme1737_group))) { |
Juerg Haefliger | e95c237 | 2007-10-07 21:27:35 -0700 | [diff] [blame] | 1888 | goto exit_remove; |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 1889 | } |
| 1890 | |
| 1891 | /* Create fan sysfs attributes */ |
| 1892 | for (ix = 0; ix < ARRAY_SIZE(dme1737_fan_group); ix++) { |
| 1893 | if (data->has_fan & (1 << ix)) { |
| 1894 | if ((err = sysfs_create_group(&dev->kobj, |
| 1895 | &dme1737_fan_group[ix]))) { |
| 1896 | goto exit_remove; |
| 1897 | } |
| 1898 | } |
| 1899 | } |
| 1900 | |
| 1901 | /* Create PWM sysfs attributes */ |
| 1902 | for (ix = 0; ix < ARRAY_SIZE(dme1737_pwm_group); ix++) { |
| 1903 | if (data->has_pwm & (1 << ix)) { |
| 1904 | if ((err = sysfs_create_group(&dev->kobj, |
| 1905 | &dme1737_pwm_group[ix]))) { |
| 1906 | goto exit_remove; |
| 1907 | } |
| 1908 | } |
| 1909 | } |
| 1910 | |
| 1911 | /* Inform if the device is locked. Otherwise change the permissions of |
| 1912 | * selected attributes from read-only to read-writeable. */ |
| 1913 | if (data->config & 0x02) { |
| 1914 | dev_info(dev, "Device is locked. Some attributes " |
| 1915 | "will be read-only.\n"); |
| 1916 | } else { |
| 1917 | /* Change permissions of standard attributes */ |
| 1918 | dme1737_chmod_group(dev, &dme1737_lock_group, |
| 1919 | S_IRUGO | S_IWUSR); |
| 1920 | |
| 1921 | /* Change permissions of PWM attributes */ |
| 1922 | for (ix = 0; ix < ARRAY_SIZE(dme1737_pwm_lock_group); ix++) { |
| 1923 | if (data->has_pwm & (1 << ix)) { |
| 1924 | dme1737_chmod_group(dev, |
| 1925 | &dme1737_pwm_lock_group[ix], |
| 1926 | S_IRUGO | S_IWUSR); |
| 1927 | } |
| 1928 | } |
| 1929 | |
| 1930 | /* Change permissions of pwm[1-3] if in manual mode */ |
| 1931 | for (ix = 0; ix < 3; ix++) { |
| 1932 | if ((data->has_pwm & (1 << ix)) && |
| 1933 | (PWM_EN_FROM_REG(data->pwm_config[ix]) == 1)) { |
| 1934 | dme1737_chmod_file(dev, |
| 1935 | dme1737_attr_pwm[ix], |
| 1936 | S_IRUGO | S_IWUSR); |
| 1937 | } |
| 1938 | } |
| 1939 | } |
| 1940 | |
| 1941 | return 0; |
| 1942 | |
| 1943 | exit_remove: |
| 1944 | dme1737_remove_files(dev); |
| 1945 | exit: |
| 1946 | return err; |
| 1947 | } |
| 1948 | |
| 1949 | static int dme1737_init_device(struct device *dev) |
| 1950 | { |
| 1951 | struct dme1737_data *data = dev_get_drvdata(dev); |
| 1952 | struct i2c_client *client = &data->client; |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1953 | int ix; |
| 1954 | u8 reg; |
| 1955 | |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 1956 | data->config = dme1737_read(client, DME1737_REG_CONFIG); |
| 1957 | /* Inform if part is not monitoring/started */ |
| 1958 | if (!(data->config & 0x01)) { |
| 1959 | if (!force_start) { |
| 1960 | dev_err(dev, "Device is not monitoring. " |
| 1961 | "Use the force_start load parameter to " |
| 1962 | "override.\n"); |
| 1963 | return -EFAULT; |
| 1964 | } |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1965 | |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 1966 | /* Force monitoring */ |
| 1967 | data->config |= 0x01; |
| 1968 | dme1737_write(client, DME1737_REG_CONFIG, data->config); |
| 1969 | } |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1970 | /* Inform if part is not ready */ |
| 1971 | if (!(data->config & 0x04)) { |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 1972 | dev_err(dev, "Device is not ready.\n"); |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1973 | return -EFAULT; |
| 1974 | } |
| 1975 | |
Juerg Haefliger | e95c237 | 2007-10-07 21:27:35 -0700 | [diff] [blame] | 1976 | /* Determine which optional fan and pwm features are enabled/present */ |
| 1977 | if (client->driver) { /* I2C chip */ |
| 1978 | data->config2 = dme1737_read(client, DME1737_REG_CONFIG2); |
| 1979 | /* Check if optional fan3 input is enabled */ |
| 1980 | if (data->config2 & 0x04) { |
| 1981 | data->has_fan |= (1 << 2); |
| 1982 | } |
| 1983 | |
| 1984 | /* Fan4 and pwm3 are only available if the client's I2C address |
| 1985 | * is the default 0x2e. Otherwise the I/Os associated with |
| 1986 | * these functions are used for addr enable/select. */ |
| 1987 | if (data->client.addr == 0x2e) { |
| 1988 | data->has_fan |= (1 << 3); |
| 1989 | data->has_pwm |= (1 << 2); |
| 1990 | } |
| 1991 | |
| 1992 | /* Determine which of the optional fan[5-6] and pwm[5-6] |
| 1993 | * features are enabled. For this, we need to query the runtime |
| 1994 | * registers through the Super-IO LPC interface. Try both |
| 1995 | * config ports 0x2e and 0x4e. */ |
| 1996 | if (dme1737_i2c_get_features(0x2e, data) && |
| 1997 | dme1737_i2c_get_features(0x4e, data)) { |
| 1998 | dev_warn(dev, "Failed to query Super-IO for optional " |
| 1999 | "features.\n"); |
| 2000 | } |
| 2001 | } else { /* ISA chip */ |
| 2002 | /* Fan3 and pwm3 are always available. Fan[4-5] and pwm[5-6] |
| 2003 | * don't exist in the ISA chip. */ |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 2004 | data->has_fan |= (1 << 2); |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 2005 | data->has_pwm |= (1 << 2); |
| 2006 | } |
| 2007 | |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 2008 | /* Fan1, fan2, pwm1, and pwm2 are always present */ |
| 2009 | data->has_fan |= 0x03; |
| 2010 | data->has_pwm |= 0x03; |
| 2011 | |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 2012 | dev_info(dev, "Optional features: pwm3=%s, pwm5=%s, pwm6=%s, " |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 2013 | "fan3=%s, fan4=%s, fan5=%s, fan6=%s.\n", |
| 2014 | (data->has_pwm & (1 << 2)) ? "yes" : "no", |
| 2015 | (data->has_pwm & (1 << 4)) ? "yes" : "no", |
| 2016 | (data->has_pwm & (1 << 5)) ? "yes" : "no", |
| 2017 | (data->has_fan & (1 << 2)) ? "yes" : "no", |
| 2018 | (data->has_fan & (1 << 3)) ? "yes" : "no", |
| 2019 | (data->has_fan & (1 << 4)) ? "yes" : "no", |
| 2020 | (data->has_fan & (1 << 5)) ? "yes" : "no"); |
| 2021 | |
| 2022 | reg = dme1737_read(client, DME1737_REG_TACH_PWM); |
| 2023 | /* Inform if fan-to-pwm mapping differs from the default */ |
Juerg Haefliger | e95c237 | 2007-10-07 21:27:35 -0700 | [diff] [blame] | 2024 | if (client->driver && reg != 0xa4) { /* I2C chip */ |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 2025 | dev_warn(dev, "Non-standard fan to pwm mapping: " |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 2026 | "fan1->pwm%d, fan2->pwm%d, fan3->pwm%d, " |
| 2027 | "fan4->pwm%d. Please report to the driver " |
| 2028 | "maintainer.\n", |
| 2029 | (reg & 0x03) + 1, ((reg >> 2) & 0x03) + 1, |
| 2030 | ((reg >> 4) & 0x03) + 1, ((reg >> 6) & 0x03) + 1); |
Juerg Haefliger | e95c237 | 2007-10-07 21:27:35 -0700 | [diff] [blame] | 2031 | } else if (!client->driver && reg != 0x24) { /* ISA chip */ |
| 2032 | dev_warn(dev, "Non-standard fan to pwm mapping: " |
| 2033 | "fan1->pwm%d, fan2->pwm%d, fan3->pwm%d. " |
| 2034 | "Please report to the driver maintainer.\n", |
| 2035 | (reg & 0x03) + 1, ((reg >> 2) & 0x03) + 1, |
| 2036 | ((reg >> 4) & 0x03) + 1); |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 2037 | } |
| 2038 | |
| 2039 | /* Switch pwm[1-3] to manual mode if they are currently disabled and |
| 2040 | * set the duty-cycles to 0% (which is identical to the PWMs being |
| 2041 | * disabled). */ |
| 2042 | if (!(data->config & 0x02)) { |
| 2043 | for (ix = 0; ix < 3; ix++) { |
| 2044 | data->pwm_config[ix] = dme1737_read(client, |
| 2045 | DME1737_REG_PWM_CONFIG(ix)); |
| 2046 | if ((data->has_pwm & (1 << ix)) && |
| 2047 | (PWM_EN_FROM_REG(data->pwm_config[ix]) == -1)) { |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 2048 | dev_info(dev, "Switching pwm%d to " |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 2049 | "manual mode.\n", ix + 1); |
| 2050 | data->pwm_config[ix] = PWM_EN_TO_REG(1, |
| 2051 | data->pwm_config[ix]); |
| 2052 | dme1737_write(client, DME1737_REG_PWM(ix), 0); |
| 2053 | dme1737_write(client, |
| 2054 | DME1737_REG_PWM_CONFIG(ix), |
| 2055 | data->pwm_config[ix]); |
| 2056 | } |
| 2057 | } |
| 2058 | } |
| 2059 | |
| 2060 | /* Initialize the default PWM auto channels zone (acz) assignments */ |
| 2061 | data->pwm_acz[0] = 1; /* pwm1 -> zone1 */ |
| 2062 | data->pwm_acz[1] = 2; /* pwm2 -> zone2 */ |
| 2063 | data->pwm_acz[2] = 4; /* pwm3 -> zone3 */ |
| 2064 | |
| 2065 | /* Set VRM */ |
| 2066 | data->vrm = vid_which_vrm(); |
| 2067 | |
| 2068 | return 0; |
| 2069 | } |
| 2070 | |
Juerg Haefliger | 67e2f32 | 2007-10-01 21:20:28 -0700 | [diff] [blame] | 2071 | /* --------------------------------------------------------------------- |
| 2072 | * I2C device detection and registration |
| 2073 | * --------------------------------------------------------------------- */ |
| 2074 | |
| 2075 | static struct i2c_driver dme1737_i2c_driver; |
| 2076 | |
| 2077 | static int dme1737_i2c_get_features(int sio_cip, struct dme1737_data *data) |
| 2078 | { |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 2079 | int err = 0, reg; |
| 2080 | u16 addr; |
| 2081 | |
Juerg Haefliger | 67e2f32 | 2007-10-01 21:20:28 -0700 | [diff] [blame] | 2082 | dme1737_sio_enter(sio_cip); |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 2083 | |
| 2084 | /* Check device ID |
| 2085 | * The DME1737 can return either 0x78 or 0x77 as its device ID. */ |
Juerg Haefliger | 345a222 | 2008-01-26 08:54:24 -0800 | [diff] [blame] | 2086 | reg = force_id ? force_id : dme1737_sio_inb(sio_cip, 0x20); |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 2087 | if (!(reg == 0x77 || reg == 0x78)) { |
| 2088 | err = -ENODEV; |
| 2089 | goto exit; |
| 2090 | } |
| 2091 | |
| 2092 | /* Select logical device A (runtime registers) */ |
| 2093 | dme1737_sio_outb(sio_cip, 0x07, 0x0a); |
| 2094 | |
| 2095 | /* Get the base address of the runtime registers */ |
| 2096 | if (!(addr = (dme1737_sio_inb(sio_cip, 0x60) << 8) | |
| 2097 | dme1737_sio_inb(sio_cip, 0x61))) { |
| 2098 | err = -ENODEV; |
| 2099 | goto exit; |
| 2100 | } |
| 2101 | |
| 2102 | /* Read the runtime registers to determine which optional features |
| 2103 | * are enabled and available. Bits [3:2] of registers 0x43-0x46 are set |
| 2104 | * to '10' if the respective feature is enabled. */ |
| 2105 | if ((inb(addr + 0x43) & 0x0c) == 0x08) { /* fan6 */ |
| 2106 | data->has_fan |= (1 << 5); |
| 2107 | } |
| 2108 | if ((inb(addr + 0x44) & 0x0c) == 0x08) { /* pwm6 */ |
| 2109 | data->has_pwm |= (1 << 5); |
| 2110 | } |
| 2111 | if ((inb(addr + 0x45) & 0x0c) == 0x08) { /* fan5 */ |
| 2112 | data->has_fan |= (1 << 4); |
| 2113 | } |
| 2114 | if ((inb(addr + 0x46) & 0x0c) == 0x08) { /* pwm5 */ |
| 2115 | data->has_pwm |= (1 << 4); |
| 2116 | } |
| 2117 | |
| 2118 | exit: |
Juerg Haefliger | 67e2f32 | 2007-10-01 21:20:28 -0700 | [diff] [blame] | 2119 | dme1737_sio_exit(sio_cip); |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 2120 | |
| 2121 | return err; |
| 2122 | } |
| 2123 | |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 2124 | static int dme1737_i2c_detect(struct i2c_adapter *adapter, int address, |
| 2125 | int kind) |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 2126 | { |
| 2127 | u8 company, verstep = 0; |
| 2128 | struct i2c_client *client; |
| 2129 | struct dme1737_data *data; |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 2130 | struct device *dev; |
| 2131 | int err = 0; |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 2132 | const char *name; |
| 2133 | |
| 2134 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) { |
| 2135 | goto exit; |
| 2136 | } |
| 2137 | |
| 2138 | if (!(data = kzalloc(sizeof(struct dme1737_data), GFP_KERNEL))) { |
| 2139 | err = -ENOMEM; |
| 2140 | goto exit; |
| 2141 | } |
| 2142 | |
| 2143 | client = &data->client; |
| 2144 | i2c_set_clientdata(client, data); |
| 2145 | client->addr = address; |
| 2146 | client->adapter = adapter; |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 2147 | client->driver = &dme1737_i2c_driver; |
| 2148 | dev = &client->dev; |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 2149 | |
| 2150 | /* A negative kind means that the driver was loaded with no force |
| 2151 | * parameter (default), so we must identify the chip. */ |
| 2152 | if (kind < 0) { |
| 2153 | company = dme1737_read(client, DME1737_REG_COMPANY); |
| 2154 | verstep = dme1737_read(client, DME1737_REG_VERSTEP); |
| 2155 | |
| 2156 | if (!((company == DME1737_COMPANY_SMSC) && |
| 2157 | ((verstep & DME1737_VERSTEP_MASK) == DME1737_VERSTEP))) { |
| 2158 | err = -ENODEV; |
| 2159 | goto exit_kfree; |
| 2160 | } |
| 2161 | } |
| 2162 | |
| 2163 | kind = dme1737; |
| 2164 | name = "dme1737"; |
| 2165 | |
| 2166 | /* Fill in the remaining client fields and put it into the global |
| 2167 | * list */ |
| 2168 | strlcpy(client->name, name, I2C_NAME_SIZE); |
| 2169 | mutex_init(&data->update_lock); |
| 2170 | |
| 2171 | /* Tell the I2C layer a new client has arrived */ |
| 2172 | if ((err = i2c_attach_client(client))) { |
| 2173 | goto exit_kfree; |
| 2174 | } |
| 2175 | |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 2176 | dev_info(dev, "Found a DME1737 chip at 0x%02x (rev 0x%02x).\n", |
| 2177 | client->addr, verstep); |
| 2178 | |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 2179 | /* Initialize the DME1737 chip */ |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 2180 | if ((err = dme1737_init_device(dev))) { |
| 2181 | dev_err(dev, "Failed to initialize device.\n"); |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 2182 | goto exit_detach; |
| 2183 | } |
| 2184 | |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 2185 | /* Create sysfs files */ |
| 2186 | if ((err = dme1737_create_files(dev))) { |
| 2187 | dev_err(dev, "Failed to create sysfs files.\n"); |
| 2188 | goto exit_detach; |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 2189 | } |
| 2190 | |
| 2191 | /* Register device */ |
Mark M. Hoffman | 62ee3e1 | 2007-10-10 22:32:50 -0400 | [diff] [blame] | 2192 | data->hwmon_dev = hwmon_device_register(dev); |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 2193 | if (IS_ERR(data->hwmon_dev)) { |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 2194 | dev_err(dev, "Failed to register device.\n"); |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 2195 | err = PTR_ERR(data->hwmon_dev); |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 2196 | goto exit_remove; |
| 2197 | } |
| 2198 | |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 2199 | return 0; |
| 2200 | |
| 2201 | exit_remove: |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 2202 | dme1737_remove_files(dev); |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 2203 | exit_detach: |
| 2204 | i2c_detach_client(client); |
| 2205 | exit_kfree: |
| 2206 | kfree(data); |
| 2207 | exit: |
| 2208 | return err; |
| 2209 | } |
| 2210 | |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 2211 | static int dme1737_i2c_attach_adapter(struct i2c_adapter *adapter) |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 2212 | { |
| 2213 | if (!(adapter->class & I2C_CLASS_HWMON)) { |
| 2214 | return 0; |
| 2215 | } |
| 2216 | |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 2217 | return i2c_probe(adapter, &addr_data, dme1737_i2c_detect); |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 2218 | } |
| 2219 | |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 2220 | static int dme1737_i2c_detach_client(struct i2c_client *client) |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 2221 | { |
| 2222 | struct dme1737_data *data = i2c_get_clientdata(client); |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 2223 | int err; |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 2224 | |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 2225 | hwmon_device_unregister(data->hwmon_dev); |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 2226 | dme1737_remove_files(&client->dev); |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 2227 | |
| 2228 | if ((err = i2c_detach_client(client))) { |
| 2229 | return err; |
| 2230 | } |
| 2231 | |
| 2232 | kfree(data); |
| 2233 | return 0; |
| 2234 | } |
| 2235 | |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 2236 | static struct i2c_driver dme1737_i2c_driver = { |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 2237 | .driver = { |
| 2238 | .name = "dme1737", |
| 2239 | }, |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 2240 | .attach_adapter = dme1737_i2c_attach_adapter, |
| 2241 | .detach_client = dme1737_i2c_detach_client, |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 2242 | }; |
| 2243 | |
Juerg Haefliger | 67e2f32 | 2007-10-01 21:20:28 -0700 | [diff] [blame] | 2244 | /* --------------------------------------------------------------------- |
Juerg Haefliger | e95c237 | 2007-10-07 21:27:35 -0700 | [diff] [blame] | 2245 | * ISA device detection and registration |
| 2246 | * --------------------------------------------------------------------- */ |
| 2247 | |
| 2248 | static int __init dme1737_isa_detect(int sio_cip, unsigned short *addr) |
| 2249 | { |
| 2250 | int err = 0, reg; |
| 2251 | unsigned short base_addr; |
| 2252 | |
| 2253 | dme1737_sio_enter(sio_cip); |
| 2254 | |
| 2255 | /* Check device ID |
| 2256 | * We currently know about SCH3112 (0x7c), SCH3114 (0x7d), and |
| 2257 | * SCH3116 (0x7f). */ |
Jean Delvare | 67b671b | 2007-12-06 23:13:42 +0100 | [diff] [blame] | 2258 | reg = force_id ? force_id : dme1737_sio_inb(sio_cip, 0x20); |
Juerg Haefliger | e95c237 | 2007-10-07 21:27:35 -0700 | [diff] [blame] | 2259 | if (!(reg == 0x7c || reg == 0x7d || reg == 0x7f)) { |
| 2260 | err = -ENODEV; |
| 2261 | goto exit; |
| 2262 | } |
| 2263 | |
| 2264 | /* Select logical device A (runtime registers) */ |
| 2265 | dme1737_sio_outb(sio_cip, 0x07, 0x0a); |
| 2266 | |
| 2267 | /* Get the base address of the runtime registers */ |
| 2268 | if (!(base_addr = (dme1737_sio_inb(sio_cip, 0x60) << 8) | |
| 2269 | dme1737_sio_inb(sio_cip, 0x61))) { |
| 2270 | printk(KERN_ERR "dme1737: Base address not set.\n"); |
| 2271 | err = -ENODEV; |
| 2272 | goto exit; |
| 2273 | } |
| 2274 | |
| 2275 | /* Access to the hwmon registers is through an index/data register |
| 2276 | * pair located at offset 0x70/0x71. */ |
| 2277 | *addr = base_addr + 0x70; |
| 2278 | |
| 2279 | exit: |
| 2280 | dme1737_sio_exit(sio_cip); |
| 2281 | return err; |
| 2282 | } |
| 2283 | |
| 2284 | static int __init dme1737_isa_device_add(unsigned short addr) |
| 2285 | { |
| 2286 | struct resource res = { |
| 2287 | .start = addr, |
| 2288 | .end = addr + DME1737_EXTENT - 1, |
| 2289 | .name = "dme1737", |
| 2290 | .flags = IORESOURCE_IO, |
| 2291 | }; |
| 2292 | int err; |
| 2293 | |
| 2294 | if (!(pdev = platform_device_alloc("dme1737", addr))) { |
| 2295 | printk(KERN_ERR "dme1737: Failed to allocate device.\n"); |
| 2296 | err = -ENOMEM; |
| 2297 | goto exit; |
| 2298 | } |
| 2299 | |
| 2300 | if ((err = platform_device_add_resources(pdev, &res, 1))) { |
| 2301 | printk(KERN_ERR "dme1737: Failed to add device resource " |
| 2302 | "(err = %d).\n", err); |
| 2303 | goto exit_device_put; |
| 2304 | } |
| 2305 | |
| 2306 | if ((err = platform_device_add(pdev))) { |
| 2307 | printk(KERN_ERR "dme1737: Failed to add device (err = %d).\n", |
| 2308 | err); |
| 2309 | goto exit_device_put; |
| 2310 | } |
| 2311 | |
| 2312 | return 0; |
| 2313 | |
| 2314 | exit_device_put: |
| 2315 | platform_device_put(pdev); |
| 2316 | pdev = NULL; |
| 2317 | exit: |
| 2318 | return err; |
| 2319 | } |
| 2320 | |
| 2321 | static int __devinit dme1737_isa_probe(struct platform_device *pdev) |
| 2322 | { |
| 2323 | u8 company, device; |
| 2324 | struct resource *res; |
| 2325 | struct i2c_client *client; |
| 2326 | struct dme1737_data *data; |
| 2327 | struct device *dev = &pdev->dev; |
| 2328 | int err; |
| 2329 | |
| 2330 | res = platform_get_resource(pdev, IORESOURCE_IO, 0); |
| 2331 | if (!request_region(res->start, DME1737_EXTENT, "dme1737")) { |
| 2332 | dev_err(dev, "Failed to request region 0x%04x-0x%04x.\n", |
| 2333 | (unsigned short)res->start, |
| 2334 | (unsigned short)res->start + DME1737_EXTENT - 1); |
| 2335 | err = -EBUSY; |
| 2336 | goto exit; |
| 2337 | } |
| 2338 | |
| 2339 | if (!(data = kzalloc(sizeof(struct dme1737_data), GFP_KERNEL))) { |
| 2340 | err = -ENOMEM; |
| 2341 | goto exit_release_region; |
| 2342 | } |
| 2343 | |
| 2344 | client = &data->client; |
| 2345 | i2c_set_clientdata(client, data); |
| 2346 | client->addr = res->start; |
| 2347 | platform_set_drvdata(pdev, data); |
| 2348 | |
| 2349 | company = dme1737_read(client, DME1737_REG_COMPANY); |
| 2350 | device = dme1737_read(client, DME1737_REG_DEVICE); |
| 2351 | |
| 2352 | if (!((company == DME1737_COMPANY_SMSC) && |
| 2353 | (device == SCH311X_DEVICE))) { |
| 2354 | err = -ENODEV; |
| 2355 | goto exit_kfree; |
| 2356 | } |
| 2357 | |
| 2358 | /* Fill in the remaining client fields and initialize the mutex */ |
| 2359 | strlcpy(client->name, "sch311x", I2C_NAME_SIZE); |
| 2360 | mutex_init(&data->update_lock); |
| 2361 | |
| 2362 | dev_info(dev, "Found a SCH311x chip at 0x%04x\n", client->addr); |
| 2363 | |
| 2364 | /* Initialize the chip */ |
| 2365 | if ((err = dme1737_init_device(dev))) { |
| 2366 | dev_err(dev, "Failed to initialize device.\n"); |
| 2367 | goto exit_kfree; |
| 2368 | } |
| 2369 | |
| 2370 | /* Create sysfs files */ |
| 2371 | if ((err = dme1737_create_files(dev))) { |
| 2372 | dev_err(dev, "Failed to create sysfs files.\n"); |
| 2373 | goto exit_kfree; |
| 2374 | } |
| 2375 | |
| 2376 | /* Register device */ |
| 2377 | data->hwmon_dev = hwmon_device_register(dev); |
| 2378 | if (IS_ERR(data->hwmon_dev)) { |
| 2379 | dev_err(dev, "Failed to register device.\n"); |
| 2380 | err = PTR_ERR(data->hwmon_dev); |
| 2381 | goto exit_remove_files; |
| 2382 | } |
| 2383 | |
| 2384 | return 0; |
| 2385 | |
| 2386 | exit_remove_files: |
| 2387 | dme1737_remove_files(dev); |
| 2388 | exit_kfree: |
| 2389 | platform_set_drvdata(pdev, NULL); |
| 2390 | kfree(data); |
| 2391 | exit_release_region: |
| 2392 | release_region(res->start, DME1737_EXTENT); |
| 2393 | exit: |
| 2394 | return err; |
| 2395 | } |
| 2396 | |
| 2397 | static int __devexit dme1737_isa_remove(struct platform_device *pdev) |
| 2398 | { |
| 2399 | struct dme1737_data *data = platform_get_drvdata(pdev); |
| 2400 | |
| 2401 | hwmon_device_unregister(data->hwmon_dev); |
| 2402 | dme1737_remove_files(&pdev->dev); |
| 2403 | release_region(data->client.addr, DME1737_EXTENT); |
| 2404 | platform_set_drvdata(pdev, NULL); |
| 2405 | kfree(data); |
| 2406 | |
| 2407 | return 0; |
| 2408 | } |
| 2409 | |
| 2410 | static struct platform_driver dme1737_isa_driver = { |
| 2411 | .driver = { |
| 2412 | .owner = THIS_MODULE, |
| 2413 | .name = "dme1737", |
| 2414 | }, |
| 2415 | .probe = dme1737_isa_probe, |
| 2416 | .remove = __devexit_p(dme1737_isa_remove), |
| 2417 | }; |
| 2418 | |
| 2419 | /* --------------------------------------------------------------------- |
Juerg Haefliger | 67e2f32 | 2007-10-01 21:20:28 -0700 | [diff] [blame] | 2420 | * Module initialization and cleanup |
| 2421 | * --------------------------------------------------------------------- */ |
| 2422 | |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 2423 | static int __init dme1737_init(void) |
| 2424 | { |
Juerg Haefliger | e95c237 | 2007-10-07 21:27:35 -0700 | [diff] [blame] | 2425 | int err; |
| 2426 | unsigned short addr; |
| 2427 | |
| 2428 | if ((err = i2c_add_driver(&dme1737_i2c_driver))) { |
| 2429 | goto exit; |
| 2430 | } |
| 2431 | |
| 2432 | if (dme1737_isa_detect(0x2e, &addr) && |
| 2433 | dme1737_isa_detect(0x4e, &addr)) { |
| 2434 | /* Return 0 if we didn't find an ISA device */ |
| 2435 | return 0; |
| 2436 | } |
| 2437 | |
| 2438 | if ((err = platform_driver_register(&dme1737_isa_driver))) { |
| 2439 | goto exit_del_i2c_driver; |
| 2440 | } |
| 2441 | |
| 2442 | /* Sets global pdev as a side effect */ |
| 2443 | if ((err = dme1737_isa_device_add(addr))) { |
| 2444 | goto exit_del_isa_driver; |
| 2445 | } |
| 2446 | |
| 2447 | return 0; |
| 2448 | |
| 2449 | exit_del_isa_driver: |
| 2450 | platform_driver_unregister(&dme1737_isa_driver); |
| 2451 | exit_del_i2c_driver: |
| 2452 | i2c_del_driver(&dme1737_i2c_driver); |
| 2453 | exit: |
| 2454 | return err; |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 2455 | } |
| 2456 | |
| 2457 | static void __exit dme1737_exit(void) |
| 2458 | { |
Juerg Haefliger | e95c237 | 2007-10-07 21:27:35 -0700 | [diff] [blame] | 2459 | if (pdev) { |
| 2460 | platform_device_unregister(pdev); |
| 2461 | platform_driver_unregister(&dme1737_isa_driver); |
| 2462 | } |
| 2463 | |
Juerg Haefliger | b237eb2 | 2007-10-01 21:19:04 -0700 | [diff] [blame] | 2464 | i2c_del_driver(&dme1737_i2c_driver); |
Juerg Haefliger | 9431996 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 2465 | } |
| 2466 | |
| 2467 | MODULE_AUTHOR("Juerg Haefliger <juergh@gmail.com>"); |
| 2468 | MODULE_DESCRIPTION("DME1737 sensors"); |
| 2469 | MODULE_LICENSE("GPL"); |
| 2470 | |
| 2471 | module_init(dme1737_init); |
| 2472 | module_exit(dme1737_exit); |