| 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 | b57dc39 | 2009-12-09 20:35:52 +0100 | [diff] [blame] | 4 | * Copyright (C) 2003-2009  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 |  | 
| Jean Delvare | e5e9f44 | 2009-12-14 21:17:27 +0100 | [diff] [blame] | 54 | enum chips { lm83, lm82 }; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |  | 
| Jean Delvare | 310ec79 | 2009-12-14 21:17:23 +0100 | [diff] [blame] | 117 | static int lm83_detect(struct i2c_client *new_client, | 
| Jean Delvare | b6aacdc | 2008-07-16 19:30:14 +0200 | [diff] [blame] | 118 | struct i2c_board_info *info); | 
|  | 119 | static int lm83_probe(struct i2c_client *client, | 
|  | 120 | const struct i2c_device_id *id); | 
|  | 121 | static int lm83_remove(struct i2c_client *client); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | static struct lm83_data *lm83_update_device(struct device *dev); | 
|  | 123 |  | 
|  | 124 | /* | 
|  | 125 | * Driver data (common to all clients) | 
|  | 126 | */ | 
|  | 127 |  | 
| Jean Delvare | b6aacdc | 2008-07-16 19:30:14 +0200 | [diff] [blame] | 128 | static const struct i2c_device_id lm83_id[] = { | 
|  | 129 | { "lm83", lm83 }, | 
|  | 130 | { "lm82", lm82 }, | 
|  | 131 | { } | 
|  | 132 | }; | 
|  | 133 | MODULE_DEVICE_TABLE(i2c, lm83_id); | 
|  | 134 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | static struct i2c_driver lm83_driver = { | 
| Jean Delvare | b6aacdc | 2008-07-16 19:30:14 +0200 | [diff] [blame] | 136 | .class		= I2C_CLASS_HWMON, | 
| Laurent Riffard | cdaf793 | 2005-11-26 20:37:41 +0100 | [diff] [blame] | 137 | .driver = { | 
| Laurent Riffard | cdaf793 | 2005-11-26 20:37:41 +0100 | [diff] [blame] | 138 | .name	= "lm83", | 
|  | 139 | }, | 
| Jean Delvare | b6aacdc | 2008-07-16 19:30:14 +0200 | [diff] [blame] | 140 | .probe		= lm83_probe, | 
|  | 141 | .remove		= lm83_remove, | 
|  | 142 | .id_table	= lm83_id, | 
|  | 143 | .detect		= lm83_detect, | 
| Jean Delvare | c3813d6 | 2009-12-14 21:17:25 +0100 | [diff] [blame] | 144 | .address_list	= normal_i2c, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | }; | 
|  | 146 |  | 
|  | 147 | /* | 
|  | 148 | * Client data (each client gets its own) | 
|  | 149 | */ | 
|  | 150 |  | 
|  | 151 | struct lm83_data { | 
| Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 152 | struct device *hwmon_dev; | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 153 | struct mutex update_lock; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | char valid; /* zero until following fields are valid */ | 
|  | 155 | unsigned long last_updated; /* in jiffies */ | 
|  | 156 |  | 
|  | 157 | /* registers values */ | 
| Jean Delvare | 1a86c05 | 2005-06-05 21:16:39 +0200 | [diff] [blame] | 158 | s8 temp[9];	/* 0..3: input 1-4, | 
|  | 159 | 4..7: high limit 1-4, | 
|  | 160 | 8   : critical limit */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | u16 alarms; /* bitvector, combined */ | 
|  | 162 | }; | 
|  | 163 |  | 
|  | 164 | /* | 
|  | 165 | * Sysfs stuff | 
|  | 166 | */ | 
|  | 167 |  | 
| Jean Delvare | 1a86c05 | 2005-06-05 21:16:39 +0200 | [diff] [blame] | 168 | static ssize_t show_temp(struct device *dev, struct device_attribute *devattr, | 
|  | 169 | char *buf) | 
|  | 170 | { | 
|  | 171 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | 
|  | 172 | struct lm83_data *data = lm83_update_device(dev); | 
|  | 173 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[attr->index])); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 |  | 
| Jean Delvare | 1a86c05 | 2005-06-05 21:16:39 +0200 | [diff] [blame] | 176 | static ssize_t set_temp(struct device *dev, struct device_attribute *devattr, | 
|  | 177 | const char *buf, size_t count) | 
|  | 178 | { | 
|  | 179 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | 
|  | 180 | struct i2c_client *client = to_i2c_client(dev); | 
|  | 181 | struct lm83_data *data = i2c_get_clientdata(client); | 
|  | 182 | long val = simple_strtol(buf, NULL, 10); | 
|  | 183 | int nr = attr->index; | 
|  | 184 |  | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 185 | mutex_lock(&data->update_lock); | 
| Jean Delvare | 1a86c05 | 2005-06-05 21:16:39 +0200 | [diff] [blame] | 186 | data->temp[nr] = TEMP_TO_REG(val); | 
|  | 187 | i2c_smbus_write_byte_data(client, LM83_REG_W_HIGH[nr - 4], | 
|  | 188 | data->temp[nr]); | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 189 | mutex_unlock(&data->update_lock); | 
| Jean Delvare | 1a86c05 | 2005-06-05 21:16:39 +0200 | [diff] [blame] | 190 | return count; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 |  | 
| Jean Delvare | 1a86c05 | 2005-06-05 21:16:39 +0200 | [diff] [blame] | 193 | static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy, | 
|  | 194 | char *buf) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | { | 
|  | 196 | struct lm83_data *data = lm83_update_device(dev); | 
|  | 197 | return sprintf(buf, "%d\n", data->alarms); | 
|  | 198 | } | 
|  | 199 |  | 
| Jean Delvare | 2d45771 | 2006-09-24 20:52:15 +0200 | [diff] [blame] | 200 | static ssize_t show_alarm(struct device *dev, struct device_attribute | 
|  | 201 | *devattr, char *buf) | 
|  | 202 | { | 
|  | 203 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | 
|  | 204 | struct lm83_data *data = lm83_update_device(dev); | 
|  | 205 | int bitnr = attr->index; | 
|  | 206 |  | 
|  | 207 | return sprintf(buf, "%d\n", (data->alarms >> bitnr) & 1); | 
|  | 208 | } | 
|  | 209 |  | 
| Jean Delvare | 1a86c05 | 2005-06-05 21:16:39 +0200 | [diff] [blame] | 210 | static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0); | 
|  | 211 | static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1); | 
|  | 212 | static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2); | 
|  | 213 | static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3); | 
|  | 214 | static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp, | 
|  | 215 | set_temp, 4); | 
|  | 216 | static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp, | 
|  | 217 | set_temp, 5); | 
|  | 218 | static SENSOR_DEVICE_ATTR(temp3_max, S_IWUSR | S_IRUGO, show_temp, | 
|  | 219 | set_temp, 6); | 
|  | 220 | static SENSOR_DEVICE_ATTR(temp4_max, S_IWUSR | S_IRUGO, show_temp, | 
|  | 221 | set_temp, 7); | 
|  | 222 | static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, show_temp, NULL, 8); | 
|  | 223 | static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, show_temp, NULL, 8); | 
|  | 224 | static SENSOR_DEVICE_ATTR(temp3_crit, S_IWUSR | S_IRUGO, show_temp, | 
|  | 225 | set_temp, 8); | 
|  | 226 | static SENSOR_DEVICE_ATTR(temp4_crit, S_IRUGO, show_temp, NULL, 8); | 
| Jean Delvare | 2d45771 | 2006-09-24 20:52:15 +0200 | [diff] [blame] | 227 |  | 
|  | 228 | /* Individual alarm files */ | 
|  | 229 | static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 0); | 
|  | 230 | 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] | 231 | static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 2); | 
| Jean Delvare | 2d45771 | 2006-09-24 20:52:15 +0200 | [diff] [blame] | 232 | static SENSOR_DEVICE_ATTR(temp3_max_alarm, S_IRUGO, show_alarm, NULL, 4); | 
|  | 233 | static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6); | 
|  | 234 | static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 8); | 
|  | 235 | 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] | 236 | static SENSOR_DEVICE_ATTR(temp4_fault, S_IRUGO, show_alarm, NULL, 10); | 
| Jean Delvare | 2d45771 | 2006-09-24 20:52:15 +0200 | [diff] [blame] | 237 | 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] | 238 | static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 13); | 
| Jean Delvare | 2d45771 | 2006-09-24 20:52:15 +0200 | [diff] [blame] | 239 | static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 15); | 
|  | 240 | /* Raw alarm file for compatibility */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL); | 
|  | 242 |  | 
| Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 243 | static struct attribute *lm83_attributes[] = { | 
|  | 244 | &sensor_dev_attr_temp1_input.dev_attr.attr, | 
|  | 245 | &sensor_dev_attr_temp3_input.dev_attr.attr, | 
|  | 246 | &sensor_dev_attr_temp1_max.dev_attr.attr, | 
|  | 247 | &sensor_dev_attr_temp3_max.dev_attr.attr, | 
|  | 248 | &sensor_dev_attr_temp1_crit.dev_attr.attr, | 
|  | 249 | &sensor_dev_attr_temp3_crit.dev_attr.attr, | 
|  | 250 |  | 
|  | 251 | &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr, | 
|  | 252 | &sensor_dev_attr_temp3_crit_alarm.dev_attr.attr, | 
| Jean Delvare | 7817a39 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 253 | &sensor_dev_attr_temp3_fault.dev_attr.attr, | 
| Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 254 | &sensor_dev_attr_temp3_max_alarm.dev_attr.attr, | 
|  | 255 | &sensor_dev_attr_temp1_max_alarm.dev_attr.attr, | 
|  | 256 | &dev_attr_alarms.attr, | 
|  | 257 | NULL | 
|  | 258 | }; | 
|  | 259 |  | 
|  | 260 | static const struct attribute_group lm83_group = { | 
|  | 261 | .attrs = lm83_attributes, | 
|  | 262 | }; | 
|  | 263 |  | 
|  | 264 | static struct attribute *lm83_attributes_opt[] = { | 
|  | 265 | &sensor_dev_attr_temp2_input.dev_attr.attr, | 
|  | 266 | &sensor_dev_attr_temp4_input.dev_attr.attr, | 
|  | 267 | &sensor_dev_attr_temp2_max.dev_attr.attr, | 
|  | 268 | &sensor_dev_attr_temp4_max.dev_attr.attr, | 
|  | 269 | &sensor_dev_attr_temp2_crit.dev_attr.attr, | 
|  | 270 | &sensor_dev_attr_temp4_crit.dev_attr.attr, | 
|  | 271 |  | 
|  | 272 | &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr, | 
|  | 273 | &sensor_dev_attr_temp4_crit_alarm.dev_attr.attr, | 
| Jean Delvare | 7817a39 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 274 | &sensor_dev_attr_temp4_fault.dev_attr.attr, | 
| Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 275 | &sensor_dev_attr_temp4_max_alarm.dev_attr.attr, | 
| Jean Delvare | 7817a39 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 276 | &sensor_dev_attr_temp2_fault.dev_attr.attr, | 
| Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 277 | &sensor_dev_attr_temp2_max_alarm.dev_attr.attr, | 
|  | 278 | NULL | 
|  | 279 | }; | 
|  | 280 |  | 
|  | 281 | static const struct attribute_group lm83_group_opt = { | 
|  | 282 | .attrs = lm83_attributes_opt, | 
|  | 283 | }; | 
|  | 284 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | /* | 
|  | 286 | * Real code | 
|  | 287 | */ | 
|  | 288 |  | 
| Jean Delvare | b6aacdc | 2008-07-16 19:30:14 +0200 | [diff] [blame] | 289 | /* Return 0 if detection is successful, -ENODEV otherwise */ | 
| Jean Delvare | 310ec79 | 2009-12-14 21:17:23 +0100 | [diff] [blame] | 290 | static int lm83_detect(struct i2c_client *new_client, | 
| Jean Delvare | b6aacdc | 2008-07-16 19:30:14 +0200 | [diff] [blame] | 291 | struct i2c_board_info *info) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | { | 
| Jean Delvare | b6aacdc | 2008-07-16 19:30:14 +0200 | [diff] [blame] | 293 | struct i2c_adapter *adapter = new_client->adapter; | 
| Jean Delvare | b57dc39 | 2009-12-09 20:35:52 +0100 | [diff] [blame] | 294 | const char *name; | 
|  | 295 | u8 man_id, chip_id; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 |  | 
|  | 297 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) | 
| Jean Delvare | b6aacdc | 2008-07-16 19:30:14 +0200 | [diff] [blame] | 298 | return -ENODEV; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 |  | 
| Jean Delvare | b57dc39 | 2009-12-09 20:35:52 +0100 | [diff] [blame] | 300 | /* Detection */ | 
|  | 301 | if ((i2c_smbus_read_byte_data(new_client, LM83_REG_R_STATUS1) & 0xA8) || | 
|  | 302 | (i2c_smbus_read_byte_data(new_client, LM83_REG_R_STATUS2) & 0x48) || | 
|  | 303 | (i2c_smbus_read_byte_data(new_client, LM83_REG_R_CONFIG) & 0x41)) { | 
|  | 304 | dev_dbg(&adapter->dev, "LM83 detection failed at 0x%02x\n", | 
|  | 305 | new_client->addr); | 
|  | 306 | return -ENODEV; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | } | 
|  | 308 |  | 
| Jean Delvare | b57dc39 | 2009-12-09 20:35:52 +0100 | [diff] [blame] | 309 | /* Identification */ | 
|  | 310 | man_id = i2c_smbus_read_byte_data(new_client, LM83_REG_R_MAN_ID); | 
|  | 311 | if (man_id != 0x01)	/* National Semiconductor */ | 
|  | 312 | return -ENODEV; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 |  | 
| Jean Delvare | b57dc39 | 2009-12-09 20:35:52 +0100 | [diff] [blame] | 314 | chip_id = i2c_smbus_read_byte_data(new_client, LM83_REG_R_CHIP_ID); | 
|  | 315 | switch (chip_id) { | 
|  | 316 | case 0x03: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | name = "lm83"; | 
| Jean Delvare | b57dc39 | 2009-12-09 20:35:52 +0100 | [diff] [blame] | 318 | break; | 
|  | 319 | case 0x01: | 
| Jordan Crouse | 43cb7eb | 2006-03-23 16:19:49 +0100 | [diff] [blame] | 320 | name = "lm82"; | 
| Jean Delvare | b57dc39 | 2009-12-09 20:35:52 +0100 | [diff] [blame] | 321 | break; | 
|  | 322 | default: | 
|  | 323 | /* identification failed */ | 
|  | 324 | dev_info(&adapter->dev, | 
|  | 325 | "Unsupported chip (man_id=0x%02X, chip_id=0x%02X)\n", | 
|  | 326 | man_id, chip_id); | 
|  | 327 | return -ENODEV; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | } | 
|  | 329 |  | 
| Jean Delvare | b6aacdc | 2008-07-16 19:30:14 +0200 | [diff] [blame] | 330 | strlcpy(info->type, name, I2C_NAME_SIZE); | 
|  | 331 |  | 
|  | 332 | return 0; | 
|  | 333 | } | 
|  | 334 |  | 
|  | 335 | static int lm83_probe(struct i2c_client *new_client, | 
|  | 336 | const struct i2c_device_id *id) | 
|  | 337 | { | 
|  | 338 | struct lm83_data *data; | 
|  | 339 | int err; | 
|  | 340 |  | 
|  | 341 | data = kzalloc(sizeof(struct lm83_data), GFP_KERNEL); | 
|  | 342 | if (!data) { | 
|  | 343 | err = -ENOMEM; | 
|  | 344 | goto exit; | 
|  | 345 | } | 
|  | 346 |  | 
|  | 347 | i2c_set_clientdata(new_client, data); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | data->valid = 0; | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 349 | mutex_init(&data->update_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | /* | 
| Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 352 | * Register sysfs hooks | 
| Jordan Crouse | 43cb7eb | 2006-03-23 16:19:49 +0100 | [diff] [blame] | 353 | * The LM82 can only monitor one external diode which is | 
|  | 354 | * at the same register as the LM83 temp3 entry - so we | 
|  | 355 | * declare 1 and 3 common, and then 2 and 4 only for the LM83. | 
|  | 356 | */ | 
|  | 357 |  | 
| Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 358 | if ((err = sysfs_create_group(&new_client->dev.kobj, &lm83_group))) | 
| Jean Delvare | b6aacdc | 2008-07-16 19:30:14 +0200 | [diff] [blame] | 359 | goto exit_free; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 |  | 
| Jean Delvare | b6aacdc | 2008-07-16 19:30:14 +0200 | [diff] [blame] | 361 | if (id->driver_data == lm83) { | 
| Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 362 | if ((err = sysfs_create_group(&new_client->dev.kobj, | 
|  | 363 | &lm83_group_opt))) | 
|  | 364 | goto exit_remove_files; | 
|  | 365 | } | 
| Jordan Crouse | 43cb7eb | 2006-03-23 16:19:49 +0100 | [diff] [blame] | 366 |  | 
| Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 367 | data->hwmon_dev = hwmon_device_register(&new_client->dev); | 
|  | 368 | if (IS_ERR(data->hwmon_dev)) { | 
|  | 369 | err = PTR_ERR(data->hwmon_dev); | 
| Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 370 | goto exit_remove_files; | 
| Jordan Crouse | 43cb7eb | 2006-03-23 16:19:49 +0100 | [diff] [blame] | 371 | } | 
|  | 372 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | return 0; | 
|  | 374 |  | 
| Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 375 | exit_remove_files: | 
|  | 376 | sysfs_remove_group(&new_client->dev.kobj, &lm83_group); | 
|  | 377 | sysfs_remove_group(&new_client->dev.kobj, &lm83_group_opt); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | exit_free: | 
|  | 379 | kfree(data); | 
|  | 380 | exit: | 
|  | 381 | return err; | 
|  | 382 | } | 
|  | 383 |  | 
| Jean Delvare | b6aacdc | 2008-07-16 19:30:14 +0200 | [diff] [blame] | 384 | static int lm83_remove(struct i2c_client *client) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | { | 
| Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 386 | struct lm83_data *data = i2c_get_clientdata(client); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 |  | 
| Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 388 | hwmon_device_unregister(data->hwmon_dev); | 
| Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 389 | sysfs_remove_group(&client->dev.kobj, &lm83_group); | 
|  | 390 | sysfs_remove_group(&client->dev.kobj, &lm83_group_opt); | 
| Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 391 |  | 
| Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 392 | kfree(data); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | return 0; | 
|  | 394 | } | 
|  | 395 |  | 
|  | 396 | static struct lm83_data *lm83_update_device(struct device *dev) | 
|  | 397 | { | 
|  | 398 | struct i2c_client *client = to_i2c_client(dev); | 
|  | 399 | struct lm83_data *data = i2c_get_clientdata(client); | 
|  | 400 |  | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 401 | mutex_lock(&data->update_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 |  | 
|  | 403 | if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) { | 
|  | 404 | int nr; | 
|  | 405 |  | 
|  | 406 | dev_dbg(&client->dev, "Updating lm83 data.\n"); | 
| Jean Delvare | 1a86c05 | 2005-06-05 21:16:39 +0200 | [diff] [blame] | 407 | for (nr = 0; nr < 9; nr++) { | 
|  | 408 | data->temp[nr] = | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | i2c_smbus_read_byte_data(client, | 
|  | 410 | LM83_REG_R_TEMP[nr]); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | data->alarms = | 
|  | 413 | i2c_smbus_read_byte_data(client, LM83_REG_R_STATUS1) | 
|  | 414 | + (i2c_smbus_read_byte_data(client, LM83_REG_R_STATUS2) | 
|  | 415 | << 8); | 
|  | 416 |  | 
|  | 417 | data->last_updated = jiffies; | 
|  | 418 | data->valid = 1; | 
|  | 419 | } | 
|  | 420 |  | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 421 | mutex_unlock(&data->update_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 |  | 
|  | 423 | return data; | 
|  | 424 | } | 
|  | 425 |  | 
|  | 426 | static int __init sensors_lm83_init(void) | 
|  | 427 | { | 
|  | 428 | return i2c_add_driver(&lm83_driver); | 
|  | 429 | } | 
|  | 430 |  | 
|  | 431 | static void __exit sensors_lm83_exit(void) | 
|  | 432 | { | 
|  | 433 | i2c_del_driver(&lm83_driver); | 
|  | 434 | } | 
|  | 435 |  | 
|  | 436 | MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>"); | 
|  | 437 | MODULE_DESCRIPTION("LM83 driver"); | 
|  | 438 | MODULE_LICENSE("GPL"); | 
|  | 439 |  | 
|  | 440 | module_init(sensors_lm83_init); | 
|  | 441 | module_exit(sensors_lm83_exit); |