Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 1 | /* cx25840 audio functions |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or |
| 4 | * modify it under the terms of the GNU General Public License |
| 5 | * as published by the Free Software Foundation; either version 2 |
| 6 | * of the License, or (at your option) any later version. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this program; if not, write to the Free Software |
| 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 16 | */ |
| 17 | |
| 18 | |
| 19 | #include <linux/videodev2.h> |
| 20 | #include <linux/i2c.h> |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 21 | #include <media/v4l2-common.h> |
Hans Verkuil | 31bc09b | 2006-03-25 10:26:09 -0300 | [diff] [blame] | 22 | #include <media/cx25840.h> |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 23 | |
Hans Verkuil | 31bc09b | 2006-03-25 10:26:09 -0300 | [diff] [blame] | 24 | #include "cx25840-core.h" |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 25 | |
Hans Verkuil | 3578d3d | 2006-01-09 15:25:41 -0200 | [diff] [blame] | 26 | static int set_audclk_freq(struct i2c_client *client, u32 freq) |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 27 | { |
| 28 | struct cx25840_state *state = i2c_get_clientdata(client); |
| 29 | |
Hans Verkuil | 3578d3d | 2006-01-09 15:25:41 -0200 | [diff] [blame] | 30 | if (freq != 32000 && freq != 44100 && freq != 48000) |
| 31 | return -EINVAL; |
| 32 | |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 33 | /* assert soft reset */ |
| 34 | cx25840_and_or(client, 0x810, ~0x1, 0x01); |
| 35 | |
| 36 | /* common for all inputs and rates */ |
| 37 | /* SA_MCLK_SEL=1, SA_MCLK_DIV=0x10 */ |
| 38 | cx25840_write(client, 0x127, 0x50); |
| 39 | |
Hans Verkuil | a8bbf12 | 2006-01-09 15:25:42 -0200 | [diff] [blame] | 40 | if (state->aud_input != CX25840_AUDIO_SERIAL) { |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 41 | switch (freq) { |
Hans Verkuil | 3578d3d | 2006-01-09 15:25:41 -0200 | [diff] [blame] | 42 | case 32000: |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 43 | /* VID_PLL and AUX_PLL */ |
| 44 | cx25840_write4(client, 0x108, 0x0f040610); |
| 45 | |
| 46 | /* AUX_PLL_FRAC */ |
| 47 | cx25840_write4(client, 0x110, 0xee39bb01); |
| 48 | |
| 49 | /* src3/4/6_ctl = 0x0801f77f */ |
| 50 | cx25840_write4(client, 0x900, 0x7ff70108); |
| 51 | cx25840_write4(client, 0x904, 0x7ff70108); |
| 52 | cx25840_write4(client, 0x90c, 0x7ff70108); |
| 53 | break; |
| 54 | |
Hans Verkuil | 3578d3d | 2006-01-09 15:25:41 -0200 | [diff] [blame] | 55 | case 44100: |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 56 | /* VID_PLL and AUX_PLL */ |
| 57 | cx25840_write4(client, 0x108, 0x0f040910); |
| 58 | |
| 59 | /* AUX_PLL_FRAC */ |
| 60 | cx25840_write4(client, 0x110, 0xd66bec00); |
| 61 | |
| 62 | /* src3/4/6_ctl = 0x08016d59 */ |
| 63 | cx25840_write4(client, 0x900, 0x596d0108); |
| 64 | cx25840_write4(client, 0x904, 0x596d0108); |
| 65 | cx25840_write4(client, 0x90c, 0x596d0108); |
| 66 | break; |
| 67 | |
Hans Verkuil | 3578d3d | 2006-01-09 15:25:41 -0200 | [diff] [blame] | 68 | case 48000: |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 69 | /* VID_PLL and AUX_PLL */ |
| 70 | cx25840_write4(client, 0x108, 0x0f040a10); |
| 71 | |
| 72 | /* AUX_PLL_FRAC */ |
| 73 | cx25840_write4(client, 0x110, 0xe5d69800); |
| 74 | |
| 75 | /* src3/4/6_ctl = 0x08014faa */ |
| 76 | cx25840_write4(client, 0x900, 0xaa4f0108); |
| 77 | cx25840_write4(client, 0x904, 0xaa4f0108); |
| 78 | cx25840_write4(client, 0x90c, 0xaa4f0108); |
| 79 | break; |
| 80 | } |
Hans Verkuil | a8bbf12 | 2006-01-09 15:25:42 -0200 | [diff] [blame] | 81 | } else { |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 82 | switch (freq) { |
Hans Verkuil | 3578d3d | 2006-01-09 15:25:41 -0200 | [diff] [blame] | 83 | case 32000: |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 84 | /* VID_PLL and AUX_PLL */ |
| 85 | cx25840_write4(client, 0x108, 0x0f04081e); |
| 86 | |
| 87 | /* AUX_PLL_FRAC */ |
| 88 | cx25840_write4(client, 0x110, 0x69082a01); |
| 89 | |
| 90 | /* src1_ctl = 0x08010000 */ |
| 91 | cx25840_write4(client, 0x8f8, 0x00000108); |
| 92 | |
| 93 | /* src3/4/6_ctl = 0x08020000 */ |
| 94 | cx25840_write4(client, 0x900, 0x00000208); |
| 95 | cx25840_write4(client, 0x904, 0x00000208); |
| 96 | cx25840_write4(client, 0x90c, 0x00000208); |
| 97 | |
| 98 | /* SA_MCLK_SEL=1, SA_MCLK_DIV=0x14 */ |
| 99 | cx25840_write(client, 0x127, 0x54); |
| 100 | break; |
| 101 | |
Hans Verkuil | 3578d3d | 2006-01-09 15:25:41 -0200 | [diff] [blame] | 102 | case 44100: |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 103 | /* VID_PLL and AUX_PLL */ |
| 104 | cx25840_write4(client, 0x108, 0x0f040918); |
| 105 | |
| 106 | /* AUX_PLL_FRAC */ |
| 107 | cx25840_write4(client, 0x110, 0xd66bec00); |
| 108 | |
| 109 | /* src1_ctl = 0x08010000 */ |
| 110 | cx25840_write4(client, 0x8f8, 0xcd600108); |
| 111 | |
| 112 | /* src3/4/6_ctl = 0x08020000 */ |
| 113 | cx25840_write4(client, 0x900, 0x85730108); |
| 114 | cx25840_write4(client, 0x904, 0x85730108); |
| 115 | cx25840_write4(client, 0x90c, 0x85730108); |
| 116 | break; |
| 117 | |
Hans Verkuil | 3578d3d | 2006-01-09 15:25:41 -0200 | [diff] [blame] | 118 | case 48000: |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 119 | /* VID_PLL and AUX_PLL */ |
| 120 | cx25840_write4(client, 0x108, 0x0f040a18); |
| 121 | |
| 122 | /* AUX_PLL_FRAC */ |
| 123 | cx25840_write4(client, 0x110, 0xe5d69800); |
| 124 | |
| 125 | /* src1_ctl = 0x08010000 */ |
| 126 | cx25840_write4(client, 0x8f8, 0x00800108); |
| 127 | |
| 128 | /* src3/4/6_ctl = 0x08020000 */ |
| 129 | cx25840_write4(client, 0x900, 0x55550108); |
| 130 | cx25840_write4(client, 0x904, 0x55550108); |
| 131 | cx25840_write4(client, 0x90c, 0x55550108); |
| 132 | break; |
| 133 | } |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | /* deassert soft reset */ |
| 137 | cx25840_and_or(client, 0x810, ~0x1, 0x00); |
| 138 | |
| 139 | state->audclk_freq = freq; |
| 140 | |
| 141 | return 0; |
| 142 | } |
| 143 | |
Hans Verkuil | a8bbf12 | 2006-01-09 15:25:42 -0200 | [diff] [blame] | 144 | void cx25840_audio_set_path(struct i2c_client *client) |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 145 | { |
| 146 | struct cx25840_state *state = i2c_get_clientdata(client); |
| 147 | |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 148 | /* stop microcontroller */ |
| 149 | cx25840_and_or(client, 0x803, ~0x10, 0); |
| 150 | |
| 151 | /* Mute everything to prevent the PFFT! */ |
| 152 | cx25840_write(client, 0x8d3, 0x1f); |
| 153 | |
Hans Verkuil | a8bbf12 | 2006-01-09 15:25:42 -0200 | [diff] [blame] | 154 | if (state->aud_input == CX25840_AUDIO_SERIAL) { |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 155 | /* Set Path1 to Serial Audio Input */ |
| 156 | cx25840_write4(client, 0x8d0, 0x12100101); |
| 157 | |
| 158 | /* The microcontroller should not be started for the |
| 159 | * non-tuner inputs: autodetection is specific for |
| 160 | * TV audio. */ |
Hans Verkuil | a8bbf12 | 2006-01-09 15:25:42 -0200 | [diff] [blame] | 161 | } else { |
| 162 | /* Set Path1 to Analog Demod Main Channel */ |
| 163 | cx25840_write4(client, 0x8d0, 0x7038061f); |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 164 | |
Hans Verkuil | a8bbf12 | 2006-01-09 15:25:42 -0200 | [diff] [blame] | 165 | /* When the microcontroller detects the |
| 166 | * audio format, it will unmute the lines */ |
| 167 | cx25840_and_or(client, 0x803, ~0x10, 0x10); |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 168 | } |
| 169 | |
Hans Verkuil | a8bbf12 | 2006-01-09 15:25:42 -0200 | [diff] [blame] | 170 | set_audclk_freq(client, state->audclk_freq); |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 171 | } |
| 172 | |
Hans Verkuil | d92c20e | 2006-01-09 15:32:41 -0200 | [diff] [blame] | 173 | static int get_volume(struct i2c_client *client) |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 174 | { |
| 175 | /* Volume runs +18dB to -96dB in 1/2dB steps |
| 176 | * change to fit the msp3400 -114dB to +12dB range */ |
| 177 | |
| 178 | /* check PATH1_VOLUME */ |
| 179 | int vol = 228 - cx25840_read(client, 0x8d4); |
| 180 | vol = (vol / 2) + 23; |
| 181 | return vol << 9; |
| 182 | } |
| 183 | |
Hans Verkuil | d92c20e | 2006-01-09 15:32:41 -0200 | [diff] [blame] | 184 | static void set_volume(struct i2c_client *client, int volume) |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 185 | { |
| 186 | /* First convert the volume to msp3400 values (0-127) */ |
| 187 | int vol = volume >> 9; |
| 188 | /* now scale it up to cx25840 values |
| 189 | * -114dB to -96dB maps to 0 |
| 190 | * this should be 19, but in my testing that was 4dB too loud */ |
| 191 | if (vol <= 23) { |
| 192 | vol = 0; |
| 193 | } else { |
| 194 | vol -= 23; |
| 195 | } |
| 196 | |
| 197 | /* PATH1_VOLUME */ |
| 198 | cx25840_write(client, 0x8d4, 228 - (vol * 2)); |
| 199 | } |
| 200 | |
Hans Verkuil | d92c20e | 2006-01-09 15:32:41 -0200 | [diff] [blame] | 201 | static int get_bass(struct i2c_client *client) |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 202 | { |
| 203 | /* bass is 49 steps +12dB to -12dB */ |
| 204 | |
| 205 | /* check PATH1_EQ_BASS_VOL */ |
| 206 | int bass = cx25840_read(client, 0x8d9) & 0x3f; |
| 207 | bass = (((48 - bass) * 0xffff) + 47) / 48; |
| 208 | return bass; |
| 209 | } |
| 210 | |
Hans Verkuil | d92c20e | 2006-01-09 15:32:41 -0200 | [diff] [blame] | 211 | static void set_bass(struct i2c_client *client, int bass) |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 212 | { |
| 213 | /* PATH1_EQ_BASS_VOL */ |
| 214 | cx25840_and_or(client, 0x8d9, ~0x3f, 48 - (bass * 48 / 0xffff)); |
| 215 | } |
| 216 | |
Hans Verkuil | d92c20e | 2006-01-09 15:32:41 -0200 | [diff] [blame] | 217 | static int get_treble(struct i2c_client *client) |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 218 | { |
| 219 | /* treble is 49 steps +12dB to -12dB */ |
| 220 | |
| 221 | /* check PATH1_EQ_TREBLE_VOL */ |
| 222 | int treble = cx25840_read(client, 0x8db) & 0x3f; |
| 223 | treble = (((48 - treble) * 0xffff) + 47) / 48; |
| 224 | return treble; |
| 225 | } |
| 226 | |
Hans Verkuil | d92c20e | 2006-01-09 15:32:41 -0200 | [diff] [blame] | 227 | static void set_treble(struct i2c_client *client, int treble) |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 228 | { |
| 229 | /* PATH1_EQ_TREBLE_VOL */ |
| 230 | cx25840_and_or(client, 0x8db, ~0x3f, 48 - (treble * 48 / 0xffff)); |
| 231 | } |
| 232 | |
Hans Verkuil | d92c20e | 2006-01-09 15:32:41 -0200 | [diff] [blame] | 233 | static int get_balance(struct i2c_client *client) |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 234 | { |
| 235 | /* balance is 7 bit, 0 to -96dB */ |
| 236 | |
| 237 | /* check PATH1_BAL_LEVEL */ |
| 238 | int balance = cx25840_read(client, 0x8d5) & 0x7f; |
| 239 | /* check PATH1_BAL_LEFT */ |
| 240 | if ((cx25840_read(client, 0x8d5) & 0x80) == 0) |
| 241 | balance = 0x80 - balance; |
| 242 | else |
| 243 | balance = 0x80 + balance; |
| 244 | return balance << 8; |
| 245 | } |
| 246 | |
Hans Verkuil | d92c20e | 2006-01-09 15:32:41 -0200 | [diff] [blame] | 247 | static void set_balance(struct i2c_client *client, int balance) |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 248 | { |
| 249 | int bal = balance >> 8; |
| 250 | if (bal > 0x80) { |
| 251 | /* PATH1_BAL_LEFT */ |
| 252 | cx25840_and_or(client, 0x8d5, 0x7f, 0x80); |
| 253 | /* PATH1_BAL_LEVEL */ |
| 254 | cx25840_and_or(client, 0x8d5, ~0x7f, bal & 0x7f); |
| 255 | } else { |
| 256 | /* PATH1_BAL_LEFT */ |
| 257 | cx25840_and_or(client, 0x8d5, 0x7f, 0x00); |
| 258 | /* PATH1_BAL_LEVEL */ |
| 259 | cx25840_and_or(client, 0x8d5, ~0x7f, 0x80 - bal); |
| 260 | } |
| 261 | } |
| 262 | |
Hans Verkuil | d92c20e | 2006-01-09 15:32:41 -0200 | [diff] [blame] | 263 | static int get_mute(struct i2c_client *client) |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 264 | { |
| 265 | /* check SRC1_MUTE_EN */ |
| 266 | return cx25840_read(client, 0x8d3) & 0x2 ? 1 : 0; |
| 267 | } |
| 268 | |
Hans Verkuil | d92c20e | 2006-01-09 15:32:41 -0200 | [diff] [blame] | 269 | static void set_mute(struct i2c_client *client, int mute) |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 270 | { |
| 271 | struct cx25840_state *state = i2c_get_clientdata(client); |
| 272 | |
Hans Verkuil | a8bbf12 | 2006-01-09 15:25:42 -0200 | [diff] [blame] | 273 | if (state->aud_input != CX25840_AUDIO_SERIAL) { |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 274 | /* Must turn off microcontroller in order to mute sound. |
| 275 | * Not sure if this is the best method, but it does work. |
| 276 | * If the microcontroller is running, then it will undo any |
| 277 | * changes to the mute register. */ |
| 278 | if (mute) { |
| 279 | /* disable microcontroller */ |
| 280 | cx25840_and_or(client, 0x803, ~0x10, 0x00); |
| 281 | cx25840_write(client, 0x8d3, 0x1f); |
| 282 | } else { |
| 283 | /* enable microcontroller */ |
| 284 | cx25840_and_or(client, 0x803, ~0x10, 0x10); |
| 285 | } |
| 286 | } else { |
| 287 | /* SRC1_MUTE_EN */ |
| 288 | cx25840_and_or(client, 0x8d3, ~0x2, mute ? 0x02 : 0x00); |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | int cx25840_audio(struct i2c_client *client, unsigned int cmd, void *arg) |
| 293 | { |
| 294 | struct v4l2_control *ctrl = arg; |
| 295 | |
| 296 | switch (cmd) { |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 297 | case VIDIOC_INT_AUDIO_CLOCK_FREQ: |
Hans Verkuil | 3578d3d | 2006-01-09 15:25:41 -0200 | [diff] [blame] | 298 | return set_audclk_freq(client, *(u32 *)arg); |
Hans Verkuil | a8bbf12 | 2006-01-09 15:25:42 -0200 | [diff] [blame] | 299 | |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 300 | case VIDIOC_G_CTRL: |
| 301 | switch (ctrl->id) { |
| 302 | case V4L2_CID_AUDIO_VOLUME: |
| 303 | ctrl->value = get_volume(client); |
| 304 | break; |
| 305 | case V4L2_CID_AUDIO_BASS: |
| 306 | ctrl->value = get_bass(client); |
| 307 | break; |
| 308 | case V4L2_CID_AUDIO_TREBLE: |
| 309 | ctrl->value = get_treble(client); |
| 310 | break; |
| 311 | case V4L2_CID_AUDIO_BALANCE: |
| 312 | ctrl->value = get_balance(client); |
| 313 | break; |
| 314 | case V4L2_CID_AUDIO_MUTE: |
| 315 | ctrl->value = get_mute(client); |
| 316 | break; |
| 317 | default: |
| 318 | return -EINVAL; |
| 319 | } |
| 320 | break; |
Hans Verkuil | a8bbf12 | 2006-01-09 15:25:42 -0200 | [diff] [blame] | 321 | |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 322 | case VIDIOC_S_CTRL: |
| 323 | switch (ctrl->id) { |
| 324 | case V4L2_CID_AUDIO_VOLUME: |
| 325 | set_volume(client, ctrl->value); |
| 326 | break; |
| 327 | case V4L2_CID_AUDIO_BASS: |
| 328 | set_bass(client, ctrl->value); |
| 329 | break; |
| 330 | case V4L2_CID_AUDIO_TREBLE: |
| 331 | set_treble(client, ctrl->value); |
| 332 | break; |
| 333 | case V4L2_CID_AUDIO_BALANCE: |
| 334 | set_balance(client, ctrl->value); |
| 335 | break; |
| 336 | case V4L2_CID_AUDIO_MUTE: |
| 337 | set_mute(client, ctrl->value); |
| 338 | break; |
| 339 | default: |
| 340 | return -EINVAL; |
| 341 | } |
| 342 | break; |
Hans Verkuil | a8bbf12 | 2006-01-09 15:25:42 -0200 | [diff] [blame] | 343 | |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 344 | default: |
| 345 | return -EINVAL; |
| 346 | } |
| 347 | |
| 348 | return 0; |
| 349 | } |