Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 Alexander Stein <alexander.stein@systec-electronic.com> |
| 3 | * |
| 4 | * The LM95245 is a sensor chip made by National Semiconductors. |
| 5 | * It reports up to two temperatures (its own plus an external one). |
| 6 | * Complete datasheet can be obtained from National's website at: |
| 7 | * http://www.national.com/ds.cgi/LM/LM95245.pdf |
| 8 | * |
| 9 | * This driver is based on lm95241.c |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 2 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License |
| 22 | * along with this program; if not, write to the Free Software |
| 23 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 24 | */ |
| 25 | |
| 26 | #include <linux/module.h> |
| 27 | #include <linux/init.h> |
| 28 | #include <linux/slab.h> |
| 29 | #include <linux/jiffies.h> |
| 30 | #include <linux/i2c.h> |
| 31 | #include <linux/hwmon.h> |
| 32 | #include <linux/hwmon-sysfs.h> |
| 33 | #include <linux/err.h> |
| 34 | #include <linux/mutex.h> |
| 35 | #include <linux/sysfs.h> |
| 36 | |
| 37 | #define DEVNAME "lm95245" |
| 38 | |
| 39 | static const unsigned short normal_i2c[] = { |
| 40 | 0x18, 0x19, 0x29, 0x4c, 0x4d, I2C_CLIENT_END }; |
| 41 | |
| 42 | /* LM95245 registers */ |
| 43 | /* general registers */ |
| 44 | #define LM95245_REG_RW_CONFIG1 0x03 |
| 45 | #define LM95245_REG_RW_CONVERS_RATE 0x04 |
| 46 | #define LM95245_REG_W_ONE_SHOT 0x0F |
| 47 | |
| 48 | /* diode configuration */ |
| 49 | #define LM95245_REG_RW_CONFIG2 0xBF |
| 50 | #define LM95245_REG_RW_REMOTE_OFFH 0x11 |
| 51 | #define LM95245_REG_RW_REMOTE_OFFL 0x12 |
| 52 | |
| 53 | /* status registers */ |
| 54 | #define LM95245_REG_R_STATUS1 0x02 |
| 55 | #define LM95245_REG_R_STATUS2 0x33 |
| 56 | |
| 57 | /* limit registers */ |
| 58 | #define LM95245_REG_RW_REMOTE_OS_LIMIT 0x07 |
| 59 | #define LM95245_REG_RW_LOCAL_OS_TCRIT_LIMIT 0x20 |
| 60 | #define LM95245_REG_RW_REMOTE_TCRIT_LIMIT 0x19 |
| 61 | #define LM95245_REG_RW_COMMON_HYSTERESIS 0x21 |
| 62 | |
| 63 | /* temperature signed */ |
| 64 | #define LM95245_REG_R_LOCAL_TEMPH_S 0x00 |
| 65 | #define LM95245_REG_R_LOCAL_TEMPL_S 0x30 |
| 66 | #define LM95245_REG_R_REMOTE_TEMPH_S 0x01 |
| 67 | #define LM95245_REG_R_REMOTE_TEMPL_S 0x10 |
| 68 | /* temperature unsigned */ |
| 69 | #define LM95245_REG_R_REMOTE_TEMPH_U 0x31 |
| 70 | #define LM95245_REG_R_REMOTE_TEMPL_U 0x32 |
| 71 | |
| 72 | /* id registers */ |
| 73 | #define LM95245_REG_R_MAN_ID 0xFE |
| 74 | #define LM95245_REG_R_CHIP_ID 0xFF |
| 75 | |
| 76 | /* LM95245 specific bitfields */ |
| 77 | #define CFG_STOP 0x40 |
| 78 | #define CFG_REMOTE_TCRIT_MASK 0x10 |
| 79 | #define CFG_REMOTE_OS_MASK 0x08 |
| 80 | #define CFG_LOCAL_TCRIT_MASK 0x04 |
| 81 | #define CFG_LOCAL_OS_MASK 0x02 |
| 82 | |
| 83 | #define CFG2_OS_A0 0x40 |
| 84 | #define CFG2_DIODE_FAULT_OS 0x20 |
| 85 | #define CFG2_DIODE_FAULT_TCRIT 0x10 |
| 86 | #define CFG2_REMOTE_TT 0x08 |
| 87 | #define CFG2_REMOTE_FILTER_DIS 0x00 |
| 88 | #define CFG2_REMOTE_FILTER_EN 0x06 |
| 89 | |
| 90 | /* conversation rate in ms */ |
| 91 | #define RATE_CR0063 0x00 |
| 92 | #define RATE_CR0364 0x01 |
| 93 | #define RATE_CR1000 0x02 |
| 94 | #define RATE_CR2500 0x03 |
| 95 | |
| 96 | #define STATUS1_DIODE_FAULT 0x04 |
| 97 | #define STATUS1_RTCRIT 0x02 |
| 98 | #define STATUS1_LOC 0x01 |
| 99 | |
| 100 | #define MANUFACTURER_ID 0x01 |
| 101 | #define DEFAULT_REVISION 0xB3 |
| 102 | |
| 103 | static const u8 lm95245_reg_address[] = { |
| 104 | LM95245_REG_R_LOCAL_TEMPH_S, |
| 105 | LM95245_REG_R_LOCAL_TEMPL_S, |
| 106 | LM95245_REG_R_REMOTE_TEMPH_S, |
| 107 | LM95245_REG_R_REMOTE_TEMPL_S, |
| 108 | LM95245_REG_R_REMOTE_TEMPH_U, |
| 109 | LM95245_REG_R_REMOTE_TEMPL_U, |
| 110 | LM95245_REG_RW_LOCAL_OS_TCRIT_LIMIT, |
| 111 | LM95245_REG_RW_REMOTE_TCRIT_LIMIT, |
| 112 | LM95245_REG_RW_COMMON_HYSTERESIS, |
| 113 | LM95245_REG_R_STATUS1, |
| 114 | }; |
| 115 | |
| 116 | /* Client data (each client gets its own) */ |
| 117 | struct lm95245_data { |
Guenter Roeck | 7276d55 | 2014-01-20 09:17:16 -0800 | [diff] [blame] | 118 | struct i2c_client *client; |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 119 | struct mutex update_lock; |
| 120 | unsigned long last_updated; /* in jiffies */ |
| 121 | unsigned long interval; /* in msecs */ |
| 122 | bool valid; /* zero until following fields are valid */ |
| 123 | /* registers values */ |
| 124 | u8 regs[ARRAY_SIZE(lm95245_reg_address)]; |
| 125 | u8 config1, config2; |
| 126 | }; |
| 127 | |
| 128 | /* Conversions */ |
| 129 | static int temp_from_reg_unsigned(u8 val_h, u8 val_l) |
| 130 | { |
| 131 | return val_h * 1000 + val_l * 1000 / 256; |
| 132 | } |
| 133 | |
| 134 | static int temp_from_reg_signed(u8 val_h, u8 val_l) |
| 135 | { |
| 136 | if (val_h & 0x80) |
| 137 | return (val_h - 0x100) * 1000; |
| 138 | return temp_from_reg_unsigned(val_h, val_l); |
| 139 | } |
| 140 | |
| 141 | static struct lm95245_data *lm95245_update_device(struct device *dev) |
| 142 | { |
Guenter Roeck | 7276d55 | 2014-01-20 09:17:16 -0800 | [diff] [blame] | 143 | struct lm95245_data *data = dev_get_drvdata(dev); |
| 144 | struct i2c_client *client = data->client; |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 145 | |
| 146 | mutex_lock(&data->update_lock); |
| 147 | |
| 148 | if (time_after(jiffies, data->last_updated |
| 149 | + msecs_to_jiffies(data->interval)) || !data->valid) { |
| 150 | int i; |
| 151 | |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 152 | for (i = 0; i < ARRAY_SIZE(lm95245_reg_address); i++) |
| 153 | data->regs[i] |
| 154 | = i2c_smbus_read_byte_data(client, |
| 155 | lm95245_reg_address[i]); |
| 156 | data->last_updated = jiffies; |
| 157 | data->valid = 1; |
| 158 | } |
| 159 | |
| 160 | mutex_unlock(&data->update_lock); |
| 161 | |
| 162 | return data; |
| 163 | } |
| 164 | |
| 165 | static unsigned long lm95245_read_conversion_rate(struct i2c_client *client) |
| 166 | { |
| 167 | int rate; |
| 168 | unsigned long interval; |
| 169 | |
| 170 | rate = i2c_smbus_read_byte_data(client, LM95245_REG_RW_CONVERS_RATE); |
| 171 | |
| 172 | switch (rate) { |
| 173 | case RATE_CR0063: |
| 174 | interval = 63; |
| 175 | break; |
| 176 | case RATE_CR0364: |
| 177 | interval = 364; |
| 178 | break; |
| 179 | case RATE_CR1000: |
| 180 | interval = 1000; |
| 181 | break; |
| 182 | case RATE_CR2500: |
| 183 | default: |
| 184 | interval = 2500; |
| 185 | break; |
| 186 | } |
| 187 | |
| 188 | return interval; |
| 189 | } |
| 190 | |
| 191 | static unsigned long lm95245_set_conversion_rate(struct i2c_client *client, |
| 192 | unsigned long interval) |
| 193 | { |
| 194 | int rate; |
| 195 | |
| 196 | if (interval <= 63) { |
| 197 | interval = 63; |
| 198 | rate = RATE_CR0063; |
| 199 | } else if (interval <= 364) { |
| 200 | interval = 364; |
| 201 | rate = RATE_CR0364; |
| 202 | } else if (interval <= 1000) { |
| 203 | interval = 1000; |
| 204 | rate = RATE_CR1000; |
| 205 | } else { |
| 206 | interval = 2500; |
| 207 | rate = RATE_CR2500; |
| 208 | } |
| 209 | |
| 210 | i2c_smbus_write_byte_data(client, LM95245_REG_RW_CONVERS_RATE, rate); |
| 211 | |
| 212 | return interval; |
| 213 | } |
| 214 | |
| 215 | /* Sysfs stuff */ |
| 216 | static ssize_t show_input(struct device *dev, struct device_attribute *attr, |
| 217 | char *buf) |
| 218 | { |
| 219 | struct lm95245_data *data = lm95245_update_device(dev); |
| 220 | int temp; |
| 221 | int index = to_sensor_dev_attr(attr)->index; |
| 222 | |
| 223 | /* |
| 224 | * Index 0 (Local temp) is always signed |
| 225 | * Index 2 (Remote temp) has both signed and unsigned data |
| 226 | * use signed calculation for remote if signed bit is set |
| 227 | */ |
| 228 | if (index == 0 || data->regs[index] & 0x80) |
| 229 | temp = temp_from_reg_signed(data->regs[index], |
| 230 | data->regs[index + 1]); |
| 231 | else |
| 232 | temp = temp_from_reg_unsigned(data->regs[index + 2], |
| 233 | data->regs[index + 3]); |
| 234 | |
| 235 | return snprintf(buf, PAGE_SIZE - 1, "%d\n", temp); |
| 236 | } |
| 237 | |
| 238 | static ssize_t show_limit(struct device *dev, struct device_attribute *attr, |
| 239 | char *buf) |
| 240 | { |
| 241 | struct lm95245_data *data = lm95245_update_device(dev); |
| 242 | int index = to_sensor_dev_attr(attr)->index; |
| 243 | |
| 244 | return snprintf(buf, PAGE_SIZE - 1, "%d\n", |
| 245 | data->regs[index] * 1000); |
| 246 | } |
| 247 | |
| 248 | static ssize_t set_limit(struct device *dev, struct device_attribute *attr, |
| 249 | const char *buf, size_t count) |
| 250 | { |
Guenter Roeck | 7276d55 | 2014-01-20 09:17:16 -0800 | [diff] [blame] | 251 | struct lm95245_data *data = dev_get_drvdata(dev); |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 252 | int index = to_sensor_dev_attr(attr)->index; |
Guenter Roeck | 7276d55 | 2014-01-20 09:17:16 -0800 | [diff] [blame] | 253 | struct i2c_client *client = data->client; |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 254 | unsigned long val; |
| 255 | |
Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 256 | if (kstrtoul(buf, 10, &val) < 0) |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 257 | return -EINVAL; |
| 258 | |
| 259 | val /= 1000; |
| 260 | |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 261 | val = clamp_val(val, 0, (index == 6 ? 127 : 255)); |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 262 | |
| 263 | mutex_lock(&data->update_lock); |
| 264 | |
| 265 | data->valid = 0; |
| 266 | |
| 267 | i2c_smbus_write_byte_data(client, lm95245_reg_address[index], val); |
| 268 | |
| 269 | mutex_unlock(&data->update_lock); |
| 270 | |
| 271 | return count; |
| 272 | } |
| 273 | |
Guenter Roeck | 408c15ea | 2014-02-02 18:29:28 -0800 | [diff] [blame] | 274 | static ssize_t show_crit_hyst(struct device *dev, struct device_attribute *attr, |
| 275 | char *buf) |
| 276 | { |
| 277 | struct lm95245_data *data = lm95245_update_device(dev); |
| 278 | int index = to_sensor_dev_attr(attr)->index; |
| 279 | int hyst = data->regs[index] - data->regs[8]; |
| 280 | |
| 281 | return snprintf(buf, PAGE_SIZE - 1, "%d\n", hyst * 1000); |
| 282 | } |
| 283 | |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 284 | static ssize_t set_crit_hyst(struct device *dev, struct device_attribute *attr, |
| 285 | const char *buf, size_t count) |
| 286 | { |
Guenter Roeck | 7276d55 | 2014-01-20 09:17:16 -0800 | [diff] [blame] | 287 | struct lm95245_data *data = dev_get_drvdata(dev); |
Guenter Roeck | 408c15ea | 2014-02-02 18:29:28 -0800 | [diff] [blame] | 288 | int index = to_sensor_dev_attr(attr)->index; |
Guenter Roeck | 7276d55 | 2014-01-20 09:17:16 -0800 | [diff] [blame] | 289 | struct i2c_client *client = data->client; |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 290 | unsigned long val; |
Guenter Roeck | 408c15ea | 2014-02-02 18:29:28 -0800 | [diff] [blame] | 291 | int hyst, limit; |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 292 | |
Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 293 | if (kstrtoul(buf, 10, &val) < 0) |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 294 | return -EINVAL; |
| 295 | |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 296 | mutex_lock(&data->update_lock); |
| 297 | |
Guenter Roeck | 408c15ea | 2014-02-02 18:29:28 -0800 | [diff] [blame] | 298 | limit = i2c_smbus_read_byte_data(client, lm95245_reg_address[index]); |
| 299 | hyst = limit - val / 1000; |
| 300 | hyst = clamp_val(hyst, 0, 31); |
| 301 | data->regs[8] = hyst; |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 302 | |
| 303 | /* shared crit hysteresis */ |
| 304 | i2c_smbus_write_byte_data(client, LM95245_REG_RW_COMMON_HYSTERESIS, |
Guenter Roeck | 408c15ea | 2014-02-02 18:29:28 -0800 | [diff] [blame] | 305 | hyst); |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 306 | |
| 307 | mutex_unlock(&data->update_lock); |
| 308 | |
| 309 | return count; |
| 310 | } |
| 311 | |
| 312 | static ssize_t show_type(struct device *dev, struct device_attribute *attr, |
| 313 | char *buf) |
| 314 | { |
Guenter Roeck | 7276d55 | 2014-01-20 09:17:16 -0800 | [diff] [blame] | 315 | struct lm95245_data *data = dev_get_drvdata(dev); |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 316 | |
| 317 | return snprintf(buf, PAGE_SIZE - 1, |
| 318 | data->config2 & CFG2_REMOTE_TT ? "1\n" : "2\n"); |
| 319 | } |
| 320 | |
| 321 | static ssize_t set_type(struct device *dev, struct device_attribute *attr, |
| 322 | const char *buf, size_t count) |
| 323 | { |
Guenter Roeck | 7276d55 | 2014-01-20 09:17:16 -0800 | [diff] [blame] | 324 | struct lm95245_data *data = dev_get_drvdata(dev); |
| 325 | struct i2c_client *client = data->client; |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 326 | unsigned long val; |
| 327 | |
Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 328 | if (kstrtoul(buf, 10, &val) < 0) |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 329 | return -EINVAL; |
| 330 | if (val != 1 && val != 2) |
| 331 | return -EINVAL; |
| 332 | |
| 333 | mutex_lock(&data->update_lock); |
| 334 | |
| 335 | if (val == 1) |
| 336 | data->config2 |= CFG2_REMOTE_TT; |
| 337 | else |
| 338 | data->config2 &= ~CFG2_REMOTE_TT; |
| 339 | |
| 340 | data->valid = 0; |
| 341 | |
| 342 | i2c_smbus_write_byte_data(client, LM95245_REG_RW_CONFIG2, |
| 343 | data->config2); |
| 344 | |
| 345 | mutex_unlock(&data->update_lock); |
| 346 | |
| 347 | return count; |
| 348 | } |
| 349 | |
| 350 | static ssize_t show_alarm(struct device *dev, struct device_attribute *attr, |
| 351 | char *buf) |
| 352 | { |
| 353 | struct lm95245_data *data = lm95245_update_device(dev); |
| 354 | int index = to_sensor_dev_attr(attr)->index; |
| 355 | |
| 356 | return snprintf(buf, PAGE_SIZE - 1, "%d\n", |
| 357 | !!(data->regs[9] & index)); |
| 358 | } |
| 359 | |
| 360 | static ssize_t show_interval(struct device *dev, struct device_attribute *attr, |
| 361 | char *buf) |
| 362 | { |
| 363 | struct lm95245_data *data = lm95245_update_device(dev); |
| 364 | |
| 365 | return snprintf(buf, PAGE_SIZE - 1, "%lu\n", data->interval); |
| 366 | } |
| 367 | |
| 368 | static ssize_t set_interval(struct device *dev, struct device_attribute *attr, |
| 369 | const char *buf, size_t count) |
| 370 | { |
Guenter Roeck | 7276d55 | 2014-01-20 09:17:16 -0800 | [diff] [blame] | 371 | struct lm95245_data *data = dev_get_drvdata(dev); |
| 372 | struct i2c_client *client = data->client; |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 373 | unsigned long val; |
| 374 | |
Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 375 | if (kstrtoul(buf, 10, &val) < 0) |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 376 | return -EINVAL; |
| 377 | |
| 378 | mutex_lock(&data->update_lock); |
| 379 | |
| 380 | data->interval = lm95245_set_conversion_rate(client, val); |
| 381 | |
| 382 | mutex_unlock(&data->update_lock); |
| 383 | |
| 384 | return count; |
| 385 | } |
| 386 | |
| 387 | static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_input, NULL, 0); |
| 388 | static SENSOR_DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_limit, |
| 389 | set_limit, 6); |
Guenter Roeck | 408c15ea | 2014-02-02 18:29:28 -0800 | [diff] [blame] | 390 | static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, show_crit_hyst, |
| 391 | set_crit_hyst, 6); |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 392 | static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, |
| 393 | STATUS1_LOC); |
| 394 | |
| 395 | static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_input, NULL, 2); |
| 396 | static SENSOR_DEVICE_ATTR(temp2_crit, S_IWUSR | S_IRUGO, show_limit, |
| 397 | set_limit, 7); |
Guenter Roeck | a41a892 | 2014-02-22 08:17:34 -0800 | [diff] [blame] | 398 | static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, show_crit_hyst, NULL, 7); |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 399 | static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, |
| 400 | STATUS1_RTCRIT); |
| 401 | static SENSOR_DEVICE_ATTR(temp2_type, S_IWUSR | S_IRUGO, show_type, |
| 402 | set_type, 0); |
| 403 | static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, |
| 404 | STATUS1_DIODE_FAULT); |
| 405 | |
| 406 | static DEVICE_ATTR(update_interval, S_IWUSR | S_IRUGO, show_interval, |
| 407 | set_interval); |
| 408 | |
Guenter Roeck | 7276d55 | 2014-01-20 09:17:16 -0800 | [diff] [blame] | 409 | static struct attribute *lm95245_attrs[] = { |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 410 | &sensor_dev_attr_temp1_input.dev_attr.attr, |
| 411 | &sensor_dev_attr_temp1_crit.dev_attr.attr, |
| 412 | &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr, |
| 413 | &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr, |
| 414 | &sensor_dev_attr_temp2_input.dev_attr.attr, |
| 415 | &sensor_dev_attr_temp2_crit.dev_attr.attr, |
| 416 | &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr, |
| 417 | &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr, |
| 418 | &sensor_dev_attr_temp2_type.dev_attr.attr, |
| 419 | &sensor_dev_attr_temp2_fault.dev_attr.attr, |
| 420 | &dev_attr_update_interval.attr, |
| 421 | NULL |
| 422 | }; |
Guenter Roeck | 7276d55 | 2014-01-20 09:17:16 -0800 | [diff] [blame] | 423 | ATTRIBUTE_GROUPS(lm95245); |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 424 | |
| 425 | /* Return 0 if detection is successful, -ENODEV otherwise */ |
| 426 | static int lm95245_detect(struct i2c_client *new_client, |
| 427 | struct i2c_board_info *info) |
| 428 | { |
| 429 | struct i2c_adapter *adapter = new_client->adapter; |
| 430 | |
| 431 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) |
| 432 | return -ENODEV; |
| 433 | |
| 434 | if (i2c_smbus_read_byte_data(new_client, LM95245_REG_R_MAN_ID) |
| 435 | != MANUFACTURER_ID |
| 436 | || i2c_smbus_read_byte_data(new_client, LM95245_REG_R_CHIP_ID) |
| 437 | != DEFAULT_REVISION) |
| 438 | return -ENODEV; |
| 439 | |
| 440 | strlcpy(info->type, DEVNAME, I2C_NAME_SIZE); |
| 441 | return 0; |
| 442 | } |
| 443 | |
Guenter Roeck | 7276d55 | 2014-01-20 09:17:16 -0800 | [diff] [blame] | 444 | static void lm95245_init_client(struct i2c_client *client, |
| 445 | struct lm95245_data *data) |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 446 | { |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 447 | data->interval = lm95245_read_conversion_rate(client); |
| 448 | |
| 449 | data->config1 = i2c_smbus_read_byte_data(client, |
| 450 | LM95245_REG_RW_CONFIG1); |
| 451 | data->config2 = i2c_smbus_read_byte_data(client, |
| 452 | LM95245_REG_RW_CONFIG2); |
| 453 | |
| 454 | if (data->config1 & CFG_STOP) { |
| 455 | /* Clear the standby bit */ |
| 456 | data->config1 &= ~CFG_STOP; |
| 457 | i2c_smbus_write_byte_data(client, LM95245_REG_RW_CONFIG1, |
| 458 | data->config1); |
| 459 | } |
| 460 | } |
| 461 | |
Guenter Roeck | 7276d55 | 2014-01-20 09:17:16 -0800 | [diff] [blame] | 462 | static int lm95245_probe(struct i2c_client *client, |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 463 | const struct i2c_device_id *id) |
| 464 | { |
Guenter Roeck | 7276d55 | 2014-01-20 09:17:16 -0800 | [diff] [blame] | 465 | struct device *dev = &client->dev; |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 466 | struct lm95245_data *data; |
Guenter Roeck | 7276d55 | 2014-01-20 09:17:16 -0800 | [diff] [blame] | 467 | struct device *hwmon_dev; |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 468 | |
Guenter Roeck | 7276d55 | 2014-01-20 09:17:16 -0800 | [diff] [blame] | 469 | data = devm_kzalloc(dev, sizeof(struct lm95245_data), GFP_KERNEL); |
Guenter Roeck | a8dd946 | 2012-06-02 09:58:12 -0700 | [diff] [blame] | 470 | if (!data) |
| 471 | return -ENOMEM; |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 472 | |
Guenter Roeck | 7276d55 | 2014-01-20 09:17:16 -0800 | [diff] [blame] | 473 | data->client = client; |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 474 | mutex_init(&data->update_lock); |
| 475 | |
| 476 | /* Initialize the LM95245 chip */ |
Guenter Roeck | 7276d55 | 2014-01-20 09:17:16 -0800 | [diff] [blame] | 477 | lm95245_init_client(client, data); |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 478 | |
Guenter Roeck | 7276d55 | 2014-01-20 09:17:16 -0800 | [diff] [blame] | 479 | hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, |
| 480 | data, |
| 481 | lm95245_groups); |
| 482 | return PTR_ERR_OR_ZERO(hwmon_dev); |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | /* Driver data (common to all clients) */ |
| 486 | static const struct i2c_device_id lm95245_id[] = { |
| 487 | { DEVNAME, 0 }, |
| 488 | { } |
| 489 | }; |
| 490 | MODULE_DEVICE_TABLE(i2c, lm95245_id); |
| 491 | |
| 492 | static struct i2c_driver lm95245_driver = { |
| 493 | .class = I2C_CLASS_HWMON, |
| 494 | .driver = { |
| 495 | .name = DEVNAME, |
| 496 | }, |
| 497 | .probe = lm95245_probe, |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 498 | .id_table = lm95245_id, |
| 499 | .detect = lm95245_detect, |
| 500 | .address_list = normal_i2c, |
| 501 | }; |
| 502 | |
Axel Lin | f0967ee | 2012-01-20 15:38:18 +0800 | [diff] [blame] | 503 | module_i2c_driver(lm95245_driver); |
Alexander Stein | fffd80c | 2011-06-28 15:11:23 +0000 | [diff] [blame] | 504 | |
| 505 | MODULE_AUTHOR("Alexander Stein <alexander.stein@systec-electronic.com>"); |
| 506 | MODULE_DESCRIPTION("LM95245 sensor driver"); |
| 507 | MODULE_LICENSE("GPL"); |