av7110_v4l.c 26 KB

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