mlx90614.c 18 KB

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