2 * w83795.c - Linux kernel driver for hardware monitoring
3 * Copyright (C) 2008 Nuvoton Technology Corp.
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.
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.
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
20 * Supports following chips:
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
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>
38 /* Addresses to scan */
39 static const unsigned short normal_i2c[] = {
40 0x2c, 0x2d, 0x2e, 0x2f, I2C_CLIENT_END
45 module_param(reset, bool, 0);
46 MODULE_PARM_DESC(reset, "Set to 1 to reset chip, not recommended");
49 #define W83795_REG_BANKSEL 0x00
50 #define W83795_REG_VENDORID 0xfd
51 #define W83795_REG_CHIPID 0xfe
52 #define W83795_REG_DEVICEID 0xfb
53 #define W83795_REG_DEVICEID_A 0xff
55 #define W83795_REG_I2C_ADDR 0xfc
56 #define W83795_REG_CONFIG 0x01
57 #define W83795_REG_CONFIG_CONFIG48 0x04
58 #define W83795_REG_CONFIG_START 0x01
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
71 #define TEMP_CRIT_HYST 2
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 */
76 static const u16 W83795_REG_TEMP[][5] = {
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 */
88 static const u16 W83795_REG_IN[][3] = {
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 */
112 #define W83795_REG_VRLSB 0x3C
114 static const u8 W83795_REG_IN_HL_LSB[] = {
118 0x94, /* VTT, 3VDD, 3VSB, 3VBAT */
127 #define IN_LSB_REG(index, type) \
128 (((type) == 1) ? W83795_REG_IN_HL_LSB[(index)] \
129 : (W83795_REG_IN_HL_LSB[(index)] + 1))
131 #define IN_LSB_SHIFT 0
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 */
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) \
163 (((index) & 1) ? 4 : 0)
165 #define W83795_REG_VID_CTRL 0x6A
167 #define W83795_REG_ALARM(index) (0x41 + (index))
168 #define W83795_REG_BEEP(index) (0x50 + (index))
170 #define W83795_REG_CLR_CHASSIS 0x4D
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
178 #define W83795_REG_TSS(index) (0x209 + (index))
183 #define PWM_NONSTOP 3
184 #define PWM_STOP_TIME 4
185 #define W83795_REG_PWM(index, nr) (0x210 + (nr) * 8 + (index))
187 #define W83795_REG_FTSH(index) (0x240 + (index) * 2)
188 #define W83795_REG_FTSL(index) (0x241 + (index) * 2)
189 #define W83795_REG_TFTS 0x250
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))
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))
206 #define W83795_REG_DTSC 0x301
207 #define W83795_REG_DTSE 0x302
208 #define W83795_REG_DTS(index) (0x26 + (index))
209 #define W83795_REG_PECI_TBASE(index) (0x320 + (index))
212 #define DTS_CRIT_HYST 1
214 #define DTS_WARN_HYST 3
215 #define W83795_REG_DTS_EXT(index) (0xB2 + (index))
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))
222 static inline u16 in_from_reg(u8 index, u16 val)
224 /* 3VDD, 3VSB and VBAT: 6 mV/bit; other inputs: 2 mV/bit */
225 if (index >= 12 && index <= 14)
231 static inline u16 in_to_reg(u8 index, u16 val)
233 if (index >= 12 && index <= 14)
239 static inline unsigned long fan_from_reg(u16 val)
241 if ((val == 0xfff) || (val == 0))
243 return 1350000UL / val;
246 static inline u16 fan_to_reg(long rpm)
250 return SENSORS_LIMIT((1350000 + (rpm >> 1)) / rpm, 1, 0xffe);
253 static inline unsigned long time_from_reg(u8 reg)
258 static inline u8 time_to_reg(unsigned long val)
260 return SENSORS_LIMIT((val + 50) / 100, 0, 0xff);
263 static inline long temp_from_reg(s8 reg)
268 static inline s8 temp_to_reg(long val, s8 min, s8 max)
270 return SENSORS_LIMIT(val / 1000, min, max);
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
278 static unsigned int pwm_freq_from_reg(u8 reg, u16 clkin)
280 unsigned long base_clock;
283 base_clock = clkin * 1000 / ((clkin == 48000) ? 384 : 256);
284 return base_clock / ((reg & 0x7f) + 1);
286 return pwm_freq_cksel0[reg & 0x0f];
289 static u8 pwm_freq_to_reg(unsigned long val, u16 clkin)
291 unsigned long base_clock;
293 unsigned long best0, best1;
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)
301 if (val < 375) /* cksel = 1 can't beat this */
303 best0 = pwm_freq_cksel0[reg0];
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);
311 /* Choose the closest one */
312 if (abs(val - best0) > abs(val - best1))
318 enum chip_types {w83795g, w83795adg};
321 struct device *hwmon_dev;
322 struct mutex update_lock;
323 unsigned long last_updated; /* In jiffies */
324 enum chip_types chip_type;
328 u32 has_in; /* Enable monitor VIN or not */
329 u8 has_dyn_in; /* Only in2-0 can have this */
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 */
334 u16 has_fan; /* Enable fan14-1 or not */
335 u16 fan[14]; /* Register value combine */
336 u16 fan_min[14]; /* Register value combine */
338 u8 has_temp; /* Enable monitor temp6-1 or not */
339 s8 temp[6][5]; /* current, crit, crit_hyst, warn, warn_hyst */
340 u8 temp_read_vrlsb[6];
341 u8 temp_mode; /* Bit vector, 0 = TR, 1 = TD */
342 u8 temp_src[3]; /* Register value */
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 */
348 s8 dts[8]; /* Register value */
349 u8 dts_read_vrlsb[8]; /* Register value */
350 s8 dts_ext[4]; /* Register value */
352 u8 has_pwm; /* 795g supports 8 pwm, 795adg only supports 2,
353 * no config register, only affected by chip
355 u8 pwm[8][5]; /* Register value, output, freq, start,
356 * non stop, stop time */
357 u16 clkin; /* CLKIN frequency in kHz */
358 u8 pwm_fcms[2]; /* Register value */
359 u8 pwm_tfmr[6]; /* Register value */
360 u8 pwm_fomc; /* Register value */
362 u16 target_speed[8]; /* Register value, target speed for speed
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 */
368 u8 setup_pwm[3]; /* Register value */
370 u8 alarms[6]; /* Register value */
371 u8 beeps[6]; /* Register value */
378 * We assume that nobdody can change the bank outside the driver.
381 /* Must be called with data->update_lock held, except during initialization */
382 static int w83795_set_bank(struct i2c_client *client, u8 bank)
384 struct w83795_data *data = i2c_get_clientdata(client);
387 /* If the same bank is already set, nothing to do */
388 if ((data->bank & 0x07) == bank)
391 /* Change to new bank, preserve all other bits */
392 bank |= data->bank & ~0x07;
393 err = i2c_smbus_write_byte_data(client, W83795_REG_BANKSEL, bank);
395 dev_err(&client->dev,
396 "Failed to set bank to %d, err %d\n",
405 /* Must be called with data->update_lock held, except during initialization */
406 static u8 w83795_read(struct i2c_client *client, u16 reg)
410 err = w83795_set_bank(client, reg >> 8);
412 return 0x00; /* Arbitrary */
414 err = i2c_smbus_read_byte_data(client, reg & 0xff);
416 dev_err(&client->dev,
417 "Failed to read from register 0x%03x, err %d\n",
419 return 0x00; /* Arbitrary */
424 /* Must be called with data->update_lock held, except during initialization */
425 static int w83795_write(struct i2c_client *client, u16 reg, u8 value)
429 err = w83795_set_bank(client, reg >> 8);
433 err = i2c_smbus_write_byte_data(client, reg & 0xff, value);
435 dev_err(&client->dev,
436 "Failed to write to register 0x%03x, err %d\n",
441 static void w83795_update_limits(struct i2c_client *client)
443 struct w83795_data *data = i2c_get_clientdata(client);
446 /* Read the voltage limits */
447 for (i = 0; i < ARRAY_SIZE(data->in); i++) {
448 if (!(data->has_in & (1 << i)))
450 data->in[i][IN_MAX] =
451 w83795_read(client, W83795_REG_IN[i][IN_MAX]);
452 data->in[i][IN_LOW] =
453 w83795_read(client, W83795_REG_IN[i][IN_LOW]);
455 for (i = 0; i < ARRAY_SIZE(data->in_lsb); i++) {
456 if ((i == 2 && data->chip_type == w83795adg) ||
457 (i >= 4 && !(data->has_in & (1 << (i + 11)))))
459 data->in_lsb[i][IN_MAX] =
460 w83795_read(client, IN_LSB_REG(i, IN_MAX));
461 data->in_lsb[i][IN_LOW] =
462 w83795_read(client, IN_LSB_REG(i, IN_LOW));
465 /* Read the fan limits */
466 for (i = 0; i < ARRAY_SIZE(data->fan); i++) {
469 /* Each register contains LSB for 2 fans, but we want to
470 * read it only once to save time */
471 if ((i & 1) == 0 && (data->has_fan & (3 << i)))
472 lsb = w83795_read(client, W83795_REG_FAN_MIN_LSB(i));
474 if (!(data->has_fan & (1 << i)))
477 w83795_read(client, W83795_REG_FAN_MIN_HL(i)) << 4;
479 (lsb >> W83795_REG_FAN_MIN_LSB_SHIFT(i)) & 0x0F;
482 /* Read the temperature limits */
483 for (i = 0; i < ARRAY_SIZE(data->temp); i++) {
484 if (!(data->has_temp & (1 << i)))
486 for (limit = TEMP_CRIT; limit <= TEMP_WARN_HYST; limit++)
487 data->temp[i][limit] =
488 w83795_read(client, W83795_REG_TEMP[i][limit]);
491 /* Read the DTS limits */
492 if (data->enable_dts != 0) {
493 for (limit = DTS_CRIT; limit <= DTS_WARN_HYST; limit++)
494 data->dts_ext[limit] =
495 w83795_read(client, W83795_REG_DTS_EXT(limit));
498 /* Read beep settings */
499 for (i = 0; i < ARRAY_SIZE(data->beeps); i++)
500 data->beeps[i] = w83795_read(client, W83795_REG_BEEP(i));
503 static void w83795_update_pwm_config(struct i2c_client *client)
505 struct w83795_data *data = i2c_get_clientdata(client);
508 /* Read temperature source selection */
509 for (i = 0; i < ARRAY_SIZE(data->temp_src); i++)
510 data->temp_src[i] = w83795_read(client, W83795_REG_TSS(i));
512 /* Read automatic fan speed control settings */
513 data->pwm_fcms[0] = w83795_read(client, W83795_REG_FCMS1);
514 data->pwm_fcms[1] = w83795_read(client, W83795_REG_FCMS2);
515 for (i = 0; i < ARRAY_SIZE(data->pwm_tfmr); i++)
516 data->pwm_tfmr[i] = w83795_read(client, W83795_REG_TFMR(i));
517 data->pwm_fomc = w83795_read(client, W83795_REG_FOMC);
518 for (i = 0; i < data->has_pwm; i++) {
519 for (tmp = PWM_FREQ; tmp <= PWM_STOP_TIME; tmp++)
521 w83795_read(client, W83795_REG_PWM(i, tmp));
523 for (i = 0; i < ARRAY_SIZE(data->target_speed); i++) {
524 data->target_speed[i] =
525 w83795_read(client, W83795_REG_FTSH(i)) << 4;
526 data->target_speed[i] |=
527 w83795_read(client, W83795_REG_FTSL(i)) >> 4;
529 data->tol_speed = w83795_read(client, W83795_REG_TFTS) & 0x3f;
531 for (i = 0; i < ARRAY_SIZE(data->pwm_temp); i++) {
532 data->pwm_temp[i][TEMP_PWM_TTTI] =
533 w83795_read(client, W83795_REG_TTTI(i)) & 0x7f;
534 data->pwm_temp[i][TEMP_PWM_CTFS] =
535 w83795_read(client, W83795_REG_CTFS(i));
536 tmp = w83795_read(client, W83795_REG_HT(i));
537 data->pwm_temp[i][TEMP_PWM_HCT] = (tmp >> 4) & 0x0f;
538 data->pwm_temp[i][TEMP_PWM_HOT] = tmp & 0x0f;
541 /* Read SmartFanIV trip points */
542 for (i = 0; i < ARRAY_SIZE(data->sf4_reg); i++) {
543 for (tmp = 0; tmp < 7; tmp++) {
544 data->sf4_reg[i][SF4_TEMP][tmp] =
546 W83795_REG_SF4_TEMP(i, tmp));
547 data->sf4_reg[i][SF4_PWM][tmp] =
548 w83795_read(client, W83795_REG_SF4_PWM(i, tmp));
553 for (i = 0; i < ARRAY_SIZE(data->setup_pwm); i++)
555 w83795_read(client, W83795_REG_SETUP_PWM(i));
558 static struct w83795_data *w83795_update_device(struct device *dev)
560 struct i2c_client *client = to_i2c_client(dev);
561 struct w83795_data *data = i2c_get_clientdata(client);
565 mutex_lock(&data->update_lock);
567 if (!(time_after(jiffies, data->last_updated + HZ * 2)
571 /* Update the voltages value */
572 for (i = 0; i < ARRAY_SIZE(data->in); i++) {
573 if (!(data->has_in & (1 << i)))
575 tmp = w83795_read(client, W83795_REG_IN[i][IN_READ]) << 2;
576 tmp |= w83795_read(client, W83795_REG_VRLSB) >> 6;
577 data->in[i][IN_READ] = tmp;
580 /* in0-2 can have dynamic limits (W83795G only) */
581 if (data->has_dyn_in) {
582 u8 lsb_max = w83795_read(client, IN_LSB_REG(0, IN_MAX));
583 u8 lsb_low = w83795_read(client, IN_LSB_REG(0, IN_LOW));
585 for (i = 0; i < 3; i++) {
586 if (!(data->has_dyn_in & (1 << i)))
588 data->in[i][IN_MAX] =
589 w83795_read(client, W83795_REG_IN[i][IN_MAX]);
590 data->in[i][IN_LOW] =
591 w83795_read(client, W83795_REG_IN[i][IN_LOW]);
592 data->in_lsb[i][IN_MAX] = (lsb_max >> (2 * i)) & 0x03;
593 data->in_lsb[i][IN_LOW] = (lsb_low >> (2 * i)) & 0x03;
598 for (i = 0; i < ARRAY_SIZE(data->fan); i++) {
599 if (!(data->has_fan & (1 << i)))
601 data->fan[i] = w83795_read(client, W83795_REG_FAN(i)) << 4;
603 (w83795_read(client, W83795_REG_VRLSB) >> 4) & 0x0F;
606 /* Update temperature */
607 for (i = 0; i < ARRAY_SIZE(data->temp); i++) {
608 data->temp[i][TEMP_READ] =
609 w83795_read(client, W83795_REG_TEMP[i][TEMP_READ]);
610 data->temp_read_vrlsb[i] =
611 w83795_read(client, W83795_REG_VRLSB);
614 /* Update dts temperature */
615 if (data->enable_dts != 0) {
616 for (i = 0; i < ARRAY_SIZE(data->dts); i++) {
617 if (!(data->has_dts & (1 << i)))
620 w83795_read(client, W83795_REG_DTS(i));
621 data->dts_read_vrlsb[i] =
622 w83795_read(client, W83795_REG_VRLSB);
626 /* Update pwm output */
627 for (i = 0; i < data->has_pwm; i++) {
628 data->pwm[i][PWM_OUTPUT] =
629 w83795_read(client, W83795_REG_PWM(i, PWM_OUTPUT));
633 for (i = 0; i < ARRAY_SIZE(data->alarms); i++)
634 data->alarms[i] = w83795_read(client, W83795_REG_ALARM(i));
636 data->last_updated = jiffies;
640 mutex_unlock(&data->update_lock);
648 #define ALARM_STATUS 0
649 #define BEEP_ENABLE 1
651 show_alarm_beep(struct device *dev, struct device_attribute *attr, char *buf)
653 struct w83795_data *data = w83795_update_device(dev);
654 struct sensor_device_attribute_2 *sensor_attr =
655 to_sensor_dev_attr_2(attr);
656 int nr = sensor_attr->nr;
657 int index = sensor_attr->index >> 3;
658 int bit = sensor_attr->index & 0x07;
661 if (ALARM_STATUS == nr) {
662 val = (data->alarms[index] >> (bit)) & 1;
663 } else { /* BEEP_ENABLE */
664 val = (data->beeps[index] >> (bit)) & 1;
667 return sprintf(buf, "%u\n", val);
671 store_beep(struct device *dev, struct device_attribute *attr,
672 const char *buf, size_t count)
674 struct i2c_client *client = to_i2c_client(dev);
675 struct w83795_data *data = i2c_get_clientdata(client);
676 struct sensor_device_attribute_2 *sensor_attr =
677 to_sensor_dev_attr_2(attr);
678 int index = sensor_attr->index >> 3;
679 int shift = sensor_attr->index & 0x07;
680 u8 beep_bit = 1 << shift;
683 if (strict_strtoul(buf, 10, &val) < 0)
685 if (val != 0 && val != 1)
688 mutex_lock(&data->update_lock);
689 data->beeps[index] = w83795_read(client, W83795_REG_BEEP(index));
690 data->beeps[index] &= ~beep_bit;
691 data->beeps[index] |= val << shift;
692 w83795_write(client, W83795_REG_BEEP(index), data->beeps[index]);
693 mutex_unlock(&data->update_lock);
698 /* Write any value to clear chassis alarm */
700 store_chassis_clear(struct device *dev,
701 struct device_attribute *attr, const char *buf,
704 struct i2c_client *client = to_i2c_client(dev);
705 struct w83795_data *data = i2c_get_clientdata(client);
708 mutex_lock(&data->update_lock);
709 val = w83795_read(client, W83795_REG_CLR_CHASSIS);
711 w83795_write(client, W83795_REG_CLR_CHASSIS, val);
712 mutex_unlock(&data->update_lock);
719 show_fan(struct device *dev, struct device_attribute *attr, char *buf)
721 struct sensor_device_attribute_2 *sensor_attr =
722 to_sensor_dev_attr_2(attr);
723 int nr = sensor_attr->nr;
724 int index = sensor_attr->index;
725 struct w83795_data *data = w83795_update_device(dev);
729 val = data->fan[index] & 0x0fff;
731 val = data->fan_min[index] & 0x0fff;
733 return sprintf(buf, "%lu\n", fan_from_reg(val));
737 store_fan_min(struct device *dev, struct device_attribute *attr,
738 const char *buf, size_t count)
740 struct sensor_device_attribute_2 *sensor_attr =
741 to_sensor_dev_attr_2(attr);
742 int index = sensor_attr->index;
743 struct i2c_client *client = to_i2c_client(dev);
744 struct w83795_data *data = i2c_get_clientdata(client);
747 if (strict_strtoul(buf, 10, &val))
749 val = fan_to_reg(val);
751 mutex_lock(&data->update_lock);
752 data->fan_min[index] = val;
753 w83795_write(client, W83795_REG_FAN_MIN_HL(index), (val >> 4) & 0xff);
757 val |= w83795_read(client, W83795_REG_FAN_MIN_LSB(index))
760 val |= w83795_read(client, W83795_REG_FAN_MIN_LSB(index))
763 w83795_write(client, W83795_REG_FAN_MIN_LSB(index), val & 0xff);
764 mutex_unlock(&data->update_lock);
770 show_pwm(struct device *dev, struct device_attribute *attr, char *buf)
772 struct w83795_data *data = w83795_update_device(dev);
773 struct sensor_device_attribute_2 *sensor_attr =
774 to_sensor_dev_attr_2(attr);
775 int nr = sensor_attr->nr;
776 int index = sensor_attr->index;
781 val = time_from_reg(data->pwm[index][nr]);
784 val = pwm_freq_from_reg(data->pwm[index][nr], data->clkin);
787 val = data->pwm[index][nr];
791 return sprintf(buf, "%u\n", val);
795 store_pwm(struct device *dev, struct device_attribute *attr,
796 const char *buf, size_t count)
798 struct i2c_client *client = to_i2c_client(dev);
799 struct w83795_data *data = i2c_get_clientdata(client);
800 struct sensor_device_attribute_2 *sensor_attr =
801 to_sensor_dev_attr_2(attr);
802 int nr = sensor_attr->nr;
803 int index = sensor_attr->index;
806 if (strict_strtoul(buf, 10, &val) < 0)
809 mutex_lock(&data->update_lock);
812 val = time_to_reg(val);
815 val = pwm_freq_to_reg(val, data->clkin);
818 val = SENSORS_LIMIT(val, 0, 0xff);
821 w83795_write(client, W83795_REG_PWM(index, nr), val);
822 data->pwm[index][nr] = val;
823 mutex_unlock(&data->update_lock);
828 show_pwm_enable(struct device *dev, struct device_attribute *attr, char *buf)
830 struct sensor_device_attribute_2 *sensor_attr =
831 to_sensor_dev_attr_2(attr);
832 struct i2c_client *client = to_i2c_client(dev);
833 struct w83795_data *data = i2c_get_clientdata(client);
834 int index = sensor_attr->index;
837 if (1 == (data->pwm_fcms[0] & (1 << index))) {
841 for (tmp = 0; tmp < 6; tmp++) {
842 if (data->pwm_tfmr[tmp] & (1 << index)) {
847 if (data->pwm_fomc & (1 << index))
853 return sprintf(buf, "%u\n", tmp);
857 store_pwm_enable(struct device *dev, struct device_attribute *attr,
858 const char *buf, size_t count)
860 struct i2c_client *client = to_i2c_client(dev);
861 struct w83795_data *data = i2c_get_clientdata(client);
862 struct sensor_device_attribute_2 *sensor_attr =
863 to_sensor_dev_attr_2(attr);
864 int index = sensor_attr->index;
868 if (strict_strtoul(buf, 10, &val) < 0)
873 mutex_lock(&data->update_lock);
877 data->pwm_fcms[0] &= ~(1 << index);
878 w83795_write(client, W83795_REG_FCMS1, data->pwm_fcms[0]);
879 for (i = 0; i < 6; i++) {
880 data->pwm_tfmr[i] &= ~(1 << index);
881 w83795_write(client, W83795_REG_TFMR(i),
884 data->pwm_fomc |= 1 << index;
885 data->pwm_fomc ^= val << index;
886 w83795_write(client, W83795_REG_FOMC, data->pwm_fomc);
889 data->pwm_fcms[0] |= (1 << index);
890 w83795_write(client, W83795_REG_FCMS1, data->pwm_fcms[0]);
893 mutex_unlock(&data->update_lock);
898 show_temp_src(struct device *dev, struct device_attribute *attr, char *buf)
900 struct sensor_device_attribute_2 *sensor_attr =
901 to_sensor_dev_attr_2(attr);
902 struct i2c_client *client = to_i2c_client(dev);
903 struct w83795_data *data = i2c_get_clientdata(client);
904 int index = sensor_attr->index;
906 u8 tmp = data->temp_src[val];
915 return sprintf(buf, "%u\n", tmp);
919 store_temp_src(struct device *dev, struct device_attribute *attr,
920 const char *buf, size_t count)
922 struct i2c_client *client = to_i2c_client(dev);
923 struct w83795_data *data = i2c_get_clientdata(client);
924 struct sensor_device_attribute_2 *sensor_attr =
925 to_sensor_dev_attr_2(attr);
926 int index = sensor_attr->index;
930 if (strict_strtoul(buf, 10, &tmp) < 0)
932 tmp = SENSORS_LIMIT(tmp, 0, 15);
934 mutex_lock(&data->update_lock);
937 data->temp_src[val] &= 0x0f;
939 data->temp_src[val] &= 0xf0;
941 data->temp_src[val] |= tmp;
942 w83795_write(client, W83795_REG_TSS(val), data->temp_src[val]);
943 mutex_unlock(&data->update_lock);
948 #define TEMP_PWM_ENABLE 0
949 #define TEMP_PWM_FAN_MAP 1
951 show_temp_pwm_enable(struct device *dev, struct device_attribute *attr,
954 struct i2c_client *client = to_i2c_client(dev);
955 struct w83795_data *data = i2c_get_clientdata(client);
956 struct sensor_device_attribute_2 *sensor_attr =
957 to_sensor_dev_attr_2(attr);
958 int nr = sensor_attr->nr;
959 int index = sensor_attr->index;
963 case TEMP_PWM_ENABLE:
964 tmp = (data->pwm_fcms[1] >> index) & 1;
970 case TEMP_PWM_FAN_MAP:
971 tmp = data->pwm_tfmr[index];
975 return sprintf(buf, "%u\n", tmp);
979 store_temp_pwm_enable(struct device *dev, struct device_attribute *attr,
980 const char *buf, size_t count)
982 struct i2c_client *client = to_i2c_client(dev);
983 struct w83795_data *data = i2c_get_clientdata(client);
984 struct sensor_device_attribute_2 *sensor_attr =
985 to_sensor_dev_attr_2(attr);
986 int nr = sensor_attr->nr;
987 int index = sensor_attr->index;
990 if (strict_strtoul(buf, 10, &tmp) < 0)
994 case TEMP_PWM_ENABLE:
995 if ((tmp != 3) && (tmp != 4))
998 mutex_lock(&data->update_lock);
999 data->pwm_fcms[1] &= ~(1 << index);
1000 data->pwm_fcms[1] |= tmp << index;
1001 w83795_write(client, W83795_REG_FCMS2, data->pwm_fcms[1]);
1002 mutex_unlock(&data->update_lock);
1004 case TEMP_PWM_FAN_MAP:
1005 mutex_lock(&data->update_lock);
1006 tmp = SENSORS_LIMIT(tmp, 0, 0xff);
1007 w83795_write(client, W83795_REG_TFMR(index), tmp);
1008 data->pwm_tfmr[index] = tmp;
1009 mutex_unlock(&data->update_lock);
1015 #define FANIN_TARGET 0
1018 show_fanin(struct device *dev, struct device_attribute *attr, char *buf)
1020 struct i2c_client *client = to_i2c_client(dev);
1021 struct w83795_data *data = i2c_get_clientdata(client);
1022 struct sensor_device_attribute_2 *sensor_attr =
1023 to_sensor_dev_attr_2(attr);
1024 int nr = sensor_attr->nr;
1025 int index = sensor_attr->index;
1030 tmp = fan_from_reg(data->target_speed[index]);
1033 tmp = data->tol_speed;
1037 return sprintf(buf, "%u\n", tmp);
1041 store_fanin(struct device *dev, struct device_attribute *attr,
1042 const char *buf, size_t count)
1044 struct i2c_client *client = to_i2c_client(dev);
1045 struct w83795_data *data = i2c_get_clientdata(client);
1046 struct sensor_device_attribute_2 *sensor_attr =
1047 to_sensor_dev_attr_2(attr);
1048 int nr = sensor_attr->nr;
1049 int index = sensor_attr->index;
1052 if (strict_strtoul(buf, 10, &val) < 0)
1055 mutex_lock(&data->update_lock);
1058 val = fan_to_reg(SENSORS_LIMIT(val, 0, 0xfff));
1059 w83795_write(client, W83795_REG_FTSH(index), (val >> 4) & 0xff);
1060 w83795_write(client, W83795_REG_FTSL(index), (val << 4) & 0xf0);
1061 data->target_speed[index] = val;
1064 val = SENSORS_LIMIT(val, 0, 0x3f);
1065 w83795_write(client, W83795_REG_TFTS, val);
1066 data->tol_speed = val;
1069 mutex_unlock(&data->update_lock);
1076 show_temp_pwm(struct device *dev, struct device_attribute *attr, char *buf)
1078 struct i2c_client *client = to_i2c_client(dev);
1079 struct w83795_data *data = i2c_get_clientdata(client);
1080 struct sensor_device_attribute_2 *sensor_attr =
1081 to_sensor_dev_attr_2(attr);
1082 int nr = sensor_attr->nr;
1083 int index = sensor_attr->index;
1084 long tmp = temp_from_reg(data->pwm_temp[index][nr]);
1086 return sprintf(buf, "%ld\n", tmp);
1090 store_temp_pwm(struct device *dev, struct device_attribute *attr,
1091 const char *buf, size_t count)
1093 struct i2c_client *client = to_i2c_client(dev);
1094 struct w83795_data *data = i2c_get_clientdata(client);
1095 struct sensor_device_attribute_2 *sensor_attr =
1096 to_sensor_dev_attr_2(attr);
1097 int nr = sensor_attr->nr;
1098 int index = sensor_attr->index;
1102 if (strict_strtoul(buf, 10, &val) < 0)
1106 mutex_lock(&data->update_lock);
1109 val = SENSORS_LIMIT(val, 0, 0x7f);
1110 w83795_write(client, W83795_REG_TTTI(index), val);
1113 val = SENSORS_LIMIT(val, 0, 0x7f);
1114 w83795_write(client, W83795_REG_CTFS(index), val);
1117 val = SENSORS_LIMIT(val, 0, 0x0f);
1118 tmp = w83795_read(client, W83795_REG_HT(index));
1120 tmp |= (val << 4) & 0xf0;
1121 w83795_write(client, W83795_REG_HT(index), tmp);
1124 val = SENSORS_LIMIT(val, 0, 0x0f);
1125 tmp = w83795_read(client, W83795_REG_HT(index));
1128 w83795_write(client, W83795_REG_HT(index), tmp);
1131 data->pwm_temp[index][nr] = val;
1132 mutex_unlock(&data->update_lock);
1138 show_sf4_pwm(struct device *dev, struct device_attribute *attr, char *buf)
1140 struct i2c_client *client = to_i2c_client(dev);
1141 struct w83795_data *data = i2c_get_clientdata(client);
1142 struct sensor_device_attribute_2 *sensor_attr =
1143 to_sensor_dev_attr_2(attr);
1144 int nr = sensor_attr->nr;
1145 int index = sensor_attr->index;
1147 return sprintf(buf, "%u\n", data->sf4_reg[index][SF4_PWM][nr]);
1151 store_sf4_pwm(struct device *dev, struct device_attribute *attr,
1152 const char *buf, size_t count)
1154 struct i2c_client *client = to_i2c_client(dev);
1155 struct w83795_data *data = i2c_get_clientdata(client);
1156 struct sensor_device_attribute_2 *sensor_attr =
1157 to_sensor_dev_attr_2(attr);
1158 int nr = sensor_attr->nr;
1159 int index = sensor_attr->index;
1162 if (strict_strtoul(buf, 10, &val) < 0)
1165 mutex_lock(&data->update_lock);
1166 w83795_write(client, W83795_REG_SF4_PWM(index, nr), val);
1167 data->sf4_reg[index][SF4_PWM][nr] = val;
1168 mutex_unlock(&data->update_lock);
1174 show_sf4_temp(struct device *dev, struct device_attribute *attr, char *buf)
1176 struct i2c_client *client = to_i2c_client(dev);
1177 struct w83795_data *data = i2c_get_clientdata(client);
1178 struct sensor_device_attribute_2 *sensor_attr =
1179 to_sensor_dev_attr_2(attr);
1180 int nr = sensor_attr->nr;
1181 int index = sensor_attr->index;
1183 return sprintf(buf, "%u\n",
1184 (data->sf4_reg[index][SF4_TEMP][nr]) * 1000);
1188 store_sf4_temp(struct device *dev, struct device_attribute *attr,
1189 const char *buf, size_t count)
1191 struct i2c_client *client = to_i2c_client(dev);
1192 struct w83795_data *data = i2c_get_clientdata(client);
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;
1199 if (strict_strtoul(buf, 10, &val) < 0)
1203 mutex_lock(&data->update_lock);
1204 w83795_write(client, W83795_REG_SF4_TEMP(index, nr), val);
1205 data->sf4_reg[index][SF4_TEMP][nr] = val;
1206 mutex_unlock(&data->update_lock);
1213 show_temp(struct device *dev, struct device_attribute *attr, char *buf)
1215 struct sensor_device_attribute_2 *sensor_attr =
1216 to_sensor_dev_attr_2(attr);
1217 int nr = sensor_attr->nr;
1218 int index = sensor_attr->index;
1219 struct w83795_data *data = w83795_update_device(dev);
1220 long temp = temp_from_reg(data->temp[index][nr]);
1222 if (TEMP_READ == nr)
1223 temp += (data->temp_read_vrlsb[index] >> 6) * 250;
1224 return sprintf(buf, "%ld\n", temp);
1228 store_temp(struct device *dev, struct device_attribute *attr,
1229 const char *buf, size_t count)
1231 struct sensor_device_attribute_2 *sensor_attr =
1232 to_sensor_dev_attr_2(attr);
1233 int nr = sensor_attr->nr;
1234 int index = sensor_attr->index;
1235 struct i2c_client *client = to_i2c_client(dev);
1236 struct w83795_data *data = i2c_get_clientdata(client);
1239 if (strict_strtol(buf, 10, &tmp) < 0)
1242 mutex_lock(&data->update_lock);
1243 data->temp[index][nr] = temp_to_reg(tmp, -128, 127);
1244 w83795_write(client, W83795_REG_TEMP[index][nr], data->temp[index][nr]);
1245 mutex_unlock(&data->update_lock);
1251 show_dts_mode(struct device *dev, struct device_attribute *attr, char *buf)
1253 struct i2c_client *client = to_i2c_client(dev);
1254 struct w83795_data *data = i2c_get_clientdata(client);
1257 if (data->enable_dts & 2)
1262 return sprintf(buf, "%d\n", tmp);
1266 show_dts(struct device *dev, struct device_attribute *attr, char *buf)
1268 struct sensor_device_attribute_2 *sensor_attr =
1269 to_sensor_dev_attr_2(attr);
1270 int index = sensor_attr->index;
1271 struct w83795_data *data = w83795_update_device(dev);
1272 long temp = temp_from_reg(data->dts[index]);
1274 temp += (data->dts_read_vrlsb[index] >> 6) * 250;
1275 return sprintf(buf, "%ld\n", temp);
1279 show_dts_ext(struct device *dev, struct device_attribute *attr, char *buf)
1281 struct sensor_device_attribute_2 *sensor_attr =
1282 to_sensor_dev_attr_2(attr);
1283 int nr = sensor_attr->nr;
1284 struct i2c_client *client = to_i2c_client(dev);
1285 struct w83795_data *data = i2c_get_clientdata(client);
1286 long temp = temp_from_reg(data->dts_ext[nr]);
1288 return sprintf(buf, "%ld\n", temp);
1292 store_dts_ext(struct device *dev, struct device_attribute *attr,
1293 const char *buf, size_t count)
1295 struct sensor_device_attribute_2 *sensor_attr =
1296 to_sensor_dev_attr_2(attr);
1297 int nr = sensor_attr->nr;
1298 struct i2c_client *client = to_i2c_client(dev);
1299 struct w83795_data *data = i2c_get_clientdata(client);
1302 if (strict_strtol(buf, 10, &tmp) < 0)
1305 mutex_lock(&data->update_lock);
1306 data->dts_ext[nr] = temp_to_reg(tmp, -128, 127);
1307 w83795_write(client, W83795_REG_DTS_EXT(nr), data->dts_ext[nr]);
1308 mutex_unlock(&data->update_lock);
1314 show_temp_mode(struct device *dev, struct device_attribute *attr, char *buf)
1316 struct i2c_client *client = to_i2c_client(dev);
1317 struct w83795_data *data = i2c_get_clientdata(client);
1318 struct sensor_device_attribute_2 *sensor_attr =
1319 to_sensor_dev_attr_2(attr);
1320 int index = sensor_attr->index;
1323 if (data->temp_mode & (1 << index))
1324 tmp = 3; /* Thermal diode */
1326 tmp = 4; /* Thermistor */
1328 return sprintf(buf, "%d\n", tmp);
1331 /* Only for temp1-4 (temp5-6 can only be thermistor) */
1333 store_temp_mode(struct device *dev, struct device_attribute *attr,
1334 const char *buf, size_t count)
1336 struct i2c_client *client = to_i2c_client(dev);
1337 struct w83795_data *data = i2c_get_clientdata(client);
1338 struct sensor_device_attribute_2 *sensor_attr =
1339 to_sensor_dev_attr_2(attr);
1340 int index = sensor_attr->index;
1345 if (strict_strtoul(buf, 10, &val) < 0)
1347 if ((val != 4) && (val != 3))
1350 mutex_lock(&data->update_lock);
1354 data->temp_mode |= 1 << index;
1355 } else if (val == 4) {
1358 data->temp_mode &= ~(1 << index);
1361 reg_shift = 2 * index;
1362 tmp = w83795_read(client, W83795_REG_TEMP_CTRL2);
1363 tmp &= ~(0x03 << reg_shift);
1364 tmp |= val << reg_shift;
1365 w83795_write(client, W83795_REG_TEMP_CTRL2, tmp);
1367 mutex_unlock(&data->update_lock);
1372 /* show/store VIN */
1374 show_in(struct device *dev, struct device_attribute *attr, char *buf)
1376 struct sensor_device_attribute_2 *sensor_attr =
1377 to_sensor_dev_attr_2(attr);
1378 int nr = sensor_attr->nr;
1379 int index = sensor_attr->index;
1380 struct w83795_data *data = w83795_update_device(dev);
1381 u16 val = data->in[index][nr];
1386 /* calculate this value again by sensors as sensors3.conf */
1387 if ((index >= 17) &&
1388 !((data->has_gain >> (index - 17)) & 1))
1393 lsb_idx = IN_LSB_SHIFT_IDX[index][IN_LSB_IDX];
1395 val |= (data->in_lsb[lsb_idx][nr] >>
1396 IN_LSB_SHIFT_IDX[lsb_idx][IN_LSB_SHIFT]) & 0x03;
1397 if ((index >= 17) &&
1398 !((data->has_gain >> (index - 17)) & 1))
1402 val = in_from_reg(index, val);
1404 return sprintf(buf, "%d\n", val);
1408 store_in(struct device *dev, struct device_attribute *attr,
1409 const char *buf, size_t count)
1411 struct sensor_device_attribute_2 *sensor_attr =
1412 to_sensor_dev_attr_2(attr);
1413 int nr = sensor_attr->nr;
1414 int index = sensor_attr->index;
1415 struct i2c_client *client = to_i2c_client(dev);
1416 struct w83795_data *data = i2c_get_clientdata(client);
1421 if (strict_strtoul(buf, 10, &val) < 0)
1423 val = in_to_reg(index, val);
1425 if ((index >= 17) &&
1426 !((data->has_gain >> (index - 17)) & 1))
1428 val = SENSORS_LIMIT(val, 0, 0x3FF);
1429 mutex_lock(&data->update_lock);
1431 lsb_idx = IN_LSB_SHIFT_IDX[index][IN_LSB_IDX];
1432 tmp = w83795_read(client, IN_LSB_REG(lsb_idx, nr));
1433 tmp &= ~(0x03 << IN_LSB_SHIFT_IDX[index][IN_LSB_SHIFT]);
1434 tmp |= (val & 0x03) << IN_LSB_SHIFT_IDX[index][IN_LSB_SHIFT];
1435 w83795_write(client, IN_LSB_REG(lsb_idx, nr), tmp);
1436 data->in_lsb[lsb_idx][nr] = tmp;
1438 tmp = (val >> 2) & 0xff;
1439 w83795_write(client, W83795_REG_IN[index][nr], tmp);
1440 data->in[index][nr] = tmp;
1442 mutex_unlock(&data->update_lock);
1448 show_sf_setup(struct device *dev, struct device_attribute *attr, char *buf)
1450 struct sensor_device_attribute_2 *sensor_attr =
1451 to_sensor_dev_attr_2(attr);
1452 int nr = sensor_attr->nr;
1453 struct i2c_client *client = to_i2c_client(dev);
1454 struct w83795_data *data = i2c_get_clientdata(client);
1455 u16 val = data->setup_pwm[nr];
1458 case SETUP_PWM_UPTIME:
1459 case SETUP_PWM_DOWNTIME:
1460 val = time_from_reg(val);
1464 return sprintf(buf, "%d\n", val);
1468 store_sf_setup(struct device *dev, struct device_attribute *attr,
1469 const char *buf, size_t count)
1471 struct sensor_device_attribute_2 *sensor_attr =
1472 to_sensor_dev_attr_2(attr);
1473 int nr = sensor_attr->nr;
1474 struct i2c_client *client = to_i2c_client(dev);
1475 struct w83795_data *data = i2c_get_clientdata(client);
1478 if (strict_strtoul(buf, 10, &val) < 0)
1482 case SETUP_PWM_DEFAULT:
1483 val = SENSORS_LIMIT(val, 0, 0xff);
1485 case SETUP_PWM_UPTIME:
1486 case SETUP_PWM_DOWNTIME:
1487 val = time_to_reg(val);
1493 mutex_lock(&data->update_lock);
1494 data->setup_pwm[nr] = val;
1495 w83795_write(client, W83795_REG_SETUP_PWM(nr), val);
1496 mutex_unlock(&data->update_lock);
1503 /* Don't change the attribute order, _max and _min are accessed by index
1504 * somewhere else in the code */
1505 #define SENSOR_ATTR_IN(index) { \
1506 SENSOR_ATTR_2(in##index##_input, S_IRUGO, show_in, NULL, \
1508 SENSOR_ATTR_2(in##index##_max, S_IRUGO | S_IWUSR, show_in, \
1509 store_in, IN_MAX, index), \
1510 SENSOR_ATTR_2(in##index##_min, S_IRUGO | S_IWUSR, show_in, \
1511 store_in, IN_LOW, index), \
1512 SENSOR_ATTR_2(in##index##_alarm, S_IRUGO, show_alarm_beep, \
1513 NULL, ALARM_STATUS, index + ((index > 14) ? 1 : 0)), \
1514 SENSOR_ATTR_2(in##index##_beep, S_IWUSR | S_IRUGO, \
1515 show_alarm_beep, store_beep, BEEP_ENABLE, \
1516 index + ((index > 14) ? 1 : 0)) }
1518 #define SENSOR_ATTR_FAN(index) { \
1519 SENSOR_ATTR_2(fan##index##_input, S_IRUGO, show_fan, \
1520 NULL, FAN_INPUT, index - 1), \
1521 SENSOR_ATTR_2(fan##index##_min, S_IWUSR | S_IRUGO, \
1522 show_fan, store_fan_min, FAN_MIN, index - 1), \
1523 SENSOR_ATTR_2(fan##index##_alarm, S_IRUGO, show_alarm_beep, \
1524 NULL, ALARM_STATUS, index + 31), \
1525 SENSOR_ATTR_2(fan##index##_beep, S_IWUSR | S_IRUGO, \
1526 show_alarm_beep, store_beep, BEEP_ENABLE, index + 31) }
1528 #define SENSOR_ATTR_PWM(index) { \
1529 SENSOR_ATTR_2(pwm##index, S_IWUSR | S_IRUGO, show_pwm, \
1530 store_pwm, PWM_OUTPUT, index - 1), \
1531 SENSOR_ATTR_2(pwm##index##_nonstop, S_IWUSR | S_IRUGO, \
1532 show_pwm, store_pwm, PWM_NONSTOP, index - 1), \
1533 SENSOR_ATTR_2(pwm##index##_start, S_IWUSR | S_IRUGO, \
1534 show_pwm, store_pwm, PWM_START, index - 1), \
1535 SENSOR_ATTR_2(pwm##index##_stop_time, S_IWUSR | S_IRUGO, \
1536 show_pwm, store_pwm, PWM_STOP_TIME, index - 1), \
1537 SENSOR_ATTR_2(pwm##index##_freq, S_IWUSR | S_IRUGO, \
1538 show_pwm, store_pwm, PWM_FREQ, index - 1), \
1539 SENSOR_ATTR_2(pwm##index##_enable, S_IWUSR | S_IRUGO, \
1540 show_pwm_enable, store_pwm_enable, NOT_USED, index - 1), \
1541 SENSOR_ATTR_2(fan##index##_target, S_IWUSR | S_IRUGO, \
1542 show_fanin, store_fanin, FANIN_TARGET, index - 1) }
1544 #define SENSOR_ATTR_DTS(index) { \
1545 SENSOR_ATTR_2(temp##index##_type, S_IRUGO , \
1546 show_dts_mode, NULL, NOT_USED, index - 7), \
1547 SENSOR_ATTR_2(temp##index##_input, S_IRUGO, show_dts, \
1548 NULL, NOT_USED, index - 7), \
1549 SENSOR_ATTR_2(temp##index##_crit, S_IRUGO | S_IWUSR, show_dts_ext, \
1550 store_dts_ext, DTS_CRIT, NOT_USED), \
1551 SENSOR_ATTR_2(temp##index##_crit_hyst, S_IRUGO | S_IWUSR, \
1552 show_dts_ext, store_dts_ext, DTS_CRIT_HYST, NOT_USED), \
1553 SENSOR_ATTR_2(temp##index##_max, S_IRUGO | S_IWUSR, show_dts_ext, \
1554 store_dts_ext, DTS_WARN, NOT_USED), \
1555 SENSOR_ATTR_2(temp##index##_max_hyst, S_IRUGO | S_IWUSR, \
1556 show_dts_ext, store_dts_ext, DTS_WARN_HYST, NOT_USED), \
1557 SENSOR_ATTR_2(temp##index##_alarm, S_IRUGO, \
1558 show_alarm_beep, NULL, ALARM_STATUS, index + 17), \
1559 SENSOR_ATTR_2(temp##index##_beep, S_IWUSR | S_IRUGO, \
1560 show_alarm_beep, store_beep, BEEP_ENABLE, index + 17) }
1562 #define SENSOR_ATTR_TEMP(index) { \
1563 SENSOR_ATTR_2(temp##index##_type, S_IRUGO | (index < 4 ? S_IWUSR : 0), \
1564 show_temp_mode, store_temp_mode, NOT_USED, index - 1), \
1565 SENSOR_ATTR_2(temp##index##_input, S_IRUGO, show_temp, \
1566 NULL, TEMP_READ, index - 1), \
1567 SENSOR_ATTR_2(temp##index##_crit, S_IRUGO | S_IWUSR, show_temp, \
1568 store_temp, TEMP_CRIT, index - 1), \
1569 SENSOR_ATTR_2(temp##index##_crit_hyst, S_IRUGO | S_IWUSR, \
1570 show_temp, store_temp, TEMP_CRIT_HYST, index - 1), \
1571 SENSOR_ATTR_2(temp##index##_max, S_IRUGO | S_IWUSR, show_temp, \
1572 store_temp, TEMP_WARN, index - 1), \
1573 SENSOR_ATTR_2(temp##index##_max_hyst, S_IRUGO | S_IWUSR, \
1574 show_temp, store_temp, TEMP_WARN_HYST, index - 1), \
1575 SENSOR_ATTR_2(temp##index##_alarm, S_IRUGO, \
1576 show_alarm_beep, NULL, ALARM_STATUS, \
1577 index + (index > 4 ? 11 : 17)), \
1578 SENSOR_ATTR_2(temp##index##_beep, S_IWUSR | S_IRUGO, \
1579 show_alarm_beep, store_beep, BEEP_ENABLE, \
1580 index + (index > 4 ? 11 : 17)), \
1581 SENSOR_ATTR_2(temp##index##_source_sel, S_IWUSR | S_IRUGO, \
1582 show_temp_src, store_temp_src, NOT_USED, index - 1), \
1583 SENSOR_ATTR_2(temp##index##_pwm_enable, S_IWUSR | S_IRUGO, \
1584 show_temp_pwm_enable, store_temp_pwm_enable, \
1585 TEMP_PWM_ENABLE, index - 1), \
1586 SENSOR_ATTR_2(temp##index##_auto_channels_pwm, S_IWUSR | S_IRUGO, \
1587 show_temp_pwm_enable, store_temp_pwm_enable, \
1588 TEMP_PWM_FAN_MAP, index - 1), \
1589 SENSOR_ATTR_2(thermal_cruise##index, S_IWUSR | S_IRUGO, \
1590 show_temp_pwm, store_temp_pwm, TEMP_PWM_TTTI, index - 1), \
1591 SENSOR_ATTR_2(temp##index##_warn, S_IWUSR | S_IRUGO, \
1592 show_temp_pwm, store_temp_pwm, TEMP_PWM_CTFS, index - 1), \
1593 SENSOR_ATTR_2(temp##index##_warn_hyst, S_IWUSR | S_IRUGO, \
1594 show_temp_pwm, store_temp_pwm, TEMP_PWM_HCT, index - 1), \
1595 SENSOR_ATTR_2(temp##index##_operation_hyst, S_IWUSR | S_IRUGO, \
1596 show_temp_pwm, store_temp_pwm, TEMP_PWM_HOT, index - 1), \
1597 SENSOR_ATTR_2(temp##index##_auto_point1_pwm, S_IRUGO | S_IWUSR, \
1598 show_sf4_pwm, store_sf4_pwm, 0, index - 1), \
1599 SENSOR_ATTR_2(temp##index##_auto_point2_pwm, S_IRUGO | S_IWUSR, \
1600 show_sf4_pwm, store_sf4_pwm, 1, index - 1), \
1601 SENSOR_ATTR_2(temp##index##_auto_point3_pwm, S_IRUGO | S_IWUSR, \
1602 show_sf4_pwm, store_sf4_pwm, 2, index - 1), \
1603 SENSOR_ATTR_2(temp##index##_auto_point4_pwm, S_IRUGO | S_IWUSR, \
1604 show_sf4_pwm, store_sf4_pwm, 3, index - 1), \
1605 SENSOR_ATTR_2(temp##index##_auto_point5_pwm, S_IRUGO | S_IWUSR, \
1606 show_sf4_pwm, store_sf4_pwm, 4, index - 1), \
1607 SENSOR_ATTR_2(temp##index##_auto_point6_pwm, S_IRUGO | S_IWUSR, \
1608 show_sf4_pwm, store_sf4_pwm, 5, index - 1), \
1609 SENSOR_ATTR_2(temp##index##_auto_point7_pwm, S_IRUGO | S_IWUSR, \
1610 show_sf4_pwm, store_sf4_pwm, 6, index - 1), \
1611 SENSOR_ATTR_2(temp##index##_auto_point1_temp, S_IRUGO | S_IWUSR,\
1612 show_sf4_temp, store_sf4_temp, 0, index - 1), \
1613 SENSOR_ATTR_2(temp##index##_auto_point2_temp, S_IRUGO | S_IWUSR,\
1614 show_sf4_temp, store_sf4_temp, 1, index - 1), \
1615 SENSOR_ATTR_2(temp##index##_auto_point3_temp, S_IRUGO | S_IWUSR,\
1616 show_sf4_temp, store_sf4_temp, 2, index - 1), \
1617 SENSOR_ATTR_2(temp##index##_auto_point4_temp, S_IRUGO | S_IWUSR,\
1618 show_sf4_temp, store_sf4_temp, 3, index - 1), \
1619 SENSOR_ATTR_2(temp##index##_auto_point5_temp, S_IRUGO | S_IWUSR,\
1620 show_sf4_temp, store_sf4_temp, 4, index - 1), \
1621 SENSOR_ATTR_2(temp##index##_auto_point6_temp, S_IRUGO | S_IWUSR,\
1622 show_sf4_temp, store_sf4_temp, 5, index - 1), \
1623 SENSOR_ATTR_2(temp##index##_auto_point7_temp, S_IRUGO | S_IWUSR,\
1624 show_sf4_temp, store_sf4_temp, 6, index - 1) }
1627 static struct sensor_device_attribute_2 w83795_in[][5] = {
1651 static const struct sensor_device_attribute_2 w83795_fan[][4] = {
1661 SENSOR_ATTR_FAN(10),
1662 SENSOR_ATTR_FAN(11),
1663 SENSOR_ATTR_FAN(12),
1664 SENSOR_ATTR_FAN(13),
1665 SENSOR_ATTR_FAN(14),
1668 static const struct sensor_device_attribute_2 w83795_temp[][29] = {
1669 SENSOR_ATTR_TEMP(1),
1670 SENSOR_ATTR_TEMP(2),
1671 SENSOR_ATTR_TEMP(3),
1672 SENSOR_ATTR_TEMP(4),
1673 SENSOR_ATTR_TEMP(5),
1674 SENSOR_ATTR_TEMP(6),
1677 static const struct sensor_device_attribute_2 w83795_dts[][8] = {
1681 SENSOR_ATTR_DTS(10),
1682 SENSOR_ATTR_DTS(11),
1683 SENSOR_ATTR_DTS(12),
1684 SENSOR_ATTR_DTS(13),
1685 SENSOR_ATTR_DTS(14),
1688 static const struct sensor_device_attribute_2 w83795_pwm[][7] = {
1699 static const struct sensor_device_attribute_2 sda_single_files[] = {
1700 SENSOR_ATTR_2(chassis, S_IWUSR | S_IRUGO, show_alarm_beep,
1701 store_chassis_clear, ALARM_STATUS, 46),
1702 SENSOR_ATTR_2(beep_enable, S_IWUSR | S_IRUGO, show_alarm_beep,
1703 store_beep, BEEP_ENABLE, 47),
1704 SENSOR_ATTR_2(speed_cruise_tolerance, S_IWUSR | S_IRUGO, show_fanin,
1705 store_fanin, FANIN_TOL, NOT_USED),
1706 SENSOR_ATTR_2(pwm_default, S_IWUSR | S_IRUGO, show_sf_setup,
1707 store_sf_setup, SETUP_PWM_DEFAULT, NOT_USED),
1708 SENSOR_ATTR_2(pwm_uptime, S_IWUSR | S_IRUGO, show_sf_setup,
1709 store_sf_setup, SETUP_PWM_UPTIME, NOT_USED),
1710 SENSOR_ATTR_2(pwm_downtime, S_IWUSR | S_IRUGO, show_sf_setup,
1711 store_sf_setup, SETUP_PWM_DOWNTIME, NOT_USED),
1718 static void w83795_init_client(struct i2c_client *client)
1720 struct w83795_data *data = i2c_get_clientdata(client);
1721 static const u16 clkin[4] = { /* in kHz */
1722 14318, 24000, 33333, 48000
1727 w83795_write(client, W83795_REG_CONFIG, 0x80);
1729 /* Start monitoring if needed */
1730 config = w83795_read(client, W83795_REG_CONFIG);
1731 if (!(config & W83795_REG_CONFIG_START)) {
1732 dev_info(&client->dev, "Enabling monitoring operations\n");
1733 w83795_write(client, W83795_REG_CONFIG,
1734 config | W83795_REG_CONFIG_START);
1737 data->clkin = clkin[(config >> 3) & 0x3];
1738 dev_dbg(&client->dev, "clkin = %u kHz\n", data->clkin);
1741 static int w83795_get_device_id(struct i2c_client *client)
1745 device_id = i2c_smbus_read_byte_data(client, W83795_REG_DEVICEID);
1747 /* Special case for rev. A chips; can't be checked first because later
1748 revisions emulate this for compatibility */
1749 if (device_id < 0 || (device_id & 0xf0) != 0x50) {
1752 alt_id = i2c_smbus_read_byte_data(client,
1753 W83795_REG_DEVICEID_A);
1761 /* Return 0 if detection is successful, -ENODEV otherwise */
1762 static int w83795_detect(struct i2c_client *client,
1763 struct i2c_board_info *info)
1765 int bank, vendor_id, device_id, expected, i2c_addr, config;
1766 struct i2c_adapter *adapter = client->adapter;
1767 unsigned short address = client->addr;
1768 const char *chip_name;
1770 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1772 bank = i2c_smbus_read_byte_data(client, W83795_REG_BANKSEL);
1773 if (bank < 0 || (bank & 0x7c)) {
1774 dev_dbg(&adapter->dev,
1775 "w83795: Detection failed at addr 0x%02hx, check %s\n",
1780 /* Check Nuvoton vendor ID */
1781 vendor_id = i2c_smbus_read_byte_data(client, W83795_REG_VENDORID);
1782 expected = bank & 0x80 ? 0x5c : 0xa3;
1783 if (vendor_id != expected) {
1784 dev_dbg(&adapter->dev,
1785 "w83795: Detection failed at addr 0x%02hx, check %s\n",
1786 address, "vendor id");
1790 /* Check device ID */
1791 device_id = w83795_get_device_id(client) |
1792 (i2c_smbus_read_byte_data(client, W83795_REG_CHIPID) << 8);
1793 if ((device_id >> 4) != 0x795) {
1794 dev_dbg(&adapter->dev,
1795 "w83795: Detection failed at addr 0x%02hx, check %s\n",
1796 address, "device id\n");
1800 /* If Nuvoton chip, address of chip and W83795_REG_I2C_ADDR
1802 if ((bank & 0x07) == 0) {
1803 i2c_addr = i2c_smbus_read_byte_data(client,
1804 W83795_REG_I2C_ADDR);
1805 if ((i2c_addr & 0x7f) != address) {
1806 dev_dbg(&adapter->dev,
1807 "w83795: Detection failed at addr 0x%02hx, "
1808 "check %s\n", address, "i2c addr");
1813 /* Check 795 chip type: 795G or 795ADG
1814 Usually we don't write to chips during detection, but here we don't
1815 quite have the choice; hopefully it's OK, we are about to return
1817 if ((bank & 0x07) != 0)
1818 i2c_smbus_write_byte_data(client, W83795_REG_BANKSEL,
1820 config = i2c_smbus_read_byte_data(client, W83795_REG_CONFIG);
1821 if (config & W83795_REG_CONFIG_CONFIG48)
1822 chip_name = "w83795adg";
1824 chip_name = "w83795g";
1826 strlcpy(info->type, chip_name, I2C_NAME_SIZE);
1827 dev_info(&adapter->dev, "Found %s rev. %c at 0x%02hx\n", chip_name,
1828 'A' + (device_id & 0xf), address);
1833 static int w83795_handle_files(struct device *dev, int (*fn)(struct device *,
1834 const struct device_attribute *))
1836 struct w83795_data *data = dev_get_drvdata(dev);
1839 for (i = 0; i < ARRAY_SIZE(w83795_in); i++) {
1840 if (!(data->has_in & (1 << i)))
1842 for (j = 0; j < ARRAY_SIZE(w83795_in[0]); j++) {
1843 err = fn(dev, &w83795_in[i][j].dev_attr);
1849 for (i = 0; i < ARRAY_SIZE(w83795_fan); i++) {
1850 if (!(data->has_fan & (1 << i)))
1852 for (j = 0; j < ARRAY_SIZE(w83795_fan[0]); j++) {
1853 err = fn(dev, &w83795_fan[i][j].dev_attr);
1859 for (i = 0; i < ARRAY_SIZE(sda_single_files); i++) {
1860 err = fn(dev, &sda_single_files[i].dev_attr);
1865 for (i = 0; i < data->has_pwm; i++) {
1866 for (j = 0; j < ARRAY_SIZE(w83795_pwm[0]); j++) {
1867 err = fn(dev, &w83795_pwm[i][j].dev_attr);
1873 for (i = 0; i < ARRAY_SIZE(w83795_temp); i++) {
1874 if (!(data->has_temp & (1 << i)))
1876 for (j = 0; j < ARRAY_SIZE(w83795_temp[0]); j++) {
1877 err = fn(dev, &w83795_temp[i][j].dev_attr);
1883 if (data->enable_dts != 0) {
1884 for (i = 0; i < ARRAY_SIZE(w83795_dts); i++) {
1885 if (!(data->has_dts & (1 << i)))
1887 for (j = 0; j < ARRAY_SIZE(w83795_dts[0]); j++) {
1888 err = fn(dev, &w83795_dts[i][j].dev_attr);
1898 /* We need a wrapper that fits in w83795_handle_files */
1899 static int device_remove_file_wrapper(struct device *dev,
1900 const struct device_attribute *attr)
1902 device_remove_file(dev, attr);
1906 static void w83795_check_dynamic_in_limits(struct i2c_client *client)
1908 struct w83795_data *data = i2c_get_clientdata(client);
1910 int i, err_max, err_min;
1912 vid_ctl = w83795_read(client, W83795_REG_VID_CTRL);
1914 /* Return immediately if VRM isn't configured */
1915 if ((vid_ctl & 0x07) == 0x00 || (vid_ctl & 0x07) == 0x07)
1918 data->has_dyn_in = (vid_ctl >> 3) & 0x07;
1919 for (i = 0; i < 2; i++) {
1920 if (!(data->has_dyn_in & (1 << i)))
1923 /* Voltage limits in dynamic mode, switch to read-only */
1924 err_max = sysfs_chmod_file(&client->dev.kobj,
1925 &w83795_in[i][2].dev_attr.attr,
1927 err_min = sysfs_chmod_file(&client->dev.kobj,
1928 &w83795_in[i][3].dev_attr.attr,
1930 if (err_max || err_min)
1931 dev_warn(&client->dev, "Failed to set in%d limits "
1932 "read-only (%d, %d)\n", i, err_max, err_min);
1934 dev_info(&client->dev, "in%d limits set dynamically "
1939 /* Check pins that can be used for either temperature or voltage monitoring */
1940 static void w83795_apply_temp_config(struct w83795_data *data, u8 config,
1941 int temp_chan, int in_chan)
1943 /* config is a 2-bit value */
1945 case 0x2: /* Voltage monitoring */
1946 data->has_in |= 1 << in_chan;
1948 case 0x1: /* Thermal diode */
1951 data->temp_mode |= 1 << temp_chan;
1953 case 0x3: /* Thermistor */
1954 data->has_temp |= 1 << temp_chan;
1959 static int w83795_probe(struct i2c_client *client,
1960 const struct i2c_device_id *id)
1964 struct device *dev = &client->dev;
1965 struct w83795_data *data;
1968 data = kzalloc(sizeof(struct w83795_data), GFP_KERNEL);
1974 i2c_set_clientdata(client, data);
1975 data->chip_type = id->driver_data;
1976 data->bank = i2c_smbus_read_byte_data(client, W83795_REG_BANKSEL);
1977 mutex_init(&data->update_lock);
1979 /* Initialize the chip */
1980 w83795_init_client(client);
1982 /* Check which voltages and fans are present */
1983 data->has_in = w83795_read(client, W83795_REG_VOLT_CTRL1)
1984 | (w83795_read(client, W83795_REG_VOLT_CTRL2) << 8);
1985 data->has_fan = w83795_read(client, W83795_REG_FANIN_CTRL1)
1986 | (w83795_read(client, W83795_REG_FANIN_CTRL2) << 8);
1988 /* Check which analog temperatures and extra voltages are present */
1989 tmp = w83795_read(client, W83795_REG_TEMP_CTRL1);
1991 data->enable_dts = 1;
1992 w83795_apply_temp_config(data, (tmp >> 2) & 0x3, 5, 16);
1993 w83795_apply_temp_config(data, tmp & 0x3, 4, 15);
1994 tmp = w83795_read(client, W83795_REG_TEMP_CTRL2);
1995 w83795_apply_temp_config(data, tmp >> 6, 3, 20);
1996 w83795_apply_temp_config(data, (tmp >> 4) & 0x3, 2, 19);
1997 w83795_apply_temp_config(data, (tmp >> 2) & 0x3, 1, 18);
1998 w83795_apply_temp_config(data, tmp & 0x3, 0, 17);
2000 /* Check DTS enable status */
2001 if (data->enable_dts) {
2002 if (1 & w83795_read(client, W83795_REG_DTSC))
2003 data->enable_dts |= 2;
2004 data->has_dts = w83795_read(client, W83795_REG_DTSE);
2007 /* Report PECI Tbase values */
2008 if (data->enable_dts == 1) {
2009 for (i = 0; i < 8; i++) {
2010 if (!(data->has_dts & (1 << i)))
2012 tmp = w83795_read(client, W83795_REG_PECI_TBASE(i));
2013 dev_info(&client->dev,
2014 "PECI agent %d Tbase temperature: %u\n",
2015 i + 1, (unsigned int)tmp & 0x7f);
2019 data->has_gain = w83795_read(client, W83795_REG_VMIGB_CTRL) & 0x0f;
2020 w83795_update_limits(client);
2022 /* pwm and smart fan */
2023 if (data->chip_type == w83795g)
2027 w83795_update_pwm_config(client);
2029 err = w83795_handle_files(dev, device_create_file);
2033 if (data->chip_type == w83795g)
2034 w83795_check_dynamic_in_limits(client);
2036 data->hwmon_dev = hwmon_device_register(dev);
2037 if (IS_ERR(data->hwmon_dev)) {
2038 err = PTR_ERR(data->hwmon_dev);
2045 w83795_handle_files(dev, device_remove_file_wrapper);
2051 static int w83795_remove(struct i2c_client *client)
2053 struct w83795_data *data = i2c_get_clientdata(client);
2055 hwmon_device_unregister(data->hwmon_dev);
2056 w83795_handle_files(&client->dev, device_remove_file_wrapper);
2063 static const struct i2c_device_id w83795_id[] = {
2064 { "w83795g", w83795g },
2065 { "w83795adg", w83795adg },
2068 MODULE_DEVICE_TABLE(i2c, w83795_id);
2070 static struct i2c_driver w83795_driver = {
2074 .probe = w83795_probe,
2075 .remove = w83795_remove,
2076 .id_table = w83795_id,
2078 .class = I2C_CLASS_HWMON,
2079 .detect = w83795_detect,
2080 .address_list = normal_i2c,
2083 static int __init sensors_w83795_init(void)
2085 return i2c_add_driver(&w83795_driver);
2088 static void __exit sensors_w83795_exit(void)
2090 i2c_del_driver(&w83795_driver);
2093 MODULE_AUTHOR("Wei Song");
2094 MODULE_DESCRIPTION("W83795G/ADG hardware monitoring driver");
2095 MODULE_LICENSE("GPL");
2097 module_init(sensors_w83795_init);
2098 module_exit(sensors_w83795_exit);