hdc3020.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * hdc3020.c - Support for the TI HDC3020,HDC3021 and HDC3022
  4. * temperature + relative humidity sensors
  5. *
  6. * Copyright (C) 2023
  7. *
  8. * Copyright (C) 2024 Liebherr-Electronics and Drives GmbH
  9. *
  10. * Datasheet: https://www.ti.com/lit/ds/symlink/hdc3020.pdf
  11. */
  12. #include <linux/bitfield.h>
  13. #include <linux/bitops.h>
  14. #include <linux/cleanup.h>
  15. #include <linux/crc8.h>
  16. #include <linux/delay.h>
  17. #include <linux/gpio/consumer.h>
  18. #include <linux/i2c.h>
  19. #include <linux/init.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/math64.h>
  22. #include <linux/module.h>
  23. #include <linux/mutex.h>
  24. #include <linux/pm.h>
  25. #include <linux/regulator/consumer.h>
  26. #include <linux/units.h>
  27. #include <linux/unaligned.h>
  28. #include <linux/iio/events.h>
  29. #include <linux/iio/iio.h>
  30. #define HDC3020_S_AUTO_10HZ_MOD0 0x2737
  31. #define HDC3020_S_STATUS 0x3041
  32. #define HDC3020_HEATER_DISABLE 0x3066
  33. #define HDC3020_HEATER_ENABLE 0x306D
  34. #define HDC3020_HEATER_CONFIG 0x306E
  35. #define HDC3020_EXIT_AUTO 0x3093
  36. #define HDC3020_S_T_RH_THRESH_LOW 0x6100
  37. #define HDC3020_S_T_RH_THRESH_LOW_CLR 0x610B
  38. #define HDC3020_S_T_RH_THRESH_HIGH_CLR 0x6116
  39. #define HDC3020_S_T_RH_THRESH_HIGH 0x611D
  40. #define HDC3020_R_T_RH_AUTO 0xE000
  41. #define HDC3020_R_T_LOW_AUTO 0xE002
  42. #define HDC3020_R_T_HIGH_AUTO 0xE003
  43. #define HDC3020_R_RH_LOW_AUTO 0xE004
  44. #define HDC3020_R_RH_HIGH_AUTO 0xE005
  45. #define HDC3020_R_T_RH_THRESH_LOW 0xE102
  46. #define HDC3020_R_T_RH_THRESH_LOW_CLR 0xE109
  47. #define HDC3020_R_T_RH_THRESH_HIGH_CLR 0xE114
  48. #define HDC3020_R_T_RH_THRESH_HIGH 0xE11F
  49. #define HDC3020_R_STATUS 0xF32D
  50. #define HDC3020_THRESH_TEMP_MASK GENMASK(8, 0)
  51. #define HDC3020_THRESH_TEMP_TRUNC_SHIFT 7
  52. #define HDC3020_THRESH_HUM_MASK GENMASK(15, 9)
  53. #define HDC3020_THRESH_HUM_TRUNC_SHIFT 9
  54. #define HDC3020_STATUS_T_LOW_ALERT BIT(6)
  55. #define HDC3020_STATUS_T_HIGH_ALERT BIT(7)
  56. #define HDC3020_STATUS_RH_LOW_ALERT BIT(8)
  57. #define HDC3020_STATUS_RH_HIGH_ALERT BIT(9)
  58. #define HDC3020_READ_RETRY_TIMES 10
  59. #define HDC3020_BUSY_DELAY_MS 10
  60. #define HDC3020_CRC8_POLYNOMIAL 0x31
  61. #define HDC3020_MIN_TEMP_MICRO -39872968
  62. #define HDC3020_MAX_TEMP_MICRO 124875639
  63. #define HDC3020_MAX_TEMP_HYST_MICRO 164748607
  64. #define HDC3020_MAX_HUM_MICRO 99220264
  65. struct hdc3020_data {
  66. struct i2c_client *client;
  67. struct gpio_desc *reset_gpio;
  68. struct regulator *vdd_supply;
  69. /*
  70. * Ensure that the sensor configuration (currently only heater is
  71. * supported) will not be changed during the process of reading
  72. * sensor data (this driver will try HDC3020_READ_RETRY_TIMES times
  73. * if the device does not respond).
  74. */
  75. struct mutex lock;
  76. };
  77. static const int hdc3020_heater_vals[] = {0, 1, 0x3FFF};
  78. static const struct iio_event_spec hdc3020_t_rh_event[] = {
  79. {
  80. .type = IIO_EV_TYPE_THRESH,
  81. .dir = IIO_EV_DIR_RISING,
  82. .mask_separate = BIT(IIO_EV_INFO_VALUE) |
  83. BIT(IIO_EV_INFO_HYSTERESIS),
  84. },
  85. {
  86. .type = IIO_EV_TYPE_THRESH,
  87. .dir = IIO_EV_DIR_FALLING,
  88. .mask_separate = BIT(IIO_EV_INFO_VALUE) |
  89. BIT(IIO_EV_INFO_HYSTERESIS),
  90. },
  91. };
  92. static const struct iio_chan_spec hdc3020_channels[] = {
  93. {
  94. .type = IIO_TEMP,
  95. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  96. BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_PEAK) |
  97. BIT(IIO_CHAN_INFO_TROUGH) | BIT(IIO_CHAN_INFO_OFFSET),
  98. .event_spec = hdc3020_t_rh_event,
  99. .num_event_specs = ARRAY_SIZE(hdc3020_t_rh_event),
  100. },
  101. {
  102. .type = IIO_HUMIDITYRELATIVE,
  103. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  104. BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_PEAK) |
  105. BIT(IIO_CHAN_INFO_TROUGH),
  106. .event_spec = hdc3020_t_rh_event,
  107. .num_event_specs = ARRAY_SIZE(hdc3020_t_rh_event),
  108. },
  109. {
  110. /*
  111. * For setting the internal heater, which can be switched on to
  112. * prevent or remove any condensation that may develop when the
  113. * ambient environment approaches its dew point temperature.
  114. */
  115. .type = IIO_CURRENT,
  116. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  117. .info_mask_separate_available = BIT(IIO_CHAN_INFO_RAW),
  118. .output = 1,
  119. },
  120. };
  121. DECLARE_CRC8_TABLE(hdc3020_crc8_table);
  122. static int hdc3020_write_bytes(struct hdc3020_data *data, u8 *buf, u8 len)
  123. {
  124. struct i2c_client *client = data->client;
  125. struct i2c_msg msg;
  126. int ret, cnt;
  127. msg.addr = client->addr;
  128. msg.flags = 0;
  129. msg.buf = buf;
  130. msg.len = len;
  131. /*
  132. * During the measurement process, HDC3020 will not return data.
  133. * So wait for a while and try again
  134. */
  135. for (cnt = 0; cnt < HDC3020_READ_RETRY_TIMES; cnt++) {
  136. ret = i2c_transfer(client->adapter, &msg, 1);
  137. if (ret == 1)
  138. return 0;
  139. mdelay(HDC3020_BUSY_DELAY_MS);
  140. }
  141. dev_err(&client->dev, "Could not write sensor command\n");
  142. return -ETIMEDOUT;
  143. }
  144. static
  145. int hdc3020_read_bytes(struct hdc3020_data *data, u16 reg, u8 *buf, int len)
  146. {
  147. u8 reg_buf[2];
  148. int ret, cnt;
  149. struct i2c_client *client = data->client;
  150. struct i2c_msg msg[2] = {
  151. [0] = {
  152. .addr = client->addr,
  153. .flags = 0,
  154. .buf = reg_buf,
  155. .len = 2,
  156. },
  157. [1] = {
  158. .addr = client->addr,
  159. .flags = I2C_M_RD,
  160. .buf = buf,
  161. .len = len,
  162. },
  163. };
  164. put_unaligned_be16(reg, reg_buf);
  165. /*
  166. * During the measurement process, HDC3020 will not return data.
  167. * So wait for a while and try again
  168. */
  169. for (cnt = 0; cnt < HDC3020_READ_RETRY_TIMES; cnt++) {
  170. ret = i2c_transfer(client->adapter, msg, 2);
  171. if (ret == 2)
  172. return 0;
  173. mdelay(HDC3020_BUSY_DELAY_MS);
  174. }
  175. dev_err(&client->dev, "Could not read sensor data\n");
  176. return -ETIMEDOUT;
  177. }
  178. static int hdc3020_read_be16(struct hdc3020_data *data, u16 reg)
  179. {
  180. u8 crc, buf[3];
  181. int ret;
  182. ret = hdc3020_read_bytes(data, reg, buf, 3);
  183. if (ret < 0)
  184. return ret;
  185. crc = crc8(hdc3020_crc8_table, buf, 2, CRC8_INIT_VALUE);
  186. if (crc != buf[2])
  187. return -EINVAL;
  188. return get_unaligned_be16(buf);
  189. }
  190. static int hdc3020_exec_cmd(struct hdc3020_data *data, u16 reg)
  191. {
  192. u8 reg_buf[2];
  193. put_unaligned_be16(reg, reg_buf);
  194. return hdc3020_write_bytes(data, reg_buf, 2);
  195. }
  196. static int hdc3020_read_measurement(struct hdc3020_data *data,
  197. enum iio_chan_type type, int *val)
  198. {
  199. u8 crc, buf[6];
  200. int ret;
  201. ret = hdc3020_read_bytes(data, HDC3020_R_T_RH_AUTO, buf, 6);
  202. if (ret < 0)
  203. return ret;
  204. /* CRC check of the temperature measurement */
  205. crc = crc8(hdc3020_crc8_table, buf, 2, CRC8_INIT_VALUE);
  206. if (crc != buf[2])
  207. return -EINVAL;
  208. /* CRC check of the relative humidity measurement */
  209. crc = crc8(hdc3020_crc8_table, buf + 3, 2, CRC8_INIT_VALUE);
  210. if (crc != buf[5])
  211. return -EINVAL;
  212. if (type == IIO_TEMP)
  213. *val = get_unaligned_be16(buf);
  214. else if (type == IIO_HUMIDITYRELATIVE)
  215. *val = get_unaligned_be16(&buf[3]);
  216. else
  217. return -EINVAL;
  218. return 0;
  219. }
  220. static int hdc3020_read_raw(struct iio_dev *indio_dev,
  221. struct iio_chan_spec const *chan, int *val,
  222. int *val2, long mask)
  223. {
  224. struct hdc3020_data *data = iio_priv(indio_dev);
  225. int ret;
  226. if (chan->type != IIO_TEMP && chan->type != IIO_HUMIDITYRELATIVE)
  227. return -EINVAL;
  228. switch (mask) {
  229. case IIO_CHAN_INFO_RAW: {
  230. guard(mutex)(&data->lock);
  231. ret = hdc3020_read_measurement(data, chan->type, val);
  232. if (ret < 0)
  233. return ret;
  234. return IIO_VAL_INT;
  235. }
  236. case IIO_CHAN_INFO_PEAK: {
  237. guard(mutex)(&data->lock);
  238. if (chan->type == IIO_TEMP)
  239. ret = hdc3020_read_be16(data, HDC3020_R_T_HIGH_AUTO);
  240. else
  241. ret = hdc3020_read_be16(data, HDC3020_R_RH_HIGH_AUTO);
  242. if (ret < 0)
  243. return ret;
  244. *val = ret;
  245. return IIO_VAL_INT;
  246. }
  247. case IIO_CHAN_INFO_TROUGH: {
  248. guard(mutex)(&data->lock);
  249. if (chan->type == IIO_TEMP)
  250. ret = hdc3020_read_be16(data, HDC3020_R_T_LOW_AUTO);
  251. else
  252. ret = hdc3020_read_be16(data, HDC3020_R_RH_LOW_AUTO);
  253. if (ret < 0)
  254. return ret;
  255. *val = ret;
  256. return IIO_VAL_INT;
  257. }
  258. case IIO_CHAN_INFO_SCALE:
  259. *val2 = 65536;
  260. if (chan->type == IIO_TEMP)
  261. *val = 175;
  262. else
  263. *val = 100;
  264. return IIO_VAL_FRACTIONAL;
  265. case IIO_CHAN_INFO_OFFSET:
  266. if (chan->type != IIO_TEMP)
  267. return -EINVAL;
  268. *val = -16852;
  269. return IIO_VAL_INT;
  270. default:
  271. return -EINVAL;
  272. }
  273. }
  274. static int hdc3020_read_available(struct iio_dev *indio_dev,
  275. struct iio_chan_spec const *chan,
  276. const int **vals,
  277. int *type, int *length, long mask)
  278. {
  279. if (mask != IIO_CHAN_INFO_RAW || chan->type != IIO_CURRENT)
  280. return -EINVAL;
  281. *vals = hdc3020_heater_vals;
  282. *type = IIO_VAL_INT;
  283. return IIO_AVAIL_RANGE;
  284. }
  285. static int hdc3020_update_heater(struct hdc3020_data *data, int val)
  286. {
  287. u8 buf[5];
  288. int ret;
  289. if (val < hdc3020_heater_vals[0] || val > hdc3020_heater_vals[2])
  290. return -EINVAL;
  291. if (!val)
  292. hdc3020_exec_cmd(data, HDC3020_HEATER_DISABLE);
  293. put_unaligned_be16(HDC3020_HEATER_CONFIG, buf);
  294. put_unaligned_be16(val & GENMASK(13, 0), &buf[2]);
  295. buf[4] = crc8(hdc3020_crc8_table, buf + 2, 2, CRC8_INIT_VALUE);
  296. ret = hdc3020_write_bytes(data, buf, 5);
  297. if (ret < 0)
  298. return ret;
  299. return hdc3020_exec_cmd(data, HDC3020_HEATER_ENABLE);
  300. }
  301. static int hdc3020_write_raw(struct iio_dev *indio_dev,
  302. struct iio_chan_spec const *chan,
  303. int val, int val2, long mask)
  304. {
  305. struct hdc3020_data *data = iio_priv(indio_dev);
  306. switch (mask) {
  307. case IIO_CHAN_INFO_RAW:
  308. if (chan->type != IIO_CURRENT)
  309. return -EINVAL;
  310. guard(mutex)(&data->lock);
  311. return hdc3020_update_heater(data, val);
  312. }
  313. return -EINVAL;
  314. }
  315. static int hdc3020_thresh_get_temp(u16 thresh)
  316. {
  317. int temp;
  318. /*
  319. * Get the temperature threshold from 9 LSBs, shift them to get
  320. * the truncated temperature threshold representation and
  321. * calculate the threshold according to the formula in the
  322. * datasheet. Result is degree celsius scaled by 65535.
  323. */
  324. temp = FIELD_GET(HDC3020_THRESH_TEMP_MASK, thresh) <<
  325. HDC3020_THRESH_TEMP_TRUNC_SHIFT;
  326. return -2949075 + (175 * temp);
  327. }
  328. static int hdc3020_thresh_get_hum(u16 thresh)
  329. {
  330. int hum;
  331. /*
  332. * Get the humidity threshold from 7 MSBs, shift them to get the
  333. * truncated humidity threshold representation and calculate the
  334. * threshold according to the formula in the datasheet. Result is
  335. * percent scaled by 65535.
  336. */
  337. hum = FIELD_GET(HDC3020_THRESH_HUM_MASK, thresh) <<
  338. HDC3020_THRESH_HUM_TRUNC_SHIFT;
  339. return hum * 100;
  340. }
  341. static u16 hdc3020_thresh_set_temp(int s_temp, u16 curr_thresh)
  342. {
  343. u64 temp;
  344. u16 thresh;
  345. /*
  346. * Calculate temperature threshold, shift it down to get the
  347. * truncated threshold representation in the 9LSBs while keeping
  348. * the current humidity threshold in the 7 MSBs.
  349. */
  350. temp = (u64)(s_temp + 45000000) * 65535ULL;
  351. temp = div_u64(temp, 1000000 * 175) >> HDC3020_THRESH_TEMP_TRUNC_SHIFT;
  352. thresh = FIELD_PREP(HDC3020_THRESH_TEMP_MASK, temp);
  353. thresh |= (FIELD_GET(HDC3020_THRESH_HUM_MASK, curr_thresh) <<
  354. HDC3020_THRESH_HUM_TRUNC_SHIFT);
  355. return thresh;
  356. }
  357. static u16 hdc3020_thresh_set_hum(int s_hum, u16 curr_thresh)
  358. {
  359. u64 hum;
  360. u16 thresh;
  361. /*
  362. * Calculate humidity threshold, shift it down and up to get the
  363. * truncated threshold representation in the 7MSBs while keeping
  364. * the current temperature threshold in the 9 LSBs.
  365. */
  366. hum = (u64)(s_hum) * 65535ULL;
  367. hum = div_u64(hum, 1000000 * 100) >> HDC3020_THRESH_HUM_TRUNC_SHIFT;
  368. thresh = FIELD_PREP(HDC3020_THRESH_HUM_MASK, hum);
  369. thresh |= FIELD_GET(HDC3020_THRESH_TEMP_MASK, curr_thresh);
  370. return thresh;
  371. }
  372. static
  373. int hdc3020_thresh_clr(s64 s_thresh, s64 s_hyst, enum iio_event_direction dir)
  374. {
  375. s64 s_clr;
  376. /*
  377. * Include directions when calculation the clear value,
  378. * since hysteresis is unsigned by definition and the
  379. * clear value is an absolute value which is signed.
  380. */
  381. if (dir == IIO_EV_DIR_RISING)
  382. s_clr = s_thresh - s_hyst;
  383. else
  384. s_clr = s_thresh + s_hyst;
  385. /* Divide by 65535 to get units of micro */
  386. return div_s64(s_clr, 65535);
  387. }
  388. static int _hdc3020_write_thresh(struct hdc3020_data *data, u16 reg, u16 val)
  389. {
  390. u8 buf[5];
  391. put_unaligned_be16(reg, buf);
  392. put_unaligned_be16(val, buf + 2);
  393. buf[4] = crc8(hdc3020_crc8_table, buf + 2, 2, CRC8_INIT_VALUE);
  394. return hdc3020_write_bytes(data, buf, 5);
  395. }
  396. static int hdc3020_write_thresh(struct iio_dev *indio_dev,
  397. const struct iio_chan_spec *chan,
  398. enum iio_event_type type,
  399. enum iio_event_direction dir,
  400. enum iio_event_info info,
  401. int val, int val2)
  402. {
  403. struct hdc3020_data *data = iio_priv(indio_dev);
  404. u16 reg, reg_val, reg_thresh_rd, reg_clr_rd, reg_thresh_wr, reg_clr_wr;
  405. s64 s_thresh, s_hyst, s_clr;
  406. int s_val, thresh, clr, ret;
  407. /* Select threshold registers */
  408. if (dir == IIO_EV_DIR_RISING) {
  409. reg_thresh_rd = HDC3020_R_T_RH_THRESH_HIGH;
  410. reg_thresh_wr = HDC3020_S_T_RH_THRESH_HIGH;
  411. reg_clr_rd = HDC3020_R_T_RH_THRESH_HIGH_CLR;
  412. reg_clr_wr = HDC3020_S_T_RH_THRESH_HIGH_CLR;
  413. } else {
  414. reg_thresh_rd = HDC3020_R_T_RH_THRESH_LOW;
  415. reg_thresh_wr = HDC3020_S_T_RH_THRESH_LOW;
  416. reg_clr_rd = HDC3020_R_T_RH_THRESH_LOW_CLR;
  417. reg_clr_wr = HDC3020_S_T_RH_THRESH_LOW_CLR;
  418. }
  419. guard(mutex)(&data->lock);
  420. ret = hdc3020_read_be16(data, reg_thresh_rd);
  421. if (ret < 0)
  422. return ret;
  423. thresh = ret;
  424. ret = hdc3020_read_be16(data, reg_clr_rd);
  425. if (ret < 0)
  426. return ret;
  427. clr = ret;
  428. /* Scale value to include decimal part into calculations */
  429. s_val = (val < 0) ? (val * 1000000 - val2) : (val * 1000000 + val2);
  430. switch (chan->type) {
  431. case IIO_TEMP:
  432. switch (info) {
  433. case IIO_EV_INFO_VALUE:
  434. s_val = max(s_val, HDC3020_MIN_TEMP_MICRO);
  435. s_val = min(s_val, HDC3020_MAX_TEMP_MICRO);
  436. reg = reg_thresh_wr;
  437. reg_val = hdc3020_thresh_set_temp(s_val, thresh);
  438. ret = _hdc3020_write_thresh(data, reg, reg_val);
  439. if (ret < 0)
  440. return ret;
  441. /* Calculate old hysteresis */
  442. s_thresh = (s64)hdc3020_thresh_get_temp(thresh) * 1000000;
  443. s_clr = (s64)hdc3020_thresh_get_temp(clr) * 1000000;
  444. s_hyst = div_s64(abs(s_thresh - s_clr), 65535);
  445. /* Set new threshold */
  446. thresh = reg_val;
  447. /* Set old hysteresis */
  448. s_val = s_hyst;
  449. fallthrough;
  450. case IIO_EV_INFO_HYSTERESIS:
  451. /*
  452. * Function hdc3020_thresh_get_temp returns temperature
  453. * in degree celsius scaled by 65535. Scale by 1000000
  454. * to be able to subtract scaled hysteresis value.
  455. */
  456. s_thresh = (s64)hdc3020_thresh_get_temp(thresh) * 1000000;
  457. /*
  458. * Units of s_val are in micro degree celsius, scale by
  459. * 65535 to get same units as s_thresh.
  460. */
  461. s_val = min(abs(s_val), HDC3020_MAX_TEMP_HYST_MICRO);
  462. s_hyst = (s64)s_val * 65535;
  463. s_clr = hdc3020_thresh_clr(s_thresh, s_hyst, dir);
  464. s_clr = max(s_clr, HDC3020_MIN_TEMP_MICRO);
  465. s_clr = min(s_clr, HDC3020_MAX_TEMP_MICRO);
  466. reg = reg_clr_wr;
  467. reg_val = hdc3020_thresh_set_temp(s_clr, clr);
  468. break;
  469. default:
  470. return -EOPNOTSUPP;
  471. }
  472. break;
  473. case IIO_HUMIDITYRELATIVE:
  474. s_val = (s_val < 0) ? 0 : min(s_val, HDC3020_MAX_HUM_MICRO);
  475. switch (info) {
  476. case IIO_EV_INFO_VALUE:
  477. reg = reg_thresh_wr;
  478. reg_val = hdc3020_thresh_set_hum(s_val, thresh);
  479. ret = _hdc3020_write_thresh(data, reg, reg_val);
  480. if (ret < 0)
  481. return ret;
  482. /* Calculate old hysteresis */
  483. s_thresh = (s64)hdc3020_thresh_get_hum(thresh) * 1000000;
  484. s_clr = (s64)hdc3020_thresh_get_hum(clr) * 1000000;
  485. s_hyst = div_s64(abs(s_thresh - s_clr), 65535);
  486. /* Set new threshold */
  487. thresh = reg_val;
  488. /* Try to set old hysteresis */
  489. s_val = min(abs(s_hyst), HDC3020_MAX_HUM_MICRO);
  490. fallthrough;
  491. case IIO_EV_INFO_HYSTERESIS:
  492. /*
  493. * Function hdc3020_thresh_get_hum returns relative
  494. * humidity in percent scaled by 65535. Scale by 1000000
  495. * to be able to subtract scaled hysteresis value.
  496. */
  497. s_thresh = (s64)hdc3020_thresh_get_hum(thresh) * 1000000;
  498. /*
  499. * Units of s_val are in micro percent, scale by 65535
  500. * to get same units as s_thresh.
  501. */
  502. s_hyst = (s64)s_val * 65535;
  503. s_clr = hdc3020_thresh_clr(s_thresh, s_hyst, dir);
  504. s_clr = max(s_clr, 0);
  505. s_clr = min(s_clr, HDC3020_MAX_HUM_MICRO);
  506. reg = reg_clr_wr;
  507. reg_val = hdc3020_thresh_set_hum(s_clr, clr);
  508. break;
  509. default:
  510. return -EOPNOTSUPP;
  511. }
  512. break;
  513. default:
  514. return -EOPNOTSUPP;
  515. }
  516. return _hdc3020_write_thresh(data, reg, reg_val);
  517. }
  518. static int hdc3020_read_thresh(struct iio_dev *indio_dev,
  519. const struct iio_chan_spec *chan,
  520. enum iio_event_type type,
  521. enum iio_event_direction dir,
  522. enum iio_event_info info,
  523. int *val, int *val2)
  524. {
  525. struct hdc3020_data *data = iio_priv(indio_dev);
  526. u16 reg_thresh, reg_clr;
  527. int thresh, clr, ret;
  528. /* Select threshold registers */
  529. if (dir == IIO_EV_DIR_RISING) {
  530. reg_thresh = HDC3020_R_T_RH_THRESH_HIGH;
  531. reg_clr = HDC3020_R_T_RH_THRESH_HIGH_CLR;
  532. } else {
  533. reg_thresh = HDC3020_R_T_RH_THRESH_LOW;
  534. reg_clr = HDC3020_R_T_RH_THRESH_LOW_CLR;
  535. }
  536. guard(mutex)(&data->lock);
  537. ret = hdc3020_read_be16(data, reg_thresh);
  538. if (ret < 0)
  539. return ret;
  540. switch (chan->type) {
  541. case IIO_TEMP:
  542. thresh = hdc3020_thresh_get_temp(ret);
  543. switch (info) {
  544. case IIO_EV_INFO_VALUE:
  545. *val = thresh;
  546. break;
  547. case IIO_EV_INFO_HYSTERESIS:
  548. ret = hdc3020_read_be16(data, reg_clr);
  549. if (ret < 0)
  550. return ret;
  551. clr = hdc3020_thresh_get_temp(ret);
  552. *val = abs(thresh - clr);
  553. break;
  554. default:
  555. return -EOPNOTSUPP;
  556. }
  557. *val2 = 65535;
  558. return IIO_VAL_FRACTIONAL;
  559. case IIO_HUMIDITYRELATIVE:
  560. thresh = hdc3020_thresh_get_hum(ret);
  561. switch (info) {
  562. case IIO_EV_INFO_VALUE:
  563. *val = thresh;
  564. break;
  565. case IIO_EV_INFO_HYSTERESIS:
  566. ret = hdc3020_read_be16(data, reg_clr);
  567. if (ret < 0)
  568. return ret;
  569. clr = hdc3020_thresh_get_hum(ret);
  570. *val = abs(thresh - clr);
  571. break;
  572. default:
  573. return -EOPNOTSUPP;
  574. }
  575. *val2 = 65535;
  576. return IIO_VAL_FRACTIONAL;
  577. default:
  578. return -EOPNOTSUPP;
  579. }
  580. }
  581. static irqreturn_t hdc3020_interrupt_handler(int irq, void *private)
  582. {
  583. struct iio_dev *indio_dev = private;
  584. struct hdc3020_data *data;
  585. s64 time;
  586. int ret;
  587. data = iio_priv(indio_dev);
  588. ret = hdc3020_read_be16(data, HDC3020_R_STATUS);
  589. if (ret < 0)
  590. return IRQ_HANDLED;
  591. if (!(ret & (HDC3020_STATUS_T_HIGH_ALERT | HDC3020_STATUS_T_LOW_ALERT |
  592. HDC3020_STATUS_RH_HIGH_ALERT | HDC3020_STATUS_RH_LOW_ALERT)))
  593. return IRQ_NONE;
  594. time = iio_get_time_ns(indio_dev);
  595. if (ret & HDC3020_STATUS_T_HIGH_ALERT)
  596. iio_push_event(indio_dev,
  597. IIO_MOD_EVENT_CODE(IIO_TEMP, 0,
  598. IIO_NO_MOD,
  599. IIO_EV_TYPE_THRESH,
  600. IIO_EV_DIR_RISING),
  601. time);
  602. if (ret & HDC3020_STATUS_T_LOW_ALERT)
  603. iio_push_event(indio_dev,
  604. IIO_MOD_EVENT_CODE(IIO_TEMP, 0,
  605. IIO_NO_MOD,
  606. IIO_EV_TYPE_THRESH,
  607. IIO_EV_DIR_FALLING),
  608. time);
  609. if (ret & HDC3020_STATUS_RH_HIGH_ALERT)
  610. iio_push_event(indio_dev,
  611. IIO_MOD_EVENT_CODE(IIO_HUMIDITYRELATIVE, 0,
  612. IIO_NO_MOD,
  613. IIO_EV_TYPE_THRESH,
  614. IIO_EV_DIR_RISING),
  615. time);
  616. if (ret & HDC3020_STATUS_RH_LOW_ALERT)
  617. iio_push_event(indio_dev,
  618. IIO_MOD_EVENT_CODE(IIO_HUMIDITYRELATIVE, 0,
  619. IIO_NO_MOD,
  620. IIO_EV_TYPE_THRESH,
  621. IIO_EV_DIR_FALLING),
  622. time);
  623. return IRQ_HANDLED;
  624. }
  625. static const struct iio_info hdc3020_info = {
  626. .read_raw = hdc3020_read_raw,
  627. .write_raw = hdc3020_write_raw,
  628. .read_avail = hdc3020_read_available,
  629. .read_event_value = hdc3020_read_thresh,
  630. .write_event_value = hdc3020_write_thresh,
  631. };
  632. static int hdc3020_power_off(struct hdc3020_data *data)
  633. {
  634. hdc3020_exec_cmd(data, HDC3020_EXIT_AUTO);
  635. if (data->reset_gpio)
  636. gpiod_set_value_cansleep(data->reset_gpio, 1);
  637. return regulator_disable(data->vdd_supply);
  638. }
  639. static int hdc3020_power_on(struct hdc3020_data *data)
  640. {
  641. int ret;
  642. ret = regulator_enable(data->vdd_supply);
  643. if (ret)
  644. return ret;
  645. fsleep(5000);
  646. if (data->reset_gpio) {
  647. gpiod_set_value_cansleep(data->reset_gpio, 0);
  648. fsleep(3000);
  649. }
  650. if (data->client->irq) {
  651. /*
  652. * The alert output is activated by default upon power up,
  653. * hardware reset, and soft reset. Clear the status register.
  654. */
  655. ret = hdc3020_exec_cmd(data, HDC3020_S_STATUS);
  656. if (ret) {
  657. hdc3020_power_off(data);
  658. return ret;
  659. }
  660. }
  661. ret = hdc3020_exec_cmd(data, HDC3020_S_AUTO_10HZ_MOD0);
  662. if (ret)
  663. hdc3020_power_off(data);
  664. return ret;
  665. }
  666. static void hdc3020_exit(void *data)
  667. {
  668. hdc3020_power_off(data);
  669. }
  670. static int hdc3020_probe(struct i2c_client *client)
  671. {
  672. struct iio_dev *indio_dev;
  673. struct hdc3020_data *data;
  674. int ret;
  675. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
  676. return -EOPNOTSUPP;
  677. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  678. if (!indio_dev)
  679. return -ENOMEM;
  680. dev_set_drvdata(&client->dev, indio_dev);
  681. data = iio_priv(indio_dev);
  682. data->client = client;
  683. mutex_init(&data->lock);
  684. crc8_populate_msb(hdc3020_crc8_table, HDC3020_CRC8_POLYNOMIAL);
  685. indio_dev->name = "hdc3020";
  686. indio_dev->modes = INDIO_DIRECT_MODE;
  687. indio_dev->info = &hdc3020_info;
  688. indio_dev->channels = hdc3020_channels;
  689. indio_dev->num_channels = ARRAY_SIZE(hdc3020_channels);
  690. data->vdd_supply = devm_regulator_get(&client->dev, "vdd");
  691. if (IS_ERR(data->vdd_supply))
  692. return dev_err_probe(&client->dev, PTR_ERR(data->vdd_supply),
  693. "Unable to get VDD regulator\n");
  694. data->reset_gpio = devm_gpiod_get_optional(&client->dev, "reset",
  695. GPIOD_OUT_HIGH);
  696. if (IS_ERR(data->reset_gpio))
  697. return dev_err_probe(&client->dev, PTR_ERR(data->reset_gpio),
  698. "Cannot get reset GPIO\n");
  699. ret = hdc3020_power_on(data);
  700. if (ret)
  701. return dev_err_probe(&client->dev, ret, "Power on failed\n");
  702. ret = devm_add_action_or_reset(&data->client->dev, hdc3020_exit, data);
  703. if (ret)
  704. return ret;
  705. if (client->irq) {
  706. ret = devm_request_threaded_irq(&client->dev, client->irq,
  707. NULL, hdc3020_interrupt_handler,
  708. IRQF_ONESHOT, "hdc3020",
  709. indio_dev);
  710. if (ret)
  711. return dev_err_probe(&client->dev, ret,
  712. "Failed to request IRQ\n");
  713. }
  714. ret = devm_iio_device_register(&data->client->dev, indio_dev);
  715. if (ret)
  716. return dev_err_probe(&client->dev, ret, "Failed to add device");
  717. return 0;
  718. }
  719. static int hdc3020_suspend(struct device *dev)
  720. {
  721. struct iio_dev *iio_dev = dev_get_drvdata(dev);
  722. struct hdc3020_data *data = iio_priv(iio_dev);
  723. return hdc3020_power_off(data);
  724. }
  725. static int hdc3020_resume(struct device *dev)
  726. {
  727. struct iio_dev *iio_dev = dev_get_drvdata(dev);
  728. struct hdc3020_data *data = iio_priv(iio_dev);
  729. return hdc3020_power_on(data);
  730. }
  731. static DEFINE_SIMPLE_DEV_PM_OPS(hdc3020_pm_ops, hdc3020_suspend, hdc3020_resume);
  732. static const struct i2c_device_id hdc3020_id[] = {
  733. { "hdc3020" },
  734. { "hdc3021" },
  735. { "hdc3022" },
  736. { }
  737. };
  738. MODULE_DEVICE_TABLE(i2c, hdc3020_id);
  739. static const struct of_device_id hdc3020_dt_ids[] = {
  740. { .compatible = "ti,hdc3020" },
  741. { .compatible = "ti,hdc3021" },
  742. { .compatible = "ti,hdc3022" },
  743. { }
  744. };
  745. MODULE_DEVICE_TABLE(of, hdc3020_dt_ids);
  746. static struct i2c_driver hdc3020_driver = {
  747. .driver = {
  748. .name = "hdc3020",
  749. .pm = pm_sleep_ptr(&hdc3020_pm_ops),
  750. .of_match_table = hdc3020_dt_ids,
  751. },
  752. .probe = hdc3020_probe,
  753. .id_table = hdc3020_id,
  754. };
  755. module_i2c_driver(hdc3020_driver);
  756. MODULE_AUTHOR("Javier Carrasco <javier.carrasco.cruz@gmail.com>");
  757. MODULE_AUTHOR("Li peiyu <579lpy@gmail.com>");
  758. MODULE_DESCRIPTION("TI HDC3020 humidity and temperature sensor driver");
  759. MODULE_LICENSE("GPL");