cx23888-ir.c 35 KB

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