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