Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

iio: dac: Support ROHM BD79703 DAC

The ROHM BD79703 is a 6 channel digital to analog converter.

Based on the data-sheet examples the hardware would support setting the
DAC word without changing the actual output. The data-sheet is not too
specific on how the enabling the output of new voltage set by DAC
should be done - hence this is not implemented by the driver.

The BD79703 would also support two specific "PULL_DOWN" modes. These
aren't currently supported by the driver either.

Add a very basic support for controlling the channel outputs one-by-one.

Signed-off-by: Matti Vaittinen <mazziesaccount@gmail.com>
Link: https://patch.msgid.link/bc77d7b979ca28408a216f597082fcd94ec63be7.1734608215.git.mazziesaccount@gmail.com
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>

authored by

Matti Vaittinen and committed by
Jonathan Cameron
af6aca65 f6ed0ca1

+171
+8
drivers/iio/dac/Kconfig
··· 348 348 To compile this driver as a module choose M here: the module will be called 349 349 ad8801. 350 350 351 + config BD79703 352 + tristate "ROHM Semiconductor BD79703 DAC driver" 353 + depends on SPI 354 + select REGMAP_SPI 355 + help 356 + Say yes here to build support for ROHM Semiconductor BD79703 Digital 357 + to Analog Converter (DAC). 358 + 351 359 config CIO_DAC 352 360 tristate "Measurement Computing CIO-DAC IIO driver" 353 361 depends on X86 && (ISA_BUS || PC104)
+1
drivers/iio/dac/Makefile
··· 34 34 obj-$(CONFIG_AD8801) += ad8801.o 35 35 obj-$(CONFIG_AD9739A) += ad9739a.o 36 36 obj-$(CONFIG_ADI_AXI_DAC) += adi-axi-dac.o 37 + obj-$(CONFIG_BD79703) += rohm-bd79703.o 37 38 obj-$(CONFIG_CIO_DAC) += cio-dac.o 38 39 obj-$(CONFIG_DPOT_DAC) += dpot-dac.o 39 40 obj-$(CONFIG_DS4424) += ds4424.o
+162
drivers/iio/dac/rohm-bd79703.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-only 2 + /* 3 + * BD79703 ROHM Digital to Analog converter 4 + * 5 + * Copyright (c) 2024, ROHM Semiconductor. 6 + */ 7 + 8 + #include <linux/bits.h> 9 + #include <linux/device.h> 10 + #include <linux/module.h> 11 + #include <linux/regmap.h> 12 + #include <linux/regulator/consumer.h> 13 + #include <linux/spi/spi.h> 14 + #include <linux/iio/iio.h> 15 + 16 + #define BD79703_MAX_REGISTER 0xf 17 + #define BD79703_DAC_BITS 8 18 + #define BD79703_REG_OUT_ALL GENMASK(2, 0) 19 + 20 + /* 21 + * The BD79703 uses 12-bit SPI commands. First four bits (high bits) define 22 + * channel(s) which are operated on, and also the mode. The mode can be to set 23 + * a DAC word only, or set DAC word and output. The data-sheet is not very 24 + * specific on how a previously set DAC word can be 'taken in to use'. Thus 25 + * this driver only uses the 'set DAC and output it' -mode. 26 + * 27 + * The BD79703 latches last 12-bits when the chip-select is toggled. Thus we 28 + * can use 16-bit transfers which should be widely supported. To simplify this 29 + * further, we treat the last 8 bits as a value, and first 8 bits as an 30 + * address. This allows us to separate channels/mode by address and treat the 31 + * 8-bit register value as DAC word. The highest 4 bits of address will be 32 + * discarded when the transfer is latched. 33 + */ 34 + static const struct regmap_config bd79703_regmap_config = { 35 + .reg_bits = 8, 36 + .val_bits = 8, 37 + .max_register = BD79703_MAX_REGISTER, 38 + .cache_type = REGCACHE_RBTREE, 39 + }; 40 + 41 + struct bd79703_data { 42 + struct regmap *regmap; 43 + int vfs; 44 + }; 45 + 46 + static int bd79703_read_raw(struct iio_dev *idev, 47 + struct iio_chan_spec const *chan, int *val, 48 + int *val2, long mask) 49 + { 50 + struct bd79703_data *data = iio_priv(idev); 51 + 52 + if (mask != IIO_CHAN_INFO_SCALE) 53 + return -EINVAL; 54 + 55 + *val = data->vfs / 1000; 56 + *val2 = BD79703_DAC_BITS; 57 + 58 + return IIO_VAL_FRACTIONAL_LOG2; 59 + } 60 + 61 + static int bd79703_write_raw(struct iio_dev *idev, 62 + struct iio_chan_spec const *chan, int val, 63 + int val2, long mask) 64 + { 65 + struct bd79703_data *data = iio_priv(idev); 66 + 67 + if (val < 0 || val >= 1 << BD79703_DAC_BITS) 68 + return -EINVAL; 69 + 70 + return regmap_write(data->regmap, chan->channel + 1, val); 71 + }; 72 + 73 + static const struct iio_info bd79703_info = { 74 + .read_raw = bd79703_read_raw, 75 + .write_raw = bd79703_write_raw, 76 + }; 77 + 78 + #define BD79703_CHAN(_chan) { \ 79 + .type = IIO_VOLTAGE, \ 80 + .indexed = 1, \ 81 + .output = 1, \ 82 + .channel = (_chan), \ 83 + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 84 + .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ 85 + .address = (_chan), \ 86 + } 87 + 88 + static const struct iio_chan_spec bd79703_channels[] = { 89 + BD79703_CHAN(0), 90 + BD79703_CHAN(1), 91 + BD79703_CHAN(2), 92 + BD79703_CHAN(3), 93 + BD79703_CHAN(4), 94 + BD79703_CHAN(5), 95 + }; 96 + 97 + static int bd79703_probe(struct spi_device *spi) 98 + { 99 + struct device *dev = &spi->dev; 100 + struct bd79703_data *data; 101 + struct iio_dev *idev; 102 + int ret; 103 + 104 + idev = devm_iio_device_alloc(dev, sizeof(*data)); 105 + if (!idev) 106 + return -ENOMEM; 107 + 108 + data = iio_priv(idev); 109 + 110 + data->regmap = devm_regmap_init_spi(spi, &bd79703_regmap_config); 111 + if (IS_ERR(data->regmap)) 112 + return dev_err_probe(dev, PTR_ERR(data->regmap), 113 + "Failed to initialize Regmap\n"); 114 + 115 + ret = devm_regulator_get_enable(dev, "vcc"); 116 + if (ret) 117 + return dev_err_probe(dev, ret, "Failed to enable VCC\n"); 118 + 119 + ret = devm_regulator_get_enable_read_voltage(dev, "vfs"); 120 + if (ret < 0) 121 + return dev_err_probe(dev, ret, "Failed to get Vfs\n"); 122 + 123 + data->vfs = ret; 124 + idev->channels = bd79703_channels; 125 + idev->num_channels = ARRAY_SIZE(bd79703_channels); 126 + idev->modes = INDIO_DIRECT_MODE; 127 + idev->info = &bd79703_info; 128 + idev->name = "bd79703"; 129 + 130 + /* Initialize all to output zero */ 131 + ret = regmap_write(data->regmap, BD79703_REG_OUT_ALL, 0); 132 + if (ret) 133 + return ret; 134 + 135 + return devm_iio_device_register(dev, idev); 136 + } 137 + 138 + static const struct spi_device_id bd79703_id[] = { 139 + { "bd79703", }, 140 + { } 141 + }; 142 + MODULE_DEVICE_TABLE(spi, bd79703_id); 143 + 144 + static const struct of_device_id bd79703_of_match[] = { 145 + { .compatible = "rohm,bd79703", }, 146 + { } 147 + }; 148 + MODULE_DEVICE_TABLE(of, bd79703_of_match); 149 + 150 + static struct spi_driver bd79703_driver = { 151 + .driver = { 152 + .name = "bd79703", 153 + .of_match_table = bd79703_of_match, 154 + }, 155 + .probe = bd79703_probe, 156 + .id_table = bd79703_id, 157 + }; 158 + module_spi_driver(bd79703_driver); 159 + 160 + MODULE_AUTHOR("Matti Vaittinen <mazziesaccount@gmail.com>"); 161 + MODULE_DESCRIPTION("ROHM BD79703 DAC driver"); 162 + MODULE_LICENSE("GPL");