radio-maestro.c 11 KB

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