1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * All Sensors DLH series low voltage digital pressure sensors
4 *
5 * Copyright (c) 2019 AVL DiTEST GmbH
6 * Tomislav Denis <tomislav.denis@avl.com>
7 *
8 * Datasheet: https://www.allsensors.com/cad/DS-0355_Rev_B.PDF
9 */
10
11 #include <linux/module.h>
12 #include <linux/delay.h>
13 #include <linux/i2c.h>
14 #include <linux/iio/iio.h>
15 #include <linux/iio/buffer.h>
16 #include <linux/iio/trigger_consumer.h>
17 #include <linux/iio/triggered_buffer.h>
18 #include <asm/unaligned.h>
19
20 /* Commands */
21 #define DLH_START_SINGLE 0xAA
22
23 /* Status bits */
24 #define DLH_STATUS_OK 0x40
25
26 /* DLH data format */
27 #define DLH_NUM_READ_BYTES 7
28 #define DLH_NUM_DATA_BYTES 3
29 #define DLH_NUM_PR_BITS 24
30 #define DLH_NUM_TEMP_BITS 24
31
32 /* DLH timings */
33 #define DLH_SINGLE_DUT_MS 5
34
35 enum dhl_ids {
36 dlhl60d,
37 dlhl60g,
38 };
39
40 struct dlh_info {
41 u8 osdig; /* digital offset factor */
42 unsigned int fss; /* full scale span (inch H2O) */
43 };
44
45 struct dlh_state {
46 struct i2c_client *client;
47 struct dlh_info info;
48 bool use_interrupt;
49 struct completion completion;
50 u8 rx_buf[DLH_NUM_READ_BYTES];
51 };
52
53 static struct dlh_info dlh_info_tbl[] = {
54 [dlhl60d] = {
55 .osdig = 2,
56 .fss = 120,
57 },
58 [dlhl60g] = {
59 .osdig = 10,
60 .fss = 60,
61 },
62 };
63
64
dlh_cmd_start_single(struct dlh_state * st)65 static int dlh_cmd_start_single(struct dlh_state *st)
66 {
67 int ret;
68
69 ret = i2c_smbus_write_byte(st->client, DLH_START_SINGLE);
70 if (ret)
71 dev_err(&st->client->dev,
72 "%s: I2C write byte failed\n", __func__);
73
74 return ret;
75 }
76
dlh_cmd_read_data(struct dlh_state * st)77 static int dlh_cmd_read_data(struct dlh_state *st)
78 {
79 int ret;
80
81 ret = i2c_master_recv(st->client, st->rx_buf, DLH_NUM_READ_BYTES);
82 if (ret < 0) {
83 dev_err(&st->client->dev,
84 "%s: I2C read block failed\n", __func__);
85 return ret;
86 }
87
88 if (st->rx_buf[0] != DLH_STATUS_OK) {
89 dev_err(&st->client->dev,
90 "%s: invalid status 0x%02x\n", __func__, st->rx_buf[0]);
91 return -EBUSY;
92 }
93
94 return 0;
95 }
96
dlh_start_capture_and_read(struct dlh_state * st)97 static int dlh_start_capture_and_read(struct dlh_state *st)
98 {
99 int ret;
100
101 if (st->use_interrupt)
102 reinit_completion(&st->completion);
103
104 ret = dlh_cmd_start_single(st);
105 if (ret)
106 return ret;
107
108 if (st->use_interrupt) {
109 ret = wait_for_completion_timeout(&st->completion,
110 msecs_to_jiffies(DLH_SINGLE_DUT_MS));
111 if (!ret) {
112 dev_err(&st->client->dev,
113 "%s: conversion timed out\n", __func__);
114 return -ETIMEDOUT;
115 }
116 } else {
117 mdelay(DLH_SINGLE_DUT_MS);
118 }
119
120 return dlh_cmd_read_data(st);
121 }
122
dlh_read_direct(struct dlh_state * st,unsigned int * pressure,unsigned int * temperature)123 static int dlh_read_direct(struct dlh_state *st,
124 unsigned int *pressure, unsigned int *temperature)
125 {
126 int ret;
127
128 ret = dlh_start_capture_and_read(st);
129 if (ret)
130 return ret;
131
132 *pressure = get_unaligned_be24(&st->rx_buf[1]);
133 *temperature = get_unaligned_be24(&st->rx_buf[4]);
134
135 return 0;
136 }
137
dlh_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * channel,int * value,int * value2,long mask)138 static int dlh_read_raw(struct iio_dev *indio_dev,
139 struct iio_chan_spec const *channel, int *value,
140 int *value2, long mask)
141 {
142 struct dlh_state *st = iio_priv(indio_dev);
143 unsigned int pressure, temperature;
144 int ret;
145 s64 tmp;
146 s32 rem;
147
148 switch (mask) {
149 case IIO_CHAN_INFO_RAW:
150 ret = iio_device_claim_direct_mode(indio_dev);
151 if (ret)
152 return ret;
153
154 ret = dlh_read_direct(st, &pressure, &temperature);
155 iio_device_release_direct_mode(indio_dev);
156 if (ret)
157 return ret;
158
159 switch (channel->type) {
160 case IIO_PRESSURE:
161 *value = pressure;
162 return IIO_VAL_INT;
163
164 case IIO_TEMP:
165 *value = temperature;
166 return IIO_VAL_INT;
167
168 default:
169 return -EINVAL;
170 }
171 case IIO_CHAN_INFO_SCALE:
172 switch (channel->type) {
173 case IIO_PRESSURE:
174 tmp = div_s64(125LL * st->info.fss * 24909 * 100,
175 1 << DLH_NUM_PR_BITS);
176 tmp = div_s64_rem(tmp, 1000000000LL, &rem);
177 *value = tmp;
178 *value2 = rem;
179 return IIO_VAL_INT_PLUS_NANO;
180
181 case IIO_TEMP:
182 *value = 125 * 1000;
183 *value2 = DLH_NUM_TEMP_BITS;
184 return IIO_VAL_FRACTIONAL_LOG2;
185
186 default:
187 return -EINVAL;
188 }
189 case IIO_CHAN_INFO_OFFSET:
190 switch (channel->type) {
191 case IIO_PRESSURE:
192 *value = -125 * st->info.fss * 24909;
193 *value2 = 100 * st->info.osdig * 100000;
194 return IIO_VAL_FRACTIONAL;
195
196 case IIO_TEMP:
197 *value = -40 * 1000;
198 return IIO_VAL_INT;
199
200 default:
201 return -EINVAL;
202 }
203 }
204
205 return -EINVAL;
206 }
207
208 static const struct iio_info dlh_info = {
209 .read_raw = dlh_read_raw,
210 };
211
212 static const struct iio_chan_spec dlh_channels[] = {
213 {
214 .type = IIO_PRESSURE,
215 .indexed = 1,
216 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
217 .info_mask_shared_by_type =
218 BIT(IIO_CHAN_INFO_SCALE) |
219 BIT(IIO_CHAN_INFO_OFFSET),
220 .scan_index = 0,
221 .scan_type = {
222 .sign = 'u',
223 .realbits = DLH_NUM_PR_BITS,
224 .storagebits = 32,
225 .shift = 8,
226 .endianness = IIO_BE,
227 },
228 }, {
229 .type = IIO_TEMP,
230 .indexed = 1,
231 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
232 .info_mask_shared_by_type =
233 BIT(IIO_CHAN_INFO_SCALE) |
234 BIT(IIO_CHAN_INFO_OFFSET),
235 .scan_index = 1,
236 .scan_type = {
237 .sign = 'u',
238 .realbits = DLH_NUM_TEMP_BITS,
239 .storagebits = 32,
240 .shift = 8,
241 .endianness = IIO_BE,
242 },
243 }
244 };
245
dlh_trigger_handler(int irq,void * private)246 static irqreturn_t dlh_trigger_handler(int irq, void *private)
247 {
248 struct iio_poll_func *pf = private;
249 struct iio_dev *indio_dev = pf->indio_dev;
250 struct dlh_state *st = iio_priv(indio_dev);
251 int ret;
252 unsigned int chn, i = 0;
253 __be32 tmp_buf[2];
254
255 ret = dlh_start_capture_and_read(st);
256 if (ret)
257 goto out;
258
259 for_each_set_bit(chn, indio_dev->active_scan_mask,
260 indio_dev->masklength) {
261 memcpy(tmp_buf + i,
262 &st->rx_buf[1] + chn * DLH_NUM_DATA_BYTES,
263 DLH_NUM_DATA_BYTES);
264 i++;
265 }
266
267 iio_push_to_buffers(indio_dev, tmp_buf);
268
269 out:
270 iio_trigger_notify_done(indio_dev->trig);
271
272 return IRQ_HANDLED;
273 }
274
dlh_interrupt(int irq,void * private)275 static irqreturn_t dlh_interrupt(int irq, void *private)
276 {
277 struct iio_dev *indio_dev = private;
278 struct dlh_state *st = iio_priv(indio_dev);
279
280 complete(&st->completion);
281
282 return IRQ_HANDLED;
283 };
284
dlh_probe(struct i2c_client * client)285 static int dlh_probe(struct i2c_client *client)
286 {
287 const struct i2c_device_id *id = i2c_client_get_device_id(client);
288 struct dlh_state *st;
289 struct iio_dev *indio_dev;
290 int ret;
291
292 if (!i2c_check_functionality(client->adapter,
293 I2C_FUNC_I2C | I2C_FUNC_SMBUS_WRITE_BYTE)) {
294 dev_err(&client->dev,
295 "adapter doesn't support required i2c functionality\n");
296 return -EOPNOTSUPP;
297 }
298
299 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*st));
300 if (!indio_dev) {
301 dev_err(&client->dev, "failed to allocate iio device\n");
302 return -ENOMEM;
303 }
304
305 i2c_set_clientdata(client, indio_dev);
306
307 st = iio_priv(indio_dev);
308 st->info = dlh_info_tbl[id->driver_data];
309 st->client = client;
310 st->use_interrupt = false;
311
312 indio_dev->name = id->name;
313 indio_dev->info = &dlh_info;
314 indio_dev->modes = INDIO_DIRECT_MODE;
315 indio_dev->channels = dlh_channels;
316 indio_dev->num_channels = ARRAY_SIZE(dlh_channels);
317
318 if (client->irq > 0) {
319 ret = devm_request_threaded_irq(&client->dev, client->irq,
320 dlh_interrupt, NULL,
321 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
322 id->name, indio_dev);
323 if (ret) {
324 dev_err(&client->dev, "failed to allocate threaded irq");
325 return ret;
326 }
327
328 st->use_interrupt = true;
329 init_completion(&st->completion);
330 }
331
332 ret = devm_iio_triggered_buffer_setup(&client->dev, indio_dev,
333 NULL, &dlh_trigger_handler, NULL);
334 if (ret) {
335 dev_err(&client->dev, "failed to setup iio buffer\n");
336 return ret;
337 }
338
339 ret = devm_iio_device_register(&client->dev, indio_dev);
340 if (ret)
341 dev_err(&client->dev, "failed to register iio device\n");
342
343 return ret;
344 }
345
346 static const struct of_device_id dlh_of_match[] = {
347 { .compatible = "asc,dlhl60d" },
348 { .compatible = "asc,dlhl60g" },
349 {}
350 };
351 MODULE_DEVICE_TABLE(of, dlh_of_match);
352
353 static const struct i2c_device_id dlh_id[] = {
354 { "dlhl60d", dlhl60d },
355 { "dlhl60g", dlhl60g },
356 {}
357 };
358 MODULE_DEVICE_TABLE(i2c, dlh_id);
359
360 static struct i2c_driver dlh_driver = {
361 .driver = {
362 .name = "dlhl60d",
363 .of_match_table = dlh_of_match,
364 },
365 .probe = dlh_probe,
366 .id_table = dlh_id,
367 };
368 module_i2c_driver(dlh_driver);
369
370 MODULE_AUTHOR("Tomislav Denis <tomislav.denis@avl.com>");
371 MODULE_DESCRIPTION("Driver for All Sensors DLH series pressure sensors");
372 MODULE_LICENSE("GPL v2");
373