33 * rtmouse.c
44 * Raspberry Pi Mouse device driver
55 *
6- * Version: 3.3.1
6+ * Version: 3.3.2
77 *
8- * Copyright (C) 2015-2021 RT Corporation <[email protected] > 8+ * Copyright (C) 2015-2024 RT Corporation <[email protected] > 99 *
1010 * This program is free software; you can redistribute it and/or modify
1111 * it under the terms of the GNU General Public License as published by
2222 * MA 02110-1301, USA.
2323 */
2424
25- #include <linux/cdev.h>
26- #include <linux/delay.h>
27- #include <linux/device.h>
28- #include <linux/errno.h>
29- #include <linux/fs.h>
30- #include <linux/i2c.h>
31- #include <linux/iio/iio.h>
32- #include <linux/init.h>
33- #include <linux/io.h>
34- #include <linux/ioport.h>
35- #include <linux/kdev_t.h>
36- #include <linux/kernel.h>
37- #include <linux/module.h>
38- #include <linux/moduleparam.h>
39- #include <linux/mutex.h>
40- #include <linux/sched.h>
41- #include <linux/slab.h>
42- #include <linux/spi/spi.h>
43- #include <linux/stat.h>
44- #include <linux/timer.h>
45- #include <linux/types.h>
46- #include <linux/uaccess.h>
47- #include <linux/version.h>
48-
49- // define the Raspberry Pi version here
50- // Raspberry Pi 1 B/A/B+/A+: 1
51- // Raspberry Pi 2 B : 2
52- // Raspberry Pi 3 B/A+/B+ : 2
53- // Raspberry Pi 4 B : 4
54- #define RASPBERRYPI 2
25+ #include "rtmouse.h"
5526
5627MODULE_AUTHOR ("RT Corporation" );
5728MODULE_LICENSE ("GPL" );
58- MODULE_VERSION ("3.3.1 " );
29+ MODULE_VERSION ("3.3.2 " );
5930MODULE_DESCRIPTION ("Raspberry Pi Mouse device driver" );
6031
61- /* --- Device ID --- */
62- #define ID_DEV_LED 0
63- #define ID_DEV_SWITCH 1
64- #define ID_DEV_SENSOR 2
65- #define ID_DEV_BUZZER 3
66- #define ID_DEV_MOTORRAWR 4
67- #define ID_DEV_MOTORRAWL 5
68- #define ID_DEV_MOTOREN 6
69- #define ID_DEV_MOTOR 7
70- #define ID_DEV_CNT 8
71- #define ID_DEV_SIZE 9
72-
73- /* --- Device Numbers --- */
74- static const unsigned int NUM_DEV [ID_DEV_SIZE ] = {
75- [ID_DEV_LED ] = 4 , [ID_DEV_SWITCH ] = 3 , [ID_DEV_SENSOR ] = 1 ,
76- [ID_DEV_BUZZER ] = 1 , [ID_DEV_MOTORRAWR ] = 1 , [ID_DEV_MOTORRAWL ] = 1 ,
77- [ID_DEV_MOTOREN ] = 1 , [ID_DEV_MOTOR ] = 1 , [ID_DEV_CNT ] = 2 };
78-
79- #define NUM_DEV_TOTAL \
80- (NUM_DEV[ID_DEV_LED] + NUM_DEV[ID_DEV_SWITCH] + \
81- NUM_DEV[ID_DEV_SENSOR] + NUM_DEV[ID_DEV_BUZZER] + \
82- NUM_DEV[ID_DEV_MOTORRAWR] + NUM_DEV[ID_DEV_MOTORRAWL] + \
83- NUM_DEV[ID_DEV_MOTOREN] + NUM_DEV[ID_DEV_MOTOR])
84-
85- /* --- Device Names --- */
86- static const char * NAME_DEV [ID_DEV_SIZE ] = {
87- [ID_DEV_LED ] = "rtled" ,
88- [ID_DEV_SWITCH ] = "rtswitch" ,
89- [ID_DEV_SENSOR ] = "rtlightsensor" ,
90- [ID_DEV_BUZZER ] = "rtbuzzer" ,
91- [ID_DEV_MOTORRAWR ] = "rtmotor_raw_r" ,
92- [ID_DEV_MOTORRAWL ] = "rtmotor_raw_l" ,
93- [ID_DEV_MOTOREN ] = "rtmotoren" ,
94- [ID_DEV_MOTOR ] = "rtmotor" };
95-
96- static const char * NAME_DEV_U [ID_DEV_SIZE ] = {
97- [ID_DEV_LED ] = "rtled%u" ,
98- [ID_DEV_SWITCH ] = "rtswitch%u" ,
99- [ID_DEV_SENSOR ] = "rtlightsensor%u" ,
100- [ID_DEV_BUZZER ] = "rtbuzzer%u" ,
101- [ID_DEV_MOTORRAWR ] = "rtmotor_raw_r%u" ,
102- [ID_DEV_MOTORRAWL ] = "rtmotor_raw_l%u" ,
103- [ID_DEV_MOTOREN ] = "rtmotoren%u" ,
104- [ID_DEV_MOTOR ] = "rtmotor%u" };
105-
106- #define DEVNAME_SENSOR "rtlightsensor"
107- #define DEVNAME_CNTR "rtcounter_r"
108- #define DEVNAME_CNTL "rtcounter_l"
109-
110- #define DRIVER_NAME "rtmouse"
111-
112- /* --- Device Major and Minor Numbers --- */
113- #define DEV_MAJOR 0
114- #define DEV_MINOR 0
115-
116- static int spi_bus_num = 0 ;
117- static int spi_chip_select = 0 ;
118-
11932static int _major_dev [ID_DEV_SIZE ] = {
12033 [ID_DEV_LED ] = DEV_MAJOR , [ID_DEV_SWITCH ] = DEV_MAJOR ,
12134 [ID_DEV_SENSOR ] = DEV_MAJOR , [ID_DEV_BUZZER ] = DEV_MAJOR ,
@@ -144,143 +57,6 @@ static volatile int cdev_index = 0;
14457
14558static struct mutex lock ;
14659
147- /* --- GPIO Pin Definitions --- */
148- #define R_AD_CH 3
149- #define L_AD_CH 0
150- #define RF_AD_CH 2
151- #define LF_AD_CH 1
152-
153- #define R_LED_BASE 22
154- #define L_LED_BASE 4
155- #define RF_LED_BASE 27
156- #define LF_LED_BASE 17
157-
158- #define LED0_BASE 25
159- #define LED1_BASE 24
160- #define LED2_BASE 23
161- #define LED3_BASE 18
162-
163- #define SW1_PIN 20
164- #define SW2_PIN 26
165- #define SW3_PIN 21
166-
167- #define BUZZER_BASE 19
168-
169- #define MOTCLK_L_BASE 12
170- #define MOTDIR_L_BASE 16
171-
172- #define MOTCLK_R_BASE 13
173- #define MOTDIR_R_BASE 6
174-
175- #define MOTEN_BASE 5
176-
177- #define PWM_ORG0_BASE 40
178- #define PWM_ORG1_BASE 45
179-
180- /* --- Register Address --- */
181- /* Base Addr */
182- #if RASPBERRYPI == 1
183- #define RPI_REG_BASE 0x20000000
184- #elif RASPBERRYPI == 2
185- #define RPI_REG_BASE 0x3f000000
186- #elif RASPBERRYPI == 4
187- #define RPI_REG_BASE 0xfe000000
188- /* 2711 has a different mechanism for pin pull-up/down/enable */
189- #define GPPUPPDN0 57 /* Pin pull-up/down for pins 15:0 */
190- #define GPPUPPDN1 58 /* Pin pull-up/down for pins 31:16 */
191- #define GPPUPPDN2 59 /* Pin pull-up/down for pins 47:32 */
192- #define GPPUPPDN3 60 /* Pin pull-up/down for pins 57:48 */
193- #endif
194-
195- /* GPIO Addr */
196- #define RPI_GPIO_OFFSET 0x200000
197- #define RPI_GPIO_SIZE 0xC0
198- #define RPI_GPIO_BASE (RPI_REG_BASE + RPI_GPIO_OFFSET)
199- #define REG_GPIO_NAME "RPi mouse GPIO"
200-
201- /* Pwm Addr */
202- #define RPI_PWM_OFFSET 0x20C000
203- #define RPI_PWM_SIZE 0xC0
204- #define RPI_PWM_BASE (RPI_REG_BASE + RPI_PWM_OFFSET)
205- #define REG_PWM_NAME "RPi mouse PWM"
206-
207- /* Clock Addr */
208- #define RPI_CLK_OFFSET 0x101000
209- #define RPI_CLK_SIZE 0x100
210- #define RPI_CLK_BASE (RPI_REG_BASE + RPI_CLK_OFFSET)
211- #define REG_CLK_NAME "RPi mouse CLK"
212-
213- /* --- General Options --- */
214- /* Clock Offset */
215- #define CLK_PWM_INDEX 0xa0
216- #define CLK_PWMDIV_INDEX 0xa4
217-
218- /* GPIO PUPD select */
219- #if RASPBERRYPI == 4
220- #define GPIO_PULLNONE 0x0
221- #define GPIO_PULLUP 0x1
222- #define GPIO_PULLDOWN 0x2
223- #else
224- #define GPIO_PULLNONE 0x0
225- #define GPIO_PULLDOWN 0x1
226- #define GPIO_PULLUP 0x2
227- #endif
228-
229- /* GPIO Function */
230- #define RPI_GPF_INPUT 0x00
231- #define RPI_GPF_OUTPUT 0x01
232- #define RPI_GPF_ALT0 0x04
233- #define RPI_GPF_ALT5 0x02
234-
235- /* GPIO Register Index */
236- #define RPI_GPFSEL0_INDEX 0
237- #define RPI_GPFSEL1_INDEX 1
238- #define RPI_GPFSEL2_INDEX 2
239- #define RPI_GPFSEL3_INDEX 3
240-
241- #define RPI_GPSET0_INDEX 7
242- #define RPI_GPCLR0_INDEX 10
243-
244- /* GPIO Mask */
245- #define RPI_GPIO_P1MASK \
246- (uint32_t)((0x01 << 2) | (0x01 << 3) | (0x01 << 4) | (0x01 << 7) | \
247- (0x01 << 8) | (0x01 << 9) | (0x01 << 10) | (0x01 << 11) | \
248- (0x01 << 14) | (0x01 << 15) | (0x01 << 17) | (0x01 << 18) | \
249- (0x01 << 22) | (0x01 << 23) | (0x01 << 24) | (0x01 << 25) | \
250- (0x01 << 27))
251- #define RPI_GPIO_P2MASK (uint32_t)0xffffffff
252-
253- /* PWM Index */
254- #define RPI_PWM_CTRL 0x0
255- #define RPI_PWM_STA 0x4
256- #define RPI_PWM_DMAC 0x8
257- #define RPI_PWM_RNG1 0x10
258- #define RPI_PWM_DAT1 0x14
259- #define RPI_PWM_FIF1 0x18
260- #define RPI_PWM_RNG2 0x20
261- #define RPI_PWM_DAT2 0x24
262-
263- #if RASPBERRYPI == 4
264- #define PWM_BASECLK 27000000
265- #else
266- #define PWM_BASECLK 9600000
267- #endif
268-
269- /* A/D Parameter */
270- #define MCP320X_PACKET_SIZE 3
271- #define MCP320X_DIFF 0
272- #define MCP320X_SINGLE 1
273- #define MCP3204_CHANNELS 4
274-
275- /* I2C Parameter */
276- #define DEV_ADDR_CNTL 0x10
277- #define DEV_ADDR_CNTR 0x11
278- #define CNT_ADDR_MSB 0x10
279- #define CNT_ADDR_LSB 0x11
280-
281- /* Motor Parameter */
282- #define MOTOR_UNCONTROLLABLE_FREQ 5
283-
28460/* --- Function Declarations --- */
28561static void set_motor_r_freq (int freq );
28662static void set_motor_l_freq (int freq );
@@ -366,8 +142,6 @@ static struct i2c_client *i2c_client_r = NULL;
366142static struct i2c_client * i2c_client_l = NULL ;
367143static unsigned int motor_l_freq_is_positive = 1 ;
368144static unsigned int motor_r_freq_is_positive = 1 ;
369- #define SIGNED_COUNT_SIZE 32767
370- #define MAX_PULSE_COUNT 65535
371145
372146/* I2C Device ID */
373147static struct i2c_device_id i2c_counter_id [] = {
@@ -390,67 +164,8 @@ static struct i2c_driver i2c_counter_driver = {
390164
391165/* -- Device Addition -- */
392166MODULE_DEVICE_TABLE (spi , mcp3204_id );
393-
394167MODULE_DEVICE_TABLE (i2c , i2c_counter_id );
395168
396- /* -- Buffer -- */
397- #define MAX_BUFLEN 64
398- // static int buflen = 0;
399-
400- #define MOTOR_MOTION 0
401- #if MOTOR_MOTION
402- /* Variable Type Definition for motor motion */
403- typedef struct {
404- signed int r_hz ;
405- signed int l_hz ;
406- unsigned int time ;
407- } t_motor_motion ;
408-
409- #define MAX_MOTORBUFLEN 16
410- static t_motor_motion motor_motion [MAX_MOTORBUFLEN ];
411- static unsigned int motor_motion_head = 0 , motor_motion_tail = 0 ;
412-
413- static int motor_motion_push (int r_hz , int l_hz , int time )
414- {
415- unsigned int next_tail = motor_motion_tail + 1 ;
416-
417- if (next_tail >= MAX_MOTORBUFLEN ) {
418- next_tail = 0 ;
419- }
420-
421- if (next_tail == motor_motion_head ) {
422- return -1 ;
423- }
424-
425- motor_motion [motor_motion_tail ].r_hz = r_hz ;
426- motor_motion [motor_motion_tail ].l_hz = l_hz ;
427- motor_motion [motor_motion_tail ].time = time ;
428-
429- motor_motion_tail = next_tail ;
430-
431- return 0 ;
432- }
433-
434- static int motor_motion_pop (t_motor_motion * * ret )
435- {
436- unsigned int next_head = motor_motion_head + 1 ;
437-
438- if (motor_motion_tail == motor_motion_head ) {
439- return -1 ;
440- }
441-
442- if (next_head >= MAX_MOTORBUFLEN ) {
443- next_head = 0 ;
444- }
445-
446- * ret = (motor_motion + motor_motion_head );
447-
448- motor_motion_head = next_head ;
449-
450- return 0 ;
451- }
452- #endif
453-
454169/* --- GPIO Operation --- */
455170/* getPWMCount function for GPIO Operation */
456171static int getPWMCount (int freq )
@@ -1531,8 +1246,8 @@ static int mcp3204_init(void)
15311246
15321247 spi_register_driver (& mcp3204_driver );
15331248
1534- mcp3204_info .bus_num = spi_bus_num ;
1535- mcp3204_info .chip_select = spi_chip_select ;
1249+ mcp3204_info .bus_num = SPI_BUS_NUM ;
1250+ mcp3204_info .chip_select = SPI_CHIP_SELECT ;
15361251
15371252 master = spi_busnum_to_master (mcp3204_info .bus_num );
15381253
0 commit comments