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