Skip to content

Commit 5b87a72

Browse files
committed
rtmouse_spi.cを作成してSPI関連の関数を移す
1 parent cead9f2 commit 5b87a72

File tree

5 files changed

+236
-209
lines changed

5 files changed

+236
-209
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_fops.o
3+
$(MODULE)-y:= $(MODULE)_main.o $(MODULE)_dev_fops.o $(MODULE)_spi.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)_dev_fops.c $(MODULE)_main.c $(MODULE).h
9+
$(MODULE).ko: $(MODULE)_main.c $(MODULE)_dev_fops.c $(MODULE)_spi.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_fops.o
3+
$(MODULE)-y:= $(MODULE)_main.o $(MODULE)_dev_fops.o $(MODULE)_spi.o
44
clean-files:= *.o *.ko *.mod.[co] *~
55

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

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

1212
clean:

src/drivers/rtmouse.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,10 +262,13 @@ extern struct file_operations dev_fops[ID_DEV_SIZE];
262262
extern struct device *mcp320x_dev;
263263
#endif
264264

265-
/* --- used in rtmouse_dev_fops.c --- */
266265
int rpi_gpio_function_set(int pin, uint32_t func);
267266
void rpi_gpio_set32(uint32_t mask, uint32_t val);
268267
void rpi_gpio_clear32(uint32_t mask, uint32_t val);
269268
void rpi_pwm_write32(uint32_t offset, uint32_t val);
270269

270+
/* --- used in rtmouse_spi.c --- */
271+
int mcp3204_init(void);
272+
void mcp3204_exit(void);
273+
271274
#endif // RTMOUSE_H

src/drivers/rtmouse_main.c

Lines changed: 5 additions & 204 deletions
Original file line numberDiff line numberDiff line change
@@ -78,21 +78,15 @@ static struct class *class_dev[ID_DEV_SIZE] = {
7878
[ID_DEV_MOTORRAWR] = NULL, [ID_DEV_MOTORRAWL] = NULL,
7979
[ID_DEV_MOTOREN] = NULL, [ID_DEV_MOTOR] = NULL};
8080

81-
volatile void __iomem *pwm_base;
8281
static volatile void __iomem *clk_base;
83-
volatile uint32_t *gpio_base;
8482
static volatile int cdev_index = 0;
83+
84+
// used in rtmouse_dev_fops.c
85+
volatile void __iomem *pwm_base;
86+
volatile uint32_t *gpio_base;
8587
struct mutex lock;
8688

8789
/* --- Function Declarations --- */
88-
#if LINUX_VERSION_CODE < KERNEL_VERSION(5, 18, 0)
89-
static int mcp3204_remove(struct spi_device *spi);
90-
#else
91-
static void mcp3204_remove(struct spi_device *spi);
92-
#endif
93-
94-
static int mcp3204_probe(struct spi_device *spi);
95-
9690
#if LINUX_VERSION_CODE < KERNEL_VERSION(6, 2, 0)
9791
static int rtcnt_i2c_probe(struct i2c_client *client,
9892
const struct i2c_device_id *id);
@@ -107,37 +101,11 @@ static void rtcnt_i2c_remove(struct i2c_client *client);
107101
#endif
108102

109103
/* --- Static variables --- */
110-
/* SPI device ID */
111-
static struct spi_device_id mcp3204_id[] = {
112-
{"mcp3204", 0},
113-
{},
114-
};
115-
116-
/* SPI Info */
117-
struct spi_board_info mcp3204_info = {
118-
.modalias = "mcp3204",
119-
.max_speed_hz = 100000,
120-
.bus_num = 0,
121-
.chip_select = 0,
122-
.mode = SPI_MODE_3,
123-
};
124-
104+
// used in rtmouse_dev_fops.c
125105
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 16, 0)
126106
struct device *mcp320x_dev;
127107
#endif
128108

129-
/* SPI Dirver Info */
130-
static struct spi_driver mcp3204_driver = {
131-
.driver =
132-
{
133-
.name = DEVNAME_SENSOR,
134-
.owner = THIS_MODULE,
135-
},
136-
.id_table = mcp3204_id,
137-
.probe = mcp3204_probe,
138-
.remove = mcp3204_remove,
139-
};
140-
141109
static struct i2c_client *i2c_client_r = NULL;
142110
static struct i2c_client *i2c_client_l = NULL;
143111

@@ -161,7 +129,6 @@ static struct i2c_driver i2c_counter_driver = {
161129
};
162130

163131
/* -- Device Addition -- */
164-
MODULE_DEVICE_TABLE(spi, mcp3204_id);
165132
MODULE_DEVICE_TABLE(i2c, i2c_counter_id);
166133

