msp3400.c 63 KB

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