Merge branch 'akpm' (Andrew's patch-bomb)
[firefly-linux-kernel-4.4.55.git] / drivers / media / radio / radio-aimslab.c
1 /*
2  * AimsLab RadioTrack (aka RadioVeveal) driver
3  *
4  * Copyright 1997 M. Kirkwood
5  *
6  * Converted to the radio-isa framework by Hans Verkuil <hans.verkuil@cisco.com>
7  * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
8  * Converted to new API by Alan Cox <alan@lxorguk.ukuu.org.uk>
9  * Various bugfixes and enhancements by Russell Kroll <rkroll@exploits.org>
10  *
11  * Notes on the hardware (reverse engineered from other peoples'
12  * reverse engineering of AIMS' code :-)
13  *
14  *  Frequency control is done digitally -- ie out(port,encodefreq(95.8));
15  *
16  *  The signal strength query is unsurprisingly inaccurate.  And it seems
17  *  to indicate that (on my card, at least) the frequency setting isn't
18  *  too great.  (I have to tune up .025MHz from what the freq should be
19  *  to get a report that the thing is tuned.)
20  *
21  *  Volume control is (ugh) analogue:
22  *   out(port, start_increasing_volume);
23  *   wait(a_wee_while);
24  *   out(port, stop_changing_the_volume);
25  *
26  * Fully tested with the Keene USB FM Transmitter and the v4l2-compliance tool.
27  */
28
29 #include <linux/module.h>       /* Modules                      */
30 #include <linux/init.h>         /* Initdata                     */
31 #include <linux/ioport.h>       /* request_region               */
32 #include <linux/delay.h>        /* msleep                       */
33 #include <linux/videodev2.h>    /* kernel radio structs         */
34 #include <linux/io.h>           /* outb, outb_p                 */
35 #include <linux/slab.h>
36 #include <media/v4l2-device.h>
37 #include <media/v4l2-ioctl.h>
38 #include <media/v4l2-ctrls.h>
39 #include "radio-isa.h"
40
41 MODULE_AUTHOR("M. Kirkwood");
42 MODULE_DESCRIPTION("A driver for the RadioTrack/RadioReveal radio card.");
43 MODULE_LICENSE("GPL");
44 MODULE_VERSION("1.0.0");
45
46 #ifndef CONFIG_RADIO_RTRACK_PORT
47 #define CONFIG_RADIO_RTRACK_PORT -1
48 #endif
49
50 #define RTRACK_MAX 2
51
52 static int io[RTRACK_MAX] = { [0] = CONFIG_RADIO_RTRACK_PORT,
53                               [1 ... (RTRACK_MAX - 1)] = -1 };
54 static int radio_nr[RTRACK_MAX] = { [0 ... (RTRACK_MAX - 1)] = -1 };
55
56 module_param_array(io, int, NULL, 0444);
57 MODULE_PARM_DESC(io, "I/O addresses of the RadioTrack card (0x20f or 0x30f)");
58 module_param_array(radio_nr, int, NULL, 0444);
59 MODULE_PARM_DESC(radio_nr, "Radio device numbers");
60
61 struct rtrack {
62         struct radio_isa_card isa;
63         int curvol;
64 };
65
66 static struct radio_isa_card *rtrack_alloc(void)
67 {
68         struct rtrack *rt = kzalloc(sizeof(struct rtrack), GFP_KERNEL);
69
70         if (rt)
71                 rt->curvol = 0xff;
72         return rt ? &rt->isa : NULL;
73 }
74
75 /* The 128+64 on these outb's is to keep the volume stable while tuning.
76  * Without them, the volume _will_ creep up with each frequency change
77  * and bit 4 (+16) is to keep the signal strength meter enabled.
78  */
79
80 static void send_0_byte(struct radio_isa_card *isa, int on)
81 {
82         outb_p(128+64+16+on+1, isa->io);        /* wr-enable + data low */
83         outb_p(128+64+16+on+2+1, isa->io);      /* clock */
84         msleep(1);
85 }
86
87 static void send_1_byte(struct radio_isa_card *isa, int on)
88 {
89         outb_p(128+64+16+on+4+1, isa->io);      /* wr-enable+data high */
90         outb_p(128+64+16+on+4+2+1, isa->io);    /* clock */
91         msleep(1);
92 }
93
94 static int rtrack_s_frequency(struct radio_isa_card *isa, u32 freq)
95 {
96         int on = v4l2_ctrl_g_ctrl(isa->mute) ? 0 : 8;
97         int i;
98
99         freq += 171200;                 /* Add 10.7 MHz IF              */
100         freq /= 800;                    /* Convert to 50 kHz units      */
101
102         send_0_byte(isa, on);           /*  0: LSB of frequency         */
103
104         for (i = 0; i < 13; i++)        /*   : frequency bits (1-13)    */
105                 if (freq & (1 << i))
106                         send_1_byte(isa, on);
107                 else
108                         send_0_byte(isa, on);
109
110         send_0_byte(isa, on);           /* 14: test bit - always 0    */
111         send_0_byte(isa, on);           /* 15: test bit - always 0    */
112
113         send_0_byte(isa, on);           /* 16: band data 0 - always 0 */
114         send_0_byte(isa, on);           /* 17: band data 1 - always 0 */
115         send_0_byte(isa, on);           /* 18: band data 2 - always 0 */
116         send_0_byte(isa, on);           /* 19: time base - always 0   */
117
118         send_0_byte(isa, on);           /* 20: spacing (0 = 25 kHz)   */
119         send_1_byte(isa, on);           /* 21: spacing (1 = 25 kHz)   */
120         send_0_byte(isa, on);           /* 22: spacing (0 = 25 kHz)   */
121         send_1_byte(isa, on);           /* 23: AM/FM (FM = 1, always) */
122
123         outb(0xd0 + on, isa->io);       /* volume steady + sigstr */
124         return 0;
125 }
126
127 static u32 rtrack_g_signal(struct radio_isa_card *isa)
128 {
129         /* bit set = no signal present */
130         return 0xffff * !(inb(isa->io) & 2);
131 }
132
133 static int rtrack_s_mute_volume(struct radio_isa_card *isa, bool mute, int vol)
134 {
135         struct rtrack *rt = container_of(isa, struct rtrack, isa);
136         int curvol = rt->curvol;
137
138         if (mute) {
139                 outb(0xd0, isa->io);    /* volume steady + sigstr + off */
140                 return 0;
141         }
142         if (vol == 0) {                 /* volume = 0 means mute the card */
143                 outb(0x48, isa->io);    /* volume down but still "on"   */
144                 msleep(curvol * 3);     /* make sure it's totally down  */
145         } else if (curvol < vol) {
146                 outb(0x98, isa->io);    /* volume up + sigstr + on      */
147                 for (; curvol < vol; curvol++)
148                         udelay(3000);
149         } else if (curvol > vol) {
150                 outb(0x58, isa->io);    /* volume down + sigstr + on    */
151                 for (; curvol > vol; curvol--)
152                         udelay(3000);
153         }
154         outb(0xd8, isa->io);            /* volume steady + sigstr + on  */
155         rt->curvol = vol;
156         return 0;
157 }
158
159 /* Mute card - prevents noisy bootups */
160 static int rtrack_initialize(struct radio_isa_card *isa)
161 {
162         /* this ensures that the volume is all the way up  */
163         outb(0x90, isa->io);    /* volume up but still "on"     */
164         msleep(3000);           /* make sure it's totally up    */
165         outb(0xc0, isa->io);    /* steady volume, mute card     */
166         return 0;
167 }
168
169 static const struct radio_isa_ops rtrack_ops = {
170         .alloc = rtrack_alloc,
171         .init = rtrack_initialize,
172         .s_mute_volume = rtrack_s_mute_volume,
173         .s_frequency = rtrack_s_frequency,
174         .g_signal = rtrack_g_signal,
175 };
176
177 static const int rtrack_ioports[] = { 0x20f, 0x30f };
178
179 static struct radio_isa_driver rtrack_driver = {
180         .driver = {
181                 .match          = radio_isa_match,
182                 .probe          = radio_isa_probe,
183                 .remove         = radio_isa_remove,
184                 .driver         = {
185                         .name   = "radio-aimslab",
186                 },
187         },
188         .io_params = io,
189         .radio_nr_params = radio_nr,
190         .io_ports = rtrack_ioports,
191         .num_of_io_ports = ARRAY_SIZE(rtrack_ioports),
192         .region_size = 2,
193         .card = "AIMSlab RadioTrack/RadioReveal",
194         .ops = &rtrack_ops,
195         .has_stereo = true,
196         .max_volume = 0xff,
197 };
198
199 static int __init rtrack_init(void)
200 {
201         return isa_register_driver(&rtrack_driver.driver, RTRACK_MAX);
202 }
203
204 static void __exit rtrack_exit(void)
205 {
206         isa_unregister_driver(&rtrack_driver.driver);
207 }
208
209 module_init(rtrack_init);
210 module_exit(rtrack_exit);