blob: c8f62044ac197a3a3564d8e51e7bb50fae0bf28a [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
Wei Song792d3762010-10-28 20:31:44 +0200148#define IN_LSB_SHIFT 0
149#define IN_LSB_IDX 1
150static const u8 IN_LSB_SHIFT_IDX[][2] = {
151 /* High/Low LSB shift, LSB No. */
152 {0x00, 0x00}, /* VSEN1 */
153 {0x02, 0x00}, /* VSEN2 */
154 {0x04, 0x00}, /* VSEN3 */
155 {0x06, 0x00}, /* VSEN4 */
156 {0x00, 0x01}, /* VSEN5 */
157 {0x02, 0x01}, /* VSEN6 */
158 {0x04, 0x01}, /* VSEN7 */
159 {0x06, 0x01}, /* VSEN8 */
160 {0x00, 0x02}, /* VSEN9 */
161 {0x02, 0x02}, /* VSEN10 */
162 {0x04, 0x02}, /* VSEN11 */
163 {0x00, 0x03}, /* VTT */
164 {0x02, 0x03}, /* 3VDD */
165 {0x04, 0x03}, /* 3VSB */
166 {0x06, 0x03}, /* VBAT */
167 {0x06, 0x04}, /* VSEN12 */
168 {0x06, 0x05}, /* VSEN13 */
169 {0x06, 0x06}, /* VSEN14 */
170 {0x06, 0x07}, /* VSEN15 */
171 {0x06, 0x08}, /* VSEN16 */
172 {0x06, 0x09}, /* VSEN17 */
173};
174
175
Wei Song792d3762010-10-28 20:31:44 +0200176#define W83795_REG_FAN(index) (0x2E + (index))
177#define W83795_REG_FAN_MIN_HL(index) (0xB6 + (index))
178#define W83795_REG_FAN_MIN_LSB(index) (0xC4 + (index) / 2)
179#define W83795_REG_FAN_MIN_LSB_SHIFT(index) \
Jean Delvare7eb8d502010-10-28 20:31:47 +0200180 (((index) & 1) ? 4 : 0)
Wei Song792d3762010-10-28 20:31:44 +0200181
182#define W83795_REG_VID_CTRL 0x6A
183
Wei Song792d3762010-10-28 20:31:44 +0200184#define W83795_REG_ALARM(index) (0x41 + (index))
185#define W83795_REG_BEEP(index) (0x50 + (index))
186
187#define W83795_REG_CLR_CHASSIS 0x4D
188
189
Wei Song792d3762010-10-28 20:31:44 +0200190#define W83795_REG_FCMS1 0x201
191#define W83795_REG_FCMS2 0x208
192#define W83795_REG_TFMR(index) (0x202 + (index))
193#define W83795_REG_FOMC 0x20F
Wei Song792d3762010-10-28 20:31:44 +0200194
195#define W83795_REG_TSS(index) (0x209 + (index))
196
197#define PWM_OUTPUT 0
198#define PWM_START 1
199#define PWM_NONSTOP 2
200#define PWM_STOP_TIME 3
Jean Delvare01879a82010-10-28 20:31:47 +0200201#define PWM_FREQ 4
Wei Song792d3762010-10-28 20:31:44 +0200202#define W83795_REG_PWM(index, nr) \
203 (((nr) == 0 ? 0x210 : \
204 (nr) == 1 ? 0x220 : \
205 (nr) == 2 ? 0x228 : \
206 (nr) == 3 ? 0x230 : 0x218) + (index))
207
Wei Song792d3762010-10-28 20:31:44 +0200208#define W83795_REG_FTSH(index) (0x240 + (index) * 2)
209#define W83795_REG_FTSL(index) (0x241 + (index) * 2)
210#define W83795_REG_TFTS 0x250
211
212#define TEMP_PWM_TTTI 0
213#define TEMP_PWM_CTFS 1
214#define TEMP_PWM_HCT 2
215#define TEMP_PWM_HOT 3
216#define W83795_REG_TTTI(index) (0x260 + (index))
217#define W83795_REG_CTFS(index) (0x268 + (index))
218#define W83795_REG_HT(index) (0x270 + (index))
219
220#define SF4_TEMP 0
221#define SF4_PWM 1
222#define W83795_REG_SF4_TEMP(temp_num, index) \
223 (0x280 + 0x10 * (temp_num) + (index))
224#define W83795_REG_SF4_PWM(temp_num, index) \
225 (0x288 + 0x10 * (temp_num) + (index))
226
227#define W83795_REG_DTSC 0x301
228#define W83795_REG_DTSE 0x302
229#define W83795_REG_DTS(index) (0x26 + (index))
Jean Delvare54891a32010-10-28 20:31:47 +0200230#define W83795_REG_PECI_TBASE(index) (0x320 + (index))
Wei Song792d3762010-10-28 20:31:44 +0200231
232#define DTS_CRIT 0
233#define DTS_CRIT_HYST 1
234#define DTS_WARN 2
235#define DTS_WARN_HYST 3
236#define W83795_REG_DTS_EXT(index) (0xB2 + (index))
237
238#define SETUP_PWM_DEFAULT 0
239#define SETUP_PWM_UPTIME 1
240#define SETUP_PWM_DOWNTIME 2
241#define W83795_REG_SETUP_PWM(index) (0x20C + (index))
242
243static inline u16 in_from_reg(u8 index, u16 val)
244{
Jean Delvare49c73472010-10-28 20:31:48 +0200245 /* 3VDD, 3VSB and VBAT: 6 mV/bit; other inputs: 2 mV/bit */
246 if (index >= 12 && index <= 14)
Wei Song792d3762010-10-28 20:31:44 +0200247 return val * 6;
248 else
249 return val * 2;
250}
251
252static inline u16 in_to_reg(u8 index, u16 val)
253{
Jean Delvare49c73472010-10-28 20:31:48 +0200254 if (index >= 12 && index <= 14)
Wei Song792d3762010-10-28 20:31:44 +0200255 return val / 6;
256 else
257 return val / 2;
258}
259
260static inline unsigned long fan_from_reg(u16 val)
261{
Jean Delvare6c82b2f2010-10-28 20:31:46 +0200262 if ((val == 0xfff) || (val == 0))
Wei Song792d3762010-10-28 20:31:44 +0200263 return 0;
264 return 1350000UL / val;
265}
266
267static inline u16 fan_to_reg(long rpm)
268{
269 if (rpm <= 0)
270 return 0x0fff;
271 return SENSORS_LIMIT((1350000 + (rpm >> 1)) / rpm, 1, 0xffe);
272}
273
274static inline unsigned long time_from_reg(u8 reg)
275{
276 return reg * 100;
277}
278
279static inline u8 time_to_reg(unsigned long val)
280{
281 return SENSORS_LIMIT((val + 50) / 100, 0, 0xff);
282}
283
284static inline long temp_from_reg(s8 reg)
285{
286 return reg * 1000;
287}
288
289static inline s8 temp_to_reg(long val, s8 min, s8 max)
290{
Jean Delvaredd127f52010-10-28 20:31:47 +0200291 return SENSORS_LIMIT(val / 1000, min, max);
Wei Song792d3762010-10-28 20:31:44 +0200292}
293
Jean Delvare01879a82010-10-28 20:31:47 +0200294static const u16 pwm_freq_cksel0[16] = {
295 1024, 512, 341, 256, 205, 171, 146, 128,
296 85, 64, 32, 16, 8, 4, 2, 1
297};
298
299static unsigned int pwm_freq_from_reg(u8 reg, u16 clkin)
300{
301 unsigned long base_clock;
302
303 if (reg & 0x80) {
304 base_clock = clkin * 1000 / ((clkin == 48000) ? 384 : 256);
305 return base_clock / ((reg & 0x7f) + 1);
306 } else
307 return pwm_freq_cksel0[reg & 0x0f];
308}
309
310static u8 pwm_freq_to_reg(unsigned long val, u16 clkin)
311{
312 unsigned long base_clock;
313 u8 reg0, reg1;
314 unsigned long best0, best1;
315
316 /* Best fit for cksel = 0 */
317 for (reg0 = 0; reg0 < ARRAY_SIZE(pwm_freq_cksel0) - 1; reg0++) {
318 if (val > (pwm_freq_cksel0[reg0] +
319 pwm_freq_cksel0[reg0 + 1]) / 2)
320 break;
321 }
322 if (val < 375) /* cksel = 1 can't beat this */
323 return reg0;
324 best0 = pwm_freq_cksel0[reg0];
325
326 /* Best fit for cksel = 1 */
327 base_clock = clkin * 1000 / ((clkin == 48000) ? 384 : 256);
328 reg1 = SENSORS_LIMIT(DIV_ROUND_CLOSEST(base_clock, val), 1, 128);
329 best1 = base_clock / reg1;
330 reg1 = 0x80 | (reg1 - 1);
331
332 /* Choose the closest one */
333 if (abs(val - best0) > abs(val - best1))
334 return reg1;
335 else
336 return reg0;
337}
Wei Song792d3762010-10-28 20:31:44 +0200338
339enum chip_types {w83795g, w83795adg};
340
341struct w83795_data {
342 struct device *hwmon_dev;
343 struct mutex update_lock;
344 unsigned long last_updated; /* In jiffies */
345 enum chip_types chip_type;
346
347 u8 bank;
348
349 u32 has_in; /* Enable monitor VIN or not */
Jean Delvare0e256012010-10-28 20:31:46 +0200350 u8 has_dyn_in; /* Only in2-0 can have this */
Wei Song792d3762010-10-28 20:31:44 +0200351 u16 in[21][3]; /* Register value, read/high/low */
352 u8 in_lsb[10][3]; /* LSB Register value, high/low */
353 u8 has_gain; /* has gain: in17-20 * 8 */
354
355 u16 has_fan; /* Enable fan14-1 or not */
356 u16 fan[14]; /* Register value combine */
357 u16 fan_min[14]; /* Register value combine */
358
359 u8 has_temp; /* Enable monitor temp6-1 or not */
Jean Delvaredd127f52010-10-28 20:31:47 +0200360 s8 temp[6][5]; /* current, crit, crit_hyst, warn, warn_hyst */
Wei Song792d3762010-10-28 20:31:44 +0200361 u8 temp_read_vrlsb[6];
362 u8 temp_mode; /* bit 0: TR mode, bit 1: TD mode */
363 u8 temp_src[3]; /* Register value */
364
365 u8 enable_dts; /* Enable PECI and SB-TSI,
366 * bit 0: =1 enable, =0 disable,
367 * bit 1: =1 AMD SB-TSI, =0 Intel PECI */
368 u8 has_dts; /* Enable monitor DTS temp */
Jean Delvaredd127f52010-10-28 20:31:47 +0200369 s8 dts[8]; /* Register value */
Wei Song792d3762010-10-28 20:31:44 +0200370 u8 dts_read_vrlsb[8]; /* Register value */
Jean Delvaredd127f52010-10-28 20:31:47 +0200371 s8 dts_ext[4]; /* Register value */
Wei Song792d3762010-10-28 20:31:44 +0200372
373 u8 has_pwm; /* 795g supports 8 pwm, 795adg only supports 2,
374 * no config register, only affected by chip
375 * type */
376 u8 pwm[8][5]; /* Register value, output, start, non stop, stop
Jean Delvare01879a82010-10-28 20:31:47 +0200377 * time, freq */
378 u16 clkin; /* CLKIN frequency in kHz */
Wei Song792d3762010-10-28 20:31:44 +0200379 u8 pwm_fcms[2]; /* Register value */
380 u8 pwm_tfmr[6]; /* Register value */
381 u8 pwm_fomc; /* Register value */
382
383 u16 target_speed[8]; /* Register value, target speed for speed
384 * cruise */
385 u8 tol_speed; /* tolerance of target speed */
386 u8 pwm_temp[6][4]; /* TTTI, CTFS, HCT, HOT */
387 u8 sf4_reg[6][2][7]; /* 6 temp, temp/dcpwm, 7 registers */
388
389 u8 setup_pwm[3]; /* Register value */
390
391 u8 alarms[6]; /* Register value */
392 u8 beeps[6]; /* Register value */
Wei Song792d3762010-10-28 20:31:44 +0200393
394 char valid;
395};
396
397/*
398 * Hardware access
Jean Delvareb2469f42010-10-28 20:31:45 +0200399 * We assume that nobdody can change the bank outside the driver.
Wei Song792d3762010-10-28 20:31:44 +0200400 */
401
Jean Delvareb2469f42010-10-28 20:31:45 +0200402/* Must be called with data->update_lock held, except during initialization */
403static int w83795_set_bank(struct i2c_client *client, u8 bank)
Wei Song792d3762010-10-28 20:31:44 +0200404{
405 struct w83795_data *data = i2c_get_clientdata(client);
Jean Delvareb2469f42010-10-28 20:31:45 +0200406 int err;
Wei Song792d3762010-10-28 20:31:44 +0200407
Jean Delvareb2469f42010-10-28 20:31:45 +0200408 /* If the same bank is already set, nothing to do */
409 if ((data->bank & 0x07) == bank)
410 return 0;
411
412 /* Change to new bank, preserve all other bits */
413 bank |= data->bank & ~0x07;
414 err = i2c_smbus_write_byte_data(client, W83795_REG_BANKSEL, bank);
415 if (err < 0) {
416 dev_err(&client->dev,
417 "Failed to set bank to %d, err %d\n",
418 (int)bank, err);
419 return err;
Wei Song792d3762010-10-28 20:31:44 +0200420 }
Jean Delvareb2469f42010-10-28 20:31:45 +0200421 data->bank = bank;
422
423 return 0;
424}
425
426/* Must be called with data->update_lock held, except during initialization */
427static u8 w83795_read(struct i2c_client *client, u16 reg)
428{
429 int err;
430
431 err = w83795_set_bank(client, reg >> 8);
432 if (err < 0)
433 return 0x00; /* Arbitrary */
434
435 err = i2c_smbus_read_byte_data(client, reg & 0xff);
436 if (err < 0) {
437 dev_err(&client->dev,
438 "Failed to read from register 0x%03x, err %d\n",
439 (int)reg, err);
440 return 0x00; /* Arbitrary */
441 }
442 return err;
Wei Song792d3762010-10-28 20:31:44 +0200443}
444
445/* Must be called with data->update_lock held, except during initialization */
446static int w83795_write(struct i2c_client *client, u16 reg, u8 value)
447{
Jean Delvareb2469f42010-10-28 20:31:45 +0200448 int err;
Wei Song792d3762010-10-28 20:31:44 +0200449
Jean Delvareb2469f42010-10-28 20:31:45 +0200450 err = w83795_set_bank(client, reg >> 8);
451 if (err < 0)
452 return err;
Wei Song792d3762010-10-28 20:31:44 +0200453
Jean Delvareb2469f42010-10-28 20:31:45 +0200454 err = i2c_smbus_write_byte_data(client, reg & 0xff, value);
455 if (err < 0)
456 dev_err(&client->dev,
457 "Failed to write to register 0x%03x, err %d\n",
458 (int)reg, err);
459 return err;
Wei Song792d3762010-10-28 20:31:44 +0200460}
461
462static struct w83795_data *w83795_update_device(struct device *dev)
463{
464 struct i2c_client *client = to_i2c_client(dev);
465 struct w83795_data *data = i2c_get_clientdata(client);
466 u16 tmp;
467 int i;
468
469 mutex_lock(&data->update_lock);
470
471 if (!(time_after(jiffies, data->last_updated + HZ * 2)
472 || !data->valid))
473 goto END;
474
475 /* Update the voltages value */
476 for (i = 0; i < ARRAY_SIZE(data->in); i++) {
477 if (!(data->has_in & (1 << i)))
478 continue;
479 tmp = w83795_read(client, W83795_REG_IN[i][IN_READ]) << 2;
Jean Delvarea654b9d42010-10-28 20:31:47 +0200480 tmp |= w83795_read(client, W83795_REG_VRLSB) >> 6;
Wei Song792d3762010-10-28 20:31:44 +0200481 data->in[i][IN_READ] = tmp;
482 }
483
Jean Delvare0e256012010-10-28 20:31:46 +0200484 /* in0-2 can have dynamic limits (W83795G only) */
485 if (data->has_dyn_in) {
486 u8 lsb_max = w83795_read(client, IN_LSB_REG(0, IN_MAX));
487 u8 lsb_low = w83795_read(client, IN_LSB_REG(0, IN_LOW));
488
489 for (i = 0; i < 3; i++) {
490 if (!(data->has_dyn_in & (1 << i)))
491 continue;
492 data->in[i][IN_MAX] =
493 w83795_read(client, W83795_REG_IN[i][IN_MAX]);
494 data->in[i][IN_LOW] =
495 w83795_read(client, W83795_REG_IN[i][IN_LOW]);
496 data->in_lsb[i][IN_MAX] = (lsb_max >> (2 * i)) & 0x03;
497 data->in_lsb[i][IN_LOW] = (lsb_low >> (2 * i)) & 0x03;
498 }
499 }
500
Wei Song792d3762010-10-28 20:31:44 +0200501 /* Update fan */
502 for (i = 0; i < ARRAY_SIZE(data->fan); i++) {
503 if (!(data->has_fan & (1 << i)))
504 continue;
505 data->fan[i] = w83795_read(client, W83795_REG_FAN(i)) << 4;
506 data->fan[i] |=
Jean Delvare6c82b2f2010-10-28 20:31:46 +0200507 (w83795_read(client, W83795_REG_VRLSB) >> 4) & 0x0F;
Wei Song792d3762010-10-28 20:31:44 +0200508 }
509
510 /* Update temperature */
511 for (i = 0; i < ARRAY_SIZE(data->temp); i++) {
512 /* even stop monitor, register still keep value, just read out
513 * it */
514 if (!(data->has_temp & (1 << i))) {
515 data->temp[i][TEMP_READ] = 0;
516 data->temp_read_vrlsb[i] = 0;
517 continue;
518 }
519 data->temp[i][TEMP_READ] =
520 w83795_read(client, W83795_REG_TEMP[i][TEMP_READ]);
521 data->temp_read_vrlsb[i] =
522 w83795_read(client, W83795_REG_VRLSB);
523 }
524
525 /* Update dts temperature */
526 if (data->enable_dts != 0) {
527 for (i = 0; i < ARRAY_SIZE(data->dts); i++) {
528 if (!(data->has_dts & (1 << i)))
529 continue;
530 data->dts[i] =
531 w83795_read(client, W83795_REG_DTS(i));
532 data->dts_read_vrlsb[i] =
533 w83795_read(client, W83795_REG_VRLSB);
534 }
535 }
536
537 /* Update pwm output */
538 for (i = 0; i < data->has_pwm; i++) {
539 data->pwm[i][PWM_OUTPUT] =
540 w83795_read(client, W83795_REG_PWM(i, PWM_OUTPUT));
541 }
542
543 /* update alarm */
Jean Delvarecd316df2010-10-28 20:31:48 +0200544 for (i = 0; i < ARRAY_SIZE(data->alarms); i++)
Wei Song792d3762010-10-28 20:31:44 +0200545 data->alarms[i] = w83795_read(client, W83795_REG_ALARM(i));
546
547 data->last_updated = jiffies;
548 data->valid = 1;
549
550END:
551 mutex_unlock(&data->update_lock);
552 return data;
553}
554
555/*
556 * Sysfs attributes
557 */
558
559#define ALARM_STATUS 0
560#define BEEP_ENABLE 1
561static ssize_t
562show_alarm_beep(struct device *dev, struct device_attribute *attr, char *buf)
563{
564 struct w83795_data *data = w83795_update_device(dev);
565 struct sensor_device_attribute_2 *sensor_attr =
566 to_sensor_dev_attr_2(attr);
567 int nr = sensor_attr->nr;
568 int index = sensor_attr->index >> 3;
569 int bit = sensor_attr->index & 0x07;
570 u8 val;
571
572 if (ALARM_STATUS == nr) {
573 val = (data->alarms[index] >> (bit)) & 1;
574 } else { /* BEEP_ENABLE */
575 val = (data->beeps[index] >> (bit)) & 1;
576 }
577
578 return sprintf(buf, "%u\n", val);
579}
580
581static ssize_t
582store_beep(struct device *dev, struct device_attribute *attr,
583 const char *buf, size_t count)
584{
585 struct i2c_client *client = to_i2c_client(dev);
586 struct w83795_data *data = i2c_get_clientdata(client);
587 struct sensor_device_attribute_2 *sensor_attr =
588 to_sensor_dev_attr_2(attr);
589 int index = sensor_attr->index >> 3;
590 int shift = sensor_attr->index & 0x07;
591 u8 beep_bit = 1 << shift;
592 unsigned long val;
593
594 if (strict_strtoul(buf, 10, &val) < 0)
595 return -EINVAL;
596 if (val != 0 && val != 1)
597 return -EINVAL;
598
599 mutex_lock(&data->update_lock);
600 data->beeps[index] = w83795_read(client, W83795_REG_BEEP(index));
601 data->beeps[index] &= ~beep_bit;
602 data->beeps[index] |= val << shift;
603 w83795_write(client, W83795_REG_BEEP(index), data->beeps[index]);
604 mutex_unlock(&data->update_lock);
605
606 return count;
607}
608
Wei Song792d3762010-10-28 20:31:44 +0200609/* Write any value to clear chassis alarm */
610static ssize_t
611store_chassis_clear(struct device *dev,
612 struct device_attribute *attr, const char *buf,
613 size_t count)
614{
615 struct i2c_client *client = to_i2c_client(dev);
616 struct w83795_data *data = i2c_get_clientdata(client);
617 u8 val;
618
619 mutex_lock(&data->update_lock);
620 val = w83795_read(client, W83795_REG_CLR_CHASSIS);
621 val |= 0x80;
622 w83795_write(client, W83795_REG_CLR_CHASSIS, val);
623 mutex_unlock(&data->update_lock);
624 return count;
625}
626
627#define FAN_INPUT 0
628#define FAN_MIN 1
629static ssize_t
630show_fan(struct device *dev, struct device_attribute *attr, char *buf)
631{
632 struct sensor_device_attribute_2 *sensor_attr =
633 to_sensor_dev_attr_2(attr);
634 int nr = sensor_attr->nr;
635 int index = sensor_attr->index;
636 struct w83795_data *data = w83795_update_device(dev);
637 u16 val;
638
639 if (FAN_INPUT == nr)
640 val = data->fan[index] & 0x0fff;
641 else
642 val = data->fan_min[index] & 0x0fff;
643
644 return sprintf(buf, "%lu\n", fan_from_reg(val));
645}
646
647static ssize_t
648store_fan_min(struct device *dev, struct device_attribute *attr,
649 const char *buf, size_t count)
650{
651 struct sensor_device_attribute_2 *sensor_attr =
652 to_sensor_dev_attr_2(attr);
653 int index = sensor_attr->index;
654 struct i2c_client *client = to_i2c_client(dev);
655 struct w83795_data *data = i2c_get_clientdata(client);
656 unsigned long val;
657
658 if (strict_strtoul(buf, 10, &val))
659 return -EINVAL;
660 val = fan_to_reg(val);
661
662 mutex_lock(&data->update_lock);
663 data->fan_min[index] = val;
664 w83795_write(client, W83795_REG_FAN_MIN_HL(index), (val >> 4) & 0xff);
665 val &= 0x0f;
Jean Delvare7eb8d502010-10-28 20:31:47 +0200666 if (index & 1) {
Wei Song792d3762010-10-28 20:31:44 +0200667 val <<= 4;
668 val |= w83795_read(client, W83795_REG_FAN_MIN_LSB(index))
669 & 0x0f;
670 } else {
671 val |= w83795_read(client, W83795_REG_FAN_MIN_LSB(index))
672 & 0xf0;
673 }
674 w83795_write(client, W83795_REG_FAN_MIN_LSB(index), val & 0xff);
675 mutex_unlock(&data->update_lock);
676
677 return count;
678}
679
680static ssize_t
681show_pwm(struct device *dev, struct device_attribute *attr, char *buf)
682{
683 struct w83795_data *data = w83795_update_device(dev);
684 struct sensor_device_attribute_2 *sensor_attr =
685 to_sensor_dev_attr_2(attr);
686 int nr = sensor_attr->nr;
687 int index = sensor_attr->index;
Jean Delvare01879a82010-10-28 20:31:47 +0200688 unsigned int val;
Wei Song792d3762010-10-28 20:31:44 +0200689
690 switch (nr) {
691 case PWM_STOP_TIME:
692 val = time_from_reg(data->pwm[index][nr]);
693 break;
Jean Delvare01879a82010-10-28 20:31:47 +0200694 case PWM_FREQ:
695 val = pwm_freq_from_reg(data->pwm[index][nr], data->clkin);
Wei Song792d3762010-10-28 20:31:44 +0200696 break;
697 default:
698 val = data->pwm[index][nr];
699 break;
700 }
701
702 return sprintf(buf, "%u\n", val);
703}
704
705static ssize_t
706store_pwm(struct device *dev, struct device_attribute *attr,
707 const char *buf, size_t count)
708{
709 struct i2c_client *client = to_i2c_client(dev);
710 struct w83795_data *data = i2c_get_clientdata(client);
711 struct sensor_device_attribute_2 *sensor_attr =
712 to_sensor_dev_attr_2(attr);
713 int nr = sensor_attr->nr;
714 int index = sensor_attr->index;
715 unsigned long val;
Wei Song792d3762010-10-28 20:31:44 +0200716
717 if (strict_strtoul(buf, 10, &val) < 0)
718 return -EINVAL;
719
720 mutex_lock(&data->update_lock);
721 switch (nr) {
722 case PWM_STOP_TIME:
723 val = time_to_reg(val);
724 break;
Jean Delvare01879a82010-10-28 20:31:47 +0200725 case PWM_FREQ:
726 val = pwm_freq_to_reg(val, data->clkin);
Wei Song792d3762010-10-28 20:31:44 +0200727 break;
728 default:
729 val = SENSORS_LIMIT(val, 0, 0xff);
730 break;
731 }
732 w83795_write(client, W83795_REG_PWM(index, nr), val);
Jean Delvare01879a82010-10-28 20:31:47 +0200733 data->pwm[index][nr] = val;
Wei Song792d3762010-10-28 20:31:44 +0200734 mutex_unlock(&data->update_lock);
735 return count;
Wei Song792d3762010-10-28 20:31:44 +0200736}
737
738static ssize_t
739show_pwm_enable(struct device *dev, struct device_attribute *attr, char *buf)
740{
741 struct sensor_device_attribute_2 *sensor_attr =
742 to_sensor_dev_attr_2(attr);
743 struct i2c_client *client = to_i2c_client(dev);
744 struct w83795_data *data = i2c_get_clientdata(client);
745 int index = sensor_attr->index;
746 u8 tmp;
747
748 if (1 == (data->pwm_fcms[0] & (1 << index))) {
749 tmp = 2;
750 goto out;
751 }
752 for (tmp = 0; tmp < 6; tmp++) {
753 if (data->pwm_tfmr[tmp] & (1 << index)) {
754 tmp = 3;
755 goto out;
756 }
757 }
758 if (data->pwm_fomc & (1 << index))
759 tmp = 0;
760 else
761 tmp = 1;
762
763out:
764 return sprintf(buf, "%u\n", tmp);
765}
766
767static ssize_t
768store_pwm_enable(struct device *dev, struct device_attribute *attr,
769 const char *buf, size_t count)
770{
771 struct i2c_client *client = to_i2c_client(dev);
772 struct w83795_data *data = i2c_get_clientdata(client);
773 struct sensor_device_attribute_2 *sensor_attr =
774 to_sensor_dev_attr_2(attr);
775 int index = sensor_attr->index;
776 unsigned long val;
777 int i;
778
779 if (strict_strtoul(buf, 10, &val) < 0)
780 return -EINVAL;
781 if (val > 2)
782 return -EINVAL;
783
784 mutex_lock(&data->update_lock);
785 switch (val) {
786 case 0:
787 case 1:
788 data->pwm_fcms[0] &= ~(1 << index);
789 w83795_write(client, W83795_REG_FCMS1, data->pwm_fcms[0]);
790 for (i = 0; i < 6; i++) {
791 data->pwm_tfmr[i] &= ~(1 << index);
792 w83795_write(client, W83795_REG_TFMR(i),
793 data->pwm_tfmr[i]);
794 }
795 data->pwm_fomc |= 1 << index;
796 data->pwm_fomc ^= val << index;
797 w83795_write(client, W83795_REG_FOMC, data->pwm_fomc);
798 break;
799 case 2:
800 data->pwm_fcms[0] |= (1 << index);
801 w83795_write(client, W83795_REG_FCMS1, data->pwm_fcms[0]);
802 break;
803 }
804 mutex_unlock(&data->update_lock);
805 return count;
Wei Song792d3762010-10-28 20:31:44 +0200806}
807
808static ssize_t
809show_temp_src(struct device *dev, struct device_attribute *attr, char *buf)
810{
811 struct sensor_device_attribute_2 *sensor_attr =
812 to_sensor_dev_attr_2(attr);
813 struct i2c_client *client = to_i2c_client(dev);
814 struct w83795_data *data = i2c_get_clientdata(client);
815 int index = sensor_attr->index;
816 u8 val = index / 2;
817 u8 tmp = data->temp_src[val];
818
Jean Delvare7eb8d502010-10-28 20:31:47 +0200819 if (index & 1)
Wei Song792d3762010-10-28 20:31:44 +0200820 val = 4;
821 else
822 val = 0;
823 tmp >>= val;
824 tmp &= 0x0f;
825
826 return sprintf(buf, "%u\n", tmp);
827}
828
829static ssize_t
830store_temp_src(struct device *dev, struct device_attribute *attr,
831 const char *buf, size_t count)
832{
833 struct i2c_client *client = to_i2c_client(dev);
834 struct w83795_data *data = i2c_get_clientdata(client);
835 struct sensor_device_attribute_2 *sensor_attr =
836 to_sensor_dev_attr_2(attr);
837 int index = sensor_attr->index;
838 unsigned long tmp;
839 u8 val = index / 2;
840
841 if (strict_strtoul(buf, 10, &tmp) < 0)
842 return -EINVAL;
843 tmp = SENSORS_LIMIT(tmp, 0, 15);
844
845 mutex_lock(&data->update_lock);
Jean Delvare7eb8d502010-10-28 20:31:47 +0200846 if (index & 1) {
Wei Song792d3762010-10-28 20:31:44 +0200847 tmp <<= 4;
848 data->temp_src[val] &= 0x0f;
849 } else {
850 data->temp_src[val] &= 0xf0;
851 }
852 data->temp_src[val] |= tmp;
853 w83795_write(client, W83795_REG_TSS(val), data->temp_src[val]);
854 mutex_unlock(&data->update_lock);
855
856 return count;
857}
858
859#define TEMP_PWM_ENABLE 0
860#define TEMP_PWM_FAN_MAP 1
861static ssize_t
862show_temp_pwm_enable(struct device *dev, struct device_attribute *attr,
863 char *buf)
864{
865 struct i2c_client *client = to_i2c_client(dev);
866 struct w83795_data *data = i2c_get_clientdata(client);
867 struct sensor_device_attribute_2 *sensor_attr =
868 to_sensor_dev_attr_2(attr);
869 int nr = sensor_attr->nr;
870 int index = sensor_attr->index;
871 u8 tmp = 0xff;
872
873 switch (nr) {
874 case TEMP_PWM_ENABLE:
875 tmp = (data->pwm_fcms[1] >> index) & 1;
876 if (tmp)
877 tmp = 4;
878 else
879 tmp = 3;
880 break;
881 case TEMP_PWM_FAN_MAP:
882 tmp = data->pwm_tfmr[index];
883 break;
884 }
885
886 return sprintf(buf, "%u\n", tmp);
887}
888
889static ssize_t
890store_temp_pwm_enable(struct device *dev, struct device_attribute *attr,
891 const char *buf, size_t count)
892{
893 struct i2c_client *client = to_i2c_client(dev);
894 struct w83795_data *data = i2c_get_clientdata(client);
895 struct sensor_device_attribute_2 *sensor_attr =
896 to_sensor_dev_attr_2(attr);
897 int nr = sensor_attr->nr;
898 int index = sensor_attr->index;
899 unsigned long tmp;
900
901 if (strict_strtoul(buf, 10, &tmp) < 0)
902 return -EINVAL;
903
904 switch (nr) {
905 case TEMP_PWM_ENABLE:
906 if ((tmp != 3) && (tmp != 4))
907 return -EINVAL;
908 tmp -= 3;
909 mutex_lock(&data->update_lock);
910 data->pwm_fcms[1] &= ~(1 << index);
911 data->pwm_fcms[1] |= tmp << index;
912 w83795_write(client, W83795_REG_FCMS2, data->pwm_fcms[1]);
913 mutex_unlock(&data->update_lock);
914 break;
915 case TEMP_PWM_FAN_MAP:
916 mutex_lock(&data->update_lock);
917 tmp = SENSORS_LIMIT(tmp, 0, 0xff);
918 w83795_write(client, W83795_REG_TFMR(index), tmp);
919 data->pwm_tfmr[index] = tmp;
920 mutex_unlock(&data->update_lock);
921 break;
922 }
923 return count;
924}
925
926#define FANIN_TARGET 0
927#define FANIN_TOL 1
928static ssize_t
929show_fanin(struct device *dev, struct device_attribute *attr, char *buf)
930{
931 struct i2c_client *client = to_i2c_client(dev);
932 struct w83795_data *data = i2c_get_clientdata(client);
933 struct sensor_device_attribute_2 *sensor_attr =
934 to_sensor_dev_attr_2(attr);
935 int nr = sensor_attr->nr;
936 int index = sensor_attr->index;
937 u16 tmp = 0;
938
939 switch (nr) {
940 case FANIN_TARGET:
941 tmp = fan_from_reg(data->target_speed[index]);
942 break;
943 case FANIN_TOL:
944 tmp = data->tol_speed;
945 break;
946 }
947
948 return sprintf(buf, "%u\n", tmp);
949}
950
951static ssize_t
952store_fanin(struct device *dev, struct device_attribute *attr,
953 const char *buf, size_t count)
954{
955 struct i2c_client *client = to_i2c_client(dev);
956 struct w83795_data *data = i2c_get_clientdata(client);
957 struct sensor_device_attribute_2 *sensor_attr =
958 to_sensor_dev_attr_2(attr);
959 int nr = sensor_attr->nr;
960 int index = sensor_attr->index;
961 unsigned long val;
962
963 if (strict_strtoul(buf, 10, &val) < 0)
964 return -EINVAL;
965
966 mutex_lock(&data->update_lock);
967 switch (nr) {
968 case FANIN_TARGET:
969 val = fan_to_reg(SENSORS_LIMIT(val, 0, 0xfff));
970 w83795_write(client, W83795_REG_FTSH(index), (val >> 4) & 0xff);
971 w83795_write(client, W83795_REG_FTSL(index), (val << 4) & 0xf0);
972 data->target_speed[index] = val;
973 break;
974 case FANIN_TOL:
975 val = SENSORS_LIMIT(val, 0, 0x3f);
976 w83795_write(client, W83795_REG_TFTS, val);
977 data->tol_speed = val;
978 break;
979 }
980 mutex_unlock(&data->update_lock);
981
982 return count;
983}
984
985
986static ssize_t
987show_temp_pwm(struct device *dev, struct device_attribute *attr, char *buf)
988{
989 struct i2c_client *client = to_i2c_client(dev);
990 struct w83795_data *data = i2c_get_clientdata(client);
991 struct sensor_device_attribute_2 *sensor_attr =
992 to_sensor_dev_attr_2(attr);
993 int nr = sensor_attr->nr;
994 int index = sensor_attr->index;
995 long tmp = temp_from_reg(data->pwm_temp[index][nr]);
996
997 return sprintf(buf, "%ld\n", tmp);
998}
999
1000static ssize_t
1001store_temp_pwm(struct device *dev, struct device_attribute *attr,
1002 const char *buf, size_t count)
1003{
1004 struct i2c_client *client = to_i2c_client(dev);
1005 struct w83795_data *data = i2c_get_clientdata(client);
1006 struct sensor_device_attribute_2 *sensor_attr =
1007 to_sensor_dev_attr_2(attr);
1008 int nr = sensor_attr->nr;
1009 int index = sensor_attr->index;
1010 unsigned long val;
1011 u8 tmp;
1012
1013 if (strict_strtoul(buf, 10, &val) < 0)
1014 return -EINVAL;
1015 val /= 1000;
1016
1017 mutex_lock(&data->update_lock);
1018 switch (nr) {
1019 case TEMP_PWM_TTTI:
1020 val = SENSORS_LIMIT(val, 0, 0x7f);
1021 w83795_write(client, W83795_REG_TTTI(index), val);
1022 break;
1023 case TEMP_PWM_CTFS:
1024 val = SENSORS_LIMIT(val, 0, 0x7f);
1025 w83795_write(client, W83795_REG_CTFS(index), val);
1026 break;
1027 case TEMP_PWM_HCT:
1028 val = SENSORS_LIMIT(val, 0, 0x0f);
1029 tmp = w83795_read(client, W83795_REG_HT(index));
1030 tmp &= 0x0f;
1031 tmp |= (val << 4) & 0xf0;
1032 w83795_write(client, W83795_REG_HT(index), tmp);
1033 break;
1034 case TEMP_PWM_HOT:
1035 val = SENSORS_LIMIT(val, 0, 0x0f);
1036 tmp = w83795_read(client, W83795_REG_HT(index));
1037 tmp &= 0xf0;
1038 tmp |= val & 0x0f;
1039 w83795_write(client, W83795_REG_HT(index), tmp);
1040 break;
1041 }
1042 data->pwm_temp[index][nr] = val;
1043 mutex_unlock(&data->update_lock);
1044
1045 return count;
1046}
1047
1048static ssize_t
1049show_sf4_pwm(struct device *dev, struct device_attribute *attr, char *buf)
1050{
1051 struct i2c_client *client = to_i2c_client(dev);
1052 struct w83795_data *data = i2c_get_clientdata(client);
1053 struct sensor_device_attribute_2 *sensor_attr =
1054 to_sensor_dev_attr_2(attr);
1055 int nr = sensor_attr->nr;
1056 int index = sensor_attr->index;
1057
1058 return sprintf(buf, "%u\n", data->sf4_reg[index][SF4_PWM][nr]);
1059}
1060
1061static ssize_t
1062store_sf4_pwm(struct device *dev, struct device_attribute *attr,
1063 const char *buf, size_t count)
1064{
1065 struct i2c_client *client = to_i2c_client(dev);
1066 struct w83795_data *data = i2c_get_clientdata(client);
1067 struct sensor_device_attribute_2 *sensor_attr =
1068 to_sensor_dev_attr_2(attr);
1069 int nr = sensor_attr->nr;
1070 int index = sensor_attr->index;
1071 unsigned long val;
1072
1073 if (strict_strtoul(buf, 10, &val) < 0)
1074 return -EINVAL;
1075
1076 mutex_lock(&data->update_lock);
1077 w83795_write(client, W83795_REG_SF4_PWM(index, nr), val);
1078 data->sf4_reg[index][SF4_PWM][nr] = val;
1079 mutex_unlock(&data->update_lock);
1080
1081 return count;
1082}
1083
1084static ssize_t
1085show_sf4_temp(struct device *dev, struct device_attribute *attr, char *buf)
1086{
1087 struct i2c_client *client = to_i2c_client(dev);
1088 struct w83795_data *data = i2c_get_clientdata(client);
1089 struct sensor_device_attribute_2 *sensor_attr =
1090 to_sensor_dev_attr_2(attr);
1091 int nr = sensor_attr->nr;
1092 int index = sensor_attr->index;
1093
1094 return sprintf(buf, "%u\n",
1095 (data->sf4_reg[index][SF4_TEMP][nr]) * 1000);
1096}
1097
1098static ssize_t
1099store_sf4_temp(struct device *dev, struct device_attribute *attr,
1100 const char *buf, size_t count)
1101{
1102 struct i2c_client *client = to_i2c_client(dev);
1103 struct w83795_data *data = i2c_get_clientdata(client);
1104 struct sensor_device_attribute_2 *sensor_attr =
1105 to_sensor_dev_attr_2(attr);
1106 int nr = sensor_attr->nr;
1107 int index = sensor_attr->index;
1108 unsigned long val;
1109
1110 if (strict_strtoul(buf, 10, &val) < 0)
1111 return -EINVAL;
1112 val /= 1000;
1113
1114 mutex_lock(&data->update_lock);
1115 w83795_write(client, W83795_REG_SF4_TEMP(index, nr), val);
1116 data->sf4_reg[index][SF4_TEMP][nr] = val;
1117 mutex_unlock(&data->update_lock);
1118
1119 return count;
1120}
1121
1122
1123static ssize_t
1124show_temp(struct device *dev, struct device_attribute *attr, char *buf)
1125{
1126 struct sensor_device_attribute_2 *sensor_attr =
1127 to_sensor_dev_attr_2(attr);
1128 int nr = sensor_attr->nr;
1129 int index = sensor_attr->index;
1130 struct w83795_data *data = w83795_update_device(dev);
Jean Delvaredd127f52010-10-28 20:31:47 +02001131 long temp = temp_from_reg(data->temp[index][nr]);
Wei Song792d3762010-10-28 20:31:44 +02001132
1133 if (TEMP_READ == nr)
Jean Delvarea654b9d42010-10-28 20:31:47 +02001134 temp += (data->temp_read_vrlsb[index] >> 6) * 250;
Wei Song792d3762010-10-28 20:31:44 +02001135 return sprintf(buf, "%ld\n", temp);
1136}
1137
1138static ssize_t
1139store_temp(struct device *dev, struct device_attribute *attr,
1140 const char *buf, size_t count)
1141{
1142 struct sensor_device_attribute_2 *sensor_attr =
1143 to_sensor_dev_attr_2(attr);
1144 int nr = sensor_attr->nr;
1145 int index = sensor_attr->index;
1146 struct i2c_client *client = to_i2c_client(dev);
1147 struct w83795_data *data = i2c_get_clientdata(client);
1148 long tmp;
1149
1150 if (strict_strtol(buf, 10, &tmp) < 0)
1151 return -EINVAL;
1152
1153 mutex_lock(&data->update_lock);
1154 data->temp[index][nr] = temp_to_reg(tmp, -128, 127);
1155 w83795_write(client, W83795_REG_TEMP[index][nr], data->temp[index][nr]);
1156 mutex_unlock(&data->update_lock);
1157 return count;
1158}
1159
1160
1161static ssize_t
1162show_dts_mode(struct device *dev, struct device_attribute *attr, char *buf)
1163{
1164 struct i2c_client *client = to_i2c_client(dev);
1165 struct w83795_data *data = i2c_get_clientdata(client);
1166 struct sensor_device_attribute_2 *sensor_attr =
1167 to_sensor_dev_attr_2(attr);
1168 int index = sensor_attr->index;
1169 u8 tmp;
1170
1171 if (data->enable_dts == 0)
1172 return sprintf(buf, "%d\n", 0);
1173
1174 if ((data->has_dts >> index) & 0x01) {
1175 if (data->enable_dts & 2)
1176 tmp = 5;
1177 else
1178 tmp = 6;
1179 } else {
1180 tmp = 0;
1181 }
1182
1183 return sprintf(buf, "%d\n", tmp);
1184}
1185
1186static ssize_t
1187show_dts(struct device *dev, struct device_attribute *attr, char *buf)
1188{
1189 struct sensor_device_attribute_2 *sensor_attr =
1190 to_sensor_dev_attr_2(attr);
1191 int index = sensor_attr->index;
1192 struct w83795_data *data = w83795_update_device(dev);
Jean Delvaredd127f52010-10-28 20:31:47 +02001193 long temp = temp_from_reg(data->dts[index]);
Wei Song792d3762010-10-28 20:31:44 +02001194
Jean Delvarea654b9d42010-10-28 20:31:47 +02001195 temp += (data->dts_read_vrlsb[index] >> 6) * 250;
Wei Song792d3762010-10-28 20:31:44 +02001196 return sprintf(buf, "%ld\n", temp);
1197}
1198
1199static ssize_t
1200show_dts_ext(struct device *dev, struct device_attribute *attr, char *buf)
1201{
1202 struct sensor_device_attribute_2 *sensor_attr =
1203 to_sensor_dev_attr_2(attr);
1204 int nr = sensor_attr->nr;
1205 struct i2c_client *client = to_i2c_client(dev);
1206 struct w83795_data *data = i2c_get_clientdata(client);
Jean Delvaredd127f52010-10-28 20:31:47 +02001207 long temp = temp_from_reg(data->dts_ext[nr]);
Wei Song792d3762010-10-28 20:31:44 +02001208
Wei Song792d3762010-10-28 20:31:44 +02001209 return sprintf(buf, "%ld\n", temp);
1210}
1211
1212static ssize_t
1213store_dts_ext(struct device *dev, struct device_attribute *attr,
1214 const char *buf, size_t count)
1215{
1216 struct sensor_device_attribute_2 *sensor_attr =
1217 to_sensor_dev_attr_2(attr);
1218 int nr = sensor_attr->nr;
1219 struct i2c_client *client = to_i2c_client(dev);
1220 struct w83795_data *data = i2c_get_clientdata(client);
1221 long tmp;
1222
1223 if (strict_strtol(buf, 10, &tmp) < 0)
1224 return -EINVAL;
1225
1226 mutex_lock(&data->update_lock);
1227 data->dts_ext[nr] = temp_to_reg(tmp, -128, 127);
1228 w83795_write(client, W83795_REG_DTS_EXT(nr), data->dts_ext[nr]);
1229 mutex_unlock(&data->update_lock);
1230 return count;
1231}
1232
1233
1234/*
1235 Type 3: Thermal diode
1236 Type 4: Thermistor
1237
1238 Temp5-6, default TR
1239 Temp1-4, default TD
1240*/
1241
1242static ssize_t
1243show_temp_mode(struct device *dev, struct device_attribute *attr, char *buf)
1244{
1245 struct i2c_client *client = to_i2c_client(dev);
1246 struct w83795_data *data = i2c_get_clientdata(client);
1247 struct sensor_device_attribute_2 *sensor_attr =
1248 to_sensor_dev_attr_2(attr);
1249 int index = sensor_attr->index;
1250 u8 tmp;
1251
1252 if (data->has_temp >> index & 0x01) {
1253 if (data->temp_mode >> index & 0x01)
1254 tmp = 3;
1255 else
1256 tmp = 4;
1257 } else {
1258 tmp = 0;
1259 }
1260
1261 return sprintf(buf, "%d\n", tmp);
1262}
1263
1264static ssize_t
1265store_temp_mode(struct device *dev, struct device_attribute *attr,
1266 const char *buf, size_t count)
1267{
1268 struct i2c_client *client = to_i2c_client(dev);
1269 struct w83795_data *data = i2c_get_clientdata(client);
1270 struct sensor_device_attribute_2 *sensor_attr =
1271 to_sensor_dev_attr_2(attr);
1272 int index = sensor_attr->index;
1273 unsigned long val;
1274 u8 tmp;
1275 u32 mask;
1276
1277 if (strict_strtoul(buf, 10, &val) < 0)
1278 return -EINVAL;
1279 if ((val != 4) && (val != 3))
1280 return -EINVAL;
1281 if ((index > 3) && (val == 3))
1282 return -EINVAL;
1283
1284 mutex_lock(&data->update_lock);
1285 if (val == 3) {
1286 val = TEMP_CTRL_TD;
1287 data->has_temp |= 1 << index;
1288 data->temp_mode |= 1 << index;
1289 } else if (val == 4) {
1290 val = TEMP_CTRL_TR;
1291 data->has_temp |= 1 << index;
1292 tmp = 1 << index;
1293 data->temp_mode &= ~tmp;
1294 }
1295
1296 if (index > 3)
1297 tmp = w83795_read(client, W83795_REG_TEMP_CTRL1);
1298 else
1299 tmp = w83795_read(client, W83795_REG_TEMP_CTRL2);
1300
1301 mask = 0x03 << W83795_REG_TEMP_CTRL[index][TEMP_CTRL_SHIFT];
1302 tmp &= ~mask;
1303 tmp |= W83795_REG_TEMP_CTRL[index][val];
1304
1305 mask = 1 << W83795_REG_TEMP_CTRL[index][TEMP_CTRL_HASIN_SHIFT];
1306 data->has_in &= ~mask;
1307
1308 if (index > 3)
1309 w83795_write(client, W83795_REG_TEMP_CTRL1, tmp);
1310 else
1311 w83795_write(client, W83795_REG_TEMP_CTRL2, tmp);
1312
1313 mutex_unlock(&data->update_lock);
1314 return count;
1315}
1316
1317
1318/* show/store VIN */
1319static ssize_t
1320show_in(struct device *dev, struct device_attribute *attr, char *buf)
1321{
1322 struct sensor_device_attribute_2 *sensor_attr =
1323 to_sensor_dev_attr_2(attr);
1324 int nr = sensor_attr->nr;
1325 int index = sensor_attr->index;
1326 struct w83795_data *data = w83795_update_device(dev);
1327 u16 val = data->in[index][nr];
1328 u8 lsb_idx;
1329
1330 switch (nr) {
1331 case IN_READ:
1332 /* calculate this value again by sensors as sensors3.conf */
1333 if ((index >= 17) &&
Jean Delvare6f9dfd82010-10-28 20:31:46 +02001334 !((data->has_gain >> (index - 17)) & 1))
Wei Song792d3762010-10-28 20:31:44 +02001335 val *= 8;
1336 break;
1337 case IN_MAX:
1338 case IN_LOW:
1339 lsb_idx = IN_LSB_SHIFT_IDX[index][IN_LSB_IDX];
1340 val <<= 2;
1341 val |= (data->in_lsb[lsb_idx][nr] >>
1342 IN_LSB_SHIFT_IDX[lsb_idx][IN_LSB_SHIFT]) & 0x03;
1343 if ((index >= 17) &&
Jean Delvare6f9dfd82010-10-28 20:31:46 +02001344 !((data->has_gain >> (index - 17)) & 1))
Wei Song792d3762010-10-28 20:31:44 +02001345 val *= 8;
1346 break;
1347 }
1348 val = in_from_reg(index, val);
1349
1350 return sprintf(buf, "%d\n", val);
1351}
1352
1353static ssize_t
1354store_in(struct device *dev, struct device_attribute *attr,
1355 const char *buf, size_t count)
1356{
1357 struct sensor_device_attribute_2 *sensor_attr =
1358 to_sensor_dev_attr_2(attr);
1359 int nr = sensor_attr->nr;
1360 int index = sensor_attr->index;
1361 struct i2c_client *client = to_i2c_client(dev);
1362 struct w83795_data *data = i2c_get_clientdata(client);
1363 unsigned long val;
1364 u8 tmp;
1365 u8 lsb_idx;
1366
1367 if (strict_strtoul(buf, 10, &val) < 0)
1368 return -EINVAL;
1369 val = in_to_reg(index, val);
1370
1371 if ((index >= 17) &&
Jean Delvare6f9dfd82010-10-28 20:31:46 +02001372 !((data->has_gain >> (index - 17)) & 1))
Wei Song792d3762010-10-28 20:31:44 +02001373 val /= 8;
1374 val = SENSORS_LIMIT(val, 0, 0x3FF);
1375 mutex_lock(&data->update_lock);
1376
1377 lsb_idx = IN_LSB_SHIFT_IDX[index][IN_LSB_IDX];
1378 tmp = w83795_read(client, IN_LSB_REG(lsb_idx, nr));
1379 tmp &= ~(0x03 << IN_LSB_SHIFT_IDX[index][IN_LSB_SHIFT]);
1380 tmp |= (val & 0x03) << IN_LSB_SHIFT_IDX[index][IN_LSB_SHIFT];
1381 w83795_write(client, IN_LSB_REG(lsb_idx, nr), tmp);
1382 data->in_lsb[lsb_idx][nr] = tmp;
1383
1384 tmp = (val >> 2) & 0xff;
1385 w83795_write(client, W83795_REG_IN[index][nr], tmp);
1386 data->in[index][nr] = tmp;
1387
1388 mutex_unlock(&data->update_lock);
1389 return count;
1390}
1391
1392
1393static ssize_t
1394show_sf_setup(struct device *dev, struct device_attribute *attr, char *buf)
1395{
1396 struct sensor_device_attribute_2 *sensor_attr =
1397 to_sensor_dev_attr_2(attr);
1398 int nr = sensor_attr->nr;
1399 struct i2c_client *client = to_i2c_client(dev);
1400 struct w83795_data *data = i2c_get_clientdata(client);
1401 u16 val = data->setup_pwm[nr];
1402
1403 switch (nr) {
1404 case SETUP_PWM_UPTIME:
1405 case SETUP_PWM_DOWNTIME:
1406 val = time_from_reg(val);
1407 break;
1408 }
1409
1410 return sprintf(buf, "%d\n", val);
1411}
1412
1413static ssize_t
1414store_sf_setup(struct device *dev, struct device_attribute *attr,
1415 const char *buf, size_t count)
1416{
1417 struct sensor_device_attribute_2 *sensor_attr =
1418 to_sensor_dev_attr_2(attr);
1419 int nr = sensor_attr->nr;
1420 struct i2c_client *client = to_i2c_client(dev);
1421 struct w83795_data *data = i2c_get_clientdata(client);
1422 unsigned long val;
1423
1424 if (strict_strtoul(buf, 10, &val) < 0)
1425 return -EINVAL;
1426
1427 switch (nr) {
1428 case SETUP_PWM_DEFAULT:
1429 val = SENSORS_LIMIT(val, 0, 0xff);
1430 break;
1431 case SETUP_PWM_UPTIME:
1432 case SETUP_PWM_DOWNTIME:
1433 val = time_to_reg(val);
1434 if (val == 0)
1435 return -EINVAL;
1436 break;
1437 }
1438
1439 mutex_lock(&data->update_lock);
1440 data->setup_pwm[nr] = val;
1441 w83795_write(client, W83795_REG_SETUP_PWM(nr), val);
1442 mutex_unlock(&data->update_lock);
1443 return count;
1444}
1445
1446
1447#define NOT_USED -1
1448
Jean Delvare0e256012010-10-28 20:31:46 +02001449/* Don't change the attribute order, _max and _min are accessed by index
1450 * somewhere else in the code */
Jean Delvare87df0da2010-10-28 20:31:45 +02001451#define SENSOR_ATTR_IN(index) { \
Wei Song792d3762010-10-28 20:31:44 +02001452 SENSOR_ATTR_2(in##index##_input, S_IRUGO, show_in, NULL, \
1453 IN_READ, index), \
1454 SENSOR_ATTR_2(in##index##_max, S_IRUGO | S_IWUSR, show_in, \
1455 store_in, IN_MAX, index), \
1456 SENSOR_ATTR_2(in##index##_min, S_IRUGO | S_IWUSR, show_in, \
1457 store_in, IN_LOW, index), \
1458 SENSOR_ATTR_2(in##index##_alarm, S_IRUGO, show_alarm_beep, \
1459 NULL, ALARM_STATUS, index + ((index > 14) ? 1 : 0)), \
1460 SENSOR_ATTR_2(in##index##_beep, S_IWUSR | S_IRUGO, \
1461 show_alarm_beep, store_beep, BEEP_ENABLE, \
Jean Delvare87df0da2010-10-28 20:31:45 +02001462 index + ((index > 14) ? 1 : 0)) }
Wei Song792d3762010-10-28 20:31:44 +02001463
Jean Delvare87df0da2010-10-28 20:31:45 +02001464#define SENSOR_ATTR_FAN(index) { \
Wei Song792d3762010-10-28 20:31:44 +02001465 SENSOR_ATTR_2(fan##index##_input, S_IRUGO, show_fan, \
1466 NULL, FAN_INPUT, index - 1), \
1467 SENSOR_ATTR_2(fan##index##_min, S_IWUSR | S_IRUGO, \
1468 show_fan, store_fan_min, FAN_MIN, index - 1), \
1469 SENSOR_ATTR_2(fan##index##_alarm, S_IRUGO, show_alarm_beep, \
1470 NULL, ALARM_STATUS, index + 31), \
1471 SENSOR_ATTR_2(fan##index##_beep, S_IWUSR | S_IRUGO, \
Jean Delvare87df0da2010-10-28 20:31:45 +02001472 show_alarm_beep, store_beep, BEEP_ENABLE, index + 31) }
Wei Song792d3762010-10-28 20:31:44 +02001473
Jean Delvareb5f6a902010-10-28 20:31:46 +02001474#define SENSOR_ATTR_PWM(index) { \
Wei Song792d3762010-10-28 20:31:44 +02001475 SENSOR_ATTR_2(pwm##index, S_IWUSR | S_IRUGO, show_pwm, \
1476 store_pwm, PWM_OUTPUT, index - 1), \
1477 SENSOR_ATTR_2(pwm##index##_nonstop, S_IWUSR | S_IRUGO, \
1478 show_pwm, store_pwm, PWM_NONSTOP, index - 1), \
1479 SENSOR_ATTR_2(pwm##index##_start, S_IWUSR | S_IRUGO, \
1480 show_pwm, store_pwm, PWM_START, index - 1), \
1481 SENSOR_ATTR_2(pwm##index##_stop_time, S_IWUSR | S_IRUGO, \
1482 show_pwm, store_pwm, PWM_STOP_TIME, index - 1), \
Jean Delvare01879a82010-10-28 20:31:47 +02001483 SENSOR_ATTR_2(pwm##index##_freq, S_IWUSR | S_IRUGO, \
1484 show_pwm, store_pwm, PWM_FREQ, index - 1), \
Wei Song792d3762010-10-28 20:31:44 +02001485 SENSOR_ATTR_2(pwm##index##_enable, S_IWUSR | S_IRUGO, \
Jean Delvareb2cc5282010-10-28 20:31:46 +02001486 show_pwm_enable, store_pwm_enable, NOT_USED, index - 1), \
1487 SENSOR_ATTR_2(fan##index##_target, S_IWUSR | S_IRUGO, \
1488 show_fanin, store_fanin, FANIN_TARGET, index - 1) }
Wei Song792d3762010-10-28 20:31:44 +02001489
Jean Delvare87df0da2010-10-28 20:31:45 +02001490#define SENSOR_ATTR_DTS(index) { \
Wei Song792d3762010-10-28 20:31:44 +02001491 SENSOR_ATTR_2(temp##index##_type, S_IRUGO , \
1492 show_dts_mode, NULL, NOT_USED, index - 7), \
1493 SENSOR_ATTR_2(temp##index##_input, S_IRUGO, show_dts, \
1494 NULL, NOT_USED, index - 7), \
Jean Delvarea0ce4022010-10-28 20:31:47 +02001495 SENSOR_ATTR_2(temp##index##_crit, S_IRUGO | S_IWUSR, show_dts_ext, \
Wei Song792d3762010-10-28 20:31:44 +02001496 store_dts_ext, DTS_CRIT, NOT_USED), \
Jean Delvarea0ce4022010-10-28 20:31:47 +02001497 SENSOR_ATTR_2(temp##index##_crit_hyst, S_IRUGO | S_IWUSR, \
Wei Song792d3762010-10-28 20:31:44 +02001498 show_dts_ext, store_dts_ext, DTS_CRIT_HYST, NOT_USED), \
Jean Delvarea0ce4022010-10-28 20:31:47 +02001499 SENSOR_ATTR_2(temp##index##_max, S_IRUGO | S_IWUSR, show_dts_ext, \
Wei Song792d3762010-10-28 20:31:44 +02001500 store_dts_ext, DTS_WARN, NOT_USED), \
Jean Delvarea0ce4022010-10-28 20:31:47 +02001501 SENSOR_ATTR_2(temp##index##_max_hyst, S_IRUGO | S_IWUSR, \
Wei Song792d3762010-10-28 20:31:44 +02001502 show_dts_ext, store_dts_ext, DTS_WARN_HYST, NOT_USED), \
1503 SENSOR_ATTR_2(temp##index##_alarm, S_IRUGO, \
1504 show_alarm_beep, NULL, ALARM_STATUS, index + 17), \
1505 SENSOR_ATTR_2(temp##index##_beep, S_IWUSR | S_IRUGO, \
Jean Delvare87df0da2010-10-28 20:31:45 +02001506 show_alarm_beep, store_beep, BEEP_ENABLE, index + 17) }
Wei Song792d3762010-10-28 20:31:44 +02001507
Jean Delvare87df0da2010-10-28 20:31:45 +02001508#define SENSOR_ATTR_TEMP(index) { \
Wei Song792d3762010-10-28 20:31:44 +02001509 SENSOR_ATTR_2(temp##index##_type, S_IRUGO | S_IWUSR, \
1510 show_temp_mode, store_temp_mode, NOT_USED, index - 1), \
1511 SENSOR_ATTR_2(temp##index##_input, S_IRUGO, show_temp, \
1512 NULL, TEMP_READ, index - 1), \
Jean Delvarea0ce4022010-10-28 20:31:47 +02001513 SENSOR_ATTR_2(temp##index##_crit, S_IRUGO | S_IWUSR, show_temp, \
Wei Song792d3762010-10-28 20:31:44 +02001514 store_temp, TEMP_CRIT, index - 1), \
Jean Delvarea0ce4022010-10-28 20:31:47 +02001515 SENSOR_ATTR_2(temp##index##_crit_hyst, S_IRUGO | S_IWUSR, \
Wei Song792d3762010-10-28 20:31:44 +02001516 show_temp, store_temp, TEMP_CRIT_HYST, index - 1), \
Jean Delvarea0ce4022010-10-28 20:31:47 +02001517 SENSOR_ATTR_2(temp##index##_max, S_IRUGO | S_IWUSR, show_temp, \
Wei Song792d3762010-10-28 20:31:44 +02001518 store_temp, TEMP_WARN, index - 1), \
Jean Delvarea0ce4022010-10-28 20:31:47 +02001519 SENSOR_ATTR_2(temp##index##_max_hyst, S_IRUGO | S_IWUSR, \
Wei Song792d3762010-10-28 20:31:44 +02001520 show_temp, store_temp, TEMP_WARN_HYST, index - 1), \
1521 SENSOR_ATTR_2(temp##index##_alarm, S_IRUGO, \
1522 show_alarm_beep, NULL, ALARM_STATUS, \
1523 index + (index > 4 ? 11 : 17)), \
1524 SENSOR_ATTR_2(temp##index##_beep, S_IWUSR | S_IRUGO, \
1525 show_alarm_beep, store_beep, BEEP_ENABLE, \
1526 index + (index > 4 ? 11 : 17)), \
1527 SENSOR_ATTR_2(temp##index##_source_sel, S_IWUSR | S_IRUGO, \
1528 show_temp_src, store_temp_src, NOT_USED, index - 1), \
1529 SENSOR_ATTR_2(temp##index##_pwm_enable, S_IWUSR | S_IRUGO, \
1530 show_temp_pwm_enable, store_temp_pwm_enable, \
1531 TEMP_PWM_ENABLE, index - 1), \
1532 SENSOR_ATTR_2(temp##index##_auto_channels_pwm, S_IWUSR | S_IRUGO, \
1533 show_temp_pwm_enable, store_temp_pwm_enable, \
1534 TEMP_PWM_FAN_MAP, index - 1), \
1535 SENSOR_ATTR_2(thermal_cruise##index, S_IWUSR | S_IRUGO, \
1536 show_temp_pwm, store_temp_pwm, TEMP_PWM_TTTI, index - 1), \
Jean Delvarea0ce4022010-10-28 20:31:47 +02001537 SENSOR_ATTR_2(temp##index##_warn, S_IWUSR | S_IRUGO, \
Wei Song792d3762010-10-28 20:31:44 +02001538 show_temp_pwm, store_temp_pwm, TEMP_PWM_CTFS, index - 1), \
Jean Delvarea0ce4022010-10-28 20:31:47 +02001539 SENSOR_ATTR_2(temp##index##_warn_hyst, S_IWUSR | S_IRUGO, \
Wei Song792d3762010-10-28 20:31:44 +02001540 show_temp_pwm, store_temp_pwm, TEMP_PWM_HCT, index - 1), \
1541 SENSOR_ATTR_2(temp##index##_operation_hyst, S_IWUSR | S_IRUGO, \
1542 show_temp_pwm, store_temp_pwm, TEMP_PWM_HOT, index - 1), \
1543 SENSOR_ATTR_2(temp##index##_auto_point1_pwm, S_IRUGO | S_IWUSR, \
1544 show_sf4_pwm, store_sf4_pwm, 0, index - 1), \
1545 SENSOR_ATTR_2(temp##index##_auto_point2_pwm, S_IRUGO | S_IWUSR, \
1546 show_sf4_pwm, store_sf4_pwm, 1, index - 1), \
1547 SENSOR_ATTR_2(temp##index##_auto_point3_pwm, S_IRUGO | S_IWUSR, \
1548 show_sf4_pwm, store_sf4_pwm, 2, index - 1), \
1549 SENSOR_ATTR_2(temp##index##_auto_point4_pwm, S_IRUGO | S_IWUSR, \
1550 show_sf4_pwm, store_sf4_pwm, 3, index - 1), \
1551 SENSOR_ATTR_2(temp##index##_auto_point5_pwm, S_IRUGO | S_IWUSR, \
1552 show_sf4_pwm, store_sf4_pwm, 4, index - 1), \
1553 SENSOR_ATTR_2(temp##index##_auto_point6_pwm, S_IRUGO | S_IWUSR, \
1554 show_sf4_pwm, store_sf4_pwm, 5, index - 1), \
1555 SENSOR_ATTR_2(temp##index##_auto_point7_pwm, S_IRUGO | S_IWUSR, \
1556 show_sf4_pwm, store_sf4_pwm, 6, index - 1), \
1557 SENSOR_ATTR_2(temp##index##_auto_point1_temp, S_IRUGO | S_IWUSR,\
1558 show_sf4_temp, store_sf4_temp, 0, index - 1), \
1559 SENSOR_ATTR_2(temp##index##_auto_point2_temp, S_IRUGO | S_IWUSR,\
1560 show_sf4_temp, store_sf4_temp, 1, index - 1), \
1561 SENSOR_ATTR_2(temp##index##_auto_point3_temp, S_IRUGO | S_IWUSR,\
1562 show_sf4_temp, store_sf4_temp, 2, index - 1), \
1563 SENSOR_ATTR_2(temp##index##_auto_point4_temp, S_IRUGO | S_IWUSR,\
1564 show_sf4_temp, store_sf4_temp, 3, index - 1), \
1565 SENSOR_ATTR_2(temp##index##_auto_point5_temp, S_IRUGO | S_IWUSR,\
1566 show_sf4_temp, store_sf4_temp, 4, index - 1), \
1567 SENSOR_ATTR_2(temp##index##_auto_point6_temp, S_IRUGO | S_IWUSR,\
1568 show_sf4_temp, store_sf4_temp, 5, index - 1), \
1569 SENSOR_ATTR_2(temp##index##_auto_point7_temp, S_IRUGO | S_IWUSR,\
Jean Delvare87df0da2010-10-28 20:31:45 +02001570 show_sf4_temp, store_sf4_temp, 6, index - 1) }
Wei Song792d3762010-10-28 20:31:44 +02001571
1572
Jean Delvare87df0da2010-10-28 20:31:45 +02001573static struct sensor_device_attribute_2 w83795_in[][5] = {
Wei Song792d3762010-10-28 20:31:44 +02001574 SENSOR_ATTR_IN(0),
1575 SENSOR_ATTR_IN(1),
1576 SENSOR_ATTR_IN(2),
1577 SENSOR_ATTR_IN(3),
1578 SENSOR_ATTR_IN(4),
1579 SENSOR_ATTR_IN(5),
1580 SENSOR_ATTR_IN(6),
1581 SENSOR_ATTR_IN(7),
1582 SENSOR_ATTR_IN(8),
1583 SENSOR_ATTR_IN(9),
1584 SENSOR_ATTR_IN(10),
1585 SENSOR_ATTR_IN(11),
1586 SENSOR_ATTR_IN(12),
1587 SENSOR_ATTR_IN(13),
1588 SENSOR_ATTR_IN(14),
1589 SENSOR_ATTR_IN(15),
1590 SENSOR_ATTR_IN(16),
1591 SENSOR_ATTR_IN(17),
1592 SENSOR_ATTR_IN(18),
1593 SENSOR_ATTR_IN(19),
1594 SENSOR_ATTR_IN(20),
1595};
1596
Jean Delvare86ef4d22010-10-28 20:31:46 +02001597static const struct sensor_device_attribute_2 w83795_fan[][4] = {
Wei Song792d3762010-10-28 20:31:44 +02001598 SENSOR_ATTR_FAN(1),
1599 SENSOR_ATTR_FAN(2),
1600 SENSOR_ATTR_FAN(3),
1601 SENSOR_ATTR_FAN(4),
1602 SENSOR_ATTR_FAN(5),
1603 SENSOR_ATTR_FAN(6),
1604 SENSOR_ATTR_FAN(7),
1605 SENSOR_ATTR_FAN(8),
1606 SENSOR_ATTR_FAN(9),
1607 SENSOR_ATTR_FAN(10),
1608 SENSOR_ATTR_FAN(11),
1609 SENSOR_ATTR_FAN(12),
1610 SENSOR_ATTR_FAN(13),
1611 SENSOR_ATTR_FAN(14),
1612};
1613
Jean Delvare86ef4d22010-10-28 20:31:46 +02001614static const struct sensor_device_attribute_2 w83795_temp[][29] = {
Wei Song792d3762010-10-28 20:31:44 +02001615 SENSOR_ATTR_TEMP(1),
1616 SENSOR_ATTR_TEMP(2),
1617 SENSOR_ATTR_TEMP(3),
1618 SENSOR_ATTR_TEMP(4),
1619 SENSOR_ATTR_TEMP(5),
1620 SENSOR_ATTR_TEMP(6),
1621};
1622
Jean Delvare86ef4d22010-10-28 20:31:46 +02001623static const struct sensor_device_attribute_2 w83795_dts[][8] = {
Wei Song792d3762010-10-28 20:31:44 +02001624 SENSOR_ATTR_DTS(7),
1625 SENSOR_ATTR_DTS(8),
1626 SENSOR_ATTR_DTS(9),
1627 SENSOR_ATTR_DTS(10),
1628 SENSOR_ATTR_DTS(11),
1629 SENSOR_ATTR_DTS(12),
1630 SENSOR_ATTR_DTS(13),
1631 SENSOR_ATTR_DTS(14),
1632};
1633
Jean Delvare86ef4d22010-10-28 20:31:46 +02001634static const struct sensor_device_attribute_2 w83795_pwm[][7] = {
Jean Delvareb5f6a902010-10-28 20:31:46 +02001635 SENSOR_ATTR_PWM(1),
1636 SENSOR_ATTR_PWM(2),
Wei Song792d3762010-10-28 20:31:44 +02001637 SENSOR_ATTR_PWM(3),
1638 SENSOR_ATTR_PWM(4),
1639 SENSOR_ATTR_PWM(5),
1640 SENSOR_ATTR_PWM(6),
1641 SENSOR_ATTR_PWM(7),
1642 SENSOR_ATTR_PWM(8),
1643};
1644
Jean Delvare86ef4d22010-10-28 20:31:46 +02001645static const struct sensor_device_attribute_2 sda_single_files[] = {
Wei Song792d3762010-10-28 20:31:44 +02001646 SENSOR_ATTR_2(chassis, S_IWUSR | S_IRUGO, show_alarm_beep,
1647 store_chassis_clear, ALARM_STATUS, 46),
Jean Delvare02728ff2010-10-28 20:31:47 +02001648 SENSOR_ATTR_2(beep_enable, S_IWUSR | S_IRUGO, show_alarm_beep,
1649 store_beep, BEEP_ENABLE, 47),
Wei Song792d3762010-10-28 20:31:44 +02001650 SENSOR_ATTR_2(speed_cruise_tolerance, S_IWUSR | S_IRUGO, show_fanin,
1651 store_fanin, FANIN_TOL, NOT_USED),
1652 SENSOR_ATTR_2(pwm_default, S_IWUSR | S_IRUGO, show_sf_setup,
1653 store_sf_setup, SETUP_PWM_DEFAULT, NOT_USED),
1654 SENSOR_ATTR_2(pwm_uptime, S_IWUSR | S_IRUGO, show_sf_setup,
1655 store_sf_setup, SETUP_PWM_UPTIME, NOT_USED),
1656 SENSOR_ATTR_2(pwm_downtime, S_IWUSR | S_IRUGO, show_sf_setup,
1657 store_sf_setup, SETUP_PWM_DOWNTIME, NOT_USED),
1658};
1659
1660/*
1661 * Driver interface
1662 */
1663
1664static void w83795_init_client(struct i2c_client *client)
1665{
Jean Delvare01879a82010-10-28 20:31:47 +02001666 struct w83795_data *data = i2c_get_clientdata(client);
1667 static const u16 clkin[4] = { /* in kHz */
1668 14318, 24000, 33333, 48000
1669 };
Jean Delvare80646b92010-10-28 20:31:46 +02001670 u8 config;
1671
Wei Song792d3762010-10-28 20:31:44 +02001672 if (reset)
1673 w83795_write(client, W83795_REG_CONFIG, 0x80);
1674
Jean Delvare80646b92010-10-28 20:31:46 +02001675 /* Start monitoring if needed */
1676 config = w83795_read(client, W83795_REG_CONFIG);
1677 if (!(config & W83795_REG_CONFIG_START)) {
1678 dev_info(&client->dev, "Enabling monitoring operations\n");
1679 w83795_write(client, W83795_REG_CONFIG,
1680 config | W83795_REG_CONFIG_START);
1681 }
Jean Delvare01879a82010-10-28 20:31:47 +02001682
1683 data->clkin = clkin[(config >> 3) & 0x3];
1684 dev_dbg(&client->dev, "clkin = %u kHz\n", data->clkin);
Wei Song792d3762010-10-28 20:31:44 +02001685}
1686
Jean Delvare2be381d2010-10-28 20:31:45 +02001687static int w83795_get_device_id(struct i2c_client *client)
1688{
1689 int device_id;
1690
1691 device_id = i2c_smbus_read_byte_data(client, W83795_REG_DEVICEID);
1692
1693 /* Special case for rev. A chips; can't be checked first because later
1694 revisions emulate this for compatibility */
1695 if (device_id < 0 || (device_id & 0xf0) != 0x50) {
1696 int alt_id;
1697
1698 alt_id = i2c_smbus_read_byte_data(client,
1699 W83795_REG_DEVICEID_A);
1700 if (alt_id == 0x50)
1701 device_id = alt_id;
1702 }
1703
1704 return device_id;
1705}
1706
Wei Song792d3762010-10-28 20:31:44 +02001707/* Return 0 if detection is successful, -ENODEV otherwise */
1708static int w83795_detect(struct i2c_client *client,
1709 struct i2c_board_info *info)
1710{
Jean Delvare2be381d2010-10-28 20:31:45 +02001711 int bank, vendor_id, device_id, expected, i2c_addr, config;
Wei Song792d3762010-10-28 20:31:44 +02001712 struct i2c_adapter *adapter = client->adapter;
1713 unsigned short address = client->addr;
Jean Delvare093d1a42010-10-28 20:31:45 +02001714 const char *chip_name;
Wei Song792d3762010-10-28 20:31:44 +02001715
1716 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1717 return -ENODEV;
1718 bank = i2c_smbus_read_byte_data(client, W83795_REG_BANKSEL);
Jean Delvare2be381d2010-10-28 20:31:45 +02001719 if (bank < 0 || (bank & 0x7c)) {
1720 dev_dbg(&adapter->dev,
1721 "w83795: Detection failed at addr 0x%02hx, check %s\n",
1722 address, "bank");
1723 return -ENODEV;
1724 }
Wei Song792d3762010-10-28 20:31:44 +02001725
Wei Song792d3762010-10-28 20:31:44 +02001726 /* Check Nuvoton vendor ID */
Jean Delvare2be381d2010-10-28 20:31:45 +02001727 vendor_id = i2c_smbus_read_byte_data(client, W83795_REG_VENDORID);
1728 expected = bank & 0x80 ? 0x5c : 0xa3;
1729 if (vendor_id != expected) {
1730 dev_dbg(&adapter->dev,
1731 "w83795: Detection failed at addr 0x%02hx, check %s\n",
1732 address, "vendor id");
1733 return -ENODEV;
1734 }
1735
1736 /* Check device ID */
1737 device_id = w83795_get_device_id(client) |
1738 (i2c_smbus_read_byte_data(client, W83795_REG_CHIPID) << 8);
1739 if ((device_id >> 4) != 0x795) {
1740 dev_dbg(&adapter->dev,
1741 "w83795: Detection failed at addr 0x%02hx, check %s\n",
1742 address, "device id\n");
Wei Song792d3762010-10-28 20:31:44 +02001743 return -ENODEV;
1744 }
1745
1746 /* If Nuvoton chip, address of chip and W83795_REG_I2C_ADDR
1747 should match */
Jean Delvare2be381d2010-10-28 20:31:45 +02001748 if ((bank & 0x07) == 0) {
1749 i2c_addr = i2c_smbus_read_byte_data(client,
1750 W83795_REG_I2C_ADDR);
1751 if ((i2c_addr & 0x7f) != address) {
1752 dev_dbg(&adapter->dev,
1753 "w83795: Detection failed at addr 0x%02hx, "
1754 "check %s\n", address, "i2c addr");
1755 return -ENODEV;
1756 }
Wei Song792d3762010-10-28 20:31:44 +02001757 }
1758
Jean Delvare093d1a42010-10-28 20:31:45 +02001759 /* Check 795 chip type: 795G or 795ADG
1760 Usually we don't write to chips during detection, but here we don't
1761 quite have the choice; hopefully it's OK, we are about to return
1762 success anyway */
1763 if ((bank & 0x07) != 0)
1764 i2c_smbus_write_byte_data(client, W83795_REG_BANKSEL,
1765 bank & ~0x07);
Jean Delvare2be381d2010-10-28 20:31:45 +02001766 config = i2c_smbus_read_byte_data(client, W83795_REG_CONFIG);
1767 if (config & W83795_REG_CONFIG_CONFIG48)
Jean Delvare093d1a42010-10-28 20:31:45 +02001768 chip_name = "w83795adg";
Jean Delvare2be381d2010-10-28 20:31:45 +02001769 else
Jean Delvare093d1a42010-10-28 20:31:45 +02001770 chip_name = "w83795g";
Wei Song792d3762010-10-28 20:31:44 +02001771
Jean Delvare093d1a42010-10-28 20:31:45 +02001772 strlcpy(info->type, chip_name, I2C_NAME_SIZE);
Jean Delvare2be381d2010-10-28 20:31:45 +02001773 dev_info(&adapter->dev, "Found %s rev. %c at 0x%02hx\n", chip_name,
1774 'A' + (device_id & 0xf), address);
Wei Song792d3762010-10-28 20:31:44 +02001775
1776 return 0;
1777}
1778
Jean Delvare6f3dcde2010-10-28 20:31:45 +02001779static int w83795_handle_files(struct device *dev, int (*fn)(struct device *,
1780 const struct device_attribute *))
Jean Delvare892514a2010-10-28 20:31:45 +02001781{
1782 struct w83795_data *data = dev_get_drvdata(dev);
Jean Delvare87df0da2010-10-28 20:31:45 +02001783 int err, i, j;
Jean Delvare892514a2010-10-28 20:31:45 +02001784
1785 for (i = 0; i < ARRAY_SIZE(w83795_in); i++) {
Jean Delvare87df0da2010-10-28 20:31:45 +02001786 if (!(data->has_in & (1 << i)))
Jean Delvare892514a2010-10-28 20:31:45 +02001787 continue;
Jean Delvare87df0da2010-10-28 20:31:45 +02001788 for (j = 0; j < ARRAY_SIZE(w83795_in[0]); j++) {
1789 err = fn(dev, &w83795_in[i][j].dev_attr);
1790 if (err)
1791 return err;
1792 }
Jean Delvare892514a2010-10-28 20:31:45 +02001793 }
1794
1795 for (i = 0; i < ARRAY_SIZE(w83795_fan); i++) {
Jean Delvare87df0da2010-10-28 20:31:45 +02001796 if (!(data->has_fan & (1 << i)))
Jean Delvare892514a2010-10-28 20:31:45 +02001797 continue;
Jean Delvare87df0da2010-10-28 20:31:45 +02001798 for (j = 0; j < ARRAY_SIZE(w83795_fan[0]); j++) {
1799 err = fn(dev, &w83795_fan[i][j].dev_attr);
1800 if (err)
1801 return err;
1802 }
Jean Delvare892514a2010-10-28 20:31:45 +02001803 }
1804
1805 for (i = 0; i < ARRAY_SIZE(sda_single_files); i++) {
Jean Delvare6f3dcde2010-10-28 20:31:45 +02001806 err = fn(dev, &sda_single_files[i].dev_attr);
Jean Delvare892514a2010-10-28 20:31:45 +02001807 if (err)
1808 return err;
1809 }
1810
Jean Delvareb5f6a902010-10-28 20:31:46 +02001811 for (i = 0; i < data->has_pwm; i++) {
1812 for (j = 0; j < ARRAY_SIZE(w83795_pwm[0]); j++) {
1813 err = fn(dev, &w83795_pwm[i][j].dev_attr);
Jean Delvare892514a2010-10-28 20:31:45 +02001814 if (err)
1815 return err;
1816 }
1817 }
1818
1819 for (i = 0; i < ARRAY_SIZE(w83795_temp); i++) {
Jean Delvare87df0da2010-10-28 20:31:45 +02001820 if (!(data->has_temp & (1 << i)))
Jean Delvare892514a2010-10-28 20:31:45 +02001821 continue;
Jean Delvare87df0da2010-10-28 20:31:45 +02001822 for (j = 0; j < ARRAY_SIZE(w83795_temp[0]); j++) {
1823 err = fn(dev, &w83795_temp[i][j].dev_attr);
1824 if (err)
1825 return err;
1826 }
Jean Delvare892514a2010-10-28 20:31:45 +02001827 }
1828
1829 if (data->enable_dts != 0) {
1830 for (i = 0; i < ARRAY_SIZE(w83795_dts); i++) {
Jean Delvare87df0da2010-10-28 20:31:45 +02001831 if (!(data->has_dts & (1 << i)))
Jean Delvare892514a2010-10-28 20:31:45 +02001832 continue;
Jean Delvare87df0da2010-10-28 20:31:45 +02001833 for (j = 0; j < ARRAY_SIZE(w83795_dts[0]); j++) {
1834 err = fn(dev, &w83795_dts[i][j].dev_attr);
1835 if (err)
1836 return err;
1837 }
Jean Delvare892514a2010-10-28 20:31:45 +02001838 }
1839 }
1840
Jean Delvare892514a2010-10-28 20:31:45 +02001841 return 0;
1842}
1843
Jean Delvare6f3dcde2010-10-28 20:31:45 +02001844/* We need a wrapper that fits in w83795_handle_files */
1845static int device_remove_file_wrapper(struct device *dev,
1846 const struct device_attribute *attr)
Jean Delvare2fa09872010-10-28 20:31:45 +02001847{
Jean Delvare6f3dcde2010-10-28 20:31:45 +02001848 device_remove_file(dev, attr);
1849 return 0;
Jean Delvare2fa09872010-10-28 20:31:45 +02001850}
1851
Jean Delvare0e256012010-10-28 20:31:46 +02001852static void w83795_check_dynamic_in_limits(struct i2c_client *client)
1853{
1854 struct w83795_data *data = i2c_get_clientdata(client);
1855 u8 vid_ctl;
1856 int i, err_max, err_min;
1857
1858 vid_ctl = w83795_read(client, W83795_REG_VID_CTRL);
1859
1860 /* Return immediately if VRM isn't configured */
1861 if ((vid_ctl & 0x07) == 0x00 || (vid_ctl & 0x07) == 0x07)
1862 return;
1863
1864 data->has_dyn_in = (vid_ctl >> 3) & 0x07;
1865 for (i = 0; i < 2; i++) {
1866 if (!(data->has_dyn_in & (1 << i)))
1867 continue;
1868
1869 /* Voltage limits in dynamic mode, switch to read-only */
1870 err_max = sysfs_chmod_file(&client->dev.kobj,
1871 &w83795_in[i][2].dev_attr.attr,
1872 S_IRUGO);
1873 err_min = sysfs_chmod_file(&client->dev.kobj,
1874 &w83795_in[i][3].dev_attr.attr,
1875 S_IRUGO);
1876 if (err_max || err_min)
1877 dev_warn(&client->dev, "Failed to set in%d limits "
1878 "read-only (%d, %d)\n", i, err_max, err_min);
1879 else
1880 dev_info(&client->dev, "in%d limits set dynamically "
1881 "from VID\n", i);
1882 }
1883}
1884
Jean Delvare71caf462010-10-28 20:31:46 +02001885/* Check pins that can be used for either temperature or voltage monitoring */
1886static void w83795_apply_temp_config(struct w83795_data *data, u8 config,
1887 int temp_chan, int in_chan)
1888{
1889 /* config is a 2-bit value */
1890 switch (config) {
1891 case 0x2: /* Voltage monitoring */
1892 data->has_in |= 1 << in_chan;
1893 break;
1894 case 0x1: /* Thermal diode */
1895 if (temp_chan >= 4)
1896 break;
1897 data->temp_mode |= 1 << temp_chan;
1898 /* fall through */
1899 case 0x3: /* Thermistor */
1900 data->has_temp |= 1 << temp_chan;
1901 break;
1902 }
1903}
1904
Wei Song792d3762010-10-28 20:31:44 +02001905static int w83795_probe(struct i2c_client *client,
1906 const struct i2c_device_id *id)
1907{
1908 int i;
1909 u8 tmp;
1910 struct device *dev = &client->dev;
1911 struct w83795_data *data;
Jean Delvare71caf462010-10-28 20:31:46 +02001912 int err;
Wei Song792d3762010-10-28 20:31:44 +02001913
1914 data = kzalloc(sizeof(struct w83795_data), GFP_KERNEL);
1915 if (!data) {
1916 err = -ENOMEM;
1917 goto exit;
1918 }
1919
1920 i2c_set_clientdata(client, data);
Jean Delvare093d1a42010-10-28 20:31:45 +02001921 data->chip_type = id->driver_data;
Wei Song792d3762010-10-28 20:31:44 +02001922 data->bank = i2c_smbus_read_byte_data(client, W83795_REG_BANKSEL);
1923 mutex_init(&data->update_lock);
1924
1925 /* Initialize the chip */
1926 w83795_init_client(client);
1927
Jean Delvare71caf462010-10-28 20:31:46 +02001928 /* Check which voltages and fans are present */
1929 data->has_in = w83795_read(client, W83795_REG_VOLT_CTRL1)
1930 | (w83795_read(client, W83795_REG_VOLT_CTRL2) << 8);
1931 data->has_fan = w83795_read(client, W83795_REG_FANIN_CTRL1)
1932 | (w83795_read(client, W83795_REG_FANIN_CTRL2) << 8);
Wei Song792d3762010-10-28 20:31:44 +02001933
Jean Delvare71caf462010-10-28 20:31:46 +02001934 /* Check which analog temperatures and extra voltages are present */
Wei Song792d3762010-10-28 20:31:44 +02001935 tmp = w83795_read(client, W83795_REG_TEMP_CTRL1);
1936 if (tmp & 0x20)
1937 data->enable_dts = 1;
Jean Delvare71caf462010-10-28 20:31:46 +02001938 w83795_apply_temp_config(data, (tmp >> 2) & 0x3, 5, 16);
1939 w83795_apply_temp_config(data, tmp & 0x3, 4, 15);
Wei Song792d3762010-10-28 20:31:44 +02001940 tmp = w83795_read(client, W83795_REG_TEMP_CTRL2);
Jean Delvare71caf462010-10-28 20:31:46 +02001941 w83795_apply_temp_config(data, tmp >> 6, 3, 20);
1942 w83795_apply_temp_config(data, (tmp >> 4) & 0x3, 2, 19);
1943 w83795_apply_temp_config(data, (tmp >> 2) & 0x3, 1, 18);
1944 w83795_apply_temp_config(data, tmp & 0x3, 0, 17);
Wei Song792d3762010-10-28 20:31:44 +02001945
1946 /* Check DTS enable status */
Jean Delvare71caf462010-10-28 20:31:46 +02001947 if (data->enable_dts) {
Wei Song792d3762010-10-28 20:31:44 +02001948 if (1 & w83795_read(client, W83795_REG_DTSC))
1949 data->enable_dts |= 2;
1950 data->has_dts = w83795_read(client, W83795_REG_DTSE);
1951 }
1952
Jean Delvare54891a32010-10-28 20:31:47 +02001953 /* Report PECI Tbase values */
1954 if (data->enable_dts == 1) {
1955 for (i = 0; i < 8; i++) {
1956 if (!(data->has_dts & (1 << i)))
1957 continue;
1958 tmp = w83795_read(client, W83795_REG_PECI_TBASE(i));
1959 dev_info(&client->dev,
1960 "PECI agent %d Tbase temperature: %u\n",
1961 i + 1, (unsigned int)tmp & 0x7f);
1962 }
1963 }
1964
Wei Song792d3762010-10-28 20:31:44 +02001965 /* First update the voltages measured value and limits */
1966 for (i = 0; i < ARRAY_SIZE(data->in); i++) {
1967 if (!(data->has_in & (1 << i)))
1968 continue;
1969 data->in[i][IN_MAX] =
1970 w83795_read(client, W83795_REG_IN[i][IN_MAX]);
1971 data->in[i][IN_LOW] =
1972 w83795_read(client, W83795_REG_IN[i][IN_LOW]);
1973 tmp = w83795_read(client, W83795_REG_IN[i][IN_READ]) << 2;
Jean Delvarea654b9d42010-10-28 20:31:47 +02001974 tmp |= w83795_read(client, W83795_REG_VRLSB) >> 6;
Wei Song792d3762010-10-28 20:31:44 +02001975 data->in[i][IN_READ] = tmp;
1976 }
Jean Delvarecd316df2010-10-28 20:31:48 +02001977 for (i = 0; i < ARRAY_SIZE(data->in_lsb); i++) {
Jean Delvarec1a792a2010-10-28 20:31:47 +02001978 if ((i == 2 && data->chip_type == w83795adg) ||
1979 (i >= 4 && !(data->has_in & (1 << (i + 11)))))
1980 continue;
Wei Song792d3762010-10-28 20:31:44 +02001981 data->in_lsb[i][IN_MAX] =
1982 w83795_read(client, IN_LSB_REG(i, IN_MAX));
1983 data->in_lsb[i][IN_LOW] =
1984 w83795_read(client, IN_LSB_REG(i, IN_LOW));
1985 }
1986 data->has_gain = w83795_read(client, W83795_REG_VMIGB_CTRL) & 0x0f;
1987
1988 /* First update fan and limits */
1989 for (i = 0; i < ARRAY_SIZE(data->fan); i++) {
Jean Delvarec1a792a2010-10-28 20:31:47 +02001990 /* Each register contains LSB for 2 fans, but we want to
1991 * read it only once to save time */
1992 if ((i & 1) == 0 && (data->has_fan & (3 << i)))
1993 tmp = w83795_read(client, W83795_REG_FAN_MIN_LSB(i));
1994
Wei Song792d3762010-10-28 20:31:44 +02001995 if (!(data->has_fan & (1 << i)))
1996 continue;
1997 data->fan_min[i] =
1998 w83795_read(client, W83795_REG_FAN_MIN_HL(i)) << 4;
1999 data->fan_min[i] |=
Jean Delvarec1a792a2010-10-28 20:31:47 +02002000 (tmp >> W83795_REG_FAN_MIN_LSB_SHIFT(i)) & 0x0F;
Wei Song792d3762010-10-28 20:31:44 +02002001 data->fan[i] = w83795_read(client, W83795_REG_FAN(i)) << 4;
Jean Delvarea654b9d42010-10-28 20:31:47 +02002002 data->fan[i] |= w83795_read(client, W83795_REG_VRLSB) >> 4;
Wei Song792d3762010-10-28 20:31:44 +02002003 }
2004
2005 /* temperature and limits */
2006 for (i = 0; i < ARRAY_SIZE(data->temp); i++) {
2007 if (!(data->has_temp & (1 << i)))
2008 continue;
2009 data->temp[i][TEMP_CRIT] =
2010 w83795_read(client, W83795_REG_TEMP[i][TEMP_CRIT]);
2011 data->temp[i][TEMP_CRIT_HYST] =
2012 w83795_read(client, W83795_REG_TEMP[i][TEMP_CRIT_HYST]);
2013 data->temp[i][TEMP_WARN] =
2014 w83795_read(client, W83795_REG_TEMP[i][TEMP_WARN]);
2015 data->temp[i][TEMP_WARN_HYST] =
2016 w83795_read(client, W83795_REG_TEMP[i][TEMP_WARN_HYST]);
2017 data->temp[i][TEMP_READ] =
2018 w83795_read(client, W83795_REG_TEMP[i][TEMP_READ]);
2019 data->temp_read_vrlsb[i] =
2020 w83795_read(client, W83795_REG_VRLSB);
2021 }
2022
2023 /* dts temperature and limits */
2024 if (data->enable_dts != 0) {
2025 data->dts_ext[DTS_CRIT] =
2026 w83795_read(client, W83795_REG_DTS_EXT(DTS_CRIT));
2027 data->dts_ext[DTS_CRIT_HYST] =
2028 w83795_read(client, W83795_REG_DTS_EXT(DTS_CRIT_HYST));
2029 data->dts_ext[DTS_WARN] =
2030 w83795_read(client, W83795_REG_DTS_EXT(DTS_WARN));
2031 data->dts_ext[DTS_WARN_HYST] =
2032 w83795_read(client, W83795_REG_DTS_EXT(DTS_WARN_HYST));
2033 for (i = 0; i < ARRAY_SIZE(data->dts); i++) {
2034 if (!(data->has_dts & (1 << i)))
2035 continue;
2036 data->dts[i] = w83795_read(client, W83795_REG_DTS(i));
2037 data->dts_read_vrlsb[i] =
2038 w83795_read(client, W83795_REG_VRLSB);
2039 }
2040 }
2041
2042 /* First update temp source selction */
2043 for (i = 0; i < 3; i++)
2044 data->temp_src[i] = w83795_read(client, W83795_REG_TSS(i));
2045
2046 /* pwm and smart fan */
2047 if (data->chip_type == w83795g)
2048 data->has_pwm = 8;
2049 else
2050 data->has_pwm = 2;
2051 data->pwm_fcms[0] = w83795_read(client, W83795_REG_FCMS1);
2052 data->pwm_fcms[1] = w83795_read(client, W83795_REG_FCMS2);
Jean Delvarecd316df2010-10-28 20:31:48 +02002053 for (i = 0; i < ARRAY_SIZE(data->pwm_tfmr); i++)
Wei Song792d3762010-10-28 20:31:44 +02002054 data->pwm_tfmr[i] = w83795_read(client, W83795_REG_TFMR(i));
2055 data->pwm_fomc = w83795_read(client, W83795_REG_FOMC);
2056 for (i = 0; i < data->has_pwm; i++) {
2057 for (tmp = 0; tmp < 5; tmp++) {
2058 data->pwm[i][tmp] =
2059 w83795_read(client, W83795_REG_PWM(i, tmp));
2060 }
2061 }
2062 for (i = 0; i < 8; i++) {
2063 data->target_speed[i] =
2064 w83795_read(client, W83795_REG_FTSH(i)) << 4;
2065 data->target_speed[i] |=
2066 w83795_read(client, W83795_REG_FTSL(i)) >> 4;
2067 }
2068 data->tol_speed = w83795_read(client, W83795_REG_TFTS) & 0x3f;
2069
Jean Delvarecd316df2010-10-28 20:31:48 +02002070 for (i = 0; i < ARRAY_SIZE(data->pwm_temp); i++) {
Wei Song792d3762010-10-28 20:31:44 +02002071 data->pwm_temp[i][TEMP_PWM_TTTI] =
2072 w83795_read(client, W83795_REG_TTTI(i)) & 0x7f;
2073 data->pwm_temp[i][TEMP_PWM_CTFS] =
2074 w83795_read(client, W83795_REG_CTFS(i));
2075 tmp = w83795_read(client, W83795_REG_HT(i));
2076 data->pwm_temp[i][TEMP_PWM_HCT] = (tmp >> 4) & 0x0f;
2077 data->pwm_temp[i][TEMP_PWM_HOT] = tmp & 0x0f;
2078 }
Jean Delvarecd316df2010-10-28 20:31:48 +02002079 for (i = 0; i < ARRAY_SIZE(data->sf4_reg); i++) {
Wei Song792d3762010-10-28 20:31:44 +02002080 for (tmp = 0; tmp < 7; tmp++) {
2081 data->sf4_reg[i][SF4_TEMP][tmp] =
2082 w83795_read(client,
2083 W83795_REG_SF4_TEMP(i, tmp));
2084 data->sf4_reg[i][SF4_PWM][tmp] =
2085 w83795_read(client, W83795_REG_SF4_PWM(i, tmp));
2086 }
2087 }
2088
2089 /* Setup PWM Register */
2090 for (i = 0; i < 3; i++) {
2091 data->setup_pwm[i] =
2092 w83795_read(client, W83795_REG_SETUP_PWM(i));
2093 }
2094
2095 /* alarm and beep */
Jean Delvarecd316df2010-10-28 20:31:48 +02002096 for (i = 0; i < ARRAY_SIZE(data->alarms); i++) {
Wei Song792d3762010-10-28 20:31:44 +02002097 data->alarms[i] = w83795_read(client, W83795_REG_ALARM(i));
2098 data->beeps[i] = w83795_read(client, W83795_REG_BEEP(i));
2099 }
Wei Song792d3762010-10-28 20:31:44 +02002100
Jean Delvare6f3dcde2010-10-28 20:31:45 +02002101 err = w83795_handle_files(dev, device_create_file);
Jean Delvare892514a2010-10-28 20:31:45 +02002102 if (err)
2103 goto exit_remove;
Wei Song792d3762010-10-28 20:31:44 +02002104
Jean Delvare0e256012010-10-28 20:31:46 +02002105 if (data->chip_type == w83795g)
2106 w83795_check_dynamic_in_limits(client);
2107
Wei Song792d3762010-10-28 20:31:44 +02002108 data->hwmon_dev = hwmon_device_register(dev);
2109 if (IS_ERR(data->hwmon_dev)) {
2110 err = PTR_ERR(data->hwmon_dev);
2111 goto exit_remove;
2112 }
2113
2114 return 0;
2115
Wei Song792d3762010-10-28 20:31:44 +02002116exit_remove:
Jean Delvare6f3dcde2010-10-28 20:31:45 +02002117 w83795_handle_files(dev, device_remove_file_wrapper);
Wei Song792d3762010-10-28 20:31:44 +02002118 kfree(data);
2119exit:
2120 return err;
2121}
2122
2123static int w83795_remove(struct i2c_client *client)
2124{
2125 struct w83795_data *data = i2c_get_clientdata(client);
Wei Song792d3762010-10-28 20:31:44 +02002126
2127 hwmon_device_unregister(data->hwmon_dev);
Jean Delvare6f3dcde2010-10-28 20:31:45 +02002128 w83795_handle_files(&client->dev, device_remove_file_wrapper);
Wei Song792d3762010-10-28 20:31:44 +02002129 kfree(data);
2130
2131 return 0;
2132}
2133
2134
2135static const struct i2c_device_id w83795_id[] = {
Jean Delvare093d1a42010-10-28 20:31:45 +02002136 { "w83795g", w83795g },
2137 { "w83795adg", w83795adg },
Wei Song792d3762010-10-28 20:31:44 +02002138 { }
2139};
2140MODULE_DEVICE_TABLE(i2c, w83795_id);
2141
2142static struct i2c_driver w83795_driver = {
2143 .driver = {
2144 .name = "w83795",
2145 },
2146 .probe = w83795_probe,
2147 .remove = w83795_remove,
2148 .id_table = w83795_id,
2149
2150 .class = I2C_CLASS_HWMON,
2151 .detect = w83795_detect,
2152 .address_list = normal_i2c,
2153};
2154
2155static int __init sensors_w83795_init(void)
2156{
2157 return i2c_add_driver(&w83795_driver);
2158}
2159
2160static void __exit sensors_w83795_exit(void)
2161{
2162 i2c_del_driver(&w83795_driver);
2163}
2164
2165MODULE_AUTHOR("Wei Song");
Jean Delvare315bacf2010-10-28 20:31:45 +02002166MODULE_DESCRIPTION("W83795G/ADG hardware monitoring driver");
Wei Song792d3762010-10-28 20:31:44 +02002167MODULE_LICENSE("GPL");
2168
2169module_init(sensors_w83795_init);
2170module_exit(sensors_w83795_exit);