Path: blob/master/drivers/media/video/cx18/cx18-av-audio.c
17778 views
/*1* cx18 ADEC audio functions2*3* Derived from cx25840-audio.c4*5* Copyright (C) 2007 Hans Verkuil <[email protected]>6* Copyright (C) 2008 Andy Walls <[email protected]>7*8* This program is free software; you can redistribute it and/or9* modify it under the terms of the GNU General Public License10* as published by the Free Software Foundation; either version 211* of the License, or (at your option) any later version.12*13* This program is distributed in the hope that it will be useful,14* but WITHOUT ANY WARRANTY; without even the implied warranty of15* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the16* GNU General Public License for more details.17*18* You should have received a copy of the GNU General Public License19* along with this program; if not, write to the Free Software20* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA21* 02110-1301, USA.22*/2324#include "cx18-driver.h"2526static int set_audclk_freq(struct cx18 *cx, u32 freq)27{28struct cx18_av_state *state = &cx->av_state;2930if (freq != 32000 && freq != 44100 && freq != 48000)31return -EINVAL;3233/*34* The PLL parameters are based on the external crystal frequency that35* would ideally be:36*37* NTSC Color subcarrier freq * 8 =38* 4.5 MHz/286 * 455/2 * 8 = 28.63636363... MHz39*40* The accidents of history and rationale that explain from where this41* combination of magic numbers originate can be found in:42*43* [1] Abrahams, I. C., "Choice of Chrominance Subcarrier Frequency in44* the NTSC Standards", Proceedings of the I-R-E, January 1954, pp 79-8045*46* [2] Abrahams, I. C., "The 'Frequency Interleaving' Principle in the47* NTSC Standards", Proceedings of the I-R-E, January 1954, pp 81-8348*49* As Mike Bradley has rightly pointed out, it's not the exact crystal50* frequency that matters, only that all parts of the driver and51* firmware are using the same value (close to the ideal value).52*53* Since I have a strong suspicion that, if the firmware ever assumes a54* crystal value at all, it will assume 28.636360 MHz, the crystal55* freq used in calculations in this driver will be:56*57* xtal_freq = 28.636360 MHz58*59* an error of less than 0.13 ppm which is way, way better than any off60* the shelf crystal will have for accuracy anyway.61*62* Below I aim to run the PLLs' VCOs near 400 MHz to minimze error.63*64* Many thanks to Jeff Campbell and Mike Bradley for their extensive65* investigation, experimentation, testing, and suggested solutions of66* of audio/video sync problems with SVideo and CVBS captures.67*/6869if (state->aud_input > CX18_AV_AUDIO_SERIAL2) {70switch (freq) {71case 32000:72/*73* VID_PLL Integer = 0x0f, VID_PLL Post Divider = 0x0474* AUX_PLL Integer = 0x0d, AUX PLL Post Divider = 0x2075*/76cx18_av_write4(cx, 0x108, 0x200d040f);7778/* VID_PLL Fraction = 0x2be2fe */79/* xtal * 0xf.15f17f0/4 = 108 MHz: 432 MHz pre-postdiv*/80cx18_av_write4(cx, 0x10c, 0x002be2fe);8182/* AUX_PLL Fraction = 0x176740c */83/* xtal * 0xd.bb3a060/0x20 = 32000 * 384: 393 MHz p-pd*/84cx18_av_write4(cx, 0x110, 0x0176740c);8586/* src3/4/6_ctl */87/* 0x1.f77f = (4 * xtal/8*2/455) / 32000 */88cx18_av_write4(cx, 0x900, 0x0801f77f);89cx18_av_write4(cx, 0x904, 0x0801f77f);90cx18_av_write4(cx, 0x90c, 0x0801f77f);9192/* SA_MCLK_SEL=1, SA_MCLK_DIV=0x20 */93cx18_av_write(cx, 0x127, 0x60);9495/* AUD_COUNT = 0x2fff = 8 samples * 4 * 384 - 1 */96cx18_av_write4(cx, 0x12c, 0x11202fff);9798/*99* EN_AV_LOCK = 0100* VID_COUNT = 0x0d2ef8 = 107999.000 * 8 =101* ((8 samples/32,000) * (13,500,000 * 8) * 4 - 1) * 8102*/103cx18_av_write4(cx, 0x128, 0xa00d2ef8);104break;105106case 44100:107/*108* VID_PLL Integer = 0x0f, VID_PLL Post Divider = 0x04109* AUX_PLL Integer = 0x0e, AUX PLL Post Divider = 0x18110*/111cx18_av_write4(cx, 0x108, 0x180e040f);112113/* VID_PLL Fraction = 0x2be2fe */114/* xtal * 0xf.15f17f0/4 = 108 MHz: 432 MHz pre-postdiv*/115cx18_av_write4(cx, 0x10c, 0x002be2fe);116117/* AUX_PLL Fraction = 0x062a1f2 */118/* xtal * 0xe.3150f90/0x18 = 44100 * 384: 406 MHz p-pd*/119cx18_av_write4(cx, 0x110, 0x0062a1f2);120121/* src3/4/6_ctl */122/* 0x1.6d59 = (4 * xtal/8*2/455) / 44100 */123cx18_av_write4(cx, 0x900, 0x08016d59);124cx18_av_write4(cx, 0x904, 0x08016d59);125cx18_av_write4(cx, 0x90c, 0x08016d59);126127/* SA_MCLK_SEL=1, SA_MCLK_DIV=0x18 */128cx18_av_write(cx, 0x127, 0x58);129130/* AUD_COUNT = 0x92ff = 49 samples * 2 * 384 - 1 */131cx18_av_write4(cx, 0x12c, 0x112092ff);132133/*134* EN_AV_LOCK = 0135* VID_COUNT = 0x1d4bf8 = 239999.000 * 8 =136* ((49 samples/44,100) * (13,500,000 * 8) * 2 - 1) * 8137*/138cx18_av_write4(cx, 0x128, 0xa01d4bf8);139break;140141case 48000:142/*143* VID_PLL Integer = 0x0f, VID_PLL Post Divider = 0x04144* AUX_PLL Integer = 0x0e, AUX PLL Post Divider = 0x16145*/146cx18_av_write4(cx, 0x108, 0x160e040f);147148/* VID_PLL Fraction = 0x2be2fe */149/* xtal * 0xf.15f17f0/4 = 108 MHz: 432 MHz pre-postdiv*/150cx18_av_write4(cx, 0x10c, 0x002be2fe);151152/* AUX_PLL Fraction = 0x05227ad */153/* xtal * 0xe.2913d68/0x16 = 48000 * 384: 406 MHz p-pd*/154cx18_av_write4(cx, 0x110, 0x005227ad);155156/* src3/4/6_ctl */157/* 0x1.4faa = (4 * xtal/8*2/455) / 48000 */158cx18_av_write4(cx, 0x900, 0x08014faa);159cx18_av_write4(cx, 0x904, 0x08014faa);160cx18_av_write4(cx, 0x90c, 0x08014faa);161162/* SA_MCLK_SEL=1, SA_MCLK_DIV=0x16 */163cx18_av_write(cx, 0x127, 0x56);164165/* AUD_COUNT = 0x5fff = 4 samples * 16 * 384 - 1 */166cx18_av_write4(cx, 0x12c, 0x11205fff);167168/*169* EN_AV_LOCK = 0170* VID_COUNT = 0x1193f8 = 143999.000 * 8 =171* ((4 samples/48,000) * (13,500,000 * 8) * 16 - 1) * 8172*/173cx18_av_write4(cx, 0x128, 0xa01193f8);174break;175}176} else {177switch (freq) {178case 32000:179/*180* VID_PLL Integer = 0x0f, VID_PLL Post Divider = 0x04181* AUX_PLL Integer = 0x0d, AUX PLL Post Divider = 0x30182*/183cx18_av_write4(cx, 0x108, 0x300d040f);184185/* VID_PLL Fraction = 0x2be2fe */186/* xtal * 0xf.15f17f0/4 = 108 MHz: 432 MHz pre-postdiv*/187cx18_av_write4(cx, 0x10c, 0x002be2fe);188189/* AUX_PLL Fraction = 0x176740c */190/* xtal * 0xd.bb3a060/0x30 = 32000 * 256: 393 MHz p-pd*/191cx18_av_write4(cx, 0x110, 0x0176740c);192193/* src1_ctl */194/* 0x1.0000 = 32000/32000 */195cx18_av_write4(cx, 0x8f8, 0x08010000);196197/* src3/4/6_ctl */198/* 0x2.0000 = 2 * (32000/32000) */199cx18_av_write4(cx, 0x900, 0x08020000);200cx18_av_write4(cx, 0x904, 0x08020000);201cx18_av_write4(cx, 0x90c, 0x08020000);202203/* SA_MCLK_SEL=1, SA_MCLK_DIV=0x30 */204cx18_av_write(cx, 0x127, 0x70);205206/* AUD_COUNT = 0x1fff = 8 samples * 4 * 256 - 1 */207cx18_av_write4(cx, 0x12c, 0x11201fff);208209/*210* EN_AV_LOCK = 0211* VID_COUNT = 0x0d2ef8 = 107999.000 * 8 =212* ((8 samples/32,000) * (13,500,000 * 8) * 4 - 1) * 8213*/214cx18_av_write4(cx, 0x128, 0xa00d2ef8);215break;216217case 44100:218/*219* VID_PLL Integer = 0x0f, VID_PLL Post Divider = 0x04220* AUX_PLL Integer = 0x0e, AUX PLL Post Divider = 0x24221*/222cx18_av_write4(cx, 0x108, 0x240e040f);223224/* VID_PLL Fraction = 0x2be2fe */225/* xtal * 0xf.15f17f0/4 = 108 MHz: 432 MHz pre-postdiv*/226cx18_av_write4(cx, 0x10c, 0x002be2fe);227228/* AUX_PLL Fraction = 0x062a1f2 */229/* xtal * 0xe.3150f90/0x24 = 44100 * 256: 406 MHz p-pd*/230cx18_av_write4(cx, 0x110, 0x0062a1f2);231232/* src1_ctl */233/* 0x1.60cd = 44100/32000 */234cx18_av_write4(cx, 0x8f8, 0x080160cd);235236/* src3/4/6_ctl */237/* 0x1.7385 = 2 * (32000/44100) */238cx18_av_write4(cx, 0x900, 0x08017385);239cx18_av_write4(cx, 0x904, 0x08017385);240cx18_av_write4(cx, 0x90c, 0x08017385);241242/* SA_MCLK_SEL=1, SA_MCLK_DIV=0x24 */243cx18_av_write(cx, 0x127, 0x64);244245/* AUD_COUNT = 0x61ff = 49 samples * 2 * 256 - 1 */246cx18_av_write4(cx, 0x12c, 0x112061ff);247248/*249* EN_AV_LOCK = 0250* VID_COUNT = 0x1d4bf8 = 239999.000 * 8 =251* ((49 samples/44,100) * (13,500,000 * 8) * 2 - 1) * 8252*/253cx18_av_write4(cx, 0x128, 0xa01d4bf8);254break;255256case 48000:257/*258* VID_PLL Integer = 0x0f, VID_PLL Post Divider = 0x04259* AUX_PLL Integer = 0x0d, AUX PLL Post Divider = 0x20260*/261cx18_av_write4(cx, 0x108, 0x200d040f);262263/* VID_PLL Fraction = 0x2be2fe */264/* xtal * 0xf.15f17f0/4 = 108 MHz: 432 MHz pre-postdiv*/265cx18_av_write4(cx, 0x10c, 0x002be2fe);266267/* AUX_PLL Fraction = 0x176740c */268/* xtal * 0xd.bb3a060/0x20 = 48000 * 256: 393 MHz p-pd*/269cx18_av_write4(cx, 0x110, 0x0176740c);270271/* src1_ctl */272/* 0x1.8000 = 48000/32000 */273cx18_av_write4(cx, 0x8f8, 0x08018000);274275/* src3/4/6_ctl */276/* 0x1.5555 = 2 * (32000/48000) */277cx18_av_write4(cx, 0x900, 0x08015555);278cx18_av_write4(cx, 0x904, 0x08015555);279cx18_av_write4(cx, 0x90c, 0x08015555);280281/* SA_MCLK_SEL=1, SA_MCLK_DIV=0x20 */282cx18_av_write(cx, 0x127, 0x60);283284/* AUD_COUNT = 0x3fff = 4 samples * 16 * 256 - 1 */285cx18_av_write4(cx, 0x12c, 0x11203fff);286287/*288* EN_AV_LOCK = 0289* VID_COUNT = 0x1193f8 = 143999.000 * 8 =290* ((4 samples/48,000) * (13,500,000 * 8) * 16 - 1) * 8291*/292cx18_av_write4(cx, 0x128, 0xa01193f8);293break;294}295}296297state->audclk_freq = freq;298299return 0;300}301302void cx18_av_audio_set_path(struct cx18 *cx)303{304struct cx18_av_state *state = &cx->av_state;305u8 v;306307/* stop microcontroller */308v = cx18_av_read(cx, 0x803) & ~0x10;309cx18_av_write_expect(cx, 0x803, v, v, 0x1f);310311/* assert soft reset */312v = cx18_av_read(cx, 0x810) | 0x01;313cx18_av_write_expect(cx, 0x810, v, v, 0x0f);314315/* Mute everything to prevent the PFFT! */316cx18_av_write(cx, 0x8d3, 0x1f);317318if (state->aud_input <= CX18_AV_AUDIO_SERIAL2) {319/* Set Path1 to Serial Audio Input */320cx18_av_write4(cx, 0x8d0, 0x01011012);321322/* The microcontroller should not be started for the323* non-tuner inputs: autodetection is specific for324* TV audio. */325} else {326/* Set Path1 to Analog Demod Main Channel */327cx18_av_write4(cx, 0x8d0, 0x1f063870);328}329330set_audclk_freq(cx, state->audclk_freq);331332/* deassert soft reset */333v = cx18_av_read(cx, 0x810) & ~0x01;334cx18_av_write_expect(cx, 0x810, v, v, 0x0f);335336if (state->aud_input > CX18_AV_AUDIO_SERIAL2) {337/* When the microcontroller detects the338* audio format, it will unmute the lines */339v = cx18_av_read(cx, 0x803) | 0x10;340cx18_av_write_expect(cx, 0x803, v, v, 0x1f);341}342}343344static void set_volume(struct cx18 *cx, int volume)345{346/* First convert the volume to msp3400 values (0-127) */347int vol = volume >> 9;348/* now scale it up to cx18_av values349* -114dB to -96dB maps to 0350* this should be 19, but in my testing that was 4dB too loud */351if (vol <= 23)352vol = 0;353else354vol -= 23;355356/* PATH1_VOLUME */357cx18_av_write(cx, 0x8d4, 228 - (vol * 2));358}359360static void set_bass(struct cx18 *cx, int bass)361{362/* PATH1_EQ_BASS_VOL */363cx18_av_and_or(cx, 0x8d9, ~0x3f, 48 - (bass * 48 / 0xffff));364}365366static void set_treble(struct cx18 *cx, int treble)367{368/* PATH1_EQ_TREBLE_VOL */369cx18_av_and_or(cx, 0x8db, ~0x3f, 48 - (treble * 48 / 0xffff));370}371372static void set_balance(struct cx18 *cx, int balance)373{374int bal = balance >> 8;375if (bal > 0x80) {376/* PATH1_BAL_LEFT */377cx18_av_and_or(cx, 0x8d5, 0x7f, 0x80);378/* PATH1_BAL_LEVEL */379cx18_av_and_or(cx, 0x8d5, ~0x7f, bal & 0x7f);380} else {381/* PATH1_BAL_LEFT */382cx18_av_and_or(cx, 0x8d5, 0x7f, 0x00);383/* PATH1_BAL_LEVEL */384cx18_av_and_or(cx, 0x8d5, ~0x7f, 0x80 - bal);385}386}387388static void set_mute(struct cx18 *cx, int mute)389{390struct cx18_av_state *state = &cx->av_state;391u8 v;392393if (state->aud_input > CX18_AV_AUDIO_SERIAL2) {394/* Must turn off microcontroller in order to mute sound.395* Not sure if this is the best method, but it does work.396* If the microcontroller is running, then it will undo any397* changes to the mute register. */398v = cx18_av_read(cx, 0x803);399if (mute) {400/* disable microcontroller */401v &= ~0x10;402cx18_av_write_expect(cx, 0x803, v, v, 0x1f);403cx18_av_write(cx, 0x8d3, 0x1f);404} else {405/* enable microcontroller */406v |= 0x10;407cx18_av_write_expect(cx, 0x803, v, v, 0x1f);408}409} else {410/* SRC1_MUTE_EN */411cx18_av_and_or(cx, 0x8d3, ~0x2, mute ? 0x02 : 0x00);412}413}414415int cx18_av_s_clock_freq(struct v4l2_subdev *sd, u32 freq)416{417struct cx18 *cx = v4l2_get_subdevdata(sd);418struct cx18_av_state *state = &cx->av_state;419int retval;420u8 v;421422if (state->aud_input > CX18_AV_AUDIO_SERIAL2) {423v = cx18_av_read(cx, 0x803) & ~0x10;424cx18_av_write_expect(cx, 0x803, v, v, 0x1f);425cx18_av_write(cx, 0x8d3, 0x1f);426}427v = cx18_av_read(cx, 0x810) | 0x1;428cx18_av_write_expect(cx, 0x810, v, v, 0x0f);429430retval = set_audclk_freq(cx, freq);431432v = cx18_av_read(cx, 0x810) & ~0x1;433cx18_av_write_expect(cx, 0x810, v, v, 0x0f);434if (state->aud_input > CX18_AV_AUDIO_SERIAL2) {435v = cx18_av_read(cx, 0x803) | 0x10;436cx18_av_write_expect(cx, 0x803, v, v, 0x1f);437}438return retval;439}440441static int cx18_av_audio_s_ctrl(struct v4l2_ctrl *ctrl)442{443struct v4l2_subdev *sd = to_sd(ctrl);444struct cx18 *cx = v4l2_get_subdevdata(sd);445446switch (ctrl->id) {447case V4L2_CID_AUDIO_VOLUME:448set_volume(cx, ctrl->val);449break;450case V4L2_CID_AUDIO_BASS:451set_bass(cx, ctrl->val);452break;453case V4L2_CID_AUDIO_TREBLE:454set_treble(cx, ctrl->val);455break;456case V4L2_CID_AUDIO_BALANCE:457set_balance(cx, ctrl->val);458break;459case V4L2_CID_AUDIO_MUTE:460set_mute(cx, ctrl->val);461break;462default:463return -EINVAL;464}465return 0;466}467468const struct v4l2_ctrl_ops cx18_av_audio_ctrl_ops = {469.s_ctrl = cx18_av_audio_s_ctrl,470};471472473