msp3400-driver.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154
  1. /*
  2. * Programming the mspx4xx 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. * TODO:
  28. * - better SAT support
  29. *
  30. * 980623 Thomas Sailer (sailer@ife.ee.ethz.ch)
  31. * using soundcore instead of OSS
  32. *
  33. * This program is free software; you can redistribute it and/or
  34. * modify it under the terms of the GNU General Public License
  35. * as published by the Free Software Foundation; either version 2
  36. * of the License, or (at your option) any later version.
  37. *
  38. * This program is distributed in the hope that it will be useful,
  39. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  40. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  41. * GNU General Public License for more details.
  42. *
  43. * You should have received a copy of the GNU General Public License
  44. * along with this program; if not, write to the Free Software
  45. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  46. */
  47. #include <linux/kernel.h>
  48. #include <linux/module.h>
  49. #include <linux/slab.h>
  50. #include <linux/i2c.h>
  51. #include <linux/videodev.h>
  52. #include <linux/videodev2.h>
  53. #include <media/v4l2-common.h>
  54. #include <media/audiochip.h>
  55. #include <linux/kthread.h>
  56. #include <linux/suspend.h>
  57. #include "msp3400.h"
  58. /* ---------------------------------------------------------------------- */
  59. MODULE_DESCRIPTION("device driver for msp34xx TV sound processor");
  60. MODULE_AUTHOR("Gerd Knorr");
  61. MODULE_LICENSE("GPL");
  62. /* module parameters */
  63. static int opmode = OPMODE_AUTO;
  64. int debug = 0; /* debug output */
  65. int once = 0; /* no continous stereo monitoring */
  66. int amsound = 0; /* hard-wire AM sound at 6.5 Hz (france),
  67. the autoscan seems work well only with FM... */
  68. int standard = 1; /* Override auto detect of audio standard, if needed. */
  69. int dolby = 0;
  70. int stereo_threshold = 0x190; /* a2 threshold for stereo/bilingual
  71. (msp34xxg only) 0x00a0-0x03c0 */
  72. /* read-only */
  73. module_param(opmode, int, 0444);
  74. /* read-write */
  75. module_param(once, bool, 0644);
  76. module_param(debug, int, 0644);
  77. module_param(stereo_threshold, int, 0644);
  78. module_param(standard, int, 0644);
  79. module_param(amsound, bool, 0644);
  80. module_param(dolby, bool, 0644);
  81. MODULE_PARM_DESC(opmode, "Forces a MSP3400 opmode. 0=Manual, 1=Autodetect, 2=Autodetect and autoselect");
  82. MODULE_PARM_DESC(once, "No continuous stereo monitoring");
  83. MODULE_PARM_DESC(debug, "Enable debug messages [0-3]");
  84. MODULE_PARM_DESC(stereo_threshold, "Sets signal threshold to activate stereo");
  85. MODULE_PARM_DESC(standard, "Specify audio standard: 32 = NTSC, 64 = radio, Default: Autodetect");
  86. MODULE_PARM_DESC(amsound, "Hardwire AM sound at 6.5Hz (France), FM can autoscan");
  87. MODULE_PARM_DESC(dolby, "Activates Dolby processsing");
  88. /* ---------------------------------------------------------------------- */
  89. /* control subaddress */
  90. #define I2C_MSP_CONTROL 0x00
  91. /* demodulator unit subaddress */
  92. #define I2C_MSP_DEM 0x10
  93. /* DSP unit subaddress */
  94. #define I2C_MSP_DSP 0x12
  95. /* Addresses to scan */
  96. static unsigned short normal_i2c[] = { 0x80 >> 1, 0x88 >> 1, I2C_CLIENT_END };
  97. I2C_CLIENT_INSMOD;
  98. /* ----------------------------------------------------------------------- */
  99. /* functions for talking to the MSP3400C Sound processor */
  100. int msp_reset(struct i2c_client *client)
  101. {
  102. /* reset and read revision code */
  103. static u8 reset_off[3] = { I2C_MSP_CONTROL, 0x80, 0x00 };
  104. static u8 reset_on[3] = { I2C_MSP_CONTROL, 0x00, 0x00 };
  105. static u8 write[3] = { I2C_MSP_DSP + 1, 0x00, 0x1e };
  106. u8 read[2];
  107. struct i2c_msg reset[2] = {
  108. { client->addr, I2C_M_IGNORE_NAK, 3, reset_off },
  109. { client->addr, I2C_M_IGNORE_NAK, 3, reset_on },
  110. };
  111. struct i2c_msg test[2] = {
  112. { client->addr, 0, 3, write },
  113. { client->addr, I2C_M_RD, 2, read },
  114. };
  115. v4l_dbg(3, client, "msp_reset\n");
  116. if (i2c_transfer(client->adapter, &reset[0], 1) != 1 ||
  117. i2c_transfer(client->adapter, &reset[1], 1) != 1 ||
  118. i2c_transfer(client->adapter, test, 2) != 2) {
  119. v4l_err(client, "chip reset failed\n");
  120. return -1;
  121. }
  122. return 0;
  123. }
  124. static int msp_read(struct i2c_client *client, int dev, int addr)
  125. {
  126. int err, retval;
  127. u8 write[3];
  128. u8 read[2];
  129. struct i2c_msg msgs[2] = {
  130. { client->addr, 0, 3, write },
  131. { client->addr, I2C_M_RD, 2, read }
  132. };
  133. write[0] = dev + 1;
  134. write[1] = addr >> 8;
  135. write[2] = addr & 0xff;
  136. for (err = 0; err < 3; err++) {
  137. if (i2c_transfer(client->adapter, msgs, 2) == 2)
  138. break;
  139. v4l_warn(client, "I/O error #%d (read 0x%02x/0x%02x)\n", err,
  140. dev, addr);
  141. current->state = TASK_INTERRUPTIBLE;
  142. schedule_timeout(msecs_to_jiffies(10));
  143. }
  144. if (err == 3) {
  145. v4l_warn(client, "giving up, resetting chip. Sound will go off, sorry folks :-|\n");
  146. msp_reset(client);
  147. return -1;
  148. }
  149. retval = read[0] << 8 | read[1];
  150. v4l_dbg(3, client, "msp_read(0x%x, 0x%x): 0x%x\n", dev, addr, retval);
  151. return retval;
  152. }
  153. int msp_read_dem(struct i2c_client *client, int addr)
  154. {
  155. return msp_read(client, I2C_MSP_DEM, addr);
  156. }
  157. int msp_read_dsp(struct i2c_client *client, int addr)
  158. {
  159. return msp_read(client, I2C_MSP_DSP, addr);
  160. }
  161. static int msp_write(struct i2c_client *client, int dev, int addr, int val)
  162. {
  163. int err;
  164. u8 buffer[5];
  165. buffer[0] = dev;
  166. buffer[1] = addr >> 8;
  167. buffer[2] = addr & 0xff;
  168. buffer[3] = val >> 8;
  169. buffer[4] = val & 0xff;
  170. v4l_dbg(3, client, "msp_write(0x%x, 0x%x, 0x%x)\n", dev, addr, val);
  171. for (err = 0; err < 3; err++) {
  172. if (i2c_master_send(client, buffer, 5) == 5)
  173. break;
  174. v4l_warn(client, "I/O error #%d (write 0x%02x/0x%02x)\n", err,
  175. dev, addr);
  176. current->state = TASK_INTERRUPTIBLE;
  177. schedule_timeout(msecs_to_jiffies(10));
  178. }
  179. if (err == 3) {
  180. v4l_warn(client, "giving up, resetting chip. Sound will go off, sorry folks :-|\n");
  181. msp_reset(client);
  182. return -1;
  183. }
  184. return 0;
  185. }
  186. int msp_write_dem(struct i2c_client *client, int addr, int val)
  187. {
  188. return msp_write(client, I2C_MSP_DEM, addr, val);
  189. }
  190. int msp_write_dsp(struct i2c_client *client, int addr, int val)
  191. {
  192. return msp_write(client, I2C_MSP_DSP, addr, val);
  193. }
  194. /* ----------------------------------------------------------------------- *
  195. * bits 9 8 5 - SCART DSP input Select:
  196. * 0 0 0 - SCART 1 to DSP input (reset position)
  197. * 0 1 0 - MONO to DSP input
  198. * 1 0 0 - SCART 2 to DSP input
  199. * 1 1 1 - Mute DSP input
  200. *
  201. * bits 11 10 6 - SCART 1 Output Select:
  202. * 0 0 0 - undefined (reset position)
  203. * 0 1 0 - SCART 2 Input to SCART 1 Output (for devices with 2 SCARTS)
  204. * 1 0 0 - MONO input to SCART 1 Output
  205. * 1 1 0 - SCART 1 DA to SCART 1 Output
  206. * 0 0 1 - SCART 2 DA to SCART 1 Output
  207. * 0 1 1 - SCART 1 Input to SCART 1 Output
  208. * 1 1 1 - Mute SCART 1 Output
  209. *
  210. * bits 13 12 7 - SCART 2 Output Select (for devices with 2 Output SCART):
  211. * 0 0 0 - SCART 1 DA to SCART 2 Output (reset position)
  212. * 0 1 0 - SCART 1 Input to SCART 2 Output
  213. * 1 0 0 - MONO input to SCART 2 Output
  214. * 0 0 1 - SCART 2 DA to SCART 2 Output
  215. * 0 1 1 - SCART 2 Input to SCART 2 Output
  216. * 1 1 0 - Mute SCART 2 Output
  217. *
  218. * Bits 4 to 0 should be zero.
  219. * ----------------------------------------------------------------------- */
  220. static int scarts[3][9] = {
  221. /* MASK IN1 IN2 IN1_DA IN2_DA IN3 IN4 MONO MUTE */
  222. /* SCART DSP Input select */
  223. { 0x0320, 0x0000, 0x0200, -1, -1, 0x0300, 0x0020, 0x0100, 0x0320 },
  224. /* SCART1 Output select */
  225. { 0x0c40, 0x0440, 0x0400, 0x0c00, 0x0040, 0x0000, 0x0840, 0x0800, 0x0c40 },
  226. /* SCART2 Output select */
  227. { 0x3080, 0x1000, 0x1080, 0x0000, 0x0080, 0x2080, 0x3080, 0x2000, 0x3000 },
  228. };
  229. static char *scart_names[] = {
  230. "mask", "in1", "in2", "in1 da", "in2 da", "in3", "in4", "mono", "mute"
  231. };
  232. void msp_set_scart(struct i2c_client *client, int in, int out)
  233. {
  234. struct msp_state *state = i2c_get_clientdata(client);
  235. state->in_scart=in;
  236. if (in >= 1 && in <= 8 && out >= 0 && out <= 2) {
  237. if (-1 == scarts[out][in])
  238. return;
  239. state->acb &= ~scarts[out][SCART_MASK];
  240. state->acb |= scarts[out][in];
  241. } else
  242. state->acb = 0xf60; /* Mute Input and SCART 1 Output */
  243. v4l_dbg(1, client, "scart switch: %s => %d (ACB=0x%04x)\n",
  244. scart_names[in], out, state->acb);
  245. msp_write_dsp(client, 0x13, state->acb);
  246. /* Sets I2S speed 0 = 1.024 Mbps, 1 = 2.048 Mbps */
  247. msp_write_dem(client, 0x40, state->i2s_mode);
  248. }
  249. void msp_set_mute(struct i2c_client *client)
  250. {
  251. v4l_dbg(1, client, "mute audio\n");
  252. msp_write_dsp(client, 0x0000, 0); /* loudspeaker */
  253. msp_write_dsp(client, 0x0006, 0); /* headphones */
  254. }
  255. void msp_set_audio(struct i2c_client *client)
  256. {
  257. struct msp_state *state = i2c_get_clientdata(client);
  258. int val = 0, bal = 0, bass, treble;
  259. if (!state->muted)
  260. val = (state->volume * 0x7f / 65535) << 8;
  261. if (val)
  262. bal = (state->balance / 256) - 128;
  263. bass = ((state->bass - 32768) * 0x60 / 65535) << 8;
  264. treble = ((state->treble - 32768) * 0x60 / 65535) << 8;
  265. v4l_dbg(1, client, "mute=%s volume=%d balance=%d bass=%d treble=%d\n",
  266. state->muted ? "on" : "off", state->volume, state->balance,
  267. state->bass, state->treble);
  268. msp_write_dsp(client, 0x0000, val); /* loudspeaker */
  269. msp_write_dsp(client, 0x0006, val); /* headphones */
  270. msp_write_dsp(client, 0x0007, state->muted ? 0x1 : (val | 0x1));
  271. msp_write_dsp(client, 0x0001, bal << 8);
  272. msp_write_dsp(client, 0x0002, bass); /* loudspeaker */
  273. msp_write_dsp(client, 0x0003, treble); /* loudspeaker */
  274. }
  275. int msp_modus(struct i2c_client *client)
  276. {
  277. struct msp_state *state = i2c_get_clientdata(client);
  278. if (state->radio) {
  279. v4l_dbg(1, client, "video mode selected to Radio\n");
  280. return 0x0003;
  281. }
  282. if (state->std & V4L2_STD_PAL) {
  283. v4l_dbg(1, client, "video mode selected to PAL\n");
  284. #if 1
  285. /* experimental: not sure this works with all chip versions */
  286. return 0x7003;
  287. #else
  288. /* previous value, try this if it breaks ... */
  289. return 0x1003;
  290. #endif
  291. }
  292. if (state->std & V4L2_STD_NTSC) {
  293. v4l_dbg(1, client, "video mode selected to NTSC\n");
  294. return 0x2003;
  295. }
  296. if (state->std & V4L2_STD_SECAM) {
  297. v4l_dbg(1, client, "video mode selected to SECAM\n");
  298. return 0x0003;
  299. }
  300. return 0x0003;
  301. }
  302. /* ------------------------------------------------------------------------ */
  303. static void msp_wake_thread(struct i2c_client *client)
  304. {
  305. struct msp_state *state = i2c_get_clientdata(client);
  306. if (NULL == state->kthread)
  307. return;
  308. msp_set_mute(client);
  309. state->watch_stereo = 0;
  310. state->restart = 1;
  311. wake_up_interruptible(&state->wq);
  312. }
  313. int msp_sleep(struct msp_state *state, int timeout)
  314. {
  315. DECLARE_WAITQUEUE(wait, current);
  316. add_wait_queue(&state->wq, &wait);
  317. if (!kthread_should_stop()) {
  318. if (timeout < 0) {
  319. set_current_state(TASK_INTERRUPTIBLE);
  320. schedule();
  321. } else {
  322. schedule_timeout_interruptible
  323. (msecs_to_jiffies(timeout));
  324. }
  325. }
  326. remove_wait_queue(&state->wq, &wait);
  327. try_to_freeze();
  328. return state->restart;
  329. }
  330. /* ------------------------------------------------------------------------ */
  331. static int msp_mode_v4l2_to_v4l1(int rxsubchans)
  332. {
  333. int mode = 0;
  334. if (rxsubchans & V4L2_TUNER_SUB_STEREO)
  335. mode |= VIDEO_SOUND_STEREO;
  336. if (rxsubchans & V4L2_TUNER_SUB_LANG2)
  337. mode |= VIDEO_SOUND_LANG2;
  338. if (rxsubchans & V4L2_TUNER_SUB_LANG1)
  339. mode |= VIDEO_SOUND_LANG1;
  340. if (mode == 0)
  341. mode |= VIDEO_SOUND_MONO;
  342. return mode;
  343. }
  344. static int msp_mode_v4l1_to_v4l2(int mode)
  345. {
  346. if (mode & VIDEO_SOUND_STEREO)
  347. return V4L2_TUNER_MODE_STEREO;
  348. if (mode & VIDEO_SOUND_LANG2)
  349. return V4L2_TUNER_MODE_LANG2;
  350. if (mode & VIDEO_SOUND_LANG1)
  351. return V4L2_TUNER_MODE_LANG1;
  352. return V4L2_TUNER_MODE_MONO;
  353. }
  354. static void msp_any_detect_stereo(struct i2c_client *client)
  355. {
  356. struct msp_state *state = i2c_get_clientdata(client);
  357. switch (state->opmode) {
  358. case OPMODE_MANUAL:
  359. case OPMODE_AUTODETECT:
  360. autodetect_stereo(client);
  361. break;
  362. case OPMODE_AUTOSELECT:
  363. msp34xxg_detect_stereo(client);
  364. break;
  365. }
  366. }
  367. static struct v4l2_queryctrl msp_qctrl[] = {
  368. {
  369. .id = V4L2_CID_AUDIO_VOLUME,
  370. .name = "Volume",
  371. .minimum = 0,
  372. .maximum = 65535,
  373. .step = 65535/100,
  374. .default_value = 58880,
  375. .flags = 0,
  376. .type = V4L2_CTRL_TYPE_INTEGER,
  377. },{
  378. .id = V4L2_CID_AUDIO_BALANCE,
  379. .name = "Balance",
  380. .minimum = 0,
  381. .maximum = 65535,
  382. .step = 65535/100,
  383. .default_value = 32768,
  384. .flags = 0,
  385. .type = V4L2_CTRL_TYPE_INTEGER,
  386. },{
  387. .id = V4L2_CID_AUDIO_MUTE,
  388. .name = "Mute",
  389. .minimum = 0,
  390. .maximum = 1,
  391. .step = 1,
  392. .default_value = 1,
  393. .flags = 0,
  394. .type = V4L2_CTRL_TYPE_BOOLEAN,
  395. },{
  396. .id = V4L2_CID_AUDIO_BASS,
  397. .name = "Bass",
  398. .minimum = 0,
  399. .maximum = 65535,
  400. .step = 65535/100,
  401. .default_value = 32768,
  402. .type = V4L2_CTRL_TYPE_INTEGER,
  403. },{
  404. .id = V4L2_CID_AUDIO_TREBLE,
  405. .name = "Treble",
  406. .minimum = 0,
  407. .maximum = 65535,
  408. .step = 65535/100,
  409. .default_value = 32768,
  410. .type = V4L2_CTRL_TYPE_INTEGER,
  411. },
  412. };
  413. static void msp_any_set_audmode(struct i2c_client *client, int audmode)
  414. {
  415. struct msp_state *state = i2c_get_clientdata(client);
  416. switch (state->opmode) {
  417. case OPMODE_MANUAL:
  418. case OPMODE_AUTODETECT:
  419. state->watch_stereo = 0;
  420. msp3400c_setstereo(client, audmode);
  421. break;
  422. case OPMODE_AUTOSELECT:
  423. msp34xxg_set_audmode(client, audmode);
  424. break;
  425. }
  426. }
  427. static int msp_get_ctrl(struct i2c_client *client, struct v4l2_control *ctrl)
  428. {
  429. struct msp_state *state = i2c_get_clientdata(client);
  430. switch (ctrl->id) {
  431. case V4L2_CID_AUDIO_MUTE:
  432. ctrl->value = state->muted;
  433. break;
  434. case V4L2_CID_AUDIO_BALANCE:
  435. ctrl->value = state->balance;
  436. break;
  437. case V4L2_CID_AUDIO_BASS:
  438. ctrl->value = state->bass;
  439. break;
  440. case V4L2_CID_AUDIO_TREBLE:
  441. ctrl->value = state->treble;
  442. break;
  443. case V4L2_CID_AUDIO_VOLUME:
  444. ctrl->value = state->volume;
  445. break;
  446. default:
  447. return -EINVAL;
  448. }
  449. return 0;
  450. }
  451. static int msp_set_ctrl(struct i2c_client *client, struct v4l2_control *ctrl)
  452. {
  453. struct msp_state *state = i2c_get_clientdata(client);
  454. switch (ctrl->id) {
  455. case V4L2_CID_AUDIO_MUTE:
  456. if (ctrl->value < 0 || ctrl->value >= 2)
  457. return -ERANGE;
  458. state->muted = ctrl->value;
  459. break;
  460. case V4L2_CID_AUDIO_BASS:
  461. state->bass = ctrl->value;
  462. break;
  463. case V4L2_CID_AUDIO_TREBLE:
  464. state->treble = ctrl->value;
  465. break;
  466. case V4L2_CID_AUDIO_BALANCE:
  467. state->balance = ctrl->value;
  468. break;
  469. case V4L2_CID_AUDIO_VOLUME:
  470. state->volume = ctrl->value;
  471. if (state->volume == 0)
  472. state->balance = 32768;
  473. break;
  474. default:
  475. return -EINVAL;
  476. }
  477. msp_set_audio(client);
  478. return 0;
  479. }
  480. static int msp_command(struct i2c_client *client, unsigned int cmd, void *arg)
  481. {
  482. struct msp_state *state = i2c_get_clientdata(client);
  483. u16 *sarg = arg;
  484. int scart = 0;
  485. if (debug >= 2)
  486. v4l_i2c_print_ioctl(client, cmd);
  487. switch (cmd) {
  488. case AUDC_SET_INPUT:
  489. if (*sarg == state->input)
  490. break;
  491. state->input = *sarg;
  492. switch (*sarg) {
  493. case AUDIO_RADIO:
  494. /* Hauppauge uses IN2 for the radio */
  495. state->mode = MSP_MODE_FM_RADIO;
  496. scart = SCART_IN2;
  497. break;
  498. case AUDIO_EXTERN_1:
  499. /* IN1 is often used for external input ... */
  500. state->mode = MSP_MODE_EXTERN;
  501. scart = SCART_IN1;
  502. break;
  503. case AUDIO_EXTERN_2:
  504. /* ... sometimes it is IN2 through ;) */
  505. state->mode = MSP_MODE_EXTERN;
  506. scart = SCART_IN2;
  507. break;
  508. case AUDIO_TUNER:
  509. state->mode = -1;
  510. break;
  511. default:
  512. if (*sarg & AUDIO_MUTE)
  513. msp_set_scart(client, SCART_MUTE, 0);
  514. break;
  515. }
  516. if (scart) {
  517. state->rxsubchans = V4L2_TUNER_SUB_STEREO;
  518. state->audmode = V4L2_TUNER_MODE_STEREO;
  519. msp_set_scart(client, scart, 0);
  520. msp_write_dsp(client, 0x000d, 0x1900);
  521. if (state->opmode != OPMODE_AUTOSELECT)
  522. msp3400c_setstereo(client, state->audmode);
  523. }
  524. msp_wake_thread(client);
  525. break;
  526. case AUDC_SET_RADIO:
  527. state->radio = 1;
  528. v4l_dbg(1, client, "switching to radio mode\n");
  529. state->watch_stereo = 0;
  530. switch (state->opmode) {
  531. case OPMODE_MANUAL:
  532. /* set msp3400 to FM radio mode */
  533. msp3400c_setmode(client, MSP_MODE_FM_RADIO);
  534. msp3400c_setcarrier(client, MSP_CARRIER(10.7),
  535. MSP_CARRIER(10.7));
  536. msp_set_audio(client);
  537. break;
  538. case OPMODE_AUTODETECT:
  539. case OPMODE_AUTOSELECT:
  540. /* the thread will do for us */
  541. msp_wake_thread(client);
  542. break;
  543. }
  544. break;
  545. /* --- v4l ioctls --- */
  546. /* take care: bttv does userspace copying, we'll get a
  547. kernel pointer here... */
  548. case VIDIOCGAUDIO:
  549. {
  550. struct video_audio *va = arg;
  551. va->flags |= VIDEO_AUDIO_VOLUME |
  552. VIDEO_AUDIO_BASS |
  553. VIDEO_AUDIO_TREBLE |
  554. VIDEO_AUDIO_MUTABLE;
  555. if (state->muted)
  556. va->flags |= VIDEO_AUDIO_MUTE;
  557. if (state->muted)
  558. va->flags |= VIDEO_AUDIO_MUTE;
  559. va->volume = state->volume;
  560. va->balance = state->volume ? state->balance : 32768;
  561. va->bass = state->bass;
  562. va->treble = state->treble;
  563. msp_any_detect_stereo(client);
  564. va->mode = msp_mode_v4l2_to_v4l1(state->rxsubchans);
  565. break;
  566. }
  567. case VIDIOCSAUDIO:
  568. {
  569. struct video_audio *va = arg;
  570. state->muted = (va->flags & VIDEO_AUDIO_MUTE);
  571. state->volume = va->volume;
  572. state->balance = va->balance;
  573. state->bass = va->bass;
  574. state->treble = va->treble;
  575. msp_set_audio(client);
  576. if (va->mode != 0 && state->radio == 0)
  577. msp_any_set_audmode(client, msp_mode_v4l1_to_v4l2(va->mode));
  578. break;
  579. }
  580. case VIDIOCSCHAN:
  581. {
  582. struct video_channel *vc = arg;
  583. state->radio = 0;
  584. if (vc->norm == VIDEO_MODE_PAL)
  585. state->std = V4L2_STD_PAL;
  586. else if (vc->norm == VIDEO_MODE_SECAM)
  587. state->std = V4L2_STD_SECAM;
  588. else
  589. state->std = V4L2_STD_NTSC;
  590. msp_wake_thread(client);
  591. break;
  592. }
  593. case VIDIOCSFREQ:
  594. case VIDIOC_S_FREQUENCY:
  595. {
  596. /* new channel -- kick audio carrier scan */
  597. msp_wake_thread(client);
  598. break;
  599. }
  600. /* msp34xx specific */
  601. case MSP_SET_MATRIX:
  602. {
  603. struct msp_matrix *mspm = arg;
  604. msp_set_scart(client, mspm->input, mspm->output);
  605. break;
  606. }
  607. /* --- v4l2 ioctls --- */
  608. case VIDIOC_S_STD:
  609. {
  610. v4l2_std_id *id = arg;
  611. state->std = *id;
  612. state->radio = 0;
  613. msp_wake_thread(client);
  614. return 0;
  615. }
  616. case VIDIOC_ENUMINPUT:
  617. {
  618. struct v4l2_input *i = arg;
  619. if (i->index != 0)
  620. return -EINVAL;
  621. i->type = V4L2_INPUT_TYPE_TUNER;
  622. switch (i->index) {
  623. case AUDIO_RADIO:
  624. strcpy(i->name, "Radio");
  625. break;
  626. case AUDIO_EXTERN_1:
  627. strcpy(i->name, "Extern 1");
  628. break;
  629. case AUDIO_EXTERN_2:
  630. strcpy(i->name, "Extern 2");
  631. break;
  632. case AUDIO_TUNER:
  633. strcpy(i->name, "Television");
  634. break;
  635. default:
  636. return -EINVAL;
  637. }
  638. return 0;
  639. }
  640. case VIDIOC_G_AUDIO:
  641. {
  642. struct v4l2_audio *a = arg;
  643. memset(a, 0, sizeof(*a));
  644. switch (a->index) {
  645. case AUDIO_RADIO:
  646. strcpy(a->name, "Radio");
  647. break;
  648. case AUDIO_EXTERN_1:
  649. strcpy(a->name, "Extern 1");
  650. break;
  651. case AUDIO_EXTERN_2:
  652. strcpy(a->name, "Extern 2");
  653. break;
  654. case AUDIO_TUNER:
  655. strcpy(a->name, "Television");
  656. break;
  657. default:
  658. return -EINVAL;
  659. }
  660. msp_any_detect_stereo(client);
  661. if (state->audmode == V4L2_TUNER_MODE_STEREO) {
  662. a->capability = V4L2_AUDCAP_STEREO;
  663. }
  664. break;
  665. }
  666. case VIDIOC_S_AUDIO:
  667. {
  668. struct v4l2_audio *sarg = arg;
  669. switch (sarg->index) {
  670. case AUDIO_RADIO:
  671. /* Hauppauge uses IN2 for the radio */
  672. state->mode = MSP_MODE_FM_RADIO;
  673. scart = SCART_IN2;
  674. break;
  675. case AUDIO_EXTERN_1:
  676. /* IN1 is often used for external input ... */
  677. state->mode = MSP_MODE_EXTERN;
  678. scart = SCART_IN1;
  679. break;
  680. case AUDIO_EXTERN_2:
  681. /* ... sometimes it is IN2 through ;) */
  682. state->mode = MSP_MODE_EXTERN;
  683. scart = SCART_IN2;
  684. break;
  685. case AUDIO_TUNER:
  686. state->mode = -1;
  687. break;
  688. }
  689. if (scart) {
  690. state->rxsubchans = V4L2_TUNER_SUB_STEREO;
  691. state->audmode = V4L2_TUNER_MODE_STEREO;
  692. msp_set_scart(client, scart, 0);
  693. msp_write_dsp(client, 0x000d, 0x1900);
  694. }
  695. if (sarg->capability == V4L2_AUDCAP_STEREO) {
  696. state->audmode = V4L2_TUNER_MODE_STEREO;
  697. } else {
  698. state->audmode &= ~V4L2_TUNER_MODE_STEREO;
  699. }
  700. msp_any_set_audmode(client, state->audmode);
  701. msp_wake_thread(client);
  702. break;
  703. }
  704. case VIDIOC_G_TUNER:
  705. {
  706. struct v4l2_tuner *vt = arg;
  707. msp_any_detect_stereo(client);
  708. vt->audmode = state->audmode;
  709. vt->rxsubchans = state->rxsubchans;
  710. vt->capability = V4L2_TUNER_CAP_STEREO |
  711. V4L2_TUNER_CAP_LANG1 | V4L2_TUNER_CAP_LANG2;
  712. break;
  713. }
  714. case VIDIOC_S_TUNER:
  715. {
  716. struct v4l2_tuner *vt = (struct v4l2_tuner *)arg;
  717. /* only set audmode */
  718. if (vt->audmode != -1 && vt->audmode != 0)
  719. msp_any_set_audmode(client, vt->audmode);
  720. break;
  721. }
  722. case VIDIOC_G_AUDOUT:
  723. {
  724. struct v4l2_audioout *a = (struct v4l2_audioout *)arg;
  725. int idx = a->index;
  726. memset(a, 0, sizeof(*a));
  727. switch (idx) {
  728. case 0:
  729. strcpy(a->name, "Scart1 Out");
  730. break;
  731. case 1:
  732. strcpy(a->name, "Scart2 Out");
  733. break;
  734. case 2:
  735. strcpy(a->name, "I2S Out");
  736. break;
  737. default:
  738. return -EINVAL;
  739. }
  740. break;
  741. }
  742. case VIDIOC_S_AUDOUT:
  743. {
  744. struct v4l2_audioout *a = (struct v4l2_audioout *)arg;
  745. if (a->index < 0 || a->index > 2)
  746. return -EINVAL;
  747. v4l_dbg(1, client, "Setting audio out on msp34xx to input %i\n", a->index);
  748. msp_set_scart(client, state->in_scart, a->index + 1);
  749. break;
  750. }
  751. case VIDIOC_INT_I2S_CLOCK_FREQ:
  752. {
  753. u32 *a = (u32 *)arg;
  754. v4l_dbg(1, client, "Setting I2S speed to %d\n", *a);
  755. switch (*a) {
  756. case 1024000:
  757. state->i2s_mode = 0;
  758. break;
  759. case 2048000:
  760. state->i2s_mode = 1;
  761. break;
  762. default:
  763. return -EINVAL;
  764. }
  765. break;
  766. }
  767. case VIDIOC_QUERYCTRL:
  768. {
  769. struct v4l2_queryctrl *qc = arg;
  770. int i;
  771. for (i = 0; i < ARRAY_SIZE(msp_qctrl); i++)
  772. if (qc->id && qc->id == msp_qctrl[i].id) {
  773. memcpy(qc, &msp_qctrl[i], sizeof(*qc));
  774. return 0;
  775. }
  776. return -EINVAL;
  777. }
  778. case VIDIOC_G_CTRL:
  779. return msp_get_ctrl(client, arg);
  780. case VIDIOC_S_CTRL:
  781. return msp_set_ctrl(client, arg);
  782. case VIDIOC_LOG_STATUS:
  783. msp_any_detect_stereo(client);
  784. v4l_info(client, "%s rev1 = 0x%04x rev2 = 0x%04x\n",
  785. client->name, state->rev1, state->rev2);
  786. v4l_info(client, "Audio: volume %d balance %d bass %d treble %d%s\n",
  787. state->volume, state->balance,
  788. state->bass, state->treble,
  789. state->muted ? " (muted)" : "");
  790. v4l_info(client, "Mode: %s (%s%s)\n", msp_standard_mode_name(state->mode),
  791. (state->rxsubchans & V4L2_TUNER_SUB_STEREO) ? "stereo" : "mono",
  792. (state->rxsubchans & V4L2_TUNER_SUB_LANG2) ? ", dual" : "");
  793. v4l_info(client, "ACB: 0x%04x\n", state->acb);
  794. break;
  795. default:
  796. /* nothing */
  797. break;
  798. }
  799. return 0;
  800. }
  801. static int msp_suspend(struct device * dev, pm_message_t state)
  802. {
  803. struct i2c_client *client = container_of(dev, struct i2c_client, dev);
  804. v4l_dbg(1, client, "suspend\n");
  805. msp_reset(client);
  806. return 0;
  807. }
  808. static int msp_resume(struct device * dev)
  809. {
  810. struct i2c_client *client = container_of(dev, struct i2c_client, dev);
  811. v4l_dbg(1, client, "resume\n");
  812. msp_wake_thread(client);
  813. return 0;
  814. }
  815. /* ----------------------------------------------------------------------- */
  816. static struct i2c_driver i2c_driver;
  817. static int msp_attach(struct i2c_adapter *adapter, int address, int kind)
  818. {
  819. struct i2c_client *client;
  820. struct msp_state *state;
  821. int (*thread_func)(void *data) = NULL;
  822. int msp_hard;
  823. int msp_family;
  824. int msp_revision;
  825. int msp_product, msp_prod_hi, msp_prod_lo;
  826. int msp_rom;
  827. client = kmalloc(sizeof(*client), GFP_KERNEL);
  828. if (client == NULL)
  829. return -ENOMEM;
  830. memset(client, 0, sizeof(*client));
  831. client->addr = address;
  832. client->adapter = adapter;
  833. client->driver = &i2c_driver;
  834. snprintf(client->name, sizeof(client->name) - 1, "msp3400");
  835. if (msp_reset(client) == -1) {
  836. v4l_dbg(1, client, "msp3400 not found\n");
  837. kfree(client);
  838. return -1;
  839. }
  840. state = kmalloc(sizeof(*state), GFP_KERNEL);
  841. if (state == NULL) {
  842. kfree(client);
  843. return -ENOMEM;
  844. }
  845. i2c_set_clientdata(client, state);
  846. memset(state, 0, sizeof(*state));
  847. state->std = V4L2_STD_NTSC;
  848. state->volume = 58880; /* 0db gain */
  849. state->balance = 32768; /* 0db gain */
  850. state->bass = 32768;
  851. state->treble = 32768;
  852. state->input = -1;
  853. state->muted = 0;
  854. state->i2s_mode = 0;
  855. init_waitqueue_head(&state->wq);
  856. state->rev1 = msp_read_dsp(client, 0x1e);
  857. if (state->rev1 != -1)
  858. state->rev2 = msp_read_dsp(client, 0x1f);
  859. v4l_dbg(1, client, "rev1=0x%04x, rev2=0x%04x\n", state->rev1, state->rev2);
  860. if (state->rev1 == -1 || (state->rev1 == 0 && state->rev2 == 0)) {
  861. v4l_dbg(1, client, "not an msp3400 (cannot read chip version)\n");
  862. kfree(state);
  863. kfree(client);
  864. return -1;
  865. }
  866. msp_set_audio(client);
  867. msp_family = ((state->rev1 >> 4) & 0x0f) + 3;
  868. msp_product = (state->rev2 >> 8) & 0xff;
  869. msp_prod_hi = msp_product / 10;
  870. msp_prod_lo = msp_product % 10;
  871. msp_revision = (state->rev1 & 0x0f) + '@';
  872. msp_hard = ((state->rev1 >> 8) & 0xff) + '@';
  873. msp_rom = state->rev2 & 0x1f;
  874. snprintf(client->name, sizeof(client->name), "MSP%d4%02d%c-%c%d",
  875. msp_family, msp_product,
  876. msp_revision, msp_hard, msp_rom);
  877. /* Has NICAM support: all mspx41x and mspx45x products have NICAM */
  878. state->has_nicam = msp_prod_hi == 1 || msp_prod_hi == 5;
  879. /* Has radio support: was added with revision G */
  880. state->has_radio = msp_revision >= 'G';
  881. /* Has headphones output: not for stripped down products */
  882. state->has_headphones = msp_prod_lo < 5;
  883. /* Has scart4 input: not in pre D revisions, not in stripped D revs */
  884. state->has_scart4 = msp_family >= 4 || (msp_revision >= 'D' && msp_prod_lo < 5);
  885. /* Has scart2 and scart3 inputs and scart2 output: not in stripped
  886. down products of the '3' family */
  887. state->has_scart23_in_scart2_out = msp_family >= 4 || msp_prod_lo < 5;
  888. /* Has subwoofer output: not in pre-D revs and not in stripped down products */
  889. state->has_subwoofer = msp_revision >= 'D' && msp_prod_lo < 5;
  890. /* Has soundprocessing (bass/treble/balance/loudness/equalizer): not in
  891. stripped down products */
  892. state->has_sound_processing = msp_prod_lo < 7;
  893. /* Has Virtual Dolby Surround: only in msp34x1 */
  894. state->has_virtual_dolby_surround = msp_revision == 'G' && msp_prod_lo == 1;
  895. /* Has Virtual Dolby Surround & Dolby Pro Logic: only in msp34x2 */
  896. state->has_dolby_pro_logic = msp_revision == 'G' && msp_prod_lo == 2;
  897. state->opmode = opmode;
  898. if (state->opmode == OPMODE_AUTO) {
  899. /* MSP revision G and up have both autodetect and autoselect */
  900. if (msp_revision >= 'G')
  901. state->opmode = OPMODE_AUTOSELECT;
  902. /* MSP revision D and up have autodetect */
  903. else if (msp_revision >= 'D')
  904. state->opmode = OPMODE_AUTODETECT;
  905. else
  906. state->opmode = OPMODE_MANUAL;
  907. }
  908. /* hello world :-) */
  909. v4l_info(client, "%s found @ 0x%x (%s)\n", client->name, address << 1, adapter->name);
  910. v4l_info(client, "%s ", client->name);
  911. if (state->has_nicam && state->has_radio)
  912. printk("supports nicam and radio, ");
  913. else if (state->has_nicam)
  914. printk("supports nicam, ");
  915. else if (state->has_radio)
  916. printk("supports radio, ");
  917. printk("mode is ");
  918. /* version-specific initialization */
  919. switch (state->opmode) {
  920. case OPMODE_MANUAL:
  921. printk("manual");
  922. thread_func = msp3400c_thread;
  923. break;
  924. case OPMODE_AUTODETECT:
  925. printk("autodetect");
  926. thread_func = msp3410d_thread;
  927. break;
  928. case OPMODE_AUTOSELECT:
  929. printk("autodetect and autoselect");
  930. thread_func = msp34xxg_thread;
  931. break;
  932. }
  933. printk("\n");
  934. /* startup control thread if needed */
  935. if (thread_func) {
  936. state->kthread = kthread_run(thread_func, client, "msp34xx");
  937. if (state->kthread == NULL)
  938. v4l_warn(client, "kernel_thread() failed\n");
  939. msp_wake_thread(client);
  940. }
  941. /* done */
  942. i2c_attach_client(client);
  943. return 0;
  944. }
  945. static int msp_probe(struct i2c_adapter *adapter)
  946. {
  947. if (adapter->class & I2C_CLASS_TV_ANALOG)
  948. return i2c_probe(adapter, &addr_data, msp_attach);
  949. return 0;
  950. }
  951. static int msp_detach(struct i2c_client *client)
  952. {
  953. struct msp_state *state = i2c_get_clientdata(client);
  954. int err;
  955. /* shutdown control thread */
  956. if (state->kthread) {
  957. state->restart = 1;
  958. kthread_stop(state->kthread);
  959. }
  960. msp_reset(client);
  961. err = i2c_detach_client(client);
  962. if (err) {
  963. return err;
  964. }
  965. kfree(state);
  966. kfree(client);
  967. return 0;
  968. }
  969. /* ----------------------------------------------------------------------- */
  970. /* i2c implementation */
  971. static struct i2c_driver i2c_driver = {
  972. .id = I2C_DRIVERID_MSP3400,
  973. .attach_adapter = msp_probe,
  974. .detach_client = msp_detach,
  975. .command = msp_command,
  976. .driver = {
  977. .name = "msp3400",
  978. .suspend = msp_suspend,
  979. .resume = msp_resume,
  980. },
  981. };
  982. static int __init msp3400_init_module(void)
  983. {
  984. return i2c_add_driver(&i2c_driver);
  985. }
  986. static void __exit msp3400_cleanup_module(void)
  987. {
  988. i2c_del_driver(&i2c_driver);
  989. }
  990. module_init(msp3400_init_module);
  991. module_exit(msp3400_cleanup_module);
  992. /*
  993. * Overrides for Emacs so that we follow Linus's tabbing style.
  994. * ---------------------------------------------------------------------------
  995. * Local variables:
  996. * c-basic-offset: 8
  997. * End:
  998. */