Merge branch 'for-4.6-ns' of git://git.kernel.org/pub/scm/linux/kernel/git/tj/cgroup
[deliverable/linux.git] / drivers / iio / health / max30100.c
1 /*
2 * max30100.c - Support for MAX30100 heart rate and pulse oximeter sensor
3 *
4 * Copyright (C) 2015 Matt Ranostay <mranostay@gmail.com>
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: enable pulse length controls via device tree properties
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 MAX30100_REGMAP_NAME "max30100_regmap"
34 #define MAX30100_DRV_NAME "max30100"
35
36 #define MAX30100_REG_INT_STATUS 0x00
37 #define MAX30100_REG_INT_STATUS_PWR_RDY BIT(0)
38 #define MAX30100_REG_INT_STATUS_SPO2_RDY BIT(4)
39 #define MAX30100_REG_INT_STATUS_HR_RDY BIT(5)
40 #define MAX30100_REG_INT_STATUS_FIFO_RDY BIT(7)
41
42 #define MAX30100_REG_INT_ENABLE 0x01
43 #define MAX30100_REG_INT_ENABLE_SPO2_EN BIT(0)
44 #define MAX30100_REG_INT_ENABLE_HR_EN BIT(1)
45 #define MAX30100_REG_INT_ENABLE_FIFO_EN BIT(3)
46 #define MAX30100_REG_INT_ENABLE_MASK 0xf0
47 #define MAX30100_REG_INT_ENABLE_MASK_SHIFT 4
48
49 #define MAX30100_REG_FIFO_WR_PTR 0x02
50 #define MAX30100_REG_FIFO_OVR_CTR 0x03
51 #define MAX30100_REG_FIFO_RD_PTR 0x04
52 #define MAX30100_REG_FIFO_DATA 0x05
53 #define MAX30100_REG_FIFO_DATA_ENTRY_COUNT 16
54 #define MAX30100_REG_FIFO_DATA_ENTRY_LEN 4
55
56 #define MAX30100_REG_MODE_CONFIG 0x06
57 #define MAX30100_REG_MODE_CONFIG_MODE_SPO2_EN BIT(0)
58 #define MAX30100_REG_MODE_CONFIG_MODE_HR_EN BIT(1)
59 #define MAX30100_REG_MODE_CONFIG_MODE_MASK 0x03
60 #define MAX30100_REG_MODE_CONFIG_TEMP_EN BIT(3)
61 #define MAX30100_REG_MODE_CONFIG_PWR BIT(7)
62
63 #define MAX30100_REG_SPO2_CONFIG 0x07
64 #define MAX30100_REG_SPO2_CONFIG_100HZ BIT(2)
65 #define MAX30100_REG_SPO2_CONFIG_HI_RES_EN BIT(6)
66 #define MAX30100_REG_SPO2_CONFIG_1600US 0x3
67
68 #define MAX30100_REG_LED_CONFIG 0x09
69 #define MAX30100_REG_LED_CONFIG_LED_MASK 0x0f
70 #define MAX30100_REG_LED_CONFIG_RED_LED_SHIFT 4
71
72 #define MAX30100_REG_LED_CONFIG_24MA 0x07
73 #define MAX30100_REG_LED_CONFIG_50MA 0x0f
74
75 #define MAX30100_REG_TEMP_INTEGER 0x16
76 #define MAX30100_REG_TEMP_FRACTION 0x17
77
78 struct max30100_data {
79 struct i2c_client *client;
80 struct iio_dev *indio_dev;
81 struct mutex lock;
82 struct regmap *regmap;
83
84 __be16 buffer[2]; /* 2 16-bit channels */
85 };
86
87 static bool max30100_is_volatile_reg(struct device *dev, unsigned int reg)
88 {
89 switch (reg) {
90 case MAX30100_REG_INT_STATUS:
91 case MAX30100_REG_MODE_CONFIG:
92 case MAX30100_REG_FIFO_WR_PTR:
93 case MAX30100_REG_FIFO_OVR_CTR:
94 case MAX30100_REG_FIFO_RD_PTR:
95 case MAX30100_REG_FIFO_DATA:
96 case MAX30100_REG_TEMP_INTEGER:
97 case MAX30100_REG_TEMP_FRACTION:
98 return true;
99 default:
100 return false;
101 }
102 }
103
104 static const struct regmap_config max30100_regmap_config = {
105 .name = MAX30100_REGMAP_NAME,
106
107 .reg_bits = 8,
108 .val_bits = 8,
109
110 .max_register = MAX30100_REG_TEMP_FRACTION,
111 .cache_type = REGCACHE_FLAT,
112
113 .volatile_reg = max30100_is_volatile_reg,
114 };
115
116 static const unsigned int max30100_led_current_mapping[] = {
117 4400, 7600, 11000, 14200, 17400,
118 20800, 24000, 27100, 30600, 33800,
119 37000, 40200, 43600, 46800, 50000
120 };
121
122 static const unsigned long max30100_scan_masks[] = {0x3, 0};
123
124 static const struct iio_chan_spec max30100_channels[] = {
125 {
126 .type = IIO_INTENSITY,
127 .channel2 = IIO_MOD_LIGHT_IR,
128 .modified = 1,
129
130 .scan_index = 0,
131 .scan_type = {
132 .sign = 'u',
133 .realbits = 16,
134 .storagebits = 16,
135 .endianness = IIO_BE,
136 },
137 },
138 {
139 .type = IIO_INTENSITY,
140 .channel2 = IIO_MOD_LIGHT_RED,
141 .modified = 1,
142
143 .scan_index = 1,
144 .scan_type = {
145 .sign = 'u',
146 .realbits = 16,
147 .storagebits = 16,
148 .endianness = IIO_BE,
149 },
150 },
151 {
152 .type = IIO_TEMP,
153 .info_mask_separate =
154 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
155 .scan_index = -1,
156 },
157 };
158
159 static int max30100_set_powermode(struct max30100_data *data, bool state)
160 {
161 return regmap_update_bits(data->regmap, MAX30100_REG_MODE_CONFIG,
162 MAX30100_REG_MODE_CONFIG_PWR,
163 state ? 0 : MAX30100_REG_MODE_CONFIG_PWR);
164 }
165
166 static int max30100_clear_fifo(struct max30100_data *data)
167 {
168 int ret;
169
170 ret = regmap_write(data->regmap, MAX30100_REG_FIFO_WR_PTR, 0);
171 if (ret)
172 return ret;
173
174 ret = regmap_write(data->regmap, MAX30100_REG_FIFO_OVR_CTR, 0);
175 if (ret)
176 return ret;
177
178 return regmap_write(data->regmap, MAX30100_REG_FIFO_RD_PTR, 0);
179 }
180
181 static int max30100_buffer_postenable(struct iio_dev *indio_dev)
182 {
183 struct max30100_data *data = iio_priv(indio_dev);
184 int ret;
185
186 ret = max30100_set_powermode(data, true);
187 if (ret)
188 return ret;
189
190 return max30100_clear_fifo(data);
191 }
192
193 static int max30100_buffer_predisable(struct iio_dev *indio_dev)
194 {
195 struct max30100_data *data = iio_priv(indio_dev);
196
197 return max30100_set_powermode(data, false);
198 }
199
200 static const struct iio_buffer_setup_ops max30100_buffer_setup_ops = {
201 .postenable = max30100_buffer_postenable,
202 .predisable = max30100_buffer_predisable,
203 };
204
205 static inline int max30100_fifo_count(struct max30100_data *data)
206 {
207 unsigned int val;
208 int ret;
209
210 ret = regmap_read(data->regmap, MAX30100_REG_INT_STATUS, &val);
211 if (ret)
212 return ret;
213
214 /* FIFO is almost full */
215 if (val & MAX30100_REG_INT_STATUS_FIFO_RDY)
216 return MAX30100_REG_FIFO_DATA_ENTRY_COUNT - 1;
217
218 return 0;
219 }
220
221 static int max30100_read_measurement(struct max30100_data *data)
222 {
223 int ret;
224
225 ret = i2c_smbus_read_i2c_block_data(data->client,
226 MAX30100_REG_FIFO_DATA,
227 MAX30100_REG_FIFO_DATA_ENTRY_LEN,
228 (u8 *) &data->buffer);
229
230 return (ret == MAX30100_REG_FIFO_DATA_ENTRY_LEN) ? 0 : ret;
231 }
232
233 static irqreturn_t max30100_interrupt_handler(int irq, void *private)
234 {
235 struct iio_dev *indio_dev = private;
236 struct max30100_data *data = iio_priv(indio_dev);
237 int ret, cnt = 0;
238
239 mutex_lock(&data->lock);
240
241 while (cnt-- || (cnt = max30100_fifo_count(data) > 0)) {
242 ret = max30100_read_measurement(data);
243 if (ret)
244 break;
245
246 iio_push_to_buffers(data->indio_dev, data->buffer);
247 }
248
249 mutex_unlock(&data->lock);
250
251 return IRQ_HANDLED;
252 }
253
254 static int max30100_get_current_idx(unsigned int val, int *reg)
255 {
256 int idx;
257
258 /* LED turned off */
259 if (val == 0) {
260 *reg = 0;
261 return 0;
262 }
263
264 for (idx = 0; idx < ARRAY_SIZE(max30100_led_current_mapping); idx++) {
265 if (max30100_led_current_mapping[idx] == val) {
266 *reg = idx + 1;
267 return 0;
268 }
269 }
270
271 return -EINVAL;
272 }
273
274 static int max30100_led_init(struct max30100_data *data)
275 {
276 struct device *dev = &data->client->dev;
277 struct device_node *np = dev->of_node;
278 unsigned int val[2];
279 int reg, ret;
280
281 ret = of_property_read_u32_array(np, "maxim,led-current-microamp",
282 (unsigned int *) &val, 2);
283 if (ret) {
284 /* Default to 24 mA RED LED, 50 mA IR LED */
285 reg = (MAX30100_REG_LED_CONFIG_24MA <<
286 MAX30100_REG_LED_CONFIG_RED_LED_SHIFT) |
287 MAX30100_REG_LED_CONFIG_50MA;
288 dev_warn(dev, "no led-current-microamp set");
289
290 return regmap_write(data->regmap, MAX30100_REG_LED_CONFIG, reg);
291 }
292
293 /* RED LED current */
294 ret = max30100_get_current_idx(val[0], &reg);
295 if (ret) {
296 dev_err(dev, "invalid RED current setting %d", val[0]);
297 return ret;
298 }
299
300 ret = regmap_update_bits(data->regmap, MAX30100_REG_LED_CONFIG,
301 MAX30100_REG_LED_CONFIG_LED_MASK <<
302 MAX30100_REG_LED_CONFIG_RED_LED_SHIFT,
303 reg << MAX30100_REG_LED_CONFIG_RED_LED_SHIFT);
304 if (ret)
305 return ret;
306
307 /* IR LED current */
308 ret = max30100_get_current_idx(val[1], &reg);
309 if (ret) {
310 dev_err(dev, "invalid IR current setting %d", val[1]);
311 return ret;
312 }
313
314 return regmap_update_bits(data->regmap, MAX30100_REG_LED_CONFIG,
315 MAX30100_REG_LED_CONFIG_LED_MASK, reg);
316 }
317
318 static int max30100_chip_init(struct max30100_data *data)
319 {
320 int ret;
321
322 /* setup LED current settings */
323 ret = max30100_led_init(data);
324 if (ret)
325 return ret;
326
327 /* enable hi-res SPO2 readings at 100Hz */
328 ret = regmap_write(data->regmap, MAX30100_REG_SPO2_CONFIG,
329 MAX30100_REG_SPO2_CONFIG_HI_RES_EN |
330 MAX30100_REG_SPO2_CONFIG_100HZ);
331 if (ret)
332 return ret;
333
334 /* enable SPO2 mode */
335 ret = regmap_update_bits(data->regmap, MAX30100_REG_MODE_CONFIG,
336 MAX30100_REG_MODE_CONFIG_MODE_MASK,
337 MAX30100_REG_MODE_CONFIG_MODE_HR_EN |
338 MAX30100_REG_MODE_CONFIG_MODE_SPO2_EN);
339 if (ret)
340 return ret;
341
342 /* enable FIFO interrupt */
343 return regmap_update_bits(data->regmap, MAX30100_REG_INT_ENABLE,
344 MAX30100_REG_INT_ENABLE_MASK,
345 MAX30100_REG_INT_ENABLE_FIFO_EN
346 << MAX30100_REG_INT_ENABLE_MASK_SHIFT);
347 }
348
349 static int max30100_read_temp(struct max30100_data *data, int *val)
350 {
351 int ret;
352 unsigned int reg;
353
354 ret = regmap_read(data->regmap, MAX30100_REG_TEMP_INTEGER, &reg);
355 if (ret < 0)
356 return ret;
357 *val = reg << 4;
358
359 ret = regmap_read(data->regmap, MAX30100_REG_TEMP_FRACTION, &reg);
360 if (ret < 0)
361 return ret;
362
363 *val |= reg & 0xf;
364 *val = sign_extend32(*val, 11);
365
366 return 0;
367 }
368
369 static int max30100_get_temp(struct max30100_data *data, int *val)
370 {
371 int ret;
372
373 /* start acquisition */
374 ret = regmap_update_bits(data->regmap, MAX30100_REG_MODE_CONFIG,
375 MAX30100_REG_MODE_CONFIG_TEMP_EN,
376 MAX30100_REG_MODE_CONFIG_TEMP_EN);
377 if (ret)
378 return ret;
379
380 usleep_range(35000, 50000);
381
382 return max30100_read_temp(data, val);
383 }
384
385 static int max30100_read_raw(struct iio_dev *indio_dev,
386 struct iio_chan_spec const *chan,
387 int *val, int *val2, long mask)
388 {
389 struct max30100_data *data = iio_priv(indio_dev);
390 int ret = -EINVAL;
391
392 switch (mask) {
393 case IIO_CHAN_INFO_RAW:
394 /*
395 * Temperature reading can only be acquired while engine
396 * is running
397 */
398 mutex_lock(&indio_dev->mlock);
399
400 if (!iio_buffer_enabled(indio_dev))
401 ret = -EAGAIN;
402 else {
403 ret = max30100_get_temp(data, val);
404 if (!ret)
405 ret = IIO_VAL_INT;
406
407 }
408
409 mutex_unlock(&indio_dev->mlock);
410 break;
411 case IIO_CHAN_INFO_SCALE:
412 *val = 1; /* 0.0625 */
413 *val2 = 16;
414 ret = IIO_VAL_FRACTIONAL;
415 break;
416 }
417
418 return ret;
419 }
420
421 static const struct iio_info max30100_info = {
422 .driver_module = THIS_MODULE,
423 .read_raw = max30100_read_raw,
424 };
425
426 static int max30100_probe(struct i2c_client *client,
427 const struct i2c_device_id *id)
428 {
429 struct max30100_data *data;
430 struct iio_buffer *buffer;
431 struct iio_dev *indio_dev;
432 int ret;
433
434 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
435 if (!indio_dev)
436 return -ENOMEM;
437
438 buffer = devm_iio_kfifo_allocate(&client->dev);
439 if (!buffer)
440 return -ENOMEM;
441
442 iio_device_attach_buffer(indio_dev, buffer);
443
444 indio_dev->name = MAX30100_DRV_NAME;
445 indio_dev->channels = max30100_channels;
446 indio_dev->info = &max30100_info;
447 indio_dev->num_channels = ARRAY_SIZE(max30100_channels);
448 indio_dev->available_scan_masks = max30100_scan_masks;
449 indio_dev->modes = (INDIO_BUFFER_SOFTWARE | INDIO_DIRECT_MODE);
450 indio_dev->setup_ops = &max30100_buffer_setup_ops;
451
452 data = iio_priv(indio_dev);
453 data->indio_dev = indio_dev;
454 data->client = client;
455
456 mutex_init(&data->lock);
457 i2c_set_clientdata(client, indio_dev);
458
459 data->regmap = devm_regmap_init_i2c(client, &max30100_regmap_config);
460 if (IS_ERR(data->regmap)) {
461 dev_err(&client->dev, "regmap initialization failed.\n");
462 return PTR_ERR(data->regmap);
463 }
464 max30100_set_powermode(data, false);
465
466 ret = max30100_chip_init(data);
467 if (ret)
468 return ret;
469
470 if (client->irq <= 0) {
471 dev_err(&client->dev, "no valid irq defined\n");
472 return -EINVAL;
473 }
474 ret = devm_request_threaded_irq(&client->dev, client->irq,
475 NULL, max30100_interrupt_handler,
476 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
477 "max30100_irq", indio_dev);
478 if (ret) {
479 dev_err(&client->dev, "request irq (%d) failed\n", client->irq);
480 return ret;
481 }
482
483 return iio_device_register(indio_dev);
484 }
485
486 static int max30100_remove(struct i2c_client *client)
487 {
488 struct iio_dev *indio_dev = i2c_get_clientdata(client);
489 struct max30100_data *data = iio_priv(indio_dev);
490
491 iio_device_unregister(indio_dev);
492 max30100_set_powermode(data, false);
493
494 return 0;
495 }
496
497 static const struct i2c_device_id max30100_id[] = {
498 { "max30100", 0 },
499 {}
500 };
501 MODULE_DEVICE_TABLE(i2c, max30100_id);
502
503 static const struct of_device_id max30100_dt_ids[] = {
504 { .compatible = "maxim,max30100" },
505 { }
506 };
507 MODULE_DEVICE_TABLE(of, max30100_dt_ids);
508
509 static struct i2c_driver max30100_driver = {
510 .driver = {
511 .name = MAX30100_DRV_NAME,
512 .of_match_table = of_match_ptr(max30100_dt_ids),
513 },
514 .probe = max30100_probe,
515 .remove = max30100_remove,
516 .id_table = max30100_id,
517 };
518 module_i2c_driver(max30100_driver);
519
520 MODULE_AUTHOR("Matt Ranostay <mranostay@gmail.com>");
521 MODULE_DESCRIPTION("MAX30100 heart rate and pulse oximeter sensor");
522 MODULE_LICENSE("GPL");
This page took 0.041876 seconds and 6 git commands to generate.