adc128d818.c (d8a66f3621c2886ad328d9df53349fc10251f583) | adc128d818.c (cffb8d74bd4e9dd0653c7093c4a5164a72c52b1f) |
---|---|
1// SPDX-License-Identifier: GPL-2.0-or-later 2/* 3 * Driver for TI ADC128D818 System Monitor with Temperature Sensor 4 * 5 * Copyright (c) 2014 Guenter Roeck 6 * 7 * Derived from lm80.c 8 * Copyright (C) 1998, 1999 Frodo Looijaard <frodol@dds.nl> --- 44 unchanged lines hidden (view full) --- 53/* No. of voltage entries in adc128_attrs */ 54#define ADC128_ATTR_NUM_VOLT (8 * 4) 55 56/* Voltage inputs visible per operation mode */ 57static const u8 num_inputs[] = { 7, 8, 4, 6 }; 58 59struct adc128_data { 60 struct i2c_client *client; | 1// SPDX-License-Identifier: GPL-2.0-or-later 2/* 3 * Driver for TI ADC128D818 System Monitor with Temperature Sensor 4 * 5 * Copyright (c) 2014 Guenter Roeck 6 * 7 * Derived from lm80.c 8 * Copyright (C) 1998, 1999 Frodo Looijaard <frodol@dds.nl> --- 44 unchanged lines hidden (view full) --- 53/* No. of voltage entries in adc128_attrs */ 54#define ADC128_ATTR_NUM_VOLT (8 * 4) 55 56/* Voltage inputs visible per operation mode */ 57static const u8 num_inputs[] = { 7, 8, 4, 6 }; 58 59struct adc128_data { 60 struct i2c_client *client; |
61 struct regulator *regulator; | |
62 int vref; /* Reference voltage in mV */ 63 struct mutex update_lock; 64 u8 mode; /* Operation mode */ 65 bool valid; /* true if following fields are valid */ 66 unsigned long last_updated; /* In jiffies */ 67 68 u16 in[3][8]; /* Register value, normalized to 12 bit 69 * 0: input voltage --- 314 unchanged lines hidden (view full) --- 384 if (i2c_smbus_read_byte_data(client, ADC128_REG_BUSY_STATUS) & 0xfc) 385 return -ENODEV; 386 387 strscpy(info->type, "adc128d818", I2C_NAME_SIZE); 388 389 return 0; 390} 391 | 61 int vref; /* Reference voltage in mV */ 62 struct mutex update_lock; 63 u8 mode; /* Operation mode */ 64 bool valid; /* true if following fields are valid */ 65 unsigned long last_updated; /* In jiffies */ 66 67 u16 in[3][8]; /* Register value, normalized to 12 bit 68 * 0: input voltage --- 314 unchanged lines hidden (view full) --- 383 if (i2c_smbus_read_byte_data(client, ADC128_REG_BUSY_STATUS) & 0xfc) 384 return -ENODEV; 385 386 strscpy(info->type, "adc128d818", I2C_NAME_SIZE); 387 388 return 0; 389} 390 |
392static int adc128_init_client(struct adc128_data *data) | 391static int adc128_init_client(struct adc128_data *data, bool external_vref) |
393{ 394 struct i2c_client *client = data->client; 395 int err; 396 u8 regval = 0x0; 397 398 /* 399 * Reset chip to defaults. 400 * This makes most other initializations unnecessary. 401 */ 402 err = i2c_smbus_write_byte_data(client, ADC128_REG_CONFIG, 0x80); 403 if (err) 404 return err; 405 406 /* Set operation mode, if non-default */ 407 if (data->mode != 0) 408 regval |= data->mode << 1; 409 410 /* If external vref is selected, configure the chip to use it */ | 392{ 393 struct i2c_client *client = data->client; 394 int err; 395 u8 regval = 0x0; 396 397 /* 398 * Reset chip to defaults. 399 * This makes most other initializations unnecessary. 400 */ 401 err = i2c_smbus_write_byte_data(client, ADC128_REG_CONFIG, 0x80); 402 if (err) 403 return err; 404 405 /* Set operation mode, if non-default */ 406 if (data->mode != 0) 407 regval |= data->mode << 1; 408 409 /* If external vref is selected, configure the chip to use it */ |
411 if (data->regulator) | 410 if (external_vref) |
412 regval |= 0x01; 413 414 /* Write advanced configuration register */ 415 if (regval != 0x0) { 416 err = i2c_smbus_write_byte_data(client, ADC128_REG_CONFIG_ADV, 417 regval); 418 if (err) 419 return err; --- 5 unchanged lines hidden (view full) --- 425 return err; 426 427 return 0; 428} 429 430static int adc128_probe(struct i2c_client *client) 431{ 432 struct device *dev = &client->dev; | 411 regval |= 0x01; 412 413 /* Write advanced configuration register */ 414 if (regval != 0x0) { 415 err = i2c_smbus_write_byte_data(client, ADC128_REG_CONFIG_ADV, 416 regval); 417 if (err) 418 return err; --- 5 unchanged lines hidden (view full) --- 424 return err; 425 426 return 0; 427} 428 429static int adc128_probe(struct i2c_client *client) 430{ 431 struct device *dev = &client->dev; |
433 struct regulator *regulator; | |
434 struct device *hwmon_dev; 435 struct adc128_data *data; | 432 struct device *hwmon_dev; 433 struct adc128_data *data; |
434 bool external_vref; |
|
436 int err, vref; 437 438 data = devm_kzalloc(dev, sizeof(struct adc128_data), GFP_KERNEL); 439 if (!data) 440 return -ENOMEM; 441 442 /* vref is optional. If specified, is used as chip reference voltage */ | 435 int err, vref; 436 437 data = devm_kzalloc(dev, sizeof(struct adc128_data), GFP_KERNEL); 438 if (!data) 439 return -ENOMEM; 440 441 /* vref is optional. If specified, is used as chip reference voltage */ |
443 regulator = devm_regulator_get_optional(dev, "vref"); 444 if (!IS_ERR(regulator)) { 445 data->regulator = regulator; 446 err = regulator_enable(regulator); 447 if (err < 0) 448 return err; 449 vref = regulator_get_voltage(regulator); 450 if (vref < 0) { 451 err = vref; 452 goto error; 453 } 454 data->vref = DIV_ROUND_CLOSEST(vref, 1000); 455 } else { | 442 vref = devm_regulator_get_enable_read_voltage(dev, "vref"); 443 if (vref == -ENODEV) { 444 external_vref = false; |
456 data->vref = 2560; /* 2.56V, in mV */ | 445 data->vref = 2560; /* 2.56V, in mV */ |
446 } else if (vref < 0) { 447 return vref; 448 } else { 449 external_vref = true; 450 data->vref = DIV_ROUND_CLOSEST(vref, 1000); |
|
457 } 458 459 /* Operation mode is optional. If unspecified, keep current mode */ 460 if (of_property_read_u8(dev->of_node, "ti,mode", &data->mode) == 0) { 461 if (data->mode > 3) { 462 dev_err(dev, "invalid operation mode %d\n", 463 data->mode); | 451 } 452 453 /* Operation mode is optional. If unspecified, keep current mode */ 454 if (of_property_read_u8(dev->of_node, "ti,mode", &data->mode) == 0) { 455 if (data->mode > 3) { 456 dev_err(dev, "invalid operation mode %d\n", 457 data->mode); |
464 err = -EINVAL; 465 goto error; | 458 return -EINVAL; |
466 } 467 } else { 468 err = i2c_smbus_read_byte_data(client, ADC128_REG_CONFIG_ADV); 469 if (err < 0) | 459 } 460 } else { 461 err = i2c_smbus_read_byte_data(client, ADC128_REG_CONFIG_ADV); 462 if (err < 0) |
470 goto error; | 463 return err; |
471 data->mode = (err >> 1) & ADC128_REG_MASK; 472 } 473 474 data->client = client; 475 i2c_set_clientdata(client, data); 476 mutex_init(&data->update_lock); 477 478 /* Initialize the chip */ | 464 data->mode = (err >> 1) & ADC128_REG_MASK; 465 } 466 467 data->client = client; 468 i2c_set_clientdata(client, data); 469 mutex_init(&data->update_lock); 470 471 /* Initialize the chip */ |
479 err = adc128_init_client(data); | 472 err = adc128_init_client(data, external_vref); |
480 if (err < 0) | 473 if (err < 0) |
481 goto error; | 474 return err; |
482 483 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, 484 data, adc128_groups); | 475 476 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, 477 data, adc128_groups); |
485 if (IS_ERR(hwmon_dev)) { 486 err = PTR_ERR(hwmon_dev); 487 goto error; 488 } | 478 if (IS_ERR(hwmon_dev)) 479 return PTR_ERR(hwmon_dev); |
489 490 return 0; | 480 481 return 0; |
491 492error: 493 if (data->regulator) 494 regulator_disable(data->regulator); 495 return err; | |
496} 497 | 482} 483 |
498static void adc128_remove(struct i2c_client *client) 499{ 500 struct adc128_data *data = i2c_get_clientdata(client); 501 502 if (data->regulator) 503 regulator_disable(data->regulator); 504} 505 | |
506static const struct i2c_device_id adc128_id[] = { | 484static const struct i2c_device_id adc128_id[] = { |
507 { "adc128d818" }, | 485 { "adc128d818", 0 }, |
508 { } 509}; 510MODULE_DEVICE_TABLE(i2c, adc128_id); 511 512static const struct of_device_id __maybe_unused adc128_of_match[] = { 513 { .compatible = "ti,adc128d818" }, 514 { }, 515}; 516MODULE_DEVICE_TABLE(of, adc128_of_match); 517 518static struct i2c_driver adc128_driver = { 519 .class = I2C_CLASS_HWMON, 520 .driver = { 521 .name = "adc128d818", 522 .of_match_table = of_match_ptr(adc128_of_match), 523 }, 524 .probe = adc128_probe, | 486 { } 487}; 488MODULE_DEVICE_TABLE(i2c, adc128_id); 489 490static const struct of_device_id __maybe_unused adc128_of_match[] = { 491 { .compatible = "ti,adc128d818" }, 492 { }, 493}; 494MODULE_DEVICE_TABLE(of, adc128_of_match); 495 496static struct i2c_driver adc128_driver = { 497 .class = I2C_CLASS_HWMON, 498 .driver = { 499 .name = "adc128d818", 500 .of_match_table = of_match_ptr(adc128_of_match), 501 }, 502 .probe = adc128_probe, |
525 .remove = adc128_remove, | |
526 .id_table = adc128_id, 527 .detect = adc128_detect, 528 .address_list = normal_i2c, 529}; 530 531module_i2c_driver(adc128_driver); 532 533MODULE_AUTHOR("Guenter Roeck"); 534MODULE_DESCRIPTION("Driver for ADC128D818"); 535MODULE_LICENSE("GPL"); | 503 .id_table = adc128_id, 504 .detect = adc128_detect, 505 .address_list = normal_i2c, 506}; 507 508module_i2c_driver(adc128_driver); 509 510MODULE_AUTHOR("Guenter Roeck"); 511MODULE_DESCRIPTION("Driver for ADC128D818"); 512MODULE_LICENSE("GPL"); |