File Index
Files and directories
Sign in
FILE

hts221_core.c

File preview with syntax highlighting.

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