From: Matti Vaittinen ROHM BD79111 is very similar to the BD79112. Both are ADC/GPIO devices, where pins can be configured to be either used as GPIO or ADC inputs. ADCs are 12-bit SAR ADCs and GPIOs support input/output but not being IRQ sources. The main difference is amount of channels/GPIOs. The BD79112 has 32 pins for this use, the BD79111 has 16. Furthermore, the BD79111 data-sheet promises 0.5 MSPS sample rate with maximum of 10 MHz SPI clock, whereas the BD79112 data-sheet mentioned 1MSPS rate with maximum of 20 MHz SPI clock. NOTE: In order to achieve those speeds one needs to use more optimized protocol compared to the one implemented in this driver. This driver retrieves samples using two 16-bit SPI transactions. The first transaction writes the channel to be sampled, and ignores data on MISO. Then the next 16-bit transaction carries the data. Achieving maximum sample-rate would require the driver to interpret the MISO data of each 16-bit transaction so, that when the channel-to-be-sampled was sent in transaction N, the data is read at transaction N+1 - which also already configures the channel to be sampled for transaction N+2. Supporting this would require the channel-to-be-read for the future read to be known when the current channel data is retrieved. For raw-reads this is not possible and for buffered reads it would make things a bit hairy. So, here we aim for simplicity instead of the peak performance. Signed-off-by: Matti Vaittinen --- drivers/iio/adc/Kconfig | 9 +-- drivers/iio/adc/rohm-bd79112.c | 113 +++++++++++++++++++++++++++------ 2 files changed, 98 insertions(+), 24 deletions(-) diff --git a/drivers/iio/adc/Kconfig b/drivers/iio/adc/Kconfig index a802d8ee4dd9..db9e0a0713e1 100644 --- a/drivers/iio/adc/Kconfig +++ b/drivers/iio/adc/Kconfig @@ -1516,14 +1516,15 @@ config RN5T618_ADC will be called rn5t618-adc. config ROHM_BD79112 - tristate "Rohm BD79112 ADC driver" + tristate "ROHM BD79112 and BD79111 ADC/GPIO driver" depends on SPI && GPIOLIB select REGMAP_SPI select IIO_ADC_HELPER help - Say yes here to build support for the ROHM BD79112 ADC. The - ROHM BD79112 is a 12-bit, 32-channel, SAR ADC. Analog inputs - can also be used for GPIO. + Say yes here to build support for the ROHM BD79112 and BD79111 + ADCs. Both of the ICs are 12-bit SAR ADCs. The ROHM BD79112 has + 32-channels, the BD79111 has 16. Analog inputs can also be used + for GPIO. config ROHM_BD79124 tristate "Rohm BD79124 ADC driver" diff --git a/drivers/iio/adc/rohm-bd79112.c b/drivers/iio/adc/rohm-bd79112.c index 2506fec7be84..604c6ccafb4f 100644 --- a/drivers/iio/adc/rohm-bd79112.c +++ b/drivers/iio/adc/rohm-bd79112.c @@ -1,6 +1,6 @@ // SPDX-License-Identifier: GPL-2.0-only /* - * ROHM ADC driver for BD79112 signal monitoring hub. + * ROHM ADC driver for BD79111 and BD79112 signal monitoring hubs. * Copyright (C) 2025, ROHM Semiconductor. * * SPI communication derived from ad7923.c and ti-ads7950.c @@ -26,7 +26,17 @@ #define BD79112_MAX_NUM_CHANNELS 32 +struct bd79112_data; + +struct bd7911x_chip_data { + const char *name; + const char * const *chan_names; + unsigned int num_channels; + int (*init_disable_gpios)(struct bd79112_data *data); +}; + struct bd79112_data { + const struct bd7911x_chip_data *cd; struct spi_device *spi; struct regmap *map; struct device *dev; @@ -369,7 +379,6 @@ static int bd79112_gpio_output(struct gpio_chip *gc, unsigned int offset, } static const struct gpio_chip bd79112_gpio_chip = { - .label = "bd79112-gpio", .get_direction = bd79112_gpio_dir_get, .direction_input = bd79112_gpio_input, .direction_output = bd79112_gpio_output, @@ -378,20 +387,20 @@ static const struct gpio_chip bd79112_gpio_chip = { .set_multiple = bd79112_gpio_set_multiple, .init_valid_mask = bd79112_gpio_init_valid_mask, .can_sleep = true, - .ngpio = 32, .base = -1, }; -static unsigned int bd79112_get_gpio_pins(const struct iio_chan_spec *cs, int num_channels) +static unsigned int bd79112_get_gpio_pins(const struct iio_chan_spec *cs, + int num_channels, int max_channels) { unsigned int i, gpio_channels; /* - * Let's initialize the mux config to say that all 32 channels are + * Let's initialize the mux config to say that all channels are * GPIOs. Then we can just loop through the iio_chan_spec and clear the * bits for found ADC channels. */ - gpio_channels = GENMASK(31, 0); + gpio_channels = GENMASK(max_channels - 1, 0); for (i = 0; i < num_channels; i++) gpio_channels &= ~BIT(cs[i].channel); @@ -410,6 +419,61 @@ static const char * const bd79112_chan_names[] = { "AGIO12B", "AGIO13B", "AGIO14B", "AGIO15B", /* 28 - 31 */ }; +static int bd79112_disable_gpios(struct bd79112_data *data) +{ + unsigned int i; + int ret; + + for (i = 0; i < BD79112_NUM_GPIO_EN_REGS; i++) { + ret = regmap_write(data->map, BD79112_FIRST_GPIO_EN_REG + i, 0); + if (ret) + return ret; + } + + return 0; +} + +/* The BD79111 only has the registers for pins 0 - 15 */ +static int bd79111_disable_gpios(struct bd79112_data *data) +{ + static const unsigned int regs[] = { + BD79112_REG_GPI_EN_A8_A15, BD79112_REG_GPI_EN_A0_A7, + BD79112_REG_GPO_EN_A8_A15, BD79112_REG_GPO_EN_A0_A7, + }; + unsigned int i; + int ret; + + for (i = 0; i < ARRAY_SIZE(regs); i++) { + ret = regmap_write(data->map, regs[i], 0); + if (ret) + return ret; + } + + return 0; +} + +/* The BD79111 channel names as in the data-sheet */ +static const char * const bd79111_chan_names[] = { + "AGIO0", "AGIO1", "AGIO2", "AGIO3", /* 0 - 3 */ + "AGIO4", "AGIO5", "AGIO6", "AGIO7", /* 4 - 7 */ + "AGIO8", "AGIO9", "AGIO10", "AGIO11", /* 8 - 11 */ + "AGIO12", "AGIO13", "AGIO14", "AGIO15", /* 12 - 15 */ +}; + +static const struct bd7911x_chip_data bd79111_chip_data = { + .name = "bd79111", + .chan_names = bd79111_chan_names, + .num_channels = 16, + .init_disable_gpios = bd79111_disable_gpios, +}; + +static const struct bd7911x_chip_data bd79112_chip_data = { + .name = "bd79112", + .chan_names = bd79112_chan_names, + .num_channels = 32, + .init_disable_gpios = bd79112_disable_gpios, +}; + static int bd79112_probe(struct spi_device *spi) { struct bd79112_data *data; @@ -425,6 +489,10 @@ static int bd79112_probe(struct spi_device *spi) return -ENOMEM; data = iio_priv(iio_dev); + data->cd = spi_get_device_match_data(spi); + if (!data->cd) + return -ENODEV; + data->spi = spi; data->dev = dev; data->map = devm_regmap_init(dev, NULL, data, &bd79112_regmap); @@ -462,7 +530,7 @@ static int bd79112_probe(struct spi_device *spi) "Failed to optimize SPI write message\n"); ret = devm_iio_adc_device_alloc_chaninfo_se(dev, &bd79112_chan_template, - BD79112_MAX_NUM_CHANNELS - 1, + data->cd->num_channels - 1, &cs); /* Register all pins as GPIOs if there are no ADC channels */ @@ -476,10 +544,10 @@ static int bd79112_probe(struct spi_device *spi) iio_dev->channels = cs; for (i = 0; i < iio_dev->num_channels; i++) - cs[i].datasheet_name = bd79112_chan_names[cs[i].channel]; + cs[i].datasheet_name = data->cd->chan_names[cs[i].channel]; iio_dev->info = &bd79112_info; - iio_dev->name = "bd79112"; + iio_dev->name = data->cd->name; iio_dev->modes = INDIO_DIRECT_MODE; /* @@ -488,12 +556,9 @@ static int bd79112_probe(struct spi_device *spi) * without having to worry about some pins being initially used for * GPIO. */ - for (i = 0; i < BD79112_NUM_GPIO_EN_REGS; i++) { - ret = regmap_write(data->map, BD79112_FIRST_GPIO_EN_REG + i, 0); - if (ret) - return dev_err_probe(dev, ret, - "Failed to initialize channels\n"); - } + ret = data->cd->init_disable_gpios(data); + if (ret) + return dev_err_probe(dev, ret, "Failed to initialize channels\n"); ret = devm_iio_device_register(data->dev, iio_dev); if (ret) @@ -501,14 +566,15 @@ static int bd79112_probe(struct spi_device *spi) register_gpios: gpio_pins = bd79112_get_gpio_pins(iio_dev->channels, - iio_dev->num_channels); + iio_dev->num_channels, + data->cd->num_channels); /* If all channels are reserved for ADC, then we're done. */ if (!gpio_pins) return 0; /* Default all the GPIO pins to GPI */ - for_each_set_bit(pin, &gpio_pins, BD79112_MAX_NUM_CHANNELS) { + for_each_set_bit(pin, &gpio_pins, data->cd->num_channels) { ret = bd79112_gpio_dir_set(data, pin, GPIO_LINE_DIRECTION_IN); if (ret) return dev_err_probe(dev, ret, @@ -518,18 +584,25 @@ static int bd79112_probe(struct spi_device *spi) data->gpio_valid_mask = gpio_pins; data->gc = bd79112_gpio_chip; data->gc.parent = dev; + data->gc.ngpio = data->cd->num_channels; + data->gc.label = devm_kasprintf(dev, GFP_KERNEL, "%s-gpio", + data->cd->name); + if (!data->gc.label) + return -ENOMEM; return devm_gpiochip_add_data(dev, &data->gc, data); } static const struct of_device_id bd79112_of_match[] = { - { .compatible = "rohm,bd79112" }, + { .compatible = "rohm,bd79111", .data = &bd79111_chip_data }, + { .compatible = "rohm,bd79112", .data = &bd79112_chip_data }, { } }; MODULE_DEVICE_TABLE(of, bd79112_of_match); static const struct spi_device_id bd79112_id[] = { - { .name = "bd79112" }, + { .name = "bd79111", .driver_data = (kernel_ulong_t)&bd79111_chip_data }, + { .name = "bd79112", .driver_data = (kernel_ulong_t)&bd79112_chip_data }, { } }; MODULE_DEVICE_TABLE(spi, bd79112_id); @@ -545,6 +618,6 @@ static struct spi_driver bd79112_driver = { module_spi_driver(bd79112_driver); MODULE_AUTHOR("Matti Vaittinen "); -MODULE_DESCRIPTION("Driver for ROHM BD79112 ADC/GPIO"); +MODULE_DESCRIPTION("Driver for ROHM BD79111 and BD79112 ADC/GPIO"); MODULE_LICENSE("GPL"); MODULE_IMPORT_NS("IIO_DRIVER"); -- 2.56.1