18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * si7005.c - Support for Silabs Si7005 humidity and temperature sensor
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (c) 2014 Peter Meerwald <pmeerw@pmeerw.net>
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * (7-bit I2C slave address 0x40)
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * TODO: heater, fast mode, processed mode (temp. / linearity compensation)
108c2ecf20Sopenharmony_ci */
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#include <linux/err.h>
138c2ecf20Sopenharmony_ci#include <linux/i2c.h>
148c2ecf20Sopenharmony_ci#include <linux/delay.h>
158c2ecf20Sopenharmony_ci#include <linux/module.h>
168c2ecf20Sopenharmony_ci#include <linux/pm.h>
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci#include <linux/iio/iio.h>
198c2ecf20Sopenharmony_ci#include <linux/iio/sysfs.h>
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci#define SI7005_STATUS 0x00
228c2ecf20Sopenharmony_ci#define SI7005_DATA 0x01 /* 16-bit, MSB */
238c2ecf20Sopenharmony_ci#define SI7005_CONFIG 0x03
248c2ecf20Sopenharmony_ci#define SI7005_ID 0x11
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci#define SI7005_STATUS_NRDY BIT(0)
278c2ecf20Sopenharmony_ci#define SI7005_CONFIG_TEMP BIT(4)
288c2ecf20Sopenharmony_ci#define SI7005_CONFIG_START BIT(0)
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci#define SI7005_ID_7005 0x50
318c2ecf20Sopenharmony_ci#define SI7005_ID_7015 0xf0
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_cistruct si7005_data {
348c2ecf20Sopenharmony_ci	struct i2c_client *client;
358c2ecf20Sopenharmony_ci	struct mutex lock;
368c2ecf20Sopenharmony_ci	u8 config;
378c2ecf20Sopenharmony_ci};
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_cistatic int si7005_read_measurement(struct si7005_data *data, bool temp)
408c2ecf20Sopenharmony_ci{
418c2ecf20Sopenharmony_ci	int tries = 50;
428c2ecf20Sopenharmony_ci	int ret;
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci	mutex_lock(&data->lock);
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci	ret = i2c_smbus_write_byte_data(data->client, SI7005_CONFIG,
478c2ecf20Sopenharmony_ci		data->config | SI7005_CONFIG_START |
488c2ecf20Sopenharmony_ci		(temp ? SI7005_CONFIG_TEMP : 0));
498c2ecf20Sopenharmony_ci	if (ret < 0)
508c2ecf20Sopenharmony_ci		goto done;
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci	while (tries-- > 0) {
538c2ecf20Sopenharmony_ci		msleep(20);
548c2ecf20Sopenharmony_ci		ret = i2c_smbus_read_byte_data(data->client, SI7005_STATUS);
558c2ecf20Sopenharmony_ci		if (ret < 0)
568c2ecf20Sopenharmony_ci			goto done;
578c2ecf20Sopenharmony_ci		if (!(ret & SI7005_STATUS_NRDY))
588c2ecf20Sopenharmony_ci			break;
598c2ecf20Sopenharmony_ci	}
608c2ecf20Sopenharmony_ci	if (tries < 0) {
618c2ecf20Sopenharmony_ci		ret = -EIO;
628c2ecf20Sopenharmony_ci		goto done;
638c2ecf20Sopenharmony_ci	}
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci	ret = i2c_smbus_read_word_swapped(data->client, SI7005_DATA);
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_cidone:
688c2ecf20Sopenharmony_ci	mutex_unlock(&data->lock);
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci	return ret;
718c2ecf20Sopenharmony_ci}
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_cistatic int si7005_read_raw(struct iio_dev *indio_dev,
748c2ecf20Sopenharmony_ci			    struct iio_chan_spec const *chan, int *val,
758c2ecf20Sopenharmony_ci			    int *val2, long mask)
768c2ecf20Sopenharmony_ci{
778c2ecf20Sopenharmony_ci	struct si7005_data *data = iio_priv(indio_dev);
788c2ecf20Sopenharmony_ci	int ret;
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci	switch (mask) {
818c2ecf20Sopenharmony_ci	case IIO_CHAN_INFO_RAW:
828c2ecf20Sopenharmony_ci		ret = si7005_read_measurement(data, chan->type == IIO_TEMP);
838c2ecf20Sopenharmony_ci		if (ret < 0)
848c2ecf20Sopenharmony_ci			return ret;
858c2ecf20Sopenharmony_ci		*val = ret;
868c2ecf20Sopenharmony_ci		return IIO_VAL_INT;
878c2ecf20Sopenharmony_ci	case IIO_CHAN_INFO_SCALE:
888c2ecf20Sopenharmony_ci		if (chan->type == IIO_TEMP) {
898c2ecf20Sopenharmony_ci			*val = 7;
908c2ecf20Sopenharmony_ci			*val2 = 812500;
918c2ecf20Sopenharmony_ci		} else {
928c2ecf20Sopenharmony_ci			*val = 3;
938c2ecf20Sopenharmony_ci			*val2 = 906250;
948c2ecf20Sopenharmony_ci		}
958c2ecf20Sopenharmony_ci		return IIO_VAL_INT_PLUS_MICRO;
968c2ecf20Sopenharmony_ci	case IIO_CHAN_INFO_OFFSET:
978c2ecf20Sopenharmony_ci		if (chan->type == IIO_TEMP)
988c2ecf20Sopenharmony_ci			*val = -50 * 32 * 4;
998c2ecf20Sopenharmony_ci		else
1008c2ecf20Sopenharmony_ci			*val = -24 * 16 * 16;
1018c2ecf20Sopenharmony_ci		return IIO_VAL_INT;
1028c2ecf20Sopenharmony_ci	default:
1038c2ecf20Sopenharmony_ci		break;
1048c2ecf20Sopenharmony_ci	}
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci	return -EINVAL;
1078c2ecf20Sopenharmony_ci}
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_cistatic const struct iio_chan_spec si7005_channels[] = {
1108c2ecf20Sopenharmony_ci	{
1118c2ecf20Sopenharmony_ci		.type = IIO_HUMIDITYRELATIVE,
1128c2ecf20Sopenharmony_ci		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1138c2ecf20Sopenharmony_ci			BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_OFFSET),
1148c2ecf20Sopenharmony_ci	},
1158c2ecf20Sopenharmony_ci	{
1168c2ecf20Sopenharmony_ci		.type = IIO_TEMP,
1178c2ecf20Sopenharmony_ci		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1188c2ecf20Sopenharmony_ci			BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_OFFSET),
1198c2ecf20Sopenharmony_ci	}
1208c2ecf20Sopenharmony_ci};
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_cistatic const struct iio_info si7005_info = {
1238c2ecf20Sopenharmony_ci	.read_raw = si7005_read_raw,
1248c2ecf20Sopenharmony_ci};
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_cistatic int si7005_probe(struct i2c_client *client,
1278c2ecf20Sopenharmony_ci			 const struct i2c_device_id *id)
1288c2ecf20Sopenharmony_ci{
1298c2ecf20Sopenharmony_ci	struct iio_dev *indio_dev;
1308c2ecf20Sopenharmony_ci	struct si7005_data *data;
1318c2ecf20Sopenharmony_ci	int ret;
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WORD_DATA))
1348c2ecf20Sopenharmony_ci		return -EOPNOTSUPP;
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
1378c2ecf20Sopenharmony_ci	if (!indio_dev)
1388c2ecf20Sopenharmony_ci		return -ENOMEM;
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci	data = iio_priv(indio_dev);
1418c2ecf20Sopenharmony_ci	i2c_set_clientdata(client, indio_dev);
1428c2ecf20Sopenharmony_ci	data->client = client;
1438c2ecf20Sopenharmony_ci	mutex_init(&data->lock);
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci	indio_dev->name = dev_name(&client->dev);
1468c2ecf20Sopenharmony_ci	indio_dev->modes = INDIO_DIRECT_MODE;
1478c2ecf20Sopenharmony_ci	indio_dev->info = &si7005_info;
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	indio_dev->channels = si7005_channels;
1508c2ecf20Sopenharmony_ci	indio_dev->num_channels = ARRAY_SIZE(si7005_channels);
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci	ret = i2c_smbus_read_byte_data(client, SI7005_ID);
1538c2ecf20Sopenharmony_ci	if (ret < 0)
1548c2ecf20Sopenharmony_ci		return ret;
1558c2ecf20Sopenharmony_ci	if (ret != SI7005_ID_7005 && ret != SI7005_ID_7015)
1568c2ecf20Sopenharmony_ci		return -ENODEV;
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci	ret = i2c_smbus_read_byte_data(client, SI7005_CONFIG);
1598c2ecf20Sopenharmony_ci	if (ret < 0)
1608c2ecf20Sopenharmony_ci		return ret;
1618c2ecf20Sopenharmony_ci	data->config = ret;
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci	return devm_iio_device_register(&client->dev, indio_dev);
1648c2ecf20Sopenharmony_ci}
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_cistatic const struct i2c_device_id si7005_id[] = {
1678c2ecf20Sopenharmony_ci	{ "si7005", 0 },
1688c2ecf20Sopenharmony_ci	{ "th02", 0 },
1698c2ecf20Sopenharmony_ci	{ }
1708c2ecf20Sopenharmony_ci};
1718c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, si7005_id);
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_cistatic struct i2c_driver si7005_driver = {
1748c2ecf20Sopenharmony_ci	.driver = {
1758c2ecf20Sopenharmony_ci		.name	= "si7005",
1768c2ecf20Sopenharmony_ci	},
1778c2ecf20Sopenharmony_ci	.probe = si7005_probe,
1788c2ecf20Sopenharmony_ci	.id_table = si7005_id,
1798c2ecf20Sopenharmony_ci};
1808c2ecf20Sopenharmony_cimodule_i2c_driver(si7005_driver);
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ciMODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
1838c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Silabs Si7005 humidity and temperature sensor driver");
1848c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
185