av7110_v4l.c 27 KB

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