av7110_v4l.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  28. #include <linux/kernel.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 "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. .capabilities = V4L2_IN_CAP_STD,
  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. .capabilities = V4L2_IN_CAP_STD,
  105. }, {
  106. .index = 2,
  107. .name = "Video",
  108. .type = V4L2_INPUT_TYPE_CAMERA,
  109. .audioset = 0,
  110. .tuner = 0,
  111. .std = V4L2_STD_PAL_BG|V4L2_STD_NTSC_M,
  112. .status = 0,
  113. .capabilities = V4L2_IN_CAP_STD,
  114. }, {
  115. .index = 3,
  116. .name = "Y/C",
  117. .type = V4L2_INPUT_TYPE_CAMERA,
  118. .audioset = 0,
  119. .tuner = 0,
  120. .std = V4L2_STD_PAL_BG|V4L2_STD_NTSC_M,
  121. .status = 0,
  122. .capabilities = V4L2_IN_CAP_STD,
  123. }
  124. };
  125. static int ves1820_writereg(struct saa7146_dev *dev, u8 addr, u8 reg, u8 data)
  126. {
  127. struct av7110 *av7110 = dev->ext_priv;
  128. u8 buf[] = { 0x00, reg, data };
  129. struct i2c_msg msg = { .addr = addr, .flags = 0, .buf = buf, .len = 3 };
  130. dprintk(4, "dev: %p\n", dev);
  131. if (1 != i2c_transfer(&av7110->i2c_adap, &msg, 1))
  132. return -1;
  133. return 0;
  134. }
  135. static int tuner_write(struct saa7146_dev *dev, u8 addr, u8 data [4])
  136. {
  137. struct av7110 *av7110 = dev->ext_priv;
  138. struct i2c_msg msg = { .addr = addr, .flags = 0, .buf = data, .len = 4 };
  139. dprintk(4, "dev: %p\n", dev);
  140. if (1 != i2c_transfer(&av7110->i2c_adap, &msg, 1))
  141. return -1;
  142. return 0;
  143. }
  144. static int ves1820_set_tv_freq(struct saa7146_dev *dev, u32 freq)
  145. {
  146. u32 div;
  147. u8 config;
  148. u8 buf[4];
  149. dprintk(4, "freq: 0x%08x\n", freq);
  150. /* magic number: 614. tuning with the frequency given by v4l2
  151. is always off by 614*62.5 = 38375 kHz...*/
  152. div = freq + 614;
  153. buf[0] = (div >> 8) & 0x7f;
  154. buf[1] = div & 0xff;
  155. buf[2] = 0x8e;
  156. if (freq < (u32) (16 * 168.25))
  157. config = 0xa0;
  158. else if (freq < (u32) (16 * 447.25))
  159. config = 0x90;
  160. else
  161. config = 0x30;
  162. config &= ~0x02;
  163. buf[3] = config;
  164. return tuner_write(dev, 0x61, buf);
  165. }
  166. static int stv0297_set_tv_freq(struct saa7146_dev *dev, u32 freq)
  167. {
  168. struct av7110 *av7110 = (struct av7110*)dev->ext_priv;
  169. u32 div;
  170. u8 data[4];
  171. div = (freq + 38900000 + 31250) / 62500;
  172. data[0] = (div >> 8) & 0x7f;
  173. data[1] = div & 0xff;
  174. data[2] = 0xce;
  175. if (freq < 45000000)
  176. return -EINVAL;
  177. else if (freq < 137000000)
  178. data[3] = 0x01;
  179. else if (freq < 403000000)
  180. data[3] = 0x02;
  181. else if (freq < 860000000)
  182. data[3] = 0x04;
  183. else
  184. return -EINVAL;
  185. if (av7110->fe->ops.i2c_gate_ctrl)
  186. av7110->fe->ops.i2c_gate_ctrl(av7110->fe, 1);
  187. return tuner_write(dev, 0x63, data);
  188. }
  189. static struct saa7146_standard analog_standard[];
  190. static struct saa7146_standard dvb_standard[];
  191. static struct saa7146_standard standard[];
  192. static struct v4l2_audio msp3400_v4l2_audio = {
  193. .index = 0,
  194. .name = "Television",
  195. .capability = V4L2_AUDCAP_STEREO
  196. };
  197. static int av7110_dvb_c_switch(struct saa7146_fh *fh)
  198. {
  199. struct saa7146_dev *dev = fh->dev;
  200. struct saa7146_vv *vv = dev->vv_data;
  201. struct av7110 *av7110 = (struct av7110*)dev->ext_priv;
  202. u16 adswitch;
  203. int source, sync, err;
  204. dprintk(4, "%p\n", av7110);
  205. if ((vv->video_status & STATUS_OVERLAY) != 0) {
  206. vv->ov_suspend = vv->video_fh;
  207. err = saa7146_stop_preview(vv->video_fh); /* side effect: video_status is now 0, video_fh is NULL */
  208. if (err != 0) {
  209. dprintk(2, "suspending video failed\n");
  210. vv->ov_suspend = NULL;
  211. }
  212. }
  213. if (0 != av7110->current_input) {
  214. dprintk(1, "switching to analog TV:\n");
  215. adswitch = 1;
  216. source = SAA7146_HPS_SOURCE_PORT_B;
  217. sync = SAA7146_HPS_SYNC_PORT_B;
  218. memcpy(standard, analog_standard, sizeof(struct saa7146_standard) * 2);
  219. switch (av7110->current_input) {
  220. case 1:
  221. dprintk(1, "switching SAA7113 to Analog Tuner Input\n");
  222. msp_writereg(av7110, MSP_WR_DSP, 0x0008, 0x0000); // loudspeaker source
  223. msp_writereg(av7110, MSP_WR_DSP, 0x0009, 0x0000); // headphone source
  224. msp_writereg(av7110, MSP_WR_DSP, 0x000a, 0x0000); // SCART 1 source
  225. msp_writereg(av7110, MSP_WR_DSP, 0x000e, 0x3000); // FM matrix, mono
  226. msp_writereg(av7110, MSP_WR_DSP, 0x0000, 0x4f00); // loudspeaker + headphone
  227. msp_writereg(av7110, MSP_WR_DSP, 0x0007, 0x4f00); // SCART 1 volume
  228. if (av7110->analog_tuner_flags & ANALOG_TUNER_VES1820) {
  229. if (ves1820_writereg(dev, 0x09, 0x0f, 0x60))
  230. dprintk(1, "setting band in demodulator failed\n");
  231. } else if (av7110->analog_tuner_flags & ANALOG_TUNER_STV0297) {
  232. saa7146_setgpio(dev, 1, SAA7146_GPIO_OUTHI); // TDA9819 pin9(STD)
  233. saa7146_setgpio(dev, 3, SAA7146_GPIO_OUTHI); // TDA9819 pin30(VIF)
  234. }
  235. if (i2c_writereg(av7110, 0x48, 0x02, 0xd0) != 1)
  236. dprintk(1, "saa7113 write failed @ card %d", av7110->dvb_adapter.num);
  237. break;
  238. case 2:
  239. dprintk(1, "switching SAA7113 to Video AV CVBS Input\n");
  240. if (i2c_writereg(av7110, 0x48, 0x02, 0xd2) != 1)
  241. dprintk(1, "saa7113 write failed @ card %d", av7110->dvb_adapter.num);
  242. break;
  243. case 3:
  244. dprintk(1, "switching SAA7113 to Video AV Y/C Input\n");
  245. if (i2c_writereg(av7110, 0x48, 0x02, 0xd9) != 1)
  246. dprintk(1, "saa7113 write failed @ card %d", av7110->dvb_adapter.num);
  247. break;
  248. default:
  249. dprintk(1, "switching SAA7113 to Input: AV7110: SAA7113: invalid input\n");
  250. }
  251. } else {
  252. adswitch = 0;
  253. source = SAA7146_HPS_SOURCE_PORT_A;
  254. sync = SAA7146_HPS_SYNC_PORT_A;
  255. memcpy(standard, dvb_standard, sizeof(struct saa7146_standard) * 2);
  256. dprintk(1, "switching DVB mode\n");
  257. msp_writereg(av7110, MSP_WR_DSP, 0x0008, 0x0220); // loudspeaker source
  258. msp_writereg(av7110, MSP_WR_DSP, 0x0009, 0x0220); // headphone source
  259. msp_writereg(av7110, MSP_WR_DSP, 0x000a, 0x0220); // SCART 1 source
  260. msp_writereg(av7110, MSP_WR_DSP, 0x000e, 0x3000); // FM matrix, mono
  261. msp_writereg(av7110, MSP_WR_DSP, 0x0000, 0x7f00); // loudspeaker + headphone
  262. msp_writereg(av7110, MSP_WR_DSP, 0x0007, 0x7f00); // SCART 1 volume
  263. if (av7110->analog_tuner_flags & ANALOG_TUNER_VES1820) {
  264. if (ves1820_writereg(dev, 0x09, 0x0f, 0x20))
  265. dprintk(1, "setting band in demodulator failed\n");
  266. } else if (av7110->analog_tuner_flags & ANALOG_TUNER_STV0297) {
  267. saa7146_setgpio(dev, 1, SAA7146_GPIO_OUTLO); // TDA9819 pin9(STD)
  268. saa7146_setgpio(dev, 3, SAA7146_GPIO_OUTLO); // TDA9819 pin30(VIF)
  269. }
  270. }
  271. /* hmm, this does not do anything!? */
  272. if (av7110_fw_cmd(av7110, COMTYPE_AUDIODAC, ADSwitch, 1, adswitch))
  273. dprintk(1, "ADSwitch error\n");
  274. saa7146_set_hps_source_and_sync(dev, source, sync);
  275. if (vv->ov_suspend != NULL) {
  276. saa7146_start_preview(vv->ov_suspend);
  277. vv->ov_suspend = NULL;
  278. }
  279. return 0;
  280. }
  281. static int vidioc_g_tuner(struct file *file, void *fh, struct v4l2_tuner *t)
  282. {
  283. struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
  284. struct av7110 *av7110 = (struct av7110 *)dev->ext_priv;
  285. u16 stereo_det;
  286. s8 stereo;
  287. dprintk(2, "VIDIOC_G_TUNER: %d\n", t->index);
  288. if (!av7110->analog_tuner_flags || t->index != 0)
  289. return -EINVAL;
  290. memset(t, 0, sizeof(*t));
  291. strcpy((char *)t->name, "Television");
  292. t->type = V4L2_TUNER_ANALOG_TV;
  293. t->capability = V4L2_TUNER_CAP_NORM | V4L2_TUNER_CAP_STEREO |
  294. V4L2_TUNER_CAP_LANG1 | V4L2_TUNER_CAP_LANG2 | V4L2_TUNER_CAP_SAP;
  295. t->rangelow = 772; /* 48.25 MHZ / 62.5 kHz = 772, see fi1216mk2-specs, page 2 */
  296. t->rangehigh = 13684; /* 855.25 MHz / 62.5 kHz = 13684 */
  297. /* FIXME: add the real signal strength here */
  298. t->signal = 0xffff;
  299. t->afc = 0;
  300. /* FIXME: standard / stereo detection is still broken */
  301. msp_readreg(av7110, MSP_RD_DEM, 0x007e, &stereo_det);
  302. dprintk(1, "VIDIOC_G_TUNER: msp3400 TV standard detection: 0x%04x\n", stereo_det);
  303. msp_readreg(av7110, MSP_RD_DSP, 0x0018, &stereo_det);
  304. dprintk(1, "VIDIOC_G_TUNER: msp3400 stereo detection: 0x%04x\n", stereo_det);
  305. stereo = (s8)(stereo_det >> 8);
  306. if (stereo > 0x10) {
  307. /* stereo */
  308. t->rxsubchans = V4L2_TUNER_SUB_STEREO | V4L2_TUNER_SUB_MONO;
  309. t->audmode = V4L2_TUNER_MODE_STEREO;
  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. } else /* mono */
  315. t->rxsubchans = V4L2_TUNER_SUB_MONO;
  316. return 0;
  317. }
  318. static int vidioc_s_tuner(struct file *file, void *fh, struct v4l2_tuner *t)
  319. {
  320. struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
  321. struct av7110 *av7110 = (struct av7110 *)dev->ext_priv;
  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. static int vidioc_g_frequency(struct file *file, void *fh, struct v4l2_frequency *f)
  360. {
  361. struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
  362. struct av7110 *av7110 = (struct av7110 *)dev->ext_priv;
  363. dprintk(2, "VIDIOC_G_FREQ: freq:0x%08x\n", f->frequency);
  364. if (!av7110->analog_tuner_flags || av7110->current_input != 1)
  365. return -EINVAL;
  366. memset(f, 0, sizeof(*f));
  367. f->type = V4L2_TUNER_ANALOG_TV;
  368. f->frequency = av7110->current_freq;
  369. return 0;
  370. }
  371. static int vidioc_s_frequency(struct file *file, void *fh, struct v4l2_frequency *f)
  372. {
  373. struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
  374. struct av7110 *av7110 = (struct av7110 *)dev->ext_priv;
  375. dprintk(2, "VIDIOC_S_FREQUENCY: freq:0x%08x\n", f->frequency);
  376. if (!av7110->analog_tuner_flags || av7110->current_input != 1)
  377. return -EINVAL;
  378. if (V4L2_TUNER_ANALOG_TV != f->type)
  379. return -EINVAL;
  380. msp_writereg(av7110, MSP_WR_DSP, 0x0000, 0xffe0); /* fast mute */
  381. msp_writereg(av7110, MSP_WR_DSP, 0x0007, 0xffe0);
  382. /* tune in desired frequency */
  383. if (av7110->analog_tuner_flags & ANALOG_TUNER_VES1820)
  384. ves1820_set_tv_freq(dev, f->frequency);
  385. else if (av7110->analog_tuner_flags & ANALOG_TUNER_STV0297)
  386. stv0297_set_tv_freq(dev, f->frequency);
  387. av7110->current_freq = f->frequency;
  388. msp_writereg(av7110, MSP_WR_DSP, 0x0015, 0x003f); /* start stereo detection */
  389. msp_writereg(av7110, MSP_WR_DSP, 0x0015, 0x0000);
  390. msp_writereg(av7110, MSP_WR_DSP, 0x0000, 0x4f00); /* loudspeaker + headphone */
  391. msp_writereg(av7110, MSP_WR_DSP, 0x0007, 0x4f00); /* SCART 1 volume */
  392. return 0;
  393. }
  394. static int vidioc_enum_input(struct file *file, void *fh, struct v4l2_input *i)
  395. {
  396. struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
  397. struct av7110 *av7110 = (struct av7110 *)dev->ext_priv;
  398. dprintk(2, "VIDIOC_ENUMINPUT: %d\n", i->index);
  399. if (av7110->analog_tuner_flags) {
  400. if (i->index >= 4)
  401. return -EINVAL;
  402. } else {
  403. if (i->index != 0)
  404. return -EINVAL;
  405. }
  406. memcpy(i, &inputs[i->index], sizeof(struct v4l2_input));
  407. return 0;
  408. }
  409. static int vidioc_g_input(struct file *file, void *fh, unsigned int *input)
  410. {
  411. struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
  412. struct av7110 *av7110 = (struct av7110 *)dev->ext_priv;
  413. *input = av7110->current_input;
  414. dprintk(2, "VIDIOC_G_INPUT: %d\n", *input);
  415. return 0;
  416. }
  417. static int vidioc_s_input(struct file *file, void *fh, unsigned int input)
  418. {
  419. struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
  420. struct av7110 *av7110 = (struct av7110 *)dev->ext_priv;
  421. dprintk(2, "VIDIOC_S_INPUT: %d\n", input);
  422. if (!av7110->analog_tuner_flags)
  423. return 0;
  424. if (input >= 4)
  425. return -EINVAL;
  426. av7110->current_input = input;
  427. return av7110_dvb_c_switch(fh);
  428. }
  429. static int vidioc_g_audio(struct file *file, void *fh, struct v4l2_audio *a)
  430. {
  431. dprintk(2, "VIDIOC_G_AUDIO: %d\n", a->index);
  432. if (a->index != 0)
  433. return -EINVAL;
  434. memcpy(a, &msp3400_v4l2_audio, sizeof(struct v4l2_audio));
  435. return 0;
  436. }
  437. static int vidioc_s_audio(struct file *file, void *fh, struct v4l2_audio *a)
  438. {
  439. dprintk(2, "VIDIOC_S_AUDIO: %d\n", a->index);
  440. return 0;
  441. }
  442. static int vidioc_g_sliced_vbi_cap(struct file *file, void *fh,
  443. struct v4l2_sliced_vbi_cap *cap)
  444. {
  445. struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
  446. struct av7110 *av7110 = (struct av7110 *)dev->ext_priv;
  447. dprintk(2, "VIDIOC_G_SLICED_VBI_CAP\n");
  448. if (cap->type != V4L2_BUF_TYPE_SLICED_VBI_OUTPUT)
  449. return -EINVAL;
  450. if (FW_VERSION(av7110->arm_app) >= 0x2623) {
  451. cap->service_set = V4L2_SLICED_WSS_625;
  452. cap->service_lines[0][23] = V4L2_SLICED_WSS_625;
  453. }
  454. return 0;
  455. }
  456. static int vidioc_g_fmt_sliced_vbi_out(struct file *file, void *fh,
  457. struct v4l2_format *f)
  458. {
  459. struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
  460. struct av7110 *av7110 = (struct av7110 *)dev->ext_priv;
  461. dprintk(2, "VIDIOC_G_FMT:\n");
  462. if (FW_VERSION(av7110->arm_app) < 0x2623)
  463. return -EINVAL;
  464. memset(&f->fmt.sliced, 0, sizeof f->fmt.sliced);
  465. if (av7110->wssMode) {
  466. f->fmt.sliced.service_set = V4L2_SLICED_WSS_625;
  467. f->fmt.sliced.service_lines[0][23] = V4L2_SLICED_WSS_625;
  468. f->fmt.sliced.io_size = sizeof(struct v4l2_sliced_vbi_data);
  469. }
  470. return 0;
  471. }
  472. static int vidioc_s_fmt_sliced_vbi_out(struct file *file, void *fh,
  473. struct v4l2_format *f)
  474. {
  475. struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
  476. struct av7110 *av7110 = (struct av7110 *)dev->ext_priv;
  477. dprintk(2, "VIDIOC_S_FMT\n");
  478. if (FW_VERSION(av7110->arm_app) < 0x2623)
  479. return -EINVAL;
  480. if (f->fmt.sliced.service_set != V4L2_SLICED_WSS_625 &&
  481. f->fmt.sliced.service_lines[0][23] != V4L2_SLICED_WSS_625) {
  482. memset(&f->fmt.sliced, 0, sizeof(f->fmt.sliced));
  483. /* WSS controlled by firmware */
  484. av7110->wssMode = 0;
  485. av7110->wssData = 0;
  486. return av7110_fw_cmd(av7110, COMTYPE_ENCODER,
  487. SetWSSConfig, 1, 0);
  488. } else {
  489. memset(&f->fmt.sliced, 0, sizeof(f->fmt.sliced));
  490. f->fmt.sliced.service_set = V4L2_SLICED_WSS_625;
  491. f->fmt.sliced.service_lines[0][23] = V4L2_SLICED_WSS_625;
  492. f->fmt.sliced.io_size = sizeof(struct v4l2_sliced_vbi_data);
  493. /* WSS controlled by userspace */
  494. av7110->wssMode = 1;
  495. av7110->wssData = 0;
  496. }
  497. return 0;
  498. }
  499. static int av7110_vbi_reset(struct file *file)
  500. {
  501. struct saa7146_fh *fh = file->private_data;
  502. struct saa7146_dev *dev = fh->dev;
  503. struct av7110 *av7110 = (struct av7110*) dev->ext_priv;
  504. dprintk(2, "%s\n", __func__);
  505. av7110->wssMode = 0;
  506. av7110->wssData = 0;
  507. if (FW_VERSION(av7110->arm_app) < 0x2623)
  508. return 0;
  509. else
  510. return av7110_fw_cmd(av7110, COMTYPE_ENCODER, SetWSSConfig, 1, 0);
  511. }
  512. static ssize_t av7110_vbi_write(struct file *file, const char __user *data, size_t count, loff_t *ppos)
  513. {
  514. struct saa7146_fh *fh = file->private_data;
  515. struct saa7146_dev *dev = fh->dev;
  516. struct av7110 *av7110 = (struct av7110*) dev->ext_priv;
  517. struct v4l2_sliced_vbi_data d;
  518. int rc;
  519. dprintk(2, "%s\n", __func__);
  520. if (FW_VERSION(av7110->arm_app) < 0x2623 || !av7110->wssMode || count != sizeof d)
  521. return -EINVAL;
  522. if (copy_from_user(&d, data, count))
  523. return -EFAULT;
  524. if ((d.id != 0 && d.id != V4L2_SLICED_WSS_625) || d.field != 0 || d.line != 23)
  525. return -EINVAL;
  526. if (d.id)
  527. av7110->wssData = ((d.data[1] << 8) & 0x3f00) | d.data[0];
  528. else
  529. av7110->wssData = 0x8000;
  530. rc = av7110_fw_cmd(av7110, COMTYPE_ENCODER, SetWSSConfig, 2, 1, av7110->wssData);
  531. return (rc < 0) ? rc : count;
  532. }
  533. /****************************************************************************
  534. * INITIALIZATION
  535. ****************************************************************************/
  536. static u8 saa7113_init_regs[] = {
  537. 0x02, 0xd0,
  538. 0x03, 0x23,
  539. 0x04, 0x00,
  540. 0x05, 0x00,
  541. 0x06, 0xe9,
  542. 0x07, 0x0d,
  543. 0x08, 0x98,
  544. 0x09, 0x02,
  545. 0x0a, 0x80,
  546. 0x0b, 0x40,
  547. 0x0c, 0x40,
  548. 0x0d, 0x00,
  549. 0x0e, 0x01,
  550. 0x0f, 0x7c,
  551. 0x10, 0x48,
  552. 0x11, 0x0c,
  553. 0x12, 0x8b,
  554. 0x13, 0x1a,
  555. 0x14, 0x00,
  556. 0x15, 0x00,
  557. 0x16, 0x00,
  558. 0x17, 0x00,
  559. 0x18, 0x00,
  560. 0x19, 0x00,
  561. 0x1a, 0x00,
  562. 0x1b, 0x00,
  563. 0x1c, 0x00,
  564. 0x1d, 0x00,
  565. 0x1e, 0x00,
  566. 0x41, 0x77,
  567. 0x42, 0x77,
  568. 0x43, 0x77,
  569. 0x44, 0x77,
  570. 0x45, 0x77,
  571. 0x46, 0x77,
  572. 0x47, 0x77,
  573. 0x48, 0x77,
  574. 0x49, 0x77,
  575. 0x4a, 0x77,
  576. 0x4b, 0x77,
  577. 0x4c, 0x77,
  578. 0x4d, 0x77,
  579. 0x4e, 0x77,
  580. 0x4f, 0x77,
  581. 0x50, 0x77,
  582. 0x51, 0x77,
  583. 0x52, 0x77,
  584. 0x53, 0x77,
  585. 0x54, 0x77,
  586. 0x55, 0x77,
  587. 0x56, 0x77,
  588. 0x57, 0xff,
  589. 0xff
  590. };
  591. static struct saa7146_ext_vv av7110_vv_data_st;
  592. static struct saa7146_ext_vv av7110_vv_data_c;
  593. int av7110_init_analog_module(struct av7110 *av7110)
  594. {
  595. u16 version1, version2;
  596. if (i2c_writereg(av7110, 0x80, 0x0, 0x80) == 1 &&
  597. i2c_writereg(av7110, 0x80, 0x0, 0) == 1) {
  598. pr_info("DVB-C analog module @ card %d detected, initializing MSP3400\n",
  599. av7110->dvb_adapter.num);
  600. av7110->adac_type = DVB_ADAC_MSP34x0;
  601. } else if (i2c_writereg(av7110, 0x84, 0x0, 0x80) == 1 &&
  602. i2c_writereg(av7110, 0x84, 0x0, 0) == 1) {
  603. pr_info("DVB-C analog module @ card %d detected, initializing MSP3415\n",
  604. av7110->dvb_adapter.num);
  605. av7110->adac_type = DVB_ADAC_MSP34x5;
  606. } else
  607. return -ENODEV;
  608. msleep(100); // the probing above resets the msp...
  609. msp_readreg(av7110, MSP_RD_DSP, 0x001e, &version1);
  610. msp_readreg(av7110, MSP_RD_DSP, 0x001f, &version2);
  611. dprintk(1, "dvb-ttpci: @ card %d MSP34xx version 0x%04x 0x%04x\n",
  612. av7110->dvb_adapter.num, version1, version2);
  613. msp_writereg(av7110, MSP_WR_DSP, 0x0013, 0x0c00);
  614. msp_writereg(av7110, MSP_WR_DSP, 0x0000, 0x7f00); // loudspeaker + headphone
  615. msp_writereg(av7110, MSP_WR_DSP, 0x0008, 0x0220); // loudspeaker source
  616. msp_writereg(av7110, MSP_WR_DSP, 0x0009, 0x0220); // headphone source
  617. msp_writereg(av7110, MSP_WR_DSP, 0x0004, 0x7f00); // loudspeaker volume
  618. msp_writereg(av7110, MSP_WR_DSP, 0x000a, 0x0220); // SCART 1 source
  619. msp_writereg(av7110, MSP_WR_DSP, 0x0007, 0x7f00); // SCART 1 volume
  620. msp_writereg(av7110, MSP_WR_DSP, 0x000d, 0x1900); // prescale SCART
  621. if (i2c_writereg(av7110, 0x48, 0x01, 0x00)!=1) {
  622. pr_info("saa7113 not accessible\n");
  623. } else {
  624. u8 *i = saa7113_init_regs;
  625. if ((av7110->dev->pci->subsystem_vendor == 0x110a) && (av7110->dev->pci->subsystem_device == 0x0000)) {
  626. /* Fujitsu/Siemens DVB-Cable */
  627. av7110->analog_tuner_flags |= ANALOG_TUNER_VES1820;
  628. } else if ((av7110->dev->pci->subsystem_vendor == 0x13c2) && (av7110->dev->pci->subsystem_device == 0x0002)) {
  629. /* Hauppauge/TT DVB-C premium */
  630. av7110->analog_tuner_flags |= ANALOG_TUNER_VES1820;
  631. } else if ((av7110->dev->pci->subsystem_vendor == 0x13c2) && (av7110->dev->pci->subsystem_device == 0x000A)) {
  632. /* Hauppauge/TT DVB-C premium */
  633. av7110->analog_tuner_flags |= ANALOG_TUNER_STV0297;
  634. }
  635. /* setup for DVB by default */
  636. if (av7110->analog_tuner_flags & ANALOG_TUNER_VES1820) {
  637. if (ves1820_writereg(av7110->dev, 0x09, 0x0f, 0x20))
  638. dprintk(1, "setting band in demodulator failed\n");
  639. } else if (av7110->analog_tuner_flags & ANALOG_TUNER_STV0297) {
  640. saa7146_setgpio(av7110->dev, 1, SAA7146_GPIO_OUTLO); // TDA9819 pin9(STD)
  641. saa7146_setgpio(av7110->dev, 3, SAA7146_GPIO_OUTLO); // TDA9819 pin30(VIF)
  642. }
  643. /* init the saa7113 */
  644. while (*i != 0xff) {
  645. if (i2c_writereg(av7110, 0x48, i[0], i[1]) != 1) {
  646. dprintk(1, "saa7113 initialization failed @ card %d", av7110->dvb_adapter.num);
  647. break;
  648. }
  649. i += 2;
  650. }
  651. /* setup msp for analog sound: B/G Dual-FM */
  652. msp_writereg(av7110, MSP_WR_DEM, 0x00bb, 0x02d0); // AD_CV
  653. msp_writereg(av7110, MSP_WR_DEM, 0x0001, 3); // FIR1
  654. msp_writereg(av7110, MSP_WR_DEM, 0x0001, 18); // FIR1
  655. msp_writereg(av7110, MSP_WR_DEM, 0x0001, 27); // FIR1
  656. msp_writereg(av7110, MSP_WR_DEM, 0x0001, 48); // FIR1
  657. msp_writereg(av7110, MSP_WR_DEM, 0x0001, 66); // FIR1
  658. msp_writereg(av7110, MSP_WR_DEM, 0x0001, 72); // FIR1
  659. msp_writereg(av7110, MSP_WR_DEM, 0x0005, 4); // FIR2
  660. msp_writereg(av7110, MSP_WR_DEM, 0x0005, 64); // FIR2
  661. msp_writereg(av7110, MSP_WR_DEM, 0x0005, 0); // FIR2
  662. msp_writereg(av7110, MSP_WR_DEM, 0x0005, 3); // FIR2
  663. msp_writereg(av7110, MSP_WR_DEM, 0x0005, 18); // FIR2
  664. msp_writereg(av7110, MSP_WR_DEM, 0x0005, 27); // FIR2
  665. msp_writereg(av7110, MSP_WR_DEM, 0x0005, 48); // FIR2
  666. msp_writereg(av7110, MSP_WR_DEM, 0x0005, 66); // FIR2
  667. msp_writereg(av7110, MSP_WR_DEM, 0x0005, 72); // FIR2
  668. msp_writereg(av7110, MSP_WR_DEM, 0x0083, 0xa000); // MODE_REG
  669. msp_writereg(av7110, MSP_WR_DEM, 0x0093, 0x00aa); // DCO1_LO 5.74MHz
  670. msp_writereg(av7110, MSP_WR_DEM, 0x009b, 0x04fc); // DCO1_HI
  671. msp_writereg(av7110, MSP_WR_DEM, 0x00a3, 0x038e); // DCO2_LO 5.5MHz
  672. msp_writereg(av7110, MSP_WR_DEM, 0x00ab, 0x04c6); // DCO2_HI
  673. msp_writereg(av7110, MSP_WR_DEM, 0x0056, 0); // LOAD_REG 1/2
  674. }
  675. memcpy(standard, dvb_standard, sizeof(struct saa7146_standard) * 2);
  676. /* set dd1 stream a & b */
  677. saa7146_write(av7110->dev, DD1_STREAM_B, 0x00000000);
  678. saa7146_write(av7110->dev, DD1_INIT, 0x03000700);
  679. saa7146_write(av7110->dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26));
  680. return 0;
  681. }
  682. int av7110_init_v4l(struct av7110 *av7110)
  683. {
  684. struct saa7146_dev* dev = av7110->dev;
  685. struct saa7146_ext_vv *vv_data;
  686. int ret;
  687. /* special case DVB-C: these cards have an analog tuner
  688. plus need some special handling, so we have separate
  689. saa7146_ext_vv data for these... */
  690. if (av7110->analog_tuner_flags)
  691. vv_data = &av7110_vv_data_c;
  692. else
  693. vv_data = &av7110_vv_data_st;
  694. ret = saa7146_vv_init(dev, vv_data);
  695. if (ret) {
  696. ERR("cannot init capture device. skipping\n");
  697. return -ENODEV;
  698. }
  699. vv_data->ops.vidioc_enum_input = vidioc_enum_input;
  700. vv_data->ops.vidioc_g_input = vidioc_g_input;
  701. vv_data->ops.vidioc_s_input = vidioc_s_input;
  702. vv_data->ops.vidioc_g_tuner = vidioc_g_tuner;
  703. vv_data->ops.vidioc_s_tuner = vidioc_s_tuner;
  704. vv_data->ops.vidioc_g_frequency = vidioc_g_frequency;
  705. vv_data->ops.vidioc_s_frequency = vidioc_s_frequency;
  706. vv_data->ops.vidioc_g_audio = vidioc_g_audio;
  707. vv_data->ops.vidioc_s_audio = vidioc_s_audio;
  708. vv_data->ops.vidioc_g_sliced_vbi_cap = vidioc_g_sliced_vbi_cap;
  709. vv_data->ops.vidioc_g_fmt_sliced_vbi_out = vidioc_g_fmt_sliced_vbi_out;
  710. vv_data->ops.vidioc_s_fmt_sliced_vbi_out = vidioc_s_fmt_sliced_vbi_out;
  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 = AV7110_VIDEO_MODE_PAL;
  774. av7110_set_vidmode(av7110, av7110->vidmode);
  775. }
  776. else if (std->id & V4L2_STD_NTSC) {
  777. av7110->vidmode = AV7110_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. .vbi_fops.open = av7110_vbi_reset,
  793. .vbi_fops.release = av7110_vbi_reset,
  794. .vbi_fops.write = av7110_vbi_write,
  795. };
  796. static struct saa7146_ext_vv av7110_vv_data_c = {
  797. .inputs = 1,
  798. .audios = 1,
  799. .capabilities = V4L2_CAP_TUNER | V4L2_CAP_SLICED_VBI_OUTPUT,
  800. .flags = SAA7146_USE_PORT_B_FOR_VBI,
  801. .stds = &standard[0],
  802. .num_stds = ARRAY_SIZE(standard),
  803. .std_callback = &std_callback,
  804. .vbi_fops.open = av7110_vbi_reset,
  805. .vbi_fops.release = av7110_vbi_reset,
  806. .vbi_fops.write = av7110_vbi_write,
  807. };