radio-terratec.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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 <asm/io.h> /* outb, outb_p */
  31. #include <asm/uaccess.h> /* copy to/from user */
  32. #include <linux/videodev2.h> /* kernel radio structs */
  33. #include <media/v4l2-common.h>
  34. #include <linux/spinlock.h>
  35. #include <linux/version.h> /* for KERNEL_VERSION MACRO */
  36. #define RADIO_VERSION KERNEL_VERSION(0,0,2)
  37. static struct v4l2_queryctrl radio_qctrl[] = {
  38. {
  39. .id = V4L2_CID_AUDIO_MUTE,
  40. .name = "Mute",
  41. .minimum = 0,
  42. .maximum = 1,
  43. .default_value = 1,
  44. .type = V4L2_CTRL_TYPE_BOOLEAN,
  45. },{
  46. .id = V4L2_CID_AUDIO_VOLUME,
  47. .name = "Volume",
  48. .minimum = 0,
  49. .maximum = 0xff,
  50. .step = 1,
  51. .default_value = 0xff,
  52. .type = V4L2_CTRL_TYPE_INTEGER,
  53. }
  54. };
  55. #ifndef CONFIG_RADIO_TERRATEC_PORT
  56. #define CONFIG_RADIO_TERRATEC_PORT 0x590
  57. #endif
  58. /**************** this ones are for the terratec *******************/
  59. #define BASEPORT 0x590
  60. #define VOLPORT 0x591
  61. #define WRT_DIS 0x00
  62. #define CLK_OFF 0x00
  63. #define IIC_DATA 0x01
  64. #define IIC_CLK 0x02
  65. #define DATA 0x04
  66. #define CLK_ON 0x08
  67. #define WRT_EN 0x10
  68. /*******************************************************************/
  69. static int io = CONFIG_RADIO_TERRATEC_PORT;
  70. static int radio_nr = -1;
  71. static spinlock_t lock;
  72. struct tt_device
  73. {
  74. int port;
  75. int curvol;
  76. unsigned long curfreq;
  77. int muted;
  78. };
  79. /* local things */
  80. static void cardWriteVol(int volume)
  81. {
  82. int i;
  83. volume = volume+(volume * 32); // change both channels
  84. spin_lock(&lock);
  85. for (i=0;i<8;i++)
  86. {
  87. if (volume & (0x80>>i))
  88. outb(0x80, VOLPORT);
  89. else outb(0x00, VOLPORT);
  90. }
  91. spin_unlock(&lock);
  92. }
  93. static void tt_mute(struct tt_device *dev)
  94. {
  95. dev->muted = 1;
  96. cardWriteVol(0);
  97. }
  98. static int tt_setvol(struct tt_device *dev, int vol)
  99. {
  100. // printk(KERN_ERR "setvol called, vol = %d\n", vol);
  101. if(vol == dev->curvol) { /* requested volume = current */
  102. if (dev->muted) { /* user is unmuting the card */
  103. dev->muted = 0;
  104. cardWriteVol(vol); /* enable card */
  105. }
  106. return 0;
  107. }
  108. if(vol == 0) { /* volume = 0 means mute the card */
  109. cardWriteVol(0); /* "turn off card" by setting vol to 0 */
  110. dev->curvol = vol; /* track the volume state! */
  111. return 0;
  112. }
  113. dev->muted = 0;
  114. cardWriteVol(vol);
  115. dev->curvol = vol;
  116. return 0;
  117. }
  118. /* this is the worst part in this driver */
  119. /* many more or less strange things are going on here, but hey, it works :) */
  120. static int tt_setfreq(struct tt_device *dev, unsigned long freq1)
  121. {
  122. int freq;
  123. int i;
  124. int p;
  125. int temp;
  126. long rest;
  127. unsigned char buffer[25]; /* we have to bit shift 25 registers */
  128. freq = freq1/160; /* convert the freq. to a nice to handle value */
  129. for(i=24;i>-1;i--)
  130. buffer[i]=0;
  131. rest = freq*10+10700; /* i once had understood what is going on here */
  132. /* maybe some wise guy (friedhelm?) can comment this stuff */
  133. i=13;
  134. p=10;
  135. temp=102400;
  136. while (rest!=0)
  137. {
  138. if (rest%temp == rest)
  139. buffer[i] = 0;
  140. else
  141. {
  142. buffer[i] = 1;
  143. rest = rest-temp;
  144. }
  145. i--;
  146. p--;
  147. temp = temp/2;
  148. }
  149. spin_lock(&lock);
  150. for (i=24;i>-1;i--) /* bit shift the values to the radiocard */
  151. {
  152. if (buffer[i]==1)
  153. {
  154. outb(WRT_EN|DATA, BASEPORT);
  155. outb(WRT_EN|DATA|CLK_ON , BASEPORT);
  156. outb(WRT_EN|DATA, BASEPORT);
  157. }
  158. else
  159. {
  160. outb(WRT_EN|0x00, BASEPORT);
  161. outb(WRT_EN|0x00|CLK_ON , BASEPORT);
  162. }
  163. }
  164. outb(0x00, BASEPORT);
  165. spin_unlock(&lock);
  166. return 0;
  167. }
  168. static int tt_getsigstr(struct tt_device *dev) /* TODO */
  169. {
  170. if (inb(io) & 2) /* bit set = no signal present */
  171. return 0;
  172. return 1; /* signal present */
  173. }
  174. /* implement the video4linux api */
  175. static int tt_do_ioctl(struct inode *inode, struct file *file,
  176. unsigned int cmd, void *arg)
  177. {
  178. struct video_device *dev = video_devdata(file);
  179. struct tt_device *tt=dev->priv;
  180. switch(cmd)
  181. {
  182. case VIDIOC_QUERYCAP:
  183. {
  184. struct v4l2_capability *v = arg;
  185. memset(v,0,sizeof(*v));
  186. strlcpy(v->driver, "radio-terratec", sizeof (v->driver));
  187. strlcpy(v->card, "ActiveRadio", sizeof (v->card));
  188. sprintf(v->bus_info,"ISA");
  189. v->version = RADIO_VERSION;
  190. v->capabilities = V4L2_CAP_TUNER;
  191. return 0;
  192. }
  193. case VIDIOC_G_TUNER:
  194. {
  195. struct v4l2_tuner *v = arg;
  196. if (v->index > 0)
  197. return -EINVAL;
  198. memset(v,0,sizeof(*v));
  199. strcpy(v->name, "FM");
  200. v->type = V4L2_TUNER_RADIO;
  201. v->rangelow=(87*16000);
  202. v->rangehigh=(108*16000);
  203. v->rxsubchans =V4L2_TUNER_SUB_MONO;
  204. v->capability=V4L2_TUNER_CAP_LOW;
  205. v->audmode = V4L2_TUNER_MODE_MONO;
  206. v->signal=0xFFFF*tt_getsigstr(tt);
  207. return 0;
  208. }
  209. case VIDIOC_S_TUNER:
  210. {
  211. struct v4l2_tuner *v = arg;
  212. if (v->index > 0)
  213. return -EINVAL;
  214. return 0;
  215. }
  216. case VIDIOC_S_FREQUENCY:
  217. {
  218. struct v4l2_frequency *f = arg;
  219. tt->curfreq = f->frequency;
  220. tt_setfreq(tt, tt->curfreq);
  221. return 0;
  222. }
  223. case VIDIOC_G_FREQUENCY:
  224. {
  225. struct v4l2_frequency *f = arg;
  226. f->type = V4L2_TUNER_RADIO;
  227. f->frequency = tt->curfreq;
  228. return 0;
  229. }
  230. case VIDIOC_QUERYCTRL:
  231. {
  232. struct v4l2_queryctrl *qc = arg;
  233. int i;
  234. for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
  235. if (qc->id && qc->id == radio_qctrl[i].id) {
  236. memcpy(qc, &(radio_qctrl[i]),
  237. sizeof(*qc));
  238. return (0);
  239. }
  240. }
  241. return -EINVAL;
  242. }
  243. case VIDIOC_G_CTRL:
  244. {
  245. struct v4l2_control *ctrl= arg;
  246. switch (ctrl->id) {
  247. case V4L2_CID_AUDIO_MUTE:
  248. if (tt->muted)
  249. ctrl->value=1;
  250. else
  251. ctrl->value=0;
  252. return (0);
  253. case V4L2_CID_AUDIO_VOLUME:
  254. ctrl->value=tt->curvol * 6554;
  255. return (0);
  256. }
  257. return -EINVAL;
  258. }
  259. case VIDIOC_S_CTRL:
  260. {
  261. struct v4l2_control *ctrl= arg;
  262. switch (ctrl->id) {
  263. case V4L2_CID_AUDIO_MUTE:
  264. if (ctrl->value) {
  265. tt_mute(tt);
  266. } else {
  267. tt_setvol(tt,tt->curvol);
  268. }
  269. return (0);
  270. case V4L2_CID_AUDIO_VOLUME:
  271. tt_setvol(tt,ctrl->value);
  272. return (0);
  273. }
  274. return -EINVAL;
  275. }
  276. default:
  277. return v4l_compat_translate_ioctl(inode,file,cmd,arg,
  278. tt_do_ioctl);
  279. }
  280. }
  281. static int tt_ioctl(struct inode *inode, struct file *file,
  282. unsigned int cmd, unsigned long arg)
  283. {
  284. return video_usercopy(inode, file, cmd, arg, tt_do_ioctl);
  285. }
  286. static struct tt_device terratec_unit;
  287. static struct file_operations terratec_fops = {
  288. .owner = THIS_MODULE,
  289. .open = video_exclusive_open,
  290. .release = video_exclusive_release,
  291. .ioctl = tt_ioctl,
  292. .compat_ioctl = v4l_compat_ioctl32,
  293. .llseek = no_llseek,
  294. };
  295. static struct video_device terratec_radio=
  296. {
  297. .owner = THIS_MODULE,
  298. .name = "TerraTec ActiveRadio",
  299. .type = VID_TYPE_TUNER,
  300. .hardware = 0,
  301. .fops = &terratec_fops,
  302. };
  303. static int __init terratec_init(void)
  304. {
  305. if(io==-1)
  306. {
  307. printk(KERN_ERR "You must set an I/O address with io=0x???\n");
  308. return -EINVAL;
  309. }
  310. if (!request_region(io, 2, "terratec"))
  311. {
  312. printk(KERN_ERR "TerraTec: port 0x%x already in use\n", io);
  313. return -EBUSY;
  314. }
  315. terratec_radio.priv=&terratec_unit;
  316. spin_lock_init(&lock);
  317. if(video_register_device(&terratec_radio, VFL_TYPE_RADIO, radio_nr)==-1)
  318. {
  319. release_region(io,2);
  320. return -EINVAL;
  321. }
  322. printk(KERN_INFO "TERRATEC ActivRadio Standalone card driver.\n");
  323. /* mute card - prevents noisy bootups */
  324. /* this ensures that the volume is all the way down */
  325. cardWriteVol(0);
  326. terratec_unit.curvol = 0;
  327. return 0;
  328. }
  329. MODULE_AUTHOR("R.OFFERMANNS & others");
  330. MODULE_DESCRIPTION("A driver for the TerraTec ActiveRadio Standalone radio card.");
  331. MODULE_LICENSE("GPL");
  332. module_param(io, int, 0);
  333. MODULE_PARM_DESC(io, "I/O address of the TerraTec ActiveRadio card (0x590 or 0x591)");
  334. module_param(radio_nr, int, 0);
  335. static void __exit terratec_cleanup_module(void)
  336. {
  337. video_unregister_device(&terratec_radio);
  338. release_region(io,2);
  339. printk(KERN_INFO "TERRATEC ActivRadio Standalone card driver unloaded.\n");
  340. }
  341. module_init(terratec_init);
  342. module_exit(terratec_cleanup_module);