hts221_core.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * STMicroelectronics hts221 sensor driver
  4. *
  5. * Copyright 2016 STMicroelectronics Inc.
  6. *
  7. * Lorenzo Bianconi <lorenzo.bianconi@st.com>
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/device.h>
  12. #include <linux/iio/sysfs.h>
  13. #include <linux/delay.h>
  14. #include <linux/pm.h>
  15. #include <linux/regmap.h>
  16. #include <linux/regulator/consumer.h>
  17. #include <linux/bitfield.h>
  18. #include "hts221.h"
  19. #define HTS221_REG_WHOAMI_ADDR 0x0f
  20. #define HTS221_REG_WHOAMI_VAL 0xbc
  21. #define HTS221_REG_CNTRL1_ADDR 0x20
  22. #define HTS221_REG_CNTRL2_ADDR 0x21
  23. #define HTS221_ODR_MASK 0x03
  24. #define HTS221_BDU_MASK BIT(2)
  25. #define HTS221_ENABLE_MASK BIT(7)
  26. /* calibration registers */
  27. #define HTS221_REG_0RH_CAL_X_H 0x36
  28. #define HTS221_REG_1RH_CAL_X_H 0x3a
  29. #define HTS221_REG_0RH_CAL_Y_H 0x30
  30. #define HTS221_REG_1RH_CAL_Y_H 0x31
  31. #define HTS221_REG_0T_CAL_X_L 0x3c
  32. #define HTS221_REG_1T_CAL_X_L 0x3e
  33. #define HTS221_REG_0T_CAL_Y_H 0x32
  34. #define HTS221_REG_1T_CAL_Y_H 0x33
  35. #define HTS221_REG_T1_T0_CAL_Y_H 0x35
  36. struct hts221_odr {
  37. u8 hz;
  38. u8 val;
  39. };
  40. #define HTS221_AVG_DEPTH 8
  41. struct hts221_avg {
  42. u8 addr;
  43. u8 mask;
  44. u16 avg_avl[HTS221_AVG_DEPTH];
  45. };
  46. static const struct hts221_odr hts221_odr_table[] = {
  47. { 1, 0x01 }, /* 1Hz */
  48. { 7, 0x02 }, /* 7Hz */
  49. { 13, 0x03 }, /* 12.5Hz */
  50. };
  51. static const struct hts221_avg hts221_avg_list[] = {
  52. {
  53. .addr = 0x10,
  54. .mask = 0x07,
  55. .avg_avl = {
  56. 4, /* 0.4 %RH */
  57. 8, /* 0.3 %RH */
  58. 16, /* 0.2 %RH */
  59. 32, /* 0.15 %RH */
  60. 64, /* 0.1 %RH */
  61. 128, /* 0.07 %RH */
  62. 256, /* 0.05 %RH */
  63. 512, /* 0.03 %RH */
  64. },
  65. },
  66. {
  67. .addr = 0x10,
  68. .mask = 0x38,
  69. .avg_avl = {
  70. 2, /* 0.08 degC */
  71. 4, /* 0.05 degC */
  72. 8, /* 0.04 degC */
  73. 16, /* 0.03 degC */
  74. 32, /* 0.02 degC */
  75. 64, /* 0.015 degC */
  76. 128, /* 0.01 degC */
  77. 256, /* 0.007 degC */
  78. },
  79. },
  80. };
  81. static const struct iio_chan_spec hts221_channels[] = {
  82. {
  83. .type = IIO_HUMIDITYRELATIVE,
  84. .address = 0x28,
  85. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  86. BIT(IIO_CHAN_INFO_OFFSET) |
  87. BIT(IIO_CHAN_INFO_SCALE) |
  88. BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
  89. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
  90. .scan_index = 0,
  91. .scan_type = {
  92. .sign = 's',
  93. .realbits = 16,
  94. .storagebits = 16,
  95. .endianness = IIO_LE,
  96. },
  97. },
  98. {
  99. .type = IIO_TEMP,
  100. .address = 0x2a,
  101. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  102. BIT(IIO_CHAN_INFO_OFFSET) |
  103. BIT(IIO_CHAN_INFO_SCALE) |
  104. BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
  105. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
  106. .scan_index = 1,
  107. .scan_type = {
  108. .sign = 's',
  109. .realbits = 16,
  110. .storagebits = 16,
  111. .endianness = IIO_LE,
  112. },
  113. },
  114. IIO_CHAN_SOFT_TIMESTAMP(2),
  115. };
  116. static int hts221_check_whoami(struct hts221_hw *hw)
  117. {
  118. int err, data;
  119. err = regmap_read(hw->regmap, HTS221_REG_WHOAMI_ADDR, &data);
  120. if (err < 0) {
  121. dev_err(hw->dev, "failed to read whoami register\n");
  122. return err;
  123. }
  124. if (data != HTS221_REG_WHOAMI_VAL) {
  125. dev_err(hw->dev, "wrong whoami {%02x vs %02x}\n",
  126. data, HTS221_REG_WHOAMI_VAL);
  127. return -ENODEV;
  128. }
  129. return 0;
  130. }
  131. static int hts221_update_odr(struct hts221_hw *hw, u8 odr)
  132. {
  133. int i, err;
  134. for (i = 0; i < ARRAY_SIZE(hts221_odr_table); i++)
  135. if (hts221_odr_table[i].hz == odr)
  136. break;
  137. if (i == ARRAY_SIZE(hts221_odr_table))
  138. return -EINVAL;
  139. err = regmap_update_bits(hw->regmap, HTS221_REG_CNTRL1_ADDR,
  140. HTS221_ODR_MASK,
  141. FIELD_PREP(HTS221_ODR_MASK,
  142. hts221_odr_table[i].val));
  143. if (err < 0)
  144. return err;
  145. hw->odr = odr;
  146. return 0;
  147. }
  148. static int hts221_update_avg(struct hts221_hw *hw,
  149. enum hts221_sensor_type type,
  150. u16 val)
  151. {
  152. const struct hts221_avg *avg = &hts221_avg_list[type];
  153. int i, err, data;
  154. for (i = 0; i < HTS221_AVG_DEPTH; i++)
  155. if (avg->avg_avl[i] == val)
  156. break;
  157. if (i == HTS221_AVG_DEPTH)
  158. return -EINVAL;
  159. data = ((i << __ffs(avg->mask)) & avg->mask);
  160. err = regmap_update_bits(hw->regmap, avg->addr,
  161. avg->mask, data);
  162. if (err < 0)
  163. return err;
  164. hw->sensors[type].cur_avg_idx = i;
  165. return 0;
  166. }
  167. static ssize_t hts221_sysfs_sampling_freq(struct device *dev,
  168. struct device_attribute *attr,
  169. char *buf)
  170. {
  171. int i;
  172. ssize_t len = 0;
  173. for (i = 0; i < ARRAY_SIZE(hts221_odr_table); i++)
  174. len += scnprintf(buf + len, PAGE_SIZE - len, "%d ",
  175. hts221_odr_table[i].hz);
  176. buf[len - 1] = '\n';
  177. return len;
  178. }
  179. static ssize_t
  180. hts221_sysfs_rh_oversampling_avail(struct device *dev,
  181. struct device_attribute *attr,
  182. char *buf)
  183. {
  184. const struct hts221_avg *avg = &hts221_avg_list[HTS221_SENSOR_H];
  185. ssize_t len = 0;
  186. int i;
  187. for (i = 0; i < ARRAY_SIZE(avg->avg_avl); i++)
  188. len += scnprintf(buf + len, PAGE_SIZE - len, "%d ",
  189. avg->avg_avl[i]);
  190. buf[len - 1] = '\n';
  191. return len;
  192. }
  193. static ssize_t
  194. hts221_sysfs_temp_oversampling_avail(struct device *dev,
  195. struct device_attribute *attr,
  196. char *buf)
  197. {
  198. const struct hts221_avg *avg = &hts221_avg_list[HTS221_SENSOR_T];
  199. ssize_t len = 0;
  200. int i;
  201. for (i = 0; i < ARRAY_SIZE(avg->avg_avl); i++)
  202. len += scnprintf(buf + len, PAGE_SIZE - len, "%d ",
  203. avg->avg_avl[i]);
  204. buf[len - 1] = '\n';
  205. return len;
  206. }
  207. int hts221_set_enable(struct hts221_hw *hw, bool enable)
  208. {
  209. int err;
  210. err = regmap_update_bits(hw->regmap, HTS221_REG_CNTRL1_ADDR,
  211. HTS221_ENABLE_MASK,
  212. FIELD_PREP(HTS221_ENABLE_MASK, enable));
  213. if (err < 0)
  214. return err;
  215. hw->enabled = enable;
  216. return 0;
  217. }
  218. static int hts221_parse_temp_caldata(struct hts221_hw *hw)
  219. {
  220. int err, *slope, *b_gen, cal0, cal1;
  221. s16 cal_x0, cal_x1, cal_y0, cal_y1;
  222. __le16 val;
  223. err = regmap_read(hw->regmap, HTS221_REG_0T_CAL_Y_H, &cal0);
  224. if (err < 0)
  225. return err;
  226. err = regmap_read(hw->regmap, HTS221_REG_T1_T0_CAL_Y_H, &cal1);
  227. if (err < 0)
  228. return err;
  229. cal_y0 = ((cal1 & 0x3) << 8) | cal0;
  230. err = regmap_read(hw->regmap, HTS221_REG_1T_CAL_Y_H, &cal0);
  231. if (err < 0)
  232. return err;
  233. cal_y1 = (((cal1 & 0xc) >> 2) << 8) | cal0;
  234. err = regmap_bulk_read(hw->regmap, HTS221_REG_0T_CAL_X_L,
  235. &val, sizeof(val));
  236. if (err < 0)
  237. return err;
  238. cal_x0 = le16_to_cpu(val);
  239. err = regmap_bulk_read(hw->regmap, HTS221_REG_1T_CAL_X_L,
  240. &val, sizeof(val));
  241. if (err < 0)
  242. return err;
  243. cal_x1 = le16_to_cpu(val);
  244. slope = &hw->sensors[HTS221_SENSOR_T].slope;
  245. b_gen = &hw->sensors[HTS221_SENSOR_T].b_gen;
  246. *slope = ((cal_y1 - cal_y0) * 8000) / (cal_x1 - cal_x0);
  247. *b_gen = (((s32)cal_x1 * cal_y0 - (s32)cal_x0 * cal_y1) * 1000) /
  248. (cal_x1 - cal_x0);
  249. *b_gen *= 8;
  250. return 0;
  251. }
  252. static int hts221_parse_rh_caldata(struct hts221_hw *hw)
  253. {
  254. int err, *slope, *b_gen, data;
  255. s16 cal_x0, cal_x1, cal_y0, cal_y1;
  256. __le16 val;
  257. err = regmap_read(hw->regmap, HTS221_REG_0RH_CAL_Y_H, &data);
  258. if (err < 0)
  259. return err;
  260. cal_y0 = data;
  261. err = regmap_read(hw->regmap, HTS221_REG_1RH_CAL_Y_H, &data);
  262. if (err < 0)
  263. return err;
  264. cal_y1 = data;
  265. err = regmap_bulk_read(hw->regmap, HTS221_REG_0RH_CAL_X_H,
  266. &val, sizeof(val));
  267. if (err < 0)
  268. return err;
  269. cal_x0 = le16_to_cpu(val);
  270. err = regmap_bulk_read(hw->regmap, HTS221_REG_1RH_CAL_X_H,
  271. &val, sizeof(val));
  272. if (err < 0)
  273. return err;
  274. cal_x1 = le16_to_cpu(val);
  275. slope = &hw->sensors[HTS221_SENSOR_H].slope;
  276. b_gen = &hw->sensors[HTS221_SENSOR_H].b_gen;
  277. *slope = ((cal_y1 - cal_y0) * 8000) / (cal_x1 - cal_x0);
  278. *b_gen = (((s32)cal_x1 * cal_y0 - (s32)cal_x0 * cal_y1) * 1000) /
  279. (cal_x1 - cal_x0);
  280. *b_gen *= 8;
  281. return 0;
  282. }
  283. static int hts221_get_sensor_scale(struct hts221_hw *hw,
  284. enum iio_chan_type ch_type,
  285. int *val, int *val2)
  286. {
  287. s64 tmp;
  288. s32 rem, div, data;
  289. switch (ch_type) {
  290. case IIO_HUMIDITYRELATIVE:
  291. data = hw->sensors[HTS221_SENSOR_H].slope;
  292. div = (1 << 4) * 1000;
  293. break;
  294. case IIO_TEMP:
  295. data = hw->sensors[HTS221_SENSOR_T].slope;
  296. div = (1 << 6) * 1000;
  297. break;
  298. default:
  299. return -EINVAL;
  300. }
  301. tmp = div_s64(data * 1000000000LL, div);
  302. tmp = div_s64_rem(tmp, 1000000000LL, &rem);
  303. *val = tmp;
  304. *val2 = rem;
  305. return IIO_VAL_INT_PLUS_NANO;
  306. }
  307. static int hts221_get_sensor_offset(struct hts221_hw *hw,
  308. enum iio_chan_type ch_type,
  309. int *val, int *val2)
  310. {
  311. s64 tmp;
  312. s32 rem, div, data;
  313. switch (ch_type) {
  314. case IIO_HUMIDITYRELATIVE:
  315. data = hw->sensors[HTS221_SENSOR_H].b_gen;
  316. div = hw->sensors[HTS221_SENSOR_H].slope;
  317. break;
  318. case IIO_TEMP:
  319. data = hw->sensors[HTS221_SENSOR_T].b_gen;
  320. div = hw->sensors[HTS221_SENSOR_T].slope;
  321. break;
  322. default:
  323. return -EINVAL;
  324. }
  325. tmp = div_s64(data * 1000000000LL, div);
  326. tmp = div_s64_rem(tmp, 1000000000LL, &rem);
  327. *val = tmp;
  328. *val2 = rem;
  329. return IIO_VAL_INT_PLUS_NANO;
  330. }
  331. static int hts221_read_oneshot(struct hts221_hw *hw, u8 addr, int *val)
  332. {
  333. __le16 data;
  334. int err;
  335. err = hts221_set_enable(hw, true);
  336. if (err < 0)
  337. return err;
  338. msleep(50);
  339. err = regmap_bulk_read(hw->regmap, addr, &data, sizeof(data));
  340. if (err < 0)
  341. return err;
  342. hts221_set_enable(hw, false);
  343. *val = (s16)le16_to_cpu(data);
  344. return IIO_VAL_INT;
  345. }
  346. static int __hts221_read_raw(struct iio_dev *iio_dev,
  347. struct iio_chan_spec const *ch,
  348. int *val, int *val2, long mask)
  349. {
  350. struct hts221_hw *hw = iio_priv(iio_dev);
  351. switch (mask) {
  352. case IIO_CHAN_INFO_RAW:
  353. return hts221_read_oneshot(hw, ch->address, val);
  354. case IIO_CHAN_INFO_SCALE:
  355. return hts221_get_sensor_scale(hw, ch->type, val, val2);
  356. case IIO_CHAN_INFO_OFFSET:
  357. return hts221_get_sensor_offset(hw, ch->type, val, val2);
  358. case IIO_CHAN_INFO_SAMP_FREQ:
  359. *val = hw->odr;
  360. return IIO_VAL_INT;
  361. case IIO_CHAN_INFO_OVERSAMPLING_RATIO: {
  362. u8 idx;
  363. const struct hts221_avg *avg;
  364. switch (ch->type) {
  365. case IIO_HUMIDITYRELATIVE:
  366. avg = &hts221_avg_list[HTS221_SENSOR_H];
  367. idx = hw->sensors[HTS221_SENSOR_H].cur_avg_idx;
  368. *val = avg->avg_avl[idx];
  369. return IIO_VAL_INT;
  370. case IIO_TEMP:
  371. avg = &hts221_avg_list[HTS221_SENSOR_T];
  372. idx = hw->sensors[HTS221_SENSOR_T].cur_avg_idx;
  373. *val = avg->avg_avl[idx];
  374. return IIO_VAL_INT;
  375. default:
  376. return -EINVAL;
  377. }
  378. }
  379. default:
  380. return -EINVAL;
  381. }
  382. }
  383. static int hts221_read_raw(struct iio_dev *iio_dev,
  384. struct iio_chan_spec const *ch,
  385. int *val, int *val2, long mask)
  386. {
  387. int ret;
  388. if (!iio_device_claim_direct(iio_dev))
  389. return -EBUSY;
  390. ret = __hts221_read_raw(iio_dev, ch, val, val2, mask);
  391. iio_device_release_direct(iio_dev);
  392. return ret;
  393. }
  394. static int __hts221_write_raw(struct iio_dev *iio_dev,
  395. struct iio_chan_spec const *chan,
  396. int val, long mask)
  397. {
  398. struct hts221_hw *hw = iio_priv(iio_dev);
  399. switch (mask) {
  400. case IIO_CHAN_INFO_SAMP_FREQ:
  401. return hts221_update_odr(hw, val);
  402. case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
  403. switch (chan->type) {
  404. case IIO_HUMIDITYRELATIVE:
  405. return hts221_update_avg(hw, HTS221_SENSOR_H, val);
  406. case IIO_TEMP:
  407. return hts221_update_avg(hw, HTS221_SENSOR_T, val);
  408. default:
  409. return -EINVAL;
  410. }
  411. default:
  412. return -EINVAL;
  413. }
  414. }
  415. static int hts221_write_raw(struct iio_dev *iio_dev,
  416. struct iio_chan_spec const *chan,
  417. int val, int val2, long mask)
  418. {
  419. int ret;
  420. if (!iio_device_claim_direct(iio_dev))
  421. return -EBUSY;
  422. ret = __hts221_write_raw(iio_dev, chan, val, mask);
  423. iio_device_release_direct(iio_dev);
  424. return ret;
  425. }
  426. static int hts221_validate_trigger(struct iio_dev *iio_dev,
  427. struct iio_trigger *trig)
  428. {
  429. struct hts221_hw *hw = iio_priv(iio_dev);
  430. return hw->trig == trig ? 0 : -EINVAL;
  431. }
  432. static IIO_DEVICE_ATTR(in_humidity_oversampling_ratio_available, S_IRUGO,
  433. hts221_sysfs_rh_oversampling_avail, NULL, 0);
  434. static IIO_DEVICE_ATTR(in_temp_oversampling_ratio_available, S_IRUGO,
  435. hts221_sysfs_temp_oversampling_avail, NULL, 0);
  436. static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(hts221_sysfs_sampling_freq);
  437. static struct attribute *hts221_attributes[] = {
  438. &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
  439. &iio_dev_attr_in_humidity_oversampling_ratio_available.dev_attr.attr,
  440. &iio_dev_attr_in_temp_oversampling_ratio_available.dev_attr.attr,
  441. NULL,
  442. };
  443. static const struct attribute_group hts221_attribute_group = {
  444. .attrs = hts221_attributes,
  445. };
  446. static const struct iio_info hts221_info = {
  447. .attrs = &hts221_attribute_group,
  448. .read_raw = hts221_read_raw,
  449. .write_raw = hts221_write_raw,
  450. .validate_trigger = hts221_validate_trigger,
  451. };
  452. static const unsigned long hts221_scan_masks[] = {0x3, 0x0};
  453. static int hts221_init_regulators(struct device *dev)
  454. {
  455. int err;
  456. err = devm_regulator_get_enable(dev, "vdd");
  457. if (err)
  458. return dev_err_probe(dev, err, "failed to get vdd regulator\n");
  459. msleep(50);
  460. return 0;
  461. }
  462. int hts221_probe(struct device *dev, int irq, const char *name,
  463. struct regmap *regmap)
  464. {
  465. struct iio_dev *iio_dev;
  466. struct hts221_hw *hw;
  467. int err;
  468. u8 data;
  469. iio_dev = devm_iio_device_alloc(dev, sizeof(*hw));
  470. if (!iio_dev)
  471. return -ENOMEM;
  472. dev_set_drvdata(dev, iio_dev);
  473. hw = iio_priv(iio_dev);
  474. hw->name = name;
  475. hw->dev = dev;
  476. hw->irq = irq;
  477. hw->regmap = regmap;
  478. err = hts221_init_regulators(dev);
  479. if (err)
  480. return err;
  481. err = hts221_check_whoami(hw);
  482. if (err < 0)
  483. return err;
  484. iio_dev->modes = INDIO_DIRECT_MODE;
  485. iio_dev->available_scan_masks = hts221_scan_masks;
  486. iio_dev->channels = hts221_channels;
  487. iio_dev->num_channels = ARRAY_SIZE(hts221_channels);
  488. iio_dev->name = HTS221_DEV_NAME;
  489. iio_dev->info = &hts221_info;
  490. /* enable Block Data Update */
  491. err = regmap_update_bits(hw->regmap, HTS221_REG_CNTRL1_ADDR,
  492. HTS221_BDU_MASK,
  493. FIELD_PREP(HTS221_BDU_MASK, 1));
  494. if (err < 0)
  495. return err;
  496. err = hts221_update_odr(hw, hts221_odr_table[0].hz);
  497. if (err < 0)
  498. return err;
  499. /* configure humidity sensor */
  500. err = hts221_parse_rh_caldata(hw);
  501. if (err < 0) {
  502. dev_err(hw->dev, "failed to get rh calibration data\n");
  503. return err;
  504. }
  505. data = hts221_avg_list[HTS221_SENSOR_H].avg_avl[3];
  506. err = hts221_update_avg(hw, HTS221_SENSOR_H, data);
  507. if (err < 0) {
  508. dev_err(hw->dev, "failed to set rh oversampling ratio\n");
  509. return err;
  510. }
  511. /* configure temperature sensor */
  512. err = hts221_parse_temp_caldata(hw);
  513. if (err < 0) {
  514. dev_err(hw->dev,
  515. "failed to get temperature calibration data\n");
  516. return err;
  517. }
  518. data = hts221_avg_list[HTS221_SENSOR_T].avg_avl[3];
  519. err = hts221_update_avg(hw, HTS221_SENSOR_T, data);
  520. if (err < 0) {
  521. dev_err(hw->dev,
  522. "failed to set temperature oversampling ratio\n");
  523. return err;
  524. }
  525. if (hw->irq > 0) {
  526. err = hts221_allocate_buffers(iio_dev);
  527. if (err < 0)
  528. return err;
  529. err = hts221_allocate_trigger(iio_dev);
  530. if (err)
  531. return err;
  532. }
  533. return devm_iio_device_register(hw->dev, iio_dev);
  534. }
  535. EXPORT_SYMBOL_NS(hts221_probe, "IIO_HTS221");
  536. static int hts221_suspend(struct device *dev)
  537. {
  538. struct iio_dev *iio_dev = dev_get_drvdata(dev);
  539. struct hts221_hw *hw = iio_priv(iio_dev);
  540. return regmap_update_bits(hw->regmap, HTS221_REG_CNTRL1_ADDR,
  541. HTS221_ENABLE_MASK,
  542. FIELD_PREP(HTS221_ENABLE_MASK, false));
  543. }
  544. static int hts221_resume(struct device *dev)
  545. {
  546. struct iio_dev *iio_dev = dev_get_drvdata(dev);
  547. struct hts221_hw *hw = iio_priv(iio_dev);
  548. int err = 0;
  549. if (hw->enabled)
  550. err = regmap_update_bits(hw->regmap, HTS221_REG_CNTRL1_ADDR,
  551. HTS221_ENABLE_MASK,
  552. FIELD_PREP(HTS221_ENABLE_MASK,
  553. true));
  554. return err;
  555. }
  556. EXPORT_NS_SIMPLE_DEV_PM_OPS(hts221_pm_ops, hts221_suspend, hts221_resume,
  557. IIO_HTS221);
  558. MODULE_AUTHOR("Lorenzo Bianconi <lorenzo.bianconi@st.com>");
  559. MODULE_DESCRIPTION("STMicroelectronics hts221 sensor driver");
  560. MODULE_LICENSE("GPL v2");