1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * mpl3115.c - Support for Freescale MPL3115A2 pressure/temperature sensor
4 *
5 * Copyright (c) 2013 Peter Meerwald <pmeerw@pmeerw.net>
6 *
7 * (7-bit I2C slave address 0x60)
8 *
9 * TODO: FIFO buffer, altimeter mode, oversampling, continuous mode,
10 * interrupts, user offset correction, raw mode
11 */
12
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>
21
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
27
28 #define MPL3115_DEVICE_ID 0xc4
29
30 #define MPL3115_STATUS_PRESS_RDY BIT(2)
31 #define MPL3115_STATUS_TEMP_RDY BIT(1)
32
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 */
37
38 struct mpl3115_data {
39 struct i2c_client *client;
40 struct mutex lock;
41 u8 ctrl_reg1;
42 };
43
mpl3115_request(struct mpl3115_data * data)44 static int mpl3115_request(struct mpl3115_data *data)
45 {
46 int ret, tries = 15;
47
48 /* trigger measurement */
49 ret = i2c_smbus_write_byte_data(data->client, MPL3115_CTRL_REG1,
50 data->ctrl_reg1 | MPL3115_CTRL_OST);
51 if (ret < 0)
52 return ret;
53
54 while (tries-- > 0) {
55 ret = i2c_smbus_read_byte_data(data->client, MPL3115_CTRL_REG1);
56 if (ret < 0)
57 return ret;
58 /* wait for data ready, i.e. OST cleared */
59 if (!(ret & MPL3115_CTRL_OST))
60 break;
61 msleep(20);
62 }
63
64 if (tries < 0) {
65 dev_err(&data->client->dev, "data not ready\n");
66 return -EIO;
67 }
68
69 return 0;
70 }
71
mpl3115_read_info_raw(struct mpl3115_data * data,struct iio_chan_spec const * chan,int * val)72 static int mpl3115_read_info_raw(struct mpl3115_data *data,
73 struct iio_chan_spec const *chan, int *val)
74 {
75 int ret;
76
77 switch (chan->type) {
78 case IIO_PRESSURE: { /* in 0.25 pascal / LSB */
79 __be32 tmp = 0;
80
81 guard(mutex)(&data->lock);
82 ret = mpl3115_request(data);
83 if (ret < 0)
84 return ret;
85
86 ret = i2c_smbus_read_i2c_block_data(data->client,
87 MPL3115_OUT_PRESS,
88 3, (u8 *) &tmp);
89 if (ret < 0)
90 return ret;
91
92 *val = be32_to_cpu(tmp) >> chan->scan_type.shift;
93 return IIO_VAL_INT;
94 }
95 case IIO_TEMP: { /* in 0.0625 celsius / LSB */
96 __be16 tmp;
97
98 guard(mutex)(&data->lock);
99 ret = mpl3115_request(data);
100 if (ret < 0)
101 return ret;
102
103 ret = i2c_smbus_read_i2c_block_data(data->client,
104 MPL3115_OUT_TEMP,
105 2, (u8 *) &tmp);
106 if (ret < 0)
107 return ret;
108
109 *val = sign_extend32(be16_to_cpu(tmp) >> chan->scan_type.shift,
110 chan->scan_type.realbits - 1);
111 return IIO_VAL_INT;
112 }
113 default:
114 return -EINVAL;
115 }
116 }
117
mpl3115_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)118 static int mpl3115_read_raw(struct iio_dev *indio_dev,
119 struct iio_chan_spec const *chan,
120 int *val, int *val2, long mask)
121 {
122 struct mpl3115_data *data = iio_priv(indio_dev);
123 int ret;
124
125 switch (mask) {
126 case IIO_CHAN_INFO_RAW:
127 if (!iio_device_claim_direct(indio_dev))
128 return -EBUSY;
129
130 ret = mpl3115_read_info_raw(data, chan, val);
131 iio_device_release_direct(indio_dev);
132 return ret;
133
134 case IIO_CHAN_INFO_SCALE:
135 switch (chan->type) {
136 case IIO_PRESSURE:
137 *val = 0;
138 *val2 = 250; /* want kilopascal */
139 return IIO_VAL_INT_PLUS_MICRO;
140 case IIO_TEMP:
141 *val = 0;
142 *val2 = 62500;
143 return IIO_VAL_INT_PLUS_MICRO;
144 default:
145 return -EINVAL;
146 }
147 }
148 return -EINVAL;
149 }
150
mpl3115_trigger_handler(int irq,void * p)151 static irqreturn_t mpl3115_trigger_handler(int irq, void *p)
152 {
153 struct iio_poll_func *pf = p;
154 struct iio_dev *indio_dev = pf->indio_dev;
155 struct mpl3115_data *data = iio_priv(indio_dev);
156 /*
157 * 32-bit channel + 16-bit channel + padding + ts
158 * Note that it is possible for only one of the first 2
159 * channels to be enabled. If that happens, the first element
160 * of the buffer may be either 16 or 32-bits. As such we cannot
161 * use a simple structure definition to express this data layout.
162 */
163 u8 buffer[16] __aligned(8) = { };
164 int ret, pos = 0;
165
166 mutex_lock(&data->lock);
167 ret = mpl3115_request(data);
168 if (ret < 0) {
169 mutex_unlock(&data->lock);
170 goto done;
171 }
172
173 if (test_bit(0, indio_dev->active_scan_mask)) {
174 ret = i2c_smbus_read_i2c_block_data(data->client,
175 MPL3115_OUT_PRESS, 3, &buffer[pos]);
176 if (ret < 0) {
177 mutex_unlock(&data->lock);
178 goto done;
179 }
180 pos += 4;
181 }
182
183 if (test_bit(1, indio_dev->active_scan_mask)) {
184 ret = i2c_smbus_read_i2c_block_data(data->client,
185 MPL3115_OUT_TEMP, 2, &buffer[pos]);
186 if (ret < 0) {
187 mutex_unlock(&data->lock);
188 goto done;
189 }
190 }
191 mutex_unlock(&data->lock);
192
193 iio_push_to_buffers_with_ts(indio_dev, buffer, sizeof(buffer),
194 iio_get_time_ns(indio_dev));
195
196 done:
197 iio_trigger_notify_done(indio_dev->trig);
198 return IRQ_HANDLED;
199 }
200
201 static const struct iio_chan_spec mpl3115_channels[] = {
202 {
203 .type = IIO_PRESSURE,
204 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
205 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
206 .scan_index = 0,
207 .scan_type = {
208 .sign = 'u',
209 .realbits = 20,
210 .storagebits = 32,
211 .shift = 12,
212 .endianness = IIO_BE,
213 }
214 },
215 {
216 .type = IIO_TEMP,
217 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
218 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
219 .scan_index = 1,
220 .scan_type = {
221 .sign = 's',
222 .realbits = 12,
223 .storagebits = 16,
224 .shift = 4,
225 .endianness = IIO_BE,
226 }
227 },
228 IIO_CHAN_SOFT_TIMESTAMP(2),
229 };
230
231 static const struct iio_info mpl3115_info = {
232 .read_raw = &mpl3115_read_raw,
233 };
234
mpl3115_probe(struct i2c_client * client)235 static int mpl3115_probe(struct i2c_client *client)
236 {
237 const struct i2c_device_id *id = i2c_client_get_device_id(client);
238 struct mpl3115_data *data;
239 struct iio_dev *indio_dev;
240 int ret;
241
242 ret = i2c_smbus_read_byte_data(client, MPL3115_WHO_AM_I);
243 if (ret < 0)
244 return ret;
245 if (ret != MPL3115_DEVICE_ID)
246 return -ENODEV;
247
248 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
249 if (!indio_dev)
250 return -ENOMEM;
251
252 data = iio_priv(indio_dev);
253 data->client = client;
254 mutex_init(&data->lock);
255
256 i2c_set_clientdata(client, indio_dev);
257 indio_dev->info = &mpl3115_info;
258 indio_dev->name = id->name;
259 indio_dev->modes = INDIO_DIRECT_MODE;
260 indio_dev->channels = mpl3115_channels;
261 indio_dev->num_channels = ARRAY_SIZE(mpl3115_channels);
262
263 /* software reset, I2C transfer is aborted (fails) */
264 i2c_smbus_write_byte_data(client, MPL3115_CTRL_REG1,
265 MPL3115_CTRL_RESET);
266 msleep(50);
267
268 data->ctrl_reg1 = MPL3115_CTRL_OS_258MS;
269 ret = i2c_smbus_write_byte_data(client, MPL3115_CTRL_REG1,
270 data->ctrl_reg1);
271 if (ret < 0)
272 return ret;
273
274 ret = iio_triggered_buffer_setup(indio_dev, NULL,
275 mpl3115_trigger_handler, NULL);
276 if (ret < 0)
277 return ret;
278
279 ret = iio_device_register(indio_dev);
280 if (ret < 0)
281 goto buffer_cleanup;
282 return 0;
283
284 buffer_cleanup:
285 iio_triggered_buffer_cleanup(indio_dev);
286 return ret;
287 }
288
mpl3115_standby(struct mpl3115_data * data)289 static int mpl3115_standby(struct mpl3115_data *data)
290 {
291 return i2c_smbus_write_byte_data(data->client, MPL3115_CTRL_REG1,
292 data->ctrl_reg1 & ~MPL3115_CTRL_ACTIVE);
293 }
294
mpl3115_remove(struct i2c_client * client)295 static void mpl3115_remove(struct i2c_client *client)
296 {
297 struct iio_dev *indio_dev = i2c_get_clientdata(client);
298
299 iio_device_unregister(indio_dev);
300 iio_triggered_buffer_cleanup(indio_dev);
301 mpl3115_standby(iio_priv(indio_dev));
302 }
303
mpl3115_suspend(struct device * dev)304 static int mpl3115_suspend(struct device *dev)
305 {
306 return mpl3115_standby(iio_priv(i2c_get_clientdata(
307 to_i2c_client(dev))));
308 }
309
mpl3115_resume(struct device * dev)310 static int mpl3115_resume(struct device *dev)
311 {
312 struct mpl3115_data *data = iio_priv(i2c_get_clientdata(
313 to_i2c_client(dev)));
314
315 return i2c_smbus_write_byte_data(data->client, MPL3115_CTRL_REG1,
316 data->ctrl_reg1);
317 }
318
319 static DEFINE_SIMPLE_DEV_PM_OPS(mpl3115_pm_ops, mpl3115_suspend,
320 mpl3115_resume);
321
322 static const struct i2c_device_id mpl3115_id[] = {
323 { "mpl3115" },
324 { }
325 };
326 MODULE_DEVICE_TABLE(i2c, mpl3115_id);
327
328 static const struct of_device_id mpl3115_of_match[] = {
329 { .compatible = "fsl,mpl3115" },
330 { }
331 };
332 MODULE_DEVICE_TABLE(of, mpl3115_of_match);
333
334 static struct i2c_driver mpl3115_driver = {
335 .driver = {
336 .name = "mpl3115",
337 .of_match_table = mpl3115_of_match,
338 .pm = pm_sleep_ptr(&mpl3115_pm_ops),
339 },
340 .probe = mpl3115_probe,
341 .remove = mpl3115_remove,
342 .id_table = mpl3115_id,
343 };
344 module_i2c_driver(mpl3115_driver);
345
346 MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
347 MODULE_DESCRIPTION("Freescale MPL3115 pressure/temperature driver");
348 MODULE_LICENSE("GPL");
349