Skip to content

Commit 295b094

Browse files
iio: adc: Fix read and write functions
- The read and write functions were tested on actual hardware and both worked fine - Fix the overlay to be correctly read by the driver Signed-off-by: Marilene A Garcia <[email protected]>
1 parent 6c5e260 commit 295b094

File tree

3 files changed

+44
-51
lines changed

3 files changed

+44
-51
lines changed

Documentation/devicetree/bindings/iio/adc/adi,max14001.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ description:
1414
Bindings for the Analog Devices MAX14001-MAX14002 Configurable,
1515
Isolated 10-bit ADCs for Multi-Range Binary Inputs.
1616

17-
Datasheet can be found here:
17+
Datasheet can be found here
1818
https://www.analog.com/media/en/technical-documentation/data-sheets/MAX14001-MAX14002.pdf
1919

2020
properties:

arch/arm/boot/dts/overlays/rpi-max14001-pmb-overlay.dts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,16 @@
1212

1313
/ {
1414
compatible = "brcm,bcm2712";
15+
};
1516

17+
&{/} {
1618
vdd: fixedregulator@0 {
1719
compatible = "regulator-fixed";
1820
regulator-name = "Isolated DC-DC Power Supply Input Voltage";
1921
regulator-min-microvolt = <3300000>;
2022
regulator-max-microvolt = <3300000>;
2123
regulator-boot-on;
22-
enable-active-high;
24+
regulator-always-on;
2325
status = "okay";
2426
};
2527

@@ -29,7 +31,7 @@
2931
regulator-min-microvolt = <3300000>;
3032
regulator-max-microvolt = <3300000>;
3133
regulator-boot-on;
32-
enable-active-high;
34+
regulator-always-on;
3335
status = "okay";
3436
};
3537

