blob: 24e2b7d2ae588410657c7ae2a4e19f18cb9f481f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * For the TDA9875 chip
3 * (The TDA9875 is used on the Diamond DTV2000 french version
4 * Other cards probably use these chips as well.)
5 * This driver will not complain if used with any
6 * other i2c device with the same address.
7 *
8 * Copyright (c) 2000 Guillaume Delvit based on Gerd Knorr source and
9 * Eric Sandeen
Mauro Carvalho Chehab5eba3572006-08-25 16:53:10 -030010 * Copyright (c) 2006 Mauro Carvalho Chehab <mchehab@infradead.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * This code is placed under the terms of the GNU General Public License
12 * Based on tda9855.c by Steve VanDeBogart (vandebo@uclink.berkeley.edu)
13 * Which was based on tda8425.c by Greg Alexander (c) 1998
14 *
15 * OPTIONS:
16 * debug - set to 1 if you'd like to see debug messages
17 *
18 * Revision: 0.1 - original version
19 */
20
21#include <linux/module.h>
22#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/string.h>
24#include <linux/timer.h>
25#include <linux/delay.h>
26#include <linux/errno.h>
27#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/i2c.h>
Hans Verkuil12f91a32008-12-18 13:43:45 -030029#include <linux/videodev2.h>
30#include <media/v4l2-device.h>
Hans Verkuila6138912009-03-29 09:04:29 -030031#include <media/v4l2-i2c-drv.h>
Mauro Carvalho Chehabfa3fcce2006-03-23 21:45:24 -030032#include <media/i2c-addr.h>
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034static int debug; /* insmod parameter */
35module_param(debug, int, S_IRUGO | S_IWUSR);
36MODULE_LICENSE("GPL");
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39/* This is a superset of the TDA9875 */
40struct tda9875 {
Hans Verkuil12f91a32008-12-18 13:43:45 -030041 struct v4l2_subdev sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 int rvol, lvol;
43 int bass, treble;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044};
45
Hans Verkuil12f91a32008-12-18 13:43:45 -030046static inline struct tda9875 *to_state(struct v4l2_subdev *sd)
47{
48 return container_of(sd, struct tda9875, sd);
49}
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51#define dprintk if (debug) printk
52
53/* The TDA9875 is made by Philips Semiconductor
54 * http://www.semiconductors.philips.com
55 * TDA9875: I2C-bus controlled DSP audio processor, FM demodulator
56 *
57 */
58
59 /* subaddresses for TDA9875 */
60#define TDA9875_MUT 0x12 /*General mute (value --> 0b11001100*/
61#define TDA9875_CFG 0x01 /* Config register (value --> 0b00000000 */
62#define TDA9875_DACOS 0x13 /*DAC i/o select (ADC) 0b0000100*/
63#define TDA9875_LOSR 0x16 /*Line output select regirter 0b0100 0001*/
64
65#define TDA9875_CH1V 0x0c /*Channel 1 volume (mute)*/
66#define TDA9875_CH2V 0x0d /*Channel 2 volume (mute)*/
67#define TDA9875_SC1 0x14 /*SCART 1 in (mono)*/
68#define TDA9875_SC2 0x15 /*SCART 2 in (mono)*/
69
70#define TDA9875_ADCIS 0x17 /*ADC input select (mono) 0b0110 000*/
71#define TDA9875_AER 0x19 /*Audio effect (AVL+Pseudo) 0b0000 0110*/
72#define TDA9875_MCS 0x18 /*Main channel select (DAC) 0b0000100*/
73#define TDA9875_MVL 0x1a /* Main volume gauche */
74#define TDA9875_MVR 0x1b /* Main volume droite */
75#define TDA9875_MBA 0x1d /* Main Basse */
76#define TDA9875_MTR 0x1e /* Main treble */
77#define TDA9875_ACS 0x1f /* Auxilary channel select (FM) 0b0000000*/
78#define TDA9875_AVL 0x20 /* Auxilary volume gauche */
79#define TDA9875_AVR 0x21 /* Auxilary volume droite */
80#define TDA9875_ABA 0x22 /* Auxilary Basse */
81#define TDA9875_ATR 0x23 /* Auxilary treble */
82
83#define TDA9875_MSR 0x02 /* Monitor select register */
84#define TDA9875_C1MSB 0x03 /* Carrier 1 (FM) frequency register MSB */
85#define TDA9875_C1MIB 0x04 /* Carrier 1 (FM) frequency register (16-8]b */
86#define TDA9875_C1LSB 0x05 /* Carrier 1 (FM) frequency register LSB */
87#define TDA9875_C2MSB 0x06 /* Carrier 2 (nicam) frequency register MSB */
88#define TDA9875_C2MIB 0x07 /* Carrier 2 (nicam) frequency register (16-8]b */
89#define TDA9875_C2LSB 0x08 /* Carrier 2 (nicam) frequency register LSB */
90#define TDA9875_DCR 0x09 /* Demodulateur configuration regirter*/
91#define TDA9875_DEEM 0x0a /* FM de-emphasis regirter*/
92#define TDA9875_FMAT 0x0b /* FM Matrix regirter*/
93
94/* values */
95#define TDA9875_MUTE_ON 0xff /* general mute */
96#define TDA9875_MUTE_OFF 0xcc /* general no mute */
97
98
99
100/* Begin code */
101
Hans Verkuil12f91a32008-12-18 13:43:45 -0300102static int tda9875_write(struct v4l2_subdev *sd, int subaddr, unsigned char val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
Hans Verkuil12f91a32008-12-18 13:43:45 -0300104 struct i2c_client *client = v4l2_get_subdevdata(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 unsigned char buffer[2];
Hans Verkuil12f91a32008-12-18 13:43:45 -0300106
107 v4l2_dbg(1, debug, sd, "Writing %d 0x%x\n", subaddr, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 buffer[0] = subaddr;
109 buffer[1] = val;
Hans Verkuil12f91a32008-12-18 13:43:45 -0300110 if (2 != i2c_master_send(client, buffer, 2)) {
111 v4l2_warn(sd, "I/O error, trying (write %d 0x%x)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 subaddr, val);
113 return -1;
114 }
115 return 0;
116}
117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Hans Verkuil12f91a32008-12-18 13:43:45 -0300119static int i2c_read_register(struct i2c_client *client, int addr, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800121 unsigned char write[1];
122 unsigned char read[1];
123 struct i2c_msg msgs[2] = {
124 { addr, 0, 1, write },
125 { addr, I2C_M_RD, 1, read }
126 };
Hans Verkuil12f91a32008-12-18 13:43:45 -0300127
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800128 write[0] = reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Hans Verkuil12f91a32008-12-18 13:43:45 -0300130 if (2 != i2c_transfer(client->adapter, msgs, 2)) {
131 v4l_warn(client, "I/O error (read2)\n");
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800132 return -1;
133 }
Hans Verkuil12f91a32008-12-18 13:43:45 -0300134 v4l_dbg(1, debug, client, "chip_read2: reg%d=0x%x\n", reg, read[0]);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800135 return read[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136}
137
Hans Verkuil12f91a32008-12-18 13:43:45 -0300138static void tda9875_set(struct v4l2_subdev *sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
Hans Verkuil12f91a32008-12-18 13:43:45 -0300140 struct tda9875 *tda = to_state(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 unsigned char a;
142
Hans Verkuil12f91a32008-12-18 13:43:45 -0300143 v4l2_dbg(1, debug, sd, "tda9875_set(%04x,%04x,%04x,%04x)\n",
144 tda->lvol, tda->rvol, tda->bass, tda->treble);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146 a = tda->lvol & 0xff;
Hans Verkuil12f91a32008-12-18 13:43:45 -0300147 tda9875_write(sd, TDA9875_MVL, a);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 a =tda->rvol & 0xff;
Hans Verkuil12f91a32008-12-18 13:43:45 -0300149 tda9875_write(sd, TDA9875_MVR, a);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 a =tda->bass & 0xff;
Hans Verkuil12f91a32008-12-18 13:43:45 -0300151 tda9875_write(sd, TDA9875_MBA, a);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 a =tda->treble & 0xff;
Hans Verkuil12f91a32008-12-18 13:43:45 -0300153 tda9875_write(sd, TDA9875_MTR, a);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154}
155
Hans Verkuil12f91a32008-12-18 13:43:45 -0300156static void do_tda9875_init(struct v4l2_subdev *sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
Hans Verkuil12f91a32008-12-18 13:43:45 -0300158 struct tda9875 *t = to_state(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Hans Verkuil12f91a32008-12-18 13:43:45 -0300160 v4l2_dbg(1, debug, sd, "In tda9875_init\n");
161 tda9875_write(sd, TDA9875_CFG, 0xd0); /*reg de config 0 (reset)*/
162 tda9875_write(sd, TDA9875_MSR, 0x03); /* Monitor 0b00000XXX*/
163 tda9875_write(sd, TDA9875_C1MSB, 0x00); /*Car1(FM) MSB XMHz*/
164 tda9875_write(sd, TDA9875_C1MIB, 0x00); /*Car1(FM) MIB XMHz*/
165 tda9875_write(sd, TDA9875_C1LSB, 0x00); /*Car1(FM) LSB XMHz*/
166 tda9875_write(sd, TDA9875_C2MSB, 0x00); /*Car2(NICAM) MSB XMHz*/
167 tda9875_write(sd, TDA9875_C2MIB, 0x00); /*Car2(NICAM) MIB XMHz*/
168 tda9875_write(sd, TDA9875_C2LSB, 0x00); /*Car2(NICAM) LSB XMHz*/
169 tda9875_write(sd, TDA9875_DCR, 0x00); /*Demod config 0x00*/
170 tda9875_write(sd, TDA9875_DEEM, 0x44); /*DE-Emph 0b0100 0100*/
171 tda9875_write(sd, TDA9875_FMAT, 0x00); /*FM Matrix reg 0x00*/
172 tda9875_write(sd, TDA9875_SC1, 0x00); /* SCART 1 (SC1)*/
173 tda9875_write(sd, TDA9875_SC2, 0x01); /* SCART 2 (sc2)*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Hans Verkuil12f91a32008-12-18 13:43:45 -0300175 tda9875_write(sd, TDA9875_CH1V, 0x10); /* Channel volume 1 mute*/
176 tda9875_write(sd, TDA9875_CH2V, 0x10); /* Channel volume 2 mute */
177 tda9875_write(sd, TDA9875_DACOS, 0x02); /* sig DAC i/o(in:nicam)*/
178 tda9875_write(sd, TDA9875_ADCIS, 0x6f); /* sig ADC input(in:mono)*/
179 tda9875_write(sd, TDA9875_LOSR, 0x00); /* line out (in:mono)*/
180 tda9875_write(sd, TDA9875_AER, 0x00); /*06 Effect (AVL+PSEUDO) */
181 tda9875_write(sd, TDA9875_MCS, 0x44); /* Main ch select (DAC) */
182 tda9875_write(sd, TDA9875_MVL, 0x03); /* Vol Main left 10dB */
183 tda9875_write(sd, TDA9875_MVR, 0x03); /* Vol Main right 10dB*/
184 tda9875_write(sd, TDA9875_MBA, 0x00); /* Main Bass Main 0dB*/
185 tda9875_write(sd, TDA9875_MTR, 0x00); /* Main Treble Main 0dB*/
186 tda9875_write(sd, TDA9875_ACS, 0x44); /* Aux chan select (dac)*/
187 tda9875_write(sd, TDA9875_AVL, 0x00); /* Vol Aux left 0dB*/
188 tda9875_write(sd, TDA9875_AVR, 0x00); /* Vol Aux right 0dB*/
189 tda9875_write(sd, TDA9875_ABA, 0x00); /* Aux Bass Main 0dB*/
190 tda9875_write(sd, TDA9875_ATR, 0x00); /* Aux Aigus Main 0dB*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Hans Verkuil12f91a32008-12-18 13:43:45 -0300192 tda9875_write(sd, TDA9875_MUT, 0xcc); /* General mute */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Hans Verkuil12f91a32008-12-18 13:43:45 -0300194 t->lvol = t->rvol = 0; /* 0dB */
195 t->bass = 0; /* 0dB */
196 t->treble = 0; /* 0dB */
197 tda9875_set(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198}
199
200
Hans Verkuil12f91a32008-12-18 13:43:45 -0300201static int tda9875_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
Hans Verkuil12f91a32008-12-18 13:43:45 -0300203 struct tda9875 *t = to_state(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Mauro Carvalho Chehab5eba3572006-08-25 16:53:10 -0300205 switch (ctrl->id) {
206 case V4L2_CID_AUDIO_VOLUME:
207 {
208 int left = (t->lvol+84)*606;
209 int right = (t->rvol+84)*606;
210
211 ctrl->value=max(left,right);
212 return 0;
213 }
214 case V4L2_CID_AUDIO_BALANCE:
215 {
216 int left = (t->lvol+84)*606;
217 int right = (t->rvol+84)*606;
218 int volume = max(left,right);
219 int balance = (32768*min(left,right))/
220 (volume ? volume : 1);
221 ctrl->value=(left<right)?
222 (65535-balance) : balance;
223 return 0;
224 }
225 case V4L2_CID_AUDIO_BASS:
226 ctrl->value = (t->bass+12)*2427; /* min -12 max +15 */
227 return 0;
228 case V4L2_CID_AUDIO_TREBLE:
229 ctrl->value = (t->treble+12)*2730;/* min -12 max +12 */
230 return 0;
231 }
232 return -EINVAL;
233}
234
Hans Verkuil12f91a32008-12-18 13:43:45 -0300235static int tda9875_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
Mauro Carvalho Chehab5eba3572006-08-25 16:53:10 -0300236{
Hans Verkuil12f91a32008-12-18 13:43:45 -0300237 struct tda9875 *t = to_state(sd);
Hans Verkuilcf8e1932009-01-17 13:09:11 -0300238 int chvol = 0, volume = 0, balance = 0, left, right;
Mauro Carvalho Chehab5eba3572006-08-25 16:53:10 -0300239
240 switch (ctrl->id) {
241 case V4L2_CID_AUDIO_VOLUME:
242 left = (t->lvol+84)*606;
243 right = (t->rvol+84)*606;
244
245 volume = max(left,right);
246 balance = (32768*min(left,right))/
247 (volume ? volume : 1);
248 balance =(left<right)?
249 (65535-balance) : balance;
250
251 volume = ctrl->value;
252
253 chvol=1;
254 break;
255 case V4L2_CID_AUDIO_BALANCE:
256 left = (t->lvol+84)*606;
257 right = (t->rvol+84)*606;
258
259 volume=max(left,right);
260
261 balance = ctrl->value;
262
263 chvol=1;
264 break;
265 case V4L2_CID_AUDIO_BASS:
266 t->bass = ((ctrl->value/2400)-12) & 0xff;
267 if (t->bass > 15)
268 t->bass = 15;
269 if (t->bass < -12)
270 t->bass = -12 & 0xff;
271 break;
272 case V4L2_CID_AUDIO_TREBLE:
273 t->treble = ((ctrl->value/2700)-12) & 0xff;
274 if (t->treble > 12)
275 t->treble = 12;
276 if (t->treble < -12)
277 t->treble = -12 & 0xff;
278 break;
279 default:
280 return -EINVAL;
281 }
282
283 if (chvol) {
284 left = (min(65536 - balance,32768) *
285 volume) / 32768;
286 right = (min(balance,32768) *
287 volume) / 32768;
288 t->lvol = ((left/606)-84) & 0xff;
289 if (t->lvol > 24)
290 t->lvol = 24;
291 if (t->lvol < -84)
292 t->lvol = -84 & 0xff;
293
294 t->rvol = ((right/606)-84) & 0xff;
295 if (t->rvol > 24)
296 t->rvol = 24;
297 if (t->rvol < -84)
298 t->rvol = -84 & 0xff;
299 }
300
Hans Verkuil12f91a32008-12-18 13:43:45 -0300301 tda9875_set(sd);
Mauro Carvalho Chehab5eba3572006-08-25 16:53:10 -0300302 return 0;
303}
304
Hans Verkuil12f91a32008-12-18 13:43:45 -0300305static int tda9875_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc)
Mauro Carvalho Chehab5eba3572006-08-25 16:53:10 -0300306{
Hans Verkuil12f91a32008-12-18 13:43:45 -0300307 switch (qc->id) {
308 case V4L2_CID_AUDIO_VOLUME:
Hans Verkuil10afbef2009-02-21 18:47:24 -0300309 return v4l2_ctrl_query_fill(qc, 0, 65535, 65535 / 100, 58880);
Hans Verkuil12f91a32008-12-18 13:43:45 -0300310 case V4L2_CID_AUDIO_BASS:
311 case V4L2_CID_AUDIO_TREBLE:
Hans Verkuil10afbef2009-02-21 18:47:24 -0300312 return v4l2_ctrl_query_fill(qc, 0, 65535, 65535 / 100, 32768);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 }
Hans Verkuil12f91a32008-12-18 13:43:45 -0300314 return -EINVAL;
315}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Hans Verkuil12f91a32008-12-18 13:43:45 -0300317/* ----------------------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Hans Verkuil12f91a32008-12-18 13:43:45 -0300319static const struct v4l2_subdev_core_ops tda9875_core_ops = {
320 .queryctrl = tda9875_queryctrl,
321 .g_ctrl = tda9875_g_ctrl,
322 .s_ctrl = tda9875_s_ctrl,
323};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Hans Verkuil12f91a32008-12-18 13:43:45 -0300325static const struct v4l2_subdev_ops tda9875_ops = {
326 .core = &tda9875_core_ops,
327};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Hans Verkuil12f91a32008-12-18 13:43:45 -0300329/* ----------------------------------------------------------------------- */
330
331
332/* *********************** *
333 * i2c interface functions *
334 * *********************** */
335
336static int tda9875_checkit(struct i2c_client *client, int addr)
337{
338 int dic, rev;
339
340 dic = i2c_read_register(client, addr, 254);
341 rev = i2c_read_register(client, addr, 255);
342
343 if (dic == 0 || dic == 2) { /* tda9875 and tda9875A */
344 v4l_info(client, "tda9875%s rev. %d detected at 0x%02x\n",
345 dic == 0 ? "" : "A", rev, addr << 1);
346 return 1;
347 }
348 v4l_info(client, "no such chip at 0x%02x (dic=0x%x rev=0x%x)\n",
349 addr << 1, dic, rev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 return 0;
351}
352
Hans Verkuil12f91a32008-12-18 13:43:45 -0300353static int tda9875_probe(struct i2c_client *client,
354 const struct i2c_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
Hans Verkuil12f91a32008-12-18 13:43:45 -0300356 struct tda9875 *t;
357 struct v4l2_subdev *sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Hans Verkuil12f91a32008-12-18 13:43:45 -0300359 v4l_info(client, "chip found @ 0x%02x (%s)\n",
360 client->addr << 1, client->adapter->name);
361
362 if (!tda9875_checkit(client, client->addr))
363 return -ENODEV;
364
365 t = kzalloc(sizeof(*t), GFP_KERNEL);
366 if (!t)
367 return -ENOMEM;
368 sd = &t->sd;
369 v4l2_i2c_subdev_init(sd, client, &tda9875_ops);
370
371 do_tda9875_init(sd);
372 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373}
374
Hans Verkuil12f91a32008-12-18 13:43:45 -0300375static int tda9875_remove(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
Hans Verkuil12f91a32008-12-18 13:43:45 -0300377 struct v4l2_subdev *sd = i2c_get_clientdata(client);
378
379 do_tda9875_init(sd);
380 v4l2_device_unregister_subdev(sd);
381 kfree(to_state(sd));
382 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383}
384
Hans Verkuil12f91a32008-12-18 13:43:45 -0300385static const struct i2c_device_id tda9875_id[] = {
386 { "tda9875", 0 },
387 { }
388};
389MODULE_DEVICE_TABLE(i2c, tda9875_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Hans Verkuil12f91a32008-12-18 13:43:45 -0300391static struct v4l2_i2c_driver_data v4l2_i2c_data = {
392 .name = "tda9875",
Hans Verkuil12f91a32008-12-18 13:43:45 -0300393 .probe = tda9875_probe,
394 .remove = tda9875_remove,
395 .id_table = tda9875_id,
396};