msp3400-driver.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  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/tvaudio.h>
  55. #include <media/msp3400.h>
  56. #include <linux/kthread.h>
  57. #include <linux/suspend.h>
  58. #include "msp3400-driver.h"
  59. /* ---------------------------------------------------------------------- */
  60. MODULE_DESCRIPTION("device driver for msp34xx TV sound processor");
  61. MODULE_AUTHOR("Gerd Knorr");
  62. MODULE_LICENSE("GPL");
  63. /* module parameters */
  64. static int opmode = OPMODE_AUTO;
  65. int msp_debug; /* msp_debug output */
  66. int msp_once; /* no continous stereo monitoring */
  67. int msp_amsound; /* hard-wire AM sound at 6.5 Hz (france),
  68. the autoscan seems work well only with FM... */
  69. int msp_standard = 1; /* Override auto detect of audio msp_standard, if needed. */
  70. int msp_dolby;
  71. int msp_stereo_thresh = 0x190; /* a2 threshold for stereo/bilingual
  72. (msp34xxg only) 0x00a0-0x03c0 */
  73. /* read-only */
  74. module_param(opmode, int, 0444);
  75. /* read-write */
  76. module_param_named(once,msp_once, bool, 0644);
  77. module_param_named(debug,msp_debug, int, 0644);
  78. module_param_named(stereo_threshold,msp_stereo_thresh, int, 0644);
  79. module_param_named(standard,msp_standard, int, 0644);
  80. module_param_named(amsound,msp_amsound, bool, 0644);
  81. module_param_named(dolby,msp_dolby, bool, 0644);
  82. MODULE_PARM_DESC(opmode, "Forces a MSP3400 opmode. 0=Manual, 1=Autodetect, 2=Autodetect and autoselect");
  83. MODULE_PARM_DESC(once, "No continuous stereo monitoring");
  84. MODULE_PARM_DESC(debug, "Enable debug messages [0-3]");
  85. MODULE_PARM_DESC(stereo_threshold, "Sets signal threshold to activate stereo");
  86. MODULE_PARM_DESC(standard, "Specify audio standard: 32 = NTSC, 64 = radio, Default: Autodetect");
  87. MODULE_PARM_DESC(amsound, "Hardwire AM sound at 6.5Hz (France), FM can autoscan");
  88. MODULE_PARM_DESC(dolby, "Activates Dolby processsing");
  89. /* ---------------------------------------------------------------------- */
  90. /* control subaddress */
  91. #define I2C_MSP_CONTROL 0x00
  92. /* demodulator unit subaddress */
  93. #define I2C_MSP_DEM 0x10
  94. /* DSP unit subaddress */
  95. #define I2C_MSP_DSP 0x12
  96. /* Addresses to scan */
  97. static unsigned short normal_i2c[] = { 0x80 >> 1, 0x88 >> 1, I2C_CLIENT_END };
  98. I2C_CLIENT_INSMOD;
  99. /* ----------------------------------------------------------------------- */
  100. /* functions for talking to the MSP3400C Sound processor */
  101. int msp_reset(struct i2c_client *client)
  102. {
  103. /* reset and read revision code */
  104. static u8 reset_off[3] = { I2C_MSP_CONTROL, 0x80, 0x00 };
  105. static u8 reset_on[3] = { I2C_MSP_CONTROL, 0x00, 0x00 };
  106. static u8 write[3] = { I2C_MSP_DSP + 1, 0x00, 0x1e };
  107. u8 read[2];
  108. struct i2c_msg reset[2] = {
  109. { client->addr, I2C_M_IGNORE_NAK, 3, reset_off },
  110. { client->addr, I2C_M_IGNORE_NAK, 3, reset_on },
  111. };
  112. struct i2c_msg test[2] = {
  113. { client->addr, 0, 3, write },
  114. { client->addr, I2C_M_RD, 2, read },
  115. };
  116. v4l_dbg(3, msp_debug, client, "msp_reset\n");
  117. if (i2c_transfer(client->adapter, &reset[0], 1) != 1 ||
  118. i2c_transfer(client->adapter, &reset[1], 1) != 1 ||
  119. i2c_transfer(client->adapter, test, 2) != 2) {
  120. v4l_err(client, "chip reset failed\n");
  121. return -1;
  122. }
  123. return 0;
  124. }
  125. static int msp_read(struct i2c_client *client, int dev, int addr)
  126. {
  127. int err, retval;
  128. u8 write[3];
  129. u8 read[2];
  130. struct i2c_msg msgs[2] = {
  131. { client->addr, 0, 3, write },
  132. { client->addr, I2C_M_RD, 2, read }
  133. };
  134. write[0] = dev + 1;
  135. write[1] = addr >> 8;
  136. write[2] = addr & 0xff;
  137. for (err = 0; err < 3; err++) {
  138. if (i2c_transfer(client->adapter, msgs, 2) == 2)
  139. break;
  140. v4l_warn(client, "I/O error #%d (read 0x%02x/0x%02x)\n", err,
  141. dev, addr);
  142. current->state = TASK_INTERRUPTIBLE;
  143. schedule_timeout(msecs_to_jiffies(10));
  144. }
  145. if (err == 3) {
  146. v4l_warn(client, "giving up, resetting chip. Sound will go off, sorry folks :-|\n");
  147. msp_reset(client);
  148. return -1;
  149. }
  150. retval = read[0] << 8 | read[1];
  151. v4l_dbg(3, msp_debug, client, "msp_read(0x%x, 0x%x): 0x%x\n", dev, addr, retval);
  152. return retval;
  153. }
  154. int msp_read_dem(struct i2c_client *client, int addr)
  155. {
  156. return msp_read(client, I2C_MSP_DEM, addr);
  157. }
  158. int msp_read_dsp(struct i2c_client *client, int addr)
  159. {
  160. return msp_read(client, I2C_MSP_DSP, addr);
  161. }
  162. static int msp_write(struct i2c_client *client, int dev, int addr, int val)
  163. {
  164. int err;
  165. u8 buffer[5];
  166. buffer[0] = dev;
  167. buffer[1] = addr >> 8;
  168. buffer[2] = addr & 0xff;
  169. buffer[3] = val >> 8;
  170. buffer[4] = val & 0xff;
  171. v4l_dbg(3, msp_debug, client, "msp_write(0x%x, 0x%x, 0x%x)\n", dev, addr, val);
  172. for (err = 0; err < 3; err++) {
  173. if (i2c_master_send(client, buffer, 5) == 5)
  174. break;
  175. v4l_warn(client, "I/O error #%d (write 0x%02x/0x%02x)\n", err,
  176. dev, addr);
  177. current->state = TASK_INTERRUPTIBLE;
  178. schedule_timeout(msecs_to_jiffies(10));
  179. }
  180. if (err == 3) {
  181. v4l_warn(client, "giving up, resetting chip. Sound will go off, sorry folks :-|\n");
  182. msp_reset(client);
  183. return -1;
  184. }
  185. return 0;
  186. }
  187. int msp_write_dem(struct i2c_client *client, int addr, int val)
  188. {
  189. return msp_write(client, I2C_MSP_DEM, addr, val);
  190. }
  191. int msp_write_dsp(struct i2c_client *client, int addr, int val)
  192. {
  193. return msp_write(client, I2C_MSP_DSP, addr, val);
  194. }
  195. /* ----------------------------------------------------------------------- *
  196. * bits 9 8 5 - SCART DSP input Select:
  197. * 0 0 0 - SCART 1 to DSP input (reset position)
  198. * 0 1 0 - MONO to DSP input
  199. * 1 0 0 - SCART 2 to DSP input
  200. * 1 1 1 - Mute DSP input
  201. *
  202. * bits 11 10 6 - SCART 1 Output Select:
  203. * 0 0 0 - undefined (reset position)
  204. * 0 1 0 - SCART 2 Input to SCART 1 Output (for devices with 2 SCARTS)
  205. * 1 0 0 - MONO input to SCART 1 Output
  206. * 1 1 0 - SCART 1 DA to SCART 1 Output
  207. * 0 0 1 - SCART 2 DA to SCART 1 Output
  208. * 0 1 1 - SCART 1 Input to SCART 1 Output
  209. * 1 1 1 - Mute SCART 1 Output
  210. *
  211. * bits 13 12 7 - SCART 2 Output Select (for devices with 2 Output SCART):
  212. * 0 0 0 - SCART 1 DA to SCART 2 Output (reset position)
  213. * 0 1 0 - SCART 1 Input to SCART 2 Output
  214. * 1 0 0 - MONO input to SCART 2 Output
  215. * 0 0 1 - SCART 2 DA to SCART 2 Output
  216. * 0 1 1 - SCART 2 Input to SCART 2 Output
  217. * 1 1 0 - Mute SCART 2 Output
  218. *
  219. * Bits 4 to 0 should be zero.
  220. * ----------------------------------------------------------------------- */
  221. static int scarts[3][9] = {
  222. /* MASK IN1 IN2 IN3 IN4 IN1_DA IN2_DA MONO MUTE */
  223. /* SCART DSP Input select */
  224. { 0x0320, 0x0000, 0x0200, 0x0300, 0x0020, -1, -1, 0x0100, 0x0320 },
  225. /* SCART1 Output select */
  226. { 0x0c40, 0x0440, 0x0400, 0x0000, 0x0840, 0x0c00, 0x0040, 0x0800, 0x0c40 },
  227. /* SCART2 Output select */
  228. { 0x3080, 0x1000, 0x1080, 0x2080, 0x3080, 0x0000, 0x0080, 0x2000, 0x3000 },
  229. };
  230. static char *scart_names[] = {
  231. "in1", "in2", "in3", "in4", "in1 da", "in2 da", "mono", "mute"
  232. };
  233. void msp_set_scart(struct i2c_client *client, int in, int out)
  234. {
  235. struct msp_state *state = i2c_get_clientdata(client);
  236. state->in_scart = in;
  237. if (in >= 0 && in <= 7 && out >= 0 && out <= 2) {
  238. if (-1 == scarts[out][in + 1])
  239. return;
  240. state->acb &= ~scarts[out][0];
  241. state->acb |= scarts[out][in + 1];
  242. } else
  243. state->acb = 0xf60; /* Mute Input and SCART 1 Output */
  244. v4l_dbg(1, msp_debug, client, "scart switch: %s => %d (ACB=0x%04x)\n",
  245. scart_names[in], out, state->acb);
  246. msp_write_dsp(client, 0x13, state->acb);
  247. /* Sets I2S speed 0 = 1.024 Mbps, 1 = 2.048 Mbps */
  248. if (state->has_i2s_conf)
  249. msp_write_dem(client, 0x40, state->i2s_mode);
  250. }
  251. void msp_set_audio(struct i2c_client *client)
  252. {
  253. struct msp_state *state = i2c_get_clientdata(client);
  254. int bal = 0, bass, treble, loudness;
  255. int val = 0;
  256. int reallymuted = state->muted | state->scan_in_progress;
  257. if (!reallymuted)
  258. val = (state->volume * 0x7f / 65535) << 8;
  259. v4l_dbg(1, msp_debug, client, "mute=%s scanning=%s volume=%d\n",
  260. state->muted ? "on" : "off", state->scan_in_progress ? "yes" : "no",
  261. state->volume);
  262. msp_write_dsp(client, 0x0000, val);
  263. msp_write_dsp(client, 0x0007, reallymuted ? 0x1 : (val | 0x1));
  264. if (state->has_scart2_out_volume)
  265. msp_write_dsp(client, 0x0040, reallymuted ? 0x1 : (val | 0x1));
  266. if (state->has_headphones)
  267. msp_write_dsp(client, 0x0006, val);
  268. if (!state->has_sound_processing)
  269. return;
  270. if (val)
  271. bal = (u8)((state->balance / 256) - 128);
  272. bass = ((state->bass - 32768) * 0x60 / 65535) << 8;
  273. treble = ((state->treble - 32768) * 0x60 / 65535) << 8;
  274. loudness = state->loudness ? ((5 * 4) << 8) : 0;
  275. v4l_dbg(1, msp_debug, client, "balance=%d bass=%d treble=%d loudness=%d\n",
  276. state->balance, state->bass, state->treble, state->loudness);
  277. msp_write_dsp(client, 0x0001, bal << 8);
  278. msp_write_dsp(client, 0x0002, bass);
  279. msp_write_dsp(client, 0x0003, treble);
  280. msp_write_dsp(client, 0x0004, loudness);
  281. if (!state->has_headphones)
  282. return;
  283. msp_write_dsp(client, 0x0030, bal << 8);
  284. msp_write_dsp(client, 0x0031, bass);
  285. msp_write_dsp(client, 0x0032, treble);
  286. msp_write_dsp(client, 0x0033, loudness);
  287. }
  288. /* ------------------------------------------------------------------------ */
  289. static void msp_wake_thread(struct i2c_client *client)
  290. {
  291. struct msp_state *state = i2c_get_clientdata(client);
  292. if (NULL == state->kthread)
  293. return;
  294. state->watch_stereo = 0;
  295. state->restart = 1;
  296. wake_up_interruptible(&state->wq);
  297. }
  298. int msp_sleep(struct msp_state *state, int timeout)
  299. {
  300. DECLARE_WAITQUEUE(wait, current);
  301. add_wait_queue(&state->wq, &wait);
  302. if (!kthread_should_stop()) {
  303. if (timeout < 0) {
  304. set_current_state(TASK_INTERRUPTIBLE);
  305. schedule();
  306. } else {
  307. schedule_timeout_interruptible
  308. (msecs_to_jiffies(timeout));
  309. }
  310. }
  311. remove_wait_queue(&state->wq, &wait);
  312. try_to_freeze();
  313. return state->restart;
  314. }
  315. /* ------------------------------------------------------------------------ */
  316. #ifdef CONFIG_VIDEO_V4L1
  317. static int msp_mode_v4l2_to_v4l1(int rxsubchans, int audmode)
  318. {
  319. if (rxsubchans == V4L2_TUNER_SUB_MONO)
  320. return VIDEO_SOUND_MONO;
  321. if (rxsubchans == V4L2_TUNER_SUB_STEREO)
  322. return VIDEO_SOUND_STEREO;
  323. if (audmode == V4L2_TUNER_MODE_LANG2)
  324. return VIDEO_SOUND_LANG2;
  325. return VIDEO_SOUND_LANG1;
  326. }
  327. static int msp_mode_v4l1_to_v4l2(int mode)
  328. {
  329. if (mode & VIDEO_SOUND_STEREO)
  330. return V4L2_TUNER_MODE_STEREO;
  331. if (mode & VIDEO_SOUND_LANG2)
  332. return V4L2_TUNER_MODE_LANG2;
  333. if (mode & VIDEO_SOUND_LANG1)
  334. return V4L2_TUNER_MODE_LANG1;
  335. return V4L2_TUNER_MODE_MONO;
  336. }
  337. #endif
  338. static int msp_get_ctrl(struct i2c_client *client, struct v4l2_control *ctrl)
  339. {
  340. struct msp_state *state = i2c_get_clientdata(client);
  341. switch (ctrl->id) {
  342. case V4L2_CID_AUDIO_VOLUME:
  343. ctrl->value = state->volume;
  344. break;
  345. case V4L2_CID_AUDIO_MUTE:
  346. ctrl->value = state->muted;
  347. break;
  348. case V4L2_CID_AUDIO_BALANCE:
  349. if (!state->has_sound_processing)
  350. return -EINVAL;
  351. ctrl->value = state->balance;
  352. break;
  353. case V4L2_CID_AUDIO_BASS:
  354. if (!state->has_sound_processing)
  355. return -EINVAL;
  356. ctrl->value = state->bass;
  357. break;
  358. case V4L2_CID_AUDIO_TREBLE:
  359. if (!state->has_sound_processing)
  360. return -EINVAL;
  361. ctrl->value = state->treble;
  362. break;
  363. case V4L2_CID_AUDIO_LOUDNESS:
  364. if (!state->has_sound_processing)
  365. return -EINVAL;
  366. ctrl->value = state->loudness;
  367. break;
  368. default:
  369. return -EINVAL;
  370. }
  371. return 0;
  372. }
  373. static int msp_set_ctrl(struct i2c_client *client, struct v4l2_control *ctrl)
  374. {
  375. struct msp_state *state = i2c_get_clientdata(client);
  376. switch (ctrl->id) {
  377. case V4L2_CID_AUDIO_VOLUME:
  378. state->volume = ctrl->value;
  379. if (state->volume == 0)
  380. state->balance = 32768;
  381. break;
  382. case V4L2_CID_AUDIO_MUTE:
  383. if (ctrl->value < 0 || ctrl->value >= 2)
  384. return -ERANGE;
  385. state->muted = ctrl->value;
  386. break;
  387. case V4L2_CID_AUDIO_BASS:
  388. if (!state->has_sound_processing)
  389. return -EINVAL;
  390. state->bass = ctrl->value;
  391. break;
  392. case V4L2_CID_AUDIO_TREBLE:
  393. if (!state->has_sound_processing)
  394. return -EINVAL;
  395. state->treble = ctrl->value;
  396. break;
  397. case V4L2_CID_AUDIO_LOUDNESS:
  398. if (!state->has_sound_processing)
  399. return -EINVAL;
  400. state->loudness = ctrl->value;
  401. break;
  402. case V4L2_CID_AUDIO_BALANCE:
  403. if (!state->has_sound_processing)
  404. return -EINVAL;
  405. state->balance = ctrl->value;
  406. break;
  407. default:
  408. return -EINVAL;
  409. }
  410. msp_set_audio(client);
  411. return 0;
  412. }
  413. static int msp_command(struct i2c_client *client, unsigned int cmd, void *arg)
  414. {
  415. struct msp_state *state = i2c_get_clientdata(client);
  416. if (msp_debug >= 2)
  417. v4l_i2c_print_ioctl(client, cmd);
  418. switch (cmd) {
  419. case AUDC_SET_RADIO:
  420. if (state->radio)
  421. return 0;
  422. state->radio = 1;
  423. v4l_dbg(1, msp_debug, client, "switching to radio mode\n");
  424. state->watch_stereo = 0;
  425. switch (state->opmode) {
  426. case OPMODE_MANUAL:
  427. /* set msp3400 to FM radio mode */
  428. msp3400c_set_mode(client, MSP_MODE_FM_RADIO);
  429. msp3400c_set_carrier(client, MSP_CARRIER(10.7),
  430. MSP_CARRIER(10.7));
  431. msp_set_audio(client);
  432. break;
  433. case OPMODE_AUTODETECT:
  434. case OPMODE_AUTOSELECT:
  435. /* the thread will do for us */
  436. msp_wake_thread(client);
  437. break;
  438. }
  439. break;
  440. /* --- v4l ioctls --- */
  441. /* take care: bttv does userspace copying, we'll get a
  442. kernel pointer here... */
  443. #ifdef CONFIG_VIDEO_V4L1
  444. case VIDIOCGAUDIO:
  445. {
  446. struct video_audio *va = arg;
  447. va->flags |= VIDEO_AUDIO_VOLUME | VIDEO_AUDIO_MUTABLE;
  448. if (state->has_sound_processing)
  449. va->flags |= VIDEO_AUDIO_BALANCE |
  450. VIDEO_AUDIO_BASS |
  451. VIDEO_AUDIO_TREBLE;
  452. if (state->muted)
  453. va->flags |= VIDEO_AUDIO_MUTE;
  454. va->volume = state->volume;
  455. va->balance = state->volume ? state->balance : 32768;
  456. va->bass = state->bass;
  457. va->treble = state->treble;
  458. if (state->radio)
  459. break;
  460. if (state->opmode == OPMODE_AUTOSELECT)
  461. msp_detect_stereo(client);
  462. va->mode = msp_mode_v4l2_to_v4l1(state->rxsubchans, state->audmode);
  463. break;
  464. }
  465. case VIDIOCSAUDIO:
  466. {
  467. struct video_audio *va = arg;
  468. state->muted = (va->flags & VIDEO_AUDIO_MUTE);
  469. state->volume = va->volume;
  470. state->balance = va->balance;
  471. state->bass = va->bass;
  472. state->treble = va->treble;
  473. msp_set_audio(client);
  474. if (va->mode != 0 && state->radio == 0 &&
  475. state->audmode != msp_mode_v4l1_to_v4l2(va->mode)) {
  476. state->audmode = msp_mode_v4l1_to_v4l2(va->mode);
  477. msp_set_audmode(client);
  478. }
  479. break;
  480. }
  481. case VIDIOCSCHAN:
  482. {
  483. struct video_channel *vc = arg;
  484. int update = 0;
  485. v4l2_std_id std;
  486. if (state->radio)
  487. update = 1;
  488. state->radio = 0;
  489. if (vc->norm == VIDEO_MODE_PAL)
  490. std = V4L2_STD_PAL;
  491. else if (vc->norm == VIDEO_MODE_SECAM)
  492. std = V4L2_STD_SECAM;
  493. else
  494. std = V4L2_STD_NTSC;
  495. if (std != state->v4l2_std) {
  496. state->v4l2_std = std;
  497. update = 1;
  498. }
  499. if (update)
  500. msp_wake_thread(client);
  501. break;
  502. }
  503. case VIDIOCSFREQ:
  504. {
  505. /* new channel -- kick audio carrier scan */
  506. msp_wake_thread(client);
  507. break;
  508. }
  509. #endif
  510. case VIDIOC_S_FREQUENCY:
  511. {
  512. /* new channel -- kick audio carrier scan */
  513. msp_wake_thread(client);
  514. break;
  515. }
  516. /* --- v4l2 ioctls --- */
  517. case VIDIOC_S_STD:
  518. {
  519. v4l2_std_id *id = arg;
  520. int update = state->radio || state->v4l2_std != *id;
  521. state->v4l2_std = *id;
  522. state->radio = 0;
  523. if (update)
  524. msp_wake_thread(client);
  525. return 0;
  526. }
  527. case VIDIOC_INT_G_AUDIO_ROUTING:
  528. {
  529. struct v4l2_routing *rt = arg;
  530. *rt = state->routing;
  531. break;
  532. }
  533. case VIDIOC_INT_S_AUDIO_ROUTING:
  534. {
  535. struct v4l2_routing *rt = arg;
  536. int tuner = (rt->input >> 3) & 1;
  537. int sc_in = rt->input & 0x7;
  538. int sc1_out = rt->output & 0xf;
  539. int sc2_out = (rt->output >> 4) & 0xf;
  540. u16 val, reg;
  541. int i;
  542. int extern_input = 1;
  543. if (state->routing.input == rt->input &&
  544. state->routing.output == rt->output)
  545. break;
  546. state->routing = *rt;
  547. /* check if the tuner input is used */
  548. for (i = 0; i < 5; i++) {
  549. if (((rt->input >> (4 + i * 4)) & 0xf) == 0)
  550. extern_input = 0;
  551. }
  552. if (extern_input)
  553. state->mode = MSP_MODE_EXTERN;
  554. else
  555. state->mode = MSP_MODE_AM_DETECT;
  556. msp_set_scart(client, sc_in, 0);
  557. msp_set_scart(client, sc1_out, 1);
  558. msp_set_scart(client, sc2_out, 2);
  559. msp_set_audmode(client);
  560. reg = (state->opmode == OPMODE_AUTOSELECT) ? 0x30 : 0xbb;
  561. val = msp_read_dem(client, reg);
  562. msp_write_dem(client, reg, (val & ~0x100) | (tuner << 8));
  563. /* wake thread when a new input is chosen */
  564. msp_wake_thread(client);
  565. break;
  566. }
  567. case VIDIOC_G_TUNER:
  568. {
  569. struct v4l2_tuner *vt = arg;
  570. if (state->radio)
  571. break;
  572. if (state->opmode == OPMODE_AUTOSELECT)
  573. msp_detect_stereo(client);
  574. vt->audmode = state->audmode;
  575. vt->rxsubchans = state->rxsubchans;
  576. vt->capability |= V4L2_TUNER_CAP_STEREO |
  577. V4L2_TUNER_CAP_LANG1 | V4L2_TUNER_CAP_LANG2;
  578. break;
  579. }
  580. case VIDIOC_S_TUNER:
  581. {
  582. struct v4l2_tuner *vt = (struct v4l2_tuner *)arg;
  583. if (state->radio) /* TODO: add mono/stereo support for radio */
  584. break;
  585. if (state->audmode == vt->audmode)
  586. break;
  587. state->audmode = vt->audmode;
  588. /* only set audmode */
  589. msp_set_audmode(client);
  590. break;
  591. }
  592. case VIDIOC_INT_I2S_CLOCK_FREQ:
  593. {
  594. u32 *a = (u32 *)arg;
  595. v4l_dbg(1, msp_debug, client, "Setting I2S speed to %d\n", *a);
  596. switch (*a) {
  597. case 1024000:
  598. state->i2s_mode = 0;
  599. break;
  600. case 2048000:
  601. state->i2s_mode = 1;
  602. break;
  603. default:
  604. return -EINVAL;
  605. }
  606. break;
  607. }
  608. case VIDIOC_QUERYCTRL:
  609. {
  610. struct v4l2_queryctrl *qc = arg;
  611. switch (qc->id) {
  612. case V4L2_CID_AUDIO_VOLUME:
  613. case V4L2_CID_AUDIO_MUTE:
  614. return v4l2_ctrl_query_fill_std(qc);
  615. default:
  616. break;
  617. }
  618. if (!state->has_sound_processing)
  619. return -EINVAL;
  620. switch (qc->id) {
  621. case V4L2_CID_AUDIO_LOUDNESS:
  622. case V4L2_CID_AUDIO_BALANCE:
  623. case V4L2_CID_AUDIO_BASS:
  624. case V4L2_CID_AUDIO_TREBLE:
  625. return v4l2_ctrl_query_fill_std(qc);
  626. default:
  627. return -EINVAL;
  628. }
  629. }
  630. case VIDIOC_G_CTRL:
  631. return msp_get_ctrl(client, arg);
  632. case VIDIOC_S_CTRL:
  633. return msp_set_ctrl(client, arg);
  634. case VIDIOC_LOG_STATUS:
  635. {
  636. const char *p;
  637. if (state->opmode == OPMODE_AUTOSELECT)
  638. msp_detect_stereo(client);
  639. v4l_info(client, "%s rev1 = 0x%04x rev2 = 0x%04x\n",
  640. client->name, state->rev1, state->rev2);
  641. v4l_info(client, "Audio: volume %d%s\n",
  642. state->volume, state->muted ? " (muted)" : "");
  643. if (state->has_sound_processing) {
  644. v4l_info(client, "Audio: balance %d bass %d treble %d loudness %s\n",
  645. state->balance, state->bass, state->treble,
  646. state->loudness ? "on" : "off");
  647. }
  648. switch (state->mode) {
  649. case MSP_MODE_AM_DETECT: p = "AM (for carrier detect)"; break;
  650. case MSP_MODE_FM_RADIO: p = "FM Radio"; break;
  651. case MSP_MODE_FM_TERRA: p = "Terrestial FM-mono + FM-stereo"; break;
  652. case MSP_MODE_FM_SAT: p = "Satellite FM-mono"; break;
  653. case MSP_MODE_FM_NICAM1: p = "NICAM/FM (B/G, D/K)"; break;
  654. case MSP_MODE_FM_NICAM2: p = "NICAM/FM (I)"; break;
  655. case MSP_MODE_AM_NICAM: p = "NICAM/AM (L)"; break;
  656. case MSP_MODE_BTSC: p = "BTSC"; break;
  657. case MSP_MODE_EXTERN: p = "External input"; break;
  658. default: p = "unknown"; break;
  659. }
  660. if (state->mode == MSP_MODE_EXTERN) {
  661. v4l_info(client, "Mode: %s\n", p);
  662. } else if (state->opmode == OPMODE_MANUAL) {
  663. v4l_info(client, "Mode: %s (%s%s)\n", p,
  664. (state->rxsubchans & V4L2_TUNER_SUB_STEREO) ? "stereo" : "mono",
  665. (state->rxsubchans & V4L2_TUNER_SUB_LANG2) ? ", dual" : "");
  666. } else {
  667. if (state->opmode == OPMODE_AUTODETECT)
  668. v4l_info(client, "Mode: %s\n", p);
  669. v4l_info(client, "Standard: %s (%s%s)\n",
  670. msp_standard_std_name(state->std),
  671. (state->rxsubchans & V4L2_TUNER_SUB_STEREO) ? "stereo" : "mono",
  672. (state->rxsubchans & V4L2_TUNER_SUB_LANG2) ? ", dual" : "");
  673. }
  674. v4l_info(client, "Audmode: 0x%04x\n", state->audmode);
  675. v4l_info(client, "Routing: 0x%08x (input) 0x%08x (output)\n",
  676. state->routing.input, state->routing.output);
  677. v4l_info(client, "ACB: 0x%04x\n", state->acb);
  678. break;
  679. }
  680. default:
  681. /* unknown */
  682. return -EINVAL;
  683. }
  684. return 0;
  685. }
  686. static int msp_suspend(struct device * dev, pm_message_t state)
  687. {
  688. struct i2c_client *client = container_of(dev, struct i2c_client, dev);
  689. v4l_dbg(1, msp_debug, client, "suspend\n");
  690. msp_reset(client);
  691. return 0;
  692. }
  693. static int msp_resume(struct device * dev)
  694. {
  695. struct i2c_client *client = container_of(dev, struct i2c_client, dev);
  696. v4l_dbg(1, msp_debug, client, "resume\n");
  697. msp_wake_thread(client);
  698. return 0;
  699. }
  700. /* ----------------------------------------------------------------------- */
  701. static struct i2c_driver i2c_driver;
  702. static int msp_attach(struct i2c_adapter *adapter, int address, int kind)
  703. {
  704. struct i2c_client *client;
  705. struct msp_state *state;
  706. int (*thread_func)(void *data) = NULL;
  707. int msp_hard;
  708. int msp_family;
  709. int msp_revision;
  710. int msp_product, msp_prod_hi, msp_prod_lo;
  711. int msp_rom;
  712. client = kmalloc(sizeof(*client), GFP_KERNEL);
  713. if (client == NULL)
  714. return -ENOMEM;
  715. memset(client, 0, sizeof(*client));
  716. client->addr = address;
  717. client->adapter = adapter;
  718. client->driver = &i2c_driver;
  719. snprintf(client->name, sizeof(client->name) - 1, "msp3400");
  720. if (msp_reset(client) == -1) {
  721. v4l_dbg(1, msp_debug, client, "msp3400 not found\n");
  722. kfree(client);
  723. return -1;
  724. }
  725. state = kmalloc(sizeof(*state), GFP_KERNEL);
  726. if (state == NULL) {
  727. kfree(client);
  728. return -ENOMEM;
  729. }
  730. i2c_set_clientdata(client, state);
  731. memset(state, 0, sizeof(*state));
  732. state->v4l2_std = V4L2_STD_NTSC;
  733. state->audmode = V4L2_TUNER_MODE_STEREO;
  734. state->volume = 58880; /* 0db gain */
  735. state->balance = 32768; /* 0db gain */
  736. state->bass = 32768;
  737. state->treble = 32768;
  738. state->loudness = 0;
  739. state->input = -1;
  740. state->muted = 0;
  741. state->i2s_mode = 0;
  742. init_waitqueue_head(&state->wq);
  743. /* These are the reset input/output positions */
  744. state->routing.input = MSP_INPUT_DEFAULT;
  745. state->routing.output = MSP_OUTPUT_DEFAULT;
  746. state->rev1 = msp_read_dsp(client, 0x1e);
  747. if (state->rev1 != -1)
  748. state->rev2 = msp_read_dsp(client, 0x1f);
  749. v4l_dbg(1, msp_debug, client, "rev1=0x%04x, rev2=0x%04x\n", state->rev1, state->rev2);
  750. if (state->rev1 == -1 || (state->rev1 == 0 && state->rev2 == 0)) {
  751. v4l_dbg(1, msp_debug, client, "not an msp3400 (cannot read chip version)\n");
  752. kfree(state);
  753. kfree(client);
  754. return -1;
  755. }
  756. msp_set_audio(client);
  757. msp_family = ((state->rev1 >> 4) & 0x0f) + 3;
  758. msp_product = (state->rev2 >> 8) & 0xff;
  759. msp_prod_hi = msp_product / 10;
  760. msp_prod_lo = msp_product % 10;
  761. msp_revision = (state->rev1 & 0x0f) + '@';
  762. msp_hard = ((state->rev1 >> 8) & 0xff) + '@';
  763. msp_rom = state->rev2 & 0x1f;
  764. snprintf(client->name, sizeof(client->name), "MSP%d4%02d%c-%c%d",
  765. msp_family, msp_product,
  766. msp_revision, msp_hard, msp_rom);
  767. /* Has NICAM support: all mspx41x and mspx45x products have NICAM */
  768. state->has_nicam = msp_prod_hi == 1 || msp_prod_hi == 5;
  769. /* Has radio support: was added with revision G */
  770. state->has_radio = msp_revision >= 'G';
  771. /* Has headphones output: not for stripped down products */
  772. state->has_headphones = msp_prod_lo < 5;
  773. /* Has scart2 input: not in stripped down products of the '3' family */
  774. state->has_scart2 = msp_family >= 4 || msp_prod_lo < 7;
  775. /* Has scart3 input: not in stripped down products of the '3' family */
  776. state->has_scart3 = msp_family >= 4 || msp_prod_lo < 5;
  777. /* Has scart4 input: not in pre D revisions, not in stripped D revs */
  778. state->has_scart4 = msp_family >= 4 || (msp_revision >= 'D' && msp_prod_lo < 5);
  779. /* Has scart2 output: not in stripped down products of the '3' family */
  780. state->has_scart2_out = msp_family >= 4 || msp_prod_lo < 5;
  781. /* Has scart2 a volume control? Not in pre-D revisions. */
  782. state->has_scart2_out_volume = msp_revision > 'C' && state->has_scart2_out;
  783. /* Has a configurable i2s out? */
  784. state->has_i2s_conf = msp_revision >= 'G' && msp_prod_lo < 7;
  785. /* Has subwoofer output: not in pre-D revs and not in stripped down products */
  786. state->has_subwoofer = msp_revision >= 'D' && msp_prod_lo < 5;
  787. /* Has soundprocessing (bass/treble/balance/loudness/equalizer): not in
  788. stripped down products */
  789. state->has_sound_processing = msp_prod_lo < 7;
  790. /* Has Virtual Dolby Surround: only in msp34x1 */
  791. state->has_virtual_dolby_surround = msp_revision == 'G' && msp_prod_lo == 1;
  792. /* Has Virtual Dolby Surround & Dolby Pro Logic: only in msp34x2 */
  793. state->has_dolby_pro_logic = msp_revision == 'G' && msp_prod_lo == 2;
  794. /* The msp343xG supports BTSC only and cannot do Automatic Standard Detection. */
  795. state->force_btsc = msp_family == 3 && msp_revision == 'G' && msp_prod_hi == 3;
  796. state->opmode = opmode;
  797. if (state->opmode == OPMODE_AUTO) {
  798. /* MSP revision G and up have both autodetect and autoselect */
  799. if (msp_revision >= 'G')
  800. state->opmode = OPMODE_AUTOSELECT;
  801. /* MSP revision D and up have autodetect */
  802. else if (msp_revision >= 'D')
  803. state->opmode = OPMODE_AUTODETECT;
  804. else
  805. state->opmode = OPMODE_MANUAL;
  806. }
  807. /* hello world :-) */
  808. v4l_info(client, "%s found @ 0x%x (%s)\n", client->name, address << 1, adapter->name);
  809. v4l_info(client, "%s ", client->name);
  810. if (state->has_nicam && state->has_radio)
  811. printk("supports nicam and radio, ");
  812. else if (state->has_nicam)
  813. printk("supports nicam, ");
  814. else if (state->has_radio)
  815. printk("supports radio, ");
  816. printk("mode is ");
  817. /* version-specific initialization */
  818. switch (state->opmode) {
  819. case OPMODE_MANUAL:
  820. printk("manual");
  821. thread_func = msp3400c_thread;
  822. break;
  823. case OPMODE_AUTODETECT:
  824. printk("autodetect");
  825. thread_func = msp3410d_thread;
  826. break;
  827. case OPMODE_AUTOSELECT:
  828. printk("autodetect and autoselect");
  829. thread_func = msp34xxg_thread;
  830. break;
  831. }
  832. printk("\n");
  833. /* startup control thread if needed */
  834. if (thread_func) {
  835. state->kthread = kthread_run(thread_func, client, "msp34xx");
  836. if (state->kthread == NULL)
  837. v4l_warn(client, "kernel_thread() failed\n");
  838. msp_wake_thread(client);
  839. }
  840. /* done */
  841. i2c_attach_client(client);
  842. return 0;
  843. }
  844. static int msp_probe(struct i2c_adapter *adapter)
  845. {
  846. if (adapter->class & I2C_CLASS_TV_ANALOG)
  847. return i2c_probe(adapter, &addr_data, msp_attach);
  848. return 0;
  849. }
  850. static int msp_detach(struct i2c_client *client)
  851. {
  852. struct msp_state *state = i2c_get_clientdata(client);
  853. int err;
  854. /* shutdown control thread */
  855. if (state->kthread) {
  856. state->restart = 1;
  857. kthread_stop(state->kthread);
  858. }
  859. msp_reset(client);
  860. err = i2c_detach_client(client);
  861. if (err) {
  862. return err;
  863. }
  864. kfree(state);
  865. kfree(client);
  866. return 0;
  867. }
  868. /* ----------------------------------------------------------------------- */
  869. /* i2c implementation */
  870. static struct i2c_driver i2c_driver = {
  871. .id = I2C_DRIVERID_MSP3400,
  872. .attach_adapter = msp_probe,
  873. .detach_client = msp_detach,
  874. .command = msp_command,
  875. .driver = {
  876. .name = "msp3400",
  877. .suspend = msp_suspend,
  878. .resume = msp_resume,
  879. },
  880. };
  881. static int __init msp3400_init_module(void)
  882. {
  883. return i2c_add_driver(&i2c_driver);
  884. }
  885. static void __exit msp3400_cleanup_module(void)
  886. {
  887. i2c_del_driver(&i2c_driver);
  888. }
  889. module_init(msp3400_init_module);
  890. module_exit(msp3400_cleanup_module);
  891. /*
  892. * Overrides for Emacs so that we follow Linus's tabbing style.
  893. * ---------------------------------------------------------------------------
  894. * Local variables:
  895. * c-basic-offset: 8
  896. * End:
  897. */