Kyle Swenson | 8d8f654 | 2021-03-15 11:02:55 -0600 | [diff] [blame^] | 1 | /* |
| 2 | * mlx90614.c - Support for Melexis MLX90614 contactless IR temperature sensor |
| 3 | * |
| 4 | * Copyright (c) 2014 Peter Meerwald <pmeerw@pmeerw.net> |
| 5 | * Copyright (c) 2015 Essensium NV |
| 6 | * Copyright (c) 2015 Melexis |
| 7 | * |
| 8 | * This file is subject to the terms and conditions of version 2 of |
| 9 | * the GNU General Public License. See the file COPYING in the main |
| 10 | * directory of this archive for more details. |
| 11 | * |
| 12 | * Driver for the Melexis MLX90614 I2C 16-bit IR thermopile sensor |
| 13 | * |
| 14 | * (7-bit I2C slave address 0x5a, 100KHz bus speed only!) |
| 15 | * |
| 16 | * To wake up from sleep mode, the SDA line must be held low while SCL is high |
| 17 | * for at least 33ms. This is achieved with an extra GPIO that can be connected |
| 18 | * directly to the SDA line. In normal operation, the GPIO is set as input and |
| 19 | * will not interfere in I2C communication. While the GPIO is driven low, the |
| 20 | * i2c adapter is locked since it cannot be used by other clients. The SCL line |
| 21 | * always has a pull-up so we do not need an extra GPIO to drive it high. If |
| 22 | * the "wakeup" GPIO is not given, power management will be disabled. |
| 23 | * |
| 24 | */ |
| 25 | |
| 26 | #include <linux/err.h> |
| 27 | #include <linux/i2c.h> |
| 28 | #include <linux/module.h> |
| 29 | #include <linux/delay.h> |
| 30 | #include <linux/jiffies.h> |
| 31 | #include <linux/gpio/consumer.h> |
| 32 | #include <linux/pm_runtime.h> |
| 33 | |
| 34 | #include <linux/iio/iio.h> |
| 35 | #include <linux/iio/sysfs.h> |
| 36 | |
| 37 | #define MLX90614_OP_RAM 0x00 |
| 38 | #define MLX90614_OP_EEPROM 0x20 |
| 39 | #define MLX90614_OP_SLEEP 0xff |
| 40 | |
| 41 | /* RAM offsets with 16-bit data, MSB first */ |
| 42 | #define MLX90614_RAW1 (MLX90614_OP_RAM | 0x04) /* raw data IR channel 1 */ |
| 43 | #define MLX90614_RAW2 (MLX90614_OP_RAM | 0x05) /* raw data IR channel 2 */ |
| 44 | #define MLX90614_TA (MLX90614_OP_RAM | 0x06) /* ambient temperature */ |
| 45 | #define MLX90614_TOBJ1 (MLX90614_OP_RAM | 0x07) /* object 1 temperature */ |
| 46 | #define MLX90614_TOBJ2 (MLX90614_OP_RAM | 0x08) /* object 2 temperature */ |
| 47 | |
| 48 | /* EEPROM offsets with 16-bit data, MSB first */ |
| 49 | #define MLX90614_EMISSIVITY (MLX90614_OP_EEPROM | 0x04) /* emissivity correction coefficient */ |
| 50 | #define MLX90614_CONFIG (MLX90614_OP_EEPROM | 0x05) /* configuration register */ |
| 51 | |
| 52 | /* Control bits in configuration register */ |
| 53 | #define MLX90614_CONFIG_IIR_SHIFT 0 /* IIR coefficient */ |
| 54 | #define MLX90614_CONFIG_IIR_MASK (0x7 << MLX90614_CONFIG_IIR_SHIFT) |
| 55 | #define MLX90614_CONFIG_DUAL_SHIFT 6 /* single (0) or dual (1) IR sensor */ |
| 56 | #define MLX90614_CONFIG_DUAL_MASK (1 << MLX90614_CONFIG_DUAL_SHIFT) |
| 57 | #define MLX90614_CONFIG_FIR_SHIFT 8 /* FIR coefficient */ |
| 58 | #define MLX90614_CONFIG_FIR_MASK (0x7 << MLX90614_CONFIG_FIR_SHIFT) |
| 59 | #define MLX90614_CONFIG_GAIN_SHIFT 11 /* gain */ |
| 60 | #define MLX90614_CONFIG_GAIN_MASK (0x7 << MLX90614_CONFIG_GAIN_SHIFT) |
| 61 | |
| 62 | /* Timings (in ms) */ |
| 63 | #define MLX90614_TIMING_EEPROM 20 /* time for EEPROM write/erase to complete */ |
| 64 | #define MLX90614_TIMING_WAKEUP 34 /* time to hold SDA low for wake-up */ |
| 65 | #define MLX90614_TIMING_STARTUP 250 /* time before first data after wake-up */ |
| 66 | |
| 67 | #define MLX90614_AUTOSLEEP_DELAY 5000 /* default autosleep delay */ |
| 68 | |
| 69 | /* Magic constants */ |
| 70 | #define MLX90614_CONST_OFFSET_DEC -13657 /* decimal part of the Kelvin offset */ |
| 71 | #define MLX90614_CONST_OFFSET_REM 500000 /* remainder of offset (273.15*50) */ |
| 72 | #define MLX90614_CONST_SCALE 20 /* Scale in milliKelvin (0.02 * 1000) */ |
| 73 | #define MLX90614_CONST_RAW_EMISSIVITY_MAX 65535 /* max value for emissivity */ |
| 74 | #define MLX90614_CONST_EMISSIVITY_RESOLUTION 15259 /* 1/65535 ~ 0.000015259 */ |
| 75 | #define MLX90614_CONST_FIR 0x7 /* Fixed value for FIR part of low pass filter */ |
| 76 | |
| 77 | struct mlx90614_data { |
| 78 | struct i2c_client *client; |
| 79 | struct mutex lock; /* for EEPROM access only */ |
| 80 | struct gpio_desc *wakeup_gpio; /* NULL to disable sleep/wake-up */ |
| 81 | unsigned long ready_timestamp; /* in jiffies */ |
| 82 | }; |
| 83 | |
| 84 | /* Bandwidth values for IIR filtering */ |
| 85 | static const int mlx90614_iir_values[] = {77, 31, 20, 15, 723, 153, 110, 86}; |
| 86 | static IIO_CONST_ATTR(in_temp_object_filter_low_pass_3db_frequency_available, |
| 87 | "0.15 0.20 0.31 0.77 0.86 1.10 1.53 7.23"); |
| 88 | |
| 89 | static struct attribute *mlx90614_attributes[] = { |
| 90 | &iio_const_attr_in_temp_object_filter_low_pass_3db_frequency_available.dev_attr.attr, |
| 91 | NULL, |
| 92 | }; |
| 93 | |
| 94 | static const struct attribute_group mlx90614_attr_group = { |
| 95 | .attrs = mlx90614_attributes, |
| 96 | }; |
| 97 | |
| 98 | /* |
| 99 | * Erase an address and write word. |
| 100 | * The mutex must be locked before calling. |
| 101 | */ |
| 102 | static s32 mlx90614_write_word(const struct i2c_client *client, u8 command, |
| 103 | u16 value) |
| 104 | { |
| 105 | /* |
| 106 | * Note: The mlx90614 requires a PEC on writing but does not send us a |
| 107 | * valid PEC on reading. Hence, we cannot set I2C_CLIENT_PEC in |
| 108 | * i2c_client.flags. As a workaround, we use i2c_smbus_xfer here. |
| 109 | */ |
| 110 | union i2c_smbus_data data; |
| 111 | s32 ret; |
| 112 | |
| 113 | dev_dbg(&client->dev, "Writing 0x%x to address 0x%x", value, command); |
| 114 | |
| 115 | data.word = 0x0000; /* erase command */ |
| 116 | ret = i2c_smbus_xfer(client->adapter, client->addr, |
| 117 | client->flags | I2C_CLIENT_PEC, |
| 118 | I2C_SMBUS_WRITE, command, |
| 119 | I2C_SMBUS_WORD_DATA, &data); |
| 120 | if (ret < 0) |
| 121 | return ret; |
| 122 | |
| 123 | msleep(MLX90614_TIMING_EEPROM); |
| 124 | |
| 125 | data.word = value; /* actual write */ |
| 126 | ret = i2c_smbus_xfer(client->adapter, client->addr, |
| 127 | client->flags | I2C_CLIENT_PEC, |
| 128 | I2C_SMBUS_WRITE, command, |
| 129 | I2C_SMBUS_WORD_DATA, &data); |
| 130 | |
| 131 | msleep(MLX90614_TIMING_EEPROM); |
| 132 | |
| 133 | return ret; |
| 134 | } |
| 135 | |
| 136 | /* |
| 137 | * Find the IIR value inside mlx90614_iir_values array and return its position |
| 138 | * which is equivalent to the bit value in sensor register |
| 139 | */ |
| 140 | static inline s32 mlx90614_iir_search(const struct i2c_client *client, |
| 141 | int value) |
| 142 | { |
| 143 | int i; |
| 144 | s32 ret; |
| 145 | |
| 146 | for (i = 0; i < ARRAY_SIZE(mlx90614_iir_values); ++i) { |
| 147 | if (value == mlx90614_iir_values[i]) |
| 148 | break; |
| 149 | } |
| 150 | |
| 151 | if (i == ARRAY_SIZE(mlx90614_iir_values)) |
| 152 | return -EINVAL; |
| 153 | |
| 154 | /* |
| 155 | * CONFIG register values must not be changed so |
| 156 | * we must read them before we actually write |
| 157 | * changes |
| 158 | */ |
| 159 | ret = i2c_smbus_read_word_data(client, MLX90614_CONFIG); |
| 160 | if (ret < 0) |
| 161 | return ret; |
| 162 | |
| 163 | ret &= ~MLX90614_CONFIG_FIR_MASK; |
| 164 | ret |= MLX90614_CONST_FIR << MLX90614_CONFIG_FIR_SHIFT; |
| 165 | ret &= ~MLX90614_CONFIG_IIR_MASK; |
| 166 | ret |= i << MLX90614_CONFIG_IIR_SHIFT; |
| 167 | |
| 168 | /* Write changed values */ |
| 169 | ret = mlx90614_write_word(client, MLX90614_CONFIG, ret); |
| 170 | return ret; |
| 171 | } |
| 172 | |
| 173 | #ifdef CONFIG_PM |
| 174 | /* |
| 175 | * If @startup is true, make sure MLX90614_TIMING_STARTUP ms have elapsed since |
| 176 | * the last wake-up. This is normally only needed to get a valid temperature |
| 177 | * reading. EEPROM access does not need such delay. |
| 178 | * Return 0 on success, <0 on error. |
| 179 | */ |
| 180 | static int mlx90614_power_get(struct mlx90614_data *data, bool startup) |
| 181 | { |
| 182 | unsigned long now; |
| 183 | |
| 184 | if (!data->wakeup_gpio) |
| 185 | return 0; |
| 186 | |
| 187 | pm_runtime_get_sync(&data->client->dev); |
| 188 | |
| 189 | if (startup) { |
| 190 | now = jiffies; |
| 191 | if (time_before(now, data->ready_timestamp) && |
| 192 | msleep_interruptible(jiffies_to_msecs( |
| 193 | data->ready_timestamp - now)) != 0) { |
| 194 | pm_runtime_put_autosuspend(&data->client->dev); |
| 195 | return -EINTR; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | return 0; |
| 200 | } |
| 201 | |
| 202 | static void mlx90614_power_put(struct mlx90614_data *data) |
| 203 | { |
| 204 | if (!data->wakeup_gpio) |
| 205 | return; |
| 206 | |
| 207 | pm_runtime_mark_last_busy(&data->client->dev); |
| 208 | pm_runtime_put_autosuspend(&data->client->dev); |
| 209 | } |
| 210 | #else |
| 211 | static inline int mlx90614_power_get(struct mlx90614_data *data, bool startup) |
| 212 | { |
| 213 | return 0; |
| 214 | } |
| 215 | |
| 216 | static inline void mlx90614_power_put(struct mlx90614_data *data) |
| 217 | { |
| 218 | } |
| 219 | #endif |
| 220 | |
| 221 | static int mlx90614_read_raw(struct iio_dev *indio_dev, |
| 222 | struct iio_chan_spec const *channel, int *val, |
| 223 | int *val2, long mask) |
| 224 | { |
| 225 | struct mlx90614_data *data = iio_priv(indio_dev); |
| 226 | u8 cmd; |
| 227 | s32 ret; |
| 228 | |
| 229 | switch (mask) { |
| 230 | case IIO_CHAN_INFO_RAW: /* 0.02K / LSB */ |
| 231 | switch (channel->channel2) { |
| 232 | case IIO_MOD_TEMP_AMBIENT: |
| 233 | cmd = MLX90614_TA; |
| 234 | break; |
| 235 | case IIO_MOD_TEMP_OBJECT: |
| 236 | switch (channel->channel) { |
| 237 | case 0: |
| 238 | cmd = MLX90614_TOBJ1; |
| 239 | break; |
| 240 | case 1: |
| 241 | cmd = MLX90614_TOBJ2; |
| 242 | break; |
| 243 | default: |
| 244 | return -EINVAL; |
| 245 | } |
| 246 | break; |
| 247 | default: |
| 248 | return -EINVAL; |
| 249 | } |
| 250 | |
| 251 | ret = mlx90614_power_get(data, true); |
| 252 | if (ret < 0) |
| 253 | return ret; |
| 254 | ret = i2c_smbus_read_word_data(data->client, cmd); |
| 255 | mlx90614_power_put(data); |
| 256 | |
| 257 | if (ret < 0) |
| 258 | return ret; |
| 259 | |
| 260 | /* MSB is an error flag */ |
| 261 | if (ret & 0x8000) |
| 262 | return -EIO; |
| 263 | |
| 264 | *val = ret; |
| 265 | return IIO_VAL_INT; |
| 266 | case IIO_CHAN_INFO_OFFSET: |
| 267 | *val = MLX90614_CONST_OFFSET_DEC; |
| 268 | *val2 = MLX90614_CONST_OFFSET_REM; |
| 269 | return IIO_VAL_INT_PLUS_MICRO; |
| 270 | case IIO_CHAN_INFO_SCALE: |
| 271 | *val = MLX90614_CONST_SCALE; |
| 272 | return IIO_VAL_INT; |
| 273 | case IIO_CHAN_INFO_CALIBEMISSIVITY: /* 1/65535 / LSB */ |
| 274 | mlx90614_power_get(data, false); |
| 275 | mutex_lock(&data->lock); |
| 276 | ret = i2c_smbus_read_word_data(data->client, |
| 277 | MLX90614_EMISSIVITY); |
| 278 | mutex_unlock(&data->lock); |
| 279 | mlx90614_power_put(data); |
| 280 | |
| 281 | if (ret < 0) |
| 282 | return ret; |
| 283 | |
| 284 | if (ret == MLX90614_CONST_RAW_EMISSIVITY_MAX) { |
| 285 | *val = 1; |
| 286 | *val2 = 0; |
| 287 | } else { |
| 288 | *val = 0; |
| 289 | *val2 = ret * MLX90614_CONST_EMISSIVITY_RESOLUTION; |
| 290 | } |
| 291 | return IIO_VAL_INT_PLUS_NANO; |
| 292 | case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY: /* IIR setting with |
| 293 | FIR = 1024 */ |
| 294 | mlx90614_power_get(data, false); |
| 295 | mutex_lock(&data->lock); |
| 296 | ret = i2c_smbus_read_word_data(data->client, MLX90614_CONFIG); |
| 297 | mutex_unlock(&data->lock); |
| 298 | mlx90614_power_put(data); |
| 299 | |
| 300 | if (ret < 0) |
| 301 | return ret; |
| 302 | |
| 303 | *val = mlx90614_iir_values[ret & MLX90614_CONFIG_IIR_MASK] / 100; |
| 304 | *val2 = (mlx90614_iir_values[ret & MLX90614_CONFIG_IIR_MASK] % 100) * |
| 305 | 10000; |
| 306 | return IIO_VAL_INT_PLUS_MICRO; |
| 307 | default: |
| 308 | return -EINVAL; |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | static int mlx90614_write_raw(struct iio_dev *indio_dev, |
| 313 | struct iio_chan_spec const *channel, int val, |
| 314 | int val2, long mask) |
| 315 | { |
| 316 | struct mlx90614_data *data = iio_priv(indio_dev); |
| 317 | s32 ret; |
| 318 | |
| 319 | switch (mask) { |
| 320 | case IIO_CHAN_INFO_CALIBEMISSIVITY: /* 1/65535 / LSB */ |
| 321 | if (val < 0 || val2 < 0 || val > 1 || (val == 1 && val2 != 0)) |
| 322 | return -EINVAL; |
| 323 | val = val * MLX90614_CONST_RAW_EMISSIVITY_MAX + |
| 324 | val2 / MLX90614_CONST_EMISSIVITY_RESOLUTION; |
| 325 | |
| 326 | mlx90614_power_get(data, false); |
| 327 | mutex_lock(&data->lock); |
| 328 | ret = mlx90614_write_word(data->client, MLX90614_EMISSIVITY, |
| 329 | val); |
| 330 | mutex_unlock(&data->lock); |
| 331 | mlx90614_power_put(data); |
| 332 | |
| 333 | return ret; |
| 334 | case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY: /* IIR Filter setting */ |
| 335 | if (val < 0 || val2 < 0) |
| 336 | return -EINVAL; |
| 337 | |
| 338 | mlx90614_power_get(data, false); |
| 339 | mutex_lock(&data->lock); |
| 340 | ret = mlx90614_iir_search(data->client, |
| 341 | val * 100 + val2 / 10000); |
| 342 | mutex_unlock(&data->lock); |
| 343 | mlx90614_power_put(data); |
| 344 | |
| 345 | return ret; |
| 346 | default: |
| 347 | return -EINVAL; |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | static int mlx90614_write_raw_get_fmt(struct iio_dev *indio_dev, |
| 352 | struct iio_chan_spec const *channel, |
| 353 | long mask) |
| 354 | { |
| 355 | switch (mask) { |
| 356 | case IIO_CHAN_INFO_CALIBEMISSIVITY: |
| 357 | return IIO_VAL_INT_PLUS_NANO; |
| 358 | case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY: |
| 359 | return IIO_VAL_INT_PLUS_MICRO; |
| 360 | default: |
| 361 | return -EINVAL; |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | static const struct iio_chan_spec mlx90614_channels[] = { |
| 366 | { |
| 367 | .type = IIO_TEMP, |
| 368 | .modified = 1, |
| 369 | .channel2 = IIO_MOD_TEMP_AMBIENT, |
| 370 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), |
| 371 | .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) | |
| 372 | BIT(IIO_CHAN_INFO_SCALE), |
| 373 | }, |
| 374 | { |
| 375 | .type = IIO_TEMP, |
| 376 | .modified = 1, |
| 377 | .channel2 = IIO_MOD_TEMP_OBJECT, |
| 378 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | |
| 379 | BIT(IIO_CHAN_INFO_CALIBEMISSIVITY) | |
| 380 | BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), |
| 381 | .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) | |
| 382 | BIT(IIO_CHAN_INFO_SCALE), |
| 383 | }, |
| 384 | { |
| 385 | .type = IIO_TEMP, |
| 386 | .indexed = 1, |
| 387 | .modified = 1, |
| 388 | .channel = 1, |
| 389 | .channel2 = IIO_MOD_TEMP_OBJECT, |
| 390 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | |
| 391 | BIT(IIO_CHAN_INFO_CALIBEMISSIVITY) | |
| 392 | BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), |
| 393 | .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) | |
| 394 | BIT(IIO_CHAN_INFO_SCALE), |
| 395 | }, |
| 396 | }; |
| 397 | |
| 398 | static const struct iio_info mlx90614_info = { |
| 399 | .read_raw = mlx90614_read_raw, |
| 400 | .write_raw = mlx90614_write_raw, |
| 401 | .write_raw_get_fmt = mlx90614_write_raw_get_fmt, |
| 402 | .attrs = &mlx90614_attr_group, |
| 403 | .driver_module = THIS_MODULE, |
| 404 | }; |
| 405 | |
| 406 | #ifdef CONFIG_PM |
| 407 | static int mlx90614_sleep(struct mlx90614_data *data) |
| 408 | { |
| 409 | s32 ret; |
| 410 | |
| 411 | if (!data->wakeup_gpio) { |
| 412 | dev_dbg(&data->client->dev, "Sleep disabled"); |
| 413 | return -ENOSYS; |
| 414 | } |
| 415 | |
| 416 | dev_dbg(&data->client->dev, "Requesting sleep"); |
| 417 | |
| 418 | mutex_lock(&data->lock); |
| 419 | ret = i2c_smbus_xfer(data->client->adapter, data->client->addr, |
| 420 | data->client->flags | I2C_CLIENT_PEC, |
| 421 | I2C_SMBUS_WRITE, MLX90614_OP_SLEEP, |
| 422 | I2C_SMBUS_BYTE, NULL); |
| 423 | mutex_unlock(&data->lock); |
| 424 | |
| 425 | return ret; |
| 426 | } |
| 427 | |
| 428 | static int mlx90614_wakeup(struct mlx90614_data *data) |
| 429 | { |
| 430 | if (!data->wakeup_gpio) { |
| 431 | dev_dbg(&data->client->dev, "Wake-up disabled"); |
| 432 | return -ENOSYS; |
| 433 | } |
| 434 | |
| 435 | dev_dbg(&data->client->dev, "Requesting wake-up"); |
| 436 | |
| 437 | i2c_lock_adapter(data->client->adapter); |
| 438 | gpiod_direction_output(data->wakeup_gpio, 0); |
| 439 | msleep(MLX90614_TIMING_WAKEUP); |
| 440 | gpiod_direction_input(data->wakeup_gpio); |
| 441 | i2c_unlock_adapter(data->client->adapter); |
| 442 | |
| 443 | data->ready_timestamp = jiffies + |
| 444 | msecs_to_jiffies(MLX90614_TIMING_STARTUP); |
| 445 | |
| 446 | /* |
| 447 | * Quirk: the i2c controller may get confused right after the |
| 448 | * wake-up signal has been sent. As a workaround, do a dummy read. |
| 449 | * If the read fails, the controller will probably be reset so that |
| 450 | * further reads will work. |
| 451 | */ |
| 452 | i2c_smbus_read_word_data(data->client, MLX90614_CONFIG); |
| 453 | |
| 454 | return 0; |
| 455 | } |
| 456 | |
| 457 | /* Return wake-up GPIO or NULL if sleep functionality should be disabled. */ |
| 458 | static struct gpio_desc *mlx90614_probe_wakeup(struct i2c_client *client) |
| 459 | { |
| 460 | struct gpio_desc *gpio; |
| 461 | |
| 462 | if (!i2c_check_functionality(client->adapter, |
| 463 | I2C_FUNC_SMBUS_WRITE_BYTE)) { |
| 464 | dev_info(&client->dev, |
| 465 | "i2c adapter does not support SMBUS_WRITE_BYTE, sleep disabled"); |
| 466 | return NULL; |
| 467 | } |
| 468 | |
| 469 | gpio = devm_gpiod_get_optional(&client->dev, "wakeup", GPIOD_IN); |
| 470 | |
| 471 | if (IS_ERR(gpio)) { |
| 472 | dev_warn(&client->dev, |
| 473 | "gpio acquisition failed with error %ld, sleep disabled", |
| 474 | PTR_ERR(gpio)); |
| 475 | return NULL; |
| 476 | } else if (!gpio) { |
| 477 | dev_info(&client->dev, |
| 478 | "wakeup-gpio not found, sleep disabled"); |
| 479 | } |
| 480 | |
| 481 | return gpio; |
| 482 | } |
| 483 | #else |
| 484 | static inline int mlx90614_sleep(struct mlx90614_data *data) |
| 485 | { |
| 486 | return -ENOSYS; |
| 487 | } |
| 488 | static inline int mlx90614_wakeup(struct mlx90614_data *data) |
| 489 | { |
| 490 | return -ENOSYS; |
| 491 | } |
| 492 | static inline struct gpio_desc *mlx90614_probe_wakeup(struct i2c_client *client) |
| 493 | { |
| 494 | return NULL; |
| 495 | } |
| 496 | #endif |
| 497 | |
| 498 | /* Return 0 for single sensor, 1 for dual sensor, <0 on error. */ |
| 499 | static int mlx90614_probe_num_ir_sensors(struct i2c_client *client) |
| 500 | { |
| 501 | s32 ret; |
| 502 | |
| 503 | ret = i2c_smbus_read_word_data(client, MLX90614_CONFIG); |
| 504 | |
| 505 | if (ret < 0) |
| 506 | return ret; |
| 507 | |
| 508 | return (ret & MLX90614_CONFIG_DUAL_MASK) ? 1 : 0; |
| 509 | } |
| 510 | |
| 511 | static int mlx90614_probe(struct i2c_client *client, |
| 512 | const struct i2c_device_id *id) |
| 513 | { |
| 514 | struct iio_dev *indio_dev; |
| 515 | struct mlx90614_data *data; |
| 516 | int ret; |
| 517 | |
| 518 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WORD_DATA)) |
| 519 | return -ENODEV; |
| 520 | |
| 521 | indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); |
| 522 | if (!indio_dev) |
| 523 | return -ENOMEM; |
| 524 | |
| 525 | data = iio_priv(indio_dev); |
| 526 | i2c_set_clientdata(client, indio_dev); |
| 527 | data->client = client; |
| 528 | mutex_init(&data->lock); |
| 529 | data->wakeup_gpio = mlx90614_probe_wakeup(client); |
| 530 | |
| 531 | mlx90614_wakeup(data); |
| 532 | |
| 533 | indio_dev->dev.parent = &client->dev; |
| 534 | indio_dev->name = id->name; |
| 535 | indio_dev->modes = INDIO_DIRECT_MODE; |
| 536 | indio_dev->info = &mlx90614_info; |
| 537 | |
| 538 | ret = mlx90614_probe_num_ir_sensors(client); |
| 539 | switch (ret) { |
| 540 | case 0: |
| 541 | dev_dbg(&client->dev, "Found single sensor"); |
| 542 | indio_dev->channels = mlx90614_channels; |
| 543 | indio_dev->num_channels = 2; |
| 544 | break; |
| 545 | case 1: |
| 546 | dev_dbg(&client->dev, "Found dual sensor"); |
| 547 | indio_dev->channels = mlx90614_channels; |
| 548 | indio_dev->num_channels = 3; |
| 549 | break; |
| 550 | default: |
| 551 | return ret; |
| 552 | } |
| 553 | |
| 554 | if (data->wakeup_gpio) { |
| 555 | pm_runtime_set_autosuspend_delay(&client->dev, |
| 556 | MLX90614_AUTOSLEEP_DELAY); |
| 557 | pm_runtime_use_autosuspend(&client->dev); |
| 558 | pm_runtime_set_active(&client->dev); |
| 559 | pm_runtime_enable(&client->dev); |
| 560 | } |
| 561 | |
| 562 | return iio_device_register(indio_dev); |
| 563 | } |
| 564 | |
| 565 | static int mlx90614_remove(struct i2c_client *client) |
| 566 | { |
| 567 | struct iio_dev *indio_dev = i2c_get_clientdata(client); |
| 568 | struct mlx90614_data *data = iio_priv(indio_dev); |
| 569 | |
| 570 | iio_device_unregister(indio_dev); |
| 571 | |
| 572 | if (data->wakeup_gpio) { |
| 573 | pm_runtime_disable(&client->dev); |
| 574 | if (!pm_runtime_status_suspended(&client->dev)) |
| 575 | mlx90614_sleep(data); |
| 576 | pm_runtime_set_suspended(&client->dev); |
| 577 | } |
| 578 | |
| 579 | return 0; |
| 580 | } |
| 581 | |
| 582 | static const struct i2c_device_id mlx90614_id[] = { |
| 583 | { "mlx90614", 0 }, |
| 584 | { } |
| 585 | }; |
| 586 | MODULE_DEVICE_TABLE(i2c, mlx90614_id); |
| 587 | |
| 588 | #ifdef CONFIG_PM_SLEEP |
| 589 | static int mlx90614_pm_suspend(struct device *dev) |
| 590 | { |
| 591 | struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); |
| 592 | struct mlx90614_data *data = iio_priv(indio_dev); |
| 593 | |
| 594 | if (data->wakeup_gpio && pm_runtime_active(dev)) |
| 595 | return mlx90614_sleep(data); |
| 596 | |
| 597 | return 0; |
| 598 | } |
| 599 | |
| 600 | static int mlx90614_pm_resume(struct device *dev) |
| 601 | { |
| 602 | struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); |
| 603 | struct mlx90614_data *data = iio_priv(indio_dev); |
| 604 | int err; |
| 605 | |
| 606 | if (data->wakeup_gpio) { |
| 607 | err = mlx90614_wakeup(data); |
| 608 | if (err < 0) |
| 609 | return err; |
| 610 | |
| 611 | pm_runtime_disable(dev); |
| 612 | pm_runtime_set_active(dev); |
| 613 | pm_runtime_enable(dev); |
| 614 | } |
| 615 | |
| 616 | return 0; |
| 617 | } |
| 618 | #endif |
| 619 | |
| 620 | #ifdef CONFIG_PM |
| 621 | static int mlx90614_pm_runtime_suspend(struct device *dev) |
| 622 | { |
| 623 | struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); |
| 624 | struct mlx90614_data *data = iio_priv(indio_dev); |
| 625 | |
| 626 | return mlx90614_sleep(data); |
| 627 | } |
| 628 | |
| 629 | static int mlx90614_pm_runtime_resume(struct device *dev) |
| 630 | { |
| 631 | struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); |
| 632 | struct mlx90614_data *data = iio_priv(indio_dev); |
| 633 | |
| 634 | return mlx90614_wakeup(data); |
| 635 | } |
| 636 | #endif |
| 637 | |
| 638 | static const struct dev_pm_ops mlx90614_pm_ops = { |
| 639 | SET_SYSTEM_SLEEP_PM_OPS(mlx90614_pm_suspend, mlx90614_pm_resume) |
| 640 | SET_RUNTIME_PM_OPS(mlx90614_pm_runtime_suspend, |
| 641 | mlx90614_pm_runtime_resume, NULL) |
| 642 | }; |
| 643 | |
| 644 | static struct i2c_driver mlx90614_driver = { |
| 645 | .driver = { |
| 646 | .name = "mlx90614", |
| 647 | .pm = &mlx90614_pm_ops, |
| 648 | }, |
| 649 | .probe = mlx90614_probe, |
| 650 | .remove = mlx90614_remove, |
| 651 | .id_table = mlx90614_id, |
| 652 | }; |
| 653 | module_i2c_driver(mlx90614_driver); |
| 654 | |
| 655 | MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>"); |
| 656 | MODULE_AUTHOR("Vianney le Clément de Saint-Marcq <vianney.leclement@essensium.com>"); |
| 657 | MODULE_AUTHOR("Crt Mori <cmo@melexis.com>"); |
| 658 | MODULE_DESCRIPTION("Melexis MLX90614 contactless IR temperature sensor driver"); |
| 659 | MODULE_LICENSE("GPL"); |