]> git.karo-electronics.de Git - mv-sheeva.git/blobdiff - drivers/media/video/cx23885/cx23888-ir.c
[media] IR: initialize ir_raw_event in few more drivers
[mv-sheeva.git] / drivers / media / video / cx23885 / cx23888-ir.c
index 28ca90ff223c0a89fc870dcdee7046e397a91928..e78e3e4c8112c79c062b319adb966a66ca72f211 100644 (file)
@@ -26,6 +26,7 @@
 
 #include <media/v4l2-device.h>
 #include <media/v4l2-chip-ident.h>
+#include <media/ir-core.h>
 
 #include "cx23885.h"
 
@@ -113,8 +114,18 @@ MODULE_PARM_DESC(ir_888_debug, "enable debug messages [CX23888 IR controller]");
 #define CX23888_VIDCLK_FREQ    108000000 /* 108 MHz, BT.656 */
 #define CX23888_IR_REFCLK_FREQ (CX23888_VIDCLK_FREQ / 2)
 
-#define CX23888_IR_RX_KFIFO_SIZE       (512 * sizeof(u32))
-#define CX23888_IR_TX_KFIFO_SIZE       (512 * sizeof(u32))
+/*
+ * We use this union internally for convenience, but callers to tx_write
+ * and rx_read will be expecting records of type struct ir_raw_event.
+ * Always ensure the size of this union is dictated by struct ir_raw_event.
+ */
+union cx23888_ir_fifo_rec {
+       u32 hw_fifo_data;
+       struct ir_raw_event ir_core_data;
+};
+
+#define CX23888_IR_RX_KFIFO_SIZE    (256 * sizeof(union cx23888_ir_fifo_rec))
+#define CX23888_IR_TX_KFIFO_SIZE    (256 * sizeof(union cx23888_ir_fifo_rec))
 
 struct cx23888_ir_state {
        struct v4l2_subdev sd;
@@ -458,8 +469,8 @@ static u32 txclk_tx_s_max_pulse_width(struct cx23885_dev *dev, u32 ns,
 {
        u64 pulse_clocks;
 
-       if (ns > V4L2_SUBDEV_IR_PULSE_MAX_WIDTH_NS)
-               ns = V4L2_SUBDEV_IR_PULSE_MAX_WIDTH_NS;
+       if (ns > IR_MAX_DURATION)
+               ns = IR_MAX_DURATION;
        pulse_clocks = ns_to_pulse_clocks(ns);
        *divider = pulse_clocks_to_clock_divider(pulse_clocks);
        cx23888_ir_write4(dev, CX23888_IR_TXCLK_REG, *divider);
@@ -471,8 +482,8 @@ static u32 rxclk_rx_s_max_pulse_width(struct cx23885_dev *dev, u32 ns,
 {
        u64 pulse_clocks;
 
-       if (ns > V4L2_SUBDEV_IR_PULSE_MAX_WIDTH_NS)
-               ns = V4L2_SUBDEV_IR_PULSE_MAX_WIDTH_NS;
+       if (ns > IR_MAX_DURATION)
+               ns = IR_MAX_DURATION;
        pulse_clocks = ns_to_pulse_clocks(ns);
        *divider = pulse_clocks_to_clock_divider(pulse_clocks);
        cx23888_ir_write4(dev, CX23888_IR_RXCLK_REG, *divider);
@@ -535,8 +546,8 @@ static int cx23888_ir_irq_handler(struct v4l2_subdev *sd, u32 status,
        u32 irqen = cx23888_ir_read4(dev, CX23888_IR_IRQEN_REG);
        u32 stats = cx23888_ir_read4(dev, CX23888_IR_STATS_REG);
 
-       u32 rx_data[FIFO_RX_DEPTH];
-       int i, j, k;
+       union cx23888_ir_fifo_rec rx_data[FIFO_RX_DEPTH];
+       unsigned int i, j, k;
        u32 events, v;
        int tsr, rsr, rto, ror, tse, rse, rte, roe, kror;
 
@@ -597,11 +608,12 @@ static int cx23888_ir_irq_handler(struct v4l2_subdev *sd, u32 status,
                        for (j = 0;
                             (v & FIFO_RX_NDV) && j < FIFO_RX_DEPTH; j++) {
                                v = cx23888_ir_read4(dev, CX23888_IR_FIFO_REG);
-                               rx_data[i++] = v & ~FIFO_RX_NDV;
+                               rx_data[i].hw_fifo_data = v & ~FIFO_RX_NDV;
+                               i++;
                        }
                        if (i == 0)
                                break;
-                       j = i * sizeof(u32);
+                       j = i * sizeof(union cx23888_ir_fifo_rec);
                        k = kfifo_in_locked(&state->rx_kfifo,
                                      (unsigned char *) rx_data, j,
                                      &state->rx_kfifo_lock);
@@ -660,10 +672,11 @@ static int cx23888_ir_rx_read(struct v4l2_subdev *sd, u8 *buf, size_t count,
        u16 divider = (u16) atomic_read(&state->rxclk_divider);
 
        unsigned int i, n;
-       u32 *p;
-       u32 u, v;
+       union cx23888_ir_fifo_rec *p;
+       unsigned u, v;
 
-       n = count / sizeof(u32) * sizeof(u32);
+       n = count / sizeof(union cx23888_ir_fifo_rec)
+               * sizeof(union cx23888_ir_fifo_rec);
        if (n == 0) {
                *num = 0;
                return 0;
@@ -671,26 +684,29 @@ static int cx23888_ir_rx_read(struct v4l2_subdev *sd, u8 *buf, size_t count,
 
        n = kfifo_out_locked(&state->rx_kfifo, buf, n, &state->rx_kfifo_lock);
 
-       n /= sizeof(u32);
-       *num = n * sizeof(u32);
+       n /= sizeof(union cx23888_ir_fifo_rec);
+       *num = n * sizeof(union cx23888_ir_fifo_rec);
 
-       for (p = (u32 *) buf, i = 0; i < n; p++, i++) {
-               if ((*p & FIFO_RXTX_RTO) == FIFO_RXTX_RTO) {
-                       *p = V4L2_SUBDEV_IR_PULSE_RX_SEQ_END;
+       for (p = (union cx23888_ir_fifo_rec *) buf, i = 0; i < n; p++, i++) {
+
+               if ((p->hw_fifo_data & FIFO_RXTX_RTO) == FIFO_RXTX_RTO) {
+                       /* Assume RTO was because of no IR light input */
+                       u = 0;
                        v4l2_dbg(2, ir_888_debug, sd, "rx read: end of rx\n");
-                       continue;
+               } else {
+                       u = (p->hw_fifo_data & FIFO_RXTX_LVL) ? 1 : 0;
+                       if (invert)
+                               u = u ? 0 : 1;
                }
 
-               u = (*p & FIFO_RXTX_LVL) ? V4L2_SUBDEV_IR_PULSE_LEVEL_MASK : 0;
-               if (invert)
-                       u = u ? 0 : V4L2_SUBDEV_IR_PULSE_LEVEL_MASK;
-
-               v = (u32) pulse_width_count_to_ns((u16) (*p & FIFO_RXTX),
-                                                 divider);
-               if (v >= V4L2_SUBDEV_IR_PULSE_MAX_WIDTH_NS)
-                       v = V4L2_SUBDEV_IR_PULSE_MAX_WIDTH_NS - 1;
+               v = (unsigned) pulse_width_count_to_ns(
+                                 (u16) (p->hw_fifo_data & FIFO_RXTX), divider);
+               if (v > IR_MAX_DURATION)
+                       v = IR_MAX_DURATION;
 
-               *p = u | v;
+               init_ir_raw_event(&p->ir_core_data);
+               p->ir_core_data.pulse = u;
+               p->ir_core_data.duration = v;
 
                v4l2_dbg(2, ir_888_debug, sd, "rx read: %10u ns  %s\n",
                         v, u ? "mark" : "space");
@@ -749,7 +765,8 @@ static int cx23888_ir_rx_s_parameters(struct v4l2_subdev *sd,
 
        o->mode = p->mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH;
 
-       o->bytes_per_data_element = p->bytes_per_data_element = sizeof(u32);
+       o->bytes_per_data_element = p->bytes_per_data_element
+                                 = sizeof(union cx23888_ir_fifo_rec);
 
        /* Before we tweak the hardware, we have to disable the receiver */
        irqenable_rx(dev, 0);
@@ -771,12 +788,15 @@ static int cx23888_ir_rx_s_parameters(struct v4l2_subdev *sd,
                                            &p->carrier_range_upper);
                o->carrier_range_lower = p->carrier_range_lower;
                o->carrier_range_upper = p->carrier_range_upper;
+
+               p->max_pulse_width =
+                       (u32) pulse_width_count_to_ns(FIFO_RXTX, rxclk_divider);
        } else {
                p->max_pulse_width =
                            rxclk_rx_s_max_pulse_width(dev, p->max_pulse_width,
                                                       &rxclk_divider);
-               o->max_pulse_width = p->max_pulse_width;
        }
+       o->max_pulse_width = p->max_pulse_width;
        atomic_set(&state->rxclk_divider, rxclk_divider);
 
        p->noise_filter_min_width =
@@ -873,7 +893,8 @@ static int cx23888_ir_tx_s_parameters(struct v4l2_subdev *sd,
 
        o->mode = p->mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH;
 
-       o->bytes_per_data_element = p->bytes_per_data_element = sizeof(u32);
+       o->bytes_per_data_element = p->bytes_per_data_element
+                                 = sizeof(union cx23888_ir_fifo_rec);
 
        /* Before we tweak the hardware, we have to disable the transmitter */
        irqenable_tx(dev, 0);
@@ -889,12 +910,15 @@ static int cx23888_ir_tx_s_parameters(struct v4l2_subdev *sd,
 
                p->duty_cycle = cduty_tx_s_duty_cycle(dev, p->duty_cycle);
                o->duty_cycle = p->duty_cycle;
+
+               p->max_pulse_width =
+                       (u32) pulse_width_count_to_ns(FIFO_RXTX, txclk_divider);
        } else {
                p->max_pulse_width =
                            txclk_tx_s_max_pulse_width(dev, p->max_pulse_width,
                                                       &txclk_divider);
-               o->max_pulse_width = p->max_pulse_width;
        }
+       o->max_pulse_width = p->max_pulse_width;
        atomic_set(&state->txclk_divider, txclk_divider);
 
        p->resolution = clock_divider_to_resolution(txclk_divider);
@@ -1000,12 +1024,10 @@ static int cx23888_ir_log_status(struct v4l2_subdev *sd)
                          "-%1d/+%1d, %u to %u Hz\n", i, j,
                          clock_divider_to_freq(rxclk, 16 + j),
                          clock_divider_to_freq(rxclk, 16 - i));
-       } else {
-               v4l2_info(sd, "\tMax measurable pulse width:        %u us, "
-                         "%llu ns\n",
-                         pulse_width_count_to_us(FIFO_RXTX, rxclk),
-                         pulse_width_count_to_ns(FIFO_RXTX, rxclk));
        }
+       v4l2_info(sd, "\tMax measurable pulse width:        %u us, %llu ns\n",
+                 pulse_width_count_to_us(FIFO_RXTX, rxclk),
+                 pulse_width_count_to_ns(FIFO_RXTX, rxclk));
        v4l2_info(sd, "\tLow pass filter:                   %s\n",
                  filtr ? "enabled" : "disabled");
        if (filtr)
@@ -1047,12 +1069,10 @@ static int cx23888_ir_log_status(struct v4l2_subdev *sd)
                          clock_divider_to_carrier_freq(txclk));
                v4l2_info(sd, "\tCarrier duty cycle:                %2u/16\n",
                          cduty + 1);
-       } else {
-               v4l2_info(sd, "\tMax pulse width:                   %u us, "
-                         "%llu ns\n",
-                         pulse_width_count_to_us(FIFO_RXTX, txclk),
-                         pulse_width_count_to_ns(FIFO_RXTX, txclk));
        }
+       v4l2_info(sd, "\tMax pulse width:                   %u us, %llu ns\n",
+                 pulse_width_count_to_us(FIFO_RXTX, txclk),
+                 pulse_width_count_to_ns(FIFO_RXTX, txclk));
        v4l2_info(sd, "\tBusy:                              %s\n",
                  stats & STATS_TBY ? "yes" : "no");
        v4l2_info(sd, "\tFIFO service requested:            %s\n",
@@ -1126,11 +1146,10 @@ static const struct v4l2_subdev_core_ops cx23888_ir_core_ops = {
        .g_register = cx23888_ir_g_register,
        .s_register = cx23888_ir_s_register,
 #endif
+       .interrupt_service_routine = cx23888_ir_irq_handler,
 };
 
 static const struct v4l2_subdev_ir_ops cx23888_ir_ir_ops = {
-       .interrupt_service_routine = cx23888_ir_irq_handler,
-
        .rx_read = cx23888_ir_rx_read,
        .rx_g_parameters = cx23888_ir_rx_g_parameters,
        .rx_s_parameters = cx23888_ir_rx_s_parameters,
@@ -1146,7 +1165,7 @@ static const struct v4l2_subdev_ops cx23888_ir_controller_ops = {
 };
 
 static const struct v4l2_subdev_ir_parameters default_rx_params = {
-       .bytes_per_data_element = sizeof(u32),
+       .bytes_per_data_element = sizeof(union cx23888_ir_fifo_rec),
        .mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH,
 
        .enable = false,
@@ -1165,7 +1184,7 @@ static const struct v4l2_subdev_ir_parameters default_rx_params = {
 };
 
 static const struct v4l2_subdev_ir_parameters default_tx_params = {
-       .bytes_per_data_element = sizeof(u32),
+       .bytes_per_data_element = sizeof(union cx23888_ir_fifo_rec),
        .mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH,
 
        .enable = false,