radio-maxiradio.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. .compat_ioctl = v4l_compat_ioctl32,
  68. .llseek = no_llseek,
  69. };
  70. static struct video_device maxiradio_radio =
  71. {
  72. .owner = THIS_MODULE,
  73. .name = "Maxi Radio FM2000 radio",
  74. .type = VID_TYPE_TUNER,
  75. .hardware = VID_HARDWARE_SF16MI,
  76. .fops = &maxiradio_fops,
  77. };
  78. static struct radio_device
  79. {
  80. __u16 io, /* base of radio io */
  81. muted, /* VIDEO_AUDIO_MUTE */
  82. stereo, /* VIDEO_TUNER_STEREO_ON */
  83. tuned; /* signal strength (0 or 0xffff) */
  84. unsigned long freq;
  85. struct semaphore lock;
  86. } radio_unit = {0, 0, 0, 0, };
  87. static void outbit(unsigned long bit, __u16 io)
  88. {
  89. if(bit != 0)
  90. {
  91. outb( power|wren|data ,io); udelay(4);
  92. outb( power|wren|data|clk ,io); udelay(4);
  93. outb( power|wren|data ,io); udelay(4);
  94. }
  95. else
  96. {
  97. outb( power|wren ,io); udelay(4);
  98. outb( power|wren|clk ,io); udelay(4);
  99. outb( power|wren ,io); udelay(4);
  100. }
  101. }
  102. static void turn_power(__u16 io, int p)
  103. {
  104. if(p != 0) outb(power, io); else outb(0,io);
  105. }
  106. static void set_freq(__u16 io, __u32 data)
  107. {
  108. unsigned long int si;
  109. int bl;
  110. /* TEA5757 shift register bits (see pdf) */
  111. outbit(0,io); // 24 search
  112. outbit(1,io); // 23 search up/down
  113. outbit(0,io); // 22 stereo/mono
  114. outbit(0,io); // 21 band
  115. outbit(0,io); // 20 band (only 00=FM works I think)
  116. outbit(0,io); // 19 port ?
  117. outbit(0,io); // 18 port ?
  118. outbit(0,io); // 17 search level
  119. outbit(0,io); // 16 search level
  120. si = 0x8000;
  121. for(bl = 1; bl <= 16 ; bl++) { outbit(data & si,io); si >>=1; }
  122. outb(power,io);
  123. }
  124. static int get_stereo(__u16 io)
  125. {
  126. outb(power,io); udelay(4);
  127. return !(inb(io) & mo_st);
  128. }
  129. static int get_tune(__u16 io)
  130. {
  131. outb(power+clk,io); udelay(4);
  132. return !(inb(io) & mo_st);
  133. }
  134. static inline int radio_function(struct inode *inode, struct file *file,
  135. unsigned int cmd, void *arg)
  136. {
  137. struct video_device *dev = video_devdata(file);
  138. struct radio_device *card=dev->priv;
  139. switch(cmd) {
  140. case VIDIOCGCAP: {
  141. struct video_capability *v = arg;
  142. memset(v,0,sizeof(*v));
  143. strcpy(v->name, "Maxi Radio FM2000 radio");
  144. v->type=VID_TYPE_TUNER;
  145. v->channels=v->audios=1;
  146. return 0;
  147. }
  148. case VIDIOCGTUNER: {
  149. struct video_tuner *v = arg;
  150. if(v->tuner)
  151. return -EINVAL;
  152. card->stereo = 0xffff * get_stereo(card->io);
  153. card->tuned = 0xffff * get_tune(card->io);
  154. v->flags = VIDEO_TUNER_LOW | card->stereo;
  155. v->signal = card->tuned;
  156. strcpy(v->name, "FM");
  157. v->rangelow = FREQ_LO;
  158. v->rangehigh = FREQ_HI;
  159. v->mode = VIDEO_MODE_AUTO;
  160. return 0;
  161. }
  162. case VIDIOCSTUNER: {
  163. struct video_tuner *v = arg;
  164. if(v->tuner!=0)
  165. return -EINVAL;
  166. return 0;
  167. }
  168. case VIDIOCGFREQ: {
  169. unsigned long *freq = arg;
  170. *freq = card->freq;
  171. return 0;
  172. }
  173. case VIDIOCSFREQ: {
  174. unsigned long *freq = arg;
  175. if (*freq < FREQ_LO || *freq > FREQ_HI)
  176. return -EINVAL;
  177. card->freq = *freq;
  178. set_freq(card->io, FREQ2BITS(card->freq));
  179. msleep(125);
  180. return 0;
  181. }
  182. case VIDIOCGAUDIO: {
  183. struct video_audio *v = arg;
  184. memset(v,0,sizeof(*v));
  185. strcpy(v->name, "Radio");
  186. v->flags=VIDEO_AUDIO_MUTABLE | card->muted;
  187. v->mode=VIDEO_SOUND_STEREO;
  188. return 0;
  189. }
  190. case VIDIOCSAUDIO: {
  191. struct video_audio *v = arg;
  192. if(v->audio)
  193. return -EINVAL;
  194. card->muted = v->flags & VIDEO_AUDIO_MUTE;
  195. if(card->muted)
  196. turn_power(card->io, 0);
  197. else
  198. set_freq(card->io, FREQ2BITS(card->freq));
  199. return 0;
  200. }
  201. case VIDIOCGUNIT: {
  202. struct video_unit *v = arg;
  203. v->video=VIDEO_NO_UNIT;
  204. v->vbi=VIDEO_NO_UNIT;
  205. v->radio=dev->minor;
  206. v->audio=0;
  207. v->teletext=VIDEO_NO_UNIT;
  208. return 0;
  209. }
  210. default: return -ENOIOCTLCMD;
  211. }
  212. }
  213. static int radio_ioctl(struct inode *inode, struct file *file,
  214. unsigned int cmd, unsigned long arg)
  215. {
  216. struct video_device *dev = video_devdata(file);
  217. struct radio_device *card=dev->priv;
  218. int ret;
  219. down(&card->lock);
  220. ret = video_usercopy(inode, file, cmd, arg, radio_function);
  221. up(&card->lock);
  222. return ret;
  223. }
  224. MODULE_AUTHOR("Dimitromanolakis Apostolos, apdim@grecian.net");
  225. MODULE_DESCRIPTION("Radio driver for the Guillemot Maxi Radio FM2000 radio.");
  226. MODULE_LICENSE("GPL");
  227. static int __devinit maxiradio_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
  228. {
  229. if(!request_region(pci_resource_start(pdev, 0),
  230. pci_resource_len(pdev, 0), "Maxi Radio FM 2000")) {
  231. printk(KERN_ERR "radio-maxiradio: can't reserve I/O ports\n");
  232. goto err_out;
  233. }
  234. if (pci_enable_device(pdev))
  235. goto err_out_free_region;
  236. radio_unit.io = pci_resource_start(pdev, 0);
  237. init_MUTEX(&radio_unit.lock);
  238. maxiradio_radio.priv = &radio_unit;
  239. if(video_register_device(&maxiradio_radio, VFL_TYPE_RADIO, radio_nr)==-1) {
  240. printk("radio-maxiradio: can't register device!");
  241. goto err_out_free_region;
  242. }
  243. printk(KERN_INFO "radio-maxiradio: version "
  244. DRIVER_VERSION
  245. " time "
  246. __TIME__ " "
  247. __DATE__
  248. "\n");
  249. printk(KERN_INFO "radio-maxiradio: found Guillemot MAXI Radio device (io = 0x%x)\n",
  250. radio_unit.io);
  251. return 0;
  252. err_out_free_region:
  253. release_region(pci_resource_start(pdev, 0), pci_resource_len(pdev, 0));
  254. err_out:
  255. return -ENODEV;
  256. }
  257. static void __devexit maxiradio_remove_one(struct pci_dev *pdev)
  258. {
  259. video_unregister_device(&maxiradio_radio);
  260. release_region(pci_resource_start(pdev, 0), pci_resource_len(pdev, 0));
  261. }
  262. static struct pci_device_id maxiradio_pci_tbl[] = {
  263. { PCI_VENDOR_ID_GUILLEMOT, PCI_DEVICE_ID_GUILLEMOT_MAXIRADIO,
  264. PCI_ANY_ID, PCI_ANY_ID, },
  265. { 0,}
  266. };
  267. MODULE_DEVICE_TABLE(pci, maxiradio_pci_tbl);
  268. static struct pci_driver maxiradio_driver = {
  269. .name = "radio-maxiradio",
  270. .id_table = maxiradio_pci_tbl,
  271. .probe = maxiradio_init_one,
  272. .remove = __devexit_p(maxiradio_remove_one),
  273. };
  274. static int __init maxiradio_radio_init(void)
  275. {
  276. return pci_register_driver(&maxiradio_driver);
  277. }
  278. static void __exit maxiradio_radio_exit(void)
  279. {
  280. pci_unregister_driver(&maxiradio_driver);
  281. }
  282. module_init(maxiradio_radio_init);
  283. module_exit(maxiradio_radio_exit);