Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | adm1021.c - Part of lm_sensors, Linux kernel modules for hardware |
Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame^] | 3 | monitoring |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl> and |
| 5 | Philip Edelbrock <phil@netroedge.com> |
| 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> |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 27 | #include <linux/hwmon.h> |
| 28 | #include <linux/err.h> |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 29 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
| 31 | |
| 32 | /* Addresses to scan */ |
| 33 | static unsigned short normal_i2c[] = { 0x18, 0x19, 0x1a, |
| 34 | 0x29, 0x2a, 0x2b, |
Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame^] | 35 | 0x4c, 0x4d, 0x4e, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | I2C_CLIENT_END }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
| 38 | /* Insmod parameters */ |
Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame^] | 39 | I2C_CLIENT_INSMOD_8(adm1021, adm1023, max1617, max1617a, thmc10, lm84, gl523sm, |
| 40 | mc1066); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
| 42 | /* adm1021 constants specified below */ |
| 43 | |
| 44 | /* The adm1021 registers */ |
| 45 | /* Read-only */ |
| 46 | #define ADM1021_REG_TEMP 0x00 |
| 47 | #define ADM1021_REG_REMOTE_TEMP 0x01 |
| 48 | #define ADM1021_REG_STATUS 0x02 |
Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame^] | 49 | /* 0x41 = AD, 0x49 = TI, 0x4D = Maxim, 0x23 = Genesys , 0x54 = Onsemi */ |
| 50 | #define ADM1021_REG_MAN_ID 0xFE |
| 51 | /* ADM1021 = 0x0X, ADM1023 = 0x3X */ |
| 52 | #define ADM1021_REG_DEV_ID 0xFF |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | /* These use different addresses for reading/writing */ |
| 54 | #define ADM1021_REG_CONFIG_R 0x03 |
| 55 | #define ADM1021_REG_CONFIG_W 0x09 |
| 56 | #define ADM1021_REG_CONV_RATE_R 0x04 |
| 57 | #define ADM1021_REG_CONV_RATE_W 0x0A |
| 58 | /* These are for the ADM1023's additional precision on the remote temp sensor */ |
Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame^] | 59 | #define ADM1023_REG_REM_TEMP_PREC 0x10 |
| 60 | #define ADM1023_REG_REM_OFFSET 0x11 |
| 61 | #define ADM1023_REG_REM_OFFSET_PREC 0x12 |
| 62 | #define ADM1023_REG_REM_TOS_PREC 0x13 |
| 63 | #define ADM1023_REG_REM_THYST_PREC 0x14 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | /* limits */ |
| 65 | #define ADM1021_REG_TOS_R 0x05 |
| 66 | #define ADM1021_REG_TOS_W 0x0B |
| 67 | #define ADM1021_REG_REMOTE_TOS_R 0x07 |
| 68 | #define ADM1021_REG_REMOTE_TOS_W 0x0D |
| 69 | #define ADM1021_REG_THYST_R 0x06 |
| 70 | #define ADM1021_REG_THYST_W 0x0C |
| 71 | #define ADM1021_REG_REMOTE_THYST_R 0x08 |
| 72 | #define ADM1021_REG_REMOTE_THYST_W 0x0E |
| 73 | /* write-only */ |
| 74 | #define ADM1021_REG_ONESHOT 0x0F |
| 75 | |
| 76 | |
| 77 | /* Conversions. Rounding and limit checking is only done on the TO_REG |
| 78 | variants. Note that you should be a bit careful with which arguments |
| 79 | these macros are called: arguments may be evaluated more than once. |
| 80 | Fixing this is just not worth it. */ |
| 81 | /* Conversions note: 1021 uses normal integer signed-byte format*/ |
Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame^] | 82 | #define TEMP_TO_REG(val) SENSORS_LIMIT((val) / 1000, -128, 127) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | |
| 84 | /* Initial values */ |
| 85 | |
Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame^] | 86 | /* Note: Even though I left the low and high limits named os and hyst, |
| 87 | they don't quite work like a thermostat the way the LM75 does. I.e., |
| 88 | a lower temp than THYST actually triggers an alarm instead of |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | clearing it. Weird, ey? --Phil */ |
| 90 | |
| 91 | /* Each client has this additional data */ |
| 92 | struct adm1021_data { |
| 93 | struct i2c_client client; |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 94 | struct class_device *class_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | enum chips type; |
| 96 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 97 | struct mutex update_lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | char valid; /* !=0 if following fields are valid */ |
| 99 | unsigned long last_updated; /* In jiffies */ |
| 100 | |
Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame^] | 101 | s8 temp_max; /* Register values */ |
| 102 | s8 temp_hyst; |
| 103 | s8 temp_input; |
| 104 | s8 remote_temp_max; |
| 105 | s8 remote_temp_hyst; |
| 106 | s8 remote_temp_input; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | u8 alarms; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | /* Special values for ADM1023 only */ |
| 109 | u8 remote_temp_prec; |
| 110 | u8 remote_temp_os_prec; |
| 111 | u8 remote_temp_hyst_prec; |
| 112 | u8 remote_temp_offset; |
| 113 | u8 remote_temp_offset_prec; |
| 114 | }; |
| 115 | |
| 116 | static int adm1021_attach_adapter(struct i2c_adapter *adapter); |
| 117 | static int adm1021_detect(struct i2c_adapter *adapter, int address, int kind); |
| 118 | static void adm1021_init_client(struct i2c_client *client); |
| 119 | static int adm1021_detach_client(struct i2c_client *client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | static struct adm1021_data *adm1021_update_device(struct device *dev); |
| 121 | |
| 122 | /* (amalysh) read only mode, otherwise any limit's writing confuse BIOS */ |
Jean Delvare | 0200296 | 2005-09-25 16:26:44 +0200 | [diff] [blame] | 123 | static int read_only; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | |
| 125 | |
| 126 | /* This is the driver that will be inserted */ |
| 127 | static struct i2c_driver adm1021_driver = { |
Laurent Riffard | cdaf793 | 2005-11-26 20:37:41 +0100 | [diff] [blame] | 128 | .driver = { |
Laurent Riffard | cdaf793 | 2005-11-26 20:37:41 +0100 | [diff] [blame] | 129 | .name = "adm1021", |
| 130 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | .id = I2C_DRIVERID_ADM1021, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | .attach_adapter = adm1021_attach_adapter, |
| 133 | .detach_client = adm1021_detach_client, |
| 134 | }; |
| 135 | |
Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame^] | 136 | #define show(value) \ |
| 137 | static ssize_t show_##value(struct device *dev, \ |
| 138 | struct device_attribute *attr, char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | { \ |
| 140 | struct adm1021_data *data = adm1021_update_device(dev); \ |
Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame^] | 141 | return sprintf(buf, "%d\n", 1000 * data->value); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | } |
| 143 | show(temp_max); |
| 144 | show(temp_hyst); |
| 145 | show(temp_input); |
| 146 | show(remote_temp_max); |
| 147 | show(remote_temp_hyst); |
| 148 | show(remote_temp_input); |
| 149 | |
Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame^] | 150 | static ssize_t show_alarms(struct device *dev, |
| 151 | struct device_attribute *attr, |
| 152 | char *buf) |
| 153 | { |
| 154 | struct adm1021_data *data = adm1021_update_device(dev); |
| 155 | return sprintf(buf, "%u\n", data->alarms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | |
Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame^] | 158 | #define set(value, reg) \ |
| 159 | static ssize_t set_##value(struct device *dev, \ |
| 160 | struct device_attribute *attr, \ |
| 161 | const char *buf, size_t count) \ |
| 162 | { \ |
| 163 | struct i2c_client *client = to_i2c_client(dev); \ |
| 164 | struct adm1021_data *data = i2c_get_clientdata(client); \ |
| 165 | int temp = simple_strtoul(buf, NULL, 10); \ |
| 166 | \ |
| 167 | mutex_lock(&data->update_lock); \ |
| 168 | data->value = TEMP_TO_REG(temp); \ |
| 169 | if (!read_only) \ |
| 170 | i2c_smbus_write_byte_data(client, reg, data->value); \ |
| 171 | mutex_unlock(&data->update_lock); \ |
| 172 | return count; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | } |
| 174 | set(temp_max, ADM1021_REG_TOS_W); |
| 175 | set(temp_hyst, ADM1021_REG_THYST_W); |
| 176 | set(remote_temp_max, ADM1021_REG_REMOTE_TOS_W); |
| 177 | set(remote_temp_hyst, ADM1021_REG_REMOTE_THYST_W); |
| 178 | |
| 179 | static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max, set_temp_max); |
| 180 | static DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp_hyst, set_temp_hyst); |
| 181 | static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input, NULL); |
| 182 | static DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_remote_temp_max, set_remote_temp_max); |
| 183 | static DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_remote_temp_hyst, set_remote_temp_hyst); |
| 184 | static DEVICE_ATTR(temp2_input, S_IRUGO, show_remote_temp_input, NULL); |
| 185 | static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | |
| 187 | |
| 188 | static int adm1021_attach_adapter(struct i2c_adapter *adapter) |
| 189 | { |
| 190 | if (!(adapter->class & I2C_CLASS_HWMON)) |
| 191 | return 0; |
Jean Delvare | 2ed2dc3 | 2005-07-31 21:42:02 +0200 | [diff] [blame] | 192 | return i2c_probe(adapter, &addr_data, adm1021_detect); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | } |
| 194 | |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 195 | static struct attribute *adm1021_attributes[] = { |
| 196 | &dev_attr_temp1_max.attr, |
| 197 | &dev_attr_temp1_min.attr, |
| 198 | &dev_attr_temp1_input.attr, |
| 199 | &dev_attr_temp2_max.attr, |
| 200 | &dev_attr_temp2_min.attr, |
| 201 | &dev_attr_temp2_input.attr, |
| 202 | &dev_attr_alarms.attr, |
| 203 | NULL |
| 204 | }; |
| 205 | |
| 206 | static const struct attribute_group adm1021_group = { |
| 207 | .attrs = adm1021_attributes, |
| 208 | }; |
| 209 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | static int adm1021_detect(struct i2c_adapter *adapter, int address, int kind) |
| 211 | { |
| 212 | int i; |
Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame^] | 213 | struct i2c_client *client; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | struct adm1021_data *data; |
| 215 | int err = 0; |
| 216 | const char *type_name = ""; |
Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame^] | 217 | int conv_rate, status, config; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | |
Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame^] | 219 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) { |
| 220 | pr_debug("adm1021: detect failed, " |
| 221 | "smbus byte data not supported!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | goto error0; |
Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame^] | 223 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | |
| 225 | /* OK. For now, we presume we have a valid client. We now create the |
| 226 | client structure, even though we cannot fill it completely yet. |
Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame^] | 227 | But it allows us to access adm1021 register values. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | |
Deepak Saxena | ba9c2e8 | 2005-10-17 23:08:32 +0200 | [diff] [blame] | 229 | if (!(data = kzalloc(sizeof(struct adm1021_data), GFP_KERNEL))) { |
Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame^] | 230 | pr_debug("adm1021: detect failed, kzalloc failed!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | err = -ENOMEM; |
| 232 | goto error0; |
| 233 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | |
Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame^] | 235 | client = &data->client; |
| 236 | i2c_set_clientdata(client, data); |
| 237 | client->addr = address; |
| 238 | client->adapter = adapter; |
| 239 | client->driver = &adm1021_driver; |
| 240 | status = i2c_smbus_read_byte_data(client, ADM1021_REG_STATUS); |
| 241 | conv_rate = i2c_smbus_read_byte_data(client, |
| 242 | ADM1021_REG_CONV_RATE_R); |
| 243 | config = i2c_smbus_read_byte_data(client, ADM1021_REG_CONFIG_R); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | |
| 245 | /* Now, we do the remaining detection. */ |
| 246 | if (kind < 0) { |
Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame^] | 247 | if ((status & 0x03) != 0x00 || (config & 0x3F) != 0x00 |
| 248 | || (conv_rate & 0xF8) != 0x00) { |
| 249 | pr_debug("adm1021: detect failed, " |
| 250 | "chip not detected!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | err = -ENODEV; |
| 252 | goto error1; |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | /* Determine the chip type. */ |
| 257 | if (kind <= 0) { |
Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame^] | 258 | i = i2c_smbus_read_byte_data(client, ADM1021_REG_MAN_ID); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | if (i == 0x41) |
Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame^] | 260 | if ((i2c_smbus_read_byte_data(client, |
| 261 | ADM1021_REG_DEV_ID) & 0xF0) == 0x30) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | kind = adm1023; |
| 263 | else |
| 264 | kind = adm1021; |
| 265 | else if (i == 0x49) |
| 266 | kind = thmc10; |
| 267 | else if (i == 0x23) |
| 268 | kind = gl523sm; |
| 269 | else if ((i == 0x4d) && |
Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame^] | 270 | (i2c_smbus_read_byte_data(client, |
| 271 | ADM1021_REG_DEV_ID) == 0x01)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | kind = max1617a; |
| 273 | else if (i == 0x54) |
| 274 | kind = mc1066; |
| 275 | /* LM84 Mfr ID in a different place, and it has more unused bits */ |
Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame^] | 276 | else if (conv_rate == 0x00 |
| 277 | && (kind == 0 /* skip extra detection */ |
| 278 | || ((config & 0x7F) == 0x00 |
| 279 | && (status & 0xAB) == 0x00))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | kind = lm84; |
| 281 | else |
| 282 | kind = max1617; |
| 283 | } |
| 284 | |
| 285 | if (kind == max1617) { |
| 286 | type_name = "max1617"; |
| 287 | } else if (kind == max1617a) { |
| 288 | type_name = "max1617a"; |
| 289 | } else if (kind == adm1021) { |
| 290 | type_name = "adm1021"; |
| 291 | } else if (kind == adm1023) { |
| 292 | type_name = "adm1023"; |
| 293 | } else if (kind == thmc10) { |
| 294 | type_name = "thmc10"; |
| 295 | } else if (kind == lm84) { |
| 296 | type_name = "lm84"; |
| 297 | } else if (kind == gl523sm) { |
| 298 | type_name = "gl523sm"; |
| 299 | } else if (kind == mc1066) { |
| 300 | type_name = "mc1066"; |
| 301 | } |
Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame^] | 302 | pr_debug("adm1021: Detected chip %s at adapter %d, address 0x%02x.\n", |
| 303 | type_name, i2c_adapter_id(adapter), address); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | |
Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame^] | 305 | /* Fill in the remaining client fields */ |
| 306 | strlcpy(client->name, type_name, I2C_NAME_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | data->type = kind; |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 308 | mutex_init(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | |
| 310 | /* Tell the I2C layer a new client has arrived */ |
Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame^] | 311 | if ((err = i2c_attach_client(client))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | goto error1; |
| 313 | |
| 314 | /* Initialize the ADM1021 chip */ |
Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame^] | 315 | if (kind != lm84 && !read_only) |
| 316 | adm1021_init_client(client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | |
| 318 | /* Register sysfs hooks */ |
Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame^] | 319 | if ((err = sysfs_create_group(&client->dev.kobj, &adm1021_group))) |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 320 | goto error2; |
| 321 | |
Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame^] | 322 | data->class_dev = hwmon_device_register(&client->dev); |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 323 | if (IS_ERR(data->class_dev)) { |
| 324 | err = PTR_ERR(data->class_dev); |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 325 | goto error3; |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 326 | } |
| 327 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | return 0; |
| 329 | |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 330 | error3: |
Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame^] | 331 | sysfs_remove_group(&client->dev.kobj, &adm1021_group); |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 332 | error2: |
Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame^] | 333 | i2c_detach_client(client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | error1: |
| 335 | kfree(data); |
| 336 | error0: |
| 337 | return err; |
| 338 | } |
| 339 | |
| 340 | static void adm1021_init_client(struct i2c_client *client) |
| 341 | { |
| 342 | /* Enable ADC and disable suspend mode */ |
Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame^] | 343 | i2c_smbus_write_byte_data(client, ADM1021_REG_CONFIG_W, |
| 344 | i2c_smbus_read_byte_data(client, ADM1021_REG_CONFIG_R) & 0xBF); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | /* Set Conversion rate to 1/sec (this can be tinkered with) */ |
Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame^] | 346 | i2c_smbus_write_byte_data(client, ADM1021_REG_CONV_RATE_W, 0x04); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | static int adm1021_detach_client(struct i2c_client *client) |
| 350 | { |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 351 | struct adm1021_data *data = i2c_get_clientdata(client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | int err; |
| 353 | |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 354 | hwmon_device_unregister(data->class_dev); |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 355 | sysfs_remove_group(&client->dev.kobj, &adm1021_group); |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 356 | |
Jean Delvare | 7bef559 | 2005-07-27 22:14:49 +0200 | [diff] [blame] | 357 | if ((err = i2c_detach_client(client))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 360 | kfree(data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | return 0; |
| 362 | } |
| 363 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | static struct adm1021_data *adm1021_update_device(struct device *dev) |
| 365 | { |
| 366 | struct i2c_client *client = to_i2c_client(dev); |
| 367 | struct adm1021_data *data = i2c_get_clientdata(client); |
| 368 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 369 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | |
| 371 | if (time_after(jiffies, data->last_updated + HZ + HZ / 2) |
| 372 | || !data->valid) { |
| 373 | dev_dbg(&client->dev, "Starting adm1021 update\n"); |
| 374 | |
Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame^] | 375 | data->temp_input = i2c_smbus_read_byte_data(client, |
| 376 | ADM1021_REG_TEMP); |
| 377 | data->temp_max = i2c_smbus_read_byte_data(client, |
| 378 | ADM1021_REG_TOS_R); |
| 379 | data->temp_hyst = i2c_smbus_read_byte_data(client, |
| 380 | ADM1021_REG_THYST_R); |
| 381 | data->remote_temp_input = i2c_smbus_read_byte_data(client, |
| 382 | ADM1021_REG_REMOTE_TEMP); |
| 383 | data->remote_temp_max = i2c_smbus_read_byte_data(client, |
| 384 | ADM1021_REG_REMOTE_TOS_R); |
| 385 | data->remote_temp_hyst = i2c_smbus_read_byte_data(client, |
| 386 | ADM1021_REG_REMOTE_THYST_R); |
| 387 | data->alarms = i2c_smbus_read_byte_data(client, |
| 388 | ADM1021_REG_STATUS) & 0x7c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | if (data->type == adm1023) { |
Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame^] | 390 | data->remote_temp_prec = |
| 391 | i2c_smbus_read_byte_data(client, |
| 392 | ADM1023_REG_REM_TEMP_PREC); |
| 393 | data->remote_temp_os_prec = |
| 394 | i2c_smbus_read_byte_data(client, |
| 395 | ADM1023_REG_REM_TOS_PREC); |
| 396 | data->remote_temp_hyst_prec = |
| 397 | i2c_smbus_read_byte_data(client, |
| 398 | ADM1023_REG_REM_THYST_PREC); |
| 399 | data->remote_temp_offset = |
| 400 | i2c_smbus_read_byte_data(client, |
| 401 | ADM1023_REG_REM_OFFSET); |
| 402 | data->remote_temp_offset_prec = |
| 403 | i2c_smbus_read_byte_data(client, |
| 404 | ADM1023_REG_REM_OFFSET_PREC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | } |
| 406 | data->last_updated = jiffies; |
| 407 | data->valid = 1; |
| 408 | } |
| 409 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 410 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | |
| 412 | return data; |
| 413 | } |
| 414 | |
| 415 | static int __init sensors_adm1021_init(void) |
| 416 | { |
| 417 | return i2c_add_driver(&adm1021_driver); |
| 418 | } |
| 419 | |
| 420 | static void __exit sensors_adm1021_exit(void) |
| 421 | { |
| 422 | i2c_del_driver(&adm1021_driver); |
| 423 | } |
| 424 | |
| 425 | MODULE_AUTHOR ("Frodo Looijaard <frodol@dds.nl> and " |
| 426 | "Philip Edelbrock <phil@netroedge.com>"); |
| 427 | MODULE_DESCRIPTION("adm1021 driver"); |
| 428 | MODULE_LICENSE("GPL"); |
| 429 | |
| 430 | module_param(read_only, bool, 0); |
| 431 | MODULE_PARM_DESC(read_only, "Don't set any values, read only mode"); |
| 432 | |
| 433 | module_init(sensors_adm1021_init) |
| 434 | module_exit(sensors_adm1021_exit) |