Skip to content

Commit 98ac8af

Browse files
committed
hwmon: (pmbus/adm1275) Prepare for protected write to PMON_CONFIG
According to ADI, changing PMON_CONFIG while ADC is running can have unexpected results. ADI recommends halting the ADC with PMON_CONTROL before setting PMON_CONFIG and then resume after. To prepare for this change, rename adm1275_read_pmon_config() and adm1275_write_pmon_config() to adm1275_read_samples() and adm1275_write_samples() to more accurately reflect the functionality of the code. Introduce new function adm1275_write_pmon_config() and use it for all code writing into the PMON_CONFIG register. Signed-off-by: Guenter Roeck <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Guenter Roeck <[email protected]>
1 parent 37f665f commit 98ac8af

File tree

1 file changed

+31
-25
lines changed

1 file changed

+31
-25
lines changed

drivers/hwmon/pmbus/adm1275.c

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,8 @@ static const struct coefficients adm1293_coefficients[] = {
173173
[18] = { 7658, 0, -3 }, /* power, 21V, irange200 */
174174
};
175175

176-
static int adm1275_read_pmon_config(const struct adm1275_data *data,
177-
struct i2c_client *client, bool is_power)
176+
static int adm1275_read_samples(const struct adm1275_data *data,
177+
struct i2c_client *client, bool is_power)
178178
{
179179
int shift, ret;
180180
u16 mask;
@@ -200,8 +200,23 @@ static int adm1275_read_pmon_config(const struct adm1275_data *data,
200200
}
201201

202202
static int adm1275_write_pmon_config(const struct adm1275_data *data,
203-
struct i2c_client *client,
204-
bool is_power, u16 word)
203+
struct i2c_client *client, u16 word)
204+
{
205+
int ret;
206+
207+
if (data->have_power_sampling)
208+
ret = i2c_smbus_write_word_data(client, ADM1275_PMON_CONFIG,
209+
word);
210+
else
211+
ret = i2c_smbus_write_byte_data(client, ADM1275_PMON_CONFIG,
212+
word);
213+
214+
return ret;
215+
}
216+
217+
static int adm1275_write_samples(const struct adm1275_data *data,
218+
struct i2c_client *client,
219+
bool is_power, u16 word)
205220
{
206221
int shift, ret;
207222
u16 mask;
@@ -219,14 +234,8 @@ static int adm1275_write_pmon_config(const struct adm1275_data *data,
219234
return ret;
220235

221236
word = (ret & ~mask) | ((word << shift) & mask);
222-
if (data->have_power_sampling)
223-
ret = i2c_smbus_write_word_data(client, ADM1275_PMON_CONFIG,
224-
word);
225-
else
226-
ret = i2c_smbus_write_byte_data(client, ADM1275_PMON_CONFIG,
227-
word);
228237

229-
return ret;
238+
return adm1275_write_pmon_config(data, client, word);
230239
}
231240

232241
static int adm1275_read_word_data(struct i2c_client *client, int page,
@@ -321,14 +330,14 @@ static int adm1275_read_word_data(struct i2c_client *client, int page,
321330
case PMBUS_VIRT_POWER_SAMPLES:
322331
if (!data->have_power_sampling)
323332
return -ENXIO;
324-
ret = adm1275_read_pmon_config(data, client, true);
333+
ret = adm1275_read_samples(data, client, true);
325334
if (ret < 0)
326335
break;
327336
ret = BIT(ret);
328337
break;
329338
case PMBUS_VIRT_IN_SAMPLES:
330339
case PMBUS_VIRT_CURR_SAMPLES:
331-
ret = adm1275_read_pmon_config(data, client, false);
340+
ret = adm1275_read_samples(data, client, false);
332341
if (ret < 0)
333342
break;
334343
ret = BIT(ret);
@@ -381,14 +390,12 @@ static int adm1275_write_word_data(struct i2c_client *client, int page, int reg,
381390
if (!data->have_power_sampling)
382391
return -ENXIO;
383392
word = clamp_val(word, 1, ADM1275_SAMPLES_AVG_MAX);
384-
ret = adm1275_write_pmon_config(data, client, true,
385-
ilog2(word));
393+
ret = adm1275_write_samples(data, client, true, ilog2(word));
386394
break;
387395
case PMBUS_VIRT_IN_SAMPLES:
388396
case PMBUS_VIRT_CURR_SAMPLES:
389397
word = clamp_val(word, 1, ADM1275_SAMPLES_AVG_MAX);
390-
ret = adm1275_write_pmon_config(data, client, false,
391-
ilog2(word));
398+
ret = adm1275_write_samples(data, client, false, ilog2(word));
392399
break;
393400
default:
394401
ret = -ENODATA;
@@ -466,13 +473,14 @@ static const struct i2c_device_id adm1275_id[] = {
466473
MODULE_DEVICE_TABLE(i2c, adm1275_id);
467474

468475
/* Enable VOUT & TEMP1 if not enabled (disabled by default) */
469-
static int adm1275_enable_vout_temp(struct i2c_client *client, int config)
476+
static int adm1275_enable_vout_temp(struct adm1275_data *data,
477+
struct i2c_client *client, int config)
470478
{
471479
int ret;
472480

473481
if ((config & ADM1278_PMON_DEFCONFIG) != ADM1278_PMON_DEFCONFIG) {
474482
config |= ADM1278_PMON_DEFCONFIG;
475-
ret = i2c_smbus_write_word_data(client, ADM1275_PMON_CONFIG, config);
483+
ret = adm1275_write_pmon_config(data, client, config);
476484
if (ret < 0) {
477485
dev_err(&client->dev, "Failed to enable VOUT/TEMP1 monitoring\n");
478486
return ret;
@@ -634,7 +642,7 @@ static int adm1275_probe(struct i2c_client *client)
634642
PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT |
635643
PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP;
636644

637-
ret = adm1275_enable_vout_temp(client, config);
645+
ret = adm1275_enable_vout_temp(data, client, config);
638646
if (ret)
639647
return ret;
640648

@@ -694,7 +702,7 @@ static int adm1275_probe(struct i2c_client *client)
694702
PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT |
695703
PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP;
696704

697-
ret = adm1275_enable_vout_temp(client, config);
705+
ret = adm1275_enable_vout_temp(data, client, config);
698706
if (ret)
699707
return ret;
700708

@@ -766,8 +774,7 @@ static int adm1275_probe(struct i2c_client *client)
766774
"Invalid number of power samples");
767775
return -EINVAL;
768776
}
769-
ret = adm1275_write_pmon_config(data, client, true,
770-
ilog2(avg));
777+
ret = adm1275_write_samples(data, client, true, ilog2(avg));
771778
if (ret < 0) {
772779
dev_err(&client->dev,
773780
"Setting power sample averaging failed with error %d",
@@ -784,8 +791,7 @@ static int adm1275_probe(struct i2c_client *client)
784791
"Invalid number of voltage/current samples");
785792
return -EINVAL;
786793
}
787-
ret = adm1275_write_pmon_config(data, client, false,
788-
ilog2(avg));
794+
ret = adm1275_write_samples(data, client, false, ilog2(avg));
789795
if (ret < 0) {
790796
dev_err(&client->dev,
791797
"Setting voltage and current sample averaging failed with error %d",

0 commit comments

Comments
 (0)