mlx90632.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * mlx90632.c - Melexis MLX90632 contactless IR temperature sensor
  4. *
  5. * Copyright (c) 2017 Melexis <cmo@melexis.com>
  6. *
  7. * Driver for the Melexis MLX90632 I2C 16-bit IR thermopile sensor
  8. */
  9. #include <linux/delay.h>
  10. #include <linux/err.h>
  11. #include <linux/gpio/consumer.h>
  12. #include <linux/i2c.h>
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/math64.h>
  16. #include <linux/of.h>
  17. #include <linux/pm_runtime.h>
  18. #include <linux/regmap.h>
  19. #include <linux/iio/iio.h>
  20. #include <linux/iio/sysfs.h>
  21. /* Memory sections addresses */
  22. #define MLX90632_ADDR_RAM 0x4000 /* Start address of ram */
  23. #define MLX90632_ADDR_EEPROM 0x2480 /* Start address of user eeprom */
  24. /* EEPROM addresses - used at startup */
  25. #define MLX90632_EE_CTRL 0x24d4 /* Control register initial value */
  26. #define MLX90632_EE_I2C_ADDR 0x24d5 /* I2C address register initial value */
  27. #define MLX90632_EE_VERSION 0x240b /* EEPROM version reg address */
  28. #define MLX90632_EE_P_R 0x240c /* P_R calibration register 32bit */
  29. #define MLX90632_EE_P_G 0x240e /* P_G calibration register 32bit */
  30. #define MLX90632_EE_P_T 0x2410 /* P_T calibration register 32bit */
  31. #define MLX90632_EE_P_O 0x2412 /* P_O calibration register 32bit */
  32. #define MLX90632_EE_Aa 0x2414 /* Aa calibration register 32bit */
  33. #define MLX90632_EE_Ab 0x2416 /* Ab calibration register 32bit */
  34. #define MLX90632_EE_Ba 0x2418 /* Ba calibration register 32bit */
  35. #define MLX90632_EE_Bb 0x241a /* Bb calibration register 32bit */
  36. #define MLX90632_EE_Ca 0x241c /* Ca calibration register 32bit */
  37. #define MLX90632_EE_Cb 0x241e /* Cb calibration register 32bit */
  38. #define MLX90632_EE_Da 0x2420 /* Da calibration register 32bit */
  39. #define MLX90632_EE_Db 0x2422 /* Db calibration register 32bit */
  40. #define MLX90632_EE_Ea 0x2424 /* Ea calibration register 32bit */
  41. #define MLX90632_EE_Eb 0x2426 /* Eb calibration register 32bit */
  42. #define MLX90632_EE_Fa 0x2428 /* Fa calibration register 32bit */
  43. #define MLX90632_EE_Fb 0x242a /* Fb calibration register 32bit */
  44. #define MLX90632_EE_Ga 0x242c /* Ga calibration register 32bit */
  45. #define MLX90632_EE_Gb 0x242e /* Gb calibration register 16bit */
  46. #define MLX90632_EE_Ka 0x242f /* Ka calibration register 16bit */
  47. #define MLX90632_EE_Ha 0x2481 /* Ha customer calib value reg 16bit */
  48. #define MLX90632_EE_Hb 0x2482 /* Hb customer calib value reg 16bit */
  49. /* Register addresses - volatile */
  50. #define MLX90632_REG_I2C_ADDR 0x3000 /* Chip I2C address register */
  51. /* Control register address - volatile */
  52. #define MLX90632_REG_CONTROL 0x3001 /* Control Register address */
  53. #define MLX90632_CFG_PWR_MASK GENMASK(2, 1) /* PowerMode Mask */
  54. /* PowerModes statuses */
  55. #define MLX90632_PWR_STATUS(ctrl_val) (ctrl_val << 1)
  56. #define MLX90632_PWR_STATUS_HALT MLX90632_PWR_STATUS(0) /* hold */
  57. #define MLX90632_PWR_STATUS_SLEEP_STEP MLX90632_PWR_STATUS(1) /* sleep step*/
  58. #define MLX90632_PWR_STATUS_STEP MLX90632_PWR_STATUS(2) /* step */
  59. #define MLX90632_PWR_STATUS_CONTINUOUS MLX90632_PWR_STATUS(3) /* continuous*/
  60. /* Device status register - volatile */
  61. #define MLX90632_REG_STATUS 0x3fff /* Device status register */
  62. #define MLX90632_STAT_BUSY BIT(10) /* Device busy indicator */
  63. #define MLX90632_STAT_EE_BUSY BIT(9) /* EEPROM busy indicator */
  64. #define MLX90632_STAT_BRST BIT(8) /* Brown out reset indicator */
  65. #define MLX90632_STAT_CYCLE_POS GENMASK(6, 2) /* Data position */
  66. #define MLX90632_STAT_DATA_RDY BIT(0) /* Data ready indicator */
  67. /* RAM_MEAS address-es for each channel */
  68. #define MLX90632_RAM_1(meas_num) (MLX90632_ADDR_RAM + 3 * meas_num)
  69. #define MLX90632_RAM_2(meas_num) (MLX90632_ADDR_RAM + 3 * meas_num + 1)
  70. #define MLX90632_RAM_3(meas_num) (MLX90632_ADDR_RAM + 3 * meas_num + 2)
  71. /* Magic constants */
  72. #define MLX90632_ID_MEDICAL 0x0105 /* EEPROM DSPv5 Medical device id */
  73. #define MLX90632_ID_CONSUMER 0x0205 /* EEPROM DSPv5 Consumer device id */
  74. #define MLX90632_DSP_VERSION 5 /* DSP version */
  75. #define MLX90632_DSP_MASK GENMASK(7, 0) /* DSP version in EE_VERSION */
  76. #define MLX90632_RESET_CMD 0x0006 /* Reset sensor (address or global) */
  77. #define MLX90632_REF_12 12LL /**< ResCtrlRef value of Ch 1 or Ch 2 */
  78. #define MLX90632_REF_3 12LL /**< ResCtrlRef value of Channel 3 */
  79. #define MLX90632_MAX_MEAS_NUM 31 /**< Maximum measurements in list */
  80. #define MLX90632_SLEEP_DELAY_MS 3000 /**< Autosleep delay */
  81. struct mlx90632_data {
  82. struct i2c_client *client;
  83. struct mutex lock; /* Multiple reads for single measurement */
  84. struct regmap *regmap;
  85. u16 emissivity;
  86. };
  87. static const struct regmap_range mlx90632_volatile_reg_range[] = {
  88. regmap_reg_range(MLX90632_REG_I2C_ADDR, MLX90632_REG_CONTROL),
  89. regmap_reg_range(MLX90632_REG_STATUS, MLX90632_REG_STATUS),
  90. regmap_reg_range(MLX90632_RAM_1(0),
  91. MLX90632_RAM_3(MLX90632_MAX_MEAS_NUM)),
  92. };
  93. static const struct regmap_access_table mlx90632_volatile_regs_tbl = {
  94. .yes_ranges = mlx90632_volatile_reg_range,
  95. .n_yes_ranges = ARRAY_SIZE(mlx90632_volatile_reg_range),
  96. };
  97. static const struct regmap_range mlx90632_read_reg_range[] = {
  98. regmap_reg_range(MLX90632_EE_VERSION, MLX90632_EE_Ka),
  99. regmap_reg_range(MLX90632_EE_CTRL, MLX90632_EE_I2C_ADDR),
  100. regmap_reg_range(MLX90632_EE_Ha, MLX90632_EE_Hb),
  101. regmap_reg_range(MLX90632_REG_I2C_ADDR, MLX90632_REG_CONTROL),
  102. regmap_reg_range(MLX90632_REG_STATUS, MLX90632_REG_STATUS),
  103. regmap_reg_range(MLX90632_RAM_1(0),
  104. MLX90632_RAM_3(MLX90632_MAX_MEAS_NUM)),
  105. };
  106. static const struct regmap_access_table mlx90632_readable_regs_tbl = {
  107. .yes_ranges = mlx90632_read_reg_range,
  108. .n_yes_ranges = ARRAY_SIZE(mlx90632_read_reg_range),
  109. };
  110. static const struct regmap_range mlx90632_no_write_reg_range[] = {
  111. regmap_reg_range(MLX90632_EE_VERSION, MLX90632_EE_Ka),
  112. regmap_reg_range(MLX90632_RAM_1(0),
  113. MLX90632_RAM_3(MLX90632_MAX_MEAS_NUM)),
  114. };
  115. static const struct regmap_access_table mlx90632_writeable_regs_tbl = {
  116. .no_ranges = mlx90632_no_write_reg_range,
  117. .n_no_ranges = ARRAY_SIZE(mlx90632_no_write_reg_range),
  118. };
  119. static const struct regmap_config mlx90632_regmap = {
  120. .reg_bits = 16,
  121. .val_bits = 16,
  122. .volatile_table = &mlx90632_volatile_regs_tbl,
  123. .rd_table = &mlx90632_readable_regs_tbl,
  124. .wr_table = &mlx90632_writeable_regs_tbl,
  125. .use_single_rw = true,
  126. .reg_format_endian = REGMAP_ENDIAN_BIG,
  127. .val_format_endian = REGMAP_ENDIAN_BIG,
  128. .cache_type = REGCACHE_RBTREE,
  129. };
  130. static s32 mlx90632_pwr_set_sleep_step(struct regmap *regmap)
  131. {
  132. return regmap_update_bits(regmap, MLX90632_REG_CONTROL,
  133. MLX90632_CFG_PWR_MASK,
  134. MLX90632_PWR_STATUS_SLEEP_STEP);
  135. }
  136. static s32 mlx90632_pwr_continuous(struct regmap *regmap)
  137. {
  138. return regmap_update_bits(regmap, MLX90632_REG_CONTROL,
  139. MLX90632_CFG_PWR_MASK,
  140. MLX90632_PWR_STATUS_CONTINUOUS);
  141. }
  142. /**
  143. * mlx90632_perform_measurement - Trigger and retrieve current measurement cycle
  144. * @*data: pointer to mlx90632_data object containing regmap information
  145. *
  146. * Perform a measurement and return latest measurement cycle position reported
  147. * by sensor. This is a blocking function for 500ms, as that is default sensor
  148. * refresh rate.
  149. */
  150. static int mlx90632_perform_measurement(struct mlx90632_data *data)
  151. {
  152. int ret, tries = 100;
  153. unsigned int reg_status;
  154. ret = regmap_update_bits(data->regmap, MLX90632_REG_STATUS,
  155. MLX90632_STAT_DATA_RDY, 0);
  156. if (ret < 0)
  157. return ret;
  158. while (tries-- > 0) {
  159. ret = regmap_read(data->regmap, MLX90632_REG_STATUS,
  160. &reg_status);
  161. if (ret < 0)
  162. return ret;
  163. if (reg_status & MLX90632_STAT_DATA_RDY)
  164. break;
  165. usleep_range(10000, 11000);
  166. }
  167. if (tries < 0) {
  168. dev_err(&data->client->dev, "data not ready");
  169. return -ETIMEDOUT;
  170. }
  171. return (reg_status & MLX90632_STAT_CYCLE_POS) >> 2;
  172. }
  173. static int mlx90632_channel_new_select(int perform_ret, uint8_t *channel_new,
  174. uint8_t *channel_old)
  175. {
  176. switch (perform_ret) {
  177. case 1:
  178. *channel_new = 1;
  179. *channel_old = 2;
  180. break;
  181. case 2:
  182. *channel_new = 2;
  183. *channel_old = 1;
  184. break;
  185. default:
  186. return -EINVAL;
  187. }
  188. return 0;
  189. }
  190. static int mlx90632_read_ambient_raw(struct regmap *regmap,
  191. s16 *ambient_new_raw, s16 *ambient_old_raw)
  192. {
  193. int ret;
  194. unsigned int read_tmp;
  195. ret = regmap_read(regmap, MLX90632_RAM_3(1), &read_tmp);
  196. if (ret < 0)
  197. return ret;
  198. *ambient_new_raw = (s16)read_tmp;
  199. ret = regmap_read(regmap, MLX90632_RAM_3(2), &read_tmp);
  200. if (ret < 0)
  201. return ret;
  202. *ambient_old_raw = (s16)read_tmp;
  203. return ret;
  204. }
  205. static int mlx90632_read_object_raw(struct regmap *regmap,
  206. int perform_measurement_ret,
  207. s16 *object_new_raw, s16 *object_old_raw)
  208. {
  209. int ret;
  210. unsigned int read_tmp;
  211. s16 read;
  212. u8 channel = 0;
  213. u8 channel_old = 0;
  214. ret = mlx90632_channel_new_select(perform_measurement_ret, &channel,
  215. &channel_old);
  216. if (ret != 0)
  217. return ret;
  218. ret = regmap_read(regmap, MLX90632_RAM_2(channel), &read_tmp);
  219. if (ret < 0)
  220. return ret;
  221. read = (s16)read_tmp;
  222. ret = regmap_read(regmap, MLX90632_RAM_1(channel), &read_tmp);
  223. if (ret < 0)
  224. return ret;
  225. *object_new_raw = (read + (s16)read_tmp) / 2;
  226. ret = regmap_read(regmap, MLX90632_RAM_2(channel_old), &read_tmp);
  227. if (ret < 0)
  228. return ret;
  229. read = (s16)read_tmp;
  230. ret = regmap_read(regmap, MLX90632_RAM_1(channel_old), &read_tmp);
  231. if (ret < 0)
  232. return ret;
  233. *object_old_raw = (read + (s16)read_tmp) / 2;
  234. return ret;
  235. }
  236. static int mlx90632_read_all_channel(struct mlx90632_data *data,
  237. s16 *ambient_new_raw, s16 *ambient_old_raw,
  238. s16 *object_new_raw, s16 *object_old_raw)
  239. {
  240. s32 ret, measurement;
  241. mutex_lock(&data->lock);
  242. measurement = mlx90632_perform_measurement(data);
  243. if (measurement < 0) {
  244. ret = measurement;
  245. goto read_unlock;
  246. }
  247. ret = mlx90632_read_ambient_raw(data->regmap, ambient_new_raw,
  248. ambient_old_raw);
  249. if (ret < 0)
  250. goto read_unlock;
  251. ret = mlx90632_read_object_raw(data->regmap, measurement,
  252. object_new_raw, object_old_raw);
  253. read_unlock:
  254. mutex_unlock(&data->lock);
  255. return ret;
  256. }
  257. static int mlx90632_read_ee_register(struct regmap *regmap, u16 reg_lsb,
  258. s32 *reg_value)
  259. {
  260. s32 ret;
  261. unsigned int read;
  262. u32 value;
  263. ret = regmap_read(regmap, reg_lsb, &read);
  264. if (ret < 0)
  265. return ret;
  266. value = read;
  267. ret = regmap_read(regmap, reg_lsb + 1, &read);
  268. if (ret < 0)
  269. return ret;
  270. *reg_value = (read << 16) | (value & 0xffff);
  271. return 0;
  272. }
  273. static s64 mlx90632_preprocess_temp_amb(s16 ambient_new_raw,
  274. s16 ambient_old_raw, s16 Gb)
  275. {
  276. s64 VR_Ta, kGb, tmp;
  277. kGb = ((s64)Gb * 1000LL) >> 10ULL;
  278. VR_Ta = (s64)ambient_old_raw * 1000000LL +
  279. kGb * div64_s64(((s64)ambient_new_raw * 1000LL),
  280. (MLX90632_REF_3));
  281. tmp = div64_s64(
  282. div64_s64(((s64)ambient_new_raw * 1000000000000LL),
  283. (MLX90632_REF_3)), VR_Ta);
  284. return div64_s64(tmp << 19ULL, 1000LL);
  285. }
  286. static s64 mlx90632_preprocess_temp_obj(s16 object_new_raw, s16 object_old_raw,
  287. s16 ambient_new_raw,
  288. s16 ambient_old_raw, s16 Ka)
  289. {
  290. s64 VR_IR, kKa, tmp;
  291. kKa = ((s64)Ka * 1000LL) >> 10ULL;
  292. VR_IR = (s64)ambient_old_raw * 1000000LL +
  293. kKa * div64_s64(((s64)ambient_new_raw * 1000LL),
  294. (MLX90632_REF_3));
  295. tmp = div64_s64(
  296. div64_s64(((s64)((object_new_raw + object_old_raw) / 2)
  297. * 1000000000000LL), (MLX90632_REF_12)),
  298. VR_IR);
  299. return div64_s64((tmp << 19ULL), 1000LL);
  300. }
  301. static s32 mlx90632_calc_temp_ambient(s16 ambient_new_raw, s16 ambient_old_raw,
  302. s32 P_T, s32 P_R, s32 P_G, s32 P_O,
  303. s16 Gb)
  304. {
  305. s64 Asub, Bsub, Ablock, Bblock, Cblock, AMB, sum;
  306. AMB = mlx90632_preprocess_temp_amb(ambient_new_raw, ambient_old_raw,
  307. Gb);
  308. Asub = ((s64)P_T * 10000000000LL) >> 44ULL;
  309. Bsub = AMB - (((s64)P_R * 1000LL) >> 8ULL);
  310. Ablock = Asub * (Bsub * Bsub);
  311. Bblock = (div64_s64(Bsub * 10000000LL, P_G)) << 20ULL;
  312. Cblock = ((s64)P_O * 10000000000LL) >> 8ULL;
  313. sum = div64_s64(Ablock, 1000000LL) + Bblock + Cblock;
  314. return div64_s64(sum, 10000000LL);
  315. }
  316. static s32 mlx90632_calc_temp_object_iteration(s32 prev_object_temp, s64 object,
  317. s64 TAdut, s32 Fa, s32 Fb,
  318. s32 Ga, s16 Ha, s16 Hb,
  319. u16 emissivity)
  320. {
  321. s64 calcedKsTO, calcedKsTA, ir_Alpha, TAdut4, Alpha_corr;
  322. s64 Ha_customer, Hb_customer;
  323. Ha_customer = ((s64)Ha * 1000000LL) >> 14ULL;
  324. Hb_customer = ((s64)Hb * 100) >> 10ULL;
  325. calcedKsTO = ((s64)((s64)Ga * (prev_object_temp - 25 * 1000LL)
  326. * 1000LL)) >> 36LL;
  327. calcedKsTA = ((s64)(Fb * (TAdut - 25 * 1000000LL))) >> 36LL;
  328. Alpha_corr = div64_s64((((s64)(Fa * 10000000000LL) >> 46LL)
  329. * Ha_customer), 1000LL);
  330. Alpha_corr *= ((s64)(1 * 1000000LL + calcedKsTO + calcedKsTA));
  331. Alpha_corr = emissivity * div64_s64(Alpha_corr, 100000LL);
  332. Alpha_corr = div64_s64(Alpha_corr, 1000LL);
  333. ir_Alpha = div64_s64((s64)object * 10000000LL, Alpha_corr);
  334. TAdut4 = (div64_s64(TAdut, 10000LL) + 27315) *
  335. (div64_s64(TAdut, 10000LL) + 27315) *
  336. (div64_s64(TAdut, 10000LL) + 27315) *
  337. (div64_s64(TAdut, 10000LL) + 27315);
  338. return (int_sqrt64(int_sqrt64(ir_Alpha * 1000000000000LL + TAdut4))
  339. - 27315 - Hb_customer) * 10;
  340. }
  341. static s32 mlx90632_calc_temp_object(s64 object, s64 ambient, s32 Ea, s32 Eb,
  342. s32 Fa, s32 Fb, s32 Ga, s16 Ha, s16 Hb,
  343. u16 tmp_emi)
  344. {
  345. s64 kTA, kTA0, TAdut;
  346. s64 temp = 25000;
  347. s8 i;
  348. kTA = (Ea * 1000LL) >> 16LL;
  349. kTA0 = (Eb * 1000LL) >> 8LL;
  350. TAdut = div64_s64(((ambient - kTA0) * 1000000LL), kTA) + 25 * 1000000LL;
  351. /* Iterations of calculation as described in datasheet */
  352. for (i = 0; i < 5; ++i) {
  353. temp = mlx90632_calc_temp_object_iteration(temp, object, TAdut,
  354. Fa, Fb, Ga, Ha, Hb,
  355. tmp_emi);
  356. }
  357. return temp;
  358. }
  359. static int mlx90632_calc_object_dsp105(struct mlx90632_data *data, int *val)
  360. {
  361. s32 ret;
  362. s32 Ea, Eb, Fa, Fb, Ga;
  363. unsigned int read_tmp;
  364. s16 Ha, Hb, Gb, Ka;
  365. s16 ambient_new_raw, ambient_old_raw, object_new_raw, object_old_raw;
  366. s64 object, ambient;
  367. ret = mlx90632_read_ee_register(data->regmap, MLX90632_EE_Ea, &Ea);
  368. if (ret < 0)
  369. return ret;
  370. ret = mlx90632_read_ee_register(data->regmap, MLX90632_EE_Eb, &Eb);
  371. if (ret < 0)
  372. return ret;
  373. ret = mlx90632_read_ee_register(data->regmap, MLX90632_EE_Fa, &Fa);
  374. if (ret < 0)
  375. return ret;
  376. ret = mlx90632_read_ee_register(data->regmap, MLX90632_EE_Fb, &Fb);
  377. if (ret < 0)
  378. return ret;
  379. ret = mlx90632_read_ee_register(data->regmap, MLX90632_EE_Ga, &Ga);
  380. if (ret < 0)
  381. return ret;
  382. ret = regmap_read(data->regmap, MLX90632_EE_Ha, &read_tmp);
  383. if (ret < 0)
  384. return ret;
  385. Ha = (s16)read_tmp;
  386. ret = regmap_read(data->regmap, MLX90632_EE_Hb, &read_tmp);
  387. if (ret < 0)
  388. return ret;
  389. Hb = (s16)read_tmp;
  390. ret = regmap_read(data->regmap, MLX90632_EE_Gb, &read_tmp);
  391. if (ret < 0)
  392. return ret;
  393. Gb = (s16)read_tmp;
  394. ret = regmap_read(data->regmap, MLX90632_EE_Ka, &read_tmp);
  395. if (ret < 0)
  396. return ret;
  397. Ka = (s16)read_tmp;
  398. ret = mlx90632_read_all_channel(data,
  399. &ambient_new_raw, &ambient_old_raw,
  400. &object_new_raw, &object_old_raw);
  401. if (ret < 0)
  402. return ret;
  403. ambient = mlx90632_preprocess_temp_amb(ambient_new_raw,
  404. ambient_old_raw, Gb);
  405. object = mlx90632_preprocess_temp_obj(object_new_raw,
  406. object_old_raw,
  407. ambient_new_raw,
  408. ambient_old_raw, Ka);
  409. *val = mlx90632_calc_temp_object(object, ambient, Ea, Eb, Fa, Fb, Ga,
  410. Ha, Hb, data->emissivity);
  411. return 0;
  412. }
  413. static int mlx90632_calc_ambient_dsp105(struct mlx90632_data *data, int *val)
  414. {
  415. s32 ret;
  416. unsigned int read_tmp;
  417. s32 PT, PR, PG, PO;
  418. s16 Gb;
  419. s16 ambient_new_raw, ambient_old_raw;
  420. ret = mlx90632_read_ee_register(data->regmap, MLX90632_EE_P_R, &PR);
  421. if (ret < 0)
  422. return ret;
  423. ret = mlx90632_read_ee_register(data->regmap, MLX90632_EE_P_G, &PG);
  424. if (ret < 0)
  425. return ret;
  426. ret = mlx90632_read_ee_register(data->regmap, MLX90632_EE_P_T, &PT);
  427. if (ret < 0)
  428. return ret;
  429. ret = mlx90632_read_ee_register(data->regmap, MLX90632_EE_P_O, &PO);
  430. if (ret < 0)
  431. return ret;
  432. ret = regmap_read(data->regmap, MLX90632_EE_Gb, &read_tmp);
  433. if (ret < 0)
  434. return ret;
  435. Gb = (s16)read_tmp;
  436. ret = mlx90632_read_ambient_raw(data->regmap, &ambient_new_raw,
  437. &ambient_old_raw);
  438. if (ret < 0)
  439. return ret;
  440. *val = mlx90632_calc_temp_ambient(ambient_new_raw, ambient_old_raw,
  441. PT, PR, PG, PO, Gb);
  442. return ret;
  443. }
  444. static int mlx90632_read_raw(struct iio_dev *indio_dev,
  445. struct iio_chan_spec const *channel, int *val,
  446. int *val2, long mask)
  447. {
  448. struct mlx90632_data *data = iio_priv(indio_dev);
  449. int ret;
  450. switch (mask) {
  451. case IIO_CHAN_INFO_PROCESSED:
  452. switch (channel->channel2) {
  453. case IIO_MOD_TEMP_AMBIENT:
  454. ret = mlx90632_calc_ambient_dsp105(data, val);
  455. if (ret < 0)
  456. return ret;
  457. return IIO_VAL_INT;
  458. case IIO_MOD_TEMP_OBJECT:
  459. ret = mlx90632_calc_object_dsp105(data, val);
  460. if (ret < 0)
  461. return ret;
  462. return IIO_VAL_INT;
  463. default:
  464. return -EINVAL;
  465. }
  466. case IIO_CHAN_INFO_CALIBEMISSIVITY:
  467. if (data->emissivity == 1000) {
  468. *val = 1;
  469. *val2 = 0;
  470. } else {
  471. *val = 0;
  472. *val2 = data->emissivity * 1000;
  473. }
  474. return IIO_VAL_INT_PLUS_MICRO;
  475. default:
  476. return -EINVAL;
  477. }
  478. }
  479. static int mlx90632_write_raw(struct iio_dev *indio_dev,
  480. struct iio_chan_spec const *channel, int val,
  481. int val2, long mask)
  482. {
  483. struct mlx90632_data *data = iio_priv(indio_dev);
  484. switch (mask) {
  485. case IIO_CHAN_INFO_CALIBEMISSIVITY:
  486. /* Confirm we are within 0 and 1.0 */
  487. if (val < 0 || val2 < 0 || val > 1 ||
  488. (val == 1 && val2 != 0))
  489. return -EINVAL;
  490. data->emissivity = val * 1000 + val2 / 1000;
  491. return 0;
  492. default:
  493. return -EINVAL;
  494. }
  495. }
  496. static const struct iio_chan_spec mlx90632_channels[] = {
  497. {
  498. .type = IIO_TEMP,
  499. .modified = 1,
  500. .channel2 = IIO_MOD_TEMP_AMBIENT,
  501. .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
  502. },
  503. {
  504. .type = IIO_TEMP,
  505. .modified = 1,
  506. .channel2 = IIO_MOD_TEMP_OBJECT,
  507. .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
  508. BIT(IIO_CHAN_INFO_CALIBEMISSIVITY),
  509. },
  510. };
  511. static const struct iio_info mlx90632_info = {
  512. .read_raw = mlx90632_read_raw,
  513. .write_raw = mlx90632_write_raw,
  514. };
  515. static int mlx90632_sleep(struct mlx90632_data *data)
  516. {
  517. regcache_mark_dirty(data->regmap);
  518. dev_dbg(&data->client->dev, "Requesting sleep");
  519. return mlx90632_pwr_set_sleep_step(data->regmap);
  520. }
  521. static int mlx90632_wakeup(struct mlx90632_data *data)
  522. {
  523. int ret;
  524. ret = regcache_sync(data->regmap);
  525. if (ret < 0) {
  526. dev_err(&data->client->dev,
  527. "Failed to sync regmap registers: %d\n", ret);
  528. return ret;
  529. }
  530. dev_dbg(&data->client->dev, "Requesting wake-up\n");
  531. return mlx90632_pwr_continuous(data->regmap);
  532. }
  533. static int mlx90632_probe(struct i2c_client *client,
  534. const struct i2c_device_id *id)
  535. {
  536. struct iio_dev *indio_dev;
  537. struct mlx90632_data *mlx90632;
  538. struct regmap *regmap;
  539. int ret;
  540. unsigned int read;
  541. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*mlx90632));
  542. if (!indio_dev) {
  543. dev_err(&client->dev, "Failed to allocate device\n");
  544. return -ENOMEM;
  545. }
  546. regmap = devm_regmap_init_i2c(client, &mlx90632_regmap);
  547. if (IS_ERR(regmap)) {
  548. ret = PTR_ERR(regmap);
  549. dev_err(&client->dev, "Failed to allocate regmap: %d\n", ret);
  550. return ret;
  551. }
  552. mlx90632 = iio_priv(indio_dev);
  553. i2c_set_clientdata(client, indio_dev);
  554. mlx90632->client = client;
  555. mlx90632->regmap = regmap;
  556. mutex_init(&mlx90632->lock);
  557. indio_dev->dev.parent = &client->dev;
  558. indio_dev->name = id->name;
  559. indio_dev->modes = INDIO_DIRECT_MODE;
  560. indio_dev->info = &mlx90632_info;
  561. indio_dev->channels = mlx90632_channels;
  562. indio_dev->num_channels = ARRAY_SIZE(mlx90632_channels);
  563. ret = mlx90632_wakeup(mlx90632);
  564. if (ret < 0) {
  565. dev_err(&client->dev, "Wakeup failed: %d\n", ret);
  566. return ret;
  567. }
  568. ret = regmap_read(mlx90632->regmap, MLX90632_EE_VERSION, &read);
  569. if (ret < 0) {
  570. dev_err(&client->dev, "read of version failed: %d\n", ret);
  571. return ret;
  572. }
  573. if (read == MLX90632_ID_MEDICAL) {
  574. dev_dbg(&client->dev,
  575. "Detected Medical EEPROM calibration %x\n", read);
  576. } else if (read == MLX90632_ID_CONSUMER) {
  577. dev_dbg(&client->dev,
  578. "Detected Consumer EEPROM calibration %x\n", read);
  579. } else if ((read & MLX90632_DSP_MASK) == MLX90632_DSP_VERSION) {
  580. dev_dbg(&client->dev,
  581. "Detected Unknown EEPROM calibration %x\n", read);
  582. } else {
  583. dev_err(&client->dev,
  584. "Wrong DSP version %x (expected %x)\n",
  585. read, MLX90632_DSP_VERSION);
  586. return -EPROTONOSUPPORT;
  587. }
  588. mlx90632->emissivity = 1000;
  589. pm_runtime_disable(&client->dev);
  590. ret = pm_runtime_set_active(&client->dev);
  591. if (ret < 0) {
  592. mlx90632_sleep(mlx90632);
  593. return ret;
  594. }
  595. pm_runtime_enable(&client->dev);
  596. pm_runtime_set_autosuspend_delay(&client->dev, MLX90632_SLEEP_DELAY_MS);
  597. pm_runtime_use_autosuspend(&client->dev);
  598. return iio_device_register(indio_dev);
  599. }
  600. static int mlx90632_remove(struct i2c_client *client)
  601. {
  602. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  603. struct mlx90632_data *data = iio_priv(indio_dev);
  604. iio_device_unregister(indio_dev);
  605. pm_runtime_disable(&client->dev);
  606. pm_runtime_set_suspended(&client->dev);
  607. pm_runtime_put_noidle(&client->dev);
  608. mlx90632_sleep(data);
  609. return 0;
  610. }
  611. static const struct i2c_device_id mlx90632_id[] = {
  612. { "mlx90632", 0 },
  613. { }
  614. };
  615. MODULE_DEVICE_TABLE(i2c, mlx90632_id);
  616. static const struct of_device_id mlx90632_of_match[] = {
  617. { .compatible = "melexis,mlx90632" },
  618. { }
  619. };
  620. MODULE_DEVICE_TABLE(of, mlx90632_of_match);
  621. static int __maybe_unused mlx90632_pm_suspend(struct device *dev)
  622. {
  623. struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
  624. struct mlx90632_data *data = iio_priv(indio_dev);
  625. return mlx90632_sleep(data);
  626. }
  627. static int __maybe_unused mlx90632_pm_resume(struct device *dev)
  628. {
  629. struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
  630. struct mlx90632_data *data = iio_priv(indio_dev);
  631. return mlx90632_wakeup(data);
  632. }
  633. static UNIVERSAL_DEV_PM_OPS(mlx90632_pm_ops, mlx90632_pm_suspend,
  634. mlx90632_pm_resume, NULL);
  635. static struct i2c_driver mlx90632_driver = {
  636. .driver = {
  637. .name = "mlx90632",
  638. .of_match_table = mlx90632_of_match,
  639. .pm = &mlx90632_pm_ops,
  640. },
  641. .probe = mlx90632_probe,
  642. .remove = mlx90632_remove,
  643. .id_table = mlx90632_id,
  644. };
  645. module_i2c_driver(mlx90632_driver);
  646. MODULE_AUTHOR("Crt Mori <cmo@melexis.com>");
  647. MODULE_DESCRIPTION("Melexis MLX90632 contactless Infra Red temperature sensor driver");
  648. MODULE_LICENSE("GPL v2");