msp3400.c 51 KB

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