radio-maestro.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /* Maestro PCI sound card radio driver for Linux support
  2. * (c) 2000 A. Tlalka, atlka@pg.gda.pl
  3. * Notes on the hardware
  4. *
  5. * + Frequency control is done digitally
  6. * + No volume control - only mute/unmute - you have to use Aux line volume
  7. * control on Maestro card to set the volume
  8. * + Radio status (tuned/not_tuned and stereo/mono) is valid some time after
  9. * frequency setting (>100ms) and only when the radio is unmuted.
  10. * version 0.02
  11. * + io port is automatically detected - only the first radio is used
  12. * version 0.03
  13. * + thread access locking additions
  14. * version 0.04
  15. * + code improvements
  16. * + VIDEO_TUNER_LOW is permanent
  17. */
  18. #include <linux/module.h>
  19. #include <linux/init.h>
  20. #include <linux/ioport.h>
  21. #include <linux/delay.h>
  22. #include <linux/sched.h>
  23. #include <asm/io.h>
  24. #include <asm/uaccess.h>
  25. #include <asm/semaphore.h>
  26. #include <linux/pci.h>
  27. #include <linux/videodev.h>
  28. #define DRIVER_VERSION "0.05"
  29. #define GPIO_DATA 0x60 /* port offset from ESS_IO_BASE */
  30. #define IO_MASK 4 /* mask register offset from GPIO_DATA
  31. bits 1=unmask write to given bit */
  32. #define IO_DIR 8 /* direction register offset from GPIO_DATA
  33. bits 0/1=read/write direction */
  34. #define GPIO6 0x0040 /* mask bits for GPIO lines */
  35. #define GPIO7 0x0080
  36. #define GPIO8 0x0100
  37. #define GPIO9 0x0200
  38. #define STR_DATA GPIO6 /* radio TEA5757 pins and GPIO bits */
  39. #define STR_CLK GPIO7
  40. #define STR_WREN GPIO8
  41. #define STR_MOST GPIO9
  42. #define FREQ_LO 50*16000
  43. #define FREQ_HI 150*16000
  44. #define FREQ_IF 171200 /* 10.7*16000 */
  45. #define FREQ_STEP 200 /* 12.5*16 */
  46. #define FREQ2BITS(x) ((((unsigned int)(x)+FREQ_IF+(FREQ_STEP<<1))\
  47. /(FREQ_STEP<<2))<<2) /* (x==fmhz*16*1000) -> bits */
  48. #define BITS2FREQ(x) ((x) * FREQ_STEP - FREQ_IF)
  49. static int radio_nr = -1;
  50. module_param(radio_nr, int, 0);
  51. static int radio_ioctl(struct inode *inode, struct file *file,
  52. unsigned int cmd, unsigned long arg);
  53. static int maestro_probe(struct pci_dev *pdev, const struct pci_device_id *ent);
  54. static void maestro_remove(struct pci_dev *pdev);
  55. static struct pci_device_id maestro_r_pci_tbl[] = {
  56. { PCI_DEVICE(PCI_VENDOR_ID_ESS, PCI_DEVICE_ID_ESS_ESS1968),
  57. .class = PCI_CLASS_MULTIMEDIA_AUDIO << 8,
  58. .class_mask = 0xffff00 },
  59. { PCI_DEVICE(PCI_VENDOR_ID_ESS, PCI_DEVICE_ID_ESS_ESS1978),
  60. .class = PCI_CLASS_MULTIMEDIA_AUDIO << 8,
  61. .class_mask = 0xffff00 },
  62. { 0 }
  63. };
  64. MODULE_DEVICE_TABLE(pci, maestro_r_pci_tbl);
  65. static struct pci_driver maestro_r_driver = {
  66. .name = "maestro_radio",
  67. .id_table = maestro_r_pci_tbl,
  68. .probe = maestro_probe,
  69. .remove = __devexit_p(maestro_remove),
  70. };
  71. static struct file_operations maestro_fops = {
  72. .owner = THIS_MODULE,
  73. .open = video_exclusive_open,
  74. .release = video_exclusive_release,
  75. .ioctl = radio_ioctl,
  76. .compat_ioctl = v4l_compat_ioctl32,
  77. .llseek = no_llseek,
  78. };
  79. static struct video_device maestro_radio=
  80. {
  81. .owner = THIS_MODULE,
  82. .name = "Maestro radio",
  83. .type = VID_TYPE_TUNER,
  84. .hardware = VID_HARDWARE_SF16MI,
  85. .fops = &maestro_fops,
  86. };
  87. struct radio_device
  88. {
  89. __u16 io, /* base of Maestro card radio io (GPIO_DATA)*/
  90. muted, /* VIDEO_AUDIO_MUTE */
  91. stereo, /* VIDEO_TUNER_STEREO_ON */
  92. tuned; /* signal strength (0 or 0xffff) */
  93. struct semaphore lock;
  94. };
  95. static __u32 radio_bits_get(struct radio_device *dev)
  96. {
  97. register __u16 io=dev->io, l, rdata;
  98. register __u32 data=0;
  99. __u16 omask;
  100. omask = inw(io + IO_MASK);
  101. outw(~(STR_CLK | STR_WREN), io + IO_MASK);
  102. outw(0, io);
  103. udelay(16);
  104. for (l=24;l--;) {
  105. outw(STR_CLK, io); /* HI state */
  106. udelay(2);
  107. if(!l)
  108. dev->tuned = inw(io) & STR_MOST ? 0 : 0xffff;
  109. outw(0, io); /* LO state */
  110. udelay(2);
  111. data <<= 1; /* shift data */
  112. rdata = inw(io);
  113. if(!l)
  114. dev->stereo = rdata & STR_MOST ?
  115. 0 : VIDEO_TUNER_STEREO_ON;
  116. else
  117. if(rdata & STR_DATA)
  118. data++;
  119. udelay(2);
  120. }
  121. if(dev->muted)
  122. outw(STR_WREN, io);
  123. udelay(4);
  124. outw(omask, io + IO_MASK);
  125. return data & 0x3ffe;
  126. }
  127. static void radio_bits_set(struct radio_device *dev, __u32 data)
  128. {
  129. register __u16 io=dev->io, l, bits;
  130. __u16 omask, odir;
  131. omask = inw(io + IO_MASK);
  132. odir = (inw(io + IO_DIR) & ~STR_DATA) | (STR_CLK | STR_WREN);
  133. outw(odir | STR_DATA, io + IO_DIR);
  134. outw(~(STR_DATA | STR_CLK | STR_WREN), io + IO_MASK);
  135. udelay(16);
  136. for (l=25;l;l--) {
  137. bits = ((data >> 18) & STR_DATA) | STR_WREN ;
  138. data <<= 1; /* shift data */
  139. outw(bits, io); /* start strobe */
  140. udelay(2);
  141. outw(bits | STR_CLK, io); /* HI level */
  142. udelay(2);
  143. outw(bits, io); /* LO level */
  144. udelay(4);
  145. }
  146. if(!dev->muted)
  147. outw(0, io);
  148. udelay(4);
  149. outw(omask, io + IO_MASK);
  150. outw(odir, io + IO_DIR);
  151. msleep(125);
  152. }
  153. static inline int radio_function(struct inode *inode, struct file *file,
  154. unsigned int cmd, void *arg)
  155. {
  156. struct video_device *dev = video_devdata(file);
  157. struct radio_device *card=dev->priv;
  158. switch(cmd) {
  159. case VIDIOCGCAP: {
  160. struct video_capability *v = arg;
  161. memset(v,0,sizeof(*v));
  162. strcpy(v->name, "Maestro radio");
  163. v->type=VID_TYPE_TUNER;
  164. v->channels=v->audios=1;
  165. return 0;
  166. }
  167. case VIDIOCGTUNER: {
  168. struct video_tuner *v = arg;
  169. if(v->tuner)
  170. return -EINVAL;
  171. (void)radio_bits_get(card);
  172. v->flags = VIDEO_TUNER_LOW | card->stereo;
  173. v->signal = card->tuned;
  174. strcpy(v->name, "FM");
  175. v->rangelow = FREQ_LO;
  176. v->rangehigh = FREQ_HI;
  177. v->mode = VIDEO_MODE_AUTO;
  178. return 0;
  179. }
  180. case VIDIOCSTUNER: {
  181. struct video_tuner *v = arg;
  182. if(v->tuner!=0)
  183. return -EINVAL;
  184. return 0;
  185. }
  186. case VIDIOCGFREQ: {
  187. unsigned long *freq = arg;
  188. *freq = BITS2FREQ(radio_bits_get(card));
  189. return 0;
  190. }
  191. case VIDIOCSFREQ: {
  192. unsigned long *freq = arg;
  193. if (*freq<FREQ_LO || *freq>FREQ_HI )
  194. return -EINVAL;
  195. radio_bits_set(card, FREQ2BITS(*freq));
  196. return 0;
  197. }
  198. case VIDIOCGAUDIO: {
  199. struct video_audio *v = arg;
  200. memset(v,0,sizeof(*v));
  201. strcpy(v->name, "Radio");
  202. v->flags=VIDEO_AUDIO_MUTABLE | card->muted;
  203. v->mode=VIDEO_SOUND_STEREO;
  204. return 0;
  205. }
  206. case VIDIOCSAUDIO: {
  207. struct video_audio *v = arg;
  208. if(v->audio)
  209. return -EINVAL;
  210. {
  211. register __u16 io=card->io;
  212. register __u16 omask = inw(io + IO_MASK);
  213. outw(~STR_WREN, io + IO_MASK);
  214. outw((card->muted = v->flags & VIDEO_AUDIO_MUTE)
  215. ? STR_WREN : 0, io);
  216. udelay(4);
  217. outw(omask, io + IO_MASK);
  218. msleep(125);
  219. return 0;
  220. }
  221. }
  222. case VIDIOCGUNIT: {
  223. struct video_unit *v = arg;
  224. v->video=VIDEO_NO_UNIT;
  225. v->vbi=VIDEO_NO_UNIT;
  226. v->radio=dev->minor;
  227. v->audio=0;
  228. v->teletext=VIDEO_NO_UNIT;
  229. return 0;
  230. }
  231. default: return -ENOIOCTLCMD;
  232. }
  233. }
  234. static int radio_ioctl(struct inode *inode, struct file *file,
  235. unsigned int cmd, unsigned long arg)
  236. {
  237. struct video_device *dev = video_devdata(file);
  238. struct radio_device *card=dev->priv;
  239. int ret;
  240. down(&card->lock);
  241. ret = video_usercopy(inode, file, cmd, arg, radio_function);
  242. up(&card->lock);
  243. return ret;
  244. }
  245. static __u16 __devinit radio_power_on(struct radio_device *dev)
  246. {
  247. register __u16 io=dev->io;
  248. register __u32 ofreq;
  249. __u16 omask, odir;
  250. omask = inw(io + IO_MASK);
  251. odir = (inw(io + IO_DIR) & ~STR_DATA) | (STR_CLK | STR_WREN);
  252. outw(odir & ~STR_WREN, io + IO_DIR);
  253. dev->muted = inw(io) & STR_WREN ? 0 : VIDEO_AUDIO_MUTE;
  254. outw(odir, io + IO_DIR);
  255. outw(~(STR_WREN | STR_CLK), io + IO_MASK);
  256. outw(dev->muted ? 0 : STR_WREN, io);
  257. udelay(16);
  258. outw(omask, io + IO_MASK);
  259. ofreq = radio_bits_get(dev);
  260. if((ofreq<FREQ2BITS(FREQ_LO)) || (ofreq>FREQ2BITS(FREQ_HI)))
  261. ofreq = FREQ2BITS(FREQ_LO);
  262. radio_bits_set(dev, ofreq);
  263. return (ofreq == radio_bits_get(dev));
  264. }
  265. static int __devinit maestro_probe(struct pci_dev *pdev,
  266. const struct pci_device_id *ent)
  267. {
  268. struct radio_device *radio_unit;
  269. struct video_device *maestro_radio_inst;
  270. int retval;
  271. retval = pci_enable_device(pdev);
  272. if (retval) {
  273. dev_err(&pdev->dev, "enabling pci device failed!\n");
  274. goto err;
  275. }
  276. retval = -ENOMEM;
  277. radio_unit = kzalloc(sizeof(*radio_unit), GFP_KERNEL);
  278. if (radio_unit == NULL) {
  279. dev_err(&pdev->dev, "not enough memory\n");
  280. goto err;
  281. }
  282. radio_unit->io = pci_resource_start(pdev, 0) + GPIO_DATA;
  283. init_MUTEX(&radio_unit->lock);
  284. maestro_radio_inst = video_device_alloc();
  285. if (maestro_radio_inst == NULL) {
  286. dev_err(&pdev->dev, "not enough memory\n");
  287. goto errfr;
  288. }
  289. memcpy(maestro_radio_inst, &maestro_radio, sizeof(maestro_radio));
  290. video_set_drvdata(maestro_radio_inst, radio_unit);
  291. pci_set_drvdata(pdev, maestro_radio_inst);
  292. retval = video_register_device(maestro_radio_inst, VFL_TYPE_RADIO,
  293. radio_nr);
  294. if (retval) {
  295. printk(KERN_ERR "can't register video device!\n");
  296. goto errfr1;
  297. }
  298. if (!radio_power_on(radio_unit)) {
  299. retval = -EIO;
  300. goto errunr;
  301. }
  302. dev_info(&pdev->dev, "version " DRIVER_VERSION " time " __TIME__ " "
  303. __DATE__ "\n");
  304. dev_info(&pdev->dev, "radio chip initialized\n");
  305. return 0;
  306. errunr:
  307. video_unregister_device(maestro_radio_inst);
  308. errfr1:
  309. kfree(maestro_radio_inst);
  310. errfr:
  311. kfree(radio_unit);
  312. err:
  313. return retval;
  314. }
  315. static void __devexit maestro_remove(struct pci_dev *pdev)
  316. {
  317. struct video_device *vdev = pci_get_drvdata(pdev);
  318. video_unregister_device(vdev);
  319. }
  320. MODULE_AUTHOR("Adam Tlalka, atlka@pg.gda.pl");
  321. MODULE_DESCRIPTION("Radio driver for the Maestro PCI sound card radio.");
  322. MODULE_LICENSE("GPL");
  323. static int __init maestro_radio_init(void)
  324. {
  325. int retval = pci_register_driver(&maestro_r_driver);
  326. if (retval)
  327. printk(KERN_ERR "error during registration pci driver\n");
  328. return retval;
  329. }
  330. static void __exit maestro_radio_exit(void)
  331. {
  332. pci_unregister_driver(&maestro_r_driver);
  333. }
  334. module_init(maestro_radio_init);
  335. module_exit(maestro_radio_exit);