1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
|
/*
* Copyright (C) 2016 - Marcin Malagowski <mrc@bourne.st>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include <linux/delay.h>
#include <linux/device.h>
#include <linux/err.h>
#include <linux/i2c.h>
#include <linux/io.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/iio/iio.h>
#define ABP060MG_ERROR_MASK 0xC000
#define ABP060MG_RESP_TIME_MS 40
#define ABP060MG_MIN_COUNTS 1638 /* = 0x0666 (10% of u14) */
#define ABP060MG_MAX_COUNTS 14745 /* = 0x3999 (90% of u14) */
#define ABP060MG_NUM_COUNTS (ABP060MG_MAX_COUNTS - ABP060MG_MIN_COUNTS)
enum abp_variant {
/* gage [kPa] */
ABP006KG, ABP010KG, ABP016KG, ABP025KG, ABP040KG, ABP060KG, ABP100KG,
ABP160KG, ABP250KG, ABP400KG, ABP600KG, ABP001GG,
/* differential [kPa] */
ABP006KD, ABP010KD, ABP016KD, ABP025KD, ABP040KD, ABP060KD, ABP100KD,
ABP160KD, ABP250KD, ABP400KD,
/* gage [psi] */
ABP001PG, ABP005PG, ABP015PG, ABP030PG, ABP060PG, ABP100PG, ABP150PG,
/* differential [psi] */
ABP001PD, ABP005PD, ABP015PD, ABP030PD, ABP060PD,
};
struct abp_config {
int min;
int max;
};
static struct abp_config abp_config[] = {
/* mbar & kPa variants */
[ABP006KG] = { .min = 0, .max = 6000 },
[ABP010KG] = { .min = 0, .max = 10000 },
[ABP016KG] = { .min = 0, .max = 16000 },
[ABP025KG] = { .min = 0, .max = 25000 },
[ABP040KG] = { .min = 0, .max = 40000 },
[ABP060KG] = { .min = 0, .max = 60000 },
[ABP100KG] = { .min = 0, .max = 100000 },
[ABP160KG] = { .min = 0, .max = 160000 },
[ABP250KG] = { .min = 0, .max = 250000 },
[ABP400KG] = { .min = 0, .max = 400000 },
[ABP600KG] = { .min = 0, .max = 600000 },
[ABP001GG] = { .min = 0, .max = 1000000 },
[ABP006KD] = { .min = -6000, .max = 6000 },
[ABP010KD] = { .min = -10000, .max = 10000 },
[ABP016KD] = { .min = -16000, .max = 16000 },
[ABP025KD] = { .min = -25000, .max = 25000 },
[ABP040KD] = { .min = -40000, .max = 40000 },
[ABP060KD] = { .min = -60000, .max = 60000 },
[ABP100KD] = { .min = -100000, .max = 100000 },
[ABP160KD] = { .min = -160000, .max = 160000 },
[ABP250KD] = { .min = -250000, .max = 250000 },
[ABP400KD] = { .min = -400000, .max = 400000 },
/* psi variants (1 psi ~ 6895 Pa) */
[ABP001PG] = { .min = 0, .max = 6985 },
[ABP005PG] = { .min = 0, .max = 34474 },
[ABP015PG] = { .min = 0, .max = 103421 },
[ABP030PG] = { .min = 0, .max = 206843 },
[ABP060PG] = { .min = 0, .max = 413686 },
[ABP100PG] = { .min = 0, .max = 689476 },
[ABP150PG] = { .min = 0, .max = 1034214 },
[ABP001PD] = { .min = -6895, .max = 6895 },
[ABP005PD] = { .min = -34474, .max = 34474 },
[ABP015PD] = { .min = -103421, .max = 103421 },
[ABP030PD] = { .min = -206843, .max = 206843 },
[ABP060PD] = { .min = -413686, .max = 413686 },
};
struct abp_state {
struct i2c_client *client;
struct mutex lock;
/*
* bus-dependent MEASURE_REQUEST length.
* If no SMBUS_QUICK support, need to send dummy byte
*/
int mreq_len;
/* model-dependent values (calculated on probe) */
int scale;
int offset;
};
static const struct iio_chan_spec abp060mg_channels[] = {
{
.type = IIO_PRESSURE,
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
BIT(IIO_CHAN_INFO_OFFSET) | BIT(IIO_CHAN_INFO_SCALE),
},
};
static int abp060mg_get_measurement(struct abp_state *state, int *val)
{
struct i2c_client *client = state->client;
__be16 buf[2];
u16 pressure;
int ret;
buf[0] = 0;
ret = i2c_master_send(client, (u8 *)&buf, state->mreq_len);
if (ret < 0)
return ret;
msleep_interruptible(ABP060MG_RESP_TIME_MS);
ret = i2c_master_recv(client, (u8 *)&buf, sizeof(buf));
if (ret < 0)
return ret;
pressure = be16_to_cpu(buf[0]);
if (pressure & ABP060MG_ERROR_MASK)
return -EIO;
if (pressure < ABP060MG_MIN_COUNTS || pressure > ABP060MG_MAX_COUNTS)
return -EIO;
*val = pressure;
return IIO_VAL_INT;
}
static int abp060mg_read_raw(struct iio_dev *indio_dev,
struct iio_chan_spec const *chan, int *val,
int *val2, long mask)
{
struct abp_state *state = iio_priv(indio_dev);
int ret;
mutex_lock(&state->lock);
switch (mask) {
case IIO_CHAN_INFO_RAW:
ret = abp060mg_get_measurement(state, val);
break;
case IIO_CHAN_INFO_OFFSET:
*val = state->offset;
ret = IIO_VAL_INT;
break;
case IIO_CHAN_INFO_SCALE:
*val = state->scale;
*val2 = ABP060MG_NUM_COUNTS * 1000; /* to kPa */
ret = IIO_VAL_FRACTIONAL;
break;
default:
ret = -EINVAL;
break;
}
mutex_unlock(&state->lock);
return ret;
}
static const struct iio_info abp060mg_info = {
.read_raw = abp060mg_read_raw,
};
static void abp060mg_init_device(struct iio_dev *indio_dev, unsigned long id)
{
struct abp_state *state = iio_priv(indio_dev);
struct abp_config *cfg = &abp_config[id];
state->scale = cfg->max - cfg->min;
state->offset = -ABP060MG_MIN_COUNTS;
if (cfg->min < 0) /* differential */
state->offset -= ABP060MG_NUM_COUNTS >> 1;
}
static int abp060mg_probe(struct i2c_client *client,
const struct i2c_device_id *id)
{
struct iio_dev *indio_dev;
struct abp_state *state;
unsigned long cfg_id = id->driver_data;
indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*state));
if (!indio_dev)
return -ENOMEM;
state = iio_priv(indio_dev);
i2c_set_clientdata(client, state);
state->client = client;
if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_QUICK))
state->mreq_len =
|