18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * hdc100x.c - Support for the TI HDC100x temperature + humidity sensors
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2015, 2018
68c2ecf20Sopenharmony_ci * Author: Matt Ranostay <matt.ranostay@konsulko.com>
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * Datasheets:
98c2ecf20Sopenharmony_ci * https://www.ti.com/product/HDC1000/datasheet
108c2ecf20Sopenharmony_ci * https://www.ti.com/product/HDC1008/datasheet
118c2ecf20Sopenharmony_ci * https://www.ti.com/product/HDC1010/datasheet
128c2ecf20Sopenharmony_ci * https://www.ti.com/product/HDC1050/datasheet
138c2ecf20Sopenharmony_ci * https://www.ti.com/product/HDC1080/datasheet
148c2ecf20Sopenharmony_ci */
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#include <linux/delay.h>
178c2ecf20Sopenharmony_ci#include <linux/module.h>
188c2ecf20Sopenharmony_ci#include <linux/mod_devicetable.h>
198c2ecf20Sopenharmony_ci#include <linux/init.h>
208c2ecf20Sopenharmony_ci#include <linux/i2c.h>
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci#include <linux/iio/iio.h>
238c2ecf20Sopenharmony_ci#include <linux/iio/sysfs.h>
248c2ecf20Sopenharmony_ci#include <linux/iio/buffer.h>
258c2ecf20Sopenharmony_ci#include <linux/iio/trigger_consumer.h>
268c2ecf20Sopenharmony_ci#include <linux/iio/triggered_buffer.h>
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci#include <linux/time.h>
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci#define HDC100X_REG_TEMP			0x00
318c2ecf20Sopenharmony_ci#define HDC100X_REG_HUMIDITY			0x01
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci#define HDC100X_REG_CONFIG			0x02
348c2ecf20Sopenharmony_ci#define HDC100X_REG_CONFIG_ACQ_MODE		BIT(12)
358c2ecf20Sopenharmony_ci#define HDC100X_REG_CONFIG_HEATER_EN		BIT(13)
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_cistruct hdc100x_data {
388c2ecf20Sopenharmony_ci	struct i2c_client *client;
398c2ecf20Sopenharmony_ci	struct mutex lock;
408c2ecf20Sopenharmony_ci	u16 config;
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci	/* integration time of the sensor */
438c2ecf20Sopenharmony_ci	int adc_int_us[2];
448c2ecf20Sopenharmony_ci	/* Ensure natural alignment of timestamp */
458c2ecf20Sopenharmony_ci	struct {
468c2ecf20Sopenharmony_ci		__be16 channels[2];
478c2ecf20Sopenharmony_ci		s64 ts __aligned(8);
488c2ecf20Sopenharmony_ci	} scan;
498c2ecf20Sopenharmony_ci};
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci/* integration time in us */
528c2ecf20Sopenharmony_cistatic const int hdc100x_int_time[][3] = {
538c2ecf20Sopenharmony_ci	{ 6350, 3650, 0 },	/* IIO_TEMP channel*/
548c2ecf20Sopenharmony_ci	{ 6500, 3850, 2500 },	/* IIO_HUMIDITYRELATIVE channel */
558c2ecf20Sopenharmony_ci};
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci/* HDC100X_REG_CONFIG shift and mask values */
588c2ecf20Sopenharmony_cistatic const struct {
598c2ecf20Sopenharmony_ci	int shift;
608c2ecf20Sopenharmony_ci	int mask;
618c2ecf20Sopenharmony_ci} hdc100x_resolution_shift[2] = {
628c2ecf20Sopenharmony_ci	{ /* IIO_TEMP channel */
638c2ecf20Sopenharmony_ci		.shift = 10,
648c2ecf20Sopenharmony_ci		.mask = 1
658c2ecf20Sopenharmony_ci	},
668c2ecf20Sopenharmony_ci	{ /* IIO_HUMIDITYRELATIVE channel */
678c2ecf20Sopenharmony_ci		.shift = 8,
688c2ecf20Sopenharmony_ci		.mask = 3,
698c2ecf20Sopenharmony_ci	},
708c2ecf20Sopenharmony_ci};
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_cistatic IIO_CONST_ATTR(temp_integration_time_available,
738c2ecf20Sopenharmony_ci		"0.00365 0.00635");
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_cistatic IIO_CONST_ATTR(humidityrelative_integration_time_available,
768c2ecf20Sopenharmony_ci		"0.0025 0.00385 0.0065");
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_cistatic IIO_CONST_ATTR(out_current_heater_raw_available,
798c2ecf20Sopenharmony_ci		"0 1");
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_cistatic struct attribute *hdc100x_attributes[] = {
828c2ecf20Sopenharmony_ci	&iio_const_attr_temp_integration_time_available.dev_attr.attr,
838c2ecf20Sopenharmony_ci	&iio_const_attr_humidityrelative_integration_time_available.dev_attr.attr,
848c2ecf20Sopenharmony_ci	&iio_const_attr_out_current_heater_raw_available.dev_attr.attr,
858c2ecf20Sopenharmony_ci	NULL
868c2ecf20Sopenharmony_ci};
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_cistatic const struct attribute_group hdc100x_attribute_group = {
898c2ecf20Sopenharmony_ci	.attrs = hdc100x_attributes,
908c2ecf20Sopenharmony_ci};
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_cistatic const struct iio_chan_spec hdc100x_channels[] = {
938c2ecf20Sopenharmony_ci	{
948c2ecf20Sopenharmony_ci		.type = IIO_TEMP,
958c2ecf20Sopenharmony_ci		.address = HDC100X_REG_TEMP,
968c2ecf20Sopenharmony_ci		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
978c2ecf20Sopenharmony_ci			BIT(IIO_CHAN_INFO_SCALE) |
988c2ecf20Sopenharmony_ci			BIT(IIO_CHAN_INFO_INT_TIME) |
998c2ecf20Sopenharmony_ci			BIT(IIO_CHAN_INFO_OFFSET),
1008c2ecf20Sopenharmony_ci		.scan_index = 0,
1018c2ecf20Sopenharmony_ci		.scan_type = {
1028c2ecf20Sopenharmony_ci			.sign = 's',
1038c2ecf20Sopenharmony_ci			.realbits = 16,
1048c2ecf20Sopenharmony_ci			.storagebits = 16,
1058c2ecf20Sopenharmony_ci			.endianness = IIO_BE,
1068c2ecf20Sopenharmony_ci		},
1078c2ecf20Sopenharmony_ci	},
1088c2ecf20Sopenharmony_ci	{
1098c2ecf20Sopenharmony_ci		.type = IIO_HUMIDITYRELATIVE,
1108c2ecf20Sopenharmony_ci		.address = HDC100X_REG_HUMIDITY,
1118c2ecf20Sopenharmony_ci		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1128c2ecf20Sopenharmony_ci			BIT(IIO_CHAN_INFO_SCALE) |
1138c2ecf20Sopenharmony_ci			BIT(IIO_CHAN_INFO_INT_TIME),
1148c2ecf20Sopenharmony_ci		.scan_index = 1,
1158c2ecf20Sopenharmony_ci		.scan_type = {
1168c2ecf20Sopenharmony_ci			.sign = 'u',
1178c2ecf20Sopenharmony_ci			.realbits = 16,
1188c2ecf20Sopenharmony_ci			.storagebits = 16,
1198c2ecf20Sopenharmony_ci			.endianness = IIO_BE,
1208c2ecf20Sopenharmony_ci		},
1218c2ecf20Sopenharmony_ci	},
1228c2ecf20Sopenharmony_ci	{
1238c2ecf20Sopenharmony_ci		.type = IIO_CURRENT,
1248c2ecf20Sopenharmony_ci		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1258c2ecf20Sopenharmony_ci		.extend_name = "heater",
1268c2ecf20Sopenharmony_ci		.output = 1,
1278c2ecf20Sopenharmony_ci		.scan_index = -1,
1288c2ecf20Sopenharmony_ci	},
1298c2ecf20Sopenharmony_ci	IIO_CHAN_SOFT_TIMESTAMP(2),
1308c2ecf20Sopenharmony_ci};
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_cistatic const unsigned long hdc100x_scan_masks[] = {0x3, 0};
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_cistatic int hdc100x_update_config(struct hdc100x_data *data, int mask, int val)
1358c2ecf20Sopenharmony_ci{
1368c2ecf20Sopenharmony_ci	int tmp = (~mask & data->config) | val;
1378c2ecf20Sopenharmony_ci	int ret;
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	ret = i2c_smbus_write_word_swapped(data->client,
1408c2ecf20Sopenharmony_ci						HDC100X_REG_CONFIG, tmp);
1418c2ecf20Sopenharmony_ci	if (!ret)
1428c2ecf20Sopenharmony_ci		data->config = tmp;
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci	return ret;
1458c2ecf20Sopenharmony_ci}
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_cistatic int hdc100x_set_it_time(struct hdc100x_data *data, int chan, int val2)
1488c2ecf20Sopenharmony_ci{
1498c2ecf20Sopenharmony_ci	int shift = hdc100x_resolution_shift[chan].shift;
1508c2ecf20Sopenharmony_ci	int ret = -EINVAL;
1518c2ecf20Sopenharmony_ci	int i;
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(hdc100x_int_time[chan]); i++) {
1548c2ecf20Sopenharmony_ci		if (val2 && val2 == hdc100x_int_time[chan][i]) {
1558c2ecf20Sopenharmony_ci			ret = hdc100x_update_config(data,
1568c2ecf20Sopenharmony_ci				hdc100x_resolution_shift[chan].mask << shift,
1578c2ecf20Sopenharmony_ci				i << shift);
1588c2ecf20Sopenharmony_ci			if (!ret)
1598c2ecf20Sopenharmony_ci				data->adc_int_us[chan] = val2;
1608c2ecf20Sopenharmony_ci			break;
1618c2ecf20Sopenharmony_ci		}
1628c2ecf20Sopenharmony_ci	}
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci	return ret;
1658c2ecf20Sopenharmony_ci}
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_cistatic int hdc100x_get_measurement(struct hdc100x_data *data,
1688c2ecf20Sopenharmony_ci				   struct iio_chan_spec const *chan)
1698c2ecf20Sopenharmony_ci{
1708c2ecf20Sopenharmony_ci	struct i2c_client *client = data->client;
1718c2ecf20Sopenharmony_ci	int delay = data->adc_int_us[chan->address] + 1*USEC_PER_MSEC;
1728c2ecf20Sopenharmony_ci	int ret;
1738c2ecf20Sopenharmony_ci	__be16 val;
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci	/* start measurement */
1768c2ecf20Sopenharmony_ci	ret = i2c_smbus_write_byte(client, chan->address);
1778c2ecf20Sopenharmony_ci	if (ret < 0) {
1788c2ecf20Sopenharmony_ci		dev_err(&client->dev, "cannot start measurement");
1798c2ecf20Sopenharmony_ci		return ret;
1808c2ecf20Sopenharmony_ci	}
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci	/* wait for integration time to pass */
1838c2ecf20Sopenharmony_ci	usleep_range(delay, delay + 1000);
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci	/* read measurement */
1868c2ecf20Sopenharmony_ci	ret = i2c_master_recv(data->client, (char *)&val, sizeof(val));
1878c2ecf20Sopenharmony_ci	if (ret < 0) {
1888c2ecf20Sopenharmony_ci		dev_err(&client->dev, "cannot read sensor data\n");
1898c2ecf20Sopenharmony_ci		return ret;
1908c2ecf20Sopenharmony_ci	}
1918c2ecf20Sopenharmony_ci	return be16_to_cpu(val);
1928c2ecf20Sopenharmony_ci}
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_cistatic int hdc100x_get_heater_status(struct hdc100x_data *data)
1958c2ecf20Sopenharmony_ci{
1968c2ecf20Sopenharmony_ci	return !!(data->config & HDC100X_REG_CONFIG_HEATER_EN);
1978c2ecf20Sopenharmony_ci}
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_cistatic int hdc100x_read_raw(struct iio_dev *indio_dev,
2008c2ecf20Sopenharmony_ci			    struct iio_chan_spec const *chan, int *val,
2018c2ecf20Sopenharmony_ci			    int *val2, long mask)
2028c2ecf20Sopenharmony_ci{
2038c2ecf20Sopenharmony_ci	struct hdc100x_data *data = iio_priv(indio_dev);
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ci	switch (mask) {
2068c2ecf20Sopenharmony_ci	case IIO_CHAN_INFO_RAW: {
2078c2ecf20Sopenharmony_ci		int ret;
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ci		mutex_lock(&data->lock);
2108c2ecf20Sopenharmony_ci		if (chan->type == IIO_CURRENT) {
2118c2ecf20Sopenharmony_ci			*val = hdc100x_get_heater_status(data);
2128c2ecf20Sopenharmony_ci			ret = IIO_VAL_INT;
2138c2ecf20Sopenharmony_ci		} else {
2148c2ecf20Sopenharmony_ci			ret = iio_device_claim_direct_mode(indio_dev);
2158c2ecf20Sopenharmony_ci			if (ret) {
2168c2ecf20Sopenharmony_ci				mutex_unlock(&data->lock);
2178c2ecf20Sopenharmony_ci				return ret;
2188c2ecf20Sopenharmony_ci			}
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci			ret = hdc100x_get_measurement(data, chan);
2218c2ecf20Sopenharmony_ci			iio_device_release_direct_mode(indio_dev);
2228c2ecf20Sopenharmony_ci			if (ret >= 0) {
2238c2ecf20Sopenharmony_ci				*val = ret;
2248c2ecf20Sopenharmony_ci				ret = IIO_VAL_INT;
2258c2ecf20Sopenharmony_ci			}
2268c2ecf20Sopenharmony_ci		}
2278c2ecf20Sopenharmony_ci		mutex_unlock(&data->lock);
2288c2ecf20Sopenharmony_ci		return ret;
2298c2ecf20Sopenharmony_ci	}
2308c2ecf20Sopenharmony_ci	case IIO_CHAN_INFO_INT_TIME:
2318c2ecf20Sopenharmony_ci		*val = 0;
2328c2ecf20Sopenharmony_ci		*val2 = data->adc_int_us[chan->address];
2338c2ecf20Sopenharmony_ci		return IIO_VAL_INT_PLUS_MICRO;
2348c2ecf20Sopenharmony_ci	case IIO_CHAN_INFO_SCALE:
2358c2ecf20Sopenharmony_ci		if (chan->type == IIO_TEMP) {
2368c2ecf20Sopenharmony_ci			*val = 165000;
2378c2ecf20Sopenharmony_ci			*val2 = 65536;
2388c2ecf20Sopenharmony_ci			return IIO_VAL_FRACTIONAL;
2398c2ecf20Sopenharmony_ci		} else {
2408c2ecf20Sopenharmony_ci			*val = 100000;
2418c2ecf20Sopenharmony_ci			*val2 = 65536;
2428c2ecf20Sopenharmony_ci			return IIO_VAL_FRACTIONAL;
2438c2ecf20Sopenharmony_ci		}
2448c2ecf20Sopenharmony_ci		break;
2458c2ecf20Sopenharmony_ci	case IIO_CHAN_INFO_OFFSET:
2468c2ecf20Sopenharmony_ci		*val = -15887;
2478c2ecf20Sopenharmony_ci		*val2 = 515151;
2488c2ecf20Sopenharmony_ci		return IIO_VAL_INT_PLUS_MICRO;
2498c2ecf20Sopenharmony_ci	default:
2508c2ecf20Sopenharmony_ci		return -EINVAL;
2518c2ecf20Sopenharmony_ci	}
2528c2ecf20Sopenharmony_ci}
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_cistatic int hdc100x_write_raw(struct iio_dev *indio_dev,
2558c2ecf20Sopenharmony_ci			     struct iio_chan_spec const *chan,
2568c2ecf20Sopenharmony_ci			     int val, int val2, long mask)
2578c2ecf20Sopenharmony_ci{
2588c2ecf20Sopenharmony_ci	struct hdc100x_data *data = iio_priv(indio_dev);
2598c2ecf20Sopenharmony_ci	int ret = -EINVAL;
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_ci	switch (mask) {
2628c2ecf20Sopenharmony_ci	case IIO_CHAN_INFO_INT_TIME:
2638c2ecf20Sopenharmony_ci		if (val != 0)
2648c2ecf20Sopenharmony_ci			return -EINVAL;
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_ci		mutex_lock(&data->lock);
2678c2ecf20Sopenharmony_ci		ret = hdc100x_set_it_time(data, chan->address, val2);
2688c2ecf20Sopenharmony_ci		mutex_unlock(&data->lock);
2698c2ecf20Sopenharmony_ci		return ret;
2708c2ecf20Sopenharmony_ci	case IIO_CHAN_INFO_RAW:
2718c2ecf20Sopenharmony_ci		if (chan->type != IIO_CURRENT || val2 != 0)
2728c2ecf20Sopenharmony_ci			return -EINVAL;
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_ci		mutex_lock(&data->lock);
2758c2ecf20Sopenharmony_ci		ret = hdc100x_update_config(data, HDC100X_REG_CONFIG_HEATER_EN,
2768c2ecf20Sopenharmony_ci					val ? HDC100X_REG_CONFIG_HEATER_EN : 0);
2778c2ecf20Sopenharmony_ci		mutex_unlock(&data->lock);
2788c2ecf20Sopenharmony_ci		return ret;
2798c2ecf20Sopenharmony_ci	default:
2808c2ecf20Sopenharmony_ci		return -EINVAL;
2818c2ecf20Sopenharmony_ci	}
2828c2ecf20Sopenharmony_ci}
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_cistatic int hdc100x_buffer_postenable(struct iio_dev *indio_dev)
2858c2ecf20Sopenharmony_ci{
2868c2ecf20Sopenharmony_ci	struct hdc100x_data *data = iio_priv(indio_dev);
2878c2ecf20Sopenharmony_ci	int ret;
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_ci	/* Buffer is enabled. First set ACQ Mode, then attach poll func */
2908c2ecf20Sopenharmony_ci	mutex_lock(&data->lock);
2918c2ecf20Sopenharmony_ci	ret = hdc100x_update_config(data, HDC100X_REG_CONFIG_ACQ_MODE,
2928c2ecf20Sopenharmony_ci				    HDC100X_REG_CONFIG_ACQ_MODE);
2938c2ecf20Sopenharmony_ci	mutex_unlock(&data->lock);
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_ci	return ret;
2968c2ecf20Sopenharmony_ci}
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_cistatic int hdc100x_buffer_predisable(struct iio_dev *indio_dev)
2998c2ecf20Sopenharmony_ci{
3008c2ecf20Sopenharmony_ci	struct hdc100x_data *data = iio_priv(indio_dev);
3018c2ecf20Sopenharmony_ci	int ret;
3028c2ecf20Sopenharmony_ci
3038c2ecf20Sopenharmony_ci	mutex_lock(&data->lock);
3048c2ecf20Sopenharmony_ci	ret = hdc100x_update_config(data, HDC100X_REG_CONFIG_ACQ_MODE, 0);
3058c2ecf20Sopenharmony_ci	mutex_unlock(&data->lock);
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_ci	return ret;
3088c2ecf20Sopenharmony_ci}
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_cistatic const struct iio_buffer_setup_ops hdc_buffer_setup_ops = {
3118c2ecf20Sopenharmony_ci	.postenable  = hdc100x_buffer_postenable,
3128c2ecf20Sopenharmony_ci	.predisable  = hdc100x_buffer_predisable,
3138c2ecf20Sopenharmony_ci};
3148c2ecf20Sopenharmony_ci
3158c2ecf20Sopenharmony_cistatic irqreturn_t hdc100x_trigger_handler(int irq, void *p)
3168c2ecf20Sopenharmony_ci{
3178c2ecf20Sopenharmony_ci	struct iio_poll_func *pf = p;
3188c2ecf20Sopenharmony_ci	struct iio_dev *indio_dev = pf->indio_dev;
3198c2ecf20Sopenharmony_ci	struct hdc100x_data *data = iio_priv(indio_dev);
3208c2ecf20Sopenharmony_ci	struct i2c_client *client = data->client;
3218c2ecf20Sopenharmony_ci	int delay = data->adc_int_us[0] + data->adc_int_us[1] + 2*USEC_PER_MSEC;
3228c2ecf20Sopenharmony_ci	int ret;
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_ci	/* dual read starts at temp register */
3258c2ecf20Sopenharmony_ci	mutex_lock(&data->lock);
3268c2ecf20Sopenharmony_ci	ret = i2c_smbus_write_byte(client, HDC100X_REG_TEMP);
3278c2ecf20Sopenharmony_ci	if (ret < 0) {
3288c2ecf20Sopenharmony_ci		dev_err(&client->dev, "cannot start measurement\n");
3298c2ecf20Sopenharmony_ci		goto err;
3308c2ecf20Sopenharmony_ci	}
3318c2ecf20Sopenharmony_ci	usleep_range(delay, delay + 1000);
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_ci	ret = i2c_master_recv(client, (u8 *)data->scan.channels, 4);
3348c2ecf20Sopenharmony_ci	if (ret < 0) {
3358c2ecf20Sopenharmony_ci		dev_err(&client->dev, "cannot read sensor data\n");
3368c2ecf20Sopenharmony_ci		goto err;
3378c2ecf20Sopenharmony_ci	}
3388c2ecf20Sopenharmony_ci
3398c2ecf20Sopenharmony_ci	iio_push_to_buffers_with_timestamp(indio_dev, &data->scan,
3408c2ecf20Sopenharmony_ci					   iio_get_time_ns(indio_dev));
3418c2ecf20Sopenharmony_cierr:
3428c2ecf20Sopenharmony_ci	mutex_unlock(&data->lock);
3438c2ecf20Sopenharmony_ci	iio_trigger_notify_done(indio_dev->trig);
3448c2ecf20Sopenharmony_ci
3458c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
3468c2ecf20Sopenharmony_ci}
3478c2ecf20Sopenharmony_ci
3488c2ecf20Sopenharmony_cistatic const struct iio_info hdc100x_info = {
3498c2ecf20Sopenharmony_ci	.read_raw = hdc100x_read_raw,
3508c2ecf20Sopenharmony_ci	.write_raw = hdc100x_write_raw,
3518c2ecf20Sopenharmony_ci	.attrs = &hdc100x_attribute_group,
3528c2ecf20Sopenharmony_ci};
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_cistatic int hdc100x_probe(struct i2c_client *client,
3558c2ecf20Sopenharmony_ci			 const struct i2c_device_id *id)
3568c2ecf20Sopenharmony_ci{
3578c2ecf20Sopenharmony_ci	struct iio_dev *indio_dev;
3588c2ecf20Sopenharmony_ci	struct hdc100x_data *data;
3598c2ecf20Sopenharmony_ci	int ret;
3608c2ecf20Sopenharmony_ci
3618c2ecf20Sopenharmony_ci	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WORD_DATA |
3628c2ecf20Sopenharmony_ci				     I2C_FUNC_SMBUS_BYTE | I2C_FUNC_I2C))
3638c2ecf20Sopenharmony_ci		return -EOPNOTSUPP;
3648c2ecf20Sopenharmony_ci
3658c2ecf20Sopenharmony_ci	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
3668c2ecf20Sopenharmony_ci	if (!indio_dev)
3678c2ecf20Sopenharmony_ci		return -ENOMEM;
3688c2ecf20Sopenharmony_ci
3698c2ecf20Sopenharmony_ci	data = iio_priv(indio_dev);
3708c2ecf20Sopenharmony_ci	i2c_set_clientdata(client, indio_dev);
3718c2ecf20Sopenharmony_ci	data->client = client;
3728c2ecf20Sopenharmony_ci	mutex_init(&data->lock);
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_ci	indio_dev->name = dev_name(&client->dev);
3758c2ecf20Sopenharmony_ci	indio_dev->modes = INDIO_DIRECT_MODE;
3768c2ecf20Sopenharmony_ci	indio_dev->info = &hdc100x_info;
3778c2ecf20Sopenharmony_ci
3788c2ecf20Sopenharmony_ci	indio_dev->channels = hdc100x_channels;
3798c2ecf20Sopenharmony_ci	indio_dev->num_channels = ARRAY_SIZE(hdc100x_channels);
3808c2ecf20Sopenharmony_ci	indio_dev->available_scan_masks = hdc100x_scan_masks;
3818c2ecf20Sopenharmony_ci
3828c2ecf20Sopenharmony_ci	/* be sure we are in a known state */
3838c2ecf20Sopenharmony_ci	hdc100x_set_it_time(data, 0, hdc100x_int_time[0][0]);
3848c2ecf20Sopenharmony_ci	hdc100x_set_it_time(data, 1, hdc100x_int_time[1][0]);
3858c2ecf20Sopenharmony_ci	hdc100x_update_config(data, HDC100X_REG_CONFIG_ACQ_MODE, 0);
3868c2ecf20Sopenharmony_ci
3878c2ecf20Sopenharmony_ci	ret = devm_iio_triggered_buffer_setup(&client->dev,
3888c2ecf20Sopenharmony_ci					 indio_dev, NULL,
3898c2ecf20Sopenharmony_ci					 hdc100x_trigger_handler,
3908c2ecf20Sopenharmony_ci					 &hdc_buffer_setup_ops);
3918c2ecf20Sopenharmony_ci	if (ret < 0) {
3928c2ecf20Sopenharmony_ci		dev_err(&client->dev, "iio triggered buffer setup failed\n");
3938c2ecf20Sopenharmony_ci		return ret;
3948c2ecf20Sopenharmony_ci	}
3958c2ecf20Sopenharmony_ci
3968c2ecf20Sopenharmony_ci	return devm_iio_device_register(&client->dev, indio_dev);
3978c2ecf20Sopenharmony_ci}
3988c2ecf20Sopenharmony_ci
3998c2ecf20Sopenharmony_cistatic const struct i2c_device_id hdc100x_id[] = {
4008c2ecf20Sopenharmony_ci	{ "hdc100x", 0 },
4018c2ecf20Sopenharmony_ci	{ "hdc1000", 0 },
4028c2ecf20Sopenharmony_ci	{ "hdc1008", 0 },
4038c2ecf20Sopenharmony_ci	{ "hdc1010", 0 },
4048c2ecf20Sopenharmony_ci	{ "hdc1050", 0 },
4058c2ecf20Sopenharmony_ci	{ "hdc1080", 0 },
4068c2ecf20Sopenharmony_ci	{ }
4078c2ecf20Sopenharmony_ci};
4088c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, hdc100x_id);
4098c2ecf20Sopenharmony_ci
4108c2ecf20Sopenharmony_cistatic const struct of_device_id hdc100x_dt_ids[] = {
4118c2ecf20Sopenharmony_ci	{ .compatible = "ti,hdc1000" },
4128c2ecf20Sopenharmony_ci	{ .compatible = "ti,hdc1008" },
4138c2ecf20Sopenharmony_ci	{ .compatible = "ti,hdc1010" },
4148c2ecf20Sopenharmony_ci	{ .compatible = "ti,hdc1050" },
4158c2ecf20Sopenharmony_ci	{ .compatible = "ti,hdc1080" },
4168c2ecf20Sopenharmony_ci	{ }
4178c2ecf20Sopenharmony_ci};
4188c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, hdc100x_dt_ids);
4198c2ecf20Sopenharmony_ci
4208c2ecf20Sopenharmony_cistatic struct i2c_driver hdc100x_driver = {
4218c2ecf20Sopenharmony_ci	.driver = {
4228c2ecf20Sopenharmony_ci		.name	= "hdc100x",
4238c2ecf20Sopenharmony_ci		.of_match_table = hdc100x_dt_ids,
4248c2ecf20Sopenharmony_ci	},
4258c2ecf20Sopenharmony_ci	.probe = hdc100x_probe,
4268c2ecf20Sopenharmony_ci	.id_table = hdc100x_id,
4278c2ecf20Sopenharmony_ci};
4288c2ecf20Sopenharmony_cimodule_i2c_driver(hdc100x_driver);
4298c2ecf20Sopenharmony_ci
4308c2ecf20Sopenharmony_ciMODULE_AUTHOR("Matt Ranostay <matt.ranostay@konsulko.com>");
4318c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("TI HDC100x humidity and temperature sensor driver");
4328c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
433