2 * w83795.c - Linux kernel driver for hardware monitoring
3 * Copyright (C) 2008 Nuvoton Technology Corp.
5 * Copyright (C) 2010 Jean Delvare <khali@linux-fr.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation - version 2.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * Supports following chips:
23 * Chip #vin #fanin #pwm #temp #dts wchipid vendid i2c ISA
24 * w83795g 21 14 8 6 8 0x79 0x5ca3 yes no
25 * w83795adg 18 14 2 6 8 0x79 0x5ca3 yes no
28 #include <linux/kernel.h>
29 #include <linux/module.h>
30 #include <linux/init.h>
31 #include <linux/slab.h>
32 #include <linux/i2c.h>
33 #include <linux/hwmon.h>
34 #include <linux/hwmon-sysfs.h>
35 #include <linux/err.h>
36 #include <linux/mutex.h>
37 #include <linux/delay.h>
39 /* Addresses to scan */
40 static const unsigned short normal_i2c[] = {
41 0x2c, 0x2d, 0x2e, 0x2f, I2C_CLIENT_END
46 module_param(reset, bool, 0);
47 MODULE_PARM_DESC(reset, "Set to 1 to reset chip, not recommended");
50 #define W83795_REG_BANKSEL 0x00
51 #define W83795_REG_VENDORID 0xfd
52 #define W83795_REG_CHIPID 0xfe
53 #define W83795_REG_DEVICEID 0xfb
54 #define W83795_REG_DEVICEID_A 0xff
56 #define W83795_REG_I2C_ADDR 0xfc
57 #define W83795_REG_CONFIG 0x01
58 #define W83795_REG_CONFIG_CONFIG48 0x04
59 #define W83795_REG_CONFIG_START 0x01
61 /* Multi-Function Pin Ctrl Registers */
62 #define W83795_REG_VOLT_CTRL1 0x02
63 #define W83795_REG_VOLT_CTRL2 0x03
64 #define W83795_REG_TEMP_CTRL1 0x04
65 #define W83795_REG_TEMP_CTRL2 0x05
66 #define W83795_REG_FANIN_CTRL1 0x06
67 #define W83795_REG_FANIN_CTRL2 0x07
68 #define W83795_REG_VMIGB_CTRL 0x08
72 #define TEMP_CRIT_HYST 2
74 #define TEMP_WARN_HYST 4
75 /* only crit and crit_hyst affect real-time alarm status
76 * current crit crit_hyst warn warn_hyst */
77 static const u16 W83795_REG_TEMP[][5] = {
78 {0x21, 0x96, 0x97, 0x98, 0x99}, /* TD1/TR1 */
79 {0x22, 0x9a, 0x9b, 0x9c, 0x9d}, /* TD2/TR2 */
80 {0x23, 0x9e, 0x9f, 0xa0, 0xa1}, /* TD3/TR3 */
81 {0x24, 0xa2, 0xa3, 0xa4, 0xa5}, /* TD4/TR4 */
82 {0x1f, 0xa6, 0xa7, 0xa8, 0xa9}, /* TR5 */
83 {0x20, 0xaa, 0xab, 0xac, 0xad}, /* TR6 */
89 static const u16 W83795_REG_IN[][3] = {
91 {0x10, 0x70, 0x71}, /* VSEN1 */
92 {0x11, 0x72, 0x73}, /* VSEN2 */
93 {0x12, 0x74, 0x75}, /* VSEN3 */
94 {0x13, 0x76, 0x77}, /* VSEN4 */
95 {0x14, 0x78, 0x79}, /* VSEN5 */
96 {0x15, 0x7a, 0x7b}, /* VSEN6 */
97 {0x16, 0x7c, 0x7d}, /* VSEN7 */
98 {0x17, 0x7e, 0x7f}, /* VSEN8 */
99 {0x18, 0x80, 0x81}, /* VSEN9 */
100 {0x19, 0x82, 0x83}, /* VSEN10 */
101 {0x1A, 0x84, 0x85}, /* VSEN11 */
102 {0x1B, 0x86, 0x87}, /* VTT */
103 {0x1C, 0x88, 0x89}, /* 3VDD */
104 {0x1D, 0x8a, 0x8b}, /* 3VSB */
105 {0x1E, 0x8c, 0x8d}, /* VBAT */
106 {0x1F, 0xa6, 0xa7}, /* VSEN12 */
107 {0x20, 0xaa, 0xab}, /* VSEN13 */
108 {0x21, 0x96, 0x97}, /* VSEN14 */
109 {0x22, 0x9a, 0x9b}, /* VSEN15 */
110 {0x23, 0x9e, 0x9f}, /* VSEN16 */
111 {0x24, 0xa2, 0xa3}, /* VSEN17 */
113 #define W83795_REG_VRLSB 0x3C
115 static const u8 W83795_REG_IN_HL_LSB[] = {
119 0x94, /* VTT, 3VDD, 3VSB, 3VBAT */
128 #define IN_LSB_REG(index, type) \
129 (((type) == 1) ? W83795_REG_IN_HL_LSB[(index)] \
130 : (W83795_REG_IN_HL_LSB[(index)] + 1))
132 #define IN_LSB_SHIFT 0
134 static const u8 IN_LSB_SHIFT_IDX[][2] = {
135 /* High/Low LSB shift, LSB No. */
136 {0x00, 0x00}, /* VSEN1 */
137 {0x02, 0x00}, /* VSEN2 */
138 {0x04, 0x00}, /* VSEN3 */
139 {0x06, 0x00}, /* VSEN4 */
140 {0x00, 0x01}, /* VSEN5 */
141 {0x02, 0x01}, /* VSEN6 */
142 {0x04, 0x01}, /* VSEN7 */
143 {0x06, 0x01}, /* VSEN8 */
144 {0x00, 0x02}, /* VSEN9 */
145 {0x02, 0x02}, /* VSEN10 */
146 {0x04, 0x02}, /* VSEN11 */
147 {0x00, 0x03}, /* VTT */
148 {0x02, 0x03}, /* 3VDD */
149 {0x04, 0x03}, /* 3VSB */
150 {0x06, 0x03}, /* VBAT */
151 {0x06, 0x04}, /* VSEN12 */
152 {0x06, 0x05}, /* VSEN13 */
153 {0x06, 0x06}, /* VSEN14 */
154 {0x06, 0x07}, /* VSEN15 */
155 {0x06, 0x08}, /* VSEN16 */
156 {0x06, 0x09}, /* VSEN17 */
160 #define W83795_REG_FAN(index) (0x2E + (index))
161 #define W83795_REG_FAN_MIN_HL(index) (0xB6 + (index))
162 #define W83795_REG_FAN_MIN_LSB(index) (0xC4 + (index) / 2)
163 #define W83795_REG_FAN_MIN_LSB_SHIFT(index) \
164 (((index) & 1) ? 4 : 0)
166 #define W83795_REG_VID_CTRL 0x6A
168 #define W83795_REG_ALARM(index) (0x41 + (index))
169 #define W83795_REG_BEEP(index) (0x50 + (index))
171 #define W83795_REG_CLR_CHASSIS 0x4D
174 #define W83795_REG_FCMS1 0x201
175 #define W83795_REG_FCMS2 0x208
176 #define W83795_REG_TFMR(index) (0x202 + (index))
177 #define W83795_REG_FOMC 0x20F
179 #define W83795_REG_TSS(index) (0x209 + (index))
181 #define TSS_MAP_RESERVED 0xff
182 static const u8 tss_map[4][6] = {
185 {10, 11, 12, 13, 2, 3},
186 { 4, 5, 4, 5, TSS_MAP_RESERVED, TSS_MAP_RESERVED},
192 #define PWM_NONSTOP 3
193 #define PWM_STOP_TIME 4
194 #define W83795_REG_PWM(index, nr) (0x210 + (nr) * 8 + (index))
196 #define W83795_REG_FTSH(index) (0x240 + (index) * 2)
197 #define W83795_REG_FTSL(index) (0x241 + (index) * 2)
198 #define W83795_REG_TFTS 0x250
200 #define TEMP_PWM_TTTI 0
201 #define TEMP_PWM_CTFS 1
202 #define TEMP_PWM_HCT 2
203 #define TEMP_PWM_HOT 3
204 #define W83795_REG_TTTI(index) (0x260 + (index))
205 #define W83795_REG_CTFS(index) (0x268 + (index))
206 #define W83795_REG_HT(index) (0x270 + (index))
210 #define W83795_REG_SF4_TEMP(temp_num, index) \
211 (0x280 + 0x10 * (temp_num) + (index))
212 #define W83795_REG_SF4_PWM(temp_num, index) \
213 (0x288 + 0x10 * (temp_num) + (index))
215 #define W83795_REG_DTSC 0x301
216 #define W83795_REG_DTSE 0x302
217 #define W83795_REG_DTS(index) (0x26 + (index))
218 #define W83795_REG_PECI_TBASE(index) (0x320 + (index))
221 #define DTS_CRIT_HYST 1
223 #define DTS_WARN_HYST 3
224 #define W83795_REG_DTS_EXT(index) (0xB2 + (index))
226 #define SETUP_PWM_DEFAULT 0
227 #define SETUP_PWM_UPTIME 1
228 #define SETUP_PWM_DOWNTIME 2
229 #define W83795_REG_SETUP_PWM(index) (0x20C + (index))
231 static inline u16 in_from_reg(u8 index, u16 val)
233 /* 3VDD, 3VSB and VBAT: 6 mV/bit; other inputs: 2 mV/bit */
234 if (index >= 12 && index <= 14)
240 static inline u16 in_to_reg(u8 index, u16 val)
242 if (index >= 12 && index <= 14)
248 static inline unsigned long fan_from_reg(u16 val)
250 if ((val == 0xfff) || (val == 0))
252 return 1350000UL / val;
255 static inline u16 fan_to_reg(long rpm)
259 return SENSORS_LIMIT((1350000 + (rpm >> 1)) / rpm, 1, 0xffe);
262 static inline unsigned long time_from_reg(u8 reg)
267 static inline u8 time_to_reg(unsigned long val)
269 return SENSORS_LIMIT((val + 50) / 100, 0, 0xff);
272 static inline long temp_from_reg(s8 reg)
277 static inline s8 temp_to_reg(long val, s8 min, s8 max)
279 return SENSORS_LIMIT(val / 1000, min, max);
282 static const u16 pwm_freq_cksel0[16] = {
283 1024, 512, 341, 256, 205, 171, 146, 128,
284 85, 64, 32, 16, 8, 4, 2, 1
287 static unsigned int pwm_freq_from_reg(u8 reg, u16 clkin)
289 unsigned long base_clock;
292 base_clock = clkin * 1000 / ((clkin == 48000) ? 384 : 256);
293 return base_clock / ((reg & 0x7f) + 1);
295 return pwm_freq_cksel0[reg & 0x0f];
298 static u8 pwm_freq_to_reg(unsigned long val, u16 clkin)
300 unsigned long base_clock;
302 unsigned long best0, best1;
304 /* Best fit for cksel = 0 */
305 for (reg0 = 0; reg0 < ARRAY_SIZE(pwm_freq_cksel0) - 1; reg0++) {
306 if (val > (pwm_freq_cksel0[reg0] +
307 pwm_freq_cksel0[reg0 + 1]) / 2)
310 if (val < 375) /* cksel = 1 can't beat this */
312 best0 = pwm_freq_cksel0[reg0];
314 /* Best fit for cksel = 1 */
315 base_clock = clkin * 1000 / ((clkin == 48000) ? 384 : 256);
316 reg1 = SENSORS_LIMIT(DIV_ROUND_CLOSEST(base_clock, val), 1, 128);
317 best1 = base_clock / reg1;
318 reg1 = 0x80 | (reg1 - 1);
320 /* Choose the closest one */
321 if (abs(val - best0) > abs(val - best1))
327 enum chip_types {w83795g, w83795adg};
330 struct device *hwmon_dev;
331 struct mutex update_lock;
332 unsigned long last_updated; /* In jiffies */
333 enum chip_types chip_type;
337 u32 has_in; /* Enable monitor VIN or not */
338 u8 has_dyn_in; /* Only in2-0 can have this */
339 u16 in[21][3]; /* Register value, read/high/low */
340 u8 in_lsb[10][3]; /* LSB Register value, high/low */
341 u8 has_gain; /* has gain: in17-20 * 8 */
343 u16 has_fan; /* Enable fan14-1 or not */
344 u16 fan[14]; /* Register value combine */
345 u16 fan_min[14]; /* Register value combine */
347 u8 has_temp; /* Enable monitor temp6-1 or not */
348 s8 temp[6][5]; /* current, crit, crit_hyst, warn, warn_hyst */
349 u8 temp_read_vrlsb[6];
350 u8 temp_mode; /* Bit vector, 0 = TR, 1 = TD */
351 u8 temp_src[3]; /* Register value */
353 u8 enable_dts; /* Enable PECI and SB-TSI,
354 * bit 0: =1 enable, =0 disable,
355 * bit 1: =1 AMD SB-TSI, =0 Intel PECI */
356 u8 has_dts; /* Enable monitor DTS temp */
357 s8 dts[8]; /* Register value */
358 u8 dts_read_vrlsb[8]; /* Register value */
359 s8 dts_ext[4]; /* Register value */
361 u8 has_pwm; /* 795g supports 8 pwm, 795adg only supports 2,
362 * no config register, only affected by chip
364 u8 pwm[8][5]; /* Register value, output, freq, start,
365 * non stop, stop time */
366 u16 clkin; /* CLKIN frequency in kHz */
367 u8 pwm_fcms[2]; /* Register value */
368 u8 pwm_tfmr[6]; /* Register value */
369 u8 pwm_fomc; /* Register value */
371 u16 target_speed[8]; /* Register value, target speed for speed
373 u8 tol_speed; /* tolerance of target speed */
374 u8 pwm_temp[6][4]; /* TTTI, CTFS, HCT, HOT */
375 u8 sf4_reg[6][2][7]; /* 6 temp, temp/dcpwm, 7 registers */
377 u8 setup_pwm[3]; /* Register value */
379 u8 alarms[6]; /* Register value */
380 u8 beeps[6]; /* Register value */
384 char valid_pwm_config;
389 * We assume that nobdody can change the bank outside the driver.
392 /* Must be called with data->update_lock held, except during initialization */
393 static int w83795_set_bank(struct i2c_client *client, u8 bank)
395 struct w83795_data *data = i2c_get_clientdata(client);
398 /* If the same bank is already set, nothing to do */
399 if ((data->bank & 0x07) == bank)
402 /* Change to new bank, preserve all other bits */
403 bank |= data->bank & ~0x07;
404 err = i2c_smbus_write_byte_data(client, W83795_REG_BANKSEL, bank);
406 dev_err(&client->dev,
407 "Failed to set bank to %d, err %d\n",
416 /* Must be called with data->update_lock held, except during initialization */
417 static u8 w83795_read(struct i2c_client *client, u16 reg)
421 err = w83795_set_bank(client, reg >> 8);
423 return 0x00; /* Arbitrary */
425 err = i2c_smbus_read_byte_data(client, reg & 0xff);
427 dev_err(&client->dev,
428 "Failed to read from register 0x%03x, err %d\n",
430 return 0x00; /* Arbitrary */
435 /* Must be called with data->update_lock held, except during initialization */
436 static int w83795_write(struct i2c_client *client, u16 reg, u8 value)
440 err = w83795_set_bank(client, reg >> 8);
444 err = i2c_smbus_write_byte_data(client, reg & 0xff, value);
446 dev_err(&client->dev,
447 "Failed to write to register 0x%03x, err %d\n",
452 static void w83795_update_limits(struct i2c_client *client)
454 struct w83795_data *data = i2c_get_clientdata(client);
457 /* Read the voltage limits */
458 for (i = 0; i < ARRAY_SIZE(data->in); i++) {
459 if (!(data->has_in & (1 << i)))
461 data->in[i][IN_MAX] =
462 w83795_read(client, W83795_REG_IN[i][IN_MAX]);
463 data->in[i][IN_LOW] =
464 w83795_read(client, W83795_REG_IN[i][IN_LOW]);
466 for (i = 0; i < ARRAY_SIZE(data->in_lsb); i++) {
467 if ((i == 2 && data->chip_type == w83795adg) ||
468 (i >= 4 && !(data->has_in & (1 << (i + 11)))))
470 data->in_lsb[i][IN_MAX] =
471 w83795_read(client, IN_LSB_REG(i, IN_MAX));
472 data->in_lsb[i][IN_LOW] =
473 w83795_read(client, IN_LSB_REG(i, IN_LOW));
476 /* Read the fan limits */
477 for (i = 0; i < ARRAY_SIZE(data->fan); i++) {
480 /* Each register contains LSB for 2 fans, but we want to
481 * read it only once to save time */
482 if ((i & 1) == 0 && (data->has_fan & (3 << i)))
483 lsb = w83795_read(client, W83795_REG_FAN_MIN_LSB(i));
485 if (!(data->has_fan & (1 << i)))
488 w83795_read(client, W83795_REG_FAN_MIN_HL(i)) << 4;
490 (lsb >> W83795_REG_FAN_MIN_LSB_SHIFT(i)) & 0x0F;
493 /* Read the temperature limits */
494 for (i = 0; i < ARRAY_SIZE(data->temp); i++) {
495 if (!(data->has_temp & (1 << i)))
497 for (limit = TEMP_CRIT; limit <= TEMP_WARN_HYST; limit++)
498 data->temp[i][limit] =
499 w83795_read(client, W83795_REG_TEMP[i][limit]);
502 /* Read the DTS limits */
503 if (data->enable_dts) {
504 for (limit = DTS_CRIT; limit <= DTS_WARN_HYST; limit++)
505 data->dts_ext[limit] =
506 w83795_read(client, W83795_REG_DTS_EXT(limit));
509 /* Read beep settings */
510 for (i = 0; i < ARRAY_SIZE(data->beeps); i++)
511 data->beeps[i] = w83795_read(client, W83795_REG_BEEP(i));
513 data->valid_limits = 1;
516 static struct w83795_data *w83795_update_pwm_config(struct device *dev)
518 struct i2c_client *client = to_i2c_client(dev);
519 struct w83795_data *data = i2c_get_clientdata(client);
522 mutex_lock(&data->update_lock);
524 if (data->valid_pwm_config)
527 /* Read temperature source selection */
528 for (i = 0; i < ARRAY_SIZE(data->temp_src); i++)
529 data->temp_src[i] = w83795_read(client, W83795_REG_TSS(i));
531 /* Read automatic fan speed control settings */
532 data->pwm_fcms[0] = w83795_read(client, W83795_REG_FCMS1);
533 data->pwm_fcms[1] = w83795_read(client, W83795_REG_FCMS2);
534 for (i = 0; i < ARRAY_SIZE(data->pwm_tfmr); i++)
535 data->pwm_tfmr[i] = w83795_read(client, W83795_REG_TFMR(i));
536 data->pwm_fomc = w83795_read(client, W83795_REG_FOMC);
537 for (i = 0; i < data->has_pwm; i++) {
538 for (tmp = PWM_FREQ; tmp <= PWM_STOP_TIME; tmp++)
540 w83795_read(client, W83795_REG_PWM(i, tmp));
542 for (i = 0; i < ARRAY_SIZE(data->target_speed); i++) {
543 data->target_speed[i] =
544 w83795_read(client, W83795_REG_FTSH(i)) << 4;
545 data->target_speed[i] |=
546 w83795_read(client, W83795_REG_FTSL(i)) >> 4;
548 data->tol_speed = w83795_read(client, W83795_REG_TFTS) & 0x3f;
550 for (i = 0; i < ARRAY_SIZE(data->pwm_temp); i++) {
551 data->pwm_temp[i][TEMP_PWM_TTTI] =
552 w83795_read(client, W83795_REG_TTTI(i)) & 0x7f;
553 data->pwm_temp[i][TEMP_PWM_CTFS] =
554 w83795_read(client, W83795_REG_CTFS(i));
555 tmp = w83795_read(client, W83795_REG_HT(i));
556 data->pwm_temp[i][TEMP_PWM_HCT] = tmp >> 4;
557 data->pwm_temp[i][TEMP_PWM_HOT] = tmp & 0x0f;
560 /* Read SmartFanIV trip points */
561 for (i = 0; i < ARRAY_SIZE(data->sf4_reg); i++) {
562 for (tmp = 0; tmp < 7; tmp++) {
563 data->sf4_reg[i][SF4_TEMP][tmp] =
565 W83795_REG_SF4_TEMP(i, tmp));
566 data->sf4_reg[i][SF4_PWM][tmp] =
567 w83795_read(client, W83795_REG_SF4_PWM(i, tmp));
572 for (i = 0; i < ARRAY_SIZE(data->setup_pwm); i++)
574 w83795_read(client, W83795_REG_SETUP_PWM(i));
576 data->valid_pwm_config = 1;
579 mutex_unlock(&data->update_lock);
583 static struct w83795_data *w83795_update_device(struct device *dev)
585 struct i2c_client *client = to_i2c_client(dev);
586 struct w83795_data *data = i2c_get_clientdata(client);
590 mutex_lock(&data->update_lock);
592 if (!data->valid_limits)
593 w83795_update_limits(client);
595 if (!(time_after(jiffies, data->last_updated + HZ * 2)
599 /* Update the voltages value */
600 for (i = 0; i < ARRAY_SIZE(data->in); i++) {
601 if (!(data->has_in & (1 << i)))
603 tmp = w83795_read(client, W83795_REG_IN[i][IN_READ]) << 2;
604 tmp |= w83795_read(client, W83795_REG_VRLSB) >> 6;
605 data->in[i][IN_READ] = tmp;
608 /* in0-2 can have dynamic limits (W83795G only) */
609 if (data->has_dyn_in) {
610 u8 lsb_max = w83795_read(client, IN_LSB_REG(0, IN_MAX));
611 u8 lsb_low = w83795_read(client, IN_LSB_REG(0, IN_LOW));
613 for (i = 0; i < 3; i++) {
614 if (!(data->has_dyn_in & (1 << i)))
616 data->in[i][IN_MAX] =
617 w83795_read(client, W83795_REG_IN[i][IN_MAX]);
618 data->in[i][IN_LOW] =
619 w83795_read(client, W83795_REG_IN[i][IN_LOW]);
620 data->in_lsb[i][IN_MAX] = (lsb_max >> (2 * i)) & 0x03;
621 data->in_lsb[i][IN_LOW] = (lsb_low >> (2 * i)) & 0x03;
626 for (i = 0; i < ARRAY_SIZE(data->fan); i++) {
627 if (!(data->has_fan & (1 << i)))
629 data->fan[i] = w83795_read(client, W83795_REG_FAN(i)) << 4;
630 data->fan[i] |= w83795_read(client, W83795_REG_VRLSB) >> 4;
633 /* Update temperature */
634 for (i = 0; i < ARRAY_SIZE(data->temp); i++) {
635 data->temp[i][TEMP_READ] =
636 w83795_read(client, W83795_REG_TEMP[i][TEMP_READ]);
637 data->temp_read_vrlsb[i] =
638 w83795_read(client, W83795_REG_VRLSB);
641 /* Update dts temperature */
642 if (data->enable_dts) {
643 for (i = 0; i < ARRAY_SIZE(data->dts); i++) {
644 if (!(data->has_dts & (1 << i)))
647 w83795_read(client, W83795_REG_DTS(i));
648 data->dts_read_vrlsb[i] =
649 w83795_read(client, W83795_REG_VRLSB);
653 /* Update pwm output */
654 for (i = 0; i < data->has_pwm; i++) {
655 data->pwm[i][PWM_OUTPUT] =
656 w83795_read(client, W83795_REG_PWM(i, PWM_OUTPUT));
660 for (i = 0; i < ARRAY_SIZE(data->alarms); i++)
661 data->alarms[i] = w83795_read(client, W83795_REG_ALARM(i));
663 data->last_updated = jiffies;
667 mutex_unlock(&data->update_lock);
675 #define ALARM_STATUS 0
676 #define BEEP_ENABLE 1
678 show_alarm_beep(struct device *dev, struct device_attribute *attr, char *buf)
680 struct w83795_data *data = w83795_update_device(dev);
681 struct sensor_device_attribute_2 *sensor_attr =
682 to_sensor_dev_attr_2(attr);
683 int nr = sensor_attr->nr;
684 int index = sensor_attr->index >> 3;
685 int bit = sensor_attr->index & 0x07;
688 if (nr == ALARM_STATUS)
689 val = (data->alarms[index] >> bit) & 1;
690 else /* BEEP_ENABLE */
691 val = (data->beeps[index] >> bit) & 1;
693 return sprintf(buf, "%u\n", val);
697 store_beep(struct device *dev, struct device_attribute *attr,
698 const char *buf, size_t count)
700 struct i2c_client *client = to_i2c_client(dev);
701 struct w83795_data *data = i2c_get_clientdata(client);
702 struct sensor_device_attribute_2 *sensor_attr =
703 to_sensor_dev_attr_2(attr);
704 int index = sensor_attr->index >> 3;
705 int shift = sensor_attr->index & 0x07;
706 u8 beep_bit = 1 << shift;
709 if (strict_strtoul(buf, 10, &val) < 0)
711 if (val != 0 && val != 1)
714 mutex_lock(&data->update_lock);
715 data->beeps[index] = w83795_read(client, W83795_REG_BEEP(index));
716 data->beeps[index] &= ~beep_bit;
717 data->beeps[index] |= val << shift;
718 w83795_write(client, W83795_REG_BEEP(index), data->beeps[index]);
719 mutex_unlock(&data->update_lock);
724 /* Write 0 to clear chassis alarm */
726 store_chassis_clear(struct device *dev,
727 struct device_attribute *attr, const char *buf,
730 struct i2c_client *client = to_i2c_client(dev);
731 struct w83795_data *data = i2c_get_clientdata(client);
734 if (strict_strtoul(buf, 10, &val) < 0 || val != 0)
737 mutex_lock(&data->update_lock);
738 val = w83795_read(client, W83795_REG_CLR_CHASSIS);
740 w83795_write(client, W83795_REG_CLR_CHASSIS, val);
741 mutex_unlock(&data->update_lock);
748 show_fan(struct device *dev, struct device_attribute *attr, char *buf)
750 struct sensor_device_attribute_2 *sensor_attr =
751 to_sensor_dev_attr_2(attr);
752 int nr = sensor_attr->nr;
753 int index = sensor_attr->index;
754 struct w83795_data *data = w83795_update_device(dev);
758 val = data->fan[index] & 0x0fff;
760 val = data->fan_min[index] & 0x0fff;
762 return sprintf(buf, "%lu\n", fan_from_reg(val));
766 store_fan_min(struct device *dev, struct device_attribute *attr,
767 const char *buf, size_t count)
769 struct sensor_device_attribute_2 *sensor_attr =
770 to_sensor_dev_attr_2(attr);
771 int index = sensor_attr->index;
772 struct i2c_client *client = to_i2c_client(dev);
773 struct w83795_data *data = i2c_get_clientdata(client);
776 if (strict_strtoul(buf, 10, &val))
778 val = fan_to_reg(val);
780 mutex_lock(&data->update_lock);
781 data->fan_min[index] = val;
782 w83795_write(client, W83795_REG_FAN_MIN_HL(index), (val >> 4) & 0xff);
786 val |= w83795_read(client, W83795_REG_FAN_MIN_LSB(index))
789 val |= w83795_read(client, W83795_REG_FAN_MIN_LSB(index))
792 w83795_write(client, W83795_REG_FAN_MIN_LSB(index), val & 0xff);
793 mutex_unlock(&data->update_lock);
799 show_pwm(struct device *dev, struct device_attribute *attr, char *buf)
801 struct w83795_data *data;
802 struct sensor_device_attribute_2 *sensor_attr =
803 to_sensor_dev_attr_2(attr);
804 int nr = sensor_attr->nr;
805 int index = sensor_attr->index;
808 data = nr == PWM_OUTPUT ? w83795_update_device(dev)
809 : w83795_update_pwm_config(dev);
813 val = time_from_reg(data->pwm[index][nr]);
816 val = pwm_freq_from_reg(data->pwm[index][nr], data->clkin);
819 val = data->pwm[index][nr];
823 return sprintf(buf, "%u\n", val);
827 store_pwm(struct device *dev, struct device_attribute *attr,
828 const char *buf, size_t count)
830 struct i2c_client *client = to_i2c_client(dev);
831 struct w83795_data *data = i2c_get_clientdata(client);
832 struct sensor_device_attribute_2 *sensor_attr =
833 to_sensor_dev_attr_2(attr);
834 int nr = sensor_attr->nr;
835 int index = sensor_attr->index;
838 if (strict_strtoul(buf, 10, &val) < 0)
841 mutex_lock(&data->update_lock);
844 val = time_to_reg(val);
847 val = pwm_freq_to_reg(val, data->clkin);
850 val = SENSORS_LIMIT(val, 0, 0xff);
853 w83795_write(client, W83795_REG_PWM(index, nr), val);
854 data->pwm[index][nr] = val;
855 mutex_unlock(&data->update_lock);
860 show_pwm_enable(struct device *dev, struct device_attribute *attr, char *buf)
862 struct sensor_device_attribute_2 *sensor_attr =
863 to_sensor_dev_attr_2(attr);
864 struct w83795_data *data = w83795_update_pwm_config(dev);
865 int index = sensor_attr->index;
868 /* Speed cruise mode */
869 if (data->pwm_fcms[0] & (1 << index)) {
873 /* Thermal cruise or SmartFan IV mode */
874 for (tmp = 0; tmp < 6; tmp++) {
875 if (data->pwm_tfmr[tmp] & (1 << index)) {
884 return sprintf(buf, "%u\n", tmp);
888 store_pwm_enable(struct device *dev, struct device_attribute *attr,
889 const char *buf, size_t count)
891 struct i2c_client *client = to_i2c_client(dev);
892 struct w83795_data *data = w83795_update_pwm_config(dev);
893 struct sensor_device_attribute_2 *sensor_attr =
894 to_sensor_dev_attr_2(attr);
895 int index = sensor_attr->index;
899 if (strict_strtoul(buf, 10, &val) < 0)
901 if (val < 1 || val > 2)
904 mutex_lock(&data->update_lock);
907 /* Clear speed cruise mode bits */
908 data->pwm_fcms[0] &= ~(1 << index);
909 w83795_write(client, W83795_REG_FCMS1, data->pwm_fcms[0]);
910 /* Clear thermal cruise mode bits */
911 for (i = 0; i < 6; i++) {
912 data->pwm_tfmr[i] &= ~(1 << index);
913 w83795_write(client, W83795_REG_TFMR(i),
918 data->pwm_fcms[0] |= (1 << index);
919 w83795_write(client, W83795_REG_FCMS1, data->pwm_fcms[0]);
922 mutex_unlock(&data->update_lock);
927 show_pwm_mode(struct device *dev, struct device_attribute *attr, char *buf)
929 struct w83795_data *data = w83795_update_pwm_config(dev);
930 int index = to_sensor_dev_attr_2(attr)->index;
933 if (data->pwm_fomc & (1 << index))
938 return sprintf(buf, "%u\n", mode);
942 * Check whether a given temperature source can ever be useful.
943 * Returns the number of selectable temperature channels which are
946 static int w83795_tss_useful(const struct w83795_data *data, int tsrc)
950 for (i = 0; i < 4; i++) {
951 if (tss_map[i][tsrc] == TSS_MAP_RESERVED)
953 if (tss_map[i][tsrc] < 6) /* Analog */
954 useful += (data->has_temp >> tss_map[i][tsrc]) & 1;
956 useful += (data->has_dts >> (tss_map[i][tsrc] - 6)) & 1;
963 show_temp_src(struct device *dev, struct device_attribute *attr, char *buf)
965 struct sensor_device_attribute_2 *sensor_attr =
966 to_sensor_dev_attr_2(attr);
967 struct w83795_data *data = w83795_update_pwm_config(dev);
968 int index = sensor_attr->index;
969 u8 tmp = data->temp_src[index / 2];
972 tmp >>= 4; /* Pick high nibble */
974 tmp &= 0x0f; /* Pick low nibble */
976 /* Look-up the actual temperature channel number */
977 if (tmp >= 4 || tss_map[tmp][index] == TSS_MAP_RESERVED)
978 return -EINVAL; /* Shouldn't happen */
980 return sprintf(buf, "%u\n", (unsigned int)tss_map[tmp][index] + 1);
984 store_temp_src(struct device *dev, struct device_attribute *attr,
985 const char *buf, size_t count)
987 struct i2c_client *client = to_i2c_client(dev);
988 struct w83795_data *data = w83795_update_pwm_config(dev);
989 struct sensor_device_attribute_2 *sensor_attr =
990 to_sensor_dev_attr_2(attr);
991 int index = sensor_attr->index;
993 unsigned long channel;
996 if (strict_strtoul(buf, 10, &channel) < 0 ||
997 channel < 1 || channel > 14)
1000 /* Check if request can be fulfilled */
1001 for (tmp = 0; tmp < 4; tmp++) {
1002 if (tss_map[tmp][index] == channel - 1)
1005 if (tmp == 4) /* No match */
1008 mutex_lock(&data->update_lock);
1011 data->temp_src[val] &= 0x0f;
1013 data->temp_src[val] &= 0xf0;
1015 data->temp_src[val] |= tmp;
1016 w83795_write(client, W83795_REG_TSS(val), data->temp_src[val]);
1017 mutex_unlock(&data->update_lock);
1022 #define TEMP_PWM_ENABLE 0
1023 #define TEMP_PWM_FAN_MAP 1
1025 show_temp_pwm_enable(struct device *dev, struct device_attribute *attr,
1028 struct w83795_data *data = w83795_update_pwm_config(dev);
1029 struct sensor_device_attribute_2 *sensor_attr =
1030 to_sensor_dev_attr_2(attr);
1031 int nr = sensor_attr->nr;
1032 int index = sensor_attr->index;
1036 case TEMP_PWM_ENABLE:
1037 tmp = (data->pwm_fcms[1] >> index) & 1;
1043 case TEMP_PWM_FAN_MAP:
1044 tmp = data->pwm_tfmr[index];
1048 return sprintf(buf, "%u\n", tmp);
1052 store_temp_pwm_enable(struct device *dev, struct device_attribute *attr,
1053 const char *buf, size_t count)
1055 struct i2c_client *client = to_i2c_client(dev);
1056 struct w83795_data *data = w83795_update_pwm_config(dev);
1057 struct sensor_device_attribute_2 *sensor_attr =
1058 to_sensor_dev_attr_2(attr);
1059 int nr = sensor_attr->nr;
1060 int index = sensor_attr->index;
1063 if (strict_strtoul(buf, 10, &tmp) < 0)
1067 case TEMP_PWM_ENABLE:
1068 if (tmp != 3 && tmp != 4)
1071 mutex_lock(&data->update_lock);
1072 data->pwm_fcms[1] &= ~(1 << index);
1073 data->pwm_fcms[1] |= tmp << index;
1074 w83795_write(client, W83795_REG_FCMS2, data->pwm_fcms[1]);
1075 mutex_unlock(&data->update_lock);
1077 case TEMP_PWM_FAN_MAP:
1078 mutex_lock(&data->update_lock);
1079 tmp = SENSORS_LIMIT(tmp, 0, 0xff);
1080 w83795_write(client, W83795_REG_TFMR(index), tmp);
1081 data->pwm_tfmr[index] = tmp;
1082 mutex_unlock(&data->update_lock);
1088 #define FANIN_TARGET 0
1091 show_fanin(struct device *dev, struct device_attribute *attr, char *buf)
1093 struct w83795_data *data = w83795_update_pwm_config(dev);
1094 struct sensor_device_attribute_2 *sensor_attr =
1095 to_sensor_dev_attr_2(attr);
1096 int nr = sensor_attr->nr;
1097 int index = sensor_attr->index;
1102 tmp = fan_from_reg(data->target_speed[index]);
1105 tmp = data->tol_speed;
1109 return sprintf(buf, "%u\n", tmp);
1113 store_fanin(struct device *dev, struct device_attribute *attr,
1114 const char *buf, size_t count)
1116 struct i2c_client *client = to_i2c_client(dev);
1117 struct w83795_data *data = i2c_get_clientdata(client);
1118 struct sensor_device_attribute_2 *sensor_attr =
1119 to_sensor_dev_attr_2(attr);
1120 int nr = sensor_attr->nr;
1121 int index = sensor_attr->index;
1124 if (strict_strtoul(buf, 10, &val) < 0)
1127 mutex_lock(&data->update_lock);
1130 val = fan_to_reg(SENSORS_LIMIT(val, 0, 0xfff));
1131 w83795_write(client, W83795_REG_FTSH(index), val >> 4);
1132 w83795_write(client, W83795_REG_FTSL(index), (val << 4) & 0xf0);
1133 data->target_speed[index] = val;
1136 val = SENSORS_LIMIT(val, 0, 0x3f);
1137 w83795_write(client, W83795_REG_TFTS, val);
1138 data->tol_speed = val;
1141 mutex_unlock(&data->update_lock);
1148 show_temp_pwm(struct device *dev, struct device_attribute *attr, char *buf)
1150 struct w83795_data *data = w83795_update_pwm_config(dev);
1151 struct sensor_device_attribute_2 *sensor_attr =
1152 to_sensor_dev_attr_2(attr);
1153 int nr = sensor_attr->nr;
1154 int index = sensor_attr->index;
1155 long tmp = temp_from_reg(data->pwm_temp[index][nr]);
1157 return sprintf(buf, "%ld\n", tmp);
1161 store_temp_pwm(struct device *dev, struct device_attribute *attr,
1162 const char *buf, size_t count)
1164 struct i2c_client *client = to_i2c_client(dev);
1165 struct w83795_data *data = i2c_get_clientdata(client);
1166 struct sensor_device_attribute_2 *sensor_attr =
1167 to_sensor_dev_attr_2(attr);
1168 int nr = sensor_attr->nr;
1169 int index = sensor_attr->index;
1173 if (strict_strtoul(buf, 10, &val) < 0)
1177 mutex_lock(&data->update_lock);
1180 val = SENSORS_LIMIT(val, 0, 0x7f);
1181 w83795_write(client, W83795_REG_TTTI(index), val);
1184 val = SENSORS_LIMIT(val, 0, 0x7f);
1185 w83795_write(client, W83795_REG_CTFS(index), val);
1188 val = SENSORS_LIMIT(val, 0, 0x0f);
1189 tmp = w83795_read(client, W83795_REG_HT(index));
1191 tmp |= (val << 4) & 0xf0;
1192 w83795_write(client, W83795_REG_HT(index), tmp);
1195 val = SENSORS_LIMIT(val, 0, 0x0f);
1196 tmp = w83795_read(client, W83795_REG_HT(index));
1199 w83795_write(client, W83795_REG_HT(index), tmp);
1202 data->pwm_temp[index][nr] = val;
1203 mutex_unlock(&data->update_lock);
1209 show_sf4_pwm(struct device *dev, struct device_attribute *attr, char *buf)
1211 struct w83795_data *data = w83795_update_pwm_config(dev);
1212 struct sensor_device_attribute_2 *sensor_attr =
1213 to_sensor_dev_attr_2(attr);
1214 int nr = sensor_attr->nr;
1215 int index = sensor_attr->index;
1217 return sprintf(buf, "%u\n", data->sf4_reg[index][SF4_PWM][nr]);
1221 store_sf4_pwm(struct device *dev, struct device_attribute *attr,
1222 const char *buf, size_t count)
1224 struct i2c_client *client = to_i2c_client(dev);
1225 struct w83795_data *data = i2c_get_clientdata(client);
1226 struct sensor_device_attribute_2 *sensor_attr =
1227 to_sensor_dev_attr_2(attr);
1228 int nr = sensor_attr->nr;
1229 int index = sensor_attr->index;
1232 if (strict_strtoul(buf, 10, &val) < 0)
1235 mutex_lock(&data->update_lock);
1236 w83795_write(client, W83795_REG_SF4_PWM(index, nr), val);
1237 data->sf4_reg[index][SF4_PWM][nr] = val;
1238 mutex_unlock(&data->update_lock);
1244 show_sf4_temp(struct device *dev, struct device_attribute *attr, char *buf)
1246 struct w83795_data *data = w83795_update_pwm_config(dev);
1247 struct sensor_device_attribute_2 *sensor_attr =
1248 to_sensor_dev_attr_2(attr);
1249 int nr = sensor_attr->nr;
1250 int index = sensor_attr->index;
1252 return sprintf(buf, "%u\n",
1253 (data->sf4_reg[index][SF4_TEMP][nr]) * 1000);
1257 store_sf4_temp(struct device *dev, struct device_attribute *attr,
1258 const char *buf, size_t count)
1260 struct i2c_client *client = to_i2c_client(dev);
1261 struct w83795_data *data = i2c_get_clientdata(client);
1262 struct sensor_device_attribute_2 *sensor_attr =
1263 to_sensor_dev_attr_2(attr);
1264 int nr = sensor_attr->nr;
1265 int index = sensor_attr->index;
1268 if (strict_strtoul(buf, 10, &val) < 0)
1272 mutex_lock(&data->update_lock);
1273 w83795_write(client, W83795_REG_SF4_TEMP(index, nr), val);
1274 data->sf4_reg[index][SF4_TEMP][nr] = val;
1275 mutex_unlock(&data->update_lock);
1282 show_temp(struct device *dev, struct device_attribute *attr, char *buf)
1284 struct sensor_device_attribute_2 *sensor_attr =
1285 to_sensor_dev_attr_2(attr);
1286 int nr = sensor_attr->nr;
1287 int index = sensor_attr->index;
1288 struct w83795_data *data = w83795_update_device(dev);
1289 long temp = temp_from_reg(data->temp[index][nr]);
1291 if (nr == TEMP_READ)
1292 temp += (data->temp_read_vrlsb[index] >> 6) * 250;
1293 return sprintf(buf, "%ld\n", temp);
1297 store_temp(struct device *dev, struct device_attribute *attr,
1298 const char *buf, size_t count)
1300 struct sensor_device_attribute_2 *sensor_attr =
1301 to_sensor_dev_attr_2(attr);
1302 int nr = sensor_attr->nr;
1303 int index = sensor_attr->index;
1304 struct i2c_client *client = to_i2c_client(dev);
1305 struct w83795_data *data = i2c_get_clientdata(client);
1308 if (strict_strtol(buf, 10, &tmp) < 0)
1311 mutex_lock(&data->update_lock);
1312 data->temp[index][nr] = temp_to_reg(tmp, -128, 127);
1313 w83795_write(client, W83795_REG_TEMP[index][nr], data->temp[index][nr]);
1314 mutex_unlock(&data->update_lock);
1320 show_dts_mode(struct device *dev, struct device_attribute *attr, char *buf)
1322 struct w83795_data *data = dev_get_drvdata(dev);
1325 if (data->enable_dts & 2)
1330 return sprintf(buf, "%d\n", tmp);
1334 show_dts(struct device *dev, struct device_attribute *attr, char *buf)
1336 struct sensor_device_attribute_2 *sensor_attr =
1337 to_sensor_dev_attr_2(attr);
1338 int index = sensor_attr->index;
1339 struct w83795_data *data = w83795_update_device(dev);
1340 long temp = temp_from_reg(data->dts[index]);
1342 temp += (data->dts_read_vrlsb[index] >> 6) * 250;
1343 return sprintf(buf, "%ld\n", temp);
1347 show_dts_ext(struct device *dev, struct device_attribute *attr, char *buf)
1349 struct sensor_device_attribute_2 *sensor_attr =
1350 to_sensor_dev_attr_2(attr);
1351 int nr = sensor_attr->nr;
1352 struct w83795_data *data = dev_get_drvdata(dev);
1353 long temp = temp_from_reg(data->dts_ext[nr]);
1355 return sprintf(buf, "%ld\n", temp);
1359 store_dts_ext(struct device *dev, struct device_attribute *attr,
1360 const char *buf, size_t count)
1362 struct sensor_device_attribute_2 *sensor_attr =
1363 to_sensor_dev_attr_2(attr);
1364 int nr = sensor_attr->nr;
1365 struct i2c_client *client = to_i2c_client(dev);
1366 struct w83795_data *data = i2c_get_clientdata(client);
1369 if (strict_strtol(buf, 10, &tmp) < 0)
1372 mutex_lock(&data->update_lock);
1373 data->dts_ext[nr] = temp_to_reg(tmp, -128, 127);
1374 w83795_write(client, W83795_REG_DTS_EXT(nr), data->dts_ext[nr]);
1375 mutex_unlock(&data->update_lock);
1381 show_temp_mode(struct device *dev, struct device_attribute *attr, char *buf)
1383 struct w83795_data *data = dev_get_drvdata(dev);
1384 struct sensor_device_attribute_2 *sensor_attr =
1385 to_sensor_dev_attr_2(attr);
1386 int index = sensor_attr->index;
1389 if (data->temp_mode & (1 << index))
1390 tmp = 3; /* Thermal diode */
1392 tmp = 4; /* Thermistor */
1394 return sprintf(buf, "%d\n", tmp);
1397 /* Only for temp1-4 (temp5-6 can only be thermistor) */
1399 store_temp_mode(struct device *dev, struct device_attribute *attr,
1400 const char *buf, size_t count)
1402 struct i2c_client *client = to_i2c_client(dev);
1403 struct w83795_data *data = i2c_get_clientdata(client);
1404 struct sensor_device_attribute_2 *sensor_attr =
1405 to_sensor_dev_attr_2(attr);
1406 int index = sensor_attr->index;
1411 if (strict_strtoul(buf, 10, &val) < 0)
1413 if ((val != 4) && (val != 3))
1416 mutex_lock(&data->update_lock);
1420 data->temp_mode |= 1 << index;
1421 } else if (val == 4) {
1424 data->temp_mode &= ~(1 << index);
1427 reg_shift = 2 * index;
1428 tmp = w83795_read(client, W83795_REG_TEMP_CTRL2);
1429 tmp &= ~(0x03 << reg_shift);
1430 tmp |= val << reg_shift;
1431 w83795_write(client, W83795_REG_TEMP_CTRL2, tmp);
1433 mutex_unlock(&data->update_lock);
1438 /* show/store VIN */
1440 show_in(struct device *dev, struct device_attribute *attr, char *buf)
1442 struct sensor_device_attribute_2 *sensor_attr =
1443 to_sensor_dev_attr_2(attr);
1444 int nr = sensor_attr->nr;
1445 int index = sensor_attr->index;
1446 struct w83795_data *data = w83795_update_device(dev);
1447 u16 val = data->in[index][nr];
1452 /* calculate this value again by sensors as sensors3.conf */
1453 if ((index >= 17) &&
1454 !((data->has_gain >> (index - 17)) & 1))
1459 lsb_idx = IN_LSB_SHIFT_IDX[index][IN_LSB_IDX];
1461 val |= (data->in_lsb[lsb_idx][nr] >>
1462 IN_LSB_SHIFT_IDX[index][IN_LSB_SHIFT]) & 0x03;
1463 if ((index >= 17) &&
1464 !((data->has_gain >> (index - 17)) & 1))
1468 val = in_from_reg(index, val);
1470 return sprintf(buf, "%d\n", val);
1474 store_in(struct device *dev, struct device_attribute *attr,
1475 const char *buf, size_t count)
1477 struct sensor_device_attribute_2 *sensor_attr =
1478 to_sensor_dev_attr_2(attr);
1479 int nr = sensor_attr->nr;
1480 int index = sensor_attr->index;
1481 struct i2c_client *client = to_i2c_client(dev);
1482 struct w83795_data *data = i2c_get_clientdata(client);
1487 if (strict_strtoul(buf, 10, &val) < 0)
1489 val = in_to_reg(index, val);
1491 if ((index >= 17) &&
1492 !((data->has_gain >> (index - 17)) & 1))
1494 val = SENSORS_LIMIT(val, 0, 0x3FF);
1495 mutex_lock(&data->update_lock);
1497 lsb_idx = IN_LSB_SHIFT_IDX[index][IN_LSB_IDX];
1498 tmp = w83795_read(client, IN_LSB_REG(lsb_idx, nr));
1499 tmp &= ~(0x03 << IN_LSB_SHIFT_IDX[index][IN_LSB_SHIFT]);
1500 tmp |= (val & 0x03) << IN_LSB_SHIFT_IDX[index][IN_LSB_SHIFT];
1501 w83795_write(client, IN_LSB_REG(lsb_idx, nr), tmp);
1502 data->in_lsb[lsb_idx][nr] = tmp;
1504 tmp = (val >> 2) & 0xff;
1505 w83795_write(client, W83795_REG_IN[index][nr], tmp);
1506 data->in[index][nr] = tmp;
1508 mutex_unlock(&data->update_lock);
1513 #ifdef CONFIG_SENSORS_W83795_FANCTRL
1515 show_sf_setup(struct device *dev, struct device_attribute *attr, char *buf)
1517 struct sensor_device_attribute_2 *sensor_attr =
1518 to_sensor_dev_attr_2(attr);
1519 int nr = sensor_attr->nr;
1520 struct w83795_data *data = w83795_update_pwm_config(dev);
1521 u16 val = data->setup_pwm[nr];
1524 case SETUP_PWM_UPTIME:
1525 case SETUP_PWM_DOWNTIME:
1526 val = time_from_reg(val);
1530 return sprintf(buf, "%d\n", val);
1534 store_sf_setup(struct device *dev, struct device_attribute *attr,
1535 const char *buf, size_t count)
1537 struct sensor_device_attribute_2 *sensor_attr =
1538 to_sensor_dev_attr_2(attr);
1539 int nr = sensor_attr->nr;
1540 struct i2c_client *client = to_i2c_client(dev);
1541 struct w83795_data *data = i2c_get_clientdata(client);
1544 if (strict_strtoul(buf, 10, &val) < 0)
1548 case SETUP_PWM_DEFAULT:
1549 val = SENSORS_LIMIT(val, 0, 0xff);
1551 case SETUP_PWM_UPTIME:
1552 case SETUP_PWM_DOWNTIME:
1553 val = time_to_reg(val);
1559 mutex_lock(&data->update_lock);
1560 data->setup_pwm[nr] = val;
1561 w83795_write(client, W83795_REG_SETUP_PWM(nr), val);
1562 mutex_unlock(&data->update_lock);
1570 /* Don't change the attribute order, _max and _min are accessed by index
1571 * somewhere else in the code */
1572 #define SENSOR_ATTR_IN(index) { \
1573 SENSOR_ATTR_2(in##index##_input, S_IRUGO, show_in, NULL, \
1575 SENSOR_ATTR_2(in##index##_max, S_IRUGO | S_IWUSR, show_in, \
1576 store_in, IN_MAX, index), \
1577 SENSOR_ATTR_2(in##index##_min, S_IRUGO | S_IWUSR, show_in, \
1578 store_in, IN_LOW, index), \
1579 SENSOR_ATTR_2(in##index##_alarm, S_IRUGO, show_alarm_beep, \
1580 NULL, ALARM_STATUS, index + ((index > 14) ? 1 : 0)), \
1581 SENSOR_ATTR_2(in##index##_beep, S_IWUSR | S_IRUGO, \
1582 show_alarm_beep, store_beep, BEEP_ENABLE, \
1583 index + ((index > 14) ? 1 : 0)) }
1585 #define SENSOR_ATTR_FAN(index) { \
1586 SENSOR_ATTR_2(fan##index##_input, S_IRUGO, show_fan, \
1587 NULL, FAN_INPUT, index - 1), \
1588 SENSOR_ATTR_2(fan##index##_min, S_IWUSR | S_IRUGO, \
1589 show_fan, store_fan_min, FAN_MIN, index - 1), \
1590 SENSOR_ATTR_2(fan##index##_alarm, S_IRUGO, show_alarm_beep, \
1591 NULL, ALARM_STATUS, index + 31), \
1592 SENSOR_ATTR_2(fan##index##_beep, S_IWUSR | S_IRUGO, \
1593 show_alarm_beep, store_beep, BEEP_ENABLE, index + 31) }
1595 #define SENSOR_ATTR_PWM(index) { \
1596 SENSOR_ATTR_2(pwm##index, S_IWUSR | S_IRUGO, show_pwm, \
1597 store_pwm, PWM_OUTPUT, index - 1), \
1598 SENSOR_ATTR_2(pwm##index##_nonstop, S_IWUSR | S_IRUGO, \
1599 show_pwm, store_pwm, PWM_NONSTOP, index - 1), \
1600 SENSOR_ATTR_2(pwm##index##_start, S_IWUSR | S_IRUGO, \
1601 show_pwm, store_pwm, PWM_START, index - 1), \
1602 SENSOR_ATTR_2(pwm##index##_stop_time, S_IWUSR | S_IRUGO, \
1603 show_pwm, store_pwm, PWM_STOP_TIME, index - 1), \
1604 SENSOR_ATTR_2(pwm##index##_freq, S_IWUSR | S_IRUGO, \
1605 show_pwm, store_pwm, PWM_FREQ, index - 1), \
1606 SENSOR_ATTR_2(pwm##index##_enable, S_IWUSR | S_IRUGO, \
1607 show_pwm_enable, store_pwm_enable, NOT_USED, index - 1), \
1608 SENSOR_ATTR_2(pwm##index##_mode, S_IRUGO, \
1609 show_pwm_mode, NULL, NOT_USED, index - 1), \
1610 SENSOR_ATTR_2(fan##index##_target, S_IWUSR | S_IRUGO, \
1611 show_fanin, store_fanin, FANIN_TARGET, index - 1) }
1613 #define SENSOR_ATTR_DTS(index) { \
1614 SENSOR_ATTR_2(temp##index##_type, S_IRUGO , \
1615 show_dts_mode, NULL, NOT_USED, index - 7), \
1616 SENSOR_ATTR_2(temp##index##_input, S_IRUGO, show_dts, \
1617 NULL, NOT_USED, index - 7), \
1618 SENSOR_ATTR_2(temp##index##_crit, S_IRUGO | S_IWUSR, show_dts_ext, \
1619 store_dts_ext, DTS_CRIT, NOT_USED), \
1620 SENSOR_ATTR_2(temp##index##_crit_hyst, S_IRUGO | S_IWUSR, \
1621 show_dts_ext, store_dts_ext, DTS_CRIT_HYST, NOT_USED), \
1622 SENSOR_ATTR_2(temp##index##_max, S_IRUGO | S_IWUSR, show_dts_ext, \
1623 store_dts_ext, DTS_WARN, NOT_USED), \
1624 SENSOR_ATTR_2(temp##index##_max_hyst, S_IRUGO | S_IWUSR, \
1625 show_dts_ext, store_dts_ext, DTS_WARN_HYST, NOT_USED), \
1626 SENSOR_ATTR_2(temp##index##_alarm, S_IRUGO, \
1627 show_alarm_beep, NULL, ALARM_STATUS, index + 17), \
1628 SENSOR_ATTR_2(temp##index##_beep, S_IWUSR | S_IRUGO, \
1629 show_alarm_beep, store_beep, BEEP_ENABLE, index + 17) }
1631 #define SENSOR_ATTR_TEMP(index) { \
1632 SENSOR_ATTR_2(temp##index##_type, S_IRUGO | (index < 4 ? S_IWUSR : 0), \
1633 show_temp_mode, store_temp_mode, NOT_USED, index - 1), \
1634 SENSOR_ATTR_2(temp##index##_input, S_IRUGO, show_temp, \
1635 NULL, TEMP_READ, index - 1), \
1636 SENSOR_ATTR_2(temp##index##_crit, S_IRUGO | S_IWUSR, show_temp, \
1637 store_temp, TEMP_CRIT, index - 1), \
1638 SENSOR_ATTR_2(temp##index##_crit_hyst, S_IRUGO | S_IWUSR, \
1639 show_temp, store_temp, TEMP_CRIT_HYST, index - 1), \
1640 SENSOR_ATTR_2(temp##index##_max, S_IRUGO | S_IWUSR, show_temp, \
1641 store_temp, TEMP_WARN, index - 1), \
1642 SENSOR_ATTR_2(temp##index##_max_hyst, S_IRUGO | S_IWUSR, \
1643 show_temp, store_temp, TEMP_WARN_HYST, index - 1), \
1644 SENSOR_ATTR_2(temp##index##_alarm, S_IRUGO, \
1645 show_alarm_beep, NULL, ALARM_STATUS, \
1646 index + (index > 4 ? 11 : 17)), \
1647 SENSOR_ATTR_2(temp##index##_beep, S_IWUSR | S_IRUGO, \
1648 show_alarm_beep, store_beep, BEEP_ENABLE, \
1649 index + (index > 4 ? 11 : 17)), \
1650 SENSOR_ATTR_2(temp##index##_pwm_enable, S_IWUSR | S_IRUGO, \
1651 show_temp_pwm_enable, store_temp_pwm_enable, \
1652 TEMP_PWM_ENABLE, index - 1), \
1653 SENSOR_ATTR_2(temp##index##_auto_channels_pwm, S_IWUSR | S_IRUGO, \
1654 show_temp_pwm_enable, store_temp_pwm_enable, \
1655 TEMP_PWM_FAN_MAP, index - 1), \
1656 SENSOR_ATTR_2(thermal_cruise##index, S_IWUSR | S_IRUGO, \
1657 show_temp_pwm, store_temp_pwm, TEMP_PWM_TTTI, index - 1), \
1658 SENSOR_ATTR_2(temp##index##_warn, S_IWUSR | S_IRUGO, \
1659 show_temp_pwm, store_temp_pwm, TEMP_PWM_CTFS, index - 1), \
1660 SENSOR_ATTR_2(temp##index##_warn_hyst, S_IWUSR | S_IRUGO, \
1661 show_temp_pwm, store_temp_pwm, TEMP_PWM_HCT, index - 1), \
1662 SENSOR_ATTR_2(temp##index##_operation_hyst, S_IWUSR | S_IRUGO, \
1663 show_temp_pwm, store_temp_pwm, TEMP_PWM_HOT, index - 1), \
1664 SENSOR_ATTR_2(temp##index##_auto_point1_pwm, S_IRUGO | S_IWUSR, \
1665 show_sf4_pwm, store_sf4_pwm, 0, index - 1), \
1666 SENSOR_ATTR_2(temp##index##_auto_point2_pwm, S_IRUGO | S_IWUSR, \
1667 show_sf4_pwm, store_sf4_pwm, 1, index - 1), \
1668 SENSOR_ATTR_2(temp##index##_auto_point3_pwm, S_IRUGO | S_IWUSR, \
1669 show_sf4_pwm, store_sf4_pwm, 2, index - 1), \
1670 SENSOR_ATTR_2(temp##index##_auto_point4_pwm, S_IRUGO | S_IWUSR, \
1671 show_sf4_pwm, store_sf4_pwm, 3, index - 1), \
1672 SENSOR_ATTR_2(temp##index##_auto_point5_pwm, S_IRUGO | S_IWUSR, \
1673 show_sf4_pwm, store_sf4_pwm, 4, index - 1), \
1674 SENSOR_ATTR_2(temp##index##_auto_point6_pwm, S_IRUGO | S_IWUSR, \
1675 show_sf4_pwm, store_sf4_pwm, 5, index - 1), \
1676 SENSOR_ATTR_2(temp##index##_auto_point7_pwm, S_IRUGO | S_IWUSR, \
1677 show_sf4_pwm, store_sf4_pwm, 6, index - 1), \
1678 SENSOR_ATTR_2(temp##index##_auto_point1_temp, S_IRUGO | S_IWUSR,\
1679 show_sf4_temp, store_sf4_temp, 0, index - 1), \
1680 SENSOR_ATTR_2(temp##index##_auto_point2_temp, S_IRUGO | S_IWUSR,\
1681 show_sf4_temp, store_sf4_temp, 1, index - 1), \
1682 SENSOR_ATTR_2(temp##index##_auto_point3_temp, S_IRUGO | S_IWUSR,\
1683 show_sf4_temp, store_sf4_temp, 2, index - 1), \
1684 SENSOR_ATTR_2(temp##index##_auto_point4_temp, S_IRUGO | S_IWUSR,\
1685 show_sf4_temp, store_sf4_temp, 3, index - 1), \
1686 SENSOR_ATTR_2(temp##index##_auto_point5_temp, S_IRUGO | S_IWUSR,\
1687 show_sf4_temp, store_sf4_temp, 4, index - 1), \
1688 SENSOR_ATTR_2(temp##index##_auto_point6_temp, S_IRUGO | S_IWUSR,\
1689 show_sf4_temp, store_sf4_temp, 5, index - 1), \
1690 SENSOR_ATTR_2(temp##index##_auto_point7_temp, S_IRUGO | S_IWUSR,\
1691 show_sf4_temp, store_sf4_temp, 6, index - 1) }
1694 static struct sensor_device_attribute_2 w83795_in[][5] = {
1718 static const struct sensor_device_attribute_2 w83795_fan[][4] = {
1728 SENSOR_ATTR_FAN(10),
1729 SENSOR_ATTR_FAN(11),
1730 SENSOR_ATTR_FAN(12),
1731 SENSOR_ATTR_FAN(13),
1732 SENSOR_ATTR_FAN(14),
1735 static const struct sensor_device_attribute_2 w83795_temp[][28] = {
1736 SENSOR_ATTR_TEMP(1),
1737 SENSOR_ATTR_TEMP(2),
1738 SENSOR_ATTR_TEMP(3),
1739 SENSOR_ATTR_TEMP(4),
1740 SENSOR_ATTR_TEMP(5),
1741 SENSOR_ATTR_TEMP(6),
1744 static const struct sensor_device_attribute_2 w83795_dts[][8] = {
1748 SENSOR_ATTR_DTS(10),
1749 SENSOR_ATTR_DTS(11),
1750 SENSOR_ATTR_DTS(12),
1751 SENSOR_ATTR_DTS(13),
1752 SENSOR_ATTR_DTS(14),
1755 static const struct sensor_device_attribute_2 w83795_pwm[][8] = {
1766 static const struct sensor_device_attribute_2 w83795_tss[6] = {
1767 SENSOR_ATTR_2(temp1_source_sel, S_IWUSR | S_IRUGO,
1768 show_temp_src, store_temp_src, NOT_USED, 0),
1769 SENSOR_ATTR_2(temp2_source_sel, S_IWUSR | S_IRUGO,
1770 show_temp_src, store_temp_src, NOT_USED, 1),
1771 SENSOR_ATTR_2(temp3_source_sel, S_IWUSR | S_IRUGO,
1772 show_temp_src, store_temp_src, NOT_USED, 2),
1773 SENSOR_ATTR_2(temp4_source_sel, S_IWUSR | S_IRUGO,
1774 show_temp_src, store_temp_src, NOT_USED, 3),
1775 SENSOR_ATTR_2(temp5_source_sel, S_IWUSR | S_IRUGO,
1776 show_temp_src, store_temp_src, NOT_USED, 4),
1777 SENSOR_ATTR_2(temp6_source_sel, S_IWUSR | S_IRUGO,
1778 show_temp_src, store_temp_src, NOT_USED, 5),
1781 static const struct sensor_device_attribute_2 sda_single_files[] = {
1782 SENSOR_ATTR_2(intrusion0_alarm, S_IWUSR | S_IRUGO, show_alarm_beep,
1783 store_chassis_clear, ALARM_STATUS, 46),
1784 SENSOR_ATTR_2(intrusion0_beep, S_IWUSR | S_IRUGO, show_alarm_beep,
1785 store_beep, BEEP_ENABLE, 46),
1786 SENSOR_ATTR_2(beep_enable, S_IWUSR | S_IRUGO, show_alarm_beep,
1787 store_beep, BEEP_ENABLE, 47),
1788 #ifdef CONFIG_SENSORS_W83795_FANCTRL
1789 SENSOR_ATTR_2(speed_cruise_tolerance, S_IWUSR | S_IRUGO, show_fanin,
1790 store_fanin, FANIN_TOL, NOT_USED),
1791 SENSOR_ATTR_2(pwm_default, S_IWUSR | S_IRUGO, show_sf_setup,
1792 store_sf_setup, SETUP_PWM_DEFAULT, NOT_USED),
1793 SENSOR_ATTR_2(pwm_uptime, S_IWUSR | S_IRUGO, show_sf_setup,
1794 store_sf_setup, SETUP_PWM_UPTIME, NOT_USED),
1795 SENSOR_ATTR_2(pwm_downtime, S_IWUSR | S_IRUGO, show_sf_setup,
1796 store_sf_setup, SETUP_PWM_DOWNTIME, NOT_USED),
1804 static void w83795_init_client(struct i2c_client *client)
1806 struct w83795_data *data = i2c_get_clientdata(client);
1807 static const u16 clkin[4] = { /* in kHz */
1808 14318, 24000, 33333, 48000
1813 w83795_write(client, W83795_REG_CONFIG, 0x80);
1815 /* Start monitoring if needed */
1816 config = w83795_read(client, W83795_REG_CONFIG);
1817 if (!(config & W83795_REG_CONFIG_START)) {
1818 dev_info(&client->dev, "Enabling monitoring operations\n");
1819 w83795_write(client, W83795_REG_CONFIG,
1820 config | W83795_REG_CONFIG_START);
1823 data->clkin = clkin[(config >> 3) & 0x3];
1824 dev_dbg(&client->dev, "clkin = %u kHz\n", data->clkin);
1827 static int w83795_get_device_id(struct i2c_client *client)
1831 device_id = i2c_smbus_read_byte_data(client, W83795_REG_DEVICEID);
1833 /* Special case for rev. A chips; can't be checked first because later
1834 revisions emulate this for compatibility */
1835 if (device_id < 0 || (device_id & 0xf0) != 0x50) {
1838 alt_id = i2c_smbus_read_byte_data(client,
1839 W83795_REG_DEVICEID_A);
1847 /* Return 0 if detection is successful, -ENODEV otherwise */
1848 static int w83795_detect(struct i2c_client *client,
1849 struct i2c_board_info *info)
1851 int bank, vendor_id, device_id, expected, i2c_addr, config;
1852 struct i2c_adapter *adapter = client->adapter;
1853 unsigned short address = client->addr;
1854 const char *chip_name;
1856 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1858 bank = i2c_smbus_read_byte_data(client, W83795_REG_BANKSEL);
1859 if (bank < 0 || (bank & 0x7c)) {
1860 dev_dbg(&adapter->dev,
1861 "w83795: Detection failed at addr 0x%02hx, check %s\n",
1866 /* Check Nuvoton vendor ID */
1867 vendor_id = i2c_smbus_read_byte_data(client, W83795_REG_VENDORID);
1868 expected = bank & 0x80 ? 0x5c : 0xa3;
1869 if (vendor_id != expected) {
1870 dev_dbg(&adapter->dev,
1871 "w83795: Detection failed at addr 0x%02hx, check %s\n",
1872 address, "vendor id");
1876 /* Check device ID */
1877 device_id = w83795_get_device_id(client) |
1878 (i2c_smbus_read_byte_data(client, W83795_REG_CHIPID) << 8);
1879 if ((device_id >> 4) != 0x795) {
1880 dev_dbg(&adapter->dev,
1881 "w83795: Detection failed at addr 0x%02hx, check %s\n",
1882 address, "device id\n");
1886 /* If Nuvoton chip, address of chip and W83795_REG_I2C_ADDR
1888 if ((bank & 0x07) == 0) {
1889 i2c_addr = i2c_smbus_read_byte_data(client,
1890 W83795_REG_I2C_ADDR);
1891 if ((i2c_addr & 0x7f) != address) {
1892 dev_dbg(&adapter->dev,
1893 "w83795: Detection failed at addr 0x%02hx, "
1894 "check %s\n", address, "i2c addr");
1899 /* Check 795 chip type: 795G or 795ADG
1900 Usually we don't write to chips during detection, but here we don't
1901 quite have the choice; hopefully it's OK, we are about to return
1903 if ((bank & 0x07) != 0)
1904 i2c_smbus_write_byte_data(client, W83795_REG_BANKSEL,
1906 config = i2c_smbus_read_byte_data(client, W83795_REG_CONFIG);
1907 if (config & W83795_REG_CONFIG_CONFIG48)
1908 chip_name = "w83795adg";
1910 chip_name = "w83795g";
1912 strlcpy(info->type, chip_name, I2C_NAME_SIZE);
1913 dev_info(&adapter->dev, "Found %s rev. %c at 0x%02hx\n", chip_name,
1914 'A' + (device_id & 0xf), address);
1919 static int w83795_handle_files(struct device *dev, int (*fn)(struct device *,
1920 const struct device_attribute *))
1922 struct w83795_data *data = dev_get_drvdata(dev);
1925 for (i = 0; i < ARRAY_SIZE(w83795_in); i++) {
1926 if (!(data->has_in & (1 << i)))
1928 for (j = 0; j < ARRAY_SIZE(w83795_in[0]); j++) {
1929 err = fn(dev, &w83795_in[i][j].dev_attr);
1935 for (i = 0; i < ARRAY_SIZE(w83795_fan); i++) {
1936 if (!(data->has_fan & (1 << i)))
1938 for (j = 0; j < ARRAY_SIZE(w83795_fan[0]); j++) {
1939 err = fn(dev, &w83795_fan[i][j].dev_attr);
1945 for (i = 0; i < ARRAY_SIZE(w83795_tss); i++) {
1946 j = w83795_tss_useful(data, i);
1949 err = fn(dev, &w83795_tss[i].dev_attr);
1954 for (i = 0; i < ARRAY_SIZE(sda_single_files); i++) {
1955 err = fn(dev, &sda_single_files[i].dev_attr);
1960 #ifdef CONFIG_SENSORS_W83795_FANCTRL
1961 for (i = 0; i < data->has_pwm; i++) {
1962 for (j = 0; j < ARRAY_SIZE(w83795_pwm[0]); j++) {
1963 err = fn(dev, &w83795_pwm[i][j].dev_attr);
1970 for (i = 0; i < ARRAY_SIZE(w83795_temp); i++) {
1971 if (!(data->has_temp & (1 << i)))
1973 #ifdef CONFIG_SENSORS_W83795_FANCTRL
1974 for (j = 0; j < ARRAY_SIZE(w83795_temp[0]); j++) {
1976 for (j = 0; j < 8; j++) {
1978 err = fn(dev, &w83795_temp[i][j].dev_attr);
1984 if (data->enable_dts) {
1985 for (i = 0; i < ARRAY_SIZE(w83795_dts); i++) {
1986 if (!(data->has_dts & (1 << i)))
1988 for (j = 0; j < ARRAY_SIZE(w83795_dts[0]); j++) {
1989 err = fn(dev, &w83795_dts[i][j].dev_attr);
1999 /* We need a wrapper that fits in w83795_handle_files */
2000 static int device_remove_file_wrapper(struct device *dev,
2001 const struct device_attribute *attr)
2003 device_remove_file(dev, attr);
2007 static void w83795_check_dynamic_in_limits(struct i2c_client *client)
2009 struct w83795_data *data = i2c_get_clientdata(client);
2011 int i, err_max, err_min;
2013 vid_ctl = w83795_read(client, W83795_REG_VID_CTRL);
2015 /* Return immediately if VRM isn't configured */
2016 if ((vid_ctl & 0x07) == 0x00 || (vid_ctl & 0x07) == 0x07)
2019 data->has_dyn_in = (vid_ctl >> 3) & 0x07;
2020 for (i = 0; i < 2; i++) {
2021 if (!(data->has_dyn_in & (1 << i)))
2024 /* Voltage limits in dynamic mode, switch to read-only */
2025 err_max = sysfs_chmod_file(&client->dev.kobj,
2026 &w83795_in[i][2].dev_attr.attr,
2028 err_min = sysfs_chmod_file(&client->dev.kobj,
2029 &w83795_in[i][3].dev_attr.attr,
2031 if (err_max || err_min)
2032 dev_warn(&client->dev, "Failed to set in%d limits "
2033 "read-only (%d, %d)\n", i, err_max, err_min);
2035 dev_info(&client->dev, "in%d limits set dynamically "
2040 /* Check pins that can be used for either temperature or voltage monitoring */
2041 static void w83795_apply_temp_config(struct w83795_data *data, u8 config,
2042 int temp_chan, int in_chan)
2044 /* config is a 2-bit value */
2046 case 0x2: /* Voltage monitoring */
2047 data->has_in |= 1 << in_chan;
2049 case 0x1: /* Thermal diode */
2052 data->temp_mode |= 1 << temp_chan;
2054 case 0x3: /* Thermistor */
2055 data->has_temp |= 1 << temp_chan;
2060 static int w83795_probe(struct i2c_client *client,
2061 const struct i2c_device_id *id)
2065 struct device *dev = &client->dev;
2066 struct w83795_data *data;
2069 data = kzalloc(sizeof(struct w83795_data), GFP_KERNEL);
2075 i2c_set_clientdata(client, data);
2076 data->chip_type = id->driver_data;
2077 data->bank = i2c_smbus_read_byte_data(client, W83795_REG_BANKSEL);
2078 mutex_init(&data->update_lock);
2080 /* Initialize the chip */
2081 w83795_init_client(client);
2083 /* Check which voltages and fans are present */
2084 data->has_in = w83795_read(client, W83795_REG_VOLT_CTRL1)
2085 | (w83795_read(client, W83795_REG_VOLT_CTRL2) << 8);
2086 data->has_fan = w83795_read(client, W83795_REG_FANIN_CTRL1)
2087 | (w83795_read(client, W83795_REG_FANIN_CTRL2) << 8);
2089 /* Check which analog temperatures and extra voltages are present */
2090 tmp = w83795_read(client, W83795_REG_TEMP_CTRL1);
2092 data->enable_dts = 1;
2093 w83795_apply_temp_config(data, (tmp >> 2) & 0x3, 5, 16);
2094 w83795_apply_temp_config(data, tmp & 0x3, 4, 15);
2095 tmp = w83795_read(client, W83795_REG_TEMP_CTRL2);
2096 w83795_apply_temp_config(data, tmp >> 6, 3, 20);
2097 w83795_apply_temp_config(data, (tmp >> 4) & 0x3, 2, 19);
2098 w83795_apply_temp_config(data, (tmp >> 2) & 0x3, 1, 18);
2099 w83795_apply_temp_config(data, tmp & 0x3, 0, 17);
2101 /* Check DTS enable status */
2102 if (data->enable_dts) {
2103 if (1 & w83795_read(client, W83795_REG_DTSC))
2104 data->enable_dts |= 2;
2105 data->has_dts = w83795_read(client, W83795_REG_DTSE);
2108 /* Report PECI Tbase values */
2109 if (data->enable_dts == 1) {
2110 for (i = 0; i < 8; i++) {
2111 if (!(data->has_dts & (1 << i)))
2113 tmp = w83795_read(client, W83795_REG_PECI_TBASE(i));
2114 dev_info(&client->dev,
2115 "PECI agent %d Tbase temperature: %u\n",
2116 i + 1, (unsigned int)tmp & 0x7f);
2120 data->has_gain = w83795_read(client, W83795_REG_VMIGB_CTRL) & 0x0f;
2122 /* pwm and smart fan */
2123 if (data->chip_type == w83795g)
2128 err = w83795_handle_files(dev, device_create_file);
2132 if (data->chip_type == w83795g)
2133 w83795_check_dynamic_in_limits(client);
2135 data->hwmon_dev = hwmon_device_register(dev);
2136 if (IS_ERR(data->hwmon_dev)) {
2137 err = PTR_ERR(data->hwmon_dev);
2144 w83795_handle_files(dev, device_remove_file_wrapper);
2150 static int w83795_remove(struct i2c_client *client)
2152 struct w83795_data *data = i2c_get_clientdata(client);
2154 hwmon_device_unregister(data->hwmon_dev);
2155 w83795_handle_files(&client->dev, device_remove_file_wrapper);
2162 static const struct i2c_device_id w83795_id[] = {
2163 { "w83795g", w83795g },
2164 { "w83795adg", w83795adg },
2167 MODULE_DEVICE_TABLE(i2c, w83795_id);
2169 static struct i2c_driver w83795_driver = {
2173 .probe = w83795_probe,
2174 .remove = w83795_remove,
2175 .id_table = w83795_id,
2177 .class = I2C_CLASS_HWMON,
2178 .detect = w83795_detect,
2179 .address_list = normal_i2c,
2182 static int __init sensors_w83795_init(void)
2184 return i2c_add_driver(&w83795_driver);
2187 static void __exit sensors_w83795_exit(void)
2189 i2c_del_driver(&w83795_driver);
2192 MODULE_AUTHOR("Wei Song, Jean Delvare <khali@linux-fr.org>");
2193 MODULE_DESCRIPTION("W83795G/ADG hardware monitoring driver");
2194 MODULE_LICENSE("GPL");
2196 module_init(sensors_w83795_init);
2197 module_exit(sensors_w83795_exit);