Skip to content

Commit 484f68c

Browse files
Marcelo Machado Lagejic23
authored andcommitted
iio: adc: mcp3422: write bit operations using bitfield.h APIs
Replace manual bit manipulations with FIELD_GET(), FIELD_PREP() and FIELD_MODIFY() calls. The resulting code is more readable and maintainable, and 6 macros previously defined in the header are not needed anymore. Signed-off-by: Marcelo Machado Lage <marcelomlage@usp.br> Co-developed-by: Vinicius Lira <vinilira@usp.br> Signed-off-by: Vinicius Lira <vinilira@usp.br> Reviewed-by: Andy Shevchenko <andriy.shevchenko@intel.com> Signed-off-by: Jonathan Cameron <jic23@kernel.org>
1 parent 0c79414 commit 484f68c

1 file changed

Lines changed: 21 additions & 33 deletions

File tree

drivers/iio/adc/mcp3422.c

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* voltage unit is nV.
1414
*/
1515

16+
#include <linux/bitfield.h>
1617
#include <linux/bits.h>
1718
#include <linux/err.h>
1819
#include <linux/i2c.h>
@@ -39,14 +40,6 @@
3940
#define MCP3422_PGA_8 3
4041
#define MCP3422_CONT_SAMPLING BIT(4)
4142

