radio-trust.c 10 KB

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