radio-trust.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /* radio-trust.c - Trust FM Radio card driver for Linux 2.2
  2. * by Eric Lammerts <eric@scintilla.utwente.nl>
  3. *
  4. * Based on radio-aztech.c. Original notes:
  5. *
  6. * Adapted to support the Video for Linux API by
  7. * Russell Kroll <rkroll@exploits.org>. Based on original tuner code by:
  8. *
  9. * Quay Ly
  10. * Donald Song
  11. * Jason Lewis (jlewis@twilight.vtc.vsc.edu)
  12. * Scott McGrath (smcgrath@twilight.vtc.vsc.edu)
  13. * William McGrath (wmcgrath@twilight.vtc.vsc.edu)
  14. *
  15. * The basis for this code may be found at http://bigbang.vtc.vsc.edu/fmradio/
  16. */
  17. #include <stdarg.h>
  18. #include <linux/module.h>
  19. #include <linux/init.h>
  20. #include <linux/ioport.h>
  21. #include <asm/io.h>
  22. #include <asm/uaccess.h>
  23. #include <linux/videodev.h>
  24. #include <linux/config.h> /* CONFIG_RADIO_TRUST_PORT */
  25. /* acceptable ports: 0x350 (JP3 shorted), 0x358 (JP3 open) */
  26. #ifndef CONFIG_RADIO_TRUST_PORT
  27. #define CONFIG_RADIO_TRUST_PORT -1
  28. #endif
  29. static int io = CONFIG_RADIO_TRUST_PORT;
  30. static int radio_nr = -1;
  31. static int ioval = 0xf;
  32. static __u16 curvol;
  33. static __u16 curbass;
  34. static __u16 curtreble;
  35. static unsigned long curfreq;
  36. static int curstereo;
  37. static int curmute;
  38. /* i2c addresses */
  39. #define TDA7318_ADDR 0x88
  40. #define TSA6060T_ADDR 0xc4
  41. #define TR_DELAY do { inb(io); inb(io); inb(io); } while(0)
  42. #define TR_SET_SCL outb(ioval |= 2, io)
  43. #define TR_CLR_SCL outb(ioval &= 0xfd, io)
  44. #define TR_SET_SDA outb(ioval |= 1, io)
  45. #define TR_CLR_SDA outb(ioval &= 0xfe, io)
  46. static void write_i2c(int n, ...)
  47. {
  48. unsigned char val, mask;
  49. va_list args;
  50. va_start(args, n);
  51. /* start condition */
  52. TR_SET_SDA;
  53. TR_SET_SCL;
  54. TR_DELAY;
  55. TR_CLR_SDA;
  56. TR_CLR_SCL;
  57. TR_DELAY;
  58. for(; n; n--) {
  59. val = va_arg(args, unsigned);
  60. for(mask = 0x80; mask; mask >>= 1) {
  61. if(val & mask)
  62. TR_SET_SDA;
  63. else
  64. TR_CLR_SDA;
  65. TR_SET_SCL;
  66. TR_DELAY;
  67. TR_CLR_SCL;
  68. TR_DELAY;
  69. }
  70. /* acknowledge bit */
  71. TR_SET_SDA;
  72. TR_SET_SCL;
  73. TR_DELAY;
  74. TR_CLR_SCL;
  75. TR_DELAY;
  76. }
  77. /* stop condition */
  78. TR_CLR_SDA;
  79. TR_DELAY;
  80. TR_SET_SCL;
  81. TR_DELAY;
  82. TR_SET_SDA;
  83. TR_DELAY;
  84. va_end(args);
  85. }
  86. static void tr_setvol(__u16 vol)
  87. {
  88. curvol = vol / 2048;
  89. write_i2c(2, TDA7318_ADDR, curvol ^ 0x1f);
  90. }
  91. static int basstreble2chip[15] = {
  92. 0, 1, 2, 3, 4, 5, 6, 7, 14, 13, 12, 11, 10, 9, 8
  93. };
  94. static void tr_setbass(__u16 bass)
  95. {
  96. curbass = bass / 4370;
  97. write_i2c(2, TDA7318_ADDR, 0x60 | basstreble2chip[curbass]);
  98. }
  99. static void tr_settreble(__u16 treble)
  100. {
  101. curtreble = treble / 4370;
  102. write_i2c(2, TDA7318_ADDR, 0x70 | basstreble2chip[curtreble]);
  103. }
  104. static void tr_setstereo(int stereo)
  105. {
  106. curstereo = !!stereo;
  107. ioval = (ioval & 0xfb) | (!curstereo << 2);
  108. outb(ioval, io);
  109. }
  110. static void tr_setmute(int mute)
  111. {
  112. curmute = !!mute;
  113. ioval = (ioval & 0xf7) | (curmute << 3);
  114. outb(ioval, io);
  115. }
  116. static int tr_getsigstr(void)
  117. {
  118. int i, v;
  119. for(i = 0, v = 0; i < 100; i++) v |= inb(io);
  120. return (v & 1)? 0 : 0xffff;
  121. }
  122. static int tr_getstereo(void)
  123. {
  124. /* don't know how to determine it, just return the setting */
  125. return curstereo;
  126. }
  127. static void tr_setfreq(unsigned long f)
  128. {
  129. f /= 160; /* Convert to 10 kHz units */
  130. f += 1070; /* Add 10.7 MHz IF */
  131. write_i2c(5, TSA6060T_ADDR, (f << 1) | 1, f >> 7, 0x60 | ((f >> 15) & 1), 0);
  132. }
  133. static int tr_do_ioctl(struct inode *inode, struct file *file,
  134. unsigned int cmd, void *arg)
  135. {
  136. switch(cmd)
  137. {
  138. case VIDIOCGCAP:
  139. {
  140. struct video_capability *v = arg;
  141. memset(v,0,sizeof(*v));
  142. v->type=VID_TYPE_TUNER;
  143. v->channels=1;
  144. v->audios=1;
  145. strcpy(v->name, "Trust FM Radio");
  146. return 0;
  147. }
  148. case VIDIOCGTUNER:
  149. {
  150. struct video_tuner *v = arg;
  151. if(v->tuner) /* Only 1 tuner */
  152. return -EINVAL;
  153. v->rangelow = 87500 * 16;
  154. v->rangehigh = 108000 * 16;
  155. v->flags = VIDEO_TUNER_LOW;
  156. v->mode = VIDEO_MODE_AUTO;
  157. v->signal = tr_getsigstr();
  158. if(tr_getstereo())
  159. v->flags |= VIDEO_TUNER_STEREO_ON;
  160. strcpy(v->name, "FM");
  161. return 0;
  162. }
  163. case VIDIOCSTUNER:
  164. {
  165. struct video_tuner *v = arg;
  166. if(v->tuner != 0)
  167. return -EINVAL;
  168. return 0;
  169. }
  170. case VIDIOCGFREQ:
  171. {
  172. unsigned long *freq = arg;
  173. *freq = curfreq;
  174. return 0;
  175. }
  176. case VIDIOCSFREQ:
  177. {
  178. unsigned long *freq = arg;
  179. tr_setfreq(*freq);
  180. return 0;
  181. }
  182. case VIDIOCGAUDIO:
  183. {
  184. struct video_audio *v = arg;
  185. memset(v,0, sizeof(*v));
  186. v->flags = VIDEO_AUDIO_MUTABLE | VIDEO_AUDIO_VOLUME |
  187. VIDEO_AUDIO_BASS | VIDEO_AUDIO_TREBLE;
  188. v->mode = curstereo? VIDEO_SOUND_STEREO : VIDEO_SOUND_MONO;
  189. v->volume = curvol * 2048;
  190. v->step = 2048;
  191. v->bass = curbass * 4370;
  192. v->treble = curtreble * 4370;
  193. strcpy(v->name, "Trust FM Radio");
  194. return 0;
  195. }
  196. case VIDIOCSAUDIO:
  197. {
  198. struct video_audio *v = arg;
  199. if(v->audio)
  200. return -EINVAL;
  201. tr_setvol(v->volume);
  202. tr_setbass(v->bass);
  203. tr_settreble(v->treble);
  204. tr_setstereo(v->mode & VIDEO_SOUND_STEREO);
  205. tr_setmute(v->flags & VIDEO_AUDIO_MUTE);
  206. return 0;
  207. }
  208. default:
  209. return -ENOIOCTLCMD;
  210. }
  211. }
  212. static int tr_ioctl(struct inode *inode, struct file *file,
  213. unsigned int cmd, unsigned long arg)
  214. {
  215. return video_usercopy(inode, file, cmd, arg, tr_do_ioctl);
  216. }
  217. static struct file_operations trust_fops = {
  218. .owner = THIS_MODULE,
  219. .open = video_exclusive_open,
  220. .release = video_exclusive_release,
  221. .ioctl = tr_ioctl,
  222. .llseek = no_llseek,
  223. };
  224. static struct video_device trust_radio=
  225. {
  226. .owner = THIS_MODULE,
  227. .name = "Trust FM Radio",
  228. .type = VID_TYPE_TUNER,
  229. .hardware = VID_HARDWARE_TRUST,
  230. .fops = &trust_fops,
  231. };
  232. static int __init trust_init(void)
  233. {
  234. if(io == -1) {
  235. printk(KERN_ERR "You must set an I/O address with io=0x???\n");
  236. return -EINVAL;
  237. }
  238. if(!request_region(io, 2, "Trust FM Radio")) {
  239. printk(KERN_ERR "trust: port 0x%x already in use\n", io);
  240. return -EBUSY;
  241. }
  242. if(video_register_device(&trust_radio, VFL_TYPE_RADIO, radio_nr)==-1)
  243. {
  244. release_region(io, 2);
  245. return -EINVAL;
  246. }
  247. printk(KERN_INFO "Trust FM Radio card driver v1.0.\n");
  248. write_i2c(2, TDA7318_ADDR, 0x80); /* speaker att. LF = 0 dB */
  249. write_i2c(2, TDA7318_ADDR, 0xa0); /* speaker att. RF = 0 dB */
  250. write_i2c(2, TDA7318_ADDR, 0xc0); /* speaker att. LR = 0 dB */
  251. write_i2c(2, TDA7318_ADDR, 0xe0); /* speaker att. RR = 0 dB */
  252. write_i2c(2, TDA7318_ADDR, 0x40); /* stereo 1 input, gain = 18.75 dB */
  253. tr_setvol(0x8000);
  254. tr_setbass(0x8000);
  255. tr_settreble(0x8000);
  256. tr_setstereo(1);
  257. /* mute card - prevents noisy bootups */
  258. tr_setmute(1);
  259. return 0;
  260. }
  261. MODULE_AUTHOR("Eric Lammerts, Russell Kroll, Quay Lu, Donald Song, Jason Lewis, Scott McGrath, William McGrath");
  262. MODULE_DESCRIPTION("A driver for the Trust FM Radio card.");
  263. MODULE_LICENSE("GPL");
  264. module_param(io, int, 0);
  265. MODULE_PARM_DESC(io, "I/O address of the Trust FM Radio card (0x350 or 0x358)");
  266. module_param(radio_nr, int, 0);
  267. static void __exit cleanup_trust_module(void)
  268. {
  269. video_unregister_device(&trust_radio);
  270. release_region(io, 2);
  271. }
  272. module_init(trust_init);
  273. module_exit(cleanup_trust_module);