Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * lm83.c - Part of lm_sensors, Linux kernel modules for hardware |
| 3 | * monitoring |
Jean Delvare | 2d45771 | 2006-09-24 20:52:15 +0200 | [diff] [blame] | 4 | * Copyright (C) 2003-2006 Jean Delvare <khali@linux-fr.org> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * |
| 6 | * Heavily inspired from the lm78, lm75 and adm1021 drivers. The LM83 is |
| 7 | * a sensor chip made by National Semiconductor. It reports up to four |
| 8 | * temperatures (its own plus up to three external ones) with a 1 deg |
| 9 | * resolution and a 3-4 deg accuracy. Complete datasheet can be obtained |
| 10 | * from National's website at: |
| 11 | * http://www.national.com/pf/LM/LM83.html |
| 12 | * Since the datasheet omits to give the chip stepping code, I give it |
| 13 | * here: 0x03 (at register 0xff). |
| 14 | * |
Jordan Crouse | 43cb7eb | 2006-03-23 16:19:49 +0100 | [diff] [blame] | 15 | * Also supports the LM82 temp sensor, which is basically a stripped down |
| 16 | * model of the LM83. Datasheet is here: |
| 17 | * http://www.national.com/pf/LM/LM82.html |
| 18 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | * This program is free software; you can redistribute it and/or modify |
| 20 | * it under the terms of the GNU General Public License as published by |
| 21 | * the Free Software Foundation; either version 2 of the License, or |
| 22 | * (at your option) any later version. |
| 23 | * |
| 24 | * This program is distributed in the hope that it will be useful, |
| 25 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 26 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 27 | * GNU General Public License for more details. |
| 28 | * |
| 29 | * You should have received a copy of the GNU General Public License |
| 30 | * along with this program; if not, write to the Free Software |
| 31 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 32 | */ |
| 33 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include <linux/module.h> |
| 35 | #include <linux/init.h> |
| 36 | #include <linux/slab.h> |
| 37 | #include <linux/jiffies.h> |
| 38 | #include <linux/i2c.h> |
Jean Delvare | 10c08f8 | 2005-06-06 19:34:45 +0200 | [diff] [blame] | 39 | #include <linux/hwmon-sysfs.h> |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 40 | #include <linux/hwmon.h> |
| 41 | #include <linux/err.h> |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 42 | #include <linux/mutex.h> |
Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 43 | #include <linux/sysfs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | |
| 45 | /* |
| 46 | * Addresses to scan |
| 47 | * Address is selected using 2 three-level pins, resulting in 9 possible |
| 48 | * addresses. |
| 49 | */ |
| 50 | |
Mark M. Hoffman | 25e9c86 | 2008-02-17 22:28:03 -0500 | [diff] [blame] | 51 | static const unsigned short normal_i2c[] = { |
| 52 | 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x4c, 0x4d, 0x4e, I2C_CLIENT_END }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
| 54 | /* |
| 55 | * Insmod parameters |
| 56 | */ |
| 57 | |
Jordan Crouse | 43cb7eb | 2006-03-23 16:19:49 +0100 | [diff] [blame] | 58 | I2C_CLIENT_INSMOD_2(lm83, lm82); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | |
| 60 | /* |
| 61 | * The LM83 registers |
| 62 | * Manufacturer ID is 0x01 for National Semiconductor. |
| 63 | */ |
| 64 | |
| 65 | #define LM83_REG_R_MAN_ID 0xFE |
| 66 | #define LM83_REG_R_CHIP_ID 0xFF |
| 67 | #define LM83_REG_R_CONFIG 0x03 |
| 68 | #define LM83_REG_W_CONFIG 0x09 |
| 69 | #define LM83_REG_R_STATUS1 0x02 |
| 70 | #define LM83_REG_R_STATUS2 0x35 |
| 71 | #define LM83_REG_R_LOCAL_TEMP 0x00 |
| 72 | #define LM83_REG_R_LOCAL_HIGH 0x05 |
| 73 | #define LM83_REG_W_LOCAL_HIGH 0x0B |
| 74 | #define LM83_REG_R_REMOTE1_TEMP 0x30 |
| 75 | #define LM83_REG_R_REMOTE1_HIGH 0x38 |
| 76 | #define LM83_REG_W_REMOTE1_HIGH 0x50 |
| 77 | #define LM83_REG_R_REMOTE2_TEMP 0x01 |
| 78 | #define LM83_REG_R_REMOTE2_HIGH 0x07 |
| 79 | #define LM83_REG_W_REMOTE2_HIGH 0x0D |
| 80 | #define LM83_REG_R_REMOTE3_TEMP 0x31 |
| 81 | #define LM83_REG_R_REMOTE3_HIGH 0x3A |
| 82 | #define LM83_REG_W_REMOTE3_HIGH 0x52 |
| 83 | #define LM83_REG_R_TCRIT 0x42 |
| 84 | #define LM83_REG_W_TCRIT 0x5A |
| 85 | |
| 86 | /* |
| 87 | * Conversions and various macros |
Steven Cole | 44bbe87 | 2005-05-03 18:21:25 -0600 | [diff] [blame] | 88 | * The LM83 uses signed 8-bit values with LSB = 1 degree Celsius. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | */ |
| 90 | |
| 91 | #define TEMP_FROM_REG(val) ((val) * 1000) |
| 92 | #define TEMP_TO_REG(val) ((val) <= -128000 ? -128 : \ |
| 93 | (val) >= 127000 ? 127 : \ |
| 94 | (val) < 0 ? ((val) - 500) / 1000 : \ |
| 95 | ((val) + 500) / 1000) |
| 96 | |
| 97 | static const u8 LM83_REG_R_TEMP[] = { |
| 98 | LM83_REG_R_LOCAL_TEMP, |
| 99 | LM83_REG_R_REMOTE1_TEMP, |
| 100 | LM83_REG_R_REMOTE2_TEMP, |
Jean Delvare | 1a86c05 | 2005-06-05 21:16:39 +0200 | [diff] [blame] | 101 | LM83_REG_R_REMOTE3_TEMP, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | LM83_REG_R_LOCAL_HIGH, |
| 103 | LM83_REG_R_REMOTE1_HIGH, |
| 104 | LM83_REG_R_REMOTE2_HIGH, |
Jean Delvare | 1a86c05 | 2005-06-05 21:16:39 +0200 | [diff] [blame] | 105 | LM83_REG_R_REMOTE3_HIGH, |
| 106 | LM83_REG_R_TCRIT, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | static const u8 LM83_REG_W_HIGH[] = { |
| 110 | LM83_REG_W_LOCAL_HIGH, |
| 111 | LM83_REG_W_REMOTE1_HIGH, |
| 112 | LM83_REG_W_REMOTE2_HIGH, |
Jean Delvare | 1a86c05 | 2005-06-05 21:16:39 +0200 | [diff] [blame] | 113 | LM83_REG_W_REMOTE3_HIGH, |
| 114 | LM83_REG_W_TCRIT, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | }; |
| 116 | |
| 117 | /* |
| 118 | * Functions declaration |
| 119 | */ |
| 120 | |
| 121 | static int lm83_attach_adapter(struct i2c_adapter *adapter); |
| 122 | static int lm83_detect(struct i2c_adapter *adapter, int address, int kind); |
| 123 | static int lm83_detach_client(struct i2c_client *client); |
| 124 | static struct lm83_data *lm83_update_device(struct device *dev); |
| 125 | |
| 126 | /* |
| 127 | * Driver data (common to all clients) |
| 128 | */ |
| 129 | |
| 130 | static struct i2c_driver lm83_driver = { |
Laurent Riffard | cdaf793 | 2005-11-26 20:37:41 +0100 | [diff] [blame] | 131 | .driver = { |
Laurent Riffard | cdaf793 | 2005-11-26 20:37:41 +0100 | [diff] [blame] | 132 | .name = "lm83", |
| 133 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | .attach_adapter = lm83_attach_adapter, |
| 135 | .detach_client = lm83_detach_client, |
| 136 | }; |
| 137 | |
| 138 | /* |
| 139 | * Client data (each client gets its own) |
| 140 | */ |
| 141 | |
| 142 | struct lm83_data { |
| 143 | struct i2c_client client; |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 144 | struct device *hwmon_dev; |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 145 | struct mutex update_lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | char valid; /* zero until following fields are valid */ |
| 147 | unsigned long last_updated; /* in jiffies */ |
| 148 | |
| 149 | /* registers values */ |
Jean Delvare | 1a86c05 | 2005-06-05 21:16:39 +0200 | [diff] [blame] | 150 | s8 temp[9]; /* 0..3: input 1-4, |
| 151 | 4..7: high limit 1-4, |
| 152 | 8 : critical limit */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | u16 alarms; /* bitvector, combined */ |
| 154 | }; |
| 155 | |
| 156 | /* |
| 157 | * Sysfs stuff |
| 158 | */ |
| 159 | |
Jean Delvare | 1a86c05 | 2005-06-05 21:16:39 +0200 | [diff] [blame] | 160 | static ssize_t show_temp(struct device *dev, struct device_attribute *devattr, |
| 161 | char *buf) |
| 162 | { |
| 163 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 164 | struct lm83_data *data = lm83_update_device(dev); |
| 165 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[attr->index])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | |
Jean Delvare | 1a86c05 | 2005-06-05 21:16:39 +0200 | [diff] [blame] | 168 | static ssize_t set_temp(struct device *dev, struct device_attribute *devattr, |
| 169 | const char *buf, size_t count) |
| 170 | { |
| 171 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 172 | struct i2c_client *client = to_i2c_client(dev); |
| 173 | struct lm83_data *data = i2c_get_clientdata(client); |
| 174 | long val = simple_strtol(buf, NULL, 10); |
| 175 | int nr = attr->index; |
| 176 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 177 | mutex_lock(&data->update_lock); |
Jean Delvare | 1a86c05 | 2005-06-05 21:16:39 +0200 | [diff] [blame] | 178 | data->temp[nr] = TEMP_TO_REG(val); |
| 179 | i2c_smbus_write_byte_data(client, LM83_REG_W_HIGH[nr - 4], |
| 180 | data->temp[nr]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 181 | mutex_unlock(&data->update_lock); |
Jean Delvare | 1a86c05 | 2005-06-05 21:16:39 +0200 | [diff] [blame] | 182 | return count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | |
Jean Delvare | 1a86c05 | 2005-06-05 21:16:39 +0200 | [diff] [blame] | 185 | static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy, |
| 186 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | { |
| 188 | struct lm83_data *data = lm83_update_device(dev); |
| 189 | return sprintf(buf, "%d\n", data->alarms); |
| 190 | } |
| 191 | |
Jean Delvare | 2d45771 | 2006-09-24 20:52:15 +0200 | [diff] [blame] | 192 | static ssize_t show_alarm(struct device *dev, struct device_attribute |
| 193 | *devattr, char *buf) |
| 194 | { |
| 195 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 196 | struct lm83_data *data = lm83_update_device(dev); |
| 197 | int bitnr = attr->index; |
| 198 | |
| 199 | return sprintf(buf, "%d\n", (data->alarms >> bitnr) & 1); |
| 200 | } |
| 201 | |
Jean Delvare | 1a86c05 | 2005-06-05 21:16:39 +0200 | [diff] [blame] | 202 | static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0); |
| 203 | static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1); |
| 204 | static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2); |
| 205 | static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3); |
| 206 | static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp, |
| 207 | set_temp, 4); |
| 208 | static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp, |
| 209 | set_temp, 5); |
| 210 | static SENSOR_DEVICE_ATTR(temp3_max, S_IWUSR | S_IRUGO, show_temp, |
| 211 | set_temp, 6); |
| 212 | static SENSOR_DEVICE_ATTR(temp4_max, S_IWUSR | S_IRUGO, show_temp, |
| 213 | set_temp, 7); |
| 214 | static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, show_temp, NULL, 8); |
| 215 | static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, show_temp, NULL, 8); |
| 216 | static SENSOR_DEVICE_ATTR(temp3_crit, S_IWUSR | S_IRUGO, show_temp, |
| 217 | set_temp, 8); |
| 218 | static SENSOR_DEVICE_ATTR(temp4_crit, S_IRUGO, show_temp, NULL, 8); |
Jean Delvare | 2d45771 | 2006-09-24 20:52:15 +0200 | [diff] [blame] | 219 | |
| 220 | /* Individual alarm files */ |
| 221 | static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 0); |
| 222 | static SENSOR_DEVICE_ATTR(temp3_crit_alarm, S_IRUGO, show_alarm, NULL, 1); |
Jean Delvare | 7817a39 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 223 | static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 2); |
Jean Delvare | 2d45771 | 2006-09-24 20:52:15 +0200 | [diff] [blame] | 224 | static SENSOR_DEVICE_ATTR(temp3_max_alarm, S_IRUGO, show_alarm, NULL, 4); |
| 225 | static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6); |
| 226 | static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 8); |
| 227 | static SENSOR_DEVICE_ATTR(temp4_crit_alarm, S_IRUGO, show_alarm, NULL, 9); |
Jean Delvare | 7817a39 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 228 | static SENSOR_DEVICE_ATTR(temp4_fault, S_IRUGO, show_alarm, NULL, 10); |
Jean Delvare | 2d45771 | 2006-09-24 20:52:15 +0200 | [diff] [blame] | 229 | static SENSOR_DEVICE_ATTR(temp4_max_alarm, S_IRUGO, show_alarm, NULL, 12); |
Jean Delvare | 7817a39 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 230 | static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 13); |
Jean Delvare | 2d45771 | 2006-09-24 20:52:15 +0200 | [diff] [blame] | 231 | static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 15); |
| 232 | /* Raw alarm file for compatibility */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL); |
| 234 | |
Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 235 | static struct attribute *lm83_attributes[] = { |
| 236 | &sensor_dev_attr_temp1_input.dev_attr.attr, |
| 237 | &sensor_dev_attr_temp3_input.dev_attr.attr, |
| 238 | &sensor_dev_attr_temp1_max.dev_attr.attr, |
| 239 | &sensor_dev_attr_temp3_max.dev_attr.attr, |
| 240 | &sensor_dev_attr_temp1_crit.dev_attr.attr, |
| 241 | &sensor_dev_attr_temp3_crit.dev_attr.attr, |
| 242 | |
| 243 | &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr, |
| 244 | &sensor_dev_attr_temp3_crit_alarm.dev_attr.attr, |
Jean Delvare | 7817a39 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 245 | &sensor_dev_attr_temp3_fault.dev_attr.attr, |
Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 246 | &sensor_dev_attr_temp3_max_alarm.dev_attr.attr, |
| 247 | &sensor_dev_attr_temp1_max_alarm.dev_attr.attr, |
| 248 | &dev_attr_alarms.attr, |
| 249 | NULL |
| 250 | }; |
| 251 | |
| 252 | static const struct attribute_group lm83_group = { |
| 253 | .attrs = lm83_attributes, |
| 254 | }; |
| 255 | |
| 256 | static struct attribute *lm83_attributes_opt[] = { |
| 257 | &sensor_dev_attr_temp2_input.dev_attr.attr, |
| 258 | &sensor_dev_attr_temp4_input.dev_attr.attr, |
| 259 | &sensor_dev_attr_temp2_max.dev_attr.attr, |
| 260 | &sensor_dev_attr_temp4_max.dev_attr.attr, |
| 261 | &sensor_dev_attr_temp2_crit.dev_attr.attr, |
| 262 | &sensor_dev_attr_temp4_crit.dev_attr.attr, |
| 263 | |
| 264 | &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr, |
| 265 | &sensor_dev_attr_temp4_crit_alarm.dev_attr.attr, |
Jean Delvare | 7817a39 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 266 | &sensor_dev_attr_temp4_fault.dev_attr.attr, |
Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 267 | &sensor_dev_attr_temp4_max_alarm.dev_attr.attr, |
Jean Delvare | 7817a39 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 268 | &sensor_dev_attr_temp2_fault.dev_attr.attr, |
Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 269 | &sensor_dev_attr_temp2_max_alarm.dev_attr.attr, |
| 270 | NULL |
| 271 | }; |
| 272 | |
| 273 | static const struct attribute_group lm83_group_opt = { |
| 274 | .attrs = lm83_attributes_opt, |
| 275 | }; |
| 276 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | /* |
| 278 | * Real code |
| 279 | */ |
| 280 | |
| 281 | static int lm83_attach_adapter(struct i2c_adapter *adapter) |
| 282 | { |
| 283 | if (!(adapter->class & I2C_CLASS_HWMON)) |
| 284 | return 0; |
Jean Delvare | 2ed2dc3 | 2005-07-31 21:42:02 +0200 | [diff] [blame] | 285 | return i2c_probe(adapter, &addr_data, lm83_detect); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | /* |
| 289 | * The following function does more than just detection. If detection |
| 290 | * succeeds, it also registers the new chip. |
| 291 | */ |
| 292 | static int lm83_detect(struct i2c_adapter *adapter, int address, int kind) |
| 293 | { |
| 294 | struct i2c_client *new_client; |
| 295 | struct lm83_data *data; |
| 296 | int err = 0; |
| 297 | const char *name = ""; |
| 298 | |
| 299 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) |
| 300 | goto exit; |
| 301 | |
Deepak Saxena | ba9c2e8 | 2005-10-17 23:08:32 +0200 | [diff] [blame] | 302 | if (!(data = kzalloc(sizeof(struct lm83_data), GFP_KERNEL))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | err = -ENOMEM; |
| 304 | goto exit; |
| 305 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | |
| 307 | /* The common I2C client data is placed right after the |
| 308 | * LM83-specific data. */ |
| 309 | new_client = &data->client; |
| 310 | i2c_set_clientdata(new_client, data); |
| 311 | new_client->addr = address; |
| 312 | new_client->adapter = adapter; |
| 313 | new_client->driver = &lm83_driver; |
| 314 | new_client->flags = 0; |
| 315 | |
| 316 | /* Now we do the detection and identification. A negative kind |
| 317 | * means that the driver was loaded with no force parameter |
| 318 | * (default), so we must both detect and identify the chip |
| 319 | * (actually there is only one possible kind of chip for now, LM83). |
| 320 | * A zero kind means that the driver was loaded with the force |
| 321 | * parameter, the detection step shall be skipped. A positive kind |
| 322 | * means that the driver was loaded with the force parameter and a |
| 323 | * given kind of chip is requested, so both the detection and the |
| 324 | * identification steps are skipped. */ |
| 325 | |
| 326 | /* Default to an LM83 if forced */ |
| 327 | if (kind == 0) |
| 328 | kind = lm83; |
| 329 | |
| 330 | if (kind < 0) { /* detection */ |
| 331 | if (((i2c_smbus_read_byte_data(new_client, LM83_REG_R_STATUS1) |
| 332 | & 0xA8) != 0x00) || |
| 333 | ((i2c_smbus_read_byte_data(new_client, LM83_REG_R_STATUS2) |
| 334 | & 0x48) != 0x00) || |
| 335 | ((i2c_smbus_read_byte_data(new_client, LM83_REG_R_CONFIG) |
| 336 | & 0x41) != 0x00)) { |
| 337 | dev_dbg(&adapter->dev, |
| 338 | "LM83 detection failed at 0x%02x.\n", address); |
| 339 | goto exit_free; |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | if (kind <= 0) { /* identification */ |
| 344 | u8 man_id, chip_id; |
| 345 | |
| 346 | man_id = i2c_smbus_read_byte_data(new_client, |
| 347 | LM83_REG_R_MAN_ID); |
| 348 | chip_id = i2c_smbus_read_byte_data(new_client, |
| 349 | LM83_REG_R_CHIP_ID); |
| 350 | |
| 351 | if (man_id == 0x01) { /* National Semiconductor */ |
| 352 | if (chip_id == 0x03) { |
| 353 | kind = lm83; |
Jordan Crouse | 43cb7eb | 2006-03-23 16:19:49 +0100 | [diff] [blame] | 354 | } else |
| 355 | if (chip_id == 0x01) { |
| 356 | kind = lm82; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | } |
| 358 | } |
| 359 | |
| 360 | if (kind <= 0) { /* identification failed */ |
| 361 | dev_info(&adapter->dev, |
| 362 | "Unsupported chip (man_id=0x%02X, " |
| 363 | "chip_id=0x%02X).\n", man_id, chip_id); |
| 364 | goto exit_free; |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | if (kind == lm83) { |
| 369 | name = "lm83"; |
Jordan Crouse | 43cb7eb | 2006-03-23 16:19:49 +0100 | [diff] [blame] | 370 | } else |
| 371 | if (kind == lm82) { |
| 372 | name = "lm82"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | /* We can fill in the remaining client fields */ |
| 376 | strlcpy(new_client->name, name, I2C_NAME_SIZE); |
| 377 | data->valid = 0; |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 378 | mutex_init(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | |
| 380 | /* Tell the I2C layer a new client has arrived */ |
| 381 | if ((err = i2c_attach_client(new_client))) |
| 382 | goto exit_free; |
| 383 | |
| 384 | /* |
Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 385 | * Register sysfs hooks |
Jordan Crouse | 43cb7eb | 2006-03-23 16:19:49 +0100 | [diff] [blame] | 386 | * The LM82 can only monitor one external diode which is |
| 387 | * at the same register as the LM83 temp3 entry - so we |
| 388 | * declare 1 and 3 common, and then 2 and 4 only for the LM83. |
| 389 | */ |
| 390 | |
Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 391 | if ((err = sysfs_create_group(&new_client->dev.kobj, &lm83_group))) |
| 392 | goto exit_detach; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | |
Jordan Crouse | 43cb7eb | 2006-03-23 16:19:49 +0100 | [diff] [blame] | 394 | if (kind == lm83) { |
Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 395 | if ((err = sysfs_create_group(&new_client->dev.kobj, |
| 396 | &lm83_group_opt))) |
| 397 | goto exit_remove_files; |
| 398 | } |
Jordan Crouse | 43cb7eb | 2006-03-23 16:19:49 +0100 | [diff] [blame] | 399 | |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 400 | data->hwmon_dev = hwmon_device_register(&new_client->dev); |
| 401 | if (IS_ERR(data->hwmon_dev)) { |
| 402 | err = PTR_ERR(data->hwmon_dev); |
Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 403 | goto exit_remove_files; |
Jordan Crouse | 43cb7eb | 2006-03-23 16:19:49 +0100 | [diff] [blame] | 404 | } |
| 405 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | return 0; |
| 407 | |
Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 408 | exit_remove_files: |
| 409 | sysfs_remove_group(&new_client->dev.kobj, &lm83_group); |
| 410 | sysfs_remove_group(&new_client->dev.kobj, &lm83_group_opt); |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 411 | exit_detach: |
| 412 | i2c_detach_client(new_client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | exit_free: |
| 414 | kfree(data); |
| 415 | exit: |
| 416 | return err; |
| 417 | } |
| 418 | |
| 419 | static int lm83_detach_client(struct i2c_client *client) |
| 420 | { |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 421 | struct lm83_data *data = i2c_get_clientdata(client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | int err; |
| 423 | |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 424 | hwmon_device_unregister(data->hwmon_dev); |
Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 425 | sysfs_remove_group(&client->dev.kobj, &lm83_group); |
| 426 | sysfs_remove_group(&client->dev.kobj, &lm83_group_opt); |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 427 | |
Jean Delvare | 7bef559 | 2005-07-27 22:14:49 +0200 | [diff] [blame] | 428 | if ((err = i2c_detach_client(client))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 431 | kfree(data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | return 0; |
| 433 | } |
| 434 | |
| 435 | static struct lm83_data *lm83_update_device(struct device *dev) |
| 436 | { |
| 437 | struct i2c_client *client = to_i2c_client(dev); |
| 438 | struct lm83_data *data = i2c_get_clientdata(client); |
| 439 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 440 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | |
| 442 | if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) { |
| 443 | int nr; |
| 444 | |
| 445 | dev_dbg(&client->dev, "Updating lm83 data.\n"); |
Jean Delvare | 1a86c05 | 2005-06-05 21:16:39 +0200 | [diff] [blame] | 446 | for (nr = 0; nr < 9; nr++) { |
| 447 | data->temp[nr] = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | i2c_smbus_read_byte_data(client, |
| 449 | LM83_REG_R_TEMP[nr]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | data->alarms = |
| 452 | i2c_smbus_read_byte_data(client, LM83_REG_R_STATUS1) |
| 453 | + (i2c_smbus_read_byte_data(client, LM83_REG_R_STATUS2) |
| 454 | << 8); |
| 455 | |
| 456 | data->last_updated = jiffies; |
| 457 | data->valid = 1; |
| 458 | } |
| 459 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 460 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | |
| 462 | return data; |
| 463 | } |
| 464 | |
| 465 | static int __init sensors_lm83_init(void) |
| 466 | { |
| 467 | return i2c_add_driver(&lm83_driver); |
| 468 | } |
| 469 | |
| 470 | static void __exit sensors_lm83_exit(void) |
| 471 | { |
| 472 | i2c_del_driver(&lm83_driver); |
| 473 | } |
| 474 | |
| 475 | MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>"); |
| 476 | MODULE_DESCRIPTION("LM83 driver"); |
| 477 | MODULE_LICENSE("GPL"); |
| 478 | |
| 479 | module_init(sensors_lm83_init); |
| 480 | module_exit(sensors_lm83_exit); |