24 #include <linux/slab.h>
26 #include <linux/module.h>
32 static unsigned int ir_debug;
36 #define CX25840_IR_REG_BASE 0x200
38 #define CX25840_IR_CNTRL_REG 0x200
39 #define CNTRL_WIN_3_3 0x00000000
40 #define CNTRL_WIN_4_3 0x00000001
41 #define CNTRL_WIN_3_4 0x00000002
42 #define CNTRL_WIN_4_4 0x00000003
43 #define CNTRL_WIN 0x00000003
44 #define CNTRL_EDG_NONE 0x00000000
45 #define CNTRL_EDG_FALL 0x00000004
46 #define CNTRL_EDG_RISE 0x00000008
47 #define CNTRL_EDG_BOTH 0x0000000C
48 #define CNTRL_EDG 0x0000000C
49 #define CNTRL_DMD 0x00000010
50 #define CNTRL_MOD 0x00000020
51 #define CNTRL_RFE 0x00000040
52 #define CNTRL_TFE 0x00000080
53 #define CNTRL_RXE 0x00000100
54 #define CNTRL_TXE 0x00000200
55 #define CNTRL_RIC 0x00000400
56 #define CNTRL_TIC 0x00000800
57 #define CNTRL_CPL 0x00001000
58 #define CNTRL_LBM 0x00002000
59 #define CNTRL_R 0x00004000
61 #define CX25840_IR_TXCLK_REG 0x204
62 #define TXCLK_TCD 0x0000FFFF
64 #define CX25840_IR_RXCLK_REG 0x208
65 #define RXCLK_RCD 0x0000FFFF
67 #define CX25840_IR_CDUTY_REG 0x20C
68 #define CDUTY_CDC 0x0000000F
70 #define CX25840_IR_STATS_REG 0x210
71 #define STATS_RTO 0x00000001
72 #define STATS_ROR 0x00000002
73 #define STATS_RBY 0x00000004
74 #define STATS_TBY 0x00000008
75 #define STATS_RSR 0x00000010
76 #define STATS_TSR 0x00000020
78 #define CX25840_IR_IRQEN_REG 0x214
79 #define IRQEN_RTE 0x00000001
80 #define IRQEN_ROE 0x00000002
81 #define IRQEN_RSE 0x00000010
82 #define IRQEN_TSE 0x00000020
83 #define IRQEN_MSK 0x00000033
85 #define CX25840_IR_FILTR_REG 0x218
86 #define FILTR_LPF 0x0000FFFF
88 #define CX25840_IR_FIFO_REG 0x23C
89 #define FIFO_RXTX 0x0000FFFF
90 #define FIFO_RXTX_LVL 0x00010000
91 #define FIFO_RXTX_RTO 0x0001FFFF
92 #define FIFO_RX_NDV 0x00020000
93 #define FIFO_RX_DEPTH 8
94 #define FIFO_TX_DEPTH 8
96 #define CX25840_VIDCLK_FREQ 108000000
97 #define CX25840_IR_REFCLK_FREQ (CX25840_VIDCLK_FREQ / 2)
109 #define CX25840_IR_RX_KFIFO_SIZE (256 * sizeof(union cx25840_ir_fifo_rec))
110 #define CX25840_IR_TX_KFIFO_SIZE (256 * sizeof(union cx25840_ir_fifo_rec))
142 static inline u16 count_to_clock_divider(
unsigned int d)
153 static inline u16 ns_to_clock_divider(
unsigned int ns)
155 return count_to_clock_divider(
159 static inline unsigned int clock_divider_to_ns(
unsigned int divider)
166 static inline u16 carrier_freq_to_clock_divider(
unsigned int freq)
168 return count_to_clock_divider(
172 static inline unsigned int clock_divider_to_carrier_freq(
unsigned int divider)
177 static inline u16 freq_to_clock_divider(
unsigned int freq,
178 unsigned int rollovers)
180 return count_to_clock_divider(
184 static inline unsigned int clock_divider_to_freq(
unsigned int divider,
185 unsigned int rollovers)
188 (divider + 1) * rollovers);
198 static inline u16 count_to_lpf_count(
unsigned int d)
207 static inline u16 ns_to_lpf_count(
unsigned int ns)
209 return count_to_lpf_count(
213 static inline unsigned int lpf_count_to_ns(
unsigned int count)
220 static inline unsigned int lpf_count_to_us(
unsigned int count)
229 static u32 clock_divider_to_resolution(
u16 divider)
249 n = (((
u64) count << 2) | 0x3) * (divider + 1) * 1000;
258 static u16 ns_to_pulse_width_count(
u32 ns,
u16 divider)
269 d = (1 << 2) * ((
u32) divider + 1) * 1000;
282 static unsigned int pulse_width_count_to_us(
u16 count,
u16 divider)
291 n = (((
u64) count << 2) | 0x3) * (divider + 1);
295 return (
unsigned int)
n;
306 static u64 ns_to_pulse_clocks(
u32 ns)
311 rem =
do_div(clocks, 1000);
317 static u16 pulse_clocks_to_clock_divider(
u64 count)
344 static inline void control_tx_irq_watermark(
struct i2c_client *
c,
350 static inline void control_rx_irq_watermark(
struct i2c_client *
c,
362 static inline void control_rx_enable(
struct i2c_client *c,
bool enable)
368 static inline void control_tx_modulation_enable(
struct i2c_client *c,
375 static inline void control_rx_demodulation_enable(
struct i2c_client *c,
382 static inline void control_rx_s_edge_detection(
struct i2c_client *c,
389 static void control_rx_s_carrier_window(
struct i2c_client *c,
390 unsigned int carrier,
391 unsigned int *carrier_range_low,
392 unsigned int *carrier_range_high)
395 unsigned int c16 = carrier * 16;
415 static inline void control_tx_polarity_invert(
struct i2c_client *c,
425 static unsigned int txclk_tx_s_carrier(
struct i2c_client *c,
429 *divider = carrier_freq_to_clock_divider(freq);
431 return clock_divider_to_carrier_freq(*divider);
434 static unsigned int rxclk_rx_s_carrier(
struct i2c_client *c,
438 *divider = carrier_freq_to_clock_divider(freq);
440 return clock_divider_to_carrier_freq(*divider);
450 pulse_clocks = ns_to_pulse_clocks(ns);
451 *divider = pulse_clocks_to_clock_divider(pulse_clocks);
453 return (
u32) pulse_width_count_to_ns(
FIFO_RXTX, *divider);
463 pulse_clocks = ns_to_pulse_clocks(ns);
464 *divider = pulse_clocks_to_clock_divider(pulse_clocks);
466 return (
u32) pulse_width_count_to_ns(
FIFO_RXTX, *divider);
472 static unsigned int cduty_tx_s_duty_cycle(
struct i2c_client *c,
473 unsigned int duty_cycle)
490 u32 count = ns_to_lpf_count(min_width_ns);
492 return lpf_count_to_ns(count);
502 if (is_cx23885(state) || is_cx23887(state))
513 if (is_cx23885(state) || is_cx23887(state))
530 unsigned int i,
j,
k;
532 int tsr, rsr, rto, ror, tse,
rse, rte, roe, kror;
536 if (ir_state ==
NULL)
542 if (!(is_cx23885(state) || is_cx23887(state)))
547 if (is_cx23885(state) || is_cx23887(state))
561 v4l2_dbg(2, ir_debug, sd,
"IR IRQ Status: %s %s %s %s %s %s\n",
562 tsr ?
"tsr" :
" ", rsr ?
"rsr" :
" ",
563 rto ?
"rto" :
" ", ror ?
"ror" :
" ",
567 v4l2_dbg(2, ir_debug, sd,
"IR IRQ Enables: %s %s %s %s\n",
568 tse ?
"tse" :
" ", rse ?
"rse" :
" ",
569 rte ?
"rte" :
" ", roe ?
"roe" :
" ");
596 if ((rse && rsr) || (rte && rto)) {
614 (
unsigned char *) rx_data, j,
626 v4l2_err(sd,
"IR receiver software FIFO overrun\n");
635 v4l2_err(sd,
"IR receiver hardware FIFO overrun\n");
662 static int cx25840_ir_rx_read(
struct v4l2_subdev *sd,
u8 *
buf,
size_t count,
672 if (ir_state ==
NULL)
704 v = (unsigned) pulse_width_count_to_ns(
714 v4l2_dbg(2, ir_debug, sd,
"rx read: %10u ns %s %s\n",
715 v, u ?
"mark" :
"space", w ?
"(timed out)" :
"");
717 v4l2_dbg(2, ir_debug, sd,
"rx read: end of rx\n");
722 static int cx25840_ir_rx_g_parameters(
struct v4l2_subdev *sd,
727 if (ir_state ==
NULL)
737 static int cx25840_ir_rx_shutdown(
struct v4l2_subdev *sd)
742 if (ir_state ==
NULL)
750 control_rx_enable(c,
false);
751 control_rx_demodulation_enable(c,
false);
753 filter_rx_s_min_width(c, 0);
762 static int cx25840_ir_rx_s_parameters(
struct v4l2_subdev *sd,
770 if (ir_state ==
NULL)
774 return cx25840_ir_rx_shutdown(sd);
794 control_rx_enable(c,
false);
796 control_rx_demodulation_enable(c, p->
modulation);
815 (
u32) pulse_width_count_to_ns(
FIFO_RXTX, rxclk_divider);
828 p->
resolution = clock_divider_to_resolution(rxclk_divider);
849 control_rx_enable(c, p->
enable);
857 static int cx25840_ir_tx_write(
struct v4l2_subdev *sd,
u8 *buf,
size_t count,
862 if (ir_state ==
NULL)
876 u32 *ns_pulse = (
u32 *) buf;
883 n =
min(n, (
unsigned int) count);
889 for (i = 0; i <
n; ) {
892 fifo_pulse[
j] = ns_to_pulse_width_count(
903 *num = n *
sizeof(
u32);
912 static int cx25840_ir_tx_g_parameters(
struct v4l2_subdev *sd,
917 if (ir_state ==
NULL)
927 static int cx25840_ir_tx_shutdown(
struct v4l2_subdev *sd)
932 if (ir_state ==
NULL)
940 control_tx_enable(c,
false);
941 control_tx_modulation_enable(c,
false);
950 static int cx25840_ir_tx_s_parameters(
struct v4l2_subdev *sd,
958 if (ir_state ==
NULL)
962 return cx25840_ir_tx_shutdown(sd);
981 control_tx_enable(c,
false);
983 control_tx_modulation_enable(c, p->
modulation);
995 (
u32) pulse_width_count_to_ns(
FIFO_RXTX, txclk_divider);
1004 p->
resolution = clock_divider_to_resolution(txclk_divider);
1027 control_tx_enable(c, p->
enable);
1047 if (is_cx23888(state))
1056 if (is_cx23885(state) || is_cx23887(state))
1063 v4l2_info(sd,
"\tDemodulation from a carrier: %s\n",
1064 cntrl &
CNTRL_DMD ?
"enabled" :
"disabled");
1066 cntrl &
CNTRL_RFE ?
"enabled" :
"disabled");
1078 s =
"rising & falling edges";
1084 v4l2_info(sd,
"\tPulse timers' start/stop trigger: %s\n", s);
1085 v4l2_info(sd,
"\tFIFO data on pulse timer overflow: %s\n",
1086 cntrl &
CNTRL_R ?
"not loaded" :
"overflow marker");
1087 v4l2_info(sd,
"\tFIFO interrupt watermark: %s\n",
1088 cntrl &
CNTRL_RIC ?
"not empty" :
"half full or greater");
1090 cntrl &
CNTRL_LBM ?
"loopback active" :
"normal receive");
1091 if (cntrl & CNTRL_DMD) {
1092 v4l2_info(sd,
"\tExpected carrier (16 clocks): %u Hz\n",
1093 clock_divider_to_carrier_freq(rxclk));
1116 v4l2_info(sd,
"\tNext carrier edge window: 16 clocks "
1117 "-%1d/+%1d, %u to %u Hz\n", i, j,
1118 clock_divider_to_freq(rxclk, 16 + j),
1119 clock_divider_to_freq(rxclk, 16 - i));
1121 v4l2_info(sd,
"\tMax measurable pulse width: %u us, %llu ns\n",
1122 pulse_width_count_to_us(
FIFO_RXTX, rxclk),
1123 pulse_width_count_to_ns(
FIFO_RXTX, rxclk));
1124 v4l2_info(sd,
"\tLow pass filter: %s\n",
1125 filtr ?
"enabled" :
"disabled");
1127 v4l2_info(sd,
"\tMin acceptable pulse width (LPF): %u us, "
1129 lpf_count_to_us(filtr),
1130 lpf_count_to_ns(filtr));
1131 v4l2_info(sd,
"\tPulse width timer timed-out: %s\n",
1133 v4l2_info(sd,
"\tPulse width timer time-out intr: %s\n",
1134 irqen &
IRQEN_RTE ?
"enabled" :
"disabled");
1137 v4l2_info(sd,
"\tFIFO overrun interrupt: %s\n",
1138 irqen &
IRQEN_ROE ?
"enabled" :
"disabled");
1141 v4l2_info(sd,
"\tFIFO service requested: %s\n",
1143 v4l2_info(sd,
"\tFIFO service request interrupt: %s\n",
1144 irqen &
IRQEN_RSE ?
"enabled" :
"disabled");
1149 v4l2_info(sd,
"\tModulation onto a carrier: %s\n",
1150 cntrl &
CNTRL_MOD ?
"enabled" :
"disabled");
1152 cntrl &
CNTRL_TFE ?
"enabled" :
"disabled");
1153 v4l2_info(sd,
"\tFIFO interrupt watermark: %s\n",
1154 cntrl &
CNTRL_TIC ?
"not empty" :
"half full or less");
1155 v4l2_info(sd,
"\tCarrier polarity: %s\n",
1156 cntrl &
CNTRL_CPL ?
"space:burst mark:noburst"
1157 :
"space:noburst mark:burst");
1158 if (cntrl & CNTRL_MOD) {
1159 v4l2_info(sd,
"\tCarrier (16 clocks): %u Hz\n",
1160 clock_divider_to_carrier_freq(txclk));
1161 v4l2_info(sd,
"\tCarrier duty cycle: %2u/16\n",
1164 v4l2_info(sd,
"\tMax pulse width: %u us, %llu ns\n",
1165 pulse_width_count_to_us(
FIFO_RXTX, txclk),
1166 pulse_width_count_to_ns(
FIFO_RXTX, txclk));
1169 v4l2_info(sd,
"\tFIFO service requested: %s\n",
1171 v4l2_info(sd,
"\tFIFO service request interrupt: %s\n",
1172 irqen &
IRQEN_TSE ?
"enabled" :
"disabled");
1179 .rx_read = cx25840_ir_rx_read,
1180 .rx_g_parameters = cx25840_ir_rx_g_parameters,
1181 .rx_s_parameters = cx25840_ir_rx_s_parameters,
1183 .tx_write = cx25840_ir_tx_write,
1184 .tx_g_parameters = cx25840_ir_tx_g_parameters,
1185 .tx_s_parameters = cx25840_ir_tx_s_parameters,
1194 .interrupt_enable =
false,
1198 .carrier_freq = 36000,
1202 .noise_filter_min_width = 333333,
1203 .carrier_range_lower = 35000,
1204 .carrier_range_upper = 37000,
1205 .invert_level =
false,
1213 .interrupt_enable =
false,
1217 .carrier_freq = 36000,
1219 .invert_level =
false,
1220 .invert_carrier_sense =
false,
1230 if (!(is_cx23885(state) || is_cx23887(state)))
1234 if (ir_state ==
NULL)
1244 ir_state->
c = state->
c;
1248 if (is_cx23885(state) || is_cx23887(state))
1254 memcpy(&default_params, &default_rx_params,
1259 memcpy(&default_params, &default_tx_params,
1271 if (ir_state ==
NULL)
1274 cx25840_ir_rx_shutdown(sd);
1275 cx25840_ir_tx_shutdown(sd);