av7110_v4l.c 27 KB

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