当前位置:网站首页>IIO device driver of I2C client driver
IIO device driver of I2C client driver
2022-07-21 15:48:00 【juno】
1、IIO equipment
IIO yes Industrial I/O Abbreviation , yes Linux The following subsystem is designed for industrial input and output . Mainly used for sensor equipment ,ADC/DAC equipment .
2、 Basic framework
/* Device private data structure */
struct foobar_i2c_device_data {
struct i2c_client *client;
struct i2c_adapter *adap;
struct iio_dev *indio;
};
static ssize_t type_show(struct device *dev, struct device_attribute *attr, char *buf)
{
int ret;
struct iio_dev *indio_dev = dev_to_iio_dev(dev);
const struct iio_chan_spec *chan = indio_dev->channels;
ret = sprintf(buf, "%d\n", chan->type);
return ret;
}
static int i2c_foobar_iio_device_read_raw(struct iio_dev *indio_dev,
const struct iio_chan_spec *chan, int *val1, int *val2, long mask)
{
struct foobar_i2c_device_data *i2c = iio_device_get_drvdata(indio_dev);
dev_info(&i2c->adap->dev, "%d\n", chan->channel2);
*val1 = chan->type;
*val2 = chan->channel2;
return 1;
}
static IIO_DEVICE_ATTR_RO(type, 0);
static struct attribute *i2c_foobar_attributes[] = {
&iio_dev_attr_type.dev_attr.attr,
NULL
};
static const struct attribute_group i2c_foobar_iio_device_attrs = {
.attrs = i2c_foobar_attributes
};
static const struct iio_info foobar_iio_info = {
.attrs = &i2c_foobar_iio_device_attrs,
.read_raw = i2c_foobar_iio_device_read_raw,
};
static const struct iio_chan_spec i2c_iio_channels[] = {
{
.type = IIO_LIGHT,
.modified = 1,
.channel2 = IIO_MOD_LIGHT_IR,
.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
},
{
.type = IIO_PROXIMITY,
.modified = 1,
.channel2 = IIO_MOD_LIGHT_CLEAR,
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
}
};
static int iio_device_init(struct foobar_i2c_device_data *device)
{
struct iio_dev *indio;
int ret;
indio = iio_device_alloc(&device->client->dev, sizeof(*indio));
if (!indio) {
ret = -ENOMEM;
goto out;
}
indio->info = &foobar_iio_info;
indio->channels = i2c_iio_channels;
indio->num_channels = ARRAY_SIZE(i2c_iio_channels);
indio->dev.parent = &device->client->dev;
indio->name = "sensor name";
ret = iio_device_register(indio);
if (ret) {
dev_err(&device->client->dev, "register iio device FAIL:%d\n", ret);
iio_device_free(indio);
goto out;
}
iio_device_set_drvdata(indio, device);
out:
return ret;
}
static int foobar_i2c_probe(struct i2c_client *client, const struct i2c_device_id *id)
{
struct i2c_adapter *adap = client->adapter;
struct foobar_i2c_device_data *device;
int ret;
device = kzalloc(sizeof(*device), GFP_KERNEL);
if (!device)
return -ENOMEM;
device->client = client;
device->adap = adap;
ret = iio_device_init(device);
if (ret) {
dev_err(&adap->dev, "device init FAIL:%d\n", ret);
kfree(device);
return ret;
}
i2c_set_clientdata(client, device);
return 0;
}
IIO The focus of the equipment is iio_info, There's a little detour here , stay iio_info Need to fill attrs and read_raw,attr Is used to provide custom attributes ,
read_raw Used to read the properties provided by the framework .IIO The device has one by default name attribute , The value of this property is iio_dev Of name, Other
Attributes of can be added by yourself .
3、read_raw matters needing attention
The return value of this parameter function is quite special ,
0: There was no return , Even if filled val1 and val2, There will be no display .
1: Without decimal point , Use only val2 Value .
>= 2:6 Decimal places above ,val1 It's a fraction .2 Is after the decimal point 6 position ,3 It's the decimal point 8 position .
边栏推荐
猜你喜欢
原码、补码以及定点数运算
Wechat vaccine appointment applet graduation project of applet completion work (6) opening defense ppt
BGP (border gateway protocol) border gateway protocol
Training of head and neck segmentation networks with shape prior on small datasets
Wechat vaccine appointment applet graduation design of applet completion work (2) applet function
这款国产API神器工具也太强了吧...让我放弃了postman
Algorithm summary] 20 questions to complete bat interview - binary tree
2021 Shandong Vocational College skills competition secondary vocational group "network security" official competition
CAN物理层介绍
[JS] dynamic rendering page
随机推荐
在竞争白热化的电商行业,链动2+1模式为什么还能生存那么久?
小程序毕设作品之微信疫苗预约小程序毕业设计(3)后台功能
Classic competition questions web penetration test record
Wechat vaccine appointment applet graduation project of applet completion work (4) opening report
LabVIEW实时监测和绘制单晶光伏板电压电流和能耗趋势
net core 3.1使用identityServer登录时signin-oidc报Correlation failed的解决方法
LeetCode_ Prefix and_ Difficulties_ 862. And the shortest subarray of at least k
2022/07/20 learning notes (Day12) string string string
[case sharing] typical configuration of policy routing
【Coggle 30 Days of ML】糖尿病遗传风险检测挑战赛(1)
开发人员使用Klocwork进行软件安全的五大原因
通过TPT的FUSION平台实现联合测试
[error]runtimeerror: expected scalar type double but found float (torch)
[JS] dynamic rendering page
Esp32-cam - intranet penetration tutorial
漏洞扫码--需要整改的
专注跨境支付一体化服务 iPayLinks荣获《财资》(The Asset)3A亚洲奖!
[test report in July] Oracle OCP 19C exam passed
Why does someone choose the one with high quotation in software development? Did they lose money?
8 月亚马逊云科技培训与认证课程,精彩不容错过!