1/*************************************************************************************
2 * Copyright (C) 2005 Bogdan D. bogdand@users.sourceforge.net
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy of this
5 * software and associated documentation files (the "Software"), to deal in the Software
6 * without restriction, including without limitation the rights to use, copy, modify,
7 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
8 * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
9 *
10 * The above copyright notice and this permission notice shall be included in all copies or
11 * substantial portions of the Software.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
14 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
15 * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM,
16 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
18 *
19 * Except as contained in this notice, the name of the author shall not be used in advertising or
20 * otherwise to promote the sale, use or other dealings in this Software without prior written
21 * authorization from the author.
22 *
23 ************************************************************************************/
24
25#ifdef HAVE_XORG_CONFIG_H
26#include <xorg-config.h>
27#endif
28
29#include "xf86.h"
30#include "xf86i2c.h"
31#include "uda1380.h"
32#include "i2c_def.h"
33
34UDA1380Ptr Detect_uda1380(I2CBusPtr b, I2CSlaveAddr addr)
35{
36	UDA1380Ptr t;
37	I2CByte a;
38
39	t = calloc(1, sizeof(UDA1380Rec));
40	if(t == NULL) return NULL;
41	switch(addr)
42	{
43		case UDA1380_ADDR_1:
44		case UDA1380_ADDR_2:
45			t->d.DevName = "UDA1380 Stereo audion coder-decoder";
46			break;
47		default:
48			t->d.DevName = "Generic UDAxxxx";
49		break;
50	}
51	t->d.SlaveAddr = addr;
52	t->d.pI2CBus = b;
53	t->d.NextDev = NULL;
54	t->d.StartTimeout = b->StartTimeout;
55	t->d.BitTimeout = b->BitTimeout;
56	t->d.AcknTimeout = b->AcknTimeout;
57	t->d.ByteTimeout = b->ByteTimeout;
58
59	if(!I2C_WriteRead(&(t->d), NULL, 0, &a, 1))
60	{
61		free(t);
62		return NULL;
63	}
64
65	/* set default parameters */
66	if(!I2CDevInit(&(t->d)))
67	{
68		free(t);
69		return NULL;
70	}
71
72	xf86DrvMsg(t->d.pI2CBus->scrnIndex,X_INFO,"UDA1380 stereo coder-decoder detected\n");
73
74	return t;
75}
76
77Bool uda1380_init(UDA1380Ptr t)
78{
79	CARD8 data[3];
80	CARD16 tmp;
81	Bool ret;
82
83	/* Power control */
84	data[0] = 0x02;
85	tmp = (1 << 13) | (1 << 10) | ( 1 << 8) | (1 << 7) | (1 << 6) | (1 << 3) | (1 << 1);
86	data[1] = (CARD8)((tmp >> 8) & 0xff);
87	data[2] = (CARD8)(tmp & 0xff);
88	ret = I2C_WriteRead(&(t->d), data, 3, NULL, 0);
89	if (ret == FALSE)
90	{
91		xf86DrvMsg(t->d.pI2CBus->scrnIndex,X_INFO,"UDA1380 failed to initialize\n");
92		return FALSE;
93	}
94
95	/* Analog mixer  (AVC) */
96	data[0] = 0x03;
97	/* the analog mixer is muted initially */
98	data[1] = 0x3f;
99	data[2] = 0x3f;
100	ret = I2C_WriteRead(&(t->d), data, 3, NULL, 0);
101	if (ret == FALSE)
102	{
103		xf86DrvMsg(t->d.pI2CBus->scrnIndex,X_INFO,"UDA1380 failed to initialize\n");
104		return FALSE;
105	}
106
107	xf86DrvMsg(t->d.pI2CBus->scrnIndex,X_INFO,"UDA1380 initialized\n");
108
109	return TRUE;
110}
111
112void uda1380_shutdown(UDA1380Ptr t)
113{
114	CARD8 data[3];
115	Bool ret;
116
117	/* Power control */
118	data[0] = 0x02;
119	data[1] = 0;
120	data[2] = 0;
121	ret = I2C_WriteRead(&(t->d), data, 3, NULL, 0);
122	if (ret == FALSE)
123		xf86DrvMsg(t->d.pI2CBus->scrnIndex,X_INFO,"UDA1380 failed to shutdown\n");
124}
125
126void uda1380_setvolume(UDA1380Ptr t, INT32 value)
127{
128	CARD8 data[3];
129	/*
130	 * We have to scale the value ranging from -1000 to 1000 to 0x2c to 0
131	 */
132	CARD8 volume = 47 - (CARD8)((value + 1000) * 47 / 2000);
133	Bool ret;
134
135	t->analog_mixer_settings = ((volume << 8) & 0x3f00) | (volume & 0x3f);
136
137	/* Analog mixer  (AVC) */
138	data[0] = 0x03;
139	data[1] = volume & 0x3f;
140	data[2] = volume & 0x3f;
141	ret = I2C_WriteRead(&(t->d), data, 3, NULL, 0);
142	if (ret == FALSE)
143		xf86DrvMsg(t->d.pI2CBus->scrnIndex,X_INFO,"UDA1380 failed to set volume\n");
144}
145
146void uda1380_mute(UDA1380Ptr t, Bool mute)
147{
148	CARD8 data[3];
149	Bool ret;
150
151	if (mute == TRUE)
152	{
153		/* Analog mixer  (AVC) */
154		data[0] = 0x03;
155		data[1] = 0xff;
156		data[2] = 0xff;
157		ret = I2C_WriteRead(&(t->d), data, 3, NULL, 0);
158		if (ret == FALSE)
159			xf86DrvMsg(t->d.pI2CBus->scrnIndex,X_INFO,"UDA1380 failed to mute\n");
160	}
161	else
162	{
163		/* Analog mixer  (AVC) */
164		data[0] = 0x03;
165		data[1] = (CARD8)((t->analog_mixer_settings >> 8) & 0x3f);
166		data[2] = (CARD8)(t->analog_mixer_settings & 0x3f);
167		ret = I2C_WriteRead(&(t->d), data, 3, NULL, 0);
168		if (ret == FALSE)
169			xf86DrvMsg(t->d.pI2CBus->scrnIndex,X_INFO,"UDA1380 failed to unmute\n");
170	}
171}
172
173void uda1380_getstatus(UDA1380Ptr t)
174{
175}
176
177void uda1380_setparameters(UDA1380Ptr t)
178{
179}
180
181void uda1380_dumpstatus(UDA1380Ptr t)
182{
183}
184