Compare commits
2 Commits
spi_workin
...
a6c4370fc4
Author | SHA1 | Date | |
---|---|---|---|
a6c4370fc4 | |||
9b87645a3d |
141
driver/counter.c
141
driver/counter.c
@ -6,6 +6,9 @@
|
||||
#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 "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");
|
||||
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;
|
||||
}
|
||||
@ -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");
|
||||
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",
|
||||
@ -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) {
|
||||
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");
|
||||
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;
|
||||
if (x != MDR0_ILO) {
|
||||
printk("counter - Error! failed to initialize counter\n");
|
||||
return -1;
|
||||
}
|
||||
printk("counter - Info! y = %d\n", y);
|
||||
|
||||
spi_client = client;
|
||||
|
||||
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 */
|
||||
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");
|
||||
printk("dt_gpio - Error! Could not load leds driver\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -221,6 +326,13 @@ static int __init my_init(void) {
|
||||
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");
|
||||
gpiod_set_value(red_led, 1);
|
||||
|
||||
@ -236,6 +348,9 @@ static void __exit my_exit(void) {
|
||||
|
||||
printk("counter - Unloading the ls7366r driver...\n");
|
||||
spi_unregister_driver(&ls7366r_driver);
|
||||
|
||||
printk("counter - Unloading the interrupt driver...\n");
|
||||
platform_driver_unregister(&interrupt_driver);
|
||||
}
|
||||
|
||||
module_init(my_init);
|
||||
|
@ -11,6 +11,10 @@
|
||||
red-led-gpio = <&gpio 26 0>;
|
||||
blue-led-gpio = <&gpio 21 0>;
|
||||
};
|
||||
intr {
|
||||
compatible = "hottis,intr";
|
||||
intr-gpio = <&gpio 19 0>;
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
|
Reference in New Issue
Block a user