PageRenderTime 182ms CodeModel.GetById 22ms RepoModel.GetById 2ms app.codeStats 0ms

/drivers/media/radio/radio-zoltrix.c

https://bitbucket.org/slukk/jb-tsm-kernel-4.2
C | 469 lines | 358 code | 74 blank | 37 comment | 49 complexity | 23c8519506966b80d4ec18cb8760898f MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
  1. /* zoltrix radio plus driver for Linux radio support
  2. * (c) 1998 C. van Schaik <carl@leg.uct.ac.za>
  3. *
  4. * BUGS
  5. * Due to the inconsistency in reading from the signal flags
  6. * it is difficult to get an accurate tuned signal.
  7. *
  8. * It seems that the card is not linear to 0 volume. It cuts off
  9. * at a low volume, and it is not possible (at least I have not found)
  10. * to get fine volume control over the low volume range.
  11. *
  12. * Some code derived from code by Romolo Manfredini
  13. * romolo@bicnet.it
  14. *
  15. * 1999-05-06 - (C. van Schaik)
  16. * - Make signal strength and stereo scans
  17. * kinder to cpu while in delay
  18. * 1999-01-05 - (C. van Schaik)
  19. * - Changed tuning to 1/160Mhz accuracy
  20. * - Added stereo support
  21. * (card defaults to stereo)
  22. * (can explicitly force mono on the card)
  23. * (can detect if station is in stereo)
  24. * - Added unmute function
  25. * - Reworked ioctl functions
  26. * 2002-07-15 - Fix Stereo typo
  27. *
  28. * 2006-07-24 - Converted to V4L2 API
  29. * by Mauro Carvalho Chehab <mchehab@infradead.org>
  30. */
  31. #include <linux/module.h> /* Modules */
  32. #include <linux/init.h> /* Initdata */
  33. #include <linux/ioport.h> /* request_region */
  34. #include <linux/delay.h> /* udelay, msleep */
  35. #include <linux/videodev2.h> /* kernel radio structs */
  36. #include <linux/mutex.h>
  37. #include <linux/version.h> /* for KERNEL_VERSION MACRO */
  38. #include <linux/io.h> /* outb, outb_p */
  39. #include <media/v4l2-device.h>
  40. #include <media/v4l2-ioctl.h>
  41. MODULE_AUTHOR("C.van Schaik");
  42. MODULE_DESCRIPTION("A driver for the Zoltrix Radio Plus.");
  43. MODULE_LICENSE("GPL");
  44. #ifndef CONFIG_RADIO_ZOLTRIX_PORT
  45. #define CONFIG_RADIO_ZOLTRIX_PORT -1
  46. #endif
  47. static int io = CONFIG_RADIO_ZOLTRIX_PORT;
  48. static int radio_nr = -1;
  49. module_param(io, int, 0);
  50. MODULE_PARM_DESC(io, "I/O address of the Zoltrix Radio Plus (0x20c or 0x30c)");
  51. module_param(radio_nr, int, 0);
  52. #define RADIO_VERSION KERNEL_VERSION(0, 0, 2)
  53. struct zoltrix {
  54. struct v4l2_device v4l2_dev;
  55. struct video_device vdev;
  56. int io;
  57. int curvol;
  58. unsigned long curfreq;
  59. int muted;
  60. unsigned int stereo;
  61. struct mutex lock;
  62. };
  63. static struct zoltrix zoltrix_card;
  64. static int zol_setvol(struct zoltrix *zol, int vol)
  65. {
  66. zol->curvol = vol;
  67. if (zol->muted)
  68. return 0;
  69. mutex_lock(&zol->lock);
  70. if (vol == 0) {
  71. outb(0, zol->io);
  72. outb(0, zol->io);
  73. inb(zol->io + 3); /* Zoltrix needs to be read to confirm */
  74. mutex_unlock(&zol->lock);
  75. return 0;
  76. }
  77. outb(zol->curvol-1, zol->io);
  78. msleep(10);
  79. inb(zol->io + 2);
  80. mutex_unlock(&zol->lock);
  81. return 0;
  82. }
  83. static void zol_mute(struct zoltrix *zol)
  84. {
  85. zol->muted = 1;
  86. mutex_lock(&zol->lock);
  87. outb(0, zol->io);
  88. outb(0, zol->io);
  89. inb(zol->io + 3); /* Zoltrix needs to be read to confirm */
  90. mutex_unlock(&zol->lock);
  91. }
  92. static void zol_unmute(struct zoltrix *zol)
  93. {
  94. zol->muted = 0;
  95. zol_setvol(zol, zol->curvol);
  96. }
  97. static int zol_setfreq(struct zoltrix *zol, unsigned long freq)
  98. {
  99. /* tunes the radio to the desired frequency */
  100. struct v4l2_device *v4l2_dev = &zol->v4l2_dev;
  101. unsigned long long bitmask, f, m;
  102. unsigned int stereo = zol->stereo;
  103. int i;
  104. if (freq == 0) {
  105. v4l2_warn(v4l2_dev, "cannot set a frequency of 0.\n");
  106. return -EINVAL;
  107. }
  108. m = (freq / 160 - 8800) * 2;
  109. f = (unsigned long long)m + 0x4d1c;
  110. bitmask = 0xc480402c10080000ull;
  111. i = 45;
  112. mutex_lock(&zol->lock);
  113. zol->curfreq = freq;
  114. outb(0, zol->io);
  115. outb(0, zol->io);
  116. inb(zol->io + 3); /* Zoltrix needs to be read to confirm */
  117. outb(0x40, zol->io);
  118. outb(0xc0, zol->io);
  119. bitmask = (bitmask ^ ((f & 0xff) << 47) ^ ((f & 0xff00) << 30) ^ (stereo << 31));
  120. while (i--) {
  121. if ((bitmask & 0x8000000000000000ull) != 0) {
  122. outb(0x80, zol->io);
  123. udelay(50);
  124. outb(0x00, zol->io);
  125. udelay(50);
  126. outb(0x80, zol->io);
  127. udelay(50);
  128. } else {
  129. outb(0xc0, zol->io);
  130. udelay(50);
  131. outb(0x40, zol->io);
  132. udelay(50);
  133. outb(0xc0, zol->io);
  134. udelay(50);
  135. }
  136. bitmask *= 2;
  137. }
  138. /* termination sequence */
  139. outb(0x80, zol->io);
  140. outb(0xc0, zol->io);
  141. outb(0x40, zol->io);
  142. udelay(1000);
  143. inb(zol->io + 2);
  144. udelay(1000);
  145. if (zol->muted) {
  146. outb(0, zol->io);
  147. outb(0, zol->io);
  148. inb(zol->io + 3);
  149. udelay(1000);
  150. }
  151. mutex_unlock(&zol->lock);
  152. if (!zol->muted)
  153. zol_setvol(zol, zol->curvol);
  154. return 0;
  155. }
  156. /* Get signal strength */
  157. static int zol_getsigstr(struct zoltrix *zol)
  158. {
  159. int a, b;
  160. mutex_lock(&zol->lock);
  161. outb(0x00, zol->io); /* This stuff I found to do nothing */
  162. outb(zol->curvol, zol->io);
  163. msleep(20);
  164. a = inb(zol->io);
  165. msleep(10);
  166. b = inb(zol->io);
  167. mutex_unlock(&zol->lock);
  168. if (a != b)
  169. return 0;
  170. /* I found this out by playing with a binary scanner on the card io */
  171. return a == 0xcf || a == 0xdf || a == 0xef;
  172. }
  173. static int zol_is_stereo(struct zoltrix *zol)
  174. {
  175. int x1, x2;
  176. mutex_lock(&zol->lock);
  177. outb(0x00, zol->io);
  178. outb(zol->curvol, zol->io);
  179. msleep(20);
  180. x1 = inb(zol->io);
  181. msleep(10);
  182. x2 = inb(zol->io);
  183. mutex_unlock(&zol->lock);
  184. return x1 == x2 && x1 == 0xcf;
  185. }
  186. static int vidioc_querycap(struct file *file, void *priv,
  187. struct v4l2_capability *v)
  188. {
  189. strlcpy(v->driver, "radio-zoltrix", sizeof(v->driver));
  190. strlcpy(v->card, "Zoltrix Radio", sizeof(v->card));
  191. strlcpy(v->bus_info, "ISA", sizeof(v->bus_info));
  192. v->version = RADIO_VERSION;
  193. v->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
  194. return 0;
  195. }
  196. static int vidioc_g_tuner(struct file *file, void *priv,
  197. struct v4l2_tuner *v)
  198. {
  199. struct zoltrix *zol = video_drvdata(file);
  200. if (v->index > 0)
  201. return -EINVAL;
  202. strlcpy(v->name, "FM", sizeof(v->name));
  203. v->type = V4L2_TUNER_RADIO;
  204. v->rangelow = 88 * 16000;
  205. v->rangehigh = 108 * 16000;
  206. v->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
  207. v->capability = V4L2_TUNER_CAP_LOW;
  208. if (zol_is_stereo(zol))
  209. v->audmode = V4L2_TUNER_MODE_STEREO;
  210. else
  211. v->audmode = V4L2_TUNER_MODE_MONO;
  212. v->signal = 0xFFFF * zol_getsigstr(zol);
  213. return 0;
  214. }
  215. static int vidioc_s_tuner(struct file *file, void *priv,
  216. struct v4l2_tuner *v)
  217. {
  218. return v->index ? -EINVAL : 0;
  219. }
  220. static int vidioc_s_frequency(struct file *file, void *priv,
  221. struct v4l2_frequency *f)
  222. {
  223. struct zoltrix *zol = video_drvdata(file);
  224. if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
  225. return -EINVAL;
  226. if (zol_setfreq(zol, f->frequency) != 0)
  227. return -EINVAL;
  228. return 0;
  229. }
  230. static int vidioc_g_frequency(struct file *file, void *priv,
  231. struct v4l2_frequency *f)
  232. {
  233. struct zoltrix *zol = video_drvdata(file);
  234. if (f->tuner != 0)
  235. return -EINVAL;
  236. f->type = V4L2_TUNER_RADIO;
  237. f->frequency = zol->curfreq;
  238. return 0;
  239. }
  240. static int vidioc_queryctrl(struct file *file, void *priv,
  241. struct v4l2_queryctrl *qc)
  242. {
  243. switch (qc->id) {
  244. case V4L2_CID_AUDIO_MUTE:
  245. return v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
  246. case V4L2_CID_AUDIO_VOLUME:
  247. return v4l2_ctrl_query_fill(qc, 0, 65535, 4096, 65535);
  248. }
  249. return -EINVAL;
  250. }
  251. static int vidioc_g_ctrl(struct file *file, void *priv,
  252. struct v4l2_control *ctrl)
  253. {
  254. struct zoltrix *zol = video_drvdata(file);
  255. switch (ctrl->id) {
  256. case V4L2_CID_AUDIO_MUTE:
  257. ctrl->value = zol->muted;
  258. return 0;
  259. case V4L2_CID_AUDIO_VOLUME:
  260. ctrl->value = zol->curvol * 4096;
  261. return 0;
  262. }
  263. return -EINVAL;
  264. }
  265. static int vidioc_s_ctrl(struct file *file, void *priv,
  266. struct v4l2_control *ctrl)
  267. {
  268. struct zoltrix *zol = video_drvdata(file);
  269. switch (ctrl->id) {
  270. case V4L2_CID_AUDIO_MUTE:
  271. if (ctrl->value)
  272. zol_mute(zol);
  273. else {
  274. zol_unmute(zol);
  275. zol_setvol(zol, zol->curvol);
  276. }
  277. return 0;
  278. case V4L2_CID_AUDIO_VOLUME:
  279. zol_setvol(zol, ctrl->value / 4096);
  280. return 0;
  281. }
  282. zol->stereo = 1;
  283. if (zol_setfreq(zol, zol->curfreq) != 0)
  284. return -EINVAL;
  285. #if 0
  286. /* FIXME: Implement stereo/mono switch on V4L2 */
  287. if (v->mode & VIDEO_SOUND_STEREO) {
  288. zol->stereo = 1;
  289. zol_setfreq(zol, zol->curfreq);
  290. }
  291. if (v->mode & VIDEO_SOUND_MONO) {
  292. zol->stereo = 0;
  293. zol_setfreq(zol, zol->curfreq);
  294. }
  295. #endif
  296. return -EINVAL;
  297. }
  298. static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
  299. {
  300. *i = 0;
  301. return 0;
  302. }
  303. static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
  304. {
  305. return i ? -EINVAL : 0;
  306. }
  307. static int vidioc_g_audio(struct file *file, void *priv,
  308. struct v4l2_audio *a)
  309. {
  310. a->index = 0;
  311. strlcpy(a->name, "Radio", sizeof(a->name));
  312. a->capability = V4L2_AUDCAP_STEREO;
  313. return 0;
  314. }
  315. static int vidioc_s_audio(struct file *file, void *priv,
  316. struct v4l2_audio *a)
  317. {
  318. return a->index ? -EINVAL : 0;
  319. }
  320. static const struct v4l2_file_operations zoltrix_fops =
  321. {
  322. .owner = THIS_MODULE,
  323. .unlocked_ioctl = video_ioctl2,
  324. };
  325. static const struct v4l2_ioctl_ops zoltrix_ioctl_ops = {
  326. .vidioc_querycap = vidioc_querycap,
  327. .vidioc_g_tuner = vidioc_g_tuner,
  328. .vidioc_s_tuner = vidioc_s_tuner,
  329. .vidioc_g_audio = vidioc_g_audio,
  330. .vidioc_s_audio = vidioc_s_audio,
  331. .vidioc_g_input = vidioc_g_input,
  332. .vidioc_s_input = vidioc_s_input,
  333. .vidioc_g_frequency = vidioc_g_frequency,
  334. .vidioc_s_frequency = vidioc_s_frequency,
  335. .vidioc_queryctrl = vidioc_queryctrl,
  336. .vidioc_g_ctrl = vidioc_g_ctrl,
  337. .vidioc_s_ctrl = vidioc_s_ctrl,
  338. };
  339. static int __init zoltrix_init(void)
  340. {
  341. struct zoltrix *zol = &zoltrix_card;
  342. struct v4l2_device *v4l2_dev = &zol->v4l2_dev;
  343. int res;
  344. strlcpy(v4l2_dev->name, "zoltrix", sizeof(v4l2_dev->name));
  345. zol->io = io;
  346. if (zol->io == -1) {
  347. v4l2_err(v4l2_dev, "You must set an I/O address with io=0x20c or 0x30c\n");
  348. return -EINVAL;
  349. }
  350. if (zol->io != 0x20c && zol->io != 0x30c) {
  351. v4l2_err(v4l2_dev, "invalid port, try 0x20c or 0x30c\n");
  352. return -ENXIO;
  353. }
  354. if (!request_region(zol->io, 2, "zoltrix")) {
  355. v4l2_err(v4l2_dev, "port 0x%x already in use\n", zol->io);
  356. return -EBUSY;
  357. }
  358. res = v4l2_device_register(NULL, v4l2_dev);
  359. if (res < 0) {
  360. release_region(zol->io, 2);
  361. v4l2_err(v4l2_dev, "Could not register v4l2_device\n");
  362. return res;
  363. }
  364. mutex_init(&zol->lock);
  365. /* mute card - prevents noisy bootups */
  366. /* this ensures that the volume is all the way down */
  367. outb(0, zol->io);
  368. outb(0, zol->io);
  369. msleep(20);
  370. inb(zol->io + 3);
  371. zol->curvol = 0;
  372. zol->stereo = 1;
  373. strlcpy(zol->vdev.name, v4l2_dev->name, sizeof(zol->vdev.name));
  374. zol->vdev.v4l2_dev = v4l2_dev;
  375. zol->vdev.fops = &zoltrix_fops;
  376. zol->vdev.ioctl_ops = &zoltrix_ioctl_ops;
  377. zol->vdev.release = video_device_release_empty;
  378. video_set_drvdata(&zol->vdev, zol);
  379. if (video_register_device(&zol->vdev, VFL_TYPE_RADIO, radio_nr) < 0) {
  380. v4l2_device_unregister(v4l2_dev);
  381. release_region(zol->io, 2);
  382. return -EINVAL;
  383. }
  384. v4l2_info(v4l2_dev, "Zoltrix Radio Plus card driver.\n");
  385. return 0;
  386. }
  387. static void __exit zoltrix_exit(void)
  388. {
  389. struct zoltrix *zol = &zoltrix_card;
  390. video_unregister_device(&zol->vdev);
  391. v4l2_device_unregister(&zol->v4l2_dev);
  392. release_region(zol->io, 2);
  393. }
  394. module_init(zoltrix_init);
  395. module_exit(zoltrix_exit);