7 Commits

Author SHA1 Message Date
581de7dabd spi working, counter seems to 2025-06-13 09:47:13 +01:00
3714d84481 still works 2 2025-06-13 07:56:37 +01:00
8788f8aff5 works 2025-06-04 17:55:16 +01:00
f86f43b5fd untabify 2025-06-04 12:50:24 +01:00
1af4490a39 some progress indeed 2025-06-04 12:43:11 +01:00
95a3395313 remove proc stuff 2025-06-04 10:53:23 +01:00
772f53a890 use johannes4linux approach 2025-06-04 10:43:22 +01:00
7 changed files with 349 additions and 125 deletions

2
.gitignore vendored
View File

@ -14,6 +14,6 @@ driver/*.mod.c
driver/*.mod.o driver/*.mod.o
driver/*.o driver/*.o
driver/modules.order driver/modules.order
dtoverlay/*.dtbo driver/*.dtbo

View File

@ -1,11 +1,30 @@
obj-m += counter.o NAME = counter
obj-m += $(NAME).o
KDIR := /lib/modules/$(shell uname -r)/build all: $(NAME).ko $(NAME).dtbo
PWD := $(shell pwd) echo Builded Device Tree Overlay and kernel module
all: $(NAME).ko: $(NAME).c
$(MAKE) -C $(KDIR) M=$(PWD) modules make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
$(NAME).dtbo: $(NAME).dts
dtc -@ -I dts -O dtb -o $@ $<
clean: clean:
$(MAKE) -C $(KDIR) M=$(PWD) clean make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
rm -rf $(NAME).dtbo
load:
sudo dtoverlay -d . $(NAME).dtbo
@echo "Overlay loaded"
sudo insmod ./$(NAME).ko
@echo "LKM loaded"
unload:
sudo rmmod ./$(NAME).ko
@echo "LKM unloaded"
sudo dtoverlay -R $(NAME)
@echo "Overlay unloaded"
list:
sudo dtoverlay -l

View File

@ -1,59 +1,244 @@
#include <linux/init.h>
#include <linux/module.h> #include <linux/module.h>
#include <linux/gpio/consumer.h> #include <linux/init.h>
#include <linux/delay.h> #include <linux/mod_devicetable.h>
#include <linux/property.h>
#include <linux/platform_device.h> #include <linux/platform_device.h>
#include <linux/of_device.h>
#include <linux/gpio/consumer.h>
#include <linux/spi/spi.h>
MODULE_LICENSE("GPL v2"); #include "ls7366r.h"
MODULE_AUTHOR("Du");
MODULE_DESCRIPTION("GPIO consumer API example for two LEDs");
static struct gpio_desc *led_blue;
static struct gpio_desc *led_red;
static int __init led_consumer_init(void) MODULE_LICENSE("GPL");
{ MODULE_AUTHOR("Wolfgang Hottgenroth");
struct device *dev = NULL; MODULE_DESCRIPTION("A simple LKM to parse the device tree for a specific device and its properties");
pr_info("GPIO consumer LED module loading\n"); /* leds stuff */
static int dt_leds_probe(struct platform_device *pdev);
static void dt_leds_remove(struct platform_device *pdev);
// GPIOs per Name aus dem Device Tree holen (via label oder node property) static struct of_device_id leds_driver_ids[] = {
led_blue = gpiod_get(dev, "led-blue", GPIOD_OUT_LOW); {
if (IS_ERR(led_blue)) { .compatible = "hottis,leds",
pr_err("Failed to get GPIO for blue LED\n"); },
return PTR_ERR(led_blue); {},
} };
MODULE_DEVICE_TABLE(of, leds_driver_ids);
led_red = gpiod_get(dev, "led-red", GPIOD_OUT_LOW); static struct platform_driver leds_driver = {
if (IS_ERR(led_red)) { .probe = dt_leds_probe,
pr_err("Failed to get GPIO for red LED\n"); .remove = dt_leds_remove,
gpiod_put(led_blue); .driver = {
return PTR_ERR(led_red); .name = "leds_device_driver",
} .of_match_table = leds_driver_ids,
},
};
// Test: LEDs einschalten static struct gpio_desc *red_led = NULL;
gpiod_set_value(led_blue, 1); static struct gpio_desc *blue_led = NULL;
gpiod_set_value(led_red, 1);
msleep(500);
gpiod_set_value(led_blue, 0);
gpiod_set_value(led_red, 0);
pr_info("LEDs blinked using gpio/consumer\n"); static int dt_leds_probe(struct platform_device *pdev) {
struct device *dev = &pdev->dev;
return 0; printk("counter - leds probing\n");
if(!device_property_present(dev, "red-led-gpio")) {
printk("counter - Error! Device property 'red-led-gpio' not found!\n");
return -1;
}
if(!device_property_present(dev, "blue-led-gpio")) {
printk("counter - Error! Device property 'blue-led-gpio' not found!\n");
return -1;
}
/* Init GPIO */
red_led = gpiod_get(dev, "red-led", GPIOD_OUT_LOW);
if(IS_ERR(red_led)) {
printk("counter - Error! Could not setup the GPIO red-led\n");
return -1 * IS_ERR(red_led);
}
blue_led = gpiod_get(dev, "blue-led", GPIOD_OUT_LOW);
if(IS_ERR(blue_led)) {
printk("counter - Error! Could not setup the GPIO blue-led\n");
return -1 * IS_ERR(blue_led);
}
return 0;
} }
static void __exit led_consumer_exit(void) static void dt_leds_remove(struct platform_device *pdev) {
{ printk("counter - leds removing\n");
gpiod_set_value(led_blue, 0); gpiod_put(red_led);
gpiod_set_value(led_red, 0); gpiod_put(blue_led);
gpiod_put(led_blue);
gpiod_put(led_red);
pr_info("GPIO consumer LED module unloaded\n");
} }
module_init(led_consumer_init);
module_exit(led_consumer_exit); /* spi stuff */
static int dt_ls7366r_probe(struct spi_device *client);
static void dt_ls7366r_remove(struct spi_device *client);
/* LS7366R functions */
static int writeCmd(struct spi_device *client, u8 c) {
u8 buf[1];
buf[0] = c;
return spi_write(client, buf, 1);
}
static int writeCmdData(struct spi_device *client, u8 c, u8 d) {
u8 buf[2];
buf[0] = c;
buf[1] = d;
return spi_write(client, buf, 2);
}
static int read32(struct spi_device *client, u8 c, u32 *r) {
u8 tx_buf[5];
tx_buf[0] = c;
u8 rx_buf[5];
struct spi_transfer t = {
.tx_buf = tx_buf,
.rx_buf = rx_buf,
.len = 5,
};
int ret = spi_sync_transfer(client, &t, 1);
if (ret < 0) {
printk("counter - Error! spi_sync_transfer failed in read32\n");
return ret;
}
printk("counter - Info! rx_buf: %02x %02x %02x %02x %02x\n", rx_buf[0], rx_buf[1], rx_buf[2], rx_buf[3], rx_buf[4]);
*r = ((u32)rx_buf[1] << 24) | ((u32)rx_buf[2] << 16) | ((u32)rx_buf[3] << 8) | ((u32)rx_buf[4]);
return 0;
}
static int read8(struct spi_device *client, u8 c, u8 *r) {
u8 tx_buf[2];
tx_buf[0] = c;
u8 rx_buf[2];
struct spi_transfer t = {
.tx_buf = tx_buf,
.rx_buf = rx_buf,
.len = 2,
};
int ret = spi_sync_transfer(client, &t, 1);
if (ret < 0) {
printk("counter - Error! spi_sync_transfer failed in read8\n");
return ret;
}
printk("counter - Info! rx_buf: %02x %02x\n", rx_buf[0], rx_buf[1]);
*r = rx_buf[1];
return 0;
}
static struct of_device_id ls7366r_driver_ids[] = {
{
.compatible = "hottis,ls7366r",
}, {}
};
MODULE_DEVICE_TABLE(of, ls7366r_driver_ids);
static struct spi_device_id ls7366r[] = {
{ "ls7366r", 0 },
{},
};
MODULE_DEVICE_TABLE(spi, ls7366r);
static struct spi_driver ls7366r_driver = {
.probe = dt_ls7366r_probe,
.remove = dt_ls7366r_remove,
.id_table = ls7366r,
.driver = {
.name = "ls7366r_device_driver",
.of_match_table = ls7366r_driver_ids,
},
};
static int dt_ls7366r_probe(struct spi_device *client) {
printk("counter - ls7366r probing\n");
int ret = spi_setup(client);
if (ret < 0) {
printk("counter - Error! Failed to set up the SPI bus\n");
return ret;
}
ret = writeCmd(client, CMD_CLR | REG_STR);
if (ret < 0) {
printk("counter - Error! writeCmd 1 failed in probe\n");
return ret;
}
ret = writeCmd(client, CMD_CLR | REG_CNTR);
if (ret < 0) {
printk("counter - Error! writeCmd 2 failed in probe\n");
return ret;
}
ret = writeCmdData(client, CMD_WR | REG_MDR0, MDR0_ILO);
if (ret < 0) {
printk("counter - Error! writeCmdData failed in probe\n");
return ret;
}
u8 x;
ret = read8(client, CMD_RD | REG_MDR0, &x);
if (ret < 0) {
printk("counter - Error! read8 failed in probe\n");
return ret;
}
printk("counter - Info! x = %02x\n", x);
u32 y;
ret = read32(client, CMD_RD | REG_OTR, &y);
if (ret < 0) {
printk("counter - Error! read32 failed in probe\n");
return ret;
}
printk("counter - Info! y = %d\n", y);
return 0;
}
static void dt_ls7366r_remove(struct spi_device *client) {
printk("counter - ls7366r removing\n");
}
/* module loading and unloading */
static int __init my_init(void) {
printk("counter - Loading the leds driver...\n");
if(platform_driver_register(&leds_driver)) {
printk("dt_gpio - Error! Could not load driver\n");
return -1;
}
printk("counter - Loading the ls7366r driver...\n");
if (spi_register_driver(&ls7366r_driver)) {
printk("counter - Error! Could not load spi driver\n");
return -1;
}
printk("counter - red led on\n");
gpiod_set_value(red_led, 1);
return 0;
}
static void __exit my_exit(void) {
printk("counter - red led off\n");
gpiod_set_value(red_led, 0);
printk("counter - Unloading the leds driver...\n");
platform_driver_unregister(&leds_driver);
printk("counter - Unloading the ls7366r driver...\n");
spi_unregister_driver(&ls7366r_driver);
}
module_init(my_init);
module_exit(my_exit);

