mlx90614.c 15 KB

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