Skip to content

Commit 9ca6696

Browse files
DanielMatyasgroeck
authored andcommitted
hwmon: (max31827) Make code cleaner
Used enums and while loops to replace switch for selecting and getting update interval from conversion rate bits. Divided the write_alarm_val function into 2 functions. The new function is more generic: it can be used not only for alarm writes, but for any kind of writes which require the device to be in shutdown mode. Signed-off-by: Daniel Matyas <[email protected]> Link: https://lore.kernel.org/r/[email protected] [groeck: Reverted error return value change (EOPNOTSUPP -> EINVAL)] Signed-off-by: Guenter Roeck <[email protected]>
1 parent b344041 commit 9ca6696

File tree

1 file changed

+54
-69
lines changed

1 file changed

+54
-69
lines changed

drivers/hwmon/max31827.c

Lines changed: 54 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,30 @@
2727

2828
#define MAX31827_12_BIT_CNV_TIME 141
2929

30-
#define MAX31827_CNV_1_DIV_64_HZ 0x1
31-
#define MAX31827_CNV_1_DIV_32_HZ 0x2
32-
#define MAX31827_CNV_1_DIV_16_HZ 0x3
33-
#define MAX31827_CNV_1_DIV_4_HZ 0x4
34-
#define MAX31827_CNV_1_HZ 0x5
35-
#define MAX31827_CNV_4_HZ 0x6
36-
#define MAX31827_CNV_8_HZ 0x7
37-
3830
#define MAX31827_16_BIT_TO_M_DGR(x) (sign_extend32(x, 15) * 1000 / 16)
3931
#define MAX31827_M_DGR_TO_16_BIT(x) (((x) << 4) / 1000)
4032
#define MAX31827_DEVICE_ENABLE(x) ((x) ? 0xA : 0x0)
4133

