radio-trust.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /* radio-trust.c - Trust FM Radio card driver for Linux 2.2
  2. * by Eric Lammerts <eric@scintilla.utwente.nl>
  3. *
  4. * Based on radio-aztech.c. Original notes:
  5. *
  6. * Adapted to support the Video for Linux API by
  7. * Russell Kroll <rkroll@exploits.org>. Based on original tuner code by:
  8. *
  9. * Quay Ly
  10. * Donald Song
  11. * Jason Lewis (jlewis@twilight.vtc.vsc.edu)
  12. * Scott McGrath (smcgrath@twilight.vtc.vsc.edu)
  13. * William McGrath (wmcgrath@twilight.vtc.vsc.edu)
  14. *
  15. * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
  16. */
  17. #include <stdarg.h>
  18. #include <linux/module.h>
  19. #include <linux/init.h>
  20. #include <linux/ioport.h>
  21. #include <linux/videodev2.h>
  22. #include <linux/io.h>
  23. #include <media/v4l2-device.h>
  24. #include <media/v4l2-ioctl.h>
  25. #include "radio-isa.h"
  26. MODULE_AUTHOR("Eric Lammerts, Russell Kroll, Quay Lu, Donald Song, Jason Lewis, Scott McGrath, William McGrath");
  27. MODULE_DESCRIPTION("A driver for the Trust FM Radio card.");
  28. MODULE_LICENSE("GPL");
  29. MODULE_VERSION("0.1.99");
  30. /* acceptable ports: 0x350 (JP3 shorted), 0x358 (JP3 open) */
  31. #ifndef CONFIG_RADIO_TRUST_PORT
  32. #define CONFIG_RADIO_TRUST_PORT -1
  33. #endif
  34. #define TRUST_MAX 2
  35. static int io[TRUST_MAX] = { [0] = CONFIG_RADIO_TRUST_PORT,
  36. [1 ... (TRUST_MAX - 1)] = -1 };
  37. static int radio_nr[TRUST_MAX] = { [0 ... (TRUST_MAX - 1)] = -1 };
  38. module_param_array(io, int, NULL, 0444);
  39. MODULE_PARM_DESC(io, "I/O addresses of the Trust FM Radio card (0x350 or 0x358)");
  40. module_param_array(radio_nr, int, NULL, 0444);
  41. MODULE_PARM_DESC(radio_nr, "Radio device numbers");
  42. struct trust {
  43. struct radio_isa_card isa;
  44. int ioval;
  45. };
  46. static struct radio_isa_card *trust_alloc(void)
  47. {
  48. struct trust *tr = kzalloc(sizeof(*tr), GFP_KERNEL);
  49. return tr ? &tr->isa : NULL;
  50. }
  51. /* i2c addresses */
  52. #define TDA7318_ADDR 0x88
  53. #define TSA6060T_ADDR 0xc4
  54. #define TR_DELAY do { inb(tr->isa.io); inb(tr->isa.io); inb(tr->isa.io); } while (0)
  55. #define TR_SET_SCL outb(tr->ioval |= 2, tr->isa.io)
  56. #define TR_CLR_SCL outb(tr->ioval &= 0xfd, tr->isa.io)
  57. #define TR_SET_SDA outb(tr->ioval |= 1, tr->isa.io)
  58. #define TR_CLR_SDA outb(tr->ioval &= 0xfe, tr->isa.io)
  59. static void write_i2c(struct trust *tr, int n, ...)
  60. {
  61. unsigned char val, mask;
  62. va_list args;
  63. va_start(args, n);
  64. /* start condition */
  65. TR_SET_SDA;
  66. TR_SET_SCL;
  67. TR_DELAY;
  68. TR_CLR_SDA;
  69. TR_CLR_SCL;
  70. TR_DELAY;
  71. for (; n; n--) {
  72. val = va_arg(args, unsigned);
  73. for (mask = 0x80; mask; mask >>= 1) {
  74. if (val & mask)
  75. TR_SET_SDA;
  76. else
  77. TR_CLR_SDA;
  78. TR_SET_SCL;
  79. TR_DELAY;
  80. TR_CLR_SCL;
  81. TR_DELAY;
  82. }
  83. /* acknowledge bit */
  84. TR_SET_SDA;
  85. TR_SET_SCL;
  86. TR_DELAY;
  87. TR_CLR_SCL;
  88. TR_DELAY;
  89. }
  90. /* stop condition */
  91. TR_CLR_SDA;
  92. TR_DELAY;
  93. TR_SET_SCL;
  94. TR_DELAY;
  95. TR_SET_SDA;
  96. TR_DELAY;
  97. va_end(args);
  98. }
  99. static int trust_s_mute_volume(struct radio_isa_card *isa, bool mute, int vol)
  100. {
  101. struct trust *tr = container_of(isa, struct trust, isa);
  102. tr->ioval = (tr->ioval & 0xf7) | (mute << 3);
  103. outb(tr->ioval, isa->io);
  104. write_i2c(tr, 2, TDA7318_ADDR, vol ^ 0x1f);
  105. return 0;
  106. }
  107. static int trust_s_stereo(struct radio_isa_card *isa, bool stereo)
  108. {
  109. struct trust *tr = container_of(isa, struct trust, isa);
  110. tr->ioval = (tr->ioval & 0xfb) | (!stereo << 2);
  111. outb(tr->ioval, isa->io);
  112. return 0;
  113. }
  114. static u32 trust_g_signal(struct radio_isa_card *isa)
  115. {
  116. int i, v;
  117. for (i = 0, v = 0; i < 100; i++)
  118. v |= inb(isa->io);
  119. return (v & 1) ? 0 : 0xffff;
  120. }
  121. static int trust_s_frequency(struct radio_isa_card *isa, u32 freq)
  122. {
  123. struct trust *tr = container_of(isa, struct trust, isa);
  124. freq /= 160; /* Convert to 10 kHz units */
  125. freq += 1070; /* Add 10.7 MHz IF */
  126. write_i2c(tr, 5, TSA6060T_ADDR, (freq << 1) | 1,
  127. freq >> 7, 0x60 | ((freq >> 15) & 1), 0);
  128. return 0;
  129. }
  130. static int basstreble2chip[15] = {
  131. 0, 1, 2, 3, 4, 5, 6, 7, 14, 13, 12, 11, 10, 9, 8
  132. };
  133. static int trust_s_ctrl(struct v4l2_ctrl *ctrl)
  134. {
  135. struct radio_isa_card *isa =
  136. container_of(ctrl->handler, struct radio_isa_card, hdl);
  137. struct trust *tr = container_of(isa, struct trust, isa);
  138. switch (ctrl->id) {
  139. case V4L2_CID_AUDIO_BASS:
  140. write_i2c(tr, 2, TDA7318_ADDR, 0x60 | basstreble2chip[ctrl->val]);
  141. return 0;
  142. case V4L2_CID_AUDIO_TREBLE:
  143. write_i2c(tr, 2, TDA7318_ADDR, 0x70 | basstreble2chip[ctrl->val]);
  144. return 0;
  145. }
  146. return -EINVAL;
  147. }
  148. static const struct v4l2_ctrl_ops trust_ctrl_ops = {
  149. .s_ctrl = trust_s_ctrl,
  150. };
  151. static int trust_initialize(struct radio_isa_card *isa)
  152. {
  153. struct trust *tr = container_of(isa, struct trust, isa);
  154. tr->ioval = 0xf;
  155. write_i2c(tr, 2, TDA7318_ADDR, 0x80); /* speaker att. LF = 0 dB */
  156. write_i2c(tr, 2, TDA7318_ADDR, 0xa0); /* speaker att. RF = 0 dB */
  157. write_i2c(tr, 2, TDA7318_ADDR, 0xc0); /* speaker att. LR = 0 dB */
  158. write_i2c(tr, 2, TDA7318_ADDR, 0xe0); /* speaker att. RR = 0 dB */
  159. write_i2c(tr, 2, TDA7318_ADDR, 0x40); /* stereo 1 input, gain = 18.75 dB */
  160. v4l2_ctrl_new_std(&isa->hdl, &trust_ctrl_ops,
  161. V4L2_CID_AUDIO_BASS, 0, 15, 1, 8);
  162. v4l2_ctrl_new_std(&isa->hdl, &trust_ctrl_ops,
  163. V4L2_CID_AUDIO_TREBLE, 0, 15, 1, 8);
  164. return isa->hdl.error;
  165. }
  166. static const struct radio_isa_ops trust_ops = {
  167. .init = trust_initialize,
  168. .alloc = trust_alloc,
  169. .s_mute_volume = trust_s_mute_volume,
  170. .s_frequency = trust_s_frequency,
  171. .s_stereo = trust_s_stereo,
  172. .g_signal = trust_g_signal,
  173. };
  174. static const int trust_ioports[] = { 0x350, 0x358 };
  175. static struct radio_isa_driver trust_driver = {
  176. .driver = {
  177. .match = radio_isa_match,
  178. .probe = radio_isa_probe,
  179. .remove = radio_isa_remove,
  180. .driver = {
  181. .name = "radio-trust",
  182. },
  183. },
  184. .io_params = io,
  185. .radio_nr_params = radio_nr,
  186. .io_ports = trust_ioports,
  187. .num_of_io_ports = ARRAY_SIZE(trust_ioports),
  188. .region_size = 2,
  189. .card = "Trust FM Radio",
  190. .ops = &trust_ops,
  191. .has_stereo = true,
  192. .max_volume = 31,
  193. };
  194. static int __init trust_init(void)
  195. {
  196. return isa_register_driver(&trust_driver.driver, TRUST_MAX);
  197. }
  198. static void __exit trust_exit(void)
  199. {
  200. isa_unregister_driver(&trust_driver.driver);
  201. }
  202. module_init(trust_init);
  203. module_exit(trust_exit);