cx25840-ir.c 34 KB

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