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