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