167134
/*
@@ -365,172 +332,6 @@ static int register_dev(int id_dev)
365332
return 0;
366333
}
367334

368-
/* mcp3204_remove - remove function lined with spi_dirver */
369-
#if LINUX_VERSION_CODE < KERNEL_VERSION(5, 18, 0)
370-
static int mcp3204_remove(struct spi_device *spi)
371-
{
372-
struct mcp3204_drvdata *data;
373-
/* get drvdata */
374-
data = (struct mcp3204_drvdata *)spi_get_drvdata(spi);
375-
/* free kernel memory */
376-
kfree(data);
377-
printk(KERN_INFO "%s: mcp3204 removed\n", DRIVER_NAME);
378-
return 0;
379-
}
380-
#else
381-
static void mcp3204_remove(struct spi_device *spi)
382-
{
383-
struct mcp3204_drvdata *data;
384-
/* get drvdata */
385-
data = (struct mcp3204_drvdata *)spi_get_drvdata(spi);
386-
/* free kernel memory */
387-
kfree(data);
388-
printk(KERN_INFO "%s: mcp3204 removed\n", DRIVER_NAME);
389-
}
390-
#endif
391-
392-
/* mcp3204_probe - probe function lined with spi_dirver */
393-
static int mcp3204_probe(struct spi_device *spi)
394-
{
395-
struct mcp3204_drvdata *data;
396-
397-
spi->max_speed_hz = mcp3204_info.max_speed_hz;
398-
spi->mode = mcp3204_info.mode;
399-
spi->bits_per_word = 8;
400-
401-
if (spi_setup(spi)) {
402-
printk(KERN_ERR "%s:spi_setup failed!\n", __func__);
403-
return -ENODEV;
404-
}
405-
406-
/* alloc kernel memory */
407-
data = kzalloc(sizeof(struct mcp3204_drvdata), GFP_KERNEL);
408-
if (data == NULL) {
409-
printk(KERN_ERR "%s:kzalloc() failed!\n", __func__);
410-
return -ENODEV;
411-
}
412-
413-
data->spi = spi;
414-
415-
mutex_init(&data->lock);
416-
417-
// memset(data->tx, 0, MCP320X_PACKET_SIZE);
418-
// memset(data->rx, 0, MCP320X_PACKET_SIZE);
419-
420-
data->xfer.tx_buf = data->tx;
421-
data->xfer.rx_buf = data->rx;
422-
data->xfer.bits_per_word = 8;
423-
data->xfer.len = MCP320X_PACKET_SIZE;
424-
data->xfer.cs_change = 0;
425-
data->xfer.speed_hz = 100000;
426-
427-
spi_message_init_with_transfers(&data->msg, &data->xfer, 1);
428-
429-
/* set drvdata */
430-
spi_set_drvdata(spi, data);
431-
432-
printk(KERN_INFO "%s: mcp3204 probed", DRIVER_NAME);
433-
434-
return 0;
435-
}
436-
437-
/*
438-
* spi_remove_device - remove SPI device
439-
* called by mcp3204_init and mcp3204_exit
440-
*/
441-
static void spi_remove_device(struct spi_master *master, unsigned int cs)
442-
{
443-
struct device *dev;
444-
char str[128];
445-
446-
snprintf(str, sizeof(str), "%s.%u", dev_name(&master->dev), cs);
447-
448-
dev = bus_find_device_by_name(&spi_bus_type, NULL, str);
449-
// ここを参考にspi_deviceを取得するプログラムを作成する
450-
if (dev) {
451-
device_del(dev);
452-
}
453-
}
454-
455-
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 16, 0)
456-
/* spiをサーチする関数 */
457-
static int __callback_find_mcp3204(struct device *dev, void *data)
458-
{
459-
printk(KERN_INFO " device_name: %s\n", dev->driver->name);
460-
if (mcp320x_dev == NULL && strcmp(dev->driver->name, "mcp320x") == 0) {
461-
mcp320x_dev = dev;
462-
mcp3204_probe(to_spi_device(dev));
463-
}
464-
return 0;
465-
}
466-
#endif
467-
468-
/*
469-
* mcp3204_init - initialize MCP3204
470-
* called by 'dev_init_module'
471-
*/
472-
static int mcp3204_init(void)
473-
{
474-
475-
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 16, 0)
476-
bus_for_each_dev(&spi_bus_type, NULL, NULL, __callback_find_mcp3204);
477-
#else
478-
struct spi_master *master;
479-
struct spi_device *spi_device;
480-
481-
spi_register_driver(&mcp3204_driver);
482-
483-
mcp3204_info.bus_num = SPI_BUS_NUM;
484-
mcp3204_info.chip_select = SPI_CHIP_SELECT;
485-
486-
master = spi_busnum_to_master(mcp3204_info.bus_num);
487-
488-
if (!master) {
489-
printk(KERN_ERR "%s: spi_busnum_to_master returned NULL\n",
490-
__func__);
491-
spi_unregister_driver(&mcp3204_driver);
492-
return -ENODEV;
493-
}
494-
495-
spi_remove_device(master, mcp3204_info.chip_select);
496-
497-
spi_device = spi_new_device(master, &mcp3204_info);
498-
if (!spi_device) {
499-
printk(KERN_ERR "%s: spi_new_device returned NULL\n", __func__);
500-
spi_unregister_driver(&mcp3204_driver);
501-
return -ENODEV;
502-
}
503-
#endif
504-
505-
return 0;
506-
}
507-
508-
/*
509-
* mcp3204_exit - cleanup MCP3204
510-
* called by dev_cleanup_module()
511-
*/
512-
static void mcp3204_exit(void)
513-
{
514-
515-
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 16, 0)
516-
printk(KERN_INFO " mcp3204_exit\n");
517-
if (mcp320x_dev) {
518-
mcp3204_remove(to_spi_device(mcp320x_dev));
519-
}
520-
#else
521-
struct spi_master *master;
522-
master = spi_busnum_to_master(mcp3204_info.bus_num);
523-
524-
if (master) {
525-
spi_remove_device(master, mcp3204_info.chip_select);
526-
} else {
527-
printk(KERN_ERR "mcp3204 remove error\n");
528-
}
529-
530-
spi_unregister_driver(&mcp3204_driver);
531-
#endif
532-
}
533-
534335
static int rtcntr_i2c_create_cdev(struct rtcnt_device_info *dev_info)
535336
{
536337
int minor;

0 commit comments

Comments
 (0)