radio-terratec.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /* Terratec ActiveRadio ISA Standalone card driver for Linux radio support
  2. * (c) 1999 R. Offermanns (rolf@offermanns.de)
  3. * based on the aimslab radio driver from M. Kirkwood
  4. * many thanks to Michael Becker and Friedhelm Birth (from TerraTec)
  5. *
  6. *
  7. * History:
  8. * 1999-05-21 First preview release
  9. *
  10. * Notes on the hardware:
  11. * There are two "main" chips on the card:
  12. * - Philips OM5610 (http://www-us.semiconductors.philips.com/acrobat/datasheets/OM5610_2.pdf)
  13. * - Philips SAA6588 (http://www-us.semiconductors.philips.com/acrobat/datasheets/SAA6588_1.pdf)
  14. * (you can get the datasheet at the above links)
  15. *
  16. * Frequency control is done digitally -- ie out(port,encodefreq(95.8));
  17. * Volume Control is done digitally
  18. *
  19. * there is a I2C controlled RDS decoder (SAA6588) onboard, which i would like to support someday
  20. * (as soon i have understand how to get started :)
  21. * If you can help me out with that, please contact me!!
  22. *
  23. *
  24. * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
  25. */
  26. #include <linux/module.h> /* Modules */
  27. #include <linux/init.h> /* Initdata */
  28. #include <linux/ioport.h> /* request_region */
  29. #include <linux/delay.h> /* udelay */
  30. #include <linux/videodev2.h> /* kernel radio structs */
  31. #include <linux/mutex.h>
  32. #include <linux/version.h> /* for KERNEL_VERSION MACRO */
  33. #include <linux/io.h> /* outb, outb_p */
  34. #include <linux/uaccess.h> /* copy to/from user */
  35. #include <media/v4l2-device.h>
  36. #include <media/v4l2-ioctl.h>
  37. MODULE_AUTHOR("R.OFFERMANNS & others");
  38. MODULE_DESCRIPTION("A driver for the TerraTec ActiveRadio Standalone radio card.");
  39. MODULE_LICENSE("GPL");
  40. #ifndef CONFIG_RADIO_TERRATEC_PORT
  41. #define CONFIG_RADIO_TERRATEC_PORT 0x590
  42. #endif
  43. static int io = CONFIG_RADIO_TERRATEC_PORT;
  44. static int radio_nr = -1;
  45. module_param(io, int, 0);
  46. MODULE_PARM_DESC(io, "I/O address of the TerraTec ActiveRadio card (0x590 or 0x591)");
  47. module_param(radio_nr, int, 0);
  48. #define RADIO_VERSION KERNEL_VERSION(0, 0, 2)
  49. static struct v4l2_queryctrl radio_qctrl[] = {
  50. {
  51. .id = V4L2_CID_AUDIO_MUTE,
  52. .name = "Mute",
  53. .minimum = 0,
  54. .maximum = 1,
  55. .default_value = 1,
  56. .type = V4L2_CTRL_TYPE_BOOLEAN,
  57. },{
  58. .id = V4L2_CID_AUDIO_VOLUME,
  59. .name = "Volume",
  60. .minimum = 0,
  61. .maximum = 0xff,
  62. .step = 1,
  63. .default_value = 0xff,
  64. .type = V4L2_CTRL_TYPE_INTEGER,
  65. }
  66. };
  67. #define WRT_DIS 0x00
  68. #define CLK_OFF 0x00
  69. #define IIC_DATA 0x01
  70. #define IIC_CLK 0x02
  71. #define DATA 0x04
  72. #define CLK_ON 0x08
  73. #define WRT_EN 0x10
  74. struct terratec
  75. {
  76. struct v4l2_device v4l2_dev;
  77. struct video_device vdev;
  78. int io;
  79. int curvol;
  80. unsigned long curfreq;
  81. int muted;
  82. struct mutex lock;
  83. };
  84. static struct terratec terratec_card;
  85. /* local things */
  86. static void tt_write_vol(struct terratec *tt, int volume)
  87. {
  88. int i;
  89. volume = volume + (volume * 32); /* change both channels */
  90. mutex_lock(&tt->lock);
  91. for (i = 0; i < 8; i++) {
  92. if (volume & (0x80 >> i))
  93. outb(0x80, tt->io + 1);
  94. else
  95. outb(0x00, tt->io + 1);
  96. }
  97. mutex_unlock(&tt->lock);
  98. }
  99. static void tt_mute(struct terratec *tt)
  100. {
  101. tt->muted = 1;
  102. tt_write_vol(tt, 0);
  103. }
  104. static int tt_setvol(struct terratec *tt, int vol)
  105. {
  106. if (vol == tt->curvol) { /* requested volume = current */
  107. if (tt->muted) { /* user is unmuting the card */
  108. tt->muted = 0;
  109. tt_write_vol(tt, vol); /* enable card */
  110. }
  111. return 0;
  112. }
  113. if (vol == 0) { /* volume = 0 means mute the card */
  114. tt_write_vol(tt, 0); /* "turn off card" by setting vol to 0 */
  115. tt->curvol = vol; /* track the volume state! */
  116. return 0;
  117. }
  118. tt->muted = 0;
  119. tt_write_vol(tt, vol);
  120. tt->curvol = vol;
  121. return 0;
  122. }
  123. /* this is the worst part in this driver */
  124. /* many more or less strange things are going on here, but hey, it works :) */
  125. static int tt_setfreq(struct terratec *tt, unsigned long freq1)
  126. {
  127. int freq;
  128. int i;
  129. int p;
  130. int temp;
  131. long rest;
  132. unsigned char buffer[25]; /* we have to bit shift 25 registers */
  133. mutex_lock(&tt->lock);
  134. tt->curfreq = freq1;
  135. freq = freq1 / 160; /* convert the freq. to a nice to handle value */
  136. memset(buffer, 0, sizeof(buffer));
  137. rest = freq * 10 + 10700; /* I once had understood what is going on here */
  138. /* maybe some wise guy (friedhelm?) can comment this stuff */
  139. i = 13;
  140. p = 10;
  141. temp = 102400;
  142. while (rest != 0) {
  143. if (rest % temp == rest)
  144. buffer[i] = 0;
  145. else {
  146. buffer[i] = 1;
  147. rest = rest - temp;
  148. }
  149. i--;
  150. p--;
  151. temp = temp / 2;
  152. }
  153. for (i = 24; i > -1; i--) { /* bit shift the values to the radiocard */
  154. if (buffer[i] == 1) {
  155. outb(WRT_EN | DATA, tt->io);
  156. outb(WRT_EN | DATA | CLK_ON, tt->io);
  157. outb(WRT_EN | DATA, tt->io);
  158. } else {
  159. outb(WRT_EN | 0x00, tt->io);
  160. outb(WRT_EN | 0x00 | CLK_ON, tt->io);
  161. }
  162. }
  163. outb(0x00, tt->io);
  164. mutex_unlock(&tt->lock);
  165. return 0;
  166. }
  167. static int tt_getsigstr(struct terratec *tt)
  168. {
  169. if (inb(tt->io) & 2) /* bit set = no signal present */
  170. return 0;
  171. return 1; /* signal present */
  172. }
  173. static int vidioc_querycap(struct file *file, void *priv,
  174. struct v4l2_capability *v)
  175. {
  176. strlcpy(v->driver, "radio-terratec", sizeof(v->driver));
  177. strlcpy(v->card, "ActiveRadio", sizeof(v->card));
  178. strlcpy(v->bus_info, "ISA", sizeof(v->bus_info));
  179. v->version = RADIO_VERSION;
  180. v->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
  181. return 0;
  182. }
  183. static int vidioc_g_tuner(struct file *file, void *priv,
  184. struct v4l2_tuner *v)
  185. {
  186. struct terratec *tt = video_drvdata(file);
  187. if (v->index > 0)
  188. return -EINVAL;
  189. strlcpy(v->name, "FM", sizeof(v->name));
  190. v->type = V4L2_TUNER_RADIO;
  191. v->rangelow = 87 * 16000;
  192. v->rangehigh = 108 * 16000;
  193. v->rxsubchans = V4L2_TUNER_SUB_MONO;
  194. v->capability = V4L2_TUNER_CAP_LOW;
  195. v->audmode = V4L2_TUNER_MODE_MONO;
  196. v->signal = 0xFFFF * tt_getsigstr(tt);
  197. return 0;
  198. }
  199. static int vidioc_s_tuner(struct file *file, void *priv,
  200. struct v4l2_tuner *v)
  201. {
  202. return v->index ? -EINVAL : 0;
  203. }
  204. static int vidioc_s_frequency(struct file *file, void *priv,
  205. struct v4l2_frequency *f)
  206. {
  207. struct terratec *tt = video_drvdata(file);
  208. tt_setfreq(tt, f->frequency);
  209. return 0;
  210. }
  211. static int vidioc_g_frequency(struct file *file, void *priv,
  212. struct v4l2_frequency *f)
  213. {
  214. struct terratec *tt = video_drvdata(file);
  215. f->type = V4L2_TUNER_RADIO;
  216. f->frequency = tt->curfreq;
  217. return 0;
  218. }
  219. static int vidioc_queryctrl(struct file *file, void *priv,
  220. struct v4l2_queryctrl *qc)
  221. {
  222. int i;
  223. for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
  224. if (qc->id && qc->id == radio_qctrl[i].id) {
  225. memcpy(qc, &(radio_qctrl[i]), sizeof(*qc));
  226. return 0;
  227. }
  228. }
  229. return -EINVAL;
  230. }
  231. static int vidioc_g_ctrl(struct file *file, void *priv,
  232. struct v4l2_control *ctrl)
  233. {
  234. struct terratec *tt = video_drvdata(file);
  235. switch (ctrl->id) {
  236. case V4L2_CID_AUDIO_MUTE:
  237. if (tt->muted)
  238. ctrl->value = 1;
  239. else
  240. ctrl->value = 0;
  241. return 0;
  242. case V4L2_CID_AUDIO_VOLUME:
  243. ctrl->value = tt->curvol * 6554;
  244. return 0;
  245. }
  246. return -EINVAL;
  247. }
  248. static int vidioc_s_ctrl(struct file *file, void *priv,
  249. struct v4l2_control *ctrl)
  250. {
  251. struct terratec *tt = video_drvdata(file);
  252. switch (ctrl->id) {
  253. case V4L2_CID_AUDIO_MUTE:
  254. if (ctrl->value)
  255. tt_mute(tt);
  256. else
  257. tt_setvol(tt,tt->curvol);
  258. return 0;
  259. case V4L2_CID_AUDIO_VOLUME:
  260. tt_setvol(tt,ctrl->value);
  261. return 0;
  262. }
  263. return -EINVAL;
  264. }
  265. static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
  266. {
  267. *i = 0;
  268. return 0;
  269. }
  270. static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
  271. {
  272. return i ? -EINVAL : 0;
  273. }
  274. static int vidioc_g_audio(struct file *file, void *priv,
  275. struct v4l2_audio *a)
  276. {
  277. a->index = 0;
  278. strlcpy(a->name, "Radio", sizeof(a->name));
  279. a->capability = V4L2_AUDCAP_STEREO;
  280. return 0;
  281. }
  282. static int vidioc_s_audio(struct file *file, void *priv,
  283. struct v4l2_audio *a)
  284. {
  285. return a->index ? -EINVAL : 0;
  286. }
  287. static int terratec_open(struct file *file)
  288. {
  289. return 0;
  290. }
  291. static int terratec_release(struct file *file)
  292. {
  293. return 0;
  294. }
  295. static const struct v4l2_file_operations terratec_fops = {
  296. .owner = THIS_MODULE,
  297. .open = terratec_open,
  298. .release = terratec_release,
  299. .ioctl = video_ioctl2,
  300. };
  301. static const struct v4l2_ioctl_ops terratec_ioctl_ops = {
  302. .vidioc_querycap = vidioc_querycap,
  303. .vidioc_g_tuner = vidioc_g_tuner,
  304. .vidioc_s_tuner = vidioc_s_tuner,
  305. .vidioc_g_frequency = vidioc_g_frequency,
  306. .vidioc_s_frequency = vidioc_s_frequency,
  307. .vidioc_queryctrl = vidioc_queryctrl,
  308. .vidioc_g_ctrl = vidioc_g_ctrl,
  309. .vidioc_s_ctrl = vidioc_s_ctrl,
  310. .vidioc_g_audio = vidioc_g_audio,
  311. .vidioc_s_audio = vidioc_s_audio,
  312. .vidioc_g_input = vidioc_g_input,
  313. .vidioc_s_input = vidioc_s_input,
  314. };
  315. static int __init terratec_init(void)
  316. {
  317. struct terratec *tt = &terratec_card;
  318. struct v4l2_device *v4l2_dev = &tt->v4l2_dev;
  319. int res;
  320. strlcpy(v4l2_dev->name, "terratec", sizeof(v4l2_dev->name));
  321. tt->io = io;
  322. if (tt->io == -1) {
  323. v4l2_err(v4l2_dev, "you must set an I/O address with io=0x590 or 0x591\n");
  324. return -EINVAL;
  325. }
  326. if (!request_region(tt->io, 2, "terratec")) {
  327. v4l2_err(v4l2_dev, "port 0x%x already in use\n", io);
  328. return -EBUSY;
  329. }
  330. res = v4l2_device_register(NULL, v4l2_dev);
  331. if (res < 0) {
  332. release_region(tt->io, 2);
  333. v4l2_err(v4l2_dev, "Could not register v4l2_device\n");
  334. return res;
  335. }
  336. strlcpy(tt->vdev.name, v4l2_dev->name, sizeof(tt->vdev.name));
  337. tt->vdev.v4l2_dev = v4l2_dev;
  338. tt->vdev.fops = &terratec_fops;
  339. tt->vdev.ioctl_ops = &terratec_ioctl_ops;
  340. tt->vdev.release = video_device_release_empty;
  341. video_set_drvdata(&tt->vdev, tt);
  342. mutex_init(&tt->lock);
  343. if (video_register_device(&tt->vdev, VFL_TYPE_RADIO, radio_nr) < 0) {
  344. v4l2_device_unregister(&tt->v4l2_dev);
  345. release_region(tt->io, 2);
  346. return -EINVAL;
  347. }
  348. v4l2_info(v4l2_dev, "TERRATEC ActivRadio Standalone card driver.\n");
  349. /* mute card - prevents noisy bootups */
  350. tt_write_vol(tt, 0);
  351. return 0;
  352. }
  353. static void __exit terratec_exit(void)
  354. {
  355. struct terratec *tt = &terratec_card;
  356. struct v4l2_device *v4l2_dev = &tt->v4l2_dev;
  357. video_unregister_device(&tt->vdev);
  358. v4l2_device_unregister(&tt->v4l2_dev);
  359. release_region(tt->io, 2);
  360. v4l2_info(v4l2_dev, "TERRATEC ActivRadio Standalone card driver unloaded.\n");
  361. }
  362. module_init(terratec_init);
  363. module_exit(terratec_exit);