1 // SPDX-License-Identifier: GPL-2.0+
3 * hdc3020.c - Support for the TI HDC3020,HDC3021 and HDC3022
4 * temperature + relative humidity sensors
8 * Datasheet: https://www.ti.com/lit/ds/symlink/hdc3020.pdf
11 #include <linux/bitops.h>
12 #include <linux/cleanup.h>
13 #include <linux/crc8.h>
14 #include <linux/delay.h>
15 #include <linux/i2c.h>
16 #include <linux/init.h>
17 #include <linux/module.h>
18 #include <linux/mutex.h>
20 #include <asm/unaligned.h>
22 #include <linux/iio/iio.h>
24 #define HDC3020_HEATER_CMD_MSB 0x30 /* shared by all heater commands */
25 #define HDC3020_HEATER_ENABLE 0x6D
26 #define HDC3020_HEATER_DISABLE 0x66
27 #define HDC3020_HEATER_CONFIG 0x6E
29 #define HDC3020_READ_RETRY_TIMES 10
30 #define HDC3020_BUSY_DELAY_MS 10
32 #define HDC3020_CRC8_POLYNOMIAL 0x31
34 static const u8 HDC3020_S_AUTO_10HZ_MOD0[2] = { 0x27, 0x37 };
36 static const u8 HDC3020_EXIT_AUTO[2] = { 0x30, 0x93 };
38 static const u8 HDC3020_R_T_RH_AUTO[2] = { 0xE0, 0x00 };
39 static const u8 HDC3020_R_T_LOW_AUTO[2] = { 0xE0, 0x02 };
40 static const u8 HDC3020_R_T_HIGH_AUTO[2] = { 0xE0, 0x03 };
41 static const u8 HDC3020_R_RH_LOW_AUTO[2] = { 0xE0, 0x04 };
42 static const u8 HDC3020_R_RH_HIGH_AUTO[2] = { 0xE0, 0x05 };
45 struct i2c_client *client;
47 * Ensure that the sensor configuration (currently only heater is
48 * supported) will not be changed during the process of reading
49 * sensor data (this driver will try HDC3020_READ_RETRY_TIMES times
50 * if the device does not respond).
55 static const int hdc3020_heater_vals[] = {0, 1, 0x3FFF};
57 static const struct iio_chan_spec hdc3020_channels[] = {
60 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
61 BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_PEAK) |
62 BIT(IIO_CHAN_INFO_TROUGH) | BIT(IIO_CHAN_INFO_OFFSET),
65 .type = IIO_HUMIDITYRELATIVE,
66 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
67 BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_PEAK) |
68 BIT(IIO_CHAN_INFO_TROUGH),
72 * For setting the internal heater, which can be switched on to
73 * prevent or remove any condensation that may develop when the
74 * ambient environment approaches its dew point temperature.
77 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
78 .info_mask_separate_available = BIT(IIO_CHAN_INFO_RAW),
83 DECLARE_CRC8_TABLE(hdc3020_crc8_table);
85 static int hdc3020_write_bytes(struct hdc3020_data *data, const u8 *buf, u8 len)
87 struct i2c_client *client = data->client;
91 msg.addr = client->addr;
93 msg.buf = (char *)buf;
97 * During the measurement process, HDC3020 will not return data.
98 * So wait for a while and try again
100 for (cnt = 0; cnt < HDC3020_READ_RETRY_TIMES; cnt++) {
101 ret = i2c_transfer(client->adapter, &msg, 1);
105 mdelay(HDC3020_BUSY_DELAY_MS);
107 dev_err(&client->dev, "Could not write sensor command\n");
112 static int hdc3020_read_bytes(struct hdc3020_data *data, const u8 *buf,
116 struct i2c_client *client = data->client;
117 struct i2c_msg msg[2] = {
119 .addr = client->addr,
125 .addr = client->addr,
133 * During the measurement process, HDC3020 will not return data.
134 * So wait for a while and try again
136 for (cnt = 0; cnt < HDC3020_READ_RETRY_TIMES; cnt++) {
137 ret = i2c_transfer(client->adapter, msg, 2);
141 mdelay(HDC3020_BUSY_DELAY_MS);
143 dev_err(&client->dev, "Could not read sensor data\n");
148 static int hdc3020_read_measurement(struct hdc3020_data *data,
149 enum iio_chan_type type, int *val)
154 ret = hdc3020_read_bytes(data, HDC3020_R_T_RH_AUTO, buf, 6);
158 /* CRC check of the temperature measurement */
159 crc = crc8(hdc3020_crc8_table, buf, 2, CRC8_INIT_VALUE);
163 /* CRC check of the relative humidity measurement */
164 crc = crc8(hdc3020_crc8_table, buf + 3, 2, CRC8_INIT_VALUE);
168 if (type == IIO_TEMP)
169 *val = get_unaligned_be16(buf);
170 else if (type == IIO_HUMIDITYRELATIVE)
171 *val = get_unaligned_be16(&buf[3]);
179 * After exiting the automatic measurement mode or resetting, the peak
180 * value will be reset to the default value
181 * This method is used to get the highest temp measured during automatic
184 static int hdc3020_read_high_peak_t(struct hdc3020_data *data, int *val)
189 ret = hdc3020_read_bytes(data, HDC3020_R_T_HIGH_AUTO, buf, 3);
193 crc = crc8(hdc3020_crc8_table, buf, 2, CRC8_INIT_VALUE);
197 *val = get_unaligned_be16(buf);
203 * This method is used to get the lowest temp measured during automatic
206 static int hdc3020_read_low_peak_t(struct hdc3020_data *data, int *val)
211 ret = hdc3020_read_bytes(data, HDC3020_R_T_LOW_AUTO, buf, 3);
215 crc = crc8(hdc3020_crc8_table, buf, 2, CRC8_INIT_VALUE);
219 *val = get_unaligned_be16(buf);
225 * This method is used to get the highest humidity measured during automatic
228 static int hdc3020_read_high_peak_rh(struct hdc3020_data *data, int *val)
233 ret = hdc3020_read_bytes(data, HDC3020_R_RH_HIGH_AUTO, buf, 3);
237 crc = crc8(hdc3020_crc8_table, buf, 2, CRC8_INIT_VALUE);
241 *val = get_unaligned_be16(buf);
247 * This method is used to get the lowest humidity measured during automatic
250 static int hdc3020_read_low_peak_rh(struct hdc3020_data *data, int *val)
255 ret = hdc3020_read_bytes(data, HDC3020_R_RH_LOW_AUTO, buf, 3);
259 crc = crc8(hdc3020_crc8_table, buf, 2, CRC8_INIT_VALUE);
263 *val = get_unaligned_be16(buf);
268 static int hdc3020_read_raw(struct iio_dev *indio_dev,
269 struct iio_chan_spec const *chan, int *val,
270 int *val2, long mask)
272 struct hdc3020_data *data = iio_priv(indio_dev);
275 if (chan->type != IIO_TEMP && chan->type != IIO_HUMIDITYRELATIVE)
279 case IIO_CHAN_INFO_RAW: {
280 guard(mutex)(&data->lock);
281 ret = hdc3020_read_measurement(data, chan->type, val);
287 case IIO_CHAN_INFO_PEAK: {
288 guard(mutex)(&data->lock);
289 if (chan->type == IIO_TEMP) {
290 ret = hdc3020_read_high_peak_t(data, val);
294 ret = hdc3020_read_high_peak_rh(data, val);
300 case IIO_CHAN_INFO_TROUGH: {
301 guard(mutex)(&data->lock);
302 if (chan->type == IIO_TEMP) {
303 ret = hdc3020_read_low_peak_t(data, val);
307 ret = hdc3020_read_low_peak_rh(data, val);
313 case IIO_CHAN_INFO_SCALE:
315 if (chan->type == IIO_TEMP)
319 return IIO_VAL_FRACTIONAL;
321 case IIO_CHAN_INFO_OFFSET:
322 if (chan->type != IIO_TEMP)
333 static int hdc3020_read_available(struct iio_dev *indio_dev,
334 struct iio_chan_spec const *chan,
336 int *type, int *length, long mask)
338 if (mask != IIO_CHAN_INFO_RAW || chan->type != IIO_CURRENT)
341 *vals = hdc3020_heater_vals;
344 return IIO_AVAIL_RANGE;
347 static int hdc3020_update_heater(struct hdc3020_data *data, int val)
352 if (val < hdc3020_heater_vals[0] || val > hdc3020_heater_vals[2])
355 buf[0] = HDC3020_HEATER_CMD_MSB;
358 buf[1] = HDC3020_HEATER_DISABLE;
359 return hdc3020_write_bytes(data, buf, 2);
362 buf[1] = HDC3020_HEATER_CONFIG;
363 put_unaligned_be16(val & GENMASK(13, 0), &buf[2]);
364 buf[4] = crc8(hdc3020_crc8_table, buf + 2, 2, CRC8_INIT_VALUE);
365 ret = hdc3020_write_bytes(data, buf, 5);
369 buf[1] = HDC3020_HEATER_ENABLE;
371 return hdc3020_write_bytes(data, buf, 2);
374 static int hdc3020_write_raw(struct iio_dev *indio_dev,
375 struct iio_chan_spec const *chan,
376 int val, int val2, long mask)
378 struct hdc3020_data *data = iio_priv(indio_dev);
381 case IIO_CHAN_INFO_RAW:
382 if (chan->type != IIO_CURRENT)
385 guard(mutex)(&data->lock);
386 return hdc3020_update_heater(data, val);
392 static const struct iio_info hdc3020_info = {
393 .read_raw = hdc3020_read_raw,
394 .write_raw = hdc3020_write_raw,
395 .read_avail = hdc3020_read_available,
398 static void hdc3020_stop(void *data)
400 hdc3020_write_bytes((struct hdc3020_data *)data, HDC3020_EXIT_AUTO, 2);
403 static int hdc3020_probe(struct i2c_client *client)
405 struct iio_dev *indio_dev;
406 struct hdc3020_data *data;
409 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
412 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
416 data = iio_priv(indio_dev);
417 data->client = client;
418 mutex_init(&data->lock);
420 crc8_populate_msb(hdc3020_crc8_table, HDC3020_CRC8_POLYNOMIAL);
422 indio_dev->name = "hdc3020";
423 indio_dev->modes = INDIO_DIRECT_MODE;
424 indio_dev->info = &hdc3020_info;
425 indio_dev->channels = hdc3020_channels;
426 indio_dev->num_channels = ARRAY_SIZE(hdc3020_channels);
428 ret = hdc3020_write_bytes(data, HDC3020_S_AUTO_10HZ_MOD0, 2);
430 return dev_err_probe(&client->dev, ret,
431 "Unable to set up measurement\n");
433 ret = devm_add_action_or_reset(&data->client->dev, hdc3020_stop, data);
437 ret = devm_iio_device_register(&data->client->dev, indio_dev);
439 return dev_err_probe(&client->dev, ret, "Failed to add device");
444 static const struct i2c_device_id hdc3020_id[] = {
450 MODULE_DEVICE_TABLE(i2c, hdc3020_id);
452 static const struct of_device_id hdc3020_dt_ids[] = {
453 { .compatible = "ti,hdc3020" },
454 { .compatible = "ti,hdc3021" },
455 { .compatible = "ti,hdc3022" },
458 MODULE_DEVICE_TABLE(of, hdc3020_dt_ids);
460 static struct i2c_driver hdc3020_driver = {
463 .of_match_table = hdc3020_dt_ids,
465 .probe = hdc3020_probe,
466 .id_table = hdc3020_id,
468 module_i2c_driver(hdc3020_driver);
470 MODULE_AUTHOR("Javier Carrasco <javier.carrasco.cruz@gmail.com>");
471 MODULE_AUTHOR("Li peiyu <579lpy@gmail.com>");
472 MODULE_DESCRIPTION("TI HDC3020 humidity and temperature sensor driver");
473 MODULE_LICENSE("GPL");