Path: blob/master/drivers/media/video/cx25840/cx25840-audio.c
17745 views
/* cx25840 audio functions1*2* This program is free software; you can redistribute it and/or3* modify it under the terms of the GNU General Public License4* as published by the Free Software Foundation; either version 25* of the License, or (at your option) any later version.6*7* This program is distributed in the hope that it will be useful,8* but WITHOUT ANY WARRANTY; without even the implied warranty of9* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the10* GNU General Public License for more details.11*12* You should have received a copy of the GNU General Public License13* along with this program; if not, write to the Free Software14* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.15*/161718#include <linux/videodev2.h>19#include <linux/i2c.h>20#include <media/v4l2-common.h>21#include <media/cx25840.h>2223#include "cx25840-core.h"2425/*26* Note: The PLL and SRC parameters are based on a reference frequency that27* would ideally be:28*29* NTSC Color subcarrier freq * 8 = 4.5 MHz/286 * 455/2 * 8 = 28.63636363... MHz30*31* However, it's not the exact reference frequency that matters, only that the32* firmware and modules that comprise the driver for a particular board all33* use the same value (close to the ideal value).34*35* Comments below will note which reference frequency is assumed for various36* parameters. They will usually be one of37*38* ref_freq = 28.636360 MHz39* or40* ref_freq = 28.636363 MHz41*/4243static int cx25840_set_audclk_freq(struct i2c_client *client, u32 freq)44{45struct cx25840_state *state = to_state(i2c_get_clientdata(client));4647if (state->aud_input != CX25840_AUDIO_SERIAL) {48switch (freq) {49case 32000:50/*51* VID_PLL Integer = 0x0f, VID_PLL Post Divider = 0x0452* AUX_PLL Integer = 0x06, AUX PLL Post Divider = 0x1053*/54cx25840_write4(client, 0x108, 0x1006040f);5556/*57* VID_PLL Fraction (register 0x10c) = 0x2be2fe58* 28636360 * 0xf.15f17f0/4 = 108 MHz59* 432 MHz pre-postdivide60*/6162/*63* AUX_PLL Fraction = 0x1bb39ee64* 28636363 * 0x6.dd9cf70/0x10 = 32000 * 38465* 196.6 MHz pre-postdivide66* FIXME < 200 MHz is out of specified valid range67* FIXME 28636363 ref_freq doesn't match VID PLL ref68*/69cx25840_write4(client, 0x110, 0x01bb39ee);7071/*72* SA_MCLK_SEL = 173* SA_MCLK_DIV = 0x10 = 384/384 * AUX_PLL post dvivider74*/75cx25840_write(client, 0x127, 0x50);7677if (is_cx2583x(state))78break;7980/* src3/4/6_ctl */81/* 0x1.f77f = (4 * 28636360/8 * 2/455) / 32000 */82cx25840_write4(client, 0x900, 0x0801f77f);83cx25840_write4(client, 0x904, 0x0801f77f);84cx25840_write4(client, 0x90c, 0x0801f77f);85break;8687case 44100:88/*89* VID_PLL Integer = 0x0f, VID_PLL Post Divider = 0x0490* AUX_PLL Integer = 0x09, AUX PLL Post Divider = 0x1091*/92cx25840_write4(client, 0x108, 0x1009040f);9394/*95* VID_PLL Fraction (register 0x10c) = 0x2be2fe96* 28636360 * 0xf.15f17f0/4 = 108 MHz97* 432 MHz pre-postdivide98*/99100/*101* AUX_PLL Fraction = 0x0ec6bd6102* 28636363 * 0x9.7635eb0/0x10 = 44100 * 384103* 271 MHz pre-postdivide104* FIXME 28636363 ref_freq doesn't match VID PLL ref105*/106cx25840_write4(client, 0x110, 0x00ec6bd6);107108/*109* SA_MCLK_SEL = 1110* SA_MCLK_DIV = 0x10 = 384/384 * AUX_PLL post dvivider111*/112cx25840_write(client, 0x127, 0x50);113114if (is_cx2583x(state))115break;116117/* src3/4/6_ctl */118/* 0x1.6d59 = (4 * 28636360/8 * 2/455) / 44100 */119cx25840_write4(client, 0x900, 0x08016d59);120cx25840_write4(client, 0x904, 0x08016d59);121cx25840_write4(client, 0x90c, 0x08016d59);122break;123124case 48000:125/*126* VID_PLL Integer = 0x0f, VID_PLL Post Divider = 0x04127* AUX_PLL Integer = 0x0a, AUX PLL Post Divider = 0x10128*/129cx25840_write4(client, 0x108, 0x100a040f);130131/*132* VID_PLL Fraction (register 0x10c) = 0x2be2fe133* 28636360 * 0xf.15f17f0/4 = 108 MHz134* 432 MHz pre-postdivide135*/136137/*138* AUX_PLL Fraction = 0x098d6e5139* 28636363 * 0xa.4c6b728/0x10 = 48000 * 384140* 295 MHz pre-postdivide141* FIXME 28636363 ref_freq doesn't match VID PLL ref142*/143cx25840_write4(client, 0x110, 0x0098d6e5);144145/*146* SA_MCLK_SEL = 1147* SA_MCLK_DIV = 0x10 = 384/384 * AUX_PLL post dvivider148*/149cx25840_write(client, 0x127, 0x50);150151if (is_cx2583x(state))152break;153154/* src3/4/6_ctl */155/* 0x1.4faa = (4 * 28636360/8 * 2/455) / 48000 */156cx25840_write4(client, 0x900, 0x08014faa);157cx25840_write4(client, 0x904, 0x08014faa);158cx25840_write4(client, 0x90c, 0x08014faa);159break;160}161} else {162switch (freq) {163case 32000:164/*165* VID_PLL Integer = 0x0f, VID_PLL Post Divider = 0x04166* AUX_PLL Integer = 0x08, AUX PLL Post Divider = 0x1e167*/168cx25840_write4(client, 0x108, 0x1e08040f);169170/*171* VID_PLL Fraction (register 0x10c) = 0x2be2fe172* 28636360 * 0xf.15f17f0/4 = 108 MHz173* 432 MHz pre-postdivide174*/175176/*177* AUX_PLL Fraction = 0x12a0869178* 28636363 * 0x8.9504348/0x1e = 32000 * 256179* 246 MHz pre-postdivide180* FIXME 28636363 ref_freq doesn't match VID PLL ref181*/182cx25840_write4(client, 0x110, 0x012a0869);183184/*185* SA_MCLK_SEL = 1186* SA_MCLK_DIV = 0x14 = 256/384 * AUX_PLL post dvivider187*/188cx25840_write(client, 0x127, 0x54);189190if (is_cx2583x(state))191break;192193/* src1_ctl */194/* 0x1.0000 = 32000/32000 */195cx25840_write4(client, 0x8f8, 0x08010000);196197/* src3/4/6_ctl */198/* 0x2.0000 = 2 * (32000/32000) */199cx25840_write4(client, 0x900, 0x08020000);200cx25840_write4(client, 0x904, 0x08020000);201cx25840_write4(client, 0x90c, 0x08020000);202break;203204case 44100:205/*206* VID_PLL Integer = 0x0f, VID_PLL Post Divider = 0x04207* AUX_PLL Integer = 0x09, AUX PLL Post Divider = 0x18208*/209cx25840_write4(client, 0x108, 0x1809040f);210211/*212* VID_PLL Fraction (register 0x10c) = 0x2be2fe213* 28636360 * 0xf.15f17f0/4 = 108 MHz214* 432 MHz pre-postdivide215*/216217/*218* AUX_PLL Fraction = 0x0ec6bd6219* 28636363 * 0x9.7635eb0/0x18 = 44100 * 256220* 271 MHz pre-postdivide221* FIXME 28636363 ref_freq doesn't match VID PLL ref222*/223cx25840_write4(client, 0x110, 0x00ec6bd6);224225/*226* SA_MCLK_SEL = 1227* SA_MCLK_DIV = 0x10 = 256/384 * AUX_PLL post dvivider228*/229cx25840_write(client, 0x127, 0x50);230231if (is_cx2583x(state))232break;233234/* src1_ctl */235/* 0x1.60cd = 44100/32000 */236cx25840_write4(client, 0x8f8, 0x080160cd);237238/* src3/4/6_ctl */239/* 0x1.7385 = 2 * (32000/44100) */240cx25840_write4(client, 0x900, 0x08017385);241cx25840_write4(client, 0x904, 0x08017385);242cx25840_write4(client, 0x90c, 0x08017385);243break;244245case 48000:246/*247* VID_PLL Integer = 0x0f, VID_PLL Post Divider = 0x04248* AUX_PLL Integer = 0x0a, AUX PLL Post Divider = 0x18249*/250cx25840_write4(client, 0x108, 0x180a040f);251252/*253* VID_PLL Fraction (register 0x10c) = 0x2be2fe254* 28636360 * 0xf.15f17f0/4 = 108 MHz255* 432 MHz pre-postdivide256*/257258/*259* AUX_PLL Fraction = 0x098d6e5260* 28636363 * 0xa.4c6b728/0x18 = 48000 * 256261* 295 MHz pre-postdivide262* FIXME 28636363 ref_freq doesn't match VID PLL ref263*/264cx25840_write4(client, 0x110, 0x0098d6e5);265266/*267* SA_MCLK_SEL = 1268* SA_MCLK_DIV = 0x10 = 256/384 * AUX_PLL post dvivider269*/270cx25840_write(client, 0x127, 0x50);271272if (is_cx2583x(state))273break;274275/* src1_ctl */276/* 0x1.8000 = 48000/32000 */277cx25840_write4(client, 0x8f8, 0x08018000);278279/* src3/4/6_ctl */280/* 0x1.5555 = 2 * (32000/48000) */281cx25840_write4(client, 0x900, 0x08015555);282cx25840_write4(client, 0x904, 0x08015555);283cx25840_write4(client, 0x90c, 0x08015555);284break;285}286}287288state->audclk_freq = freq;289290return 0;291}292293static inline int cx25836_set_audclk_freq(struct i2c_client *client, u32 freq)294{295return cx25840_set_audclk_freq(client, freq);296}297298static int cx23885_set_audclk_freq(struct i2c_client *client, u32 freq)299{300struct cx25840_state *state = to_state(i2c_get_clientdata(client));301302if (state->aud_input != CX25840_AUDIO_SERIAL) {303switch (freq) {304case 32000:305case 44100:306case 48000:307/* We don't have register values308* so avoid destroying registers. */309/* FIXME return -EINVAL; */310break;311}312} else {313switch (freq) {314case 32000:315case 44100:316/* We don't have register values317* so avoid destroying registers. */318/* FIXME return -EINVAL; */319break;320321case 48000:322/* src1_ctl */323/* 0x1.867c = 48000 / (2 * 28636360/8 * 2/455) */324cx25840_write4(client, 0x8f8, 0x0801867c);325326/* src3/4/6_ctl */327/* 0x1.4faa = (4 * 28636360/8 * 2/455) / 48000 */328cx25840_write4(client, 0x900, 0x08014faa);329cx25840_write4(client, 0x904, 0x08014faa);330cx25840_write4(client, 0x90c, 0x08014faa);331break;332}333}334335state->audclk_freq = freq;336337return 0;338}339340static int cx231xx_set_audclk_freq(struct i2c_client *client, u32 freq)341{342struct cx25840_state *state = to_state(i2c_get_clientdata(client));343344if (state->aud_input != CX25840_AUDIO_SERIAL) {345switch (freq) {346case 32000:347/* src3/4/6_ctl */348/* 0x1.f77f = (4 * 28636360/8 * 2/455) / 32000 */349cx25840_write4(client, 0x900, 0x0801f77f);350cx25840_write4(client, 0x904, 0x0801f77f);351cx25840_write4(client, 0x90c, 0x0801f77f);352break;353354case 44100:355/* src3/4/6_ctl */356/* 0x1.6d59 = (4 * 28636360/8 * 2/455) / 44100 */357cx25840_write4(client, 0x900, 0x08016d59);358cx25840_write4(client, 0x904, 0x08016d59);359cx25840_write4(client, 0x90c, 0x08016d59);360break;361362case 48000:363/* src3/4/6_ctl */364/* 0x1.4faa = (4 * 28636360/8 * 2/455) / 48000 */365cx25840_write4(client, 0x900, 0x08014faa);366cx25840_write4(client, 0x904, 0x08014faa);367cx25840_write4(client, 0x90c, 0x08014faa);368break;369}370} else {371switch (freq) {372/* FIXME These cases make different assumptions about audclk */373case 32000:374/* src1_ctl */375/* 0x1.0000 = 32000/32000 */376cx25840_write4(client, 0x8f8, 0x08010000);377378/* src3/4/6_ctl */379/* 0x2.0000 = 2 * (32000/32000) */380cx25840_write4(client, 0x900, 0x08020000);381cx25840_write4(client, 0x904, 0x08020000);382cx25840_write4(client, 0x90c, 0x08020000);383break;384385case 44100:386/* src1_ctl */387/* 0x1.60cd = 44100/32000 */388cx25840_write4(client, 0x8f8, 0x080160cd);389390/* src3/4/6_ctl */391/* 0x1.7385 = 2 * (32000/44100) */392cx25840_write4(client, 0x900, 0x08017385);393cx25840_write4(client, 0x904, 0x08017385);394cx25840_write4(client, 0x90c, 0x08017385);395break;396397case 48000:398/* src1_ctl */399/* 0x1.867c = 48000 / (2 * 28636360/8 * 2/455) */400cx25840_write4(client, 0x8f8, 0x0801867c);401402/* src3/4/6_ctl */403/* 0x1.4faa = (4 * 28636360/8 * 2/455) / 48000 */404cx25840_write4(client, 0x900, 0x08014faa);405cx25840_write4(client, 0x904, 0x08014faa);406cx25840_write4(client, 0x90c, 0x08014faa);407break;408}409}410411state->audclk_freq = freq;412413return 0;414}415416static int set_audclk_freq(struct i2c_client *client, u32 freq)417{418struct cx25840_state *state = to_state(i2c_get_clientdata(client));419420if (freq != 32000 && freq != 44100 && freq != 48000)421return -EINVAL;422423if (is_cx231xx(state))424return cx231xx_set_audclk_freq(client, freq);425426if (is_cx2388x(state))427return cx23885_set_audclk_freq(client, freq);428429if (is_cx2583x(state))430return cx25836_set_audclk_freq(client, freq);431432return cx25840_set_audclk_freq(client, freq);433}434435void cx25840_audio_set_path(struct i2c_client *client)436{437struct cx25840_state *state = to_state(i2c_get_clientdata(client));438439if (!is_cx2583x(state)) {440/* assert soft reset */441cx25840_and_or(client, 0x810, ~0x1, 0x01);442443/* stop microcontroller */444cx25840_and_or(client, 0x803, ~0x10, 0);445446/* Mute everything to prevent the PFFT! */447cx25840_write(client, 0x8d3, 0x1f);448449if (state->aud_input == CX25840_AUDIO_SERIAL) {450/* Set Path1 to Serial Audio Input */451cx25840_write4(client, 0x8d0, 0x01011012);452453/* The microcontroller should not be started for the454* non-tuner inputs: autodetection is specific for455* TV audio. */456} else {457/* Set Path1 to Analog Demod Main Channel */458cx25840_write4(client, 0x8d0, 0x1f063870);459}460}461462set_audclk_freq(client, state->audclk_freq);463464if (!is_cx2583x(state)) {465if (state->aud_input != CX25840_AUDIO_SERIAL) {466/* When the microcontroller detects the467* audio format, it will unmute the lines */468cx25840_and_or(client, 0x803, ~0x10, 0x10);469}470471/* deassert soft reset */472cx25840_and_or(client, 0x810, ~0x1, 0x00);473474/* Ensure the controller is running when we exit */475if (is_cx2388x(state) || is_cx231xx(state))476cx25840_and_or(client, 0x803, ~0x10, 0x10);477}478}479480static void set_volume(struct i2c_client *client, int volume)481{482int vol;483484/* Convert the volume to msp3400 values (0-127) */485vol = volume >> 9;486487/* now scale it up to cx25840 values488* -114dB to -96dB maps to 0489* this should be 19, but in my testing that was 4dB too loud */490if (vol <= 23) {491vol = 0;492} else {493vol -= 23;494}495496/* PATH1_VOLUME */497cx25840_write(client, 0x8d4, 228 - (vol * 2));498}499500static void set_balance(struct i2c_client *client, int balance)501{502int bal = balance >> 8;503if (bal > 0x80) {504/* PATH1_BAL_LEFT */505cx25840_and_or(client, 0x8d5, 0x7f, 0x80);506/* PATH1_BAL_LEVEL */507cx25840_and_or(client, 0x8d5, ~0x7f, bal & 0x7f);508} else {509/* PATH1_BAL_LEFT */510cx25840_and_or(client, 0x8d5, 0x7f, 0x00);511/* PATH1_BAL_LEVEL */512cx25840_and_or(client, 0x8d5, ~0x7f, 0x80 - bal);513}514}515516int cx25840_s_clock_freq(struct v4l2_subdev *sd, u32 freq)517{518struct i2c_client *client = v4l2_get_subdevdata(sd);519struct cx25840_state *state = to_state(sd);520int retval;521522if (!is_cx2583x(state))523cx25840_and_or(client, 0x810, ~0x1, 1);524if (state->aud_input != CX25840_AUDIO_SERIAL) {525cx25840_and_or(client, 0x803, ~0x10, 0);526cx25840_write(client, 0x8d3, 0x1f);527}528retval = set_audclk_freq(client, freq);529if (state->aud_input != CX25840_AUDIO_SERIAL)530cx25840_and_or(client, 0x803, ~0x10, 0x10);531if (!is_cx2583x(state))532cx25840_and_or(client, 0x810, ~0x1, 0);533return retval;534}535536static int cx25840_audio_s_ctrl(struct v4l2_ctrl *ctrl)537{538struct v4l2_subdev *sd = to_sd(ctrl);539struct cx25840_state *state = to_state(sd);540struct i2c_client *client = v4l2_get_subdevdata(sd);541542switch (ctrl->id) {543case V4L2_CID_AUDIO_VOLUME:544if (state->mute->val)545set_volume(client, 0);546else547set_volume(client, state->volume->val);548break;549case V4L2_CID_AUDIO_BASS:550/* PATH1_EQ_BASS_VOL */551cx25840_and_or(client, 0x8d9, ~0x3f,55248 - (ctrl->val * 48 / 0xffff));553break;554case V4L2_CID_AUDIO_TREBLE:555/* PATH1_EQ_TREBLE_VOL */556cx25840_and_or(client, 0x8db, ~0x3f,55748 - (ctrl->val * 48 / 0xffff));558break;559case V4L2_CID_AUDIO_BALANCE:560set_balance(client, ctrl->val);561break;562default:563return -EINVAL;564}565return 0;566}567568const struct v4l2_ctrl_ops cx25840_audio_ctrl_ops = {569.s_ctrl = cx25840_audio_s_ctrl,570};571572573