blob: 342b1a98a8cc241fcb77f0501af448519620c8e3 [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>
42#include <linux/i2c-sensor.h>
43#include <linux/i2c-vid.h>
44#include <linux/init.h>
Dominik Hacklff324092005-05-16 18:12:18 +020045#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include "lm75.h"
47
48/*
49 HISTORY:
50 2003-12-29 1.0.0 Ported from lm_sensors project for kernel 2.6
51*/
52#define ASB100_VERSION "1.0.0"
53
54/* I2C addresses to scan */
55static unsigned short normal_i2c[] = { 0x2d, I2C_CLIENT_END };
56
57/* ISA addresses to scan (none) */
58static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
59
60/* Insmod parameters */
61SENSORS_INSMOD_1(asb100);
62I2C_CLIENT_MODULE_PARM(force_subclients, "List of subclient addresses: "
63 "{bus, clientaddr, subclientaddr1, subclientaddr2}");
64
65/* Voltage IN registers 0-6 */
66#define ASB100_REG_IN(nr) (0x20 + (nr))
67#define ASB100_REG_IN_MAX(nr) (0x2b + (nr * 2))
68#define ASB100_REG_IN_MIN(nr) (0x2c + (nr * 2))
69
70/* FAN IN registers 1-3 */
71#define ASB100_REG_FAN(nr) (0x28 + (nr))
72#define ASB100_REG_FAN_MIN(nr) (0x3b + (nr))
73
74/* TEMPERATURE registers 1-4 */
75static const u16 asb100_reg_temp[] = {0, 0x27, 0x150, 0x250, 0x17};
76static const u16 asb100_reg_temp_max[] = {0, 0x39, 0x155, 0x255, 0x18};
77static const u16 asb100_reg_temp_hyst[] = {0, 0x3a, 0x153, 0x253, 0x19};
78
79#define ASB100_REG_TEMP(nr) (asb100_reg_temp[nr])
80#define ASB100_REG_TEMP_MAX(nr) (asb100_reg_temp_max[nr])
81#define ASB100_REG_TEMP_HYST(nr) (asb100_reg_temp_hyst[nr])
82
83#define ASB100_REG_TEMP2_CONFIG 0x0152
84#define ASB100_REG_TEMP3_CONFIG 0x0252
85
86
87#define ASB100_REG_CONFIG 0x40
88#define ASB100_REG_ALARM1 0x41
89#define ASB100_REG_ALARM2 0x42
90#define ASB100_REG_SMIM1 0x43
91#define ASB100_REG_SMIM2 0x44
92#define ASB100_REG_VID_FANDIV 0x47
93#define ASB100_REG_I2C_ADDR 0x48
94#define ASB100_REG_CHIPID 0x49
95#define ASB100_REG_I2C_SUBADDR 0x4a
96#define ASB100_REG_PIN 0x4b
97#define ASB100_REG_IRQ 0x4c
98#define ASB100_REG_BANK 0x4e
99#define ASB100_REG_CHIPMAN 0x4f
100
101#define ASB100_REG_WCHIPID 0x58
102
103/* bit 7 -> enable, bits 0-3 -> duty cycle */
104#define ASB100_REG_PWM1 0x59
105
106/* CONVERSIONS
107 Rounding and limit checking is only done on the TO_REG variants. */
108
109/* These constants are a guess, consistent w/ w83781d */
110#define ASB100_IN_MIN ( 0)
111#define ASB100_IN_MAX (4080)
112
113/* IN: 1/1000 V (0V to 4.08V)
114 REG: 16mV/bit */
115static u8 IN_TO_REG(unsigned val)
116{
117 unsigned nval = SENSORS_LIMIT(val, ASB100_IN_MIN, ASB100_IN_MAX);
118 return (nval + 8) / 16;
119}
120
121static unsigned IN_FROM_REG(u8 reg)
122{
123 return reg * 16;
124}
125
126static u8 FAN_TO_REG(long rpm, int div)
127{
128 if (rpm == -1)
129 return 0;
130 if (rpm == 0)
131 return 255;
132 rpm = SENSORS_LIMIT(rpm, 1, 1000000);
133 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
134}
135
136static int FAN_FROM_REG(u8 val, int div)
137{
138 return val==0 ? -1 : val==255 ? 0 : 1350000/(val*div);
139}
140
141/* These constants are a guess, consistent w/ w83781d */
142#define ASB100_TEMP_MIN (-128000)
143#define ASB100_TEMP_MAX ( 127000)
144
145/* TEMP: 0.001C/bit (-128C to +127C)
146 REG: 1C/bit, two's complement */
147static u8 TEMP_TO_REG(int temp)
148{
149 int ntemp = SENSORS_LIMIT(temp, ASB100_TEMP_MIN, ASB100_TEMP_MAX);
150 ntemp += (ntemp<0 ? -500 : 500);
151 return (u8)(ntemp / 1000);
152}
153
154static int TEMP_FROM_REG(u8 reg)
155{
156 return (s8)reg * 1000;
157}
158
159/* PWM: 0 - 255 per sensors documentation
160 REG: (6.25% duty cycle per bit) */
161static u8 ASB100_PWM_TO_REG(int pwm)
162{
163 pwm = SENSORS_LIMIT(pwm, 0, 255);
164 return (u8)(pwm / 16);
165}
166
167static int ASB100_PWM_FROM_REG(u8 reg)
168{
169 return reg * 16;
170}
171
172#define ALARMS_FROM_REG(val) (val)
173
174#define DIV_FROM_REG(val) (1 << (val))
175
176/* FAN DIV: 1, 2, 4, or 8 (defaults to 2)
177 REG: 0, 1, 2, or 3 (respectively) (defaults to 1) */
178static u8 DIV_TO_REG(long val)
179{
180 return val==8 ? 3 : val==4 ? 2 : val==1 ? 0 : 1;
181}
182
183/* For each registered client, we need to keep some data in memory. That
184 data is pointed to by client->data. The structure itself is
185 dynamically allocated, at the same time the client itself is allocated. */
186struct asb100_data {
187 struct i2c_client client;
188 struct semaphore lock;
189 enum chips type;
190
191 struct semaphore update_lock;
192 unsigned long last_updated; /* In jiffies */
193
194 /* array of 2 pointers to subclients */
195 struct i2c_client *lm75[2];
196
197 char valid; /* !=0 if following fields are valid */
198 u8 in[7]; /* Register value */
199 u8 in_max[7]; /* Register value */
200 u8 in_min[7]; /* Register value */
201 u8 fan[3]; /* Register value */
202 u8 fan_min[3]; /* Register value */
203 u16 temp[4]; /* Register value (0 and 3 are u8 only) */
204 u16 temp_max[4]; /* Register value (0 and 3 are u8 only) */
205 u16 temp_hyst[4]; /* Register value (0 and 3 are u8 only) */
206 u8 fan_div[3]; /* Register encoding, right justified */
207 u8 pwm; /* Register encoding */
208 u8 vid; /* Register encoding, combined */
209 u32 alarms; /* Register encoding, combined */
210 u8 vrm;
211};
212
213static int asb100_read_value(struct i2c_client *client, u16 reg);
214static void asb100_write_value(struct i2c_client *client, u16 reg, u16 val);
215
216static int asb100_attach_adapter(struct i2c_adapter *adapter);
217static int asb100_detect(struct i2c_adapter *adapter, int address, int kind);
218static int asb100_detach_client(struct i2c_client *client);
219static struct asb100_data *asb100_update_device(struct device *dev);
220static void asb100_init_client(struct i2c_client *client);
221
222static struct i2c_driver asb100_driver = {
223 .owner = THIS_MODULE,
224 .name = "asb100",
225 .id = I2C_DRIVERID_ASB100,
226 .flags = I2C_DF_NOTIFY,
227 .attach_adapter = asb100_attach_adapter,
228 .detach_client = asb100_detach_client,
229};
230
231/* 7 Voltages */
232#define show_in_reg(reg) \
233static ssize_t show_##reg (struct device *dev, char *buf, int nr) \
234{ \
235 struct asb100_data *data = asb100_update_device(dev); \
236 return sprintf(buf, "%d\n", IN_FROM_REG(data->reg[nr])); \
237}
238
239show_in_reg(in)
240show_in_reg(in_min)
241show_in_reg(in_max)
242
243#define set_in_reg(REG, reg) \
244static ssize_t set_in_##reg(struct device *dev, const char *buf, \
245 size_t count, int nr) \
246{ \
247 struct i2c_client *client = to_i2c_client(dev); \
248 struct asb100_data *data = i2c_get_clientdata(client); \
249 unsigned long val = simple_strtoul(buf, NULL, 10); \
250 \
251 down(&data->update_lock); \
252 data->in_##reg[nr] = IN_TO_REG(val); \
253 asb100_write_value(client, ASB100_REG_IN_##REG(nr), \
254 data->in_##reg[nr]); \
255 up(&data->update_lock); \
256 return count; \
257}
258
259set_in_reg(MIN, min)
260set_in_reg(MAX, max)
261
262#define sysfs_in(offset) \
263static ssize_t \
Yani Ioannou30f74292005-05-17 06:41:35 -0400264 show_in##offset (struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{ \
266 return show_in(dev, buf, offset); \
267} \
268static DEVICE_ATTR(in##offset##_input, S_IRUGO, \
269 show_in##offset, NULL); \
270static ssize_t \
Yani Ioannou30f74292005-05-17 06:41:35 -0400271 show_in##offset##_min (struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{ \
273 return show_in_min(dev, buf, offset); \
274} \
275static ssize_t \
Yani Ioannou30f74292005-05-17 06:41:35 -0400276 show_in##offset##_max (struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{ \
278 return show_in_max(dev, buf, offset); \
279} \
Yani Ioannou30f74292005-05-17 06:41:35 -0400280static ssize_t set_in##offset##_min (struct device *dev, struct device_attribute *attr, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 const char *buf, size_t count) \
282{ \
283 return set_in_min(dev, buf, count, offset); \
284} \
Yani Ioannou30f74292005-05-17 06:41:35 -0400285static ssize_t set_in##offset##_max (struct device *dev, struct device_attribute *attr, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 const char *buf, size_t count) \
287{ \
288 return set_in_max(dev, buf, count, offset); \
289} \
290static DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
291 show_in##offset##_min, set_in##offset##_min); \
292static DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
293 show_in##offset##_max, set_in##offset##_max);
294
295sysfs_in(0);
296sysfs_in(1);
297sysfs_in(2);
298sysfs_in(3);
299sysfs_in(4);
300sysfs_in(5);
301sysfs_in(6);
302
303#define device_create_file_in(client, offset) do { \
304 device_create_file(&client->dev, &dev_attr_in##offset##_input); \
305 device_create_file(&client->dev, &dev_attr_in##offset##_min); \
306 device_create_file(&client->dev, &dev_attr_in##offset##_max); \
307} while (0)
308
309/* 3 Fans */
310static ssize_t show_fan(struct device *dev, char *buf, int nr)
311{
312 struct asb100_data *data = asb100_update_device(dev);
313 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
314 DIV_FROM_REG(data->fan_div[nr])));
315}
316
317static ssize_t show_fan_min(struct device *dev, char *buf, int nr)
318{
319 struct asb100_data *data = asb100_update_device(dev);
320 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
321 DIV_FROM_REG(data->fan_div[nr])));
322}
323
324static ssize_t show_fan_div(struct device *dev, char *buf, int nr)
325{
326 struct asb100_data *data = asb100_update_device(dev);
327 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
328}
329
330static ssize_t set_fan_min(struct device *dev, const char *buf,
331 size_t count, int nr)
332{
333 struct i2c_client *client = to_i2c_client(dev);
334 struct asb100_data *data = i2c_get_clientdata(client);
335 u32 val = simple_strtoul(buf, NULL, 10);
336
337 down(&data->update_lock);
338 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
339 asb100_write_value(client, ASB100_REG_FAN_MIN(nr), data->fan_min[nr]);
340 up(&data->update_lock);
341 return count;
342}
343
344/* Note: we save and restore the fan minimum here, because its value is
345 determined in part by the fan divisor. This follows the principle of
346 least suprise; the user doesn't expect the fan minimum to change just
347 because the divisor changed. */
348static ssize_t set_fan_div(struct device *dev, const char *buf,
349 size_t count, int nr)
350{
351 struct i2c_client *client = to_i2c_client(dev);
352 struct asb100_data *data = i2c_get_clientdata(client);
353 unsigned long min;
354 unsigned long val = simple_strtoul(buf, NULL, 10);
355 int reg;
356
357 down(&data->update_lock);
358
359 min = FAN_FROM_REG(data->fan_min[nr],
360 DIV_FROM_REG(data->fan_div[nr]));
361 data->fan_div[nr] = DIV_TO_REG(val);
362
363 switch(nr) {
364 case 0: /* fan 1 */
365 reg = asb100_read_value(client, ASB100_REG_VID_FANDIV);
366 reg = (reg & 0xcf) | (data->fan_div[0] << 4);
367 asb100_write_value(client, ASB100_REG_VID_FANDIV, reg);
368 break;
369
370 case 1: /* fan 2 */
371 reg = asb100_read_value(client, ASB100_REG_VID_FANDIV);
372 reg = (reg & 0x3f) | (data->fan_div[1] << 6);
373 asb100_write_value(client, ASB100_REG_VID_FANDIV, reg);
374 break;
375
376 case 2: /* fan 3 */
377 reg = asb100_read_value(client, ASB100_REG_PIN);
378 reg = (reg & 0x3f) | (data->fan_div[2] << 6);
379 asb100_write_value(client, ASB100_REG_PIN, reg);
380 break;
381 }
382
383 data->fan_min[nr] =
384 FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
385 asb100_write_value(client, ASB100_REG_FAN_MIN(nr), data->fan_min[nr]);
386
387 up(&data->update_lock);
388
389 return count;
390}
391
392#define sysfs_fan(offset) \
Yani Ioannou30f74292005-05-17 06:41:35 -0400393static ssize_t show_fan##offset(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{ \
395 return show_fan(dev, buf, offset - 1); \
396} \
Yani Ioannou30f74292005-05-17 06:41:35 -0400397static ssize_t show_fan##offset##_min(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{ \
399 return show_fan_min(dev, buf, offset - 1); \
400} \
Yani Ioannou30f74292005-05-17 06:41:35 -0400401static ssize_t show_fan##offset##_div(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{ \
403 return show_fan_div(dev, buf, offset - 1); \
404} \
Yani Ioannou30f74292005-05-17 06:41:35 -0400405static ssize_t set_fan##offset##_min(struct device *dev, struct device_attribute *attr, const char *buf, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 size_t count) \
407{ \
408 return set_fan_min(dev, buf, count, offset - 1); \
409} \
Yani Ioannou30f74292005-05-17 06:41:35 -0400410static ssize_t set_fan##offset##_div(struct device *dev, struct device_attribute *attr, const char *buf, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 size_t count) \
412{ \
413 return set_fan_div(dev, buf, count, offset - 1); \
414} \
415static DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
416 show_fan##offset, NULL); \
417static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
418 show_fan##offset##_min, set_fan##offset##_min); \
419static DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
420 show_fan##offset##_div, set_fan##offset##_div);
421
422sysfs_fan(1);
423sysfs_fan(2);
424sysfs_fan(3);
425
426#define device_create_file_fan(client, offset) do { \
427 device_create_file(&client->dev, &dev_attr_fan##offset##_input); \
428 device_create_file(&client->dev, &dev_attr_fan##offset##_min); \
429 device_create_file(&client->dev, &dev_attr_fan##offset##_div); \
430} while (0)
431
432/* 4 Temp. Sensors */
433static int sprintf_temp_from_reg(u16 reg, char *buf, int nr)
434{
435 int ret = 0;
436
437 switch (nr) {
438 case 1: case 2:
439 ret = sprintf(buf, "%d\n", LM75_TEMP_FROM_REG(reg));
440 break;
441 case 0: case 3: default:
442 ret = sprintf(buf, "%d\n", TEMP_FROM_REG(reg));
443 break;
444 }
445 return ret;
446}
447
448#define show_temp_reg(reg) \
449static ssize_t show_##reg(struct device *dev, char *buf, int nr) \
450{ \
451 struct asb100_data *data = asb100_update_device(dev); \
452 return sprintf_temp_from_reg(data->reg[nr], buf, nr); \
453}
454
455show_temp_reg(temp);
456show_temp_reg(temp_max);
457show_temp_reg(temp_hyst);
458
459#define set_temp_reg(REG, reg) \
460static ssize_t set_##reg(struct device *dev, const char *buf, \
461 size_t count, int nr) \
462{ \
463 struct i2c_client *client = to_i2c_client(dev); \
464 struct asb100_data *data = i2c_get_clientdata(client); \
465 unsigned long val = simple_strtoul(buf, NULL, 10); \
466 \
467 down(&data->update_lock); \
468 switch (nr) { \
469 case 1: case 2: \
470 data->reg[nr] = LM75_TEMP_TO_REG(val); \
471 break; \
472 case 0: case 3: default: \
473 data->reg[nr] = TEMP_TO_REG(val); \
474 break; \
475 } \
476 asb100_write_value(client, ASB100_REG_TEMP_##REG(nr+1), \
477 data->reg[nr]); \
478 up(&data->update_lock); \
479 return count; \
480}
481
482set_temp_reg(MAX, temp_max);
483set_temp_reg(HYST, temp_hyst);
484
485#define sysfs_temp(num) \
Yani Ioannou30f74292005-05-17 06:41:35 -0400486static ssize_t show_temp##num(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{ \
488 return show_temp(dev, buf, num-1); \
489} \
490static DEVICE_ATTR(temp##num##_input, S_IRUGO, show_temp##num, NULL); \
Yani Ioannou30f74292005-05-17 06:41:35 -0400491static ssize_t show_temp_max##num(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{ \
493 return show_temp_max(dev, buf, num-1); \
494} \
Yani Ioannou30f74292005-05-17 06:41:35 -0400495static ssize_t set_temp_max##num(struct device *dev, struct device_attribute *attr, const char *buf, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 size_t count) \
497{ \
498 return set_temp_max(dev, buf, count, num-1); \
499} \
500static DEVICE_ATTR(temp##num##_max, S_IRUGO | S_IWUSR, \
501 show_temp_max##num, set_temp_max##num); \
Yani Ioannou30f74292005-05-17 06:41:35 -0400502static ssize_t show_temp_hyst##num(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{ \
504 return show_temp_hyst(dev, buf, num-1); \
505} \
Yani Ioannou30f74292005-05-17 06:41:35 -0400506static ssize_t set_temp_hyst##num(struct device *dev, struct device_attribute *attr, const char *buf, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 size_t count) \
508{ \
509 return set_temp_hyst(dev, buf, count, num-1); \
510} \
511static DEVICE_ATTR(temp##num##_max_hyst, S_IRUGO | S_IWUSR, \
512 show_temp_hyst##num, set_temp_hyst##num);
513
514sysfs_temp(1);
515sysfs_temp(2);
516sysfs_temp(3);
517sysfs_temp(4);
518
519/* VID */
520#define device_create_file_temp(client, num) do { \
521 device_create_file(&client->dev, &dev_attr_temp##num##_input); \
522 device_create_file(&client->dev, &dev_attr_temp##num##_max); \
523 device_create_file(&client->dev, &dev_attr_temp##num##_max_hyst); \
524} while (0)
525
Yani Ioannou30f74292005-05-17 06:41:35 -0400526static ssize_t show_vid(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
528 struct asb100_data *data = asb100_update_device(dev);
529 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
530}
531
532static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
533#define device_create_file_vid(client) \
534device_create_file(&client->dev, &dev_attr_cpu0_vid)
535
536/* VRM */
Yani Ioannou30f74292005-05-17 06:41:35 -0400537static ssize_t show_vrm(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
539 struct asb100_data *data = asb100_update_device(dev);
540 return sprintf(buf, "%d\n", data->vrm);
541}
542
Yani Ioannou30f74292005-05-17 06:41:35 -0400543static ssize_t set_vrm(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
545 struct i2c_client *client = to_i2c_client(dev);
546 struct asb100_data *data = i2c_get_clientdata(client);
547 unsigned long val = simple_strtoul(buf, NULL, 10);
548 data->vrm = val;
549 return count;
550}
551
552/* Alarms */
553static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm);
554#define device_create_file_vrm(client) \
555device_create_file(&client->dev, &dev_attr_vrm);
556
Yani Ioannou30f74292005-05-17 06:41:35 -0400557static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{
559 struct asb100_data *data = asb100_update_device(dev);
560 return sprintf(buf, "%d\n", ALARMS_FROM_REG(data->alarms));
561}
562
563static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
564#define device_create_file_alarms(client) \
565device_create_file(&client->dev, &dev_attr_alarms)
566
567/* 1 PWM */
Yani Ioannou30f74292005-05-17 06:41:35 -0400568static ssize_t show_pwm1(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
570 struct asb100_data *data = asb100_update_device(dev);
571 return sprintf(buf, "%d\n", ASB100_PWM_FROM_REG(data->pwm & 0x0f));
572}
573
Yani Ioannou30f74292005-05-17 06:41:35 -0400574static ssize_t set_pwm1(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575{
576 struct i2c_client *client = to_i2c_client(dev);
577 struct asb100_data *data = i2c_get_clientdata(client);
578 unsigned long val = simple_strtoul(buf, NULL, 10);
579
580 down(&data->update_lock);
581 data->pwm &= 0x80; /* keep the enable bit */
582 data->pwm |= (0x0f & ASB100_PWM_TO_REG(val));
583 asb100_write_value(client, ASB100_REG_PWM1, data->pwm);
584 up(&data->update_lock);
585 return count;
586}
587
Yani Ioannou30f74292005-05-17 06:41:35 -0400588static ssize_t show_pwm_enable1(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
590 struct asb100_data *data = asb100_update_device(dev);
591 return sprintf(buf, "%d\n", (data->pwm & 0x80) ? 1 : 0);
592}
593
Yani Ioannou30f74292005-05-17 06:41:35 -0400594static ssize_t set_pwm_enable1(struct device *dev, struct device_attribute *attr, const char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 size_t count)
596{
597 struct i2c_client *client = to_i2c_client(dev);
598 struct asb100_data *data = i2c_get_clientdata(client);
599 unsigned long val = simple_strtoul(buf, NULL, 10);
600
601 down(&data->update_lock);
602 data->pwm &= 0x0f; /* keep the duty cycle bits */
603 data->pwm |= (val ? 0x80 : 0x00);
604 asb100_write_value(client, ASB100_REG_PWM1, data->pwm);
605 up(&data->update_lock);
606 return count;
607}
608
609static DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm1, set_pwm1);
610static DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
611 show_pwm_enable1, set_pwm_enable1);
612#define device_create_file_pwm1(client) do { \
613 device_create_file(&new_client->dev, &dev_attr_pwm1); \
614 device_create_file(&new_client->dev, &dev_attr_pwm1_enable); \
615} while (0)
616
617/* This function is called when:
618 asb100_driver is inserted (when this module is loaded), for each
619 available adapter
620 when a new adapter is inserted (and asb100_driver is still present)
621 */
622static int asb100_attach_adapter(struct i2c_adapter *adapter)
623{
624 if (!(adapter->class & I2C_CLASS_HWMON))
625 return 0;
626 return i2c_detect(adapter, &addr_data, asb100_detect);
627}
628
629static int asb100_detect_subclients(struct i2c_adapter *adapter, int address,
630 int kind, struct i2c_client *new_client)
631{
632 int i, id, err;
633 struct asb100_data *data = i2c_get_clientdata(new_client);
634
635 data->lm75[0] = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
636 if (!(data->lm75[0])) {
637 err = -ENOMEM;
638 goto ERROR_SC_0;
639 }
640 memset(data->lm75[0], 0x00, sizeof(struct i2c_client));
641
642 data->lm75[1] = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
643 if (!(data->lm75[1])) {
644 err = -ENOMEM;
645 goto ERROR_SC_1;
646 }
647 memset(data->lm75[1], 0x00, sizeof(struct i2c_client));
648
649 id = i2c_adapter_id(adapter);
650
651 if (force_subclients[0] == id && force_subclients[1] == address) {
652 for (i = 2; i <= 3; i++) {
653 if (force_subclients[i] < 0x48 ||
654 force_subclients[i] > 0x4f) {
655 dev_err(&new_client->dev, "invalid subclient "
656 "address %d; must be 0x48-0x4f\n",
657 force_subclients[i]);
658 err = -ENODEV;
659 goto ERROR_SC_2;
660 }
661 }
662 asb100_write_value(new_client, ASB100_REG_I2C_SUBADDR,
663 (force_subclients[2] & 0x07) |
664 ((force_subclients[3] & 0x07) <<4));
665 data->lm75[0]->addr = force_subclients[2];
666 data->lm75[1]->addr = force_subclients[3];
667 } else {
668 int val = asb100_read_value(new_client, ASB100_REG_I2C_SUBADDR);
669 data->lm75[0]->addr = 0x48 + (val & 0x07);
670 data->lm75[1]->addr = 0x48 + ((val >> 4) & 0x07);
671 }
672
673 if(data->lm75[0]->addr == data->lm75[1]->addr) {
674 dev_err(&new_client->dev, "duplicate addresses 0x%x "
675 "for subclients\n", data->lm75[0]->addr);
676 err = -ENODEV;
677 goto ERROR_SC_2;
678 }
679
680 for (i = 0; i <= 1; i++) {
681 i2c_set_clientdata(data->lm75[i], NULL);
682 data->lm75[i]->adapter = adapter;
683 data->lm75[i]->driver = &asb100_driver;
684 data->lm75[i]->flags = 0;
685 strlcpy(data->lm75[i]->name, "asb100 subclient", I2C_NAME_SIZE);
686 }
687
688 if ((err = i2c_attach_client(data->lm75[0]))) {
689 dev_err(&new_client->dev, "subclient %d registration "
690 "at address 0x%x failed.\n", i, data->lm75[0]->addr);
691 goto ERROR_SC_2;
692 }
693
694 if ((err = i2c_attach_client(data->lm75[1]))) {
695 dev_err(&new_client->dev, "subclient %d registration "
696 "at address 0x%x failed.\n", i, data->lm75[1]->addr);
697 goto ERROR_SC_3;
698 }
699
700 return 0;
701
702/* Undo inits in case of errors */
703ERROR_SC_3:
704 i2c_detach_client(data->lm75[0]);
705ERROR_SC_2:
706 kfree(data->lm75[1]);
707ERROR_SC_1:
708 kfree(data->lm75[0]);
709ERROR_SC_0:
710 return err;
711}
712
713static int asb100_detect(struct i2c_adapter *adapter, int address, int kind)
714{
715 int err;
716 struct i2c_client *new_client;
717 struct asb100_data *data;
718
719 /* asb100 is SMBus only */
720 if (i2c_is_isa_adapter(adapter)) {
721 pr_debug("asb100.o: detect failed, "
722 "cannot attach to legacy adapter!\n");
723 err = -ENODEV;
724 goto ERROR0;
725 }
726
727 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
728 pr_debug("asb100.o: detect failed, "
729 "smbus byte data not supported!\n");
730 err = -ENODEV;
731 goto ERROR0;
732 }
733
734 /* OK. For now, we presume we have a valid client. We now create the
735 client structure, even though we cannot fill it completely yet.
736 But it allows us to access asb100_{read,write}_value. */
737
738 if (!(data = kmalloc(sizeof(struct asb100_data), GFP_KERNEL))) {
739 pr_debug("asb100.o: detect failed, kmalloc failed!\n");
740 err = -ENOMEM;
741 goto ERROR0;
742 }
743 memset(data, 0, sizeof(struct asb100_data));
744
745 new_client = &data->client;
746 init_MUTEX(&data->lock);
747 i2c_set_clientdata(new_client, data);
748 new_client->addr = address;
749 new_client->adapter = adapter;
750 new_client->driver = &asb100_driver;
751 new_client->flags = 0;
752
753 /* Now, we do the remaining detection. */
754
755 /* The chip may be stuck in some other bank than bank 0. This may
756 make reading other information impossible. Specify a force=... or
757 force_*=... parameter, and the chip will be reset to the right
758 bank. */
759 if (kind < 0) {
760
761 int val1 = asb100_read_value(new_client, ASB100_REG_BANK);
762 int val2 = asb100_read_value(new_client, ASB100_REG_CHIPMAN);
763
764 /* If we're in bank 0 */
765 if ( (!(val1 & 0x07)) &&
766 /* Check for ASB100 ID (low byte) */
767 ( ((!(val1 & 0x80)) && (val2 != 0x94)) ||
768 /* Check for ASB100 ID (high byte ) */
769 ((val1 & 0x80) && (val2 != 0x06)) ) ) {
770 pr_debug("asb100.o: detect failed, "
771 "bad chip id 0x%02x!\n", val2);
772 err = -ENODEV;
773 goto ERROR1;
774 }
775
776 } /* kind < 0 */
777
778 /* We have either had a force parameter, or we have already detected
779 Winbond. Put it now into bank 0 and Vendor ID High Byte */
780 asb100_write_value(new_client, ASB100_REG_BANK,
781 (asb100_read_value(new_client, ASB100_REG_BANK) & 0x78) | 0x80);
782
783 /* Determine the chip type. */
784 if (kind <= 0) {
785 int val1 = asb100_read_value(new_client, ASB100_REG_WCHIPID);
786 int val2 = asb100_read_value(new_client, ASB100_REG_CHIPMAN);
787
788 if ((val1 == 0x31) && (val2 == 0x06))
789 kind = asb100;
790 else {
791 if (kind == 0)
792 dev_warn(&new_client->dev, "ignoring "
793 "'force' parameter for unknown chip "
794 "at adapter %d, address 0x%02x.\n",
795 i2c_adapter_id(adapter), address);
796 err = -ENODEV;
797 goto ERROR1;
798 }
799 }
800
801 /* Fill in remaining client fields and put it into the global list */
802 strlcpy(new_client->name, "asb100", I2C_NAME_SIZE);
803 data->type = kind;
804
805 data->valid = 0;
806 init_MUTEX(&data->update_lock);
807
808 /* Tell the I2C layer a new client has arrived */
809 if ((err = i2c_attach_client(new_client)))
810 goto ERROR1;
811
812 /* Attach secondary lm75 clients */
813 if ((err = asb100_detect_subclients(adapter, address, kind,
814 new_client)))
815 goto ERROR2;
816
817 /* Initialize the chip */
818 asb100_init_client(new_client);
819
820 /* A few vars need to be filled upon startup */
821 data->fan_min[0] = asb100_read_value(new_client, ASB100_REG_FAN_MIN(0));
822 data->fan_min[1] = asb100_read_value(new_client, ASB100_REG_FAN_MIN(1));
823 data->fan_min[2] = asb100_read_value(new_client, ASB100_REG_FAN_MIN(2));
824
825 /* Register sysfs hooks */
826 device_create_file_in(new_client, 0);
827 device_create_file_in(new_client, 1);
828 device_create_file_in(new_client, 2);
829 device_create_file_in(new_client, 3);
830 device_create_file_in(new_client, 4);
831 device_create_file_in(new_client, 5);
832 device_create_file_in(new_client, 6);
833
834 device_create_file_fan(new_client, 1);
835 device_create_file_fan(new_client, 2);
836 device_create_file_fan(new_client, 3);
837
838 device_create_file_temp(new_client, 1);
839 device_create_file_temp(new_client, 2);
840 device_create_file_temp(new_client, 3);
841 device_create_file_temp(new_client, 4);
842
843 device_create_file_vid(new_client);
844 device_create_file_vrm(new_client);
845
846 device_create_file_alarms(new_client);
847
848 device_create_file_pwm1(new_client);
849
850 return 0;
851
852ERROR2:
853 i2c_detach_client(new_client);
854ERROR1:
855 kfree(data);
856ERROR0:
857 return err;
858}
859
860static int asb100_detach_client(struct i2c_client *client)
861{
862 int err;
863
864 if ((err = i2c_detach_client(client))) {
865 dev_err(&client->dev, "client deregistration failed; "
866 "client not detached.\n");
867 return err;
868 }
869
870 if (i2c_get_clientdata(client)==NULL) {
871 /* subclients */
872 kfree(client);
873 } else {
874 /* main client */
875 kfree(i2c_get_clientdata(client));
876 }
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
889 down(&data->lock);
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
923 up(&data->lock);
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
934 down(&data->lock);
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
964 up(&data->lock);
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;
974 data->vrm = i2c_which_vrm();
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
988 down(&data->update_lock);
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
1046 up(&data->update_lock);
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