cx25840-ir.c 36 KB

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