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 | 1a86c05 | 2005-06-05 21:16:39 +0200 | [diff] [blame^] | 4 | * Copyright (C) 2003-2005 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 | * |
| 15 | * This program is free software; you can redistribute it and/or modify |
| 16 | * it under the terms of the GNU General Public License as published by |
| 17 | * the Free Software Foundation; either version 2 of the License, or |
| 18 | * (at your option) any later version. |
| 19 | * |
| 20 | * This program is distributed in the hope that it will be useful, |
| 21 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 23 | * GNU General Public License for more details. |
| 24 | * |
| 25 | * You should have received a copy of the GNU General Public License |
| 26 | * along with this program; if not, write to the Free Software |
| 27 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 28 | */ |
| 29 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include <linux/module.h> |
| 31 | #include <linux/init.h> |
| 32 | #include <linux/slab.h> |
| 33 | #include <linux/jiffies.h> |
| 34 | #include <linux/i2c.h> |
| 35 | #include <linux/i2c-sensor.h> |
Jean Delvare | 1a86c05 | 2005-06-05 21:16:39 +0200 | [diff] [blame^] | 36 | #include <linux/i2c-sysfs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
| 38 | /* |
| 39 | * Addresses to scan |
| 40 | * Address is selected using 2 three-level pins, resulting in 9 possible |
| 41 | * addresses. |
| 42 | */ |
| 43 | |
| 44 | static unsigned short normal_i2c[] = { 0x18, 0x19, 0x1a, |
| 45 | 0x29, 0x2a, 0x2b, |
| 46 | 0x4c, 0x4d, 0x4e, |
| 47 | I2C_CLIENT_END }; |
| 48 | static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END }; |
| 49 | |
| 50 | /* |
| 51 | * Insmod parameters |
| 52 | */ |
| 53 | |
| 54 | SENSORS_INSMOD_1(lm83); |
| 55 | |
| 56 | /* |
| 57 | * The LM83 registers |
| 58 | * Manufacturer ID is 0x01 for National Semiconductor. |
| 59 | */ |
| 60 | |
| 61 | #define LM83_REG_R_MAN_ID 0xFE |
| 62 | #define LM83_REG_R_CHIP_ID 0xFF |
| 63 | #define LM83_REG_R_CONFIG 0x03 |
| 64 | #define LM83_REG_W_CONFIG 0x09 |
| 65 | #define LM83_REG_R_STATUS1 0x02 |
| 66 | #define LM83_REG_R_STATUS2 0x35 |
| 67 | #define LM83_REG_R_LOCAL_TEMP 0x00 |
| 68 | #define LM83_REG_R_LOCAL_HIGH 0x05 |
| 69 | #define LM83_REG_W_LOCAL_HIGH 0x0B |
| 70 | #define LM83_REG_R_REMOTE1_TEMP 0x30 |
| 71 | #define LM83_REG_R_REMOTE1_HIGH 0x38 |
| 72 | #define LM83_REG_W_REMOTE1_HIGH 0x50 |
| 73 | #define LM83_REG_R_REMOTE2_TEMP 0x01 |
| 74 | #define LM83_REG_R_REMOTE2_HIGH 0x07 |
| 75 | #define LM83_REG_W_REMOTE2_HIGH 0x0D |
| 76 | #define LM83_REG_R_REMOTE3_TEMP 0x31 |
| 77 | #define LM83_REG_R_REMOTE3_HIGH 0x3A |
| 78 | #define LM83_REG_W_REMOTE3_HIGH 0x52 |
| 79 | #define LM83_REG_R_TCRIT 0x42 |
| 80 | #define LM83_REG_W_TCRIT 0x5A |
| 81 | |
| 82 | /* |
| 83 | * Conversions and various macros |
Steven Cole | 44bbe87 | 2005-05-03 18:21:25 -0600 | [diff] [blame] | 84 | * The LM83 uses signed 8-bit values with LSB = 1 degree Celsius. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | */ |
| 86 | |
| 87 | #define TEMP_FROM_REG(val) ((val) * 1000) |
| 88 | #define TEMP_TO_REG(val) ((val) <= -128000 ? -128 : \ |
| 89 | (val) >= 127000 ? 127 : \ |
| 90 | (val) < 0 ? ((val) - 500) / 1000 : \ |
| 91 | ((val) + 500) / 1000) |
| 92 | |
| 93 | static const u8 LM83_REG_R_TEMP[] = { |
| 94 | LM83_REG_R_LOCAL_TEMP, |
| 95 | LM83_REG_R_REMOTE1_TEMP, |
| 96 | LM83_REG_R_REMOTE2_TEMP, |
Jean Delvare | 1a86c05 | 2005-06-05 21:16:39 +0200 | [diff] [blame^] | 97 | LM83_REG_R_REMOTE3_TEMP, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | LM83_REG_R_LOCAL_HIGH, |
| 99 | LM83_REG_R_REMOTE1_HIGH, |
| 100 | LM83_REG_R_REMOTE2_HIGH, |
Jean Delvare | 1a86c05 | 2005-06-05 21:16:39 +0200 | [diff] [blame^] | 101 | LM83_REG_R_REMOTE3_HIGH, |
| 102 | LM83_REG_R_TCRIT, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | static const u8 LM83_REG_W_HIGH[] = { |
| 106 | LM83_REG_W_LOCAL_HIGH, |
| 107 | LM83_REG_W_REMOTE1_HIGH, |
| 108 | LM83_REG_W_REMOTE2_HIGH, |
Jean Delvare | 1a86c05 | 2005-06-05 21:16:39 +0200 | [diff] [blame^] | 109 | LM83_REG_W_REMOTE3_HIGH, |
| 110 | LM83_REG_W_TCRIT, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | }; |
| 112 | |
| 113 | /* |
| 114 | * Functions declaration |
| 115 | */ |
| 116 | |
| 117 | static int lm83_attach_adapter(struct i2c_adapter *adapter); |
| 118 | static int lm83_detect(struct i2c_adapter *adapter, int address, int kind); |
| 119 | static int lm83_detach_client(struct i2c_client *client); |
| 120 | static struct lm83_data *lm83_update_device(struct device *dev); |
| 121 | |
| 122 | /* |
| 123 | * Driver data (common to all clients) |
| 124 | */ |
| 125 | |
| 126 | static struct i2c_driver lm83_driver = { |
| 127 | .owner = THIS_MODULE, |
| 128 | .name = "lm83", |
| 129 | .id = I2C_DRIVERID_LM83, |
| 130 | .flags = I2C_DF_NOTIFY, |
| 131 | .attach_adapter = lm83_attach_adapter, |
| 132 | .detach_client = lm83_detach_client, |
| 133 | }; |
| 134 | |
| 135 | /* |
| 136 | * Client data (each client gets its own) |
| 137 | */ |
| 138 | |
| 139 | struct lm83_data { |
| 140 | struct i2c_client client; |
| 141 | struct semaphore update_lock; |
| 142 | char valid; /* zero until following fields are valid */ |
| 143 | unsigned long last_updated; /* in jiffies */ |
| 144 | |
| 145 | /* registers values */ |
Jean Delvare | 1a86c05 | 2005-06-05 21:16:39 +0200 | [diff] [blame^] | 146 | s8 temp[9]; /* 0..3: input 1-4, |
| 147 | 4..7: high limit 1-4, |
| 148 | 8 : critical limit */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | u16 alarms; /* bitvector, combined */ |
| 150 | }; |
| 151 | |
| 152 | /* |
| 153 | * Sysfs stuff |
| 154 | */ |
| 155 | |
Jean Delvare | 1a86c05 | 2005-06-05 21:16:39 +0200 | [diff] [blame^] | 156 | static ssize_t show_temp(struct device *dev, struct device_attribute *devattr, |
| 157 | char *buf) |
| 158 | { |
| 159 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 160 | struct lm83_data *data = lm83_update_device(dev); |
| 161 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[attr->index])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | |
Jean Delvare | 1a86c05 | 2005-06-05 21:16:39 +0200 | [diff] [blame^] | 164 | static ssize_t set_temp(struct device *dev, struct device_attribute *devattr, |
| 165 | const char *buf, size_t count) |
| 166 | { |
| 167 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 168 | struct i2c_client *client = to_i2c_client(dev); |
| 169 | struct lm83_data *data = i2c_get_clientdata(client); |
| 170 | long val = simple_strtol(buf, NULL, 10); |
| 171 | int nr = attr->index; |
| 172 | |
| 173 | down(&data->update_lock); |
| 174 | data->temp[nr] = TEMP_TO_REG(val); |
| 175 | i2c_smbus_write_byte_data(client, LM83_REG_W_HIGH[nr - 4], |
| 176 | data->temp[nr]); |
| 177 | up(&data->update_lock); |
| 178 | return count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | |
Jean Delvare | 1a86c05 | 2005-06-05 21:16:39 +0200 | [diff] [blame^] | 181 | static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy, |
| 182 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | { |
| 184 | struct lm83_data *data = lm83_update_device(dev); |
| 185 | return sprintf(buf, "%d\n", data->alarms); |
| 186 | } |
| 187 | |
Jean Delvare | 1a86c05 | 2005-06-05 21:16:39 +0200 | [diff] [blame^] | 188 | static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0); |
| 189 | static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1); |
| 190 | static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2); |
| 191 | static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3); |
| 192 | static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp, |
| 193 | set_temp, 4); |
| 194 | static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp, |
| 195 | set_temp, 5); |
| 196 | static SENSOR_DEVICE_ATTR(temp3_max, S_IWUSR | S_IRUGO, show_temp, |
| 197 | set_temp, 6); |
| 198 | static SENSOR_DEVICE_ATTR(temp4_max, S_IWUSR | S_IRUGO, show_temp, |
| 199 | set_temp, 7); |
| 200 | static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, show_temp, NULL, 8); |
| 201 | static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, show_temp, NULL, 8); |
| 202 | static SENSOR_DEVICE_ATTR(temp3_crit, S_IWUSR | S_IRUGO, show_temp, |
| 203 | set_temp, 8); |
| 204 | static SENSOR_DEVICE_ATTR(temp4_crit, S_IRUGO, show_temp, NULL, 8); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL); |
| 206 | |
| 207 | /* |
| 208 | * Real code |
| 209 | */ |
| 210 | |
| 211 | static int lm83_attach_adapter(struct i2c_adapter *adapter) |
| 212 | { |
| 213 | if (!(adapter->class & I2C_CLASS_HWMON)) |
| 214 | return 0; |
| 215 | return i2c_detect(adapter, &addr_data, lm83_detect); |
| 216 | } |
| 217 | |
| 218 | /* |
| 219 | * The following function does more than just detection. If detection |
| 220 | * succeeds, it also registers the new chip. |
| 221 | */ |
| 222 | static int lm83_detect(struct i2c_adapter *adapter, int address, int kind) |
| 223 | { |
| 224 | struct i2c_client *new_client; |
| 225 | struct lm83_data *data; |
| 226 | int err = 0; |
| 227 | const char *name = ""; |
| 228 | |
| 229 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) |
| 230 | goto exit; |
| 231 | |
| 232 | if (!(data = kmalloc(sizeof(struct lm83_data), GFP_KERNEL))) { |
| 233 | err = -ENOMEM; |
| 234 | goto exit; |
| 235 | } |
| 236 | memset(data, 0, sizeof(struct lm83_data)); |
| 237 | |
| 238 | /* The common I2C client data is placed right after the |
| 239 | * LM83-specific data. */ |
| 240 | new_client = &data->client; |
| 241 | i2c_set_clientdata(new_client, data); |
| 242 | new_client->addr = address; |
| 243 | new_client->adapter = adapter; |
| 244 | new_client->driver = &lm83_driver; |
| 245 | new_client->flags = 0; |
| 246 | |
| 247 | /* Now we do the detection and identification. A negative kind |
| 248 | * means that the driver was loaded with no force parameter |
| 249 | * (default), so we must both detect and identify the chip |
| 250 | * (actually there is only one possible kind of chip for now, LM83). |
| 251 | * A zero kind means that the driver was loaded with the force |
| 252 | * parameter, the detection step shall be skipped. A positive kind |
| 253 | * means that the driver was loaded with the force parameter and a |
| 254 | * given kind of chip is requested, so both the detection and the |
| 255 | * identification steps are skipped. */ |
| 256 | |
| 257 | /* Default to an LM83 if forced */ |
| 258 | if (kind == 0) |
| 259 | kind = lm83; |
| 260 | |
| 261 | if (kind < 0) { /* detection */ |
| 262 | if (((i2c_smbus_read_byte_data(new_client, LM83_REG_R_STATUS1) |
| 263 | & 0xA8) != 0x00) || |
| 264 | ((i2c_smbus_read_byte_data(new_client, LM83_REG_R_STATUS2) |
| 265 | & 0x48) != 0x00) || |
| 266 | ((i2c_smbus_read_byte_data(new_client, LM83_REG_R_CONFIG) |
| 267 | & 0x41) != 0x00)) { |
| 268 | dev_dbg(&adapter->dev, |
| 269 | "LM83 detection failed at 0x%02x.\n", address); |
| 270 | goto exit_free; |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | if (kind <= 0) { /* identification */ |
| 275 | u8 man_id, chip_id; |
| 276 | |
| 277 | man_id = i2c_smbus_read_byte_data(new_client, |
| 278 | LM83_REG_R_MAN_ID); |
| 279 | chip_id = i2c_smbus_read_byte_data(new_client, |
| 280 | LM83_REG_R_CHIP_ID); |
| 281 | |
| 282 | if (man_id == 0x01) { /* National Semiconductor */ |
| 283 | if (chip_id == 0x03) { |
| 284 | kind = lm83; |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | if (kind <= 0) { /* identification failed */ |
| 289 | dev_info(&adapter->dev, |
| 290 | "Unsupported chip (man_id=0x%02X, " |
| 291 | "chip_id=0x%02X).\n", man_id, chip_id); |
| 292 | goto exit_free; |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | if (kind == lm83) { |
| 297 | name = "lm83"; |
| 298 | } |
| 299 | |
| 300 | /* We can fill in the remaining client fields */ |
| 301 | strlcpy(new_client->name, name, I2C_NAME_SIZE); |
| 302 | data->valid = 0; |
| 303 | init_MUTEX(&data->update_lock); |
| 304 | |
| 305 | /* Tell the I2C layer a new client has arrived */ |
| 306 | if ((err = i2c_attach_client(new_client))) |
| 307 | goto exit_free; |
| 308 | |
| 309 | /* |
| 310 | * Initialize the LM83 chip |
| 311 | * (Nothing to do for this one.) |
| 312 | */ |
| 313 | |
| 314 | /* Register sysfs hooks */ |
Jean Delvare | 1a86c05 | 2005-06-05 21:16:39 +0200 | [diff] [blame^] | 315 | device_create_file(&new_client->dev, |
| 316 | &sensor_dev_attr_temp1_input.dev_attr); |
| 317 | device_create_file(&new_client->dev, |
| 318 | &sensor_dev_attr_temp2_input.dev_attr); |
| 319 | device_create_file(&new_client->dev, |
| 320 | &sensor_dev_attr_temp3_input.dev_attr); |
| 321 | device_create_file(&new_client->dev, |
| 322 | &sensor_dev_attr_temp4_input.dev_attr); |
| 323 | device_create_file(&new_client->dev, |
| 324 | &sensor_dev_attr_temp1_max.dev_attr); |
| 325 | device_create_file(&new_client->dev, |
| 326 | &sensor_dev_attr_temp2_max.dev_attr); |
| 327 | device_create_file(&new_client->dev, |
| 328 | &sensor_dev_attr_temp3_max.dev_attr); |
| 329 | device_create_file(&new_client->dev, |
| 330 | &sensor_dev_attr_temp4_max.dev_attr); |
| 331 | device_create_file(&new_client->dev, |
| 332 | &sensor_dev_attr_temp1_crit.dev_attr); |
| 333 | device_create_file(&new_client->dev, |
| 334 | &sensor_dev_attr_temp2_crit.dev_attr); |
| 335 | device_create_file(&new_client->dev, |
| 336 | &sensor_dev_attr_temp3_crit.dev_attr); |
| 337 | device_create_file(&new_client->dev, |
| 338 | &sensor_dev_attr_temp4_crit.dev_attr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | device_create_file(&new_client->dev, &dev_attr_alarms); |
| 340 | |
| 341 | return 0; |
| 342 | |
| 343 | exit_free: |
| 344 | kfree(data); |
| 345 | exit: |
| 346 | return err; |
| 347 | } |
| 348 | |
| 349 | static int lm83_detach_client(struct i2c_client *client) |
| 350 | { |
| 351 | int err; |
| 352 | |
| 353 | if ((err = i2c_detach_client(client))) { |
| 354 | dev_err(&client->dev, |
| 355 | "Client deregistration failed, client not detached.\n"); |
| 356 | return err; |
| 357 | } |
| 358 | |
| 359 | kfree(i2c_get_clientdata(client)); |
| 360 | return 0; |
| 361 | } |
| 362 | |
| 363 | static struct lm83_data *lm83_update_device(struct device *dev) |
| 364 | { |
| 365 | struct i2c_client *client = to_i2c_client(dev); |
| 366 | struct lm83_data *data = i2c_get_clientdata(client); |
| 367 | |
| 368 | down(&data->update_lock); |
| 369 | |
| 370 | if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) { |
| 371 | int nr; |
| 372 | |
| 373 | dev_dbg(&client->dev, "Updating lm83 data.\n"); |
Jean Delvare | 1a86c05 | 2005-06-05 21:16:39 +0200 | [diff] [blame^] | 374 | for (nr = 0; nr < 9; nr++) { |
| 375 | data->temp[nr] = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | i2c_smbus_read_byte_data(client, |
| 377 | LM83_REG_R_TEMP[nr]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | data->alarms = |
| 380 | i2c_smbus_read_byte_data(client, LM83_REG_R_STATUS1) |
| 381 | + (i2c_smbus_read_byte_data(client, LM83_REG_R_STATUS2) |
| 382 | << 8); |
| 383 | |
| 384 | data->last_updated = jiffies; |
| 385 | data->valid = 1; |
| 386 | } |
| 387 | |
| 388 | up(&data->update_lock); |
| 389 | |
| 390 | return data; |
| 391 | } |
| 392 | |
| 393 | static int __init sensors_lm83_init(void) |
| 394 | { |
| 395 | return i2c_add_driver(&lm83_driver); |
| 396 | } |
| 397 | |
| 398 | static void __exit sensors_lm83_exit(void) |
| 399 | { |
| 400 | i2c_del_driver(&lm83_driver); |
| 401 | } |
| 402 | |
| 403 | MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>"); |
| 404 | MODULE_DESCRIPTION("LM83 driver"); |
| 405 | MODULE_LICENSE("GPL"); |
| 406 | |
| 407 | module_init(sensors_lm83_init); |
| 408 | module_exit(sensors_lm83_exit); |