xref: /openbmc/linux/drivers/iio/health/afe4404.c (revision e285d5bf)
1 /*
2  * AFE4404 Heart Rate Monitors and Low-Cost Pulse Oximeters
3  *
4  * Copyright (C) 2015-2016 Texas Instruments Incorporated - http://www.ti.com/
5  *	Andrew F. Davis <afd@ti.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  */
16 
17 #include <linux/device.h>
18 #include <linux/err.h>
19 #include <linux/interrupt.h>
20 #include <linux/i2c.h>
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/regmap.h>
24 #include <linux/sysfs.h>
25 #include <linux/regulator/consumer.h>
26 
27 #include <linux/iio/iio.h>
28 #include <linux/iio/sysfs.h>
29 #include <linux/iio/buffer.h>
30 #include <linux/iio/trigger.h>
31 #include <linux/iio/triggered_buffer.h>
32 #include <linux/iio/trigger_consumer.h>
33 
34 #include "afe440x.h"
35 
36 #define AFE4404_DRIVER_NAME		"afe4404"
37 
38 /* AFE4404 registers */
39 #define AFE4404_TIA_GAIN_SEP		0x20
40 #define AFE4404_TIA_GAIN		0x21
41 #define AFE4404_PROG_TG_STC		0x34
42 #define AFE4404_PROG_TG_ENDC		0x35
43 #define AFE4404_LED3LEDSTC		0x36
44 #define AFE4404_LED3LEDENDC		0x37
45 #define AFE4404_CLKDIV_PRF		0x39
46 #define AFE4404_OFFDAC			0x3a
47 #define AFE4404_DEC			0x3d
48 #define AFE4404_AVG_LED2_ALED2VAL	0x3f
49 #define AFE4404_AVG_LED1_ALED1VAL	0x40
50 
51 /* AFE4404 CONTROL2 register fields */
52 #define AFE440X_CONTROL2_OSC_ENABLE	BIT(9)
53 
54 enum afe4404_fields {
55 	/* Gains */
56 	F_TIA_GAIN_SEP, F_TIA_CF_SEP,
57 	F_TIA_GAIN, TIA_CF,
58 
59 	/* LED Current */
60 	F_ILED1, F_ILED2, F_ILED3,
61 
62 	/* Offset DAC */
63 	F_OFFDAC_AMB2, F_OFFDAC_LED1, F_OFFDAC_AMB1, F_OFFDAC_LED2,
64 
65 	/* sentinel */
66 	F_MAX_FIELDS
67 };
68 
69 static const struct reg_field afe4404_reg_fields[] = {
70 	/* Gains */
71 	[F_TIA_GAIN_SEP]	= REG_FIELD(AFE4404_TIA_GAIN_SEP, 0, 2),
72 	[F_TIA_CF_SEP]		= REG_FIELD(AFE4404_TIA_GAIN_SEP, 3, 5),
73 	[F_TIA_GAIN]		= REG_FIELD(AFE4404_TIA_GAIN, 0, 2),
74 	[TIA_CF]		= REG_FIELD(AFE4404_TIA_GAIN, 3, 5),
75 	/* LED Current */
76 	[F_ILED1]		= REG_FIELD(AFE440X_LEDCNTRL, 0, 5),
77 	[F_ILED2]		= REG_FIELD(AFE440X_LEDCNTRL, 6, 11),
78 	[F_ILED3]		= REG_FIELD(AFE440X_LEDCNTRL, 12, 17),
79 	/* Offset DAC */
80 	[F_OFFDAC_AMB2]		= REG_FIELD(AFE4404_OFFDAC, 0, 4),
81 	[F_OFFDAC_LED1]		= REG_FIELD(AFE4404_OFFDAC, 5, 9),
82 	[F_OFFDAC_AMB1]		= REG_FIELD(AFE4404_OFFDAC, 10, 14),
83 	[F_OFFDAC_LED2]		= REG_FIELD(AFE4404_OFFDAC, 15, 19),
84 };
85 
86 /**
87  * struct afe4404_data - AFE4404 device instance data
88  * @dev: Device structure
89  * @regmap: Register map of the device
90  * @fields: Register fields of the device
91  * @regulator: Pointer to the regulator for the IC
92  * @trig: IIO trigger for this device
93  * @irq: ADC_RDY line interrupt number
94  */
95 struct afe4404_data {
96 	struct device *dev;
97 	struct regmap *regmap;
98 	struct regmap_field *fields[F_MAX_FIELDS];
99 	struct regulator *regulator;
100 	struct iio_trigger *trig;
101 	int irq;
102 };
103 
104 enum afe4404_chan_id {
105 	LED2 = 1,
106 	ALED2,
107 	LED1,
108 	ALED1,
109 	LED2_ALED2,
110 	LED1_ALED1,
111 };
112 
113 static const unsigned int afe4404_channel_values[] = {
114 	[LED2] = AFE440X_LED2VAL,
115 	[ALED2] = AFE440X_ALED2VAL,
116 	[LED1] = AFE440X_LED1VAL,
117 	[ALED1] = AFE440X_ALED1VAL,
118 	[LED2_ALED2] = AFE440X_LED2_ALED2VAL,
119 	[LED1_ALED1] = AFE440X_LED1_ALED1VAL,
120 };
121 
122 static const unsigned int afe4404_channel_leds[] = {
123 	[LED2] = F_ILED2,
124 	[ALED2] = F_ILED3,
125 	[LED1] = F_ILED1,
126 };
127 
128 static const unsigned int afe4404_channel_offdacs[] = {
129 	[LED2] = F_OFFDAC_LED2,
130 	[ALED2] = F_OFFDAC_AMB2,
131 	[LED1] = F_OFFDAC_LED1,
132 	[ALED1] = F_OFFDAC_AMB1,
133 };
134 
135 static const struct iio_chan_spec afe4404_channels[] = {
136 	/* ADC values */
137 	AFE440X_INTENSITY_CHAN(LED2, BIT(IIO_CHAN_INFO_OFFSET)),
138 	AFE440X_INTENSITY_CHAN(ALED2, BIT(IIO_CHAN_INFO_OFFSET)),
139 	AFE440X_INTENSITY_CHAN(LED1, BIT(IIO_CHAN_INFO_OFFSET)),
140 	AFE440X_INTENSITY_CHAN(ALED1, BIT(IIO_CHAN_INFO_OFFSET)),
141 	AFE440X_INTENSITY_CHAN(LED2_ALED2, 0),
142 	AFE440X_INTENSITY_CHAN(LED1_ALED1, 0),
143 	/* LED current */
144 	AFE440X_CURRENT_CHAN(LED2),
145 	AFE440X_CURRENT_CHAN(ALED2),
146 	AFE440X_CURRENT_CHAN(LED1),
147 };
148 
149 static const struct afe440x_val_table afe4404_res_table[] = {
150 	{ .integer = 500000, .fract = 0 },
151 	{ .integer = 250000, .fract = 0 },
152 	{ .integer = 100000, .fract = 0 },
153 	{ .integer = 50000, .fract = 0 },
154 	{ .integer = 25000, .fract = 0 },
155 	{ .integer = 10000, .fract = 0 },
156 	{ .integer = 1000000, .fract = 0 },
157 	{ .integer = 2000000, .fract = 0 },
158 };
159 AFE440X_TABLE_ATTR(in_intensity_resistance_available, afe4404_res_table);
160 
161 static const struct afe440x_val_table afe4404_cap_table[] = {
162 	{ .integer = 0, .fract = 5000 },
163 	{ .integer = 0, .fract = 2500 },
164 	{ .integer = 0, .fract = 10000 },
165 	{ .integer = 0, .fract = 7500 },
166 	{ .integer = 0, .fract = 20000 },
167 	{ .integer = 0, .fract = 17500 },
168 	{ .integer = 0, .fract = 25000 },
169 	{ .integer = 0, .fract = 22500 },
170 };
171 AFE440X_TABLE_ATTR(in_intensity_capacitance_available, afe4404_cap_table);
172 
173 static ssize_t afe440x_show_register(struct device *dev,
174 				     struct device_attribute *attr,
175 				     char *buf)
176 {
177 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
178 	struct afe4404_data *afe = iio_priv(indio_dev);
179 	struct afe440x_attr *afe440x_attr = to_afe440x_attr(attr);
180 	unsigned int reg_val;
181 	int vals[2];
182 	int ret;
183 
184 	ret = regmap_field_read(afe->fields[afe440x_attr->field], &reg_val);
185 	if (ret)
186 		return ret;
187 
188 	if (reg_val >= afe440x_attr->table_size)
189 		return -EINVAL;
190 
191 	vals[0] = afe440x_attr->val_table[reg_val].integer;
192 	vals[1] = afe440x_attr->val_table[reg_val].fract;
193 
194 	return iio_format_value(buf, IIO_VAL_INT_PLUS_MICRO, 2, vals);
195 }
196 
197 static ssize_t afe440x_store_register(struct device *dev,
198 				      struct device_attribute *attr,
199 				      const char *buf, size_t count)
200 {
201 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
202 	struct afe4404_data *afe = iio_priv(indio_dev);
203 	struct afe440x_attr *afe440x_attr = to_afe440x_attr(attr);
204 	int val, integer, fract, ret;
205 
206 	ret = iio_str_to_fixpoint(buf, 100000, &integer, &fract);
207 	if (ret)
208 		return ret;
209 
210 	for (val = 0; val < afe440x_attr->table_size; val++)
211 		if (afe440x_attr->val_table[val].integer == integer &&
212 		    afe440x_attr->val_table[val].fract == fract)
213 			break;
214 	if (val == afe440x_attr->table_size)
215 		return -EINVAL;
216 
217 	ret = regmap_field_write(afe->fields[afe440x_attr->field], val);
218 	if (ret)
219 		return ret;
220 
221 	return count;
222 }
223 
224 static AFE440X_ATTR(in_intensity1_resistance, F_TIA_GAIN_SEP, afe4404_res_table);
225 static AFE440X_ATTR(in_intensity1_capacitance, F_TIA_CF_SEP, afe4404_cap_table);
226 
227 static AFE440X_ATTR(in_intensity2_resistance, F_TIA_GAIN_SEP, afe4404_res_table);
228 static AFE440X_ATTR(in_intensity2_capacitance, F_TIA_CF_SEP, afe4404_cap_table);
229 
230 static AFE440X_ATTR(in_intensity3_resistance, F_TIA_GAIN, afe4404_res_table);
231 static AFE440X_ATTR(in_intensity3_capacitance, TIA_CF, afe4404_cap_table);
232 
233 static AFE440X_ATTR(in_intensity4_resistance, F_TIA_GAIN, afe4404_res_table);
234 static AFE440X_ATTR(in_intensity4_capacitance, TIA_CF, afe4404_cap_table);
235 
236 static struct attribute *afe440x_attributes[] = {
237 	&dev_attr_in_intensity_resistance_available.attr,
238 	&dev_attr_in_intensity_capacitance_available.attr,
239 	&afe440x_attr_in_intensity1_resistance.dev_attr.attr,
240 	&afe440x_attr_in_intensity1_capacitance.dev_attr.attr,
241 	&afe440x_attr_in_intensity2_resistance.dev_attr.attr,
242 	&afe440x_attr_in_intensity2_capacitance.dev_attr.attr,
243 	&afe440x_attr_in_intensity3_resistance.dev_attr.attr,
244 	&afe440x_attr_in_intensity3_capacitance.dev_attr.attr,
245 	&afe440x_attr_in_intensity4_resistance.dev_attr.attr,
246 	&afe440x_attr_in_intensity4_capacitance.dev_attr.attr,
247 	NULL
248 };
249 
250 static const struct attribute_group afe440x_attribute_group = {
251 	.attrs = afe440x_attributes
252 };
253 
254 static int afe4404_read_raw(struct iio_dev *indio_dev,
255 			    struct iio_chan_spec const *chan,
256 			    int *val, int *val2, long mask)
257 {
258 	struct afe4404_data *afe = iio_priv(indio_dev);
259 	unsigned int value_reg = afe4404_channel_values[chan->address];
260 	unsigned int led_field = afe4404_channel_leds[chan->address];
261 	unsigned int offdac_field = afe4404_channel_offdacs[chan->address];
262 	int ret;
263 
264 	switch (chan->type) {
265 	case IIO_INTENSITY:
266 		switch (mask) {
267 		case IIO_CHAN_INFO_RAW:
268 			ret = regmap_read(afe->regmap, value_reg, val);
269 			if (ret)
270 				return ret;
271 			return IIO_VAL_INT;
272 		case IIO_CHAN_INFO_OFFSET:
273 			ret = regmap_field_read(afe->fields[offdac_field], val);
274 			if (ret)
275 				return ret;
276 			return IIO_VAL_INT;
277 		}
278 		break;
279 	case IIO_CURRENT:
280 		switch (mask) {
281 		case IIO_CHAN_INFO_RAW:
282 			ret = regmap_field_read(afe->fields[led_field], val);
283 			if (ret)
284 				return ret;
285 			return IIO_VAL_INT;
286 		case IIO_CHAN_INFO_SCALE:
287 			*val = 0;
288 			*val2 = 800000;
289 			return IIO_VAL_INT_PLUS_MICRO;
290 		}
291 		break;
292 	default:
293 		break;
294 	}
295 
296 	return -EINVAL;
297 }
298 
299 static int afe4404_write_raw(struct iio_dev *indio_dev,
300 			     struct iio_chan_spec const *chan,
301 			     int val, int val2, long mask)
302 {
303 	struct afe4404_data *afe = iio_priv(indio_dev);
304 	unsigned int led_field = afe4404_channel_leds[chan->address];
305 	unsigned int offdac_field = afe4404_channel_offdacs[chan->address];
306 
307 	switch (chan->type) {
308 	case IIO_INTENSITY:
309 		switch (mask) {
310 		case IIO_CHAN_INFO_OFFSET:
311 			return regmap_field_write(afe->fields[offdac_field], val);
312 		}
313 		break;
314 	case IIO_CURRENT:
315 		switch (mask) {
316 		case IIO_CHAN_INFO_RAW:
317 			return regmap_field_write(afe->fields[led_field], val);
318 		}
319 		break;
320 	default:
321 		break;
322 	}
323 
324 	return -EINVAL;
325 }
326 
327 static const struct iio_info afe4404_iio_info = {
328 	.attrs = &afe440x_attribute_group,
329 	.read_raw = afe4404_read_raw,
330 	.write_raw = afe4404_write_raw,
331 };
332 
333 static irqreturn_t afe4404_trigger_handler(int irq, void *private)
334 {
335 	struct iio_poll_func *pf = private;
336 	struct iio_dev *indio_dev = pf->indio_dev;
337 	struct afe4404_data *afe = iio_priv(indio_dev);
338 	int ret, bit, i = 0;
339 	s32 buffer[10];
340 
341 	for_each_set_bit(bit, indio_dev->active_scan_mask,
342 			 indio_dev->masklength) {
343 		ret = regmap_read(afe->regmap, afe4404_channel_values[bit],
344 				  &buffer[i++]);
345 		if (ret)
346 			goto err;
347 	}
348 
349 	iio_push_to_buffers_with_timestamp(indio_dev, buffer, pf->timestamp);
350 err:
351 	iio_trigger_notify_done(indio_dev->trig);
352 
353 	return IRQ_HANDLED;
354 }
355 
356 static const struct iio_trigger_ops afe4404_trigger_ops = {
357 };
358 
359 /* Default timings from data-sheet */
360 #define AFE4404_TIMING_PAIRS			\
361 	{ AFE440X_PRPCOUNT,	39999	},	\
362 	{ AFE440X_LED2LEDSTC,	0	},	\
363 	{ AFE440X_LED2LEDENDC,	398	},	\
364 	{ AFE440X_LED2STC,	80	},	\
365 	{ AFE440X_LED2ENDC,	398	},	\
366 	{ AFE440X_ADCRSTSTCT0,	5600	},	\
367 	{ AFE440X_ADCRSTENDCT0,	5606	},	\
368 	{ AFE440X_LED2CONVST,	5607	},	\
369 	{ AFE440X_LED2CONVEND,	6066	},	\
370 	{ AFE4404_LED3LEDSTC,	400	},	\
371 	{ AFE4404_LED3LEDENDC,	798	},	\
372 	{ AFE440X_ALED2STC,	480	},	\
373 	{ AFE440X_ALED2ENDC,	798	},	\
374 	{ AFE440X_ADCRSTSTCT1,	6068	},	\
375 	{ AFE440X_ADCRSTENDCT1,	6074	},	\
376 	{ AFE440X_ALED2CONVST,	6075	},	\
377 	{ AFE440X_ALED2CONVEND,	6534	},	\
378 	{ AFE440X_LED1LEDSTC,	800	},	\
379 	{ AFE440X_LED1LEDENDC,	1198	},	\
380 	{ AFE440X_LED1STC,	880	},	\
381 	{ AFE440X_LED1ENDC,	1198	},	\
382 	{ AFE440X_ADCRSTSTCT2,	6536	},	\
383 	{ AFE440X_ADCRSTENDCT2,	6542	},	\
384 	{ AFE440X_LED1CONVST,	6543	},	\
385 	{ AFE440X_LED1CONVEND,	7003	},	\
386 	{ AFE440X_ALED1STC,	1280	},	\
387 	{ AFE440X_ALED1ENDC,	1598	},	\
388 	{ AFE440X_ADCRSTSTCT3,	7005	},	\
389 	{ AFE440X_ADCRSTENDCT3,	7011	},	\
390 	{ AFE440X_ALED1CONVST,	7012	},	\
391 	{ AFE440X_ALED1CONVEND,	7471	},	\
392 	{ AFE440X_PDNCYCLESTC,	7671	},	\
393 	{ AFE440X_PDNCYCLEENDC,	39199	}
394 
395 static const struct reg_sequence afe4404_reg_sequences[] = {
396 	AFE4404_TIMING_PAIRS,
397 	{ AFE440X_CONTROL1, AFE440X_CONTROL1_TIMEREN },
398 	{ AFE4404_TIA_GAIN_SEP, AFE440X_TIAGAIN_ENSEPGAIN },
399 	{ AFE440X_CONTROL2, AFE440X_CONTROL2_OSC_ENABLE	},
400 };
401 
402 static const struct regmap_range afe4404_yes_ranges[] = {
403 	regmap_reg_range(AFE440X_LED2VAL, AFE440X_LED1_ALED1VAL),
404 	regmap_reg_range(AFE4404_AVG_LED2_ALED2VAL, AFE4404_AVG_LED1_ALED1VAL),
405 };
406 
407 static const struct regmap_access_table afe4404_volatile_table = {
408 	.yes_ranges = afe4404_yes_ranges,
409 	.n_yes_ranges = ARRAY_SIZE(afe4404_yes_ranges),
410 };
411 
412 static const struct regmap_config afe4404_regmap_config = {
413 	.reg_bits = 8,
414 	.val_bits = 24,
415 
416 	.max_register = AFE4404_AVG_LED1_ALED1VAL,
417 	.cache_type = REGCACHE_RBTREE,
418 	.volatile_table = &afe4404_volatile_table,
419 };
420 
421 static const struct of_device_id afe4404_of_match[] = {
422 	{ .compatible = "ti,afe4404", },
423 	{ /* sentinel */ }
424 };
425 MODULE_DEVICE_TABLE(of, afe4404_of_match);
426 
427 static int __maybe_unused afe4404_suspend(struct device *dev)
428 {
429 	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
430 	struct afe4404_data *afe = iio_priv(indio_dev);
431 	int ret;
432 
433 	ret = regmap_update_bits(afe->regmap, AFE440X_CONTROL2,
434 				 AFE440X_CONTROL2_PDN_AFE,
435 				 AFE440X_CONTROL2_PDN_AFE);
436 	if (ret)
437 		return ret;
438 
439 	ret = regulator_disable(afe->regulator);
440 	if (ret) {
441 		dev_err(dev, "Unable to disable regulator\n");
442 		return ret;
443 	}
444 
445 	return 0;
446 }
447 
448 static int __maybe_unused afe4404_resume(struct device *dev)
449 {
450 	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
451 	struct afe4404_data *afe = iio_priv(indio_dev);
452 	int ret;
453 
454 	ret = regulator_enable(afe->regulator);
455 	if (ret) {
456 		dev_err(dev, "Unable to enable regulator\n");
457 		return ret;
458 	}
459 
460 	ret = regmap_update_bits(afe->regmap, AFE440X_CONTROL2,
461 				 AFE440X_CONTROL2_PDN_AFE, 0);
462 	if (ret)
463 		return ret;
464 
465 	return 0;
466 }
467 
468 static SIMPLE_DEV_PM_OPS(afe4404_pm_ops, afe4404_suspend, afe4404_resume);
469 
470 static int afe4404_probe(struct i2c_client *client,
471 			 const struct i2c_device_id *id)
472 {
473 	struct iio_dev *indio_dev;
474 	struct afe4404_data *afe;
475 	int i, ret;
476 
477 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*afe));
478 	if (!indio_dev)
479 		return -ENOMEM;
480 
481 	afe = iio_priv(indio_dev);
482 	i2c_set_clientdata(client, indio_dev);
483 
484 	afe->dev = &client->dev;
485 	afe->irq = client->irq;
486 
487 	afe->regmap = devm_regmap_init_i2c(client, &afe4404_regmap_config);
488 	if (IS_ERR(afe->regmap)) {
489 		dev_err(afe->dev, "Unable to allocate register map\n");
490 		return PTR_ERR(afe->regmap);
491 	}
492 
493 	for (i = 0; i < F_MAX_FIELDS; i++) {
494 		afe->fields[i] = devm_regmap_field_alloc(afe->dev, afe->regmap,
495 							 afe4404_reg_fields[i]);
496 		if (IS_ERR(afe->fields[i])) {
497 			dev_err(afe->dev, "Unable to allocate regmap fields\n");
498 			return PTR_ERR(afe->fields[i]);
499 		}
500 	}
501 
502 	afe->regulator = devm_regulator_get(afe->dev, "tx_sup");
503 	if (IS_ERR(afe->regulator)) {
504 		dev_err(afe->dev, "Unable to get regulator\n");
505 		return PTR_ERR(afe->regulator);
506 	}
507 	ret = regulator_enable(afe->regulator);
508 	if (ret) {
509 		dev_err(afe->dev, "Unable to enable regulator\n");
510 		return ret;
511 	}
512 
513 	ret = regmap_write(afe->regmap, AFE440X_CONTROL0,
514 			   AFE440X_CONTROL0_SW_RESET);
515 	if (ret) {
516 		dev_err(afe->dev, "Unable to reset device\n");
517 		goto disable_reg;
518 	}
519 
520 	ret = regmap_multi_reg_write(afe->regmap, afe4404_reg_sequences,
521 				     ARRAY_SIZE(afe4404_reg_sequences));
522 	if (ret) {
523 		dev_err(afe->dev, "Unable to set register defaults\n");
524 		goto disable_reg;
525 	}
526 
527 	indio_dev->modes = INDIO_DIRECT_MODE;
528 	indio_dev->dev.parent = afe->dev;
529 	indio_dev->channels = afe4404_channels;
530 	indio_dev->num_channels = ARRAY_SIZE(afe4404_channels);
531 	indio_dev->name = AFE4404_DRIVER_NAME;
532 	indio_dev->info = &afe4404_iio_info;
533 
534 	if (afe->irq > 0) {
535 		afe->trig = devm_iio_trigger_alloc(afe->dev,
536 						   "%s-dev%d",
537 						   indio_dev->name,
538 						   indio_dev->id);
539 		if (!afe->trig) {
540 			dev_err(afe->dev, "Unable to allocate IIO trigger\n");
541 			ret = -ENOMEM;
542 			goto disable_reg;
543 		}
544 
545 		iio_trigger_set_drvdata(afe->trig, indio_dev);
546 
547 		afe->trig->ops = &afe4404_trigger_ops;
548 		afe->trig->dev.parent = afe->dev;
549 
550 		ret = iio_trigger_register(afe->trig);
551 		if (ret) {
552 			dev_err(afe->dev, "Unable to register IIO trigger\n");
553 			goto disable_reg;
554 		}
555 
556 		ret = devm_request_threaded_irq(afe->dev, afe->irq,
557 						iio_trigger_generic_data_rdy_poll,
558 						NULL, IRQF_ONESHOT,
559 						AFE4404_DRIVER_NAME,
560 						afe->trig);
561 		if (ret) {
562 			dev_err(afe->dev, "Unable to request IRQ\n");
563 			goto disable_reg;
564 		}
565 	}
566 
567 	ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
568 					 afe4404_trigger_handler, NULL);
569 	if (ret) {
570 		dev_err(afe->dev, "Unable to setup buffer\n");
571 		goto unregister_trigger;
572 	}
573 
574 	ret = iio_device_register(indio_dev);
575 	if (ret) {
576 		dev_err(afe->dev, "Unable to register IIO device\n");
577 		goto unregister_triggered_buffer;
578 	}
579 
580 	return 0;
581 
582 unregister_triggered_buffer:
583 	iio_triggered_buffer_cleanup(indio_dev);
584 unregister_trigger:
585 	if (afe->irq > 0)
586 		iio_trigger_unregister(afe->trig);
587 disable_reg:
588 	regulator_disable(afe->regulator);
589 
590 	return ret;
591 }
592 
593 static int afe4404_remove(struct i2c_client *client)
594 {
595 	struct iio_dev *indio_dev = i2c_get_clientdata(client);
596 	struct afe4404_data *afe = iio_priv(indio_dev);
597 	int ret;
598 
599 	iio_device_unregister(indio_dev);
600 
601 	iio_triggered_buffer_cleanup(indio_dev);
602 
603 	if (afe->irq > 0)
604 		iio_trigger_unregister(afe->trig);
605 
606 	ret = regulator_disable(afe->regulator);
607 	if (ret) {
608 		dev_err(afe->dev, "Unable to disable regulator\n");
609 		return ret;
610 	}
611 
612 	return 0;
613 }
614 
615 static const struct i2c_device_id afe4404_ids[] = {
616 	{ "afe4404", 0 },
617 	{ /* sentinel */ }
618 };
619 MODULE_DEVICE_TABLE(i2c, afe4404_ids);
620 
621 static struct i2c_driver afe4404_i2c_driver = {
622 	.driver = {
623 		.name = AFE4404_DRIVER_NAME,
624 		.of_match_table = afe4404_of_match,
625 		.pm = &afe4404_pm_ops,
626 	},
627 	.probe = afe4404_probe,
628 	.remove = afe4404_remove,
629 	.id_table = afe4404_ids,
630 };
631 module_i2c_driver(afe4404_i2c_driver);
632 
633 MODULE_AUTHOR("Andrew F. Davis <afd@ti.com>");
634 MODULE_DESCRIPTION("TI AFE4404 Heart Rate Monitor and Pulse Oximeter AFE");
635 MODULE_LICENSE("GPL v2");
636