radio-maestro.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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 <linux/mutex.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. .name = "Maestro radio",
  81. .type = VID_TYPE_TUNER,
  82. .hardware = VID_HARDWARE_SF16MI,
  83. .fops = &maestro_fops,
  84. };
  85. struct radio_device {
  86. u16 io, /* base of Maestro card radio io (GPIO_DATA)*/
  87. muted, /* VIDEO_AUDIO_MUTE */
  88. stereo, /* VIDEO_TUNER_STEREO_ON */
  89. tuned; /* signal strength (0 or 0xffff) */
  90. struct mutex lock;
  91. };
  92. static u32 radio_bits_get(struct radio_device *dev)
  93. {
  94. register u16 io=dev->io, l, rdata;
  95. register u32 data=0;
  96. u16 omask;
  97. omask = inw(io + IO_MASK);
  98. outw(~(STR_CLK | STR_WREN), io + IO_MASK);
  99. outw(0, io);
  100. udelay(16);
  101. for (l=24;l--;) {
  102. outw(STR_CLK, io); /* HI state */
  103. udelay(2);
  104. if(!l)
  105. dev->tuned = inw(io) & STR_MOST ? 0 : 0xffff;
  106. outw(0, io); /* LO state */
  107. udelay(2);
  108. data <<= 1; /* shift data */
  109. rdata = inw(io);
  110. if(!l)
  111. dev->stereo = rdata & STR_MOST ?
  112. 0 : VIDEO_TUNER_STEREO_ON;
  113. else
  114. if(rdata & STR_DATA)
  115. data++;
  116. udelay(2);
  117. }
  118. if(dev->muted)
  119. outw(STR_WREN, io);
  120. udelay(4);
  121. outw(omask, io + IO_MASK);
  122. return data & 0x3ffe;
  123. }
  124. static void radio_bits_set(struct radio_device *dev, u32 data)
  125. {
  126. register u16 io=dev->io, l, bits;
  127. u16 omask, odir;
  128. omask = inw(io + IO_MASK);
  129. odir = (inw(io + IO_DIR) & ~STR_DATA) | (STR_CLK | STR_WREN);
  130. outw(odir | STR_DATA, io + IO_DIR);
  131. outw(~(STR_DATA | STR_CLK | STR_WREN), io + IO_MASK);
  132. udelay(16);
  133. for (l=25;l;l--) {
  134. bits = ((data >> 18) & STR_DATA) | STR_WREN ;
  135. data <<= 1; /* shift data */
  136. outw(bits, io); /* start strobe */
  137. udelay(2);
  138. outw(bits | STR_CLK, io); /* HI level */
  139. udelay(2);
  140. outw(bits, io); /* LO level */
  141. udelay(4);
  142. }
  143. if(!dev->muted)
  144. outw(0, io);
  145. udelay(4);
  146. outw(omask, io + IO_MASK);
  147. outw(odir, io + IO_DIR);
  148. msleep(125);
  149. }
  150. static inline int radio_function(struct inode *inode, struct file *file,
  151. unsigned int cmd, void *arg)
  152. {
  153. struct video_device *dev = video_devdata(file);
  154. struct radio_device *card = video_get_drvdata(dev);
  155. switch (cmd) {
  156. case VIDIOCGCAP: {
  157. struct video_capability *v = arg;
  158. memset(v, 0, sizeof(*v));
  159. strcpy(v->name, "Maestro radio");
  160. v->type = VID_TYPE_TUNER;
  161. v->channels = v->audios = 1;
  162. return 0;
  163. } case VIDIOCGTUNER: {
  164. struct video_tuner *v = arg;
  165. if (v->tuner)
  166. return -EINVAL;
  167. (void)radio_bits_get(card);
  168. v->flags = VIDEO_TUNER_LOW | card->stereo;
  169. v->signal = card->tuned;
  170. strcpy(v->name, "FM");
  171. v->rangelow = FREQ_LO;
  172. v->rangehigh = FREQ_HI;
  173. v->mode = VIDEO_MODE_AUTO;
  174. return 0;
  175. } case VIDIOCSTUNER: {
  176. struct video_tuner *v = arg;
  177. if (v->tuner != 0)
  178. return -EINVAL;
  179. return 0;
  180. } case VIDIOCGFREQ: {
  181. unsigned long *freq = arg;
  182. *freq = BITS2FREQ(radio_bits_get(card));
  183. return 0;
  184. } case VIDIOCSFREQ: {
  185. unsigned long *freq = arg;
  186. if (*freq < FREQ_LO || *freq > FREQ_HI)
  187. return -EINVAL;
  188. radio_bits_set(card, FREQ2BITS(*freq));
  189. return 0;
  190. } case VIDIOCGAUDIO: {
  191. struct video_audio *v = arg;
  192. memset(v, 0, sizeof(*v));
  193. strcpy(v->name, "Radio");
  194. v->flags = VIDEO_AUDIO_MUTABLE | card->muted;
  195. v->mode = VIDEO_SOUND_STEREO;
  196. return 0;
  197. } case VIDIOCSAUDIO: {
  198. struct video_audio *v = arg;
  199. if (v->audio)
  200. return -EINVAL;
  201. {
  202. register u16 io = card->io;
  203. register u16 omask = inw(io + IO_MASK);
  204. outw(~STR_WREN, io + IO_MASK);
  205. outw((card->muted = v->flags & VIDEO_AUDIO_MUTE) ?
  206. STR_WREN : 0, io);
  207. udelay(4);
  208. outw(omask, io + IO_MASK);
  209. msleep(125);
  210. return 0;
  211. }
  212. } case VIDIOCGUNIT: {
  213. struct video_unit *v = arg;
  214. v->video = VIDEO_NO_UNIT;
  215. v->vbi = VIDEO_NO_UNIT;
  216. v->radio = dev->minor;
  217. v->audio = 0;
  218. v->teletext = VIDEO_NO_UNIT;
  219. return 0;
  220. } default:
  221. return -ENOIOCTLCMD;
  222. }
  223. }
  224. static int radio_ioctl(struct inode *inode, struct file *file,
  225. unsigned int cmd, unsigned long arg)
  226. {
  227. struct video_device *dev = video_devdata(file);
  228. struct radio_device *card = video_get_drvdata(dev);
  229. int ret;
  230. mutex_lock(&card->lock);
  231. ret = video_usercopy(inode, file, cmd, arg, radio_function);
  232. mutex_unlock(&card->lock);
  233. return ret;
  234. }
  235. static u16 __devinit radio_power_on(struct radio_device *dev)
  236. {
  237. register u16 io = dev->io;
  238. register u32 ofreq;
  239. u16 omask, odir;
  240. omask = inw(io + IO_MASK);
  241. odir = (inw(io + IO_DIR) & ~STR_DATA) | (STR_CLK | STR_WREN);
  242. outw(odir & ~STR_WREN, io + IO_DIR);
  243. dev->muted = inw(io) & STR_WREN ? 0 : VIDEO_AUDIO_MUTE;
  244. outw(odir, io + IO_DIR);
  245. outw(~(STR_WREN | STR_CLK), io + IO_MASK);
  246. outw(dev->muted ? 0 : STR_WREN, io);
  247. udelay(16);
  248. outw(omask, io + IO_MASK);
  249. ofreq = radio_bits_get(dev);
  250. if ((ofreq < FREQ2BITS(FREQ_LO)) || (ofreq > FREQ2BITS(FREQ_HI)))
  251. ofreq = FREQ2BITS(FREQ_LO);
  252. radio_bits_set(dev, ofreq);
  253. return (ofreq == radio_bits_get(dev));
  254. }
  255. static int __devinit maestro_probe(struct pci_dev *pdev,
  256. const struct pci_device_id *ent)
  257. {
  258. struct radio_device *radio_unit;
  259. struct video_device *maestro_radio_inst;
  260. int retval;
  261. retval = pci_enable_device(pdev);
  262. if (retval) {
  263. dev_err(&pdev->dev, "enabling pci device failed!\n");
  264. goto err;
  265. }
  266. retval = -ENOMEM;
  267. radio_unit = kzalloc(sizeof(*radio_unit), GFP_KERNEL);
  268. if (radio_unit == NULL) {
  269. dev_err(&pdev->dev, "not enough memory\n");
  270. goto err;
  271. }
  272. radio_unit->io = pci_resource_start(pdev, 0) + GPIO_DATA;
  273. mutex_init(&radio_unit->lock);
  274. maestro_radio_inst = video_device_alloc();
  275. if (maestro_radio_inst == NULL) {
  276. dev_err(&pdev->dev, "not enough memory\n");
  277. goto errfr;
  278. }
  279. memcpy(maestro_radio_inst, &maestro_radio, sizeof(maestro_radio));
  280. video_set_drvdata(maestro_radio_inst, radio_unit);
  281. pci_set_drvdata(pdev, maestro_radio_inst);
  282. retval = video_register_device(maestro_radio_inst, VFL_TYPE_RADIO,
  283. radio_nr);
  284. if (retval) {
  285. printk(KERN_ERR "can't register video device!\n");
  286. goto errfr1;
  287. }
  288. if (!radio_power_on(radio_unit)) {
  289. retval = -EIO;
  290. goto errunr;
  291. }
  292. dev_info(&pdev->dev, "version " DRIVER_VERSION " time " __TIME__ " "
  293. __DATE__ "\n");
  294. dev_info(&pdev->dev, "radio chip initialized\n");
  295. return 0;
  296. errunr:
  297. video_unregister_device(maestro_radio_inst);
  298. errfr1:
  299. kfree(maestro_radio_inst);
  300. errfr:
  301. kfree(radio_unit);
  302. err:
  303. return retval;
  304. }
  305. static void __devexit maestro_remove(struct pci_dev *pdev)
  306. {
  307. struct video_device *vdev = pci_get_drvdata(pdev);
  308. video_unregister_device(vdev);
  309. }
  310. static int __init maestro_radio_init(void)
  311. {
  312. int retval = pci_register_driver(&maestro_r_driver);
  313. if (retval)
  314. printk(KERN_ERR "error during registration pci driver\n");
  315. return retval;
  316. }
  317. static void __exit maestro_radio_exit(void)
  318. {
  319. pci_unregister_driver(&maestro_r_driver);
  320. }
  321. module_init(maestro_radio_init);
  322. module_exit(maestro_radio_exit);
  323. MODULE_AUTHOR("Adam Tlalka, atlka@pg.gda.pl");
  324. MODULE_DESCRIPTION("Radio driver for the Maestro PCI sound card radio.");
  325. MODULE_LICENSE("GPL");