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 | |
Guenter Roeck | c291f61 | 2021-12-22 20:24:02 -0800 | [diff] [blame] | 21 | #include <linux/bits.h> |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 22 | #include <linux/err.h> |
Guenter Roeck | 7c68c2c | 2021-12-22 11:42:38 -0800 | [diff] [blame] | 23 | #include <linux/i2c.h> |
| 24 | #include <linux/init.h> |
Guenter Roeck | 7c68c2c | 2021-12-22 11:42:38 -0800 | [diff] [blame] | 25 | #include <linux/hwmon.h> |
Guenter Roeck | 7c68c2c | 2021-12-22 11:42:38 -0800 | [diff] [blame] | 26 | #include <linux/module.h> |
Guenter Roeck | 719af4f1 | 2021-12-22 16:22:00 -0800 | [diff] [blame] | 27 | #include <linux/regmap.h> |
Guenter Roeck | 7c68c2c | 2021-12-22 11:42:38 -0800 | [diff] [blame] | 28 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
| 30 | /* |
| 31 | * Addresses to scan |
| 32 | * Address is selected using 2 three-level pins, resulting in 9 possible |
| 33 | * addresses. |
| 34 | */ |
| 35 | |
Mark M. Hoffman | 25e9c86 | 2008-02-17 22:28:03 -0500 | [diff] [blame] | 36 | static const unsigned short normal_i2c[] = { |
| 37 | 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x4c, 0x4d, 0x4e, I2C_CLIENT_END }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
Jean Delvare | e5e9f44 | 2009-12-14 21:17:27 +0100 | [diff] [blame] | 39 | enum chips { lm83, lm82 }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | |
| 41 | /* |
| 42 | * The LM83 registers |
| 43 | * Manufacturer ID is 0x01 for National Semiconductor. |
| 44 | */ |
| 45 | |
| 46 | #define LM83_REG_R_MAN_ID 0xFE |
| 47 | #define LM83_REG_R_CHIP_ID 0xFF |
| 48 | #define LM83_REG_R_CONFIG 0x03 |
| 49 | #define LM83_REG_W_CONFIG 0x09 |
| 50 | #define LM83_REG_R_STATUS1 0x02 |
| 51 | #define LM83_REG_R_STATUS2 0x35 |
| 52 | #define LM83_REG_R_LOCAL_TEMP 0x00 |
| 53 | #define LM83_REG_R_LOCAL_HIGH 0x05 |
| 54 | #define LM83_REG_W_LOCAL_HIGH 0x0B |
| 55 | #define LM83_REG_R_REMOTE1_TEMP 0x30 |
| 56 | #define LM83_REG_R_REMOTE1_HIGH 0x38 |
| 57 | #define LM83_REG_W_REMOTE1_HIGH 0x50 |
| 58 | #define LM83_REG_R_REMOTE2_TEMP 0x01 |
| 59 | #define LM83_REG_R_REMOTE2_HIGH 0x07 |
| 60 | #define LM83_REG_W_REMOTE2_HIGH 0x0D |
| 61 | #define LM83_REG_R_REMOTE3_TEMP 0x31 |
| 62 | #define LM83_REG_R_REMOTE3_HIGH 0x3A |
| 63 | #define LM83_REG_W_REMOTE3_HIGH 0x52 |
| 64 | #define LM83_REG_R_TCRIT 0x42 |
| 65 | #define LM83_REG_W_TCRIT 0x5A |
| 66 | |
Guenter Roeck | 719af4f1 | 2021-12-22 16:22:00 -0800 | [diff] [blame] | 67 | static const u8 LM83_REG_TEMP[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | LM83_REG_R_LOCAL_TEMP, |
| 69 | LM83_REG_R_REMOTE1_TEMP, |
| 70 | LM83_REG_R_REMOTE2_TEMP, |
Jean Delvare | 1a86c05 | 2005-06-05 21:16:39 +0200 | [diff] [blame] | 71 | LM83_REG_R_REMOTE3_TEMP, |
Guenter Roeck | c291f61 | 2021-12-22 20:24:02 -0800 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | static const u8 LM83_REG_MAX[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | LM83_REG_R_LOCAL_HIGH, |
| 76 | LM83_REG_R_REMOTE1_HIGH, |
| 77 | LM83_REG_R_REMOTE2_HIGH, |
Jean Delvare | 1a86c05 | 2005-06-05 21:16:39 +0200 | [diff] [blame] | 78 | LM83_REG_R_REMOTE3_HIGH, |
Guenter Roeck | c291f61 | 2021-12-22 20:24:02 -0800 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | /* alarm and fault registers and bits, indexed by channel */ |
| 82 | static const u8 LM83_ALARM_REG[] = { |
| 83 | LM83_REG_R_STATUS1, LM83_REG_R_STATUS2, LM83_REG_R_STATUS1, LM83_REG_R_STATUS2 |
| 84 | }; |
| 85 | |
| 86 | static const u8 LM83_MAX_ALARM_BIT[] = { |
| 87 | BIT(6), BIT(7), BIT(4), BIT(4) |
| 88 | }; |
| 89 | |
| 90 | static const u8 LM83_CRIT_ALARM_BIT[] = { |
| 91 | BIT(0), BIT(0), BIT(1), BIT(1) |
| 92 | }; |
| 93 | |
| 94 | static const u8 LM83_FAULT_BIT[] = { |
| 95 | 0, BIT(5), BIT(2), BIT(2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | }; |
| 97 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | * Client data (each client gets its own) |
| 100 | */ |
| 101 | |
| 102 | struct lm83_data { |
Guenter Roeck | 719af4f1 | 2021-12-22 16:22:00 -0800 | [diff] [blame] | 103 | struct regmap *regmap; |
Guenter Roeck | c291f61 | 2021-12-22 20:24:02 -0800 | [diff] [blame] | 104 | enum chips type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | }; |
| 106 | |
Guenter Roeck | 719af4f1 | 2021-12-22 16:22:00 -0800 | [diff] [blame] | 107 | /* regmap code */ |
| 108 | |
| 109 | static int lm83_regmap_reg_read(void *context, unsigned int reg, unsigned int *val) |
Guenter Roeck | 4193637 | 2014-04-12 12:39:41 -0700 | [diff] [blame] | 110 | { |
Guenter Roeck | 719af4f1 | 2021-12-22 16:22:00 -0800 | [diff] [blame] | 111 | struct i2c_client *client = context; |
| 112 | int ret; |
Guenter Roeck | 4193637 | 2014-04-12 12:39:41 -0700 | [diff] [blame] | 113 | |
Guenter Roeck | 719af4f1 | 2021-12-22 16:22:00 -0800 | [diff] [blame] | 114 | ret = i2c_smbus_read_byte_data(client, reg); |
| 115 | if (ret < 0) |
| 116 | return ret; |
Guenter Roeck | 4193637 | 2014-04-12 12:39:41 -0700 | [diff] [blame] | 117 | |
Guenter Roeck | 719af4f1 | 2021-12-22 16:22:00 -0800 | [diff] [blame] | 118 | *val = ret; |
| 119 | return 0; |
| 120 | } |
Guenter Roeck | 4193637 | 2014-04-12 12:39:41 -0700 | [diff] [blame] | 121 | |
Guenter Roeck | 719af4f1 | 2021-12-22 16:22:00 -0800 | [diff] [blame] | 122 | /* |
| 123 | * The regmap write function maps read register addresses to write register |
| 124 | * addresses. This is necessary for regmap register caching to work. |
| 125 | * An alternative would be to clear the regmap cache whenever a register is |
| 126 | * written, but that would be much more expensive. |
| 127 | */ |
| 128 | static int lm83_regmap_reg_write(void *context, unsigned int reg, unsigned int val) |
| 129 | { |
| 130 | struct i2c_client *client = context; |
Guenter Roeck | 4193637 | 2014-04-12 12:39:41 -0700 | [diff] [blame] | 131 | |
Guenter Roeck | 719af4f1 | 2021-12-22 16:22:00 -0800 | [diff] [blame] | 132 | switch (reg) { |
| 133 | case LM83_REG_R_CONFIG: |
| 134 | case LM83_REG_R_LOCAL_HIGH: |
| 135 | case LM83_REG_R_REMOTE2_HIGH: |
| 136 | reg += 0x06; |
| 137 | break; |
| 138 | case LM83_REG_R_REMOTE1_HIGH: |
| 139 | case LM83_REG_R_REMOTE3_HIGH: |
| 140 | case LM83_REG_R_TCRIT: |
| 141 | reg += 0x18; |
| 142 | break; |
| 143 | default: |
| 144 | break; |
Guenter Roeck | 4193637 | 2014-04-12 12:39:41 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Guenter Roeck | 719af4f1 | 2021-12-22 16:22:00 -0800 | [diff] [blame] | 147 | return i2c_smbus_write_byte_data(client, reg, val); |
Guenter Roeck | 4193637 | 2014-04-12 12:39:41 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Guenter Roeck | 719af4f1 | 2021-12-22 16:22:00 -0800 | [diff] [blame] | 150 | static bool lm83_regmap_is_volatile(struct device *dev, unsigned int reg) |
| 151 | { |
| 152 | switch (reg) { |
| 153 | case LM83_REG_R_LOCAL_TEMP: |
| 154 | case LM83_REG_R_REMOTE1_TEMP: |
| 155 | case LM83_REG_R_REMOTE2_TEMP: |
| 156 | case LM83_REG_R_REMOTE3_TEMP: |
| 157 | case LM83_REG_R_STATUS1: |
| 158 | case LM83_REG_R_STATUS2: |
| 159 | return true; |
| 160 | default: |
| 161 | return false; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | static const struct regmap_config lm83_regmap_config = { |
| 166 | .reg_bits = 8, |
| 167 | .val_bits = 8, |
| 168 | .cache_type = REGCACHE_RBTREE, |
| 169 | .volatile_reg = lm83_regmap_is_volatile, |
| 170 | .reg_read = lm83_regmap_reg_read, |
| 171 | .reg_write = lm83_regmap_reg_write, |
| 172 | }; |
| 173 | |
Guenter Roeck | c291f61 | 2021-12-22 20:24:02 -0800 | [diff] [blame] | 174 | /* hwmon API */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | |
Guenter Roeck | c291f61 | 2021-12-22 20:24:02 -0800 | [diff] [blame] | 176 | static int lm83_temp_read(struct device *dev, u32 attr, int channel, long *val) |
Jean Delvare | 1a86c05 | 2005-06-05 21:16:39 +0200 | [diff] [blame] | 177 | { |
Guenter Roeck | 719af4f1 | 2021-12-22 16:22:00 -0800 | [diff] [blame] | 178 | struct lm83_data *data = dev_get_drvdata(dev); |
| 179 | unsigned int regval; |
Frans Meulenbroeks | b3789a0 | 2012-01-10 23:01:40 +0100 | [diff] [blame] | 180 | int err; |
| 181 | |
Guenter Roeck | c291f61 | 2021-12-22 20:24:02 -0800 | [diff] [blame] | 182 | switch (attr) { |
| 183 | case hwmon_temp_input: |
| 184 | err = regmap_read(data->regmap, LM83_REG_TEMP[channel], ®val); |
| 185 | if (err < 0) |
| 186 | return err; |
| 187 | *val = (s8)regval * 1000; |
| 188 | break; |
| 189 | case hwmon_temp_max: |
| 190 | err = regmap_read(data->regmap, LM83_REG_MAX[channel], ®val); |
| 191 | if (err < 0) |
| 192 | return err; |
| 193 | *val = (s8)regval * 1000; |
| 194 | break; |
| 195 | case hwmon_temp_crit: |
| 196 | err = regmap_read(data->regmap, LM83_REG_R_TCRIT, ®val); |
| 197 | if (err < 0) |
| 198 | return err; |
| 199 | *val = (s8)regval * 1000; |
| 200 | break; |
| 201 | case hwmon_temp_max_alarm: |
| 202 | err = regmap_read(data->regmap, LM83_ALARM_REG[channel], ®val); |
| 203 | if (err < 0) |
| 204 | return err; |
| 205 | *val = !!(regval & LM83_MAX_ALARM_BIT[channel]); |
| 206 | break; |
| 207 | case hwmon_temp_crit_alarm: |
| 208 | err = regmap_read(data->regmap, LM83_ALARM_REG[channel], ®val); |
| 209 | if (err < 0) |
| 210 | return err; |
| 211 | *val = !!(regval & LM83_CRIT_ALARM_BIT[channel]); |
| 212 | break; |
| 213 | case hwmon_temp_fault: |
| 214 | err = regmap_read(data->regmap, LM83_ALARM_REG[channel], ®val); |
| 215 | if (err < 0) |
| 216 | return err; |
| 217 | *val = !!(regval & LM83_FAULT_BIT[channel]); |
| 218 | break; |
| 219 | default: |
| 220 | return -EOPNOTSUPP; |
| 221 | } |
| 222 | return 0; |
| 223 | } |
| 224 | |
| 225 | static int lm83_temp_write(struct device *dev, u32 attr, int channel, long val) |
| 226 | { |
| 227 | struct lm83_data *data = dev_get_drvdata(dev); |
| 228 | unsigned int regval; |
| 229 | int err; |
Jean Delvare | 1a86c05 | 2005-06-05 21:16:39 +0200 | [diff] [blame] | 230 | |
Guenter Roeck | 362c566 | 2021-12-22 19:03:04 -0800 | [diff] [blame] | 231 | regval = DIV_ROUND_CLOSEST(clamp_val(val, -128000, 127000), 1000); |
Guenter Roeck | c291f61 | 2021-12-22 20:24:02 -0800 | [diff] [blame] | 232 | |
| 233 | switch (attr) { |
| 234 | case hwmon_temp_max: |
| 235 | err = regmap_write(data->regmap, LM83_REG_MAX[channel], regval); |
| 236 | if (err < 0) |
| 237 | return err; |
| 238 | break; |
| 239 | case hwmon_temp_crit: |
| 240 | err = regmap_write(data->regmap, LM83_REG_R_TCRIT, regval); |
| 241 | if (err < 0) |
| 242 | return err; |
| 243 | break; |
| 244 | default: |
| 245 | return -EOPNOTSUPP; |
| 246 | } |
| 247 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | |
Guenter Roeck | c291f61 | 2021-12-22 20:24:02 -0800 | [diff] [blame] | 250 | static int lm83_chip_read(struct device *dev, u32 attr, int channel, long *val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | { |
Guenter Roeck | 719af4f1 | 2021-12-22 16:22:00 -0800 | [diff] [blame] | 252 | struct lm83_data *data = dev_get_drvdata(dev); |
Guenter Roeck | c291f61 | 2021-12-22 20:24:02 -0800 | [diff] [blame] | 253 | unsigned int regval; |
Guenter Roeck | 719af4f1 | 2021-12-22 16:22:00 -0800 | [diff] [blame] | 254 | int err; |
| 255 | |
Guenter Roeck | c291f61 | 2021-12-22 20:24:02 -0800 | [diff] [blame] | 256 | switch (attr) { |
| 257 | case hwmon_chip_alarms: |
| 258 | err = regmap_read(data->regmap, LM83_REG_R_STATUS1, ®val); |
| 259 | if (err < 0) |
| 260 | return err; |
| 261 | *val = regval; |
| 262 | err = regmap_read(data->regmap, LM83_REG_R_STATUS2, ®val); |
| 263 | if (err < 0) |
| 264 | return err; |
| 265 | *val |= regval << 8; |
| 266 | return 0; |
| 267 | default: |
| 268 | return -EOPNOTSUPP; |
Guenter Roeck | 719af4f1 | 2021-12-22 16:22:00 -0800 | [diff] [blame] | 269 | } |
Guenter Roeck | c291f61 | 2021-12-22 20:24:02 -0800 | [diff] [blame] | 270 | |
| 271 | return 0; |
Jean Delvare | 2d45771 | 2006-09-24 20:52:15 +0200 | [diff] [blame] | 272 | } |
| 273 | |
Guenter Roeck | c291f61 | 2021-12-22 20:24:02 -0800 | [diff] [blame] | 274 | static int lm83_read(struct device *dev, enum hwmon_sensor_types type, |
| 275 | u32 attr, int channel, long *val) |
| 276 | { |
| 277 | switch (type) { |
| 278 | case hwmon_chip: |
| 279 | return lm83_chip_read(dev, attr, channel, val); |
| 280 | case hwmon_temp: |
| 281 | return lm83_temp_read(dev, attr, channel, val); |
| 282 | default: |
| 283 | return -EOPNOTSUPP; |
| 284 | } |
| 285 | } |
Jean Delvare | 2d45771 | 2006-09-24 20:52:15 +0200 | [diff] [blame] | 286 | |
Guenter Roeck | c291f61 | 2021-12-22 20:24:02 -0800 | [diff] [blame] | 287 | static int lm83_write(struct device *dev, enum hwmon_sensor_types type, |
| 288 | u32 attr, int channel, long val) |
| 289 | { |
| 290 | switch (type) { |
| 291 | case hwmon_temp: |
| 292 | return lm83_temp_write(dev, attr, channel, val); |
| 293 | default: |
| 294 | return -EOPNOTSUPP; |
| 295 | } |
| 296 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | |
Guenter Roeck | c291f61 | 2021-12-22 20:24:02 -0800 | [diff] [blame] | 298 | static umode_t lm83_is_visible(const void *_data, enum hwmon_sensor_types type, |
| 299 | u32 attr, int channel) |
| 300 | { |
| 301 | const struct lm83_data *data = _data; |
Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 302 | |
Guenter Roeck | c291f61 | 2021-12-22 20:24:02 -0800 | [diff] [blame] | 303 | /* |
| 304 | * LM82 only supports a single external channel, modeled as channel 2. |
| 305 | */ |
| 306 | if (data->type == lm82 && (channel == 1 || channel == 3)) |
| 307 | return 0; |
| 308 | |
| 309 | switch (type) { |
| 310 | case hwmon_chip: |
| 311 | if (attr == hwmon_chip_alarms) |
| 312 | return 0444; |
| 313 | break; |
| 314 | case hwmon_temp: |
| 315 | switch (attr) { |
| 316 | case hwmon_temp_input: |
| 317 | case hwmon_temp_max_alarm: |
| 318 | case hwmon_temp_crit_alarm: |
| 319 | return 0444; |
| 320 | case hwmon_temp_fault: |
| 321 | if (channel) |
| 322 | return 0444; |
| 323 | break; |
| 324 | case hwmon_temp_max: |
| 325 | return 0644; |
| 326 | case hwmon_temp_crit: |
| 327 | if (channel == 2) |
| 328 | return 0644; |
| 329 | return 0444; |
| 330 | default: |
| 331 | break; |
| 332 | } |
| 333 | break; |
| 334 | default: |
| 335 | break; |
| 336 | } |
| 337 | return 0; |
| 338 | } |
| 339 | |
Krzysztof Kozlowski | 3c37024 | 2023-04-06 22:30:22 +0200 | [diff] [blame] | 340 | static const struct hwmon_channel_info * const lm83_info[] = { |
Guenter Roeck | c291f61 | 2021-12-22 20:24:02 -0800 | [diff] [blame] | 341 | HWMON_CHANNEL_INFO(chip, HWMON_C_ALARMS), |
| 342 | HWMON_CHANNEL_INFO(temp, |
| 343 | HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_CRIT | |
| 344 | HWMON_T_MAX_ALARM | HWMON_T_CRIT_ALARM, |
| 345 | HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_CRIT | |
| 346 | HWMON_T_MAX_ALARM | HWMON_T_CRIT_ALARM | HWMON_T_FAULT, |
| 347 | HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_CRIT | |
| 348 | HWMON_T_MAX_ALARM | HWMON_T_CRIT_ALARM | HWMON_T_FAULT, |
| 349 | HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_CRIT | |
| 350 | HWMON_T_MAX_ALARM | HWMON_T_CRIT_ALARM | HWMON_T_FAULT |
| 351 | ), |
Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 352 | NULL |
| 353 | }; |
| 354 | |
Guenter Roeck | c291f61 | 2021-12-22 20:24:02 -0800 | [diff] [blame] | 355 | static const struct hwmon_ops lm83_hwmon_ops = { |
| 356 | .is_visible = lm83_is_visible, |
| 357 | .read = lm83_read, |
| 358 | .write = lm83_write, |
Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 359 | }; |
| 360 | |
Guenter Roeck | c291f61 | 2021-12-22 20:24:02 -0800 | [diff] [blame] | 361 | static const struct hwmon_chip_info lm83_chip_info = { |
| 362 | .ops = &lm83_hwmon_ops, |
| 363 | .info = lm83_info, |
Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 364 | }; |
| 365 | |
Jean Delvare | b6aacdc | 2008-07-16 19:30:14 +0200 | [diff] [blame] | 366 | /* Return 0 if detection is successful, -ENODEV otherwise */ |
Guenter Roeck | 81de0ee | 2021-12-22 16:23:31 -0800 | [diff] [blame] | 367 | static int lm83_detect(struct i2c_client *client, |
Jean Delvare | b6aacdc | 2008-07-16 19:30:14 +0200 | [diff] [blame] | 368 | struct i2c_board_info *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | { |
Guenter Roeck | 81de0ee | 2021-12-22 16:23:31 -0800 | [diff] [blame] | 370 | struct i2c_adapter *adapter = client->adapter; |
Jean Delvare | b57dc39 | 2009-12-09 20:35:52 +0100 | [diff] [blame] | 371 | const char *name; |
| 372 | u8 man_id, chip_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | |
| 374 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) |
Jean Delvare | b6aacdc | 2008-07-16 19:30:14 +0200 | [diff] [blame] | 375 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | |
Jean Delvare | b57dc39 | 2009-12-09 20:35:52 +0100 | [diff] [blame] | 377 | /* Detection */ |
Guenter Roeck | 81de0ee | 2021-12-22 16:23:31 -0800 | [diff] [blame] | 378 | if ((i2c_smbus_read_byte_data(client, LM83_REG_R_STATUS1) & 0xA8) || |
| 379 | (i2c_smbus_read_byte_data(client, LM83_REG_R_STATUS2) & 0x48) || |
| 380 | (i2c_smbus_read_byte_data(client, LM83_REG_R_CONFIG) & 0x41)) { |
Jean Delvare | b57dc39 | 2009-12-09 20:35:52 +0100 | [diff] [blame] | 381 | dev_dbg(&adapter->dev, "LM83 detection failed at 0x%02x\n", |
Guenter Roeck | 81de0ee | 2021-12-22 16:23:31 -0800 | [diff] [blame] | 382 | client->addr); |
Jean Delvare | b57dc39 | 2009-12-09 20:35:52 +0100 | [diff] [blame] | 383 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | } |
| 385 | |
Jean Delvare | b57dc39 | 2009-12-09 20:35:52 +0100 | [diff] [blame] | 386 | /* Identification */ |
Guenter Roeck | 81de0ee | 2021-12-22 16:23:31 -0800 | [diff] [blame] | 387 | man_id = i2c_smbus_read_byte_data(client, LM83_REG_R_MAN_ID); |
Jean Delvare | b57dc39 | 2009-12-09 20:35:52 +0100 | [diff] [blame] | 388 | if (man_id != 0x01) /* National Semiconductor */ |
| 389 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | |
Guenter Roeck | 81de0ee | 2021-12-22 16:23:31 -0800 | [diff] [blame] | 391 | chip_id = i2c_smbus_read_byte_data(client, LM83_REG_R_CHIP_ID); |
Jean Delvare | b57dc39 | 2009-12-09 20:35:52 +0100 | [diff] [blame] | 392 | switch (chip_id) { |
| 393 | case 0x03: |
Guenter Roeck | 913ac02 | 2021-12-24 11:31:32 -0800 | [diff] [blame] | 394 | /* |
| 395 | * According to the LM82 datasheet dated March 2013, recent |
| 396 | * revisions of LM82 have a die revision of 0x03. This was |
| 397 | * confirmed with a real chip. Further details in this revision |
| 398 | * of the LM82 datasheet strongly suggest that LM82 is just a |
| 399 | * repackaged LM83. It is therefore impossible to distinguish |
| 400 | * those chips from LM83, and they will be misdetected as LM83. |
| 401 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | name = "lm83"; |
Jean Delvare | b57dc39 | 2009-12-09 20:35:52 +0100 | [diff] [blame] | 403 | break; |
| 404 | case 0x01: |
Jordan Crouse | 43cb7eb | 2006-03-23 16:19:49 +0100 | [diff] [blame] | 405 | name = "lm82"; |
Jean Delvare | b57dc39 | 2009-12-09 20:35:52 +0100 | [diff] [blame] | 406 | break; |
| 407 | default: |
| 408 | /* identification failed */ |
Guenter Roeck | 4d63c2d | 2021-12-22 23:49:59 -0800 | [diff] [blame] | 409 | dev_dbg(&adapter->dev, |
| 410 | "Unsupported chip (man_id=0x%02X, chip_id=0x%02X)\n", |
| 411 | man_id, chip_id); |
Jean Delvare | b57dc39 | 2009-12-09 20:35:52 +0100 | [diff] [blame] | 412 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | } |
| 414 | |
Wolfram Sang | f2f394d | 2022-08-18 23:00:11 +0200 | [diff] [blame] | 415 | strscpy(info->type, name, I2C_NAME_SIZE); |
Jean Delvare | b6aacdc | 2008-07-16 19:30:14 +0200 | [diff] [blame] | 416 | |
| 417 | return 0; |
| 418 | } |
| 419 | |
Guenter Roeck | 11e3377 | 2021-12-22 11:49:04 -0800 | [diff] [blame] | 420 | static const struct i2c_device_id lm83_id[] = { |
| 421 | { "lm83", lm83 }, |
| 422 | { "lm82", lm82 }, |
| 423 | { } |
| 424 | }; |
| 425 | MODULE_DEVICE_TABLE(i2c, lm83_id); |
Stephen Kitt | 6748703 | 2020-08-13 18:02:22 +0200 | [diff] [blame] | 426 | |
Guenter Roeck | 81de0ee | 2021-12-22 16:23:31 -0800 | [diff] [blame] | 427 | static int lm83_probe(struct i2c_client *client) |
Jean Delvare | b6aacdc | 2008-07-16 19:30:14 +0200 | [diff] [blame] | 428 | { |
Guenter Roeck | 719af4f1 | 2021-12-22 16:22:00 -0800 | [diff] [blame] | 429 | struct device *dev = &client->dev; |
Guenter Roeck | a0ac840 | 2014-04-12 12:46:34 -0700 | [diff] [blame] | 430 | struct device *hwmon_dev; |
Jean Delvare | b6aacdc | 2008-07-16 19:30:14 +0200 | [diff] [blame] | 431 | struct lm83_data *data; |
Jean Delvare | b6aacdc | 2008-07-16 19:30:14 +0200 | [diff] [blame] | 432 | |
Guenter Roeck | 719af4f1 | 2021-12-22 16:22:00 -0800 | [diff] [blame] | 433 | data = devm_kzalloc(dev, sizeof(struct lm83_data), GFP_KERNEL); |
Guenter Roeck | c087f73 | 2012-06-02 09:58:10 -0700 | [diff] [blame] | 434 | if (!data) |
| 435 | return -ENOMEM; |
Jean Delvare | b6aacdc | 2008-07-16 19:30:14 +0200 | [diff] [blame] | 436 | |
Guenter Roeck | 719af4f1 | 2021-12-22 16:22:00 -0800 | [diff] [blame] | 437 | data->regmap = devm_regmap_init(dev, NULL, client, &lm83_regmap_config); |
| 438 | if (IS_ERR(data->regmap)) |
| 439 | return PTR_ERR(data->regmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | |
Guenter Roeck | c291f61 | 2021-12-22 20:24:02 -0800 | [diff] [blame] | 441 | data->type = i2c_match_id(lm83_id, client)->driver_data; |
Jordan Crouse | 43cb7eb | 2006-03-23 16:19:49 +0100 | [diff] [blame] | 442 | |
Guenter Roeck | c291f61 | 2021-12-22 20:24:02 -0800 | [diff] [blame] | 443 | hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name, |
| 444 | data, &lm83_chip_info, NULL); |
Guenter Roeck | a0ac840 | 2014-04-12 12:46:34 -0700 | [diff] [blame] | 445 | return PTR_ERR_OR_ZERO(hwmon_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | } |
| 447 | |
Guenter Roeck | 4193637 | 2014-04-12 12:39:41 -0700 | [diff] [blame] | 448 | /* |
| 449 | * Driver data (common to all clients) |
| 450 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | |
Guenter Roeck | 4193637 | 2014-04-12 12:39:41 -0700 | [diff] [blame] | 452 | static struct i2c_driver lm83_driver = { |
| 453 | .class = I2C_CLASS_HWMON, |
| 454 | .driver = { |
| 455 | .name = "lm83", |
| 456 | }, |
Stephen Kitt | 6748703 | 2020-08-13 18:02:22 +0200 | [diff] [blame] | 457 | .probe_new = lm83_probe, |
Guenter Roeck | 4193637 | 2014-04-12 12:39:41 -0700 | [diff] [blame] | 458 | .id_table = lm83_id, |
| 459 | .detect = lm83_detect, |
| 460 | .address_list = normal_i2c, |
| 461 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | |
Axel Lin | f0967ee | 2012-01-20 15:38:18 +0800 | [diff] [blame] | 463 | module_i2c_driver(lm83_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | |
Jean Delvare | 7c81c60f | 2014-01-29 20:40:08 +0100 | [diff] [blame] | 465 | MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | MODULE_DESCRIPTION("LM83 driver"); |
| 467 | MODULE_LICENSE("GPL"); |