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 
11*b7cb7bf1SMiquel Raynal #include <linux/bitfield.h>
1201636eb9SPatil, Rachna #include <linux/mfd/core.h>
1348959fcdSMiquel Raynal #include <linux/units.h>
1401636eb9SPatil, Rachna 
1501636eb9SPatil, Rachna #define REG_RAWIRQSTATUS	0x024
1601636eb9SPatil, Rachna #define REG_IRQSTATUS		0x028
1701636eb9SPatil, Rachna #define REG_IRQENABLE		0x02C
1801636eb9SPatil, Rachna #define REG_IRQCLR		0x030
1901636eb9SPatil, Rachna #define REG_IRQWAKEUP		0x034
20f438b9daSMugunthan V N #define REG_DMAENABLE_SET	0x038
21f438b9daSMugunthan V N #define REG_DMAENABLE_CLEAR	0x03c
2201636eb9SPatil, Rachna #define REG_CTRL		0x040
2301636eb9SPatil, Rachna #define REG_ADCFSM		0x044
2401636eb9SPatil, Rachna #define REG_CLKDIV		0x04C
2501636eb9SPatil, Rachna #define REG_SE			0x054
2601636eb9SPatil, Rachna #define REG_IDLECONFIG		0x058
2701636eb9SPatil, Rachna #define REG_CHARGECONFIG	0x05C
2801636eb9SPatil, Rachna #define REG_CHARGEDELAY		0x060
298c896308SSebastian Andrzej Siewior #define REG_STEPCONFIG(n)	(0x64 + ((n) * 8))
308c896308SSebastian Andrzej Siewior #define REG_STEPDELAY(n)	(0x68 + ((n) * 8))
3101636eb9SPatil, Rachna #define REG_FIFO0CNT		0xE4
3201636eb9SPatil, Rachna #define REG_FIFO0THR		0xE8
3301636eb9SPatil, Rachna #define REG_FIFO1CNT		0xF0
3401636eb9SPatil, Rachna #define REG_FIFO1THR		0xF4
35f438b9daSMugunthan V N #define REG_DMA1REQ		0xF8
3601636eb9SPatil, Rachna #define REG_FIFO0		0x100
3701636eb9SPatil, Rachna #define REG_FIFO1		0x200
3801636eb9SPatil, Rachna 
3901636eb9SPatil, Rachna /*	Register Bitfields	*/
4001636eb9SPatil, Rachna /* IRQ wakeup enable */
4101636eb9SPatil, Rachna #define IRQWKUP_ENB		BIT(0)
4201636eb9SPatil, Rachna 
4301636eb9SPatil, Rachna /* IRQ enable */
4401636eb9SPatil, Rachna #define IRQENB_HW_PEN		BIT(0)
45344d635bSBrad Griffis #define IRQENB_EOS		BIT(1)
4601636eb9SPatil, Rachna #define IRQENB_FIFO0THRES	BIT(2)
47ca9a5638SZubair Lutfullah #define IRQENB_FIFO0OVRRUN	BIT(3)
48ca9a5638SZubair Lutfullah #define IRQENB_FIFO0UNDRFLW	BIT(4)
4901636eb9SPatil, Rachna #define IRQENB_FIFO1THRES	BIT(5)
50ca9a5638SZubair Lutfullah #define IRQENB_FIFO1OVRRUN	BIT(6)
51ca9a5638SZubair Lutfullah #define IRQENB_FIFO1UNDRFLW	BIT(7)
5201636eb9SPatil, Rachna #define IRQENB_PENUP		BIT(9)
5301636eb9SPatil, Rachna 
5401636eb9SPatil, Rachna /* Step Configuration */
55*b7cb7bf1SMiquel Raynal #define STEPCONFIG_MODE_MASK	GENMASK(1, 0)
56*b7cb7bf1SMiquel Raynal #define STEPCONFIG_MODE(val)	FIELD_PREP(STEPCONFIG_MODE_MASK, (val))
57ca9a5638SZubair Lutfullah #define STEPCONFIG_MODE_SWCNT	STEPCONFIG_MODE(1)
5801636eb9SPatil, Rachna #define STEPCONFIG_MODE_HWSYNC	STEPCONFIG_MODE(2)
59*b7cb7bf1SMiquel Raynal #define STEPCONFIG_AVG_MASK	GENMASK(4, 2)
60*b7cb7bf1SMiquel Raynal #define STEPCONFIG_AVG(val)	FIELD_PREP(STEPCONFIG_AVG_MASK, (val))
6101636eb9SPatil, Rachna #define STEPCONFIG_AVG_16	STEPCONFIG_AVG(4)
6201636eb9SPatil, Rachna #define STEPCONFIG_XPP		BIT(5)
6301636eb9SPatil, Rachna #define STEPCONFIG_XNN		BIT(6)
6401636eb9SPatil, Rachna #define STEPCONFIG_YPP		BIT(7)
6501636eb9SPatil, Rachna #define STEPCONFIG_YNN		BIT(8)
6601636eb9SPatil, Rachna #define STEPCONFIG_XNP		BIT(9)
6701636eb9SPatil, Rachna #define STEPCONFIG_YPN		BIT(10)
68*b7cb7bf1SMiquel Raynal #define STEPCONFIG_RFP_VREFP	GENMASK(13, 12)
69*b7cb7bf1SMiquel Raynal #define STEPCONFIG_RFP(val)	FIELD_PREP(STEPCONFIG_RFP_VREFP, (val))
70*b7cb7bf1SMiquel Raynal #define STEPCONFIG_INM_MASK	GENMASK(18, 15)
71*b7cb7bf1SMiquel Raynal #define STEPCONFIG_INM(val)	FIELD_PREP(STEPCONFIG_INM_MASK, (val))
7201636eb9SPatil, Rachna #define STEPCONFIG_INM_ADCREFM	STEPCONFIG_INM(8)
73*b7cb7bf1SMiquel Raynal #define STEPCONFIG_INP_MASK	GENMASK(22, 19)
74*b7cb7bf1SMiquel Raynal #define STEPCONFIG_INP(val)	FIELD_PREP(STEPCONFIG_INP_MASK, (val))
7501636eb9SPatil, Rachna #define STEPCONFIG_INP_AN4	STEPCONFIG_INP(4)
7601636eb9SPatil, Rachna #define STEPCONFIG_INP_ADCREFM	STEPCONFIG_INP(8)
7701636eb9SPatil, Rachna #define STEPCONFIG_FIFO1	BIT(26)
78*b7cb7bf1SMiquel Raynal #define STEPCONFIG_RFM_VREFN	GENMASK(24, 23)
79*b7cb7bf1SMiquel Raynal #define STEPCONFIG_RFM(val)	FIELD_PREP(STEPCONFIG_RFM_VREFN, (val))
8001636eb9SPatil, Rachna 
8101636eb9SPatil, Rachna /* Delay register */
82*b7cb7bf1SMiquel Raynal #define STEPDELAY_OPEN_MASK	GENMASK(17, 0)
83*b7cb7bf1SMiquel Raynal #define STEPDELAY_OPEN(val)	FIELD_PREP(STEPDELAY_OPEN_MASK, (val))
8401636eb9SPatil, Rachna #define STEPCONFIG_OPENDLY	STEPDELAY_OPEN(0x098)
85*b7cb7bf1SMiquel Raynal #define STEPDELAY_SAMPLE_MASK	GENMASK(31, 24)
86*b7cb7bf1SMiquel Raynal #define STEPDELAY_SAMPLE(val)	FIELD_PREP(STEPDELAY_SAMPLE_MASK, (val))
8701636eb9SPatil, Rachna #define STEPCONFIG_SAMPLEDLY	STEPDELAY_SAMPLE(0)
8801636eb9SPatil, Rachna 
8901636eb9SPatil, Rachna /* Charge Config */
90*b7cb7bf1SMiquel Raynal #define STEPCHARGE_RFP_MASK	GENMASK(14, 12)
91*b7cb7bf1SMiquel Raynal #define STEPCHARGE_RFP(val)	FIELD_PREP(STEPCHARGE_RFP_MASK, (val))
9201636eb9SPatil, Rachna #define STEPCHARGE_RFP_XPUL	STEPCHARGE_RFP(1)
93*b7cb7bf1SMiquel Raynal #define STEPCHARGE_INM_MASK	GENMASK(18, 15)
94*b7cb7bf1SMiquel Raynal #define STEPCHARGE_INM(val)	FIELD_PREP(STEPCHARGE_INM_MASK, (val))
9501636eb9SPatil, Rachna #define STEPCHARGE_INM_AN1	STEPCHARGE_INM(1)
96*b7cb7bf1SMiquel Raynal #define STEPCHARGE_INP_MASK	GENMASK(22, 19)
97*b7cb7bf1SMiquel Raynal #define STEPCHARGE_INP(val)	FIELD_PREP(STEPCHARGE_INP_MASK, (val))
98*b7cb7bf1SMiquel Raynal #define STEPCHARGE_RFM_MASK	GENMASK(24, 23)
99*b7cb7bf1SMiquel Raynal #define STEPCHARGE_RFM(val)	FIELD_PREP(STEPCHARGE_RFM_MASK, (val))
10001636eb9SPatil, Rachna #define STEPCHARGE_RFM_XNUR	STEPCHARGE_RFM(1)
10101636eb9SPatil, Rachna 
10201636eb9SPatil, Rachna /* Charge delay */
103*b7cb7bf1SMiquel Raynal #define CHARGEDLY_OPEN_MASK	GENMASK(17, 0)
104*b7cb7bf1SMiquel Raynal #define CHARGEDLY_OPEN(val)	FIELD_PREP(CHARGEDLY_OPEN_MASK, (val))
105344d635bSBrad Griffis #define CHARGEDLY_OPENDLY	CHARGEDLY_OPEN(0x400)
10601636eb9SPatil, Rachna 
10701636eb9SPatil, Rachna /* Control register */
10801636eb9SPatil, Rachna #define CNTRLREG_TSCSSENB	BIT(0)
10901636eb9SPatil, Rachna #define CNTRLREG_STEPID		BIT(1)
11001636eb9SPatil, Rachna #define CNTRLREG_STEPCONFIGWRT	BIT(2)
11101636eb9SPatil, Rachna #define CNTRLREG_POWERDOWN	BIT(4)
112*b7cb7bf1SMiquel Raynal #define CNTRLREG_AFE_CTRL_MASK	GENMASK(6, 5)
113*b7cb7bf1SMiquel Raynal #define CNTRLREG_AFE_CTRL(val)	FIELD_PREP(CNTRLREG_AFE_CTRL_MASK, (val))
11401636eb9SPatil, Rachna #define CNTRLREG_4WIRE		CNTRLREG_AFE_CTRL(1)
11501636eb9SPatil, Rachna #define CNTRLREG_5WIRE		CNTRLREG_AFE_CTRL(2)
11601636eb9SPatil, Rachna #define CNTRLREG_8WIRE		CNTRLREG_AFE_CTRL(3)
11701636eb9SPatil, Rachna #define CNTRLREG_TSCENB		BIT(7)
11801636eb9SPatil, Rachna 
119b1451e54SPatil, Rachna /* FIFO READ Register */
120*b7cb7bf1SMiquel Raynal #define FIFOREAD_DATA_MASK	GENMASK(11, 0)
121*b7cb7bf1SMiquel Raynal #define FIFOREAD_CHNLID_MASK	GENMASK(19, 16)
122b1451e54SPatil, Rachna 
123f438b9daSMugunthan V N /* DMA ENABLE/CLEAR Register */
124f438b9daSMugunthan V N #define DMA_FIFO0		BIT(0)
125f438b9daSMugunthan V N #define DMA_FIFO1		BIT(1)
126f438b9daSMugunthan V N 
127b1451e54SPatil, Rachna /* Sequencer Status */
128b1451e54SPatil, Rachna #define SEQ_STATUS		BIT(5)
129b10848e6SVignesh R #define CHARGE_STEP		0x11
130b1451e54SPatil, Rachna 
13148959fcdSMiquel Raynal #define ADC_CLK			(3 * HZ_PER_MHZ)
1325e53a69bSPatil, Rachna #define TOTAL_STEPS		16
1335e53a69bSPatil, Rachna #define TOTAL_CHANNELS		8
134ca9a5638SZubair Lutfullah #define FIFO1_THRESHOLD		19
13501636eb9SPatil, Rachna 
136b1451e54SPatil, Rachna /*
1371a54b7daSMatthias Kaehlcke  * time in us for processing a single channel, calculated as follows:
1381a54b7daSMatthias Kaehlcke  *
1397175cce1SVignesh R  * max num cycles = open delay + (sample delay + conv time) * averaging
1401a54b7daSMatthias Kaehlcke  *
1417175cce1SVignesh R  * max num cycles: 262143 + (255 + 13) * 16 = 266431
1421a54b7daSMatthias Kaehlcke  *
1431a54b7daSMatthias Kaehlcke  * clock frequency: 26MHz / 8 = 3.25MHz
1441a54b7daSMatthias Kaehlcke  * clock period: 1 / 3.25MHz = 308ns
1451a54b7daSMatthias Kaehlcke  *
1467175cce1SVignesh R  * max processing time: 266431 * 308ns = 83ms(approx)
147b1451e54SPatil, Rachna  */
1487175cce1SVignesh R #define IDLE_TIMEOUT		83 /* milliseconds */
149b1451e54SPatil, Rachna 
1505e53a69bSPatil, Rachna #define TSCADC_CELLS		2
1512b99bafaSPatil, Rachna 
152f7834843SMiquel Raynal struct ti_tscadc_data {
153f7834843SMiquel Raynal 	char *adc_feature_name;
154f7834843SMiquel Raynal 	char *adc_feature_compatible;
155f7834843SMiquel Raynal 	char *secondary_feature_name;
156f7834843SMiquel Raynal 	char *secondary_feature_compatible;
157f7834843SMiquel Raynal 	unsigned int target_clk_rate;
158f7834843SMiquel Raynal };
159f7834843SMiquel Raynal 
16001636eb9SPatil, Rachna struct ti_tscadc_dev {
16101636eb9SPatil, Rachna 	struct device *dev;
1620d3a7cceSAndrew F. Davis 	struct regmap *regmap;
16301636eb9SPatil, Rachna 	void __iomem *tscadc_base;
164c9329d86SMugunthan V N 	phys_addr_t tscadc_phys_base;
165f7834843SMiquel Raynal 	const struct ti_tscadc_data *data;
16601636eb9SPatil, Rachna 	int irq;
16701636eb9SPatil, Rachna 	struct mfd_cell cells[TSCADC_CELLS];
168b813f320SMiquel Raynal 	u32 ctrl;
169abeccee4SPatil, Rachna 	u32 reg_se_cache;
1707ca6740cSSebastian Andrzej Siewior 	bool adc_waiting;
1717ca6740cSSebastian Andrzej Siewior 	bool adc_in_use;
1727ca6740cSSebastian Andrzej Siewior 	wait_queue_head_t reg_se_wait;
173abeccee4SPatil, Rachna 	spinlock_t reg_lock;
174e90f8754SMatthias Kaehlcke 	unsigned int clk_div;
1752b99bafaSPatil, Rachna 
1762b99bafaSPatil, Rachna 	/* tsc device */
1772b99bafaSPatil, Rachna 	struct titsc *tsc;
1785e53a69bSPatil, Rachna 
1795e53a69bSPatil, Rachna 	/* adc device */
1805e53a69bSPatil, Rachna 	struct adc_device *adc;
18101636eb9SPatil, Rachna };
18201636eb9SPatil, Rachna 
183a9bce1b0SSebastian Andrzej Siewior static inline struct ti_tscadc_dev *ti_tscadc_dev_get(struct platform_device *p)
184a9bce1b0SSebastian Andrzej Siewior {
185a9bce1b0SSebastian Andrzej Siewior 	struct ti_tscadc_dev **tscadc_dev = p->dev.platform_data;
186a9bce1b0SSebastian Andrzej Siewior 
187a9bce1b0SSebastian Andrzej Siewior 	return *tscadc_dev;
188a9bce1b0SSebastian Andrzej Siewior }
189a9bce1b0SSebastian Andrzej Siewior 
1907e170c6eSSebastian Andrzej Siewior void am335x_tsc_se_set_cache(struct ti_tscadc_dev *tsadc, u32 val);
1917e170c6eSSebastian Andrzej Siewior void am335x_tsc_se_set_once(struct ti_tscadc_dev *tsadc, u32 val);
192abeccee4SPatil, Rachna void am335x_tsc_se_clr(struct ti_tscadc_dev *tsadc, u32 val);
1937ca6740cSSebastian Andrzej Siewior void am335x_tsc_se_adc_done(struct ti_tscadc_dev *tsadc);
194abeccee4SPatil, Rachna 
19501636eb9SPatil, Rachna #endif
196