42
driver/counter.dts Normal file
View File

@ -0,0 +1,42 @@
/dts-v1/;
/plugin/;
/ {
compatible = "brcm,bcm2835";
fragment@0 {
target-path = "/soc";
__overlay__ {
status = "okay";
leds {
compatible = "hottis,leds";
red-led-gpio = <&gpio 26 0>;
blue-led-gpio = <&gpio 21 0>;
};
};
};
fragment@1 {
target = <&spidev0>;
__overlay__ {
status = "disabled";
};
};
fragment@2 {
target = <&spi0>;
__overlay__ {
status = "okay";
#address-cells = <1>;
#size-cells = <0>;
ls7366r: ls7366r@0 {
compatible = "hottis,ls7366r";
reg = <0x0>;
spi-max-frequency = <1000000>;
spi-bits-per-word = <8>;
status = "okay";
};
};
};
};

53
driver/ls7366r.h Normal file
View File

@ -0,0 +1,53 @@
#ifndef _LS7366R_H_
#define _LS7366R_H_
#define REG_MDR0 0b00001000
#define REG_MDR1 0b00010000
#define REG_DTR 0b00011000
#define REG_CNTR 0b00100000
#define REG_OTR 0b00101000
#define REG_STR 0b00110000
#define CMD_CLR 0b00000000
#define CMD_RD 0b01000000
#define CMD_WR 0b10000000
#define CMD_LOAD 0b11000000
#define STR_CY 0b10000000
#define STR_BW 0b01000000
#define STR_CMP 0b00100000
#define STR_IDX 0b00010000
#define STR_CEN 0b00001000
#define STR_PLS 0b00000100
#define STR_UD 0b00000010
#define STR_S 0b00000001
#define MDR0_NOQ 0b00000000
#define MDR0_Q1 0b00000001
#define MDR0_Q2 0b00000010
#define MDR0_Q4 0b00000011
#define MDR0_FRC 0b00000000
#define MDR0_SCC 0b00000100
#define MDR0_RLC 0b00001000
#define MDR0_MNC 0b00001100
#define MDR0_DI 0b00000000
#define MDR0_ILC 0b00010000
#define MDR0_IRC 0b00100000
#define MDR0_ILO 0b00110000
#define MDR0_AI 0b00000000
#define MDR0_SI 0b01000000
#define MDR0_FC1 0b00000000
#define MDR0_FC2 0b10000000
#define MDR1_4CM 0b00000000
#define MDR1_3CM 0b00000001
#define MDR1_2CM 0b00000010
#define MDR1_1CM 0b00000011
#define MDR1_EC 0b00000000
#define MDR1_DC 0b00000100
#define MDR1_F_IDX 0b00010000
#define MDR1_F_CMP 0b00100000
#define MDR1_F_BW 0b01000000
#define MDR1_F_CY 0b10000000
#endif /* _LS7366R_H_ */

