mlx90614.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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. *
  7. * This file is subject to the terms and conditions of version 2 of
  8. * the GNU General Public License. See the file COPYING in the main
  9. * directory of this archive for more details.
  10. *
  11. * Driver for the Melexis MLX90614 I2C 16-bit IR thermopile sensor
  12. *
  13. * (7-bit I2C slave address 0x5a, 100KHz bus speed only!)
  14. *
  15. * To wake up from sleep mode, the SDA line must be held low while SCL is high
  16. * for at least 33ms. This is achieved with an extra GPIO that can be connected
  17. * directly to the SDA line. In normal operation, the GPIO is set as input and
  18. * will not interfere in I2C communication. While the GPIO is driven low, the
  19. * i2c adapter is locked since it cannot be used by other clients. The SCL line
  20. * always has a pull-up so we do not need an extra GPIO to drive it high. If
  21. * the "wakeup" GPIO is not given, power management will be disabled.
  22. *
  23. * TODO: filter configuration
  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. #define MLX90614_OP_RAM 0x00
  34. #define MLX90614_OP_EEPROM 0x20
  35. #define MLX90614_OP_SLEEP 0xff
  36. /* RAM offsets with 16-bit data, MSB first */
  37. #define MLX90614_RAW1 (MLX90614_OP_RAM | 0x04) /* raw data IR channel 1 */
  38. #define MLX90614_RAW2 (MLX90614_OP_RAM | 0x05) /* raw data IR channel 2 */
  39. #define MLX90614_TA (MLX90614_OP_RAM | 0x06) /* ambient temperature */
  40. #define MLX90614_TOBJ1 (MLX90614_OP_RAM | 0x07) /* object 1 temperature */
  41. #define MLX90614_TOBJ2 (MLX90614_OP_RAM | 0x08) /* object 2 temperature */
  42. /* EEPROM offsets with 16-bit data, MSB first */
  43. #define MLX90614_EMISSIVITY (MLX90614_OP_EEPROM | 0x04) /* emissivity correction coefficient */
  44. #define MLX90614_CONFIG (MLX90614_OP_EEPROM | 0x05) /* configuration register */
  45. /* Control bits in configuration register */
  46. #define MLX90614_CONFIG_IIR_SHIFT 0 /* IIR coefficient */
  47. #define MLX90614_CONFIG_IIR_MASK (0x7 << MLX90614_CONFIG_IIR_SHIFT)
  48. #define MLX90614_CONFIG_DUAL_SHIFT 6 /* single (0) or dual (1) IR sensor */
  49. #define MLX90614_CONFIG_DUAL_MASK (1 << MLX90614_CONFIG_DUAL_SHIFT)
  50. #define MLX90614_CONFIG_FIR_SHIFT 8 /* FIR coefficient */
  51. #define MLX90614_CONFIG_FIR_MASK (0x7 << MLX90614_CONFIG_FIR_SHIFT)
  52. #define MLX90614_CONFIG_GAIN_SHIFT 11 /* gain */
  53. #define MLX90614_CONFIG_GAIN_MASK (0x7 << MLX90614_CONFIG_GAIN_SHIFT)
  54. /* Timings (in ms) */
  55. #define MLX90614_TIMING_EEPROM 20 /* time for EEPROM write/erase to complete */
  56. #define MLX90614_TIMING_WAKEUP 34 /* time to hold SDA low for wake-up */
  57. #define MLX90614_TIMING_STARTUP 250 /* time before first data after wake-up */
  58. #define MLX90614_AUTOSLEEP_DELAY 5000 /* default autosleep delay */
  59. /* Magic constants */
  60. #define MLX90614_CONST_OFFSET_DEC -13657 /* decimal part of the Kelvin offset */
  61. #define MLX90614_CONST_OFFSET_REM 500000 /* remainder of offset (273.15*50) */
  62. #define MLX90614_CONST_SCALE 20 /* Scale in milliKelvin (0.02 * 1000) */
  63. #define MLX90614_CONST_RAW_EMISSIVITY_MAX 65535 /* max value for emissivity */
  64. #define MLX90614_CONST_EMISSIVITY_RESOLUTION 15259 /* 1/65535 ~ 0.000015259 */
  65. struct mlx90614_data {
  66. struct i2c_client *client;
  67. struct mutex lock; /* for EEPROM access only */
  68. struct gpio_desc *wakeup_gpio; /* NULL to disable sleep/wake-up */
  69. unsigned long ready_timestamp; /* in jiffies */
  70. };
  71. /*
  72. * Erase an address and write word.
  73. * The mutex must be locked before calling.
  74. */
  75. static s32 mlx90614_write_word(const struct i2c_client *client, u8 command,
  76. u16 value)
  77. {
  78. /*
  79. * Note: The mlx90614 requires a PEC on writing but does not send us a
  80. * valid PEC on reading. Hence, we cannot set I2C_CLIENT_PEC in
  81. * i2c_client.flags. As a workaround, we use i2c_smbus_xfer here.
  82. */
  83. union i2c_smbus_data data;
  84. s32 ret;
  85. dev_dbg(&client->dev, "Writing 0x%x to address 0x%x", value, command);
  86. data.word = 0x0000; /* erase command */
  87. ret = i2c_smbus_xfer(client->adapter, client->addr,
  88. client->flags | I2C_CLIENT_PEC,
  89. I2C_SMBUS_WRITE, command,
  90. I2C_SMBUS_WORD_DATA, &data);
  91. if (ret < 0)
  92. return ret;
  93. msleep(MLX90614_TIMING_EEPROM);
  94. data.word = value; /* actual write */
  95. ret = i2c_smbus_xfer(client->adapter, client->addr,
  96. client->flags | I2C_CLIENT_PEC,
  97. I2C_SMBUS_WRITE, command,
  98. I2C_SMBUS_WORD_DATA, &data);
  99. msleep(MLX90614_TIMING_EEPROM);
  100. return ret;
  101. }
  102. #ifdef CONFIG_PM
  103. /*
  104. * If @startup is true, make sure MLX90614_TIMING_STARTUP ms have elapsed since
  105. * the last wake-up. This is normally only needed to get a valid temperature
  106. * reading. EEPROM access does not need such delay.
  107. * Return 0 on success, <0 on error.
  108. */
  109. static int mlx90614_power_get(struct mlx90614_data *data, bool startup)
  110. {
  111. unsigned long now;
  112. if (!data->wakeup_gpio)
  113. return 0;
  114. pm_runtime_get_sync(&data->client->dev);
  115. if (startup) {
  116. now = jiffies;
  117. if (time_before(now, data->ready_timestamp) &&
  118. msleep_interruptible(jiffies_to_msecs(
  119. data->ready_timestamp - now)) != 0) {
  120. pm_runtime_put_autosuspend(&data->client->dev);
  121. return -EINTR;
  122. }
  123. }
  124. return 0;
  125. }
  126. static void mlx90614_power_put(struct mlx90614_data *data)
  127. {
  128. if (!data->wakeup_gpio)
  129. return;
  130. pm_runtime_mark_last_busy(&data->client->dev);
  131. pm_runtime_put_autosuspend(&data->client->dev);
  132. }
  133. #else
  134. static inline int mlx90614_power_get(struct mlx90614_data *data, bool startup)
  135. {
  136. return 0;
  137. }
  138. static inline void mlx90614_power_put(struct mlx90614_data *data)
  139. {
  140. }
  141. #endif
  142. static int mlx90614_read_raw(struct iio_dev *indio_dev,
  143. struct iio_chan_spec const *channel, int *val,
  144. int *val2, long mask)
  145. {
  146. struct mlx90614_data *data = iio_priv(indio_dev);
  147. u8 cmd;
  148. s32 ret;
  149. switch (mask) {
  150. case IIO_CHAN_INFO_RAW: /* 0.02K / LSB */
  151. switch (channel->channel2) {
  152. case IIO_MOD_TEMP_AMBIENT:
  153. cmd = MLX90614_TA;
  154. break;
  155. case IIO_MOD_TEMP_OBJECT:
  156. switch (channel->channel) {
  157. case 0:
  158. cmd = MLX90614_TOBJ1;
  159. break;
  160. case 1:
  161. cmd = MLX90614_TOBJ2;
  162. break;
  163. default:
  164. return -EINVAL;
  165. }
  166. break;
  167. default:
  168. return -EINVAL;
  169. }
  170. ret = mlx90614_power_get(data, true);
  171. if (ret < 0)
  172. return ret;
  173. ret = i2c_smbus_read_word_data(data->client, cmd);
  174. mlx90614_power_put(data);
  175. if (ret < 0)
  176. return ret;
  177. /* MSB is an error flag */
  178. if (ret & 0x8000)
  179. return -EIO;
  180. *val = ret;
  181. return IIO_VAL_INT;
  182. case IIO_CHAN_INFO_OFFSET:
  183. *val = MLX90614_CONST_OFFSET_DEC;
  184. *val2 = MLX90614_CONST_OFFSET_REM;
  185. return IIO_VAL_INT_PLUS_MICRO;
  186. case IIO_CHAN_INFO_SCALE:
  187. *val = MLX90614_CONST_SCALE;
  188. return IIO_VAL_INT;
  189. case IIO_CHAN_INFO_CALIBEMISSIVITY: /* 1/65535 / LSB */
  190. mlx90614_power_get(data, false);
  191. mutex_lock(&data->lock);
  192. ret = i2c_smbus_read_word_data(data->client,
  193. MLX90614_EMISSIVITY);
  194. mutex_unlock(&data->lock);
  195. mlx90614_power_put(data);
  196. if (ret < 0)
  197. return ret;
  198. if (ret == MLX90614_CONST_RAW_EMISSIVITY_MAX) {
  199. *val = 1;
  200. *val2 = 0;
  201. } else {
  202. *val = 0;
  203. *val2 = ret * MLX90614_CONST_EMISSIVITY_RESOLUTION;
  204. }
  205. return IIO_VAL_INT_PLUS_NANO;
  206. default:
  207. return -EINVAL;
  208. }
  209. }
  210. static int mlx90614_write_raw(struct iio_dev *indio_dev,
  211. struct iio_chan_spec const *channel, int val,
  212. int val2, long mask)
  213. {
  214. struct mlx90614_data *data = iio_priv(indio_dev);
  215. s32 ret;
  216. switch (mask) {
  217. case IIO_CHAN_INFO_CALIBEMISSIVITY: /* 1/65535 / LSB */
  218. if (val < 0 || val2 < 0 || val > 1 || (val == 1 && val2 != 0))
  219. return -EINVAL;
  220. val = val * MLX90614_CONST_RAW_EMISSIVITY_MAX +
  221. val2 / MLX90614_CONST_EMISSIVITY_RESOLUTION;
  222. mlx90614_power_get(data, false);
  223. mutex_lock(&data->lock);
  224. ret = mlx90614_write_word(data->client, MLX90614_EMISSIVITY,
  225. val);
  226. mutex_unlock(&data->lock);
  227. mlx90614_power_put(data);
  228. return ret;
  229. default:
  230. return -EINVAL;
  231. }
  232. }
  233. static int mlx90614_write_raw_get_fmt(struct iio_dev *indio_dev,
  234. struct iio_chan_spec const *channel,
  235. long mask)
  236. {
  237. switch (mask) {
  238. case IIO_CHAN_INFO_CALIBEMISSIVITY:
  239. return IIO_VAL_INT_PLUS_NANO;
  240. default:
  241. return -EINVAL;
  242. }
  243. }
  244. static const struct iio_chan_spec mlx90614_channels[] = {
  245. {
  246. .type = IIO_TEMP,
  247. .modified = 1,
  248. .channel2 = IIO_MOD_TEMP_AMBIENT,
  249. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  250. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
  251. BIT(IIO_CHAN_INFO_SCALE),
  252. },
  253. {
  254. .type = IIO_TEMP,
  255. .modified = 1,
  256. .channel2 = IIO_MOD_TEMP_OBJECT,
  257. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  258. BIT(IIO_CHAN_INFO_CALIBEMISSIVITY),
  259. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
  260. BIT(IIO_CHAN_INFO_SCALE),
  261. },
  262. {
  263. .type = IIO_TEMP,
  264. .indexed = 1,
  265. .modified = 1,
  266. .channel = 1,
  267. .channel2 = IIO_MOD_TEMP_OBJECT,
  268. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  269. BIT(IIO_CHAN_INFO_CALIBEMISSIVITY),
  270. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
  271. BIT(IIO_CHAN_INFO_SCALE),
  272. },
  273. };
  274. static const struct iio_info mlx90614_info = {
  275. .read_raw = mlx90614_read_raw,
  276. .write_raw = mlx90614_write_raw,
  277. .write_raw_get_fmt = mlx90614_write_raw_get_fmt,
  278. .driver_module = THIS_MODULE,
  279. };
  280. #ifdef CONFIG_PM
  281. static int mlx90614_sleep(struct mlx90614_data *data)
  282. {
  283. s32 ret;
  284. if (!data->wakeup_gpio) {
  285. dev_dbg(&data->client->dev, "Sleep disabled");
  286. return -ENOSYS;
  287. }
  288. dev_dbg(&data->client->dev, "Requesting sleep");
  289. mutex_lock(&data->lock);
  290. ret = i2c_smbus_xfer(data->client->adapter, data->client->addr,
  291. data->client->flags | I2C_CLIENT_PEC,
  292. I2C_SMBUS_WRITE, MLX90614_OP_SLEEP,
  293. I2C_SMBUS_BYTE, NULL);
  294. mutex_unlock(&data->lock);
  295. return ret;
  296. }
  297. static int mlx90614_wakeup(struct mlx90614_data *data)
  298. {
  299. if (!data->wakeup_gpio) {
  300. dev_dbg(&data->client->dev, "Wake-up disabled");
  301. return -ENOSYS;
  302. }
  303. dev_dbg(&data->client->dev, "Requesting wake-up");
  304. i2c_lock_adapter(data->client->adapter);
  305. gpiod_direction_output(data->wakeup_gpio, 0);
  306. msleep(MLX90614_TIMING_WAKEUP);
  307. gpiod_direction_input(data->wakeup_gpio);
  308. i2c_unlock_adapter(data->client->adapter);
  309. data->ready_timestamp = jiffies +
  310. msecs_to_jiffies(MLX90614_TIMING_STARTUP);
  311. /*
  312. * Quirk: the i2c controller may get confused right after the
  313. * wake-up signal has been sent. As a workaround, do a dummy read.
  314. * If the read fails, the controller will probably be reset so that
  315. * further reads will work.
  316. */
  317. i2c_smbus_read_word_data(data->client, MLX90614_CONFIG);
  318. return 0;
  319. }
  320. /* Return wake-up GPIO or NULL if sleep functionality should be disabled. */
  321. static struct gpio_desc *mlx90614_probe_wakeup(struct i2c_client *client)
  322. {
  323. struct gpio_desc *gpio;
  324. if (!i2c_check_functionality(client->adapter,
  325. I2C_FUNC_SMBUS_WRITE_BYTE)) {
  326. dev_info(&client->dev,
  327. "i2c adapter does not support SMBUS_WRITE_BYTE, sleep disabled");
  328. return NULL;
  329. }
  330. gpio = devm_gpiod_get_optional(&client->dev, "wakeup", GPIOD_IN);
  331. if (IS_ERR(gpio)) {
  332. dev_warn(&client->dev,
  333. "gpio acquisition failed with error %ld, sleep disabled",
  334. PTR_ERR(gpio));
  335. return NULL;
  336. } else if (!gpio) {
  337. dev_info(&client->dev,
  338. "wakeup-gpio not found, sleep disabled");
  339. }
  340. return gpio;
  341. }
  342. #else
  343. static inline int mlx90614_sleep(struct mlx90614_data *data)
  344. {
  345. return -ENOSYS;
  346. }
  347. static inline int mlx90614_wakeup(struct mlx90614_data *data)
  348. {
  349. return -ENOSYS;
  350. }
  351. static inline struct gpio_desc *mlx90614_probe_wakeup(struct i2c_client *client)
  352. {
  353. return NULL;
  354. }
  355. #endif
  356. /* Return 0 for single sensor, 1 for dual sensor, <0 on error. */
  357. static int mlx90614_probe_num_ir_sensors(struct i2c_client *client)
  358. {
  359. s32 ret;
  360. ret = i2c_smbus_read_word_data(client, MLX90614_CONFIG);
  361. if (ret < 0)
  362. return ret;
  363. return (ret & MLX90614_CONFIG_DUAL_MASK) ? 1 : 0;
  364. }
  365. static int mlx90614_probe(struct i2c_client *client,
  366. const struct i2c_device_id *id)
  367. {
  368. struct iio_dev *indio_dev;
  369. struct mlx90614_data *data;
  370. int ret;
  371. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WORD_DATA))
  372. return -ENODEV;
  373. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  374. if (!indio_dev)
  375. return -ENOMEM;
  376. data = iio_priv(indio_dev);
  377. i2c_set_clientdata(client, indio_dev);
  378. data->client = client;
  379. mutex_init(&data->lock);
  380. data->wakeup_gpio = mlx90614_probe_wakeup(client);
  381. mlx90614_wakeup(data);
  382. indio_dev->dev.parent = &client->dev;
  383. indio_dev->name = id->name;
  384. indio_dev->modes = INDIO_DIRECT_MODE;
  385. indio_dev->info = &mlx90614_info;
  386. ret = mlx90614_probe_num_ir_sensors(client);
  387. switch (ret) {
  388. case 0:
  389. dev_dbg(&client->dev, "Found single sensor");
  390. indio_dev->channels = mlx90614_channels;
  391. indio_dev->num_channels = 2;
  392. break;
  393. case 1:
  394. dev_dbg(&client->dev, "Found dual sensor");
  395. indio_dev->channels = mlx90614_channels;
  396. indio_dev->num_channels = 3;
  397. break;
  398. default:
  399. return ret;
  400. }
  401. if (data->wakeup_gpio) {
  402. pm_runtime_set_autosuspend_delay(&client->dev,
  403. MLX90614_AUTOSLEEP_DELAY);
  404. pm_runtime_use_autosuspend(&client->dev);
  405. pm_runtime_set_active(&client->dev);
  406. pm_runtime_enable(&client->dev);
  407. }
  408. return iio_device_register(indio_dev);
  409. }
  410. static int mlx90614_remove(struct i2c_client *client)
  411. {
  412. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  413. struct mlx90614_data *data = iio_priv(indio_dev);
  414. iio_device_unregister(indio_dev);
  415. if (data->wakeup_gpio) {
  416. pm_runtime_disable(&client->dev);
  417. if (!pm_runtime_status_suspended(&client->dev))
  418. mlx90614_sleep(data);
  419. pm_runtime_set_suspended(&client->dev);
  420. }
  421. return 0;
  422. }
  423. static const struct i2c_device_id mlx90614_id[] = {
  424. { "mlx90614", 0 },
  425. { }
  426. };
  427. MODULE_DEVICE_TABLE(i2c, mlx90614_id);
  428. #ifdef CONFIG_PM_SLEEP
  429. static int mlx90614_pm_suspend(struct device *dev)
  430. {
  431. struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
  432. struct mlx90614_data *data = iio_priv(indio_dev);
  433. if (data->wakeup_gpio && pm_runtime_active(dev))
  434. return mlx90614_sleep(data);
  435. return 0;
  436. }
  437. static int mlx90614_pm_resume(struct device *dev)
  438. {
  439. struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
  440. struct mlx90614_data *data = iio_priv(indio_dev);
  441. int err;
  442. if (data->wakeup_gpio) {
  443. err = mlx90614_wakeup(data);
  444. if (err < 0)
  445. return err;
  446. pm_runtime_disable(dev);
  447. pm_runtime_set_active(dev);
  448. pm_runtime_enable(dev);
  449. }
  450. return 0;
  451. }
  452. #endif
  453. #ifdef CONFIG_PM
  454. static int mlx90614_pm_runtime_suspend(struct device *dev)
  455. {
  456. struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
  457. struct mlx90614_data *data = iio_priv(indio_dev);
  458. return mlx90614_sleep(data);
  459. }
  460. static int mlx90614_pm_runtime_resume(struct device *dev)
  461. {
  462. struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
  463. struct mlx90614_data *data = iio_priv(indio_dev);
  464. return mlx90614_wakeup(data);
  465. }
  466. #endif
  467. static const struct dev_pm_ops mlx90614_pm_ops = {
  468. SET_SYSTEM_SLEEP_PM_OPS(mlx90614_pm_suspend, mlx90614_pm_resume)
  469. SET_RUNTIME_PM_OPS(mlx90614_pm_runtime_suspend,
  470. mlx90614_pm_runtime_resume, NULL)
  471. };
  472. static struct i2c_driver mlx90614_driver = {
  473. .driver = {
  474. .name = "mlx90614",
  475. .pm = &mlx90614_pm_ops,
  476. },
  477. .probe = mlx90614_probe,
  478. .remove = mlx90614_remove,
  479. .id_table = mlx90614_id,
  480. };
  481. module_i2c_driver(mlx90614_driver);
  482. MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
  483. MODULE_AUTHOR("Vianney le Clément de Saint-Marcq <vianney.leclement@essensium.com>");
  484. MODULE_DESCRIPTION("Melexis MLX90614 contactless IR temperature sensor driver");
  485. MODULE_LICENSE("GPL");