blob: dc81d422b3947944cddee6d0a76a47f792e8f91b [file] [log] [blame]
Ondrej Zary3491a882012-05-17 04:55:01 -03001/* SF16-FMR2 and SF16-FMD2 radio driver for Linux
Ondrej Zary9e8fa0e2011-06-01 16:57:11 -03002 * Copyright (c) 2011 Ondrej Zary
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Ondrej Zary9e8fa0e2011-06-01 16:57:11 -03004 * Original driver was (c) 2000-2002 Ziglio Frediano, freddy77@angelfire.com
5 * but almost nothing remained here after conversion to generic TEA575x
6 * implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 */
8
Ondrej Zary9e8fa0e2011-06-01 16:57:11 -03009#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/module.h> /* Modules */
11#include <linux/init.h> /* Initdata */
Hans Verkuild4ecc832012-02-27 05:30:13 -030012#include <linux/slab.h>
Peter Osterlundfb911ee2005-09-13 01:25:15 -070013#include <linux/ioport.h> /* request_region */
Hans Verkuilc32a9d72009-03-06 13:53:26 -030014#include <linux/io.h> /* outb, outb_p */
Hans Verkuild4ecc832012-02-27 05:30:13 -030015#include <linux/isa.h>
Ondrej Zary3491a882012-05-17 04:55:01 -030016#include <linux/pnp.h>
Mauro Carvalho Chehabd647f0b2015-11-13 19:40:07 -020017#include <media/drv-intf/tea575x.h>
Hans Verkuilc32a9d72009-03-06 13:53:26 -030018
Ondrej Zary9e8fa0e2011-06-01 16:57:11 -030019MODULE_AUTHOR("Ondrej Zary");
Ondrej Zary3491a882012-05-17 04:55:01 -030020MODULE_DESCRIPTION("MediaForte SF16-FMR2 and SF16-FMD2 FM radio card driver");
Hans Verkuilc32a9d72009-03-06 13:53:26 -030021MODULE_LICENSE("GPL");
22
Ondrej Zary3491a882012-05-17 04:55:01 -030023/* these cards can only use two different ports (0x384 and 0x284) */
24#define FMR2_MAX 2
25
26static int radio_nr[FMR2_MAX] = { [0 ... (FMR2_MAX - 1)] = -1 };
27module_param_array(radio_nr, int, NULL, 0444);
28MODULE_PARM_DESC(radio_nr, "Radio device numbers");
Hans Verkuild4ecc832012-02-27 05:30:13 -030029
Ondrej Zary9e8fa0e2011-06-01 16:57:11 -030030struct fmr2 {
Hans Verkuilc32a9d72009-03-06 13:53:26 -030031 int io;
Hans Verkuild4ecc832012-02-27 05:30:13 -030032 struct v4l2_device v4l2_dev;
Ondrej Zary9e8fa0e2011-06-01 16:57:11 -030033 struct snd_tea575x tea;
34 struct v4l2_ctrl *volume;
35 struct v4l2_ctrl *balance;
Ondrej Zary3491a882012-05-17 04:55:01 -030036 bool is_fmd2;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037};
38
Ondrej Zary3491a882012-05-17 04:55:01 -030039static int num_fmr2_cards;
40static struct fmr2 *fmr2_cards[FMR2_MAX];
41static bool isa_registered;
42static bool pnp_registered;
43
44/* the port is hardwired on SF16-FMR2 */
Ondrej Zary9e8fa0e2011-06-01 16:57:11 -030045#define FMR2_PORT 0x384
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Ondrej Zary9e8fa0e2011-06-01 16:57:11 -030047/* TEA575x tuner pins */
48#define STR_DATA (1 << 0)
49#define STR_CLK (1 << 1)
50#define STR_WREN (1 << 2)
51#define STR_MOST (1 << 3)
52/* PT2254A/TC9154A volume control pins */
53#define PT_ST (1 << 4)
54#define PT_CK (1 << 5)
55#define PT_DATA (1 << 6)
56/* volume control presence pin */
57#define FMR2_HASVOL (1 << 7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Ondrej Zary9e8fa0e2011-06-01 16:57:11 -030059static void fmr2_tea575x_set_pins(struct snd_tea575x *tea, u8 pins)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060{
Ondrej Zary9e8fa0e2011-06-01 16:57:11 -030061 struct fmr2 *fmr2 = tea->private_data;
62 u8 bits = 0;
63
64 bits |= (pins & TEA575X_DATA) ? STR_DATA : 0;
65 bits |= (pins & TEA575X_CLK) ? STR_CLK : 0;
66 /* WRITE_ENABLE is inverted, DATA must be high during read */
67 bits |= (pins & TEA575X_WREN) ? 0 : STR_WREN | STR_DATA;
68
69 outb(bits, fmr2->io);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070}
71
Ondrej Zary9e8fa0e2011-06-01 16:57:11 -030072static u8 fmr2_tea575x_get_pins(struct snd_tea575x *tea)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073{
Ondrej Zary9e8fa0e2011-06-01 16:57:11 -030074 struct fmr2 *fmr2 = tea->private_data;
75 u8 bits = inb(fmr2->io);
Hans Verkuilc32a9d72009-03-06 13:53:26 -030076
Dan Carpenterb5c2e0a2013-08-22 13:07:17 -030077 return ((bits & STR_DATA) ? TEA575X_DATA : 0) |
78 ((bits & STR_MOST) ? TEA575X_MOST : 0);
Ondrej Zary9e8fa0e2011-06-01 16:57:11 -030079}
80
81static void fmr2_tea575x_set_direction(struct snd_tea575x *tea, bool output)
82{
83}
84
Julia Lawall22dbec22015-11-22 08:32:53 -020085static const struct snd_tea575x_ops fmr2_tea_ops = {
Ondrej Zary9e8fa0e2011-06-01 16:57:11 -030086 .set_pins = fmr2_tea575x_set_pins,
87 .get_pins = fmr2_tea575x_get_pins,
88 .set_direction = fmr2_tea575x_set_direction,
89};
90
91/* TC9154A/PT2254A volume control */
92
93/* 18-bit shift register bit definitions */
94#define TC9154A_ATT_MAJ_0DB (1 << 0)
95#define TC9154A_ATT_MAJ_10DB (1 << 1)
96#define TC9154A_ATT_MAJ_20DB (1 << 2)
97#define TC9154A_ATT_MAJ_30DB (1 << 3)
98#define TC9154A_ATT_MAJ_40DB (1 << 4)
99#define TC9154A_ATT_MAJ_50DB (1 << 5)
100#define TC9154A_ATT_MAJ_60DB (1 << 6)
101
102#define TC9154A_ATT_MIN_0DB (1 << 7)
103#define TC9154A_ATT_MIN_2DB (1 << 8)
104#define TC9154A_ATT_MIN_4DB (1 << 9)
105#define TC9154A_ATT_MIN_6DB (1 << 10)
106#define TC9154A_ATT_MIN_8DB (1 << 11)
107/* bit 12 is ignored */
108#define TC9154A_CHANNEL_LEFT (1 << 13)
109#define TC9154A_CHANNEL_RIGHT (1 << 14)
110/* bits 15, 16, 17 must be 0 */
111
112#define TC9154A_ATT_MAJ(x) (1 << x)
113#define TC9154A_ATT_MIN(x) (1 << (7 + x))
114
115static void tc9154a_set_pins(struct fmr2 *fmr2, u8 pins)
116{
117 if (!fmr2->tea.mute)
118 pins |= STR_WREN;
119
120 outb(pins, fmr2->io);
121}
122
123static void tc9154a_set_attenuation(struct fmr2 *fmr2, int att, u32 channel)
124{
125 int i;
126 u32 reg;
127 u8 bit;
128
129 reg = TC9154A_ATT_MAJ(att / 10) | TC9154A_ATT_MIN((att % 10) / 2);
130 reg |= channel;
131 /* write 18-bit shift register, LSB first */
132 for (i = 0; i < 18; i++) {
133 bit = reg & (1 << i) ? PT_DATA : 0;
134 tc9154a_set_pins(fmr2, bit);
135 udelay(5);
136 tc9154a_set_pins(fmr2, bit | PT_CK);
137 udelay(5);
138 tc9154a_set_pins(fmr2, bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 }
Ondrej Zary9e8fa0e2011-06-01 16:57:11 -0300140
141 /* latch register data */
142 udelay(5);
143 tc9154a_set_pins(fmr2, PT_ST);
144 udelay(5);
145 tc9154a_set_pins(fmr2, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146}
147
Ondrej Zary9e8fa0e2011-06-01 16:57:11 -0300148static int fmr2_s_ctrl(struct v4l2_ctrl *ctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
Ondrej Zary9e8fa0e2011-06-01 16:57:11 -0300150 struct snd_tea575x *tea = container_of(ctrl->handler, struct snd_tea575x, ctrl_handler);
151 struct fmr2 *fmr2 = tea->private_data;
152 int volume, balance, left, right;
Douglas Landgraf34ab9622007-04-23 17:51:37 -0300153
154 switch (ctrl->id) {
Douglas Landgraf34ab9622007-04-23 17:51:37 -0300155 case V4L2_CID_AUDIO_VOLUME:
Ondrej Zary9e8fa0e2011-06-01 16:57:11 -0300156 volume = ctrl->val;
157 balance = fmr2->balance->cur.val;
Douglas Landgraf34ab9622007-04-23 17:51:37 -0300158 break;
Ondrej Zary9e8fa0e2011-06-01 16:57:11 -0300159 case V4L2_CID_AUDIO_BALANCE:
160 balance = ctrl->val;
161 volume = fmr2->volume->cur.val;
Douglas Landgraf34ab9622007-04-23 17:51:37 -0300162 break;
163 default:
164 return -EINVAL;
165 }
166
Ondrej Zary9e8fa0e2011-06-01 16:57:11 -0300167 left = right = volume;
168 if (balance < 0)
169 right = max(0, right + balance);
170 if (balance > 0)
171 left = max(0, left - balance);
Douglas Landgraf34ab9622007-04-23 17:51:37 -0300172
Ondrej Zary9e8fa0e2011-06-01 16:57:11 -0300173 tc9154a_set_attenuation(fmr2, abs(left - 68), TC9154A_CHANNEL_LEFT);
174 tc9154a_set_attenuation(fmr2, abs(right - 68), TC9154A_CHANNEL_RIGHT);
175
Douglas Landgraf34ab9622007-04-23 17:51:37 -0300176 return 0;
177}
178
Ondrej Zary9e8fa0e2011-06-01 16:57:11 -0300179static const struct v4l2_ctrl_ops fmr2_ctrl_ops = {
180 .s_ctrl = fmr2_s_ctrl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181};
182
Ondrej Zary9e8fa0e2011-06-01 16:57:11 -0300183static int fmr2_tea_ext_init(struct snd_tea575x *tea)
184{
185 struct fmr2 *fmr2 = tea->private_data;
186
Ondrej Zary3491a882012-05-17 04:55:01 -0300187 /* FMR2 can have volume control, FMD2 can't (uses SB16 mixer) */
188 if (!fmr2->is_fmd2 && inb(fmr2->io) & FMR2_HASVOL) {
Ondrej Zary9e8fa0e2011-06-01 16:57:11 -0300189 fmr2->volume = v4l2_ctrl_new_std(&tea->ctrl_handler, &fmr2_ctrl_ops, V4L2_CID_AUDIO_VOLUME, 0, 68, 2, 56);
190 fmr2->balance = v4l2_ctrl_new_std(&tea->ctrl_handler, &fmr2_ctrl_ops, V4L2_CID_AUDIO_BALANCE, -68, 68, 2, 0);
191 if (tea->ctrl_handler.error) {
Masanari Iidabdb2c412012-02-14 10:06:09 -0300192 printk(KERN_ERR "radio-sf16fmr2: can't initialize controls\n");
Ondrej Zary9e8fa0e2011-06-01 16:57:11 -0300193 return tea->ctrl_handler.error;
194 }
195 }
196
197 return 0;
198}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -0800200static struct pnp_device_id fmr2_pnp_ids[] = {
Ondrej Zary3491a882012-05-17 04:55:01 -0300201 { .id = "MFRad13" }, /* tuner subdevice of SF16-FMD2 */
202 { .id = "" }
203};
204MODULE_DEVICE_TABLE(pnp, fmr2_pnp_ids);
205
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -0800206static int fmr2_probe(struct fmr2 *fmr2, struct device *pdev, int io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
Ondrej Zary3491a882012-05-17 04:55:01 -0300208 int err, i;
209 char *card_name = fmr2->is_fmd2 ? "SF16-FMD2" : "SF16-FMR2";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Ondrej Zary3491a882012-05-17 04:55:01 -0300211 /* avoid errors if a card was already registered at given port */
212 for (i = 0; i < num_fmr2_cards; i++)
213 if (io == fmr2_cards[i]->io)
214 return -EBUSY;
Hans Verkuild4ecc832012-02-27 05:30:13 -0300215
Ondrej Zary3491a882012-05-17 04:55:01 -0300216 strlcpy(fmr2->v4l2_dev.name, "radio-sf16fmr2",
217 sizeof(fmr2->v4l2_dev.name)),
218 fmr2->io = io;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Hans Verkuild4ecc832012-02-27 05:30:13 -0300220 if (!request_region(fmr2->io, 2, fmr2->v4l2_dev.name)) {
Ondrej Zary9e8fa0e2011-06-01 16:57:11 -0300221 printk(KERN_ERR "radio-sf16fmr2: I/O port 0x%x already in use\n", fmr2->io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 return -EBUSY;
223 }
224
Hans Verkuild4ecc832012-02-27 05:30:13 -0300225 dev_set_drvdata(pdev, fmr2);
226 err = v4l2_device_register(pdev, &fmr2->v4l2_dev);
227 if (err < 0) {
228 v4l2_err(&fmr2->v4l2_dev, "Could not register v4l2_device\n");
229 release_region(fmr2->io, 2);
Hans Verkuild4ecc832012-02-27 05:30:13 -0300230 return err;
231 }
232 fmr2->tea.v4l2_dev = &fmr2->v4l2_dev;
Ondrej Zary9e8fa0e2011-06-01 16:57:11 -0300233 fmr2->tea.private_data = fmr2;
Ondrej Zary3491a882012-05-17 04:55:01 -0300234 fmr2->tea.radio_nr = radio_nr[num_fmr2_cards];
Ondrej Zary9e8fa0e2011-06-01 16:57:11 -0300235 fmr2->tea.ops = &fmr2_tea_ops;
236 fmr2->tea.ext_init = fmr2_tea_ext_init;
Ondrej Zary3491a882012-05-17 04:55:01 -0300237 strlcpy(fmr2->tea.card, card_name, sizeof(fmr2->tea.card));
238 snprintf(fmr2->tea.bus_info, sizeof(fmr2->tea.bus_info), "%s:%s",
239 fmr2->is_fmd2 ? "PnP" : "ISA", dev_name(pdev));
Ondrej Zary9e8fa0e2011-06-01 16:57:11 -0300240
Hans de Goede5daf53a2012-05-19 07:57:03 -0300241 if (snd_tea575x_init(&fmr2->tea, THIS_MODULE)) {
Ondrej Zary9e8fa0e2011-06-01 16:57:11 -0300242 printk(KERN_ERR "radio-sf16fmr2: Unable to detect TEA575x tuner\n");
Hans Verkuilc32a9d72009-03-06 13:53:26 -0300243 release_region(fmr2->io, 2);
Ondrej Zary9e8fa0e2011-06-01 16:57:11 -0300244 return -ENODEV;
Hans Verkuilc32a9d72009-03-06 13:53:26 -0300245 }
246
Ondrej Zary3491a882012-05-17 04:55:01 -0300247 printk(KERN_INFO "radio-sf16fmr2: %s radio card at 0x%x.\n",
248 card_name, fmr2->io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 return 0;
250}
251
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -0800252static int fmr2_isa_match(struct device *pdev, unsigned int ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
Ondrej Zary3491a882012-05-17 04:55:01 -0300254 struct fmr2 *fmr2 = kzalloc(sizeof(*fmr2), GFP_KERNEL);
255 if (!fmr2)
256 return 0;
Hans Verkuilc32a9d72009-03-06 13:53:26 -0300257
Ondrej Zary3491a882012-05-17 04:55:01 -0300258 if (fmr2_probe(fmr2, pdev, FMR2_PORT)) {
259 kfree(fmr2);
260 return 0;
261 }
262 dev_set_drvdata(pdev, fmr2);
263 fmr2_cards[num_fmr2_cards++] = fmr2;
264
265 return 1;
266}
267
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -0800268static int fmr2_pnp_probe(struct pnp_dev *pdev, const struct pnp_device_id *id)
Ondrej Zary3491a882012-05-17 04:55:01 -0300269{
270 int ret;
271 struct fmr2 *fmr2 = kzalloc(sizeof(*fmr2), GFP_KERNEL);
272 if (!fmr2)
273 return -ENOMEM;
274
275 fmr2->is_fmd2 = true;
276 ret = fmr2_probe(fmr2, &pdev->dev, pnp_port_start(pdev, 0));
277 if (ret) {
278 kfree(fmr2);
279 return ret;
280 }
281 pnp_set_drvdata(pdev, fmr2);
282 fmr2_cards[num_fmr2_cards++] = fmr2;
283
284 return 0;
285}
286
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -0800287static void fmr2_remove(struct fmr2 *fmr2)
Ondrej Zary3491a882012-05-17 04:55:01 -0300288{
Ondrej Zary9e8fa0e2011-06-01 16:57:11 -0300289 snd_tea575x_exit(&fmr2->tea);
Hans Verkuilc32a9d72009-03-06 13:53:26 -0300290 release_region(fmr2->io, 2);
Hans Verkuild4ecc832012-02-27 05:30:13 -0300291 v4l2_device_unregister(&fmr2->v4l2_dev);
292 kfree(fmr2);
Ondrej Zary3491a882012-05-17 04:55:01 -0300293}
294
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -0800295static int fmr2_isa_remove(struct device *pdev, unsigned int ndev)
Ondrej Zary3491a882012-05-17 04:55:01 -0300296{
297 fmr2_remove(dev_get_drvdata(pdev));
Ondrej Zary3491a882012-05-17 04:55:01 -0300298
Hans Verkuild4ecc832012-02-27 05:30:13 -0300299 return 0;
300}
301
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -0800302static void fmr2_pnp_remove(struct pnp_dev *pdev)
Ondrej Zary3491a882012-05-17 04:55:01 -0300303{
304 fmr2_remove(pnp_get_drvdata(pdev));
305 pnp_set_drvdata(pdev, NULL);
306}
307
Mauro Carvalho Chehab25fb62b2014-09-24 15:44:08 -0300308static struct isa_driver fmr2_isa_driver = {
Ondrej Zary3491a882012-05-17 04:55:01 -0300309 .match = fmr2_isa_match,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -0800310 .remove = fmr2_isa_remove,
Hans Verkuild4ecc832012-02-27 05:30:13 -0300311 .driver = {
312 .name = "radio-sf16fmr2",
313 },
314};
315
Mauro Carvalho Chehab25fb62b2014-09-24 15:44:08 -0300316static struct pnp_driver fmr2_pnp_driver = {
Ondrej Zary3491a882012-05-17 04:55:01 -0300317 .name = "radio-sf16fmr2",
318 .id_table = fmr2_pnp_ids,
319 .probe = fmr2_pnp_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -0800320 .remove = fmr2_pnp_remove,
Ondrej Zary3491a882012-05-17 04:55:01 -0300321};
322
Hans Verkuild4ecc832012-02-27 05:30:13 -0300323static int __init fmr2_init(void)
324{
Ondrej Zary3491a882012-05-17 04:55:01 -0300325 int ret;
326
327 ret = pnp_register_driver(&fmr2_pnp_driver);
328 if (!ret)
329 pnp_registered = true;
330 ret = isa_register_driver(&fmr2_isa_driver, 1);
331 if (!ret)
332 isa_registered = true;
333
334 return (pnp_registered || isa_registered) ? 0 : ret;
Hans Verkuild4ecc832012-02-27 05:30:13 -0300335}
336
337static void __exit fmr2_exit(void)
338{
Ondrej Zary3491a882012-05-17 04:55:01 -0300339 if (pnp_registered)
340 pnp_unregister_driver(&fmr2_pnp_driver);
341 if (isa_registered)
342 isa_unregister_driver(&fmr2_isa_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343}
344
345module_init(fmr2_init);
Hans Verkuilc32a9d72009-03-06 13:53:26 -0300346module_exit(fmr2_exit);