@@ -39,7 +41,7 @@
3941
regulator-min-microvolt = <1250000>;
4042
regulator-max-microvolt = <1250000>;
4143
regulator-boot-on;
42-
enable-active-high;
44+
regulator-always-on;
4345
status = "okay";
4446
};
4547
};
@@ -57,9 +59,9 @@
5759
};
5860
max14001_current_channel: max14001@1 {
5961
compatible = "adi,max14001";
62+
current-channel;
6063
reg = <0x1>;
6164
spi-max-frequency = <5000000>;
62-
current-channel;
6365
vdd-supply = <&vdd>;
6466
vddl-supply = <&vddl>;
6567
vrefin-supply = <&vrefin>;

drivers/iio/adc/max14001.c

Lines changed: 37 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -103,50 +103,53 @@ struct max14001_state {
103103

104104
static int max14001_spi_read(struct max14001_state *st, u16 reg, int *val)
105105
{
106-
u16 rx, reversed;
107-
u16 tx = 0;
106+
u16 rx, tx = 0;
108107
int ret;
109108

110-
dev_info(&st->spi->dev, "%s: reg: %x, val: %x\n", __func__, reg, *val);
111-
112109
tx |= FIELD_PREP(MAX14001_MASK_ADDR, reg);
113110
tx |= FIELD_PREP(MAX14001_MASK_WR, MAX14001_REG_READ);
114-
reversed = bitrev16(tx);
115-
116-
ret = spi_write_then_read(st->spi, &reversed, 2, &rx, 2);
117-
if (ret < 0)
118-
return ret;
119-
120-
/* TODO: Validate this line in the hw, could be le16_to_cpu */
121-
reversed = bitrev16(be16_to_cpu(rx));
122-
*val = FIELD_GET(MAX14001_MASK_DATA, reversed);
111+
tx = bitrev16(tx);
112+
113+
struct spi_transfer xfer[] = {
114+
{
115+
.tx_buf = &tx,
116+
.len = sizeof(tx),
117+
.bits_per_word = 16,
118+
.cs_change = 1,
119+
},
120+
{
121+
.rx_buf = &rx,
122+
.len = sizeof(rx),
123+
.bits_per_word = 16,
124+
},
125+
};
126+
ret = spi_sync_transfer(st->spi, xfer, ARRAY_SIZE(xfer));
127+
128+
rx = bitrev16(rx);
129+
*val = FIELD_GET(MAX14001_MASK_DATA, rx);
123130

124131
return ret;
125132
}
126133

127134
static int max14001_spi_write(struct max14001_state *st, u16 reg, u16 val)
128135
{
129-
struct spi_transfer xfer;
136+
u16 tx = 0;
130137
int ret;
131-
u16 tx, reversed;
132-
u16 msg = 0;
133-
134-
dev_info(&st->spi->dev, "%s: reg: %x, val: %x\n", __func__, reg, val);
135-
136-
msg |= FIELD_PREP(MAX14001_MASK_ADDR, reg);
137-
msg |= FIELD_PREP(MAX14001_MASK_WR, MAX14001_REG_WRITE);
138-
msg |= FIELD_PREP(MAX14001_MASK_DATA, val);
139138

140-
reversed = bitrev16(msg);
141-
/* TODO: Validate this line in the hw, could be put_unaligned_le16 */
142-
put_unaligned_be16(reversed, &tx);
143-
144-
xfer.tx_buf = &tx;
145-
xfer.len = sizeof(tx);
146-
147-
dev_info(&st->spi->dev, "%s: msg: %x, tx: %x\n", __func__, msg, tx);
148-
149-
ret = spi_sync_transfer(st->spi, &xfer, 1);
139+
tx |= FIELD_PREP(MAX14001_MASK_ADDR, reg);
140+
tx |= FIELD_PREP(MAX14001_MASK_WR, MAX14001_REG_WRITE);
141+
tx |= FIELD_PREP(MAX14001_MASK_DATA, val);
142+
tx = bitrev16(tx);
143+
144+
struct spi_transfer xfer[] = {
145+
{
146+
.tx_buf = &tx,
147+
.len = sizeof(tx),
148+
.bits_per_word = 16,
149+
},
150+
};
151+
152+
ret = spi_sync_transfer(st->spi, xfer, ARRAY_SIZE(xfer));
150153
if (ret < 0)
151154
return ret;
152155

@@ -222,22 +225,19 @@ static int max14001_read_raw(struct iio_dev *indio_dev,
222225
switch (mask) {
223226
case IIO_CHAN_INFO_RAW:
224227
ret = max14001_spi_read(st, MAX14001_REG_ADC, val);
225-
dev_info(&st->spi->dev, "%s: IIO_CHAN_INFO_RAW: channel: %d, val: %d\n", __func__, chan->channel, *val);
226228
if (ret < 0)
227229
return ret;
228230

229231
return IIO_VAL_INT;
230232
case IIO_CHAN_INFO_AVERAGE_RAW:
231233
ret = max14001_spi_read(st, MAX14001_REG_FADC, val);
232-
dev_info(&st->spi->dev, "%s: IIO_CHAN_INFO_AVERAGE_RAW: channel: %d, val: %d\n", __func__, chan->channel, *val);
233234
if (ret < 0)
234235
return ret;
235236

236237
return IIO_VAL_INT;
237238
case IIO_CHAN_INFO_SCALE:
238239
*val = st->vref_mv;
239240
*val2 = 10;
240-
dev_info(&st->spi->dev, "%s: IIO_CHAN_INFO_SCALE: val: %d, val2: %d\n", __func__, *val, *val2);
241241

242242
return IIO_VAL_FRACTIONAL_LOG2;
243243
}
@@ -277,8 +277,8 @@ static int max14001_probe(struct spi_device *spi)
277277
struct device *dev = &spi->dev;
278278
struct max14001_state *st;
279279
struct iio_dev *indio_dev;
280-
bool current_channel = false;
281280
int ret;
281+
bool current_channel = false;
282282

283283
info = spi_get_device_match_data(spi);
284284
if (!dev)
@@ -307,18 +307,11 @@ static int max14001_probe(struct spi_device *spi)
307307
ret = devm_regulator_get_enable_read_voltage(dev, "vrefin");
308308
if (ret < 0) {
309309
st->vref_mv = 1250000 / 1000;
310-
dev_info(&st->spi->dev, "%s: vrefin not found. vref_mv %d\n", __func__, st->vref_mv);
311310
} else {
312311
st->vref_mv = ret / 1000;
313-
dev_info(&st->spi->dev, "%s: vrefin found. vref_mv %d\n", __func__, st->vref_mv);
314-
}
315-
316-
for_each_available_child_of_node_scoped(spi->dev.of_node, child) {
317-
current_channel = of_property_read_bool(child, "current-channel");
318-
if (current_channel)
319-
break;
320312
}
321313

314+
current_channel = device_property_read_bool(dev, "current-channel");
322315
if (current_channel) {
323316
indio_dev->channels = max14001_channel_current;
324317
indio_dev->num_channels = ARRAY_SIZE(max14001_channel_current);
@@ -327,8 +320,6 @@ static int max14001_probe(struct spi_device *spi)
327320
indio_dev->num_channels = ARRAY_SIZE(max14001_channel_voltage);
328321
}
329322

330-
dev_info(&st->spi->dev, "%s: probe\n", __func__);
331-
332323
/* Write the appropriate verification registers values to clear the
333324
* failed memory validation (MV Fault)
334325
*/

0 commit comments

Comments
 (0)