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