radio-trust.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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. * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
  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/videodev2.h>
  24. #include <media/v4l2-common.h>
  25. #include <linux/version.h> /* for KERNEL_VERSION MACRO */
  26. #define RADIO_VERSION KERNEL_VERSION(0,0,2)
  27. static struct v4l2_queryctrl radio_qctrl[] = {
  28. {
  29. .id = V4L2_CID_AUDIO_MUTE,
  30. .name = "Mute",
  31. .minimum = 0,
  32. .maximum = 1,
  33. .default_value = 1,
  34. .type = V4L2_CTRL_TYPE_BOOLEAN,
  35. },{
  36. .id = V4L2_CID_AUDIO_VOLUME,
  37. .name = "Volume",
  38. .minimum = 0,
  39. .maximum = 65535,
  40. .step = 2048,
  41. .default_value = 65535,
  42. .type = V4L2_CTRL_TYPE_INTEGER,
  43. },{
  44. .id = V4L2_CID_AUDIO_BASS,
  45. .name = "Bass",
  46. .minimum = 0,
  47. .maximum = 65535,
  48. .step = 4370,
  49. .default_value = 32768,
  50. .type = V4L2_CTRL_TYPE_INTEGER,
  51. },{
  52. .id = V4L2_CID_AUDIO_TREBLE,
  53. .name = "Treble",
  54. .minimum = 0,
  55. .maximum = 65535,
  56. .step = 4370,
  57. .default_value = 32768,
  58. .type = V4L2_CTRL_TYPE_INTEGER,
  59. },
  60. };
  61. /* acceptable ports: 0x350 (JP3 shorted), 0x358 (JP3 open) */
  62. #ifndef CONFIG_RADIO_TRUST_PORT
  63. #define CONFIG_RADIO_TRUST_PORT -1
  64. #endif
  65. static int io = CONFIG_RADIO_TRUST_PORT;
  66. static int radio_nr = -1;
  67. static int ioval = 0xf;
  68. static __u16 curvol;
  69. static __u16 curbass;
  70. static __u16 curtreble;
  71. static unsigned long curfreq;
  72. static int curstereo;
  73. static int curmute;
  74. /* i2c addresses */
  75. #define TDA7318_ADDR 0x88
  76. #define TSA6060T_ADDR 0xc4
  77. #define TR_DELAY do { inb(io); inb(io); inb(io); } while(0)
  78. #define TR_SET_SCL outb(ioval |= 2, io)
  79. #define TR_CLR_SCL outb(ioval &= 0xfd, io)
  80. #define TR_SET_SDA outb(ioval |= 1, io)
  81. #define TR_CLR_SDA outb(ioval &= 0xfe, io)
  82. static void write_i2c(int n, ...)
  83. {
  84. unsigned char val, mask;
  85. va_list args;
  86. va_start(args, n);
  87. /* start condition */
  88. TR_SET_SDA;
  89. TR_SET_SCL;
  90. TR_DELAY;
  91. TR_CLR_SDA;
  92. TR_CLR_SCL;
  93. TR_DELAY;
  94. for(; n; n--) {
  95. val = va_arg(args, unsigned);
  96. for(mask = 0x80; mask; mask >>= 1) {
  97. if(val & mask)
  98. TR_SET_SDA;
  99. else
  100. TR_CLR_SDA;
  101. TR_SET_SCL;
  102. TR_DELAY;
  103. TR_CLR_SCL;
  104. TR_DELAY;
  105. }
  106. /* acknowledge bit */
  107. TR_SET_SDA;
  108. TR_SET_SCL;
  109. TR_DELAY;
  110. TR_CLR_SCL;
  111. TR_DELAY;
  112. }
  113. /* stop condition */
  114. TR_CLR_SDA;
  115. TR_DELAY;
  116. TR_SET_SCL;
  117. TR_DELAY;
  118. TR_SET_SDA;
  119. TR_DELAY;
  120. va_end(args);
  121. }
  122. static void tr_setvol(__u16 vol)
  123. {
  124. curvol = vol / 2048;
  125. write_i2c(2, TDA7318_ADDR, curvol ^ 0x1f);
  126. }
  127. static int basstreble2chip[15] = {
  128. 0, 1, 2, 3, 4, 5, 6, 7, 14, 13, 12, 11, 10, 9, 8
  129. };
  130. static void tr_setbass(__u16 bass)
  131. {
  132. curbass = bass / 4370;
  133. write_i2c(2, TDA7318_ADDR, 0x60 | basstreble2chip[curbass]);
  134. }
  135. static void tr_settreble(__u16 treble)
  136. {
  137. curtreble = treble / 4370;
  138. write_i2c(2, TDA7318_ADDR, 0x70 | basstreble2chip[curtreble]);
  139. }
  140. static void tr_setstereo(int stereo)
  141. {
  142. curstereo = !!stereo;
  143. ioval = (ioval & 0xfb) | (!curstereo << 2);
  144. outb(ioval, io);
  145. }
  146. static void tr_setmute(int mute)
  147. {
  148. curmute = !!mute;
  149. ioval = (ioval & 0xf7) | (curmute << 3);
  150. outb(ioval, io);
  151. }
  152. static int tr_getsigstr(void)
  153. {
  154. int i, v;
  155. for(i = 0, v = 0; i < 100; i++) v |= inb(io);
  156. return (v & 1)? 0 : 0xffff;
  157. }
  158. static int tr_getstereo(void)
  159. {
  160. /* don't know how to determine it, just return the setting */
  161. return curstereo;
  162. }
  163. static void tr_setfreq(unsigned long f)
  164. {
  165. f /= 160; /* Convert to 10 kHz units */
  166. f += 1070; /* Add 10.7 MHz IF */
  167. write_i2c(5, TSA6060T_ADDR, (f << 1) | 1, f >> 7, 0x60 | ((f >> 15) & 1), 0);
  168. }
  169. static int vidioc_querycap(struct file *file, void *priv,
  170. struct v4l2_capability *v)
  171. {
  172. strlcpy(v->driver, "radio-trust", sizeof(v->driver));
  173. strlcpy(v->card, "Trust FM Radio", sizeof(v->card));
  174. sprintf(v->bus_info, "ISA");
  175. v->version = RADIO_VERSION;
  176. v->capabilities = V4L2_CAP_TUNER;
  177. return 0;
  178. }
  179. static int vidioc_g_tuner(struct file *file, void *priv,
  180. struct v4l2_tuner *v)
  181. {
  182. if (v->index > 0)
  183. return -EINVAL;
  184. strcpy(v->name, "FM");
  185. v->type = V4L2_TUNER_RADIO;
  186. v->rangelow = (87.5*16000);
  187. v->rangehigh = (108*16000);
  188. v->rxsubchans = V4L2_TUNER_SUB_MONO|V4L2_TUNER_SUB_STEREO;
  189. v->capability = V4L2_TUNER_CAP_LOW;
  190. if (tr_getstereo())
  191. v->audmode = V4L2_TUNER_MODE_STEREO;
  192. else
  193. v->audmode = V4L2_TUNER_MODE_MONO;
  194. v->signal = tr_getsigstr();
  195. return 0;
  196. }
  197. static int vidioc_s_tuner(struct file *file, void *priv,
  198. struct v4l2_tuner *v)
  199. {
  200. if (v->index > 0)
  201. return -EINVAL;
  202. return 0;
  203. }
  204. static int vidioc_s_frequency(struct file *file, void *priv,
  205. struct v4l2_frequency *f)
  206. {
  207. curfreq = f->frequency;
  208. tr_setfreq(curfreq);
  209. return 0;
  210. }
  211. static int vidioc_g_frequency(struct file *file, void *priv,
  212. struct v4l2_frequency *f)
  213. {
  214. f->type = V4L2_TUNER_RADIO;
  215. f->frequency = curfreq;
  216. return 0;
  217. }
  218. static int vidioc_queryctrl(struct file *file, void *priv,
  219. struct v4l2_queryctrl *qc)
  220. {
  221. int i;
  222. for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
  223. if (qc->id && qc->id == radio_qctrl[i].id) {
  224. memcpy(qc, &(radio_qctrl[i]),
  225. 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. switch (ctrl->id) {
  235. case V4L2_CID_AUDIO_MUTE:
  236. ctrl->value = curmute;
  237. return 0;
  238. case V4L2_CID_AUDIO_VOLUME:
  239. ctrl->value = curvol * 2048;
  240. return 0;
  241. case V4L2_CID_AUDIO_BASS:
  242. ctrl->value = curbass * 4370;
  243. return 0;
  244. case V4L2_CID_AUDIO_TREBLE:
  245. ctrl->value = curtreble * 4370;
  246. return 0;
  247. }
  248. return -EINVAL;
  249. }
  250. static int vidioc_s_ctrl(struct file *file, void *priv,
  251. struct v4l2_control *ctrl)
  252. {
  253. switch (ctrl->id) {
  254. case V4L2_CID_AUDIO_MUTE:
  255. tr_setmute(ctrl->value);
  256. return 0;
  257. case V4L2_CID_AUDIO_VOLUME:
  258. tr_setvol(ctrl->value);
  259. return 0;
  260. case V4L2_CID_AUDIO_BASS:
  261. tr_setbass(ctrl->value);
  262. return 0;
  263. case V4L2_CID_AUDIO_TREBLE:
  264. tr_settreble(ctrl->value);
  265. return 0;
  266. }
  267. return -EINVAL;
  268. }
  269. static int vidioc_g_audio(struct file *file, void *priv,
  270. struct v4l2_audio *a)
  271. {
  272. if (a->index > 1)
  273. return -EINVAL;
  274. strcpy(a->name, "Radio");
  275. a->capability = V4L2_AUDCAP_STEREO;
  276. return 0;
  277. }
  278. static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
  279. {
  280. *i = 0;
  281. return 0;
  282. }
  283. static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
  284. {
  285. if (i != 0)
  286. return -EINVAL;
  287. return 0;
  288. }
  289. static int vidioc_s_audio(struct file *file, void *priv,
  290. struct v4l2_audio *a)
  291. {
  292. if (a->index != 0)
  293. return -EINVAL;
  294. return 0;
  295. }
  296. static const struct file_operations trust_fops = {
  297. .owner = THIS_MODULE,
  298. .open = video_exclusive_open,
  299. .release = video_exclusive_release,
  300. .ioctl = video_ioctl2,
  301. .compat_ioctl = v4l_compat_ioctl32,
  302. .llseek = no_llseek,
  303. };
  304. static struct video_device trust_radio=
  305. {
  306. .owner = THIS_MODULE,
  307. .name = "Trust FM Radio",
  308. .type = VID_TYPE_TUNER,
  309. .fops = &trust_fops,
  310. .vidioc_querycap = vidioc_querycap,
  311. .vidioc_g_tuner = vidioc_g_tuner,
  312. .vidioc_s_tuner = vidioc_s_tuner,
  313. .vidioc_g_frequency = vidioc_g_frequency,
  314. .vidioc_s_frequency = vidioc_s_frequency,
  315. .vidioc_queryctrl = vidioc_queryctrl,
  316. .vidioc_g_ctrl = vidioc_g_ctrl,
  317. .vidioc_s_ctrl = vidioc_s_ctrl,
  318. .vidioc_g_audio = vidioc_g_audio,
  319. .vidioc_s_audio = vidioc_s_audio,
  320. .vidioc_g_input = vidioc_g_input,
  321. .vidioc_s_input = vidioc_s_input,
  322. };
  323. static int __init trust_init(void)
  324. {
  325. if(io == -1) {
  326. printk(KERN_ERR "You must set an I/O address with io=0x???\n");
  327. return -EINVAL;
  328. }
  329. if(!request_region(io, 2, "Trust FM Radio")) {
  330. printk(KERN_ERR "trust: port 0x%x already in use\n", io);
  331. return -EBUSY;
  332. }
  333. if(video_register_device(&trust_radio, VFL_TYPE_RADIO, radio_nr)==-1)
  334. {
  335. release_region(io, 2);
  336. return -EINVAL;
  337. }
  338. printk(KERN_INFO "Trust FM Radio card driver v1.0.\n");
  339. write_i2c(2, TDA7318_ADDR, 0x80); /* speaker att. LF = 0 dB */
  340. write_i2c(2, TDA7318_ADDR, 0xa0); /* speaker att. RF = 0 dB */
  341. write_i2c(2, TDA7318_ADDR, 0xc0); /* speaker att. LR = 0 dB */
  342. write_i2c(2, TDA7318_ADDR, 0xe0); /* speaker att. RR = 0 dB */
  343. write_i2c(2, TDA7318_ADDR, 0x40); /* stereo 1 input, gain = 18.75 dB */
  344. tr_setvol(0x8000);
  345. tr_setbass(0x8000);
  346. tr_settreble(0x8000);
  347. tr_setstereo(1);
  348. /* mute card - prevents noisy bootups */
  349. tr_setmute(1);
  350. return 0;
  351. }
  352. MODULE_AUTHOR("Eric Lammerts, Russell Kroll, Quay Lu, Donald Song, Jason Lewis, Scott McGrath, William McGrath");
  353. MODULE_DESCRIPTION("A driver for the Trust FM Radio card.");
  354. MODULE_LICENSE("GPL");
  355. module_param(io, int, 0);
  356. MODULE_PARM_DESC(io, "I/O address of the Trust FM Radio card (0x350 or 0x358)");
  357. module_param(radio_nr, int, 0);
  358. static void __exit cleanup_trust_module(void)
  359. {
  360. video_unregister_device(&trust_radio);
  361. release_region(io, 2);
  362. }
  363. module_init(trust_init);
  364. module_exit(cleanup_trust_module);