blob: 2502a0a6709783b8c01d5de639d759d097f0f1cd [file] [log] [blame]
Andy Walls29f8a0a2009-09-26 23:17:30 -03001/*
2 * Driver for the Conexant CX23885/7/8 PCIe bridge
3 *
4 * CX23888 Integrated Consumer Infrared Controller
5 *
Andy Walls6afdeaf2010-05-23 18:53:35 -03006 * Copyright (C) 2009 Andy Walls <awalls@md.metrocast.net>
Andy Walls29f8a0a2009-09-26 23:17:30 -03007 *
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
Andy Walls1a0b9d82009-09-27 18:31:37 -030024#include <linux/kfifo.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Andy Walls1a0b9d82009-09-27 18:31:37 -030026
Andy Walls29f8a0a2009-09-26 23:17:30 -030027#include <media/v4l2-device.h>
28#include <media/v4l2-chip-ident.h>
Andy Wallsc02e0d12010-08-01 02:18:13 -030029#include <media/ir-core.h>
Andy Walls29f8a0a2009-09-26 23:17:30 -030030
31#include "cx23885.h"
32
Andy Walls1a0b9d82009-09-27 18:31:37 -030033static unsigned int ir_888_debug;
34module_param(ir_888_debug, int, 0644);
35MODULE_PARM_DESC(ir_888_debug, "enable debug messages [CX23888 IR controller]");
36
Andy Walls29f8a0a2009-09-26 23:17:30 -030037#define CX23888_IR_REG_BASE 0x170000
38/*
39 * These CX23888 register offsets have a straightforward one to one mapping
40 * to the CX23885 register offsets of 0x200 through 0x218
41 */
42#define CX23888_IR_CNTRL_REG 0x170000
Andy Walls1a0b9d82009-09-27 18:31:37 -030043#define CNTRL_WIN_3_3 0x00000000
44#define CNTRL_WIN_4_3 0x00000001
45#define CNTRL_WIN_3_4 0x00000002
46#define CNTRL_WIN_4_4 0x00000003
47#define CNTRL_WIN 0x00000003
48#define CNTRL_EDG_NONE 0x00000000
49#define CNTRL_EDG_FALL 0x00000004
50#define CNTRL_EDG_RISE 0x00000008
51#define CNTRL_EDG_BOTH 0x0000000C
52#define CNTRL_EDG 0x0000000C
53#define CNTRL_DMD 0x00000010
54#define CNTRL_MOD 0x00000020
55#define CNTRL_RFE 0x00000040
56#define CNTRL_TFE 0x00000080
57#define CNTRL_RXE 0x00000100
58#define CNTRL_TXE 0x00000200
59#define CNTRL_RIC 0x00000400
60#define CNTRL_TIC 0x00000800
61#define CNTRL_CPL 0x00001000
62#define CNTRL_LBM 0x00002000
63#define CNTRL_R 0x00004000
Andy Walls5a28d9a2010-07-18 19:57:25 -030064/* CX23888 specific control flag */
65#define CNTRL_IVO 0x00008000
Andy Walls1a0b9d82009-09-27 18:31:37 -030066
Andy Walls29f8a0a2009-09-26 23:17:30 -030067#define CX23888_IR_TXCLK_REG 0x170004
Andy Walls1a0b9d82009-09-27 18:31:37 -030068#define TXCLK_TCD 0x0000FFFF
69
Andy Walls29f8a0a2009-09-26 23:17:30 -030070#define CX23888_IR_RXCLK_REG 0x170008
Andy Walls1a0b9d82009-09-27 18:31:37 -030071#define RXCLK_RCD 0x0000FFFF
72
Andy Walls29f8a0a2009-09-26 23:17:30 -030073#define CX23888_IR_CDUTY_REG 0x17000C
Andy Walls1a0b9d82009-09-27 18:31:37 -030074#define CDUTY_CDC 0x0000000F
75
Andy Walls29f8a0a2009-09-26 23:17:30 -030076#define CX23888_IR_STATS_REG 0x170010
Andy Walls1a0b9d82009-09-27 18:31:37 -030077#define STATS_RTO 0x00000001
78#define STATS_ROR 0x00000002
79#define STATS_RBY 0x00000004
80#define STATS_TBY 0x00000008
81#define STATS_RSR 0x00000010
82#define STATS_TSR 0x00000020
83
Andy Walls29f8a0a2009-09-26 23:17:30 -030084#define CX23888_IR_IRQEN_REG 0x170014
Andy Walls1a0b9d82009-09-27 18:31:37 -030085#define IRQEN_RTE 0x00000001
86#define IRQEN_ROE 0x00000002
87#define IRQEN_RSE 0x00000010
88#define IRQEN_TSE 0x00000020
89
Andy Walls29f8a0a2009-09-26 23:17:30 -030090#define CX23888_IR_FILTR_REG 0x170018
Andy Walls1a0b9d82009-09-27 18:31:37 -030091#define FILTR_LPF 0x0000FFFF
92
Andy Walls29f8a0a2009-09-26 23:17:30 -030093/* This register doesn't follow the pattern; it's 0x23C on a CX23885 */
94#define CX23888_IR_FIFO_REG 0x170040
Andy Walls1a0b9d82009-09-27 18:31:37 -030095#define FIFO_RXTX 0x0000FFFF
96#define FIFO_RXTX_LVL 0x00010000
97#define FIFO_RXTX_RTO 0x0001FFFF
98#define FIFO_RX_NDV 0x00020000
99#define FIFO_RX_DEPTH 8
100#define FIFO_TX_DEPTH 8
Andy Walls29f8a0a2009-09-26 23:17:30 -0300101
102/* CX23888 unique registers */
103#define CX23888_IR_SEEDP_REG 0x17001C
104#define CX23888_IR_TIMOL_REG 0x170020
105#define CX23888_IR_WAKE0_REG 0x170024
106#define CX23888_IR_WAKE1_REG 0x170028
107#define CX23888_IR_WAKE2_REG 0x17002C
108#define CX23888_IR_MASK0_REG 0x170030
109#define CX23888_IR_MASK1_REG 0x170034
110#define CX23888_IR_MAKS2_REG 0x170038
111#define CX23888_IR_DPIPG_REG 0x17003C
112#define CX23888_IR_LEARN_REG 0x170044
113
Andy Walls1a0b9d82009-09-27 18:31:37 -0300114#define CX23888_VIDCLK_FREQ 108000000 /* 108 MHz, BT.656 */
Andy Walls928213a2009-10-29 22:24:34 -0300115#define CX23888_IR_REFCLK_FREQ (CX23888_VIDCLK_FREQ / 2)
Andy Walls1a0b9d82009-09-27 18:31:37 -0300116
Andy Wallsc02e0d12010-08-01 02:18:13 -0300117/*
118 * We use this union internally for convenience, but callers to tx_write
119 * and rx_read will be expecting records of type struct ir_raw_event.
120 * Always ensure the size of this union is dictated by struct ir_raw_event.
121 */
122union cx23888_ir_fifo_rec {
123 u32 hw_fifo_data;
124 struct ir_raw_event ir_core_data;
125};
126
127#define CX23888_IR_RX_KFIFO_SIZE (256 * sizeof(union cx23888_ir_fifo_rec))
128#define CX23888_IR_TX_KFIFO_SIZE (256 * sizeof(union cx23888_ir_fifo_rec))
Andy Walls29f8a0a2009-09-26 23:17:30 -0300129
130struct cx23888_ir_state {
131 struct v4l2_subdev sd;
132 struct cx23885_dev *dev;
133 u32 id;
134 u32 rev;
Andy Walls1a0b9d82009-09-27 18:31:37 -0300135
136 struct v4l2_subdev_ir_parameters rx_params;
137 struct mutex rx_params_lock;
138 atomic_t rxclk_divider;
139 atomic_t rx_invert;
140
Stefani Seibold7801edb2009-12-21 14:37:33 -0800141 struct kfifo rx_kfifo;
Andy Walls1a0b9d82009-09-27 18:31:37 -0300142 spinlock_t rx_kfifo_lock;
143
144 struct v4l2_subdev_ir_parameters tx_params;
145 struct mutex tx_params_lock;
146 atomic_t txclk_divider;
Andy Walls29f8a0a2009-09-26 23:17:30 -0300147};
148
149static inline struct cx23888_ir_state *to_state(struct v4l2_subdev *sd)
150{
151 return v4l2_get_subdevdata(sd);
152}
153
Andy Walls1a0b9d82009-09-27 18:31:37 -0300154/*
155 * IR register block read and write functions
156 */
Andy Walls29f8a0a2009-09-26 23:17:30 -0300157static
158inline int cx23888_ir_write4(struct cx23885_dev *dev, u32 addr, u32 value)
159{
160 cx_write(addr, value);
161 return 0;
162}
163
Andy Walls29f8a0a2009-09-26 23:17:30 -0300164static inline u32 cx23888_ir_read4(struct cx23885_dev *dev, u32 addr)
165{
166 return cx_read(addr);
167}
168
Andy Walls29f8a0a2009-09-26 23:17:30 -0300169static inline int cx23888_ir_and_or4(struct cx23885_dev *dev, u32 addr,
170 u32 and_mask, u32 or_value)
171{
Andy Walls1a0b9d82009-09-27 18:31:37 -0300172 cx_andor(addr, ~and_mask, or_value);
Andy Walls29f8a0a2009-09-26 23:17:30 -0300173 return 0;
174}
175
Andy Walls1a0b9d82009-09-27 18:31:37 -0300176/*
177 * Rx and Tx Clock Divider register computations
178 *
179 * Note the largest clock divider value of 0xffff corresponds to:
180 * (0xffff + 1) * 1000 / 108/2 MHz = 1,213,629.629... ns
181 * which fits in 21 bits, so we'll use unsigned int for time arguments.
182 */
183static inline u16 count_to_clock_divider(unsigned int d)
184{
Andy Walls928213a2009-10-29 22:24:34 -0300185 if (d > RXCLK_RCD + 1)
Andy Walls1a0b9d82009-09-27 18:31:37 -0300186 d = RXCLK_RCD;
187 else if (d < 2)
188 d = 1;
189 else
190 d--;
191 return (u16) d;
192}
193
194static inline u16 ns_to_clock_divider(unsigned int ns)
195{
196 return count_to_clock_divider(
Andy Walls928213a2009-10-29 22:24:34 -0300197 DIV_ROUND_CLOSEST(CX23888_IR_REFCLK_FREQ / 1000000 * ns, 1000));
Andy Walls1a0b9d82009-09-27 18:31:37 -0300198}
199
200static inline unsigned int clock_divider_to_ns(unsigned int divider)
201{
202 /* Period of the Rx or Tx clock in ns */
203 return DIV_ROUND_CLOSEST((divider + 1) * 1000,
Andy Walls928213a2009-10-29 22:24:34 -0300204 CX23888_IR_REFCLK_FREQ / 1000000);
Andy Walls1a0b9d82009-09-27 18:31:37 -0300205}
206
207static inline u16 carrier_freq_to_clock_divider(unsigned int freq)
208{
209 return count_to_clock_divider(
210 DIV_ROUND_CLOSEST(CX23888_IR_REFCLK_FREQ, freq * 16));
211}
212
213static inline unsigned int clock_divider_to_carrier_freq(unsigned int divider)
214{
215 return DIV_ROUND_CLOSEST(CX23888_IR_REFCLK_FREQ, (divider + 1) * 16);
216}
217
218static inline u16 freq_to_clock_divider(unsigned int freq,
219 unsigned int rollovers)
220{
221 return count_to_clock_divider(
222 DIV_ROUND_CLOSEST(CX23888_IR_REFCLK_FREQ, freq * rollovers));
223}
224
225static inline unsigned int clock_divider_to_freq(unsigned int divider,
226 unsigned int rollovers)
227{
228 return DIV_ROUND_CLOSEST(CX23888_IR_REFCLK_FREQ,
229 (divider + 1) * rollovers);
230}
231
232/*
233 * Low Pass Filter register calculations
234 *
235 * Note the largest count value of 0xffff corresponds to:
236 * 0xffff * 1000 / 108/2 MHz = 1,213,611.11... ns
237 * which fits in 21 bits, so we'll use unsigned int for time arguments.
238 */
239static inline u16 count_to_lpf_count(unsigned int d)
240{
241 if (d > FILTR_LPF)
242 d = FILTR_LPF;
243 else if (d < 4)
244 d = 0;
245 return (u16) d;
246}
247
248static inline u16 ns_to_lpf_count(unsigned int ns)
249{
250 return count_to_lpf_count(
Andy Walls928213a2009-10-29 22:24:34 -0300251 DIV_ROUND_CLOSEST(CX23888_IR_REFCLK_FREQ / 1000000 * ns, 1000));
Andy Walls1a0b9d82009-09-27 18:31:37 -0300252}
253
254static inline unsigned int lpf_count_to_ns(unsigned int count)
255{
256 /* Duration of the Low Pass Filter rejection window in ns */
Andy Walls928213a2009-10-29 22:24:34 -0300257 return DIV_ROUND_CLOSEST(count * 1000,
258 CX23888_IR_REFCLK_FREQ / 1000000);
Andy Walls1a0b9d82009-09-27 18:31:37 -0300259}
260
261static inline unsigned int lpf_count_to_us(unsigned int count)
262{
263 /* Duration of the Low Pass Filter rejection window in us */
Andy Walls928213a2009-10-29 22:24:34 -0300264 return DIV_ROUND_CLOSEST(count, CX23888_IR_REFCLK_FREQ / 1000000);
Andy Walls1a0b9d82009-09-27 18:31:37 -0300265}
266
267/*
268 * FIFO register pulse width count compuations
269 */
270static u32 clock_divider_to_resolution(u16 divider)
271{
272 /*
273 * Resolution is the duration of 1 tick of the readable portion of
274 * of the pulse width counter as read from the FIFO. The two lsb's are
275 * not readable, hence the << 2. This function returns ns.
276 */
277 return DIV_ROUND_CLOSEST((1 << 2) * ((u32) divider + 1) * 1000,
Andy Walls928213a2009-10-29 22:24:34 -0300278 CX23888_IR_REFCLK_FREQ / 1000000);
Andy Walls1a0b9d82009-09-27 18:31:37 -0300279}
280
281static u64 pulse_width_count_to_ns(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) * 1000; /* millicycles */
Andy Walls928213a2009-10-29 22:24:34 -0300291 rem = do_div(n, CX23888_IR_REFCLK_FREQ / 1000000); /* / MHz => ns */
292 if (rem >= CX23888_IR_REFCLK_FREQ / 1000000 / 2)
Andy Walls1a0b9d82009-09-27 18:31:37 -0300293 n++;
294 return n;
295}
296
297static unsigned int pulse_width_count_to_us(u16 count, u16 divider)
298{
299 u64 n;
300 u32 rem;
301
302 /*
303 * The 2 lsb's of the pulse width timer count are not readable, hence
304 * the (count << 2) | 0x3
305 */
Andy Walls928213a2009-10-29 22:24:34 -0300306 n = (((u64) count << 2) | 0x3) * (divider + 1); /* cycles */
307 rem = do_div(n, CX23888_IR_REFCLK_FREQ / 1000000); /* / MHz => us */
308 if (rem >= CX23888_IR_REFCLK_FREQ / 1000000 / 2)
Andy Walls1a0b9d82009-09-27 18:31:37 -0300309 n++;
310 return (unsigned int) n;
311}
312
313/*
314 * Pulse Clocks computations: Combined Pulse Width Count & Rx Clock Counts
315 *
316 * The total pulse clock count is an 18 bit pulse width timer count as the most
317 * significant part and (up to) 16 bit clock divider count as a modulus.
318 * When the Rx clock divider ticks down to 0, it increments the 18 bit pulse
319 * width timer count's least significant bit.
320 */
321static u64 ns_to_pulse_clocks(u32 ns)
322{
323 u64 clocks;
324 u32 rem;
Andy Walls928213a2009-10-29 22:24:34 -0300325 clocks = CX23888_IR_REFCLK_FREQ / 1000000 * (u64) ns; /* millicycles */
Andy Walls1a0b9d82009-09-27 18:31:37 -0300326 rem = do_div(clocks, 1000); /* /1000 = cycles */
Andy Walls928213a2009-10-29 22:24:34 -0300327 if (rem >= 1000 / 2)
Andy Walls1a0b9d82009-09-27 18:31:37 -0300328 clocks++;
329 return clocks;
330}
331
332static u16 pulse_clocks_to_clock_divider(u64 count)
333{
334 u32 rem;
335
336 rem = do_div(count, (FIFO_RXTX << 2) | 0x3);
337
338 /* net result needs to be rounded down and decremented by 1 */
Andy Walls928213a2009-10-29 22:24:34 -0300339 if (count > RXCLK_RCD + 1)
Andy Walls1a0b9d82009-09-27 18:31:37 -0300340 count = RXCLK_RCD;
341 else if (count < 2)
342 count = 1;
343 else
344 count--;
345 return (u16) count;
346}
347
348/*
349 * IR Control Register helpers
350 */
351enum tx_fifo_watermark {
352 TX_FIFO_HALF_EMPTY = 0,
353 TX_FIFO_EMPTY = CNTRL_TIC,
354};
355
356enum rx_fifo_watermark {
357 RX_FIFO_HALF_FULL = 0,
358 RX_FIFO_NOT_EMPTY = CNTRL_RIC,
359};
360
361static inline void control_tx_irq_watermark(struct cx23885_dev *dev,
362 enum tx_fifo_watermark level)
363{
364 cx23888_ir_and_or4(dev, CX23888_IR_CNTRL_REG, ~CNTRL_TIC, level);
365}
366
367static inline void control_rx_irq_watermark(struct cx23885_dev *dev,
368 enum rx_fifo_watermark level)
369{
370 cx23888_ir_and_or4(dev, CX23888_IR_CNTRL_REG, ~CNTRL_RIC, level);
371}
372
373static inline void control_tx_enable(struct cx23885_dev *dev, bool enable)
374{
375 cx23888_ir_and_or4(dev, CX23888_IR_CNTRL_REG, ~(CNTRL_TXE | CNTRL_TFE),
376 enable ? (CNTRL_TXE | CNTRL_TFE) : 0);
377}
378
379static inline void control_rx_enable(struct cx23885_dev *dev, bool enable)
380{
381 cx23888_ir_and_or4(dev, CX23888_IR_CNTRL_REG, ~(CNTRL_RXE | CNTRL_RFE),
382 enable ? (CNTRL_RXE | CNTRL_RFE) : 0);
383}
384
385static inline void control_tx_modulation_enable(struct cx23885_dev *dev,
386 bool enable)
387{
388 cx23888_ir_and_or4(dev, CX23888_IR_CNTRL_REG, ~CNTRL_MOD,
389 enable ? CNTRL_MOD : 0);
390}
391
392static inline void control_rx_demodulation_enable(struct cx23885_dev *dev,
393 bool enable)
394{
395 cx23888_ir_and_or4(dev, CX23888_IR_CNTRL_REG, ~CNTRL_DMD,
396 enable ? CNTRL_DMD : 0);
397}
398
399static inline void control_rx_s_edge_detection(struct cx23885_dev *dev,
400 u32 edge_types)
401{
402 cx23888_ir_and_or4(dev, CX23888_IR_CNTRL_REG, ~CNTRL_EDG_BOTH,
403 edge_types & CNTRL_EDG_BOTH);
404}
405
406static void control_rx_s_carrier_window(struct cx23885_dev *dev,
407 unsigned int carrier,
408 unsigned int *carrier_range_low,
409 unsigned int *carrier_range_high)
410{
411 u32 v;
412 unsigned int c16 = carrier * 16;
413
414 if (*carrier_range_low < DIV_ROUND_CLOSEST(c16, 16 + 3)) {
415 v = CNTRL_WIN_3_4;
416 *carrier_range_low = DIV_ROUND_CLOSEST(c16, 16 + 4);
417 } else {
418 v = CNTRL_WIN_3_3;
419 *carrier_range_low = DIV_ROUND_CLOSEST(c16, 16 + 3);
420 }
421
422 if (*carrier_range_high > DIV_ROUND_CLOSEST(c16, 16 - 3)) {
423 v |= CNTRL_WIN_4_3;
424 *carrier_range_high = DIV_ROUND_CLOSEST(c16, 16 - 4);
425 } else {
426 v |= CNTRL_WIN_3_3;
427 *carrier_range_high = DIV_ROUND_CLOSEST(c16, 16 - 3);
428 }
429 cx23888_ir_and_or4(dev, CX23888_IR_CNTRL_REG, ~CNTRL_WIN, v);
430}
431
432static inline void control_tx_polarity_invert(struct cx23885_dev *dev,
433 bool invert)
434{
435 cx23888_ir_and_or4(dev, CX23888_IR_CNTRL_REG, ~CNTRL_CPL,
436 invert ? CNTRL_CPL : 0);
437}
438
Andy Walls5a28d9a2010-07-18 19:57:25 -0300439static inline void control_tx_level_invert(struct cx23885_dev *dev,
440 bool invert)
441{
442 cx23888_ir_and_or4(dev, CX23888_IR_CNTRL_REG, ~CNTRL_IVO,
443 invert ? CNTRL_IVO : 0);
444}
445
Andy Walls1a0b9d82009-09-27 18:31:37 -0300446/*
447 * IR Rx & Tx Clock Register helpers
448 */
449static unsigned int txclk_tx_s_carrier(struct cx23885_dev *dev,
450 unsigned int freq,
451 u16 *divider)
452{
453 *divider = carrier_freq_to_clock_divider(freq);
454 cx23888_ir_write4(dev, CX23888_IR_TXCLK_REG, *divider);
455 return clock_divider_to_carrier_freq(*divider);
456}
457
458static unsigned int rxclk_rx_s_carrier(struct cx23885_dev *dev,
459 unsigned int freq,
460 u16 *divider)
461{
462 *divider = carrier_freq_to_clock_divider(freq);
463 cx23888_ir_write4(dev, CX23888_IR_RXCLK_REG, *divider);
464 return clock_divider_to_carrier_freq(*divider);
465}
466
467static u32 txclk_tx_s_max_pulse_width(struct cx23885_dev *dev, u32 ns,
468 u16 *divider)
469{
470 u64 pulse_clocks;
471
Andy Wallsc02e0d12010-08-01 02:18:13 -0300472 if (ns > IR_MAX_DURATION)
473 ns = IR_MAX_DURATION;
Andy Walls1a0b9d82009-09-27 18:31:37 -0300474 pulse_clocks = ns_to_pulse_clocks(ns);
475 *divider = pulse_clocks_to_clock_divider(pulse_clocks);
476 cx23888_ir_write4(dev, CX23888_IR_TXCLK_REG, *divider);
477 return (u32) pulse_width_count_to_ns(FIFO_RXTX, *divider);
478}
479
480static u32 rxclk_rx_s_max_pulse_width(struct cx23885_dev *dev, u32 ns,
481 u16 *divider)
482{
483 u64 pulse_clocks;
484
Andy Wallsc02e0d12010-08-01 02:18:13 -0300485 if (ns > IR_MAX_DURATION)
486 ns = IR_MAX_DURATION;
Andy Walls1a0b9d82009-09-27 18:31:37 -0300487 pulse_clocks = ns_to_pulse_clocks(ns);
488 *divider = pulse_clocks_to_clock_divider(pulse_clocks);
489 cx23888_ir_write4(dev, CX23888_IR_RXCLK_REG, *divider);
490 return (u32) pulse_width_count_to_ns(FIFO_RXTX, *divider);
491}
492
493/*
494 * IR Tx Carrier Duty Cycle register helpers
495 */
496static unsigned int cduty_tx_s_duty_cycle(struct cx23885_dev *dev,
497 unsigned int duty_cycle)
498{
499 u32 n;
500 n = DIV_ROUND_CLOSEST(duty_cycle * 100, 625); /* 16ths of 100% */
501 if (n != 0)
502 n--;
503 if (n > 15)
504 n = 15;
505 cx23888_ir_write4(dev, CX23888_IR_CDUTY_REG, n);
Andy Walls928213a2009-10-29 22:24:34 -0300506 return DIV_ROUND_CLOSEST((n + 1) * 100, 16);
Andy Walls1a0b9d82009-09-27 18:31:37 -0300507}
508
509/*
510 * IR Filter Register helpers
511 */
512static u32 filter_rx_s_min_width(struct cx23885_dev *dev, u32 min_width_ns)
513{
514 u32 count = ns_to_lpf_count(min_width_ns);
515 cx23888_ir_write4(dev, CX23888_IR_FILTR_REG, count);
516 return lpf_count_to_ns(count);
517}
518
519/*
520 * IR IRQ Enable Register helpers
521 */
522static inline void irqenable_rx(struct cx23885_dev *dev, u32 mask)
523{
524 mask &= (IRQEN_RTE | IRQEN_ROE | IRQEN_RSE);
525 cx23888_ir_and_or4(dev, CX23888_IR_IRQEN_REG,
526 ~(IRQEN_RTE | IRQEN_ROE | IRQEN_RSE), mask);
527}
528
529static inline void irqenable_tx(struct cx23885_dev *dev, u32 mask)
530{
531 mask &= IRQEN_TSE;
532 cx23888_ir_and_or4(dev, CX23888_IR_IRQEN_REG, ~IRQEN_TSE, mask);
533}
534
535/*
536 * V4L2 Subdevice IR Ops
537 */
538static int cx23888_ir_irq_handler(struct v4l2_subdev *sd, u32 status,
539 bool *handled)
540{
541 struct cx23888_ir_state *state = to_state(sd);
542 struct cx23885_dev *dev = state->dev;
Stefani Seibold7801edb2009-12-21 14:37:33 -0800543 unsigned long flags;
Andy Walls1a0b9d82009-09-27 18:31:37 -0300544
545 u32 cntrl = cx23888_ir_read4(dev, CX23888_IR_CNTRL_REG);
546 u32 irqen = cx23888_ir_read4(dev, CX23888_IR_IRQEN_REG);
547 u32 stats = cx23888_ir_read4(dev, CX23888_IR_STATS_REG);
548
Andy Wallsc02e0d12010-08-01 02:18:13 -0300549 union cx23888_ir_fifo_rec rx_data[FIFO_RX_DEPTH];
550 unsigned int i, j, k;
Andy Walls1a0b9d82009-09-27 18:31:37 -0300551 u32 events, v;
552 int tsr, rsr, rto, ror, tse, rse, rte, roe, kror;
553
554 tsr = stats & STATS_TSR; /* Tx FIFO Service Request */
555 rsr = stats & STATS_RSR; /* Rx FIFO Service Request */
556 rto = stats & STATS_RTO; /* Rx Pulse Width Timer Time Out */
557 ror = stats & STATS_ROR; /* Rx FIFO Over Run */
558
559 tse = irqen & IRQEN_TSE; /* Tx FIFO Service Request IRQ Enable */
560 rse = irqen & IRQEN_RSE; /* Rx FIFO Service Reuqest IRQ Enable */
561 rte = irqen & IRQEN_RTE; /* Rx Pulse Width Timer Time Out IRQ Enable */
562 roe = irqen & IRQEN_ROE; /* Rx FIFO Over Run IRQ Enable */
563
564 *handled = false;
565 v4l2_dbg(2, ir_888_debug, sd, "IRQ Status: %s %s %s %s %s %s\n",
566 tsr ? "tsr" : " ", rsr ? "rsr" : " ",
567 rto ? "rto" : " ", ror ? "ror" : " ",
568 stats & STATS_TBY ? "tby" : " ",
569 stats & STATS_RBY ? "rby" : " ");
570
571 v4l2_dbg(2, ir_888_debug, sd, "IRQ Enables: %s %s %s %s\n",
572 tse ? "tse" : " ", rse ? "rse" : " ",
573 rte ? "rte" : " ", roe ? "roe" : " ");
574
575 /*
576 * Transmitter interrupt service
577 */
578 if (tse && tsr) {
579 /*
580 * TODO:
581 * Check the watermark threshold setting
582 * Pull FIFO_TX_DEPTH or FIFO_TX_DEPTH/2 entries from tx_kfifo
583 * Push the data to the hardware FIFO.
584 * If there was nothing more to send in the tx_kfifo, disable
585 * the TSR IRQ and notify the v4l2_device.
586 * If there was something in the tx_kfifo, check the tx_kfifo
587 * level and notify the v4l2_device, if it is low.
588 */
589 /* For now, inhibit TSR interrupt until Tx is implemented */
590 irqenable_tx(dev, 0);
591 events = V4L2_SUBDEV_IR_TX_FIFO_SERVICE_REQ;
592 v4l2_subdev_notify(sd, V4L2_SUBDEV_IR_TX_NOTIFY, &events);
593 *handled = true;
594 }
595
596 /*
597 * Receiver interrupt service
598 */
599 kror = 0;
600 if ((rse && rsr) || (rte && rto)) {
601 /*
602 * Receive data on RSR to clear the STATS_RSR.
603 * Receive data on RTO, since we may not have yet hit the RSR
604 * watermark when we receive the RTO.
605 */
606 for (i = 0, v = FIFO_RX_NDV;
607 (v & FIFO_RX_NDV) && !kror; i = 0) {
608 for (j = 0;
609 (v & FIFO_RX_NDV) && j < FIFO_RX_DEPTH; j++) {
610 v = cx23888_ir_read4(dev, CX23888_IR_FIFO_REG);
Andy Wallsc02e0d12010-08-01 02:18:13 -0300611 rx_data[i].hw_fifo_data = v & ~FIFO_RX_NDV;
612 i++;
Andy Walls1a0b9d82009-09-27 18:31:37 -0300613 }
614 if (i == 0)
615 break;
Andy Wallsc02e0d12010-08-01 02:18:13 -0300616 j = i * sizeof(union cx23888_ir_fifo_rec);
Stefani Seibold7801edb2009-12-21 14:37:33 -0800617 k = kfifo_in_locked(&state->rx_kfifo,
618 (unsigned char *) rx_data, j,
619 &state->rx_kfifo_lock);
Andy Walls1a0b9d82009-09-27 18:31:37 -0300620 if (k != j)
621 kror++; /* rx_kfifo over run */
622 }
623 *handled = true;
624 }
625
626 events = 0;
627 v = 0;
628 if (kror) {
629 events |= V4L2_SUBDEV_IR_RX_SW_FIFO_OVERRUN;
630 v4l2_err(sd, "IR receiver software FIFO overrun\n");
631 }
632 if (roe && ror) {
633 /*
634 * The RX FIFO Enable (CNTRL_RFE) must be toggled to clear
635 * the Rx FIFO Over Run status (STATS_ROR)
636 */
637 v |= CNTRL_RFE;
638 events |= V4L2_SUBDEV_IR_RX_HW_FIFO_OVERRUN;
639 v4l2_err(sd, "IR receiver hardware FIFO overrun\n");
640 }
641 if (rte && rto) {
642 /*
643 * The IR Receiver Enable (CNTRL_RXE) must be toggled to clear
644 * the Rx Pulse Width Timer Time Out (STATS_RTO)
645 */
646 v |= CNTRL_RXE;
647 events |= V4L2_SUBDEV_IR_RX_END_OF_RX_DETECTED;
648 }
649 if (v) {
650 /* Clear STATS_ROR & STATS_RTO as needed by reseting hardware */
651 cx23888_ir_write4(dev, CX23888_IR_CNTRL_REG, cntrl & ~v);
652 cx23888_ir_write4(dev, CX23888_IR_CNTRL_REG, cntrl);
653 *handled = true;
654 }
Stefani Seibold7801edb2009-12-21 14:37:33 -0800655
656 spin_lock_irqsave(&state->rx_kfifo_lock, flags);
657 if (kfifo_len(&state->rx_kfifo) >= CX23888_IR_RX_KFIFO_SIZE / 2)
Andy Walls1a0b9d82009-09-27 18:31:37 -0300658 events |= V4L2_SUBDEV_IR_RX_FIFO_SERVICE_REQ;
Stefani Seibold7801edb2009-12-21 14:37:33 -0800659 spin_unlock_irqrestore(&state->rx_kfifo_lock, flags);
Andy Walls1a0b9d82009-09-27 18:31:37 -0300660
661 if (events)
662 v4l2_subdev_notify(sd, V4L2_SUBDEV_IR_RX_NOTIFY, &events);
663 return 0;
664}
665
666/* Receiver */
667static int cx23888_ir_rx_read(struct v4l2_subdev *sd, u8 *buf, size_t count,
668 ssize_t *num)
669{
670 struct cx23888_ir_state *state = to_state(sd);
671 bool invert = (bool) atomic_read(&state->rx_invert);
672 u16 divider = (u16) atomic_read(&state->rxclk_divider);
673
674 unsigned int i, n;
Andy Wallsc02e0d12010-08-01 02:18:13 -0300675 union cx23888_ir_fifo_rec *p;
676 unsigned u, v;
Andy Walls1a0b9d82009-09-27 18:31:37 -0300677
Andy Wallsc02e0d12010-08-01 02:18:13 -0300678 n = count / sizeof(union cx23888_ir_fifo_rec)
679 * sizeof(union cx23888_ir_fifo_rec);
Andy Walls1a0b9d82009-09-27 18:31:37 -0300680 if (n == 0) {
681 *num = 0;
682 return 0;
683 }
684
Stefani Seibold7801edb2009-12-21 14:37:33 -0800685 n = kfifo_out_locked(&state->rx_kfifo, buf, n, &state->rx_kfifo_lock);
Andy Walls1a0b9d82009-09-27 18:31:37 -0300686
Andy Wallsc02e0d12010-08-01 02:18:13 -0300687 n /= sizeof(union cx23888_ir_fifo_rec);
688 *num = n * sizeof(union cx23888_ir_fifo_rec);
Andy Walls1a0b9d82009-09-27 18:31:37 -0300689
Andy Wallsc02e0d12010-08-01 02:18:13 -0300690 for (p = (union cx23888_ir_fifo_rec *) buf, i = 0; i < n; p++, i++) {
Andy Walls1a0b9d82009-09-27 18:31:37 -0300691
Andy Wallsc02e0d12010-08-01 02:18:13 -0300692 if ((p->hw_fifo_data & FIFO_RXTX_RTO) == FIFO_RXTX_RTO) {
Andy Walls2560d942010-07-31 23:28:37 -0300693 /* Assume RTO was because of no IR light input */
694 u = 0;
695 v4l2_dbg(2, ir_888_debug, sd, "rx read: end of rx\n");
696 } else {
Andy Wallsc02e0d12010-08-01 02:18:13 -0300697 u = (p->hw_fifo_data & FIFO_RXTX_LVL) ? 1 : 0;
Andy Walls2560d942010-07-31 23:28:37 -0300698 if (invert)
Andy Wallsc02e0d12010-08-01 02:18:13 -0300699 u = u ? 0 : 1;
Andy Walls2560d942010-07-31 23:28:37 -0300700 }
Andy Walls1a0b9d82009-09-27 18:31:37 -0300701
Andy Wallsc02e0d12010-08-01 02:18:13 -0300702 v = (unsigned) pulse_width_count_to_ns(
703 (u16) (p->hw_fifo_data & FIFO_RXTX), divider);
704 if (v > IR_MAX_DURATION)
705 v = IR_MAX_DURATION;
Andy Walls1a0b9d82009-09-27 18:31:37 -0300706
Andy Wallsc02e0d12010-08-01 02:18:13 -0300707 p->ir_core_data.pulse = u;
708 p->ir_core_data.duration = v;
Andy Walls1a0b9d82009-09-27 18:31:37 -0300709
710 v4l2_dbg(2, ir_888_debug, sd, "rx read: %10u ns %s\n",
711 v, u ? "mark" : "space");
712 }
713 return 0;
714}
715
716static int cx23888_ir_rx_g_parameters(struct v4l2_subdev *sd,
717 struct v4l2_subdev_ir_parameters *p)
718{
719 struct cx23888_ir_state *state = to_state(sd);
720 mutex_lock(&state->rx_params_lock);
721 memcpy(p, &state->rx_params, sizeof(struct v4l2_subdev_ir_parameters));
722 mutex_unlock(&state->rx_params_lock);
723 return 0;
724}
725
726static int cx23888_ir_rx_shutdown(struct v4l2_subdev *sd)
727{
728 struct cx23888_ir_state *state = to_state(sd);
729 struct cx23885_dev *dev = state->dev;
730
731 mutex_lock(&state->rx_params_lock);
732
733 /* Disable or slow down all IR Rx circuits and counters */
734 irqenable_rx(dev, 0);
735 control_rx_enable(dev, false);
736 control_rx_demodulation_enable(dev, false);
737 control_rx_s_edge_detection(dev, CNTRL_EDG_NONE);
738 filter_rx_s_min_width(dev, 0);
739 cx23888_ir_write4(dev, CX23888_IR_RXCLK_REG, RXCLK_RCD);
740
741 state->rx_params.shutdown = true;
742
743 mutex_unlock(&state->rx_params_lock);
744 return 0;
745}
746
747static int cx23888_ir_rx_s_parameters(struct v4l2_subdev *sd,
748 struct v4l2_subdev_ir_parameters *p)
749{
750 struct cx23888_ir_state *state = to_state(sd);
751 struct cx23885_dev *dev = state->dev;
752 struct v4l2_subdev_ir_parameters *o = &state->rx_params;
753 u16 rxclk_divider;
754
755 if (p->shutdown)
756 return cx23888_ir_rx_shutdown(sd);
757
758 if (p->mode != V4L2_SUBDEV_IR_MODE_PULSE_WIDTH)
759 return -ENOSYS;
760
761 mutex_lock(&state->rx_params_lock);
762
763 o->shutdown = p->shutdown;
764
765 o->mode = p->mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH;
766
Andy Wallsc02e0d12010-08-01 02:18:13 -0300767 o->bytes_per_data_element = p->bytes_per_data_element
768 = sizeof(union cx23888_ir_fifo_rec);
Andy Walls1a0b9d82009-09-27 18:31:37 -0300769
770 /* Before we tweak the hardware, we have to disable the receiver */
771 irqenable_rx(dev, 0);
772 control_rx_enable(dev, false);
773
774 control_rx_demodulation_enable(dev, p->modulation);
775 o->modulation = p->modulation;
776
777 if (p->modulation) {
778 p->carrier_freq = rxclk_rx_s_carrier(dev, p->carrier_freq,
779 &rxclk_divider);
780
781 o->carrier_freq = p->carrier_freq;
782
783 o->duty_cycle = p->duty_cycle = 50;
784
785 control_rx_s_carrier_window(dev, p->carrier_freq,
786 &p->carrier_range_lower,
787 &p->carrier_range_upper);
788 o->carrier_range_lower = p->carrier_range_lower;
789 o->carrier_range_upper = p->carrier_range_upper;
Andy Wallsceb152a2010-07-31 21:57:42 -0300790
791 p->max_pulse_width =
792 (u32) pulse_width_count_to_ns(FIFO_RXTX, rxclk_divider);
Andy Walls1a0b9d82009-09-27 18:31:37 -0300793 } else {
794 p->max_pulse_width =
795 rxclk_rx_s_max_pulse_width(dev, p->max_pulse_width,
796 &rxclk_divider);
Andy Walls1a0b9d82009-09-27 18:31:37 -0300797 }
Andy Wallsceb152a2010-07-31 21:57:42 -0300798 o->max_pulse_width = p->max_pulse_width;
Andy Walls1a0b9d82009-09-27 18:31:37 -0300799 atomic_set(&state->rxclk_divider, rxclk_divider);
800
801 p->noise_filter_min_width =
802 filter_rx_s_min_width(dev, p->noise_filter_min_width);
803 o->noise_filter_min_width = p->noise_filter_min_width;
804
805 p->resolution = clock_divider_to_resolution(rxclk_divider);
806 o->resolution = p->resolution;
807
808 /* FIXME - make this dependent on resolution for better performance */
809 control_rx_irq_watermark(dev, RX_FIFO_HALF_FULL);
810
811 control_rx_s_edge_detection(dev, CNTRL_EDG_BOTH);
812
Andy Walls5a28d9a2010-07-18 19:57:25 -0300813 o->invert_level = p->invert_level;
814 atomic_set(&state->rx_invert, p->invert_level);
Andy Walls1a0b9d82009-09-27 18:31:37 -0300815
816 o->interrupt_enable = p->interrupt_enable;
817 o->enable = p->enable;
818 if (p->enable) {
Stefani Seibold7801edb2009-12-21 14:37:33 -0800819 unsigned long flags;
820
821 spin_lock_irqsave(&state->rx_kfifo_lock, flags);
822 kfifo_reset(&state->rx_kfifo);
823 /* reset tx_fifo too if there is one... */
824 spin_unlock_irqrestore(&state->rx_kfifo_lock, flags);
Andy Walls1a0b9d82009-09-27 18:31:37 -0300825 if (p->interrupt_enable)
826 irqenable_rx(dev, IRQEN_RSE | IRQEN_RTE | IRQEN_ROE);
827 control_rx_enable(dev, p->enable);
828 }
829
830 mutex_unlock(&state->rx_params_lock);
831 return 0;
832}
833
834/* Transmitter */
835static int cx23888_ir_tx_write(struct v4l2_subdev *sd, u8 *buf, size_t count,
836 ssize_t *num)
837{
838 struct cx23888_ir_state *state = to_state(sd);
839 struct cx23885_dev *dev = state->dev;
840 /* For now enable the Tx FIFO Service interrupt & pretend we did work */
841 irqenable_tx(dev, IRQEN_TSE);
842 *num = count;
843 return 0;
844}
845
846static int cx23888_ir_tx_g_parameters(struct v4l2_subdev *sd,
847 struct v4l2_subdev_ir_parameters *p)
848{
849 struct cx23888_ir_state *state = to_state(sd);
850 mutex_lock(&state->tx_params_lock);
851 memcpy(p, &state->tx_params, sizeof(struct v4l2_subdev_ir_parameters));
852 mutex_unlock(&state->tx_params_lock);
853 return 0;
854}
855
856static int cx23888_ir_tx_shutdown(struct v4l2_subdev *sd)
857{
858 struct cx23888_ir_state *state = to_state(sd);
859 struct cx23885_dev *dev = state->dev;
860
861 mutex_lock(&state->tx_params_lock);
862
863 /* Disable or slow down all IR Tx circuits and counters */
864 irqenable_tx(dev, 0);
865 control_tx_enable(dev, false);
866 control_tx_modulation_enable(dev, false);
867 cx23888_ir_write4(dev, CX23888_IR_TXCLK_REG, TXCLK_TCD);
868
869 state->tx_params.shutdown = true;
870
871 mutex_unlock(&state->tx_params_lock);
872 return 0;
873}
874
875static int cx23888_ir_tx_s_parameters(struct v4l2_subdev *sd,
876 struct v4l2_subdev_ir_parameters *p)
877{
878 struct cx23888_ir_state *state = to_state(sd);
879 struct cx23885_dev *dev = state->dev;
880 struct v4l2_subdev_ir_parameters *o = &state->tx_params;
881 u16 txclk_divider;
882
883 if (p->shutdown)
884 return cx23888_ir_tx_shutdown(sd);
885
886 if (p->mode != V4L2_SUBDEV_IR_MODE_PULSE_WIDTH)
887 return -ENOSYS;
888
889 mutex_lock(&state->tx_params_lock);
890
891 o->shutdown = p->shutdown;
892
893 o->mode = p->mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH;
894
Andy Wallsc02e0d12010-08-01 02:18:13 -0300895 o->bytes_per_data_element = p->bytes_per_data_element
896 = sizeof(union cx23888_ir_fifo_rec);
Andy Walls1a0b9d82009-09-27 18:31:37 -0300897
898 /* Before we tweak the hardware, we have to disable the transmitter */
899 irqenable_tx(dev, 0);
900 control_tx_enable(dev, false);
901
902 control_tx_modulation_enable(dev, p->modulation);
903 o->modulation = p->modulation;
904
905 if (p->modulation) {
906 p->carrier_freq = txclk_tx_s_carrier(dev, p->carrier_freq,
907 &txclk_divider);
908 o->carrier_freq = p->carrier_freq;
909
910 p->duty_cycle = cduty_tx_s_duty_cycle(dev, p->duty_cycle);
911 o->duty_cycle = p->duty_cycle;
Andy Wallsceb152a2010-07-31 21:57:42 -0300912
913 p->max_pulse_width =
914 (u32) pulse_width_count_to_ns(FIFO_RXTX, txclk_divider);
Andy Walls1a0b9d82009-09-27 18:31:37 -0300915 } else {
916 p->max_pulse_width =
917 txclk_tx_s_max_pulse_width(dev, p->max_pulse_width,
918 &txclk_divider);
Andy Walls1a0b9d82009-09-27 18:31:37 -0300919 }
Andy Wallsceb152a2010-07-31 21:57:42 -0300920 o->max_pulse_width = p->max_pulse_width;
Andy Walls1a0b9d82009-09-27 18:31:37 -0300921 atomic_set(&state->txclk_divider, txclk_divider);
922
923 p->resolution = clock_divider_to_resolution(txclk_divider);
924 o->resolution = p->resolution;
925
926 /* FIXME - make this dependent on resolution for better performance */
927 control_tx_irq_watermark(dev, TX_FIFO_HALF_EMPTY);
928
Andy Walls5a28d9a2010-07-18 19:57:25 -0300929 control_tx_polarity_invert(dev, p->invert_carrier_sense);
930 o->invert_carrier_sense = p->invert_carrier_sense;
931
932 control_tx_level_invert(dev, p->invert_level);
933 o->invert_level = p->invert_level;
Andy Walls1a0b9d82009-09-27 18:31:37 -0300934
935 o->interrupt_enable = p->interrupt_enable;
936 o->enable = p->enable;
937 if (p->enable) {
Andy Walls1a0b9d82009-09-27 18:31:37 -0300938 if (p->interrupt_enable)
939 irqenable_tx(dev, IRQEN_TSE);
940 control_tx_enable(dev, p->enable);
941 }
942
943 mutex_unlock(&state->tx_params_lock);
944 return 0;
945}
946
947
948/*
949 * V4L2 Subdevice Core Ops
950 */
Andy Walls29f8a0a2009-09-26 23:17:30 -0300951static int cx23888_ir_log_status(struct v4l2_subdev *sd)
952{
953 struct cx23888_ir_state *state = to_state(sd);
954 struct cx23885_dev *dev = state->dev;
Andy Walls1a0b9d82009-09-27 18:31:37 -0300955 char *s;
956 int i, j;
957
958 u32 cntrl = cx23888_ir_read4(dev, CX23888_IR_CNTRL_REG);
959 u32 txclk = cx23888_ir_read4(dev, CX23888_IR_TXCLK_REG) & TXCLK_TCD;
960 u32 rxclk = cx23888_ir_read4(dev, CX23888_IR_RXCLK_REG) & RXCLK_RCD;
961 u32 cduty = cx23888_ir_read4(dev, CX23888_IR_CDUTY_REG) & CDUTY_CDC;
962 u32 stats = cx23888_ir_read4(dev, CX23888_IR_STATS_REG);
963 u32 irqen = cx23888_ir_read4(dev, CX23888_IR_IRQEN_REG);
964 u32 filtr = cx23888_ir_read4(dev, CX23888_IR_FILTR_REG) & FILTR_LPF;
965
966 v4l2_info(sd, "IR Receiver:\n");
967 v4l2_info(sd, "\tEnabled: %s\n",
968 cntrl & CNTRL_RXE ? "yes" : "no");
969 v4l2_info(sd, "\tDemodulation from a carrier: %s\n",
970 cntrl & CNTRL_DMD ? "enabled" : "disabled");
971 v4l2_info(sd, "\tFIFO: %s\n",
972 cntrl & CNTRL_RFE ? "enabled" : "disabled");
973 switch (cntrl & CNTRL_EDG) {
974 case CNTRL_EDG_NONE:
975 s = "disabled";
976 break;
977 case CNTRL_EDG_FALL:
978 s = "falling edge";
979 break;
980 case CNTRL_EDG_RISE:
981 s = "rising edge";
982 break;
983 case CNTRL_EDG_BOTH:
984 s = "rising & falling edges";
985 break;
986 default:
987 s = "??? edge";
988 break;
989 }
990 v4l2_info(sd, "\tPulse timers' start/stop trigger: %s\n", s);
991 v4l2_info(sd, "\tFIFO data on pulse timer overflow: %s\n",
992 cntrl & CNTRL_R ? "not loaded" : "overflow marker");
993 v4l2_info(sd, "\tFIFO interrupt watermark: %s\n",
994 cntrl & CNTRL_RIC ? "not empty" : "half full or greater");
995 v4l2_info(sd, "\tLoopback mode: %s\n",
996 cntrl & CNTRL_LBM ? "loopback active" : "normal receive");
997 if (cntrl & CNTRL_DMD) {
998 v4l2_info(sd, "\tExpected carrier (16 clocks): %u Hz\n",
999 clock_divider_to_carrier_freq(rxclk));
1000 switch (cntrl & CNTRL_WIN) {
1001 case CNTRL_WIN_3_3:
1002 i = 3;
1003 j = 3;
1004 break;
1005 case CNTRL_WIN_4_3:
1006 i = 4;
1007 j = 3;
1008 break;
1009 case CNTRL_WIN_3_4:
1010 i = 3;
1011 j = 4;
1012 break;
1013 case CNTRL_WIN_4_4:
1014 i = 4;
1015 j = 4;
1016 break;
1017 default:
1018 i = 0;
1019 j = 0;
1020 break;
1021 }
1022 v4l2_info(sd, "\tNext carrier edge window: 16 clocks "
1023 "-%1d/+%1d, %u to %u Hz\n", i, j,
1024 clock_divider_to_freq(rxclk, 16 + j),
1025 clock_divider_to_freq(rxclk, 16 - i));
Andy Walls1a0b9d82009-09-27 18:31:37 -03001026 }
Andy Wallsceb152a2010-07-31 21:57:42 -03001027 v4l2_info(sd, "\tMax measurable pulse width: %u us, %llu ns\n",
1028 pulse_width_count_to_us(FIFO_RXTX, rxclk),
1029 pulse_width_count_to_ns(FIFO_RXTX, rxclk));
Andy Walls1a0b9d82009-09-27 18:31:37 -03001030 v4l2_info(sd, "\tLow pass filter: %s\n",
1031 filtr ? "enabled" : "disabled");
1032 if (filtr)
1033 v4l2_info(sd, "\tMin acceptable pulse width (LPF): %u us, "
1034 "%u ns\n",
1035 lpf_count_to_us(filtr),
1036 lpf_count_to_ns(filtr));
1037 v4l2_info(sd, "\tPulse width timer timed-out: %s\n",
1038 stats & STATS_RTO ? "yes" : "no");
1039 v4l2_info(sd, "\tPulse width timer time-out intr: %s\n",
1040 irqen & IRQEN_RTE ? "enabled" : "disabled");
1041 v4l2_info(sd, "\tFIFO overrun: %s\n",
1042 stats & STATS_ROR ? "yes" : "no");
1043 v4l2_info(sd, "\tFIFO overrun interrupt: %s\n",
1044 irqen & IRQEN_ROE ? "enabled" : "disabled");
1045 v4l2_info(sd, "\tBusy: %s\n",
1046 stats & STATS_RBY ? "yes" : "no");
1047 v4l2_info(sd, "\tFIFO service requested: %s\n",
1048 stats & STATS_RSR ? "yes" : "no");
1049 v4l2_info(sd, "\tFIFO service request interrupt: %s\n",
1050 irqen & IRQEN_RSE ? "enabled" : "disabled");
1051
1052 v4l2_info(sd, "IR Transmitter:\n");
1053 v4l2_info(sd, "\tEnabled: %s\n",
1054 cntrl & CNTRL_TXE ? "yes" : "no");
1055 v4l2_info(sd, "\tModulation onto a carrier: %s\n",
1056 cntrl & CNTRL_MOD ? "enabled" : "disabled");
1057 v4l2_info(sd, "\tFIFO: %s\n",
1058 cntrl & CNTRL_TFE ? "enabled" : "disabled");
1059 v4l2_info(sd, "\tFIFO interrupt watermark: %s\n",
1060 cntrl & CNTRL_TIC ? "not empty" : "half full or less");
Andy Walls5a28d9a2010-07-18 19:57:25 -03001061 v4l2_info(sd, "\tOutput pin level inversion %s\n",
1062 cntrl & CNTRL_IVO ? "yes" : "no");
1063 v4l2_info(sd, "\tCarrier polarity: %s\n",
1064 cntrl & CNTRL_CPL ? "space:burst mark:noburst"
1065 : "space:noburst mark:burst");
Andy Walls1a0b9d82009-09-27 18:31:37 -03001066 if (cntrl & CNTRL_MOD) {
1067 v4l2_info(sd, "\tCarrier (16 clocks): %u Hz\n",
1068 clock_divider_to_carrier_freq(txclk));
1069 v4l2_info(sd, "\tCarrier duty cycle: %2u/16\n",
1070 cduty + 1);
Andy Walls1a0b9d82009-09-27 18:31:37 -03001071 }
Andy Wallsceb152a2010-07-31 21:57:42 -03001072 v4l2_info(sd, "\tMax pulse width: %u us, %llu ns\n",
1073 pulse_width_count_to_us(FIFO_RXTX, txclk),
1074 pulse_width_count_to_ns(FIFO_RXTX, txclk));
Andy Walls1a0b9d82009-09-27 18:31:37 -03001075 v4l2_info(sd, "\tBusy: %s\n",
1076 stats & STATS_TBY ? "yes" : "no");
1077 v4l2_info(sd, "\tFIFO service requested: %s\n",
1078 stats & STATS_TSR ? "yes" : "no");
1079 v4l2_info(sd, "\tFIFO service request interrupt: %s\n",
1080 irqen & IRQEN_TSE ? "enabled" : "disabled");
1081
Andy Walls29f8a0a2009-09-26 23:17:30 -03001082 return 0;
1083}
1084
1085static inline int cx23888_ir_dbg_match(const struct v4l2_dbg_match *match)
1086{
1087 return match->type == V4L2_CHIP_MATCH_HOST && match->addr == 2;
1088}
1089
1090static int cx23888_ir_g_chip_ident(struct v4l2_subdev *sd,
1091 struct v4l2_dbg_chip_ident *chip)
1092{
1093 struct cx23888_ir_state *state = to_state(sd);
1094
1095 if (cx23888_ir_dbg_match(&chip->match)) {
1096 chip->ident = state->id;
1097 chip->revision = state->rev;
1098 }
1099 return 0;
1100}
1101
1102#ifdef CONFIG_VIDEO_ADV_DEBUG
1103static int cx23888_ir_g_register(struct v4l2_subdev *sd,
1104 struct v4l2_dbg_register *reg)
1105{
1106 struct cx23888_ir_state *state = to_state(sd);
1107 u32 addr = CX23888_IR_REG_BASE + (u32) reg->reg;
1108
1109 if (!cx23888_ir_dbg_match(&reg->match))
1110 return -EINVAL;
1111 if ((addr & 0x3) != 0)
1112 return -EINVAL;
1113 if (addr < CX23888_IR_CNTRL_REG || addr > CX23888_IR_LEARN_REG)
1114 return -EINVAL;
1115 if (!capable(CAP_SYS_ADMIN))
1116 return -EPERM;
1117 reg->size = 4;
1118 reg->val = cx23888_ir_read4(state->dev, addr);
1119 return 0;
1120}
1121
1122static int cx23888_ir_s_register(struct v4l2_subdev *sd,
1123 struct v4l2_dbg_register *reg)
1124{
1125 struct cx23888_ir_state *state = to_state(sd);
1126 u32 addr = CX23888_IR_REG_BASE + (u32) reg->reg;
1127
1128 if (!cx23888_ir_dbg_match(&reg->match))
1129 return -EINVAL;
1130 if ((addr & 0x3) != 0)
1131 return -EINVAL;
1132 if (addr < CX23888_IR_CNTRL_REG || addr > CX23888_IR_LEARN_REG)
1133 return -EINVAL;
1134 if (!capable(CAP_SYS_ADMIN))
1135 return -EPERM;
1136 cx23888_ir_write4(state->dev, addr, reg->val);
1137 return 0;
1138}
1139#endif
1140
1141static const struct v4l2_subdev_core_ops cx23888_ir_core_ops = {
1142 .g_chip_ident = cx23888_ir_g_chip_ident,
1143 .log_status = cx23888_ir_log_status,
1144#ifdef CONFIG_VIDEO_ADV_DEBUG
1145 .g_register = cx23888_ir_g_register,
1146 .s_register = cx23888_ir_s_register,
1147#endif
Andy Walls260e689b2010-07-18 20:54:52 -03001148 .interrupt_service_routine = cx23888_ir_irq_handler,
Andy Walls29f8a0a2009-09-26 23:17:30 -03001149};
1150
Andy Walls1a0b9d82009-09-27 18:31:37 -03001151static const struct v4l2_subdev_ir_ops cx23888_ir_ir_ops = {
Andy Walls1a0b9d82009-09-27 18:31:37 -03001152 .rx_read = cx23888_ir_rx_read,
1153 .rx_g_parameters = cx23888_ir_rx_g_parameters,
1154 .rx_s_parameters = cx23888_ir_rx_s_parameters,
1155
1156 .tx_write = cx23888_ir_tx_write,
1157 .tx_g_parameters = cx23888_ir_tx_g_parameters,
1158 .tx_s_parameters = cx23888_ir_tx_s_parameters,
1159};
1160
Andy Walls29f8a0a2009-09-26 23:17:30 -03001161static const struct v4l2_subdev_ops cx23888_ir_controller_ops = {
1162 .core = &cx23888_ir_core_ops,
Andy Walls1a0b9d82009-09-27 18:31:37 -03001163 .ir = &cx23888_ir_ir_ops,
1164};
1165
1166static const struct v4l2_subdev_ir_parameters default_rx_params = {
Andy Wallsc02e0d12010-08-01 02:18:13 -03001167 .bytes_per_data_element = sizeof(union cx23888_ir_fifo_rec),
Andy Walls1a0b9d82009-09-27 18:31:37 -03001168 .mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH,
1169
1170 .enable = false,
1171 .interrupt_enable = false,
1172 .shutdown = true,
1173
1174 .modulation = true,
1175 .carrier_freq = 36000, /* 36 kHz - RC-5, RC-6, and RC-6A carrier */
1176
1177 /* RC-5: 666,667 ns = 1/36 kHz * 32 cycles * 1 mark * 0.75 */
1178 /* RC-6A: 333,333 ns = 1/36 kHz * 16 cycles * 1 mark * 0.75 */
1179 .noise_filter_min_width = 333333, /* ns */
1180 .carrier_range_lower = 35000,
1181 .carrier_range_upper = 37000,
Andy Walls5a28d9a2010-07-18 19:57:25 -03001182 .invert_level = false,
Andy Walls1a0b9d82009-09-27 18:31:37 -03001183};
1184
1185static const struct v4l2_subdev_ir_parameters default_tx_params = {
Andy Wallsc02e0d12010-08-01 02:18:13 -03001186 .bytes_per_data_element = sizeof(union cx23888_ir_fifo_rec),
Andy Walls1a0b9d82009-09-27 18:31:37 -03001187 .mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH,
1188
1189 .enable = false,
1190 .interrupt_enable = false,
1191 .shutdown = true,
1192
1193 .modulation = true,
1194 .carrier_freq = 36000, /* 36 kHz - RC-5 carrier */
1195 .duty_cycle = 25, /* 25 % - RC-5 carrier */
Andy Walls5a28d9a2010-07-18 19:57:25 -03001196 .invert_level = false,
1197 .invert_carrier_sense = false,
Andy Walls29f8a0a2009-09-26 23:17:30 -03001198};
1199
1200int cx23888_ir_probe(struct cx23885_dev *dev)
1201{
1202 struct cx23888_ir_state *state;
1203 struct v4l2_subdev *sd;
Andy Walls1a0b9d82009-09-27 18:31:37 -03001204 struct v4l2_subdev_ir_parameters default_params;
1205 int ret;
Andy Walls29f8a0a2009-09-26 23:17:30 -03001206
1207 state = kzalloc(sizeof(struct cx23888_ir_state), GFP_KERNEL);
1208 if (state == NULL)
1209 return -ENOMEM;
1210
Andy Walls1a0b9d82009-09-27 18:31:37 -03001211 spin_lock_init(&state->rx_kfifo_lock);
Stefani Seibold7801edb2009-12-21 14:37:33 -08001212 if (kfifo_alloc(&state->rx_kfifo, CX23888_IR_RX_KFIFO_SIZE, GFP_KERNEL))
Andy Walls1a0b9d82009-09-27 18:31:37 -03001213 return -ENOMEM;
1214
Andy Walls29f8a0a2009-09-26 23:17:30 -03001215 state->dev = dev;
1216 state->id = V4L2_IDENT_CX23888_IR;
1217 state->rev = 0;
1218 sd = &state->sd;
1219
1220 v4l2_subdev_init(sd, &cx23888_ir_controller_ops);
1221 v4l2_set_subdevdata(sd, state);
1222 /* FIXME - fix the formatting of dev->v4l2_dev.name and use it */
1223 snprintf(sd->name, sizeof(sd->name), "%s/888-ir", dev->name);
1224 sd->grp_id = CX23885_HW_888_IR;
Andy Walls1a0b9d82009-09-27 18:31:37 -03001225
1226 ret = v4l2_device_register_subdev(&dev->v4l2_dev, sd);
1227 if (ret == 0) {
1228 /*
1229 * Ensure no interrupts arrive from '888 specific conditions,
1230 * since we ignore them in this driver to have commonality with
1231 * similar IR controller cores.
1232 */
1233 cx23888_ir_write4(dev, CX23888_IR_IRQEN_REG, 0);
1234
1235 mutex_init(&state->rx_params_lock);
1236 memcpy(&default_params, &default_rx_params,
1237 sizeof(struct v4l2_subdev_ir_parameters));
1238 v4l2_subdev_call(sd, ir, rx_s_parameters, &default_params);
1239
1240 mutex_init(&state->tx_params_lock);
1241 memcpy(&default_params, &default_tx_params,
1242 sizeof(struct v4l2_subdev_ir_parameters));
1243 v4l2_subdev_call(sd, ir, tx_s_parameters, &default_params);
1244 } else {
Stefani Seibold7801edb2009-12-21 14:37:33 -08001245 kfifo_free(&state->rx_kfifo);
Andy Walls1a0b9d82009-09-27 18:31:37 -03001246 }
1247 return ret;
Andy Walls29f8a0a2009-09-26 23:17:30 -03001248}
1249
1250int cx23888_ir_remove(struct cx23885_dev *dev)
1251{
1252 struct v4l2_subdev *sd;
1253 struct cx23888_ir_state *state;
1254
1255 sd = cx23885_find_hw(dev, CX23885_HW_888_IR);
1256 if (sd == NULL)
1257 return -ENODEV;
1258
Andy Walls1a0b9d82009-09-27 18:31:37 -03001259 cx23888_ir_rx_shutdown(sd);
1260 cx23888_ir_tx_shutdown(sd);
Andy Walls29f8a0a2009-09-26 23:17:30 -03001261
1262 state = to_state(sd);
1263 v4l2_device_unregister_subdev(sd);
Stefani Seibold7801edb2009-12-21 14:37:33 -08001264 kfifo_free(&state->rx_kfifo);
Andy Walls29f8a0a2009-09-26 23:17:30 -03001265 kfree(state);
1266 /* Nothing more to free() as state held the actual v4l2_subdev object */
1267 return 0;
1268}