iio:adc: Add RZ/N1D ADC driver
Subsystems (3)
PATCH 1/1 iio:adc: Add RZ/N1D ADC driver
From: Tomasz Kazimierz Motyl <[email protected]>
This is ADC driver that can be found in the Renesas RZ/N1D SoC
https://www.renesas.com/us/en/document/man/rzn1d-group-rzn1s-group-rzn1l-group-users-manual-peripherals-0?language=en&r=1054561
ADC Core Features
- Up to 2 units
- Resolution 12-bit
- Sampling rate from 0.0625 MSPS to 1 MSPS
- Successive approximation
- Maximal conversion time 21 ADC_CLK
- Analog inputs 8 channels per core
(5 standard channels + 3 channels with sample/hold)
- Each channel has his own input trigger to start the conversion, the triggers are managed by the ADC Controller
- Power down mode
- ADC clock frequency from 4 MHz to 20 MHz
--- .../devicetree/bindings/iio/adc/rzn1-adc.txt | 64 ++++ drivers/iio/adc/Kconfig | 9 + drivers/iio/adc/Makefile | 1 + drivers/iio/adc/rzn1-adc.c | 325 ++++++++++++++++++ 4 files changed, 399 insertions(+) create mode 100644 Documentation/devicetree/bindings/iio/adc/rzn1-adc.txt create mode 100644 drivers/iio/adc/rzn1-adc.c
Documentation/devicetree/bindings/iio/adc/rzn1-adc.txt new file +64 -0
@@ -0,0 +1,64 @@ +ADC for the Renesas RZ/N1D (R9A06G032). +Specifications on the converters can be found at: +https://www.renesas.com/us/en/document/man/rzn1d-group-rzn1s-group-rzn1l-group-users-manual-peripherals-0?language=en&r=1054561 + +Required properties: + +- compatible: Must be "iio,rzn1-adc", "rzn1-adc" +- reg: Offset and length of the register set for the device +- clocks: phandle to the master clock (all_adc) + see: Documentation/devicetree/bindings/clock/clock-bindings.txt +- clock-names: Must be "rzn1_adc_clk". + +Optional properties: +channels: +label: A channel label that is used as channel's extended name part i.e.: + A label = "temp1" for channel1@1 results in a following entry in the sysfs: + /sys/bus/iio/devices/iio:device0/in_voltage1_temp1_raw . + If the label would not be specified the resulting sysfs entry would be as follows: + /sys/bus/iio/devices/iio:device0/in_voltage1_raw . + + +Whenever specified incorrectly or unspecified an entry gets skipped and driver falls back to hard-coded defaults. + +Example: + + rzn1_adc: rzn1-adc@0x40065000 { + #address-cells = <1>; + #size-cells = <1>; + compatible = "renesas,r9a06g032-adc", "iio,rzn1-adc", "rzn1-adc"; + reg = <0x40065000 0x1000>; + clocks = <&sysctrl R9A06G032_CLK_ADC>; + clock-names = "rzn1_adc_clk"; + status = "okay"; + + channels { + #address-cells = <1>; + #size-cells = <0>; + + chan0@0 { /* Channel 0 placeholder */ + reg = <0>; + label = "adc1_in0"; + }; + + chan1@1 { /* Channel 1 placeholder */ + reg = <1>; + label = "adc1_in1"; + }; + + chan2@2 { /* Channel 2 placeholder */ + reg = <2>; + label = "adc1_in2"; + }; + + chan3@3 { /* Channel 3 placeholder */ + reg = <3>; + label = "adc1_in3"; + }; + + chan4@4 { /* Channel 4 placeholder */ + reg = <4>; + label = "adc1_in4"; + }; + }; /* channels */ + }; /* rzn1-adc */
drivers/iio/adc/Kconfig +9 -0
@@ -746,6 +746,15 @@ config ROCKCHIP_SARADC To compile this driver as a module, choose M here: the module will be called rockchip_saradc. +config RZN1_ADC + tristate "Renesas ADC driver" + help + Say yes here to build support for the ADCs found in SoCs from + Renesas. + + To compile this driver as a module, choose M here: the + module will be called rzn1_adc. + config SC27XX_ADC tristate "Spreadtrum SC27xx series PMICs ADC" depends on MFD_SC27XX_PMIC || COMPILE_TEST
drivers/iio/adc/Makefile +1 -0
@@ -70,6 +70,7 @@ obj-$(CONFIG_QCOM_SPMI_VADC) += qcom-spmi-vadc.o obj-$(CONFIG_QCOM_PM8XXX_XOADC) += qcom-pm8xxx-xoadc.o obj-$(CONFIG_RCAR_GYRO_ADC) += rcar-gyroadc.o obj-$(CONFIG_ROCKCHIP_SARADC) += rockchip_saradc.o +obj-$(CONFIG_RZN1_ADC) += rzn1-adc.o obj-$(CONFIG_SC27XX_ADC) += sc27xx_adc.o obj-$(CONFIG_SPEAR_ADC) += spear_adc.o obj-$(CONFIG_STX104) += stx104.o
drivers/iio/adc/rzn1-adc.c new file +325 -0
325 lines not on this page · raw message
Signature · 1 line
2.30.1
Quoted 3 lines
This is ADC driver that can be found in the Renesas RZ/N1D SoC
https://www.renesas.com/us/en/document/man/rzn1d-group-rzn1s-group-rzn1l-group-users-manual-peripherals-0?language=en&r=1054561
some comments below; some are purely matter-of-taste and can be ignored :)
Quoted 39 lines
ADC Core Features
- Up to 2 units
- Resolution 12-bit
- Sampling rate from 0.0625 MSPS to 1 MSPS
- Successive approximation
- Maximal conversion time 21ADC_CLK
- Analog inputs 8 channels per core
(5 standard channels + 3 channels with sample/hold)
- Each channel has his own input trigger to start the conversion, the triggers are managed by the ADC Controller
- Power down mode
- ADC clock frequency from 4 MHz to 20 MHzSigned-off-by Tomasz Kazimierz Motyl <[email protected]>Signed-off-by Frederic Loreaud <[email protected]>Signed-off-by Pierre Castellan <[email protected]>--- .../devicetree/bindings/iio/adc/rzn1-adc.txt | 64 ++++ drivers/iio/adc/Kconfig | 9 + drivers/iio/adc/Makefile | 1 + drivers/iio/adc/rzn1-adc.c | 325 ++++++++++++++++++ 4 files changed, 399 insertions(+) create mode 100644 Documentation/devicetree/bindings/iio/adc/rzn1-adc.txt create mode 100644 drivers/iio/adc/rzn1-adc.cDocumentation/devicetree/bindings/iio/adc/rzn1-adc.txt new file +8
-0@@ -0,0 +1,64 @@ +ADC for the Renesas RZ/N1D (R9A06G032). +Specifications on the converters can be found at: +https://www.renesas.com/us/en/document/man/rzn1d-group-rzn1s-group-rzn1l-group-users-manual-peripherals-0?language=en&r=1054561 + +Required properties: + +- compatible: Must be "iio,rzn1-adc", "rzn1-adc" +- reg: Offset and length of the register set for the device
Documentation/devicetree/bindings/iio/adc/rzn1-adc.txt line 18
Quoted 10 lines
Documentation/devicetree/bindings/iio/adc/rzn1-adc.txt new file +10
-0+- clocks: phandle to the master clock (all_adc) + see: Documentation/devicetree/bindings/clock/clock-bindings.txt +- clock-names: Must be "rzn1_adc_clk". + +Optional properties: +channels: +label: A channel label that is used as channel's extended name part i.e.: + A label = "temp1" for channel1@1 results in a following entry in the sysfs: + /sys/bus/iio/devices/iio:device0/in_voltage1_temp1_raw . + If the label would not be specified the resulting sysfs entry would be as follows:
maybe: If the label is not specified, the results sysfs entry will be as follows:
Documentation/devicetree/bindings/iio/adc/rzn1-adc.txt line 22
Quoted 4 lines
Documentation/devicetree/bindings/iio/adc/rzn1-adc.txt new file +4
-0+ /sys/bus/iio/devices/iio:device0/in_voltage1_raw . + + +Whenever specified incorrectly or unspecified an entry gets skipped and driver falls back to hard-coded defaults.
maybe comma after 'unspecified'
Quoted 115 lines
Documentation/devicetree/bindings/iio/adc/rzn1-adc.txt new file +42
-0+ +Example: + + rzn1_adc: rzn1-adc@0x40065000 { + #address-cells = <1>; + #size-cells = <1>; + compatible = "renesas,r9a06g032-adc", "iio,rzn1-adc", "rzn1-adc"; + reg = <0x40065000 0x1000>; + clocks = <&sysctrl R9A06G032_CLK_ADC>; + clock-names = "rzn1_adc_clk"; + status = "okay"; + + channels { + #address-cells = <1>; + #size-cells = <0>; + + chan0@0 { /* Channel 0 placeholder */ + reg = <0>; + label = "adc1_in0"; + }; + + chan1@1 { /* Channel 1 placeholder */ + reg = <1>; + label = "adc1_in1"; + }; + + chan2@2 { /* Channel 2 placeholder */ + reg = <2>; + label = "adc1_in2"; + }; + + chan3@3 { /* Channel 3 placeholder */ + reg = <3>; + label = "adc1_in3"; + }; + + chan4@4 { /* Channel 4 placeholder */ + reg = <4>; + label = "adc1_in4"; + }; + }; /* channels */ + }; /* rzn1-adc */drivers/iio/adc/Kconfig +9
-0@@ -746,6 +746,15 @@ config ROCKCHIP_SARADC To compile this driver as a module, choose M here: the module will be called rockchip_saradc. +config RZN1_ADC + tristate "Renesas ADC driver" + help + Say yes here to build support for the ADCs found in SoCs from + Renesas. + + To compile this driver as a module, choose M here: the + module will be called rzn1_adc. + config SC27XX_ADC tristate "Spreadtrum SC27xx series PMICs ADC" depends on MFD_SC27XX_PMIC || COMPILE_TESTdrivers/iio/adc/Makefile +1
-0@@ -70,6 +70,7 @@ obj-$(CONFIG_QCOM_SPMI_VADC) += qcom-spmi-vadc.o obj-$(CONFIG_QCOM_PM8XXX_XOADC) += qcom-pm8xxx-xoadc.o obj-$(CONFIG_RCAR_GYRO_ADC) += rcar-gyroadc.o obj-$(CONFIG_ROCKCHIP_SARADC) += rockchip_saradc.o +obj-$(CONFIG_RZN1_ADC) += rzn1-adc.o obj-$(CONFIG_SC27XX_ADC) += sc27xx_adc.o obj-$(CONFIG_SPEAR_ADC) += spear_adc.o obj-$(CONFIG_STX104) += stx104.odrivers/iio/adc/rzn1-adc.c new file +35
-0@@ -0,0 +1,325 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* Driver for Renesas RZN1 SoC built-in ADC +* +* Authors: +* Tomasz Kazimierz Motyl +* Frédéric Loreaud +* Pierre Castellan +* +* Copyright (C) 2021 Schneider-Electric +*/ + +#include <linux/kernel.h> +#include <linux/slab.h> +#include <linux/module.h> +#include <linux/platform_device.h> +#include <linux/interrupt.h> +#include <linux/mutex.h> +#include <linux/completion.h> +#include <linux/iio/iio.h> +#include <linux/iio/machine.h> +#include <linux/iio/driver.h> +#include <linux/io.h> +#include <linux/clk.h> +#include <linux/firmware/lces_monitor.h> +#include <linux/delay.h> +#include <linux/bits.h> + +static const uint32_t RZN1_ADC_SET_FORCE_REG_OFFSET =13 *sizeof (uint32_t );// force conversion register + +static const uint32_t RZN1_ADC_CONFIG_REG_OFFSET =16 *sizeof (uint32_t );// configuration register +static const uint32_t RZN1_ADC_CONFIG_POWER_DOWN_BIT =BIT (3 );// configuration register's power down bit + +static const uint32_t RZN1_ADC_VIRTUAL_CHANNNELS_REGS_OFFSET =48 *sizeof (uint32_t );// virtual channels 0..15 control registers +static const uint32_t RZN1_ADC_VIRTUAL_CHANNEL_ADC1_SELECTION_MASK =GENMASK (2 ,0 );// control registers' virtual channels' bits +static const uint32_t RZN1_ADC_VIRTUAL_CHANNEL_ADC1_ENABLE_BIT =BIT (15 );// control registers' enable bit
drivers/iio/adc/rzn1-adc.c line 45
Quoted 10 lines
drivers/iio/adc/rzn1-adc.c new file +10
-0+ +static const uint32_t RZN1_ADC_VIRTUAL_CHANNEL_ADC1_CONVERSION_DATA_REGS_OFFSET =64 *sizeof (uint32_t );// ADC 1 virtual channels conversion data register + +static const uint32_t RZN1_ADC_READ_DATA_VALUE_MASK =GENMASK (11 ,0 );// read data register's bits +static const uint32_t RZN1_ADC_READ_DATA_UPDATE_BIT =BIT (31 );// read data register's update bit + +#define RZN1_ADC_CHANNEL (index, bits, _shift, _info_mask_sep, name) { \ + .type = IIO_VOLTAGE, \ + .indexed = 1, \ + .channel = index, \
maybe also parenthesis, (index)
Quoted 47 lines
drivers/iio/adc/rzn1-adc.c new file +47
-0+ .info_mask_separate = _info_mask_sep, \ + .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ + .scan_type = { \ + .sign = 'u', \ + .realbits = (bits), \ + .storagebits = 16, \ + .shift = (_shift), \ + .endianness = IIO_BE, \ + }, \ + .extend_name = name, \ +} + +#define RZN1_ADC_NUM_CHANNELS 8 // RZN1 ADC has 8 channels + +static struct iio_chan_spec rzn1_adc_channels[RZN1_ADC_NUM_CHANNELS ] = { +RZN1_ADC_CHANNEL (0 ,12 ,0 ,BIT (IIO_CHAN_INFO_RAW ),NULL ), +RZN1_ADC_CHANNEL (1 ,12 ,0 ,BIT (IIO_CHAN_INFO_RAW ),NULL ), +RZN1_ADC_CHANNEL (2 ,12 ,0 ,BIT (IIO_CHAN_INFO_RAW ),NULL ), +RZN1_ADC_CHANNEL (3 ,12 ,0 ,BIT (IIO_CHAN_INFO_RAW ),NULL ), +RZN1_ADC_CHANNEL (4 ,12 ,0 ,BIT (IIO_CHAN_INFO_RAW ),NULL ), +RZN1_ADC_CHANNEL (5 ,12 ,0 ,BIT (IIO_CHAN_INFO_RAW ),NULL ), +RZN1_ADC_CHANNEL (6 ,12 ,0 ,BIT (IIO_CHAN_INFO_RAW ),NULL ), +RZN1_ADC_CHANNEL (7 ,12 ,0 ,BIT (IIO_CHAN_INFO_RAW ),NULL ) +}; + +// Device's private data +struct rzn1_adc { +struct device *dev; + +struct mutex lock; +struct completion complete; +void __iomem *registers; +resource_size_t phys_base; +struct clk *clk; +}; + +static inline uint32_t rzn1_adc_read32 (struct rzn1_adc *const adc,const uint32_t reg_off) +{ +#if defined(CONFIG_LCES_SECURE_MONITOR ) +return __monitor_readl (adc->phys_base + reg_off); +#else +void __iomem *addr = adc->registers + reg_off; + +return ioread32 (addr); +#endif +} +
drivers/iio/adc/rzn1-adc.c line 102
Quoted 10 lines
drivers/iio/adc/rzn1-adc.c new file +10
-0+static inline void rzn1_adc_write32 (struct rzn1_adc *const adc,const uint32_t reg_off,const uint32_t val) +{ +#if defined(CONFIG_LCES_SECURE_MONITOR ) +__monitor_masked_writel (val, adc->phys_base + reg_off, ~0UL ); +#else +iowrite32 (val, adc->registers + reg_off); +#endif +} + +static bool interrupt_status (struct rzn1_adc *const adc,const int virtual_channel)
rzn1_adc_ prefix for consistency please
Quoted 18 lines
drivers/iio/adc/rzn1-adc.c new file +18
-0+{ +bool ret = false; + +if ((virtual_channel >=0 ) && (virtual_channel <RZN1_ADC_NUM_CHANNELS )) { +// interrupt 0 status register has a 0 offset in register table +uint32_t status =rzn1_adc_read32 (adc,0 ); + +if (status &BIT (virtual_channel)) + ret = true; + } + +return ret; +} + +static int rzn1_adc_read_raw (struct iio_dev *indio_dev,struct iio_chan_spec const *chan,int *val,int *val2,long mask) +{ +int ret =IIO_VAL_INT ; +
drivers/iio/adc/rzn1-adc.c line 130
Quoted 10 lines
drivers/iio/adc/rzn1-adc.c new file +10
-0+struct rzn1_adc *adc =iio_priv (indio_dev); +int virtual_channel = chan->channel; +uint32_t virtual_channel_control_offset =RZN1_ADC_VIRTUAL_CHANNNELS_REGS_OFFSET +4 * virtual_channel; +uint32_t virtual_channel_control =RZN1_ADC_VIRTUAL_CHANNEL_ADC1_ENABLE_BIT | (RZN1_ADC_VIRTUAL_CHANNEL_ADC1_SELECTION_MASK & virtual_channel); +uint32_t read_data =0 ; + + (void )mask; + (void )val2; + +if ((virtual_channel >=0 ) && (virtual_channel <RZN1_ADC_NUM_CHANNELS )) {
not sure if the channel number can be invalid, maybe the IIO framework guarantees it is in range
I think it is generally more readable to check for error condition and error out, like so:
if (sth_wrong) {
return -EINVAL;
}sth_right;
Quoted 20 lines
drivers/iio/adc/rzn1-adc.c new file +20
-0+mutex_lock (&adc->lock); + +// disable power down mode, disable DMA and Sample & Hold mode +rzn1_adc_write32 (adc,RZN1_ADC_CONFIG_REG_OFFSET ,0x00000000 ); + +// map virtual to physical channels 1:1 +rzn1_adc_write32 (adc, virtual_channel_control_offset, virtual_channel_control); + +// force conversion on vc[chan_idx] channel +rzn1_adc_write32 (adc,RZN1_ADC_SET_FORCE_REG_OFFSET ,BIT (virtual_channel)); + +// Wait for maximum conversion duration of 21 ADC clock periods +// ADC clock frequency is 20 MHz hence period is 50 ns +// Add one more period for safety +ndelay (22 *50 ); + + read_data =rzn1_adc_read32 (adc,RZN1_ADC_VIRTUAL_CHANNEL_ADC1_CONVERSION_DATA_REGS_OFFSET +4 * virtual_channel); + +if ((read_data &RZN1_ADC_READ_DATA_UPDATE_BIT ) + && (interrupt_status (adc, virtual_channel) == true))
drivers/iio/adc/rzn1-adc.c line 160
Quoted 10 lines
drivers/iio/adc/rzn1-adc.c new file +10
-0+ *val = read_data &RZN1_ADC_READ_DATA_VALUE_MASK ; + else + ret = -EINVAL ;// error reading input value + +// enable power down mode, keep DMA and Sample & Hold mode disabled +rzn1_adc_write32 (adc,RZN1_ADC_CONFIG_REG_OFFSET ,RZN1_ADC_CONFIG_POWER_DOWN_BIT ); + +mutex_unlock (&adc->lock); + }else { +dev_err (adc->dev,"virtual channel index (%i) is greater than 16\n" , virtual_channel);
why 16? _NUM_CHANNELS is 8?
Quoted 4 lines
drivers/iio/adc/rzn1-adc.c new file +4
-0+ } + +return ret; +}
drivers/iio/adc/rzn1-adc.c line 174
Quoted 10 lines
drivers/iio/adc/rzn1-adc.c new file +10
-0+ +static const struct iio_info rzn1_adc_info = { + .read_raw = &rzn1_adc_read_raw +}; + +static int rzn1_adc_parse_channels_of (struct rzn1_adc *adc,struct device_node *dn,const int num_channels) +{ +int ret =0 ; + +struct device_node *channels =NULL ;
no need to initialize
drivers/iio/adc/rzn1-adc.c line 175
Quoted 1 line
drivers/iio/adc/rzn1-adc.c new file +1
-0+struct device_node *channel =NULL ;
put in local scope?
drivers/iio/adc/rzn1-adc.c line 180
Quoted 5 lines
drivers/iio/adc/rzn1-adc.c new file +5
-0+ + channels =of_get_child_by_name (dn,"channels" ); +if (channels ==NULL ) { +dev_err (adc->dev,"no channels child node found\n" ); + ret = -EINVAL ;
maybe just return -EINVAL here?
Quoted 16 lines
drivers/iio/adc/rzn1-adc.c new file +16
-0+ }else { +for_each_available_child_of_node (channels, channel) { +// Stop parsing channels if any error occured +if (!ret) { +uint32_t reg =0 ; + + ret =of_property_read_u32 (channel,"reg" , ®); +if (ret) { +dev_err (adc->dev,"no reg child node found\n" ); + }else { +if (reg >= num_channels) { +dev_err (adc->dev,"wrong reg child node value %i\n" , reg); + ret = -EINVAL ; + }else { +struct iio_chan_spec *chanp = &rzn1_adc_channels[reg]; + chanp->extend_name =of_get_property (channel,"label" ,NULL );
drivers/iio/adc/rzn1-adc.c line 206
Quoted 10 lines
drivers/iio/adc/rzn1-adc.c new file +10
-0+ } + } + } + } + } + +return ret; +} + +static int setup_channel_names (struct rzn1_adc *adc,struct iio_dev *const indio_dev)
rzn1_adc_ prefix?
drivers/iio/adc/rzn1-adc.c line 208
Quoted 2 lines
drivers/iio/adc/rzn1-adc.c new file +2
-0+{ +//struct iio_chan_spec rzn1_adc_channels
drop comment
drivers/iio/adc/rzn1-adc.c line 210
Quoted 2 lines
drivers/iio/adc/rzn1-adc.c new file +2
-0+struct device_node *dn = indio_dev->dev.of_node; +int err =rzn1_adc_parse_channels_of (adc, dn, indio_dev->num_channels);
maybe err -> ret?
drivers/iio/adc/rzn1-adc.c line 221
Quoted 11 lines
drivers/iio/adc/rzn1-adc.c new file +11
-0+ +if (err <0 ) +dev_warn (adc->dev,"unable to parse channels!\n" ); + +return err; +} + +static int rzn1_adc_probe (struct platform_device *pdev) +{ +struct device *dev = &pdev->dev; +struct iio_dev *indio_dev =NULL ;
no need to initialize
drivers/iio/adc/rzn1-adc.c line 223
Quoted 2 lines
drivers/iio/adc/rzn1-adc.c new file +2
-0+struct rzn1_adc *adc =NULL ; +struct resource *res =NULL ;
put in local scope, init needed?
drivers/iio/adc/rzn1-adc.c line 230
Quoted 7 lines
drivers/iio/adc/rzn1-adc.c new file +7
-0+int ret =0 ; + + indio_dev =devm_iio_device_alloc (dev,sizeof (*adc)); + +if (indio_dev ==NULL ) { +dev_err (&pdev->dev,"Failed to allocate memory for an IIO device!\n" ); + ret = -ENOMEM ;
just return -ENOMEM?
Quoted 38 lines
drivers/iio/adc/rzn1-adc.c new file +38
-0+ }else { +platform_set_drvdata (pdev, indio_dev); + + adc =iio_priv (indio_dev); + + res =platform_get_resource (pdev,IORESOURCE_MEM ,0 ); +// initilize private physical address for RZN1 secure monitor calls + adc->phys_base = res->start; + adc->registers =devm_ioremap_resource (dev, res); + +if (IS_ERR (adc->registers)) { +dev_err (dev,"Unable to acquire memory mapping for the registers!\n" ); + ret =PTR_ERR (adc->registers); + }else { +platform_set_drvdata (pdev, indio_dev); + adc->dev = dev; + +// Enabling clock for the ADC + adc->clk =devm_clk_get (&pdev->dev,"rzn1_adc_clk" ); + +if (IS_ERR (adc->clk)) { +dev_err (dev,"Failed to get the clock!\n" ); + ret =PTR_ERR (adc->clk); + }else { + ret =clk_prepare_enable (adc->clk); +if (ret) { +dev_err (dev,"Could not prepare or enable the clock!\n" ); + }else { +dev_info (dev,"ADC clock rate: %lu Hz\n" ,clk_get_rate (adc->clk)); + +mutex_init (&adc->lock); +init_completion (&adc->complete); + + indio_dev->name =dev_name (dev); + indio_dev->dev.parent = dev; + indio_dev->dev.of_node = pdev->dev.of_node; + indio_dev->info = &rzn1_adc_info; + indio_dev->modes =INDIO_DIRECT_MODE ;
drivers/iio/adc/rzn1-adc.c line 278
Quoted 10 lines
drivers/iio/adc/rzn1-adc.c new file +10
-0+ indio_dev->channels = rzn1_adc_channels; + indio_dev->num_channels =ARRAY_SIZE (rzn1_adc_channels); + +if (setup_channel_names (adc, indio_dev) <0 ) +dev_warn (dev,"Invalid channels information - using defaults.\n" ); + + ret =iio_device_register (indio_dev); +if (ret) { +dev_err (dev,"Failed to register IIO device %s': %d\n" , indio_dev->name, ret); + }
avoid nested blocks
shouldn't the clock be disabled on failure?
Quoted 48 lines
drivers/iio/adc/rzn1-adc.c new file +47
-0+ } + } + } + } + +return ret; +} + +static int rzn1_adc_remove (struct platform_device *pdev) +{ +struct iio_dev *indio_dev =platform_get_drvdata (pdev); + +iio_device_unregister (indio_dev); +iio_map_array_unregister (indio_dev); + +return 0 ; +} + +static const struct platform_device_id rzn1_adc_ids[] = { + { .name ="rzn1-adc" , }, + { }, +}; + +MODULE_DEVICE_TABLE (platform, rzn1_adc_ids); + +static const struct of_device_id rzn1_adc_dt_ids[] = { + { .compatible ="rzn1-adc" , }, + { }, +}; + +MODULE_DEVICE_TABLE (of, rzn1_adc_dt_ids); + +static struct platform_driver rzn1_adc_driver = { + .driver = { + .name ="rzn1-adc" , + .of_match_table =of_match_ptr (rzn1_adc_dt_ids), + }, + .probe = rzn1_adc_probe, + .remove = rzn1_adc_remove, + .id_table = rzn1_adc_ids, +}; + +module_platform_driver (rzn1_adc_driver); + +MODULE_DESCRIPTION ("RZN1 ADC Driver for LCES2" ); +MODULE_AUTHOR ("Tomasz Kazimierz Motyl <[email protected]>" ); +MODULE_LICENSE ("GPL" );
Signature · 2 lines
Peter Meerwald-Stadler Mobile: +43 664 24 44 418
Jonathan Cameron
maintainerQuoted 4 lines · On Fri, 5 Mar 2021 12:35:52 +0100 (CET) Peter Meerwald-Stadler…
Quoted 3 lines
This is ADC driver that can be found in the Renesas RZ/N1D SoC
https://www.renesas.com/us/en/document/man/rzn1d-group-rzn1s-group-rzn1l-group-users-manual-peripherals-0?language=en&r=1054561some comments below; some are purely matter-of-taste and can be ignored :)
Thanks Peter.
I'll review on top of yours so hopefully no overlap.
Jonathan
Quoted 26 lines
Quoted 26 lines
ADC Core Features
- Up to 2 units
- Resolution 12-bit
- Sampling rate from 0.0625 MSPS to 1 MSPS
- Successive approximation
- Maximal conversion time 21ADC_CLK
- Analog inputs 8 channels per core
(5 standard channels + 3 channels with sample/hold)
- Each channel has his own input trigger to start the conversion, the triggers are managed by the ADC Controller
- Power down mode
- ADC clock frequency from 4 MHz to 20 MHzSigned-off-by Tomasz Kazimierz Motyl <[email protected]>Signed-off-by Frederic Loreaud <[email protected]>Signed-off-by Pierre Castellan <[email protected]>--- .../devicetree/bindings/iio/adc/rzn1-adc.txt | 64 ++++ drivers/iio/adc/Kconfig | 9 + drivers/iio/adc/Makefile | 1 + drivers/iio/adc/rzn1-adc.c | 325 ++++++++++++++++++ 4 files changed, 399 insertions(+) create mode 100644 Documentation/devicetree/bindings/iio/adc/rzn1-adc.txt create mode 100644 drivers/iio/adc/rzn1-adc.c
Quoted 5 lines
Documentation/devicetree/bindings/iio/adc/rzn1-adc.txt
Quoted 5 lines
Documentation/devicetree/bindings/iio/adc/rzn1-adc.txt new file +0
-0
yaml
No new bindings accepted in txt (certainly for IIO, but probably more general by now).
Binding also needs to be in a separate patch and cc'd to device tree maintainers and list.
Quoted 6 lines
Quoted 6 lines
Documentation/devicetree/bindings/iio/adc/rzn1-adc.txt new file +5
-0@@ -0,0 +1,64 @@ +ADC for the Renesas RZ/N1D (R9A06G032). +Specifications on the converters can be found at: +https://www.renesas.com/us/en/document/man/rzn1d-group-rzn1s-group-rzn1l-group-users-manual-peripherals-0?language=en&r=1054561 + +Required properties:
Quoted 8 lines
Documentation/devicetree/bindings/iio/adc/rzn1-adc.txt line 13
Quoted 8 lines
Documentation/devicetree/bindings/iio/adc/rzn1-adc.txt new file +8
-0+ +- compatible: Must be "iio,rzn1-adc", "rzn1-adc" +- reg: Offset and length of the register set for the device +- clocks: phandle to the master clock (all_adc) + see: Documentation/devicetree/bindings/clock/clock-bindings.txt +- clock-names: Must be "rzn1_adc_clk". + +Optional properties:
If optional, you shouldn't print a message if it is not there.
Quoted 7 lines
Documentation/devicetree/bindings/iio/adc/rzn1-adc.txt line 18
Quoted 5 lines
Documentation/devicetree/bindings/iio/adc/rzn1-adc.txt new file +5
-0+channels: +label: A channel label that is used as channel's extended name part i.e.: + A label = "temp1" for channel1@1 results in a following entry in the sysfs: + /sys/bus/iio/devices/iio:device0/in_voltage1_temp1_raw . + If the label would not be specified the resulting sysfs entry would be as follows:maybe: If the label is not specified, the results sysfs entry will be as follows:
given we now havein_voltage1_label well defined via the read_label() callback and that deals with the mess around extend_name and having to dynamically generate the chan_spec array, just use that instead.
It's a standard binding in Documentation/devicetree/bindings/iio/adc/adc.yaml
As a side note, bindings don't describe linux behaviour (used across lots of different software even if they do still hid in the kernel tree) so drop all this description anyway.
Quoted 58 lines
Documentation/devicetree/bindings/iio/adc/rzn1-adc.txt line 22
Quoted 5 lines
Documentation/devicetree/bindings/iio/adc/rzn1-adc.txt new file +4
-0+ /sys/bus/iio/devices/iio:device0/in_voltage1_raw . + + +Whenever specified incorrectly or unspecified an entry gets skipped and driver falls back to hard-coded defaults.maybe comma after 'unspecified'
Quoted 50 lines
Documentation/devicetree/bindings/iio/adc/rzn1-adc.txt new file +42
-0+ +Example: + + rzn1_adc: rzn1-adc@0x40065000 { + #address-cells = <1>; + #size-cells = <1>; + compatible = "renesas,r9a06g032-adc", "iio,rzn1-adc", "rzn1-adc"; + reg = <0x40065000 0x1000>; + clocks = <&sysctrl R9A06G032_CLK_ADC>; + clock-names = "rzn1_adc_clk"; + status = "okay"; + + channels { + #address-cells = <1>; + #size-cells = <0>; + + chan0@0 { /* Channel 0 placeholder */ + reg = <0>; + label = "adc1_in0"; + }; + + chan1@1 { /* Channel 1 placeholder */ + reg = <1>; + label = "adc1_in1"; + }; + + chan2@2 { /* Channel 2 placeholder */ + reg = <2>; + label = "adc1_in2"; + }; + + chan3@3 { /* Channel 3 placeholder */ + reg = <3>; + label = "adc1_in3"; + }; + + chan4@4 { /* Channel 4 placeholder */ + reg = <4>; + label = "adc1_in4"; + }; + }; /* channels */ + }; /* rzn1-adc */drivers/iio/adc/Kconfig +0
-0@@ -746,6 +746,15 @@ config ROCKCHIP_SARADC To compile this driver as a module, choose M here: the module will be called rockchip_saradc.
Quoted 2 lines
drivers/iio/adc/Kconfig config ROCKCHIP_SARADC · line 750
Quoted 2 lines
drivers/iio/adc/Kconfig +2
-0+config RZN1_ADC + tristate "Renesas ADC driver"
Depends on SOMETHING_RENESAS || COMILE_TEST
would normally be the case to avoid this accidentally turning up in the distro on my x86 laptop.
Quoted 48 lines
Quoted 48 lines
drivers/iio/adc/Kconfig +7
-0+ help + Say yes here to build support for the ADCs found in SoCs from + Renesas. + + To compile this driver as a module, choose M here: the + module will be called rzn1_adc. + config SC27XX_ADC tristate "Spreadtrum SC27xx series PMICs ADC" depends on MFD_SC27XX_PMIC || COMPILE_TESTdrivers/iio/adc/Makefile +1
-0@@ -70,6 +70,7 @@ obj-$(CONFIG_QCOM_SPMI_VADC) += qcom-spmi-vadc.o obj-$(CONFIG_QCOM_PM8XXX_XOADC) += qcom-pm8xxx-xoadc.o obj-$(CONFIG_RCAR_GYRO_ADC) += rcar-gyroadc.o obj-$(CONFIG_ROCKCHIP_SARADC) += rockchip_saradc.o +obj-$(CONFIG_RZN1_ADC) += rzn1-adc.o obj-$(CONFIG_SC27XX_ADC) += sc27xx_adc.o obj-$(CONFIG_SPEAR_ADC) += spear_adc.o obj-$(CONFIG_STX104) += stx104.odrivers/iio/adc/rzn1-adc.c new file +20
-0@@ -0,0 +1,325 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* Driver for Renesas RZN1 SoC built-in ADC +* +* Authors: +* Tomasz Kazimierz Motyl +* Frédéric Loreaud +* Pierre Castellan +* +* Copyright (C) 2021 Schneider-Electric +*/ + +#include <linux/kernel.h> +#include <linux/slab.h> +#include <linux/module.h> +#include <linux/platform_device.h> +#include <linux/interrupt.h> +#include <linux/mutex.h> +#include <linux/completion.h> +#include <linux/iio/iio.h> +#include <linux/iio/machine.h>
Quoted 8 lines
drivers/iio/adc/rzn1-adc.c line 28
Quoted 8 lines
drivers/iio/adc/rzn1-adc.c new file +8
-0+#include <linux/iio/driver.h> +#include <linux/io.h> +#include <linux/clk.h> +#include <linux/firmware/lces_monitor.h> +#include <linux/delay.h> +#include <linux/bits.h> + +static const uint32_t RZN1_ADC_SET_FORCE_REG_OFFSET =13 *sizeof (uint32_t );// force conversion register
Very long lines so think about how to reduce these. Also, why not just use defines?
Quoted 20 lines
Quoted 7 lines
drivers/iio/adc/rzn1-adc.c new file +7
-0+ +static const uint32_t RZN1_ADC_CONFIG_REG_OFFSET =16 *sizeof (uint32_t );// configuration register +static const uint32_t RZN1_ADC_CONFIG_POWER_DOWN_BIT =BIT (3 );// configuration register's power down bit + +static const uint32_t RZN1_ADC_VIRTUAL_CHANNNELS_REGS_OFFSET =48 *sizeof (uint32_t );// virtual channels 0..15 control registers +static const uint32_t RZN1_ADC_VIRTUAL_CHANNEL_ADC1_SELECTION_MASK =GENMASK (2 ,0 );// control registers' virtual channels' bits +static const uint32_t RZN1_ADC_VIRTUAL_CHANNEL_ADC1_ENABLE_BIT =BIT (15 );// control registers' enable bit drivers/iio/adc/rzn1-adc.c line 45
Quoted 10 lines
drivers/iio/adc/rzn1-adc.c new file +10
-0+ +static const uint32_t RZN1_ADC_VIRTUAL_CHANNEL_ADC1_CONVERSION_DATA_REGS_OFFSET =64 *sizeof (uint32_t );// ADC 1 virtual channels conversion data register + +static const uint32_t RZN1_ADC_READ_DATA_VALUE_MASK =GENMASK (11 ,0 );// read data register's bits +static const uint32_t RZN1_ADC_READ_DATA_UPDATE_BIT =BIT (31 );// read data register's update bit + +#define RZN1_ADC_CHANNEL (index, bits, _shift, _info_mask_sep, name) { \ + .type = IIO_VOLTAGE, \ + .indexed = 1, \ + .channel = index, \maybe also parenthesis, (index)
Quoted 15 lines
Quoted 5 lines
drivers/iio/adc/rzn1-adc.c new file +5
-0+ .info_mask_separate = _info_mask_sep, \ + .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ + .scan_type = { \ + .sign = 'u', \ + .realbits = (bits), \drivers/iio/adc/rzn1-adc.c line 60
Quoted 10 lines
drivers/iio/adc/rzn1-adc.c new file +10
-0+ .storagebits = 16, \ + .shift = (_shift), \ + .endianness = IIO_BE, \ + }, \ + .extend_name = name, \ +} + +#define RZN1_ADC_NUM_CHANNELS 8 // RZN1 ADC has 8 channels + +static struct iio_chan_spec rzn1_adc_channels[RZN1_ADC_NUM_CHANNELS] = {
const
If you use the label stuff I mention below then driver won't be modifying these.
Quoted 1 line
drivers/iio/adc/rzn1-adc.c line 61
Quoted 1 line
drivers/iio/adc/rzn1-adc.c new file +1
-0+ RZN1_ADC_CHANNEL(0, 12, 0, BIT(IIO_CHAN_INFO_RAW), NULL),
Odd to have a parameter then set it to same value in all cases. RZN1_ADC_CHANNEL(0) looks like enough to me.
Quoted 6 lines
Quoted 6 lines
drivers/iio/adc/rzn1-adc.c new file +6
-0+RZN1_ADC_CHANNEL (1 ,12 ,0 ,BIT (IIO_CHAN_INFO_RAW ),NULL ), +RZN1_ADC_CHANNEL (2 ,12 ,0 ,BIT (IIO_CHAN_INFO_RAW ),NULL ), +RZN1_ADC_CHANNEL (3 ,12 ,0 ,BIT (IIO_CHAN_INFO_RAW ),NULL ), +RZN1_ADC_CHANNEL (4 ,12 ,0 ,BIT (IIO_CHAN_INFO_RAW ),NULL ), +RZN1_ADC_CHANNEL (5 ,12 ,0 ,BIT (IIO_CHAN_INFO_RAW ),NULL ), + RZN1_ADC_CHANNEL(6, 12, 0, BIT(IIO_CHAN_INFO_RAW), NULL),
Quoted 8 lines
drivers/iio/adc/rzn1-adc.c line 75
Quoted 8 lines
drivers/iio/adc/rzn1-adc.c new file +8
-0+RZN1_ADC_CHANNEL (7 ,12 ,0 ,BIT (IIO_CHAN_INFO_RAW ),NULL ) +}; + +// Device's private data +struct rzn1_adc { +struct device *dev; + +struct mutex lock;
Locks should have a comment defining exactly what they are protecting. Here I think it is serializing reads...
Quoted 12 lines
Quoted 12 lines
drivers/iio/adc/rzn1-adc.c new file +12
-0+struct completion complete; +void __iomem *registers; +resource_size_t phys_base; +struct clk *clk; +}; + +static inline uint32_t rzn1_adc_read32 (struct rzn1_adc *const adc,const uint32_t reg_off) +{ +#if defined(CONFIG_LCES_SECURE_MONITOR ) +return __monitor_readl (adc->phys_base + reg_off); +#else +void __iomem *addr = adc->registers + reg_off;
Quoted 8 lines
drivers/iio/adc/rzn1-adc.c line 95
Quoted 8 lines
drivers/iio/adc/rzn1-adc.c new file +8
-0+ +return ioread32 (addr); +#endif +} + +static inline void rzn1_adc_write32 (struct rzn1_adc *const adc,const uint32_t reg_off,const uint32_t val) +{ +#if defined(CONFIG_LCES_SECURE_MONITOR )
Is this upstream? It sounds like an odd thing to do via a define rather than some sort of runtime switch.
Quoted 10 lines
drivers/iio/adc/rzn1-adc.c line 102
Quoted 7 lines
drivers/iio/adc/rzn1-adc.c new file +7
-0+__monitor_masked_writel (val, adc->phys_base + reg_off, ~0UL ); +#else +iowrite32 (val, adc->registers + reg_off); +#endif +} + +static bool interrupt_status (struct rzn1_adc *const adc,const int virtual_channel)
rzn1_adc_prefix for consistency please
Quoted 4 lines
drivers/iio/adc/rzn1-adc.c line 106
Quoted 4 lines
drivers/iio/adc/rzn1-adc.c new file +4
-0+{ +bool ret = false; + +if ((virtual_channel >=0 ) && (virtual_channel <RZN1_ADC_NUM_CHANNELS )) {
I think you've already checked this in the caller. No point in doing it again.
Quoted 5 lines
drivers/iio/adc/rzn1-adc.c line 111
Quoted 5 lines
drivers/iio/adc/rzn1-adc.c new file +5
-0+// interrupt 0 status register has a 0 offset in register table +uint32_t status =rzn1_adc_read32 (adc,0 ); + +if (status &BIT (virtual_channel)) + ret = true;
return true;
or
return status & BIT(virtual_channel);
Quoted 3 lines
drivers/iio/adc/rzn1-adc.c line 114
Quoted 3 lines
drivers/iio/adc/rzn1-adc.c new file +3
-0+ } + +return ret;
return false;
Quoted 9 lines
drivers/iio/adc/rzn1-adc.c line 123
Quoted 9 lines
drivers/iio/adc/rzn1-adc.c new file +9
-0+} + +static int rzn1_adc_read_raw (struct iio_dev *indio_dev,struct iio_chan_spec const *chan,int *val,int *val2,long mask) +{ +int ret =IIO_VAL_INT ; + +struct rzn1_adc *adc =iio_priv (indio_dev); +int virtual_channel = chan->channel; +uint32_t virtual_channel_control_offset =RZN1_ADC_VIRTUAL_CHANNNELS_REGS_OFFSET +4 * virtual_channel;
Rethink naming to reduce length of these lines to manageable level. Kernel uses u32 and similar types for internal stuff.
u32 virt_chan_ctl_off = RZN1_ADC_VIRT_CHAN_REG_OFF + 4 * virt_chan;
for example
Quoted 4 lines
drivers/iio/adc/rzn1-adc.c line 127
Quoted 4 lines
drivers/iio/adc/rzn1-adc.c new file +4
-0+uint32_t virtual_channel_control =RZN1_ADC_VIRTUAL_CHANNEL_ADC1_ENABLE_BIT | (RZN1_ADC_VIRTUAL_CHANNEL_ADC1_SELECTION_MASK & virtual_channel); +uint32_t read_data =0 ; + + (void )mask;
? Kernel doesn't care about unused variables in callback functions.
Quoted 6 lines
drivers/iio/adc/rzn1-adc.c line 130
Quoted 3 lines
drivers/iio/adc/rzn1-adc.c new file +3
-0+ (void )val2; + +if ((virtual_channel >=0 ) && (virtual_channel <RZN1_ADC_NUM_CHANNELS )) {not sure if the channel number can be invalid, maybe the IIO framework guarantees it is in range
It does though not uncommon to exercise a bit of paranoia and check it so I don't mind if this stays
Quoted 7 lines
I think it is generally more readable to check for error condition and error out, like so:
if (sth_wrong) {
return-EINVAL;
}sth_right;
Definitely (I'm reviewing backwards so a many comments on this below :)
If you have a mutex held, use a goto unlock; and appropriate label.
Quoted 34 lines
Quoted 21 lines
drivers/iio/adc/rzn1-adc.c new file +20
-0+mutex_lock (&adc->lock); + +// disable power down mode, disable DMA and Sample & Hold mode +rzn1_adc_write32 (adc,RZN1_ADC_CONFIG_REG_OFFSET ,0x00000000 ); + +// map virtual to physical channels 1:1 +rzn1_adc_write32 (adc, virtual_channel_control_offset, virtual_channel_control); + +// force conversion on vc[chan_idx] channel +rzn1_adc_write32 (adc,RZN1_ADC_SET_FORCE_REG_OFFSET ,BIT (virtual_channel)); + +// Wait for maximum conversion duration of 21 ADC clock periods +// ADC clock frequency is 20 MHz hence period is 50 ns +// Add one more period for safety +ndelay (22 *50 ); + + read_data =rzn1_adc_read32 (adc,RZN1_ADC_VIRTUAL_CHANNEL_ADC1_CONVERSION_DATA_REGS_OFFSET +4 * virtual_channel); + +if ((read_data &RZN1_ADC_READ_DATA_UPDATE_BIT ) + && (interrupt_status (adc, virtual_channel) == true))drivers/iio/adc/rzn1-adc.c line 160
Quoted 10 lines
drivers/iio/adc/rzn1-adc.c new file +10
-0+ *val = read_data &RZN1_ADC_READ_DATA_VALUE_MASK ; + else + ret = -EINVAL ;// error reading input value + +// enable power down mode, keep DMA and Sample & Hold mode disabled +rzn1_adc_write32 (adc,RZN1_ADC_CONFIG_REG_OFFSET ,RZN1_ADC_CONFIG_POWER_DOWN_BIT ); + +mutex_unlock (&adc->lock); + }else { +dev_err (adc->dev,"virtual channel index (%i) is greater than 16\n" , virtual_channel);why 16?
_NUM_CHANNELSis 8?
Quoted 10 lines
drivers/iio/adc/rzn1-adc.c line 170
Quoted 10 lines
drivers/iio/adc/rzn1-adc.c new file +10
-0+ } + +return ret; +} + +static const struct iio_info rzn1_adc_info = { + .read_raw = &rzn1_adc_read_raw +}; + +static int rzn1_adc_parse_channels_of (struct rzn1_adc *adc,struct device_node *dn,const int num_channels)
Very long lines. Keep to 80 chars max unless there is a good reason and a very very good reason is needed to go beyond 100 chars (usually something like not splitting a string up someone might want to grep for)
Quoted 3 lines
drivers/iio/adc/rzn1-adc.c line 173
Quoted 3 lines
drivers/iio/adc/rzn1-adc.c new file +3
-0+{ +int ret =0 ; +
No blank lines in variable declarations unless they add something.
Quoted 8 lines
drivers/iio/adc/rzn1-adc.c line 174
Quoted 1 line
drivers/iio/adc/rzn1-adc.c new file +1
-0+struct device_node *channels =NULL ;no need to initialize
drivers/iio/adc/rzn1-adc.c line 175
Quoted 1 line
drivers/iio/adc/rzn1-adc.c new file +1
-0+struct device_node *channel =NULL ;put in local scope?
Quoted 7 lines
drivers/iio/adc/rzn1-adc.c line 180
Quoted 5 lines
drivers/iio/adc/rzn1-adc.c new file +5
-0+ + channels =of_get_child_by_name (dn,"channels" ); +if (channels ==NULL ) { +dev_err (adc->dev,"no channels child node found\n" ); + ret = -EINVAL ;maybe just return
-EINVALhere?
As below, definitely so as to allow dropping indentation of following code.
Quoted 5 lines
drivers/iio/adc/rzn1-adc.c line 184
Quoted 5 lines
drivers/iio/adc/rzn1-adc.c new file +4
-0+ }else { +for_each_available_child_of_node (channels, channel) { +// Stop parsing channels if any error occured +if (!ret) {
Just return where you get the error rather than this odd carry on of the loop whilst not doing anything.
Quoted 5 lines
drivers/iio/adc/rzn1-adc.c line 189
Quoted 5 lines
drivers/iio/adc/rzn1-adc.c new file +5
-0+uint32_t reg =0 ; + + ret =of_property_read_u32 (channel,"reg" , ®); +if (ret) { +dev_err (adc->dev,"no reg child node found\n" );
return ret;
Quoted 4 lines
drivers/iio/adc/rzn1-adc.c line 193
Quoted 4 lines
drivers/iio/adc/rzn1-adc.c new file +4
-0+ }else { +if (reg >= num_channels) { +dev_err (adc->dev,"wrong reg child node value %i\n" , reg); + ret = -EINVAL ;
return -EINVAL;
Quoted 3 lines
drivers/iio/adc/rzn1-adc.c line 196
Quoted 3 lines
drivers/iio/adc/rzn1-adc.c new file +3
-0+ }else { +struct iio_chan_spec *chanp = &rzn1_adc_channels[reg]; + chanp->extend_name =of_get_property (channel,"label" ,NULL );
We now have a core support for a _label attribute for channels. That's a much nicer interface than extend_name ever was so please switch over to that. It uses the read_label() callback.
Quoted 10 lines
drivers/iio/adc/rzn1-adc.c line 206
Quoted 10 lines
drivers/iio/adc/rzn1-adc.c new file +10
-0+ } + } + } + } + } + +return ret; +} + +static int setup_channel_names (struct rzn1_adc *adc,struct iio_dev *const indio_dev)
This wrapper doesn't do much, so I'd just call the parse_channels_of() directly at the call site.
Quoted 8 lines
rzn1_adc_prefix?drivers/iio/adc/rzn1-adc.c line 208
Quoted 2 lines
drivers/iio/adc/rzn1-adc.c new file +2
-0+{ +//struct iio_chan_spec rzn1_adc_channels drop comment
Quoted 8 lines
drivers/iio/adc/rzn1-adc.c line 210
Quoted 2 lines
drivers/iio/adc/rzn1-adc.c new file +2
-0+struct device_node *dn = indio_dev->dev.of_node; +int err =rzn1_adc_parse_channels_of (adc, dn, indio_dev->num_channels);maybe err -> ret?
drivers/iio/adc/rzn1-adc.c line 213
Quoted 3 lines
drivers/iio/adc/rzn1-adc.c new file +3
-0+ +if (err <0 ) +dev_warn (adc->dev,"unable to parse channels!\n" );
Not fatal? Sounds like it probably should be, or we shouldn't be warning.
Quoted 16 lines
drivers/iio/adc/rzn1-adc.c line 221
Quoted 8 lines
drivers/iio/adc/rzn1-adc.c new file +8
-0+ +return err; +} + +static int rzn1_adc_probe (struct platform_device *pdev) +{ +struct device *dev = &pdev->dev; +struct iio_dev *indio_dev =NULL ;no need to initialize
drivers/iio/adc/rzn1-adc.c line 223
Quoted 2 lines
drivers/iio/adc/rzn1-adc.c new file +2
-0+struct rzn1_adc *adc =NULL ; +struct resource *res =NULL ;put in local scope, init needed?
Quoted 9 lines
drivers/iio/adc/rzn1-adc.c line 230
Quoted 7 lines
drivers/iio/adc/rzn1-adc.c new file +7
-0+int ret =0 ; + + indio_dev =devm_iio_device_alloc (dev,sizeof (*adc)); + +if (indio_dev ==NULL ) { +dev_err (&pdev->dev,"Failed to allocate memory for an IIO device!\n" ); + ret = -ENOMEM ;just return
-ENOMEM?
Absolutely.
Quoted 2 lines
drivers/iio/adc/rzn1-adc.c line 231
Quoted 2 lines
drivers/iio/adc/rzn1-adc.c new file +1
-0+ }else {
returning above also means you can drop indentation here. If you do the devm_ stuff I've commented on in remove I suspect platform_set_drvdata() isn't needed any more.
Quoted 6 lines
drivers/iio/adc/rzn1-adc.c line 237
Quoted 6 lines
drivers/iio/adc/rzn1-adc.c new file +6
-0+platform_set_drvdata (pdev, indio_dev); + + adc =iio_priv (indio_dev); + + res =platform_get_resource (pdev,IORESOURCE_MEM ,0 ); +// initilize private physical address for RZN1 secure monitor calls
In IIO, all comments use /* */ syntax.
Quoted 6 lines
drivers/iio/adc/rzn1-adc.c line 243
Quoted 6 lines
drivers/iio/adc/rzn1-adc.c new file +6
-0+ adc->phys_base = res->start; + adc->registers =devm_ioremap_resource (dev, res); + +if (IS_ERR (adc->registers)) { +dev_err (dev,"Unable to acquire memory mapping for the registers!\n" ); + ret =PTR_ERR (adc->registers);
return PTR_ERR(adc->registers);
Quoted 1 line
drivers/iio/adc/rzn1-adc.c line 244
Quoted 1 line
drivers/iio/adc/rzn1-adc.c new file +1
-0+ }else {
Having returned above can drop indent.
Quoted 9 lines
drivers/iio/adc/rzn1-adc.c line 253
Quoted 9 lines
drivers/iio/adc/rzn1-adc.c new file +9
-0+platform_set_drvdata (pdev, indio_dev); + adc->dev = dev; + +// Enabling clock for the ADC + adc->clk =devm_clk_get (&pdev->dev,"rzn1_adc_clk" ); + +if (IS_ERR (adc->clk)) { +dev_err (dev,"Failed to get the clock!\n" ); + ret =PTR_ERR (adc->clk);
return PTR_ERR() etc
Quoted 2 lines
drivers/iio/adc/rzn1-adc.c line 255
Quoted 2 lines
drivers/iio/adc/rzn1-adc.c new file +2
-0+ }else { + ret =clk_prepare_enable (adc->clk);
Use devm_add_action_or_reset() to ensure this is disable on error or remove. Lots of examples of this in other drivers.
Quoted 2 lines
drivers/iio/adc/rzn1-adc.c line 257
Quoted 2 lines
drivers/iio/adc/rzn1-adc.c new file +2
-0+if (ret) { +dev_err (dev,"Could not prepare or enable the clock!\n" );
That's a failure case if it happens (I would imagine) so why not error out here. If it's optional then make that clear with a comment or similar.
Quoted 2 lines
drivers/iio/adc/rzn1-adc.c line 259
Quoted 2 lines
drivers/iio/adc/rzn1-adc.c new file +2
-0+ }else { +dev_info (dev,"ADC clock rate: %lu Hz\n" ,clk_get_rate (adc->clk));
Is that interesting to a normal user? If not, dev_dbg()
Quoted 5 lines
drivers/iio/adc/rzn1-adc.c line 264
Quoted 5 lines
drivers/iio/adc/rzn1-adc.c new file +5
-0+ +mutex_init (&adc->lock); +init_completion (&adc->complete); + + indio_dev->name =dev_name (dev);
I'm lazy - what does that end up as? This should be a part number or similar.
Quoted 18 lines
Quoted 4 lines
drivers/iio/adc/rzn1-adc.c new file +4
-0+ indio_dev->dev.parent = dev; + indio_dev->dev.of_node = pdev->dev.of_node; + indio_dev->info = &rzn1_adc_info; + indio_dev->modes =INDIO_DIRECT_MODE ;drivers/iio/adc/rzn1-adc.c line 278
Quoted 10 lines
drivers/iio/adc/rzn1-adc.c new file +10
-0+ indio_dev->channels = rzn1_adc_channels; + indio_dev->num_channels =ARRAY_SIZE (rzn1_adc_channels); + +if (setup_channel_names (adc, indio_dev) <0 ) +dev_warn (dev,"Invalid channels information - using defaults.\n" ); + + ret =iio_device_register (indio_dev); +if (ret) { +dev_err (dev,"Failed to register IIO device %s': %d\n" , indio_dev->name, ret); + }avoid nested blocks
shouldn't the clock be disabled on failure?
Quoted 14 lines
Quoted 4 lines
drivers/iio/adc/rzn1-adc.c new file +4
-0+ } + } + } + }drivers/iio/adc/rzn1-adc.c line 292
Quoted 10 lines
drivers/iio/adc/rzn1-adc.c new file +10
-0+ +return ret; +} + +static int rzn1_adc_remove (struct platform_device *pdev) +{ +struct iio_dev *indio_dev =platform_get_drvdata (pdev); + +iio_device_unregister (indio_dev); +iio_map_array_unregister (indio_dev);
Hmm.. We still don't have devm_iio_map_array_register and probably should by now. In meantime (if you don't want to add it) you can use devm_add_action_or_reset() to register automated unwinding of this and then use devm_iio_device_register() and get rid of _remove() entirely.
Quoted 6 lines
drivers/iio/adc/rzn1-adc.c line 298
Quoted 6 lines
drivers/iio/adc/rzn1-adc.c new file +6
-0+ +return 0 ; +} + +static const struct platform_device_id rzn1_adc_ids[] = { + { .name ="rzn1-adc" , },
Do we actually need this? I'm guessing all modern platforms are DT based and won't use this?
Quoted 7 lines
drivers/iio/adc/rzn1-adc.c line 305
Quoted 7 lines
drivers/iio/adc/rzn1-adc.c new file +7
-0+ { }, +}; + +MODULE_DEVICE_TABLE (platform, rzn1_adc_ids); + +static const struct of_device_id rzn1_adc_dt_ids[] = { + { .compatible ="rzn1-adc" , },
Vendor prefix
Quoted 9 lines
drivers/iio/adc/rzn1-adc.c line 314
Quoted 9 lines
drivers/iio/adc/rzn1-adc.c new file +9
-0+ { }, +}; + +MODULE_DEVICE_TABLE (of, rzn1_adc_dt_ids); + +static struct platform_driver rzn1_adc_driver = { + .driver = { + .name ="rzn1-adc" , + .of_match_table =of_match_ptr (rzn1_adc_dt_ids),
Whilst I guess we can be fairly sure this driver will only ever be used with DT, our preference in IIO is to not use the of_match_ptr protection and just potentially waste a tiny bit of storage if the driver is built for a non DT system.
Main reason for this is to reduce places it can be cut and paste from where other firmware options are common (this blocks use of ACPI PRP0001 based bindings for example)
Quoted 1 line
drivers/iio/adc/rzn1-adc.c line 315
Quoted 1 line
drivers/iio/adc/rzn1-adc.c new file +1
-0+ },
Indent of that bracket is unusual. Normally aligns with .
Quoted 12 lines
Quoted 11 lines
drivers/iio/adc/rzn1-adc.c new file +10
-0+ .probe = rzn1_adc_probe, + .remove = rzn1_adc_remove, + .id_table = rzn1_adc_ids, +}; + +module_platform_driver (rzn1_adc_driver); + +MODULE_DESCRIPTION ("RZN1 ADC Driver for LCES2" ); +MODULE_AUTHOR ("Tomasz Kazimierz Motyl <[email protected]>" ); +MODULE_LICENSE ("GPL" );