radio-terratec.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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. if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
  207. return -EINVAL;
  208. tt_setfreq(tt, 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 terratec *tt = video_drvdata(file);
  215. if (f->tuner != 0)
  216. return -EINVAL;
  217. f->type = V4L2_TUNER_RADIO;
  218. f->frequency = tt->curfreq;
  219. return 0;
  220. }
  221. static int vidioc_queryctrl(struct file *file, void *priv,
  222. struct v4l2_queryctrl *qc)
  223. {
  224. int i;
  225. for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
  226. if (qc->id && qc->id == radio_qctrl[i].id) {
  227. memcpy(qc, &(radio_qctrl[i]), 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. struct terratec *tt = video_drvdata(file);
  237. switch (ctrl->id) {
  238. case V4L2_CID_AUDIO_MUTE:
  239. if (tt->muted)
  240. ctrl->value = 1;
  241. else
  242. ctrl->value = 0;
  243. return 0;
  244. case V4L2_CID_AUDIO_VOLUME:
  245. ctrl->value = tt->curvol * 6554;
  246. return 0;
  247. }
  248. return -EINVAL;
  249. }
  250. static int vidioc_s_ctrl(struct file *file, void *priv,
  251. struct v4l2_control *ctrl)
  252. {
  253. struct terratec *tt = video_drvdata(file);
  254. switch (ctrl->id) {
  255. case V4L2_CID_AUDIO_MUTE:
  256. if (ctrl->value)
  257. tt_mute(tt);
  258. else
  259. tt_setvol(tt,tt->curvol);
  260. return 0;
  261. case V4L2_CID_AUDIO_VOLUME:
  262. tt_setvol(tt,ctrl->value);
  263. return 0;
  264. }
  265. return -EINVAL;
  266. }
  267. static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
  268. {
  269. *i = 0;
  270. return 0;
  271. }
  272. static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
  273. {
  274. return i ? -EINVAL : 0;
  275. }
  276. static int vidioc_g_audio(struct file *file, void *priv,
  277. struct v4l2_audio *a)
  278. {
  279. a->index = 0;
  280. strlcpy(a->name, "Radio", sizeof(a->name));
  281. a->capability = V4L2_AUDCAP_STEREO;
  282. return 0;
  283. }
  284. static int vidioc_s_audio(struct file *file, void *priv,
  285. struct v4l2_audio *a)
  286. {
  287. return a->index ? -EINVAL : 0;
  288. }
  289. static const struct v4l2_file_operations terratec_fops = {
  290. .owner = THIS_MODULE,
  291. .unlocked_ioctl = video_ioctl2,
  292. };
  293. static const struct v4l2_ioctl_ops terratec_ioctl_ops = {
  294. .vidioc_querycap = vidioc_querycap,
  295. .vidioc_g_tuner = vidioc_g_tuner,
  296. .vidioc_s_tuner = vidioc_s_tuner,
  297. .vidioc_g_frequency = vidioc_g_frequency,
  298. .vidioc_s_frequency = vidioc_s_frequency,
  299. .vidioc_queryctrl = vidioc_queryctrl,
  300. .vidioc_g_ctrl = vidioc_g_ctrl,
  301. .vidioc_s_ctrl = vidioc_s_ctrl,
  302. .vidioc_g_audio = vidioc_g_audio,
  303. .vidioc_s_audio = vidioc_s_audio,
  304. .vidioc_g_input = vidioc_g_input,
  305. .vidioc_s_input = vidioc_s_input,
  306. };
  307. static int __init terratec_init(void)
  308. {
  309. struct terratec *tt = &terratec_card;
  310. struct v4l2_device *v4l2_dev = &tt->v4l2_dev;
  311. int res;
  312. strlcpy(v4l2_dev->name, "terratec", sizeof(v4l2_dev->name));
  313. tt->io = io;
  314. if (tt->io == -1) {
  315. v4l2_err(v4l2_dev, "you must set an I/O address with io=0x590 or 0x591\n");
  316. return -EINVAL;
  317. }
  318. if (!request_region(tt->io, 2, "terratec")) {
  319. v4l2_err(v4l2_dev, "port 0x%x already in use\n", io);
  320. return -EBUSY;
  321. }
  322. res = v4l2_device_register(NULL, v4l2_dev);
  323. if (res < 0) {
  324. release_region(tt->io, 2);
  325. v4l2_err(v4l2_dev, "Could not register v4l2_device\n");
  326. return res;
  327. }
  328. strlcpy(tt->vdev.name, v4l2_dev->name, sizeof(tt->vdev.name));
  329. tt->vdev.v4l2_dev = v4l2_dev;
  330. tt->vdev.fops = &terratec_fops;
  331. tt->vdev.ioctl_ops = &terratec_ioctl_ops;
  332. tt->vdev.release = video_device_release_empty;
  333. video_set_drvdata(&tt->vdev, tt);
  334. mutex_init(&tt->lock);
  335. /* mute card - prevents noisy bootups */
  336. tt_write_vol(tt, 0);
  337. if (video_register_device(&tt->vdev, VFL_TYPE_RADIO, radio_nr) < 0) {
  338. v4l2_device_unregister(&tt->v4l2_dev);
  339. release_region(tt->io, 2);
  340. return -EINVAL;
  341. }
  342. v4l2_info(v4l2_dev, "TERRATEC ActivRadio Standalone card driver.\n");
  343. return 0;
  344. }
  345. static void __exit terratec_exit(void)
  346. {
  347. struct terratec *tt = &terratec_card;
  348. struct v4l2_device *v4l2_dev = &tt->v4l2_dev;
  349. video_unregister_device(&tt->vdev);
  350. v4l2_device_unregister(&tt->v4l2_dev);
  351. release_region(tt->io, 2);
  352. v4l2_info(v4l2_dev, "TERRATEC ActivRadio Standalone card driver unloaded.\n");
  353. }
  354. module_init(terratec_init);
  355. module_exit(terratec_exit);