cx25840-ir.c 35 KB

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