radio-maxiradio.c 11 KB

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