radio-sf16fmr2.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. /* SF16FMR2 radio driver for Linux radio support
  2. * heavily based on fmi driver...
  3. * (c) 2000-2002 Ziglio Frediano, freddy77@angelfire.com
  4. *
  5. * Notes on the hardware
  6. *
  7. * Frequency control is done digitally -- ie out(port,encodefreq(95.8));
  8. * No volume control - only mute/unmute - you have to use line volume
  9. *
  10. * For read stereo/mono you must wait 0.1 sec after set frequency and
  11. * card unmuted so I set frequency on unmute
  12. * Signal handling seem to work only on autoscanning (not implemented)
  13. *
  14. * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
  15. */
  16. #include <linux/module.h> /* Modules */
  17. #include <linux/init.h> /* Initdata */
  18. #include <linux/ioport.h> /* request_region */
  19. #include <linux/delay.h> /* udelay */
  20. #include <linux/videodev2.h> /* kernel radio structs */
  21. #include <linux/mutex.h>
  22. #include <linux/version.h> /* for KERNEL_VERSION MACRO */
  23. #include <linux/io.h> /* outb, outb_p */
  24. #include <media/v4l2-device.h>
  25. #include <media/v4l2-ioctl.h>
  26. MODULE_AUTHOR("Ziglio Frediano, freddy77@angelfire.com");
  27. MODULE_DESCRIPTION("A driver for the SF16FMR2 radio.");
  28. MODULE_LICENSE("GPL");
  29. static int io = 0x384;
  30. static int radio_nr = -1;
  31. module_param(io, int, 0);
  32. MODULE_PARM_DESC(io, "I/O address of the SF16FMR2 card (should be 0x384, if do not work try 0x284)");
  33. module_param(radio_nr, int, 0);
  34. #define RADIO_VERSION KERNEL_VERSION(0,0,2)
  35. #define AUD_VOL_INDEX 1
  36. #undef DEBUG
  37. //#define DEBUG 1
  38. #ifdef DEBUG
  39. # define debug_print(s) printk s
  40. #else
  41. # define debug_print(s)
  42. #endif
  43. /* this should be static vars for module size */
  44. struct fmr2
  45. {
  46. struct v4l2_device v4l2_dev;
  47. struct video_device vdev;
  48. struct mutex lock;
  49. int io;
  50. int curvol; /* 0-15 */
  51. int mute;
  52. int stereo; /* card is producing stereo audio */
  53. unsigned long curfreq; /* freq in kHz */
  54. int card_type;
  55. u32 flags;
  56. };
  57. static struct fmr2 fmr2_card;
  58. /* hw precision is 12.5 kHz
  59. * It is only useful to give freq in intervall of 200 (=0.0125Mhz),
  60. * other bits will be truncated
  61. */
  62. #define RSF16_ENCODE(x) ((x) / 200 + 856)
  63. #define RSF16_MINFREQ (87 * 16000)
  64. #define RSF16_MAXFREQ (108 * 16000)
  65. static inline void wait(int n, int io)
  66. {
  67. for (; n; --n)
  68. inb(io);
  69. }
  70. static void outbits(int bits, unsigned int data, int nWait, int io)
  71. {
  72. int bit;
  73. for (; --bits >= 0;) {
  74. bit = (data >> bits) & 1;
  75. outb(bit, io);
  76. wait(nWait, io);
  77. outb(bit | 2, io);
  78. wait(nWait, io);
  79. outb(bit, io);
  80. wait(nWait, io);
  81. }
  82. }
  83. static inline void fmr2_mute(int io)
  84. {
  85. outb(0x00, io);
  86. wait(4, io);
  87. }
  88. static inline void fmr2_unmute(int io)
  89. {
  90. outb(0x04, io);
  91. wait(4, io);
  92. }
  93. static inline int fmr2_stereo_mode(int io)
  94. {
  95. int n = inb(io);
  96. outb(6, io);
  97. inb(io);
  98. n = ((n >> 3) & 1) ^ 1;
  99. debug_print((KERN_DEBUG "stereo: %d\n", n));
  100. return n;
  101. }
  102. static int fmr2_product_info(struct fmr2 *dev)
  103. {
  104. int n = inb(dev->io);
  105. n &= 0xC1;
  106. if (n == 0) {
  107. /* this should support volume set */
  108. dev->card_type = 12;
  109. return 0;
  110. }
  111. /* not volume (mine is 11) */
  112. dev->card_type = (n == 128) ? 11 : 0;
  113. return n;
  114. }
  115. static inline int fmr2_getsigstr(struct fmr2 *dev)
  116. {
  117. /* !!! works only if scanning freq */
  118. int res = 0xffff;
  119. outb(5, dev->io);
  120. wait(4, dev->io);
  121. if (!(inb(dev->io) & 1))
  122. res = 0;
  123. debug_print((KERN_DEBUG "signal: %d\n", res));
  124. return res;
  125. }
  126. /* set frequency and unmute card */
  127. static int fmr2_setfreq(struct fmr2 *dev)
  128. {
  129. unsigned long freq = dev->curfreq;
  130. fmr2_mute(dev->io);
  131. /* 0x42 for mono output
  132. * 0x102 forward scanning
  133. * 0x182 scansione avanti
  134. */
  135. outbits(9, 0x2, 3, dev->io);
  136. outbits(16, RSF16_ENCODE(freq), 2, dev->io);
  137. fmr2_unmute(dev->io);
  138. /* wait 0.11 sec */
  139. msleep(110);
  140. /* NOTE if mute this stop radio
  141. you must set freq on unmute */
  142. dev->stereo = fmr2_stereo_mode(dev->io);
  143. return 0;
  144. }
  145. /* !!! not tested, in my card this does't work !!! */
  146. static int fmr2_setvolume(struct fmr2 *dev)
  147. {
  148. int vol[16] = { 0x021, 0x084, 0x090, 0x104,
  149. 0x110, 0x204, 0x210, 0x402,
  150. 0x404, 0x408, 0x410, 0x801,
  151. 0x802, 0x804, 0x808, 0x810 };
  152. int i, a;
  153. int n = vol[dev->curvol & 0x0f];
  154. if (dev->card_type != 11)
  155. return 1;
  156. for (i = 12; --i >= 0; ) {
  157. a = ((n >> i) & 1) << 6; /* if (a==0) a = 0; else a = 0x40; */
  158. outb(a | 4, dev->io);
  159. wait(4, dev->io);
  160. outb(a | 0x24, dev->io);
  161. wait(4, dev->io);
  162. outb(a | 4, dev->io);
  163. wait(4, dev->io);
  164. }
  165. for (i = 6; --i >= 0; ) {
  166. a = ((0x18 >> i) & 1) << 6;
  167. outb(a | 4, dev->io);
  168. wait(4, dev->io);
  169. outb(a | 0x24, dev->io);
  170. wait(4, dev->io);
  171. outb(a | 4, dev->io);
  172. wait(4, dev->io);
  173. }
  174. wait(4, dev->io);
  175. outb(0x14, dev->io);
  176. return 0;
  177. }
  178. static int vidioc_querycap(struct file *file, void *priv,
  179. struct v4l2_capability *v)
  180. {
  181. strlcpy(v->driver, "radio-sf16fmr2", sizeof(v->driver));
  182. strlcpy(v->card, "SF16-FMR2 radio", sizeof(v->card));
  183. strlcpy(v->bus_info, "ISA", sizeof(v->bus_info));
  184. v->version = RADIO_VERSION;
  185. v->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
  186. return 0;
  187. }
  188. static int vidioc_g_tuner(struct file *file, void *priv,
  189. struct v4l2_tuner *v)
  190. {
  191. int mult;
  192. struct fmr2 *fmr2 = video_drvdata(file);
  193. if (v->index > 0)
  194. return -EINVAL;
  195. strlcpy(v->name, "FM", sizeof(v->name));
  196. v->type = V4L2_TUNER_RADIO;
  197. mult = (fmr2->flags & V4L2_TUNER_CAP_LOW) ? 1 : 1000;
  198. v->rangelow = RSF16_MINFREQ / mult;
  199. v->rangehigh = RSF16_MAXFREQ / mult;
  200. v->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_MODE_STEREO;
  201. v->capability = fmr2->flags&V4L2_TUNER_CAP_LOW;
  202. v->audmode = fmr2->stereo ? V4L2_TUNER_MODE_STEREO:
  203. V4L2_TUNER_MODE_MONO;
  204. mutex_lock(&fmr2->lock);
  205. v->signal = fmr2_getsigstr(fmr2);
  206. mutex_unlock(&fmr2->lock);
  207. return 0;
  208. }
  209. static int vidioc_s_tuner(struct file *file, void *priv,
  210. struct v4l2_tuner *v)
  211. {
  212. return v->index ? -EINVAL : 0;
  213. }
  214. static int vidioc_s_frequency(struct file *file, void *priv,
  215. struct v4l2_frequency *f)
  216. {
  217. struct fmr2 *fmr2 = video_drvdata(file);
  218. if (!(fmr2->flags & V4L2_TUNER_CAP_LOW))
  219. f->frequency *= 1000;
  220. if (f->frequency < RSF16_MINFREQ ||
  221. f->frequency > RSF16_MAXFREQ)
  222. return -EINVAL;
  223. /* rounding in steps of 200 to match the freq
  224. that will be used */
  225. fmr2->curfreq = (f->frequency / 200) * 200;
  226. /* set card freq (if not muted) */
  227. if (fmr2->curvol && !fmr2->mute) {
  228. mutex_lock(&fmr2->lock);
  229. fmr2_setfreq(fmr2);
  230. mutex_unlock(&fmr2->lock);
  231. }
  232. return 0;
  233. }
  234. static int vidioc_g_frequency(struct file *file, void *priv,
  235. struct v4l2_frequency *f)
  236. {
  237. struct fmr2 *fmr2 = video_drvdata(file);
  238. f->type = V4L2_TUNER_RADIO;
  239. f->frequency = fmr2->curfreq;
  240. if (!(fmr2->flags & V4L2_TUNER_CAP_LOW))
  241. f->frequency /= 1000;
  242. return 0;
  243. }
  244. static int vidioc_queryctrl(struct file *file, void *priv,
  245. struct v4l2_queryctrl *qc)
  246. {
  247. struct fmr2 *fmr2 = video_drvdata(file);
  248. switch (qc->id) {
  249. case V4L2_CID_AUDIO_MUTE:
  250. return v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
  251. case V4L2_CID_AUDIO_VOLUME:
  252. /* Only card_type == 11 implements volume */
  253. if (fmr2->card_type == 11)
  254. return v4l2_ctrl_query_fill(qc, 0, 15, 1, 0);
  255. return v4l2_ctrl_query_fill(qc, 0, 1, 1, 0);
  256. }
  257. return -EINVAL;
  258. }
  259. static int vidioc_g_ctrl(struct file *file, void *priv,
  260. struct v4l2_control *ctrl)
  261. {
  262. struct fmr2 *fmr2 = video_drvdata(file);
  263. switch (ctrl->id) {
  264. case V4L2_CID_AUDIO_MUTE:
  265. ctrl->value = fmr2->mute;
  266. return 0;
  267. case V4L2_CID_AUDIO_VOLUME:
  268. ctrl->value = fmr2->curvol;
  269. return 0;
  270. }
  271. return -EINVAL;
  272. }
  273. static int vidioc_s_ctrl(struct file *file, void *priv,
  274. struct v4l2_control *ctrl)
  275. {
  276. struct fmr2 *fmr2 = video_drvdata(file);
  277. switch (ctrl->id) {
  278. case V4L2_CID_AUDIO_MUTE:
  279. fmr2->mute = ctrl->value;
  280. break;
  281. case V4L2_CID_AUDIO_VOLUME:
  282. fmr2->curvol = ctrl->value;
  283. break;
  284. default:
  285. return -EINVAL;
  286. }
  287. #ifdef DEBUG
  288. if (fmr2->curvol && !fmr2->mute)
  289. printk(KERN_DEBUG "unmute\n");
  290. else
  291. printk(KERN_DEBUG "mute\n");
  292. #endif
  293. mutex_lock(&fmr2->lock);
  294. if (fmr2->curvol && !fmr2->mute) {
  295. fmr2_setvolume(fmr2);
  296. /* Set frequency and unmute card */
  297. fmr2_setfreq(fmr2);
  298. } else
  299. fmr2_mute(fmr2->io);
  300. mutex_unlock(&fmr2->lock);
  301. return 0;
  302. }
  303. static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
  304. {
  305. *i = 0;
  306. return 0;
  307. }
  308. static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
  309. {
  310. return i ? -EINVAL : 0;
  311. }
  312. static int vidioc_g_audio(struct file *file, void *priv,
  313. struct v4l2_audio *a)
  314. {
  315. a->index = 0;
  316. strlcpy(a->name, "Radio", sizeof(a->name));
  317. a->capability = V4L2_AUDCAP_STEREO;
  318. return 0;
  319. }
  320. static int vidioc_s_audio(struct file *file, void *priv,
  321. struct v4l2_audio *a)
  322. {
  323. return a->index ? -EINVAL : 0;
  324. }
  325. static const struct v4l2_file_operations fmr2_fops = {
  326. .owner = THIS_MODULE,
  327. .ioctl = video_ioctl2,
  328. };
  329. static const struct v4l2_ioctl_ops fmr2_ioctl_ops = {
  330. .vidioc_querycap = vidioc_querycap,
  331. .vidioc_g_tuner = vidioc_g_tuner,
  332. .vidioc_s_tuner = vidioc_s_tuner,
  333. .vidioc_g_audio = vidioc_g_audio,
  334. .vidioc_s_audio = vidioc_s_audio,
  335. .vidioc_g_input = vidioc_g_input,
  336. .vidioc_s_input = vidioc_s_input,
  337. .vidioc_g_frequency = vidioc_g_frequency,
  338. .vidioc_s_frequency = vidioc_s_frequency,
  339. .vidioc_queryctrl = vidioc_queryctrl,
  340. .vidioc_g_ctrl = vidioc_g_ctrl,
  341. .vidioc_s_ctrl = vidioc_s_ctrl,
  342. };
  343. static int __init fmr2_init(void)
  344. {
  345. struct fmr2 *fmr2 = &fmr2_card;
  346. struct v4l2_device *v4l2_dev = &fmr2->v4l2_dev;
  347. int res;
  348. strlcpy(v4l2_dev->name, "sf16fmr2", sizeof(v4l2_dev->name));
  349. fmr2->io = io;
  350. fmr2->stereo = 1;
  351. fmr2->flags = V4L2_TUNER_CAP_LOW;
  352. mutex_init(&fmr2->lock);
  353. if (!request_region(fmr2->io, 2, "sf16fmr2")) {
  354. v4l2_err(v4l2_dev, "request_region failed!\n");
  355. return -EBUSY;
  356. }
  357. res = v4l2_device_register(NULL, v4l2_dev);
  358. if (res < 0) {
  359. release_region(fmr2->io, 2);
  360. v4l2_err(v4l2_dev, "Could not register v4l2_device\n");
  361. return res;
  362. }
  363. strlcpy(fmr2->vdev.name, v4l2_dev->name, sizeof(fmr2->vdev.name));
  364. fmr2->vdev.v4l2_dev = v4l2_dev;
  365. fmr2->vdev.fops = &fmr2_fops;
  366. fmr2->vdev.ioctl_ops = &fmr2_ioctl_ops;
  367. fmr2->vdev.release = video_device_release_empty;
  368. video_set_drvdata(&fmr2->vdev, fmr2);
  369. if (video_register_device(&fmr2->vdev, VFL_TYPE_RADIO, radio_nr) < 0) {
  370. v4l2_device_unregister(v4l2_dev);
  371. release_region(fmr2->io, 2);
  372. return -EINVAL;
  373. }
  374. v4l2_info(v4l2_dev, "SF16FMR2 radio card driver at 0x%x.\n", fmr2->io);
  375. /* mute card - prevents noisy bootups */
  376. mutex_lock(&fmr2->lock);
  377. fmr2_mute(fmr2->io);
  378. fmr2_product_info(fmr2);
  379. mutex_unlock(&fmr2->lock);
  380. debug_print((KERN_DEBUG "card_type %d\n", fmr2->card_type));
  381. return 0;
  382. }
  383. static void __exit fmr2_exit(void)
  384. {
  385. struct fmr2 *fmr2 = &fmr2_card;
  386. video_unregister_device(&fmr2->vdev);
  387. v4l2_device_unregister(&fmr2->v4l2_dev);
  388. release_region(fmr2->io, 2);
  389. }
  390. module_init(fmr2_init);
  391. module_exit(fmr2_exit);
  392. #ifndef MODULE
  393. static int __init fmr2_setup_io(char *str)
  394. {
  395. get_option(&str, &io);
  396. return 1;
  397. }
  398. __setup("sf16fmr2=", fmr2_setup_io);
  399. #endif