]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/hwmon/asb100.c
vt1211: Document module parameters
[mirror_ubuntu-artful-kernel.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 */
146static u8 TEMP_TO_REG(int temp)
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;
943b0830 185 struct class_device *class_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 224 .id = I2C_DRIVERID_ASB100,
1da177e4
LT
225 .attach_adapter = asb100_attach_adapter,
226 .detach_client = asb100_detach_client,
227};
228
229/* 7 Voltages */
230#define show_in_reg(reg) \
231static ssize_t show_##reg (struct device *dev, char *buf, int nr) \
232{ \
233 struct asb100_data *data = asb100_update_device(dev); \
234 return sprintf(buf, "%d\n", IN_FROM_REG(data->reg[nr])); \
235}
236
237show_in_reg(in)
238show_in_reg(in_min)
239show_in_reg(in_max)
240
241#define set_in_reg(REG, reg) \
242static ssize_t set_in_##reg(struct device *dev, const char *buf, \
243 size_t count, int nr) \
244{ \
245 struct i2c_client *client = to_i2c_client(dev); \
246 struct asb100_data *data = i2c_get_clientdata(client); \
247 unsigned long val = simple_strtoul(buf, NULL, 10); \
248 \
9a61bf63 249 mutex_lock(&data->update_lock); \
1da177e4
LT
250 data->in_##reg[nr] = IN_TO_REG(val); \
251 asb100_write_value(client, ASB100_REG_IN_##REG(nr), \
252 data->in_##reg[nr]); \
9a61bf63 253 mutex_unlock(&data->update_lock); \
1da177e4
LT
254 return count; \
255}
256
257set_in_reg(MIN, min)
258set_in_reg(MAX, max)
259
260#define sysfs_in(offset) \
261static ssize_t \
30f74292 262 show_in##offset (struct device *dev, struct device_attribute *attr, char *buf) \
1da177e4
LT
263{ \
264 return show_in(dev, buf, offset); \
265} \
266static DEVICE_ATTR(in##offset##_input, S_IRUGO, \
267 show_in##offset, NULL); \
268static ssize_t \
30f74292 269 show_in##offset##_min (struct device *dev, struct device_attribute *attr, char *buf) \
1da177e4
LT
270{ \
271 return show_in_min(dev, buf, offset); \
272} \
273static ssize_t \
30f74292 274 show_in##offset##_max (struct device *dev, struct device_attribute *attr, char *buf) \
1da177e4
LT
275{ \
276 return show_in_max(dev, buf, offset); \
277} \
30f74292 278static ssize_t set_in##offset##_min (struct device *dev, struct device_attribute *attr, \
1da177e4
LT
279 const char *buf, size_t count) \
280{ \
281 return set_in_min(dev, buf, count, offset); \
282} \
30f74292 283static ssize_t set_in##offset##_max (struct device *dev, struct device_attribute *attr, \
1da177e4
LT
284 const char *buf, size_t count) \
285{ \
286 return set_in_max(dev, buf, count, offset); \
287} \
288static DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
289 show_in##offset##_min, set_in##offset##_min); \
290static DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
291 show_in##offset##_max, set_in##offset##_max);
292
293sysfs_in(0);
294sysfs_in(1);
295sysfs_in(2);
296sysfs_in(3);
297sysfs_in(4);
298sysfs_in(5);
299sysfs_in(6);
300
301#define device_create_file_in(client, offset) do { \
302 device_create_file(&client->dev, &dev_attr_in##offset##_input); \
303 device_create_file(&client->dev, &dev_attr_in##offset##_min); \
304 device_create_file(&client->dev, &dev_attr_in##offset##_max); \
305} while (0)
306
307/* 3 Fans */
308static ssize_t show_fan(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[nr],
312 DIV_FROM_REG(data->fan_div[nr])));
313}
314
315static ssize_t show_fan_min(struct device *dev, char *buf, int nr)
316{
317 struct asb100_data *data = asb100_update_device(dev);
318 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
319 DIV_FROM_REG(data->fan_div[nr])));
320}
321
322static ssize_t show_fan_div(struct device *dev, char *buf, int nr)
323{
324 struct asb100_data *data = asb100_update_device(dev);
325 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
326}
327
328static ssize_t set_fan_min(struct device *dev, const char *buf,
329 size_t count, int nr)
330{
331 struct i2c_client *client = to_i2c_client(dev);
332 struct asb100_data *data = i2c_get_clientdata(client);
333 u32 val = simple_strtoul(buf, NULL, 10);
334
9a61bf63 335 mutex_lock(&data->update_lock);
1da177e4
LT
336 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
337 asb100_write_value(client, ASB100_REG_FAN_MIN(nr), data->fan_min[nr]);
9a61bf63 338 mutex_unlock(&data->update_lock);
1da177e4
LT
339 return count;
340}
341
342/* Note: we save and restore the fan minimum here, because its value is
343 determined in part by the fan divisor. This follows the principle of
d6e05edc 344 least surprise; the user doesn't expect the fan minimum to change just
1da177e4
LT
345 because the divisor changed. */
346static ssize_t set_fan_div(struct device *dev, const char *buf,
347 size_t count, int nr)
348{
349 struct i2c_client *client = to_i2c_client(dev);
350 struct asb100_data *data = i2c_get_clientdata(client);
351 unsigned long min;
352 unsigned long val = simple_strtoul(buf, NULL, 10);
353 int reg;
354
9a61bf63 355 mutex_lock(&data->update_lock);
1da177e4
LT
356
357 min = FAN_FROM_REG(data->fan_min[nr],
358 DIV_FROM_REG(data->fan_div[nr]));
359 data->fan_div[nr] = DIV_TO_REG(val);
360
361 switch(nr) {
362 case 0: /* fan 1 */
363 reg = asb100_read_value(client, ASB100_REG_VID_FANDIV);
364 reg = (reg & 0xcf) | (data->fan_div[0] << 4);
365 asb100_write_value(client, ASB100_REG_VID_FANDIV, reg);
366 break;
367
368 case 1: /* fan 2 */
369 reg = asb100_read_value(client, ASB100_REG_VID_FANDIV);
370 reg = (reg & 0x3f) | (data->fan_div[1] << 6);
371 asb100_write_value(client, ASB100_REG_VID_FANDIV, reg);
372 break;
373
374 case 2: /* fan 3 */
375 reg = asb100_read_value(client, ASB100_REG_PIN);
376 reg = (reg & 0x3f) | (data->fan_div[2] << 6);
377 asb100_write_value(client, ASB100_REG_PIN, reg);
378 break;
379 }
380
381 data->fan_min[nr] =
382 FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
383 asb100_write_value(client, ASB100_REG_FAN_MIN(nr), data->fan_min[nr]);
384
9a61bf63 385 mutex_unlock(&data->update_lock);
1da177e4
LT
386
387 return count;
388}
389
390#define sysfs_fan(offset) \
30f74292 391static ssize_t show_fan##offset(struct device *dev, struct device_attribute *attr, char *buf) \
1da177e4
LT
392{ \
393 return show_fan(dev, buf, offset - 1); \
394} \
30f74292 395static ssize_t show_fan##offset##_min(struct device *dev, struct device_attribute *attr, char *buf) \
1da177e4
LT
396{ \
397 return show_fan_min(dev, buf, offset - 1); \
398} \
30f74292 399static ssize_t show_fan##offset##_div(struct device *dev, struct device_attribute *attr, char *buf) \
1da177e4
LT
400{ \
401 return show_fan_div(dev, buf, offset - 1); \
402} \
30f74292 403static ssize_t set_fan##offset##_min(struct device *dev, struct device_attribute *attr, const char *buf, \
1da177e4
LT
404 size_t count) \
405{ \
406 return set_fan_min(dev, buf, count, offset - 1); \
407} \
30f74292 408static ssize_t set_fan##offset##_div(struct device *dev, struct device_attribute *attr, const char *buf, \
1da177e4
LT
409 size_t count) \
410{ \
411 return set_fan_div(dev, buf, count, offset - 1); \
412} \
413static DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
414 show_fan##offset, NULL); \
415static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
416 show_fan##offset##_min, set_fan##offset##_min); \
417static DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
418 show_fan##offset##_div, set_fan##offset##_div);
419
420sysfs_fan(1);
421sysfs_fan(2);
422sysfs_fan(3);
423
424#define device_create_file_fan(client, offset) do { \
425 device_create_file(&client->dev, &dev_attr_fan##offset##_input); \
426 device_create_file(&client->dev, &dev_attr_fan##offset##_min); \
427 device_create_file(&client->dev, &dev_attr_fan##offset##_div); \
428} while (0)
429
430/* 4 Temp. Sensors */
431static int sprintf_temp_from_reg(u16 reg, char *buf, int nr)
432{
433 int ret = 0;
434
435 switch (nr) {
436 case 1: case 2:
437 ret = sprintf(buf, "%d\n", LM75_TEMP_FROM_REG(reg));
438 break;
439 case 0: case 3: default:
440 ret = sprintf(buf, "%d\n", TEMP_FROM_REG(reg));
441 break;
442 }
443 return ret;
444}
445
446#define show_temp_reg(reg) \
447static ssize_t show_##reg(struct device *dev, char *buf, int nr) \
448{ \
449 struct asb100_data *data = asb100_update_device(dev); \
450 return sprintf_temp_from_reg(data->reg[nr], buf, nr); \
451}
452
453show_temp_reg(temp);
454show_temp_reg(temp_max);
455show_temp_reg(temp_hyst);
456
457#define set_temp_reg(REG, reg) \
458static ssize_t set_##reg(struct device *dev, const char *buf, \
459 size_t count, int nr) \
460{ \
461 struct i2c_client *client = to_i2c_client(dev); \
462 struct asb100_data *data = i2c_get_clientdata(client); \
463 unsigned long val = simple_strtoul(buf, NULL, 10); \
464 \
9a61bf63 465 mutex_lock(&data->update_lock); \
1da177e4
LT
466 switch (nr) { \
467 case 1: case 2: \
468 data->reg[nr] = LM75_TEMP_TO_REG(val); \
469 break; \
470 case 0: case 3: default: \
471 data->reg[nr] = TEMP_TO_REG(val); \
472 break; \
473 } \
474 asb100_write_value(client, ASB100_REG_TEMP_##REG(nr+1), \
475 data->reg[nr]); \
9a61bf63 476 mutex_unlock(&data->update_lock); \
1da177e4
LT
477 return count; \
478}
479
480set_temp_reg(MAX, temp_max);
481set_temp_reg(HYST, temp_hyst);
482
483#define sysfs_temp(num) \
30f74292 484static ssize_t show_temp##num(struct device *dev, struct device_attribute *attr, char *buf) \
1da177e4
LT
485{ \
486 return show_temp(dev, buf, num-1); \
487} \
488static DEVICE_ATTR(temp##num##_input, S_IRUGO, show_temp##num, NULL); \
30f74292 489static ssize_t show_temp_max##num(struct device *dev, struct device_attribute *attr, char *buf) \
1da177e4
LT
490{ \
491 return show_temp_max(dev, buf, num-1); \
492} \
30f74292 493static ssize_t set_temp_max##num(struct device *dev, struct device_attribute *attr, const char *buf, \
1da177e4
LT
494 size_t count) \
495{ \
496 return set_temp_max(dev, buf, count, num-1); \
497} \
498static DEVICE_ATTR(temp##num##_max, S_IRUGO | S_IWUSR, \
499 show_temp_max##num, set_temp_max##num); \
30f74292 500static ssize_t show_temp_hyst##num(struct device *dev, struct device_attribute *attr, char *buf) \
1da177e4
LT
501{ \
502 return show_temp_hyst(dev, buf, num-1); \
503} \
30f74292 504static ssize_t set_temp_hyst##num(struct device *dev, struct device_attribute *attr, const char *buf, \
1da177e4
LT
505 size_t count) \
506{ \
507 return set_temp_hyst(dev, buf, count, num-1); \
508} \
509static DEVICE_ATTR(temp##num##_max_hyst, S_IRUGO | S_IWUSR, \
510 show_temp_hyst##num, set_temp_hyst##num);
511
512sysfs_temp(1);
513sysfs_temp(2);
514sysfs_temp(3);
515sysfs_temp(4);
516
517/* VID */
518#define device_create_file_temp(client, num) do { \
519 device_create_file(&client->dev, &dev_attr_temp##num##_input); \
520 device_create_file(&client->dev, &dev_attr_temp##num##_max); \
521 device_create_file(&client->dev, &dev_attr_temp##num##_max_hyst); \
522} while (0)
523
30f74292 524static ssize_t show_vid(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
525{
526 struct asb100_data *data = asb100_update_device(dev);
527 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
528}
529
530static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
531#define device_create_file_vid(client) \
532device_create_file(&client->dev, &dev_attr_cpu0_vid)
533
534/* VRM */
30f74292 535static ssize_t show_vrm(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
536{
537 struct asb100_data *data = asb100_update_device(dev);
538 return sprintf(buf, "%d\n", data->vrm);
539}
540
30f74292 541static ssize_t set_vrm(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1da177e4
LT
542{
543 struct i2c_client *client = to_i2c_client(dev);
544 struct asb100_data *data = i2c_get_clientdata(client);
545 unsigned long val = simple_strtoul(buf, NULL, 10);
546 data->vrm = val;
547 return count;
548}
549
550/* Alarms */
551static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm);
552#define device_create_file_vrm(client) \
553device_create_file(&client->dev, &dev_attr_vrm);
554
30f74292 555static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
556{
557 struct asb100_data *data = asb100_update_device(dev);
68188ba7 558 return sprintf(buf, "%u\n", data->alarms);
1da177e4
LT
559}
560
561static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
562#define device_create_file_alarms(client) \
563device_create_file(&client->dev, &dev_attr_alarms)
564
565/* 1 PWM */
30f74292 566static ssize_t show_pwm1(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
567{
568 struct asb100_data *data = asb100_update_device(dev);
569 return sprintf(buf, "%d\n", ASB100_PWM_FROM_REG(data->pwm & 0x0f));
570}
571
30f74292 572static ssize_t set_pwm1(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1da177e4
LT
573{
574 struct i2c_client *client = to_i2c_client(dev);
575 struct asb100_data *data = i2c_get_clientdata(client);
576 unsigned long val = simple_strtoul(buf, NULL, 10);
577
9a61bf63 578 mutex_lock(&data->update_lock);
1da177e4
LT
579 data->pwm &= 0x80; /* keep the enable bit */
580 data->pwm |= (0x0f & ASB100_PWM_TO_REG(val));
581 asb100_write_value(client, ASB100_REG_PWM1, data->pwm);
9a61bf63 582 mutex_unlock(&data->update_lock);
1da177e4
LT
583 return count;
584}
585
30f74292 586static ssize_t show_pwm_enable1(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
587{
588 struct asb100_data *data = asb100_update_device(dev);
589 return sprintf(buf, "%d\n", (data->pwm & 0x80) ? 1 : 0);
590}
591
30f74292 592static ssize_t set_pwm_enable1(struct device *dev, struct device_attribute *attr, const char *buf,
1da177e4
LT
593 size_t count)
594{
595 struct i2c_client *client = to_i2c_client(dev);
596 struct asb100_data *data = i2c_get_clientdata(client);
597 unsigned long val = simple_strtoul(buf, NULL, 10);
598
9a61bf63 599 mutex_lock(&data->update_lock);
1da177e4
LT
600 data->pwm &= 0x0f; /* keep the duty cycle bits */
601 data->pwm |= (val ? 0x80 : 0x00);
602 asb100_write_value(client, ASB100_REG_PWM1, data->pwm);
9a61bf63 603 mutex_unlock(&data->update_lock);
1da177e4
LT
604 return count;
605}
606
607static DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm1, set_pwm1);
608static DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
609 show_pwm_enable1, set_pwm_enable1);
610#define device_create_file_pwm1(client) do { \
611 device_create_file(&new_client->dev, &dev_attr_pwm1); \
612 device_create_file(&new_client->dev, &dev_attr_pwm1_enable); \
613} while (0)
614
615/* This function is called when:
616 asb100_driver is inserted (when this module is loaded), for each
617 available adapter
618 when a new adapter is inserted (and asb100_driver is still present)
619 */
620static int asb100_attach_adapter(struct i2c_adapter *adapter)
621{
622 if (!(adapter->class & I2C_CLASS_HWMON))
623 return 0;
2ed2dc3c 624 return i2c_probe(adapter, &addr_data, asb100_detect);
1da177e4
LT
625}
626
627static int asb100_detect_subclients(struct i2c_adapter *adapter, int address,
628 int kind, struct i2c_client *new_client)
629{
630 int i, id, err;
631 struct asb100_data *data = i2c_get_clientdata(new_client);
632
ba9c2e8d 633 data->lm75[0] = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
1da177e4
LT
634 if (!(data->lm75[0])) {
635 err = -ENOMEM;
636 goto ERROR_SC_0;
637 }
1da177e4 638
ba9c2e8d 639 data->lm75[1] = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
1da177e4
LT
640 if (!(data->lm75[1])) {
641 err = -ENOMEM;
642 goto ERROR_SC_1;
643 }
1da177e4
LT
644
645 id = i2c_adapter_id(adapter);
646
647 if (force_subclients[0] == id && force_subclients[1] == address) {
648 for (i = 2; i <= 3; i++) {
649 if (force_subclients[i] < 0x48 ||
650 force_subclients[i] > 0x4f) {
651 dev_err(&new_client->dev, "invalid subclient "
652 "address %d; must be 0x48-0x4f\n",
653 force_subclients[i]);
654 err = -ENODEV;
655 goto ERROR_SC_2;
656 }
657 }
658 asb100_write_value(new_client, ASB100_REG_I2C_SUBADDR,
659 (force_subclients[2] & 0x07) |
660 ((force_subclients[3] & 0x07) <<4));
661 data->lm75[0]->addr = force_subclients[2];
662 data->lm75[1]->addr = force_subclients[3];
663 } else {
664 int val = asb100_read_value(new_client, ASB100_REG_I2C_SUBADDR);
665 data->lm75[0]->addr = 0x48 + (val & 0x07);
666 data->lm75[1]->addr = 0x48 + ((val >> 4) & 0x07);
667 }
668
669 if(data->lm75[0]->addr == data->lm75[1]->addr) {
670 dev_err(&new_client->dev, "duplicate addresses 0x%x "
671 "for subclients\n", data->lm75[0]->addr);
672 err = -ENODEV;
673 goto ERROR_SC_2;
674 }
675
676 for (i = 0; i <= 1; i++) {
677 i2c_set_clientdata(data->lm75[i], NULL);
678 data->lm75[i]->adapter = adapter;
679 data->lm75[i]->driver = &asb100_driver;
680 data->lm75[i]->flags = 0;
681 strlcpy(data->lm75[i]->name, "asb100 subclient", I2C_NAME_SIZE);
682 }
683
684 if ((err = i2c_attach_client(data->lm75[0]))) {
685 dev_err(&new_client->dev, "subclient %d registration "
686 "at address 0x%x failed.\n", i, data->lm75[0]->addr);
687 goto ERROR_SC_2;
688 }
689
690 if ((err = i2c_attach_client(data->lm75[1]))) {
691 dev_err(&new_client->dev, "subclient %d registration "
692 "at address 0x%x failed.\n", i, data->lm75[1]->addr);
693 goto ERROR_SC_3;
694 }
695
696 return 0;
697
698/* Undo inits in case of errors */
699ERROR_SC_3:
700 i2c_detach_client(data->lm75[0]);
701ERROR_SC_2:
702 kfree(data->lm75[1]);
703ERROR_SC_1:
704 kfree(data->lm75[0]);
705ERROR_SC_0:
706 return err;
707}
708
709static int asb100_detect(struct i2c_adapter *adapter, int address, int kind)
710{
711 int err;
712 struct i2c_client *new_client;
713 struct asb100_data *data;
714
1da177e4
LT
715 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
716 pr_debug("asb100.o: detect failed, "
717 "smbus byte data not supported!\n");
718 err = -ENODEV;
719 goto ERROR0;
720 }
721
722 /* OK. For now, we presume we have a valid client. We now create the
723 client structure, even though we cannot fill it completely yet.
724 But it allows us to access asb100_{read,write}_value. */
725
ba9c2e8d
DS
726 if (!(data = kzalloc(sizeof(struct asb100_data), GFP_KERNEL))) {
727 pr_debug("asb100.o: detect failed, kzalloc failed!\n");
1da177e4
LT
728 err = -ENOMEM;
729 goto ERROR0;
730 }
1da177e4
LT
731
732 new_client = &data->client;
9a61bf63 733 mutex_init(&data->lock);
1da177e4
LT
734 i2c_set_clientdata(new_client, data);
735 new_client->addr = address;
736 new_client->adapter = adapter;
737 new_client->driver = &asb100_driver;
738 new_client->flags = 0;
739
740 /* Now, we do the remaining detection. */
741
742 /* The chip may be stuck in some other bank than bank 0. This may
743 make reading other information impossible. Specify a force=... or
744 force_*=... parameter, and the chip will be reset to the right
745 bank. */
746 if (kind < 0) {
747
748 int val1 = asb100_read_value(new_client, ASB100_REG_BANK);
749 int val2 = asb100_read_value(new_client, ASB100_REG_CHIPMAN);
750
751 /* If we're in bank 0 */
752 if ( (!(val1 & 0x07)) &&
753 /* Check for ASB100 ID (low byte) */
754 ( ((!(val1 & 0x80)) && (val2 != 0x94)) ||
755 /* Check for ASB100 ID (high byte ) */
756 ((val1 & 0x80) && (val2 != 0x06)) ) ) {
757 pr_debug("asb100.o: detect failed, "
758 "bad chip id 0x%02x!\n", val2);
759 err = -ENODEV;
760 goto ERROR1;
761 }
762
763 } /* kind < 0 */
764
765 /* We have either had a force parameter, or we have already detected
766 Winbond. Put it now into bank 0 and Vendor ID High Byte */
767 asb100_write_value(new_client, ASB100_REG_BANK,
768 (asb100_read_value(new_client, ASB100_REG_BANK) & 0x78) | 0x80);
769
770 /* Determine the chip type. */
771 if (kind <= 0) {
772 int val1 = asb100_read_value(new_client, ASB100_REG_WCHIPID);
773 int val2 = asb100_read_value(new_client, ASB100_REG_CHIPMAN);
774
775 if ((val1 == 0x31) && (val2 == 0x06))
776 kind = asb100;
777 else {
778 if (kind == 0)
779 dev_warn(&new_client->dev, "ignoring "
780 "'force' parameter for unknown chip "
781 "at adapter %d, address 0x%02x.\n",
782 i2c_adapter_id(adapter), address);
783 err = -ENODEV;
784 goto ERROR1;
785 }
786 }
787
788 /* Fill in remaining client fields and put it into the global list */
789 strlcpy(new_client->name, "asb100", I2C_NAME_SIZE);
790 data->type = kind;
791
792 data->valid = 0;
9a61bf63 793 mutex_init(&data->update_lock);
1da177e4
LT
794
795 /* Tell the I2C layer a new client has arrived */
796 if ((err = i2c_attach_client(new_client)))
797 goto ERROR1;
798
799 /* Attach secondary lm75 clients */
800 if ((err = asb100_detect_subclients(adapter, address, kind,
801 new_client)))
802 goto ERROR2;
803
804 /* Initialize the chip */
805 asb100_init_client(new_client);
806
807 /* A few vars need to be filled upon startup */
808 data->fan_min[0] = asb100_read_value(new_client, ASB100_REG_FAN_MIN(0));
809 data->fan_min[1] = asb100_read_value(new_client, ASB100_REG_FAN_MIN(1));
810 data->fan_min[2] = asb100_read_value(new_client, ASB100_REG_FAN_MIN(2));
811
812 /* Register sysfs hooks */
943b0830
MH
813 data->class_dev = hwmon_device_register(&new_client->dev);
814 if (IS_ERR(data->class_dev)) {
815 err = PTR_ERR(data->class_dev);
816 goto ERROR3;
817 }
818
1da177e4
LT
819 device_create_file_in(new_client, 0);
820 device_create_file_in(new_client, 1);
821 device_create_file_in(new_client, 2);
822 device_create_file_in(new_client, 3);
823 device_create_file_in(new_client, 4);
824 device_create_file_in(new_client, 5);
825 device_create_file_in(new_client, 6);
826
827 device_create_file_fan(new_client, 1);
828 device_create_file_fan(new_client, 2);
829 device_create_file_fan(new_client, 3);
830
831 device_create_file_temp(new_client, 1);
832 device_create_file_temp(new_client, 2);
833 device_create_file_temp(new_client, 3);
834 device_create_file_temp(new_client, 4);
835
836 device_create_file_vid(new_client);
837 device_create_file_vrm(new_client);
838
839 device_create_file_alarms(new_client);
840
841 device_create_file_pwm1(new_client);
842
843 return 0;
844
943b0830
MH
845ERROR3:
846 i2c_detach_client(data->lm75[1]);
847 i2c_detach_client(data->lm75[0]);
848 kfree(data->lm75[1]);
849 kfree(data->lm75[0]);
1da177e4
LT
850ERROR2:
851 i2c_detach_client(new_client);
852ERROR1:
853 kfree(data);
854ERROR0:
855 return err;
856}
857
858static int asb100_detach_client(struct i2c_client *client)
859{
943b0830 860 struct asb100_data *data = i2c_get_clientdata(client);
1da177e4
LT
861 int err;
862
943b0830
MH
863 /* main client */
864 if (data)
865 hwmon_device_unregister(data->class_dev);
866
7bef5594 867 if ((err = i2c_detach_client(client)))
1da177e4 868 return err;
1da177e4 869
943b0830
MH
870 /* main client */
871 if (data)
872 kfree(data);
873
874 /* subclient */
875 else
1da177e4 876 kfree(client);
1da177e4
LT
877
878 return 0;
879}
880
881/* The SMBus locks itself, usually, but nothing may access the chip between
882 bank switches. */
883static int asb100_read_value(struct i2c_client *client, u16 reg)
884{
885 struct asb100_data *data = i2c_get_clientdata(client);
886 struct i2c_client *cl;
887 int res, bank;
888
9a61bf63 889 mutex_lock(&data->lock);
1da177e4
LT
890
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 res = i2c_smbus_read_byte_data(client, reg & 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 0x50: /* TEMP */
905 res = swab16(i2c_smbus_read_word_data (cl, 0));
906 break;
907 case 0x52: /* CONFIG */
908 res = i2c_smbus_read_byte_data(cl, 1);
909 break;
910 case 0x53: /* HYST */
911 res = swab16(i2c_smbus_read_word_data (cl, 2));
912 break;
913 case 0x55: /* MAX */
914 default:
915 res = swab16(i2c_smbus_read_word_data (cl, 3));
916 break;
917 }
918 }
919
920 if (bank > 2)
921 i2c_smbus_write_byte_data(client, ASB100_REG_BANK, 0);
922
9a61bf63 923 mutex_unlock(&data->lock);
1da177e4
LT
924
925 return res;
926}
927
928static void asb100_write_value(struct i2c_client *client, u16 reg, u16 value)
929{
930 struct asb100_data *data = i2c_get_clientdata(client);
931 struct i2c_client *cl;
932 int bank;
933
9a61bf63 934 mutex_lock(&data->lock);
1da177e4
LT
935
936 bank = (reg >> 8) & 0x0f;
937 if (bank > 2)
938 /* switch banks */
939 i2c_smbus_write_byte_data(client, ASB100_REG_BANK, bank);
940
941 if (bank == 0 || bank > 2) {
942 i2c_smbus_write_byte_data(client, reg & 0xff, value & 0xff);
943 } else {
944 /* switch to subclient */
945 cl = data->lm75[bank - 1];
946
947 /* convert from ISA to LM75 I2C addresses */
948 switch (reg & 0xff) {
949 case 0x52: /* CONFIG */
950 i2c_smbus_write_byte_data(cl, 1, value & 0xff);
951 break;
952 case 0x53: /* HYST */
953 i2c_smbus_write_word_data(cl, 2, swab16(value));
954 break;
955 case 0x55: /* MAX */
956 i2c_smbus_write_word_data(cl, 3, swab16(value));
957 break;
958 }
959 }
960
961 if (bank > 2)
962 i2c_smbus_write_byte_data(client, ASB100_REG_BANK, 0);
963
9a61bf63 964 mutex_unlock(&data->lock);
1da177e4
LT
965}
966
967static void asb100_init_client(struct i2c_client *client)
968{
969 struct asb100_data *data = i2c_get_clientdata(client);
970 int vid = 0;
971
972 vid = asb100_read_value(client, ASB100_REG_VID_FANDIV) & 0x0f;
973 vid |= (asb100_read_value(client, ASB100_REG_CHIPID) & 0x01) << 4;
303760b4 974 data->vrm = vid_which_vrm();
1da177e4
LT
975 vid = vid_from_reg(vid, data->vrm);
976
977 /* Start monitoring */
978 asb100_write_value(client, ASB100_REG_CONFIG,
979 (asb100_read_value(client, ASB100_REG_CONFIG) & 0xf7) | 0x01);
980}
981
982static struct asb100_data *asb100_update_device(struct device *dev)
983{
984 struct i2c_client *client = to_i2c_client(dev);
985 struct asb100_data *data = i2c_get_clientdata(client);
986 int i;
987
9a61bf63 988 mutex_lock(&data->update_lock);
1da177e4
LT
989
990 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
991 || !data->valid) {
992
993 dev_dbg(&client->dev, "starting device update...\n");
994
995 /* 7 voltage inputs */
996 for (i = 0; i < 7; i++) {
997 data->in[i] = asb100_read_value(client,
998 ASB100_REG_IN(i));
999 data->in_min[i] = asb100_read_value(client,
1000 ASB100_REG_IN_MIN(i));
1001 data->in_max[i] = asb100_read_value(client,
1002 ASB100_REG_IN_MAX(i));
1003 }
1004
1005 /* 3 fan inputs */
1006 for (i = 0; i < 3; i++) {
1007 data->fan[i] = asb100_read_value(client,
1008 ASB100_REG_FAN(i));
1009 data->fan_min[i] = asb100_read_value(client,
1010 ASB100_REG_FAN_MIN(i));
1011 }
1012
1013 /* 4 temperature inputs */
1014 for (i = 1; i <= 4; i++) {
1015 data->temp[i-1] = asb100_read_value(client,
1016 ASB100_REG_TEMP(i));
1017 data->temp_max[i-1] = asb100_read_value(client,
1018 ASB100_REG_TEMP_MAX(i));
1019 data->temp_hyst[i-1] = asb100_read_value(client,
1020 ASB100_REG_TEMP_HYST(i));
1021 }
1022
1023 /* VID and fan divisors */
1024 i = asb100_read_value(client, ASB100_REG_VID_FANDIV);
1025 data->vid = i & 0x0f;
1026 data->vid |= (asb100_read_value(client,
1027 ASB100_REG_CHIPID) & 0x01) << 4;
1028 data->fan_div[0] = (i >> 4) & 0x03;
1029 data->fan_div[1] = (i >> 6) & 0x03;
1030 data->fan_div[2] = (asb100_read_value(client,
1031 ASB100_REG_PIN) >> 6) & 0x03;
1032
1033 /* PWM */
1034 data->pwm = asb100_read_value(client, ASB100_REG_PWM1);
1035
1036 /* alarms */
1037 data->alarms = asb100_read_value(client, ASB100_REG_ALARM1) +
1038 (asb100_read_value(client, ASB100_REG_ALARM2) << 8);
1039
1040 data->last_updated = jiffies;
1041 data->valid = 1;
1042
1043 dev_dbg(&client->dev, "... device update complete\n");
1044 }
1045
9a61bf63 1046 mutex_unlock(&data->update_lock);
1da177e4
LT
1047
1048 return data;
1049}
1050
1051static int __init asb100_init(void)
1052{
1053 return i2c_add_driver(&asb100_driver);
1054}
1055
1056static void __exit asb100_exit(void)
1057{
1058 i2c_del_driver(&asb100_driver);
1059}
1060
1061MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
1062MODULE_DESCRIPTION("ASB100 Bach driver");
1063MODULE_LICENSE("GPL");
1064
1065module_init(asb100_init);
1066module_exit(asb100_exit);
1067