cx18-av-core.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248
  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. struct v4l2_subdev *sd = &state->sd;
  211. v4l2_std_id std = state->std;
  212. int hblank, hactive, burst, vblank, vactive, sc;
  213. int vblank656, src_decimation;
  214. int luma_lpf, uv_lpf, comb;
  215. u32 pll_int, pll_frac, pll_post;
  216. /* datasheet startup, step 8d */
  217. if (std & ~V4L2_STD_NTSC)
  218. cx18_av_write(cx, 0x49f, 0x11);
  219. else
  220. cx18_av_write(cx, 0x49f, 0x14);
  221. if (std & V4L2_STD_625_50) {
  222. /* FIXME - revisit these for Sliced VBI */
  223. hblank = 132;
  224. hactive = 720;
  225. burst = 93;
  226. vblank = 36;
  227. vactive = 580;
  228. vblank656 = 40;
  229. src_decimation = 0x21f;
  230. luma_lpf = 2;
  231. if (std & V4L2_STD_PAL) {
  232. uv_lpf = 1;
  233. comb = 0x20;
  234. sc = 688739;
  235. } else if (std == V4L2_STD_PAL_Nc) {
  236. uv_lpf = 1;
  237. comb = 0x20;
  238. sc = 556453;
  239. } else { /* SECAM */
  240. uv_lpf = 0;
  241. comb = 0;
  242. sc = 672351;
  243. }
  244. } else {
  245. /*
  246. * The following relationships of half line counts should hold:
  247. * 525 = vsync + vactive + vblank656
  248. * 12 = vblank656 - vblank
  249. *
  250. * vsync: always 6 half-lines of vsync pulses
  251. * vactive: half lines of active video
  252. * vblank656: half lines, after line 3, of blanked video
  253. * vblank: half lines, after line 9, of blanked video
  254. *
  255. * vblank656 starts counting from the falling edge of the first
  256. * vsync pulse (start of line 4)
  257. * vblank starts counting from the after the 6 vsync pulses and
  258. * 6 equalization pulses (start of line 10)
  259. *
  260. * For 525 line systems the driver will extract VBI information
  261. * from lines 10 through 21. To avoid the EAV RP code from
  262. * toggling at the start of hblank at line 22, where sliced VBI
  263. * data from line 21 is stuffed, also treat line 22 as blanked.
  264. */
  265. vblank656 = 38; /* lines 4 through 22 */
  266. vblank = 26; /* lines 10 through 22 */
  267. vactive = 481; /* lines 23 through 262.5 */
  268. hactive = 720;
  269. hblank = 122;
  270. luma_lpf = 1;
  271. uv_lpf = 1;
  272. src_decimation = 0x21f;
  273. if (std == V4L2_STD_PAL_60) {
  274. burst = 0x5b;
  275. luma_lpf = 2;
  276. comb = 0x20;
  277. sc = 688739;
  278. } else if (std == V4L2_STD_PAL_M) {
  279. burst = 0x61;
  280. comb = 0x20;
  281. sc = 555452;
  282. } else {
  283. burst = 0x5b;
  284. comb = 0x66;
  285. sc = 556063;
  286. }
  287. }
  288. /* DEBUG: Displays configured PLL frequency */
  289. pll_int = cx18_av_read(cx, 0x108);
  290. pll_frac = cx18_av_read4(cx, 0x10c) & 0x1ffffff;
  291. pll_post = cx18_av_read(cx, 0x109);
  292. CX18_DEBUG_INFO_DEV(sd, "PLL regs = int: %u, frac: %u, post: %u\n",
  293. pll_int, pll_frac, pll_post);
  294. if (pll_post) {
  295. int fin, fsc, pll;
  296. pll = (28636360L * ((((u64)pll_int) << 25) + pll_frac)) >> 25;
  297. pll /= pll_post;
  298. CX18_DEBUG_INFO_DEV(sd, "PLL = %d.%06d MHz\n",
  299. pll / 1000000, pll % 1000000);
  300. CX18_DEBUG_INFO_DEV(sd, "PLL/8 = %d.%06d MHz\n",
  301. pll / 8000000, (pll / 8) % 1000000);
  302. fin = ((u64)src_decimation * pll) >> 12;
  303. CX18_DEBUG_INFO_DEV(sd, "ADC Sampling freq = %d.%06d MHz\n",
  304. fin / 1000000, fin % 1000000);
  305. fsc = (((u64)sc) * pll) >> 24L;
  306. CX18_DEBUG_INFO_DEV(sd,
  307. "Chroma sub-carrier freq = %d.%06d MHz\n",
  308. fsc / 1000000, fsc % 1000000);
  309. CX18_DEBUG_INFO_DEV(sd, "hblank %i, hactive %i, vblank %i, "
  310. "vactive %i, vblank656 %i, src_dec %i, "
  311. "burst 0x%02x, luma_lpf %i, uv_lpf %i, "
  312. "comb 0x%02x, sc 0x%06x\n",
  313. hblank, hactive, vblank, vactive, vblank656,
  314. src_decimation, burst, luma_lpf, uv_lpf,
  315. comb, sc);
  316. }
  317. /* Sets horizontal blanking delay and active lines */
  318. cx18_av_write(cx, 0x470, hblank);
  319. cx18_av_write(cx, 0x471, 0xff & (((hblank >> 8) & 0x3) |
  320. (hactive << 4)));
  321. cx18_av_write(cx, 0x472, hactive >> 4);
  322. /* Sets burst gate delay */
  323. cx18_av_write(cx, 0x473, burst);
  324. /* Sets vertical blanking delay and active duration */
  325. cx18_av_write(cx, 0x474, vblank);
  326. cx18_av_write(cx, 0x475, 0xff & (((vblank >> 8) & 0x3) |
  327. (vactive << 4)));
  328. cx18_av_write(cx, 0x476, vactive >> 4);
  329. cx18_av_write(cx, 0x477, vblank656);
  330. /* Sets src decimation rate */
  331. cx18_av_write(cx, 0x478, 0xff & src_decimation);
  332. cx18_av_write(cx, 0x479, 0xff & (src_decimation >> 8));
  333. /* Sets Luma and UV Low pass filters */
  334. cx18_av_write(cx, 0x47a, luma_lpf << 6 | ((uv_lpf << 4) & 0x30));
  335. /* Enables comb filters */
  336. cx18_av_write(cx, 0x47b, comb);
  337. /* Sets SC Step*/
  338. cx18_av_write(cx, 0x47c, sc);
  339. cx18_av_write(cx, 0x47d, 0xff & sc >> 8);
  340. cx18_av_write(cx, 0x47e, 0xff & sc >> 16);
  341. if (std & V4L2_STD_625_50) {
  342. state->slicer_line_delay = 1;
  343. state->slicer_line_offset = (6 + state->slicer_line_delay - 2);
  344. } else {
  345. state->slicer_line_delay = 0;
  346. state->slicer_line_offset = (10 + state->slicer_line_delay - 2);
  347. }
  348. cx18_av_write(cx, 0x47f, state->slicer_line_delay);
  349. }
  350. static int cx18_av_decode_vbi_line(struct v4l2_subdev *sd,
  351. struct v4l2_decode_vbi_line *vbi_line)
  352. {
  353. struct cx18 *cx = v4l2_get_subdevdata(sd);
  354. return cx18_av_vbi(cx, VIDIOC_INT_DECODE_VBI_LINE, vbi_line);
  355. }
  356. static int cx18_av_s_clock_freq(struct v4l2_subdev *sd, u32 freq)
  357. {
  358. struct cx18 *cx = v4l2_get_subdevdata(sd);
  359. return cx18_av_audio(cx, VIDIOC_INT_AUDIO_CLOCK_FREQ, &freq);
  360. }
  361. static void input_change(struct cx18 *cx)
  362. {
  363. struct cx18_av_state *state = &cx->av_state;
  364. v4l2_std_id std = state->std;
  365. u8 v;
  366. /* Follow step 8c and 8d of section 3.16 in the cx18_av datasheet */
  367. cx18_av_write(cx, 0x49f, (std & V4L2_STD_NTSC) ? 0x14 : 0x11);
  368. cx18_av_and_or(cx, 0x401, ~0x60, 0);
  369. cx18_av_and_or(cx, 0x401, ~0x60, 0x60);
  370. if (std & V4L2_STD_525_60) {
  371. if (std == V4L2_STD_NTSC_M_JP) {
  372. /* Japan uses EIAJ audio standard */
  373. cx18_av_write_expect(cx, 0x808, 0xf7, 0xf7, 0xff);
  374. cx18_av_write_expect(cx, 0x80b, 0x02, 0x02, 0x3f);
  375. } else if (std == V4L2_STD_NTSC_M_KR) {
  376. /* South Korea uses A2 audio standard */
  377. cx18_av_write_expect(cx, 0x808, 0xf8, 0xf8, 0xff);
  378. cx18_av_write_expect(cx, 0x80b, 0x03, 0x03, 0x3f);
  379. } else {
  380. /* Others use the BTSC audio standard */
  381. cx18_av_write_expect(cx, 0x808, 0xf6, 0xf6, 0xff);
  382. cx18_av_write_expect(cx, 0x80b, 0x01, 0x01, 0x3f);
  383. }
  384. } else if (std & V4L2_STD_PAL) {
  385. /* Follow tuner change procedure for PAL */
  386. cx18_av_write_expect(cx, 0x808, 0xff, 0xff, 0xff);
  387. cx18_av_write_expect(cx, 0x80b, 0x03, 0x03, 0x3f);
  388. } else if (std & V4L2_STD_SECAM) {
  389. /* Select autodetect for SECAM */
  390. cx18_av_write_expect(cx, 0x808, 0xff, 0xff, 0xff);
  391. cx18_av_write_expect(cx, 0x80b, 0x03, 0x03, 0x3f);
  392. }
  393. v = cx18_av_read(cx, 0x803);
  394. if (v & 0x10) {
  395. /* restart audio decoder microcontroller */
  396. v &= ~0x10;
  397. cx18_av_write_expect(cx, 0x803, v, v, 0x1f);
  398. v |= 0x10;
  399. cx18_av_write_expect(cx, 0x803, v, v, 0x1f);
  400. }
  401. }
  402. static int cx18_av_s_frequency(struct v4l2_subdev *sd,
  403. struct v4l2_frequency *freq)
  404. {
  405. struct cx18 *cx = v4l2_get_subdevdata(sd);
  406. input_change(cx);
  407. return 0;
  408. }
  409. static int set_input(struct cx18 *cx, enum cx18_av_video_input vid_input,
  410. enum cx18_av_audio_input aud_input)
  411. {
  412. struct cx18_av_state *state = &cx->av_state;
  413. struct v4l2_subdev *sd = &state->sd;
  414. u8 is_composite = (vid_input >= CX18_AV_COMPOSITE1 &&
  415. vid_input <= CX18_AV_COMPOSITE8);
  416. u8 reg;
  417. u8 v;
  418. CX18_DEBUG_INFO_DEV(sd, "decoder set video input %d, audio input %d\n",
  419. vid_input, aud_input);
  420. if (is_composite) {
  421. reg = 0xf0 + (vid_input - CX18_AV_COMPOSITE1);
  422. } else {
  423. int luma = vid_input & 0xf0;
  424. int chroma = vid_input & 0xf00;
  425. if ((vid_input & ~0xff0) ||
  426. luma < CX18_AV_SVIDEO_LUMA1 ||
  427. luma > CX18_AV_SVIDEO_LUMA8 ||
  428. chroma < CX18_AV_SVIDEO_CHROMA4 ||
  429. chroma > CX18_AV_SVIDEO_CHROMA8) {
  430. CX18_ERR_DEV(sd, "0x%04x is not a valid video input!\n",
  431. vid_input);
  432. return -EINVAL;
  433. }
  434. reg = 0xf0 + ((luma - CX18_AV_SVIDEO_LUMA1) >> 4);
  435. if (chroma >= CX18_AV_SVIDEO_CHROMA7) {
  436. reg &= 0x3f;
  437. reg |= (chroma - CX18_AV_SVIDEO_CHROMA7) >> 2;
  438. } else {
  439. reg &= 0xcf;
  440. reg |= (chroma - CX18_AV_SVIDEO_CHROMA4) >> 4;
  441. }
  442. }
  443. switch (aud_input) {
  444. case CX18_AV_AUDIO_SERIAL1:
  445. case CX18_AV_AUDIO_SERIAL2:
  446. /* do nothing, use serial audio input */
  447. break;
  448. case CX18_AV_AUDIO4: reg &= ~0x30; break;
  449. case CX18_AV_AUDIO5: reg &= ~0x30; reg |= 0x10; break;
  450. case CX18_AV_AUDIO6: reg &= ~0x30; reg |= 0x20; break;
  451. case CX18_AV_AUDIO7: reg &= ~0xc0; break;
  452. case CX18_AV_AUDIO8: reg &= ~0xc0; reg |= 0x40; break;
  453. default:
  454. CX18_ERR_DEV(sd, "0x%04x is not a valid audio input!\n",
  455. aud_input);
  456. return -EINVAL;
  457. }
  458. cx18_av_write_expect(cx, 0x103, reg, reg, 0xf7);
  459. /* Set INPUT_MODE to Composite (0) or S-Video (1) */
  460. cx18_av_and_or(cx, 0x401, ~0x6, is_composite ? 0 : 0x02);
  461. /* Set CH_SEL_ADC2 to 1 if input comes from CH3 */
  462. v = cx18_av_read(cx, 0x102);
  463. if (reg & 0x80)
  464. v &= ~0x2;
  465. else
  466. v |= 0x2;
  467. /* Set DUAL_MODE_ADC2 to 1 if input comes from both CH2 and CH3 */
  468. if ((reg & 0xc0) != 0xc0 && (reg & 0x30) != 0x30)
  469. v |= 0x4;
  470. else
  471. v &= ~0x4;
  472. cx18_av_write_expect(cx, 0x102, v, v, 0x17);
  473. /*cx18_av_and_or4(cx, 0x104, ~0x001b4180, 0x00004180);*/
  474. state->vid_input = vid_input;
  475. state->aud_input = aud_input;
  476. cx18_av_audio_set_path(cx);
  477. input_change(cx);
  478. return 0;
  479. }
  480. static int cx18_av_s_video_routing(struct v4l2_subdev *sd,
  481. const struct v4l2_routing *route)
  482. {
  483. struct cx18_av_state *state = to_cx18_av_state(sd);
  484. struct cx18 *cx = v4l2_get_subdevdata(sd);
  485. return set_input(cx, route->input, state->aud_input);
  486. }
  487. static int cx18_av_s_audio_routing(struct v4l2_subdev *sd,
  488. const struct v4l2_routing *route)
  489. {
  490. struct cx18_av_state *state = to_cx18_av_state(sd);
  491. struct cx18 *cx = v4l2_get_subdevdata(sd);
  492. return set_input(cx, state->vid_input, route->input);
  493. }
  494. static int cx18_av_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
  495. {
  496. struct cx18_av_state *state = to_cx18_av_state(sd);
  497. struct cx18 *cx = v4l2_get_subdevdata(sd);
  498. u8 vpres;
  499. u8 mode;
  500. int val = 0;
  501. if (state->radio)
  502. return 0;
  503. vpres = cx18_av_read(cx, 0x40e) & 0x20;
  504. vt->signal = vpres ? 0xffff : 0x0;
  505. vt->capability |=
  506. V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_LANG1 |
  507. V4L2_TUNER_CAP_LANG2 | V4L2_TUNER_CAP_SAP;
  508. mode = cx18_av_read(cx, 0x804);
  509. /* get rxsubchans and audmode */
  510. if ((mode & 0xf) == 1)
  511. val |= V4L2_TUNER_SUB_STEREO;
  512. else
  513. val |= V4L2_TUNER_SUB_MONO;
  514. if (mode == 2 || mode == 4)
  515. val = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
  516. if (mode & 0x10)
  517. val |= V4L2_TUNER_SUB_SAP;
  518. vt->rxsubchans = val;
  519. vt->audmode = state->audmode;
  520. return 0;
  521. }
  522. static int cx18_av_s_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
  523. {
  524. struct cx18_av_state *state = to_cx18_av_state(sd);
  525. struct cx18 *cx = v4l2_get_subdevdata(sd);
  526. u8 v;
  527. if (state->radio)
  528. return 0;
  529. v = cx18_av_read(cx, 0x809);
  530. v &= ~0xf;
  531. switch (vt->audmode) {
  532. case V4L2_TUNER_MODE_MONO:
  533. /* mono -> mono
  534. stereo -> mono
  535. bilingual -> lang1 */
  536. break;
  537. case V4L2_TUNER_MODE_STEREO:
  538. case V4L2_TUNER_MODE_LANG1:
  539. /* mono -> mono
  540. stereo -> stereo
  541. bilingual -> lang1 */
  542. v |= 0x4;
  543. break;
  544. case V4L2_TUNER_MODE_LANG1_LANG2:
  545. /* mono -> mono
  546. stereo -> stereo
  547. bilingual -> lang1/lang2 */
  548. v |= 0x7;
  549. break;
  550. case V4L2_TUNER_MODE_LANG2:
  551. /* mono -> mono
  552. stereo -> stereo
  553. bilingual -> lang2 */
  554. v |= 0x1;
  555. break;
  556. default:
  557. return -EINVAL;
  558. }
  559. cx18_av_write_expect(cx, 0x809, v, v, 0xff);
  560. state->audmode = vt->audmode;
  561. return 0;
  562. }
  563. static int cx18_av_s_std(struct v4l2_subdev *sd, v4l2_std_id norm)
  564. {
  565. struct cx18_av_state *state = to_cx18_av_state(sd);
  566. struct cx18 *cx = v4l2_get_subdevdata(sd);
  567. u8 fmt = 0; /* zero is autodetect */
  568. u8 pal_m = 0;
  569. if (state->radio == 0 && state->std == norm)
  570. return 0;
  571. state->radio = 0;
  572. state->std = norm;
  573. /* First tests should be against specific std */
  574. if (state->std == V4L2_STD_NTSC_M_JP) {
  575. fmt = 0x2;
  576. } else if (state->std == V4L2_STD_NTSC_443) {
  577. fmt = 0x3;
  578. } else if (state->std == V4L2_STD_PAL_M) {
  579. pal_m = 1;
  580. fmt = 0x5;
  581. } else if (state->std == V4L2_STD_PAL_N) {
  582. fmt = 0x6;
  583. } else if (state->std == V4L2_STD_PAL_Nc) {
  584. fmt = 0x7;
  585. } else if (state->std == V4L2_STD_PAL_60) {
  586. fmt = 0x8;
  587. } else {
  588. /* Then, test against generic ones */
  589. if (state->std & V4L2_STD_NTSC)
  590. fmt = 0x1;
  591. else if (state->std & V4L2_STD_PAL)
  592. fmt = 0x4;
  593. else if (state->std & V4L2_STD_SECAM)
  594. fmt = 0xc;
  595. }
  596. CX18_DEBUG_INFO_DEV(sd, "changing video std to fmt %i\n", fmt);
  597. /* Follow step 9 of section 3.16 in the cx18_av datasheet.
  598. Without this PAL may display a vertical ghosting effect.
  599. This happens for example with the Yuan MPC622. */
  600. if (fmt >= 4 && fmt < 8) {
  601. /* Set format to NTSC-M */
  602. cx18_av_and_or(cx, 0x400, ~0xf, 1);
  603. /* Turn off LCOMB */
  604. cx18_av_and_or(cx, 0x47b, ~6, 0);
  605. }
  606. cx18_av_and_or(cx, 0x400, ~0x2f, fmt | 0x20);
  607. cx18_av_and_or(cx, 0x403, ~0x3, pal_m);
  608. cx18_av_std_setup(cx);
  609. input_change(cx);
  610. return 0;
  611. }
  612. static int cx18_av_s_radio(struct v4l2_subdev *sd)
  613. {
  614. struct cx18_av_state *state = to_cx18_av_state(sd);
  615. state->radio = 1;
  616. return 0;
  617. }
  618. static int cx18_av_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
  619. {
  620. struct cx18 *cx = v4l2_get_subdevdata(sd);
  621. switch (ctrl->id) {
  622. case V4L2_CID_BRIGHTNESS:
  623. if (ctrl->value < 0 || ctrl->value > 255) {
  624. CX18_ERR_DEV(sd, "invalid brightness setting %d\n",
  625. ctrl->value);
  626. return -ERANGE;
  627. }
  628. cx18_av_write(cx, 0x414, ctrl->value - 128);
  629. break;
  630. case V4L2_CID_CONTRAST:
  631. if (ctrl->value < 0 || ctrl->value > 127) {
  632. CX18_ERR_DEV(sd, "invalid contrast setting %d\n",
  633. ctrl->value);
  634. return -ERANGE;
  635. }
  636. cx18_av_write(cx, 0x415, ctrl->value << 1);
  637. break;
  638. case V4L2_CID_SATURATION:
  639. if (ctrl->value < 0 || ctrl->value > 127) {
  640. CX18_ERR_DEV(sd, "invalid saturation setting %d\n",
  641. ctrl->value);
  642. return -ERANGE;
  643. }
  644. cx18_av_write(cx, 0x420, ctrl->value << 1);
  645. cx18_av_write(cx, 0x421, ctrl->value << 1);
  646. break;
  647. case V4L2_CID_HUE:
  648. if (ctrl->value < -128 || ctrl->value > 127) {
  649. CX18_ERR_DEV(sd, "invalid hue setting %d\n",
  650. ctrl->value);
  651. return -ERANGE;
  652. }
  653. cx18_av_write(cx, 0x422, ctrl->value);
  654. break;
  655. case V4L2_CID_AUDIO_VOLUME:
  656. case V4L2_CID_AUDIO_BASS:
  657. case V4L2_CID_AUDIO_TREBLE:
  658. case V4L2_CID_AUDIO_BALANCE:
  659. case V4L2_CID_AUDIO_MUTE:
  660. return cx18_av_audio(cx, VIDIOC_S_CTRL, ctrl);
  661. default:
  662. return -EINVAL;
  663. }
  664. return 0;
  665. }
  666. static int cx18_av_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
  667. {
  668. struct cx18 *cx = v4l2_get_subdevdata(sd);
  669. switch (ctrl->id) {
  670. case V4L2_CID_BRIGHTNESS:
  671. ctrl->value = (s8)cx18_av_read(cx, 0x414) + 128;
  672. break;
  673. case V4L2_CID_CONTRAST:
  674. ctrl->value = cx18_av_read(cx, 0x415) >> 1;
  675. break;
  676. case V4L2_CID_SATURATION:
  677. ctrl->value = cx18_av_read(cx, 0x420) >> 1;
  678. break;
  679. case V4L2_CID_HUE:
  680. ctrl->value = (s8)cx18_av_read(cx, 0x422);
  681. break;
  682. case V4L2_CID_AUDIO_VOLUME:
  683. case V4L2_CID_AUDIO_BASS:
  684. case V4L2_CID_AUDIO_TREBLE:
  685. case V4L2_CID_AUDIO_BALANCE:
  686. case V4L2_CID_AUDIO_MUTE:
  687. return cx18_av_audio(cx, VIDIOC_G_CTRL, ctrl);
  688. default:
  689. return -EINVAL;
  690. }
  691. return 0;
  692. }
  693. static int cx18_av_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc)
  694. {
  695. struct cx18_av_state *state = to_cx18_av_state(sd);
  696. switch (qc->id) {
  697. case V4L2_CID_BRIGHTNESS:
  698. return v4l2_ctrl_query_fill(qc, 0, 255, 1, 128);
  699. case V4L2_CID_CONTRAST:
  700. case V4L2_CID_SATURATION:
  701. return v4l2_ctrl_query_fill(qc, 0, 127, 1, 64);
  702. case V4L2_CID_HUE:
  703. return v4l2_ctrl_query_fill(qc, -128, 127, 1, 0);
  704. default:
  705. break;
  706. }
  707. switch (qc->id) {
  708. case V4L2_CID_AUDIO_VOLUME:
  709. return v4l2_ctrl_query_fill(qc, 0, 65535,
  710. 65535 / 100, state->default_volume);
  711. case V4L2_CID_AUDIO_MUTE:
  712. return v4l2_ctrl_query_fill(qc, 0, 1, 1, 0);
  713. case V4L2_CID_AUDIO_BALANCE:
  714. case V4L2_CID_AUDIO_BASS:
  715. case V4L2_CID_AUDIO_TREBLE:
  716. return v4l2_ctrl_query_fill(qc, 0, 65535, 65535 / 100, 32768);
  717. default:
  718. return -EINVAL;
  719. }
  720. return -EINVAL;
  721. }
  722. static int cx18_av_g_fmt(struct v4l2_subdev *sd, struct v4l2_format *fmt)
  723. {
  724. struct cx18 *cx = v4l2_get_subdevdata(sd);
  725. switch (fmt->type) {
  726. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  727. return cx18_av_vbi(cx, VIDIOC_G_FMT, fmt);
  728. default:
  729. return -EINVAL;
  730. }
  731. return 0;
  732. }
  733. static int cx18_av_s_fmt(struct v4l2_subdev *sd, struct v4l2_format *fmt)
  734. {
  735. struct cx18_av_state *state = to_cx18_av_state(sd);
  736. struct cx18 *cx = v4l2_get_subdevdata(sd);
  737. struct v4l2_pix_format *pix;
  738. int HSC, VSC, Vsrc, Hsrc, filter, Vlines;
  739. int is_50Hz = !(state->std & V4L2_STD_525_60);
  740. switch (fmt->type) {
  741. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  742. pix = &(fmt->fmt.pix);
  743. Vsrc = (cx18_av_read(cx, 0x476) & 0x3f) << 4;
  744. Vsrc |= (cx18_av_read(cx, 0x475) & 0xf0) >> 4;
  745. Hsrc = (cx18_av_read(cx, 0x472) & 0x3f) << 4;
  746. Hsrc |= (cx18_av_read(cx, 0x471) & 0xf0) >> 4;
  747. Vlines = pix->height + (is_50Hz ? 4 : 7);
  748. if ((pix->width * 16 < Hsrc) || (Hsrc < pix->width) ||
  749. (Vlines * 8 < Vsrc) || (Vsrc < Vlines)) {
  750. CX18_ERR_DEV(sd, "%dx%d is not a valid size!\n",
  751. pix->width, pix->height);
  752. return -ERANGE;
  753. }
  754. HSC = (Hsrc * (1 << 20)) / pix->width - (1 << 20);
  755. VSC = (1 << 16) - (Vsrc * (1 << 9) / Vlines - (1 << 9));
  756. VSC &= 0x1fff;
  757. if (pix->width >= 385)
  758. filter = 0;
  759. else if (pix->width > 192)
  760. filter = 1;
  761. else if (pix->width > 96)
  762. filter = 2;
  763. else
  764. filter = 3;
  765. CX18_DEBUG_INFO_DEV(sd,
  766. "decoder set size %dx%d -> scale %ux%u\n",
  767. pix->width, pix->height, HSC, VSC);
  768. /* HSCALE=HSC */
  769. cx18_av_write(cx, 0x418, HSC & 0xff);
  770. cx18_av_write(cx, 0x419, (HSC >> 8) & 0xff);
  771. cx18_av_write(cx, 0x41a, HSC >> 16);
  772. /* VSCALE=VSC */
  773. cx18_av_write(cx, 0x41c, VSC & 0xff);
  774. cx18_av_write(cx, 0x41d, VSC >> 8);
  775. /* VS_INTRLACE=1 VFILT=filter */
  776. cx18_av_write(cx, 0x41e, 0x8 | filter);
  777. break;
  778. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  779. return cx18_av_vbi(cx, VIDIOC_S_FMT, fmt);
  780. case V4L2_BUF_TYPE_VBI_CAPTURE:
  781. return cx18_av_vbi(cx, VIDIOC_S_FMT, fmt);
  782. default:
  783. return -EINVAL;
  784. }
  785. return 0;
  786. }
  787. static int cx18_av_s_stream(struct v4l2_subdev *sd, int enable)
  788. {
  789. struct cx18 *cx = v4l2_get_subdevdata(sd);
  790. CX18_DEBUG_INFO_DEV(sd, "%s output\n", enable ? "enable" : "disable");
  791. if (enable) {
  792. cx18_av_write(cx, 0x115, 0x8c);
  793. cx18_av_write(cx, 0x116, 0x07);
  794. } else {
  795. cx18_av_write(cx, 0x115, 0x00);
  796. cx18_av_write(cx, 0x116, 0x00);
  797. }
  798. return 0;
  799. }
  800. static void log_video_status(struct cx18 *cx)
  801. {
  802. static const char *const fmt_strs[] = {
  803. "0x0",
  804. "NTSC-M", "NTSC-J", "NTSC-4.43",
  805. "PAL-BDGHI", "PAL-M", "PAL-N", "PAL-Nc", "PAL-60",
  806. "0x9", "0xA", "0xB",
  807. "SECAM",
  808. "0xD", "0xE", "0xF"
  809. };
  810. struct cx18_av_state *state = &cx->av_state;
  811. struct v4l2_subdev *sd = &state->sd;
  812. u8 vidfmt_sel = cx18_av_read(cx, 0x400) & 0xf;
  813. u8 gen_stat1 = cx18_av_read(cx, 0x40d);
  814. u8 gen_stat2 = cx18_av_read(cx, 0x40e);
  815. int vid_input = state->vid_input;
  816. CX18_INFO_DEV(sd, "Video signal: %spresent\n",
  817. (gen_stat2 & 0x20) ? "" : "not ");
  818. CX18_INFO_DEV(sd, "Detected format: %s\n",
  819. fmt_strs[gen_stat1 & 0xf]);
  820. CX18_INFO_DEV(sd, "Specified standard: %s\n",
  821. vidfmt_sel ? fmt_strs[vidfmt_sel]
  822. : "automatic detection");
  823. if (vid_input >= CX18_AV_COMPOSITE1 &&
  824. vid_input <= CX18_AV_COMPOSITE8) {
  825. CX18_INFO_DEV(sd, "Specified video input: Composite %d\n",
  826. vid_input - CX18_AV_COMPOSITE1 + 1);
  827. } else {
  828. CX18_INFO_DEV(sd, "Specified video input: "
  829. "S-Video (Luma In%d, Chroma In%d)\n",
  830. (vid_input & 0xf0) >> 4,
  831. (vid_input & 0xf00) >> 8);
  832. }
  833. CX18_INFO_DEV(sd, "Specified audioclock freq: %d Hz\n",
  834. state->audclk_freq);
  835. }
  836. static void log_audio_status(struct cx18 *cx)
  837. {
  838. struct cx18_av_state *state = &cx->av_state;
  839. struct v4l2_subdev *sd = &state->sd;
  840. u8 download_ctl = cx18_av_read(cx, 0x803);
  841. u8 mod_det_stat0 = cx18_av_read(cx, 0x804);
  842. u8 mod_det_stat1 = cx18_av_read(cx, 0x805);
  843. u8 audio_config = cx18_av_read(cx, 0x808);
  844. u8 pref_mode = cx18_av_read(cx, 0x809);
  845. u8 afc0 = cx18_av_read(cx, 0x80b);
  846. u8 mute_ctl = cx18_av_read(cx, 0x8d3);
  847. int aud_input = state->aud_input;
  848. char *p;
  849. switch (mod_det_stat0) {
  850. case 0x00: p = "mono"; break;
  851. case 0x01: p = "stereo"; break;
  852. case 0x02: p = "dual"; break;
  853. case 0x04: p = "tri"; break;
  854. case 0x10: p = "mono with SAP"; break;
  855. case 0x11: p = "stereo with SAP"; break;
  856. case 0x12: p = "dual with SAP"; break;
  857. case 0x14: p = "tri with SAP"; break;
  858. case 0xfe: p = "forced mode"; break;
  859. default: p = "not defined"; break;
  860. }
  861. CX18_INFO_DEV(sd, "Detected audio mode: %s\n", p);
  862. switch (mod_det_stat1) {
  863. case 0x00: p = "not defined"; break;
  864. case 0x01: p = "EIAJ"; break;
  865. case 0x02: p = "A2-M"; break;
  866. case 0x03: p = "A2-BG"; break;
  867. case 0x04: p = "A2-DK1"; break;
  868. case 0x05: p = "A2-DK2"; break;
  869. case 0x06: p = "A2-DK3"; break;
  870. case 0x07: p = "A1 (6.0 MHz FM Mono)"; break;
  871. case 0x08: p = "AM-L"; break;
  872. case 0x09: p = "NICAM-BG"; break;
  873. case 0x0a: p = "NICAM-DK"; break;
  874. case 0x0b: p = "NICAM-I"; break;
  875. case 0x0c: p = "NICAM-L"; break;
  876. case 0x0d: p = "BTSC/EIAJ/A2-M Mono (4.5 MHz FMMono)"; break;
  877. case 0x0e: p = "IF FM Radio"; break;
  878. case 0x0f: p = "BTSC"; break;
  879. case 0x10: p = "detected chrominance"; break;
  880. case 0xfd: p = "unknown audio standard"; break;
  881. case 0xfe: p = "forced audio standard"; break;
  882. case 0xff: p = "no detected audio standard"; break;
  883. default: p = "not defined"; break;
  884. }
  885. CX18_INFO_DEV(sd, "Detected audio standard: %s\n", p);
  886. CX18_INFO_DEV(sd, "Audio muted: %s\n",
  887. (mute_ctl & 0x2) ? "yes" : "no");
  888. CX18_INFO_DEV(sd, "Audio microcontroller: %s\n",
  889. (download_ctl & 0x10) ? "running" : "stopped");
  890. switch (audio_config >> 4) {
  891. case 0x00: p = "undefined"; break;
  892. case 0x01: p = "BTSC"; break;
  893. case 0x02: p = "EIAJ"; break;
  894. case 0x03: p = "A2-M"; break;
  895. case 0x04: p = "A2-BG"; break;
  896. case 0x05: p = "A2-DK1"; break;
  897. case 0x06: p = "A2-DK2"; break;
  898. case 0x07: p = "A2-DK3"; break;
  899. case 0x08: p = "A1 (6.0 MHz FM Mono)"; break;
  900. case 0x09: p = "AM-L"; break;
  901. case 0x0a: p = "NICAM-BG"; break;
  902. case 0x0b: p = "NICAM-DK"; break;
  903. case 0x0c: p = "NICAM-I"; break;
  904. case 0x0d: p = "NICAM-L"; break;
  905. case 0x0e: p = "FM radio"; break;
  906. case 0x0f: p = "automatic detection"; break;
  907. default: p = "undefined"; break;
  908. }
  909. CX18_INFO_DEV(sd, "Configured audio standard: %s\n", p);
  910. if ((audio_config >> 4) < 0xF) {
  911. switch (audio_config & 0xF) {
  912. case 0x00: p = "MONO1 (LANGUAGE A/Mono L+R channel for BTSC, EIAJ, A2)"; break;
  913. case 0x01: p = "MONO2 (LANGUAGE B)"; break;
  914. case 0x02: p = "MONO3 (STEREO forced MONO)"; break;
  915. case 0x03: p = "MONO4 (NICAM ANALOG-Language C/Analog Fallback)"; break;
  916. case 0x04: p = "STEREO"; break;
  917. case 0x05: p = "DUAL1 (AC)"; break;
  918. case 0x06: p = "DUAL2 (BC)"; break;
  919. case 0x07: p = "DUAL3 (AB)"; break;
  920. default: p = "undefined";
  921. }
  922. CX18_INFO_DEV(sd, "Configured audio mode: %s\n", p);
  923. } else {
  924. switch (audio_config & 0xF) {
  925. case 0x00: p = "BG"; break;
  926. case 0x01: p = "DK1"; break;
  927. case 0x02: p = "DK2"; break;
  928. case 0x03: p = "DK3"; break;
  929. case 0x04: p = "I"; break;
  930. case 0x05: p = "L"; break;
  931. case 0x06: p = "BTSC"; break;
  932. case 0x07: p = "EIAJ"; break;
  933. case 0x08: p = "A2-M"; break;
  934. case 0x09: p = "FM Radio (4.5 MHz)"; break;
  935. case 0x0a: p = "FM Radio (5.5 MHz)"; break;
  936. case 0x0b: p = "S-Video"; break;
  937. case 0x0f: p = "automatic standard and mode detection"; break;
  938. default: p = "undefined"; break;
  939. }
  940. CX18_INFO_DEV(sd, "Configured audio system: %s\n", p);
  941. }
  942. if (aud_input)
  943. CX18_INFO_DEV(sd, "Specified audio input: Tuner (In%d)\n",
  944. aud_input);
  945. else
  946. CX18_INFO_DEV(sd, "Specified audio input: External\n");
  947. switch (pref_mode & 0xf) {
  948. case 0: p = "mono/language A"; break;
  949. case 1: p = "language B"; break;
  950. case 2: p = "language C"; break;
  951. case 3: p = "analog fallback"; break;
  952. case 4: p = "stereo"; break;
  953. case 5: p = "language AC"; break;
  954. case 6: p = "language BC"; break;
  955. case 7: p = "language AB"; break;
  956. default: p = "undefined"; break;
  957. }
  958. CX18_INFO_DEV(sd, "Preferred audio mode: %s\n", p);
  959. if ((audio_config & 0xf) == 0xf) {
  960. switch ((afc0 >> 3) & 0x1) {
  961. case 0: p = "system DK"; break;
  962. case 1: p = "system L"; break;
  963. }
  964. CX18_INFO_DEV(sd, "Selected 65 MHz format: %s\n", p);
  965. switch (afc0 & 0x7) {
  966. case 0: p = "Chroma"; break;
  967. case 1: p = "BTSC"; break;
  968. case 2: p = "EIAJ"; break;
  969. case 3: p = "A2-M"; break;
  970. case 4: p = "autodetect"; break;
  971. default: p = "undefined"; break;
  972. }
  973. CX18_INFO_DEV(sd, "Selected 45 MHz format: %s\n", p);
  974. }
  975. }
  976. static int cx18_av_log_status(struct v4l2_subdev *sd)
  977. {
  978. struct cx18 *cx = v4l2_get_subdevdata(sd);
  979. log_video_status(cx);
  980. log_audio_status(cx);
  981. return 0;
  982. }
  983. static inline int cx18_av_dbg_match(const struct v4l2_dbg_match *match)
  984. {
  985. return match->type == V4L2_CHIP_MATCH_HOST && match->addr == 1;
  986. }
  987. static int cx18_av_g_chip_ident(struct v4l2_subdev *sd,
  988. struct v4l2_dbg_chip_ident *chip)
  989. {
  990. struct cx18_av_state *state = to_cx18_av_state(sd);
  991. if (cx18_av_dbg_match(&chip->match)) {
  992. chip->ident = state->id;
  993. chip->revision = state->rev;
  994. }
  995. return 0;
  996. }
  997. #ifdef CONFIG_VIDEO_ADV_DEBUG
  998. static int cx18_av_g_register(struct v4l2_subdev *sd,
  999. struct v4l2_dbg_register *reg)
  1000. {
  1001. struct cx18 *cx = v4l2_get_subdevdata(sd);
  1002. if (!cx18_av_dbg_match(&reg->match))
  1003. return -EINVAL;
  1004. if ((reg->reg & 0x3) != 0)
  1005. return -EINVAL;
  1006. if (!capable(CAP_SYS_ADMIN))
  1007. return -EPERM;
  1008. reg->size = 4;
  1009. reg->val = cx18_av_read4(cx, reg->reg & 0x00000ffc);
  1010. return 0;
  1011. }
  1012. static int cx18_av_s_register(struct v4l2_subdev *sd,
  1013. struct v4l2_dbg_register *reg)
  1014. {
  1015. struct cx18 *cx = v4l2_get_subdevdata(sd);
  1016. if (!cx18_av_dbg_match(&reg->match))
  1017. return -EINVAL;
  1018. if ((reg->reg & 0x3) != 0)
  1019. return -EINVAL;
  1020. if (!capable(CAP_SYS_ADMIN))
  1021. return -EPERM;
  1022. cx18_av_write4(cx, reg->reg & 0x00000ffc, reg->val);
  1023. return 0;
  1024. }
  1025. #endif
  1026. static const struct v4l2_subdev_core_ops cx18_av_general_ops = {
  1027. .g_chip_ident = cx18_av_g_chip_ident,
  1028. .log_status = cx18_av_log_status,
  1029. .init = cx18_av_init,
  1030. .reset = cx18_av_reset,
  1031. .queryctrl = cx18_av_queryctrl,
  1032. .g_ctrl = cx18_av_g_ctrl,
  1033. .s_ctrl = cx18_av_s_ctrl,
  1034. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1035. .g_register = cx18_av_g_register,
  1036. .s_register = cx18_av_s_register,
  1037. #endif
  1038. };
  1039. static const struct v4l2_subdev_tuner_ops cx18_av_tuner_ops = {
  1040. .s_radio = cx18_av_s_radio,
  1041. .s_frequency = cx18_av_s_frequency,
  1042. .g_tuner = cx18_av_g_tuner,
  1043. .s_tuner = cx18_av_s_tuner,
  1044. .s_std = cx18_av_s_std,
  1045. };
  1046. static const struct v4l2_subdev_audio_ops cx18_av_audio_ops = {
  1047. .s_clock_freq = cx18_av_s_clock_freq,
  1048. .s_routing = cx18_av_s_audio_routing,
  1049. };
  1050. static const struct v4l2_subdev_video_ops cx18_av_video_ops = {
  1051. .s_routing = cx18_av_s_video_routing,
  1052. .decode_vbi_line = cx18_av_decode_vbi_line,
  1053. .s_stream = cx18_av_s_stream,
  1054. .g_fmt = cx18_av_g_fmt,
  1055. .s_fmt = cx18_av_s_fmt,
  1056. };
  1057. static const struct v4l2_subdev_ops cx18_av_ops = {
  1058. .core = &cx18_av_general_ops,
  1059. .tuner = &cx18_av_tuner_ops,
  1060. .audio = &cx18_av_audio_ops,
  1061. .video = &cx18_av_video_ops,
  1062. };
  1063. int cx18_av_probe(struct cx18 *cx)
  1064. {
  1065. struct cx18_av_state *state = &cx->av_state;
  1066. struct v4l2_subdev *sd;
  1067. state->rev = cx18_av_read4(cx, CXADEC_CHIP_CTRL) & 0xffff;
  1068. state->id = ((state->rev >> 4) == CXADEC_CHIP_TYPE_MAKO)
  1069. ? V4L2_IDENT_CX23418_843 : V4L2_IDENT_UNKNOWN;
  1070. state->vid_input = CX18_AV_COMPOSITE7;
  1071. state->aud_input = CX18_AV_AUDIO8;
  1072. state->audclk_freq = 48000;
  1073. state->audmode = V4L2_TUNER_MODE_LANG1;
  1074. state->slicer_line_delay = 0;
  1075. state->slicer_line_offset = (10 + state->slicer_line_delay - 2);
  1076. sd = &state->sd;
  1077. v4l2_subdev_init(sd, &cx18_av_ops);
  1078. v4l2_set_subdevdata(sd, cx);
  1079. snprintf(sd->name, sizeof(sd->name),
  1080. "%s %03x", cx->v4l2_dev.name, (state->rev >> 4));
  1081. sd->grp_id = CX18_HW_418_AV;
  1082. return v4l2_device_register_subdev(&cx->v4l2_dev, sd);
  1083. }