Merge branch 'sock_hold-misuses'
[linux/fpc-iii.git] / drivers / iio / humidity / hdc100x.c
blob265c34da52d1b584c8a969480ce1a9f5e46e73fa
1 /*
2 * hdc100x.c - Support for the TI HDC100x temperature + humidity sensors
4 * Copyright (C) 2015 Matt Ranostay <mranostay@gmail.com>
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.
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.
18 #include <linux/delay.h>
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/i2c.h>
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>
29 #define HDC100X_REG_TEMP 0x00
30 #define HDC100X_REG_HUMIDITY 0x01
32 #define HDC100X_REG_CONFIG 0x02
33 #define HDC100X_REG_CONFIG_ACQ_MODE BIT(12)
34 #define HDC100X_REG_CONFIG_HEATER_EN BIT(13)
36 struct hdc100x_data {
37 struct i2c_client *client;
38 struct mutex lock;
39 u16 config;
41 /* integration time of the sensor */
42 int adc_int_us[2];
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 */
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
60 { /* IIO_HUMIDITYRELATIVE channel */
61 .shift = 8,
62 .mask = 3,
66 static IIO_CONST_ATTR(temp_integration_time_available,
67 "0.00365 0.00635");
69 static IIO_CONST_ATTR(humidityrelative_integration_time_available,
70 "0.0025 0.00385 0.0065");
72 static IIO_CONST_ATTR(out_current_heater_raw_available,
73 "0 1");
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
82 static struct attribute_group hdc100x_attribute_group = {
83 .attrs = hdc100x_attributes,
86 static const struct iio_chan_spec hdc100x_channels[] = {
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,
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,
117 .type = IIO_CURRENT,
118 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
119 .extend_name = "heater",
120 .output = 1,
121 .scan_index = -1,
123 IIO_CHAN_SOFT_TIMESTAMP(2),
126 static const unsigned long hdc100x_scan_masks[] = {0x3, 0};
128 static int hdc100x_update_config(struct hdc100x_data *data, int mask, int val)
130 int tmp = (~mask & data->config) | val;
131 int ret;
133 ret = i2c_smbus_write_word_swapped(data->client,
134 HDC100X_REG_CONFIG, tmp);
135 if (!ret)
136 data->config = tmp;
138 return ret;
141 static int hdc100x_set_it_time(struct hdc100x_data *data, int chan, int val2)
143 int shift = hdc100x_resolution_shift[chan].shift;
144 int ret = -EINVAL;
145 int i;
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;
158 return ret;
161 static int hdc100x_get_measurement(struct hdc100x_data *data,
162 struct iio_chan_spec const *chan)
164 struct i2c_client *client = data->client;
165 int delay = data->adc_int_us[chan->address];
166 int ret;
167 __be16 val;
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;
176 /* wait for integration time to pass */
177 usleep_range(delay, delay + 1000);
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;
185 return be16_to_cpu(val);
188 static int hdc100x_get_heater_status(struct hdc100x_data *data)
190 return !!(data->config & HDC100X_REG_CONFIG_HEATER_EN);
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)
197 struct hdc100x_data *data = iio_priv(indio_dev);
199 switch (mask) {
200 case IIO_CHAN_INFO_RAW: {
201 int ret;
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;
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;
221 mutex_unlock(&data->lock);
222 return ret;
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;
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;
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)
252 struct hdc100x_data *data = iio_priv(indio_dev);
253 int ret = -EINVAL;
255 switch (mask) {
256 case IIO_CHAN_INFO_INT_TIME:
257 if (val != 0)
258 return -EINVAL;
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;
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;
278 static int hdc100x_buffer_postenable(struct iio_dev *indio_dev)
280 struct hdc100x_data *data = iio_priv(indio_dev);
281 int ret;
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;
291 return iio_triggered_buffer_postenable(indio_dev);
294 static int hdc100x_buffer_predisable(struct iio_dev *indio_dev)
296 struct hdc100x_data *data = iio_priv(indio_dev);
297 int ret;
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;
304 mutex_lock(&data->lock);
305 ret = hdc100x_update_config(data, HDC100X_REG_CONFIG_ACQ_MODE, 0);
306 mutex_unlock(&data->lock);
308 return ret;
311 static const struct iio_buffer_setup_ops hdc_buffer_setup_ops = {
312 .postenable = hdc100x_buffer_postenable,
313 .predisable = hdc100x_buffer_predisable,
316 static irqreturn_t hdc100x_trigger_handler(int irq, void *p)
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 */
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;
333 usleep_range(delay, delay + 1000);
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;
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);
347 return IRQ_HANDLED;
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,
357 static int hdc100x_probe(struct i2c_client *client,
358 const struct i2c_device_id *id)
360 struct iio_dev *indio_dev;
361 struct hdc100x_data *data;
362 int ret;
364 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WORD_DATA |
365 I2C_FUNC_SMBUS_BYTE | I2C_FUNC_I2C))
366 return -EOPNOTSUPP;
368 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
369 if (!indio_dev)
370 return -ENOMEM;
372 data = iio_priv(indio_dev);
373 i2c_set_clientdata(client, indio_dev);
374 data->client = client;
375 mutex_init(&data->lock);
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;
382 indio_dev->channels = hdc100x_channels;
383 indio_dev->num_channels = ARRAY_SIZE(hdc100x_channels);
384 indio_dev->available_scan_masks = hdc100x_scan_masks;
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);
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;
398 ret = iio_device_register(indio_dev);
399 if (ret < 0)
400 iio_triggered_buffer_cleanup(indio_dev);
402 return ret;
405 static int hdc100x_remove(struct i2c_client *client)
407 struct iio_dev *indio_dev = i2c_get_clientdata(client);
409 iio_device_unregister(indio_dev);
410 iio_triggered_buffer_cleanup(indio_dev);
412 return 0;
415 static const struct i2c_device_id hdc100x_id[] = {
416 { "hdc100x", 0 },
419 MODULE_DEVICE_TABLE(i2c, hdc100x_id);
421 static struct i2c_driver hdc100x_driver = {
422 .driver = {
423 .name = "hdc100x",
425 .probe = hdc100x_probe,
426 .remove = hdc100x_remove,
427 .id_table = hdc100x_id,
429 module_i2c_driver(hdc100x_driver);
431 MODULE_AUTHOR("Matt Ranostay <mranostay@gmail.com>");
432 MODULE_DESCRIPTION("TI HDC100x humidity and temperature sensor driver");
433 MODULE_LICENSE("GPL");