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
);
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
);
148 * 32-bit channel + 16-bit channel + padding + ts
149 * Note that it is possible for only one of the first 2
150 * channels to be enabled. If that happens, the first element
151 * of the buffer may be either 16 or 32-bits. As such we cannot
152 * use a simple structure definition to express this data layout.
154 u8 buffer
[16] __aligned(8);
157 mutex_lock(&data
->lock
);
158 ret
= mpl3115_request(data
);
160 mutex_unlock(&data
->lock
);
164 memset(buffer
, 0, sizeof(buffer
));
165 if (test_bit(0, indio_dev
->active_scan_mask
)) {
166 ret
= i2c_smbus_read_i2c_block_data(data
->client
,
167 MPL3115_OUT_PRESS
, 3, &buffer
[pos
]);
169 mutex_unlock(&data
->lock
);
175 if (test_bit(1, indio_dev
->active_scan_mask
)) {
176 ret
= i2c_smbus_read_i2c_block_data(data
->client
,
177 MPL3115_OUT_TEMP
, 2, &buffer
[pos
]);
179 mutex_unlock(&data
->lock
);
183 mutex_unlock(&data
->lock
);
185 iio_push_to_buffers_with_timestamp(indio_dev
, buffer
,
186 iio_get_time_ns(indio_dev
));
189 iio_trigger_notify_done(indio_dev
->trig
);
193 static const struct iio_chan_spec mpl3115_channels
[] = {
195 .type
= IIO_PRESSURE
,
196 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
197 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_SCALE
),
204 .endianness
= IIO_BE
,
209 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
210 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_SCALE
),
217 .endianness
= IIO_BE
,
220 IIO_CHAN_SOFT_TIMESTAMP(2),
223 static const struct iio_info mpl3115_info
= {
224 .read_raw
= &mpl3115_read_raw
,
227 static int mpl3115_probe(struct i2c_client
*client
,
228 const struct i2c_device_id
*id
)
230 struct mpl3115_data
*data
;
231 struct iio_dev
*indio_dev
;
234 ret
= i2c_smbus_read_byte_data(client
, MPL3115_WHO_AM_I
);
237 if (ret
!= MPL3115_DEVICE_ID
)
240 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
244 data
= iio_priv(indio_dev
);
245 data
->client
= client
;
246 mutex_init(&data
->lock
);
248 i2c_set_clientdata(client
, indio_dev
);
249 indio_dev
->info
= &mpl3115_info
;
250 indio_dev
->name
= id
->name
;
251 indio_dev
->modes
= INDIO_DIRECT_MODE
;
252 indio_dev
->channels
= mpl3115_channels
;
253 indio_dev
->num_channels
= ARRAY_SIZE(mpl3115_channels
);
255 /* software reset, I2C transfer is aborted (fails) */
256 i2c_smbus_write_byte_data(client
, MPL3115_CTRL_REG1
,
260 data
->ctrl_reg1
= MPL3115_CTRL_OS_258MS
;
261 ret
= i2c_smbus_write_byte_data(client
, MPL3115_CTRL_REG1
,
266 ret
= iio_triggered_buffer_setup(indio_dev
, NULL
,
267 mpl3115_trigger_handler
, NULL
);
271 ret
= iio_device_register(indio_dev
);
277 iio_triggered_buffer_cleanup(indio_dev
);
281 static int mpl3115_standby(struct mpl3115_data
*data
)
283 return i2c_smbus_write_byte_data(data
->client
, MPL3115_CTRL_REG1
,
284 data
->ctrl_reg1
& ~MPL3115_CTRL_ACTIVE
);
287 static int mpl3115_remove(struct i2c_client
*client
)
289 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
291 iio_device_unregister(indio_dev
);
292 iio_triggered_buffer_cleanup(indio_dev
);
293 mpl3115_standby(iio_priv(indio_dev
));
298 #ifdef CONFIG_PM_SLEEP
299 static int mpl3115_suspend(struct device
*dev
)
301 return mpl3115_standby(iio_priv(i2c_get_clientdata(
302 to_i2c_client(dev
))));
305 static int mpl3115_resume(struct device
*dev
)
307 struct mpl3115_data
*data
= iio_priv(i2c_get_clientdata(
308 to_i2c_client(dev
)));
310 return i2c_smbus_write_byte_data(data
->client
, MPL3115_CTRL_REG1
,
314 static SIMPLE_DEV_PM_OPS(mpl3115_pm_ops
, mpl3115_suspend
, mpl3115_resume
);
315 #define MPL3115_PM_OPS (&mpl3115_pm_ops)
317 #define MPL3115_PM_OPS NULL
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
= 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");