cx25840-ir.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284
  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, w;
  588. if (ir_state == NULL)
  589. return -ENODEV;
  590. invert = (bool) atomic_read(&ir_state->rx_invert);
  591. divider = (u16) atomic_read(&ir_state->rxclk_divider);
  592. n = count / sizeof(union cx25840_ir_fifo_rec)
  593. * sizeof(union cx25840_ir_fifo_rec);
  594. if (n == 0) {
  595. *num = 0;
  596. return 0;
  597. }
  598. n = kfifo_out_locked(&ir_state->rx_kfifo, buf, n,
  599. &ir_state->rx_kfifo_lock);
  600. n /= sizeof(union cx25840_ir_fifo_rec);
  601. *num = n * sizeof(union cx25840_ir_fifo_rec);
  602. for (p = (union cx25840_ir_fifo_rec *) buf, i = 0; i < n; p++, i++) {
  603. if ((p->hw_fifo_data & FIFO_RXTX_RTO) == FIFO_RXTX_RTO) {
  604. /* Assume RTO was because of no IR light input */
  605. u = 0;
  606. w = 1;
  607. } else {
  608. u = (p->hw_fifo_data & FIFO_RXTX_LVL) ? 1 : 0;
  609. if (invert)
  610. u = u ? 0 : 1;
  611. w = 0;
  612. }
  613. v = (unsigned) pulse_width_count_to_ns(
  614. (u16) (p->hw_fifo_data & FIFO_RXTX), divider);
  615. if (v > IR_MAX_DURATION)
  616. v = IR_MAX_DURATION;
  617. init_ir_raw_event(&p->ir_core_data);
  618. p->ir_core_data.pulse = u;
  619. p->ir_core_data.duration = v;
  620. p->ir_core_data.timeout = w;
  621. v4l2_dbg(2, ir_debug, sd, "rx read: %10u ns %s %s\n",
  622. v, u ? "mark" : "space", w ? "(timed out)" : "");
  623. if (w)
  624. v4l2_dbg(2, ir_debug, sd, "rx read: end of rx\n");
  625. }
  626. return 0;
  627. }
  628. static int cx25840_ir_rx_g_parameters(struct v4l2_subdev *sd,
  629. struct v4l2_subdev_ir_parameters *p)
  630. {
  631. struct cx25840_ir_state *ir_state = to_ir_state(sd);
  632. if (ir_state == NULL)
  633. return -ENODEV;
  634. mutex_lock(&ir_state->rx_params_lock);
  635. memcpy(p, &ir_state->rx_params,
  636. sizeof(struct v4l2_subdev_ir_parameters));
  637. mutex_unlock(&ir_state->rx_params_lock);
  638. return 0;
  639. }
  640. static int cx25840_ir_rx_shutdown(struct v4l2_subdev *sd)
  641. {
  642. struct cx25840_ir_state *ir_state = to_ir_state(sd);
  643. struct i2c_client *c;
  644. if (ir_state == NULL)
  645. return -ENODEV;
  646. c = ir_state->c;
  647. mutex_lock(&ir_state->rx_params_lock);
  648. /* Disable or slow down all IR Rx circuits and counters */
  649. irqenable_rx(sd, 0);
  650. control_rx_enable(c, false);
  651. control_rx_demodulation_enable(c, false);
  652. control_rx_s_edge_detection(c, CNTRL_EDG_NONE);
  653. filter_rx_s_min_width(c, 0);
  654. cx25840_write4(c, CX25840_IR_RXCLK_REG, RXCLK_RCD);
  655. ir_state->rx_params.shutdown = true;
  656. mutex_unlock(&ir_state->rx_params_lock);
  657. return 0;
  658. }
  659. static int cx25840_ir_rx_s_parameters(struct v4l2_subdev *sd,
  660. struct v4l2_subdev_ir_parameters *p)
  661. {
  662. struct cx25840_ir_state *ir_state = to_ir_state(sd);
  663. struct i2c_client *c;
  664. struct v4l2_subdev_ir_parameters *o;
  665. u16 rxclk_divider;
  666. if (ir_state == NULL)
  667. return -ENODEV;
  668. if (p->shutdown)
  669. return cx25840_ir_rx_shutdown(sd);
  670. if (p->mode != V4L2_SUBDEV_IR_MODE_PULSE_WIDTH)
  671. return -ENOSYS;
  672. c = ir_state->c;
  673. o = &ir_state->rx_params;
  674. mutex_lock(&ir_state->rx_params_lock);
  675. o->shutdown = p->shutdown;
  676. p->mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH;
  677. o->mode = p->mode;
  678. p->bytes_per_data_element = sizeof(union cx25840_ir_fifo_rec);
  679. o->bytes_per_data_element = p->bytes_per_data_element;
  680. /* Before we tweak the hardware, we have to disable the receiver */
  681. irqenable_rx(sd, 0);
  682. control_rx_enable(c, false);
  683. control_rx_demodulation_enable(c, p->modulation);
  684. o->modulation = p->modulation;
  685. if (p->modulation) {
  686. p->carrier_freq = rxclk_rx_s_carrier(c, p->carrier_freq,
  687. &rxclk_divider);
  688. o->carrier_freq = p->carrier_freq;
  689. p->duty_cycle = 50;
  690. o->duty_cycle = p->duty_cycle;
  691. control_rx_s_carrier_window(c, p->carrier_freq,
  692. &p->carrier_range_lower,
  693. &p->carrier_range_upper);
  694. o->carrier_range_lower = p->carrier_range_lower;
  695. o->carrier_range_upper = p->carrier_range_upper;
  696. p->max_pulse_width =
  697. (u32) pulse_width_count_to_ns(FIFO_RXTX, rxclk_divider);
  698. } else {
  699. p->max_pulse_width =
  700. rxclk_rx_s_max_pulse_width(c, p->max_pulse_width,
  701. &rxclk_divider);
  702. }
  703. o->max_pulse_width = p->max_pulse_width;
  704. atomic_set(&ir_state->rxclk_divider, rxclk_divider);
  705. p->noise_filter_min_width =
  706. filter_rx_s_min_width(c, p->noise_filter_min_width);
  707. o->noise_filter_min_width = p->noise_filter_min_width;
  708. p->resolution = clock_divider_to_resolution(rxclk_divider);
  709. o->resolution = p->resolution;
  710. /* FIXME - make this dependent on resolution for better performance */
  711. control_rx_irq_watermark(c, RX_FIFO_HALF_FULL);
  712. control_rx_s_edge_detection(c, CNTRL_EDG_BOTH);
  713. o->invert_level = p->invert_level;
  714. atomic_set(&ir_state->rx_invert, p->invert_level);
  715. o->interrupt_enable = p->interrupt_enable;
  716. o->enable = p->enable;
  717. if (p->enable) {
  718. unsigned long flags;
  719. spin_lock_irqsave(&ir_state->rx_kfifo_lock, flags);
  720. kfifo_reset(&ir_state->rx_kfifo);
  721. spin_unlock_irqrestore(&ir_state->rx_kfifo_lock, flags);
  722. if (p->interrupt_enable)
  723. irqenable_rx(sd, IRQEN_RSE | IRQEN_RTE | IRQEN_ROE);
  724. control_rx_enable(c, p->enable);
  725. }
  726. mutex_unlock(&ir_state->rx_params_lock);
  727. return 0;
  728. }
  729. /* Transmitter */
  730. static int cx25840_ir_tx_write(struct v4l2_subdev *sd, u8 *buf, size_t count,
  731. ssize_t *num)
  732. {
  733. struct cx25840_ir_state *ir_state = to_ir_state(sd);
  734. struct i2c_client *c;
  735. if (ir_state == NULL)
  736. return -ENODEV;
  737. c = ir_state->c;
  738. #if 0
  739. /*
  740. * FIXME - the code below is an incomplete and untested sketch of what
  741. * may need to be done. The critical part is to get 4 (or 8) pulses
  742. * from the tx_kfifo, or converted from ns to the proper units from the
  743. * input, and push them off to the hardware Tx FIFO right away, if the
  744. * HW TX fifo needs service. The rest can be pushed to the tx_kfifo in
  745. * a less critical timeframe. Also watch out for overruning the
  746. * tx_kfifo - don't let it happen and let the caller know not all his
  747. * pulses were written.
  748. */
  749. u32 *ns_pulse = (u32 *) buf;
  750. unsigned int n;
  751. u32 fifo_pulse[FIFO_TX_DEPTH];
  752. u32 mark;
  753. /* Compute how much we can fit in the tx kfifo */
  754. n = CX25840_IR_TX_KFIFO_SIZE - kfifo_len(ir_state->tx_kfifo);
  755. n = min(n, (unsigned int) count);
  756. n /= sizeof(u32);
  757. /* FIXME - turn on Tx Fifo service interrupt
  758. * check hardware fifo level, and other stuff
  759. */
  760. for (i = 0; i < n; ) {
  761. for (j = 0; j < FIFO_TX_DEPTH / 2 && i < n; j++) {
  762. mark = ns_pulse[i] & LEVEL_MASK;
  763. fifo_pulse[j] = ns_to_pulse_width_count(
  764. ns_pulse[i] &
  765. ~LEVEL_MASK,
  766. ir_state->txclk_divider);
  767. if (mark)
  768. fifo_pulse[j] &= FIFO_RXTX_LVL;
  769. i++;
  770. }
  771. kfifo_put(ir_state->tx_kfifo, (u8 *) fifo_pulse,
  772. j * sizeof(u32));
  773. }
  774. *num = n * sizeof(u32);
  775. #else
  776. /* For now enable the Tx FIFO Service interrupt & pretend we did work */
  777. irqenable_tx(sd, IRQEN_TSE);
  778. *num = count;
  779. #endif
  780. return 0;
  781. }
  782. static int cx25840_ir_tx_g_parameters(struct v4l2_subdev *sd,
  783. struct v4l2_subdev_ir_parameters *p)
  784. {
  785. struct cx25840_ir_state *ir_state = to_ir_state(sd);
  786. if (ir_state == NULL)
  787. return -ENODEV;
  788. mutex_lock(&ir_state->tx_params_lock);
  789. memcpy(p, &ir_state->tx_params,
  790. sizeof(struct v4l2_subdev_ir_parameters));
  791. mutex_unlock(&ir_state->tx_params_lock);
  792. return 0;
  793. }
  794. static int cx25840_ir_tx_shutdown(struct v4l2_subdev *sd)
  795. {
  796. struct cx25840_ir_state *ir_state = to_ir_state(sd);
  797. struct i2c_client *c;
  798. if (ir_state == NULL)
  799. return -ENODEV;
  800. c = ir_state->c;
  801. mutex_lock(&ir_state->tx_params_lock);
  802. /* Disable or slow down all IR Tx circuits and counters */
  803. irqenable_tx(sd, 0);
  804. control_tx_enable(c, false);
  805. control_tx_modulation_enable(c, false);
  806. cx25840_write4(c, CX25840_IR_TXCLK_REG, TXCLK_TCD);
  807. ir_state->tx_params.shutdown = true;
  808. mutex_unlock(&ir_state->tx_params_lock);
  809. return 0;
  810. }
  811. static int cx25840_ir_tx_s_parameters(struct v4l2_subdev *sd,
  812. struct v4l2_subdev_ir_parameters *p)
  813. {
  814. struct cx25840_ir_state *ir_state = to_ir_state(sd);
  815. struct i2c_client *c;
  816. struct v4l2_subdev_ir_parameters *o;
  817. u16 txclk_divider;
  818. if (ir_state == NULL)
  819. return -ENODEV;
  820. if (p->shutdown)
  821. return cx25840_ir_tx_shutdown(sd);
  822. if (p->mode != V4L2_SUBDEV_IR_MODE_PULSE_WIDTH)
  823. return -ENOSYS;
  824. c = ir_state->c;
  825. o = &ir_state->tx_params;
  826. mutex_lock(&ir_state->tx_params_lock);
  827. o->shutdown = p->shutdown;
  828. p->mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH;
  829. o->mode = p->mode;
  830. p->bytes_per_data_element = sizeof(union cx25840_ir_fifo_rec);
  831. o->bytes_per_data_element = p->bytes_per_data_element;
  832. /* Before we tweak the hardware, we have to disable the transmitter */
  833. irqenable_tx(sd, 0);
  834. control_tx_enable(c, false);
  835. control_tx_modulation_enable(c, p->modulation);
  836. o->modulation = p->modulation;
  837. if (p->modulation) {
  838. p->carrier_freq = txclk_tx_s_carrier(c, p->carrier_freq,
  839. &txclk_divider);
  840. o->carrier_freq = p->carrier_freq;
  841. p->duty_cycle = cduty_tx_s_duty_cycle(c, p->duty_cycle);
  842. o->duty_cycle = p->duty_cycle;
  843. p->max_pulse_width =
  844. (u32) pulse_width_count_to_ns(FIFO_RXTX, txclk_divider);
  845. } else {
  846. p->max_pulse_width =
  847. txclk_tx_s_max_pulse_width(c, p->max_pulse_width,
  848. &txclk_divider);
  849. }
  850. o->max_pulse_width = p->max_pulse_width;
  851. atomic_set(&ir_state->txclk_divider, txclk_divider);
  852. p->resolution = clock_divider_to_resolution(txclk_divider);
  853. o->resolution = p->resolution;
  854. /* FIXME - make this dependent on resolution for better performance */
  855. control_tx_irq_watermark(c, TX_FIFO_HALF_EMPTY);
  856. control_tx_polarity_invert(c, p->invert_carrier_sense);
  857. o->invert_carrier_sense = p->invert_carrier_sense;
  858. /*
  859. * FIXME: we don't have hardware help for IO pin level inversion
  860. * here like we have on the CX23888.
  861. * Act on this with some mix of logical inversion of data levels,
  862. * carrier polarity, and carrier duty cycle.
  863. */
  864. o->invert_level = p->invert_level;
  865. o->interrupt_enable = p->interrupt_enable;
  866. o->enable = p->enable;
  867. if (p->enable) {
  868. /* reset tx_fifo here */
  869. if (p->interrupt_enable)
  870. irqenable_tx(sd, IRQEN_TSE);
  871. control_tx_enable(c, p->enable);
  872. }
  873. mutex_unlock(&ir_state->tx_params_lock);
  874. return 0;
  875. }
  876. /*
  877. * V4L2 Subdevice Core Ops support
  878. */
  879. int cx25840_ir_log_status(struct v4l2_subdev *sd)
  880. {
  881. struct cx25840_state *state = to_state(sd);
  882. struct i2c_client *c = state->c;
  883. char *s;
  884. int i, j;
  885. u32 cntrl, txclk, rxclk, cduty, stats, irqen, filtr;
  886. /* The CX23888 chip doesn't have an IR controller on the A/V core */
  887. if (is_cx23888(state))
  888. return 0;
  889. cntrl = cx25840_read4(c, CX25840_IR_CNTRL_REG);
  890. txclk = cx25840_read4(c, CX25840_IR_TXCLK_REG) & TXCLK_TCD;
  891. rxclk = cx25840_read4(c, CX25840_IR_RXCLK_REG) & RXCLK_RCD;
  892. cduty = cx25840_read4(c, CX25840_IR_CDUTY_REG) & CDUTY_CDC;
  893. stats = cx25840_read4(c, CX25840_IR_STATS_REG);
  894. irqen = cx25840_read4(c, CX25840_IR_IRQEN_REG);
  895. if (is_cx23885(state) || is_cx23887(state))
  896. irqen ^= IRQEN_MSK;
  897. filtr = cx25840_read4(c, CX25840_IR_FILTR_REG) & FILTR_LPF;
  898. v4l2_info(sd, "IR Receiver:\n");
  899. v4l2_info(sd, "\tEnabled: %s\n",
  900. cntrl & CNTRL_RXE ? "yes" : "no");
  901. v4l2_info(sd, "\tDemodulation from a carrier: %s\n",
  902. cntrl & CNTRL_DMD ? "enabled" : "disabled");
  903. v4l2_info(sd, "\tFIFO: %s\n",
  904. cntrl & CNTRL_RFE ? "enabled" : "disabled");
  905. switch (cntrl & CNTRL_EDG) {
  906. case CNTRL_EDG_NONE:
  907. s = "disabled";
  908. break;
  909. case CNTRL_EDG_FALL:
  910. s = "falling edge";
  911. break;
  912. case CNTRL_EDG_RISE:
  913. s = "rising edge";
  914. break;
  915. case CNTRL_EDG_BOTH:
  916. s = "rising & falling edges";
  917. break;
  918. default:
  919. s = "??? edge";
  920. break;
  921. }
  922. v4l2_info(sd, "\tPulse timers' start/stop trigger: %s\n", s);
  923. v4l2_info(sd, "\tFIFO data on pulse timer overflow: %s\n",
  924. cntrl & CNTRL_R ? "not loaded" : "overflow marker");
  925. v4l2_info(sd, "\tFIFO interrupt watermark: %s\n",
  926. cntrl & CNTRL_RIC ? "not empty" : "half full or greater");
  927. v4l2_info(sd, "\tLoopback mode: %s\n",
  928. cntrl & CNTRL_LBM ? "loopback active" : "normal receive");
  929. if (cntrl & CNTRL_DMD) {
  930. v4l2_info(sd, "\tExpected carrier (16 clocks): %u Hz\n",
  931. clock_divider_to_carrier_freq(rxclk));
  932. switch (cntrl & CNTRL_WIN) {
  933. case CNTRL_WIN_3_3:
  934. i = 3;
  935. j = 3;
  936. break;
  937. case CNTRL_WIN_4_3:
  938. i = 4;
  939. j = 3;
  940. break;
  941. case CNTRL_WIN_3_4:
  942. i = 3;
  943. j = 4;
  944. break;
  945. case CNTRL_WIN_4_4:
  946. i = 4;
  947. j = 4;
  948. break;
  949. default:
  950. i = 0;
  951. j = 0;
  952. break;
  953. }
  954. v4l2_info(sd, "\tNext carrier edge window: 16 clocks "
  955. "-%1d/+%1d, %u to %u Hz\n", i, j,
  956. clock_divider_to_freq(rxclk, 16 + j),
  957. clock_divider_to_freq(rxclk, 16 - i));
  958. }
  959. v4l2_info(sd, "\tMax measurable pulse width: %u us, %llu ns\n",
  960. pulse_width_count_to_us(FIFO_RXTX, rxclk),
  961. pulse_width_count_to_ns(FIFO_RXTX, rxclk));
  962. v4l2_info(sd, "\tLow pass filter: %s\n",
  963. filtr ? "enabled" : "disabled");
  964. if (filtr)
  965. v4l2_info(sd, "\tMin acceptable pulse width (LPF): %u us, "
  966. "%u ns\n",
  967. lpf_count_to_us(filtr),
  968. lpf_count_to_ns(filtr));
  969. v4l2_info(sd, "\tPulse width timer timed-out: %s\n",
  970. stats & STATS_RTO ? "yes" : "no");
  971. v4l2_info(sd, "\tPulse width timer time-out intr: %s\n",
  972. irqen & IRQEN_RTE ? "enabled" : "disabled");
  973. v4l2_info(sd, "\tFIFO overrun: %s\n",
  974. stats & STATS_ROR ? "yes" : "no");
  975. v4l2_info(sd, "\tFIFO overrun interrupt: %s\n",
  976. irqen & IRQEN_ROE ? "enabled" : "disabled");
  977. v4l2_info(sd, "\tBusy: %s\n",
  978. stats & STATS_RBY ? "yes" : "no");
  979. v4l2_info(sd, "\tFIFO service requested: %s\n",
  980. stats & STATS_RSR ? "yes" : "no");
  981. v4l2_info(sd, "\tFIFO service request interrupt: %s\n",
  982. irqen & IRQEN_RSE ? "enabled" : "disabled");
  983. v4l2_info(sd, "IR Transmitter:\n");
  984. v4l2_info(sd, "\tEnabled: %s\n",
  985. cntrl & CNTRL_TXE ? "yes" : "no");
  986. v4l2_info(sd, "\tModulation onto a carrier: %s\n",
  987. cntrl & CNTRL_MOD ? "enabled" : "disabled");
  988. v4l2_info(sd, "\tFIFO: %s\n",
  989. cntrl & CNTRL_TFE ? "enabled" : "disabled");
  990. v4l2_info(sd, "\tFIFO interrupt watermark: %s\n",
  991. cntrl & CNTRL_TIC ? "not empty" : "half full or less");
  992. v4l2_info(sd, "\tCarrier polarity: %s\n",
  993. cntrl & CNTRL_CPL ? "space:burst mark:noburst"
  994. : "space:noburst mark:burst");
  995. if (cntrl & CNTRL_MOD) {
  996. v4l2_info(sd, "\tCarrier (16 clocks): %u Hz\n",
  997. clock_divider_to_carrier_freq(txclk));
  998. v4l2_info(sd, "\tCarrier duty cycle: %2u/16\n",
  999. cduty + 1);
  1000. }
  1001. v4l2_info(sd, "\tMax pulse width: %u us, %llu ns\n",
  1002. pulse_width_count_to_us(FIFO_RXTX, txclk),
  1003. pulse_width_count_to_ns(FIFO_RXTX, txclk));
  1004. v4l2_info(sd, "\tBusy: %s\n",
  1005. stats & STATS_TBY ? "yes" : "no");
  1006. v4l2_info(sd, "\tFIFO service requested: %s\n",
  1007. stats & STATS_TSR ? "yes" : "no");
  1008. v4l2_info(sd, "\tFIFO service request interrupt: %s\n",
  1009. irqen & IRQEN_TSE ? "enabled" : "disabled");
  1010. return 0;
  1011. }
  1012. const struct v4l2_subdev_ir_ops cx25840_ir_ops = {
  1013. .rx_read = cx25840_ir_rx_read,
  1014. .rx_g_parameters = cx25840_ir_rx_g_parameters,
  1015. .rx_s_parameters = cx25840_ir_rx_s_parameters,
  1016. .tx_write = cx25840_ir_tx_write,
  1017. .tx_g_parameters = cx25840_ir_tx_g_parameters,
  1018. .tx_s_parameters = cx25840_ir_tx_s_parameters,
  1019. };
  1020. static const struct v4l2_subdev_ir_parameters default_rx_params = {
  1021. .bytes_per_data_element = sizeof(union cx25840_ir_fifo_rec),
  1022. .mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH,
  1023. .enable = false,
  1024. .interrupt_enable = false,
  1025. .shutdown = true,
  1026. .modulation = true,
  1027. .carrier_freq = 36000, /* 36 kHz - RC-5, and RC-6 carrier */
  1028. /* RC-5: 666,667 ns = 1/36 kHz * 32 cycles * 1 mark * 0.75 */
  1029. /* RC-6: 333,333 ns = 1/36 kHz * 16 cycles * 1 mark * 0.75 */
  1030. .noise_filter_min_width = 333333, /* ns */
  1031. .carrier_range_lower = 35000,
  1032. .carrier_range_upper = 37000,
  1033. .invert_level = false,
  1034. };
  1035. static const struct v4l2_subdev_ir_parameters default_tx_params = {
  1036. .bytes_per_data_element = sizeof(union cx25840_ir_fifo_rec),
  1037. .mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH,
  1038. .enable = false,
  1039. .interrupt_enable = false,
  1040. .shutdown = true,
  1041. .modulation = true,
  1042. .carrier_freq = 36000, /* 36 kHz - RC-5 carrier */
  1043. .duty_cycle = 25, /* 25 % - RC-5 carrier */
  1044. .invert_level = false,
  1045. .invert_carrier_sense = false,
  1046. };
  1047. int cx25840_ir_probe(struct v4l2_subdev *sd)
  1048. {
  1049. struct cx25840_state *state = to_state(sd);
  1050. struct cx25840_ir_state *ir_state;
  1051. struct v4l2_subdev_ir_parameters default_params;
  1052. /* Only init the IR controller for the CX2388[57] AV Core for now */
  1053. if (!(is_cx23885(state) || is_cx23887(state)))
  1054. return 0;
  1055. ir_state = kzalloc(sizeof(struct cx25840_ir_state), GFP_KERNEL);
  1056. if (ir_state == NULL)
  1057. return -ENOMEM;
  1058. spin_lock_init(&ir_state->rx_kfifo_lock);
  1059. if (kfifo_alloc(&ir_state->rx_kfifo,
  1060. CX25840_IR_RX_KFIFO_SIZE, GFP_KERNEL)) {
  1061. kfree(ir_state);
  1062. return -ENOMEM;
  1063. }
  1064. ir_state->c = state->c;
  1065. state->ir_state = ir_state;
  1066. /* Ensure no interrupts arrive yet */
  1067. if (is_cx23885(state) || is_cx23887(state))
  1068. cx25840_write4(ir_state->c, CX25840_IR_IRQEN_REG, IRQEN_MSK);
  1069. else
  1070. cx25840_write4(ir_state->c, CX25840_IR_IRQEN_REG, 0);
  1071. mutex_init(&ir_state->rx_params_lock);
  1072. memcpy(&default_params, &default_rx_params,
  1073. sizeof(struct v4l2_subdev_ir_parameters));
  1074. v4l2_subdev_call(sd, ir, rx_s_parameters, &default_params);
  1075. mutex_init(&ir_state->tx_params_lock);
  1076. memcpy(&default_params, &default_tx_params,
  1077. sizeof(struct v4l2_subdev_ir_parameters));
  1078. v4l2_subdev_call(sd, ir, tx_s_parameters, &default_params);
  1079. return 0;
  1080. }
  1081. int cx25840_ir_remove(struct v4l2_subdev *sd)
  1082. {
  1083. struct cx25840_state *state = to_state(sd);
  1084. struct cx25840_ir_state *ir_state = to_ir_state(sd);
  1085. if (ir_state == NULL)
  1086. return -ENODEV;
  1087. cx25840_ir_rx_shutdown(sd);
  1088. cx25840_ir_tx_shutdown(sd);
  1089. kfifo_free(&ir_state->rx_kfifo);
  1090. kfree(ir_state);
  1091. state->ir_state = NULL;
  1092. return 0;
  1093. }