18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * AFE440X Heart Rate Monitors and Low-Cost Pulse Oximeters
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2015 Texas Instruments Incorporated - https://www.ti.com/
68c2ecf20Sopenharmony_ci *	Andrew F. Davis <afd@ti.com>
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#ifndef _AFE440X_H
108c2ecf20Sopenharmony_ci#define _AFE440X_H
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci/* AFE440X registers */
138c2ecf20Sopenharmony_ci#define AFE440X_CONTROL0		0x00
148c2ecf20Sopenharmony_ci#define AFE440X_LED2STC			0x01
158c2ecf20Sopenharmony_ci#define AFE440X_LED2ENDC		0x02
168c2ecf20Sopenharmony_ci#define AFE440X_LED1LEDSTC		0x03
178c2ecf20Sopenharmony_ci#define AFE440X_LED1LEDENDC		0x04
188c2ecf20Sopenharmony_ci#define AFE440X_ALED2STC		0x05
198c2ecf20Sopenharmony_ci#define AFE440X_ALED2ENDC		0x06
208c2ecf20Sopenharmony_ci#define AFE440X_LED1STC			0x07
218c2ecf20Sopenharmony_ci#define AFE440X_LED1ENDC		0x08
228c2ecf20Sopenharmony_ci#define AFE440X_LED2LEDSTC		0x09
238c2ecf20Sopenharmony_ci#define AFE440X_LED2LEDENDC		0x0a
248c2ecf20Sopenharmony_ci#define AFE440X_ALED1STC		0x0b
258c2ecf20Sopenharmony_ci#define AFE440X_ALED1ENDC		0x0c
268c2ecf20Sopenharmony_ci#define AFE440X_LED2CONVST		0x0d
278c2ecf20Sopenharmony_ci#define AFE440X_LED2CONVEND		0x0e
288c2ecf20Sopenharmony_ci#define AFE440X_ALED2CONVST		0x0f
298c2ecf20Sopenharmony_ci#define AFE440X_ALED2CONVEND		0x10
308c2ecf20Sopenharmony_ci#define AFE440X_LED1CONVST		0x11
318c2ecf20Sopenharmony_ci#define AFE440X_LED1CONVEND		0x12
328c2ecf20Sopenharmony_ci#define AFE440X_ALED1CONVST		0x13
338c2ecf20Sopenharmony_ci#define AFE440X_ALED1CONVEND		0x14
348c2ecf20Sopenharmony_ci#define AFE440X_ADCRSTSTCT0		0x15
358c2ecf20Sopenharmony_ci#define AFE440X_ADCRSTENDCT0		0x16
368c2ecf20Sopenharmony_ci#define AFE440X_ADCRSTSTCT1		0x17
378c2ecf20Sopenharmony_ci#define AFE440X_ADCRSTENDCT1		0x18
388c2ecf20Sopenharmony_ci#define AFE440X_ADCRSTSTCT2		0x19
398c2ecf20Sopenharmony_ci#define AFE440X_ADCRSTENDCT2		0x1a
408c2ecf20Sopenharmony_ci#define AFE440X_ADCRSTSTCT3		0x1b
418c2ecf20Sopenharmony_ci#define AFE440X_ADCRSTENDCT3		0x1c
428c2ecf20Sopenharmony_ci#define AFE440X_PRPCOUNT		0x1d
438c2ecf20Sopenharmony_ci#define AFE440X_CONTROL1		0x1e
448c2ecf20Sopenharmony_ci#define AFE440X_LEDCNTRL		0x22
458c2ecf20Sopenharmony_ci#define AFE440X_CONTROL2		0x23
468c2ecf20Sopenharmony_ci#define AFE440X_ALARM			0x29
478c2ecf20Sopenharmony_ci#define AFE440X_LED2VAL			0x2a
488c2ecf20Sopenharmony_ci#define AFE440X_ALED2VAL		0x2b
498c2ecf20Sopenharmony_ci#define AFE440X_LED1VAL			0x2c
508c2ecf20Sopenharmony_ci#define AFE440X_ALED1VAL		0x2d
518c2ecf20Sopenharmony_ci#define AFE440X_LED2_ALED2VAL		0x2e
528c2ecf20Sopenharmony_ci#define AFE440X_LED1_ALED1VAL		0x2f
538c2ecf20Sopenharmony_ci#define AFE440X_CONTROL3		0x31
548c2ecf20Sopenharmony_ci#define AFE440X_PDNCYCLESTC		0x32
558c2ecf20Sopenharmony_ci#define AFE440X_PDNCYCLEENDC		0x33
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci/* CONTROL0 register fields */
588c2ecf20Sopenharmony_ci#define AFE440X_CONTROL0_REG_READ	BIT(0)
598c2ecf20Sopenharmony_ci#define AFE440X_CONTROL0_TM_COUNT_RST	BIT(1)
608c2ecf20Sopenharmony_ci#define AFE440X_CONTROL0_SW_RESET	BIT(3)
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci/* CONTROL1 register fields */
638c2ecf20Sopenharmony_ci#define AFE440X_CONTROL1_TIMEREN	BIT(8)
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci/* TIAGAIN register fields */
668c2ecf20Sopenharmony_ci#define AFE440X_TIAGAIN_ENSEPGAIN	BIT(15)
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci/* CONTROL2 register fields */
698c2ecf20Sopenharmony_ci#define AFE440X_CONTROL2_PDN_AFE	BIT(0)
708c2ecf20Sopenharmony_ci#define AFE440X_CONTROL2_PDN_RX		BIT(1)
718c2ecf20Sopenharmony_ci#define AFE440X_CONTROL2_DYNAMIC4	BIT(3)
728c2ecf20Sopenharmony_ci#define AFE440X_CONTROL2_DYNAMIC3	BIT(4)
738c2ecf20Sopenharmony_ci#define AFE440X_CONTROL2_DYNAMIC2	BIT(14)
748c2ecf20Sopenharmony_ci#define AFE440X_CONTROL2_DYNAMIC1	BIT(20)
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci/* CONTROL3 register fields */
778c2ecf20Sopenharmony_ci#define AFE440X_CONTROL3_CLKDIV		GENMASK(2, 0)
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci/* CONTROL0 values */
808c2ecf20Sopenharmony_ci#define AFE440X_CONTROL0_WRITE		0x0
818c2ecf20Sopenharmony_ci#define AFE440X_CONTROL0_READ		0x1
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci#define AFE440X_INTENSITY_CHAN(_index, _mask)			\
848c2ecf20Sopenharmony_ci	{							\
858c2ecf20Sopenharmony_ci		.type = IIO_INTENSITY,				\
868c2ecf20Sopenharmony_ci		.channel = _index,				\
878c2ecf20Sopenharmony_ci		.address = _index,				\
888c2ecf20Sopenharmony_ci		.scan_index = _index,				\
898c2ecf20Sopenharmony_ci		.scan_type = {					\
908c2ecf20Sopenharmony_ci				.sign = 's',			\
918c2ecf20Sopenharmony_ci				.realbits = 24,			\
928c2ecf20Sopenharmony_ci				.storagebits = 32,		\
938c2ecf20Sopenharmony_ci				.endianness = IIO_CPU,		\
948c2ecf20Sopenharmony_ci		},						\
958c2ecf20Sopenharmony_ci		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |	\
968c2ecf20Sopenharmony_ci			_mask,					\
978c2ecf20Sopenharmony_ci		.indexed = true,				\
988c2ecf20Sopenharmony_ci	}
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci#define AFE440X_CURRENT_CHAN(_index)				\
1018c2ecf20Sopenharmony_ci	{							\
1028c2ecf20Sopenharmony_ci		.type = IIO_CURRENT,				\
1038c2ecf20Sopenharmony_ci		.channel = _index,				\
1048c2ecf20Sopenharmony_ci		.address = _index,				\
1058c2ecf20Sopenharmony_ci		.scan_index = -1,				\
1068c2ecf20Sopenharmony_ci		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |	\
1078c2ecf20Sopenharmony_ci			BIT(IIO_CHAN_INFO_SCALE),		\
1088c2ecf20Sopenharmony_ci		.indexed = true,				\
1098c2ecf20Sopenharmony_ci		.output = true,					\
1108c2ecf20Sopenharmony_ci	}
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_cistruct afe440x_val_table {
1138c2ecf20Sopenharmony_ci	int integer;
1148c2ecf20Sopenharmony_ci	int fract;
1158c2ecf20Sopenharmony_ci};
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci#define AFE440X_TABLE_ATTR(_name, _table)				\
1188c2ecf20Sopenharmony_cistatic ssize_t _name ## _show(struct device *dev,			\
1198c2ecf20Sopenharmony_ci			      struct device_attribute *attr, char *buf)	\
1208c2ecf20Sopenharmony_ci{									\
1218c2ecf20Sopenharmony_ci	ssize_t len = 0;						\
1228c2ecf20Sopenharmony_ci	int i;								\
1238c2ecf20Sopenharmony_ci									\
1248c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(_table); i++)			\
1258c2ecf20Sopenharmony_ci		len += scnprintf(buf + len, PAGE_SIZE - len, "%d.%06u ", \
1268c2ecf20Sopenharmony_ci				 _table[i].integer,			\
1278c2ecf20Sopenharmony_ci				 _table[i].fract);			\
1288c2ecf20Sopenharmony_ci									\
1298c2ecf20Sopenharmony_ci	buf[len - 1] = '\n';						\
1308c2ecf20Sopenharmony_ci									\
1318c2ecf20Sopenharmony_ci	return len;							\
1328c2ecf20Sopenharmony_ci}									\
1338c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RO(_name)
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_cistruct afe440x_attr {
1368c2ecf20Sopenharmony_ci	struct device_attribute dev_attr;
1378c2ecf20Sopenharmony_ci	unsigned int field;
1388c2ecf20Sopenharmony_ci	const struct afe440x_val_table *val_table;
1398c2ecf20Sopenharmony_ci	unsigned int table_size;
1408c2ecf20Sopenharmony_ci};
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci#define to_afe440x_attr(_dev_attr)				\
1438c2ecf20Sopenharmony_ci	container_of(_dev_attr, struct afe440x_attr, dev_attr)
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci#define AFE440X_ATTR(_name, _field, _table)			\
1468c2ecf20Sopenharmony_ci	struct afe440x_attr afe440x_attr_##_name = {		\
1478c2ecf20Sopenharmony_ci		.dev_attr = __ATTR(_name, (S_IRUGO | S_IWUSR),	\
1488c2ecf20Sopenharmony_ci				   afe440x_show_register,	\
1498c2ecf20Sopenharmony_ci				   afe440x_store_register),	\
1508c2ecf20Sopenharmony_ci		.field = _field,				\
1518c2ecf20Sopenharmony_ci		.val_table = _table,				\
1528c2ecf20Sopenharmony_ci		.table_size = ARRAY_SIZE(_table),		\
1538c2ecf20Sopenharmony_ci	}
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci#endif /* _AFE440X_H */
156