cx18-av-core.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239
  1. /*
  2. * cx18 ADEC audio functions
  3. *
  4. * Derived from cx25840-core.c
  5. *
  6. * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
  7. * Copyright (C) 2008 Andy Walls <awalls@radix.net>
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version 2
  12. * of the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  22. * 02110-1301, USA.
  23. */
  24. #include <media/v4l2-chip-ident.h>
  25. #include "cx18-driver.h"
  26. #include "cx18-io.h"
  27. #include "cx18-cards.h"
  28. int cx18_av_write(struct cx18 *cx, u16 addr, u8 value)
  29. {
  30. u32 reg = 0xc40000 + (addr & ~3);
  31. u32 mask = 0xff;
  32. int shift = (addr & 3) * 8;
  33. u32 x = cx18_read_reg(cx, reg);
  34. x = (x & ~(mask << shift)) | ((u32)value << shift);
  35. cx18_write_reg(cx, x, reg);
  36. return 0;
  37. }
  38. int cx18_av_write_expect(struct cx18 *cx, u16 addr, u8 value, u8 eval, u8 mask)
  39. {
  40. u32 reg = 0xc40000 + (addr & ~3);
  41. int shift = (addr & 3) * 8;
  42. u32 x = cx18_read_reg(cx, reg);
  43. x = (x & ~((u32)0xff << shift)) | ((u32)value << shift);
  44. cx18_write_reg_expect(cx, x, reg,
  45. ((u32)eval << shift), ((u32)mask << shift));
  46. return 0;
  47. }
  48. int cx18_av_write4(struct cx18 *cx, u16 addr, u32 value)
  49. {
  50. cx18_write_reg(cx, value, 0xc40000 + addr);
  51. return 0;
  52. }
  53. int
  54. cx18_av_write4_expect(struct cx18 *cx, u16 addr, u32 value, u32 eval, u32 mask)
  55. {
  56. cx18_write_reg_expect(cx, value, 0xc40000 + addr, eval, mask);
  57. return 0;
  58. }
  59. int cx18_av_write4_noretry(struct cx18 *cx, u16 addr, u32 value)
  60. {
  61. cx18_write_reg_noretry(cx, value, 0xc40000 + addr);
  62. return 0;
  63. }
  64. u8 cx18_av_read(struct cx18 *cx, u16 addr)
  65. {
  66. u32 x = cx18_read_reg(cx, 0xc40000 + (addr & ~3));
  67. int shift = (addr & 3) * 8;
  68. return (x >> shift) & 0xff;
  69. }
  70. u32 cx18_av_read4(struct cx18 *cx, u16 addr)
  71. {
  72. return cx18_read_reg(cx, 0xc40000 + addr);
  73. }
  74. int cx18_av_and_or(struct cx18 *cx, u16 addr, unsigned and_mask,
  75. u8 or_value)
  76. {
  77. return cx18_av_write(cx, addr,
  78. (cx18_av_read(cx, addr) & and_mask) |
  79. or_value);
  80. }
  81. int cx18_av_and_or4(struct cx18 *cx, u16 addr, u32 and_mask,
  82. u32 or_value)
  83. {
  84. return cx18_av_write4(cx, addr,
  85. (cx18_av_read4(cx, addr) & and_mask) |
  86. or_value);
  87. }
  88. static void cx18_av_initialize(struct cx18 *cx)
  89. {
  90. struct cx18_av_state *state = &cx->av_state;
  91. u32 v;
  92. cx18_av_loadfw(cx);
  93. /* Stop 8051 code execution */
  94. cx18_av_write4_expect(cx, CXADEC_DL_CTL, 0x03000000,
  95. 0x03000000, 0x13000000);
  96. /* initallize the PLL by toggling sleep bit */
  97. v = cx18_av_read4(cx, CXADEC_HOST_REG1);
  98. /* enable sleep mode - register appears to be read only... */
  99. cx18_av_write4_expect(cx, CXADEC_HOST_REG1, v | 1, v, 0xfffe);
  100. /* disable sleep mode */
  101. cx18_av_write4_expect(cx, CXADEC_HOST_REG1, v & 0xfffe,
  102. v & 0xfffe, 0xffff);
  103. /* initialize DLLs */
  104. v = cx18_av_read4(cx, CXADEC_DLL1_DIAG_CTRL) & 0xE1FFFEFF;
  105. /* disable FLD */
  106. cx18_av_write4(cx, CXADEC_DLL1_DIAG_CTRL, v);
  107. /* enable FLD */
  108. cx18_av_write4(cx, CXADEC_DLL1_DIAG_CTRL, v | 0x10000100);
  109. v = cx18_av_read4(cx, CXADEC_DLL2_DIAG_CTRL) & 0xE1FFFEFF;
  110. /* disable FLD */
  111. cx18_av_write4(cx, CXADEC_DLL2_DIAG_CTRL, v);
  112. /* enable FLD */
  113. cx18_av_write4(cx, CXADEC_DLL2_DIAG_CTRL, v | 0x06000100);
  114. /* set analog bias currents. Set Vreg to 1.20V. */
  115. cx18_av_write4(cx, CXADEC_AFE_DIAG_CTRL1, 0x000A1802);
  116. v = cx18_av_read4(cx, CXADEC_AFE_DIAG_CTRL3) | 1;
  117. /* enable TUNE_FIL_RST */
  118. cx18_av_write4_expect(cx, CXADEC_AFE_DIAG_CTRL3, v, v, 0x03009F0F);
  119. /* disable TUNE_FIL_RST */
  120. cx18_av_write4_expect(cx, CXADEC_AFE_DIAG_CTRL3,
  121. v & 0xFFFFFFFE, v & 0xFFFFFFFE, 0x03009F0F);
  122. /* enable 656 output */
  123. cx18_av_and_or4(cx, CXADEC_PIN_CTRL1, ~0, 0x040C00);
  124. /* video output drive strength */
  125. cx18_av_and_or4(cx, CXADEC_PIN_CTRL2, ~0, 0x2);
  126. /* reset video */
  127. cx18_av_write4(cx, CXADEC_SOFT_RST_CTRL, 0x8000);
  128. cx18_av_write4(cx, CXADEC_SOFT_RST_CTRL, 0);
  129. /* set video to auto-detect */
  130. /* Clear bits 11-12 to enable slow locking mode. Set autodetect mode */
  131. /* set the comb notch = 1 */
  132. cx18_av_and_or4(cx, CXADEC_MODE_CTRL, 0xFFF7E7F0, 0x02040800);
  133. /* Enable wtw_en in CRUSH_CTRL (Set bit 22) */
  134. /* Enable maj_sel in CRUSH_CTRL (Set bit 20) */
  135. cx18_av_and_or4(cx, CXADEC_CRUSH_CTRL, ~0, 0x00500000);
  136. /* Set VGA_TRACK_RANGE to 0x20 */
  137. cx18_av_and_or4(cx, CXADEC_DFE_CTRL2, 0xFFFF00FF, 0x00002000);
  138. /*
  139. * Initial VBI setup
  140. * VIP-1.1, 10 bit mode, enable Raw, disable sliced,
  141. * don't clamp raw samples when codes are in use, 1 byte user D-words,
  142. * IDID0 has line #, RP code V bit transition on VBLANK, data during
  143. * blanking intervals
  144. */
  145. cx18_av_write4(cx, CXADEC_OUT_CTRL1, 0x4013252e);
  146. /* Set the video input.
  147. The setting in MODE_CTRL gets lost when we do the above setup */
  148. /* EncSetSignalStd(dwDevNum, pEnc->dwSigStd); */
  149. /* EncSetVideoInput(dwDevNum, pEnc->VidIndSelection); */
  150. v = cx18_av_read4(cx, CXADEC_AFE_CTRL);
  151. v &= 0xFFFBFFFF; /* turn OFF bit 18 for droop_comp_ch1 */
  152. v &= 0xFFFF7FFF; /* turn OFF bit 9 for clamp_sel_ch1 */
  153. v &= 0xFFFFFFFE; /* turn OFF bit 0 for 12db_ch1 */
  154. /* v |= 0x00000001;*/ /* turn ON bit 0 for 12db_ch1 */
  155. cx18_av_write4(cx, CXADEC_AFE_CTRL, v);
  156. /* if(dwEnable && dw3DCombAvailable) { */
  157. /* CxDevWrReg(CXADEC_SRC_COMB_CFG, 0x7728021F); */
  158. /* } else { */
  159. /* CxDevWrReg(CXADEC_SRC_COMB_CFG, 0x6628021F); */
  160. /* } */
  161. cx18_av_write4(cx, CXADEC_SRC_COMB_CFG, 0x6628021F);
  162. state->default_volume = 228 - cx18_av_read(cx, 0x8d4);
  163. state->default_volume = ((state->default_volume / 2) + 23) << 9;
  164. }
  165. static int cx18_av_reset(struct v4l2_subdev *sd, u32 val)
  166. {
  167. struct cx18 *cx = v4l2_get_subdevdata(sd);
  168. cx18_av_initialize(cx);
  169. return 0;
  170. }
  171. static int cx18_av_init(struct v4l2_subdev *sd, u32 val)
  172. {
  173. struct cx18_av_state *state = to_cx18_av_state(sd);
  174. struct cx18 *cx = v4l2_get_subdevdata(sd);
  175. switch (val) {
  176. case CX18_AV_INIT_PLLS:
  177. /*
  178. * The crystal freq used in calculations in this driver will be
  179. * 28.636360 MHz.
  180. * Aim to run the PLLs' VCOs near 400 MHz to minimze errors.
  181. */
  182. /*
  183. * VDCLK Integer = 0x0f, Post Divider = 0x04
  184. * AIMCLK Integer = 0x0e, Post Divider = 0x16
  185. */
  186. cx18_av_write4(cx, CXADEC_PLL_CTRL1, 0x160e040f);
  187. /* VDCLK Fraction = 0x2be2fe */
  188. /* xtal * 0xf.15f17f0/4 = 108 MHz: 432 MHz before post divide */
  189. cx18_av_write4(cx, CXADEC_VID_PLL_FRAC, 0x002be2fe);
  190. /* AIMCLK Fraction = 0x05227ad */
  191. /* xtal * 0xe.2913d68/0x16 = 48000 * 384: 406 MHz pre post-div*/
  192. cx18_av_write4(cx, CXADEC_AUX_PLL_FRAC, 0x005227ad);
  193. /* SA_MCLK_SEL=1, SA_MCLK_DIV=0x16 */
  194. cx18_av_write(cx, CXADEC_I2S_MCLK, 0x56);
  195. break;
  196. case CX18_AV_INIT_NORMAL:
  197. default:
  198. if (!state->is_initialized) {
  199. /* initialize on first use */
  200. state->is_initialized = 1;
  201. cx18_av_initialize(cx);
  202. }
  203. break;
  204. }
  205. return 0;
  206. }
  207. void cx18_av_std_setup(struct cx18 *cx)
  208. {
  209. struct cx18_av_state *state = &cx->av_state;
  210. v4l2_std_id std = state->std;
  211. int hblank, hactive, burst, vblank, vactive, sc;
  212. int vblank656, src_decimation;
  213. int luma_lpf, uv_lpf, comb;
  214. u32 pll_int, pll_frac, pll_post;
  215. /* datasheet startup, step 8d */
  216. if (std & ~V4L2_STD_NTSC)
  217. cx18_av_write(cx, 0x49f, 0x11);
  218. else
  219. cx18_av_write(cx, 0x49f, 0x14);
  220. if (std & V4L2_STD_625_50) {
  221. /* FIXME - revisit these for Sliced VBI */
  222. hblank = 132;
  223. hactive = 720;
  224. burst = 93;
  225. vblank = 36;
  226. vactive = 580;
  227. vblank656 = 40;
  228. src_decimation = 0x21f;
  229. luma_lpf = 2;
  230. if (std & V4L2_STD_PAL) {
  231. uv_lpf = 1;
  232. comb = 0x20;
  233. sc = 688739;
  234. } else if (std == V4L2_STD_PAL_Nc) {
  235. uv_lpf = 1;
  236. comb = 0x20;
  237. sc = 556453;
  238. } else { /* SECAM */
  239. uv_lpf = 0;
  240. comb = 0;
  241. sc = 672351;
  242. }
  243. } else {
  244. /*
  245. * The following relationships of half line counts should hold:
  246. * 525 = vsync + vactive + vblank656
  247. * 12 = vblank656 - vblank
  248. *
  249. * vsync: always 6 half-lines of vsync pulses
  250. * vactive: half lines of active video
  251. * vblank656: half lines, after line 3, of blanked video
  252. * vblank: half lines, after line 9, of blanked video
  253. *
  254. * vblank656 starts counting from the falling edge of the first
  255. * vsync pulse (start of line 4)
  256. * vblank starts counting from the after the 6 vsync pulses and
  257. * 6 equalization pulses (start of line 10)
  258. *
  259. * For 525 line systems the driver will extract VBI information
  260. * from lines 10 through 21. To avoid the EAV RP code from
  261. * toggling at the start of hblank at line 22, where sliced VBI
  262. * data from line 21 is stuffed, also treat line 22 as blanked.
  263. */
  264. vblank656 = 38; /* lines 4 through 22 */
  265. vblank = 26; /* lines 10 through 22 */
  266. vactive = 481; /* lines 23 through 262.5 */
  267. hactive = 720;
  268. hblank = 122;
  269. luma_lpf = 1;
  270. uv_lpf = 1;
  271. src_decimation = 0x21f;
  272. if (std == V4L2_STD_PAL_60) {
  273. burst = 0x5b;
  274. luma_lpf = 2;
  275. comb = 0x20;
  276. sc = 688739;
  277. } else if (std == V4L2_STD_PAL_M) {
  278. burst = 0x61;
  279. comb = 0x20;
  280. sc = 555452;
  281. } else {
  282. burst = 0x5b;
  283. comb = 0x66;
  284. sc = 556063;
  285. }
  286. }
  287. /* DEBUG: Displays configured PLL frequency */
  288. pll_int = cx18_av_read(cx, 0x108);
  289. pll_frac = cx18_av_read4(cx, 0x10c) & 0x1ffffff;
  290. pll_post = cx18_av_read(cx, 0x109);
  291. CX18_DEBUG_INFO("PLL regs = int: %u, frac: %u, post: %u\n",
  292. pll_int, pll_frac, pll_post);
  293. if (pll_post) {
  294. int fin, fsc, pll;
  295. pll = (28636360L * ((((u64)pll_int) << 25) + pll_frac)) >> 25;
  296. pll /= pll_post;
  297. CX18_DEBUG_INFO("PLL = %d.%06d MHz\n",
  298. pll / 1000000, pll % 1000000);
  299. CX18_DEBUG_INFO("PLL/8 = %d.%06d MHz\n",
  300. pll / 8000000, (pll / 8) % 1000000);
  301. fin = ((u64)src_decimation * pll) >> 12;
  302. CX18_DEBUG_INFO("ADC Sampling freq = %d.%06d MHz\n",
  303. fin / 1000000, fin % 1000000);
  304. fsc = (((u64)sc) * pll) >> 24L;
  305. CX18_DEBUG_INFO("Chroma sub-carrier freq = %d.%06d MHz\n",
  306. fsc / 1000000, fsc % 1000000);
  307. CX18_DEBUG_INFO("hblank %i, hactive %i, "
  308. "vblank %i , vactive %i, vblank656 %i, src_dec %i,"
  309. "burst 0x%02x, luma_lpf %i, uv_lpf %i, comb 0x%02x,"
  310. " sc 0x%06x\n",
  311. hblank, hactive, vblank, vactive, vblank656,
  312. src_decimation, burst, luma_lpf, uv_lpf, comb, sc);
  313. }
  314. /* Sets horizontal blanking delay and active lines */
  315. cx18_av_write(cx, 0x470, hblank);
  316. cx18_av_write(cx, 0x471, 0xff & (((hblank >> 8) & 0x3) |
  317. (hactive << 4)));
  318. cx18_av_write(cx, 0x472, hactive >> 4);
  319. /* Sets burst gate delay */
  320. cx18_av_write(cx, 0x473, burst);
  321. /* Sets vertical blanking delay and active duration */
  322. cx18_av_write(cx, 0x474, vblank);
  323. cx18_av_write(cx, 0x475, 0xff & (((vblank >> 8) & 0x3) |
  324. (vactive << 4)));
  325. cx18_av_write(cx, 0x476, vactive >> 4);
  326. cx18_av_write(cx, 0x477, vblank656);
  327. /* Sets src decimation rate */
  328. cx18_av_write(cx, 0x478, 0xff & src_decimation);
  329. cx18_av_write(cx, 0x479, 0xff & (src_decimation >> 8));
  330. /* Sets Luma and UV Low pass filters */
  331. cx18_av_write(cx, 0x47a, luma_lpf << 6 | ((uv_lpf << 4) & 0x30));
  332. /* Enables comb filters */
  333. cx18_av_write(cx, 0x47b, comb);
  334. /* Sets SC Step*/
  335. cx18_av_write(cx, 0x47c, sc);
  336. cx18_av_write(cx, 0x47d, 0xff & sc >> 8);
  337. cx18_av_write(cx, 0x47e, 0xff & sc >> 16);
  338. if (std & V4L2_STD_625_50) {
  339. state->slicer_line_delay = 1;
  340. state->slicer_line_offset = (6 + state->slicer_line_delay - 2);
  341. } else {
  342. state->slicer_line_delay = 0;
  343. state->slicer_line_offset = (10 + state->slicer_line_delay - 2);
  344. }
  345. cx18_av_write(cx, 0x47f, state->slicer_line_delay);
  346. }
  347. static int cx18_av_decode_vbi_line(struct v4l2_subdev *sd,
  348. struct v4l2_decode_vbi_line *vbi_line)
  349. {
  350. struct cx18 *cx = v4l2_get_subdevdata(sd);
  351. return cx18_av_vbi(cx, VIDIOC_INT_DECODE_VBI_LINE, vbi_line);
  352. }
  353. static int cx18_av_s_clock_freq(struct v4l2_subdev *sd, u32 freq)
  354. {
  355. struct cx18 *cx = v4l2_get_subdevdata(sd);
  356. return cx18_av_audio(cx, VIDIOC_INT_AUDIO_CLOCK_FREQ, &freq);
  357. }
  358. static void input_change(struct cx18 *cx)
  359. {
  360. struct cx18_av_state *state = &cx->av_state;
  361. v4l2_std_id std = state->std;
  362. u8 v;
  363. /* Follow step 8c and 8d of section 3.16 in the cx18_av datasheet */
  364. cx18_av_write(cx, 0x49f, (std & V4L2_STD_NTSC) ? 0x14 : 0x11);
  365. cx18_av_and_or(cx, 0x401, ~0x60, 0);
  366. cx18_av_and_or(cx, 0x401, ~0x60, 0x60);
  367. if (std & V4L2_STD_525_60) {
  368. if (std == V4L2_STD_NTSC_M_JP) {
  369. /* Japan uses EIAJ audio standard */
  370. cx18_av_write_expect(cx, 0x808, 0xf7, 0xf7, 0xff);
  371. cx18_av_write_expect(cx, 0x80b, 0x02, 0x02, 0x3f);
  372. } else if (std == V4L2_STD_NTSC_M_KR) {
  373. /* South Korea uses A2 audio standard */
  374. cx18_av_write_expect(cx, 0x808, 0xf8, 0xf8, 0xff);
  375. cx18_av_write_expect(cx, 0x80b, 0x03, 0x03, 0x3f);
  376. } else {
  377. /* Others use the BTSC audio standard */
  378. cx18_av_write_expect(cx, 0x808, 0xf6, 0xf6, 0xff);
  379. cx18_av_write_expect(cx, 0x80b, 0x01, 0x01, 0x3f);
  380. }
  381. } else if (std & V4L2_STD_PAL) {
  382. /* Follow tuner change procedure for PAL */
  383. cx18_av_write_expect(cx, 0x808, 0xff, 0xff, 0xff);
  384. cx18_av_write_expect(cx, 0x80b, 0x03, 0x03, 0x3f);
  385. } else if (std & V4L2_STD_SECAM) {
  386. /* Select autodetect for SECAM */
  387. cx18_av_write_expect(cx, 0x808, 0xff, 0xff, 0xff);
  388. cx18_av_write_expect(cx, 0x80b, 0x03, 0x03, 0x3f);
  389. }
  390. v = cx18_av_read(cx, 0x803);
  391. if (v & 0x10) {
  392. /* restart audio decoder microcontroller */
  393. v &= ~0x10;
  394. cx18_av_write_expect(cx, 0x803, v, v, 0x1f);
  395. v |= 0x10;
  396. cx18_av_write_expect(cx, 0x803, v, v, 0x1f);
  397. }
  398. }
  399. static int cx18_av_s_frequency(struct v4l2_subdev *sd,
  400. struct v4l2_frequency *freq)
  401. {
  402. struct cx18 *cx = v4l2_get_subdevdata(sd);
  403. input_change(cx);
  404. return 0;
  405. }
  406. static int set_input(struct cx18 *cx, enum cx18_av_video_input vid_input,
  407. enum cx18_av_audio_input aud_input)
  408. {
  409. struct cx18_av_state *state = &cx->av_state;
  410. u8 is_composite = (vid_input >= CX18_AV_COMPOSITE1 &&
  411. vid_input <= CX18_AV_COMPOSITE8);
  412. u8 reg;
  413. u8 v;
  414. CX18_DEBUG_INFO("decoder set video input %d, audio input %d\n",
  415. vid_input, aud_input);
  416. if (is_composite) {
  417. reg = 0xf0 + (vid_input - CX18_AV_COMPOSITE1);
  418. } else {
  419. int luma = vid_input & 0xf0;
  420. int chroma = vid_input & 0xf00;
  421. if ((vid_input & ~0xff0) ||
  422. luma < CX18_AV_SVIDEO_LUMA1 ||
  423. luma > CX18_AV_SVIDEO_LUMA8 ||
  424. chroma < CX18_AV_SVIDEO_CHROMA4 ||
  425. chroma > CX18_AV_SVIDEO_CHROMA8) {
  426. CX18_ERR("0x%04x is not a valid video input!\n",
  427. vid_input);
  428. return -EINVAL;
  429. }
  430. reg = 0xf0 + ((luma - CX18_AV_SVIDEO_LUMA1) >> 4);
  431. if (chroma >= CX18_AV_SVIDEO_CHROMA7) {
  432. reg &= 0x3f;
  433. reg |= (chroma - CX18_AV_SVIDEO_CHROMA7) >> 2;
  434. } else {
  435. reg &= 0xcf;
  436. reg |= (chroma - CX18_AV_SVIDEO_CHROMA4) >> 4;
  437. }
  438. }
  439. switch (aud_input) {
  440. case CX18_AV_AUDIO_SERIAL1:
  441. case CX18_AV_AUDIO_SERIAL2:
  442. /* do nothing, use serial audio input */
  443. break;
  444. case CX18_AV_AUDIO4: reg &= ~0x30; break;
  445. case CX18_AV_AUDIO5: reg &= ~0x30; reg |= 0x10; break;
  446. case CX18_AV_AUDIO6: reg &= ~0x30; reg |= 0x20; break;
  447. case CX18_AV_AUDIO7: reg &= ~0xc0; break;
  448. case CX18_AV_AUDIO8: reg &= ~0xc0; reg |= 0x40; break;
  449. default:
  450. CX18_ERR("0x%04x is not a valid audio input!\n", aud_input);
  451. return -EINVAL;
  452. }
  453. cx18_av_write_expect(cx, 0x103, reg, reg, 0xf7);
  454. /* Set INPUT_MODE to Composite (0) or S-Video (1) */
  455. cx18_av_and_or(cx, 0x401, ~0x6, is_composite ? 0 : 0x02);
  456. /* Set CH_SEL_ADC2 to 1 if input comes from CH3 */
  457. v = cx18_av_read(cx, 0x102);
  458. if (reg & 0x80)
  459. v &= ~0x2;
  460. else
  461. v |= 0x2;
  462. /* Set DUAL_MODE_ADC2 to 1 if input comes from both CH2 and CH3 */
  463. if ((reg & 0xc0) != 0xc0 && (reg & 0x30) != 0x30)
  464. v |= 0x4;
  465. else
  466. v &= ~0x4;
  467. cx18_av_write_expect(cx, 0x102, v, v, 0x17);
  468. /*cx18_av_and_or4(cx, 0x104, ~0x001b4180, 0x00004180);*/
  469. state->vid_input = vid_input;
  470. state->aud_input = aud_input;
  471. cx18_av_audio_set_path(cx);
  472. input_change(cx);
  473. return 0;
  474. }
  475. static int cx18_av_s_video_routing(struct v4l2_subdev *sd,
  476. const struct v4l2_routing *route)
  477. {
  478. struct cx18_av_state *state = to_cx18_av_state(sd);
  479. struct cx18 *cx = v4l2_get_subdevdata(sd);
  480. return set_input(cx, route->input, state->aud_input);
  481. }
  482. static int cx18_av_s_audio_routing(struct v4l2_subdev *sd,
  483. const struct v4l2_routing *route)
  484. {
  485. struct cx18_av_state *state = to_cx18_av_state(sd);
  486. struct cx18 *cx = v4l2_get_subdevdata(sd);
  487. return set_input(cx, state->vid_input, route->input);
  488. }
  489. static int cx18_av_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
  490. {
  491. struct cx18_av_state *state = to_cx18_av_state(sd);
  492. struct cx18 *cx = v4l2_get_subdevdata(sd);
  493. u8 vpres;
  494. u8 mode;
  495. int val = 0;
  496. if (state->radio)
  497. return 0;
  498. vpres = cx18_av_read(cx, 0x40e) & 0x20;
  499. vt->signal = vpres ? 0xffff : 0x0;
  500. vt->capability |=
  501. V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_LANG1 |
  502. V4L2_TUNER_CAP_LANG2 | V4L2_TUNER_CAP_SAP;
  503. mode = cx18_av_read(cx, 0x804);
  504. /* get rxsubchans and audmode */
  505. if ((mode & 0xf) == 1)
  506. val |= V4L2_TUNER_SUB_STEREO;
  507. else
  508. val |= V4L2_TUNER_SUB_MONO;
  509. if (mode == 2 || mode == 4)
  510. val = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
  511. if (mode & 0x10)
  512. val |= V4L2_TUNER_SUB_SAP;
  513. vt->rxsubchans = val;
  514. vt->audmode = state->audmode;
  515. return 0;
  516. }
  517. static int cx18_av_s_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
  518. {
  519. struct cx18_av_state *state = to_cx18_av_state(sd);
  520. struct cx18 *cx = v4l2_get_subdevdata(sd);
  521. u8 v;
  522. if (state->radio)
  523. return 0;
  524. v = cx18_av_read(cx, 0x809);
  525. v &= ~0xf;
  526. switch (vt->audmode) {
  527. case V4L2_TUNER_MODE_MONO:
  528. /* mono -> mono
  529. stereo -> mono
  530. bilingual -> lang1 */
  531. break;
  532. case V4L2_TUNER_MODE_STEREO:
  533. case V4L2_TUNER_MODE_LANG1:
  534. /* mono -> mono
  535. stereo -> stereo
  536. bilingual -> lang1 */
  537. v |= 0x4;
  538. break;
  539. case V4L2_TUNER_MODE_LANG1_LANG2:
  540. /* mono -> mono
  541. stereo -> stereo
  542. bilingual -> lang1/lang2 */
  543. v |= 0x7;
  544. break;
  545. case V4L2_TUNER_MODE_LANG2:
  546. /* mono -> mono
  547. stereo -> stereo
  548. bilingual -> lang2 */
  549. v |= 0x1;
  550. break;
  551. default:
  552. return -EINVAL;
  553. }
  554. cx18_av_write_expect(cx, 0x809, v, v, 0xff);
  555. state->audmode = vt->audmode;
  556. return 0;
  557. }
  558. static int cx18_av_s_std(struct v4l2_subdev *sd, v4l2_std_id norm)
  559. {
  560. struct cx18_av_state *state = to_cx18_av_state(sd);
  561. struct cx18 *cx = v4l2_get_subdevdata(sd);
  562. u8 fmt = 0; /* zero is autodetect */
  563. u8 pal_m = 0;
  564. if (state->radio == 0 && state->std == norm)
  565. return 0;
  566. state->radio = 0;
  567. state->std = norm;
  568. /* First tests should be against specific std */
  569. if (state->std == V4L2_STD_NTSC_M_JP) {
  570. fmt = 0x2;
  571. } else if (state->std == V4L2_STD_NTSC_443) {
  572. fmt = 0x3;
  573. } else if (state->std == V4L2_STD_PAL_M) {
  574. pal_m = 1;
  575. fmt = 0x5;
  576. } else if (state->std == V4L2_STD_PAL_N) {
  577. fmt = 0x6;
  578. } else if (state->std == V4L2_STD_PAL_Nc) {
  579. fmt = 0x7;
  580. } else if (state->std == V4L2_STD_PAL_60) {
  581. fmt = 0x8;
  582. } else {
  583. /* Then, test against generic ones */
  584. if (state->std & V4L2_STD_NTSC)
  585. fmt = 0x1;
  586. else if (state->std & V4L2_STD_PAL)
  587. fmt = 0x4;
  588. else if (state->std & V4L2_STD_SECAM)
  589. fmt = 0xc;
  590. }
  591. CX18_DEBUG_INFO("changing video std to fmt %i\n", fmt);
  592. /* Follow step 9 of section 3.16 in the cx18_av datasheet.
  593. Without this PAL may display a vertical ghosting effect.
  594. This happens for example with the Yuan MPC622. */
  595. if (fmt >= 4 && fmt < 8) {
  596. /* Set format to NTSC-M */
  597. cx18_av_and_or(cx, 0x400, ~0xf, 1);
  598. /* Turn off LCOMB */
  599. cx18_av_and_or(cx, 0x47b, ~6, 0);
  600. }
  601. cx18_av_and_or(cx, 0x400, ~0x2f, fmt | 0x20);
  602. cx18_av_and_or(cx, 0x403, ~0x3, pal_m);
  603. cx18_av_std_setup(cx);
  604. input_change(cx);
  605. return 0;
  606. }
  607. static int cx18_av_s_radio(struct v4l2_subdev *sd)
  608. {
  609. struct cx18_av_state *state = to_cx18_av_state(sd);
  610. state->radio = 1;
  611. return 0;
  612. }
  613. static int cx18_av_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
  614. {
  615. struct cx18 *cx = v4l2_get_subdevdata(sd);
  616. switch (ctrl->id) {
  617. case V4L2_CID_BRIGHTNESS:
  618. if (ctrl->value < 0 || ctrl->value > 255) {
  619. CX18_ERR("invalid brightness setting %d\n",
  620. ctrl->value);
  621. return -ERANGE;
  622. }
  623. cx18_av_write(cx, 0x414, ctrl->value - 128);
  624. break;
  625. case V4L2_CID_CONTRAST:
  626. if (ctrl->value < 0 || ctrl->value > 127) {
  627. CX18_ERR("invalid contrast setting %d\n",
  628. ctrl->value);
  629. return -ERANGE;
  630. }
  631. cx18_av_write(cx, 0x415, ctrl->value << 1);
  632. break;
  633. case V4L2_CID_SATURATION:
  634. if (ctrl->value < 0 || ctrl->value > 127) {
  635. CX18_ERR("invalid saturation setting %d\n",
  636. ctrl->value);
  637. return -ERANGE;
  638. }
  639. cx18_av_write(cx, 0x420, ctrl->value << 1);
  640. cx18_av_write(cx, 0x421, ctrl->value << 1);
  641. break;
  642. case V4L2_CID_HUE:
  643. if (ctrl->value < -128 || ctrl->value > 127) {
  644. CX18_ERR("invalid hue setting %d\n", ctrl->value);
  645. return -ERANGE;
  646. }
  647. cx18_av_write(cx, 0x422, ctrl->value);
  648. break;
  649. case V4L2_CID_AUDIO_VOLUME:
  650. case V4L2_CID_AUDIO_BASS:
  651. case V4L2_CID_AUDIO_TREBLE:
  652. case V4L2_CID_AUDIO_BALANCE:
  653. case V4L2_CID_AUDIO_MUTE:
  654. return cx18_av_audio(cx, VIDIOC_S_CTRL, ctrl);
  655. default:
  656. return -EINVAL;
  657. }
  658. return 0;
  659. }
  660. static int cx18_av_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
  661. {
  662. struct cx18 *cx = v4l2_get_subdevdata(sd);
  663. switch (ctrl->id) {
  664. case V4L2_CID_BRIGHTNESS:
  665. ctrl->value = (s8)cx18_av_read(cx, 0x414) + 128;
  666. break;
  667. case V4L2_CID_CONTRAST:
  668. ctrl->value = cx18_av_read(cx, 0x415) >> 1;
  669. break;
  670. case V4L2_CID_SATURATION:
  671. ctrl->value = cx18_av_read(cx, 0x420) >> 1;
  672. break;
  673. case V4L2_CID_HUE:
  674. ctrl->value = (s8)cx18_av_read(cx, 0x422);
  675. break;
  676. case V4L2_CID_AUDIO_VOLUME:
  677. case V4L2_CID_AUDIO_BASS:
  678. case V4L2_CID_AUDIO_TREBLE:
  679. case V4L2_CID_AUDIO_BALANCE:
  680. case V4L2_CID_AUDIO_MUTE:
  681. return cx18_av_audio(cx, VIDIOC_G_CTRL, ctrl);
  682. default:
  683. return -EINVAL;
  684. }
  685. return 0;
  686. }
  687. static int cx18_av_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc)
  688. {
  689. struct cx18_av_state *state = to_cx18_av_state(sd);
  690. switch (qc->id) {
  691. case V4L2_CID_BRIGHTNESS:
  692. return v4l2_ctrl_query_fill(qc, 0, 255, 1, 128);
  693. case V4L2_CID_CONTRAST:
  694. case V4L2_CID_SATURATION:
  695. return v4l2_ctrl_query_fill(qc, 0, 127, 1, 64);
  696. case V4L2_CID_HUE:
  697. return v4l2_ctrl_query_fill(qc, -128, 127, 1, 0);
  698. default:
  699. break;
  700. }
  701. switch (qc->id) {
  702. case V4L2_CID_AUDIO_VOLUME:
  703. return v4l2_ctrl_query_fill(qc, 0, 65535,
  704. 65535 / 100, state->default_volume);
  705. case V4L2_CID_AUDIO_MUTE:
  706. return v4l2_ctrl_query_fill(qc, 0, 1, 1, 0);
  707. case V4L2_CID_AUDIO_BALANCE:
  708. case V4L2_CID_AUDIO_BASS:
  709. case V4L2_CID_AUDIO_TREBLE:
  710. return v4l2_ctrl_query_fill(qc, 0, 65535, 65535 / 100, 32768);
  711. default:
  712. return -EINVAL;
  713. }
  714. return -EINVAL;
  715. }
  716. static int cx18_av_g_fmt(struct v4l2_subdev *sd, struct v4l2_format *fmt)
  717. {
  718. struct cx18 *cx = v4l2_get_subdevdata(sd);
  719. switch (fmt->type) {
  720. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  721. return cx18_av_vbi(cx, VIDIOC_G_FMT, fmt);
  722. default:
  723. return -EINVAL;
  724. }
  725. return 0;
  726. }
  727. static int cx18_av_s_fmt(struct v4l2_subdev *sd, struct v4l2_format *fmt)
  728. {
  729. struct cx18_av_state *state = to_cx18_av_state(sd);
  730. struct cx18 *cx = v4l2_get_subdevdata(sd);
  731. struct v4l2_pix_format *pix;
  732. int HSC, VSC, Vsrc, Hsrc, filter, Vlines;
  733. int is_50Hz = !(state->std & V4L2_STD_525_60);
  734. switch (fmt->type) {
  735. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  736. pix = &(fmt->fmt.pix);
  737. Vsrc = (cx18_av_read(cx, 0x476) & 0x3f) << 4;
  738. Vsrc |= (cx18_av_read(cx, 0x475) & 0xf0) >> 4;
  739. Hsrc = (cx18_av_read(cx, 0x472) & 0x3f) << 4;
  740. Hsrc |= (cx18_av_read(cx, 0x471) & 0xf0) >> 4;
  741. Vlines = pix->height + (is_50Hz ? 4 : 7);
  742. if ((pix->width * 16 < Hsrc) || (Hsrc < pix->width) ||
  743. (Vlines * 8 < Vsrc) || (Vsrc < Vlines)) {
  744. CX18_ERR("%dx%d is not a valid size!\n",
  745. pix->width, pix->height);
  746. return -ERANGE;
  747. }
  748. HSC = (Hsrc * (1 << 20)) / pix->width - (1 << 20);
  749. VSC = (1 << 16) - (Vsrc * (1 << 9) / Vlines - (1 << 9));
  750. VSC &= 0x1fff;
  751. if (pix->width >= 385)
  752. filter = 0;
  753. else if (pix->width > 192)
  754. filter = 1;
  755. else if (pix->width > 96)
  756. filter = 2;
  757. else
  758. filter = 3;
  759. CX18_DEBUG_INFO("decoder set size %dx%d -> scale %ux%u\n",
  760. pix->width, pix->height, HSC, VSC);
  761. /* HSCALE=HSC */
  762. cx18_av_write(cx, 0x418, HSC & 0xff);
  763. cx18_av_write(cx, 0x419, (HSC >> 8) & 0xff);
  764. cx18_av_write(cx, 0x41a, HSC >> 16);
  765. /* VSCALE=VSC */
  766. cx18_av_write(cx, 0x41c, VSC & 0xff);
  767. cx18_av_write(cx, 0x41d, VSC >> 8);
  768. /* VS_INTRLACE=1 VFILT=filter */
  769. cx18_av_write(cx, 0x41e, 0x8 | filter);
  770. break;
  771. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  772. return cx18_av_vbi(cx, VIDIOC_S_FMT, fmt);
  773. case V4L2_BUF_TYPE_VBI_CAPTURE:
  774. return cx18_av_vbi(cx, VIDIOC_S_FMT, fmt);
  775. default:
  776. return -EINVAL;
  777. }
  778. return 0;
  779. }
  780. static int cx18_av_s_stream(struct v4l2_subdev *sd, int enable)
  781. {
  782. struct cx18 *cx = v4l2_get_subdevdata(sd);
  783. CX18_DEBUG_INFO("%s output\n", enable ? "enable" : "disable");
  784. if (enable) {
  785. cx18_av_write(cx, 0x115, 0x8c);
  786. cx18_av_write(cx, 0x116, 0x07);
  787. } else {
  788. cx18_av_write(cx, 0x115, 0x00);
  789. cx18_av_write(cx, 0x116, 0x00);
  790. }
  791. return 0;
  792. }
  793. static void log_video_status(struct cx18 *cx)
  794. {
  795. static const char *const fmt_strs[] = {
  796. "0x0",
  797. "NTSC-M", "NTSC-J", "NTSC-4.43",
  798. "PAL-BDGHI", "PAL-M", "PAL-N", "PAL-Nc", "PAL-60",
  799. "0x9", "0xA", "0xB",
  800. "SECAM",
  801. "0xD", "0xE", "0xF"
  802. };
  803. struct cx18_av_state *state = &cx->av_state;
  804. u8 vidfmt_sel = cx18_av_read(cx, 0x400) & 0xf;
  805. u8 gen_stat1 = cx18_av_read(cx, 0x40d);
  806. u8 gen_stat2 = cx18_av_read(cx, 0x40e);
  807. int vid_input = state->vid_input;
  808. CX18_INFO("Video signal: %spresent\n",
  809. (gen_stat2 & 0x20) ? "" : "not ");
  810. CX18_INFO("Detected format: %s\n",
  811. fmt_strs[gen_stat1 & 0xf]);
  812. CX18_INFO("Specified standard: %s\n",
  813. vidfmt_sel ? fmt_strs[vidfmt_sel] : "automatic detection");
  814. if (vid_input >= CX18_AV_COMPOSITE1 &&
  815. vid_input <= CX18_AV_COMPOSITE8) {
  816. CX18_INFO("Specified video input: Composite %d\n",
  817. vid_input - CX18_AV_COMPOSITE1 + 1);
  818. } else {
  819. CX18_INFO("Specified video input: S-Video (Luma In%d, Chroma In%d)\n",
  820. (vid_input & 0xf0) >> 4, (vid_input & 0xf00) >> 8);
  821. }
  822. CX18_INFO("Specified audioclock freq: %d Hz\n", state->audclk_freq);
  823. }
  824. static void log_audio_status(struct cx18 *cx)
  825. {
  826. struct cx18_av_state *state = &cx->av_state;
  827. u8 download_ctl = cx18_av_read(cx, 0x803);
  828. u8 mod_det_stat0 = cx18_av_read(cx, 0x804);
  829. u8 mod_det_stat1 = cx18_av_read(cx, 0x805);
  830. u8 audio_config = cx18_av_read(cx, 0x808);
  831. u8 pref_mode = cx18_av_read(cx, 0x809);
  832. u8 afc0 = cx18_av_read(cx, 0x80b);
  833. u8 mute_ctl = cx18_av_read(cx, 0x8d3);
  834. int aud_input = state->aud_input;
  835. char *p;
  836. switch (mod_det_stat0) {
  837. case 0x00: p = "mono"; break;
  838. case 0x01: p = "stereo"; break;
  839. case 0x02: p = "dual"; break;
  840. case 0x04: p = "tri"; break;
  841. case 0x10: p = "mono with SAP"; break;
  842. case 0x11: p = "stereo with SAP"; break;
  843. case 0x12: p = "dual with SAP"; break;
  844. case 0x14: p = "tri with SAP"; break;
  845. case 0xfe: p = "forced mode"; break;
  846. default: p = "not defined"; break;
  847. }
  848. CX18_INFO("Detected audio mode: %s\n", p);
  849. switch (mod_det_stat1) {
  850. case 0x00: p = "not defined"; break;
  851. case 0x01: p = "EIAJ"; break;
  852. case 0x02: p = "A2-M"; break;
  853. case 0x03: p = "A2-BG"; break;
  854. case 0x04: p = "A2-DK1"; break;
  855. case 0x05: p = "A2-DK2"; break;
  856. case 0x06: p = "A2-DK3"; break;
  857. case 0x07: p = "A1 (6.0 MHz FM Mono)"; break;
  858. case 0x08: p = "AM-L"; break;
  859. case 0x09: p = "NICAM-BG"; break;
  860. case 0x0a: p = "NICAM-DK"; break;
  861. case 0x0b: p = "NICAM-I"; break;
  862. case 0x0c: p = "NICAM-L"; break;
  863. case 0x0d: p = "BTSC/EIAJ/A2-M Mono (4.5 MHz FMMono)"; break;
  864. case 0x0e: p = "IF FM Radio"; break;
  865. case 0x0f: p = "BTSC"; break;
  866. case 0x10: p = "detected chrominance"; break;
  867. case 0xfd: p = "unknown audio standard"; break;
  868. case 0xfe: p = "forced audio standard"; break;
  869. case 0xff: p = "no detected audio standard"; break;
  870. default: p = "not defined"; break;
  871. }
  872. CX18_INFO("Detected audio standard: %s\n", p);
  873. CX18_INFO("Audio muted: %s\n",
  874. (mute_ctl & 0x2) ? "yes" : "no");
  875. CX18_INFO("Audio microcontroller: %s\n",
  876. (download_ctl & 0x10) ? "running" : "stopped");
  877. switch (audio_config >> 4) {
  878. case 0x00: p = "undefined"; break;
  879. case 0x01: p = "BTSC"; break;
  880. case 0x02: p = "EIAJ"; break;
  881. case 0x03: p = "A2-M"; break;
  882. case 0x04: p = "A2-BG"; break;
  883. case 0x05: p = "A2-DK1"; break;
  884. case 0x06: p = "A2-DK2"; break;
  885. case 0x07: p = "A2-DK3"; break;
  886. case 0x08: p = "A1 (6.0 MHz FM Mono)"; break;
  887. case 0x09: p = "AM-L"; break;
  888. case 0x0a: p = "NICAM-BG"; break;
  889. case 0x0b: p = "NICAM-DK"; break;
  890. case 0x0c: p = "NICAM-I"; break;
  891. case 0x0d: p = "NICAM-L"; break;
  892. case 0x0e: p = "FM radio"; break;
  893. case 0x0f: p = "automatic detection"; break;
  894. default: p = "undefined"; break;
  895. }
  896. CX18_INFO("Configured audio standard: %s\n", p);
  897. if ((audio_config >> 4) < 0xF) {
  898. switch (audio_config & 0xF) {
  899. case 0x00: p = "MONO1 (LANGUAGE A/Mono L+R channel for BTSC, EIAJ, A2)"; break;
  900. case 0x01: p = "MONO2 (LANGUAGE B)"; break;
  901. case 0x02: p = "MONO3 (STEREO forced MONO)"; break;
  902. case 0x03: p = "MONO4 (NICAM ANALOG-Language C/Analog Fallback)"; break;
  903. case 0x04: p = "STEREO"; break;
  904. case 0x05: p = "DUAL1 (AC)"; break;
  905. case 0x06: p = "DUAL2 (BC)"; break;
  906. case 0x07: p = "DUAL3 (AB)"; break;
  907. default: p = "undefined";
  908. }
  909. CX18_INFO("Configured audio mode: %s\n", p);
  910. } else {
  911. switch (audio_config & 0xF) {
  912. case 0x00: p = "BG"; break;
  913. case 0x01: p = "DK1"; break;
  914. case 0x02: p = "DK2"; break;
  915. case 0x03: p = "DK3"; break;
  916. case 0x04: p = "I"; break;
  917. case 0x05: p = "L"; break;
  918. case 0x06: p = "BTSC"; break;
  919. case 0x07: p = "EIAJ"; break;
  920. case 0x08: p = "A2-M"; break;
  921. case 0x09: p = "FM Radio (4.5 MHz)"; break;
  922. case 0x0a: p = "FM Radio (5.5 MHz)"; break;
  923. case 0x0b: p = "S-Video"; break;
  924. case 0x0f: p = "automatic standard and mode detection"; break;
  925. default: p = "undefined"; break;
  926. }
  927. CX18_INFO("Configured audio system: %s\n", p);
  928. }
  929. if (aud_input)
  930. CX18_INFO("Specified audio input: Tuner (In%d)\n",
  931. aud_input);
  932. else
  933. CX18_INFO("Specified audio input: External\n");
  934. switch (pref_mode & 0xf) {
  935. case 0: p = "mono/language A"; break;
  936. case 1: p = "language B"; break;
  937. case 2: p = "language C"; break;
  938. case 3: p = "analog fallback"; break;
  939. case 4: p = "stereo"; break;
  940. case 5: p = "language AC"; break;
  941. case 6: p = "language BC"; break;
  942. case 7: p = "language AB"; break;
  943. default: p = "undefined"; break;
  944. }
  945. CX18_INFO("Preferred audio mode: %s\n", p);
  946. if ((audio_config & 0xf) == 0xf) {
  947. switch ((afc0 >> 3) & 0x1) {
  948. case 0: p = "system DK"; break;
  949. case 1: p = "system L"; break;
  950. }
  951. CX18_INFO("Selected 65 MHz format: %s\n", p);
  952. switch (afc0 & 0x7) {
  953. case 0: p = "Chroma"; break;
  954. case 1: p = "BTSC"; break;
  955. case 2: p = "EIAJ"; break;
  956. case 3: p = "A2-M"; break;
  957. case 4: p = "autodetect"; break;
  958. default: p = "undefined"; break;
  959. }
  960. CX18_INFO("Selected 45 MHz format: %s\n", p);
  961. }
  962. }
  963. static int cx18_av_log_status(struct v4l2_subdev *sd)
  964. {
  965. struct cx18 *cx = v4l2_get_subdevdata(sd);
  966. log_video_status(cx);
  967. log_audio_status(cx);
  968. return 0;
  969. }
  970. static inline int cx18_av_dbg_match(const struct v4l2_dbg_match *match)
  971. {
  972. return match->type == V4L2_CHIP_MATCH_HOST && match->addr == 1;
  973. }
  974. static int cx18_av_g_chip_ident(struct v4l2_subdev *sd,
  975. struct v4l2_dbg_chip_ident *chip)
  976. {
  977. struct cx18_av_state *state = to_cx18_av_state(sd);
  978. if (cx18_av_dbg_match(&chip->match)) {
  979. chip->ident = state->id;
  980. chip->revision = state->rev;
  981. }
  982. return 0;
  983. }
  984. #ifdef CONFIG_VIDEO_ADV_DEBUG
  985. static int cx18_av_g_register(struct v4l2_subdev *sd,
  986. struct v4l2_dbg_register *reg)
  987. {
  988. struct cx18 *cx = v4l2_get_subdevdata(sd);
  989. if (!cx18_av_dbg_match(&reg->match))
  990. return -EINVAL;
  991. if ((reg->reg & 0x3) != 0)
  992. return -EINVAL;
  993. if (!capable(CAP_SYS_ADMIN))
  994. return -EPERM;
  995. reg->size = 4;
  996. reg->val = cx18_av_read4(cx, reg->reg & 0x00000ffc);
  997. return 0;
  998. }
  999. static int cx18_av_s_register(struct v4l2_subdev *sd,
  1000. struct v4l2_dbg_register *reg)
  1001. {
  1002. struct cx18 *cx = v4l2_get_subdevdata(sd);
  1003. if (!cx18_av_dbg_match(&reg->match))
  1004. return -EINVAL;
  1005. if ((reg->reg & 0x3) != 0)
  1006. return -EINVAL;
  1007. if (!capable(CAP_SYS_ADMIN))
  1008. return -EPERM;
  1009. cx18_av_write4(cx, reg->reg & 0x00000ffc, reg->val);
  1010. return 0;
  1011. }
  1012. #endif
  1013. static const struct v4l2_subdev_core_ops cx18_av_general_ops = {
  1014. .g_chip_ident = cx18_av_g_chip_ident,
  1015. .log_status = cx18_av_log_status,
  1016. .init = cx18_av_init,
  1017. .reset = cx18_av_reset,
  1018. .queryctrl = cx18_av_queryctrl,
  1019. .g_ctrl = cx18_av_g_ctrl,
  1020. .s_ctrl = cx18_av_s_ctrl,
  1021. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1022. .g_register = cx18_av_g_register,
  1023. .s_register = cx18_av_s_register,
  1024. #endif
  1025. };
  1026. static const struct v4l2_subdev_tuner_ops cx18_av_tuner_ops = {
  1027. .s_radio = cx18_av_s_radio,
  1028. .s_frequency = cx18_av_s_frequency,
  1029. .g_tuner = cx18_av_g_tuner,
  1030. .s_tuner = cx18_av_s_tuner,
  1031. .s_std = cx18_av_s_std,
  1032. };
  1033. static const struct v4l2_subdev_audio_ops cx18_av_audio_ops = {
  1034. .s_clock_freq = cx18_av_s_clock_freq,
  1035. .s_routing = cx18_av_s_audio_routing,
  1036. };
  1037. static const struct v4l2_subdev_video_ops cx18_av_video_ops = {
  1038. .s_routing = cx18_av_s_video_routing,
  1039. .decode_vbi_line = cx18_av_decode_vbi_line,
  1040. .s_stream = cx18_av_s_stream,
  1041. .g_fmt = cx18_av_g_fmt,
  1042. .s_fmt = cx18_av_s_fmt,
  1043. };
  1044. static const struct v4l2_subdev_ops cx18_av_ops = {
  1045. .core = &cx18_av_general_ops,
  1046. .tuner = &cx18_av_tuner_ops,
  1047. .audio = &cx18_av_audio_ops,
  1048. .video = &cx18_av_video_ops,
  1049. };
  1050. int cx18_av_probe(struct cx18 *cx, struct v4l2_subdev **sd)
  1051. {
  1052. struct cx18_av_state *state = &cx->av_state;
  1053. state->rev = cx18_av_read4(cx, CXADEC_CHIP_CTRL) & 0xffff;
  1054. state->id = ((state->rev >> 4) == CXADEC_CHIP_TYPE_MAKO)
  1055. ? V4L2_IDENT_CX23418_843 : V4L2_IDENT_UNKNOWN;
  1056. state->vid_input = CX18_AV_COMPOSITE7;
  1057. state->aud_input = CX18_AV_AUDIO8;
  1058. state->audclk_freq = 48000;
  1059. state->audmode = V4L2_TUNER_MODE_LANG1;
  1060. state->slicer_line_delay = 0;
  1061. state->slicer_line_offset = (10 + state->slicer_line_delay - 2);
  1062. *sd = &state->sd;
  1063. v4l2_subdev_init(*sd, &cx18_av_ops);
  1064. v4l2_set_subdevdata(*sd, cx);
  1065. snprintf((*sd)->name, sizeof((*sd)->name),
  1066. "%s internal A/V decoder", cx->v4l2_dev.name);
  1067. (*sd)->grp_id = CX18_HW_CX23418;
  1068. return v4l2_device_register_subdev(&cx->v4l2_dev, *sd);
  1069. }
  1070. void cx18_av_exit(struct cx18 *cx, struct v4l2_subdev *sd)
  1071. {
  1072. v4l2_device_unregister_subdev(&cx->av_state.sd);
  1073. }