cx23888-ir.c 34 KB

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