radio-terratec.c 10 KB

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