cx23888-ir.c 35 KB

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