cx18-av-core.c 24 KB

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