radio-terratec.c 9.8 KB

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