cx25840-ir.c 35 KB

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