mlx90614.c 22 KB

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