summaryrefslogtreecommitdiffstats
path: root/package/gpio_rtl8196c/src/gpio_rtl8196c.c
blob: c2aae8d674f0b6dcbac248d406ab35c4c7ee6394 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
 * 
 *
 * Copyright (C)2006-2008 by 
 * All rights reserved.
 *
 */
 
#include <linux/init.h>
#include <linux/module.h>
#include <linux/gpio.h>
#include <linux/stat.h>
#include <linux/moduleparam.h>
#include <linux/irq.h>

////////////////////////////////////////////////////////////////////////
//DEBUG macroses
#define MOD_NAME "gpio_rtl8196c"

#ifdef DEBUG
	#define PRINT( format, args ...) printk( "%15s:%3d = " format, MOD_NAME,__LINE__, ##args)
#else
	#define PRINT( format, args ...) ;
#endif

////////////////////////////////////////////////////////////////////////
#define RTL8196C_BASE_ADDR		0xb8000000
#define RTL8196C_GPIO_ADDR		(RTL8196C_BASE_ADDR + 0x3500)
#define RTL8196C_GPIO_PABCD_CNT_ADDR	(RTL8196C_GPIO_ADDR + 0x00)
#define RTL8196C_GPIO_PABCD_TYPE_ADDR	(RTL8196C_GPIO_ADDR + 0x00)
#define RTL8196C_GPIO_PABCD_DIR_ADDR	(RTL8196C_GPIO_ADDR + 0x08)
#define RTL8196C_GPIO_PABCD_DAT_ADDR	(RTL8196C_GPIO_ADDR + 0x0c)
#define RTL8196C_GPIO_PABCD_ISR_ADDR	(RTL8196C_GPIO_ADDR + 0x10)
#define RTL8196C_GPIO_PAB_IMR_ADDR	(RTL8196C_GPIO_ADDR + 0x14)
#define RTL8196C_GPIO_PCD_IMR_ADDR	(RTL8196C_GPIO_ADDR + 0x18)

#define RTL8196C_MUX_ADDR		(RTL8196C_BASE_ADDR + 0x40)

static int g_muxdummy;

//gpio shared pin mapping list
#define RTL8196C_GPIO_SPML_LED_S0	//LED_PORT0,GPIOB2
#define RTL8196C_GPIO_SPML_LED_S1	//LED_PORT1,GPIOB3
#define RTL8196C_GPIO_SPML_LED_S2	//LED_PORT2,GPIOB4
#define RTL8196C_GPIO_SPML_LED_S3	//LED_PORT3,GPIOB5
#define RTL8196C_GPIO_SPML_LED_P0	//LED_PORT4,GPIOB6
#define RTL8196C_GPIO_SPML_LED_P1	//GPIOB7
#define RTL8196C_GPIO_SPML_LED_P2	//GPIOC0
#define RTL8196C_GPIO_SPML_MEM		//NF_CS1#MCS1, DRAM_CKE, GPIOA[1:0]
#define RTL8196C_GPIO_SPML_JTAG		//JTAG, GPIOA[6:2]
#define RTL8196C_GPIO_SPML_UART		
#define RTL8196C_GPIO_SPML_PCIE		

static uint32_t rtl8196c_mux_value	=	0x340FFF;

struct rtl_gpio_chip 
{
	struct gpio_chip	chip;
	uint32_t		gpio_cnt;
	uint32_t		gpio_data;
	uint32_t		gpio_dir;
	volatile uint8_t*	regs;
	spinlock_t		lock;/* Lock used for synchronization */
};

extern struct rtl_gpio_chip gpio_rtl8196c;

static inline struct rtl_gpio_chip *to_rtl_gpio_chip(struct gpio_chip *gc)
{
	return container_of(gc, struct rtl_gpio_chip, chip);
}

////////////////////////////////////////////////////////////////////////
//configure ports as GPIO
static int param_set_rtl8196c_mux( const char *val, struct kernel_param *kp )
{
	volatile uint32_t *mux = (uint32_t *)RTL8196C_MUX_ADDR;
	unsigned long flags;
	
	PRINT("param_set_rtl8196c_mux\n");
	if ( !val )
	{
		return -EINVAL;
	}
	if ( sscanf( val, "0x%x", &rtl8196c_mux_value ) < 0 )
	{
		PRINT("err val\n");
		return -EINVAL;
	}
	spin_lock_irqsave(&gpio_rtl8196c.lock, flags);
	PRINT("mux set 0x%08x\n",rtl8196c_mux_value);
	*mux = rtl8196c_mux_value;
	spin_unlock_irqrestore(&gpio_rtl8196c.lock, flags);
	return 0;
}

#define param_check_rtl8196c_mux(a,b)  ;;

