radio-sf16fmr2.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /* SF16-FMR2 radio driver for Linux
  2. * Copyright (c) 2011 Ondrej Zary
  3. *
  4. * Original driver was (c) 2000-2002 Ziglio Frediano, freddy77@angelfire.com
  5. * but almost nothing remained here after conversion to generic TEA575x
  6. * implementation
  7. */
  8. #include <linux/delay.h>
  9. #include <linux/module.h> /* Modules */
  10. #include <linux/init.h> /* Initdata */
  11. #include <linux/slab.h>
  12. #include <linux/ioport.h> /* request_region */
  13. #include <linux/io.h> /* outb, outb_p */
  14. #include <linux/isa.h>
  15. #include <sound/tea575x-tuner.h>
  16. MODULE_AUTHOR("Ondrej Zary");
  17. MODULE_DESCRIPTION("MediaForte SF16-FMR2 FM radio card driver");
  18. MODULE_LICENSE("GPL");
  19. static int radio_nr = -1;
  20. module_param(radio_nr, int, 0444);
  21. MODULE_PARM_DESC(radio_nr, "Radio device number");
  22. struct fmr2 {
  23. int io;
  24. struct v4l2_device v4l2_dev;
  25. struct snd_tea575x tea;
  26. struct v4l2_ctrl *volume;
  27. struct v4l2_ctrl *balance;
  28. };
  29. /* the port is hardwired so no need to support multiple cards */
  30. #define FMR2_PORT 0x384
  31. /* TEA575x tuner pins */
  32. #define STR_DATA (1 << 0)
  33. #define STR_CLK (1 << 1)
  34. #define STR_WREN (1 << 2)
  35. #define STR_MOST (1 << 3)
  36. /* PT2254A/TC9154A volume control pins */
  37. #define PT_ST (1 << 4)
  38. #define PT_CK (1 << 5)
  39. #define PT_DATA (1 << 6)
  40. /* volume control presence pin */
  41. #define FMR2_HASVOL (1 << 7)
  42. static void fmr2_tea575x_set_pins(struct snd_tea575x *tea, u8 pins)
  43. {
  44. struct fmr2 *fmr2 = tea->private_data;
  45. u8 bits = 0;
  46. bits |= (pins & TEA575X_DATA) ? STR_DATA : 0;
  47. bits |= (pins & TEA575X_CLK) ? STR_CLK : 0;
  48. /* WRITE_ENABLE is inverted, DATA must be high during read */
  49. bits |= (pins & TEA575X_WREN) ? 0 : STR_WREN | STR_DATA;
  50. outb(bits, fmr2->io);
  51. }
  52. static u8 fmr2_tea575x_get_pins(struct snd_tea575x *tea)
  53. {
  54. struct fmr2 *fmr2 = tea->private_data;
  55. u8 bits = inb(fmr2->io);
  56. return (bits & STR_DATA) ? TEA575X_DATA : 0 |
  57. (bits & STR_MOST) ? TEA575X_MOST : 0;
  58. }
  59. static void fmr2_tea575x_set_direction(struct snd_tea575x *tea, bool output)
  60. {
  61. }
  62. static struct snd_tea575x_ops fmr2_tea_ops = {
  63. .set_pins = fmr2_tea575x_set_pins,
  64. .get_pins = fmr2_tea575x_get_pins,
  65. .set_direction = fmr2_tea575x_set_direction,
  66. };
  67. /* TC9154A/PT2254A volume control */
  68. /* 18-bit shift register bit definitions */
  69. #define TC9154A_ATT_MAJ_0DB (1 << 0)
  70. #define TC9154A_ATT_MAJ_10DB (1 << 1)
  71. #define TC9154A_ATT_MAJ_20DB (1 << 2)
  72. #define TC9154A_ATT_MAJ_30DB (1 << 3)
  73. #define TC9154A_ATT_MAJ_40DB (1 << 4)
  74. #define TC9154A_ATT_MAJ_50DB (1 << 5)
  75. #define TC9154A_ATT_MAJ_60DB (1 << 6)
  76. #define TC9154A_ATT_MIN_0DB (1 << 7)
  77. #define TC9154A_ATT_MIN_2DB (1 << 8)
  78. #define TC9154A_ATT_MIN_4DB (1 << 9)
  79. #define TC9154A_ATT_MIN_6DB (1 << 10)
  80. #define TC9154A_ATT_MIN_8DB (1 << 11)
  81. /* bit 12 is ignored */
  82. #define TC9154A_CHANNEL_LEFT (1 << 13)
  83. #define TC9154A_CHANNEL_RIGHT (1 << 14)
  84. /* bits 15, 16, 17 must be 0 */
  85. #define TC9154A_ATT_MAJ(x) (1 << x)
  86. #define TC9154A_ATT_MIN(x) (1 << (7 + x))
  87. static void tc9154a_set_pins(struct fmr2 *fmr2, u8 pins)
  88. {
  89. if (!fmr2->tea.mute)
  90. pins |= STR_WREN;
  91. outb(pins, fmr2->io);
  92. }
  93. static void tc9154a_set_attenuation(struct fmr2 *fmr2, int att, u32 channel)
  94. {
  95. int i;
  96. u32 reg;
  97. u8 bit;
  98. reg = TC9154A_ATT_MAJ(att / 10) | TC9154A_ATT_MIN((att % 10) / 2);
  99. reg |= channel;
  100. /* write 18-bit shift register, LSB first */
  101. for (i = 0; i < 18; i++) {
  102. bit = reg & (1 << i) ? PT_DATA : 0;
  103. tc9154a_set_pins(fmr2, bit);
  104. udelay(5);
  105. tc9154a_set_pins(fmr2, bit | PT_CK);
  106. udelay(5);
  107. tc9154a_set_pins(fmr2, bit);
  108. }
  109. /* latch register data */
  110. udelay(5);
  111. tc9154a_set_pins(fmr2, PT_ST);
  112. udelay(5);
  113. tc9154a_set_pins(fmr2, 0);
  114. }
  115. static int fmr2_s_ctrl(struct v4l2_ctrl *ctrl)
  116. {
  117. struct snd_tea575x *tea = container_of(ctrl->handler, struct snd_tea575x, ctrl_handler);
  118. struct fmr2 *fmr2 = tea->private_data;
  119. int volume, balance, left, right;
  120. switch (ctrl->id) {
  121. case V4L2_CID_AUDIO_VOLUME:
  122. volume = ctrl->val;
  123. balance = fmr2->balance->cur.val;
  124. break;
  125. case V4L2_CID_AUDIO_BALANCE:
  126. balance = ctrl->val;
  127. volume = fmr2->volume->cur.val;
  128. break;
  129. default:
  130. return -EINVAL;
  131. }
  132. left = right = volume;
  133. if (balance < 0)
  134. right = max(0, right + balance);
  135. if (balance > 0)
  136. left = max(0, left - balance);
  137. tc9154a_set_attenuation(fmr2, abs(left - 68), TC9154A_CHANNEL_LEFT);
  138. tc9154a_set_attenuation(fmr2, abs(right - 68), TC9154A_CHANNEL_RIGHT);
  139. return 0;
  140. }
  141. static const struct v4l2_ctrl_ops fmr2_ctrl_ops = {
  142. .s_ctrl = fmr2_s_ctrl,
  143. };
  144. static int fmr2_tea_ext_init(struct snd_tea575x *tea)
  145. {
  146. struct fmr2 *fmr2 = tea->private_data;
  147. if (inb(fmr2->io) & FMR2_HASVOL) {
  148. fmr2->volume = v4l2_ctrl_new_std(&tea->ctrl_handler, &fmr2_ctrl_ops, V4L2_CID_AUDIO_VOLUME, 0, 68, 2, 56);
  149. fmr2->balance = v4l2_ctrl_new_std(&tea->ctrl_handler, &fmr2_ctrl_ops, V4L2_CID_AUDIO_BALANCE, -68, 68, 2, 0);
  150. if (tea->ctrl_handler.error) {
  151. printk(KERN_ERR "radio-sf16fmr2: can't initialize controls\n");
  152. return tea->ctrl_handler.error;
  153. }
  154. }
  155. return 0;
  156. }
  157. static int __devinit fmr2_probe(struct device *pdev, unsigned int dev)
  158. {
  159. struct fmr2 *fmr2;
  160. int err;
  161. fmr2 = kzalloc(sizeof(*fmr2), GFP_KERNEL);
  162. if (fmr2 == NULL)
  163. return -ENOMEM;
  164. strlcpy(fmr2->v4l2_dev.name, dev_name(pdev),
  165. sizeof(fmr2->v4l2_dev.name));
  166. fmr2->io = FMR2_PORT;
  167. if (!request_region(fmr2->io, 2, fmr2->v4l2_dev.name)) {
  168. printk(KERN_ERR "radio-sf16fmr2: I/O port 0x%x already in use\n", fmr2->io);
  169. kfree(fmr2);
  170. return -EBUSY;
  171. }
  172. dev_set_drvdata(pdev, fmr2);
  173. err = v4l2_device_register(pdev, &fmr2->v4l2_dev);
  174. if (err < 0) {
  175. v4l2_err(&fmr2->v4l2_dev, "Could not register v4l2_device\n");
  176. release_region(fmr2->io, 2);
  177. kfree(fmr2);
  178. return err;
  179. }
  180. fmr2->tea.v4l2_dev = &fmr2->v4l2_dev;
  181. fmr2->tea.private_data = fmr2;
  182. fmr2->tea.radio_nr = radio_nr;
  183. fmr2->tea.ops = &fmr2_tea_ops;
  184. fmr2->tea.ext_init = fmr2_tea_ext_init;
  185. strlcpy(fmr2->tea.card, "SF16-FMR2", sizeof(fmr2->tea.card));
  186. snprintf(fmr2->tea.bus_info, sizeof(fmr2->tea.bus_info), "ISA:%s",
  187. fmr2->v4l2_dev.name);
  188. if (snd_tea575x_init(&fmr2->tea)) {
  189. printk(KERN_ERR "radio-sf16fmr2: Unable to detect TEA575x tuner\n");
  190. release_region(fmr2->io, 2);
  191. kfree(fmr2);
  192. return -ENODEV;
  193. }
  194. printk(KERN_INFO "radio-sf16fmr2: SF16-FMR2 radio card at 0x%x.\n", fmr2->io);
  195. return 0;
  196. }
  197. static int __exit fmr2_remove(struct device *pdev, unsigned int dev)
  198. {
  199. struct fmr2 *fmr2 = dev_get_drvdata(pdev);
  200. snd_tea575x_exit(&fmr2->tea);
  201. release_region(fmr2->io, 2);
  202. v4l2_device_unregister(&fmr2->v4l2_dev);
  203. kfree(fmr2);
  204. return 0;
  205. }
  206. struct isa_driver fmr2_driver = {
  207. .probe = fmr2_probe,
  208. .remove = fmr2_remove,
  209. .driver = {
  210. .name = "radio-sf16fmr2",
  211. },
  212. };
  213. static int __init fmr2_init(void)
  214. {
  215. return isa_register_driver(&fmr2_driver, 1);
  216. }
  217. static void __exit fmr2_exit(void)
  218. {
  219. isa_unregister_driver(&fmr2_driver);
  220. }
  221. module_init(fmr2_init);
  222. module_exit(fmr2_exit);