radio-gemtek.c 9.9 KB

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