radio-maestro.c 11 KB

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