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