| 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> | 
| Jean Delvare | c40769f | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 5 | Copyright (c) 2007        Jean Delvare <khali@linux-fr.org> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 |  | 
|  | 7 | This program is free software; you can redistribute it and/or modify | 
|  | 8 | it under the terms of the GNU General Public License as published by | 
|  | 9 | the Free Software Foundation; either version 2 of the License, or | 
|  | 10 | (at your option) any later version. | 
|  | 11 |  | 
|  | 12 | This program is distributed in the hope that it will be useful, | 
|  | 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|  | 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|  | 15 | GNU General Public License for more details. | 
|  | 16 |  | 
|  | 17 | You should have received a copy of the GNU General Public License | 
|  | 18 | along with this program; if not, write to the Free Software | 
|  | 19 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | 
|  | 20 | */ | 
|  | 21 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/module.h> | 
|  | 23 | #include <linux/init.h> | 
|  | 24 | #include <linux/slab.h> | 
|  | 25 | #include <linux/jiffies.h> | 
|  | 26 | #include <linux/i2c.h> | 
| Jean Delvare | c40769f | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 27 | #include <linux/platform_device.h> | 
|  | 28 | #include <linux/ioport.h> | 
| Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 29 | #include <linux/hwmon.h> | 
| Jean Delvare | 19f673e | 2005-07-31 22:12:09 +0200 | [diff] [blame] | 30 | #include <linux/hwmon-vid.h> | 
| Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 31 | #include <linux/hwmon-sysfs.h> | 
| Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 32 | #include <linux/err.h> | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 33 | #include <linux/mutex.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include <asm/io.h> | 
|  | 35 |  | 
| Jean Delvare | c40769f | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 36 | /* ISA device, if found */ | 
|  | 37 | static struct platform_device *pdev; | 
|  | 38 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | /* Addresses to scan */ | 
| Jean Delvare | 6722fea | 2007-10-07 12:25:46 +0200 | [diff] [blame] | 40 | static unsigned short normal_i2c[] = { 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, | 
|  | 41 | 0x2e, 0x2f, I2C_CLIENT_END }; | 
| Jean Delvare | 2d8672c | 2005-07-19 23:56:35 +0200 | [diff] [blame] | 42 | static unsigned short isa_address = 0x290; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 |  | 
|  | 44 | /* Insmod parameters */ | 
| Jean Delvare | f4b5026 | 2005-07-31 21:49:03 +0200 | [diff] [blame] | 45 | I2C_CLIENT_INSMOD_2(lm78, lm79); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 |  | 
|  | 47 | /* Many LM78 constants specified below */ | 
|  | 48 |  | 
|  | 49 | /* Length of ISA address segment */ | 
|  | 50 | #define LM78_EXTENT 8 | 
|  | 51 |  | 
|  | 52 | /* Where are the ISA address/data registers relative to the base address */ | 
|  | 53 | #define LM78_ADDR_REG_OFFSET 5 | 
|  | 54 | #define LM78_DATA_REG_OFFSET 6 | 
|  | 55 |  | 
|  | 56 | /* The LM78 registers */ | 
|  | 57 | #define LM78_REG_IN_MAX(nr) (0x2b + (nr) * 2) | 
|  | 58 | #define LM78_REG_IN_MIN(nr) (0x2c + (nr) * 2) | 
|  | 59 | #define LM78_REG_IN(nr) (0x20 + (nr)) | 
|  | 60 |  | 
|  | 61 | #define LM78_REG_FAN_MIN(nr) (0x3b + (nr)) | 
|  | 62 | #define LM78_REG_FAN(nr) (0x28 + (nr)) | 
|  | 63 |  | 
|  | 64 | #define LM78_REG_TEMP 0x27 | 
|  | 65 | #define LM78_REG_TEMP_OVER 0x39 | 
|  | 66 | #define LM78_REG_TEMP_HYST 0x3a | 
|  | 67 |  | 
|  | 68 | #define LM78_REG_ALARM1 0x41 | 
|  | 69 | #define LM78_REG_ALARM2 0x42 | 
|  | 70 |  | 
|  | 71 | #define LM78_REG_VID_FANDIV 0x47 | 
|  | 72 |  | 
|  | 73 | #define LM78_REG_CONFIG 0x40 | 
|  | 74 | #define LM78_REG_CHIPID 0x49 | 
|  | 75 | #define LM78_REG_I2C_ADDR 0x48 | 
|  | 76 |  | 
|  | 77 |  | 
|  | 78 | /* Conversions. Rounding and limit checking is only done on the TO_REG | 
|  | 79 | variants. */ | 
|  | 80 |  | 
|  | 81 | /* IN: mV, (0V to 4.08V) | 
|  | 82 | REG: 16mV/bit */ | 
|  | 83 | static inline u8 IN_TO_REG(unsigned long val) | 
|  | 84 | { | 
|  | 85 | unsigned long nval = SENSORS_LIMIT(val, 0, 4080); | 
|  | 86 | return (nval + 8) / 16; | 
|  | 87 | } | 
|  | 88 | #define IN_FROM_REG(val) ((val) *  16) | 
|  | 89 |  | 
|  | 90 | static inline u8 FAN_TO_REG(long rpm, int div) | 
|  | 91 | { | 
|  | 92 | if (rpm <= 0) | 
|  | 93 | return 255; | 
|  | 94 | return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1, 254); | 
|  | 95 | } | 
|  | 96 |  | 
|  | 97 | static inline int FAN_FROM_REG(u8 val, int div) | 
|  | 98 | { | 
|  | 99 | return val==0 ? -1 : val==255 ? 0 : 1350000/(val*div); | 
|  | 100 | } | 
|  | 101 |  | 
|  | 102 | /* TEMP: mC (-128C to +127C) | 
|  | 103 | REG: 1C/bit, two's complement */ | 
|  | 104 | static inline s8 TEMP_TO_REG(int val) | 
|  | 105 | { | 
|  | 106 | int nval = SENSORS_LIMIT(val, -128000, 127000) ; | 
|  | 107 | return nval<0 ? (nval-500)/1000 : (nval+500)/1000; | 
|  | 108 | } | 
|  | 109 |  | 
|  | 110 | static inline int TEMP_FROM_REG(s8 val) | 
|  | 111 | { | 
|  | 112 | return val * 1000; | 
|  | 113 | } | 
|  | 114 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | #define DIV_FROM_REG(val) (1 << (val)) | 
|  | 116 |  | 
|  | 117 | /* There are some complications in a module like this. First off, LM78 chips | 
|  | 118 | may be both present on the SMBus and the ISA bus, and we have to handle | 
|  | 119 | those cases separately at some places. Second, there might be several | 
|  | 120 | LM78 chips available (well, actually, that is probably never done; but | 
|  | 121 | it is a clean illustration of how to handle a case like that). Finally, | 
|  | 122 | a specific chip may be attached to *both* ISA and SMBus, and we would | 
|  | 123 | not like to detect it double. Fortunately, in the case of the LM78 at | 
|  | 124 | least, a register tells us what SMBus address we are on, so that helps | 
|  | 125 | a bit - except if there could be more than one SMBus. Groan. No solution | 
|  | 126 | for this yet. */ | 
|  | 127 |  | 
| Jean Delvare | c40769f | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 128 | /* For ISA chips, we abuse the i2c_client addr and name fields. We also use | 
|  | 129 | the driver field to differentiate between I2C and ISA chips. */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | struct lm78_data { | 
|  | 131 | struct i2c_client client; | 
| Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 132 | struct device *hwmon_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); | 
|  | 155 | static int lm78_detect(struct i2c_adapter *adapter, int address, int kind); | 
|  | 156 | static int lm78_detach_client(struct i2c_client *client); | 
|  | 157 |  | 
| Jean Delvare | c40769f | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 158 | static int __devinit lm78_isa_probe(struct platform_device *pdev); | 
|  | 159 | static int __devexit lm78_isa_remove(struct platform_device *pdev); | 
|  | 160 |  | 
| Jean Delvare | c59cc30 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 161 | static int lm78_read_value(struct lm78_data *data, u8 reg); | 
|  | 162 | static int lm78_write_value(struct lm78_data *data, 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); | 
| Jean Delvare | c59cc30 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 164 | static void lm78_init_device(struct lm78_data *data); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | .attach_adapter	= lm78_attach_adapter, | 
|  | 172 | .detach_client	= lm78_detach_client, | 
|  | 173 | }; | 
|  | 174 |  | 
| Jean Delvare | c40769f | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 175 | static struct platform_driver lm78_isa_driver = { | 
| Laurent Riffard | cdaf793 | 2005-11-26 20:37:41 +0100 | [diff] [blame] | 176 | .driver = { | 
| Jean Delvare | 8721884 | 2006-09-03 22:36:14 +0200 | [diff] [blame] | 177 | .owner	= THIS_MODULE, | 
| Jean Delvare | c40769f | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 178 | .name	= "lm78", | 
| Laurent Riffard | cdaf793 | 2005-11-26 20:37:41 +0100 | [diff] [blame] | 179 | }, | 
| Jean Delvare | c40769f | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 180 | .probe		= lm78_isa_probe, | 
|  | 181 | .remove		= lm78_isa_remove, | 
| Jean Delvare | fde0950 | 2005-07-19 23:51:07 +0200 | [diff] [blame] | 182 | }; | 
|  | 183 |  | 
|  | 184 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | /* 7 Voltages */ | 
| Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 186 | static ssize_t show_in(struct device *dev, struct device_attribute *da, | 
|  | 187 | char *buf) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | { | 
| Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 189 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | struct lm78_data *data = lm78_update_device(dev); | 
| Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 191 | return sprintf(buf, "%d\n", IN_FROM_REG(data->in[attr->index])); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | } | 
|  | 193 |  | 
| Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 194 | static ssize_t show_in_min(struct device *dev, struct device_attribute *da, | 
|  | 195 | char *buf) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | { | 
| Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 197 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | struct lm78_data *data = lm78_update_device(dev); | 
| Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 199 | return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[attr->index])); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | } | 
|  | 201 |  | 
| Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 202 | static ssize_t show_in_max(struct device *dev, struct device_attribute *da, | 
|  | 203 | char *buf) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | { | 
| Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 205 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | struct lm78_data *data = lm78_update_device(dev); | 
| Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 207 | return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[attr->index])); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | } | 
|  | 209 |  | 
| Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 210 | static ssize_t set_in_min(struct device *dev, struct device_attribute *da, | 
|  | 211 | const char *buf, size_t count) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | { | 
| Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 213 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); | 
| Jean Delvare | c40769f | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 214 | struct lm78_data *data = dev_get_drvdata(dev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | unsigned long val = simple_strtoul(buf, NULL, 10); | 
| Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 216 | int nr = attr->index; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 |  | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 218 | mutex_lock(&data->update_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | data->in_min[nr] = IN_TO_REG(val); | 
| Jean Delvare | c59cc30 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 220 | lm78_write_value(data, LM78_REG_IN_MIN(nr), data->in_min[nr]); | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 221 | mutex_unlock(&data->update_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | return count; | 
|  | 223 | } | 
|  | 224 |  | 
| Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 225 | static ssize_t set_in_max(struct device *dev, struct device_attribute *da, | 
|  | 226 | const char *buf, size_t count) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | { | 
| Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 228 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); | 
| Jean Delvare | c40769f | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 229 | struct lm78_data *data = dev_get_drvdata(dev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | unsigned long val = simple_strtoul(buf, NULL, 10); | 
| Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 231 | int nr = attr->index; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 |  | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 233 | mutex_lock(&data->update_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | data->in_max[nr] = IN_TO_REG(val); | 
| Jean Delvare | c59cc30 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 235 | lm78_write_value(data, LM78_REG_IN_MAX(nr), data->in_max[nr]); | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 236 | mutex_unlock(&data->update_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | return count; | 
|  | 238 | } | 
|  | 239 |  | 
|  | 240 | #define show_in_offset(offset)					\ | 
| Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 241 | static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO,		\ | 
|  | 242 | show_in, NULL, offset);				\ | 
|  | 243 | static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR,	\ | 
|  | 244 | show_in_min, set_in_min, offset);		\ | 
|  | 245 | static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR,	\ | 
|  | 246 | show_in_max, set_in_max, offset); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 |  | 
|  | 248 | show_in_offset(0); | 
|  | 249 | show_in_offset(1); | 
|  | 250 | show_in_offset(2); | 
|  | 251 | show_in_offset(3); | 
|  | 252 | show_in_offset(4); | 
|  | 253 | show_in_offset(5); | 
|  | 254 | show_in_offset(6); | 
|  | 255 |  | 
|  | 256 | /* Temperature */ | 
| Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 257 | static ssize_t show_temp(struct device *dev, struct device_attribute *da, | 
|  | 258 | char *buf) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | { | 
|  | 260 | struct lm78_data *data = lm78_update_device(dev); | 
|  | 261 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp)); | 
|  | 262 | } | 
|  | 263 |  | 
| Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 264 | static ssize_t show_temp_over(struct device *dev, struct device_attribute *da, | 
|  | 265 | char *buf) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | { | 
|  | 267 | struct lm78_data *data = lm78_update_device(dev); | 
|  | 268 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_over)); | 
|  | 269 | } | 
|  | 270 |  | 
| Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 271 | static ssize_t set_temp_over(struct device *dev, struct device_attribute *da, | 
|  | 272 | const char *buf, size_t count) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | { | 
| Jean Delvare | c40769f | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 274 | struct lm78_data *data = dev_get_drvdata(dev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | long val = simple_strtol(buf, NULL, 10); | 
|  | 276 |  | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 277 | mutex_lock(&data->update_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | data->temp_over = TEMP_TO_REG(val); | 
| Jean Delvare | c59cc30 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 279 | lm78_write_value(data, LM78_REG_TEMP_OVER, data->temp_over); | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 280 | mutex_unlock(&data->update_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | return count; | 
|  | 282 | } | 
|  | 283 |  | 
| Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 284 | static ssize_t show_temp_hyst(struct device *dev, struct device_attribute *da, | 
|  | 285 | char *buf) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | { | 
|  | 287 | struct lm78_data *data = lm78_update_device(dev); | 
|  | 288 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_hyst)); | 
|  | 289 | } | 
|  | 290 |  | 
| Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 291 | static ssize_t set_temp_hyst(struct device *dev, struct device_attribute *da, | 
|  | 292 | const char *buf, size_t count) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | { | 
| Jean Delvare | c40769f | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 294 | struct lm78_data *data = dev_get_drvdata(dev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | long val = simple_strtol(buf, NULL, 10); | 
|  | 296 |  | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 297 | mutex_lock(&data->update_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | data->temp_hyst = TEMP_TO_REG(val); | 
| Jean Delvare | c59cc30 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 299 | lm78_write_value(data, LM78_REG_TEMP_HYST, data->temp_hyst); | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 300 | mutex_unlock(&data->update_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | return count; | 
|  | 302 | } | 
|  | 303 |  | 
|  | 304 | static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL); | 
|  | 305 | static DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR, | 
|  | 306 | show_temp_over, set_temp_over); | 
|  | 307 | static DEVICE_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR, | 
|  | 308 | show_temp_hyst, set_temp_hyst); | 
|  | 309 |  | 
|  | 310 | /* 3 Fans */ | 
| Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 311 | static ssize_t show_fan(struct device *dev, struct device_attribute *da, | 
|  | 312 | char *buf) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | { | 
| Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 314 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | struct lm78_data *data = lm78_update_device(dev); | 
| Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 316 | int nr = attr->index; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr], | 
|  | 318 | DIV_FROM_REG(data->fan_div[nr])) ); | 
|  | 319 | } | 
|  | 320 |  | 
| Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 321 | static ssize_t show_fan_min(struct device *dev, struct device_attribute *da, | 
|  | 322 | char *buf) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | { | 
| Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 324 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | struct lm78_data *data = lm78_update_device(dev); | 
| Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 326 | int nr = attr->index; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan_min[nr], | 
|  | 328 | DIV_FROM_REG(data->fan_div[nr])) ); | 
|  | 329 | } | 
|  | 330 |  | 
| Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 331 | static ssize_t set_fan_min(struct device *dev, struct device_attribute *da, | 
|  | 332 | const char *buf, size_t count) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | { | 
| Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 334 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); | 
| Jean Delvare | c40769f | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 335 | struct lm78_data *data = dev_get_drvdata(dev); | 
| Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 336 | int nr = attr->index; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | unsigned long val = simple_strtoul(buf, NULL, 10); | 
|  | 338 |  | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 339 | mutex_lock(&data->update_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr])); | 
| Jean Delvare | c59cc30 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 341 | lm78_write_value(data, LM78_REG_FAN_MIN(nr), data->fan_min[nr]); | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 342 | mutex_unlock(&data->update_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | return count; | 
|  | 344 | } | 
|  | 345 |  | 
| Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 346 | static ssize_t show_fan_div(struct device *dev, struct device_attribute *da, | 
|  | 347 | char *buf) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | { | 
| Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 349 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | struct lm78_data *data = lm78_update_device(dev); | 
| Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 351 | return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[attr->index])); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | } | 
|  | 353 |  | 
|  | 354 | /* Note: we save and restore the fan minimum here, because its value is | 
|  | 355 | determined in part by the fan divisor.  This follows the principle of | 
| Andreas Mohr | d6e05ed | 2006-06-26 18:35:02 +0200 | [diff] [blame] | 356 | 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] | 357 | because the divisor changed. */ | 
| Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 358 | static ssize_t set_fan_div(struct device *dev, struct device_attribute *da, | 
|  | 359 | const char *buf, size_t count) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | { | 
| Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 361 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); | 
| Jean Delvare | c40769f | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 362 | struct lm78_data *data = dev_get_drvdata(dev); | 
| Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 363 | int nr = attr->index; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | unsigned long val = simple_strtoul(buf, NULL, 10); | 
|  | 365 | unsigned long min; | 
|  | 366 | u8 reg; | 
|  | 367 |  | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 368 | mutex_lock(&data->update_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | min = FAN_FROM_REG(data->fan_min[nr], | 
|  | 370 | DIV_FROM_REG(data->fan_div[nr])); | 
|  | 371 |  | 
|  | 372 | switch (val) { | 
|  | 373 | case 1: data->fan_div[nr] = 0; break; | 
|  | 374 | case 2: data->fan_div[nr] = 1; break; | 
|  | 375 | case 4: data->fan_div[nr] = 2; break; | 
|  | 376 | case 8: data->fan_div[nr] = 3; break; | 
|  | 377 | default: | 
| Jean Delvare | c40769f | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 378 | dev_err(dev, "fan_div value %ld not " | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | "supported. Choose one of 1, 2, 4 or 8!\n", val); | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 380 | mutex_unlock(&data->update_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | return -EINVAL; | 
|  | 382 | } | 
|  | 383 |  | 
| Jean Delvare | c59cc30 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 384 | reg = lm78_read_value(data, LM78_REG_VID_FANDIV); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | switch (nr) { | 
|  | 386 | case 0: | 
|  | 387 | reg = (reg & 0xcf) | (data->fan_div[nr] << 4); | 
|  | 388 | break; | 
|  | 389 | case 1: | 
|  | 390 | reg = (reg & 0x3f) | (data->fan_div[nr] << 6); | 
|  | 391 | break; | 
|  | 392 | } | 
| Jean Delvare | c59cc30 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 393 | lm78_write_value(data, LM78_REG_VID_FANDIV, reg); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 |  | 
|  | 395 | data->fan_min[nr] = | 
|  | 396 | FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr])); | 
| Jean Delvare | c59cc30 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 397 | lm78_write_value(data, LM78_REG_FAN_MIN(nr), data->fan_min[nr]); | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 398 | mutex_unlock(&data->update_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 |  | 
|  | 400 | return count; | 
|  | 401 | } | 
|  | 402 |  | 
| Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 403 | #define show_fan_offset(offset)				\ | 
|  | 404 | static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO,		\ | 
|  | 405 | show_fan, NULL, offset - 1);			\ | 
|  | 406 | static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR,	\ | 
|  | 407 | show_fan_min, set_fan_min, offset - 1); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 |  | 
|  | 409 | show_fan_offset(1); | 
|  | 410 | show_fan_offset(2); | 
|  | 411 | show_fan_offset(3); | 
|  | 412 |  | 
|  | 413 | /* Fan 3 divisor is locked in H/W */ | 
| Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 414 | static SENSOR_DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR, | 
|  | 415 | show_fan_div, set_fan_div, 0); | 
|  | 416 | static SENSOR_DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR, | 
|  | 417 | show_fan_div, set_fan_div, 1); | 
|  | 418 | static SENSOR_DEVICE_ATTR(fan3_div, S_IRUGO, show_fan_div, NULL, 2); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 |  | 
|  | 420 | /* VID */ | 
| Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 421 | static ssize_t show_vid(struct device *dev, struct device_attribute *da, | 
|  | 422 | char *buf) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | { | 
|  | 424 | struct lm78_data *data = lm78_update_device(dev); | 
| Jean Delvare | d0d3cd6 | 2005-11-23 15:44:26 -0800 | [diff] [blame] | 425 | return sprintf(buf, "%d\n", vid_from_reg(data->vid, 82)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | } | 
|  | 427 | static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL); | 
|  | 428 |  | 
|  | 429 | /* Alarms */ | 
| Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 430 | static ssize_t show_alarms(struct device *dev, struct device_attribute *da, | 
|  | 431 | char *buf) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | { | 
|  | 433 | struct lm78_data *data = lm78_update_device(dev); | 
|  | 434 | return sprintf(buf, "%u\n", data->alarms); | 
|  | 435 | } | 
|  | 436 | static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL); | 
|  | 437 |  | 
| Jean Delvare | 428a703 | 2007-09-04 23:25:33 +0200 | [diff] [blame] | 438 | static ssize_t show_alarm(struct device *dev, struct device_attribute *da, | 
|  | 439 | char *buf) | 
|  | 440 | { | 
|  | 441 | struct lm78_data *data = lm78_update_device(dev); | 
|  | 442 | int nr = to_sensor_dev_attr(da)->index; | 
|  | 443 | return sprintf(buf, "%u\n", (data->alarms >> nr) & 1); | 
|  | 444 | } | 
|  | 445 | static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0); | 
|  | 446 | static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1); | 
|  | 447 | static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2); | 
|  | 448 | static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3); | 
|  | 449 | static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8); | 
|  | 450 | static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 9); | 
|  | 451 | static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 10); | 
|  | 452 | static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6); | 
|  | 453 | static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7); | 
|  | 454 | static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 11); | 
|  | 455 | static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4); | 
|  | 456 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | /* This function is called when: | 
|  | 458 | * lm78_driver is inserted (when this module is loaded), for each | 
|  | 459 | available adapter | 
|  | 460 | * when a new adapter is inserted (and lm78_driver is still present) */ | 
|  | 461 | static int lm78_attach_adapter(struct i2c_adapter *adapter) | 
|  | 462 | { | 
|  | 463 | if (!(adapter->class & I2C_CLASS_HWMON)) | 
|  | 464 | return 0; | 
| Jean Delvare | 2ed2dc3 | 2005-07-31 21:42:02 +0200 | [diff] [blame] | 465 | return i2c_probe(adapter, &addr_data, lm78_detect); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | } | 
|  | 467 |  | 
| Mark M. Hoffman | c1685f6 | 2006-09-24 20:59:49 +0200 | [diff] [blame] | 468 | static struct attribute *lm78_attributes[] = { | 
| Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 469 | &sensor_dev_attr_in0_input.dev_attr.attr, | 
|  | 470 | &sensor_dev_attr_in0_min.dev_attr.attr, | 
|  | 471 | &sensor_dev_attr_in0_max.dev_attr.attr, | 
| Jean Delvare | 428a703 | 2007-09-04 23:25:33 +0200 | [diff] [blame] | 472 | &sensor_dev_attr_in0_alarm.dev_attr.attr, | 
| Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 473 | &sensor_dev_attr_in1_input.dev_attr.attr, | 
|  | 474 | &sensor_dev_attr_in1_min.dev_attr.attr, | 
|  | 475 | &sensor_dev_attr_in1_max.dev_attr.attr, | 
| Jean Delvare | 428a703 | 2007-09-04 23:25:33 +0200 | [diff] [blame] | 476 | &sensor_dev_attr_in1_alarm.dev_attr.attr, | 
| Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 477 | &sensor_dev_attr_in2_input.dev_attr.attr, | 
|  | 478 | &sensor_dev_attr_in2_min.dev_attr.attr, | 
|  | 479 | &sensor_dev_attr_in2_max.dev_attr.attr, | 
| Jean Delvare | 428a703 | 2007-09-04 23:25:33 +0200 | [diff] [blame] | 480 | &sensor_dev_attr_in2_alarm.dev_attr.attr, | 
| Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 481 | &sensor_dev_attr_in3_input.dev_attr.attr, | 
|  | 482 | &sensor_dev_attr_in3_min.dev_attr.attr, | 
|  | 483 | &sensor_dev_attr_in3_max.dev_attr.attr, | 
| Jean Delvare | 428a703 | 2007-09-04 23:25:33 +0200 | [diff] [blame] | 484 | &sensor_dev_attr_in3_alarm.dev_attr.attr, | 
| Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 485 | &sensor_dev_attr_in4_input.dev_attr.attr, | 
|  | 486 | &sensor_dev_attr_in4_min.dev_attr.attr, | 
|  | 487 | &sensor_dev_attr_in4_max.dev_attr.attr, | 
| Jean Delvare | 428a703 | 2007-09-04 23:25:33 +0200 | [diff] [blame] | 488 | &sensor_dev_attr_in4_alarm.dev_attr.attr, | 
| Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 489 | &sensor_dev_attr_in5_input.dev_attr.attr, | 
|  | 490 | &sensor_dev_attr_in5_min.dev_attr.attr, | 
|  | 491 | &sensor_dev_attr_in5_max.dev_attr.attr, | 
| Jean Delvare | 428a703 | 2007-09-04 23:25:33 +0200 | [diff] [blame] | 492 | &sensor_dev_attr_in5_alarm.dev_attr.attr, | 
| Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 493 | &sensor_dev_attr_in6_input.dev_attr.attr, | 
|  | 494 | &sensor_dev_attr_in6_min.dev_attr.attr, | 
|  | 495 | &sensor_dev_attr_in6_max.dev_attr.attr, | 
| Jean Delvare | 428a703 | 2007-09-04 23:25:33 +0200 | [diff] [blame] | 496 | &sensor_dev_attr_in6_alarm.dev_attr.attr, | 
| Mark M. Hoffman | c1685f6 | 2006-09-24 20:59:49 +0200 | [diff] [blame] | 497 | &dev_attr_temp1_input.attr, | 
|  | 498 | &dev_attr_temp1_max.attr, | 
|  | 499 | &dev_attr_temp1_max_hyst.attr, | 
| Jean Delvare | 428a703 | 2007-09-04 23:25:33 +0200 | [diff] [blame] | 500 | &sensor_dev_attr_temp1_alarm.dev_attr.attr, | 
| Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 501 | &sensor_dev_attr_fan1_input.dev_attr.attr, | 
|  | 502 | &sensor_dev_attr_fan1_min.dev_attr.attr, | 
|  | 503 | &sensor_dev_attr_fan1_div.dev_attr.attr, | 
| Jean Delvare | 428a703 | 2007-09-04 23:25:33 +0200 | [diff] [blame] | 504 | &sensor_dev_attr_fan1_alarm.dev_attr.attr, | 
| Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 505 | &sensor_dev_attr_fan2_input.dev_attr.attr, | 
|  | 506 | &sensor_dev_attr_fan2_min.dev_attr.attr, | 
|  | 507 | &sensor_dev_attr_fan2_div.dev_attr.attr, | 
| Jean Delvare | 428a703 | 2007-09-04 23:25:33 +0200 | [diff] [blame] | 508 | &sensor_dev_attr_fan2_alarm.dev_attr.attr, | 
| Jean Delvare | 247dde4 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 509 | &sensor_dev_attr_fan3_input.dev_attr.attr, | 
|  | 510 | &sensor_dev_attr_fan3_min.dev_attr.attr, | 
|  | 511 | &sensor_dev_attr_fan3_div.dev_attr.attr, | 
| Jean Delvare | 428a703 | 2007-09-04 23:25:33 +0200 | [diff] [blame] | 512 | &sensor_dev_attr_fan3_alarm.dev_attr.attr, | 
| Mark M. Hoffman | c1685f6 | 2006-09-24 20:59:49 +0200 | [diff] [blame] | 513 | &dev_attr_alarms.attr, | 
|  | 514 | &dev_attr_cpu0_vid.attr, | 
|  | 515 |  | 
|  | 516 | NULL | 
|  | 517 | }; | 
|  | 518 |  | 
|  | 519 | static const struct attribute_group lm78_group = { | 
|  | 520 | .attrs = lm78_attributes, | 
|  | 521 | }; | 
|  | 522 |  | 
| Jean Delvare | c40769f | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 523 | /* I2C devices get this name attribute automatically, but for ISA devices | 
|  | 524 | we must create it by ourselves. */ | 
|  | 525 | static ssize_t show_name(struct device *dev, struct device_attribute | 
|  | 526 | *devattr, char *buf) | 
|  | 527 | { | 
|  | 528 | struct lm78_data *data = dev_get_drvdata(dev); | 
|  | 529 |  | 
|  | 530 | return sprintf(buf, "%s\n", data->client.name); | 
|  | 531 | } | 
|  | 532 | static DEVICE_ATTR(name, S_IRUGO, show_name, NULL); | 
|  | 533 |  | 
| Jean Delvare | 2ed2dc3 | 2005-07-31 21:42:02 +0200 | [diff] [blame] | 534 | /* This function is called by i2c_probe */ | 
| Ben Dooks | d8d2061 | 2005-10-26 21:05:46 +0200 | [diff] [blame] | 535 | static int lm78_detect(struct i2c_adapter *adapter, int address, int kind) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | { | 
|  | 537 | int i, err; | 
|  | 538 | struct i2c_client *new_client; | 
|  | 539 | struct lm78_data *data; | 
|  | 540 | const char *client_name = ""; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 |  | 
| Jean Delvare | c40769f | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 542 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | err = -ENODEV; | 
| Jean Delvare | c40769f | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 544 | goto ERROR1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | } | 
|  | 546 |  | 
|  | 547 | /* OK. For now, we presume we have a valid client. We now create the | 
|  | 548 | client structure, even though we cannot fill it completely yet. | 
|  | 549 | But it allows us to access lm78_{read,write}_value. */ | 
|  | 550 |  | 
| Deepak Saxena | ba9c2e8 | 2005-10-17 23:08:32 +0200 | [diff] [blame] | 551 | if (!(data = kzalloc(sizeof(struct lm78_data), GFP_KERNEL))) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | err = -ENOMEM; | 
|  | 553 | goto ERROR1; | 
|  | 554 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 |  | 
|  | 556 | new_client = &data->client; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | i2c_set_clientdata(new_client, data); | 
|  | 558 | new_client->addr = address; | 
|  | 559 | new_client->adapter = adapter; | 
| Jean Delvare | c40769f | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 560 | new_client->driver = &lm78_driver; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 |  | 
|  | 562 | /* Now, we do the remaining detection. */ | 
|  | 563 | if (kind < 0) { | 
| Jean Delvare | c59cc30 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 564 | if (lm78_read_value(data, LM78_REG_CONFIG) & 0x80) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | err = -ENODEV; | 
|  | 566 | goto ERROR2; | 
|  | 567 | } | 
| Jean Delvare | c59cc30 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 568 | if (lm78_read_value(data, LM78_REG_I2C_ADDR) != | 
| Jean Delvare | c40769f | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 569 | address) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | err = -ENODEV; | 
|  | 571 | goto ERROR2; | 
|  | 572 | } | 
|  | 573 | } | 
|  | 574 |  | 
|  | 575 | /* Determine the chip type. */ | 
|  | 576 | if (kind <= 0) { | 
| Jean Delvare | c59cc30 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 577 | i = lm78_read_value(data, LM78_REG_CHIPID); | 
| Jean Delvare | 27fe048 | 2005-07-27 21:30:16 +0200 | [diff] [blame] | 578 | if (i == 0x00 || i == 0x20	/* LM78 */ | 
|  | 579 | || i == 0x40)			/* LM78-J */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | kind = lm78; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | else if ((i & 0xfe) == 0xc0) | 
|  | 582 | kind = lm79; | 
|  | 583 | else { | 
|  | 584 | if (kind == 0) | 
|  | 585 | dev_warn(&adapter->dev, "Ignoring 'force' " | 
|  | 586 | "parameter for unknown chip at " | 
|  | 587 | "adapter %d, address 0x%02x\n", | 
|  | 588 | i2c_adapter_id(adapter), address); | 
|  | 589 | err = -ENODEV; | 
|  | 590 | goto ERROR2; | 
|  | 591 | } | 
|  | 592 | } | 
|  | 593 |  | 
|  | 594 | if (kind == lm78) { | 
|  | 595 | client_name = "lm78"; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | } else if (kind == lm79) { | 
|  | 597 | client_name = "lm79"; | 
|  | 598 | } | 
|  | 599 |  | 
|  | 600 | /* Fill in the remaining client fields and put into the global list */ | 
|  | 601 | strlcpy(new_client->name, client_name, I2C_NAME_SIZE); | 
|  | 602 | data->type = kind; | 
|  | 603 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 */ | 
| Jean Delvare | c59cc30 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 609 | lm78_init_device(data); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | /* Register sysfs hooks */ | 
| Mark M. Hoffman | c1685f6 | 2006-09-24 20:59:49 +0200 | [diff] [blame] | 612 | if ((err = sysfs_create_group(&new_client->dev.kobj, &lm78_group))) | 
|  | 613 | goto ERROR3; | 
|  | 614 |  | 
| Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 615 | data->hwmon_dev = hwmon_device_register(&new_client->dev); | 
|  | 616 | if (IS_ERR(data->hwmon_dev)) { | 
|  | 617 | err = PTR_ERR(data->hwmon_dev); | 
| Mark M. Hoffman | c1685f6 | 2006-09-24 20:59:49 +0200 | [diff] [blame] | 618 | goto ERROR4; | 
| Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 619 | } | 
|  | 620 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | return 0; | 
|  | 622 |  | 
| Mark M. Hoffman | c1685f6 | 2006-09-24 20:59:49 +0200 | [diff] [blame] | 623 | ERROR4: | 
|  | 624 | sysfs_remove_group(&new_client->dev.kobj, &lm78_group); | 
| Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 625 | ERROR3: | 
|  | 626 | i2c_detach_client(new_client); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | ERROR2: | 
|  | 628 | kfree(data); | 
|  | 629 | ERROR1: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | return err; | 
|  | 631 | } | 
|  | 632 |  | 
|  | 633 | static int lm78_detach_client(struct i2c_client *client) | 
|  | 634 | { | 
| Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 635 | struct lm78_data *data = i2c_get_clientdata(client); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | int err; | 
|  | 637 |  | 
| Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 638 | hwmon_device_unregister(data->hwmon_dev); | 
| Mark M. Hoffman | c1685f6 | 2006-09-24 20:59:49 +0200 | [diff] [blame] | 639 | sysfs_remove_group(&client->dev.kobj, &lm78_group); | 
| Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 640 |  | 
| Jean Delvare | 7bef559 | 2005-07-27 22:14:49 +0200 | [diff] [blame] | 641 | if ((err = i2c_detach_client(client))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | return err; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 |  | 
| Jean Delvare | c40769f | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 644 | kfree(data); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 |  | 
| Jean Delvare | c40769f | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 646 | return 0; | 
|  | 647 | } | 
|  | 648 |  | 
|  | 649 | static int __devinit lm78_isa_probe(struct platform_device *pdev) | 
|  | 650 | { | 
|  | 651 | int err; | 
|  | 652 | struct lm78_data *data; | 
|  | 653 | struct resource *res; | 
|  | 654 | const char *name; | 
|  | 655 |  | 
|  | 656 | /* Reserve the ISA region */ | 
|  | 657 | res = platform_get_resource(pdev, IORESOURCE_IO, 0); | 
|  | 658 | if (!request_region(res->start, LM78_EXTENT, "lm78")) { | 
|  | 659 | err = -EBUSY; | 
|  | 660 | goto exit; | 
|  | 661 | } | 
|  | 662 |  | 
|  | 663 | if (!(data = kzalloc(sizeof(struct lm78_data), GFP_KERNEL))) { | 
|  | 664 | err = -ENOMEM; | 
|  | 665 | goto exit_release_region; | 
|  | 666 | } | 
|  | 667 | mutex_init(&data->lock); | 
|  | 668 | data->client.addr = res->start; | 
|  | 669 | i2c_set_clientdata(&data->client, data); | 
|  | 670 | platform_set_drvdata(pdev, data); | 
|  | 671 |  | 
| Jean Delvare | c59cc30 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 672 | if (lm78_read_value(data, LM78_REG_CHIPID) & 0x80) { | 
| Jean Delvare | c40769f | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 673 | data->type = lm79; | 
|  | 674 | name = "lm79"; | 
|  | 675 | } else { | 
|  | 676 | data->type = lm78; | 
|  | 677 | name = "lm78"; | 
|  | 678 | } | 
|  | 679 | strlcpy(data->client.name, name, I2C_NAME_SIZE); | 
|  | 680 |  | 
|  | 681 | /* Initialize the LM78 chip */ | 
| Jean Delvare | c59cc30 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 682 | lm78_init_device(data); | 
| Jean Delvare | c40769f | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 683 |  | 
|  | 684 | /* Register sysfs hooks */ | 
|  | 685 | if ((err = sysfs_create_group(&pdev->dev.kobj, &lm78_group)) | 
|  | 686 | || (err = device_create_file(&pdev->dev, &dev_attr_name))) | 
|  | 687 | goto exit_remove_files; | 
|  | 688 |  | 
| Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 689 | data->hwmon_dev = hwmon_device_register(&pdev->dev); | 
|  | 690 | if (IS_ERR(data->hwmon_dev)) { | 
|  | 691 | err = PTR_ERR(data->hwmon_dev); | 
| Jean Delvare | c40769f | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 692 | goto exit_remove_files; | 
|  | 693 | } | 
|  | 694 |  | 
|  | 695 | return 0; | 
|  | 696 |  | 
|  | 697 | exit_remove_files: | 
|  | 698 | sysfs_remove_group(&pdev->dev.kobj, &lm78_group); | 
|  | 699 | device_remove_file(&pdev->dev, &dev_attr_name); | 
|  | 700 | kfree(data); | 
|  | 701 | exit_release_region: | 
|  | 702 | release_region(res->start, LM78_EXTENT); | 
|  | 703 | exit: | 
|  | 704 | return err; | 
|  | 705 | } | 
|  | 706 |  | 
|  | 707 | static int __devexit lm78_isa_remove(struct platform_device *pdev) | 
|  | 708 | { | 
|  | 709 | struct lm78_data *data = platform_get_drvdata(pdev); | 
|  | 710 |  | 
| Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 711 | hwmon_device_unregister(data->hwmon_dev); | 
| Jean Delvare | c40769f | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 712 | sysfs_remove_group(&pdev->dev.kobj, &lm78_group); | 
|  | 713 | device_remove_file(&pdev->dev, &dev_attr_name); | 
|  | 714 | release_region(data->client.addr, LM78_EXTENT); | 
| Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 715 | kfree(data); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 |  | 
|  | 717 | return 0; | 
|  | 718 | } | 
|  | 719 |  | 
| Steven Cole | 44bbe87 | 2005-05-03 18:21:25 -0600 | [diff] [blame] | 720 | /* The SMBus locks itself, but ISA access must be locked explicitly! | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | We don't want to lock the whole ISA bus, so we lock each client | 
|  | 722 | separately. | 
|  | 723 | We ignore the LM78 BUSY flag at this moment - it could lead to deadlocks, | 
|  | 724 | would slow down the LM78 access and should not be necessary.  */ | 
| Jean Delvare | c59cc30 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 725 | static int lm78_read_value(struct lm78_data *data, u8 reg) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | { | 
| Jean Delvare | c59cc30 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 727 | struct i2c_client *client = &data->client; | 
|  | 728 |  | 
| Jean Delvare | c40769f | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 729 | if (!client->driver) { /* ISA device */ | 
| Jean Delvare | c59cc30 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 730 | int res; | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 731 | mutex_lock(&data->lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | outb_p(reg, client->addr + LM78_ADDR_REG_OFFSET); | 
|  | 733 | res = inb_p(client->addr + LM78_DATA_REG_OFFSET); | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 734 | mutex_unlock(&data->lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | return res; | 
|  | 736 | } else | 
|  | 737 | return i2c_smbus_read_byte_data(client, reg); | 
|  | 738 | } | 
|  | 739 |  | 
| Steven Cole | 44bbe87 | 2005-05-03 18:21:25 -0600 | [diff] [blame] | 740 | /* The SMBus locks itself, but ISA access muse be locked explicitly! | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | We don't want to lock the whole ISA bus, so we lock each client | 
|  | 742 | separately. | 
|  | 743 | We ignore the LM78 BUSY flag at this moment - it could lead to deadlocks, | 
|  | 744 | would slow down the LM78 access and should not be necessary. | 
|  | 745 | There are some ugly typecasts here, but the good new is - they should | 
|  | 746 | nowhere else be necessary! */ | 
| Jean Delvare | c59cc30 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 747 | static int lm78_write_value(struct lm78_data *data, u8 reg, u8 value) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | { | 
| Jean Delvare | c59cc30 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 749 | struct i2c_client *client = &data->client; | 
|  | 750 |  | 
| Jean Delvare | c40769f | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 751 | if (!client->driver) { /* ISA device */ | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 752 | mutex_lock(&data->lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | outb_p(reg, client->addr + LM78_ADDR_REG_OFFSET); | 
|  | 754 | outb_p(value, client->addr + LM78_DATA_REG_OFFSET); | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 755 | mutex_unlock(&data->lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | return 0; | 
|  | 757 | } else | 
|  | 758 | return i2c_smbus_write_byte_data(client, reg, value); | 
|  | 759 | } | 
|  | 760 |  | 
| Jean Delvare | c59cc30 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 761 | static void lm78_init_device(struct lm78_data *data) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | { | 
| Jean Delvare | c40769f | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 763 | u8 config; | 
|  | 764 | int i; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 |  | 
|  | 766 | /* Start monitoring */ | 
| Jean Delvare | c59cc30 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 767 | config = lm78_read_value(data, LM78_REG_CONFIG); | 
| Jean Delvare | c40769f | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 768 | if ((config & 0x09) != 0x01) | 
| Jean Delvare | c59cc30 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 769 | lm78_write_value(data, LM78_REG_CONFIG, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | (config & 0xf7) | 0x01); | 
| Jean Delvare | c40769f | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 771 |  | 
|  | 772 | /* A few vars need to be filled upon startup */ | 
|  | 773 | for (i = 0; i < 3; i++) { | 
| Jean Delvare | c59cc30 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 774 | data->fan_min[i] = lm78_read_value(data, | 
| Jean Delvare | c40769f | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 775 | LM78_REG_FAN_MIN(i)); | 
|  | 776 | } | 
|  | 777 |  | 
|  | 778 | mutex_init(&data->update_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | } | 
|  | 780 |  | 
|  | 781 | static struct lm78_data *lm78_update_device(struct device *dev) | 
|  | 782 | { | 
| Jean Delvare | c40769f | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 783 | struct lm78_data *data = dev_get_drvdata(dev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | int i; | 
|  | 785 |  | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 786 | mutex_lock(&data->update_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 |  | 
|  | 788 | if (time_after(jiffies, data->last_updated + HZ + HZ / 2) | 
|  | 789 | || !data->valid) { | 
|  | 790 |  | 
| Jean Delvare | c40769f | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 791 | dev_dbg(dev, "Starting lm78 update\n"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 |  | 
|  | 793 | for (i = 0; i <= 6; i++) { | 
|  | 794 | data->in[i] = | 
| Jean Delvare | c59cc30 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 795 | lm78_read_value(data, LM78_REG_IN(i)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | data->in_min[i] = | 
| Jean Delvare | c59cc30 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 797 | lm78_read_value(data, LM78_REG_IN_MIN(i)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | data->in_max[i] = | 
| Jean Delvare | c59cc30 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 799 | lm78_read_value(data, LM78_REG_IN_MAX(i)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | } | 
|  | 801 | for (i = 0; i < 3; i++) { | 
|  | 802 | data->fan[i] = | 
| Jean Delvare | c59cc30 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 803 | lm78_read_value(data, LM78_REG_FAN(i)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | data->fan_min[i] = | 
| Jean Delvare | c59cc30 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 805 | lm78_read_value(data, LM78_REG_FAN_MIN(i)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | } | 
| Jean Delvare | c59cc30 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 807 | data->temp = lm78_read_value(data, LM78_REG_TEMP); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | data->temp_over = | 
| Jean Delvare | c59cc30 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 809 | lm78_read_value(data, LM78_REG_TEMP_OVER); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | data->temp_hyst = | 
| Jean Delvare | c59cc30 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 811 | lm78_read_value(data, LM78_REG_TEMP_HYST); | 
|  | 812 | i = lm78_read_value(data, LM78_REG_VID_FANDIV); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | data->vid = i & 0x0f; | 
|  | 814 | if (data->type == lm79) | 
|  | 815 | data->vid |= | 
| Jean Delvare | c59cc30 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 816 | (lm78_read_value(data, LM78_REG_CHIPID) & | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | 0x01) << 4; | 
|  | 818 | else | 
|  | 819 | data->vid |= 0x10; | 
|  | 820 | data->fan_div[0] = (i >> 4) & 0x03; | 
|  | 821 | data->fan_div[1] = i >> 6; | 
| Jean Delvare | c59cc30 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 822 | data->alarms = lm78_read_value(data, LM78_REG_ALARM1) + | 
|  | 823 | (lm78_read_value(data, LM78_REG_ALARM2) << 8); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | data->last_updated = jiffies; | 
|  | 825 | data->valid = 1; | 
|  | 826 |  | 
|  | 827 | data->fan_div[2] = 1; | 
|  | 828 | } | 
|  | 829 |  | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 830 | mutex_unlock(&data->update_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 |  | 
|  | 832 | return data; | 
|  | 833 | } | 
|  | 834 |  | 
| Jean Delvare | c40769f | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 835 | /* return 1 if a supported chip is found, 0 otherwise */ | 
|  | 836 | static int __init lm78_isa_found(unsigned short address) | 
|  | 837 | { | 
|  | 838 | int val, save, found = 0; | 
|  | 839 |  | 
|  | 840 | if (!request_region(address, LM78_EXTENT, "lm78")) | 
|  | 841 | return 0; | 
|  | 842 |  | 
|  | 843 | #define REALLY_SLOW_IO | 
|  | 844 | /* We need the timeouts for at least some LM78-like | 
|  | 845 | chips. But only if we read 'undefined' registers. */ | 
|  | 846 | val = inb_p(address + 1); | 
|  | 847 | if (inb_p(address + 2) != val | 
|  | 848 | || inb_p(address + 3) != val | 
|  | 849 | || inb_p(address + 7) != val) | 
|  | 850 | goto release; | 
|  | 851 | #undef REALLY_SLOW_IO | 
|  | 852 |  | 
|  | 853 | /* We should be able to change the 7 LSB of the address port. The | 
|  | 854 | MSB (busy flag) should be clear initially, set after the write. */ | 
|  | 855 | save = inb_p(address + LM78_ADDR_REG_OFFSET); | 
|  | 856 | if (save & 0x80) | 
|  | 857 | goto release; | 
|  | 858 | val = ~save & 0x7f; | 
|  | 859 | outb_p(val, address + LM78_ADDR_REG_OFFSET); | 
|  | 860 | if (inb_p(address + LM78_ADDR_REG_OFFSET) != (val | 0x80)) { | 
|  | 861 | outb_p(save, address + LM78_ADDR_REG_OFFSET); | 
|  | 862 | goto release; | 
|  | 863 | } | 
|  | 864 |  | 
|  | 865 | /* We found a device, now see if it could be an LM78 */ | 
|  | 866 | outb_p(LM78_REG_CONFIG, address + LM78_ADDR_REG_OFFSET); | 
|  | 867 | val = inb_p(address + LM78_DATA_REG_OFFSET); | 
|  | 868 | if (val & 0x80) | 
|  | 869 | goto release; | 
|  | 870 | outb_p(LM78_REG_I2C_ADDR, address + LM78_ADDR_REG_OFFSET); | 
|  | 871 | val = inb_p(address + LM78_DATA_REG_OFFSET); | 
|  | 872 | if (val < 0x03 || val > 0x77)	/* Not a valid I2C address */ | 
|  | 873 | goto release; | 
|  | 874 |  | 
|  | 875 | /* The busy flag should be clear again */ | 
|  | 876 | if (inb_p(address + LM78_ADDR_REG_OFFSET) & 0x80) | 
|  | 877 | goto release; | 
|  | 878 |  | 
|  | 879 | /* Explicitly prevent the misdetection of Winbond chips */ | 
|  | 880 | outb_p(0x4f, address + LM78_ADDR_REG_OFFSET); | 
|  | 881 | val = inb_p(address + LM78_DATA_REG_OFFSET); | 
|  | 882 | if (val == 0xa3 || val == 0x5c) | 
|  | 883 | goto release; | 
|  | 884 |  | 
|  | 885 | /* Explicitly prevent the misdetection of ITE chips */ | 
|  | 886 | outb_p(0x58, address + LM78_ADDR_REG_OFFSET); | 
|  | 887 | val = inb_p(address + LM78_DATA_REG_OFFSET); | 
|  | 888 | if (val == 0x90) | 
|  | 889 | goto release; | 
|  | 890 |  | 
|  | 891 | /* Determine the chip type */ | 
|  | 892 | outb_p(LM78_REG_CHIPID, address + LM78_ADDR_REG_OFFSET); | 
|  | 893 | val = inb_p(address + LM78_DATA_REG_OFFSET); | 
| Hans de Goede | acf346a | 2007-07-24 23:36:00 +0200 | [diff] [blame] | 894 | if (val == 0x00 || val == 0x20	/* LM78 */ | 
| Jean Delvare | c40769f | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 895 | || val == 0x40			/* LM78-J */ | 
|  | 896 | || (val & 0xfe) == 0xc0)	/* LM79 */ | 
|  | 897 | found = 1; | 
|  | 898 |  | 
|  | 899 | if (found) | 
|  | 900 | pr_info("lm78: Found an %s chip at %#x\n", | 
|  | 901 | val & 0x80 ? "LM79" : "LM78", (int)address); | 
|  | 902 |  | 
|  | 903 | release: | 
|  | 904 | release_region(address, LM78_EXTENT); | 
|  | 905 | return found; | 
|  | 906 | } | 
|  | 907 |  | 
|  | 908 | static int __init lm78_isa_device_add(unsigned short address) | 
|  | 909 | { | 
|  | 910 | struct resource res = { | 
|  | 911 | .start	= address, | 
| Jean Delvare | 15bde2f | 2007-08-29 10:39:57 +0200 | [diff] [blame] | 912 | .end	= address + LM78_EXTENT - 1, | 
| Jean Delvare | c40769f | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 913 | .name	= "lm78", | 
|  | 914 | .flags	= IORESOURCE_IO, | 
|  | 915 | }; | 
|  | 916 | int err; | 
|  | 917 |  | 
|  | 918 | pdev = platform_device_alloc("lm78", address); | 
|  | 919 | if (!pdev) { | 
|  | 920 | err = -ENOMEM; | 
|  | 921 | printk(KERN_ERR "lm78: Device allocation failed\n"); | 
|  | 922 | goto exit; | 
|  | 923 | } | 
|  | 924 |  | 
|  | 925 | err = platform_device_add_resources(pdev, &res, 1); | 
|  | 926 | if (err) { | 
|  | 927 | printk(KERN_ERR "lm78: Device resource addition failed " | 
|  | 928 | "(%d)\n", err); | 
|  | 929 | goto exit_device_put; | 
|  | 930 | } | 
|  | 931 |  | 
|  | 932 | err = platform_device_add(pdev); | 
|  | 933 | if (err) { | 
|  | 934 | printk(KERN_ERR "lm78: Device addition failed (%d)\n", | 
|  | 935 | err); | 
|  | 936 | goto exit_device_put; | 
|  | 937 | } | 
|  | 938 |  | 
|  | 939 | return 0; | 
|  | 940 |  | 
|  | 941 | exit_device_put: | 
|  | 942 | platform_device_put(pdev); | 
|  | 943 | exit: | 
|  | 944 | pdev = NULL; | 
|  | 945 | return err; | 
|  | 946 | } | 
|  | 947 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 948 | static int __init sm_lm78_init(void) | 
|  | 949 | { | 
| Jean Delvare | fde0950 | 2005-07-19 23:51:07 +0200 | [diff] [blame] | 950 | int res; | 
|  | 951 |  | 
|  | 952 | res = i2c_add_driver(&lm78_driver); | 
|  | 953 | if (res) | 
| Jean Delvare | c40769f | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 954 | goto exit; | 
| Jean Delvare | fde0950 | 2005-07-19 23:51:07 +0200 | [diff] [blame] | 955 |  | 
| Jean Delvare | c40769f | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 956 | if (lm78_isa_found(isa_address)) { | 
|  | 957 | res = platform_driver_register(&lm78_isa_driver); | 
|  | 958 | if (res) | 
|  | 959 | goto exit_unreg_i2c_driver; | 
|  | 960 |  | 
|  | 961 | /* Sets global pdev as a side effect */ | 
|  | 962 | res = lm78_isa_device_add(isa_address); | 
|  | 963 | if (res) | 
|  | 964 | goto exit_unreg_isa_driver; | 
|  | 965 | } | 
| Jean Delvare | fde0950 | 2005-07-19 23:51:07 +0200 | [diff] [blame] | 966 |  | 
|  | 967 | return 0; | 
| Jean Delvare | c40769f | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 968 |  | 
|  | 969 | exit_unreg_isa_driver: | 
|  | 970 | platform_driver_unregister(&lm78_isa_driver); | 
|  | 971 | exit_unreg_i2c_driver: | 
|  | 972 | i2c_del_driver(&lm78_driver); | 
|  | 973 | exit: | 
|  | 974 | return res; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 975 | } | 
|  | 976 |  | 
|  | 977 | static void __exit sm_lm78_exit(void) | 
|  | 978 | { | 
| Jean Delvare | c40769f | 2007-05-08 17:22:00 +0200 | [diff] [blame] | 979 | if (pdev) { | 
|  | 980 | platform_device_unregister(pdev); | 
|  | 981 | platform_driver_unregister(&lm78_isa_driver); | 
|  | 982 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 983 | i2c_del_driver(&lm78_driver); | 
|  | 984 | } | 
|  | 985 |  | 
|  | 986 |  | 
|  | 987 |  | 
|  | 988 | MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>"); | 
| Jean Delvare | 27fe048 | 2005-07-27 21:30:16 +0200 | [diff] [blame] | 989 | MODULE_DESCRIPTION("LM78/LM79 driver"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | MODULE_LICENSE("GPL"); | 
|  | 991 |  | 
|  | 992 | module_init(sm_lm78_init); | 
|  | 993 | module_exit(sm_lm78_exit); |