27
27
28
28
#define MAX31827_12_BIT_CNV_TIME 141
29
29
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
-
38
30
#define MAX31827_16_BIT_TO_M_DGR (x ) (sign_extend32(x, 15) * 1000 / 16)
39
31
#define MAX31827_M_DGR_TO_16_BIT (x ) (((x) << 4) / 1000)
40
32
#define MAX31827_DEVICE_ENABLE (x ) ((x) ? 0xA : 0x0)
41
33
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
+
42
54
struct max31827_state {
43
55
/*
44
56
* Prevent simultaneous access to the i2c client.
@@ -54,15 +66,13 @@ static const struct regmap_config max31827_regmap = {
54
66
.max_register = 0xA ,
55
67
};
56
68
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 )
59
71
{
60
72
unsigned int cfg ;
61
- unsigned int tmp ;
73
+ unsigned int cnv_rate ;
62
74
int ret ;
63
75
64
- val = MAX31827_M_DGR_TO_16_BIT (val );
65
-
66
76
/*
67
77
* Before the Temperature Threshold Alarm and Alarm Hysteresis Threshold
68
78
* 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,
82
92
if (ret )
83
93
goto unlock ;
84
94
85
- tmp = cfg & ~(MAX31827_CONFIGURATION_1SHOT_MASK |
95
+ cnv_rate = MAX31827_CONFIGURATION_CNV_RATE_MASK & cfg ;
96
+ cfg = cfg & ~(MAX31827_CONFIGURATION_1SHOT_MASK |
86
97
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 );
88
99
if (ret )
89
100
goto unlock ;
90
101
91
102
ret = regmap_write (st -> regmap , reg , val );
92
103
if (ret )
93
104
goto unlock ;
94
105
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 );
96
109
97
110
unlock :
98
111
mutex_unlock (& st -> lock );
99
112
return ret ;
100
113
}
101
114
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
+
102
123
static umode_t max31827_is_visible (const void * state ,
103
124
enum hwmon_sensor_types type , u32 attr ,
104
125
int channel )
@@ -243,32 +264,7 @@ static int max31827_read(struct device *dev, enum hwmon_sensor_types type,
243
264
244
265
uval = FIELD_GET (MAX31827_CONFIGURATION_CNV_RATE_MASK ,
245
266
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 ];
272
268
}
273
269
break ;
274
270
@@ -284,6 +280,7 @@ static int max31827_write(struct device *dev, enum hwmon_sensor_types type,
284
280
u32 attr , int channel , long val )
285
281
{
286
282
struct max31827_state * st = dev_get_drvdata (dev );
283
+ int res = 1 ;
287
284
int ret ;
288
285
289
286
switch (type ) {
@@ -333,39 +330,27 @@ static int max31827_write(struct device *dev, enum hwmon_sensor_types type,
333
330
if (!st -> enable )
334
331
return - EINVAL ;
335
332
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 ])
359
345
return - EINVAL ;
360
- }
361
346
362
- val = FIELD_PREP (MAX31827_CONFIGURATION_CNV_RATE_MASK ,
363
- val );
347
+ res = FIELD_PREP (MAX31827_CONFIGURATION_CNV_RATE_MASK ,
348
+ res );
364
349
365
350
return regmap_update_bits (st -> regmap ,
366
351
MAX31827_CONFIGURATION_REG ,
367
352
MAX31827_CONFIGURATION_CNV_RATE_MASK ,
368
- val );
353
+ res );
369
354
}
370
355
break ;
371
356
0 commit comments