xref: /linux/drivers/iio/humidity/hts221_buffer.c (revision 3a2c4d55e32ad65efebdb6de44eef3bfa08bb49d)
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/interrupt.h>
13 #include <linux/irqreturn.h>
14 #include <linux/property.h>
15 #include <linux/regmap.h>
16 #include <linux/bitfield.h>
17 
18 #include <linux/iio/iio.h>
19 #include <linux/iio/trigger.h>
20 #include <linux/iio/events.h>
21 #include <linux/iio/trigger_consumer.h>
22 #include <linux/iio/triggered_buffer.h>
23 #include <linux/iio/buffer.h>
24 
25 #include <linux/platform_data/st_sensors_pdata.h>
26 
27 #include "hts221.h"
28 
29 #define HTS221_REG_DRDY_HL_ADDR		0x22
30 #define HTS221_REG_DRDY_HL_MASK		BIT(7)
31 #define HTS221_REG_DRDY_PP_OD_ADDR	0x22
32 #define HTS221_REG_DRDY_PP_OD_MASK	BIT(6)
33 #define HTS221_REG_DRDY_EN_ADDR		0x22
34 #define HTS221_REG_DRDY_EN_MASK		BIT(2)
35 #define HTS221_REG_STATUS_ADDR		0x27
36 #define HTS221_RH_DRDY_MASK		BIT(1)
37 #define HTS221_TEMP_DRDY_MASK		BIT(0)
38 
39 static int hts221_trig_set_state(struct iio_trigger *trig, bool state)
40 {
41 	struct iio_dev *iio_dev = iio_trigger_get_drvdata(trig);
42 	struct hts221_hw *hw = iio_priv(iio_dev);
43 
44 	return regmap_update_bits(hw->regmap, HTS221_REG_DRDY_EN_ADDR,
45 				  HTS221_REG_DRDY_EN_MASK,
46 				  FIELD_PREP(HTS221_REG_DRDY_EN_MASK, state));
47 }
48 
49 static const struct iio_trigger_ops hts221_trigger_ops = {
50 	.set_trigger_state = hts221_trig_set_state,
51 };
52 
53 static irqreturn_t hts221_trigger_handler_thread(int irq, void *private)
54 {
55 	struct hts221_hw *hw = private;
56 	int err, status;
57 
58 	err = regmap_read(hw->regmap, HTS221_REG_STATUS_ADDR, &status);
59 	if (err < 0)
60 		return IRQ_HANDLED;
61 
62 	/*
63 	 * H_DA bit (humidity data available) is routed to DRDY line.
64 	 * Humidity sample is computed after temperature one.
65 	 * Here we can assume data channels are both available if H_DA bit
66 	 * is set in status register
67 	 */
68 	if (!(status & HTS221_RH_DRDY_MASK))
69 		return IRQ_NONE;
70 
71 	iio_trigger_poll_nested(hw->trig);
72 
73 	return IRQ_HANDLED;
74 }
75 
76 int hts221_allocate_trigger(struct iio_dev *iio_dev)
77 {
78 	struct hts221_hw *hw = iio_priv(iio_dev);
79 	struct st_sensors_platform_data *pdata = dev_get_platdata(hw->dev);
80 	bool irq_active_low = false, open_drain = false;
81 	unsigned long irq_type;
82 	int err;
83 
84 	irq_type = irq_get_trigger_type(hw->irq);
85 	switch (irq_type) {
86 	case IRQF_TRIGGER_HIGH:
87 	case IRQF_TRIGGER_RISING:
88 		break;
89 	case IRQF_TRIGGER_LOW:
90 	case IRQF_TRIGGER_FALLING:
91 		irq_active_low = true;
92 		break;
93 	default:
94 		dev_info(hw->dev,
95 			 "mode %lx unsupported, using IRQF_TRIGGER_RISING\n",
96 			 irq_type);
97 		irq_type = IRQF_TRIGGER_RISING;
98 		break;
99 	}
100 
101 	err = regmap_update_bits(hw->regmap, HTS221_REG_DRDY_HL_ADDR,
102 				 HTS221_REG_DRDY_HL_MASK,
103 				 FIELD_PREP(HTS221_REG_DRDY_HL_MASK,
104 					    irq_active_low));
105 	if (err < 0)
106 		return err;
107 
108 	if (device_property_read_bool(hw->dev, "drive-open-drain") ||
109 	    (pdata && pdata->open_drain)) {
110 		irq_type |= IRQF_SHARED;
111 		open_drain = true;
112 	}
113 
114 	err = regmap_update_bits(hw->regmap, HTS221_REG_DRDY_PP_OD_ADDR,
115 				 HTS221_REG_DRDY_PP_OD_MASK,
116 				 FIELD_PREP(HTS221_REG_DRDY_PP_OD_MASK,
117 					    open_drain));
118 	if (err < 0)
119 		return err;
120 
121 	err = devm_request_threaded_irq(hw->dev, hw->irq, NULL,
122 					hts221_trigger_handler_thread,
123 					irq_type | IRQF_ONESHOT,
124 					hw->name, hw);
125 	if (err)
126 		return err;
127 
128 	hw->trig = devm_iio_trigger_alloc(hw->dev, "%s-trigger",
129 					  iio_dev->name);
130 	if (!hw->trig)
131 		return -ENOMEM;
132 
133 	iio_trigger_set_drvdata(hw->trig, iio_dev);
134 	hw->trig->ops = &hts221_trigger_ops;
135 
136 	err = devm_iio_trigger_register(hw->dev, hw->trig);
137 
138 	iio_dev->trig = iio_trigger_get(hw->trig);
139 
140 	return err;
141 }
142 
143 static int hts221_buffer_preenable(struct iio_dev *iio_dev)
144 {
145 	return hts221_set_enable(iio_priv(iio_dev), true);
146 }
147 
148 static int hts221_buffer_postdisable(struct iio_dev *iio_dev)
149 {
150 	return hts221_set_enable(iio_priv(iio_dev), false);
151 }
152 
153 static const struct iio_buffer_setup_ops hts221_buffer_ops = {
154 	.preenable = hts221_buffer_preenable,
155 	.postdisable = hts221_buffer_postdisable,
156 };
157 
158 static irqreturn_t hts221_buffer_handler_thread(int irq, void *p)
159 {
160 	struct iio_poll_func *pf = p;
161 	struct iio_dev *iio_dev = pf->indio_dev;
162 	struct hts221_hw *hw = iio_priv(iio_dev);
163 	struct iio_chan_spec const *ch;
164 	int err;
165 
166 	/* humidity data */
167 	ch = &iio_dev->channels[HTS221_SENSOR_H];
168 	err = regmap_bulk_read(hw->regmap, ch->address,
169 			       &hw->scan.channels[0],
170 			       sizeof(hw->scan.channels[0]));
171 	if (err < 0)
172 		goto out;
173 
174 	/* temperature data */
175 	ch = &iio_dev->channels[HTS221_SENSOR_T];
176 	err = regmap_bulk_read(hw->regmap, ch->address,
177 			       &hw->scan.channels[1],
178 			       sizeof(hw->scan.channels[1]));
179 	if (err < 0)
180 		goto out;
181 
182 	iio_push_to_buffers_with_timestamp(iio_dev, &hw->scan,
183 					   iio_get_time_ns(iio_dev));
184 
185 out:
186 	iio_trigger_notify_done(hw->trig);
187 
188 	return IRQ_HANDLED;
189 }
190 
191 int hts221_allocate_buffers(struct iio_dev *iio_dev)
192 {
193 	struct hts221_hw *hw = iio_priv(iio_dev);
194 	return devm_iio_triggered_buffer_setup(hw->dev, iio_dev,
195 					NULL, hts221_buffer_handler_thread,
196 					&hts221_buffer_ops);
197 }
198 
199 MODULE_AUTHOR("Lorenzo Bianconi <lorenzo.bianconi@st.com>");
200 MODULE_DESCRIPTION("STMicroelectronics hts221 buffer driver");
201 MODULE_LICENSE("GPL v2");
202