radio-maestro.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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/mutex.h>
  27. #include <linux/pci.h>
  28. #include <linux/videodev2.h>
  29. #include <media/v4l2-common.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. .compat_ioctl = v4l_compat_ioctl32,
  89. .llseek = no_llseek,
  90. };
  91. struct radio_device {
  92. u16 io, /* base of Maestro card radio io (GPIO_DATA)*/
  93. muted, /* VIDEO_AUDIO_MUTE */
  94. stereo, /* VIDEO_TUNER_STEREO_ON */
  95. tuned; /* signal strength (0 or 0xffff) */
  96. struct mutex lock;
  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. mutex_init(&radio_unit->lock);
  340. maestro_radio_inst = video_device_alloc();
  341. if (maestro_radio_inst == NULL) {
  342. dev_err(&pdev->dev, "not enough memory\n");
  343. goto errfr;
  344. }
  345. memcpy(maestro_radio_inst, &maestro_radio, sizeof(maestro_radio));
  346. video_set_drvdata(maestro_radio_inst, radio_unit);
  347. pci_set_drvdata(pdev, maestro_radio_inst);
  348. retval = video_register_device(maestro_radio_inst, VFL_TYPE_RADIO,
  349. radio_nr);
  350. if (retval) {
  351. printk(KERN_ERR "can't register video device!\n");
  352. goto errfr1;
  353. }
  354. if (!radio_power_on(radio_unit)) {
  355. retval = -EIO;
  356. goto errunr;
  357. }
  358. dev_info(&pdev->dev, "version " DRIVER_VERSION " time " __TIME__ " "
  359. __DATE__ "\n");
  360. dev_info(&pdev->dev, "radio chip initialized\n");
  361. return 0;
  362. errunr:
  363. video_unregister_device(maestro_radio_inst);
  364. errfr1:
  365. kfree(maestro_radio_inst);
  366. errfr:
  367. kfree(radio_unit);
  368. err:
  369. return retval;
  370. }
  371. static void __devexit maestro_remove(struct pci_dev *pdev)
  372. {
  373. struct video_device *vdev = pci_get_drvdata(pdev);
  374. video_unregister_device(vdev);
  375. }
  376. static int __init maestro_radio_init(void)
  377. {
  378. int retval = pci_register_driver(&maestro_r_driver);
  379. if (retval)
  380. printk(KERN_ERR "error during registration pci driver\n");
  381. return retval;
  382. }
  383. static void __exit maestro_radio_exit(void)
  384. {
  385. pci_unregister_driver(&maestro_r_driver);
  386. }
  387. module_init(maestro_radio_init);
  388. module_exit(maestro_radio_exit);
  389. MODULE_AUTHOR("Adam Tlalka, atlka@pg.gda.pl");
  390. MODULE_DESCRIPTION("Radio driver for the Maestro PCI sound card radio.");
  391. MODULE_LICENSE("GPL");