msp3400.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880
  1. /*
  2. * programming the msp34* sound processor family
  3. *
  4. * (c) 1997-2001 Gerd Knorr <kraxel@bytesex.org>
  5. *
  6. * what works and what doesn't:
  7. *
  8. * AM-Mono
  9. * Support for Hauppauge cards added (decoding handled by tuner) added by
  10. * Frederic Crozat <fcrozat@mail.dotcom.fr>
  11. *
  12. * FM-Mono
  13. * should work. The stereo modes are backward compatible to FM-mono,
  14. * therefore FM-Mono should be allways available.
  15. *
  16. * FM-Stereo (B/G, used in germany)
  17. * should work, with autodetect
  18. *
  19. * FM-Stereo (satellite)
  20. * should work, no autodetect (i.e. default is mono, but you can
  21. * switch to stereo -- untested)
  22. *
  23. * NICAM (B/G, L , used in UK, Scandinavia, Spain and France)
  24. * should work, with autodetect. Support for NICAM was added by
  25. * Pekka Pietikainen <pp@netppl.fi>
  26. *
  27. *
  28. * TODO:
  29. * - better SAT support
  30. *
  31. *
  32. * 980623 Thomas Sailer (sailer@ife.ee.ethz.ch)
  33. * using soundcore instead of OSS
  34. *
  35. */
  36. #include <linux/config.h>
  37. #include <linux/module.h>
  38. #include <linux/moduleparam.h>
  39. #include <linux/kernel.h>
  40. #include <linux/sched.h>
  41. #include <linux/string.h>
  42. #include <linux/timer.h>
  43. #include <linux/delay.h>
  44. #include <linux/errno.h>
  45. #include <linux/slab.h>
  46. #include <linux/i2c.h>
  47. #include <linux/videodev.h>
  48. #include <linux/init.h>
  49. #include <linux/smp_lock.h>
  50. #include <linux/kthread.h>
  51. #include <linux/suspend.h>
  52. #include <asm/semaphore.h>
  53. #include <asm/pgtable.h>
  54. #include <media/audiochip.h>
  55. #include <media/id.h>
  56. #include "msp3400.h"
  57. #define OPMODE_AUTO -1
  58. #define OPMODE_MANUAL 0
  59. #define OPMODE_SIMPLE 1 /* use short programming (>= msp3410 only) */
  60. #define OPMODE_SIMPLER 2 /* use shorter programming (>= msp34xxG) */
  61. /* insmod parameters */
  62. static int opmode = OPMODE_AUTO;
  63. static int debug = 0; /* debug output */
  64. static int once = 0; /* no continous stereo monitoring */
  65. static int amsound = 0; /* hard-wire AM sound at 6.5 Hz (france),
  66. the autoscan seems work well only with FM... */
  67. static int standard = 1; /* Override auto detect of audio standard, if needed. */
  68. static int dolby = 0;
  69. static int stereo_threshold = 0x190; /* a2 threshold for stereo/bilingual
  70. (msp34xxg only) 0x00a0-0x03c0 */
  71. struct msp3400c {
  72. int rev1,rev2;
  73. int opmode;
  74. int mode;
  75. int norm;
  76. int nicam_on;
  77. int acb;
  78. int main, second; /* sound carrier */
  79. int input;
  80. int source; /* see msp34xxg_set_source */
  81. /* v4l2 */
  82. int audmode;
  83. int rxsubchans;
  84. int muted;
  85. int volume, balance;
  86. int bass, treble;
  87. /* thread */
  88. struct task_struct *kthread;
  89. wait_queue_head_t wq;
  90. int restart:1;
  91. int watch_stereo:1;
  92. };
  93. #define HAVE_NICAM(msp) (((msp->rev2>>8) & 0xff) != 00)
  94. #define HAVE_SIMPLE(msp) ((msp->rev1 & 0xff) >= 'D'-'@')
  95. #define HAVE_SIMPLER(msp) ((msp->rev1 & 0xff) >= 'G'-'@')
  96. #define HAVE_RADIO(msp) ((msp->rev1 & 0xff) >= 'G'-'@')
  97. #define VIDEO_MODE_RADIO 16 /* norm magic for radio mode */
  98. /* ---------------------------------------------------------------------- */
  99. #define dprintk if (debug >= 1) printk
  100. #define d2printk if (debug >= 2) printk
  101. /* read-only */
  102. module_param(opmode, int, 0444);
  103. /* read-write */
  104. module_param(once, int, 0644);
  105. module_param(debug, int, 0644);
  106. module_param(stereo_threshold, int, 0644);
  107. module_param(standard, int, 0644);
  108. module_param(amsound, int, 0644);
  109. module_param(dolby, int, 0644);
  110. MODULE_PARM_DESC(once, "No continuous stereo monitoring");
  111. MODULE_PARM_DESC(debug, "Enable debug messages");
  112. MODULE_PARM_DESC(standard, "Specify audio standard: 32 = NTSC, 64 = radio, Default: Autodetect");
  113. MODULE_PARM_DESC(amsound, "Hardwire AM sound at 6.5Hz (France), FM can autoscan");
  114. MODULE_DESCRIPTION("device driver for msp34xx TV sound processor");
  115. MODULE_AUTHOR("Gerd Knorr");
  116. MODULE_LICENSE("Dual BSD/GPL"); /* FreeBSD uses this too */
  117. /* ---------------------------------------------------------------------- */
  118. #define I2C_MSP3400C 0x80
  119. #define I2C_MSP3400C_ALT 0x88
  120. #define I2C_MSP3400C_DEM 0x10
  121. #define I2C_MSP3400C_DFP 0x12
  122. /* Addresses to scan */
  123. static unsigned short normal_i2c[] = {
  124. I2C_MSP3400C >> 1,
  125. I2C_MSP3400C_ALT >> 1,
  126. I2C_CLIENT_END
  127. };
  128. static unsigned short normal_i2c_range[] = {I2C_CLIENT_END,I2C_CLIENT_END};
  129. I2C_CLIENT_INSMOD;
  130. /* ----------------------------------------------------------------------- */
  131. /* functions for talking to the MSP3400C Sound processor */
  132. static int msp3400c_reset(struct i2c_client *client)
  133. {
  134. /* reset and read revision code */
  135. static char reset_off[3] = { 0x00, 0x80, 0x00 };
  136. static char reset_on[3] = { 0x00, 0x00, 0x00 };
  137. static char write[3] = { I2C_MSP3400C_DFP + 1, 0x00, 0x1e };
  138. char read[2];
  139. struct i2c_msg reset[2] = {
  140. { client->addr, I2C_M_IGNORE_NAK, 3, reset_off },
  141. { client->addr, I2C_M_IGNORE_NAK, 3, reset_on },
  142. };
  143. struct i2c_msg test[2] = {
  144. { client->addr, 0, 3, write },
  145. { client->addr, I2C_M_RD, 2, read },
  146. };
  147. if ( (1 != i2c_transfer(client->adapter,&reset[0],1)) ||
  148. (1 != i2c_transfer(client->adapter,&reset[1],1)) ||
  149. (2 != i2c_transfer(client->adapter,test,2)) ) {
  150. printk(KERN_ERR "msp3400: chip reset failed\n");
  151. return -1;
  152. }
  153. return 0;
  154. }
  155. static int
  156. msp3400c_read(struct i2c_client *client, int dev, int addr)
  157. {
  158. int err;
  159. unsigned char write[3];
  160. unsigned char read[2];
  161. struct i2c_msg msgs[2] = {
  162. { client->addr, 0, 3, write },
  163. { client->addr, I2C_M_RD, 2, read }
  164. };
  165. write[0] = dev+1;
  166. write[1] = addr >> 8;
  167. write[2] = addr & 0xff;
  168. for (err = 0; err < 3;) {
  169. if (2 == i2c_transfer(client->adapter,msgs,2))
  170. break;
  171. err++;
  172. printk(KERN_WARNING "msp34xx: I/O error #%d (read 0x%02x/0x%02x)\n",
  173. err, dev, addr);
  174. msleep(10);
  175. }
  176. if (3 == err) {
  177. printk(KERN_WARNING "msp34xx: giving up, reseting chip. Sound will go off, sorry folks :-|\n");
  178. msp3400c_reset(client);
  179. return -1;
  180. }
  181. return read[0] << 8 | read[1];
  182. }
  183. static int
  184. msp3400c_write(struct i2c_client *client, int dev, int addr, int val)
  185. {
  186. int err;
  187. unsigned char buffer[5];
  188. buffer[0] = dev;
  189. buffer[1] = addr >> 8;
  190. buffer[2] = addr & 0xff;
  191. buffer[3] = val >> 8;
  192. buffer[4] = val & 0xff;
  193. for (err = 0; err < 3;) {
  194. if (5 == i2c_master_send(client, buffer, 5))
  195. break;
  196. err++;
  197. printk(KERN_WARNING "msp34xx: I/O error #%d (write 0x%02x/0x%02x)\n",
  198. err, dev, addr);
  199. msleep(10);
  200. }
  201. if (3 == err) {
  202. printk(KERN_WARNING "msp34xx: giving up, reseting chip. Sound will go off, sorry folks :-|\n");
  203. msp3400c_reset(client);
  204. return -1;
  205. }
  206. return 0;
  207. }
  208. /* ------------------------------------------------------------------------ */
  209. /* This macro is allowed for *constants* only, gcc must calculate it
  210. at compile time. Remember -- no floats in kernel mode */
  211. #define MSP_CARRIER(freq) ((int)((float)(freq/18.432)*(1<<24)))
  212. #define MSP_MODE_AM_DETECT 0
  213. #define MSP_MODE_FM_RADIO 2
  214. #define MSP_MODE_FM_TERRA 3
  215. #define MSP_MODE_FM_SAT 4
  216. #define MSP_MODE_FM_NICAM1 5
  217. #define MSP_MODE_FM_NICAM2 6
  218. #define MSP_MODE_AM_NICAM 7
  219. #define MSP_MODE_BTSC 8
  220. #define MSP_MODE_EXTERN 9
  221. static struct MSP_INIT_DATA_DEM {
  222. int fir1[6];
  223. int fir2[6];
  224. int cdo1;
  225. int cdo2;
  226. int ad_cv;
  227. int mode_reg;
  228. int dfp_src;
  229. int dfp_matrix;
  230. } msp_init_data[] = {
  231. /* AM (for carrier detect / msp3400) */
  232. { { 75, 19, 36, 35, 39, 40 }, { 75, 19, 36, 35, 39, 40 },
  233. MSP_CARRIER(5.5), MSP_CARRIER(5.5),
  234. 0x00d0, 0x0500, 0x0020, 0x3000},
  235. /* AM (for carrier detect / msp3410) */
  236. { { -1, -1, -8, 2, 59, 126 }, { -1, -1, -8, 2, 59, 126 },
  237. MSP_CARRIER(5.5), MSP_CARRIER(5.5),
  238. 0x00d0, 0x0100, 0x0020, 0x3000},
  239. /* FM Radio */
  240. { { -8, -8, 4, 6, 78, 107 }, { -8, -8, 4, 6, 78, 107 },
  241. MSP_CARRIER(10.7), MSP_CARRIER(10.7),
  242. 0x00d0, 0x0480, 0x0020, 0x3000 },
  243. /* Terrestial FM-mono + FM-stereo */
  244. { { 3, 18, 27, 48, 66, 72 }, { 3, 18, 27, 48, 66, 72 },
  245. MSP_CARRIER(5.5), MSP_CARRIER(5.5),
  246. 0x00d0, 0x0480, 0x0030, 0x3000},
  247. /* Sat FM-mono */
  248. { { 1, 9, 14, 24, 33, 37 }, { 3, 18, 27, 48, 66, 72 },
  249. MSP_CARRIER(6.5), MSP_CARRIER(6.5),
  250. 0x00c6, 0x0480, 0x0000, 0x3000},
  251. /* NICAM/FM -- B/G (5.5/5.85), D/K (6.5/5.85) */
  252. { { -2, -8, -10, 10, 50, 86 }, { 3, 18, 27, 48, 66, 72 },
  253. MSP_CARRIER(5.5), MSP_CARRIER(5.5),
  254. 0x00d0, 0x0040, 0x0120, 0x3000},
  255. /* NICAM/FM -- I (6.0/6.552) */
  256. { { 2, 4, -6, -4, 40, 94 }, { 3, 18, 27, 48, 66, 72 },
  257. MSP_CARRIER(6.0), MSP_CARRIER(6.0),
  258. 0x00d0, 0x0040, 0x0120, 0x3000},
  259. /* NICAM/AM -- L (6.5/5.85) */
  260. { { -2, -8, -10, 10, 50, 86 }, { -4, -12, -9, 23, 79, 126 },
  261. MSP_CARRIER(6.5), MSP_CARRIER(6.5),
  262. 0x00c6, 0x0140, 0x0120, 0x7c03},
  263. };
  264. struct CARRIER_DETECT {
  265. int cdo;
  266. char *name;
  267. };
  268. static struct CARRIER_DETECT carrier_detect_main[] = {
  269. /* main carrier */
  270. { MSP_CARRIER(4.5), "4.5 NTSC" },
  271. { MSP_CARRIER(5.5), "5.5 PAL B/G" },
  272. { MSP_CARRIER(6.0), "6.0 PAL I" },
  273. { MSP_CARRIER(6.5), "6.5 PAL D/K + SAT + SECAM" }
  274. };
  275. static struct CARRIER_DETECT carrier_detect_55[] = {
  276. /* PAL B/G */
  277. { MSP_CARRIER(5.7421875), "5.742 PAL B/G FM-stereo" },
  278. { MSP_CARRIER(5.85), "5.85 PAL B/G NICAM" }
  279. };
  280. static struct CARRIER_DETECT carrier_detect_65[] = {
  281. /* PAL SAT / SECAM */
  282. { MSP_CARRIER(5.85), "5.85 PAL D/K + SECAM NICAM" },
  283. { MSP_CARRIER(6.2578125), "6.25 PAL D/K1 FM-stereo" },
  284. { MSP_CARRIER(6.7421875), "6.74 PAL D/K2 FM-stereo" },
  285. { MSP_CARRIER(7.02), "7.02 PAL SAT FM-stereo s/b" },
  286. { MSP_CARRIER(7.20), "7.20 PAL SAT FM-stereo s" },
  287. { MSP_CARRIER(7.38), "7.38 PAL SAT FM-stereo b" },
  288. };
  289. #define CARRIER_COUNT(x) (sizeof(x)/sizeof(struct CARRIER_DETECT))
  290. /* ----------------------------------------------------------------------- */
  291. static int scarts[3][9] = {
  292. /* MASK IN1 IN2 IN1_DA IN2_DA IN3 IN4 MONO MUTE */
  293. { 0x0320, 0x0000, 0x0200, -1, -1, 0x0300, 0x0020, 0x0100, 0x0320 },
  294. { 0x0c40, 0x0440, 0x0400, 0x0c00, 0x0040, 0x0000, 0x0840, 0x0800, 0x0c40 },
  295. { 0x3080, 0x1000, 0x1080, 0x0000, 0x0080, 0x2080, 0x3080, 0x2000, 0x3000 },
  296. };
  297. static char *scart_names[] = {
  298. "mask", "in1", "in2", "in1 da", "in2 da", "in3", "in4", "mono", "mute"
  299. };
  300. static void
  301. msp3400c_set_scart(struct i2c_client *client, int in, int out)
  302. {
  303. struct msp3400c *msp = i2c_get_clientdata(client);
  304. if (-1 == scarts[out][in])
  305. return;
  306. dprintk(KERN_DEBUG
  307. "msp34xx: scart switch: %s => %d\n",scart_names[in],out);
  308. msp->acb &= ~scarts[out][SCART_MASK];
  309. msp->acb |= scarts[out][in];
  310. msp3400c_write(client,I2C_MSP3400C_DFP, 0x0013, msp->acb);
  311. }
  312. /* ------------------------------------------------------------------------ */
  313. static void msp3400c_setcarrier(struct i2c_client *client, int cdo1, int cdo2)
  314. {
  315. msp3400c_write(client,I2C_MSP3400C_DEM, 0x0093, cdo1 & 0xfff);
  316. msp3400c_write(client,I2C_MSP3400C_DEM, 0x009b, cdo1 >> 12);
  317. msp3400c_write(client,I2C_MSP3400C_DEM, 0x00a3, cdo2 & 0xfff);
  318. msp3400c_write(client,I2C_MSP3400C_DEM, 0x00ab, cdo2 >> 12);
  319. msp3400c_write(client,I2C_MSP3400C_DEM, 0x0056, 0); /*LOAD_REG_1/2*/
  320. }
  321. static void msp3400c_setvolume(struct i2c_client *client,
  322. int muted, int volume, int balance)
  323. {
  324. int val = 0, bal = 0;
  325. if (!muted) {
  326. /* 0x7f instead if 0x73 here has sound quality issues,
  327. * probably due to overmodulation + clipping ... */
  328. val = (volume * 0x73 / 65535) << 8;
  329. }
  330. if (val) {
  331. bal = (balance / 256) - 128;
  332. }
  333. dprintk(KERN_DEBUG
  334. "msp34xx: setvolume: mute=%s %d:%d v=0x%02x b=0x%02x\n",
  335. muted ? "on" : "off", volume, balance, val>>8, bal);
  336. msp3400c_write(client,I2C_MSP3400C_DFP, 0x0000, val); /* loudspeaker */
  337. msp3400c_write(client,I2C_MSP3400C_DFP, 0x0006, val); /* headphones */
  338. msp3400c_write(client,I2C_MSP3400C_DFP, 0x0007,
  339. muted ? 0x01 : (val | 0x01));
  340. msp3400c_write(client,I2C_MSP3400C_DFP, 0x0001, bal << 8);
  341. }
  342. static void msp3400c_setbass(struct i2c_client *client, int bass)
  343. {
  344. int val = ((bass-32768) * 0x60 / 65535) << 8;
  345. dprintk(KERN_DEBUG "msp34xx: setbass: %d 0x%02x\n",bass, val>>8);
  346. msp3400c_write(client,I2C_MSP3400C_DFP, 0x0002, val); /* loudspeaker */
  347. }
  348. static void msp3400c_settreble(struct i2c_client *client, int treble)
  349. {
  350. int val = ((treble-32768) * 0x60 / 65535) << 8;
  351. dprintk(KERN_DEBUG "msp34xx: settreble: %d 0x%02x\n",treble, val>>8);
  352. msp3400c_write(client,I2C_MSP3400C_DFP, 0x0003, val); /* loudspeaker */
  353. }
  354. static void msp3400c_setmode(struct i2c_client *client, int type)
  355. {
  356. struct msp3400c *msp = i2c_get_clientdata(client);
  357. int i;
  358. dprintk(KERN_DEBUG "msp3400: setmode: %d\n",type);
  359. msp->mode = type;
  360. msp->audmode = V4L2_TUNER_MODE_MONO;
  361. msp->rxsubchans = V4L2_TUNER_SUB_MONO;
  362. msp3400c_write(client,I2C_MSP3400C_DEM, 0x00bb, /* ad_cv */
  363. msp_init_data[type].ad_cv);
  364. for (i = 5; i >= 0; i--) /* fir 1 */
  365. msp3400c_write(client,I2C_MSP3400C_DEM, 0x0001,
  366. msp_init_data[type].fir1[i]);
  367. msp3400c_write(client,I2C_MSP3400C_DEM, 0x0005, 0x0004); /* fir 2 */
  368. msp3400c_write(client,I2C_MSP3400C_DEM, 0x0005, 0x0040);
  369. msp3400c_write(client,I2C_MSP3400C_DEM, 0x0005, 0x0000);
  370. for (i = 5; i >= 0; i--)
  371. msp3400c_write(client,I2C_MSP3400C_DEM, 0x0005,
  372. msp_init_data[type].fir2[i]);
  373. msp3400c_write(client,I2C_MSP3400C_DEM, 0x0083, /* MODE_REG */
  374. msp_init_data[type].mode_reg);
  375. msp3400c_setcarrier(client, msp_init_data[type].cdo1,
  376. msp_init_data[type].cdo2);
  377. msp3400c_write(client,I2C_MSP3400C_DEM, 0x0056, 0); /*LOAD_REG_1/2*/
  378. if (dolby) {
  379. msp3400c_write(client,I2C_MSP3400C_DFP, 0x0008,
  380. 0x0520); /* I2S1 */
  381. msp3400c_write(client,I2C_MSP3400C_DFP, 0x0009,
  382. 0x0620); /* I2S2 */
  383. msp3400c_write(client,I2C_MSP3400C_DFP, 0x000b,
  384. msp_init_data[type].dfp_src);
  385. } else {
  386. msp3400c_write(client,I2C_MSP3400C_DFP, 0x0008,
  387. msp_init_data[type].dfp_src);
  388. msp3400c_write(client,I2C_MSP3400C_DFP, 0x0009,
  389. msp_init_data[type].dfp_src);
  390. msp3400c_write(client,I2C_MSP3400C_DFP, 0x000b,
  391. msp_init_data[type].dfp_src);
  392. }
  393. msp3400c_write(client,I2C_MSP3400C_DFP, 0x000a,
  394. msp_init_data[type].dfp_src);
  395. msp3400c_write(client,I2C_MSP3400C_DFP, 0x000e,
  396. msp_init_data[type].dfp_matrix);
  397. if (HAVE_NICAM(msp)) {
  398. /* nicam prescale */
  399. msp3400c_write(client,I2C_MSP3400C_DFP, 0x0010, 0x5a00); /* was: 0x3000 */
  400. }
  401. }
  402. static int best_audio_mode(int rxsubchans)
  403. {
  404. if (rxsubchans & V4L2_TUNER_SUB_STEREO)
  405. return V4L2_TUNER_MODE_STEREO;
  406. if (rxsubchans & V4L2_TUNER_SUB_LANG1)
  407. return V4L2_TUNER_MODE_LANG1;
  408. if (rxsubchans & V4L2_TUNER_SUB_LANG2)
  409. return V4L2_TUNER_MODE_LANG2;
  410. return V4L2_TUNER_MODE_MONO;
  411. }
  412. /* turn on/off nicam + stereo */
  413. static void msp3400c_set_audmode(struct i2c_client *client, int audmode)
  414. {
  415. static char *strmode[16] = {
  416. #if __GNUC__ >= 3
  417. [ 0 ... 15 ] = "invalid",
  418. #endif
  419. [ V4L2_TUNER_MODE_MONO ] = "mono",
  420. [ V4L2_TUNER_MODE_STEREO ] = "stereo",
  421. [ V4L2_TUNER_MODE_LANG1 ] = "lang1",
  422. [ V4L2_TUNER_MODE_LANG2 ] = "lang2",
  423. };
  424. struct msp3400c *msp = i2c_get_clientdata(client);
  425. int nicam=0; /* channel source: FM/AM or nicam */
  426. int src=0;
  427. BUG_ON(msp->opmode == OPMODE_SIMPLER);
  428. msp->audmode = audmode;
  429. /* switch demodulator */
  430. switch (msp->mode) {
  431. case MSP_MODE_FM_TERRA:
  432. dprintk(KERN_DEBUG "msp3400: FM setstereo: %s\n",
  433. strmode[audmode]);
  434. msp3400c_setcarrier(client,msp->second,msp->main);
  435. switch (audmode) {
  436. case V4L2_TUNER_MODE_STEREO:
  437. msp3400c_write(client,I2C_MSP3400C_DFP, 0x000e, 0x3001);
  438. break;
  439. case V4L2_TUNER_MODE_MONO:
  440. case V4L2_TUNER_MODE_LANG1:
  441. case V4L2_TUNER_MODE_LANG2:
  442. msp3400c_write(client,I2C_MSP3400C_DFP, 0x000e, 0x3000);
  443. break;
  444. }
  445. break;
  446. case MSP_MODE_FM_SAT:
  447. dprintk(KERN_DEBUG "msp3400: SAT setstereo: %s\n",
  448. strmode[audmode]);
  449. switch (audmode) {
  450. case V4L2_TUNER_MODE_MONO:
  451. msp3400c_setcarrier(client, MSP_CARRIER(6.5), MSP_CARRIER(6.5));
  452. break;
  453. case V4L2_TUNER_MODE_STEREO:
  454. msp3400c_setcarrier(client, MSP_CARRIER(7.2), MSP_CARRIER(7.02));
  455. break;
  456. case V4L2_TUNER_MODE_LANG1:
  457. msp3400c_setcarrier(client, MSP_CARRIER(7.38), MSP_CARRIER(7.02));
  458. break;
  459. case V4L2_TUNER_MODE_LANG2:
  460. msp3400c_setcarrier(client, MSP_CARRIER(7.38), MSP_CARRIER(7.02));
  461. break;
  462. }
  463. break;
  464. case MSP_MODE_FM_NICAM1:
  465. case MSP_MODE_FM_NICAM2:
  466. case MSP_MODE_AM_NICAM:
  467. dprintk(KERN_DEBUG "msp3400: NICAM setstereo: %s\n",
  468. strmode[audmode]);
  469. msp3400c_setcarrier(client,msp->second,msp->main);
  470. if (msp->nicam_on)
  471. nicam=0x0100;
  472. break;
  473. case MSP_MODE_BTSC:
  474. dprintk(KERN_DEBUG "msp3400: BTSC setstereo: %s\n",
  475. strmode[audmode]);
  476. nicam=0x0300;
  477. break;
  478. case MSP_MODE_EXTERN:
  479. dprintk(KERN_DEBUG "msp3400: extern setstereo: %s\n",
  480. strmode[audmode]);
  481. nicam = 0x0200;
  482. break;
  483. case MSP_MODE_FM_RADIO:
  484. dprintk(KERN_DEBUG "msp3400: FM-Radio setstereo: %s\n",
  485. strmode[audmode]);
  486. break;
  487. default:
  488. dprintk(KERN_DEBUG "msp3400: mono setstereo\n");
  489. return;
  490. }
  491. /* switch audio */
  492. switch (audmode) {
  493. case V4L2_TUNER_MODE_STEREO:
  494. src = 0x0020 | nicam;
  495. #if 0
  496. /* spatial effect */
  497. msp3400c_write(client,I2C_MSP3400C_DFP, 0x0005,0x4000);
  498. #endif
  499. break;
  500. case V4L2_TUNER_MODE_MONO:
  501. if (msp->mode == MSP_MODE_AM_NICAM) {
  502. dprintk("msp3400: switching to AM mono\n");
  503. /* AM mono decoding is handled by tuner, not MSP chip */
  504. /* SCART switching control register */
  505. msp3400c_set_scart(client,SCART_MONO,0);
  506. src = 0x0200;
  507. break;
  508. }
  509. case V4L2_TUNER_MODE_LANG1:
  510. src = 0x0000 | nicam;
  511. break;
  512. case V4L2_TUNER_MODE_LANG2:
  513. src = 0x0010 | nicam;
  514. break;
  515. }
  516. dprintk(KERN_DEBUG
  517. "msp3400: setstereo final source/matrix = 0x%x\n", src);
  518. if (dolby) {
  519. msp3400c_write(client,I2C_MSP3400C_DFP, 0x0008,0x0520);
  520. msp3400c_write(client,I2C_MSP3400C_DFP, 0x0009,0x0620);
  521. msp3400c_write(client,I2C_MSP3400C_DFP, 0x000a,src);
  522. msp3400c_write(client,I2C_MSP3400C_DFP, 0x000b,src);
  523. } else {
  524. msp3400c_write(client,I2C_MSP3400C_DFP, 0x0008,src);
  525. msp3400c_write(client,I2C_MSP3400C_DFP, 0x0009,src);
  526. msp3400c_write(client,I2C_MSP3400C_DFP, 0x000a,src);
  527. msp3400c_write(client,I2C_MSP3400C_DFP, 0x000b,src);
  528. }
  529. }
  530. static void
  531. msp3400c_print_mode(struct msp3400c *msp)
  532. {
  533. if (msp->main == msp->second) {
  534. printk(KERN_DEBUG "msp3400: mono sound carrier: %d.%03d MHz\n",
  535. msp->main/910000,(msp->main/910)%1000);
  536. } else {
  537. printk(KERN_DEBUG "msp3400: main sound carrier: %d.%03d MHz\n",
  538. msp->main/910000,(msp->main/910)%1000);
  539. }
  540. if (msp->mode == MSP_MODE_FM_NICAM1 ||
  541. msp->mode == MSP_MODE_FM_NICAM2)
  542. printk(KERN_DEBUG "msp3400: NICAM/FM carrier : %d.%03d MHz\n",
  543. msp->second/910000,(msp->second/910)%1000);
  544. if (msp->mode == MSP_MODE_AM_NICAM)
  545. printk(KERN_DEBUG "msp3400: NICAM/AM carrier : %d.%03d MHz\n",
  546. msp->second/910000,(msp->second/910)%1000);
  547. if (msp->mode == MSP_MODE_FM_TERRA &&
  548. msp->main != msp->second) {
  549. printk(KERN_DEBUG "msp3400: FM-stereo carrier : %d.%03d MHz\n",
  550. msp->second/910000,(msp->second/910)%1000);
  551. }
  552. }
  553. /* ----------------------------------------------------------------------- */
  554. struct REGISTER_DUMP {
  555. int addr;
  556. char *name;
  557. };
  558. static int
  559. autodetect_stereo(struct i2c_client *client)
  560. {
  561. struct msp3400c *msp = i2c_get_clientdata(client);
  562. int val;
  563. int rxsubchans = msp->rxsubchans;
  564. int newnicam = msp->nicam_on;
  565. int update = 0;
  566. switch (msp->mode) {
  567. case MSP_MODE_FM_TERRA:
  568. val = msp3400c_read(client, I2C_MSP3400C_DFP, 0x18);
  569. if (val > 32767)
  570. val -= 65536;
  571. dprintk(KERN_DEBUG
  572. "msp34xx: stereo detect register: %d\n",val);
  573. if (val > 4096) {
  574. rxsubchans = V4L2_TUNER_SUB_STEREO | V4L2_TUNER_SUB_MONO;
  575. } else if (val < -4096) {
  576. rxsubchans = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
  577. } else {
  578. rxsubchans = V4L2_TUNER_SUB_MONO;
  579. }
  580. newnicam = 0;
  581. break;
  582. case MSP_MODE_FM_NICAM1:
  583. case MSP_MODE_FM_NICAM2:
  584. case MSP_MODE_AM_NICAM:
  585. val = msp3400c_read(client, I2C_MSP3400C_DEM, 0x23);
  586. dprintk(KERN_DEBUG
  587. "msp34xx: nicam sync=%d, mode=%d\n",
  588. val & 1, (val & 0x1e) >> 1);
  589. if (val & 1) {
  590. /* nicam synced */
  591. switch ((val & 0x1e) >> 1) {
  592. case 0:
  593. case 8:
  594. rxsubchans = V4L2_TUNER_SUB_STEREO;
  595. break;
  596. case 1:
  597. case 9:
  598. rxsubchans = V4L2_TUNER_SUB_MONO
  599. | V4L2_TUNER_SUB_LANG1;
  600. break;
  601. case 2:
  602. case 10:
  603. rxsubchans = V4L2_TUNER_SUB_MONO
  604. | V4L2_TUNER_SUB_LANG1
  605. | V4L2_TUNER_SUB_LANG2;
  606. break;
  607. default:
  608. rxsubchans = V4L2_TUNER_SUB_MONO;
  609. break;
  610. }
  611. newnicam=1;
  612. } else {
  613. newnicam = 0;
  614. rxsubchans = V4L2_TUNER_SUB_MONO;
  615. }
  616. break;
  617. case MSP_MODE_BTSC:
  618. val = msp3400c_read(client, I2C_MSP3400C_DEM, 0x200);
  619. dprintk(KERN_DEBUG
  620. "msp3410: status=0x%x (pri=%s, sec=%s, %s%s%s)\n",
  621. val,
  622. (val & 0x0002) ? "no" : "yes",
  623. (val & 0x0004) ? "no" : "yes",
  624. (val & 0x0040) ? "stereo" : "mono",
  625. (val & 0x0080) ? ", nicam 2nd mono" : "",
  626. (val & 0x0100) ? ", bilingual/SAP" : "");
  627. rxsubchans = V4L2_TUNER_SUB_MONO;
  628. if (val & 0x0040) rxsubchans |= V4L2_TUNER_SUB_STEREO;
  629. if (val & 0x0100) rxsubchans |= V4L2_TUNER_SUB_LANG1;
  630. break;
  631. }
  632. if (rxsubchans != msp->rxsubchans) {
  633. update = 1;
  634. dprintk(KERN_DEBUG "msp34xx: watch: rxsubchans %d => %d\n",
  635. msp->rxsubchans,rxsubchans);
  636. msp->rxsubchans = rxsubchans;
  637. }
  638. if (newnicam != msp->nicam_on) {
  639. update = 1;
  640. dprintk(KERN_DEBUG "msp34xx: watch: nicam %d => %d\n",
  641. msp->nicam_on,newnicam);
  642. msp->nicam_on = newnicam;
  643. }
  644. return update;
  645. }
  646. /*
  647. * A kernel thread for msp3400 control -- we don't want to block the
  648. * in the ioctl while doing the sound carrier & stereo detect
  649. */
  650. static int msp34xx_sleep(struct msp3400c *msp, int timeout)
  651. {
  652. DECLARE_WAITQUEUE(wait, current);
  653. add_wait_queue(&msp->wq, &wait);
  654. if (!kthread_should_stop()) {
  655. if (timeout < 0) {
  656. set_current_state(TASK_INTERRUPTIBLE);
  657. schedule();
  658. } else {
  659. #if 0
  660. /* hmm, that one doesn't return on wakeup ... */
  661. msleep_interruptible(timeout);
  662. #else
  663. set_current_state(TASK_INTERRUPTIBLE);
  664. schedule_timeout(msecs_to_jiffies(timeout));
  665. #endif
  666. }
  667. }
  668. if (current->flags & PF_FREEZE)
  669. refrigerator(PF_FREEZE);
  670. remove_wait_queue(&msp->wq, &wait);
  671. return msp->restart;
  672. }
  673. /* stereo/multilang monitoring */
  674. static void watch_stereo(struct i2c_client *client)
  675. {
  676. struct msp3400c *msp = i2c_get_clientdata(client);
  677. if (autodetect_stereo(client))
  678. msp3400c_set_audmode(client,best_audio_mode(msp->rxsubchans));
  679. if (once)
  680. msp->watch_stereo = 0;
  681. }
  682. static int msp3400c_thread(void *data)
  683. {
  684. struct i2c_client *client = data;
  685. struct msp3400c *msp = i2c_get_clientdata(client);
  686. struct CARRIER_DETECT *cd;
  687. int count, max1,max2,val1,val2, val,this;
  688. printk("msp3400: kthread started\n");
  689. for (;;) {
  690. d2printk("msp3400: thread: sleep\n");
  691. msp34xx_sleep(msp,-1);
  692. d2printk("msp3400: thread: wakeup\n");
  693. restart:
  694. dprintk("msp3410: thread: restart scan\n");
  695. msp->restart = 0;
  696. if (kthread_should_stop())
  697. break;
  698. if (VIDEO_MODE_RADIO == msp->norm ||
  699. MSP_MODE_EXTERN == msp->mode) {
  700. /* no carrier scan, just unmute */
  701. printk("msp3400: thread: no carrier scan\n");
  702. msp3400c_setvolume(client, msp->muted,
  703. msp->volume, msp->balance);
  704. continue;
  705. }
  706. /* mute */
  707. msp3400c_setvolume(client, msp->muted, 0, 0);
  708. msp3400c_setmode(client, MSP_MODE_AM_DETECT /* +1 */ );
  709. val1 = val2 = 0;
  710. max1 = max2 = -1;
  711. msp->watch_stereo = 0;
  712. /* some time for the tuner to sync */
  713. if (msp34xx_sleep(msp,200))
  714. goto restart;
  715. /* carrier detect pass #1 -- main carrier */
  716. cd = carrier_detect_main; count = CARRIER_COUNT(carrier_detect_main);
  717. if (amsound && (msp->norm == VIDEO_MODE_SECAM)) {
  718. /* autodetect doesn't work well with AM ... */
  719. max1 = 3;
  720. count = 0;
  721. dprintk("msp3400: AM sound override\n");
  722. }
  723. for (this = 0; this < count; this++) {
  724. msp3400c_setcarrier(client, cd[this].cdo,cd[this].cdo);
  725. if (msp34xx_sleep(msp,100))
  726. goto restart;
  727. val = msp3400c_read(client, I2C_MSP3400C_DFP, 0x1b);
  728. if (val > 32767)
  729. val -= 65536;
  730. if (val1 < val)
  731. val1 = val, max1 = this;
  732. dprintk("msp3400: carrier1 val: %5d / %s\n", val,cd[this].name);
  733. }
  734. /* carrier detect pass #2 -- second (stereo) carrier */
  735. switch (max1) {
  736. case 1: /* 5.5 */
  737. cd = carrier_detect_55;
  738. count = CARRIER_COUNT(carrier_detect_55);
  739. break;
  740. case 3: /* 6.5 */
  741. cd = carrier_detect_65;
  742. count = CARRIER_COUNT(carrier_detect_65);
  743. break;
  744. case 0: /* 4.5 */
  745. case 2: /* 6.0 */
  746. default:
  747. cd = NULL; count = 0;
  748. break;
  749. }
  750. if (amsound && (msp->norm == VIDEO_MODE_SECAM)) {
  751. /* autodetect doesn't work well with AM ... */
  752. cd = NULL; count = 0; max2 = 0;
  753. }
  754. for (this = 0; this < count; this++) {
  755. msp3400c_setcarrier(client, cd[this].cdo,cd[this].cdo);
  756. if (msp34xx_sleep(msp,100))
  757. goto restart;
  758. val = msp3400c_read(client, I2C_MSP3400C_DFP, 0x1b);
  759. if (val > 32767)
  760. val -= 65536;
  761. if (val2 < val)
  762. val2 = val, max2 = this;
  763. dprintk("msp3400: carrier2 val: %5d / %s\n", val,cd[this].name);
  764. }
  765. /* programm the msp3400 according to the results */
  766. msp->main = carrier_detect_main[max1].cdo;
  767. switch (max1) {
  768. case 1: /* 5.5 */
  769. if (max2 == 0) {
  770. /* B/G FM-stereo */
  771. msp->second = carrier_detect_55[max2].cdo;
  772. msp3400c_setmode(client, MSP_MODE_FM_TERRA);
  773. msp->nicam_on = 0;
  774. msp3400c_set_audmode(client, V4L2_TUNER_MODE_MONO);
  775. msp->watch_stereo = 1;
  776. } else if (max2 == 1 && HAVE_NICAM(msp)) {
  777. /* B/G NICAM */
  778. msp->second = carrier_detect_55[max2].cdo;
  779. msp3400c_setmode(client, MSP_MODE_FM_NICAM1);
  780. msp->nicam_on = 1;
  781. msp3400c_setcarrier(client, msp->second, msp->main);
  782. msp->watch_stereo = 1;
  783. } else {
  784. goto no_second;
  785. }
  786. break;
  787. case 2: /* 6.0 */
  788. /* PAL I NICAM */
  789. msp->second = MSP_CARRIER(6.552);
  790. msp3400c_setmode(client, MSP_MODE_FM_NICAM2);
  791. msp->nicam_on = 1;
  792. msp3400c_setcarrier(client, msp->second, msp->main);
  793. msp->watch_stereo = 1;
  794. break;
  795. case 3: /* 6.5 */
  796. if (max2 == 1 || max2 == 2) {
  797. /* D/K FM-stereo */
  798. msp->second = carrier_detect_65[max2].cdo;
  799. msp3400c_setmode(client, MSP_MODE_FM_TERRA);
  800. msp->nicam_on = 0;
  801. msp3400c_set_audmode(client, V4L2_TUNER_MODE_MONO);
  802. msp->watch_stereo = 1;
  803. } else if (max2 == 0 &&
  804. msp->norm == VIDEO_MODE_SECAM) {
  805. /* L NICAM or AM-mono */
  806. msp->second = carrier_detect_65[max2].cdo;
  807. msp3400c_setmode(client, MSP_MODE_AM_NICAM);
  808. msp->nicam_on = 0;
  809. msp3400c_set_audmode(client, V4L2_TUNER_MODE_MONO);
  810. msp3400c_setcarrier(client, msp->second, msp->main);
  811. /* volume prescale for SCART (AM mono input) */
  812. msp3400c_write(client,I2C_MSP3400C_DFP, 0x000d, 0x1900);
  813. msp->watch_stereo = 1;
  814. } else if (max2 == 0 && HAVE_NICAM(msp)) {
  815. /* D/K NICAM */
  816. msp->second = carrier_detect_65[max2].cdo;
  817. msp3400c_setmode(client, MSP_MODE_FM_NICAM1);
  818. msp->nicam_on = 1;
  819. msp3400c_setcarrier(client, msp->second, msp->main);
  820. msp->watch_stereo = 1;
  821. } else {
  822. goto no_second;
  823. }
  824. break;
  825. case 0: /* 4.5 */
  826. default:
  827. no_second:
  828. msp->second = carrier_detect_main[max1].cdo;
  829. msp3400c_setmode(client, MSP_MODE_FM_TERRA);
  830. msp->nicam_on = 0;
  831. msp3400c_setcarrier(client, msp->second, msp->main);
  832. msp->rxsubchans = V4L2_TUNER_SUB_MONO;
  833. msp3400c_set_audmode(client, V4L2_TUNER_MODE_MONO);
  834. break;
  835. }
  836. /* unmute */
  837. msp3400c_setvolume(client, msp->muted,
  838. msp->volume, msp->balance);
  839. if (debug)
  840. msp3400c_print_mode(msp);
  841. /* monitor tv audio mode */
  842. while (msp->watch_stereo) {
  843. if (msp34xx_sleep(msp,5000))
  844. goto restart;
  845. watch_stereo(client);
  846. }
  847. }
  848. dprintk(KERN_DEBUG "msp3400: thread: exit\n");
  849. return 0;
  850. }
  851. /* ----------------------------------------------------------------------- */
  852. /* this one uses the automatic sound standard detection of newer */
  853. /* msp34xx chip versions */
  854. static struct MODES {
  855. int retval;
  856. int main, second;
  857. char *name;
  858. } modelist[] = {
  859. { 0x0000, 0, 0, "ERROR" },
  860. { 0x0001, 0, 0, "autodetect start" },
  861. { 0x0002, MSP_CARRIER(4.5), MSP_CARRIER(4.72), "4.5/4.72 M Dual FM-Stereo" },
  862. { 0x0003, MSP_CARRIER(5.5), MSP_CARRIER(5.7421875), "5.5/5.74 B/G Dual FM-Stereo" },
  863. { 0x0004, MSP_CARRIER(6.5), MSP_CARRIER(6.2578125), "6.5/6.25 D/K1 Dual FM-Stereo" },
  864. { 0x0005, MSP_CARRIER(6.5), MSP_CARRIER(6.7421875), "6.5/6.74 D/K2 Dual FM-Stereo" },
  865. { 0x0006, MSP_CARRIER(6.5), MSP_CARRIER(6.5), "6.5 D/K FM-Mono (HDEV3)" },
  866. { 0x0008, MSP_CARRIER(5.5), MSP_CARRIER(5.85), "5.5/5.85 B/G NICAM FM" },
  867. { 0x0009, MSP_CARRIER(6.5), MSP_CARRIER(5.85), "6.5/5.85 L NICAM AM" },
  868. { 0x000a, MSP_CARRIER(6.0), MSP_CARRIER(6.55), "6.0/6.55 I NICAM FM" },
  869. { 0x000b, MSP_CARRIER(6.5), MSP_CARRIER(5.85), "6.5/5.85 D/K NICAM FM" },
  870. { 0x000c, MSP_CARRIER(6.5), MSP_CARRIER(5.85), "6.5/5.85 D/K NICAM FM (HDEV2)" },
  871. { 0x0020, MSP_CARRIER(4.5), MSP_CARRIER(4.5), "4.5 M BTSC-Stereo" },
  872. { 0x0021, MSP_CARRIER(4.5), MSP_CARRIER(4.5), "4.5 M BTSC-Mono + SAP" },
  873. { 0x0030, MSP_CARRIER(4.5), MSP_CARRIER(4.5), "4.5 M EIA-J Japan Stereo" },
  874. { 0x0040, MSP_CARRIER(10.7), MSP_CARRIER(10.7), "10.7 FM-Stereo Radio" },
  875. { 0x0050, MSP_CARRIER(6.5), MSP_CARRIER(6.5), "6.5 SAT-Mono" },
  876. { 0x0051, MSP_CARRIER(7.02), MSP_CARRIER(7.20), "7.02/7.20 SAT-Stereo" },
  877. { 0x0060, MSP_CARRIER(7.2), MSP_CARRIER(7.2), "7.2 SAT ADR" },
  878. { -1, 0, 0, NULL }, /* EOF */
  879. };
  880. static inline const char *msp34xx_standard_mode_name(int mode)
  881. {
  882. int i;
  883. for (i = 0; modelist[i].name != NULL; i++)
  884. if (modelist[i].retval == mode)
  885. return modelist[i].name;
  886. return "unknown";
  887. }
  888. static int msp34xx_modus(int norm)
  889. {
  890. switch (norm) {
  891. case VIDEO_MODE_PAL:
  892. #if 1
  893. /* experimental: not sure this works with all chip versions */
  894. return 0x7003;
  895. #else
  896. /* previous value, try this if it breaks ... */
  897. return 0x1003;
  898. #endif
  899. case VIDEO_MODE_NTSC: /* BTSC */
  900. return 0x2003;
  901. case VIDEO_MODE_SECAM:
  902. return 0x0003;
  903. case VIDEO_MODE_RADIO:
  904. return 0x0003;
  905. case VIDEO_MODE_AUTO:
  906. return 0x2003;
  907. default:
  908. return 0x0003;
  909. }
  910. }
  911. static int msp34xx_standard(int norm)
  912. {
  913. switch (norm) {
  914. case VIDEO_MODE_PAL:
  915. return 1;
  916. case VIDEO_MODE_NTSC: /* BTSC */
  917. return 0x0020;
  918. case VIDEO_MODE_SECAM:
  919. return 1;
  920. case VIDEO_MODE_RADIO:
  921. return 0x0040;
  922. default:
  923. return 1;
  924. }
  925. }
  926. static int msp3410d_thread(void *data)
  927. {
  928. struct i2c_client *client = data;
  929. struct msp3400c *msp = i2c_get_clientdata(client);
  930. int mode,val,i,std;
  931. printk("msp3410: daemon started\n");
  932. for (;;) {
  933. d2printk(KERN_DEBUG "msp3410: thread: sleep\n");
  934. msp34xx_sleep(msp,-1);
  935. d2printk(KERN_DEBUG "msp3410: thread: wakeup\n");
  936. restart:
  937. dprintk("msp3410: thread: restart scan\n");
  938. msp->restart = 0;
  939. if (kthread_should_stop())
  940. break;
  941. if (msp->mode == MSP_MODE_EXTERN) {
  942. /* no carrier scan needed, just unmute */
  943. dprintk(KERN_DEBUG "msp3410: thread: no carrier scan\n");
  944. msp3400c_setvolume(client, msp->muted,
  945. msp->volume, msp->balance);
  946. continue;
  947. }
  948. /* put into sane state (and mute) */
  949. msp3400c_reset(client);
  950. /* some time for the tuner to sync */
  951. if (msp34xx_sleep(msp,200))
  952. goto restart;
  953. /* start autodetect */
  954. mode = msp34xx_modus(msp->norm);
  955. std = msp34xx_standard(msp->norm);
  956. msp3400c_write(client, I2C_MSP3400C_DEM, 0x30, mode);
  957. msp3400c_write(client, I2C_MSP3400C_DEM, 0x20, std);
  958. msp->watch_stereo = 0;
  959. if (debug)
  960. printk(KERN_DEBUG "msp3410: setting mode: %s (0x%04x)\n",
  961. msp34xx_standard_mode_name(std) ,std);
  962. if (std != 1) {
  963. /* programmed some specific mode */
  964. val = std;
  965. } else {
  966. /* triggered autodetect */
  967. for (;;) {
  968. if (msp34xx_sleep(msp,100))
  969. goto restart;
  970. /* check results */
  971. val = msp3400c_read(client, I2C_MSP3400C_DEM, 0x7e);
  972. if (val < 0x07ff)
  973. break;
  974. dprintk(KERN_DEBUG "msp3410: detection still in progress\n");
  975. }
  976. }
  977. for (i = 0; modelist[i].name != NULL; i++)
  978. if (modelist[i].retval == val)
  979. break;
  980. dprintk(KERN_DEBUG "msp3410: current mode: %s (0x%04x)\n",
  981. modelist[i].name ? modelist[i].name : "unknown",
  982. val);
  983. msp->main = modelist[i].main;
  984. msp->second = modelist[i].second;
  985. if (amsound && (msp->norm == VIDEO_MODE_SECAM) && (val != 0x0009)) {
  986. /* autodetection has failed, let backup */
  987. dprintk(KERN_DEBUG "msp3410: autodetection failed,"
  988. " switching to backup mode: %s (0x%04x)\n",
  989. modelist[8].name ? modelist[8].name : "unknown",val);
  990. val = 0x0009;
  991. msp3400c_write(client, I2C_MSP3400C_DEM, 0x20, val);
  992. }
  993. /* set various prescales */
  994. msp3400c_write(client, I2C_MSP3400C_DFP, 0x0d, 0x1900); /* scart */
  995. msp3400c_write(client, I2C_MSP3400C_DFP, 0x0e, 0x2403); /* FM */
  996. msp3400c_write(client, I2C_MSP3400C_DFP, 0x10, 0x5a00); /* nicam */
  997. /* set stereo */
  998. switch (val) {
  999. case 0x0008: /* B/G NICAM */
  1000. case 0x000a: /* I NICAM */
  1001. if (val == 0x0008)
  1002. msp->mode = MSP_MODE_FM_NICAM1;
  1003. else
  1004. msp->mode = MSP_MODE_FM_NICAM2;
  1005. /* just turn on stereo */
  1006. msp->rxsubchans = V4L2_TUNER_SUB_STEREO;
  1007. msp->nicam_on = 1;
  1008. msp->watch_stereo = 1;
  1009. msp3400c_set_audmode(client,V4L2_TUNER_MODE_STEREO);
  1010. break;
  1011. case 0x0009:
  1012. msp->mode = MSP_MODE_AM_NICAM;
  1013. msp->rxsubchans = V4L2_TUNER_SUB_MONO;
  1014. msp->nicam_on = 1;
  1015. msp3400c_set_audmode(client,V4L2_TUNER_MODE_MONO);
  1016. msp->watch_stereo = 1;
  1017. break;
  1018. case 0x0020: /* BTSC */
  1019. /* just turn on stereo */
  1020. msp->mode = MSP_MODE_BTSC;
  1021. msp->rxsubchans = V4L2_TUNER_SUB_STEREO;
  1022. msp->nicam_on = 0;
  1023. msp->watch_stereo = 1;
  1024. msp3400c_set_audmode(client,V4L2_TUNER_MODE_STEREO);
  1025. break;
  1026. case 0x0040: /* FM radio */
  1027. msp->mode = MSP_MODE_FM_RADIO;
  1028. msp->rxsubchans = V4L2_TUNER_SUB_STEREO;
  1029. msp->audmode = V4L2_TUNER_MODE_STEREO;
  1030. msp->nicam_on = 0;
  1031. msp->watch_stereo = 0;
  1032. /* not needed in theory if HAVE_RADIO(), but
  1033. short programming enables carrier mute */
  1034. msp3400c_setmode(client,MSP_MODE_FM_RADIO);
  1035. msp3400c_setcarrier(client, MSP_CARRIER(10.7),
  1036. MSP_CARRIER(10.7));
  1037. /* scart routing */
  1038. msp3400c_set_scart(client,SCART_IN2,0);
  1039. #if 0
  1040. /* radio from SCART_IN2 */
  1041. msp3400c_write(client,I2C_MSP3400C_DFP, 0x08, 0x0220);
  1042. msp3400c_write(client,I2C_MSP3400C_DFP, 0x09, 0x0220);
  1043. msp3400c_write(client,I2C_MSP3400C_DFP, 0x0b, 0x0220);
  1044. #else
  1045. /* msp34xx does radio decoding */
  1046. msp3400c_write(client,I2C_MSP3400C_DFP, 0x08, 0x0020);
  1047. msp3400c_write(client,I2C_MSP3400C_DFP, 0x09, 0x0020);
  1048. msp3400c_write(client,I2C_MSP3400C_DFP, 0x0b, 0x0020);
  1049. #endif
  1050. break;
  1051. case 0x0003:
  1052. case 0x0004:
  1053. case 0x0005:
  1054. msp->mode = MSP_MODE_FM_TERRA;
  1055. msp->rxsubchans = V4L2_TUNER_SUB_MONO;
  1056. msp->audmode = V4L2_TUNER_MODE_MONO;
  1057. msp->nicam_on = 0;
  1058. msp->watch_stereo = 1;
  1059. break;
  1060. }
  1061. /* unmute, restore misc registers */
  1062. msp3400c_setbass(client, msp->bass);
  1063. msp3400c_settreble(client, msp->treble);
  1064. msp3400c_setvolume(client, msp->muted,
  1065. msp->volume, msp->balance);
  1066. msp3400c_write(client, I2C_MSP3400C_DFP, 0x0013, msp->acb);
  1067. /* monitor tv audio mode */
  1068. while (msp->watch_stereo) {
  1069. if (msp34xx_sleep(msp,5000))
  1070. goto restart;
  1071. watch_stereo(client);
  1072. }
  1073. }
  1074. dprintk(KERN_DEBUG "msp3410: thread: exit\n");
  1075. return 0;
  1076. }
  1077. /* ----------------------------------------------------------------------- */
  1078. /* msp34xxG + (simpler no-thread) */
  1079. /* this one uses both automatic standard detection and automatic sound */
  1080. /* select which are available in the newer G versions */
  1081. /* struct msp: only norm, acb and source are really used in this mode */
  1082. static void msp34xxg_set_source(struct i2c_client *client, int source);
  1083. /* (re-)initialize the msp34xxg, according to the current norm in msp->norm
  1084. * return 0 if it worked, -1 if it failed
  1085. */
  1086. static int msp34xxg_init(struct i2c_client *client)
  1087. {
  1088. struct msp3400c *msp = i2c_get_clientdata(client);
  1089. int modus,std;
  1090. if (msp3400c_reset(client))
  1091. return -1;
  1092. /* make sure that input/output is muted (paranoid mode) */
  1093. if (msp3400c_write(client,
  1094. I2C_MSP3400C_DFP,
  1095. 0x13, /* ACB */
  1096. 0x0f20 /* mute DSP input, mute SCART 1 */))
  1097. return -1;
  1098. /* step-by-step initialisation, as described in the manual */
  1099. modus = msp34xx_modus(msp->norm);
  1100. std = msp34xx_standard(msp->norm);
  1101. modus &= ~0x03; /* STATUS_CHANGE=0 */
  1102. modus |= 0x01; /* AUTOMATIC_SOUND_DETECTION=1 */
  1103. if (msp3400c_write(client,
  1104. I2C_MSP3400C_DEM,
  1105. 0x30/*MODUS*/,
  1106. modus))
  1107. return -1;
  1108. if (msp3400c_write(client,
  1109. I2C_MSP3400C_DEM,
  1110. 0x20/*stanard*/,
  1111. std))
  1112. return -1;
  1113. /* write the dfps that may have an influence on
  1114. standard/audio autodetection right now */
  1115. msp34xxg_set_source(client, msp->source);
  1116. if (msp3400c_write(client, I2C_MSP3400C_DFP,
  1117. 0x0e, /* AM/FM Prescale */
  1118. 0x3000 /* default: [15:8] 75khz deviation */))
  1119. return -1;
  1120. if (msp3400c_write(client, I2C_MSP3400C_DFP,
  1121. 0x10, /* NICAM Prescale */
  1122. 0x5a00 /* default: 9db gain (as recommended) */))
  1123. return -1;
  1124. if (msp3400c_write(client,
  1125. I2C_MSP3400C_DEM,
  1126. 0x20, /* STANDARD SELECT */
  1127. standard /* default: 0x01 for automatic standard select*/))
  1128. return -1;
  1129. return 0;
  1130. }
  1131. static int msp34xxg_thread(void *data)
  1132. {
  1133. struct i2c_client *client = data;
  1134. struct msp3400c *msp = i2c_get_clientdata(client);
  1135. int val, std, i;
  1136. printk("msp34xxg: daemon started\n");
  1137. msp->source = 1; /* default */
  1138. for (;;) {
  1139. d2printk(KERN_DEBUG "msp34xxg: thread: sleep\n");
  1140. msp34xx_sleep(msp,-1);
  1141. d2printk(KERN_DEBUG "msp34xxg: thread: wakeup\n");
  1142. restart:
  1143. dprintk("msp34xxg: thread: restart scan\n");
  1144. msp->restart = 0;
  1145. if (kthread_should_stop())
  1146. break;
  1147. /* setup the chip*/
  1148. msp34xxg_init(client);
  1149. std = standard;
  1150. if (std != 0x01)
  1151. goto unmute;
  1152. /* watch autodetect */
  1153. dprintk("msp34xxg: triggered autodetect, waiting for result\n");
  1154. for (i = 0; i < 10; i++) {
  1155. if (msp34xx_sleep(msp,100))
  1156. goto restart;
  1157. /* check results */
  1158. val = msp3400c_read(client, I2C_MSP3400C_DEM, 0x7e);
  1159. if (val < 0x07ff) {
  1160. std = val;
  1161. break;
  1162. }
  1163. dprintk("msp34xxg: detection still in progress\n");
  1164. }
  1165. if (0x01 == std) {
  1166. dprintk("msp34xxg: detection still in progress after 10 tries. giving up.\n");
  1167. continue;
  1168. }
  1169. unmute:
  1170. dprintk("msp34xxg: current mode: %s (0x%04x)\n",
  1171. msp34xx_standard_mode_name(std), std);
  1172. /* unmute: dispatch sound to scart output, set scart volume */
  1173. dprintk("msp34xxg: unmute\n");
  1174. msp3400c_setbass(client, msp->bass);
  1175. msp3400c_settreble(client, msp->treble);
  1176. msp3400c_setvolume(client, msp->muted, msp->volume, msp->balance);
  1177. /* restore ACB */
  1178. if (msp3400c_write(client,
  1179. I2C_MSP3400C_DFP,
  1180. 0x13, /* ACB */
  1181. msp->acb))
  1182. return -1;
  1183. }
  1184. dprintk(KERN_DEBUG "msp34xxg: thread: exit\n");
  1185. return 0;
  1186. }
  1187. /* set the same 'source' for the loudspeaker, scart and quasi-peak detector
  1188. * the value for source is the same as bit 15:8 of DFP registers 0x08,
  1189. * 0x0a and 0x0c: 0=mono, 1=stereo or A|B, 2=SCART, 3=stereo or A, 4=stereo or B
  1190. *
  1191. * this function replaces msp3400c_setstereo
  1192. */
  1193. static void msp34xxg_set_source(struct i2c_client *client, int source)
  1194. {
  1195. struct msp3400c *msp = i2c_get_clientdata(client);
  1196. /* fix matrix mode to stereo and let the msp choose what
  1197. * to output according to 'source', as recommended
  1198. * for MONO (source==0) downmixing set bit[7:0] to 0x30
  1199. */
  1200. int value = (source&0x07)<<8|(source==0 ? 0x30:0x20);
  1201. dprintk("msp34xxg: set source to %d (0x%x)\n", source, value);
  1202. msp3400c_write(client,
  1203. I2C_MSP3400C_DFP,
  1204. 0x08, /* Loudspeaker Output */
  1205. value);
  1206. msp3400c_write(client,
  1207. I2C_MSP3400C_DFP,
  1208. 0x0a, /* SCART1 DA Output */
  1209. value);
  1210. msp3400c_write(client,
  1211. I2C_MSP3400C_DFP,
  1212. 0x0c, /* Quasi-peak detector */
  1213. value);
  1214. /*
  1215. * set identification threshold. Personally, I
  1216. * I set it to a higher value that the default
  1217. * of 0x190 to ignore noisy stereo signals.
  1218. * this needs tuning. (recommended range 0x00a0-0x03c0)
  1219. * 0x7f0 = forced mono mode
  1220. */
  1221. msp3400c_write(client,
  1222. I2C_MSP3400C_DEM,
  1223. 0x22, /* a2 threshold for stereo/bilingual */
  1224. stereo_threshold);
  1225. msp->source=source;
  1226. }
  1227. static void msp34xxg_detect_stereo(struct i2c_client *client)
  1228. {
  1229. struct msp3400c *msp = i2c_get_clientdata(client);
  1230. int status = msp3400c_read(client,
  1231. I2C_MSP3400C_DEM,
  1232. 0x0200 /* STATUS */);
  1233. int is_bilingual = status&0x100;
  1234. int is_stereo = status&0x40;
  1235. msp->rxsubchans = 0;
  1236. if (is_stereo)
  1237. msp->rxsubchans |= V4L2_TUNER_SUB_STEREO;
  1238. else
  1239. msp->rxsubchans |= V4L2_TUNER_SUB_MONO;
  1240. if (is_bilingual) {
  1241. msp->rxsubchans |= V4L2_TUNER_SUB_LANG1|V4L2_TUNER_SUB_LANG2;
  1242. /* I'm supposed to check whether it's SAP or not
  1243. * and set only LANG2/SAP in this case. Yet, the MSP
  1244. * does a lot of work to hide this and handle everything
  1245. * the same way. I don't want to work around it so unless
  1246. * this is a problem, I'll handle SAP just like lang1/lang2.
  1247. */
  1248. }
  1249. dprintk("msp34xxg: status=0x%x, stereo=%d, bilingual=%d -> rxsubchans=%d\n",
  1250. status, is_stereo, is_bilingual, msp->rxsubchans);
  1251. }
  1252. static void msp34xxg_set_audmode(struct i2c_client *client, int audmode)
  1253. {
  1254. struct msp3400c *msp = i2c_get_clientdata(client);
  1255. int source;
  1256. switch (audmode) {
  1257. case V4L2_TUNER_MODE_MONO:
  1258. source=0; /* mono only */
  1259. break;
  1260. case V4L2_TUNER_MODE_STEREO:
  1261. source=1; /* stereo or A|B, see comment in msp34xxg_get_v4l2_stereo() */
  1262. /* problem: that could also mean 2 (scart input) */
  1263. break;
  1264. case V4L2_TUNER_MODE_LANG1:
  1265. source=3; /* stereo or A */
  1266. break;
  1267. case V4L2_TUNER_MODE_LANG2:
  1268. source=4; /* stereo or B */
  1269. break;
  1270. default:
  1271. audmode = 0;
  1272. source = 1;
  1273. break;
  1274. }
  1275. msp->audmode = audmode;
  1276. msp34xxg_set_source(client, source);
  1277. }
  1278. /* ----------------------------------------------------------------------- */
  1279. static int msp_attach(struct i2c_adapter *adap, int addr, int kind);
  1280. static int msp_detach(struct i2c_client *client);
  1281. static int msp_probe(struct i2c_adapter *adap);
  1282. static int msp_command(struct i2c_client *client, unsigned int cmd, void *arg);
  1283. static int msp_suspend(struct device * dev, u32 state, u32 level);
  1284. static int msp_resume(struct device * dev, u32 level);
  1285. static void msp_wake_thread(struct i2c_client *client);
  1286. static struct i2c_driver driver = {
  1287. .owner = THIS_MODULE,
  1288. .name = "i2c msp3400 driver",
  1289. .id = I2C_DRIVERID_MSP3400,
  1290. .flags = I2C_DF_NOTIFY,
  1291. .attach_adapter = msp_probe,
  1292. .detach_client = msp_detach,
  1293. .command = msp_command,
  1294. .driver = {
  1295. .suspend = msp_suspend,
  1296. .resume = msp_resume,
  1297. },
  1298. };
  1299. static struct i2c_client client_template =
  1300. {
  1301. I2C_DEVNAME("(unset)"),
  1302. .flags = I2C_CLIENT_ALLOW_USE,
  1303. .driver = &driver,
  1304. };
  1305. static int msp_attach(struct i2c_adapter *adap, int addr, int kind)
  1306. {
  1307. struct msp3400c *msp;
  1308. struct i2c_client *c;
  1309. int (*thread_func)(void *data) = NULL;
  1310. client_template.adapter = adap;
  1311. client_template.addr = addr;
  1312. if (-1 == msp3400c_reset(&client_template)) {
  1313. dprintk("msp3400: no chip found\n");
  1314. return -1;
  1315. }
  1316. if (NULL == (c = kmalloc(sizeof(struct i2c_client),GFP_KERNEL)))
  1317. return -ENOMEM;
  1318. memcpy(c,&client_template,sizeof(struct i2c_client));
  1319. if (NULL == (msp = kmalloc(sizeof(struct msp3400c),GFP_KERNEL))) {
  1320. kfree(c);
  1321. return -ENOMEM;
  1322. }
  1323. memset(msp,0,sizeof(struct msp3400c));
  1324. msp->volume = 58880; /* 0db gain */
  1325. msp->balance = 32768;
  1326. msp->bass = 32768;
  1327. msp->treble = 32768;
  1328. msp->input = -1;
  1329. msp->muted = 1;
  1330. i2c_set_clientdata(c, msp);
  1331. init_waitqueue_head(&msp->wq);
  1332. if (-1 == msp3400c_reset(c)) {
  1333. kfree(msp);
  1334. kfree(c);
  1335. dprintk("msp3400: no chip found\n");
  1336. return -1;
  1337. }
  1338. msp->rev1 = msp3400c_read(c, I2C_MSP3400C_DFP, 0x1e);
  1339. if (-1 != msp->rev1)
  1340. msp->rev2 = msp3400c_read(c, I2C_MSP3400C_DFP, 0x1f);
  1341. if ((-1 == msp->rev1) || (0 == msp->rev1 && 0 == msp->rev2)) {
  1342. kfree(msp);
  1343. kfree(c);
  1344. printk("msp3400: error while reading chip version\n");
  1345. return -1;
  1346. }
  1347. #if 0
  1348. /* this will turn on a 1kHz beep - might be useful for debugging... */
  1349. msp3400c_write(c,I2C_MSP3400C_DFP, 0x0014, 0x1040);
  1350. #endif
  1351. msp3400c_setvolume(c, msp->muted, msp->volume, msp->balance);
  1352. snprintf(c->name, sizeof(c->name), "MSP34%02d%c-%c%d",
  1353. (msp->rev2>>8)&0xff, (msp->rev1&0xff)+'@',
  1354. ((msp->rev1>>8)&0xff)+'@', msp->rev2&0x1f);
  1355. msp->opmode = opmode;
  1356. if (OPMODE_AUTO == msp->opmode) {
  1357. if (HAVE_SIMPLER(msp))
  1358. msp->opmode = OPMODE_SIMPLER;
  1359. else if (HAVE_SIMPLE(msp))
  1360. msp->opmode = OPMODE_SIMPLE;
  1361. else
  1362. msp->opmode = OPMODE_MANUAL;
  1363. }
  1364. /* hello world :-) */
  1365. printk(KERN_INFO "msp34xx: init: chip=%s",i2c_clientname(c));
  1366. if (HAVE_NICAM(msp))
  1367. printk(" +nicam");
  1368. if (HAVE_SIMPLE(msp))
  1369. printk(" +simple");
  1370. if (HAVE_SIMPLER(msp))
  1371. printk(" +simpler");
  1372. if (HAVE_RADIO(msp))
  1373. printk(" +radio");
  1374. /* version-specific initialization */
  1375. switch (msp->opmode) {
  1376. case OPMODE_MANUAL:
  1377. printk(" mode=manual");
  1378. thread_func = msp3400c_thread;
  1379. break;
  1380. case OPMODE_SIMPLE:
  1381. printk(" mode=simple");
  1382. thread_func = msp3410d_thread;
  1383. break;
  1384. case OPMODE_SIMPLER:
  1385. printk(" mode=simpler");
  1386. thread_func = msp34xxg_thread;
  1387. break;
  1388. }
  1389. printk("\n");
  1390. /* startup control thread if needed */
  1391. if (thread_func) {
  1392. msp->kthread = kthread_run(thread_func, c, "msp34xx");
  1393. if (NULL == msp->kthread)
  1394. printk(KERN_WARNING "msp34xx: kernel_thread() failed\n");
  1395. msp_wake_thread(c);
  1396. }
  1397. /* done */
  1398. i2c_attach_client(c);
  1399. return 0;
  1400. }
  1401. static int msp_detach(struct i2c_client *client)
  1402. {
  1403. struct msp3400c *msp = i2c_get_clientdata(client);
  1404. /* shutdown control thread */
  1405. if (msp->kthread >= 0) {
  1406. msp->restart = 1;
  1407. kthread_stop(msp->kthread);
  1408. }
  1409. msp3400c_reset(client);
  1410. i2c_detach_client(client);
  1411. kfree(msp);
  1412. kfree(client);
  1413. return 0;
  1414. }
  1415. static int msp_probe(struct i2c_adapter *adap)
  1416. {
  1417. if (adap->class & I2C_CLASS_TV_ANALOG)
  1418. return i2c_probe(adap, &addr_data, msp_attach);
  1419. return 0;
  1420. }
  1421. static void msp_wake_thread(struct i2c_client *client)
  1422. {
  1423. struct msp3400c *msp = i2c_get_clientdata(client);
  1424. if (NULL == msp->kthread)
  1425. return;
  1426. msp3400c_setvolume(client,msp->muted,0,0);
  1427. msp->watch_stereo = 0;
  1428. msp->restart = 1;
  1429. wake_up_interruptible(&msp->wq);
  1430. }
  1431. /* ----------------------------------------------------------------------- */
  1432. static int mode_v4l2_to_v4l1(int rxsubchans)
  1433. {
  1434. int mode = 0;
  1435. if (rxsubchans & V4L2_TUNER_SUB_STEREO)
  1436. mode |= VIDEO_SOUND_STEREO;
  1437. if (rxsubchans & V4L2_TUNER_SUB_LANG2)
  1438. mode |= VIDEO_SOUND_LANG2;
  1439. if (rxsubchans & V4L2_TUNER_SUB_LANG1)
  1440. mode |= VIDEO_SOUND_LANG1;
  1441. if (0 == mode)
  1442. mode |= VIDEO_SOUND_MONO;
  1443. return mode;
  1444. }
  1445. static int mode_v4l1_to_v4l2(int mode)
  1446. {
  1447. if (mode & VIDEO_SOUND_STEREO)
  1448. return V4L2_TUNER_MODE_STEREO;
  1449. if (mode & VIDEO_SOUND_LANG2)
  1450. return V4L2_TUNER_MODE_LANG2;
  1451. if (mode & VIDEO_SOUND_LANG1)
  1452. return V4L2_TUNER_MODE_LANG1;
  1453. return V4L2_TUNER_MODE_MONO;
  1454. }
  1455. static void msp_any_detect_stereo(struct i2c_client *client)
  1456. {
  1457. struct msp3400c *msp = i2c_get_clientdata(client);
  1458. switch (msp->opmode) {
  1459. case OPMODE_MANUAL:
  1460. case OPMODE_SIMPLE:
  1461. autodetect_stereo(client);
  1462. break;
  1463. case OPMODE_SIMPLER:
  1464. msp34xxg_detect_stereo(client);
  1465. break;
  1466. }
  1467. }
  1468. static void msp_any_set_audmode(struct i2c_client *client, int audmode)
  1469. {
  1470. struct msp3400c *msp = i2c_get_clientdata(client);
  1471. switch (msp->opmode) {
  1472. case OPMODE_MANUAL:
  1473. case OPMODE_SIMPLE:
  1474. msp->watch_stereo = 0;
  1475. msp3400c_set_audmode(client, audmode);
  1476. break;
  1477. case OPMODE_SIMPLER:
  1478. msp34xxg_set_audmode(client, audmode);
  1479. break;
  1480. }
  1481. }
  1482. static int msp_command(struct i2c_client *client, unsigned int cmd, void *arg)
  1483. {
  1484. struct msp3400c *msp = i2c_get_clientdata(client);
  1485. __u16 *sarg = arg;
  1486. int scart = 0;
  1487. switch (cmd) {
  1488. case AUDC_SET_INPUT:
  1489. dprintk(KERN_DEBUG "msp34xx: AUDC_SET_INPUT(%d)\n",*sarg);
  1490. if (*sarg == msp->input)
  1491. break;
  1492. msp->input = *sarg;
  1493. switch (*sarg) {
  1494. case AUDIO_RADIO:
  1495. /* Hauppauge uses IN2 for the radio */
  1496. msp->mode = MSP_MODE_FM_RADIO;
  1497. scart = SCART_IN2;
  1498. break;
  1499. case AUDIO_EXTERN_1:
  1500. /* IN1 is often used for external input ... */
  1501. msp->mode = MSP_MODE_EXTERN;
  1502. scart = SCART_IN1;
  1503. break;
  1504. case AUDIO_EXTERN_2:
  1505. /* ... sometimes it is IN2 through ;) */
  1506. msp->mode = MSP_MODE_EXTERN;
  1507. scart = SCART_IN2;
  1508. break;
  1509. case AUDIO_TUNER:
  1510. msp->mode = -1;
  1511. break;
  1512. default:
  1513. if (*sarg & AUDIO_MUTE)
  1514. msp3400c_set_scart(client,SCART_MUTE,0);
  1515. break;
  1516. }
  1517. if (scart) {
  1518. msp->rxsubchans = V4L2_TUNER_SUB_STEREO;
  1519. msp->audmode = V4L2_TUNER_MODE_STEREO;
  1520. msp3400c_set_scart(client,scart,0);
  1521. msp3400c_write(client,I2C_MSP3400C_DFP,0x000d,0x1900);
  1522. if (msp->opmode != OPMODE_SIMPLER)
  1523. msp3400c_set_audmode(client, msp->audmode);
  1524. }
  1525. msp_wake_thread(client);
  1526. break;
  1527. case AUDC_SET_RADIO:
  1528. dprintk(KERN_DEBUG "msp34xx: AUDC_SET_RADIO\n");
  1529. msp->norm = VIDEO_MODE_RADIO;
  1530. dprintk(KERN_DEBUG "msp34xx: switching to radio mode\n");
  1531. msp->watch_stereo = 0;
  1532. switch (msp->opmode) {
  1533. case OPMODE_MANUAL:
  1534. /* set msp3400 to FM radio mode */
  1535. msp3400c_setmode(client,MSP_MODE_FM_RADIO);
  1536. msp3400c_setcarrier(client, MSP_CARRIER(10.7),
  1537. MSP_CARRIER(10.7));
  1538. msp3400c_setvolume(client, msp->muted,
  1539. msp->volume, msp->balance);
  1540. break;
  1541. case OPMODE_SIMPLE:
  1542. case OPMODE_SIMPLER:
  1543. /* the thread will do for us */
  1544. msp_wake_thread(client);
  1545. break;
  1546. }
  1547. break;
  1548. /* --- v4l ioctls --- */
  1549. /* take care: bttv does userspace copying, we'll get a
  1550. kernel pointer here... */
  1551. case VIDIOCGAUDIO:
  1552. {
  1553. struct video_audio *va = arg;
  1554. dprintk(KERN_DEBUG "msp34xx: VIDIOCGAUDIO\n");
  1555. va->flags |= VIDEO_AUDIO_VOLUME |
  1556. VIDEO_AUDIO_BASS |
  1557. VIDEO_AUDIO_TREBLE |
  1558. VIDEO_AUDIO_MUTABLE;
  1559. if (msp->muted)
  1560. va->flags |= VIDEO_AUDIO_MUTE;
  1561. va->volume = msp->volume;
  1562. va->balance = (va->volume) ? msp->balance : 32768;
  1563. va->bass = msp->bass;
  1564. va->treble = msp->treble;
  1565. msp_any_detect_stereo(client);
  1566. va->mode = mode_v4l2_to_v4l1(msp->rxsubchans);
  1567. break;
  1568. }
  1569. case VIDIOCSAUDIO:
  1570. {
  1571. struct video_audio *va = arg;
  1572. dprintk(KERN_DEBUG "msp34xx: VIDIOCSAUDIO\n");
  1573. msp->muted = (va->flags & VIDEO_AUDIO_MUTE);
  1574. msp->volume = va->volume;
  1575. msp->balance = va->balance;
  1576. msp->bass = va->bass;
  1577. msp->treble = va->treble;
  1578. msp3400c_setvolume(client, msp->muted,
  1579. msp->volume, msp->balance);
  1580. msp3400c_setbass(client,msp->bass);
  1581. msp3400c_settreble(client,msp->treble);
  1582. if (va->mode != 0 && msp->norm != VIDEO_MODE_RADIO)
  1583. msp_any_set_audmode(client,mode_v4l1_to_v4l2(va->mode));
  1584. break;
  1585. }
  1586. case VIDIOCSCHAN:
  1587. {
  1588. struct video_channel *vc = arg;
  1589. dprintk(KERN_DEBUG "msp34xx: VIDIOCSCHAN (norm=%d)\n",vc->norm);
  1590. msp->norm = vc->norm;
  1591. msp_wake_thread(client);
  1592. break;
  1593. }
  1594. case VIDIOCSFREQ:
  1595. case VIDIOC_S_FREQUENCY:
  1596. {
  1597. /* new channel -- kick audio carrier scan */
  1598. dprintk(KERN_DEBUG "msp34xx: VIDIOCSFREQ\n");
  1599. msp_wake_thread(client);
  1600. break;
  1601. }
  1602. /* --- v4l2 ioctls --- */
  1603. case VIDIOC_G_TUNER:
  1604. {
  1605. struct v4l2_tuner *vt = arg;
  1606. msp_any_detect_stereo(client);
  1607. vt->audmode = msp->audmode;
  1608. vt->rxsubchans = msp->rxsubchans;
  1609. vt->capability = V4L2_TUNER_CAP_STEREO |
  1610. V4L2_TUNER_CAP_LANG1|
  1611. V4L2_TUNER_CAP_LANG2;
  1612. break;
  1613. }
  1614. case VIDIOC_S_TUNER:
  1615. {
  1616. struct v4l2_tuner *vt=(struct v4l2_tuner *)arg;
  1617. /* only set audmode */
  1618. if (vt->audmode != -1 && vt->audmode != 0)
  1619. msp_any_set_audmode(client, vt->audmode);
  1620. break;
  1621. }
  1622. /* msp34xx specific */
  1623. case MSP_SET_MATRIX:
  1624. {
  1625. struct msp_matrix *mspm = arg;
  1626. dprintk(KERN_DEBUG "msp34xx: MSP_SET_MATRIX\n");
  1627. msp3400c_set_scart(client, mspm->input, mspm->output);
  1628. break;
  1629. }
  1630. default:
  1631. /* nothing */
  1632. break;
  1633. }
  1634. return 0;
  1635. }
  1636. static int msp_suspend(struct device * dev, u32 state, u32 level)
  1637. {
  1638. struct i2c_client *c = container_of(dev, struct i2c_client, dev);
  1639. dprintk("msp34xx: suspend\n");
  1640. msp3400c_reset(c);
  1641. return 0;
  1642. }
  1643. static int msp_resume(struct device * dev, u32 level)
  1644. {
  1645. struct i2c_client *c = container_of(dev, struct i2c_client, dev);
  1646. dprintk("msp34xx: resume\n");
  1647. msp_wake_thread(c);
  1648. return 0;
  1649. }
  1650. /* ----------------------------------------------------------------------- */
  1651. static int __init msp3400_init_module(void)
  1652. {
  1653. return i2c_add_driver(&driver);
  1654. }
  1655. static void __exit msp3400_cleanup_module(void)
  1656. {
  1657. i2c_del_driver(&driver);
  1658. }
  1659. module_init(msp3400_init_module);
  1660. module_exit(msp3400_cleanup_module);
  1661. /*
  1662. * Overrides for Emacs so that we follow Linus's tabbing style.
  1663. * ---------------------------------------------------------------------------
  1664. * Local variables:
  1665. * c-basic-offset: 8
  1666. * End:
  1667. */