2 * mpl115.c - Support for Freescale MPL115A2 pressure/temperature sensor
4 * Copyright (c) 2014 Peter Meerwald <pmeerw@pmeerw.net>
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.
10 * (7-bit I2C slave address 0x60)
16 #include <linux/module.h>
17 #include <linux/i2c.h>
18 #include <linux/iio/iio.h>
19 #include <linux/delay.h>
21 #define MPL115_PADC 0x00 /* pressure ADC output value, MSB first, 10 bit */
22 #define MPL115_TADC 0x02 /* temperature ADC output value, MSB first, 10 bit */
23 #define MPL115_A0 0x04 /* 12 bit integer, 3 bit fraction */
24 #define MPL115_B1 0x06 /* 2 bit integer, 13 bit fraction */
25 #define MPL115_B2 0x08 /* 1 bit integer, 14 bit fraction */
26 #define MPL115_C12 0x0a /* 0 bit integer, 13 bit fraction */
27 #define MPL115_CONVERT 0x12 /* convert temperature and pressure */
30 struct i2c_client
*client
;
37 static int mpl115_request(struct mpl115_data
*data
)
39 int ret
= i2c_smbus_write_byte_data(data
->client
, MPL115_CONVERT
, 0);
43 usleep_range(3000, 4000);
48 static int mpl115_comp_pressure(struct mpl115_data
*data
, int *val
, int *val2
)
55 mutex_lock(&data
->lock
);
56 ret
= mpl115_request(data
);
60 ret
= i2c_smbus_read_word_swapped(data
->client
, MPL115_PADC
);
65 ret
= i2c_smbus_read_word_swapped(data
->client
, MPL115_TADC
);
70 /* see Freescale AN3785 */
71 a1
= data
->b1
+ ((data
->c12
* tadc
) >> 11);
72 y1
= (data
->a0
<< 10) + a1
* padc
;
74 /* compensated pressure with 4 fractional bits */
75 pcomp
= (y1
+ ((data
->b2
* (int) tadc
) >> 1)) >> 9;
77 kpa
= pcomp
* (115 - 50) / 1023 + (50 << 4);
79 *val2
= (kpa
& 15) * (1000000 >> 4);
81 mutex_unlock(&data
->lock
);
85 static int mpl115_read_temp(struct mpl115_data
*data
)
89 mutex_lock(&data
->lock
);
90 ret
= mpl115_request(data
);
93 ret
= i2c_smbus_read_word_swapped(data
->client
, MPL115_TADC
);
95 mutex_unlock(&data
->lock
);
99 static int mpl115_read_raw(struct iio_dev
*indio_dev
,
100 struct iio_chan_spec
const *chan
,
101 int *val
, int *val2
, long mask
)
103 struct mpl115_data
*data
= iio_priv(indio_dev
);
107 case IIO_CHAN_INFO_PROCESSED
:
108 ret
= mpl115_comp_pressure(data
, val
, val2
);
111 return IIO_VAL_INT_PLUS_MICRO
;
112 case IIO_CHAN_INFO_RAW
:
113 /* temperature -5.35 C / LSB, 472 LSB is 25 C */
114 ret
= mpl115_read_temp(data
);
119 case IIO_CHAN_INFO_OFFSET
:
122 return IIO_VAL_INT_PLUS_MICRO
;
123 case IIO_CHAN_INFO_SCALE
:
126 return IIO_VAL_INT_PLUS_MICRO
;
131 static const struct iio_chan_spec mpl115_channels
[] = {
133 .type
= IIO_PRESSURE
,
134 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
),
138 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
139 BIT(IIO_CHAN_INFO_OFFSET
) | BIT(IIO_CHAN_INFO_SCALE
),
143 static const struct iio_info mpl115_info
= {
144 .read_raw
= &mpl115_read_raw
,
145 .driver_module
= THIS_MODULE
,
148 static int mpl115_probe(struct i2c_client
*client
,
149 const struct i2c_device_id
*id
)
151 struct mpl115_data
*data
;
152 struct iio_dev
*indio_dev
;
155 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_SMBUS_WORD_DATA
))
158 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
162 data
= iio_priv(indio_dev
);
163 data
->client
= client
;
164 mutex_init(&data
->lock
);
166 i2c_set_clientdata(client
, indio_dev
);
167 indio_dev
->info
= &mpl115_info
;
168 indio_dev
->name
= id
->name
;
169 indio_dev
->dev
.parent
= &client
->dev
;
170 indio_dev
->modes
= INDIO_DIRECT_MODE
;
171 indio_dev
->channels
= mpl115_channels
;
172 indio_dev
->num_channels
= ARRAY_SIZE(mpl115_channels
);
174 ret
= i2c_smbus_read_word_swapped(data
->client
, MPL115_A0
);
178 ret
= i2c_smbus_read_word_swapped(data
->client
, MPL115_B1
);
182 ret
= i2c_smbus_read_word_swapped(data
->client
, MPL115_B2
);
186 ret
= i2c_smbus_read_word_swapped(data
->client
, MPL115_C12
);
191 return devm_iio_device_register(&client
->dev
, indio_dev
);
194 static const struct i2c_device_id mpl115_id
[] = {
198 MODULE_DEVICE_TABLE(i2c
, mpl115_id
);
200 static struct i2c_driver mpl115_driver
= {
204 .probe
= mpl115_probe
,
205 .id_table
= mpl115_id
,
207 module_i2c_driver(mpl115_driver
);
209 MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
210 MODULE_DESCRIPTION("Freescale MPL115 pressure/temperature driver");
211 MODULE_LICENSE("GPL");