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 | /* common for all inputs and rates */ |
| 34 | /* SA_MCLK_SEL=1, SA_MCLK_DIV=0x10 */ |
Steven Toth | f234081 | 2008-01-10 01:22:39 -0300 | [diff] [blame] | 35 | if (!state->is_cx23885) |
| 36 | cx25840_write(client, 0x127, 0x50); |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 37 | |
Hans Verkuil | a8bbf12 | 2006-01-09 15:25:42 -0200 | [diff] [blame] | 38 | if (state->aud_input != CX25840_AUDIO_SERIAL) { |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 39 | switch (freq) { |
Hans Verkuil | 3578d3d | 2006-01-09 15:25:41 -0200 | [diff] [blame] | 40 | case 32000: |
Steven Toth | f234081 | 2008-01-10 01:22:39 -0300 | [diff] [blame] | 41 | if (state->is_cx23885) { |
| 42 | /* We don't have register values |
| 43 | * so avoid destroying registers. */ |
| 44 | break; |
| 45 | } |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 46 | /* VID_PLL and AUX_PLL */ |
Hans Verkuil | 4a56eb3 | 2007-12-02 07:03:45 -0300 | [diff] [blame] | 47 | cx25840_write4(client, 0x108, 0x1006040f); |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 48 | |
| 49 | /* AUX_PLL_FRAC */ |
Hans Verkuil | 4a56eb3 | 2007-12-02 07:03:45 -0300 | [diff] [blame] | 50 | cx25840_write4(client, 0x110, 0x01bb39ee); |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 51 | |
Hans Verkuil | e2b8cf4 | 2006-04-22 10:22:46 -0300 | [diff] [blame] | 52 | if (state->is_cx25836) |
| 53 | break; |
| 54 | |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 55 | /* src3/4/6_ctl = 0x0801f77f */ |
Hans Verkuil | 4a56eb3 | 2007-12-02 07:03:45 -0300 | [diff] [blame] | 56 | cx25840_write4(client, 0x900, 0x0801f77f); |
| 57 | cx25840_write4(client, 0x904, 0x0801f77f); |
| 58 | cx25840_write4(client, 0x90c, 0x0801f77f); |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 59 | break; |
| 60 | |
Hans Verkuil | 3578d3d | 2006-01-09 15:25:41 -0200 | [diff] [blame] | 61 | case 44100: |
Steven Toth | f234081 | 2008-01-10 01:22:39 -0300 | [diff] [blame] | 62 | if (state->is_cx23885) { |
| 63 | /* We don't have register values |
| 64 | * so avoid destroying registers. */ |
| 65 | break; |
| 66 | } |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 67 | /* VID_PLL and AUX_PLL */ |
Hans Verkuil | 4a56eb3 | 2007-12-02 07:03:45 -0300 | [diff] [blame] | 68 | cx25840_write4(client, 0x108, 0x1009040f); |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 69 | |
| 70 | /* AUX_PLL_FRAC */ |
Hans Verkuil | 4a56eb3 | 2007-12-02 07:03:45 -0300 | [diff] [blame] | 71 | cx25840_write4(client, 0x110, 0x00ec6bd6); |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 72 | |
Hans Verkuil | e2b8cf4 | 2006-04-22 10:22:46 -0300 | [diff] [blame] | 73 | if (state->is_cx25836) |
| 74 | break; |
| 75 | |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 76 | /* src3/4/6_ctl = 0x08016d59 */ |
Hans Verkuil | 4a56eb3 | 2007-12-02 07:03:45 -0300 | [diff] [blame] | 77 | cx25840_write4(client, 0x900, 0x08016d59); |
| 78 | cx25840_write4(client, 0x904, 0x08016d59); |
| 79 | cx25840_write4(client, 0x90c, 0x08016d59); |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 80 | break; |
| 81 | |
Hans Verkuil | 3578d3d | 2006-01-09 15:25:41 -0200 | [diff] [blame] | 82 | case 48000: |
Steven Toth | f234081 | 2008-01-10 01:22:39 -0300 | [diff] [blame] | 83 | if (state->is_cx23885) { |
| 84 | /* We don't have register values |
| 85 | * so avoid destroying registers. */ |
| 86 | break; |
| 87 | } |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 88 | /* VID_PLL and AUX_PLL */ |
Hans Verkuil | 4a56eb3 | 2007-12-02 07:03:45 -0300 | [diff] [blame] | 89 | cx25840_write4(client, 0x108, 0x100a040f); |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 90 | |
| 91 | /* AUX_PLL_FRAC */ |
Hans Verkuil | 4a56eb3 | 2007-12-02 07:03:45 -0300 | [diff] [blame] | 92 | cx25840_write4(client, 0x110, 0x0098d6e5); |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 93 | |
Hans Verkuil | e2b8cf4 | 2006-04-22 10:22:46 -0300 | [diff] [blame] | 94 | if (state->is_cx25836) |
| 95 | break; |
| 96 | |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 97 | /* src3/4/6_ctl = 0x08014faa */ |
Hans Verkuil | 4a56eb3 | 2007-12-02 07:03:45 -0300 | [diff] [blame] | 98 | cx25840_write4(client, 0x900, 0x08014faa); |
| 99 | cx25840_write4(client, 0x904, 0x08014faa); |
| 100 | cx25840_write4(client, 0x90c, 0x08014faa); |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 101 | break; |
| 102 | } |
Hans Verkuil | a8bbf12 | 2006-01-09 15:25:42 -0200 | [diff] [blame] | 103 | } else { |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 104 | switch (freq) { |
Hans Verkuil | 3578d3d | 2006-01-09 15:25:41 -0200 | [diff] [blame] | 105 | case 32000: |
Steven Toth | f234081 | 2008-01-10 01:22:39 -0300 | [diff] [blame] | 106 | if (state->is_cx23885) { |
| 107 | /* We don't have register values |
| 108 | * so avoid destroying registers. */ |
| 109 | break; |
| 110 | } |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 111 | /* VID_PLL and AUX_PLL */ |
Hans Verkuil | 4a56eb3 | 2007-12-02 07:03:45 -0300 | [diff] [blame] | 112 | cx25840_write4(client, 0x108, 0x1e08040f); |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 113 | |
| 114 | /* AUX_PLL_FRAC */ |
Hans Verkuil | 4a56eb3 | 2007-12-02 07:03:45 -0300 | [diff] [blame] | 115 | cx25840_write4(client, 0x110, 0x012a0869); |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 116 | |
Hans Verkuil | e2b8cf4 | 2006-04-22 10:22:46 -0300 | [diff] [blame] | 117 | if (state->is_cx25836) |
| 118 | break; |
| 119 | |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 120 | /* src1_ctl = 0x08010000 */ |
Hans Verkuil | 4a56eb3 | 2007-12-02 07:03:45 -0300 | [diff] [blame] | 121 | cx25840_write4(client, 0x8f8, 0x08010000); |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 122 | |
| 123 | /* src3/4/6_ctl = 0x08020000 */ |
Hans Verkuil | 4a56eb3 | 2007-12-02 07:03:45 -0300 | [diff] [blame] | 124 | cx25840_write4(client, 0x900, 0x08020000); |
| 125 | cx25840_write4(client, 0x904, 0x08020000); |
| 126 | cx25840_write4(client, 0x90c, 0x08020000); |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 127 | |
| 128 | /* SA_MCLK_SEL=1, SA_MCLK_DIV=0x14 */ |
| 129 | cx25840_write(client, 0x127, 0x54); |
| 130 | break; |
| 131 | |
Hans Verkuil | 3578d3d | 2006-01-09 15:25:41 -0200 | [diff] [blame] | 132 | case 44100: |
Steven Toth | f234081 | 2008-01-10 01:22:39 -0300 | [diff] [blame] | 133 | if (state->is_cx23885) { |
| 134 | /* We don't have register values |
| 135 | * so avoid destroying registers. */ |
| 136 | break; |
| 137 | } |
| 138 | |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 139 | /* VID_PLL and AUX_PLL */ |
Hans Verkuil | 4a56eb3 | 2007-12-02 07:03:45 -0300 | [diff] [blame] | 140 | cx25840_write4(client, 0x108, 0x1809040f); |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 141 | |
| 142 | /* AUX_PLL_FRAC */ |
Hans Verkuil | 4a56eb3 | 2007-12-02 07:03:45 -0300 | [diff] [blame] | 143 | cx25840_write4(client, 0x110, 0x00ec6bd6); |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 144 | |
Hans Verkuil | e2b8cf4 | 2006-04-22 10:22:46 -0300 | [diff] [blame] | 145 | if (state->is_cx25836) |
| 146 | break; |
| 147 | |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 148 | /* src1_ctl = 0x08010000 */ |
Hans Verkuil | 4a56eb3 | 2007-12-02 07:03:45 -0300 | [diff] [blame] | 149 | cx25840_write4(client, 0x8f8, 0x080160cd); |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 150 | |
| 151 | /* src3/4/6_ctl = 0x08020000 */ |
Hans Verkuil | 4a56eb3 | 2007-12-02 07:03:45 -0300 | [diff] [blame] | 152 | cx25840_write4(client, 0x900, 0x08017385); |
| 153 | cx25840_write4(client, 0x904, 0x08017385); |
| 154 | cx25840_write4(client, 0x90c, 0x08017385); |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 155 | break; |
| 156 | |
Hans Verkuil | 3578d3d | 2006-01-09 15:25:41 -0200 | [diff] [blame] | 157 | case 48000: |
Steven Toth | f234081 | 2008-01-10 01:22:39 -0300 | [diff] [blame] | 158 | if (!state->is_cx23885) { |
| 159 | /* VID_PLL and AUX_PLL */ |
| 160 | cx25840_write4(client, 0x108, 0x180a040f); |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 161 | |
Steven Toth | f234081 | 2008-01-10 01:22:39 -0300 | [diff] [blame] | 162 | /* AUX_PLL_FRAC */ |
| 163 | cx25840_write4(client, 0x110, 0x0098d6e5); |
| 164 | } |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 165 | |
Hans Verkuil | e2b8cf4 | 2006-04-22 10:22:46 -0300 | [diff] [blame] | 166 | if (state->is_cx25836) |
| 167 | break; |
| 168 | |
Steven Toth | f234081 | 2008-01-10 01:22:39 -0300 | [diff] [blame] | 169 | if (!state->is_cx23885) { |
| 170 | /* src1_ctl */ |
| 171 | cx25840_write4(client, 0x8f8, 0x08018000); |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 172 | |
Steven Toth | f234081 | 2008-01-10 01:22:39 -0300 | [diff] [blame] | 173 | /* src3/4/6_ctl */ |
| 174 | cx25840_write4(client, 0x900, 0x08015555); |
| 175 | cx25840_write4(client, 0x904, 0x08015555); |
| 176 | cx25840_write4(client, 0x90c, 0x08015555); |
| 177 | } else { |
| 178 | |
| 179 | cx25840_write4(client, 0x8f8, 0x0801867c); |
| 180 | |
| 181 | cx25840_write4(client, 0x900, 0x08014faa); |
| 182 | cx25840_write4(client, 0x904, 0x08014faa); |
| 183 | cx25840_write4(client, 0x90c, 0x08014faa); |
| 184 | } |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 185 | break; |
| 186 | } |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 187 | } |
| 188 | |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 189 | state->audclk_freq = freq; |
| 190 | |
| 191 | return 0; |
| 192 | } |
| 193 | |
Hans Verkuil | a8bbf12 | 2006-01-09 15:25:42 -0200 | [diff] [blame] | 194 | void cx25840_audio_set_path(struct i2c_client *client) |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 195 | { |
| 196 | struct cx25840_state *state = i2c_get_clientdata(client); |
| 197 | |
Hans Verkuil | 8267761 | 2007-08-07 07:16:07 -0300 | [diff] [blame] | 198 | /* assert soft reset */ |
| 199 | cx25840_and_or(client, 0x810, ~0x1, 0x01); |
| 200 | |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 201 | /* stop microcontroller */ |
| 202 | cx25840_and_or(client, 0x803, ~0x10, 0); |
| 203 | |
| 204 | /* Mute everything to prevent the PFFT! */ |
| 205 | cx25840_write(client, 0x8d3, 0x1f); |
| 206 | |
Hans Verkuil | a8bbf12 | 2006-01-09 15:25:42 -0200 | [diff] [blame] | 207 | if (state->aud_input == CX25840_AUDIO_SERIAL) { |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 208 | /* Set Path1 to Serial Audio Input */ |
Hans Verkuil | 4a56eb3 | 2007-12-02 07:03:45 -0300 | [diff] [blame] | 209 | cx25840_write4(client, 0x8d0, 0x01011012); |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 210 | |
| 211 | /* The microcontroller should not be started for the |
| 212 | * non-tuner inputs: autodetection is specific for |
| 213 | * TV audio. */ |
Hans Verkuil | a8bbf12 | 2006-01-09 15:25:42 -0200 | [diff] [blame] | 214 | } else { |
| 215 | /* Set Path1 to Analog Demod Main Channel */ |
Hans Verkuil | 4a56eb3 | 2007-12-02 07:03:45 -0300 | [diff] [blame] | 216 | cx25840_write4(client, 0x8d0, 0x1f063870); |
Hans Verkuil | c0c044a | 2006-04-29 12:11:18 -0300 | [diff] [blame] | 217 | } |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 218 | |
Hans Verkuil | c0c044a | 2006-04-29 12:11:18 -0300 | [diff] [blame] | 219 | set_audclk_freq(client, state->audclk_freq); |
| 220 | |
| 221 | if (state->aud_input != CX25840_AUDIO_SERIAL) { |
Hans Verkuil | a8bbf12 | 2006-01-09 15:25:42 -0200 | [diff] [blame] | 222 | /* When the microcontroller detects the |
| 223 | * audio format, it will unmute the lines */ |
| 224 | cx25840_and_or(client, 0x803, ~0x10, 0x10); |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 225 | } |
Hans Verkuil | 8267761 | 2007-08-07 07:16:07 -0300 | [diff] [blame] | 226 | |
| 227 | /* deassert soft reset */ |
| 228 | cx25840_and_or(client, 0x810, ~0x1, 0x00); |
Steven Toth | f234081 | 2008-01-10 01:22:39 -0300 | [diff] [blame] | 229 | |
| 230 | if (state->is_cx23885) { |
| 231 | /* Ensure the controller is running when we exit */ |
| 232 | cx25840_and_or(client, 0x803, ~0x10, 0x10); |
| 233 | } |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 234 | } |
| 235 | |
Hans Verkuil | d92c20e | 2006-01-09 15:32:41 -0200 | [diff] [blame] | 236 | static int get_volume(struct i2c_client *client) |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 237 | { |
Hans Verkuil | 87410da | 2007-08-05 08:00:36 -0300 | [diff] [blame] | 238 | struct cx25840_state *state = i2c_get_clientdata(client); |
| 239 | int vol; |
| 240 | |
| 241 | if (state->unmute_volume >= 0) |
| 242 | return state->unmute_volume; |
| 243 | |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 244 | /* Volume runs +18dB to -96dB in 1/2dB steps |
| 245 | * change to fit the msp3400 -114dB to +12dB range */ |
| 246 | |
| 247 | /* check PATH1_VOLUME */ |
Hans Verkuil | 87410da | 2007-08-05 08:00:36 -0300 | [diff] [blame] | 248 | vol = 228 - cx25840_read(client, 0x8d4); |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 249 | vol = (vol / 2) + 23; |
| 250 | return vol << 9; |
| 251 | } |
| 252 | |
Hans Verkuil | d92c20e | 2006-01-09 15:32:41 -0200 | [diff] [blame] | 253 | static void set_volume(struct i2c_client *client, int volume) |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 254 | { |
Hans Verkuil | 87410da | 2007-08-05 08:00:36 -0300 | [diff] [blame] | 255 | struct cx25840_state *state = i2c_get_clientdata(client); |
| 256 | int vol; |
| 257 | |
| 258 | if (state->unmute_volume >= 0) { |
| 259 | state->unmute_volume = volume; |
| 260 | return; |
| 261 | } |
| 262 | |
| 263 | /* Convert the volume to msp3400 values (0-127) */ |
| 264 | vol = volume >> 9; |
| 265 | |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 266 | /* now scale it up to cx25840 values |
| 267 | * -114dB to -96dB maps to 0 |
| 268 | * this should be 19, but in my testing that was 4dB too loud */ |
| 269 | if (vol <= 23) { |
| 270 | vol = 0; |
| 271 | } else { |
| 272 | vol -= 23; |
| 273 | } |
| 274 | |
| 275 | /* PATH1_VOLUME */ |
| 276 | cx25840_write(client, 0x8d4, 228 - (vol * 2)); |
| 277 | } |
| 278 | |
Hans Verkuil | d92c20e | 2006-01-09 15:32:41 -0200 | [diff] [blame] | 279 | static int get_bass(struct i2c_client *client) |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 280 | { |
| 281 | /* bass is 49 steps +12dB to -12dB */ |
| 282 | |
| 283 | /* check PATH1_EQ_BASS_VOL */ |
| 284 | int bass = cx25840_read(client, 0x8d9) & 0x3f; |
| 285 | bass = (((48 - bass) * 0xffff) + 47) / 48; |
| 286 | return bass; |
| 287 | } |
| 288 | |
Hans Verkuil | d92c20e | 2006-01-09 15:32:41 -0200 | [diff] [blame] | 289 | static void set_bass(struct i2c_client *client, int bass) |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 290 | { |
| 291 | /* PATH1_EQ_BASS_VOL */ |
| 292 | cx25840_and_or(client, 0x8d9, ~0x3f, 48 - (bass * 48 / 0xffff)); |
| 293 | } |
| 294 | |
Hans Verkuil | d92c20e | 2006-01-09 15:32:41 -0200 | [diff] [blame] | 295 | static int get_treble(struct i2c_client *client) |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 296 | { |
| 297 | /* treble is 49 steps +12dB to -12dB */ |
| 298 | |
| 299 | /* check PATH1_EQ_TREBLE_VOL */ |
| 300 | int treble = cx25840_read(client, 0x8db) & 0x3f; |
| 301 | treble = (((48 - treble) * 0xffff) + 47) / 48; |
| 302 | return treble; |
| 303 | } |
| 304 | |
Hans Verkuil | d92c20e | 2006-01-09 15:32:41 -0200 | [diff] [blame] | 305 | static void set_treble(struct i2c_client *client, int treble) |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 306 | { |
| 307 | /* PATH1_EQ_TREBLE_VOL */ |
| 308 | cx25840_and_or(client, 0x8db, ~0x3f, 48 - (treble * 48 / 0xffff)); |
| 309 | } |
| 310 | |
Hans Verkuil | d92c20e | 2006-01-09 15:32:41 -0200 | [diff] [blame] | 311 | static int get_balance(struct i2c_client *client) |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 312 | { |
| 313 | /* balance is 7 bit, 0 to -96dB */ |
| 314 | |
| 315 | /* check PATH1_BAL_LEVEL */ |
| 316 | int balance = cx25840_read(client, 0x8d5) & 0x7f; |
| 317 | /* check PATH1_BAL_LEFT */ |
| 318 | if ((cx25840_read(client, 0x8d5) & 0x80) == 0) |
| 319 | balance = 0x80 - balance; |
| 320 | else |
| 321 | balance = 0x80 + balance; |
| 322 | return balance << 8; |
| 323 | } |
| 324 | |
Hans Verkuil | d92c20e | 2006-01-09 15:32:41 -0200 | [diff] [blame] | 325 | static void set_balance(struct i2c_client *client, int balance) |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 326 | { |
| 327 | int bal = balance >> 8; |
| 328 | if (bal > 0x80) { |
| 329 | /* PATH1_BAL_LEFT */ |
| 330 | cx25840_and_or(client, 0x8d5, 0x7f, 0x80); |
| 331 | /* PATH1_BAL_LEVEL */ |
| 332 | cx25840_and_or(client, 0x8d5, ~0x7f, bal & 0x7f); |
| 333 | } else { |
| 334 | /* PATH1_BAL_LEFT */ |
| 335 | cx25840_and_or(client, 0x8d5, 0x7f, 0x00); |
| 336 | /* PATH1_BAL_LEVEL */ |
| 337 | cx25840_and_or(client, 0x8d5, ~0x7f, 0x80 - bal); |
| 338 | } |
| 339 | } |
| 340 | |
Hans Verkuil | d92c20e | 2006-01-09 15:32:41 -0200 | [diff] [blame] | 341 | static int get_mute(struct i2c_client *client) |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 342 | { |
Hans Verkuil | 87410da | 2007-08-05 08:00:36 -0300 | [diff] [blame] | 343 | struct cx25840_state *state = i2c_get_clientdata(client); |
| 344 | |
| 345 | return state->unmute_volume >= 0; |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 346 | } |
| 347 | |
Hans Verkuil | d92c20e | 2006-01-09 15:32:41 -0200 | [diff] [blame] | 348 | static void set_mute(struct i2c_client *client, int mute) |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 349 | { |
| 350 | struct cx25840_state *state = i2c_get_clientdata(client); |
| 351 | |
Hans Verkuil | 87410da | 2007-08-05 08:00:36 -0300 | [diff] [blame] | 352 | if (mute && state->unmute_volume == -1) { |
| 353 | int vol = get_volume(client); |
| 354 | |
| 355 | set_volume(client, 0); |
| 356 | state->unmute_volume = vol; |
| 357 | } |
| 358 | else if (!mute && state->unmute_volume != -1) { |
| 359 | int vol = state->unmute_volume; |
| 360 | |
| 361 | state->unmute_volume = -1; |
| 362 | set_volume(client, vol); |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 363 | } |
| 364 | } |
| 365 | |
| 366 | int cx25840_audio(struct i2c_client *client, unsigned int cmd, void *arg) |
| 367 | { |
Hans Verkuil | c0c044a | 2006-04-29 12:11:18 -0300 | [diff] [blame] | 368 | struct cx25840_state *state = i2c_get_clientdata(client); |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 369 | struct v4l2_control *ctrl = arg; |
Hans Verkuil | c0c044a | 2006-04-29 12:11:18 -0300 | [diff] [blame] | 370 | int retval; |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 371 | |
| 372 | switch (cmd) { |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 373 | case VIDIOC_INT_AUDIO_CLOCK_FREQ: |
Hans Verkuil | 8267761 | 2007-08-07 07:16:07 -0300 | [diff] [blame] | 374 | if (!state->is_cx25836) |
| 375 | cx25840_and_or(client, 0x810, ~0x1, 1); |
Hans Verkuil | c0c044a | 2006-04-29 12:11:18 -0300 | [diff] [blame] | 376 | if (state->aud_input != CX25840_AUDIO_SERIAL) { |
| 377 | cx25840_and_or(client, 0x803, ~0x10, 0); |
| 378 | cx25840_write(client, 0x8d3, 0x1f); |
| 379 | } |
| 380 | retval = set_audclk_freq(client, *(u32 *)arg); |
| 381 | if (state->aud_input != CX25840_AUDIO_SERIAL) { |
| 382 | cx25840_and_or(client, 0x803, ~0x10, 0x10); |
| 383 | } |
Hans Verkuil | 8267761 | 2007-08-07 07:16:07 -0300 | [diff] [blame] | 384 | if (!state->is_cx25836) |
| 385 | cx25840_and_or(client, 0x810, ~0x1, 0); |
Hans Verkuil | c0c044a | 2006-04-29 12:11:18 -0300 | [diff] [blame] | 386 | return retval; |
Hans Verkuil | a8bbf12 | 2006-01-09 15:25:42 -0200 | [diff] [blame] | 387 | |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 388 | case VIDIOC_G_CTRL: |
| 389 | switch (ctrl->id) { |
| 390 | case V4L2_CID_AUDIO_VOLUME: |
| 391 | ctrl->value = get_volume(client); |
| 392 | break; |
| 393 | case V4L2_CID_AUDIO_BASS: |
| 394 | ctrl->value = get_bass(client); |
| 395 | break; |
| 396 | case V4L2_CID_AUDIO_TREBLE: |
| 397 | ctrl->value = get_treble(client); |
| 398 | break; |
| 399 | case V4L2_CID_AUDIO_BALANCE: |
| 400 | ctrl->value = get_balance(client); |
| 401 | break; |
| 402 | case V4L2_CID_AUDIO_MUTE: |
| 403 | ctrl->value = get_mute(client); |
| 404 | break; |
| 405 | default: |
| 406 | return -EINVAL; |
| 407 | } |
| 408 | break; |
Hans Verkuil | a8bbf12 | 2006-01-09 15:25:42 -0200 | [diff] [blame] | 409 | |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 410 | case VIDIOC_S_CTRL: |
| 411 | switch (ctrl->id) { |
| 412 | case V4L2_CID_AUDIO_VOLUME: |
| 413 | set_volume(client, ctrl->value); |
| 414 | break; |
| 415 | case V4L2_CID_AUDIO_BASS: |
| 416 | set_bass(client, ctrl->value); |
| 417 | break; |
| 418 | case V4L2_CID_AUDIO_TREBLE: |
| 419 | set_treble(client, ctrl->value); |
| 420 | break; |
| 421 | case V4L2_CID_AUDIO_BALANCE: |
| 422 | set_balance(client, ctrl->value); |
| 423 | break; |
| 424 | case V4L2_CID_AUDIO_MUTE: |
| 425 | set_mute(client, ctrl->value); |
| 426 | break; |
| 427 | default: |
| 428 | return -EINVAL; |
| 429 | } |
| 430 | break; |
Hans Verkuil | a8bbf12 | 2006-01-09 15:25:42 -0200 | [diff] [blame] | 431 | |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 432 | default: |
| 433 | return -EINVAL; |
| 434 | } |
| 435 | |
| 436 | return 0; |
| 437 | } |