blob: 0123347bed582ac973a095267410d872545e472b [file] [log] [blame]
Matt Ranostayb3c590c2017-02-04 02:22:51 -08001/*
2 * max30102.c - Support for MAX30102 heart rate and pulse oximeter sensor
3 *
4 * Copyright (C) 2017 Matt Ranostay <matt@ranostay.consulting>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * TODO: proximity power saving feature
17 */
18
19#include <linux/module.h>
20#include <linux/init.h>
21#include <linux/interrupt.h>
22#include <linux/delay.h>
23#include <linux/err.h>
24#include <linux/irq.h>
25#include <linux/i2c.h>
26#include <linux/mutex.h>
27#include <linux/of.h>
28#include <linux/regmap.h>
29#include <linux/iio/iio.h>
30#include <linux/iio/buffer.h>
31#include <linux/iio/kfifo_buf.h>
32
33#define MAX30102_REGMAP_NAME "max30102_regmap"
34#define MAX30102_DRV_NAME "max30102"
Peter Meerwald-Stadler84b0ce02017-10-27 21:45:36 +020035#define MAX30102_PART_NUMBER 0x15
Matt Ranostayb3c590c2017-02-04 02:22:51 -080036
37#define MAX30102_REG_INT_STATUS 0x00
38#define MAX30102_REG_INT_STATUS_PWR_RDY BIT(0)
39#define MAX30102_REG_INT_STATUS_PROX_INT BIT(4)
40#define MAX30102_REG_INT_STATUS_ALC_OVF BIT(5)
41#define MAX30102_REG_INT_STATUS_PPG_RDY BIT(6)
42#define MAX30102_REG_INT_STATUS_FIFO_RDY BIT(7)
43
44#define MAX30102_REG_INT_ENABLE 0x02
45#define MAX30102_REG_INT_ENABLE_PROX_INT_EN BIT(4)
46#define MAX30102_REG_INT_ENABLE_ALC_OVF_EN BIT(5)
47#define MAX30102_REG_INT_ENABLE_PPG_EN BIT(6)
48#define MAX30102_REG_INT_ENABLE_FIFO_EN BIT(7)
49#define MAX30102_REG_INT_ENABLE_MASK 0xf0
50#define MAX30102_REG_INT_ENABLE_MASK_SHIFT 4
51
52#define MAX30102_REG_FIFO_WR_PTR 0x04
53#define MAX30102_REG_FIFO_OVR_CTR 0x05
54#define MAX30102_REG_FIFO_RD_PTR 0x06
55#define MAX30102_REG_FIFO_DATA 0x07
56#define MAX30102_REG_FIFO_DATA_ENTRY_LEN 6
57
58#define MAX30102_REG_FIFO_CONFIG 0x08
59#define MAX30102_REG_FIFO_CONFIG_AVG_4SAMPLES BIT(1)
60#define MAX30102_REG_FIFO_CONFIG_AVG_SHIFT 5
61#define MAX30102_REG_FIFO_CONFIG_AFULL BIT(0)
62
63#define MAX30102_REG_MODE_CONFIG 0x09
Peter Meerwald-Stadler7b0b0ec2017-10-27 21:45:34 +020064#define MAX30102_REG_MODE_CONFIG_MODE_HR 0x02 /* red LED */
65#define MAX30102_REG_MODE_CONFIG_MODE_HR_SPO2 0x03 /* red + IR LED */
66#define MAX30102_REG_MODE_CONFIG_MODE_MULTI 0x07 /* multi-LED mode */
67#define MAX30102_REG_MODE_CONFIG_MODE_MASK GENMASK(2, 0)
Matt Ranostayb3c590c2017-02-04 02:22:51 -080068#define MAX30102_REG_MODE_CONFIG_PWR BIT(7)
69
70#define MAX30102_REG_SPO2_CONFIG 0x0a
71#define MAX30102_REG_SPO2_CONFIG_PULSE_411_US 0x03
72#define MAX30102_REG_SPO2_CONFIG_SR_400HZ 0x03
73#define MAX30102_REG_SPO2_CONFIG_SR_MASK 0x07
74#define MAX30102_REG_SPO2_CONFIG_SR_MASK_SHIFT 2
75#define MAX30102_REG_SPO2_CONFIG_ADC_4096_STEPS BIT(0)
76#define MAX30102_REG_SPO2_CONFIG_ADC_MASK_SHIFT 5
77
78#define MAX30102_REG_RED_LED_CONFIG 0x0c
79#define MAX30102_REG_IR_LED_CONFIG 0x0d
80
81#define MAX30102_REG_TEMP_CONFIG 0x21
82#define MAX30102_REG_TEMP_CONFIG_TEMP_EN BIT(0)
83
84#define MAX30102_REG_TEMP_INTEGER 0x1f
85#define MAX30102_REG_TEMP_FRACTION 0x20
86
Peter Meerwald-Stadler84b0ce02017-10-27 21:45:36 +020087#define MAX30102_REG_REV_ID 0xfe
88#define MAX30102_REG_PART_ID 0xff
89
Matt Ranostayb3c590c2017-02-04 02:22:51 -080090struct max30102_data {
91 struct i2c_client *client;
92 struct iio_dev *indio_dev;
93 struct mutex lock;
94 struct regmap *regmap;
95
96 u8 buffer[8];
97 __be32 processed_buffer[2]; /* 2 x 18-bit (padded to 32-bits) */
98};
99
100static const struct regmap_config max30102_regmap_config = {
101 .name = MAX30102_REGMAP_NAME,
102
103 .reg_bits = 8,
104 .val_bits = 8,
105};
106
107static const unsigned long max30102_scan_masks[] = {0x3, 0};
108
109static const struct iio_chan_spec max30102_channels[] = {
110 {
111 .type = IIO_INTENSITY,
112 .channel2 = IIO_MOD_LIGHT_RED,
113 .modified = 1,
114
115 .scan_index = 0,
116 .scan_type = {
117 .sign = 'u',
118 .shift = 8,
119 .realbits = 18,
120 .storagebits = 32,
121 .endianness = IIO_BE,
122 },
123 },
124 {
125 .type = IIO_INTENSITY,
126 .channel2 = IIO_MOD_LIGHT_IR,
127 .modified = 1,
128
129 .scan_index = 1,
130 .scan_type = {
131 .sign = 'u',
132 .shift = 8,
133 .realbits = 18,
134 .storagebits = 32,
135 .endianness = IIO_BE,
136 },
137 },
138 {
139 .type = IIO_TEMP,
140 .info_mask_separate =
141 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
142 .scan_index = -1,
143 },
144};
145
146static int max30102_set_powermode(struct max30102_data *data, bool state)
147{
148 return regmap_update_bits(data->regmap, MAX30102_REG_MODE_CONFIG,
149 MAX30102_REG_MODE_CONFIG_PWR,
150 state ? 0 : MAX30102_REG_MODE_CONFIG_PWR);
151}
152
153static int max30102_buffer_postenable(struct iio_dev *indio_dev)
154{
155 struct max30102_data *data = iio_priv(indio_dev);
156
157 return max30102_set_powermode(data, true);
158}
159
160static int max30102_buffer_predisable(struct iio_dev *indio_dev)
161{
162 struct max30102_data *data = iio_priv(indio_dev);
163
164 return max30102_set_powermode(data, false);
165}
166
167static const struct iio_buffer_setup_ops max30102_buffer_setup_ops = {
168 .postenable = max30102_buffer_postenable,
169 .predisable = max30102_buffer_predisable,
170};
171
172static inline int max30102_fifo_count(struct max30102_data *data)
173{
174 unsigned int val;
175 int ret;
176
177 ret = regmap_read(data->regmap, MAX30102_REG_INT_STATUS, &val);
178 if (ret)
179 return ret;
180
181 /* FIFO has one sample slot left */
182 if (val & MAX30102_REG_INT_STATUS_FIFO_RDY)
183 return 1;
184
185 return 0;
186}
187
188static int max30102_read_measurement(struct max30102_data *data)
189{
190 int ret;
191 u8 *buffer = (u8 *) &data->buffer;
192
193 ret = i2c_smbus_read_i2c_block_data(data->client,
194 MAX30102_REG_FIFO_DATA,
195 MAX30102_REG_FIFO_DATA_ENTRY_LEN,
196 buffer);
197
198 memcpy(&data->processed_buffer[0], &buffer[0], 3);
199 memcpy(&data->processed_buffer[1], &buffer[3], 3);
200
201 return (ret == MAX30102_REG_FIFO_DATA_ENTRY_LEN) ? 0 : -EINVAL;
202}
203
204static irqreturn_t max30102_interrupt_handler(int irq, void *private)
205{
206 struct iio_dev *indio_dev = private;
207 struct max30102_data *data = iio_priv(indio_dev);
208 int ret, cnt = 0;
209
210 mutex_lock(&data->lock);
211
212 while (cnt || (cnt = max30102_fifo_count(data)) > 0) {
213 ret = max30102_read_measurement(data);
214 if (ret)
215 break;
216
217 iio_push_to_buffers(data->indio_dev, data->processed_buffer);
218 cnt--;
219 }
220
221 mutex_unlock(&data->lock);
222
223 return IRQ_HANDLED;
224}
225
226static int max30102_get_current_idx(unsigned int val, int *reg)
227{
228 /* each step is 0.200 mA */
229 *reg = val / 200;
230
231 return *reg > 0xff ? -EINVAL : 0;
232}
233
234static int max30102_led_init(struct max30102_data *data)
235{
236 struct device *dev = &data->client->dev;
237 struct device_node *np = dev->of_node;
238 unsigned int val;
239 int reg, ret;
240
241 ret = of_property_read_u32(np, "maxim,red-led-current-microamp", &val);
242 if (ret) {
243 dev_info(dev, "no red-led-current-microamp set\n");
244
245 /* Default to 7 mA RED LED */
246 val = 7000;
247 }
248
249 ret = max30102_get_current_idx(val, &reg);
250 if (ret) {
251 dev_err(dev, "invalid RED LED current setting %d\n", val);
252 return ret;
253 }
254
255 ret = regmap_write(data->regmap, MAX30102_REG_RED_LED_CONFIG, reg);
256 if (ret)
257 return ret;
258
259 ret = of_property_read_u32(np, "maxim,ir-led-current-microamp", &val);
260 if (ret) {
261 dev_info(dev, "no ir-led-current-microamp set\n");
262
263 /* Default to 7 mA IR LED */
264 val = 7000;
265 }
266
267 ret = max30102_get_current_idx(val, &reg);
268 if (ret) {
Peter Meerwald-Stadlerdd86dbf2017-10-27 21:45:32 +0200269 dev_err(dev, "invalid IR LED current setting %d\n", val);
Matt Ranostayb3c590c2017-02-04 02:22:51 -0800270 return ret;
271 }
272
273 return regmap_write(data->regmap, MAX30102_REG_IR_LED_CONFIG, reg);
274}
275
276static int max30102_chip_init(struct max30102_data *data)
277{
278 int ret;
279
280 /* setup LED current settings */
281 ret = max30102_led_init(data);
282 if (ret)
283 return ret;
284
285 /* enable 18-bit HR + SPO2 readings at 400Hz */
286 ret = regmap_write(data->regmap, MAX30102_REG_SPO2_CONFIG,
287 (MAX30102_REG_SPO2_CONFIG_ADC_4096_STEPS
288 << MAX30102_REG_SPO2_CONFIG_ADC_MASK_SHIFT) |
289 (MAX30102_REG_SPO2_CONFIG_SR_400HZ
290 << MAX30102_REG_SPO2_CONFIG_SR_MASK_SHIFT) |
291 MAX30102_REG_SPO2_CONFIG_PULSE_411_US);
292 if (ret)
293 return ret;
294
Peter Meerwald-Stadler7b0b0ec2017-10-27 21:45:34 +0200295 /* enable HR + SPO2 mode */
Matt Ranostayb3c590c2017-02-04 02:22:51 -0800296 ret = regmap_update_bits(data->regmap, MAX30102_REG_MODE_CONFIG,
297 MAX30102_REG_MODE_CONFIG_MODE_MASK,
Peter Meerwald-Stadler7b0b0ec2017-10-27 21:45:34 +0200298 MAX30102_REG_MODE_CONFIG_MODE_HR_SPO2);
Matt Ranostayb3c590c2017-02-04 02:22:51 -0800299 if (ret)
300 return ret;
301
302 /* average 4 samples + generate FIFO interrupt */
303 ret = regmap_write(data->regmap, MAX30102_REG_FIFO_CONFIG,
304 (MAX30102_REG_FIFO_CONFIG_AVG_4SAMPLES
305 << MAX30102_REG_FIFO_CONFIG_AVG_SHIFT) |
306 MAX30102_REG_FIFO_CONFIG_AFULL);
307 if (ret)
308 return ret;
309
310 /* enable FIFO interrupt */
311 return regmap_update_bits(data->regmap, MAX30102_REG_INT_ENABLE,
312 MAX30102_REG_INT_ENABLE_MASK,
313 MAX30102_REG_INT_ENABLE_FIFO_EN);
314}
315
316static int max30102_read_temp(struct max30102_data *data, int *val)
317{
318 int ret;
319 unsigned int reg;
320
321 ret = regmap_read(data->regmap, MAX30102_REG_TEMP_INTEGER, &reg);
322 if (ret < 0)
323 return ret;
324 *val = reg << 4;
325
326 ret = regmap_read(data->regmap, MAX30102_REG_TEMP_FRACTION, &reg);
327 if (ret < 0)
328 return ret;
329
330 *val |= reg & 0xf;
331 *val = sign_extend32(*val, 11);
332
333 return 0;
334}
335
336static int max30102_get_temp(struct max30102_data *data, int *val)
337{
338 int ret;
339
340 /* start acquisition */
341 ret = regmap_update_bits(data->regmap, MAX30102_REG_TEMP_CONFIG,
342 MAX30102_REG_TEMP_CONFIG_TEMP_EN,
343 MAX30102_REG_TEMP_CONFIG_TEMP_EN);
344 if (ret)
345 return ret;
346
347 msleep(35);
348
349 return max30102_read_temp(data, val);
350}
351
352static int max30102_read_raw(struct iio_dev *indio_dev,
353 struct iio_chan_spec const *chan,
354 int *val, int *val2, long mask)
355{
356 struct max30102_data *data = iio_priv(indio_dev);
357 int ret = -EINVAL;
358
359 switch (mask) {
360 case IIO_CHAN_INFO_RAW:
361 /*
362 * Temperature reading can only be acquired while engine
363 * is running
364 */
365 mutex_lock(&indio_dev->mlock);
366
367 if (!iio_buffer_enabled(indio_dev))
368 ret = -EBUSY;
369 else {
370 ret = max30102_get_temp(data, val);
371 if (!ret)
372 ret = IIO_VAL_INT;
373 }
374
375 mutex_unlock(&indio_dev->mlock);
376 break;
377 case IIO_CHAN_INFO_SCALE:
378 *val = 1; /* 0.0625 */
379 *val2 = 16;
380 ret = IIO_VAL_FRACTIONAL;
381 break;
382 }
383
384 return ret;
385}
386
387static const struct iio_info max30102_info = {
Matt Ranostayb3c590c2017-02-04 02:22:51 -0800388 .read_raw = max30102_read_raw,
389};
390
391static int max30102_probe(struct i2c_client *client,
392 const struct i2c_device_id *id)
393{
394 struct max30102_data *data;
395 struct iio_buffer *buffer;
396 struct iio_dev *indio_dev;
397 int ret;
Peter Meerwald-Stadler84b0ce02017-10-27 21:45:36 +0200398 unsigned int reg;
Matt Ranostayb3c590c2017-02-04 02:22:51 -0800399
400 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
401 if (!indio_dev)
402 return -ENOMEM;
403
404 buffer = devm_iio_kfifo_allocate(&client->dev);
405 if (!buffer)
406 return -ENOMEM;
407
408 iio_device_attach_buffer(indio_dev, buffer);
409
410 indio_dev->name = MAX30102_DRV_NAME;
411 indio_dev->channels = max30102_channels;
412 indio_dev->info = &max30102_info;
413 indio_dev->num_channels = ARRAY_SIZE(max30102_channels);
414 indio_dev->available_scan_masks = max30102_scan_masks;
415 indio_dev->modes = (INDIO_BUFFER_SOFTWARE | INDIO_DIRECT_MODE);
416 indio_dev->setup_ops = &max30102_buffer_setup_ops;
Lars-Peter Clausenfa722492017-02-11 13:34:18 +0100417 indio_dev->dev.parent = &client->dev;
Matt Ranostayb3c590c2017-02-04 02:22:51 -0800418
419 data = iio_priv(indio_dev);
420 data->indio_dev = indio_dev;
421 data->client = client;
422
423 mutex_init(&data->lock);
424 i2c_set_clientdata(client, indio_dev);
425
426 data->regmap = devm_regmap_init_i2c(client, &max30102_regmap_config);
427 if (IS_ERR(data->regmap)) {
Peter Meerwald-Stadlerc31c9462017-10-27 21:45:33 +0200428 dev_err(&client->dev, "regmap initialization failed\n");
Matt Ranostayb3c590c2017-02-04 02:22:51 -0800429 return PTR_ERR(data->regmap);
430 }
Peter Meerwald-Stadlerd0b950c2017-10-27 21:45:35 +0200431
Peter Meerwald-Stadler84b0ce02017-10-27 21:45:36 +0200432 /* check part ID */
433 ret = regmap_read(data->regmap, MAX30102_REG_PART_ID, &reg);
434 if (ret)
435 return ret;
436 if (reg != MAX30102_PART_NUMBER)
437 return -ENODEV;
438
439 /* show revision ID */
440 ret = regmap_read(data->regmap, MAX30102_REG_REV_ID, &reg);
441 if (ret)
442 return ret;
443 dev_dbg(&client->dev, "max3010x revision %02x\n", reg);
444
Peter Meerwald-Stadlerd0b950c2017-10-27 21:45:35 +0200445 ret = max30102_set_powermode(data, false);
446 if (ret)
447 return ret;
Matt Ranostayb3c590c2017-02-04 02:22:51 -0800448
449 ret = max30102_chip_init(data);
450 if (ret)
451 return ret;
452
453 if (client->irq <= 0) {
454 dev_err(&client->dev, "no valid irq defined\n");
455 return -EINVAL;
456 }
457
458 ret = devm_request_threaded_irq(&client->dev, client->irq,
459 NULL, max30102_interrupt_handler,
460 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
461 "max30102_irq", indio_dev);
462 if (ret) {
463 dev_err(&client->dev, "request irq (%d) failed\n", client->irq);
464 return ret;
465 }
466
467 return iio_device_register(indio_dev);
468}
469
470static int max30102_remove(struct i2c_client *client)
471{
472 struct iio_dev *indio_dev = i2c_get_clientdata(client);
473 struct max30102_data *data = iio_priv(indio_dev);
474
475 iio_device_unregister(indio_dev);
476 max30102_set_powermode(data, false);
477
478 return 0;
479}
480
481static const struct i2c_device_id max30102_id[] = {
482 { "max30102", 0 },
483 {}
484};
485MODULE_DEVICE_TABLE(i2c, max30102_id);
486
487static const struct of_device_id max30102_dt_ids[] = {
488 { .compatible = "maxim,max30102" },
489 { }
490};
491MODULE_DEVICE_TABLE(of, max30102_dt_ids);
492
493static struct i2c_driver max30102_driver = {
494 .driver = {
495 .name = MAX30102_DRV_NAME,
496 .of_match_table = of_match_ptr(max30102_dt_ids),
497 },
498 .probe = max30102_probe,
499 .remove = max30102_remove,
500 .id_table = max30102_id,
501};
502module_i2c_driver(max30102_driver);
503
504MODULE_AUTHOR("Matt Ranostay <matt@ranostay.consulting>");
505MODULE_DESCRIPTION("MAX30102 heart rate and pulse oximeter sensor");
506MODULE_LICENSE("GPL");