Documentation/driver-api/iio/core.rst GitHub 원문 ↗

Linux 6.18.37 · Driver API

Core elements

IIO device lifecycle, userspace interface, channel specification과 attribute mask·modifier·indexing을 설명합니다.

Source pathDocumentation/driver-api/iio/core.rst
Source versionLinux v6.18.37
TranslationDUJINLABS 전문 번역 + 해설

요약·해설과 원문, 전문 번역을 서로 분리했습니다. API 이름, symbol, source path는 원문 표기를 사용합니다.

1. 요약·해설

원문의 핵심 논리와 kernel programming 관점의 보충 설명입니다. 아래의 전문 번역과는 별도로 작성했습니다.

요약과 해설

core.rst:1-182

IIO core는 sensor 하나를 iio_dev로, 측정 축·spectrum·instance를 iio_chan_spec으로 표현합니다. Probe와 remove는 allocation·registration을 정확히 역순으로 수행하고 mask·modifier·index가 생성될 sysfs attribute 이름과 공유 범위를 결정합니다.

2. 영어 원문 전체

번역 기준이 된 Linux v6.18.37 원문입니다. 줄 번호는 이 버전의 파일 좌표입니다.

원문 전체 펼치기
1 =============
2 Core elements
3 =============
4
5 The Industrial I/O core offers both a unified framework for writing drivers for
6 many different types of embedded sensors and a standard interface to user space
7 applications manipulating sensors. The implementation can be found under
8 :file:`drivers/iio/industrialio-*`
9
10 Industrial I/O Devices
11 ----------------------
12
13 * struct iio_dev - industrial I/O device
14 * iio_device_alloc() - allocate an :c:type:`iio_dev` from a driver
15 * iio_device_free() - free an :c:type:`iio_dev` from a driver
16 * iio_device_register() - register a device with the IIO subsystem
17 * iio_device_unregister() - unregister a device from the IIO
18 subsystem
19
20 An IIO device usually corresponds to a single hardware sensor and it
21 provides all the information needed by a driver handling a device.
22 Let's first have a look at the functionality embedded in an IIO device
23 then we will show how a device driver makes use of an IIO device.
24
25 There are two ways for a user space application to interact with an IIO driver.
26
27 1. :file:`/sys/bus/iio/devices/iio:device{X}/`, this represents a hardware sensor
28 and groups together the data channels of the same chip.
29 2. :file:`/dev/iio:device{X}`, character device node interface used for
30 buffered data transfer and for events information retrieval.
31
32 A typical IIO driver will register itself as an :doc:`I2C <../i2c>` or
33 :doc:`SPI <../spi>` driver and will create two routines, probe and remove.
34
35 At probe:
36
37 1. Call iio_device_alloc(), which allocates memory for an IIO device.
38 2. Initialize IIO device fields with driver specific information (e.g.
39 device name, device channels).
40 3. Call iio_device_register(), this registers the device with the
41 IIO core. After this call the device is ready to accept requests from user
42 space applications.
43
44 At remove, we free the resources allocated in probe in reverse order:
45
46 1. iio_device_unregister(), unregister the device from the IIO core.
47 2. iio_device_free(), free the memory allocated for the IIO device.
48
49 IIO device sysfs interface
50 ==========================
51
52 Attributes are sysfs files used to expose chip info and also allowing
53 applications to set various configuration parameters. For device with
54 index X, attributes can be found under /sys/bus/iio/devices/iio:deviceX/
55 directory. Common attributes are:
56
57 * :file:`name`, description of the physical chip.
58 * :file:`dev`, shows the major:minor pair associated with
59 :file:`/dev/iio:deviceX` node.
60 * :file:`sampling_frequency_available`, available discrete set of sampling
61 frequency values for device.
62 * Available standard attributes for IIO devices are described in the
63 :file:Documentation/ABI/testing/sysfs-bus-iio file in the Linux kernel
64 sources.
65
66 IIO device channels
67 ===================
68
69 struct iio_chan_spec - specification of a single channel
70
71 An IIO device channel is a representation of a data channel. An IIO device can
72 have one or multiple channels. For example:
73
74 * a thermometer sensor has one channel representing the temperature measurement.
75 * a light sensor with two channels indicating the measurements in the visible
76 and infrared spectrum.
77 * an accelerometer can have up to 3 channels representing acceleration on X, Y
78 and Z axes.
79
80 An IIO channel is described by the struct iio_chan_spec.
81 A thermometer driver for the temperature sensor in the example above would
82 have to describe its channel as follows::
83
84 static const struct iio_chan_spec temp_channel[] = {
85 {
86 .type = IIO_TEMP,
87 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
88 },
89 };
90
91 Channel sysfs attributes exposed to userspace are specified in the form of
92 bitmasks. Depending on their shared info, attributes can be set in one of the
93 following masks:
94
95 * **info_mask_separate**, attributes will be specific to
96 this channel
97 * **info_mask_shared_by_type**, attributes are shared by all channels of the
98 same type
99 * **info_mask_shared_by_dir**, attributes are shared by all channels of the same
100 direction
101 * **info_mask_shared_by_all**, attributes are shared by all channels
102
103 When there are multiple data channels per channel type we have two ways to
104 distinguish between them:
105
106 * set **.modified** field of :c:type:`iio_chan_spec` to 1. Modifiers are
107 specified using **.channel2** field of the same :c:type:`iio_chan_spec`
108 structure and are used to indicate a physically unique characteristic of the
109 channel such as its direction or spectral response. For example, a light
110 sensor can have two channels, one for infrared light and one for both
111 infrared and visible light.
112 * set **.indexed** field of :c:type:`iio_chan_spec` to 1. In this case the
113 channel is simply another instance with an index specified by the **.channel**
114 field.
115
116 Here is how we can make use of the channel's modifiers::
117
118 static const struct iio_chan_spec light_channels[] = {
119 {
120 .type = IIO_INTENSITY,
121 .modified = 1,
122 .channel2 = IIO_MOD_LIGHT_IR,
123 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
124 .info_mask_shared = BIT(IIO_CHAN_INFO_SAMP_FREQ),
125 },
126 {
127 .type = IIO_INTENSITY,
128 .modified = 1,
129 .channel2 = IIO_MOD_LIGHT_BOTH,
130 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
131 .info_mask_shared = BIT(IIO_CHAN_INFO_SAMP_FREQ),
132 },
133 {
134 .type = IIO_LIGHT,
135 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
136 .info_mask_shared = BIT(IIO_CHAN_INFO_SAMP_FREQ),
137 },
138 }
139
140 This channel's definition will generate two separate sysfs files for raw data
141 retrieval:
142
143 * :file:`/sys/bus/iio/devices/iio:device{X}/in_intensity_ir_raw`
144 * :file:`/sys/bus/iio/devices/iio:device{X}/in_intensity_both_raw`
145
146 one file for processed data:
147
148 * :file:`/sys/bus/iio/devices/iio:device{X}/in_illuminance_input`
149
150 and one shared sysfs file for sampling frequency:
151
152 * :file:`/sys/bus/iio/devices/iio:device{X}/sampling_frequency`.
153
154 Here is how we can make use of the channel's indexing::
155
156 static const struct iio_chan_spec light_channels[] = {
157 {
158 .type = IIO_VOLTAGE,
159 .indexed = 1,
160 .channel = 0,
161 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
162 },
163 {
164 .type = IIO_VOLTAGE,
165 .indexed = 1,
166 .channel = 1,
167 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
168 },
169 }
170
171 This will generate two separate attributes files for raw data retrieval:
172
173 * :file:`/sys/bus/iio/devices/iio:device{X}/in_voltage0_raw`, representing
174 voltage measurement for channel 0.
175 * :file:`/sys/bus/iio/devices/iio:device{X}/in_voltage1_raw`, representing
176 voltage measurement for channel 1.
177
178 More details
179 ============
180 .. kernel-doc:: include/linux/iio/iio.h
181 .. kernel-doc:: drivers/iio/industrialio-core.c
182 :export:
183

