radio-maestro.c 11 KB

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