radio-terratec.c 9.9 KB

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