summaryrefslogtreecommitdiffstats
path: root/drivers/iio/humidity/hdc2010.c
blob: 1381df46187c8cdba064651a55cd57f34ee5aac1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
// SPDX-License-Identifier: GPL-2.0+
/*
 * hdc2010.c - Support for the TI HDC2010 and HDC2080
 * temperature + relative humidity sensors
 *
 * Copyright (C) 2020 Norphonic AS
 * Author: Eugene Zaikonnikov <ez@norphonic.com>
 *
 * Datasheet: https://www.ti.com/product/HDC2010/datasheet
 * Datasheet: https://www.ti.com/product/HDC2080/datasheet
 */

#include <linux/module.h>
#include <linux/init.h>
#include <linux/i2c.h>
#include <linux/bitops.h>

#include <linux/iio/iio.h>
#include <linux/iio/sysfs.h>

#define HDC2010_REG_TEMP_LOW			0x00
#define HDC2010_REG_TEMP_HIGH			0x01
#define HDC2010_REG_HUMIDITY_LOW		0x02
#define HDC2010_REG_HUMIDITY_HIGH		0x03
#define HDC2010_REG_INTERRUPT_DRDY		0x04
#define HDC2010_REG_TEMP_MAX			0x05
#define HDC2010_REG_HUMIDITY_MAX		0x06
#define HDC2010_REG_INTERRUPT_EN		0x07
#define HDC2010_REG_TEMP_OFFSET_ADJ		0x08
#define HDC2010_REG_HUMIDITY_OFFSET_ADJ		0x09
#define HDC2010_REG_TEMP_THR_L			0x0a
#define HDC2010_REG_TEMP_THR_H			0x0b
#define HDC2010_REG_RH_THR_L			0x0c
#define HDC2010_REG_RH_THR_H			0x0d
#define HDC2010_REG_RESET_DRDY_INT_CONF		0x0e
#define HDC2010_REG_MEASUREMENT_CONF		0x0f

#define HDC2010_MEAS_CONF			GENMASK(2, 1)
#define HDC2010_MEAS_TRIG			BIT(0)
#define HDC2010_HEATER_EN			BIT(3)
#define HDC2010_AMM				GENMASK(6, 4)

struct hdc2010_data {
	struct i2c_client *client;
	struct mutex lock;
	u8 measurement_config;
	u8 interrupt_config;
	u8 drdy_config;
};

enum hdc2010_addr_groups {
	HDC2010_GROUP_TEMP = 0,
	HDC2010_GROUP_HUMIDITY,
};

struct hdc2010_reg_record {
	unsigned long primary;
	unsigned long peak;
};

static const struct hdc2010_reg_record hdc2010_reg_translation[] = {
	[HDC2010_GROUP_TEMP] = {
		.primary = HDC2010_REG_TEMP_LOW,
		.peak = HDC2010_REG_TEMP_MAX,
	},
	[HDC2010_GROUP_HUMIDITY] = {
		.primary = HDC2010_REG_HUMIDITY_LOW,
		.peak = HDC2010_REG_HUMIDITY_MAX,
	},
};

static IIO_CONST_ATTR(out_current_heater_raw_available, "0 1");

static struct attribute *hdc2010_attributes[] = {
	&iio_const_attr_out_current_heater_raw_available.dev_attr.attr,
	NULL
};

static const struct attribute_group hdc2010_attribute_group = {
	.attrs = hdc2010_attributes,
};

static const struct iio_chan_spec hdc2010_channels[] = {
	{
		.type = IIO_TEMP,
		.address = HDC2010_GROUP_TEMP,
		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
			BIT(IIO_CHAN_INFO_PEAK) |
			BIT(IIO_CHAN_INFO_OFFSET) |
			BIT(IIO_CHAN_INFO_SCALE),
	},
	{
		.type = IIO_HUMIDITYRELATIVE,
		.address = HDC2010_GROUP_HUMIDITY,
		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
			BIT(IIO_CHAN_INFO_PEAK) |
			BIT(IIO_CHAN_INFO_SCALE),
	},
	{
		.type = IIO_CURRENT,
		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
		.extend_name = "heater",
		.output = 1,
	},
};

