Matt Ranostay | d6ad805 | 2018-02-17 21:36:46 -0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Matt Ranostay | 4839367 | 2015-09-06 13:27:39 -0700 | [diff] [blame] | 2 | /* |
| 3 | * hdc100x.c - Support for the TI HDC100x temperature + humidity sensors |
| 4 | * |
Matt Ranostay | d6ad805 | 2018-02-17 21:36:46 -0800 | [diff] [blame] | 5 | * Copyright (C) 2015, 2018 |
| 6 | * Author: Matt Ranostay <matt.ranostay@konsulko.com> |
Matt Ranostay | 4839367 | 2015-09-06 13:27:39 -0700 | [diff] [blame] | 7 | * |
Michael Stecklein | 8f4c957 | 2017-06-19 11:01:05 -0500 | [diff] [blame] | 8 | * Datasheets: |
Alexander A. Klimov | 3593cd5 | 2020-07-04 21:27:43 +0200 | [diff] [blame] | 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 |
Matt Ranostay | 4839367 | 2015-09-06 13:27:39 -0700 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | #include <linux/delay.h> |
| 17 | #include <linux/module.h> |
Jonathan Cameron | d136431 | 2020-09-10 18:32:34 +0100 | [diff] [blame] | 18 | #include <linux/mod_devicetable.h> |
Matt Ranostay | 4839367 | 2015-09-06 13:27:39 -0700 | [diff] [blame] | 19 | #include <linux/init.h> |
| 20 | #include <linux/i2c.h> |
| 21 | |
| 22 | #include <linux/iio/iio.h> |
| 23 | #include <linux/iio/sysfs.h> |
Alison Schofield | 16bf793 | 2016-09-02 10:23:17 -0700 | [diff] [blame] | 24 | #include <linux/iio/buffer.h> |
| 25 | #include <linux/iio/trigger_consumer.h> |
| 26 | #include <linux/iio/triggered_buffer.h> |
Matt Ranostay | 4839367 | 2015-09-06 13:27:39 -0700 | [diff] [blame] | 27 | |
Chris Lesiak | 84edec8 | 2021-06-14 09:18:20 -0500 | [diff] [blame] | 28 | #include <linux/time.h> |
| 29 | |
Matt Ranostay | 4839367 | 2015-09-06 13:27:39 -0700 | [diff] [blame] | 30 | #define HDC100X_REG_TEMP 0x00 |
| 31 | #define HDC100X_REG_HUMIDITY 0x01 |
| 32 | |
| 33 | #define HDC100X_REG_CONFIG 0x02 |
Alison Schofield | 16bf793 | 2016-09-02 10:23:17 -0700 | [diff] [blame] | 34 | #define HDC100X_REG_CONFIG_ACQ_MODE BIT(12) |
Matt Ranostay | 4839367 | 2015-09-06 13:27:39 -0700 | [diff] [blame] | 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]; |
Jonathan Cameron | ea5e7a7 | 2020-06-07 16:53:52 +0100 | [diff] [blame] | 44 | /* Ensure natural alignment of timestamp */ |
| 45 | struct { |
| 46 | __be16 channels[2]; |
| 47 | s64 ts __aligned(8); |
| 48 | } scan; |
Matt Ranostay | 4839367 | 2015-09-06 13:27:39 -0700 | [diff] [blame] | 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, |
Alison Schofield | 0e35cf5 | 2016-05-20 10:06:41 -0700 | [diff] [blame] | 68 | .mask = 3, |
Matt Ranostay | 4839367 | 2015-09-06 13:27:39 -0700 | [diff] [blame] | 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 | |
simran singhal | 757cff8 | 2017-04-01 13:53:33 +0530 | [diff] [blame] | 88 | static const struct attribute_group hdc100x_attribute_group = { |
Matt Ranostay | 4839367 | 2015-09-06 13:27:39 -0700 | [diff] [blame] | 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), |
Alison Schofield | 16bf793 | 2016-09-02 10:23:17 -0700 | [diff] [blame] | 100 | .scan_index = 0, |
| 101 | .scan_type = { |
| 102 | .sign = 's', |
| 103 | .realbits = 16, |
| 104 | .storagebits = 16, |
| 105 | .endianness = IIO_BE, |
| 106 | }, |
Matt Ranostay | 4839367 | 2015-09-06 13:27:39 -0700 | [diff] [blame] | 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) | |
Alison Schofield | 16bf793 | 2016-09-02 10:23:17 -0700 | [diff] [blame] | 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 | }, |
Matt Ranostay | 4839367 | 2015-09-06 13:27:39 -0700 | [diff] [blame] | 121 | }, |
| 122 | { |
| 123 | .type = IIO_CURRENT, |
| 124 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), |
| 125 | .extend_name = "heater", |
| 126 | .output = 1, |
Alison Schofield | 16bf793 | 2016-09-02 10:23:17 -0700 | [diff] [blame] | 127 | .scan_index = -1, |
Matt Ranostay | 4839367 | 2015-09-06 13:27:39 -0700 | [diff] [blame] | 128 | }, |
Alison Schofield | 16bf793 | 2016-09-02 10:23:17 -0700 | [diff] [blame] | 129 | IIO_CHAN_SOFT_TIMESTAMP(2), |
Matt Ranostay | 4839367 | 2015-09-06 13:27:39 -0700 | [diff] [blame] | 130 | }; |
| 131 | |
Alison Schofield | 16bf793 | 2016-09-02 10:23:17 -0700 | [diff] [blame] | 132 | static const unsigned long hdc100x_scan_masks[] = {0x3, 0}; |
| 133 | |
Matt Ranostay | 4839367 | 2015-09-06 13:27:39 -0700 | [diff] [blame] | 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 | |
| 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 | |
| 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; |
Chris Lesiak | 84edec8 | 2021-06-14 09:18:20 -0500 | [diff] [blame] | 171 | int delay = data->adc_int_us[chan->address] + 1*USEC_PER_MSEC; |
Matt Ranostay | 4839367 | 2015-09-06 13:27:39 -0700 | [diff] [blame] | 172 | int ret; |
Alison Schofield | 0d9dcf8 | 2016-08-08 11:14:36 -0700 | [diff] [blame] | 173 | __be16 val; |
Matt Ranostay | 4839367 | 2015-09-06 13:27:39 -0700 | [diff] [blame] | 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 | |
Alison Schofield | 0d9dcf8 | 2016-08-08 11:14:36 -0700 | [diff] [blame] | 185 | /* read measurement */ |
| 186 | ret = i2c_master_recv(data->client, (char *)&val, sizeof(val)); |
Matt Ranostay | 4839367 | 2015-09-06 13:27:39 -0700 | [diff] [blame] | 187 | if (ret < 0) { |
Alison Schofield | 0d9dcf8 | 2016-08-08 11:14:36 -0700 | [diff] [blame] | 188 | dev_err(&client->dev, "cannot read sensor data\n"); |
Matt Ranostay | 4839367 | 2015-09-06 13:27:39 -0700 | [diff] [blame] | 189 | return ret; |
| 190 | } |
Alison Schofield | 0d9dcf8 | 2016-08-08 11:14:36 -0700 | [diff] [blame] | 191 | return be16_to_cpu(val); |
Matt Ranostay | 4839367 | 2015-09-06 13:27:39 -0700 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | static int hdc100x_get_heater_status(struct hdc100x_data *data) |
| 195 | { |
| 196 | return !!(data->config & HDC100X_REG_CONFIG_HEATER_EN); |
| 197 | } |
| 198 | |
| 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 | mutex_lock(&data->lock); |
| 210 | if (chan->type == IIO_CURRENT) { |
| 211 | *val = hdc100x_get_heater_status(data); |
| 212 | ret = IIO_VAL_INT; |
| 213 | } else { |
Alison Schofield | 16bf793 | 2016-09-02 10:23:17 -0700 | [diff] [blame] | 214 | ret = iio_device_claim_direct_mode(indio_dev); |
| 215 | if (ret) { |
| 216 | mutex_unlock(&data->lock); |
| 217 | return ret; |
| 218 | } |
| 219 | |
Matt Ranostay | 4839367 | 2015-09-06 13:27:39 -0700 | [diff] [blame] | 220 | ret = hdc100x_get_measurement(data, chan); |
Alison Schofield | 16bf793 | 2016-09-02 10:23:17 -0700 | [diff] [blame] | 221 | iio_device_release_direct_mode(indio_dev); |
Matt Ranostay | 4839367 | 2015-09-06 13:27:39 -0700 | [diff] [blame] | 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) { |
Matt Ranostay | 09bc0dda | 2016-05-26 19:55:06 -0700 | [diff] [blame] | 236 | *val = 165000; |
Matt Ranostay | 94bef00 | 2016-05-29 19:52:02 -0700 | [diff] [blame] | 237 | *val2 = 65536; |
Matt Ranostay | 4839367 | 2015-09-06 13:27:39 -0700 | [diff] [blame] | 238 | return IIO_VAL_FRACTIONAL; |
| 239 | } else { |
Chris Lesiak | 342a692 | 2019-11-21 20:39:42 +0000 | [diff] [blame] | 240 | *val = 100000; |
Matt Ranostay | 94bef00 | 2016-05-29 19:52:02 -0700 | [diff] [blame] | 241 | *val2 = 65536; |
| 242 | return IIO_VAL_FRACTIONAL; |
Matt Ranostay | 4839367 | 2015-09-06 13:27:39 -0700 | [diff] [blame] | 243 | } |
| 244 | break; |
| 245 | case IIO_CHAN_INFO_OFFSET: |
Matt Ranostay | 94bef00 | 2016-05-29 19:52:02 -0700 | [diff] [blame] | 246 | *val = -15887; |
| 247 | *val2 = 515151; |
Matt Ranostay | d3a21ce | 2015-09-26 23:18:57 -0700 | [diff] [blame] | 248 | return IIO_VAL_INT_PLUS_MICRO; |
Matt Ranostay | 4839367 | 2015-09-06 13:27:39 -0700 | [diff] [blame] | 249 | default: |
| 250 | return -EINVAL; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | static 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 | |
Alison Schofield | 16bf793 | 2016-09-02 10:23:17 -0700 | [diff] [blame] | 284 | static 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); |
Alison Schofield | 16bf793 | 2016-09-02 10:23:17 -0700 | [diff] [blame] | 294 | |
Alexandru Ardelean | 9b58916 | 2019-10-23 11:27:14 +0300 | [diff] [blame] | 295 | return ret; |
Alison Schofield | 16bf793 | 2016-09-02 10:23:17 -0700 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | static int hdc100x_buffer_predisable(struct iio_dev *indio_dev) |
| 299 | { |
| 300 | struct hdc100x_data *data = iio_priv(indio_dev); |
Lars-Peter Clausen | f11d59d | 2020-05-25 14:38:53 +0300 | [diff] [blame] | 301 | int ret; |
Alison Schofield | 16bf793 | 2016-09-02 10:23:17 -0700 | [diff] [blame] | 302 | |
| 303 | mutex_lock(&data->lock); |
| 304 | ret = hdc100x_update_config(data, HDC100X_REG_CONFIG_ACQ_MODE, 0); |
| 305 | mutex_unlock(&data->lock); |
| 306 | |
| 307 | return ret; |
| 308 | } |
| 309 | |
| 310 | static const struct iio_buffer_setup_ops hdc_buffer_setup_ops = { |
| 311 | .postenable = hdc100x_buffer_postenable, |
| 312 | .predisable = hdc100x_buffer_predisable, |
| 313 | }; |
| 314 | |
| 315 | static irqreturn_t hdc100x_trigger_handler(int irq, void *p) |
| 316 | { |
| 317 | struct iio_poll_func *pf = p; |
| 318 | struct iio_dev *indio_dev = pf->indio_dev; |
| 319 | struct hdc100x_data *data = iio_priv(indio_dev); |
| 320 | struct i2c_client *client = data->client; |
Chris Lesiak | 84edec8 | 2021-06-14 09:18:20 -0500 | [diff] [blame] | 321 | int delay = data->adc_int_us[0] + data->adc_int_us[1] + 2*USEC_PER_MSEC; |
Alison Schofield | 16bf793 | 2016-09-02 10:23:17 -0700 | [diff] [blame] | 322 | int ret; |
Alison Schofield | 16bf793 | 2016-09-02 10:23:17 -0700 | [diff] [blame] | 323 | |
| 324 | /* dual read starts at temp register */ |
| 325 | mutex_lock(&data->lock); |
| 326 | ret = i2c_smbus_write_byte(client, HDC100X_REG_TEMP); |
| 327 | if (ret < 0) { |
| 328 | dev_err(&client->dev, "cannot start measurement\n"); |
| 329 | goto err; |
| 330 | } |
| 331 | usleep_range(delay, delay + 1000); |
| 332 | |
Jonathan Cameron | ea5e7a7 | 2020-06-07 16:53:52 +0100 | [diff] [blame] | 333 | ret = i2c_master_recv(client, (u8 *)data->scan.channels, 4); |
Alison Schofield | 16bf793 | 2016-09-02 10:23:17 -0700 | [diff] [blame] | 334 | if (ret < 0) { |
| 335 | dev_err(&client->dev, "cannot read sensor data\n"); |
| 336 | goto err; |
| 337 | } |
| 338 | |
Jonathan Cameron | ea5e7a7 | 2020-06-07 16:53:52 +0100 | [diff] [blame] | 339 | iio_push_to_buffers_with_timestamp(indio_dev, &data->scan, |
Alison Schofield | 16bf793 | 2016-09-02 10:23:17 -0700 | [diff] [blame] | 340 | iio_get_time_ns(indio_dev)); |
| 341 | err: |
| 342 | mutex_unlock(&data->lock); |
| 343 | iio_trigger_notify_done(indio_dev->trig); |
| 344 | |
| 345 | return IRQ_HANDLED; |
| 346 | } |
| 347 | |
Matt Ranostay | 4839367 | 2015-09-06 13:27:39 -0700 | [diff] [blame] | 348 | static const struct iio_info hdc100x_info = { |
| 349 | .read_raw = hdc100x_read_raw, |
| 350 | .write_raw = hdc100x_write_raw, |
| 351 | .attrs = &hdc100x_attribute_group, |
Matt Ranostay | 4839367 | 2015-09-06 13:27:39 -0700 | [diff] [blame] | 352 | }; |
| 353 | |
| 354 | static int hdc100x_probe(struct i2c_client *client, |
| 355 | const struct i2c_device_id *id) |
| 356 | { |
| 357 | struct iio_dev *indio_dev; |
| 358 | struct hdc100x_data *data; |
Alison Schofield | 16bf793 | 2016-09-02 10:23:17 -0700 | [diff] [blame] | 359 | int ret; |
Matt Ranostay | 4839367 | 2015-09-06 13:27:39 -0700 | [diff] [blame] | 360 | |
Alison Schofield | 0d9dcf8 | 2016-08-08 11:14:36 -0700 | [diff] [blame] | 361 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WORD_DATA | |
| 362 | I2C_FUNC_SMBUS_BYTE | I2C_FUNC_I2C)) |
Matt Ranostay | f8d9d3b | 2016-02-26 22:13:49 -0800 | [diff] [blame] | 363 | return -EOPNOTSUPP; |
Matt Ranostay | 4839367 | 2015-09-06 13:27:39 -0700 | [diff] [blame] | 364 | |
| 365 | indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); |
| 366 | if (!indio_dev) |
| 367 | return -ENOMEM; |
| 368 | |
| 369 | data = iio_priv(indio_dev); |
| 370 | i2c_set_clientdata(client, indio_dev); |
| 371 | data->client = client; |
| 372 | mutex_init(&data->lock); |
| 373 | |
Matt Ranostay | 4839367 | 2015-09-06 13:27:39 -0700 | [diff] [blame] | 374 | indio_dev->name = dev_name(&client->dev); |
| 375 | indio_dev->modes = INDIO_DIRECT_MODE; |
| 376 | indio_dev->info = &hdc100x_info; |
| 377 | |
| 378 | indio_dev->channels = hdc100x_channels; |
| 379 | indio_dev->num_channels = ARRAY_SIZE(hdc100x_channels); |
Alison Schofield | 16bf793 | 2016-09-02 10:23:17 -0700 | [diff] [blame] | 380 | indio_dev->available_scan_masks = hdc100x_scan_masks; |
Matt Ranostay | 4839367 | 2015-09-06 13:27:39 -0700 | [diff] [blame] | 381 | |
| 382 | /* be sure we are in a known state */ |
| 383 | hdc100x_set_it_time(data, 0, hdc100x_int_time[0][0]); |
| 384 | hdc100x_set_it_time(data, 1, hdc100x_int_time[1][0]); |
Alison Schofield | 16bf793 | 2016-09-02 10:23:17 -0700 | [diff] [blame] | 385 | hdc100x_update_config(data, HDC100X_REG_CONFIG_ACQ_MODE, 0); |
Matt Ranostay | 4839367 | 2015-09-06 13:27:39 -0700 | [diff] [blame] | 386 | |
Chuhong Yuan | 23f1ce3 | 2019-07-26 16:02:55 +0800 | [diff] [blame] | 387 | ret = devm_iio_triggered_buffer_setup(&client->dev, |
| 388 | indio_dev, NULL, |
Alison Schofield | 16bf793 | 2016-09-02 10:23:17 -0700 | [diff] [blame] | 389 | hdc100x_trigger_handler, |
| 390 | &hdc_buffer_setup_ops); |
| 391 | if (ret < 0) { |
| 392 | dev_err(&client->dev, "iio triggered buffer setup failed\n"); |
| 393 | return ret; |
| 394 | } |
Alison Schofield | 16bf793 | 2016-09-02 10:23:17 -0700 | [diff] [blame] | 395 | |
Chuhong Yuan | 23f1ce3 | 2019-07-26 16:02:55 +0800 | [diff] [blame] | 396 | return devm_iio_device_register(&client->dev, indio_dev); |
Matt Ranostay | 4839367 | 2015-09-06 13:27:39 -0700 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | static const struct i2c_device_id hdc100x_id[] = { |
| 400 | { "hdc100x", 0 }, |
Michael Stecklein | 8f4c957 | 2017-06-19 11:01:05 -0500 | [diff] [blame] | 401 | { "hdc1000", 0 }, |
| 402 | { "hdc1008", 0 }, |
| 403 | { "hdc1010", 0 }, |
| 404 | { "hdc1050", 0 }, |
| 405 | { "hdc1080", 0 }, |
Matt Ranostay | 4839367 | 2015-09-06 13:27:39 -0700 | [diff] [blame] | 406 | { } |
| 407 | }; |
| 408 | MODULE_DEVICE_TABLE(i2c, hdc100x_id); |
| 409 | |
Michael Stecklein | a2d80de | 2017-06-19 11:01:06 -0500 | [diff] [blame] | 410 | static const struct of_device_id hdc100x_dt_ids[] = { |
| 411 | { .compatible = "ti,hdc1000" }, |
| 412 | { .compatible = "ti,hdc1008" }, |
| 413 | { .compatible = "ti,hdc1010" }, |
| 414 | { .compatible = "ti,hdc1050" }, |
| 415 | { .compatible = "ti,hdc1080" }, |
| 416 | { } |
| 417 | }; |
| 418 | MODULE_DEVICE_TABLE(of, hdc100x_dt_ids); |
| 419 | |
Matt Ranostay | 4839367 | 2015-09-06 13:27:39 -0700 | [diff] [blame] | 420 | static struct i2c_driver hdc100x_driver = { |
| 421 | .driver = { |
| 422 | .name = "hdc100x", |
Jonathan Cameron | d136431 | 2020-09-10 18:32:34 +0100 | [diff] [blame] | 423 | .of_match_table = hdc100x_dt_ids, |
Matt Ranostay | 4839367 | 2015-09-06 13:27:39 -0700 | [diff] [blame] | 424 | }, |
| 425 | .probe = hdc100x_probe, |
Matt Ranostay | 4839367 | 2015-09-06 13:27:39 -0700 | [diff] [blame] | 426 | .id_table = hdc100x_id, |
| 427 | }; |
| 428 | module_i2c_driver(hdc100x_driver); |
| 429 | |
Matt Ranostay | d6ad805 | 2018-02-17 21:36:46 -0800 | [diff] [blame] | 430 | MODULE_AUTHOR("Matt Ranostay <matt.ranostay@konsulko.com>"); |
Matt Ranostay | 4839367 | 2015-09-06 13:27:39 -0700 | [diff] [blame] | 431 | MODULE_DESCRIPTION("TI HDC100x humidity and temperature sensor driver"); |
| 432 | MODULE_LICENSE("GPL"); |