Skip to content

Commit 6e95fbf

Browse files
committed
rtmouse_gpio.cを作成してGPIO関連の関数を移す
1 parent 0cc93d9 commit 6e95fbf

File tree

7 files changed

+149
-124
lines changed

7 files changed

+149
-124
lines changed

src/drivers/Makefile.header_from_apt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
MODULE:= rtmouse
22
obj-m:= $(MODULE).o
3-
$(MODULE)-y:= $(MODULE)_main.o $(MODULE)_dev.o $(MODULE)_spi.o $(MODULE)_i2c.o
3+
$(MODULE)-y:= $(MODULE)_main.o $(MODULE)_dev.o $(MODULE)_spi.o $(MODULE)_i2c.o $(MODULE)_gpio.o
44
clean-files:= *.o *.ko *.mod.[co] *~
55

66
LINUX_SRC_DIR:=/usr/src/linux-headers-$(shell uname -r)
77
VERBOSE:=0
88

9-
$(MODULE).ko: $(MODULE)_main.c $(MODULE)_dev.c $(MODULE)_spi.c $(MODULE)_i2c.c $(MODULE).h
9+
$(MODULE).ko: $(MODULE)_main.c $(MODULE)_dev.c $(MODULE)_spi.c $(MODULE)_i2c.c $(MODULE)_gpio.c $(MODULE).h
1010
make -C $(LINUX_SRC_DIR) M=$(shell pwd) V=$(VERBOSE) modules
1111

1212
clean:

src/drivers/Makefile.header_from_source

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
MODULE:= rtmouse
22
obj-m:= $(MODULE).o
3-
$(MODULE)-y:= $(MODULE)_main.o $(MODULE)_dev.o $(MODULE)_spi.o $(MODULE)_i2c.o
3+
$(MODULE)-y:= $(MODULE)_main.o $(MODULE)_dev.o $(MODULE)_spi.o $(MODULE)_i2c.o $(MODULE)_gpio.o
44
clean-files:= *.o *.ko *.mod.[co] *~
55

66
LINUX_SRC_DIR:=/usr/src/linux
77
VERBOSE:=0
88

9-
$(MODULE).ko: $(MODULE)_main.c $(MODULE)_dev.c $(MODULE)_spi.c $(MODULE)_i2c.c $(MODULE).h
9+
$(MODULE).ko: $(MODULE)_main.c $(MODULE)_dev.c $(MODULE)_spi.c $(MODULE)_i2c.c $(MODULE)_gpio.c $(MODULE).h
1010
make -C $(LINUX_SRC_DIR) M=$(shell pwd) V=$(VERBOSE) modules
1111

1212
clean:

src/drivers/rtmouse.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,5 +285,7 @@ int rpi_gpio_function_set(int pin, uint32_t func);
285285
void rpi_gpio_set32(uint32_t mask, uint32_t val);
286286
void rpi_gpio_clear32(uint32_t mask, uint32_t val);
287287
void rpi_pwm_write32(uint32_t offset, uint32_t val);
288+
int gpio_map(void);
289+
int gpio_unmap(void);
288290

289291
#endif // RTMOUSE_H

