radio-terratec.c 9.8 KB

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