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 | 7c81c60 | 2014-01-29 20:40:08 +0100 | [diff] [blame] | 4 | * Copyright (C) 2003-2009 Jean Delvare <jdelvare@suse.de> |
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. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Jean Delvare | 10c08f8 | 2005-06-06 19:34:45 +0200 | [diff] [blame] | 35 | #include <linux/hwmon-sysfs.h> |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 36 | #include <linux/hwmon.h> |
| 37 | #include <linux/err.h> |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 38 | #include <linux/mutex.h> |
Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 39 | #include <linux/sysfs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | |
| 41 | /* |
| 42 | * Addresses to scan |
| 43 | * Address is selected using 2 three-level pins, resulting in 9 possible |
| 44 | * addresses. |
| 45 | */ |
| 46 | |
Mark M. Hoffman | 25e9c86 | 2008-02-17 22:28:03 -0500 | [diff] [blame] | 47 | static const unsigned short normal_i2c[] = { |
| 48 | 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x4c, 0x4d, 0x4e, I2C_CLIENT_END }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
Jean Delvare | e5e9f44 | 2009-12-14 21:17:27 +0100 | [diff] [blame] | 50 | enum chips { lm83, lm82 }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
| 52 | /* |
| 53 | * The LM83 registers |
| 54 | * Manufacturer ID is 0x01 for National Semiconductor. |
| 55 | */ |
| 56 | |
| 57 | #define LM83_REG_R_MAN_ID 0xFE |
| 58 | #define LM83_REG_R_CHIP_ID 0xFF |
| 59 | #define LM83_REG_R_CONFIG 0x03 |
| 60 | #define LM83_REG_W_CONFIG 0x09 |
| 61 | #define LM83_REG_R_STATUS1 0x02 |
| 62 | #define LM83_REG_R_STATUS2 0x35 |
| 63 | #define LM83_REG_R_LOCAL_TEMP 0x00 |
| 64 | #define LM83_REG_R_LOCAL_HIGH 0x05 |
| 65 | #define LM83_REG_W_LOCAL_HIGH 0x0B |
| 66 | #define LM83_REG_R_REMOTE1_TEMP 0x30 |
| 67 | #define LM83_REG_R_REMOTE1_HIGH 0x38 |
| 68 | #define LM83_REG_W_REMOTE1_HIGH 0x50 |
| 69 | #define LM83_REG_R_REMOTE2_TEMP 0x01 |
| 70 | #define LM83_REG_R_REMOTE2_HIGH 0x07 |
| 71 | #define LM83_REG_W_REMOTE2_HIGH 0x0D |
| 72 | #define LM83_REG_R_REMOTE3_TEMP 0x31 |
| 73 | #define LM83_REG_R_REMOTE3_HIGH 0x3A |
| 74 | #define LM83_REG_W_REMOTE3_HIGH 0x52 |
| 75 | #define LM83_REG_R_TCRIT 0x42 |
| 76 | #define LM83_REG_W_TCRIT 0x5A |
| 77 | |
| 78 | /* |
| 79 | * Conversions and various macros |
Steven Cole | 44bbe87 | 2005-05-03 18:21:25 -0600 | [diff] [blame] | 80 | * The LM83 uses signed 8-bit values with LSB = 1 degree Celsius. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | */ |
| 82 | |
| 83 | #define TEMP_FROM_REG(val) ((val) * 1000) |
| 84 | #define TEMP_TO_REG(val) ((val) <= -128000 ? -128 : \ |
| 85 | (val) >= 127000 ? 127 : \ |
| 86 | (val) < 0 ? ((val) - 500) / 1000 : \ |
| 87 | ((val) + 500) / 1000) |
| 88 | |
| 89 | static const u8 LM83_REG_R_TEMP[] = { |
| 90 | LM83_REG_R_LOCAL_TEMP, |
| 91 | LM83_REG_R_REMOTE1_TEMP, |
| 92 | LM83_REG_R_REMOTE2_TEMP, |
Jean Delvare | 1a86c05 | 2005-06-05 21:16:39 +0200 | [diff] [blame] | 93 | LM83_REG_R_REMOTE3_TEMP, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | LM83_REG_R_LOCAL_HIGH, |
| 95 | LM83_REG_R_REMOTE1_HIGH, |
| 96 | LM83_REG_R_REMOTE2_HIGH, |
Jean Delvare | 1a86c05 | 2005-06-05 21:16:39 +0200 | [diff] [blame] | 97 | LM83_REG_R_REMOTE3_HIGH, |
| 98 | LM83_REG_R_TCRIT, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | static const u8 LM83_REG_W_HIGH[] = { |
| 102 | LM83_REG_W_LOCAL_HIGH, |
| 103 | LM83_REG_W_REMOTE1_HIGH, |
| 104 | LM83_REG_W_REMOTE2_HIGH, |
Jean Delvare | 1a86c05 | 2005-06-05 21:16:39 +0200 | [diff] [blame] | 105 | LM83_REG_W_REMOTE3_HIGH, |
| 106 | LM83_REG_W_TCRIT, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | * Client data (each client gets its own) |
| 111 | */ |
| 112 | |
| 113 | struct lm83_data { |
Guenter Roeck | a0ac840 | 2014-04-12 12:46:34 -0700 | [diff] [blame] | 114 | struct i2c_client *client; |
| 115 | const struct attribute_group *groups[3]; |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 116 | struct mutex update_lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | char valid; /* zero until following fields are valid */ |
| 118 | unsigned long last_updated; /* in jiffies */ |
| 119 | |
| 120 | /* registers values */ |
Jean Delvare | 1a86c05 | 2005-06-05 21:16:39 +0200 | [diff] [blame] | 121 | s8 temp[9]; /* 0..3: input 1-4, |
| 122 | 4..7: high limit 1-4, |
| 123 | 8 : critical limit */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | u16 alarms; /* bitvector, combined */ |
| 125 | }; |
| 126 | |
Guenter Roeck | 4193637 | 2014-04-12 12:39:41 -0700 | [diff] [blame] | 127 | static struct lm83_data *lm83_update_device(struct device *dev) |
| 128 | { |
Guenter Roeck | a0ac840 | 2014-04-12 12:46:34 -0700 | [diff] [blame] | 129 | struct lm83_data *data = dev_get_drvdata(dev); |
| 130 | struct i2c_client *client = data->client; |
Guenter Roeck | 4193637 | 2014-04-12 12:39:41 -0700 | [diff] [blame] | 131 | |
| 132 | mutex_lock(&data->update_lock); |
| 133 | |
| 134 | if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) { |
| 135 | int nr; |
| 136 | |
| 137 | dev_dbg(&client->dev, "Updating lm83 data.\n"); |
| 138 | for (nr = 0; nr < 9; nr++) { |
| 139 | data->temp[nr] = |
| 140 | i2c_smbus_read_byte_data(client, |
| 141 | LM83_REG_R_TEMP[nr]); |
| 142 | } |
| 143 | data->alarms = |
| 144 | i2c_smbus_read_byte_data(client, LM83_REG_R_STATUS1) |
| 145 | + (i2c_smbus_read_byte_data(client, LM83_REG_R_STATUS2) |
| 146 | << 8); |
| 147 | |
| 148 | data->last_updated = jiffies; |
| 149 | data->valid = 1; |
| 150 | } |
| 151 | |
| 152 | mutex_unlock(&data->update_lock); |
| 153 | |
| 154 | return data; |
| 155 | } |
| 156 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | /* |
| 158 | * Sysfs stuff |
| 159 | */ |
| 160 | |
Jean Delvare | 1a86c05 | 2005-06-05 21:16:39 +0200 | [diff] [blame] | 161 | static ssize_t show_temp(struct device *dev, struct device_attribute *devattr, |
| 162 | char *buf) |
| 163 | { |
| 164 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 165 | struct lm83_data *data = lm83_update_device(dev); |
| 166 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[attr->index])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | |
Jean Delvare | 1a86c05 | 2005-06-05 21:16:39 +0200 | [diff] [blame] | 169 | static ssize_t set_temp(struct device *dev, struct device_attribute *devattr, |
| 170 | const char *buf, size_t count) |
| 171 | { |
| 172 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Guenter Roeck | a0ac840 | 2014-04-12 12:46:34 -0700 | [diff] [blame] | 173 | struct lm83_data *data = dev_get_drvdata(dev); |
| 174 | struct i2c_client *client = data->client; |
Frans Meulenbroeks | b3789a0 | 2012-01-10 23:01:40 +0100 | [diff] [blame] | 175 | long val; |
Jean Delvare | 1a86c05 | 2005-06-05 21:16:39 +0200 | [diff] [blame] | 176 | int nr = attr->index; |
Frans Meulenbroeks | b3789a0 | 2012-01-10 23:01:40 +0100 | [diff] [blame] | 177 | int err; |
| 178 | |
| 179 | err = kstrtol(buf, 10, &val); |
| 180 | if (err < 0) |
| 181 | return err; |
Jean Delvare | 1a86c05 | 2005-06-05 21:16:39 +0200 | [diff] [blame] | 182 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 183 | mutex_lock(&data->update_lock); |
Jean Delvare | 1a86c05 | 2005-06-05 21:16:39 +0200 | [diff] [blame] | 184 | data->temp[nr] = TEMP_TO_REG(val); |
| 185 | i2c_smbus_write_byte_data(client, LM83_REG_W_HIGH[nr - 4], |
| 186 | data->temp[nr]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 187 | mutex_unlock(&data->update_lock); |
Jean Delvare | 1a86c05 | 2005-06-05 21:16:39 +0200 | [diff] [blame] | 188 | return count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | |
Jean Delvare | 1a86c05 | 2005-06-05 21:16:39 +0200 | [diff] [blame] | 191 | static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy, |
| 192 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | { |
| 194 | struct lm83_data *data = lm83_update_device(dev); |
| 195 | return sprintf(buf, "%d\n", data->alarms); |
| 196 | } |
| 197 | |
Jean Delvare | 2d45771 | 2006-09-24 20:52:15 +0200 | [diff] [blame] | 198 | static ssize_t show_alarm(struct device *dev, struct device_attribute |
| 199 | *devattr, char *buf) |
| 200 | { |
| 201 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 202 | struct lm83_data *data = lm83_update_device(dev); |
| 203 | int bitnr = attr->index; |
| 204 | |
| 205 | return sprintf(buf, "%d\n", (data->alarms >> bitnr) & 1); |
| 206 | } |
| 207 | |
Jean Delvare | 1a86c05 | 2005-06-05 21:16:39 +0200 | [diff] [blame] | 208 | static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0); |
| 209 | static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1); |
| 210 | static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2); |
| 211 | static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3); |
| 212 | static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp, |
| 213 | set_temp, 4); |
| 214 | static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp, |
| 215 | set_temp, 5); |
| 216 | static SENSOR_DEVICE_ATTR(temp3_max, S_IWUSR | S_IRUGO, show_temp, |
| 217 | set_temp, 6); |
| 218 | static SENSOR_DEVICE_ATTR(temp4_max, S_IWUSR | S_IRUGO, show_temp, |
| 219 | set_temp, 7); |
| 220 | static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, show_temp, NULL, 8); |
| 221 | static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, show_temp, NULL, 8); |
| 222 | static SENSOR_DEVICE_ATTR(temp3_crit, S_IWUSR | S_IRUGO, show_temp, |
| 223 | set_temp, 8); |
| 224 | static SENSOR_DEVICE_ATTR(temp4_crit, S_IRUGO, show_temp, NULL, 8); |
Jean Delvare | 2d45771 | 2006-09-24 20:52:15 +0200 | [diff] [blame] | 225 | |
| 226 | /* Individual alarm files */ |
| 227 | static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 0); |
| 228 | 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] | 229 | static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 2); |
Jean Delvare | 2d45771 | 2006-09-24 20:52:15 +0200 | [diff] [blame] | 230 | static SENSOR_DEVICE_ATTR(temp3_max_alarm, S_IRUGO, show_alarm, NULL, 4); |
| 231 | static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6); |
| 232 | static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 8); |
| 233 | 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] | 234 | static SENSOR_DEVICE_ATTR(temp4_fault, S_IRUGO, show_alarm, NULL, 10); |
Jean Delvare | 2d45771 | 2006-09-24 20:52:15 +0200 | [diff] [blame] | 235 | 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] | 236 | static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 13); |
Jean Delvare | 2d45771 | 2006-09-24 20:52:15 +0200 | [diff] [blame] | 237 | static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 15); |
| 238 | /* Raw alarm file for compatibility */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL); |
| 240 | |
Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 241 | static struct attribute *lm83_attributes[] = { |
| 242 | &sensor_dev_attr_temp1_input.dev_attr.attr, |
| 243 | &sensor_dev_attr_temp3_input.dev_attr.attr, |
| 244 | &sensor_dev_attr_temp1_max.dev_attr.attr, |
| 245 | &sensor_dev_attr_temp3_max.dev_attr.attr, |
| 246 | &sensor_dev_attr_temp1_crit.dev_attr.attr, |
| 247 | &sensor_dev_attr_temp3_crit.dev_attr.attr, |
| 248 | |
| 249 | &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr, |
| 250 | &sensor_dev_attr_temp3_crit_alarm.dev_attr.attr, |
Jean Delvare | 7817a39 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 251 | &sensor_dev_attr_temp3_fault.dev_attr.attr, |
Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 252 | &sensor_dev_attr_temp3_max_alarm.dev_attr.attr, |
| 253 | &sensor_dev_attr_temp1_max_alarm.dev_attr.attr, |
| 254 | &dev_attr_alarms.attr, |
| 255 | NULL |
| 256 | }; |
| 257 | |
| 258 | static const struct attribute_group lm83_group = { |
| 259 | .attrs = lm83_attributes, |
| 260 | }; |
| 261 | |
| 262 | static struct attribute *lm83_attributes_opt[] = { |
| 263 | &sensor_dev_attr_temp2_input.dev_attr.attr, |
| 264 | &sensor_dev_attr_temp4_input.dev_attr.attr, |
| 265 | &sensor_dev_attr_temp2_max.dev_attr.attr, |
| 266 | &sensor_dev_attr_temp4_max.dev_attr.attr, |
| 267 | &sensor_dev_attr_temp2_crit.dev_attr.attr, |
| 268 | &sensor_dev_attr_temp4_crit.dev_attr.attr, |
| 269 | |
| 270 | &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr, |
| 271 | &sensor_dev_attr_temp4_crit_alarm.dev_attr.attr, |
Jean Delvare | 7817a39 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 272 | &sensor_dev_attr_temp4_fault.dev_attr.attr, |
Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 273 | &sensor_dev_attr_temp4_max_alarm.dev_attr.attr, |
Jean Delvare | 7817a39 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 274 | &sensor_dev_attr_temp2_fault.dev_attr.attr, |
Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 275 | &sensor_dev_attr_temp2_max_alarm.dev_attr.attr, |
| 276 | NULL |
| 277 | }; |
| 278 | |
| 279 | static const struct attribute_group lm83_group_opt = { |
| 280 | .attrs = lm83_attributes_opt, |
| 281 | }; |
| 282 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | /* |
| 284 | * Real code |
| 285 | */ |
| 286 | |
Jean Delvare | b6aacdc | 2008-07-16 19:30:14 +0200 | [diff] [blame] | 287 | /* Return 0 if detection is successful, -ENODEV otherwise */ |
Jean Delvare | 310ec79 | 2009-12-14 21:17:23 +0100 | [diff] [blame] | 288 | static int lm83_detect(struct i2c_client *new_client, |
Jean Delvare | b6aacdc | 2008-07-16 19:30:14 +0200 | [diff] [blame] | 289 | struct i2c_board_info *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | { |
Jean Delvare | b6aacdc | 2008-07-16 19:30:14 +0200 | [diff] [blame] | 291 | struct i2c_adapter *adapter = new_client->adapter; |
Jean Delvare | b57dc39 | 2009-12-09 20:35:52 +0100 | [diff] [blame] | 292 | const char *name; |
| 293 | u8 man_id, chip_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | |
| 295 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) |
Jean Delvare | b6aacdc | 2008-07-16 19:30:14 +0200 | [diff] [blame] | 296 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | |
Jean Delvare | b57dc39 | 2009-12-09 20:35:52 +0100 | [diff] [blame] | 298 | /* Detection */ |
| 299 | if ((i2c_smbus_read_byte_data(new_client, LM83_REG_R_STATUS1) & 0xA8) || |
| 300 | (i2c_smbus_read_byte_data(new_client, LM83_REG_R_STATUS2) & 0x48) || |
| 301 | (i2c_smbus_read_byte_data(new_client, LM83_REG_R_CONFIG) & 0x41)) { |
| 302 | dev_dbg(&adapter->dev, "LM83 detection failed at 0x%02x\n", |
| 303 | new_client->addr); |
| 304 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | } |
| 306 | |
Jean Delvare | b57dc39 | 2009-12-09 20:35:52 +0100 | [diff] [blame] | 307 | /* Identification */ |
| 308 | man_id = i2c_smbus_read_byte_data(new_client, LM83_REG_R_MAN_ID); |
| 309 | if (man_id != 0x01) /* National Semiconductor */ |
| 310 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | |
Jean Delvare | b57dc39 | 2009-12-09 20:35:52 +0100 | [diff] [blame] | 312 | chip_id = i2c_smbus_read_byte_data(new_client, LM83_REG_R_CHIP_ID); |
| 313 | switch (chip_id) { |
| 314 | case 0x03: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | name = "lm83"; |
Jean Delvare | b57dc39 | 2009-12-09 20:35:52 +0100 | [diff] [blame] | 316 | break; |
| 317 | case 0x01: |
Jordan Crouse | 43cb7eb | 2006-03-23 16:19:49 +0100 | [diff] [blame] | 318 | name = "lm82"; |
Jean Delvare | b57dc39 | 2009-12-09 20:35:52 +0100 | [diff] [blame] | 319 | break; |
| 320 | default: |
| 321 | /* identification failed */ |
| 322 | dev_info(&adapter->dev, |
| 323 | "Unsupported chip (man_id=0x%02X, chip_id=0x%02X)\n", |
| 324 | man_id, chip_id); |
| 325 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | } |
| 327 | |
Jean Delvare | b6aacdc | 2008-07-16 19:30:14 +0200 | [diff] [blame] | 328 | strlcpy(info->type, name, I2C_NAME_SIZE); |
| 329 | |
| 330 | return 0; |
| 331 | } |
| 332 | |
| 333 | static int lm83_probe(struct i2c_client *new_client, |
| 334 | const struct i2c_device_id *id) |
| 335 | { |
Guenter Roeck | a0ac840 | 2014-04-12 12:46:34 -0700 | [diff] [blame] | 336 | struct device *hwmon_dev; |
Jean Delvare | b6aacdc | 2008-07-16 19:30:14 +0200 | [diff] [blame] | 337 | struct lm83_data *data; |
Jean Delvare | b6aacdc | 2008-07-16 19:30:14 +0200 | [diff] [blame] | 338 | |
Guenter Roeck | c087f73 | 2012-06-02 09:58:10 -0700 | [diff] [blame] | 339 | data = devm_kzalloc(&new_client->dev, sizeof(struct lm83_data), |
| 340 | GFP_KERNEL); |
| 341 | if (!data) |
| 342 | return -ENOMEM; |
Jean Delvare | b6aacdc | 2008-07-16 19:30:14 +0200 | [diff] [blame] | 343 | |
Guenter Roeck | a0ac840 | 2014-04-12 12:46:34 -0700 | [diff] [blame] | 344 | data->client = new_client; |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 345 | mutex_init(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | /* |
Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 348 | * Register sysfs hooks |
Jordan Crouse | 43cb7eb | 2006-03-23 16:19:49 +0100 | [diff] [blame] | 349 | * The LM82 can only monitor one external diode which is |
| 350 | * at the same register as the LM83 temp3 entry - so we |
| 351 | * declare 1 and 3 common, and then 2 and 4 only for the LM83. |
| 352 | */ |
Guenter Roeck | a0ac840 | 2014-04-12 12:46:34 -0700 | [diff] [blame] | 353 | data->groups[0] = &lm83_group; |
| 354 | if (id->driver_data == lm83) |
| 355 | data->groups[1] = &lm83_group_opt; |
Jordan Crouse | 43cb7eb | 2006-03-23 16:19:49 +0100 | [diff] [blame] | 356 | |
Guenter Roeck | a0ac840 | 2014-04-12 12:46:34 -0700 | [diff] [blame] | 357 | hwmon_dev = devm_hwmon_device_register_with_groups(&new_client->dev, |
| 358 | new_client->name, |
| 359 | data, data->groups); |
| 360 | return PTR_ERR_OR_ZERO(hwmon_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | } |
| 362 | |
Guenter Roeck | 4193637 | 2014-04-12 12:39:41 -0700 | [diff] [blame] | 363 | /* |
| 364 | * Driver data (common to all clients) |
| 365 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | |
Guenter Roeck | 4193637 | 2014-04-12 12:39:41 -0700 | [diff] [blame] | 367 | static const struct i2c_device_id lm83_id[] = { |
| 368 | { "lm83", lm83 }, |
| 369 | { "lm82", lm82 }, |
| 370 | { } |
| 371 | }; |
| 372 | MODULE_DEVICE_TABLE(i2c, lm83_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | |
Guenter Roeck | 4193637 | 2014-04-12 12:39:41 -0700 | [diff] [blame] | 374 | static struct i2c_driver lm83_driver = { |
| 375 | .class = I2C_CLASS_HWMON, |
| 376 | .driver = { |
| 377 | .name = "lm83", |
| 378 | }, |
| 379 | .probe = lm83_probe, |
Guenter Roeck | 4193637 | 2014-04-12 12:39:41 -0700 | [diff] [blame] | 380 | .id_table = lm83_id, |
| 381 | .detect = lm83_detect, |
| 382 | .address_list = normal_i2c, |
| 383 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | |
Axel Lin | f0967ee | 2012-01-20 15:38:18 +0800 | [diff] [blame] | 385 | module_i2c_driver(lm83_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | |
Jean Delvare | 7c81c60 | 2014-01-29 20:40:08 +0100 | [diff] [blame] | 387 | MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | MODULE_DESCRIPTION("LM83 driver"); |
| 389 | MODULE_LICENSE("GPL"); |