radio-terratec.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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 tt_device *tt = video_drvdata(file);
  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 tt_device *tt = video_drvdata(file);
  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 tt_device *tt = video_drvdata(file);
  221. f->type = V4L2_TUNER_RADIO;
  222. f->frequency = tt->curfreq;
  223. return 0;
  224. }
  225. static int vidioc_queryctrl(struct file *file, void *priv,
  226. struct v4l2_queryctrl *qc)
  227. {
  228. int i;
  229. for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
  230. if (qc->id && qc->id == radio_qctrl[i].id) {
  231. memcpy(qc, &(radio_qctrl[i]),
  232. sizeof(*qc));
  233. return 0;
  234. }
  235. }
  236. return -EINVAL;
  237. }
  238. static int vidioc_g_ctrl(struct file *file, void *priv,
  239. struct v4l2_control *ctrl)
  240. {
  241. struct tt_device *tt = video_drvdata(file);
  242. switch (ctrl->id) {
  243. case V4L2_CID_AUDIO_MUTE:
  244. if (tt->muted)
  245. ctrl->value = 1;
  246. else
  247. ctrl->value = 0;
  248. return 0;
  249. case V4L2_CID_AUDIO_VOLUME:
  250. ctrl->value = tt->curvol * 6554;
  251. return 0;
  252. }
  253. return -EINVAL;
  254. }
  255. static int vidioc_s_ctrl(struct file *file, void *priv,
  256. struct v4l2_control *ctrl)
  257. {
  258. struct tt_device *tt = video_drvdata(file);
  259. switch (ctrl->id) {
  260. case V4L2_CID_AUDIO_MUTE:
  261. if (ctrl->value)
  262. tt_mute(tt);
  263. else
  264. tt_setvol(tt,tt->curvol);
  265. return 0;
  266. case V4L2_CID_AUDIO_VOLUME:
  267. tt_setvol(tt,ctrl->value);
  268. return 0;
  269. }
  270. return -EINVAL;
  271. }
  272. static int vidioc_g_audio(struct file *file, void *priv,
  273. struct v4l2_audio *a)
  274. {
  275. if (a->index > 1)
  276. return -EINVAL;
  277. strcpy(a->name, "Radio");
  278. a->capability = V4L2_AUDCAP_STEREO;
  279. return 0;
  280. }
  281. static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
  282. {
  283. *i = 0;
  284. return 0;
  285. }
  286. static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
  287. {
  288. if (i != 0)
  289. return -EINVAL;
  290. return 0;
  291. }
  292. static int vidioc_s_audio(struct file *file, void *priv,
  293. struct v4l2_audio *a)
  294. {
  295. if (a->index != 0)
  296. return -EINVAL;
  297. return 0;
  298. }
  299. static struct tt_device terratec_unit;
  300. static int terratec_exclusive_open(struct file *file)
  301. {
  302. return test_and_set_bit(0, &terratec_unit.in_use) ? -EBUSY : 0;
  303. }
  304. static int terratec_exclusive_release(struct file *file)
  305. {
  306. clear_bit(0, &terratec_unit.in_use);
  307. return 0;
  308. }
  309. static const struct v4l2_file_operations terratec_fops = {
  310. .owner = THIS_MODULE,
  311. .open = terratec_exclusive_open,
  312. .release = terratec_exclusive_release,
  313. .ioctl = video_ioctl2,
  314. };
  315. static const struct v4l2_ioctl_ops terratec_ioctl_ops = {
  316. .vidioc_querycap = vidioc_querycap,
  317. .vidioc_g_tuner = vidioc_g_tuner,
  318. .vidioc_s_tuner = vidioc_s_tuner,
  319. .vidioc_g_frequency = vidioc_g_frequency,
  320. .vidioc_s_frequency = vidioc_s_frequency,
  321. .vidioc_queryctrl = vidioc_queryctrl,
  322. .vidioc_g_ctrl = vidioc_g_ctrl,
  323. .vidioc_s_ctrl = vidioc_s_ctrl,
  324. .vidioc_g_audio = vidioc_g_audio,
  325. .vidioc_s_audio = vidioc_s_audio,
  326. .vidioc_g_input = vidioc_g_input,
  327. .vidioc_s_input = vidioc_s_input,
  328. };
  329. static struct video_device terratec_radio = {
  330. .name = "TerraTec ActiveRadio",
  331. .fops = &terratec_fops,
  332. .ioctl_ops = &terratec_ioctl_ops,
  333. .release = video_device_release_empty,
  334. };
  335. static int __init terratec_init(void)
  336. {
  337. if(io==-1)
  338. {
  339. printk(KERN_ERR "You must set an I/O address with io=0x???\n");
  340. return -EINVAL;
  341. }
  342. if (!request_region(io, 2, "terratec"))
  343. {
  344. printk(KERN_ERR "TerraTec: port 0x%x already in use\n", io);
  345. return -EBUSY;
  346. }
  347. video_set_drvdata(&terratec_radio, &terratec_unit);
  348. spin_lock_init(&lock);
  349. if (video_register_device(&terratec_radio, VFL_TYPE_RADIO, radio_nr) < 0) {
  350. release_region(io,2);
  351. return -EINVAL;
  352. }
  353. printk(KERN_INFO "TERRATEC ActivRadio Standalone card driver.\n");
  354. /* mute card - prevents noisy bootups */
  355. /* this ensures that the volume is all the way down */
  356. cardWriteVol(0);
  357. terratec_unit.curvol = 0;
  358. return 0;
  359. }
  360. MODULE_AUTHOR("R.OFFERMANNS & others");
  361. MODULE_DESCRIPTION("A driver for the TerraTec ActiveRadio Standalone radio card.");
  362. MODULE_LICENSE("GPL");
  363. module_param(io, int, 0);
  364. MODULE_PARM_DESC(io, "I/O address of the TerraTec ActiveRadio card (0x590 or 0x591)");
  365. module_param(radio_nr, int, 0);
  366. static void __exit terratec_cleanup_module(void)
  367. {
  368. video_unregister_device(&terratec_radio);
  369. release_region(io,2);
  370. printk(KERN_INFO "TERRATEC ActivRadio Standalone card driver unloaded.\n");
  371. }
  372. module_init(terratec_init);
  373. module_exit(terratec_cleanup_module);