Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753276AbdDNC31 (ORCPT ); Thu, 13 Apr 2017 22:29:27 -0400 Received: from mail-by2nam01on0114.outbound.protection.outlook.com ([104.47.34.114]:15910 "EHLO NAM01-BY2-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1751438AbdDNC3U (ORCPT ); Thu, 13 Apr 2017 22:29:20 -0400 Authentication-Results: spf=pass (sender IP is 117.103.190.43) smtp.mailfrom=sony.com; vger.kernel.org; dkim=none (message not signed) header.d=none;vger.kernel.org; dmarc=bestguesspass action=none header.from=sony.com; From: To: , , CC: , , Yasunari Takiguchi , Masayuki Yamamoto , Hideki Nozawa , "Kota Yonezawa" , Toshihiko Matsumoto , Satoshi Watanabe Subject: [PATCH v2 08/15] [media] cxd2880: Add top level of the driver Date: Fri, 14 Apr 2017 11:31:50 +0900 Message-ID: <20170414023150.17685-1-Yasunari.Takiguchi@sony.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20170414015043.16731-1-Yasunari.Takiguchi@sony.com> References: <20170414015043.16731-1-Yasunari.Takiguchi@sony.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [43.25.42.176] X-EOPAttributedMessage: 0 X-MS-Office365-Filtering-HT: Tenant X-Forefront-Antispam-Report: CIP:117.103.190.43;IPV:NLI;CTRY:JP;EFV:NLI;SFV:NSPM;SFS:(10019020)(6009001)(39450400003)(39410400002)(39850400002)(39400400002)(39840400002)(39860400002)(2980300002)(438002)(199003)(189002)(86152003)(1076002)(8936002)(38730400002)(107886003)(5660300001)(5890100001)(6306002)(54906002)(53946003)(39060400002)(50226002)(2876002)(2201001)(36756003)(86362001)(48376002)(6666003)(66066001)(356003)(50466002)(2950100002)(76176999)(7636002)(50986999)(8676002)(47776003)(5003940100001)(49486002)(575784001)(6116002)(4326008)(2906002)(106466001)(189998001)(7736002)(305945005)(3846002)(2004002)(579004)(473944003)(414714003);DIR:OUT;SFP:1102;SCL:1;SRVR:CY4PR13MB1463;H:JPYOKXEG103.jp.sony.com;FPR:;SPF:Pass;MLV:ovrnspm;A:1;MX:1;PTR:jpyokxeg103.jp.sony.com;LANG:en; X-Microsoft-Exchange-Diagnostics: 1;CY1NAM02FT039;1:yaOBhSFDDbyRdKKb1q265KJitK89YZn9cLCLKdxYCBZDYVUxWL0zpRUHbOodK1w5o0WhoRc/CVQDJmnhxHODPJ/KrtWGc1S6aThhybNukpyML9L4ejB8/AW4oMM8vQWdZhQSZzzBLKde6F9LzRqCtJEO7YRv4MSn+/Nl1vw6EyNWaQTwhDEi5b6McsFapp3i3gKLSp9M+Umgeh0i7Y0SrJMPc9rguRYlTOR0bdQz010La0CMAADbc4FGPS4WvDKyiO3/jfbkYFMw98MLeYDgsIr43QPWB3lywRlaJQ4B1pXFl6l0b1zK/U/g9ftno0Gb3nRH+fRpl5bjBZxx48Y4l33Gk/1kcROlL11xG5H9IjIJIGZdI/EBBbhpSGpXibcm30tWZde4XyNW1jJmvMEOb1mrnyp90PoFfZokjPIaDy2ITkuXwjkouMyB0x0+oXB6icAd0IhXqF+FvrDgqB+KxhZec0Um4mm7/LAwKxveC9chgYJhHyLXaUd2Rau8yYz9MzISEMRaGssHZbmN4TLxXf5iGyN9bW7VCRkcefwkWCk= X-MS-Office365-Filtering-Correlation-Id: 11eec5a1-20d1-4441-b9fb-08d482de0cb9 X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:(22001)(8251501002)(2017030254075)(201703131423075)(201703031133081)(201702281549075);SRVR:CY4PR13MB1463; X-Microsoft-Exchange-Diagnostics: 1;CY4PR13MB1463;3:FLZsFyC39t51f9cMbsxXILNxm1+ofUBk6heqgDbIupW0lO1BUqyzjADhE7pzWMP7S/UeJ0Aw3LBt4dtFoWkiLLwk7s00Qma794htczApw2ZYqJF/nlx+K3MWJIv08jCaS13IbAbjRI2ykr8K9raTx6Fnff2AvPy0F+9qxpH9xInD4pPyMZaUjdvv4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AG05HJmQqKkXpYvDUVZBcf1F0jFoN1K7vMaVJEJk3QC7eS4j+JRkS1jRt7lAhw== X-Microsoft-Exchange-Diagnostics: 1;CY4PR13MB1463;20:bQc5DZnTo7WcVWTTALoHMfnA9b1+1I6eVX0+QbJxfLZBL8T+LZXL0xmbo3WJrOAD4rFkdwiI4JfL51P+UVMU1bAp8kkeGrC7okWv/6E0wWG4OskmzP02pLN0oFOEOdG0482a06PDcV1bYQ92FBjUkro0y9bzg18ldy5ccIuFBIkClr69MPrqHifutGDXzypsHYIrGFlsLpDrJBkvTRm7gCn6oCJxsT+E0/1eab7nep6mk2F1lKmltRwQTHot5ueff1hciFI4/IFqpzm8utuiRvwNZ1ck5k32QTsEEoDbGBCeG2eXPG1nQvevsnQzCpZmMgEeiFzOsGmsVafay/RZ46MaoXFm/5OhUkbbw+vKXI9ZRzLCpgBi7/D/tTVuDv+Dx3XkdC9vSOhw4kIry50i/Cn6j3Dvm4BFvJTQRepgmQnTQXdgXlVKfF6MZKc4DG8AA2pSMsP6kVLNt+T4D3pQGcTy0ymd8HeH4FtZqjvkXVIvHeqPjowhbh+2s0X+Rj2P X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(250305191791016)(182409339516656)(22074186197030); X-Exchange-Antispam-Report-CFA-Test: BCL:0;PCL:0;RULEID:(6040450)(601004)(2401047)(13018025)(13016025)(13023025)(13024025)(8121501046)(5005006)(10201501046)(93006095)(93004095)(3002001)(6055026)(6041248)(201703131423075)(201702281528075)(201703061421075)(20161123564025)(20161123560025)(20161123555025)(20161123562025)(6072148);SRVR:CY4PR13MB1463;BCL:0;PCL:0;RULEID:;SRVR:CY4PR13MB1463; X-Microsoft-Exchange-Diagnostics: 1;CY4PR13MB1463;4:/Nr7TzXUn7/BBhhV7Mk2mxBjq44C87JvbUNnzcUcGqij4qbvtOr5ltKFsWBzXn2CJv0cqthCsc55pZlwaI3T1fzEe/3KKRwW+xgEPp2A7oo2ra/cae6Jew10cjvK+o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qEQJ/U3F30jquVJPb6mJ9PSQJpe38lC6nQRsxHOIJ9D/qCsMPidCX8Vnl5Q= SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1;CY4PR13MB1463;7:ReGCMCp9FELtIc4ZjxnkgNRpnrbpBZlkU7BSIgFyQ5+VtEDmdMnNdUF5C06vm9JK5lCTwfUO+zm2vVRnyXU5FTTw2Gz3iPutJf2XFnJINl+UAViPvAuDKPk7sOI1xOMGqse0fjeVLHsP+8Nb9eU+ZQQncdYUvanXOapJwgPJXyIixd310k9qA6HjG4JdE54Ue6ZGzeee9QMFY7Rc535mu+MOUF8XXnQBZNhlXNeP8pOMcRXdhCvxQ6XBd0L1dD1fcoabtY/TeEOmOVNOmk0ULP0w27IZ4wg3+rWajdFGH+71IYmaQZpit2eKzIigkAtw9nkVC5NLG73tgByUzI8EXA== X-OriginatorOrg: sony.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Apr 2017 02:29:16.9087 (UTC) X-MS-Exchange-CrossTenant-Id: 66c65d8a-9158-4521-a2d8-664963db48e4 X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=66c65d8a-9158-4521-a2d8-664963db48e4;Ip=[117.103.190.43];Helo=[JPYOKXEG103.jp.sony.com] X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY4PR13MB1463 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 44334 Lines: 1574 From: Yasunari Takiguchi This provides the main dvb frontend operation functions for the Sony CXD2880 DVB-T2/T tuner + demodulator driver. Signed-off-by: Yasunari Takiguchi Signed-off-by: Masayuki Yamamoto Signed-off-by: Hideki Nozawa Signed-off-by: Kota Yonezawa Signed-off-by: Toshihiko Matsumoto Signed-off-by: Satoshi Watanabe --- drivers/media/dvb-frontends/cxd2880/cxd2880_top.c | 1550 +++++++++++++++++++++ 1 file changed, 1550 insertions(+) create mode 100644 drivers/media/dvb-frontends/cxd2880/cxd2880_top.c diff --git a/drivers/media/dvb-frontends/cxd2880/cxd2880_top.c b/drivers/media/dvb-frontends/cxd2880/cxd2880_top.c new file mode 100644 index 000000000000..66d78fb93a13 --- /dev/null +++ b/drivers/media/dvb-frontends/cxd2880/cxd2880_top.c @@ -0,0 +1,1550 @@ +/* + * cxd2880_top.c + * Sony CXD2880 DVB-T2/T tuner + demodulator driver + * + * Copyright (C) 2016, 2017 Sony Semiconductor Solutions Corporation + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; version 2 of the License. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN + * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, see . + */ + +#include + +#include "dvb_frontend.h" + +#include "cxd2880.h" +#include "cxd2880_tnrdmd_mon.h" +#include "cxd2880_tnrdmd_dvbt2_mon.h" +#include "cxd2880_tnrdmd_dvbt_mon.h" +#include "cxd2880_integ_dvbt2.h" +#include "cxd2880_integ_dvbt.h" +#include "cxd2880_devio_spi.h" +#include "cxd2880_spi_device.h" +#include "cxd2880_tnrdmd_driver_version.h" + +struct cxd2880_priv { + struct cxd2880_tnrdmd tnrdmd; + struct spi_device *spi; + struct cxd2880_io regio; + struct cxd2880_spi_device spi_device; + struct cxd2880_spi cxd2880_spi; + struct cxd2880_dvbt_tune_param dvbt_tune_param; + struct cxd2880_dvbt2_tune_param dvbt2_tune_param; + struct mutex *spi_mutex; /* For SPI access exclusive control */ +}; + +/* + * return value conversion table + */ +static int return_tbl[] = { + 0, /* CXD2880_RESULT_OK */ + -EINVAL, /* CXD2880_RESULT_ERROR_ARG*/ + -EIO, /* CXD2880_RESULT_ERROR_IO */ + -EPERM, /* CXD2880_RESULT_ERROR_SW_STATE */ + -EBUSY, /* CXD2880_RESULT_ERROR_HW_STATE */ + -ETIME, /* CXD2880_RESULT_ERROR_TIMEOUT */ + -EAGAIN, /* CXD2880_RESULT_ERROR_UNLOCK */ + -ERANGE, /* CXD2880_RESULT_ERROR_RANGE */ + -EOPNOTSUPP, /* CXD2880_RESULT_ERROR_NOSUPPORT */ + -ECANCELED, /* CXD2880_RESULT_ERROR_CANCEL */ + -EPERM, /* CXD2880_RESULT_ERROR_OTHER */ + -EOVERFLOW, /* CXD2880_RESULT_ERROR_OVERFLOW */ + 0, /* CXD2880_RESULT_OK_CONFIRM */ +}; + +static enum cxd2880_ret cxd2880_pre_bit_err_t( + struct cxd2880_tnrdmd *tnrdmd, u32 *pre_bit_err, + u32 *pre_bit_count) +{ + u8 rdata[2]; + + if ((!tnrdmd) || (!pre_bit_err) || (!pre_bit_count)) + return CXD2880_RESULT_ERROR_ARG; + + if (tnrdmd->diver_mode == CXD2880_TNRDMD_DIVERMODE_SUB) + return CXD2880_RESULT_ERROR_ARG; + + if (tnrdmd->state != CXD2880_TNRDMD_STATE_ACTIVE) + return CXD2880_RESULT_ERROR_SW_STATE; + + if (tnrdmd->sys != CXD2880_DTV_SYS_DVBT) + return CXD2880_RESULT_ERROR_SW_STATE; + + if (slvt_freeze_reg(tnrdmd) != CXD2880_RESULT_OK) + return CXD2880_RESULT_ERROR_IO; + + if (tnrdmd->io->write_reg(tnrdmd->io, CXD2880_IO_TGT_DMD, + 0x00, 0x10) != CXD2880_RESULT_OK) { + slvt_unfreeze_reg(tnrdmd); + return CXD2880_RESULT_ERROR_IO; + } + + if (tnrdmd->io->read_regs(tnrdmd->io, CXD2880_IO_TGT_DMD, + 0x39, rdata, 1) != CXD2880_RESULT_OK) { + slvt_unfreeze_reg(tnrdmd); + return CXD2880_RESULT_ERROR_IO; + } + + if ((rdata[0] & 0x01) == 0) { + slvt_unfreeze_reg(tnrdmd); + return CXD2880_RESULT_ERROR_HW_STATE; + } + + if (tnrdmd->io->read_regs(tnrdmd->io, CXD2880_IO_TGT_DMD, + 0x22, rdata, 2) != CXD2880_RESULT_OK) { + slvt_unfreeze_reg(tnrdmd); + return CXD2880_RESULT_ERROR_IO; + } + + *pre_bit_err = (rdata[0] << 8) | rdata[1]; + + if (tnrdmd->io->read_regs(tnrdmd->io, CXD2880_IO_TGT_DMD, + 0x6F, rdata, 1) != CXD2880_RESULT_OK) { + slvt_unfreeze_reg(tnrdmd); + return CXD2880_RESULT_ERROR_IO; + } + + slvt_unfreeze_reg(tnrdmd); + + *pre_bit_count = ((rdata[0] & 0x07) == 0) ? + 256 : (0x1000 << (rdata[0] & 0x07)); + + return CXD2880_RESULT_OK; +} + +static enum cxd2880_ret cxd2880_pre_bit_err_t2( + struct cxd2880_tnrdmd *tnrdmd, u32 *pre_bit_err, + u32 *pre_bit_count) +{ + u32 period_exp = 0; + u32 n_ldpc = 0; + u8 data[5]; + + if ((!tnrdmd) || (!pre_bit_err) || (!pre_bit_count)) + return CXD2880_RESULT_ERROR_ARG; + + if (tnrdmd->diver_mode == CXD2880_TNRDMD_DIVERMODE_SUB) + return CXD2880_RESULT_ERROR_ARG; + + if (tnrdmd->state != CXD2880_TNRDMD_STATE_ACTIVE) + return CXD2880_RESULT_ERROR_SW_STATE; + + if (tnrdmd->sys != CXD2880_DTV_SYS_DVBT2) + return CXD2880_RESULT_ERROR_SW_STATE; + + if (slvt_freeze_reg(tnrdmd) != CXD2880_RESULT_OK) + return CXD2880_RESULT_ERROR_IO; + + if (tnrdmd->io->write_reg(tnrdmd->io, CXD2880_IO_TGT_DMD, + 0x00, 0x0B) != CXD2880_RESULT_OK) { + slvt_unfreeze_reg(tnrdmd); + return CXD2880_RESULT_ERROR_IO; + } + + if (tnrdmd->io->read_regs(tnrdmd->io, CXD2880_IO_TGT_DMD, + 0x3C, data, sizeof(data)) + != CXD2880_RESULT_OK) { + slvt_unfreeze_reg(tnrdmd); + return CXD2880_RESULT_ERROR_IO; + } + + if (!(data[0] & 0x01)) { + slvt_unfreeze_reg(tnrdmd); + return CXD2880_RESULT_ERROR_HW_STATE; + } + *pre_bit_err = + ((data[1] & 0x0F) << 24) | (data[2] << 16) | (data[3] << 8) | data[4]; + + if (tnrdmd->io->read_regs(tnrdmd->io, CXD2880_IO_TGT_DMD, + 0xA0, data, 1) != CXD2880_RESULT_OK) { + slvt_unfreeze_reg(tnrdmd); + return CXD2880_RESULT_ERROR_IO; + } + + if (((enum cxd2880_dvbt2_plp_fec)(data[0] & 0x03)) == + CXD2880_DVBT2_FEC_LDPC_16K) + n_ldpc = 16200; + else + n_ldpc = 64800; + slvt_unfreeze_reg(tnrdmd); + + if (tnrdmd->io->write_reg(tnrdmd->io, CXD2880_IO_TGT_DMD, + 0x00, 0x20) != CXD2880_RESULT_OK) + return CXD2880_RESULT_ERROR_IO; + + if (tnrdmd->io->read_regs(tnrdmd->io, CXD2880_IO_TGT_DMD, + 0x6F, data, 1) != CXD2880_RESULT_OK) + return CXD2880_RESULT_ERROR_IO; + + period_exp = data[0] & 0x0F; + + *pre_bit_count = (1U << period_exp) * n_ldpc; + + return CXD2880_RESULT_OK; +} + +static enum cxd2880_ret cxd2880_post_bit_err_t(struct cxd2880_tnrdmd *tnrdmd, + u32 *post_bit_err, + u32 *post_bit_count) +{ + u8 rdata[3]; + u32 bit_error = 0; + u32 period_exp = 0; + + if ((!tnrdmd) || (!post_bit_err) || (!post_bit_count)) + return CXD2880_RESULT_ERROR_ARG; + + if (tnrdmd->diver_mode == CXD2880_TNRDMD_DIVERMODE_SUB) + return CXD2880_RESULT_ERROR_ARG; + + if (tnrdmd->state != CXD2880_TNRDMD_STATE_ACTIVE) + return CXD2880_RESULT_ERROR_SW_STATE; + + if (tnrdmd->sys != CXD2880_DTV_SYS_DVBT) + return CXD2880_RESULT_ERROR_SW_STATE; + + if (tnrdmd->io->write_reg(tnrdmd->io, CXD2880_IO_TGT_DMD, + 0x00, 0x0D) != CXD2880_RESULT_OK) + return CXD2880_RESULT_ERROR_IO; + + if (tnrdmd->io->read_regs(tnrdmd->io, CXD2880_IO_TGT_DMD, + 0x15, rdata, 3) != CXD2880_RESULT_OK) + return CXD2880_RESULT_ERROR_IO; + + if ((rdata[0] & 0x40) == 0) + return CXD2880_RESULT_ERROR_HW_STATE; + + *post_bit_err = ((rdata[0] & 0x3F) << 16) | (rdata[1] << 8) | rdata[2]; + + if (tnrdmd->io->write_reg(tnrdmd->io, CXD2880_IO_TGT_DMD, + 0x00, 0x10) != CXD2880_RESULT_OK) + return CXD2880_RESULT_ERROR_IO; + + if (tnrdmd->io->read_regs(tnrdmd->io, CXD2880_IO_TGT_DMD, + 0x60, rdata, 1) != CXD2880_RESULT_OK) + return CXD2880_RESULT_ERROR_IO; + + period_exp = (rdata[0] & 0x1F); + + if ((period_exp <= 11) && (bit_error > (1U << period_exp) * 204 * 8)) + return CXD2880_RESULT_ERROR_HW_STATE; + + if (period_exp == 11) + *post_bit_count = 3342336; + else + *post_bit_count = (1U << period_exp) * 204 * 81; + + return CXD2880_RESULT_OK; +} + +static enum cxd2880_ret cxd2880_post_bit_err_t2(struct cxd2880_tnrdmd *tnrdmd, + u32 *post_bit_err, + u32 *post_bit_count) +{ + u32 period_exp = 0; + u32 n_bch = 0; + + if ((!tnrdmd) || (!post_bit_err) || (!post_bit_count)) + return CXD2880_RESULT_ERROR_ARG; + + if (tnrdmd->diver_mode == CXD2880_TNRDMD_DIVERMODE_SUB) + return CXD2880_RESULT_ERROR_ARG; + + if (tnrdmd->state != CXD2880_TNRDMD_STATE_ACTIVE) + return CXD2880_RESULT_ERROR_SW_STATE; + + if (tnrdmd->sys != CXD2880_DTV_SYS_DVBT2) + return CXD2880_RESULT_ERROR_SW_STATE; + + { + u8 data[3]; + enum cxd2880_dvbt2_plp_fec plp_fec_type = + CXD2880_DVBT2_FEC_LDPC_16K; + enum cxd2880_dvbt2_plp_code_rate plp_code_rate = + CXD2880_DVBT2_R1_2; + + static const u16 n_bch_bits_lookup[2][8] = { + {7200, 9720, 10800, 11880, 12600, 13320, 5400, 6480}, + {32400, 38880, 43200, 48600, 51840, 54000, 21600, 25920} + }; + + if (slvt_freeze_reg(tnrdmd) != CXD2880_RESULT_OK) + return CXD2880_RESULT_ERROR_IO; + + if (tnrdmd->io->write_reg(tnrdmd->io, CXD2880_IO_TGT_DMD, + 0x00, 0x0B) != CXD2880_RESULT_OK) { + slvt_unfreeze_reg(tnrdmd); + return CXD2880_RESULT_ERROR_IO; + } + + if (tnrdmd->io->read_regs(tnrdmd->io, CXD2880_IO_TGT_DMD, + 0x15, data, 3) != CXD2880_RESULT_OK) { + slvt_unfreeze_reg(tnrdmd); + return CXD2880_RESULT_ERROR_IO; + } + + if (!(data[0] & 0x40)) { + slvt_unfreeze_reg(tnrdmd); + return CXD2880_RESULT_ERROR_HW_STATE; + } + + *post_bit_err = + ((data[0] & 0x3F) << 16) | (data[1] << 8) | data[2]; + + if (tnrdmd->io->read_regs(tnrdmd->io, CXD2880_IO_TGT_DMD, + 0x9D, data, 1) != CXD2880_RESULT_OK) { + slvt_unfreeze_reg(tnrdmd); + return CXD2880_RESULT_ERROR_IO; + } + + plp_code_rate = + (enum cxd2880_dvbt2_plp_code_rate)(data[0] & 0x07); + + if (tnrdmd->io->read_regs(tnrdmd->io, CXD2880_IO_TGT_DMD, + 0xA0, data, 1) != CXD2880_RESULT_OK) { + slvt_unfreeze_reg(tnrdmd); + return CXD2880_RESULT_ERROR_IO; + } + + plp_fec_type = (enum cxd2880_dvbt2_plp_fec)(data[0] & 0x03); + + slvt_unfreeze_reg(tnrdmd); + + if (tnrdmd->io->write_reg(tnrdmd->io, CXD2880_IO_TGT_DMD, + 0x00, 0x20) != CXD2880_RESULT_OK) + return CXD2880_RESULT_ERROR_IO; + + if (tnrdmd->io->read_regs(tnrdmd->io, CXD2880_IO_TGT_DMD, + 0x72, data, 1) != CXD2880_RESULT_OK) + return CXD2880_RESULT_ERROR_IO; + + period_exp = data[0] & 0x0F; + + if ((plp_fec_type > CXD2880_DVBT2_FEC_LDPC_64K) || + (plp_code_rate > CXD2880_DVBT2_R2_5)) + return CXD2880_RESULT_ERROR_HW_STATE; + + n_bch = n_bch_bits_lookup[plp_fec_type][plp_code_rate]; + } + + if (*post_bit_err > ((1U << period_exp) * n_bch)) + return CXD2880_RESULT_ERROR_HW_STATE; + + *post_bit_count = (1U << period_exp) * n_bch; + + return CXD2880_RESULT_OK; +} + +static enum cxd2880_ret cxd2880_read_block_err_t( + struct cxd2880_tnrdmd *tnrdmd, + u32 *block_err, + u32 *block_count) +{ + u8 rdata[3]; + + if ((!tnrdmd) || (!block_err) || (!block_count)) + return CXD2880_RESULT_ERROR_ARG; + + if (tnrdmd->diver_mode == CXD2880_TNRDMD_DIVERMODE_SUB) + return CXD2880_RESULT_ERROR_ARG; + + if (tnrdmd->state != CXD2880_TNRDMD_STATE_ACTIVE) + return CXD2880_RESULT_ERROR_SW_STATE; + + if (tnrdmd->sys != CXD2880_DTV_SYS_DVBT) + return CXD2880_RESULT_ERROR_SW_STATE; + + if (tnrdmd->io->write_reg(tnrdmd->io, CXD2880_IO_TGT_DMD, + 0x00, 0x0D) != CXD2880_RESULT_OK) + return CXD2880_RESULT_ERROR_IO; + + if (tnrdmd->io->read_regs(tnrdmd->io, CXD2880_IO_TGT_DMD, + 0x18, rdata, 3) != CXD2880_RESULT_OK) + return CXD2880_RESULT_ERROR_IO; + + if ((rdata[0] & 0x01) == 0) + return CXD2880_RESULT_ERROR_HW_STATE; + + *block_err = (rdata[1] << 8) | rdata[2]; + + if (tnrdmd->io->write_reg(tnrdmd->io, CXD2880_IO_TGT_DMD, + 0x00, 0x10) != CXD2880_RESULT_OK) + return CXD2880_RESULT_ERROR_IO; + + if (tnrdmd->io->read_regs(tnrdmd->io, CXD2880_IO_TGT_DMD, + 0x5C, rdata, 1) != CXD2880_RESULT_OK) + return CXD2880_RESULT_ERROR_IO; + + *block_count = 1U << (rdata[0] & 0x0F); + + if ((*block_count == 0) || (*block_err > *block_count)) + return CXD2880_RESULT_ERROR_HW_STATE; + + return CXD2880_RESULT_OK; +} + +static enum cxd2880_ret cxd2880_read_block_err_t2( + struct cxd2880_tnrdmd *tnrdmd, + u32 *block_err, + u32 *block_count) +{ + if ((!tnrdmd) || (!block_err) || (!block_count)) + return CXD2880_RESULT_ERROR_ARG; + + if (tnrdmd->diver_mode == CXD2880_TNRDMD_DIVERMODE_SUB) + return CXD2880_RESULT_ERROR_ARG; + + if (tnrdmd->state != CXD2880_TNRDMD_STATE_ACTIVE) + return CXD2880_RESULT_ERROR_SW_STATE; + if (tnrdmd->sys != CXD2880_DTV_SYS_DVBT2) + return CXD2880_RESULT_ERROR_SW_STATE; + + { + u8 rdata[3]; + + if (tnrdmd->io->write_reg(tnrdmd->io, CXD2880_IO_TGT_DMD, + 0x00, 0x0B) != CXD2880_RESULT_OK) + return CXD2880_RESULT_ERROR_IO; + + if (tnrdmd->io->read_regs(tnrdmd->io, CXD2880_IO_TGT_DMD, + 0x18, rdata, 3) != CXD2880_RESULT_OK) + return CXD2880_RESULT_ERROR_IO; + + if ((rdata[0] & 0x01) == 0) + return CXD2880_RESULT_ERROR_HW_STATE; + + *block_err = (rdata[1] << 8) | rdata[2]; + + if (tnrdmd->io->write_reg(tnrdmd->io, CXD2880_IO_TGT_DMD, + 0x00, 0x24) != CXD2880_RESULT_OK) + return CXD2880_RESULT_ERROR_IO; + + if (tnrdmd->io->read_regs(tnrdmd->io, CXD2880_IO_TGT_DMD, + 0xDC, rdata, 1) != CXD2880_RESULT_OK) + return CXD2880_RESULT_ERROR_IO; + + *block_count = 1U << (rdata[0] & 0x0F); + } + + if ((*block_count == 0) || (*block_err > *block_count)) + return CXD2880_RESULT_ERROR_HW_STATE; + + return CXD2880_RESULT_OK; +} + +static void cxd2880_release(struct dvb_frontend *fe) +{ + struct cxd2880_priv *priv = NULL; + + if (!fe) { + pr_err("%s: invalid arg.\n", __func__); + return; + } + priv = (struct cxd2880_priv *)fe->demodulator_priv; + kfree(priv); +} + +static int cxd2880_init(struct dvb_frontend *fe) +{ + enum cxd2880_ret ret = CXD2880_RESULT_OK; + struct cxd2880_priv *priv = NULL; + struct cxd2880_tnrdmd_create_param create_param; + + if (!fe) { + pr_err("%s: invalid arg.\n", __func__); + return -EINVAL; + } + + priv = (struct cxd2880_priv *)fe->demodulator_priv; + + create_param.ts_output_if = CXD2880_TNRDMD_TSOUT_IF_SPI; + create_param.xtal_share_type = CXD2880_TNRDMD_XTAL_SHARE_NONE; + create_param.en_internal_ldo = 1; + create_param.xosc_cap = 18; + create_param.xosc_i = 8; + create_param.stationary_use = 1; + + mutex_lock(priv->spi_mutex); + if (priv->tnrdmd.io != &priv->regio) { + ret = cxd2880_tnrdmd_create(&priv->tnrdmd, + &priv->regio, &create_param); + if (ret != CXD2880_RESULT_OK) { + mutex_unlock(priv->spi_mutex); + dev_info(&priv->spi->dev, + "%s: cxd2880 tnrdmd create failed %d\n", + __func__, ret); + return return_tbl[ret]; + } + } + ret = cxd2880_integ_init(&priv->tnrdmd); + if (ret != CXD2880_RESULT_OK) { + mutex_unlock(priv->spi_mutex); + dev_err(&priv->spi->dev, "%s: cxd2880 integ init failed %d\n", + __func__, ret); + return return_tbl[ret]; + } + mutex_unlock(priv->spi_mutex); + + dev_dbg(&priv->spi->dev, "%s: OK.\n", __func__); + + return return_tbl[ret]; +} + +static int cxd2880_sleep(struct dvb_frontend *fe) +{ + enum cxd2880_ret ret = CXD2880_RESULT_OK; + struct cxd2880_priv *priv = NULL; + + if (!fe) { + pr_err("%s: inavlid arg\n", __func__); + return -EINVAL; + } + + priv = (struct cxd2880_priv *)fe->demodulator_priv; + + mutex_lock(priv->spi_mutex); + ret = cxd2880_tnrdmd_sleep(&priv->tnrdmd); + mutex_unlock(priv->spi_mutex); + + dev_dbg(&priv->spi->dev, "%s: tnrdmd_sleep ret %d\n", + __func__, ret); + + return return_tbl[ret]; +} + +static int cxd2880_read_signal_strength(struct dvb_frontend *fe, + u16 *strength) +{ + enum cxd2880_ret ret = CXD2880_RESULT_OK; + struct cxd2880_priv *priv = NULL; + struct dtv_frontend_properties *c = NULL; + int level = 0; + + if ((!fe) || (!strength)) { + pr_err("%s: inavlid arg\n", __func__); + return -EINVAL; + } + + priv = (struct cxd2880_priv *)fe->demodulator_priv; + c = &fe->dtv_property_cache; + + mutex_lock(priv->spi_mutex); + if ((c->delivery_system == SYS_DVBT) || + (c->delivery_system == SYS_DVBT2)) { + ret = cxd2880_tnrdmd_mon_rf_lvl(&priv->tnrdmd, &level); + } else { + dev_dbg(&priv->spi->dev, "%s: invalid system\n", __func__); + mutex_unlock(priv->spi_mutex); + return -EINVAL; + } + mutex_unlock(priv->spi_mutex); + + level /= 125; + /* -105dBm - -30dBm (-105000/125 = -840, -30000/125 = -240 */ + level = clamp(level, -840, -240); + /* scale value to 0x0000-0xFFFF */ + *strength = (u16)(((level + 840) * 0xFFFF) / (-240 + 840)); + + if (ret != CXD2880_RESULT_OK) + dev_dbg(&priv->spi->dev, "%s: ret = %d\n", __func__, ret); + + return return_tbl[ret]; +} + +static int cxd2880_read_snr(struct dvb_frontend *fe, u16 *snr) +{ + enum cxd2880_ret ret = CXD2880_RESULT_OK; + int snrvalue = 0; + struct cxd2880_priv *priv = NULL; + struct dtv_frontend_properties *c = NULL; + + if ((!fe) || (!snr)) { + pr_err("%s: inavlid arg\n", __func__); + return -EINVAL; + } + + priv = (struct cxd2880_priv *)fe->demodulator_priv; + c = &fe->dtv_property_cache; + + mutex_lock(priv->spi_mutex); + if (c->delivery_system == SYS_DVBT) { + ret = cxd2880_tnrdmd_dvbt_mon_snr(&priv->tnrdmd, + &snrvalue); + } else if (c->delivery_system == SYS_DVBT2) { + ret = cxd2880_tnrdmd_dvbt2_mon_snr(&priv->tnrdmd, + &snrvalue); + } else { + dev_err(&priv->spi->dev, "%s: invalid system\n", __func__); + mutex_unlock(priv->spi_mutex); + return -EINVAL; + } + mutex_unlock(priv->spi_mutex); + + if (snrvalue < 0) + snrvalue = 0; + *snr = (u16)snrvalue; + + if (ret != CXD2880_RESULT_OK) + dev_dbg(&priv->spi->dev, "%s: ret = %d\n", __func__, ret); + + return return_tbl[ret]; +} + +static int cxd2880_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks) +{ + enum cxd2880_ret ret = CXD2880_RESULT_OK; + struct cxd2880_priv *priv = NULL; + struct dtv_frontend_properties *c = NULL; + + if ((!fe) || (!ucblocks)) { + pr_err("%s: inavlid arg\n", __func__); + return -EINVAL; + } + + priv = (struct cxd2880_priv *)fe->demodulator_priv; + c = &fe->dtv_property_cache; + + mutex_lock(priv->spi_mutex); + if (c->delivery_system == SYS_DVBT) { + ret = cxd2880_tnrdmd_dvbt_mon_packet_error_number( + &priv->tnrdmd, + ucblocks); + } else if (c->delivery_system == SYS_DVBT2) { + ret = cxd2880_tnrdmd_dvbt2_mon_packet_error_number( + &priv->tnrdmd, + ucblocks); + } else { + dev_err(&priv->spi->dev, "%s: invlaid system\n", __func__); + mutex_unlock(priv->spi_mutex); + return -EINVAL; + } + mutex_unlock(priv->spi_mutex); + + if (ret != CXD2880_RESULT_OK) + dev_dbg(&priv->spi->dev, "%s: ret = %d\n", __func__, ret); + + return return_tbl[ret]; +} + +static int cxd2880_read_ber(struct dvb_frontend *fe, u32 *ber) +{ + enum cxd2880_ret ret = CXD2880_RESULT_OK; + struct cxd2880_priv *priv = NULL; + struct dtv_frontend_properties *c = NULL; + + if ((!fe) || (!ber)) { + pr_err("%s: inavlid arg\n", __func__); + return -EINVAL; + } + + priv = (struct cxd2880_priv *)fe->demodulator_priv; + c = &fe->dtv_property_cache; + + mutex_lock(priv->spi_mutex); + if (c->delivery_system == SYS_DVBT) { + ret = cxd2880_tnrdmd_dvbt_mon_pre_rsber(&priv->tnrdmd, + ber); + /* x100 to change unit.(10^7 -> 10^9 */ + *ber *= 100; + } else if (c->delivery_system == SYS_DVBT2) { + ret = cxd2880_tnrdmd_dvbt2_mon_pre_bchber(&priv->tnrdmd, + ber); + } else { + dev_err(&priv->spi->dev, "%s: invlaid system\n", __func__); + mutex_unlock(priv->spi_mutex); + return -EINVAL; + } + mutex_unlock(priv->spi_mutex); + + if (ret != CXD2880_RESULT_OK) + dev_dbg(&priv->spi->dev, "%s: ret = %d\n", __func__, ret); + + return return_tbl[ret]; +} + +static int cxd2880_set_frontend(struct dvb_frontend *fe) +{ + enum cxd2880_ret ret = CXD2880_RESULT_OK; + struct dtv_frontend_properties *c; + struct cxd2880_priv *priv; + enum cxd2880_dtv_bandwidth bw = CXD2880_DTV_BW_1_7_MHZ; + + if (!fe) { + pr_err("%s: inavlid arg\n", __func__); + return -EINVAL; + } + + priv = (struct cxd2880_priv *)fe->demodulator_priv; + c = &fe->dtv_property_cache; + + switch (c->bandwidth_hz) { + case 1712000: + bw = CXD2880_DTV_BW_1_7_MHZ; + break; + case 5000000: + bw = CXD2880_DTV_BW_5_MHZ; + break; + case 6000000: + bw = CXD2880_DTV_BW_6_MHZ; + break; + case 7000000: + bw = CXD2880_DTV_BW_7_MHZ; + break; + case 8000000: + bw = CXD2880_DTV_BW_8_MHZ; + break; + default: + return -EINVAL; + } + + dev_info(&priv->spi->dev, "%s: sys:%d freq:%d bw:%d\n", __func__, + c->delivery_system, c->frequency, bw); + mutex_lock(priv->spi_mutex); + if (c->delivery_system == SYS_DVBT) { + priv->tnrdmd.sys = CXD2880_DTV_SYS_DVBT; + priv->dvbt_tune_param.center_freq_khz = c->frequency / 1000; + priv->dvbt_tune_param.bandwidth = bw; + priv->dvbt_tune_param.profile = CXD2880_DVBT_PROFILE_HP; + ret = cxd2880_integ_dvbt_tune(&priv->tnrdmd, + &priv->dvbt_tune_param); + } else if (c->delivery_system == SYS_DVBT2) { + priv->tnrdmd.sys = CXD2880_DTV_SYS_DVBT2; + priv->dvbt2_tune_param.center_freq_khz = c->frequency / 1000; + priv->dvbt2_tune_param.bandwidth = bw; + priv->dvbt2_tune_param.data_plp_id = (u16)c->stream_id; + ret = cxd2880_integ_dvbt2_tune(&priv->tnrdmd, + &priv->dvbt2_tune_param); + } else { + dev_err(&priv->spi->dev, "%s: invalid system\n", __func__); + mutex_unlock(priv->spi_mutex); + return -EINVAL; + } + mutex_unlock(priv->spi_mutex); + dev_info(&priv->spi->dev, "%s: tune result %d\n", __func__, ret); + + return return_tbl[ret]; +} + +static int cxd2880_read_status(struct dvb_frontend *fe, + enum fe_status *status) +{ + enum cxd2880_ret ret = CXD2880_RESULT_OK; + u8 sync = 0; + u8 lock = 0; + u8 unlock = 0; + struct cxd2880_priv *priv = NULL; + struct dtv_frontend_properties *c = NULL; + + if ((!fe) || (!status)) { + pr_err("%s: invalid arg\n", __func__); + return -EINVAL; + } + + priv = (struct cxd2880_priv *)fe->demodulator_priv; + c = &fe->dtv_property_cache; + *status = 0; + + if (priv->tnrdmd.state == CXD2880_TNRDMD_STATE_ACTIVE) { + mutex_lock(priv->spi_mutex); + if (c->delivery_system == SYS_DVBT) { + ret = cxd2880_tnrdmd_dvbt_mon_sync_stat( + &priv->tnrdmd, + &sync, + &lock, + &unlock); + } else if (c->delivery_system == SYS_DVBT2) { + ret = cxd2880_tnrdmd_dvbt2_mon_sync_stat( + &priv->tnrdmd, + &sync, + &lock, + &unlock); + } else { + dev_err(&priv->spi->dev, + "%s: invlaid system", __func__); + mutex_unlock(priv->spi_mutex); + return -EINVAL; + } + + mutex_unlock(priv->spi_mutex); + if (ret != CXD2880_RESULT_OK) { + dev_err(&priv->spi->dev, "%s: failed. sys = %d\n", + __func__, priv->tnrdmd.sys); + return return_tbl[ret]; + } + + if (sync == 6) { + *status = FE_HAS_SIGNAL | + FE_HAS_CARRIER; + } + if (lock) + *status |= FE_HAS_VITERBI | + FE_HAS_SYNC | + FE_HAS_LOCK; + } + + dev_dbg(&priv->spi->dev, "%s: status %d result %d\n", __func__, + *status, ret); + + return return_tbl[CXD2880_RESULT_OK]; +} + +static int cxd2880_tune(struct dvb_frontend *fe, + bool retune, + unsigned int mode_flags, + unsigned int *delay, + enum fe_status *status) +{ + int ret = 0; + + if ((!fe) || (!delay) || (!status)) { + pr_err("%s: invalid arg.", __func__); + return -EINVAL; + } + + if (retune) { + ret = cxd2880_set_frontend(fe); + if (ret) { + pr_err("%s: cxd2880_set_frontend failed %d\n", + __func__, ret); + return ret; + } + } + + *delay = HZ / 5; + + return cxd2880_read_status(fe, status); +} + +static int cxd2880_get_frontend_t(struct dvb_frontend *fe, + struct dtv_frontend_properties *c) +{ + enum cxd2880_ret ret = CXD2880_RESULT_OK; + int result = 0; + struct cxd2880_priv *priv = NULL; + enum cxd2880_dvbt_mode mode = CXD2880_DVBT_MODE_2K; + enum cxd2880_dvbt_guard guard = CXD2880_DVBT_GUARD_1_32; + struct cxd2880_dvbt_tpsinfo tps; + enum cxd2880_tnrdmd_spectrum_sense sense; + u16 snr = 0; + int strength = 0; + u32 pre_bit_err = 0, pre_bit_count = 0; + u32 post_bit_err = 0, post_bit_count = 0; + u32 block_err = 0, block_count = 0; + + if ((!fe) || (!c)) { + pr_err("%s: invalid arg\n", __func__); + return -EINVAL; + } + + priv = (struct cxd2880_priv *)fe->demodulator_priv; + + mutex_lock(priv->spi_mutex); + ret = cxd2880_tnrdmd_dvbt_mon_mode_guard(&priv->tnrdmd, + &mode, &guard); + mutex_unlock(priv->spi_mutex); + if (ret == CXD2880_RESULT_OK) { + switch (mode) { + case CXD2880_DVBT_MODE_2K: + c->transmission_mode = TRANSMISSION_MODE_2K; + break; + case CXD2880_DVBT_MODE_8K: + c->transmission_mode = TRANSMISSION_MODE_8K; + break; + default: + c->transmission_mode = TRANSMISSION_MODE_2K; + dev_err(&priv->spi->dev, "%s: get invalid mode %d\n", + __func__, mode); + break; + } + switch (guard) { + case CXD2880_DVBT_GUARD_1_32: + c->guard_interval = GUARD_INTERVAL_1_32; + break; + case CXD2880_DVBT_GUARD_1_16: + c->guard_interval = GUARD_INTERVAL_1_16; + break; + case CXD2880_DVBT_GUARD_1_8: + c->guard_interval = GUARD_INTERVAL_1_8; + break; + case CXD2880_DVBT_GUARD_1_4: + c->guard_interval = GUARD_INTERVAL_1_4; + break; + default: + c->guard_interval = GUARD_INTERVAL_1_32; + dev_err(&priv->spi->dev, "%s: get invalid guard %d\n", + __func__, guard); + break; + } + } else { + c->transmission_mode = TRANSMISSION_MODE_2K; + c->guard_interval = GUARD_INTERVAL_1_32; + dev_dbg(&priv->spi->dev, + "%s: ModeGuard err %d\n", __func__, ret); + } + + mutex_lock(priv->spi_mutex); + ret = cxd2880_tnrdmd_dvbt_mon_tps_info(&priv->tnrdmd, &tps); + mutex_unlock(priv->spi_mutex); + if (ret == CXD2880_RESULT_OK) { + switch (tps.hierarchy) { + case CXD2880_DVBT_HIERARCHY_NON: + c->hierarchy = HIERARCHY_NONE; + break; + case CXD2880_DVBT_HIERARCHY_1: + c->hierarchy = HIERARCHY_1; + break; + case CXD2880_DVBT_HIERARCHY_2: + c->hierarchy = HIERARCHY_2; + break; + case CXD2880_DVBT_HIERARCHY_4: + c->hierarchy = HIERARCHY_4; + break; + default: + c->hierarchy = HIERARCHY_NONE; + dev_err(&priv->spi->dev, + "%s: TPSInfo hierarchy invalid %d\n", + __func__, tps.hierarchy); + break; + } + + switch (tps.rate_hp) { + case CXD2880_DVBT_CODERATE_1_2: + c->code_rate_HP = FEC_1_2; + break; + case CXD2880_DVBT_CODERATE_2_3: + c->code_rate_HP = FEC_2_3; + break; + case CXD2880_DVBT_CODERATE_3_4: + c->code_rate_HP = FEC_3_4; + break; + case CXD2880_DVBT_CODERATE_5_6: + c->code_rate_HP = FEC_5_6; + break; + case CXD2880_DVBT_CODERATE_7_8: + c->code_rate_HP = FEC_7_8; + break; + default: + c->code_rate_HP = FEC_NONE; + dev_err(&priv->spi->dev, + "%s: TPSInfo rateHP invalid %d\n", + __func__, tps.rate_hp); + break; + } + switch (tps.rate_lp) { + case CXD2880_DVBT_CODERATE_1_2: + c->code_rate_LP = FEC_1_2; + break; + case CXD2880_DVBT_CODERATE_2_3: + c->code_rate_LP = FEC_2_3; + break; + case CXD2880_DVBT_CODERATE_3_4: + c->code_rate_LP = FEC_3_4; + break; + case CXD2880_DVBT_CODERATE_5_6: + c->code_rate_LP = FEC_5_6; + break; + case CXD2880_DVBT_CODERATE_7_8: + c->code_rate_LP = FEC_7_8; + break; + default: + c->code_rate_LP = FEC_NONE; + dev_err(&priv->spi->dev, + "%s: TPSInfo rateLP invalid %d\n", + __func__, tps.rate_lp); + break; + } + switch (tps.constellation) { + case CXD2880_DVBT_CONSTELLATION_QPSK: + c->modulation = QPSK; + break; + case CXD2880_DVBT_CONSTELLATION_16QAM: + c->modulation = QAM_16; + break; + case CXD2880_DVBT_CONSTELLATION_64QAM: + c->modulation = QAM_64; + break; + default: + c->modulation = QPSK; + dev_err(&priv->spi->dev, + "%s: TPSInfo constellation invalid %d\n", + __func__, tps.constellation); + break; + } + } else { + c->hierarchy = HIERARCHY_NONE; + c->code_rate_HP = FEC_NONE; + c->code_rate_LP = FEC_NONE; + c->modulation = QPSK; + dev_dbg(&priv->spi->dev, + "%s: TPS info err %d\n", __func__, ret); + } + + mutex_lock(priv->spi_mutex); + ret = cxd2880_tnrdmd_dvbt_mon_spectrum_sense(&priv->tnrdmd, &sense); + mutex_unlock(priv->spi_mutex); + if (ret == CXD2880_RESULT_OK) { + switch (sense) { + case CXD2880_TNRDMD_SPECTRUM_NORMAL: + c->inversion = INVERSION_OFF; + break; + case CXD2880_TNRDMD_SPECTRUM_INV: + c->inversion = INVERSION_ON; + break; + default: + c->inversion = INVERSION_OFF; + dev_err(&priv->spi->dev, + "%s: spectrum sense invalid %d\n", + __func__, sense); + break; + } + } else { + c->inversion = INVERSION_OFF; + dev_dbg(&priv->spi->dev, + "%s: spectrum_sense %d\n", __func__, ret); + } + + mutex_lock(priv->spi_mutex); + ret = cxd2880_tnrdmd_mon_rf_lvl(&priv->tnrdmd, &strength); + mutex_unlock(priv->spi_mutex); + if (ret == CXD2880_RESULT_OK) { + c->strength.len = 1; + c->strength.stat[0].scale = FE_SCALE_DECIBEL; + c->strength.stat[0].svalue = strength; + } else { + c->strength.len = 1; + c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE; + dev_dbg(&priv->spi->dev, "%s: mon_rf_lvl %d\n", + __func__, result); + } + + result = cxd2880_read_snr(fe, &snr); + if (!result) { + c->cnr.len = 1; + c->cnr.stat[0].scale = FE_SCALE_DECIBEL; + c->cnr.stat[0].svalue = snr; + } else { + c->cnr.len = 1; + c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE; + dev_dbg(&priv->spi->dev, "%s: read_snr %d\n", __func__, result); + } + + mutex_lock(priv->spi_mutex); + ret = cxd2880_pre_bit_err_t(&priv->tnrdmd, &pre_bit_err, + &pre_bit_count); + mutex_unlock(priv->spi_mutex); + if (ret == CXD2880_RESULT_OK) { + c->pre_bit_error.len = 1; + c->pre_bit_error.stat[0].scale = FE_SCALE_COUNTER; + c->pre_bit_error.stat[0].uvalue = pre_bit_err; + c->pre_bit_count.len = 1; + c->pre_bit_count.stat[0].scale = FE_SCALE_COUNTER; + c->pre_bit_count.stat[0].uvalue = pre_bit_count; + } else { + c->pre_bit_error.len = 1; + c->pre_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE; + c->pre_bit_count.len = 1; + c->pre_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE; + dev_dbg(&priv->spi->dev, + "%s: pre_bit_error_t failed %d\n", + __func__, ret); + } + + mutex_lock(priv->spi_mutex); + ret = cxd2880_post_bit_err_t(&priv->tnrdmd, + &post_bit_err, &post_bit_count); + mutex_unlock(priv->spi_mutex); + if (ret == CXD2880_RESULT_OK) { + c->post_bit_error.len = 1; + c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER; + c->post_bit_error.stat[0].uvalue = post_bit_err; + c->post_bit_count.len = 1; + c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER; + c->post_bit_count.stat[0].uvalue = post_bit_count; + } else { + c->post_bit_error.len = 1; + c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE; + c->post_bit_count.len = 1; + c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE; + dev_dbg(&priv->spi->dev, + "%s: post_bit_err_t %d\n", __func__, ret); + } + + mutex_lock(priv->spi_mutex); + ret = cxd2880_read_block_err_t(&priv->tnrdmd, + &block_err, &block_count); + mutex_unlock(priv->spi_mutex); + if (ret == CXD2880_RESULT_OK) { + c->block_error.len = 1; + c->block_error.stat[0].scale = FE_SCALE_COUNTER; + c->block_error.stat[0].uvalue = block_err; + c->block_count.len = 1; + c->block_count.stat[0].scale = FE_SCALE_COUNTER; + c->block_count.stat[0].uvalue = block_count; + } else { + c->block_error.len = 1; + c->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE; + c->block_count.len = 1; + c->block_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE; + dev_dbg(&priv->spi->dev, + "%s: read_block_err_t %d\n", __func__, ret); + } + + return 0; +} + +static int cxd2880_get_frontend_t2(struct dvb_frontend *fe, + struct dtv_frontend_properties *c) +{ + enum cxd2880_ret ret = CXD2880_RESULT_OK; + int result = 0; + struct cxd2880_priv *priv = NULL; + struct cxd2880_dvbt2_l1pre l1pre; + enum cxd2880_dvbt2_plp_code_rate coderate; + enum cxd2880_dvbt2_plp_constell qam; + enum cxd2880_tnrdmd_spectrum_sense sense; + u16 snr = 0; + int strength = 0; + u32 pre_bit_err = 0, pre_bit_count = 0; + u32 post_bit_err = 0, post_bit_count = 0; + u32 block_err = 0, block_count = 0; + + if ((!fe) || (!c)) { + pr_err("%s: invalid arg.\n", __func__); + return -EINVAL; + } + + priv = (struct cxd2880_priv *)fe->demodulator_priv; + + mutex_lock(priv->spi_mutex); + ret = cxd2880_tnrdmd_dvbt2_mon_l1_pre(&priv->tnrdmd, &l1pre); + mutex_unlock(priv->spi_mutex); + if (ret == CXD2880_RESULT_OK) { + switch (l1pre.fft_mode) { + case CXD2880_DVBT2_M2K: + c->transmission_mode = TRANSMISSION_MODE_2K; + break; + case CXD2880_DVBT2_M8K: + c->transmission_mode = TRANSMISSION_MODE_8K; + break; + case CXD2880_DVBT2_M4K: + c->transmission_mode = TRANSMISSION_MODE_4K; + break; + case CXD2880_DVBT2_M1K: + c->transmission_mode = TRANSMISSION_MODE_1K; + break; + case CXD2880_DVBT2_M16K: + c->transmission_mode = TRANSMISSION_MODE_16K; + break; + case CXD2880_DVBT2_M32K: + c->transmission_mode = TRANSMISSION_MODE_32K; + break; + default: + c->transmission_mode = TRANSMISSION_MODE_2K; + dev_err(&priv->spi->dev, + "%s: L1Pre fft_mode invalid %d\n", + __func__, l1pre.fft_mode); + break; + } + switch (l1pre.gi) { + case CXD2880_DVBT2_G1_32: + c->guard_interval = GUARD_INTERVAL_1_32; + break; + case CXD2880_DVBT2_G1_16: + c->guard_interval = GUARD_INTERVAL_1_16; + break; + case CXD2880_DVBT2_G1_8: + c->guard_interval = GUARD_INTERVAL_1_8; + break; + case CXD2880_DVBT2_G1_4: + c->guard_interval = GUARD_INTERVAL_1_4; + break; + case CXD2880_DVBT2_G1_128: + c->guard_interval = GUARD_INTERVAL_1_128; + break; + case CXD2880_DVBT2_G19_128: + c->guard_interval = GUARD_INTERVAL_19_128; + break; + case CXD2880_DVBT2_G19_256: + c->guard_interval = GUARD_INTERVAL_19_256; + break; + default: + c->guard_interval = GUARD_INTERVAL_1_32; + dev_err(&priv->spi->dev, + "%s: L1Pre gi invalid %d\n", + __func__, l1pre.gi); + break; + } + } else { + c->transmission_mode = TRANSMISSION_MODE_2K; + c->guard_interval = GUARD_INTERVAL_1_32; + dev_dbg(&priv->spi->dev, + "%s: L1Pre err %d\n", __func__, ret); + } + + mutex_lock(priv->spi_mutex); + ret = cxd2880_tnrdmd_dvbt2_mon_code_rate(&priv->tnrdmd, + CXD2880_DVBT2_PLP_DATA, + &coderate); + mutex_unlock(priv->spi_mutex); + if (ret == CXD2880_RESULT_OK) { + switch (coderate) { + case CXD2880_DVBT2_R1_2: + c->fec_inner = FEC_1_2; + break; + case CXD2880_DVBT2_R3_5: + c->fec_inner = FEC_3_5; + break; + case CXD2880_DVBT2_R2_3: + c->fec_inner = FEC_2_3; + break; + case CXD2880_DVBT2_R3_4: + c->fec_inner = FEC_3_4; + break; + case CXD2880_DVBT2_R4_5: + c->fec_inner = FEC_4_5; + break; + case CXD2880_DVBT2_R5_6: + c->fec_inner = FEC_5_6; + break; + default: + c->fec_inner = FEC_NONE; + dev_err(&priv->spi->dev, + "%s: CodeRate invalid %d\n", + __func__, coderate); + break; + } + } else { + c->fec_inner = FEC_NONE; + dev_dbg(&priv->spi->dev, "%s: CodeRate %d\n", __func__, ret); + } + + mutex_lock(priv->spi_mutex); + ret = cxd2880_tnrdmd_dvbt2_mon_qam(&priv->tnrdmd, + CXD2880_DVBT2_PLP_DATA, + &qam); + mutex_unlock(priv->spi_mutex); + if (ret == CXD2880_RESULT_OK) { + switch (qam) { + case CXD2880_DVBT2_QPSK: + c->modulation = QPSK; + break; + case CXD2880_DVBT2_QAM16: + c->modulation = QAM_16; + break; + case CXD2880_DVBT2_QAM64: + c->modulation = QAM_64; + break; + case CXD2880_DVBT2_QAM256: + c->modulation = QAM_256; + break; + default: + c->modulation = QPSK; + dev_err(&priv->spi->dev, + "%s: QAM invalid %d\n", + __func__, qam); + break; + } + } else { + c->modulation = QPSK; + dev_dbg(&priv->spi->dev, "%s: QAM %d\n", __func__, ret); + } + + mutex_lock(priv->spi_mutex); + ret = cxd2880_tnrdmd_dvbt2_mon_spectrum_sense(&priv->tnrdmd, &sense); + mutex_unlock(priv->spi_mutex); + if (ret == CXD2880_RESULT_OK) { + switch (sense) { + case CXD2880_TNRDMD_SPECTRUM_NORMAL: + c->inversion = INVERSION_OFF; + break; + case CXD2880_TNRDMD_SPECTRUM_INV: + c->inversion = INVERSION_ON; + break; + default: + c->inversion = INVERSION_OFF; + dev_err(&priv->spi->dev, + "%s: spectrum sense invalid %d\n", + __func__, sense); + break; + } + } else { + c->inversion = INVERSION_OFF; + dev_dbg(&priv->spi->dev, + "%s: SpectrumSense %d\n", __func__, ret); + } + + mutex_lock(priv->spi_mutex); + ret = cxd2880_tnrdmd_mon_rf_lvl(&priv->tnrdmd, &strength); + mutex_unlock(priv->spi_mutex); + if (ret == CXD2880_RESULT_OK) { + c->strength.len = 1; + c->strength.stat[0].scale = FE_SCALE_DECIBEL; + c->strength.stat[0].svalue = strength; + } else { + c->strength.len = 1; + c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE; + dev_dbg(&priv->spi->dev, + "%s: mon_rf_lvl %d\n", __func__, ret); + } + + result = cxd2880_read_snr(fe, &snr); + if (!result) { + c->cnr.len = 1; + c->cnr.stat[0].scale = FE_SCALE_DECIBEL; + c->cnr.stat[0].svalue = snr; + } else { + c->cnr.len = 1; + c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE; + dev_dbg(&priv->spi->dev, "%s: read_snr %d\n", __func__, result); + } + + mutex_lock(priv->spi_mutex); + ret = cxd2880_pre_bit_err_t2(&priv->tnrdmd, + &pre_bit_err, + &pre_bit_count); + mutex_unlock(priv->spi_mutex); + if (ret == CXD2880_RESULT_OK) { + c->pre_bit_error.len = 1; + c->pre_bit_error.stat[0].scale = FE_SCALE_COUNTER; + c->pre_bit_error.stat[0].uvalue = pre_bit_err; + c->pre_bit_count.len = 1; + c->pre_bit_count.stat[0].scale = FE_SCALE_COUNTER; + c->pre_bit_count.stat[0].uvalue = pre_bit_count; + } else { + c->pre_bit_error.len = 1; + c->pre_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE; + c->pre_bit_count.len = 1; + c->pre_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE; + dev_dbg(&priv->spi->dev, + "%s: read_bit_err_t2 %d\n", __func__, ret); + } + + mutex_lock(priv->spi_mutex); + ret = cxd2880_post_bit_err_t2(&priv->tnrdmd, + &post_bit_err, &post_bit_count); + mutex_unlock(priv->spi_mutex); + if (ret == CXD2880_RESULT_OK) { + c->post_bit_error.len = 1; + c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER; + c->post_bit_error.stat[0].uvalue = post_bit_err; + c->post_bit_count.len = 1; + c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER; + c->post_bit_count.stat[0].uvalue = post_bit_count; + } else { + c->post_bit_error.len = 1; + c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE; + c->post_bit_count.len = 1; + c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE; + dev_dbg(&priv->spi->dev, + "%s: post_bit_err_t2 %d\n", __func__, ret); + } + + mutex_lock(priv->spi_mutex); + ret = cxd2880_read_block_err_t2(&priv->tnrdmd, + &block_err, &block_count); + mutex_unlock(priv->spi_mutex); + if (ret == CXD2880_RESULT_OK) { + c->block_error.len = 1; + c->block_error.stat[0].scale = FE_SCALE_COUNTER; + c->block_error.stat[0].uvalue = block_err; + c->block_count.len = 1; + c->block_count.stat[0].scale = FE_SCALE_COUNTER; + c->block_count.stat[0].uvalue = block_count; + } else { + c->block_error.len = 1; + c->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE; + c->block_count.len = 1; + c->block_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE; + dev_dbg(&priv->spi->dev, + "%s: read_block_err_t2 %d\n", __func__, ret); + } + + return 0; +} + +static int cxd2880_get_frontend(struct dvb_frontend *fe, + struct dtv_frontend_properties *props) +{ + struct cxd2880_priv *priv = NULL; + int result = 0; + + if ((!fe) || (!props)) { + pr_err("%s: invalid arg.", __func__); + return -EINVAL; + } + + priv = (struct cxd2880_priv *)fe->demodulator_priv; + + dev_dbg(&priv->spi->dev, "%s: system=%d\n", __func__, + fe->dtv_property_cache.delivery_system); + switch (fe->dtv_property_cache.delivery_system) { + case SYS_DVBT: + result = cxd2880_get_frontend_t(fe, props); + break; + case SYS_DVBT2: + result = cxd2880_get_frontend_t2(fe, props); + break; + default: + result = -EINVAL; + break; + } + + return result; +} + +static enum dvbfe_algo cxd2880_get_frontend_algo(struct dvb_frontend *fe) +{ + return DVBFE_ALGO_HW; +} + +static struct dvb_frontend_ops cxd2880_dvbt_t2_ops; + +struct dvb_frontend *cxd2880_attach(struct dvb_frontend *fe, + struct cxd2880_config *cfg) +{ + enum cxd2880_ret ret = CXD2880_RESULT_OK; + enum cxd2880_tnrdmd_chip_id chipid = + CXD2880_TNRDMD_CHIP_ID_UNKNOWN; + static struct cxd2880_priv *priv; + u8 data = 0; + + if (!fe) { + pr_err("%s: invalid arg.\n", __func__); + return NULL; + } + + priv = kzalloc(sizeof(struct cxd2880_priv), GFP_KERNEL); + if (!priv) + return NULL; + + priv->spi = cfg->spi; + priv->spi_mutex = cfg->spi_mutex; + priv->spi_device.spi = cfg->spi; + + memcpy(&fe->ops, &cxd2880_dvbt_t2_ops, + sizeof(struct dvb_frontend_ops)); + + ret = cxd2880_spi_device_initialize(&priv->spi_device, + CXD2880_SPI_MODE_0, + 55000000); + if (ret != CXD2880_RESULT_OK) { + dev_err(&priv->spi->dev, + "%s: spi_device_initialize failed. %d\n", + __func__, ret); + kfree(priv); + return NULL; + } + + ret = cxd2880_spi_device_create_spi(&priv->cxd2880_spi, + &priv->spi_device); + if (ret != CXD2880_RESULT_OK) { + dev_err(&priv->spi->dev, + "%s: spi_device_create_spi failed. %d\n", + __func__, ret); + kfree(priv); + return NULL; + } + + ret = cxd2880_io_spi_create(&priv->regio, &priv->cxd2880_spi, 0); + if (ret != CXD2880_RESULT_OK) { + dev_err(&priv->spi->dev, + "%s: io_spi_create failed. %d\n", __func__, ret); + kfree(priv); + return NULL; + } + if (priv->regio.write_reg(&priv->regio, CXD2880_IO_TGT_SYS, 0x00, 0x00) + != CXD2880_RESULT_OK) { + dev_err(&priv->spi->dev, + "%s: set bank to 0x00 failed.\n", __func__); + kfree(priv); + return NULL; + } + if (priv->regio.read_regs(&priv->regio, + CXD2880_IO_TGT_SYS, 0xFD, &data, 1) + != CXD2880_RESULT_OK) { + dev_err(&priv->spi->dev, + "%s: read chip id failed.\n", __func__); + kfree(priv); + return NULL; + } + + chipid = (enum cxd2880_tnrdmd_chip_id)data; + if ((chipid != CXD2880_TNRDMD_CHIP_ID_CXD2880_ES1_0X) && + (chipid != CXD2880_TNRDMD_CHIP_ID_CXD2880_ES1_11)) { + dev_err(&priv->spi->dev, + "%s: chip id invalid.\n", __func__); + kfree(priv); + return NULL; + } + + fe->demodulator_priv = priv; + dev_info(&priv->spi->dev, + "CXD2880 driver version: Ver %s\n", + CXD2880_TNRDMD_DRIVER_VERSION); + + return fe; +} +EXPORT_SYMBOL(cxd2880_attach); + +static struct dvb_frontend_ops cxd2880_dvbt_t2_ops = { + .info = { + .name = "Sony CXD2880", + .frequency_min = 174000000, + .frequency_max = 862000000, + .frequency_stepsize = 1000, + .caps = FE_CAN_INVERSION_AUTO | + FE_CAN_FEC_1_2 | + FE_CAN_FEC_2_3 | + FE_CAN_FEC_3_4 | + FE_CAN_FEC_4_5 | + FE_CAN_FEC_5_6 | + FE_CAN_FEC_7_8 | + FE_CAN_FEC_AUTO | + FE_CAN_QPSK | + FE_CAN_QAM_16 | + FE_CAN_QAM_32 | + FE_CAN_QAM_64 | + FE_CAN_QAM_128 | + FE_CAN_QAM_256 | + FE_CAN_QAM_AUTO | + FE_CAN_TRANSMISSION_MODE_AUTO | + FE_CAN_GUARD_INTERVAL_AUTO | + FE_CAN_2G_MODULATION | + FE_CAN_RECOVER | + FE_CAN_MUTE_TS, + }, + .delsys = { SYS_DVBT, SYS_DVBT2 }, + + .release = cxd2880_release, + .init = cxd2880_init, + .sleep = cxd2880_sleep, + .tune = cxd2880_tune, + .set_frontend = cxd2880_set_frontend, + .get_frontend = cxd2880_get_frontend, + .read_status = cxd2880_read_status, + .read_ber = cxd2880_read_ber, + .read_signal_strength = cxd2880_read_signal_strength, + .read_snr = cxd2880_read_snr, + .read_ucblocks = cxd2880_read_ucblocks, + .get_frontend_algo = cxd2880_get_frontend_algo, +}; + +MODULE_DESCRIPTION( +"Sony CXD2880 DVB-T2/T tuner + demodulator drvier"); +MODULE_AUTHOR("Sony Semiconductor Solutions Corporation"); +MODULE_LICENSE("GPL v2"); -- 2.11.0