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