radio-maestro.c 11 KB

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