radio-maestro.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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. * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
  19. */
  20. #include <linux/module.h>
  21. #include <linux/init.h>
  22. #include <linux/ioport.h>
  23. #include <linux/delay.h>
  24. #include <linux/sched.h>
  25. #include <asm/io.h>
  26. #include <asm/uaccess.h>
  27. #include <linux/mutex.h>
  28. #include <linux/pci.h>
  29. #include <linux/videodev2.h>
  30. #include <media/v4l2-common.h>
  31. #include <linux/version.h> /* for KERNEL_VERSION MACRO */
  32. #define RADIO_VERSION KERNEL_VERSION(0,0,6)
  33. #define DRIVER_VERSION "0.06"
  34. static struct v4l2_queryctrl radio_qctrl[] = {
  35. {
  36. .id = V4L2_CID_AUDIO_MUTE,
  37. .name = "Mute",
  38. .minimum = 0,
  39. .maximum = 1,
  40. .default_value = 1,
  41. .type = V4L2_CTRL_TYPE_BOOLEAN,
  42. }
  43. };
  44. #define GPIO_DATA 0x60 /* port offset from ESS_IO_BASE */
  45. #define IO_MASK 4 /* mask register offset from GPIO_DATA
  46. bits 1=unmask write to given bit */
  47. #define IO_DIR 8 /* direction register offset from GPIO_DATA
  48. bits 0/1=read/write direction */
  49. #define GPIO6 0x0040 /* mask bits for GPIO lines */
  50. #define GPIO7 0x0080
  51. #define GPIO8 0x0100
  52. #define GPIO9 0x0200
  53. #define STR_DATA GPIO6 /* radio TEA5757 pins and GPIO bits */
  54. #define STR_CLK GPIO7
  55. #define STR_WREN GPIO8
  56. #define STR_MOST GPIO9
  57. #define FREQ_LO 50*16000
  58. #define FREQ_HI 150*16000
  59. #define FREQ_IF 171200 /* 10.7*16000 */
  60. #define FREQ_STEP 200 /* 12.5*16 */
  61. #define FREQ2BITS(x) ((((unsigned int)(x)+FREQ_IF+(FREQ_STEP<<1))\
  62. /(FREQ_STEP<<2))<<2) /* (x==fmhz*16*1000) -> bits */
  63. #define BITS2FREQ(x) ((x) * FREQ_STEP - FREQ_IF)
  64. static int radio_nr = -1;
  65. module_param(radio_nr, int, 0);
  66. static int radio_ioctl(struct inode *inode, struct file *file,
  67. unsigned int cmd, unsigned long arg);
  68. static int maestro_probe(struct pci_dev *pdev, const struct pci_device_id *ent);
  69. static void maestro_remove(struct pci_dev *pdev);
  70. static struct pci_device_id maestro_r_pci_tbl[] = {
  71. { PCI_DEVICE(PCI_VENDOR_ID_ESS, PCI_DEVICE_ID_ESS_ESS1968),
  72. .class = PCI_CLASS_MULTIMEDIA_AUDIO << 8,
  73. .class_mask = 0xffff00 },
  74. { PCI_DEVICE(PCI_VENDOR_ID_ESS, PCI_DEVICE_ID_ESS_ESS1978),
  75. .class = PCI_CLASS_MULTIMEDIA_AUDIO << 8,
  76. .class_mask = 0xffff00 },
  77. { 0 }
  78. };
  79. MODULE_DEVICE_TABLE(pci, maestro_r_pci_tbl);
  80. static struct pci_driver maestro_r_driver = {
  81. .name = "maestro_radio",
  82. .id_table = maestro_r_pci_tbl,
  83. .probe = maestro_probe,
  84. .remove = __devexit_p(maestro_remove),
  85. };
  86. static struct file_operations maestro_fops = {
  87. .owner = THIS_MODULE,
  88. .open = video_exclusive_open,
  89. .release = video_exclusive_release,
  90. .ioctl = radio_ioctl,
  91. .compat_ioctl = v4l_compat_ioctl32,
  92. .llseek = no_llseek,
  93. };
  94. static struct video_device maestro_radio = {
  95. .name = "Maestro radio",
  96. .type = VID_TYPE_TUNER,
  97. .hardware = 0,
  98. .fops = &maestro_fops,
  99. };
  100. struct radio_device {
  101. u16 io, /* base of Maestro card radio io (GPIO_DATA)*/
  102. muted, /* VIDEO_AUDIO_MUTE */
  103. stereo, /* VIDEO_TUNER_STEREO_ON */
  104. tuned; /* signal strength (0 or 0xffff) */
  105. struct mutex lock;
  106. };
  107. static u32 radio_bits_get(struct radio_device *dev)
  108. {
  109. register u16 io=dev->io, l, rdata;
  110. register u32 data=0;
  111. u16 omask;
  112. omask = inw(io + IO_MASK);
  113. outw(~(STR_CLK | STR_WREN), io + IO_MASK);
  114. outw(0, io);
  115. udelay(16);
  116. for (l=24;l--;) {
  117. outw(STR_CLK, io); /* HI state */
  118. udelay(2);
  119. if(!l)
  120. dev->tuned = inw(io) & STR_MOST ? 0 : 0xffff;
  121. outw(0, io); /* LO state */
  122. udelay(2);
  123. data <<= 1; /* shift data */
  124. rdata = inw(io);
  125. if(!l)
  126. dev->stereo = rdata & STR_MOST ?
  127. 0 : 1;
  128. else
  129. if(rdata & STR_DATA)
  130. data++;
  131. udelay(2);
  132. }
  133. if(dev->muted)
  134. outw(STR_WREN, io);
  135. udelay(4);
  136. outw(omask, io + IO_MASK);
  137. return data & 0x3ffe;
  138. }
  139. static void radio_bits_set(struct radio_device *dev, u32 data)
  140. {
  141. register u16 io=dev->io, l, bits;
  142. u16 omask, odir;
  143. omask = inw(io + IO_MASK);
  144. odir = (inw(io + IO_DIR) & ~STR_DATA) | (STR_CLK | STR_WREN);
  145. outw(odir | STR_DATA, io + IO_DIR);
  146. outw(~(STR_DATA | STR_CLK | STR_WREN), io + IO_MASK);
  147. udelay(16);
  148. for (l=25;l;l--) {
  149. bits = ((data >> 18) & STR_DATA) | STR_WREN ;
  150. data <<= 1; /* shift data */
  151. outw(bits, io); /* start strobe */
  152. udelay(2);
  153. outw(bits | STR_CLK, io); /* HI level */
  154. udelay(2);
  155. outw(bits, io); /* LO level */
  156. udelay(4);
  157. }
  158. if(!dev->muted)
  159. outw(0, io);
  160. udelay(4);
  161. outw(omask, io + IO_MASK);
  162. outw(odir, io + IO_DIR);
  163. msleep(125);
  164. }
  165. static inline int radio_function(struct inode *inode, struct file *file,
  166. unsigned int cmd, void *arg)
  167. {
  168. struct video_device *dev = video_devdata(file);
  169. struct radio_device *card = video_get_drvdata(dev);
  170. switch (cmd) {
  171. case VIDIOC_QUERYCAP:
  172. {
  173. struct v4l2_capability *v = arg;
  174. memset(v,0,sizeof(*v));
  175. strlcpy(v->driver, "radio-maestro", sizeof (v->driver));
  176. strlcpy(v->card, "Maestro Radio", sizeof (v->card));
  177. sprintf(v->bus_info,"PCI");
  178. v->version = RADIO_VERSION;
  179. v->capabilities = V4L2_CAP_TUNER;
  180. return 0;
  181. }
  182. case VIDIOC_G_TUNER:
  183. {
  184. struct v4l2_tuner *v = arg;
  185. if (v->index > 0)
  186. return -EINVAL;
  187. (void)radio_bits_get(card);
  188. memset(v,0,sizeof(*v));
  189. strcpy(v->name, "FM");
  190. v->type = V4L2_TUNER_RADIO;
  191. v->rangelow = FREQ_LO;
  192. v->rangehigh = FREQ_HI;
  193. v->rxsubchans =V4L2_TUNER_SUB_MONO|V4L2_TUNER_SUB_STEREO;
  194. v->capability=V4L2_TUNER_CAP_LOW;
  195. if(card->stereo)
  196. v->audmode = V4L2_TUNER_MODE_STEREO;
  197. else
  198. v->audmode = V4L2_TUNER_MODE_MONO;
  199. v->signal=card->tuned;
  200. return 0;
  201. }
  202. case VIDIOC_S_TUNER:
  203. {
  204. struct v4l2_tuner *v = arg;
  205. if (v->index > 0)
  206. return -EINVAL;
  207. return 0;
  208. }
  209. case VIDIOC_S_FREQUENCY:
  210. {
  211. struct v4l2_frequency *f = arg;
  212. if (f->frequency < FREQ_LO || f->frequency > FREQ_HI)
  213. return -EINVAL;
  214. radio_bits_set(card, FREQ2BITS(f->frequency));
  215. return 0;
  216. }
  217. case VIDIOC_G_FREQUENCY:
  218. {
  219. struct v4l2_frequency *f = arg;
  220. f->type = V4L2_TUNER_RADIO;
  221. f->frequency = BITS2FREQ(radio_bits_get(card));
  222. return 0;
  223. }
  224. case VIDIOC_QUERYCTRL:
  225. {
  226. struct v4l2_queryctrl *qc = arg;
  227. int i;
  228. for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
  229. if (qc->id && qc->id == radio_qctrl[i].id) {
  230. memcpy(qc, &(radio_qctrl[i]),
  231. sizeof(*qc));
  232. return (0);
  233. }
  234. }
  235. return -EINVAL;
  236. }
  237. case VIDIOC_G_CTRL:
  238. {
  239. struct v4l2_control *ctrl= arg;
  240. switch (ctrl->id) {
  241. case V4L2_CID_AUDIO_MUTE:
  242. ctrl->value=card->muted;
  243. return (0);
  244. }
  245. return -EINVAL;
  246. }
  247. case VIDIOC_S_CTRL:
  248. {
  249. struct v4l2_control *ctrl= arg;
  250. switch (ctrl->id) {
  251. case V4L2_CID_AUDIO_MUTE:
  252. {
  253. register u16 io = card->io;
  254. register u16 omask = inw(io + IO_MASK);
  255. outw(~STR_WREN, io + IO_MASK);
  256. outw((card->muted = ctrl->value ) ?
  257. STR_WREN : 0, io);
  258. udelay(4);
  259. outw(omask, io + IO_MASK);
  260. msleep(125);
  261. return (0);
  262. }
  263. }
  264. return -EINVAL;
  265. }
  266. default:
  267. return v4l_compat_translate_ioctl(inode,file,cmd,arg,
  268. radio_function);
  269. }
  270. }
  271. static int radio_ioctl(struct inode *inode, struct file *file,
  272. unsigned int cmd, unsigned long arg)
  273. {
  274. struct video_device *dev = video_devdata(file);
  275. struct radio_device *card = video_get_drvdata(dev);
  276. int ret;
  277. mutex_lock(&card->lock);
  278. ret = video_usercopy(inode, file, cmd, arg, radio_function);
  279. mutex_unlock(&card->lock);
  280. return ret;
  281. }
  282. static u16 __devinit radio_power_on(struct radio_device *dev)
  283. {
  284. register u16 io = dev->io;
  285. register u32 ofreq;
  286. u16 omask, odir;
  287. omask = inw(io + IO_MASK);
  288. odir = (inw(io + IO_DIR) & ~STR_DATA) | (STR_CLK | STR_WREN);
  289. outw(odir & ~STR_WREN, io + IO_DIR);
  290. dev->muted = inw(io) & STR_WREN ? 0 : 1;
  291. outw(odir, io + IO_DIR);
  292. outw(~(STR_WREN | STR_CLK), io + IO_MASK);
  293. outw(dev->muted ? 0 : STR_WREN, io);
  294. udelay(16);
  295. outw(omask, io + IO_MASK);
  296. ofreq = radio_bits_get(dev);
  297. if ((ofreq < FREQ2BITS(FREQ_LO)) || (ofreq > FREQ2BITS(FREQ_HI)))
  298. ofreq = FREQ2BITS(FREQ_LO);
  299. radio_bits_set(dev, ofreq);
  300. return (ofreq == radio_bits_get(dev));
  301. }
  302. static int __devinit maestro_probe(struct pci_dev *pdev,
  303. const struct pci_device_id *ent)
  304. {
  305. struct radio_device *radio_unit;
  306. struct video_device *maestro_radio_inst;
  307. int retval;
  308. retval = pci_enable_device(pdev);
  309. if (retval) {
  310. dev_err(&pdev->dev, "enabling pci device failed!\n");
  311. goto err;
  312. }
  313. retval = -ENOMEM;
  314. radio_unit = kzalloc(sizeof(*radio_unit), GFP_KERNEL);
  315. if (radio_unit == NULL) {
  316. dev_err(&pdev->dev, "not enough memory\n");
  317. goto err;
  318. }
  319. radio_unit->io = pci_resource_start(pdev, 0) + GPIO_DATA;
  320. mutex_init(&radio_unit->lock);
  321. maestro_radio_inst = video_device_alloc();
  322. if (maestro_radio_inst == NULL) {
  323. dev_err(&pdev->dev, "not enough memory\n");
  324. goto errfr;
  325. }
  326. memcpy(maestro_radio_inst, &maestro_radio, sizeof(maestro_radio));
  327. video_set_drvdata(maestro_radio_inst, radio_unit);
  328. pci_set_drvdata(pdev, maestro_radio_inst);
  329. retval = video_register_device(maestro_radio_inst, VFL_TYPE_RADIO,
  330. radio_nr);
  331. if (retval) {
  332. printk(KERN_ERR "can't register video device!\n");
  333. goto errfr1;
  334. }
  335. if (!radio_power_on(radio_unit)) {
  336. retval = -EIO;
  337. goto errunr;
  338. }
  339. dev_info(&pdev->dev, "version " DRIVER_VERSION " time " __TIME__ " "
  340. __DATE__ "\n");
  341. dev_info(&pdev->dev, "radio chip initialized\n");
  342. return 0;
  343. errunr:
  344. video_unregister_device(maestro_radio_inst);
  345. errfr1:
  346. kfree(maestro_radio_inst);
  347. errfr:
  348. kfree(radio_unit);
  349. err:
  350. return retval;
  351. }
  352. static void __devexit maestro_remove(struct pci_dev *pdev)
  353. {
  354. struct video_device *vdev = pci_get_drvdata(pdev);
  355. video_unregister_device(vdev);
  356. }
  357. static int __init maestro_radio_init(void)
  358. {
  359. int retval = pci_register_driver(&maestro_r_driver);
  360. if (retval)
  361. printk(KERN_ERR "error during registration pci driver\n");
  362. return retval;
  363. }
  364. static void __exit maestro_radio_exit(void)
  365. {
  366. pci_unregister_driver(&maestro_r_driver);
  367. }
  368. module_init(maestro_radio_init);
  369. module_exit(maestro_radio_exit);
  370. MODULE_AUTHOR("Adam Tlalka, atlka@pg.gda.pl");
  371. MODULE_DESCRIPTION("Radio driver for the Maestro PCI sound card radio.");
  372. MODULE_LICENSE("GPL");