View File

@ -1,29 +0,0 @@
# Source and target file names
DTS = counter.dts
DTBO = counter.dtbo
# Installation paths
OVERLAY_DIR = /boot/overlays
CONFIG_TXT = /boot/firmware/config.txt
OVERLAY_NAME = counter
all: $(DTBO)
$(DTBO): $(DTS)
dtc -@ -I dts -O dtb -o $@ $<
load: $(DTBO)
sudo dtoverlay -d . $(OVERLAY_NAME)
@echo "Overlay loaded"
unload: $(DTBO)
sudo dtoverlay -R $(OVERLAY_NAME)
@echo "Overlay unloaded"
list:
sudo dtoverlay -l
clean:
rm -f *.dtbo

View File

@ -1,46 +0,0 @@
/dts-v1/;
/plugin/;
/ {
compatible = "brcm,bcm2835";
fragment@0 {
target = <&gpio>;
__overlay__ {
gpio_led_blue: gpio_led_blues@26 {
brcm,pins = <26>;
brcm,function = <1>; // output
brcm,pull = <0>; // no pull
};
gpio_led_red: gpio_led_reds@21 {
brcm,pins = <21>;
brcm,function = <1>; // output
brcm,pull = <0>; // no pull
};
};
};
fragment@1 {
target-path = "/";
__overlay__ {
mygpioleds {
compatible = "my,gpio-led-controller";
pinctrl-names = "default";
pinctrl-0 = <&gpio_led_blue &gpio_led_red>;
led-blue {
gpios = <&gpio 26 0>;
label = "led-blue";
};
led-red {
gpios = <&gpio 21 0>;
label = "led-red";
};
};
};
};
};