radio-trust.c 9.5 KB

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