////////////////////////////////////////////////////////////////////////
//get current configured port value
static int param_get_rtl8196c_mux( char *buf, struct kernel_param *kp )
{
	volatile uint32_t *mux = (uint32_t*)RTL8196C_MUX_ADDR;
	unsigned long flags;
	
	PRINT("param_get_rtl8196c_mux\n");
	spin_lock_irqsave(&gpio_rtl8196c.lock, flags);
	sprintf( buf, "0x%x", *mux );
	spin_unlock_irqrestore(&gpio_rtl8196c.lock, flags);
	return strlen( buf );
}

module_param_named(rtl8196c_mux, g_muxdummy, rtl8196c_mux, S_IRUGO | S_IWUSR);

////////////////////////////////////////////////////////////////////////
static int gpio_rtl8196c_dir_in( struct gpio_chip *chip, unsigned int gpio )
{
	volatile uint32_t *dir = (uint32_t*)RTL8196C_GPIO_PABCD_DIR_ADDR;
	unsigned long flags;
	struct rtl_gpio_chip *rgc = to_rtl_gpio_chip( chip );
	
	PRINT("gpio_rtl8196c_dir_in\n");
	PRINT("offset = %d\n", gpio);
	
	spin_lock_irqsave(rgc->lock, flags);
	rgc->gpio_dir &= ~(1 << gpio);
	*dir = rgc->gpio_dir;
	spin_unlock_irqrestore(rgc->lock, flags);
	
	return 0;
}


////////////////////////////////////////////////////////////////////////
static int gpio_rtl8196c_dir_out( struct gpio_chip *chip, unsigned int gpio )
{
	volatile uint32_t *dir = (uint32_t*)RTL8196C_GPIO_PABCD_DIR_ADDR;
	unsigned long flags;
	struct rtl_gpio_chip *rgc = to_rtl_gpio_chip( chip );
	
	PRINT("gpio_rtl8196c_dir_out\n");
	PRINT("offset = %d\n", gpio);
	
	spin_lock_irqsave(rgc->lock, flags);
	rgc->gpio_dir |= (1 << gpio);
	*dir = rgc->gpio_dir;
	spin_unlock_irqrestore(rgc->lock, flags);
	
	return 0;
}


////////////////////////////////////////////////////////////////////////
static int gpio_rtl8196c_get( struct gpio_chip *chip, unsigned int gpio )
{
	volatile uint32_t *dat = (uint32_t*)RTL8196C_GPIO_PABCD_DAT_ADDR;
	int res=0;
	unsigned long flags;
	struct rtl_gpio_chip *rgc = to_rtl_gpio_chip( chip );
	
	PRINT("gpio_rtl8196c_get\n");
	PRINT("offset = %d\n", gpio);
	
	spin_lock_irqsave(rgc->lock, flags);
	//or get it from data register?
	res = ((*dat)>>gpio)&0x1;
	spin_unlock_irqrestore(rgc->lock, flags);
	return res;
}


////////////////////////////////////////////////////////////////////////
static void gpio_rtl8196c_set( struct gpio_chip *chip, unsigned int gpio, int value )
{
	volatile uint32_t *dat = (uint32_t*)RTL8196C_GPIO_PABCD_DAT_ADDR;
	int res=0;
	unsigned long flags;
	struct rtl_gpio_chip *rgc = to_rtl_gpio_chip( chip );
	
	PRINT("gpio_rtl8196c_set\n");
	PRINT("offset = %d = %d\n", gpio, value);
	
	spin_lock_irqsave(rgc->lock, flags);
	if ( value )
		rgc->gpio_data |= (1 << gpio);
	else
		rgc->gpio_data &= ~(1 << gpio);
	*dat = rgc->gpio_data;
	spin_unlock_irqrestore(rgc->lock, flags);
	
}

////////////////////////////////////////////////////////////////////////
static int gpio_rtl8196c_to_irq(struct gpio_chip *chip, unsigned offset)
{
	return -EINVAL;
}

////////////////////////////////////////////////////////////////////////
//init module
struct rtl_gpio_chip gpio_rtl8196c = 
{
	.chip = {
		.label =		"gpio_rtl8196c",
		.direction_input =	gpio_rtl8196c_dir_in,
		.get =			gpio_rtl8196c_get,
		.direction_output =	gpio_rtl8196c_dir_out,
		.set =			gpio_rtl8196c_set,
		.to_irq =		gpio_rtl8196c_to_irq,
		.base =			0,
		.ngpio =		32,
	},
	.gpio_dir = 0x0,
};

static int __init gpio_rtl8196c_init( void )
{
	PRINT("Start\n");
	gpiochip_add( &gpio_rtl8196c.chip );
	return 0;
}

////////////////////////////////////////////////////////////////////////
//exit from module
static int gpio_rtl8196c_exit( void )
{
	PRINT("End\n");
	gpiochip_remove( &gpio_rtl8196c.chip );
	return 0;
}



MODULE_AUTHOR("Artur Artamonov");
MODULE_DESCRIPTION("gpio_rtl8196c");
MODULE_LICENSE("GPL");

module_init( gpio_rtl8196c_init );
module_exit( gpio_rtl8196c_exit );