radio-maestro.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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.04"
  29. #define PCI_VENDOR_ESS 0x125D
  30. #define PCI_DEVICE_ID_ESS_ESS1968 0x1968 /* Maestro 2 */
  31. #define PCI_DEVICE_ID_ESS_ESS1978 0x1978 /* Maestro 2E */
  32. #define GPIO_DATA 0x60 /* port offset from ESS_IO_BASE */
  33. #define IO_MASK 4 /* mask register offset from GPIO_DATA
  34. bits 1=unmask write to given bit */
  35. #define IO_DIR 8 /* direction register offset from GPIO_DATA
  36. bits 0/1=read/write direction */
  37. #define GPIO6 0x0040 /* mask bits for GPIO lines */
  38. #define GPIO7 0x0080
  39. #define GPIO8 0x0100
  40. #define GPIO9 0x0200
  41. #define STR_DATA GPIO6 /* radio TEA5757 pins and GPIO bits */
  42. #define STR_CLK GPIO7
  43. #define STR_WREN GPIO8
  44. #define STR_MOST GPIO9
  45. #define FREQ_LO 50*16000
  46. #define FREQ_HI 150*16000
  47. #define FREQ_IF 171200 /* 10.7*16000 */
  48. #define FREQ_STEP 200 /* 12.5*16 */
  49. #define FREQ2BITS(x) ((((unsigned int)(x)+FREQ_IF+(FREQ_STEP<<1))\
  50. /(FREQ_STEP<<2))<<2) /* (x==fmhz*16*1000) -> bits */
  51. #define BITS2FREQ(x) ((x) * FREQ_STEP - FREQ_IF)
  52. static int radio_nr = -1;
  53. module_param(radio_nr, int, 0);
  54. static int radio_ioctl(struct inode *inode, struct file *file,
  55. unsigned int cmd, unsigned long arg);
  56. static struct file_operations maestro_fops = {
  57. .owner = THIS_MODULE,
  58. .open = video_exclusive_open,
  59. .release = video_exclusive_release,
  60. .ioctl = radio_ioctl,
  61. .compat_ioctl = v4l_compat_ioctl32,
  62. .llseek = no_llseek,
  63. };
  64. static struct video_device maestro_radio=
  65. {
  66. .owner = THIS_MODULE,
  67. .name = "Maestro radio",
  68. .type = VID_TYPE_TUNER,
  69. .hardware = VID_HARDWARE_SF16MI,
  70. .fops = &maestro_fops,
  71. };
  72. static struct radio_device
  73. {
  74. __u16 io, /* base of Maestro card radio io (GPIO_DATA)*/
  75. muted, /* VIDEO_AUDIO_MUTE */
  76. stereo, /* VIDEO_TUNER_STEREO_ON */
  77. tuned; /* signal strength (0 or 0xffff) */
  78. struct semaphore lock;
  79. } radio_unit = {0, 0, 0, 0, };
  80. static __u32 radio_bits_get(struct radio_device *dev)
  81. {
  82. register __u16 io=dev->io, l, rdata;
  83. register __u32 data=0;
  84. __u16 omask;
  85. omask = inw(io + IO_MASK);
  86. outw(~(STR_CLK | STR_WREN), io + IO_MASK);
  87. outw(0, io);
  88. udelay(16);
  89. for (l=24;l--;) {
  90. outw(STR_CLK, io); /* HI state */
  91. udelay(2);
  92. if(!l)
  93. dev->tuned = inw(io) & STR_MOST ? 0 : 0xffff;
  94. outw(0, io); /* LO state */
  95. udelay(2);
  96. data <<= 1; /* shift data */
  97. rdata = inw(io);
  98. if(!l)
  99. dev->stereo = rdata & STR_MOST ?
  100. 0 : VIDEO_TUNER_STEREO_ON;
  101. else
  102. if(rdata & STR_DATA)
  103. data++;
  104. udelay(2);
  105. }
  106. if(dev->muted)
  107. outw(STR_WREN, io);
  108. udelay(4);
  109. outw(omask, io + IO_MASK);
  110. return data & 0x3ffe;
  111. }
  112. static void radio_bits_set(struct radio_device *dev, __u32 data)
  113. {
  114. register __u16 io=dev->io, l, bits;
  115. __u16 omask, odir;
  116. omask = inw(io + IO_MASK);
  117. odir = (inw(io + IO_DIR) & ~STR_DATA) | (STR_CLK | STR_WREN);
  118. outw(odir | STR_DATA, io + IO_DIR);
  119. outw(~(STR_DATA | STR_CLK | STR_WREN), io + IO_MASK);
  120. udelay(16);
  121. for (l=25;l;l--) {
  122. bits = ((data >> 18) & STR_DATA) | STR_WREN ;
  123. data <<= 1; /* shift data */
  124. outw(bits, io); /* start strobe */
  125. udelay(2);
  126. outw(bits | STR_CLK, io); /* HI level */
  127. udelay(2);
  128. outw(bits, io); /* LO level */
  129. udelay(4);
  130. }
  131. if(!dev->muted)
  132. outw(0, io);
  133. udelay(4);
  134. outw(omask, io + IO_MASK);
  135. outw(odir, io + IO_DIR);
  136. msleep(125);
  137. }
  138. static inline int radio_function(struct inode *inode, struct file *file,
  139. unsigned int cmd, void *arg)
  140. {
  141. struct video_device *dev = video_devdata(file);
  142. struct radio_device *card=dev->priv;
  143. switch(cmd) {
  144. case VIDIOCGCAP: {
  145. struct video_capability *v = arg;
  146. memset(v,0,sizeof(*v));
  147. strcpy(v->name, "Maestro radio");
  148. v->type=VID_TYPE_TUNER;
  149. v->channels=v->audios=1;
  150. return 0;
  151. }
  152. case VIDIOCGTUNER: {
  153. struct video_tuner *v = arg;
  154. if(v->tuner)
  155. return -EINVAL;
  156. (void)radio_bits_get(card);
  157. v->flags = VIDEO_TUNER_LOW | card->stereo;
  158. v->signal = card->tuned;
  159. strcpy(v->name, "FM");
  160. v->rangelow = FREQ_LO;
  161. v->rangehigh = FREQ_HI;
  162. v->mode = VIDEO_MODE_AUTO;
  163. return 0;
  164. }
  165. case VIDIOCSTUNER: {
  166. struct video_tuner *v = arg;
  167. if(v->tuner!=0)
  168. return -EINVAL;
  169. return 0;
  170. }
  171. case VIDIOCGFREQ: {
  172. unsigned long *freq = arg;
  173. *freq = BITS2FREQ(radio_bits_get(card));
  174. return 0;
  175. }
  176. case VIDIOCSFREQ: {
  177. unsigned long *freq = arg;
  178. if (*freq<FREQ_LO || *freq>FREQ_HI )
  179. return -EINVAL;
  180. radio_bits_set(card, FREQ2BITS(*freq));
  181. return 0;
  182. }
  183. case VIDIOCGAUDIO: {
  184. struct video_audio *v = arg;
  185. memset(v,0,sizeof(*v));
  186. strcpy(v->name, "Radio");
  187. v->flags=VIDEO_AUDIO_MUTABLE | card->muted;
  188. v->mode=VIDEO_SOUND_STEREO;
  189. return 0;
  190. }
  191. case VIDIOCSAUDIO: {
  192. struct video_audio *v = arg;
  193. if(v->audio)
  194. return -EINVAL;
  195. {
  196. register __u16 io=card->io;
  197. register __u16 omask = inw(io + IO_MASK);
  198. outw(~STR_WREN, io + IO_MASK);
  199. outw((card->muted = v->flags & VIDEO_AUDIO_MUTE)
  200. ? STR_WREN : 0, io);
  201. udelay(4);
  202. outw(omask, io + IO_MASK);
  203. msleep(125);
  204. return 0;
  205. }
  206. }
  207. case VIDIOCGUNIT: {
  208. struct video_unit *v = arg;
  209. v->video=VIDEO_NO_UNIT;
  210. v->vbi=VIDEO_NO_UNIT;
  211. v->radio=dev->minor;
  212. v->audio=0;
  213. v->teletext=VIDEO_NO_UNIT;
  214. return 0;
  215. }
  216. default: return -ENOIOCTLCMD;
  217. }
  218. }
  219. static int radio_ioctl(struct inode *inode, struct file *file,
  220. unsigned int cmd, unsigned long arg)
  221. {
  222. struct video_device *dev = video_devdata(file);
  223. struct radio_device *card=dev->priv;
  224. int ret;
  225. down(&card->lock);
  226. ret = video_usercopy(inode, file, cmd, arg, radio_function);
  227. up(&card->lock);
  228. return ret;
  229. }
  230. static __u16 radio_install(struct pci_dev *pcidev);
  231. MODULE_AUTHOR("Adam Tlalka, atlka@pg.gda.pl");
  232. MODULE_DESCRIPTION("Radio driver for the Maestro PCI sound card radio.");
  233. MODULE_LICENSE("GPL");
  234. static void __exit maestro_radio_exit(void)
  235. {
  236. video_unregister_device(&maestro_radio);
  237. }
  238. static int __init maestro_radio_init(void)
  239. {
  240. register __u16 found=0;
  241. struct pci_dev *pcidev = NULL;
  242. while(!found && (pcidev = pci_find_device(PCI_VENDOR_ESS,
  243. PCI_DEVICE_ID_ESS_ESS1968,
  244. pcidev)))
  245. found |= radio_install(pcidev);
  246. while(!found && (pcidev = pci_find_device(PCI_VENDOR_ESS,
  247. PCI_DEVICE_ID_ESS_ESS1978,
  248. pcidev)))
  249. found |= radio_install(pcidev);
  250. if(!found) {
  251. printk(KERN_INFO "radio-maestro: no devices found.\n");
  252. return -ENODEV;
  253. }
  254. return 0;
  255. }
  256. module_init(maestro_radio_init);
  257. module_exit(maestro_radio_exit);
  258. static inline __u16 radio_power_on(struct radio_device *dev)
  259. {
  260. register __u16 io=dev->io;
  261. register __u32 ofreq;
  262. __u16 omask, odir;
  263. omask = inw(io + IO_MASK);
  264. odir = (inw(io + IO_DIR) & ~STR_DATA) | (STR_CLK | STR_WREN);
  265. outw(odir & ~STR_WREN, io + IO_DIR);
  266. dev->muted = inw(io) & STR_WREN ? 0 : VIDEO_AUDIO_MUTE;
  267. outw(odir, io + IO_DIR);
  268. outw(~(STR_WREN | STR_CLK), io + IO_MASK);
  269. outw(dev->muted ? 0 : STR_WREN, io);
  270. udelay(16);
  271. outw(omask, io + IO_MASK);
  272. ofreq = radio_bits_get(dev);
  273. if((ofreq<FREQ2BITS(FREQ_LO)) || (ofreq>FREQ2BITS(FREQ_HI)))
  274. ofreq = FREQ2BITS(FREQ_LO);
  275. radio_bits_set(dev, ofreq);
  276. return (ofreq == radio_bits_get(dev));
  277. }
  278. static __u16 radio_install(struct pci_dev *pcidev)
  279. {
  280. if(((pcidev->class >> 8) & 0xffff) != PCI_CLASS_MULTIMEDIA_AUDIO)
  281. return 0;
  282. radio_unit.io = pcidev->resource[0].start + GPIO_DATA;
  283. maestro_radio.priv = &radio_unit;
  284. init_MUTEX(&radio_unit.lock);
  285. if(radio_power_on(&radio_unit)) {
  286. if(video_register_device(&maestro_radio, VFL_TYPE_RADIO, radio_nr)==-1) {
  287. printk("radio-maestro: can't register device!");
  288. return 0;
  289. }
  290. printk(KERN_INFO "radio-maestro: version "
  291. DRIVER_VERSION
  292. " time "
  293. __TIME__ " "
  294. __DATE__
  295. "\n");
  296. printk(KERN_INFO "radio-maestro: radio chip initialized\n");
  297. return 1;
  298. } else
  299. return 0;
  300. }