3. 한국어 전문 번역

영어 원문의 문단 순서와 의미를 유지한 전체 번역입니다. 코드, 함수명, symbol과 URL은 원문 표기를 유지합니다.

Industrial I/O core

1-9

문서 제목은 `Core elements`입니다. Industrial I/O core는 여러 종류의 embedded sensor driver를 작성하는 unified framework와 sensor를 조작하는 userspace application용 standard interface를 함께 제공합니다. 구현은 `drivers/iio/industrialio-*` 아래에 있습니다.

IIO core 역할
Embedded sensor hardwareDevice-specific IIO driverdrivers/iio/industrialio-* coreUnified channel·event·buffer frameworkStandard sysfs·character-device interfaceUserspace application

Hardware sensor에서 공통 userspace interface까지의 계층입니다.

IIO device lifecycle

10-48

`struct iio_dev`는 industrial I/O device입니다. Driver는 `iio_device_alloc()`으로 할당하고 `iio_device_free()`로 해제하며, `iio_device_register()`로 IIO subsystem에 등록하고 `iio_device_unregister()`로 등록을 해제합니다.

IIO device는 보통 hardware sensor 하나에 대응하며 device를 다루는 driver에 필요한 모든 정보를 제공합니다.

Userspace application이 IIO driver와 상호작용하는 방법은 두 가지입니다. `/sys/bus/iio/devices/iio:device{X}/`는 hardware sensor를 나타내고 같은 chip의 data channel을 묶습니다. `/dev/iio:device{X}` character device node는 buffered data transfer와 event information retrieval에 사용합니다.

