radio-sf16fmr2.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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->frequency < RSF16_MINFREQ ||
  216. f->frequency > RSF16_MAXFREQ)
  217. return -EINVAL;
  218. /* rounding in steps of 200 to match the freq
  219. that will be used */
  220. fmr2->curfreq = (f->frequency / 200) * 200;
  221. /* set card freq (if not muted) */
  222. if (fmr2->curvol && !fmr2->mute) {
  223. mutex_lock(&fmr2->lock);
  224. fmr2_setfreq(fmr2);
  225. mutex_unlock(&fmr2->lock);
  226. }
  227. return 0;
  228. }
  229. static int vidioc_g_frequency(struct file *file, void *priv,
  230. struct v4l2_frequency *f)
  231. {
  232. struct fmr2 *fmr2 = video_drvdata(file);
  233. f->type = V4L2_TUNER_RADIO;
  234. f->frequency = fmr2->curfreq;
  235. return 0;
  236. }
  237. static int vidioc_queryctrl(struct file *file, void *priv,
  238. struct v4l2_queryctrl *qc)
  239. {
  240. struct fmr2 *fmr2 = video_drvdata(file);
  241. switch (qc->id) {
  242. case V4L2_CID_AUDIO_MUTE:
  243. return v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
  244. case V4L2_CID_AUDIO_VOLUME:
  245. /* Only card_type == 11 implements volume */
  246. if (fmr2->card_type == 11)
  247. return v4l2_ctrl_query_fill(qc, 0, 15, 1, 0);
  248. return v4l2_ctrl_query_fill(qc, 0, 1, 1, 0);
  249. }
  250. return -EINVAL;
  251. }
  252. static int vidioc_g_ctrl(struct file *file, void *priv,
  253. struct v4l2_control *ctrl)
  254. {
  255. struct fmr2 *fmr2 = video_drvdata(file);
  256. switch (ctrl->id) {
  257. case V4L2_CID_AUDIO_MUTE:
  258. ctrl->value = fmr2->mute;
  259. return 0;
  260. case V4L2_CID_AUDIO_VOLUME:
  261. ctrl->value = fmr2->curvol;
  262. return 0;
  263. }
  264. return -EINVAL;
  265. }
  266. static int vidioc_s_ctrl(struct file *file, void *priv,
  267. struct v4l2_control *ctrl)
  268. {
  269. struct fmr2 *fmr2 = video_drvdata(file);
  270. switch (ctrl->id) {
  271. case V4L2_CID_AUDIO_MUTE:
  272. fmr2->mute = ctrl->value;
  273. break;
  274. case V4L2_CID_AUDIO_VOLUME:
  275. fmr2->curvol = ctrl->value;
  276. break;
  277. default:
  278. return -EINVAL;
  279. }
  280. #ifdef DEBUG
  281. if (fmr2->curvol && !fmr2->mute)
  282. printk(KERN_DEBUG "unmute\n");
  283. else
  284. printk(KERN_DEBUG "mute\n");
  285. #endif
  286. mutex_lock(&fmr2->lock);
  287. if (fmr2->curvol && !fmr2->mute) {
  288. fmr2_setvolume(fmr2);
  289. /* Set frequency and unmute card */
  290. fmr2_setfreq(fmr2);
  291. } else
  292. fmr2_mute(fmr2->io);
  293. mutex_unlock(&fmr2->lock);
  294. return 0;
  295. }
  296. static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
  297. {
  298. *i = 0;
  299. return 0;
  300. }
  301. static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
  302. {
  303. return i ? -EINVAL : 0;
  304. }
  305. static int vidioc_g_audio(struct file *file, void *priv,
  306. struct v4l2_audio *a)
  307. {
  308. a->index = 0;
  309. strlcpy(a->name, "Radio", sizeof(a->name));
  310. a->capability = V4L2_AUDCAP_STEREO;
  311. return 0;
  312. }
  313. static int vidioc_s_audio(struct file *file, void *priv,
  314. struct v4l2_audio *a)
  315. {
  316. return a->index ? -EINVAL : 0;
  317. }
  318. static const struct v4l2_file_operations fmr2_fops = {
  319. .owner = THIS_MODULE,
  320. .ioctl = video_ioctl2,
  321. };
  322. static const struct v4l2_ioctl_ops fmr2_ioctl_ops = {
  323. .vidioc_querycap = vidioc_querycap,
  324. .vidioc_g_tuner = vidioc_g_tuner,
  325. .vidioc_s_tuner = vidioc_s_tuner,
  326. .vidioc_g_audio = vidioc_g_audio,
  327. .vidioc_s_audio = vidioc_s_audio,
  328. .vidioc_g_input = vidioc_g_input,
  329. .vidioc_s_input = vidioc_s_input,
  330. .vidioc_g_frequency = vidioc_g_frequency,
  331. .vidioc_s_frequency = vidioc_s_frequency,
  332. .vidioc_queryctrl = vidioc_queryctrl,
  333. .vidioc_g_ctrl = vidioc_g_ctrl,
  334. .vidioc_s_ctrl = vidioc_s_ctrl,
  335. };
  336. static int __init fmr2_init(void)
  337. {
  338. struct fmr2 *fmr2 = &fmr2_card;
  339. struct v4l2_device *v4l2_dev = &fmr2->v4l2_dev;
  340. int res;
  341. strlcpy(v4l2_dev->name, "sf16fmr2", sizeof(v4l2_dev->name));
  342. fmr2->io = io;
  343. fmr2->stereo = 1;
  344. mutex_init(&fmr2->lock);
  345. if (!request_region(fmr2->io, 2, "sf16fmr2")) {
  346. v4l2_err(v4l2_dev, "request_region failed!\n");
  347. return -EBUSY;
  348. }
  349. res = v4l2_device_register(NULL, v4l2_dev);
  350. if (res < 0) {
  351. release_region(fmr2->io, 2);
  352. v4l2_err(v4l2_dev, "Could not register v4l2_device\n");
  353. return res;
  354. }
  355. strlcpy(fmr2->vdev.name, v4l2_dev->name, sizeof(fmr2->vdev.name));
  356. fmr2->vdev.v4l2_dev = v4l2_dev;
  357. fmr2->vdev.fops = &fmr2_fops;
  358. fmr2->vdev.ioctl_ops = &fmr2_ioctl_ops;
  359. fmr2->vdev.release = video_device_release_empty;
  360. video_set_drvdata(&fmr2->vdev, fmr2);
  361. if (video_register_device(&fmr2->vdev, VFL_TYPE_RADIO, radio_nr) < 0) {
  362. v4l2_device_unregister(v4l2_dev);
  363. release_region(fmr2->io, 2);
  364. return -EINVAL;
  365. }
  366. v4l2_info(v4l2_dev, "SF16FMR2 radio card driver at 0x%x.\n", fmr2->io);
  367. /* mute card - prevents noisy bootups */
  368. mutex_lock(&fmr2->lock);
  369. fmr2_mute(fmr2->io);
  370. fmr2_product_info(fmr2);
  371. mutex_unlock(&fmr2->lock);
  372. debug_print((KERN_DEBUG "card_type %d\n", fmr2->card_type));
  373. return 0;
  374. }
  375. static void __exit fmr2_exit(void)
  376. {
  377. struct fmr2 *fmr2 = &fmr2_card;
  378. video_unregister_device(&fmr2->vdev);
  379. v4l2_device_unregister(&fmr2->v4l2_dev);
  380. release_region(fmr2->io, 2);
  381. }
  382. module_init(fmr2_init);
  383. module_exit(fmr2_exit);
  384. #ifndef MODULE
  385. static int __init fmr2_setup_io(char *str)
  386. {
  387. get_option(&str, &io);
  388. return 1;
  389. }
  390. __setup("sf16fmr2=", fmr2_setup_io);
  391. #endif