Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | lm78.c - Part of lm_sensors, Linux kernel modules for hardware |
| 3 | monitoring |
| 4 | Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl> |
| 5 | |
| 6 | This program is free software; you can redistribute it and/or modify |
| 7 | it under the terms of the GNU General Public License as published by |
| 8 | the Free Software Foundation; either version 2 of the License, or |
| 9 | (at your option) any later version. |
| 10 | |
| 11 | This program is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | GNU General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with this program; if not, write to the Free Software |
| 18 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 19 | */ |
| 20 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <linux/module.h> |
| 22 | #include <linux/init.h> |
| 23 | #include <linux/slab.h> |
| 24 | #include <linux/jiffies.h> |
| 25 | #include <linux/i2c.h> |
Jean Delvare | fde0950 | 2005-07-19 23:51:07 +0200 | [diff] [blame] | 26 | #include <linux/i2c-isa.h> |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 27 | #include <linux/hwmon.h> |
Jean Delvare | 19f673e | 2005-07-31 22:12:09 +0200 | [diff] [blame] | 28 | #include <linux/hwmon-vid.h> |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 29 | #include <linux/err.h> |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 30 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include <asm/io.h> |
| 32 | |
| 33 | /* Addresses to scan */ |
| 34 | static unsigned short normal_i2c[] = { 0x20, 0x21, 0x22, 0x23, 0x24, |
| 35 | 0x25, 0x26, 0x27, 0x28, 0x29, |
| 36 | 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, |
| 37 | 0x2f, I2C_CLIENT_END }; |
Jean Delvare | 2d8672c | 2005-07-19 23:56:35 +0200 | [diff] [blame] | 38 | static unsigned short isa_address = 0x290; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
| 40 | /* Insmod parameters */ |
Jean Delvare | f4b5026 | 2005-07-31 21:49:03 +0200 | [diff] [blame] | 41 | I2C_CLIENT_INSMOD_2(lm78, lm79); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
| 43 | /* Many LM78 constants specified below */ |
| 44 | |
| 45 | /* Length of ISA address segment */ |
| 46 | #define LM78_EXTENT 8 |
| 47 | |
| 48 | /* Where are the ISA address/data registers relative to the base address */ |
| 49 | #define LM78_ADDR_REG_OFFSET 5 |
| 50 | #define LM78_DATA_REG_OFFSET 6 |
| 51 | |
| 52 | /* The LM78 registers */ |
| 53 | #define LM78_REG_IN_MAX(nr) (0x2b + (nr) * 2) |
| 54 | #define LM78_REG_IN_MIN(nr) (0x2c + (nr) * 2) |
| 55 | #define LM78_REG_IN(nr) (0x20 + (nr)) |
| 56 | |
| 57 | #define LM78_REG_FAN_MIN(nr) (0x3b + (nr)) |
| 58 | #define LM78_REG_FAN(nr) (0x28 + (nr)) |
| 59 | |
| 60 | #define LM78_REG_TEMP 0x27 |
| 61 | #define LM78_REG_TEMP_OVER 0x39 |
| 62 | #define LM78_REG_TEMP_HYST 0x3a |
| 63 | |
| 64 | #define LM78_REG_ALARM1 0x41 |
| 65 | #define LM78_REG_ALARM2 0x42 |
| 66 | |
| 67 | #define LM78_REG_VID_FANDIV 0x47 |
| 68 | |
| 69 | #define LM78_REG_CONFIG 0x40 |
| 70 | #define LM78_REG_CHIPID 0x49 |
| 71 | #define LM78_REG_I2C_ADDR 0x48 |
| 72 | |
| 73 | |
| 74 | /* Conversions. Rounding and limit checking is only done on the TO_REG |
| 75 | variants. */ |
| 76 | |
| 77 | /* IN: mV, (0V to 4.08V) |
| 78 | REG: 16mV/bit */ |
| 79 | static inline u8 IN_TO_REG(unsigned long val) |
| 80 | { |
| 81 | unsigned long nval = SENSORS_LIMIT(val, 0, 4080); |
| 82 | return (nval + 8) / 16; |
| 83 | } |
| 84 | #define IN_FROM_REG(val) ((val) * 16) |
| 85 | |
| 86 | static inline u8 FAN_TO_REG(long rpm, int div) |
| 87 | { |
| 88 | if (rpm <= 0) |
| 89 | return 255; |
| 90 | return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1, 254); |
| 91 | } |
| 92 | |
| 93 | static inline int FAN_FROM_REG(u8 val, int div) |
| 94 | { |
| 95 | return val==0 ? -1 : val==255 ? 0 : 1350000/(val*div); |
| 96 | } |
| 97 | |
| 98 | /* TEMP: mC (-128C to +127C) |
| 99 | REG: 1C/bit, two's complement */ |
| 100 | static inline s8 TEMP_TO_REG(int val) |
| 101 | { |
| 102 | int nval = SENSORS_LIMIT(val, -128000, 127000) ; |
| 103 | return nval<0 ? (nval-500)/1000 : (nval+500)/1000; |
| 104 | } |
| 105 | |
| 106 | static inline int TEMP_FROM_REG(s8 val) |
| 107 | { |
| 108 | return val * 1000; |
| 109 | } |
| 110 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | #define DIV_FROM_REG(val) (1 << (val)) |
| 112 | |
| 113 | /* There are some complications in a module like this. First off, LM78 chips |
| 114 | may be both present on the SMBus and the ISA bus, and we have to handle |
| 115 | those cases separately at some places. Second, there might be several |
| 116 | LM78 chips available (well, actually, that is probably never done; but |
| 117 | it is a clean illustration of how to handle a case like that). Finally, |
| 118 | a specific chip may be attached to *both* ISA and SMBus, and we would |
| 119 | not like to detect it double. Fortunately, in the case of the LM78 at |
| 120 | least, a register tells us what SMBus address we are on, so that helps |
| 121 | a bit - except if there could be more than one SMBus. Groan. No solution |
| 122 | for this yet. */ |
| 123 | |
| 124 | /* This module may seem overly long and complicated. In fact, it is not so |
| 125 | bad. Quite a lot of bookkeeping is done. A real driver can often cut |
| 126 | some corners. */ |
| 127 | |
| 128 | /* For each registered LM78, we need to keep some data in memory. That |
| 129 | data is pointed to by lm78_list[NR]->data. The structure itself is |
| 130 | dynamically allocated, at the same time when a new lm78 client is |
| 131 | allocated. */ |
| 132 | struct lm78_data { |
| 133 | struct i2c_client client; |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 134 | struct class_device *class_dev; |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 135 | struct mutex lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | enum chips type; |
| 137 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 138 | struct mutex update_lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | char valid; /* !=0 if following fields are valid */ |
| 140 | unsigned long last_updated; /* In jiffies */ |
| 141 | |
| 142 | u8 in[7]; /* Register value */ |
| 143 | u8 in_max[7]; /* Register value */ |
| 144 | u8 in_min[7]; /* Register value */ |
| 145 | u8 fan[3]; /* Register value */ |
| 146 | u8 fan_min[3]; /* Register value */ |
| 147 | s8 temp; /* Register value */ |
| 148 | s8 temp_over; /* Register value */ |
| 149 | s8 temp_hyst; /* Register value */ |
| 150 | u8 fan_div[3]; /* Register encoding, shifted right */ |
| 151 | u8 vid; /* Register encoding, combined */ |
| 152 | u16 alarms; /* Register encoding, combined */ |
| 153 | }; |
| 154 | |
| 155 | |
| 156 | static int lm78_attach_adapter(struct i2c_adapter *adapter); |
Jean Delvare | 2d8672c | 2005-07-19 23:56:35 +0200 | [diff] [blame] | 157 | static int lm78_isa_attach_adapter(struct i2c_adapter *adapter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | static int lm78_detect(struct i2c_adapter *adapter, int address, int kind); |
| 159 | static int lm78_detach_client(struct i2c_client *client); |
| 160 | |
Darren Jenkins | f6c27fc | 2006-02-27 23:14:58 +0100 | [diff] [blame^] | 161 | static int lm78_read_value(struct i2c_client *client, u8 reg); |
| 162 | static int lm78_write_value(struct i2c_client *client, u8 reg, u8 value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | static struct lm78_data *lm78_update_device(struct device *dev); |
| 164 | static void lm78_init_client(struct i2c_client *client); |
| 165 | |
| 166 | |
| 167 | static struct i2c_driver lm78_driver = { |
Laurent Riffard | cdaf793 | 2005-11-26 20:37:41 +0100 | [diff] [blame] | 168 | .driver = { |
Laurent Riffard | cdaf793 | 2005-11-26 20:37:41 +0100 | [diff] [blame] | 169 | .name = "lm78", |
| 170 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | .id = I2C_DRIVERID_LM78, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | .attach_adapter = lm78_attach_adapter, |
| 173 | .detach_client = lm78_detach_client, |
| 174 | }; |
| 175 | |
Jean Delvare | fde0950 | 2005-07-19 23:51:07 +0200 | [diff] [blame] | 176 | static struct i2c_driver lm78_isa_driver = { |
Laurent Riffard | cdaf793 | 2005-11-26 20:37:41 +0100 | [diff] [blame] | 177 | .driver = { |
Laurent Riffard | cdaf793 | 2005-11-26 20:37:41 +0100 | [diff] [blame] | 178 | .name = "lm78-isa", |
| 179 | }, |
Jean Delvare | 2d8672c | 2005-07-19 23:56:35 +0200 | [diff] [blame] | 180 | .attach_adapter = lm78_isa_attach_adapter, |
Jean Delvare | fde0950 | 2005-07-19 23:51:07 +0200 | [diff] [blame] | 181 | .detach_client = lm78_detach_client, |
| 182 | }; |
| 183 | |
| 184 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | /* 7 Voltages */ |
| 186 | static ssize_t show_in(struct device *dev, char *buf, int nr) |
| 187 | { |
| 188 | struct lm78_data *data = lm78_update_device(dev); |
| 189 | return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr])); |
| 190 | } |
| 191 | |
| 192 | static ssize_t show_in_min(struct device *dev, char *buf, int nr) |
| 193 | { |
| 194 | struct lm78_data *data = lm78_update_device(dev); |
| 195 | return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr])); |
| 196 | } |
| 197 | |
| 198 | static ssize_t show_in_max(struct device *dev, char *buf, int nr) |
| 199 | { |
| 200 | struct lm78_data *data = lm78_update_device(dev); |
| 201 | return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr])); |
| 202 | } |
| 203 | |
| 204 | static ssize_t set_in_min(struct device *dev, const char *buf, |
| 205 | size_t count, int nr) |
| 206 | { |
| 207 | struct i2c_client *client = to_i2c_client(dev); |
| 208 | struct lm78_data *data = i2c_get_clientdata(client); |
| 209 | unsigned long val = simple_strtoul(buf, NULL, 10); |
| 210 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 211 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | data->in_min[nr] = IN_TO_REG(val); |
| 213 | lm78_write_value(client, LM78_REG_IN_MIN(nr), data->in_min[nr]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 214 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | return count; |
| 216 | } |
| 217 | |
| 218 | static ssize_t set_in_max(struct device *dev, const char *buf, |
| 219 | size_t count, int nr) |
| 220 | { |
| 221 | struct i2c_client *client = to_i2c_client(dev); |
| 222 | struct lm78_data *data = i2c_get_clientdata(client); |
| 223 | unsigned long val = simple_strtoul(buf, NULL, 10); |
| 224 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 225 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | data->in_max[nr] = IN_TO_REG(val); |
| 227 | lm78_write_value(client, LM78_REG_IN_MAX(nr), data->in_max[nr]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 228 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | return count; |
| 230 | } |
| 231 | |
| 232 | #define show_in_offset(offset) \ |
| 233 | static ssize_t \ |
Yani Ioannou | 8627f9b | 2005-05-17 06:42:03 -0400 | [diff] [blame] | 234 | show_in##offset (struct device *dev, struct device_attribute *attr, char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | { \ |
| 236 | return show_in(dev, buf, offset); \ |
| 237 | } \ |
| 238 | static DEVICE_ATTR(in##offset##_input, S_IRUGO, \ |
| 239 | show_in##offset, NULL); \ |
| 240 | static ssize_t \ |
Yani Ioannou | 8627f9b | 2005-05-17 06:42:03 -0400 | [diff] [blame] | 241 | 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] | 242 | { \ |
| 243 | return show_in_min(dev, buf, offset); \ |
| 244 | } \ |
| 245 | static ssize_t \ |
Yani Ioannou | 8627f9b | 2005-05-17 06:42:03 -0400 | [diff] [blame] | 246 | 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] | 247 | { \ |
| 248 | return show_in_max(dev, buf, offset); \ |
| 249 | } \ |
Yani Ioannou | 8627f9b | 2005-05-17 06:42:03 -0400 | [diff] [blame] | 250 | 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] | 251 | const char *buf, size_t count) \ |
| 252 | { \ |
| 253 | return set_in_min(dev, buf, count, offset); \ |
| 254 | } \ |
Yani Ioannou | 8627f9b | 2005-05-17 06:42:03 -0400 | [diff] [blame] | 255 | 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] | 256 | const char *buf, size_t count) \ |
| 257 | { \ |
| 258 | return set_in_max(dev, buf, count, offset); \ |
| 259 | } \ |
| 260 | static DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \ |
| 261 | show_in##offset##_min, set_in##offset##_min); \ |
| 262 | static DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \ |
| 263 | show_in##offset##_max, set_in##offset##_max); |
| 264 | |
| 265 | show_in_offset(0); |
| 266 | show_in_offset(1); |
| 267 | show_in_offset(2); |
| 268 | show_in_offset(3); |
| 269 | show_in_offset(4); |
| 270 | show_in_offset(5); |
| 271 | show_in_offset(6); |
| 272 | |
| 273 | /* Temperature */ |
Yani Ioannou | 8627f9b | 2005-05-17 06:42:03 -0400 | [diff] [blame] | 274 | static ssize_t show_temp(struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | { |
| 276 | struct lm78_data *data = lm78_update_device(dev); |
| 277 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp)); |
| 278 | } |
| 279 | |
Yani Ioannou | 8627f9b | 2005-05-17 06:42:03 -0400 | [diff] [blame] | 280 | static ssize_t show_temp_over(struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | { |
| 282 | struct lm78_data *data = lm78_update_device(dev); |
| 283 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_over)); |
| 284 | } |
| 285 | |
Yani Ioannou | 8627f9b | 2005-05-17 06:42:03 -0400 | [diff] [blame] | 286 | static ssize_t set_temp_over(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] | 287 | { |
| 288 | struct i2c_client *client = to_i2c_client(dev); |
| 289 | struct lm78_data *data = i2c_get_clientdata(client); |
| 290 | long val = simple_strtol(buf, NULL, 10); |
| 291 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 292 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | data->temp_over = TEMP_TO_REG(val); |
| 294 | lm78_write_value(client, LM78_REG_TEMP_OVER, data->temp_over); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 295 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | return count; |
| 297 | } |
| 298 | |
Yani Ioannou | 8627f9b | 2005-05-17 06:42:03 -0400 | [diff] [blame] | 299 | static ssize_t show_temp_hyst(struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | { |
| 301 | struct lm78_data *data = lm78_update_device(dev); |
| 302 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_hyst)); |
| 303 | } |
| 304 | |
Yani Ioannou | 8627f9b | 2005-05-17 06:42:03 -0400 | [diff] [blame] | 305 | static ssize_t set_temp_hyst(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] | 306 | { |
| 307 | struct i2c_client *client = to_i2c_client(dev); |
| 308 | struct lm78_data *data = i2c_get_clientdata(client); |
| 309 | long val = simple_strtol(buf, NULL, 10); |
| 310 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 311 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | data->temp_hyst = TEMP_TO_REG(val); |
| 313 | lm78_write_value(client, LM78_REG_TEMP_HYST, data->temp_hyst); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 314 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | return count; |
| 316 | } |
| 317 | |
| 318 | static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL); |
| 319 | static DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR, |
| 320 | show_temp_over, set_temp_over); |
| 321 | static DEVICE_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR, |
| 322 | show_temp_hyst, set_temp_hyst); |
| 323 | |
| 324 | /* 3 Fans */ |
| 325 | static ssize_t show_fan(struct device *dev, char *buf, int nr) |
| 326 | { |
| 327 | struct lm78_data *data = lm78_update_device(dev); |
| 328 | return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr], |
| 329 | DIV_FROM_REG(data->fan_div[nr])) ); |
| 330 | } |
| 331 | |
| 332 | static ssize_t show_fan_min(struct device *dev, char *buf, int nr) |
| 333 | { |
| 334 | struct lm78_data *data = lm78_update_device(dev); |
| 335 | return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan_min[nr], |
| 336 | DIV_FROM_REG(data->fan_div[nr])) ); |
| 337 | } |
| 338 | |
| 339 | static ssize_t set_fan_min(struct device *dev, const char *buf, |
| 340 | size_t count, int nr) |
| 341 | { |
| 342 | struct i2c_client *client = to_i2c_client(dev); |
| 343 | struct lm78_data *data = i2c_get_clientdata(client); |
| 344 | unsigned long val = simple_strtoul(buf, NULL, 10); |
| 345 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 346 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr])); |
| 348 | lm78_write_value(client, LM78_REG_FAN_MIN(nr), data->fan_min[nr]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 349 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | return count; |
| 351 | } |
| 352 | |
| 353 | static ssize_t show_fan_div(struct device *dev, char *buf, int nr) |
| 354 | { |
| 355 | struct lm78_data *data = lm78_update_device(dev); |
| 356 | return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]) ); |
| 357 | } |
| 358 | |
| 359 | /* Note: we save and restore the fan minimum here, because its value is |
| 360 | determined in part by the fan divisor. This follows the principle of |
| 361 | least suprise; the user doesn't expect the fan minimum to change just |
| 362 | because the divisor changed. */ |
| 363 | static ssize_t set_fan_div(struct device *dev, const char *buf, |
| 364 | size_t count, int nr) |
| 365 | { |
| 366 | struct i2c_client *client = to_i2c_client(dev); |
| 367 | struct lm78_data *data = i2c_get_clientdata(client); |
| 368 | unsigned long val = simple_strtoul(buf, NULL, 10); |
| 369 | unsigned long min; |
| 370 | u8 reg; |
| 371 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 372 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | min = FAN_FROM_REG(data->fan_min[nr], |
| 374 | DIV_FROM_REG(data->fan_div[nr])); |
| 375 | |
| 376 | switch (val) { |
| 377 | case 1: data->fan_div[nr] = 0; break; |
| 378 | case 2: data->fan_div[nr] = 1; break; |
| 379 | case 4: data->fan_div[nr] = 2; break; |
| 380 | case 8: data->fan_div[nr] = 3; break; |
| 381 | default: |
| 382 | dev_err(&client->dev, "fan_div value %ld not " |
| 383 | "supported. Choose one of 1, 2, 4 or 8!\n", val); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 384 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | return -EINVAL; |
| 386 | } |
| 387 | |
| 388 | reg = lm78_read_value(client, LM78_REG_VID_FANDIV); |
| 389 | switch (nr) { |
| 390 | case 0: |
| 391 | reg = (reg & 0xcf) | (data->fan_div[nr] << 4); |
| 392 | break; |
| 393 | case 1: |
| 394 | reg = (reg & 0x3f) | (data->fan_div[nr] << 6); |
| 395 | break; |
| 396 | } |
| 397 | lm78_write_value(client, LM78_REG_VID_FANDIV, reg); |
| 398 | |
| 399 | data->fan_min[nr] = |
| 400 | FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr])); |
| 401 | lm78_write_value(client, LM78_REG_FAN_MIN(nr), data->fan_min[nr]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 402 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | |
| 404 | return count; |
| 405 | } |
| 406 | |
| 407 | #define show_fan_offset(offset) \ |
Yani Ioannou | 8627f9b | 2005-05-17 06:42:03 -0400 | [diff] [blame] | 408 | 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] | 409 | { \ |
| 410 | return show_fan(dev, buf, offset - 1); \ |
| 411 | } \ |
Yani Ioannou | 8627f9b | 2005-05-17 06:42:03 -0400 | [diff] [blame] | 412 | 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] | 413 | { \ |
| 414 | return show_fan_min(dev, buf, offset - 1); \ |
| 415 | } \ |
Yani Ioannou | 8627f9b | 2005-05-17 06:42:03 -0400 | [diff] [blame] | 416 | 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] | 417 | { \ |
| 418 | return show_fan_div(dev, buf, offset - 1); \ |
| 419 | } \ |
Yani Ioannou | 8627f9b | 2005-05-17 06:42:03 -0400 | [diff] [blame] | 420 | static ssize_t set_fan_##offset##_min (struct device *dev, struct device_attribute *attr, \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | const char *buf, size_t count) \ |
| 422 | { \ |
| 423 | return set_fan_min(dev, buf, count, offset - 1); \ |
| 424 | } \ |
| 425 | static DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_fan_##offset, NULL);\ |
| 426 | static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \ |
| 427 | show_fan_##offset##_min, set_fan_##offset##_min); |
| 428 | |
Yani Ioannou | 8627f9b | 2005-05-17 06:42:03 -0400 | [diff] [blame] | 429 | static ssize_t set_fan_1_div(struct device *dev, struct device_attribute *attr, const char *buf, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | size_t count) |
| 431 | { |
| 432 | return set_fan_div(dev, buf, count, 0) ; |
| 433 | } |
| 434 | |
Yani Ioannou | 8627f9b | 2005-05-17 06:42:03 -0400 | [diff] [blame] | 435 | static ssize_t set_fan_2_div(struct device *dev, struct device_attribute *attr, const char *buf, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | size_t count) |
| 437 | { |
| 438 | return set_fan_div(dev, buf, count, 1) ; |
| 439 | } |
| 440 | |
| 441 | show_fan_offset(1); |
| 442 | show_fan_offset(2); |
| 443 | show_fan_offset(3); |
| 444 | |
| 445 | /* Fan 3 divisor is locked in H/W */ |
| 446 | static DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR, |
| 447 | show_fan_1_div, set_fan_1_div); |
| 448 | static DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR, |
| 449 | show_fan_2_div, set_fan_2_div); |
| 450 | static DEVICE_ATTR(fan3_div, S_IRUGO, show_fan_3_div, NULL); |
| 451 | |
| 452 | /* VID */ |
Yani Ioannou | 8627f9b | 2005-05-17 06:42:03 -0400 | [diff] [blame] | 453 | 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] | 454 | { |
| 455 | struct lm78_data *data = lm78_update_device(dev); |
Jean Delvare | d0d3cd6 | 2005-11-23 15:44:26 -0800 | [diff] [blame] | 456 | return sprintf(buf, "%d\n", vid_from_reg(data->vid, 82)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | } |
| 458 | static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL); |
| 459 | |
| 460 | /* Alarms */ |
Yani Ioannou | 8627f9b | 2005-05-17 06:42:03 -0400 | [diff] [blame] | 461 | 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] | 462 | { |
| 463 | struct lm78_data *data = lm78_update_device(dev); |
| 464 | return sprintf(buf, "%u\n", data->alarms); |
| 465 | } |
| 466 | static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL); |
| 467 | |
| 468 | /* This function is called when: |
| 469 | * lm78_driver is inserted (when this module is loaded), for each |
| 470 | available adapter |
| 471 | * when a new adapter is inserted (and lm78_driver is still present) */ |
| 472 | static int lm78_attach_adapter(struct i2c_adapter *adapter) |
| 473 | { |
| 474 | if (!(adapter->class & I2C_CLASS_HWMON)) |
| 475 | return 0; |
Jean Delvare | 2ed2dc3 | 2005-07-31 21:42:02 +0200 | [diff] [blame] | 476 | return i2c_probe(adapter, &addr_data, lm78_detect); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | } |
| 478 | |
Jean Delvare | 2d8672c | 2005-07-19 23:56:35 +0200 | [diff] [blame] | 479 | static int lm78_isa_attach_adapter(struct i2c_adapter *adapter) |
| 480 | { |
| 481 | return lm78_detect(adapter, isa_address, -1); |
| 482 | } |
| 483 | |
Jean Delvare | 2ed2dc3 | 2005-07-31 21:42:02 +0200 | [diff] [blame] | 484 | /* This function is called by i2c_probe */ |
Ben Dooks | d8d2061 | 2005-10-26 21:05:46 +0200 | [diff] [blame] | 485 | static int lm78_detect(struct i2c_adapter *adapter, int address, int kind) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | { |
| 487 | int i, err; |
| 488 | struct i2c_client *new_client; |
| 489 | struct lm78_data *data; |
| 490 | const char *client_name = ""; |
| 491 | int is_isa = i2c_is_isa_adapter(adapter); |
| 492 | |
| 493 | if (!is_isa && |
| 494 | !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) { |
| 495 | err = -ENODEV; |
| 496 | goto ERROR0; |
| 497 | } |
| 498 | |
| 499 | /* Reserve the ISA region */ |
| 500 | if (is_isa) |
Jean Delvare | fde0950 | 2005-07-19 23:51:07 +0200 | [diff] [blame] | 501 | if (!request_region(address, LM78_EXTENT, |
Laurent Riffard | cdaf793 | 2005-11-26 20:37:41 +0100 | [diff] [blame] | 502 | lm78_isa_driver.driver.name)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | err = -EBUSY; |
| 504 | goto ERROR0; |
| 505 | } |
| 506 | |
| 507 | /* Probe whether there is anything available on this address. Already |
| 508 | done for SMBus clients */ |
| 509 | if (kind < 0) { |
| 510 | if (is_isa) { |
| 511 | |
| 512 | #define REALLY_SLOW_IO |
| 513 | /* We need the timeouts for at least some LM78-like |
| 514 | chips. But only if we read 'undefined' registers. */ |
| 515 | i = inb_p(address + 1); |
| 516 | if (inb_p(address + 2) != i) { |
| 517 | err = -ENODEV; |
| 518 | goto ERROR1; |
| 519 | } |
| 520 | if (inb_p(address + 3) != i) { |
| 521 | err = -ENODEV; |
| 522 | goto ERROR1; |
| 523 | } |
| 524 | if (inb_p(address + 7) != i) { |
| 525 | err = -ENODEV; |
| 526 | goto ERROR1; |
| 527 | } |
| 528 | #undef REALLY_SLOW_IO |
| 529 | |
| 530 | /* Let's just hope nothing breaks here */ |
| 531 | i = inb_p(address + 5) & 0x7f; |
| 532 | outb_p(~i & 0x7f, address + 5); |
| 533 | if ((inb_p(address + 5) & 0x7f) != (~i & 0x7f)) { |
| 534 | outb_p(i, address + 5); |
| 535 | err = -ENODEV; |
| 536 | goto ERROR1; |
| 537 | } |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | /* OK. For now, we presume we have a valid client. We now create the |
| 542 | client structure, even though we cannot fill it completely yet. |
| 543 | But it allows us to access lm78_{read,write}_value. */ |
| 544 | |
Deepak Saxena | ba9c2e8 | 2005-10-17 23:08:32 +0200 | [diff] [blame] | 545 | if (!(data = kzalloc(sizeof(struct lm78_data), GFP_KERNEL))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | err = -ENOMEM; |
| 547 | goto ERROR1; |
| 548 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | |
| 550 | new_client = &data->client; |
| 551 | if (is_isa) |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 552 | mutex_init(&data->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | i2c_set_clientdata(new_client, data); |
| 554 | new_client->addr = address; |
| 555 | new_client->adapter = adapter; |
Jean Delvare | fde0950 | 2005-07-19 23:51:07 +0200 | [diff] [blame] | 556 | new_client->driver = is_isa ? &lm78_isa_driver : &lm78_driver; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | new_client->flags = 0; |
| 558 | |
| 559 | /* Now, we do the remaining detection. */ |
| 560 | if (kind < 0) { |
| 561 | if (lm78_read_value(new_client, LM78_REG_CONFIG) & 0x80) { |
| 562 | err = -ENODEV; |
| 563 | goto ERROR2; |
| 564 | } |
| 565 | if (!is_isa && (lm78_read_value( |
| 566 | new_client, LM78_REG_I2C_ADDR) != address)) { |
| 567 | err = -ENODEV; |
| 568 | goto ERROR2; |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | /* Determine the chip type. */ |
| 573 | if (kind <= 0) { |
| 574 | i = lm78_read_value(new_client, LM78_REG_CHIPID); |
Jean Delvare | 27fe048 | 2005-07-27 21:30:16 +0200 | [diff] [blame] | 575 | if (i == 0x00 || i == 0x20 /* LM78 */ |
| 576 | || i == 0x40) /* LM78-J */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | kind = lm78; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | else if ((i & 0xfe) == 0xc0) |
| 579 | kind = lm79; |
| 580 | else { |
| 581 | if (kind == 0) |
| 582 | dev_warn(&adapter->dev, "Ignoring 'force' " |
| 583 | "parameter for unknown chip at " |
| 584 | "adapter %d, address 0x%02x\n", |
| 585 | i2c_adapter_id(adapter), address); |
| 586 | err = -ENODEV; |
| 587 | goto ERROR2; |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | if (kind == lm78) { |
| 592 | client_name = "lm78"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | } else if (kind == lm79) { |
| 594 | client_name = "lm79"; |
| 595 | } |
| 596 | |
| 597 | /* Fill in the remaining client fields and put into the global list */ |
| 598 | strlcpy(new_client->name, client_name, I2C_NAME_SIZE); |
| 599 | data->type = kind; |
| 600 | |
| 601 | data->valid = 0; |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 602 | mutex_init(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | |
| 604 | /* Tell the I2C layer a new client has arrived */ |
| 605 | if ((err = i2c_attach_client(new_client))) |
| 606 | goto ERROR2; |
| 607 | |
| 608 | /* Initialize the LM78 chip */ |
| 609 | lm78_init_client(new_client); |
| 610 | |
| 611 | /* A few vars need to be filled upon startup */ |
| 612 | for (i = 0; i < 3; i++) { |
| 613 | data->fan_min[i] = lm78_read_value(new_client, |
| 614 | LM78_REG_FAN_MIN(i)); |
| 615 | } |
| 616 | |
| 617 | /* Register sysfs hooks */ |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 618 | data->class_dev = hwmon_device_register(&new_client->dev); |
| 619 | if (IS_ERR(data->class_dev)) { |
| 620 | err = PTR_ERR(data->class_dev); |
| 621 | goto ERROR3; |
| 622 | } |
| 623 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | device_create_file(&new_client->dev, &dev_attr_in0_input); |
| 625 | device_create_file(&new_client->dev, &dev_attr_in0_min); |
| 626 | device_create_file(&new_client->dev, &dev_attr_in0_max); |
| 627 | device_create_file(&new_client->dev, &dev_attr_in1_input); |
| 628 | device_create_file(&new_client->dev, &dev_attr_in1_min); |
| 629 | device_create_file(&new_client->dev, &dev_attr_in1_max); |
| 630 | device_create_file(&new_client->dev, &dev_attr_in2_input); |
| 631 | device_create_file(&new_client->dev, &dev_attr_in2_min); |
| 632 | device_create_file(&new_client->dev, &dev_attr_in2_max); |
| 633 | device_create_file(&new_client->dev, &dev_attr_in3_input); |
| 634 | device_create_file(&new_client->dev, &dev_attr_in3_min); |
| 635 | device_create_file(&new_client->dev, &dev_attr_in3_max); |
| 636 | device_create_file(&new_client->dev, &dev_attr_in4_input); |
| 637 | device_create_file(&new_client->dev, &dev_attr_in4_min); |
| 638 | device_create_file(&new_client->dev, &dev_attr_in4_max); |
| 639 | device_create_file(&new_client->dev, &dev_attr_in5_input); |
| 640 | device_create_file(&new_client->dev, &dev_attr_in5_min); |
| 641 | device_create_file(&new_client->dev, &dev_attr_in5_max); |
| 642 | device_create_file(&new_client->dev, &dev_attr_in6_input); |
| 643 | device_create_file(&new_client->dev, &dev_attr_in6_min); |
| 644 | device_create_file(&new_client->dev, &dev_attr_in6_max); |
| 645 | device_create_file(&new_client->dev, &dev_attr_temp1_input); |
| 646 | device_create_file(&new_client->dev, &dev_attr_temp1_max); |
| 647 | device_create_file(&new_client->dev, &dev_attr_temp1_max_hyst); |
| 648 | device_create_file(&new_client->dev, &dev_attr_fan1_input); |
| 649 | device_create_file(&new_client->dev, &dev_attr_fan1_min); |
| 650 | device_create_file(&new_client->dev, &dev_attr_fan1_div); |
| 651 | device_create_file(&new_client->dev, &dev_attr_fan2_input); |
| 652 | device_create_file(&new_client->dev, &dev_attr_fan2_min); |
| 653 | device_create_file(&new_client->dev, &dev_attr_fan2_div); |
| 654 | device_create_file(&new_client->dev, &dev_attr_fan3_input); |
| 655 | device_create_file(&new_client->dev, &dev_attr_fan3_min); |
| 656 | device_create_file(&new_client->dev, &dev_attr_fan3_div); |
| 657 | device_create_file(&new_client->dev, &dev_attr_alarms); |
| 658 | device_create_file(&new_client->dev, &dev_attr_cpu0_vid); |
| 659 | |
| 660 | return 0; |
| 661 | |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 662 | ERROR3: |
| 663 | i2c_detach_client(new_client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | ERROR2: |
| 665 | kfree(data); |
| 666 | ERROR1: |
| 667 | if (is_isa) |
| 668 | release_region(address, LM78_EXTENT); |
| 669 | ERROR0: |
| 670 | return err; |
| 671 | } |
| 672 | |
| 673 | static int lm78_detach_client(struct i2c_client *client) |
| 674 | { |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 675 | struct lm78_data *data = i2c_get_clientdata(client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | int err; |
| 677 | |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 678 | hwmon_device_unregister(data->class_dev); |
| 679 | |
Jean Delvare | 7bef559 | 2005-07-27 22:14:49 +0200 | [diff] [blame] | 680 | if ((err = i2c_detach_client(client))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | |
| 683 | if(i2c_is_isa_client(client)) |
| 684 | release_region(client->addr, LM78_EXTENT); |
| 685 | |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 686 | kfree(data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | |
| 688 | return 0; |
| 689 | } |
| 690 | |
Steven Cole | 44bbe87 | 2005-05-03 18:21:25 -0600 | [diff] [blame] | 691 | /* The SMBus locks itself, but ISA access must be locked explicitly! |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | We don't want to lock the whole ISA bus, so we lock each client |
| 693 | separately. |
| 694 | We ignore the LM78 BUSY flag at this moment - it could lead to deadlocks, |
| 695 | would slow down the LM78 access and should not be necessary. */ |
| 696 | static int lm78_read_value(struct i2c_client *client, u8 reg) |
| 697 | { |
| 698 | int res; |
| 699 | if (i2c_is_isa_client(client)) { |
| 700 | struct lm78_data *data = i2c_get_clientdata(client); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 701 | mutex_lock(&data->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 | outb_p(reg, client->addr + LM78_ADDR_REG_OFFSET); |
| 703 | res = inb_p(client->addr + LM78_DATA_REG_OFFSET); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 704 | mutex_unlock(&data->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | return res; |
| 706 | } else |
| 707 | return i2c_smbus_read_byte_data(client, reg); |
| 708 | } |
| 709 | |
Steven Cole | 44bbe87 | 2005-05-03 18:21:25 -0600 | [diff] [blame] | 710 | /* The SMBus locks itself, but ISA access muse be locked explicitly! |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | We don't want to lock the whole ISA bus, so we lock each client |
| 712 | separately. |
| 713 | We ignore the LM78 BUSY flag at this moment - it could lead to deadlocks, |
| 714 | would slow down the LM78 access and should not be necessary. |
| 715 | There are some ugly typecasts here, but the good new is - they should |
| 716 | nowhere else be necessary! */ |
| 717 | static int lm78_write_value(struct i2c_client *client, u8 reg, u8 value) |
| 718 | { |
| 719 | if (i2c_is_isa_client(client)) { |
| 720 | struct lm78_data *data = i2c_get_clientdata(client); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 721 | mutex_lock(&data->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 722 | outb_p(reg, client->addr + LM78_ADDR_REG_OFFSET); |
| 723 | outb_p(value, client->addr + LM78_DATA_REG_OFFSET); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 724 | mutex_unlock(&data->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | return 0; |
| 726 | } else |
| 727 | return i2c_smbus_write_byte_data(client, reg, value); |
| 728 | } |
| 729 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | static void lm78_init_client(struct i2c_client *client) |
| 731 | { |
| 732 | u8 config = lm78_read_value(client, LM78_REG_CONFIG); |
| 733 | |
| 734 | /* Start monitoring */ |
| 735 | if (!(config & 0x01)) |
| 736 | lm78_write_value(client, LM78_REG_CONFIG, |
| 737 | (config & 0xf7) | 0x01); |
| 738 | } |
| 739 | |
| 740 | static struct lm78_data *lm78_update_device(struct device *dev) |
| 741 | { |
| 742 | struct i2c_client *client = to_i2c_client(dev); |
| 743 | struct lm78_data *data = i2c_get_clientdata(client); |
| 744 | int i; |
| 745 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 746 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | |
| 748 | if (time_after(jiffies, data->last_updated + HZ + HZ / 2) |
| 749 | || !data->valid) { |
| 750 | |
| 751 | dev_dbg(&client->dev, "Starting lm78 update\n"); |
| 752 | |
| 753 | for (i = 0; i <= 6; i++) { |
| 754 | data->in[i] = |
| 755 | lm78_read_value(client, LM78_REG_IN(i)); |
| 756 | data->in_min[i] = |
| 757 | lm78_read_value(client, LM78_REG_IN_MIN(i)); |
| 758 | data->in_max[i] = |
| 759 | lm78_read_value(client, LM78_REG_IN_MAX(i)); |
| 760 | } |
| 761 | for (i = 0; i < 3; i++) { |
| 762 | data->fan[i] = |
| 763 | lm78_read_value(client, LM78_REG_FAN(i)); |
| 764 | data->fan_min[i] = |
| 765 | lm78_read_value(client, LM78_REG_FAN_MIN(i)); |
| 766 | } |
| 767 | data->temp = lm78_read_value(client, LM78_REG_TEMP); |
| 768 | data->temp_over = |
| 769 | lm78_read_value(client, LM78_REG_TEMP_OVER); |
| 770 | data->temp_hyst = |
| 771 | lm78_read_value(client, LM78_REG_TEMP_HYST); |
| 772 | i = lm78_read_value(client, LM78_REG_VID_FANDIV); |
| 773 | data->vid = i & 0x0f; |
| 774 | if (data->type == lm79) |
| 775 | data->vid |= |
| 776 | (lm78_read_value(client, LM78_REG_CHIPID) & |
| 777 | 0x01) << 4; |
| 778 | else |
| 779 | data->vid |= 0x10; |
| 780 | data->fan_div[0] = (i >> 4) & 0x03; |
| 781 | data->fan_div[1] = i >> 6; |
| 782 | data->alarms = lm78_read_value(client, LM78_REG_ALARM1) + |
| 783 | (lm78_read_value(client, LM78_REG_ALARM2) << 8); |
| 784 | data->last_updated = jiffies; |
| 785 | data->valid = 1; |
| 786 | |
| 787 | data->fan_div[2] = 1; |
| 788 | } |
| 789 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 790 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | |
| 792 | return data; |
| 793 | } |
| 794 | |
| 795 | static int __init sm_lm78_init(void) |
| 796 | { |
Jean Delvare | fde0950 | 2005-07-19 23:51:07 +0200 | [diff] [blame] | 797 | int res; |
| 798 | |
| 799 | res = i2c_add_driver(&lm78_driver); |
| 800 | if (res) |
| 801 | return res; |
| 802 | |
| 803 | res = i2c_isa_add_driver(&lm78_isa_driver); |
| 804 | if (res) { |
| 805 | i2c_del_driver(&lm78_driver); |
| 806 | return res; |
| 807 | } |
| 808 | |
| 809 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | } |
| 811 | |
| 812 | static void __exit sm_lm78_exit(void) |
| 813 | { |
Jean Delvare | fde0950 | 2005-07-19 23:51:07 +0200 | [diff] [blame] | 814 | i2c_isa_del_driver(&lm78_isa_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | i2c_del_driver(&lm78_driver); |
| 816 | } |
| 817 | |
| 818 | |
| 819 | |
| 820 | MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>"); |
Jean Delvare | 27fe048 | 2005-07-27 21:30:16 +0200 | [diff] [blame] | 821 | MODULE_DESCRIPTION("LM78/LM79 driver"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | MODULE_LICENSE("GPL"); |
| 823 | |
| 824 | module_init(sm_lm78_init); |
| 825 | module_exit(sm_lm78_exit); |