saa7134-tvaudio.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088
  1. /*
  2. *
  3. * device driver for philips saa7134 based TV cards
  4. * tv audio decoder (fm stereo, nicam, ...)
  5. *
  6. * (c) 2001-03 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #include <linux/init.h>
  23. #include <linux/list.h>
  24. #include <linux/module.h>
  25. #include <linux/kernel.h>
  26. #include <linux/kthread.h>
  27. #include <linux/delay.h>
  28. #include <linux/freezer.h>
  29. #include <asm/div64.h>
  30. #include "saa7134-reg.h"
  31. #include "saa7134.h"
  32. /* ------------------------------------------------------------------ */
  33. static unsigned int audio_debug;
  34. module_param(audio_debug, int, 0644);
  35. MODULE_PARM_DESC(audio_debug,"enable debug messages [tv audio]");
  36. static unsigned int audio_ddep;
  37. module_param(audio_ddep, int, 0644);
  38. MODULE_PARM_DESC(audio_ddep,"audio ddep overwrite");
  39. static int audio_clock_override = UNSET;
  40. module_param(audio_clock_override, int, 0644);
  41. static int audio_clock_tweak;
  42. module_param(audio_clock_tweak, int, 0644);
  43. MODULE_PARM_DESC(audio_clock_tweak, "Audio clock tick fine tuning for cards with audio crystal that's slightly off (range [-1024 .. 1024])");
  44. #define dprintk(fmt, arg...) if (audio_debug) \
  45. printk(KERN_DEBUG "%s/audio: " fmt, dev->name , ## arg)
  46. #define d2printk(fmt, arg...) if (audio_debug > 1) \
  47. printk(KERN_DEBUG "%s/audio: " fmt, dev->name, ## arg)
  48. #define print_regb(reg) printk("%s: reg 0x%03x [%-16s]: 0x%02x\n", \
  49. dev->name,(SAA7134_##reg),(#reg),saa_readb((SAA7134_##reg)))
  50. /* msecs */
  51. #define SCAN_INITIAL_DELAY 1000
  52. #define SCAN_SAMPLE_DELAY 200
  53. #define SCAN_SUBCARRIER_DELAY 2000
  54. /* ------------------------------------------------------------------ */
  55. /* saa7134 code */
  56. static struct mainscan {
  57. char *name;
  58. v4l2_std_id std;
  59. int carr;
  60. } mainscan[] = {
  61. {
  62. .name = "MN",
  63. .std = V4L2_STD_MN,
  64. .carr = 4500,
  65. },{
  66. .name = "BGH",
  67. .std = V4L2_STD_B | V4L2_STD_GH,
  68. .carr = 5500,
  69. },{
  70. .name = "I",
  71. .std = V4L2_STD_PAL_I,
  72. .carr = 6000,
  73. },{
  74. .name = "DKL",
  75. .std = V4L2_STD_DK | V4L2_STD_SECAM_L | V4L2_STD_SECAM_LC,
  76. .carr = 6500,
  77. }
  78. };
  79. static struct saa7134_tvaudio tvaudio[] = {
  80. {
  81. .name = "PAL-B/G FM-stereo",
  82. .std = V4L2_STD_PAL_BG,
  83. .mode = TVAUDIO_FM_BG_STEREO,
  84. .carr1 = 5500,
  85. .carr2 = 5742,
  86. },{
  87. .name = "PAL-D/K1 FM-stereo",
  88. .std = V4L2_STD_PAL_DK,
  89. .carr1 = 6500,
  90. .carr2 = 6258,
  91. .mode = TVAUDIO_FM_BG_STEREO,
  92. },{
  93. .name = "PAL-D/K2 FM-stereo",
  94. .std = V4L2_STD_PAL_DK,
  95. .carr1 = 6500,
  96. .carr2 = 6742,
  97. .mode = TVAUDIO_FM_BG_STEREO,
  98. },{
  99. .name = "PAL-D/K3 FM-stereo",
  100. .std = V4L2_STD_PAL_DK,
  101. .carr1 = 6500,
  102. .carr2 = 5742,
  103. .mode = TVAUDIO_FM_BG_STEREO,
  104. },{
  105. .name = "PAL-B/G NICAM",
  106. .std = V4L2_STD_PAL_BG,
  107. .carr1 = 5500,
  108. .carr2 = 5850,
  109. .mode = TVAUDIO_NICAM_FM,
  110. },{
  111. .name = "PAL-I NICAM",
  112. .std = V4L2_STD_PAL_I,
  113. .carr1 = 6000,
  114. .carr2 = 6552,
  115. .mode = TVAUDIO_NICAM_FM,
  116. },{
  117. .name = "PAL-D/K NICAM",
  118. .std = V4L2_STD_PAL_DK,
  119. .carr1 = 6500,
  120. .carr2 = 5850,
  121. .mode = TVAUDIO_NICAM_FM,
  122. },{
  123. .name = "SECAM-L NICAM",
  124. .std = V4L2_STD_SECAM_L,
  125. .carr1 = 6500,
  126. .carr2 = 5850,
  127. .mode = TVAUDIO_NICAM_AM,
  128. },{
  129. .name = "SECAM-D/K NICAM",
  130. .std = V4L2_STD_SECAM_DK,
  131. .carr1 = 6500,
  132. .carr2 = 5850,
  133. .mode = TVAUDIO_NICAM_FM,
  134. },{
  135. .name = "NTSC-A2 FM-stereo",
  136. .std = V4L2_STD_NTSC,
  137. .carr1 = 4500,
  138. .carr2 = 4724,
  139. .mode = TVAUDIO_FM_K_STEREO,
  140. },{
  141. .name = "NTSC-M",
  142. .std = V4L2_STD_NTSC,
  143. .carr1 = 4500,
  144. .carr2 = -1,
  145. .mode = TVAUDIO_FM_MONO,
  146. }
  147. };
  148. #define TVAUDIO ARRAY_SIZE(tvaudio)
  149. /* ------------------------------------------------------------------ */
  150. static u32 tvaudio_carr2reg(u32 carrier)
  151. {
  152. u64 a = carrier;
  153. a <<= 24;
  154. do_div(a,12288);
  155. return a;
  156. }
  157. static void tvaudio_setcarrier(struct saa7134_dev *dev,
  158. int primary, int secondary)
  159. {
  160. if (-1 == secondary)
  161. secondary = primary;
  162. saa_writel(SAA7134_CARRIER1_FREQ0 >> 2, tvaudio_carr2reg(primary));
  163. saa_writel(SAA7134_CARRIER2_FREQ0 >> 2, tvaudio_carr2reg(secondary));
  164. }
  165. #define SAA7134_MUTE_MASK 0xbb
  166. #define SAA7134_MUTE_ANALOG 0x04
  167. #define SAA7134_MUTE_I2S 0x40
  168. static void mute_input_7134(struct saa7134_dev *dev)
  169. {
  170. unsigned int mute;
  171. struct saa7134_input *in;
  172. int ausel=0, ics=0, ocs=0;
  173. int mask;
  174. /* look what is to do ... */
  175. in = dev->input;
  176. mute = (dev->ctl_mute ||
  177. (dev->automute && (&card(dev).radio) != in));
  178. if (card(dev).mute.name) {
  179. /*
  180. * 7130 - we'll mute using some unconnected audio input
  181. * 7134 - we'll probably should switch external mux with gpio
  182. */
  183. if (mute)
  184. in = &card(dev).mute;
  185. }
  186. if (dev->hw_mute == mute &&
  187. dev->hw_input == in && !dev->insuspend) {
  188. dprintk("mute/input: nothing to do [mute=%d,input=%s]\n",
  189. mute,in->name);
  190. return;
  191. }
  192. dprintk("ctl_mute=%d automute=%d input=%s => mute=%d input=%s\n",
  193. dev->ctl_mute,dev->automute,dev->input->name,mute,in->name);
  194. dev->hw_mute = mute;
  195. dev->hw_input = in;
  196. if (PCI_DEVICE_ID_PHILIPS_SAA7134 == dev->pci->device)
  197. /* 7134 mute */
  198. saa_writeb(SAA7134_AUDIO_MUTE_CTRL, mute ?
  199. SAA7134_MUTE_MASK |
  200. SAA7134_MUTE_ANALOG |
  201. SAA7134_MUTE_I2S :
  202. SAA7134_MUTE_MASK);
  203. /* switch internal audio mux */
  204. switch (in->amux) {
  205. case TV: ausel=0xc0; ics=0x00; ocs=0x02; break;
  206. case LINE1: ausel=0x80; ics=0x00; ocs=0x00; break;
  207. case LINE2: ausel=0x80; ics=0x08; ocs=0x01; break;
  208. case LINE2_LEFT: ausel=0x80; ics=0x08; ocs=0x05; break;
  209. }
  210. saa_andorb(SAA7134_AUDIO_FORMAT_CTRL, 0xc0, ausel);
  211. saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x08, ics);
  212. saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x07, ocs);
  213. // for oss, we need to change the clock configuration
  214. if (in->amux == TV)
  215. saa_andorb(SAA7134_SIF_SAMPLE_FREQ, 0x03, 0x00);
  216. else
  217. saa_andorb(SAA7134_SIF_SAMPLE_FREQ, 0x03, 0x01);
  218. /* switch gpio-connected external audio mux */
  219. if (0 == card(dev).gpiomask)
  220. return;
  221. mask = card(dev).gpiomask;
  222. saa_andorl(SAA7134_GPIO_GPMODE0 >> 2, mask, mask);
  223. saa_andorl(SAA7134_GPIO_GPSTATUS0 >> 2, mask, in->gpio);
  224. saa7134_track_gpio(dev,in->name);
  225. }
  226. static void tvaudio_setmode(struct saa7134_dev *dev,
  227. struct saa7134_tvaudio *audio,
  228. char *note)
  229. {
  230. int acpf, tweak = 0;
  231. if (dev->tvnorm->id == V4L2_STD_NTSC) {
  232. acpf = 0x19066;
  233. } else {
  234. acpf = 0x1e000;
  235. }
  236. if (audio_clock_tweak > -1024 && audio_clock_tweak < 1024)
  237. tweak = audio_clock_tweak;
  238. if (note)
  239. dprintk("tvaudio_setmode: %s %s [%d.%03d/%d.%03d MHz] acpf=%d%+d\n",
  240. note,audio->name,
  241. audio->carr1 / 1000, audio->carr1 % 1000,
  242. audio->carr2 / 1000, audio->carr2 % 1000,
  243. acpf, tweak);
  244. acpf += tweak;
  245. saa_writeb(SAA7134_AUDIO_CLOCKS_PER_FIELD0, (acpf & 0x0000ff) >> 0);
  246. saa_writeb(SAA7134_AUDIO_CLOCKS_PER_FIELD1, (acpf & 0x00ff00) >> 8);
  247. saa_writeb(SAA7134_AUDIO_CLOCKS_PER_FIELD2, (acpf & 0x030000) >> 16);
  248. tvaudio_setcarrier(dev,audio->carr1,audio->carr2);
  249. switch (audio->mode) {
  250. case TVAUDIO_FM_MONO:
  251. case TVAUDIO_FM_BG_STEREO:
  252. saa_writeb(SAA7134_DEMODULATOR, 0x00);
  253. saa_writeb(SAA7134_DCXO_IDENT_CTRL, 0x00);
  254. saa_writeb(SAA7134_FM_DEEMPHASIS, 0x22);
  255. saa_writeb(SAA7134_FM_DEMATRIX, 0x80);
  256. saa_writeb(SAA7134_STEREO_DAC_OUTPUT_SELECT, 0xa0);
  257. break;
  258. case TVAUDIO_FM_K_STEREO:
  259. saa_writeb(SAA7134_DEMODULATOR, 0x00);
  260. saa_writeb(SAA7134_DCXO_IDENT_CTRL, 0x01);
  261. saa_writeb(SAA7134_FM_DEEMPHASIS, 0x22);
  262. saa_writeb(SAA7134_FM_DEMATRIX, 0x80);
  263. saa_writeb(SAA7134_STEREO_DAC_OUTPUT_SELECT, 0xa0);
  264. break;
  265. case TVAUDIO_NICAM_FM:
  266. saa_writeb(SAA7134_DEMODULATOR, 0x10);
  267. saa_writeb(SAA7134_DCXO_IDENT_CTRL, 0x00);
  268. saa_writeb(SAA7134_FM_DEEMPHASIS, 0x44);
  269. saa_writeb(SAA7134_STEREO_DAC_OUTPUT_SELECT, 0xa1);
  270. saa_writeb(SAA7134_NICAM_CONFIG, 0x00);
  271. break;
  272. case TVAUDIO_NICAM_AM:
  273. saa_writeb(SAA7134_DEMODULATOR, 0x12);
  274. saa_writeb(SAA7134_DCXO_IDENT_CTRL, 0x00);
  275. saa_writeb(SAA7134_FM_DEEMPHASIS, 0x44);
  276. saa_writeb(SAA7134_STEREO_DAC_OUTPUT_SELECT, 0xa1);
  277. saa_writeb(SAA7134_NICAM_CONFIG, 0x00);
  278. break;
  279. case TVAUDIO_FM_SAT_STEREO:
  280. /* not implemented (yet) */
  281. break;
  282. }
  283. }
  284. static int tvaudio_sleep(struct saa7134_dev *dev, int timeout)
  285. {
  286. if (dev->thread.scan1 == dev->thread.scan2 &&
  287. !kthread_should_stop()) {
  288. if (timeout < 0) {
  289. set_current_state(TASK_INTERRUPTIBLE);
  290. schedule();
  291. } else {
  292. schedule_timeout_interruptible
  293. (msecs_to_jiffies(timeout));
  294. }
  295. }
  296. return dev->thread.scan1 != dev->thread.scan2;
  297. }
  298. static int tvaudio_checkcarrier(struct saa7134_dev *dev, struct mainscan *scan)
  299. {
  300. __s32 left,right,value;
  301. if (!(dev->tvnorm->id & scan->std)) {
  302. value = 0;
  303. dprintk("skipping %d.%03d MHz [%4s]\n",
  304. scan->carr / 1000, scan->carr % 1000, scan->name);
  305. return 0;
  306. }
  307. if (audio_debug > 1) {
  308. int i;
  309. dprintk("debug %d:",scan->carr);
  310. for (i = -150; i <= 150; i += 30) {
  311. tvaudio_setcarrier(dev,scan->carr+i,scan->carr+i);
  312. saa_readl(SAA7134_LEVEL_READOUT1 >> 2);
  313. if (tvaudio_sleep(dev,SCAN_SAMPLE_DELAY))
  314. return -1;
  315. value = saa_readl(SAA7134_LEVEL_READOUT1 >> 2);
  316. if (0 == i)
  317. printk(" # %6d # ",value >> 16);
  318. else
  319. printk(" %6d",value >> 16);
  320. }
  321. printk("\n");
  322. }
  323. tvaudio_setcarrier(dev,scan->carr-90,scan->carr-90);
  324. saa_readl(SAA7134_LEVEL_READOUT1 >> 2);
  325. if (tvaudio_sleep(dev,SCAN_SAMPLE_DELAY))
  326. return -1;
  327. left = saa_readl(SAA7134_LEVEL_READOUT1 >> 2);
  328. tvaudio_setcarrier(dev,scan->carr+90,scan->carr+90);
  329. saa_readl(SAA7134_LEVEL_READOUT1 >> 2);
  330. if (tvaudio_sleep(dev,SCAN_SAMPLE_DELAY))
  331. return -1;
  332. right = saa_readl(SAA7134_LEVEL_READOUT1 >> 2);
  333. left >>= 16;
  334. right >>= 16;
  335. value = left > right ? left - right : right - left;
  336. dprintk("scanning %d.%03d MHz [%4s] => dc is %5d [%d/%d]\n",
  337. scan->carr / 1000, scan->carr % 1000,
  338. scan->name, value, left, right);
  339. return value;
  340. }
  341. static int tvaudio_getstereo(struct saa7134_dev *dev, struct saa7134_tvaudio *audio)
  342. {
  343. __u32 idp, nicam, nicam_status;
  344. int retval = -1;
  345. switch (audio->mode) {
  346. case TVAUDIO_FM_MONO:
  347. return V4L2_TUNER_SUB_MONO;
  348. case TVAUDIO_FM_K_STEREO:
  349. case TVAUDIO_FM_BG_STEREO:
  350. idp = (saa_readb(SAA7134_IDENT_SIF) & 0xe0) >> 5;
  351. dprintk("getstereo: fm/stereo: idp=0x%x\n",idp);
  352. if (0x03 == (idp & 0x03))
  353. retval = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
  354. else if (0x05 == (idp & 0x05))
  355. retval = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
  356. else if (0x01 == (idp & 0x01))
  357. retval = V4L2_TUNER_SUB_MONO;
  358. break;
  359. case TVAUDIO_FM_SAT_STEREO:
  360. /* not implemented (yet) */
  361. break;
  362. case TVAUDIO_NICAM_FM:
  363. case TVAUDIO_NICAM_AM:
  364. nicam = saa_readb(SAA7134_AUDIO_STATUS);
  365. dprintk("getstereo: nicam=0x%x\n",nicam);
  366. if (nicam & 0x1) {
  367. nicam_status = saa_readb(SAA7134_NICAM_STATUS);
  368. dprintk("getstereo: nicam_status=0x%x\n", nicam_status);
  369. switch (nicam_status & 0x03) {
  370. case 0x01:
  371. retval = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
  372. break;
  373. case 0x02:
  374. retval = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
  375. break;
  376. default:
  377. retval = V4L2_TUNER_SUB_MONO;
  378. }
  379. } else {
  380. /* No nicam detected */
  381. }
  382. break;
  383. }
  384. if (retval != -1)
  385. dprintk("found audio subchannels:%s%s%s%s\n",
  386. (retval & V4L2_TUNER_SUB_MONO) ? " mono" : "",
  387. (retval & V4L2_TUNER_SUB_STEREO) ? " stereo" : "",
  388. (retval & V4L2_TUNER_SUB_LANG1) ? " lang1" : "",
  389. (retval & V4L2_TUNER_SUB_LANG2) ? " lang2" : "");
  390. return retval;
  391. }
  392. static int tvaudio_setstereo(struct saa7134_dev *dev, struct saa7134_tvaudio *audio,
  393. u32 mode)
  394. {
  395. static char *name[] = {
  396. [ V4L2_TUNER_MODE_MONO ] = "mono",
  397. [ V4L2_TUNER_MODE_STEREO ] = "stereo",
  398. [ V4L2_TUNER_MODE_LANG1 ] = "lang1",
  399. [ V4L2_TUNER_MODE_LANG2 ] = "lang2",
  400. [ V4L2_TUNER_MODE_LANG1_LANG2 ] = "lang1+lang2",
  401. };
  402. static u32 fm[] = {
  403. [ V4L2_TUNER_MODE_MONO ] = 0x00, /* ch1 */
  404. [ V4L2_TUNER_MODE_STEREO ] = 0x80, /* auto */
  405. [ V4L2_TUNER_MODE_LANG1 ] = 0x00, /* ch1 */
  406. [ V4L2_TUNER_MODE_LANG2 ] = 0x01, /* ch2 */
  407. [ V4L2_TUNER_MODE_LANG1_LANG2 ] = 0x80, /* auto */
  408. };
  409. u32 reg;
  410. switch (audio->mode) {
  411. case TVAUDIO_FM_MONO:
  412. /* nothing to do ... */
  413. break;
  414. case TVAUDIO_FM_K_STEREO:
  415. case TVAUDIO_FM_BG_STEREO:
  416. case TVAUDIO_NICAM_AM:
  417. case TVAUDIO_NICAM_FM:
  418. dprintk("setstereo [fm] => %s\n",
  419. name[ mode % ARRAY_SIZE(name) ]);
  420. reg = fm[ mode % ARRAY_SIZE(fm) ];
  421. saa_writeb(SAA7134_FM_DEMATRIX, reg);
  422. break;
  423. case TVAUDIO_FM_SAT_STEREO:
  424. /* Not implemented */
  425. break;
  426. }
  427. return 0;
  428. }
  429. static int tvaudio_thread(void *data)
  430. {
  431. struct saa7134_dev *dev = data;
  432. int carr_vals[ARRAY_SIZE(mainscan)];
  433. unsigned int i, audio, nscan;
  434. int max1,max2,carrier,rx,mode,lastmode,default_carrier;
  435. set_freezable();
  436. for (;;) {
  437. tvaudio_sleep(dev,-1);
  438. if (kthread_should_stop())
  439. goto done;
  440. restart:
  441. try_to_freeze();
  442. dev->thread.scan1 = dev->thread.scan2;
  443. dprintk("tvaudio thread scan start [%d]\n",dev->thread.scan1);
  444. dev->tvaudio = NULL;
  445. saa_writeb(SAA7134_MONITOR_SELECT, 0xa0);
  446. saa_writeb(SAA7134_FM_DEMATRIX, 0x80);
  447. if (dev->ctl_automute)
  448. dev->automute = 1;
  449. mute_input_7134(dev);
  450. /* give the tuner some time */
  451. if (tvaudio_sleep(dev,SCAN_INITIAL_DELAY))
  452. goto restart;
  453. max1 = 0;
  454. max2 = 0;
  455. nscan = 0;
  456. carrier = 0;
  457. default_carrier = 0;
  458. for (i = 0; i < ARRAY_SIZE(mainscan); i++) {
  459. if (!(dev->tvnorm->id & mainscan[i].std))
  460. continue;
  461. if (!default_carrier)
  462. default_carrier = mainscan[i].carr;
  463. nscan++;
  464. }
  465. if (1 == nscan) {
  466. /* only one candidate -- skip scan ;) */
  467. dprintk("only one main carrier candidate - skipping scan\n");
  468. max1 = 12345;
  469. carrier = default_carrier;
  470. } else {
  471. /* scan for the main carrier */
  472. saa_writeb(SAA7134_MONITOR_SELECT,0x00);
  473. tvaudio_setmode(dev,&tvaudio[0],NULL);
  474. for (i = 0; i < ARRAY_SIZE(mainscan); i++) {
  475. carr_vals[i] = tvaudio_checkcarrier(dev, mainscan+i);
  476. if (dev->thread.scan1 != dev->thread.scan2)
  477. goto restart;
  478. }
  479. for (max1 = 0, max2 = 0, i = 0; i < ARRAY_SIZE(mainscan); i++) {
  480. if (max1 < carr_vals[i]) {
  481. max2 = max1;
  482. max1 = carr_vals[i];
  483. carrier = mainscan[i].carr;
  484. } else if (max2 < carr_vals[i]) {
  485. max2 = carr_vals[i];
  486. }
  487. }
  488. }
  489. if (0 != carrier && max1 > 2000 && max1 > max2*3) {
  490. /* found good carrier */
  491. dprintk("found %s main sound carrier @ %d.%03d MHz [%d/%d]\n",
  492. dev->tvnorm->name, carrier/1000, carrier%1000,
  493. max1, max2);
  494. dev->last_carrier = carrier;
  495. dev->automute = 0;
  496. } else if (0 != dev->last_carrier) {
  497. /* no carrier -- try last detected one as fallback */
  498. carrier = dev->last_carrier;
  499. dprintk("audio carrier scan failed, "
  500. "using %d.%03d MHz [last detected]\n",
  501. carrier/1000, carrier%1000);
  502. dev->automute = 1;
  503. } else {
  504. /* no carrier + no fallback -- use default */
  505. carrier = default_carrier;
  506. dprintk("audio carrier scan failed, "
  507. "using %d.%03d MHz [default]\n",
  508. carrier/1000, carrier%1000);
  509. dev->automute = 1;
  510. }
  511. tvaudio_setcarrier(dev,carrier,carrier);
  512. saa_andorb(SAA7134_STEREO_DAC_OUTPUT_SELECT, 0x30, 0x00);
  513. saa7134_tvaudio_setmute(dev);
  514. /* find the exact tv audio norm */
  515. for (audio = UNSET, i = 0; i < TVAUDIO; i++) {
  516. if (dev->tvnorm->id != UNSET &&
  517. !(dev->tvnorm->id & tvaudio[i].std))
  518. continue;
  519. if (tvaudio[i].carr1 != carrier)
  520. continue;
  521. /* Note: at least the primary carrier is right here */
  522. if (UNSET == audio)
  523. audio = i;
  524. tvaudio_setmode(dev,&tvaudio[i],"trying");
  525. if (tvaudio_sleep(dev,SCAN_SUBCARRIER_DELAY))
  526. goto restart;
  527. if (-1 != tvaudio_getstereo(dev,&tvaudio[i])) {
  528. audio = i;
  529. break;
  530. }
  531. }
  532. saa_andorb(SAA7134_STEREO_DAC_OUTPUT_SELECT, 0x30, 0x30);
  533. if (UNSET == audio)
  534. continue;
  535. tvaudio_setmode(dev,&tvaudio[audio],"using");
  536. tvaudio_setstereo(dev,&tvaudio[audio],V4L2_TUNER_MODE_MONO);
  537. dev->tvaudio = &tvaudio[audio];
  538. lastmode = 42;
  539. for (;;) {
  540. try_to_freeze();
  541. if (tvaudio_sleep(dev,5000))
  542. goto restart;
  543. if (kthread_should_stop())
  544. break;
  545. if (UNSET == dev->thread.mode) {
  546. rx = tvaudio_getstereo(dev, &tvaudio[audio]);
  547. mode = saa7134_tvaudio_rx2mode(rx);
  548. } else {
  549. mode = dev->thread.mode;
  550. }
  551. if (lastmode != mode) {
  552. tvaudio_setstereo(dev,&tvaudio[audio],mode);
  553. lastmode = mode;
  554. }
  555. }
  556. }
  557. done:
  558. dev->thread.stopped = 1;
  559. return 0;
  560. }
  561. /* ------------------------------------------------------------------ */
  562. /* saa7133 / saa7135 code */
  563. static char *stdres[0x20] = {
  564. [0x00] = "no standard detected",
  565. [0x01] = "B/G (in progress)",
  566. [0x02] = "D/K (in progress)",
  567. [0x03] = "M (in progress)",
  568. [0x04] = "B/G A2",
  569. [0x05] = "B/G NICAM",
  570. [0x06] = "D/K A2 (1)",
  571. [0x07] = "D/K A2 (2)",
  572. [0x08] = "D/K A2 (3)",
  573. [0x09] = "D/K NICAM",
  574. [0x0a] = "L NICAM",
  575. [0x0b] = "I NICAM",
  576. [0x0c] = "M Korea",
  577. [0x0d] = "M BTSC ",
  578. [0x0e] = "M EIAJ",
  579. [0x0f] = "FM radio / IF 10.7 / 50 deemp",
  580. [0x10] = "FM radio / IF 10.7 / 75 deemp",
  581. [0x11] = "FM radio / IF sel / 50 deemp",
  582. [0x12] = "FM radio / IF sel / 75 deemp",
  583. [0x13 ... 0x1e ] = "unknown",
  584. [0x1f] = "??? [in progress]",
  585. };
  586. #define DSP_RETRY 32
  587. #define DSP_DELAY 16
  588. #define SAA7135_DSP_RWCLEAR_RERR 1
  589. static inline int saa_dsp_reset_error_bit(struct saa7134_dev *dev)
  590. {
  591. int state = saa_readb(SAA7135_DSP_RWSTATE);
  592. if (unlikely(state & SAA7135_DSP_RWSTATE_ERR)) {
  593. d2printk("%s: resetting error bit\n", dev->name);
  594. saa_writeb(SAA7135_DSP_RWCLEAR, SAA7135_DSP_RWCLEAR_RERR);
  595. }
  596. return 0;
  597. }
  598. static inline int saa_dsp_wait_bit(struct saa7134_dev *dev, int bit)
  599. {
  600. int state, count = DSP_RETRY;
  601. state = saa_readb(SAA7135_DSP_RWSTATE);
  602. if (unlikely(state & SAA7135_DSP_RWSTATE_ERR)) {
  603. printk(KERN_WARNING "%s: dsp access error\n", dev->name);
  604. saa_dsp_reset_error_bit(dev);
  605. return -EIO;
  606. }
  607. while (0 == (state & bit)) {
  608. if (unlikely(0 == count)) {
  609. printk("%s: dsp access wait timeout [bit=%s]\n",
  610. dev->name,
  611. (bit & SAA7135_DSP_RWSTATE_WRR) ? "WRR" :
  612. (bit & SAA7135_DSP_RWSTATE_RDB) ? "RDB" :
  613. (bit & SAA7135_DSP_RWSTATE_IDA) ? "IDA" :
  614. "???");
  615. return -EIO;
  616. }
  617. saa_wait(DSP_DELAY);
  618. state = saa_readb(SAA7135_DSP_RWSTATE);
  619. count--;
  620. }
  621. return 0;
  622. }
  623. int saa_dsp_writel(struct saa7134_dev *dev, int reg, u32 value)
  624. {
  625. int err;
  626. d2printk("dsp write reg 0x%x = 0x%06x\n",reg<<2,value);
  627. err = saa_dsp_wait_bit(dev,SAA7135_DSP_RWSTATE_WRR);
  628. if (err < 0)
  629. return err;
  630. saa_writel(reg,value);
  631. err = saa_dsp_wait_bit(dev,SAA7135_DSP_RWSTATE_WRR);
  632. if (err < 0)
  633. return err;
  634. return 0;
  635. }
  636. static int getstereo_7133(struct saa7134_dev *dev)
  637. {
  638. int retval = V4L2_TUNER_SUB_MONO;
  639. u32 value;
  640. value = saa_readl(0x528 >> 2);
  641. if (value & 0x20)
  642. retval = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
  643. if (value & 0x40)
  644. retval = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
  645. return retval;
  646. }
  647. static int mute_input_7133(struct saa7134_dev *dev)
  648. {
  649. u32 reg = 0;
  650. u32 xbarin, xbarout;
  651. int mask;
  652. struct saa7134_input *in;
  653. xbarin = 0x03;
  654. switch (dev->input->amux) {
  655. case TV:
  656. reg = 0x02;
  657. xbarin = 0;
  658. break;
  659. case LINE1:
  660. reg = 0x00;
  661. break;
  662. case LINE2:
  663. case LINE2_LEFT:
  664. reg = 0x09;
  665. break;
  666. }
  667. saa_dsp_writel(dev, 0x464 >> 2, xbarin);
  668. if (dev->ctl_mute) {
  669. reg = 0x07;
  670. xbarout = 0xbbbbbb;
  671. } else
  672. xbarout = 0xbbbb10;
  673. saa_dsp_writel(dev, 0x46c >> 2, xbarout);
  674. saa_writel(0x594 >> 2, reg);
  675. /* switch gpio-connected external audio mux */
  676. if (0 != card(dev).gpiomask) {
  677. mask = card(dev).gpiomask;
  678. if (card(dev).mute.name && dev->ctl_mute)
  679. in = &card(dev).mute;
  680. else
  681. in = dev->input;
  682. saa_andorl(SAA7134_GPIO_GPMODE0 >> 2, mask, mask);
  683. saa_andorl(SAA7134_GPIO_GPSTATUS0 >> 2, mask, in->gpio);
  684. saa7134_track_gpio(dev,in->name);
  685. }
  686. return 0;
  687. }
  688. static int tvaudio_thread_ddep(void *data)
  689. {
  690. struct saa7134_dev *dev = data;
  691. u32 value, norms;
  692. set_freezable();
  693. for (;;) {
  694. tvaudio_sleep(dev,-1);
  695. if (kthread_should_stop())
  696. goto done;
  697. restart:
  698. try_to_freeze();
  699. dev->thread.scan1 = dev->thread.scan2;
  700. dprintk("tvaudio thread scan start [%d]\n",dev->thread.scan1);
  701. if (audio_ddep >= 0x04 && audio_ddep <= 0x0e) {
  702. /* insmod option override */
  703. norms = (audio_ddep << 2) | 0x01;
  704. dprintk("ddep override: %s\n",stdres[audio_ddep]);
  705. } else if (&card(dev).radio == dev->input) {
  706. dprintk("FM Radio\n");
  707. if (dev->tuner_type == TUNER_PHILIPS_TDA8290) {
  708. norms = (0x11 << 2) | 0x01;
  709. saa_dsp_writel(dev, 0x42c >> 2, 0x729555);
  710. } else {
  711. norms = (0x0f << 2) | 0x01;
  712. }
  713. } else {
  714. /* (let chip) scan for sound carrier */
  715. norms = 0;
  716. if (dev->tvnorm->id & (V4L2_STD_B | V4L2_STD_GH))
  717. norms |= 0x04;
  718. if (dev->tvnorm->id & V4L2_STD_PAL_I)
  719. norms |= 0x20;
  720. if (dev->tvnorm->id & V4L2_STD_DK)
  721. norms |= 0x08;
  722. if (dev->tvnorm->id & V4L2_STD_MN)
  723. norms |= 0x40;
  724. if (dev->tvnorm->id & (V4L2_STD_SECAM_L | V4L2_STD_SECAM_LC))
  725. norms |= 0x10;
  726. if (0 == norms)
  727. norms = 0x7c; /* all */
  728. dprintk("scanning:%s%s%s%s%s\n",
  729. (norms & 0x04) ? " B/G" : "",
  730. (norms & 0x08) ? " D/K" : "",
  731. (norms & 0x10) ? " L/L'" : "",
  732. (norms & 0x20) ? " I" : "",
  733. (norms & 0x40) ? " M" : "");
  734. }
  735. /* kick automatic standard detection */
  736. saa_dsp_writel(dev, 0x454 >> 2, 0);
  737. saa_dsp_writel(dev, 0x454 >> 2, norms | 0x80);
  738. /* setup crossbars */
  739. saa_dsp_writel(dev, 0x464 >> 2, 0x000000);
  740. saa_dsp_writel(dev, 0x470 >> 2, 0x101010);
  741. if (tvaudio_sleep(dev,3000))
  742. goto restart;
  743. value = saa_readl(0x528 >> 2) & 0xffffff;
  744. dprintk("tvaudio thread status: 0x%x [%s%s%s]\n",
  745. value, stdres[value & 0x1f],
  746. (value & 0x000020) ? ",stereo" : "",
  747. (value & 0x000040) ? ",dual" : "");
  748. dprintk("detailed status: "
  749. "%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s\n",
  750. (value & 0x000080) ? " A2/EIAJ pilot tone " : "",
  751. (value & 0x000100) ? " A2/EIAJ dual " : "",
  752. (value & 0x000200) ? " A2/EIAJ stereo " : "",
  753. (value & 0x000400) ? " A2/EIAJ noise mute " : "",
  754. (value & 0x000800) ? " BTSC/FM radio pilot " : "",
  755. (value & 0x001000) ? " SAP carrier " : "",
  756. (value & 0x002000) ? " BTSC stereo noise mute " : "",
  757. (value & 0x004000) ? " SAP noise mute " : "",
  758. (value & 0x008000) ? " VDSP " : "",
  759. (value & 0x010000) ? " NICST " : "",
  760. (value & 0x020000) ? " NICDU " : "",
  761. (value & 0x040000) ? " NICAM muted " : "",
  762. (value & 0x080000) ? " NICAM reserve sound " : "",
  763. (value & 0x100000) ? " init done " : "");
  764. }
  765. done:
  766. dev->thread.stopped = 1;
  767. return 0;
  768. }
  769. /* ------------------------------------------------------------------ */
  770. /* common stuff + external entry points */
  771. void saa7134_enable_i2s(struct saa7134_dev *dev)
  772. {
  773. int i2s_format;
  774. if (!card_is_empress(dev))
  775. return;
  776. if (dev->pci->device == PCI_DEVICE_ID_PHILIPS_SAA7130)
  777. return;
  778. /* configure GPIO for out */
  779. saa_andorl(SAA7134_GPIO_GPMODE0 >> 2, 0x0E000000, 0x00000000);
  780. switch (dev->pci->device) {
  781. case PCI_DEVICE_ID_PHILIPS_SAA7133:
  782. case PCI_DEVICE_ID_PHILIPS_SAA7135:
  783. /* Set I2S format (SONY)  */
  784. saa_writeb(SAA7133_I2S_AUDIO_CONTROL, 0x00);
  785. /* Start I2S */
  786. saa_writeb(SAA7134_I2S_AUDIO_OUTPUT, 0x11);
  787. break;
  788. case PCI_DEVICE_ID_PHILIPS_SAA7134:
  789. i2s_format = (dev->input->amux == TV) ? 0x00 : 0x01;
  790. /* enable I2S audio output for the mpeg encoder */
  791. saa_writeb(SAA7134_I2S_OUTPUT_SELECT, 0x80);
  792. saa_writeb(SAA7134_I2S_OUTPUT_FORMAT, i2s_format);
  793. saa_writeb(SAA7134_I2S_OUTPUT_LEVEL, 0x0F);
  794. saa_writeb(SAA7134_I2S_AUDIO_OUTPUT, 0x01);
  795. default:
  796. break;
  797. }
  798. }
  799. int saa7134_tvaudio_rx2mode(u32 rx)
  800. {
  801. u32 mode;
  802. mode = V4L2_TUNER_MODE_MONO;
  803. if (rx & V4L2_TUNER_SUB_STEREO)
  804. mode = V4L2_TUNER_MODE_STEREO;
  805. else if (rx & V4L2_TUNER_SUB_LANG1)
  806. mode = V4L2_TUNER_MODE_LANG1;
  807. else if (rx & V4L2_TUNER_SUB_LANG2)
  808. mode = V4L2_TUNER_MODE_LANG2;
  809. return mode;
  810. }
  811. void saa7134_tvaudio_setmute(struct saa7134_dev *dev)
  812. {
  813. switch (dev->pci->device) {
  814. case PCI_DEVICE_ID_PHILIPS_SAA7130:
  815. case PCI_DEVICE_ID_PHILIPS_SAA7134:
  816. mute_input_7134(dev);
  817. break;
  818. case PCI_DEVICE_ID_PHILIPS_SAA7133:
  819. case PCI_DEVICE_ID_PHILIPS_SAA7135:
  820. mute_input_7133(dev);
  821. break;
  822. }
  823. }
  824. void saa7134_tvaudio_setinput(struct saa7134_dev *dev,
  825. struct saa7134_input *in)
  826. {
  827. dev->input = in;
  828. switch (dev->pci->device) {
  829. case PCI_DEVICE_ID_PHILIPS_SAA7130:
  830. case PCI_DEVICE_ID_PHILIPS_SAA7134:
  831. mute_input_7134(dev);
  832. break;
  833. case PCI_DEVICE_ID_PHILIPS_SAA7133:
  834. case PCI_DEVICE_ID_PHILIPS_SAA7135:
  835. mute_input_7133(dev);
  836. break;
  837. }
  838. saa7134_enable_i2s(dev);
  839. }
  840. void saa7134_tvaudio_setvolume(struct saa7134_dev *dev, int level)
  841. {
  842. switch (dev->pci->device) {
  843. case PCI_DEVICE_ID_PHILIPS_SAA7134:
  844. saa_writeb(SAA7134_CHANNEL1_LEVEL, level & 0x1f);
  845. saa_writeb(SAA7134_CHANNEL2_LEVEL, level & 0x1f);
  846. saa_writeb(SAA7134_NICAM_LEVEL_ADJUST, level & 0x1f);
  847. break;
  848. }
  849. }
  850. int saa7134_tvaudio_getstereo(struct saa7134_dev *dev)
  851. {
  852. int retval = V4L2_TUNER_SUB_MONO;
  853. switch (dev->pci->device) {
  854. case PCI_DEVICE_ID_PHILIPS_SAA7134:
  855. if (dev->tvaudio)
  856. retval = tvaudio_getstereo(dev,dev->tvaudio);
  857. break;
  858. case PCI_DEVICE_ID_PHILIPS_SAA7133:
  859. case PCI_DEVICE_ID_PHILIPS_SAA7135:
  860. retval = getstereo_7133(dev);
  861. break;
  862. }
  863. return retval;
  864. }
  865. void saa7134_tvaudio_init(struct saa7134_dev *dev)
  866. {
  867. int clock = saa7134_boards[dev->board].audio_clock;
  868. if (UNSET != audio_clock_override)
  869. clock = audio_clock_override;
  870. switch (dev->pci->device) {
  871. case PCI_DEVICE_ID_PHILIPS_SAA7134:
  872. /* init all audio registers */
  873. saa_writeb(SAA7134_AUDIO_PLL_CTRL, 0x00);
  874. if (need_resched())
  875. schedule();
  876. else
  877. udelay(10);
  878. saa_writeb(SAA7134_AUDIO_CLOCK0, clock & 0xff);
  879. saa_writeb(SAA7134_AUDIO_CLOCK1, (clock >> 8) & 0xff);
  880. saa_writeb(SAA7134_AUDIO_CLOCK2, (clock >> 16) & 0xff);
  881. /* frame locked audio is mandatory for NICAM */
  882. saa_writeb(SAA7134_AUDIO_PLL_CTRL, 0x01);
  883. saa_writeb(SAA7134_NICAM_ERROR_LOW, 0x14);
  884. saa_writeb(SAA7134_NICAM_ERROR_HIGH, 0x50);
  885. break;
  886. case PCI_DEVICE_ID_PHILIPS_SAA7133:
  887. case PCI_DEVICE_ID_PHILIPS_SAA7135:
  888. saa_writel(0x598 >> 2, clock);
  889. saa_dsp_writel(dev, 0x474 >> 2, 0x00);
  890. saa_dsp_writel(dev, 0x450 >> 2, 0x00);
  891. }
  892. }
  893. int saa7134_tvaudio_init2(struct saa7134_dev *dev)
  894. {
  895. int (*my_thread)(void *data) = NULL;
  896. switch (dev->pci->device) {
  897. case PCI_DEVICE_ID_PHILIPS_SAA7134:
  898. my_thread = tvaudio_thread;
  899. break;
  900. case PCI_DEVICE_ID_PHILIPS_SAA7133:
  901. case PCI_DEVICE_ID_PHILIPS_SAA7135:
  902. my_thread = tvaudio_thread_ddep;
  903. break;
  904. }
  905. dev->thread.thread = NULL;
  906. dev->thread.scan1 = dev->thread.scan2 = 0;
  907. if (my_thread) {
  908. saa7134_tvaudio_init(dev);
  909. /* start tvaudio thread */
  910. dev->thread.thread = kthread_run(my_thread, dev, "%s", dev->name);
  911. if (IS_ERR(dev->thread.thread)) {
  912. printk(KERN_WARNING "%s: kernel_thread() failed\n",
  913. dev->name);
  914. /* XXX: missing error handling here */
  915. }
  916. }
  917. saa7134_enable_i2s(dev);
  918. return 0;
  919. }
  920. int saa7134_tvaudio_close(struct saa7134_dev *dev)
  921. {
  922. dev->automute = 1;
  923. /* anything else to undo? */
  924. return 0;
  925. }
  926. int saa7134_tvaudio_fini(struct saa7134_dev *dev)
  927. {
  928. /* shutdown tvaudio thread */
  929. if (dev->thread.thread && !dev->thread.stopped)
  930. kthread_stop(dev->thread.thread);
  931. saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x07, 0x00); /* LINE1 */
  932. return 0;
  933. }
  934. int saa7134_tvaudio_do_scan(struct saa7134_dev *dev)
  935. {
  936. if (dev->input->amux != TV) {
  937. dprintk("sound IF not in use, skipping scan\n");
  938. dev->automute = 0;
  939. saa7134_tvaudio_setmute(dev);
  940. } else if (dev->thread.thread) {
  941. dev->thread.mode = UNSET;
  942. dev->thread.scan2++;
  943. if (!dev->insuspend && !dev->thread.stopped)
  944. wake_up_process(dev->thread.thread);
  945. } else {
  946. dev->automute = 0;
  947. saa7134_tvaudio_setmute(dev);
  948. }
  949. return 0;
  950. }
  951. EXPORT_SYMBOL(saa_dsp_writel);
  952. EXPORT_SYMBOL(saa7134_tvaudio_setmute);
  953. /* ----------------------------------------------------------- */
  954. /*
  955. * Local variables:
  956. * c-basic-offset: 8
  957. * End:
  958. */