1 // SPDX-License-Identifier: GPL-2.0-only
3 * mpl3115.c - Support for Freescale MPL3115A2 pressure/temperature sensor
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);
81 case IIO_CHAN_INFO_RAW:
82 ret = iio_device_claim_direct_mode(indio_dev);
87 case IIO_PRESSURE: /* in 0.25 pascal / LSB */
88 mutex_lock(&data->lock);
89 ret = mpl3115_request(data);
91 mutex_unlock(&data->lock);
94 ret = i2c_smbus_read_i2c_block_data(data->client,
95 MPL3115_OUT_PRESS, 3, (u8 *) &tmp);
96 mutex_unlock(&data->lock);
99 *val = be32_to_cpu(tmp) >> 12;
102 case IIO_TEMP: /* in 0.0625 celsius / LSB */
103 mutex_lock(&data->lock);
104 ret = mpl3115_request(data);
106 mutex_unlock(&data->lock);
109 ret = i2c_smbus_read_i2c_block_data(data->client,
110 MPL3115_OUT_TEMP, 2, (u8 *) &tmp);
111 mutex_unlock(&data->lock);
114 *val = sign_extend32(be32_to_cpu(tmp) >> 20, 11);
122 iio_device_release_direct_mode(indio_dev);
125 case IIO_CHAN_INFO_SCALE:
126 switch (chan->type) {
129 *val2 = 250; /* want kilopascal */
130 return IIO_VAL_INT_PLUS_MICRO;
134 return IIO_VAL_INT_PLUS_MICRO;
142 static irqreturn_t mpl3115_trigger_handler(int irq, void *p)
144 struct iio_poll_func *pf = p;
145 struct iio_dev *indio_dev = pf->indio_dev;
146 struct mpl3115_data *data = iio_priv(indio_dev);
147 u8 buffer[16]; /* 32-bit channel + 16-bit channel + padding + ts */
150 mutex_lock(&data->lock);
151 ret = mpl3115_request(data);
153 mutex_unlock(&data->lock);
157 memset(buffer, 0, sizeof(buffer));
158 if (test_bit(0, indio_dev->active_scan_mask)) {
159 ret = i2c_smbus_read_i2c_block_data(data->client,
160 MPL3115_OUT_PRESS, 3, &buffer[pos]);
162 mutex_unlock(&data->lock);
168 if (test_bit(1, indio_dev->active_scan_mask)) {
169 ret = i2c_smbus_read_i2c_block_data(data->client,
170 MPL3115_OUT_TEMP, 2, &buffer[pos]);
172 mutex_unlock(&data->lock);
176 mutex_unlock(&data->lock);
178 iio_push_to_buffers_with_timestamp(indio_dev, buffer,
179 iio_get_time_ns(indio_dev));
182 iio_trigger_notify_done(indio_dev->trig);
186 static const struct iio_chan_spec mpl3115_channels[] = {
188 .type = IIO_PRESSURE,
189 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
190 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
197 .endianness = IIO_BE,
202 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
203 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
210 .endianness = IIO_BE,
213 IIO_CHAN_SOFT_TIMESTAMP(2),
216 static const struct iio_info mpl3115_info = {
217 .read_raw = &mpl3115_read_raw,
220 static int mpl3115_probe(struct i2c_client *client,
221 const struct i2c_device_id *id)
223 struct mpl3115_data *data;
224 struct iio_dev *indio_dev;
227 ret = i2c_smbus_read_byte_data(client, MPL3115_WHO_AM_I);
230 if (ret != MPL3115_DEVICE_ID)
233 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
237 data = iio_priv(indio_dev);
238 data->client = client;
239 mutex_init(&data->lock);
241 i2c_set_clientdata(client, indio_dev);
242 indio_dev->info = &mpl3115_info;
243 indio_dev->name = id->name;
244 indio_dev->modes = INDIO_DIRECT_MODE;
245 indio_dev->channels = mpl3115_channels;
246 indio_dev->num_channels = ARRAY_SIZE(mpl3115_channels);
248 /* software reset, I2C transfer is aborted (fails) */
249 i2c_smbus_write_byte_data(client, MPL3115_CTRL_REG1,
253 data->ctrl_reg1 = MPL3115_CTRL_OS_258MS;
254 ret = i2c_smbus_write_byte_data(client, MPL3115_CTRL_REG1,
259 ret = iio_triggered_buffer_setup(indio_dev, NULL,
260 mpl3115_trigger_handler, NULL);
264 ret = iio_device_register(indio_dev);
270 iio_triggered_buffer_cleanup(indio_dev);
274 static int mpl3115_standby(struct mpl3115_data *data)
276 return i2c_smbus_write_byte_data(data->client, MPL3115_CTRL_REG1,
277 data->ctrl_reg1 & ~MPL3115_CTRL_ACTIVE);
280 static int mpl3115_remove(struct i2c_client *client)
282 struct iio_dev *indio_dev = i2c_get_clientdata(client);
284 iio_device_unregister(indio_dev);
285 iio_triggered_buffer_cleanup(indio_dev);
286 mpl3115_standby(iio_priv(indio_dev));
291 #ifdef CONFIG_PM_SLEEP
292 static int mpl3115_suspend(struct device *dev)
294 return mpl3115_standby(iio_priv(i2c_get_clientdata(
295 to_i2c_client(dev))));
298 static int mpl3115_resume(struct device *dev)
300 struct mpl3115_data *data = iio_priv(i2c_get_clientdata(
301 to_i2c_client(dev)));
303 return i2c_smbus_write_byte_data(data->client, MPL3115_CTRL_REG1,
307 static SIMPLE_DEV_PM_OPS(mpl3115_pm_ops, mpl3115_suspend, mpl3115_resume);
308 #define MPL3115_PM_OPS (&mpl3115_pm_ops)
310 #define MPL3115_PM_OPS NULL
313 static const struct i2c_device_id mpl3115_id[] = {
317 MODULE_DEVICE_TABLE(i2c, mpl3115_id);
319 static const struct of_device_id mpl3115_of_match[] = {
320 { .compatible = "fsl,mpl3115" },
323 MODULE_DEVICE_TABLE(of, mpl3115_of_match);
325 static struct i2c_driver mpl3115_driver = {
328 .of_match_table = mpl3115_of_match,
329 .pm = MPL3115_PM_OPS,
331 .probe = mpl3115_probe,
332 .remove = mpl3115_remove,
333 .id_table = mpl3115_id,
335 module_i2c_driver(mpl3115_driver);
338 MODULE_DESCRIPTION("Freescale MPL3115 pressure/temperature driver");
339 MODULE_LICENSE("GPL");