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
69 #define TEMP_CTRL_DISABLE 0
70 #define TEMP_CTRL_TD 1
71 #define TEMP_CTRL_VSEN 2
72 #define TEMP_CTRL_TR 3
73 #define TEMP_CTRL_SHIFT 4
74 #define TEMP_CTRL_HASIN_SHIFT 5
75 /* temp mode may effect VSEN17-12 (in20-15) */
76 static const u16 W83795_REG_TEMP_CTRL[][6] = {
77 /* Disable, TD, VSEN, TR, register shift value, has_in shift num */
78 {0x00, 0x01, 0x02, 0x03, 0, 17}, /* TR1 */
79 {0x00, 0x04, 0x08, 0x0C, 2, 18}, /* TR2 */
80 {0x00, 0x10, 0x20, 0x30, 4, 19}, /* TR3 */
81 {0x00, 0x40, 0x80, 0xC0, 6, 20}, /* TR4 */
82 {0x00, 0x00, 0x02, 0x03, 0, 15}, /* TR5 */
83 {0x00, 0x00, 0x08, 0x0C, 2, 16}, /* TR6 */
88 #define TEMP_CRIT_HYST 2
90 #define TEMP_WARN_HYST 4
91 /* only crit and crit_hyst affect real-time alarm status
92 * current crit crit_hyst warn warn_hyst */
93 static const u16 W83795_REG_TEMP[][5] = {
94 {0x21, 0x96, 0x97, 0x98, 0x99}, /* TD1/TR1 */
95 {0x22, 0x9a, 0x9b, 0x9c, 0x9d}, /* TD2/TR2 */
96 {0x23, 0x9e, 0x9f, 0xa0, 0xa1}, /* TD3/TR3 */
97 {0x24, 0xa2, 0xa3, 0xa4, 0xa5}, /* TD4/TR4 */
98 {0x1f, 0xa6, 0xa7, 0xa8, 0xa9}, /* TR5 */
99 {0x20, 0xaa, 0xab, 0xac, 0xad}, /* TR6 */
105 static const u16 W83795_REG_IN[][3] = {
106 /* Current, HL, LL */
107 {0x10, 0x70, 0x71}, /* VSEN1 */
108 {0x11, 0x72, 0x73}, /* VSEN2 */
109 {0x12, 0x74, 0x75}, /* VSEN3 */
110 {0x13, 0x76, 0x77}, /* VSEN4 */
111 {0x14, 0x78, 0x79}, /* VSEN5 */
112 {0x15, 0x7a, 0x7b}, /* VSEN6 */
113 {0x16, 0x7c, 0x7d}, /* VSEN7 */
114 {0x17, 0x7e, 0x7f}, /* VSEN8 */
115 {0x18, 0x80, 0x81}, /* VSEN9 */
116 {0x19, 0x82, 0x83}, /* VSEN10 */
117 {0x1A, 0x84, 0x85}, /* VSEN11 */
118 {0x1B, 0x86, 0x87}, /* VTT */
119 {0x1C, 0x88, 0x89}, /* 3VDD */
120 {0x1D, 0x8a, 0x8b}, /* 3VSB */
121 {0x1E, 0x8c, 0x8d}, /* VBAT */
122 {0x1F, 0xa6, 0xa7}, /* VSEN12 */
123 {0x20, 0xaa, 0xab}, /* VSEN13 */
124 {0x21, 0x96, 0x97}, /* VSEN14 */
125 {0x22, 0x9a, 0x9b}, /* VSEN15 */
126 {0x23, 0x9e, 0x9f}, /* VSEN16 */
127 {0x24, 0xa2, 0xa3}, /* VSEN17 */
129 #define W83795_REG_VRLSB 0x3C
130 #define VRLSB_SHIFT 6
132 static const u8 W83795_REG_IN_HL_LSB[] = {
136 0x94, /* VTT, 3VDD, 3VSB, 3VBAT */
145 #define IN_LSB_REG(index, type) \
146 (((type) == 1) ? W83795_REG_IN_HL_LSB[(index)] \
147 : (W83795_REG_IN_HL_LSB[(index)] + 1))
149 #define IN_LSB_REG_NUM 10
151 #define IN_LSB_SHIFT 0
153 static const u8 IN_LSB_SHIFT_IDX[][2] = {
154 /* High/Low LSB shift, LSB No. */
155 {0x00, 0x00}, /* VSEN1 */
156 {0x02, 0x00}, /* VSEN2 */
157 {0x04, 0x00}, /* VSEN3 */
158 {0x06, 0x00}, /* VSEN4 */
159 {0x00, 0x01}, /* VSEN5 */
160 {0x02, 0x01}, /* VSEN6 */
161 {0x04, 0x01}, /* VSEN7 */
162 {0x06, 0x01}, /* VSEN8 */
163 {0x00, 0x02}, /* VSEN9 */
164 {0x02, 0x02}, /* VSEN10 */
165 {0x04, 0x02}, /* VSEN11 */
166 {0x00, 0x03}, /* VTT */
167 {0x02, 0x03}, /* 3VDD */
168 {0x04, 0x03}, /* 3VSB */
169 {0x06, 0x03}, /* VBAT */
170 {0x06, 0x04}, /* VSEN12 */
171 {0x06, 0x05}, /* VSEN13 */
172 {0x06, 0x06}, /* VSEN14 */
173 {0x06, 0x07}, /* VSEN15 */
174 {0x06, 0x08}, /* VSEN16 */
175 {0x06, 0x09}, /* VSEN17 */
179 /* 3VDD, 3VSB, VBAT * 0.006 */
180 #define REST_VLT_BEGIN 12 /* the 13th volt to 15th */
181 #define REST_VLT_END 14 /* the 13th volt to 15th */
183 #define W83795_REG_FAN(index) (0x2E + (index))
184 #define W83795_REG_FAN_MIN_HL(index) (0xB6 + (index))
185 #define W83795_REG_FAN_MIN_LSB(index) (0xC4 + (index) / 2)
186 #define W83795_REG_FAN_MIN_LSB_SHIFT(index) \
187 (((index) % 1) ? 4 : 0)
189 #define W83795_REG_VID_CTRL 0x6A
191 #define ALARM_BEEP_REG_NUM 6
192 #define W83795_REG_ALARM(index) (0x41 + (index))
193 #define W83795_REG_BEEP(index) (0x50 + (index))
195 #define W83795_REG_CLR_CHASSIS 0x4D
198 #define W83795_REG_TEMP_NUM 6
199 #define W83795_REG_FCMS1 0x201
200 #define W83795_REG_FCMS2 0x208
201 #define W83795_REG_TFMR(index) (0x202 + (index))
202 #define W83795_REG_FOMC 0x20F
204 #define W83795_REG_TSS(index) (0x209 + (index))
208 #define PWM_NONSTOP 2
209 #define PWM_STOP_TIME 3
211 #define W83795_REG_PWM(index, nr) \
212 (((nr) == 0 ? 0x210 : \
213 (nr) == 1 ? 0x220 : \
214 (nr) == 2 ? 0x228 : \
215 (nr) == 3 ? 0x230 : 0x218) + (index))
217 #define W83795_REG_FTSH(index) (0x240 + (index) * 2)
218 #define W83795_REG_FTSL(index) (0x241 + (index) * 2)
219 #define W83795_REG_TFTS 0x250
221 #define TEMP_PWM_TTTI 0
222 #define TEMP_PWM_CTFS 1
223 #define TEMP_PWM_HCT 2
224 #define TEMP_PWM_HOT 3
225 #define W83795_REG_TTTI(index) (0x260 + (index))
226 #define W83795_REG_CTFS(index) (0x268 + (index))
227 #define W83795_REG_HT(index) (0x270 + (index))
231 #define W83795_REG_SF4_TEMP(temp_num, index) \
232 (0x280 + 0x10 * (temp_num) + (index))
233 #define W83795_REG_SF4_PWM(temp_num, index) \
234 (0x288 + 0x10 * (temp_num) + (index))
236 #define W83795_REG_DTSC 0x301
237 #define W83795_REG_DTSE 0x302
238 #define W83795_REG_DTS(index) (0x26 + (index))
241 #define DTS_CRIT_HYST 1
243 #define DTS_WARN_HYST 3
244 #define W83795_REG_DTS_EXT(index) (0xB2 + (index))
246 #define SETUP_PWM_DEFAULT 0
247 #define SETUP_PWM_UPTIME 1
248 #define SETUP_PWM_DOWNTIME 2
249 #define W83795_REG_SETUP_PWM(index) (0x20C + (index))
251 static inline u16 in_from_reg(u8 index, u16 val)
253 if ((index >= REST_VLT_BEGIN) && (index <= REST_VLT_END))
259 static inline u16 in_to_reg(u8 index, u16 val)
261 if ((index >= REST_VLT_BEGIN) && (index <= REST_VLT_END))
267 static inline unsigned long fan_from_reg(u16 val)
269 if ((val == 0xfff) || (val == 0))
271 return 1350000UL / val;
274 static inline u16 fan_to_reg(long rpm)
278 return SENSORS_LIMIT((1350000 + (rpm >> 1)) / rpm, 1, 0xffe);
281 static inline unsigned long time_from_reg(u8 reg)
286 static inline u8 time_to_reg(unsigned long val)
288 return SENSORS_LIMIT((val + 50) / 100, 0, 0xff);
291 static inline long temp_from_reg(s8 reg)
296 static inline s8 temp_to_reg(long val, s8 min, s8 max)
298 return SENSORS_LIMIT(val / 1000, min, max);
301 static const u16 pwm_freq_cksel0[16] = {
302 1024, 512, 341, 256, 205, 171, 146, 128,
303 85, 64, 32, 16, 8, 4, 2, 1
306 static unsigned int pwm_freq_from_reg(u8 reg, u16 clkin)
308 unsigned long base_clock;
311 base_clock = clkin * 1000 / ((clkin == 48000) ? 384 : 256);
312 return base_clock / ((reg & 0x7f) + 1);
314 return pwm_freq_cksel0[reg & 0x0f];
317 static u8 pwm_freq_to_reg(unsigned long val, u16 clkin)
319 unsigned long base_clock;
321 unsigned long best0, best1;
323 /* Best fit for cksel = 0 */
324 for (reg0 = 0; reg0 < ARRAY_SIZE(pwm_freq_cksel0) - 1; reg0++) {
325 if (val > (pwm_freq_cksel0[reg0] +
326 pwm_freq_cksel0[reg0 + 1]) / 2)
329 if (val < 375) /* cksel = 1 can't beat this */
331 best0 = pwm_freq_cksel0[reg0];
333 /* Best fit for cksel = 1 */
334 base_clock = clkin * 1000 / ((clkin == 48000) ? 384 : 256);
335 reg1 = SENSORS_LIMIT(DIV_ROUND_CLOSEST(base_clock, val), 1, 128);
336 best1 = base_clock / reg1;
337 reg1 = 0x80 | (reg1 - 1);
339 /* Choose the closest one */
340 if (abs(val - best0) > abs(val - best1))
346 enum chip_types {w83795g, w83795adg};
349 struct device *hwmon_dev;
350 struct mutex update_lock;
351 unsigned long last_updated; /* In jiffies */
352 enum chip_types chip_type;
356 u32 has_in; /* Enable monitor VIN or not */
357 u8 has_dyn_in; /* Only in2-0 can have this */
358 u16 in[21][3]; /* Register value, read/high/low */
359 u8 in_lsb[10][3]; /* LSB Register value, high/low */
360 u8 has_gain; /* has gain: in17-20 * 8 */
362 u16 has_fan; /* Enable fan14-1 or not */
363 u16 fan[14]; /* Register value combine */
364 u16 fan_min[14]; /* Register value combine */
366 u8 has_temp; /* Enable monitor temp6-1 or not */
367 s8 temp[6][5]; /* current, crit, crit_hyst, warn, warn_hyst */
368 u8 temp_read_vrlsb[6];
369 u8 temp_mode; /* bit 0: TR mode, bit 1: TD mode */
370 u8 temp_src[3]; /* Register value */
372 u8 enable_dts; /* Enable PECI and SB-TSI,
373 * bit 0: =1 enable, =0 disable,
374 * bit 1: =1 AMD SB-TSI, =0 Intel PECI */
375 u8 has_dts; /* Enable monitor DTS temp */
376 s8 dts[8]; /* Register value */
377 u8 dts_read_vrlsb[8]; /* Register value */
378 s8 dts_ext[4]; /* Register value */
380 u8 has_pwm; /* 795g supports 8 pwm, 795adg only supports 2,
381 * no config register, only affected by chip
383 u8 pwm[8][5]; /* Register value, output, start, non stop, stop
385 u16 clkin; /* CLKIN frequency in kHz */
386 u8 pwm_fcms[2]; /* Register value */
387 u8 pwm_tfmr[6]; /* Register value */
388 u8 pwm_fomc; /* Register value */
390 u16 target_speed[8]; /* Register value, target speed for speed
392 u8 tol_speed; /* tolerance of target speed */
393 u8 pwm_temp[6][4]; /* TTTI, CTFS, HCT, HOT */
394 u8 sf4_reg[6][2][7]; /* 6 temp, temp/dcpwm, 7 registers */
396 u8 setup_pwm[3]; /* Register value */
398 u8 alarms[6]; /* Register value */
399 u8 beeps[6]; /* Register value */
407 * We assume that nobdody can change the bank outside the driver.
410 /* Must be called with data->update_lock held, except during initialization */
411 static int w83795_set_bank(struct i2c_client *client, u8 bank)
413 struct w83795_data *data = i2c_get_clientdata(client);
416 /* If the same bank is already set, nothing to do */
417 if ((data->bank & 0x07) == bank)
420 /* Change to new bank, preserve all other bits */
421 bank |= data->bank & ~0x07;
422 err = i2c_smbus_write_byte_data(client, W83795_REG_BANKSEL, bank);
424 dev_err(&client->dev,
425 "Failed to set bank to %d, err %d\n",
434 /* Must be called with data->update_lock held, except during initialization */
435 static u8 w83795_read(struct i2c_client *client, u16 reg)
439 err = w83795_set_bank(client, reg >> 8);
441 return 0x00; /* Arbitrary */
443 err = i2c_smbus_read_byte_data(client, reg & 0xff);
445 dev_err(&client->dev,
446 "Failed to read from register 0x%03x, err %d\n",
448 return 0x00; /* Arbitrary */
453 /* Must be called with data->update_lock held, except during initialization */
454 static int w83795_write(struct i2c_client *client, u16 reg, u8 value)
458 err = w83795_set_bank(client, reg >> 8);
462 err = i2c_smbus_write_byte_data(client, reg & 0xff, value);
464 dev_err(&client->dev,
465 "Failed to write to register 0x%03x, err %d\n",
470 static struct w83795_data *w83795_update_device(struct device *dev)
472 struct i2c_client *client = to_i2c_client(dev);
473 struct w83795_data *data = i2c_get_clientdata(client);
477 mutex_lock(&data->update_lock);
479 if (!(time_after(jiffies, data->last_updated + HZ * 2)
483 /* Update the voltages value */
484 for (i = 0; i < ARRAY_SIZE(data->in); i++) {
485 if (!(data->has_in & (1 << i)))
487 tmp = w83795_read(client, W83795_REG_IN[i][IN_READ]) << 2;
488 tmp |= (w83795_read(client, W83795_REG_VRLSB)
489 >> VRLSB_SHIFT) & 0x03;
490 data->in[i][IN_READ] = tmp;
493 /* in0-2 can have dynamic limits (W83795G only) */
494 if (data->has_dyn_in) {
495 u8 lsb_max = w83795_read(client, IN_LSB_REG(0, IN_MAX));
496 u8 lsb_low = w83795_read(client, IN_LSB_REG(0, IN_LOW));
498 for (i = 0; i < 3; i++) {
499 if (!(data->has_dyn_in & (1 << i)))
501 data->in[i][IN_MAX] =
502 w83795_read(client, W83795_REG_IN[i][IN_MAX]);
503 data->in[i][IN_LOW] =
504 w83795_read(client, W83795_REG_IN[i][IN_LOW]);
505 data->in_lsb[i][IN_MAX] = (lsb_max >> (2 * i)) & 0x03;
506 data->in_lsb[i][IN_LOW] = (lsb_low >> (2 * i)) & 0x03;
511 for (i = 0; i < ARRAY_SIZE(data->fan); i++) {
512 if (!(data->has_fan & (1 << i)))
514 data->fan[i] = w83795_read(client, W83795_REG_FAN(i)) << 4;
516 (w83795_read(client, W83795_REG_VRLSB) >> 4) & 0x0F;
519 /* Update temperature */
520 for (i = 0; i < ARRAY_SIZE(data->temp); i++) {
521 /* even stop monitor, register still keep value, just read out
523 if (!(data->has_temp & (1 << i))) {
524 data->temp[i][TEMP_READ] = 0;
525 data->temp_read_vrlsb[i] = 0;
528 data->temp[i][TEMP_READ] =
529 w83795_read(client, W83795_REG_TEMP[i][TEMP_READ]);
530 data->temp_read_vrlsb[i] =
531 w83795_read(client, W83795_REG_VRLSB);
534 /* Update dts temperature */
535 if (data->enable_dts != 0) {
536 for (i = 0; i < ARRAY_SIZE(data->dts); i++) {
537 if (!(data->has_dts & (1 << i)))
540 w83795_read(client, W83795_REG_DTS(i));
541 data->dts_read_vrlsb[i] =
542 w83795_read(client, W83795_REG_VRLSB);
546 /* Update pwm output */
547 for (i = 0; i < data->has_pwm; i++) {
548 data->pwm[i][PWM_OUTPUT] =
549 w83795_read(client, W83795_REG_PWM(i, PWM_OUTPUT));
553 for (i = 0; i < ALARM_BEEP_REG_NUM; i++)
554 data->alarms[i] = w83795_read(client, W83795_REG_ALARM(i));
556 data->last_updated = jiffies;
560 mutex_unlock(&data->update_lock);
568 #define ALARM_STATUS 0
569 #define BEEP_ENABLE 1
571 show_alarm_beep(struct device *dev, struct device_attribute *attr, char *buf)
573 struct w83795_data *data = w83795_update_device(dev);
574 struct sensor_device_attribute_2 *sensor_attr =
575 to_sensor_dev_attr_2(attr);
576 int nr = sensor_attr->nr;
577 int index = sensor_attr->index >> 3;
578 int bit = sensor_attr->index & 0x07;
581 if (ALARM_STATUS == nr) {
582 val = (data->alarms[index] >> (bit)) & 1;
583 } else { /* BEEP_ENABLE */
584 val = (data->beeps[index] >> (bit)) & 1;
587 return sprintf(buf, "%u\n", val);
591 store_beep(struct device *dev, struct device_attribute *attr,
592 const char *buf, size_t count)
594 struct i2c_client *client = to_i2c_client(dev);
595 struct w83795_data *data = i2c_get_clientdata(client);
596 struct sensor_device_attribute_2 *sensor_attr =
597 to_sensor_dev_attr_2(attr);
598 int index = sensor_attr->index >> 3;
599 int shift = sensor_attr->index & 0x07;
600 u8 beep_bit = 1 << shift;
603 if (strict_strtoul(buf, 10, &val) < 0)
605 if (val != 0 && val != 1)
608 mutex_lock(&data->update_lock);
609 data->beeps[index] = w83795_read(client, W83795_REG_BEEP(index));
610 data->beeps[index] &= ~beep_bit;
611 data->beeps[index] |= val << shift;
612 w83795_write(client, W83795_REG_BEEP(index), data->beeps[index]);
613 mutex_unlock(&data->update_lock);
619 show_beep_enable(struct device *dev, struct device_attribute *attr, char *buf)
621 struct i2c_client *client = to_i2c_client(dev);
622 struct w83795_data *data = i2c_get_clientdata(client);
623 return sprintf(buf, "%u\n", data->beep_enable);
627 store_beep_enable(struct device *dev, struct device_attribute *attr,
628 const char *buf, size_t count)
630 struct i2c_client *client = to_i2c_client(dev);
631 struct w83795_data *data = i2c_get_clientdata(client);
635 if (strict_strtoul(buf, 10, &val) < 0)
637 if (val != 0 && val != 1)
640 mutex_lock(&data->update_lock);
641 data->beep_enable = val;
642 tmp = w83795_read(client, W83795_REG_BEEP(5));
645 w83795_write(client, W83795_REG_BEEP(5), tmp);
646 mutex_unlock(&data->update_lock);
651 /* Write any value to clear chassis alarm */
653 store_chassis_clear(struct device *dev,
654 struct device_attribute *attr, const char *buf,
657 struct i2c_client *client = to_i2c_client(dev);
658 struct w83795_data *data = i2c_get_clientdata(client);
661 mutex_lock(&data->update_lock);
662 val = w83795_read(client, W83795_REG_CLR_CHASSIS);
664 w83795_write(client, W83795_REG_CLR_CHASSIS, val);
665 mutex_unlock(&data->update_lock);
672 show_fan(struct device *dev, struct device_attribute *attr, char *buf)
674 struct sensor_device_attribute_2 *sensor_attr =
675 to_sensor_dev_attr_2(attr);
676 int nr = sensor_attr->nr;
677 int index = sensor_attr->index;
678 struct w83795_data *data = w83795_update_device(dev);
682 val = data->fan[index] & 0x0fff;
684 val = data->fan_min[index] & 0x0fff;
686 return sprintf(buf, "%lu\n", fan_from_reg(val));
690 store_fan_min(struct device *dev, struct device_attribute *attr,
691 const char *buf, size_t count)
693 struct sensor_device_attribute_2 *sensor_attr =
694 to_sensor_dev_attr_2(attr);
695 int index = sensor_attr->index;
696 struct i2c_client *client = to_i2c_client(dev);
697 struct w83795_data *data = i2c_get_clientdata(client);
700 if (strict_strtoul(buf, 10, &val))
702 val = fan_to_reg(val);
704 mutex_lock(&data->update_lock);
705 data->fan_min[index] = val;
706 w83795_write(client, W83795_REG_FAN_MIN_HL(index), (val >> 4) & 0xff);
710 val |= w83795_read(client, W83795_REG_FAN_MIN_LSB(index))
713 val |= w83795_read(client, W83795_REG_FAN_MIN_LSB(index))
716 w83795_write(client, W83795_REG_FAN_MIN_LSB(index), val & 0xff);
717 mutex_unlock(&data->update_lock);
723 show_pwm(struct device *dev, struct device_attribute *attr, char *buf)
725 struct w83795_data *data = w83795_update_device(dev);
726 struct sensor_device_attribute_2 *sensor_attr =
727 to_sensor_dev_attr_2(attr);
728 int nr = sensor_attr->nr;
729 int index = sensor_attr->index;
734 val = time_from_reg(data->pwm[index][nr]);
737 val = pwm_freq_from_reg(data->pwm[index][nr], data->clkin);
740 val = data->pwm[index][nr];
744 return sprintf(buf, "%u\n", val);
748 store_pwm(struct device *dev, struct device_attribute *attr,
749 const char *buf, size_t count)
751 struct i2c_client *client = to_i2c_client(dev);
752 struct w83795_data *data = i2c_get_clientdata(client);
753 struct sensor_device_attribute_2 *sensor_attr =
754 to_sensor_dev_attr_2(attr);
755 int nr = sensor_attr->nr;
756 int index = sensor_attr->index;
759 if (strict_strtoul(buf, 10, &val) < 0)
762 mutex_lock(&data->update_lock);
765 val = time_to_reg(val);
768 val = pwm_freq_to_reg(val, data->clkin);
771 val = SENSORS_LIMIT(val, 0, 0xff);
774 w83795_write(client, W83795_REG_PWM(index, nr), val);
775 data->pwm[index][nr] = val;
776 mutex_unlock(&data->update_lock);
781 show_pwm_enable(struct device *dev, struct device_attribute *attr, char *buf)
783 struct sensor_device_attribute_2 *sensor_attr =
784 to_sensor_dev_attr_2(attr);
785 struct i2c_client *client = to_i2c_client(dev);
786 struct w83795_data *data = i2c_get_clientdata(client);
787 int index = sensor_attr->index;
790 if (1 == (data->pwm_fcms[0] & (1 << index))) {
794 for (tmp = 0; tmp < 6; tmp++) {
795 if (data->pwm_tfmr[tmp] & (1 << index)) {
800 if (data->pwm_fomc & (1 << index))
806 return sprintf(buf, "%u\n", tmp);
810 store_pwm_enable(struct device *dev, struct device_attribute *attr,
811 const char *buf, size_t count)
813 struct i2c_client *client = to_i2c_client(dev);
814 struct w83795_data *data = i2c_get_clientdata(client);
815 struct sensor_device_attribute_2 *sensor_attr =
816 to_sensor_dev_attr_2(attr);
817 int index = sensor_attr->index;
821 if (strict_strtoul(buf, 10, &val) < 0)
826 mutex_lock(&data->update_lock);
830 data->pwm_fcms[0] &= ~(1 << index);
831 w83795_write(client, W83795_REG_FCMS1, data->pwm_fcms[0]);
832 for (i = 0; i < 6; i++) {
833 data->pwm_tfmr[i] &= ~(1 << index);
834 w83795_write(client, W83795_REG_TFMR(i),
837 data->pwm_fomc |= 1 << index;
838 data->pwm_fomc ^= val << index;
839 w83795_write(client, W83795_REG_FOMC, data->pwm_fomc);
842 data->pwm_fcms[0] |= (1 << index);
843 w83795_write(client, W83795_REG_FCMS1, data->pwm_fcms[0]);
846 mutex_unlock(&data->update_lock);
851 show_temp_src(struct device *dev, struct device_attribute *attr, char *buf)
853 struct sensor_device_attribute_2 *sensor_attr =
854 to_sensor_dev_attr_2(attr);
855 struct i2c_client *client = to_i2c_client(dev);
856 struct w83795_data *data = i2c_get_clientdata(client);
857 int index = sensor_attr->index;
859 u8 tmp = data->temp_src[val];
868 return sprintf(buf, "%u\n", tmp);
872 store_temp_src(struct device *dev, struct device_attribute *attr,
873 const char *buf, size_t count)
875 struct i2c_client *client = to_i2c_client(dev);
876 struct w83795_data *data = i2c_get_clientdata(client);
877 struct sensor_device_attribute_2 *sensor_attr =
878 to_sensor_dev_attr_2(attr);
879 int index = sensor_attr->index;
883 if (strict_strtoul(buf, 10, &tmp) < 0)
885 tmp = SENSORS_LIMIT(tmp, 0, 15);
887 mutex_lock(&data->update_lock);
890 data->temp_src[val] &= 0x0f;
892 data->temp_src[val] &= 0xf0;
894 data->temp_src[val] |= tmp;
895 w83795_write(client, W83795_REG_TSS(val), data->temp_src[val]);
896 mutex_unlock(&data->update_lock);
901 #define TEMP_PWM_ENABLE 0
902 #define TEMP_PWM_FAN_MAP 1
904 show_temp_pwm_enable(struct device *dev, struct device_attribute *attr,
907 struct i2c_client *client = to_i2c_client(dev);
908 struct w83795_data *data = i2c_get_clientdata(client);
909 struct sensor_device_attribute_2 *sensor_attr =
910 to_sensor_dev_attr_2(attr);
911 int nr = sensor_attr->nr;
912 int index = sensor_attr->index;
916 case TEMP_PWM_ENABLE:
917 tmp = (data->pwm_fcms[1] >> index) & 1;
923 case TEMP_PWM_FAN_MAP:
924 tmp = data->pwm_tfmr[index];
928 return sprintf(buf, "%u\n", tmp);
932 store_temp_pwm_enable(struct device *dev, struct device_attribute *attr,
933 const char *buf, size_t count)
935 struct i2c_client *client = to_i2c_client(dev);
936 struct w83795_data *data = i2c_get_clientdata(client);
937 struct sensor_device_attribute_2 *sensor_attr =
938 to_sensor_dev_attr_2(attr);
939 int nr = sensor_attr->nr;
940 int index = sensor_attr->index;
943 if (strict_strtoul(buf, 10, &tmp) < 0)
947 case TEMP_PWM_ENABLE:
948 if ((tmp != 3) && (tmp != 4))
951 mutex_lock(&data->update_lock);
952 data->pwm_fcms[1] &= ~(1 << index);
953 data->pwm_fcms[1] |= tmp << index;
954 w83795_write(client, W83795_REG_FCMS2, data->pwm_fcms[1]);
955 mutex_unlock(&data->update_lock);
957 case TEMP_PWM_FAN_MAP:
958 mutex_lock(&data->update_lock);
959 tmp = SENSORS_LIMIT(tmp, 0, 0xff);
960 w83795_write(client, W83795_REG_TFMR(index), tmp);
961 data->pwm_tfmr[index] = tmp;
962 mutex_unlock(&data->update_lock);
968 #define FANIN_TARGET 0
971 show_fanin(struct device *dev, struct device_attribute *attr, char *buf)
973 struct i2c_client *client = to_i2c_client(dev);
974 struct w83795_data *data = i2c_get_clientdata(client);
975 struct sensor_device_attribute_2 *sensor_attr =
976 to_sensor_dev_attr_2(attr);
977 int nr = sensor_attr->nr;
978 int index = sensor_attr->index;
983 tmp = fan_from_reg(data->target_speed[index]);
986 tmp = data->tol_speed;
990 return sprintf(buf, "%u\n", tmp);
994 store_fanin(struct device *dev, struct device_attribute *attr,
995 const char *buf, size_t count)
997 struct i2c_client *client = to_i2c_client(dev);
998 struct w83795_data *data = i2c_get_clientdata(client);
999 struct sensor_device_attribute_2 *sensor_attr =
1000 to_sensor_dev_attr_2(attr);
1001 int nr = sensor_attr->nr;
1002 int index = sensor_attr->index;
1005 if (strict_strtoul(buf, 10, &val) < 0)
1008 mutex_lock(&data->update_lock);
1011 val = fan_to_reg(SENSORS_LIMIT(val, 0, 0xfff));
1012 w83795_write(client, W83795_REG_FTSH(index), (val >> 4) & 0xff);
1013 w83795_write(client, W83795_REG_FTSL(index), (val << 4) & 0xf0);
1014 data->target_speed[index] = val;
1017 val = SENSORS_LIMIT(val, 0, 0x3f);
1018 w83795_write(client, W83795_REG_TFTS, val);
1019 data->tol_speed = val;
1022 mutex_unlock(&data->update_lock);
1029 show_temp_pwm(struct device *dev, struct device_attribute *attr, char *buf)
1031 struct i2c_client *client = to_i2c_client(dev);
1032 struct w83795_data *data = i2c_get_clientdata(client);
1033 struct sensor_device_attribute_2 *sensor_attr =
1034 to_sensor_dev_attr_2(attr);
1035 int nr = sensor_attr->nr;
1036 int index = sensor_attr->index;
1037 long tmp = temp_from_reg(data->pwm_temp[index][nr]);
1039 return sprintf(buf, "%ld\n", tmp);
1043 store_temp_pwm(struct device *dev, struct device_attribute *attr,
1044 const char *buf, size_t count)
1046 struct i2c_client *client = to_i2c_client(dev);
1047 struct w83795_data *data = i2c_get_clientdata(client);
1048 struct sensor_device_attribute_2 *sensor_attr =
1049 to_sensor_dev_attr_2(attr);
1050 int nr = sensor_attr->nr;
1051 int index = sensor_attr->index;
1055 if (strict_strtoul(buf, 10, &val) < 0)
1059 mutex_lock(&data->update_lock);
1062 val = SENSORS_LIMIT(val, 0, 0x7f);
1063 w83795_write(client, W83795_REG_TTTI(index), val);
1066 val = SENSORS_LIMIT(val, 0, 0x7f);
1067 w83795_write(client, W83795_REG_CTFS(index), val);
1070 val = SENSORS_LIMIT(val, 0, 0x0f);
1071 tmp = w83795_read(client, W83795_REG_HT(index));
1073 tmp |= (val << 4) & 0xf0;
1074 w83795_write(client, W83795_REG_HT(index), tmp);
1077 val = SENSORS_LIMIT(val, 0, 0x0f);
1078 tmp = w83795_read(client, W83795_REG_HT(index));
1081 w83795_write(client, W83795_REG_HT(index), tmp);
1084 data->pwm_temp[index][nr] = val;
1085 mutex_unlock(&data->update_lock);
1091 show_sf4_pwm(struct device *dev, struct device_attribute *attr, char *buf)
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;
1100 return sprintf(buf, "%u\n", data->sf4_reg[index][SF4_PWM][nr]);
1104 store_sf4_pwm(struct device *dev, struct device_attribute *attr,
1105 const char *buf, size_t count)
1107 struct i2c_client *client = to_i2c_client(dev);
1108 struct w83795_data *data = i2c_get_clientdata(client);
1109 struct sensor_device_attribute_2 *sensor_attr =
1110 to_sensor_dev_attr_2(attr);
1111 int nr = sensor_attr->nr;
1112 int index = sensor_attr->index;
1115 if (strict_strtoul(buf, 10, &val) < 0)
1118 mutex_lock(&data->update_lock);
1119 w83795_write(client, W83795_REG_SF4_PWM(index, nr), val);
1120 data->sf4_reg[index][SF4_PWM][nr] = val;
1121 mutex_unlock(&data->update_lock);
1127 show_sf4_temp(struct device *dev, struct device_attribute *attr, char *buf)
1129 struct i2c_client *client = to_i2c_client(dev);
1130 struct w83795_data *data = i2c_get_clientdata(client);
1131 struct sensor_device_attribute_2 *sensor_attr =
1132 to_sensor_dev_attr_2(attr);
1133 int nr = sensor_attr->nr;
1134 int index = sensor_attr->index;
1136 return sprintf(buf, "%u\n",
1137 (data->sf4_reg[index][SF4_TEMP][nr]) * 1000);
1141 store_sf4_temp(struct device *dev, struct device_attribute *attr,
1142 const char *buf, size_t count)
1144 struct i2c_client *client = to_i2c_client(dev);
1145 struct w83795_data *data = i2c_get_clientdata(client);
1146 struct sensor_device_attribute_2 *sensor_attr =
1147 to_sensor_dev_attr_2(attr);
1148 int nr = sensor_attr->nr;
1149 int index = sensor_attr->index;
1152 if (strict_strtoul(buf, 10, &val) < 0)
1156 mutex_lock(&data->update_lock);
1157 w83795_write(client, W83795_REG_SF4_TEMP(index, nr), val);
1158 data->sf4_reg[index][SF4_TEMP][nr] = val;
1159 mutex_unlock(&data->update_lock);
1166 show_temp(struct device *dev, struct device_attribute *attr, char *buf)
1168 struct sensor_device_attribute_2 *sensor_attr =
1169 to_sensor_dev_attr_2(attr);
1170 int nr = sensor_attr->nr;
1171 int index = sensor_attr->index;
1172 struct w83795_data *data = w83795_update_device(dev);
1173 long temp = temp_from_reg(data->temp[index][nr]);
1175 if (TEMP_READ == nr)
1176 temp += ((data->temp_read_vrlsb[index] >> VRLSB_SHIFT) & 0x03)
1178 return sprintf(buf, "%ld\n", temp);
1182 store_temp(struct device *dev, struct device_attribute *attr,
1183 const char *buf, size_t count)
1185 struct sensor_device_attribute_2 *sensor_attr =
1186 to_sensor_dev_attr_2(attr);
1187 int nr = sensor_attr->nr;
1188 int index = sensor_attr->index;
1189 struct i2c_client *client = to_i2c_client(dev);
1190 struct w83795_data *data = i2c_get_clientdata(client);
1193 if (strict_strtol(buf, 10, &tmp) < 0)
1196 mutex_lock(&data->update_lock);
1197 data->temp[index][nr] = temp_to_reg(tmp, -128, 127);
1198 w83795_write(client, W83795_REG_TEMP[index][nr], data->temp[index][nr]);
1199 mutex_unlock(&data->update_lock);
1205 show_dts_mode(struct device *dev, struct device_attribute *attr, char *buf)
1207 struct i2c_client *client = to_i2c_client(dev);
1208 struct w83795_data *data = i2c_get_clientdata(client);
1209 struct sensor_device_attribute_2 *sensor_attr =
1210 to_sensor_dev_attr_2(attr);
1211 int index = sensor_attr->index;
1214 if (data->enable_dts == 0)
1215 return sprintf(buf, "%d\n", 0);
1217 if ((data->has_dts >> index) & 0x01) {
1218 if (data->enable_dts & 2)
1226 return sprintf(buf, "%d\n", tmp);
1230 show_dts(struct device *dev, struct device_attribute *attr, char *buf)
1232 struct sensor_device_attribute_2 *sensor_attr =
1233 to_sensor_dev_attr_2(attr);
1234 int index = sensor_attr->index;
1235 struct w83795_data *data = w83795_update_device(dev);
1236 long temp = temp_from_reg(data->dts[index]);
1238 temp += ((data->dts_read_vrlsb[index] >> VRLSB_SHIFT) & 0x03) * 250;
1239 return sprintf(buf, "%ld\n", temp);
1243 show_dts_ext(struct device *dev, struct device_attribute *attr, char *buf)
1245 struct sensor_device_attribute_2 *sensor_attr =
1246 to_sensor_dev_attr_2(attr);
1247 int nr = sensor_attr->nr;
1248 struct i2c_client *client = to_i2c_client(dev);
1249 struct w83795_data *data = i2c_get_clientdata(client);
1250 long temp = temp_from_reg(data->dts_ext[nr]);
1252 return sprintf(buf, "%ld\n", temp);
1256 store_dts_ext(struct device *dev, struct device_attribute *attr,
1257 const char *buf, size_t count)
1259 struct sensor_device_attribute_2 *sensor_attr =
1260 to_sensor_dev_attr_2(attr);
1261 int nr = sensor_attr->nr;
1262 struct i2c_client *client = to_i2c_client(dev);
1263 struct w83795_data *data = i2c_get_clientdata(client);
1266 if (strict_strtol(buf, 10, &tmp) < 0)
1269 mutex_lock(&data->update_lock);
1270 data->dts_ext[nr] = temp_to_reg(tmp, -128, 127);
1271 w83795_write(client, W83795_REG_DTS_EXT(nr), data->dts_ext[nr]);
1272 mutex_unlock(&data->update_lock);
1278 Type 3: Thermal diode
1286 show_temp_mode(struct device *dev, struct device_attribute *attr, char *buf)
1288 struct i2c_client *client = to_i2c_client(dev);
1289 struct w83795_data *data = i2c_get_clientdata(client);
1290 struct sensor_device_attribute_2 *sensor_attr =
1291 to_sensor_dev_attr_2(attr);
1292 int index = sensor_attr->index;
1295 if (data->has_temp >> index & 0x01) {
1296 if (data->temp_mode >> index & 0x01)
1304 return sprintf(buf, "%d\n", tmp);
1308 store_temp_mode(struct device *dev, struct device_attribute *attr,
1309 const char *buf, size_t count)
1311 struct i2c_client *client = to_i2c_client(dev);
1312 struct w83795_data *data = i2c_get_clientdata(client);
1313 struct sensor_device_attribute_2 *sensor_attr =
1314 to_sensor_dev_attr_2(attr);
1315 int index = sensor_attr->index;
1320 if (strict_strtoul(buf, 10, &val) < 0)
1322 if ((val != 4) && (val != 3))
1324 if ((index > 3) && (val == 3))
1327 mutex_lock(&data->update_lock);
1330 data->has_temp |= 1 << index;
1331 data->temp_mode |= 1 << index;
1332 } else if (val == 4) {
1334 data->has_temp |= 1 << index;
1336 data->temp_mode &= ~tmp;
1340 tmp = w83795_read(client, W83795_REG_TEMP_CTRL1);
1342 tmp = w83795_read(client, W83795_REG_TEMP_CTRL2);
1344 mask = 0x03 << W83795_REG_TEMP_CTRL[index][TEMP_CTRL_SHIFT];
1346 tmp |= W83795_REG_TEMP_CTRL[index][val];
1348 mask = 1 << W83795_REG_TEMP_CTRL[index][TEMP_CTRL_HASIN_SHIFT];
1349 data->has_in &= ~mask;
1352 w83795_write(client, W83795_REG_TEMP_CTRL1, tmp);
1354 w83795_write(client, W83795_REG_TEMP_CTRL2, tmp);
1356 mutex_unlock(&data->update_lock);
1361 /* show/store VIN */
1363 show_in(struct device *dev, struct device_attribute *attr, char *buf)
1365 struct sensor_device_attribute_2 *sensor_attr =
1366 to_sensor_dev_attr_2(attr);
1367 int nr = sensor_attr->nr;
1368 int index = sensor_attr->index;
1369 struct w83795_data *data = w83795_update_device(dev);
1370 u16 val = data->in[index][nr];
1375 /* calculate this value again by sensors as sensors3.conf */
1376 if ((index >= 17) &&
1377 !((data->has_gain >> (index - 17)) & 1))
1382 lsb_idx = IN_LSB_SHIFT_IDX[index][IN_LSB_IDX];
1384 val |= (data->in_lsb[lsb_idx][nr] >>
1385 IN_LSB_SHIFT_IDX[lsb_idx][IN_LSB_SHIFT]) & 0x03;
1386 if ((index >= 17) &&
1387 !((data->has_gain >> (index - 17)) & 1))
1391 val = in_from_reg(index, val);
1393 return sprintf(buf, "%d\n", val);
1397 store_in(struct device *dev, struct device_attribute *attr,
1398 const char *buf, size_t count)
1400 struct sensor_device_attribute_2 *sensor_attr =
1401 to_sensor_dev_attr_2(attr);
1402 int nr = sensor_attr->nr;
1403 int index = sensor_attr->index;
1404 struct i2c_client *client = to_i2c_client(dev);
1405 struct w83795_data *data = i2c_get_clientdata(client);
1410 if (strict_strtoul(buf, 10, &val) < 0)
1412 val = in_to_reg(index, val);
1414 if ((index >= 17) &&
1415 !((data->has_gain >> (index - 17)) & 1))
1417 val = SENSORS_LIMIT(val, 0, 0x3FF);
1418 mutex_lock(&data->update_lock);
1420 lsb_idx = IN_LSB_SHIFT_IDX[index][IN_LSB_IDX];
1421 tmp = w83795_read(client, IN_LSB_REG(lsb_idx, nr));
1422 tmp &= ~(0x03 << IN_LSB_SHIFT_IDX[index][IN_LSB_SHIFT]);
1423 tmp |= (val & 0x03) << IN_LSB_SHIFT_IDX[index][IN_LSB_SHIFT];
1424 w83795_write(client, IN_LSB_REG(lsb_idx, nr), tmp);
1425 data->in_lsb[lsb_idx][nr] = tmp;
1427 tmp = (val >> 2) & 0xff;
1428 w83795_write(client, W83795_REG_IN[index][nr], tmp);
1429 data->in[index][nr] = tmp;
1431 mutex_unlock(&data->update_lock);
1437 show_sf_setup(struct device *dev, struct device_attribute *attr, char *buf)
1439 struct sensor_device_attribute_2 *sensor_attr =
1440 to_sensor_dev_attr_2(attr);
1441 int nr = sensor_attr->nr;
1442 struct i2c_client *client = to_i2c_client(dev);
1443 struct w83795_data *data = i2c_get_clientdata(client);
1444 u16 val = data->setup_pwm[nr];
1447 case SETUP_PWM_UPTIME:
1448 case SETUP_PWM_DOWNTIME:
1449 val = time_from_reg(val);
1453 return sprintf(buf, "%d\n", val);
1457 store_sf_setup(struct device *dev, struct device_attribute *attr,
1458 const char *buf, size_t count)
1460 struct sensor_device_attribute_2 *sensor_attr =
1461 to_sensor_dev_attr_2(attr);
1462 int nr = sensor_attr->nr;
1463 struct i2c_client *client = to_i2c_client(dev);
1464 struct w83795_data *data = i2c_get_clientdata(client);
1467 if (strict_strtoul(buf, 10, &val) < 0)
1471 case SETUP_PWM_DEFAULT:
1472 val = SENSORS_LIMIT(val, 0, 0xff);
1474 case SETUP_PWM_UPTIME:
1475 case SETUP_PWM_DOWNTIME:
1476 val = time_to_reg(val);
1482 mutex_lock(&data->update_lock);
1483 data->setup_pwm[nr] = val;
1484 w83795_write(client, W83795_REG_SETUP_PWM(nr), val);
1485 mutex_unlock(&data->update_lock);
1492 /* Don't change the attribute order, _max and _min are accessed by index
1493 * somewhere else in the code */
1494 #define SENSOR_ATTR_IN(index) { \
1495 SENSOR_ATTR_2(in##index##_input, S_IRUGO, show_in, NULL, \
1497 SENSOR_ATTR_2(in##index##_max, S_IRUGO | S_IWUSR, show_in, \
1498 store_in, IN_MAX, index), \
1499 SENSOR_ATTR_2(in##index##_min, S_IRUGO | S_IWUSR, show_in, \
1500 store_in, IN_LOW, index), \
1501 SENSOR_ATTR_2(in##index##_alarm, S_IRUGO, show_alarm_beep, \
1502 NULL, ALARM_STATUS, index + ((index > 14) ? 1 : 0)), \
1503 SENSOR_ATTR_2(in##index##_beep, S_IWUSR | S_IRUGO, \
1504 show_alarm_beep, store_beep, BEEP_ENABLE, \
1505 index + ((index > 14) ? 1 : 0)) }
1507 #define SENSOR_ATTR_FAN(index) { \
1508 SENSOR_ATTR_2(fan##index##_input, S_IRUGO, show_fan, \
1509 NULL, FAN_INPUT, index - 1), \
1510 SENSOR_ATTR_2(fan##index##_min, S_IWUSR | S_IRUGO, \
1511 show_fan, store_fan_min, FAN_MIN, index - 1), \
1512 SENSOR_ATTR_2(fan##index##_alarm, S_IRUGO, show_alarm_beep, \
1513 NULL, ALARM_STATUS, index + 31), \
1514 SENSOR_ATTR_2(fan##index##_beep, S_IWUSR | S_IRUGO, \
1515 show_alarm_beep, store_beep, BEEP_ENABLE, index + 31) }
1517 #define SENSOR_ATTR_PWM(index) { \
1518 SENSOR_ATTR_2(pwm##index, S_IWUSR | S_IRUGO, show_pwm, \
1519 store_pwm, PWM_OUTPUT, index - 1), \
1520 SENSOR_ATTR_2(pwm##index##_nonstop, S_IWUSR | S_IRUGO, \
1521 show_pwm, store_pwm, PWM_NONSTOP, index - 1), \
1522 SENSOR_ATTR_2(pwm##index##_start, S_IWUSR | S_IRUGO, \
1523 show_pwm, store_pwm, PWM_START, index - 1), \
1524 SENSOR_ATTR_2(pwm##index##_stop_time, S_IWUSR | S_IRUGO, \
1525 show_pwm, store_pwm, PWM_STOP_TIME, index - 1), \
1526 SENSOR_ATTR_2(pwm##index##_freq, S_IWUSR | S_IRUGO, \
1527 show_pwm, store_pwm, PWM_FREQ, index - 1), \
1528 SENSOR_ATTR_2(pwm##index##_enable, S_IWUSR | S_IRUGO, \
1529 show_pwm_enable, store_pwm_enable, NOT_USED, index - 1), \
1530 SENSOR_ATTR_2(fan##index##_target, S_IWUSR | S_IRUGO, \
1531 show_fanin, store_fanin, FANIN_TARGET, index - 1) }
1533 #define SENSOR_ATTR_DTS(index) { \
1534 SENSOR_ATTR_2(temp##index##_type, S_IRUGO , \
1535 show_dts_mode, NULL, NOT_USED, index - 7), \
1536 SENSOR_ATTR_2(temp##index##_input, S_IRUGO, show_dts, \
1537 NULL, NOT_USED, index - 7), \
1538 SENSOR_ATTR_2(temp##index##_crit, S_IRUGO | S_IWUSR, show_dts_ext, \
1539 store_dts_ext, DTS_CRIT, NOT_USED), \
1540 SENSOR_ATTR_2(temp##index##_crit_hyst, S_IRUGO | S_IWUSR, \
1541 show_dts_ext, store_dts_ext, DTS_CRIT_HYST, NOT_USED), \
1542 SENSOR_ATTR_2(temp##index##_max, S_IRUGO | S_IWUSR, show_dts_ext, \
1543 store_dts_ext, DTS_WARN, NOT_USED), \
1544 SENSOR_ATTR_2(temp##index##_max_hyst, S_IRUGO | S_IWUSR, \
1545 show_dts_ext, store_dts_ext, DTS_WARN_HYST, NOT_USED), \
1546 SENSOR_ATTR_2(temp##index##_alarm, S_IRUGO, \
1547 show_alarm_beep, NULL, ALARM_STATUS, index + 17), \
1548 SENSOR_ATTR_2(temp##index##_beep, S_IWUSR | S_IRUGO, \
1549 show_alarm_beep, store_beep, BEEP_ENABLE, index + 17) }
1551 #define SENSOR_ATTR_TEMP(index) { \
1552 SENSOR_ATTR_2(temp##index##_type, S_IRUGO | S_IWUSR, \
1553 show_temp_mode, store_temp_mode, NOT_USED, index - 1), \
1554 SENSOR_ATTR_2(temp##index##_input, S_IRUGO, show_temp, \
1555 NULL, TEMP_READ, index - 1), \
1556 SENSOR_ATTR_2(temp##index##_crit, S_IRUGO | S_IWUSR, show_temp, \
1557 store_temp, TEMP_CRIT, index - 1), \
1558 SENSOR_ATTR_2(temp##index##_crit_hyst, S_IRUGO | S_IWUSR, \
1559 show_temp, store_temp, TEMP_CRIT_HYST, index - 1), \
1560 SENSOR_ATTR_2(temp##index##_max, S_IRUGO | S_IWUSR, show_temp, \
1561 store_temp, TEMP_WARN, index - 1), \
1562 SENSOR_ATTR_2(temp##index##_max_hyst, S_IRUGO | S_IWUSR, \
1563 show_temp, store_temp, TEMP_WARN_HYST, index - 1), \
1564 SENSOR_ATTR_2(temp##index##_alarm, S_IRUGO, \
1565 show_alarm_beep, NULL, ALARM_STATUS, \
1566 index + (index > 4 ? 11 : 17)), \
1567 SENSOR_ATTR_2(temp##index##_beep, S_IWUSR | S_IRUGO, \
1568 show_alarm_beep, store_beep, BEEP_ENABLE, \
1569 index + (index > 4 ? 11 : 17)), \
1570 SENSOR_ATTR_2(temp##index##_source_sel, S_IWUSR | S_IRUGO, \
1571 show_temp_src, store_temp_src, NOT_USED, index - 1), \
1572 SENSOR_ATTR_2(temp##index##_pwm_enable, S_IWUSR | S_IRUGO, \
1573 show_temp_pwm_enable, store_temp_pwm_enable, \
1574 TEMP_PWM_ENABLE, index - 1), \
1575 SENSOR_ATTR_2(temp##index##_auto_channels_pwm, S_IWUSR | S_IRUGO, \
1576 show_temp_pwm_enable, store_temp_pwm_enable, \
1577 TEMP_PWM_FAN_MAP, index - 1), \
1578 SENSOR_ATTR_2(thermal_cruise##index, S_IWUSR | S_IRUGO, \
1579 show_temp_pwm, store_temp_pwm, TEMP_PWM_TTTI, index - 1), \
1580 SENSOR_ATTR_2(temp##index##_warn, S_IWUSR | S_IRUGO, \
1581 show_temp_pwm, store_temp_pwm, TEMP_PWM_CTFS, index - 1), \
1582 SENSOR_ATTR_2(temp##index##_warn_hyst, S_IWUSR | S_IRUGO, \
1583 show_temp_pwm, store_temp_pwm, TEMP_PWM_HCT, index - 1), \
1584 SENSOR_ATTR_2(temp##index##_operation_hyst, S_IWUSR | S_IRUGO, \
1585 show_temp_pwm, store_temp_pwm, TEMP_PWM_HOT, index - 1), \
1586 SENSOR_ATTR_2(temp##index##_auto_point1_pwm, S_IRUGO | S_IWUSR, \
1587 show_sf4_pwm, store_sf4_pwm, 0, index - 1), \
1588 SENSOR_ATTR_2(temp##index##_auto_point2_pwm, S_IRUGO | S_IWUSR, \
1589 show_sf4_pwm, store_sf4_pwm, 1, index - 1), \
1590 SENSOR_ATTR_2(temp##index##_auto_point3_pwm, S_IRUGO | S_IWUSR, \
1591 show_sf4_pwm, store_sf4_pwm, 2, index - 1), \
1592 SENSOR_ATTR_2(temp##index##_auto_point4_pwm, S_IRUGO | S_IWUSR, \
1593 show_sf4_pwm, store_sf4_pwm, 3, index - 1), \
1594 SENSOR_ATTR_2(temp##index##_auto_point5_pwm, S_IRUGO | S_IWUSR, \
1595 show_sf4_pwm, store_sf4_pwm, 4, index - 1), \
1596 SENSOR_ATTR_2(temp##index##_auto_point6_pwm, S_IRUGO | S_IWUSR, \
1597 show_sf4_pwm, store_sf4_pwm, 5, index - 1), \
1598 SENSOR_ATTR_2(temp##index##_auto_point7_pwm, S_IRUGO | S_IWUSR, \
1599 show_sf4_pwm, store_sf4_pwm, 6, index - 1), \
1600 SENSOR_ATTR_2(temp##index##_auto_point1_temp, S_IRUGO | S_IWUSR,\
1601 show_sf4_temp, store_sf4_temp, 0, index - 1), \
1602 SENSOR_ATTR_2(temp##index##_auto_point2_temp, S_IRUGO | S_IWUSR,\
1603 show_sf4_temp, store_sf4_temp, 1, index - 1), \
1604 SENSOR_ATTR_2(temp##index##_auto_point3_temp, S_IRUGO | S_IWUSR,\
1605 show_sf4_temp, store_sf4_temp, 2, index - 1), \
1606 SENSOR_ATTR_2(temp##index##_auto_point4_temp, S_IRUGO | S_IWUSR,\
1607 show_sf4_temp, store_sf4_temp, 3, index - 1), \
1608 SENSOR_ATTR_2(temp##index##_auto_point5_temp, S_IRUGO | S_IWUSR,\
1609 show_sf4_temp, store_sf4_temp, 4, index - 1), \
1610 SENSOR_ATTR_2(temp##index##_auto_point6_temp, S_IRUGO | S_IWUSR,\
1611 show_sf4_temp, store_sf4_temp, 5, index - 1), \
1612 SENSOR_ATTR_2(temp##index##_auto_point7_temp, S_IRUGO | S_IWUSR,\
1613 show_sf4_temp, store_sf4_temp, 6, index - 1) }
1616 static struct sensor_device_attribute_2 w83795_in[][5] = {
1640 static const struct sensor_device_attribute_2 w83795_fan[][4] = {
1650 SENSOR_ATTR_FAN(10),
1651 SENSOR_ATTR_FAN(11),
1652 SENSOR_ATTR_FAN(12),
1653 SENSOR_ATTR_FAN(13),
1654 SENSOR_ATTR_FAN(14),
1657 static const struct sensor_device_attribute_2 w83795_temp[][29] = {
1658 SENSOR_ATTR_TEMP(1),
1659 SENSOR_ATTR_TEMP(2),
1660 SENSOR_ATTR_TEMP(3),
1661 SENSOR_ATTR_TEMP(4),
1662 SENSOR_ATTR_TEMP(5),
1663 SENSOR_ATTR_TEMP(6),
1666 static const struct sensor_device_attribute_2 w83795_dts[][8] = {
1670 SENSOR_ATTR_DTS(10),
1671 SENSOR_ATTR_DTS(11),
1672 SENSOR_ATTR_DTS(12),
1673 SENSOR_ATTR_DTS(13),
1674 SENSOR_ATTR_DTS(14),
1677 static const struct sensor_device_attribute_2 w83795_pwm[][7] = {
1688 static const struct sensor_device_attribute_2 sda_single_files[] = {
1689 SENSOR_ATTR_2(chassis, S_IWUSR | S_IRUGO, show_alarm_beep,
1690 store_chassis_clear, ALARM_STATUS, 46),
1691 SENSOR_ATTR_2(beep_enable, S_IWUSR | S_IRUGO, show_beep_enable,
1692 store_beep_enable, NOT_USED, NOT_USED),
1693 SENSOR_ATTR_2(speed_cruise_tolerance, S_IWUSR | S_IRUGO, show_fanin,
1694 store_fanin, FANIN_TOL, NOT_USED),
1695 SENSOR_ATTR_2(pwm_default, S_IWUSR | S_IRUGO, show_sf_setup,
1696 store_sf_setup, SETUP_PWM_DEFAULT, NOT_USED),
1697 SENSOR_ATTR_2(pwm_uptime, S_IWUSR | S_IRUGO, show_sf_setup,
1698 store_sf_setup, SETUP_PWM_UPTIME, NOT_USED),
1699 SENSOR_ATTR_2(pwm_downtime, S_IWUSR | S_IRUGO, show_sf_setup,
1700 store_sf_setup, SETUP_PWM_DOWNTIME, NOT_USED),
1707 static void w83795_init_client(struct i2c_client *client)
1709 struct w83795_data *data = i2c_get_clientdata(client);
1710 static const u16 clkin[4] = { /* in kHz */
1711 14318, 24000, 33333, 48000
1716 w83795_write(client, W83795_REG_CONFIG, 0x80);
1718 /* Start monitoring if needed */
1719 config = w83795_read(client, W83795_REG_CONFIG);
1720 if (!(config & W83795_REG_CONFIG_START)) {
1721 dev_info(&client->dev, "Enabling monitoring operations\n");
1722 w83795_write(client, W83795_REG_CONFIG,
1723 config | W83795_REG_CONFIG_START);
1726 data->clkin = clkin[(config >> 3) & 0x3];
1727 dev_dbg(&client->dev, "clkin = %u kHz\n", data->clkin);
1730 static int w83795_get_device_id(struct i2c_client *client)
1734 device_id = i2c_smbus_read_byte_data(client, W83795_REG_DEVICEID);
1736 /* Special case for rev. A chips; can't be checked first because later
1737 revisions emulate this for compatibility */
1738 if (device_id < 0 || (device_id & 0xf0) != 0x50) {
1741 alt_id = i2c_smbus_read_byte_data(client,
1742 W83795_REG_DEVICEID_A);
1750 /* Return 0 if detection is successful, -ENODEV otherwise */
1751 static int w83795_detect(struct i2c_client *client,
1752 struct i2c_board_info *info)
1754 int bank, vendor_id, device_id, expected, i2c_addr, config;
1755 struct i2c_adapter *adapter = client->adapter;
1756 unsigned short address = client->addr;
1757 const char *chip_name;
1759 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1761 bank = i2c_smbus_read_byte_data(client, W83795_REG_BANKSEL);
1762 if (bank < 0 || (bank & 0x7c)) {
1763 dev_dbg(&adapter->dev,
1764 "w83795: Detection failed at addr 0x%02hx, check %s\n",
1769 /* Check Nuvoton vendor ID */
1770 vendor_id = i2c_smbus_read_byte_data(client, W83795_REG_VENDORID);
1771 expected = bank & 0x80 ? 0x5c : 0xa3;
1772 if (vendor_id != expected) {
1773 dev_dbg(&adapter->dev,
1774 "w83795: Detection failed at addr 0x%02hx, check %s\n",
1775 address, "vendor id");
1779 /* Check device ID */
1780 device_id = w83795_get_device_id(client) |
1781 (i2c_smbus_read_byte_data(client, W83795_REG_CHIPID) << 8);
1782 if ((device_id >> 4) != 0x795) {
1783 dev_dbg(&adapter->dev,
1784 "w83795: Detection failed at addr 0x%02hx, check %s\n",
1785 address, "device id\n");
1789 /* If Nuvoton chip, address of chip and W83795_REG_I2C_ADDR
1791 if ((bank & 0x07) == 0) {
1792 i2c_addr = i2c_smbus_read_byte_data(client,
1793 W83795_REG_I2C_ADDR);
1794 if ((i2c_addr & 0x7f) != address) {
1795 dev_dbg(&adapter->dev,
1796 "w83795: Detection failed at addr 0x%02hx, "
1797 "check %s\n", address, "i2c addr");
1802 /* Check 795 chip type: 795G or 795ADG
1803 Usually we don't write to chips during detection, but here we don't
1804 quite have the choice; hopefully it's OK, we are about to return
1806 if ((bank & 0x07) != 0)
1807 i2c_smbus_write_byte_data(client, W83795_REG_BANKSEL,
1809 config = i2c_smbus_read_byte_data(client, W83795_REG_CONFIG);
1810 if (config & W83795_REG_CONFIG_CONFIG48)
1811 chip_name = "w83795adg";
1813 chip_name = "w83795g";
1815 strlcpy(info->type, chip_name, I2C_NAME_SIZE);
1816 dev_info(&adapter->dev, "Found %s rev. %c at 0x%02hx\n", chip_name,
1817 'A' + (device_id & 0xf), address);
1822 static int w83795_handle_files(struct device *dev, int (*fn)(struct device *,
1823 const struct device_attribute *))
1825 struct w83795_data *data = dev_get_drvdata(dev);
1828 for (i = 0; i < ARRAY_SIZE(w83795_in); i++) {
1829 if (!(data->has_in & (1 << i)))
1831 for (j = 0; j < ARRAY_SIZE(w83795_in[0]); j++) {
1832 err = fn(dev, &w83795_in[i][j].dev_attr);
1838 for (i = 0; i < ARRAY_SIZE(w83795_fan); i++) {
1839 if (!(data->has_fan & (1 << i)))
1841 for (j = 0; j < ARRAY_SIZE(w83795_fan[0]); j++) {
1842 err = fn(dev, &w83795_fan[i][j].dev_attr);
1848 for (i = 0; i < ARRAY_SIZE(sda_single_files); i++) {
1849 err = fn(dev, &sda_single_files[i].dev_attr);
1854 for (i = 0; i < data->has_pwm; i++) {
1855 for (j = 0; j < ARRAY_SIZE(w83795_pwm[0]); j++) {
1856 err = fn(dev, &w83795_pwm[i][j].dev_attr);
1862 for (i = 0; i < ARRAY_SIZE(w83795_temp); i++) {
1863 if (!(data->has_temp & (1 << i)))
1865 for (j = 0; j < ARRAY_SIZE(w83795_temp[0]); j++) {
1866 err = fn(dev, &w83795_temp[i][j].dev_attr);
1872 if (data->enable_dts != 0) {
1873 for (i = 0; i < ARRAY_SIZE(w83795_dts); i++) {
1874 if (!(data->has_dts & (1 << i)))
1876 for (j = 0; j < ARRAY_SIZE(w83795_dts[0]); j++) {
1877 err = fn(dev, &w83795_dts[i][j].dev_attr);
1887 /* We need a wrapper that fits in w83795_handle_files */
1888 static int device_remove_file_wrapper(struct device *dev,
1889 const struct device_attribute *attr)
1891 device_remove_file(dev, attr);
1895 static void w83795_check_dynamic_in_limits(struct i2c_client *client)
1897 struct w83795_data *data = i2c_get_clientdata(client);
1899 int i, err_max, err_min;
1901 vid_ctl = w83795_read(client, W83795_REG_VID_CTRL);
1903 /* Return immediately if VRM isn't configured */
1904 if ((vid_ctl & 0x07) == 0x00 || (vid_ctl & 0x07) == 0x07)
1907 data->has_dyn_in = (vid_ctl >> 3) & 0x07;
1908 for (i = 0; i < 2; i++) {
1909 if (!(data->has_dyn_in & (1 << i)))
1912 /* Voltage limits in dynamic mode, switch to read-only */
1913 err_max = sysfs_chmod_file(&client->dev.kobj,
1914 &w83795_in[i][2].dev_attr.attr,
1916 err_min = sysfs_chmod_file(&client->dev.kobj,
1917 &w83795_in[i][3].dev_attr.attr,
1919 if (err_max || err_min)
1920 dev_warn(&client->dev, "Failed to set in%d limits "
1921 "read-only (%d, %d)\n", i, err_max, err_min);
1923 dev_info(&client->dev, "in%d limits set dynamically "
1928 /* Check pins that can be used for either temperature or voltage monitoring */
1929 static void w83795_apply_temp_config(struct w83795_data *data, u8 config,
1930 int temp_chan, int in_chan)
1932 /* config is a 2-bit value */
1934 case 0x2: /* Voltage monitoring */
1935 data->has_in |= 1 << in_chan;
1937 case 0x1: /* Thermal diode */
1940 data->temp_mode |= 1 << temp_chan;
1942 case 0x3: /* Thermistor */
1943 data->has_temp |= 1 << temp_chan;
1948 static int w83795_probe(struct i2c_client *client,
1949 const struct i2c_device_id *id)
1953 struct device *dev = &client->dev;
1954 struct w83795_data *data;
1957 data = kzalloc(sizeof(struct w83795_data), GFP_KERNEL);
1963 i2c_set_clientdata(client, data);
1964 data->chip_type = id->driver_data;
1965 data->bank = i2c_smbus_read_byte_data(client, W83795_REG_BANKSEL);
1966 mutex_init(&data->update_lock);
1968 /* Initialize the chip */
1969 w83795_init_client(client);
1971 /* Check which voltages and fans are present */
1972 data->has_in = w83795_read(client, W83795_REG_VOLT_CTRL1)
1973 | (w83795_read(client, W83795_REG_VOLT_CTRL2) << 8);
1974 data->has_fan = w83795_read(client, W83795_REG_FANIN_CTRL1)
1975 | (w83795_read(client, W83795_REG_FANIN_CTRL2) << 8);
1977 /* Check which analog temperatures and extra voltages are present */
1978 tmp = w83795_read(client, W83795_REG_TEMP_CTRL1);
1980 data->enable_dts = 1;
1981 w83795_apply_temp_config(data, (tmp >> 2) & 0x3, 5, 16);
1982 w83795_apply_temp_config(data, tmp & 0x3, 4, 15);
1983 tmp = w83795_read(client, W83795_REG_TEMP_CTRL2);
1984 w83795_apply_temp_config(data, tmp >> 6, 3, 20);
1985 w83795_apply_temp_config(data, (tmp >> 4) & 0x3, 2, 19);
1986 w83795_apply_temp_config(data, (tmp >> 2) & 0x3, 1, 18);
1987 w83795_apply_temp_config(data, tmp & 0x3, 0, 17);
1989 /* Check DTS enable status */
1990 if (data->enable_dts) {
1991 if (1 & w83795_read(client, W83795_REG_DTSC))
1992 data->enable_dts |= 2;
1993 data->has_dts = w83795_read(client, W83795_REG_DTSE);
1996 /* First update the voltages measured value and limits */
1997 for (i = 0; i < ARRAY_SIZE(data->in); i++) {
1998 if (!(data->has_in & (1 << i)))
2000 data->in[i][IN_MAX] =
2001 w83795_read(client, W83795_REG_IN[i][IN_MAX]);
2002 data->in[i][IN_LOW] =
2003 w83795_read(client, W83795_REG_IN[i][IN_LOW]);
2004 tmp = w83795_read(client, W83795_REG_IN[i][IN_READ]) << 2;
2005 tmp |= (w83795_read(client, W83795_REG_VRLSB)
2006 >> VRLSB_SHIFT) & 0x03;
2007 data->in[i][IN_READ] = tmp;
2009 for (i = 0; i < IN_LSB_REG_NUM; i++) {
2010 data->in_lsb[i][IN_MAX] =
2011 w83795_read(client, IN_LSB_REG(i, IN_MAX));
2012 data->in_lsb[i][IN_LOW] =
2013 w83795_read(client, IN_LSB_REG(i, IN_LOW));
2015 data->has_gain = w83795_read(client, W83795_REG_VMIGB_CTRL) & 0x0f;
2017 /* First update fan and limits */
2018 for (i = 0; i < ARRAY_SIZE(data->fan); i++) {
2019 if (!(data->has_fan & (1 << i)))
2022 w83795_read(client, W83795_REG_FAN_MIN_HL(i)) << 4;
2024 (w83795_read(client, W83795_REG_FAN_MIN_LSB(i)) >>
2025 W83795_REG_FAN_MIN_LSB_SHIFT(i)) & 0x0F;
2026 data->fan[i] = w83795_read(client, W83795_REG_FAN(i)) << 4;
2028 (w83795_read(client, W83795_REG_VRLSB) >> 4) & 0x0F;
2031 /* temperature and limits */
2032 for (i = 0; i < ARRAY_SIZE(data->temp); i++) {
2033 if (!(data->has_temp & (1 << i)))
2035 data->temp[i][TEMP_CRIT] =
2036 w83795_read(client, W83795_REG_TEMP[i][TEMP_CRIT]);
2037 data->temp[i][TEMP_CRIT_HYST] =
2038 w83795_read(client, W83795_REG_TEMP[i][TEMP_CRIT_HYST]);
2039 data->temp[i][TEMP_WARN] =
2040 w83795_read(client, W83795_REG_TEMP[i][TEMP_WARN]);
2041 data->temp[i][TEMP_WARN_HYST] =
2042 w83795_read(client, W83795_REG_TEMP[i][TEMP_WARN_HYST]);
2043 data->temp[i][TEMP_READ] =
2044 w83795_read(client, W83795_REG_TEMP[i][TEMP_READ]);
2045 data->temp_read_vrlsb[i] =
2046 w83795_read(client, W83795_REG_VRLSB);
2049 /* dts temperature and limits */
2050 if (data->enable_dts != 0) {
2051 data->dts_ext[DTS_CRIT] =
2052 w83795_read(client, W83795_REG_DTS_EXT(DTS_CRIT));
2053 data->dts_ext[DTS_CRIT_HYST] =
2054 w83795_read(client, W83795_REG_DTS_EXT(DTS_CRIT_HYST));
2055 data->dts_ext[DTS_WARN] =
2056 w83795_read(client, W83795_REG_DTS_EXT(DTS_WARN));
2057 data->dts_ext[DTS_WARN_HYST] =
2058 w83795_read(client, W83795_REG_DTS_EXT(DTS_WARN_HYST));
2059 for (i = 0; i < ARRAY_SIZE(data->dts); i++) {
2060 if (!(data->has_dts & (1 << i)))
2062 data->dts[i] = w83795_read(client, W83795_REG_DTS(i));
2063 data->dts_read_vrlsb[i] =
2064 w83795_read(client, W83795_REG_VRLSB);
2068 /* First update temp source selction */
2069 for (i = 0; i < 3; i++)
2070 data->temp_src[i] = w83795_read(client, W83795_REG_TSS(i));
2072 /* pwm and smart fan */
2073 if (data->chip_type == w83795g)
2077 data->pwm_fcms[0] = w83795_read(client, W83795_REG_FCMS1);
2078 data->pwm_fcms[1] = w83795_read(client, W83795_REG_FCMS2);
2079 for (i = 0; i < W83795_REG_TEMP_NUM; i++)
2080 data->pwm_tfmr[i] = w83795_read(client, W83795_REG_TFMR(i));
2081 data->pwm_fomc = w83795_read(client, W83795_REG_FOMC);
2082 for (i = 0; i < data->has_pwm; i++) {
2083 for (tmp = 0; tmp < 5; tmp++) {
2085 w83795_read(client, W83795_REG_PWM(i, tmp));
2088 for (i = 0; i < 8; i++) {
2089 data->target_speed[i] =
2090 w83795_read(client, W83795_REG_FTSH(i)) << 4;
2091 data->target_speed[i] |=
2092 w83795_read(client, W83795_REG_FTSL(i)) >> 4;
2094 data->tol_speed = w83795_read(client, W83795_REG_TFTS) & 0x3f;
2096 for (i = 0; i < W83795_REG_TEMP_NUM; i++) {
2097 data->pwm_temp[i][TEMP_PWM_TTTI] =
2098 w83795_read(client, W83795_REG_TTTI(i)) & 0x7f;
2099 data->pwm_temp[i][TEMP_PWM_CTFS] =
2100 w83795_read(client, W83795_REG_CTFS(i));
2101 tmp = w83795_read(client, W83795_REG_HT(i));
2102 data->pwm_temp[i][TEMP_PWM_HCT] = (tmp >> 4) & 0x0f;
2103 data->pwm_temp[i][TEMP_PWM_HOT] = tmp & 0x0f;
2105 for (i = 0; i < W83795_REG_TEMP_NUM; i++) {
2106 for (tmp = 0; tmp < 7; tmp++) {
2107 data->sf4_reg[i][SF4_TEMP][tmp] =
2109 W83795_REG_SF4_TEMP(i, tmp));
2110 data->sf4_reg[i][SF4_PWM][tmp] =
2111 w83795_read(client, W83795_REG_SF4_PWM(i, tmp));
2115 /* Setup PWM Register */
2116 for (i = 0; i < 3; i++) {
2117 data->setup_pwm[i] =
2118 w83795_read(client, W83795_REG_SETUP_PWM(i));
2121 /* alarm and beep */
2122 for (i = 0; i < ALARM_BEEP_REG_NUM; i++) {
2123 data->alarms[i] = w83795_read(client, W83795_REG_ALARM(i));
2124 data->beeps[i] = w83795_read(client, W83795_REG_BEEP(i));
2127 (w83795_read(client, W83795_REG_BEEP(5)) >> 7) & 0x01;
2129 err = w83795_handle_files(dev, device_create_file);
2133 if (data->chip_type == w83795g)
2134 w83795_check_dynamic_in_limits(client);
2136 data->hwmon_dev = hwmon_device_register(dev);
2137 if (IS_ERR(data->hwmon_dev)) {
2138 err = PTR_ERR(data->hwmon_dev);
2145 w83795_handle_files(dev, device_remove_file_wrapper);
2151 static int w83795_remove(struct i2c_client *client)
2153 struct w83795_data *data = i2c_get_clientdata(client);
2155 hwmon_device_unregister(data->hwmon_dev);
2156 w83795_handle_files(&client->dev, device_remove_file_wrapper);
2163 static const struct i2c_device_id w83795_id[] = {
2164 { "w83795g", w83795g },
2165 { "w83795adg", w83795adg },
2168 MODULE_DEVICE_TABLE(i2c, w83795_id);
2170 static struct i2c_driver w83795_driver = {
2174 .probe = w83795_probe,
2175 .remove = w83795_remove,
2176 .id_table = w83795_id,
2178 .class = I2C_CLASS_HWMON,
2179 .detect = w83795_detect,
2180 .address_list = normal_i2c,
2183 static int __init sensors_w83795_init(void)
2185 return i2c_add_driver(&w83795_driver);
2188 static void __exit sensors_w83795_exit(void)
2190 i2c_del_driver(&w83795_driver);
2193 MODULE_AUTHOR("Wei Song");
2194 MODULE_DESCRIPTION("W83795G/ADG hardware monitoring driver");
2195 MODULE_LICENSE("GPL");
2197 module_init(sensors_w83795_init);
2198 module_exit(sensors_w83795_exit);