msp3400.c 64 KB

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