diff --git a/driver/counter.c b/driver/counter.c index 68b6b4f..e192cbd 100644 --- a/driver/counter.c +++ b/driver/counter.c @@ -8,6 +8,7 @@ #include #include #include +#include #include "ls7366r.h" @@ -109,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; } @@ -128,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", @@ -159,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"); @@ -190,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; } @@ -233,9 +228,41 @@ static struct platform_driver interrupt_driver = { 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) { - printk("counter - interrupt received\n"); + counter_irq_wq_data.timestamp = ktime_get(); + schedule_work(&counter_irq_wq_data.work); return IRQ_HANDLED; } @@ -244,6 +271,11 @@ static int dt_interrupt_probe(struct platform_device *pdev) { 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");