Skip to content

Commit cffb8d7

Browse files
dlechbroonie
authored andcommitted
hwmon: (adc128d818) Use devm_regulator_get_enable_read_voltage()
We can reduce boilerplate code and eliminate the driver remove() function by using devm_regulator_get_enable_read_voltage(). A new external_vref flag is added since we no longer have the handle to the regulator to check if it is present. Reviewed-by: Jonathan Cameron <[email protected]> Signed-off-by: David Lechner <[email protected]> Acked-by: Guenter Roeck <[email protected]> Link: https://lore.kernel.org/r/20240429-regulator-get-enable-get-votlage-v2-2-b1f11ab766c1@baylibre.com Signed-off-by: Mark Brown <[email protected]>
1 parent b250c20 commit cffb8d7

File tree

1 file changed

+17
-40
lines changed

1 file changed

+17
-40
lines changed

drivers/hwmon/adc128d818.c

Lines changed: 17 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ static const u8 num_inputs[] = { 7, 8, 4, 6 };
5858

5959
struct adc128_data {
6060
struct i2c_client *client;
61-
struct regulator *regulator;
6261
int vref; /* Reference voltage in mV */
6362
struct mutex update_lock;
6463
u8 mode; /* Operation mode */
@@ -389,7 +388,7 @@ static int adc128_detect(struct i2c_client *client, struct i2c_board_info *info)
389388
return 0;
390389
}
391390

392-
static int adc128_init_client(struct adc128_data *data)
391+
static int adc128_init_client(struct adc128_data *data, bool external_vref)
393392
{
394393
struct i2c_client *client = data->client;
395394
int err;
@@ -408,7 +407,7 @@ static int adc128_init_client(struct adc128_data *data)
408407
regval |= data->mode << 1;
409408

410409
/* If external vref is selected, configure the chip to use it */
411-
if (data->regulator)
410+
if (external_vref)
412411
regval |= 0x01;
413412

414413
/* Write advanced configuration register */
@@ -430,44 +429,38 @@ static int adc128_init_client(struct adc128_data *data)
430429
static int adc128_probe(struct i2c_client *client)
431430
{
432431
struct device *dev = &client->dev;
433-
struct regulator *regulator;
434432
struct device *hwmon_dev;
435433
struct adc128_data *data;
434+
bool external_vref;
436435
int err, vref;
437436

438437
data = devm_kzalloc(dev, sizeof(struct adc128_data), GFP_KERNEL);
439438
if (!data)
440439
return -ENOMEM;
441440

442441
/* 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;
456445
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);
457451
}
458452

459453
/* Operation mode is optional. If unspecified, keep current mode */
460454
if (of_property_read_u8(dev->of_node, "ti,mode", &data->mode) == 0) {
461455
if (data->mode > 3) {
462456
dev_err(dev, "invalid operation mode %d\n",
463457
data->mode);
464-
err = -EINVAL;
465-
goto error;
458+
return -EINVAL;
466459
}
467460
} else {
468461
err = i2c_smbus_read_byte_data(client, ADC128_REG_CONFIG_ADV);
469462
if (err < 0)
470-
goto error;
463+
return err;
471464
data->mode = (err >> 1) & ADC128_REG_MASK;
472465
}
473466

@@ -476,31 +469,16 @@ static int adc128_probe(struct i2c_client *client)
476469
mutex_init(&data->update_lock);
477470

478471
/* Initialize the chip */
479-
err = adc128_init_client(data);
472+
err = adc128_init_client(data, external_vref);
480473
if (err < 0)
481-
goto error;
474+
return err;
482475

483476
hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
484477
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);
489480

490481
return 0;
491-
492-
error:
493-
if (data->regulator)
494-
regulator_disable(data->regulator);
495-
return err;
496-
}
497-
498-
static 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);
504482
}
505483

506484
static const struct i2c_device_id adc128_id[] = {
@@ -522,7 +500,6 @@ static struct i2c_driver adc128_driver = {
522500
.of_match_table = of_match_ptr(adc128_of_match),
523501
},
524502
.probe = adc128_probe,
525-
.remove = adc128_remove,
526503
.id_table = adc128_id,
527504
.detect = adc128_detect,
528505
.address_list = normal_i2c,

0 commit comments

Comments
 (0)