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 * https://www.ti.com/product/HDC1000/datasheet
10 * https://www.ti.com/product/HDC1008/datasheet
11 * https://www.ti.com/product/HDC1010/datasheet
12 * https://www.ti.com/product/HDC1050/datasheet
13 * https://www.ti.com/product/HDC1080/datasheet
14 */
15
16 #include <linux/cleanup.h>
17 #include <linux/delay.h>
18 #include <linux/module.h>
19 #include <linux/init.h>
20 #include <linux/i2c.h>
21
22 #include <linux/iio/iio.h>
23 #include <linux/iio/sysfs.h>
24 #include <linux/iio/buffer.h>
25 #include <linux/iio/trigger_consumer.h>
26 #include <linux/iio/triggered_buffer.h>
27
28 #include <linux/time.h>
29
30 #define HDC100X_REG_TEMP 0x00
31 #define HDC100X_REG_HUMIDITY 0x01
32
33 #define HDC100X_REG_CONFIG 0x02
34 #define HDC100X_REG_CONFIG_ACQ_MODE BIT(12)
35 #define HDC100X_REG_CONFIG_HEATER_EN BIT(13)
36
37 struct hdc100x_data {
38 struct i2c_client *client;
39 struct mutex lock;
40 u16 config;
41
42 /* integration time of the sensor */
43 int adc_int_us[2];
44 /* Ensure natural alignment of timestamp */
45 struct {
46 __be16 channels[2];
47 aligned_s64 ts;
48 } scan;
49 };
50
51 /* integration time in us */
52 static 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 */
58 static 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,
68 .mask = 3,
69 },
70 };
71
72 static IIO_CONST_ATTR(temp_integration_time_available,
73 "0.00365 0.00635");
74
75 static IIO_CONST_ATTR(humidityrelative_integration_time_available,
76 "0.0025 0.00385 0.0065");
77
78 static IIO_CONST_ATTR(out_current_heater_raw_available,
79 "0 1");
80
81 static 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
88 static const struct attribute_group hdc100x_attribute_group = {
89 .attrs = hdc100x_attributes,
90 };
91
92 static 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),
100 .scan_index = 0,
101 .scan_type = {
102 .sign = 's',
103 .realbits = 16,
104 .storagebits = 16,
105 .endianness = IIO_BE,
106 },
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) |
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 },
121 },
122 {
123 .type = IIO_CURRENT,
124 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
125 .extend_name = "heater",
126 .output = 1,
127 .scan_index = -1,
128 },
129 IIO_CHAN_SOFT_TIMESTAMP(2),
130 };
131
132 static const unsigned long hdc100x_scan_masks[] = {0x3, 0};
133
hdc100x_update_config(struct hdc100x_data * data,int mask,int val)134 static 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
hdc100x_set_it_time(struct hdc100x_data * data,int chan,int val2)147 static 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
hdc100x_get_measurement(struct hdc100x_data * data,struct iio_chan_spec const * chan)167 static 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] + 1*USEC_PER_MSEC;
172 int ret;
173 __be16 val;
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
185 /* read measurement */
186 ret = i2c_master_recv(data->client, (char *)&val, sizeof(val));
187 if (ret < 0) {
188 dev_err(&client->dev, "cannot read sensor data\n");
189 return ret;
190 }
191 return be16_to_cpu(val);
192 }
193
hdc100x_get_heater_status(struct hdc100x_data * data)194 static int hdc100x_get_heater_status(struct hdc100x_data *data)
195 {
196 return !!(data->config & HDC100X_REG_CONFIG_HEATER_EN);
197 }
198
hdc100x_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)199 static 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 guard(mutex)(&data->lock);
210 if (chan->type == IIO_CURRENT) {
211 *val = hdc100x_get_heater_status(data);
212 return IIO_VAL_INT;
213 }
214 if (!iio_device_claim_direct(indio_dev))
215 return -EBUSY;
216
217 ret = hdc100x_get_measurement(data, chan);
218 iio_device_release_direct(indio_dev);
219 if (ret < 0)
220 return ret;
221 *val = ret;
222 return IIO_VAL_INT;
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 = 100000;
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
hdc100x_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long mask)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
254 switch (mask) {
255 case IIO_CHAN_INFO_INT_TIME: {
256 if (val != 0)
257 return -EINVAL;
258
259 guard(mutex)(&data->lock);
260 return hdc100x_set_it_time(data, chan->address, val2);
261 }
262 case IIO_CHAN_INFO_RAW: {
263 if (chan->type != IIO_CURRENT || val2 != 0)
264 return -EINVAL;
265
266 guard(mutex)(&data->lock);
267 return hdc100x_update_config(data, HDC100X_REG_CONFIG_HEATER_EN,
268 val ? HDC100X_REG_CONFIG_HEATER_EN : 0);
269 }
270 default:
271 return -EINVAL;
272 }
273 }
274
hdc100x_buffer_postenable(struct iio_dev * indio_dev)275 static int hdc100x_buffer_postenable(struct iio_dev *indio_dev)
276 {
277 struct hdc100x_data *data = iio_priv(indio_dev);
278
279 /* Buffer is enabled. First set ACQ Mode, then attach poll func */
280 guard(mutex)(&data->lock);
281 return hdc100x_update_config(data, HDC100X_REG_CONFIG_ACQ_MODE,
282 HDC100X_REG_CONFIG_ACQ_MODE);
283 }
284
hdc100x_buffer_predisable(struct iio_dev * indio_dev)285 static int hdc100x_buffer_predisable(struct iio_dev *indio_dev)
286 {
287 struct hdc100x_data *data = iio_priv(indio_dev);
288
289 guard(mutex)(&data->lock);
290 return hdc100x_update_config(data, HDC100X_REG_CONFIG_ACQ_MODE, 0);
291 }
292
293 static const struct iio_buffer_setup_ops hdc_buffer_setup_ops = {
294 .postenable = hdc100x_buffer_postenable,
295 .predisable = hdc100x_buffer_predisable,
296 };
297
hdc100x_trigger_handler(int irq,void * p)298 static irqreturn_t hdc100x_trigger_handler(int irq, void *p)
299 {
300 struct iio_poll_func *pf = p;
301 struct iio_dev *indio_dev = pf->indio_dev;
302 struct hdc100x_data *data = iio_priv(indio_dev);
303 struct i2c_client *client = data->client;
304 int delay = data->adc_int_us[0] + data->adc_int_us[1] + 2*USEC_PER_MSEC;
305 int ret;
306
307 /* dual read starts at temp register */
308 mutex_lock(&data->lock);
309 ret = i2c_smbus_write_byte(client, HDC100X_REG_TEMP);
310 if (ret < 0) {
311 dev_err(&client->dev, "cannot start measurement\n");
312 goto err;
313 }
314 usleep_range(delay, delay + 1000);
315
316 ret = i2c_master_recv(client, (u8 *)data->scan.channels, 4);
317 if (ret < 0) {
318 dev_err(&client->dev, "cannot read sensor data\n");
319 goto err;
320 }
321
322 iio_push_to_buffers_with_timestamp(indio_dev, &data->scan,
323 iio_get_time_ns(indio_dev));
324 err:
325 mutex_unlock(&data->lock);
326 iio_trigger_notify_done(indio_dev->trig);
327
328 return IRQ_HANDLED;
329 }
330
331 static const struct iio_info hdc100x_info = {
332 .read_raw = hdc100x_read_raw,
333 .write_raw = hdc100x_write_raw,
334 .attrs = &hdc100x_attribute_group,
335 };
336
hdc100x_probe(struct i2c_client * client)337 static int hdc100x_probe(struct i2c_client *client)
338 {
339 struct iio_dev *indio_dev;
340 struct hdc100x_data *data;
341 int ret;
342
343 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WORD_DATA |
344 I2C_FUNC_SMBUS_BYTE | I2C_FUNC_I2C))
345 return -EOPNOTSUPP;
346
347 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
348 if (!indio_dev)
349 return -ENOMEM;
350
351 data = iio_priv(indio_dev);
352 i2c_set_clientdata(client, indio_dev);
353 data->client = client;
354 mutex_init(&data->lock);
355
356 indio_dev->name = dev_name(&client->dev);
357 indio_dev->modes = INDIO_DIRECT_MODE;
358 indio_dev->info = &hdc100x_info;
359
360 indio_dev->channels = hdc100x_channels;
361 indio_dev->num_channels = ARRAY_SIZE(hdc100x_channels);
362 indio_dev->available_scan_masks = hdc100x_scan_masks;
363
364 /* be sure we are in a known state */
365 hdc100x_set_it_time(data, 0, hdc100x_int_time[0][0]);
366 hdc100x_set_it_time(data, 1, hdc100x_int_time[1][0]);
367 hdc100x_update_config(data, HDC100X_REG_CONFIG_ACQ_MODE, 0);
368
369 ret = devm_iio_triggered_buffer_setup(&client->dev,
370 indio_dev, NULL,
371 hdc100x_trigger_handler,
372 &hdc_buffer_setup_ops);
373 if (ret < 0) {
374 dev_err(&client->dev, "iio triggered buffer setup failed\n");
375 return ret;
376 }
377
378 return devm_iio_device_register(&client->dev, indio_dev);
379 }
380
381 static const struct i2c_device_id hdc100x_id[] = {
382 { .name = "hdc100x" },
383 { .name = "hdc1000" },
384 { .name = "hdc1008" },
385 { .name = "hdc1010" },
386 { .name = "hdc1050" },
387 { .name = "hdc1080" },
388 { }
389 };
390 MODULE_DEVICE_TABLE(i2c, hdc100x_id);
391
392 static const struct of_device_id hdc100x_dt_ids[] = {
393 { .compatible = "ti,hdc1000" },
394 { .compatible = "ti,hdc1008" },
395 { .compatible = "ti,hdc1010" },
396 { .compatible = "ti,hdc1050" },
397 { .compatible = "ti,hdc1080" },
398 { }
399 };
400 MODULE_DEVICE_TABLE(of, hdc100x_dt_ids);
401
402 static const struct acpi_device_id hdc100x_acpi_match[] = {
403 { "TXNW1010" },
404 { }
405 };
406 MODULE_DEVICE_TABLE(acpi, hdc100x_acpi_match);
407
408 static struct i2c_driver hdc100x_driver = {
409 .driver = {
410 .name = "hdc100x",
411 .of_match_table = hdc100x_dt_ids,
412 .acpi_match_table = hdc100x_acpi_match,
413 },
414 .probe = hdc100x_probe,
415 .id_table = hdc100x_id,
416 };
417 module_i2c_driver(hdc100x_driver);
418
419 MODULE_AUTHOR("Matt Ranostay <matt.ranostay@konsulko.com>");
420 MODULE_DESCRIPTION("TI HDC100x humidity and temperature sensor driver");
421 MODULE_LICENSE("GPL");
422