radio-sf16fmr2.c 10 KB

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