Linux 开发IIO子系统入门

电子说

1.2w人已加入

描述

前阵子工作上做了一些关于 ADC 的支持,由于现在 ADC 相关的支持都被移动到了 IIO (Industrial I/O) 子系统下,我查阅了一些关于 IIO 资料,包括书籍、文章、内核文档和代码。个人感觉最好的入门文章应该是 ST WiKi 网站上 的 IIO Overview(2019) 和 Analog Device Wiki 网站上的 Linux Industrial I/O Subsystem(2017),为了方便爱偷懒或者英文不好的小伙伴,我提炼了多篇文章的精华内容并在其基础上进行完善,尽量控制篇幅,希望能给大家提供一点小小的帮助。

Linux 驱动开发 / IIO子系统入门1

正文目录:

1. 什么是 IIO 子系统? 1.1 IIO 概述 1.2 IIO 相关的组件2. IIO 功能特性3. IIO 相关配置 3.1 配置内核 3.2 配置设备树 3.2.1 IIO providers 3.2.2 IIO consumers4. IIO API 4.1 用户空间 API 4.1.1 4种接口 4.1.2 操作实例5. 更多值得学习的知识点6. 相关参考

写作目的:

整理一些 IIO 子系统的入门知识。

1. 什么是 IIO 子系统?

1.1 IIO 概述

Industrial I/O 子系统旨在为某种意义上是模数或数模转换器 (ADC,DAC) 的设备提供支持,于2009年由 Huawei 的 Jonathan Cameront 添加。

简单框图:

adc

支持的设备包括:

ADC / DAC加速度计磁力计陀螺仪压力传感器湿度传感器温度传感器。。.

很久以前,对于上述硬件的支持散落在 Linux 源码中的各种地方。

IIO 的出现,提供了一个统一的框架用于访问和控制上述各种类型的传感器,并且为用户态应用访问传感器提供了标准的接口:sysfs/devfs,并且填补了 Hwmon 和 Input 子系统之间的空白。

另外,IIO 不仅可以支持低速的 SoC ADC,还可支持高速、高数据速率的工业设备,例如 100M samples/sec 工业 ADC。

1.2 IIO 相关的组件

adc

IIO-overview

上图基于 STM32 MPU,来源见文末。

1) 客户端应用程序(用户空间):

该组件会使用 libiio 库来配置 IIO 设备,然后从 IIO 设备读数据或者写数据到 IIO 设备中。客户端程序可以细分为 local client 和 remote client。

2) libiio 库(用户空间):

adc

libiio 是 Analog Device 公司发起的一个用于访问 IIO 设备的开源库。

它封装了对 /sys/bus/iio/devices(配置 iio) 和 /dev/iio/deviceX(读写iio) 的访问,并且提供了便于测试的 iio 命令行工具 (iio_info / iio_readdev) 和 iiod server。

iiod server 内含 local backend 和 remote backend 以支持 local client 和 remote client 的访问。

libiio 的源码位于:github libiio

3) 访问接口(用户空间):

iio 支持多种标准的 Linux 设备访问接口:

char device, sysfs, configfs, debugfs。

4) 内核空间的 iio 消费者(即 IIO consumers):

adc

除了用户空间的应用程序能访问 iio 设备之外,在内核里也有其他设备驱动需要使用 iio 子系统的 API 来编写符合自身框架的设备驱动。

例如在 iio 子系统里支持了某款 Soc ADC 后,可能会有不同的硬件设备接到该 ADC 通道上,典型的例子是触摸芯片,开发人员需要在 input 子系统的框架下编写 touch driver,在 touch driver的 irq handler 中 调用 iio in-kern API 来读取触摸屏的 X、Y 值。

iio in-kernl API的定义位于头文件:

include/linux/iio/consumer.h

5) IIO framework(内核空间):

IIO 子系统的核心实现。

6) IIO device driver(或称 IIO providers):

7) Linux 内核自带的 IIO 调试工具:

2. IIO 的功能特性

1) 基础的设备注册和访问

类似于 hwmon 子系统,它们都可以通过 sysfs 以轮循的方式访问设备;

2) 可读取事件的字符设备(Event chrdevs)

类似于 input 子系统,iio 子系统也可以向应用层上报事件(hardware triggered events),例如阈值检测事件,自由落体检测事件、更复杂的动作检测事件;

目前 event 的格式为:event code + 时间戳;

3) 支持硬件 buffer

4) 支持 Trigger 和软件 buffer

3. IIO 相关配置

3.1 配置内核

Linux-4.14:

$ make menuconfigDevice Drivers ---》 《*》 Industrial I/O support ---》 [*] Enable buffer support within IIO 《 》 IIO callback buffer used for push in-kernel interfaces 《*》 Industrial I/O HW buffering 《*》 Industrial I/O buffering based on kfifo 《 》 Enable IIO configuration via configfs [*] Enable triggered sampling support (2) Maximum number of consumers per trigger 《 》 Enable software triggers support Accelerometers ---》 Analog to digital converters ---》 Amplifiers ---》 Chemical Sensors ---》 Hid Sensor IIO Common ---- SSP Sensor Common ---》 Digital to analog converters ---》 IIO dummy driver ---》 Frequency Synthesizers DDS/PLL ---》 Digital gyroscope sensors ---》 Health Sensors ---》 Humidity sensors ---》 Inertial measurement units ---》 Light sensors ---》 Magnetometer sensors ---》 Inclinometer sensors ---- Triggers - standalone ---》 Digital potentiometers ---》 Pressure sensors ---》 Lightning sensors ---》 Proximity sensors ---》 Temperature sensors ---》

从配置项的数目来看,IIO 的用途真的很广泛。

