radio-maxiradio.c 8.0 KB

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