cx23888-ir.c 36 KB

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