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