cx18-av-core.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  1. /*
  2. * cx18 ADEC audio functions
  3. *
  4. * Derived from cx25840-core.c
  5. *
  6. * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
  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 "cx18-driver.h"
  24. int cx18_av_write(struct cx18 *cx, u16 addr, u8 value)
  25. {
  26. u32 x = readl(cx->reg_mem + 0xc40000 + (addr & ~3));
  27. u32 mask = 0xff;
  28. int shift = (addr & 3) * 8;
  29. x = (x & ~(mask << shift)) | ((u32)value << shift);
  30. writel(x, cx->reg_mem + 0xc40000 + (addr & ~3));
  31. return 0;
  32. }
  33. int cx18_av_write4(struct cx18 *cx, u16 addr, u32 value)
  34. {
  35. writel(value, cx->reg_mem + 0xc40000 + addr);
  36. return 0;
  37. }
  38. u8 cx18_av_read(struct cx18 *cx, u16 addr)
  39. {
  40. u32 x = readl(cx->reg_mem + 0xc40000 + (addr & ~3));
  41. int shift = (addr & 3) * 8;
  42. return (x >> shift) & 0xff;
  43. }
  44. u32 cx18_av_read4(struct cx18 *cx, u16 addr)
  45. {
  46. return readl(cx->reg_mem + 0xc40000 + addr);
  47. }
  48. int cx18_av_and_or(struct cx18 *cx, u16 addr, unsigned and_mask,
  49. u8 or_value)
  50. {
  51. return cx18_av_write(cx, addr,
  52. (cx18_av_read(cx, addr) & and_mask) |
  53. or_value);
  54. }
  55. int cx18_av_and_or4(struct cx18 *cx, u16 addr, u32 and_mask,
  56. u32 or_value)
  57. {
  58. return cx18_av_write4(cx, addr,
  59. (cx18_av_read4(cx, addr) & and_mask) |
  60. or_value);
  61. }
  62. /* ----------------------------------------------------------------------- */
  63. static int set_input(struct cx18 *cx, enum cx18_av_video_input vid_input,
  64. enum cx18_av_audio_input aud_input);
  65. static void log_audio_status(struct cx18 *cx);
  66. static void log_video_status(struct cx18 *cx);
  67. /* ----------------------------------------------------------------------- */
  68. static void cx18_av_initialize(struct cx18 *cx)
  69. {
  70. u32 v;
  71. cx18_av_loadfw(cx);
  72. /* Stop 8051 code execution */
  73. cx18_av_write4(cx, CXADEC_DL_CTL, 0x03000000);
  74. /* initallize the PLL by toggling sleep bit */
  75. v = cx18_av_read4(cx, CXADEC_HOST_REG1);
  76. /* enable sleep mode */
  77. cx18_av_write4(cx, CXADEC_HOST_REG1, v | 1);
  78. /* disable sleep mode */
  79. cx18_av_write4(cx, CXADEC_HOST_REG1, v & 0xfffe);
  80. /* initialize DLLs */
  81. v = cx18_av_read4(cx, CXADEC_DLL1_DIAG_CTRL) & 0xE1FFFEFF;
  82. /* disable FLD */
  83. cx18_av_write4(cx, CXADEC_DLL1_DIAG_CTRL, v);
  84. /* enable FLD */
  85. cx18_av_write4(cx, CXADEC_DLL1_DIAG_CTRL, v | 0x10000100);
  86. v = cx18_av_read4(cx, CXADEC_DLL2_DIAG_CTRL) & 0xE1FFFEFF;
  87. /* disable FLD */
  88. cx18_av_write4(cx, CXADEC_DLL2_DIAG_CTRL, v);
  89. /* enable FLD */
  90. cx18_av_write4(cx, CXADEC_DLL2_DIAG_CTRL, v | 0x06000100);
  91. /* set analog bias currents. Set Vreg to 1.20V. */
  92. cx18_av_write4(cx, CXADEC_AFE_DIAG_CTRL1, 0x000A1802);
  93. v = cx18_av_read4(cx, CXADEC_AFE_DIAG_CTRL3) | 1;
  94. /* enable TUNE_FIL_RST */
  95. cx18_av_write4(cx, CXADEC_AFE_DIAG_CTRL3, v);
  96. /* disable TUNE_FIL_RST */
  97. cx18_av_write4(cx, CXADEC_AFE_DIAG_CTRL3, v & 0xFFFFFFFE);
  98. /* enable 656 output */
  99. cx18_av_and_or4(cx, CXADEC_PIN_CTRL1, ~0, 0x040C00);
  100. /* video output drive strength */
  101. cx18_av_and_or4(cx, CXADEC_PIN_CTRL2, ~0, 0x2);
  102. /* reset video */
  103. cx18_av_write4(cx, CXADEC_SOFT_RST_CTRL, 0x8000);
  104. cx18_av_write4(cx, CXADEC_SOFT_RST_CTRL, 0);
  105. /* set video to auto-detect */
  106. /* Clear bits 11-12 to enable slow locking mode. Set autodetect mode */
  107. /* set the comb notch = 1 */
  108. cx18_av_and_or4(cx, CXADEC_MODE_CTRL, 0xFFF7E7F0, 0x02040800);
  109. /* Enable wtw_en in CRUSH_CTRL (Set bit 22) */
  110. /* Enable maj_sel in CRUSH_CTRL (Set bit 20) */
  111. cx18_av_and_or4(cx, CXADEC_CRUSH_CTRL, ~0, 0x00500000);
  112. /* Set VGA_TRACK_RANGE to 0x20 */
  113. cx18_av_and_or4(cx, CXADEC_DFE_CTRL2, 0xFFFF00FF, 0x00002000);
  114. /* Enable VBI capture */
  115. cx18_av_write4(cx, CXADEC_OUT_CTRL1, 0x4010253F);
  116. /* cx18_av_write4(cx, CXADEC_OUT_CTRL1, 0x4010253E); */
  117. /* Set the video input.
  118. The setting in MODE_CTRL gets lost when we do the above setup */
  119. /* EncSetSignalStd(dwDevNum, pEnc->dwSigStd); */
  120. /* EncSetVideoInput(dwDevNum, pEnc->VidIndSelection); */
  121. v = cx18_av_read4(cx, CXADEC_AFE_CTRL);
  122. v &= 0xFFFBFFFF; /* turn OFF bit 18 for droop_comp_ch1 */
  123. v &= 0xFFFF7FFF; /* turn OFF bit 9 for clamp_sel_ch1 */
  124. v &= 0xFFFFFFFE; /* turn OFF bit 0 for 12db_ch1 */
  125. /* v |= 0x00000001;*/ /* turn ON bit 0 for 12db_ch1 */
  126. cx18_av_write4(cx, CXADEC_AFE_CTRL, v);
  127. /* if(dwEnable && dw3DCombAvailable) { */
  128. /* CxDevWrReg(CXADEC_SRC_COMB_CFG, 0x7728021F); */
  129. /* } else { */
  130. /* CxDevWrReg(CXADEC_SRC_COMB_CFG, 0x6628021F); */
  131. /* } */
  132. cx18_av_write4(cx, CXADEC_SRC_COMB_CFG, 0x6628021F);
  133. }
  134. /* ----------------------------------------------------------------------- */
  135. static void input_change(struct cx18 *cx)
  136. {
  137. struct cx18_av_state *state = &cx->av_state;
  138. v4l2_std_id std = state->std;
  139. /* Follow step 8c and 8d of section 3.16 in the cx18_av datasheet */
  140. if (std & V4L2_STD_SECAM)
  141. cx18_av_write(cx, 0x402, 0);
  142. else {
  143. cx18_av_write(cx, 0x402, 0x04);
  144. cx18_av_write(cx, 0x49f, (std & V4L2_STD_NTSC) ? 0x14 : 0x11);
  145. }
  146. cx18_av_and_or(cx, 0x401, ~0x60, 0);
  147. cx18_av_and_or(cx, 0x401, ~0x60, 0x60);
  148. if (std & V4L2_STD_525_60) {
  149. if (std == V4L2_STD_NTSC_M_JP) {
  150. /* Japan uses EIAJ audio standard */
  151. cx18_av_write(cx, 0x808, 0xf7);
  152. cx18_av_write(cx, 0x80b, 0x02);
  153. } else if (std == V4L2_STD_NTSC_M_KR) {
  154. /* South Korea uses A2 audio standard */
  155. cx18_av_write(cx, 0x808, 0xf8);
  156. cx18_av_write(cx, 0x80b, 0x03);
  157. } else {
  158. /* Others use the BTSC audio standard */
  159. cx18_av_write(cx, 0x808, 0xf6);
  160. cx18_av_write(cx, 0x80b, 0x01);
  161. }
  162. } else if (std & V4L2_STD_PAL) {
  163. /* Follow tuner change procedure for PAL */
  164. cx18_av_write(cx, 0x808, 0xff);
  165. cx18_av_write(cx, 0x80b, 0x03);
  166. } else if (std & V4L2_STD_SECAM) {
  167. /* Select autodetect for SECAM */
  168. cx18_av_write(cx, 0x808, 0xff);
  169. cx18_av_write(cx, 0x80b, 0x03);
  170. }
  171. if (cx18_av_read(cx, 0x803) & 0x10) {
  172. /* restart audio decoder microcontroller */
  173. cx18_av_and_or(cx, 0x803, ~0x10, 0x00);
  174. cx18_av_and_or(cx, 0x803, ~0x10, 0x10);
  175. }
  176. }
  177. static int set_input(struct cx18 *cx, enum cx18_av_video_input vid_input,
  178. enum cx18_av_audio_input aud_input)
  179. {
  180. struct cx18_av_state *state = &cx->av_state;
  181. u8 is_composite = (vid_input >= CX18_AV_COMPOSITE1 &&
  182. vid_input <= CX18_AV_COMPOSITE8);
  183. u8 reg;
  184. CX18_DEBUG_INFO("decoder set video input %d, audio input %d\n",
  185. vid_input, aud_input);
  186. if (is_composite) {
  187. reg = 0xf0 + (vid_input - CX18_AV_COMPOSITE1);
  188. } else {
  189. int luma = vid_input & 0xf0;
  190. int chroma = vid_input & 0xf00;
  191. if ((vid_input & ~0xff0) ||
  192. luma < CX18_AV_SVIDEO_LUMA1 ||
  193. luma > CX18_AV_SVIDEO_LUMA8 ||
  194. chroma < CX18_AV_SVIDEO_CHROMA4 ||
  195. chroma > CX18_AV_SVIDEO_CHROMA8) {
  196. CX18_ERR("0x%04x is not a valid video input!\n",
  197. vid_input);
  198. return -EINVAL;
  199. }
  200. reg = 0xf0 + ((luma - CX18_AV_SVIDEO_LUMA1) >> 4);
  201. if (chroma >= CX18_AV_SVIDEO_CHROMA7) {
  202. reg &= 0x3f;
  203. reg |= (chroma - CX18_AV_SVIDEO_CHROMA7) >> 2;
  204. } else {
  205. reg &= 0xcf;
  206. reg |= (chroma - CX18_AV_SVIDEO_CHROMA4) >> 4;
  207. }
  208. }
  209. switch (aud_input) {
  210. case CX18_AV_AUDIO_SERIAL:
  211. /* do nothing, use serial audio input */
  212. break;
  213. case CX18_AV_AUDIO4: reg &= ~0x30; break;
  214. case CX18_AV_AUDIO5: reg &= ~0x30; reg |= 0x10; break;
  215. case CX18_AV_AUDIO6: reg &= ~0x30; reg |= 0x20; break;
  216. case CX18_AV_AUDIO7: reg &= ~0xc0; break;
  217. case CX18_AV_AUDIO8: reg &= ~0xc0; reg |= 0x40; break;
  218. default:
  219. CX18_ERR("0x%04x is not a valid audio input!\n", aud_input);
  220. return -EINVAL;
  221. }
  222. cx18_av_write(cx, 0x103, reg);
  223. /* Set INPUT_MODE to Composite (0) or S-Video (1) */
  224. cx18_av_and_or(cx, 0x401, ~0x6, is_composite ? 0 : 0x02);
  225. /* Set CH_SEL_ADC2 to 1 if input comes from CH3 */
  226. cx18_av_and_or(cx, 0x102, ~0x2, (reg & 0x80) == 0 ? 2 : 0);
  227. /* Set DUAL_MODE_ADC2 to 1 if input comes from both CH2 and CH3 */
  228. if ((reg & 0xc0) != 0xc0 && (reg & 0x30) != 0x30)
  229. cx18_av_and_or(cx, 0x102, ~0x4, 4);
  230. else
  231. cx18_av_and_or(cx, 0x102, ~0x4, 0);
  232. /*cx18_av_and_or4(cx, 0x104, ~0x001b4180, 0x00004180);*/
  233. state->vid_input = vid_input;
  234. state->aud_input = aud_input;
  235. cx18_av_audio_set_path(cx);
  236. input_change(cx);
  237. return 0;
  238. }
  239. /* ----------------------------------------------------------------------- */
  240. static int set_v4lstd(struct cx18 *cx)
  241. {
  242. struct cx18_av_state *state = &cx->av_state;
  243. u8 fmt = 0; /* zero is autodetect */
  244. u8 pal_m = 0;
  245. /* First tests should be against specific std */
  246. if (state->std == V4L2_STD_NTSC_M_JP) {
  247. fmt = 0x2;
  248. } else if (state->std == V4L2_STD_NTSC_443) {
  249. fmt = 0x3;
  250. } else if (state->std == V4L2_STD_PAL_M) {
  251. pal_m = 1;
  252. fmt = 0x5;
  253. } else if (state->std == V4L2_STD_PAL_N) {
  254. fmt = 0x6;
  255. } else if (state->std == V4L2_STD_PAL_Nc) {
  256. fmt = 0x7;
  257. } else if (state->std == V4L2_STD_PAL_60) {
  258. fmt = 0x8;
  259. } else {
  260. /* Then, test against generic ones */
  261. if (state->std & V4L2_STD_NTSC)
  262. fmt = 0x1;
  263. else if (state->std & V4L2_STD_PAL)
  264. fmt = 0x4;
  265. else if (state->std & V4L2_STD_SECAM)
  266. fmt = 0xc;
  267. }
  268. CX18_DEBUG_INFO("changing video std to fmt %i\n", fmt);
  269. /* Follow step 9 of section 3.16 in the cx18_av datasheet.
  270. Without this PAL may display a vertical ghosting effect.
  271. This happens for example with the Yuan MPC622. */
  272. if (fmt >= 4 && fmt < 8) {
  273. /* Set format to NTSC-M */
  274. cx18_av_and_or(cx, 0x400, ~0xf, 1);
  275. /* Turn off LCOMB */
  276. cx18_av_and_or(cx, 0x47b, ~6, 0);
  277. }
  278. cx18_av_and_or(cx, 0x400, ~0xf, fmt);
  279. cx18_av_and_or(cx, 0x403, ~0x3, pal_m);
  280. cx18_av_vbi_setup(cx);
  281. input_change(cx);
  282. return 0;
  283. }
  284. /* ----------------------------------------------------------------------- */
  285. static int set_v4lctrl(struct cx18 *cx, struct v4l2_control *ctrl)
  286. {
  287. switch (ctrl->id) {
  288. case V4L2_CID_BRIGHTNESS:
  289. if (ctrl->value < 0 || ctrl->value > 255) {
  290. CX18_ERR("invalid brightness setting %d\n",
  291. ctrl->value);
  292. return -ERANGE;
  293. }
  294. cx18_av_write(cx, 0x414, ctrl->value - 128);
  295. break;
  296. case V4L2_CID_CONTRAST:
  297. if (ctrl->value < 0 || ctrl->value > 127) {
  298. CX18_ERR("invalid contrast setting %d\n",
  299. ctrl->value);
  300. return -ERANGE;
  301. }
  302. cx18_av_write(cx, 0x415, ctrl->value << 1);
  303. break;
  304. case V4L2_CID_SATURATION:
  305. if (ctrl->value < 0 || ctrl->value > 127) {
  306. CX18_ERR("invalid saturation setting %d\n",
  307. ctrl->value);
  308. return -ERANGE;
  309. }
  310. cx18_av_write(cx, 0x420, ctrl->value << 1);
  311. cx18_av_write(cx, 0x421, ctrl->value << 1);
  312. break;
  313. case V4L2_CID_HUE:
  314. if (ctrl->value < -127 || ctrl->value > 127) {
  315. CX18_ERR("invalid hue setting %d\n", ctrl->value);
  316. return -ERANGE;
  317. }
  318. cx18_av_write(cx, 0x422, ctrl->value);
  319. break;
  320. case V4L2_CID_AUDIO_VOLUME:
  321. case V4L2_CID_AUDIO_BASS:
  322. case V4L2_CID_AUDIO_TREBLE:
  323. case V4L2_CID_AUDIO_BALANCE:
  324. case V4L2_CID_AUDIO_MUTE:
  325. return cx18_av_audio(cx, VIDIOC_S_CTRL, ctrl);
  326. default:
  327. return -EINVAL;
  328. }
  329. return 0;
  330. }
  331. static int get_v4lctrl(struct cx18 *cx, struct v4l2_control *ctrl)
  332. {
  333. switch (ctrl->id) {
  334. case V4L2_CID_BRIGHTNESS:
  335. ctrl->value = (s8)cx18_av_read(cx, 0x414) + 128;
  336. break;
  337. case V4L2_CID_CONTRAST:
  338. ctrl->value = cx18_av_read(cx, 0x415) >> 1;
  339. break;
  340. case V4L2_CID_SATURATION:
  341. ctrl->value = cx18_av_read(cx, 0x420) >> 1;
  342. break;
  343. case V4L2_CID_HUE:
  344. ctrl->value = (s8)cx18_av_read(cx, 0x422);
  345. break;
  346. case V4L2_CID_AUDIO_VOLUME:
  347. case V4L2_CID_AUDIO_BASS:
  348. case V4L2_CID_AUDIO_TREBLE:
  349. case V4L2_CID_AUDIO_BALANCE:
  350. case V4L2_CID_AUDIO_MUTE:
  351. return cx18_av_audio(cx, VIDIOC_G_CTRL, ctrl);
  352. default:
  353. return -EINVAL;
  354. }
  355. return 0;
  356. }
  357. /* ----------------------------------------------------------------------- */
  358. static int get_v4lfmt(struct cx18 *cx, struct v4l2_format *fmt)
  359. {
  360. switch (fmt->type) {
  361. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  362. return cx18_av_vbi(cx, VIDIOC_G_FMT, fmt);
  363. default:
  364. return -EINVAL;
  365. }
  366. return 0;
  367. }
  368. static int set_v4lfmt(struct cx18 *cx, struct v4l2_format *fmt)
  369. {
  370. struct cx18_av_state *state = &cx->av_state;
  371. struct v4l2_pix_format *pix;
  372. int HSC, VSC, Vsrc, Hsrc, filter, Vlines;
  373. int is_50Hz = !(state->std & V4L2_STD_525_60);
  374. switch (fmt->type) {
  375. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  376. pix = &(fmt->fmt.pix);
  377. Vsrc = (cx18_av_read(cx, 0x476) & 0x3f) << 4;
  378. Vsrc |= (cx18_av_read(cx, 0x475) & 0xf0) >> 4;
  379. Hsrc = (cx18_av_read(cx, 0x472) & 0x3f) << 4;
  380. Hsrc |= (cx18_av_read(cx, 0x471) & 0xf0) >> 4;
  381. Vlines = pix->height + (is_50Hz ? 4 : 7);
  382. if ((pix->width * 16 < Hsrc) || (Hsrc < pix->width) ||
  383. (Vlines * 8 < Vsrc) || (Vsrc < Vlines)) {
  384. CX18_ERR("%dx%d is not a valid size!\n",
  385. pix->width, pix->height);
  386. return -ERANGE;
  387. }
  388. HSC = (Hsrc * (1 << 20)) / pix->width - (1 << 20);
  389. VSC = (1 << 16) - (Vsrc * (1 << 9) / Vlines - (1 << 9));
  390. VSC &= 0x1fff;
  391. if (pix->width >= 385)
  392. filter = 0;
  393. else if (pix->width > 192)
  394. filter = 1;
  395. else if (pix->width > 96)
  396. filter = 2;
  397. else
  398. filter = 3;
  399. CX18_DEBUG_INFO("decoder set size %dx%d -> scale %ux%u\n",
  400. pix->width, pix->height, HSC, VSC);
  401. /* HSCALE=HSC */
  402. cx18_av_write(cx, 0x418, HSC & 0xff);
  403. cx18_av_write(cx, 0x419, (HSC >> 8) & 0xff);
  404. cx18_av_write(cx, 0x41a, HSC >> 16);
  405. /* VSCALE=VSC */
  406. cx18_av_write(cx, 0x41c, VSC & 0xff);
  407. cx18_av_write(cx, 0x41d, VSC >> 8);
  408. /* VS_INTRLACE=1 VFILT=filter */
  409. cx18_av_write(cx, 0x41e, 0x8 | filter);
  410. break;
  411. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  412. return cx18_av_vbi(cx, VIDIOC_S_FMT, fmt);
  413. case V4L2_BUF_TYPE_VBI_CAPTURE:
  414. return cx18_av_vbi(cx, VIDIOC_S_FMT, fmt);
  415. default:
  416. return -EINVAL;
  417. }
  418. return 0;
  419. }
  420. /* ----------------------------------------------------------------------- */
  421. int cx18_av_cmd(struct cx18 *cx, unsigned int cmd, void *arg)
  422. {
  423. struct cx18_av_state *state = &cx->av_state;
  424. struct v4l2_tuner *vt = arg;
  425. struct v4l2_routing *route = arg;
  426. /* ignore these commands */
  427. switch (cmd) {
  428. case TUNER_SET_TYPE_ADDR:
  429. return 0;
  430. }
  431. if (!state->is_initialized) {
  432. CX18_DEBUG_INFO("cmd %08x triggered fw load\n", cmd);
  433. /* initialize on first use */
  434. state->is_initialized = 1;
  435. cx18_av_initialize(cx);
  436. }
  437. switch (cmd) {
  438. case VIDIOC_INT_DECODE_VBI_LINE:
  439. return cx18_av_vbi(cx, cmd, arg);
  440. case VIDIOC_INT_AUDIO_CLOCK_FREQ:
  441. return cx18_av_audio(cx, cmd, arg);
  442. case VIDIOC_STREAMON:
  443. CX18_DEBUG_INFO("enable output\n");
  444. cx18_av_write(cx, 0x115, 0x8c);
  445. cx18_av_write(cx, 0x116, 0x07);
  446. break;
  447. case VIDIOC_STREAMOFF:
  448. CX18_DEBUG_INFO("disable output\n");
  449. cx18_av_write(cx, 0x115, 0x00);
  450. cx18_av_write(cx, 0x116, 0x00);
  451. break;
  452. case VIDIOC_LOG_STATUS:
  453. log_video_status(cx);
  454. log_audio_status(cx);
  455. break;
  456. case VIDIOC_G_CTRL:
  457. return get_v4lctrl(cx, (struct v4l2_control *)arg);
  458. case VIDIOC_S_CTRL:
  459. return set_v4lctrl(cx, (struct v4l2_control *)arg);
  460. case VIDIOC_QUERYCTRL:
  461. {
  462. struct v4l2_queryctrl *qc = arg;
  463. switch (qc->id) {
  464. case V4L2_CID_BRIGHTNESS:
  465. case V4L2_CID_CONTRAST:
  466. case V4L2_CID_SATURATION:
  467. case V4L2_CID_HUE:
  468. return v4l2_ctrl_query_fill_std(qc);
  469. default:
  470. break;
  471. }
  472. switch (qc->id) {
  473. case V4L2_CID_AUDIO_VOLUME:
  474. case V4L2_CID_AUDIO_MUTE:
  475. case V4L2_CID_AUDIO_BALANCE:
  476. case V4L2_CID_AUDIO_BASS:
  477. case V4L2_CID_AUDIO_TREBLE:
  478. return v4l2_ctrl_query_fill_std(qc);
  479. default:
  480. return -EINVAL;
  481. }
  482. return -EINVAL;
  483. }
  484. case VIDIOC_G_STD:
  485. *(v4l2_std_id *)arg = state->std;
  486. break;
  487. case VIDIOC_S_STD:
  488. if (state->radio == 0 && state->std == *(v4l2_std_id *)arg)
  489. return 0;
  490. state->radio = 0;
  491. state->std = *(v4l2_std_id *)arg;
  492. return set_v4lstd(cx);
  493. case AUDC_SET_RADIO:
  494. state->radio = 1;
  495. break;
  496. case VIDIOC_INT_G_VIDEO_ROUTING:
  497. route->input = state->vid_input;
  498. route->output = 0;
  499. break;
  500. case VIDIOC_INT_S_VIDEO_ROUTING:
  501. return set_input(cx, route->input, state->aud_input);
  502. case VIDIOC_INT_G_AUDIO_ROUTING:
  503. route->input = state->aud_input;
  504. route->output = 0;
  505. break;
  506. case VIDIOC_INT_S_AUDIO_ROUTING:
  507. return set_input(cx, state->vid_input, route->input);
  508. case VIDIOC_S_FREQUENCY:
  509. input_change(cx);
  510. break;
  511. case VIDIOC_G_TUNER:
  512. {
  513. u8 vpres = cx18_av_read(cx, 0x40e) & 0x20;
  514. u8 mode;
  515. int val = 0;
  516. if (state->radio)
  517. break;
  518. vt->signal = vpres ? 0xffff : 0x0;
  519. vt->capability |=
  520. V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_LANG1 |
  521. V4L2_TUNER_CAP_LANG2 | V4L2_TUNER_CAP_SAP;
  522. mode = cx18_av_read(cx, 0x804);
  523. /* get rxsubchans and audmode */
  524. if ((mode & 0xf) == 1)
  525. val |= V4L2_TUNER_SUB_STEREO;
  526. else
  527. val |= V4L2_TUNER_SUB_MONO;
  528. if (mode == 2 || mode == 4)
  529. val = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
  530. if (mode & 0x10)
  531. val |= V4L2_TUNER_SUB_SAP;
  532. vt->rxsubchans = val;
  533. vt->audmode = state->audmode;
  534. break;
  535. }
  536. case VIDIOC_S_TUNER:
  537. if (state->radio)
  538. break;
  539. switch (vt->audmode) {
  540. case V4L2_TUNER_MODE_MONO:
  541. /* mono -> mono
  542. stereo -> mono
  543. bilingual -> lang1 */
  544. cx18_av_and_or(cx, 0x809, ~0xf, 0x00);
  545. break;
  546. case V4L2_TUNER_MODE_STEREO:
  547. case V4L2_TUNER_MODE_LANG1:
  548. /* mono -> mono
  549. stereo -> stereo
  550. bilingual -> lang1 */
  551. cx18_av_and_or(cx, 0x809, ~0xf, 0x04);
  552. break;
  553. case V4L2_TUNER_MODE_LANG1_LANG2:
  554. /* mono -> mono
  555. stereo -> stereo
  556. bilingual -> lang1/lang2 */
  557. cx18_av_and_or(cx, 0x809, ~0xf, 0x07);
  558. break;
  559. case V4L2_TUNER_MODE_LANG2:
  560. /* mono -> mono
  561. stereo -> stereo
  562. bilingual -> lang2 */
  563. cx18_av_and_or(cx, 0x809, ~0xf, 0x01);
  564. break;
  565. default:
  566. return -EINVAL;
  567. }
  568. state->audmode = vt->audmode;
  569. break;
  570. case VIDIOC_G_FMT:
  571. return get_v4lfmt(cx, (struct v4l2_format *)arg);
  572. case VIDIOC_S_FMT:
  573. return set_v4lfmt(cx, (struct v4l2_format *)arg);
  574. case VIDIOC_INT_RESET:
  575. cx18_av_initialize(cx);
  576. break;
  577. default:
  578. return -EINVAL;
  579. }
  580. return 0;
  581. }
  582. /* ----------------------------------------------------------------------- */
  583. /* ----------------------------------------------------------------------- */
  584. static void log_video_status(struct cx18 *cx)
  585. {
  586. static const char *const fmt_strs[] = {
  587. "0x0",
  588. "NTSC-M", "NTSC-J", "NTSC-4.43",
  589. "PAL-BDGHI", "PAL-M", "PAL-N", "PAL-Nc", "PAL-60",
  590. "0x9", "0xA", "0xB",
  591. "SECAM",
  592. "0xD", "0xE", "0xF"
  593. };
  594. struct cx18_av_state *state = &cx->av_state;
  595. u8 vidfmt_sel = cx18_av_read(cx, 0x400) & 0xf;
  596. u8 gen_stat1 = cx18_av_read(cx, 0x40d);
  597. u8 gen_stat2 = cx18_av_read(cx, 0x40e);
  598. int vid_input = state->vid_input;
  599. CX18_INFO("Video signal: %spresent\n",
  600. (gen_stat2 & 0x20) ? "" : "not ");
  601. CX18_INFO("Detected format: %s\n",
  602. fmt_strs[gen_stat1 & 0xf]);
  603. CX18_INFO("Specified standard: %s\n",
  604. vidfmt_sel ? fmt_strs[vidfmt_sel] : "automatic detection");
  605. if (vid_input >= CX18_AV_COMPOSITE1 &&
  606. vid_input <= CX18_AV_COMPOSITE8) {
  607. CX18_INFO("Specified video input: Composite %d\n",
  608. vid_input - CX18_AV_COMPOSITE1 + 1);
  609. } else {
  610. CX18_INFO("Specified video input: S-Video (Luma In%d, Chroma In%d)\n",
  611. (vid_input & 0xf0) >> 4, (vid_input & 0xf00) >> 8);
  612. }
  613. CX18_INFO("Specified audioclock freq: %d Hz\n", state->audclk_freq);
  614. }
  615. /* ----------------------------------------------------------------------- */
  616. static void log_audio_status(struct cx18 *cx)
  617. {
  618. struct cx18_av_state *state = &cx->av_state;
  619. u8 download_ctl = cx18_av_read(cx, 0x803);
  620. u8 mod_det_stat0 = cx18_av_read(cx, 0x804);
  621. u8 mod_det_stat1 = cx18_av_read(cx, 0x805);
  622. u8 audio_config = cx18_av_read(cx, 0x808);
  623. u8 pref_mode = cx18_av_read(cx, 0x809);
  624. u8 afc0 = cx18_av_read(cx, 0x80b);
  625. u8 mute_ctl = cx18_av_read(cx, 0x8d3);
  626. int aud_input = state->aud_input;
  627. char *p;
  628. switch (mod_det_stat0) {
  629. case 0x00: p = "mono"; break;
  630. case 0x01: p = "stereo"; break;
  631. case 0x02: p = "dual"; break;
  632. case 0x04: p = "tri"; break;
  633. case 0x10: p = "mono with SAP"; break;
  634. case 0x11: p = "stereo with SAP"; break;
  635. case 0x12: p = "dual with SAP"; break;
  636. case 0x14: p = "tri with SAP"; break;
  637. case 0xfe: p = "forced mode"; break;
  638. default: p = "not defined"; break;
  639. }
  640. CX18_INFO("Detected audio mode: %s\n", p);
  641. switch (mod_det_stat1) {
  642. case 0x00: p = "not defined"; break;
  643. case 0x01: p = "EIAJ"; break;
  644. case 0x02: p = "A2-M"; break;
  645. case 0x03: p = "A2-BG"; break;
  646. case 0x04: p = "A2-DK1"; break;
  647. case 0x05: p = "A2-DK2"; break;
  648. case 0x06: p = "A2-DK3"; break;
  649. case 0x07: p = "A1 (6.0 MHz FM Mono)"; break;
  650. case 0x08: p = "AM-L"; break;
  651. case 0x09: p = "NICAM-BG"; break;
  652. case 0x0a: p = "NICAM-DK"; break;
  653. case 0x0b: p = "NICAM-I"; break;
  654. case 0x0c: p = "NICAM-L"; break;
  655. case 0x0d: p = "BTSC/EIAJ/A2-M Mono (4.5 MHz FMMono)"; break;
  656. case 0x0e: p = "IF FM Radio"; break;
  657. case 0x0f: p = "BTSC"; break;
  658. case 0x10: p = "detected chrominance"; break;
  659. case 0xfd: p = "unknown audio standard"; break;
  660. case 0xfe: p = "forced audio standard"; break;
  661. case 0xff: p = "no detected audio standard"; break;
  662. default: p = "not defined"; break;
  663. }
  664. CX18_INFO("Detected audio standard: %s\n", p);
  665. CX18_INFO("Audio muted: %s\n",
  666. (mute_ctl & 0x2) ? "yes" : "no");
  667. CX18_INFO("Audio microcontroller: %s\n",
  668. (download_ctl & 0x10) ? "running" : "stopped");
  669. switch (audio_config >> 4) {
  670. case 0x00: p = "undefined"; break;
  671. case 0x01: p = "BTSC"; break;
  672. case 0x02: p = "EIAJ"; break;
  673. case 0x03: p = "A2-M"; break;
  674. case 0x04: p = "A2-BG"; break;
  675. case 0x05: p = "A2-DK1"; break;
  676. case 0x06: p = "A2-DK2"; break;
  677. case 0x07: p = "A2-DK3"; break;
  678. case 0x08: p = "A1 (6.0 MHz FM Mono)"; break;
  679. case 0x09: p = "AM-L"; break;
  680. case 0x0a: p = "NICAM-BG"; break;
  681. case 0x0b: p = "NICAM-DK"; break;
  682. case 0x0c: p = "NICAM-I"; break;
  683. case 0x0d: p = "NICAM-L"; break;
  684. case 0x0e: p = "FM radio"; break;
  685. case 0x0f: p = "automatic detection"; break;
  686. default: p = "undefined"; break;
  687. }
  688. CX18_INFO("Configured audio standard: %s\n", p);
  689. if ((audio_config >> 4) < 0xF) {
  690. switch (audio_config & 0xF) {
  691. case 0x00: p = "MONO1 (LANGUAGE A/Mono L+R channel for BTSC, EIAJ, A2)"; break;
  692. case 0x01: p = "MONO2 (LANGUAGE B)"; break;
  693. case 0x02: p = "MONO3 (STEREO forced MONO)"; break;
  694. case 0x03: p = "MONO4 (NICAM ANALOG-Language C/Analog Fallback)"; break;
  695. case 0x04: p = "STEREO"; break;
  696. case 0x05: p = "DUAL1 (AC)"; break;
  697. case 0x06: p = "DUAL2 (BC)"; break;
  698. case 0x07: p = "DUAL3 (AB)"; break;
  699. default: p = "undefined";
  700. }
  701. CX18_INFO("Configured audio mode: %s\n", p);
  702. } else {
  703. switch (audio_config & 0xF) {
  704. case 0x00: p = "BG"; break;
  705. case 0x01: p = "DK1"; break;
  706. case 0x02: p = "DK2"; break;
  707. case 0x03: p = "DK3"; break;
  708. case 0x04: p = "I"; break;
  709. case 0x05: p = "L"; break;
  710. case 0x06: p = "BTSC"; break;
  711. case 0x07: p = "EIAJ"; break;
  712. case 0x08: p = "A2-M"; break;
  713. case 0x09: p = "FM Radio (4.5 MHz)"; break;
  714. case 0x0a: p = "FM Radio (5.5 MHz)"; break;
  715. case 0x0b: p = "S-Video"; break;
  716. case 0x0f: p = "automatic standard and mode detection"; break;
  717. default: p = "undefined"; break;
  718. }
  719. CX18_INFO("Configured audio system: %s\n", p);
  720. }
  721. if (aud_input)
  722. CX18_INFO("Specified audio input: Tuner (In%d)\n",
  723. aud_input);
  724. else
  725. CX18_INFO("Specified audio input: External\n");
  726. switch (pref_mode & 0xf) {
  727. case 0: p = "mono/language A"; break;
  728. case 1: p = "language B"; break;
  729. case 2: p = "language C"; break;
  730. case 3: p = "analog fallback"; break;
  731. case 4: p = "stereo"; break;
  732. case 5: p = "language AC"; break;
  733. case 6: p = "language BC"; break;
  734. case 7: p = "language AB"; break;
  735. default: p = "undefined"; break;
  736. }
  737. CX18_INFO("Preferred audio mode: %s\n", p);
  738. if ((audio_config & 0xf) == 0xf) {
  739. switch ((afc0 >> 3) & 0x1) {
  740. case 0: p = "system DK"; break;
  741. case 1: p = "system L"; break;
  742. }
  743. CX18_INFO("Selected 65 MHz format: %s\n", p);
  744. switch (afc0 & 0x7) {
  745. case 0: p = "Chroma"; break;
  746. case 1: p = "BTSC"; break;
  747. case 2: p = "EIAJ"; break;
  748. case 3: p = "A2-M"; break;
  749. case 4: p = "autodetect"; break;
  750. default: p = "undefined"; break;
  751. }
  752. CX18_INFO("Selected 45 MHz format: %s\n", p);
  753. }
  754. }