radio-maestro.c 8.6 KB

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