Thomas Gleixner | c942fdd | 2019-05-27 08:55:06 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Driver for Texas Instruments / National Semiconductor LM95234 |
| 4 | * |
Guenter Roeck | dfcd4c5 | 2014-04-22 09:34:14 -0700 | [diff] [blame] | 5 | * Copyright (c) 2013, 2014 Guenter Roeck <linux@roeck-us.net> |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 6 | * |
| 7 | * Derived from lm95241.c |
| 8 | * Copyright (C) 2008, 2010 Davide Rizzo <elpa.rizzo@gmail.com> |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/init.h> |
| 13 | #include <linux/slab.h> |
| 14 | #include <linux/jiffies.h> |
| 15 | #include <linux/i2c.h> |
| 16 | #include <linux/hwmon.h> |
| 17 | #include <linux/hwmon-sysfs.h> |
| 18 | #include <linux/err.h> |
| 19 | #include <linux/mutex.h> |
| 20 | #include <linux/sysfs.h> |
| 21 | |
| 22 | #define DRVNAME "lm95234" |
| 23 | |
Guenter Roeck | dfcd4c5 | 2014-04-22 09:34:14 -0700 | [diff] [blame] | 24 | enum chips { lm95233, lm95234 }; |
| 25 | |
| 26 | static const unsigned short normal_i2c[] = { |
| 27 | 0x18, 0x2a, 0x2b, 0x4d, 0x4e, I2C_CLIENT_END }; |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 28 | |
| 29 | /* LM95234 registers */ |
| 30 | #define LM95234_REG_MAN_ID 0xFE |
| 31 | #define LM95234_REG_CHIP_ID 0xFF |
| 32 | #define LM95234_REG_STATUS 0x02 |
| 33 | #define LM95234_REG_CONFIG 0x03 |
| 34 | #define LM95234_REG_CONVRATE 0x04 |
| 35 | #define LM95234_REG_STS_FAULT 0x07 |
| 36 | #define LM95234_REG_STS_TCRIT1 0x08 |
| 37 | #define LM95234_REG_STS_TCRIT2 0x09 |
| 38 | #define LM95234_REG_TEMPH(x) ((x) + 0x10) |
| 39 | #define LM95234_REG_TEMPL(x) ((x) + 0x20) |
| 40 | #define LM95234_REG_UTEMPH(x) ((x) + 0x19) /* Remote only */ |
| 41 | #define LM95234_REG_UTEMPL(x) ((x) + 0x29) |
| 42 | #define LM95234_REG_REM_MODEL 0x30 |
| 43 | #define LM95234_REG_REM_MODEL_STS 0x38 |
| 44 | #define LM95234_REG_OFFSET(x) ((x) + 0x31) /* Remote only */ |
| 45 | #define LM95234_REG_TCRIT1(x) ((x) + 0x40) |
| 46 | #define LM95234_REG_TCRIT2(x) ((x) + 0x49) /* Remote channel 1,2 */ |
| 47 | #define LM95234_REG_TCRIT_HYST 0x5a |
| 48 | |
| 49 | #define NATSEMI_MAN_ID 0x01 |
Guenter Roeck | dfcd4c5 | 2014-04-22 09:34:14 -0700 | [diff] [blame] | 50 | #define LM95233_CHIP_ID 0x89 |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 51 | #define LM95234_CHIP_ID 0x79 |
| 52 | |
| 53 | /* Client data (each client gets its own) */ |
| 54 | struct lm95234_data { |
Guenter Roeck | 9d86bd6 | 2013-09-04 16:39:12 -0700 | [diff] [blame] | 55 | struct i2c_client *client; |
Guenter Roeck | dfcd4c5 | 2014-04-22 09:34:14 -0700 | [diff] [blame] | 56 | const struct attribute_group *groups[3]; |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 57 | struct mutex update_lock; |
| 58 | unsigned long last_updated, interval; /* in jiffies */ |
| 59 | bool valid; /* false until following fields are valid */ |
| 60 | /* registers values */ |
| 61 | int temp[5]; /* temperature (signed) */ |
| 62 | u32 status; /* fault/alarm status */ |
| 63 | u8 tcrit1[5]; /* critical temperature limit */ |
| 64 | u8 tcrit2[2]; /* high temperature limit */ |
| 65 | s8 toffset[4]; /* remote temperature offset */ |
| 66 | u8 thyst; /* common hysteresis */ |
| 67 | |
| 68 | u8 sensor_type; /* temperature sensor type */ |
| 69 | }; |
| 70 | |
| 71 | static int lm95234_read_temp(struct i2c_client *client, int index, int *t) |
| 72 | { |
| 73 | int val; |
| 74 | u16 temp = 0; |
| 75 | |
| 76 | if (index) { |
| 77 | val = i2c_smbus_read_byte_data(client, |
| 78 | LM95234_REG_UTEMPH(index - 1)); |
| 79 | if (val < 0) |
| 80 | return val; |
| 81 | temp = val << 8; |
| 82 | val = i2c_smbus_read_byte_data(client, |
| 83 | LM95234_REG_UTEMPL(index - 1)); |
| 84 | if (val < 0) |
| 85 | return val; |
| 86 | temp |= val; |
| 87 | *t = temp; |
| 88 | } |
| 89 | /* |
| 90 | * Read signed temperature if unsigned temperature is 0, |
| 91 | * or if this is the local sensor. |
| 92 | */ |
| 93 | if (!temp) { |
| 94 | val = i2c_smbus_read_byte_data(client, |
| 95 | LM95234_REG_TEMPH(index)); |
| 96 | if (val < 0) |
| 97 | return val; |
| 98 | temp = val << 8; |
| 99 | val = i2c_smbus_read_byte_data(client, |
| 100 | LM95234_REG_TEMPL(index)); |
| 101 | if (val < 0) |
| 102 | return val; |
| 103 | temp |= val; |
| 104 | *t = (s16)temp; |
| 105 | } |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | static u16 update_intervals[] = { 143, 364, 1000, 2500 }; |
| 110 | |
| 111 | /* Fill value cache. Must be called with update lock held. */ |
| 112 | |
Guenter Roeck | 9d86bd6 | 2013-09-04 16:39:12 -0700 | [diff] [blame] | 113 | static int lm95234_fill_cache(struct lm95234_data *data, |
| 114 | struct i2c_client *client) |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 115 | { |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 116 | int i, ret; |
| 117 | |
| 118 | ret = i2c_smbus_read_byte_data(client, LM95234_REG_CONVRATE); |
| 119 | if (ret < 0) |
| 120 | return ret; |
| 121 | |
| 122 | data->interval = msecs_to_jiffies(update_intervals[ret & 0x03]); |
| 123 | |
| 124 | for (i = 0; i < ARRAY_SIZE(data->tcrit1); i++) { |
| 125 | ret = i2c_smbus_read_byte_data(client, LM95234_REG_TCRIT1(i)); |
| 126 | if (ret < 0) |
| 127 | return ret; |
| 128 | data->tcrit1[i] = ret; |
| 129 | } |
| 130 | for (i = 0; i < ARRAY_SIZE(data->tcrit2); i++) { |
| 131 | ret = i2c_smbus_read_byte_data(client, LM95234_REG_TCRIT2(i)); |
| 132 | if (ret < 0) |
| 133 | return ret; |
| 134 | data->tcrit2[i] = ret; |
| 135 | } |
| 136 | for (i = 0; i < ARRAY_SIZE(data->toffset); i++) { |
| 137 | ret = i2c_smbus_read_byte_data(client, LM95234_REG_OFFSET(i)); |
| 138 | if (ret < 0) |
| 139 | return ret; |
| 140 | data->toffset[i] = ret; |
| 141 | } |
| 142 | |
| 143 | ret = i2c_smbus_read_byte_data(client, LM95234_REG_TCRIT_HYST); |
| 144 | if (ret < 0) |
| 145 | return ret; |
| 146 | data->thyst = ret; |
| 147 | |
| 148 | ret = i2c_smbus_read_byte_data(client, LM95234_REG_REM_MODEL); |
| 149 | if (ret < 0) |
| 150 | return ret; |
| 151 | data->sensor_type = ret; |
| 152 | |
| 153 | return 0; |
| 154 | } |
| 155 | |
Guenter Roeck | 9d86bd6 | 2013-09-04 16:39:12 -0700 | [diff] [blame] | 156 | static int lm95234_update_device(struct lm95234_data *data) |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 157 | { |
Guenter Roeck | 9d86bd6 | 2013-09-04 16:39:12 -0700 | [diff] [blame] | 158 | struct i2c_client *client = data->client; |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 159 | int ret; |
| 160 | |
| 161 | mutex_lock(&data->update_lock); |
| 162 | |
| 163 | if (time_after(jiffies, data->last_updated + data->interval) || |
| 164 | !data->valid) { |
| 165 | int i; |
| 166 | |
| 167 | if (!data->valid) { |
Guenter Roeck | 9d86bd6 | 2013-09-04 16:39:12 -0700 | [diff] [blame] | 168 | ret = lm95234_fill_cache(data, client); |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 169 | if (ret < 0) |
| 170 | goto abort; |
| 171 | } |
| 172 | |
| 173 | data->valid = false; |
| 174 | for (i = 0; i < ARRAY_SIZE(data->temp); i++) { |
| 175 | ret = lm95234_read_temp(client, i, &data->temp[i]); |
| 176 | if (ret < 0) |
| 177 | goto abort; |
| 178 | } |
| 179 | |
| 180 | ret = i2c_smbus_read_byte_data(client, LM95234_REG_STS_FAULT); |
| 181 | if (ret < 0) |
| 182 | goto abort; |
| 183 | data->status = ret; |
| 184 | |
| 185 | ret = i2c_smbus_read_byte_data(client, LM95234_REG_STS_TCRIT1); |
| 186 | if (ret < 0) |
| 187 | goto abort; |
| 188 | data->status |= ret << 8; |
| 189 | |
| 190 | ret = i2c_smbus_read_byte_data(client, LM95234_REG_STS_TCRIT2); |
| 191 | if (ret < 0) |
| 192 | goto abort; |
| 193 | data->status |= ret << 16; |
| 194 | |
| 195 | data->last_updated = jiffies; |
| 196 | data->valid = true; |
| 197 | } |
| 198 | ret = 0; |
| 199 | abort: |
| 200 | mutex_unlock(&data->update_lock); |
| 201 | |
| 202 | return ret; |
| 203 | } |
| 204 | |
Guenter Roeck | 33721d9 | 2018-12-06 10:38:29 -0800 | [diff] [blame] | 205 | static ssize_t temp_show(struct device *dev, struct device_attribute *attr, |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 206 | char *buf) |
| 207 | { |
Guenter Roeck | 9d86bd6 | 2013-09-04 16:39:12 -0700 | [diff] [blame] | 208 | struct lm95234_data *data = dev_get_drvdata(dev); |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 209 | int index = to_sensor_dev_attr(attr)->index; |
Guenter Roeck | 9d86bd6 | 2013-09-04 16:39:12 -0700 | [diff] [blame] | 210 | int ret = lm95234_update_device(data); |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 211 | |
| 212 | if (ret) |
| 213 | return ret; |
| 214 | |
| 215 | return sprintf(buf, "%d\n", |
| 216 | DIV_ROUND_CLOSEST(data->temp[index] * 125, 32)); |
| 217 | } |
| 218 | |
Guenter Roeck | 33721d9 | 2018-12-06 10:38:29 -0800 | [diff] [blame] | 219 | static ssize_t alarm_show(struct device *dev, struct device_attribute *attr, |
| 220 | char *buf) |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 221 | { |
Guenter Roeck | 9d86bd6 | 2013-09-04 16:39:12 -0700 | [diff] [blame] | 222 | struct lm95234_data *data = dev_get_drvdata(dev); |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 223 | u32 mask = to_sensor_dev_attr(attr)->index; |
Guenter Roeck | 9d86bd6 | 2013-09-04 16:39:12 -0700 | [diff] [blame] | 224 | int ret = lm95234_update_device(data); |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 225 | |
| 226 | if (ret) |
| 227 | return ret; |
| 228 | |
| 229 | return sprintf(buf, "%u", !!(data->status & mask)); |
| 230 | } |
| 231 | |
Guenter Roeck | 33721d9 | 2018-12-06 10:38:29 -0800 | [diff] [blame] | 232 | static ssize_t type_show(struct device *dev, struct device_attribute *attr, |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 233 | char *buf) |
| 234 | { |
Guenter Roeck | 9d86bd6 | 2013-09-04 16:39:12 -0700 | [diff] [blame] | 235 | struct lm95234_data *data = dev_get_drvdata(dev); |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 236 | u8 mask = to_sensor_dev_attr(attr)->index; |
Guenter Roeck | 9d86bd6 | 2013-09-04 16:39:12 -0700 | [diff] [blame] | 237 | int ret = lm95234_update_device(data); |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 238 | |
| 239 | if (ret) |
| 240 | return ret; |
| 241 | |
| 242 | return sprintf(buf, data->sensor_type & mask ? "1\n" : "2\n"); |
| 243 | } |
| 244 | |
Guenter Roeck | 33721d9 | 2018-12-06 10:38:29 -0800 | [diff] [blame] | 245 | static ssize_t type_store(struct device *dev, struct device_attribute *attr, |
| 246 | const char *buf, size_t count) |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 247 | { |
Guenter Roeck | 9d86bd6 | 2013-09-04 16:39:12 -0700 | [diff] [blame] | 248 | struct lm95234_data *data = dev_get_drvdata(dev); |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 249 | unsigned long val; |
| 250 | u8 mask = to_sensor_dev_attr(attr)->index; |
Guenter Roeck | 9d86bd6 | 2013-09-04 16:39:12 -0700 | [diff] [blame] | 251 | int ret = lm95234_update_device(data); |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 252 | |
| 253 | if (ret) |
| 254 | return ret; |
| 255 | |
| 256 | ret = kstrtoul(buf, 10, &val); |
| 257 | if (ret < 0) |
| 258 | return ret; |
| 259 | |
| 260 | if (val != 1 && val != 2) |
| 261 | return -EINVAL; |
| 262 | |
| 263 | mutex_lock(&data->update_lock); |
| 264 | if (val == 1) |
| 265 | data->sensor_type |= mask; |
| 266 | else |
| 267 | data->sensor_type &= ~mask; |
| 268 | data->valid = false; |
Guenter Roeck | 9d86bd6 | 2013-09-04 16:39:12 -0700 | [diff] [blame] | 269 | i2c_smbus_write_byte_data(data->client, LM95234_REG_REM_MODEL, |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 270 | data->sensor_type); |
| 271 | mutex_unlock(&data->update_lock); |
| 272 | |
| 273 | return count; |
| 274 | } |
| 275 | |
Guenter Roeck | 33721d9 | 2018-12-06 10:38:29 -0800 | [diff] [blame] | 276 | static ssize_t tcrit2_show(struct device *dev, struct device_attribute *attr, |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 277 | char *buf) |
| 278 | { |
Guenter Roeck | 9d86bd6 | 2013-09-04 16:39:12 -0700 | [diff] [blame] | 279 | struct lm95234_data *data = dev_get_drvdata(dev); |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 280 | int index = to_sensor_dev_attr(attr)->index; |
Guenter Roeck | 9d86bd6 | 2013-09-04 16:39:12 -0700 | [diff] [blame] | 281 | int ret = lm95234_update_device(data); |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 282 | |
| 283 | if (ret) |
| 284 | return ret; |
| 285 | |
| 286 | return sprintf(buf, "%u", data->tcrit2[index] * 1000); |
| 287 | } |
| 288 | |
Guenter Roeck | 33721d9 | 2018-12-06 10:38:29 -0800 | [diff] [blame] | 289 | static ssize_t tcrit2_store(struct device *dev, struct device_attribute *attr, |
| 290 | const char *buf, size_t count) |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 291 | { |
Guenter Roeck | 9d86bd6 | 2013-09-04 16:39:12 -0700 | [diff] [blame] | 292 | struct lm95234_data *data = dev_get_drvdata(dev); |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 293 | int index = to_sensor_dev_attr(attr)->index; |
| 294 | long val; |
Guenter Roeck | 9d86bd6 | 2013-09-04 16:39:12 -0700 | [diff] [blame] | 295 | int ret = lm95234_update_device(data); |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 296 | |
| 297 | if (ret) |
| 298 | return ret; |
| 299 | |
| 300 | ret = kstrtol(buf, 10, &val); |
| 301 | if (ret < 0) |
| 302 | return ret; |
| 303 | |
| 304 | val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), 0, index ? 255 : 127); |
| 305 | |
| 306 | mutex_lock(&data->update_lock); |
| 307 | data->tcrit2[index] = val; |
Guenter Roeck | 9d86bd6 | 2013-09-04 16:39:12 -0700 | [diff] [blame] | 308 | i2c_smbus_write_byte_data(data->client, LM95234_REG_TCRIT2(index), val); |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 309 | mutex_unlock(&data->update_lock); |
| 310 | |
| 311 | return count; |
| 312 | } |
| 313 | |
Guenter Roeck | 33721d9 | 2018-12-06 10:38:29 -0800 | [diff] [blame] | 314 | static ssize_t tcrit2_hyst_show(struct device *dev, |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 315 | struct device_attribute *attr, char *buf) |
| 316 | { |
Guenter Roeck | 9d86bd6 | 2013-09-04 16:39:12 -0700 | [diff] [blame] | 317 | struct lm95234_data *data = dev_get_drvdata(dev); |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 318 | int index = to_sensor_dev_attr(attr)->index; |
Guenter Roeck | 9d86bd6 | 2013-09-04 16:39:12 -0700 | [diff] [blame] | 319 | int ret = lm95234_update_device(data); |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 320 | |
| 321 | if (ret) |
| 322 | return ret; |
| 323 | |
| 324 | /* Result can be negative, so be careful with unsigned operands */ |
| 325 | return sprintf(buf, "%d", |
| 326 | ((int)data->tcrit2[index] - (int)data->thyst) * 1000); |
| 327 | } |
| 328 | |
Guenter Roeck | 33721d9 | 2018-12-06 10:38:29 -0800 | [diff] [blame] | 329 | static ssize_t tcrit1_show(struct device *dev, struct device_attribute *attr, |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 330 | char *buf) |
| 331 | { |
Guenter Roeck | 9d86bd6 | 2013-09-04 16:39:12 -0700 | [diff] [blame] | 332 | struct lm95234_data *data = dev_get_drvdata(dev); |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 333 | int index = to_sensor_dev_attr(attr)->index; |
| 334 | |
| 335 | return sprintf(buf, "%u", data->tcrit1[index] * 1000); |
| 336 | } |
| 337 | |
Guenter Roeck | 33721d9 | 2018-12-06 10:38:29 -0800 | [diff] [blame] | 338 | static ssize_t tcrit1_store(struct device *dev, struct device_attribute *attr, |
| 339 | const char *buf, size_t count) |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 340 | { |
Guenter Roeck | 9d86bd6 | 2013-09-04 16:39:12 -0700 | [diff] [blame] | 341 | struct lm95234_data *data = dev_get_drvdata(dev); |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 342 | int index = to_sensor_dev_attr(attr)->index; |
Guenter Roeck | 9d86bd6 | 2013-09-04 16:39:12 -0700 | [diff] [blame] | 343 | int ret = lm95234_update_device(data); |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 344 | long val; |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 345 | |
| 346 | if (ret) |
| 347 | return ret; |
| 348 | |
| 349 | ret = kstrtol(buf, 10, &val); |
| 350 | if (ret < 0) |
| 351 | return ret; |
| 352 | |
| 353 | val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), 0, 255); |
| 354 | |
| 355 | mutex_lock(&data->update_lock); |
| 356 | data->tcrit1[index] = val; |
Guenter Roeck | 9d86bd6 | 2013-09-04 16:39:12 -0700 | [diff] [blame] | 357 | i2c_smbus_write_byte_data(data->client, LM95234_REG_TCRIT1(index), val); |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 358 | mutex_unlock(&data->update_lock); |
| 359 | |
| 360 | return count; |
| 361 | } |
| 362 | |
Guenter Roeck | 33721d9 | 2018-12-06 10:38:29 -0800 | [diff] [blame] | 363 | static ssize_t tcrit1_hyst_show(struct device *dev, |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 364 | struct device_attribute *attr, char *buf) |
| 365 | { |
Guenter Roeck | 9d86bd6 | 2013-09-04 16:39:12 -0700 | [diff] [blame] | 366 | struct lm95234_data *data = dev_get_drvdata(dev); |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 367 | int index = to_sensor_dev_attr(attr)->index; |
Guenter Roeck | 9d86bd6 | 2013-09-04 16:39:12 -0700 | [diff] [blame] | 368 | int ret = lm95234_update_device(data); |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 369 | |
| 370 | if (ret) |
| 371 | return ret; |
| 372 | |
| 373 | /* Result can be negative, so be careful with unsigned operands */ |
| 374 | return sprintf(buf, "%d", |
| 375 | ((int)data->tcrit1[index] - (int)data->thyst) * 1000); |
| 376 | } |
| 377 | |
Guenter Roeck | 33721d9 | 2018-12-06 10:38:29 -0800 | [diff] [blame] | 378 | static ssize_t tcrit1_hyst_store(struct device *dev, |
| 379 | struct device_attribute *attr, |
| 380 | const char *buf, size_t count) |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 381 | { |
Guenter Roeck | 9d86bd6 | 2013-09-04 16:39:12 -0700 | [diff] [blame] | 382 | struct lm95234_data *data = dev_get_drvdata(dev); |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 383 | int index = to_sensor_dev_attr(attr)->index; |
Guenter Roeck | 9d86bd6 | 2013-09-04 16:39:12 -0700 | [diff] [blame] | 384 | int ret = lm95234_update_device(data); |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 385 | long val; |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 386 | |
| 387 | if (ret) |
| 388 | return ret; |
| 389 | |
| 390 | ret = kstrtol(buf, 10, &val); |
| 391 | if (ret < 0) |
| 392 | return ret; |
| 393 | |
| 394 | val = DIV_ROUND_CLOSEST(val, 1000); |
| 395 | val = clamp_val((int)data->tcrit1[index] - val, 0, 31); |
| 396 | |
| 397 | mutex_lock(&data->update_lock); |
| 398 | data->thyst = val; |
Guenter Roeck | 9d86bd6 | 2013-09-04 16:39:12 -0700 | [diff] [blame] | 399 | i2c_smbus_write_byte_data(data->client, LM95234_REG_TCRIT_HYST, val); |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 400 | mutex_unlock(&data->update_lock); |
| 401 | |
| 402 | return count; |
| 403 | } |
| 404 | |
Guenter Roeck | 33721d9 | 2018-12-06 10:38:29 -0800 | [diff] [blame] | 405 | static ssize_t offset_show(struct device *dev, struct device_attribute *attr, |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 406 | char *buf) |
| 407 | { |
Guenter Roeck | 9d86bd6 | 2013-09-04 16:39:12 -0700 | [diff] [blame] | 408 | struct lm95234_data *data = dev_get_drvdata(dev); |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 409 | int index = to_sensor_dev_attr(attr)->index; |
Guenter Roeck | 9d86bd6 | 2013-09-04 16:39:12 -0700 | [diff] [blame] | 410 | int ret = lm95234_update_device(data); |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 411 | |
| 412 | if (ret) |
| 413 | return ret; |
| 414 | |
| 415 | return sprintf(buf, "%d", data->toffset[index] * 500); |
| 416 | } |
| 417 | |
Guenter Roeck | 33721d9 | 2018-12-06 10:38:29 -0800 | [diff] [blame] | 418 | static ssize_t offset_store(struct device *dev, struct device_attribute *attr, |
| 419 | const char *buf, size_t count) |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 420 | { |
Guenter Roeck | 9d86bd6 | 2013-09-04 16:39:12 -0700 | [diff] [blame] | 421 | struct lm95234_data *data = dev_get_drvdata(dev); |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 422 | int index = to_sensor_dev_attr(attr)->index; |
Guenter Roeck | 9d86bd6 | 2013-09-04 16:39:12 -0700 | [diff] [blame] | 423 | int ret = lm95234_update_device(data); |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 424 | long val; |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 425 | |
| 426 | if (ret) |
| 427 | return ret; |
| 428 | |
| 429 | ret = kstrtol(buf, 10, &val); |
| 430 | if (ret < 0) |
| 431 | return ret; |
| 432 | |
| 433 | /* Accuracy is 1/2 degrees C */ |
| 434 | val = clamp_val(DIV_ROUND_CLOSEST(val, 500), -128, 127); |
| 435 | |
| 436 | mutex_lock(&data->update_lock); |
| 437 | data->toffset[index] = val; |
Guenter Roeck | 9d86bd6 | 2013-09-04 16:39:12 -0700 | [diff] [blame] | 438 | i2c_smbus_write_byte_data(data->client, LM95234_REG_OFFSET(index), val); |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 439 | mutex_unlock(&data->update_lock); |
| 440 | |
| 441 | return count; |
| 442 | } |
| 443 | |
Julia Lawall | 2f2dbea | 2016-12-22 13:05:32 +0100 | [diff] [blame] | 444 | static ssize_t update_interval_show(struct device *dev, |
| 445 | struct device_attribute *attr, char *buf) |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 446 | { |
Guenter Roeck | 9d86bd6 | 2013-09-04 16:39:12 -0700 | [diff] [blame] | 447 | struct lm95234_data *data = dev_get_drvdata(dev); |
| 448 | int ret = lm95234_update_device(data); |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 449 | |
| 450 | if (ret) |
| 451 | return ret; |
| 452 | |
| 453 | return sprintf(buf, "%lu\n", |
| 454 | DIV_ROUND_CLOSEST(data->interval * 1000, HZ)); |
| 455 | } |
| 456 | |
Julia Lawall | 2f2dbea | 2016-12-22 13:05:32 +0100 | [diff] [blame] | 457 | static ssize_t update_interval_store(struct device *dev, |
| 458 | struct device_attribute *attr, |
| 459 | const char *buf, size_t count) |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 460 | { |
Guenter Roeck | 9d86bd6 | 2013-09-04 16:39:12 -0700 | [diff] [blame] | 461 | struct lm95234_data *data = dev_get_drvdata(dev); |
| 462 | int ret = lm95234_update_device(data); |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 463 | unsigned long val; |
| 464 | u8 regval; |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 465 | |
| 466 | if (ret) |
| 467 | return ret; |
| 468 | |
| 469 | ret = kstrtoul(buf, 10, &val); |
| 470 | if (ret < 0) |
| 471 | return ret; |
| 472 | |
| 473 | for (regval = 0; regval < 3; regval++) { |
| 474 | if (val <= update_intervals[regval]) |
| 475 | break; |
| 476 | } |
| 477 | |
| 478 | mutex_lock(&data->update_lock); |
| 479 | data->interval = msecs_to_jiffies(update_intervals[regval]); |
Guenter Roeck | 9d86bd6 | 2013-09-04 16:39:12 -0700 | [diff] [blame] | 480 | i2c_smbus_write_byte_data(data->client, LM95234_REG_CONVRATE, regval); |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 481 | mutex_unlock(&data->update_lock); |
| 482 | |
| 483 | return count; |
| 484 | } |
| 485 | |
Guenter Roeck | 33721d9 | 2018-12-06 10:38:29 -0800 | [diff] [blame] | 486 | static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, 0); |
| 487 | static SENSOR_DEVICE_ATTR_RO(temp2_input, temp, 1); |
| 488 | static SENSOR_DEVICE_ATTR_RO(temp3_input, temp, 2); |
| 489 | static SENSOR_DEVICE_ATTR_RO(temp4_input, temp, 3); |
| 490 | static SENSOR_DEVICE_ATTR_RO(temp5_input, temp, 4); |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 491 | |
Guenter Roeck | 33721d9 | 2018-12-06 10:38:29 -0800 | [diff] [blame] | 492 | static SENSOR_DEVICE_ATTR_RO(temp2_fault, alarm, BIT(0) | BIT(1)); |
| 493 | static SENSOR_DEVICE_ATTR_RO(temp3_fault, alarm, BIT(2) | BIT(3)); |
| 494 | static SENSOR_DEVICE_ATTR_RO(temp4_fault, alarm, BIT(4) | BIT(5)); |
| 495 | static SENSOR_DEVICE_ATTR_RO(temp5_fault, alarm, BIT(6) | BIT(7)); |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 496 | |
Guenter Roeck | 33721d9 | 2018-12-06 10:38:29 -0800 | [diff] [blame] | 497 | static SENSOR_DEVICE_ATTR_RW(temp2_type, type, BIT(1)); |
| 498 | static SENSOR_DEVICE_ATTR_RW(temp3_type, type, BIT(2)); |
| 499 | static SENSOR_DEVICE_ATTR_RW(temp4_type, type, BIT(3)); |
| 500 | static SENSOR_DEVICE_ATTR_RW(temp5_type, type, BIT(4)); |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 501 | |
Guenter Roeck | 33721d9 | 2018-12-06 10:38:29 -0800 | [diff] [blame] | 502 | static SENSOR_DEVICE_ATTR_RW(temp1_max, tcrit1, 0); |
| 503 | static SENSOR_DEVICE_ATTR_RW(temp2_max, tcrit2, 0); |
| 504 | static SENSOR_DEVICE_ATTR_RW(temp3_max, tcrit2, 1); |
| 505 | static SENSOR_DEVICE_ATTR_RW(temp4_max, tcrit1, 3); |
| 506 | static SENSOR_DEVICE_ATTR_RW(temp5_max, tcrit1, 4); |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 507 | |
Guenter Roeck | 33721d9 | 2018-12-06 10:38:29 -0800 | [diff] [blame] | 508 | static SENSOR_DEVICE_ATTR_RW(temp1_max_hyst, tcrit1_hyst, 0); |
| 509 | static SENSOR_DEVICE_ATTR_RO(temp2_max_hyst, tcrit2_hyst, 0); |
| 510 | static SENSOR_DEVICE_ATTR_RO(temp3_max_hyst, tcrit2_hyst, 1); |
| 511 | static SENSOR_DEVICE_ATTR_RO(temp4_max_hyst, tcrit1_hyst, 3); |
| 512 | static SENSOR_DEVICE_ATTR_RO(temp5_max_hyst, tcrit1_hyst, 4); |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 513 | |
Guenter Roeck | 33721d9 | 2018-12-06 10:38:29 -0800 | [diff] [blame] | 514 | static SENSOR_DEVICE_ATTR_RO(temp1_max_alarm, alarm, BIT(0 + 8)); |
| 515 | static SENSOR_DEVICE_ATTR_RO(temp2_max_alarm, alarm, BIT(1 + 16)); |
| 516 | static SENSOR_DEVICE_ATTR_RO(temp3_max_alarm, alarm, BIT(2 + 16)); |
| 517 | static SENSOR_DEVICE_ATTR_RO(temp4_max_alarm, alarm, BIT(3 + 8)); |
| 518 | static SENSOR_DEVICE_ATTR_RO(temp5_max_alarm, alarm, BIT(4 + 8)); |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 519 | |
Guenter Roeck | 33721d9 | 2018-12-06 10:38:29 -0800 | [diff] [blame] | 520 | static SENSOR_DEVICE_ATTR_RW(temp2_crit, tcrit1, 1); |
| 521 | static SENSOR_DEVICE_ATTR_RW(temp3_crit, tcrit1, 2); |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 522 | |
Guenter Roeck | 33721d9 | 2018-12-06 10:38:29 -0800 | [diff] [blame] | 523 | static SENSOR_DEVICE_ATTR_RO(temp2_crit_hyst, tcrit1_hyst, 1); |
| 524 | static SENSOR_DEVICE_ATTR_RO(temp3_crit_hyst, tcrit1_hyst, 2); |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 525 | |
Guenter Roeck | 33721d9 | 2018-12-06 10:38:29 -0800 | [diff] [blame] | 526 | static SENSOR_DEVICE_ATTR_RO(temp2_crit_alarm, alarm, BIT(1 + 8)); |
| 527 | static SENSOR_DEVICE_ATTR_RO(temp3_crit_alarm, alarm, BIT(2 + 8)); |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 528 | |
Guenter Roeck | 33721d9 | 2018-12-06 10:38:29 -0800 | [diff] [blame] | 529 | static SENSOR_DEVICE_ATTR_RW(temp2_offset, offset, 0); |
| 530 | static SENSOR_DEVICE_ATTR_RW(temp3_offset, offset, 1); |
| 531 | static SENSOR_DEVICE_ATTR_RW(temp4_offset, offset, 2); |
| 532 | static SENSOR_DEVICE_ATTR_RW(temp5_offset, offset, 3); |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 533 | |
Julia Lawall | 2f2dbea | 2016-12-22 13:05:32 +0100 | [diff] [blame] | 534 | static DEVICE_ATTR_RW(update_interval); |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 535 | |
Guenter Roeck | dfcd4c5 | 2014-04-22 09:34:14 -0700 | [diff] [blame] | 536 | static struct attribute *lm95234_common_attrs[] = { |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 537 | &sensor_dev_attr_temp1_input.dev_attr.attr, |
| 538 | &sensor_dev_attr_temp2_input.dev_attr.attr, |
| 539 | &sensor_dev_attr_temp3_input.dev_attr.attr, |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 540 | &sensor_dev_attr_temp2_fault.dev_attr.attr, |
| 541 | &sensor_dev_attr_temp3_fault.dev_attr.attr, |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 542 | &sensor_dev_attr_temp2_type.dev_attr.attr, |
| 543 | &sensor_dev_attr_temp3_type.dev_attr.attr, |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 544 | &sensor_dev_attr_temp1_max.dev_attr.attr, |
| 545 | &sensor_dev_attr_temp2_max.dev_attr.attr, |
| 546 | &sensor_dev_attr_temp3_max.dev_attr.attr, |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 547 | &sensor_dev_attr_temp1_max_hyst.dev_attr.attr, |
| 548 | &sensor_dev_attr_temp2_max_hyst.dev_attr.attr, |
| 549 | &sensor_dev_attr_temp3_max_hyst.dev_attr.attr, |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 550 | &sensor_dev_attr_temp1_max_alarm.dev_attr.attr, |
| 551 | &sensor_dev_attr_temp2_max_alarm.dev_attr.attr, |
| 552 | &sensor_dev_attr_temp3_max_alarm.dev_attr.attr, |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 553 | &sensor_dev_attr_temp2_crit.dev_attr.attr, |
| 554 | &sensor_dev_attr_temp3_crit.dev_attr.attr, |
| 555 | &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr, |
| 556 | &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr, |
| 557 | &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr, |
| 558 | &sensor_dev_attr_temp3_crit_alarm.dev_attr.attr, |
| 559 | &sensor_dev_attr_temp2_offset.dev_attr.attr, |
| 560 | &sensor_dev_attr_temp3_offset.dev_attr.attr, |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 561 | &dev_attr_update_interval.attr, |
| 562 | NULL |
| 563 | }; |
Guenter Roeck | dfcd4c5 | 2014-04-22 09:34:14 -0700 | [diff] [blame] | 564 | |
| 565 | static const struct attribute_group lm95234_common_group = { |
| 566 | .attrs = lm95234_common_attrs, |
| 567 | }; |
| 568 | |
| 569 | static struct attribute *lm95234_attrs[] = { |
| 570 | &sensor_dev_attr_temp4_input.dev_attr.attr, |
| 571 | &sensor_dev_attr_temp5_input.dev_attr.attr, |
| 572 | &sensor_dev_attr_temp4_fault.dev_attr.attr, |
| 573 | &sensor_dev_attr_temp5_fault.dev_attr.attr, |
| 574 | &sensor_dev_attr_temp4_type.dev_attr.attr, |
| 575 | &sensor_dev_attr_temp5_type.dev_attr.attr, |
| 576 | &sensor_dev_attr_temp4_max.dev_attr.attr, |
| 577 | &sensor_dev_attr_temp5_max.dev_attr.attr, |
| 578 | &sensor_dev_attr_temp4_max_hyst.dev_attr.attr, |
| 579 | &sensor_dev_attr_temp5_max_hyst.dev_attr.attr, |
| 580 | &sensor_dev_attr_temp4_max_alarm.dev_attr.attr, |
| 581 | &sensor_dev_attr_temp5_max_alarm.dev_attr.attr, |
| 582 | &sensor_dev_attr_temp4_offset.dev_attr.attr, |
| 583 | &sensor_dev_attr_temp5_offset.dev_attr.attr, |
| 584 | NULL |
| 585 | }; |
| 586 | |
| 587 | static const struct attribute_group lm95234_group = { |
| 588 | .attrs = lm95234_attrs, |
| 589 | }; |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 590 | |
| 591 | static int lm95234_detect(struct i2c_client *client, |
| 592 | struct i2c_board_info *info) |
| 593 | { |
| 594 | struct i2c_adapter *adapter = client->adapter; |
Guenter Roeck | dfcd4c5 | 2014-04-22 09:34:14 -0700 | [diff] [blame] | 595 | int address = client->addr; |
| 596 | u8 config_mask, model_mask; |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 597 | int mfg_id, chip_id, val; |
Guenter Roeck | dfcd4c5 | 2014-04-22 09:34:14 -0700 | [diff] [blame] | 598 | const char *name; |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 599 | |
| 600 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) |
| 601 | return -ENODEV; |
| 602 | |
| 603 | mfg_id = i2c_smbus_read_byte_data(client, LM95234_REG_MAN_ID); |
| 604 | if (mfg_id != NATSEMI_MAN_ID) |
| 605 | return -ENODEV; |
| 606 | |
| 607 | chip_id = i2c_smbus_read_byte_data(client, LM95234_REG_CHIP_ID); |
Guenter Roeck | dfcd4c5 | 2014-04-22 09:34:14 -0700 | [diff] [blame] | 608 | switch (chip_id) { |
| 609 | case LM95233_CHIP_ID: |
| 610 | if (address != 0x18 && address != 0x2a && address != 0x2b) |
| 611 | return -ENODEV; |
| 612 | config_mask = 0xbf; |
| 613 | model_mask = 0xf9; |
| 614 | name = "lm95233"; |
| 615 | break; |
| 616 | case LM95234_CHIP_ID: |
| 617 | if (address != 0x18 && address != 0x4d && address != 0x4e) |
| 618 | return -ENODEV; |
| 619 | config_mask = 0xbc; |
| 620 | model_mask = 0xe1; |
| 621 | name = "lm95234"; |
| 622 | break; |
| 623 | default: |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 624 | return -ENODEV; |
Guenter Roeck | dfcd4c5 | 2014-04-22 09:34:14 -0700 | [diff] [blame] | 625 | } |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 626 | |
| 627 | val = i2c_smbus_read_byte_data(client, LM95234_REG_STATUS); |
| 628 | if (val & 0x30) |
| 629 | return -ENODEV; |
| 630 | |
| 631 | val = i2c_smbus_read_byte_data(client, LM95234_REG_CONFIG); |
Guenter Roeck | dfcd4c5 | 2014-04-22 09:34:14 -0700 | [diff] [blame] | 632 | if (val & config_mask) |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 633 | return -ENODEV; |
| 634 | |
| 635 | val = i2c_smbus_read_byte_data(client, LM95234_REG_CONVRATE); |
| 636 | if (val & 0xfc) |
| 637 | return -ENODEV; |
| 638 | |
| 639 | val = i2c_smbus_read_byte_data(client, LM95234_REG_REM_MODEL); |
Guenter Roeck | dfcd4c5 | 2014-04-22 09:34:14 -0700 | [diff] [blame] | 640 | if (val & model_mask) |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 641 | return -ENODEV; |
| 642 | |
| 643 | val = i2c_smbus_read_byte_data(client, LM95234_REG_REM_MODEL_STS); |
Guenter Roeck | dfcd4c5 | 2014-04-22 09:34:14 -0700 | [diff] [blame] | 644 | if (val & model_mask) |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 645 | return -ENODEV; |
| 646 | |
Guenter Roeck | dfcd4c5 | 2014-04-22 09:34:14 -0700 | [diff] [blame] | 647 | strlcpy(info->type, name, I2C_NAME_SIZE); |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 648 | return 0; |
| 649 | } |
| 650 | |
| 651 | static int lm95234_init_client(struct i2c_client *client) |
| 652 | { |
| 653 | int val, model; |
| 654 | |
| 655 | /* start conversion if necessary */ |
| 656 | val = i2c_smbus_read_byte_data(client, LM95234_REG_CONFIG); |
| 657 | if (val < 0) |
| 658 | return val; |
| 659 | if (val & 0x40) |
| 660 | i2c_smbus_write_byte_data(client, LM95234_REG_CONFIG, |
| 661 | val & ~0x40); |
| 662 | |
| 663 | /* If diode type status reports an error, try to fix it */ |
| 664 | val = i2c_smbus_read_byte_data(client, LM95234_REG_REM_MODEL_STS); |
| 665 | if (val < 0) |
| 666 | return val; |
| 667 | model = i2c_smbus_read_byte_data(client, LM95234_REG_REM_MODEL); |
| 668 | if (model < 0) |
| 669 | return model; |
| 670 | if (model & val) { |
| 671 | dev_notice(&client->dev, |
| 672 | "Fixing remote diode type misconfiguration (0x%x)\n", |
| 673 | val); |
| 674 | i2c_smbus_write_byte_data(client, LM95234_REG_REM_MODEL, |
| 675 | model & ~val); |
| 676 | } |
| 677 | return 0; |
| 678 | } |
| 679 | |
Stephen Kitt | 6748703 | 2020-08-13 18:02:22 +0200 | [diff] [blame] | 680 | static const struct i2c_device_id lm95234_id[]; |
| 681 | |
| 682 | static int lm95234_probe(struct i2c_client *client) |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 683 | { |
| 684 | struct device *dev = &client->dev; |
| 685 | struct lm95234_data *data; |
Guenter Roeck | 9d86bd6 | 2013-09-04 16:39:12 -0700 | [diff] [blame] | 686 | struct device *hwmon_dev; |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 687 | int err; |
| 688 | |
| 689 | data = devm_kzalloc(dev, sizeof(struct lm95234_data), GFP_KERNEL); |
| 690 | if (!data) |
| 691 | return -ENOMEM; |
| 692 | |
Guenter Roeck | 9d86bd6 | 2013-09-04 16:39:12 -0700 | [diff] [blame] | 693 | data->client = client; |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 694 | mutex_init(&data->update_lock); |
| 695 | |
| 696 | /* Initialize the LM95234 chip */ |
| 697 | err = lm95234_init_client(client); |
| 698 | if (err < 0) |
| 699 | return err; |
| 700 | |
Guenter Roeck | dfcd4c5 | 2014-04-22 09:34:14 -0700 | [diff] [blame] | 701 | data->groups[0] = &lm95234_common_group; |
Stephen Kitt | 6748703 | 2020-08-13 18:02:22 +0200 | [diff] [blame] | 702 | if (i2c_match_id(lm95234_id, client)->driver_data == lm95234) |
Guenter Roeck | dfcd4c5 | 2014-04-22 09:34:14 -0700 | [diff] [blame] | 703 | data->groups[1] = &lm95234_group; |
| 704 | |
Guenter Roeck | 9d86bd6 | 2013-09-04 16:39:12 -0700 | [diff] [blame] | 705 | hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, |
Guenter Roeck | dfcd4c5 | 2014-04-22 09:34:14 -0700 | [diff] [blame] | 706 | data, data->groups); |
Fengguang Wu | 3ea85ba | 2013-09-17 20:54:14 -0700 | [diff] [blame] | 707 | return PTR_ERR_OR_ZERO(hwmon_dev); |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | /* Driver data (common to all clients) */ |
| 711 | static const struct i2c_device_id lm95234_id[] = { |
Guenter Roeck | dfcd4c5 | 2014-04-22 09:34:14 -0700 | [diff] [blame] | 712 | { "lm95233", lm95233 }, |
| 713 | { "lm95234", lm95234 }, |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 714 | { } |
| 715 | }; |
| 716 | MODULE_DEVICE_TABLE(i2c, lm95234_id); |
| 717 | |
| 718 | static struct i2c_driver lm95234_driver = { |
| 719 | .class = I2C_CLASS_HWMON, |
| 720 | .driver = { |
| 721 | .name = DRVNAME, |
| 722 | }, |
Stephen Kitt | 6748703 | 2020-08-13 18:02:22 +0200 | [diff] [blame] | 723 | .probe_new = lm95234_probe, |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 724 | .id_table = lm95234_id, |
| 725 | .detect = lm95234_detect, |
| 726 | .address_list = normal_i2c, |
| 727 | }; |
| 728 | |
| 729 | module_i2c_driver(lm95234_driver); |
| 730 | |
| 731 | MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>"); |
Guenter Roeck | dfcd4c5 | 2014-04-22 09:34:14 -0700 | [diff] [blame] | 732 | MODULE_DESCRIPTION("LM95233/LM95234 sensor driver"); |
Guenter Roeck | e1eb490 | 2013-03-10 16:54:19 -0700 | [diff] [blame] | 733 | MODULE_LICENSE("GPL"); |