av7110_v4l.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966
  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 = 1,
  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, const 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, const 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 input ? -EINVAL : 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_enumaudio(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. *a = msp3400_v4l2_audio;
  435. return 0;
  436. }
  437. static int vidioc_g_audio(struct file *file, void *fh, struct v4l2_audio *a)
  438. {
  439. struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
  440. struct av7110 *av7110 = (struct av7110 *)dev->ext_priv;
  441. dprintk(2, "VIDIOC_G_AUDIO: %d\n", a->index);
  442. if (a->index != 0)
  443. return -EINVAL;
  444. if (av7110->current_input >= 2)
  445. return -EINVAL;
  446. *a = msp3400_v4l2_audio;
  447. return 0;
  448. }
  449. static int vidioc_s_audio(struct file *file, void *fh, const struct v4l2_audio *a)
  450. {
  451. struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
  452. struct av7110 *av7110 = (struct av7110 *)dev->ext_priv;
  453. dprintk(2, "VIDIOC_S_AUDIO: %d\n", a->index);
  454. if (av7110->current_input >= 2)
  455. return -EINVAL;
  456. return a->index ? -EINVAL : 0;
  457. }
  458. static int vidioc_g_sliced_vbi_cap(struct file *file, void *fh,
  459. struct v4l2_sliced_vbi_cap *cap)
  460. {
  461. struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
  462. struct av7110 *av7110 = (struct av7110 *)dev->ext_priv;
  463. dprintk(2, "VIDIOC_G_SLICED_VBI_CAP\n");
  464. if (cap->type != V4L2_BUF_TYPE_SLICED_VBI_OUTPUT)
  465. return -EINVAL;
  466. if (FW_VERSION(av7110->arm_app) >= 0x2623) {
  467. cap->service_set = V4L2_SLICED_WSS_625;
  468. cap->service_lines[0][23] = V4L2_SLICED_WSS_625;
  469. }
  470. return 0;
  471. }
  472. static int vidioc_g_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_G_FMT:\n");
  478. if (FW_VERSION(av7110->arm_app) < 0x2623)
  479. return -EINVAL;
  480. memset(&f->fmt.sliced, 0, sizeof f->fmt.sliced);
  481. if (av7110->wssMode) {
  482. f->fmt.sliced.service_set = V4L2_SLICED_WSS_625;
  483. f->fmt.sliced.service_lines[0][23] = V4L2_SLICED_WSS_625;
  484. f->fmt.sliced.io_size = sizeof(struct v4l2_sliced_vbi_data);
  485. }
  486. return 0;
  487. }
  488. static int vidioc_s_fmt_sliced_vbi_out(struct file *file, void *fh,
  489. struct v4l2_format *f)
  490. {
  491. struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
  492. struct av7110 *av7110 = (struct av7110 *)dev->ext_priv;
  493. dprintk(2, "VIDIOC_S_FMT\n");
  494. if (FW_VERSION(av7110->arm_app) < 0x2623)
  495. return -EINVAL;
  496. if (f->fmt.sliced.service_set != V4L2_SLICED_WSS_625 &&
  497. f->fmt.sliced.service_lines[0][23] != V4L2_SLICED_WSS_625) {
  498. memset(&f->fmt.sliced, 0, sizeof(f->fmt.sliced));
  499. /* WSS controlled by firmware */
  500. av7110->wssMode = 0;
  501. av7110->wssData = 0;
  502. return av7110_fw_cmd(av7110, COMTYPE_ENCODER,
  503. SetWSSConfig, 1, 0);
  504. } else {
  505. memset(&f->fmt.sliced, 0, sizeof(f->fmt.sliced));
  506. f->fmt.sliced.service_set = V4L2_SLICED_WSS_625;
  507. f->fmt.sliced.service_lines[0][23] = V4L2_SLICED_WSS_625;
  508. f->fmt.sliced.io_size = sizeof(struct v4l2_sliced_vbi_data);
  509. /* WSS controlled by userspace */
  510. av7110->wssMode = 1;
  511. av7110->wssData = 0;
  512. }
  513. return 0;
  514. }
  515. static int av7110_vbi_reset(struct file *file)
  516. {
  517. struct saa7146_fh *fh = file->private_data;
  518. struct saa7146_dev *dev = fh->dev;
  519. struct av7110 *av7110 = (struct av7110*) dev->ext_priv;
  520. dprintk(2, "%s\n", __func__);
  521. av7110->wssMode = 0;
  522. av7110->wssData = 0;
  523. if (FW_VERSION(av7110->arm_app) < 0x2623)
  524. return 0;
  525. else
  526. return av7110_fw_cmd(av7110, COMTYPE_ENCODER, SetWSSConfig, 1, 0);
  527. }
  528. static ssize_t av7110_vbi_write(struct file *file, const char __user *data, size_t count, loff_t *ppos)
  529. {
  530. struct saa7146_fh *fh = file->private_data;
  531. struct saa7146_dev *dev = fh->dev;
  532. struct av7110 *av7110 = (struct av7110*) dev->ext_priv;
  533. struct v4l2_sliced_vbi_data d;
  534. int rc;
  535. dprintk(2, "%s\n", __func__);
  536. if (FW_VERSION(av7110->arm_app) < 0x2623 || !av7110->wssMode || count != sizeof d)
  537. return -EINVAL;
  538. if (copy_from_user(&d, data, count))
  539. return -EFAULT;
  540. if ((d.id != 0 && d.id != V4L2_SLICED_WSS_625) || d.field != 0 || d.line != 23)
  541. return -EINVAL;
  542. if (d.id)
  543. av7110->wssData = ((d.data[1] << 8) & 0x3f00) | d.data[0];
  544. else
  545. av7110->wssData = 0x8000;
  546. rc = av7110_fw_cmd(av7110, COMTYPE_ENCODER, SetWSSConfig, 2, 1, av7110->wssData);
  547. return (rc < 0) ? rc : count;
  548. }
  549. /****************************************************************************
  550. * INITIALIZATION
  551. ****************************************************************************/
  552. static u8 saa7113_init_regs[] = {
  553. 0x02, 0xd0,
  554. 0x03, 0x23,
  555. 0x04, 0x00,
  556. 0x05, 0x00,
  557. 0x06, 0xe9,
  558. 0x07, 0x0d,
  559. 0x08, 0x98,
  560. 0x09, 0x02,
  561. 0x0a, 0x80,
  562. 0x0b, 0x40,
  563. 0x0c, 0x40,
  564. 0x0d, 0x00,
  565. 0x0e, 0x01,
  566. 0x0f, 0x7c,
  567. 0x10, 0x48,
  568. 0x11, 0x0c,
  569. 0x12, 0x8b,
  570. 0x13, 0x1a,
  571. 0x14, 0x00,
  572. 0x15, 0x00,
  573. 0x16, 0x00,
  574. 0x17, 0x00,
  575. 0x18, 0x00,
  576. 0x19, 0x00,
  577. 0x1a, 0x00,
  578. 0x1b, 0x00,
  579. 0x1c, 0x00,
  580. 0x1d, 0x00,
  581. 0x1e, 0x00,
  582. 0x41, 0x77,
  583. 0x42, 0x77,
  584. 0x43, 0x77,
  585. 0x44, 0x77,
  586. 0x45, 0x77,
  587. 0x46, 0x77,
  588. 0x47, 0x77,
  589. 0x48, 0x77,
  590. 0x49, 0x77,
  591. 0x4a, 0x77,
  592. 0x4b, 0x77,
  593. 0x4c, 0x77,
  594. 0x4d, 0x77,
  595. 0x4e, 0x77,
  596. 0x4f, 0x77,
  597. 0x50, 0x77,
  598. 0x51, 0x77,
  599. 0x52, 0x77,
  600. 0x53, 0x77,
  601. 0x54, 0x77,
  602. 0x55, 0x77,
  603. 0x56, 0x77,
  604. 0x57, 0xff,
  605. 0xff
  606. };
  607. static struct saa7146_ext_vv av7110_vv_data_st;
  608. static struct saa7146_ext_vv av7110_vv_data_c;
  609. int av7110_init_analog_module(struct av7110 *av7110)
  610. {
  611. u16 version1, version2;
  612. if (i2c_writereg(av7110, 0x80, 0x0, 0x80) == 1 &&
  613. i2c_writereg(av7110, 0x80, 0x0, 0) == 1) {
  614. pr_info("DVB-C analog module @ card %d detected, initializing MSP3400\n",
  615. av7110->dvb_adapter.num);
  616. av7110->adac_type = DVB_ADAC_MSP34x0;
  617. } else if (i2c_writereg(av7110, 0x84, 0x0, 0x80) == 1 &&
  618. i2c_writereg(av7110, 0x84, 0x0, 0) == 1) {
  619. pr_info("DVB-C analog module @ card %d detected, initializing MSP3415\n",
  620. av7110->dvb_adapter.num);
  621. av7110->adac_type = DVB_ADAC_MSP34x5;
  622. } else
  623. return -ENODEV;
  624. msleep(100); // the probing above resets the msp...
  625. msp_readreg(av7110, MSP_RD_DSP, 0x001e, &version1);
  626. msp_readreg(av7110, MSP_RD_DSP, 0x001f, &version2);
  627. dprintk(1, "dvb-ttpci: @ card %d MSP34xx version 0x%04x 0x%04x\n",
  628. av7110->dvb_adapter.num, version1, version2);
  629. msp_writereg(av7110, MSP_WR_DSP, 0x0013, 0x0c00);
  630. msp_writereg(av7110, MSP_WR_DSP, 0x0000, 0x7f00); // loudspeaker + headphone
  631. msp_writereg(av7110, MSP_WR_DSP, 0x0008, 0x0220); // loudspeaker source
  632. msp_writereg(av7110, MSP_WR_DSP, 0x0009, 0x0220); // headphone source
  633. msp_writereg(av7110, MSP_WR_DSP, 0x0004, 0x7f00); // loudspeaker volume
  634. msp_writereg(av7110, MSP_WR_DSP, 0x000a, 0x0220); // SCART 1 source
  635. msp_writereg(av7110, MSP_WR_DSP, 0x0007, 0x7f00); // SCART 1 volume
  636. msp_writereg(av7110, MSP_WR_DSP, 0x000d, 0x1900); // prescale SCART
  637. if (i2c_writereg(av7110, 0x48, 0x01, 0x00)!=1) {
  638. pr_info("saa7113 not accessible\n");
  639. } else {
  640. u8 *i = saa7113_init_regs;
  641. if ((av7110->dev->pci->subsystem_vendor == 0x110a) && (av7110->dev->pci->subsystem_device == 0x0000)) {
  642. /* Fujitsu/Siemens DVB-Cable */
  643. av7110->analog_tuner_flags |= ANALOG_TUNER_VES1820;
  644. } else if ((av7110->dev->pci->subsystem_vendor == 0x13c2) && (av7110->dev->pci->subsystem_device == 0x0002)) {
  645. /* Hauppauge/TT DVB-C premium */
  646. av7110->analog_tuner_flags |= ANALOG_TUNER_VES1820;
  647. } else if ((av7110->dev->pci->subsystem_vendor == 0x13c2) && (av7110->dev->pci->subsystem_device == 0x000A)) {
  648. /* Hauppauge/TT DVB-C premium */
  649. av7110->analog_tuner_flags |= ANALOG_TUNER_STV0297;
  650. }
  651. /* setup for DVB by default */
  652. if (av7110->analog_tuner_flags & ANALOG_TUNER_VES1820) {
  653. if (ves1820_writereg(av7110->dev, 0x09, 0x0f, 0x20))
  654. dprintk(1, "setting band in demodulator failed\n");
  655. } else if (av7110->analog_tuner_flags & ANALOG_TUNER_STV0297) {
  656. saa7146_setgpio(av7110->dev, 1, SAA7146_GPIO_OUTLO); // TDA9819 pin9(STD)
  657. saa7146_setgpio(av7110->dev, 3, SAA7146_GPIO_OUTLO); // TDA9819 pin30(VIF)
  658. }
  659. /* init the saa7113 */
  660. while (*i != 0xff) {
  661. if (i2c_writereg(av7110, 0x48, i[0], i[1]) != 1) {
  662. dprintk(1, "saa7113 initialization failed @ card %d", av7110->dvb_adapter.num);
  663. break;
  664. }
  665. i += 2;
  666. }
  667. /* setup msp for analog sound: B/G Dual-FM */
  668. msp_writereg(av7110, MSP_WR_DEM, 0x00bb, 0x02d0); // AD_CV
  669. msp_writereg(av7110, MSP_WR_DEM, 0x0001, 3); // FIR1
  670. msp_writereg(av7110, MSP_WR_DEM, 0x0001, 18); // FIR1
  671. msp_writereg(av7110, MSP_WR_DEM, 0x0001, 27); // FIR1
  672. msp_writereg(av7110, MSP_WR_DEM, 0x0001, 48); // FIR1
  673. msp_writereg(av7110, MSP_WR_DEM, 0x0001, 66); // FIR1
  674. msp_writereg(av7110, MSP_WR_DEM, 0x0001, 72); // FIR1
  675. msp_writereg(av7110, MSP_WR_DEM, 0x0005, 4); // FIR2
  676. msp_writereg(av7110, MSP_WR_DEM, 0x0005, 64); // FIR2
  677. msp_writereg(av7110, MSP_WR_DEM, 0x0005, 0); // FIR2
  678. msp_writereg(av7110, MSP_WR_DEM, 0x0005, 3); // FIR2
  679. msp_writereg(av7110, MSP_WR_DEM, 0x0005, 18); // FIR2
  680. msp_writereg(av7110, MSP_WR_DEM, 0x0005, 27); // FIR2
  681. msp_writereg(av7110, MSP_WR_DEM, 0x0005, 48); // FIR2
  682. msp_writereg(av7110, MSP_WR_DEM, 0x0005, 66); // FIR2
  683. msp_writereg(av7110, MSP_WR_DEM, 0x0005, 72); // FIR2
  684. msp_writereg(av7110, MSP_WR_DEM, 0x0083, 0xa000); // MODE_REG
  685. msp_writereg(av7110, MSP_WR_DEM, 0x0093, 0x00aa); // DCO1_LO 5.74MHz
  686. msp_writereg(av7110, MSP_WR_DEM, 0x009b, 0x04fc); // DCO1_HI
  687. msp_writereg(av7110, MSP_WR_DEM, 0x00a3, 0x038e); // DCO2_LO 5.5MHz
  688. msp_writereg(av7110, MSP_WR_DEM, 0x00ab, 0x04c6); // DCO2_HI
  689. msp_writereg(av7110, MSP_WR_DEM, 0x0056, 0); // LOAD_REG 1/2
  690. }
  691. memcpy(standard, dvb_standard, sizeof(struct saa7146_standard) * 2);
  692. /* set dd1 stream a & b */
  693. saa7146_write(av7110->dev, DD1_STREAM_B, 0x00000000);
  694. saa7146_write(av7110->dev, DD1_INIT, 0x03000700);
  695. saa7146_write(av7110->dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26));
  696. return 0;
  697. }
  698. int av7110_init_v4l(struct av7110 *av7110)
  699. {
  700. struct saa7146_dev* dev = av7110->dev;
  701. struct saa7146_ext_vv *vv_data;
  702. int ret;
  703. /* special case DVB-C: these cards have an analog tuner
  704. plus need some special handling, so we have separate
  705. saa7146_ext_vv data for these... */
  706. if (av7110->analog_tuner_flags)
  707. vv_data = &av7110_vv_data_c;
  708. else
  709. vv_data = &av7110_vv_data_st;
  710. ret = saa7146_vv_init(dev, vv_data);
  711. if (ret) {
  712. ERR("cannot init capture device. skipping\n");
  713. return -ENODEV;
  714. }
  715. vv_data->vid_ops.vidioc_enum_input = vidioc_enum_input;
  716. vv_data->vid_ops.vidioc_g_input = vidioc_g_input;
  717. vv_data->vid_ops.vidioc_s_input = vidioc_s_input;
  718. vv_data->vid_ops.vidioc_g_tuner = vidioc_g_tuner;
  719. vv_data->vid_ops.vidioc_s_tuner = vidioc_s_tuner;
  720. vv_data->vid_ops.vidioc_g_frequency = vidioc_g_frequency;
  721. vv_data->vid_ops.vidioc_s_frequency = vidioc_s_frequency;
  722. vv_data->vid_ops.vidioc_enumaudio = vidioc_enumaudio;
  723. vv_data->vid_ops.vidioc_g_audio = vidioc_g_audio;
  724. vv_data->vid_ops.vidioc_s_audio = vidioc_s_audio;
  725. vv_data->vid_ops.vidioc_g_fmt_vbi_cap = NULL;
  726. vv_data->vbi_ops.vidioc_g_tuner = vidioc_g_tuner;
  727. vv_data->vbi_ops.vidioc_s_tuner = vidioc_s_tuner;
  728. vv_data->vbi_ops.vidioc_g_frequency = vidioc_g_frequency;
  729. vv_data->vbi_ops.vidioc_s_frequency = vidioc_s_frequency;
  730. vv_data->vbi_ops.vidioc_g_fmt_vbi_cap = NULL;
  731. vv_data->vbi_ops.vidioc_g_sliced_vbi_cap = vidioc_g_sliced_vbi_cap;
  732. vv_data->vbi_ops.vidioc_g_fmt_sliced_vbi_out = vidioc_g_fmt_sliced_vbi_out;
  733. vv_data->vbi_ops.vidioc_s_fmt_sliced_vbi_out = vidioc_s_fmt_sliced_vbi_out;
  734. if (FW_VERSION(av7110->arm_app) < 0x2623)
  735. vv_data->capabilities &= ~V4L2_CAP_SLICED_VBI_OUTPUT;
  736. if (saa7146_register_device(&av7110->v4l_dev, dev, "av7110", VFL_TYPE_GRABBER)) {
  737. ERR("cannot register capture device. skipping\n");
  738. saa7146_vv_release(dev);
  739. return -ENODEV;
  740. }
  741. if (FW_VERSION(av7110->arm_app) >= 0x2623) {
  742. if (saa7146_register_device(&av7110->vbi_dev, dev, "av7110", VFL_TYPE_VBI))
  743. ERR("cannot register vbi v4l2 device. skipping\n");
  744. }
  745. return 0;
  746. }
  747. int av7110_exit_v4l(struct av7110 *av7110)
  748. {
  749. struct saa7146_dev* dev = av7110->dev;
  750. saa7146_unregister_device(&av7110->v4l_dev, av7110->dev);
  751. saa7146_unregister_device(&av7110->vbi_dev, av7110->dev);
  752. saa7146_vv_release(dev);
  753. return 0;
  754. }
  755. /* FIXME: these values are experimental values that look better than the
  756. values from the latest "official" driver -- at least for me... (MiHu) */
  757. static struct saa7146_standard standard[] = {
  758. {
  759. .name = "PAL", .id = V4L2_STD_PAL_BG,
  760. .v_offset = 0x15, .v_field = 288,
  761. .h_offset = 0x48, .h_pixels = 708,
  762. .v_max_out = 576, .h_max_out = 768,
  763. }, {
  764. .name = "NTSC", .id = V4L2_STD_NTSC,
  765. .v_offset = 0x10, .v_field = 244,
  766. .h_offset = 0x40, .h_pixels = 708,
  767. .v_max_out = 480, .h_max_out = 640,
  768. }
  769. };
  770. static struct saa7146_standard analog_standard[] = {
  771. {
  772. .name = "PAL", .id = V4L2_STD_PAL_BG,
  773. .v_offset = 0x1b, .v_field = 288,
  774. .h_offset = 0x08, .h_pixels = 708,
  775. .v_max_out = 576, .h_max_out = 768,
  776. }, {
  777. .name = "NTSC", .id = V4L2_STD_NTSC,
  778. .v_offset = 0x10, .v_field = 244,
  779. .h_offset = 0x40, .h_pixels = 708,
  780. .v_max_out = 480, .h_max_out = 640,
  781. }
  782. };
  783. static struct saa7146_standard dvb_standard[] = {
  784. {
  785. .name = "PAL", .id = V4L2_STD_PAL_BG,
  786. .v_offset = 0x14, .v_field = 288,
  787. .h_offset = 0x48, .h_pixels = 708,
  788. .v_max_out = 576, .h_max_out = 768,
  789. }, {
  790. .name = "NTSC", .id = V4L2_STD_NTSC,
  791. .v_offset = 0x10, .v_field = 244,
  792. .h_offset = 0x40, .h_pixels = 708,
  793. .v_max_out = 480, .h_max_out = 640,
  794. }
  795. };
  796. static int std_callback(struct saa7146_dev* dev, struct saa7146_standard *std)
  797. {
  798. struct av7110 *av7110 = (struct av7110*) dev->ext_priv;
  799. if (std->id & V4L2_STD_PAL) {
  800. av7110->vidmode = AV7110_VIDEO_MODE_PAL;
  801. av7110_set_vidmode(av7110, av7110->vidmode);
  802. }
  803. else if (std->id & V4L2_STD_NTSC) {
  804. av7110->vidmode = AV7110_VIDEO_MODE_NTSC;
  805. av7110_set_vidmode(av7110, av7110->vidmode);
  806. }
  807. else
  808. return -1;
  809. return 0;
  810. }
  811. static struct saa7146_ext_vv av7110_vv_data_st = {
  812. .inputs = 1,
  813. .audios = 1,
  814. .capabilities = V4L2_CAP_SLICED_VBI_OUTPUT | V4L2_CAP_AUDIO,
  815. .flags = 0,
  816. .stds = &standard[0],
  817. .num_stds = ARRAY_SIZE(standard),
  818. .std_callback = &std_callback,
  819. .vbi_fops.open = av7110_vbi_reset,
  820. .vbi_fops.release = av7110_vbi_reset,
  821. .vbi_fops.write = av7110_vbi_write,
  822. };
  823. static struct saa7146_ext_vv av7110_vv_data_c = {
  824. .inputs = 1,
  825. .audios = 1,
  826. .capabilities = V4L2_CAP_TUNER | V4L2_CAP_SLICED_VBI_OUTPUT | V4L2_CAP_AUDIO,
  827. .flags = SAA7146_USE_PORT_B_FOR_VBI,
  828. .stds = &standard[0],
  829. .num_stds = ARRAY_SIZE(standard),
  830. .std_callback = &std_callback,
  831. .vbi_fops.open = av7110_vbi_reset,
  832. .vbi_fops.release = av7110_vbi_reset,
  833. .vbi_fops.write = av7110_vbi_write,
  834. };