radio-maestro.c 11 KB

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