1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Driver for the ADT7411 (I2C/SPI 8 channel 10 bit ADC & temperature-sensor) 4 * 5 * Copyright (C) 2008, 2010 Pengutronix 6 * 7 * TODO: SPI, use power-down mode for suspend?, interrupt handling? 8 */ 9 10 #include <linux/kernel.h> 11 #include <linux/module.h> 12 #include <linux/init.h> 13 #include <linux/err.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/slab.h> 19 20 #define ADT7411_REG_STAT_1 0x00 21 #define ADT7411_STAT_1_INT_TEMP_HIGH BIT(0) 22 #define ADT7411_STAT_1_INT_TEMP_LOW BIT(1) 23 #define ADT7411_STAT_1_EXT_TEMP_HIGH_AIN1 BIT(2) 24 #define ADT7411_STAT_1_EXT_TEMP_LOW BIT(3) 25 #define ADT7411_STAT_1_EXT_TEMP_FAULT BIT(4) 26 #define ADT7411_STAT_1_AIN2 BIT(5) 27 #define ADT7411_STAT_1_AIN3 BIT(6) 28 #define ADT7411_STAT_1_AIN4 BIT(7) 29 #define ADT7411_REG_STAT_2 0x01 30 #define ADT7411_STAT_2_AIN5 BIT(0) 31 #define ADT7411_STAT_2_AIN6 BIT(1) 32 #define ADT7411_STAT_2_AIN7 BIT(2) 33 #define ADT7411_STAT_2_AIN8 BIT(3) 34 #define ADT7411_STAT_2_VDD BIT(4) 35 #define ADT7411_REG_INT_TEMP_VDD_LSB 0x03 36 #define ADT7411_REG_EXT_TEMP_AIN14_LSB 0x04 37 #define ADT7411_REG_VDD_MSB 0x06 38 #define ADT7411_REG_INT_TEMP_MSB 0x07 39 #define ADT7411_REG_EXT_TEMP_AIN1_MSB 0x08 40 41 #define ADT7411_REG_CFG1 0x18 42 #define ADT7411_CFG1_START_MONITOR BIT(0) 43 #define ADT7411_CFG1_RESERVED_BIT1 BIT(1) 44 #define ADT7411_CFG1_EXT_TDM BIT(2) 45 #define ADT7411_CFG1_RESERVED_BIT3 BIT(3) 46 47 #define ADT7411_REG_CFG2 0x19 48 #define ADT7411_CFG2_DISABLE_AVG BIT(5) 49 50 #define ADT7411_REG_CFG3 0x1a 51 #define ADT7411_CFG3_ADC_CLK_225 BIT(0) 52 #define ADT7411_CFG3_RESERVED_BIT1 BIT(1) 53 #define ADT7411_CFG3_RESERVED_BIT2 BIT(2) 54 #define ADT7411_CFG3_RESERVED_BIT3 BIT(3) 55 #define ADT7411_CFG3_REF_VDD BIT(4) 56 57 #define ADT7411_REG_VDD_HIGH 0x23 58 #define ADT7411_REG_VDD_LOW 0x24 59 #define ADT7411_REG_TEMP_HIGH(nr) (0x25 + 2 * (nr)) 60 #define ADT7411_REG_TEMP_LOW(nr) (0x26 + 2 * (nr)) 61 #define ADT7411_REG_IN_HIGH(nr) ((nr) > 1 \ 62 ? 0x2b + 2 * ((nr)-2) \ 63 : 0x27) 64 #define ADT7411_REG_IN_LOW(nr) ((nr) > 1 \ 65 ? 0x2c + 2 * ((nr)-2) \ 66 : 0x28) 67 68 #define ADT7411_REG_DEVICE_ID 0x4d 69 #define ADT7411_REG_MANUFACTURER_ID 0x4e 70 71 #define ADT7411_DEVICE_ID 0x2 72 #define ADT7411_MANUFACTURER_ID 0x41 73 74 static const unsigned short normal_i2c[] = { 0x48, 0x4a, 0x4b, I2C_CLIENT_END }; 75 76 static const u8 adt7411_in_alarm_reg[] = { 77 ADT7411_REG_STAT_2, 78 ADT7411_REG_STAT_1, 79 ADT7411_REG_STAT_1, 80 ADT7411_REG_STAT_1, 81 ADT7411_REG_STAT_1, 82 ADT7411_REG_STAT_2, 83 ADT7411_REG_STAT_2, 84 ADT7411_REG_STAT_2, 85 ADT7411_REG_STAT_2, 86 }; 87 88 static const u8 adt7411_in_alarm_bits[] = { 89 ADT7411_STAT_2_VDD, 90 ADT7411_STAT_1_EXT_TEMP_HIGH_AIN1, 91 ADT7411_STAT_1_AIN2, 92 ADT7411_STAT_1_AIN3, 93 ADT7411_STAT_1_AIN4, 94 ADT7411_STAT_2_AIN5, 95 ADT7411_STAT_2_AIN6, 96 ADT7411_STAT_2_AIN7, 97 ADT7411_STAT_2_AIN8, 98 }; 99 100 struct adt7411_data { 101 unsigned long next_update; 102 long vref_cached; 103 struct i2c_client *client; 104 bool use_ext_temp; 105 }; 106 107 /* 108 * When reading a register containing (up to 4) lsb, all associated 109 * msb-registers get locked by the hardware. After _one_ of those msb is read, 110 * _all_ are unlocked. 111 */ 112 static int adt7411_read_10_bit(struct i2c_client *client, u8 lsb_reg, 113 u8 msb_reg, u8 lsb_shift) 114 { 115 int val, tmp; 116 117 val = i2c_smbus_read_byte_data(client, lsb_reg); 118 if (val < 0) 119 return val; 120 121 tmp = (val >> lsb_shift) & 3; 122 val = i2c_smbus_read_byte_data(client, msb_reg); 123 if (val < 0) 124 return val; 125 126 val = (val << 2) | tmp; 127 return val; 128 } 129 130 static int adt7411_modify_bit(struct i2c_client *client, u8 reg, u8 bit, 131 bool flag) 132 { 133 int ret, val; 134 135 ret = i2c_smbus_read_byte_data(client, reg); 136 if (ret < 0) 137 return ret; 138 139 if (flag) 140 val = ret | bit; 141 else 142 val = ret & ~bit; 143 144 return i2c_smbus_write_byte_data(client, reg, val); 145 } 146 147 static ssize_t adt7411_show_bit(struct device *dev, 148 struct device_attribute *attr, char *buf) 149 { 150 struct sensor_device_attribute_2 *attr2 = to_sensor_dev_attr_2(attr); 151 struct adt7411_data *data = dev_get_drvdata(dev); 152 struct i2c_client *client = data->client; 153 int ret = i2c_smbus_read_byte_data(client, attr2->index); 154 155 return ret < 0 ? ret : sprintf(buf, "%u\n", !!(ret & attr2->nr)); 156 } 157 158 static ssize_t adt7411_set_bit(struct device *dev, 159 struct device_attribute *attr, const char *buf, 160 size_t count) 161 { 162 struct sensor_device_attribute_2 *s_attr2 = to_sensor_dev_attr_2(attr); 163 struct adt7411_data *data = dev_get_drvdata(dev); 164 struct i2c_client *client = data->client; 165 int ret; 166 unsigned long flag; 167 168 ret = kstrtoul(buf, 0, &flag); 169 if (ret || flag > 1) 170 return -EINVAL; 171 172 hwmon_lock(dev); 173 ret = adt7411_modify_bit(client, s_attr2->index, s_attr2->nr, flag); 174 /* force update */ 175 data->next_update = jiffies; 176 hwmon_unlock(dev); 177 178 return ret < 0 ? ret : count; 179 } 180 181 #define ADT7411_BIT_ATTR(__name, __reg, __bit) \ 182 SENSOR_DEVICE_ATTR_2(__name, S_IRUGO | S_IWUSR, adt7411_show_bit, \ 183 adt7411_set_bit, __bit, __reg) 184 185 static ADT7411_BIT_ATTR(no_average, ADT7411_REG_CFG2, ADT7411_CFG2_DISABLE_AVG); 186 static ADT7411_BIT_ATTR(fast_sampling, ADT7411_REG_CFG3, ADT7411_CFG3_ADC_CLK_225); 187 static ADT7411_BIT_ATTR(adc_ref_vdd, ADT7411_REG_CFG3, ADT7411_CFG3_REF_VDD); 188 189 static struct attribute *adt7411_attrs[] = { 190 &sensor_dev_attr_no_average.dev_attr.attr, 191 &sensor_dev_attr_fast_sampling.dev_attr.attr, 192 &sensor_dev_attr_adc_ref_vdd.dev_attr.attr, 193 NULL 194 }; 195 ATTRIBUTE_GROUPS(adt7411); 196 197 static int adt7411_read_in_alarm(struct device *dev, int channel, long *val) 198 { 199 struct adt7411_data *data = dev_get_drvdata(dev); 200 struct i2c_client *client = data->client; 201 int ret; 202 203 ret = i2c_smbus_read_byte_data(client, adt7411_in_alarm_reg[channel]); 204 if (ret < 0) 205 return ret; 206 *val = !!(ret & adt7411_in_alarm_bits[channel]); 207 return 0; 208 } 209 210 static int adt7411_read_in_vdd(struct device *dev, u32 attr, long *val) 211 { 212 struct adt7411_data *data = dev_get_drvdata(dev); 213 struct i2c_client *client = data->client; 214 int ret; 215 216 switch (attr) { 217 case hwmon_in_input: 218 ret = adt7411_read_10_bit(client, ADT7411_REG_INT_TEMP_VDD_LSB, 219 ADT7411_REG_VDD_MSB, 2); 220 if (ret < 0) 221 return ret; 222 *val = ret * 7000 / 1024; 223 return 0; 224 case hwmon_in_min: 225 ret = i2c_smbus_read_byte_data(client, ADT7411_REG_VDD_LOW); 226 if (ret < 0) 227 return ret; 228 *val = ret * 7000 / 256; 229 return 0; 230 case hwmon_in_max: 231 ret = i2c_smbus_read_byte_data(client, ADT7411_REG_VDD_HIGH); 232 if (ret < 0) 233 return ret; 234 *val = ret * 7000 / 256; 235 return 0; 236 case hwmon_in_alarm: 237 return adt7411_read_in_alarm(dev, 0, val); 238 default: 239 return -EOPNOTSUPP; 240 } 241 } 242 243 static int adt7411_update_vref(struct device *dev) 244 { 245 struct adt7411_data *data = dev_get_drvdata(dev); 246 struct i2c_client *client = data->client; 247 int val; 248 249 if (time_after_eq(jiffies, data->next_update)) { 250 val = i2c_smbus_read_byte_data(client, ADT7411_REG_CFG3); 251 if (val < 0) 252 return val; 253 254 if (val & ADT7411_CFG3_REF_VDD) { 255 val = adt7411_read_in_vdd(dev, hwmon_in_input, 256 &data->vref_cached); 257 if (val < 0) 258 return val; 259 } else { 260 data->vref_cached = 2250; 261 } 262 263 data->next_update = jiffies + HZ; 264 } 265 266 return 0; 267 } 268 269 static int adt7411_read_in_chan(struct device *dev, u32 attr, int channel, 270 long *val) 271 { 272 struct adt7411_data *data = dev_get_drvdata(dev); 273 struct i2c_client *client = data->client; 274 275 int ret; 276 int reg, lsb_reg, lsb_shift; 277 int nr = channel - 1; 278 279 ret = adt7411_update_vref(dev); 280 if (ret < 0) 281 return ret; 282 283 switch (attr) { 284 case hwmon_in_input: 285 lsb_reg = ADT7411_REG_EXT_TEMP_AIN14_LSB + (nr >> 2); 286 lsb_shift = 2 * (nr & 0x03); 287 ret = adt7411_read_10_bit(client, lsb_reg, 288 ADT7411_REG_EXT_TEMP_AIN1_MSB + nr, 289 lsb_shift); 290 if (ret < 0) 291 return ret; 292 *val = ret * data->vref_cached / 1024; 293 ret = 0; 294 break; 295 case hwmon_in_min: 296 case hwmon_in_max: 297 reg = (attr == hwmon_in_min) 298 ? ADT7411_REG_IN_LOW(channel) 299 : ADT7411_REG_IN_HIGH(channel); 300 ret = i2c_smbus_read_byte_data(client, reg); 301 if (ret < 0) 302 return ret; 303 *val = ret * data->vref_cached / 256; 304 ret = 0; 305 break; 306 case hwmon_in_alarm: 307 ret = adt7411_read_in_alarm(dev, channel, val); 308 break; 309 default: 310 ret = -EOPNOTSUPP; 311 break; 312 } 313 return ret; 314 } 315 316 static int adt7411_read_in(struct device *dev, u32 attr, int channel, 317 long *val) 318 { 319 if (channel == 0) 320 return adt7411_read_in_vdd(dev, attr, val); 321 else 322 return adt7411_read_in_chan(dev, attr, channel, val); 323 } 324 325 326 static int adt7411_read_temp_alarm(struct device *dev, u32 attr, int channel, 327 long *val) 328 { 329 struct adt7411_data *data = dev_get_drvdata(dev); 330 struct i2c_client *client = data->client; 331 int ret, bit; 332 333 ret = i2c_smbus_read_byte_data(client, ADT7411_REG_STAT_1); 334 if (ret < 0) 335 return ret; 336 337 switch (attr) { 338 case hwmon_temp_min_alarm: 339 bit = channel ? ADT7411_STAT_1_EXT_TEMP_LOW 340 : ADT7411_STAT_1_INT_TEMP_LOW; 341 break; 342 case hwmon_temp_max_alarm: 343 bit = channel ? ADT7411_STAT_1_EXT_TEMP_HIGH_AIN1 344 : ADT7411_STAT_1_INT_TEMP_HIGH; 345 break; 346 case hwmon_temp_fault: 347 bit = ADT7411_STAT_1_EXT_TEMP_FAULT; 348 break; 349 default: 350 return -EOPNOTSUPP; 351 } 352 353 *val = !!(ret & bit); 354 return 0; 355 } 356 357 static int adt7411_read_temp(struct device *dev, u32 attr, int channel, 358 long *val) 359 { 360 struct adt7411_data *data = dev_get_drvdata(dev); 361 struct i2c_client *client = data->client; 362 int ret, reg, regl, regh; 363 364 switch (attr) { 365 case hwmon_temp_input: 366 regl = channel ? ADT7411_REG_EXT_TEMP_AIN14_LSB : 367 ADT7411_REG_INT_TEMP_VDD_LSB; 368 regh = channel ? ADT7411_REG_EXT_TEMP_AIN1_MSB : 369 ADT7411_REG_INT_TEMP_MSB; 370 ret = adt7411_read_10_bit(client, regl, regh, 0); 371 if (ret < 0) 372 return ret; 373 ret = ret & 0x200 ? ret - 0x400 : ret; /* 10 bit signed */ 374 *val = ret * 250; 375 return 0; 376 case hwmon_temp_min: 377 case hwmon_temp_max: 378 reg = (attr == hwmon_temp_min) 379 ? ADT7411_REG_TEMP_LOW(channel) 380 : ADT7411_REG_TEMP_HIGH(channel); 381 ret = i2c_smbus_read_byte_data(client, reg); 382 if (ret < 0) 383 return ret; 384 ret = ret & 0x80 ? ret - 0x100 : ret; /* 8 bit signed */ 385 *val = ret * 1000; 386 return 0; 387 case hwmon_temp_min_alarm: 388 case hwmon_temp_max_alarm: 389 case hwmon_temp_fault: 390 return adt7411_read_temp_alarm(dev, attr, channel, val); 391 default: 392 return -EOPNOTSUPP; 393 } 394 } 395 396 static int adt7411_read(struct device *dev, enum hwmon_sensor_types type, 397 u32 attr, int channel, long *val) 398 { 399 switch (type) { 400 case hwmon_in: 401 return adt7411_read_in(dev, attr, channel, val); 402 case hwmon_temp: 403 return adt7411_read_temp(dev, attr, channel, val); 404 default: 405 return -EOPNOTSUPP; 406 } 407 } 408 409 static int adt7411_write_in_vdd(struct device *dev, u32 attr, long val) 410 { 411 struct adt7411_data *data = dev_get_drvdata(dev); 412 struct i2c_client *client = data->client; 413 int reg; 414 415 val = clamp_val(val, 0, 255 * 7000 / 256); 416 val = DIV_ROUND_CLOSEST(val * 256, 7000); 417 418 switch (attr) { 419 case hwmon_in_min: 420 reg = ADT7411_REG_VDD_LOW; 421 break; 422 case hwmon_in_max: 423 reg = ADT7411_REG_VDD_HIGH; 424 break; 425 default: 426 return -EOPNOTSUPP; 427 } 428 429 return i2c_smbus_write_byte_data(client, reg, val); 430 } 431 432 static int adt7411_write_in_chan(struct device *dev, u32 attr, int channel, 433 long val) 434 { 435 struct adt7411_data *data = dev_get_drvdata(dev); 436 struct i2c_client *client = data->client; 437 int ret, reg; 438 439 ret = adt7411_update_vref(dev); 440 if (ret < 0) 441 return ret; 442 val = clamp_val(val, 0, 255 * data->vref_cached / 256); 443 val = DIV_ROUND_CLOSEST(val * 256, data->vref_cached); 444 445 switch (attr) { 446 case hwmon_in_min: 447 reg = ADT7411_REG_IN_LOW(channel); 448 break; 449 case hwmon_in_max: 450 reg = ADT7411_REG_IN_HIGH(channel); 451 break; 452 default: 453 return -EOPNOTSUPP; 454 } 455 456 ret = i2c_smbus_write_byte_data(client, reg, val); 457 return ret; 458 } 459 460 static int adt7411_write_in(struct device *dev, u32 attr, int channel, 461 long val) 462 { 463 if (channel == 0) 464 return adt7411_write_in_vdd(dev, attr, val); 465 else 466 return adt7411_write_in_chan(dev, attr, channel, val); 467 } 468 469 static int adt7411_write_temp(struct device *dev, u32 attr, int channel, 470 long val) 471 { 472 struct adt7411_data *data = dev_get_drvdata(dev); 473 struct i2c_client *client = data->client; 474 int reg; 475 476 val = clamp_val(val, -128000, 127000); 477 val = DIV_ROUND_CLOSEST(val, 1000); 478 479 switch (attr) { 480 case hwmon_temp_min: 481 reg = ADT7411_REG_TEMP_LOW(channel); 482 break; 483 case hwmon_temp_max: 484 reg = ADT7411_REG_TEMP_HIGH(channel); 485 break; 486 default: 487 return -EOPNOTSUPP; 488 } 489 490 return i2c_smbus_write_byte_data(client, reg, val); 491 } 492 493 static int adt7411_write(struct device *dev, enum hwmon_sensor_types type, 494 u32 attr, int channel, long val) 495 { 496 switch (type) { 497 case hwmon_in: 498 return adt7411_write_in(dev, attr, channel, val); 499 case hwmon_temp: 500 return adt7411_write_temp(dev, attr, channel, val); 501 default: 502 return -EOPNOTSUPP; 503 } 504 } 505 506 static umode_t adt7411_is_visible(const void *_data, 507 enum hwmon_sensor_types type, 508 u32 attr, int channel) 509 { 510 const struct adt7411_data *data = _data; 511 bool visible; 512 513 switch (type) { 514 case hwmon_in: 515 visible = channel == 0 || channel >= 3 || !data->use_ext_temp; 516 switch (attr) { 517 case hwmon_in_input: 518 case hwmon_in_alarm: 519 return visible ? S_IRUGO : 0; 520 case hwmon_in_min: 521 case hwmon_in_max: 522 return visible ? S_IRUGO | S_IWUSR : 0; 523 } 524 break; 525 case hwmon_temp: 526 visible = channel == 0 || data->use_ext_temp; 527 switch (attr) { 528 case hwmon_temp_input: 529 case hwmon_temp_min_alarm: 530 case hwmon_temp_max_alarm: 531 case hwmon_temp_fault: 532 return visible ? S_IRUGO : 0; 533 case hwmon_temp_min: 534 case hwmon_temp_max: 535 return visible ? S_IRUGO | S_IWUSR : 0; 536 } 537 break; 538 default: 539 break; 540 } 541 return 0; 542 } 543 544 static int adt7411_detect(struct i2c_client *client, 545 struct i2c_board_info *info) 546 { 547 int val; 548 549 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA)) 550 return -ENODEV; 551 552 val = i2c_smbus_read_byte_data(client, ADT7411_REG_MANUFACTURER_ID); 553 if (val < 0 || val != ADT7411_MANUFACTURER_ID) { 554 dev_dbg(&client->dev, 555 "Wrong manufacturer ID. Got %d, expected %d\n", 556 val, ADT7411_MANUFACTURER_ID); 557 return -ENODEV; 558 } 559 560 val = i2c_smbus_read_byte_data(client, ADT7411_REG_DEVICE_ID); 561 if (val < 0 || val != ADT7411_DEVICE_ID) { 562 dev_dbg(&client->dev, 563 "Wrong device ID. Got %d, expected %d\n", 564 val, ADT7411_DEVICE_ID); 565 return -ENODEV; 566 } 567 568 strscpy(info->type, "adt7411", I2C_NAME_SIZE); 569 570 return 0; 571 } 572 573 static int adt7411_init_device(struct adt7411_data *data) 574 { 575 int ret; 576 u8 val; 577 578 ret = i2c_smbus_read_byte_data(data->client, ADT7411_REG_CFG3); 579 if (ret < 0) 580 return ret; 581 582 /* 583 * We must only write zero to bit 1 and bit 2 and only one to bit 3 584 * according to the datasheet. 585 */ 586 val = ret; 587 val &= ~(ADT7411_CFG3_RESERVED_BIT1 | ADT7411_CFG3_RESERVED_BIT2); 588 val |= ADT7411_CFG3_RESERVED_BIT3; 589 590 ret = i2c_smbus_write_byte_data(data->client, ADT7411_REG_CFG3, val); 591 if (ret < 0) 592 return ret; 593 594 ret = i2c_smbus_read_byte_data(data->client, ADT7411_REG_CFG1); 595 if (ret < 0) 596 return ret; 597 598 data->use_ext_temp = ret & ADT7411_CFG1_EXT_TDM; 599 600 /* 601 * We must only write zero to bit 1 and only one to bit 3 according to 602 * the datasheet. 603 */ 604 val = ret; 605 val &= ~ADT7411_CFG1_RESERVED_BIT1; 606 val |= ADT7411_CFG1_RESERVED_BIT3; 607 608 /* enable monitoring */ 609 val |= ADT7411_CFG1_START_MONITOR; 610 611 return i2c_smbus_write_byte_data(data->client, ADT7411_REG_CFG1, val); 612 } 613 614 static const struct hwmon_channel_info * const adt7411_info[] = { 615 HWMON_CHANNEL_INFO(in, 616 HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX | HWMON_I_ALARM, 617 HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX | HWMON_I_ALARM, 618 HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX | HWMON_I_ALARM, 619 HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX | HWMON_I_ALARM, 620 HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX | HWMON_I_ALARM, 621 HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX | HWMON_I_ALARM, 622 HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX | HWMON_I_ALARM, 623 HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX | HWMON_I_ALARM, 624 HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX | HWMON_I_ALARM), 625 HWMON_CHANNEL_INFO(temp, 626 HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MIN_ALARM | 627 HWMON_T_MAX | HWMON_T_MAX_ALARM, 628 HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MIN_ALARM | 629 HWMON_T_MAX | HWMON_T_MAX_ALARM | HWMON_T_FAULT), 630 NULL 631 }; 632 633 static const struct hwmon_ops adt7411_hwmon_ops = { 634 .is_visible = adt7411_is_visible, 635 .read = adt7411_read, 636 .write = adt7411_write, 637 }; 638 639 static const struct hwmon_chip_info adt7411_chip_info = { 640 .ops = &adt7411_hwmon_ops, 641 .info = adt7411_info, 642 }; 643 644 static int adt7411_probe(struct i2c_client *client) 645 { 646 struct device *dev = &client->dev; 647 struct adt7411_data *data; 648 struct device *hwmon_dev; 649 int ret; 650 651 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); 652 if (!data) 653 return -ENOMEM; 654 655 i2c_set_clientdata(client, data); 656 data->client = client; 657 658 ret = adt7411_init_device(data); 659 if (ret < 0) 660 return ret; 661 662 /* force update on first occasion */ 663 data->next_update = jiffies; 664 665 hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name, 666 data, 667 &adt7411_chip_info, 668 adt7411_groups); 669 return PTR_ERR_OR_ZERO(hwmon_dev); 670 } 671 672 static const struct i2c_device_id adt7411_id[] = { 673 { "adt7411" }, 674 { } 675 }; 676 MODULE_DEVICE_TABLE(i2c, adt7411_id); 677 678 static struct i2c_driver adt7411_driver = { 679 .driver = { 680 .name = "adt7411", 681 }, 682 .probe = adt7411_probe, 683 .id_table = adt7411_id, 684 .detect = adt7411_detect, 685 .address_list = normal_i2c, 686 .class = I2C_CLASS_HWMON, 687 }; 688 689 module_i2c_driver(adt7411_driver); 690 691 MODULE_AUTHOR("Sascha Hauer, Wolfram Sang <kernel@pengutronix.de>"); 692 MODULE_DESCRIPTION("ADT7411 driver"); 693 MODULE_LICENSE("GPL v2"); 694