radio-terratec.c 10 KB

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