msp3400.c 63 KB

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