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