cx23888-ir.c 35 KB

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