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