av7110_v4l.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  1. /*
  2. * av7110_v4l.c: av7110 video4linux interface for DVB and Siemens DVB-C analog module
  3. *
  4. * Copyright (C) 1999-2002 Ralph Metzler
  5. * & Marcus Metzler for convergence integrated media GmbH
  6. *
  7. * originally based on code by:
  8. * Copyright (C) 1998,1999 Christian Theiss <mistert@rz.fh-augsburg.de>
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version 2
  13. * of the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. * To obtain the license, point your browser to
  20. * http://www.gnu.org/copyleft/gpl.html
  21. *
  22. * the project's page is at https://linuxtv.org
  23. */
  24. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  25. #include <linux/kernel.h>
  26. #include <linux/types.h>
  27. #include <linux/delay.h>
  28. #include <linux/fs.h>
  29. #include <linux/timer.h>
  30. #include <linux/poll.h>
  31. #include "av7110.h"
  32. #include "av7110_hw.h"
  33. #include "av7110_av.h"
  34. int msp_writereg(struct av7110 *av7110, u8 dev, u16 reg, u16 val)
  35. {
  36. u8 msg[5] = { dev, reg >> 8, reg & 0xff, val >> 8 , val & 0xff };
  37. struct i2c_msg msgs = { .flags = 0, .len = 5, .buf = msg };
  38. switch (av7110->adac_type) {
  39. case DVB_ADAC_MSP34x0:
  40. msgs.addr = 0x40;
  41. break;
  42. case DVB_ADAC_MSP34x5:
  43. msgs.addr = 0x42;
  44. break;
  45. default:
  46. return 0;
  47. }
  48. if (i2c_transfer(&av7110->i2c_adap, &msgs, 1) != 1) {
  49. dprintk(1, "dvb-ttpci: failed @ card %d, %u = %u\n",
  50. av7110->dvb_adapter.num, reg, val);
  51. return -EIO;
  52. }
  53. return 0;
  54. }
  55. static int msp_readreg(struct av7110 *av7110, u8 dev, u16 reg, u16 *val)
  56. {
  57. u8 msg1[3] = { dev, reg >> 8, reg & 0xff };
  58. u8 msg2[2];
  59. struct i2c_msg msgs[2] = {
  60. { .flags = 0 , .len = 3, .buf = msg1 },
  61. { .flags = I2C_M_RD, .len = 2, .buf = msg2 }
  62. };
  63. switch (av7110->adac_type) {
  64. case DVB_ADAC_MSP34x0:
  65. msgs[0].addr = 0x40;
  66. msgs[1].addr = 0x40;
  67. break;
  68. case DVB_ADAC_MSP34x5:
  69. msgs[0].addr = 0x42;
  70. msgs[1].addr = 0x42;
  71. break;
  72. default:
  73. return 0;
  74. }
  75. if (i2c_transfer(&av7110->i2c_adap, &msgs[0], 2) != 2) {
  76. dprintk(1, "dvb-ttpci: failed @ card %d, %u\n",
  77. av7110->dvb_adapter.num, reg);
  78. return -EIO;
  79. }
  80. *val = (msg2[0] << 8) | msg2[1];
  81. return 0;
  82. }
  83. static struct v4l2_input inputs[4] = {
  84. {
  85. .index = 0,
  86. .name = "DVB",
  87. .type = V4L2_INPUT_TYPE_CAMERA,
  88. .audioset = 1,
  89. .tuner = 0, /* ignored */
  90. .std = V4L2_STD_PAL_BG|V4L2_STD_NTSC_M,
  91. .status = 0,
  92. .capabilities = V4L2_IN_CAP_STD,
  93. }, {
  94. .index = 1,
  95. .name = "Television",
  96. .type = V4L2_INPUT_TYPE_TUNER,
  97. .audioset = 1,
  98. .tuner = 0,
  99. .std = V4L2_STD_PAL_BG|V4L2_STD_NTSC_M,
  100. .status = 0,
  101. .capabilities = V4L2_IN_CAP_STD,
  102. }, {
  103. .index = 2,
  104. .name = "Video",
  105. .type = V4L2_INPUT_TYPE_CAMERA,
  106. .audioset = 0,
  107. .tuner = 0,
  108. .std = V4L2_STD_PAL_BG|V4L2_STD_NTSC_M,
  109. .status = 0,
  110. .capabilities = V4L2_IN_CAP_STD,
  111. }, {
  112. .index = 3,
  113. .name = "Y/C",
  114. .type = V4L2_INPUT_TYPE_CAMERA,
  115. .audioset = 0,
  116. .tuner = 0,
  117. .std = V4L2_STD_PAL_BG|V4L2_STD_NTSC_M,
  118. .status = 0,
  119. .capabilities = V4L2_IN_CAP_STD,
  120. }
  121. };
  122. static int ves1820_writereg(struct saa7146_dev *dev, u8 addr, u8 reg, u8 data)
  123. {
  124. struct av7110 *av7110 = dev->ext_priv;
  125. u8 buf[] = { 0x00, reg, data };
  126. struct i2c_msg msg = { .addr = addr, .flags = 0, .buf = buf, .len = 3 };
  127. dprintk(4, "dev: %p\n", dev);
  128. if (1 != i2c_transfer(&av7110->i2c_adap, &msg, 1))
  129. return -1;
  130. return 0;
  131. }
  132. static int tuner_write(struct saa7146_dev *dev, u8 addr, u8 data [4])
  133. {
  134. struct av7110 *av7110 = dev->ext_priv;
  135. struct i2c_msg msg = { .addr = addr, .flags = 0, .buf = data, .len = 4 };
  136. dprintk(4, "dev: %p\n", dev);
  137. if (1 != i2c_transfer(&av7110->i2c_adap, &msg, 1))
  138. return -1;
  139. return 0;
  140. }
  141. static int ves1820_set_tv_freq(struct saa7146_dev *dev, u32 freq)
  142. {
  143. u32 div;
  144. u8 config;
  145. u8 buf[4];
  146. dprintk(4, "freq: 0x%08x\n", freq);
  147. /* magic number: 614. tuning with the frequency given by v4l2
  148. is always off by 614*62.5 = 38375 kHz...*/
  149. div = freq + 614;
  150. buf[0] = (div >> 8) & 0x7f;
  151. buf[1] = div & 0xff;
  152. buf[2] = 0x8e;
  153. if (freq < (u32) (16 * 168.25))
  154. config = 0xa0;
  155. else if (freq < (u32) (16 * 447.25))
  156. config = 0x90;
  157. else
  158. config = 0x30;
  159. config &= ~0x02;
  160. buf[3] = config;
  161. return tuner_write(dev, 0x61, buf);
  162. }
  163. static int stv0297_set_tv_freq(struct saa7146_dev *dev, u32 freq)
  164. {
  165. struct av7110 *av7110 = (struct av7110*)dev->ext_priv;
  166. u32 div;
  167. u8 data[4];
  168. div = (freq + 38900000 + 31250) / 62500;
  169. data[0] = (div >> 8) & 0x7f;
  170. data[1] = div & 0xff;
  171. data[2] = 0xce;
  172. if (freq < 45000000)
  173. return -EINVAL;
  174. else if (freq < 137000000)
  175. data[3] = 0x01;
  176. else if (freq < 403000000)
  177. data[3] = 0x02;
  178. else if (freq < 860000000)
  179. data[3] = 0x04;
  180. else
  181. return -EINVAL;
  182. if (av7110->fe->ops.i2c_gate_ctrl)
  183. av7110->fe->ops.i2c_gate_ctrl(av7110->fe, 1);
  184. return tuner_write(dev, 0x63, data);
  185. }
  186. static struct saa7146_standard analog_standard[];
  187. static struct saa7146_standard dvb_standard[];
  188. static struct saa7146_standard standard[];
  189. static const struct v4l2_audio msp3400_v4l2_audio = {
  190. .index = 0,
  191. .name = "Television",
  192. .capability = V4L2_AUDCAP_STEREO
  193. };
  194. static int av7110_dvb_c_switch(struct saa7146_fh *fh)
  195. {
  196. struct saa7146_dev *dev = fh->dev;
  197. struct saa7146_vv *vv = dev->vv_data;
  198. struct av7110 *av7110 = (struct av7110*)dev->ext_priv;
  199. u16 adswitch;
  200. int source, sync, err;
  201. dprintk(4, "%p\n", av7110);
  202. if ((vv->video_status & STATUS_OVERLAY) != 0) {
  203. vv->ov_suspend = vv->video_fh;
  204. err = saa7146_stop_preview(vv->video_fh); /* side effect: video_status is now 0, video_fh is NULL */
  205. if (err != 0) {
  206. dprintk(2, "suspending video failed\n");
  207. vv->ov_suspend = NULL;
  208. }
  209. }
  210. if (0 != av7110->current_input) {
  211. dprintk(1, "switching to analog TV:\n");
  212. adswitch = 1;
  213. source = SAA7146_HPS_SOURCE_PORT_B;
  214. sync = SAA7146_HPS_SYNC_PORT_B;
  215. memcpy(standard, analog_standard, sizeof(struct saa7146_standard) * 2);
  216. switch (av7110->current_input) {
  217. case 1:
  218. dprintk(1, "switching SAA7113 to Analog Tuner Input\n");
  219. msp_writereg(av7110, MSP_WR_DSP, 0x0008, 0x0000); // loudspeaker source
  220. msp_writereg(av7110, MSP_WR_DSP, 0x0009, 0x0000); // headphone source
  221. msp_writereg(av7110, MSP_WR_DSP, 0x000a, 0x0000); // SCART 1 source
  222. msp_writereg(av7110, MSP_WR_DSP, 0x000e, 0x3000); // FM matrix, mono
  223. msp_writereg(av7110, MSP_WR_DSP, 0x0000, 0x4f00); // loudspeaker + headphone
  224. msp_writereg(av7110, MSP_WR_DSP, 0x0007, 0x4f00); // SCART 1 volume
  225. if (av7110->analog_tuner_flags & ANALOG_TUNER_VES1820) {
  226. if (ves1820_writereg(dev, 0x09, 0x0f, 0x60))
  227. dprintk(1, "setting band in demodulator failed\n");
  228. } else if (av7110->analog_tuner_flags & ANALOG_TUNER_STV0297) {
  229. saa7146_setgpio(dev, 1, SAA7146_GPIO_OUTHI); // TDA9819 pin9(STD)
  230. saa7146_setgpio(dev, 3, SAA7146_GPIO_OUTHI); // TDA9819 pin30(VIF)
  231. }
  232. if (i2c_writereg(av7110, 0x48, 0x02, 0xd0) != 1)
  233. dprintk(1, "saa7113 write failed @ card %d", av7110->dvb_adapter.num);
  234. break;
  235. case 2:
  236. dprintk(1, "switching SAA7113 to Video AV CVBS Input\n");
  237. if (i2c_writereg(av7110, 0x48, 0x02, 0xd2) != 1)
  238. dprintk(1, "saa7113 write failed @ card %d", av7110->dvb_adapter.num);
  239. break;
  240. case 3:
  241. dprintk(1, "switching SAA7113 to Video AV Y/C Input\n");
  242. if (i2c_writereg(av7110, 0x48, 0x02, 0xd9) != 1)
  243. dprintk(1, "saa7113 write failed @ card %d", av7110->dvb_adapter.num);
  244. break;
  245. default:
  246. dprintk(1, "switching SAA7113 to Input: AV7110: SAA7113: invalid input\n");
  247. }
  248. } else {
  249. adswitch = 0;
  250. source = SAA7146_HPS_SOURCE_PORT_A;
  251. sync = SAA7146_HPS_SYNC_PORT_A;
  252. memcpy(standard, dvb_standard, sizeof(struct saa7146_standard) * 2);
  253. dprintk(1, "switching DVB mode\n");
  254. msp_writereg(av7110, MSP_WR_DSP, 0x0008, 0x0220); // loudspeaker source
  255. msp_writereg(av7110, MSP_WR_DSP, 0x0009, 0x0220); // headphone source
  256. msp_writereg(av7110, MSP_WR_DSP, 0x000a, 0x0220); // SCART 1 source
  257. msp_writereg(av7110, MSP_WR_DSP, 0x000e, 0x3000); // FM matrix, mono
  258. msp_writereg(av7110, MSP_WR_DSP, 0x0000, 0x7f00); // loudspeaker + headphone
  259. msp_writereg(av7110, MSP_WR_DSP, 0x0007, 0x7f00); // SCART 1 volume
  260. if (av7110->analog_tuner_flags & ANALOG_TUNER_VES1820) {
  261. if (ves1820_writereg(dev, 0x09, 0x0f, 0x20))
  262. dprintk(1, "setting band in demodulator failed\n");
  263. } else if (av7110->analog_tuner_flags & ANALOG_TUNER_STV0297) {
  264. saa7146_setgpio(dev, 1, SAA7146_GPIO_OUTLO); // TDA9819 pin9(STD)
  265. saa7146_setgpio(dev, 3, SAA7146_GPIO_OUTLO); // TDA9819 pin30(VIF)
  266. }
  267. }
  268. /* hmm, this does not do anything!? */
  269. if (av7110_fw_cmd(av7110, COMTYPE_AUDIODAC, ADSwitch, 1, adswitch))
  270. dprintk(1, "ADSwitch error\n");
  271. saa7146_set_hps_source_and_sync(dev, source, sync);
  272. if (vv->ov_suspend != NULL) {
  273. saa7146_start_preview(vv->ov_suspend);
  274. vv->ov_suspend = NULL;
  275. }
  276. return 0;
  277. }
  278. static int vidioc_g_tuner(struct file *file, void *fh, struct v4l2_tuner *t)
  279. {
  280. struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
  281. struct av7110 *av7110 = (struct av7110 *)dev->ext_priv;
  282. u16 stereo_det;
  283. s8 stereo;
  284. dprintk(2, "VIDIOC_G_TUNER: %d\n", t->index);
  285. if (!av7110->analog_tuner_flags || t->index != 0)
  286. return -EINVAL;
  287. memset(t, 0, sizeof(*t));
  288. strcpy((char *)t->name, "Television");
  289. t->type = V4L2_TUNER_ANALOG_TV;
  290. t->capability = V4L2_TUNER_CAP_NORM | V4L2_TUNER_CAP_STEREO |
  291. V4L2_TUNER_CAP_LANG1 | V4L2_TUNER_CAP_LANG2 | V4L2_TUNER_CAP_SAP;
  292. t->rangelow = 772; /* 48.25 MHZ / 62.5 kHz = 772, see fi1216mk2-specs, page 2 */
  293. t->rangehigh = 13684; /* 855.25 MHz / 62.5 kHz = 13684 */
  294. /* FIXME: add the real signal strength here */
  295. t->signal = 0xffff;
  296. t->afc = 0;
  297. /* FIXME: standard / stereo detection is still broken */
  298. msp_readreg(av7110, MSP_RD_DEM, 0x007e, &stereo_det);
  299. dprintk(1, "VIDIOC_G_TUNER: msp3400 TV standard detection: 0x%04x\n", stereo_det);
  300. msp_readreg(av7110, MSP_RD_DSP, 0x0018, &stereo_det);
  301. dprintk(1, "VIDIOC_G_TUNER: msp3400 stereo detection: 0x%04x\n", stereo_det);
  302. stereo = (s8)(stereo_det >> 8);
  303. if (stereo > 0x10) {
  304. /* stereo */
  305. t->rxsubchans = V4L2_TUNER_SUB_STEREO | V4L2_TUNER_SUB_MONO;
  306. t->audmode = V4L2_TUNER_MODE_STEREO;
  307. } else if (stereo < -0x10) {
  308. /* bilingual */
  309. t->rxsubchans = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
  310. t->audmode = V4L2_TUNER_MODE_LANG1;
  311. } else /* mono */
  312. t->rxsubchans = V4L2_TUNER_SUB_MONO;
  313. return 0;
  314. }
  315. static int vidioc_s_tuner(struct file *file, void *fh, const struct v4l2_tuner *t)
  316. {
  317. struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
  318. struct av7110 *av7110 = (struct av7110 *)dev->ext_priv;
  319. u16 fm_matrix, src;
  320. dprintk(2, "VIDIOC_S_TUNER: %d\n", t->index);
  321. if (!av7110->analog_tuner_flags || av7110->current_input != 1)
  322. return -EINVAL;
  323. switch (t->audmode) {
  324. case V4L2_TUNER_MODE_STEREO:
  325. dprintk(2, "VIDIOC_S_TUNER: V4L2_TUNER_MODE_STEREO\n");
  326. fm_matrix = 0x3001; /* stereo */
  327. src = 0x0020;
  328. break;
  329. case V4L2_TUNER_MODE_LANG1_LANG2:
  330. dprintk(2, "VIDIOC_S_TUNER: V4L2_TUNER_MODE_LANG1_LANG2\n");
  331. fm_matrix = 0x3000; /* bilingual */
  332. src = 0x0020;
  333. break;
  334. case V4L2_TUNER_MODE_LANG1:
  335. dprintk(2, "VIDIOC_S_TUNER: V4L2_TUNER_MODE_LANG1\n");
  336. fm_matrix = 0x3000; /* mono */
  337. src = 0x0000;
  338. break;
  339. case V4L2_TUNER_MODE_LANG2:
  340. dprintk(2, "VIDIOC_S_TUNER: V4L2_TUNER_MODE_LANG2\n");
  341. fm_matrix = 0x3000; /* mono */
  342. src = 0x0010;
  343. break;
  344. default: /* case V4L2_TUNER_MODE_MONO: */
  345. dprintk(2, "VIDIOC_S_TUNER: TDA9840_SET_MONO\n");
  346. fm_matrix = 0x3000; /* mono */
  347. src = 0x0030;
  348. break;
  349. }
  350. msp_writereg(av7110, MSP_WR_DSP, 0x000e, fm_matrix);
  351. msp_writereg(av7110, MSP_WR_DSP, 0x0008, src);
  352. msp_writereg(av7110, MSP_WR_DSP, 0x0009, src);
  353. msp_writereg(av7110, MSP_WR_DSP, 0x000a, src);
  354. return 0;
  355. }
  356. static int vidioc_g_frequency(struct file *file, void *fh, struct v4l2_frequency *f)
  357. {
  358. struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
  359. struct av7110 *av7110 = (struct av7110 *)dev->ext_priv;
  360. dprintk(2, "VIDIOC_G_FREQ: freq:0x%08x\n", f->frequency);
  361. if (!av7110->analog_tuner_flags || av7110->current_input != 1)
  362. return -EINVAL;
  363. memset(f, 0, sizeof(*f));
  364. f->type = V4L2_TUNER_ANALOG_TV;
  365. f->frequency = av7110->current_freq;
  366. return 0;
  367. }
  368. static int vidioc_s_frequency(struct file *file, void *fh, const struct v4l2_frequency *f)
  369. {
  370. struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
  371. struct av7110 *av7110 = (struct av7110 *)dev->ext_priv;
  372. dprintk(2, "VIDIOC_S_FREQUENCY: freq:0x%08x\n", f->frequency);
  373. if (!av7110->analog_tuner_flags || av7110->current_input != 1)
  374. return -EINVAL;
  375. if (V4L2_TUNER_ANALOG_TV != f->type)
  376. return -EINVAL;
  377. msp_writereg(av7110, MSP_WR_DSP, 0x0000, 0xffe0); /* fast mute */
  378. msp_writereg(av7110, MSP_WR_DSP, 0x0007, 0xffe0);
  379. /* tune in desired frequency */
  380. if (av7110->analog_tuner_flags & ANALOG_TUNER_VES1820)
  381. ves1820_set_tv_freq(dev, f->frequency);
  382. else if (av7110->analog_tuner_flags & ANALOG_TUNER_STV0297)
  383. stv0297_set_tv_freq(dev, f->frequency);
  384. av7110->current_freq = f->frequency;
  385. msp_writereg(av7110, MSP_WR_DSP, 0x0015, 0x003f); /* start stereo detection */
  386. msp_writereg(av7110, MSP_WR_DSP, 0x0015, 0x0000);
  387. msp_writereg(av7110, MSP_WR_DSP, 0x0000, 0x4f00); /* loudspeaker + headphone */
  388. msp_writereg(av7110, MSP_WR_DSP, 0x0007, 0x4f00); /* SCART 1 volume */
  389. return 0;
  390. }
  391. static int vidioc_enum_input(struct file *file, void *fh, struct v4l2_input *i)
  392. {
  393. struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
  394. struct av7110 *av7110 = (struct av7110 *)dev->ext_priv;
  395. dprintk(2, "VIDIOC_ENUMINPUT: %d\n", i->index);
  396. if (av7110->analog_tuner_flags) {
  397. if (i->index >= 4)
  398. return -EINVAL;
  399. } else {
  400. if (i->index != 0)
  401. return -EINVAL;
  402. }
  403. memcpy(i, &inputs[i->index], sizeof(struct v4l2_input));
  404. return 0;
  405. }
  406. static int vidioc_g_input(struct file *file, void *fh, unsigned int *input)
  407. {
  408. struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
  409. struct av7110 *av7110 = (struct av7110 *)dev->ext_priv;
  410. *input = av7110->current_input;
  411. dprintk(2, "VIDIOC_G_INPUT: %d\n", *input);
  412. return 0;
  413. }
  414. static int vidioc_s_input(struct file *file, void *fh, unsigned int input)
  415. {
  416. struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
  417. struct av7110 *av7110 = (struct av7110 *)dev->ext_priv;
  418. dprintk(2, "VIDIOC_S_INPUT: %d\n", input);
  419. if (!av7110->analog_tuner_flags)
  420. return input ? -EINVAL : 0;
  421. if (input >= 4)
  422. return -EINVAL;
  423. av7110->current_input = input;
  424. return av7110_dvb_c_switch(fh);
  425. }
  426. static int vidioc_enumaudio(struct file *file, void *fh, struct v4l2_audio *a)
  427. {
  428. dprintk(2, "VIDIOC_G_AUDIO: %d\n", a->index);
  429. if (a->index != 0)
  430. return -EINVAL;
  431. *a = msp3400_v4l2_audio;
  432. return 0;
  433. }
  434. static int vidioc_g_audio(struct file *file, void *fh, struct v4l2_audio *a)
  435. {
  436. struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
  437. struct av7110 *av7110 = (struct av7110 *)dev->ext_priv;
  438. dprintk(2, "VIDIOC_G_AUDIO: %d\n", a->index);
  439. if (a->index != 0)
  440. return -EINVAL;
  441. if (av7110->current_input >= 2)
  442. return -EINVAL;
  443. *a = msp3400_v4l2_audio;
  444. return 0;
  445. }
  446. static int vidioc_s_audio(struct file *file, void *fh, const struct v4l2_audio *a)
  447. {
  448. struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
  449. struct av7110 *av7110 = (struct av7110 *)dev->ext_priv;
  450. dprintk(2, "VIDIOC_S_AUDIO: %d\n", a->index);
  451. if (av7110->current_input >= 2)
  452. return -EINVAL;
  453. return a->index ? -EINVAL : 0;
  454. }
  455. static int vidioc_g_sliced_vbi_cap(struct file *file, void *fh,
  456. struct v4l2_sliced_vbi_cap *cap)
  457. {
  458. struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
  459. struct av7110 *av7110 = (struct av7110 *)dev->ext_priv;
  460. dprintk(2, "VIDIOC_G_SLICED_VBI_CAP\n");
  461. if (cap->type != V4L2_BUF_TYPE_SLICED_VBI_OUTPUT)
  462. return -EINVAL;
  463. if (FW_VERSION(av7110->arm_app) >= 0x2623) {
  464. cap->service_set = V4L2_SLICED_WSS_625;
  465. cap->service_lines[0][23] = V4L2_SLICED_WSS_625;
  466. }
  467. return 0;
  468. }
  469. static int vidioc_g_fmt_sliced_vbi_out(struct file *file, void *fh,
  470. struct v4l2_format *f)
  471. {
  472. struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
  473. struct av7110 *av7110 = (struct av7110 *)dev->ext_priv;
  474. dprintk(2, "VIDIOC_G_FMT:\n");
  475. if (FW_VERSION(av7110->arm_app) < 0x2623)
  476. return -EINVAL;
  477. memset(&f->fmt.sliced, 0, sizeof f->fmt.sliced);
  478. if (av7110->wssMode) {
  479. f->fmt.sliced.service_set = V4L2_SLICED_WSS_625;
  480. f->fmt.sliced.service_lines[0][23] = V4L2_SLICED_WSS_625;
  481. f->fmt.sliced.io_size = sizeof(struct v4l2_sliced_vbi_data);
  482. }
  483. return 0;
  484. }
  485. static int vidioc_s_fmt_sliced_vbi_out(struct file *file, void *fh,
  486. struct v4l2_format *f)
  487. {
  488. struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
  489. struct av7110 *av7110 = (struct av7110 *)dev->ext_priv;
  490. dprintk(2, "VIDIOC_S_FMT\n");
  491. if (FW_VERSION(av7110->arm_app) < 0x2623)
  492. return -EINVAL;
  493. if (f->fmt.sliced.service_set != V4L2_SLICED_WSS_625 &&
  494. f->fmt.sliced.service_lines[0][23] != V4L2_SLICED_WSS_625) {
  495. memset(&f->fmt.sliced, 0, sizeof(f->fmt.sliced));
  496. /* WSS controlled by firmware */
  497. av7110->wssMode = 0;
  498. av7110->wssData = 0;
  499. return av7110_fw_cmd(av7110, COMTYPE_ENCODER,
  500. SetWSSConfig, 1, 0);
  501. } else {
  502. memset(&f->fmt.sliced, 0, sizeof(f->fmt.sliced));
  503. f->fmt.sliced.service_set = V4L2_SLICED_WSS_625;
  504. f->fmt.sliced.service_lines[0][23] = V4L2_SLICED_WSS_625;
  505. f->fmt.sliced.io_size = sizeof(struct v4l2_sliced_vbi_data);
  506. /* WSS controlled by userspace */
  507. av7110->wssMode = 1;
  508. av7110->wssData = 0;
  509. }
  510. return 0;
  511. }
  512. static int av7110_vbi_reset(struct file *file)
  513. {
  514. struct saa7146_fh *fh = file->private_data;
  515. struct saa7146_dev *dev = fh->dev;
  516. struct av7110 *av7110 = (struct av7110*) dev->ext_priv;
  517. dprintk(2, "%s\n", __func__);
  518. av7110->wssMode = 0;
  519. av7110->wssData = 0;
  520. if (FW_VERSION(av7110->arm_app) < 0x2623)
  521. return 0;
  522. else
  523. return av7110_fw_cmd(av7110, COMTYPE_ENCODER, SetWSSConfig, 1, 0);
  524. }
  525. static ssize_t av7110_vbi_write(struct file *file, const char __user *data, size_t count, loff_t *ppos)
  526. {
  527. struct saa7146_fh *fh = file->private_data;
  528. struct saa7146_dev *dev = fh->dev;
  529. struct av7110 *av7110 = (struct av7110*) dev->ext_priv;
  530. struct v4l2_sliced_vbi_data d;
  531. int rc;
  532. dprintk(2, "%s\n", __func__);
  533. if (FW_VERSION(av7110->arm_app) < 0x2623 || !av7110->wssMode || count != sizeof d)
  534. return -EINVAL;
  535. if (copy_from_user(&d, data, count))
  536. return -EFAULT;
  537. if ((d.id != 0 && d.id != V4L2_SLICED_WSS_625) || d.field != 0 || d.line != 23)
  538. return -EINVAL;
  539. if (d.id)
  540. av7110->wssData = ((d.data[1] << 8) & 0x3f00) | d.data[0];
  541. else
  542. av7110->wssData = 0x8000;
  543. rc = av7110_fw_cmd(av7110, COMTYPE_ENCODER, SetWSSConfig, 2, 1, av7110->wssData);
  544. return (rc < 0) ? rc : count;
  545. }
  546. /****************************************************************************
  547. * INITIALIZATION
  548. ****************************************************************************/
  549. static u8 saa7113_init_regs[] = {
  550. 0x02, 0xd0,
  551. 0x03, 0x23,
  552. 0x04, 0x00,
  553. 0x05, 0x00,
  554. 0x06, 0xe9,
  555. 0x07, 0x0d,
  556. 0x08, 0x98,
  557. 0x09, 0x02,
  558. 0x0a, 0x80,
  559. 0x0b, 0x40,
  560. 0x0c, 0x40,
  561. 0x0d, 0x00,
  562. 0x0e, 0x01,
  563. 0x0f, 0x7c,
  564. 0x10, 0x48,
  565. 0x11, 0x0c,
  566. 0x12, 0x8b,
  567. 0x13, 0x1a,
  568. 0x14, 0x00,
  569. 0x15, 0x00,
  570. 0x16, 0x00,
  571. 0x17, 0x00,
  572. 0x18, 0x00,
  573. 0x19, 0x00,
  574. 0x1a, 0x00,
  575. 0x1b, 0x00,
  576. 0x1c, 0x00,
  577. 0x1d, 0x00,
  578. 0x1e, 0x00,
  579. 0x41, 0x77,
  580. 0x42, 0x77,
  581. 0x43, 0x77,
  582. 0x44, 0x77,
  583. 0x45, 0x77,
  584. 0x46, 0x77,
  585. 0x47, 0x77,
  586. 0x48, 0x77,
  587. 0x49, 0x77,
  588. 0x4a, 0x77,
  589. 0x4b, 0x77,
  590. 0x4c, 0x77,
  591. 0x4d, 0x77,
  592. 0x4e, 0x77,
  593. 0x4f, 0x77,
  594. 0x50, 0x77,
  595. 0x51, 0x77,
  596. 0x52, 0x77,
  597. 0x53, 0x77,
  598. 0x54, 0x77,
  599. 0x55, 0x77,
  600. 0x56, 0x77,
  601. 0x57, 0xff,
  602. 0xff
  603. };
  604. static struct saa7146_ext_vv av7110_vv_data_st;
  605. static struct saa7146_ext_vv av7110_vv_data_c;
  606. int av7110_init_analog_module(struct av7110 *av7110)
  607. {
  608. u16 version1, version2;
  609. if (i2c_writereg(av7110, 0x80, 0x0, 0x80) == 1 &&
  610. i2c_writereg(av7110, 0x80, 0x0, 0) == 1) {
  611. pr_info("DVB-C analog module @ card %d detected, initializing MSP3400\n",
  612. av7110->dvb_adapter.num);
  613. av7110->adac_type = DVB_ADAC_MSP34x0;
  614. } else if (i2c_writereg(av7110, 0x84, 0x0, 0x80) == 1 &&
  615. i2c_writereg(av7110, 0x84, 0x0, 0) == 1) {
  616. pr_info("DVB-C analog module @ card %d detected, initializing MSP3415\n",
  617. av7110->dvb_adapter.num);
  618. av7110->adac_type = DVB_ADAC_MSP34x5;
  619. } else
  620. return -ENODEV;
  621. msleep(100); // the probing above resets the msp...
  622. msp_readreg(av7110, MSP_RD_DSP, 0x001e, &version1);
  623. msp_readreg(av7110, MSP_RD_DSP, 0x001f, &version2);
  624. dprintk(1, "dvb-ttpci: @ card %d MSP34xx version 0x%04x 0x%04x\n",
  625. av7110->dvb_adapter.num, version1, version2);
  626. msp_writereg(av7110, MSP_WR_DSP, 0x0013, 0x0c00);
  627. msp_writereg(av7110, MSP_WR_DSP, 0x0000, 0x7f00); // loudspeaker + headphone
  628. msp_writereg(av7110, MSP_WR_DSP, 0x0008, 0x0220); // loudspeaker source
  629. msp_writereg(av7110, MSP_WR_DSP, 0x0009, 0x0220); // headphone source
  630. msp_writereg(av7110, MSP_WR_DSP, 0x0004, 0x7f00); // loudspeaker volume
  631. msp_writereg(av7110, MSP_WR_DSP, 0x000a, 0x0220); // SCART 1 source
  632. msp_writereg(av7110, MSP_WR_DSP, 0x0007, 0x7f00); // SCART 1 volume
  633. msp_writereg(av7110, MSP_WR_DSP, 0x000d, 0x1900); // prescale SCART
  634. if (i2c_writereg(av7110, 0x48, 0x01, 0x00)!=1) {
  635. pr_info("saa7113 not accessible\n");
  636. } else {
  637. u8 *i = saa7113_init_regs;
  638. if ((av7110->dev->pci->subsystem_vendor == 0x110a) && (av7110->dev->pci->subsystem_device == 0x0000)) {
  639. /* Fujitsu/Siemens DVB-Cable */
  640. av7110->analog_tuner_flags |= ANALOG_TUNER_VES1820;
  641. } else if ((av7110->dev->pci->subsystem_vendor == 0x13c2) && (av7110->dev->pci->subsystem_device == 0x0002)) {
  642. /* Hauppauge/TT DVB-C premium */
  643. av7110->analog_tuner_flags |= ANALOG_TUNER_VES1820;
  644. } else if ((av7110->dev->pci->subsystem_vendor == 0x13c2) && (av7110->dev->pci->subsystem_device == 0x000A)) {
  645. /* Hauppauge/TT DVB-C premium */
  646. av7110->analog_tuner_flags |= ANALOG_TUNER_STV0297;
  647. }
  648. /* setup for DVB by default */
  649. if (av7110->analog_tuner_flags & ANALOG_TUNER_VES1820) {
  650. if (ves1820_writereg(av7110->dev, 0x09, 0x0f, 0x20))
  651. dprintk(1, "setting band in demodulator failed\n");
  652. } else if (av7110->analog_tuner_flags & ANALOG_TUNER_STV0297) {
  653. saa7146_setgpio(av7110->dev, 1, SAA7146_GPIO_OUTLO); // TDA9819 pin9(STD)
  654. saa7146_setgpio(av7110->dev, 3, SAA7146_GPIO_OUTLO); // TDA9819 pin30(VIF)
  655. }
  656. /* init the saa7113 */
  657. while (*i != 0xff) {
  658. if (i2c_writereg(av7110, 0x48, i[0], i[1]) != 1) {
  659. dprintk(1, "saa7113 initialization failed @ card %d", av7110->dvb_adapter.num);
  660. break;
  661. }
  662. i += 2;
  663. }
  664. /* setup msp for analog sound: B/G Dual-FM */
  665. msp_writereg(av7110, MSP_WR_DEM, 0x00bb, 0x02d0); // AD_CV
  666. msp_writereg(av7110, MSP_WR_DEM, 0x0001, 3); // FIR1
  667. msp_writereg(av7110, MSP_WR_DEM, 0x0001, 18); // FIR1
  668. msp_writereg(av7110, MSP_WR_DEM, 0x0001, 27); // FIR1
  669. msp_writereg(av7110, MSP_WR_DEM, 0x0001, 48); // FIR1
  670. msp_writereg(av7110, MSP_WR_DEM, 0x0001, 66); // FIR1
  671. msp_writereg(av7110, MSP_WR_DEM, 0x0001, 72); // FIR1
  672. msp_writereg(av7110, MSP_WR_DEM, 0x0005, 4); // FIR2
  673. msp_writereg(av7110, MSP_WR_DEM, 0x0005, 64); // FIR2
  674. msp_writereg(av7110, MSP_WR_DEM, 0x0005, 0); // FIR2
  675. msp_writereg(av7110, MSP_WR_DEM, 0x0005, 3); // FIR2
  676. msp_writereg(av7110, MSP_WR_DEM, 0x0005, 18); // FIR2
  677. msp_writereg(av7110, MSP_WR_DEM, 0x0005, 27); // FIR2
  678. msp_writereg(av7110, MSP_WR_DEM, 0x0005, 48); // FIR2
  679. msp_writereg(av7110, MSP_WR_DEM, 0x0005, 66); // FIR2
  680. msp_writereg(av7110, MSP_WR_DEM, 0x0005, 72); // FIR2
  681. msp_writereg(av7110, MSP_WR_DEM, 0x0083, 0xa000); // MODE_REG
  682. msp_writereg(av7110, MSP_WR_DEM, 0x0093, 0x00aa); // DCO1_LO 5.74MHz
  683. msp_writereg(av7110, MSP_WR_DEM, 0x009b, 0x04fc); // DCO1_HI
  684. msp_writereg(av7110, MSP_WR_DEM, 0x00a3, 0x038e); // DCO2_LO 5.5MHz
  685. msp_writereg(av7110, MSP_WR_DEM, 0x00ab, 0x04c6); // DCO2_HI
  686. msp_writereg(av7110, MSP_WR_DEM, 0x0056, 0); // LOAD_REG 1/2
  687. }
  688. memcpy(standard, dvb_standard, sizeof(struct saa7146_standard) * 2);
  689. /* set dd1 stream a & b */
  690. saa7146_write(av7110->dev, DD1_STREAM_B, 0x00000000);
  691. saa7146_write(av7110->dev, DD1_INIT, 0x03000700);
  692. saa7146_write(av7110->dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26));
  693. return 0;
  694. }
  695. int av7110_init_v4l(struct av7110 *av7110)
  696. {
  697. struct saa7146_dev* dev = av7110->dev;
  698. struct saa7146_ext_vv *vv_data;
  699. int ret;
  700. /* special case DVB-C: these cards have an analog tuner
  701. plus need some special handling, so we have separate
  702. saa7146_ext_vv data for these... */
  703. if (av7110->analog_tuner_flags)
  704. vv_data = &av7110_vv_data_c;
  705. else
  706. vv_data = &av7110_vv_data_st;
  707. ret = saa7146_vv_init(dev, vv_data);
  708. if (ret) {
  709. ERR("cannot init capture device. skipping\n");
  710. return -ENODEV;
  711. }
  712. vv_data->vid_ops.vidioc_enum_input = vidioc_enum_input;
  713. vv_data->vid_ops.vidioc_g_input = vidioc_g_input;
  714. vv_data->vid_ops.vidioc_s_input = vidioc_s_input;
  715. vv_data->vid_ops.vidioc_g_tuner = vidioc_g_tuner;
  716. vv_data->vid_ops.vidioc_s_tuner = vidioc_s_tuner;
  717. vv_data->vid_ops.vidioc_g_frequency = vidioc_g_frequency;
  718. vv_data->vid_ops.vidioc_s_frequency = vidioc_s_frequency;
  719. vv_data->vid_ops.vidioc_enumaudio = vidioc_enumaudio;
  720. vv_data->vid_ops.vidioc_g_audio = vidioc_g_audio;
  721. vv_data->vid_ops.vidioc_s_audio = vidioc_s_audio;
  722. vv_data->vid_ops.vidioc_g_fmt_vbi_cap = NULL;
  723. vv_data->vbi_ops.vidioc_g_tuner = vidioc_g_tuner;
  724. vv_data->vbi_ops.vidioc_s_tuner = vidioc_s_tuner;
  725. vv_data->vbi_ops.vidioc_g_frequency = vidioc_g_frequency;
  726. vv_data->vbi_ops.vidioc_s_frequency = vidioc_s_frequency;
  727. vv_data->vbi_ops.vidioc_g_fmt_vbi_cap = NULL;
  728. vv_data->vbi_ops.vidioc_g_sliced_vbi_cap = vidioc_g_sliced_vbi_cap;
  729. vv_data->vbi_ops.vidioc_g_fmt_sliced_vbi_out = vidioc_g_fmt_sliced_vbi_out;
  730. vv_data->vbi_ops.vidioc_s_fmt_sliced_vbi_out = vidioc_s_fmt_sliced_vbi_out;
  731. if (FW_VERSION(av7110->arm_app) < 0x2623)
  732. vv_data->capabilities &= ~V4L2_CAP_SLICED_VBI_OUTPUT;
  733. if (saa7146_register_device(&av7110->v4l_dev, dev, "av7110", VFL_TYPE_GRABBER)) {
  734. ERR("cannot register capture device. skipping\n");
  735. saa7146_vv_release(dev);
  736. return -ENODEV;
  737. }
  738. if (FW_VERSION(av7110->arm_app) >= 0x2623) {
  739. if (saa7146_register_device(&av7110->vbi_dev, dev, "av7110", VFL_TYPE_VBI))
  740. ERR("cannot register vbi v4l2 device. skipping\n");
  741. }
  742. return 0;
  743. }
  744. int av7110_exit_v4l(struct av7110 *av7110)
  745. {
  746. struct saa7146_dev* dev = av7110->dev;
  747. saa7146_unregister_device(&av7110->v4l_dev, av7110->dev);
  748. saa7146_unregister_device(&av7110->vbi_dev, av7110->dev);
  749. saa7146_vv_release(dev);
  750. return 0;
  751. }
  752. /* FIXME: these values are experimental values that look better than the
  753. values from the latest "official" driver -- at least for me... (MiHu) */
  754. static struct saa7146_standard standard[] = {
  755. {
  756. .name = "PAL", .id = V4L2_STD_PAL_BG,
  757. .v_offset = 0x15, .v_field = 288,
  758. .h_offset = 0x48, .h_pixels = 708,
  759. .v_max_out = 576, .h_max_out = 768,
  760. }, {
  761. .name = "NTSC", .id = V4L2_STD_NTSC,
  762. .v_offset = 0x10, .v_field = 244,
  763. .h_offset = 0x40, .h_pixels = 708,
  764. .v_max_out = 480, .h_max_out = 640,
  765. }
  766. };
  767. static struct saa7146_standard analog_standard[] = {
  768. {
  769. .name = "PAL", .id = V4L2_STD_PAL_BG,
  770. .v_offset = 0x1b, .v_field = 288,
  771. .h_offset = 0x08, .h_pixels = 708,
  772. .v_max_out = 576, .h_max_out = 768,
  773. }, {
  774. .name = "NTSC", .id = V4L2_STD_NTSC,
  775. .v_offset = 0x10, .v_field = 244,
  776. .h_offset = 0x40, .h_pixels = 708,
  777. .v_max_out = 480, .h_max_out = 640,
  778. }
  779. };
  780. static struct saa7146_standard dvb_standard[] = {
  781. {
  782. .name = "PAL", .id = V4L2_STD_PAL_BG,
  783. .v_offset = 0x14, .v_field = 288,
  784. .h_offset = 0x48, .h_pixels = 708,
  785. .v_max_out = 576, .h_max_out = 768,
  786. }, {
  787. .name = "NTSC", .id = V4L2_STD_NTSC,
  788. .v_offset = 0x10, .v_field = 244,
  789. .h_offset = 0x40, .h_pixels = 708,
  790. .v_max_out = 480, .h_max_out = 640,
  791. }
  792. };
  793. static int std_callback(struct saa7146_dev* dev, struct saa7146_standard *std)
  794. {
  795. struct av7110 *av7110 = (struct av7110*) dev->ext_priv;
  796. if (std->id & V4L2_STD_PAL) {
  797. av7110->vidmode = AV7110_VIDEO_MODE_PAL;
  798. av7110_set_vidmode(av7110, av7110->vidmode);
  799. }
  800. else if (std->id & V4L2_STD_NTSC) {
  801. av7110->vidmode = AV7110_VIDEO_MODE_NTSC;
  802. av7110_set_vidmode(av7110, av7110->vidmode);
  803. }
  804. else
  805. return -1;
  806. return 0;
  807. }
  808. static struct saa7146_ext_vv av7110_vv_data_st = {
  809. .inputs = 1,
  810. .audios = 1,
  811. .capabilities = V4L2_CAP_SLICED_VBI_OUTPUT | V4L2_CAP_AUDIO,
  812. .flags = 0,
  813. .stds = &standard[0],
  814. .num_stds = ARRAY_SIZE(standard),
  815. .std_callback = &std_callback,
  816. .vbi_fops.open = av7110_vbi_reset,
  817. .vbi_fops.release = av7110_vbi_reset,
  818. .vbi_fops.write = av7110_vbi_write,
  819. };
  820. static struct saa7146_ext_vv av7110_vv_data_c = {
  821. .inputs = 1,
  822. .audios = 1,
  823. .capabilities = V4L2_CAP_TUNER | V4L2_CAP_SLICED_VBI_OUTPUT | V4L2_CAP_AUDIO,
  824. .flags = SAA7146_USE_PORT_B_FOR_VBI,
  825. .stds = &standard[0],
  826. .num_stds = ARRAY_SIZE(standard),
  827. .std_callback = &std_callback,
  828. .vbi_fops.open = av7110_vbi_reset,
  829. .vbi_fops.release = av7110_vbi_reset,
  830. .vbi_fops.write = av7110_vbi_write,
  831. };