src/drivers/rtmouse_gpio.c

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/*
2+
*
3+
* rtmouse_gpio.c
4+
* GPIO driver
5+
*
6+
* Copyright (C) 2024 RT Corporation <shop@rt-net.jp>
7+
*
8+
* This program is free software; you can redistribute it and/or modify
9+
* it under the terms of the GNU General Public License as published by
10+
* the Free Software Foundation; version 2.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20+
* MA 02110-1301, USA.
21+
*/
22+
23+
#include "rtmouse.h"
24+
25+
static volatile void __iomem *clk_base;
26+
27+
/*
28+
* set function
29+
* called by buzzer_init(), set_motor_l_freq(), set_motor_r_freq() and
30+
* buzzer_write()
31+
*/
32+
int rpi_gpio_function_set(int pin, uint32_t func)
33+
{
34+
int index = RPI_GPFSEL0_INDEX + pin / 10;
35+
uint32_t mask = ~(0x7 << ((pin % 10) * 3));
36+
37+
gpio_base[index] =
38+
(gpio_base[index] & mask) | ((func & 0x7) << ((pin % 10) * 3));
39+
40+
return 1;
41+
}
42+
43+
/*
44+
* set mask and value
45+
* called by sensor_read(), set_motor_l_freq(), set_motor_r_freq(), led_put()
46+
* and motoren_write()
47+
*/
48+
void rpi_gpio_set32(uint32_t mask, uint32_t val)
49+
{
50+
gpio_base[RPI_GPSET0_INDEX] = val & mask;
51+
}
52+
53+
/*
54+
* clear mask and value
55+
* called by sensor_read(), set_motor_l_freq(), set_motor_r_freq(), led_del()
56+
* and motoren_write()
57+
*/
58+
void rpi_gpio_clear32(uint32_t mask, uint32_t val)
59+
{
60+
gpio_base[RPI_GPCLR0_INDEX] = val & mask;
61+
}
62+
63+
/*
64+
* pwm set function
65+
* called by buzzer_init(), set_motor_l_freq(), set_motor_r_freq()
66+
* and buzzer_write()
67+
*/
68+
void rpi_pwm_write32(uint32_t offset, uint32_t val)
69+
{
70+
iowrite32(val, pwm_base + offset);
71+
}
72+
73+
/* --- GPIO mapping for Device Open/Close --- */
74+
/*
75+
* Get gpio addresses and set them to global variables.
76+
* - gpio_base
77+
* - pwm_base
78+
* - clk_base
79+
* - clk_status
80+
*/
81+
int gpio_map(void)
82+
{
83+
static int clk_status = 1;
84+
85+
#if LINUX_VERSION_CODE <= KERNEL_VERSION(5, 0, 0)
86+
if (gpio_base == NULL) {
87+
gpio_base = ioremap_nocache(RPI_GPIO_BASE, RPI_GPIO_SIZE);
88+
}
89+
90+
if (pwm_base == NULL) {
91+
pwm_base = ioremap_nocache(RPI_PWM_BASE, RPI_PWM_SIZE);
92+
}
93+
94+
if (clk_base == NULL) {
95+
clk_base = ioremap_nocache(RPI_CLK_BASE, RPI_CLK_SIZE);
96+
}
97+
#else
98+
if (gpio_base == NULL) {
99+
gpio_base = ioremap(RPI_GPIO_BASE, RPI_GPIO_SIZE);
100+
}
101+
102+
if (pwm_base == NULL) {
103+
pwm_base = ioremap(RPI_PWM_BASE, RPI_PWM_SIZE);
104+
}
105+
106+
if (clk_base == NULL) {
107+
clk_base = ioremap(RPI_CLK_BASE, RPI_CLK_SIZE);
108+
}
109+
#endif
110+
111+
/* kill */
112+
if (clk_status == 1) {
113+
iowrite32(0x5a000000 | (1 << 5), clk_base + CLK_PWM_INDEX);
114+
udelay(1000);
115+
116+
/* clk set */
117+
iowrite32(0x5a000000 | (2 << 12), clk_base + CLK_PWMDIV_INDEX);
118+
iowrite32(0x5a000011, clk_base + CLK_PWM_INDEX);
119+
120+
udelay(1000); /* wait for 1msec */
121+
122+
clk_status = 0;
123+
}
124+
125+
return 0;
126+
}
127+
128+
/* Unmap GPIO addresses */
129+
int gpio_unmap(void)
130+
{
131+
iounmap(gpio_base);
132+
iounmap(pwm_base);
133+
iounmap(clk_base);
134+
135+
gpio_base = NULL;
136+
pwm_base = NULL;
137+
clk_base = NULL;
138+
return 0;
139+
}

src/drivers/rtmouse_i2c.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
*
33
* rtmouse_i2c.c
4-
* I2C Driver
4+
* I2C driver
55
*
66
* Copyright (C) 2024 RT Corporation <shop@rt-net.jp>
77
*

src/drivers/rtmouse_main.c

Lines changed: 2 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -88,137 +88,21 @@ struct class *class_dev[ID_DEV_SIZE] = {
8888
[ID_DEV_MOTORRAWR] = NULL, [ID_DEV_MOTORRAWL] = NULL,
8989
[ID_DEV_MOTOREN] = NULL, [ID_DEV_MOTOR] = NULL};
9090

91-
static volatile void __iomem *clk_base;
92-
9391
// used in rtmouse_i2c.c
9492
struct cdev *cdev_array = NULL;
9593
volatile int cdev_index = 0;
9694

97-
// used in rtmouse_dev_fops.c
95+
// used in rtmouse_dev.c
9896
volatile void __iomem *pwm_base;
9997
volatile uint32_t *gpio_base;
10098
struct mutex lock;
10199

102100
/* --- Static variables --- */
103-
// used in rtmouse_dev_fops.c
101+
// used in rtmouse_dev.c and rtmouse_spi.c
104102
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 16, 0)
105103
struct device *mcp320x_dev;
106104
#endif
107105

