1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * mpl3115.c - Support for Freescale MPL3115A2 pressure/temperature sensor
4 *
5 * Copyright (c) 2013 Peter Meerwald <pmeerw@pmeerw.net>
6 *
7 * (7-bit I2C slave address 0x60)
8 *
9 * TODO: FIFO buffer, altimeter mode, oversampling, continuous mode,
10 * interrupts, user offset correction, raw mode
11 */
12
13 #include <linux/module.h>
14 #include <linux/i2c.h>
15 #include <linux/iio/iio.h>
16 #include <linux/iio/sysfs.h>
17 #include <linux/iio/trigger_consumer.h>
18 #include <linux/iio/buffer.h>
19 #include <linux/iio/triggered_buffer.h>
20 #include <linux/delay.h>
21
22 #define MPL3115_STATUS 0x00
23 #define MPL3115_OUT_PRESS 0x01 /* MSB first, 20 bit */
24 #define MPL3115_OUT_TEMP 0x04 /* MSB first, 12 bit */
25 #define MPL3115_WHO_AM_I 0x0c
26 #define MPL3115_CTRL_REG1 0x26
27
28 #define MPL3115_DEVICE_ID 0xc4
29
30 #define MPL3115_STATUS_PRESS_RDY BIT(2)
31 #define MPL3115_STATUS_TEMP_RDY BIT(1)
32
33 #define MPL3115_CTRL_RESET BIT(2) /* software reset */
34 #define MPL3115_CTRL_OST BIT(1) /* initiate measurement */
35 #define MPL3115_CTRL_ACTIVE BIT(0) /* continuous measurement */
36 #define MPL3115_CTRL_OS_258MS (BIT(5) | BIT(4)) /* 64x oversampling */
37
38 struct mpl3115_data {
39 struct i2c_client *client;
40 struct mutex lock;
41 u8 ctrl_reg1;
42 };
43
mpl3115_request(struct mpl3115_data * data)44 static int mpl3115_request(struct mpl3115_data *data)
45 {
46 int ret, tries = 15;
47
48 /* trigger measurement */
49 ret = i2c_smbus_write_byte_data(data->client, MPL3115_CTRL_REG1,
50 data->ctrl_reg1 | MPL3115_CTRL_OST);
51 if (ret < 0)
52 return ret;
53
54 while (tries-- > 0) {
55 ret = i2c_smbus_read_byte_data(data->client, MPL3115_CTRL_REG1);
56 if (ret < 0)
57 return ret;
58 /* wait for data ready, i.e. OST cleared */
59 if (!(ret & MPL3115_CTRL_OST))
60 break;
61 msleep(20);
62 }
63
64 if (tries < 0) {
65 dev_err(&data->client->dev, "data not ready\n");
66 return -EIO;
67 }
68
69 return 0;
70 }
71
mpl3115_read_info_raw(struct mpl3115_data * data,struct iio_chan_spec const * chan,int * val)72 static int mpl3115_read_info_raw(struct mpl3115_data *data,
73 struct iio_chan_spec const *chan, int *val)
74 {
75 int ret;
76
77 switch (chan->type) {
78 case IIO_PRESSURE: { /* in 0.25 pascal / LSB */
79 __be32 tmp = 0;
80
81 guard(mutex)(&data->lock);
82 ret = mpl3115_request(data);
83 if (ret < 0)
84 return ret;
85
86 ret = i2c_smbus_read_i2c_block_data(data->client,
87 MPL3115_OUT_PRESS,
88 3, (u8 *) &tmp);
89 if (ret < 0)
90 return ret;
91
92 *val = be32_to_cpu(tmp) >> chan->scan_type.shift;
93 return IIO_VAL_INT;
94 }
95 case IIO_TEMP: { /* in 0.0625 celsius / LSB */
96 __be16 tmp;
97
98 guard(mutex)(&data->lock);
99 ret = mpl3115_request(data);
100 if (ret < 0)
101 return ret;
102
103 ret = i2c_smbus_read_i2c_block_data(data->client,
104 MPL3115_OUT_TEMP,
105 2, (u8 *) &tmp);
106 if (ret < 0)
107 return ret;
108
109 *val = sign_extend32(be16_to_cpu(tmp) >> chan->scan_type.shift,
110 chan->scan_type.realbits - 1);
111 return IIO_VAL_INT;
112 }
113 default:
114 return -EINVAL;
115 }
116 }
117
mpl3115_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)118 static int mpl3115_read_raw(struct iio_dev *indio_dev,
119 struct iio_chan_spec const *chan,
120 int *val, int *val2, long mask)
121 {
122 struct mpl3115_data *data = iio_priv(indio_dev);
123 int ret;
124
125 switch (mask) {
126 case IIO_CHAN_INFO_RAW:
127 if (!iio_device_claim_direct(indio_dev))
128 return -EBUSY;
129
130 ret = mpl3115_read_info_raw(data, chan, val);
131 iio_device_release_direct(indio_dev);
132 return ret;
133
134 case IIO_CHAN_INFO_SCALE:
135 switch (chan->type) {
136 case IIO_PRESSURE:
137 *val = 0;
138 *val2 = 250; /* want kilopascal */
139 return IIO_VAL_INT_PLUS_MICRO;
140 case IIO_TEMP:
141 *val = 0;
142 *val2 = 62500;
143 return IIO_VAL_INT_PLUS_MICRO;
144 default:
145 return -EINVAL;
146 }
147 }
148 return -EINVAL;
149 }
150
mpl3115_trigger_handler(int irq,void * p)151 static irqreturn_t mpl3115_trigger_handler(int irq, void *p)
152 {
153 struct iio_poll_func *pf = p;
154 struct iio_dev *indio_dev = pf->indio_dev;
155 struct mpl3115_data *data = iio_priv(indio_dev);
156 /*
157 * 32-bit channel + 16-bit channel + padding + ts
158 * Note that it is possible for only one of the first 2
159 * channels to be enabled. If that happens, the first element
160 * of the buffer may be either 16 or 32-bits. As such we cannot
161 * use a simple structure definition to express this data layout.
162 */
163 u8 buffer[16] __aligned(8);
164 int ret, pos = 0;
165
166 mutex_lock(&data->lock);
167 ret = mpl3115_request(data);
168 if (ret < 0) {
169 mutex_unlock(&data->lock);
170 goto done;
171 }
172
173 memset(buffer, 0, sizeof(buffer));
174 if (test_bit(0, indio_dev->active_scan_mask)) {
175 ret = i2c_smbus_read_i2c_block_data(data->client,
176 MPL3115_OUT_PRESS, 3, &buffer[pos]);
177 if (ret < 0) {
178 mutex_unlock(&data->lock);
179 goto done;
180 }
181 pos += 4;
182 }
183
184 if (test_bit(1, indio_dev->active_scan_mask)) {
185 ret = i2c_smbus_read_i2c_block_data(data->client,
186 MPL3115_OUT_TEMP, 2, &buffer[pos]);
187 if (ret < 0) {
188 mutex_unlock(&data->lock);
189 goto done;
190 }
191 }
192 mutex_unlock(&data->lock);
193
194 iio_push_to_buffers_with_ts(indio_dev, buffer, sizeof(buffer),
195 iio_get_time_ns(indio_dev));
196
197 done:
198 iio_trigger_notify_done(indio_dev->trig);
199 return IRQ_HANDLED;
200 }
201
202 static const struct iio_chan_spec mpl3115_channels[] = {
203 {
204 .type = IIO_PRESSURE,
205 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
206 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
207 .scan_index = 0,
208 .scan_type = {
209 .sign = 'u',
210 .realbits = 20,
211 .storagebits = 32,
212 .shift = 12,
213 .endianness = IIO_BE,
214 }
215 },
216 {
217 .type = IIO_TEMP,
218 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
219 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
220 .scan_index = 1,
221 .scan_type = {
222 .sign = 's',
223 .realbits = 12,
224 .storagebits = 16,
225 .shift = 4,
226 .endianness = IIO_BE,
227 }
228 },
229 IIO_CHAN_SOFT_TIMESTAMP(2),
230 };
231
232 static const struct iio_info mpl3115_info = {
233 .read_raw = &mpl3115_read_raw,
234 };
235
mpl3115_probe(struct i2c_client * client)236 static int mpl3115_probe(struct i2c_client *client)
237 {
238 const struct i2c_device_id *id = i2c_client_get_device_id(client);
239 struct mpl3115_data *data;
240 struct iio_dev *indio_dev;
241 int ret;
242
243 ret = i2c_smbus_read_byte_data(client, MPL3115_WHO_AM_I);
244 if (ret < 0)
245 return ret;
246 if (ret != MPL3115_DEVICE_ID)
247 return -ENODEV;
248
249 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
250 if (!indio_dev)
251 return -ENOMEM;
252
253 data = iio_priv(indio_dev);
254 data->client = client;
255 mutex_init(&data->lock);
256
257 i2c_set_clientdata(client, indio_dev);
258 indio_dev->info = &mpl3115_info;
259 indio_dev->name = id->name;
260 indio_dev->modes = INDIO_DIRECT_MODE;
261 indio_dev->channels = mpl3115_channels;
262 indio_dev->num_channels = ARRAY_SIZE(mpl3115_channels);
263
264 /* software reset, I2C transfer is aborted (fails) */
265 i2c_smbus_write_byte_data(client, MPL3115_CTRL_REG1,
266 MPL3115_CTRL_RESET);
267 msleep(50);
268
269 data->ctrl_reg1 = MPL3115_CTRL_OS_258MS;
270 ret = i2c_smbus_write_byte_data(client, MPL3115_CTRL_REG1,
271 data->ctrl_reg1);
272 if (ret < 0)
273 return ret;
274
275 ret = iio_triggered_buffer_setup(indio_dev, NULL,
276 mpl3115_trigger_handler, NULL);
277 if (ret < 0)
278 return ret;
279
280 ret = iio_device_register(indio_dev);
281 if (ret < 0)
282 goto buffer_cleanup;
283 return 0;
284
285 buffer_cleanup:
286 iio_triggered_buffer_cleanup(indio_dev);
287 return ret;
288 }
289
mpl3115_standby(struct mpl3115_data * data)290 static int mpl3115_standby(struct mpl3115_data *data)
291 {
292 return i2c_smbus_write_byte_data(data->client, MPL3115_CTRL_REG1,
293 data->ctrl_reg1 & ~MPL3115_CTRL_ACTIVE);
294 }
295
mpl3115_remove(struct i2c_client * client)296 static void mpl3115_remove(struct i2c_client *client)
297 {
298 struct iio_dev *indio_dev = i2c_get_clientdata(client);
299
300 iio_device_unregister(indio_dev);
301 iio_triggered_buffer_cleanup(indio_dev);
302 mpl3115_standby(iio_priv(indio_dev));
303 }
304
mpl3115_suspend(struct device * dev)305 static int mpl3115_suspend(struct device *dev)
306 {
307 return mpl3115_standby(iio_priv(i2c_get_clientdata(
308 to_i2c_client(dev))));
309 }
310
mpl3115_resume(struct device * dev)311 static int mpl3115_resume(struct device *dev)
312 {
313 struct mpl3115_data *data = iio_priv(i2c_get_clientdata(
314 to_i2c_client(dev)));
315
316 return i2c_smbus_write_byte_data(data->client, MPL3115_CTRL_REG1,
317 data->ctrl_reg1);
318 }
319
320 static DEFINE_SIMPLE_DEV_PM_OPS(mpl3115_pm_ops, mpl3115_suspend,
321 mpl3115_resume);
322
323 static const struct i2c_device_id mpl3115_id[] = {
324 { "mpl3115" },
325 { }
326 };
327 MODULE_DEVICE_TABLE(i2c, mpl3115_id);
328
329 static const struct of_device_id mpl3115_of_match[] = {
330 { .compatible = "fsl,mpl3115" },
331 { }
332 };
333 MODULE_DEVICE_TABLE(of, mpl3115_of_match);
334
335 static struct i2c_driver mpl3115_driver = {
336 .driver = {
337 .name = "mpl3115",
338 .of_match_table = mpl3115_of_match,
339 .pm = pm_sleep_ptr(&mpl3115_pm_ops),
340 },
341 .probe = mpl3115_probe,
342 .remove = mpl3115_remove,
343 .id_table = mpl3115_id,
344 };
345 module_i2c_driver(mpl3115_driver);
346
347 MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
348 MODULE_DESCRIPTION("Freescale MPL3115 pressure/temperature driver");
349 MODULE_LICENSE("GPL");
350