1 // SPDX-License-Identifier: GPL-2.0-only
3 * mpl3115.c - Support for Freescale MPL3115A2 pressure/temperature sensor
5 * Copyright (c) 2013 Peter Meerwald <pmeerw@pmeerw.net>
7 * (7-bit I2C slave address 0x60)
9 * TODO: FIFO buffer, altimeter mode, oversampling, continuous mode,
10 * interrupts, user offset correction, raw mode
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>
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
28 #define MPL3115_DEVICE_ID 0xc4
30 #define MPL3115_STATUS_PRESS_RDY BIT(2)
31 #define MPL3115_STATUS_TEMP_RDY BIT(1)
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 */
39 struct i2c_client *client;
44 static int mpl3115_request(struct mpl3115_data *data)
48 /* trigger measurement */
49 ret = i2c_smbus_write_byte_data(data->client, MPL3115_CTRL_REG1,
50 data->ctrl_reg1 | MPL3115_CTRL_OST);
55 ret = i2c_smbus_read_byte_data(data->client, MPL3115_CTRL_REG1);
58 /* wait for data ready, i.e. OST cleared */
59 if (!(ret & MPL3115_CTRL_OST))
65 dev_err(&data->client->dev, "data not ready\n");
72 static int mpl3115_read_raw(struct iio_dev *indio_dev,
73 struct iio_chan_spec const *chan,
74 int *val, int *val2, long mask)
76 struct mpl3115_data *data = iio_priv(indio_dev);
80 case IIO_CHAN_INFO_RAW:
81 ret = iio_device_claim_direct_mode(indio_dev);
86 case IIO_PRESSURE: { /* in 0.25 pascal / LSB */
89 mutex_lock(&data->lock);
90 ret = mpl3115_request(data);
92 mutex_unlock(&data->lock);
95 ret = i2c_smbus_read_i2c_block_data(data->client,
96 MPL3115_OUT_PRESS, 3, (u8 *) &tmp);
97 mutex_unlock(&data->lock);
100 *val = be32_to_cpu(tmp) >> chan->scan_type.shift;
104 case IIO_TEMP: { /* in 0.0625 celsius / LSB */
107 mutex_lock(&data->lock);
108 ret = mpl3115_request(data);
110 mutex_unlock(&data->lock);
113 ret = i2c_smbus_read_i2c_block_data(data->client,
114 MPL3115_OUT_TEMP, 2, (u8 *) &tmp);
115 mutex_unlock(&data->lock);
118 *val = sign_extend32(be16_to_cpu(tmp) >> chan->scan_type.shift,
119 chan->scan_type.realbits - 1);
128 iio_device_release_direct_mode(indio_dev);
131 case IIO_CHAN_INFO_SCALE:
132 switch (chan->type) {
135 *val2 = 250; /* want kilopascal */
136 return IIO_VAL_INT_PLUS_MICRO;
140 return IIO_VAL_INT_PLUS_MICRO;
148 static irqreturn_t mpl3115_trigger_handler(int irq, void *p)
150 struct iio_poll_func *pf = p;
151 struct iio_dev *indio_dev = pf->indio_dev;
152 struct mpl3115_data *data = iio_priv(indio_dev);
154 * 32-bit channel + 16-bit channel + padding + ts
155 * Note that it is possible for only one of the first 2
156 * channels to be enabled. If that happens, the first element
157 * of the buffer may be either 16 or 32-bits. As such we cannot
158 * use a simple structure definition to express this data layout.
160 u8 buffer[16] __aligned(8);
163 mutex_lock(&data->lock);
164 ret = mpl3115_request(data);
166 mutex_unlock(&data->lock);
170 memset(buffer, 0, sizeof(buffer));
171 if (test_bit(0, indio_dev->active_scan_mask)) {
172 ret = i2c_smbus_read_i2c_block_data(data->client,
173 MPL3115_OUT_PRESS, 3, &buffer[pos]);
175 mutex_unlock(&data->lock);
181 if (test_bit(1, indio_dev->active_scan_mask)) {
182 ret = i2c_smbus_read_i2c_block_data(data->client,
183 MPL3115_OUT_TEMP, 2, &buffer[pos]);
185 mutex_unlock(&data->lock);
189 mutex_unlock(&data->lock);
191 iio_push_to_buffers_with_timestamp(indio_dev, buffer,
192 iio_get_time_ns(indio_dev));
195 iio_trigger_notify_done(indio_dev->trig);
199 static const struct iio_chan_spec mpl3115_channels[] = {
201 .type = IIO_PRESSURE,
202 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
203 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
210 .endianness = IIO_BE,
215 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
216 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
223 .endianness = IIO_BE,
226 IIO_CHAN_SOFT_TIMESTAMP(2),
229 static const struct iio_info mpl3115_info = {
230 .read_raw = &mpl3115_read_raw,
233 static int mpl3115_probe(struct i2c_client *client)
235 const struct i2c_device_id *id = i2c_client_get_device_id(client);
236 struct mpl3115_data *data;
237 struct iio_dev *indio_dev;
240 ret = i2c_smbus_read_byte_data(client, MPL3115_WHO_AM_I);
243 if (ret != MPL3115_DEVICE_ID)
246 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
250 data = iio_priv(indio_dev);
251 data->client = client;
252 mutex_init(&data->lock);
254 i2c_set_clientdata(client, indio_dev);
255 indio_dev->info = &mpl3115_info;
256 indio_dev->name = id->name;
257 indio_dev->modes = INDIO_DIRECT_MODE;
258 indio_dev->channels = mpl3115_channels;
259 indio_dev->num_channels = ARRAY_SIZE(mpl3115_channels);
261 /* software reset, I2C transfer is aborted (fails) */
262 i2c_smbus_write_byte_data(client, MPL3115_CTRL_REG1,
266 data->ctrl_reg1 = MPL3115_CTRL_OS_258MS;
267 ret = i2c_smbus_write_byte_data(client, MPL3115_CTRL_REG1,
272 ret = iio_triggered_buffer_setup(indio_dev, NULL,
273 mpl3115_trigger_handler, NULL);
277 ret = iio_device_register(indio_dev);
283 iio_triggered_buffer_cleanup(indio_dev);
287 static int mpl3115_standby(struct mpl3115_data *data)
289 return i2c_smbus_write_byte_data(data->client, MPL3115_CTRL_REG1,
290 data->ctrl_reg1 & ~MPL3115_CTRL_ACTIVE);
293 static void mpl3115_remove(struct i2c_client *client)
295 struct iio_dev *indio_dev = i2c_get_clientdata(client);
297 iio_device_unregister(indio_dev);
298 iio_triggered_buffer_cleanup(indio_dev);
299 mpl3115_standby(iio_priv(indio_dev));
302 static int mpl3115_suspend(struct device *dev)
304 return mpl3115_standby(iio_priv(i2c_get_clientdata(
305 to_i2c_client(dev))));
308 static int mpl3115_resume(struct device *dev)
310 struct mpl3115_data *data = iio_priv(i2c_get_clientdata(
311 to_i2c_client(dev)));
313 return i2c_smbus_write_byte_data(data->client, MPL3115_CTRL_REG1,
317 static DEFINE_SIMPLE_DEV_PM_OPS(mpl3115_pm_ops, mpl3115_suspend,
320 static const struct i2c_device_id mpl3115_id[] = {
324 MODULE_DEVICE_TABLE(i2c, mpl3115_id);
326 static const struct of_device_id mpl3115_of_match[] = {
327 { .compatible = "fsl,mpl3115" },
330 MODULE_DEVICE_TABLE(of, mpl3115_of_match);
332 static struct i2c_driver mpl3115_driver = {
335 .of_match_table = mpl3115_of_match,
336 .pm = pm_sleep_ptr(&mpl3115_pm_ops),
338 .probe = mpl3115_probe,
339 .remove = mpl3115_remove,
340 .id_table = mpl3115_id,
342 module_i2c_driver(mpl3115_driver);
344 MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
345 MODULE_DESCRIPTION("Freescale MPL3115 pressure/temperature driver");
346 MODULE_LICENSE("GPL");