-
Notifications
You must be signed in to change notification settings - Fork 448
Expand file tree
/
Copy pathrtmouse.h
More file actions
293 lines (251 loc) · 7.46 KB
/
rtmouse.h
File metadata and controls
293 lines (251 loc) · 7.46 KB
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
*
* rtmouse.h
* Raspberry Pi Mouse device driver header
*
* Copyright (C) 2024 RT Corporation <shop@rt-net.jp>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#ifndef RTMOUSE_H
#define RTMOUSE_H
#include <linux/cdev.h>
#include <linux/delay.h>
#include <linux/device.h>
#include <linux/errno.h>
#include <linux/fs.h>
#include <linux/i2c.h>
#include <linux/iio/iio.h>
#include <linux/init.h>
#include <linux/io.h>
#include <linux/ioport.h>
#include <linux/kdev_t.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/mutex.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/spi/spi.h>
#include <linux/stat.h>
#include <linux/timer.h>
#include <linux/types.h>
#include <linux/uaccess.h>
#include <linux/version.h>
// define the Raspberry Pi version here
// Raspberry Pi 1 B/A/B+/A+: 1
// Raspberry Pi 2 B : 2
// Raspberry Pi 3 B/A+/B+ : 2
// Raspberry Pi 4 B : 4
#define RASPBERRYPI 2
#define DEV_RIGHT 0
#define DEV_LEFT 1
/* --- Device ID --- */
#define ID_DEV_LED 0
#define ID_DEV_SWITCH 1
#define ID_DEV_SENSOR 2
#define ID_DEV_BUZZER 3
#define ID_DEV_MOTORRAWR 4
#define ID_DEV_MOTORRAWL 5
#define ID_DEV_MOTOREN 6
#define ID_DEV_MOTOR 7
#define ID_DEV_CNT 8
#define ID_DEV_SIZE 9
#define NUM_DEV_TOTAL \
(NUM_DEV[ID_DEV_LED] + NUM_DEV[ID_DEV_SWITCH] + \
NUM_DEV[ID_DEV_SENSOR] + NUM_DEV[ID_DEV_BUZZER] + \
NUM_DEV[ID_DEV_MOTORRAWR] + NUM_DEV[ID_DEV_MOTORRAWL] + \
NUM_DEV[ID_DEV_MOTOREN] + NUM_DEV[ID_DEV_MOTOR])
#define DEVNAME_SENSOR "rtlightsensor"
#define DEVNAME_CNTR "rtcounter_r"
#define DEVNAME_CNTL "rtcounter_l"
#define DRIVER_NAME "rtmouse"
/* SPI */
#define SPI_BUS_NUM 0
#define SPI_CHIP_SELECT 0
/* --- Device Major and Minor Numbers --- */
#define DEV_MAJOR 0
#define DEV_MINOR 0
/* --- GPIO Pin Definitions --- */
#define R_AD_CH 3
#define L_AD_CH 0
#define RF_AD_CH 2
#define LF_AD_CH 1
#define R_LED_BASE 22
#define L_LED_BASE 4
#define RF_LED_BASE 27
#define LF_LED_BASE 17
#define LED0_BASE 25
#define LED1_BASE 24
#define LED2_BASE 23
#define LED3_BASE 18
#define SW1_PIN 20
#define SW2_PIN 26
#define SW3_PIN 21
#define BUZZER_BASE 19
#define MOTCLK_L_BASE 12
#define MOTDIR_L_BASE 16
#define MOTCLK_R_BASE 13
#define MOTDIR_R_BASE 6
#define MOTEN_BASE 5
#define PWM_ORG0_BASE 40
#define PWM_ORG1_BASE 45
/* --- Register Address --- */
/* Base Addr */
#if RASPBERRYPI == 1
#define RPI_REG_BASE 0x20000000
#elif RASPBERRYPI == 2
#define RPI_REG_BASE 0x3f000000
#elif RASPBERRYPI == 4
#define RPI_REG_BASE 0xfe000000
/* 2711 has a different mechanism for pin pull-up/down/enable */
#define GPPUPPDN0 57 /* Pin pull-up/down for pins 15:0 */
#define GPPUPPDN1 58 /* Pin pull-up/down for pins 31:16 */
#define GPPUPPDN2 59 /* Pin pull-up/down for pins 47:32 */
#define GPPUPPDN3 60 /* Pin pull-up/down for pins 57:48 */
#endif
/* GPIO Addr */
#define RPI_GPIO_OFFSET 0x200000
#define RPI_GPIO_SIZE 0xC0
#define RPI_GPIO_BASE (RPI_REG_BASE + RPI_GPIO_OFFSET)
#define REG_GPIO_NAME "RPi mouse GPIO"
/* Pwm Addr */
#define RPI_PWM_OFFSET 0x20C000
#define RPI_PWM_SIZE 0xC0
#define RPI_PWM_BASE (RPI_REG_BASE + RPI_PWM_OFFSET)
#define REG_PWM_NAME "RPi mouse PWM"
/* Clock Addr */
#define RPI_CLK_OFFSET 0x101000
#define RPI_CLK_SIZE 0x100
#define RPI_CLK_BASE (RPI_REG_BASE + RPI_CLK_OFFSET)
#define REG_CLK_NAME "RPi mouse CLK"
/* --- General Options --- */
/* Clock Offset */
#define CLK_PWM_INDEX 0xa0
#define CLK_PWMDIV_INDEX 0xa4
/* GPIO PUPD select */
#if RASPBERRYPI == 4
#define GPIO_PULLNONE 0x0
#define GPIO_PULLUP 0x1
#define GPIO_PULLDOWN 0x2
#else
#define GPIO_PULLNONE 0x0
#define GPIO_PULLDOWN 0x1
#define GPIO_PULLUP 0x2
#endif
/* GPIO Function */
#define RPI_GPF_INPUT 0x00
#define RPI_GPF_OUTPUT 0x01
#define RPI_GPF_ALT0 0x04
#define RPI_GPF_ALT5 0x02
/* GPIO Register Index */
#define RPI_GPFSEL0_INDEX 0
#define RPI_GPFSEL1_INDEX 1
#define RPI_GPFSEL2_INDEX 2
#define RPI_GPFSEL3_INDEX 3
#define RPI_GPSET0_INDEX 7
#define RPI_GPCLR0_INDEX 10
/* GPIO Mask */
#define RPI_GPIO_P1MASK \
(uint32_t)((0x01 << 2) | (0x01 << 3) | (0x01 << 4) | (0x01 << 7) | \
(0x01 << 8) | (0x01 << 9) | (0x01 << 10) | (0x01 << 11) | \
(0x01 << 14) | (0x01 << 15) | (0x01 << 17) | (0x01 << 18) | \
(0x01 << 22) | (0x01 << 23) | (0x01 << 24) | (0x01 << 25) | \
(0x01 << 27))
#define RPI_GPIO_P2MASK (uint32_t)0xffffffff
/* PWM Index */
#define RPI_PWM_CTRL 0x0
#define RPI_PWM_STA 0x4
#define RPI_PWM_DMAC 0x8
#define RPI_PWM_RNG1 0x10
#define RPI_PWM_DAT1 0x14
#define RPI_PWM_FIF1 0x18
#define RPI_PWM_RNG2 0x20
#define RPI_PWM_DAT2 0x24
#if RASPBERRYPI == 4
#define PWM_BASECLK 27000000
#else
#define PWM_BASECLK 9600000
#endif
/* A/D Parameter */
#define MCP320X_PACKET_SIZE 3
#define MCP320X_DIFF 0
#define MCP320X_SINGLE 1
#define MCP3204_CHANNELS 4
/* I2C Parameter */
#define DEV_ADDR_CNTL 0x10
#define DEV_ADDR_CNTR 0x11
#define CNT_ADDR_MSB 0x10
#define CNT_ADDR_LSB 0x11
/* Motor Parameter */
#define MOTOR_UNCONTROLLABLE_FREQ 5
/* I2C */
#define SIGNED_COUNT_SIZE 32767
#define MAX_PULSE_COUNT 65535
/* --- Buffer --- */
#define MAX_BUFLEN 64
/* --- Variable Type definitions --- */
/* SPI */
struct mcp3204_drvdata {
struct spi_device *spi;
struct mutex lock;
unsigned char tx[MCP320X_PACKET_SIZE] ____cacheline_aligned;
unsigned char rx[MCP320X_PACKET_SIZE] ____cacheline_aligned;
struct spi_transfer xfer ____cacheline_aligned;
struct spi_message msg ____cacheline_aligned;
};
/* I2C */
struct rtcnt_device_info {
struct cdev cdev;
unsigned int device_major;
unsigned int device_minor;
struct class *device_class;
struct i2c_client *client;
struct mutex lock;
int signed_pulse_count;
int raw_pulse_count;
};
/* --- used in rtmouse_dev.c --- */
extern const char *NAME_DEV[ID_DEV_SIZE];
extern int _major_dev[ID_DEV_SIZE];
extern int _minor_dev[ID_DEV_SIZE];
extern struct class *class_dev[ID_DEV_SIZE];
extern volatile void __iomem *pwm_base;
extern volatile uint32_t *gpio_base;
extern struct mutex lock;
extern struct spi_board_info mcp3204_info;
extern struct file_operations dev_fops[ID_DEV_SIZE];
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 16, 0)
extern struct device *mcp320x_dev;
#endif
int buzzer_init(void);
int register_dev(int id_dev);
/* --- used in rtmouse_spi.c --- */
int mcp3204_init(void);
void mcp3204_exit(void);
/* --- used in rtmouse_i2c.c --- */
extern const unsigned int NUM_DEV[ID_DEV_SIZE];
extern struct cdev *cdev_array;
extern volatile int cdev_index;
int i2c_counter_init(void);
void i2c_counter_exit(void);
/* --- used in rtmouse_gpio.c --- */
int rpi_gpio_function_set(int pin, uint32_t func);
void rpi_gpio_set32(uint32_t mask, uint32_t val);
void rpi_gpio_clear32(uint32_t mask, uint32_t val);
void rpi_pwm_write32(uint32_t offset, uint32_t val);
int gpio_map(void);
int gpio_unmap(void);
#endif // RTMOUSE_H