일반적인 IIO driver는 I2C 또는 SPI driver로 등록하고 `probe`와 `remove` routine을 만듭니다. Probe에서는 `iio_device_alloc()`으로 memory를 할당하고 device name·channel 같은 driver-specific field를 초기화한 뒤 `iio_device_register()`를 호출합니다. 이 호출 뒤 device가 userspace request를 받을 수 있습니다.

Remove에서는 probe의 역순으로 resource를 해제합니다. 먼저 `iio_device_unregister()`로 IIO core 등록을 해제하고 `iio_device_free()`로 device memory를 해제합니다.

IIO userspace interface
Interface용도
/sys/bus/iio/devices/iio:device{X}/Sensor 정보·같은 chip의 channel·configuration
/dev/iio:device{X}Buffered data transfer·event retrieval

Sysfs와 character device의 용도를 비교합니다.

IIO device probe·remove
Probe: iio_device_alloc()Driver-specific field 초기화iio_device_register()Userspace request 처리Remove: iio_device_unregister()iio_device_free()

등록과 해제를 정확히 역순으로 수행합니다.

IIO device sysfs interface

49-65

Sysfs attribute는 chip information을 노출하고 application이 configuration parameter를 설정하게 하는 file입니다. Index X인 device의 attribute는 `/sys/bus/iio/devices/iio:deviceX/` 아래에 있습니다.

일반 attribute에는 physical chip 설명인 `name`, `/dev/iio:deviceX` node의 `major:minor` pair를 보여 주는 `dev`, device가 지원하는 discrete sampling frequency 집합인 `sampling_frequency_available`이 있습니다. IIO standard attribute 전체는 kernel source의 `Documentation/ABI/testing/sysfs-bus-iio`에 설명됩니다.

IIO device attribute
Attribute내용
namePhysical chip 설명
dev/dev/iio:deviceX의 major:minor
sampling_frequency_available지원하는 discrete sampling frequency
Standard ABIDocumentation/ABI/testing/sysfs-bus-iio

Device-level 공통 sysfs file입니다.

IIO channel과 iio_chan_spec

66-90

`struct iio_chan_spec`은 single channel specification입니다. IIO device channel은 data channel 하나를 표현하며 device에는 channel이 하나 또는 여러 개 있을 수 있습니다.

Thermometer는 temperature measurement channel 하나, light sensor는 visible·infrared spectrum channel 두 개, accelerometer는 X·Y·Z acceleration channel을 최대 세 개 가질 수 있습니다.

IIO channel은 `struct iio_chan_spec`으로 설명합니다. 예제 thermometer는 `IIO_TEMP` type이고 `IIO_CHAN_INFO_PROCESSED`를 channel-specific processed attribute로 노출합니다.

static const struct iio_chan_spec temp_channel[] = {
     {
         .type = IIO_TEMP,
         .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
     },
};
Sensor channel 예
SensorChannel
ThermometerTemperature 1개
Light sensorVisible + infrared 2개
AccelerometerX + Y + Z 최대 3개

Sensor 종류별 channel 구성을 비교합니다.

Channel attribute mask와 식별

91-115

Userspace에 노출하는 channel sysfs attribute는 bitmask로 지정합니다. Shared 범위에 따라 `info_mask_separate`는 특정 channel만, `info_mask_shared_by_type`은 같은 type의 모든 channel, `info_mask_shared_by_dir`은 같은 direction의 모든 channel, `info_mask_shared_by_all`은 모든 channel이 공유합니다.

한 channel type에 data channel이 여러 개면 두 방식으로 구분합니다. 첫째, `iio_chan_spec.modified = 1`로 두고 같은 structure의 `.channel2`에 modifier를 지정합니다. Modifier는 direction이나 spectral response처럼 물리적으로 unique한 특성을 나타냅니다. 예를 들어 light sensor는 infrared channel과 infrared+visible channel을 둘 수 있습니다.

