cx23888-ir.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268
  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/ir-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. p->ir_core_data.pulse = u;
  619. p->ir_core_data.duration = v;
  620. v4l2_dbg(2, ir_888_debug, sd, "rx read: %10u ns %s\n",
  621. v, u ? "mark" : "space");
  622. }
  623. return 0;
  624. }
  625. static int cx23888_ir_rx_g_parameters(struct v4l2_subdev *sd,
  626. struct v4l2_subdev_ir_parameters *p)
  627. {
  628. struct cx23888_ir_state *state = to_state(sd);
  629. mutex_lock(&state->rx_params_lock);
  630. memcpy(p, &state->rx_params, sizeof(struct v4l2_subdev_ir_parameters));
  631. mutex_unlock(&state->rx_params_lock);
  632. return 0;
  633. }
  634. static int cx23888_ir_rx_shutdown(struct v4l2_subdev *sd)
  635. {
  636. struct cx23888_ir_state *state = to_state(sd);
  637. struct cx23885_dev *dev = state->dev;
  638. mutex_lock(&state->rx_params_lock);
  639. /* Disable or slow down all IR Rx circuits and counters */
  640. irqenable_rx(dev, 0);
  641. control_rx_enable(dev, false);
  642. control_rx_demodulation_enable(dev, false);
  643. control_rx_s_edge_detection(dev, CNTRL_EDG_NONE);
  644. filter_rx_s_min_width(dev, 0);
  645. cx23888_ir_write4(dev, CX23888_IR_RXCLK_REG, RXCLK_RCD);
  646. state->rx_params.shutdown = true;
  647. mutex_unlock(&state->rx_params_lock);
  648. return 0;
  649. }
  650. static int cx23888_ir_rx_s_parameters(struct v4l2_subdev *sd,
  651. struct v4l2_subdev_ir_parameters *p)
  652. {
  653. struct cx23888_ir_state *state = to_state(sd);
  654. struct cx23885_dev *dev = state->dev;
  655. struct v4l2_subdev_ir_parameters *o = &state->rx_params;
  656. u16 rxclk_divider;
  657. if (p->shutdown)
  658. return cx23888_ir_rx_shutdown(sd);
  659. if (p->mode != V4L2_SUBDEV_IR_MODE_PULSE_WIDTH)
  660. return -ENOSYS;
  661. mutex_lock(&state->rx_params_lock);
  662. o->shutdown = p->shutdown;
  663. o->mode = p->mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH;
  664. o->bytes_per_data_element = p->bytes_per_data_element
  665. = sizeof(union cx23888_ir_fifo_rec);
  666. /* Before we tweak the hardware, we have to disable the receiver */
  667. irqenable_rx(dev, 0);
  668. control_rx_enable(dev, false);
  669. control_rx_demodulation_enable(dev, p->modulation);
  670. o->modulation = p->modulation;
  671. if (p->modulation) {
  672. p->carrier_freq = rxclk_rx_s_carrier(dev, p->carrier_freq,
  673. &rxclk_divider);
  674. o->carrier_freq = p->carrier_freq;
  675. o->duty_cycle = p->duty_cycle = 50;
  676. control_rx_s_carrier_window(dev, p->carrier_freq,
  677. &p->carrier_range_lower,
  678. &p->carrier_range_upper);
  679. o->carrier_range_lower = p->carrier_range_lower;
  680. o->carrier_range_upper = p->carrier_range_upper;
  681. p->max_pulse_width =
  682. (u32) pulse_width_count_to_ns(FIFO_RXTX, rxclk_divider);
  683. } else {
  684. p->max_pulse_width =
  685. rxclk_rx_s_max_pulse_width(dev, p->max_pulse_width,
  686. &rxclk_divider);
  687. }
  688. o->max_pulse_width = p->max_pulse_width;
  689. atomic_set(&state->rxclk_divider, rxclk_divider);
  690. p->noise_filter_min_width =
  691. filter_rx_s_min_width(dev, p->noise_filter_min_width);
  692. o->noise_filter_min_width = p->noise_filter_min_width;
  693. p->resolution = clock_divider_to_resolution(rxclk_divider);
  694. o->resolution = p->resolution;
  695. /* FIXME - make this dependent on resolution for better performance */
  696. control_rx_irq_watermark(dev, RX_FIFO_HALF_FULL);
  697. control_rx_s_edge_detection(dev, CNTRL_EDG_BOTH);
  698. o->invert_level = p->invert_level;
  699. atomic_set(&state->rx_invert, p->invert_level);
  700. o->interrupt_enable = p->interrupt_enable;
  701. o->enable = p->enable;
  702. if (p->enable) {
  703. unsigned long flags;
  704. spin_lock_irqsave(&state->rx_kfifo_lock, flags);
  705. kfifo_reset(&state->rx_kfifo);
  706. /* reset tx_fifo too if there is one... */
  707. spin_unlock_irqrestore(&state->rx_kfifo_lock, flags);
  708. if (p->interrupt_enable)
  709. irqenable_rx(dev, IRQEN_RSE | IRQEN_RTE | IRQEN_ROE);
  710. control_rx_enable(dev, p->enable);
  711. }
  712. mutex_unlock(&state->rx_params_lock);
  713. return 0;
  714. }
  715. /* Transmitter */
  716. static int cx23888_ir_tx_write(struct v4l2_subdev *sd, u8 *buf, size_t count,
  717. ssize_t *num)
  718. {
  719. struct cx23888_ir_state *state = to_state(sd);
  720. struct cx23885_dev *dev = state->dev;
  721. /* For now enable the Tx FIFO Service interrupt & pretend we did work */
  722. irqenable_tx(dev, IRQEN_TSE);
  723. *num = count;
  724. return 0;
  725. }
  726. static int cx23888_ir_tx_g_parameters(struct v4l2_subdev *sd,
  727. struct v4l2_subdev_ir_parameters *p)
  728. {
  729. struct cx23888_ir_state *state = to_state(sd);
  730. mutex_lock(&state->tx_params_lock);
  731. memcpy(p, &state->tx_params, sizeof(struct v4l2_subdev_ir_parameters));
  732. mutex_unlock(&state->tx_params_lock);
  733. return 0;
  734. }
  735. static int cx23888_ir_tx_shutdown(struct v4l2_subdev *sd)
  736. {
  737. struct cx23888_ir_state *state = to_state(sd);
  738. struct cx23885_dev *dev = state->dev;
  739. mutex_lock(&state->tx_params_lock);
  740. /* Disable or slow down all IR Tx circuits and counters */
  741. irqenable_tx(dev, 0);
  742. control_tx_enable(dev, false);
  743. control_tx_modulation_enable(dev, false);
  744. cx23888_ir_write4(dev, CX23888_IR_TXCLK_REG, TXCLK_TCD);
  745. state->tx_params.shutdown = true;
  746. mutex_unlock(&state->tx_params_lock);
  747. return 0;
  748. }
  749. static int cx23888_ir_tx_s_parameters(struct v4l2_subdev *sd,
  750. struct v4l2_subdev_ir_parameters *p)
  751. {
  752. struct cx23888_ir_state *state = to_state(sd);
  753. struct cx23885_dev *dev = state->dev;
  754. struct v4l2_subdev_ir_parameters *o = &state->tx_params;
  755. u16 txclk_divider;
  756. if (p->shutdown)
  757. return cx23888_ir_tx_shutdown(sd);
  758. if (p->mode != V4L2_SUBDEV_IR_MODE_PULSE_WIDTH)
  759. return -ENOSYS;
  760. mutex_lock(&state->tx_params_lock);
  761. o->shutdown = p->shutdown;
  762. o->mode = p->mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH;
  763. o->bytes_per_data_element = p->bytes_per_data_element
  764. = sizeof(union cx23888_ir_fifo_rec);
  765. /* Before we tweak the hardware, we have to disable the transmitter */
  766. irqenable_tx(dev, 0);
  767. control_tx_enable(dev, false);
  768. control_tx_modulation_enable(dev, p->modulation);
  769. o->modulation = p->modulation;
  770. if (p->modulation) {
  771. p->carrier_freq = txclk_tx_s_carrier(dev, p->carrier_freq,
  772. &txclk_divider);
  773. o->carrier_freq = p->carrier_freq;
  774. p->duty_cycle = cduty_tx_s_duty_cycle(dev, p->duty_cycle);
  775. o->duty_cycle = p->duty_cycle;
  776. p->max_pulse_width =
  777. (u32) pulse_width_count_to_ns(FIFO_RXTX, txclk_divider);
  778. } else {
  779. p->max_pulse_width =
  780. txclk_tx_s_max_pulse_width(dev, p->max_pulse_width,
  781. &txclk_divider);
  782. }
  783. o->max_pulse_width = p->max_pulse_width;
  784. atomic_set(&state->txclk_divider, txclk_divider);
  785. p->resolution = clock_divider_to_resolution(txclk_divider);
  786. o->resolution = p->resolution;
  787. /* FIXME - make this dependent on resolution for better performance */
  788. control_tx_irq_watermark(dev, TX_FIFO_HALF_EMPTY);
  789. control_tx_polarity_invert(dev, p->invert_carrier_sense);
  790. o->invert_carrier_sense = p->invert_carrier_sense;
  791. control_tx_level_invert(dev, p->invert_level);
  792. o->invert_level = p->invert_level;
  793. o->interrupt_enable = p->interrupt_enable;
  794. o->enable = p->enable;
  795. if (p->enable) {
  796. if (p->interrupt_enable)
  797. irqenable_tx(dev, IRQEN_TSE);
  798. control_tx_enable(dev, p->enable);
  799. }
  800. mutex_unlock(&state->tx_params_lock);
  801. return 0;
  802. }
  803. /*
  804. * V4L2 Subdevice Core Ops
  805. */
  806. static int cx23888_ir_log_status(struct v4l2_subdev *sd)
  807. {
  808. struct cx23888_ir_state *state = to_state(sd);
  809. struct cx23885_dev *dev = state->dev;
  810. char *s;
  811. int i, j;
  812. u32 cntrl = cx23888_ir_read4(dev, CX23888_IR_CNTRL_REG);
  813. u32 txclk = cx23888_ir_read4(dev, CX23888_IR_TXCLK_REG) & TXCLK_TCD;
  814. u32 rxclk = cx23888_ir_read4(dev, CX23888_IR_RXCLK_REG) & RXCLK_RCD;
  815. u32 cduty = cx23888_ir_read4(dev, CX23888_IR_CDUTY_REG) & CDUTY_CDC;
  816. u32 stats = cx23888_ir_read4(dev, CX23888_IR_STATS_REG);
  817. u32 irqen = cx23888_ir_read4(dev, CX23888_IR_IRQEN_REG);
  818. u32 filtr = cx23888_ir_read4(dev, CX23888_IR_FILTR_REG) & FILTR_LPF;
  819. v4l2_info(sd, "IR Receiver:\n");
  820. v4l2_info(sd, "\tEnabled: %s\n",
  821. cntrl & CNTRL_RXE ? "yes" : "no");
  822. v4l2_info(sd, "\tDemodulation from a carrier: %s\n",
  823. cntrl & CNTRL_DMD ? "enabled" : "disabled");
  824. v4l2_info(sd, "\tFIFO: %s\n",
  825. cntrl & CNTRL_RFE ? "enabled" : "disabled");
  826. switch (cntrl & CNTRL_EDG) {
  827. case CNTRL_EDG_NONE:
  828. s = "disabled";
  829. break;
  830. case CNTRL_EDG_FALL:
  831. s = "falling edge";
  832. break;
  833. case CNTRL_EDG_RISE:
  834. s = "rising edge";
  835. break;
  836. case CNTRL_EDG_BOTH:
  837. s = "rising & falling edges";
  838. break;
  839. default:
  840. s = "??? edge";
  841. break;
  842. }
  843. v4l2_info(sd, "\tPulse timers' start/stop trigger: %s\n", s);
  844. v4l2_info(sd, "\tFIFO data on pulse timer overflow: %s\n",
  845. cntrl & CNTRL_R ? "not loaded" : "overflow marker");
  846. v4l2_info(sd, "\tFIFO interrupt watermark: %s\n",
  847. cntrl & CNTRL_RIC ? "not empty" : "half full or greater");
  848. v4l2_info(sd, "\tLoopback mode: %s\n",
  849. cntrl & CNTRL_LBM ? "loopback active" : "normal receive");
  850. if (cntrl & CNTRL_DMD) {
  851. v4l2_info(sd, "\tExpected carrier (16 clocks): %u Hz\n",
  852. clock_divider_to_carrier_freq(rxclk));
  853. switch (cntrl & CNTRL_WIN) {
  854. case CNTRL_WIN_3_3:
  855. i = 3;
  856. j = 3;
  857. break;
  858. case CNTRL_WIN_4_3:
  859. i = 4;
  860. j = 3;
  861. break;
  862. case CNTRL_WIN_3_4:
  863. i = 3;
  864. j = 4;
  865. break;
  866. case CNTRL_WIN_4_4:
  867. i = 4;
  868. j = 4;
  869. break;
  870. default:
  871. i = 0;
  872. j = 0;
  873. break;
  874. }
  875. v4l2_info(sd, "\tNext carrier edge window: 16 clocks "
  876. "-%1d/+%1d, %u to %u Hz\n", i, j,
  877. clock_divider_to_freq(rxclk, 16 + j),
  878. clock_divider_to_freq(rxclk, 16 - i));
  879. }
  880. v4l2_info(sd, "\tMax measurable pulse width: %u us, %llu ns\n",
  881. pulse_width_count_to_us(FIFO_RXTX, rxclk),
  882. pulse_width_count_to_ns(FIFO_RXTX, rxclk));
  883. v4l2_info(sd, "\tLow pass filter: %s\n",
  884. filtr ? "enabled" : "disabled");
  885. if (filtr)
  886. v4l2_info(sd, "\tMin acceptable pulse width (LPF): %u us, "
  887. "%u ns\n",
  888. lpf_count_to_us(filtr),
  889. lpf_count_to_ns(filtr));
  890. v4l2_info(sd, "\tPulse width timer timed-out: %s\n",
  891. stats & STATS_RTO ? "yes" : "no");
  892. v4l2_info(sd, "\tPulse width timer time-out intr: %s\n",
  893. irqen & IRQEN_RTE ? "enabled" : "disabled");
  894. v4l2_info(sd, "\tFIFO overrun: %s\n",
  895. stats & STATS_ROR ? "yes" : "no");
  896. v4l2_info(sd, "\tFIFO overrun interrupt: %s\n",
  897. irqen & IRQEN_ROE ? "enabled" : "disabled");
  898. v4l2_info(sd, "\tBusy: %s\n",
  899. stats & STATS_RBY ? "yes" : "no");
  900. v4l2_info(sd, "\tFIFO service requested: %s\n",
  901. stats & STATS_RSR ? "yes" : "no");
  902. v4l2_info(sd, "\tFIFO service request interrupt: %s\n",
  903. irqen & IRQEN_RSE ? "enabled" : "disabled");
  904. v4l2_info(sd, "IR Transmitter:\n");
  905. v4l2_info(sd, "\tEnabled: %s\n",
  906. cntrl & CNTRL_TXE ? "yes" : "no");
  907. v4l2_info(sd, "\tModulation onto a carrier: %s\n",
  908. cntrl & CNTRL_MOD ? "enabled" : "disabled");
  909. v4l2_info(sd, "\tFIFO: %s\n",
  910. cntrl & CNTRL_TFE ? "enabled" : "disabled");
  911. v4l2_info(sd, "\tFIFO interrupt watermark: %s\n",
  912. cntrl & CNTRL_TIC ? "not empty" : "half full or less");
  913. v4l2_info(sd, "\tOutput pin level inversion %s\n",
  914. cntrl & CNTRL_IVO ? "yes" : "no");
  915. v4l2_info(sd, "\tCarrier polarity: %s\n",
  916. cntrl & CNTRL_CPL ? "space:burst mark:noburst"
  917. : "space:noburst mark:burst");
  918. if (cntrl & CNTRL_MOD) {
  919. v4l2_info(sd, "\tCarrier (16 clocks): %u Hz\n",
  920. clock_divider_to_carrier_freq(txclk));
  921. v4l2_info(sd, "\tCarrier duty cycle: %2u/16\n",
  922. cduty + 1);
  923. }
  924. v4l2_info(sd, "\tMax pulse width: %u us, %llu ns\n",
  925. pulse_width_count_to_us(FIFO_RXTX, txclk),
  926. pulse_width_count_to_ns(FIFO_RXTX, txclk));
  927. v4l2_info(sd, "\tBusy: %s\n",
  928. stats & STATS_TBY ? "yes" : "no");
  929. v4l2_info(sd, "\tFIFO service requested: %s\n",
  930. stats & STATS_TSR ? "yes" : "no");
  931. v4l2_info(sd, "\tFIFO service request interrupt: %s\n",
  932. irqen & IRQEN_TSE ? "enabled" : "disabled");
  933. return 0;
  934. }
  935. static inline int cx23888_ir_dbg_match(const struct v4l2_dbg_match *match)
  936. {
  937. return match->type == V4L2_CHIP_MATCH_HOST && match->addr == 2;
  938. }
  939. static int cx23888_ir_g_chip_ident(struct v4l2_subdev *sd,
  940. struct v4l2_dbg_chip_ident *chip)
  941. {
  942. struct cx23888_ir_state *state = to_state(sd);
  943. if (cx23888_ir_dbg_match(&chip->match)) {
  944. chip->ident = state->id;
  945. chip->revision = state->rev;
  946. }
  947. return 0;
  948. }
  949. #ifdef CONFIG_VIDEO_ADV_DEBUG
  950. static int cx23888_ir_g_register(struct v4l2_subdev *sd,
  951. struct v4l2_dbg_register *reg)
  952. {
  953. struct cx23888_ir_state *state = to_state(sd);
  954. u32 addr = CX23888_IR_REG_BASE + (u32) reg->reg;
  955. if (!cx23888_ir_dbg_match(&reg->match))
  956. return -EINVAL;
  957. if ((addr & 0x3) != 0)
  958. return -EINVAL;
  959. if (addr < CX23888_IR_CNTRL_REG || addr > CX23888_IR_LEARN_REG)
  960. return -EINVAL;
  961. if (!capable(CAP_SYS_ADMIN))
  962. return -EPERM;
  963. reg->size = 4;
  964. reg->val = cx23888_ir_read4(state->dev, addr);
  965. return 0;
  966. }
  967. static int cx23888_ir_s_register(struct v4l2_subdev *sd,
  968. struct v4l2_dbg_register *reg)
  969. {
  970. struct cx23888_ir_state *state = to_state(sd);
  971. u32 addr = CX23888_IR_REG_BASE + (u32) reg->reg;
  972. if (!cx23888_ir_dbg_match(&reg->match))
  973. return -EINVAL;
  974. if ((addr & 0x3) != 0)
  975. return -EINVAL;
  976. if (addr < CX23888_IR_CNTRL_REG || addr > CX23888_IR_LEARN_REG)
  977. return -EINVAL;
  978. if (!capable(CAP_SYS_ADMIN))
  979. return -EPERM;
  980. cx23888_ir_write4(state->dev, addr, reg->val);
  981. return 0;
  982. }
  983. #endif
  984. static const struct v4l2_subdev_core_ops cx23888_ir_core_ops = {
  985. .g_chip_ident = cx23888_ir_g_chip_ident,
  986. .log_status = cx23888_ir_log_status,
  987. #ifdef CONFIG_VIDEO_ADV_DEBUG
  988. .g_register = cx23888_ir_g_register,
  989. .s_register = cx23888_ir_s_register,
  990. #endif
  991. .interrupt_service_routine = cx23888_ir_irq_handler,
  992. };
  993. static const struct v4l2_subdev_ir_ops cx23888_ir_ir_ops = {
  994. .rx_read = cx23888_ir_rx_read,
  995. .rx_g_parameters = cx23888_ir_rx_g_parameters,
  996. .rx_s_parameters = cx23888_ir_rx_s_parameters,
  997. .tx_write = cx23888_ir_tx_write,
  998. .tx_g_parameters = cx23888_ir_tx_g_parameters,
  999. .tx_s_parameters = cx23888_ir_tx_s_parameters,
  1000. };
  1001. static const struct v4l2_subdev_ops cx23888_ir_controller_ops = {
  1002. .core = &cx23888_ir_core_ops,
  1003. .ir = &cx23888_ir_ir_ops,
  1004. };
  1005. static const struct v4l2_subdev_ir_parameters default_rx_params = {
  1006. .bytes_per_data_element = sizeof(union cx23888_ir_fifo_rec),
  1007. .mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH,
  1008. .enable = false,
  1009. .interrupt_enable = false,
  1010. .shutdown = true,
  1011. .modulation = true,
  1012. .carrier_freq = 36000, /* 36 kHz - RC-5, RC-6, and RC-6A carrier */
  1013. /* RC-5: 666,667 ns = 1/36 kHz * 32 cycles * 1 mark * 0.75 */
  1014. /* RC-6A: 333,333 ns = 1/36 kHz * 16 cycles * 1 mark * 0.75 */
  1015. .noise_filter_min_width = 333333, /* ns */
  1016. .carrier_range_lower = 35000,
  1017. .carrier_range_upper = 37000,
  1018. .invert_level = false,
  1019. };
  1020. static const struct v4l2_subdev_ir_parameters default_tx_params = {
  1021. .bytes_per_data_element = sizeof(union cx23888_ir_fifo_rec),
  1022. .mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH,
  1023. .enable = false,
  1024. .interrupt_enable = false,
  1025. .shutdown = true,
  1026. .modulation = true,
  1027. .carrier_freq = 36000, /* 36 kHz - RC-5 carrier */
  1028. .duty_cycle = 25, /* 25 % - RC-5 carrier */
  1029. .invert_level = false,
  1030. .invert_carrier_sense = false,
  1031. };
  1032. int cx23888_ir_probe(struct cx23885_dev *dev)
  1033. {
  1034. struct cx23888_ir_state *state;
  1035. struct v4l2_subdev *sd;
  1036. struct v4l2_subdev_ir_parameters default_params;
  1037. int ret;
  1038. state = kzalloc(sizeof(struct cx23888_ir_state), GFP_KERNEL);
  1039. if (state == NULL)
  1040. return -ENOMEM;
  1041. spin_lock_init(&state->rx_kfifo_lock);
  1042. if (kfifo_alloc(&state->rx_kfifo, CX23888_IR_RX_KFIFO_SIZE, GFP_KERNEL))
  1043. return -ENOMEM;
  1044. state->dev = dev;
  1045. state->id = V4L2_IDENT_CX23888_IR;
  1046. state->rev = 0;
  1047. sd = &state->sd;
  1048. v4l2_subdev_init(sd, &cx23888_ir_controller_ops);
  1049. v4l2_set_subdevdata(sd, state);
  1050. /* FIXME - fix the formatting of dev->v4l2_dev.name and use it */
  1051. snprintf(sd->name, sizeof(sd->name), "%s/888-ir", dev->name);
  1052. sd->grp_id = CX23885_HW_888_IR;
  1053. ret = v4l2_device_register_subdev(&dev->v4l2_dev, sd);
  1054. if (ret == 0) {
  1055. /*
  1056. * Ensure no interrupts arrive from '888 specific conditions,
  1057. * since we ignore them in this driver to have commonality with
  1058. * similar IR controller cores.
  1059. */
  1060. cx23888_ir_write4(dev, CX23888_IR_IRQEN_REG, 0);
  1061. mutex_init(&state->rx_params_lock);
  1062. memcpy(&default_params, &default_rx_params,
  1063. sizeof(struct v4l2_subdev_ir_parameters));
  1064. v4l2_subdev_call(sd, ir, rx_s_parameters, &default_params);
  1065. mutex_init(&state->tx_params_lock);
  1066. memcpy(&default_params, &default_tx_params,
  1067. sizeof(struct v4l2_subdev_ir_parameters));
  1068. v4l2_subdev_call(sd, ir, tx_s_parameters, &default_params);
  1069. } else {
  1070. kfifo_free(&state->rx_kfifo);
  1071. }
  1072. return ret;
  1073. }
  1074. int cx23888_ir_remove(struct cx23885_dev *dev)
  1075. {
  1076. struct v4l2_subdev *sd;
  1077. struct cx23888_ir_state *state;
  1078. sd = cx23885_find_hw(dev, CX23885_HW_888_IR);
  1079. if (sd == NULL)
  1080. return -ENODEV;
  1081. cx23888_ir_rx_shutdown(sd);
  1082. cx23888_ir_tx_shutdown(sd);
  1083. state = to_state(sd);
  1084. v4l2_device_unregister_subdev(sd);
  1085. kfifo_free(&state->rx_kfifo);
  1086. kfree(state);
  1087. /* Nothing more to free() as state held the actual v4l2_subdev object */
  1088. return 0;
  1089. }