radio-maestro.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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/uaccess.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->frequency < FREQ_LO || f->frequency > FREQ_HI)
  171. return -EINVAL;
  172. mutex_lock(&dev->lock);
  173. radio_bits_set(dev, FREQ2BITS(f->frequency));
  174. mutex_unlock(&dev->lock);
  175. return 0;
  176. }
  177. static int vidioc_g_frequency(struct file *file, void *priv,
  178. struct v4l2_frequency *f)
  179. {
  180. struct maestro *dev = video_drvdata(file);
  181. f->type = V4L2_TUNER_RADIO;
  182. mutex_lock(&dev->lock);
  183. f->frequency = BITS2FREQ(radio_bits_get(dev));
  184. mutex_unlock(&dev->lock);
  185. return 0;
  186. }
  187. static int vidioc_queryctrl(struct file *file, void *priv,
  188. struct v4l2_queryctrl *qc)
  189. {
  190. switch (qc->id) {
  191. case V4L2_CID_AUDIO_MUTE:
  192. return v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
  193. }
  194. return -EINVAL;
  195. }
  196. static int vidioc_g_ctrl(struct file *file, void *priv,
  197. struct v4l2_control *ctrl)
  198. {
  199. struct maestro *dev = video_drvdata(file);
  200. switch (ctrl->id) {
  201. case V4L2_CID_AUDIO_MUTE:
  202. ctrl->value = dev->muted;
  203. return 0;
  204. }
  205. return -EINVAL;
  206. }
  207. static int vidioc_s_ctrl(struct file *file, void *priv,
  208. struct v4l2_control *ctrl)
  209. {
  210. struct maestro *dev = video_drvdata(file);
  211. u16 io = dev->io;
  212. u16 omask;
  213. switch (ctrl->id) {
  214. case V4L2_CID_AUDIO_MUTE:
  215. mutex_lock(&dev->lock);
  216. omask = inw(io + IO_MASK);
  217. outw(~STR_WREN, io + IO_MASK);
  218. dev->muted = ctrl->value;
  219. outw(dev->muted ? STR_WREN : 0, io);
  220. udelay(4);
  221. outw(omask, io + IO_MASK);
  222. msleep(125);
  223. mutex_unlock(&dev->lock);
  224. return 0;
  225. }
  226. return -EINVAL;
  227. }
  228. static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
  229. {
  230. *i = 0;
  231. return 0;
  232. }
  233. static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
  234. {
  235. return i ? -EINVAL : 0;
  236. }
  237. static int vidioc_g_audio(struct file *file, void *priv,
  238. struct v4l2_audio *a)
  239. {
  240. a->index = 0;
  241. strlcpy(a->name, "Radio", sizeof(a->name));
  242. a->capability = V4L2_AUDCAP_STEREO;
  243. return 0;
  244. }
  245. static int vidioc_s_audio(struct file *file, void *priv,
  246. struct v4l2_audio *a)
  247. {
  248. return a->index ? -EINVAL : 0;
  249. }
  250. static int maestro_open(struct file *file)
  251. {
  252. return 0;
  253. }
  254. static int maestro_release(struct file *file)
  255. {
  256. return 0;
  257. }
  258. static const struct v4l2_file_operations maestro_fops = {
  259. .owner = THIS_MODULE,
  260. .open = maestro_open,
  261. .release = maestro_release,
  262. .ioctl = video_ioctl2,
  263. };
  264. static const struct v4l2_ioctl_ops maestro_ioctl_ops = {
  265. .vidioc_querycap = vidioc_querycap,
  266. .vidioc_g_tuner = vidioc_g_tuner,
  267. .vidioc_s_tuner = vidioc_s_tuner,
  268. .vidioc_g_audio = vidioc_g_audio,
  269. .vidioc_s_audio = vidioc_s_audio,
  270. .vidioc_g_input = vidioc_g_input,
  271. .vidioc_s_input = vidioc_s_input,
  272. .vidioc_g_frequency = vidioc_g_frequency,
  273. .vidioc_s_frequency = vidioc_s_frequency,
  274. .vidioc_queryctrl = vidioc_queryctrl,
  275. .vidioc_g_ctrl = vidioc_g_ctrl,
  276. .vidioc_s_ctrl = vidioc_s_ctrl,
  277. };
  278. static u16 __devinit radio_power_on(struct maestro *dev)
  279. {
  280. register u16 io = dev->io;
  281. register u32 ofreq;
  282. u16 omask, odir;
  283. omask = inw(io + IO_MASK);
  284. odir = (inw(io + IO_DIR) & ~STR_DATA) | (STR_CLK | STR_WREN);
  285. outw(odir & ~STR_WREN, io + IO_DIR);
  286. dev->muted = inw(io) & STR_WREN ? 0 : 1;
  287. outw(odir, io + IO_DIR);
  288. outw(~(STR_WREN | STR_CLK), io + IO_MASK);
  289. outw(dev->muted ? 0 : STR_WREN, io);
  290. udelay(16);
  291. outw(omask, io + IO_MASK);
  292. ofreq = radio_bits_get(dev);
  293. if ((ofreq < FREQ2BITS(FREQ_LO)) || (ofreq > FREQ2BITS(FREQ_HI)))
  294. ofreq = FREQ2BITS(FREQ_LO);
  295. radio_bits_set(dev, ofreq);
  296. return (ofreq == radio_bits_get(dev));
  297. }
  298. static int __devinit maestro_probe(struct pci_dev *pdev,
  299. const struct pci_device_id *ent)
  300. {
  301. struct maestro *dev;
  302. struct v4l2_device *v4l2_dev;
  303. int retval;
  304. retval = pci_enable_device(pdev);
  305. if (retval) {
  306. dev_err(&pdev->dev, "enabling pci device failed!\n");
  307. goto err;
  308. }
  309. retval = -ENOMEM;
  310. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  311. if (dev == NULL) {
  312. dev_err(&pdev->dev, "not enough memory\n");
  313. goto err;
  314. }
  315. v4l2_dev = &dev->v4l2_dev;
  316. mutex_init(&dev->lock);
  317. dev->pdev = pdev;
  318. strlcpy(v4l2_dev->name, "maestro", sizeof(v4l2_dev->name));
  319. retval = v4l2_device_register(&pdev->dev, v4l2_dev);
  320. if (retval < 0) {
  321. v4l2_err(v4l2_dev, "Could not register v4l2_device\n");
  322. goto errfr;
  323. }
  324. dev->io = pci_resource_start(pdev, 0) + GPIO_DATA;
  325. strlcpy(dev->vdev.name, v4l2_dev->name, sizeof(dev->vdev.name));
  326. dev->vdev.v4l2_dev = v4l2_dev;
  327. dev->vdev.fops = &maestro_fops;
  328. dev->vdev.ioctl_ops = &maestro_ioctl_ops;
  329. dev->vdev.release = video_device_release_empty;
  330. video_set_drvdata(&dev->vdev, dev);
  331. retval = video_register_device(&dev->vdev, VFL_TYPE_RADIO, radio_nr);
  332. if (retval) {
  333. v4l2_err(v4l2_dev, "can't register video device!\n");
  334. goto errfr1;
  335. }
  336. if (!radio_power_on(dev)) {
  337. retval = -EIO;
  338. goto errunr;
  339. }
  340. v4l2_info(v4l2_dev, "version " DRIVER_VERSION "\n");
  341. return 0;
  342. errunr:
  343. video_unregister_device(&dev->vdev);
  344. errfr1:
  345. v4l2_device_unregister(v4l2_dev);
  346. errfr:
  347. kfree(dev);
  348. err:
  349. return retval;
  350. }
  351. static void __devexit maestro_remove(struct pci_dev *pdev)
  352. {
  353. struct v4l2_device *v4l2_dev = dev_get_drvdata(&pdev->dev);
  354. struct maestro *dev = to_maestro(v4l2_dev);
  355. video_unregister_device(&dev->vdev);
  356. v4l2_device_unregister(&dev->v4l2_dev);
  357. }
  358. static struct pci_device_id maestro_r_pci_tbl[] = {
  359. { PCI_DEVICE(PCI_VENDOR_ID_ESS, PCI_DEVICE_ID_ESS_ESS1968),
  360. .class = PCI_CLASS_MULTIMEDIA_AUDIO << 8,
  361. .class_mask = 0xffff00 },
  362. { PCI_DEVICE(PCI_VENDOR_ID_ESS, PCI_DEVICE_ID_ESS_ESS1978),
  363. .class = PCI_CLASS_MULTIMEDIA_AUDIO << 8,
  364. .class_mask = 0xffff00 },
  365. { 0 }
  366. };
  367. MODULE_DEVICE_TABLE(pci, maestro_r_pci_tbl);
  368. static struct pci_driver maestro_r_driver = {
  369. .name = "maestro_radio",
  370. .id_table = maestro_r_pci_tbl,
  371. .probe = maestro_probe,
  372. .remove = __devexit_p(maestro_remove),
  373. };
  374. static int __init maestro_radio_init(void)
  375. {
  376. int retval = pci_register_driver(&maestro_r_driver);
  377. if (retval)
  378. printk(KERN_ERR "error during registration pci driver\n");
  379. return retval;
  380. }
  381. static void __exit maestro_radio_exit(void)
  382. {
  383. pci_unregister_driver(&maestro_r_driver);
  384. }
  385. module_init(maestro_radio_init);
  386. module_exit(maestro_radio_exit);