| 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 | |
| 34 | UDA1380Ptr |
| 35 | Detect_uda1380(I2CBusPtr b, I2CSlaveAddr addr) |
| 36 | { |
| 37 | UDA1380Ptr t; |
| 38 | I2CByte a; |
| 39 | |
| 40 | t = calloc(1, sizeof(UDA1380Rec)); |
| 41 | if (t == NULL) |
| 42 | return NULL; |
| 43 | switch (addr) { |
| 44 | case UDA1380_ADDR_1: |
| 45 | case UDA1380_ADDR_2: |
| 46 | t->d.DevName = "UDA1380 Stereo audion coder-decoder"; |
| 47 | break; |
| 48 | default: |
| 49 | t->d.DevName = "Generic UDAxxxx"; |
| 50 | break; |
| 51 | } |
| 52 | t->d.SlaveAddr = addr; |
| 53 | t->d.pI2CBus = b; |
| 54 | t->d.NextDev = NULL; |
| 55 | t->d.StartTimeout = b->StartTimeout; |
| 56 | t->d.BitTimeout = b->BitTimeout; |
| 57 | t->d.AcknTimeout = b->AcknTimeout; |
| 58 | t->d.ByteTimeout = b->ByteTimeout; |
| 59 | |
| 60 | if (!I2C_WriteRead(&(t->d), NULL, 0, &a, 1)) { |
| 61 | free(t); |
| 62 | return NULL; |
| 63 | } |
| 64 | |
| 65 | /* set default parameters */ |
| 66 | if (!I2CDevInit(&(t->d))) { |
| 67 | free(t); |
| 68 | return NULL; |
| 69 | } |
| 70 | |
| 71 | xf86DrvMsg(t->d.pI2CBus->scrnIndex, X_INFO, |
| 72 | "UDA1380 stereo coder-decoder detected\n"); |
| 73 | |
| 74 | return t; |
| 75 | } |
| 76 | |
| 77 | Bool |
| 78 | uda1380_init(UDA1380Ptr t) |
| 79 | { |
| 80 | CARD8 data[3]; |
| 81 | CARD16 tmp; |
| 82 | Bool ret; |
| 83 | |
| 84 | /* Power control */ |
| 85 | data[0] = 0x02; |
| 86 | tmp = |
| 87 | (1 << 13) | (1 << 10) | (1 << 8) | (1 << 7) | (1 << 6) | (1 << 3) | (1 |
| 88 | << |
| 89 | 1); |
| 90 | data[1] = (CARD8) ((tmp >> 8) & 0xff); |
| 91 | data[2] = (CARD8) (tmp & 0xff); |
| 92 | ret = I2C_WriteRead(&(t->d), data, 3, NULL, 0); |
| 93 | if (ret == FALSE) { |
| 94 | xf86DrvMsg(t->d.pI2CBus->scrnIndex, X_INFO, |
| 95 | "UDA1380 failed to initialize\n"); |
| 96 | return FALSE; |
| 97 | } |
| 98 | |
| 99 | /* Analog mixer (AVC) */ |
| 100 | data[0] = 0x03; |
| 101 | /* the analog mixer is muted initially */ |
| 102 | data[1] = 0x3f; |
| 103 | data[2] = 0x3f; |
| 104 | ret = I2C_WriteRead(&(t->d), data, 3, NULL, 0); |
| 105 | if (ret == FALSE) { |
| 106 | xf86DrvMsg(t->d.pI2CBus->scrnIndex, X_INFO, |
| 107 | "UDA1380 failed to initialize\n"); |
| 108 | return FALSE; |
| 109 | } |
| 110 | |
| 111 | xf86DrvMsg(t->d.pI2CBus->scrnIndex, X_INFO, "UDA1380 initialized\n"); |
| 112 | |
| 113 | return TRUE; |
| 114 | } |
| 115 | |
| 116 | void |
| 117 | uda1380_shutdown(UDA1380Ptr t) |
| 118 | { |
| 119 | CARD8 data[3]; |
| 120 | Bool ret; |
| 121 | |
| 122 | /* Power control */ |
| 123 | data[0] = 0x02; |
| 124 | data[1] = 0; |
| 125 | data[2] = 0; |
| 126 | ret = I2C_WriteRead(&(t->d), data, 3, NULL, 0); |
| 127 | if (ret == FALSE) |
| 128 | xf86DrvMsg(t->d.pI2CBus->scrnIndex, X_INFO, |
| 129 | "UDA1380 failed to shutdown\n"); |
| 130 | } |
| 131 | |
| 132 | void |
| 133 | uda1380_setvolume(UDA1380Ptr t, INT32 value) |
| 134 | { |
| 135 | CARD8 data[3]; |
| 136 | |
| 137 | /* |
| 138 | * We have to scale the value ranging from -1000 to 1000 to 0x2c to 0 |
| 139 | */ |
| 140 | CARD8 volume = 47 - (CARD8) ((value + 1000) * 47 / 2000); |
| 141 | Bool ret; |
| 142 | |
| 143 | t->analog_mixer_settings = ((volume << 8) & 0x3f00) | (volume & 0x3f); |
| 144 | |
| 145 | /* Analog mixer (AVC) */ |
| 146 | data[0] = 0x03; |
| 147 | data[1] = volume & 0x3f; |
| 148 | data[2] = volume & 0x3f; |
| 149 | ret = I2C_WriteRead(&(t->d), data, 3, NULL, 0); |
| 150 | if (ret == FALSE) |
| 151 | xf86DrvMsg(t->d.pI2CBus->scrnIndex, X_INFO, |
| 152 | "UDA1380 failed to set volume\n"); |
| 153 | } |
| 154 | |
| 155 | void |
| 156 | uda1380_mute(UDA1380Ptr t, Bool mute) |
| 157 | { |
| 158 | CARD8 data[3]; |
| 159 | Bool ret; |
| 160 | |
| 161 | if (mute == TRUE) { |
| 162 | /* Analog mixer (AVC) */ |
| 163 | data[0] = 0x03; |
| 164 | data[1] = 0xff; |
| 165 | data[2] = 0xff; |
| 166 | ret = I2C_WriteRead(&(t->d), data, 3, NULL, 0); |
| 167 | if (ret == FALSE) |
| 168 | xf86DrvMsg(t->d.pI2CBus->scrnIndex, X_INFO, |
| 169 | "UDA1380 failed to mute\n"); |
| 170 | } |
| 171 | else { |
| 172 | /* Analog mixer (AVC) */ |
| 173 | data[0] = 0x03; |
| 174 | data[1] = (CARD8) ((t->analog_mixer_settings >> 8) & 0x3f); |
| 175 | data[2] = (CARD8) (t->analog_mixer_settings & 0x3f); |
| 176 | ret = I2C_WriteRead(&(t->d), data, 3, NULL, 0); |
| 177 | if (ret == FALSE) |
| 178 | xf86DrvMsg(t->d.pI2CBus->scrnIndex, X_INFO, |
| 179 | "UDA1380 failed to unmute\n"); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | void |
| 184 | uda1380_getstatus(UDA1380Ptr t) |
| 185 | { |
| 186 | } |
| 187 | |
| 188 | void |
| 189 | uda1380_setparameters(UDA1380Ptr t) |
| 190 | { |
| 191 | } |
| 192 | |
| 193 | void |
| 194 | uda1380_dumpstatus(UDA1380Ptr t) |
| 195 | { |
| 196 | } |