radio-maxiradio.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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 Sun Feb 4 22:51:27 EET 2001
  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. * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
  31. */
  32. #include <linux/module.h>
  33. #include <linux/init.h>
  34. #include <linux/ioport.h>
  35. #include <linux/delay.h>
  36. #include <linux/sched.h>
  37. #include <asm/io.h>
  38. #include <asm/uaccess.h>
  39. #include <linux/mutex.h>
  40. #include <linux/pci.h>
  41. #include <linux/videodev2.h>
  42. #include <media/v4l2-common.h>
  43. #define DRIVER_VERSION "0.76"
  44. #include <linux/version.h> /* for KERNEL_VERSION MACRO */
  45. #define RADIO_VERSION KERNEL_VERSION(0,7,6)
  46. static struct v4l2_queryctrl radio_qctrl[] = {
  47. {
  48. .id = V4L2_CID_AUDIO_MUTE,
  49. .name = "Mute",
  50. .minimum = 0,
  51. .maximum = 1,
  52. .default_value = 1,
  53. .type = V4L2_CTRL_TYPE_BOOLEAN,
  54. }
  55. };
  56. #ifndef PCI_VENDOR_ID_GUILLEMOT
  57. #define PCI_VENDOR_ID_GUILLEMOT 0x5046
  58. #endif
  59. #ifndef PCI_DEVICE_ID_GUILLEMOT
  60. #define PCI_DEVICE_ID_GUILLEMOT_MAXIRADIO 0x1001
  61. #endif
  62. /* TEA5757 pin mappings */
  63. static const int clk = 1, data = 2, wren = 4, mo_st = 8, power = 16 ;
  64. static int radio_nr = -1;
  65. module_param(radio_nr, int, 0);
  66. #define FREQ_LO 50*16000
  67. #define FREQ_HI 150*16000
  68. #define FREQ_IF 171200 /* 10.7*16000 */
  69. #define FREQ_STEP 200 /* 12.5*16 */
  70. #define FREQ2BITS(x) ((( (unsigned int)(x)+FREQ_IF+(FREQ_STEP<<1))\
  71. /(FREQ_STEP<<2))<<2) /* (x==fmhz*16*1000) -> bits */
  72. #define BITS2FREQ(x) ((x) * FREQ_STEP - FREQ_IF)
  73. static int radio_ioctl(struct inode *inode, struct file *file,
  74. unsigned int cmd, unsigned long arg);
  75. static struct file_operations maxiradio_fops = {
  76. .owner = THIS_MODULE,
  77. .open = video_exclusive_open,
  78. .release = video_exclusive_release,
  79. .ioctl = radio_ioctl,
  80. .compat_ioctl = v4l_compat_ioctl32,
  81. .llseek = no_llseek,
  82. };
  83. static struct video_device maxiradio_radio =
  84. {
  85. .owner = THIS_MODULE,
  86. .name = "Maxi Radio FM2000 radio",
  87. .type = VID_TYPE_TUNER,
  88. .fops = &maxiradio_fops,
  89. };
  90. static struct radio_device
  91. {
  92. __u16 io, /* base of radio io */
  93. muted, /* VIDEO_AUDIO_MUTE */
  94. stereo, /* VIDEO_TUNER_STEREO_ON */
  95. tuned; /* signal strength (0 or 0xffff) */
  96. unsigned long freq;
  97. struct mutex lock;
  98. } radio_unit = {0, 0, 0, 0, };
  99. static void outbit(unsigned long bit, __u16 io)
  100. {
  101. if(bit != 0)
  102. {
  103. outb( power|wren|data ,io); udelay(4);
  104. outb( power|wren|data|clk ,io); udelay(4);
  105. outb( power|wren|data ,io); udelay(4);
  106. }
  107. else
  108. {
  109. outb( power|wren ,io); udelay(4);
  110. outb( power|wren|clk ,io); udelay(4);
  111. outb( power|wren ,io); udelay(4);
  112. }
  113. }
  114. static void turn_power(__u16 io, int p)
  115. {
  116. if(p != 0) outb(power, io); else outb(0,io);
  117. }
  118. static void set_freq(__u16 io, __u32 data)
  119. {
  120. unsigned long int si;
  121. int bl;
  122. /* TEA5757 shift register bits (see pdf) */
  123. outbit(0,io); // 24 search
  124. outbit(1,io); // 23 search up/down
  125. outbit(0,io); // 22 stereo/mono
  126. outbit(0,io); // 21 band
  127. outbit(0,io); // 20 band (only 00=FM works I think)
  128. outbit(0,io); // 19 port ?
  129. outbit(0,io); // 18 port ?
  130. outbit(0,io); // 17 search level
  131. outbit(0,io); // 16 search level
  132. si = 0x8000;
  133. for(bl = 1; bl <= 16 ; bl++) { outbit(data & si,io); si >>=1; }
  134. outb(power,io);
  135. }
  136. static int get_stereo(__u16 io)
  137. {
  138. outb(power,io); udelay(4);
  139. return !(inb(io) & mo_st);
  140. }
  141. static int get_tune(__u16 io)
  142. {
  143. outb(power+clk,io); udelay(4);
  144. return !(inb(io) & mo_st);
  145. }
  146. static inline int radio_function(struct inode *inode, struct file *file,
  147. unsigned int cmd, void *arg)
  148. {
  149. struct video_device *dev = video_devdata(file);
  150. struct radio_device *card=dev->priv;
  151. switch(cmd) {
  152. case VIDIOC_QUERYCAP:
  153. {
  154. struct v4l2_capability *v = arg;
  155. memset(v,0,sizeof(*v));
  156. strlcpy(v->driver, "radio-maxiradio", sizeof (v->driver));
  157. strlcpy(v->card, "Maxi Radio FM2000 radio", sizeof (v->card));
  158. sprintf(v->bus_info,"ISA");
  159. v->version = RADIO_VERSION;
  160. v->capabilities = V4L2_CAP_TUNER;
  161. return 0;
  162. }
  163. case VIDIOC_G_TUNER:
  164. {
  165. struct v4l2_tuner *v = arg;
  166. if (v->index > 0)
  167. return -EINVAL;
  168. memset(v,0,sizeof(*v));
  169. strcpy(v->name, "FM");
  170. v->type = V4L2_TUNER_RADIO;
  171. v->rangelow=FREQ_LO;
  172. v->rangehigh=FREQ_HI;
  173. v->rxsubchans =V4L2_TUNER_SUB_MONO|V4L2_TUNER_SUB_STEREO;
  174. v->capability=V4L2_TUNER_CAP_LOW;
  175. if(get_stereo(card->io))
  176. v->audmode = V4L2_TUNER_MODE_STEREO;
  177. else
  178. v->audmode = V4L2_TUNER_MODE_MONO;
  179. v->signal=0xffff*get_tune(card->io);
  180. return 0;
  181. }
  182. case VIDIOC_S_TUNER:
  183. {
  184. struct v4l2_tuner *v = arg;
  185. if (v->index > 0)
  186. return -EINVAL;
  187. return 0;
  188. }
  189. case VIDIOC_S_FREQUENCY:
  190. {
  191. struct v4l2_frequency *f = arg;
  192. if (f->frequency < FREQ_LO || f->frequency > FREQ_HI)
  193. return -EINVAL;
  194. card->freq = f->frequency;
  195. set_freq(card->io, FREQ2BITS(card->freq));
  196. msleep(125);
  197. return 0;
  198. }
  199. case VIDIOC_G_FREQUENCY:
  200. {
  201. struct v4l2_frequency *f = arg;
  202. f->type = V4L2_TUNER_RADIO;
  203. f->frequency = card->freq;
  204. return 0;
  205. }
  206. case VIDIOC_QUERYCTRL:
  207. {
  208. struct v4l2_queryctrl *qc = arg;
  209. int i;
  210. for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
  211. if (qc->id && qc->id == radio_qctrl[i].id) {
  212. memcpy(qc, &(radio_qctrl[i]),
  213. sizeof(*qc));
  214. return (0);
  215. }
  216. }
  217. return -EINVAL;
  218. }
  219. case VIDIOC_G_CTRL:
  220. {
  221. struct v4l2_control *ctrl= arg;
  222. switch (ctrl->id) {
  223. case V4L2_CID_AUDIO_MUTE:
  224. ctrl->value=card->muted;
  225. return (0);
  226. }
  227. return -EINVAL;
  228. }
  229. case VIDIOC_S_CTRL:
  230. {
  231. struct v4l2_control *ctrl= arg;
  232. switch (ctrl->id) {
  233. case V4L2_CID_AUDIO_MUTE:
  234. card->muted = ctrl->value;
  235. if(card->muted)
  236. turn_power(card->io, 0);
  237. else
  238. set_freq(card->io, FREQ2BITS(card->freq));
  239. return 0;
  240. }
  241. return -EINVAL;
  242. }
  243. default:
  244. return v4l_compat_translate_ioctl(inode,file,cmd,arg,
  245. radio_function);
  246. }
  247. }
  248. static int radio_ioctl(struct inode *inode, struct file *file,
  249. unsigned int cmd, unsigned long arg)
  250. {
  251. struct video_device *dev = video_devdata(file);
  252. struct radio_device *card=dev->priv;
  253. int ret;
  254. mutex_lock(&card->lock);
  255. ret = video_usercopy(inode, file, cmd, arg, radio_function);
  256. mutex_unlock(&card->lock);
  257. return ret;
  258. }
  259. MODULE_AUTHOR("Dimitromanolakis Apostolos, apdim@grecian.net");
  260. MODULE_DESCRIPTION("Radio driver for the Guillemot Maxi Radio FM2000 radio.");
  261. MODULE_LICENSE("GPL");
  262. static int __devinit maxiradio_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
  263. {
  264. if(!request_region(pci_resource_start(pdev, 0),
  265. pci_resource_len(pdev, 0), "Maxi Radio FM 2000")) {
  266. printk(KERN_ERR "radio-maxiradio: can't reserve I/O ports\n");
  267. goto err_out;
  268. }
  269. if (pci_enable_device(pdev))
  270. goto err_out_free_region;
  271. radio_unit.io = pci_resource_start(pdev, 0);
  272. mutex_init(&radio_unit.lock);
  273. maxiradio_radio.priv = &radio_unit;
  274. if(video_register_device(&maxiradio_radio, VFL_TYPE_RADIO, radio_nr)==-1) {
  275. printk("radio-maxiradio: can't register device!");
  276. goto err_out_free_region;
  277. }
  278. printk(KERN_INFO "radio-maxiradio: version "
  279. DRIVER_VERSION
  280. " time "
  281. __TIME__ " "
  282. __DATE__
  283. "\n");
  284. printk(KERN_INFO "radio-maxiradio: found Guillemot MAXI Radio device (io = 0x%x)\n",
  285. radio_unit.io);
  286. return 0;
  287. err_out_free_region:
  288. release_region(pci_resource_start(pdev, 0), pci_resource_len(pdev, 0));
  289. err_out:
  290. return -ENODEV;
  291. }
  292. static void __devexit maxiradio_remove_one(struct pci_dev *pdev)
  293. {
  294. video_unregister_device(&maxiradio_radio);
  295. release_region(pci_resource_start(pdev, 0), pci_resource_len(pdev, 0));
  296. }
  297. static struct pci_device_id maxiradio_pci_tbl[] = {
  298. { PCI_VENDOR_ID_GUILLEMOT, PCI_DEVICE_ID_GUILLEMOT_MAXIRADIO,
  299. PCI_ANY_ID, PCI_ANY_ID, },
  300. { 0,}
  301. };
  302. MODULE_DEVICE_TABLE(pci, maxiradio_pci_tbl);
  303. static struct pci_driver maxiradio_driver = {
  304. .name = "radio-maxiradio",
  305. .id_table = maxiradio_pci_tbl,
  306. .probe = maxiradio_init_one,
  307. .remove = __devexit_p(maxiradio_remove_one),
  308. };
  309. static int __init maxiradio_radio_init(void)
  310. {
  311. return pci_register_driver(&maxiradio_driver);
  312. }
  313. static void __exit maxiradio_radio_exit(void)
  314. {
  315. pci_unregister_driver(&maxiradio_driver);
  316. }
  317. module_init(maxiradio_radio_init);
  318. module_exit(maxiradio_radio_exit);