108-
/*
109-
* set function
110-
* called by buzzer_init(), set_motor_l_freq(), set_motor_r_freq() and
111-
* buzzer_write()
112-
*/
113-
int rpi_gpio_function_set(int pin, uint32_t func)
114-
{
115-
int index = RPI_GPFSEL0_INDEX + pin / 10;
116-
uint32_t mask = ~(0x7 << ((pin % 10) * 3));
117-
118-
gpio_base[index] =
119-
(gpio_base[index] & mask) | ((func & 0x7) << ((pin % 10) * 3));
120-
121-
return 1;
122-
}
123-
124-
/*
125-
* set mask and value
126-
* called by sensor_read(), set_motor_l_freq(), set_motor_r_freq(), led_put()
127-
* and motoren_write()
128-
*/
129-
void rpi_gpio_set32(uint32_t mask, uint32_t val)
130-
{
131-
gpio_base[RPI_GPSET0_INDEX] = val & mask;
132-
}
133-
134-
/*
135-
* clear mask and value
136-
* called by sensor_read(), set_motor_l_freq(), set_motor_r_freq(), led_del()
137-
* and motoren_write()
138-
*/
139-
void rpi_gpio_clear32(uint32_t mask, uint32_t val)
140-
{
141-
gpio_base[RPI_GPCLR0_INDEX] = val & mask;
142-
}
143-
144-
/*
145-
* pwm set function
146-
* called by buzzer_init(), set_motor_l_freq(), set_motor_r_freq()
147-
* and buzzer_write()
148-
*/
149-
void rpi_pwm_write32(uint32_t offset, uint32_t val)
150-
{
151-
iowrite32(val, pwm_base + offset);
152-
}
153-
154-
/* --- GPIO mapping for Device Open/Close --- */
155-
/*
156-
* Get gpio addresses and set them to global variables.
157-
* - gpio_base
158-
* - pwm_base
159-
* - clk_base
160-
* - clk_status
161-
*/
162-
static int gpio_map(void)
163-
{
164-
static int clk_status = 1;
165-
166-
#if LINUX_VERSION_CODE <= KERNEL_VERSION(5, 0, 0)
167-
if (gpio_base == NULL) {
168-
gpio_base = ioremap_nocache(RPI_GPIO_BASE, RPI_GPIO_SIZE);
169-
}
170-
171-
if (pwm_base == NULL) {
172-
pwm_base = ioremap_nocache(RPI_PWM_BASE, RPI_PWM_SIZE);
173-
}
174-
175-
if (clk_base == NULL) {
176-
clk_base = ioremap_nocache(RPI_CLK_BASE, RPI_CLK_SIZE);
177-
}
178-
#else
179-
if (gpio_base == NULL) {
180-
gpio_base = ioremap(RPI_GPIO_BASE, RPI_GPIO_SIZE);
181-
}
182-
183-
if (pwm_base == NULL) {
184-
pwm_base = ioremap(RPI_PWM_BASE, RPI_PWM_SIZE);
185-
}
186-
187-
if (clk_base == NULL) {
188-
clk_base = ioremap(RPI_CLK_BASE, RPI_CLK_SIZE);
189-
}
190-
#endif
191-
192-
/* kill */
193-
if (clk_status == 1) {
194-
iowrite32(0x5a000000 | (1 << 5), clk_base + CLK_PWM_INDEX);
195-
udelay(1000);
196-
197-
/* clk set */
198-
iowrite32(0x5a000000 | (2 << 12), clk_base + CLK_PWMDIV_INDEX);
199-
iowrite32(0x5a000011, clk_base + CLK_PWM_INDEX);
200-
201-
udelay(1000); /* wait for 1msec */
202-
203-
clk_status = 0;
204-
}
205-
206-
return 0;
207-
}
208-
209-
/* Unmap GPIO addresses */
210-
static int gpio_unmap(void)
211-
{
212-
iounmap(gpio_base);
213-
iounmap(pwm_base);
214-
iounmap(clk_base);
215-
216-
gpio_base = NULL;
217-
pwm_base = NULL;
218-
clk_base = NULL;
219-
return 0;
220-
}
221-
222106
/*
223107
* dev_init_module - register driver module
224108
* called by module_init(dev_init_module)

src/drivers/rtmouse_spi.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
*
33
* rtmouse_spi.c
4-
* SPI Driver
4+
* SPI driver
55
*
66
* Copyright (C) 2024 RT Corporation <shop@rt-net.jp>
77
*

0 commit comments

Comments
 (0)