static int hdc2010_update_drdy_config(struct hdc2010_data *data,
					     char mask, char val)
{
	u8 tmp = (~mask & data->drdy_config) | val;
	int ret;

	ret = i2c_smbus_write_byte_data(data->client,
					HDC2010_REG_RESET_DRDY_INT_CONF, tmp);
	if (ret)
		return ret;

	data->drdy_config = tmp;

	return 0;
}

static int hdc2010_get_prim_measurement_word(struct hdc2010_data *data,
					     struct iio_chan_spec const *chan)
{
	struct i2c_client *client = data->client;
	s32 ret;

	ret = i2c_smbus_read_word_data(client,
			hdc2010_reg_translation[chan->address].primary);

	if (ret < 0)
		dev_err(&client->dev, "Could not read sensor measurement word\n");

	return ret;
}

static int hdc2010_get_peak_measurement_byte(struct hdc2010_data *data,
					     struct iio_chan_spec const *chan)
{
	struct i2c_client *client = data->client;
	s32 ret;

	ret = i2c_smbus_read_byte_data(client,
			hdc2010_reg_translation[chan->address].peak);

	if (ret < 0)
		dev_err(&client->dev, "Could not read sensor measurement byte\n");

	return ret;
}

static int hdc2010_get_heater_status(struct hdc2010_data *data)
{
	return !!(data->drdy_config & HDC2010_HEATER_EN);
}

static int hdc2010_read_raw(struct iio_dev *indio_dev,
			    struct iio_chan_spec const *chan, int *val,
			    int *val2, long mask)
{
	struct hdc2010_data *data = iio_priv(indio_dev);

	switch (mask) {
	case IIO_CHAN_INFO_RAW: {
		int ret;

		if (chan->type == IIO_CURRENT) {
			*val = hdc2010_get_heater_status(data);
			return IIO_VAL_INT;
		}
		ret = iio_device_claim_direct_mode(indio_dev);
		if (ret)
			return ret;
		mutex_lock(&data->lock);
		ret = hdc2010_get_prim_measurement_word(data, chan);
		mutex_unlock(&data->lock);
		iio_device_release_direct_mode(indio_dev);
		if (ret < 0)
			return ret;
		*val = ret;
		return IIO_VAL_INT;
	}
	case IIO_CHAN_INFO_PEAK: {
		int ret;

		ret = iio_device_claim_direct_mode(indio_dev);
		if (ret)
			return ret;
		mutex_lock(&data->lock);
		ret = hdc2010_get_peak_measurement_byte(data, chan);
		mutex_unlock(&data->lock);
		iio_device_release_direct_mode(indio_dev);
		if (ret < 0)
			return ret;
		/* Scaling up the value so we can use same offset as RAW */
		*val = ret * 256;
		return IIO_VAL_INT;
	}
	case IIO_CHAN_INFO_SCALE:
		*val2 = 65536;
		if (chan->type == IIO_TEMP)
			*val = 165000;
		else
			*val = 100000;
		return IIO_VAL_FRACTIONAL;
	case IIO_CHAN_INFO_OFFSET:
		*val = -15887;
		*val2 = 515151;
		return IIO_VAL_INT_PLUS_MICRO;
	default:
		return -EINVAL;
	}
}

static int hdc2010_write_raw(struct iio_dev *indio_dev,
			     struct iio_chan_spec const *chan,
			     int val, int val2, long mask)
{
	struct hdc2010_data *data = iio_priv(indio_dev);
	int new, ret;

	switch (mask) {
	case IIO_CHAN_INFO_RAW:
		if (chan->type != IIO_CURRENT || val2 != 0)
			return -EINVAL;

		switch (val) {
		case 1:
			new = HDC2010_HEATER_EN;
			break;
		case 0:
			new = 0;
			break;
		default:
			return -EINVAL;
		}

		mutex_lock(&data->lock);
		ret = hdc2010_update_drdy_config(data, HDC2010_HEATER_EN, new);
		mutex_unlock(&data->lock);
		return ret;
	default:
		return -EINVAL;
	}
}

