dt-bindings: iio: dac: add MCP4821
Subsystems (3)
Anshul Dalal
maintainerPATCH v2 1/2 dt-bindings: iio: dac: add MCP4821
Adds support for MCP48xx series of DACs.
Datasheet: https://ww1.microchip.com/downloads/en/DeviceDoc/22244B.pdf #MCP48x1
Datasheet: https://ww1.microchip.com/downloads/en/DeviceDoc/20002249B.pdf #MCP48x2
---
Changes for v2:
- Changed order in device table to numerical
- Made vdd_supply required
- Added 'Reviewed-by: Conor Dooley'
Previous versions:
v1: https://lore.kernel.org/lkml/[email protected]/
--- .../bindings/iio/dac/microchip,mcp4821.yaml | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 Documentation/devicetree/bindings/iio/dac/microchip,mcp4821.yaml
Documentation/devicetree/bindings/iio/dac/microchip,mcp4821.yaml new file +64 -0
@@ -0,0 +1,64 @@ +# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/iio/dac/microchip,mcp4821.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Microchip MCP4821 and similar DACs + +description: | + Supports MCP48x1 (single channel) and MCP48x2 (dual channel) series of DACs. + Device supports simplex communication over SPI in Mode 0,1 and Mode 1,1. + + +---------+--------------+-------------+ + | Device | Resolution | Channels | + |---------|--------------|-------------| + | MCP4801 | 8-bit | 1 | + | MCP4802 | 8-bit | 2 | + | MCP4811 | 10-bit | 1 | + | MCP4812 | 10-bit | 2 | + | MCP4821 | 12-bit | 1 | + | MCP4822 | 12-bit | 2 | + +---------+--------------+-------------+ + + Datasheet: + MCP48x1: https://ww1.microchip.com/downloads/en/DeviceDoc/22244B.pdf + MCP48x2: https://ww1.microchip.com/downloads/en/DeviceDoc/20002249B.pdf + +maintainers: + - Anshul Dalal <[email protected]> + +properties: + compatible: + enum: + - microchip,mcp4801 + - microchip,mcp4802 + - microchip,mcp4811 + - microchip,mcp4812 + - microchip,mcp4821 + - microchip,mcp4822 + + reg: + maxItems: 1 + + vdd-supply: true + +required: + - compatible + - reg + - vdd-supply + +additionalProperties: false + +examples: + - | + spi { + #address-cells = <1>; + #size-cells = <0>; + + dac@0 { + compatible = "microchip,mcp4821"; + reg = <0>; + vdd-supply = <&vdd_regulator>; + }; + };
Signature · 1 line
2.43.0
Anshul Dalal
maintainerPATCH v2 2/2 iio: dac: driver for MCP4821
Adds driver for the MCP48xx series of DACs.
Device uses a simplex SPI channel. To set the value of an output channel, a 16-bit data of following format must be written:
Bit field | Description
15 [MSB] | Channel selection bit
0 -> Channel A
1 -> Channel B
13 | Output Gain Selection bit
0 -> 2x Gain (Vref = 4.096V)
1 -> 1x Gain (Vref = 2.048V)
12 | Output Shutdown Control bit
0 -> Shutdown the selected channel
1 -> Active mode operation11-0 [LSB]| DAC Input Data bits
Value's big endian representation is taken as input for the selected DAC channel. For devices with a resolution of less than 12-bits, only the x most significant bits are considered where x is the resolution of the device.
Reference: Page#22 [MCP48x2 Datasheet]
Supported devices:
+---------+--------------+-------------+ | Device | Resolution | Channels | |---------|--------------|-------------| | MCP4801 | 8-bit | 1 | | MCP4802 | 8-bit | 2 | | MCP4811 | 10-bit | 1 | | MCP4812 | 10-bit | 2 | | MCP4821 | 12-bit | 1 | | MCP4822 | 12-bit | 2 | +---------+--------------+-------------+
Devices tested:
MCP4821 [12-bit single channel]
MCP4802 [8-bit dual channel]
Tested on Raspberry Pi Zero 2W
Datasheet: https://ww1.microchip.com/downloads/en/DeviceDoc/22244B.pdf #MCP48x1
Datasheet: https://ww1.microchip.com/downloads/en/DeviceDoc/20002249B.pdf #MCP48x2
---
Changes for v2:
- Changed device ordering to numerical
- Fixed ordering of headers
- Added struct mcp4821_chip_info instead of relying on driver_data from spi device ids
- Use scoped_guards for all mutex locks
- Changed write_val from __be16 to u16 in mcp4821_write_raw Fixes sparse warning: incorrect type in assignment
Previous versions:
v1: https://lore.kernel.org/lkml/[email protected]/
--- MAINTAINERS | 7 ++ drivers/iio/dac/Kconfig | 10 ++ drivers/iio/dac/Makefile | 1 + drivers/iio/dac/mcp4821.c | 228 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 246 insertions(+) create mode 100644 drivers/iio/dac/mcp4821.c
MAINTAINERS +7 -0
@@ -13029,6 +13029,13 @@ F: Documentation/ABI/testing/sysfs-bus-iio-potentiometer-mcp4531 F: drivers/iio/potentiometer/mcp4018.c F: drivers/iio/potentiometer/mcp4531.c +MCP4821 DAC DRIVER +M: Anshul Dalal <[email protected]> +L: [email protected] +S: Maintained +F: Documentation/devicetree/bindings/iio/dac/microchip,mcp4821.yaml +F: drivers/iio/dac/mcp4821.c + MCR20A IEEE-802.15.4 RADIO DRIVER M: Stefan Schmidt <[email protected]> L: [email protected]
drivers/iio/dac/Kconfig +10 -0
@@ -400,6 +400,16 @@ config MCP4728 To compile this driver as a module, choose M here: the module will be called mcp4728. +config MCP4821 + tristate "MCP4801/02/11/12/21/22 DAC driver" + depends on SPI + help + Say yes here to build the driver for the Microchip MCP4801 + MCP4802, MCP4811, MCP4812, MCP4821 and MCP4822 DAC devices. + + To compile this driver as a module, choose M here: the module + will be called mcp4821. + config MCP4922 tristate "MCP4902, MCP4912, MCP4922 DAC driver" depends on SPI
drivers/iio/dac/Makefile +1 -0
@@ -42,6 +42,7 @@ obj-$(CONFIG_MAX5522) += max5522.o obj-$(CONFIG_MAX5821) += max5821.o obj-$(CONFIG_MCP4725) += mcp4725.o obj-$(CONFIG_MCP4728) += mcp4728.o +obj-$(CONFIG_MCP4821) += mcp4821.o obj-$(CONFIG_MCP4922) += mcp4922.o obj-$(CONFIG_STM32_DAC_CORE) += stm32-dac-core.o obj-$(CONFIG_STM32_DAC) += stm32-dac.o
drivers/iio/dac/mcp4821.c new file +228 -0
228 lines not on this page · raw message
Signature · 1 line
2.43.0
David Lechner
reviewerQuoted 29 lines · Anshul Dalal
Adds support for MCP48xx series of DACs.
Datasheet: https://ww1.microchip.com/downloads/en/DeviceDoc/22244B.pdf #MCP48x1
Datasheet: https://ww1.microchip.com/downloads/en/DeviceDoc/20002249B.pdf #MCP48x2Reviewed-by Conor Dooley <[email protected]>Signed-off-by Anshul Dalal <[email protected]>---
Changes for v2:
- Changed order in device table to numerical
- Madevdd_supplyrequired
- Added 'Reviewed-by: Conor Dooley'Previous versions:
v1: https://lore.kernel.org/lkml/[email protected]/--- .../bindings/iio/dac/microchip,mcp4821.yaml | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 Documentation/devicetree/bindings/iio/dac/microchip,mcp4821.yamlDocumentation/devicetree/bindings/iio/dac/microchip,mcp4821.yaml new file +1
-0@@ -0,0 +1,64 @@ +# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
Documentation/devicetree/bindings/iio/dac/microchip,mcp4821.yaml line 11
Quoted 10 lines
Documentation/devicetree/bindings/iio/dac/microchip,mcp4821.yaml new file +10
-0+%YAML 1.2 +--- +$id: http://devicetree.org/schemas/iio/dac/microchip,mcp4821.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Microchip MCP4821 and similar DACs + +description: | + Supports MCP48x1 (single channel) and MCP48x2 (dual channel) series of DACs. + Device supports simplex communication over SPI in Mode 0,1 and Mode 1,1.
It seems like SPI modes usually only have one number in them, i.e. mode 1 and mode 3 in this case, I'm guessing.
Quoted 23 lines
Documentation/devicetree/bindings/iio/dac/microchip,mcp4821.yaml new file +23
-0+ + +---------+--------------+-------------+ + | Device | Resolution | Channels | + |---------|--------------|-------------| + | MCP4801 | 8-bit | 1 | + | MCP4802 | 8-bit | 2 | + | MCP4811 | 10-bit | 1 | + | MCP4812 | 10-bit | 2 | + | MCP4821 | 12-bit | 1 | + | MCP4822 | 12-bit | 2 | + +---------+--------------+-------------+ + + Datasheet: + MCP48x1: https://ww1.microchip.com/downloads/en/DeviceDoc/22244B.pdf + MCP48x2: https://ww1.microchip.com/downloads/en/DeviceDoc/20002249B.pdf + +maintainers: + - Anshul Dalal <[email protected]> + +properties: + compatible: + enum: + - microchip,mcp4801
Documentation/devicetree/bindings/iio/dac/microchip,mcp4821.yaml line 44
Quoted 10 lines
Documentation/devicetree/bindings/iio/dac/microchip,mcp4821.yaml new file +10
-0+ - microchip,mcp4802 + - microchip,mcp4811 + - microchip,mcp4812 + - microchip,mcp4821 + - microchip,mcp4822 + + reg: + maxItems: 1 + + vdd-supply: true
What about the SHDN and LDAC pins? It seems like all should have an ldac-gpios property and MCP48x1 should have a shdn-gpios property for these.
Quoted 24 lines
Documentation/devicetree/bindings/iio/dac/microchip,mcp4821.yaml new file +20
-1+ +required: + - compatible + - reg + - vdd-supply + +additionalProperties: false + +examples: + - | + spi { + #address-cells = <1>; + #size-cells = <0>; + + dac@0 { + compatible = "microchip,mcp4821"; + reg = <0>; + vdd-supply = <&vdd_regulator>; + }; + }; --2.43.0
David Lechner
reviewerQuoted 36 lines · David Lechner
Quoted 34 lines · Anshul Dalal
Adds support for MCP48xx series of DACs.
Datasheet: https://ww1.microchip.com/downloads/en/DeviceDoc/22244B.pdf #MCP48x1
Datasheet: https://ww1.microchip.com/downloads/en/DeviceDoc/20002249B.pdf #MCP48x2Reviewed-by Conor Dooley <[email protected]>Signed-off-by Anshul Dalal <[email protected]>---
Changes for v2:
- Changed order in device table to numerical
- Madevdd_supplyrequired
- Added 'Reviewed-by: Conor Dooley'Previous versions:
v1: https://lore.kernel.org/lkml/[email protected]/--- .../bindings/iio/dac/microchip,mcp4821.yaml | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 Documentation/devicetree/bindings/iio/dac/microchip,mcp4821.yamlDocumentation/devicetree/bindings/iio/dac/microchip,mcp4821.yaml new file +6
-0@@ -0,0 +1,64 @@ +# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/iio/dac/microchip,mcp4821.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# +
Quoted 8 lines
Documentation/devicetree/bindings/iio/dac/microchip,mcp4821.yaml line 11
Quoted 5 lines
Documentation/devicetree/bindings/iio/dac/microchip,mcp4821.yaml new file +5
-0+title: Microchip MCP4821 and similar DACs + +description: | + Supports MCP48x1 (single channel) and MCP48x2 (dual channel) series of DACs. + Device supports simplex communication over SPI in Mode 0,1 and Mode 1,1.It seems like SPI modes usually only have one number in them, i.e. mode 1 and mode 3 in this case, I'm guessing.
Oh, and doesn't this mean that we need to include spi-cpha and spi-cpol properties since they aren't implicit in the bindings anymore since [1]?
[1]: https://lore.kernel.org/all/[email protected]/
Quoted 63 lines
Quoted 24 lines
Documentation/devicetree/bindings/iio/dac/microchip,mcp4821.yaml new file +23
-0+ + +---------+--------------+-------------+ + | Device | Resolution | Channels | + |---------|--------------|-------------| + | MCP4801 | 8-bit | 1 | + | MCP4802 | 8-bit | 2 | + | MCP4811 | 10-bit | 1 | + | MCP4812 | 10-bit | 2 | + | MCP4821 | 12-bit | 1 | + | MCP4822 | 12-bit | 2 | + +---------+--------------+-------------+ + + Datasheet: + MCP48x1: https://ww1.microchip.com/downloads/en/DeviceDoc/22244B.pdf + MCP48x2: https://ww1.microchip.com/downloads/en/DeviceDoc/20002249B.pdf + +maintainers: + - Anshul Dalal <[email protected]> + +properties: + compatible: + enum: + - microchip,mcp4801Documentation/devicetree/bindings/iio/dac/microchip,mcp4821.yaml line 44
Quoted 10 lines
Documentation/devicetree/bindings/iio/dac/microchip,mcp4821.yaml new file +10
-0+ - microchip,mcp4802 + - microchip,mcp4811 + - microchip,mcp4812 + - microchip,mcp4821 + - microchip,mcp4822 + + reg: + maxItems: 1 + + vdd-supply: trueWhat about the SHDN and LDAC pins? It seems like all should have an ldac-gpios property and MCP48x1 should have a shdn-gpios property for these.
Quoted 24 lines
Documentation/devicetree/bindings/iio/dac/microchip,mcp4821.yaml new file +20
-1+ +required: + - compatible + - reg + - vdd-supply + +additionalProperties: false + +examples: + - | + spi { + #address-cells = <1>; + #size-cells = <0>; + + dac@0 { + compatible = "microchip,mcp4821"; + reg = <0>; + vdd-supply = <&vdd_regulator>; + }; + }; --2.43.0
Jonathan Cameron
maintainerRe: [PATCH v2 2/2] iio: dac: driver for MCP4821
In reply to Anshul DalalQuoted 38 lines · On Sun, 17 Dec 2023 23:38:34 +0530 Anshul Dalal <[email protected]> wrote:
Adds driver for the MCP48xx series of DACs.
Device uses a simplex SPI channel. To set the value of an output channel, a 16-bit data of following format must be written:
Bit field | Description
15 [MSB] | Channel selection bit0 -> Channel A 1 -> Channel B 13 | Output Gain Selection bit 0 -> 2x Gain (Vref = 4.096V) 1 -> 1x Gain (Vref = 2.048V) 12 | Output Shutdown Control bit 0 -> Shutdown the selected channel 1 -> Active mode operation11-0 [LSB]| DAC Input Data bits
Value's big endian representation is taken as input for the selected DAC channel. For devices with a resolution of less than 12-bits, only the x most significant bits are considered where x is the resolution of the device.
Reference: Page#22 [MCP48x2 Datasheet]Supported devices:
+---------+--------------+-------------+ | Device | Resolution | Channels | |---------|--------------|-------------| | MCP4801 | 8-bit | 1 | | MCP4802 | 8-bit | 2 | | MCP4811 | 10-bit | 1 | | MCP4812 | 10-bit | 2 | | MCP4821 | 12-bit | 1 | | MCP4822 | 12-bit | 2 | +---------+--------------+-------------+Devices tested:
MCP4821 [12-bit single channel]
MCP4802 [8-bit dual channel]
Quoted 7 lines
Tested on Raspberry Pi Zero 2W
Datasheet: https://ww1.microchip.com/downloads/en/DeviceDoc/22244B.pdf #MCP48x1
Datasheet: https://ww1.microchip.com/downloads/en/DeviceDoc/20002249B.pdf #MCP48x2Signed-off-by Anshul Dalal <[email protected]>
Hi Anshul,
Various minor comments inline.
Thanks,
Jonathan
Quoted 42 lines
drivers/iio/dac/mcp4821.c new file +36
-0@@ -0,0 +1,228 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Copyright (C) 2023 Anshul Dalal <[email protected]> +* +* Driver for Microchip MCP4801, MCP4802, MCP4811, MCP4812, MCP4821 and MCP4822 +* +* Based on the work of: +* Michael Welling (MCP4922 Driver) +* +* Datasheet: +* MCP48x1: https://ww1.microchip.com/downloads/en/DeviceDoc/22244B.pdf +* MCP48x2: https://ww1.microchip.com/downloads/en/DeviceDoc/20002249B.pdf +* +* TODO: +* - Configurable gain +* - Regulator control +*/ + +#include <linux/module.h> +#include <linux/mutex.h> +#include <linux/of.h> +#include <linux/spi/spi.h> + +#include <linux/iio/iio.h> +#include <linux/iio/types.h> + +#include <asm/unaligned.h> + +#define MCP4821_ACTIVE_MODE BIT(12) +#define MCP4802_SECOND_CHAN BIT(15) + +/* DAC uses an internal Voltage reference of 4.096V at a gain of 2x */ +#define MCP4821_2X_GAIN_VREF_MV 4096 + +enum mcp4821_supported_drvice_ids { +ID_MCP4801 ,
drivers/iio/dac/mcp4821.c line 46
Quoted 10 lines
drivers/iio/dac/mcp4821.c new file +10
-0+ ID_MCP4802, +ID_MCP4811 , +ID_MCP4812 , +ID_MCP4821 , + ID_MCP4822, +}; + +struct mcp4821_state { +struct spi_device *spi; +// Protects dac_value
/* Protects dac_value */
However I'm not clear why a u16 array needs protecting? Whilst in theory the compiler might be annoying and do torn writes (write it as 2 1 byte values) that's pretty unlikely and we don't normally bother to protect against that.
Quoted 8 lines
drivers/iio/dac/mcp4821.c new file +8
-0+struct mutex lock; +u16 dac_value[2 ]; +}; + +struct mcp4821_chip_info { +const char *name; +int num_channels; +const struct iio_chan_spec channels[2 ];
drivers/iio/dac/mcp4821.c line 64
Quoted 10 lines
drivers/iio/dac/mcp4821.c new file +10
-0+}; + +#define MCP4821_CHAN (channel_id, resolution) \ + { \ + .type = IIO_VOLTAGE, .output = 1, .indexed = 1, \ + .channel = (channel_id), \ + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ + .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ + .scan_type = { \ + .sign = 'u', \
Not used so don't bother setting it until you add buffered support.
drivers/iio/dac/mcp4821.c line 66
Quoted 2 lines
drivers/iio/dac/mcp4821.c new file +2
-0+ .realbits = (resolution), \ + .storagebits = 16, \
Not used, so don't bother setting it.
Quoted 27 lines
drivers/iio/dac/mcp4821.c new file +26
-0+ .shift = 12 - (resolution), \ + }, \ + } +static int mcp4821_read_raw (struct iio_dev *indio_dev, +struct iio_chan_spec const *chan,int *val, +int *val2,long mask) +{ +struct mcp4821_state *state; + +switch (mask) { +case IIO_CHAN_INFO_RAW : + state =iio_priv (indio_dev); +scoped_guard (mutex, &state->lock) + *val = state->dac_value[chan->channel]; +return IIO_VAL_INT ; +case IIO_CHAN_INFO_SCALE : + *val =MCP4821_2X_GAIN_VREF_MV ; + *val2 = chan->scan_type.realbits; +return IIO_VAL_FRACTIONAL_LOG2 ; +default : +return -EINVAL ; + } +} + +static int mcp4821_write_raw(struct iio_dev *indio_dev, + struct iio_chan_spec const *chan, int val,
drivers/iio/dac/mcp4821.c line 139
Quoted 10 lines
drivers/iio/dac/mcp4821.c new file +10
-0+ int val2, long mask) +{ +struct mcp4821_state *state; +u16 write_val; +__be16 write_buffer; +int ret; +bool is_value_valid = val >=0 && val <BIT (chan->scan_type.realbits) && + val2 ==0 ; + +if (!is_value_valid)
Check the opposite and drop the local variable that adds nothing.
if ((val < 0) || (val >= BIT(chan_type).realbits)) || (val2 != 0))
or split it up into checks on val2 in one check then on val in the other.
drivers/iio/dac/mcp4821.c line 144
Quoted 5 lines
drivers/iio/dac/mcp4821.c new file +5
-0+return -EINVAL ; +if (mask !=IIO_CHAN_INFO_RAW ) +return -EINVAL ; + + state =iio_priv (indio_dev);
I'd do this at point of local variable declaration above.
struct mcp4821_state *state = iio_priv(indio_dev);
drivers/iio/dac/mcp4821.c line 148
Quoted 4 lines
drivers/iio/dac/mcp4821.c new file +4
-0+ write_val =MCP4821_ACTIVE_MODE | val << chan->scan_type.shift; +if (chan->channel) + write_val |=MCP4802_SECOND_CHAN ; +put_unaligned_be16 (write_val, &write_buffer);
It's aligned given it's a __be16
drivers/iio/dac/mcp4821.c line 159
Quoted 12 lines
drivers/iio/dac/mcp4821.c new file +11
-0+ ret =spi_write (state->spi, &write_buffer,sizeof (write_buffer)); +if (ret) { +dev_err (&state->spi->dev,"Failed to write to device: %d" , ret); +return ret; + } + +scoped_guard (mutex, &state->lock) + state->dac_value[chan->channel] = val; +return 0 ; +} +
Jonathan