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