radio-maxiradio.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /*
  2. * Guillemot Maxi Radio FM 2000 PCI radio card driver for Linux
  3. * (C) 2001 Dimitromanolakis Apostolos <apdim@grecian.net>
  4. *
  5. * Based in the radio Maestro PCI driver. Actually it uses the same chip
  6. * for radio but different pci controller.
  7. *
  8. * I didn't have any specs I reversed engineered the protocol from
  9. * the windows driver (radio.dll).
  10. *
  11. * The card uses the TEA5757 chip that includes a search function but it
  12. * is useless as I haven't found any way to read back the frequency. If
  13. * anybody does please mail me.
  14. *
  15. * For the pdf file see:
  16. * http://www.semiconductors.philips.com/pip/TEA5757H/V1
  17. *
  18. *
  19. * CHANGES:
  20. * 0.75b
  21. * - better pci interface thanks to Francois Romieu <romieu@cogenit.fr>
  22. *
  23. * 0.75
  24. * - tiding up
  25. * - removed support for multiple devices as it didn't work anyway
  26. *
  27. * BUGS:
  28. * - card unmutes if you change frequency
  29. *
  30. */
  31. #include <linux/module.h>
  32. #include <linux/init.h>
  33. #include <linux/ioport.h>
  34. #include <linux/delay.h>
  35. #include <linux/sched.h>
  36. #include <asm/io.h>
  37. #include <asm/uaccess.h>
  38. #include <asm/semaphore.h>
  39. #include <linux/pci.h>
  40. #include <linux/videodev.h>
  41. /* version 0.75 Sun Feb 4 22:51:27 EET 2001 */
  42. #define DRIVER_VERSION "0.75"
  43. #ifndef PCI_VENDOR_ID_GUILLEMOT
  44. #define PCI_VENDOR_ID_GUILLEMOT 0x5046
  45. #endif
  46. #ifndef PCI_DEVICE_ID_GUILLEMOT
  47. #define PCI_DEVICE_ID_GUILLEMOT_MAXIRADIO 0x1001
  48. #endif
  49. /* TEA5757 pin mappings */
  50. static const int clk = 1, data = 2, wren = 4, mo_st = 8, power = 16 ;
  51. static int radio_nr = -1;
  52. module_param(radio_nr, int, 0);
  53. #define FREQ_LO 50*16000
  54. #define FREQ_HI 150*16000
  55. #define FREQ_IF 171200 /* 10.7*16000 */
  56. #define FREQ_STEP 200 /* 12.5*16 */
  57. #define FREQ2BITS(x) ((( (unsigned int)(x)+FREQ_IF+(FREQ_STEP<<1))\
  58. /(FREQ_STEP<<2))<<2) /* (x==fmhz*16*1000) -> bits */
  59. #define BITS2FREQ(x) ((x) * FREQ_STEP - FREQ_IF)
  60. static int radio_ioctl(struct inode *inode, struct file *file,
  61. unsigned int cmd, unsigned long arg);
  62. static struct file_operations maxiradio_fops = {
  63. .owner = THIS_MODULE,
  64. .open = video_exclusive_open,
  65. .release = video_exclusive_release,
  66. .ioctl = radio_ioctl,
  67. .llseek = no_llseek,
  68. };
  69. static struct video_device maxiradio_radio =
  70. {
  71. .owner = THIS_MODULE,
  72. .name = "Maxi Radio FM2000 radio",
  73. .type = VID_TYPE_TUNER,
  74. .hardware = VID_HARDWARE_SF16MI,
  75. .fops = &maxiradio_fops,
  76. };
  77. static struct radio_device
  78. {
  79. __u16 io, /* base of radio io */
  80. muted, /* VIDEO_AUDIO_MUTE */
  81. stereo, /* VIDEO_TUNER_STEREO_ON */
  82. tuned; /* signal strength (0 or 0xffff) */
  83. unsigned long freq;
  84. struct semaphore lock;
  85. } radio_unit = {0, 0, 0, 0, };
  86. static void outbit(unsigned long bit, __u16 io)
  87. {
  88. if(bit != 0)
  89. {
  90. outb( power|wren|data ,io); udelay(4);
  91. outb( power|wren|data|clk ,io); udelay(4);
  92. outb( power|wren|data ,io); udelay(4);
  93. }
  94. else
  95. {
  96. outb( power|wren ,io); udelay(4);
  97. outb( power|wren|clk ,io); udelay(4);
  98. outb( power|wren ,io); udelay(4);
  99. }
  100. }
  101. static void turn_power(__u16 io, int p)
  102. {
  103. if(p != 0) outb(power, io); else outb(0,io);
  104. }
  105. static void set_freq(__u16 io, __u32 data)
  106. {
  107. unsigned long int si;
  108. int bl;
  109. /* TEA5757 shift register bits (see pdf) */
  110. outbit(0,io); // 24 search
  111. outbit(1,io); // 23 search up/down
  112. outbit(0,io); // 22 stereo/mono
  113. outbit(0,io); // 21 band
  114. outbit(0,io); // 20 band (only 00=FM works I think)
  115. outbit(0,io); // 19 port ?
  116. outbit(0,io); // 18 port ?
  117. outbit(0,io); // 17 search level
  118. outbit(0,io); // 16 search level
  119. si = 0x8000;
  120. for(bl = 1; bl <= 16 ; bl++) { outbit(data & si,io); si >>=1; }
  121. outb(power,io);
  122. }
  123. static int get_stereo(__u16 io)
  124. {
  125. outb(power,io); udelay(4);
  126. return !(inb(io) & mo_st);
  127. }
  128. static int get_tune(__u16 io)
  129. {
  130. outb(power+clk,io); udelay(4);
  131. return !(inb(io) & mo_st);
  132. }
  133. static inline int radio_function(struct inode *inode, struct file *file,
  134. unsigned int cmd, void *arg)
  135. {
  136. struct video_device *dev = video_devdata(file);
  137. struct radio_device *card=dev->priv;
  138. switch(cmd) {
  139. case VIDIOCGCAP: {
  140. struct video_capability *v = arg;
  141. memset(v,0,sizeof(*v));
  142. strcpy(v->name, "Maxi Radio FM2000 radio");
  143. v->type=VID_TYPE_TUNER;
  144. v->channels=v->audios=1;
  145. return 0;
  146. }
  147. case VIDIOCGTUNER: {
  148. struct video_tuner *v = arg;
  149. if(v->tuner)
  150. return -EINVAL;
  151. card->stereo = 0xffff * get_stereo(card->io);
  152. card->tuned = 0xffff * get_tune(card->io);
  153. v->flags = VIDEO_TUNER_LOW | card->stereo;
  154. v->signal = card->tuned;
  155. strcpy(v->name, "FM");
  156. v->rangelow = FREQ_LO;
  157. v->rangehigh = FREQ_HI;
  158. v->mode = VIDEO_MODE_AUTO;
  159. return 0;
  160. }
  161. case VIDIOCSTUNER: {
  162. struct video_tuner *v = arg;
  163. if(v->tuner!=0)
  164. return -EINVAL;
  165. return 0;
  166. }
  167. case VIDIOCGFREQ: {
  168. unsigned long *freq = arg;
  169. *freq = card->freq;
  170. return 0;
  171. }
  172. case VIDIOCSFREQ: {
  173. unsigned long *freq = arg;
  174. if (*freq < FREQ_LO || *freq > FREQ_HI)
  175. return -EINVAL;
  176. card->freq = *freq;
  177. set_freq(card->io, FREQ2BITS(card->freq));
  178. msleep(125);
  179. return 0;
  180. }
  181. case VIDIOCGAUDIO: {
  182. struct video_audio *v = arg;
  183. memset(v,0,sizeof(*v));
  184. strcpy(v->name, "Radio");
  185. v->flags=VIDEO_AUDIO_MUTABLE | card->muted;
  186. v->mode=VIDEO_SOUND_STEREO;
  187. return 0;
  188. }
  189. case VIDIOCSAUDIO: {
  190. struct video_audio *v = arg;
  191. if(v->audio)
  192. return -EINVAL;
  193. card->muted = v->flags & VIDEO_AUDIO_MUTE;
  194. if(card->muted)
  195. turn_power(card->io, 0);
  196. else
  197. set_freq(card->io, FREQ2BITS(card->freq));
  198. return 0;
  199. }
  200. case VIDIOCGUNIT: {
  201. struct video_unit *v = arg;
  202. v->video=VIDEO_NO_UNIT;
  203. v->vbi=VIDEO_NO_UNIT;
  204. v->radio=dev->minor;
  205. v->audio=0;
  206. v->teletext=VIDEO_NO_UNIT;
  207. return 0;
  208. }
  209. default: return -ENOIOCTLCMD;
  210. }
  211. }
  212. static int radio_ioctl(struct inode *inode, struct file *file,
  213. unsigned int cmd, unsigned long arg)
  214. {
  215. struct video_device *dev = video_devdata(file);
  216. struct radio_device *card=dev->priv;
  217. int ret;
  218. down(&card->lock);
  219. ret = video_usercopy(inode, file, cmd, arg, radio_function);
  220. up(&card->lock);
  221. return ret;
  222. }
  223. MODULE_AUTHOR("Dimitromanolakis Apostolos, apdim@grecian.net");
  224. MODULE_DESCRIPTION("Radio driver for the Guillemot Maxi Radio FM2000 radio.");
  225. MODULE_LICENSE("GPL");
  226. static int __devinit maxiradio_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
  227. {
  228. if(!request_region(pci_resource_start(pdev, 0),
  229. pci_resource_len(pdev, 0), "Maxi Radio FM 2000")) {
  230. printk(KERN_ERR "radio-maxiradio: can't reserve I/O ports\n");
  231. goto err_out;
  232. }
  233. if (pci_enable_device(pdev))
  234. goto err_out_free_region;
  235. radio_unit.io = pci_resource_start(pdev, 0);
  236. init_MUTEX(&radio_unit.lock);
  237. maxiradio_radio.priv = &radio_unit;
  238. if(video_register_device(&maxiradio_radio, VFL_TYPE_RADIO, radio_nr)==-1) {
  239. printk("radio-maxiradio: can't register device!");
  240. goto err_out_free_region;
  241. }
  242. printk(KERN_INFO "radio-maxiradio: version "
  243. DRIVER_VERSION
  244. " time "
  245. __TIME__ " "
  246. __DATE__
  247. "\n");
  248. printk(KERN_INFO "radio-maxiradio: found Guillemot MAXI Radio device (io = 0x%x)\n",
  249. radio_unit.io);
  250. return 0;
  251. err_out_free_region:
  252. release_region(pci_resource_start(pdev, 0), pci_resource_len(pdev, 0));
  253. err_out:
  254. return -ENODEV;
  255. }
  256. static void __devexit maxiradio_remove_one(struct pci_dev *pdev)
  257. {
  258. video_unregister_device(&maxiradio_radio);
  259. release_region(pci_resource_start(pdev, 0), pci_resource_len(pdev, 0));
  260. }
  261. static struct pci_device_id maxiradio_pci_tbl[] = {
  262. { PCI_VENDOR_ID_GUILLEMOT, PCI_DEVICE_ID_GUILLEMOT_MAXIRADIO,
  263. PCI_ANY_ID, PCI_ANY_ID, },
  264. { 0,}
  265. };
  266. MODULE_DEVICE_TABLE(pci, maxiradio_pci_tbl);
  267. static struct pci_driver maxiradio_driver = {
  268. .name = "radio-maxiradio",
  269. .id_table = maxiradio_pci_tbl,
  270. .probe = maxiradio_init_one,
  271. .remove = __devexit_p(maxiradio_remove_one),
  272. };
  273. static int __init maxiradio_radio_init(void)
  274. {
  275. return pci_module_init(&maxiradio_driver);
  276. }
  277. static void __exit maxiradio_radio_exit(void)
  278. {
  279. pci_unregister_driver(&maxiradio_driver);
  280. }
  281. module_init(maxiradio_radio_init);
  282. module_exit(maxiradio_radio_exit);