136782dabSMiquel Raynal /* SPDX-License-Identifier: GPL-2.0-only */
201636eb9SPatil, Rachna /*
301636eb9SPatil, Rachna  * TI Touch Screen / ADC MFD driver
401636eb9SPatil, Rachna  *
54f4ed454SAlexander A. Klimov  * Copyright (C) 2012 Texas Instruments Incorporated - https://www.ti.com/
601636eb9SPatil, Rachna  */
701636eb9SPatil, Rachna 
836782dabSMiquel Raynal #ifndef __LINUX_TI_AM335X_TSCADC_MFD_H
936782dabSMiquel Raynal #define __LINUX_TI_AM335X_TSCADC_MFD_H
1036782dabSMiquel Raynal 
1101636eb9SPatil, Rachna #include <linux/mfd/core.h>
12*48959fcdSMiquel Raynal #include <linux/units.h>
1301636eb9SPatil, Rachna 
1401636eb9SPatil, Rachna #define REG_RAWIRQSTATUS	0x024
1501636eb9SPatil, Rachna #define REG_IRQSTATUS		0x028
1601636eb9SPatil, Rachna #define REG_IRQENABLE		0x02C
1701636eb9SPatil, Rachna #define REG_IRQCLR		0x030
1801636eb9SPatil, Rachna #define REG_IRQWAKEUP		0x034
19f438b9daSMugunthan V N #define REG_DMAENABLE_SET	0x038
20f438b9daSMugunthan V N #define REG_DMAENABLE_CLEAR	0x03c
2101636eb9SPatil, Rachna #define REG_CTRL		0x040
2201636eb9SPatil, Rachna #define REG_ADCFSM		0x044
2301636eb9SPatil, Rachna #define REG_CLKDIV		0x04C
2401636eb9SPatil, Rachna #define REG_SE			0x054
2501636eb9SPatil, Rachna #define REG_IDLECONFIG		0x058
2601636eb9SPatil, Rachna #define REG_CHARGECONFIG	0x05C
2701636eb9SPatil, Rachna #define REG_CHARGEDELAY		0x060
288c896308SSebastian Andrzej Siewior #define REG_STEPCONFIG(n)	(0x64 + ((n) * 8))
298c896308SSebastian Andrzej Siewior #define REG_STEPDELAY(n)	(0x68 + ((n) * 8))
3001636eb9SPatil, Rachna #define REG_FIFO0CNT		0xE4
3101636eb9SPatil, Rachna #define REG_FIFO0THR		0xE8
3201636eb9SPatil, Rachna #define REG_FIFO1CNT		0xF0
3301636eb9SPatil, Rachna #define REG_FIFO1THR		0xF4
34f438b9daSMugunthan V N #define REG_DMA1REQ		0xF8
3501636eb9SPatil, Rachna #define REG_FIFO0		0x100
3601636eb9SPatil, Rachna #define REG_FIFO1		0x200
3701636eb9SPatil, Rachna 
3801636eb9SPatil, Rachna /*	Register Bitfields	*/
3901636eb9SPatil, Rachna /* IRQ wakeup enable */
4001636eb9SPatil, Rachna #define IRQWKUP_ENB		BIT(0)
4101636eb9SPatil, Rachna 
4201636eb9SPatil, Rachna /* Step Enable */
4301636eb9SPatil, Rachna #define STEPENB_MASK		(0x1FFFF << 0)
4401636eb9SPatil, Rachna #define STEPENB(val)		((val) << 0)
45ca9a5638SZubair Lutfullah #define ENB(val)		(1 << (val))
46ca9a5638SZubair Lutfullah #define STPENB_STEPENB		STEPENB(0x1FFFF)
47ca9a5638SZubair Lutfullah #define STPENB_STEPENB_TC	STEPENB(0x1FFF)
4801636eb9SPatil, Rachna 
4901636eb9SPatil, Rachna /* IRQ enable */
5001636eb9SPatil, Rachna #define IRQENB_HW_PEN		BIT(0)
51344d635bSBrad Griffis #define IRQENB_EOS		BIT(1)
5201636eb9SPatil, Rachna #define IRQENB_FIFO0THRES	BIT(2)
53ca9a5638SZubair Lutfullah #define IRQENB_FIFO0OVRRUN	BIT(3)
54ca9a5638SZubair Lutfullah #define IRQENB_FIFO0UNDRFLW	BIT(4)
5501636eb9SPatil, Rachna #define IRQENB_FIFO1THRES	BIT(5)
56ca9a5638SZubair Lutfullah #define IRQENB_FIFO1OVRRUN	BIT(6)
57ca9a5638SZubair Lutfullah #define IRQENB_FIFO1UNDRFLW	BIT(7)
5801636eb9SPatil, Rachna #define IRQENB_PENUP		BIT(9)
5901636eb9SPatil, Rachna 
6001636eb9SPatil, Rachna /* Step Configuration */
6101636eb9SPatil, Rachna #define STEPCONFIG_MODE_MASK	(3 << 0)
6201636eb9SPatil, Rachna #define STEPCONFIG_MODE(val)	((val) << 0)
63ca9a5638SZubair Lutfullah #define STEPCONFIG_MODE_SWCNT	STEPCONFIG_MODE(1)
6401636eb9SPatil, Rachna #define STEPCONFIG_MODE_HWSYNC	STEPCONFIG_MODE(2)
6501636eb9SPatil, Rachna #define STEPCONFIG_AVG_MASK	(7 << 2)
6601636eb9SPatil, Rachna #define STEPCONFIG_AVG(val)	((val) << 2)
6701636eb9SPatil, Rachna #define STEPCONFIG_AVG_16	STEPCONFIG_AVG(4)
6801636eb9SPatil, Rachna #define STEPCONFIG_XPP		BIT(5)
6901636eb9SPatil, Rachna #define STEPCONFIG_XNN		BIT(6)
7001636eb9SPatil, Rachna #define STEPCONFIG_YPP		BIT(7)
7101636eb9SPatil, Rachna #define STEPCONFIG_YNN		BIT(8)
7201636eb9SPatil, Rachna #define STEPCONFIG_XNP		BIT(9)
7301636eb9SPatil, Rachna #define STEPCONFIG_YPN		BIT(10)
744b3ab937SVignesh R #define STEPCONFIG_RFP(val)	((val) << 12)
754b3ab937SVignesh R #define STEPCONFIG_RFP_VREFP	(0x3 << 12)
7601636eb9SPatil, Rachna #define STEPCONFIG_INM_MASK	(0xF << 15)
7701636eb9SPatil, Rachna #define STEPCONFIG_INM(val)	((val) << 15)
7801636eb9SPatil, Rachna #define STEPCONFIG_INM_ADCREFM	STEPCONFIG_INM(8)
7901636eb9SPatil, Rachna #define STEPCONFIG_INP_MASK	(0xF << 19)
8001636eb9SPatil, Rachna #define STEPCONFIG_INP(val)	((val) << 19)
8101636eb9SPatil, Rachna #define STEPCONFIG_INP_AN4	STEPCONFIG_INP(4)
8201636eb9SPatil, Rachna #define STEPCONFIG_INP_ADCREFM	STEPCONFIG_INP(8)
8301636eb9SPatil, Rachna #define STEPCONFIG_FIFO1	BIT(26)
844b3ab937SVignesh R #define STEPCONFIG_RFM(val)	((val) << 23)
854b3ab937SVignesh R #define STEPCONFIG_RFM_VREFN	(0x3 << 23)
8601636eb9SPatil, Rachna 
8701636eb9SPatil, Rachna /* Delay register */
8801636eb9SPatil, Rachna #define STEPDELAY_OPEN_MASK	(0x3FFFF << 0)
8901636eb9SPatil, Rachna #define STEPDELAY_OPEN(val)	((val) << 0)
9001636eb9SPatil, Rachna #define STEPCONFIG_OPENDLY	STEPDELAY_OPEN(0x098)
9101636eb9SPatil, Rachna #define STEPDELAY_SAMPLE_MASK	(0xFF << 24)
9201636eb9SPatil, Rachna #define STEPDELAY_SAMPLE(val)	((val) << 24)
9301636eb9SPatil, Rachna #define STEPCONFIG_SAMPLEDLY	STEPDELAY_SAMPLE(0)
9401636eb9SPatil, Rachna 
9501636eb9SPatil, Rachna /* Charge Config */
9601636eb9SPatil, Rachna #define STEPCHARGE_RFP_MASK	(7 << 12)
9701636eb9SPatil, Rachna #define STEPCHARGE_RFP(val)	((val) << 12)
9801636eb9SPatil, Rachna #define STEPCHARGE_RFP_XPUL	STEPCHARGE_RFP(1)
9901636eb9SPatil, Rachna #define STEPCHARGE_INM_MASK	(0xF << 15)
10001636eb9SPatil, Rachna #define STEPCHARGE_INM(val)	((val) << 15)
10101636eb9SPatil, Rachna #define STEPCHARGE_INM_AN1	STEPCHARGE_INM(1)
10201636eb9SPatil, Rachna #define STEPCHARGE_INP_MASK	(0xF << 19)
10301636eb9SPatil, Rachna #define STEPCHARGE_INP(val)	((val) << 19)
10401636eb9SPatil, Rachna #define STEPCHARGE_RFM_MASK	(3 << 23)
10501636eb9SPatil, Rachna #define STEPCHARGE_RFM(val)	((val) << 23)
10601636eb9SPatil, Rachna #define STEPCHARGE_RFM_XNUR	STEPCHARGE_RFM(1)
10701636eb9SPatil, Rachna 
10801636eb9SPatil, Rachna /* Charge delay */
10901636eb9SPatil, Rachna #define CHARGEDLY_OPEN_MASK	(0x3FFFF << 0)
11001636eb9SPatil, Rachna #define CHARGEDLY_OPEN(val)	((val) << 0)
111344d635bSBrad Griffis #define CHARGEDLY_OPENDLY	CHARGEDLY_OPEN(0x400)
11201636eb9SPatil, Rachna 
11301636eb9SPatil, Rachna /* Control register */
11401636eb9SPatil, Rachna #define CNTRLREG_TSCSSENB	BIT(0)
11501636eb9SPatil, Rachna #define CNTRLREG_STEPID		BIT(1)
11601636eb9SPatil, Rachna #define CNTRLREG_STEPCONFIGWRT	BIT(2)
11701636eb9SPatil, Rachna #define CNTRLREG_POWERDOWN	BIT(4)
11801636eb9SPatil, Rachna #define CNTRLREG_AFE_CTRL_MASK	(3 << 5)
11901636eb9SPatil, Rachna #define CNTRLREG_AFE_CTRL(val)	((val) << 5)
12001636eb9SPatil, Rachna #define CNTRLREG_4WIRE		CNTRLREG_AFE_CTRL(1)
12101636eb9SPatil, Rachna #define CNTRLREG_5WIRE		CNTRLREG_AFE_CTRL(2)
12201636eb9SPatil, Rachna #define CNTRLREG_8WIRE		CNTRLREG_AFE_CTRL(3)
12301636eb9SPatil, Rachna #define CNTRLREG_TSCENB		BIT(7)
12401636eb9SPatil, Rachna 
125b1451e54SPatil, Rachna /* FIFO READ Register */
126b1451e54SPatil, Rachna #define FIFOREAD_DATA_MASK	(0xfff << 0)
127b1451e54SPatil, Rachna #define FIFOREAD_CHNLID_MASK	(0xf << 16)
128b1451e54SPatil, Rachna 
129f438b9daSMugunthan V N /* DMA ENABLE/CLEAR Register */
130f438b9daSMugunthan V N #define DMA_FIFO0		BIT(0)
131f438b9daSMugunthan V N #define DMA_FIFO1		BIT(1)
132f438b9daSMugunthan V N 
133b1451e54SPatil, Rachna /* Sequencer Status */
134b1451e54SPatil, Rachna #define SEQ_STATUS		BIT(5)
135b10848e6SVignesh R #define CHARGE_STEP		0x11
136b1451e54SPatil, Rachna 
137*48959fcdSMiquel Raynal #define ADC_CLK			(3 * HZ_PER_MHZ)
1385e53a69bSPatil, Rachna #define TOTAL_STEPS		16
1395e53a69bSPatil, Rachna #define TOTAL_CHANNELS		8
140ca9a5638SZubair Lutfullah #define FIFO1_THRESHOLD		19
14101636eb9SPatil, Rachna 
142b1451e54SPatil, Rachna /*
1431a54b7daSMatthias Kaehlcke  * time in us for processing a single channel, calculated as follows:
1441a54b7daSMatthias Kaehlcke  *
1457175cce1SVignesh R  * max num cycles = open delay + (sample delay + conv time) * averaging
1461a54b7daSMatthias Kaehlcke  *
1477175cce1SVignesh R  * max num cycles: 262143 + (255 + 13) * 16 = 266431
1481a54b7daSMatthias Kaehlcke  *
1491a54b7daSMatthias Kaehlcke  * clock frequency: 26MHz / 8 = 3.25MHz
1501a54b7daSMatthias Kaehlcke  * clock period: 1 / 3.25MHz = 308ns
1511a54b7daSMatthias Kaehlcke  *
1527175cce1SVignesh R  * max processing time: 266431 * 308ns = 83ms(approx)
153b1451e54SPatil, Rachna  */
1547175cce1SVignesh R #define IDLE_TIMEOUT		83 /* milliseconds */
155b1451e54SPatil, Rachna 
1565e53a69bSPatil, Rachna #define TSCADC_CELLS		2
1572b99bafaSPatil, Rachna 
158f7834843SMiquel Raynal struct ti_tscadc_data {
159f7834843SMiquel Raynal 	char *adc_feature_name;
160f7834843SMiquel Raynal 	char *adc_feature_compatible;
161f7834843SMiquel Raynal 	char *secondary_feature_name;
162f7834843SMiquel Raynal 	char *secondary_feature_compatible;
163f7834843SMiquel Raynal 	unsigned int target_clk_rate;
164f7834843SMiquel Raynal };
165f7834843SMiquel Raynal 
16601636eb9SPatil, Rachna struct ti_tscadc_dev {
16701636eb9SPatil, Rachna 	struct device *dev;
1680d3a7cceSAndrew F. Davis 	struct regmap *regmap;
16901636eb9SPatil, Rachna 	void __iomem *tscadc_base;
170c9329d86SMugunthan V N 	phys_addr_t tscadc_phys_base;
171f7834843SMiquel Raynal 	const struct ti_tscadc_data *data;
17201636eb9SPatil, Rachna 	int irq;
17301636eb9SPatil, Rachna 	struct mfd_cell cells[TSCADC_CELLS];
174b813f320SMiquel Raynal 	u32 ctrl;
175abeccee4SPatil, Rachna 	u32 reg_se_cache;
1767ca6740cSSebastian Andrzej Siewior 	bool adc_waiting;
1777ca6740cSSebastian Andrzej Siewior 	bool adc_in_use;
1787ca6740cSSebastian Andrzej Siewior 	wait_queue_head_t reg_se_wait;
179abeccee4SPatil, Rachna 	spinlock_t reg_lock;
180e90f8754SMatthias Kaehlcke 	unsigned int clk_div;
1812b99bafaSPatil, Rachna 
1822b99bafaSPatil, Rachna 	/* tsc device */
1832b99bafaSPatil, Rachna 	struct titsc *tsc;
1845e53a69bSPatil, Rachna 
1855e53a69bSPatil, Rachna 	/* adc device */
1865e53a69bSPatil, Rachna 	struct adc_device *adc;
18701636eb9SPatil, Rachna };
18801636eb9SPatil, Rachna 
189a9bce1b0SSebastian Andrzej Siewior static inline struct ti_tscadc_dev *ti_tscadc_dev_get(struct platform_device *p)
190a9bce1b0SSebastian Andrzej Siewior {
191a9bce1b0SSebastian Andrzej Siewior 	struct ti_tscadc_dev **tscadc_dev = p->dev.platform_data;
192a9bce1b0SSebastian Andrzej Siewior 
193a9bce1b0SSebastian Andrzej Siewior 	return *tscadc_dev;
194a9bce1b0SSebastian Andrzej Siewior }
195a9bce1b0SSebastian Andrzej Siewior 
1967e170c6eSSebastian Andrzej Siewior void am335x_tsc_se_set_cache(struct ti_tscadc_dev *tsadc, u32 val);
1977e170c6eSSebastian Andrzej Siewior void am335x_tsc_se_set_once(struct ti_tscadc_dev *tsadc, u32 val);
198abeccee4SPatil, Rachna void am335x_tsc_se_clr(struct ti_tscadc_dev *tsadc, u32 val);
1997ca6740cSSebastian Andrzej Siewior void am335x_tsc_se_adc_done(struct ti_tscadc_dev *tsadc);
200abeccee4SPatil, Rachna 
20101636eb9SPatil, Rachna #endif
202