2 * mpl115.c - Support for Freescale MPL115A 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.
14 #include <linux/module.h>
15 #include <linux/iio/iio.h>
16 #include <linux/delay.h>
20 #define MPL115_PADC 0x00 /* pressure ADC output value, MSB first, 10 bit */
21 #define MPL115_TADC 0x02 /* temperature ADC output value, MSB first, 10 bit */
22 #define MPL115_A0 0x04 /* 12 bit integer, 3 bit fraction */
23 #define MPL115_B1 0x06 /* 2 bit integer, 13 bit fraction */
24 #define MPL115_B2 0x08 /* 1 bit integer, 14 bit fraction */
25 #define MPL115_C12 0x0a /* 0 bit integer, 13 bit fraction */
26 #define MPL115_CONVERT 0x12 /* convert temperature and pressure */
34 const struct mpl115_ops
*ops
;
37 static int mpl115_request(struct mpl115_data
*data
)
39 int ret
= data
->ops
->write(data
->dev
, MPL115_CONVERT
, 0);
44 usleep_range(3000, 4000);
49 static int mpl115_comp_pressure(struct mpl115_data
*data
, int *val
, int *val2
)
56 mutex_lock(&data
->lock
);
57 ret
= mpl115_request(data
);
61 ret
= data
->ops
->read(data
->dev
, MPL115_PADC
);
66 ret
= data
->ops
->read(data
->dev
, MPL115_TADC
);
71 /* see Freescale AN3785 */
72 a1
= data
->b1
+ ((data
->c12
* tadc
) >> 11);
73 y1
= (data
->a0
<< 10) + a1
* padc
;
75 /* compensated pressure with 4 fractional bits */
76 pcomp
= (y1
+ ((data
->b2
* (int) tadc
) >> 1)) >> 9;
78 kpa
= pcomp
* (115 - 50) / 1023 + (50 << 4);
80 *val2
= (kpa
& 15) * (1000000 >> 4);
82 mutex_unlock(&data
->lock
);
86 static int mpl115_read_temp(struct mpl115_data
*data
)
90 mutex_lock(&data
->lock
);
91 ret
= mpl115_request(data
);
94 ret
= data
->ops
->read(data
->dev
, MPL115_TADC
);
96 mutex_unlock(&data
->lock
);
100 static int mpl115_read_raw(struct iio_dev
*indio_dev
,
101 struct iio_chan_spec
const *chan
,
102 int *val
, int *val2
, long mask
)
104 struct mpl115_data
*data
= iio_priv(indio_dev
);
108 case IIO_CHAN_INFO_PROCESSED
:
109 ret
= mpl115_comp_pressure(data
, val
, val2
);
112 return IIO_VAL_INT_PLUS_MICRO
;
113 case IIO_CHAN_INFO_RAW
:
114 /* temperature -5.35 C / LSB, 472 LSB is 25 C */
115 ret
= mpl115_read_temp(data
);
120 case IIO_CHAN_INFO_OFFSET
:
123 return IIO_VAL_INT_PLUS_MICRO
;
124 case IIO_CHAN_INFO_SCALE
:
127 return IIO_VAL_INT_PLUS_MICRO
;
132 static const struct iio_chan_spec mpl115_channels
[] = {
134 .type
= IIO_PRESSURE
,
135 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
),
139 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
140 .info_mask_shared_by_type
=
141 BIT(IIO_CHAN_INFO_OFFSET
) | BIT(IIO_CHAN_INFO_SCALE
),
145 static const struct iio_info mpl115_info
= {
146 .read_raw
= &mpl115_read_raw
,
147 .driver_module
= THIS_MODULE
,
150 int mpl115_probe(struct device
*dev
, const char *name
,
151 const struct mpl115_ops
*ops
)
153 struct mpl115_data
*data
;
154 struct iio_dev
*indio_dev
;
157 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*data
));
161 data
= iio_priv(indio_dev
);
164 mutex_init(&data
->lock
);
166 indio_dev
->info
= &mpl115_info
;
167 indio_dev
->name
= name
;
168 indio_dev
->dev
.parent
= dev
;
169 indio_dev
->modes
= INDIO_DIRECT_MODE
;
170 indio_dev
->channels
= mpl115_channels
;
171 indio_dev
->num_channels
= ARRAY_SIZE(mpl115_channels
);
173 ret
= data
->ops
->init(data
->dev
);
177 ret
= data
->ops
->read(data
->dev
, MPL115_A0
);
181 ret
= data
->ops
->read(data
->dev
, MPL115_B1
);
185 ret
= data
->ops
->read(data
->dev
, MPL115_B2
);
189 ret
= data
->ops
->read(data
->dev
, MPL115_C12
);
194 return devm_iio_device_register(dev
, indio_dev
);
196 EXPORT_SYMBOL_GPL(mpl115_probe
);
198 MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
199 MODULE_DESCRIPTION("Freescale MPL115 pressure/temperature driver");
200 MODULE_LICENSE("GPL");