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