radio-terratec.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /* Terratec ActiveRadio ISA Standalone card driver for Linux radio support
  2. * (c) 1999 R. Offermanns (rolf@offermanns.de)
  3. * based on the aimslab radio driver from M. Kirkwood
  4. * many thanks to Michael Becker and Friedhelm Birth (from TerraTec)
  5. *
  6. *
  7. * History:
  8. * 1999-05-21 First preview release
  9. *
  10. * Notes on the hardware:
  11. * There are two "main" chips on the card:
  12. * - Philips OM5610 (http://www-us.semiconductors.philips.com/acrobat/datasheets/OM5610_2.pdf)
  13. * - Philips SAA6588 (http://www-us.semiconductors.philips.com/acrobat/datasheets/SAA6588_1.pdf)
  14. * (you can get the datasheet at the above links)
  15. *
  16. * Frequency control is done digitally -- ie out(port,encodefreq(95.8));
  17. * Volume Control is done digitally
  18. *
  19. * there is a I2C controlled RDS decoder (SAA6588) onboard, which i would like to support someday
  20. * (as soon i have understand how to get started :)
  21. * If you can help me out with that, please contact me!!
  22. *
  23. *
  24. * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
  25. */
  26. #include <linux/module.h> /* Modules */
  27. #include <linux/init.h> /* Initdata */
  28. #include <linux/ioport.h> /* request_region */
  29. #include <linux/videodev2.h> /* kernel radio structs */
  30. #include <linux/mutex.h>
  31. #include <linux/version.h> /* for KERNEL_VERSION MACRO */
  32. #include <linux/io.h> /* outb, outb_p */
  33. #include <media/v4l2-device.h>
  34. #include <media/v4l2-ioctl.h>
  35. MODULE_AUTHOR("R.OFFERMANNS & others");
  36. MODULE_DESCRIPTION("A driver for the TerraTec ActiveRadio Standalone radio card.");
  37. MODULE_LICENSE("GPL");
  38. #ifndef CONFIG_RADIO_TERRATEC_PORT
  39. #define CONFIG_RADIO_TERRATEC_PORT 0x590
  40. #endif
  41. static int io = CONFIG_RADIO_TERRATEC_PORT;
  42. static int radio_nr = -1;
  43. module_param(io, int, 0);
  44. MODULE_PARM_DESC(io, "I/O address of the TerraTec ActiveRadio card (0x590 or 0x591)");
  45. module_param(radio_nr, int, 0);
  46. #define RADIO_VERSION KERNEL_VERSION(0, 0, 2)
  47. static struct v4l2_queryctrl radio_qctrl[] = {
  48. {
  49. .id = V4L2_CID_AUDIO_MUTE,
  50. .name = "Mute",
  51. .minimum = 0,
  52. .maximum = 1,
  53. .default_value = 1,
  54. .type = V4L2_CTRL_TYPE_BOOLEAN,
  55. },{
  56. .id = V4L2_CID_AUDIO_VOLUME,
  57. .name = "Volume",
  58. .minimum = 0,
  59. .maximum = 0xff,
  60. .step = 1,
  61. .default_value = 0xff,
  62. .type = V4L2_CTRL_TYPE_INTEGER,
  63. }
  64. };
  65. #define WRT_DIS 0x00
  66. #define CLK_OFF 0x00
  67. #define IIC_DATA 0x01
  68. #define IIC_CLK 0x02
  69. #define DATA 0x04
  70. #define CLK_ON 0x08
  71. #define WRT_EN 0x10
  72. struct terratec
  73. {
  74. struct v4l2_device v4l2_dev;
  75. struct video_device vdev;
  76. int io;
  77. int curvol;
  78. unsigned long curfreq;
  79. int muted;
  80. struct mutex lock;
  81. };
  82. static struct terratec terratec_card;
  83. /* local things */
  84. static void tt_write_vol(struct terratec *tt, int volume)
  85. {
  86. int i;
  87. volume = volume + (volume * 32); /* change both channels */
  88. mutex_lock(&tt->lock);
  89. for (i = 0; i < 8; i++) {
  90. if (volume & (0x80 >> i))
  91. outb(0x80, tt->io + 1);
  92. else
  93. outb(0x00, tt->io + 1);
  94. }
  95. mutex_unlock(&tt->lock);
  96. }
  97. static void tt_mute(struct terratec *tt)
  98. {
  99. tt->muted = 1;
  100. tt_write_vol(tt, 0);
  101. }
  102. static int tt_setvol(struct terratec *tt, int vol)
  103. {
  104. if (vol == tt->curvol) { /* requested volume = current */
  105. if (tt->muted) { /* user is unmuting the card */
  106. tt->muted = 0;
  107. tt_write_vol(tt, vol); /* enable card */
  108. }
  109. return 0;
  110. }
  111. if (vol == 0) { /* volume = 0 means mute the card */
  112. tt_write_vol(tt, 0); /* "turn off card" by setting vol to 0 */
  113. tt->curvol = vol; /* track the volume state! */
  114. return 0;
  115. }
  116. tt->muted = 0;
  117. tt_write_vol(tt, vol);
  118. tt->curvol = vol;
  119. return 0;
  120. }
  121. /* this is the worst part in this driver */
  122. /* many more or less strange things are going on here, but hey, it works :) */
  123. static int tt_setfreq(struct terratec *tt, unsigned long freq1)
  124. {
  125. int freq;
  126. int i;
  127. int p;
  128. int temp;
  129. long rest;
  130. unsigned char buffer[25]; /* we have to bit shift 25 registers */
  131. mutex_lock(&tt->lock);
  132. tt->curfreq = freq1;
  133. freq = freq1 / 160; /* convert the freq. to a nice to handle value */
  134. memset(buffer, 0, sizeof(buffer));
  135. rest = freq * 10 + 10700; /* I once had understood what is going on here */
  136. /* maybe some wise guy (friedhelm?) can comment this stuff */
  137. i = 13;
  138. p = 10;
  139. temp = 102400;
  140. while (rest != 0) {
  141. if (rest % temp == rest)
  142. buffer[i] = 0;
  143. else {
  144. buffer[i] = 1;
  145. rest = rest - temp;
  146. }
  147. i--;
  148. p--;
  149. temp = temp / 2;
  150. }
  151. for (i = 24; i > -1; i--) { /* bit shift the values to the radiocard */
  152. if (buffer[i] == 1) {
  153. outb(WRT_EN | DATA, tt->io);
  154. outb(WRT_EN | DATA | CLK_ON, tt->io);
  155. outb(WRT_EN | DATA, tt->io);
  156. } else {
  157. outb(WRT_EN | 0x00, tt->io);
  158. outb(WRT_EN | 0x00 | CLK_ON, tt->io);
  159. }
  160. }
  161. outb(0x00, tt->io);
  162. mutex_unlock(&tt->lock);
  163. return 0;
  164. }
  165. static int tt_getsigstr(struct terratec *tt)
  166. {
  167. if (inb(tt->io) & 2) /* bit set = no signal present */
  168. return 0;
  169. return 1; /* signal present */
  170. }
  171. static int vidioc_querycap(struct file *file, void *priv,
  172. struct v4l2_capability *v)
  173. {
  174. strlcpy(v->driver, "radio-terratec", sizeof(v->driver));
  175. strlcpy(v->card, "ActiveRadio", sizeof(v->card));
  176. strlcpy(v->bus_info, "ISA", sizeof(v->bus_info));
  177. v->version = RADIO_VERSION;
  178. v->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
  179. return 0;
  180. }
  181. static int vidioc_g_tuner(struct file *file, void *priv,
  182. struct v4l2_tuner *v)
  183. {
  184. struct terratec *tt = video_drvdata(file);
  185. if (v->index > 0)
  186. return -EINVAL;
  187. strlcpy(v->name, "FM", sizeof(v->name));
  188. v->type = V4L2_TUNER_RADIO;
  189. v->rangelow = 87 * 16000;
  190. v->rangehigh = 108 * 16000;
  191. v->rxsubchans = V4L2_TUNER_SUB_MONO;
  192. v->capability = V4L2_TUNER_CAP_LOW;
  193. v->audmode = V4L2_TUNER_MODE_MONO;
  194. v->signal = 0xFFFF * tt_getsigstr(tt);
  195. return 0;
  196. }
  197. static int vidioc_s_tuner(struct file *file, void *priv,
  198. struct v4l2_tuner *v)
  199. {
  200. return v->index ? -EINVAL : 0;
  201. }
  202. static int vidioc_s_frequency(struct file *file, void *priv,
  203. struct v4l2_frequency *f)
  204. {
  205. struct terratec *tt = video_drvdata(file);
  206. tt_setfreq(tt, f->frequency);
  207. return 0;
  208. }
  209. static int vidioc_g_frequency(struct file *file, void *priv,
  210. struct v4l2_frequency *f)
  211. {
  212. struct terratec *tt = video_drvdata(file);
  213. f->type = V4L2_TUNER_RADIO;
  214. f->frequency = tt->curfreq;
  215. return 0;
  216. }
  217. static int vidioc_queryctrl(struct file *file, void *priv,
  218. struct v4l2_queryctrl *qc)
  219. {
  220. int i;
  221. for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
  222. if (qc->id && qc->id == radio_qctrl[i].id) {
  223. memcpy(qc, &(radio_qctrl[i]), sizeof(*qc));
  224. return 0;
  225. }
  226. }
  227. return -EINVAL;
  228. }
  229. static int vidioc_g_ctrl(struct file *file, void *priv,
  230. struct v4l2_control *ctrl)
  231. {
  232. struct terratec *tt = video_drvdata(file);
  233. switch (ctrl->id) {
  234. case V4L2_CID_AUDIO_MUTE:
  235. if (tt->muted)
  236. ctrl->value = 1;
  237. else
  238. ctrl->value = 0;
  239. return 0;
  240. case V4L2_CID_AUDIO_VOLUME:
  241. ctrl->value = tt->curvol * 6554;
  242. return 0;
  243. }
  244. return -EINVAL;
  245. }
  246. static int vidioc_s_ctrl(struct file *file, void *priv,
  247. struct v4l2_control *ctrl)
  248. {
  249. struct terratec *tt = video_drvdata(file);
  250. switch (ctrl->id) {
  251. case V4L2_CID_AUDIO_MUTE:
  252. if (ctrl->value)
  253. tt_mute(tt);
  254. else
  255. tt_setvol(tt,tt->curvol);
  256. return 0;
  257. case V4L2_CID_AUDIO_VOLUME:
  258. tt_setvol(tt,ctrl->value);
  259. return 0;
  260. }
  261. return -EINVAL;
  262. }
  263. static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
  264. {
  265. *i = 0;
  266. return 0;
  267. }
  268. static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
  269. {
  270. return i ? -EINVAL : 0;
  271. }
  272. static int vidioc_g_audio(struct file *file, void *priv,
  273. struct v4l2_audio *a)
  274. {
  275. a->index = 0;
  276. strlcpy(a->name, "Radio", sizeof(a->name));
  277. a->capability = V4L2_AUDCAP_STEREO;
  278. return 0;
  279. }
  280. static int vidioc_s_audio(struct file *file, void *priv,
  281. struct v4l2_audio *a)
  282. {
  283. return a->index ? -EINVAL : 0;
  284. }
  285. static const struct v4l2_file_operations terratec_fops = {
  286. .owner = THIS_MODULE,
  287. .ioctl = video_ioctl2,
  288. };
  289. static const struct v4l2_ioctl_ops terratec_ioctl_ops = {
  290. .vidioc_querycap = vidioc_querycap,
  291. .vidioc_g_tuner = vidioc_g_tuner,
  292. .vidioc_s_tuner = vidioc_s_tuner,
  293. .vidioc_g_frequency = vidioc_g_frequency,
  294. .vidioc_s_frequency = vidioc_s_frequency,
  295. .vidioc_queryctrl = vidioc_queryctrl,
  296. .vidioc_g_ctrl = vidioc_g_ctrl,
  297. .vidioc_s_ctrl = vidioc_s_ctrl,
  298. .vidioc_g_audio = vidioc_g_audio,
  299. .vidioc_s_audio = vidioc_s_audio,
  300. .vidioc_g_input = vidioc_g_input,
  301. .vidioc_s_input = vidioc_s_input,
  302. };
  303. static int __init terratec_init(void)
  304. {
  305. struct terratec *tt = &terratec_card;
  306. struct v4l2_device *v4l2_dev = &tt->v4l2_dev;
  307. int res;
  308. strlcpy(v4l2_dev->name, "terratec", sizeof(v4l2_dev->name));
  309. tt->io = io;
  310. if (tt->io == -1) {
  311. v4l2_err(v4l2_dev, "you must set an I/O address with io=0x590 or 0x591\n");
  312. return -EINVAL;
  313. }
  314. if (!request_region(tt->io, 2, "terratec")) {
  315. v4l2_err(v4l2_dev, "port 0x%x already in use\n", io);
  316. return -EBUSY;
  317. }
  318. res = v4l2_device_register(NULL, v4l2_dev);
  319. if (res < 0) {
  320. release_region(tt->io, 2);
  321. v4l2_err(v4l2_dev, "Could not register v4l2_device\n");
  322. return res;
  323. }
  324. strlcpy(tt->vdev.name, v4l2_dev->name, sizeof(tt->vdev.name));
  325. tt->vdev.v4l2_dev = v4l2_dev;
  326. tt->vdev.fops = &terratec_fops;
  327. tt->vdev.ioctl_ops = &terratec_ioctl_ops;
  328. tt->vdev.release = video_device_release_empty;
  329. video_set_drvdata(&tt->vdev, tt);
  330. mutex_init(&tt->lock);
  331. if (video_register_device(&tt->vdev, VFL_TYPE_RADIO, radio_nr) < 0) {
  332. v4l2_device_unregister(&tt->v4l2_dev);
  333. release_region(tt->io, 2);
  334. return -EINVAL;
  335. }
  336. v4l2_info(v4l2_dev, "TERRATEC ActivRadio Standalone card driver.\n");
  337. /* mute card - prevents noisy bootups */
  338. tt_write_vol(tt, 0);
  339. return 0;
  340. }
  341. static void __exit terratec_exit(void)
  342. {
  343. struct terratec *tt = &terratec_card;
  344. struct v4l2_device *v4l2_dev = &tt->v4l2_dev;
  345. video_unregister_device(&tt->vdev);
  346. v4l2_device_unregister(&tt->v4l2_dev);
  347. release_region(tt->io, 2);
  348. v4l2_info(v4l2_dev, "TERRATEC ActivRadio Standalone card driver unloaded.\n");
  349. }
  350. module_init(terratec_init);
  351. module_exit(terratec_exit);