42-
#define MCP3422_CHANNEL(config) (((config) & MCP3422_CHANNEL_MASK) >> 5)
43-
#define MCP3422_PGA(config) ((config) & MCP3422_PGA_MASK)
44-
#define MCP3422_SAMPLE_RATE(config) (((config) & MCP3422_SRATE_MASK) >> 2)
45-
46-
#define MCP3422_CHANNEL_VALUE(value) (((value) << 5) & MCP3422_CHANNEL_MASK)
47-
#define MCP3422_PGA_VALUE(value) ((value) & MCP3422_PGA_MASK)
48-
#define MCP3422_SAMPLE_RATE_VALUE(value) ((value << 2) & MCP3422_SRATE_MASK)
49-
5043
#define MCP3422_CHAN(_index) \
5144
{ \
5245
.type = IIO_VOLTAGE, \
@@ -109,7 +102,7 @@ static int mcp3422_update_config(struct mcp3422 *adc, u8 newconfig)
109102
static int mcp3422_read(struct mcp3422 *adc, int *value, u8 *config)
110103
{
111104
int ret = 0;
112-
u8 sample_rate = MCP3422_SAMPLE_RATE(adc->config);
105+
u8 sample_rate = FIELD_GET(MCP3422_SRATE_MASK, adc->config);
113106
u8 buf[4] = {0, 0, 0, 0};
114107
u32 temp;
115108

@@ -137,18 +130,18 @@ static int mcp3422_read_channel(struct mcp3422 *adc,
137130

138131
mutex_lock(&adc->lock);
139132

140-
if (req_channel != MCP3422_CHANNEL(adc->config)) {
133+
if (req_channel != FIELD_GET(MCP3422_CHANNEL_MASK, adc->config)) {
141134
config = adc->config;
142-
config &= ~MCP3422_CHANNEL_MASK;
143-
config |= MCP3422_CHANNEL_VALUE(req_channel);
144-
config &= ~MCP3422_PGA_MASK;
145-
config |= MCP3422_PGA_VALUE(adc->pga[req_channel]);
135+
136+
FIELD_MODIFY(MCP3422_CHANNEL_MASK, &config, req_channel);
137+
FIELD_MODIFY(MCP3422_PGA_MASK, &config, adc->pga[req_channel]);
138+
146139
ret = mcp3422_update_config(adc, config);
147140
if (ret < 0) {
148141
mutex_unlock(&adc->lock);
149142
return ret;
150143
}
151-
msleep(mcp3422_read_times[MCP3422_SAMPLE_RATE(adc->config)]);
144+
msleep(mcp3422_read_times[FIELD_GET(MCP3422_SRATE_MASK, adc->config)]);
152145
}
153146

154147
ret = mcp3422_read(adc, value, &config);
@@ -164,9 +157,8 @@ static int mcp3422_read_raw(struct iio_dev *iio,
164157
{
165158
struct mcp3422 *adc = iio_priv(iio);
166159
int err;
167-
168-
u8 sample_rate = MCP3422_SAMPLE_RATE(adc->config);
169-
u8 pga = MCP3422_PGA(adc->config);
160+
u8 sample_rate = FIELD_GET(MCP3422_SRATE_MASK, adc->config);
161+
u8 pga = FIELD_GET(MCP3422_PGA_MASK, adc->config);
170162

171163
switch (mask) {
172164
case IIO_CHAN_INFO_RAW:
@@ -182,7 +174,7 @@ static int mcp3422_read_raw(struct iio_dev *iio,
182174
return IIO_VAL_INT_PLUS_NANO;
183175

184176
case IIO_CHAN_INFO_SAMP_FREQ:
185-
*val1 = mcp3422_sample_rates[MCP3422_SAMPLE_RATE(adc->config)];
177+
*val1 = mcp3422_sample_rates[FIELD_GET(MCP3422_SRATE_MASK, adc->config)];
186178
return IIO_VAL_INT;
187179

188180
default:
@@ -200,7 +192,7 @@ static int mcp3422_write_raw(struct iio_dev *iio,
200192
u8 temp;
201193
u8 config = adc->config;
202194
u8 req_channel = channel->channel;
203-
u8 sample_rate = MCP3422_SAMPLE_RATE(config);
195+
u8 sample_rate = FIELD_GET(MCP3422_SRATE_MASK, config);
204196
u8 i;
205197

206198
switch (mask) {
@@ -212,10 +204,8 @@ static int mcp3422_write_raw(struct iio_dev *iio,
212204
if (val2 == mcp3422_scales[sample_rate][i]) {
213205
adc->pga[req_channel] = i;
214206

215-
config &= ~MCP3422_CHANNEL_MASK;
216-
config |= MCP3422_CHANNEL_VALUE(req_channel);
217-
config &= ~MCP3422_PGA_MASK;
218-
config |= MCP3422_PGA_VALUE(adc->pga[req_channel]);
207+
FIELD_MODIFY(MCP3422_CHANNEL_MASK, &config, req_channel);
208+
FIELD_MODIFY(MCP3422_PGA_MASK, &config, adc->pga[req_channel]);
219209

220210
return mcp3422_update_config(adc, config);
221211
}
@@ -242,10 +232,8 @@ static int mcp3422_write_raw(struct iio_dev *iio,
242232
return -EINVAL;
243233
}
244234

245-
config &= ~MCP3422_CHANNEL_MASK;
246-
config |= MCP3422_CHANNEL_VALUE(req_channel);
247-
config &= ~MCP3422_SRATE_MASK;
248-
config |= MCP3422_SAMPLE_RATE_VALUE(temp);
235+
FIELD_MODIFY(MCP3422_CHANNEL_MASK, &config, req_channel);
236+
FIELD_MODIFY(MCP3422_SRATE_MASK, &config, temp);
249237

250238
return mcp3422_update_config(adc, config);
251239

@@ -284,7 +272,7 @@ static ssize_t mcp3422_show_scales(struct device *dev,
284272
struct device_attribute *attr, char *buf)
285273
{
286274
struct mcp3422 *adc = iio_priv(dev_to_iio_dev(dev));
287-
u8 sample_rate = MCP3422_SAMPLE_RATE(adc->config);
275+
u8 sample_rate = FIELD_GET(MCP3422_SRATE_MASK, adc->config);
288276

289277
return sprintf(buf, "0.%09u 0.%09u 0.%09u 0.%09u\n",
290278
mcp3422_scales[sample_rate][0],
@@ -377,10 +365,10 @@ static int mcp3422_probe(struct i2c_client *client)
377365
}
378366

379367
/* meaningful default configuration */
380-
config = (MCP3422_CONT_SAMPLING
381-
| MCP3422_CHANNEL_VALUE(0)
382-
| MCP3422_PGA_VALUE(MCP3422_PGA_1)
383-
| MCP3422_SAMPLE_RATE_VALUE(MCP3422_SRATE_240));
368+
config = MCP3422_CONT_SAMPLING |
369+
FIELD_PREP(MCP3422_CHANNEL_MASK, 0) |
370+
FIELD_PREP(MCP3422_PGA_MASK, MCP3422_PGA_1) |
371+
FIELD_PREP(MCP3422_SRATE_MASK, MCP3422_SRATE_240);
384372
err = mcp3422_update_config(adc, config);
385373
if (err < 0)
386374
return err;

0 commit comments

Comments
 (0)