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