av7110_v4l.c 26 KB

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