PageRenderTime 244ms CodeModel.GetById 6ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/media/video/cx25840/cx25840-ir.c

https://bitbucket.org/slukk/jb-tsm-kernel-4.2
C | 1280 lines | 926 code | 191 blank | 163 comment | 117 complexity | 216e028ce94470533c6ddab2e32aed8a MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
  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 <media/cx25840.h>
  26. #include <media/rc-core.h>
  27. #include "cx25840-core.h"
  28. static unsigned int ir_debug;
  29. module_param(ir_debug, int, 0644);
  30. MODULE_PARM_DESC(ir_debug, "enable integrated IR debug messages");
  31. #define CX25840_IR_REG_BASE 0x200
  32. #define CX25840_IR_CNTRL_REG 0x200
  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. #define CX25840_IR_TXCLK_REG 0x204
  55. #define TXCLK_TCD 0x0000FFFF
  56. #define CX25840_IR_RXCLK_REG 0x208
  57. #define RXCLK_RCD 0x0000FFFF
  58. #define CX25840_IR_CDUTY_REG 0x20C
  59. #define CDUTY_CDC 0x0000000F
  60. #define CX25840_IR_STATS_REG 0x210
  61. #define STATS_RTO 0x00000001
  62. #define STATS_ROR 0x00000002
  63. #define STATS_RBY 0x00000004
  64. #define STATS_TBY 0x00000008
  65. #define STATS_RSR 0x00000010
  66. #define STATS_TSR 0x00000020
  67. #define CX25840_IR_IRQEN_REG 0x214
  68. #define IRQEN_RTE 0x00000001
  69. #define IRQEN_ROE 0x00000002
  70. #define IRQEN_RSE 0x00000010
  71. #define IRQEN_TSE 0x00000020
  72. #define IRQEN_MSK 0x00000033
  73. #define CX25840_IR_FILTR_REG 0x218
  74. #define FILTR_LPF 0x0000FFFF
  75. #define CX25840_IR_FIFO_REG 0x23C
  76. #define FIFO_RXTX 0x0000FFFF
  77. #define FIFO_RXTX_LVL 0x00010000
  78. #define FIFO_RXTX_RTO 0x0001FFFF
  79. #define FIFO_RX_NDV 0x00020000
  80. #define FIFO_RX_DEPTH 8
  81. #define FIFO_TX_DEPTH 8
  82. #define CX25840_VIDCLK_FREQ 108000000 /* 108 MHz, BT.656 */
  83. #define CX25840_IR_REFCLK_FREQ (CX25840_VIDCLK_FREQ / 2)
  84. /*
  85. * We use this union internally for convenience, but callers to tx_write
  86. * and rx_read will be expecting records of type struct ir_raw_event.
  87. * Always ensure the size of this union is dictated by struct ir_raw_event.
  88. */
  89. union cx25840_ir_fifo_rec {
  90. u32 hw_fifo_data;
  91. struct ir_raw_event ir_core_data;
  92. };
  93. #define CX25840_IR_RX_KFIFO_SIZE (256 * sizeof(union cx25840_ir_fifo_rec))
  94. #define CX25840_IR_TX_KFIFO_SIZE (256 * sizeof(union cx25840_ir_fifo_rec))
  95. struct cx25840_ir_state {
  96. struct i2c_client *c;
  97. struct v4l2_subdev_ir_parameters rx_params;
  98. struct mutex rx_params_lock; /* protects Rx parameter settings cache */
  99. atomic_t rxclk_divider;
  100. atomic_t rx_invert;
  101. struct kfifo rx_kfifo;
  102. spinlock_t rx_kfifo_lock; /* protect Rx data kfifo */
  103. struct v4l2_subdev_ir_parameters tx_params;
  104. struct mutex tx_params_lock; /* protects Tx parameter settings cache */
  105. atomic_t txclk_divider;
  106. };
  107. static inline struct cx25840_ir_state *to_ir_state(struct v4l2_subdev *sd)
  108. {
  109. struct cx25840_state *state = to_state(sd);
  110. return state ? state->ir_state : NULL;
  111. }
  112. /*
  113. * Rx and Tx Clock Divider register computations
  114. *
  115. * Note the largest clock divider value of 0xffff corresponds to:
  116. * (0xffff + 1) * 1000 / 108/2 MHz = 1,213,629.629... ns
  117. * which fits in 21 bits, so we'll use unsigned int for time arguments.
  118. */
  119. static inline u16 count_to_clock_divider(unsigned int d)
  120. {
  121. if (d > RXCLK_RCD + 1)
  122. d = RXCLK_RCD;
  123. else if (d < 2)
  124. d = 1;
  125. else
  126. d--;
  127. return (u16) d;
  128. }
  129. static inline u16 ns_to_clock_divider(unsigned int ns)
  130. {
  131. return count_to_clock_divider(
  132. DIV_ROUND_CLOSEST(CX25840_IR_REFCLK_FREQ / 1000000 * ns, 1000));
  133. }
  134. static inline unsigned int clock_divider_to_ns(unsigned int divider)
  135. {
  136. /* Period of the Rx or Tx clock in ns */
  137. return DIV_ROUND_CLOSEST((divider + 1) * 1000,
  138. CX25840_IR_REFCLK_FREQ / 1000000);
  139. }
  140. static inline u16 carrier_freq_to_clock_divider(unsigned int freq)
  141. {
  142. return count_to_clock_divider(
  143. DIV_ROUND_CLOSEST(CX25840_IR_REFCLK_FREQ, freq * 16));
  144. }
  145. static inline unsigned int clock_divider_to_carrier_freq(unsigned int divider)
  146. {
  147. return DIV_ROUND_CLOSEST(CX25840_IR_REFCLK_FREQ, (divider + 1) * 16);
  148. }
  149. static inline u16 freq_to_clock_divider(unsigned int freq,
  150. unsigned int rollovers)
  151. {
  152. return count_to_clock_divider(
  153. DIV_ROUND_CLOSEST(CX25840_IR_REFCLK_FREQ, freq * rollovers));
  154. }
  155. static inline unsigned int clock_divider_to_freq(unsigned int divider,
  156. unsigned int rollovers)
  157. {
  158. return DIV_ROUND_CLOSEST(CX25840_IR_REFCLK_FREQ,
  159. (divider + 1) * rollovers);
  160. }
  161. /*
  162. * Low Pass Filter register calculations
  163. *
  164. * Note the largest count value of 0xffff corresponds to:
  165. * 0xffff * 1000 / 108/2 MHz = 1,213,611.11... ns
  166. * which fits in 21 bits, so we'll use unsigned int for time arguments.
  167. */
  168. static inline u16 count_to_lpf_count(unsigned int d)
  169. {
  170. if (d > FILTR_LPF)
  171. d = FILTR_LPF;
  172. else if (d < 4)
  173. d = 0;
  174. return (u16) d;
  175. }
  176. static inline u16 ns_to_lpf_count(unsigned int ns)
  177. {
  178. return count_to_lpf_count(
  179. DIV_ROUND_CLOSEST(CX25840_IR_REFCLK_FREQ / 1000000 * ns, 1000));
  180. }
  181. static inline unsigned int lpf_count_to_ns(unsigned int count)
  182. {
  183. /* Duration of the Low Pass Filter rejection window in ns */
  184. return DIV_ROUND_CLOSEST(count * 1000,
  185. CX25840_IR_REFCLK_FREQ / 1000000);
  186. }
  187. static inline unsigned int lpf_count_to_us(unsigned int count)
  188. {
  189. /* Duration of the Low Pass Filter rejection window in us */
  190. return DIV_ROUND_CLOSEST(count, CX25840_IR_REFCLK_FREQ / 1000000);
  191. }
  192. /*
  193. * FIFO register pulse width count compuations
  194. */
  195. static u32 clock_divider_to_resolution(u16 divider)
  196. {
  197. /*
  198. * Resolution is the duration of 1 tick of the readable portion of
  199. * of the pulse width counter as read from the FIFO. The two lsb's are
  200. * not readable, hence the << 2. This function returns ns.
  201. */
  202. return DIV_ROUND_CLOSEST((1 << 2) * ((u32) divider + 1) * 1000,
  203. CX25840_IR_REFCLK_FREQ / 1000000);
  204. }
  205. static u64 pulse_width_count_to_ns(u16 count, u16 divider)
  206. {
  207. u64 n;
  208. u32 rem;
  209. /*
  210. * The 2 lsb's of the pulse width timer count are not readable, hence
  211. * the (count << 2) | 0x3
  212. */
  213. n = (((u64) count << 2) | 0x3) * (divider + 1) * 1000; /* millicycles */
  214. rem = do_div(n, CX25840_IR_REFCLK_FREQ / 1000000); /* / MHz => ns */
  215. if (rem >= CX25840_IR_REFCLK_FREQ / 1000000 / 2)
  216. n++;
  217. return n;
  218. }
  219. #if 0
  220. /* Keep as we will need this for Transmit functionality */
  221. static u16 ns_to_pulse_width_count(u32 ns, u16 divider)
  222. {
  223. u64 n;
  224. u32 d;
  225. u32 rem;
  226. /*
  227. * The 2 lsb's of the pulse width timer count are not accessible, hence
  228. * the (1 << 2)
  229. */
  230. n = ((u64) ns) * CX25840_IR_REFCLK_FREQ / 1000000; /* millicycles */
  231. d = (1 << 2) * ((u32) divider + 1) * 1000; /* millicycles/count */
  232. rem = do_div(n, d);
  233. if (rem >= d / 2)
  234. n++;
  235. if (n > FIFO_RXTX)
  236. n = FIFO_RXTX;
  237. else if (n == 0)
  238. n = 1;
  239. return (u16) n;
  240. }
  241. #endif
  242. static unsigned int pulse_width_count_to_us(u16 count, u16 divider)
  243. {
  244. u64 n;
  245. u32 rem;
  246. /*
  247. * The 2 lsb's of the pulse width timer count are not readable, hence
  248. * the (count << 2) | 0x3
  249. */
  250. n = (((u64) count << 2) | 0x3) * (divider + 1); /* cycles */
  251. rem = do_div(n, CX25840_IR_REFCLK_FREQ / 1000000); /* / MHz => us */
  252. if (rem >= CX25840_IR_REFCLK_FREQ / 1000000 / 2)
  253. n++;
  254. return (unsigned int) n;
  255. }
  256. /*
  257. * Pulse Clocks computations: Combined Pulse Width Count & Rx Clock Counts
  258. *
  259. * The total pulse clock count is an 18 bit pulse width timer count as the most
  260. * significant part and (up to) 16 bit clock divider count as a modulus.
  261. * When the Rx clock divider ticks down to 0, it increments the 18 bit pulse
  262. * width timer count's least significant bit.
  263. */
  264. static u64 ns_to_pulse_clocks(u32 ns)
  265. {
  266. u64 clocks;
  267. u32 rem;
  268. clocks = CX25840_IR_REFCLK_FREQ / 1000000 * (u64) ns; /* millicycles */
  269. rem = do_div(clocks, 1000); /* /1000 = cycles */
  270. if (rem >= 1000 / 2)
  271. clocks++;
  272. return clocks;
  273. }
  274. static u16 pulse_clocks_to_clock_divider(u64 count)
  275. {
  276. u32 rem;
  277. rem = 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;
  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. v4l2_dbg(2, ir_debug, sd, "rx read: end of rx\n");
  607. } else {
  608. u = (p->hw_fifo_data & FIFO_RXTX_LVL) ? 1 : 0;
  609. if (invert)
  610. u = u ? 0 : 1;
  611. }
  612. v = (unsigned) pulse_width_count_to_ns(
  613. (u16) (p->hw_fifo_data & FIFO_RXTX), divider);
  614. if (v > IR_MAX_DURATION)
  615. v = IR_MAX_DURATION;
  616. init_ir_raw_event(&p->ir_core_data);
  617. p->ir_core_data.pulse = u;
  618. p->ir_core_data.duration = v;
  619. v4l2_dbg(2, ir_debug, sd, "rx read: %10u ns %s\n",
  620. v, u ? "mark" : "space");
  621. }
  622. return 0;
  623. }
  624. static int cx25840_ir_rx_g_parameters(struct v4l2_subdev *sd,
  625. struct v4l2_subdev_ir_parameters *p)
  626. {
  627. struct cx25840_ir_state *ir_state = to_ir_state(sd);
  628. if (ir_state == NULL)
  629. return -ENODEV;
  630. mutex_lock(&ir_state->rx_params_lock);
  631. memcpy(p, &ir_state->rx_params,
  632. sizeof(struct v4l2_subdev_ir_parameters));
  633. mutex_unlock(&ir_state->rx_params_lock);
  634. return 0;
  635. }
  636. static int cx25840_ir_rx_shutdown(struct v4l2_subdev *sd)
  637. {
  638. struct cx25840_ir_state *ir_state = to_ir_state(sd);
  639. struct i2c_client *c;
  640. if (ir_state == NULL)
  641. return -ENODEV;
  642. c = ir_state->c;
  643. mutex_lock(&ir_state->rx_params_lock);
  644. /* Disable or slow down all IR Rx circuits and counters */
  645. irqenable_rx(sd, 0);
  646. control_rx_enable(c, false);
  647. control_rx_demodulation_enable(c, false);
  648. control_rx_s_edge_detection(c, CNTRL_EDG_NONE);
  649. filter_rx_s_min_width(c, 0);
  650. cx25840_write4(c, CX25840_IR_RXCLK_REG, RXCLK_RCD);
  651. ir_state->rx_params.shutdown = true;
  652. mutex_unlock(&ir_state->rx_params_lock);
  653. return 0;
  654. }
  655. static int cx25840_ir_rx_s_parameters(struct v4l2_subdev *sd,
  656. struct v4l2_subdev_ir_parameters *p)
  657. {
  658. struct cx25840_ir_state *ir_state = to_ir_state(sd);
  659. struct i2c_client *c;
  660. struct v4l2_subdev_ir_parameters *o;
  661. u16 rxclk_divider;
  662. if (ir_state == NULL)
  663. return -ENODEV;
  664. if (p->shutdown)
  665. return cx25840_ir_rx_shutdown(sd);
  666. if (p->mode != V4L2_SUBDEV_IR_MODE_PULSE_WIDTH)
  667. return -ENOSYS;
  668. c = ir_state->c;
  669. o = &ir_state->rx_params;
  670. mutex_lock(&ir_state->rx_params_lock);
  671. o->shutdown = p->shutdown;
  672. p->mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH;
  673. o->mode = p->mode;
  674. p->bytes_per_data_element = sizeof(union cx25840_ir_fifo_rec);
  675. o->bytes_per_data_element = p->bytes_per_data_element;
  676. /* Before we tweak the hardware, we have to disable the receiver */
  677. irqenable_rx(sd, 0);
  678. control_rx_enable(c, false);
  679. control_rx_demodulation_enable(c, p->modulation);
  680. o->modulation = p->modulation;
  681. if (p->modulation) {
  682. p->carrier_freq = rxclk_rx_s_carrier(c, p->carrier_freq,
  683. &rxclk_divider);
  684. o->carrier_freq = p->carrier_freq;
  685. p->duty_cycle = 50;
  686. o->duty_cycle = p->duty_cycle;
  687. control_rx_s_carrier_window(c, p->carrier_freq,
  688. &p->carrier_range_lower,
  689. &p->carrier_range_upper);
  690. o->carrier_range_lower = p->carrier_range_lower;
  691. o->carrier_range_upper = p->carrier_range_upper;
  692. p->max_pulse_width =
  693. (u32) pulse_width_count_to_ns(FIFO_RXTX, rxclk_divider);
  694. } else {
  695. p->max_pulse_width =
  696. rxclk_rx_s_max_pulse_width(c, p->max_pulse_width,
  697. &rxclk_divider);
  698. }
  699. o->max_pulse_width = p->max_pulse_width;
  700. atomic_set(&ir_state->rxclk_divider, rxclk_divider);
  701. p->noise_filter_min_width =
  702. filter_rx_s_min_width(c, p->noise_filter_min_width);
  703. o->noise_filter_min_width = p->noise_filter_min_width;
  704. p->resolution = clock_divider_to_resolution(rxclk_divider);
  705. o->resolution = p->resolution;
  706. /* FIXME - make this dependent on resolution for better performance */
  707. control_rx_irq_watermark(c, RX_FIFO_HALF_FULL);
  708. control_rx_s_edge_detection(c, CNTRL_EDG_BOTH);
  709. o->invert_level = p->invert_level;
  710. atomic_set(&ir_state->rx_invert, p->invert_level);
  711. o->interrupt_enable = p->interrupt_enable;
  712. o->enable = p->enable;
  713. if (p->enable) {
  714. unsigned long flags;
  715. spin_lock_irqsave(&ir_state->rx_kfifo_lock, flags);
  716. kfifo_reset(&ir_state->rx_kfifo);
  717. spin_unlock_irqrestore(&ir_state->rx_kfifo_lock, flags);
  718. if (p->interrupt_enable)
  719. irqenable_rx(sd, IRQEN_RSE | IRQEN_RTE | IRQEN_ROE);
  720. control_rx_enable(c, p->enable);
  721. }
  722. mutex_unlock(&ir_state->rx_params_lock);
  723. return 0;
  724. }
  725. /* Transmitter */
  726. static int cx25840_ir_tx_write(struct v4l2_subdev *sd, u8 *buf, size_t count,
  727. ssize_t *num)
  728. {
  729. struct cx25840_ir_state *ir_state = to_ir_state(sd);
  730. struct i2c_client *c;
  731. if (ir_state == NULL)
  732. return -ENODEV;
  733. c = ir_state->c;
  734. #if 0
  735. /*
  736. * FIXME - the code below is an incomplete and untested sketch of what
  737. * may need to be done. The critical part is to get 4 (or 8) pulses
  738. * from the tx_kfifo, or converted from ns to the proper units from the
  739. * input, and push them off to the hardware Tx FIFO right away, if the
  740. * HW TX fifo needs service. The rest can be pushed to the tx_kfifo in
  741. * a less critical timeframe. Also watch out for overruning the
  742. * tx_kfifo - don't let it happen and let the caller know not all his
  743. * pulses were written.
  744. */
  745. u32 *ns_pulse = (u32 *) buf;
  746. unsigned int n;
  747. u32 fifo_pulse[FIFO_TX_DEPTH];
  748. u32 mark;
  749. /* Compute how much we can fit in the tx kfifo */
  750. n = CX25840_IR_TX_KFIFO_SIZE - kfifo_len(ir_state->tx_kfifo);
  751. n = min(n, (unsigned int) count);
  752. n /= sizeof(u32);
  753. /* FIXME - turn on Tx Fifo service interrupt
  754. * check hardware fifo level, and other stuff
  755. */
  756. for (i = 0; i < n; ) {
  757. for (j = 0; j < FIFO_TX_DEPTH / 2 && i < n; j++) {
  758. mark = ns_pulse[i] & LEVEL_MASK;
  759. fifo_pulse[j] = ns_to_pulse_width_count(
  760. ns_pulse[i] &
  761. ~LEVEL_MASK,
  762. ir_state->txclk_divider);
  763. if (mark)
  764. fifo_pulse[j] &= FIFO_RXTX_LVL;
  765. i++;
  766. }
  767. kfifo_put(ir_state->tx_kfifo, (u8 *) fifo_pulse,
  768. j * sizeof(u32));
  769. }
  770. *num = n * sizeof(u32);
  771. #else
  772. /* For now enable the Tx FIFO Service interrupt & pretend we did work */
  773. irqenable_tx(sd, IRQEN_TSE);
  774. *num = count;
  775. #endif
  776. return 0;
  777. }
  778. static int cx25840_ir_tx_g_parameters(struct v4l2_subdev *sd,
  779. struct v4l2_subdev_ir_parameters *p)
  780. {
  781. struct cx25840_ir_state *ir_state = to_ir_state(sd);
  782. if (ir_state == NULL)
  783. return -ENODEV;
  784. mutex_lock(&ir_state->tx_params_lock);
  785. memcpy(p, &ir_state->tx_params,
  786. sizeof(struct v4l2_subdev_ir_parameters));
  787. mutex_unlock(&ir_state->tx_params_lock);
  788. return 0;
  789. }
  790. static int cx25840_ir_tx_shutdown(struct v4l2_subdev *sd)
  791. {
  792. struct cx25840_ir_state *ir_state = to_ir_state(sd);
  793. struct i2c_client *c;
  794. if (ir_state == NULL)
  795. return -ENODEV;
  796. c = ir_state->c;
  797. mutex_lock(&ir_state->tx_params_lock);
  798. /* Disable or slow down all IR Tx circuits and counters */
  799. irqenable_tx(sd, 0);
  800. control_tx_enable(c, false);
  801. control_tx_modulation_enable(c, false);
  802. cx25840_write4(c, CX25840_IR_TXCLK_REG, TXCLK_TCD);
  803. ir_state->tx_params.shutdown = true;
  804. mutex_unlock(&ir_state->tx_params_lock);
  805. return 0;
  806. }
  807. static int cx25840_ir_tx_s_parameters(struct v4l2_subdev *sd,
  808. struct v4l2_subdev_ir_parameters *p)
  809. {
  810. struct cx25840_ir_state *ir_state = to_ir_state(sd);
  811. struct i2c_client *c;
  812. struct v4l2_subdev_ir_parameters *o;
  813. u16 txclk_divider;
  814. if (ir_state == NULL)
  815. return -ENODEV;
  816. if (p->shutdown)
  817. return cx25840_ir_tx_shutdown(sd);
  818. if (p->mode != V4L2_SUBDEV_IR_MODE_PULSE_WIDTH)
  819. return -ENOSYS;
  820. c = ir_state->c;
  821. o = &ir_state->tx_params;
  822. mutex_lock(&ir_state->tx_params_lock);
  823. o->shutdown = p->shutdown;
  824. p->mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH;
  825. o->mode = p->mode;
  826. p->bytes_per_data_element = sizeof(union cx25840_ir_fifo_rec);
  827. o->bytes_per_data_element = p->bytes_per_data_element;
  828. /* Before we tweak the hardware, we have to disable the transmitter */
  829. irqenable_tx(sd, 0);
  830. control_tx_enable(c, false);
  831. control_tx_modulation_enable(c, p->modulation);
  832. o->modulation = p->modulation;
  833. if (p->modulation) {
  834. p->carrier_freq = txclk_tx_s_carrier(c, p->carrier_freq,
  835. &txclk_divider);
  836. o->carrier_freq = p->carrier_freq;
  837. p->duty_cycle = cduty_tx_s_duty_cycle(c, p->duty_cycle);
  838. o->duty_cycle = p->duty_cycle;
  839. p->max_pulse_width =
  840. (u32) pulse_width_count_to_ns(FIFO_RXTX, txclk_divider);
  841. } else {
  842. p->max_pulse_width =
  843. txclk_tx_s_max_pulse_width(c, p->max_pulse_width,
  844. &txclk_divider);
  845. }
  846. o->max_pulse_width = p->max_pulse_width;
  847. atomic_set(&ir_state->txclk_divider, txclk_divider);
  848. p->resolution = clock_divider_to_resolution(txclk_divider);
  849. o->resolution = p->resolution;
  850. /* FIXME - make this dependent on resolution for better performance */
  851. control_tx_irq_watermark(c, TX_FIFO_HALF_EMPTY);
  852. control_tx_polarity_invert(c, p->invert_carrier_sense);
  853. o->invert_carrier_sense = p->invert_carrier_sense;
  854. /*
  855. * FIXME: we don't have hardware help for IO pin level inversion
  856. * here like we have on the CX23888.
  857. * Act on this with some mix of logical inversion of data levels,
  858. * carrier polarity, and carrier duty cycle.
  859. */
  860. o->invert_level = p->invert_level;
  861. o->interrupt_enable = p->interrupt_enable;
  862. o->enable = p->enable;
  863. if (p->enable) {
  864. /* reset tx_fifo here */
  865. if (p->interrupt_enable)
  866. irqenable_tx(sd, IRQEN_TSE);
  867. control_tx_enable(c, p->enable);
  868. }
  869. mutex_unlock(&ir_state->tx_params_lock);
  870. return 0;
  871. }
  872. /*
  873. * V4L2 Subdevice Core Ops support
  874. */
  875. int cx25840_ir_log_status(struct v4l2_subdev *sd)
  876. {
  877. struct cx25840_state *state = to_state(sd);
  878. struct i2c_client *c = state->c;
  879. char *s;
  880. int i, j;
  881. u32 cntrl, txclk, rxclk, cduty, stats, irqen, filtr;
  882. /* The CX23888 chip doesn't have an IR controller on the A/V core */
  883. if (is_cx23888(state))
  884. return 0;
  885. cntrl = cx25840_read4(c, CX25840_IR_CNTRL_REG);
  886. txclk = cx25840_read4(c, CX25840_IR_TXCLK_REG) & TXCLK_TCD;
  887. rxclk = cx25840_read4(c, CX25840_IR_RXCLK_REG) & RXCLK_RCD;
  888. cduty = cx25840_read4(c, CX25840_IR_CDUTY_REG) & CDUTY_CDC;
  889. stats = cx25840_read4(c, CX25840_IR_STATS_REG);
  890. irqen = cx25840_read4(c, CX25840_IR_IRQEN_REG);
  891. if (is_cx23885(state) || is_cx23887(state))
  892. irqen ^= IRQEN_MSK;
  893. filtr = cx25840_read4(c, CX25840_IR_FILTR_REG) & FILTR_LPF;
  894. v4l2_info(sd, "IR Receiver:\n");
  895. v4l2_info(sd, "\tEnabled: %s\n",
  896. cntrl & CNTRL_RXE ? "yes" : "no");
  897. v4l2_info(sd, "\tDemodulation from a carrier: %s\n",
  898. cntrl & CNTRL_DMD ? "enabled" : "disabled");
  899. v4l2_info(sd, "\tFIFO: %s\n",
  900. cntrl & CNTRL_RFE ? "enabled" : "disabled");
  901. switch (cntrl & CNTRL_EDG) {
  902. case CNTRL_EDG_NONE:
  903. s = "disabled";
  904. break;
  905. case CNTRL_EDG_FALL:
  906. s = "falling edge";
  907. break;
  908. case CNTRL_EDG_RISE:
  909. s = "rising edge";
  910. break;
  911. case CNTRL_EDG_BOTH:
  912. s = "rising & falling edges";
  913. break;
  914. default:
  915. s = "??? edge";
  916. break;
  917. }
  918. v4l2_info(sd, "\tPulse timers' start/stop trigger: %s\n", s);
  919. v4l2_info(sd, "\tFIFO data on pulse timer overflow: %s\n",
  920. cntrl & CNTRL_R ? "not loaded" : "overflow marker");
  921. v4l2_info(sd, "\tFIFO interrupt watermark: %s\n",
  922. cntrl & CNTRL_RIC ? "not empty" : "half full or greater");
  923. v4l2_info(sd, "\tLoopback mode: %s\n",
  924. cntrl & CNTRL_LBM ? "loopback active" : "normal receive");
  925. if (cntrl & CNTRL_DMD) {
  926. v4l2_info(sd, "\tExpected carrier (16 clocks): %u Hz\n",
  927. clock_divider_to_carrier_freq(rxclk));
  928. switch (cntrl & CNTRL_WIN) {
  929. case CNTRL_WIN_3_3:
  930. i = 3;
  931. j = 3;
  932. break;
  933. case CNTRL_WIN_4_3:
  934. i = 4;
  935. j = 3;
  936. break;
  937. case CNTRL_WIN_3_4:
  938. i = 3;
  939. j = 4;
  940. break;
  941. case CNTRL_WIN_4_4:
  942. i = 4;
  943. j = 4;
  944. break;
  945. default:
  946. i = 0;
  947. j = 0;
  948. break;
  949. }
  950. v4l2_info(sd, "\tNext carrier edge window: 16 clocks "
  951. "-%1d/+%1d, %u to %u Hz\n", i, j,
  952. clock_divider_to_freq(rxclk, 16 + j),
  953. clock_divider_to_freq(rxclk, 16 - i));
  954. }
  955. v4l2_info(sd, "\tMax measurable pulse width: %u us, %llu ns\n",
  956. pulse_width_count_to_us(FIFO_RXTX, rxclk),
  957. pulse_width_count_to_ns(FIFO_RXTX, rxclk));
  958. v4l2_info(sd, "\tLow pass filter: %s\n",
  959. filtr ? "enabled" : "disabled");
  960. if (filtr)
  961. v4l2_info(sd, "\tMin acceptable pulse width (LPF): %u us, "
  962. "%u ns\n",
  963. lpf_count_to_us(filtr),
  964. lpf_count_to_ns(filtr));
  965. v4l2_info(sd, "\tPulse width timer timed-out: %s\n",
  966. stats & STATS_RTO ? "yes" : "no");
  967. v4l2_info(sd, "\tPulse width timer time-out intr: %s\n",
  968. irqen & IRQEN_RTE ? "enabled" : "disabled");
  969. v4l2_info(sd, "\tFIFO overrun: %s\n",
  970. stats & STATS_ROR ? "yes" : "no");
  971. v4l2_info(sd, "\tFIFO overrun interrupt: %s\n",
  972. irqen & IRQEN_ROE ? "enabled" : "disabled");
  973. v4l2_info(sd, "\tBusy: %s\n",
  974. stats & STATS_RBY ? "yes" : "no");
  975. v4l2_info(sd, "\tFIFO service requested: %s\n",
  976. stats & STATS_RSR ? "yes" : "no");
  977. v4l2_info(sd, "\tFIFO service request interrupt: %s\n",
  978. irqen & IRQEN_RSE ? "enabled" : "disabled");
  979. v4l2_info(sd, "IR Transmitter:\n");
  980. v4l2_info(sd, "\tEnabled: %s\n",
  981. cntrl & CNTRL_TXE ? "yes" : "no");
  982. v4l2_info(sd, "\tModulation onto a carrier: %s\n",
  983. cntrl & CNTRL_MOD ? "enabled" : "disabled");
  984. v4l2_info(sd, "\tFIFO: %s\n",
  985. cntrl & CNTRL_TFE ? "enabled" : "disabled");
  986. v4l2_info(sd, "\tFIFO interrupt watermark: %s\n",
  987. cntrl & CNTRL_TIC ? "not empty" : "half full or less");
  988. v4l2_info(sd, "\tCarrier polarity: %s\n",
  989. cntrl & CNTRL_CPL ? "space:burst mark:noburst"
  990. : "space:noburst mark:burst");
  991. if (cntrl & CNTRL_MOD) {
  992. v4l2_info(sd, "\tCarrier (16 clocks): %u Hz\n",
  993. clock_divider_to_carrier_freq(txclk));
  994. v4l2_info(sd, "\tCarrier duty cycle: %2u/16\n",
  995. cduty + 1);
  996. }
  997. v4l2_info(sd, "\tMax pulse width: %u us, %llu ns\n",
  998. pulse_width_count_to_us(FIFO_RXTX, txclk),
  999. pulse_width_count_to_ns(FIFO_RXTX, txclk));
  1000. v4l2_info(sd, "\tBusy: %s\n",
  1001. stats & STATS_TBY ? "yes" : "no");
  1002. v4l2_info(sd, "\tFIFO service requested: %s\n",
  1003. stats & STATS_TSR ? "yes" : "no");
  1004. v4l2_info(sd, "\tFIFO service request interrupt: %s\n",
  1005. irqen & IRQEN_TSE ? "enabled" : "disabled");
  1006. return 0;
  1007. }
  1008. const struct v4l2_subdev_ir_ops cx25840_ir_ops = {
  1009. .rx_read = cx25840_ir_rx_read,
  1010. .rx_g_parameters = cx25840_ir_rx_g_parameters,
  1011. .rx_s_parameters = cx25840_ir_rx_s_parameters,
  1012. .tx_write = cx25840_ir_tx_write,
  1013. .tx_g_parameters = cx25840_ir_tx_g_parameters,
  1014. .tx_s_parameters = cx25840_ir_tx_s_parameters,
  1015. };
  1016. static const struct v4l2_subdev_ir_parameters default_rx_params = {
  1017. .bytes_per_data_element = sizeof(union cx25840_ir_fifo_rec),
  1018. .mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH,
  1019. .enable = false,
  1020. .interrupt_enable = false,
  1021. .shutdown = true,
  1022. .modulation = true,
  1023. .carrier_freq = 36000, /* 36 kHz - RC-5, and RC-6 carrier */
  1024. /* RC-5: 666,667 ns = 1/36 kHz * 32 cycles * 1 mark * 0.75 */
  1025. /* RC-6: 333,333 ns = 1/36 kHz * 16 cycles * 1 mark * 0.75 */
  1026. .noise_filter_min_width = 333333, /* ns */
  1027. .carrier_range_lower = 35000,
  1028. .carrier_range_upper = 37000,
  1029. .invert_level = false,
  1030. };
  1031. static const struct v4l2_subdev_ir_parameters default_tx_params = {
  1032. .bytes_per_data_element = sizeof(union cx25840_ir_fifo_rec),
  1033. .mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH,
  1034. .enable = false,
  1035. .interrupt_enable = false,
  1036. .shutdown = true,
  1037. .modulation = true,
  1038. .carrier_freq = 36000, /* 36 kHz - RC-5 carrier */
  1039. .duty_cycle = 25, /* 25 % - RC-5 carrier */
  1040. .invert_level = false,
  1041. .invert_carrier_sense = false,
  1042. };
  1043. int cx25840_ir_probe(struct v4l2_subdev *sd)
  1044. {
  1045. struct cx25840_state *state = to_state(sd);
  1046. struct cx25840_ir_state *ir_state;
  1047. struct v4l2_subdev_ir_parameters default_params;
  1048. /* Only init the IR controller for the CX2388[57] AV Core for now */
  1049. if (!(is_cx23885(state) || is_cx23887(state)))
  1050. return 0;
  1051. ir_state = kzalloc(sizeof(struct cx25840_ir_state), GFP_KERNEL);
  1052. if (ir_state == NULL)
  1053. return -ENOMEM;
  1054. spin_lock_init(&ir_state->rx_kfifo_lock);
  1055. if (kfifo_alloc(&ir_state->rx_kfifo,
  1056. CX25840_IR_RX_KFIFO_SIZE, GFP_KERNEL)) {
  1057. kfree(ir_state);
  1058. return -ENOMEM;
  1059. }
  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. memcpy(&default_params, &default_rx_params,
  1069. sizeof(struct v4l2_subdev_ir_parameters));
  1070. v4l2_subdev_call(sd, ir, rx_s_parameters, &default_params);
  1071. mutex_init(&ir_state->tx_params_lock);
  1072. memcpy(&default_params, &default_tx_params,
  1073. sizeof(struct v4l2_subdev_ir_parameters));
  1074. v4l2_subdev_call(sd, ir, tx_s_parameters, &default_params);
  1075. return 0;
  1076. }
  1077. int cx25840_ir_remove(struct v4l2_subdev *sd)
  1078. {
  1079. struct cx25840_state *state = to_state(sd);
  1080. struct cx25840_ir_state *ir_state = to_ir_state(sd);
  1081. if (ir_state == NULL)
  1082. return -ENODEV;
  1083. cx25840_ir_rx_shutdown(sd);
  1084. cx25840_ir_tx_shutdown(sd);
  1085. kfifo_free(&ir_state->rx_kfifo);
  1086. kfree(ir_state);
  1087. state->ir_state = NULL;
  1088. return 0;
  1089. }