cx23888-ir.c 36 KB

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