module: Convert symbol namespace to string literal
[linux.git] / drivers / iio / humidity / si7005.c
blob0797ece1fcba6dd228f2bde4f043715b85c1bcb9
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)
128 struct iio_dev *indio_dev;
129 struct si7005_data *data;
130 int ret;
132 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WORD_DATA))
133 return -EOPNOTSUPP;
135 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
136 if (!indio_dev)
137 return -ENOMEM;
139 data = iio_priv(indio_dev);
140 i2c_set_clientdata(client, indio_dev);
141 data->client = client;
142 mutex_init(&data->lock);
144 indio_dev->name = dev_name(&client->dev);
145 indio_dev->modes = INDIO_DIRECT_MODE;
146 indio_dev->info = &si7005_info;
148 indio_dev->channels = si7005_channels;
149 indio_dev->num_channels = ARRAY_SIZE(si7005_channels);
151 ret = i2c_smbus_read_byte_data(client, SI7005_ID);
152 if (ret < 0)
153 return ret;
154 if (ret != SI7005_ID_7005 && ret != SI7005_ID_7015)
155 return -ENODEV;
157 ret = i2c_smbus_read_byte_data(client, SI7005_CONFIG);
158 if (ret < 0)
159 return ret;
160 data->config = ret;
162 return devm_iio_device_register(&client->dev, indio_dev);
165 static const struct i2c_device_id si7005_id[] = {
166 { "si7005" },
167 { "th02" },
170 MODULE_DEVICE_TABLE(i2c, si7005_id);
172 static const struct of_device_id si7005_dt_ids[] = {
173 { .compatible = "silabs,si7005" },
176 MODULE_DEVICE_TABLE(of, si7005_dt_ids);
178 static struct i2c_driver si7005_driver = {
179 .driver = {
180 .name = "si7005",
181 .of_match_table = si7005_dt_ids,
183 .probe = si7005_probe,
184 .id_table = si7005_id,
186 module_i2c_driver(si7005_driver);
188 MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
189 MODULE_DESCRIPTION("Silabs Si7005 humidity and temperature sensor driver");
190 MODULE_LICENSE("GPL");