sony-btf-mpx.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*
  2. * Copyright (C) 2005-2006 Micronas USA Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License (Version 2) as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/init.h>
  15. #include <linux/i2c.h>
  16. #include <linux/videodev2.h>
  17. #include <media/tuner.h>
  18. #include <media/v4l2-common.h>
  19. #include <media/v4l2-ioctl.h>
  20. #include <media/v4l2-device.h>
  21. #include <linux/slab.h>
  22. MODULE_DESCRIPTION("sony-btf-mpx driver");
  23. MODULE_LICENSE("GPL v2");
  24. static int debug;
  25. module_param(debug, int, 0644);
  26. MODULE_PARM_DESC(debug, "debug level 0=off(default) 1=on");
  27. /* #define MPX_DEBUG */
  28. /*
  29. * Note:
  30. *
  31. * AS(IF/MPX) pin: LOW HIGH/OPEN
  32. * IF/MPX address: 0x42/0x40 0x43/0x44
  33. */
  34. static int force_mpx_mode = -1;
  35. module_param(force_mpx_mode, int, 0644);
  36. struct sony_btf_mpx {
  37. struct v4l2_subdev sd;
  38. int mpxmode;
  39. u32 audmode;
  40. };
  41. static inline struct sony_btf_mpx *to_state(struct v4l2_subdev *sd)
  42. {
  43. return container_of(sd, struct sony_btf_mpx, sd);
  44. }
  45. static int mpx_write(struct i2c_client *client, int dev, int addr, int val)
  46. {
  47. u8 buffer[5];
  48. struct i2c_msg msg;
  49. buffer[0] = dev;
  50. buffer[1] = addr >> 8;
  51. buffer[2] = addr & 0xff;
  52. buffer[3] = val >> 8;
  53. buffer[4] = val & 0xff;
  54. msg.addr = client->addr;
  55. msg.flags = 0;
  56. msg.len = 5;
  57. msg.buf = buffer;
  58. i2c_transfer(client->adapter, &msg, 1);
  59. return 0;
  60. }
  61. /*
  62. * MPX register values for the BTF-PG472Z:
  63. *
  64. * FM_ NICAM_ SCART_
  65. * MODUS SOURCE ACB PRESCAL PRESCAL PRESCAL SYSTEM VOLUME
  66. * 10/0030 12/0008 12/0013 12/000E 12/0010 12/0000 10/0020 12/0000
  67. * ---------------------------------------------------------------
  68. * Auto 1003 0020 0100 2603 5000 XXXX 0001 7500
  69. *
  70. * B/G
  71. * Mono 1003 0020 0100 2603 5000 XXXX 0003 7500
  72. * A2 1003 0020 0100 2601 5000 XXXX 0003 7500
  73. * NICAM 1003 0120 0100 2603 5000 XXXX 0008 7500
  74. *
  75. * I
  76. * Mono 1003 0020 0100 2603 7900 XXXX 000A 7500
  77. * NICAM 1003 0120 0100 2603 7900 XXXX 000A 7500
  78. *
  79. * D/K
  80. * Mono 1003 0020 0100 2603 5000 XXXX 0004 7500
  81. * A2-1 1003 0020 0100 2601 5000 XXXX 0004 7500
  82. * A2-2 1003 0020 0100 2601 5000 XXXX 0005 7500
  83. * A2-3 1003 0020 0100 2601 5000 XXXX 0007 7500
  84. * NICAM 1003 0120 0100 2603 5000 XXXX 000B 7500
  85. *
  86. * L/L'
  87. * Mono 0003 0200 0100 7C03 5000 2200 0009 7500
  88. * NICAM 0003 0120 0100 7C03 5000 XXXX 0009 7500
  89. *
  90. * M
  91. * Mono 1003 0200 0100 2B03 5000 2B00 0002 7500
  92. *
  93. * For Asia, replace the 0x26XX in FM_PRESCALE with 0x14XX.
  94. *
  95. * Bilingual selection in A2/NICAM:
  96. *
  97. * High byte of SOURCE Left chan Right chan
  98. * 0x01 MAIN SUB
  99. * 0x03 MAIN MAIN
  100. * 0x04 SUB SUB
  101. *
  102. * Force mono in NICAM by setting the high byte of SOURCE to 0x02 (L/L') or
  103. * 0x00 (all other bands). Force mono in A2 with FMONO_A2:
  104. *
  105. * FMONO_A2
  106. * 10/0022
  107. * --------
  108. * Forced mono ON 07F0
  109. * Forced mono OFF 0190
  110. */
  111. static const struct {
  112. enum { AUD_MONO, AUD_A2, AUD_NICAM, AUD_NICAM_L } audio_mode;
  113. u16 modus;
  114. u16 source;
  115. u16 acb;
  116. u16 fm_prescale;
  117. u16 nicam_prescale;
  118. u16 scart_prescale;
  119. u16 system;
  120. u16 volume;
  121. } mpx_audio_modes[] = {
  122. /* Auto */ { AUD_MONO, 0x1003, 0x0020, 0x0100, 0x2603,
  123. 0x5000, 0x0000, 0x0001, 0x7500 },
  124. /* B/G Mono */ { AUD_MONO, 0x1003, 0x0020, 0x0100, 0x2603,
  125. 0x5000, 0x0000, 0x0003, 0x7500 },
  126. /* B/G A2 */ { AUD_A2, 0x1003, 0x0020, 0x0100, 0x2601,
  127. 0x5000, 0x0000, 0x0003, 0x7500 },
  128. /* B/G NICAM */ { AUD_NICAM, 0x1003, 0x0120, 0x0100, 0x2603,
  129. 0x5000, 0x0000, 0x0008, 0x7500 },
  130. /* I Mono */ { AUD_MONO, 0x1003, 0x0020, 0x0100, 0x2603,
  131. 0x7900, 0x0000, 0x000A, 0x7500 },
  132. /* I NICAM */ { AUD_NICAM, 0x1003, 0x0120, 0x0100, 0x2603,
  133. 0x7900, 0x0000, 0x000A, 0x7500 },
  134. /* D/K Mono */ { AUD_MONO, 0x1003, 0x0020, 0x0100, 0x2603,
  135. 0x5000, 0x0000, 0x0004, 0x7500 },
  136. /* D/K A2-1 */ { AUD_A2, 0x1003, 0x0020, 0x0100, 0x2601,
  137. 0x5000, 0x0000, 0x0004, 0x7500 },
  138. /* D/K A2-2 */ { AUD_A2, 0x1003, 0x0020, 0x0100, 0x2601,
  139. 0x5000, 0x0000, 0x0005, 0x7500 },
  140. /* D/K A2-3 */ { AUD_A2, 0x1003, 0x0020, 0x0100, 0x2601,
  141. 0x5000, 0x0000, 0x0007, 0x7500 },
  142. /* D/K NICAM */ { AUD_NICAM, 0x1003, 0x0120, 0x0100, 0x2603,
  143. 0x5000, 0x0000, 0x000B, 0x7500 },
  144. /* L/L' Mono */ { AUD_MONO, 0x0003, 0x0200, 0x0100, 0x7C03,
  145. 0x5000, 0x2200, 0x0009, 0x7500 },
  146. /* L/L' NICAM */{ AUD_NICAM_L, 0x0003, 0x0120, 0x0100, 0x7C03,
  147. 0x5000, 0x0000, 0x0009, 0x7500 },
  148. };
  149. #define MPX_NUM_MODES ARRAY_SIZE(mpx_audio_modes)
  150. static int mpx_setup(struct sony_btf_mpx *t)
  151. {
  152. struct i2c_client *client = v4l2_get_subdevdata(&t->sd);
  153. u16 source = 0;
  154. u8 buffer[3];
  155. struct i2c_msg msg;
  156. int mode = t->mpxmode;
  157. /* reset MPX */
  158. buffer[0] = 0x00;
  159. buffer[1] = 0x80;
  160. buffer[2] = 0x00;
  161. msg.addr = client->addr;
  162. msg.flags = 0;
  163. msg.len = 3;
  164. msg.buf = buffer;
  165. i2c_transfer(client->adapter, &msg, 1);
  166. buffer[1] = 0x00;
  167. i2c_transfer(client->adapter, &msg, 1);
  168. if (t->audmode != V4L2_TUNER_MODE_MONO)
  169. mode++;
  170. if (mpx_audio_modes[mode].audio_mode != AUD_MONO) {
  171. switch (t->audmode) {
  172. case V4L2_TUNER_MODE_MONO:
  173. switch (mpx_audio_modes[mode].audio_mode) {
  174. case AUD_A2:
  175. source = mpx_audio_modes[mode].source;
  176. break;
  177. case AUD_NICAM:
  178. source = 0x0000;
  179. break;
  180. case AUD_NICAM_L:
  181. source = 0x0200;
  182. break;
  183. default:
  184. break;
  185. }
  186. break;
  187. case V4L2_TUNER_MODE_STEREO:
  188. source = mpx_audio_modes[mode].source;
  189. break;
  190. case V4L2_TUNER_MODE_LANG1:
  191. source = 0x0300;
  192. break;
  193. case V4L2_TUNER_MODE_LANG2:
  194. source = 0x0400;
  195. break;
  196. }
  197. source |= mpx_audio_modes[mode].source & 0x00ff;
  198. } else
  199. source = mpx_audio_modes[mode].source;
  200. mpx_write(client, 0x10, 0x0030, mpx_audio_modes[mode].modus);
  201. mpx_write(client, 0x12, 0x0008, source);
  202. mpx_write(client, 0x12, 0x0013, mpx_audio_modes[mode].acb);
  203. mpx_write(client, 0x12, 0x000e,
  204. mpx_audio_modes[mode].fm_prescale);
  205. mpx_write(client, 0x12, 0x0010,
  206. mpx_audio_modes[mode].nicam_prescale);
  207. mpx_write(client, 0x12, 0x000d,
  208. mpx_audio_modes[mode].scart_prescale);
  209. mpx_write(client, 0x10, 0x0020, mpx_audio_modes[mode].system);
  210. mpx_write(client, 0x12, 0x0000, mpx_audio_modes[mode].volume);
  211. if (mpx_audio_modes[mode].audio_mode == AUD_A2)
  212. mpx_write(client, 0x10, 0x0022,
  213. t->audmode == V4L2_TUNER_MODE_MONO ? 0x07f0 : 0x0190);
  214. #ifdef MPX_DEBUG
  215. {
  216. u8 buf1[3], buf2[2];
  217. struct i2c_msg msgs[2];
  218. v4l2_info(client,
  219. "MPX registers: %04x %04x %04x %04x %04x %04x %04x %04x\n",
  220. mpx_audio_modes[mode].modus,
  221. source,
  222. mpx_audio_modes[mode].acb,
  223. mpx_audio_modes[mode].fm_prescale,
  224. mpx_audio_modes[mode].nicam_prescale,
  225. mpx_audio_modes[mode].scart_prescale,
  226. mpx_audio_modes[mode].system,
  227. mpx_audio_modes[mode].volume);
  228. buf1[0] = 0x11;
  229. buf1[1] = 0x00;
  230. buf1[2] = 0x7e;
  231. msgs[0].addr = client->addr;
  232. msgs[0].flags = 0;
  233. msgs[0].len = 3;
  234. msgs[0].buf = buf1;
  235. msgs[1].addr = client->addr;
  236. msgs[1].flags = I2C_M_RD;
  237. msgs[1].len = 2;
  238. msgs[1].buf = buf2;
  239. i2c_transfer(client->adapter, msgs, 2);
  240. v4l2_info(client, "MPX system: %02x%02x\n",
  241. buf2[0], buf2[1]);
  242. buf1[0] = 0x11;
  243. buf1[1] = 0x02;
  244. buf1[2] = 0x00;
  245. i2c_transfer(client->adapter, msgs, 2);
  246. v4l2_info(client, "MPX status: %02x%02x\n",
  247. buf2[0], buf2[1]);
  248. }
  249. #endif
  250. return 0;
  251. }
  252. static int sony_btf_mpx_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
  253. {
  254. struct sony_btf_mpx *t = to_state(sd);
  255. int default_mpx_mode = 0;
  256. if (std & V4L2_STD_PAL_BG)
  257. default_mpx_mode = 1;
  258. else if (std & V4L2_STD_PAL_I)
  259. default_mpx_mode = 4;
  260. else if (std & V4L2_STD_PAL_DK)
  261. default_mpx_mode = 6;
  262. else if (std & V4L2_STD_SECAM_L)
  263. default_mpx_mode = 11;
  264. if (default_mpx_mode != t->mpxmode) {
  265. t->mpxmode = default_mpx_mode;
  266. mpx_setup(t);
  267. }
  268. return 0;
  269. }
  270. static int sony_btf_mpx_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
  271. {
  272. struct sony_btf_mpx *t = to_state(sd);
  273. vt->capability = V4L2_TUNER_CAP_NORM |
  274. V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_LANG1 |
  275. V4L2_TUNER_CAP_LANG2;
  276. vt->rxsubchans = V4L2_TUNER_SUB_MONO |
  277. V4L2_TUNER_SUB_STEREO | V4L2_TUNER_SUB_LANG1 |
  278. V4L2_TUNER_SUB_LANG2;
  279. vt->audmode = t->audmode;
  280. return 0;
  281. }
  282. static int sony_btf_mpx_s_tuner(struct v4l2_subdev *sd, const struct v4l2_tuner *vt)
  283. {
  284. struct sony_btf_mpx *t = to_state(sd);
  285. if (vt->type != V4L2_TUNER_ANALOG_TV)
  286. return -EINVAL;
  287. if (vt->audmode != t->audmode) {
  288. t->audmode = vt->audmode;
  289. mpx_setup(t);
  290. }
  291. return 0;
  292. }
  293. /* --------------------------------------------------------------------------*/
  294. static const struct v4l2_subdev_tuner_ops sony_btf_mpx_tuner_ops = {
  295. .s_tuner = sony_btf_mpx_s_tuner,
  296. .g_tuner = sony_btf_mpx_g_tuner,
  297. };
  298. static const struct v4l2_subdev_video_ops sony_btf_mpx_video_ops = {
  299. .s_std = sony_btf_mpx_s_std,
  300. };
  301. static const struct v4l2_subdev_ops sony_btf_mpx_ops = {
  302. .tuner = &sony_btf_mpx_tuner_ops,
  303. .video = &sony_btf_mpx_video_ops,
  304. };
  305. /* --------------------------------------------------------------------------*/
  306. static int sony_btf_mpx_probe(struct i2c_client *client,
  307. const struct i2c_device_id *id)
  308. {
  309. struct sony_btf_mpx *t;
  310. struct v4l2_subdev *sd;
  311. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_I2C_BLOCK))
  312. return -ENODEV;
  313. v4l_info(client, "chip found @ 0x%x (%s)\n",
  314. client->addr << 1, client->adapter->name);
  315. t = devm_kzalloc(&client->dev, sizeof(*t), GFP_KERNEL);
  316. if (t == NULL)
  317. return -ENOMEM;
  318. sd = &t->sd;
  319. v4l2_i2c_subdev_init(sd, client, &sony_btf_mpx_ops);
  320. /* Initialize sony_btf_mpx */
  321. t->mpxmode = 0;
  322. t->audmode = V4L2_TUNER_MODE_STEREO;
  323. return 0;
  324. }
  325. static int sony_btf_mpx_remove(struct i2c_client *client)
  326. {
  327. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  328. v4l2_device_unregister_subdev(sd);
  329. return 0;
  330. }
  331. /* ----------------------------------------------------------------------- */
  332. static const struct i2c_device_id sony_btf_mpx_id[] = {
  333. { "sony-btf-mpx", 0 },
  334. { }
  335. };
  336. MODULE_DEVICE_TABLE(i2c, sony_btf_mpx_id);
  337. static struct i2c_driver sony_btf_mpx_driver = {
  338. .driver = {
  339. .name = "sony-btf-mpx",
  340. },
  341. .probe = sony_btf_mpx_probe,
  342. .remove = sony_btf_mpx_remove,
  343. .id_table = sony_btf_mpx_id,
  344. };
  345. module_i2c_driver(sony_btf_mpx_driver);