radio-terratec.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. */
  25. #include <linux/module.h> /* Modules */
  26. #include <linux/init.h> /* Initdata */
  27. #include <linux/ioport.h> /* request_region */
  28. #include <linux/delay.h> /* udelay */
  29. #include <asm/io.h> /* outb, outb_p */
  30. #include <asm/uaccess.h> /* copy to/from user */
  31. #include <linux/videodev.h> /* kernel radio structs */
  32. #include <media/v4l2-common.h>
  33. #include <linux/config.h> /* CONFIG_RADIO_TERRATEC_PORT */
  34. #include <linux/spinlock.h>
  35. #ifndef CONFIG_RADIO_TERRATEC_PORT
  36. #define CONFIG_RADIO_TERRATEC_PORT 0x590
  37. #endif
  38. /**************** this ones are for the terratec *******************/
  39. #define BASEPORT 0x590
  40. #define VOLPORT 0x591
  41. #define WRT_DIS 0x00
  42. #define CLK_OFF 0x00
  43. #define IIC_DATA 0x01
  44. #define IIC_CLK 0x02
  45. #define DATA 0x04
  46. #define CLK_ON 0x08
  47. #define WRT_EN 0x10
  48. /*******************************************************************/
  49. static int io = CONFIG_RADIO_TERRATEC_PORT;
  50. static int radio_nr = -1;
  51. static spinlock_t lock;
  52. struct tt_device
  53. {
  54. int port;
  55. int curvol;
  56. unsigned long curfreq;
  57. int muted;
  58. };
  59. /* local things */
  60. static void cardWriteVol(int volume)
  61. {
  62. int i;
  63. volume = volume+(volume * 32); // change both channels
  64. spin_lock(&lock);
  65. for (i=0;i<8;i++)
  66. {
  67. if (volume & (0x80>>i))
  68. outb(0x80, VOLPORT);
  69. else outb(0x00, VOLPORT);
  70. }
  71. spin_unlock(&lock);
  72. }
  73. static void tt_mute(struct tt_device *dev)
  74. {
  75. dev->muted = 1;
  76. cardWriteVol(0);
  77. }
  78. static int tt_setvol(struct tt_device *dev, int vol)
  79. {
  80. // printk(KERN_ERR "setvol called, vol = %d\n", vol);
  81. if(vol == dev->curvol) { /* requested volume = current */
  82. if (dev->muted) { /* user is unmuting the card */
  83. dev->muted = 0;
  84. cardWriteVol(vol); /* enable card */
  85. }
  86. return 0;
  87. }
  88. if(vol == 0) { /* volume = 0 means mute the card */
  89. cardWriteVol(0); /* "turn off card" by setting vol to 0 */
  90. dev->curvol = vol; /* track the volume state! */
  91. return 0;
  92. }
  93. dev->muted = 0;
  94. cardWriteVol(vol);
  95. dev->curvol = vol;
  96. return 0;
  97. }
  98. /* this is the worst part in this driver */
  99. /* many more or less strange things are going on here, but hey, it works :) */
  100. static int tt_setfreq(struct tt_device *dev, unsigned long freq1)
  101. {
  102. int freq;
  103. int i;
  104. int p;
  105. int temp;
  106. long rest;
  107. unsigned char buffer[25]; /* we have to bit shift 25 registers */
  108. freq = freq1/160; /* convert the freq. to a nice to handle value */
  109. for(i=24;i>-1;i--)
  110. buffer[i]=0;
  111. rest = freq*10+10700; /* i once had understood what is going on here */
  112. /* maybe some wise guy (friedhelm?) can comment this stuff */
  113. i=13;
  114. p=10;
  115. temp=102400;
  116. while (rest!=0)
  117. {
  118. if (rest%temp == rest)
  119. buffer[i] = 0;
  120. else
  121. {
  122. buffer[i] = 1;
  123. rest = rest-temp;
  124. }
  125. i--;
  126. p--;
  127. temp = temp/2;
  128. }
  129. spin_lock(&lock);
  130. for (i=24;i>-1;i--) /* bit shift the values to the radiocard */
  131. {
  132. if (buffer[i]==1)
  133. {
  134. outb(WRT_EN|DATA, BASEPORT);
  135. outb(WRT_EN|DATA|CLK_ON , BASEPORT);
  136. outb(WRT_EN|DATA, BASEPORT);
  137. }
  138. else
  139. {
  140. outb(WRT_EN|0x00, BASEPORT);
  141. outb(WRT_EN|0x00|CLK_ON , BASEPORT);
  142. }
  143. }
  144. outb(0x00, BASEPORT);
  145. spin_unlock(&lock);
  146. return 0;
  147. }
  148. static int tt_getsigstr(struct tt_device *dev) /* TODO */
  149. {
  150. if (inb(io) & 2) /* bit set = no signal present */
  151. return 0;
  152. return 1; /* signal present */
  153. }
  154. /* implement the video4linux api */
  155. static int tt_do_ioctl(struct inode *inode, struct file *file,
  156. unsigned int cmd, void *arg)
  157. {
  158. struct video_device *dev = video_devdata(file);
  159. struct tt_device *tt=dev->priv;
  160. switch(cmd)
  161. {
  162. case VIDIOCGCAP:
  163. {
  164. struct video_capability *v = arg;
  165. memset(v,0,sizeof(*v));
  166. v->type=VID_TYPE_TUNER;
  167. v->channels=1;
  168. v->audios=1;
  169. strcpy(v->name, "ActiveRadio");
  170. return 0;
  171. }
  172. case VIDIOCGTUNER:
  173. {
  174. struct video_tuner *v = arg;
  175. if(v->tuner) /* Only 1 tuner */
  176. return -EINVAL;
  177. v->rangelow=(87*16000);
  178. v->rangehigh=(108*16000);
  179. v->flags=VIDEO_TUNER_LOW;
  180. v->mode=VIDEO_MODE_AUTO;
  181. strcpy(v->name, "FM");
  182. v->signal=0xFFFF*tt_getsigstr(tt);
  183. return 0;
  184. }
  185. case VIDIOCSTUNER:
  186. {
  187. struct video_tuner *v = arg;
  188. if(v->tuner!=0)
  189. return -EINVAL;
  190. /* Only 1 tuner so no setting needed ! */
  191. return 0;
  192. }
  193. case VIDIOCGFREQ:
  194. {
  195. unsigned long *freq = arg;
  196. *freq = tt->curfreq;
  197. return 0;
  198. }
  199. case VIDIOCSFREQ:
  200. {
  201. unsigned long *freq = arg;
  202. tt->curfreq = *freq;
  203. tt_setfreq(tt, tt->curfreq);
  204. return 0;
  205. }
  206. case VIDIOCGAUDIO:
  207. {
  208. struct video_audio *v = arg;
  209. memset(v,0, sizeof(*v));
  210. v->flags|=VIDEO_AUDIO_MUTABLE|VIDEO_AUDIO_VOLUME;
  211. v->volume=tt->curvol * 6554;
  212. v->step=6554;
  213. strcpy(v->name, "Radio");
  214. return 0;
  215. }
  216. case VIDIOCSAUDIO:
  217. {
  218. struct video_audio *v = arg;
  219. if(v->audio)
  220. return -EINVAL;
  221. if(v->flags&VIDEO_AUDIO_MUTE)
  222. tt_mute(tt);
  223. else
  224. tt_setvol(tt,v->volume/6554);
  225. return 0;
  226. }
  227. default:
  228. return -ENOIOCTLCMD;
  229. }
  230. }
  231. static int tt_ioctl(struct inode *inode, struct file *file,
  232. unsigned int cmd, unsigned long arg)
  233. {
  234. return video_usercopy(inode, file, cmd, arg, tt_do_ioctl);
  235. }
  236. static struct tt_device terratec_unit;
  237. static struct file_operations terratec_fops = {
  238. .owner = THIS_MODULE,
  239. .open = video_exclusive_open,
  240. .release = video_exclusive_release,
  241. .ioctl = tt_ioctl,
  242. .compat_ioctl = v4l_compat_ioctl32,
  243. .llseek = no_llseek,
  244. };
  245. static struct video_device terratec_radio=
  246. {
  247. .owner = THIS_MODULE,
  248. .name = "TerraTec ActiveRadio",
  249. .type = VID_TYPE_TUNER,
  250. .hardware = VID_HARDWARE_TERRATEC,
  251. .fops = &terratec_fops,
  252. };
  253. static int __init terratec_init(void)
  254. {
  255. if(io==-1)
  256. {
  257. printk(KERN_ERR "You must set an I/O address with io=0x???\n");
  258. return -EINVAL;
  259. }
  260. if (!request_region(io, 2, "terratec"))
  261. {
  262. printk(KERN_ERR "TerraTec: port 0x%x already in use\n", io);
  263. return -EBUSY;
  264. }
  265. terratec_radio.priv=&terratec_unit;
  266. spin_lock_init(&lock);
  267. if(video_register_device(&terratec_radio, VFL_TYPE_RADIO, radio_nr)==-1)
  268. {
  269. release_region(io,2);
  270. return -EINVAL;
  271. }
  272. printk(KERN_INFO "TERRATEC ActivRadio Standalone card driver.\n");
  273. /* mute card - prevents noisy bootups */
  274. /* this ensures that the volume is all the way down */
  275. cardWriteVol(0);
  276. terratec_unit.curvol = 0;
  277. return 0;
  278. }
  279. MODULE_AUTHOR("R.OFFERMANNS & others");
  280. MODULE_DESCRIPTION("A driver for the TerraTec ActiveRadio Standalone radio card.");
  281. MODULE_LICENSE("GPL");
  282. module_param(io, int, 0);
  283. MODULE_PARM_DESC(io, "I/O address of the TerraTec ActiveRadio card (0x590 or 0x591)");
  284. module_param(radio_nr, int, 0);
  285. static void __exit terratec_cleanup_module(void)
  286. {
  287. video_unregister_device(&terratec_radio);
  288. release_region(io,2);
  289. printk(KERN_INFO "TERRATEC ActivRadio Standalone card driver unloaded.\n");
  290. }
  291. module_init(terratec_init);
  292. module_exit(terratec_cleanup_module);