1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * hdc100x.c - Support for the TI HDC100x temperature + humidity sensors
4  *
5  * Copyright (C) 2015, 2018
6  * Author: Matt Ranostay <matt.ranostay@konsulko.com>
7  *
8  * Datasheets:
9  * http://www.ti.com/product/HDC1000/datasheet
10  * http://www.ti.com/product/HDC1008/datasheet
11  * http://www.ti.com/product/HDC1010/datasheet
12  * http://www.ti.com/product/HDC1050/datasheet
13  * http://www.ti.com/product/HDC1080/datasheet
14  */
15 
16 #include <linux/delay.h>
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/i2c.h>
20 
21 #include <linux/iio/iio.h>
22 #include <linux/iio/sysfs.h>
23 #include <linux/iio/buffer.h>
24 #include <linux/iio/trigger_consumer.h>
25 #include <linux/iio/triggered_buffer.h>
26 
27 #include <linux/time.h>
28 
29 #define HDC100X_REG_TEMP			0x00
30 #define HDC100X_REG_HUMIDITY			0x01
31 
32 #define HDC100X_REG_CONFIG			0x02
33 #define HDC100X_REG_CONFIG_ACQ_MODE		BIT(12)
34 #define HDC100X_REG_CONFIG_HEATER_EN		BIT(13)
35 
36 struct hdc100x_data {
37 	struct i2c_client *client;
38 	struct mutex lock;
39 	u16 config;
40 
41 	/* integration time of the sensor */
42 	int adc_int_us[2];
43 	/* Ensure natural alignment of timestamp */
44 	struct {
45 		__be16 channels[2];
46 		s64 ts __aligned(8);
47 	} scan;
48 };
49 
50 /* integration time in us */
51 static const int hdc100x_int_time[][3] = {
52 	{ 6350, 3650, 0 },	/* IIO_TEMP channel*/
53 	{ 6500, 3850, 2500 },	/* IIO_HUMIDITYRELATIVE channel */
54 };
55 
56 /* HDC100X_REG_CONFIG shift and mask values */
57 static const struct {
58 	int shift;
59 	int mask;
60 } hdc100x_resolution_shift[2] = {
61 	{ /* IIO_TEMP channel */
62 		.shift = 10,
63 		.mask = 1
64 	},
65 	{ /* IIO_HUMIDITYRELATIVE channel */
66 		.shift = 8,
67 		.mask = 3,
68 	},
69 };
70 
71 static IIO_CONST_ATTR(temp_integration_time_available,
72 		"0.00365 0.00635");
73 
74 static IIO_CONST_ATTR(humidityrelative_integration_time_available,
75 		"0.0025 0.00385 0.0065");
76 
77 static IIO_CONST_ATTR(out_current_heater_raw_available,
78 		"0 1");
79 
80 static struct attribute *hdc100x_attributes[] = {
81 	&iio_const_attr_temp_integration_time_available.dev_attr.attr,
82 	&iio_const_attr_humidityrelative_integration_time_available.dev_attr.attr,
83 	&iio_const_attr_out_current_heater_raw_available.dev_attr.attr,
84 	NULL
85 };
86 
87 static const struct attribute_group hdc100x_attribute_group = {
88 	.attrs = hdc100x_attributes,
89 };
90 
91 static const struct iio_chan_spec hdc100x_channels[] = {
92 	{
93 		.type = IIO_TEMP,
94 		.address = HDC100X_REG_TEMP,
95 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
96 			BIT(IIO_CHAN_INFO_SCALE) |
97 			BIT(IIO_CHAN_INFO_INT_TIME) |
98 			BIT(IIO_CHAN_INFO_OFFSET),
99 		.scan_index = 0,
100 		.scan_type = {
101 			.sign = 's',
102 			.realbits = 16,
103 			.storagebits = 16,
104 			.endianness = IIO_BE,
105 		},
106 	},
107 	{
108 		.type = IIO_HUMIDITYRELATIVE,
109 		.address = HDC100X_REG_HUMIDITY,
110 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
111 			BIT(IIO_CHAN_INFO_SCALE) |
112 			BIT(IIO_CHAN_INFO_INT_TIME),
113 		.scan_index = 1,
114 		.scan_type = {
115 			.sign = 'u',
116 			.realbits = 16,
117 			.storagebits = 16,
118 			.endianness = IIO_BE,
119 		},
120 	},
121 	{
122 		.type = IIO_CURRENT,
123 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
124 		.extend_name = "heater",
125 		.output = 1,
126 		.scan_index = -1,
127 	},
128 	IIO_CHAN_SOFT_TIMESTAMP(2),
129 };
130 
131 static const unsigned long hdc100x_scan_masks[] = {0x3, 0};
132 
hdc100x_update_config(struct hdc100x_data * data,int mask,int val)133 static int hdc100x_update_config(struct hdc100x_data *data, int mask, int val)
134 {
135 	int tmp = (~mask & data->config) | val;
136 	int ret;
137 
138 	ret = i2c_smbus_write_word_swapped(data->client,
139 						HDC100X_REG_CONFIG, tmp);
140 	if (!ret)
141 		data->config = tmp;
142 
143 	return ret;
144 }
145 
hdc100x_set_it_time(struct hdc100x_data * data,int chan,int val2)146 static int hdc100x_set_it_time(struct hdc100x_data *data, int chan, int val2)
147 {
148 	int shift = hdc100x_resolution_shift[chan].shift;
149 	int ret = -EINVAL;
150 	int i;
151 
152 	for (i = 0; i < ARRAY_SIZE(hdc100x_int_time[chan]); i++) {
153 		if (val2 && val2 == hdc100x_int_time[chan][i]) {
154 			ret = hdc100x_update_config(data,
155 				hdc100x_resolution_shift[chan].mask << shift,
156 				i << shift);
157 			if (!ret)
158 				data->adc_int_us[chan] = val2;
159 			break;
160 		}
161 	}
162 
163 	return ret;
164 }
165 
hdc100x_get_measurement(struct hdc100x_data * data,struct iio_chan_spec const * chan)166 static int hdc100x_get_measurement(struct hdc100x_data *data,
167 				   struct iio_chan_spec const *chan)
168 {
169 	struct i2c_client *client = data->client;
170 	int delay = data->adc_int_us[chan->address] + 1*USEC_PER_MSEC;
171 	int ret;
172 	__be16 val;
173 
174 	/* start measurement */
175 	ret = i2c_smbus_write_byte(client, chan->address);
176 	if (ret < 0) {
177 		dev_err(&client->dev, "cannot start measurement");
178 		return ret;
179 	}
180 
181 	/* wait for integration time to pass */
182 	usleep_range(delay, delay + 1000);
183 
184 	/* read measurement */
185 	ret = i2c_master_recv(data->client, (char *)&val, sizeof(val));
186 	if (ret < 0) {
187 		dev_err(&client->dev, "cannot read sensor data\n");
188 		return ret;
189 	}
190 	return be16_to_cpu(val);
191 }
192 
hdc100x_get_heater_status(struct hdc100x_data * data)193 static int hdc100x_get_heater_status(struct hdc100x_data *data)
194 {
195 	return !!(data->config & HDC100X_REG_CONFIG_HEATER_EN);
196 }
197 
hdc100x_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)198 static int hdc100x_read_raw(struct iio_dev *indio_dev,
199 			    struct iio_chan_spec const *chan, int *val,
200 			    int *val2, long mask)
201 {
202 	struct hdc100x_data *data = iio_priv(indio_dev);
203 
204 	switch (mask) {
205 	case IIO_CHAN_INFO_RAW: {
206 		int ret;
207 
208 		mutex_lock(&data->lock);
209 		if (chan->type == IIO_CURRENT) {
210 			*val = hdc100x_get_heater_status(data);
211 			ret = IIO_VAL_INT;
212 		} else {
213 			ret = iio_device_claim_direct_mode(indio_dev);
214 			if (ret) {
215 				mutex_unlock(&data->lock);
216 				return ret;
217 			}
218 
219 			ret = hdc100x_get_measurement(data, chan);
220 			iio_device_release_direct_mode(indio_dev);
221 			if (ret >= 0) {
222 				*val = ret;
223 				ret = IIO_VAL_INT;
224 			}
225 		}
226 		mutex_unlock(&data->lock);
227 		return ret;
228 	}
229 	case IIO_CHAN_INFO_INT_TIME:
230 		*val = 0;
231 		*val2 = data->adc_int_us[chan->address];
232 		return IIO_VAL_INT_PLUS_MICRO;
233 	case IIO_CHAN_INFO_SCALE:
234 		if (chan->type == IIO_TEMP) {
235 			*val = 165000;
236 			*val2 = 65536;
237 			return IIO_VAL_FRACTIONAL;
238 		} else {
239 			*val = 100000;
240 			*val2 = 65536;
241 			return IIO_VAL_FRACTIONAL;
242 		}
243 		break;
244 	case IIO_CHAN_INFO_OFFSET:
245 		*val = -15887;
246 		*val2 = 515151;
247 		return IIO_VAL_INT_PLUS_MICRO;
248 	default:
249 		return -EINVAL;
250 	}
251 }
252 
hdc100x_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long mask)253 static int hdc100x_write_raw(struct iio_dev *indio_dev,
254 			     struct iio_chan_spec const *chan,
255 			     int val, int val2, long mask)
256 {
257 	struct hdc100x_data *data = iio_priv(indio_dev);
258 	int ret = -EINVAL;
259 
260 	switch (mask) {
261 	case IIO_CHAN_INFO_INT_TIME:
262 		if (val != 0)
263 			return -EINVAL;
264 
265 		mutex_lock(&data->lock);
266 		ret = hdc100x_set_it_time(data, chan->address, val2);
267 		mutex_unlock(&data->lock);
268 		return ret;
269 	case IIO_CHAN_INFO_RAW:
270 		if (chan->type != IIO_CURRENT || val2 != 0)
271 			return -EINVAL;
272 
273 		mutex_lock(&data->lock);
274 		ret = hdc100x_update_config(data, HDC100X_REG_CONFIG_HEATER_EN,
275 					val ? HDC100X_REG_CONFIG_HEATER_EN : 0);
276 		mutex_unlock(&data->lock);
277 		return ret;
278 	default:
279 		return -EINVAL;
280 	}
281 }
282 
hdc100x_buffer_postenable(struct iio_dev * indio_dev)283 static int hdc100x_buffer_postenable(struct iio_dev *indio_dev)
284 {
285 	struct hdc100x_data *data = iio_priv(indio_dev);
286 	int ret;
287 
288 	/* Buffer is enabled. First set ACQ Mode, then attach poll func */
289 	mutex_lock(&data->lock);
290 	ret = hdc100x_update_config(data, HDC100X_REG_CONFIG_ACQ_MODE,
291 				    HDC100X_REG_CONFIG_ACQ_MODE);
292 	mutex_unlock(&data->lock);
293 	if (ret)
294 		return ret;
295 
296 	return iio_triggered_buffer_postenable(indio_dev);
297 }
298 
hdc100x_buffer_predisable(struct iio_dev * indio_dev)299 static int hdc100x_buffer_predisable(struct iio_dev *indio_dev)
300 {
301 	struct hdc100x_data *data = iio_priv(indio_dev);
302 	int ret;
303 
304 	/* First detach poll func, then reset ACQ mode. OK to disable buffer */
305 	ret = iio_triggered_buffer_predisable(indio_dev);
306 	if (ret)
307 		return ret;
308 
309 	mutex_lock(&data->lock);
310 	ret = hdc100x_update_config(data, HDC100X_REG_CONFIG_ACQ_MODE, 0);
311 	mutex_unlock(&data->lock);
312 
313 	return ret;
314 }
315 
316 static const struct iio_buffer_setup_ops hdc_buffer_setup_ops = {
317 	.postenable  = hdc100x_buffer_postenable,
318 	.predisable  = hdc100x_buffer_predisable,
319 };
320 
hdc100x_trigger_handler(int irq,void * p)321 static irqreturn_t hdc100x_trigger_handler(int irq, void *p)
322 {
323 	struct iio_poll_func *pf = p;
324 	struct iio_dev *indio_dev = pf->indio_dev;
325 	struct hdc100x_data *data = iio_priv(indio_dev);
326 	struct i2c_client *client = data->client;
327 	int delay = data->adc_int_us[0] + data->adc_int_us[1] + 2*USEC_PER_MSEC;
328 	int ret;
329 
330 	/* dual read starts at temp register */
331 	mutex_lock(&data->lock);
332 	ret = i2c_smbus_write_byte(client, HDC100X_REG_TEMP);
333 	if (ret < 0) {
334 		dev_err(&client->dev, "cannot start measurement\n");
335 		goto err;
336 	}
337 	usleep_range(delay, delay + 1000);
338 
339 	ret = i2c_master_recv(client, (u8 *)data->scan.channels, 4);
340 	if (ret < 0) {
341 		dev_err(&client->dev, "cannot read sensor data\n");
342 		goto err;
343 	}
344 
345 	iio_push_to_buffers_with_timestamp(indio_dev, &data->scan,
346 					   iio_get_time_ns(indio_dev));
347 err:
348 	mutex_unlock(&data->lock);
349 	iio_trigger_notify_done(indio_dev->trig);
350 
351 	return IRQ_HANDLED;
352 }
353 
354 static const struct iio_info hdc100x_info = {
355 	.read_raw = hdc100x_read_raw,
356 	.write_raw = hdc100x_write_raw,
357 	.attrs = &hdc100x_attribute_group,
358 };
359 
hdc100x_probe(struct i2c_client * client,const struct i2c_device_id * id)360 static int hdc100x_probe(struct i2c_client *client,
361 			 const struct i2c_device_id *id)
362 {
363 	struct iio_dev *indio_dev;
364 	struct hdc100x_data *data;
365 	int ret;
366 
367 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WORD_DATA |
368 				     I2C_FUNC_SMBUS_BYTE | I2C_FUNC_I2C))
369 		return -EOPNOTSUPP;
370 
371 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
372 	if (!indio_dev)
373 		return -ENOMEM;
374 
375 	data = iio_priv(indio_dev);
376 	i2c_set_clientdata(client, indio_dev);
377 	data->client = client;
378 	mutex_init(&data->lock);
379 
380 	indio_dev->dev.parent = &client->dev;
381 	indio_dev->name = dev_name(&client->dev);
382 	indio_dev->modes = INDIO_DIRECT_MODE;
383 	indio_dev->info = &hdc100x_info;
384 
385 	indio_dev->channels = hdc100x_channels;
386 	indio_dev->num_channels = ARRAY_SIZE(hdc100x_channels);
387 	indio_dev->available_scan_masks = hdc100x_scan_masks;
388 
389 	/* be sure we are in a known state */
390 	hdc100x_set_it_time(data, 0, hdc100x_int_time[0][0]);
391 	hdc100x_set_it_time(data, 1, hdc100x_int_time[1][0]);
392 	hdc100x_update_config(data, HDC100X_REG_CONFIG_ACQ_MODE, 0);
393 
394 	ret = iio_triggered_buffer_setup(indio_dev, NULL,
395 					 hdc100x_trigger_handler,
396 					 &hdc_buffer_setup_ops);
397 	if (ret < 0) {
398 		dev_err(&client->dev, "iio triggered buffer setup failed\n");
399 		return ret;
400 	}
401 	ret = iio_device_register(indio_dev);
402 	if (ret < 0)
403 		iio_triggered_buffer_cleanup(indio_dev);
404 
405 	return ret;
406 }
407 
hdc100x_remove(struct i2c_client * client)408 static int hdc100x_remove(struct i2c_client *client)
409 {
410 	struct iio_dev *indio_dev = i2c_get_clientdata(client);
411 
412 	iio_device_unregister(indio_dev);
413 	iio_triggered_buffer_cleanup(indio_dev);
414 
415 	return 0;
416 }
417 
418 static const struct i2c_device_id hdc100x_id[] = {
419 	{ "hdc100x", 0 },
420 	{ "hdc1000", 0 },
421 	{ "hdc1008", 0 },
422 	{ "hdc1010", 0 },
423 	{ "hdc1050", 0 },
424 	{ "hdc1080", 0 },
425 	{ }
426 };
427 MODULE_DEVICE_TABLE(i2c, hdc100x_id);
428 
429 static const struct of_device_id hdc100x_dt_ids[] = {
430 	{ .compatible = "ti,hdc1000" },
431 	{ .compatible = "ti,hdc1008" },
432 	{ .compatible = "ti,hdc1010" },
433 	{ .compatible = "ti,hdc1050" },
434 	{ .compatible = "ti,hdc1080" },
435 	{ }
436 };
437 MODULE_DEVICE_TABLE(of, hdc100x_dt_ids);
438 
439 static struct i2c_driver hdc100x_driver = {
440 	.driver = {
441 		.name	= "hdc100x",
442 		.of_match_table = of_match_ptr(hdc100x_dt_ids),
443 	},
444 	.probe = hdc100x_probe,
445 	.remove = hdc100x_remove,
446 	.id_table = hdc100x_id,
447 };
448 module_i2c_driver(hdc100x_driver);
449 
450 MODULE_AUTHOR("Matt Ranostay <matt.ranostay@konsulko.com>");
451 MODULE_DESCRIPTION("TI HDC100x humidity and temperature sensor driver");
452 MODULE_LICENSE("GPL");
453