cx23888-ir.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269
  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. 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. u32 id;
  117. u32 rev;
  118. struct v4l2_subdev_ir_parameters rx_params;
  119. struct mutex rx_params_lock;
  120. atomic_t rxclk_divider;
  121. atomic_t rx_invert;
  122. struct kfifo rx_kfifo;
  123. spinlock_t rx_kfifo_lock;
  124. struct v4l2_subdev_ir_parameters tx_params;
  125. struct mutex tx_params_lock;
  126. atomic_t txclk_divider;
  127. };
  128. static inline struct cx23888_ir_state *to_state(struct v4l2_subdev *sd)
  129. {
  130. return v4l2_get_subdevdata(sd);
  131. }
  132. /*
  133. * IR register block read and write functions
  134. */
  135. static
  136. inline int cx23888_ir_write4(struct cx23885_dev *dev, u32 addr, u32 value)
  137. {
  138. cx_write(addr, value);
  139. return 0;
  140. }
  141. static inline u32 cx23888_ir_read4(struct cx23885_dev *dev, u32 addr)
  142. {
  143. return cx_read(addr);
  144. }
  145. static inline int cx23888_ir_and_or4(struct cx23885_dev *dev, u32 addr,
  146. u32 and_mask, u32 or_value)
  147. {
  148. cx_andor(addr, ~and_mask, or_value);
  149. return 0;
  150. }
  151. /*
  152. * Rx and Tx Clock Divider register computations
  153. *
  154. * Note the largest clock divider value of 0xffff corresponds to:
  155. * (0xffff + 1) * 1000 / 108/2 MHz = 1,213,629.629... ns
  156. * which fits in 21 bits, so we'll use unsigned int for time arguments.
  157. */
  158. static inline u16 count_to_clock_divider(unsigned int d)
  159. {
  160. if (d > RXCLK_RCD + 1)
  161. d = RXCLK_RCD;
  162. else if (d < 2)
  163. d = 1;
  164. else
  165. d--;
  166. return (u16) d;
  167. }
  168. static inline u16 ns_to_clock_divider(unsigned int ns)
  169. {
  170. return count_to_clock_divider(
  171. DIV_ROUND_CLOSEST(CX23888_IR_REFCLK_FREQ / 1000000 * ns, 1000));
  172. }
  173. static inline unsigned int clock_divider_to_ns(unsigned int divider)
  174. {
  175. /* Period of the Rx or Tx clock in ns */
  176. return DIV_ROUND_CLOSEST((divider + 1) * 1000,
  177. CX23888_IR_REFCLK_FREQ / 1000000);
  178. }
  179. static inline u16 carrier_freq_to_clock_divider(unsigned int freq)
  180. {
  181. return count_to_clock_divider(
  182. DIV_ROUND_CLOSEST(CX23888_IR_REFCLK_FREQ, freq * 16));
  183. }
  184. static inline unsigned int clock_divider_to_carrier_freq(unsigned int divider)
  185. {
  186. return DIV_ROUND_CLOSEST(CX23888_IR_REFCLK_FREQ, (divider + 1) * 16);
  187. }
  188. static inline u16 freq_to_clock_divider(unsigned int freq,
  189. unsigned int rollovers)
  190. {
  191. return count_to_clock_divider(
  192. DIV_ROUND_CLOSEST(CX23888_IR_REFCLK_FREQ, freq * rollovers));
  193. }
  194. static inline unsigned int clock_divider_to_freq(unsigned int divider,
  195. unsigned int rollovers)
  196. {
  197. return DIV_ROUND_CLOSEST(CX23888_IR_REFCLK_FREQ,
  198. (divider + 1) * rollovers);
  199. }
  200. /*
  201. * Low Pass Filter register calculations
  202. *
  203. * Note the largest count value of 0xffff corresponds to:
  204. * 0xffff * 1000 / 108/2 MHz = 1,213,611.11... ns
  205. * which fits in 21 bits, so we'll use unsigned int for time arguments.
  206. */
  207. static inline u16 count_to_lpf_count(unsigned int d)
  208. {
  209. if (d > FILTR_LPF)
  210. d = FILTR_LPF;
  211. else if (d < 4)
  212. d = 0;
  213. return (u16) d;
  214. }
  215. static inline u16 ns_to_lpf_count(unsigned int ns)
  216. {
  217. return count_to_lpf_count(
  218. DIV_ROUND_CLOSEST(CX23888_IR_REFCLK_FREQ / 1000000 * ns, 1000));
  219. }
  220. static inline unsigned int lpf_count_to_ns(unsigned int count)
  221. {
  222. /* Duration of the Low Pass Filter rejection window in ns */
  223. return DIV_ROUND_CLOSEST(count * 1000,
  224. CX23888_IR_REFCLK_FREQ / 1000000);
  225. }
  226. static inline unsigned int lpf_count_to_us(unsigned int count)
  227. {
  228. /* Duration of the Low Pass Filter rejection window in us */
  229. return DIV_ROUND_CLOSEST(count, CX23888_IR_REFCLK_FREQ / 1000000);
  230. }
  231. /*
  232. * FIFO register pulse width count compuations
  233. */
  234. static u32 clock_divider_to_resolution(u16 divider)
  235. {
  236. /*
  237. * Resolution is the duration of 1 tick of the readable portion of
  238. * of the pulse width counter as read from the FIFO. The two lsb's are
  239. * not readable, hence the << 2. This function returns ns.
  240. */
  241. return DIV_ROUND_CLOSEST((1 << 2) * ((u32) divider + 1) * 1000,
  242. CX23888_IR_REFCLK_FREQ / 1000000);
  243. }
  244. static u64 pulse_width_count_to_ns(u16 count, u16 divider)
  245. {
  246. u64 n;
  247. u32 rem;
  248. /*
  249. * The 2 lsb's of the pulse width timer count are not readable, hence
  250. * the (count << 2) | 0x3
  251. */
  252. n = (((u64) count << 2) | 0x3) * (divider + 1) * 1000; /* millicycles */
  253. rem = do_div(n, CX23888_IR_REFCLK_FREQ / 1000000); /* / MHz => ns */
  254. if (rem >= CX23888_IR_REFCLK_FREQ / 1000000 / 2)
  255. n++;
  256. return n;
  257. }
  258. static unsigned int pulse_width_count_to_us(u16 count, u16 divider)
  259. {
  260. u64 n;
  261. u32 rem;
  262. /*
  263. * The 2 lsb's of the pulse width timer count are not readable, hence
  264. * the (count << 2) | 0x3
  265. */
  266. n = (((u64) count << 2) | 0x3) * (divider + 1); /* cycles */
  267. rem = do_div(n, CX23888_IR_REFCLK_FREQ / 1000000); /* / MHz => us */
  268. if (rem >= CX23888_IR_REFCLK_FREQ / 1000000 / 2)
  269. n++;
  270. return (unsigned int) n;
  271. }
  272. /*
  273. * Pulse Clocks computations: Combined Pulse Width Count & Rx Clock Counts
  274. *
  275. * The total pulse clock count is an 18 bit pulse width timer count as the most
  276. * significant part and (up to) 16 bit clock divider count as a modulus.
  277. * When the Rx clock divider ticks down to 0, it increments the 18 bit pulse
  278. * width timer count's least significant bit.
  279. */
  280. static u64 ns_to_pulse_clocks(u32 ns)
  281. {
  282. u64 clocks;
  283. u32 rem;
  284. clocks = CX23888_IR_REFCLK_FREQ / 1000000 * (u64) ns; /* millicycles */
  285. rem = do_div(clocks, 1000); /* /1000 = cycles */
  286. if (rem >= 1000 / 2)
  287. clocks++;
  288. return clocks;
  289. }
  290. static u16 pulse_clocks_to_clock_divider(u64 count)
  291. {
  292. u32 rem;
  293. rem = 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;
  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. v4l2_dbg(2, ir_888_debug, sd, "rx read: end of rx\n");
  609. } else {
  610. u = (p->hw_fifo_data & FIFO_RXTX_LVL) ? 1 : 0;
  611. if (invert)
  612. u = u ? 0 : 1;
  613. }
  614. v = (unsigned) pulse_width_count_to_ns(
  615. (u16) (p->hw_fifo_data & FIFO_RXTX), divider);
  616. if (v > IR_MAX_DURATION)
  617. v = IR_MAX_DURATION;
  618. init_ir_raw_event(&p->ir_core_data);
  619. p->ir_core_data.pulse = u;
  620. p->ir_core_data.duration = v;
  621. v4l2_dbg(2, ir_888_debug, sd, "rx read: %10u ns %s\n",
  622. v, u ? "mark" : "space");
  623. }
  624. return 0;
  625. }
  626. static int cx23888_ir_rx_g_parameters(struct v4l2_subdev *sd,
  627. struct v4l2_subdev_ir_parameters *p)
  628. {
  629. struct cx23888_ir_state *state = to_state(sd);
  630. mutex_lock(&state->rx_params_lock);
  631. memcpy(p, &state->rx_params, sizeof(struct v4l2_subdev_ir_parameters));
  632. mutex_unlock(&state->rx_params_lock);
  633. return 0;
  634. }
  635. static int cx23888_ir_rx_shutdown(struct v4l2_subdev *sd)
  636. {
  637. struct cx23888_ir_state *state = to_state(sd);
  638. struct cx23885_dev *dev = state->dev;
  639. mutex_lock(&state->rx_params_lock);
  640. /* Disable or slow down all IR Rx circuits and counters */
  641. irqenable_rx(dev, 0);
  642. control_rx_enable(dev, false);
  643. control_rx_demodulation_enable(dev, false);
  644. control_rx_s_edge_detection(dev, CNTRL_EDG_NONE);
  645. filter_rx_s_min_width(dev, 0);
  646. cx23888_ir_write4(dev, CX23888_IR_RXCLK_REG, RXCLK_RCD);
  647. state->rx_params.shutdown = true;
  648. mutex_unlock(&state->rx_params_lock);
  649. return 0;
  650. }
  651. static int cx23888_ir_rx_s_parameters(struct v4l2_subdev *sd,
  652. struct v4l2_subdev_ir_parameters *p)
  653. {
  654. struct cx23888_ir_state *state = to_state(sd);
  655. struct cx23885_dev *dev = state->dev;
  656. struct v4l2_subdev_ir_parameters *o = &state->rx_params;
  657. u16 rxclk_divider;
  658. if (p->shutdown)
  659. return cx23888_ir_rx_shutdown(sd);
  660. if (p->mode != V4L2_SUBDEV_IR_MODE_PULSE_WIDTH)
  661. return -ENOSYS;
  662. mutex_lock(&state->rx_params_lock);
  663. o->shutdown = p->shutdown;
  664. o->mode = p->mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH;
  665. o->bytes_per_data_element = p->bytes_per_data_element
  666. = sizeof(union cx23888_ir_fifo_rec);
  667. /* Before we tweak the hardware, we have to disable the receiver */
  668. irqenable_rx(dev, 0);
  669. control_rx_enable(dev, false);
  670. control_rx_demodulation_enable(dev, p->modulation);
  671. o->modulation = p->modulation;
  672. if (p->modulation) {
  673. p->carrier_freq = rxclk_rx_s_carrier(dev, p->carrier_freq,
  674. &rxclk_divider);
  675. o->carrier_freq = p->carrier_freq;
  676. o->duty_cycle = p->duty_cycle = 50;
  677. control_rx_s_carrier_window(dev, p->carrier_freq,
  678. &p->carrier_range_lower,
  679. &p->carrier_range_upper);
  680. o->carrier_range_lower = p->carrier_range_lower;
  681. o->carrier_range_upper = p->carrier_range_upper;
  682. p->max_pulse_width =
  683. (u32) pulse_width_count_to_ns(FIFO_RXTX, rxclk_divider);
  684. } else {
  685. p->max_pulse_width =
  686. rxclk_rx_s_max_pulse_width(dev, p->max_pulse_width,
  687. &rxclk_divider);
  688. }
  689. o->max_pulse_width = p->max_pulse_width;
  690. atomic_set(&state->rxclk_divider, rxclk_divider);
  691. p->noise_filter_min_width =
  692. filter_rx_s_min_width(dev, p->noise_filter_min_width);
  693. o->noise_filter_min_width = p->noise_filter_min_width;
  694. p->resolution = clock_divider_to_resolution(rxclk_divider);
  695. o->resolution = p->resolution;
  696. /* FIXME - make this dependent on resolution for better performance */
  697. control_rx_irq_watermark(dev, RX_FIFO_HALF_FULL);
  698. control_rx_s_edge_detection(dev, CNTRL_EDG_BOTH);
  699. o->invert_level = p->invert_level;
  700. atomic_set(&state->rx_invert, p->invert_level);
  701. o->interrupt_enable = p->interrupt_enable;
  702. o->enable = p->enable;
  703. if (p->enable) {
  704. unsigned long flags;
  705. spin_lock_irqsave(&state->rx_kfifo_lock, flags);
  706. kfifo_reset(&state->rx_kfifo);
  707. /* reset tx_fifo too if there is one... */
  708. spin_unlock_irqrestore(&state->rx_kfifo_lock, flags);
  709. if (p->interrupt_enable)
  710. irqenable_rx(dev, IRQEN_RSE | IRQEN_RTE | IRQEN_ROE);
  711. control_rx_enable(dev, p->enable);
  712. }
  713. mutex_unlock(&state->rx_params_lock);
  714. return 0;
  715. }
  716. /* Transmitter */
  717. static int cx23888_ir_tx_write(struct v4l2_subdev *sd, u8 *buf, size_t count,
  718. ssize_t *num)
  719. {
  720. struct cx23888_ir_state *state = to_state(sd);
  721. struct cx23885_dev *dev = state->dev;
  722. /* For now enable the Tx FIFO Service interrupt & pretend we did work */
  723. irqenable_tx(dev, IRQEN_TSE);
  724. *num = count;
  725. return 0;
  726. }
  727. static int cx23888_ir_tx_g_parameters(struct v4l2_subdev *sd,
  728. struct v4l2_subdev_ir_parameters *p)
  729. {
  730. struct cx23888_ir_state *state = to_state(sd);
  731. mutex_lock(&state->tx_params_lock);
  732. memcpy(p, &state->tx_params, sizeof(struct v4l2_subdev_ir_parameters));
  733. mutex_unlock(&state->tx_params_lock);
  734. return 0;
  735. }
  736. static int cx23888_ir_tx_shutdown(struct v4l2_subdev *sd)
  737. {
  738. struct cx23888_ir_state *state = to_state(sd);
  739. struct cx23885_dev *dev = state->dev;
  740. mutex_lock(&state->tx_params_lock);
  741. /* Disable or slow down all IR Tx circuits and counters */
  742. irqenable_tx(dev, 0);
  743. control_tx_enable(dev, false);
  744. control_tx_modulation_enable(dev, false);
  745. cx23888_ir_write4(dev, CX23888_IR_TXCLK_REG, TXCLK_TCD);
  746. state->tx_params.shutdown = true;
  747. mutex_unlock(&state->tx_params_lock);
  748. return 0;
  749. }
  750. static int cx23888_ir_tx_s_parameters(struct v4l2_subdev *sd,
  751. struct v4l2_subdev_ir_parameters *p)
  752. {
  753. struct cx23888_ir_state *state = to_state(sd);
  754. struct cx23885_dev *dev = state->dev;
  755. struct v4l2_subdev_ir_parameters *o = &state->tx_params;
  756. u16 txclk_divider;
  757. if (p->shutdown)
  758. return cx23888_ir_tx_shutdown(sd);
  759. if (p->mode != V4L2_SUBDEV_IR_MODE_PULSE_WIDTH)
  760. return -ENOSYS;
  761. mutex_lock(&state->tx_params_lock);
  762. o->shutdown = p->shutdown;
  763. o->mode = p->mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH;
  764. o->bytes_per_data_element = p->bytes_per_data_element
  765. = sizeof(union cx23888_ir_fifo_rec);
  766. /* Before we tweak the hardware, we have to disable the transmitter */
  767. irqenable_tx(dev, 0);
  768. control_tx_enable(dev, false);
  769. control_tx_modulation_enable(dev, p->modulation);
  770. o->modulation = p->modulation;
  771. if (p->modulation) {
  772. p->carrier_freq = txclk_tx_s_carrier(dev, p->carrier_freq,
  773. &txclk_divider);
  774. o->carrier_freq = p->carrier_freq;
  775. p->duty_cycle = cduty_tx_s_duty_cycle(dev, p->duty_cycle);
  776. o->duty_cycle = p->duty_cycle;
  777. p->max_pulse_width =
  778. (u32) pulse_width_count_to_ns(FIFO_RXTX, txclk_divider);
  779. } else {
  780. p->max_pulse_width =
  781. txclk_tx_s_max_pulse_width(dev, p->max_pulse_width,
  782. &txclk_divider);
  783. }
  784. o->max_pulse_width = p->max_pulse_width;
  785. atomic_set(&state->txclk_divider, txclk_divider);
  786. p->resolution = clock_divider_to_resolution(txclk_divider);
  787. o->resolution = p->resolution;
  788. /* FIXME - make this dependent on resolution for better performance */
  789. control_tx_irq_watermark(dev, TX_FIFO_HALF_EMPTY);
  790. control_tx_polarity_invert(dev, p->invert_carrier_sense);
  791. o->invert_carrier_sense = p->invert_carrier_sense;
  792. control_tx_level_invert(dev, p->invert_level);
  793. o->invert_level = p->invert_level;
  794. o->interrupt_enable = p->interrupt_enable;
  795. o->enable = p->enable;
  796. if (p->enable) {
  797. if (p->interrupt_enable)
  798. irqenable_tx(dev, IRQEN_TSE);
  799. control_tx_enable(dev, p->enable);
  800. }
  801. mutex_unlock(&state->tx_params_lock);
  802. return 0;
  803. }
  804. /*
  805. * V4L2 Subdevice Core Ops
  806. */
  807. static int cx23888_ir_log_status(struct v4l2_subdev *sd)
  808. {
  809. struct cx23888_ir_state *state = to_state(sd);
  810. struct cx23885_dev *dev = state->dev;
  811. char *s;
  812. int i, j;
  813. u32 cntrl = cx23888_ir_read4(dev, CX23888_IR_CNTRL_REG);
  814. u32 txclk = cx23888_ir_read4(dev, CX23888_IR_TXCLK_REG) & TXCLK_TCD;
  815. u32 rxclk = cx23888_ir_read4(dev, CX23888_IR_RXCLK_REG) & RXCLK_RCD;
  816. u32 cduty = cx23888_ir_read4(dev, CX23888_IR_CDUTY_REG) & CDUTY_CDC;
  817. u32 stats = cx23888_ir_read4(dev, CX23888_IR_STATS_REG);
  818. u32 irqen = cx23888_ir_read4(dev, CX23888_IR_IRQEN_REG);
  819. u32 filtr = cx23888_ir_read4(dev, CX23888_IR_FILTR_REG) & FILTR_LPF;
  820. v4l2_info(sd, "IR Receiver:\n");
  821. v4l2_info(sd, "\tEnabled: %s\n",
  822. cntrl & CNTRL_RXE ? "yes" : "no");
  823. v4l2_info(sd, "\tDemodulation from a carrier: %s\n",
  824. cntrl & CNTRL_DMD ? "enabled" : "disabled");
  825. v4l2_info(sd, "\tFIFO: %s\n",
  826. cntrl & CNTRL_RFE ? "enabled" : "disabled");
  827. switch (cntrl & CNTRL_EDG) {
  828. case CNTRL_EDG_NONE:
  829. s = "disabled";
  830. break;
  831. case CNTRL_EDG_FALL:
  832. s = "falling edge";
  833. break;
  834. case CNTRL_EDG_RISE:
  835. s = "rising edge";
  836. break;
  837. case CNTRL_EDG_BOTH:
  838. s = "rising & falling edges";
  839. break;
  840. default:
  841. s = "??? edge";
  842. break;
  843. }
  844. v4l2_info(sd, "\tPulse timers' start/stop trigger: %s\n", s);
  845. v4l2_info(sd, "\tFIFO data on pulse timer overflow: %s\n",
  846. cntrl & CNTRL_R ? "not loaded" : "overflow marker");
  847. v4l2_info(sd, "\tFIFO interrupt watermark: %s\n",
  848. cntrl & CNTRL_RIC ? "not empty" : "half full or greater");
  849. v4l2_info(sd, "\tLoopback mode: %s\n",
  850. cntrl & CNTRL_LBM ? "loopback active" : "normal receive");
  851. if (cntrl & CNTRL_DMD) {
  852. v4l2_info(sd, "\tExpected carrier (16 clocks): %u Hz\n",
  853. clock_divider_to_carrier_freq(rxclk));
  854. switch (cntrl & CNTRL_WIN) {
  855. case CNTRL_WIN_3_3:
  856. i = 3;
  857. j = 3;
  858. break;
  859. case CNTRL_WIN_4_3:
  860. i = 4;
  861. j = 3;
  862. break;
  863. case CNTRL_WIN_3_4:
  864. i = 3;
  865. j = 4;
  866. break;
  867. case CNTRL_WIN_4_4:
  868. i = 4;
  869. j = 4;
  870. break;
  871. default:
  872. i = 0;
  873. j = 0;
  874. break;
  875. }
  876. v4l2_info(sd, "\tNext carrier edge window: 16 clocks "
  877. "-%1d/+%1d, %u to %u Hz\n", i, j,
  878. clock_divider_to_freq(rxclk, 16 + j),
  879. clock_divider_to_freq(rxclk, 16 - i));
  880. }
  881. v4l2_info(sd, "\tMax measurable pulse width: %u us, %llu ns\n",
  882. pulse_width_count_to_us(FIFO_RXTX, rxclk),
  883. pulse_width_count_to_ns(FIFO_RXTX, rxclk));
  884. v4l2_info(sd, "\tLow pass filter: %s\n",
  885. filtr ? "enabled" : "disabled");
  886. if (filtr)
  887. v4l2_info(sd, "\tMin acceptable pulse width (LPF): %u us, "
  888. "%u ns\n",
  889. lpf_count_to_us(filtr),
  890. lpf_count_to_ns(filtr));
  891. v4l2_info(sd, "\tPulse width timer timed-out: %s\n",
  892. stats & STATS_RTO ? "yes" : "no");
  893. v4l2_info(sd, "\tPulse width timer time-out intr: %s\n",
  894. irqen & IRQEN_RTE ? "enabled" : "disabled");
  895. v4l2_info(sd, "\tFIFO overrun: %s\n",
  896. stats & STATS_ROR ? "yes" : "no");
  897. v4l2_info(sd, "\tFIFO overrun interrupt: %s\n",
  898. irqen & IRQEN_ROE ? "enabled" : "disabled");
  899. v4l2_info(sd, "\tBusy: %s\n",
  900. stats & STATS_RBY ? "yes" : "no");
  901. v4l2_info(sd, "\tFIFO service requested: %s\n",
  902. stats & STATS_RSR ? "yes" : "no");
  903. v4l2_info(sd, "\tFIFO service request interrupt: %s\n",
  904. irqen & IRQEN_RSE ? "enabled" : "disabled");
  905. v4l2_info(sd, "IR Transmitter:\n");
  906. v4l2_info(sd, "\tEnabled: %s\n",
  907. cntrl & CNTRL_TXE ? "yes" : "no");
  908. v4l2_info(sd, "\tModulation onto a carrier: %s\n",
  909. cntrl & CNTRL_MOD ? "enabled" : "disabled");
  910. v4l2_info(sd, "\tFIFO: %s\n",
  911. cntrl & CNTRL_TFE ? "enabled" : "disabled");
  912. v4l2_info(sd, "\tFIFO interrupt watermark: %s\n",
  913. cntrl & CNTRL_TIC ? "not empty" : "half full or less");
  914. v4l2_info(sd, "\tOutput pin level inversion %s\n",
  915. cntrl & CNTRL_IVO ? "yes" : "no");
  916. v4l2_info(sd, "\tCarrier polarity: %s\n",
  917. cntrl & CNTRL_CPL ? "space:burst mark:noburst"
  918. : "space:noburst mark:burst");
  919. if (cntrl & CNTRL_MOD) {
  920. v4l2_info(sd, "\tCarrier (16 clocks): %u Hz\n",
  921. clock_divider_to_carrier_freq(txclk));
  922. v4l2_info(sd, "\tCarrier duty cycle: %2u/16\n",
  923. cduty + 1);
  924. }
  925. v4l2_info(sd, "\tMax pulse width: %u us, %llu ns\n",
  926. pulse_width_count_to_us(FIFO_RXTX, txclk),
  927. pulse_width_count_to_ns(FIFO_RXTX, txclk));
  928. v4l2_info(sd, "\tBusy: %s\n",
  929. stats & STATS_TBY ? "yes" : "no");
  930. v4l2_info(sd, "\tFIFO service requested: %s\n",
  931. stats & STATS_TSR ? "yes" : "no");
  932. v4l2_info(sd, "\tFIFO service request interrupt: %s\n",
  933. irqen & IRQEN_TSE ? "enabled" : "disabled");
  934. return 0;
  935. }
  936. static inline int cx23888_ir_dbg_match(const struct v4l2_dbg_match *match)
  937. {
  938. return match->type == V4L2_CHIP_MATCH_HOST && match->addr == 2;
  939. }
  940. static int cx23888_ir_g_chip_ident(struct v4l2_subdev *sd,
  941. struct v4l2_dbg_chip_ident *chip)
  942. {
  943. struct cx23888_ir_state *state = to_state(sd);
  944. if (cx23888_ir_dbg_match(&chip->match)) {
  945. chip->ident = state->id;
  946. chip->revision = state->rev;
  947. }
  948. return 0;
  949. }
  950. #ifdef CONFIG_VIDEO_ADV_DEBUG
  951. static int cx23888_ir_g_register(struct v4l2_subdev *sd,
  952. struct v4l2_dbg_register *reg)
  953. {
  954. struct cx23888_ir_state *state = to_state(sd);
  955. u32 addr = CX23888_IR_REG_BASE + (u32) reg->reg;
  956. if (!cx23888_ir_dbg_match(&reg->match))
  957. return -EINVAL;
  958. if ((addr & 0x3) != 0)
  959. return -EINVAL;
  960. if (addr < CX23888_IR_CNTRL_REG || addr > CX23888_IR_LEARN_REG)
  961. return -EINVAL;
  962. if (!capable(CAP_SYS_ADMIN))
  963. return -EPERM;
  964. reg->size = 4;
  965. reg->val = cx23888_ir_read4(state->dev, addr);
  966. return 0;
  967. }
  968. static int cx23888_ir_s_register(struct v4l2_subdev *sd,
  969. struct v4l2_dbg_register *reg)
  970. {
  971. struct cx23888_ir_state *state = to_state(sd);
  972. u32 addr = CX23888_IR_REG_BASE + (u32) reg->reg;
  973. if (!cx23888_ir_dbg_match(&reg->match))
  974. return -EINVAL;
  975. if ((addr & 0x3) != 0)
  976. return -EINVAL;
  977. if (addr < CX23888_IR_CNTRL_REG || addr > CX23888_IR_LEARN_REG)
  978. return -EINVAL;
  979. if (!capable(CAP_SYS_ADMIN))
  980. return -EPERM;
  981. cx23888_ir_write4(state->dev, addr, reg->val);
  982. return 0;
  983. }
  984. #endif
  985. static const struct v4l2_subdev_core_ops cx23888_ir_core_ops = {
  986. .g_chip_ident = cx23888_ir_g_chip_ident,
  987. .log_status = cx23888_ir_log_status,
  988. #ifdef CONFIG_VIDEO_ADV_DEBUG
  989. .g_register = cx23888_ir_g_register,
  990. .s_register = cx23888_ir_s_register,
  991. #endif
  992. .interrupt_service_routine = cx23888_ir_irq_handler,
  993. };
  994. static const struct v4l2_subdev_ir_ops cx23888_ir_ir_ops = {
  995. .rx_read = cx23888_ir_rx_read,
  996. .rx_g_parameters = cx23888_ir_rx_g_parameters,
  997. .rx_s_parameters = cx23888_ir_rx_s_parameters,
  998. .tx_write = cx23888_ir_tx_write,
  999. .tx_g_parameters = cx23888_ir_tx_g_parameters,
  1000. .tx_s_parameters = cx23888_ir_tx_s_parameters,
  1001. };
  1002. static const struct v4l2_subdev_ops cx23888_ir_controller_ops = {
  1003. .core = &cx23888_ir_core_ops,
  1004. .ir = &cx23888_ir_ir_ops,
  1005. };
  1006. static const struct v4l2_subdev_ir_parameters default_rx_params = {
  1007. .bytes_per_data_element = sizeof(union cx23888_ir_fifo_rec),
  1008. .mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH,
  1009. .enable = false,
  1010. .interrupt_enable = false,
  1011. .shutdown = true,
  1012. .modulation = true,
  1013. .carrier_freq = 36000, /* 36 kHz - RC-5, RC-6, and RC-6A carrier */
  1014. /* RC-5: 666,667 ns = 1/36 kHz * 32 cycles * 1 mark * 0.75 */
  1015. /* RC-6A: 333,333 ns = 1/36 kHz * 16 cycles * 1 mark * 0.75 */
  1016. .noise_filter_min_width = 333333, /* ns */
  1017. .carrier_range_lower = 35000,
  1018. .carrier_range_upper = 37000,
  1019. .invert_level = false,
  1020. };
  1021. static const struct v4l2_subdev_ir_parameters default_tx_params = {
  1022. .bytes_per_data_element = sizeof(union cx23888_ir_fifo_rec),
  1023. .mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH,
  1024. .enable = false,
  1025. .interrupt_enable = false,
  1026. .shutdown = true,
  1027. .modulation = true,
  1028. .carrier_freq = 36000, /* 36 kHz - RC-5 carrier */
  1029. .duty_cycle = 25, /* 25 % - RC-5 carrier */
  1030. .invert_level = false,
  1031. .invert_carrier_sense = false,
  1032. };
  1033. int cx23888_ir_probe(struct cx23885_dev *dev)
  1034. {
  1035. struct cx23888_ir_state *state;
  1036. struct v4l2_subdev *sd;
  1037. struct v4l2_subdev_ir_parameters default_params;
  1038. int ret;
  1039. state = kzalloc(sizeof(struct cx23888_ir_state), GFP_KERNEL);
  1040. if (state == NULL)
  1041. return -ENOMEM;
  1042. spin_lock_init(&state->rx_kfifo_lock);
  1043. if (kfifo_alloc(&state->rx_kfifo, CX23888_IR_RX_KFIFO_SIZE, GFP_KERNEL))
  1044. return -ENOMEM;
  1045. state->dev = dev;
  1046. state->id = V4L2_IDENT_CX23888_IR;
  1047. state->rev = 0;
  1048. sd = &state->sd;
  1049. v4l2_subdev_init(sd, &cx23888_ir_controller_ops);
  1050. v4l2_set_subdevdata(sd, state);
  1051. /* FIXME - fix the formatting of dev->v4l2_dev.name and use it */
  1052. snprintf(sd->name, sizeof(sd->name), "%s/888-ir", dev->name);
  1053. sd->grp_id = CX23885_HW_888_IR;
  1054. ret = v4l2_device_register_subdev(&dev->v4l2_dev, sd);
  1055. if (ret == 0) {
  1056. /*
  1057. * Ensure no interrupts arrive from '888 specific conditions,
  1058. * since we ignore them in this driver to have commonality with
  1059. * similar IR controller cores.
  1060. */
  1061. cx23888_ir_write4(dev, CX23888_IR_IRQEN_REG, 0);
  1062. mutex_init(&state->rx_params_lock);
  1063. memcpy(&default_params, &default_rx_params,
  1064. sizeof(struct v4l2_subdev_ir_parameters));
  1065. v4l2_subdev_call(sd, ir, rx_s_parameters, &default_params);
  1066. mutex_init(&state->tx_params_lock);
  1067. memcpy(&default_params, &default_tx_params,
  1068. sizeof(struct v4l2_subdev_ir_parameters));
  1069. v4l2_subdev_call(sd, ir, tx_s_parameters, &default_params);
  1070. } else {
  1071. kfifo_free(&state->rx_kfifo);
  1072. }
  1073. return ret;
  1074. }
  1075. int cx23888_ir_remove(struct cx23885_dev *dev)
  1076. {
  1077. struct v4l2_subdev *sd;
  1078. struct cx23888_ir_state *state;
  1079. sd = cx23885_find_hw(dev, CX23885_HW_888_IR);
  1080. if (sd == NULL)
  1081. return -ENODEV;
  1082. cx23888_ir_rx_shutdown(sd);
  1083. cx23888_ir_tx_shutdown(sd);
  1084. state = to_state(sd);
  1085. v4l2_device_unregister_subdev(sd);
  1086. kfifo_free(&state->rx_kfifo);
  1087. kfree(state);
  1088. /* Nothing more to free() as state held the actual v4l2_subdev object */
  1089. return 0;
  1090. }