25 #include <linux/slab.h>
33 static unsigned int ir_888_debug;
35 MODULE_PARM_DESC(ir_888_debug,
"enable debug messages [CX23888 IR controller]");
37 #define CX23888_IR_REG_BASE 0x170000
42 #define CX23888_IR_CNTRL_REG 0x170000
43 #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
65 #define CNTRL_IVO 0x00008000
67 #define CX23888_IR_TXCLK_REG 0x170004
68 #define TXCLK_TCD 0x0000FFFF
70 #define CX23888_IR_RXCLK_REG 0x170008
71 #define RXCLK_RCD 0x0000FFFF
73 #define CX23888_IR_CDUTY_REG 0x17000C
74 #define CDUTY_CDC 0x0000000F
76 #define CX23888_IR_STATS_REG 0x170010
77 #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
84 #define CX23888_IR_IRQEN_REG 0x170014
85 #define IRQEN_RTE 0x00000001
86 #define IRQEN_ROE 0x00000002
87 #define IRQEN_RSE 0x00000010
88 #define IRQEN_TSE 0x00000020
90 #define CX23888_IR_FILTR_REG 0x170018
91 #define FILTR_LPF 0x0000FFFF
94 #define CX23888_IR_FIFO_REG 0x170040
95 #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
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
114 #define CX23888_VIDCLK_FREQ 108000000
115 #define CX23888_IR_REFCLK_FREQ (CX23888_VIDCLK_FREQ / 2)
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))
151 return v4l2_get_subdevdata(sd);
170 u32 and_mask,
u32 or_value)
172 cx_andor(addr, ~and_mask, or_value);
183 static inline u16 count_to_clock_divider(
unsigned int d)
194 static inline u16 ns_to_clock_divider(
unsigned int ns)
196 return count_to_clock_divider(
200 static inline unsigned int clock_divider_to_ns(
unsigned int divider)
207 static inline u16 carrier_freq_to_clock_divider(
unsigned int freq)
209 return count_to_clock_divider(
213 static inline unsigned int clock_divider_to_carrier_freq(
unsigned int divider)
218 static inline u16 freq_to_clock_divider(
unsigned int freq,
219 unsigned int rollovers)
221 return count_to_clock_divider(
225 static inline unsigned int clock_divider_to_freq(
unsigned int divider,
226 unsigned int rollovers)
229 (divider + 1) * rollovers);
239 static inline u16 count_to_lpf_count(
unsigned int d)
248 static inline u16 ns_to_lpf_count(
unsigned int ns)
250 return count_to_lpf_count(
254 static inline unsigned int lpf_count_to_ns(
unsigned int count)
261 static inline unsigned int lpf_count_to_us(
unsigned int count)
270 static u32 clock_divider_to_resolution(
u16 divider)
290 n = (((
u64) count << 2) | 0x3) * (divider + 1) * 1000;
297 static unsigned int pulse_width_count_to_us(
u16 count,
u16 divider)
306 n = (((
u64) count << 2) | 0x3) * (divider + 1);
310 return (
unsigned int)
n;
321 static u64 ns_to_pulse_clocks(
u32 ns)
326 rem =
do_div(clocks, 1000);
332 static u16 pulse_clocks_to_clock_divider(
u64 count)
359 static inline void control_tx_irq_watermark(
struct cx23885_dev *
dev,
365 static inline void control_rx_irq_watermark(
struct cx23885_dev *
dev,
377 static inline void control_rx_enable(
struct cx23885_dev *dev,
bool enable)
383 static inline void control_tx_modulation_enable(
struct cx23885_dev *dev,
390 static inline void control_rx_demodulation_enable(
struct cx23885_dev *dev,
397 static inline void control_rx_s_edge_detection(
struct cx23885_dev *dev,
404 static void control_rx_s_carrier_window(
struct cx23885_dev *dev,
405 unsigned int carrier,
406 unsigned int *carrier_range_low,
407 unsigned int *carrier_range_high)
410 unsigned int c16 = carrier * 16;
430 static inline void control_tx_polarity_invert(
struct cx23885_dev *dev,
437 static inline void control_tx_level_invert(
struct cx23885_dev *dev,
447 static unsigned int txclk_tx_s_carrier(
struct cx23885_dev *dev,
451 *divider = carrier_freq_to_clock_divider(freq);
453 return clock_divider_to_carrier_freq(*divider);
456 static unsigned int rxclk_rx_s_carrier(
struct cx23885_dev *dev,
460 *divider = carrier_freq_to_clock_divider(freq);
462 return clock_divider_to_carrier_freq(*divider);
472 pulse_clocks = ns_to_pulse_clocks(ns);
473 *divider = pulse_clocks_to_clock_divider(pulse_clocks);
475 return (
u32) pulse_width_count_to_ns(
FIFO_RXTX, *divider);
485 pulse_clocks = ns_to_pulse_clocks(ns);
486 *divider = pulse_clocks_to_clock_divider(pulse_clocks);
488 return (
u32) pulse_width_count_to_ns(
FIFO_RXTX, *divider);
494 static unsigned int cduty_tx_s_duty_cycle(
struct cx23885_dev *dev,
495 unsigned int duty_cycle)
512 u32 count = ns_to_lpf_count(min_width_ns);
514 return lpf_count_to_ns(count);
527 static inline void irqenable_tx(
struct cx23885_dev *dev,
u32 mask)
548 unsigned int i,
j,
k;
550 int tsr, rsr, rto, ror, tse,
rse, rte, roe, kror;
563 v4l2_dbg(2, ir_888_debug, sd,
"IRQ Status: %s %s %s %s %s %s\n",
564 tsr ?
"tsr" :
" ", rsr ?
"rsr" :
" ",
565 rto ?
"rto" :
" ", ror ?
"ror" :
" ",
569 v4l2_dbg(2, ir_888_debug, sd,
"IRQ Enables: %s %s %s %s\n",
570 tse ?
"tse" :
" ", rse ?
"rse" :
" ",
571 rte ?
"rte" :
" ", roe ?
"roe" :
" ");
588 irqenable_tx(dev, 0);
598 if ((rse && rsr) || (rte && rto)) {
616 (
unsigned char *) rx_data, j,
628 v4l2_err(sd,
"IR receiver software FIFO overrun\n");
637 v4l2_err(sd,
"IR receiver hardware FIFO overrun\n");
665 static int cx23888_ir_rx_read(
struct v4l2_subdev *sd,
u8 *
buf,
size_t count,
701 v = (unsigned) pulse_width_count_to_ns(
711 v4l2_dbg(2, ir_888_debug, sd,
"rx read: %10u ns %s %s\n",
712 v, u ?
"mark" :
"space", w ?
"(timed out)" :
"");
714 v4l2_dbg(2, ir_888_debug, sd,
"rx read: end of rx\n");
719 static int cx23888_ir_rx_g_parameters(
struct v4l2_subdev *sd,
729 static int cx23888_ir_rx_shutdown(
struct v4l2_subdev *sd)
737 irqenable_rx(dev, 0);
738 control_rx_enable(dev,
false);
739 control_rx_demodulation_enable(dev,
false);
741 filter_rx_s_min_width(dev, 0);
750 static int cx23888_ir_rx_s_parameters(
struct v4l2_subdev *sd,
759 return cx23888_ir_rx_shutdown(sd);
774 irqenable_rx(dev, 0);
775 control_rx_enable(dev,
false);
777 control_rx_demodulation_enable(dev, p->
modulation);
795 (
u32) pulse_width_count_to_ns(
FIFO_RXTX, rxclk_divider);
808 p->
resolution = clock_divider_to_resolution(rxclk_divider);
829 irqenable_rx(dev, IRQEN_RSE | IRQEN_RTE | IRQEN_ROE);
830 control_rx_enable(dev, p->
enable);
838 static int cx23888_ir_tx_write(
struct v4l2_subdev *sd,
u8 *buf,
size_t count,
844 irqenable_tx(dev, IRQEN_TSE);
849 static int cx23888_ir_tx_g_parameters(
struct v4l2_subdev *sd,
859 static int cx23888_ir_tx_shutdown(
struct v4l2_subdev *sd)
867 irqenable_tx(dev, 0);
868 control_tx_enable(dev,
false);
869 control_tx_modulation_enable(dev,
false);
878 static int cx23888_ir_tx_s_parameters(
struct v4l2_subdev *sd,
887 return cx23888_ir_tx_shutdown(sd);
902 irqenable_tx(dev, 0);
903 control_tx_enable(dev,
false);
905 control_tx_modulation_enable(dev, p->
modulation);
917 (
u32) pulse_width_count_to_ns(
FIFO_RXTX, txclk_divider);
926 p->
resolution = clock_divider_to_resolution(txclk_divider);
942 irqenable_tx(dev, IRQEN_TSE);
943 control_tx_enable(dev, p->
enable);
954 static int cx23888_ir_log_status(
struct v4l2_subdev *sd)
972 v4l2_info(sd,
"\tDemodulation from a carrier: %s\n",
973 cntrl &
CNTRL_DMD ?
"enabled" :
"disabled");
975 cntrl &
CNTRL_RFE ?
"enabled" :
"disabled");
987 s =
"rising & falling edges";
993 v4l2_info(sd,
"\tPulse timers' start/stop trigger: %s\n", s);
994 v4l2_info(sd,
"\tFIFO data on pulse timer overflow: %s\n",
995 cntrl &
CNTRL_R ?
"not loaded" :
"overflow marker");
996 v4l2_info(sd,
"\tFIFO interrupt watermark: %s\n",
997 cntrl &
CNTRL_RIC ?
"not empty" :
"half full or greater");
999 cntrl &
CNTRL_LBM ?
"loopback active" :
"normal receive");
1001 v4l2_info(sd,
"\tExpected carrier (16 clocks): %u Hz\n",
1002 clock_divider_to_carrier_freq(rxclk));
1025 v4l2_info(sd,
"\tNext carrier edge window: 16 clocks "
1026 "-%1d/+%1d, %u to %u Hz\n", i, j,
1027 clock_divider_to_freq(rxclk, 16 + j),
1028 clock_divider_to_freq(rxclk, 16 - i));
1030 v4l2_info(sd,
"\tMax measurable pulse width: %u us, %llu ns\n",
1031 pulse_width_count_to_us(
FIFO_RXTX, rxclk),
1032 pulse_width_count_to_ns(
FIFO_RXTX, rxclk));
1033 v4l2_info(sd,
"\tLow pass filter: %s\n",
1034 filtr ?
"enabled" :
"disabled");
1036 v4l2_info(sd,
"\tMin acceptable pulse width (LPF): %u us, "
1038 lpf_count_to_us(filtr),
1039 lpf_count_to_ns(filtr));
1040 v4l2_info(sd,
"\tPulse width timer timed-out: %s\n",
1041 stats & STATS_RTO ?
"yes" :
"no");
1042 v4l2_info(sd,
"\tPulse width timer time-out intr: %s\n",
1043 irqen & IRQEN_RTE ?
"enabled" :
"disabled");
1045 stats & STATS_ROR ?
"yes" :
"no");
1046 v4l2_info(sd,
"\tFIFO overrun interrupt: %s\n",
1047 irqen & IRQEN_ROE ?
"enabled" :
"disabled");
1050 v4l2_info(sd,
"\tFIFO service requested: %s\n",
1051 stats & STATS_RSR ?
"yes" :
"no");
1052 v4l2_info(sd,
"\tFIFO service request interrupt: %s\n",
1053 irqen & IRQEN_RSE ?
"enabled" :
"disabled");
1058 v4l2_info(sd,
"\tModulation onto a carrier: %s\n",
1059 cntrl &
CNTRL_MOD ?
"enabled" :
"disabled");
1061 cntrl &
CNTRL_TFE ?
"enabled" :
"disabled");
1062 v4l2_info(sd,
"\tFIFO interrupt watermark: %s\n",
1063 cntrl &
CNTRL_TIC ?
"not empty" :
"half full or less");
1064 v4l2_info(sd,
"\tOutput pin level inversion %s\n",
1066 v4l2_info(sd,
"\tCarrier polarity: %s\n",
1067 cntrl &
CNTRL_CPL ?
"space:burst mark:noburst"
1068 :
"space:noburst mark:burst");
1070 v4l2_info(sd,
"\tCarrier (16 clocks): %u Hz\n",
1071 clock_divider_to_carrier_freq(txclk));
1072 v4l2_info(sd,
"\tCarrier duty cycle: %2u/16\n",
1075 v4l2_info(sd,
"\tMax pulse width: %u us, %llu ns\n",
1076 pulse_width_count_to_us(
FIFO_RXTX, txclk),
1077 pulse_width_count_to_ns(
FIFO_RXTX, txclk));
1080 v4l2_info(sd,
"\tFIFO service requested: %s\n",
1081 stats & STATS_TSR ?
"yes" :
"no");
1082 v4l2_info(sd,
"\tFIFO service request interrupt: %s\n",
1083 irqen & IRQEN_TSE ?
"enabled" :
"disabled");
1093 static int cx23888_ir_g_chip_ident(
struct v4l2_subdev *sd,
1098 if (cx23888_ir_dbg_match(&chip->
match)) {
1105 #ifdef CONFIG_VIDEO_ADV_DEBUG
1106 static int cx23888_ir_g_register(
struct v4l2_subdev *sd,
1112 if (!cx23888_ir_dbg_match(®->
match))
1114 if ((addr & 0x3) != 0)
1121 reg->
val = cx23888_ir_read4(state->
dev, addr);
1125 static int cx23888_ir_s_register(
struct v4l2_subdev *sd,
1131 if (!cx23888_ir_dbg_match(®->
match))
1133 if ((addr & 0x3) != 0)
1135 if (addr < CX23888_IR_CNTRL_REG || addr > CX23888_IR_LEARN_REG)
1139 cx23888_ir_write4(state->
dev, addr, reg->
val);
1145 .g_chip_ident = cx23888_ir_g_chip_ident,
1146 .log_status = cx23888_ir_log_status,
1147 #ifdef CONFIG_VIDEO_ADV_DEBUG
1148 .g_register = cx23888_ir_g_register,
1149 .s_register = cx23888_ir_s_register,
1151 .interrupt_service_routine = cx23888_ir_irq_handler,
1155 .rx_read = cx23888_ir_rx_read,
1156 .rx_g_parameters = cx23888_ir_rx_g_parameters,
1157 .rx_s_parameters = cx23888_ir_rx_s_parameters,
1159 .tx_write = cx23888_ir_tx_write,
1160 .tx_g_parameters = cx23888_ir_tx_g_parameters,
1161 .tx_s_parameters = cx23888_ir_tx_s_parameters,
1165 .core = &cx23888_ir_core_ops,
1166 .ir = &cx23888_ir_ir_ops,
1174 .interrupt_enable =
false,
1178 .carrier_freq = 36000,
1182 .noise_filter_min_width = 333333,
1183 .carrier_range_lower = 35000,
1184 .carrier_range_upper = 37000,
1185 .invert_level =
false,
1193 .interrupt_enable =
false,
1197 .carrier_freq = 36000,
1199 .invert_level =
false,
1200 .invert_carrier_sense =
false,
1224 v4l2_set_subdevdata(sd, state);
1239 memcpy(&default_params, &default_rx_params,
1244 memcpy(&default_params, &default_tx_params,
1262 cx23888_ir_rx_shutdown(sd);
1263 cx23888_ir_tx_shutdown(sd);
1265 state = to_state(sd);