radio-maestro.c 11 KB

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