radio-maxiradio.c 9.9 KB

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