msp3400.c 53 KB

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