1. ADC

1.1. Introduction

The ROC-RK3588S-PC development board has two kinds of AD interface, respectively: Temperature Sensor and Successive Approximation Register. Among them:

  • TS-ADC (Temperature Sensor): Supports seven-channel.

  • SAR-ADC (Successive Approximation Register): Supports eight-channel single-ended 12-bit SAR-ADC, The maximum conversion rate is 1msps, and 20MHz a/d converter clock is used.

The kernel uses the industrial I/O subsystem to control the ADC, which is mainly designed for AD conversion or DA conversion sensor. The following is an example of SAR-ADC using ADC fan to introduce the basic configuration method of ADC.

1.2. DTS configuration

1.2.1. Configure DTS nodes

The SAR-ADC nodes of ROC-RK3588S-PC defined in kernel/arch/arm64/boot/dts/rockchip/rk3588s.dtsi file, as showm below:

saradc: saradc@fec10000 {
     compatible = "rockchip,rk3588-saradc";
     reg = <0x0 0xfec10000 0x0 0x10000>;
     interrupts = <GIC_SPI 398 IRQ_TYPE_LEVEL_HIGH>;
     #io-channel-cells = <1>;
     clocks = <&cru CLK_SARADC>, <&cru PCLK_SARADC>;
     clock-names = "saradc", "apb_pclk";
     resets = <&cru SRST_P_SARADC>;
     reset-names = "saradc-apb";
     status = "disabled";
};

Firstly, user needs to add the resource description of ADC in the DTS file:

kernel-5.10/arch/arm64/boot/dts/rockchip/rk3588-firefly-demo.dtsi :

adc_demo: adc_demo{
     compatible = "firefly,rk3588-adc";
     status = "disabled";
     io-channels = <&saradc 4>;
};

The application here is for saradc channel 4, which is an ADC connected to the fan

_images/usage_adc_interface.jpg

1.2.2. Matches DTS nodes in the driver file

The user driver can refer to Firefly adc demo: kernel/drivers/iio/adc/adc-firefly-demo.c, which is a driver that detects the status of ROC-RK3588S-PC’s fan. First, define the of_device_id structure array in the driver file:

static const struct of_device_id firefly_adc_match[] = {
     { .compatible = "firefly,rk3588-adc" },
     {},
};

Then populate the structure array with platform_driver to use ADC:

static struct platform_driver firefly_adc_driver = {
    .probe      = firefly_adc_probe,
    .remove     = firefly_adc_remove,
    .driver     = {
        .name   = "firefly_adc",
        .owner  = THIS_MODULE,
        .of_match_table = firefly_adc_match,
        },
};

Then, in firefly_adc_probe, the added resource of DTS is parsed:

static int firefly_adc_probe(struct platform_device *pdev)
{
    printk("firefly_adc_probe!\n");
    chan = iio_channel_get(&(pdev->dev), NULL);
    if (IS_ERR(chan)){
        chan = NULL;
        printk("%s() have not set adc chan\n", __FUNCTION__);
        return -1;
    }
    fan_insert = false;
    if (chan) {
        INIT_DELAYED_WORK(&adc_poll_work, firefly_demo_adc_poll);
        schedule_delayed_work(&adc_poll_work,1000);
    }
    return 0;
}

1.3. Drive instructions

1.3.1. Get AD channel

struct iio_channel *chan;                    #Defines the IIO channel structure
chan = iio_channel_get(&pdev->dev, NULL);    #Get AD channel structure

Note: iio_channel_get gets the IIO channel structure through the parameter pdev passed in by the probe function. The probe function is as follows:

static int XXX_probe(struct platform_device *pdev);

1.3.2. Read the original data collected by AD

int val,ret;
ret = iio_read_channel_raw(chan, &val);

Call the iio_read_channel_raw function to read the raw data collected by AD and store it in val.

1.3.3. Calculate the collected voltage

The standard voltage is used to convert the value of AD to the voltage value required by the user. The calculation formula is as follows:

Vref / (2^n-1) = Vresult / raw

Note:

  • Vref is the standard voltage

  • n is the number of bits converted to AD

  • Vresult is the collection voltage required by the user

  • raw is the original data that AD collects

For example, the standard voltage is 1.8V, the collection bits of AD is 10, and the original data collected by AD is 568, then:

Vresult = (1800mv * 568) / 1023;

1.4. Interface specification

struct iio_channel *iio_channel_get(struct device *dev, const char *consumer_channel);
  • Function : Gets the iio channel description.

  • Parameters :

    • dev: Using the device description pointer of this channel.

    • consumer_channel: The IIO channel description pointer used by the device.

void iio_channel_release(struct iio_channel *chan);
  • Function : Release the channel obtained by the iio_channel_get function.

  • Parameters :

    • chan :The channel description pointer to be released.

int iio_read_channel_raw(struct iio_channel *chan, int *val);
  • Function : Read the original data collected by chan channel AD.

  • Parameters :

    • chan:Collection channel pointer to read.

    • val:The pointer to the result of reading.

1.5. Debug method

1.5.1. Demo program

Enable adc_demo in the kernel/arch/arm64/boot/dts/rockchip/rk3588-firefly-demo.dtsi, change “disabled” to “okay” :

adc_demo: adc_demo{
     status = "okay";
     compatible = "firefly,rk3588-adc";
     io-channels = <&saradc 4>;
};

Compile the kernel, upgrade the kernel to ROC-RK3588S-PC development board, and then plug out the fan, the kernel log information will be printed as follows:

[   85.158104] Fan insert! raw= 135 Voltage= 237mV
[   88.422124] Fan out! raw= 709 Voltage=1247mV

1.5.2. Gets all ADC values

There is a convenient way to query the value of each SARADC:

cat /sys/bus/iio/devices/iio\:device0/in_voltage*_raw

1.6. FAQs

1.6.1. Follow the steps above to apply for SARADC. Why is there an application error?

When the driver needs to get the ADC channel to use, it needs to control the load time of the driver, which must be after saradc initialization. Saradc uses module_platform_driver() for platform device driver registration and ultimately calls module_init(). Therefore, the driver loading function of the user only needs to use one with lower priority than module_init(), such as late_initcall(), so as to ensure that the loading time of the driver is later than the initialization time of saradc and avoid errors.