cx23888-ir.c 35 KB

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