6 Commits

2 changed files with 132 additions and 14 deletions

View File

@ -6,6 +6,9 @@
#include <linux/of_device.h> #include <linux/of_device.h>
#include <linux/gpio/consumer.h> #include <linux/gpio/consumer.h>
#include <linux/spi/spi.h> #include <linux/spi/spi.h>
#include <linux/interrupt.h>
#include <linux/of_irq.h>
#include <linux/workqueue.h>
#include "ls7366r.h" #include "ls7366r.h"
@ -107,7 +110,6 @@ static int read32(struct spi_device *client, u8 c, u32 *r) {
printk("counter - Error! spi_sync_transfer failed in read32\n"); printk("counter - Error! spi_sync_transfer failed in read32\n");
return ret; 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]); *r = ((u32)rx_buf[1] << 24) | ((u32)rx_buf[2] << 16) | ((u32)rx_buf[3] << 8) | ((u32)rx_buf[4]);
return 0; return 0;
} }
@ -126,14 +128,11 @@ static int read8(struct spi_device *client, u8 c, u8 *r) {
printk("counter - Error! spi_sync_transfer failed in read8\n"); printk("counter - Error! spi_sync_transfer failed in read8\n");
return ret; return ret;
} }
printk("counter - Info! rx_buf: %02x %02x\n", rx_buf[0], rx_buf[1]);
*r = rx_buf[1]; *r = rx_buf[1];
return 0; return 0;
} }
static struct of_device_id ls7366r_driver_ids[] = { static struct of_device_id ls7366r_driver_ids[] = {
{ {
.compatible = "hottis,ls7366r", .compatible = "hottis,ls7366r",
@ -157,6 +156,8 @@ static struct spi_driver ls7366r_driver = {
}, },
}; };
static struct spi_device *spi_client;
static int dt_ls7366r_probe(struct spi_device *client) { static int dt_ls7366r_probe(struct spi_device *client) {
printk("counter - ls7366r probing\n"); printk("counter - ls7366r probing\n");
@ -188,16 +189,12 @@ static int dt_ls7366r_probe(struct spi_device *client) {
printk("counter - Error! read8 failed in probe\n"); printk("counter - Error! read8 failed in probe\n");
return ret; return ret;
} }
printk("counter - Info! x = %02x\n", x); if (x != MDR0_ILO) {
printk("counter - Error! failed to initialize counter\n");
u32 y; return -1;
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);
spi_client = client;
return 0; return 0;
} }
@ -207,11 +204,119 @@ static void dt_ls7366r_remove(struct spi_device *client) {
} }
/* 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);
static 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;
u64 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);
u64 ts = data->timestamp;
u32 counter_value;
int ret = read32(data->client, CMD_RD | REG_OTR, &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;
u64 duration = ts - data->last_timestamp;
printk("counter - Info! ts = %llu, duration = %llu, period = %u\n", ts, duration, period);
}
data->last_value = counter_value;
data->last_timestamp = ts;
}
// ISR
static irqreturn_t counter_isr(int irq, void *dev_id) {
counter_irq_wq_data.timestamp = ktime_get();
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 = spi_client;
counter_irq_wq_data.last_value = 0;
counter_irq_wq_data.last_timestamp = 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);
}
/* module loading and unloading */ /* module loading and unloading */
static int __init my_init(void) { static int __init my_init(void) {
printk("counter - Loading the leds driver...\n"); printk("counter - Loading the leds driver...\n");
if(platform_driver_register(&leds_driver)) { if(platform_driver_register(&leds_driver)) {
printk("dt_gpio - Error! Could not load driver\n"); printk("dt_gpio - Error! Could not load leds driver\n");
return -1; return -1;
} }
@ -221,6 +326,13 @@ static int __init my_init(void) {
return -1; return -1;
} }
printk("counter - Loading the interrupt driver...\n");
if(platform_driver_register(&interrupt_driver)) {
printk("dt_gpio - Error! Could not load interrupt driver\n");
return -1;
}
printk("counter - red led on\n"); printk("counter - red led on\n");
gpiod_set_value(red_led, 1); gpiod_set_value(red_led, 1);
@ -236,6 +348,9 @@ static void __exit my_exit(void) {
printk("counter - Unloading the ls7366r driver...\n"); printk("counter - Unloading the ls7366r driver...\n");
spi_unregister_driver(&ls7366r_driver); spi_unregister_driver(&ls7366r_driver);
printk("counter - Unloading the interrupt driver...\n");
platform_driver_unregister(&interrupt_driver);
} }
module_init(my_init); module_init(my_init);

View File

@ -6,12 +6,15 @@
target-path = "/soc"; target-path = "/soc";
__overlay__ { __overlay__ {
status = "okay"; status = "okay";
leds { leds {
compatible = "hottis,leds"; 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>;
};
}; };
}; };