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