radio-gemtek.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*
  2. * GemTek radio card driver
  3. *
  4. * Copyright 1998 Jonas Munsin <jmunsin@iki.fi>
  5. *
  6. * GemTek hasn't released any specs on the card, so the protocol had to
  7. * be reverse engineered with dosemu.
  8. *
  9. * Besides the protocol changes, this is mostly a copy of:
  10. *
  11. * RadioTrack II driver for Linux radio support (C) 1998 Ben Pfaff
  12. *
  13. * Based on RadioTrack I/RadioReveal (C) 1997 M. Kirkwood
  14. * Converted to new API by Alan Cox <alan@lxorguk.ukuu.org.uk>
  15. * Various bugfixes and enhancements by Russell Kroll <rkroll@exploits.org>
  16. *
  17. * Converted to the radio-isa framework by Hans Verkuil <hans.verkuil@cisco.com>
  18. * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@kernel.org>
  19. *
  20. * Note: this card seems to swap the left and right audio channels!
  21. *
  22. * Fully tested with the Keene USB FM Transmitter and the v4l2-compliance tool.
  23. */
  24. #include <linux/module.h> /* Modules */
  25. #include <linux/init.h> /* Initdata */
  26. #include <linux/ioport.h> /* request_region */
  27. #include <linux/delay.h> /* udelay */
  28. #include <linux/videodev2.h> /* kernel radio structs */
  29. #include <linux/mutex.h>
  30. #include <linux/io.h> /* outb, outb_p */
  31. #include <linux/pnp.h>
  32. #include <linux/slab.h>
  33. #include <media/v4l2-ioctl.h>
  34. #include <media/v4l2-device.h>
  35. #include "radio-isa.h"
  36. /*
  37. * Module info.
  38. */
  39. MODULE_AUTHOR("Jonas Munsin, Pekka Seppänen <pexu@kapsi.fi>");
  40. MODULE_DESCRIPTION("A driver for the GemTek Radio card.");
  41. MODULE_LICENSE("GPL");
  42. MODULE_VERSION("1.0.0");
  43. /*
  44. * Module params.
  45. */
  46. #ifndef CONFIG_RADIO_GEMTEK_PORT
  47. #define CONFIG_RADIO_GEMTEK_PORT -1
  48. #endif
  49. #ifndef CONFIG_RADIO_GEMTEK_PROBE
  50. #define CONFIG_RADIO_GEMTEK_PROBE 1
  51. #endif
  52. #define GEMTEK_MAX 4
  53. static bool probe = CONFIG_RADIO_GEMTEK_PROBE;
  54. static bool hardmute;
  55. static int io[GEMTEK_MAX] = { [0] = CONFIG_RADIO_GEMTEK_PORT,
  56. [1 ... (GEMTEK_MAX - 1)] = -1 };
  57. static int radio_nr[GEMTEK_MAX] = { [0 ... (GEMTEK_MAX - 1)] = -1 };
  58. module_param(probe, bool, 0444);
  59. MODULE_PARM_DESC(probe, "Enable automatic device probing.");
  60. module_param(hardmute, bool, 0644);
  61. MODULE_PARM_DESC(hardmute, "Enable 'hard muting' by shutting down PLL, may reduce static noise.");
  62. module_param_array(io, int, NULL, 0444);
  63. MODULE_PARM_DESC(io, "Force I/O ports for the GemTek Radio card if automatic probing is disabled or fails. The most common I/O ports are: 0x20c 0x30c, 0x24c or 0x34c (0x20c, 0x248 and 0x28c have been reported to work for the combined sound/radiocard).");
  64. module_param_array(radio_nr, int, NULL, 0444);
  65. MODULE_PARM_DESC(radio_nr, "Radio device numbers");
  66. /*
  67. * Frequency calculation constants. Intermediate frequency 10.52 MHz (nominal
  68. * value 10.7 MHz), reference divisor 6.39 kHz (nominal 6.25 kHz).
  69. */
  70. #define FSCALE 8
  71. #define IF_OFFSET ((unsigned int)(10.52 * 16000 * (1<<FSCALE)))
  72. #define REF_FREQ ((unsigned int)(6.39 * 16 * (1<<FSCALE)))
  73. #define GEMTEK_CK 0x01 /* Clock signal */
  74. #define GEMTEK_DA 0x02 /* Serial data */
  75. #define GEMTEK_CE 0x04 /* Chip enable */
  76. #define GEMTEK_NS 0x08 /* No signal */
  77. #define GEMTEK_MT 0x10 /* Line mute */
  78. #define GEMTEK_STDF_3_125_KHZ 0x01 /* Standard frequency 3.125 kHz */
  79. #define GEMTEK_PLL_OFF 0x07 /* PLL off */
  80. #define BU2614_BUS_SIZE 32 /* BU2614 / BU2614FS bus size */
  81. #define SHORT_DELAY 5 /* usec */
  82. #define LONG_DELAY 75 /* usec */
  83. struct gemtek {
  84. struct radio_isa_card isa;
  85. bool muted;
  86. u32 bu2614data;
  87. };
  88. #define BU2614_FREQ_BITS 16 /* D0..D15, Frequency data */
  89. #define BU2614_PORT_BITS 3 /* P0..P2, Output port control data */
  90. #define BU2614_VOID_BITS 4 /* unused */
  91. #define BU2614_FMES_BITS 1 /* CT, Frequency measurement beginning data */
  92. #define BU2614_STDF_BITS 3 /* R0..R2, Standard frequency data */
  93. #define BU2614_SWIN_BITS 1 /* S, Switch between FMIN / AMIN */
  94. #define BU2614_SWAL_BITS 1 /* PS, Swallow counter division (AMIN only)*/
  95. #define BU2614_VOID2_BITS 1 /* unused */
  96. #define BU2614_FMUN_BITS 1 /* GT, Frequency measurement time & unlock */
  97. #define BU2614_TEST_BITS 1 /* TS, Test data is input */
  98. #define BU2614_FREQ_SHIFT 0
  99. #define BU2614_PORT_SHIFT (BU2614_FREQ_BITS + BU2614_FREQ_SHIFT)
  100. #define BU2614_VOID_SHIFT (BU2614_PORT_BITS + BU2614_PORT_SHIFT)
  101. #define BU2614_FMES_SHIFT (BU2614_VOID_BITS + BU2614_VOID_SHIFT)
  102. #define BU2614_STDF_SHIFT (BU2614_FMES_BITS + BU2614_FMES_SHIFT)
  103. #define BU2614_SWIN_SHIFT (BU2614_STDF_BITS + BU2614_STDF_SHIFT)
  104. #define BU2614_SWAL_SHIFT (BU2614_SWIN_BITS + BU2614_SWIN_SHIFT)
  105. #define BU2614_VOID2_SHIFT (BU2614_SWAL_BITS + BU2614_SWAL_SHIFT)
  106. #define BU2614_FMUN_SHIFT (BU2614_VOID2_BITS + BU2614_VOID2_SHIFT)
  107. #define BU2614_TEST_SHIFT (BU2614_FMUN_BITS + BU2614_FMUN_SHIFT)
  108. #define MKMASK(field) (((1<<BU2614_##field##_BITS) - 1) << \
  109. BU2614_##field##_SHIFT)
  110. #define BU2614_PORT_MASK MKMASK(PORT)
  111. #define BU2614_FREQ_MASK MKMASK(FREQ)
  112. #define BU2614_VOID_MASK MKMASK(VOID)
  113. #define BU2614_FMES_MASK MKMASK(FMES)
  114. #define BU2614_STDF_MASK MKMASK(STDF)
  115. #define BU2614_SWIN_MASK MKMASK(SWIN)
  116. #define BU2614_SWAL_MASK MKMASK(SWAL)
  117. #define BU2614_VOID2_MASK MKMASK(VOID2)
  118. #define BU2614_FMUN_MASK MKMASK(FMUN)
  119. #define BU2614_TEST_MASK MKMASK(TEST)
  120. /*
  121. * Set data which will be sent to BU2614FS.
  122. */
  123. #define gemtek_bu2614_set(dev, field, data) ((dev)->bu2614data = \
  124. ((dev)->bu2614data & ~field##_MASK) | ((data) << field##_SHIFT))
  125. /*
  126. * Transmit settings to BU2614FS over GemTek IC.
  127. */
  128. static void gemtek_bu2614_transmit(struct gemtek *gt)
  129. {
  130. struct radio_isa_card *isa = &gt->isa;
  131. int i, bit, q, mute;
  132. mute = gt->muted ? GEMTEK_MT : 0x00;
  133. outb_p(mute | GEMTEK_CE | GEMTEK_DA | GEMTEK_CK, isa->io);
  134. udelay(LONG_DELAY);
  135. for (i = 0, q = gt->bu2614data; i < 32; i++, q >>= 1) {
  136. bit = (q & 1) ? GEMTEK_DA : 0;
  137. outb_p(mute | GEMTEK_CE | bit, isa->io);
  138. udelay(SHORT_DELAY);
  139. outb_p(mute | GEMTEK_CE | bit | GEMTEK_CK, isa->io);
  140. udelay(SHORT_DELAY);
  141. }
  142. outb_p(mute | GEMTEK_DA | GEMTEK_CK, isa->io);
  143. udelay(SHORT_DELAY);
  144. }
  145. /*
  146. * Calculate divisor from FM-frequency for BU2614FS (3.125 KHz STDF expected).
  147. */
  148. static unsigned long gemtek_convfreq(unsigned long freq)
  149. {
  150. return ((freq << FSCALE) + IF_OFFSET + REF_FREQ / 2) / REF_FREQ;
  151. }
  152. static struct radio_isa_card *gemtek_alloc(void)
  153. {
  154. struct gemtek *gt = kzalloc(sizeof(*gt), GFP_KERNEL);
  155. if (gt)
  156. gt->muted = true;
  157. return gt ? &gt->isa : NULL;
  158. }
  159. /*
  160. * Set FM-frequency.
  161. */
  162. static int gemtek_s_frequency(struct radio_isa_card *isa, u32 freq)
  163. {
  164. struct gemtek *gt = container_of(isa, struct gemtek, isa);
  165. if (hardmute && gt->muted)
  166. return 0;
  167. gemtek_bu2614_set(gt, BU2614_PORT, 0);
  168. gemtek_bu2614_set(gt, BU2614_FMES, 0);
  169. gemtek_bu2614_set(gt, BU2614_SWIN, 0); /* FM-mode */
  170. gemtek_bu2614_set(gt, BU2614_SWAL, 0);
  171. gemtek_bu2614_set(gt, BU2614_FMUN, 1); /* GT bit set */
  172. gemtek_bu2614_set(gt, BU2614_TEST, 0);
  173. gemtek_bu2614_set(gt, BU2614_STDF, GEMTEK_STDF_3_125_KHZ);
  174. gemtek_bu2614_set(gt, BU2614_FREQ, gemtek_convfreq(freq));
  175. gemtek_bu2614_transmit(gt);
  176. return 0;
  177. }
  178. /*
  179. * Set mute flag.
  180. */
  181. static int gemtek_s_mute_volume(struct radio_isa_card *isa, bool mute, int vol)
  182. {
  183. struct gemtek *gt = container_of(isa, struct gemtek, isa);
  184. int i;
  185. gt->muted = mute;
  186. if (hardmute) {
  187. if (!mute)
  188. return gemtek_s_frequency(isa, isa->freq);
  189. /* Turn off PLL, disable data output */
  190. gemtek_bu2614_set(gt, BU2614_PORT, 0);
  191. gemtek_bu2614_set(gt, BU2614_FMES, 0); /* CT bit off */
  192. gemtek_bu2614_set(gt, BU2614_SWIN, 0); /* FM-mode */
  193. gemtek_bu2614_set(gt, BU2614_SWAL, 0);
  194. gemtek_bu2614_set(gt, BU2614_FMUN, 0); /* GT bit off */
  195. gemtek_bu2614_set(gt, BU2614_TEST, 0);
  196. gemtek_bu2614_set(gt, BU2614_STDF, GEMTEK_PLL_OFF);
  197. gemtek_bu2614_set(gt, BU2614_FREQ, 0);
  198. gemtek_bu2614_transmit(gt);
  199. return 0;
  200. }
  201. /* Read bus contents (CE, CK and DA). */
  202. i = inb_p(isa->io);
  203. /* Write it back with mute flag set. */
  204. outb_p((i >> 5) | (mute ? GEMTEK_MT : 0), isa->io);
  205. udelay(SHORT_DELAY);
  206. return 0;
  207. }
  208. static u32 gemtek_g_rxsubchans(struct radio_isa_card *isa)
  209. {
  210. if (inb_p(isa->io) & GEMTEK_NS)
  211. return V4L2_TUNER_SUB_MONO;
  212. return V4L2_TUNER_SUB_STEREO;
  213. }
  214. /*
  215. * Check if requested card acts like GemTek Radio card.
  216. */
  217. static bool gemtek_probe(struct radio_isa_card *isa, int io)
  218. {
  219. int i, q;
  220. q = inb_p(io); /* Read bus contents before probing. */
  221. /* Try to turn on CE, CK and DA respectively and check if card responds
  222. properly. */
  223. for (i = 0; i < 3; ++i) {
  224. outb_p(1 << i, io);
  225. udelay(SHORT_DELAY);
  226. if ((inb_p(io) & ~GEMTEK_NS) != (0x17 | (1 << (i + 5))))
  227. return false;
  228. }
  229. outb_p(q >> 5, io); /* Write bus contents back. */
  230. udelay(SHORT_DELAY);
  231. return true;
  232. }
  233. static const struct radio_isa_ops gemtek_ops = {
  234. .alloc = gemtek_alloc,
  235. .probe = gemtek_probe,
  236. .s_mute_volume = gemtek_s_mute_volume,
  237. .s_frequency = gemtek_s_frequency,
  238. .g_rxsubchans = gemtek_g_rxsubchans,
  239. };
  240. static const int gemtek_ioports[] = { 0x20c, 0x30c, 0x24c, 0x34c, 0x248, 0x28c };
  241. #ifdef CONFIG_PNP
  242. static const struct pnp_device_id gemtek_pnp_devices[] = {
  243. /* AOpen FX-3D/Pro Radio */
  244. {.id = "ADS7183", .driver_data = 0},
  245. {.id = ""}
  246. };
  247. MODULE_DEVICE_TABLE(pnp, gemtek_pnp_devices);
  248. #endif
  249. static struct radio_isa_driver gemtek_driver = {
  250. .driver = {
  251. .match = radio_isa_match,
  252. .probe = radio_isa_probe,
  253. .remove = radio_isa_remove,
  254. .driver = {
  255. .name = "radio-gemtek",
  256. },
  257. },
  258. #ifdef CONFIG_PNP
  259. .pnp_driver = {
  260. .name = "radio-gemtek",
  261. .id_table = gemtek_pnp_devices,
  262. .probe = radio_isa_pnp_probe,
  263. .remove = radio_isa_pnp_remove,
  264. },
  265. #endif
  266. .io_params = io,
  267. .radio_nr_params = radio_nr,
  268. .io_ports = gemtek_ioports,
  269. .num_of_io_ports = ARRAY_SIZE(gemtek_ioports),
  270. .region_size = 1,
  271. .card = "GemTek Radio",
  272. .ops = &gemtek_ops,
  273. .has_stereo = true,
  274. };
  275. static int __init gemtek_init(void)
  276. {
  277. gemtek_driver.probe = probe;
  278. #ifdef CONFIG_PNP
  279. pnp_register_driver(&gemtek_driver.pnp_driver);
  280. #endif
  281. return isa_register_driver(&gemtek_driver.driver, GEMTEK_MAX);
  282. }
  283. static void __exit gemtek_exit(void)
  284. {
  285. hardmute = true; /* Turn off PLL */
  286. #ifdef CONFIG_PNP
  287. pnp_unregister_driver(&gemtek_driver.pnp_driver);
  288. #endif
  289. isa_unregister_driver(&gemtek_driver.driver);
  290. }
  291. module_init(gemtek_init);
  292. module_exit(gemtek_exit);