Compare commits
16 Commits
8788f8aff5
...
main
Author | SHA1 | Date | |
---|---|---|---|
9595b72bc1 | |||
8145e1c790 | |||
49d5847980 | |||
34ea87ded0 | |||
48e9f86d33 | |||
738d2ffa8e | |||
46acdf410e | |||
e8f5493e5d | |||
a6c4370fc4 | |||
9b87645a3d | |||
8dd3c67c05 | |||
581de7dabd | |||
3714d84481 | |||
184b877b34 | |||
17ad8044fb | |||
31e308efe9 |
@ -1,30 +1,31 @@
|
|||||||
NAME = counter
|
NAME = counter
|
||||||
|
OBJS = init.o leds.o ls7366r.o trigger.o io.o
|
||||||
|
KDIR := /lib/modules/$(shell uname -r)/build
|
||||||
|
DTC_FLAGS := -@ -I dts -O dtb
|
||||||
|
|
||||||
obj-m += $(NAME).o
|
obj-m += $(NAME).o
|
||||||
|
$(NAME)-objs := $(OBJS)
|
||||||
|
|
||||||
all: $(NAME).ko $(NAME).dtbo
|
all: $(NAME).ko $(NAME).dtbo
|
||||||
echo Builded Device Tree Overlay and kernel module
|
echo Builded Device Tree Overlay and kernel module
|
||||||
|
|
||||||
$(NAME).ko: $(NAME).c
|
$(NAME).ko:
|
||||||
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
|
make -C $(KDIR) M=$(PWD) modules
|
||||||
|
|
||||||
$(NAME).dtbo: $(NAME).dts
|
$(NAME).dtbo: $(NAME).dts
|
||||||
dtc -@ -I dts -O dtb -o $@ $<
|
dtc $(DTC_FLAGS) -o $@ $<
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
|
make -C $(KDIR) M=$(PWD) clean
|
||||||
rm -rf $(NAME).dtbo
|
rm -rf $(NAME).dtbo
|
||||||
|
|
||||||
load:
|
load:
|
||||||
sudo dtoverlay -d . $(NAME).dtbo
|
sudo dtoverlay -d . $(NAME).dtbo && echo "Overlay loaded"
|
||||||
@echo "Overlay loaded"
|
sudo insmod ./$(NAME).ko && echo "LKM loaded"
|
||||||
sudo insmod ./$(NAME).ko
|
|
||||||
@echo "LKM loaded"
|
|
||||||
|
|
||||||
unload:
|
unload:
|
||||||
sudo rmmod ./$(NAME).ko
|
sudo rmmod ./$(NAME).ko && echo "LKM unloaded"
|
||||||
@echo "LKM unloaded"
|
sudo dtoverlay -R $(NAME) && echo "Overlay unloaded"
|
||||||
sudo dtoverlay -R $(NAME)
|
|
||||||
@echo "Overlay unloaded"
|
|
||||||
|
|
||||||
list:
|
list:
|
||||||
sudo dtoverlay -l
|
sudo dtoverlay -l
|
||||||
|
112
driver/counter.c
112
driver/counter.c
@ -1,112 +0,0 @@
|
|||||||
#include <linux/module.h>
|
|
||||||
#include <linux/init.h>
|
|
||||||
#include <linux/mod_devicetable.h>
|
|
||||||
#include <linux/property.h>
|
|
||||||
#include <linux/platform_device.h>
|
|
||||||
#include <linux/of_device.h>
|
|
||||||
#include <linux/gpio/consumer.h>
|
|
||||||
|
|
||||||
/* Meta Information */
|
|
||||||
MODULE_LICENSE("GPL");
|
|
||||||
MODULE_AUTHOR("Wolfgang Hottgenroth");
|
|
||||||
MODULE_DESCRIPTION("A simple LKM to parse the device tree for a specific device and its properties");
|
|
||||||
|
|
||||||
/* Declate the probe and remove functions */
|
|
||||||
static int dt_probe(struct platform_device *pdev);
|
|
||||||
static void dt_remove(struct platform_device *pdev);
|
|
||||||
|
|
||||||
static struct of_device_id my_driver_ids[] = {
|
|
||||||
{
|
|
||||||
.compatible = "hottis,counter",
|
|
||||||
}, { /* sentinel */ }
|
|
||||||
};
|
|
||||||
MODULE_DEVICE_TABLE(of, my_driver_ids);
|
|
||||||
|
|
||||||
static struct platform_driver my_driver = {
|
|
||||||
.probe = dt_probe,
|
|
||||||
.remove = dt_remove,
|
|
||||||
.driver = {
|
|
||||||
.name = "my_device_driver",
|
|
||||||
.of_match_table = my_driver_ids,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
/* GPIO variable */
|
|
||||||
static struct gpio_desc *red_led = NULL;
|
|
||||||
static struct gpio_desc *blue_led = NULL;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief This function is called on loading the driver
|
|
||||||
*/
|
|
||||||
static int dt_probe(struct platform_device *pdev) {
|
|
||||||
struct device *dev = &pdev->dev;
|
|
||||||
|
|
||||||
printk("counter - 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief This function is called on unloading the driver
|
|
||||||
*/
|
|
||||||
static void dt_remove(struct platform_device *pdev) {
|
|
||||||
printk("counter - removing\n");
|
|
||||||
gpiod_put(red_led);
|
|
||||||
gpiod_put(blue_led);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief This function is called, when the module is loaded into the kernel
|
|
||||||
*/
|
|
||||||
static int __init my_init(void) {
|
|
||||||
printk("counter - Loading the driver...\n");
|
|
||||||
if(platform_driver_register(&my_driver)) {
|
|
||||||
printk("dt_gpio - Error! Could not load driver\n");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
printk("counter - red led on\n");
|
|
||||||
gpiod_set_value(red_led, 1);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief This function is called, when the module is removed from the kernel
|
|
||||||
*/
|
|
||||||
static void __exit my_exit(void) {
|
|
||||||
printk("counter - Unload driver");
|
|
||||||
|
|
||||||
printk("counter - red led off\n");
|
|
||||||
gpiod_set_value(red_led, 0);
|
|
||||||
|
|
||||||
platform_driver_unregister(&my_driver);
|
|
||||||
}
|
|
||||||
|
|
||||||
module_init(my_init);
|
|
||||||
module_exit(my_exit);
|
|
||||||
|
|
||||||
|
|
@ -3,14 +3,43 @@
|
|||||||
/ {
|
/ {
|
||||||
compatible = "brcm,bcm2835";
|
compatible = "brcm,bcm2835";
|
||||||
fragment@0 {
|
fragment@0 {
|
||||||
target-path = "/";
|
target-path = "/soc";
|
||||||
__overlay__ {
|
__overlay__ {
|
||||||
status = "okay";
|
status = "okay";
|
||||||
counter {
|
leds {
|
||||||
compatible = "hottis,counter";
|
compatible = "hottis,leds";
|
||||||
red-led-gpio = <&gpio 26 0>;
|
red-led-gpio = <&gpio 26 0>;
|
||||||
blue-led-gpio = <&gpio 21 0>;
|
blue-led-gpio = <&gpio 21 0>;
|
||||||
};
|
};
|
||||||
|
intr {
|
||||||
|
compatible = "hottis,intr";
|
||||||
|
intr-gpio = <&gpio 19 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";
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
};
|
};
|
||||||
|
26
driver/counter_io_calls.h
Normal file
26
driver/counter_io_calls.h
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
#ifndef _COUNTER_IO_CALLS_H_
|
||||||
|
#define _COUNTER_IO_CALLS_H_
|
||||||
|
|
||||||
|
#ifndef __KERNEL__
|
||||||
|
#include <stdint.h>
|
||||||
|
#define u64 uint64_t
|
||||||
|
#define u32 uint32_t
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include "leds_codes.h"
|
||||||
|
|
||||||
|
struct observation {
|
||||||
|
u64 ts_sec;
|
||||||
|
u64 ts_nsec;
|
||||||
|
u32 value;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct led_cmd {
|
||||||
|
enum led_color_e color;
|
||||||
|
enum led_state_e state;
|
||||||
|
};
|
||||||
|
|
||||||
|
#define LED_CMD _IOW('l', 'c', struct led_cmd *);
|
||||||
|
|
||||||
|
|
||||||
|
#endif /* _COUNTER_IO_CALLS_H_ */
|
64
driver/init.c
Normal file
64
driver/init.c
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
#include <linux/module.h>
|
||||||
|
#include <linux/init.h>
|
||||||
|
#include <linux/platform_device.h>
|
||||||
|
#include <linux/spi/spi.h>
|
||||||
|
|
||||||
|
#include "ls7366r.h"
|
||||||
|
#include "trigger.h"
|
||||||
|
#include "leds.h"
|
||||||
|
#include "io.h"
|
||||||
|
|
||||||
|
|
||||||
|
MODULE_LICENSE("GPL");
|
||||||
|
MODULE_AUTHOR("Wolfgang Hottgenroth");
|
||||||
|
MODULE_DESCRIPTION("Driver for the MainsCnt measurement hat");
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/* module loading and unloading */
|
||||||
|
static int __init my_init(void) {
|
||||||
|
printk("counter - Loading IO subsystem...\n");
|
||||||
|
if (io_init()) {
|
||||||
|
printk("counter - Error! Could not load IO subsystem\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
printk("counter - Loading the leds driver...\n");
|
||||||
|
if(platform_driver_register(&leds_driver)) {
|
||||||
|
printk("counter - Error! Could not load leds 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 - Loading the interrupt driver...\n");
|
||||||
|
if(platform_driver_register(&interrupt_driver)) {
|
||||||
|
printk("counter - Error! Could not load interrupt driver\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void __exit my_exit(void) {
|
||||||
|
printk("counter - Unloading the leds driver...\n");
|
||||||
|
platform_driver_unregister(&leds_driver);
|
||||||
|
|
||||||
|
printk("counter - Unloading the ls7366r driver...\n");
|
||||||
|
spi_unregister_driver(&ls7366r_driver);
|
||||||
|
|
||||||
|
printk("counter - Unloading the interrupt driver...\n");
|
||||||
|
platform_driver_unregister(&interrupt_driver);
|
||||||
|
|
||||||
|
printk("counter - Unloading IO subsystem...\n");
|
||||||
|
io_exit();
|
||||||
|
}
|
||||||
|
|
||||||
|
module_init(my_init);
|
||||||
|
module_exit(my_exit);
|
||||||
|
|
||||||
|
|
53
driver/io.c
Normal file
53
driver/io.c
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
#include <linux/module.h>
|
||||||
|
#include <linux/init.h>
|
||||||
|
#include <linux/fs.h>
|
||||||
|
#include <linux/ioctl.h>
|
||||||
|
|
||||||
|
#include "io.h"
|
||||||
|
#include "counter_io_calls.h"
|
||||||
|
|
||||||
|
|
||||||
|
static dev_t io_device_no;
|
||||||
|
static struct class *io_device_class;
|
||||||
|
static struct cdev io_device;
|
||||||
|
|
||||||
|
#define DRIVER_NAME "cntiodev"
|
||||||
|
#define DRIVER_CLASS "CounterClass"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
static int io_open(struct inode *device_file, struct file *instance) {
|
||||||
|
printk("counter - Info! io open was called\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int io_close(struct inode *device_file, struct file *instance) {
|
||||||
|
printk("counter - Info! io close was called\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
static struct file_operations fops = {
|
||||||
|
.owner = THIS_MODULE,
|
||||||
|
.open = io_open,
|
||||||
|
.release = io_close,
|
||||||
|
};
|
||||||
|
|
||||||
|
int io_init(void) {
|
||||||
|
printk("counter - Info! io init\n");
|
||||||
|
|
||||||
|
if (alloc_chrdev_region(&io_device_no, 0, 1, DRIVER_NAME) < 0) {
|
||||||
|
printk("counter - ERROR! Device No. could not be allocated\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
printk("counter - Info! Device No. Major: %d, Minor: %d was registered!\n", my_device_nr >> 20, my_device_nr & 0xfffff);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void io_exit(void) {
|
||||||
|
}
|
||||||
|
|
8
driver/io.h
Normal file
8
driver/io.h
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
#ifndef _IO_H_
|
||||||
|
#define _IO_H_
|
||||||
|
|
||||||
|
|
||||||
|
int io_init(void);
|
||||||
|
void io_exit(void);
|
||||||
|
|
||||||
|
#endif /* _IO_H_ */
|
99
driver/leds.c
Normal file
99
driver/leds.c
Normal file
@ -0,0 +1,99 @@
|
|||||||
|
#include <linux/module.h>
|
||||||
|
#include <linux/init.h>
|
||||||
|
#include <linux/mod_devicetable.h>
|
||||||
|
#include <linux/property.h>
|
||||||
|
#include <linux/platform_device.h>
|
||||||
|
#include <linux/of_device.h>
|
||||||
|
#include <linux/gpio/consumer.h>
|
||||||
|
|
||||||
|
#include "leds.h"
|
||||||
|
|
||||||
|
|
||||||
|
/* leds stuff */
|
||||||
|
static int dt_leds_probe(struct platform_device *pdev);
|
||||||
|
static void dt_leds_remove(struct platform_device *pdev);
|
||||||
|
|
||||||
|
static struct of_device_id leds_driver_ids[] = {
|
||||||
|
{
|
||||||
|
.compatible = "hottis,leds",
|
||||||
|
},
|
||||||
|
{},
|
||||||
|
};
|
||||||
|
MODULE_DEVICE_TABLE(of, leds_driver_ids);
|
||||||
|
|
||||||
|
struct platform_driver leds_driver = {
|
||||||
|
.probe = dt_leds_probe,
|
||||||
|
.remove = dt_leds_remove,
|
||||||
|
.driver = {
|
||||||
|
.name = "leds_device_driver",
|
||||||
|
.of_match_table = leds_driver_ids,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static struct gpio_desc *red_led = NULL;
|
||||||
|
static struct gpio_desc *blue_led = NULL;
|
||||||
|
|
||||||
|
static int dt_leds_probe(struct platform_device *pdev) {
|
||||||
|
struct device *dev = &pdev->dev;
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
printk("counter - red led on\n");
|
||||||
|
gpiod_set_value(red_led, 1);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void dt_leds_remove(struct platform_device *pdev) {
|
||||||
|
printk("counter - red led off\n");
|
||||||
|
gpiod_set_value(red_led, 0);
|
||||||
|
|
||||||
|
printk("counter - leds removing\n");
|
||||||
|
gpiod_put(red_led);
|
||||||
|
gpiod_put(blue_led);
|
||||||
|
}
|
||||||
|
|
||||||
|
void led_ctrl(enum led_color_e color, enum led_state_e state) {
|
||||||
|
struct gpio_desc *led;
|
||||||
|
switch (color) {
|
||||||
|
case e_RED:
|
||||||
|
led = red_led;
|
||||||
|
break;
|
||||||
|
case e_BLUE:
|
||||||
|
led = blue_led;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
int st;
|
||||||
|
switch (state) {
|
||||||
|
case e_ON:
|
||||||
|
st = 1;
|
||||||
|
break;
|
||||||
|
case e_OFF:
|
||||||
|
st = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
gpiod_set_value(led, st);
|
||||||
|
}
|
||||||
|
|
13
driver/leds.h
Normal file
13
driver/leds.h
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
#ifndef _LEDS_H_
|
||||||
|
#define _LEDS_H_
|
||||||
|
|
||||||
|
#include <linux/platform_device.h>
|
||||||
|
|
||||||
|
#include "leds_codes.h"
|
||||||
|
|
||||||
|
|
||||||
|
extern struct platform_driver leds_driver;
|
||||||
|
|
||||||
|
void led_ctrl(enum led_color_e color, enum led_state_e state);
|
||||||
|
|
||||||
|
#endif /* _LEDS_H_ */
|
7
driver/leds_codes.h
Normal file
7
driver/leds_codes.h
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
#ifndef _LEDS_CODES_H_
|
||||||
|
#define _LEDS_CODES_H_
|
||||||
|
|
||||||
|
enum led_color_e { e_RED, e_BLUE };
|
||||||
|
enum led_state_e { e_ON, e_OFF };
|
||||||
|
|
||||||
|
#endif /* _LEDS_CODES_H_ */
|
142
driver/ls7366r.c
Normal file
142
driver/ls7366r.c
Normal file
@ -0,0 +1,142 @@
|
|||||||
|
#include <linux/module.h>
|
||||||
|
#include <linux/init.h>
|
||||||
|
#include <linux/mod_devicetable.h>
|
||||||
|
#include <linux/property.h>
|
||||||
|
#include <linux/platform_device.h>
|
||||||
|
#include <linux/of_device.h>
|
||||||
|
#include <linux/gpio/consumer.h>
|
||||||
|
#include <linux/spi/spi.h>
|
||||||
|
|
||||||
|
#include "ls7366r_registers.h"
|
||||||
|
#include "ls7366r.h"
|
||||||
|
|
||||||
|
|
||||||
|
/* 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;
|
||||||
|
}
|
||||||
|
*r = ((u32)rx_buf[1] << 24) | ((u32)rx_buf[2] << 16) | ((u32)rx_buf[3] << 8) | ((u32)rx_buf[4]);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int read_otr(struct spi_device *client, u32 *r) {
|
||||||
|
return read32(client, CMD_RD | REG_OTR, r);
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
*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);
|
||||||
|
|
||||||
|
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,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
struct spi_device *ls7366r_spi_client;
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
if (x != MDR0_ILO) {
|
||||||
|
printk("counter - Error! failed to initialize counter\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
ls7366r_spi_client = client;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void dt_ls7366r_remove(struct spi_device *client) {
|
||||||
|
printk("counter - ls7366r removing\n");
|
||||||
|
}
|
||||||
|
|
11
driver/ls7366r.h
Normal file
11
driver/ls7366r.h
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
#ifndef _LS7366R_H_
|
||||||
|
#define _LS7366R_H_
|
||||||
|
|
||||||
|
#include <linux/spi/spi.h>
|
||||||
|
|
||||||
|
extern struct spi_driver ls7366r_driver;
|
||||||
|
extern struct spi_device *ls7366r_spi_client;
|
||||||
|
|
||||||
|
int read_otr(struct spi_device *client, u32 *r);
|
||||||
|
|
||||||
|
#endif /* _LS7366R_H_ */
|
53
driver/ls7366r_registers.h
Normal file
53
driver/ls7366r_registers.h
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
#ifndef _LS7366R_REGISTERS_H_
|
||||||
|
#define _LS7366R_REGISTERS_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_REGISTERS_H_ */
|
129
driver/trigger.c
Normal file
129
driver/trigger.c
Normal file
@ -0,0 +1,129 @@
|
|||||||
|
#include <linux/module.h>
|
||||||
|
#include <linux/init.h>
|
||||||
|
#include <linux/mod_devicetable.h>
|
||||||
|
#include <linux/property.h>
|
||||||
|
#include <linux/platform_device.h>
|
||||||
|
#include <linux/of_device.h>
|
||||||
|
#include <linux/gpio/consumer.h>
|
||||||
|
#include <linux/spi/spi.h>
|
||||||
|
#include <linux/interrupt.h>
|
||||||
|
#include <linux/of_irq.h>
|
||||||
|
#include <linux/workqueue.h>
|
||||||
|
#include <linux/timekeeping.h>
|
||||||
|
|
||||||
|
#include "ls7366r.h"
|
||||||
|
#include "leds.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/* interrupt stuff */
|
||||||
|
static int dt_interrupt_probe(struct platform_device *pdev);
|
||||||
|
static void dt_interrupt_remove(struct platform_device *pdev);
|
||||||
|
|
||||||
|
static struct of_device_id interrupt_driver_ids[] = {
|
||||||
|
{
|
||||||
|
.compatible = "hottis,intr",
|
||||||
|
},
|
||||||
|
{},
|
||||||
|
};
|
||||||
|
MODULE_DEVICE_TABLE(of, interrupt_driver_ids);
|
||||||
|
|
||||||
|
struct platform_driver interrupt_driver = {
|
||||||
|
.probe = dt_interrupt_probe,
|
||||||
|
.remove = dt_interrupt_remove,
|
||||||
|
.driver = {
|
||||||
|
.name = "interrupt_device_driver",
|
||||||
|
.of_match_table = interrupt_driver_ids,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
static struct gpio_desc *counter_interrupt;
|
||||||
|
static int irq_number;
|
||||||
|
|
||||||
|
// workqueue
|
||||||
|
struct counter_irq_data {
|
||||||
|
struct work_struct work;
|
||||||
|
u64 timestamp;
|
||||||
|
struct spi_device *client;
|
||||||
|
struct timespec64 current_timestamp;
|
||||||
|
struct timespec64 last_timestamp;
|
||||||
|
u32 last_value;
|
||||||
|
};
|
||||||
|
static struct counter_irq_data counter_irq_wq_data;
|
||||||
|
|
||||||
|
static void counter_worker(struct work_struct *work) {
|
||||||
|
struct counter_irq_data *data = container_of(work, struct counter_irq_data, work);
|
||||||
|
|
||||||
|
u32 counter_value;
|
||||||
|
int ret = read_otr(data->client, &counter_value);
|
||||||
|
|
||||||
|
if (ret < 0) {
|
||||||
|
printk("counter - Error! read32 failed in counter_worker\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (data->last_value) {
|
||||||
|
u32 period = counter_value - data->last_value;
|
||||||
|
printk("counter %lld.%09lu %u\n",
|
||||||
|
(s64)((data->current_timestamp).tv_sec),
|
||||||
|
((data->current_timestamp).tv_nsec),
|
||||||
|
period);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
data->last_value = counter_value;
|
||||||
|
data->last_timestamp = data->current_timestamp;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ISR
|
||||||
|
static irqreturn_t counter_isr(int irq, void *dev_id) {
|
||||||
|
ktime_get_real_ts64(&counter_irq_wq_data.current_timestamp);
|
||||||
|
schedule_work(&counter_irq_wq_data.work);
|
||||||
|
return IRQ_HANDLED;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int dt_interrupt_probe(struct platform_device *pdev) {
|
||||||
|
struct device *dev = &pdev->dev;
|
||||||
|
|
||||||
|
printk("counter - interrupt probing\n");
|
||||||
|
|
||||||
|
INIT_WORK(&counter_irq_wq_data.work, counter_worker);
|
||||||
|
counter_irq_wq_data.client = ls7366r_spi_client;
|
||||||
|
counter_irq_wq_data.last_value = 0;
|
||||||
|
|
||||||
|
counter_interrupt = gpiod_get(dev, "intr", GPIOD_IN);
|
||||||
|
if (IS_ERR(counter_interrupt)) {
|
||||||
|
printk("counter - Error! Could not setup the GPIO intr\n");
|
||||||
|
return -1 * IS_ERR(counter_interrupt);
|
||||||
|
}
|
||||||
|
|
||||||
|
irq_number = gpiod_to_irq(counter_interrupt);
|
||||||
|
if (irq_number < 0) {
|
||||||
|
printk("counter - Error! Could not map GPIO to IRQ\n");
|
||||||
|
gpiod_put(counter_interrupt);
|
||||||
|
return irq_number;
|
||||||
|
}
|
||||||
|
printk("counter - Info! IRQ %d assigned\n", irq_number);
|
||||||
|
|
||||||
|
int ret = request_irq(irq_number, counter_isr,
|
||||||
|
IRQF_TRIGGER_RISING, "gpio19_irq", dev);
|
||||||
|
if (ret) {
|
||||||
|
printk("counter - Error! Could not request IRQ\n");
|
||||||
|
gpiod_put(counter_interrupt);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
printk("counter - Info! Interrupt assigned\n");
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void dt_interrupt_remove(struct platform_device *pdev) {
|
||||||
|
printk("counter - interrupt removing\n");
|
||||||
|
struct device *dev = &pdev->dev;
|
||||||
|
free_irq(irq_number, dev);
|
||||||
|
gpiod_put(counter_interrupt);
|
||||||
|
}
|
||||||
|
|
8
driver/trigger.h
Normal file
8
driver/trigger.h
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
#ifndef _TRIGGER_H_
|
||||||
|
#define _TRIGGER_H_
|
||||||
|
|
||||||
|
#include <linux/platform_device.h>
|
||||||
|
|
||||||
|
extern struct platform_driver interrupt_driver;
|
||||||
|
|
||||||
|
#endif /* _TRIGGER_H_ */
|
Reference in New Issue
Block a user