Matt Ranostay | cd8d9777 | 2015-09-13 20:26:14 -0700 | [diff] [blame] | 1 | /* |
| 2 | * vz89x.c - Support for SGX Sensortech MiCS VZ89X VOC 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 | */ |
| 17 | |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/mutex.h> |
| 20 | #include <linux/init.h> |
| 21 | #include <linux/i2c.h> |
Matt Ranostay | 8376882 | 2016-08-24 23:44:47 -0700 | [diff] [blame] | 22 | #include <linux/of.h> |
| 23 | #include <linux/of_device.h> |
Matt Ranostay | cd8d9777 | 2015-09-13 20:26:14 -0700 | [diff] [blame] | 24 | |
| 25 | #include <linux/iio/iio.h> |
| 26 | #include <linux/iio/sysfs.h> |
| 27 | |
| 28 | #define VZ89X_REG_MEASUREMENT 0x09 |
Matt Ranostay | 8376882 | 2016-08-24 23:44:47 -0700 | [diff] [blame] | 29 | #define VZ89X_REG_MEASUREMENT_RD_SIZE 6 |
| 30 | #define VZ89X_REG_MEASUREMENT_WR_SIZE 3 |
Matt Ranostay | cd8d9777 | 2015-09-13 20:26:14 -0700 | [diff] [blame] | 31 | |
| 32 | #define VZ89X_VOC_CO2_IDX 0 |
| 33 | #define VZ89X_VOC_SHORT_IDX 1 |
| 34 | #define VZ89X_VOC_TVOC_IDX 2 |
| 35 | #define VZ89X_VOC_RESISTANCE_IDX 3 |
| 36 | |
Matt Ranostay | 0a735aa | 2016-08-24 23:44:48 -0700 | [diff] [blame] | 37 | #define VZ89TE_REG_MEASUREMENT 0x0c |
| 38 | #define VZ89TE_REG_MEASUREMENT_RD_SIZE 7 |
| 39 | #define VZ89TE_REG_MEASUREMENT_WR_SIZE 6 |
| 40 | |
| 41 | #define VZ89TE_VOC_TVOC_IDX 0 |
| 42 | #define VZ89TE_VOC_CO2_IDX 1 |
| 43 | #define VZ89TE_VOC_RESISTANCE_IDX 2 |
| 44 | |
Matt Ranostay | 8376882 | 2016-08-24 23:44:47 -0700 | [diff] [blame] | 45 | enum { |
| 46 | VZ89X, |
Matt Ranostay | 0a735aa | 2016-08-24 23:44:48 -0700 | [diff] [blame] | 47 | VZ89TE, |
Matt Ranostay | 8376882 | 2016-08-24 23:44:47 -0700 | [diff] [blame] | 48 | }; |
| 49 | |
| 50 | struct vz89x_chip_data; |
| 51 | |
Matt Ranostay | cd8d9777 | 2015-09-13 20:26:14 -0700 | [diff] [blame] | 52 | struct vz89x_data { |
| 53 | struct i2c_client *client; |
Matt Ranostay | 8376882 | 2016-08-24 23:44:47 -0700 | [diff] [blame] | 54 | const struct vz89x_chip_data *chip; |
Matt Ranostay | cd8d9777 | 2015-09-13 20:26:14 -0700 | [diff] [blame] | 55 | struct mutex lock; |
Matt Ranostay | 93e87d7 | 2015-12-01 21:47:20 -0800 | [diff] [blame] | 56 | int (*xfer)(struct vz89x_data *data, u8 cmd); |
Matt Ranostay | cd8d9777 | 2015-09-13 20:26:14 -0700 | [diff] [blame] | 57 | |
Matt Ranostay | 9d1894c | 2016-08-24 23:44:49 -0700 | [diff] [blame] | 58 | bool is_valid; |
Matt Ranostay | 93e87d7 | 2015-12-01 21:47:20 -0800 | [diff] [blame] | 59 | unsigned long last_update; |
Matt Ranostay | 0a735aa | 2016-08-24 23:44:48 -0700 | [diff] [blame] | 60 | u8 buffer[VZ89TE_REG_MEASUREMENT_RD_SIZE]; |
Matt Ranostay | 8376882 | 2016-08-24 23:44:47 -0700 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | struct vz89x_chip_data { |
| 64 | bool (*valid)(struct vz89x_data *data); |
| 65 | const struct iio_chan_spec *channels; |
| 66 | u8 num_channels; |
| 67 | |
| 68 | u8 cmd; |
| 69 | u8 read_size; |
| 70 | u8 write_size; |
Matt Ranostay | cd8d9777 | 2015-09-13 20:26:14 -0700 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | static const struct iio_chan_spec vz89x_channels[] = { |
| 74 | { |
| 75 | .type = IIO_CONCENTRATION, |
| 76 | .channel2 = IIO_MOD_CO2, |
| 77 | .modified = 1, |
| 78 | .info_mask_separate = |
| 79 | BIT(IIO_CHAN_INFO_OFFSET) | BIT(IIO_CHAN_INFO_RAW), |
| 80 | .address = VZ89X_VOC_CO2_IDX, |
| 81 | }, |
| 82 | { |
| 83 | .type = IIO_CONCENTRATION, |
| 84 | .channel2 = IIO_MOD_VOC, |
| 85 | .modified = 1, |
| 86 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), |
| 87 | .address = VZ89X_VOC_SHORT_IDX, |
| 88 | .extend_name = "short", |
| 89 | }, |
| 90 | { |
| 91 | .type = IIO_CONCENTRATION, |
| 92 | .channel2 = IIO_MOD_VOC, |
| 93 | .modified = 1, |
| 94 | .info_mask_separate = |
| 95 | BIT(IIO_CHAN_INFO_OFFSET) | BIT(IIO_CHAN_INFO_RAW), |
| 96 | .address = VZ89X_VOC_TVOC_IDX, |
| 97 | }, |
| 98 | { |
| 99 | .type = IIO_RESISTANCE, |
| 100 | .info_mask_separate = |
| 101 | BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), |
| 102 | .address = VZ89X_VOC_RESISTANCE_IDX, |
Matt Ranostay | 0a735aa | 2016-08-24 23:44:48 -0700 | [diff] [blame] | 103 | .scan_index = -1, |
| 104 | .scan_type = { |
| 105 | .endianness = IIO_LE, |
| 106 | }, |
| 107 | }, |
| 108 | }; |
| 109 | |
| 110 | static const struct iio_chan_spec vz89te_channels[] = { |
| 111 | { |
| 112 | .type = IIO_CONCENTRATION, |
| 113 | .channel2 = IIO_MOD_VOC, |
| 114 | .modified = 1, |
| 115 | .info_mask_separate = |
| 116 | BIT(IIO_CHAN_INFO_OFFSET) | BIT(IIO_CHAN_INFO_RAW), |
| 117 | .address = VZ89TE_VOC_TVOC_IDX, |
| 118 | }, |
| 119 | |
| 120 | { |
| 121 | .type = IIO_CONCENTRATION, |
| 122 | .channel2 = IIO_MOD_CO2, |
| 123 | .modified = 1, |
| 124 | .info_mask_separate = |
| 125 | BIT(IIO_CHAN_INFO_OFFSET) | BIT(IIO_CHAN_INFO_RAW), |
| 126 | .address = VZ89TE_VOC_CO2_IDX, |
| 127 | }, |
| 128 | { |
| 129 | .type = IIO_RESISTANCE, |
| 130 | .info_mask_separate = |
| 131 | BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), |
| 132 | .address = VZ89TE_VOC_RESISTANCE_IDX, |
| 133 | .scan_index = -1, |
| 134 | .scan_type = { |
| 135 | .endianness = IIO_BE, |
| 136 | }, |
Matt Ranostay | cd8d9777 | 2015-09-13 20:26:14 -0700 | [diff] [blame] | 137 | }, |
| 138 | }; |
| 139 | |
| 140 | static IIO_CONST_ATTR(in_concentration_co2_scale, "0.00000698689"); |
| 141 | static IIO_CONST_ATTR(in_concentration_voc_scale, "0.00000000436681223"); |
| 142 | |
| 143 | static struct attribute *vz89x_attributes[] = { |
| 144 | &iio_const_attr_in_concentration_co2_scale.dev_attr.attr, |
| 145 | &iio_const_attr_in_concentration_voc_scale.dev_attr.attr, |
| 146 | NULL, |
| 147 | }; |
| 148 | |
| 149 | static const struct attribute_group vz89x_attrs_group = { |
| 150 | .attrs = vz89x_attributes, |
| 151 | }; |
| 152 | |
Matt Ranostay | bf23cee | 2015-09-22 21:25:09 -0700 | [diff] [blame] | 153 | /* |
| 154 | * Chipset sometime updates in the middle of a reading causing it to reset the |
| 155 | * data pointer, and causing invalid reading of previous data. |
| 156 | * We can check for this by reading MSB of the resistance reading that is |
| 157 | * always zero, and by also confirming the VOC_short isn't zero. |
| 158 | */ |
| 159 | |
Matt Ranostay | 8376882 | 2016-08-24 23:44:47 -0700 | [diff] [blame] | 160 | static bool vz89x_measurement_is_valid(struct vz89x_data *data) |
Matt Ranostay | bf23cee | 2015-09-22 21:25:09 -0700 | [diff] [blame] | 161 | { |
| 162 | if (data->buffer[VZ89X_VOC_SHORT_IDX] == 0) |
kbuild test robot | 2535cc7 | 2016-09-01 08:38:38 +0800 | [diff] [blame] | 163 | return true; |
Matt Ranostay | bf23cee | 2015-09-22 21:25:09 -0700 | [diff] [blame] | 164 | |
Matt Ranostay | 8376882 | 2016-08-24 23:44:47 -0700 | [diff] [blame] | 165 | return !!(data->buffer[data->chip->read_size - 1] > 0); |
Matt Ranostay | bf23cee | 2015-09-22 21:25:09 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Matt Ranostay | 0a735aa | 2016-08-24 23:44:48 -0700 | [diff] [blame] | 168 | /* VZ89TE device has a modified CRC-8 two complement check */ |
| 169 | static bool vz89te_measurement_is_valid(struct vz89x_data *data) |
| 170 | { |
| 171 | u8 crc = 0; |
| 172 | int i, sum = 0; |
| 173 | |
| 174 | for (i = 0; i < (data->chip->read_size - 1); i++) { |
| 175 | sum = crc + data->buffer[i]; |
| 176 | crc = sum; |
| 177 | crc += sum / 256; |
| 178 | } |
| 179 | |
| 180 | return !((0xff - crc) == data->buffer[data->chip->read_size - 1]); |
| 181 | } |
| 182 | |
Matt Ranostay | 93e87d7 | 2015-12-01 21:47:20 -0800 | [diff] [blame] | 183 | static int vz89x_i2c_xfer(struct vz89x_data *data, u8 cmd) |
| 184 | { |
Matt Ranostay | 8376882 | 2016-08-24 23:44:47 -0700 | [diff] [blame] | 185 | const struct vz89x_chip_data *chip = data->chip; |
Matt Ranostay | 93e87d7 | 2015-12-01 21:47:20 -0800 | [diff] [blame] | 186 | struct i2c_client *client = data->client; |
| 187 | struct i2c_msg msg[2]; |
| 188 | int ret; |
Matt Ranostay | 0a735aa | 2016-08-24 23:44:48 -0700 | [diff] [blame] | 189 | u8 buf[6] = { cmd, 0, 0, 0, 0, 0xf3 }; |
Matt Ranostay | 93e87d7 | 2015-12-01 21:47:20 -0800 | [diff] [blame] | 190 | |
| 191 | msg[0].addr = client->addr; |
| 192 | msg[0].flags = client->flags; |
Matt Ranostay | 8376882 | 2016-08-24 23:44:47 -0700 | [diff] [blame] | 193 | msg[0].len = chip->write_size; |
Matt Ranostay | 93e87d7 | 2015-12-01 21:47:20 -0800 | [diff] [blame] | 194 | msg[0].buf = (char *) &buf; |
| 195 | |
| 196 | msg[1].addr = client->addr; |
| 197 | msg[1].flags = client->flags | I2C_M_RD; |
Matt Ranostay | 8376882 | 2016-08-24 23:44:47 -0700 | [diff] [blame] | 198 | msg[1].len = chip->read_size; |
Matt Ranostay | 93e87d7 | 2015-12-01 21:47:20 -0800 | [diff] [blame] | 199 | msg[1].buf = (char *) &data->buffer; |
| 200 | |
| 201 | ret = i2c_transfer(client->adapter, msg, 2); |
| 202 | |
| 203 | return (ret == 2) ? 0 : ret; |
| 204 | } |
| 205 | |
| 206 | static int vz89x_smbus_xfer(struct vz89x_data *data, u8 cmd) |
| 207 | { |
| 208 | struct i2c_client *client = data->client; |
| 209 | int ret; |
| 210 | int i; |
| 211 | |
| 212 | ret = i2c_smbus_write_word_data(client, cmd, 0); |
| 213 | if (ret < 0) |
| 214 | return ret; |
| 215 | |
Matt Ranostay | 8376882 | 2016-08-24 23:44:47 -0700 | [diff] [blame] | 216 | for (i = 0; i < data->chip->read_size; i++) { |
Matt Ranostay | 93e87d7 | 2015-12-01 21:47:20 -0800 | [diff] [blame] | 217 | ret = i2c_smbus_read_byte(client); |
| 218 | if (ret < 0) |
| 219 | return ret; |
| 220 | data->buffer[i] = ret; |
| 221 | } |
| 222 | |
| 223 | return 0; |
| 224 | } |
| 225 | |
Matt Ranostay | cd8d9777 | 2015-09-13 20:26:14 -0700 | [diff] [blame] | 226 | static int vz89x_get_measurement(struct vz89x_data *data) |
| 227 | { |
Matt Ranostay | 8376882 | 2016-08-24 23:44:47 -0700 | [diff] [blame] | 228 | const struct vz89x_chip_data *chip = data->chip; |
Matt Ranostay | cd8d9777 | 2015-09-13 20:26:14 -0700 | [diff] [blame] | 229 | int ret; |
Matt Ranostay | cd8d9777 | 2015-09-13 20:26:14 -0700 | [diff] [blame] | 230 | |
| 231 | /* sensor can only be polled once a second max per datasheet */ |
| 232 | if (!time_after(jiffies, data->last_update + HZ)) |
Matt Ranostay | 9d1894c | 2016-08-24 23:44:49 -0700 | [diff] [blame] | 233 | return data->is_valid ? 0 : -EAGAIN; |
| 234 | |
| 235 | data->is_valid = false; |
| 236 | data->last_update = jiffies; |
Matt Ranostay | cd8d9777 | 2015-09-13 20:26:14 -0700 | [diff] [blame] | 237 | |
Matt Ranostay | 8376882 | 2016-08-24 23:44:47 -0700 | [diff] [blame] | 238 | ret = data->xfer(data, chip->cmd); |
Matt Ranostay | cd8d9777 | 2015-09-13 20:26:14 -0700 | [diff] [blame] | 239 | if (ret < 0) |
| 240 | return ret; |
| 241 | |
Matt Ranostay | 8376882 | 2016-08-24 23:44:47 -0700 | [diff] [blame] | 242 | ret = chip->valid(data); |
Matt Ranostay | bf23cee | 2015-09-22 21:25:09 -0700 | [diff] [blame] | 243 | if (ret) |
| 244 | return -EAGAIN; |
| 245 | |
Matt Ranostay | 9d1894c | 2016-08-24 23:44:49 -0700 | [diff] [blame] | 246 | data->is_valid = true; |
Matt Ranostay | cd8d9777 | 2015-09-13 20:26:14 -0700 | [diff] [blame] | 247 | |
| 248 | return 0; |
| 249 | } |
| 250 | |
Matt Ranostay | 0a735aa | 2016-08-24 23:44:48 -0700 | [diff] [blame] | 251 | static int vz89x_get_resistance_reading(struct vz89x_data *data, |
| 252 | struct iio_chan_spec const *chan, |
| 253 | int *val) |
Matt Ranostay | cd8d9777 | 2015-09-13 20:26:14 -0700 | [diff] [blame] | 254 | { |
Matt Ranostay | 0a735aa | 2016-08-24 23:44:48 -0700 | [diff] [blame] | 255 | u8 *tmp = (u8 *) &data->buffer[chan->address]; |
Matt Ranostay | cd8d9777 | 2015-09-13 20:26:14 -0700 | [diff] [blame] | 256 | |
Matt Ranostay | 0a735aa | 2016-08-24 23:44:48 -0700 | [diff] [blame] | 257 | switch (chan->scan_type.endianness) { |
| 258 | case IIO_LE: |
| 259 | *val = le32_to_cpup((__le32 *) tmp) & GENMASK(23, 0); |
| 260 | break; |
| 261 | case IIO_BE: |
| 262 | *val = be32_to_cpup((__be32 *) tmp) >> 8; |
| 263 | break; |
| 264 | default: |
| 265 | return -EINVAL; |
| 266 | } |
| 267 | |
| 268 | return 0; |
Matt Ranostay | cd8d9777 | 2015-09-13 20:26:14 -0700 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | static int vz89x_read_raw(struct iio_dev *indio_dev, |
| 272 | struct iio_chan_spec const *chan, int *val, |
| 273 | int *val2, long mask) |
| 274 | { |
| 275 | struct vz89x_data *data = iio_priv(indio_dev); |
| 276 | int ret = -EINVAL; |
| 277 | |
| 278 | switch (mask) { |
| 279 | case IIO_CHAN_INFO_RAW: |
| 280 | mutex_lock(&data->lock); |
| 281 | ret = vz89x_get_measurement(data); |
| 282 | mutex_unlock(&data->lock); |
| 283 | |
| 284 | if (ret) |
| 285 | return ret; |
| 286 | |
Matt Ranostay | 0a735aa | 2016-08-24 23:44:48 -0700 | [diff] [blame] | 287 | switch (chan->type) { |
| 288 | case IIO_CONCENTRATION: |
Matt Ranostay | cd8d9777 | 2015-09-13 20:26:14 -0700 | [diff] [blame] | 289 | *val = data->buffer[chan->address]; |
| 290 | return IIO_VAL_INT; |
Matt Ranostay | 0a735aa | 2016-08-24 23:44:48 -0700 | [diff] [blame] | 291 | case IIO_RESISTANCE: |
| 292 | ret = vz89x_get_resistance_reading(data, chan, val); |
| 293 | if (!ret) |
| 294 | return IIO_VAL_INT; |
| 295 | break; |
Matt Ranostay | cd8d9777 | 2015-09-13 20:26:14 -0700 | [diff] [blame] | 296 | default: |
| 297 | return -EINVAL; |
| 298 | } |
| 299 | break; |
| 300 | case IIO_CHAN_INFO_SCALE: |
| 301 | switch (chan->type) { |
| 302 | case IIO_RESISTANCE: |
| 303 | *val = 10; |
| 304 | return IIO_VAL_INT; |
| 305 | default: |
| 306 | return -EINVAL; |
| 307 | } |
| 308 | break; |
| 309 | case IIO_CHAN_INFO_OFFSET: |
Matt Ranostay | 0a735aa | 2016-08-24 23:44:48 -0700 | [diff] [blame] | 310 | switch (chan->channel2) { |
| 311 | case IIO_MOD_CO2: |
Matt Ranostay | cd8d9777 | 2015-09-13 20:26:14 -0700 | [diff] [blame] | 312 | *val = 44; |
| 313 | *val2 = 250000; |
| 314 | return IIO_VAL_INT_PLUS_MICRO; |
Matt Ranostay | 0a735aa | 2016-08-24 23:44:48 -0700 | [diff] [blame] | 315 | case IIO_MOD_VOC: |
Matt Ranostay | cd8d9777 | 2015-09-13 20:26:14 -0700 | [diff] [blame] | 316 | *val = -13; |
| 317 | return IIO_VAL_INT; |
| 318 | default: |
| 319 | return -EINVAL; |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | return ret; |
| 324 | } |
| 325 | |
| 326 | static const struct iio_info vz89x_info = { |
| 327 | .attrs = &vz89x_attrs_group, |
| 328 | .read_raw = vz89x_read_raw, |
| 329 | .driver_module = THIS_MODULE, |
| 330 | }; |
| 331 | |
Matt Ranostay | 8376882 | 2016-08-24 23:44:47 -0700 | [diff] [blame] | 332 | static const struct vz89x_chip_data vz89x_chips[] = { |
| 333 | { |
| 334 | .valid = vz89x_measurement_is_valid, |
| 335 | |
| 336 | .cmd = VZ89X_REG_MEASUREMENT, |
| 337 | .read_size = VZ89X_REG_MEASUREMENT_RD_SIZE, |
| 338 | .write_size = VZ89X_REG_MEASUREMENT_WR_SIZE, |
| 339 | |
| 340 | .channels = vz89x_channels, |
| 341 | .num_channels = ARRAY_SIZE(vz89x_channels), |
| 342 | }, |
Matt Ranostay | 0a735aa | 2016-08-24 23:44:48 -0700 | [diff] [blame] | 343 | { |
| 344 | .valid = vz89te_measurement_is_valid, |
| 345 | |
| 346 | .cmd = VZ89TE_REG_MEASUREMENT, |
| 347 | .read_size = VZ89TE_REG_MEASUREMENT_RD_SIZE, |
| 348 | .write_size = VZ89TE_REG_MEASUREMENT_WR_SIZE, |
| 349 | |
| 350 | .channels = vz89te_channels, |
| 351 | .num_channels = ARRAY_SIZE(vz89te_channels), |
| 352 | }, |
Matt Ranostay | 8376882 | 2016-08-24 23:44:47 -0700 | [diff] [blame] | 353 | }; |
| 354 | |
| 355 | static const struct of_device_id vz89x_dt_ids[] = { |
| 356 | { .compatible = "sgx,vz89x", .data = (void *) VZ89X }, |
Matt Ranostay | 0a735aa | 2016-08-24 23:44:48 -0700 | [diff] [blame] | 357 | { .compatible = "sgx,vz89te", .data = (void *) VZ89TE }, |
Matt Ranostay | 8376882 | 2016-08-24 23:44:47 -0700 | [diff] [blame] | 358 | { } |
| 359 | }; |
| 360 | MODULE_DEVICE_TABLE(of, vz89x_dt_ids); |
| 361 | |
Matt Ranostay | cd8d9777 | 2015-09-13 20:26:14 -0700 | [diff] [blame] | 362 | static int vz89x_probe(struct i2c_client *client, |
| 363 | const struct i2c_device_id *id) |
| 364 | { |
| 365 | struct iio_dev *indio_dev; |
| 366 | struct vz89x_data *data; |
Matt Ranostay | 8376882 | 2016-08-24 23:44:47 -0700 | [diff] [blame] | 367 | const struct of_device_id *of_id; |
| 368 | int chip_id; |
Matt Ranostay | cd8d9777 | 2015-09-13 20:26:14 -0700 | [diff] [blame] | 369 | |
Matt Ranostay | cd8d9777 | 2015-09-13 20:26:14 -0700 | [diff] [blame] | 370 | indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); |
| 371 | if (!indio_dev) |
| 372 | return -ENOMEM; |
Matt Ranostay | cd8d9777 | 2015-09-13 20:26:14 -0700 | [diff] [blame] | 373 | data = iio_priv(indio_dev); |
Matt Ranostay | 93e87d7 | 2015-12-01 21:47:20 -0800 | [diff] [blame] | 374 | |
| 375 | if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) |
| 376 | data->xfer = vz89x_i2c_xfer; |
| 377 | else if (i2c_check_functionality(client->adapter, |
| 378 | I2C_FUNC_SMBUS_WORD_DATA | I2C_FUNC_SMBUS_BYTE)) |
| 379 | data->xfer = vz89x_smbus_xfer; |
| 380 | else |
Matt Ranostay | f8d9d3b | 2016-02-26 22:13:49 -0800 | [diff] [blame] | 381 | return -EOPNOTSUPP; |
Matt Ranostay | 93e87d7 | 2015-12-01 21:47:20 -0800 | [diff] [blame] | 382 | |
Matt Ranostay | 8376882 | 2016-08-24 23:44:47 -0700 | [diff] [blame] | 383 | of_id = of_match_device(vz89x_dt_ids, &client->dev); |
| 384 | if (!of_id) |
| 385 | chip_id = id->driver_data; |
| 386 | else |
| 387 | chip_id = (unsigned long)of_id->data; |
| 388 | |
Matt Ranostay | cd8d9777 | 2015-09-13 20:26:14 -0700 | [diff] [blame] | 389 | i2c_set_clientdata(client, indio_dev); |
| 390 | data->client = client; |
Matt Ranostay | 8376882 | 2016-08-24 23:44:47 -0700 | [diff] [blame] | 391 | data->chip = &vz89x_chips[chip_id]; |
Matt Ranostay | cd8d9777 | 2015-09-13 20:26:14 -0700 | [diff] [blame] | 392 | data->last_update = jiffies - HZ; |
| 393 | mutex_init(&data->lock); |
| 394 | |
| 395 | indio_dev->dev.parent = &client->dev; |
Arushi Singhal | 5e13b6f | 2017-03-30 18:11:22 +0530 | [diff] [blame] | 396 | indio_dev->info = &vz89x_info; |
Matt Ranostay | cd8d9777 | 2015-09-13 20:26:14 -0700 | [diff] [blame] | 397 | indio_dev->name = dev_name(&client->dev); |
| 398 | indio_dev->modes = INDIO_DIRECT_MODE; |
| 399 | |
Matt Ranostay | 8376882 | 2016-08-24 23:44:47 -0700 | [diff] [blame] | 400 | indio_dev->channels = data->chip->channels; |
| 401 | indio_dev->num_channels = data->chip->num_channels; |
Matt Ranostay | cd8d9777 | 2015-09-13 20:26:14 -0700 | [diff] [blame] | 402 | |
| 403 | return devm_iio_device_register(&client->dev, indio_dev); |
| 404 | } |
| 405 | |
| 406 | static const struct i2c_device_id vz89x_id[] = { |
Matt Ranostay | 8376882 | 2016-08-24 23:44:47 -0700 | [diff] [blame] | 407 | { "vz89x", VZ89X }, |
Matt Ranostay | 0a735aa | 2016-08-24 23:44:48 -0700 | [diff] [blame] | 408 | { "vz89te", VZ89TE }, |
Matt Ranostay | cd8d9777 | 2015-09-13 20:26:14 -0700 | [diff] [blame] | 409 | { } |
| 410 | }; |
| 411 | MODULE_DEVICE_TABLE(i2c, vz89x_id); |
| 412 | |
Matt Ranostay | cd8d9777 | 2015-09-13 20:26:14 -0700 | [diff] [blame] | 413 | static struct i2c_driver vz89x_driver = { |
| 414 | .driver = { |
| 415 | .name = "vz89x", |
| 416 | .of_match_table = of_match_ptr(vz89x_dt_ids), |
| 417 | }, |
| 418 | .probe = vz89x_probe, |
| 419 | .id_table = vz89x_id, |
| 420 | }; |
| 421 | module_i2c_driver(vz89x_driver); |
| 422 | |
| 423 | MODULE_AUTHOR("Matt Ranostay <mranostay@gmail.com>"); |
| 424 | MODULE_DESCRIPTION("SGX Sensortech MiCS VZ89X VOC sensors"); |
| 425 | MODULE_LICENSE("GPL v2"); |