blob: 4e54edc415d71aba2058052e79619e28149731bf [file] [log] [blame]
Wei Song792d3762010-10-28 20:31:44 +02001/*
2 * w83795.c - Linux kernel driver for hardware monitoring
3 * Copyright (C) 2008 Nuvoton Technology Corp.
4 * Wei Song
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation - version 2.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 * 02110-1301 USA.
19 *
20 * Supports following chips:
21 *
22 * Chip #vin #fanin #pwm #temp #dts wchipid vendid i2c ISA
23 * w83795g 21 14 8 6 8 0x79 0x5ca3 yes no
24 * w83795adg 18 14 2 6 8 0x79 0x5ca3 yes no
25 */
26
27#include <linux/kernel.h>
28#include <linux/module.h>
29#include <linux/init.h>
30#include <linux/slab.h>
31#include <linux/i2c.h>
32#include <linux/hwmon.h>
33#include <linux/hwmon-sysfs.h>
34#include <linux/err.h>
35#include <linux/mutex.h>
36#include <linux/delay.h>
37
38/* Addresses to scan */
Jean Delvare86ef4d22010-10-28 20:31:46 +020039static const unsigned short normal_i2c[] = {
40 0x2c, 0x2d, 0x2e, 0x2f, I2C_CLIENT_END
41};
Wei Song792d3762010-10-28 20:31:44 +020042
Wei Song792d3762010-10-28 20:31:44 +020043
44static int reset;
45module_param(reset, bool, 0);
46MODULE_PARM_DESC(reset, "Set to 1 to reset chip, not recommended");
47
48
49#define W83795_REG_BANKSEL 0x00
50#define W83795_REG_VENDORID 0xfd
51#define W83795_REG_CHIPID 0xfe
52#define W83795_REG_DEVICEID 0xfb
Jean Delvare2be381d2010-10-28 20:31:45 +020053#define W83795_REG_DEVICEID_A 0xff
Wei Song792d3762010-10-28 20:31:44 +020054
55#define W83795_REG_I2C_ADDR 0xfc
56#define W83795_REG_CONFIG 0x01
57#define W83795_REG_CONFIG_CONFIG48 0x04
Jean Delvare80646b92010-10-28 20:31:46 +020058#define W83795_REG_CONFIG_START 0x01
Wei Song792d3762010-10-28 20:31:44 +020059
60/* Multi-Function Pin Ctrl Registers */
61#define W83795_REG_VOLT_CTRL1 0x02
62#define W83795_REG_VOLT_CTRL2 0x03
63#define W83795_REG_TEMP_CTRL1 0x04
64#define W83795_REG_TEMP_CTRL2 0x05
65#define W83795_REG_FANIN_CTRL1 0x06
66#define W83795_REG_FANIN_CTRL2 0x07
67#define W83795_REG_VMIGB_CTRL 0x08
68
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) */
Jean Delvare86ef4d22010-10-28 20:31:46 +020076static const u16 W83795_REG_TEMP_CTRL[][6] = {
Wei Song792d3762010-10-28 20:31:44 +020077 /* 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 */
84};
85
86#define TEMP_READ 0
87#define TEMP_CRIT 1
88#define TEMP_CRIT_HYST 2
89#define TEMP_WARN 3
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 */
Jean Delvare86ef4d22010-10-28 20:31:46 +020093static const u16 W83795_REG_TEMP[][5] = {
Wei Song792d3762010-10-28 20:31:44 +020094 {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 */
100};
101
102#define IN_READ 0
103#define IN_MAX 1
104#define IN_LOW 2
105static 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 */
128};
129#define W83795_REG_VRLSB 0x3C
Wei Song792d3762010-10-28 20:31:44 +0200130
131static const u8 W83795_REG_IN_HL_LSB[] = {
132 0x8e, /* VSEN1-4 */
133 0x90, /* VSEN5-8 */
134 0x92, /* VSEN9-11 */
135 0x94, /* VTT, 3VDD, 3VSB, 3VBAT */
136 0xa8, /* VSEN12 */
137 0xac, /* VSEN13 */
138 0x98, /* VSEN14 */
139 0x9c, /* VSEN15 */
140 0xa0, /* VSEN16 */
141 0xa4, /* VSEN17 */
142};
143
144#define IN_LSB_REG(index, type) \
145 (((type) == 1) ? W83795_REG_IN_HL_LSB[(index)] \
146 : (W83795_REG_IN_HL_LSB[(index)] + 1))
147
148#define IN_LSB_REG_NUM 10
149
150#define IN_LSB_SHIFT 0
151#define IN_LSB_IDX 1
152static const u8 IN_LSB_SHIFT_IDX[][2] = {
153 /* High/Low LSB shift, LSB No. */
154 {0x00, 0x00}, /* VSEN1 */
155 {0x02, 0x00}, /* VSEN2 */
156 {0x04, 0x00}, /* VSEN3 */
157 {0x06, 0x00}, /* VSEN4 */
158 {0x00, 0x01}, /* VSEN5 */
159 {0x02, 0x01}, /* VSEN6 */
160 {0x04, 0x01}, /* VSEN7 */
161 {0x06, 0x01}, /* VSEN8 */
162 {0x00, 0x02}, /* VSEN9 */
163 {0x02, 0x02}, /* VSEN10 */
164 {0x04, 0x02}, /* VSEN11 */
165 {0x00, 0x03}, /* VTT */
166 {0x02, 0x03}, /* 3VDD */
167 {0x04, 0x03}, /* 3VSB */
168 {0x06, 0x03}, /* VBAT */
169 {0x06, 0x04}, /* VSEN12 */
170 {0x06, 0x05}, /* VSEN13 */
171 {0x06, 0x06}, /* VSEN14 */
172 {0x06, 0x07}, /* VSEN15 */
173 {0x06, 0x08}, /* VSEN16 */
174 {0x06, 0x09}, /* VSEN17 */
175};
176
177
Wei Song792d3762010-10-28 20:31:44 +0200178#define W83795_REG_FAN(index) (0x2E + (index))
179#define W83795_REG_FAN_MIN_HL(index) (0xB6 + (index))
180#define W83795_REG_FAN_MIN_LSB(index) (0xC4 + (index) / 2)
181#define W83795_REG_FAN_MIN_LSB_SHIFT(index) \
Jean Delvare7eb8d502010-10-28 20:31:47 +0200182 (((index) & 1) ? 4 : 0)
Wei Song792d3762010-10-28 20:31:44 +0200183
184#define W83795_REG_VID_CTRL 0x6A
185
186#define ALARM_BEEP_REG_NUM 6
187#define W83795_REG_ALARM(index) (0x41 + (index))
188#define W83795_REG_BEEP(index) (0x50 + (index))
189
190#define W83795_REG_CLR_CHASSIS 0x4D
191
192
193#define W83795_REG_TEMP_NUM 6
194#define W83795_REG_FCMS1 0x201
195#define W83795_REG_FCMS2 0x208
196#define W83795_REG_TFMR(index) (0x202 + (index))
197#define W83795_REG_FOMC 0x20F
Wei Song792d3762010-10-28 20:31:44 +0200198
199#define W83795_REG_TSS(index) (0x209 + (index))
200
201#define PWM_OUTPUT 0
202#define PWM_START 1
203#define PWM_NONSTOP 2
204#define PWM_STOP_TIME 3
Jean Delvare01879a82010-10-28 20:31:47 +0200205#define PWM_FREQ 4
Wei Song792d3762010-10-28 20:31:44 +0200206#define W83795_REG_PWM(index, nr) \
207 (((nr) == 0 ? 0x210 : \
208 (nr) == 1 ? 0x220 : \
209 (nr) == 2 ? 0x228 : \
210 (nr) == 3 ? 0x230 : 0x218) + (index))
211
Wei Song792d3762010-10-28 20:31:44 +0200212#define W83795_REG_FTSH(index) (0x240 + (index) * 2)
213#define W83795_REG_FTSL(index) (0x241 + (index) * 2)
214#define W83795_REG_TFTS 0x250
215
216#define TEMP_PWM_TTTI 0
217#define TEMP_PWM_CTFS 1
218#define TEMP_PWM_HCT 2
219#define TEMP_PWM_HOT 3
220#define W83795_REG_TTTI(index) (0x260 + (index))
221#define W83795_REG_CTFS(index) (0x268 + (index))
222#define W83795_REG_HT(index) (0x270 + (index))
223
224#define SF4_TEMP 0
225#define SF4_PWM 1
226#define W83795_REG_SF4_TEMP(temp_num, index) \
227 (0x280 + 0x10 * (temp_num) + (index))
228#define W83795_REG_SF4_PWM(temp_num, index) \
229 (0x288 + 0x10 * (temp_num) + (index))
230
231#define W83795_REG_DTSC 0x301
232#define W83795_REG_DTSE 0x302
233#define W83795_REG_DTS(index) (0x26 + (index))
Jean Delvare54891a32010-10-28 20:31:47 +0200234#define W83795_REG_PECI_TBASE(index) (0x320 + (index))
Wei Song792d3762010-10-28 20:31:44 +0200235
236#define DTS_CRIT 0
237#define DTS_CRIT_HYST 1
238#define DTS_WARN 2
239#define DTS_WARN_HYST 3
240#define W83795_REG_DTS_EXT(index) (0xB2 + (index))
241
242#define SETUP_PWM_DEFAULT 0
243#define SETUP_PWM_UPTIME 1
244#define SETUP_PWM_DOWNTIME 2
245#define W83795_REG_SETUP_PWM(index) (0x20C + (index))
246
247static inline u16 in_from_reg(u8 index, u16 val)
248{
Jean Delvare49c73472010-10-28 20:31:48 +0200249 /* 3VDD, 3VSB and VBAT: 6 mV/bit; other inputs: 2 mV/bit */
250 if (index >= 12 && index <= 14)
Wei Song792d3762010-10-28 20:31:44 +0200251 return val * 6;
252 else
253 return val * 2;
254}
255
256static inline u16 in_to_reg(u8 index, u16 val)
257{
Jean Delvare49c73472010-10-28 20:31:48 +0200258 if (index >= 12 && index <= 14)
Wei Song792d3762010-10-28 20:31:44 +0200259 return val / 6;
260 else
261 return val / 2;
262}
263
264static inline unsigned long fan_from_reg(u16 val)
265{
Jean Delvare6c82b2f2010-10-28 20:31:46 +0200266 if ((val == 0xfff) || (val == 0))
Wei Song792d3762010-10-28 20:31:44 +0200267 return 0;
268 return 1350000UL / val;
269}
270
271static inline u16 fan_to_reg(long rpm)
272{
273 if (rpm <= 0)
274 return 0x0fff;
275 return SENSORS_LIMIT((1350000 + (rpm >> 1)) / rpm, 1, 0xffe);
276}
277
278static inline unsigned long time_from_reg(u8 reg)
279{
280 return reg * 100;
281}
282
283static inline u8 time_to_reg(unsigned long val)
284{
285 return SENSORS_LIMIT((val + 50) / 100, 0, 0xff);
286}
287
288static inline long temp_from_reg(s8 reg)
289{
290 return reg * 1000;
291}
292
293static inline s8 temp_to_reg(long val, s8 min, s8 max)
294{
Jean Delvaredd127f52010-10-28 20:31:47 +0200295 return SENSORS_LIMIT(val / 1000, min, max);
Wei Song792d3762010-10-28 20:31:44 +0200296}
297
Jean Delvare01879a82010-10-28 20:31:47 +0200298static const u16 pwm_freq_cksel0[16] = {
299 1024, 512, 341, 256, 205, 171, 146, 128,
300 85, 64, 32, 16, 8, 4, 2, 1
301};
302
303static unsigned int pwm_freq_from_reg(u8 reg, u16 clkin)
304{
305 unsigned long base_clock;
306
307 if (reg & 0x80) {
308 base_clock = clkin * 1000 / ((clkin == 48000) ? 384 : 256);
309 return base_clock / ((reg & 0x7f) + 1);
310 } else
311 return pwm_freq_cksel0[reg & 0x0f];
312}
313
314static u8 pwm_freq_to_reg(unsigned long val, u16 clkin)
315{
316 unsigned long base_clock;
317 u8 reg0, reg1;
318 unsigned long best0, best1;
319
320 /* Best fit for cksel = 0 */
321 for (reg0 = 0; reg0 < ARRAY_SIZE(pwm_freq_cksel0) - 1; reg0++) {
322 if (val > (pwm_freq_cksel0[reg0] +
323 pwm_freq_cksel0[reg0 + 1]) / 2)
324 break;
325 }
326 if (val < 375) /* cksel = 1 can't beat this */
327 return reg0;
328 best0 = pwm_freq_cksel0[reg0];
329
330 /* Best fit for cksel = 1 */
331 base_clock = clkin * 1000 / ((clkin == 48000) ? 384 : 256);
332 reg1 = SENSORS_LIMIT(DIV_ROUND_CLOSEST(base_clock, val), 1, 128);
333 best1 = base_clock / reg1;
334 reg1 = 0x80 | (reg1 - 1);
335
336 /* Choose the closest one */
337 if (abs(val - best0) > abs(val - best1))
338 return reg1;
339 else
340 return reg0;
341}
Wei Song792d3762010-10-28 20:31:44 +0200342
343enum chip_types {w83795g, w83795adg};
344
345struct w83795_data {
346 struct device *hwmon_dev;
347 struct mutex update_lock;
348 unsigned long last_updated; /* In jiffies */
349 enum chip_types chip_type;
350
351 u8 bank;
352
353 u32 has_in; /* Enable monitor VIN or not */
Jean Delvare0e256012010-10-28 20:31:46 +0200354 u8 has_dyn_in; /* Only in2-0 can have this */
Wei Song792d3762010-10-28 20:31:44 +0200355 u16 in[21][3]; /* Register value, read/high/low */
356 u8 in_lsb[10][3]; /* LSB Register value, high/low */
357 u8 has_gain; /* has gain: in17-20 * 8 */
358
359 u16 has_fan; /* Enable fan14-1 or not */
360 u16 fan[14]; /* Register value combine */
361 u16 fan_min[14]; /* Register value combine */
362
363 u8 has_temp; /* Enable monitor temp6-1 or not */
Jean Delvaredd127f52010-10-28 20:31:47 +0200364 s8 temp[6][5]; /* current, crit, crit_hyst, warn, warn_hyst */
Wei Song792d3762010-10-28 20:31:44 +0200365 u8 temp_read_vrlsb[6];
366 u8 temp_mode; /* bit 0: TR mode, bit 1: TD mode */
367 u8 temp_src[3]; /* Register value */
368
369 u8 enable_dts; /* Enable PECI and SB-TSI,
370 * bit 0: =1 enable, =0 disable,
371 * bit 1: =1 AMD SB-TSI, =0 Intel PECI */
372 u8 has_dts; /* Enable monitor DTS temp */
Jean Delvaredd127f52010-10-28 20:31:47 +0200373 s8 dts[8]; /* Register value */
Wei Song792d3762010-10-28 20:31:44 +0200374 u8 dts_read_vrlsb[8]; /* Register value */
Jean Delvaredd127f52010-10-28 20:31:47 +0200375 s8 dts_ext[4]; /* Register value */
Wei Song792d3762010-10-28 20:31:44 +0200376
377 u8 has_pwm; /* 795g supports 8 pwm, 795adg only supports 2,
378 * no config register, only affected by chip
379 * type */
380 u8 pwm[8][5]; /* Register value, output, start, non stop, stop
Jean Delvare01879a82010-10-28 20:31:47 +0200381 * time, freq */
382 u16 clkin; /* CLKIN frequency in kHz */
Wei Song792d3762010-10-28 20:31:44 +0200383 u8 pwm_fcms[2]; /* Register value */
384 u8 pwm_tfmr[6]; /* Register value */
385 u8 pwm_fomc; /* Register value */
386
387 u16 target_speed[8]; /* Register value, target speed for speed
388 * cruise */
389 u8 tol_speed; /* tolerance of target speed */
390 u8 pwm_temp[6][4]; /* TTTI, CTFS, HCT, HOT */
391 u8 sf4_reg[6][2][7]; /* 6 temp, temp/dcpwm, 7 registers */
392
393 u8 setup_pwm[3]; /* Register value */
394
395 u8 alarms[6]; /* Register value */
396 u8 beeps[6]; /* Register value */
Wei Song792d3762010-10-28 20:31:44 +0200397
398 char valid;
399};
400
401/*
402 * Hardware access
Jean Delvareb2469f42010-10-28 20:31:45 +0200403 * We assume that nobdody can change the bank outside the driver.
Wei Song792d3762010-10-28 20:31:44 +0200404 */
405
Jean Delvareb2469f42010-10-28 20:31:45 +0200406/* Must be called with data->update_lock held, except during initialization */
407static int w83795_set_bank(struct i2c_client *client, u8 bank)
Wei Song792d3762010-10-28 20:31:44 +0200408{
409 struct w83795_data *data = i2c_get_clientdata(client);
Jean Delvareb2469f42010-10-28 20:31:45 +0200410 int err;
Wei Song792d3762010-10-28 20:31:44 +0200411
Jean Delvareb2469f42010-10-28 20:31:45 +0200412 /* If the same bank is already set, nothing to do */
413 if ((data->bank & 0x07) == bank)
414 return 0;
415
416 /* Change to new bank, preserve all other bits */
417 bank |= data->bank & ~0x07;
418 err = i2c_smbus_write_byte_data(client, W83795_REG_BANKSEL, bank);
419 if (err < 0) {
420 dev_err(&client->dev,
421 "Failed to set bank to %d, err %d\n",
422 (int)bank, err);
423 return err;
Wei Song792d3762010-10-28 20:31:44 +0200424 }
Jean Delvareb2469f42010-10-28 20:31:45 +0200425 data->bank = bank;
426
427 return 0;
428}
429
430/* Must be called with data->update_lock held, except during initialization */
431static u8 w83795_read(struct i2c_client *client, u16 reg)
432{
433 int err;
434
435 err = w83795_set_bank(client, reg >> 8);
436 if (err < 0)
437 return 0x00; /* Arbitrary */
438
439 err = i2c_smbus_read_byte_data(client, reg & 0xff);
440 if (err < 0) {
441 dev_err(&client->dev,
442 "Failed to read from register 0x%03x, err %d\n",
443 (int)reg, err);
444 return 0x00; /* Arbitrary */
445 }
446 return err;
Wei Song792d3762010-10-28 20:31:44 +0200447}
448
449/* Must be called with data->update_lock held, except during initialization */
450static int w83795_write(struct i2c_client *client, u16 reg, u8 value)
451{
Jean Delvareb2469f42010-10-28 20:31:45 +0200452 int err;
Wei Song792d3762010-10-28 20:31:44 +0200453
Jean Delvareb2469f42010-10-28 20:31:45 +0200454 err = w83795_set_bank(client, reg >> 8);
455 if (err < 0)
456 return err;
Wei Song792d3762010-10-28 20:31:44 +0200457
Jean Delvareb2469f42010-10-28 20:31:45 +0200458 err = i2c_smbus_write_byte_data(client, reg & 0xff, value);
459 if (err < 0)
460 dev_err(&client->dev,
461 "Failed to write to register 0x%03x, err %d\n",
462 (int)reg, err);
463 return err;
Wei Song792d3762010-10-28 20:31:44 +0200464}
465
466static struct w83795_data *w83795_update_device(struct device *dev)
467{
468 struct i2c_client *client = to_i2c_client(dev);
469 struct w83795_data *data = i2c_get_clientdata(client);
470 u16 tmp;
471 int i;
472
473 mutex_lock(&data->update_lock);
474
475 if (!(time_after(jiffies, data->last_updated + HZ * 2)
476 || !data->valid))
477 goto END;
478
479 /* Update the voltages value */
480 for (i = 0; i < ARRAY_SIZE(data->in); i++) {
481 if (!(data->has_in & (1 << i)))
482 continue;
483 tmp = w83795_read(client, W83795_REG_IN[i][IN_READ]) << 2;
Jean Delvarea654b9d42010-10-28 20:31:47 +0200484 tmp |= w83795_read(client, W83795_REG_VRLSB) >> 6;
Wei Song792d3762010-10-28 20:31:44 +0200485 data->in[i][IN_READ] = tmp;
486 }
487
Jean Delvare0e256012010-10-28 20:31:46 +0200488 /* in0-2 can have dynamic limits (W83795G only) */
489 if (data->has_dyn_in) {
490 u8 lsb_max = w83795_read(client, IN_LSB_REG(0, IN_MAX));
491 u8 lsb_low = w83795_read(client, IN_LSB_REG(0, IN_LOW));
492
493 for (i = 0; i < 3; i++) {
494 if (!(data->has_dyn_in & (1 << i)))
495 continue;
496 data->in[i][IN_MAX] =
497 w83795_read(client, W83795_REG_IN[i][IN_MAX]);
498 data->in[i][IN_LOW] =
499 w83795_read(client, W83795_REG_IN[i][IN_LOW]);
500 data->in_lsb[i][IN_MAX] = (lsb_max >> (2 * i)) & 0x03;
501 data->in_lsb[i][IN_LOW] = (lsb_low >> (2 * i)) & 0x03;
502 }
503 }
504
Wei Song792d3762010-10-28 20:31:44 +0200505 /* Update fan */
506 for (i = 0; i < ARRAY_SIZE(data->fan); i++) {
507 if (!(data->has_fan & (1 << i)))
508 continue;
509 data->fan[i] = w83795_read(client, W83795_REG_FAN(i)) << 4;
510 data->fan[i] |=
Jean Delvare6c82b2f2010-10-28 20:31:46 +0200511 (w83795_read(client, W83795_REG_VRLSB) >> 4) & 0x0F;
Wei Song792d3762010-10-28 20:31:44 +0200512 }
513
514 /* Update temperature */
515 for (i = 0; i < ARRAY_SIZE(data->temp); i++) {
516 /* even stop monitor, register still keep value, just read out
517 * it */
518 if (!(data->has_temp & (1 << i))) {
519 data->temp[i][TEMP_READ] = 0;
520 data->temp_read_vrlsb[i] = 0;
521 continue;
522 }
523 data->temp[i][TEMP_READ] =
524 w83795_read(client, W83795_REG_TEMP[i][TEMP_READ]);
525 data->temp_read_vrlsb[i] =
526 w83795_read(client, W83795_REG_VRLSB);
527 }
528
529 /* Update dts temperature */
530 if (data->enable_dts != 0) {
531 for (i = 0; i < ARRAY_SIZE(data->dts); i++) {
532 if (!(data->has_dts & (1 << i)))
533 continue;
534 data->dts[i] =
535 w83795_read(client, W83795_REG_DTS(i));
536 data->dts_read_vrlsb[i] =
537 w83795_read(client, W83795_REG_VRLSB);
538 }
539 }
540
541 /* Update pwm output */
542 for (i = 0; i < data->has_pwm; i++) {
543 data->pwm[i][PWM_OUTPUT] =
544 w83795_read(client, W83795_REG_PWM(i, PWM_OUTPUT));
545 }
546
547 /* update alarm */
548 for (i = 0; i < ALARM_BEEP_REG_NUM; i++)
549 data->alarms[i] = w83795_read(client, W83795_REG_ALARM(i));
550
551 data->last_updated = jiffies;
552 data->valid = 1;
553
554END:
555 mutex_unlock(&data->update_lock);
556 return data;
557}
558
559/*
560 * Sysfs attributes
561 */
562
563#define ALARM_STATUS 0
564#define BEEP_ENABLE 1
565static ssize_t
566show_alarm_beep(struct device *dev, struct device_attribute *attr, char *buf)
567{
568 struct w83795_data *data = w83795_update_device(dev);
569 struct sensor_device_attribute_2 *sensor_attr =
570 to_sensor_dev_attr_2(attr);
571 int nr = sensor_attr->nr;
572 int index = sensor_attr->index >> 3;
573 int bit = sensor_attr->index & 0x07;
574 u8 val;
575
576 if (ALARM_STATUS == nr) {
577 val = (data->alarms[index] >> (bit)) & 1;
578 } else { /* BEEP_ENABLE */
579 val = (data->beeps[index] >> (bit)) & 1;
580 }
581
582 return sprintf(buf, "%u\n", val);
583}
584
585static ssize_t
586store_beep(struct device *dev, struct device_attribute *attr,
587 const char *buf, size_t count)
588{
589 struct i2c_client *client = to_i2c_client(dev);
590 struct w83795_data *data = i2c_get_clientdata(client);
591 struct sensor_device_attribute_2 *sensor_attr =
592 to_sensor_dev_attr_2(attr);
593 int index = sensor_attr->index >> 3;
594 int shift = sensor_attr->index & 0x07;
595 u8 beep_bit = 1 << shift;
596 unsigned long val;
597
598 if (strict_strtoul(buf, 10, &val) < 0)
599 return -EINVAL;
600 if (val != 0 && val != 1)
601 return -EINVAL;
602
603 mutex_lock(&data->update_lock);
604 data->beeps[index] = w83795_read(client, W83795_REG_BEEP(index));
605 data->beeps[index] &= ~beep_bit;
606 data->beeps[index] |= val << shift;
607 w83795_write(client, W83795_REG_BEEP(index), data->beeps[index]);
608 mutex_unlock(&data->update_lock);
609
610 return count;
611}
612
Wei Song792d3762010-10-28 20:31:44 +0200613/* Write any value to clear chassis alarm */
614static ssize_t
615store_chassis_clear(struct device *dev,
616 struct device_attribute *attr, const char *buf,
617 size_t count)
618{
619 struct i2c_client *client = to_i2c_client(dev);
620 struct w83795_data *data = i2c_get_clientdata(client);
621 u8 val;
622
623 mutex_lock(&data->update_lock);
624 val = w83795_read(client, W83795_REG_CLR_CHASSIS);
625 val |= 0x80;
626 w83795_write(client, W83795_REG_CLR_CHASSIS, val);
627 mutex_unlock(&data->update_lock);
628 return count;
629}
630
631#define FAN_INPUT 0
632#define FAN_MIN 1
633static ssize_t
634show_fan(struct device *dev, struct device_attribute *attr, char *buf)
635{
636 struct sensor_device_attribute_2 *sensor_attr =
637 to_sensor_dev_attr_2(attr);
638 int nr = sensor_attr->nr;
639 int index = sensor_attr->index;
640 struct w83795_data *data = w83795_update_device(dev);
641 u16 val;
642
643 if (FAN_INPUT == nr)
644 val = data->fan[index] & 0x0fff;
645 else
646 val = data->fan_min[index] & 0x0fff;
647
648 return sprintf(buf, "%lu\n", fan_from_reg(val));
649}
650
651static ssize_t
652store_fan_min(struct device *dev, struct device_attribute *attr,
653 const char *buf, size_t count)
654{
655 struct sensor_device_attribute_2 *sensor_attr =
656 to_sensor_dev_attr_2(attr);
657 int index = sensor_attr->index;
658 struct i2c_client *client = to_i2c_client(dev);
659 struct w83795_data *data = i2c_get_clientdata(client);
660 unsigned long val;
661
662 if (strict_strtoul(buf, 10, &val))
663 return -EINVAL;
664 val = fan_to_reg(val);
665
666 mutex_lock(&data->update_lock);
667 data->fan_min[index] = val;
668 w83795_write(client, W83795_REG_FAN_MIN_HL(index), (val >> 4) & 0xff);
669 val &= 0x0f;
Jean Delvare7eb8d502010-10-28 20:31:47 +0200670 if (index & 1) {
Wei Song792d3762010-10-28 20:31:44 +0200671 val <<= 4;
672 val |= w83795_read(client, W83795_REG_FAN_MIN_LSB(index))
673 & 0x0f;
674 } else {
675 val |= w83795_read(client, W83795_REG_FAN_MIN_LSB(index))
676 & 0xf0;
677 }
678 w83795_write(client, W83795_REG_FAN_MIN_LSB(index), val & 0xff);
679 mutex_unlock(&data->update_lock);
680
681 return count;
682}
683
684static ssize_t
685show_pwm(struct device *dev, struct device_attribute *attr, char *buf)
686{
687 struct w83795_data *data = w83795_update_device(dev);
688 struct sensor_device_attribute_2 *sensor_attr =
689 to_sensor_dev_attr_2(attr);
690 int nr = sensor_attr->nr;
691 int index = sensor_attr->index;
Jean Delvare01879a82010-10-28 20:31:47 +0200692 unsigned int val;
Wei Song792d3762010-10-28 20:31:44 +0200693
694 switch (nr) {
695 case PWM_STOP_TIME:
696 val = time_from_reg(data->pwm[index][nr]);
697 break;
Jean Delvare01879a82010-10-28 20:31:47 +0200698 case PWM_FREQ:
699 val = pwm_freq_from_reg(data->pwm[index][nr], data->clkin);
Wei Song792d3762010-10-28 20:31:44 +0200700 break;
701 default:
702 val = data->pwm[index][nr];
703 break;
704 }
705
706 return sprintf(buf, "%u\n", val);
707}
708
709static ssize_t
710store_pwm(struct device *dev, struct device_attribute *attr,
711 const char *buf, size_t count)
712{
713 struct i2c_client *client = to_i2c_client(dev);
714 struct w83795_data *data = i2c_get_clientdata(client);
715 struct sensor_device_attribute_2 *sensor_attr =
716 to_sensor_dev_attr_2(attr);
717 int nr = sensor_attr->nr;
718 int index = sensor_attr->index;
719 unsigned long val;
Wei Song792d3762010-10-28 20:31:44 +0200720
721 if (strict_strtoul(buf, 10, &val) < 0)
722 return -EINVAL;
723
724 mutex_lock(&data->update_lock);
725 switch (nr) {
726 case PWM_STOP_TIME:
727 val = time_to_reg(val);
728 break;
Jean Delvare01879a82010-10-28 20:31:47 +0200729 case PWM_FREQ:
730 val = pwm_freq_to_reg(val, data->clkin);
Wei Song792d3762010-10-28 20:31:44 +0200731 break;
732 default:
733 val = SENSORS_LIMIT(val, 0, 0xff);
734 break;
735 }
736 w83795_write(client, W83795_REG_PWM(index, nr), val);
Jean Delvare01879a82010-10-28 20:31:47 +0200737 data->pwm[index][nr] = val;
Wei Song792d3762010-10-28 20:31:44 +0200738 mutex_unlock(&data->update_lock);
739 return count;
Wei Song792d3762010-10-28 20:31:44 +0200740}
741
742static ssize_t
743show_pwm_enable(struct device *dev, struct device_attribute *attr, char *buf)
744{
745 struct sensor_device_attribute_2 *sensor_attr =
746 to_sensor_dev_attr_2(attr);
747 struct i2c_client *client = to_i2c_client(dev);
748 struct w83795_data *data = i2c_get_clientdata(client);
749 int index = sensor_attr->index;
750 u8 tmp;
751
752 if (1 == (data->pwm_fcms[0] & (1 << index))) {
753 tmp = 2;
754 goto out;
755 }
756 for (tmp = 0; tmp < 6; tmp++) {
757 if (data->pwm_tfmr[tmp] & (1 << index)) {
758 tmp = 3;
759 goto out;
760 }
761 }
762 if (data->pwm_fomc & (1 << index))
763 tmp = 0;
764 else
765 tmp = 1;
766
767out:
768 return sprintf(buf, "%u\n", tmp);
769}
770
771static ssize_t
772store_pwm_enable(struct device *dev, struct device_attribute *attr,
773 const char *buf, size_t count)
774{
775 struct i2c_client *client = to_i2c_client(dev);
776 struct w83795_data *data = i2c_get_clientdata(client);
777 struct sensor_device_attribute_2 *sensor_attr =
778 to_sensor_dev_attr_2(attr);
779 int index = sensor_attr->index;
780 unsigned long val;
781 int i;
782
783 if (strict_strtoul(buf, 10, &val) < 0)
784 return -EINVAL;
785 if (val > 2)
786 return -EINVAL;
787
788 mutex_lock(&data->update_lock);
789 switch (val) {
790 case 0:
791 case 1:
792 data->pwm_fcms[0] &= ~(1 << index);
793 w83795_write(client, W83795_REG_FCMS1, data->pwm_fcms[0]);
794 for (i = 0; i < 6; i++) {
795 data->pwm_tfmr[i] &= ~(1 << index);
796 w83795_write(client, W83795_REG_TFMR(i),
797 data->pwm_tfmr[i]);
798 }
799 data->pwm_fomc |= 1 << index;
800 data->pwm_fomc ^= val << index;
801 w83795_write(client, W83795_REG_FOMC, data->pwm_fomc);
802 break;
803 case 2:
804 data->pwm_fcms[0] |= (1 << index);
805 w83795_write(client, W83795_REG_FCMS1, data->pwm_fcms[0]);
806 break;
807 }
808 mutex_unlock(&data->update_lock);
809 return count;
Wei Song792d3762010-10-28 20:31:44 +0200810}
811
812static ssize_t
813show_temp_src(struct device *dev, struct device_attribute *attr, char *buf)
814{
815 struct sensor_device_attribute_2 *sensor_attr =
816 to_sensor_dev_attr_2(attr);
817 struct i2c_client *client = to_i2c_client(dev);
818 struct w83795_data *data = i2c_get_clientdata(client);
819 int index = sensor_attr->index;
820 u8 val = index / 2;
821 u8 tmp = data->temp_src[val];
822
Jean Delvare7eb8d502010-10-28 20:31:47 +0200823 if (index & 1)
Wei Song792d3762010-10-28 20:31:44 +0200824 val = 4;
825 else
826 val = 0;
827 tmp >>= val;
828 tmp &= 0x0f;
829
830 return sprintf(buf, "%u\n", tmp);
831}
832
833static ssize_t
834store_temp_src(struct device *dev, struct device_attribute *attr,
835 const char *buf, size_t count)
836{
837 struct i2c_client *client = to_i2c_client(dev);
838 struct w83795_data *data = i2c_get_clientdata(client);
839 struct sensor_device_attribute_2 *sensor_attr =
840 to_sensor_dev_attr_2(attr);
841 int index = sensor_attr->index;
842 unsigned long tmp;
843 u8 val = index / 2;
844
845 if (strict_strtoul(buf, 10, &tmp) < 0)
846 return -EINVAL;
847 tmp = SENSORS_LIMIT(tmp, 0, 15);
848
849 mutex_lock(&data->update_lock);
Jean Delvare7eb8d502010-10-28 20:31:47 +0200850 if (index & 1) {
Wei Song792d3762010-10-28 20:31:44 +0200851 tmp <<= 4;
852 data->temp_src[val] &= 0x0f;
853 } else {
854 data->temp_src[val] &= 0xf0;
855 }
856 data->temp_src[val] |= tmp;
857 w83795_write(client, W83795_REG_TSS(val), data->temp_src[val]);
858 mutex_unlock(&data->update_lock);
859
860 return count;
861}
862
863#define TEMP_PWM_ENABLE 0
864#define TEMP_PWM_FAN_MAP 1
865static ssize_t
866show_temp_pwm_enable(struct device *dev, struct device_attribute *attr,
867 char *buf)
868{
869 struct i2c_client *client = to_i2c_client(dev);
870 struct w83795_data *data = i2c_get_clientdata(client);
871 struct sensor_device_attribute_2 *sensor_attr =
872 to_sensor_dev_attr_2(attr);
873 int nr = sensor_attr->nr;
874 int index = sensor_attr->index;
875 u8 tmp = 0xff;
876
877 switch (nr) {
878 case TEMP_PWM_ENABLE:
879 tmp = (data->pwm_fcms[1] >> index) & 1;
880 if (tmp)
881 tmp = 4;
882 else
883 tmp = 3;
884 break;
885 case TEMP_PWM_FAN_MAP:
886 tmp = data->pwm_tfmr[index];
887 break;
888 }
889
890 return sprintf(buf, "%u\n", tmp);
891}
892
893static ssize_t
894store_temp_pwm_enable(struct device *dev, struct device_attribute *attr,
895 const char *buf, size_t count)
896{
897 struct i2c_client *client = to_i2c_client(dev);
898 struct w83795_data *data = i2c_get_clientdata(client);
899 struct sensor_device_attribute_2 *sensor_attr =
900 to_sensor_dev_attr_2(attr);
901 int nr = sensor_attr->nr;
902 int index = sensor_attr->index;
903 unsigned long tmp;
904
905 if (strict_strtoul(buf, 10, &tmp) < 0)
906 return -EINVAL;
907
908 switch (nr) {
909 case TEMP_PWM_ENABLE:
910 if ((tmp != 3) && (tmp != 4))
911 return -EINVAL;
912 tmp -= 3;
913 mutex_lock(&data->update_lock);
914 data->pwm_fcms[1] &= ~(1 << index);
915 data->pwm_fcms[1] |= tmp << index;
916 w83795_write(client, W83795_REG_FCMS2, data->pwm_fcms[1]);
917 mutex_unlock(&data->update_lock);
918 break;
919 case TEMP_PWM_FAN_MAP:
920 mutex_lock(&data->update_lock);
921 tmp = SENSORS_LIMIT(tmp, 0, 0xff);
922 w83795_write(client, W83795_REG_TFMR(index), tmp);
923 data->pwm_tfmr[index] = tmp;
924 mutex_unlock(&data->update_lock);
925 break;
926 }
927 return count;
928}
929
930#define FANIN_TARGET 0
931#define FANIN_TOL 1
932static ssize_t
933show_fanin(struct device *dev, struct device_attribute *attr, char *buf)
934{
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;
941 u16 tmp = 0;
942
943 switch (nr) {
944 case FANIN_TARGET:
945 tmp = fan_from_reg(data->target_speed[index]);
946 break;
947 case FANIN_TOL:
948 tmp = data->tol_speed;
949 break;
950 }
951
952 return sprintf(buf, "%u\n", tmp);
953}
954
955static ssize_t
956store_fanin(struct device *dev, struct device_attribute *attr,
957 const char *buf, size_t count)
958{
959 struct i2c_client *client = to_i2c_client(dev);
960 struct w83795_data *data = i2c_get_clientdata(client);
961 struct sensor_device_attribute_2 *sensor_attr =
962 to_sensor_dev_attr_2(attr);
963 int nr = sensor_attr->nr;
964 int index = sensor_attr->index;
965 unsigned long val;
966
967 if (strict_strtoul(buf, 10, &val) < 0)
968 return -EINVAL;
969
970 mutex_lock(&data->update_lock);
971 switch (nr) {
972 case FANIN_TARGET:
973 val = fan_to_reg(SENSORS_LIMIT(val, 0, 0xfff));
974 w83795_write(client, W83795_REG_FTSH(index), (val >> 4) & 0xff);
975 w83795_write(client, W83795_REG_FTSL(index), (val << 4) & 0xf0);
976 data->target_speed[index] = val;
977 break;
978 case FANIN_TOL:
979 val = SENSORS_LIMIT(val, 0, 0x3f);
980 w83795_write(client, W83795_REG_TFTS, val);
981 data->tol_speed = val;
982 break;
983 }
984 mutex_unlock(&data->update_lock);
985
986 return count;
987}
988
989
990static ssize_t
991show_temp_pwm(struct device *dev, struct device_attribute *attr, char *buf)
992{
993 struct i2c_client *client = to_i2c_client(dev);
994 struct w83795_data *data = i2c_get_clientdata(client);
995 struct sensor_device_attribute_2 *sensor_attr =
996 to_sensor_dev_attr_2(attr);
997 int nr = sensor_attr->nr;
998 int index = sensor_attr->index;
999 long tmp = temp_from_reg(data->pwm_temp[index][nr]);
1000
1001 return sprintf(buf, "%ld\n", tmp);
1002}
1003
1004static ssize_t
1005store_temp_pwm(struct device *dev, struct device_attribute *attr,
1006 const char *buf, size_t count)
1007{
1008 struct i2c_client *client = to_i2c_client(dev);
1009 struct w83795_data *data = i2c_get_clientdata(client);
1010 struct sensor_device_attribute_2 *sensor_attr =
1011 to_sensor_dev_attr_2(attr);
1012 int nr = sensor_attr->nr;
1013 int index = sensor_attr->index;
1014 unsigned long val;
1015 u8 tmp;
1016
1017 if (strict_strtoul(buf, 10, &val) < 0)
1018 return -EINVAL;
1019 val /= 1000;
1020
1021 mutex_lock(&data->update_lock);
1022 switch (nr) {
1023 case TEMP_PWM_TTTI:
1024 val = SENSORS_LIMIT(val, 0, 0x7f);
1025 w83795_write(client, W83795_REG_TTTI(index), val);
1026 break;
1027 case TEMP_PWM_CTFS:
1028 val = SENSORS_LIMIT(val, 0, 0x7f);
1029 w83795_write(client, W83795_REG_CTFS(index), val);
1030 break;
1031 case TEMP_PWM_HCT:
1032 val = SENSORS_LIMIT(val, 0, 0x0f);
1033 tmp = w83795_read(client, W83795_REG_HT(index));
1034 tmp &= 0x0f;
1035 tmp |= (val << 4) & 0xf0;
1036 w83795_write(client, W83795_REG_HT(index), tmp);
1037 break;
1038 case TEMP_PWM_HOT:
1039 val = SENSORS_LIMIT(val, 0, 0x0f);
1040 tmp = w83795_read(client, W83795_REG_HT(index));
1041 tmp &= 0xf0;
1042 tmp |= val & 0x0f;
1043 w83795_write(client, W83795_REG_HT(index), tmp);
1044 break;
1045 }
1046 data->pwm_temp[index][nr] = val;
1047 mutex_unlock(&data->update_lock);
1048
1049 return count;
1050}
1051
1052static ssize_t
1053show_sf4_pwm(struct device *dev, struct device_attribute *attr, char *buf)
1054{
1055 struct i2c_client *client = to_i2c_client(dev);
1056 struct w83795_data *data = i2c_get_clientdata(client);
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;
1061
1062 return sprintf(buf, "%u\n", data->sf4_reg[index][SF4_PWM][nr]);
1063}
1064
1065static ssize_t
1066store_sf4_pwm(struct device *dev, struct device_attribute *attr,
1067 const char *buf, size_t count)
1068{
1069 struct i2c_client *client = to_i2c_client(dev);
1070 struct w83795_data *data = i2c_get_clientdata(client);
1071 struct sensor_device_attribute_2 *sensor_attr =
1072 to_sensor_dev_attr_2(attr);
1073 int nr = sensor_attr->nr;
1074 int index = sensor_attr->index;
1075 unsigned long val;
1076
1077 if (strict_strtoul(buf, 10, &val) < 0)
1078 return -EINVAL;
1079
1080 mutex_lock(&data->update_lock);
1081 w83795_write(client, W83795_REG_SF4_PWM(index, nr), val);
1082 data->sf4_reg[index][SF4_PWM][nr] = val;
1083 mutex_unlock(&data->update_lock);
1084
1085 return count;
1086}
1087
1088static ssize_t
1089show_sf4_temp(struct device *dev, struct device_attribute *attr, char *buf)
1090{
1091 struct i2c_client *client = to_i2c_client(dev);
1092 struct w83795_data *data = i2c_get_clientdata(client);
1093 struct sensor_device_attribute_2 *sensor_attr =
1094 to_sensor_dev_attr_2(attr);
1095 int nr = sensor_attr->nr;
1096 int index = sensor_attr->index;
1097
1098 return sprintf(buf, "%u\n",
1099 (data->sf4_reg[index][SF4_TEMP][nr]) * 1000);
1100}
1101
1102static ssize_t
1103store_sf4_temp(struct device *dev, struct device_attribute *attr,
1104 const char *buf, size_t count)
1105{
1106 struct i2c_client *client = to_i2c_client(dev);
1107 struct w83795_data *data = i2c_get_clientdata(client);
1108 struct sensor_device_attribute_2 *sensor_attr =
1109 to_sensor_dev_attr_2(attr);
1110 int nr = sensor_attr->nr;
1111 int index = sensor_attr->index;
1112 unsigned long val;
1113
1114 if (strict_strtoul(buf, 10, &val) < 0)
1115 return -EINVAL;
1116 val /= 1000;
1117
1118 mutex_lock(&data->update_lock);
1119 w83795_write(client, W83795_REG_SF4_TEMP(index, nr), val);
1120 data->sf4_reg[index][SF4_TEMP][nr] = val;
1121 mutex_unlock(&data->update_lock);
1122
1123 return count;
1124}
1125
1126
1127static ssize_t
1128show_temp(struct device *dev, struct device_attribute *attr, char *buf)
1129{
1130 struct sensor_device_attribute_2 *sensor_attr =
1131 to_sensor_dev_attr_2(attr);
1132 int nr = sensor_attr->nr;
1133 int index = sensor_attr->index;
1134 struct w83795_data *data = w83795_update_device(dev);
Jean Delvaredd127f52010-10-28 20:31:47 +02001135 long temp = temp_from_reg(data->temp[index][nr]);
Wei Song792d3762010-10-28 20:31:44 +02001136
1137 if (TEMP_READ == nr)
Jean Delvarea654b9d42010-10-28 20:31:47 +02001138 temp += (data->temp_read_vrlsb[index] >> 6) * 250;
Wei Song792d3762010-10-28 20:31:44 +02001139 return sprintf(buf, "%ld\n", temp);
1140}
1141
1142static ssize_t
1143store_temp(struct device *dev, struct device_attribute *attr,
1144 const char *buf, size_t count)
1145{
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;
1150 struct i2c_client *client = to_i2c_client(dev);
1151 struct w83795_data *data = i2c_get_clientdata(client);
1152 long tmp;
1153
1154 if (strict_strtol(buf, 10, &tmp) < 0)
1155 return -EINVAL;
1156
1157 mutex_lock(&data->update_lock);
1158 data->temp[index][nr] = temp_to_reg(tmp, -128, 127);
1159 w83795_write(client, W83795_REG_TEMP[index][nr], data->temp[index][nr]);
1160 mutex_unlock(&data->update_lock);
1161 return count;
1162}
1163
1164
1165static ssize_t
1166show_dts_mode(struct device *dev, struct device_attribute *attr, char *buf)
1167{
1168 struct i2c_client *client = to_i2c_client(dev);
1169 struct w83795_data *data = i2c_get_clientdata(client);
1170 struct sensor_device_attribute_2 *sensor_attr =
1171 to_sensor_dev_attr_2(attr);
1172 int index = sensor_attr->index;
1173 u8 tmp;
1174
1175 if (data->enable_dts == 0)
1176 return sprintf(buf, "%d\n", 0);
1177
1178 if ((data->has_dts >> index) & 0x01) {
1179 if (data->enable_dts & 2)
1180 tmp = 5;
1181 else
1182 tmp = 6;
1183 } else {
1184 tmp = 0;
1185 }
1186
1187 return sprintf(buf, "%d\n", tmp);
1188}
1189
1190static ssize_t
1191show_dts(struct device *dev, struct device_attribute *attr, char *buf)
1192{
1193 struct sensor_device_attribute_2 *sensor_attr =
1194 to_sensor_dev_attr_2(attr);
1195 int index = sensor_attr->index;
1196 struct w83795_data *data = w83795_update_device(dev);
Jean Delvaredd127f52010-10-28 20:31:47 +02001197 long temp = temp_from_reg(data->dts[index]);
Wei Song792d3762010-10-28 20:31:44 +02001198
Jean Delvarea654b9d42010-10-28 20:31:47 +02001199 temp += (data->dts_read_vrlsb[index] >> 6) * 250;
Wei Song792d3762010-10-28 20:31:44 +02001200 return sprintf(buf, "%ld\n", temp);
1201}
1202
1203static ssize_t
1204show_dts_ext(struct device *dev, struct device_attribute *attr, char *buf)
1205{
1206 struct sensor_device_attribute_2 *sensor_attr =
1207 to_sensor_dev_attr_2(attr);
1208 int nr = sensor_attr->nr;
1209 struct i2c_client *client = to_i2c_client(dev);
1210 struct w83795_data *data = i2c_get_clientdata(client);
Jean Delvaredd127f52010-10-28 20:31:47 +02001211 long temp = temp_from_reg(data->dts_ext[nr]);
Wei Song792d3762010-10-28 20:31:44 +02001212
Wei Song792d3762010-10-28 20:31:44 +02001213 return sprintf(buf, "%ld\n", temp);
1214}
1215
1216static ssize_t
1217store_dts_ext(struct device *dev, struct device_attribute *attr,
1218 const char *buf, size_t count)
1219{
1220 struct sensor_device_attribute_2 *sensor_attr =
1221 to_sensor_dev_attr_2(attr);
1222 int nr = sensor_attr->nr;
1223 struct i2c_client *client = to_i2c_client(dev);
1224 struct w83795_data *data = i2c_get_clientdata(client);
1225 long tmp;
1226
1227 if (strict_strtol(buf, 10, &tmp) < 0)
1228 return -EINVAL;
1229
1230 mutex_lock(&data->update_lock);
1231 data->dts_ext[nr] = temp_to_reg(tmp, -128, 127);
1232 w83795_write(client, W83795_REG_DTS_EXT(nr), data->dts_ext[nr]);
1233 mutex_unlock(&data->update_lock);
1234 return count;
1235}
1236
1237
1238/*
1239 Type 3: Thermal diode
1240 Type 4: Thermistor
1241
1242 Temp5-6, default TR
1243 Temp1-4, default TD
1244*/
1245
1246static ssize_t
1247show_temp_mode(struct device *dev, struct device_attribute *attr, char *buf)
1248{
1249 struct i2c_client *client = to_i2c_client(dev);
1250 struct w83795_data *data = i2c_get_clientdata(client);
1251 struct sensor_device_attribute_2 *sensor_attr =
1252 to_sensor_dev_attr_2(attr);
1253 int index = sensor_attr->index;
1254 u8 tmp;
1255
1256 if (data->has_temp >> index & 0x01) {
1257 if (data->temp_mode >> index & 0x01)
1258 tmp = 3;
1259 else
1260 tmp = 4;
1261 } else {
1262 tmp = 0;
1263 }
1264
1265 return sprintf(buf, "%d\n", tmp);
1266}
1267
1268static ssize_t
1269store_temp_mode(struct device *dev, struct device_attribute *attr,
1270 const char *buf, size_t count)
1271{
1272 struct i2c_client *client = to_i2c_client(dev);
1273 struct w83795_data *data = i2c_get_clientdata(client);
1274 struct sensor_device_attribute_2 *sensor_attr =
1275 to_sensor_dev_attr_2(attr);
1276 int index = sensor_attr->index;
1277 unsigned long val;
1278 u8 tmp;
1279 u32 mask;
1280
1281 if (strict_strtoul(buf, 10, &val) < 0)
1282 return -EINVAL;
1283 if ((val != 4) && (val != 3))
1284 return -EINVAL;
1285 if ((index > 3) && (val == 3))
1286 return -EINVAL;
1287
1288 mutex_lock(&data->update_lock);
1289 if (val == 3) {
1290 val = TEMP_CTRL_TD;
1291 data->has_temp |= 1 << index;
1292 data->temp_mode |= 1 << index;
1293 } else if (val == 4) {
1294 val = TEMP_CTRL_TR;
1295 data->has_temp |= 1 << index;
1296 tmp = 1 << index;
1297 data->temp_mode &= ~tmp;
1298 }
1299
1300 if (index > 3)
1301 tmp = w83795_read(client, W83795_REG_TEMP_CTRL1);
1302 else
1303 tmp = w83795_read(client, W83795_REG_TEMP_CTRL2);
1304
1305 mask = 0x03 << W83795_REG_TEMP_CTRL[index][TEMP_CTRL_SHIFT];
1306 tmp &= ~mask;
1307 tmp |= W83795_REG_TEMP_CTRL[index][val];
1308
1309 mask = 1 << W83795_REG_TEMP_CTRL[index][TEMP_CTRL_HASIN_SHIFT];
1310 data->has_in &= ~mask;
1311
1312 if (index > 3)
1313 w83795_write(client, W83795_REG_TEMP_CTRL1, tmp);
1314 else
1315 w83795_write(client, W83795_REG_TEMP_CTRL2, tmp);
1316
1317 mutex_unlock(&data->update_lock);
1318 return count;
1319}
1320
1321
1322/* show/store VIN */
1323static ssize_t
1324show_in(struct device *dev, struct device_attribute *attr, char *buf)
1325{
1326 struct sensor_device_attribute_2 *sensor_attr =
1327 to_sensor_dev_attr_2(attr);
1328 int nr = sensor_attr->nr;
1329 int index = sensor_attr->index;
1330 struct w83795_data *data = w83795_update_device(dev);
1331 u16 val = data->in[index][nr];
1332 u8 lsb_idx;
1333
1334 switch (nr) {
1335 case IN_READ:
1336 /* calculate this value again by sensors as sensors3.conf */
1337 if ((index >= 17) &&
Jean Delvare6f9dfd82010-10-28 20:31:46 +02001338 !((data->has_gain >> (index - 17)) & 1))
Wei Song792d3762010-10-28 20:31:44 +02001339 val *= 8;
1340 break;
1341 case IN_MAX:
1342 case IN_LOW:
1343 lsb_idx = IN_LSB_SHIFT_IDX[index][IN_LSB_IDX];
1344 val <<= 2;
1345 val |= (data->in_lsb[lsb_idx][nr] >>
1346 IN_LSB_SHIFT_IDX[lsb_idx][IN_LSB_SHIFT]) & 0x03;
1347 if ((index >= 17) &&
Jean Delvare6f9dfd82010-10-28 20:31:46 +02001348 !((data->has_gain >> (index - 17)) & 1))
Wei Song792d3762010-10-28 20:31:44 +02001349 val *= 8;
1350 break;
1351 }
1352 val = in_from_reg(index, val);
1353
1354 return sprintf(buf, "%d\n", val);
1355}
1356
1357static ssize_t
1358store_in(struct device *dev, struct device_attribute *attr,
1359 const char *buf, size_t count)
1360{
1361 struct sensor_device_attribute_2 *sensor_attr =
1362 to_sensor_dev_attr_2(attr);
1363 int nr = sensor_attr->nr;
1364 int index = sensor_attr->index;
1365 struct i2c_client *client = to_i2c_client(dev);
1366 struct w83795_data *data = i2c_get_clientdata(client);
1367 unsigned long val;
1368 u8 tmp;
1369 u8 lsb_idx;
1370
1371 if (strict_strtoul(buf, 10, &val) < 0)
1372 return -EINVAL;
1373 val = in_to_reg(index, val);
1374
1375 if ((index >= 17) &&
Jean Delvare6f9dfd82010-10-28 20:31:46 +02001376 !((data->has_gain >> (index - 17)) & 1))
Wei Song792d3762010-10-28 20:31:44 +02001377 val /= 8;
1378 val = SENSORS_LIMIT(val, 0, 0x3FF);
1379 mutex_lock(&data->update_lock);
1380
1381 lsb_idx = IN_LSB_SHIFT_IDX[index][IN_LSB_IDX];
1382 tmp = w83795_read(client, IN_LSB_REG(lsb_idx, nr));
1383 tmp &= ~(0x03 << IN_LSB_SHIFT_IDX[index][IN_LSB_SHIFT]);
1384 tmp |= (val & 0x03) << IN_LSB_SHIFT_IDX[index][IN_LSB_SHIFT];
1385 w83795_write(client, IN_LSB_REG(lsb_idx, nr), tmp);
1386 data->in_lsb[lsb_idx][nr] = tmp;
1387
1388 tmp = (val >> 2) & 0xff;
1389 w83795_write(client, W83795_REG_IN[index][nr], tmp);
1390 data->in[index][nr] = tmp;
1391
1392 mutex_unlock(&data->update_lock);
1393 return count;
1394}
1395
1396
1397static ssize_t
1398show_sf_setup(struct device *dev, struct device_attribute *attr, char *buf)
1399{
1400 struct sensor_device_attribute_2 *sensor_attr =
1401 to_sensor_dev_attr_2(attr);
1402 int nr = sensor_attr->nr;
1403 struct i2c_client *client = to_i2c_client(dev);
1404 struct w83795_data *data = i2c_get_clientdata(client);
1405 u16 val = data->setup_pwm[nr];
1406
1407 switch (nr) {
1408 case SETUP_PWM_UPTIME:
1409 case SETUP_PWM_DOWNTIME:
1410 val = time_from_reg(val);
1411 break;
1412 }
1413
1414 return sprintf(buf, "%d\n", val);
1415}
1416
1417static ssize_t
1418store_sf_setup(struct device *dev, struct device_attribute *attr,
1419 const char *buf, size_t count)
1420{
1421 struct sensor_device_attribute_2 *sensor_attr =
1422 to_sensor_dev_attr_2(attr);
1423 int nr = sensor_attr->nr;
1424 struct i2c_client *client = to_i2c_client(dev);
1425 struct w83795_data *data = i2c_get_clientdata(client);
1426 unsigned long val;
1427
1428 if (strict_strtoul(buf, 10, &val) < 0)
1429 return -EINVAL;
1430
1431 switch (nr) {
1432 case SETUP_PWM_DEFAULT:
1433 val = SENSORS_LIMIT(val, 0, 0xff);
1434 break;
1435 case SETUP_PWM_UPTIME:
1436 case SETUP_PWM_DOWNTIME:
1437 val = time_to_reg(val);
1438 if (val == 0)
1439 return -EINVAL;
1440 break;
1441 }
1442
1443 mutex_lock(&data->update_lock);
1444 data->setup_pwm[nr] = val;
1445 w83795_write(client, W83795_REG_SETUP_PWM(nr), val);
1446 mutex_unlock(&data->update_lock);
1447 return count;
1448}
1449
1450
1451#define NOT_USED -1
1452
Jean Delvare0e256012010-10-28 20:31:46 +02001453/* Don't change the attribute order, _max and _min are accessed by index
1454 * somewhere else in the code */
Jean Delvare87df0da2010-10-28 20:31:45 +02001455#define SENSOR_ATTR_IN(index) { \
Wei Song792d3762010-10-28 20:31:44 +02001456 SENSOR_ATTR_2(in##index##_input, S_IRUGO, show_in, NULL, \
1457 IN_READ, index), \
1458 SENSOR_ATTR_2(in##index##_max, S_IRUGO | S_IWUSR, show_in, \
1459 store_in, IN_MAX, index), \
1460 SENSOR_ATTR_2(in##index##_min, S_IRUGO | S_IWUSR, show_in, \
1461 store_in, IN_LOW, index), \
1462 SENSOR_ATTR_2(in##index##_alarm, S_IRUGO, show_alarm_beep, \
1463 NULL, ALARM_STATUS, index + ((index > 14) ? 1 : 0)), \
1464 SENSOR_ATTR_2(in##index##_beep, S_IWUSR | S_IRUGO, \
1465 show_alarm_beep, store_beep, BEEP_ENABLE, \
Jean Delvare87df0da2010-10-28 20:31:45 +02001466 index + ((index > 14) ? 1 : 0)) }
Wei Song792d3762010-10-28 20:31:44 +02001467
Jean Delvare87df0da2010-10-28 20:31:45 +02001468#define SENSOR_ATTR_FAN(index) { \
Wei Song792d3762010-10-28 20:31:44 +02001469 SENSOR_ATTR_2(fan##index##_input, S_IRUGO, show_fan, \
1470 NULL, FAN_INPUT, index - 1), \
1471 SENSOR_ATTR_2(fan##index##_min, S_IWUSR | S_IRUGO, \
1472 show_fan, store_fan_min, FAN_MIN, index - 1), \
1473 SENSOR_ATTR_2(fan##index##_alarm, S_IRUGO, show_alarm_beep, \
1474 NULL, ALARM_STATUS, index + 31), \
1475 SENSOR_ATTR_2(fan##index##_beep, S_IWUSR | S_IRUGO, \
Jean Delvare87df0da2010-10-28 20:31:45 +02001476 show_alarm_beep, store_beep, BEEP_ENABLE, index + 31) }
Wei Song792d3762010-10-28 20:31:44 +02001477
Jean Delvareb5f6a902010-10-28 20:31:46 +02001478#define SENSOR_ATTR_PWM(index) { \
Wei Song792d3762010-10-28 20:31:44 +02001479 SENSOR_ATTR_2(pwm##index, S_IWUSR | S_IRUGO, show_pwm, \
1480 store_pwm, PWM_OUTPUT, index - 1), \
1481 SENSOR_ATTR_2(pwm##index##_nonstop, S_IWUSR | S_IRUGO, \
1482 show_pwm, store_pwm, PWM_NONSTOP, index - 1), \
1483 SENSOR_ATTR_2(pwm##index##_start, S_IWUSR | S_IRUGO, \
1484 show_pwm, store_pwm, PWM_START, index - 1), \
1485 SENSOR_ATTR_2(pwm##index##_stop_time, S_IWUSR | S_IRUGO, \
1486 show_pwm, store_pwm, PWM_STOP_TIME, index - 1), \
Jean Delvare01879a82010-10-28 20:31:47 +02001487 SENSOR_ATTR_2(pwm##index##_freq, S_IWUSR | S_IRUGO, \
1488 show_pwm, store_pwm, PWM_FREQ, index - 1), \
Wei Song792d3762010-10-28 20:31:44 +02001489 SENSOR_ATTR_2(pwm##index##_enable, S_IWUSR | S_IRUGO, \
Jean Delvareb2cc5282010-10-28 20:31:46 +02001490 show_pwm_enable, store_pwm_enable, NOT_USED, index - 1), \
1491 SENSOR_ATTR_2(fan##index##_target, S_IWUSR | S_IRUGO, \
1492 show_fanin, store_fanin, FANIN_TARGET, index - 1) }
Wei Song792d3762010-10-28 20:31:44 +02001493
Jean Delvare87df0da2010-10-28 20:31:45 +02001494#define SENSOR_ATTR_DTS(index) { \
Wei Song792d3762010-10-28 20:31:44 +02001495 SENSOR_ATTR_2(temp##index##_type, S_IRUGO , \
1496 show_dts_mode, NULL, NOT_USED, index - 7), \
1497 SENSOR_ATTR_2(temp##index##_input, S_IRUGO, show_dts, \
1498 NULL, NOT_USED, index - 7), \
Jean Delvarea0ce4022010-10-28 20:31:47 +02001499 SENSOR_ATTR_2(temp##index##_crit, S_IRUGO | S_IWUSR, show_dts_ext, \
Wei Song792d3762010-10-28 20:31:44 +02001500 store_dts_ext, DTS_CRIT, NOT_USED), \
Jean Delvarea0ce4022010-10-28 20:31:47 +02001501 SENSOR_ATTR_2(temp##index##_crit_hyst, S_IRUGO | S_IWUSR, \
Wei Song792d3762010-10-28 20:31:44 +02001502 show_dts_ext, store_dts_ext, DTS_CRIT_HYST, NOT_USED), \
Jean Delvarea0ce4022010-10-28 20:31:47 +02001503 SENSOR_ATTR_2(temp##index##_max, S_IRUGO | S_IWUSR, show_dts_ext, \
Wei Song792d3762010-10-28 20:31:44 +02001504 store_dts_ext, DTS_WARN, NOT_USED), \
Jean Delvarea0ce4022010-10-28 20:31:47 +02001505 SENSOR_ATTR_2(temp##index##_max_hyst, S_IRUGO | S_IWUSR, \
Wei Song792d3762010-10-28 20:31:44 +02001506 show_dts_ext, store_dts_ext, DTS_WARN_HYST, NOT_USED), \
1507 SENSOR_ATTR_2(temp##index##_alarm, S_IRUGO, \
1508 show_alarm_beep, NULL, ALARM_STATUS, index + 17), \
1509 SENSOR_ATTR_2(temp##index##_beep, S_IWUSR | S_IRUGO, \
Jean Delvare87df0da2010-10-28 20:31:45 +02001510 show_alarm_beep, store_beep, BEEP_ENABLE, index + 17) }
Wei Song792d3762010-10-28 20:31:44 +02001511
Jean Delvare87df0da2010-10-28 20:31:45 +02001512#define SENSOR_ATTR_TEMP(index) { \
Wei Song792d3762010-10-28 20:31:44 +02001513 SENSOR_ATTR_2(temp##index##_type, S_IRUGO | S_IWUSR, \
1514 show_temp_mode, store_temp_mode, NOT_USED, index - 1), \
1515 SENSOR_ATTR_2(temp##index##_input, S_IRUGO, show_temp, \
1516 NULL, TEMP_READ, index - 1), \
Jean Delvarea0ce4022010-10-28 20:31:47 +02001517 SENSOR_ATTR_2(temp##index##_crit, S_IRUGO | S_IWUSR, show_temp, \
Wei Song792d3762010-10-28 20:31:44 +02001518 store_temp, TEMP_CRIT, index - 1), \
Jean Delvarea0ce4022010-10-28 20:31:47 +02001519 SENSOR_ATTR_2(temp##index##_crit_hyst, S_IRUGO | S_IWUSR, \
Wei Song792d3762010-10-28 20:31:44 +02001520 show_temp, store_temp, TEMP_CRIT_HYST, index - 1), \
Jean Delvarea0ce4022010-10-28 20:31:47 +02001521 SENSOR_ATTR_2(temp##index##_max, S_IRUGO | S_IWUSR, show_temp, \
Wei Song792d3762010-10-28 20:31:44 +02001522 store_temp, TEMP_WARN, index - 1), \
Jean Delvarea0ce4022010-10-28 20:31:47 +02001523 SENSOR_ATTR_2(temp##index##_max_hyst, S_IRUGO | S_IWUSR, \
Wei Song792d3762010-10-28 20:31:44 +02001524 show_temp, store_temp, TEMP_WARN_HYST, index - 1), \
1525 SENSOR_ATTR_2(temp##index##_alarm, S_IRUGO, \
1526 show_alarm_beep, NULL, ALARM_STATUS, \
1527 index + (index > 4 ? 11 : 17)), \
1528 SENSOR_ATTR_2(temp##index##_beep, S_IWUSR | S_IRUGO, \
1529 show_alarm_beep, store_beep, BEEP_ENABLE, \
1530 index + (index > 4 ? 11 : 17)), \
1531 SENSOR_ATTR_2(temp##index##_source_sel, S_IWUSR | S_IRUGO, \
1532 show_temp_src, store_temp_src, NOT_USED, index - 1), \
1533 SENSOR_ATTR_2(temp##index##_pwm_enable, S_IWUSR | S_IRUGO, \
1534 show_temp_pwm_enable, store_temp_pwm_enable, \
1535 TEMP_PWM_ENABLE, index - 1), \
1536 SENSOR_ATTR_2(temp##index##_auto_channels_pwm, S_IWUSR | S_IRUGO, \
1537 show_temp_pwm_enable, store_temp_pwm_enable, \
1538 TEMP_PWM_FAN_MAP, index - 1), \
1539 SENSOR_ATTR_2(thermal_cruise##index, S_IWUSR | S_IRUGO, \
1540 show_temp_pwm, store_temp_pwm, TEMP_PWM_TTTI, index - 1), \
Jean Delvarea0ce4022010-10-28 20:31:47 +02001541 SENSOR_ATTR_2(temp##index##_warn, S_IWUSR | S_IRUGO, \
Wei Song792d3762010-10-28 20:31:44 +02001542 show_temp_pwm, store_temp_pwm, TEMP_PWM_CTFS, index - 1), \
Jean Delvarea0ce4022010-10-28 20:31:47 +02001543 SENSOR_ATTR_2(temp##index##_warn_hyst, S_IWUSR | S_IRUGO, \
Wei Song792d3762010-10-28 20:31:44 +02001544 show_temp_pwm, store_temp_pwm, TEMP_PWM_HCT, index - 1), \
1545 SENSOR_ATTR_2(temp##index##_operation_hyst, S_IWUSR | S_IRUGO, \
1546 show_temp_pwm, store_temp_pwm, TEMP_PWM_HOT, index - 1), \
1547 SENSOR_ATTR_2(temp##index##_auto_point1_pwm, S_IRUGO | S_IWUSR, \
1548 show_sf4_pwm, store_sf4_pwm, 0, index - 1), \
1549 SENSOR_ATTR_2(temp##index##_auto_point2_pwm, S_IRUGO | S_IWUSR, \
1550 show_sf4_pwm, store_sf4_pwm, 1, index - 1), \
1551 SENSOR_ATTR_2(temp##index##_auto_point3_pwm, S_IRUGO | S_IWUSR, \
1552 show_sf4_pwm, store_sf4_pwm, 2, index - 1), \
1553 SENSOR_ATTR_2(temp##index##_auto_point4_pwm, S_IRUGO | S_IWUSR, \
1554 show_sf4_pwm, store_sf4_pwm, 3, index - 1), \
1555 SENSOR_ATTR_2(temp##index##_auto_point5_pwm, S_IRUGO | S_IWUSR, \
1556 show_sf4_pwm, store_sf4_pwm, 4, index - 1), \
1557 SENSOR_ATTR_2(temp##index##_auto_point6_pwm, S_IRUGO | S_IWUSR, \
1558 show_sf4_pwm, store_sf4_pwm, 5, index - 1), \
1559 SENSOR_ATTR_2(temp##index##_auto_point7_pwm, S_IRUGO | S_IWUSR, \
1560 show_sf4_pwm, store_sf4_pwm, 6, index - 1), \
1561 SENSOR_ATTR_2(temp##index##_auto_point1_temp, S_IRUGO | S_IWUSR,\
1562 show_sf4_temp, store_sf4_temp, 0, index - 1), \
1563 SENSOR_ATTR_2(temp##index##_auto_point2_temp, S_IRUGO | S_IWUSR,\
1564 show_sf4_temp, store_sf4_temp, 1, index - 1), \
1565 SENSOR_ATTR_2(temp##index##_auto_point3_temp, S_IRUGO | S_IWUSR,\
1566 show_sf4_temp, store_sf4_temp, 2, index - 1), \
1567 SENSOR_ATTR_2(temp##index##_auto_point4_temp, S_IRUGO | S_IWUSR,\
1568 show_sf4_temp, store_sf4_temp, 3, index - 1), \
1569 SENSOR_ATTR_2(temp##index##_auto_point5_temp, S_IRUGO | S_IWUSR,\
1570 show_sf4_temp, store_sf4_temp, 4, index - 1), \
1571 SENSOR_ATTR_2(temp##index##_auto_point6_temp, S_IRUGO | S_IWUSR,\
1572 show_sf4_temp, store_sf4_temp, 5, index - 1), \
1573 SENSOR_ATTR_2(temp##index##_auto_point7_temp, S_IRUGO | S_IWUSR,\
Jean Delvare87df0da2010-10-28 20:31:45 +02001574 show_sf4_temp, store_sf4_temp, 6, index - 1) }
Wei Song792d3762010-10-28 20:31:44 +02001575
1576
Jean Delvare87df0da2010-10-28 20:31:45 +02001577static struct sensor_device_attribute_2 w83795_in[][5] = {
Wei Song792d3762010-10-28 20:31:44 +02001578 SENSOR_ATTR_IN(0),
1579 SENSOR_ATTR_IN(1),
1580 SENSOR_ATTR_IN(2),
1581 SENSOR_ATTR_IN(3),
1582 SENSOR_ATTR_IN(4),
1583 SENSOR_ATTR_IN(5),
1584 SENSOR_ATTR_IN(6),
1585 SENSOR_ATTR_IN(7),
1586 SENSOR_ATTR_IN(8),
1587 SENSOR_ATTR_IN(9),
1588 SENSOR_ATTR_IN(10),
1589 SENSOR_ATTR_IN(11),
1590 SENSOR_ATTR_IN(12),
1591 SENSOR_ATTR_IN(13),
1592 SENSOR_ATTR_IN(14),
1593 SENSOR_ATTR_IN(15),
1594 SENSOR_ATTR_IN(16),
1595 SENSOR_ATTR_IN(17),
1596 SENSOR_ATTR_IN(18),
1597 SENSOR_ATTR_IN(19),
1598 SENSOR_ATTR_IN(20),
1599};
1600
Jean Delvare86ef4d22010-10-28 20:31:46 +02001601static const struct sensor_device_attribute_2 w83795_fan[][4] = {
Wei Song792d3762010-10-28 20:31:44 +02001602 SENSOR_ATTR_FAN(1),
1603 SENSOR_ATTR_FAN(2),
1604 SENSOR_ATTR_FAN(3),
1605 SENSOR_ATTR_FAN(4),
1606 SENSOR_ATTR_FAN(5),
1607 SENSOR_ATTR_FAN(6),
1608 SENSOR_ATTR_FAN(7),
1609 SENSOR_ATTR_FAN(8),
1610 SENSOR_ATTR_FAN(9),
1611 SENSOR_ATTR_FAN(10),
1612 SENSOR_ATTR_FAN(11),
1613 SENSOR_ATTR_FAN(12),
1614 SENSOR_ATTR_FAN(13),
1615 SENSOR_ATTR_FAN(14),
1616};
1617
Jean Delvare86ef4d22010-10-28 20:31:46 +02001618static const struct sensor_device_attribute_2 w83795_temp[][29] = {
Wei Song792d3762010-10-28 20:31:44 +02001619 SENSOR_ATTR_TEMP(1),
1620 SENSOR_ATTR_TEMP(2),
1621 SENSOR_ATTR_TEMP(3),
1622 SENSOR_ATTR_TEMP(4),
1623 SENSOR_ATTR_TEMP(5),
1624 SENSOR_ATTR_TEMP(6),
1625};
1626
Jean Delvare86ef4d22010-10-28 20:31:46 +02001627static const struct sensor_device_attribute_2 w83795_dts[][8] = {
Wei Song792d3762010-10-28 20:31:44 +02001628 SENSOR_ATTR_DTS(7),
1629 SENSOR_ATTR_DTS(8),
1630 SENSOR_ATTR_DTS(9),
1631 SENSOR_ATTR_DTS(10),
1632 SENSOR_ATTR_DTS(11),
1633 SENSOR_ATTR_DTS(12),
1634 SENSOR_ATTR_DTS(13),
1635 SENSOR_ATTR_DTS(14),
1636};
1637
Jean Delvare86ef4d22010-10-28 20:31:46 +02001638static const struct sensor_device_attribute_2 w83795_pwm[][7] = {
Jean Delvareb5f6a902010-10-28 20:31:46 +02001639 SENSOR_ATTR_PWM(1),
1640 SENSOR_ATTR_PWM(2),
Wei Song792d3762010-10-28 20:31:44 +02001641 SENSOR_ATTR_PWM(3),
1642 SENSOR_ATTR_PWM(4),
1643 SENSOR_ATTR_PWM(5),
1644 SENSOR_ATTR_PWM(6),
1645 SENSOR_ATTR_PWM(7),
1646 SENSOR_ATTR_PWM(8),
1647};
1648
Jean Delvare86ef4d22010-10-28 20:31:46 +02001649static const struct sensor_device_attribute_2 sda_single_files[] = {
Wei Song792d3762010-10-28 20:31:44 +02001650 SENSOR_ATTR_2(chassis, S_IWUSR | S_IRUGO, show_alarm_beep,
1651 store_chassis_clear, ALARM_STATUS, 46),
Jean Delvare02728ff2010-10-28 20:31:47 +02001652 SENSOR_ATTR_2(beep_enable, S_IWUSR | S_IRUGO, show_alarm_beep,
1653 store_beep, BEEP_ENABLE, 47),
Wei Song792d3762010-10-28 20:31:44 +02001654 SENSOR_ATTR_2(speed_cruise_tolerance, S_IWUSR | S_IRUGO, show_fanin,
1655 store_fanin, FANIN_TOL, NOT_USED),
1656 SENSOR_ATTR_2(pwm_default, S_IWUSR | S_IRUGO, show_sf_setup,
1657 store_sf_setup, SETUP_PWM_DEFAULT, NOT_USED),
1658 SENSOR_ATTR_2(pwm_uptime, S_IWUSR | S_IRUGO, show_sf_setup,
1659 store_sf_setup, SETUP_PWM_UPTIME, NOT_USED),
1660 SENSOR_ATTR_2(pwm_downtime, S_IWUSR | S_IRUGO, show_sf_setup,
1661 store_sf_setup, SETUP_PWM_DOWNTIME, NOT_USED),
1662};
1663
1664/*
1665 * Driver interface
1666 */
1667
1668static void w83795_init_client(struct i2c_client *client)
1669{
Jean Delvare01879a82010-10-28 20:31:47 +02001670 struct w83795_data *data = i2c_get_clientdata(client);
1671 static const u16 clkin[4] = { /* in kHz */
1672 14318, 24000, 33333, 48000
1673 };
Jean Delvare80646b92010-10-28 20:31:46 +02001674 u8 config;
1675
Wei Song792d3762010-10-28 20:31:44 +02001676 if (reset)
1677 w83795_write(client, W83795_REG_CONFIG, 0x80);
1678
Jean Delvare80646b92010-10-28 20:31:46 +02001679 /* Start monitoring if needed */
1680 config = w83795_read(client, W83795_REG_CONFIG);
1681 if (!(config & W83795_REG_CONFIG_START)) {
1682 dev_info(&client->dev, "Enabling monitoring operations\n");
1683 w83795_write(client, W83795_REG_CONFIG,
1684 config | W83795_REG_CONFIG_START);
1685 }
Jean Delvare01879a82010-10-28 20:31:47 +02001686
1687 data->clkin = clkin[(config >> 3) & 0x3];
1688 dev_dbg(&client->dev, "clkin = %u kHz\n", data->clkin);
Wei Song792d3762010-10-28 20:31:44 +02001689}
1690
Jean Delvare2be381d2010-10-28 20:31:45 +02001691static int w83795_get_device_id(struct i2c_client *client)
1692{
1693 int device_id;
1694
1695 device_id = i2c_smbus_read_byte_data(client, W83795_REG_DEVICEID);
1696
1697 /* Special case for rev. A chips; can't be checked first because later
1698 revisions emulate this for compatibility */
1699 if (device_id < 0 || (device_id & 0xf0) != 0x50) {
1700 int alt_id;
1701
1702 alt_id = i2c_smbus_read_byte_data(client,
1703 W83795_REG_DEVICEID_A);
1704 if (alt_id == 0x50)
1705 device_id = alt_id;
1706 }
1707
1708 return device_id;
1709}
1710
Wei Song792d3762010-10-28 20:31:44 +02001711/* Return 0 if detection is successful, -ENODEV otherwise */
1712static int w83795_detect(struct i2c_client *client,
1713 struct i2c_board_info *info)
1714{
Jean Delvare2be381d2010-10-28 20:31:45 +02001715 int bank, vendor_id, device_id, expected, i2c_addr, config;
Wei Song792d3762010-10-28 20:31:44 +02001716 struct i2c_adapter *adapter = client->adapter;
1717 unsigned short address = client->addr;
Jean Delvare093d1a42010-10-28 20:31:45 +02001718 const char *chip_name;
Wei Song792d3762010-10-28 20:31:44 +02001719
1720 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1721 return -ENODEV;
1722 bank = i2c_smbus_read_byte_data(client, W83795_REG_BANKSEL);
Jean Delvare2be381d2010-10-28 20:31:45 +02001723 if (bank < 0 || (bank & 0x7c)) {
1724 dev_dbg(&adapter->dev,
1725 "w83795: Detection failed at addr 0x%02hx, check %s\n",
1726 address, "bank");
1727 return -ENODEV;
1728 }
Wei Song792d3762010-10-28 20:31:44 +02001729
Wei Song792d3762010-10-28 20:31:44 +02001730 /* Check Nuvoton vendor ID */
Jean Delvare2be381d2010-10-28 20:31:45 +02001731 vendor_id = i2c_smbus_read_byte_data(client, W83795_REG_VENDORID);
1732 expected = bank & 0x80 ? 0x5c : 0xa3;
1733 if (vendor_id != expected) {
1734 dev_dbg(&adapter->dev,
1735 "w83795: Detection failed at addr 0x%02hx, check %s\n",
1736 address, "vendor id");
1737 return -ENODEV;
1738 }
1739
1740 /* Check device ID */
1741 device_id = w83795_get_device_id(client) |
1742 (i2c_smbus_read_byte_data(client, W83795_REG_CHIPID) << 8);
1743 if ((device_id >> 4) != 0x795) {
1744 dev_dbg(&adapter->dev,
1745 "w83795: Detection failed at addr 0x%02hx, check %s\n",
1746 address, "device id\n");
Wei Song792d3762010-10-28 20:31:44 +02001747 return -ENODEV;
1748 }
1749
1750 /* If Nuvoton chip, address of chip and W83795_REG_I2C_ADDR
1751 should match */
Jean Delvare2be381d2010-10-28 20:31:45 +02001752 if ((bank & 0x07) == 0) {
1753 i2c_addr = i2c_smbus_read_byte_data(client,
1754 W83795_REG_I2C_ADDR);
1755 if ((i2c_addr & 0x7f) != address) {
1756 dev_dbg(&adapter->dev,
1757 "w83795: Detection failed at addr 0x%02hx, "
1758 "check %s\n", address, "i2c addr");
1759 return -ENODEV;
1760 }
Wei Song792d3762010-10-28 20:31:44 +02001761 }
1762
Jean Delvare093d1a42010-10-28 20:31:45 +02001763 /* Check 795 chip type: 795G or 795ADG
1764 Usually we don't write to chips during detection, but here we don't
1765 quite have the choice; hopefully it's OK, we are about to return
1766 success anyway */
1767 if ((bank & 0x07) != 0)
1768 i2c_smbus_write_byte_data(client, W83795_REG_BANKSEL,
1769 bank & ~0x07);
Jean Delvare2be381d2010-10-28 20:31:45 +02001770 config = i2c_smbus_read_byte_data(client, W83795_REG_CONFIG);
1771 if (config & W83795_REG_CONFIG_CONFIG48)
Jean Delvare093d1a42010-10-28 20:31:45 +02001772 chip_name = "w83795adg";
Jean Delvare2be381d2010-10-28 20:31:45 +02001773 else
Jean Delvare093d1a42010-10-28 20:31:45 +02001774 chip_name = "w83795g";
Wei Song792d3762010-10-28 20:31:44 +02001775
Jean Delvare093d1a42010-10-28 20:31:45 +02001776 strlcpy(info->type, chip_name, I2C_NAME_SIZE);
Jean Delvare2be381d2010-10-28 20:31:45 +02001777 dev_info(&adapter->dev, "Found %s rev. %c at 0x%02hx\n", chip_name,
1778 'A' + (device_id & 0xf), address);
Wei Song792d3762010-10-28 20:31:44 +02001779
1780 return 0;
1781}
1782
Jean Delvare6f3dcde2010-10-28 20:31:45 +02001783static int w83795_handle_files(struct device *dev, int (*fn)(struct device *,
1784 const struct device_attribute *))
Jean Delvare892514a2010-10-28 20:31:45 +02001785{
1786 struct w83795_data *data = dev_get_drvdata(dev);
Jean Delvare87df0da2010-10-28 20:31:45 +02001787 int err, i, j;
Jean Delvare892514a2010-10-28 20:31:45 +02001788
1789 for (i = 0; i < ARRAY_SIZE(w83795_in); i++) {
Jean Delvare87df0da2010-10-28 20:31:45 +02001790 if (!(data->has_in & (1 << i)))
Jean Delvare892514a2010-10-28 20:31:45 +02001791 continue;
Jean Delvare87df0da2010-10-28 20:31:45 +02001792 for (j = 0; j < ARRAY_SIZE(w83795_in[0]); j++) {
1793 err = fn(dev, &w83795_in[i][j].dev_attr);
1794 if (err)
1795 return err;
1796 }
Jean Delvare892514a2010-10-28 20:31:45 +02001797 }
1798
1799 for (i = 0; i < ARRAY_SIZE(w83795_fan); i++) {
Jean Delvare87df0da2010-10-28 20:31:45 +02001800 if (!(data->has_fan & (1 << i)))
Jean Delvare892514a2010-10-28 20:31:45 +02001801 continue;
Jean Delvare87df0da2010-10-28 20:31:45 +02001802 for (j = 0; j < ARRAY_SIZE(w83795_fan[0]); j++) {
1803 err = fn(dev, &w83795_fan[i][j].dev_attr);
1804 if (err)
1805 return err;
1806 }
Jean Delvare892514a2010-10-28 20:31:45 +02001807 }
1808
1809 for (i = 0; i < ARRAY_SIZE(sda_single_files); i++) {
Jean Delvare6f3dcde2010-10-28 20:31:45 +02001810 err = fn(dev, &sda_single_files[i].dev_attr);
Jean Delvare892514a2010-10-28 20:31:45 +02001811 if (err)
1812 return err;
1813 }
1814
Jean Delvareb5f6a902010-10-28 20:31:46 +02001815 for (i = 0; i < data->has_pwm; i++) {
1816 for (j = 0; j < ARRAY_SIZE(w83795_pwm[0]); j++) {
1817 err = fn(dev, &w83795_pwm[i][j].dev_attr);
Jean Delvare892514a2010-10-28 20:31:45 +02001818 if (err)
1819 return err;
1820 }
1821 }
1822
1823 for (i = 0; i < ARRAY_SIZE(w83795_temp); i++) {
Jean Delvare87df0da2010-10-28 20:31:45 +02001824 if (!(data->has_temp & (1 << i)))
Jean Delvare892514a2010-10-28 20:31:45 +02001825 continue;
Jean Delvare87df0da2010-10-28 20:31:45 +02001826 for (j = 0; j < ARRAY_SIZE(w83795_temp[0]); j++) {
1827 err = fn(dev, &w83795_temp[i][j].dev_attr);
1828 if (err)
1829 return err;
1830 }
Jean Delvare892514a2010-10-28 20:31:45 +02001831 }
1832
1833 if (data->enable_dts != 0) {
1834 for (i = 0; i < ARRAY_SIZE(w83795_dts); i++) {
Jean Delvare87df0da2010-10-28 20:31:45 +02001835 if (!(data->has_dts & (1 << i)))
Jean Delvare892514a2010-10-28 20:31:45 +02001836 continue;
Jean Delvare87df0da2010-10-28 20:31:45 +02001837 for (j = 0; j < ARRAY_SIZE(w83795_dts[0]); j++) {
1838 err = fn(dev, &w83795_dts[i][j].dev_attr);
1839 if (err)
1840 return err;
1841 }
Jean Delvare892514a2010-10-28 20:31:45 +02001842 }
1843 }
1844
Jean Delvare892514a2010-10-28 20:31:45 +02001845 return 0;
1846}
1847
Jean Delvare6f3dcde2010-10-28 20:31:45 +02001848/* We need a wrapper that fits in w83795_handle_files */
1849static int device_remove_file_wrapper(struct device *dev,
1850 const struct device_attribute *attr)
Jean Delvare2fa09872010-10-28 20:31:45 +02001851{
Jean Delvare6f3dcde2010-10-28 20:31:45 +02001852 device_remove_file(dev, attr);
1853 return 0;
Jean Delvare2fa09872010-10-28 20:31:45 +02001854}
1855
Jean Delvare0e256012010-10-28 20:31:46 +02001856static void w83795_check_dynamic_in_limits(struct i2c_client *client)
1857{
1858 struct w83795_data *data = i2c_get_clientdata(client);
1859 u8 vid_ctl;
1860 int i, err_max, err_min;
1861
1862 vid_ctl = w83795_read(client, W83795_REG_VID_CTRL);
1863
1864 /* Return immediately if VRM isn't configured */
1865 if ((vid_ctl & 0x07) == 0x00 || (vid_ctl & 0x07) == 0x07)
1866 return;
1867
1868 data->has_dyn_in = (vid_ctl >> 3) & 0x07;
1869 for (i = 0; i < 2; i++) {
1870 if (!(data->has_dyn_in & (1 << i)))
1871 continue;
1872
1873 /* Voltage limits in dynamic mode, switch to read-only */
1874 err_max = sysfs_chmod_file(&client->dev.kobj,
1875 &w83795_in[i][2].dev_attr.attr,
1876 S_IRUGO);
1877 err_min = sysfs_chmod_file(&client->dev.kobj,
1878 &w83795_in[i][3].dev_attr.attr,
1879 S_IRUGO);
1880 if (err_max || err_min)
1881 dev_warn(&client->dev, "Failed to set in%d limits "
1882 "read-only (%d, %d)\n", i, err_max, err_min);
1883 else
1884 dev_info(&client->dev, "in%d limits set dynamically "
1885 "from VID\n", i);
1886 }
1887}
1888
Jean Delvare71caf462010-10-28 20:31:46 +02001889/* Check pins that can be used for either temperature or voltage monitoring */
1890static void w83795_apply_temp_config(struct w83795_data *data, u8 config,
1891 int temp_chan, int in_chan)
1892{
1893 /* config is a 2-bit value */
1894 switch (config) {
1895 case 0x2: /* Voltage monitoring */
1896 data->has_in |= 1 << in_chan;
1897 break;
1898 case 0x1: /* Thermal diode */
1899 if (temp_chan >= 4)
1900 break;
1901 data->temp_mode |= 1 << temp_chan;
1902 /* fall through */
1903 case 0x3: /* Thermistor */
1904 data->has_temp |= 1 << temp_chan;
1905 break;
1906 }
1907}
1908
Wei Song792d3762010-10-28 20:31:44 +02001909static int w83795_probe(struct i2c_client *client,
1910 const struct i2c_device_id *id)
1911{
1912 int i;
1913 u8 tmp;
1914 struct device *dev = &client->dev;
1915 struct w83795_data *data;
Jean Delvare71caf462010-10-28 20:31:46 +02001916 int err;
Wei Song792d3762010-10-28 20:31:44 +02001917
1918 data = kzalloc(sizeof(struct w83795_data), GFP_KERNEL);
1919 if (!data) {
1920 err = -ENOMEM;
1921 goto exit;
1922 }
1923
1924 i2c_set_clientdata(client, data);
Jean Delvare093d1a42010-10-28 20:31:45 +02001925 data->chip_type = id->driver_data;
Wei Song792d3762010-10-28 20:31:44 +02001926 data->bank = i2c_smbus_read_byte_data(client, W83795_REG_BANKSEL);
1927 mutex_init(&data->update_lock);
1928
1929 /* Initialize the chip */
1930 w83795_init_client(client);
1931
Jean Delvare71caf462010-10-28 20:31:46 +02001932 /* Check which voltages and fans are present */
1933 data->has_in = w83795_read(client, W83795_REG_VOLT_CTRL1)
1934 | (w83795_read(client, W83795_REG_VOLT_CTRL2) << 8);
1935 data->has_fan = w83795_read(client, W83795_REG_FANIN_CTRL1)
1936 | (w83795_read(client, W83795_REG_FANIN_CTRL2) << 8);
Wei Song792d3762010-10-28 20:31:44 +02001937
Jean Delvare71caf462010-10-28 20:31:46 +02001938 /* Check which analog temperatures and extra voltages are present */
Wei Song792d3762010-10-28 20:31:44 +02001939 tmp = w83795_read(client, W83795_REG_TEMP_CTRL1);
1940 if (tmp & 0x20)
1941 data->enable_dts = 1;
Jean Delvare71caf462010-10-28 20:31:46 +02001942 w83795_apply_temp_config(data, (tmp >> 2) & 0x3, 5, 16);
1943 w83795_apply_temp_config(data, tmp & 0x3, 4, 15);
Wei Song792d3762010-10-28 20:31:44 +02001944 tmp = w83795_read(client, W83795_REG_TEMP_CTRL2);
Jean Delvare71caf462010-10-28 20:31:46 +02001945 w83795_apply_temp_config(data, tmp >> 6, 3, 20);
1946 w83795_apply_temp_config(data, (tmp >> 4) & 0x3, 2, 19);
1947 w83795_apply_temp_config(data, (tmp >> 2) & 0x3, 1, 18);
1948 w83795_apply_temp_config(data, tmp & 0x3, 0, 17);
Wei Song792d3762010-10-28 20:31:44 +02001949
1950 /* Check DTS enable status */
Jean Delvare71caf462010-10-28 20:31:46 +02001951 if (data->enable_dts) {
Wei Song792d3762010-10-28 20:31:44 +02001952 if (1 & w83795_read(client, W83795_REG_DTSC))
1953 data->enable_dts |= 2;
1954 data->has_dts = w83795_read(client, W83795_REG_DTSE);
1955 }
1956
Jean Delvare54891a32010-10-28 20:31:47 +02001957 /* Report PECI Tbase values */
1958 if (data->enable_dts == 1) {
1959 for (i = 0; i < 8; i++) {
1960 if (!(data->has_dts & (1 << i)))
1961 continue;
1962 tmp = w83795_read(client, W83795_REG_PECI_TBASE(i));
1963 dev_info(&client->dev,
1964 "PECI agent %d Tbase temperature: %u\n",
1965 i + 1, (unsigned int)tmp & 0x7f);
1966 }
1967 }
1968
Wei Song792d3762010-10-28 20:31:44 +02001969 /* First update the voltages measured value and limits */
1970 for (i = 0; i < ARRAY_SIZE(data->in); i++) {
1971 if (!(data->has_in & (1 << i)))
1972 continue;
1973 data->in[i][IN_MAX] =
1974 w83795_read(client, W83795_REG_IN[i][IN_MAX]);
1975 data->in[i][IN_LOW] =
1976 w83795_read(client, W83795_REG_IN[i][IN_LOW]);
1977 tmp = w83795_read(client, W83795_REG_IN[i][IN_READ]) << 2;
Jean Delvarea654b9d42010-10-28 20:31:47 +02001978 tmp |= w83795_read(client, W83795_REG_VRLSB) >> 6;
Wei Song792d3762010-10-28 20:31:44 +02001979 data->in[i][IN_READ] = tmp;
1980 }
1981 for (i = 0; i < IN_LSB_REG_NUM; i++) {
Jean Delvarec1a792a2010-10-28 20:31:47 +02001982 if ((i == 2 && data->chip_type == w83795adg) ||
1983 (i >= 4 && !(data->has_in & (1 << (i + 11)))))
1984 continue;
Wei Song792d3762010-10-28 20:31:44 +02001985 data->in_lsb[i][IN_MAX] =
1986 w83795_read(client, IN_LSB_REG(i, IN_MAX));
1987 data->in_lsb[i][IN_LOW] =
1988 w83795_read(client, IN_LSB_REG(i, IN_LOW));
1989 }
1990 data->has_gain = w83795_read(client, W83795_REG_VMIGB_CTRL) & 0x0f;
1991
1992 /* First update fan and limits */
1993 for (i = 0; i < ARRAY_SIZE(data->fan); i++) {
Jean Delvarec1a792a2010-10-28 20:31:47 +02001994 /* Each register contains LSB for 2 fans, but we want to
1995 * read it only once to save time */
1996 if ((i & 1) == 0 && (data->has_fan & (3 << i)))
1997 tmp = w83795_read(client, W83795_REG_FAN_MIN_LSB(i));
1998
Wei Song792d3762010-10-28 20:31:44 +02001999 if (!(data->has_fan & (1 << i)))
2000 continue;
2001 data->fan_min[i] =
2002 w83795_read(client, W83795_REG_FAN_MIN_HL(i)) << 4;
2003 data->fan_min[i] |=
Jean Delvarec1a792a2010-10-28 20:31:47 +02002004 (tmp >> W83795_REG_FAN_MIN_LSB_SHIFT(i)) & 0x0F;
Wei Song792d3762010-10-28 20:31:44 +02002005 data->fan[i] = w83795_read(client, W83795_REG_FAN(i)) << 4;
Jean Delvarea654b9d42010-10-28 20:31:47 +02002006 data->fan[i] |= w83795_read(client, W83795_REG_VRLSB) >> 4;
Wei Song792d3762010-10-28 20:31:44 +02002007 }
2008
2009 /* temperature and limits */
2010 for (i = 0; i < ARRAY_SIZE(data->temp); i++) {
2011 if (!(data->has_temp & (1 << i)))
2012 continue;
2013 data->temp[i][TEMP_CRIT] =
2014 w83795_read(client, W83795_REG_TEMP[i][TEMP_CRIT]);
2015 data->temp[i][TEMP_CRIT_HYST] =
2016 w83795_read(client, W83795_REG_TEMP[i][TEMP_CRIT_HYST]);
2017 data->temp[i][TEMP_WARN] =
2018 w83795_read(client, W83795_REG_TEMP[i][TEMP_WARN]);
2019 data->temp[i][TEMP_WARN_HYST] =
2020 w83795_read(client, W83795_REG_TEMP[i][TEMP_WARN_HYST]);
2021 data->temp[i][TEMP_READ] =
2022 w83795_read(client, W83795_REG_TEMP[i][TEMP_READ]);
2023 data->temp_read_vrlsb[i] =
2024 w83795_read(client, W83795_REG_VRLSB);
2025 }
2026
2027 /* dts temperature and limits */
2028 if (data->enable_dts != 0) {
2029 data->dts_ext[DTS_CRIT] =
2030 w83795_read(client, W83795_REG_DTS_EXT(DTS_CRIT));
2031 data->dts_ext[DTS_CRIT_HYST] =
2032 w83795_read(client, W83795_REG_DTS_EXT(DTS_CRIT_HYST));
2033 data->dts_ext[DTS_WARN] =
2034 w83795_read(client, W83795_REG_DTS_EXT(DTS_WARN));
2035 data->dts_ext[DTS_WARN_HYST] =
2036 w83795_read(client, W83795_REG_DTS_EXT(DTS_WARN_HYST));
2037 for (i = 0; i < ARRAY_SIZE(data->dts); i++) {
2038 if (!(data->has_dts & (1 << i)))
2039 continue;
2040 data->dts[i] = w83795_read(client, W83795_REG_DTS(i));
2041 data->dts_read_vrlsb[i] =
2042 w83795_read(client, W83795_REG_VRLSB);
2043 }
2044 }
2045
2046 /* First update temp source selction */
2047 for (i = 0; i < 3; i++)
2048 data->temp_src[i] = w83795_read(client, W83795_REG_TSS(i));
2049
2050 /* pwm and smart fan */
2051 if (data->chip_type == w83795g)
2052 data->has_pwm = 8;
2053 else
2054 data->has_pwm = 2;
2055 data->pwm_fcms[0] = w83795_read(client, W83795_REG_FCMS1);
2056 data->pwm_fcms[1] = w83795_read(client, W83795_REG_FCMS2);
Wei Song792d3762010-10-28 20:31:44 +02002057 for (i = 0; i < W83795_REG_TEMP_NUM; i++)
2058 data->pwm_tfmr[i] = w83795_read(client, W83795_REG_TFMR(i));
2059 data->pwm_fomc = w83795_read(client, W83795_REG_FOMC);
2060 for (i = 0; i < data->has_pwm; i++) {
2061 for (tmp = 0; tmp < 5; tmp++) {
2062 data->pwm[i][tmp] =
2063 w83795_read(client, W83795_REG_PWM(i, tmp));
2064 }
2065 }
2066 for (i = 0; i < 8; i++) {
2067 data->target_speed[i] =
2068 w83795_read(client, W83795_REG_FTSH(i)) << 4;
2069 data->target_speed[i] |=
2070 w83795_read(client, W83795_REG_FTSL(i)) >> 4;
2071 }
2072 data->tol_speed = w83795_read(client, W83795_REG_TFTS) & 0x3f;
2073
2074 for (i = 0; i < W83795_REG_TEMP_NUM; i++) {
2075 data->pwm_temp[i][TEMP_PWM_TTTI] =
2076 w83795_read(client, W83795_REG_TTTI(i)) & 0x7f;
2077 data->pwm_temp[i][TEMP_PWM_CTFS] =
2078 w83795_read(client, W83795_REG_CTFS(i));
2079 tmp = w83795_read(client, W83795_REG_HT(i));
2080 data->pwm_temp[i][TEMP_PWM_HCT] = (tmp >> 4) & 0x0f;
2081 data->pwm_temp[i][TEMP_PWM_HOT] = tmp & 0x0f;
2082 }
2083 for (i = 0; i < W83795_REG_TEMP_NUM; i++) {
2084 for (tmp = 0; tmp < 7; tmp++) {
2085 data->sf4_reg[i][SF4_TEMP][tmp] =
2086 w83795_read(client,
2087 W83795_REG_SF4_TEMP(i, tmp));
2088 data->sf4_reg[i][SF4_PWM][tmp] =
2089 w83795_read(client, W83795_REG_SF4_PWM(i, tmp));
2090 }
2091 }
2092
2093 /* Setup PWM Register */
2094 for (i = 0; i < 3; i++) {
2095 data->setup_pwm[i] =
2096 w83795_read(client, W83795_REG_SETUP_PWM(i));
2097 }
2098
2099 /* alarm and beep */
2100 for (i = 0; i < ALARM_BEEP_REG_NUM; i++) {
2101 data->alarms[i] = w83795_read(client, W83795_REG_ALARM(i));
2102 data->beeps[i] = w83795_read(client, W83795_REG_BEEP(i));
2103 }
Wei Song792d3762010-10-28 20:31:44 +02002104
Jean Delvare6f3dcde2010-10-28 20:31:45 +02002105 err = w83795_handle_files(dev, device_create_file);
Jean Delvare892514a2010-10-28 20:31:45 +02002106 if (err)
2107 goto exit_remove;
Wei Song792d3762010-10-28 20:31:44 +02002108
Jean Delvare0e256012010-10-28 20:31:46 +02002109 if (data->chip_type == w83795g)
2110 w83795_check_dynamic_in_limits(client);
2111
Wei Song792d3762010-10-28 20:31:44 +02002112 data->hwmon_dev = hwmon_device_register(dev);
2113 if (IS_ERR(data->hwmon_dev)) {
2114 err = PTR_ERR(data->hwmon_dev);
2115 goto exit_remove;
2116 }
2117
2118 return 0;
2119
Wei Song792d3762010-10-28 20:31:44 +02002120exit_remove:
Jean Delvare6f3dcde2010-10-28 20:31:45 +02002121 w83795_handle_files(dev, device_remove_file_wrapper);
Wei Song792d3762010-10-28 20:31:44 +02002122 kfree(data);
2123exit:
2124 return err;
2125}
2126
2127static int w83795_remove(struct i2c_client *client)
2128{
2129 struct w83795_data *data = i2c_get_clientdata(client);
Wei Song792d3762010-10-28 20:31:44 +02002130
2131 hwmon_device_unregister(data->hwmon_dev);
Jean Delvare6f3dcde2010-10-28 20:31:45 +02002132 w83795_handle_files(&client->dev, device_remove_file_wrapper);
Wei Song792d3762010-10-28 20:31:44 +02002133 kfree(data);
2134
2135 return 0;
2136}
2137
2138
2139static const struct i2c_device_id w83795_id[] = {
Jean Delvare093d1a42010-10-28 20:31:45 +02002140 { "w83795g", w83795g },
2141 { "w83795adg", w83795adg },
Wei Song792d3762010-10-28 20:31:44 +02002142 { }
2143};
2144MODULE_DEVICE_TABLE(i2c, w83795_id);
2145
2146static struct i2c_driver w83795_driver = {
2147 .driver = {
2148 .name = "w83795",
2149 },
2150 .probe = w83795_probe,
2151 .remove = w83795_remove,
2152 .id_table = w83795_id,
2153
2154 .class = I2C_CLASS_HWMON,
2155 .detect = w83795_detect,
2156 .address_list = normal_i2c,
2157};
2158
2159static int __init sensors_w83795_init(void)
2160{
2161 return i2c_add_driver(&w83795_driver);
2162}
2163
2164static void __exit sensors_w83795_exit(void)
2165{
2166 i2c_del_driver(&w83795_driver);
2167}
2168
2169MODULE_AUTHOR("Wei Song");
Jean Delvare315bacf2010-10-28 20:31:45 +02002170MODULE_DESCRIPTION("W83795G/ADG hardware monitoring driver");
Wei Song792d3762010-10-28 20:31:44 +02002171MODULE_LICENSE("GPL");
2172
2173module_init(sensors_w83795_init);
2174module_exit(sensors_w83795_exit);