radio-trust.c 6.9 KB

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