blob: 407c86c20ecb04a1240a75acdd94fb2c6b0941f7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040042#include <linux/hwmon.h>
Jean Delvarefad33c52008-01-03 23:21:07 +010043#include <linux/hwmon-sysfs.h>
Jean Delvare303760b2005-07-31 21:52:01 +020044#include <linux/hwmon-vid.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040045#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/init.h>
Dominik Hacklff324092005-05-16 18:12:18 +020047#include <linux/jiffies.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010048#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include "lm75.h"
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051/* I2C addresses to scan */
52static unsigned short normal_i2c[] = { 0x2d, I2C_CLIENT_END };
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054/* Insmod parameters */
Jean Delvaref4b50262005-07-31 21:49:03 +020055I2C_CLIENT_INSMOD_1(asb100);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056I2C_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 */
Christian Hohnstaedt5bfedac2007-08-16 11:40:10 +0200141static u8 TEMP_TO_REG(long temp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166#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;
Tony Jones1beeffe2007-08-20 13:46:20 -0700180 struct device *hwmon_dev;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100181 struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 enum chips type;
183
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100184 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 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 = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100216 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100217 .name = "asb100",
218 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 .attach_adapter = asb100_attach_adapter,
220 .detach_client = asb100_detach_client,
221};
222
223/* 7 Voltages */
224#define show_in_reg(reg) \
Jean Delvarefad33c52008-01-03 23:21:07 +0100225static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
226 char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{ \
Jean Delvarefad33c52008-01-03 23:21:07 +0100228 int nr = to_sensor_dev_attr(attr)->index; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 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) \
Jean Delvarefad33c52008-01-03 23:21:07 +0100238static ssize_t set_in_##reg(struct device *dev, struct device_attribute *attr, \
239 const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240{ \
Jean Delvarefad33c52008-01-03 23:21:07 +0100241 int nr = to_sensor_dev_attr(attr)->index; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 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 \
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100246 mutex_lock(&data->update_lock); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 data->in_##reg[nr] = IN_TO_REG(val); \
248 asb100_write_value(client, ASB100_REG_IN_##REG(nr), \
249 data->in_##reg[nr]); \
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100250 mutex_unlock(&data->update_lock); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 return count; \
252}
253
254set_in_reg(MIN, min)
255set_in_reg(MAX, max)
256
257#define sysfs_in(offset) \
Jean Delvarefad33c52008-01-03 23:21:07 +0100258static 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)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
265sysfs_in(0);
266sysfs_in(1);
267sysfs_in(2);
268sysfs_in(3);
269sysfs_in(4);
270sysfs_in(5);
271sysfs_in(6);
272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273/* 3 Fans */
Jean Delvarefad33c52008-01-03 23:21:07 +0100274static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
275 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
Jean Delvarefad33c52008-01-03 23:21:07 +0100277 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 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
Jean Delvarefad33c52008-01-03 23:21:07 +0100283static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
284 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
Jean Delvarefad33c52008-01-03 23:21:07 +0100286 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 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
Jean Delvarefad33c52008-01-03 23:21:07 +0100292static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
293 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
Jean Delvarefad33c52008-01-03 23:21:07 +0100295 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 struct asb100_data *data = asb100_update_device(dev);
297 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
298}
299
Jean Delvarefad33c52008-01-03 23:21:07 +0100300static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
301 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
Jean Delvarefad33c52008-01-03 23:21:07 +0100303 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 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
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100308 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 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]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100311 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 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
Andreas Mohrd6e05ed2006-06-26 18:35:02 +0200317 least surprise; the user doesn't expect the fan minimum to change just
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 because the divisor changed. */
Jean Delvarefad33c52008-01-03 23:21:07 +0100319static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
320 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
Jean Delvarefad33c52008-01-03 23:21:07 +0100322 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 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;
Jean Delvareaf2219312008-01-03 23:15:49 +0100328
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100329 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
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
Jean Delvareaf2219312008-01-03 23:15:49 +0100335 switch (nr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 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
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100359 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
361 return count;
362}
363
364#define sysfs_fan(offset) \
Jean Delvarefad33c52008-01-03 23:21:07 +0100365static 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)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372sysfs_fan(1);
373sysfs_fan(2);
374sysfs_fan(3);
375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376/* 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}
Jean Delvareaf2219312008-01-03 23:15:49 +0100391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392#define show_temp_reg(reg) \
Jean Delvarefad33c52008-01-03 23:21:07 +0100393static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
394 char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395{ \
Jean Delvarefad33c52008-01-03 23:21:07 +0100396 int nr = to_sensor_dev_attr(attr)->index; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 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) \
Jean Delvarefad33c52008-01-03 23:21:07 +0100406static ssize_t set_##reg(struct device *dev, struct device_attribute *attr, \
407 const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{ \
Jean Delvarefad33c52008-01-03 23:21:07 +0100409 int nr = to_sensor_dev_attr(attr)->index; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 struct i2c_client *client = to_i2c_client(dev); \
411 struct asb100_data *data = i2c_get_clientdata(client); \
Christian Hohnstaedt5bfedac2007-08-16 11:40:10 +0200412 long val = simple_strtol(buf, NULL, 10); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 \
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100414 mutex_lock(&data->update_lock); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 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]); \
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100425 mutex_unlock(&data->update_lock); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 return count; \
427}
428
429set_temp_reg(MAX, temp_max);
430set_temp_reg(HYST, temp_hyst);
431
432#define sysfs_temp(num) \
Jean Delvarefad33c52008-01-03 23:21:07 +0100433static 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)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
440sysfs_temp(1);
441sysfs_temp(2);
442sysfs_temp(3);
443sysfs_temp(4);
444
445/* VID */
Jean Delvareaf2219312008-01-03 23:15:49 +0100446static ssize_t show_vid(struct device *dev, struct device_attribute *attr,
447 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448{
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
455/* VRM */
Jean Delvareaf2219312008-01-03 23:15:49 +0100456static ssize_t show_vrm(struct device *dev, struct device_attribute *attr,
457 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
Jean Delvare90d66192007-10-08 18:24:35 +0200459 struct asb100_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 return sprintf(buf, "%d\n", data->vrm);
461}
462
Jean Delvareaf2219312008-01-03 23:15:49 +0100463static ssize_t set_vrm(struct device *dev, struct device_attribute *attr,
464 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
Jean Delvare8f74efe2007-12-01 11:25:33 +0100466 struct asb100_data *data = dev_get_drvdata(dev);
467 data->vrm = simple_strtoul(buf, NULL, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 return count;
469}
470
471/* Alarms */
472static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Jean Delvareaf2219312008-01-03 23:15:49 +0100474static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
475 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
477 struct asb100_data *data = asb100_update_device(dev);
Jean Delvare68188ba2005-05-16 18:52:38 +0200478 return sprintf(buf, "%u\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479}
480
481static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
483/* 1 PWM */
Jean Delvareaf2219312008-01-03 23:15:49 +0100484static ssize_t show_pwm1(struct device *dev, struct device_attribute *attr,
485 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
487 struct asb100_data *data = asb100_update_device(dev);
488 return sprintf(buf, "%d\n", ASB100_PWM_FROM_REG(data->pwm & 0x0f));
489}
490
Jean Delvareaf2219312008-01-03 23:15:49 +0100491static ssize_t set_pwm1(struct device *dev, struct device_attribute *attr,
492 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
494 struct i2c_client *client = to_i2c_client(dev);
495 struct asb100_data *data = i2c_get_clientdata(client);
496 unsigned long val = simple_strtoul(buf, NULL, 10);
497
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100498 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 data->pwm &= 0x80; /* keep the enable bit */
500 data->pwm |= (0x0f & ASB100_PWM_TO_REG(val));
501 asb100_write_value(client, ASB100_REG_PWM1, data->pwm);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100502 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 return count;
504}
505
Jean Delvareaf2219312008-01-03 23:15:49 +0100506static ssize_t show_pwm_enable1(struct device *dev,
507 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
509 struct asb100_data *data = asb100_update_device(dev);
510 return sprintf(buf, "%d\n", (data->pwm & 0x80) ? 1 : 0);
511}
512
Jean Delvareaf2219312008-01-03 23:15:49 +0100513static ssize_t set_pwm_enable1(struct device *dev,
514 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
516 struct i2c_client *client = to_i2c_client(dev);
517 struct asb100_data *data = i2c_get_clientdata(client);
518 unsigned long val = simple_strtoul(buf, NULL, 10);
519
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100520 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 data->pwm &= 0x0f; /* keep the duty cycle bits */
522 data->pwm |= (val ? 0x80 : 0x00);
523 asb100_write_value(client, ASB100_REG_PWM1, data->pwm);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100524 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 return count;
526}
527
528static DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm1, set_pwm1);
529static DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
530 show_pwm_enable1, set_pwm_enable1);
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200531
532static struct attribute *asb100_attributes[] = {
Jean Delvarefad33c52008-01-03 23:21:07 +0100533 &sensor_dev_attr_in0_input.dev_attr.attr,
534 &sensor_dev_attr_in0_min.dev_attr.attr,
535 &sensor_dev_attr_in0_max.dev_attr.attr,
536 &sensor_dev_attr_in1_input.dev_attr.attr,
537 &sensor_dev_attr_in1_min.dev_attr.attr,
538 &sensor_dev_attr_in1_max.dev_attr.attr,
539 &sensor_dev_attr_in2_input.dev_attr.attr,
540 &sensor_dev_attr_in2_min.dev_attr.attr,
541 &sensor_dev_attr_in2_max.dev_attr.attr,
542 &sensor_dev_attr_in3_input.dev_attr.attr,
543 &sensor_dev_attr_in3_min.dev_attr.attr,
544 &sensor_dev_attr_in3_max.dev_attr.attr,
545 &sensor_dev_attr_in4_input.dev_attr.attr,
546 &sensor_dev_attr_in4_min.dev_attr.attr,
547 &sensor_dev_attr_in4_max.dev_attr.attr,
548 &sensor_dev_attr_in5_input.dev_attr.attr,
549 &sensor_dev_attr_in5_min.dev_attr.attr,
550 &sensor_dev_attr_in5_max.dev_attr.attr,
551 &sensor_dev_attr_in6_input.dev_attr.attr,
552 &sensor_dev_attr_in6_min.dev_attr.attr,
553 &sensor_dev_attr_in6_max.dev_attr.attr,
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200554
Jean Delvarefad33c52008-01-03 23:21:07 +0100555 &sensor_dev_attr_fan1_input.dev_attr.attr,
556 &sensor_dev_attr_fan1_min.dev_attr.attr,
557 &sensor_dev_attr_fan1_div.dev_attr.attr,
558 &sensor_dev_attr_fan2_input.dev_attr.attr,
559 &sensor_dev_attr_fan2_min.dev_attr.attr,
560 &sensor_dev_attr_fan2_div.dev_attr.attr,
561 &sensor_dev_attr_fan3_input.dev_attr.attr,
562 &sensor_dev_attr_fan3_min.dev_attr.attr,
563 &sensor_dev_attr_fan3_div.dev_attr.attr,
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200564
Jean Delvarefad33c52008-01-03 23:21:07 +0100565 &sensor_dev_attr_temp1_input.dev_attr.attr,
566 &sensor_dev_attr_temp1_max.dev_attr.attr,
567 &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
568 &sensor_dev_attr_temp2_input.dev_attr.attr,
569 &sensor_dev_attr_temp2_max.dev_attr.attr,
570 &sensor_dev_attr_temp2_max_hyst.dev_attr.attr,
571 &sensor_dev_attr_temp3_input.dev_attr.attr,
572 &sensor_dev_attr_temp3_max.dev_attr.attr,
573 &sensor_dev_attr_temp3_max_hyst.dev_attr.attr,
574 &sensor_dev_attr_temp4_input.dev_attr.attr,
575 &sensor_dev_attr_temp4_max.dev_attr.attr,
576 &sensor_dev_attr_temp4_max_hyst.dev_attr.attr,
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200577
578 &dev_attr_cpu0_vid.attr,
579 &dev_attr_vrm.attr,
580 &dev_attr_alarms.attr,
581 &dev_attr_pwm1.attr,
582 &dev_attr_pwm1_enable.attr,
583
584 NULL
585};
586
587static const struct attribute_group asb100_group = {
588 .attrs = asb100_attributes,
589};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
591/* This function is called when:
592 asb100_driver is inserted (when this module is loaded), for each
593 available adapter
594 when a new adapter is inserted (and asb100_driver is still present)
595 */
596static int asb100_attach_adapter(struct i2c_adapter *adapter)
597{
598 if (!(adapter->class & I2C_CLASS_HWMON))
599 return 0;
Jean Delvare2ed2dc32005-07-31 21:42:02 +0200600 return i2c_probe(adapter, &addr_data, asb100_detect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601}
602
603static int asb100_detect_subclients(struct i2c_adapter *adapter, int address,
Jean Delvareaf2219312008-01-03 23:15:49 +0100604 int kind, struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
606 int i, id, err;
Jean Delvareaf2219312008-01-03 23:15:49 +0100607 struct asb100_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Deepak Saxenaba9c2e82005-10-17 23:08:32 +0200609 data->lm75[0] = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 if (!(data->lm75[0])) {
611 err = -ENOMEM;
612 goto ERROR_SC_0;
613 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Deepak Saxenaba9c2e82005-10-17 23:08:32 +0200615 data->lm75[1] = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 if (!(data->lm75[1])) {
617 err = -ENOMEM;
618 goto ERROR_SC_1;
619 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
621 id = i2c_adapter_id(adapter);
622
623 if (force_subclients[0] == id && force_subclients[1] == address) {
624 for (i = 2; i <= 3; i++) {
625 if (force_subclients[i] < 0x48 ||
626 force_subclients[i] > 0x4f) {
Jean Delvareaf2219312008-01-03 23:15:49 +0100627 dev_err(&client->dev, "invalid subclient "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 "address %d; must be 0x48-0x4f\n",
629 force_subclients[i]);
630 err = -ENODEV;
631 goto ERROR_SC_2;
632 }
633 }
Jean Delvareaf2219312008-01-03 23:15:49 +0100634 asb100_write_value(client, ASB100_REG_I2C_SUBADDR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 (force_subclients[2] & 0x07) |
Jean Delvareaf2219312008-01-03 23:15:49 +0100636 ((force_subclients[3] & 0x07) << 4));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 data->lm75[0]->addr = force_subclients[2];
638 data->lm75[1]->addr = force_subclients[3];
639 } else {
Jean Delvareaf2219312008-01-03 23:15:49 +0100640 int val = asb100_read_value(client, ASB100_REG_I2C_SUBADDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 data->lm75[0]->addr = 0x48 + (val & 0x07);
642 data->lm75[1]->addr = 0x48 + ((val >> 4) & 0x07);
643 }
644
Jean Delvareaf2219312008-01-03 23:15:49 +0100645 if (data->lm75[0]->addr == data->lm75[1]->addr) {
646 dev_err(&client->dev, "duplicate addresses 0x%x "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 "for subclients\n", data->lm75[0]->addr);
648 err = -ENODEV;
649 goto ERROR_SC_2;
650 }
651
652 for (i = 0; i <= 1; i++) {
653 i2c_set_clientdata(data->lm75[i], NULL);
654 data->lm75[i]->adapter = adapter;
655 data->lm75[i]->driver = &asb100_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 strlcpy(data->lm75[i]->name, "asb100 subclient", I2C_NAME_SIZE);
657 }
658
659 if ((err = i2c_attach_client(data->lm75[0]))) {
Jean Delvareaf2219312008-01-03 23:15:49 +0100660 dev_err(&client->dev, "subclient %d registration "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 "at address 0x%x failed.\n", i, data->lm75[0]->addr);
662 goto ERROR_SC_2;
663 }
664
665 if ((err = i2c_attach_client(data->lm75[1]))) {
Jean Delvareaf2219312008-01-03 23:15:49 +0100666 dev_err(&client->dev, "subclient %d registration "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 "at address 0x%x failed.\n", i, data->lm75[1]->addr);
668 goto ERROR_SC_3;
669 }
670
671 return 0;
672
673/* Undo inits in case of errors */
674ERROR_SC_3:
675 i2c_detach_client(data->lm75[0]);
676ERROR_SC_2:
677 kfree(data->lm75[1]);
678ERROR_SC_1:
679 kfree(data->lm75[0]);
680ERROR_SC_0:
681 return err;
682}
683
684static int asb100_detect(struct i2c_adapter *adapter, int address, int kind)
685{
686 int err;
Jean Delvareaf2219312008-01-03 23:15:49 +0100687 struct i2c_client *client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 struct asb100_data *data;
689
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
691 pr_debug("asb100.o: detect failed, "
692 "smbus byte data not supported!\n");
693 err = -ENODEV;
694 goto ERROR0;
695 }
696
697 /* OK. For now, we presume we have a valid client. We now create the
698 client structure, even though we cannot fill it completely yet.
699 But it allows us to access asb100_{read,write}_value. */
700
Deepak Saxenaba9c2e82005-10-17 23:08:32 +0200701 if (!(data = kzalloc(sizeof(struct asb100_data), GFP_KERNEL))) {
702 pr_debug("asb100.o: detect failed, kzalloc failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 err = -ENOMEM;
704 goto ERROR0;
705 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Jean Delvareaf2219312008-01-03 23:15:49 +0100707 client = &data->client;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100708 mutex_init(&data->lock);
Jean Delvareaf2219312008-01-03 23:15:49 +0100709 i2c_set_clientdata(client, data);
710 client->addr = address;
711 client->adapter = adapter;
712 client->driver = &asb100_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
714 /* Now, we do the remaining detection. */
715
716 /* The chip may be stuck in some other bank than bank 0. This may
717 make reading other information impossible. Specify a force=... or
718 force_*=... parameter, and the chip will be reset to the right
719 bank. */
720 if (kind < 0) {
721
Jean Delvareaf2219312008-01-03 23:15:49 +0100722 int val1 = asb100_read_value(client, ASB100_REG_BANK);
723 int val2 = asb100_read_value(client, ASB100_REG_CHIPMAN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
725 /* If we're in bank 0 */
Jean Delvareaf2219312008-01-03 23:15:49 +0100726 if ((!(val1 & 0x07)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 /* Check for ASB100 ID (low byte) */
Jean Delvareaf2219312008-01-03 23:15:49 +0100728 (((!(val1 & 0x80)) && (val2 != 0x94)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 /* Check for ASB100 ID (high byte ) */
Jean Delvareaf2219312008-01-03 23:15:49 +0100730 ((val1 & 0x80) && (val2 != 0x06)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 pr_debug("asb100.o: detect failed, "
732 "bad chip id 0x%02x!\n", val2);
733 err = -ENODEV;
734 goto ERROR1;
735 }
736
737 } /* kind < 0 */
738
739 /* We have either had a force parameter, or we have already detected
740 Winbond. Put it now into bank 0 and Vendor ID High Byte */
Jean Delvareaf2219312008-01-03 23:15:49 +0100741 asb100_write_value(client, ASB100_REG_BANK,
742 (asb100_read_value(client, ASB100_REG_BANK) & 0x78) | 0x80);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
744 /* Determine the chip type. */
745 if (kind <= 0) {
Jean Delvareaf2219312008-01-03 23:15:49 +0100746 int val1 = asb100_read_value(client, ASB100_REG_WCHIPID);
747 int val2 = asb100_read_value(client, ASB100_REG_CHIPMAN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
749 if ((val1 == 0x31) && (val2 == 0x06))
750 kind = asb100;
751 else {
752 if (kind == 0)
Jean Delvareaf2219312008-01-03 23:15:49 +0100753 dev_warn(&client->dev, "ignoring "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 "'force' parameter for unknown chip "
755 "at adapter %d, address 0x%02x.\n",
756 i2c_adapter_id(adapter), address);
757 err = -ENODEV;
758 goto ERROR1;
759 }
760 }
761
762 /* Fill in remaining client fields and put it into the global list */
Jean Delvareaf2219312008-01-03 23:15:49 +0100763 strlcpy(client->name, "asb100", I2C_NAME_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 data->type = kind;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100765 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
767 /* Tell the I2C layer a new client has arrived */
Jean Delvareaf2219312008-01-03 23:15:49 +0100768 if ((err = i2c_attach_client(client)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 goto ERROR1;
770
771 /* Attach secondary lm75 clients */
772 if ((err = asb100_detect_subclients(adapter, address, kind,
Jean Delvareaf2219312008-01-03 23:15:49 +0100773 client)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 goto ERROR2;
775
776 /* Initialize the chip */
Jean Delvareaf2219312008-01-03 23:15:49 +0100777 asb100_init_client(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
779 /* A few vars need to be filled upon startup */
Jean Delvareaf2219312008-01-03 23:15:49 +0100780 data->fan_min[0] = asb100_read_value(client, ASB100_REG_FAN_MIN(0));
781 data->fan_min[1] = asb100_read_value(client, ASB100_REG_FAN_MIN(1));
782 data->fan_min[2] = asb100_read_value(client, ASB100_REG_FAN_MIN(2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
784 /* Register sysfs hooks */
Jean Delvareaf2219312008-01-03 23:15:49 +0100785 if ((err = sysfs_create_group(&client->dev.kobj, &asb100_group)))
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200786 goto ERROR3;
787
Jean Delvareaf2219312008-01-03 23:15:49 +0100788 data->hwmon_dev = hwmon_device_register(&client->dev);
Tony Jones1beeffe2007-08-20 13:46:20 -0700789 if (IS_ERR(data->hwmon_dev)) {
790 err = PTR_ERR(data->hwmon_dev);
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200791 goto ERROR4;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400792 }
793
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 return 0;
795
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200796ERROR4:
Jean Delvareaf2219312008-01-03 23:15:49 +0100797 sysfs_remove_group(&client->dev.kobj, &asb100_group);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400798ERROR3:
799 i2c_detach_client(data->lm75[1]);
800 i2c_detach_client(data->lm75[0]);
801 kfree(data->lm75[1]);
802 kfree(data->lm75[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803ERROR2:
Jean Delvareaf2219312008-01-03 23:15:49 +0100804 i2c_detach_client(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805ERROR1:
806 kfree(data);
807ERROR0:
808 return err;
809}
810
811static int asb100_detach_client(struct i2c_client *client)
812{
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400813 struct asb100_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 int err;
815
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400816 /* main client */
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200817 if (data) {
Tony Jones1beeffe2007-08-20 13:46:20 -0700818 hwmon_device_unregister(data->hwmon_dev);
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200819 sysfs_remove_group(&client->dev.kobj, &asb100_group);
820 }
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400821
Jean Delvare7bef5592005-07-27 22:14:49 +0200822 if ((err = i2c_detach_client(client)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400825 /* main client */
826 if (data)
827 kfree(data);
828
829 /* subclient */
830 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 kfree(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
833 return 0;
834}
835
836/* The SMBus locks itself, usually, but nothing may access the chip between
837 bank switches. */
838static int asb100_read_value(struct i2c_client *client, u16 reg)
839{
840 struct asb100_data *data = i2c_get_clientdata(client);
841 struct i2c_client *cl;
842 int res, bank;
843
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100844 mutex_lock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
846 bank = (reg >> 8) & 0x0f;
847 if (bank > 2)
848 /* switch banks */
849 i2c_smbus_write_byte_data(client, ASB100_REG_BANK, bank);
850
851 if (bank == 0 || bank > 2) {
852 res = i2c_smbus_read_byte_data(client, reg & 0xff);
853 } else {
854 /* switch to subclient */
855 cl = data->lm75[bank - 1];
856
857 /* convert from ISA to LM75 I2C addresses */
858 switch (reg & 0xff) {
859 case 0x50: /* TEMP */
Jean Delvareaf2219312008-01-03 23:15:49 +0100860 res = swab16(i2c_smbus_read_word_data(cl, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 break;
862 case 0x52: /* CONFIG */
863 res = i2c_smbus_read_byte_data(cl, 1);
864 break;
865 case 0x53: /* HYST */
Jean Delvareaf2219312008-01-03 23:15:49 +0100866 res = swab16(i2c_smbus_read_word_data(cl, 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 break;
868 case 0x55: /* MAX */
869 default:
Jean Delvareaf2219312008-01-03 23:15:49 +0100870 res = swab16(i2c_smbus_read_word_data(cl, 3));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 break;
872 }
873 }
874
875 if (bank > 2)
876 i2c_smbus_write_byte_data(client, ASB100_REG_BANK, 0);
877
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100878 mutex_unlock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
880 return res;
881}
882
883static void asb100_write_value(struct i2c_client *client, u16 reg, u16 value)
884{
885 struct asb100_data *data = i2c_get_clientdata(client);
886 struct i2c_client *cl;
887 int bank;
888
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100889 mutex_lock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
891 bank = (reg >> 8) & 0x0f;
892 if (bank > 2)
893 /* switch banks */
894 i2c_smbus_write_byte_data(client, ASB100_REG_BANK, bank);
895
896 if (bank == 0 || bank > 2) {
897 i2c_smbus_write_byte_data(client, reg & 0xff, value & 0xff);
898 } else {
899 /* switch to subclient */
900 cl = data->lm75[bank - 1];
901
902 /* convert from ISA to LM75 I2C addresses */
903 switch (reg & 0xff) {
904 case 0x52: /* CONFIG */
905 i2c_smbus_write_byte_data(cl, 1, value & 0xff);
906 break;
907 case 0x53: /* HYST */
908 i2c_smbus_write_word_data(cl, 2, swab16(value));
909 break;
910 case 0x55: /* MAX */
911 i2c_smbus_write_word_data(cl, 3, swab16(value));
912 break;
913 }
914 }
915
916 if (bank > 2)
917 i2c_smbus_write_byte_data(client, ASB100_REG_BANK, 0);
918
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100919 mutex_unlock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920}
921
922static void asb100_init_client(struct i2c_client *client)
923{
924 struct asb100_data *data = i2c_get_clientdata(client);
925 int vid = 0;
926
927 vid = asb100_read_value(client, ASB100_REG_VID_FANDIV) & 0x0f;
928 vid |= (asb100_read_value(client, ASB100_REG_CHIPID) & 0x01) << 4;
Jean Delvare303760b2005-07-31 21:52:01 +0200929 data->vrm = vid_which_vrm();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 vid = vid_from_reg(vid, data->vrm);
931
932 /* Start monitoring */
Jean Delvareaf2219312008-01-03 23:15:49 +0100933 asb100_write_value(client, ASB100_REG_CONFIG,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 (asb100_read_value(client, ASB100_REG_CONFIG) & 0xf7) | 0x01);
935}
936
937static struct asb100_data *asb100_update_device(struct device *dev)
938{
939 struct i2c_client *client = to_i2c_client(dev);
940 struct asb100_data *data = i2c_get_clientdata(client);
941 int i;
942
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100943 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
945 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
946 || !data->valid) {
947
948 dev_dbg(&client->dev, "starting device update...\n");
949
950 /* 7 voltage inputs */
951 for (i = 0; i < 7; i++) {
952 data->in[i] = asb100_read_value(client,
953 ASB100_REG_IN(i));
954 data->in_min[i] = asb100_read_value(client,
955 ASB100_REG_IN_MIN(i));
956 data->in_max[i] = asb100_read_value(client,
957 ASB100_REG_IN_MAX(i));
958 }
959
960 /* 3 fan inputs */
961 for (i = 0; i < 3; i++) {
962 data->fan[i] = asb100_read_value(client,
963 ASB100_REG_FAN(i));
964 data->fan_min[i] = asb100_read_value(client,
965 ASB100_REG_FAN_MIN(i));
966 }
967
968 /* 4 temperature inputs */
969 for (i = 1; i <= 4; i++) {
970 data->temp[i-1] = asb100_read_value(client,
971 ASB100_REG_TEMP(i));
972 data->temp_max[i-1] = asb100_read_value(client,
973 ASB100_REG_TEMP_MAX(i));
974 data->temp_hyst[i-1] = asb100_read_value(client,
975 ASB100_REG_TEMP_HYST(i));
976 }
977
978 /* VID and fan divisors */
979 i = asb100_read_value(client, ASB100_REG_VID_FANDIV);
980 data->vid = i & 0x0f;
981 data->vid |= (asb100_read_value(client,
982 ASB100_REG_CHIPID) & 0x01) << 4;
983 data->fan_div[0] = (i >> 4) & 0x03;
984 data->fan_div[1] = (i >> 6) & 0x03;
985 data->fan_div[2] = (asb100_read_value(client,
986 ASB100_REG_PIN) >> 6) & 0x03;
987
988 /* PWM */
989 data->pwm = asb100_read_value(client, ASB100_REG_PWM1);
990
991 /* alarms */
992 data->alarms = asb100_read_value(client, ASB100_REG_ALARM1) +
993 (asb100_read_value(client, ASB100_REG_ALARM2) << 8);
994
995 data->last_updated = jiffies;
996 data->valid = 1;
997
998 dev_dbg(&client->dev, "... device update complete\n");
999 }
1000
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001001 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
1003 return data;
1004}
1005
1006static int __init asb100_init(void)
1007{
1008 return i2c_add_driver(&asb100_driver);
1009}
1010
1011static void __exit asb100_exit(void)
1012{
1013 i2c_del_driver(&asb100_driver);
1014}
1015
1016MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
1017MODULE_DESCRIPTION("ASB100 Bach driver");
1018MODULE_LICENSE("GPL");
1019
1020module_init(asb100_init);
1021module_exit(asb100_exit);