hwmon: (dme1737) fix Super-IO device ID override
[deliverable/linux.git] / drivers / hwmon / asb100.c
CommitLineData
1da177e4
LT
1/*
2 asb100.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring
4
5 Copyright (C) 2004 Mark M. Hoffman <mhoffman@lightlink.com>
6
7 (derived from w83781d.c)
8
9 Copyright (C) 1998 - 2003 Frodo Looijaard <frodol@dds.nl>,
10 Philip Edelbrock <phil@netroedge.com>, and
11 Mark Studebaker <mdsxyz123@yahoo.com>
12
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
17
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26*/
27
28/*
29 This driver supports the hardware sensor chips: Asus ASB100 and
30 ASB100-A "BACH".
31
32 ASB100-A supports pwm1, while plain ASB100 does not. There is no known
33 way for the driver to tell which one is there.
34
35 Chip #vin #fanin #pwm #temp wchipid vendid i2c ISA
36 asb100 7 3 1 4 0x31 0x0694 yes no
37*/
38
39#include <linux/module.h>
40#include <linux/slab.h>
41#include <linux/i2c.h>
943b0830 42#include <linux/hwmon.h>
303760b4 43#include <linux/hwmon-vid.h>
943b0830 44#include <linux/err.h>
1da177e4 45#include <linux/init.h>
ff324094 46#include <linux/jiffies.h>
9a61bf63 47#include <linux/mutex.h>
1da177e4
LT
48#include "lm75.h"
49
50/*
51 HISTORY:
52 2003-12-29 1.0.0 Ported from lm_sensors project for kernel 2.6
53*/
54#define ASB100_VERSION "1.0.0"
55
56/* I2C addresses to scan */
57static unsigned short normal_i2c[] = { 0x2d, I2C_CLIENT_END };
58
1da177e4 59/* Insmod parameters */
f4b50261 60I2C_CLIENT_INSMOD_1(asb100);
1da177e4
LT
61I2C_CLIENT_MODULE_PARM(force_subclients, "List of subclient addresses: "
62 "{bus, clientaddr, subclientaddr1, subclientaddr2}");
63
64/* Voltage IN registers 0-6 */
65#define ASB100_REG_IN(nr) (0x20 + (nr))
66#define ASB100_REG_IN_MAX(nr) (0x2b + (nr * 2))
67#define ASB100_REG_IN_MIN(nr) (0x2c + (nr * 2))
68
69/* FAN IN registers 1-3 */
70#define ASB100_REG_FAN(nr) (0x28 + (nr))
71#define ASB100_REG_FAN_MIN(nr) (0x3b + (nr))
72
73/* TEMPERATURE registers 1-4 */
74static const u16 asb100_reg_temp[] = {0, 0x27, 0x150, 0x250, 0x17};
75static const u16 asb100_reg_temp_max[] = {0, 0x39, 0x155, 0x255, 0x18};
76static const u16 asb100_reg_temp_hyst[] = {0, 0x3a, 0x153, 0x253, 0x19};
77
78#define ASB100_REG_TEMP(nr) (asb100_reg_temp[nr])
79#define ASB100_REG_TEMP_MAX(nr) (asb100_reg_temp_max[nr])
80#define ASB100_REG_TEMP_HYST(nr) (asb100_reg_temp_hyst[nr])
81
82#define ASB100_REG_TEMP2_CONFIG 0x0152
83#define ASB100_REG_TEMP3_CONFIG 0x0252
84
85
86#define ASB100_REG_CONFIG 0x40
87#define ASB100_REG_ALARM1 0x41
88#define ASB100_REG_ALARM2 0x42
89#define ASB100_REG_SMIM1 0x43
90#define ASB100_REG_SMIM2 0x44
91#define ASB100_REG_VID_FANDIV 0x47
92#define ASB100_REG_I2C_ADDR 0x48
93#define ASB100_REG_CHIPID 0x49
94#define ASB100_REG_I2C_SUBADDR 0x4a
95#define ASB100_REG_PIN 0x4b
96#define ASB100_REG_IRQ 0x4c
97#define ASB100_REG_BANK 0x4e
98#define ASB100_REG_CHIPMAN 0x4f
99
100#define ASB100_REG_WCHIPID 0x58
101
102/* bit 7 -> enable, bits 0-3 -> duty cycle */
103#define ASB100_REG_PWM1 0x59
104
105/* CONVERSIONS
106 Rounding and limit checking is only done on the TO_REG variants. */
107
108/* These constants are a guess, consistent w/ w83781d */
109#define ASB100_IN_MIN ( 0)
110#define ASB100_IN_MAX (4080)
111
112/* IN: 1/1000 V (0V to 4.08V)
113 REG: 16mV/bit */
114static u8 IN_TO_REG(unsigned val)
115{
116 unsigned nval = SENSORS_LIMIT(val, ASB100_IN_MIN, ASB100_IN_MAX);
117 return (nval + 8) / 16;
118}
119
120static unsigned IN_FROM_REG(u8 reg)
121{
122 return reg * 16;
123}
124
125static u8 FAN_TO_REG(long rpm, int div)
126{
127 if (rpm == -1)
128 return 0;
129 if (rpm == 0)
130 return 255;
131 rpm = SENSORS_LIMIT(rpm, 1, 1000000);
132 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
133}
134
135static int FAN_FROM_REG(u8 val, int div)
136{
137 return val==0 ? -1 : val==255 ? 0 : 1350000/(val*div);
138}
139
140/* These constants are a guess, consistent w/ w83781d */
141#define ASB100_TEMP_MIN (-128000)
142#define ASB100_TEMP_MAX ( 127000)
143
144/* TEMP: 0.001C/bit (-128C to +127C)
145 REG: 1C/bit, two's complement */
5bfedac0 146static u8 TEMP_TO_REG(long temp)
1da177e4
LT
147{
148 int ntemp = SENSORS_LIMIT(temp, ASB100_TEMP_MIN, ASB100_TEMP_MAX);
149 ntemp += (ntemp<0 ? -500 : 500);
150 return (u8)(ntemp / 1000);
151}
152
153static int TEMP_FROM_REG(u8 reg)
154{
155 return (s8)reg * 1000;
156}
157
158/* PWM: 0 - 255 per sensors documentation
159 REG: (6.25% duty cycle per bit) */
160static u8 ASB100_PWM_TO_REG(int pwm)
161{
162 pwm = SENSORS_LIMIT(pwm, 0, 255);
163 return (u8)(pwm / 16);
164}
165
166static int ASB100_PWM_FROM_REG(u8 reg)
167{
168 return reg * 16;
169}
170
1da177e4
LT
171#define DIV_FROM_REG(val) (1 << (val))
172
173/* FAN DIV: 1, 2, 4, or 8 (defaults to 2)
174 REG: 0, 1, 2, or 3 (respectively) (defaults to 1) */
175static u8 DIV_TO_REG(long val)
176{
177 return val==8 ? 3 : val==4 ? 2 : val==1 ? 0 : 1;
178}
179
180/* For each registered client, we need to keep some data in memory. That
181 data is pointed to by client->data. The structure itself is
182 dynamically allocated, at the same time the client itself is allocated. */
183struct asb100_data {
184 struct i2c_client client;
1beeffe4 185 struct device *hwmon_dev;
9a61bf63 186 struct mutex lock;
1da177e4
LT
187 enum chips type;
188
9a61bf63 189 struct mutex update_lock;
1da177e4
LT
190 unsigned long last_updated; /* In jiffies */
191
192 /* array of 2 pointers to subclients */
193 struct i2c_client *lm75[2];
194
195 char valid; /* !=0 if following fields are valid */
196 u8 in[7]; /* Register value */
197 u8 in_max[7]; /* Register value */
198 u8 in_min[7]; /* Register value */
199 u8 fan[3]; /* Register value */
200 u8 fan_min[3]; /* Register value */
201 u16 temp[4]; /* Register value (0 and 3 are u8 only) */
202 u16 temp_max[4]; /* Register value (0 and 3 are u8 only) */
203 u16 temp_hyst[4]; /* Register value (0 and 3 are u8 only) */
204 u8 fan_div[3]; /* Register encoding, right justified */
205 u8 pwm; /* Register encoding */
206 u8 vid; /* Register encoding, combined */
207 u32 alarms; /* Register encoding, combined */
208 u8 vrm;
209};
210
211static int asb100_read_value(struct i2c_client *client, u16 reg);
212static void asb100_write_value(struct i2c_client *client, u16 reg, u16 val);
213
214static int asb100_attach_adapter(struct i2c_adapter *adapter);
215static int asb100_detect(struct i2c_adapter *adapter, int address, int kind);
216static int asb100_detach_client(struct i2c_client *client);
217static struct asb100_data *asb100_update_device(struct device *dev);
218static void asb100_init_client(struct i2c_client *client);
219
220static struct i2c_driver asb100_driver = {
cdaf7934 221 .driver = {
cdaf7934
LR
222 .name = "asb100",
223 },
1da177e4
LT
224 .attach_adapter = asb100_attach_adapter,
225 .detach_client = asb100_detach_client,
226};
227
228/* 7 Voltages */
229#define show_in_reg(reg) \
230static ssize_t show_##reg (struct device *dev, char *buf, int nr) \
231{ \
232 struct asb100_data *data = asb100_update_device(dev); \
233 return sprintf(buf, "%d\n", IN_FROM_REG(data->reg[nr])); \
234}
235
236show_in_reg(in)
237show_in_reg(in_min)
238show_in_reg(in_max)
239
240#define set_in_reg(REG, reg) \
241static ssize_t set_in_##reg(struct device *dev, const char *buf, \
242 size_t count, int nr) \
243{ \
244 struct i2c_client *client = to_i2c_client(dev); \
245 struct asb100_data *data = i2c_get_clientdata(client); \
246 unsigned long val = simple_strtoul(buf, NULL, 10); \
247 \
9a61bf63 248 mutex_lock(&data->update_lock); \
1da177e4
LT
249 data->in_##reg[nr] = IN_TO_REG(val); \
250 asb100_write_value(client, ASB100_REG_IN_##REG(nr), \
251 data->in_##reg[nr]); \
9a61bf63 252 mutex_unlock(&data->update_lock); \
1da177e4
LT
253 return count; \
254}
255
256set_in_reg(MIN, min)
257set_in_reg(MAX, max)
258
259#define sysfs_in(offset) \
260static ssize_t \
30f74292 261 show_in##offset (struct device *dev, struct device_attribute *attr, char *buf) \
1da177e4
LT
262{ \
263 return show_in(dev, buf, offset); \
264} \
265static DEVICE_ATTR(in##offset##_input, S_IRUGO, \
266 show_in##offset, NULL); \
267static ssize_t \
30f74292 268 show_in##offset##_min (struct device *dev, struct device_attribute *attr, char *buf) \
1da177e4
LT
269{ \
270 return show_in_min(dev, buf, offset); \
271} \
272static ssize_t \
30f74292 273 show_in##offset##_max (struct device *dev, struct device_attribute *attr, char *buf) \
1da177e4
LT
274{ \
275 return show_in_max(dev, buf, offset); \
276} \
30f74292 277static ssize_t set_in##offset##_min (struct device *dev, struct device_attribute *attr, \
1da177e4
LT
278 const char *buf, size_t count) \
279{ \
280 return set_in_min(dev, buf, count, offset); \
281} \
30f74292 282static ssize_t set_in##offset##_max (struct device *dev, struct device_attribute *attr, \
1da177e4
LT
283 const char *buf, size_t count) \
284{ \
285 return set_in_max(dev, buf, count, offset); \
286} \
287static DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
288 show_in##offset##_min, set_in##offset##_min); \
289static DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
290 show_in##offset##_max, set_in##offset##_max);
291
292sysfs_in(0);
293sysfs_in(1);
294sysfs_in(2);
295sysfs_in(3);
296sysfs_in(4);
297sysfs_in(5);
298sysfs_in(6);
299
1da177e4
LT
300/* 3 Fans */
301static ssize_t show_fan(struct device *dev, char *buf, int nr)
302{
303 struct asb100_data *data = asb100_update_device(dev);
304 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
305 DIV_FROM_REG(data->fan_div[nr])));
306}
307
308static ssize_t show_fan_min(struct device *dev, char *buf, int nr)
309{
310 struct asb100_data *data = asb100_update_device(dev);
311 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
312 DIV_FROM_REG(data->fan_div[nr])));
313}
314
315static ssize_t show_fan_div(struct device *dev, char *buf, int nr)
316{
317 struct asb100_data *data = asb100_update_device(dev);
318 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
319}
320
321static ssize_t set_fan_min(struct device *dev, const char *buf,
322 size_t count, int nr)
323{
324 struct i2c_client *client = to_i2c_client(dev);
325 struct asb100_data *data = i2c_get_clientdata(client);
326 u32 val = simple_strtoul(buf, NULL, 10);
327
9a61bf63 328 mutex_lock(&data->update_lock);
1da177e4
LT
329 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
330 asb100_write_value(client, ASB100_REG_FAN_MIN(nr), data->fan_min[nr]);
9a61bf63 331 mutex_unlock(&data->update_lock);
1da177e4
LT
332 return count;
333}
334
335/* Note: we save and restore the fan minimum here, because its value is
336 determined in part by the fan divisor. This follows the principle of
d6e05edc 337 least surprise; the user doesn't expect the fan minimum to change just
1da177e4
LT
338 because the divisor changed. */
339static ssize_t set_fan_div(struct device *dev, const char *buf,
340 size_t count, int nr)
341{
342 struct i2c_client *client = to_i2c_client(dev);
343 struct asb100_data *data = i2c_get_clientdata(client);
344 unsigned long min;
345 unsigned long val = simple_strtoul(buf, NULL, 10);
346 int reg;
347
9a61bf63 348 mutex_lock(&data->update_lock);
1da177e4
LT
349
350 min = FAN_FROM_REG(data->fan_min[nr],
351 DIV_FROM_REG(data->fan_div[nr]));
352 data->fan_div[nr] = DIV_TO_REG(val);
353
354 switch(nr) {
355 case 0: /* fan 1 */
356 reg = asb100_read_value(client, ASB100_REG_VID_FANDIV);
357 reg = (reg & 0xcf) | (data->fan_div[0] << 4);
358 asb100_write_value(client, ASB100_REG_VID_FANDIV, reg);
359 break;
360
361 case 1: /* fan 2 */
362 reg = asb100_read_value(client, ASB100_REG_VID_FANDIV);
363 reg = (reg & 0x3f) | (data->fan_div[1] << 6);
364 asb100_write_value(client, ASB100_REG_VID_FANDIV, reg);
365 break;
366
367 case 2: /* fan 3 */
368 reg = asb100_read_value(client, ASB100_REG_PIN);
369 reg = (reg & 0x3f) | (data->fan_div[2] << 6);
370 asb100_write_value(client, ASB100_REG_PIN, reg);
371 break;
372 }
373
374 data->fan_min[nr] =
375 FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
376 asb100_write_value(client, ASB100_REG_FAN_MIN(nr), data->fan_min[nr]);
377
9a61bf63 378 mutex_unlock(&data->update_lock);
1da177e4
LT
379
380 return count;
381}
382
383#define sysfs_fan(offset) \
30f74292 384static ssize_t show_fan##offset(struct device *dev, struct device_attribute *attr, char *buf) \
1da177e4
LT
385{ \
386 return show_fan(dev, buf, offset - 1); \
387} \
30f74292 388static ssize_t show_fan##offset##_min(struct device *dev, struct device_attribute *attr, char *buf) \
1da177e4
LT
389{ \
390 return show_fan_min(dev, buf, offset - 1); \
391} \
30f74292 392static ssize_t show_fan##offset##_div(struct device *dev, struct device_attribute *attr, char *buf) \
1da177e4
LT
393{ \
394 return show_fan_div(dev, buf, offset - 1); \
395} \
30f74292 396static ssize_t set_fan##offset##_min(struct device *dev, struct device_attribute *attr, const char *buf, \
1da177e4
LT
397 size_t count) \
398{ \
399 return set_fan_min(dev, buf, count, offset - 1); \
400} \
30f74292 401static ssize_t set_fan##offset##_div(struct device *dev, struct device_attribute *attr, const char *buf, \
1da177e4
LT
402 size_t count) \
403{ \
404 return set_fan_div(dev, buf, count, offset - 1); \
405} \
406static DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
407 show_fan##offset, NULL); \
408static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
409 show_fan##offset##_min, set_fan##offset##_min); \
410static DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
411 show_fan##offset##_div, set_fan##offset##_div);
412
413sysfs_fan(1);
414sysfs_fan(2);
415sysfs_fan(3);
416
1da177e4
LT
417/* 4 Temp. Sensors */
418static int sprintf_temp_from_reg(u16 reg, char *buf, int nr)
419{
420 int ret = 0;
421
422 switch (nr) {
423 case 1: case 2:
424 ret = sprintf(buf, "%d\n", LM75_TEMP_FROM_REG(reg));
425 break;
426 case 0: case 3: default:
427 ret = sprintf(buf, "%d\n", TEMP_FROM_REG(reg));
428 break;
429 }
430 return ret;
431}
432
433#define show_temp_reg(reg) \
434static ssize_t show_##reg(struct device *dev, char *buf, int nr) \
435{ \
436 struct asb100_data *data = asb100_update_device(dev); \
437 return sprintf_temp_from_reg(data->reg[nr], buf, nr); \
438}
439
440show_temp_reg(temp);
441show_temp_reg(temp_max);
442show_temp_reg(temp_hyst);
443
444#define set_temp_reg(REG, reg) \
445static ssize_t set_##reg(struct device *dev, const char *buf, \
446 size_t count, int nr) \
447{ \
448 struct i2c_client *client = to_i2c_client(dev); \
449 struct asb100_data *data = i2c_get_clientdata(client); \
5bfedac0 450 long val = simple_strtol(buf, NULL, 10); \
1da177e4 451 \
9a61bf63 452 mutex_lock(&data->update_lock); \
1da177e4
LT
453 switch (nr) { \
454 case 1: case 2: \
455 data->reg[nr] = LM75_TEMP_TO_REG(val); \
456 break; \
457 case 0: case 3: default: \
458 data->reg[nr] = TEMP_TO_REG(val); \
459 break; \
460 } \
461 asb100_write_value(client, ASB100_REG_TEMP_##REG(nr+1), \
462 data->reg[nr]); \
9a61bf63 463 mutex_unlock(&data->update_lock); \
1da177e4
LT
464 return count; \
465}
466
467set_temp_reg(MAX, temp_max);
468set_temp_reg(HYST, temp_hyst);
469
470#define sysfs_temp(num) \
30f74292 471static ssize_t show_temp##num(struct device *dev, struct device_attribute *attr, char *buf) \
1da177e4
LT
472{ \
473 return show_temp(dev, buf, num-1); \
474} \
475static DEVICE_ATTR(temp##num##_input, S_IRUGO, show_temp##num, NULL); \
30f74292 476static ssize_t show_temp_max##num(struct device *dev, struct device_attribute *attr, char *buf) \
1da177e4
LT
477{ \
478 return show_temp_max(dev, buf, num-1); \
479} \
30f74292 480static ssize_t set_temp_max##num(struct device *dev, struct device_attribute *attr, const char *buf, \
1da177e4
LT
481 size_t count) \
482{ \
483 return set_temp_max(dev, buf, count, num-1); \
484} \
485static DEVICE_ATTR(temp##num##_max, S_IRUGO | S_IWUSR, \
486 show_temp_max##num, set_temp_max##num); \
30f74292 487static ssize_t show_temp_hyst##num(struct device *dev, struct device_attribute *attr, char *buf) \
1da177e4
LT
488{ \
489 return show_temp_hyst(dev, buf, num-1); \
490} \
30f74292 491static ssize_t set_temp_hyst##num(struct device *dev, struct device_attribute *attr, const char *buf, \
1da177e4
LT
492 size_t count) \
493{ \
494 return set_temp_hyst(dev, buf, count, num-1); \
495} \
496static DEVICE_ATTR(temp##num##_max_hyst, S_IRUGO | S_IWUSR, \
497 show_temp_hyst##num, set_temp_hyst##num);
498
499sysfs_temp(1);
500sysfs_temp(2);
501sysfs_temp(3);
502sysfs_temp(4);
503
504/* VID */
30f74292 505static ssize_t show_vid(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
506{
507 struct asb100_data *data = asb100_update_device(dev);
508 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
509}
510
511static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
1da177e4
LT
512
513/* VRM */
30f74292 514static ssize_t show_vrm(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4 515{
90d6619a 516 struct asb100_data *data = dev_get_drvdata(dev);
1da177e4
LT
517 return sprintf(buf, "%d\n", data->vrm);
518}
519
30f74292 520static ssize_t set_vrm(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1da177e4
LT
521{
522 struct i2c_client *client = to_i2c_client(dev);
523 struct asb100_data *data = i2c_get_clientdata(client);
524 unsigned long val = simple_strtoul(buf, NULL, 10);
525 data->vrm = val;
526 return count;
527}
528
529/* Alarms */
530static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm);
1da177e4 531
30f74292 532static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
533{
534 struct asb100_data *data = asb100_update_device(dev);
68188ba7 535 return sprintf(buf, "%u\n", data->alarms);
1da177e4
LT
536}
537
538static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
1da177e4
LT
539
540/* 1 PWM */
30f74292 541static ssize_t show_pwm1(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
542{
543 struct asb100_data *data = asb100_update_device(dev);
544 return sprintf(buf, "%d\n", ASB100_PWM_FROM_REG(data->pwm & 0x0f));
545}
546
30f74292 547static ssize_t set_pwm1(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1da177e4
LT
548{
549 struct i2c_client *client = to_i2c_client(dev);
550 struct asb100_data *data = i2c_get_clientdata(client);
551 unsigned long val = simple_strtoul(buf, NULL, 10);
552
9a61bf63 553 mutex_lock(&data->update_lock);
1da177e4
LT
554 data->pwm &= 0x80; /* keep the enable bit */
555 data->pwm |= (0x0f & ASB100_PWM_TO_REG(val));
556 asb100_write_value(client, ASB100_REG_PWM1, data->pwm);
9a61bf63 557 mutex_unlock(&data->update_lock);
1da177e4
LT
558 return count;
559}
560
30f74292 561static ssize_t show_pwm_enable1(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
562{
563 struct asb100_data *data = asb100_update_device(dev);
564 return sprintf(buf, "%d\n", (data->pwm & 0x80) ? 1 : 0);
565}
566
30f74292 567static ssize_t set_pwm_enable1(struct device *dev, struct device_attribute *attr, const char *buf,
1da177e4
LT
568 size_t count)
569{
570 struct i2c_client *client = to_i2c_client(dev);
571 struct asb100_data *data = i2c_get_clientdata(client);
572 unsigned long val = simple_strtoul(buf, NULL, 10);
573
9a61bf63 574 mutex_lock(&data->update_lock);
1da177e4
LT
575 data->pwm &= 0x0f; /* keep the duty cycle bits */
576 data->pwm |= (val ? 0x80 : 0x00);
577 asb100_write_value(client, ASB100_REG_PWM1, data->pwm);
9a61bf63 578 mutex_unlock(&data->update_lock);
1da177e4
LT
579 return count;
580}
581
582static DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm1, set_pwm1);
583static DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
584 show_pwm_enable1, set_pwm_enable1);
c1685f61
MH
585
586static struct attribute *asb100_attributes[] = {
587 &dev_attr_in0_input.attr,
588 &dev_attr_in0_min.attr,
589 &dev_attr_in0_max.attr,
590 &dev_attr_in1_input.attr,
591 &dev_attr_in1_min.attr,
592 &dev_attr_in1_max.attr,
593 &dev_attr_in2_input.attr,
594 &dev_attr_in2_min.attr,
595 &dev_attr_in2_max.attr,
596 &dev_attr_in3_input.attr,
597 &dev_attr_in3_min.attr,
598 &dev_attr_in3_max.attr,
599 &dev_attr_in4_input.attr,
600 &dev_attr_in4_min.attr,
601 &dev_attr_in4_max.attr,
602 &dev_attr_in5_input.attr,
603 &dev_attr_in5_min.attr,
604 &dev_attr_in5_max.attr,
605 &dev_attr_in6_input.attr,
606 &dev_attr_in6_min.attr,
607 &dev_attr_in6_max.attr,
608
609 &dev_attr_fan1_input.attr,
610 &dev_attr_fan1_min.attr,
611 &dev_attr_fan1_div.attr,
612 &dev_attr_fan2_input.attr,
613 &dev_attr_fan2_min.attr,
614 &dev_attr_fan2_div.attr,
615 &dev_attr_fan3_input.attr,
616 &dev_attr_fan3_min.attr,
617 &dev_attr_fan3_div.attr,
618
619 &dev_attr_temp1_input.attr,
620 &dev_attr_temp1_max.attr,
621 &dev_attr_temp1_max_hyst.attr,
622 &dev_attr_temp2_input.attr,
623 &dev_attr_temp2_max.attr,
624 &dev_attr_temp2_max_hyst.attr,
625 &dev_attr_temp3_input.attr,
626 &dev_attr_temp3_max.attr,
627 &dev_attr_temp3_max_hyst.attr,
628 &dev_attr_temp4_input.attr,
629 &dev_attr_temp4_max.attr,
630 &dev_attr_temp4_max_hyst.attr,
631
632 &dev_attr_cpu0_vid.attr,
633 &dev_attr_vrm.attr,
634 &dev_attr_alarms.attr,
635 &dev_attr_pwm1.attr,
636 &dev_attr_pwm1_enable.attr,
637
638 NULL
639};
640
641static const struct attribute_group asb100_group = {
642 .attrs = asb100_attributes,
643};
1da177e4
LT
644
645/* This function is called when:
646 asb100_driver is inserted (when this module is loaded), for each
647 available adapter
648 when a new adapter is inserted (and asb100_driver is still present)
649 */
650static int asb100_attach_adapter(struct i2c_adapter *adapter)
651{
652 if (!(adapter->class & I2C_CLASS_HWMON))
653 return 0;
2ed2dc3c 654 return i2c_probe(adapter, &addr_data, asb100_detect);
1da177e4
LT
655}
656
657static int asb100_detect_subclients(struct i2c_adapter *adapter, int address,
658 int kind, struct i2c_client *new_client)
659{
660 int i, id, err;
661 struct asb100_data *data = i2c_get_clientdata(new_client);
662
ba9c2e8d 663 data->lm75[0] = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
1da177e4
LT
664 if (!(data->lm75[0])) {
665 err = -ENOMEM;
666 goto ERROR_SC_0;
667 }
1da177e4 668
ba9c2e8d 669 data->lm75[1] = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
1da177e4
LT
670 if (!(data->lm75[1])) {
671 err = -ENOMEM;
672 goto ERROR_SC_1;
673 }
1da177e4
LT
674
675 id = i2c_adapter_id(adapter);
676
677 if (force_subclients[0] == id && force_subclients[1] == address) {
678 for (i = 2; i <= 3; i++) {
679 if (force_subclients[i] < 0x48 ||
680 force_subclients[i] > 0x4f) {
681 dev_err(&new_client->dev, "invalid subclient "
682 "address %d; must be 0x48-0x4f\n",
683 force_subclients[i]);
684 err = -ENODEV;
685 goto ERROR_SC_2;
686 }
687 }
688 asb100_write_value(new_client, ASB100_REG_I2C_SUBADDR,
689 (force_subclients[2] & 0x07) |
690 ((force_subclients[3] & 0x07) <<4));
691 data->lm75[0]->addr = force_subclients[2];
692 data->lm75[1]->addr = force_subclients[3];
693 } else {
694 int val = asb100_read_value(new_client, ASB100_REG_I2C_SUBADDR);
695 data->lm75[0]->addr = 0x48 + (val & 0x07);
696 data->lm75[1]->addr = 0x48 + ((val >> 4) & 0x07);
697 }
698
699 if(data->lm75[0]->addr == data->lm75[1]->addr) {
700 dev_err(&new_client->dev, "duplicate addresses 0x%x "
701 "for subclients\n", data->lm75[0]->addr);
702 err = -ENODEV;
703 goto ERROR_SC_2;
704 }
705
706 for (i = 0; i <= 1; i++) {
707 i2c_set_clientdata(data->lm75[i], NULL);
708 data->lm75[i]->adapter = adapter;
709 data->lm75[i]->driver = &asb100_driver;
710 data->lm75[i]->flags = 0;
711 strlcpy(data->lm75[i]->name, "asb100 subclient", I2C_NAME_SIZE);
712 }
713
714 if ((err = i2c_attach_client(data->lm75[0]))) {
715 dev_err(&new_client->dev, "subclient %d registration "
716 "at address 0x%x failed.\n", i, data->lm75[0]->addr);
717 goto ERROR_SC_2;
718 }
719
720 if ((err = i2c_attach_client(data->lm75[1]))) {
721 dev_err(&new_client->dev, "subclient %d registration "
722 "at address 0x%x failed.\n", i, data->lm75[1]->addr);
723 goto ERROR_SC_3;
724 }
725
726 return 0;
727
728/* Undo inits in case of errors */
729ERROR_SC_3:
730 i2c_detach_client(data->lm75[0]);
731ERROR_SC_2:
732 kfree(data->lm75[1]);
733ERROR_SC_1:
734 kfree(data->lm75[0]);
735ERROR_SC_0:
736 return err;
737}
738
739static int asb100_detect(struct i2c_adapter *adapter, int address, int kind)
740{
741 int err;
742 struct i2c_client *new_client;
743 struct asb100_data *data;
744
1da177e4
LT
745 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
746 pr_debug("asb100.o: detect failed, "
747 "smbus byte data not supported!\n");
748 err = -ENODEV;
749 goto ERROR0;
750 }
751
752 /* OK. For now, we presume we have a valid client. We now create the
753 client structure, even though we cannot fill it completely yet.
754 But it allows us to access asb100_{read,write}_value. */
755
ba9c2e8d
DS
756 if (!(data = kzalloc(sizeof(struct asb100_data), GFP_KERNEL))) {
757 pr_debug("asb100.o: detect failed, kzalloc failed!\n");
1da177e4
LT
758 err = -ENOMEM;
759 goto ERROR0;
760 }
1da177e4
LT
761
762 new_client = &data->client;
9a61bf63 763 mutex_init(&data->lock);
1da177e4
LT
764 i2c_set_clientdata(new_client, data);
765 new_client->addr = address;
766 new_client->adapter = adapter;
767 new_client->driver = &asb100_driver;
768 new_client->flags = 0;
769
770 /* Now, we do the remaining detection. */
771
772 /* The chip may be stuck in some other bank than bank 0. This may
773 make reading other information impossible. Specify a force=... or
774 force_*=... parameter, and the chip will be reset to the right
775 bank. */
776 if (kind < 0) {
777
778 int val1 = asb100_read_value(new_client, ASB100_REG_BANK);
779 int val2 = asb100_read_value(new_client, ASB100_REG_CHIPMAN);
780
781 /* If we're in bank 0 */
782 if ( (!(val1 & 0x07)) &&
783 /* Check for ASB100 ID (low byte) */
784 ( ((!(val1 & 0x80)) && (val2 != 0x94)) ||
785 /* Check for ASB100 ID (high byte ) */
786 ((val1 & 0x80) && (val2 != 0x06)) ) ) {
787 pr_debug("asb100.o: detect failed, "
788 "bad chip id 0x%02x!\n", val2);
789 err = -ENODEV;
790 goto ERROR1;
791 }
792
793 } /* kind < 0 */
794
795 /* We have either had a force parameter, or we have already detected
796 Winbond. Put it now into bank 0 and Vendor ID High Byte */
797 asb100_write_value(new_client, ASB100_REG_BANK,
798 (asb100_read_value(new_client, ASB100_REG_BANK) & 0x78) | 0x80);
799
800 /* Determine the chip type. */
801 if (kind <= 0) {
802 int val1 = asb100_read_value(new_client, ASB100_REG_WCHIPID);
803 int val2 = asb100_read_value(new_client, ASB100_REG_CHIPMAN);
804
805 if ((val1 == 0x31) && (val2 == 0x06))
806 kind = asb100;
807 else {
808 if (kind == 0)
809 dev_warn(&new_client->dev, "ignoring "
810 "'force' parameter for unknown chip "
811 "at adapter %d, address 0x%02x.\n",
812 i2c_adapter_id(adapter), address);
813 err = -ENODEV;
814 goto ERROR1;
815 }
816 }
817
818 /* Fill in remaining client fields and put it into the global list */
819 strlcpy(new_client->name, "asb100", I2C_NAME_SIZE);
820 data->type = kind;
821
822 data->valid = 0;
9a61bf63 823 mutex_init(&data->update_lock);
1da177e4
LT
824
825 /* Tell the I2C layer a new client has arrived */
826 if ((err = i2c_attach_client(new_client)))
827 goto ERROR1;
828
829 /* Attach secondary lm75 clients */
830 if ((err = asb100_detect_subclients(adapter, address, kind,
831 new_client)))
832 goto ERROR2;
833
834 /* Initialize the chip */
835 asb100_init_client(new_client);
836
837 /* A few vars need to be filled upon startup */
838 data->fan_min[0] = asb100_read_value(new_client, ASB100_REG_FAN_MIN(0));
839 data->fan_min[1] = asb100_read_value(new_client, ASB100_REG_FAN_MIN(1));
840 data->fan_min[2] = asb100_read_value(new_client, ASB100_REG_FAN_MIN(2));
841
842 /* Register sysfs hooks */
c1685f61
MH
843 if ((err = sysfs_create_group(&new_client->dev.kobj, &asb100_group)))
844 goto ERROR3;
845
1beeffe4
TJ
846 data->hwmon_dev = hwmon_device_register(&new_client->dev);
847 if (IS_ERR(data->hwmon_dev)) {
848 err = PTR_ERR(data->hwmon_dev);
c1685f61 849 goto ERROR4;
943b0830
MH
850 }
851
1da177e4
LT
852 return 0;
853
c1685f61
MH
854ERROR4:
855 sysfs_remove_group(&new_client->dev.kobj, &asb100_group);
943b0830
MH
856ERROR3:
857 i2c_detach_client(data->lm75[1]);
858 i2c_detach_client(data->lm75[0]);
859 kfree(data->lm75[1]);
860 kfree(data->lm75[0]);
1da177e4
LT
861ERROR2:
862 i2c_detach_client(new_client);
863ERROR1:
864 kfree(data);
865ERROR0:
866 return err;
867}
868
869static int asb100_detach_client(struct i2c_client *client)
870{
943b0830 871 struct asb100_data *data = i2c_get_clientdata(client);
1da177e4
LT
872 int err;
873
943b0830 874 /* main client */
c1685f61 875 if (data) {
1beeffe4 876 hwmon_device_unregister(data->hwmon_dev);
c1685f61
MH
877 sysfs_remove_group(&client->dev.kobj, &asb100_group);
878 }
943b0830 879
7bef5594 880 if ((err = i2c_detach_client(client)))
1da177e4 881 return err;
1da177e4 882
943b0830
MH
883 /* main client */
884 if (data)
885 kfree(data);
886
887 /* subclient */
888 else
1da177e4 889 kfree(client);
1da177e4
LT
890
891 return 0;
892}
893
894/* The SMBus locks itself, usually, but nothing may access the chip between
895 bank switches. */
896static int asb100_read_value(struct i2c_client *client, u16 reg)
897{
898 struct asb100_data *data = i2c_get_clientdata(client);
899 struct i2c_client *cl;
900 int res, bank;
901
9a61bf63 902 mutex_lock(&data->lock);
1da177e4
LT
903
904 bank = (reg >> 8) & 0x0f;
905 if (bank > 2)
906 /* switch banks */
907 i2c_smbus_write_byte_data(client, ASB100_REG_BANK, bank);
908
909 if (bank == 0 || bank > 2) {
910 res = i2c_smbus_read_byte_data(client, reg & 0xff);
911 } else {
912 /* switch to subclient */
913 cl = data->lm75[bank - 1];
914
915 /* convert from ISA to LM75 I2C addresses */
916 switch (reg & 0xff) {
917 case 0x50: /* TEMP */
918 res = swab16(i2c_smbus_read_word_data (cl, 0));
919 break;
920 case 0x52: /* CONFIG */
921 res = i2c_smbus_read_byte_data(cl, 1);
922 break;
923 case 0x53: /* HYST */
924 res = swab16(i2c_smbus_read_word_data (cl, 2));
925 break;
926 case 0x55: /* MAX */
927 default:
928 res = swab16(i2c_smbus_read_word_data (cl, 3));
929 break;
930 }
931 }
932
933 if (bank > 2)
934 i2c_smbus_write_byte_data(client, ASB100_REG_BANK, 0);
935
9a61bf63 936 mutex_unlock(&data->lock);
1da177e4
LT
937
938 return res;
939}
940
941static void asb100_write_value(struct i2c_client *client, u16 reg, u16 value)
942{
943 struct asb100_data *data = i2c_get_clientdata(client);
944 struct i2c_client *cl;
945 int bank;
946
9a61bf63 947 mutex_lock(&data->lock);
1da177e4
LT
948
949 bank = (reg >> 8) & 0x0f;
950 if (bank > 2)
951 /* switch banks */
952 i2c_smbus_write_byte_data(client, ASB100_REG_BANK, bank);
953
954 if (bank == 0 || bank > 2) {
955 i2c_smbus_write_byte_data(client, reg & 0xff, value & 0xff);
956 } else {
957 /* switch to subclient */
958 cl = data->lm75[bank - 1];
959
960 /* convert from ISA to LM75 I2C addresses */
961 switch (reg & 0xff) {
962 case 0x52: /* CONFIG */
963 i2c_smbus_write_byte_data(cl, 1, value & 0xff);
964 break;
965 case 0x53: /* HYST */
966 i2c_smbus_write_word_data(cl, 2, swab16(value));
967 break;
968 case 0x55: /* MAX */
969 i2c_smbus_write_word_data(cl, 3, swab16(value));
970 break;
971 }
972 }
973
974 if (bank > 2)
975 i2c_smbus_write_byte_data(client, ASB100_REG_BANK, 0);
976
9a61bf63 977 mutex_unlock(&data->lock);
1da177e4
LT
978}
979
980static void asb100_init_client(struct i2c_client *client)
981{
982 struct asb100_data *data = i2c_get_clientdata(client);
983 int vid = 0;
984
985 vid = asb100_read_value(client, ASB100_REG_VID_FANDIV) & 0x0f;
986 vid |= (asb100_read_value(client, ASB100_REG_CHIPID) & 0x01) << 4;
303760b4 987 data->vrm = vid_which_vrm();
1da177e4
LT
988 vid = vid_from_reg(vid, data->vrm);
989
990 /* Start monitoring */
991 asb100_write_value(client, ASB100_REG_CONFIG,
992 (asb100_read_value(client, ASB100_REG_CONFIG) & 0xf7) | 0x01);
993}
994
995static struct asb100_data *asb100_update_device(struct device *dev)
996{
997 struct i2c_client *client = to_i2c_client(dev);
998 struct asb100_data *data = i2c_get_clientdata(client);
999 int i;
1000
9a61bf63 1001 mutex_lock(&data->update_lock);
1da177e4
LT
1002
1003 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
1004 || !data->valid) {
1005
1006 dev_dbg(&client->dev, "starting device update...\n");
1007
1008 /* 7 voltage inputs */
1009 for (i = 0; i < 7; i++) {
1010 data->in[i] = asb100_read_value(client,
1011 ASB100_REG_IN(i));
1012 data->in_min[i] = asb100_read_value(client,
1013 ASB100_REG_IN_MIN(i));
1014 data->in_max[i] = asb100_read_value(client,
1015 ASB100_REG_IN_MAX(i));
1016 }
1017
1018 /* 3 fan inputs */
1019 for (i = 0; i < 3; i++) {
1020 data->fan[i] = asb100_read_value(client,
1021 ASB100_REG_FAN(i));
1022 data->fan_min[i] = asb100_read_value(client,
1023 ASB100_REG_FAN_MIN(i));
1024 }
1025
1026 /* 4 temperature inputs */
1027 for (i = 1; i <= 4; i++) {
1028 data->temp[i-1] = asb100_read_value(client,
1029 ASB100_REG_TEMP(i));
1030 data->temp_max[i-1] = asb100_read_value(client,
1031 ASB100_REG_TEMP_MAX(i));
1032 data->temp_hyst[i-1] = asb100_read_value(client,
1033 ASB100_REG_TEMP_HYST(i));
1034 }
1035
1036 /* VID and fan divisors */
1037 i = asb100_read_value(client, ASB100_REG_VID_FANDIV);
1038 data->vid = i & 0x0f;
1039 data->vid |= (asb100_read_value(client,
1040 ASB100_REG_CHIPID) & 0x01) << 4;
1041 data->fan_div[0] = (i >> 4) & 0x03;
1042 data->fan_div[1] = (i >> 6) & 0x03;
1043 data->fan_div[2] = (asb100_read_value(client,
1044 ASB100_REG_PIN) >> 6) & 0x03;
1045
1046 /* PWM */
1047 data->pwm = asb100_read_value(client, ASB100_REG_PWM1);
1048
1049 /* alarms */
1050 data->alarms = asb100_read_value(client, ASB100_REG_ALARM1) +
1051 (asb100_read_value(client, ASB100_REG_ALARM2) << 8);
1052
1053 data->last_updated = jiffies;
1054 data->valid = 1;
1055
1056 dev_dbg(&client->dev, "... device update complete\n");
1057 }
1058
9a61bf63 1059 mutex_unlock(&data->update_lock);
1da177e4
LT
1060
1061 return data;
1062}
1063
1064static int __init asb100_init(void)
1065{
1066 return i2c_add_driver(&asb100_driver);
1067}
1068
1069static void __exit asb100_exit(void)
1070{
1071 i2c_del_driver(&asb100_driver);
1072}
1073
1074MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
1075MODULE_DESCRIPTION("ASB100 Bach driver");
1076MODULE_LICENSE("GPL");
1077
1078module_init(asb100_init);
1079module_exit(asb100_exit);
1080
This page took 0.377734 seconds and 5 git commands to generate.