Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / media / i2c / cx25840 / cx25840-ir.c
blob8cef9656c61224861f1b0027be5e67fdec18cacd
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Driver for the Conexant CX2584x Audio/Video decoder chip and related cores
5 * Integrated Consumer Infrared Controller
7 * Copyright (C) 2010 Andy Walls <awalls@md.metrocast.net>
8 */
10 #include <linux/slab.h>
11 #include <linux/kfifo.h>
12 #include <linux/module.h>
13 #include <media/drv-intf/cx25840.h>
14 #include <media/rc-core.h>
16 #include "cx25840-core.h"
18 static unsigned int ir_debug;
19 module_param(ir_debug, int, 0644);
20 MODULE_PARM_DESC(ir_debug, "enable integrated IR debug messages");
22 #define CX25840_IR_REG_BASE 0x200
24 #define CX25840_IR_CNTRL_REG 0x200
25 #define CNTRL_WIN_3_3 0x00000000
26 #define CNTRL_WIN_4_3 0x00000001
27 #define CNTRL_WIN_3_4 0x00000002
28 #define CNTRL_WIN_4_4 0x00000003
29 #define CNTRL_WIN 0x00000003
30 #define CNTRL_EDG_NONE 0x00000000
31 #define CNTRL_EDG_FALL 0x00000004
32 #define CNTRL_EDG_RISE 0x00000008
33 #define CNTRL_EDG_BOTH 0x0000000C
34 #define CNTRL_EDG 0x0000000C
35 #define CNTRL_DMD 0x00000010
36 #define CNTRL_MOD 0x00000020
37 #define CNTRL_RFE 0x00000040
38 #define CNTRL_TFE 0x00000080
39 #define CNTRL_RXE 0x00000100
40 #define CNTRL_TXE 0x00000200
41 #define CNTRL_RIC 0x00000400
42 #define CNTRL_TIC 0x00000800
43 #define CNTRL_CPL 0x00001000
44 #define CNTRL_LBM 0x00002000
45 #define CNTRL_R 0x00004000
47 #define CX25840_IR_TXCLK_REG 0x204
48 #define TXCLK_TCD 0x0000FFFF
50 #define CX25840_IR_RXCLK_REG 0x208
51 #define RXCLK_RCD 0x0000FFFF
53 #define CX25840_IR_CDUTY_REG 0x20C
54 #define CDUTY_CDC 0x0000000F
56 #define CX25840_IR_STATS_REG 0x210
57 #define STATS_RTO 0x00000001
58 #define STATS_ROR 0x00000002
59 #define STATS_RBY 0x00000004
60 #define STATS_TBY 0x00000008
61 #define STATS_RSR 0x00000010
62 #define STATS_TSR 0x00000020
64 #define CX25840_IR_IRQEN_REG 0x214
65 #define IRQEN_RTE 0x00000001
66 #define IRQEN_ROE 0x00000002
67 #define IRQEN_RSE 0x00000010
68 #define IRQEN_TSE 0x00000020
69 #define IRQEN_MSK 0x00000033
71 #define CX25840_IR_FILTR_REG 0x218
72 #define FILTR_LPF 0x0000FFFF
74 #define CX25840_IR_FIFO_REG 0x23C
75 #define FIFO_RXTX 0x0000FFFF
76 #define FIFO_RXTX_LVL 0x00010000
77 #define FIFO_RXTX_RTO 0x0001FFFF
78 #define FIFO_RX_NDV 0x00020000
79 #define FIFO_RX_DEPTH 8
80 #define FIFO_TX_DEPTH 8
82 #define CX25840_VIDCLK_FREQ 108000000 /* 108 MHz, BT.656 */
83 #define CX25840_IR_REFCLK_FREQ (CX25840_VIDCLK_FREQ / 2)
86 * We use this union internally for convenience, but callers to tx_write
87 * and rx_read will be expecting records of type struct ir_raw_event.
88 * Always ensure the size of this union is dictated by struct ir_raw_event.
90 union cx25840_ir_fifo_rec {
91 u32 hw_fifo_data;
92 struct ir_raw_event ir_core_data;
95 #define CX25840_IR_RX_KFIFO_SIZE (256 * sizeof(union cx25840_ir_fifo_rec))
96 #define CX25840_IR_TX_KFIFO_SIZE (256 * sizeof(union cx25840_ir_fifo_rec))
98 struct cx25840_ir_state {
99 struct i2c_client *c;
101 struct v4l2_subdev_ir_parameters rx_params;
102 struct mutex rx_params_lock; /* protects Rx parameter settings cache */
103 atomic_t rxclk_divider;
104 atomic_t rx_invert;
106 struct kfifo rx_kfifo;
107 spinlock_t rx_kfifo_lock; /* protect Rx data kfifo */
109 struct v4l2_subdev_ir_parameters tx_params;
110 struct mutex tx_params_lock; /* protects Tx parameter settings cache */
111 atomic_t txclk_divider;
114 static inline struct cx25840_ir_state *to_ir_state(struct v4l2_subdev *sd)
116 struct cx25840_state *state = to_state(sd);
117 return state ? state->ir_state : NULL;
122 * Rx and Tx Clock Divider register computations
124 * Note the largest clock divider value of 0xffff corresponds to:
125 * (0xffff + 1) * 1000 / 108/2 MHz = 1,213,629.629... ns
126 * which fits in 21 bits, so we'll use unsigned int for time arguments.
128 static inline u16 count_to_clock_divider(unsigned int d)
130 if (d > RXCLK_RCD + 1)
131 d = RXCLK_RCD;
132 else if (d < 2)
133 d = 1;
134 else
135 d--;
136 return (u16) d;
139 static inline u16 carrier_freq_to_clock_divider(unsigned int freq)
141 return count_to_clock_divider(
142 DIV_ROUND_CLOSEST(CX25840_IR_REFCLK_FREQ, freq * 16));
145 static inline unsigned int clock_divider_to_carrier_freq(unsigned int divider)
147 return DIV_ROUND_CLOSEST(CX25840_IR_REFCLK_FREQ, (divider + 1) * 16);
150 static inline unsigned int clock_divider_to_freq(unsigned int divider,
151 unsigned int rollovers)
153 return DIV_ROUND_CLOSEST(CX25840_IR_REFCLK_FREQ,
154 (divider + 1) * rollovers);
158 * Low Pass Filter register calculations
160 * Note the largest count value of 0xffff corresponds to:
161 * 0xffff * 1000 / 108/2 MHz = 1,213,611.11... ns
162 * which fits in 21 bits, so we'll use unsigned int for time arguments.
164 static inline u16 count_to_lpf_count(unsigned int d)
166 if (d > FILTR_LPF)
167 d = FILTR_LPF;
168 else if (d < 4)
169 d = 0;
170 return (u16) d;
173 static inline u16 ns_to_lpf_count(unsigned int ns)
175 return count_to_lpf_count(
176 DIV_ROUND_CLOSEST(CX25840_IR_REFCLK_FREQ / 1000000 * ns, 1000));
179 static inline unsigned int lpf_count_to_ns(unsigned int count)
181 /* Duration of the Low Pass Filter rejection window in ns */
182 return DIV_ROUND_CLOSEST(count * 1000,
183 CX25840_IR_REFCLK_FREQ / 1000000);
186 static inline unsigned int lpf_count_to_us(unsigned int count)
188 /* Duration of the Low Pass Filter rejection window in us */
189 return DIV_ROUND_CLOSEST(count, CX25840_IR_REFCLK_FREQ / 1000000);
193 * FIFO register pulse width count computations
195 static u32 clock_divider_to_resolution(u16 divider)
198 * Resolution is the duration of 1 tick of the readable portion of
199 * the pulse width counter as read from the FIFO. The two lsb's are
200 * not readable, hence the << 2. This function returns ns.
202 return DIV_ROUND_CLOSEST((1 << 2) * ((u32) divider + 1) * 1000,
203 CX25840_IR_REFCLK_FREQ / 1000000);
206 static u64 pulse_width_count_to_ns(u16 count, u16 divider)
208 u64 n;
209 u32 rem;
212 * The 2 lsb's of the pulse width timer count are not readable, hence
213 * the (count << 2) | 0x3
215 n = (((u64) count << 2) | 0x3) * (divider + 1) * 1000; /* millicycles */
216 rem = do_div(n, CX25840_IR_REFCLK_FREQ / 1000000); /* / MHz => ns */
217 if (rem >= CX25840_IR_REFCLK_FREQ / 1000000 / 2)
218 n++;
219 return n;
222 #if 0
223 /* Keep as we will need this for Transmit functionality */
224 static u16 ns_to_pulse_width_count(u32 ns, u16 divider)
226 u64 n;
227 u32 d;
228 u32 rem;
231 * The 2 lsb's of the pulse width timer count are not accessible, hence
232 * the (1 << 2)
234 n = ((u64) ns) * CX25840_IR_REFCLK_FREQ / 1000000; /* millicycles */
235 d = (1 << 2) * ((u32) divider + 1) * 1000; /* millicycles/count */
236 rem = do_div(n, d);
237 if (rem >= d / 2)
238 n++;
240 if (n > FIFO_RXTX)
241 n = FIFO_RXTX;
242 else if (n == 0)
243 n = 1;
244 return (u16) n;
247 #endif
248 static unsigned int pulse_width_count_to_us(u16 count, u16 divider)
250 u64 n;
251 u32 rem;
254 * The 2 lsb's of the pulse width timer count are not readable, hence
255 * the (count << 2) | 0x3
257 n = (((u64) count << 2) | 0x3) * (divider + 1); /* cycles */
258 rem = do_div(n, CX25840_IR_REFCLK_FREQ / 1000000); /* / MHz => us */
259 if (rem >= CX25840_IR_REFCLK_FREQ / 1000000 / 2)
260 n++;
261 return (unsigned int) n;
265 * Pulse Clocks computations: Combined Pulse Width Count & Rx Clock Counts
267 * The total pulse clock count is an 18 bit pulse width timer count as the most
268 * significant part and (up to) 16 bit clock divider count as a modulus.
269 * When the Rx clock divider ticks down to 0, it increments the 18 bit pulse
270 * width timer count's least significant bit.
272 static u64 ns_to_pulse_clocks(u32 ns)
274 u64 clocks;
275 u32 rem;
276 clocks = CX25840_IR_REFCLK_FREQ / 1000000 * (u64) ns; /* millicycles */
277 rem = do_div(clocks, 1000); /* /1000 = cycles */
278 if (rem >= 1000 / 2)
279 clocks++;
280 return clocks;
283 static u16 pulse_clocks_to_clock_divider(u64 count)
285 do_div(count, (FIFO_RXTX << 2) | 0x3);
287 /* net result needs to be rounded down and decremented by 1 */
288 if (count > RXCLK_RCD + 1)
289 count = RXCLK_RCD;
290 else if (count < 2)
291 count = 1;
292 else
293 count--;
294 return (u16) count;
298 * IR Control Register helpers
300 enum tx_fifo_watermark {
301 TX_FIFO_HALF_EMPTY = 0,
302 TX_FIFO_EMPTY = CNTRL_TIC,
305 enum rx_fifo_watermark {
306 RX_FIFO_HALF_FULL = 0,
307 RX_FIFO_NOT_EMPTY = CNTRL_RIC,
310 static inline void control_tx_irq_watermark(struct i2c_client *c,
311 enum tx_fifo_watermark level)
313 cx25840_and_or4(c, CX25840_IR_CNTRL_REG, ~CNTRL_TIC, level);
316 static inline void control_rx_irq_watermark(struct i2c_client *c,
317 enum rx_fifo_watermark level)
319 cx25840_and_or4(c, CX25840_IR_CNTRL_REG, ~CNTRL_RIC, level);
322 static inline void control_tx_enable(struct i2c_client *c, bool enable)
324 cx25840_and_or4(c, CX25840_IR_CNTRL_REG, ~(CNTRL_TXE | CNTRL_TFE),
325 enable ? (CNTRL_TXE | CNTRL_TFE) : 0);
328 static inline void control_rx_enable(struct i2c_client *c, bool enable)
330 cx25840_and_or4(c, CX25840_IR_CNTRL_REG, ~(CNTRL_RXE | CNTRL_RFE),
331 enable ? (CNTRL_RXE | CNTRL_RFE) : 0);
334 static inline void control_tx_modulation_enable(struct i2c_client *c,
335 bool enable)
337 cx25840_and_or4(c, CX25840_IR_CNTRL_REG, ~CNTRL_MOD,
338 enable ? CNTRL_MOD : 0);
341 static inline void control_rx_demodulation_enable(struct i2c_client *c,
342 bool enable)
344 cx25840_and_or4(c, CX25840_IR_CNTRL_REG, ~CNTRL_DMD,
345 enable ? CNTRL_DMD : 0);
348 static inline void control_rx_s_edge_detection(struct i2c_client *c,
349 u32 edge_types)
351 cx25840_and_or4(c, CX25840_IR_CNTRL_REG, ~CNTRL_EDG_BOTH,
352 edge_types & CNTRL_EDG_BOTH);
355 static void control_rx_s_carrier_window(struct i2c_client *c,
356 unsigned int carrier,
357 unsigned int *carrier_range_low,
358 unsigned int *carrier_range_high)
360 u32 v;
361 unsigned int c16 = carrier * 16;
363 if (*carrier_range_low < DIV_ROUND_CLOSEST(c16, 16 + 3)) {
364 v = CNTRL_WIN_3_4;
365 *carrier_range_low = DIV_ROUND_CLOSEST(c16, 16 + 4);
366 } else {
367 v = CNTRL_WIN_3_3;
368 *carrier_range_low = DIV_ROUND_CLOSEST(c16, 16 + 3);
371 if (*carrier_range_high > DIV_ROUND_CLOSEST(c16, 16 - 3)) {
372 v |= CNTRL_WIN_4_3;
373 *carrier_range_high = DIV_ROUND_CLOSEST(c16, 16 - 4);
374 } else {
375 v |= CNTRL_WIN_3_3;
376 *carrier_range_high = DIV_ROUND_CLOSEST(c16, 16 - 3);
378 cx25840_and_or4(c, CX25840_IR_CNTRL_REG, ~CNTRL_WIN, v);
381 static inline void control_tx_polarity_invert(struct i2c_client *c,
382 bool invert)
384 cx25840_and_or4(c, CX25840_IR_CNTRL_REG, ~CNTRL_CPL,
385 invert ? CNTRL_CPL : 0);
389 * IR Rx & Tx Clock Register helpers
391 static unsigned int txclk_tx_s_carrier(struct i2c_client *c,
392 unsigned int freq,
393 u16 *divider)
395 *divider = carrier_freq_to_clock_divider(freq);
396 cx25840_write4(c, CX25840_IR_TXCLK_REG, *divider);
397 return clock_divider_to_carrier_freq(*divider);
400 static unsigned int rxclk_rx_s_carrier(struct i2c_client *c,
401 unsigned int freq,
402 u16 *divider)
404 *divider = carrier_freq_to_clock_divider(freq);
405 cx25840_write4(c, CX25840_IR_RXCLK_REG, *divider);
406 return clock_divider_to_carrier_freq(*divider);
409 static u32 txclk_tx_s_max_pulse_width(struct i2c_client *c, u32 ns,
410 u16 *divider)
412 u64 pulse_clocks;
414 if (ns > IR_MAX_DURATION)
415 ns = IR_MAX_DURATION;
416 pulse_clocks = ns_to_pulse_clocks(ns);
417 *divider = pulse_clocks_to_clock_divider(pulse_clocks);
418 cx25840_write4(c, CX25840_IR_TXCLK_REG, *divider);
419 return (u32) pulse_width_count_to_ns(FIFO_RXTX, *divider);
422 static u32 rxclk_rx_s_max_pulse_width(struct i2c_client *c, u32 ns,
423 u16 *divider)
425 u64 pulse_clocks;
427 if (ns > IR_MAX_DURATION)
428 ns = IR_MAX_DURATION;
429 pulse_clocks = ns_to_pulse_clocks(ns);
430 *divider = pulse_clocks_to_clock_divider(pulse_clocks);
431 cx25840_write4(c, CX25840_IR_RXCLK_REG, *divider);
432 return (u32) pulse_width_count_to_ns(FIFO_RXTX, *divider);
436 * IR Tx Carrier Duty Cycle register helpers
438 static unsigned int cduty_tx_s_duty_cycle(struct i2c_client *c,
439 unsigned int duty_cycle)
441 u32 n;
442 n = DIV_ROUND_CLOSEST(duty_cycle * 100, 625); /* 16ths of 100% */
443 if (n != 0)
444 n--;
445 if (n > 15)
446 n = 15;
447 cx25840_write4(c, CX25840_IR_CDUTY_REG, n);
448 return DIV_ROUND_CLOSEST((n + 1) * 100, 16);
452 * IR Filter Register helpers
454 static u32 filter_rx_s_min_width(struct i2c_client *c, u32 min_width_ns)
456 u32 count = ns_to_lpf_count(min_width_ns);
457 cx25840_write4(c, CX25840_IR_FILTR_REG, count);
458 return lpf_count_to_ns(count);
462 * IR IRQ Enable Register helpers
464 static inline void irqenable_rx(struct v4l2_subdev *sd, u32 mask)
466 struct cx25840_state *state = to_state(sd);
468 if (is_cx23885(state) || is_cx23887(state))
469 mask ^= IRQEN_MSK;
470 mask &= (IRQEN_RTE | IRQEN_ROE | IRQEN_RSE);
471 cx25840_and_or4(state->c, CX25840_IR_IRQEN_REG,
472 ~(IRQEN_RTE | IRQEN_ROE | IRQEN_RSE), mask);
475 static inline void irqenable_tx(struct v4l2_subdev *sd, u32 mask)
477 struct cx25840_state *state = to_state(sd);
479 if (is_cx23885(state) || is_cx23887(state))
480 mask ^= IRQEN_MSK;
481 mask &= IRQEN_TSE;
482 cx25840_and_or4(state->c, CX25840_IR_IRQEN_REG, ~IRQEN_TSE, mask);
486 * V4L2 Subdevice IR Ops
488 int cx25840_ir_irq_handler(struct v4l2_subdev *sd, u32 status, bool *handled)
490 struct cx25840_state *state = to_state(sd);
491 struct cx25840_ir_state *ir_state = to_ir_state(sd);
492 struct i2c_client *c = NULL;
493 unsigned long flags;
495 union cx25840_ir_fifo_rec rx_data[FIFO_RX_DEPTH];
496 unsigned int i, j, k;
497 u32 events, v;
498 int tsr, rsr, rto, ror, tse, rse, rte, roe, kror;
499 u32 cntrl, irqen, stats;
501 *handled = false;
502 if (ir_state == NULL)
503 return -ENODEV;
505 c = ir_state->c;
507 /* Only support the IR controller for the CX2388[57] AV Core for now */
508 if (!(is_cx23885(state) || is_cx23887(state)))
509 return -ENODEV;
511 cntrl = cx25840_read4(c, CX25840_IR_CNTRL_REG);
512 irqen = cx25840_read4(c, CX25840_IR_IRQEN_REG);
513 if (is_cx23885(state) || is_cx23887(state))
514 irqen ^= IRQEN_MSK;
515 stats = cx25840_read4(c, CX25840_IR_STATS_REG);
517 tsr = stats & STATS_TSR; /* Tx FIFO Service Request */
518 rsr = stats & STATS_RSR; /* Rx FIFO Service Request */
519 rto = stats & STATS_RTO; /* Rx Pulse Width Timer Time Out */
520 ror = stats & STATS_ROR; /* Rx FIFO Over Run */
522 tse = irqen & IRQEN_TSE; /* Tx FIFO Service Request IRQ Enable */
523 rse = irqen & IRQEN_RSE; /* Rx FIFO Service Request IRQ Enable */
524 rte = irqen & IRQEN_RTE; /* Rx Pulse Width Timer Time Out IRQ Enable */
525 roe = irqen & IRQEN_ROE; /* Rx FIFO Over Run IRQ Enable */
527 v4l2_dbg(2, ir_debug, sd, "IR IRQ Status: %s %s %s %s %s %s\n",
528 tsr ? "tsr" : " ", rsr ? "rsr" : " ",
529 rto ? "rto" : " ", ror ? "ror" : " ",
530 stats & STATS_TBY ? "tby" : " ",
531 stats & STATS_RBY ? "rby" : " ");
533 v4l2_dbg(2, ir_debug, sd, "IR IRQ Enables: %s %s %s %s\n",
534 tse ? "tse" : " ", rse ? "rse" : " ",
535 rte ? "rte" : " ", roe ? "roe" : " ");
538 * Transmitter interrupt service
540 if (tse && tsr) {
542 * TODO:
543 * Check the watermark threshold setting
544 * Pull FIFO_TX_DEPTH or FIFO_TX_DEPTH/2 entries from tx_kfifo
545 * Push the data to the hardware FIFO.
546 * If there was nothing more to send in the tx_kfifo, disable
547 * the TSR IRQ and notify the v4l2_device.
548 * If there was something in the tx_kfifo, check the tx_kfifo
549 * level and notify the v4l2_device, if it is low.
551 /* For now, inhibit TSR interrupt until Tx is implemented */
552 irqenable_tx(sd, 0);
553 events = V4L2_SUBDEV_IR_TX_FIFO_SERVICE_REQ;
554 v4l2_subdev_notify(sd, V4L2_SUBDEV_IR_TX_NOTIFY, &events);
555 *handled = true;
559 * Receiver interrupt service
561 kror = 0;
562 if ((rse && rsr) || (rte && rto)) {
564 * Receive data on RSR to clear the STATS_RSR.
565 * Receive data on RTO, since we may not have yet hit the RSR
566 * watermark when we receive the RTO.
568 for (i = 0, v = FIFO_RX_NDV;
569 (v & FIFO_RX_NDV) && !kror; i = 0) {
570 for (j = 0;
571 (v & FIFO_RX_NDV) && j < FIFO_RX_DEPTH; j++) {
572 v = cx25840_read4(c, CX25840_IR_FIFO_REG);
573 rx_data[i].hw_fifo_data = v & ~FIFO_RX_NDV;
574 i++;
576 if (i == 0)
577 break;
578 j = i * sizeof(union cx25840_ir_fifo_rec);
579 k = kfifo_in_locked(&ir_state->rx_kfifo,
580 (unsigned char *) rx_data, j,
581 &ir_state->rx_kfifo_lock);
582 if (k != j)
583 kror++; /* rx_kfifo over run */
585 *handled = true;
588 events = 0;
589 v = 0;
590 if (kror) {
591 events |= V4L2_SUBDEV_IR_RX_SW_FIFO_OVERRUN;
592 v4l2_err(sd, "IR receiver software FIFO overrun\n");
594 if (roe && ror) {
596 * The RX FIFO Enable (CNTRL_RFE) must be toggled to clear
597 * the Rx FIFO Over Run status (STATS_ROR)
599 v |= CNTRL_RFE;
600 events |= V4L2_SUBDEV_IR_RX_HW_FIFO_OVERRUN;
601 v4l2_err(sd, "IR receiver hardware FIFO overrun\n");
603 if (rte && rto) {
605 * The IR Receiver Enable (CNTRL_RXE) must be toggled to clear
606 * the Rx Pulse Width Timer Time Out (STATS_RTO)
608 v |= CNTRL_RXE;
609 events |= V4L2_SUBDEV_IR_RX_END_OF_RX_DETECTED;
611 if (v) {
612 /* Clear STATS_ROR & STATS_RTO as needed by resetting hardware */
613 cx25840_write4(c, CX25840_IR_CNTRL_REG, cntrl & ~v);
614 cx25840_write4(c, CX25840_IR_CNTRL_REG, cntrl);
615 *handled = true;
617 spin_lock_irqsave(&ir_state->rx_kfifo_lock, flags);
618 if (kfifo_len(&ir_state->rx_kfifo) >= CX25840_IR_RX_KFIFO_SIZE / 2)
619 events |= V4L2_SUBDEV_IR_RX_FIFO_SERVICE_REQ;
620 spin_unlock_irqrestore(&ir_state->rx_kfifo_lock, flags);
622 if (events)
623 v4l2_subdev_notify(sd, V4L2_SUBDEV_IR_RX_NOTIFY, &events);
624 return 0;
627 /* Receiver */
628 static int cx25840_ir_rx_read(struct v4l2_subdev *sd, u8 *buf, size_t count,
629 ssize_t *num)
631 struct cx25840_ir_state *ir_state = to_ir_state(sd);
632 bool invert;
633 u16 divider;
634 unsigned int i, n;
635 union cx25840_ir_fifo_rec *p;
636 unsigned u, v, w;
638 if (ir_state == NULL)
639 return -ENODEV;
641 invert = (bool) atomic_read(&ir_state->rx_invert);
642 divider = (u16) atomic_read(&ir_state->rxclk_divider);
644 n = count / sizeof(union cx25840_ir_fifo_rec)
645 * sizeof(union cx25840_ir_fifo_rec);
646 if (n == 0) {
647 *num = 0;
648 return 0;
651 n = kfifo_out_locked(&ir_state->rx_kfifo, buf, n,
652 &ir_state->rx_kfifo_lock);
654 n /= sizeof(union cx25840_ir_fifo_rec);
655 *num = n * sizeof(union cx25840_ir_fifo_rec);
657 for (p = (union cx25840_ir_fifo_rec *) buf, i = 0; i < n; p++, i++) {
659 if ((p->hw_fifo_data & FIFO_RXTX_RTO) == FIFO_RXTX_RTO) {
660 /* Assume RTO was because of no IR light input */
661 u = 0;
662 w = 1;
663 } else {
664 u = (p->hw_fifo_data & FIFO_RXTX_LVL) ? 1 : 0;
665 if (invert)
666 u = u ? 0 : 1;
667 w = 0;
670 v = (unsigned) pulse_width_count_to_ns(
671 (u16)(p->hw_fifo_data & FIFO_RXTX), divider) / 1000;
672 if (v > IR_MAX_DURATION)
673 v = IR_MAX_DURATION;
675 p->ir_core_data = (struct ir_raw_event)
676 { .pulse = u, .duration = v, .timeout = w };
678 v4l2_dbg(2, ir_debug, sd, "rx read: %10u ns %s %s\n",
679 v, u ? "mark" : "space", w ? "(timed out)" : "");
680 if (w)
681 v4l2_dbg(2, ir_debug, sd, "rx read: end of rx\n");
683 return 0;
686 static int cx25840_ir_rx_g_parameters(struct v4l2_subdev *sd,
687 struct v4l2_subdev_ir_parameters *p)
689 struct cx25840_ir_state *ir_state = to_ir_state(sd);
691 if (ir_state == NULL)
692 return -ENODEV;
694 mutex_lock(&ir_state->rx_params_lock);
695 memcpy(p, &ir_state->rx_params,
696 sizeof(struct v4l2_subdev_ir_parameters));
697 mutex_unlock(&ir_state->rx_params_lock);
698 return 0;
701 static int cx25840_ir_rx_shutdown(struct v4l2_subdev *sd)
703 struct cx25840_ir_state *ir_state = to_ir_state(sd);
704 struct i2c_client *c;
706 if (ir_state == NULL)
707 return -ENODEV;
709 c = ir_state->c;
710 mutex_lock(&ir_state->rx_params_lock);
712 /* Disable or slow down all IR Rx circuits and counters */
713 irqenable_rx(sd, 0);
714 control_rx_enable(c, false);
715 control_rx_demodulation_enable(c, false);
716 control_rx_s_edge_detection(c, CNTRL_EDG_NONE);
717 filter_rx_s_min_width(c, 0);
718 cx25840_write4(c, CX25840_IR_RXCLK_REG, RXCLK_RCD);
720 ir_state->rx_params.shutdown = true;
722 mutex_unlock(&ir_state->rx_params_lock);
723 return 0;
726 static int cx25840_ir_rx_s_parameters(struct v4l2_subdev *sd,
727 struct v4l2_subdev_ir_parameters *p)
729 struct cx25840_ir_state *ir_state = to_ir_state(sd);
730 struct i2c_client *c;
731 struct v4l2_subdev_ir_parameters *o;
732 u16 rxclk_divider;
734 if (ir_state == NULL)
735 return -ENODEV;
737 if (p->shutdown)
738 return cx25840_ir_rx_shutdown(sd);
740 if (p->mode != V4L2_SUBDEV_IR_MODE_PULSE_WIDTH)
741 return -ENOSYS;
743 c = ir_state->c;
744 o = &ir_state->rx_params;
746 mutex_lock(&ir_state->rx_params_lock);
748 o->shutdown = p->shutdown;
750 p->mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH;
751 o->mode = p->mode;
753 p->bytes_per_data_element = sizeof(union cx25840_ir_fifo_rec);
754 o->bytes_per_data_element = p->bytes_per_data_element;
756 /* Before we tweak the hardware, we have to disable the receiver */
757 irqenable_rx(sd, 0);
758 control_rx_enable(c, false);
760 control_rx_demodulation_enable(c, p->modulation);
761 o->modulation = p->modulation;
763 if (p->modulation) {
764 p->carrier_freq = rxclk_rx_s_carrier(c, p->carrier_freq,
765 &rxclk_divider);
767 o->carrier_freq = p->carrier_freq;
769 p->duty_cycle = 50;
770 o->duty_cycle = p->duty_cycle;
772 control_rx_s_carrier_window(c, p->carrier_freq,
773 &p->carrier_range_lower,
774 &p->carrier_range_upper);
775 o->carrier_range_lower = p->carrier_range_lower;
776 o->carrier_range_upper = p->carrier_range_upper;
778 p->max_pulse_width =
779 (u32) pulse_width_count_to_ns(FIFO_RXTX, rxclk_divider);
780 } else {
781 p->max_pulse_width =
782 rxclk_rx_s_max_pulse_width(c, p->max_pulse_width,
783 &rxclk_divider);
785 o->max_pulse_width = p->max_pulse_width;
786 atomic_set(&ir_state->rxclk_divider, rxclk_divider);
788 p->noise_filter_min_width =
789 filter_rx_s_min_width(c, p->noise_filter_min_width);
790 o->noise_filter_min_width = p->noise_filter_min_width;
792 p->resolution = clock_divider_to_resolution(rxclk_divider);
793 o->resolution = p->resolution;
795 /* FIXME - make this dependent on resolution for better performance */
796 control_rx_irq_watermark(c, RX_FIFO_HALF_FULL);
798 control_rx_s_edge_detection(c, CNTRL_EDG_BOTH);
800 o->invert_level = p->invert_level;
801 atomic_set(&ir_state->rx_invert, p->invert_level);
803 o->interrupt_enable = p->interrupt_enable;
804 o->enable = p->enable;
805 if (p->enable) {
806 unsigned long flags;
808 spin_lock_irqsave(&ir_state->rx_kfifo_lock, flags);
809 kfifo_reset(&ir_state->rx_kfifo);
810 spin_unlock_irqrestore(&ir_state->rx_kfifo_lock, flags);
811 if (p->interrupt_enable)
812 irqenable_rx(sd, IRQEN_RSE | IRQEN_RTE | IRQEN_ROE);
813 control_rx_enable(c, p->enable);
816 mutex_unlock(&ir_state->rx_params_lock);
817 return 0;
820 /* Transmitter */
821 static int cx25840_ir_tx_write(struct v4l2_subdev *sd, u8 *buf, size_t count,
822 ssize_t *num)
824 struct cx25840_ir_state *ir_state = to_ir_state(sd);
826 if (ir_state == NULL)
827 return -ENODEV;
829 #if 0
831 * FIXME - the code below is an incomplete and untested sketch of what
832 * may need to be done. The critical part is to get 4 (or 8) pulses
833 * from the tx_kfifo, or converted from ns to the proper units from the
834 * input, and push them off to the hardware Tx FIFO right away, if the
835 * HW TX fifo needs service. The rest can be pushed to the tx_kfifo in
836 * a less critical timeframe. Also watch out for overruning the
837 * tx_kfifo - don't let it happen and let the caller know not all his
838 * pulses were written.
840 u32 *ns_pulse = (u32 *) buf;
841 unsigned int n;
842 u32 fifo_pulse[FIFO_TX_DEPTH];
843 u32 mark;
845 /* Compute how much we can fit in the tx kfifo */
846 n = CX25840_IR_TX_KFIFO_SIZE - kfifo_len(ir_state->tx_kfifo);
847 n = min(n, (unsigned int) count);
848 n /= sizeof(u32);
850 /* FIXME - turn on Tx Fifo service interrupt
851 * check hardware fifo level, and other stuff
853 for (i = 0; i < n; ) {
854 for (j = 0; j < FIFO_TX_DEPTH / 2 && i < n; j++) {
855 mark = ns_pulse[i] & LEVEL_MASK;
856 fifo_pulse[j] = ns_to_pulse_width_count(
857 ns_pulse[i] &
858 ~LEVEL_MASK,
859 ir_state->txclk_divider);
860 if (mark)
861 fifo_pulse[j] &= FIFO_RXTX_LVL;
862 i++;
864 kfifo_put(ir_state->tx_kfifo, (u8 *) fifo_pulse,
865 j * sizeof(u32));
867 *num = n * sizeof(u32);
868 #else
869 /* For now enable the Tx FIFO Service interrupt & pretend we did work */
870 irqenable_tx(sd, IRQEN_TSE);
871 *num = count;
872 #endif
873 return 0;
876 static int cx25840_ir_tx_g_parameters(struct v4l2_subdev *sd,
877 struct v4l2_subdev_ir_parameters *p)
879 struct cx25840_ir_state *ir_state = to_ir_state(sd);
881 if (ir_state == NULL)
882 return -ENODEV;
884 mutex_lock(&ir_state->tx_params_lock);
885 memcpy(p, &ir_state->tx_params,
886 sizeof(struct v4l2_subdev_ir_parameters));
887 mutex_unlock(&ir_state->tx_params_lock);
888 return 0;
891 static int cx25840_ir_tx_shutdown(struct v4l2_subdev *sd)
893 struct cx25840_ir_state *ir_state = to_ir_state(sd);
894 struct i2c_client *c;
896 if (ir_state == NULL)
897 return -ENODEV;
899 c = ir_state->c;
900 mutex_lock(&ir_state->tx_params_lock);
902 /* Disable or slow down all IR Tx circuits and counters */
903 irqenable_tx(sd, 0);
904 control_tx_enable(c, false);
905 control_tx_modulation_enable(c, false);
906 cx25840_write4(c, CX25840_IR_TXCLK_REG, TXCLK_TCD);
908 ir_state->tx_params.shutdown = true;
910 mutex_unlock(&ir_state->tx_params_lock);
911 return 0;
914 static int cx25840_ir_tx_s_parameters(struct v4l2_subdev *sd,
915 struct v4l2_subdev_ir_parameters *p)
917 struct cx25840_ir_state *ir_state = to_ir_state(sd);
918 struct i2c_client *c;
919 struct v4l2_subdev_ir_parameters *o;
920 u16 txclk_divider;
922 if (ir_state == NULL)
923 return -ENODEV;
925 if (p->shutdown)
926 return cx25840_ir_tx_shutdown(sd);
928 if (p->mode != V4L2_SUBDEV_IR_MODE_PULSE_WIDTH)
929 return -ENOSYS;
931 c = ir_state->c;
932 o = &ir_state->tx_params;
933 mutex_lock(&ir_state->tx_params_lock);
935 o->shutdown = p->shutdown;
937 p->mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH;
938 o->mode = p->mode;
940 p->bytes_per_data_element = sizeof(union cx25840_ir_fifo_rec);
941 o->bytes_per_data_element = p->bytes_per_data_element;
943 /* Before we tweak the hardware, we have to disable the transmitter */
944 irqenable_tx(sd, 0);
945 control_tx_enable(c, false);
947 control_tx_modulation_enable(c, p->modulation);
948 o->modulation = p->modulation;
950 if (p->modulation) {
951 p->carrier_freq = txclk_tx_s_carrier(c, p->carrier_freq,
952 &txclk_divider);
953 o->carrier_freq = p->carrier_freq;
955 p->duty_cycle = cduty_tx_s_duty_cycle(c, p->duty_cycle);
956 o->duty_cycle = p->duty_cycle;
958 p->max_pulse_width =
959 (u32) pulse_width_count_to_ns(FIFO_RXTX, txclk_divider);
960 } else {
961 p->max_pulse_width =
962 txclk_tx_s_max_pulse_width(c, p->max_pulse_width,
963 &txclk_divider);
965 o->max_pulse_width = p->max_pulse_width;
966 atomic_set(&ir_state->txclk_divider, txclk_divider);
968 p->resolution = clock_divider_to_resolution(txclk_divider);
969 o->resolution = p->resolution;
971 /* FIXME - make this dependent on resolution for better performance */
972 control_tx_irq_watermark(c, TX_FIFO_HALF_EMPTY);
974 control_tx_polarity_invert(c, p->invert_carrier_sense);
975 o->invert_carrier_sense = p->invert_carrier_sense;
978 * FIXME: we don't have hardware help for IO pin level inversion
979 * here like we have on the CX23888.
980 * Act on this with some mix of logical inversion of data levels,
981 * carrier polarity, and carrier duty cycle.
983 o->invert_level = p->invert_level;
985 o->interrupt_enable = p->interrupt_enable;
986 o->enable = p->enable;
987 if (p->enable) {
988 /* reset tx_fifo here */
989 if (p->interrupt_enable)
990 irqenable_tx(sd, IRQEN_TSE);
991 control_tx_enable(c, p->enable);
994 mutex_unlock(&ir_state->tx_params_lock);
995 return 0;
1000 * V4L2 Subdevice Core Ops support
1002 int cx25840_ir_log_status(struct v4l2_subdev *sd)
1004 struct cx25840_state *state = to_state(sd);
1005 struct i2c_client *c = state->c;
1006 char *s;
1007 int i, j;
1008 u32 cntrl, txclk, rxclk, cduty, stats, irqen, filtr;
1010 /* The CX23888 chip doesn't have an IR controller on the A/V core */
1011 if (is_cx23888(state))
1012 return 0;
1014 cntrl = cx25840_read4(c, CX25840_IR_CNTRL_REG);
1015 txclk = cx25840_read4(c, CX25840_IR_TXCLK_REG) & TXCLK_TCD;
1016 rxclk = cx25840_read4(c, CX25840_IR_RXCLK_REG) & RXCLK_RCD;
1017 cduty = cx25840_read4(c, CX25840_IR_CDUTY_REG) & CDUTY_CDC;
1018 stats = cx25840_read4(c, CX25840_IR_STATS_REG);
1019 irqen = cx25840_read4(c, CX25840_IR_IRQEN_REG);
1020 if (is_cx23885(state) || is_cx23887(state))
1021 irqen ^= IRQEN_MSK;
1022 filtr = cx25840_read4(c, CX25840_IR_FILTR_REG) & FILTR_LPF;
1024 v4l2_info(sd, "IR Receiver:\n");
1025 v4l2_info(sd, "\tEnabled: %s\n",
1026 cntrl & CNTRL_RXE ? "yes" : "no");
1027 v4l2_info(sd, "\tDemodulation from a carrier: %s\n",
1028 cntrl & CNTRL_DMD ? "enabled" : "disabled");
1029 v4l2_info(sd, "\tFIFO: %s\n",
1030 cntrl & CNTRL_RFE ? "enabled" : "disabled");
1031 switch (cntrl & CNTRL_EDG) {
1032 case CNTRL_EDG_NONE:
1033 s = "disabled";
1034 break;
1035 case CNTRL_EDG_FALL:
1036 s = "falling edge";
1037 break;
1038 case CNTRL_EDG_RISE:
1039 s = "rising edge";
1040 break;
1041 case CNTRL_EDG_BOTH:
1042 s = "rising & falling edges";
1043 break;
1044 default:
1045 s = "??? edge";
1046 break;
1048 v4l2_info(sd, "\tPulse timers' start/stop trigger: %s\n", s);
1049 v4l2_info(sd, "\tFIFO data on pulse timer overflow: %s\n",
1050 cntrl & CNTRL_R ? "not loaded" : "overflow marker");
1051 v4l2_info(sd, "\tFIFO interrupt watermark: %s\n",
1052 cntrl & CNTRL_RIC ? "not empty" : "half full or greater");
1053 v4l2_info(sd, "\tLoopback mode: %s\n",
1054 cntrl & CNTRL_LBM ? "loopback active" : "normal receive");
1055 if (cntrl & CNTRL_DMD) {
1056 v4l2_info(sd, "\tExpected carrier (16 clocks): %u Hz\n",
1057 clock_divider_to_carrier_freq(rxclk));
1058 switch (cntrl & CNTRL_WIN) {
1059 case CNTRL_WIN_3_3:
1060 i = 3;
1061 j = 3;
1062 break;
1063 case CNTRL_WIN_4_3:
1064 i = 4;
1065 j = 3;
1066 break;
1067 case CNTRL_WIN_3_4:
1068 i = 3;
1069 j = 4;
1070 break;
1071 case CNTRL_WIN_4_4:
1072 i = 4;
1073 j = 4;
1074 break;
1075 default:
1076 i = 0;
1077 j = 0;
1078 break;
1080 v4l2_info(sd, "\tNext carrier edge window: 16 clocks -%1d/+%1d, %u to %u Hz\n",
1081 i, j,
1082 clock_divider_to_freq(rxclk, 16 + j),
1083 clock_divider_to_freq(rxclk, 16 - i));
1085 v4l2_info(sd, "\tMax measurable pulse width: %u us, %llu ns\n",
1086 pulse_width_count_to_us(FIFO_RXTX, rxclk),
1087 pulse_width_count_to_ns(FIFO_RXTX, rxclk));
1088 v4l2_info(sd, "\tLow pass filter: %s\n",
1089 filtr ? "enabled" : "disabled");
1090 if (filtr)
1091 v4l2_info(sd, "\tMin acceptable pulse width (LPF): %u us, %u ns\n",
1092 lpf_count_to_us(filtr),
1093 lpf_count_to_ns(filtr));
1094 v4l2_info(sd, "\tPulse width timer timed-out: %s\n",
1095 stats & STATS_RTO ? "yes" : "no");
1096 v4l2_info(sd, "\tPulse width timer time-out intr: %s\n",
1097 irqen & IRQEN_RTE ? "enabled" : "disabled");
1098 v4l2_info(sd, "\tFIFO overrun: %s\n",
1099 stats & STATS_ROR ? "yes" : "no");
1100 v4l2_info(sd, "\tFIFO overrun interrupt: %s\n",
1101 irqen & IRQEN_ROE ? "enabled" : "disabled");
1102 v4l2_info(sd, "\tBusy: %s\n",
1103 stats & STATS_RBY ? "yes" : "no");
1104 v4l2_info(sd, "\tFIFO service requested: %s\n",
1105 stats & STATS_RSR ? "yes" : "no");
1106 v4l2_info(sd, "\tFIFO service request interrupt: %s\n",
1107 irqen & IRQEN_RSE ? "enabled" : "disabled");
1109 v4l2_info(sd, "IR Transmitter:\n");
1110 v4l2_info(sd, "\tEnabled: %s\n",
1111 cntrl & CNTRL_TXE ? "yes" : "no");
1112 v4l2_info(sd, "\tModulation onto a carrier: %s\n",
1113 cntrl & CNTRL_MOD ? "enabled" : "disabled");
1114 v4l2_info(sd, "\tFIFO: %s\n",
1115 cntrl & CNTRL_TFE ? "enabled" : "disabled");
1116 v4l2_info(sd, "\tFIFO interrupt watermark: %s\n",
1117 cntrl & CNTRL_TIC ? "not empty" : "half full or less");
1118 v4l2_info(sd, "\tCarrier polarity: %s\n",
1119 cntrl & CNTRL_CPL ? "space:burst mark:noburst"
1120 : "space:noburst mark:burst");
1121 if (cntrl & CNTRL_MOD) {
1122 v4l2_info(sd, "\tCarrier (16 clocks): %u Hz\n",
1123 clock_divider_to_carrier_freq(txclk));
1124 v4l2_info(sd, "\tCarrier duty cycle: %2u/16\n",
1125 cduty + 1);
1127 v4l2_info(sd, "\tMax pulse width: %u us, %llu ns\n",
1128 pulse_width_count_to_us(FIFO_RXTX, txclk),
1129 pulse_width_count_to_ns(FIFO_RXTX, txclk));
1130 v4l2_info(sd, "\tBusy: %s\n",
1131 stats & STATS_TBY ? "yes" : "no");
1132 v4l2_info(sd, "\tFIFO service requested: %s\n",
1133 stats & STATS_TSR ? "yes" : "no");
1134 v4l2_info(sd, "\tFIFO service request interrupt: %s\n",
1135 irqen & IRQEN_TSE ? "enabled" : "disabled");
1137 return 0;
1141 const struct v4l2_subdev_ir_ops cx25840_ir_ops = {
1142 .rx_read = cx25840_ir_rx_read,
1143 .rx_g_parameters = cx25840_ir_rx_g_parameters,
1144 .rx_s_parameters = cx25840_ir_rx_s_parameters,
1146 .tx_write = cx25840_ir_tx_write,
1147 .tx_g_parameters = cx25840_ir_tx_g_parameters,
1148 .tx_s_parameters = cx25840_ir_tx_s_parameters,
1152 static const struct v4l2_subdev_ir_parameters default_rx_params = {
1153 .bytes_per_data_element = sizeof(union cx25840_ir_fifo_rec),
1154 .mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH,
1156 .enable = false,
1157 .interrupt_enable = false,
1158 .shutdown = true,
1160 .modulation = true,
1161 .carrier_freq = 36000, /* 36 kHz - RC-5, and RC-6 carrier */
1163 /* RC-5: 666,667 ns = 1/36 kHz * 32 cycles * 1 mark * 0.75 */
1164 /* RC-6: 333,333 ns = 1/36 kHz * 16 cycles * 1 mark * 0.75 */
1165 .noise_filter_min_width = 333333, /* ns */
1166 .carrier_range_lower = 35000,
1167 .carrier_range_upper = 37000,
1168 .invert_level = false,
1171 static const struct v4l2_subdev_ir_parameters default_tx_params = {
1172 .bytes_per_data_element = sizeof(union cx25840_ir_fifo_rec),
1173 .mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH,
1175 .enable = false,
1176 .interrupt_enable = false,
1177 .shutdown = true,
1179 .modulation = true,
1180 .carrier_freq = 36000, /* 36 kHz - RC-5 carrier */
1181 .duty_cycle = 25, /* 25 % - RC-5 carrier */
1182 .invert_level = false,
1183 .invert_carrier_sense = false,
1186 int cx25840_ir_probe(struct v4l2_subdev *sd)
1188 struct cx25840_state *state = to_state(sd);
1189 struct cx25840_ir_state *ir_state;
1190 struct v4l2_subdev_ir_parameters default_params;
1192 /* Only init the IR controller for the CX2388[57] AV Core for now */
1193 if (!(is_cx23885(state) || is_cx23887(state)))
1194 return 0;
1196 ir_state = devm_kzalloc(&state->c->dev, sizeof(*ir_state), GFP_KERNEL);
1197 if (ir_state == NULL)
1198 return -ENOMEM;
1200 spin_lock_init(&ir_state->rx_kfifo_lock);
1201 if (kfifo_alloc(&ir_state->rx_kfifo,
1202 CX25840_IR_RX_KFIFO_SIZE, GFP_KERNEL))
1203 return -ENOMEM;
1205 ir_state->c = state->c;
1206 state->ir_state = ir_state;
1208 /* Ensure no interrupts arrive yet */
1209 if (is_cx23885(state) || is_cx23887(state))
1210 cx25840_write4(ir_state->c, CX25840_IR_IRQEN_REG, IRQEN_MSK);
1211 else
1212 cx25840_write4(ir_state->c, CX25840_IR_IRQEN_REG, 0);
1214 mutex_init(&ir_state->rx_params_lock);
1215 default_params = default_rx_params;
1216 v4l2_subdev_call(sd, ir, rx_s_parameters, &default_params);
1218 mutex_init(&ir_state->tx_params_lock);
1219 default_params = default_tx_params;
1220 v4l2_subdev_call(sd, ir, tx_s_parameters, &default_params);
1222 return 0;
1225 int cx25840_ir_remove(struct v4l2_subdev *sd)
1227 struct cx25840_state *state = to_state(sd);
1228 struct cx25840_ir_state *ir_state = to_ir_state(sd);
1230 if (ir_state == NULL)
1231 return -ENODEV;
1233 cx25840_ir_rx_shutdown(sd);
1234 cx25840_ir_tx_shutdown(sd);
1236 kfifo_free(&ir_state->rx_kfifo);
1237 state->ir_state = NULL;
1238 return 0;