]> Pileus Git - ~andy/linux/blob - drivers/media/video/cx25840/cx25840-ir.c
Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wirel...
[~andy/linux] / drivers / media / video / cx25840 / cx25840-ir.c
1 /*
2  *  Driver for the Conexant CX2584x Audio/Video decoder chip and related cores
3  *
4  *  Integrated Consumer Infrared Controller
5  *
6  *  Copyright (C) 2010  Andy Walls <awalls@md.metrocast.net>
7  *
8  *  This program is free software; you can redistribute it and/or
9  *  modify it under the terms of the GNU General Public License
10  *  as published by the Free Software Foundation; either version 2
11  *  of the License, or (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  *  02110-1301, USA.
22  */
23
24 #include <linux/slab.h>
25 #include <linux/kfifo.h>
26 #include <media/cx25840.h>
27 #include <media/rc-core.h>
28
29 #include "cx25840-core.h"
30
31 static unsigned int ir_debug;
32 module_param(ir_debug, int, 0644);
33 MODULE_PARM_DESC(ir_debug, "enable integrated IR debug messages");
34
35 #define CX25840_IR_REG_BASE     0x200
36
37 #define CX25840_IR_CNTRL_REG    0x200
38 #define CNTRL_WIN_3_3   0x00000000
39 #define CNTRL_WIN_4_3   0x00000001
40 #define CNTRL_WIN_3_4   0x00000002
41 #define CNTRL_WIN_4_4   0x00000003
42 #define CNTRL_WIN       0x00000003
43 #define CNTRL_EDG_NONE  0x00000000
44 #define CNTRL_EDG_FALL  0x00000004
45 #define CNTRL_EDG_RISE  0x00000008
46 #define CNTRL_EDG_BOTH  0x0000000C
47 #define CNTRL_EDG       0x0000000C
48 #define CNTRL_DMD       0x00000010
49 #define CNTRL_MOD       0x00000020
50 #define CNTRL_RFE       0x00000040
51 #define CNTRL_TFE       0x00000080
52 #define CNTRL_RXE       0x00000100
53 #define CNTRL_TXE       0x00000200
54 #define CNTRL_RIC       0x00000400
55 #define CNTRL_TIC       0x00000800
56 #define CNTRL_CPL       0x00001000
57 #define CNTRL_LBM       0x00002000
58 #define CNTRL_R         0x00004000
59
60 #define CX25840_IR_TXCLK_REG    0x204
61 #define TXCLK_TCD       0x0000FFFF
62
63 #define CX25840_IR_RXCLK_REG    0x208
64 #define RXCLK_RCD       0x0000FFFF
65
66 #define CX25840_IR_CDUTY_REG    0x20C
67 #define CDUTY_CDC       0x0000000F
68
69 #define CX25840_IR_STATS_REG    0x210
70 #define STATS_RTO       0x00000001
71 #define STATS_ROR       0x00000002
72 #define STATS_RBY       0x00000004
73 #define STATS_TBY       0x00000008
74 #define STATS_RSR       0x00000010
75 #define STATS_TSR       0x00000020
76
77 #define CX25840_IR_IRQEN_REG    0x214
78 #define IRQEN_RTE       0x00000001
79 #define IRQEN_ROE       0x00000002
80 #define IRQEN_RSE       0x00000010
81 #define IRQEN_TSE       0x00000020
82 #define IRQEN_MSK       0x00000033
83
84 #define CX25840_IR_FILTR_REG    0x218
85 #define FILTR_LPF       0x0000FFFF
86
87 #define CX25840_IR_FIFO_REG     0x23C
88 #define FIFO_RXTX       0x0000FFFF
89 #define FIFO_RXTX_LVL   0x00010000
90 #define FIFO_RXTX_RTO   0x0001FFFF
91 #define FIFO_RX_NDV     0x00020000
92 #define FIFO_RX_DEPTH   8
93 #define FIFO_TX_DEPTH   8
94
95 #define CX25840_VIDCLK_FREQ     108000000 /* 108 MHz, BT.656 */
96 #define CX25840_IR_REFCLK_FREQ  (CX25840_VIDCLK_FREQ / 2)
97
98 /*
99  * We use this union internally for convenience, but callers to tx_write
100  * and rx_read will be expecting records of type struct ir_raw_event.
101  * Always ensure the size of this union is dictated by struct ir_raw_event.
102  */
103 union cx25840_ir_fifo_rec {
104         u32 hw_fifo_data;
105         struct ir_raw_event ir_core_data;
106 };
107
108 #define CX25840_IR_RX_KFIFO_SIZE    (256 * sizeof(union cx25840_ir_fifo_rec))
109 #define CX25840_IR_TX_KFIFO_SIZE    (256 * sizeof(union cx25840_ir_fifo_rec))
110
111 struct cx25840_ir_state {
112         struct i2c_client *c;
113
114         struct v4l2_subdev_ir_parameters rx_params;
115         struct mutex rx_params_lock; /* protects Rx parameter settings cache */
116         atomic_t rxclk_divider;
117         atomic_t rx_invert;
118
119         struct kfifo rx_kfifo;
120         spinlock_t rx_kfifo_lock; /* protect Rx data kfifo */
121
122         struct v4l2_subdev_ir_parameters tx_params;
123         struct mutex tx_params_lock; /* protects Tx parameter settings cache */
124         atomic_t txclk_divider;
125 };
126
127 static inline struct cx25840_ir_state *to_ir_state(struct v4l2_subdev *sd)
128 {
129         struct cx25840_state *state = to_state(sd);
130         return state ? state->ir_state : NULL;
131 }
132
133
134 /*
135  * Rx and Tx Clock Divider register computations
136  *
137  * Note the largest clock divider value of 0xffff corresponds to:
138  *      (0xffff + 1) * 1000 / 108/2 MHz = 1,213,629.629... ns
139  * which fits in 21 bits, so we'll use unsigned int for time arguments.
140  */
141 static inline u16 count_to_clock_divider(unsigned int d)
142 {
143         if (d > RXCLK_RCD + 1)
144                 d = RXCLK_RCD;
145         else if (d < 2)
146                 d = 1;
147         else
148                 d--;
149         return (u16) d;
150 }
151
152 static inline u16 ns_to_clock_divider(unsigned int ns)
153 {
154         return count_to_clock_divider(
155                 DIV_ROUND_CLOSEST(CX25840_IR_REFCLK_FREQ / 1000000 * ns, 1000));
156 }
157
158 static inline unsigned int clock_divider_to_ns(unsigned int divider)
159 {
160         /* Period of the Rx or Tx clock in ns */
161         return DIV_ROUND_CLOSEST((divider + 1) * 1000,
162                                  CX25840_IR_REFCLK_FREQ / 1000000);
163 }
164
165 static inline u16 carrier_freq_to_clock_divider(unsigned int freq)
166 {
167         return count_to_clock_divider(
168                           DIV_ROUND_CLOSEST(CX25840_IR_REFCLK_FREQ, freq * 16));
169 }
170
171 static inline unsigned int clock_divider_to_carrier_freq(unsigned int divider)
172 {
173         return DIV_ROUND_CLOSEST(CX25840_IR_REFCLK_FREQ, (divider + 1) * 16);
174 }
175
176 static inline u16 freq_to_clock_divider(unsigned int freq,
177                                         unsigned int rollovers)
178 {
179         return count_to_clock_divider(
180                    DIV_ROUND_CLOSEST(CX25840_IR_REFCLK_FREQ, freq * rollovers));
181 }
182
183 static inline unsigned int clock_divider_to_freq(unsigned int divider,
184                                                  unsigned int rollovers)
185 {
186         return DIV_ROUND_CLOSEST(CX25840_IR_REFCLK_FREQ,
187                                  (divider + 1) * rollovers);
188 }
189
190 /*
191  * Low Pass Filter register calculations
192  *
193  * Note the largest count value of 0xffff corresponds to:
194  *      0xffff * 1000 / 108/2 MHz = 1,213,611.11... ns
195  * which fits in 21 bits, so we'll use unsigned int for time arguments.
196  */
197 static inline u16 count_to_lpf_count(unsigned int d)
198 {
199         if (d > FILTR_LPF)
200                 d = FILTR_LPF;
201         else if (d < 4)
202                 d = 0;
203         return (u16) d;
204 }
205
206 static inline u16 ns_to_lpf_count(unsigned int ns)
207 {
208         return count_to_lpf_count(
209                 DIV_ROUND_CLOSEST(CX25840_IR_REFCLK_FREQ / 1000000 * ns, 1000));
210 }
211
212 static inline unsigned int lpf_count_to_ns(unsigned int count)
213 {
214         /* Duration of the Low Pass Filter rejection window in ns */
215         return DIV_ROUND_CLOSEST(count * 1000,
216                                  CX25840_IR_REFCLK_FREQ / 1000000);
217 }
218
219 static inline unsigned int lpf_count_to_us(unsigned int count)
220 {
221         /* Duration of the Low Pass Filter rejection window in us */
222         return DIV_ROUND_CLOSEST(count, CX25840_IR_REFCLK_FREQ / 1000000);
223 }
224
225 /*
226  * FIFO register pulse width count compuations
227  */
228 static u32 clock_divider_to_resolution(u16 divider)
229 {
230         /*
231          * Resolution is the duration of 1 tick of the readable portion of
232          * of the pulse width counter as read from the FIFO.  The two lsb's are
233          * not readable, hence the << 2.  This function returns ns.
234          */
235         return DIV_ROUND_CLOSEST((1 << 2)  * ((u32) divider + 1) * 1000,
236                                  CX25840_IR_REFCLK_FREQ / 1000000);
237 }
238
239 static u64 pulse_width_count_to_ns(u16 count, u16 divider)
240 {
241         u64 n;
242         u32 rem;
243
244         /*
245          * The 2 lsb's of the pulse width timer count are not readable, hence
246          * the (count << 2) | 0x3
247          */
248         n = (((u64) count << 2) | 0x3) * (divider + 1) * 1000; /* millicycles */
249         rem = do_div(n, CX25840_IR_REFCLK_FREQ / 1000000);     /* / MHz => ns */
250         if (rem >= CX25840_IR_REFCLK_FREQ / 1000000 / 2)
251                 n++;
252         return n;
253 }
254
255 #if 0
256 /* Keep as we will need this for Transmit functionality */
257 static u16 ns_to_pulse_width_count(u32 ns, u16 divider)
258 {
259         u64 n;
260         u32 d;
261         u32 rem;
262
263         /*
264          * The 2 lsb's of the pulse width timer count are not accessible, hence
265          * the (1 << 2)
266          */
267         n = ((u64) ns) * CX25840_IR_REFCLK_FREQ / 1000000; /* millicycles */
268         d = (1 << 2) * ((u32) divider + 1) * 1000; /* millicycles/count */
269         rem = do_div(n, d);
270         if (rem >= d / 2)
271                 n++;
272
273         if (n > FIFO_RXTX)
274                 n = FIFO_RXTX;
275         else if (n == 0)
276                 n = 1;
277         return (u16) n;
278 }
279
280 #endif
281 static unsigned int pulse_width_count_to_us(u16 count, u16 divider)
282 {
283         u64 n;
284         u32 rem;
285
286         /*
287          * The 2 lsb's of the pulse width timer count are not readable, hence
288          * the (count << 2) | 0x3
289          */
290         n = (((u64) count << 2) | 0x3) * (divider + 1);    /* cycles      */
291         rem = do_div(n, CX25840_IR_REFCLK_FREQ / 1000000); /* / MHz => us */
292         if (rem >= CX25840_IR_REFCLK_FREQ / 1000000 / 2)
293                 n++;
294         return (unsigned int) n;
295 }
296
297 /*
298  * Pulse Clocks computations: Combined Pulse Width Count & Rx Clock Counts
299  *
300  * The total pulse clock count is an 18 bit pulse width timer count as the most
301  * significant part and (up to) 16 bit clock divider count as a modulus.
302  * When the Rx clock divider ticks down to 0, it increments the 18 bit pulse
303  * width timer count's least significant bit.
304  */
305 static u64 ns_to_pulse_clocks(u32 ns)
306 {
307         u64 clocks;
308         u32 rem;
309         clocks = CX25840_IR_REFCLK_FREQ / 1000000 * (u64) ns; /* millicycles  */
310         rem = do_div(clocks, 1000);                         /* /1000 = cycles */
311         if (rem >= 1000 / 2)
312                 clocks++;
313         return clocks;
314 }
315
316 static u16 pulse_clocks_to_clock_divider(u64 count)
317 {
318         u32 rem;
319
320         rem = do_div(count, (FIFO_RXTX << 2) | 0x3);
321
322         /* net result needs to be rounded down and decremented by 1 */
323         if (count > RXCLK_RCD + 1)
324                 count = RXCLK_RCD;
325         else if (count < 2)
326                 count = 1;
327         else
328                 count--;
329         return (u16) count;
330 }
331
332 /*
333  * IR Control Register helpers
334  */
335 enum tx_fifo_watermark {
336         TX_FIFO_HALF_EMPTY = 0,
337         TX_FIFO_EMPTY      = CNTRL_TIC,
338 };
339
340 enum rx_fifo_watermark {
341         RX_FIFO_HALF_FULL = 0,
342         RX_FIFO_NOT_EMPTY = CNTRL_RIC,
343 };
344
345 static inline void control_tx_irq_watermark(struct i2c_client *c,
346                                             enum tx_fifo_watermark level)
347 {
348         cx25840_and_or4(c, CX25840_IR_CNTRL_REG, ~CNTRL_TIC, level);
349 }
350
351 static inline void control_rx_irq_watermark(struct i2c_client *c,
352                                             enum rx_fifo_watermark level)
353 {
354         cx25840_and_or4(c, CX25840_IR_CNTRL_REG, ~CNTRL_RIC, level);
355 }
356
357 static inline void control_tx_enable(struct i2c_client *c, bool enable)
358 {
359         cx25840_and_or4(c, CX25840_IR_CNTRL_REG, ~(CNTRL_TXE | CNTRL_TFE),
360                         enable ? (CNTRL_TXE | CNTRL_TFE) : 0);
361 }
362
363 static inline void control_rx_enable(struct i2c_client *c, bool enable)
364 {
365         cx25840_and_or4(c, CX25840_IR_CNTRL_REG, ~(CNTRL_RXE | CNTRL_RFE),
366                         enable ? (CNTRL_RXE | CNTRL_RFE) : 0);
367 }
368
369 static inline void control_tx_modulation_enable(struct i2c_client *c,
370                                                 bool enable)
371 {
372         cx25840_and_or4(c, CX25840_IR_CNTRL_REG, ~CNTRL_MOD,
373                         enable ? CNTRL_MOD : 0);
374 }
375
376 static inline void control_rx_demodulation_enable(struct i2c_client *c,
377                                                   bool enable)
378 {
379         cx25840_and_or4(c, CX25840_IR_CNTRL_REG, ~CNTRL_DMD,
380                         enable ? CNTRL_DMD : 0);
381 }
382
383 static inline void control_rx_s_edge_detection(struct i2c_client *c,
384                                                u32 edge_types)
385 {
386         cx25840_and_or4(c, CX25840_IR_CNTRL_REG, ~CNTRL_EDG_BOTH,
387                         edge_types & CNTRL_EDG_BOTH);
388 }
389
390 static void control_rx_s_carrier_window(struct i2c_client *c,
391                                         unsigned int carrier,
392                                         unsigned int *carrier_range_low,
393                                         unsigned int *carrier_range_high)
394 {
395         u32 v;
396         unsigned int c16 = carrier * 16;
397
398         if (*carrier_range_low < DIV_ROUND_CLOSEST(c16, 16 + 3)) {
399                 v = CNTRL_WIN_3_4;
400                 *carrier_range_low = DIV_ROUND_CLOSEST(c16, 16 + 4);
401         } else {
402                 v = CNTRL_WIN_3_3;
403                 *carrier_range_low = DIV_ROUND_CLOSEST(c16, 16 + 3);
404         }
405
406         if (*carrier_range_high > DIV_ROUND_CLOSEST(c16, 16 - 3)) {
407                 v |= CNTRL_WIN_4_3;
408                 *carrier_range_high = DIV_ROUND_CLOSEST(c16, 16 - 4);
409         } else {
410                 v |= CNTRL_WIN_3_3;
411                 *carrier_range_high = DIV_ROUND_CLOSEST(c16, 16 - 3);
412         }
413         cx25840_and_or4(c, CX25840_IR_CNTRL_REG, ~CNTRL_WIN, v);
414 }
415
416 static inline void control_tx_polarity_invert(struct i2c_client *c,
417                                               bool invert)
418 {
419         cx25840_and_or4(c, CX25840_IR_CNTRL_REG, ~CNTRL_CPL,
420                         invert ? CNTRL_CPL : 0);
421 }
422
423 /*
424  * IR Rx & Tx Clock Register helpers
425  */
426 static unsigned int txclk_tx_s_carrier(struct i2c_client *c,
427                                        unsigned int freq,
428                                        u16 *divider)
429 {
430         *divider = carrier_freq_to_clock_divider(freq);
431         cx25840_write4(c, CX25840_IR_TXCLK_REG, *divider);
432         return clock_divider_to_carrier_freq(*divider);
433 }
434
435 static unsigned int rxclk_rx_s_carrier(struct i2c_client *c,
436                                        unsigned int freq,
437                                        u16 *divider)
438 {
439         *divider = carrier_freq_to_clock_divider(freq);
440         cx25840_write4(c, CX25840_IR_RXCLK_REG, *divider);
441         return clock_divider_to_carrier_freq(*divider);
442 }
443
444 static u32 txclk_tx_s_max_pulse_width(struct i2c_client *c, u32 ns,
445                                       u16 *divider)
446 {
447         u64 pulse_clocks;
448
449         if (ns > IR_MAX_DURATION)
450                 ns = IR_MAX_DURATION;
451         pulse_clocks = ns_to_pulse_clocks(ns);
452         *divider = pulse_clocks_to_clock_divider(pulse_clocks);
453         cx25840_write4(c, CX25840_IR_TXCLK_REG, *divider);
454         return (u32) pulse_width_count_to_ns(FIFO_RXTX, *divider);
455 }
456
457 static u32 rxclk_rx_s_max_pulse_width(struct i2c_client *c, u32 ns,
458                                       u16 *divider)
459 {
460         u64 pulse_clocks;
461
462         if (ns > IR_MAX_DURATION)
463                 ns = IR_MAX_DURATION;
464         pulse_clocks = ns_to_pulse_clocks(ns);
465         *divider = pulse_clocks_to_clock_divider(pulse_clocks);
466         cx25840_write4(c, CX25840_IR_RXCLK_REG, *divider);
467         return (u32) pulse_width_count_to_ns(FIFO_RXTX, *divider);
468 }
469
470 /*
471  * IR Tx Carrier Duty Cycle register helpers
472  */
473 static unsigned int cduty_tx_s_duty_cycle(struct i2c_client *c,
474                                           unsigned int duty_cycle)
475 {
476         u32 n;
477         n = DIV_ROUND_CLOSEST(duty_cycle * 100, 625); /* 16ths of 100% */
478         if (n != 0)
479                 n--;
480         if (n > 15)
481                 n = 15;
482         cx25840_write4(c, CX25840_IR_CDUTY_REG, n);
483         return DIV_ROUND_CLOSEST((n + 1) * 100, 16);
484 }
485
486 /*
487  * IR Filter Register helpers
488  */
489 static u32 filter_rx_s_min_width(struct i2c_client *c, u32 min_width_ns)
490 {
491         u32 count = ns_to_lpf_count(min_width_ns);
492         cx25840_write4(c, CX25840_IR_FILTR_REG, count);
493         return lpf_count_to_ns(count);
494 }
495
496 /*
497  * IR IRQ Enable Register helpers
498  */
499 static inline void irqenable_rx(struct v4l2_subdev *sd, u32 mask)
500 {
501         struct cx25840_state *state = to_state(sd);
502
503         if (is_cx23885(state) || is_cx23887(state))
504                 mask ^= IRQEN_MSK;
505         mask &= (IRQEN_RTE | IRQEN_ROE | IRQEN_RSE);
506         cx25840_and_or4(state->c, CX25840_IR_IRQEN_REG,
507                         ~(IRQEN_RTE | IRQEN_ROE | IRQEN_RSE), mask);
508 }
509
510 static inline void irqenable_tx(struct v4l2_subdev *sd, u32 mask)
511 {
512         struct cx25840_state *state = to_state(sd);
513
514         if (is_cx23885(state) || is_cx23887(state))
515                 mask ^= IRQEN_MSK;
516         mask &= IRQEN_TSE;
517         cx25840_and_or4(state->c, CX25840_IR_IRQEN_REG, ~IRQEN_TSE, mask);
518 }
519
520 /*
521  * V4L2 Subdevice IR Ops
522  */
523 int cx25840_ir_irq_handler(struct v4l2_subdev *sd, u32 status, bool *handled)
524 {
525         struct cx25840_state *state = to_state(sd);
526         struct cx25840_ir_state *ir_state = to_ir_state(sd);
527         struct i2c_client *c = NULL;
528         unsigned long flags;
529
530         union cx25840_ir_fifo_rec rx_data[FIFO_RX_DEPTH];
531         unsigned int i, j, k;
532         u32 events, v;
533         int tsr, rsr, rto, ror, tse, rse, rte, roe, kror;
534         u32 cntrl, irqen, stats;
535
536         *handled = false;
537         if (ir_state == NULL)
538                 return -ENODEV;
539
540         c = ir_state->c;
541
542         /* Only support the IR controller for the CX2388[57] AV Core for now */
543         if (!(is_cx23885(state) || is_cx23887(state)))
544                 return -ENODEV;
545
546         cntrl = cx25840_read4(c, CX25840_IR_CNTRL_REG);
547         irqen = cx25840_read4(c, CX25840_IR_IRQEN_REG);
548         if (is_cx23885(state) || is_cx23887(state))
549                 irqen ^= IRQEN_MSK;
550         stats = cx25840_read4(c, CX25840_IR_STATS_REG);
551
552         tsr = stats & STATS_TSR; /* Tx FIFO Service Request */
553         rsr = stats & STATS_RSR; /* Rx FIFO Service Request */
554         rto = stats & STATS_RTO; /* Rx Pulse Width Timer Time Out */
555         ror = stats & STATS_ROR; /* Rx FIFO Over Run */
556
557         tse = irqen & IRQEN_TSE; /* Tx FIFO Service Request IRQ Enable */
558         rse = irqen & IRQEN_RSE; /* Rx FIFO Service Reuqest IRQ Enable */
559         rte = irqen & IRQEN_RTE; /* Rx Pulse Width Timer Time Out IRQ Enable */
560         roe = irqen & IRQEN_ROE; /* Rx FIFO Over Run IRQ Enable */
561
562         v4l2_dbg(2, ir_debug, sd, "IR IRQ Status:  %s %s %s %s %s %s\n",
563                  tsr ? "tsr" : "   ", rsr ? "rsr" : "   ",
564                  rto ? "rto" : "   ", ror ? "ror" : "   ",
565                  stats & STATS_TBY ? "tby" : "   ",
566                  stats & STATS_RBY ? "rby" : "   ");
567
568         v4l2_dbg(2, ir_debug, sd, "IR IRQ Enables: %s %s %s %s\n",
569                  tse ? "tse" : "   ", rse ? "rse" : "   ",
570                  rte ? "rte" : "   ", roe ? "roe" : "   ");
571
572         /*
573          * Transmitter interrupt service
574          */
575         if (tse && tsr) {
576                 /*
577                  * TODO:
578                  * Check the watermark threshold setting
579                  * Pull FIFO_TX_DEPTH or FIFO_TX_DEPTH/2 entries from tx_kfifo
580                  * Push the data to the hardware FIFO.
581                  * If there was nothing more to send in the tx_kfifo, disable
582                  *      the TSR IRQ and notify the v4l2_device.
583                  * If there was something in the tx_kfifo, check the tx_kfifo
584                  *      level and notify the v4l2_device, if it is low.
585                  */
586                 /* For now, inhibit TSR interrupt until Tx is implemented */
587                 irqenable_tx(sd, 0);
588                 events = V4L2_SUBDEV_IR_TX_FIFO_SERVICE_REQ;
589                 v4l2_subdev_notify(sd, V4L2_SUBDEV_IR_TX_NOTIFY, &events);
590                 *handled = true;
591         }
592
593         /*
594          * Receiver interrupt service
595          */
596         kror = 0;
597         if ((rse && rsr) || (rte && rto)) {
598                 /*
599                  * Receive data on RSR to clear the STATS_RSR.
600                  * Receive data on RTO, since we may not have yet hit the RSR
601                  * watermark when we receive the RTO.
602                  */
603                 for (i = 0, v = FIFO_RX_NDV;
604                      (v & FIFO_RX_NDV) && !kror; i = 0) {
605                         for (j = 0;
606                              (v & FIFO_RX_NDV) && j < FIFO_RX_DEPTH; j++) {
607                                 v = cx25840_read4(c, CX25840_IR_FIFO_REG);
608                                 rx_data[i].hw_fifo_data = v & ~FIFO_RX_NDV;
609                                 i++;
610                         }
611                         if (i == 0)
612                                 break;
613                         j = i * sizeof(union cx25840_ir_fifo_rec);
614                         k = kfifo_in_locked(&ir_state->rx_kfifo,
615                                             (unsigned char *) rx_data, j,
616                                             &ir_state->rx_kfifo_lock);
617                         if (k != j)
618                                 kror++; /* rx_kfifo over run */
619                 }
620                 *handled = true;
621         }
622
623         events = 0;
624         v = 0;
625         if (kror) {
626                 events |= V4L2_SUBDEV_IR_RX_SW_FIFO_OVERRUN;
627                 v4l2_err(sd, "IR receiver software FIFO overrun\n");
628         }
629         if (roe && ror) {
630                 /*
631                  * The RX FIFO Enable (CNTRL_RFE) must be toggled to clear
632                  * the Rx FIFO Over Run status (STATS_ROR)
633                  */
634                 v |= CNTRL_RFE;
635                 events |= V4L2_SUBDEV_IR_RX_HW_FIFO_OVERRUN;
636                 v4l2_err(sd, "IR receiver hardware FIFO overrun\n");
637         }
638         if (rte && rto) {
639                 /*
640                  * The IR Receiver Enable (CNTRL_RXE) must be toggled to clear
641                  * the Rx Pulse Width Timer Time Out (STATS_RTO)
642                  */
643                 v |= CNTRL_RXE;
644                 events |= V4L2_SUBDEV_IR_RX_END_OF_RX_DETECTED;
645         }
646         if (v) {
647                 /* Clear STATS_ROR & STATS_RTO as needed by reseting hardware */
648                 cx25840_write4(c, CX25840_IR_CNTRL_REG, cntrl & ~v);
649                 cx25840_write4(c, CX25840_IR_CNTRL_REG, cntrl);
650                 *handled = true;
651         }
652         spin_lock_irqsave(&ir_state->rx_kfifo_lock, flags);
653         if (kfifo_len(&ir_state->rx_kfifo) >= CX25840_IR_RX_KFIFO_SIZE / 2)
654                 events |= V4L2_SUBDEV_IR_RX_FIFO_SERVICE_REQ;
655         spin_unlock_irqrestore(&ir_state->rx_kfifo_lock, flags);
656
657         if (events)
658                 v4l2_subdev_notify(sd, V4L2_SUBDEV_IR_RX_NOTIFY, &events);
659         return 0;
660 }
661
662 /* Receiver */
663 static int cx25840_ir_rx_read(struct v4l2_subdev *sd, u8 *buf, size_t count,
664                               ssize_t *num)
665 {
666         struct cx25840_ir_state *ir_state = to_ir_state(sd);
667         bool invert;
668         u16 divider;
669         unsigned int i, n;
670         union cx25840_ir_fifo_rec *p;
671         unsigned u, v, w;
672
673         if (ir_state == NULL)
674                 return -ENODEV;
675
676         invert = (bool) atomic_read(&ir_state->rx_invert);
677         divider = (u16) atomic_read(&ir_state->rxclk_divider);
678
679         n = count / sizeof(union cx25840_ir_fifo_rec)
680                 * sizeof(union cx25840_ir_fifo_rec);
681         if (n == 0) {
682                 *num = 0;
683                 return 0;
684         }
685
686         n = kfifo_out_locked(&ir_state->rx_kfifo, buf, n,
687                              &ir_state->rx_kfifo_lock);
688
689         n /= sizeof(union cx25840_ir_fifo_rec);
690         *num = n * sizeof(union cx25840_ir_fifo_rec);
691
692         for (p = (union cx25840_ir_fifo_rec *) buf, i = 0; i < n; p++, i++) {
693
694                 if ((p->hw_fifo_data & FIFO_RXTX_RTO) == FIFO_RXTX_RTO) {
695                         /* Assume RTO was because of no IR light input */
696                         u = 0;
697                         w = 1;
698                 } else {
699                         u = (p->hw_fifo_data & FIFO_RXTX_LVL) ? 1 : 0;
700                         if (invert)
701                                 u = u ? 0 : 1;
702                         w = 0;
703                 }
704
705                 v = (unsigned) pulse_width_count_to_ns(
706                                   (u16) (p->hw_fifo_data & FIFO_RXTX), divider);
707                 if (v > IR_MAX_DURATION)
708                         v = IR_MAX_DURATION;
709
710                 init_ir_raw_event(&p->ir_core_data);
711                 p->ir_core_data.pulse = u;
712                 p->ir_core_data.duration = v;
713                 p->ir_core_data.timeout = w;
714
715                 v4l2_dbg(2, ir_debug, sd, "rx read: %10u ns  %s  %s\n",
716                          v, u ? "mark" : "space", w ? "(timed out)" : "");
717                 if (w)
718                         v4l2_dbg(2, ir_debug, sd, "rx read: end of rx\n");
719         }
720         return 0;
721 }
722
723 static int cx25840_ir_rx_g_parameters(struct v4l2_subdev *sd,
724                                       struct v4l2_subdev_ir_parameters *p)
725 {
726         struct cx25840_ir_state *ir_state = to_ir_state(sd);
727
728         if (ir_state == NULL)
729                 return -ENODEV;
730
731         mutex_lock(&ir_state->rx_params_lock);
732         memcpy(p, &ir_state->rx_params,
733                                       sizeof(struct v4l2_subdev_ir_parameters));
734         mutex_unlock(&ir_state->rx_params_lock);
735         return 0;
736 }
737
738 static int cx25840_ir_rx_shutdown(struct v4l2_subdev *sd)
739 {
740         struct cx25840_ir_state *ir_state = to_ir_state(sd);
741         struct i2c_client *c;
742
743         if (ir_state == NULL)
744                 return -ENODEV;
745
746         c = ir_state->c;
747         mutex_lock(&ir_state->rx_params_lock);
748
749         /* Disable or slow down all IR Rx circuits and counters */
750         irqenable_rx(sd, 0);
751         control_rx_enable(c, false);
752         control_rx_demodulation_enable(c, false);
753         control_rx_s_edge_detection(c, CNTRL_EDG_NONE);
754         filter_rx_s_min_width(c, 0);
755         cx25840_write4(c, CX25840_IR_RXCLK_REG, RXCLK_RCD);
756
757         ir_state->rx_params.shutdown = true;
758
759         mutex_unlock(&ir_state->rx_params_lock);
760         return 0;
761 }
762
763 static int cx25840_ir_rx_s_parameters(struct v4l2_subdev *sd,
764                                       struct v4l2_subdev_ir_parameters *p)
765 {
766         struct cx25840_ir_state *ir_state = to_ir_state(sd);
767         struct i2c_client *c;
768         struct v4l2_subdev_ir_parameters *o;
769         u16 rxclk_divider;
770
771         if (ir_state == NULL)
772                 return -ENODEV;
773
774         if (p->shutdown)
775                 return cx25840_ir_rx_shutdown(sd);
776
777         if (p->mode != V4L2_SUBDEV_IR_MODE_PULSE_WIDTH)
778                 return -ENOSYS;
779
780         c = ir_state->c;
781         o = &ir_state->rx_params;
782
783         mutex_lock(&ir_state->rx_params_lock);
784
785         o->shutdown = p->shutdown;
786
787         p->mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH;
788         o->mode = p->mode;
789
790         p->bytes_per_data_element = sizeof(union cx25840_ir_fifo_rec);
791         o->bytes_per_data_element = p->bytes_per_data_element;
792
793         /* Before we tweak the hardware, we have to disable the receiver */
794         irqenable_rx(sd, 0);
795         control_rx_enable(c, false);
796
797         control_rx_demodulation_enable(c, p->modulation);
798         o->modulation = p->modulation;
799
800         if (p->modulation) {
801                 p->carrier_freq = rxclk_rx_s_carrier(c, p->carrier_freq,
802                                                      &rxclk_divider);
803
804                 o->carrier_freq = p->carrier_freq;
805
806                 p->duty_cycle = 50;
807                 o->duty_cycle = p->duty_cycle;
808
809                 control_rx_s_carrier_window(c, p->carrier_freq,
810                                             &p->carrier_range_lower,
811                                             &p->carrier_range_upper);
812                 o->carrier_range_lower = p->carrier_range_lower;
813                 o->carrier_range_upper = p->carrier_range_upper;
814
815                 p->max_pulse_width =
816                         (u32) pulse_width_count_to_ns(FIFO_RXTX, rxclk_divider);
817         } else {
818                 p->max_pulse_width =
819                             rxclk_rx_s_max_pulse_width(c, p->max_pulse_width,
820                                                        &rxclk_divider);
821         }
822         o->max_pulse_width = p->max_pulse_width;
823         atomic_set(&ir_state->rxclk_divider, rxclk_divider);
824
825         p->noise_filter_min_width =
826                             filter_rx_s_min_width(c, p->noise_filter_min_width);
827         o->noise_filter_min_width = p->noise_filter_min_width;
828
829         p->resolution = clock_divider_to_resolution(rxclk_divider);
830         o->resolution = p->resolution;
831
832         /* FIXME - make this dependent on resolution for better performance */
833         control_rx_irq_watermark(c, RX_FIFO_HALF_FULL);
834
835         control_rx_s_edge_detection(c, CNTRL_EDG_BOTH);
836
837         o->invert_level = p->invert_level;
838         atomic_set(&ir_state->rx_invert, p->invert_level);
839
840         o->interrupt_enable = p->interrupt_enable;
841         o->enable = p->enable;
842         if (p->enable) {
843                 unsigned long flags;
844
845                 spin_lock_irqsave(&ir_state->rx_kfifo_lock, flags);
846                 kfifo_reset(&ir_state->rx_kfifo);
847                 spin_unlock_irqrestore(&ir_state->rx_kfifo_lock, flags);
848                 if (p->interrupt_enable)
849                         irqenable_rx(sd, IRQEN_RSE | IRQEN_RTE | IRQEN_ROE);
850                 control_rx_enable(c, p->enable);
851         }
852
853         mutex_unlock(&ir_state->rx_params_lock);
854         return 0;
855 }
856
857 /* Transmitter */
858 static int cx25840_ir_tx_write(struct v4l2_subdev *sd, u8 *buf, size_t count,
859                                ssize_t *num)
860 {
861         struct cx25840_ir_state *ir_state = to_ir_state(sd);
862         struct i2c_client *c;
863
864         if (ir_state == NULL)
865                 return -ENODEV;
866
867         c = ir_state->c;
868 #if 0
869         /*
870          * FIXME - the code below is an incomplete and untested sketch of what
871          * may need to be done.  The critical part is to get 4 (or 8) pulses
872          * from the tx_kfifo, or converted from ns to the proper units from the
873          * input, and push them off to the hardware Tx FIFO right away, if the
874          * HW TX fifo needs service.  The rest can be pushed to the tx_kfifo in
875          * a less critical timeframe.  Also watch out for overruning the
876          * tx_kfifo - don't let it happen and let the caller know not all his
877          * pulses were written.
878          */
879         u32 *ns_pulse = (u32 *) buf;
880         unsigned int n;
881         u32 fifo_pulse[FIFO_TX_DEPTH];
882         u32 mark;
883
884         /* Compute how much we can fit in the tx kfifo */
885         n = CX25840_IR_TX_KFIFO_SIZE - kfifo_len(ir_state->tx_kfifo);
886         n = min(n, (unsigned int) count);
887         n /= sizeof(u32);
888
889         /* FIXME - turn on Tx Fifo service interrupt
890          * check hardware fifo level, and other stuff
891          */
892         for (i = 0; i < n; ) {
893                 for (j = 0; j < FIFO_TX_DEPTH / 2 && i < n; j++) {
894                         mark = ns_pulse[i] & LEVEL_MASK;
895                         fifo_pulse[j] = ns_to_pulse_width_count(
896                                          ns_pulse[i] &
897                                                ~LEVEL_MASK,
898                                          ir_state->txclk_divider);
899                         if (mark)
900                                 fifo_pulse[j] &= FIFO_RXTX_LVL;
901                         i++;
902                 }
903                 kfifo_put(ir_state->tx_kfifo, (u8 *) fifo_pulse,
904                                                                j * sizeof(u32));
905         }
906         *num = n * sizeof(u32);
907 #else
908         /* For now enable the Tx FIFO Service interrupt & pretend we did work */
909         irqenable_tx(sd, IRQEN_TSE);
910         *num = count;
911 #endif
912         return 0;
913 }
914
915 static int cx25840_ir_tx_g_parameters(struct v4l2_subdev *sd,
916                                       struct v4l2_subdev_ir_parameters *p)
917 {
918         struct cx25840_ir_state *ir_state = to_ir_state(sd);
919
920         if (ir_state == NULL)
921                 return -ENODEV;
922
923         mutex_lock(&ir_state->tx_params_lock);
924         memcpy(p, &ir_state->tx_params,
925                                       sizeof(struct v4l2_subdev_ir_parameters));
926         mutex_unlock(&ir_state->tx_params_lock);
927         return 0;
928 }
929
930 static int cx25840_ir_tx_shutdown(struct v4l2_subdev *sd)
931 {
932         struct cx25840_ir_state *ir_state = to_ir_state(sd);
933         struct i2c_client *c;
934
935         if (ir_state == NULL)
936                 return -ENODEV;
937
938         c = ir_state->c;
939         mutex_lock(&ir_state->tx_params_lock);
940
941         /* Disable or slow down all IR Tx circuits and counters */
942         irqenable_tx(sd, 0);
943         control_tx_enable(c, false);
944         control_tx_modulation_enable(c, false);
945         cx25840_write4(c, CX25840_IR_TXCLK_REG, TXCLK_TCD);
946
947         ir_state->tx_params.shutdown = true;
948
949         mutex_unlock(&ir_state->tx_params_lock);
950         return 0;
951 }
952
953 static int cx25840_ir_tx_s_parameters(struct v4l2_subdev *sd,
954                                       struct v4l2_subdev_ir_parameters *p)
955 {
956         struct cx25840_ir_state *ir_state = to_ir_state(sd);
957         struct i2c_client *c;
958         struct v4l2_subdev_ir_parameters *o;
959         u16 txclk_divider;
960
961         if (ir_state == NULL)
962                 return -ENODEV;
963
964         if (p->shutdown)
965                 return cx25840_ir_tx_shutdown(sd);
966
967         if (p->mode != V4L2_SUBDEV_IR_MODE_PULSE_WIDTH)
968                 return -ENOSYS;
969
970         c = ir_state->c;
971         o = &ir_state->tx_params;
972         mutex_lock(&ir_state->tx_params_lock);
973
974         o->shutdown = p->shutdown;
975
976         p->mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH;
977         o->mode = p->mode;
978
979         p->bytes_per_data_element = sizeof(union cx25840_ir_fifo_rec);
980         o->bytes_per_data_element = p->bytes_per_data_element;
981
982         /* Before we tweak the hardware, we have to disable the transmitter */
983         irqenable_tx(sd, 0);
984         control_tx_enable(c, false);
985
986         control_tx_modulation_enable(c, p->modulation);
987         o->modulation = p->modulation;
988
989         if (p->modulation) {
990                 p->carrier_freq = txclk_tx_s_carrier(c, p->carrier_freq,
991                                                      &txclk_divider);
992                 o->carrier_freq = p->carrier_freq;
993
994                 p->duty_cycle = cduty_tx_s_duty_cycle(c, p->duty_cycle);
995                 o->duty_cycle = p->duty_cycle;
996
997                 p->max_pulse_width =
998                         (u32) pulse_width_count_to_ns(FIFO_RXTX, txclk_divider);
999         } else {
1000                 p->max_pulse_width =
1001                             txclk_tx_s_max_pulse_width(c, p->max_pulse_width,
1002                                                        &txclk_divider);
1003         }
1004         o->max_pulse_width = p->max_pulse_width;
1005         atomic_set(&ir_state->txclk_divider, txclk_divider);
1006
1007         p->resolution = clock_divider_to_resolution(txclk_divider);
1008         o->resolution = p->resolution;
1009
1010         /* FIXME - make this dependent on resolution for better performance */
1011         control_tx_irq_watermark(c, TX_FIFO_HALF_EMPTY);
1012
1013         control_tx_polarity_invert(c, p->invert_carrier_sense);
1014         o->invert_carrier_sense = p->invert_carrier_sense;
1015
1016         /*
1017          * FIXME: we don't have hardware help for IO pin level inversion
1018          * here like we have on the CX23888.
1019          * Act on this with some mix of logical inversion of data levels,
1020          * carrier polarity, and carrier duty cycle.
1021          */
1022         o->invert_level = p->invert_level;
1023
1024         o->interrupt_enable = p->interrupt_enable;
1025         o->enable = p->enable;
1026         if (p->enable) {
1027                 /* reset tx_fifo here */
1028                 if (p->interrupt_enable)
1029                         irqenable_tx(sd, IRQEN_TSE);
1030                 control_tx_enable(c, p->enable);
1031         }
1032
1033         mutex_unlock(&ir_state->tx_params_lock);
1034         return 0;
1035 }
1036
1037
1038 /*
1039  * V4L2 Subdevice Core Ops support
1040  */
1041 int cx25840_ir_log_status(struct v4l2_subdev *sd)
1042 {
1043         struct cx25840_state *state = to_state(sd);
1044         struct i2c_client *c = state->c;
1045         char *s;
1046         int i, j;
1047         u32 cntrl, txclk, rxclk, cduty, stats, irqen, filtr;
1048
1049         /* The CX23888 chip doesn't have an IR controller on the A/V core */
1050         if (is_cx23888(state))
1051                 return 0;
1052
1053         cntrl = cx25840_read4(c, CX25840_IR_CNTRL_REG);
1054         txclk = cx25840_read4(c, CX25840_IR_TXCLK_REG) & TXCLK_TCD;
1055         rxclk = cx25840_read4(c, CX25840_IR_RXCLK_REG) & RXCLK_RCD;
1056         cduty = cx25840_read4(c, CX25840_IR_CDUTY_REG) & CDUTY_CDC;
1057         stats = cx25840_read4(c, CX25840_IR_STATS_REG);
1058         irqen = cx25840_read4(c, CX25840_IR_IRQEN_REG);
1059         if (is_cx23885(state) || is_cx23887(state))
1060                 irqen ^= IRQEN_MSK;
1061         filtr = cx25840_read4(c, CX25840_IR_FILTR_REG) & FILTR_LPF;
1062
1063         v4l2_info(sd, "IR Receiver:\n");
1064         v4l2_info(sd, "\tEnabled:                           %s\n",
1065                   cntrl & CNTRL_RXE ? "yes" : "no");
1066         v4l2_info(sd, "\tDemodulation from a carrier:       %s\n",
1067                   cntrl & CNTRL_DMD ? "enabled" : "disabled");
1068         v4l2_info(sd, "\tFIFO:                              %s\n",
1069                   cntrl & CNTRL_RFE ? "enabled" : "disabled");
1070         switch (cntrl & CNTRL_EDG) {
1071         case CNTRL_EDG_NONE:
1072                 s = "disabled";
1073                 break;
1074         case CNTRL_EDG_FALL:
1075                 s = "falling edge";
1076                 break;
1077         case CNTRL_EDG_RISE:
1078                 s = "rising edge";
1079                 break;
1080         case CNTRL_EDG_BOTH:
1081                 s = "rising & falling edges";
1082                 break;
1083         default:
1084                 s = "??? edge";
1085                 break;
1086         }
1087         v4l2_info(sd, "\tPulse timers' start/stop trigger:  %s\n", s);
1088         v4l2_info(sd, "\tFIFO data on pulse timer overflow: %s\n",
1089                   cntrl & CNTRL_R ? "not loaded" : "overflow marker");
1090         v4l2_info(sd, "\tFIFO interrupt watermark:          %s\n",
1091                   cntrl & CNTRL_RIC ? "not empty" : "half full or greater");
1092         v4l2_info(sd, "\tLoopback mode:                     %s\n",
1093                   cntrl & CNTRL_LBM ? "loopback active" : "normal receive");
1094         if (cntrl & CNTRL_DMD) {
1095                 v4l2_info(sd, "\tExpected carrier (16 clocks):      %u Hz\n",
1096                           clock_divider_to_carrier_freq(rxclk));
1097                 switch (cntrl & CNTRL_WIN) {
1098                 case CNTRL_WIN_3_3:
1099                         i = 3;
1100                         j = 3;
1101                         break;
1102                 case CNTRL_WIN_4_3:
1103                         i = 4;
1104                         j = 3;
1105                         break;
1106                 case CNTRL_WIN_3_4:
1107                         i = 3;
1108                         j = 4;
1109                         break;
1110                 case CNTRL_WIN_4_4:
1111                         i = 4;
1112                         j = 4;
1113                         break;
1114                 default:
1115                         i = 0;
1116                         j = 0;
1117                         break;
1118                 }
1119                 v4l2_info(sd, "\tNext carrier edge window:          16 clocks "
1120                           "-%1d/+%1d, %u to %u Hz\n", i, j,
1121                           clock_divider_to_freq(rxclk, 16 + j),
1122                           clock_divider_to_freq(rxclk, 16 - i));
1123         }
1124         v4l2_info(sd, "\tMax measurable pulse width:        %u us, %llu ns\n",
1125                   pulse_width_count_to_us(FIFO_RXTX, rxclk),
1126                   pulse_width_count_to_ns(FIFO_RXTX, rxclk));
1127         v4l2_info(sd, "\tLow pass filter:                   %s\n",
1128                   filtr ? "enabled" : "disabled");
1129         if (filtr)
1130                 v4l2_info(sd, "\tMin acceptable pulse width (LPF):  %u us, "
1131                           "%u ns\n",
1132                           lpf_count_to_us(filtr),
1133                           lpf_count_to_ns(filtr));
1134         v4l2_info(sd, "\tPulse width timer timed-out:       %s\n",
1135                   stats & STATS_RTO ? "yes" : "no");
1136         v4l2_info(sd, "\tPulse width timer time-out intr:   %s\n",
1137                   irqen & IRQEN_RTE ? "enabled" : "disabled");
1138         v4l2_info(sd, "\tFIFO overrun:                      %s\n",
1139                   stats & STATS_ROR ? "yes" : "no");
1140         v4l2_info(sd, "\tFIFO overrun interrupt:            %s\n",
1141                   irqen & IRQEN_ROE ? "enabled" : "disabled");
1142         v4l2_info(sd, "\tBusy:                              %s\n",
1143                   stats & STATS_RBY ? "yes" : "no");
1144         v4l2_info(sd, "\tFIFO service requested:            %s\n",
1145                   stats & STATS_RSR ? "yes" : "no");
1146         v4l2_info(sd, "\tFIFO service request interrupt:    %s\n",
1147                   irqen & IRQEN_RSE ? "enabled" : "disabled");
1148
1149         v4l2_info(sd, "IR Transmitter:\n");
1150         v4l2_info(sd, "\tEnabled:                           %s\n",
1151                   cntrl & CNTRL_TXE ? "yes" : "no");
1152         v4l2_info(sd, "\tModulation onto a carrier:         %s\n",
1153                   cntrl & CNTRL_MOD ? "enabled" : "disabled");
1154         v4l2_info(sd, "\tFIFO:                              %s\n",
1155                   cntrl & CNTRL_TFE ? "enabled" : "disabled");
1156         v4l2_info(sd, "\tFIFO interrupt watermark:          %s\n",
1157                   cntrl & CNTRL_TIC ? "not empty" : "half full or less");
1158         v4l2_info(sd, "\tCarrier polarity:                  %s\n",
1159                   cntrl & CNTRL_CPL ? "space:burst mark:noburst"
1160                                     : "space:noburst mark:burst");
1161         if (cntrl & CNTRL_MOD) {
1162                 v4l2_info(sd, "\tCarrier (16 clocks):               %u Hz\n",
1163                           clock_divider_to_carrier_freq(txclk));
1164                 v4l2_info(sd, "\tCarrier duty cycle:                %2u/16\n",
1165                           cduty + 1);
1166         }
1167         v4l2_info(sd, "\tMax pulse width:                   %u us, %llu ns\n",
1168                   pulse_width_count_to_us(FIFO_RXTX, txclk),
1169                   pulse_width_count_to_ns(FIFO_RXTX, txclk));
1170         v4l2_info(sd, "\tBusy:                              %s\n",
1171                   stats & STATS_TBY ? "yes" : "no");
1172         v4l2_info(sd, "\tFIFO service requested:            %s\n",
1173                   stats & STATS_TSR ? "yes" : "no");
1174         v4l2_info(sd, "\tFIFO service request interrupt:    %s\n",
1175                   irqen & IRQEN_TSE ? "enabled" : "disabled");
1176
1177         return 0;
1178 }
1179
1180
1181 const struct v4l2_subdev_ir_ops cx25840_ir_ops = {
1182         .rx_read = cx25840_ir_rx_read,
1183         .rx_g_parameters = cx25840_ir_rx_g_parameters,
1184         .rx_s_parameters = cx25840_ir_rx_s_parameters,
1185
1186         .tx_write = cx25840_ir_tx_write,
1187         .tx_g_parameters = cx25840_ir_tx_g_parameters,
1188         .tx_s_parameters = cx25840_ir_tx_s_parameters,
1189 };
1190
1191
1192 static const struct v4l2_subdev_ir_parameters default_rx_params = {
1193         .bytes_per_data_element = sizeof(union cx25840_ir_fifo_rec),
1194         .mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH,
1195
1196         .enable = false,
1197         .interrupt_enable = false,
1198         .shutdown = true,
1199
1200         .modulation = true,
1201         .carrier_freq = 36000, /* 36 kHz - RC-5, and RC-6 carrier */
1202
1203         /* RC-5: 666,667 ns = 1/36 kHz * 32 cycles * 1 mark * 0.75 */
1204         /* RC-6: 333,333 ns = 1/36 kHz * 16 cycles * 1 mark * 0.75 */
1205         .noise_filter_min_width = 333333, /* ns */
1206         .carrier_range_lower = 35000,
1207         .carrier_range_upper = 37000,
1208         .invert_level = false,
1209 };
1210
1211 static const struct v4l2_subdev_ir_parameters default_tx_params = {
1212         .bytes_per_data_element = sizeof(union cx25840_ir_fifo_rec),
1213         .mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH,
1214
1215         .enable = false,
1216         .interrupt_enable = false,
1217         .shutdown = true,
1218
1219         .modulation = true,
1220         .carrier_freq = 36000, /* 36 kHz - RC-5 carrier */
1221         .duty_cycle = 25,      /* 25 %   - RC-5 carrier */
1222         .invert_level = false,
1223         .invert_carrier_sense = false,
1224 };
1225
1226 int cx25840_ir_probe(struct v4l2_subdev *sd)
1227 {
1228         struct cx25840_state *state = to_state(sd);
1229         struct cx25840_ir_state *ir_state;
1230         struct v4l2_subdev_ir_parameters default_params;
1231
1232         /* Only init the IR controller for the CX2388[57] AV Core for now */
1233         if (!(is_cx23885(state) || is_cx23887(state)))
1234                 return 0;
1235
1236         ir_state = kzalloc(sizeof(struct cx25840_ir_state), GFP_KERNEL);
1237         if (ir_state == NULL)
1238                 return -ENOMEM;
1239
1240         spin_lock_init(&ir_state->rx_kfifo_lock);
1241         if (kfifo_alloc(&ir_state->rx_kfifo,
1242                         CX25840_IR_RX_KFIFO_SIZE, GFP_KERNEL)) {
1243                 kfree(ir_state);
1244                 return -ENOMEM;
1245         }
1246
1247         ir_state->c = state->c;
1248         state->ir_state = ir_state;
1249
1250         /* Ensure no interrupts arrive yet */
1251         if (is_cx23885(state) || is_cx23887(state))
1252                 cx25840_write4(ir_state->c, CX25840_IR_IRQEN_REG, IRQEN_MSK);
1253         else
1254                 cx25840_write4(ir_state->c, CX25840_IR_IRQEN_REG, 0);
1255
1256         mutex_init(&ir_state->rx_params_lock);
1257         memcpy(&default_params, &default_rx_params,
1258                        sizeof(struct v4l2_subdev_ir_parameters));
1259         v4l2_subdev_call(sd, ir, rx_s_parameters, &default_params);
1260
1261         mutex_init(&ir_state->tx_params_lock);
1262         memcpy(&default_params, &default_tx_params,
1263                        sizeof(struct v4l2_subdev_ir_parameters));
1264         v4l2_subdev_call(sd, ir, tx_s_parameters, &default_params);
1265
1266         return 0;
1267 }
1268
1269 int cx25840_ir_remove(struct v4l2_subdev *sd)
1270 {
1271         struct cx25840_state *state = to_state(sd);
1272         struct cx25840_ir_state *ir_state = to_ir_state(sd);
1273
1274         if (ir_state == NULL)
1275                 return -ENODEV;
1276
1277         cx25840_ir_rx_shutdown(sd);
1278         cx25840_ir_tx_shutdown(sd);
1279
1280         kfifo_free(&ir_state->rx_kfifo);
1281         kfree(ir_state);
1282         state->ir_state = NULL;
1283         return 0;
1284 }