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