blob: c224f0e7348d96267304d0ef3a3e641b6eaff695 [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
Peter Meerwald-Stadlerad90e572017-10-27 21:45:39 +020037enum max3012_led_idx {
38 MAX30102_LED_RED,
39 MAX30102_LED_IR,
40};
41
Matt Ranostayb3c590c2017-02-04 02:22:51 -080042#define MAX30102_REG_INT_STATUS 0x00
43#define MAX30102_REG_INT_STATUS_PWR_RDY BIT(0)
44#define MAX30102_REG_INT_STATUS_PROX_INT BIT(4)
45#define MAX30102_REG_INT_STATUS_ALC_OVF BIT(5)
46#define MAX30102_REG_INT_STATUS_PPG_RDY BIT(6)
47#define MAX30102_REG_INT_STATUS_FIFO_RDY BIT(7)
48
49#define MAX30102_REG_INT_ENABLE 0x02
50#define MAX30102_REG_INT_ENABLE_PROX_INT_EN BIT(4)
51#define MAX30102_REG_INT_ENABLE_ALC_OVF_EN BIT(5)
52#define MAX30102_REG_INT_ENABLE_PPG_EN BIT(6)
53#define MAX30102_REG_INT_ENABLE_FIFO_EN BIT(7)
54#define MAX30102_REG_INT_ENABLE_MASK 0xf0
55#define MAX30102_REG_INT_ENABLE_MASK_SHIFT 4
56
57#define MAX30102_REG_FIFO_WR_PTR 0x04
58#define MAX30102_REG_FIFO_OVR_CTR 0x05
59#define MAX30102_REG_FIFO_RD_PTR 0x06
60#define MAX30102_REG_FIFO_DATA 0x07
61#define MAX30102_REG_FIFO_DATA_ENTRY_LEN 6
62
63#define MAX30102_REG_FIFO_CONFIG 0x08
64#define MAX30102_REG_FIFO_CONFIG_AVG_4SAMPLES BIT(1)
65#define MAX30102_REG_FIFO_CONFIG_AVG_SHIFT 5
66#define MAX30102_REG_FIFO_CONFIG_AFULL BIT(0)
67
68#define MAX30102_REG_MODE_CONFIG 0x09
Peter Meerwald-Stadler7b0b0ec2017-10-27 21:45:34 +020069#define MAX30102_REG_MODE_CONFIG_MODE_HR 0x02 /* red LED */
70#define MAX30102_REG_MODE_CONFIG_MODE_HR_SPO2 0x03 /* red + IR LED */
71#define MAX30102_REG_MODE_CONFIG_MODE_MULTI 0x07 /* multi-LED mode */
72#define MAX30102_REG_MODE_CONFIG_MODE_MASK GENMASK(2, 0)
Matt Ranostayb3c590c2017-02-04 02:22:51 -080073#define MAX30102_REG_MODE_CONFIG_PWR BIT(7)
74
75#define MAX30102_REG_SPO2_CONFIG 0x0a
76#define MAX30102_REG_SPO2_CONFIG_PULSE_411_US 0x03
77#define MAX30102_REG_SPO2_CONFIG_SR_400HZ 0x03
78#define MAX30102_REG_SPO2_CONFIG_SR_MASK 0x07
79#define MAX30102_REG_SPO2_CONFIG_SR_MASK_SHIFT 2
80#define MAX30102_REG_SPO2_CONFIG_ADC_4096_STEPS BIT(0)
81#define MAX30102_REG_SPO2_CONFIG_ADC_MASK_SHIFT 5
82
83#define MAX30102_REG_RED_LED_CONFIG 0x0c
84#define MAX30102_REG_IR_LED_CONFIG 0x0d
85
86#define MAX30102_REG_TEMP_CONFIG 0x21
87#define MAX30102_REG_TEMP_CONFIG_TEMP_EN BIT(0)
88
89#define MAX30102_REG_TEMP_INTEGER 0x1f
90#define MAX30102_REG_TEMP_FRACTION 0x20
91
Peter Meerwald-Stadler84b0ce02017-10-27 21:45:36 +020092#define MAX30102_REG_REV_ID 0xfe
93#define MAX30102_REG_PART_ID 0xff
94
Matt Ranostayb3c590c2017-02-04 02:22:51 -080095struct max30102_data {
96 struct i2c_client *client;
97 struct iio_dev *indio_dev;
98 struct mutex lock;
99 struct regmap *regmap;
100
101 u8 buffer[8];
102 __be32 processed_buffer[2]; /* 2 x 18-bit (padded to 32-bits) */
103};
104
105static const struct regmap_config max30102_regmap_config = {
106 .name = MAX30102_REGMAP_NAME,
107
108 .reg_bits = 8,
109 .val_bits = 8,
110};
111
Peter Meerwald-Stadlerad90e572017-10-27 21:45:39 +0200112static const unsigned long max30102_scan_masks[] = {
113 BIT(MAX30102_LED_RED) | BIT(MAX30102_LED_IR),
114 0
115};
Matt Ranostayb3c590c2017-02-04 02:22:51 -0800116
Peter Meerwald-Stadler839a74c2017-10-27 21:45:37 +0200117#define MAX30102_INTENSITY_CHANNEL(_si, _mod) { \
118 .type = IIO_INTENSITY, \
119 .channel2 = _mod, \
120 .modified = 1, \
121 .scan_index = _si, \
122 .scan_type = { \
123 .sign = 'u', \
124 .shift = 8, \
125 .realbits = 18, \
126 .storagebits = 32, \
127 .endianness = IIO_BE, \
128 }, \
129 }
130
Matt Ranostayb3c590c2017-02-04 02:22:51 -0800131static const struct iio_chan_spec max30102_channels[] = {
Peter Meerwald-Stadlerad90e572017-10-27 21:45:39 +0200132 MAX30102_INTENSITY_CHANNEL(MAX30102_LED_RED, IIO_MOD_LIGHT_RED),
133 MAX30102_INTENSITY_CHANNEL(MAX30102_LED_IR, IIO_MOD_LIGHT_IR),
Matt Ranostayb3c590c2017-02-04 02:22:51 -0800134 {
135 .type = IIO_TEMP,
136 .info_mask_separate =
137 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
138 .scan_index = -1,
139 },
140};
141
142static int max30102_set_powermode(struct max30102_data *data, bool state)
143{
144 return regmap_update_bits(data->regmap, MAX30102_REG_MODE_CONFIG,
145 MAX30102_REG_MODE_CONFIG_PWR,
146 state ? 0 : MAX30102_REG_MODE_CONFIG_PWR);
147}
148
149static int max30102_buffer_postenable(struct iio_dev *indio_dev)
150{
151 struct max30102_data *data = iio_priv(indio_dev);
152
153 return max30102_set_powermode(data, true);
154}
155
156static int max30102_buffer_predisable(struct iio_dev *indio_dev)
157{
158 struct max30102_data *data = iio_priv(indio_dev);
159
160 return max30102_set_powermode(data, false);
161}
162
163static const struct iio_buffer_setup_ops max30102_buffer_setup_ops = {
164 .postenable = max30102_buffer_postenable,
165 .predisable = max30102_buffer_predisable,
166};
167
168static inline int max30102_fifo_count(struct max30102_data *data)
169{
170 unsigned int val;
171 int ret;
172
173 ret = regmap_read(data->regmap, MAX30102_REG_INT_STATUS, &val);
174 if (ret)
175 return ret;
176
177 /* FIFO has one sample slot left */
178 if (val & MAX30102_REG_INT_STATUS_FIFO_RDY)
179 return 1;
180
181 return 0;
182}
183
184static int max30102_read_measurement(struct max30102_data *data)
185{
186 int ret;
187 u8 *buffer = (u8 *) &data->buffer;
188
189 ret = i2c_smbus_read_i2c_block_data(data->client,
190 MAX30102_REG_FIFO_DATA,
191 MAX30102_REG_FIFO_DATA_ENTRY_LEN,
192 buffer);
193
194 memcpy(&data->processed_buffer[0], &buffer[0], 3);
195 memcpy(&data->processed_buffer[1], &buffer[3], 3);
196
197 return (ret == MAX30102_REG_FIFO_DATA_ENTRY_LEN) ? 0 : -EINVAL;
198}
199
200static irqreturn_t max30102_interrupt_handler(int irq, void *private)
201{
202 struct iio_dev *indio_dev = private;
203 struct max30102_data *data = iio_priv(indio_dev);
204 int ret, cnt = 0;
205
206 mutex_lock(&data->lock);
207
208 while (cnt || (cnt = max30102_fifo_count(data)) > 0) {
209 ret = max30102_read_measurement(data);
210 if (ret)
211 break;
212
213 iio_push_to_buffers(data->indio_dev, data->processed_buffer);
214 cnt--;
215 }
216
217 mutex_unlock(&data->lock);
218
219 return IRQ_HANDLED;
220}
221
222static int max30102_get_current_idx(unsigned int val, int *reg)
223{
224 /* each step is 0.200 mA */
225 *reg = val / 200;
226
227 return *reg > 0xff ? -EINVAL : 0;
228}
229
230static int max30102_led_init(struct max30102_data *data)
231{
232 struct device *dev = &data->client->dev;
233 struct device_node *np = dev->of_node;
234 unsigned int val;
235 int reg, ret;
236
237 ret = of_property_read_u32(np, "maxim,red-led-current-microamp", &val);
238 if (ret) {
239 dev_info(dev, "no red-led-current-microamp set\n");
240
241 /* Default to 7 mA RED LED */
242 val = 7000;
243 }
244
245 ret = max30102_get_current_idx(val, &reg);
246 if (ret) {
247 dev_err(dev, "invalid RED LED current setting %d\n", val);
248 return ret;
249 }
250
251 ret = regmap_write(data->regmap, MAX30102_REG_RED_LED_CONFIG, reg);
252 if (ret)
253 return ret;
254
255 ret = of_property_read_u32(np, "maxim,ir-led-current-microamp", &val);
256 if (ret) {
257 dev_info(dev, "no ir-led-current-microamp set\n");
258
259 /* Default to 7 mA IR LED */
260 val = 7000;
261 }
262
263 ret = max30102_get_current_idx(val, &reg);
264 if (ret) {
Peter Meerwald-Stadlerdd86dbf2017-10-27 21:45:32 +0200265 dev_err(dev, "invalid IR LED current setting %d\n", val);
Matt Ranostayb3c590c2017-02-04 02:22:51 -0800266 return ret;
267 }
268
269 return regmap_write(data->regmap, MAX30102_REG_IR_LED_CONFIG, reg);
270}
271
272static int max30102_chip_init(struct max30102_data *data)
273{
274 int ret;
275
276 /* setup LED current settings */
277 ret = max30102_led_init(data);
278 if (ret)
279 return ret;
280
281 /* enable 18-bit HR + SPO2 readings at 400Hz */
282 ret = regmap_write(data->regmap, MAX30102_REG_SPO2_CONFIG,
283 (MAX30102_REG_SPO2_CONFIG_ADC_4096_STEPS
284 << MAX30102_REG_SPO2_CONFIG_ADC_MASK_SHIFT) |
285 (MAX30102_REG_SPO2_CONFIG_SR_400HZ
286 << MAX30102_REG_SPO2_CONFIG_SR_MASK_SHIFT) |
287 MAX30102_REG_SPO2_CONFIG_PULSE_411_US);
288 if (ret)
289 return ret;
290
Peter Meerwald-Stadler7b0b0ec2017-10-27 21:45:34 +0200291 /* enable HR + SPO2 mode */
Matt Ranostayb3c590c2017-02-04 02:22:51 -0800292 ret = regmap_update_bits(data->regmap, MAX30102_REG_MODE_CONFIG,
293 MAX30102_REG_MODE_CONFIG_MODE_MASK,
Peter Meerwald-Stadler7b0b0ec2017-10-27 21:45:34 +0200294 MAX30102_REG_MODE_CONFIG_MODE_HR_SPO2);
Matt Ranostayb3c590c2017-02-04 02:22:51 -0800295 if (ret)
296 return ret;
297
298 /* average 4 samples + generate FIFO interrupt */
299 ret = regmap_write(data->regmap, MAX30102_REG_FIFO_CONFIG,
300 (MAX30102_REG_FIFO_CONFIG_AVG_4SAMPLES
301 << MAX30102_REG_FIFO_CONFIG_AVG_SHIFT) |
302 MAX30102_REG_FIFO_CONFIG_AFULL);
303 if (ret)
304 return ret;
305
306 /* enable FIFO interrupt */
307 return regmap_update_bits(data->regmap, MAX30102_REG_INT_ENABLE,
308 MAX30102_REG_INT_ENABLE_MASK,
309 MAX30102_REG_INT_ENABLE_FIFO_EN);
310}
311
312static int max30102_read_temp(struct max30102_data *data, int *val)
313{
314 int ret;
315 unsigned int reg;
316
317 ret = regmap_read(data->regmap, MAX30102_REG_TEMP_INTEGER, &reg);
318 if (ret < 0)
319 return ret;
320 *val = reg << 4;
321
322 ret = regmap_read(data->regmap, MAX30102_REG_TEMP_FRACTION, &reg);
323 if (ret < 0)
324 return ret;
325
326 *val |= reg & 0xf;
327 *val = sign_extend32(*val, 11);
328
329 return 0;
330}
331
Peter Meerwald-Stadlera9c47ab2017-10-27 21:45:38 +0200332static int max30102_get_temp(struct max30102_data *data, int *val, bool en)
Matt Ranostayb3c590c2017-02-04 02:22:51 -0800333{
334 int ret;
335
Peter Meerwald-Stadlera9c47ab2017-10-27 21:45:38 +0200336 if (en) {
337 ret = max30102_set_powermode(data, true);
338 if (ret)
339 return ret;
340 }
341
Matt Ranostayb3c590c2017-02-04 02:22:51 -0800342 /* start acquisition */
343 ret = regmap_update_bits(data->regmap, MAX30102_REG_TEMP_CONFIG,
344 MAX30102_REG_TEMP_CONFIG_TEMP_EN,
345 MAX30102_REG_TEMP_CONFIG_TEMP_EN);
346 if (ret)
Peter Meerwald-Stadlera9c47ab2017-10-27 21:45:38 +0200347 goto out;
Matt Ranostayb3c590c2017-02-04 02:22:51 -0800348
349 msleep(35);
Peter Meerwald-Stadlera9c47ab2017-10-27 21:45:38 +0200350 ret = max30102_read_temp(data, val);
Matt Ranostayb3c590c2017-02-04 02:22:51 -0800351
Peter Meerwald-Stadlera9c47ab2017-10-27 21:45:38 +0200352out:
353 if (en)
354 max30102_set_powermode(data, false);
355
356 return ret;
Matt Ranostayb3c590c2017-02-04 02:22:51 -0800357}
358
359static int max30102_read_raw(struct iio_dev *indio_dev,
360 struct iio_chan_spec const *chan,
361 int *val, int *val2, long mask)
362{
363 struct max30102_data *data = iio_priv(indio_dev);
364 int ret = -EINVAL;
365
366 switch (mask) {
367 case IIO_CHAN_INFO_RAW:
368 /*
Peter Meerwald-Stadlera9c47ab2017-10-27 21:45:38 +0200369 * Temperature reading can only be acquired when not in
370 * shutdown; leave shutdown briefly when buffer not running
Matt Ranostayb3c590c2017-02-04 02:22:51 -0800371 */
372 mutex_lock(&indio_dev->mlock);
Matt Ranostayb3c590c2017-02-04 02:22:51 -0800373 if (!iio_buffer_enabled(indio_dev))
Peter Meerwald-Stadlera9c47ab2017-10-27 21:45:38 +0200374 ret = max30102_get_temp(data, val, true);
375 else
376 ret = max30102_get_temp(data, val, false);
Matt Ranostayb3c590c2017-02-04 02:22:51 -0800377 mutex_unlock(&indio_dev->mlock);
Peter Meerwald-Stadlera9c47ab2017-10-27 21:45:38 +0200378 if (ret)
379 return ret;
380
381 ret = IIO_VAL_INT;
Matt Ranostayb3c590c2017-02-04 02:22:51 -0800382 break;
383 case IIO_CHAN_INFO_SCALE:
384 *val = 1; /* 0.0625 */
385 *val2 = 16;
386 ret = IIO_VAL_FRACTIONAL;
387 break;
388 }
389
390 return ret;
391}
392
393static const struct iio_info max30102_info = {
Matt Ranostayb3c590c2017-02-04 02:22:51 -0800394 .read_raw = max30102_read_raw,
395};
396
397static int max30102_probe(struct i2c_client *client,
398 const struct i2c_device_id *id)
399{
400 struct max30102_data *data;
401 struct iio_buffer *buffer;
402 struct iio_dev *indio_dev;
403 int ret;
Peter Meerwald-Stadler84b0ce02017-10-27 21:45:36 +0200404 unsigned int reg;
Matt Ranostayb3c590c2017-02-04 02:22:51 -0800405
406 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
407 if (!indio_dev)
408 return -ENOMEM;
409
410 buffer = devm_iio_kfifo_allocate(&client->dev);
411 if (!buffer)
412 return -ENOMEM;
413
414 iio_device_attach_buffer(indio_dev, buffer);
415
416 indio_dev->name = MAX30102_DRV_NAME;
417 indio_dev->channels = max30102_channels;
418 indio_dev->info = &max30102_info;
419 indio_dev->num_channels = ARRAY_SIZE(max30102_channels);
420 indio_dev->available_scan_masks = max30102_scan_masks;
421 indio_dev->modes = (INDIO_BUFFER_SOFTWARE | INDIO_DIRECT_MODE);
422 indio_dev->setup_ops = &max30102_buffer_setup_ops;
Lars-Peter Clausenfa722492017-02-11 13:34:18 +0100423 indio_dev->dev.parent = &client->dev;
Matt Ranostayb3c590c2017-02-04 02:22:51 -0800424
425 data = iio_priv(indio_dev);
426 data->indio_dev = indio_dev;
427 data->client = client;
428
429 mutex_init(&data->lock);
430 i2c_set_clientdata(client, indio_dev);
431
432 data->regmap = devm_regmap_init_i2c(client, &max30102_regmap_config);
433 if (IS_ERR(data->regmap)) {
Peter Meerwald-Stadlerc31c9462017-10-27 21:45:33 +0200434 dev_err(&client->dev, "regmap initialization failed\n");
Matt Ranostayb3c590c2017-02-04 02:22:51 -0800435 return PTR_ERR(data->regmap);
436 }
Peter Meerwald-Stadlerd0b950c2017-10-27 21:45:35 +0200437
Peter Meerwald-Stadler84b0ce02017-10-27 21:45:36 +0200438 /* check part ID */
439 ret = regmap_read(data->regmap, MAX30102_REG_PART_ID, &reg);
440 if (ret)
441 return ret;
442 if (reg != MAX30102_PART_NUMBER)
443 return -ENODEV;
444
445 /* show revision ID */
446 ret = regmap_read(data->regmap, MAX30102_REG_REV_ID, &reg);
447 if (ret)
448 return ret;
449 dev_dbg(&client->dev, "max3010x revision %02x\n", reg);
450
Peter Meerwald-Stadlerd0b950c2017-10-27 21:45:35 +0200451 ret = max30102_set_powermode(data, false);
452 if (ret)
453 return ret;
Matt Ranostayb3c590c2017-02-04 02:22:51 -0800454
455 ret = max30102_chip_init(data);
456 if (ret)
457 return ret;
458
459 if (client->irq <= 0) {
460 dev_err(&client->dev, "no valid irq defined\n");
461 return -EINVAL;
462 }
463
464 ret = devm_request_threaded_irq(&client->dev, client->irq,
465 NULL, max30102_interrupt_handler,
466 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
467 "max30102_irq", indio_dev);
468 if (ret) {
469 dev_err(&client->dev, "request irq (%d) failed\n", client->irq);
470 return ret;
471 }
472
473 return iio_device_register(indio_dev);
474}
475
476static int max30102_remove(struct i2c_client *client)
477{
478 struct iio_dev *indio_dev = i2c_get_clientdata(client);
479 struct max30102_data *data = iio_priv(indio_dev);
480
481 iio_device_unregister(indio_dev);
482 max30102_set_powermode(data, false);
483
484 return 0;
485}
486
487static const struct i2c_device_id max30102_id[] = {
488 { "max30102", 0 },
489 {}
490};
491MODULE_DEVICE_TABLE(i2c, max30102_id);
492
493static const struct of_device_id max30102_dt_ids[] = {
494 { .compatible = "maxim,max30102" },
495 { }
496};
497MODULE_DEVICE_TABLE(of, max30102_dt_ids);
498
499static struct i2c_driver max30102_driver = {
500 .driver = {
501 .name = MAX30102_DRV_NAME,
502 .of_match_table = of_match_ptr(max30102_dt_ids),
503 },
504 .probe = max30102_probe,
505 .remove = max30102_remove,
506 .id_table = max30102_id,
507};
508module_i2c_driver(max30102_driver);
509
510MODULE_AUTHOR("Matt Ranostay <matt@ranostay.consulting>");
511MODULE_DESCRIPTION("MAX30102 heart rate and pulse oximeter sensor");
512MODULE_LICENSE("GPL");