Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / iio / humidity / si7005.c
blob160b3d92df619c3ece14db020b84d8966b748d8f
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * si7005.c - Support for Silabs Si7005 humidity and temperature sensor
5 * Copyright (c) 2014 Peter Meerwald <pmeerw@pmeerw.net>
7 * (7-bit I2C slave address 0x40)
9 * TODO: heater, fast mode, processed mode (temp. / linearity compensation)
12 #include <linux/err.h>
13 #include <linux/i2c.h>
14 #include <linux/delay.h>
15 #include <linux/module.h>
16 #include <linux/pm.h>
18 #include <linux/iio/iio.h>
19 #include <linux/iio/sysfs.h>
21 #define SI7005_STATUS 0x00
22 #define SI7005_DATA 0x01 /* 16-bit, MSB */
23 #define SI7005_CONFIG 0x03
24 #define SI7005_ID 0x11
26 #define SI7005_STATUS_NRDY BIT(0)
27 #define SI7005_CONFIG_TEMP BIT(4)
28 #define SI7005_CONFIG_START BIT(0)
30 #define SI7005_ID_7005 0x50
31 #define SI7005_ID_7015 0xf0
33 struct si7005_data {
34 struct i2c_client *client;
35 struct mutex lock;
36 u8 config;
39 static int si7005_read_measurement(struct si7005_data *data, bool temp)
41 int tries = 50;
42 int ret;
44 mutex_lock(&data->lock);
46 ret = i2c_smbus_write_byte_data(data->client, SI7005_CONFIG,
47 data->config | SI7005_CONFIG_START |
48 (temp ? SI7005_CONFIG_TEMP : 0));
49 if (ret < 0)
50 goto done;
52 while (tries-- > 0) {
53 msleep(20);
54 ret = i2c_smbus_read_byte_data(data->client, SI7005_STATUS);
55 if (ret < 0)
56 goto done;
57 if (!(ret & SI7005_STATUS_NRDY))
58 break;
60 if (tries < 0) {
61 ret = -EIO;
62 goto done;
65 ret = i2c_smbus_read_word_swapped(data->client, SI7005_DATA);
67 done:
68 mutex_unlock(&data->lock);
70 return ret;
73 static int si7005_read_raw(struct iio_dev *indio_dev,
74 struct iio_chan_spec const *chan, int *val,
75 int *val2, long mask)
77 struct si7005_data *data = iio_priv(indio_dev);
78 int ret;
80 switch (mask) {
81 case IIO_CHAN_INFO_RAW:
82 ret = si7005_read_measurement(data, chan->type == IIO_TEMP);
83 if (ret < 0)
84 return ret;
85 *val = ret;
86 return IIO_VAL_INT;
87 case IIO_CHAN_INFO_SCALE:
88 if (chan->type == IIO_TEMP) {
89 *val = 7;
90 *val2 = 812500;
91 } else {
92 *val = 3;
93 *val2 = 906250;
95 return IIO_VAL_INT_PLUS_MICRO;
96 case IIO_CHAN_INFO_OFFSET:
97 if (chan->type == IIO_TEMP)
98 *val = -50 * 32 * 4;
99 else
100 *val = -24 * 16 * 16;
101 return IIO_VAL_INT;
102 default:
103 break;
106 return -EINVAL;
109 static const struct iio_chan_spec si7005_channels[] = {
111 .type = IIO_HUMIDITYRELATIVE,
112 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
113 BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_OFFSET),
116 .type = IIO_TEMP,
117 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
118 BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_OFFSET),
122 static const struct iio_info si7005_info = {
123 .read_raw = si7005_read_raw,
126 static int si7005_probe(struct i2c_client *client,
127 const struct i2c_device_id *id)
129 struct iio_dev *indio_dev;
130 struct si7005_data *data;
131 int ret;
133 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WORD_DATA))
134 return -EOPNOTSUPP;
136 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
137 if (!indio_dev)
138 return -ENOMEM;
140 data = iio_priv(indio_dev);
141 i2c_set_clientdata(client, indio_dev);
142 data->client = client;
143 mutex_init(&data->lock);
145 indio_dev->name = dev_name(&client->dev);
146 indio_dev->modes = INDIO_DIRECT_MODE;
147 indio_dev->info = &si7005_info;
149 indio_dev->channels = si7005_channels;
150 indio_dev->num_channels = ARRAY_SIZE(si7005_channels);
152 ret = i2c_smbus_read_byte_data(client, SI7005_ID);
153 if (ret < 0)
154 return ret;
155 if (ret != SI7005_ID_7005 && ret != SI7005_ID_7015)
156 return -ENODEV;
158 ret = i2c_smbus_read_byte_data(client, SI7005_CONFIG);
159 if (ret < 0)
160 return ret;
161 data->config = ret;
163 return devm_iio_device_register(&client->dev, indio_dev);
166 static const struct i2c_device_id si7005_id[] = {
167 { "si7005", 0 },
168 { "th02", 0 },
171 MODULE_DEVICE_TABLE(i2c, si7005_id);
173 static struct i2c_driver si7005_driver = {
174 .driver = {
175 .name = "si7005",
177 .probe = si7005_probe,
178 .id_table = si7005_id,
180 module_i2c_driver(si7005_driver);
182 MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
183 MODULE_DESCRIPTION("Silabs Si7005 humidity and temperature sensor driver");
184 MODULE_LICENSE("GPL");