radio-maestro.c 11 KB

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