static const struct iio_info hdc2010_info = {
	.read_raw = hdc2010_read_raw,
	.write_raw = hdc2010_write_raw,
	.attrs = &hdc2010_attribute_group,
};

static int hdc2010_probe(struct i2c_client *client,
			 const struct i2c_device_id *id)
{
	struct iio_dev *indio_dev;
	struct hdc2010_data *data;
	u8 tmp;
	int ret;

	if (!i2c_check_functionality(client->adapter,
	    I2C_FUNC_SMBUS_WORD_DATA | I2C_FUNC_SMBUS_BYTE | I2C_FUNC_I2C))
		return -EOPNOTSUPP;

	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
	if (!indio_dev)
		return -ENOMEM;

	data = iio_priv(indio_dev);
	i2c_set_clientdata(client, indio_dev);
	data->client = client;
	mutex_init(&data->lock);

	/*
	 * As DEVICE ID register does not differentiate between
	 * HDC2010 and HDC2080, we have the name hardcoded
	 */
	indio_dev->name = "hdc2010";
	indio_dev->modes = INDIO_DIRECT_MODE;
	indio_dev->info = &hdc2010_info;

	indio_dev->channels = hdc2010_channels;
	indio_dev->num_channels = ARRAY_SIZE(hdc2010_channels);

	/* Enable Automatic Measurement Mode at 5Hz */
	ret = hdc2010_update_drdy_config(data, HDC2010_AMM, HDC2010_AMM);
	if (ret)
		return ret;

	/*
	 * We enable both temp and humidity measurement.
	 * However the measurement won't start even in AMM until triggered.
	 */
	tmp = (data->measurement_config & ~HDC2010_MEAS_CONF) |
		HDC2010_MEAS_TRIG;

	ret = i2c_smbus_write_byte_data(client, HDC2010_REG_MEASUREMENT_CONF, tmp);
	if (ret) {
		dev_warn(&client->dev, "Unable to set up measurement\n");
		if (hdc2010_update_drdy_config(data, HDC2010_AMM, 0))
			dev_warn(&client->dev, "Unable to restore default AMM\n");
		return ret;
	}

	data->measurement_config = tmp;

	return iio_device_register(indio_dev);
}

static int hdc2010_remove(struct i2c_client *client)
{
	struct iio_dev *indio_dev = i2c_get_clientdata(client);
	struct hdc2010_data *data = iio_priv(indio_dev);

	iio_device_unregister(indio_dev);

	/* Disable Automatic Measurement Mode */
	if (hdc2010_update_drdy_config(data, HDC2010_AMM, 0))
		dev_warn(&client->dev, "Unable to restore default AMM\n");

	return 0;
}

static const struct i2c_device_id hdc2010_id[] = {
	{ "hdc2010" },
	{ "hdc2080" },
	{ }
};
MODULE_DEVICE_TABLE(i2c, hdc2010_id);

static const struct of_device_id hdc2010_dt_ids[] = {
	{ .compatible = "ti,hdc2010" },
	{ .compatible = "ti,hdc2080" },
	{ }
};
MODULE_DEVICE_TABLE(of, hdc2010_dt_ids);

static struct i2c_driver hdc2010_driver = {
	.driver = {
		.name	= "hdc2010",
		.of_match_table = hdc2010_dt_ids,
	},
	.probe = hdc2010_probe,
	.remove = hdc2010_remove,
	.id_table = hdc2010_id,
};
module_i2c_driver(hdc2010_driver);

MODULE_AUTHOR("Eugene Zaikonnikov <ez@norphonic.com>");
MODULE_DESCRIPTION("TI HDC2010 humidity and temperature sensor driver");
MODULE_LICENSE("GPL");