radio-trust.c 5.9 KB

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