blob: 400558d97f3d16af63edc09769341a717cef29f4 [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
Jean Delvaree3760b42010-10-28 20:31:49 +02005 * Copyright (C) 2010 Jean Delvare <khali@linux-fr.org>
Wei Song792d3762010-10-28 20:31:44 +02006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation - version 2.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 * 02110-1301 USA.
20 *
21 * Supports following chips:
22 *
23 * Chip #vin #fanin #pwm #temp #dts wchipid vendid i2c ISA
24 * w83795g 21 14 8 6 8 0x79 0x5ca3 yes no
25 * w83795adg 18 14 2 6 8 0x79 0x5ca3 yes no
26 */
27
28#include <linux/kernel.h>
29#include <linux/module.h>
30#include <linux/init.h>
31#include <linux/slab.h>
32#include <linux/i2c.h>
33#include <linux/hwmon.h>
34#include <linux/hwmon-sysfs.h>
35#include <linux/err.h>
36#include <linux/mutex.h>
37#include <linux/delay.h>
38
39/* Addresses to scan */
Jean Delvare86ef4d22010-10-28 20:31:46 +020040static const unsigned short normal_i2c[] = {
41 0x2c, 0x2d, 0x2e, 0x2f, I2C_CLIENT_END
42};
Wei Song792d3762010-10-28 20:31:44 +020043
Wei Song792d3762010-10-28 20:31:44 +020044
45static int reset;
46module_param(reset, bool, 0);
47MODULE_PARM_DESC(reset, "Set to 1 to reset chip, not recommended");
48
49
50#define W83795_REG_BANKSEL 0x00
51#define W83795_REG_VENDORID 0xfd
52#define W83795_REG_CHIPID 0xfe
53#define W83795_REG_DEVICEID 0xfb
Jean Delvare2be381d2010-10-28 20:31:45 +020054#define W83795_REG_DEVICEID_A 0xff
Wei Song792d3762010-10-28 20:31:44 +020055
56#define W83795_REG_I2C_ADDR 0xfc
57#define W83795_REG_CONFIG 0x01
58#define W83795_REG_CONFIG_CONFIG48 0x04
Jean Delvare80646b92010-10-28 20:31:46 +020059#define W83795_REG_CONFIG_START 0x01
Wei Song792d3762010-10-28 20:31:44 +020060
61/* Multi-Function Pin Ctrl Registers */
62#define W83795_REG_VOLT_CTRL1 0x02
63#define W83795_REG_VOLT_CTRL2 0x03
64#define W83795_REG_TEMP_CTRL1 0x04
65#define W83795_REG_TEMP_CTRL2 0x05
66#define W83795_REG_FANIN_CTRL1 0x06
67#define W83795_REG_FANIN_CTRL2 0x07
68#define W83795_REG_VMIGB_CTRL 0x08
69
Wei Song792d3762010-10-28 20:31:44 +020070#define TEMP_READ 0
71#define TEMP_CRIT 1
72#define TEMP_CRIT_HYST 2
73#define TEMP_WARN 3
74#define TEMP_WARN_HYST 4
75/* only crit and crit_hyst affect real-time alarm status
76 * current crit crit_hyst warn warn_hyst */
Jean Delvare86ef4d22010-10-28 20:31:46 +020077static const u16 W83795_REG_TEMP[][5] = {
Wei Song792d3762010-10-28 20:31:44 +020078 {0x21, 0x96, 0x97, 0x98, 0x99}, /* TD1/TR1 */
79 {0x22, 0x9a, 0x9b, 0x9c, 0x9d}, /* TD2/TR2 */
80 {0x23, 0x9e, 0x9f, 0xa0, 0xa1}, /* TD3/TR3 */
81 {0x24, 0xa2, 0xa3, 0xa4, 0xa5}, /* TD4/TR4 */
82 {0x1f, 0xa6, 0xa7, 0xa8, 0xa9}, /* TR5 */
83 {0x20, 0xaa, 0xab, 0xac, 0xad}, /* TR6 */
84};
85
86#define IN_READ 0
87#define IN_MAX 1
88#define IN_LOW 2
89static const u16 W83795_REG_IN[][3] = {
90 /* Current, HL, LL */
91 {0x10, 0x70, 0x71}, /* VSEN1 */
92 {0x11, 0x72, 0x73}, /* VSEN2 */
93 {0x12, 0x74, 0x75}, /* VSEN3 */
94 {0x13, 0x76, 0x77}, /* VSEN4 */
95 {0x14, 0x78, 0x79}, /* VSEN5 */
96 {0x15, 0x7a, 0x7b}, /* VSEN6 */
97 {0x16, 0x7c, 0x7d}, /* VSEN7 */
98 {0x17, 0x7e, 0x7f}, /* VSEN8 */
99 {0x18, 0x80, 0x81}, /* VSEN9 */
100 {0x19, 0x82, 0x83}, /* VSEN10 */
101 {0x1A, 0x84, 0x85}, /* VSEN11 */
102 {0x1B, 0x86, 0x87}, /* VTT */
103 {0x1C, 0x88, 0x89}, /* 3VDD */
104 {0x1D, 0x8a, 0x8b}, /* 3VSB */
105 {0x1E, 0x8c, 0x8d}, /* VBAT */
106 {0x1F, 0xa6, 0xa7}, /* VSEN12 */
107 {0x20, 0xaa, 0xab}, /* VSEN13 */
108 {0x21, 0x96, 0x97}, /* VSEN14 */
109 {0x22, 0x9a, 0x9b}, /* VSEN15 */
110 {0x23, 0x9e, 0x9f}, /* VSEN16 */
111 {0x24, 0xa2, 0xa3}, /* VSEN17 */
112};
113#define W83795_REG_VRLSB 0x3C
Wei Song792d3762010-10-28 20:31:44 +0200114
115static const u8 W83795_REG_IN_HL_LSB[] = {
116 0x8e, /* VSEN1-4 */
117 0x90, /* VSEN5-8 */
118 0x92, /* VSEN9-11 */
119 0x94, /* VTT, 3VDD, 3VSB, 3VBAT */
120 0xa8, /* VSEN12 */
121 0xac, /* VSEN13 */
122 0x98, /* VSEN14 */
123 0x9c, /* VSEN15 */
124 0xa0, /* VSEN16 */
125 0xa4, /* VSEN17 */
126};
127
128#define IN_LSB_REG(index, type) \
129 (((type) == 1) ? W83795_REG_IN_HL_LSB[(index)] \
130 : (W83795_REG_IN_HL_LSB[(index)] + 1))
131
Wei Song792d3762010-10-28 20:31:44 +0200132#define IN_LSB_SHIFT 0
133#define IN_LSB_IDX 1
134static const u8 IN_LSB_SHIFT_IDX[][2] = {
135 /* High/Low LSB shift, LSB No. */
136 {0x00, 0x00}, /* VSEN1 */
137 {0x02, 0x00}, /* VSEN2 */
138 {0x04, 0x00}, /* VSEN3 */
139 {0x06, 0x00}, /* VSEN4 */
140 {0x00, 0x01}, /* VSEN5 */
141 {0x02, 0x01}, /* VSEN6 */
142 {0x04, 0x01}, /* VSEN7 */
143 {0x06, 0x01}, /* VSEN8 */
144 {0x00, 0x02}, /* VSEN9 */
145 {0x02, 0x02}, /* VSEN10 */
146 {0x04, 0x02}, /* VSEN11 */
147 {0x00, 0x03}, /* VTT */
148 {0x02, 0x03}, /* 3VDD */
149 {0x04, 0x03}, /* 3VSB */
150 {0x06, 0x03}, /* VBAT */
151 {0x06, 0x04}, /* VSEN12 */
152 {0x06, 0x05}, /* VSEN13 */
153 {0x06, 0x06}, /* VSEN14 */
154 {0x06, 0x07}, /* VSEN15 */
155 {0x06, 0x08}, /* VSEN16 */
156 {0x06, 0x09}, /* VSEN17 */
157};
158
159
Wei Song792d3762010-10-28 20:31:44 +0200160#define W83795_REG_FAN(index) (0x2E + (index))
161#define W83795_REG_FAN_MIN_HL(index) (0xB6 + (index))
162#define W83795_REG_FAN_MIN_LSB(index) (0xC4 + (index) / 2)
163#define W83795_REG_FAN_MIN_LSB_SHIFT(index) \
Jean Delvare7eb8d502010-10-28 20:31:47 +0200164 (((index) & 1) ? 4 : 0)
Wei Song792d3762010-10-28 20:31:44 +0200165
166#define W83795_REG_VID_CTRL 0x6A
167
Wei Song792d3762010-10-28 20:31:44 +0200168#define W83795_REG_ALARM(index) (0x41 + (index))
169#define W83795_REG_BEEP(index) (0x50 + (index))
170
171#define W83795_REG_CLR_CHASSIS 0x4D
172
173
Wei Song792d3762010-10-28 20:31:44 +0200174#define W83795_REG_FCMS1 0x201
175#define W83795_REG_FCMS2 0x208
176#define W83795_REG_TFMR(index) (0x202 + (index))
177#define W83795_REG_FOMC 0x20F
Wei Song792d3762010-10-28 20:31:44 +0200178
179#define W83795_REG_TSS(index) (0x209 + (index))
180
Jean Delvareedff2f82010-11-15 21:38:56 +0100181#define TSS_MAP_RESERVED 0xff
182static const u8 tss_map[4][6] = {
183 { 0, 1, 2, 3, 4, 5},
184 { 6, 7, 8, 9, 0, 1},
185 {10, 11, 12, 13, 2, 3},
186 { 4, 5, 4, 5, TSS_MAP_RESERVED, TSS_MAP_RESERVED},
187};
188
Wei Song792d3762010-10-28 20:31:44 +0200189#define PWM_OUTPUT 0
Jean Delvarefd7f82b2010-10-28 20:31:48 +0200190#define PWM_FREQ 1
191#define PWM_START 2
192#define PWM_NONSTOP 3
193#define PWM_STOP_TIME 4
194#define W83795_REG_PWM(index, nr) (0x210 + (nr) * 8 + (index))
Wei Song792d3762010-10-28 20:31:44 +0200195
Wei Song792d3762010-10-28 20:31:44 +0200196#define W83795_REG_FTSH(index) (0x240 + (index) * 2)
197#define W83795_REG_FTSL(index) (0x241 + (index) * 2)
198#define W83795_REG_TFTS 0x250
199
200#define TEMP_PWM_TTTI 0
201#define TEMP_PWM_CTFS 1
202#define TEMP_PWM_HCT 2
203#define TEMP_PWM_HOT 3
204#define W83795_REG_TTTI(index) (0x260 + (index))
205#define W83795_REG_CTFS(index) (0x268 + (index))
206#define W83795_REG_HT(index) (0x270 + (index))
207
208#define SF4_TEMP 0
209#define SF4_PWM 1
210#define W83795_REG_SF4_TEMP(temp_num, index) \
211 (0x280 + 0x10 * (temp_num) + (index))
212#define W83795_REG_SF4_PWM(temp_num, index) \
213 (0x288 + 0x10 * (temp_num) + (index))
214
215#define W83795_REG_DTSC 0x301
216#define W83795_REG_DTSE 0x302
217#define W83795_REG_DTS(index) (0x26 + (index))
Jean Delvare54891a32010-10-28 20:31:47 +0200218#define W83795_REG_PECI_TBASE(index) (0x320 + (index))
Wei Song792d3762010-10-28 20:31:44 +0200219
220#define DTS_CRIT 0
221#define DTS_CRIT_HYST 1
222#define DTS_WARN 2
223#define DTS_WARN_HYST 3
224#define W83795_REG_DTS_EXT(index) (0xB2 + (index))
225
226#define SETUP_PWM_DEFAULT 0
227#define SETUP_PWM_UPTIME 1
228#define SETUP_PWM_DOWNTIME 2
229#define W83795_REG_SETUP_PWM(index) (0x20C + (index))
230
231static inline u16 in_from_reg(u8 index, u16 val)
232{
Jean Delvare49c73472010-10-28 20:31:48 +0200233 /* 3VDD, 3VSB and VBAT: 6 mV/bit; other inputs: 2 mV/bit */
234 if (index >= 12 && index <= 14)
Wei Song792d3762010-10-28 20:31:44 +0200235 return val * 6;
236 else
237 return val * 2;
238}
239
240static inline u16 in_to_reg(u8 index, u16 val)
241{
Jean Delvare49c73472010-10-28 20:31:48 +0200242 if (index >= 12 && index <= 14)
Wei Song792d3762010-10-28 20:31:44 +0200243 return val / 6;
244 else
245 return val / 2;
246}
247
248static inline unsigned long fan_from_reg(u16 val)
249{
Jean Delvare6c82b2f2010-10-28 20:31:46 +0200250 if ((val == 0xfff) || (val == 0))
Wei Song792d3762010-10-28 20:31:44 +0200251 return 0;
252 return 1350000UL / val;
253}
254
255static inline u16 fan_to_reg(long rpm)
256{
257 if (rpm <= 0)
258 return 0x0fff;
259 return SENSORS_LIMIT((1350000 + (rpm >> 1)) / rpm, 1, 0xffe);
260}
261
262static inline unsigned long time_from_reg(u8 reg)
263{
264 return reg * 100;
265}
266
267static inline u8 time_to_reg(unsigned long val)
268{
269 return SENSORS_LIMIT((val + 50) / 100, 0, 0xff);
270}
271
272static inline long temp_from_reg(s8 reg)
273{
274 return reg * 1000;
275}
276
277static inline s8 temp_to_reg(long val, s8 min, s8 max)
278{
Jean Delvaredd127f52010-10-28 20:31:47 +0200279 return SENSORS_LIMIT(val / 1000, min, max);
Wei Song792d3762010-10-28 20:31:44 +0200280}
281
Jean Delvare01879a82010-10-28 20:31:47 +0200282static const u16 pwm_freq_cksel0[16] = {
283 1024, 512, 341, 256, 205, 171, 146, 128,
284 85, 64, 32, 16, 8, 4, 2, 1
285};
286
287static unsigned int pwm_freq_from_reg(u8 reg, u16 clkin)
288{
289 unsigned long base_clock;
290
291 if (reg & 0x80) {
292 base_clock = clkin * 1000 / ((clkin == 48000) ? 384 : 256);
293 return base_clock / ((reg & 0x7f) + 1);
294 } else
295 return pwm_freq_cksel0[reg & 0x0f];
296}
297
298static u8 pwm_freq_to_reg(unsigned long val, u16 clkin)
299{
300 unsigned long base_clock;
301 u8 reg0, reg1;
302 unsigned long best0, best1;
303
304 /* Best fit for cksel = 0 */
305 for (reg0 = 0; reg0 < ARRAY_SIZE(pwm_freq_cksel0) - 1; reg0++) {
306 if (val > (pwm_freq_cksel0[reg0] +
307 pwm_freq_cksel0[reg0 + 1]) / 2)
308 break;
309 }
310 if (val < 375) /* cksel = 1 can't beat this */
311 return reg0;
312 best0 = pwm_freq_cksel0[reg0];
313
314 /* Best fit for cksel = 1 */
315 base_clock = clkin * 1000 / ((clkin == 48000) ? 384 : 256);
316 reg1 = SENSORS_LIMIT(DIV_ROUND_CLOSEST(base_clock, val), 1, 128);
317 best1 = base_clock / reg1;
318 reg1 = 0x80 | (reg1 - 1);
319
320 /* Choose the closest one */
321 if (abs(val - best0) > abs(val - best1))
322 return reg1;
323 else
324 return reg0;
325}
Wei Song792d3762010-10-28 20:31:44 +0200326
327enum chip_types {w83795g, w83795adg};
328
329struct w83795_data {
330 struct device *hwmon_dev;
331 struct mutex update_lock;
332 unsigned long last_updated; /* In jiffies */
333 enum chip_types chip_type;
334
335 u8 bank;
336
337 u32 has_in; /* Enable monitor VIN or not */
Jean Delvare0e256012010-10-28 20:31:46 +0200338 u8 has_dyn_in; /* Only in2-0 can have this */
Wei Song792d3762010-10-28 20:31:44 +0200339 u16 in[21][3]; /* Register value, read/high/low */
340 u8 in_lsb[10][3]; /* LSB Register value, high/low */
341 u8 has_gain; /* has gain: in17-20 * 8 */
342
343 u16 has_fan; /* Enable fan14-1 or not */
344 u16 fan[14]; /* Register value combine */
345 u16 fan_min[14]; /* Register value combine */
346
347 u8 has_temp; /* Enable monitor temp6-1 or not */
Jean Delvaredd127f52010-10-28 20:31:47 +0200348 s8 temp[6][5]; /* current, crit, crit_hyst, warn, warn_hyst */
Wei Song792d3762010-10-28 20:31:44 +0200349 u8 temp_read_vrlsb[6];
Jean Delvare39deb692010-10-28 20:31:48 +0200350 u8 temp_mode; /* Bit vector, 0 = TR, 1 = TD */
Wei Song792d3762010-10-28 20:31:44 +0200351 u8 temp_src[3]; /* Register value */
352
353 u8 enable_dts; /* Enable PECI and SB-TSI,
354 * bit 0: =1 enable, =0 disable,
355 * bit 1: =1 AMD SB-TSI, =0 Intel PECI */
356 u8 has_dts; /* Enable monitor DTS temp */
Jean Delvaredd127f52010-10-28 20:31:47 +0200357 s8 dts[8]; /* Register value */
Wei Song792d3762010-10-28 20:31:44 +0200358 u8 dts_read_vrlsb[8]; /* Register value */
Jean Delvaredd127f52010-10-28 20:31:47 +0200359 s8 dts_ext[4]; /* Register value */
Wei Song792d3762010-10-28 20:31:44 +0200360
361 u8 has_pwm; /* 795g supports 8 pwm, 795adg only supports 2,
362 * no config register, only affected by chip
363 * type */
Jean Delvarefd7f82b2010-10-28 20:31:48 +0200364 u8 pwm[8][5]; /* Register value, output, freq, start,
365 * non stop, stop time */
Jean Delvare01879a82010-10-28 20:31:47 +0200366 u16 clkin; /* CLKIN frequency in kHz */
Wei Song792d3762010-10-28 20:31:44 +0200367 u8 pwm_fcms[2]; /* Register value */
368 u8 pwm_tfmr[6]; /* Register value */
369 u8 pwm_fomc; /* Register value */
370
371 u16 target_speed[8]; /* Register value, target speed for speed
372 * cruise */
373 u8 tol_speed; /* tolerance of target speed */
374 u8 pwm_temp[6][4]; /* TTTI, CTFS, HCT, HOT */
375 u8 sf4_reg[6][2][7]; /* 6 temp, temp/dcpwm, 7 registers */
376
377 u8 setup_pwm[3]; /* Register value */
378
379 u8 alarms[6]; /* Register value */
380 u8 beeps[6]; /* Register value */
Wei Song792d3762010-10-28 20:31:44 +0200381
382 char valid;
Jean Delvare2ae61de2010-10-28 20:31:48 +0200383 char valid_limits;
Jean Delvare1bb34502010-10-28 20:31:48 +0200384 char valid_pwm_config;
Wei Song792d3762010-10-28 20:31:44 +0200385};
386
387/*
388 * Hardware access
Jean Delvareb2469f42010-10-28 20:31:45 +0200389 * We assume that nobdody can change the bank outside the driver.
Wei Song792d3762010-10-28 20:31:44 +0200390 */
391
Jean Delvareb2469f42010-10-28 20:31:45 +0200392/* Must be called with data->update_lock held, except during initialization */
393static int w83795_set_bank(struct i2c_client *client, u8 bank)
Wei Song792d3762010-10-28 20:31:44 +0200394{
395 struct w83795_data *data = i2c_get_clientdata(client);
Jean Delvareb2469f42010-10-28 20:31:45 +0200396 int err;
Wei Song792d3762010-10-28 20:31:44 +0200397
Jean Delvareb2469f42010-10-28 20:31:45 +0200398 /* If the same bank is already set, nothing to do */
399 if ((data->bank & 0x07) == bank)
400 return 0;
401
402 /* Change to new bank, preserve all other bits */
403 bank |= data->bank & ~0x07;
404 err = i2c_smbus_write_byte_data(client, W83795_REG_BANKSEL, bank);
405 if (err < 0) {
406 dev_err(&client->dev,
407 "Failed to set bank to %d, err %d\n",
408 (int)bank, err);
409 return err;
Wei Song792d3762010-10-28 20:31:44 +0200410 }
Jean Delvareb2469f42010-10-28 20:31:45 +0200411 data->bank = bank;
412
413 return 0;
414}
415
416/* Must be called with data->update_lock held, except during initialization */
417static u8 w83795_read(struct i2c_client *client, u16 reg)
418{
419 int err;
420
421 err = w83795_set_bank(client, reg >> 8);
422 if (err < 0)
423 return 0x00; /* Arbitrary */
424
425 err = i2c_smbus_read_byte_data(client, reg & 0xff);
426 if (err < 0) {
427 dev_err(&client->dev,
428 "Failed to read from register 0x%03x, err %d\n",
429 (int)reg, err);
430 return 0x00; /* Arbitrary */
431 }
432 return err;
Wei Song792d3762010-10-28 20:31:44 +0200433}
434
435/* Must be called with data->update_lock held, except during initialization */
436static int w83795_write(struct i2c_client *client, u16 reg, u8 value)
437{
Jean Delvareb2469f42010-10-28 20:31:45 +0200438 int err;
Wei Song792d3762010-10-28 20:31:44 +0200439
Jean Delvareb2469f42010-10-28 20:31:45 +0200440 err = w83795_set_bank(client, reg >> 8);
441 if (err < 0)
442 return err;
Wei Song792d3762010-10-28 20:31:44 +0200443
Jean Delvareb2469f42010-10-28 20:31:45 +0200444 err = i2c_smbus_write_byte_data(client, reg & 0xff, value);
445 if (err < 0)
446 dev_err(&client->dev,
447 "Failed to write to register 0x%03x, err %d\n",
448 (int)reg, err);
449 return err;
Wei Song792d3762010-10-28 20:31:44 +0200450}
451
Jean Delvare0d7237bf2010-10-28 20:31:48 +0200452static void w83795_update_limits(struct i2c_client *client)
453{
454 struct w83795_data *data = i2c_get_clientdata(client);
455 int i, limit;
456
457 /* Read the voltage limits */
458 for (i = 0; i < ARRAY_SIZE(data->in); i++) {
459 if (!(data->has_in & (1 << i)))
460 continue;
461 data->in[i][IN_MAX] =
462 w83795_read(client, W83795_REG_IN[i][IN_MAX]);
463 data->in[i][IN_LOW] =
464 w83795_read(client, W83795_REG_IN[i][IN_LOW]);
465 }
466 for (i = 0; i < ARRAY_SIZE(data->in_lsb); i++) {
467 if ((i == 2 && data->chip_type == w83795adg) ||
468 (i >= 4 && !(data->has_in & (1 << (i + 11)))))
469 continue;
470 data->in_lsb[i][IN_MAX] =
471 w83795_read(client, IN_LSB_REG(i, IN_MAX));
472 data->in_lsb[i][IN_LOW] =
473 w83795_read(client, IN_LSB_REG(i, IN_LOW));
474 }
475
476 /* Read the fan limits */
477 for (i = 0; i < ARRAY_SIZE(data->fan); i++) {
478 u8 lsb;
479
480 /* Each register contains LSB for 2 fans, but we want to
481 * read it only once to save time */
482 if ((i & 1) == 0 && (data->has_fan & (3 << i)))
483 lsb = w83795_read(client, W83795_REG_FAN_MIN_LSB(i));
484
485 if (!(data->has_fan & (1 << i)))
486 continue;
487 data->fan_min[i] =
488 w83795_read(client, W83795_REG_FAN_MIN_HL(i)) << 4;
489 data->fan_min[i] |=
490 (lsb >> W83795_REG_FAN_MIN_LSB_SHIFT(i)) & 0x0F;
491 }
492
493 /* Read the temperature limits */
494 for (i = 0; i < ARRAY_SIZE(data->temp); i++) {
495 if (!(data->has_temp & (1 << i)))
496 continue;
497 for (limit = TEMP_CRIT; limit <= TEMP_WARN_HYST; limit++)
498 data->temp[i][limit] =
499 w83795_read(client, W83795_REG_TEMP[i][limit]);
500 }
501
502 /* Read the DTS limits */
Jean Delvareeb027552010-10-28 20:31:49 +0200503 if (data->enable_dts) {
Jean Delvare0d7237bf2010-10-28 20:31:48 +0200504 for (limit = DTS_CRIT; limit <= DTS_WARN_HYST; limit++)
505 data->dts_ext[limit] =
506 w83795_read(client, W83795_REG_DTS_EXT(limit));
507 }
508
509 /* Read beep settings */
510 for (i = 0; i < ARRAY_SIZE(data->beeps); i++)
511 data->beeps[i] = w83795_read(client, W83795_REG_BEEP(i));
Jean Delvare2ae61de2010-10-28 20:31:48 +0200512
513 data->valid_limits = 1;
Jean Delvare0d7237bf2010-10-28 20:31:48 +0200514}
515
Jean Delvare1bb34502010-10-28 20:31:48 +0200516static struct w83795_data *w83795_update_pwm_config(struct device *dev)
Jean Delvare0d7237bf2010-10-28 20:31:48 +0200517{
Jean Delvare1bb34502010-10-28 20:31:48 +0200518 struct i2c_client *client = to_i2c_client(dev);
Jean Delvare0d7237bf2010-10-28 20:31:48 +0200519 struct w83795_data *data = i2c_get_clientdata(client);
520 int i, tmp;
521
Jean Delvare1bb34502010-10-28 20:31:48 +0200522 mutex_lock(&data->update_lock);
523
524 if (data->valid_pwm_config)
525 goto END;
526
Jean Delvare0d7237bf2010-10-28 20:31:48 +0200527 /* Read temperature source selection */
528 for (i = 0; i < ARRAY_SIZE(data->temp_src); i++)
529 data->temp_src[i] = w83795_read(client, W83795_REG_TSS(i));
530
531 /* Read automatic fan speed control settings */
532 data->pwm_fcms[0] = w83795_read(client, W83795_REG_FCMS1);
533 data->pwm_fcms[1] = w83795_read(client, W83795_REG_FCMS2);
534 for (i = 0; i < ARRAY_SIZE(data->pwm_tfmr); i++)
535 data->pwm_tfmr[i] = w83795_read(client, W83795_REG_TFMR(i));
536 data->pwm_fomc = w83795_read(client, W83795_REG_FOMC);
537 for (i = 0; i < data->has_pwm; i++) {
538 for (tmp = PWM_FREQ; tmp <= PWM_STOP_TIME; tmp++)
539 data->pwm[i][tmp] =
540 w83795_read(client, W83795_REG_PWM(i, tmp));
541 }
542 for (i = 0; i < ARRAY_SIZE(data->target_speed); i++) {
543 data->target_speed[i] =
544 w83795_read(client, W83795_REG_FTSH(i)) << 4;
545 data->target_speed[i] |=
546 w83795_read(client, W83795_REG_FTSL(i)) >> 4;
547 }
548 data->tol_speed = w83795_read(client, W83795_REG_TFTS) & 0x3f;
549
550 for (i = 0; i < ARRAY_SIZE(data->pwm_temp); i++) {
551 data->pwm_temp[i][TEMP_PWM_TTTI] =
552 w83795_read(client, W83795_REG_TTTI(i)) & 0x7f;
553 data->pwm_temp[i][TEMP_PWM_CTFS] =
554 w83795_read(client, W83795_REG_CTFS(i));
555 tmp = w83795_read(client, W83795_REG_HT(i));
Jean Delvareeb027552010-10-28 20:31:49 +0200556 data->pwm_temp[i][TEMP_PWM_HCT] = tmp >> 4;
Jean Delvare0d7237bf2010-10-28 20:31:48 +0200557 data->pwm_temp[i][TEMP_PWM_HOT] = tmp & 0x0f;
558 }
559
560 /* Read SmartFanIV trip points */
561 for (i = 0; i < ARRAY_SIZE(data->sf4_reg); i++) {
562 for (tmp = 0; tmp < 7; tmp++) {
563 data->sf4_reg[i][SF4_TEMP][tmp] =
564 w83795_read(client,
565 W83795_REG_SF4_TEMP(i, tmp));
566 data->sf4_reg[i][SF4_PWM][tmp] =
567 w83795_read(client, W83795_REG_SF4_PWM(i, tmp));
568 }
569 }
570
571 /* Read setup PWM */
572 for (i = 0; i < ARRAY_SIZE(data->setup_pwm); i++)
573 data->setup_pwm[i] =
574 w83795_read(client, W83795_REG_SETUP_PWM(i));
Jean Delvare1bb34502010-10-28 20:31:48 +0200575
576 data->valid_pwm_config = 1;
577
578END:
579 mutex_unlock(&data->update_lock);
580 return data;
Jean Delvare0d7237bf2010-10-28 20:31:48 +0200581}
582
Wei Song792d3762010-10-28 20:31:44 +0200583static struct w83795_data *w83795_update_device(struct device *dev)
584{
585 struct i2c_client *client = to_i2c_client(dev);
586 struct w83795_data *data = i2c_get_clientdata(client);
587 u16 tmp;
588 int i;
589
590 mutex_lock(&data->update_lock);
591
Jean Delvare2ae61de2010-10-28 20:31:48 +0200592 if (!data->valid_limits)
593 w83795_update_limits(client);
594
Wei Song792d3762010-10-28 20:31:44 +0200595 if (!(time_after(jiffies, data->last_updated + HZ * 2)
596 || !data->valid))
597 goto END;
598
599 /* Update the voltages value */
600 for (i = 0; i < ARRAY_SIZE(data->in); i++) {
601 if (!(data->has_in & (1 << i)))
602 continue;
603 tmp = w83795_read(client, W83795_REG_IN[i][IN_READ]) << 2;
Jean Delvarea654b9d42010-10-28 20:31:47 +0200604 tmp |= w83795_read(client, W83795_REG_VRLSB) >> 6;
Wei Song792d3762010-10-28 20:31:44 +0200605 data->in[i][IN_READ] = tmp;
606 }
607
Jean Delvare0e256012010-10-28 20:31:46 +0200608 /* in0-2 can have dynamic limits (W83795G only) */
609 if (data->has_dyn_in) {
610 u8 lsb_max = w83795_read(client, IN_LSB_REG(0, IN_MAX));
611 u8 lsb_low = w83795_read(client, IN_LSB_REG(0, IN_LOW));
612
613 for (i = 0; i < 3; i++) {
614 if (!(data->has_dyn_in & (1 << i)))
615 continue;
616 data->in[i][IN_MAX] =
617 w83795_read(client, W83795_REG_IN[i][IN_MAX]);
618 data->in[i][IN_LOW] =
619 w83795_read(client, W83795_REG_IN[i][IN_LOW]);
620 data->in_lsb[i][IN_MAX] = (lsb_max >> (2 * i)) & 0x03;
621 data->in_lsb[i][IN_LOW] = (lsb_low >> (2 * i)) & 0x03;
622 }
623 }
624
Wei Song792d3762010-10-28 20:31:44 +0200625 /* Update fan */
626 for (i = 0; i < ARRAY_SIZE(data->fan); i++) {
627 if (!(data->has_fan & (1 << i)))
628 continue;
629 data->fan[i] = w83795_read(client, W83795_REG_FAN(i)) << 4;
Jean Delvareeb027552010-10-28 20:31:49 +0200630 data->fan[i] |= w83795_read(client, W83795_REG_VRLSB) >> 4;
Wei Song792d3762010-10-28 20:31:44 +0200631 }
632
633 /* Update temperature */
634 for (i = 0; i < ARRAY_SIZE(data->temp); i++) {
Wei Song792d3762010-10-28 20:31:44 +0200635 data->temp[i][TEMP_READ] =
636 w83795_read(client, W83795_REG_TEMP[i][TEMP_READ]);
637 data->temp_read_vrlsb[i] =
638 w83795_read(client, W83795_REG_VRLSB);
639 }
640
641 /* Update dts temperature */
Jean Delvareeb027552010-10-28 20:31:49 +0200642 if (data->enable_dts) {
Wei Song792d3762010-10-28 20:31:44 +0200643 for (i = 0; i < ARRAY_SIZE(data->dts); i++) {
644 if (!(data->has_dts & (1 << i)))
645 continue;
646 data->dts[i] =
647 w83795_read(client, W83795_REG_DTS(i));
648 data->dts_read_vrlsb[i] =
649 w83795_read(client, W83795_REG_VRLSB);
650 }
651 }
652
653 /* Update pwm output */
654 for (i = 0; i < data->has_pwm; i++) {
655 data->pwm[i][PWM_OUTPUT] =
656 w83795_read(client, W83795_REG_PWM(i, PWM_OUTPUT));
657 }
658
659 /* update alarm */
Jean Delvarecd316df2010-10-28 20:31:48 +0200660 for (i = 0; i < ARRAY_SIZE(data->alarms); i++)
Wei Song792d3762010-10-28 20:31:44 +0200661 data->alarms[i] = w83795_read(client, W83795_REG_ALARM(i));
662
663 data->last_updated = jiffies;
664 data->valid = 1;
665
666END:
667 mutex_unlock(&data->update_lock);
668 return data;
669}
670
671/*
672 * Sysfs attributes
673 */
674
675#define ALARM_STATUS 0
676#define BEEP_ENABLE 1
677static ssize_t
678show_alarm_beep(struct device *dev, struct device_attribute *attr, char *buf)
679{
680 struct w83795_data *data = w83795_update_device(dev);
681 struct sensor_device_attribute_2 *sensor_attr =
682 to_sensor_dev_attr_2(attr);
683 int nr = sensor_attr->nr;
684 int index = sensor_attr->index >> 3;
685 int bit = sensor_attr->index & 0x07;
686 u8 val;
687
Jean Delvareeb027552010-10-28 20:31:49 +0200688 if (nr == ALARM_STATUS)
689 val = (data->alarms[index] >> bit) & 1;
690 else /* BEEP_ENABLE */
691 val = (data->beeps[index] >> bit) & 1;
Wei Song792d3762010-10-28 20:31:44 +0200692
693 return sprintf(buf, "%u\n", val);
694}
695
696static ssize_t
697store_beep(struct device *dev, struct device_attribute *attr,
698 const char *buf, size_t count)
699{
700 struct i2c_client *client = to_i2c_client(dev);
701 struct w83795_data *data = i2c_get_clientdata(client);
702 struct sensor_device_attribute_2 *sensor_attr =
703 to_sensor_dev_attr_2(attr);
704 int index = sensor_attr->index >> 3;
705 int shift = sensor_attr->index & 0x07;
706 u8 beep_bit = 1 << shift;
707 unsigned long val;
708
709 if (strict_strtoul(buf, 10, &val) < 0)
710 return -EINVAL;
711 if (val != 0 && val != 1)
712 return -EINVAL;
713
714 mutex_lock(&data->update_lock);
715 data->beeps[index] = w83795_read(client, W83795_REG_BEEP(index));
716 data->beeps[index] &= ~beep_bit;
717 data->beeps[index] |= val << shift;
718 w83795_write(client, W83795_REG_BEEP(index), data->beeps[index]);
719 mutex_unlock(&data->update_lock);
720
721 return count;
722}
723
Jean Delvare24377102010-10-28 20:31:49 +0200724/* Write 0 to clear chassis alarm */
Wei Song792d3762010-10-28 20:31:44 +0200725static ssize_t
726store_chassis_clear(struct device *dev,
727 struct device_attribute *attr, const char *buf,
728 size_t count)
729{
730 struct i2c_client *client = to_i2c_client(dev);
731 struct w83795_data *data = i2c_get_clientdata(client);
Jean Delvare24377102010-10-28 20:31:49 +0200732 unsigned long val;
733
734 if (strict_strtoul(buf, 10, &val) < 0 || val != 0)
735 return -EINVAL;
Wei Song792d3762010-10-28 20:31:44 +0200736
737 mutex_lock(&data->update_lock);
738 val = w83795_read(client, W83795_REG_CLR_CHASSIS);
739 val |= 0x80;
740 w83795_write(client, W83795_REG_CLR_CHASSIS, val);
741 mutex_unlock(&data->update_lock);
742 return count;
743}
744
745#define FAN_INPUT 0
746#define FAN_MIN 1
747static ssize_t
748show_fan(struct device *dev, struct device_attribute *attr, char *buf)
749{
750 struct sensor_device_attribute_2 *sensor_attr =
751 to_sensor_dev_attr_2(attr);
752 int nr = sensor_attr->nr;
753 int index = sensor_attr->index;
754 struct w83795_data *data = w83795_update_device(dev);
755 u16 val;
756
Jean Delvareeb027552010-10-28 20:31:49 +0200757 if (nr == FAN_INPUT)
Wei Song792d3762010-10-28 20:31:44 +0200758 val = data->fan[index] & 0x0fff;
759 else
760 val = data->fan_min[index] & 0x0fff;
761
762 return sprintf(buf, "%lu\n", fan_from_reg(val));
763}
764
765static ssize_t
766store_fan_min(struct device *dev, struct device_attribute *attr,
767 const char *buf, size_t count)
768{
769 struct sensor_device_attribute_2 *sensor_attr =
770 to_sensor_dev_attr_2(attr);
771 int index = sensor_attr->index;
772 struct i2c_client *client = to_i2c_client(dev);
773 struct w83795_data *data = i2c_get_clientdata(client);
774 unsigned long val;
775
776 if (strict_strtoul(buf, 10, &val))
777 return -EINVAL;
778 val = fan_to_reg(val);
779
780 mutex_lock(&data->update_lock);
781 data->fan_min[index] = val;
782 w83795_write(client, W83795_REG_FAN_MIN_HL(index), (val >> 4) & 0xff);
783 val &= 0x0f;
Jean Delvare7eb8d502010-10-28 20:31:47 +0200784 if (index & 1) {
Wei Song792d3762010-10-28 20:31:44 +0200785 val <<= 4;
786 val |= w83795_read(client, W83795_REG_FAN_MIN_LSB(index))
787 & 0x0f;
788 } else {
789 val |= w83795_read(client, W83795_REG_FAN_MIN_LSB(index))
790 & 0xf0;
791 }
792 w83795_write(client, W83795_REG_FAN_MIN_LSB(index), val & 0xff);
793 mutex_unlock(&data->update_lock);
794
795 return count;
796}
797
798static ssize_t
799show_pwm(struct device *dev, struct device_attribute *attr, char *buf)
800{
Jean Delvare1bb34502010-10-28 20:31:48 +0200801 struct w83795_data *data;
Wei Song792d3762010-10-28 20:31:44 +0200802 struct sensor_device_attribute_2 *sensor_attr =
803 to_sensor_dev_attr_2(attr);
804 int nr = sensor_attr->nr;
805 int index = sensor_attr->index;
Jean Delvare01879a82010-10-28 20:31:47 +0200806 unsigned int val;
Wei Song792d3762010-10-28 20:31:44 +0200807
Jean Delvare1bb34502010-10-28 20:31:48 +0200808 data = nr == PWM_OUTPUT ? w83795_update_device(dev)
809 : w83795_update_pwm_config(dev);
810
Wei Song792d3762010-10-28 20:31:44 +0200811 switch (nr) {
812 case PWM_STOP_TIME:
813 val = time_from_reg(data->pwm[index][nr]);
814 break;
Jean Delvare01879a82010-10-28 20:31:47 +0200815 case PWM_FREQ:
816 val = pwm_freq_from_reg(data->pwm[index][nr], data->clkin);
Wei Song792d3762010-10-28 20:31:44 +0200817 break;
818 default:
819 val = data->pwm[index][nr];
820 break;
821 }
822
823 return sprintf(buf, "%u\n", val);
824}
825
826static ssize_t
827store_pwm(struct device *dev, struct device_attribute *attr,
828 const char *buf, size_t count)
829{
830 struct i2c_client *client = to_i2c_client(dev);
831 struct w83795_data *data = i2c_get_clientdata(client);
832 struct sensor_device_attribute_2 *sensor_attr =
833 to_sensor_dev_attr_2(attr);
834 int nr = sensor_attr->nr;
835 int index = sensor_attr->index;
836 unsigned long val;
Wei Song792d3762010-10-28 20:31:44 +0200837
838 if (strict_strtoul(buf, 10, &val) < 0)
839 return -EINVAL;
840
841 mutex_lock(&data->update_lock);
842 switch (nr) {
843 case PWM_STOP_TIME:
844 val = time_to_reg(val);
845 break;
Jean Delvare01879a82010-10-28 20:31:47 +0200846 case PWM_FREQ:
847 val = pwm_freq_to_reg(val, data->clkin);
Wei Song792d3762010-10-28 20:31:44 +0200848 break;
849 default:
850 val = SENSORS_LIMIT(val, 0, 0xff);
851 break;
852 }
853 w83795_write(client, W83795_REG_PWM(index, nr), val);
Jean Delvare01879a82010-10-28 20:31:47 +0200854 data->pwm[index][nr] = val;
Wei Song792d3762010-10-28 20:31:44 +0200855 mutex_unlock(&data->update_lock);
856 return count;
Wei Song792d3762010-10-28 20:31:44 +0200857}
858
859static ssize_t
860show_pwm_enable(struct device *dev, struct device_attribute *attr, char *buf)
861{
862 struct sensor_device_attribute_2 *sensor_attr =
863 to_sensor_dev_attr_2(attr);
Jean Delvare1bb34502010-10-28 20:31:48 +0200864 struct w83795_data *data = w83795_update_pwm_config(dev);
Wei Song792d3762010-10-28 20:31:44 +0200865 int index = sensor_attr->index;
866 u8 tmp;
867
Jean Delvareae51cd92010-11-15 21:38:56 +0100868 /* Speed cruise mode */
869 if (data->pwm_fcms[0] & (1 << index)) {
Wei Song792d3762010-10-28 20:31:44 +0200870 tmp = 2;
871 goto out;
872 }
Jean Delvareae51cd92010-11-15 21:38:56 +0100873 /* Thermal cruise or SmartFan IV mode */
Wei Song792d3762010-10-28 20:31:44 +0200874 for (tmp = 0; tmp < 6; tmp++) {
875 if (data->pwm_tfmr[tmp] & (1 << index)) {
876 tmp = 3;
877 goto out;
878 }
879 }
Jean Delvareae51cd92010-11-15 21:38:56 +0100880 /* Manual mode */
881 tmp = 1;
Wei Song792d3762010-10-28 20:31:44 +0200882
883out:
884 return sprintf(buf, "%u\n", tmp);
885}
886
887static ssize_t
888store_pwm_enable(struct device *dev, struct device_attribute *attr,
889 const char *buf, size_t count)
890{
891 struct i2c_client *client = to_i2c_client(dev);
Jean Delvare1bb34502010-10-28 20:31:48 +0200892 struct w83795_data *data = w83795_update_pwm_config(dev);
Wei Song792d3762010-10-28 20:31:44 +0200893 struct sensor_device_attribute_2 *sensor_attr =
894 to_sensor_dev_attr_2(attr);
895 int index = sensor_attr->index;
896 unsigned long val;
897 int i;
898
899 if (strict_strtoul(buf, 10, &val) < 0)
900 return -EINVAL;
Jean Delvareae51cd92010-11-15 21:38:56 +0100901 if (val < 1 || val > 2)
Wei Song792d3762010-10-28 20:31:44 +0200902 return -EINVAL;
903
904 mutex_lock(&data->update_lock);
905 switch (val) {
Wei Song792d3762010-10-28 20:31:44 +0200906 case 1:
Jean Delvareae51cd92010-11-15 21:38:56 +0100907 /* Clear speed cruise mode bits */
Wei Song792d3762010-10-28 20:31:44 +0200908 data->pwm_fcms[0] &= ~(1 << index);
909 w83795_write(client, W83795_REG_FCMS1, data->pwm_fcms[0]);
Jean Delvareae51cd92010-11-15 21:38:56 +0100910 /* Clear thermal cruise mode bits */
Wei Song792d3762010-10-28 20:31:44 +0200911 for (i = 0; i < 6; i++) {
912 data->pwm_tfmr[i] &= ~(1 << index);
913 w83795_write(client, W83795_REG_TFMR(i),
914 data->pwm_tfmr[i]);
915 }
Wei Song792d3762010-10-28 20:31:44 +0200916 break;
917 case 2:
918 data->pwm_fcms[0] |= (1 << index);
919 w83795_write(client, W83795_REG_FCMS1, data->pwm_fcms[0]);
920 break;
921 }
922 mutex_unlock(&data->update_lock);
923 return count;
Wei Song792d3762010-10-28 20:31:44 +0200924}
925
926static ssize_t
Jean Delvared5ab8452010-11-15 21:38:56 +0100927show_pwm_mode(struct device *dev, struct device_attribute *attr, char *buf)
928{
929 struct w83795_data *data = w83795_update_pwm_config(dev);
930 int index = to_sensor_dev_attr_2(attr)->index;
931 unsigned int mode;
932
933 if (data->pwm_fomc & (1 << index))
934 mode = 0; /* DC */
935 else
936 mode = 1; /* PWM */
937
938 return sprintf(buf, "%u\n", mode);
939}
940
Jean Delvareedff2f82010-11-15 21:38:56 +0100941/*
942 * Check whether a given temperature source can ever be useful.
943 * Returns the number of selectable temperature channels which are
944 * enabled.
945 */
946static int w83795_tss_useful(const struct w83795_data *data, int tsrc)
947{
948 int useful = 0, i;
949
950 for (i = 0; i < 4; i++) {
951 if (tss_map[i][tsrc] == TSS_MAP_RESERVED)
952 continue;
953 if (tss_map[i][tsrc] < 6) /* Analog */
954 useful += (data->has_temp >> tss_map[i][tsrc]) & 1;
955 else /* Digital */
956 useful += (data->has_dts >> (tss_map[i][tsrc] - 6)) & 1;
957 }
958
959 return useful;
960}
961
Jean Delvared5ab8452010-11-15 21:38:56 +0100962static ssize_t
Wei Song792d3762010-10-28 20:31:44 +0200963show_temp_src(struct device *dev, struct device_attribute *attr, char *buf)
964{
965 struct sensor_device_attribute_2 *sensor_attr =
966 to_sensor_dev_attr_2(attr);
Jean Delvare1bb34502010-10-28 20:31:48 +0200967 struct w83795_data *data = w83795_update_pwm_config(dev);
Wei Song792d3762010-10-28 20:31:44 +0200968 int index = sensor_attr->index;
Jean Delvare2a2d27d2010-11-15 21:38:56 +0100969 u8 tmp = data->temp_src[index / 2];
Wei Song792d3762010-10-28 20:31:44 +0200970
Jean Delvare7eb8d502010-10-28 20:31:47 +0200971 if (index & 1)
Jean Delvare2a2d27d2010-11-15 21:38:56 +0100972 tmp >>= 4; /* Pick high nibble */
Wei Song792d3762010-10-28 20:31:44 +0200973 else
Jean Delvare2a2d27d2010-11-15 21:38:56 +0100974 tmp &= 0x0f; /* Pick low nibble */
Wei Song792d3762010-10-28 20:31:44 +0200975
Jean Delvare2a2d27d2010-11-15 21:38:56 +0100976 /* Look-up the actual temperature channel number */
977 if (tmp >= 4 || tss_map[tmp][index] == TSS_MAP_RESERVED)
978 return -EINVAL; /* Shouldn't happen */
979
980 return sprintf(buf, "%u\n", (unsigned int)tss_map[tmp][index] + 1);
Wei Song792d3762010-10-28 20:31:44 +0200981}
982
983static ssize_t
984store_temp_src(struct device *dev, struct device_attribute *attr,
985 const char *buf, size_t count)
986{
987 struct i2c_client *client = to_i2c_client(dev);
Jean Delvare1bb34502010-10-28 20:31:48 +0200988 struct w83795_data *data = w83795_update_pwm_config(dev);
Wei Song792d3762010-10-28 20:31:44 +0200989 struct sensor_device_attribute_2 *sensor_attr =
990 to_sensor_dev_attr_2(attr);
991 int index = sensor_attr->index;
Jean Delvare2a2d27d2010-11-15 21:38:56 +0100992 int tmp;
993 unsigned long channel;
Wei Song792d3762010-10-28 20:31:44 +0200994 u8 val = index / 2;
995
Jean Delvare2a2d27d2010-11-15 21:38:56 +0100996 if (strict_strtoul(buf, 10, &channel) < 0 ||
997 channel < 1 || channel > 14)
Wei Song792d3762010-10-28 20:31:44 +0200998 return -EINVAL;
Jean Delvare2a2d27d2010-11-15 21:38:56 +0100999
1000 /* Check if request can be fulfilled */
1001 for (tmp = 0; tmp < 4; tmp++) {
1002 if (tss_map[tmp][index] == channel - 1)
1003 break;
1004 }
1005 if (tmp == 4) /* No match */
1006 return -EINVAL;
Wei Song792d3762010-10-28 20:31:44 +02001007
1008 mutex_lock(&data->update_lock);
Jean Delvare7eb8d502010-10-28 20:31:47 +02001009 if (index & 1) {
Wei Song792d3762010-10-28 20:31:44 +02001010 tmp <<= 4;
1011 data->temp_src[val] &= 0x0f;
1012 } else {
1013 data->temp_src[val] &= 0xf0;
1014 }
1015 data->temp_src[val] |= tmp;
1016 w83795_write(client, W83795_REG_TSS(val), data->temp_src[val]);
1017 mutex_unlock(&data->update_lock);
1018
1019 return count;
1020}
1021
1022#define TEMP_PWM_ENABLE 0
1023#define TEMP_PWM_FAN_MAP 1
1024static ssize_t
1025show_temp_pwm_enable(struct device *dev, struct device_attribute *attr,
1026 char *buf)
1027{
Jean Delvare1bb34502010-10-28 20:31:48 +02001028 struct w83795_data *data = w83795_update_pwm_config(dev);
Wei Song792d3762010-10-28 20:31:44 +02001029 struct sensor_device_attribute_2 *sensor_attr =
1030 to_sensor_dev_attr_2(attr);
1031 int nr = sensor_attr->nr;
1032 int index = sensor_attr->index;
1033 u8 tmp = 0xff;
1034
1035 switch (nr) {
1036 case TEMP_PWM_ENABLE:
1037 tmp = (data->pwm_fcms[1] >> index) & 1;
1038 if (tmp)
1039 tmp = 4;
1040 else
1041 tmp = 3;
1042 break;
1043 case TEMP_PWM_FAN_MAP:
1044 tmp = data->pwm_tfmr[index];
1045 break;
1046 }
1047
1048 return sprintf(buf, "%u\n", tmp);
1049}
1050
1051static ssize_t
1052store_temp_pwm_enable(struct device *dev, struct device_attribute *attr,
1053 const char *buf, size_t count)
1054{
1055 struct i2c_client *client = to_i2c_client(dev);
Jean Delvare1bb34502010-10-28 20:31:48 +02001056 struct w83795_data *data = w83795_update_pwm_config(dev);
Wei Song792d3762010-10-28 20:31:44 +02001057 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 unsigned long tmp;
1062
1063 if (strict_strtoul(buf, 10, &tmp) < 0)
1064 return -EINVAL;
1065
1066 switch (nr) {
1067 case TEMP_PWM_ENABLE:
Jean Delvareeb027552010-10-28 20:31:49 +02001068 if (tmp != 3 && tmp != 4)
Wei Song792d3762010-10-28 20:31:44 +02001069 return -EINVAL;
1070 tmp -= 3;
1071 mutex_lock(&data->update_lock);
1072 data->pwm_fcms[1] &= ~(1 << index);
1073 data->pwm_fcms[1] |= tmp << index;
1074 w83795_write(client, W83795_REG_FCMS2, data->pwm_fcms[1]);
1075 mutex_unlock(&data->update_lock);
1076 break;
1077 case TEMP_PWM_FAN_MAP:
1078 mutex_lock(&data->update_lock);
1079 tmp = SENSORS_LIMIT(tmp, 0, 0xff);
1080 w83795_write(client, W83795_REG_TFMR(index), tmp);
1081 data->pwm_tfmr[index] = tmp;
1082 mutex_unlock(&data->update_lock);
1083 break;
1084 }
1085 return count;
1086}
1087
1088#define FANIN_TARGET 0
1089#define FANIN_TOL 1
1090static ssize_t
1091show_fanin(struct device *dev, struct device_attribute *attr, char *buf)
1092{
Jean Delvare1bb34502010-10-28 20:31:48 +02001093 struct w83795_data *data = w83795_update_pwm_config(dev);
Wei Song792d3762010-10-28 20:31:44 +02001094 struct sensor_device_attribute_2 *sensor_attr =
1095 to_sensor_dev_attr_2(attr);
1096 int nr = sensor_attr->nr;
1097 int index = sensor_attr->index;
1098 u16 tmp = 0;
1099
1100 switch (nr) {
1101 case FANIN_TARGET:
1102 tmp = fan_from_reg(data->target_speed[index]);
1103 break;
1104 case FANIN_TOL:
1105 tmp = data->tol_speed;
1106 break;
1107 }
1108
1109 return sprintf(buf, "%u\n", tmp);
1110}
1111
1112static ssize_t
1113store_fanin(struct device *dev, struct device_attribute *attr,
1114 const char *buf, size_t count)
1115{
1116 struct i2c_client *client = to_i2c_client(dev);
1117 struct w83795_data *data = i2c_get_clientdata(client);
1118 struct sensor_device_attribute_2 *sensor_attr =
1119 to_sensor_dev_attr_2(attr);
1120 int nr = sensor_attr->nr;
1121 int index = sensor_attr->index;
1122 unsigned long val;
1123
1124 if (strict_strtoul(buf, 10, &val) < 0)
1125 return -EINVAL;
1126
1127 mutex_lock(&data->update_lock);
1128 switch (nr) {
1129 case FANIN_TARGET:
1130 val = fan_to_reg(SENSORS_LIMIT(val, 0, 0xfff));
Jean Delvareeb027552010-10-28 20:31:49 +02001131 w83795_write(client, W83795_REG_FTSH(index), val >> 4);
Wei Song792d3762010-10-28 20:31:44 +02001132 w83795_write(client, W83795_REG_FTSL(index), (val << 4) & 0xf0);
1133 data->target_speed[index] = val;
1134 break;
1135 case FANIN_TOL:
1136 val = SENSORS_LIMIT(val, 0, 0x3f);
1137 w83795_write(client, W83795_REG_TFTS, val);
1138 data->tol_speed = val;
1139 break;
1140 }
1141 mutex_unlock(&data->update_lock);
1142
1143 return count;
1144}
1145
1146
1147static ssize_t
1148show_temp_pwm(struct device *dev, struct device_attribute *attr, char *buf)
1149{
Jean Delvare1bb34502010-10-28 20:31:48 +02001150 struct w83795_data *data = w83795_update_pwm_config(dev);
Wei Song792d3762010-10-28 20:31:44 +02001151 struct sensor_device_attribute_2 *sensor_attr =
1152 to_sensor_dev_attr_2(attr);
1153 int nr = sensor_attr->nr;
1154 int index = sensor_attr->index;
1155 long tmp = temp_from_reg(data->pwm_temp[index][nr]);
1156
1157 return sprintf(buf, "%ld\n", tmp);
1158}
1159
1160static ssize_t
1161store_temp_pwm(struct device *dev, struct device_attribute *attr,
1162 const char *buf, size_t count)
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 nr = sensor_attr->nr;
1169 int index = sensor_attr->index;
1170 unsigned long val;
1171 u8 tmp;
1172
1173 if (strict_strtoul(buf, 10, &val) < 0)
1174 return -EINVAL;
1175 val /= 1000;
1176
1177 mutex_lock(&data->update_lock);
1178 switch (nr) {
1179 case TEMP_PWM_TTTI:
1180 val = SENSORS_LIMIT(val, 0, 0x7f);
1181 w83795_write(client, W83795_REG_TTTI(index), val);
1182 break;
1183 case TEMP_PWM_CTFS:
1184 val = SENSORS_LIMIT(val, 0, 0x7f);
1185 w83795_write(client, W83795_REG_CTFS(index), val);
1186 break;
1187 case TEMP_PWM_HCT:
1188 val = SENSORS_LIMIT(val, 0, 0x0f);
1189 tmp = w83795_read(client, W83795_REG_HT(index));
1190 tmp &= 0x0f;
1191 tmp |= (val << 4) & 0xf0;
1192 w83795_write(client, W83795_REG_HT(index), tmp);
1193 break;
1194 case TEMP_PWM_HOT:
1195 val = SENSORS_LIMIT(val, 0, 0x0f);
1196 tmp = w83795_read(client, W83795_REG_HT(index));
1197 tmp &= 0xf0;
1198 tmp |= val & 0x0f;
1199 w83795_write(client, W83795_REG_HT(index), tmp);
1200 break;
1201 }
1202 data->pwm_temp[index][nr] = val;
1203 mutex_unlock(&data->update_lock);
1204
1205 return count;
1206}
1207
1208static ssize_t
1209show_sf4_pwm(struct device *dev, struct device_attribute *attr, char *buf)
1210{
Jean Delvare1bb34502010-10-28 20:31:48 +02001211 struct w83795_data *data = w83795_update_pwm_config(dev);
Wei Song792d3762010-10-28 20:31:44 +02001212 struct sensor_device_attribute_2 *sensor_attr =
1213 to_sensor_dev_attr_2(attr);
1214 int nr = sensor_attr->nr;
1215 int index = sensor_attr->index;
1216
1217 return sprintf(buf, "%u\n", data->sf4_reg[index][SF4_PWM][nr]);
1218}
1219
1220static ssize_t
1221store_sf4_pwm(struct device *dev, struct device_attribute *attr,
1222 const char *buf, size_t count)
1223{
1224 struct i2c_client *client = to_i2c_client(dev);
1225 struct w83795_data *data = i2c_get_clientdata(client);
1226 struct sensor_device_attribute_2 *sensor_attr =
1227 to_sensor_dev_attr_2(attr);
1228 int nr = sensor_attr->nr;
1229 int index = sensor_attr->index;
1230 unsigned long val;
1231
1232 if (strict_strtoul(buf, 10, &val) < 0)
1233 return -EINVAL;
1234
1235 mutex_lock(&data->update_lock);
1236 w83795_write(client, W83795_REG_SF4_PWM(index, nr), val);
1237 data->sf4_reg[index][SF4_PWM][nr] = val;
1238 mutex_unlock(&data->update_lock);
1239
1240 return count;
1241}
1242
1243static ssize_t
1244show_sf4_temp(struct device *dev, struct device_attribute *attr, char *buf)
1245{
Jean Delvare1bb34502010-10-28 20:31:48 +02001246 struct w83795_data *data = w83795_update_pwm_config(dev);
Wei Song792d3762010-10-28 20:31:44 +02001247 struct sensor_device_attribute_2 *sensor_attr =
1248 to_sensor_dev_attr_2(attr);
1249 int nr = sensor_attr->nr;
1250 int index = sensor_attr->index;
1251
1252 return sprintf(buf, "%u\n",
1253 (data->sf4_reg[index][SF4_TEMP][nr]) * 1000);
1254}
1255
1256static ssize_t
1257store_sf4_temp(struct device *dev, struct device_attribute *attr,
1258 const char *buf, size_t count)
1259{
1260 struct i2c_client *client = to_i2c_client(dev);
1261 struct w83795_data *data = i2c_get_clientdata(client);
1262 struct sensor_device_attribute_2 *sensor_attr =
1263 to_sensor_dev_attr_2(attr);
1264 int nr = sensor_attr->nr;
1265 int index = sensor_attr->index;
1266 unsigned long val;
1267
1268 if (strict_strtoul(buf, 10, &val) < 0)
1269 return -EINVAL;
1270 val /= 1000;
1271
1272 mutex_lock(&data->update_lock);
1273 w83795_write(client, W83795_REG_SF4_TEMP(index, nr), val);
1274 data->sf4_reg[index][SF4_TEMP][nr] = val;
1275 mutex_unlock(&data->update_lock);
1276
1277 return count;
1278}
1279
1280
1281static ssize_t
1282show_temp(struct device *dev, struct device_attribute *attr, char *buf)
1283{
1284 struct sensor_device_attribute_2 *sensor_attr =
1285 to_sensor_dev_attr_2(attr);
1286 int nr = sensor_attr->nr;
1287 int index = sensor_attr->index;
1288 struct w83795_data *data = w83795_update_device(dev);
Jean Delvaredd127f52010-10-28 20:31:47 +02001289 long temp = temp_from_reg(data->temp[index][nr]);
Wei Song792d3762010-10-28 20:31:44 +02001290
Jean Delvareeb027552010-10-28 20:31:49 +02001291 if (nr == TEMP_READ)
Jean Delvarea654b9d42010-10-28 20:31:47 +02001292 temp += (data->temp_read_vrlsb[index] >> 6) * 250;
Wei Song792d3762010-10-28 20:31:44 +02001293 return sprintf(buf, "%ld\n", temp);
1294}
1295
1296static ssize_t
1297store_temp(struct device *dev, struct device_attribute *attr,
1298 const char *buf, size_t count)
1299{
1300 struct sensor_device_attribute_2 *sensor_attr =
1301 to_sensor_dev_attr_2(attr);
1302 int nr = sensor_attr->nr;
1303 int index = sensor_attr->index;
1304 struct i2c_client *client = to_i2c_client(dev);
1305 struct w83795_data *data = i2c_get_clientdata(client);
1306 long tmp;
1307
1308 if (strict_strtol(buf, 10, &tmp) < 0)
1309 return -EINVAL;
1310
1311 mutex_lock(&data->update_lock);
1312 data->temp[index][nr] = temp_to_reg(tmp, -128, 127);
1313 w83795_write(client, W83795_REG_TEMP[index][nr], data->temp[index][nr]);
1314 mutex_unlock(&data->update_lock);
1315 return count;
1316}
1317
1318
1319static ssize_t
1320show_dts_mode(struct device *dev, struct device_attribute *attr, char *buf)
1321{
Jean Delvare21fc9772010-10-28 20:31:49 +02001322 struct w83795_data *data = dev_get_drvdata(dev);
Jean Delvare39deb692010-10-28 20:31:48 +02001323 int tmp;
Wei Song792d3762010-10-28 20:31:44 +02001324
Jean Delvare39deb692010-10-28 20:31:48 +02001325 if (data->enable_dts & 2)
1326 tmp = 5;
1327 else
1328 tmp = 6;
Wei Song792d3762010-10-28 20:31:44 +02001329
1330 return sprintf(buf, "%d\n", tmp);
1331}
1332
1333static ssize_t
1334show_dts(struct device *dev, struct device_attribute *attr, char *buf)
1335{
1336 struct sensor_device_attribute_2 *sensor_attr =
1337 to_sensor_dev_attr_2(attr);
1338 int index = sensor_attr->index;
1339 struct w83795_data *data = w83795_update_device(dev);
Jean Delvaredd127f52010-10-28 20:31:47 +02001340 long temp = temp_from_reg(data->dts[index]);
Wei Song792d3762010-10-28 20:31:44 +02001341
Jean Delvarea654b9d42010-10-28 20:31:47 +02001342 temp += (data->dts_read_vrlsb[index] >> 6) * 250;
Wei Song792d3762010-10-28 20:31:44 +02001343 return sprintf(buf, "%ld\n", temp);
1344}
1345
1346static ssize_t
1347show_dts_ext(struct device *dev, struct device_attribute *attr, char *buf)
1348{
1349 struct sensor_device_attribute_2 *sensor_attr =
1350 to_sensor_dev_attr_2(attr);
1351 int nr = sensor_attr->nr;
Jean Delvare21fc9772010-10-28 20:31:49 +02001352 struct w83795_data *data = dev_get_drvdata(dev);
Jean Delvaredd127f52010-10-28 20:31:47 +02001353 long temp = temp_from_reg(data->dts_ext[nr]);
Wei Song792d3762010-10-28 20:31:44 +02001354
Wei Song792d3762010-10-28 20:31:44 +02001355 return sprintf(buf, "%ld\n", temp);
1356}
1357
1358static ssize_t
1359store_dts_ext(struct device *dev, struct device_attribute *attr,
1360 const char *buf, size_t count)
1361{
1362 struct sensor_device_attribute_2 *sensor_attr =
1363 to_sensor_dev_attr_2(attr);
1364 int nr = sensor_attr->nr;
1365 struct i2c_client *client = to_i2c_client(dev);
1366 struct w83795_data *data = i2c_get_clientdata(client);
1367 long tmp;
1368
1369 if (strict_strtol(buf, 10, &tmp) < 0)
1370 return -EINVAL;
1371
1372 mutex_lock(&data->update_lock);
1373 data->dts_ext[nr] = temp_to_reg(tmp, -128, 127);
1374 w83795_write(client, W83795_REG_DTS_EXT(nr), data->dts_ext[nr]);
1375 mutex_unlock(&data->update_lock);
1376 return count;
1377}
1378
1379
Wei Song792d3762010-10-28 20:31:44 +02001380static ssize_t
1381show_temp_mode(struct device *dev, struct device_attribute *attr, char *buf)
1382{
Jean Delvare21fc9772010-10-28 20:31:49 +02001383 struct w83795_data *data = dev_get_drvdata(dev);
Wei Song792d3762010-10-28 20:31:44 +02001384 struct sensor_device_attribute_2 *sensor_attr =
1385 to_sensor_dev_attr_2(attr);
1386 int index = sensor_attr->index;
Jean Delvare39deb692010-10-28 20:31:48 +02001387 int tmp;
Wei Song792d3762010-10-28 20:31:44 +02001388
Jean Delvare39deb692010-10-28 20:31:48 +02001389 if (data->temp_mode & (1 << index))
1390 tmp = 3; /* Thermal diode */
1391 else
1392 tmp = 4; /* Thermistor */
Wei Song792d3762010-10-28 20:31:44 +02001393
1394 return sprintf(buf, "%d\n", tmp);
1395}
1396
Jean Delvare39deb692010-10-28 20:31:48 +02001397/* Only for temp1-4 (temp5-6 can only be thermistor) */
Wei Song792d3762010-10-28 20:31:44 +02001398static ssize_t
1399store_temp_mode(struct device *dev, struct device_attribute *attr,
1400 const char *buf, size_t count)
1401{
1402 struct i2c_client *client = to_i2c_client(dev);
1403 struct w83795_data *data = i2c_get_clientdata(client);
1404 struct sensor_device_attribute_2 *sensor_attr =
1405 to_sensor_dev_attr_2(attr);
1406 int index = sensor_attr->index;
Jean Delvare39deb692010-10-28 20:31:48 +02001407 int reg_shift;
Wei Song792d3762010-10-28 20:31:44 +02001408 unsigned long val;
1409 u8 tmp;
Wei Song792d3762010-10-28 20:31:44 +02001410
1411 if (strict_strtoul(buf, 10, &val) < 0)
1412 return -EINVAL;
1413 if ((val != 4) && (val != 3))
1414 return -EINVAL;
Wei Song792d3762010-10-28 20:31:44 +02001415
1416 mutex_lock(&data->update_lock);
1417 if (val == 3) {
Jean Delvare39deb692010-10-28 20:31:48 +02001418 /* Thermal diode */
1419 val = 0x01;
Wei Song792d3762010-10-28 20:31:44 +02001420 data->temp_mode |= 1 << index;
1421 } else if (val == 4) {
Jean Delvare39deb692010-10-28 20:31:48 +02001422 /* Thermistor */
1423 val = 0x03;
1424 data->temp_mode &= ~(1 << index);
Wei Song792d3762010-10-28 20:31:44 +02001425 }
1426
Jean Delvare39deb692010-10-28 20:31:48 +02001427 reg_shift = 2 * index;
1428 tmp = w83795_read(client, W83795_REG_TEMP_CTRL2);
1429 tmp &= ~(0x03 << reg_shift);
1430 tmp |= val << reg_shift;
1431 w83795_write(client, W83795_REG_TEMP_CTRL2, tmp);
Wei Song792d3762010-10-28 20:31:44 +02001432
1433 mutex_unlock(&data->update_lock);
1434 return count;
1435}
1436
1437
1438/* show/store VIN */
1439static ssize_t
1440show_in(struct device *dev, struct device_attribute *attr, char *buf)
1441{
1442 struct sensor_device_attribute_2 *sensor_attr =
1443 to_sensor_dev_attr_2(attr);
1444 int nr = sensor_attr->nr;
1445 int index = sensor_attr->index;
1446 struct w83795_data *data = w83795_update_device(dev);
1447 u16 val = data->in[index][nr];
1448 u8 lsb_idx;
1449
1450 switch (nr) {
1451 case IN_READ:
1452 /* calculate this value again by sensors as sensors3.conf */
1453 if ((index >= 17) &&
Jean Delvare6f9dfd82010-10-28 20:31:46 +02001454 !((data->has_gain >> (index - 17)) & 1))
Wei Song792d3762010-10-28 20:31:44 +02001455 val *= 8;
1456 break;
1457 case IN_MAX:
1458 case IN_LOW:
1459 lsb_idx = IN_LSB_SHIFT_IDX[index][IN_LSB_IDX];
1460 val <<= 2;
1461 val |= (data->in_lsb[lsb_idx][nr] >>
Jean Delvare5d2cd952010-10-28 20:31:49 +02001462 IN_LSB_SHIFT_IDX[index][IN_LSB_SHIFT]) & 0x03;
Wei Song792d3762010-10-28 20:31:44 +02001463 if ((index >= 17) &&
Jean Delvare6f9dfd82010-10-28 20:31:46 +02001464 !((data->has_gain >> (index - 17)) & 1))
Wei Song792d3762010-10-28 20:31:44 +02001465 val *= 8;
1466 break;
1467 }
1468 val = in_from_reg(index, val);
1469
1470 return sprintf(buf, "%d\n", val);
1471}
1472
1473static ssize_t
1474store_in(struct device *dev, struct device_attribute *attr,
1475 const char *buf, size_t count)
1476{
1477 struct sensor_device_attribute_2 *sensor_attr =
1478 to_sensor_dev_attr_2(attr);
1479 int nr = sensor_attr->nr;
1480 int index = sensor_attr->index;
1481 struct i2c_client *client = to_i2c_client(dev);
1482 struct w83795_data *data = i2c_get_clientdata(client);
1483 unsigned long val;
1484 u8 tmp;
1485 u8 lsb_idx;
1486
1487 if (strict_strtoul(buf, 10, &val) < 0)
1488 return -EINVAL;
1489 val = in_to_reg(index, val);
1490
1491 if ((index >= 17) &&
Jean Delvare6f9dfd82010-10-28 20:31:46 +02001492 !((data->has_gain >> (index - 17)) & 1))
Wei Song792d3762010-10-28 20:31:44 +02001493 val /= 8;
1494 val = SENSORS_LIMIT(val, 0, 0x3FF);
1495 mutex_lock(&data->update_lock);
1496
1497 lsb_idx = IN_LSB_SHIFT_IDX[index][IN_LSB_IDX];
1498 tmp = w83795_read(client, IN_LSB_REG(lsb_idx, nr));
1499 tmp &= ~(0x03 << IN_LSB_SHIFT_IDX[index][IN_LSB_SHIFT]);
1500 tmp |= (val & 0x03) << IN_LSB_SHIFT_IDX[index][IN_LSB_SHIFT];
1501 w83795_write(client, IN_LSB_REG(lsb_idx, nr), tmp);
1502 data->in_lsb[lsb_idx][nr] = tmp;
1503
1504 tmp = (val >> 2) & 0xff;
1505 w83795_write(client, W83795_REG_IN[index][nr], tmp);
1506 data->in[index][nr] = tmp;
1507
1508 mutex_unlock(&data->update_lock);
1509 return count;
1510}
1511
1512
Jean Delvare00030af2010-10-28 20:31:49 +02001513#ifdef CONFIG_SENSORS_W83795_FANCTRL
Wei Song792d3762010-10-28 20:31:44 +02001514static ssize_t
1515show_sf_setup(struct device *dev, struct device_attribute *attr, char *buf)
1516{
1517 struct sensor_device_attribute_2 *sensor_attr =
1518 to_sensor_dev_attr_2(attr);
1519 int nr = sensor_attr->nr;
Jean Delvare1bb34502010-10-28 20:31:48 +02001520 struct w83795_data *data = w83795_update_pwm_config(dev);
Wei Song792d3762010-10-28 20:31:44 +02001521 u16 val = data->setup_pwm[nr];
1522
1523 switch (nr) {
1524 case SETUP_PWM_UPTIME:
1525 case SETUP_PWM_DOWNTIME:
1526 val = time_from_reg(val);
1527 break;
1528 }
1529
1530 return sprintf(buf, "%d\n", val);
1531}
1532
1533static ssize_t
1534store_sf_setup(struct device *dev, struct device_attribute *attr,
1535 const char *buf, size_t count)
1536{
1537 struct sensor_device_attribute_2 *sensor_attr =
1538 to_sensor_dev_attr_2(attr);
1539 int nr = sensor_attr->nr;
1540 struct i2c_client *client = to_i2c_client(dev);
1541 struct w83795_data *data = i2c_get_clientdata(client);
1542 unsigned long val;
1543
1544 if (strict_strtoul(buf, 10, &val) < 0)
1545 return -EINVAL;
1546
1547 switch (nr) {
1548 case SETUP_PWM_DEFAULT:
1549 val = SENSORS_LIMIT(val, 0, 0xff);
1550 break;
1551 case SETUP_PWM_UPTIME:
1552 case SETUP_PWM_DOWNTIME:
1553 val = time_to_reg(val);
1554 if (val == 0)
1555 return -EINVAL;
1556 break;
1557 }
1558
1559 mutex_lock(&data->update_lock);
1560 data->setup_pwm[nr] = val;
1561 w83795_write(client, W83795_REG_SETUP_PWM(nr), val);
1562 mutex_unlock(&data->update_lock);
1563 return count;
1564}
Jean Delvare00030af2010-10-28 20:31:49 +02001565#endif
Wei Song792d3762010-10-28 20:31:44 +02001566
1567
1568#define NOT_USED -1
1569
Jean Delvare0e256012010-10-28 20:31:46 +02001570/* Don't change the attribute order, _max and _min are accessed by index
1571 * somewhere else in the code */
Jean Delvare87df0da2010-10-28 20:31:45 +02001572#define SENSOR_ATTR_IN(index) { \
Wei Song792d3762010-10-28 20:31:44 +02001573 SENSOR_ATTR_2(in##index##_input, S_IRUGO, show_in, NULL, \
1574 IN_READ, index), \
1575 SENSOR_ATTR_2(in##index##_max, S_IRUGO | S_IWUSR, show_in, \
1576 store_in, IN_MAX, index), \
1577 SENSOR_ATTR_2(in##index##_min, S_IRUGO | S_IWUSR, show_in, \
1578 store_in, IN_LOW, index), \
1579 SENSOR_ATTR_2(in##index##_alarm, S_IRUGO, show_alarm_beep, \
1580 NULL, ALARM_STATUS, index + ((index > 14) ? 1 : 0)), \
1581 SENSOR_ATTR_2(in##index##_beep, S_IWUSR | S_IRUGO, \
1582 show_alarm_beep, store_beep, BEEP_ENABLE, \
Jean Delvare87df0da2010-10-28 20:31:45 +02001583 index + ((index > 14) ? 1 : 0)) }
Wei Song792d3762010-10-28 20:31:44 +02001584
Jean Delvare87df0da2010-10-28 20:31:45 +02001585#define SENSOR_ATTR_FAN(index) { \
Wei Song792d3762010-10-28 20:31:44 +02001586 SENSOR_ATTR_2(fan##index##_input, S_IRUGO, show_fan, \
1587 NULL, FAN_INPUT, index - 1), \
1588 SENSOR_ATTR_2(fan##index##_min, S_IWUSR | S_IRUGO, \
1589 show_fan, store_fan_min, FAN_MIN, index - 1), \
1590 SENSOR_ATTR_2(fan##index##_alarm, S_IRUGO, show_alarm_beep, \
1591 NULL, ALARM_STATUS, index + 31), \
1592 SENSOR_ATTR_2(fan##index##_beep, S_IWUSR | S_IRUGO, \
Jean Delvare87df0da2010-10-28 20:31:45 +02001593 show_alarm_beep, store_beep, BEEP_ENABLE, index + 31) }
Wei Song792d3762010-10-28 20:31:44 +02001594
Jean Delvareb5f6a902010-10-28 20:31:46 +02001595#define SENSOR_ATTR_PWM(index) { \
Wei Song792d3762010-10-28 20:31:44 +02001596 SENSOR_ATTR_2(pwm##index, S_IWUSR | S_IRUGO, show_pwm, \
1597 store_pwm, PWM_OUTPUT, index - 1), \
1598 SENSOR_ATTR_2(pwm##index##_nonstop, S_IWUSR | S_IRUGO, \
1599 show_pwm, store_pwm, PWM_NONSTOP, index - 1), \
1600 SENSOR_ATTR_2(pwm##index##_start, S_IWUSR | S_IRUGO, \
1601 show_pwm, store_pwm, PWM_START, index - 1), \
1602 SENSOR_ATTR_2(pwm##index##_stop_time, S_IWUSR | S_IRUGO, \
1603 show_pwm, store_pwm, PWM_STOP_TIME, index - 1), \
Jean Delvare01879a82010-10-28 20:31:47 +02001604 SENSOR_ATTR_2(pwm##index##_freq, S_IWUSR | S_IRUGO, \
1605 show_pwm, store_pwm, PWM_FREQ, index - 1), \
Wei Song792d3762010-10-28 20:31:44 +02001606 SENSOR_ATTR_2(pwm##index##_enable, S_IWUSR | S_IRUGO, \
Jean Delvareb2cc5282010-10-28 20:31:46 +02001607 show_pwm_enable, store_pwm_enable, NOT_USED, index - 1), \
Jean Delvared5ab8452010-11-15 21:38:56 +01001608 SENSOR_ATTR_2(pwm##index##_mode, S_IRUGO, \
1609 show_pwm_mode, NULL, NOT_USED, index - 1), \
Jean Delvareb2cc5282010-10-28 20:31:46 +02001610 SENSOR_ATTR_2(fan##index##_target, S_IWUSR | S_IRUGO, \
1611 show_fanin, store_fanin, FANIN_TARGET, index - 1) }
Wei Song792d3762010-10-28 20:31:44 +02001612
Jean Delvare87df0da2010-10-28 20:31:45 +02001613#define SENSOR_ATTR_DTS(index) { \
Wei Song792d3762010-10-28 20:31:44 +02001614 SENSOR_ATTR_2(temp##index##_type, S_IRUGO , \
1615 show_dts_mode, NULL, NOT_USED, index - 7), \
1616 SENSOR_ATTR_2(temp##index##_input, S_IRUGO, show_dts, \
1617 NULL, NOT_USED, index - 7), \
Jean Delvarea0ce4022010-10-28 20:31:47 +02001618 SENSOR_ATTR_2(temp##index##_crit, S_IRUGO | S_IWUSR, show_dts_ext, \
Wei Song792d3762010-10-28 20:31:44 +02001619 store_dts_ext, DTS_CRIT, NOT_USED), \
Jean Delvarea0ce4022010-10-28 20:31:47 +02001620 SENSOR_ATTR_2(temp##index##_crit_hyst, S_IRUGO | S_IWUSR, \
Wei Song792d3762010-10-28 20:31:44 +02001621 show_dts_ext, store_dts_ext, DTS_CRIT_HYST, NOT_USED), \
Jean Delvarea0ce4022010-10-28 20:31:47 +02001622 SENSOR_ATTR_2(temp##index##_max, S_IRUGO | S_IWUSR, show_dts_ext, \
Wei Song792d3762010-10-28 20:31:44 +02001623 store_dts_ext, DTS_WARN, NOT_USED), \
Jean Delvarea0ce4022010-10-28 20:31:47 +02001624 SENSOR_ATTR_2(temp##index##_max_hyst, S_IRUGO | S_IWUSR, \
Wei Song792d3762010-10-28 20:31:44 +02001625 show_dts_ext, store_dts_ext, DTS_WARN_HYST, NOT_USED), \
1626 SENSOR_ATTR_2(temp##index##_alarm, S_IRUGO, \
1627 show_alarm_beep, NULL, ALARM_STATUS, index + 17), \
1628 SENSOR_ATTR_2(temp##index##_beep, S_IWUSR | S_IRUGO, \
Jean Delvare87df0da2010-10-28 20:31:45 +02001629 show_alarm_beep, store_beep, BEEP_ENABLE, index + 17) }
Wei Song792d3762010-10-28 20:31:44 +02001630
Jean Delvare87df0da2010-10-28 20:31:45 +02001631#define SENSOR_ATTR_TEMP(index) { \
Jean Delvare39deb692010-10-28 20:31:48 +02001632 SENSOR_ATTR_2(temp##index##_type, S_IRUGO | (index < 4 ? S_IWUSR : 0), \
Wei Song792d3762010-10-28 20:31:44 +02001633 show_temp_mode, store_temp_mode, NOT_USED, index - 1), \
1634 SENSOR_ATTR_2(temp##index##_input, S_IRUGO, show_temp, \
1635 NULL, TEMP_READ, index - 1), \
Jean Delvarea0ce4022010-10-28 20:31:47 +02001636 SENSOR_ATTR_2(temp##index##_crit, S_IRUGO | S_IWUSR, show_temp, \
Wei Song792d3762010-10-28 20:31:44 +02001637 store_temp, TEMP_CRIT, index - 1), \
Jean Delvarea0ce4022010-10-28 20:31:47 +02001638 SENSOR_ATTR_2(temp##index##_crit_hyst, S_IRUGO | S_IWUSR, \
Wei Song792d3762010-10-28 20:31:44 +02001639 show_temp, store_temp, TEMP_CRIT_HYST, index - 1), \
Jean Delvarea0ce4022010-10-28 20:31:47 +02001640 SENSOR_ATTR_2(temp##index##_max, S_IRUGO | S_IWUSR, show_temp, \
Wei Song792d3762010-10-28 20:31:44 +02001641 store_temp, TEMP_WARN, index - 1), \
Jean Delvarea0ce4022010-10-28 20:31:47 +02001642 SENSOR_ATTR_2(temp##index##_max_hyst, S_IRUGO | S_IWUSR, \
Wei Song792d3762010-10-28 20:31:44 +02001643 show_temp, store_temp, TEMP_WARN_HYST, index - 1), \
1644 SENSOR_ATTR_2(temp##index##_alarm, S_IRUGO, \
1645 show_alarm_beep, NULL, ALARM_STATUS, \
1646 index + (index > 4 ? 11 : 17)), \
1647 SENSOR_ATTR_2(temp##index##_beep, S_IWUSR | S_IRUGO, \
1648 show_alarm_beep, store_beep, BEEP_ENABLE, \
1649 index + (index > 4 ? 11 : 17)), \
Wei Song792d3762010-10-28 20:31:44 +02001650 SENSOR_ATTR_2(temp##index##_pwm_enable, S_IWUSR | S_IRUGO, \
1651 show_temp_pwm_enable, store_temp_pwm_enable, \
1652 TEMP_PWM_ENABLE, index - 1), \
1653 SENSOR_ATTR_2(temp##index##_auto_channels_pwm, S_IWUSR | S_IRUGO, \
1654 show_temp_pwm_enable, store_temp_pwm_enable, \
1655 TEMP_PWM_FAN_MAP, index - 1), \
1656 SENSOR_ATTR_2(thermal_cruise##index, S_IWUSR | S_IRUGO, \
1657 show_temp_pwm, store_temp_pwm, TEMP_PWM_TTTI, index - 1), \
Jean Delvarea0ce4022010-10-28 20:31:47 +02001658 SENSOR_ATTR_2(temp##index##_warn, S_IWUSR | S_IRUGO, \
Wei Song792d3762010-10-28 20:31:44 +02001659 show_temp_pwm, store_temp_pwm, TEMP_PWM_CTFS, index - 1), \
Jean Delvarea0ce4022010-10-28 20:31:47 +02001660 SENSOR_ATTR_2(temp##index##_warn_hyst, S_IWUSR | S_IRUGO, \
Wei Song792d3762010-10-28 20:31:44 +02001661 show_temp_pwm, store_temp_pwm, TEMP_PWM_HCT, index - 1), \
1662 SENSOR_ATTR_2(temp##index##_operation_hyst, S_IWUSR | S_IRUGO, \
1663 show_temp_pwm, store_temp_pwm, TEMP_PWM_HOT, index - 1), \
1664 SENSOR_ATTR_2(temp##index##_auto_point1_pwm, S_IRUGO | S_IWUSR, \
1665 show_sf4_pwm, store_sf4_pwm, 0, index - 1), \
1666 SENSOR_ATTR_2(temp##index##_auto_point2_pwm, S_IRUGO | S_IWUSR, \
1667 show_sf4_pwm, store_sf4_pwm, 1, index - 1), \
1668 SENSOR_ATTR_2(temp##index##_auto_point3_pwm, S_IRUGO | S_IWUSR, \
1669 show_sf4_pwm, store_sf4_pwm, 2, index - 1), \
1670 SENSOR_ATTR_2(temp##index##_auto_point4_pwm, S_IRUGO | S_IWUSR, \
1671 show_sf4_pwm, store_sf4_pwm, 3, index - 1), \
1672 SENSOR_ATTR_2(temp##index##_auto_point5_pwm, S_IRUGO | S_IWUSR, \
1673 show_sf4_pwm, store_sf4_pwm, 4, index - 1), \
1674 SENSOR_ATTR_2(temp##index##_auto_point6_pwm, S_IRUGO | S_IWUSR, \
1675 show_sf4_pwm, store_sf4_pwm, 5, index - 1), \
1676 SENSOR_ATTR_2(temp##index##_auto_point7_pwm, S_IRUGO | S_IWUSR, \
1677 show_sf4_pwm, store_sf4_pwm, 6, index - 1), \
1678 SENSOR_ATTR_2(temp##index##_auto_point1_temp, S_IRUGO | S_IWUSR,\
1679 show_sf4_temp, store_sf4_temp, 0, index - 1), \
1680 SENSOR_ATTR_2(temp##index##_auto_point2_temp, S_IRUGO | S_IWUSR,\
1681 show_sf4_temp, store_sf4_temp, 1, index - 1), \
1682 SENSOR_ATTR_2(temp##index##_auto_point3_temp, S_IRUGO | S_IWUSR,\
1683 show_sf4_temp, store_sf4_temp, 2, index - 1), \
1684 SENSOR_ATTR_2(temp##index##_auto_point4_temp, S_IRUGO | S_IWUSR,\
1685 show_sf4_temp, store_sf4_temp, 3, index - 1), \
1686 SENSOR_ATTR_2(temp##index##_auto_point5_temp, S_IRUGO | S_IWUSR,\
1687 show_sf4_temp, store_sf4_temp, 4, index - 1), \
1688 SENSOR_ATTR_2(temp##index##_auto_point6_temp, S_IRUGO | S_IWUSR,\
1689 show_sf4_temp, store_sf4_temp, 5, index - 1), \
1690 SENSOR_ATTR_2(temp##index##_auto_point7_temp, S_IRUGO | S_IWUSR,\
Jean Delvare87df0da2010-10-28 20:31:45 +02001691 show_sf4_temp, store_sf4_temp, 6, index - 1) }
Wei Song792d3762010-10-28 20:31:44 +02001692
1693
Jean Delvare87df0da2010-10-28 20:31:45 +02001694static struct sensor_device_attribute_2 w83795_in[][5] = {
Wei Song792d3762010-10-28 20:31:44 +02001695 SENSOR_ATTR_IN(0),
1696 SENSOR_ATTR_IN(1),
1697 SENSOR_ATTR_IN(2),
1698 SENSOR_ATTR_IN(3),
1699 SENSOR_ATTR_IN(4),
1700 SENSOR_ATTR_IN(5),
1701 SENSOR_ATTR_IN(6),
1702 SENSOR_ATTR_IN(7),
1703 SENSOR_ATTR_IN(8),
1704 SENSOR_ATTR_IN(9),
1705 SENSOR_ATTR_IN(10),
1706 SENSOR_ATTR_IN(11),
1707 SENSOR_ATTR_IN(12),
1708 SENSOR_ATTR_IN(13),
1709 SENSOR_ATTR_IN(14),
1710 SENSOR_ATTR_IN(15),
1711 SENSOR_ATTR_IN(16),
1712 SENSOR_ATTR_IN(17),
1713 SENSOR_ATTR_IN(18),
1714 SENSOR_ATTR_IN(19),
1715 SENSOR_ATTR_IN(20),
1716};
1717
Jean Delvare86ef4d22010-10-28 20:31:46 +02001718static const struct sensor_device_attribute_2 w83795_fan[][4] = {
Wei Song792d3762010-10-28 20:31:44 +02001719 SENSOR_ATTR_FAN(1),
1720 SENSOR_ATTR_FAN(2),
1721 SENSOR_ATTR_FAN(3),
1722 SENSOR_ATTR_FAN(4),
1723 SENSOR_ATTR_FAN(5),
1724 SENSOR_ATTR_FAN(6),
1725 SENSOR_ATTR_FAN(7),
1726 SENSOR_ATTR_FAN(8),
1727 SENSOR_ATTR_FAN(9),
1728 SENSOR_ATTR_FAN(10),
1729 SENSOR_ATTR_FAN(11),
1730 SENSOR_ATTR_FAN(12),
1731 SENSOR_ATTR_FAN(13),
1732 SENSOR_ATTR_FAN(14),
1733};
1734
Jean Delvareedff2f82010-11-15 21:38:56 +01001735static const struct sensor_device_attribute_2 w83795_temp[][28] = {
Wei Song792d3762010-10-28 20:31:44 +02001736 SENSOR_ATTR_TEMP(1),
1737 SENSOR_ATTR_TEMP(2),
1738 SENSOR_ATTR_TEMP(3),
1739 SENSOR_ATTR_TEMP(4),
1740 SENSOR_ATTR_TEMP(5),
1741 SENSOR_ATTR_TEMP(6),
1742};
1743
Jean Delvare86ef4d22010-10-28 20:31:46 +02001744static const struct sensor_device_attribute_2 w83795_dts[][8] = {
Wei Song792d3762010-10-28 20:31:44 +02001745 SENSOR_ATTR_DTS(7),
1746 SENSOR_ATTR_DTS(8),
1747 SENSOR_ATTR_DTS(9),
1748 SENSOR_ATTR_DTS(10),
1749 SENSOR_ATTR_DTS(11),
1750 SENSOR_ATTR_DTS(12),
1751 SENSOR_ATTR_DTS(13),
1752 SENSOR_ATTR_DTS(14),
1753};
1754
Jean Delvared5ab8452010-11-15 21:38:56 +01001755static const struct sensor_device_attribute_2 w83795_pwm[][8] = {
Jean Delvareb5f6a902010-10-28 20:31:46 +02001756 SENSOR_ATTR_PWM(1),
1757 SENSOR_ATTR_PWM(2),
Wei Song792d3762010-10-28 20:31:44 +02001758 SENSOR_ATTR_PWM(3),
1759 SENSOR_ATTR_PWM(4),
1760 SENSOR_ATTR_PWM(5),
1761 SENSOR_ATTR_PWM(6),
1762 SENSOR_ATTR_PWM(7),
1763 SENSOR_ATTR_PWM(8),
1764};
1765
Jean Delvareedff2f82010-11-15 21:38:56 +01001766static const struct sensor_device_attribute_2 w83795_tss[6] = {
1767 SENSOR_ATTR_2(temp1_source_sel, S_IWUSR | S_IRUGO,
1768 show_temp_src, store_temp_src, NOT_USED, 0),
1769 SENSOR_ATTR_2(temp2_source_sel, S_IWUSR | S_IRUGO,
1770 show_temp_src, store_temp_src, NOT_USED, 1),
1771 SENSOR_ATTR_2(temp3_source_sel, S_IWUSR | S_IRUGO,
1772 show_temp_src, store_temp_src, NOT_USED, 2),
1773 SENSOR_ATTR_2(temp4_source_sel, S_IWUSR | S_IRUGO,
1774 show_temp_src, store_temp_src, NOT_USED, 3),
1775 SENSOR_ATTR_2(temp5_source_sel, S_IWUSR | S_IRUGO,
1776 show_temp_src, store_temp_src, NOT_USED, 4),
1777 SENSOR_ATTR_2(temp6_source_sel, S_IWUSR | S_IRUGO,
1778 show_temp_src, store_temp_src, NOT_USED, 5),
1779};
1780
Jean Delvare86ef4d22010-10-28 20:31:46 +02001781static const struct sensor_device_attribute_2 sda_single_files[] = {
Jean Delvare24377102010-10-28 20:31:49 +02001782 SENSOR_ATTR_2(intrusion0_alarm, S_IWUSR | S_IRUGO, show_alarm_beep,
Wei Song792d3762010-10-28 20:31:44 +02001783 store_chassis_clear, ALARM_STATUS, 46),
Jean Delvare24377102010-10-28 20:31:49 +02001784 SENSOR_ATTR_2(intrusion0_beep, S_IWUSR | S_IRUGO, show_alarm_beep,
1785 store_beep, BEEP_ENABLE, 46),
Jean Delvare02728ff2010-10-28 20:31:47 +02001786 SENSOR_ATTR_2(beep_enable, S_IWUSR | S_IRUGO, show_alarm_beep,
1787 store_beep, BEEP_ENABLE, 47),
Jean Delvare00030af2010-10-28 20:31:49 +02001788#ifdef CONFIG_SENSORS_W83795_FANCTRL
Wei Song792d3762010-10-28 20:31:44 +02001789 SENSOR_ATTR_2(speed_cruise_tolerance, S_IWUSR | S_IRUGO, show_fanin,
1790 store_fanin, FANIN_TOL, NOT_USED),
1791 SENSOR_ATTR_2(pwm_default, S_IWUSR | S_IRUGO, show_sf_setup,
1792 store_sf_setup, SETUP_PWM_DEFAULT, NOT_USED),
1793 SENSOR_ATTR_2(pwm_uptime, S_IWUSR | S_IRUGO, show_sf_setup,
1794 store_sf_setup, SETUP_PWM_UPTIME, NOT_USED),
1795 SENSOR_ATTR_2(pwm_downtime, S_IWUSR | S_IRUGO, show_sf_setup,
1796 store_sf_setup, SETUP_PWM_DOWNTIME, NOT_USED),
Jean Delvare00030af2010-10-28 20:31:49 +02001797#endif
Wei Song792d3762010-10-28 20:31:44 +02001798};
1799
1800/*
1801 * Driver interface
1802 */
1803
1804static void w83795_init_client(struct i2c_client *client)
1805{
Jean Delvare01879a82010-10-28 20:31:47 +02001806 struct w83795_data *data = i2c_get_clientdata(client);
1807 static const u16 clkin[4] = { /* in kHz */
1808 14318, 24000, 33333, 48000
1809 };
Jean Delvare80646b92010-10-28 20:31:46 +02001810 u8 config;
1811
Wei Song792d3762010-10-28 20:31:44 +02001812 if (reset)
1813 w83795_write(client, W83795_REG_CONFIG, 0x80);
1814
Jean Delvare80646b92010-10-28 20:31:46 +02001815 /* Start monitoring if needed */
1816 config = w83795_read(client, W83795_REG_CONFIG);
1817 if (!(config & W83795_REG_CONFIG_START)) {
1818 dev_info(&client->dev, "Enabling monitoring operations\n");
1819 w83795_write(client, W83795_REG_CONFIG,
1820 config | W83795_REG_CONFIG_START);
1821 }
Jean Delvare01879a82010-10-28 20:31:47 +02001822
1823 data->clkin = clkin[(config >> 3) & 0x3];
1824 dev_dbg(&client->dev, "clkin = %u kHz\n", data->clkin);
Wei Song792d3762010-10-28 20:31:44 +02001825}
1826
Jean Delvare2be381d2010-10-28 20:31:45 +02001827static int w83795_get_device_id(struct i2c_client *client)
1828{
1829 int device_id;
1830
1831 device_id = i2c_smbus_read_byte_data(client, W83795_REG_DEVICEID);
1832
1833 /* Special case for rev. A chips; can't be checked first because later
1834 revisions emulate this for compatibility */
1835 if (device_id < 0 || (device_id & 0xf0) != 0x50) {
1836 int alt_id;
1837
1838 alt_id = i2c_smbus_read_byte_data(client,
1839 W83795_REG_DEVICEID_A);
1840 if (alt_id == 0x50)
1841 device_id = alt_id;
1842 }
1843
1844 return device_id;
1845}
1846
Wei Song792d3762010-10-28 20:31:44 +02001847/* Return 0 if detection is successful, -ENODEV otherwise */
1848static int w83795_detect(struct i2c_client *client,
1849 struct i2c_board_info *info)
1850{
Jean Delvare2be381d2010-10-28 20:31:45 +02001851 int bank, vendor_id, device_id, expected, i2c_addr, config;
Wei Song792d3762010-10-28 20:31:44 +02001852 struct i2c_adapter *adapter = client->adapter;
1853 unsigned short address = client->addr;
Jean Delvare093d1a42010-10-28 20:31:45 +02001854 const char *chip_name;
Wei Song792d3762010-10-28 20:31:44 +02001855
1856 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1857 return -ENODEV;
1858 bank = i2c_smbus_read_byte_data(client, W83795_REG_BANKSEL);
Jean Delvare2be381d2010-10-28 20:31:45 +02001859 if (bank < 0 || (bank & 0x7c)) {
1860 dev_dbg(&adapter->dev,
1861 "w83795: Detection failed at addr 0x%02hx, check %s\n",
1862 address, "bank");
1863 return -ENODEV;
1864 }
Wei Song792d3762010-10-28 20:31:44 +02001865
Wei Song792d3762010-10-28 20:31:44 +02001866 /* Check Nuvoton vendor ID */
Jean Delvare2be381d2010-10-28 20:31:45 +02001867 vendor_id = i2c_smbus_read_byte_data(client, W83795_REG_VENDORID);
1868 expected = bank & 0x80 ? 0x5c : 0xa3;
1869 if (vendor_id != expected) {
1870 dev_dbg(&adapter->dev,
1871 "w83795: Detection failed at addr 0x%02hx, check %s\n",
1872 address, "vendor id");
1873 return -ENODEV;
1874 }
1875
1876 /* Check device ID */
1877 device_id = w83795_get_device_id(client) |
1878 (i2c_smbus_read_byte_data(client, W83795_REG_CHIPID) << 8);
1879 if ((device_id >> 4) != 0x795) {
1880 dev_dbg(&adapter->dev,
1881 "w83795: Detection failed at addr 0x%02hx, check %s\n",
1882 address, "device id\n");
Wei Song792d3762010-10-28 20:31:44 +02001883 return -ENODEV;
1884 }
1885
1886 /* If Nuvoton chip, address of chip and W83795_REG_I2C_ADDR
1887 should match */
Jean Delvare2be381d2010-10-28 20:31:45 +02001888 if ((bank & 0x07) == 0) {
1889 i2c_addr = i2c_smbus_read_byte_data(client,
1890 W83795_REG_I2C_ADDR);
1891 if ((i2c_addr & 0x7f) != address) {
1892 dev_dbg(&adapter->dev,
1893 "w83795: Detection failed at addr 0x%02hx, "
1894 "check %s\n", address, "i2c addr");
1895 return -ENODEV;
1896 }
Wei Song792d3762010-10-28 20:31:44 +02001897 }
1898
Jean Delvare093d1a42010-10-28 20:31:45 +02001899 /* Check 795 chip type: 795G or 795ADG
1900 Usually we don't write to chips during detection, but here we don't
1901 quite have the choice; hopefully it's OK, we are about to return
1902 success anyway */
1903 if ((bank & 0x07) != 0)
1904 i2c_smbus_write_byte_data(client, W83795_REG_BANKSEL,
1905 bank & ~0x07);
Jean Delvare2be381d2010-10-28 20:31:45 +02001906 config = i2c_smbus_read_byte_data(client, W83795_REG_CONFIG);
1907 if (config & W83795_REG_CONFIG_CONFIG48)
Jean Delvare093d1a42010-10-28 20:31:45 +02001908 chip_name = "w83795adg";
Jean Delvare2be381d2010-10-28 20:31:45 +02001909 else
Jean Delvare093d1a42010-10-28 20:31:45 +02001910 chip_name = "w83795g";
Wei Song792d3762010-10-28 20:31:44 +02001911
Jean Delvare093d1a42010-10-28 20:31:45 +02001912 strlcpy(info->type, chip_name, I2C_NAME_SIZE);
Jean Delvare2be381d2010-10-28 20:31:45 +02001913 dev_info(&adapter->dev, "Found %s rev. %c at 0x%02hx\n", chip_name,
1914 'A' + (device_id & 0xf), address);
Wei Song792d3762010-10-28 20:31:44 +02001915
1916 return 0;
1917}
1918
Jean Delvare6f3dcde2010-10-28 20:31:45 +02001919static int w83795_handle_files(struct device *dev, int (*fn)(struct device *,
1920 const struct device_attribute *))
Jean Delvare892514a2010-10-28 20:31:45 +02001921{
1922 struct w83795_data *data = dev_get_drvdata(dev);
Jean Delvare87df0da2010-10-28 20:31:45 +02001923 int err, i, j;
Jean Delvare892514a2010-10-28 20:31:45 +02001924
1925 for (i = 0; i < ARRAY_SIZE(w83795_in); i++) {
Jean Delvare87df0da2010-10-28 20:31:45 +02001926 if (!(data->has_in & (1 << i)))
Jean Delvare892514a2010-10-28 20:31:45 +02001927 continue;
Jean Delvare87df0da2010-10-28 20:31:45 +02001928 for (j = 0; j < ARRAY_SIZE(w83795_in[0]); j++) {
1929 err = fn(dev, &w83795_in[i][j].dev_attr);
1930 if (err)
1931 return err;
1932 }
Jean Delvare892514a2010-10-28 20:31:45 +02001933 }
1934
1935 for (i = 0; i < ARRAY_SIZE(w83795_fan); i++) {
Jean Delvare87df0da2010-10-28 20:31:45 +02001936 if (!(data->has_fan & (1 << i)))
Jean Delvare892514a2010-10-28 20:31:45 +02001937 continue;
Jean Delvare87df0da2010-10-28 20:31:45 +02001938 for (j = 0; j < ARRAY_SIZE(w83795_fan[0]); j++) {
1939 err = fn(dev, &w83795_fan[i][j].dev_attr);
1940 if (err)
1941 return err;
1942 }
Jean Delvare892514a2010-10-28 20:31:45 +02001943 }
1944
Jean Delvareedff2f82010-11-15 21:38:56 +01001945 for (i = 0; i < ARRAY_SIZE(w83795_tss); i++) {
1946 j = w83795_tss_useful(data, i);
1947 if (!j)
1948 continue;
1949 err = fn(dev, &w83795_tss[i].dev_attr);
1950 if (err)
1951 return err;
1952 }
1953
Jean Delvare892514a2010-10-28 20:31:45 +02001954 for (i = 0; i < ARRAY_SIZE(sda_single_files); i++) {
Jean Delvare6f3dcde2010-10-28 20:31:45 +02001955 err = fn(dev, &sda_single_files[i].dev_attr);
Jean Delvare892514a2010-10-28 20:31:45 +02001956 if (err)
1957 return err;
1958 }
1959
Jean Delvare00030af2010-10-28 20:31:49 +02001960#ifdef CONFIG_SENSORS_W83795_FANCTRL
Jean Delvareb5f6a902010-10-28 20:31:46 +02001961 for (i = 0; i < data->has_pwm; i++) {
1962 for (j = 0; j < ARRAY_SIZE(w83795_pwm[0]); j++) {
1963 err = fn(dev, &w83795_pwm[i][j].dev_attr);
Jean Delvare892514a2010-10-28 20:31:45 +02001964 if (err)
1965 return err;
1966 }
1967 }
Jean Delvare00030af2010-10-28 20:31:49 +02001968#endif
Jean Delvare892514a2010-10-28 20:31:45 +02001969
1970 for (i = 0; i < ARRAY_SIZE(w83795_temp); i++) {
Jean Delvare87df0da2010-10-28 20:31:45 +02001971 if (!(data->has_temp & (1 << i)))
Jean Delvare892514a2010-10-28 20:31:45 +02001972 continue;
Jean Delvare00030af2010-10-28 20:31:49 +02001973#ifdef CONFIG_SENSORS_W83795_FANCTRL
Jean Delvare87df0da2010-10-28 20:31:45 +02001974 for (j = 0; j < ARRAY_SIZE(w83795_temp[0]); j++) {
Jean Delvare00030af2010-10-28 20:31:49 +02001975#else
1976 for (j = 0; j < 8; j++) {
1977#endif
Jean Delvare87df0da2010-10-28 20:31:45 +02001978 err = fn(dev, &w83795_temp[i][j].dev_attr);
1979 if (err)
1980 return err;
1981 }
Jean Delvare892514a2010-10-28 20:31:45 +02001982 }
1983
Jean Delvareeb027552010-10-28 20:31:49 +02001984 if (data->enable_dts) {
Jean Delvare892514a2010-10-28 20:31:45 +02001985 for (i = 0; i < ARRAY_SIZE(w83795_dts); i++) {
Jean Delvare87df0da2010-10-28 20:31:45 +02001986 if (!(data->has_dts & (1 << i)))
Jean Delvare892514a2010-10-28 20:31:45 +02001987 continue;
Jean Delvare87df0da2010-10-28 20:31:45 +02001988 for (j = 0; j < ARRAY_SIZE(w83795_dts[0]); j++) {
1989 err = fn(dev, &w83795_dts[i][j].dev_attr);
1990 if (err)
1991 return err;
1992 }
Jean Delvare892514a2010-10-28 20:31:45 +02001993 }
1994 }
1995
Jean Delvare892514a2010-10-28 20:31:45 +02001996 return 0;
1997}
1998
Jean Delvare6f3dcde2010-10-28 20:31:45 +02001999/* We need a wrapper that fits in w83795_handle_files */
2000static int device_remove_file_wrapper(struct device *dev,
2001 const struct device_attribute *attr)
Jean Delvare2fa09872010-10-28 20:31:45 +02002002{
Jean Delvare6f3dcde2010-10-28 20:31:45 +02002003 device_remove_file(dev, attr);
2004 return 0;
Jean Delvare2fa09872010-10-28 20:31:45 +02002005}
2006
Jean Delvare0e256012010-10-28 20:31:46 +02002007static void w83795_check_dynamic_in_limits(struct i2c_client *client)
2008{
2009 struct w83795_data *data = i2c_get_clientdata(client);
2010 u8 vid_ctl;
2011 int i, err_max, err_min;
2012
2013 vid_ctl = w83795_read(client, W83795_REG_VID_CTRL);
2014
2015 /* Return immediately if VRM isn't configured */
2016 if ((vid_ctl & 0x07) == 0x00 || (vid_ctl & 0x07) == 0x07)
2017 return;
2018
2019 data->has_dyn_in = (vid_ctl >> 3) & 0x07;
2020 for (i = 0; i < 2; i++) {
2021 if (!(data->has_dyn_in & (1 << i)))
2022 continue;
2023
2024 /* Voltage limits in dynamic mode, switch to read-only */
2025 err_max = sysfs_chmod_file(&client->dev.kobj,
2026 &w83795_in[i][2].dev_attr.attr,
2027 S_IRUGO);
2028 err_min = sysfs_chmod_file(&client->dev.kobj,
2029 &w83795_in[i][3].dev_attr.attr,
2030 S_IRUGO);
2031 if (err_max || err_min)
2032 dev_warn(&client->dev, "Failed to set in%d limits "
2033 "read-only (%d, %d)\n", i, err_max, err_min);
2034 else
2035 dev_info(&client->dev, "in%d limits set dynamically "
2036 "from VID\n", i);
2037 }
2038}
2039
Jean Delvare71caf462010-10-28 20:31:46 +02002040/* Check pins that can be used for either temperature or voltage monitoring */
2041static void w83795_apply_temp_config(struct w83795_data *data, u8 config,
2042 int temp_chan, int in_chan)
2043{
2044 /* config is a 2-bit value */
2045 switch (config) {
2046 case 0x2: /* Voltage monitoring */
2047 data->has_in |= 1 << in_chan;
2048 break;
2049 case 0x1: /* Thermal diode */
2050 if (temp_chan >= 4)
2051 break;
2052 data->temp_mode |= 1 << temp_chan;
2053 /* fall through */
2054 case 0x3: /* Thermistor */
2055 data->has_temp |= 1 << temp_chan;
2056 break;
2057 }
2058}
2059
Wei Song792d3762010-10-28 20:31:44 +02002060static int w83795_probe(struct i2c_client *client,
2061 const struct i2c_device_id *id)
2062{
2063 int i;
2064 u8 tmp;
2065 struct device *dev = &client->dev;
2066 struct w83795_data *data;
Jean Delvare71caf462010-10-28 20:31:46 +02002067 int err;
Wei Song792d3762010-10-28 20:31:44 +02002068
2069 data = kzalloc(sizeof(struct w83795_data), GFP_KERNEL);
2070 if (!data) {
2071 err = -ENOMEM;
2072 goto exit;
2073 }
2074
2075 i2c_set_clientdata(client, data);
Jean Delvare093d1a42010-10-28 20:31:45 +02002076 data->chip_type = id->driver_data;
Wei Song792d3762010-10-28 20:31:44 +02002077 data->bank = i2c_smbus_read_byte_data(client, W83795_REG_BANKSEL);
2078 mutex_init(&data->update_lock);
2079
2080 /* Initialize the chip */
2081 w83795_init_client(client);
2082
Jean Delvare71caf462010-10-28 20:31:46 +02002083 /* Check which voltages and fans are present */
2084 data->has_in = w83795_read(client, W83795_REG_VOLT_CTRL1)
2085 | (w83795_read(client, W83795_REG_VOLT_CTRL2) << 8);
2086 data->has_fan = w83795_read(client, W83795_REG_FANIN_CTRL1)
2087 | (w83795_read(client, W83795_REG_FANIN_CTRL2) << 8);
Wei Song792d3762010-10-28 20:31:44 +02002088
Jean Delvare71caf462010-10-28 20:31:46 +02002089 /* Check which analog temperatures and extra voltages are present */
Wei Song792d3762010-10-28 20:31:44 +02002090 tmp = w83795_read(client, W83795_REG_TEMP_CTRL1);
2091 if (tmp & 0x20)
2092 data->enable_dts = 1;
Jean Delvare71caf462010-10-28 20:31:46 +02002093 w83795_apply_temp_config(data, (tmp >> 2) & 0x3, 5, 16);
2094 w83795_apply_temp_config(data, tmp & 0x3, 4, 15);
Wei Song792d3762010-10-28 20:31:44 +02002095 tmp = w83795_read(client, W83795_REG_TEMP_CTRL2);
Jean Delvare71caf462010-10-28 20:31:46 +02002096 w83795_apply_temp_config(data, tmp >> 6, 3, 20);
2097 w83795_apply_temp_config(data, (tmp >> 4) & 0x3, 2, 19);
2098 w83795_apply_temp_config(data, (tmp >> 2) & 0x3, 1, 18);
2099 w83795_apply_temp_config(data, tmp & 0x3, 0, 17);
Wei Song792d3762010-10-28 20:31:44 +02002100
2101 /* Check DTS enable status */
Jean Delvare71caf462010-10-28 20:31:46 +02002102 if (data->enable_dts) {
Wei Song792d3762010-10-28 20:31:44 +02002103 if (1 & w83795_read(client, W83795_REG_DTSC))
2104 data->enable_dts |= 2;
2105 data->has_dts = w83795_read(client, W83795_REG_DTSE);
2106 }
2107
Jean Delvare54891a32010-10-28 20:31:47 +02002108 /* Report PECI Tbase values */
2109 if (data->enable_dts == 1) {
2110 for (i = 0; i < 8; i++) {
2111 if (!(data->has_dts & (1 << i)))
2112 continue;
2113 tmp = w83795_read(client, W83795_REG_PECI_TBASE(i));
2114 dev_info(&client->dev,
2115 "PECI agent %d Tbase temperature: %u\n",
2116 i + 1, (unsigned int)tmp & 0x7f);
2117 }
2118 }
2119
Wei Song792d3762010-10-28 20:31:44 +02002120 data->has_gain = w83795_read(client, W83795_REG_VMIGB_CTRL) & 0x0f;
Wei Song792d3762010-10-28 20:31:44 +02002121
2122 /* pwm and smart fan */
2123 if (data->chip_type == w83795g)
2124 data->has_pwm = 8;
2125 else
2126 data->has_pwm = 2;
Wei Song792d3762010-10-28 20:31:44 +02002127
Jean Delvare6f3dcde2010-10-28 20:31:45 +02002128 err = w83795_handle_files(dev, device_create_file);
Jean Delvare892514a2010-10-28 20:31:45 +02002129 if (err)
2130 goto exit_remove;
Wei Song792d3762010-10-28 20:31:44 +02002131
Jean Delvare0e256012010-10-28 20:31:46 +02002132 if (data->chip_type == w83795g)
2133 w83795_check_dynamic_in_limits(client);
2134
Wei Song792d3762010-10-28 20:31:44 +02002135 data->hwmon_dev = hwmon_device_register(dev);
2136 if (IS_ERR(data->hwmon_dev)) {
2137 err = PTR_ERR(data->hwmon_dev);
2138 goto exit_remove;
2139 }
2140
2141 return 0;
2142
Wei Song792d3762010-10-28 20:31:44 +02002143exit_remove:
Jean Delvare6f3dcde2010-10-28 20:31:45 +02002144 w83795_handle_files(dev, device_remove_file_wrapper);
Wei Song792d3762010-10-28 20:31:44 +02002145 kfree(data);
2146exit:
2147 return err;
2148}
2149
2150static int w83795_remove(struct i2c_client *client)
2151{
2152 struct w83795_data *data = i2c_get_clientdata(client);
Wei Song792d3762010-10-28 20:31:44 +02002153
2154 hwmon_device_unregister(data->hwmon_dev);
Jean Delvare6f3dcde2010-10-28 20:31:45 +02002155 w83795_handle_files(&client->dev, device_remove_file_wrapper);
Wei Song792d3762010-10-28 20:31:44 +02002156 kfree(data);
2157
2158 return 0;
2159}
2160
2161
2162static const struct i2c_device_id w83795_id[] = {
Jean Delvare093d1a42010-10-28 20:31:45 +02002163 { "w83795g", w83795g },
2164 { "w83795adg", w83795adg },
Wei Song792d3762010-10-28 20:31:44 +02002165 { }
2166};
2167MODULE_DEVICE_TABLE(i2c, w83795_id);
2168
2169static struct i2c_driver w83795_driver = {
2170 .driver = {
2171 .name = "w83795",
2172 },
2173 .probe = w83795_probe,
2174 .remove = w83795_remove,
2175 .id_table = w83795_id,
2176
2177 .class = I2C_CLASS_HWMON,
2178 .detect = w83795_detect,
2179 .address_list = normal_i2c,
2180};
2181
2182static int __init sensors_w83795_init(void)
2183{
2184 return i2c_add_driver(&w83795_driver);
2185}
2186
2187static void __exit sensors_w83795_exit(void)
2188{
2189 i2c_del_driver(&w83795_driver);
2190}
2191
Jean Delvaree3760b42010-10-28 20:31:49 +02002192MODULE_AUTHOR("Wei Song, Jean Delvare <khali@linux-fr.org>");
Jean Delvare315bacf2010-10-28 20:31:45 +02002193MODULE_DESCRIPTION("W83795G/ADG hardware monitoring driver");
Wei Song792d3762010-10-28 20:31:44 +02002194MODULE_LICENSE("GPL");
2195
2196module_init(sensors_w83795_init);
2197module_exit(sensors_w83795_exit);