3.2 配置设备树

3.2.1 IIO providers

1) 相关要点:

IIO channels 源在设备树中用 IIO providers 来指定;

2) 必要属性:

io-channel-cells,0 表示只有 1 路 IIO output,1 表示有多路 IIO output;

io-channel-ranges: 一个 empty 属性(即不用赋值),会在 driver/iio/inkern.c/iio_channel_get() 中被引用,它表明继承了当前节点的子节点可以引用当前节点的 IIO channel;

3) 例子1 (no trigger)

adc: voltage-sensor@35 { compatible = “maxim,max1139”; reg = 《0x35》; #io-channel-cells = 《1》;};

4) 例子2 (with trigger)

adc@35 { compatible = “some-vendor,some-adc”; reg = 《0x35》; adc1: iio-device@0 { #io-channel-cells = 《1》; /* other properties */ }; adc2: iio-device@1 { #io-channel-cells = 《1》; /* other properties */ };};

3.2.2 IIO consumers

1) 相关要点:

IIO consumer 节点的形式是 《phandle iio_specifier》;

它的作用是连接 IIO provider 的 input 端到 IIO consumer 的 output 端;

其中,phandle 是 IIO provider 的句柄,specifier 用于选择第几路 channel;

类似 gpio specifier, IIO specifier 是有 1 个或者多个 cells 的数组,用于确定 IIO device的 output 端,即 1 个 cell 对应一个 IIO channel output;

IIO specifier 数组的长度由 IIO provider 节点的 #io-channel-cells 属性决定;

2) 必要属性:

io-channels: 《phandle iio_specifier》 列表, 一个 《phandle iio_specifier》 代表该设备连接着的一路 IIO input。如果 IIO provider 的 io-channel-cells=0 (即只有1路 IIO output), 则省略 iio_specifier。

3) 可选属性:

io-channel-names: IIO input 的名称列表,顺序要和 io-channels 属性保持一致,Consumers drivers 会将该名称和 iio_specifier 指定的 IIO input match 到一起。

4) 例子1

some_consumer { io-channels = 《&adc 1》, 《&ref 0》; io-channel-names = “vcc”, “vdd”; };

上述例子的引用了provider &adc 的第1路 channel,和proiver &ref 的第0路 channel。

4. IIO API

4.1 用户空间 API

相关参考:

IIO user space interface

How to use the IIO user space interface

4.1.1 4种接口

1)。 sysfs interface

/sys/bus/iio/devices/iio:deviceX;

可用于配置 /dev/iio:deviceX 接口的 events / data

可用于轮循的方式低速地直接读/写 IIO 设备;

Documentation/ABI/testing/sysfs-bus-iio;

2)。 character device

/dev/iio:deviceX,该接口在 IIO 子系统里是可选非必要的;

标准的文件 IO API: open(), read(), write(), close()。

用于读取 events 和 data;

3)。 configfs

用于配置额外的 IIO 特性,例如:软件 triggers 或者 hrtimer triggers;

详细说明:

Documentation/ABI/testing/configfs-iio;

Documentation/iio/iio_configfs.txt;

4)。 debugfs

一些调试功能,例如 direct_reg_access 节点可用于读写寄存器;

4.1.2 操作实例

IIO direct mode: 通过 sysfs 以轮循的方式读 ADC 或者写 DAC:

1) 直接读 ADC

确定 sysfs 节点(方式1,不依赖工具)

$ grep -H “” /sys/bus/iio/devices/*/name | grep adc/sys/bus/iio/devices/iio:device0/name:48003000.adc:adc@0/sys/bus/iio/devices/iio:device1/name:48003000.adc:adc@1

sysfs 中的 iio:device0 sysfs 对应 ADC1;

$ cd /sys/bus/iio/devices/iio:device0/$ cat in_voltage6_raw # Convert ADC1 channel 0 (analog-to-digital): get raw value40603$ cat in_voltage_scale # Read scale0.044250488$ cat in_voltage_offset # Read offset0$ awk “BEGIN{printf (\”%d\n\“, (40603 + 0) * 0.044250488)}”1796

计算公式: Scaled value = (raw + offset) * scale = 1796 mV;

2) 直接写 DAC

确定 sysfs 节点(方式2)

$ lsiio | grep dacDevice 003: 40017000.dac:dac@1Device 004: 40017000.dac:dac@2

sysfs 中的 iio:device3 sysfs 对应 DAC1,lsiio 来源于Linux 内核源码(tools/iio/)。

$ cd /sys/bus/iio/devices/iio:device3/$ cat out_voltage1_scale # Read scale0.708007812$ awk “BEGIN{printf (\”%d\n\“, 2000 / 0.708007812)}” # 假设要输出 2000 mV2824$ echo 2824 》 out_voltage1_raw # Write raw value to DAC1$ echo 0 》 out_voltage1_powerdown # Enable DAC1 (out of power-down mode)

5. 更多值得学习的知识点

以 timer triggers 和 buffers 的方式读 ADC 或者写 DAC;

IIO 内核空间 API;

编写 IIO device driver

编写 IIO consumer driver

鉴于大多数人的注意力无法在一篇文章里上集中太久,更多的内容请大家先自行去阅读吧,不是自己理解到的东西是消化不了的。有机会的话我会把更多的读书心得放在后面的文章。

6. 相关参考

IIO Overview

Linux Industrial I/O Subsystem

《Linux Device Drivers Development》

本文只是一篇入门的文章,仍然有许多细节的知识点没有被描述到。如果本文能让你对 IIO 子系统有个大概的认识,那么本文的目的也就算达成了,以后还会继续写更多 IIO 子系统的文章。

责任编辑:haq

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分