radio-gemtek.c 9.9 KB

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