둘째, `iio_chan_spec.indexed = 1`로 두고 `.channel` field에 index를 지정합니다. 이 경우 channel은 단순히 index가 다른 instance입니다.

Channel info mask scope
Mask공유 범위
info_mask_separate해당 channel만
info_mask_shared_by_type같은 type의 모든 channel
info_mask_shared_by_dir같은 direction의 모든 channel
info_mask_shared_by_all모든 channel

Attribute가 공유되는 channel 범위입니다.

Channel 구분 방식
방식Field의미
Modified.modified=1 + .channel2Direction·spectral response 같은 물리 특성
Indexed.indexed=1 + .channel동일 type의 번호가 다른 instance

Modifier와 index를 쓰는 조건입니다.

Channel modifier와 생성 attribute

116-153

예제 light sensor는 `IIO_INTENSITY` channel 두 개에 `IIO_MOD_LIGHT_IR`와 `IIO_MOD_LIGHT_BOTH` modifier를 적용하고 raw data를 channel별로, sampling frequency를 공유 attribute로 노출합니다. 별도의 `IIO_LIGHT` channel은 processed illuminance 값을 노출합니다.

static const struct iio_chan_spec light_channels[] = {
        {
                .type = IIO_INTENSITY,
                .modified = 1,
                .channel2 = IIO_MOD_LIGHT_IR,
                .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
                .info_mask_shared = BIT(IIO_CHAN_INFO_SAMP_FREQ),
        },
        {
                .type = IIO_INTENSITY,
                .modified = 1,
                .channel2 = IIO_MOD_LIGHT_BOTH,
                .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
                .info_mask_shared = BIT(IIO_CHAN_INFO_SAMP_FREQ),
        },
        {
                .type = IIO_LIGHT,
                .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
                .info_mask_shared = BIT(IIO_CHAN_INFO_SAMP_FREQ),
        },
   }

이 정의는 raw data용 `/sys/bus/iio/devices/iio:device{X}/in_intensity_ir_raw`와 `in_intensity_both_raw`, processed data용 `in_illuminance_input`, 공유 sampling frequency용 `sampling_frequency` file을 생성합니다.

Modifier에서 sysfs 이름 생성
IIO_INTENSITY typeIIO_MOD_LIGHT_IR 또는 IIO_MOD_LIGHT_BOTHIIO_CHAN_INFO_RAW separatein_intensity_ir_raw 또는 in_intensity_both_rawSampling frequency는 shared공통 sampling_frequency file

Channel type·modifier·info mask가 attribute path를 만듭니다.

Light sensor 생성 file
DataSysfs file
IR rawin_intensity_ir_raw
IR + visible rawin_intensity_both_raw
Processed illuminancein_illuminance_input
Shared sample ratesampling_frequency

Channel definition과 결과 sysfs attribute입니다.

Indexed channel과 생성 attribute

154-177

Channel indexing 예제는 `IIO_VOLTAGE` type 두 channel에 `.indexed = 1`을 설정하고 `.channel`을 0과 1로 지정합니다.

static const struct iio_chan_spec light_channels[] = {
        {
                .type = IIO_VOLTAGE,
                .indexed = 1,
                .channel = 0,
                .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
        },
        {
                .type = IIO_VOLTAGE,
                .indexed = 1,
                .channel = 1,
                .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
        },
}

그 결과 `/sys/bus/iio/devices/iio:device{X}/in_voltage0_raw`는 channel 0 voltage measurement를, `in_voltage1_raw`는 channel 1 measurement를 나타냅니다.

Indexed voltage channel
.channelAttribute측정
0in_voltage0_rawChannel 0 voltage
1in_voltage1_rawChannel 1 voltage

Channel number가 sysfs file 이름에 들어갑니다.

IIO core kernel API source

178-182

IIO public core declaration은 `include/linux/iio/iio.h`, exported core implementation은 `drivers/iio/industrialio-core.c`의 kernel-doc에서 가져옵니다.

.. kernel-doc:: include/linux/iio/iio.h
.. kernel-doc:: drivers/iio/industrialio-core.c
   :export:
IIO core kernel-doc source
범위Source path
Core declarationinclude/linux/iio/iio.h
Exported coredrivers/iio/industrialio-core.c

Public header와 exported implementation입니다.