1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Honeywell ABP2 series pressure sensor driver
4 *
5 * Copyright (c) 2025 Petre Rodan <petre.rodan@subdimension.ro>
6 *
7 * Datasheet: https://prod-edam.honeywell.com/content/dam/honeywell-edam/sps/siot/en-us/products/sensors/pressure-sensors/board-mount-pressure-sensors/basic-abp2-series/documents/sps-siot-abp2-series-datasheet-32350268-en.pdf
8 */
9
10 #include <linux/array_size.h>
11 #include <linux/bits.h>
12 #include <linux/completion.h>
13 #include <linux/delay.h>
14 #include <linux/dev_printk.h>
15 #include <linux/device.h>
16 #include <linux/errno.h>
17 #include <linux/export.h>
18 #include <linux/gpio/consumer.h>
19 #include <linux/interrupt.h>
20 #include <linux/jiffies.h>
21 #include <linux/math64.h>
22 #include <linux/module.h>
23 #include <linux/property.h>
24 #include <linux/regulator/consumer.h>
25 #include <linux/string.h>
26 #include <linux/time.h>
27 #include <linux/types.h>
28 #include <linux/unaligned.h>
29 #include <linux/units.h>
30
31 #include <linux/iio/buffer.h>
32 #include <linux/iio/iio.h>
33 #include <linux/iio/trigger_consumer.h>
34 #include <linux/iio/triggered_buffer.h>
35
36 #include "abp2030pa.h"
37
38 /* Status byte flags */
39 #define ABP2_ST_POWER BIT(6) /* 1 if device is powered */
40 #define ABP2_ST_BUSY BIT(5) /* 1 if device is busy */
41
42 #define ABP2_CMD_NOP 0xf0
43 #define ABP2_CMD_SYNC 0xaa
44 #define ABP2_PKT_SYNC_LEN 3
45 #define ABP2_PKT_NOP_LEN ABP2_MEASUREMENT_RD_SIZE
46
47 struct abp2_func_spec {
48 u32 output_min;
49 u32 output_max;
50 };
51
52 /* transfer function A: 10% to 90% of 2^24 */
53 static const struct abp2_func_spec abp2_func_spec[] = {
54 [ABP2_FUNCTION_A] = { .output_min = 1677722, .output_max = 15099494 },
55 };
56
57 enum abp2_variants {
58 ABP2001BA, ABP21_6BA, ABP22_5BA, ABP2004BA, ABP2006BA, ABP2008BA,
59 ABP2010BA, ABP2012BA, ABP2001BD, ABP21_6BD, ABP22_5BD, ABP2004BD,
60 ABP2001BG, ABP21_6BG, ABP22_5BG, ABP2004BG, ABP2006BG, ABP2008BG,
61 ABP2010BG, ABP2012BG, ABP2001GG, ABP21_2GG, ABP2100KA, ABP2160KA,
62 ABP2250KA, ABP2001KD, ABP21_6KD, ABP22_5KD, ABP2004KD, ABP2006KD,
63 ABP2010KD, ABP2016KD, ABP2025KD, ABP2040KD, ABP2060KD, ABP2100KD,
64 ABP2160KD, ABP2250KD, ABP2400KD, ABP2001KG, ABP21_6KG, ABP22_5KG,
65 ABP2004KG, ABP2006KG, ABP2010KG, ABP2016KG, ABP2025KG, ABP2040KG,
66 ABP2060KG, ABP2100KG, ABP2160KG, ABP2250KG, ABP2400KG, ABP2600KG,
67 ABP2800KG, ABP2250LD, ABP2600LD, ABP2600LG, ABP22_5MD, ABP2006MD,
68 ABP2010MD, ABP2016MD, ABP2025MD, ABP2040MD, ABP2060MD, ABP2100MD,
69 ABP2160MD, ABP2250MD, ABP2400MD, ABP2600MD, ABP2006MG, ABP2010MG,
70 ABP2016MG, ABP2025MG, ABP2040MG, ABP2060MG, ABP2100MG, ABP2160MG,
71 ABP2250MG, ABP2400MG, ABP2600MG, ABP2001ND, ABP2002ND, ABP2004ND,
72 ABP2005ND, ABP2010ND, ABP2020ND, ABP2030ND, ABP2002NG, ABP2004NG,
73 ABP2005NG, ABP2010NG, ABP2020NG, ABP2030NG, ABP2015PA, ABP2030PA,
74 ABP2060PA, ABP2100PA, ABP2150PA, ABP2175PA, ABP2001PD, ABP2005PD,
75 ABP2015PD, ABP2030PD, ABP2060PD, ABP2001PG, ABP2005PG, ABP2015PG,
76 ABP2030PG, ABP2060PG, ABP2100PG, ABP2150PG, ABP2175PG,
77 };
78
79 static const char * const abp2_triplet_variants[] = {
80 [ABP2001BA] = "001BA", [ABP21_6BA] = "1.6BA", [ABP22_5BA] = "2.5BA",
81 [ABP2004BA] = "004BA", [ABP2006BA] = "006BA", [ABP2008BA] = "008BA",
82 [ABP2010BA] = "010BA", [ABP2012BA] = "012BA", [ABP2001BD] = "001BD",
83 [ABP21_6BD] = "1.6BD", [ABP22_5BD] = "2.5BD", [ABP2004BD] = "004BD",
84 [ABP2001BG] = "001BG", [ABP21_6BG] = "1.6BG", [ABP22_5BG] = "2.5BG",
85 [ABP2004BG] = "004BG", [ABP2006BG] = "006BG", [ABP2008BG] = "008BG",
86 [ABP2010BG] = "010BG", [ABP2012BG] = "012BG", [ABP2001GG] = "001GG",
87 [ABP21_2GG] = "1.2GG", [ABP2100KA] = "100KA", [ABP2160KA] = "160KA",
88 [ABP2250KA] = "250KA", [ABP2001KD] = "001KD", [ABP21_6KD] = "1.6KD",
89 [ABP22_5KD] = "2.5KD", [ABP2004KD] = "004KD", [ABP2006KD] = "006KD",
90 [ABP2010KD] = "010KD", [ABP2016KD] = "016KD", [ABP2025KD] = "025KD",
91 [ABP2040KD] = "040KD", [ABP2060KD] = "060KD", [ABP2100KD] = "100KD",
92 [ABP2160KD] = "160KD", [ABP2250KD] = "250KD", [ABP2400KD] = "400KD",
93 [ABP2001KG] = "001KG", [ABP21_6KG] = "1.6KG", [ABP22_5KG] = "2.5KG",
94 [ABP2004KG] = "004KG", [ABP2006KG] = "006KG", [ABP2010KG] = "010KG",
95 [ABP2016KG] = "016KG", [ABP2025KG] = "025KG", [ABP2040KG] = "040KG",
96 [ABP2060KG] = "060KG", [ABP2100KG] = "100KG", [ABP2160KG] = "160KG",
97 [ABP2250KG] = "250KG", [ABP2400KG] = "400KG", [ABP2600KG] = "600KG",
98 [ABP2800KG] = "800KG", [ABP2250LD] = "250LD", [ABP2600LD] = "600LD",
99 [ABP2600LG] = "600LG", [ABP22_5MD] = "2.5MD", [ABP2006MD] = "006MD",
100 [ABP2010MD] = "010MD", [ABP2016MD] = "016MD", [ABP2025MD] = "025MD",
101 [ABP2040MD] = "040MD", [ABP2060MD] = "060MD", [ABP2100MD] = "100MD",
102 [ABP2160MD] = "160MD", [ABP2250MD] = "250MD", [ABP2400MD] = "400MD",
103 [ABP2600MD] = "600MD", [ABP2006MG] = "006MG", [ABP2010MG] = "010MG",
104 [ABP2016MG] = "016MG", [ABP2025MG] = "025MG", [ABP2040MG] = "040MG",
105 [ABP2060MG] = "060MG", [ABP2100MG] = "100MG", [ABP2160MG] = "160MG",
106 [ABP2250MG] = "250MG", [ABP2400MG] = "400MG", [ABP2600MG] = "600MG",
107 [ABP2001ND] = "001ND", [ABP2002ND] = "002ND", [ABP2004ND] = "004ND",
108 [ABP2005ND] = "005ND", [ABP2010ND] = "010ND", [ABP2020ND] = "020ND",
109 [ABP2030ND] = "030ND", [ABP2002NG] = "002NG", [ABP2004NG] = "004NG",
110 [ABP2005NG] = "005NG", [ABP2010NG] = "010NG", [ABP2020NG] = "020NG",
111 [ABP2030NG] = "030NG", [ABP2015PA] = "015PA", [ABP2030PA] = "030PA",
112 [ABP2060PA] = "060PA", [ABP2100PA] = "100PA", [ABP2150PA] = "150PA",
113 [ABP2175PA] = "175PA", [ABP2001PD] = "001PD", [ABP2005PD] = "005PD",
114 [ABP2015PD] = "015PD", [ABP2030PD] = "030PD", [ABP2060PD] = "060PD",
115 [ABP2001PG] = "001PG", [ABP2005PG] = "005PG", [ABP2015PG] = "015PG",
116 [ABP2030PG] = "030PG", [ABP2060PG] = "060PG", [ABP2100PG] = "100PG",
117 [ABP2150PG] = "150PG", [ABP2175PG] = "175PG",
118 };
119
120 /**
121 * struct abp2_range_config - list of pressure ranges based on nomenclature
122 * @pmin: lowest pressure that can be measured
123 * @pmax: highest pressure that can be measured
124 */
125 struct abp2_range_config {
126 s32 pmin;
127 s32 pmax;
128 };
129
130 /* All min max limits have been converted to pascals */
131 static const struct abp2_range_config abp2_range_config[] = {
132 [ABP2001BA] = { .pmin = 0, .pmax = 100000 },
133 [ABP21_6BA] = { .pmin = 0, .pmax = 160000 },
134 [ABP22_5BA] = { .pmin = 0, .pmax = 250000 },
135 [ABP2004BA] = { .pmin = 0, .pmax = 400000 },
136 [ABP2006BA] = { .pmin = 0, .pmax = 600000 },
137 [ABP2008BA] = { .pmin = 0, .pmax = 800000 },
138 [ABP2010BA] = { .pmin = 0, .pmax = 1000000 },
139 [ABP2012BA] = { .pmin = 0, .pmax = 1200000 },
140 [ABP2001BD] = { .pmin = -100000, .pmax = 100000 },
141 [ABP21_6BD] = { .pmin = -160000, .pmax = 160000 },
142 [ABP22_5BD] = { .pmin = -250000, .pmax = 250000 },
143 [ABP2004BD] = { .pmin = -400000, .pmax = 400000 },
144 [ABP2001BG] = { .pmin = 0, .pmax = 100000 },
145 [ABP21_6BG] = { .pmin = 0, .pmax = 160000 },
146 [ABP22_5BG] = { .pmin = 0, .pmax = 250000 },
147 [ABP2004BG] = { .pmin = 0, .pmax = 400000 },
148 [ABP2006BG] = { .pmin = 0, .pmax = 600000 },
149 [ABP2008BG] = { .pmin = 0, .pmax = 800000 },
150 [ABP2010BG] = { .pmin = 0, .pmax = 1000000 },
151 [ABP2012BG] = { .pmin = 0, .pmax = 1200000 },
152 [ABP2001GG] = { .pmin = 0, .pmax = 1000000 },
153 [ABP21_2GG] = { .pmin = 0, .pmax = 1200000 },
154 [ABP2100KA] = { .pmin = 0, .pmax = 100000 },
155 [ABP2160KA] = { .pmin = 0, .pmax = 160000 },
156 [ABP2250KA] = { .pmin = 0, .pmax = 250000 },
157 [ABP2001KD] = { .pmin = -1000, .pmax = 1000 },
158 [ABP21_6KD] = { .pmin = -1600, .pmax = 1600 },
159 [ABP22_5KD] = { .pmin = -2500, .pmax = 2500 },
160 [ABP2004KD] = { .pmin = -4000, .pmax = 4000 },
161 [ABP2006KD] = { .pmin = -6000, .pmax = 6000 },
162 [ABP2010KD] = { .pmin = -10000, .pmax = 10000 },
163 [ABP2016KD] = { .pmin = -16000, .pmax = 16000 },
164 [ABP2025KD] = { .pmin = -25000, .pmax = 25000 },
165 [ABP2040KD] = { .pmin = -40000, .pmax = 40000 },
166 [ABP2060KD] = { .pmin = -60000, .pmax = 60000 },
167 [ABP2100KD] = { .pmin = -100000, .pmax = 100000 },
168 [ABP2160KD] = { .pmin = -160000, .pmax = 160000 },
169 [ABP2250KD] = { .pmin = -250000, .pmax = 250000 },
170 [ABP2400KD] = { .pmin = -400000, .pmax = 400000 },
171 [ABP2001KG] = { .pmin = 0, .pmax = 1000 },
172 [ABP21_6KG] = { .pmin = 0, .pmax = 1600 },
173 [ABP22_5KG] = { .pmin = 0, .pmax = 2500 },
174 [ABP2004KG] = { .pmin = 0, .pmax = 4000 },
175 [ABP2006KG] = { .pmin = 0, .pmax = 6000 },
176 [ABP2010KG] = { .pmin = 0, .pmax = 10000 },
177 [ABP2016KG] = { .pmin = 0, .pmax = 16000 },
178 [ABP2025KG] = { .pmin = 0, .pmax = 25000 },
179 [ABP2040KG] = { .pmin = 0, .pmax = 40000 },
180 [ABP2060KG] = { .pmin = 0, .pmax = 60000 },
181 [ABP2100KG] = { .pmin = 0, .pmax = 100000 },
182 [ABP2160KG] = { .pmin = 0, .pmax = 160000 },
183 [ABP2250KG] = { .pmin = 0, .pmax = 250000 },
184 [ABP2400KG] = { .pmin = 0, .pmax = 400000 },
185 [ABP2600KG] = { .pmin = 0, .pmax = 600000 },
186 [ABP2800KG] = { .pmin = 0, .pmax = 800000 },
187 [ABP2250LD] = { .pmin = -250, .pmax = 250 },
188 [ABP2600LD] = { .pmin = -600, .pmax = 600 },
189 [ABP2600LG] = { .pmin = 0, .pmax = 600 },
190 [ABP22_5MD] = { .pmin = -250, .pmax = 250 },
191 [ABP2006MD] = { .pmin = -600, .pmax = 600 },
192 [ABP2010MD] = { .pmin = -1000, .pmax = 1000 },
193 [ABP2016MD] = { .pmin = -1600, .pmax = 1600 },
194 [ABP2025MD] = { .pmin = -2500, .pmax = 2500 },
195 [ABP2040MD] = { .pmin = -4000, .pmax = 4000 },
196 [ABP2060MD] = { .pmin = -6000, .pmax = 6000 },
197 [ABP2100MD] = { .pmin = -10000, .pmax = 10000 },
198 [ABP2160MD] = { .pmin = -16000, .pmax = 16000 },
199 [ABP2250MD] = { .pmin = -25000, .pmax = 25000 },
200 [ABP2400MD] = { .pmin = -40000, .pmax = 40000 },
201 [ABP2600MD] = { .pmin = -60000, .pmax = 60000 },
202 [ABP2006MG] = { .pmin = 0, .pmax = 600 },
203 [ABP2010MG] = { .pmin = 0, .pmax = 1000 },
204 [ABP2016MG] = { .pmin = 0, .pmax = 1600 },
205 [ABP2025MG] = { .pmin = 0, .pmax = 2500 },
206 [ABP2040MG] = { .pmin = 0, .pmax = 4000 },
207 [ABP2060MG] = { .pmin = 0, .pmax = 6000 },
208 [ABP2100MG] = { .pmin = 0, .pmax = 10000 },
209 [ABP2160MG] = { .pmin = 0, .pmax = 16000 },
210 [ABP2250MG] = { .pmin = 0, .pmax = 25000 },
211 [ABP2400MG] = { .pmin = 0, .pmax = 40000 },
212 [ABP2600MG] = { .pmin = 0, .pmax = 60000 },
213 [ABP2001ND] = { .pmin = -249, .pmax = 249 },
214 [ABP2002ND] = { .pmin = -498, .pmax = 498 },
215 [ABP2004ND] = { .pmin = -996, .pmax = 996 },
216 [ABP2005ND] = { .pmin = -1245, .pmax = 1245 },
217 [ABP2010ND] = { .pmin = -2491, .pmax = 2491 },
218 [ABP2020ND] = { .pmin = -4982, .pmax = 4982 },
219 [ABP2030ND] = { .pmin = -7473, .pmax = 7473 },
220 [ABP2002NG] = { .pmin = 0, .pmax = 498 },
221 [ABP2004NG] = { .pmin = 0, .pmax = 996 },
222 [ABP2005NG] = { .pmin = 0, .pmax = 1245 },
223 [ABP2010NG] = { .pmin = 0, .pmax = 2491 },
224 [ABP2020NG] = { .pmin = 0, .pmax = 4982 },
225 [ABP2030NG] = { .pmin = 0, .pmax = 7473 },
226 [ABP2015PA] = { .pmin = 0, .pmax = 103421 },
227 [ABP2030PA] = { .pmin = 0, .pmax = 206843 },
228 [ABP2060PA] = { .pmin = 0, .pmax = 413685 },
229 [ABP2100PA] = { .pmin = 0, .pmax = 689476 },
230 [ABP2150PA] = { .pmin = 0, .pmax = 1034214 },
231 [ABP2175PA] = { .pmin = 0, .pmax = 1206583 },
232 [ABP2001PD] = { .pmin = -6895, .pmax = 6895 },
233 [ABP2005PD] = { .pmin = -34474, .pmax = 34474 },
234 [ABP2015PD] = { .pmin = -103421, .pmax = 103421 },
235 [ABP2030PD] = { .pmin = -206843, .pmax = 206843 },
236 [ABP2060PD] = { .pmin = -413685, .pmax = 413685 },
237 [ABP2001PG] = { .pmin = 0, .pmax = 6895 },
238 [ABP2005PG] = { .pmin = 0, .pmax = 34474 },
239 [ABP2015PG] = { .pmin = 0, .pmax = 103421 },
240 [ABP2030PG] = { .pmin = 0, .pmax = 206843 },
241 [ABP2060PG] = { .pmin = 0, .pmax = 413685 },
242 [ABP2100PG] = { .pmin = 0, .pmax = 689476 },
243 [ABP2150PG] = { .pmin = 0, .pmax = 1034214 },
244 [ABP2175PG] = { .pmin = 0, .pmax = 1206583 },
245 };
246
247 static_assert(ARRAY_SIZE(abp2_triplet_variants) == ARRAY_SIZE(abp2_range_config));
248
abp2_get_measurement(struct abp2_data * data)249 static int abp2_get_measurement(struct abp2_data *data)
250 {
251 struct device *dev = data->dev;
252 int ret;
253
254 reinit_completion(&data->completion);
255
256 ret = data->ops->write(data, ABP2_CMD_SYNC, ABP2_PKT_SYNC_LEN);
257 if (ret < 0)
258 return ret;
259
260 if (data->irq > 0) {
261 ret = wait_for_completion_timeout(&data->completion, HZ);
262 if (!ret) {
263 dev_err(dev, "timeout waiting for EOC interrupt\n");
264 return -ETIMEDOUT;
265 }
266 } else {
267 fsleep(5 * USEC_PER_MSEC);
268 }
269
270 memset(data->rx_buf, 0, sizeof(data->rx_buf));
271 ret = data->ops->read(data, ABP2_CMD_NOP, ABP2_PKT_NOP_LEN);
272 if (ret < 0)
273 return ret;
274
275 /*
276 * Status byte flags
277 * bit7 SANITY_CHK - must always be 0
278 * bit6 ABP2_ST_POWER - 1 if device is powered
279 * bit5 ABP2_ST_BUSY - 1 if device has no new conversion ready
280 * bit4 SANITY_CHK - must always be 0
281 * bit3 SANITY_CHK - must always be 0
282 * bit2 MEMORY_ERR - 1 if integrity test has failed
283 * bit1 SANITY_CHK - must always be 0
284 * bit0 MATH_ERR - 1 during internal math saturation error
285 */
286
287 if (data->rx_buf[0] == (ABP2_ST_POWER | ABP2_ST_BUSY))
288 return -EBUSY;
289
290 /*
291 * The ABP2 sensor series seem to have a noticeable latch-up sensitivity.
292 * A partial latch-up condition manifests as either
293 * - output of invalid status bytes
294 * - zeroed out conversions (despite a normal status byte)
295 * - the MOSI line being pulled low randomly in sync with the SCLK
296 * signal (visible during the ABP2_CMD_NOP command).
297 * https://e2e.ti.com/support/processors-group/processors/f/processors-forum/1588325/am3358-spi-tx-data-corruption
298 */
299
300 if (data->rx_buf[0] != ABP2_ST_POWER) {
301 dev_err(data->dev,
302 "unexpected status byte 0x%02x\n", data->rx_buf[0]);
303 return -EIO;
304 }
305
306 return 0;
307 }
308
abp2_eoc_handler(int irq,void * private)309 static irqreturn_t abp2_eoc_handler(int irq, void *private)
310 {
311 struct abp2_data *data = private;
312
313 complete(&data->completion);
314
315 return IRQ_HANDLED;
316 }
317
abp2_trigger_handler(int irq,void * private)318 static irqreturn_t abp2_trigger_handler(int irq, void *private)
319 {
320 int ret;
321 struct iio_poll_func *pf = private;
322 struct iio_dev *indio_dev = pf->indio_dev;
323 struct abp2_data *data = iio_priv(indio_dev);
324
325 ret = abp2_get_measurement(data);
326 if (ret < 0)
327 goto out_notify_done;
328
329 data->scan.chan[0] = get_unaligned_be24(&data->rx_buf[1]);
330 data->scan.chan[1] = get_unaligned_be24(&data->rx_buf[4]);
331
332 iio_push_to_buffers_with_ts(indio_dev, &data->scan, sizeof(data->scan),
333 iio_get_time_ns(indio_dev));
334
335 out_notify_done:
336 iio_trigger_notify_done(indio_dev->trig);
337
338 return IRQ_HANDLED;
339 }
340
341 /*
342 * IIO ABI expects
343 * value = (conv + offset) * scale
344 *
345 * temp[C] = conv * a + b
346 * where a = 200/16777215; b = -50
347 *
348 * temp[C] = (conv + (b/a)) * a * (1000)
349 * =>
350 * scale = a * 1000 = .0000119209296 * 1000 = .01192092966562
351 * offset = b/a = -50 * 16777215 / 200 = -4194303.75
352 *
353 * pressure = (conv - Omin) * Q + Pmin =
354 * ((conv - Omin) + Pmin/Q) * Q
355 * =>
356 * scale = Q = (Pmax - Pmin) / (Omax - Omin)
357 * offset = Pmin/Q - Omin = Pmin * (Omax - Omin) / (Pmax - Pmin) - Omin
358 */
abp2_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * channel,int * val,int * val2,long mask)359 static int abp2_read_raw(struct iio_dev *indio_dev,
360 struct iio_chan_spec const *channel, int *val,
361 int *val2, long mask)
362 {
363 struct abp2_data *data = iio_priv(indio_dev);
364 int ret;
365
366 switch (mask) {
367 case IIO_CHAN_INFO_RAW:
368 ret = abp2_get_measurement(data);
369 if (ret < 0)
370 return ret;
371
372 switch (channel->type) {
373 case IIO_PRESSURE:
374 *val = get_unaligned_be24(&data->rx_buf[1]);
375 return IIO_VAL_INT;
376 case IIO_TEMP:
377 *val = get_unaligned_be24(&data->rx_buf[4]);
378 return IIO_VAL_INT;
379 default:
380 return -EINVAL;
381 }
382 return IIO_VAL_INT;
383
384 case IIO_CHAN_INFO_SCALE:
385 switch (channel->type) {
386 case IIO_TEMP:
387 *val = 0;
388 *val2 = 11920929;
389 return IIO_VAL_INT_PLUS_NANO;
390 case IIO_PRESSURE:
391 *val = data->p_scale;
392 *val2 = data->p_scale_dec;
393 return IIO_VAL_INT_PLUS_NANO;
394 default:
395 return -EINVAL;
396 }
397
398 case IIO_CHAN_INFO_OFFSET:
399 switch (channel->type) {
400 case IIO_TEMP:
401 *val = -4194304;
402 return IIO_VAL_INT;
403 case IIO_PRESSURE:
404 *val = data->p_offset;
405 return IIO_VAL_INT;
406 default:
407 return -EINVAL;
408 }
409
410 default:
411 return -EINVAL;
412 }
413 }
414
415 static const struct iio_info abp2_info = {
416 .read_raw = &abp2_read_raw,
417 };
418
419 static const unsigned long abp2_scan_masks[] = {0x3, 0};
420
421 static const struct iio_chan_spec abp2_channels[] = {
422 {
423 .type = IIO_PRESSURE,
424 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
425 BIT(IIO_CHAN_INFO_SCALE) |
426 BIT(IIO_CHAN_INFO_OFFSET),
427 .scan_index = 0,
428 .scan_type = {
429 .sign = 'u',
430 .realbits = 24,
431 .storagebits = 32,
432 .endianness = IIO_CPU,
433 },
434 },
435 {
436 .type = IIO_TEMP,
437 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
438 BIT(IIO_CHAN_INFO_SCALE) |
439 BIT(IIO_CHAN_INFO_OFFSET),
440 .scan_index = 1,
441 .scan_type = {
442 .sign = 'u',
443 .realbits = 24,
444 .storagebits = 32,
445 .endianness = IIO_CPU,
446 },
447 },
448 IIO_CHAN_SOFT_TIMESTAMP(2),
449 };
450
abp2_common_probe(struct device * dev,const struct abp2_ops * ops,int irq)451 int abp2_common_probe(struct device *dev, const struct abp2_ops *ops, int irq)
452 {
453 int ret;
454 struct abp2_data *data;
455 struct iio_dev *indio_dev;
456 const char *triplet;
457 s32 tmp;
458 s64 odelta, pdelta;
459
460 indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
461 if (!indio_dev)
462 return -ENOMEM;
463
464 data = iio_priv(indio_dev);
465 data->dev = dev;
466 data->ops = ops;
467 data->irq = irq;
468
469 init_completion(&data->completion);
470
471 indio_dev->name = "abp2030pa";
472 indio_dev->info = &abp2_info;
473 indio_dev->channels = abp2_channels;
474 indio_dev->num_channels = ARRAY_SIZE(abp2_channels);
475 indio_dev->modes = INDIO_DIRECT_MODE;
476 indio_dev->available_scan_masks = abp2_scan_masks;
477
478 ret = devm_regulator_get_enable(dev, "vdd");
479 if (ret)
480 return dev_err_probe(dev, ret, "can't get and enable vdd supply\n");
481
482 ret = device_property_read_string(dev, "honeywell,pressure-triplet",
483 &triplet);
484 if (ret) {
485 ret = device_property_read_u32(dev, "honeywell,pmin-pascal",
486 &data->pmin);
487 if (ret)
488 return dev_err_probe(dev, ret,
489 "honeywell,pmin-pascal could not be read\n");
490
491 ret = device_property_read_u32(dev, "honeywell,pmax-pascal",
492 &data->pmax);
493 if (ret)
494 return dev_err_probe(dev, ret,
495 "honeywell,pmax-pascal could not be read\n");
496 } else {
497 ret = device_property_match_property_string(dev,
498 "honeywell,pressure-triplet",
499 abp2_triplet_variants,
500 ARRAY_SIZE(abp2_triplet_variants));
501 if (ret < 0)
502 return dev_err_probe(dev, -EINVAL, "honeywell,pressure-triplet is invalid\n");
503
504 data->pmin = abp2_range_config[ret].pmin;
505 data->pmax = abp2_range_config[ret].pmax;
506 }
507
508 if (data->pmin >= data->pmax)
509 return dev_err_probe(dev, -EINVAL, "pressure limits are invalid\n");
510
511 data->outmin = abp2_func_spec[data->function].output_min;
512 data->outmax = abp2_func_spec[data->function].output_max;
513
514 odelta = data->outmax - data->outmin;
515 pdelta = data->pmax - data->pmin;
516
517 data->p_scale = div_s64_rem(div_s64(pdelta * NANO, odelta), NANO, &tmp);
518 data->p_scale_dec = tmp;
519
520 data->p_offset = div_s64(odelta * data->pmin, pdelta) - data->outmin;
521
522 if (data->irq > 0) {
523 ret = devm_request_irq(dev, irq, abp2_eoc_handler, 0,
524 dev_name(dev), data);
525 if (ret)
526 return ret;
527 }
528
529 ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL,
530 abp2_trigger_handler, NULL);
531 if (ret)
532 return dev_err_probe(dev, ret, "iio triggered buffer setup failed\n");
533
534 ret = devm_iio_device_register(dev, indio_dev);
535 if (ret)
536 return dev_err_probe(dev, ret, "unable to register iio device\n");
537
538 return 0;
539 }
540 EXPORT_SYMBOL_NS_GPL(abp2_common_probe, "IIO_HONEYWELL_ABP2030PA");
541
542 MODULE_AUTHOR("Petre Rodan <petre.rodan@subdimension.ro>");
543 MODULE_DESCRIPTION("Honeywell ABP2 pressure sensor core driver");
544 MODULE_LICENSE("GPL");
545