34+
enum max31827_cnv {
35+
MAX31827_CNV_1_DIV_64_HZ = 1,
36+
MAX31827_CNV_1_DIV_32_HZ,
37+
MAX31827_CNV_1_DIV_16_HZ,
38+
MAX31827_CNV_1_DIV_4_HZ,
39+
MAX31827_CNV_1_HZ,
40+
MAX31827_CNV_4_HZ,
41+
MAX31827_CNV_8_HZ,
42+
};
43+
44+
static const u16 max31827_conversions[] = {
45+
[MAX31827_CNV_1_DIV_64_HZ] = 64000,
46+
[MAX31827_CNV_1_DIV_32_HZ] = 32000,
47+
[MAX31827_CNV_1_DIV_16_HZ] = 16000,
48+
[MAX31827_CNV_1_DIV_4_HZ] = 4000,
49+
[MAX31827_CNV_1_HZ] = 1000,
50+
[MAX31827_CNV_4_HZ] = 250,
51+
[MAX31827_CNV_8_HZ] = 125,
52+
};
53+
4254
struct max31827_state {
4355
/*
4456
* Prevent simultaneous access to the i2c client.
@@ -54,15 +66,13 @@ static const struct regmap_config max31827_regmap = {
5466
.max_register = 0xA,
5567
};
5668

57-
static int write_alarm_val(struct max31827_state *st, unsigned int reg,
58-
long val)
69+
static int shutdown_write(struct max31827_state *st, unsigned int reg,
70+
unsigned int val)
5971
{
6072
unsigned int cfg;
61-
unsigned int tmp;
73+
unsigned int cnv_rate;
6274
int ret;
6375

64-
val = MAX31827_M_DGR_TO_16_BIT(val);
65-
6676
/*
6777
* Before the Temperature Threshold Alarm and Alarm Hysteresis Threshold
6878
* register values are changed over I2C, the part must be in shutdown
@@ -82,23 +92,34 @@ static int write_alarm_val(struct max31827_state *st, unsigned int reg,
8292
if (ret)
8393
goto unlock;
8494

85-
tmp = cfg & ~(MAX31827_CONFIGURATION_1SHOT_MASK |
95+
cnv_rate = MAX31827_CONFIGURATION_CNV_RATE_MASK & cfg;
96+
cfg = cfg & ~(MAX31827_CONFIGURATION_1SHOT_MASK |
8697
MAX31827_CONFIGURATION_CNV_RATE_MASK);
87-
ret = regmap_write(st->regmap, MAX31827_CONFIGURATION_REG, tmp);
98+
ret = regmap_write(st->regmap, MAX31827_CONFIGURATION_REG, cfg);
8899
if (ret)
89100
goto unlock;
90101

91102
ret = regmap_write(st->regmap, reg, val);
92103
if (ret)
93104
goto unlock;
94105

95-
ret = regmap_write(st->regmap, MAX31827_CONFIGURATION_REG, cfg);
106+
ret = regmap_update_bits(st->regmap, MAX31827_CONFIGURATION_REG,
107+
MAX31827_CONFIGURATION_CNV_RATE_MASK,
108+
cnv_rate);
96109

97110
unlock:
98111
mutex_unlock(&st->lock);
99112
return ret;
100113
}
101114

115+
static int write_alarm_val(struct max31827_state *st, unsigned int reg,
116+
long val)
117+
{
118+
val = MAX31827_M_DGR_TO_16_BIT(val);
119+
120+
return shutdown_write(st, reg, val);
121+
}
122+
102123
static umode_t max31827_is_visible(const void *state,
103124
enum hwmon_sensor_types type, u32 attr,
104125
int channel)
@@ -243,32 +264,7 @@ static int max31827_read(struct device *dev, enum hwmon_sensor_types type,
243264

244265
uval = FIELD_GET(MAX31827_CONFIGURATION_CNV_RATE_MASK,
245266
uval);
246-
switch (uval) {
247-
case MAX31827_CNV_1_DIV_64_HZ:
248-
*val = 64000;
249-
break;
250-
case MAX31827_CNV_1_DIV_32_HZ:
251-
*val = 32000;
252-
break;
253-
case MAX31827_CNV_1_DIV_16_HZ:
254-
*val = 16000;
255-
break;
256-
case MAX31827_CNV_1_DIV_4_HZ:
257-
*val = 4000;
258-
break;
259-
case MAX31827_CNV_1_HZ:
260-
*val = 1000;
261-
break;
262-
case MAX31827_CNV_4_HZ:
263-
*val = 250;
264-
break;
265-
case MAX31827_CNV_8_HZ:
266-
*val = 125;
267-
break;
268-
default:
269-
*val = 0;
270-
break;
271-
}
267+
*val = max31827_conversions[uval];
272268
}
273269
break;
274270

@@ -284,6 +280,7 @@ static int max31827_write(struct device *dev, enum hwmon_sensor_types type,
284280
u32 attr, int channel, long val)
285281
{
286282
struct max31827_state *st = dev_get_drvdata(dev);
283+
int res = 1;
287284
int ret;
288285

289286
switch (type) {
@@ -333,39 +330,27 @@ static int max31827_write(struct device *dev, enum hwmon_sensor_types type,
333330
if (!st->enable)
334331
return -EINVAL;
335332

336-
switch (val) {
337-
case 125:
338-
val = MAX31827_CNV_8_HZ;
339-
break;
340-
case 250:
341-
val = MAX31827_CNV_4_HZ;
342-
break;
343-
case 1000:
344-
val = MAX31827_CNV_1_HZ;
345-
break;
346-
case 4000:
347-
val = MAX31827_CNV_1_DIV_4_HZ;
348-
break;
349-
case 16000:
350-
val = MAX31827_CNV_1_DIV_16_HZ;
351-
break;
352-
case 32000:
353-
val = MAX31827_CNV_1_DIV_32_HZ;
354-
break;
355-
case 64000:
356-
val = MAX31827_CNV_1_DIV_64_HZ;
357-
break;
358-
default:
333+
/*
334+
* Convert the desired conversion rate into register
335+
* bits. res is already initialized with 1.
336+
*
337+
* This was inspired by lm73 driver.
338+
*/
339+
while (res < ARRAY_SIZE(max31827_conversions) &&
340+
val < max31827_conversions[res])
341+
res++;
342+
343+
if (res == ARRAY_SIZE(max31827_conversions) ||
344+
val != max31827_conversions[res])
359345
return -EINVAL;
360-
}
361346

362-
val = FIELD_PREP(MAX31827_CONFIGURATION_CNV_RATE_MASK,
363-
val);
347+
res = FIELD_PREP(MAX31827_CONFIGURATION_CNV_RATE_MASK,
348+
res);
364349

365350
return regmap_update_bits(st->regmap,
366351
MAX31827_CONFIGURATION_REG,
367352
MAX31827_CONFIGURATION_CNV_RATE_MASK,
368-
val);
353+
res);
369354
}
370355
break;
371356

0 commit comments

Comments
 (0)