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