Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 1 | /* |
| 2 | * w83795.c - Linux kernel driver for hardware monitoring |
| 3 | * Copyright (C) 2008 Nuvoton Technology Corp. |
| 4 | * Wei Song |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation - version 2. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 18 | * 02110-1301 USA. |
| 19 | * |
| 20 | * Supports following chips: |
| 21 | * |
| 22 | * Chip #vin #fanin #pwm #temp #dts wchipid vendid i2c ISA |
| 23 | * w83795g 21 14 8 6 8 0x79 0x5ca3 yes no |
| 24 | * w83795adg 18 14 2 6 8 0x79 0x5ca3 yes no |
| 25 | */ |
| 26 | |
| 27 | #include <linux/kernel.h> |
| 28 | #include <linux/module.h> |
| 29 | #include <linux/init.h> |
| 30 | #include <linux/slab.h> |
| 31 | #include <linux/i2c.h> |
| 32 | #include <linux/hwmon.h> |
| 33 | #include <linux/hwmon-sysfs.h> |
| 34 | #include <linux/err.h> |
| 35 | #include <linux/mutex.h> |
| 36 | #include <linux/delay.h> |
| 37 | |
| 38 | /* Addresses to scan */ |
Jean Delvare | 86ef4d2 | 2010-10-28 20:31:46 +0200 | [diff] [blame] | 39 | static const unsigned short normal_i2c[] = { |
| 40 | 0x2c, 0x2d, 0x2e, 0x2f, I2C_CLIENT_END |
| 41 | }; |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 42 | |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 43 | |
| 44 | static int reset; |
| 45 | module_param(reset, bool, 0); |
| 46 | MODULE_PARM_DESC(reset, "Set to 1 to reset chip, not recommended"); |
| 47 | |
| 48 | |
| 49 | #define W83795_REG_BANKSEL 0x00 |
| 50 | #define W83795_REG_VENDORID 0xfd |
| 51 | #define W83795_REG_CHIPID 0xfe |
| 52 | #define W83795_REG_DEVICEID 0xfb |
Jean Delvare | 2be381d | 2010-10-28 20:31:45 +0200 | [diff] [blame] | 53 | #define W83795_REG_DEVICEID_A 0xff |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 54 | |
| 55 | #define W83795_REG_I2C_ADDR 0xfc |
| 56 | #define W83795_REG_CONFIG 0x01 |
| 57 | #define W83795_REG_CONFIG_CONFIG48 0x04 |
Jean Delvare | 80646b9 | 2010-10-28 20:31:46 +0200 | [diff] [blame] | 58 | #define W83795_REG_CONFIG_START 0x01 |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 59 | |
| 60 | /* Multi-Function Pin Ctrl Registers */ |
| 61 | #define W83795_REG_VOLT_CTRL1 0x02 |
| 62 | #define W83795_REG_VOLT_CTRL2 0x03 |
| 63 | #define W83795_REG_TEMP_CTRL1 0x04 |
| 64 | #define W83795_REG_TEMP_CTRL2 0x05 |
| 65 | #define W83795_REG_FANIN_CTRL1 0x06 |
| 66 | #define W83795_REG_FANIN_CTRL2 0x07 |
| 67 | #define W83795_REG_VMIGB_CTRL 0x08 |
| 68 | |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 69 | #define TEMP_READ 0 |
| 70 | #define TEMP_CRIT 1 |
| 71 | #define TEMP_CRIT_HYST 2 |
| 72 | #define TEMP_WARN 3 |
| 73 | #define TEMP_WARN_HYST 4 |
| 74 | /* only crit and crit_hyst affect real-time alarm status |
| 75 | * current crit crit_hyst warn warn_hyst */ |
Jean Delvare | 86ef4d2 | 2010-10-28 20:31:46 +0200 | [diff] [blame] | 76 | static const u16 W83795_REG_TEMP[][5] = { |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 77 | {0x21, 0x96, 0x97, 0x98, 0x99}, /* TD1/TR1 */ |
| 78 | {0x22, 0x9a, 0x9b, 0x9c, 0x9d}, /* TD2/TR2 */ |
| 79 | {0x23, 0x9e, 0x9f, 0xa0, 0xa1}, /* TD3/TR3 */ |
| 80 | {0x24, 0xa2, 0xa3, 0xa4, 0xa5}, /* TD4/TR4 */ |
| 81 | {0x1f, 0xa6, 0xa7, 0xa8, 0xa9}, /* TR5 */ |
| 82 | {0x20, 0xaa, 0xab, 0xac, 0xad}, /* TR6 */ |
| 83 | }; |
| 84 | |
| 85 | #define IN_READ 0 |
| 86 | #define IN_MAX 1 |
| 87 | #define IN_LOW 2 |
| 88 | static const u16 W83795_REG_IN[][3] = { |
| 89 | /* Current, HL, LL */ |
| 90 | {0x10, 0x70, 0x71}, /* VSEN1 */ |
| 91 | {0x11, 0x72, 0x73}, /* VSEN2 */ |
| 92 | {0x12, 0x74, 0x75}, /* VSEN3 */ |
| 93 | {0x13, 0x76, 0x77}, /* VSEN4 */ |
| 94 | {0x14, 0x78, 0x79}, /* VSEN5 */ |
| 95 | {0x15, 0x7a, 0x7b}, /* VSEN6 */ |
| 96 | {0x16, 0x7c, 0x7d}, /* VSEN7 */ |
| 97 | {0x17, 0x7e, 0x7f}, /* VSEN8 */ |
| 98 | {0x18, 0x80, 0x81}, /* VSEN9 */ |
| 99 | {0x19, 0x82, 0x83}, /* VSEN10 */ |
| 100 | {0x1A, 0x84, 0x85}, /* VSEN11 */ |
| 101 | {0x1B, 0x86, 0x87}, /* VTT */ |
| 102 | {0x1C, 0x88, 0x89}, /* 3VDD */ |
| 103 | {0x1D, 0x8a, 0x8b}, /* 3VSB */ |
| 104 | {0x1E, 0x8c, 0x8d}, /* VBAT */ |
| 105 | {0x1F, 0xa6, 0xa7}, /* VSEN12 */ |
| 106 | {0x20, 0xaa, 0xab}, /* VSEN13 */ |
| 107 | {0x21, 0x96, 0x97}, /* VSEN14 */ |
| 108 | {0x22, 0x9a, 0x9b}, /* VSEN15 */ |
| 109 | {0x23, 0x9e, 0x9f}, /* VSEN16 */ |
| 110 | {0x24, 0xa2, 0xa3}, /* VSEN17 */ |
| 111 | }; |
| 112 | #define W83795_REG_VRLSB 0x3C |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 113 | |
| 114 | static const u8 W83795_REG_IN_HL_LSB[] = { |
| 115 | 0x8e, /* VSEN1-4 */ |
| 116 | 0x90, /* VSEN5-8 */ |
| 117 | 0x92, /* VSEN9-11 */ |
| 118 | 0x94, /* VTT, 3VDD, 3VSB, 3VBAT */ |
| 119 | 0xa8, /* VSEN12 */ |
| 120 | 0xac, /* VSEN13 */ |
| 121 | 0x98, /* VSEN14 */ |
| 122 | 0x9c, /* VSEN15 */ |
| 123 | 0xa0, /* VSEN16 */ |
| 124 | 0xa4, /* VSEN17 */ |
| 125 | }; |
| 126 | |
| 127 | #define IN_LSB_REG(index, type) \ |
| 128 | (((type) == 1) ? W83795_REG_IN_HL_LSB[(index)] \ |
| 129 | : (W83795_REG_IN_HL_LSB[(index)] + 1)) |
| 130 | |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 131 | #define IN_LSB_SHIFT 0 |
| 132 | #define IN_LSB_IDX 1 |
| 133 | static const u8 IN_LSB_SHIFT_IDX[][2] = { |
| 134 | /* High/Low LSB shift, LSB No. */ |
| 135 | {0x00, 0x00}, /* VSEN1 */ |
| 136 | {0x02, 0x00}, /* VSEN2 */ |
| 137 | {0x04, 0x00}, /* VSEN3 */ |
| 138 | {0x06, 0x00}, /* VSEN4 */ |
| 139 | {0x00, 0x01}, /* VSEN5 */ |
| 140 | {0x02, 0x01}, /* VSEN6 */ |
| 141 | {0x04, 0x01}, /* VSEN7 */ |
| 142 | {0x06, 0x01}, /* VSEN8 */ |
| 143 | {0x00, 0x02}, /* VSEN9 */ |
| 144 | {0x02, 0x02}, /* VSEN10 */ |
| 145 | {0x04, 0x02}, /* VSEN11 */ |
| 146 | {0x00, 0x03}, /* VTT */ |
| 147 | {0x02, 0x03}, /* 3VDD */ |
| 148 | {0x04, 0x03}, /* 3VSB */ |
| 149 | {0x06, 0x03}, /* VBAT */ |
| 150 | {0x06, 0x04}, /* VSEN12 */ |
| 151 | {0x06, 0x05}, /* VSEN13 */ |
| 152 | {0x06, 0x06}, /* VSEN14 */ |
| 153 | {0x06, 0x07}, /* VSEN15 */ |
| 154 | {0x06, 0x08}, /* VSEN16 */ |
| 155 | {0x06, 0x09}, /* VSEN17 */ |
| 156 | }; |
| 157 | |
| 158 | |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 159 | #define W83795_REG_FAN(index) (0x2E + (index)) |
| 160 | #define W83795_REG_FAN_MIN_HL(index) (0xB6 + (index)) |
| 161 | #define W83795_REG_FAN_MIN_LSB(index) (0xC4 + (index) / 2) |
| 162 | #define W83795_REG_FAN_MIN_LSB_SHIFT(index) \ |
Jean Delvare | 7eb8d50 | 2010-10-28 20:31:47 +0200 | [diff] [blame] | 163 | (((index) & 1) ? 4 : 0) |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 164 | |
| 165 | #define W83795_REG_VID_CTRL 0x6A |
| 166 | |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 167 | #define W83795_REG_ALARM(index) (0x41 + (index)) |
| 168 | #define W83795_REG_BEEP(index) (0x50 + (index)) |
| 169 | |
| 170 | #define W83795_REG_CLR_CHASSIS 0x4D |
| 171 | |
| 172 | |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 173 | #define W83795_REG_FCMS1 0x201 |
| 174 | #define W83795_REG_FCMS2 0x208 |
| 175 | #define W83795_REG_TFMR(index) (0x202 + (index)) |
| 176 | #define W83795_REG_FOMC 0x20F |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 177 | |
| 178 | #define W83795_REG_TSS(index) (0x209 + (index)) |
| 179 | |
| 180 | #define PWM_OUTPUT 0 |
Jean Delvare | fd7f82b | 2010-10-28 20:31:48 +0200 | [diff] [blame] | 181 | #define PWM_FREQ 1 |
| 182 | #define PWM_START 2 |
| 183 | #define PWM_NONSTOP 3 |
| 184 | #define PWM_STOP_TIME 4 |
| 185 | #define W83795_REG_PWM(index, nr) (0x210 + (nr) * 8 + (index)) |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 186 | |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 187 | #define W83795_REG_FTSH(index) (0x240 + (index) * 2) |
| 188 | #define W83795_REG_FTSL(index) (0x241 + (index) * 2) |
| 189 | #define W83795_REG_TFTS 0x250 |
| 190 | |
| 191 | #define TEMP_PWM_TTTI 0 |
| 192 | #define TEMP_PWM_CTFS 1 |
| 193 | #define TEMP_PWM_HCT 2 |
| 194 | #define TEMP_PWM_HOT 3 |
| 195 | #define W83795_REG_TTTI(index) (0x260 + (index)) |
| 196 | #define W83795_REG_CTFS(index) (0x268 + (index)) |
| 197 | #define W83795_REG_HT(index) (0x270 + (index)) |
| 198 | |
| 199 | #define SF4_TEMP 0 |
| 200 | #define SF4_PWM 1 |
| 201 | #define W83795_REG_SF4_TEMP(temp_num, index) \ |
| 202 | (0x280 + 0x10 * (temp_num) + (index)) |
| 203 | #define W83795_REG_SF4_PWM(temp_num, index) \ |
| 204 | (0x288 + 0x10 * (temp_num) + (index)) |
| 205 | |
| 206 | #define W83795_REG_DTSC 0x301 |
| 207 | #define W83795_REG_DTSE 0x302 |
| 208 | #define W83795_REG_DTS(index) (0x26 + (index)) |
Jean Delvare | 54891a3 | 2010-10-28 20:31:47 +0200 | [diff] [blame] | 209 | #define W83795_REG_PECI_TBASE(index) (0x320 + (index)) |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 210 | |
| 211 | #define DTS_CRIT 0 |
| 212 | #define DTS_CRIT_HYST 1 |
| 213 | #define DTS_WARN 2 |
| 214 | #define DTS_WARN_HYST 3 |
| 215 | #define W83795_REG_DTS_EXT(index) (0xB2 + (index)) |
| 216 | |
| 217 | #define SETUP_PWM_DEFAULT 0 |
| 218 | #define SETUP_PWM_UPTIME 1 |
| 219 | #define SETUP_PWM_DOWNTIME 2 |
| 220 | #define W83795_REG_SETUP_PWM(index) (0x20C + (index)) |
| 221 | |
| 222 | static inline u16 in_from_reg(u8 index, u16 val) |
| 223 | { |
Jean Delvare | 49c7347 | 2010-10-28 20:31:48 +0200 | [diff] [blame] | 224 | /* 3VDD, 3VSB and VBAT: 6 mV/bit; other inputs: 2 mV/bit */ |
| 225 | if (index >= 12 && index <= 14) |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 226 | return val * 6; |
| 227 | else |
| 228 | return val * 2; |
| 229 | } |
| 230 | |
| 231 | static inline u16 in_to_reg(u8 index, u16 val) |
| 232 | { |
Jean Delvare | 49c7347 | 2010-10-28 20:31:48 +0200 | [diff] [blame] | 233 | if (index >= 12 && index <= 14) |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 234 | return val / 6; |
| 235 | else |
| 236 | return val / 2; |
| 237 | } |
| 238 | |
| 239 | static inline unsigned long fan_from_reg(u16 val) |
| 240 | { |
Jean Delvare | 6c82b2f | 2010-10-28 20:31:46 +0200 | [diff] [blame] | 241 | if ((val == 0xfff) || (val == 0)) |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 242 | return 0; |
| 243 | return 1350000UL / val; |
| 244 | } |
| 245 | |
| 246 | static inline u16 fan_to_reg(long rpm) |
| 247 | { |
| 248 | if (rpm <= 0) |
| 249 | return 0x0fff; |
| 250 | return SENSORS_LIMIT((1350000 + (rpm >> 1)) / rpm, 1, 0xffe); |
| 251 | } |
| 252 | |
| 253 | static inline unsigned long time_from_reg(u8 reg) |
| 254 | { |
| 255 | return reg * 100; |
| 256 | } |
| 257 | |
| 258 | static inline u8 time_to_reg(unsigned long val) |
| 259 | { |
| 260 | return SENSORS_LIMIT((val + 50) / 100, 0, 0xff); |
| 261 | } |
| 262 | |
| 263 | static inline long temp_from_reg(s8 reg) |
| 264 | { |
| 265 | return reg * 1000; |
| 266 | } |
| 267 | |
| 268 | static inline s8 temp_to_reg(long val, s8 min, s8 max) |
| 269 | { |
Jean Delvare | dd127f5 | 2010-10-28 20:31:47 +0200 | [diff] [blame] | 270 | return SENSORS_LIMIT(val / 1000, min, max); |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 271 | } |
| 272 | |
Jean Delvare | 01879a8 | 2010-10-28 20:31:47 +0200 | [diff] [blame] | 273 | static const u16 pwm_freq_cksel0[16] = { |
| 274 | 1024, 512, 341, 256, 205, 171, 146, 128, |
| 275 | 85, 64, 32, 16, 8, 4, 2, 1 |
| 276 | }; |
| 277 | |
| 278 | static unsigned int pwm_freq_from_reg(u8 reg, u16 clkin) |
| 279 | { |
| 280 | unsigned long base_clock; |
| 281 | |
| 282 | if (reg & 0x80) { |
| 283 | base_clock = clkin * 1000 / ((clkin == 48000) ? 384 : 256); |
| 284 | return base_clock / ((reg & 0x7f) + 1); |
| 285 | } else |
| 286 | return pwm_freq_cksel0[reg & 0x0f]; |
| 287 | } |
| 288 | |
| 289 | static u8 pwm_freq_to_reg(unsigned long val, u16 clkin) |
| 290 | { |
| 291 | unsigned long base_clock; |
| 292 | u8 reg0, reg1; |
| 293 | unsigned long best0, best1; |
| 294 | |
| 295 | /* Best fit for cksel = 0 */ |
| 296 | for (reg0 = 0; reg0 < ARRAY_SIZE(pwm_freq_cksel0) - 1; reg0++) { |
| 297 | if (val > (pwm_freq_cksel0[reg0] + |
| 298 | pwm_freq_cksel0[reg0 + 1]) / 2) |
| 299 | break; |
| 300 | } |
| 301 | if (val < 375) /* cksel = 1 can't beat this */ |
| 302 | return reg0; |
| 303 | best0 = pwm_freq_cksel0[reg0]; |
| 304 | |
| 305 | /* Best fit for cksel = 1 */ |
| 306 | base_clock = clkin * 1000 / ((clkin == 48000) ? 384 : 256); |
| 307 | reg1 = SENSORS_LIMIT(DIV_ROUND_CLOSEST(base_clock, val), 1, 128); |
| 308 | best1 = base_clock / reg1; |
| 309 | reg1 = 0x80 | (reg1 - 1); |
| 310 | |
| 311 | /* Choose the closest one */ |
| 312 | if (abs(val - best0) > abs(val - best1)) |
| 313 | return reg1; |
| 314 | else |
| 315 | return reg0; |
| 316 | } |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 317 | |
| 318 | enum chip_types {w83795g, w83795adg}; |
| 319 | |
| 320 | struct w83795_data { |
| 321 | struct device *hwmon_dev; |
| 322 | struct mutex update_lock; |
| 323 | unsigned long last_updated; /* In jiffies */ |
| 324 | enum chip_types chip_type; |
| 325 | |
| 326 | u8 bank; |
| 327 | |
| 328 | u32 has_in; /* Enable monitor VIN or not */ |
Jean Delvare | 0e25601 | 2010-10-28 20:31:46 +0200 | [diff] [blame] | 329 | u8 has_dyn_in; /* Only in2-0 can have this */ |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 330 | u16 in[21][3]; /* Register value, read/high/low */ |
| 331 | u8 in_lsb[10][3]; /* LSB Register value, high/low */ |
| 332 | u8 has_gain; /* has gain: in17-20 * 8 */ |
| 333 | |
| 334 | u16 has_fan; /* Enable fan14-1 or not */ |
| 335 | u16 fan[14]; /* Register value combine */ |
| 336 | u16 fan_min[14]; /* Register value combine */ |
| 337 | |
| 338 | u8 has_temp; /* Enable monitor temp6-1 or not */ |
Jean Delvare | dd127f5 | 2010-10-28 20:31:47 +0200 | [diff] [blame] | 339 | s8 temp[6][5]; /* current, crit, crit_hyst, warn, warn_hyst */ |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 340 | u8 temp_read_vrlsb[6]; |
Jean Delvare | 39deb69 | 2010-10-28 20:31:48 +0200 | [diff] [blame] | 341 | u8 temp_mode; /* Bit vector, 0 = TR, 1 = TD */ |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 342 | u8 temp_src[3]; /* Register value */ |
| 343 | |
| 344 | u8 enable_dts; /* Enable PECI and SB-TSI, |
| 345 | * bit 0: =1 enable, =0 disable, |
| 346 | * bit 1: =1 AMD SB-TSI, =0 Intel PECI */ |
| 347 | u8 has_dts; /* Enable monitor DTS temp */ |
Jean Delvare | dd127f5 | 2010-10-28 20:31:47 +0200 | [diff] [blame] | 348 | s8 dts[8]; /* Register value */ |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 349 | u8 dts_read_vrlsb[8]; /* Register value */ |
Jean Delvare | dd127f5 | 2010-10-28 20:31:47 +0200 | [diff] [blame] | 350 | s8 dts_ext[4]; /* Register value */ |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 351 | |
| 352 | u8 has_pwm; /* 795g supports 8 pwm, 795adg only supports 2, |
| 353 | * no config register, only affected by chip |
| 354 | * type */ |
Jean Delvare | fd7f82b | 2010-10-28 20:31:48 +0200 | [diff] [blame] | 355 | u8 pwm[8][5]; /* Register value, output, freq, start, |
| 356 | * non stop, stop time */ |
Jean Delvare | 01879a8 | 2010-10-28 20:31:47 +0200 | [diff] [blame] | 357 | u16 clkin; /* CLKIN frequency in kHz */ |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 358 | u8 pwm_fcms[2]; /* Register value */ |
| 359 | u8 pwm_tfmr[6]; /* Register value */ |
| 360 | u8 pwm_fomc; /* Register value */ |
| 361 | |
| 362 | u16 target_speed[8]; /* Register value, target speed for speed |
| 363 | * cruise */ |
| 364 | u8 tol_speed; /* tolerance of target speed */ |
| 365 | u8 pwm_temp[6][4]; /* TTTI, CTFS, HCT, HOT */ |
| 366 | u8 sf4_reg[6][2][7]; /* 6 temp, temp/dcpwm, 7 registers */ |
| 367 | |
| 368 | u8 setup_pwm[3]; /* Register value */ |
| 369 | |
| 370 | u8 alarms[6]; /* Register value */ |
| 371 | u8 beeps[6]; /* Register value */ |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 372 | |
| 373 | char valid; |
Jean Delvare | 2ae61de | 2010-10-28 20:31:48 +0200 | [diff] [blame] | 374 | char valid_limits; |
Jean Delvare | 1bb3450 | 2010-10-28 20:31:48 +0200 | [diff] [blame^] | 375 | char valid_pwm_config; |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 376 | }; |
| 377 | |
| 378 | /* |
| 379 | * Hardware access |
Jean Delvare | b2469f4 | 2010-10-28 20:31:45 +0200 | [diff] [blame] | 380 | * We assume that nobdody can change the bank outside the driver. |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 381 | */ |
| 382 | |
Jean Delvare | b2469f4 | 2010-10-28 20:31:45 +0200 | [diff] [blame] | 383 | /* Must be called with data->update_lock held, except during initialization */ |
| 384 | static int w83795_set_bank(struct i2c_client *client, u8 bank) |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 385 | { |
| 386 | struct w83795_data *data = i2c_get_clientdata(client); |
Jean Delvare | b2469f4 | 2010-10-28 20:31:45 +0200 | [diff] [blame] | 387 | int err; |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 388 | |
Jean Delvare | b2469f4 | 2010-10-28 20:31:45 +0200 | [diff] [blame] | 389 | /* If the same bank is already set, nothing to do */ |
| 390 | if ((data->bank & 0x07) == bank) |
| 391 | return 0; |
| 392 | |
| 393 | /* Change to new bank, preserve all other bits */ |
| 394 | bank |= data->bank & ~0x07; |
| 395 | err = i2c_smbus_write_byte_data(client, W83795_REG_BANKSEL, bank); |
| 396 | if (err < 0) { |
| 397 | dev_err(&client->dev, |
| 398 | "Failed to set bank to %d, err %d\n", |
| 399 | (int)bank, err); |
| 400 | return err; |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 401 | } |
Jean Delvare | b2469f4 | 2010-10-28 20:31:45 +0200 | [diff] [blame] | 402 | data->bank = bank; |
| 403 | |
| 404 | return 0; |
| 405 | } |
| 406 | |
| 407 | /* Must be called with data->update_lock held, except during initialization */ |
| 408 | static u8 w83795_read(struct i2c_client *client, u16 reg) |
| 409 | { |
| 410 | int err; |
| 411 | |
| 412 | err = w83795_set_bank(client, reg >> 8); |
| 413 | if (err < 0) |
| 414 | return 0x00; /* Arbitrary */ |
| 415 | |
| 416 | err = i2c_smbus_read_byte_data(client, reg & 0xff); |
| 417 | if (err < 0) { |
| 418 | dev_err(&client->dev, |
| 419 | "Failed to read from register 0x%03x, err %d\n", |
| 420 | (int)reg, err); |
| 421 | return 0x00; /* Arbitrary */ |
| 422 | } |
| 423 | return err; |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | /* Must be called with data->update_lock held, except during initialization */ |
| 427 | static int w83795_write(struct i2c_client *client, u16 reg, u8 value) |
| 428 | { |
Jean Delvare | b2469f4 | 2010-10-28 20:31:45 +0200 | [diff] [blame] | 429 | int err; |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 430 | |
Jean Delvare | b2469f4 | 2010-10-28 20:31:45 +0200 | [diff] [blame] | 431 | err = w83795_set_bank(client, reg >> 8); |
| 432 | if (err < 0) |
| 433 | return err; |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 434 | |
Jean Delvare | b2469f4 | 2010-10-28 20:31:45 +0200 | [diff] [blame] | 435 | err = i2c_smbus_write_byte_data(client, reg & 0xff, value); |
| 436 | if (err < 0) |
| 437 | dev_err(&client->dev, |
| 438 | "Failed to write to register 0x%03x, err %d\n", |
| 439 | (int)reg, err); |
| 440 | return err; |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 441 | } |
| 442 | |
Jean Delvare | 0d7237bf | 2010-10-28 20:31:48 +0200 | [diff] [blame] | 443 | static void w83795_update_limits(struct i2c_client *client) |
| 444 | { |
| 445 | struct w83795_data *data = i2c_get_clientdata(client); |
| 446 | int i, limit; |
| 447 | |
| 448 | /* Read the voltage limits */ |
| 449 | for (i = 0; i < ARRAY_SIZE(data->in); i++) { |
| 450 | if (!(data->has_in & (1 << i))) |
| 451 | continue; |
| 452 | data->in[i][IN_MAX] = |
| 453 | w83795_read(client, W83795_REG_IN[i][IN_MAX]); |
| 454 | data->in[i][IN_LOW] = |
| 455 | w83795_read(client, W83795_REG_IN[i][IN_LOW]); |
| 456 | } |
| 457 | for (i = 0; i < ARRAY_SIZE(data->in_lsb); i++) { |
| 458 | if ((i == 2 && data->chip_type == w83795adg) || |
| 459 | (i >= 4 && !(data->has_in & (1 << (i + 11))))) |
| 460 | continue; |
| 461 | data->in_lsb[i][IN_MAX] = |
| 462 | w83795_read(client, IN_LSB_REG(i, IN_MAX)); |
| 463 | data->in_lsb[i][IN_LOW] = |
| 464 | w83795_read(client, IN_LSB_REG(i, IN_LOW)); |
| 465 | } |
| 466 | |
| 467 | /* Read the fan limits */ |
| 468 | for (i = 0; i < ARRAY_SIZE(data->fan); i++) { |
| 469 | u8 lsb; |
| 470 | |
| 471 | /* Each register contains LSB for 2 fans, but we want to |
| 472 | * read it only once to save time */ |
| 473 | if ((i & 1) == 0 && (data->has_fan & (3 << i))) |
| 474 | lsb = w83795_read(client, W83795_REG_FAN_MIN_LSB(i)); |
| 475 | |
| 476 | if (!(data->has_fan & (1 << i))) |
| 477 | continue; |
| 478 | data->fan_min[i] = |
| 479 | w83795_read(client, W83795_REG_FAN_MIN_HL(i)) << 4; |
| 480 | data->fan_min[i] |= |
| 481 | (lsb >> W83795_REG_FAN_MIN_LSB_SHIFT(i)) & 0x0F; |
| 482 | } |
| 483 | |
| 484 | /* Read the temperature limits */ |
| 485 | for (i = 0; i < ARRAY_SIZE(data->temp); i++) { |
| 486 | if (!(data->has_temp & (1 << i))) |
| 487 | continue; |
| 488 | for (limit = TEMP_CRIT; limit <= TEMP_WARN_HYST; limit++) |
| 489 | data->temp[i][limit] = |
| 490 | w83795_read(client, W83795_REG_TEMP[i][limit]); |
| 491 | } |
| 492 | |
| 493 | /* Read the DTS limits */ |
| 494 | if (data->enable_dts != 0) { |
| 495 | for (limit = DTS_CRIT; limit <= DTS_WARN_HYST; limit++) |
| 496 | data->dts_ext[limit] = |
| 497 | w83795_read(client, W83795_REG_DTS_EXT(limit)); |
| 498 | } |
| 499 | |
| 500 | /* Read beep settings */ |
| 501 | for (i = 0; i < ARRAY_SIZE(data->beeps); i++) |
| 502 | data->beeps[i] = w83795_read(client, W83795_REG_BEEP(i)); |
Jean Delvare | 2ae61de | 2010-10-28 20:31:48 +0200 | [diff] [blame] | 503 | |
| 504 | data->valid_limits = 1; |
Jean Delvare | 0d7237bf | 2010-10-28 20:31:48 +0200 | [diff] [blame] | 505 | } |
| 506 | |
Jean Delvare | 1bb3450 | 2010-10-28 20:31:48 +0200 | [diff] [blame^] | 507 | static struct w83795_data *w83795_update_pwm_config(struct device *dev) |
Jean Delvare | 0d7237bf | 2010-10-28 20:31:48 +0200 | [diff] [blame] | 508 | { |
Jean Delvare | 1bb3450 | 2010-10-28 20:31:48 +0200 | [diff] [blame^] | 509 | struct i2c_client *client = to_i2c_client(dev); |
Jean Delvare | 0d7237bf | 2010-10-28 20:31:48 +0200 | [diff] [blame] | 510 | struct w83795_data *data = i2c_get_clientdata(client); |
| 511 | int i, tmp; |
| 512 | |
Jean Delvare | 1bb3450 | 2010-10-28 20:31:48 +0200 | [diff] [blame^] | 513 | mutex_lock(&data->update_lock); |
| 514 | |
| 515 | if (data->valid_pwm_config) |
| 516 | goto END; |
| 517 | |
Jean Delvare | 0d7237bf | 2010-10-28 20:31:48 +0200 | [diff] [blame] | 518 | /* Read temperature source selection */ |
| 519 | for (i = 0; i < ARRAY_SIZE(data->temp_src); i++) |
| 520 | data->temp_src[i] = w83795_read(client, W83795_REG_TSS(i)); |
| 521 | |
| 522 | /* Read automatic fan speed control settings */ |
| 523 | data->pwm_fcms[0] = w83795_read(client, W83795_REG_FCMS1); |
| 524 | data->pwm_fcms[1] = w83795_read(client, W83795_REG_FCMS2); |
| 525 | for (i = 0; i < ARRAY_SIZE(data->pwm_tfmr); i++) |
| 526 | data->pwm_tfmr[i] = w83795_read(client, W83795_REG_TFMR(i)); |
| 527 | data->pwm_fomc = w83795_read(client, W83795_REG_FOMC); |
| 528 | for (i = 0; i < data->has_pwm; i++) { |
| 529 | for (tmp = PWM_FREQ; tmp <= PWM_STOP_TIME; tmp++) |
| 530 | data->pwm[i][tmp] = |
| 531 | w83795_read(client, W83795_REG_PWM(i, tmp)); |
| 532 | } |
| 533 | for (i = 0; i < ARRAY_SIZE(data->target_speed); i++) { |
| 534 | data->target_speed[i] = |
| 535 | w83795_read(client, W83795_REG_FTSH(i)) << 4; |
| 536 | data->target_speed[i] |= |
| 537 | w83795_read(client, W83795_REG_FTSL(i)) >> 4; |
| 538 | } |
| 539 | data->tol_speed = w83795_read(client, W83795_REG_TFTS) & 0x3f; |
| 540 | |
| 541 | for (i = 0; i < ARRAY_SIZE(data->pwm_temp); i++) { |
| 542 | data->pwm_temp[i][TEMP_PWM_TTTI] = |
| 543 | w83795_read(client, W83795_REG_TTTI(i)) & 0x7f; |
| 544 | data->pwm_temp[i][TEMP_PWM_CTFS] = |
| 545 | w83795_read(client, W83795_REG_CTFS(i)); |
| 546 | tmp = w83795_read(client, W83795_REG_HT(i)); |
| 547 | data->pwm_temp[i][TEMP_PWM_HCT] = (tmp >> 4) & 0x0f; |
| 548 | data->pwm_temp[i][TEMP_PWM_HOT] = tmp & 0x0f; |
| 549 | } |
| 550 | |
| 551 | /* Read SmartFanIV trip points */ |
| 552 | for (i = 0; i < ARRAY_SIZE(data->sf4_reg); i++) { |
| 553 | for (tmp = 0; tmp < 7; tmp++) { |
| 554 | data->sf4_reg[i][SF4_TEMP][tmp] = |
| 555 | w83795_read(client, |
| 556 | W83795_REG_SF4_TEMP(i, tmp)); |
| 557 | data->sf4_reg[i][SF4_PWM][tmp] = |
| 558 | w83795_read(client, W83795_REG_SF4_PWM(i, tmp)); |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | /* Read setup PWM */ |
| 563 | for (i = 0; i < ARRAY_SIZE(data->setup_pwm); i++) |
| 564 | data->setup_pwm[i] = |
| 565 | w83795_read(client, W83795_REG_SETUP_PWM(i)); |
Jean Delvare | 1bb3450 | 2010-10-28 20:31:48 +0200 | [diff] [blame^] | 566 | |
| 567 | data->valid_pwm_config = 1; |
| 568 | |
| 569 | END: |
| 570 | mutex_unlock(&data->update_lock); |
| 571 | return data; |
Jean Delvare | 0d7237bf | 2010-10-28 20:31:48 +0200 | [diff] [blame] | 572 | } |
| 573 | |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 574 | static struct w83795_data *w83795_update_device(struct device *dev) |
| 575 | { |
| 576 | struct i2c_client *client = to_i2c_client(dev); |
| 577 | struct w83795_data *data = i2c_get_clientdata(client); |
| 578 | u16 tmp; |
| 579 | int i; |
| 580 | |
| 581 | mutex_lock(&data->update_lock); |
| 582 | |
Jean Delvare | 2ae61de | 2010-10-28 20:31:48 +0200 | [diff] [blame] | 583 | if (!data->valid_limits) |
| 584 | w83795_update_limits(client); |
| 585 | |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 586 | if (!(time_after(jiffies, data->last_updated + HZ * 2) |
| 587 | || !data->valid)) |
| 588 | goto END; |
| 589 | |
| 590 | /* Update the voltages value */ |
| 591 | for (i = 0; i < ARRAY_SIZE(data->in); i++) { |
| 592 | if (!(data->has_in & (1 << i))) |
| 593 | continue; |
| 594 | tmp = w83795_read(client, W83795_REG_IN[i][IN_READ]) << 2; |
Jean Delvare | a654b9d4 | 2010-10-28 20:31:47 +0200 | [diff] [blame] | 595 | tmp |= w83795_read(client, W83795_REG_VRLSB) >> 6; |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 596 | data->in[i][IN_READ] = tmp; |
| 597 | } |
| 598 | |
Jean Delvare | 0e25601 | 2010-10-28 20:31:46 +0200 | [diff] [blame] | 599 | /* in0-2 can have dynamic limits (W83795G only) */ |
| 600 | if (data->has_dyn_in) { |
| 601 | u8 lsb_max = w83795_read(client, IN_LSB_REG(0, IN_MAX)); |
| 602 | u8 lsb_low = w83795_read(client, IN_LSB_REG(0, IN_LOW)); |
| 603 | |
| 604 | for (i = 0; i < 3; i++) { |
| 605 | if (!(data->has_dyn_in & (1 << i))) |
| 606 | continue; |
| 607 | data->in[i][IN_MAX] = |
| 608 | w83795_read(client, W83795_REG_IN[i][IN_MAX]); |
| 609 | data->in[i][IN_LOW] = |
| 610 | w83795_read(client, W83795_REG_IN[i][IN_LOW]); |
| 611 | data->in_lsb[i][IN_MAX] = (lsb_max >> (2 * i)) & 0x03; |
| 612 | data->in_lsb[i][IN_LOW] = (lsb_low >> (2 * i)) & 0x03; |
| 613 | } |
| 614 | } |
| 615 | |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 616 | /* Update fan */ |
| 617 | for (i = 0; i < ARRAY_SIZE(data->fan); i++) { |
| 618 | if (!(data->has_fan & (1 << i))) |
| 619 | continue; |
| 620 | data->fan[i] = w83795_read(client, W83795_REG_FAN(i)) << 4; |
| 621 | data->fan[i] |= |
Jean Delvare | 6c82b2f | 2010-10-28 20:31:46 +0200 | [diff] [blame] | 622 | (w83795_read(client, W83795_REG_VRLSB) >> 4) & 0x0F; |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | /* Update temperature */ |
| 626 | for (i = 0; i < ARRAY_SIZE(data->temp); i++) { |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 627 | data->temp[i][TEMP_READ] = |
| 628 | w83795_read(client, W83795_REG_TEMP[i][TEMP_READ]); |
| 629 | data->temp_read_vrlsb[i] = |
| 630 | w83795_read(client, W83795_REG_VRLSB); |
| 631 | } |
| 632 | |
| 633 | /* Update dts temperature */ |
| 634 | if (data->enable_dts != 0) { |
| 635 | for (i = 0; i < ARRAY_SIZE(data->dts); i++) { |
| 636 | if (!(data->has_dts & (1 << i))) |
| 637 | continue; |
| 638 | data->dts[i] = |
| 639 | w83795_read(client, W83795_REG_DTS(i)); |
| 640 | data->dts_read_vrlsb[i] = |
| 641 | w83795_read(client, W83795_REG_VRLSB); |
| 642 | } |
| 643 | } |
| 644 | |
| 645 | /* Update pwm output */ |
| 646 | for (i = 0; i < data->has_pwm; i++) { |
| 647 | data->pwm[i][PWM_OUTPUT] = |
| 648 | w83795_read(client, W83795_REG_PWM(i, PWM_OUTPUT)); |
| 649 | } |
| 650 | |
| 651 | /* update alarm */ |
Jean Delvare | cd316df | 2010-10-28 20:31:48 +0200 | [diff] [blame] | 652 | for (i = 0; i < ARRAY_SIZE(data->alarms); i++) |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 653 | data->alarms[i] = w83795_read(client, W83795_REG_ALARM(i)); |
| 654 | |
| 655 | data->last_updated = jiffies; |
| 656 | data->valid = 1; |
| 657 | |
| 658 | END: |
| 659 | mutex_unlock(&data->update_lock); |
| 660 | return data; |
| 661 | } |
| 662 | |
| 663 | /* |
| 664 | * Sysfs attributes |
| 665 | */ |
| 666 | |
| 667 | #define ALARM_STATUS 0 |
| 668 | #define BEEP_ENABLE 1 |
| 669 | static ssize_t |
| 670 | show_alarm_beep(struct device *dev, struct device_attribute *attr, char *buf) |
| 671 | { |
| 672 | struct w83795_data *data = w83795_update_device(dev); |
| 673 | struct sensor_device_attribute_2 *sensor_attr = |
| 674 | to_sensor_dev_attr_2(attr); |
| 675 | int nr = sensor_attr->nr; |
| 676 | int index = sensor_attr->index >> 3; |
| 677 | int bit = sensor_attr->index & 0x07; |
| 678 | u8 val; |
| 679 | |
| 680 | if (ALARM_STATUS == nr) { |
| 681 | val = (data->alarms[index] >> (bit)) & 1; |
| 682 | } else { /* BEEP_ENABLE */ |
| 683 | val = (data->beeps[index] >> (bit)) & 1; |
| 684 | } |
| 685 | |
| 686 | return sprintf(buf, "%u\n", val); |
| 687 | } |
| 688 | |
| 689 | static ssize_t |
| 690 | store_beep(struct device *dev, struct device_attribute *attr, |
| 691 | const char *buf, size_t count) |
| 692 | { |
| 693 | struct i2c_client *client = to_i2c_client(dev); |
| 694 | struct w83795_data *data = i2c_get_clientdata(client); |
| 695 | struct sensor_device_attribute_2 *sensor_attr = |
| 696 | to_sensor_dev_attr_2(attr); |
| 697 | int index = sensor_attr->index >> 3; |
| 698 | int shift = sensor_attr->index & 0x07; |
| 699 | u8 beep_bit = 1 << shift; |
| 700 | unsigned long val; |
| 701 | |
| 702 | if (strict_strtoul(buf, 10, &val) < 0) |
| 703 | return -EINVAL; |
| 704 | if (val != 0 && val != 1) |
| 705 | return -EINVAL; |
| 706 | |
| 707 | mutex_lock(&data->update_lock); |
| 708 | data->beeps[index] = w83795_read(client, W83795_REG_BEEP(index)); |
| 709 | data->beeps[index] &= ~beep_bit; |
| 710 | data->beeps[index] |= val << shift; |
| 711 | w83795_write(client, W83795_REG_BEEP(index), data->beeps[index]); |
| 712 | mutex_unlock(&data->update_lock); |
| 713 | |
| 714 | return count; |
| 715 | } |
| 716 | |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 717 | /* Write any value to clear chassis alarm */ |
| 718 | static ssize_t |
| 719 | store_chassis_clear(struct device *dev, |
| 720 | struct device_attribute *attr, const char *buf, |
| 721 | size_t count) |
| 722 | { |
| 723 | struct i2c_client *client = to_i2c_client(dev); |
| 724 | struct w83795_data *data = i2c_get_clientdata(client); |
| 725 | u8 val; |
| 726 | |
| 727 | mutex_lock(&data->update_lock); |
| 728 | val = w83795_read(client, W83795_REG_CLR_CHASSIS); |
| 729 | val |= 0x80; |
| 730 | w83795_write(client, W83795_REG_CLR_CHASSIS, val); |
| 731 | mutex_unlock(&data->update_lock); |
| 732 | return count; |
| 733 | } |
| 734 | |
| 735 | #define FAN_INPUT 0 |
| 736 | #define FAN_MIN 1 |
| 737 | static ssize_t |
| 738 | show_fan(struct device *dev, struct device_attribute *attr, char *buf) |
| 739 | { |
| 740 | struct sensor_device_attribute_2 *sensor_attr = |
| 741 | to_sensor_dev_attr_2(attr); |
| 742 | int nr = sensor_attr->nr; |
| 743 | int index = sensor_attr->index; |
| 744 | struct w83795_data *data = w83795_update_device(dev); |
| 745 | u16 val; |
| 746 | |
| 747 | if (FAN_INPUT == nr) |
| 748 | val = data->fan[index] & 0x0fff; |
| 749 | else |
| 750 | val = data->fan_min[index] & 0x0fff; |
| 751 | |
| 752 | return sprintf(buf, "%lu\n", fan_from_reg(val)); |
| 753 | } |
| 754 | |
| 755 | static ssize_t |
| 756 | store_fan_min(struct device *dev, struct device_attribute *attr, |
| 757 | const char *buf, size_t count) |
| 758 | { |
| 759 | struct sensor_device_attribute_2 *sensor_attr = |
| 760 | to_sensor_dev_attr_2(attr); |
| 761 | int index = sensor_attr->index; |
| 762 | struct i2c_client *client = to_i2c_client(dev); |
| 763 | struct w83795_data *data = i2c_get_clientdata(client); |
| 764 | unsigned long val; |
| 765 | |
| 766 | if (strict_strtoul(buf, 10, &val)) |
| 767 | return -EINVAL; |
| 768 | val = fan_to_reg(val); |
| 769 | |
| 770 | mutex_lock(&data->update_lock); |
| 771 | data->fan_min[index] = val; |
| 772 | w83795_write(client, W83795_REG_FAN_MIN_HL(index), (val >> 4) & 0xff); |
| 773 | val &= 0x0f; |
Jean Delvare | 7eb8d50 | 2010-10-28 20:31:47 +0200 | [diff] [blame] | 774 | if (index & 1) { |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 775 | val <<= 4; |
| 776 | val |= w83795_read(client, W83795_REG_FAN_MIN_LSB(index)) |
| 777 | & 0x0f; |
| 778 | } else { |
| 779 | val |= w83795_read(client, W83795_REG_FAN_MIN_LSB(index)) |
| 780 | & 0xf0; |
| 781 | } |
| 782 | w83795_write(client, W83795_REG_FAN_MIN_LSB(index), val & 0xff); |
| 783 | mutex_unlock(&data->update_lock); |
| 784 | |
| 785 | return count; |
| 786 | } |
| 787 | |
| 788 | static ssize_t |
| 789 | show_pwm(struct device *dev, struct device_attribute *attr, char *buf) |
| 790 | { |
Jean Delvare | 1bb3450 | 2010-10-28 20:31:48 +0200 | [diff] [blame^] | 791 | struct w83795_data *data; |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 792 | struct sensor_device_attribute_2 *sensor_attr = |
| 793 | to_sensor_dev_attr_2(attr); |
| 794 | int nr = sensor_attr->nr; |
| 795 | int index = sensor_attr->index; |
Jean Delvare | 01879a8 | 2010-10-28 20:31:47 +0200 | [diff] [blame] | 796 | unsigned int val; |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 797 | |
Jean Delvare | 1bb3450 | 2010-10-28 20:31:48 +0200 | [diff] [blame^] | 798 | data = nr == PWM_OUTPUT ? w83795_update_device(dev) |
| 799 | : w83795_update_pwm_config(dev); |
| 800 | |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 801 | switch (nr) { |
| 802 | case PWM_STOP_TIME: |
| 803 | val = time_from_reg(data->pwm[index][nr]); |
| 804 | break; |
Jean Delvare | 01879a8 | 2010-10-28 20:31:47 +0200 | [diff] [blame] | 805 | case PWM_FREQ: |
| 806 | val = pwm_freq_from_reg(data->pwm[index][nr], data->clkin); |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 807 | break; |
| 808 | default: |
| 809 | val = data->pwm[index][nr]; |
| 810 | break; |
| 811 | } |
| 812 | |
| 813 | return sprintf(buf, "%u\n", val); |
| 814 | } |
| 815 | |
| 816 | static ssize_t |
| 817 | store_pwm(struct device *dev, struct device_attribute *attr, |
| 818 | const char *buf, size_t count) |
| 819 | { |
| 820 | struct i2c_client *client = to_i2c_client(dev); |
| 821 | struct w83795_data *data = i2c_get_clientdata(client); |
| 822 | struct sensor_device_attribute_2 *sensor_attr = |
| 823 | to_sensor_dev_attr_2(attr); |
| 824 | int nr = sensor_attr->nr; |
| 825 | int index = sensor_attr->index; |
| 826 | unsigned long val; |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 827 | |
| 828 | if (strict_strtoul(buf, 10, &val) < 0) |
| 829 | return -EINVAL; |
| 830 | |
| 831 | mutex_lock(&data->update_lock); |
| 832 | switch (nr) { |
| 833 | case PWM_STOP_TIME: |
| 834 | val = time_to_reg(val); |
| 835 | break; |
Jean Delvare | 01879a8 | 2010-10-28 20:31:47 +0200 | [diff] [blame] | 836 | case PWM_FREQ: |
| 837 | val = pwm_freq_to_reg(val, data->clkin); |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 838 | break; |
| 839 | default: |
| 840 | val = SENSORS_LIMIT(val, 0, 0xff); |
| 841 | break; |
| 842 | } |
| 843 | w83795_write(client, W83795_REG_PWM(index, nr), val); |
Jean Delvare | 01879a8 | 2010-10-28 20:31:47 +0200 | [diff] [blame] | 844 | data->pwm[index][nr] = val; |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 845 | mutex_unlock(&data->update_lock); |
| 846 | return count; |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 847 | } |
| 848 | |
| 849 | static ssize_t |
| 850 | show_pwm_enable(struct device *dev, struct device_attribute *attr, char *buf) |
| 851 | { |
| 852 | struct sensor_device_attribute_2 *sensor_attr = |
| 853 | to_sensor_dev_attr_2(attr); |
Jean Delvare | 1bb3450 | 2010-10-28 20:31:48 +0200 | [diff] [blame^] | 854 | struct w83795_data *data = w83795_update_pwm_config(dev); |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 855 | int index = sensor_attr->index; |
| 856 | u8 tmp; |
| 857 | |
| 858 | if (1 == (data->pwm_fcms[0] & (1 << index))) { |
| 859 | tmp = 2; |
| 860 | goto out; |
| 861 | } |
| 862 | for (tmp = 0; tmp < 6; tmp++) { |
| 863 | if (data->pwm_tfmr[tmp] & (1 << index)) { |
| 864 | tmp = 3; |
| 865 | goto out; |
| 866 | } |
| 867 | } |
| 868 | if (data->pwm_fomc & (1 << index)) |
| 869 | tmp = 0; |
| 870 | else |
| 871 | tmp = 1; |
| 872 | |
| 873 | out: |
| 874 | return sprintf(buf, "%u\n", tmp); |
| 875 | } |
| 876 | |
| 877 | static ssize_t |
| 878 | store_pwm_enable(struct device *dev, struct device_attribute *attr, |
| 879 | const char *buf, size_t count) |
| 880 | { |
| 881 | struct i2c_client *client = to_i2c_client(dev); |
Jean Delvare | 1bb3450 | 2010-10-28 20:31:48 +0200 | [diff] [blame^] | 882 | struct w83795_data *data = w83795_update_pwm_config(dev); |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 883 | struct sensor_device_attribute_2 *sensor_attr = |
| 884 | to_sensor_dev_attr_2(attr); |
| 885 | int index = sensor_attr->index; |
| 886 | unsigned long val; |
| 887 | int i; |
| 888 | |
| 889 | if (strict_strtoul(buf, 10, &val) < 0) |
| 890 | return -EINVAL; |
| 891 | if (val > 2) |
| 892 | return -EINVAL; |
| 893 | |
| 894 | mutex_lock(&data->update_lock); |
| 895 | switch (val) { |
| 896 | case 0: |
| 897 | case 1: |
| 898 | data->pwm_fcms[0] &= ~(1 << index); |
| 899 | w83795_write(client, W83795_REG_FCMS1, data->pwm_fcms[0]); |
| 900 | for (i = 0; i < 6; i++) { |
| 901 | data->pwm_tfmr[i] &= ~(1 << index); |
| 902 | w83795_write(client, W83795_REG_TFMR(i), |
| 903 | data->pwm_tfmr[i]); |
| 904 | } |
| 905 | data->pwm_fomc |= 1 << index; |
| 906 | data->pwm_fomc ^= val << index; |
| 907 | w83795_write(client, W83795_REG_FOMC, data->pwm_fomc); |
| 908 | break; |
| 909 | case 2: |
| 910 | data->pwm_fcms[0] |= (1 << index); |
| 911 | w83795_write(client, W83795_REG_FCMS1, data->pwm_fcms[0]); |
| 912 | break; |
| 913 | } |
| 914 | mutex_unlock(&data->update_lock); |
| 915 | return count; |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 916 | } |
| 917 | |
| 918 | static ssize_t |
| 919 | show_temp_src(struct device *dev, struct device_attribute *attr, char *buf) |
| 920 | { |
| 921 | struct sensor_device_attribute_2 *sensor_attr = |
| 922 | to_sensor_dev_attr_2(attr); |
Jean Delvare | 1bb3450 | 2010-10-28 20:31:48 +0200 | [diff] [blame^] | 923 | struct w83795_data *data = w83795_update_pwm_config(dev); |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 924 | int index = sensor_attr->index; |
| 925 | u8 val = index / 2; |
| 926 | u8 tmp = data->temp_src[val]; |
| 927 | |
Jean Delvare | 7eb8d50 | 2010-10-28 20:31:47 +0200 | [diff] [blame] | 928 | if (index & 1) |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 929 | val = 4; |
| 930 | else |
| 931 | val = 0; |
| 932 | tmp >>= val; |
| 933 | tmp &= 0x0f; |
| 934 | |
| 935 | return sprintf(buf, "%u\n", tmp); |
| 936 | } |
| 937 | |
| 938 | static ssize_t |
| 939 | store_temp_src(struct device *dev, struct device_attribute *attr, |
| 940 | const char *buf, size_t count) |
| 941 | { |
| 942 | struct i2c_client *client = to_i2c_client(dev); |
Jean Delvare | 1bb3450 | 2010-10-28 20:31:48 +0200 | [diff] [blame^] | 943 | struct w83795_data *data = w83795_update_pwm_config(dev); |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 944 | struct sensor_device_attribute_2 *sensor_attr = |
| 945 | to_sensor_dev_attr_2(attr); |
| 946 | int index = sensor_attr->index; |
| 947 | unsigned long tmp; |
| 948 | u8 val = index / 2; |
| 949 | |
| 950 | if (strict_strtoul(buf, 10, &tmp) < 0) |
| 951 | return -EINVAL; |
| 952 | tmp = SENSORS_LIMIT(tmp, 0, 15); |
| 953 | |
| 954 | mutex_lock(&data->update_lock); |
Jean Delvare | 7eb8d50 | 2010-10-28 20:31:47 +0200 | [diff] [blame] | 955 | if (index & 1) { |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 956 | tmp <<= 4; |
| 957 | data->temp_src[val] &= 0x0f; |
| 958 | } else { |
| 959 | data->temp_src[val] &= 0xf0; |
| 960 | } |
| 961 | data->temp_src[val] |= tmp; |
| 962 | w83795_write(client, W83795_REG_TSS(val), data->temp_src[val]); |
| 963 | mutex_unlock(&data->update_lock); |
| 964 | |
| 965 | return count; |
| 966 | } |
| 967 | |
| 968 | #define TEMP_PWM_ENABLE 0 |
| 969 | #define TEMP_PWM_FAN_MAP 1 |
| 970 | static ssize_t |
| 971 | show_temp_pwm_enable(struct device *dev, struct device_attribute *attr, |
| 972 | char *buf) |
| 973 | { |
Jean Delvare | 1bb3450 | 2010-10-28 20:31:48 +0200 | [diff] [blame^] | 974 | struct w83795_data *data = w83795_update_pwm_config(dev); |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 975 | struct sensor_device_attribute_2 *sensor_attr = |
| 976 | to_sensor_dev_attr_2(attr); |
| 977 | int nr = sensor_attr->nr; |
| 978 | int index = sensor_attr->index; |
| 979 | u8 tmp = 0xff; |
| 980 | |
| 981 | switch (nr) { |
| 982 | case TEMP_PWM_ENABLE: |
| 983 | tmp = (data->pwm_fcms[1] >> index) & 1; |
| 984 | if (tmp) |
| 985 | tmp = 4; |
| 986 | else |
| 987 | tmp = 3; |
| 988 | break; |
| 989 | case TEMP_PWM_FAN_MAP: |
| 990 | tmp = data->pwm_tfmr[index]; |
| 991 | break; |
| 992 | } |
| 993 | |
| 994 | return sprintf(buf, "%u\n", tmp); |
| 995 | } |
| 996 | |
| 997 | static ssize_t |
| 998 | store_temp_pwm_enable(struct device *dev, struct device_attribute *attr, |
| 999 | const char *buf, size_t count) |
| 1000 | { |
| 1001 | struct i2c_client *client = to_i2c_client(dev); |
Jean Delvare | 1bb3450 | 2010-10-28 20:31:48 +0200 | [diff] [blame^] | 1002 | struct w83795_data *data = w83795_update_pwm_config(dev); |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 1003 | struct sensor_device_attribute_2 *sensor_attr = |
| 1004 | to_sensor_dev_attr_2(attr); |
| 1005 | int nr = sensor_attr->nr; |
| 1006 | int index = sensor_attr->index; |
| 1007 | unsigned long tmp; |
| 1008 | |
| 1009 | if (strict_strtoul(buf, 10, &tmp) < 0) |
| 1010 | return -EINVAL; |
| 1011 | |
| 1012 | switch (nr) { |
| 1013 | case TEMP_PWM_ENABLE: |
| 1014 | if ((tmp != 3) && (tmp != 4)) |
| 1015 | return -EINVAL; |
| 1016 | tmp -= 3; |
| 1017 | mutex_lock(&data->update_lock); |
| 1018 | data->pwm_fcms[1] &= ~(1 << index); |
| 1019 | data->pwm_fcms[1] |= tmp << index; |
| 1020 | w83795_write(client, W83795_REG_FCMS2, data->pwm_fcms[1]); |
| 1021 | mutex_unlock(&data->update_lock); |
| 1022 | break; |
| 1023 | case TEMP_PWM_FAN_MAP: |
| 1024 | mutex_lock(&data->update_lock); |
| 1025 | tmp = SENSORS_LIMIT(tmp, 0, 0xff); |
| 1026 | w83795_write(client, W83795_REG_TFMR(index), tmp); |
| 1027 | data->pwm_tfmr[index] = tmp; |
| 1028 | mutex_unlock(&data->update_lock); |
| 1029 | break; |
| 1030 | } |
| 1031 | return count; |
| 1032 | } |
| 1033 | |
| 1034 | #define FANIN_TARGET 0 |
| 1035 | #define FANIN_TOL 1 |
| 1036 | static ssize_t |
| 1037 | show_fanin(struct device *dev, struct device_attribute *attr, char *buf) |
| 1038 | { |
Jean Delvare | 1bb3450 | 2010-10-28 20:31:48 +0200 | [diff] [blame^] | 1039 | struct w83795_data *data = w83795_update_pwm_config(dev); |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 1040 | struct sensor_device_attribute_2 *sensor_attr = |
| 1041 | to_sensor_dev_attr_2(attr); |
| 1042 | int nr = sensor_attr->nr; |
| 1043 | int index = sensor_attr->index; |
| 1044 | u16 tmp = 0; |
| 1045 | |
| 1046 | switch (nr) { |
| 1047 | case FANIN_TARGET: |
| 1048 | tmp = fan_from_reg(data->target_speed[index]); |
| 1049 | break; |
| 1050 | case FANIN_TOL: |
| 1051 | tmp = data->tol_speed; |
| 1052 | break; |
| 1053 | } |
| 1054 | |
| 1055 | return sprintf(buf, "%u\n", tmp); |
| 1056 | } |
| 1057 | |
| 1058 | static ssize_t |
| 1059 | store_fanin(struct device *dev, struct device_attribute *attr, |
| 1060 | const char *buf, size_t count) |
| 1061 | { |
| 1062 | struct i2c_client *client = to_i2c_client(dev); |
| 1063 | struct w83795_data *data = i2c_get_clientdata(client); |
| 1064 | struct sensor_device_attribute_2 *sensor_attr = |
| 1065 | to_sensor_dev_attr_2(attr); |
| 1066 | int nr = sensor_attr->nr; |
| 1067 | int index = sensor_attr->index; |
| 1068 | unsigned long val; |
| 1069 | |
| 1070 | if (strict_strtoul(buf, 10, &val) < 0) |
| 1071 | return -EINVAL; |
| 1072 | |
| 1073 | mutex_lock(&data->update_lock); |
| 1074 | switch (nr) { |
| 1075 | case FANIN_TARGET: |
| 1076 | val = fan_to_reg(SENSORS_LIMIT(val, 0, 0xfff)); |
| 1077 | w83795_write(client, W83795_REG_FTSH(index), (val >> 4) & 0xff); |
| 1078 | w83795_write(client, W83795_REG_FTSL(index), (val << 4) & 0xf0); |
| 1079 | data->target_speed[index] = val; |
| 1080 | break; |
| 1081 | case FANIN_TOL: |
| 1082 | val = SENSORS_LIMIT(val, 0, 0x3f); |
| 1083 | w83795_write(client, W83795_REG_TFTS, val); |
| 1084 | data->tol_speed = val; |
| 1085 | break; |
| 1086 | } |
| 1087 | mutex_unlock(&data->update_lock); |
| 1088 | |
| 1089 | return count; |
| 1090 | } |
| 1091 | |
| 1092 | |
| 1093 | static ssize_t |
| 1094 | show_temp_pwm(struct device *dev, struct device_attribute *attr, char *buf) |
| 1095 | { |
Jean Delvare | 1bb3450 | 2010-10-28 20:31:48 +0200 | [diff] [blame^] | 1096 | struct w83795_data *data = w83795_update_pwm_config(dev); |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 1097 | struct sensor_device_attribute_2 *sensor_attr = |
| 1098 | to_sensor_dev_attr_2(attr); |
| 1099 | int nr = sensor_attr->nr; |
| 1100 | int index = sensor_attr->index; |
| 1101 | long tmp = temp_from_reg(data->pwm_temp[index][nr]); |
| 1102 | |
| 1103 | return sprintf(buf, "%ld\n", tmp); |
| 1104 | } |
| 1105 | |
| 1106 | static ssize_t |
| 1107 | store_temp_pwm(struct device *dev, struct device_attribute *attr, |
| 1108 | const char *buf, size_t count) |
| 1109 | { |
| 1110 | struct i2c_client *client = to_i2c_client(dev); |
| 1111 | struct w83795_data *data = i2c_get_clientdata(client); |
| 1112 | struct sensor_device_attribute_2 *sensor_attr = |
| 1113 | to_sensor_dev_attr_2(attr); |
| 1114 | int nr = sensor_attr->nr; |
| 1115 | int index = sensor_attr->index; |
| 1116 | unsigned long val; |
| 1117 | u8 tmp; |
| 1118 | |
| 1119 | if (strict_strtoul(buf, 10, &val) < 0) |
| 1120 | return -EINVAL; |
| 1121 | val /= 1000; |
| 1122 | |
| 1123 | mutex_lock(&data->update_lock); |
| 1124 | switch (nr) { |
| 1125 | case TEMP_PWM_TTTI: |
| 1126 | val = SENSORS_LIMIT(val, 0, 0x7f); |
| 1127 | w83795_write(client, W83795_REG_TTTI(index), val); |
| 1128 | break; |
| 1129 | case TEMP_PWM_CTFS: |
| 1130 | val = SENSORS_LIMIT(val, 0, 0x7f); |
| 1131 | w83795_write(client, W83795_REG_CTFS(index), val); |
| 1132 | break; |
| 1133 | case TEMP_PWM_HCT: |
| 1134 | val = SENSORS_LIMIT(val, 0, 0x0f); |
| 1135 | tmp = w83795_read(client, W83795_REG_HT(index)); |
| 1136 | tmp &= 0x0f; |
| 1137 | tmp |= (val << 4) & 0xf0; |
| 1138 | w83795_write(client, W83795_REG_HT(index), tmp); |
| 1139 | break; |
| 1140 | case TEMP_PWM_HOT: |
| 1141 | val = SENSORS_LIMIT(val, 0, 0x0f); |
| 1142 | tmp = w83795_read(client, W83795_REG_HT(index)); |
| 1143 | tmp &= 0xf0; |
| 1144 | tmp |= val & 0x0f; |
| 1145 | w83795_write(client, W83795_REG_HT(index), tmp); |
| 1146 | break; |
| 1147 | } |
| 1148 | data->pwm_temp[index][nr] = val; |
| 1149 | mutex_unlock(&data->update_lock); |
| 1150 | |
| 1151 | return count; |
| 1152 | } |
| 1153 | |
| 1154 | static ssize_t |
| 1155 | show_sf4_pwm(struct device *dev, struct device_attribute *attr, char *buf) |
| 1156 | { |
Jean Delvare | 1bb3450 | 2010-10-28 20:31:48 +0200 | [diff] [blame^] | 1157 | struct w83795_data *data = w83795_update_pwm_config(dev); |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 1158 | struct sensor_device_attribute_2 *sensor_attr = |
| 1159 | to_sensor_dev_attr_2(attr); |
| 1160 | int nr = sensor_attr->nr; |
| 1161 | int index = sensor_attr->index; |
| 1162 | |
| 1163 | return sprintf(buf, "%u\n", data->sf4_reg[index][SF4_PWM][nr]); |
| 1164 | } |
| 1165 | |
| 1166 | static ssize_t |
| 1167 | store_sf4_pwm(struct device *dev, struct device_attribute *attr, |
| 1168 | const char *buf, size_t count) |
| 1169 | { |
| 1170 | struct i2c_client *client = to_i2c_client(dev); |
| 1171 | struct w83795_data *data = i2c_get_clientdata(client); |
| 1172 | struct sensor_device_attribute_2 *sensor_attr = |
| 1173 | to_sensor_dev_attr_2(attr); |
| 1174 | int nr = sensor_attr->nr; |
| 1175 | int index = sensor_attr->index; |
| 1176 | unsigned long val; |
| 1177 | |
| 1178 | if (strict_strtoul(buf, 10, &val) < 0) |
| 1179 | return -EINVAL; |
| 1180 | |
| 1181 | mutex_lock(&data->update_lock); |
| 1182 | w83795_write(client, W83795_REG_SF4_PWM(index, nr), val); |
| 1183 | data->sf4_reg[index][SF4_PWM][nr] = val; |
| 1184 | mutex_unlock(&data->update_lock); |
| 1185 | |
| 1186 | return count; |
| 1187 | } |
| 1188 | |
| 1189 | static ssize_t |
| 1190 | show_sf4_temp(struct device *dev, struct device_attribute *attr, char *buf) |
| 1191 | { |
Jean Delvare | 1bb3450 | 2010-10-28 20:31:48 +0200 | [diff] [blame^] | 1192 | struct w83795_data *data = w83795_update_pwm_config(dev); |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 1193 | struct sensor_device_attribute_2 *sensor_attr = |
| 1194 | to_sensor_dev_attr_2(attr); |
| 1195 | int nr = sensor_attr->nr; |
| 1196 | int index = sensor_attr->index; |
| 1197 | |
| 1198 | return sprintf(buf, "%u\n", |
| 1199 | (data->sf4_reg[index][SF4_TEMP][nr]) * 1000); |
| 1200 | } |
| 1201 | |
| 1202 | static ssize_t |
| 1203 | store_sf4_temp(struct device *dev, struct device_attribute *attr, |
| 1204 | const char *buf, size_t count) |
| 1205 | { |
| 1206 | struct i2c_client *client = to_i2c_client(dev); |
| 1207 | struct w83795_data *data = i2c_get_clientdata(client); |
| 1208 | struct sensor_device_attribute_2 *sensor_attr = |
| 1209 | to_sensor_dev_attr_2(attr); |
| 1210 | int nr = sensor_attr->nr; |
| 1211 | int index = sensor_attr->index; |
| 1212 | unsigned long val; |
| 1213 | |
| 1214 | if (strict_strtoul(buf, 10, &val) < 0) |
| 1215 | return -EINVAL; |
| 1216 | val /= 1000; |
| 1217 | |
| 1218 | mutex_lock(&data->update_lock); |
| 1219 | w83795_write(client, W83795_REG_SF4_TEMP(index, nr), val); |
| 1220 | data->sf4_reg[index][SF4_TEMP][nr] = val; |
| 1221 | mutex_unlock(&data->update_lock); |
| 1222 | |
| 1223 | return count; |
| 1224 | } |
| 1225 | |
| 1226 | |
| 1227 | static ssize_t |
| 1228 | show_temp(struct device *dev, struct device_attribute *attr, char *buf) |
| 1229 | { |
| 1230 | struct sensor_device_attribute_2 *sensor_attr = |
| 1231 | to_sensor_dev_attr_2(attr); |
| 1232 | int nr = sensor_attr->nr; |
| 1233 | int index = sensor_attr->index; |
| 1234 | struct w83795_data *data = w83795_update_device(dev); |
Jean Delvare | dd127f5 | 2010-10-28 20:31:47 +0200 | [diff] [blame] | 1235 | long temp = temp_from_reg(data->temp[index][nr]); |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 1236 | |
| 1237 | if (TEMP_READ == nr) |
Jean Delvare | a654b9d4 | 2010-10-28 20:31:47 +0200 | [diff] [blame] | 1238 | temp += (data->temp_read_vrlsb[index] >> 6) * 250; |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 1239 | return sprintf(buf, "%ld\n", temp); |
| 1240 | } |
| 1241 | |
| 1242 | static ssize_t |
| 1243 | store_temp(struct device *dev, struct device_attribute *attr, |
| 1244 | const char *buf, size_t count) |
| 1245 | { |
| 1246 | struct sensor_device_attribute_2 *sensor_attr = |
| 1247 | to_sensor_dev_attr_2(attr); |
| 1248 | int nr = sensor_attr->nr; |
| 1249 | int index = sensor_attr->index; |
| 1250 | struct i2c_client *client = to_i2c_client(dev); |
| 1251 | struct w83795_data *data = i2c_get_clientdata(client); |
| 1252 | long tmp; |
| 1253 | |
| 1254 | if (strict_strtol(buf, 10, &tmp) < 0) |
| 1255 | return -EINVAL; |
| 1256 | |
| 1257 | mutex_lock(&data->update_lock); |
| 1258 | data->temp[index][nr] = temp_to_reg(tmp, -128, 127); |
| 1259 | w83795_write(client, W83795_REG_TEMP[index][nr], data->temp[index][nr]); |
| 1260 | mutex_unlock(&data->update_lock); |
| 1261 | return count; |
| 1262 | } |
| 1263 | |
| 1264 | |
| 1265 | static ssize_t |
| 1266 | show_dts_mode(struct device *dev, struct device_attribute *attr, char *buf) |
| 1267 | { |
| 1268 | struct i2c_client *client = to_i2c_client(dev); |
| 1269 | struct w83795_data *data = i2c_get_clientdata(client); |
Jean Delvare | 39deb69 | 2010-10-28 20:31:48 +0200 | [diff] [blame] | 1270 | int tmp; |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 1271 | |
Jean Delvare | 39deb69 | 2010-10-28 20:31:48 +0200 | [diff] [blame] | 1272 | if (data->enable_dts & 2) |
| 1273 | tmp = 5; |
| 1274 | else |
| 1275 | tmp = 6; |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 1276 | |
| 1277 | return sprintf(buf, "%d\n", tmp); |
| 1278 | } |
| 1279 | |
| 1280 | static ssize_t |
| 1281 | show_dts(struct device *dev, struct device_attribute *attr, char *buf) |
| 1282 | { |
| 1283 | struct sensor_device_attribute_2 *sensor_attr = |
| 1284 | to_sensor_dev_attr_2(attr); |
| 1285 | int index = sensor_attr->index; |
| 1286 | struct w83795_data *data = w83795_update_device(dev); |
Jean Delvare | dd127f5 | 2010-10-28 20:31:47 +0200 | [diff] [blame] | 1287 | long temp = temp_from_reg(data->dts[index]); |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 1288 | |
Jean Delvare | a654b9d4 | 2010-10-28 20:31:47 +0200 | [diff] [blame] | 1289 | temp += (data->dts_read_vrlsb[index] >> 6) * 250; |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 1290 | return sprintf(buf, "%ld\n", temp); |
| 1291 | } |
| 1292 | |
| 1293 | static ssize_t |
| 1294 | show_dts_ext(struct device *dev, struct device_attribute *attr, char *buf) |
| 1295 | { |
| 1296 | struct sensor_device_attribute_2 *sensor_attr = |
| 1297 | to_sensor_dev_attr_2(attr); |
| 1298 | int nr = sensor_attr->nr; |
| 1299 | struct i2c_client *client = to_i2c_client(dev); |
| 1300 | struct w83795_data *data = i2c_get_clientdata(client); |
Jean Delvare | dd127f5 | 2010-10-28 20:31:47 +0200 | [diff] [blame] | 1301 | long temp = temp_from_reg(data->dts_ext[nr]); |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 1302 | |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 1303 | return sprintf(buf, "%ld\n", temp); |
| 1304 | } |
| 1305 | |
| 1306 | static ssize_t |
| 1307 | store_dts_ext(struct device *dev, struct device_attribute *attr, |
| 1308 | const char *buf, size_t count) |
| 1309 | { |
| 1310 | struct sensor_device_attribute_2 *sensor_attr = |
| 1311 | to_sensor_dev_attr_2(attr); |
| 1312 | int nr = sensor_attr->nr; |
| 1313 | struct i2c_client *client = to_i2c_client(dev); |
| 1314 | struct w83795_data *data = i2c_get_clientdata(client); |
| 1315 | long tmp; |
| 1316 | |
| 1317 | if (strict_strtol(buf, 10, &tmp) < 0) |
| 1318 | return -EINVAL; |
| 1319 | |
| 1320 | mutex_lock(&data->update_lock); |
| 1321 | data->dts_ext[nr] = temp_to_reg(tmp, -128, 127); |
| 1322 | w83795_write(client, W83795_REG_DTS_EXT(nr), data->dts_ext[nr]); |
| 1323 | mutex_unlock(&data->update_lock); |
| 1324 | return count; |
| 1325 | } |
| 1326 | |
| 1327 | |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 1328 | static ssize_t |
| 1329 | show_temp_mode(struct device *dev, struct device_attribute *attr, char *buf) |
| 1330 | { |
| 1331 | struct i2c_client *client = to_i2c_client(dev); |
| 1332 | struct w83795_data *data = i2c_get_clientdata(client); |
| 1333 | struct sensor_device_attribute_2 *sensor_attr = |
| 1334 | to_sensor_dev_attr_2(attr); |
| 1335 | int index = sensor_attr->index; |
Jean Delvare | 39deb69 | 2010-10-28 20:31:48 +0200 | [diff] [blame] | 1336 | int tmp; |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 1337 | |
Jean Delvare | 39deb69 | 2010-10-28 20:31:48 +0200 | [diff] [blame] | 1338 | if (data->temp_mode & (1 << index)) |
| 1339 | tmp = 3; /* Thermal diode */ |
| 1340 | else |
| 1341 | tmp = 4; /* Thermistor */ |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 1342 | |
| 1343 | return sprintf(buf, "%d\n", tmp); |
| 1344 | } |
| 1345 | |
Jean Delvare | 39deb69 | 2010-10-28 20:31:48 +0200 | [diff] [blame] | 1346 | /* Only for temp1-4 (temp5-6 can only be thermistor) */ |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 1347 | static ssize_t |
| 1348 | store_temp_mode(struct device *dev, struct device_attribute *attr, |
| 1349 | const char *buf, size_t count) |
| 1350 | { |
| 1351 | struct i2c_client *client = to_i2c_client(dev); |
| 1352 | struct w83795_data *data = i2c_get_clientdata(client); |
| 1353 | struct sensor_device_attribute_2 *sensor_attr = |
| 1354 | to_sensor_dev_attr_2(attr); |
| 1355 | int index = sensor_attr->index; |
Jean Delvare | 39deb69 | 2010-10-28 20:31:48 +0200 | [diff] [blame] | 1356 | int reg_shift; |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 1357 | unsigned long val; |
| 1358 | u8 tmp; |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 1359 | |
| 1360 | if (strict_strtoul(buf, 10, &val) < 0) |
| 1361 | return -EINVAL; |
| 1362 | if ((val != 4) && (val != 3)) |
| 1363 | return -EINVAL; |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 1364 | |
| 1365 | mutex_lock(&data->update_lock); |
| 1366 | if (val == 3) { |
Jean Delvare | 39deb69 | 2010-10-28 20:31:48 +0200 | [diff] [blame] | 1367 | /* Thermal diode */ |
| 1368 | val = 0x01; |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 1369 | data->temp_mode |= 1 << index; |
| 1370 | } else if (val == 4) { |
Jean Delvare | 39deb69 | 2010-10-28 20:31:48 +0200 | [diff] [blame] | 1371 | /* Thermistor */ |
| 1372 | val = 0x03; |
| 1373 | data->temp_mode &= ~(1 << index); |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 1374 | } |
| 1375 | |
Jean Delvare | 39deb69 | 2010-10-28 20:31:48 +0200 | [diff] [blame] | 1376 | reg_shift = 2 * index; |
| 1377 | tmp = w83795_read(client, W83795_REG_TEMP_CTRL2); |
| 1378 | tmp &= ~(0x03 << reg_shift); |
| 1379 | tmp |= val << reg_shift; |
| 1380 | w83795_write(client, W83795_REG_TEMP_CTRL2, tmp); |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 1381 | |
| 1382 | mutex_unlock(&data->update_lock); |
| 1383 | return count; |
| 1384 | } |
| 1385 | |
| 1386 | |
| 1387 | /* show/store VIN */ |
| 1388 | static ssize_t |
| 1389 | show_in(struct device *dev, struct device_attribute *attr, char *buf) |
| 1390 | { |
| 1391 | struct sensor_device_attribute_2 *sensor_attr = |
| 1392 | to_sensor_dev_attr_2(attr); |
| 1393 | int nr = sensor_attr->nr; |
| 1394 | int index = sensor_attr->index; |
| 1395 | struct w83795_data *data = w83795_update_device(dev); |
| 1396 | u16 val = data->in[index][nr]; |
| 1397 | u8 lsb_idx; |
| 1398 | |
| 1399 | switch (nr) { |
| 1400 | case IN_READ: |
| 1401 | /* calculate this value again by sensors as sensors3.conf */ |
| 1402 | if ((index >= 17) && |
Jean Delvare | 6f9dfd8 | 2010-10-28 20:31:46 +0200 | [diff] [blame] | 1403 | !((data->has_gain >> (index - 17)) & 1)) |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 1404 | val *= 8; |
| 1405 | break; |
| 1406 | case IN_MAX: |
| 1407 | case IN_LOW: |
| 1408 | lsb_idx = IN_LSB_SHIFT_IDX[index][IN_LSB_IDX]; |
| 1409 | val <<= 2; |
| 1410 | val |= (data->in_lsb[lsb_idx][nr] >> |
| 1411 | IN_LSB_SHIFT_IDX[lsb_idx][IN_LSB_SHIFT]) & 0x03; |
| 1412 | if ((index >= 17) && |
Jean Delvare | 6f9dfd8 | 2010-10-28 20:31:46 +0200 | [diff] [blame] | 1413 | !((data->has_gain >> (index - 17)) & 1)) |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 1414 | val *= 8; |
| 1415 | break; |
| 1416 | } |
| 1417 | val = in_from_reg(index, val); |
| 1418 | |
| 1419 | return sprintf(buf, "%d\n", val); |
| 1420 | } |
| 1421 | |
| 1422 | static ssize_t |
| 1423 | store_in(struct device *dev, struct device_attribute *attr, |
| 1424 | const char *buf, size_t count) |
| 1425 | { |
| 1426 | struct sensor_device_attribute_2 *sensor_attr = |
| 1427 | to_sensor_dev_attr_2(attr); |
| 1428 | int nr = sensor_attr->nr; |
| 1429 | int index = sensor_attr->index; |
| 1430 | struct i2c_client *client = to_i2c_client(dev); |
| 1431 | struct w83795_data *data = i2c_get_clientdata(client); |
| 1432 | unsigned long val; |
| 1433 | u8 tmp; |
| 1434 | u8 lsb_idx; |
| 1435 | |
| 1436 | if (strict_strtoul(buf, 10, &val) < 0) |
| 1437 | return -EINVAL; |
| 1438 | val = in_to_reg(index, val); |
| 1439 | |
| 1440 | if ((index >= 17) && |
Jean Delvare | 6f9dfd8 | 2010-10-28 20:31:46 +0200 | [diff] [blame] | 1441 | !((data->has_gain >> (index - 17)) & 1)) |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 1442 | val /= 8; |
| 1443 | val = SENSORS_LIMIT(val, 0, 0x3FF); |
| 1444 | mutex_lock(&data->update_lock); |
| 1445 | |
| 1446 | lsb_idx = IN_LSB_SHIFT_IDX[index][IN_LSB_IDX]; |
| 1447 | tmp = w83795_read(client, IN_LSB_REG(lsb_idx, nr)); |
| 1448 | tmp &= ~(0x03 << IN_LSB_SHIFT_IDX[index][IN_LSB_SHIFT]); |
| 1449 | tmp |= (val & 0x03) << IN_LSB_SHIFT_IDX[index][IN_LSB_SHIFT]; |
| 1450 | w83795_write(client, IN_LSB_REG(lsb_idx, nr), tmp); |
| 1451 | data->in_lsb[lsb_idx][nr] = tmp; |
| 1452 | |
| 1453 | tmp = (val >> 2) & 0xff; |
| 1454 | w83795_write(client, W83795_REG_IN[index][nr], tmp); |
| 1455 | data->in[index][nr] = tmp; |
| 1456 | |
| 1457 | mutex_unlock(&data->update_lock); |
| 1458 | return count; |
| 1459 | } |
| 1460 | |
| 1461 | |
| 1462 | static ssize_t |
| 1463 | show_sf_setup(struct device *dev, struct device_attribute *attr, char *buf) |
| 1464 | { |
| 1465 | struct sensor_device_attribute_2 *sensor_attr = |
| 1466 | to_sensor_dev_attr_2(attr); |
| 1467 | int nr = sensor_attr->nr; |
Jean Delvare | 1bb3450 | 2010-10-28 20:31:48 +0200 | [diff] [blame^] | 1468 | struct w83795_data *data = w83795_update_pwm_config(dev); |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 1469 | u16 val = data->setup_pwm[nr]; |
| 1470 | |
| 1471 | switch (nr) { |
| 1472 | case SETUP_PWM_UPTIME: |
| 1473 | case SETUP_PWM_DOWNTIME: |
| 1474 | val = time_from_reg(val); |
| 1475 | break; |
| 1476 | } |
| 1477 | |
| 1478 | return sprintf(buf, "%d\n", val); |
| 1479 | } |
| 1480 | |
| 1481 | static ssize_t |
| 1482 | store_sf_setup(struct device *dev, struct device_attribute *attr, |
| 1483 | const char *buf, size_t count) |
| 1484 | { |
| 1485 | struct sensor_device_attribute_2 *sensor_attr = |
| 1486 | to_sensor_dev_attr_2(attr); |
| 1487 | int nr = sensor_attr->nr; |
| 1488 | struct i2c_client *client = to_i2c_client(dev); |
| 1489 | struct w83795_data *data = i2c_get_clientdata(client); |
| 1490 | unsigned long val; |
| 1491 | |
| 1492 | if (strict_strtoul(buf, 10, &val) < 0) |
| 1493 | return -EINVAL; |
| 1494 | |
| 1495 | switch (nr) { |
| 1496 | case SETUP_PWM_DEFAULT: |
| 1497 | val = SENSORS_LIMIT(val, 0, 0xff); |
| 1498 | break; |
| 1499 | case SETUP_PWM_UPTIME: |
| 1500 | case SETUP_PWM_DOWNTIME: |
| 1501 | val = time_to_reg(val); |
| 1502 | if (val == 0) |
| 1503 | return -EINVAL; |
| 1504 | break; |
| 1505 | } |
| 1506 | |
| 1507 | mutex_lock(&data->update_lock); |
| 1508 | data->setup_pwm[nr] = val; |
| 1509 | w83795_write(client, W83795_REG_SETUP_PWM(nr), val); |
| 1510 | mutex_unlock(&data->update_lock); |
| 1511 | return count; |
| 1512 | } |
| 1513 | |
| 1514 | |
| 1515 | #define NOT_USED -1 |
| 1516 | |
Jean Delvare | 0e25601 | 2010-10-28 20:31:46 +0200 | [diff] [blame] | 1517 | /* Don't change the attribute order, _max and _min are accessed by index |
| 1518 | * somewhere else in the code */ |
Jean Delvare | 87df0da | 2010-10-28 20:31:45 +0200 | [diff] [blame] | 1519 | #define SENSOR_ATTR_IN(index) { \ |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 1520 | SENSOR_ATTR_2(in##index##_input, S_IRUGO, show_in, NULL, \ |
| 1521 | IN_READ, index), \ |
| 1522 | SENSOR_ATTR_2(in##index##_max, S_IRUGO | S_IWUSR, show_in, \ |
| 1523 | store_in, IN_MAX, index), \ |
| 1524 | SENSOR_ATTR_2(in##index##_min, S_IRUGO | S_IWUSR, show_in, \ |
| 1525 | store_in, IN_LOW, index), \ |
| 1526 | SENSOR_ATTR_2(in##index##_alarm, S_IRUGO, show_alarm_beep, \ |
| 1527 | NULL, ALARM_STATUS, index + ((index > 14) ? 1 : 0)), \ |
| 1528 | SENSOR_ATTR_2(in##index##_beep, S_IWUSR | S_IRUGO, \ |
| 1529 | show_alarm_beep, store_beep, BEEP_ENABLE, \ |
Jean Delvare | 87df0da | 2010-10-28 20:31:45 +0200 | [diff] [blame] | 1530 | index + ((index > 14) ? 1 : 0)) } |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 1531 | |
Jean Delvare | 87df0da | 2010-10-28 20:31:45 +0200 | [diff] [blame] | 1532 | #define SENSOR_ATTR_FAN(index) { \ |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 1533 | SENSOR_ATTR_2(fan##index##_input, S_IRUGO, show_fan, \ |
| 1534 | NULL, FAN_INPUT, index - 1), \ |
| 1535 | SENSOR_ATTR_2(fan##index##_min, S_IWUSR | S_IRUGO, \ |
| 1536 | show_fan, store_fan_min, FAN_MIN, index - 1), \ |
| 1537 | SENSOR_ATTR_2(fan##index##_alarm, S_IRUGO, show_alarm_beep, \ |
| 1538 | NULL, ALARM_STATUS, index + 31), \ |
| 1539 | SENSOR_ATTR_2(fan##index##_beep, S_IWUSR | S_IRUGO, \ |
Jean Delvare | 87df0da | 2010-10-28 20:31:45 +0200 | [diff] [blame] | 1540 | show_alarm_beep, store_beep, BEEP_ENABLE, index + 31) } |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 1541 | |
Jean Delvare | b5f6a90 | 2010-10-28 20:31:46 +0200 | [diff] [blame] | 1542 | #define SENSOR_ATTR_PWM(index) { \ |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 1543 | SENSOR_ATTR_2(pwm##index, S_IWUSR | S_IRUGO, show_pwm, \ |
| 1544 | store_pwm, PWM_OUTPUT, index - 1), \ |
| 1545 | SENSOR_ATTR_2(pwm##index##_nonstop, S_IWUSR | S_IRUGO, \ |
| 1546 | show_pwm, store_pwm, PWM_NONSTOP, index - 1), \ |
| 1547 | SENSOR_ATTR_2(pwm##index##_start, S_IWUSR | S_IRUGO, \ |
| 1548 | show_pwm, store_pwm, PWM_START, index - 1), \ |
| 1549 | SENSOR_ATTR_2(pwm##index##_stop_time, S_IWUSR | S_IRUGO, \ |
| 1550 | show_pwm, store_pwm, PWM_STOP_TIME, index - 1), \ |
Jean Delvare | 01879a8 | 2010-10-28 20:31:47 +0200 | [diff] [blame] | 1551 | SENSOR_ATTR_2(pwm##index##_freq, S_IWUSR | S_IRUGO, \ |
| 1552 | show_pwm, store_pwm, PWM_FREQ, index - 1), \ |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 1553 | SENSOR_ATTR_2(pwm##index##_enable, S_IWUSR | S_IRUGO, \ |
Jean Delvare | b2cc528 | 2010-10-28 20:31:46 +0200 | [diff] [blame] | 1554 | show_pwm_enable, store_pwm_enable, NOT_USED, index - 1), \ |
| 1555 | SENSOR_ATTR_2(fan##index##_target, S_IWUSR | S_IRUGO, \ |
| 1556 | show_fanin, store_fanin, FANIN_TARGET, index - 1) } |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 1557 | |
Jean Delvare | 87df0da | 2010-10-28 20:31:45 +0200 | [diff] [blame] | 1558 | #define SENSOR_ATTR_DTS(index) { \ |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 1559 | SENSOR_ATTR_2(temp##index##_type, S_IRUGO , \ |
| 1560 | show_dts_mode, NULL, NOT_USED, index - 7), \ |
| 1561 | SENSOR_ATTR_2(temp##index##_input, S_IRUGO, show_dts, \ |
| 1562 | NULL, NOT_USED, index - 7), \ |
Jean Delvare | a0ce402 | 2010-10-28 20:31:47 +0200 | [diff] [blame] | 1563 | SENSOR_ATTR_2(temp##index##_crit, S_IRUGO | S_IWUSR, show_dts_ext, \ |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 1564 | store_dts_ext, DTS_CRIT, NOT_USED), \ |
Jean Delvare | a0ce402 | 2010-10-28 20:31:47 +0200 | [diff] [blame] | 1565 | SENSOR_ATTR_2(temp##index##_crit_hyst, S_IRUGO | S_IWUSR, \ |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 1566 | show_dts_ext, store_dts_ext, DTS_CRIT_HYST, NOT_USED), \ |
Jean Delvare | a0ce402 | 2010-10-28 20:31:47 +0200 | [diff] [blame] | 1567 | SENSOR_ATTR_2(temp##index##_max, S_IRUGO | S_IWUSR, show_dts_ext, \ |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 1568 | store_dts_ext, DTS_WARN, NOT_USED), \ |
Jean Delvare | a0ce402 | 2010-10-28 20:31:47 +0200 | [diff] [blame] | 1569 | SENSOR_ATTR_2(temp##index##_max_hyst, S_IRUGO | S_IWUSR, \ |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 1570 | show_dts_ext, store_dts_ext, DTS_WARN_HYST, NOT_USED), \ |
| 1571 | SENSOR_ATTR_2(temp##index##_alarm, S_IRUGO, \ |
| 1572 | show_alarm_beep, NULL, ALARM_STATUS, index + 17), \ |
| 1573 | SENSOR_ATTR_2(temp##index##_beep, S_IWUSR | S_IRUGO, \ |
Jean Delvare | 87df0da | 2010-10-28 20:31:45 +0200 | [diff] [blame] | 1574 | show_alarm_beep, store_beep, BEEP_ENABLE, index + 17) } |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 1575 | |
Jean Delvare | 87df0da | 2010-10-28 20:31:45 +0200 | [diff] [blame] | 1576 | #define SENSOR_ATTR_TEMP(index) { \ |
Jean Delvare | 39deb69 | 2010-10-28 20:31:48 +0200 | [diff] [blame] | 1577 | SENSOR_ATTR_2(temp##index##_type, S_IRUGO | (index < 4 ? S_IWUSR : 0), \ |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 1578 | show_temp_mode, store_temp_mode, NOT_USED, index - 1), \ |
| 1579 | SENSOR_ATTR_2(temp##index##_input, S_IRUGO, show_temp, \ |
| 1580 | NULL, TEMP_READ, index - 1), \ |
Jean Delvare | a0ce402 | 2010-10-28 20:31:47 +0200 | [diff] [blame] | 1581 | SENSOR_ATTR_2(temp##index##_crit, S_IRUGO | S_IWUSR, show_temp, \ |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 1582 | store_temp, TEMP_CRIT, index - 1), \ |
Jean Delvare | a0ce402 | 2010-10-28 20:31:47 +0200 | [diff] [blame] | 1583 | SENSOR_ATTR_2(temp##index##_crit_hyst, S_IRUGO | S_IWUSR, \ |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 1584 | show_temp, store_temp, TEMP_CRIT_HYST, index - 1), \ |
Jean Delvare | a0ce402 | 2010-10-28 20:31:47 +0200 | [diff] [blame] | 1585 | SENSOR_ATTR_2(temp##index##_max, S_IRUGO | S_IWUSR, show_temp, \ |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 1586 | store_temp, TEMP_WARN, index - 1), \ |
Jean Delvare | a0ce402 | 2010-10-28 20:31:47 +0200 | [diff] [blame] | 1587 | SENSOR_ATTR_2(temp##index##_max_hyst, S_IRUGO | S_IWUSR, \ |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 1588 | show_temp, store_temp, TEMP_WARN_HYST, index - 1), \ |
| 1589 | SENSOR_ATTR_2(temp##index##_alarm, S_IRUGO, \ |
| 1590 | show_alarm_beep, NULL, ALARM_STATUS, \ |
| 1591 | index + (index > 4 ? 11 : 17)), \ |
| 1592 | SENSOR_ATTR_2(temp##index##_beep, S_IWUSR | S_IRUGO, \ |
| 1593 | show_alarm_beep, store_beep, BEEP_ENABLE, \ |
| 1594 | index + (index > 4 ? 11 : 17)), \ |
| 1595 | SENSOR_ATTR_2(temp##index##_source_sel, S_IWUSR | S_IRUGO, \ |
| 1596 | show_temp_src, store_temp_src, NOT_USED, index - 1), \ |
| 1597 | SENSOR_ATTR_2(temp##index##_pwm_enable, S_IWUSR | S_IRUGO, \ |
| 1598 | show_temp_pwm_enable, store_temp_pwm_enable, \ |
| 1599 | TEMP_PWM_ENABLE, index - 1), \ |
| 1600 | SENSOR_ATTR_2(temp##index##_auto_channels_pwm, S_IWUSR | S_IRUGO, \ |
| 1601 | show_temp_pwm_enable, store_temp_pwm_enable, \ |
| 1602 | TEMP_PWM_FAN_MAP, index - 1), \ |
| 1603 | SENSOR_ATTR_2(thermal_cruise##index, S_IWUSR | S_IRUGO, \ |
| 1604 | show_temp_pwm, store_temp_pwm, TEMP_PWM_TTTI, index - 1), \ |
Jean Delvare | a0ce402 | 2010-10-28 20:31:47 +0200 | [diff] [blame] | 1605 | SENSOR_ATTR_2(temp##index##_warn, S_IWUSR | S_IRUGO, \ |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 1606 | show_temp_pwm, store_temp_pwm, TEMP_PWM_CTFS, index - 1), \ |
Jean Delvare | a0ce402 | 2010-10-28 20:31:47 +0200 | [diff] [blame] | 1607 | SENSOR_ATTR_2(temp##index##_warn_hyst, S_IWUSR | S_IRUGO, \ |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 1608 | show_temp_pwm, store_temp_pwm, TEMP_PWM_HCT, index - 1), \ |
| 1609 | SENSOR_ATTR_2(temp##index##_operation_hyst, S_IWUSR | S_IRUGO, \ |
| 1610 | show_temp_pwm, store_temp_pwm, TEMP_PWM_HOT, index - 1), \ |
| 1611 | SENSOR_ATTR_2(temp##index##_auto_point1_pwm, S_IRUGO | S_IWUSR, \ |
| 1612 | show_sf4_pwm, store_sf4_pwm, 0, index - 1), \ |
| 1613 | SENSOR_ATTR_2(temp##index##_auto_point2_pwm, S_IRUGO | S_IWUSR, \ |
| 1614 | show_sf4_pwm, store_sf4_pwm, 1, index - 1), \ |
| 1615 | SENSOR_ATTR_2(temp##index##_auto_point3_pwm, S_IRUGO | S_IWUSR, \ |
| 1616 | show_sf4_pwm, store_sf4_pwm, 2, index - 1), \ |
| 1617 | SENSOR_ATTR_2(temp##index##_auto_point4_pwm, S_IRUGO | S_IWUSR, \ |
| 1618 | show_sf4_pwm, store_sf4_pwm, 3, index - 1), \ |
| 1619 | SENSOR_ATTR_2(temp##index##_auto_point5_pwm, S_IRUGO | S_IWUSR, \ |
| 1620 | show_sf4_pwm, store_sf4_pwm, 4, index - 1), \ |
| 1621 | SENSOR_ATTR_2(temp##index##_auto_point6_pwm, S_IRUGO | S_IWUSR, \ |
| 1622 | show_sf4_pwm, store_sf4_pwm, 5, index - 1), \ |
| 1623 | SENSOR_ATTR_2(temp##index##_auto_point7_pwm, S_IRUGO | S_IWUSR, \ |
| 1624 | show_sf4_pwm, store_sf4_pwm, 6, index - 1), \ |
| 1625 | SENSOR_ATTR_2(temp##index##_auto_point1_temp, S_IRUGO | S_IWUSR,\ |
| 1626 | show_sf4_temp, store_sf4_temp, 0, index - 1), \ |
| 1627 | SENSOR_ATTR_2(temp##index##_auto_point2_temp, S_IRUGO | S_IWUSR,\ |
| 1628 | show_sf4_temp, store_sf4_temp, 1, index - 1), \ |
| 1629 | SENSOR_ATTR_2(temp##index##_auto_point3_temp, S_IRUGO | S_IWUSR,\ |
| 1630 | show_sf4_temp, store_sf4_temp, 2, index - 1), \ |
| 1631 | SENSOR_ATTR_2(temp##index##_auto_point4_temp, S_IRUGO | S_IWUSR,\ |
| 1632 | show_sf4_temp, store_sf4_temp, 3, index - 1), \ |
| 1633 | SENSOR_ATTR_2(temp##index##_auto_point5_temp, S_IRUGO | S_IWUSR,\ |
| 1634 | show_sf4_temp, store_sf4_temp, 4, index - 1), \ |
| 1635 | SENSOR_ATTR_2(temp##index##_auto_point6_temp, S_IRUGO | S_IWUSR,\ |
| 1636 | show_sf4_temp, store_sf4_temp, 5, index - 1), \ |
| 1637 | SENSOR_ATTR_2(temp##index##_auto_point7_temp, S_IRUGO | S_IWUSR,\ |
Jean Delvare | 87df0da | 2010-10-28 20:31:45 +0200 | [diff] [blame] | 1638 | show_sf4_temp, store_sf4_temp, 6, index - 1) } |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 1639 | |
| 1640 | |
Jean Delvare | 87df0da | 2010-10-28 20:31:45 +0200 | [diff] [blame] | 1641 | static struct sensor_device_attribute_2 w83795_in[][5] = { |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 1642 | SENSOR_ATTR_IN(0), |
| 1643 | SENSOR_ATTR_IN(1), |
| 1644 | SENSOR_ATTR_IN(2), |
| 1645 | SENSOR_ATTR_IN(3), |
| 1646 | SENSOR_ATTR_IN(4), |
| 1647 | SENSOR_ATTR_IN(5), |
| 1648 | SENSOR_ATTR_IN(6), |
| 1649 | SENSOR_ATTR_IN(7), |
| 1650 | SENSOR_ATTR_IN(8), |
| 1651 | SENSOR_ATTR_IN(9), |
| 1652 | SENSOR_ATTR_IN(10), |
| 1653 | SENSOR_ATTR_IN(11), |
| 1654 | SENSOR_ATTR_IN(12), |
| 1655 | SENSOR_ATTR_IN(13), |
| 1656 | SENSOR_ATTR_IN(14), |
| 1657 | SENSOR_ATTR_IN(15), |
| 1658 | SENSOR_ATTR_IN(16), |
| 1659 | SENSOR_ATTR_IN(17), |
| 1660 | SENSOR_ATTR_IN(18), |
| 1661 | SENSOR_ATTR_IN(19), |
| 1662 | SENSOR_ATTR_IN(20), |
| 1663 | }; |
| 1664 | |
Jean Delvare | 86ef4d2 | 2010-10-28 20:31:46 +0200 | [diff] [blame] | 1665 | static const struct sensor_device_attribute_2 w83795_fan[][4] = { |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 1666 | SENSOR_ATTR_FAN(1), |
| 1667 | SENSOR_ATTR_FAN(2), |
| 1668 | SENSOR_ATTR_FAN(3), |
| 1669 | SENSOR_ATTR_FAN(4), |
| 1670 | SENSOR_ATTR_FAN(5), |
| 1671 | SENSOR_ATTR_FAN(6), |
| 1672 | SENSOR_ATTR_FAN(7), |
| 1673 | SENSOR_ATTR_FAN(8), |
| 1674 | SENSOR_ATTR_FAN(9), |
| 1675 | SENSOR_ATTR_FAN(10), |
| 1676 | SENSOR_ATTR_FAN(11), |
| 1677 | SENSOR_ATTR_FAN(12), |
| 1678 | SENSOR_ATTR_FAN(13), |
| 1679 | SENSOR_ATTR_FAN(14), |
| 1680 | }; |
| 1681 | |
Jean Delvare | 86ef4d2 | 2010-10-28 20:31:46 +0200 | [diff] [blame] | 1682 | static const struct sensor_device_attribute_2 w83795_temp[][29] = { |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 1683 | SENSOR_ATTR_TEMP(1), |
| 1684 | SENSOR_ATTR_TEMP(2), |
| 1685 | SENSOR_ATTR_TEMP(3), |
| 1686 | SENSOR_ATTR_TEMP(4), |
| 1687 | SENSOR_ATTR_TEMP(5), |
| 1688 | SENSOR_ATTR_TEMP(6), |
| 1689 | }; |
| 1690 | |
Jean Delvare | 86ef4d2 | 2010-10-28 20:31:46 +0200 | [diff] [blame] | 1691 | static const struct sensor_device_attribute_2 w83795_dts[][8] = { |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 1692 | SENSOR_ATTR_DTS(7), |
| 1693 | SENSOR_ATTR_DTS(8), |
| 1694 | SENSOR_ATTR_DTS(9), |
| 1695 | SENSOR_ATTR_DTS(10), |
| 1696 | SENSOR_ATTR_DTS(11), |
| 1697 | SENSOR_ATTR_DTS(12), |
| 1698 | SENSOR_ATTR_DTS(13), |
| 1699 | SENSOR_ATTR_DTS(14), |
| 1700 | }; |
| 1701 | |
Jean Delvare | 86ef4d2 | 2010-10-28 20:31:46 +0200 | [diff] [blame] | 1702 | static const struct sensor_device_attribute_2 w83795_pwm[][7] = { |
Jean Delvare | b5f6a90 | 2010-10-28 20:31:46 +0200 | [diff] [blame] | 1703 | SENSOR_ATTR_PWM(1), |
| 1704 | SENSOR_ATTR_PWM(2), |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 1705 | SENSOR_ATTR_PWM(3), |
| 1706 | SENSOR_ATTR_PWM(4), |
| 1707 | SENSOR_ATTR_PWM(5), |
| 1708 | SENSOR_ATTR_PWM(6), |
| 1709 | SENSOR_ATTR_PWM(7), |
| 1710 | SENSOR_ATTR_PWM(8), |
| 1711 | }; |
| 1712 | |
Jean Delvare | 86ef4d2 | 2010-10-28 20:31:46 +0200 | [diff] [blame] | 1713 | static const struct sensor_device_attribute_2 sda_single_files[] = { |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 1714 | SENSOR_ATTR_2(chassis, S_IWUSR | S_IRUGO, show_alarm_beep, |
| 1715 | store_chassis_clear, ALARM_STATUS, 46), |
Jean Delvare | 02728ff | 2010-10-28 20:31:47 +0200 | [diff] [blame] | 1716 | SENSOR_ATTR_2(beep_enable, S_IWUSR | S_IRUGO, show_alarm_beep, |
| 1717 | store_beep, BEEP_ENABLE, 47), |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 1718 | SENSOR_ATTR_2(speed_cruise_tolerance, S_IWUSR | S_IRUGO, show_fanin, |
| 1719 | store_fanin, FANIN_TOL, NOT_USED), |
| 1720 | SENSOR_ATTR_2(pwm_default, S_IWUSR | S_IRUGO, show_sf_setup, |
| 1721 | store_sf_setup, SETUP_PWM_DEFAULT, NOT_USED), |
| 1722 | SENSOR_ATTR_2(pwm_uptime, S_IWUSR | S_IRUGO, show_sf_setup, |
| 1723 | store_sf_setup, SETUP_PWM_UPTIME, NOT_USED), |
| 1724 | SENSOR_ATTR_2(pwm_downtime, S_IWUSR | S_IRUGO, show_sf_setup, |
| 1725 | store_sf_setup, SETUP_PWM_DOWNTIME, NOT_USED), |
| 1726 | }; |
| 1727 | |
| 1728 | /* |
| 1729 | * Driver interface |
| 1730 | */ |
| 1731 | |
| 1732 | static void w83795_init_client(struct i2c_client *client) |
| 1733 | { |
Jean Delvare | 01879a8 | 2010-10-28 20:31:47 +0200 | [diff] [blame] | 1734 | struct w83795_data *data = i2c_get_clientdata(client); |
| 1735 | static const u16 clkin[4] = { /* in kHz */ |
| 1736 | 14318, 24000, 33333, 48000 |
| 1737 | }; |
Jean Delvare | 80646b9 | 2010-10-28 20:31:46 +0200 | [diff] [blame] | 1738 | u8 config; |
| 1739 | |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 1740 | if (reset) |
| 1741 | w83795_write(client, W83795_REG_CONFIG, 0x80); |
| 1742 | |
Jean Delvare | 80646b9 | 2010-10-28 20:31:46 +0200 | [diff] [blame] | 1743 | /* Start monitoring if needed */ |
| 1744 | config = w83795_read(client, W83795_REG_CONFIG); |
| 1745 | if (!(config & W83795_REG_CONFIG_START)) { |
| 1746 | dev_info(&client->dev, "Enabling monitoring operations\n"); |
| 1747 | w83795_write(client, W83795_REG_CONFIG, |
| 1748 | config | W83795_REG_CONFIG_START); |
| 1749 | } |
Jean Delvare | 01879a8 | 2010-10-28 20:31:47 +0200 | [diff] [blame] | 1750 | |
| 1751 | data->clkin = clkin[(config >> 3) & 0x3]; |
| 1752 | dev_dbg(&client->dev, "clkin = %u kHz\n", data->clkin); |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 1753 | } |
| 1754 | |
Jean Delvare | 2be381d | 2010-10-28 20:31:45 +0200 | [diff] [blame] | 1755 | static int w83795_get_device_id(struct i2c_client *client) |
| 1756 | { |
| 1757 | int device_id; |
| 1758 | |
| 1759 | device_id = i2c_smbus_read_byte_data(client, W83795_REG_DEVICEID); |
| 1760 | |
| 1761 | /* Special case for rev. A chips; can't be checked first because later |
| 1762 | revisions emulate this for compatibility */ |
| 1763 | if (device_id < 0 || (device_id & 0xf0) != 0x50) { |
| 1764 | int alt_id; |
| 1765 | |
| 1766 | alt_id = i2c_smbus_read_byte_data(client, |
| 1767 | W83795_REG_DEVICEID_A); |
| 1768 | if (alt_id == 0x50) |
| 1769 | device_id = alt_id; |
| 1770 | } |
| 1771 | |
| 1772 | return device_id; |
| 1773 | } |
| 1774 | |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 1775 | /* Return 0 if detection is successful, -ENODEV otherwise */ |
| 1776 | static int w83795_detect(struct i2c_client *client, |
| 1777 | struct i2c_board_info *info) |
| 1778 | { |
Jean Delvare | 2be381d | 2010-10-28 20:31:45 +0200 | [diff] [blame] | 1779 | int bank, vendor_id, device_id, expected, i2c_addr, config; |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 1780 | struct i2c_adapter *adapter = client->adapter; |
| 1781 | unsigned short address = client->addr; |
Jean Delvare | 093d1a4 | 2010-10-28 20:31:45 +0200 | [diff] [blame] | 1782 | const char *chip_name; |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 1783 | |
| 1784 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) |
| 1785 | return -ENODEV; |
| 1786 | bank = i2c_smbus_read_byte_data(client, W83795_REG_BANKSEL); |
Jean Delvare | 2be381d | 2010-10-28 20:31:45 +0200 | [diff] [blame] | 1787 | if (bank < 0 || (bank & 0x7c)) { |
| 1788 | dev_dbg(&adapter->dev, |
| 1789 | "w83795: Detection failed at addr 0x%02hx, check %s\n", |
| 1790 | address, "bank"); |
| 1791 | return -ENODEV; |
| 1792 | } |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 1793 | |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 1794 | /* Check Nuvoton vendor ID */ |
Jean Delvare | 2be381d | 2010-10-28 20:31:45 +0200 | [diff] [blame] | 1795 | vendor_id = i2c_smbus_read_byte_data(client, W83795_REG_VENDORID); |
| 1796 | expected = bank & 0x80 ? 0x5c : 0xa3; |
| 1797 | if (vendor_id != expected) { |
| 1798 | dev_dbg(&adapter->dev, |
| 1799 | "w83795: Detection failed at addr 0x%02hx, check %s\n", |
| 1800 | address, "vendor id"); |
| 1801 | return -ENODEV; |
| 1802 | } |
| 1803 | |
| 1804 | /* Check device ID */ |
| 1805 | device_id = w83795_get_device_id(client) | |
| 1806 | (i2c_smbus_read_byte_data(client, W83795_REG_CHIPID) << 8); |
| 1807 | if ((device_id >> 4) != 0x795) { |
| 1808 | dev_dbg(&adapter->dev, |
| 1809 | "w83795: Detection failed at addr 0x%02hx, check %s\n", |
| 1810 | address, "device id\n"); |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 1811 | return -ENODEV; |
| 1812 | } |
| 1813 | |
| 1814 | /* If Nuvoton chip, address of chip and W83795_REG_I2C_ADDR |
| 1815 | should match */ |
Jean Delvare | 2be381d | 2010-10-28 20:31:45 +0200 | [diff] [blame] | 1816 | if ((bank & 0x07) == 0) { |
| 1817 | i2c_addr = i2c_smbus_read_byte_data(client, |
| 1818 | W83795_REG_I2C_ADDR); |
| 1819 | if ((i2c_addr & 0x7f) != address) { |
| 1820 | dev_dbg(&adapter->dev, |
| 1821 | "w83795: Detection failed at addr 0x%02hx, " |
| 1822 | "check %s\n", address, "i2c addr"); |
| 1823 | return -ENODEV; |
| 1824 | } |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 1825 | } |
| 1826 | |
Jean Delvare | 093d1a4 | 2010-10-28 20:31:45 +0200 | [diff] [blame] | 1827 | /* Check 795 chip type: 795G or 795ADG |
| 1828 | Usually we don't write to chips during detection, but here we don't |
| 1829 | quite have the choice; hopefully it's OK, we are about to return |
| 1830 | success anyway */ |
| 1831 | if ((bank & 0x07) != 0) |
| 1832 | i2c_smbus_write_byte_data(client, W83795_REG_BANKSEL, |
| 1833 | bank & ~0x07); |
Jean Delvare | 2be381d | 2010-10-28 20:31:45 +0200 | [diff] [blame] | 1834 | config = i2c_smbus_read_byte_data(client, W83795_REG_CONFIG); |
| 1835 | if (config & W83795_REG_CONFIG_CONFIG48) |
Jean Delvare | 093d1a4 | 2010-10-28 20:31:45 +0200 | [diff] [blame] | 1836 | chip_name = "w83795adg"; |
Jean Delvare | 2be381d | 2010-10-28 20:31:45 +0200 | [diff] [blame] | 1837 | else |
Jean Delvare | 093d1a4 | 2010-10-28 20:31:45 +0200 | [diff] [blame] | 1838 | chip_name = "w83795g"; |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 1839 | |
Jean Delvare | 093d1a4 | 2010-10-28 20:31:45 +0200 | [diff] [blame] | 1840 | strlcpy(info->type, chip_name, I2C_NAME_SIZE); |
Jean Delvare | 2be381d | 2010-10-28 20:31:45 +0200 | [diff] [blame] | 1841 | dev_info(&adapter->dev, "Found %s rev. %c at 0x%02hx\n", chip_name, |
| 1842 | 'A' + (device_id & 0xf), address); |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 1843 | |
| 1844 | return 0; |
| 1845 | } |
| 1846 | |
Jean Delvare | 6f3dcde | 2010-10-28 20:31:45 +0200 | [diff] [blame] | 1847 | static int w83795_handle_files(struct device *dev, int (*fn)(struct device *, |
| 1848 | const struct device_attribute *)) |
Jean Delvare | 892514a | 2010-10-28 20:31:45 +0200 | [diff] [blame] | 1849 | { |
| 1850 | struct w83795_data *data = dev_get_drvdata(dev); |
Jean Delvare | 87df0da | 2010-10-28 20:31:45 +0200 | [diff] [blame] | 1851 | int err, i, j; |
Jean Delvare | 892514a | 2010-10-28 20:31:45 +0200 | [diff] [blame] | 1852 | |
| 1853 | for (i = 0; i < ARRAY_SIZE(w83795_in); i++) { |
Jean Delvare | 87df0da | 2010-10-28 20:31:45 +0200 | [diff] [blame] | 1854 | if (!(data->has_in & (1 << i))) |
Jean Delvare | 892514a | 2010-10-28 20:31:45 +0200 | [diff] [blame] | 1855 | continue; |
Jean Delvare | 87df0da | 2010-10-28 20:31:45 +0200 | [diff] [blame] | 1856 | for (j = 0; j < ARRAY_SIZE(w83795_in[0]); j++) { |
| 1857 | err = fn(dev, &w83795_in[i][j].dev_attr); |
| 1858 | if (err) |
| 1859 | return err; |
| 1860 | } |
Jean Delvare | 892514a | 2010-10-28 20:31:45 +0200 | [diff] [blame] | 1861 | } |
| 1862 | |
| 1863 | for (i = 0; i < ARRAY_SIZE(w83795_fan); i++) { |
Jean Delvare | 87df0da | 2010-10-28 20:31:45 +0200 | [diff] [blame] | 1864 | if (!(data->has_fan & (1 << i))) |
Jean Delvare | 892514a | 2010-10-28 20:31:45 +0200 | [diff] [blame] | 1865 | continue; |
Jean Delvare | 87df0da | 2010-10-28 20:31:45 +0200 | [diff] [blame] | 1866 | for (j = 0; j < ARRAY_SIZE(w83795_fan[0]); j++) { |
| 1867 | err = fn(dev, &w83795_fan[i][j].dev_attr); |
| 1868 | if (err) |
| 1869 | return err; |
| 1870 | } |
Jean Delvare | 892514a | 2010-10-28 20:31:45 +0200 | [diff] [blame] | 1871 | } |
| 1872 | |
| 1873 | for (i = 0; i < ARRAY_SIZE(sda_single_files); i++) { |
Jean Delvare | 6f3dcde | 2010-10-28 20:31:45 +0200 | [diff] [blame] | 1874 | err = fn(dev, &sda_single_files[i].dev_attr); |
Jean Delvare | 892514a | 2010-10-28 20:31:45 +0200 | [diff] [blame] | 1875 | if (err) |
| 1876 | return err; |
| 1877 | } |
| 1878 | |
Jean Delvare | b5f6a90 | 2010-10-28 20:31:46 +0200 | [diff] [blame] | 1879 | for (i = 0; i < data->has_pwm; i++) { |
| 1880 | for (j = 0; j < ARRAY_SIZE(w83795_pwm[0]); j++) { |
| 1881 | err = fn(dev, &w83795_pwm[i][j].dev_attr); |
Jean Delvare | 892514a | 2010-10-28 20:31:45 +0200 | [diff] [blame] | 1882 | if (err) |
| 1883 | return err; |
| 1884 | } |
| 1885 | } |
| 1886 | |
| 1887 | for (i = 0; i < ARRAY_SIZE(w83795_temp); i++) { |
Jean Delvare | 87df0da | 2010-10-28 20:31:45 +0200 | [diff] [blame] | 1888 | if (!(data->has_temp & (1 << i))) |
Jean Delvare | 892514a | 2010-10-28 20:31:45 +0200 | [diff] [blame] | 1889 | continue; |
Jean Delvare | 87df0da | 2010-10-28 20:31:45 +0200 | [diff] [blame] | 1890 | for (j = 0; j < ARRAY_SIZE(w83795_temp[0]); j++) { |
| 1891 | err = fn(dev, &w83795_temp[i][j].dev_attr); |
| 1892 | if (err) |
| 1893 | return err; |
| 1894 | } |
Jean Delvare | 892514a | 2010-10-28 20:31:45 +0200 | [diff] [blame] | 1895 | } |
| 1896 | |
| 1897 | if (data->enable_dts != 0) { |
| 1898 | for (i = 0; i < ARRAY_SIZE(w83795_dts); i++) { |
Jean Delvare | 87df0da | 2010-10-28 20:31:45 +0200 | [diff] [blame] | 1899 | if (!(data->has_dts & (1 << i))) |
Jean Delvare | 892514a | 2010-10-28 20:31:45 +0200 | [diff] [blame] | 1900 | continue; |
Jean Delvare | 87df0da | 2010-10-28 20:31:45 +0200 | [diff] [blame] | 1901 | for (j = 0; j < ARRAY_SIZE(w83795_dts[0]); j++) { |
| 1902 | err = fn(dev, &w83795_dts[i][j].dev_attr); |
| 1903 | if (err) |
| 1904 | return err; |
| 1905 | } |
Jean Delvare | 892514a | 2010-10-28 20:31:45 +0200 | [diff] [blame] | 1906 | } |
| 1907 | } |
| 1908 | |
Jean Delvare | 892514a | 2010-10-28 20:31:45 +0200 | [diff] [blame] | 1909 | return 0; |
| 1910 | } |
| 1911 | |
Jean Delvare | 6f3dcde | 2010-10-28 20:31:45 +0200 | [diff] [blame] | 1912 | /* We need a wrapper that fits in w83795_handle_files */ |
| 1913 | static int device_remove_file_wrapper(struct device *dev, |
| 1914 | const struct device_attribute *attr) |
Jean Delvare | 2fa0987 | 2010-10-28 20:31:45 +0200 | [diff] [blame] | 1915 | { |
Jean Delvare | 6f3dcde | 2010-10-28 20:31:45 +0200 | [diff] [blame] | 1916 | device_remove_file(dev, attr); |
| 1917 | return 0; |
Jean Delvare | 2fa0987 | 2010-10-28 20:31:45 +0200 | [diff] [blame] | 1918 | } |
| 1919 | |
Jean Delvare | 0e25601 | 2010-10-28 20:31:46 +0200 | [diff] [blame] | 1920 | static void w83795_check_dynamic_in_limits(struct i2c_client *client) |
| 1921 | { |
| 1922 | struct w83795_data *data = i2c_get_clientdata(client); |
| 1923 | u8 vid_ctl; |
| 1924 | int i, err_max, err_min; |
| 1925 | |
| 1926 | vid_ctl = w83795_read(client, W83795_REG_VID_CTRL); |
| 1927 | |
| 1928 | /* Return immediately if VRM isn't configured */ |
| 1929 | if ((vid_ctl & 0x07) == 0x00 || (vid_ctl & 0x07) == 0x07) |
| 1930 | return; |
| 1931 | |
| 1932 | data->has_dyn_in = (vid_ctl >> 3) & 0x07; |
| 1933 | for (i = 0; i < 2; i++) { |
| 1934 | if (!(data->has_dyn_in & (1 << i))) |
| 1935 | continue; |
| 1936 | |
| 1937 | /* Voltage limits in dynamic mode, switch to read-only */ |
| 1938 | err_max = sysfs_chmod_file(&client->dev.kobj, |
| 1939 | &w83795_in[i][2].dev_attr.attr, |
| 1940 | S_IRUGO); |
| 1941 | err_min = sysfs_chmod_file(&client->dev.kobj, |
| 1942 | &w83795_in[i][3].dev_attr.attr, |
| 1943 | S_IRUGO); |
| 1944 | if (err_max || err_min) |
| 1945 | dev_warn(&client->dev, "Failed to set in%d limits " |
| 1946 | "read-only (%d, %d)\n", i, err_max, err_min); |
| 1947 | else |
| 1948 | dev_info(&client->dev, "in%d limits set dynamically " |
| 1949 | "from VID\n", i); |
| 1950 | } |
| 1951 | } |
| 1952 | |
Jean Delvare | 71caf46 | 2010-10-28 20:31:46 +0200 | [diff] [blame] | 1953 | /* Check pins that can be used for either temperature or voltage monitoring */ |
| 1954 | static void w83795_apply_temp_config(struct w83795_data *data, u8 config, |
| 1955 | int temp_chan, int in_chan) |
| 1956 | { |
| 1957 | /* config is a 2-bit value */ |
| 1958 | switch (config) { |
| 1959 | case 0x2: /* Voltage monitoring */ |
| 1960 | data->has_in |= 1 << in_chan; |
| 1961 | break; |
| 1962 | case 0x1: /* Thermal diode */ |
| 1963 | if (temp_chan >= 4) |
| 1964 | break; |
| 1965 | data->temp_mode |= 1 << temp_chan; |
| 1966 | /* fall through */ |
| 1967 | case 0x3: /* Thermistor */ |
| 1968 | data->has_temp |= 1 << temp_chan; |
| 1969 | break; |
| 1970 | } |
| 1971 | } |
| 1972 | |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 1973 | static int w83795_probe(struct i2c_client *client, |
| 1974 | const struct i2c_device_id *id) |
| 1975 | { |
| 1976 | int i; |
| 1977 | u8 tmp; |
| 1978 | struct device *dev = &client->dev; |
| 1979 | struct w83795_data *data; |
Jean Delvare | 71caf46 | 2010-10-28 20:31:46 +0200 | [diff] [blame] | 1980 | int err; |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 1981 | |
| 1982 | data = kzalloc(sizeof(struct w83795_data), GFP_KERNEL); |
| 1983 | if (!data) { |
| 1984 | err = -ENOMEM; |
| 1985 | goto exit; |
| 1986 | } |
| 1987 | |
| 1988 | i2c_set_clientdata(client, data); |
Jean Delvare | 093d1a4 | 2010-10-28 20:31:45 +0200 | [diff] [blame] | 1989 | data->chip_type = id->driver_data; |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 1990 | data->bank = i2c_smbus_read_byte_data(client, W83795_REG_BANKSEL); |
| 1991 | mutex_init(&data->update_lock); |
| 1992 | |
| 1993 | /* Initialize the chip */ |
| 1994 | w83795_init_client(client); |
| 1995 | |
Jean Delvare | 71caf46 | 2010-10-28 20:31:46 +0200 | [diff] [blame] | 1996 | /* Check which voltages and fans are present */ |
| 1997 | data->has_in = w83795_read(client, W83795_REG_VOLT_CTRL1) |
| 1998 | | (w83795_read(client, W83795_REG_VOLT_CTRL2) << 8); |
| 1999 | data->has_fan = w83795_read(client, W83795_REG_FANIN_CTRL1) |
| 2000 | | (w83795_read(client, W83795_REG_FANIN_CTRL2) << 8); |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 2001 | |
Jean Delvare | 71caf46 | 2010-10-28 20:31:46 +0200 | [diff] [blame] | 2002 | /* Check which analog temperatures and extra voltages are present */ |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 2003 | tmp = w83795_read(client, W83795_REG_TEMP_CTRL1); |
| 2004 | if (tmp & 0x20) |
| 2005 | data->enable_dts = 1; |
Jean Delvare | 71caf46 | 2010-10-28 20:31:46 +0200 | [diff] [blame] | 2006 | w83795_apply_temp_config(data, (tmp >> 2) & 0x3, 5, 16); |
| 2007 | w83795_apply_temp_config(data, tmp & 0x3, 4, 15); |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 2008 | tmp = w83795_read(client, W83795_REG_TEMP_CTRL2); |
Jean Delvare | 71caf46 | 2010-10-28 20:31:46 +0200 | [diff] [blame] | 2009 | w83795_apply_temp_config(data, tmp >> 6, 3, 20); |
| 2010 | w83795_apply_temp_config(data, (tmp >> 4) & 0x3, 2, 19); |
| 2011 | w83795_apply_temp_config(data, (tmp >> 2) & 0x3, 1, 18); |
| 2012 | w83795_apply_temp_config(data, tmp & 0x3, 0, 17); |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 2013 | |
| 2014 | /* Check DTS enable status */ |
Jean Delvare | 71caf46 | 2010-10-28 20:31:46 +0200 | [diff] [blame] | 2015 | if (data->enable_dts) { |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 2016 | if (1 & w83795_read(client, W83795_REG_DTSC)) |
| 2017 | data->enable_dts |= 2; |
| 2018 | data->has_dts = w83795_read(client, W83795_REG_DTSE); |
| 2019 | } |
| 2020 | |
Jean Delvare | 54891a3 | 2010-10-28 20:31:47 +0200 | [diff] [blame] | 2021 | /* Report PECI Tbase values */ |
| 2022 | if (data->enable_dts == 1) { |
| 2023 | for (i = 0; i < 8; i++) { |
| 2024 | if (!(data->has_dts & (1 << i))) |
| 2025 | continue; |
| 2026 | tmp = w83795_read(client, W83795_REG_PECI_TBASE(i)); |
| 2027 | dev_info(&client->dev, |
| 2028 | "PECI agent %d Tbase temperature: %u\n", |
| 2029 | i + 1, (unsigned int)tmp & 0x7f); |
| 2030 | } |
| 2031 | } |
| 2032 | |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 2033 | data->has_gain = w83795_read(client, W83795_REG_VMIGB_CTRL) & 0x0f; |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 2034 | |
| 2035 | /* pwm and smart fan */ |
| 2036 | if (data->chip_type == w83795g) |
| 2037 | data->has_pwm = 8; |
| 2038 | else |
| 2039 | data->has_pwm = 2; |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 2040 | |
Jean Delvare | 6f3dcde | 2010-10-28 20:31:45 +0200 | [diff] [blame] | 2041 | err = w83795_handle_files(dev, device_create_file); |
Jean Delvare | 892514a | 2010-10-28 20:31:45 +0200 | [diff] [blame] | 2042 | if (err) |
| 2043 | goto exit_remove; |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 2044 | |
Jean Delvare | 0e25601 | 2010-10-28 20:31:46 +0200 | [diff] [blame] | 2045 | if (data->chip_type == w83795g) |
| 2046 | w83795_check_dynamic_in_limits(client); |
| 2047 | |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 2048 | data->hwmon_dev = hwmon_device_register(dev); |
| 2049 | if (IS_ERR(data->hwmon_dev)) { |
| 2050 | err = PTR_ERR(data->hwmon_dev); |
| 2051 | goto exit_remove; |
| 2052 | } |
| 2053 | |
| 2054 | return 0; |
| 2055 | |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 2056 | exit_remove: |
Jean Delvare | 6f3dcde | 2010-10-28 20:31:45 +0200 | [diff] [blame] | 2057 | w83795_handle_files(dev, device_remove_file_wrapper); |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 2058 | kfree(data); |
| 2059 | exit: |
| 2060 | return err; |
| 2061 | } |
| 2062 | |
| 2063 | static int w83795_remove(struct i2c_client *client) |
| 2064 | { |
| 2065 | struct w83795_data *data = i2c_get_clientdata(client); |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 2066 | |
| 2067 | hwmon_device_unregister(data->hwmon_dev); |
Jean Delvare | 6f3dcde | 2010-10-28 20:31:45 +0200 | [diff] [blame] | 2068 | w83795_handle_files(&client->dev, device_remove_file_wrapper); |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 2069 | kfree(data); |
| 2070 | |
| 2071 | return 0; |
| 2072 | } |
| 2073 | |
| 2074 | |
| 2075 | static const struct i2c_device_id w83795_id[] = { |
Jean Delvare | 093d1a4 | 2010-10-28 20:31:45 +0200 | [diff] [blame] | 2076 | { "w83795g", w83795g }, |
| 2077 | { "w83795adg", w83795adg }, |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 2078 | { } |
| 2079 | }; |
| 2080 | MODULE_DEVICE_TABLE(i2c, w83795_id); |
| 2081 | |
| 2082 | static struct i2c_driver w83795_driver = { |
| 2083 | .driver = { |
| 2084 | .name = "w83795", |
| 2085 | }, |
| 2086 | .probe = w83795_probe, |
| 2087 | .remove = w83795_remove, |
| 2088 | .id_table = w83795_id, |
| 2089 | |
| 2090 | .class = I2C_CLASS_HWMON, |
| 2091 | .detect = w83795_detect, |
| 2092 | .address_list = normal_i2c, |
| 2093 | }; |
| 2094 | |
| 2095 | static int __init sensors_w83795_init(void) |
| 2096 | { |
| 2097 | return i2c_add_driver(&w83795_driver); |
| 2098 | } |
| 2099 | |
| 2100 | static void __exit sensors_w83795_exit(void) |
| 2101 | { |
| 2102 | i2c_del_driver(&w83795_driver); |
| 2103 | } |
| 2104 | |
| 2105 | MODULE_AUTHOR("Wei Song"); |
Jean Delvare | 315bacf | 2010-10-28 20:31:45 +0200 | [diff] [blame] | 2106 | MODULE_DESCRIPTION("W83795G/ADG hardware monitoring driver"); |
Wei Song | 792d376 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 2107 | MODULE_LICENSE("GPL"); |
| 2108 | |
| 2109 | module_init(sensors_w83795_init); |
| 2110 | module_exit(sensors_w83795_exit); |