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