radio-maxiradio.c 11 KB

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