radio-gemtek.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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@infradead.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/slab.h>
  32. #include <media/v4l2-ioctl.h>
  33. #include <media/v4l2-device.h>
  34. #include "radio-isa.h"
  35. /*
  36. * Module info.
  37. */
  38. MODULE_AUTHOR("Jonas Munsin, Pekka Seppänen <pexu@kapsi.fi>");
  39. MODULE_DESCRIPTION("A driver for the GemTek Radio card.");
  40. MODULE_LICENSE("GPL");
  41. MODULE_VERSION("1.0.0");
  42. /*
  43. * Module params.
  44. */
  45. #ifndef CONFIG_RADIO_GEMTEK_PORT
  46. #define CONFIG_RADIO_GEMTEK_PORT -1
  47. #endif
  48. #ifndef CONFIG_RADIO_GEMTEK_PROBE
  49. #define CONFIG_RADIO_GEMTEK_PROBE 1
  50. #endif
  51. #define GEMTEK_MAX 4
  52. static bool probe = CONFIG_RADIO_GEMTEK_PROBE;
  53. static bool hardmute;
  54. static int io[GEMTEK_MAX] = { [0] = CONFIG_RADIO_GEMTEK_PORT,
  55. [1 ... (GEMTEK_MAX - 1)] = -1 };
  56. static int radio_nr[GEMTEK_MAX] = { [0 ... (GEMTEK_MAX - 1)] = -1 };
  57. module_param(probe, bool, 0444);
  58. MODULE_PARM_DESC(probe, "Enable automatic device probing.");
  59. module_param(hardmute, bool, 0644);
  60. MODULE_PARM_DESC(hardmute, "Enable 'hard muting' by shutting down PLL, may "
  61. "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 "
  64. "probing is disabled or fails. The most common I/O ports are: 0x20c "
  65. "0x30c, 0x24c or 0x34c (0x20c, 0x248 and 0x28c have been reported to "
  66. "work for the combined sound/radiocard).");
  67. module_param_array(radio_nr, int, NULL, 0444);
  68. MODULE_PARM_DESC(radio_nr, "Radio device numbers");
  69. /*
  70. * Frequency calculation constants. Intermediate frequency 10.52 MHz (nominal
  71. * value 10.7 MHz), reference divisor 6.39 kHz (nominal 6.25 kHz).
  72. */
  73. #define FSCALE 8
  74. #define IF_OFFSET ((unsigned int)(10.52 * 16000 * (1<<FSCALE)))
  75. #define REF_FREQ ((unsigned int)(6.39 * 16 * (1<<FSCALE)))
  76. #define GEMTEK_CK 0x01 /* Clock signal */
  77. #define GEMTEK_DA 0x02 /* Serial data */
  78. #define GEMTEK_CE 0x04 /* Chip enable */
  79. #define GEMTEK_NS 0x08 /* No signal */
  80. #define GEMTEK_MT 0x10 /* Line mute */
  81. #define GEMTEK_STDF_3_125_KHZ 0x01 /* Standard frequency 3.125 kHz */
  82. #define GEMTEK_PLL_OFF 0x07 /* PLL off */
  83. #define BU2614_BUS_SIZE 32 /* BU2614 / BU2614FS bus size */
  84. #define SHORT_DELAY 5 /* usec */
  85. #define LONG_DELAY 75 /* usec */
  86. struct gemtek {
  87. struct radio_isa_card isa;
  88. bool muted;
  89. u32 bu2614data;
  90. };
  91. #define BU2614_FREQ_BITS 16 /* D0..D15, Frequency data */
  92. #define BU2614_PORT_BITS 3 /* P0..P2, Output port control data */
  93. #define BU2614_VOID_BITS 4 /* unused */
  94. #define BU2614_FMES_BITS 1 /* CT, Frequency measurement beginning data */
  95. #define BU2614_STDF_BITS 3 /* R0..R2, Standard frequency data */
  96. #define BU2614_SWIN_BITS 1 /* S, Switch between FMIN / AMIN */
  97. #define BU2614_SWAL_BITS 1 /* PS, Swallow counter division (AMIN only)*/
  98. #define BU2614_VOID2_BITS 1 /* unused */
  99. #define BU2614_FMUN_BITS 1 /* GT, Frequency measurement time & unlock */
  100. #define BU2614_TEST_BITS 1 /* TS, Test data is input */
  101. #define BU2614_FREQ_SHIFT 0
  102. #define BU2614_PORT_SHIFT (BU2614_FREQ_BITS + BU2614_FREQ_SHIFT)
  103. #define BU2614_VOID_SHIFT (BU2614_PORT_BITS + BU2614_PORT_SHIFT)
  104. #define BU2614_FMES_SHIFT (BU2614_VOID_BITS + BU2614_VOID_SHIFT)
  105. #define BU2614_STDF_SHIFT (BU2614_FMES_BITS + BU2614_FMES_SHIFT)
  106. #define BU2614_SWIN_SHIFT (BU2614_STDF_BITS + BU2614_STDF_SHIFT)
  107. #define BU2614_SWAL_SHIFT (BU2614_SWIN_BITS + BU2614_SWIN_SHIFT)
  108. #define BU2614_VOID2_SHIFT (BU2614_SWAL_BITS + BU2614_SWAL_SHIFT)
  109. #define BU2614_FMUN_SHIFT (BU2614_VOID2_BITS + BU2614_VOID2_SHIFT)
  110. #define BU2614_TEST_SHIFT (BU2614_FMUN_BITS + BU2614_FMUN_SHIFT)
  111. #define MKMASK(field) (((1<<BU2614_##field##_BITS) - 1) << \
  112. BU2614_##field##_SHIFT)
  113. #define BU2614_PORT_MASK MKMASK(PORT)
  114. #define BU2614_FREQ_MASK MKMASK(FREQ)
  115. #define BU2614_VOID_MASK MKMASK(VOID)
  116. #define BU2614_FMES_MASK MKMASK(FMES)
  117. #define BU2614_STDF_MASK MKMASK(STDF)
  118. #define BU2614_SWIN_MASK MKMASK(SWIN)
  119. #define BU2614_SWAL_MASK MKMASK(SWAL)
  120. #define BU2614_VOID2_MASK MKMASK(VOID2)
  121. #define BU2614_FMUN_MASK MKMASK(FMUN)
  122. #define BU2614_TEST_MASK MKMASK(TEST)
  123. /*
  124. * Set data which will be sent to BU2614FS.
  125. */
  126. #define gemtek_bu2614_set(dev, field, data) ((dev)->bu2614data = \
  127. ((dev)->bu2614data & ~field##_MASK) | ((data) << field##_SHIFT))
  128. /*
  129. * Transmit settings to BU2614FS over GemTek IC.
  130. */
  131. static void gemtek_bu2614_transmit(struct gemtek *gt)
  132. {
  133. struct radio_isa_card *isa = &gt->isa;
  134. int i, bit, q, mute;
  135. mute = gt->muted ? GEMTEK_MT : 0x00;
  136. outb_p(mute | GEMTEK_CE | GEMTEK_DA | GEMTEK_CK, isa->io);
  137. udelay(LONG_DELAY);
  138. for (i = 0, q = gt->bu2614data; i < 32; i++, q >>= 1) {
  139. bit = (q & 1) ? GEMTEK_DA : 0;
  140. outb_p(mute | GEMTEK_CE | bit, isa->io);
  141. udelay(SHORT_DELAY);
  142. outb_p(mute | GEMTEK_CE | bit | GEMTEK_CK, isa->io);
  143. udelay(SHORT_DELAY);
  144. }
  145. outb_p(mute | GEMTEK_DA | GEMTEK_CK, isa->io);
  146. udelay(SHORT_DELAY);
  147. }
  148. /*
  149. * Calculate divisor from FM-frequency for BU2614FS (3.125 KHz STDF expected).
  150. */
  151. static unsigned long gemtek_convfreq(unsigned long freq)
  152. {
  153. return ((freq << FSCALE) + IF_OFFSET + REF_FREQ / 2) / REF_FREQ;
  154. }
  155. static struct radio_isa_card *gemtek_alloc(void)
  156. {
  157. struct gemtek *gt = kzalloc(sizeof(*gt), GFP_KERNEL);
  158. if (gt)
  159. gt->muted = true;
  160. return gt ? &gt->isa : NULL;
  161. }
  162. /*
  163. * Set FM-frequency.
  164. */
  165. static int gemtek_s_frequency(struct radio_isa_card *isa, u32 freq)
  166. {
  167. struct gemtek *gt = container_of(isa, struct gemtek, isa);
  168. if (hardmute && gt->muted)
  169. return 0;
  170. gemtek_bu2614_set(gt, BU2614_PORT, 0);
  171. gemtek_bu2614_set(gt, BU2614_FMES, 0);
  172. gemtek_bu2614_set(gt, BU2614_SWIN, 0); /* FM-mode */
  173. gemtek_bu2614_set(gt, BU2614_SWAL, 0);
  174. gemtek_bu2614_set(gt, BU2614_FMUN, 1); /* GT bit set */
  175. gemtek_bu2614_set(gt, BU2614_TEST, 0);
  176. gemtek_bu2614_set(gt, BU2614_STDF, GEMTEK_STDF_3_125_KHZ);
  177. gemtek_bu2614_set(gt, BU2614_FREQ, gemtek_convfreq(freq));
  178. gemtek_bu2614_transmit(gt);
  179. return 0;
  180. }
  181. /*
  182. * Set mute flag.
  183. */
  184. static int gemtek_s_mute_volume(struct radio_isa_card *isa, bool mute, int vol)
  185. {
  186. struct gemtek *gt = container_of(isa, struct gemtek, isa);
  187. int i;
  188. gt->muted = mute;
  189. if (hardmute) {
  190. if (!mute)
  191. return gemtek_s_frequency(isa, isa->freq);
  192. /* Turn off PLL, disable data output */
  193. gemtek_bu2614_set(gt, BU2614_PORT, 0);
  194. gemtek_bu2614_set(gt, BU2614_FMES, 0); /* CT bit off */
  195. gemtek_bu2614_set(gt, BU2614_SWIN, 0); /* FM-mode */
  196. gemtek_bu2614_set(gt, BU2614_SWAL, 0);
  197. gemtek_bu2614_set(gt, BU2614_FMUN, 0); /* GT bit off */
  198. gemtek_bu2614_set(gt, BU2614_TEST, 0);
  199. gemtek_bu2614_set(gt, BU2614_STDF, GEMTEK_PLL_OFF);
  200. gemtek_bu2614_set(gt, BU2614_FREQ, 0);
  201. gemtek_bu2614_transmit(gt);
  202. return 0;
  203. }
  204. /* Read bus contents (CE, CK and DA). */
  205. i = inb_p(isa->io);
  206. /* Write it back with mute flag set. */
  207. outb_p((i >> 5) | (mute ? GEMTEK_MT : 0), isa->io);
  208. udelay(SHORT_DELAY);
  209. return 0;
  210. }
  211. static u32 gemtek_g_rxsubchans(struct radio_isa_card *isa)
  212. {
  213. if (inb_p(isa->io) & GEMTEK_NS)
  214. return V4L2_TUNER_SUB_MONO;
  215. return V4L2_TUNER_SUB_STEREO;
  216. }
  217. /*
  218. * Check if requested card acts like GemTek Radio card.
  219. */
  220. static bool gemtek_probe(struct radio_isa_card *isa, int io)
  221. {
  222. int i, q;
  223. q = inb_p(io); /* Read bus contents before probing. */
  224. /* Try to turn on CE, CK and DA respectively and check if card responds
  225. properly. */
  226. for (i = 0; i < 3; ++i) {
  227. outb_p(1 << i, io);
  228. udelay(SHORT_DELAY);
  229. if ((inb_p(io) & ~GEMTEK_NS) != (0x17 | (1 << (i + 5))))
  230. return false;
  231. }
  232. outb_p(q >> 5, io); /* Write bus contents back. */
  233. udelay(SHORT_DELAY);
  234. return true;
  235. }
  236. static const struct radio_isa_ops gemtek_ops = {
  237. .alloc = gemtek_alloc,
  238. .probe = gemtek_probe,
  239. .s_mute_volume = gemtek_s_mute_volume,
  240. .s_frequency = gemtek_s_frequency,
  241. .g_rxsubchans = gemtek_g_rxsubchans,
  242. };
  243. static const int gemtek_ioports[] = { 0x20c, 0x30c, 0x24c, 0x34c, 0x248, 0x28c };
  244. static struct radio_isa_driver gemtek_driver = {
  245. .driver = {
  246. .match = radio_isa_match,
  247. .probe = radio_isa_probe,
  248. .remove = radio_isa_remove,
  249. .driver = {
  250. .name = "radio-gemtek",
  251. },
  252. },
  253. .io_params = io,
  254. .radio_nr_params = radio_nr,
  255. .io_ports = gemtek_ioports,
  256. .num_of_io_ports = ARRAY_SIZE(gemtek_ioports),
  257. .region_size = 1,
  258. .card = "GemTek Radio",
  259. .ops = &gemtek_ops,
  260. .has_stereo = true,
  261. };
  262. static int __init gemtek_init(void)
  263. {
  264. gemtek_driver.probe = probe;
  265. return isa_register_driver(&gemtek_driver.driver, GEMTEK_MAX);
  266. }
  267. static void __exit gemtek_exit(void)
  268. {
  269. hardmute = 1; /* Turn off PLL */
  270. isa_unregister_driver(&gemtek_driver.driver);
  271. }
  272. module_init(gemtek_init);
  273. module_exit(gemtek_exit);