18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * si7020.c - Silicon Labs Si7013/20/21 Relative Humidity and Temp Sensors
48c2ecf20Sopenharmony_ci * Copyright (c) 2013,2014  Uplogix, Inc.
58c2ecf20Sopenharmony_ci * David Barksdale <dbarksdale@uplogix.com>
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci/*
98c2ecf20Sopenharmony_ci * The Silicon Labs Si7013/20/21 Relative Humidity and Temperature Sensors
108c2ecf20Sopenharmony_ci * are i2c devices which have an identical programming interface for
118c2ecf20Sopenharmony_ci * measuring relative humidity and temperature. The Si7013 has an additional
128c2ecf20Sopenharmony_ci * temperature input which this driver does not support.
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci * Data Sheets:
158c2ecf20Sopenharmony_ci *   Si7013: http://www.silabs.com/Support%20Documents/TechnicalDocs/Si7013.pdf
168c2ecf20Sopenharmony_ci *   Si7020: http://www.silabs.com/Support%20Documents/TechnicalDocs/Si7020.pdf
178c2ecf20Sopenharmony_ci *   Si7021: http://www.silabs.com/Support%20Documents/TechnicalDocs/Si7021.pdf
188c2ecf20Sopenharmony_ci */
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci#include <linux/delay.h>
218c2ecf20Sopenharmony_ci#include <linux/i2c.h>
228c2ecf20Sopenharmony_ci#include <linux/module.h>
238c2ecf20Sopenharmony_ci#include <linux/mod_devicetable.h>
248c2ecf20Sopenharmony_ci#include <linux/slab.h>
258c2ecf20Sopenharmony_ci#include <linux/sysfs.h>
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci#include <linux/iio/iio.h>
288c2ecf20Sopenharmony_ci#include <linux/iio/sysfs.h>
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci/* Measure Relative Humidity, Hold Master Mode */
318c2ecf20Sopenharmony_ci#define SI7020CMD_RH_HOLD	0xE5
328c2ecf20Sopenharmony_ci/* Measure Temperature, Hold Master Mode */
338c2ecf20Sopenharmony_ci#define SI7020CMD_TEMP_HOLD	0xE3
348c2ecf20Sopenharmony_ci/* Software Reset */
358c2ecf20Sopenharmony_ci#define SI7020CMD_RESET		0xFE
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_cistatic int si7020_read_raw(struct iio_dev *indio_dev,
388c2ecf20Sopenharmony_ci			   struct iio_chan_spec const *chan, int *val,
398c2ecf20Sopenharmony_ci			   int *val2, long mask)
408c2ecf20Sopenharmony_ci{
418c2ecf20Sopenharmony_ci	struct i2c_client **client = iio_priv(indio_dev);
428c2ecf20Sopenharmony_ci	int ret;
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci	switch (mask) {
458c2ecf20Sopenharmony_ci	case IIO_CHAN_INFO_RAW:
468c2ecf20Sopenharmony_ci		ret = i2c_smbus_read_word_swapped(*client,
478c2ecf20Sopenharmony_ci						  chan->type == IIO_TEMP ?
488c2ecf20Sopenharmony_ci						  SI7020CMD_TEMP_HOLD :
498c2ecf20Sopenharmony_ci						  SI7020CMD_RH_HOLD);
508c2ecf20Sopenharmony_ci		if (ret < 0)
518c2ecf20Sopenharmony_ci			return ret;
528c2ecf20Sopenharmony_ci		*val = ret >> 2;
538c2ecf20Sopenharmony_ci		/*
548c2ecf20Sopenharmony_ci		 * Humidity values can slightly exceed the 0-100%RH
558c2ecf20Sopenharmony_ci		 * range and should be corrected by software
568c2ecf20Sopenharmony_ci		 */
578c2ecf20Sopenharmony_ci		if (chan->type == IIO_HUMIDITYRELATIVE)
588c2ecf20Sopenharmony_ci			*val = clamp_val(*val, 786, 13893);
598c2ecf20Sopenharmony_ci		return IIO_VAL_INT;
608c2ecf20Sopenharmony_ci	case IIO_CHAN_INFO_SCALE:
618c2ecf20Sopenharmony_ci		if (chan->type == IIO_TEMP)
628c2ecf20Sopenharmony_ci			*val = 175720; /* = 175.72 * 1000 */
638c2ecf20Sopenharmony_ci		else
648c2ecf20Sopenharmony_ci			*val = 125 * 1000;
658c2ecf20Sopenharmony_ci		*val2 = 65536 >> 2;
668c2ecf20Sopenharmony_ci		return IIO_VAL_FRACTIONAL;
678c2ecf20Sopenharmony_ci	case IIO_CHAN_INFO_OFFSET:
688c2ecf20Sopenharmony_ci		/*
698c2ecf20Sopenharmony_ci		 * Since iio_convert_raw_to_processed_unlocked assumes offset
708c2ecf20Sopenharmony_ci		 * is an integer we have to round these values and lose
718c2ecf20Sopenharmony_ci		 * accuracy.
728c2ecf20Sopenharmony_ci		 * Relative humidity will be 0.0032959% too high and
738c2ecf20Sopenharmony_ci		 * temperature will be 0.00277344 degrees too high.
748c2ecf20Sopenharmony_ci		 * This is no big deal because it's within the accuracy of the
758c2ecf20Sopenharmony_ci		 * sensor.
768c2ecf20Sopenharmony_ci		 */
778c2ecf20Sopenharmony_ci		if (chan->type == IIO_TEMP)
788c2ecf20Sopenharmony_ci			*val = -4368; /* = -46.85 * (65536 >> 2) / 175.72 */
798c2ecf20Sopenharmony_ci		else
808c2ecf20Sopenharmony_ci			*val = -786; /* = -6 * (65536 >> 2) / 125 */
818c2ecf20Sopenharmony_ci		return IIO_VAL_INT;
828c2ecf20Sopenharmony_ci	default:
838c2ecf20Sopenharmony_ci		break;
848c2ecf20Sopenharmony_ci	}
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci	return -EINVAL;
878c2ecf20Sopenharmony_ci}
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_cistatic const struct iio_chan_spec si7020_channels[] = {
908c2ecf20Sopenharmony_ci	{
918c2ecf20Sopenharmony_ci		.type = IIO_HUMIDITYRELATIVE,
928c2ecf20Sopenharmony_ci		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
938c2ecf20Sopenharmony_ci			BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_OFFSET),
948c2ecf20Sopenharmony_ci	},
958c2ecf20Sopenharmony_ci	{
968c2ecf20Sopenharmony_ci		.type = IIO_TEMP,
978c2ecf20Sopenharmony_ci		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
988c2ecf20Sopenharmony_ci			BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_OFFSET),
998c2ecf20Sopenharmony_ci	}
1008c2ecf20Sopenharmony_ci};
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_cistatic const struct iio_info si7020_info = {
1038c2ecf20Sopenharmony_ci	.read_raw = si7020_read_raw,
1048c2ecf20Sopenharmony_ci};
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_cistatic int si7020_probe(struct i2c_client *client,
1078c2ecf20Sopenharmony_ci			const struct i2c_device_id *id)
1088c2ecf20Sopenharmony_ci{
1098c2ecf20Sopenharmony_ci	struct iio_dev *indio_dev;
1108c2ecf20Sopenharmony_ci	struct i2c_client **data;
1118c2ecf20Sopenharmony_ci	int ret;
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	if (!i2c_check_functionality(client->adapter,
1148c2ecf20Sopenharmony_ci				     I2C_FUNC_SMBUS_WRITE_BYTE |
1158c2ecf20Sopenharmony_ci				     I2C_FUNC_SMBUS_READ_WORD_DATA))
1168c2ecf20Sopenharmony_ci		return -EOPNOTSUPP;
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci	/* Reset device, loads default settings. */
1198c2ecf20Sopenharmony_ci	ret = i2c_smbus_write_byte(client, SI7020CMD_RESET);
1208c2ecf20Sopenharmony_ci	if (ret < 0)
1218c2ecf20Sopenharmony_ci		return ret;
1228c2ecf20Sopenharmony_ci	/* Wait the maximum power-up time after software reset. */
1238c2ecf20Sopenharmony_ci	msleep(15);
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
1268c2ecf20Sopenharmony_ci	if (!indio_dev)
1278c2ecf20Sopenharmony_ci		return -ENOMEM;
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci	data = iio_priv(indio_dev);
1308c2ecf20Sopenharmony_ci	*data = client;
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci	indio_dev->name = dev_name(&client->dev);
1338c2ecf20Sopenharmony_ci	indio_dev->modes = INDIO_DIRECT_MODE;
1348c2ecf20Sopenharmony_ci	indio_dev->info = &si7020_info;
1358c2ecf20Sopenharmony_ci	indio_dev->channels = si7020_channels;
1368c2ecf20Sopenharmony_ci	indio_dev->num_channels = ARRAY_SIZE(si7020_channels);
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci	return devm_iio_device_register(&client->dev, indio_dev);
1398c2ecf20Sopenharmony_ci}
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_cistatic const struct i2c_device_id si7020_id[] = {
1428c2ecf20Sopenharmony_ci	{ "si7020", 0 },
1438c2ecf20Sopenharmony_ci	{ "th06", 0 },
1448c2ecf20Sopenharmony_ci	{ }
1458c2ecf20Sopenharmony_ci};
1468c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, si7020_id);
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_cistatic const struct of_device_id si7020_dt_ids[] = {
1498c2ecf20Sopenharmony_ci	{ .compatible = "silabs,si7020" },
1508c2ecf20Sopenharmony_ci	{ }
1518c2ecf20Sopenharmony_ci};
1528c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, si7020_dt_ids);
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_cistatic struct i2c_driver si7020_driver = {
1558c2ecf20Sopenharmony_ci	.driver = {
1568c2ecf20Sopenharmony_ci		.name = "si7020",
1578c2ecf20Sopenharmony_ci		.of_match_table = si7020_dt_ids,
1588c2ecf20Sopenharmony_ci	},
1598c2ecf20Sopenharmony_ci	.probe		= si7020_probe,
1608c2ecf20Sopenharmony_ci	.id_table	= si7020_id,
1618c2ecf20Sopenharmony_ci};
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_cimodule_i2c_driver(si7020_driver);
1648c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Silicon Labs Si7013/20/21 Relative Humidity and Temperature Sensors");
1658c2ecf20Sopenharmony_ciMODULE_AUTHOR("David Barksdale <dbarksdale@uplogix.com>");
1668c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
167