blob: 3f90985d545e9420c7a6cd2739e3bddb084897aa [file] [log] [blame]
Kyle Swenson8d8f6542021-03-15 11:02:55 -06001/*
2 * mpl115.c - Support for Freescale MPL115A2 pressure/temperature sensor
3 *
4 * Copyright (c) 2014 Peter Meerwald <pmeerw@pmeerw.net>
5 *
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.
9 *
10 * (7-bit I2C slave address 0x60)
11 *
12 * TODO: shutdown pin
13 *
14 */
15
16#include <linux/module.h>
17#include <linux/i2c.h>
18#include <linux/iio/iio.h>
19#include <linux/delay.h>
20
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 */
28
29struct mpl115_data {
30 struct i2c_client *client;
31 struct mutex lock;
32 s16 a0;
33 s16 b1, b2;
34 s16 c12;
35};
36
37static int mpl115_request(struct mpl115_data *data)
38{
39 int ret = i2c_smbus_write_byte_data(data->client, MPL115_CONVERT, 0);
40 if (ret < 0)
41 return ret;
42
43 usleep_range(3000, 4000);
44
45 return 0;
46}
47
48static int mpl115_comp_pressure(struct mpl115_data *data, int *val, int *val2)
49{
50 int ret;
51 u16 padc, tadc;
52 int a1, y1, pcomp;
53 unsigned kpa;
54
55 mutex_lock(&data->lock);
56 ret = mpl115_request(data);
57 if (ret < 0)
58 goto done;
59
60 ret = i2c_smbus_read_word_swapped(data->client, MPL115_PADC);
61 if (ret < 0)
62 goto done;
63 padc = ret >> 6;
64
65 ret = i2c_smbus_read_word_swapped(data->client, MPL115_TADC);
66 if (ret < 0)
67 goto done;
68 tadc = ret >> 6;
69
70 /* see Freescale AN3785 */
71 a1 = data->b1 + ((data->c12 * tadc) >> 11);
72 y1 = (data->a0 << 10) + a1 * padc;
73
74 /* compensated pressure with 4 fractional bits */
75 pcomp = (y1 + ((data->b2 * (int) tadc) >> 1)) >> 9;
76
77 kpa = pcomp * (115 - 50) / 1023 + (50 << 4);
78 *val = kpa >> 4;
79 *val2 = (kpa & 15) * (1000000 >> 4);
80done:
81 mutex_unlock(&data->lock);
82 return ret;
83}
84
85static int mpl115_read_temp(struct mpl115_data *data)
86{
87 int ret;
88
89 mutex_lock(&data->lock);
90 ret = mpl115_request(data);
91 if (ret < 0)
92 goto done;
93 ret = i2c_smbus_read_word_swapped(data->client, MPL115_TADC);
94done:
95 mutex_unlock(&data->lock);
96 return ret;
97}
98
99static int mpl115_read_raw(struct iio_dev *indio_dev,
100 struct iio_chan_spec const *chan,
101 int *val, int *val2, long mask)
102{
103 struct mpl115_data *data = iio_priv(indio_dev);
104 int ret;
105
106 switch (mask) {
107 case IIO_CHAN_INFO_PROCESSED:
108 ret = mpl115_comp_pressure(data, val, val2);
109 if (ret < 0)
110 return ret;
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);
115 if (ret < 0)
116 return ret;
117 *val = ret >> 6;
118 return IIO_VAL_INT;
119 case IIO_CHAN_INFO_OFFSET:
120 *val = -605;
121 *val2 = 750000;
122 return IIO_VAL_INT_PLUS_MICRO;
123 case IIO_CHAN_INFO_SCALE:
124 *val = -186;
125 *val2 = 915888;
126 return IIO_VAL_INT_PLUS_MICRO;
127 }
128 return -EINVAL;
129}
130
131static const struct iio_chan_spec mpl115_channels[] = {
132 {
133 .type = IIO_PRESSURE,
134 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
135 },
136 {
137 .type = IIO_TEMP,
138 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
139 .info_mask_shared_by_type =
140 BIT(IIO_CHAN_INFO_OFFSET) | BIT(IIO_CHAN_INFO_SCALE),
141 },
142};
143
144static const struct iio_info mpl115_info = {
145 .read_raw = &mpl115_read_raw,
146 .driver_module = THIS_MODULE,
147};
148
149static int mpl115_probe(struct i2c_client *client,
150 const struct i2c_device_id *id)
151{
152 struct mpl115_data *data;
153 struct iio_dev *indio_dev;
154 int ret;
155
156 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WORD_DATA))
157 return -ENODEV;
158
159 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
160 if (!indio_dev)
161 return -ENOMEM;
162
163 data = iio_priv(indio_dev);
164 data->client = client;
165 mutex_init(&data->lock);
166
167 i2c_set_clientdata(client, indio_dev);
168 indio_dev->info = &mpl115_info;
169 indio_dev->name = id->name;
170 indio_dev->dev.parent = &client->dev;
171 indio_dev->modes = INDIO_DIRECT_MODE;
172 indio_dev->channels = mpl115_channels;
173 indio_dev->num_channels = ARRAY_SIZE(mpl115_channels);
174
175 ret = i2c_smbus_read_word_swapped(data->client, MPL115_A0);
176 if (ret < 0)
177 return ret;
178 data->a0 = ret;
179 ret = i2c_smbus_read_word_swapped(data->client, MPL115_B1);
180 if (ret < 0)
181 return ret;
182 data->b1 = ret;
183 ret = i2c_smbus_read_word_swapped(data->client, MPL115_B2);
184 if (ret < 0)
185 return ret;
186 data->b2 = ret;
187 ret = i2c_smbus_read_word_swapped(data->client, MPL115_C12);
188 if (ret < 0)
189 return ret;
190 data->c12 = ret;
191
192 return devm_iio_device_register(&client->dev, indio_dev);
193}
194
195static const struct i2c_device_id mpl115_id[] = {
196 { "mpl115", 0 },
197 { }
198};
199MODULE_DEVICE_TABLE(i2c, mpl115_id);
200
201static struct i2c_driver mpl115_driver = {
202 .driver = {
203 .name = "mpl115",
204 },
205 .probe = mpl115_probe,
206 .id_table = mpl115_id,
207};
208module_i2c_driver(mpl115_driver);
209
210MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
211MODULE_DESCRIPTION("Freescale MPL115 pressure/temperature driver");
212MODULE_LICENSE("GPL");