Received: by 2002:ac0:a581:0:0:0:0:0 with SMTP id m1-v6csp3018801imm; Sun, 24 Jun 2018 09:32:41 -0700 (PDT) X-Google-Smtp-Source: ADUXVKLcE7+o02nLhPM/3Ra9EJIORxt7z75lAZCWlH9s83AUkcN9o3LfcVNU3aD8zh0sMRI6GEI1 X-Received: by 2002:a17:902:b706:: with SMTP id d6-v6mr9328704pls.105.1529857961778; Sun, 24 Jun 2018 09:32:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1529857961; cv=none; d=google.com; s=arc-20160816; b=RvoYyAor/VzB44KhvWyUwIzvFDF5LHwVa9czcPXuE7/QYjUD0DEZJf32NImTArG0fk +51z2KiUbuF4PESxCR3QGj0N3HMJpwqFepR2Ur1AkyA3P45aRPw7FOMA/iWo/2vMH0VE pcRm667v+mjjzZ6sP+LHC68ETBqY5UdIJgAl8uQm3XRhFDOiatIDFZLIruHzhFPwWQ1M R+d9eHCoE7evekeqWszo0Wjuw3wIyF2ZYQBIziKN/jX0BHMP2J4K5iANgel0OUerqX/8 8EDA9JC2evoUEbRNNk9mEXAkaQy0hXZ0QZd61RkWkxcWUScTPBbdWgE3etdlzDW8w63u Ze8w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:subject:references:in-reply-to:message-id :date:cc:to:from:arc-authentication-results; bh=wM77s/qABbwGTuSz1zV5tG+Ez4e03bsmQT7PEcAvlVw=; b=uryW7lYzhmXBIlbhrrPaMB7peBxcLPBs+PXiG0mCeTU7z9TWW16CxQ25gePvCh74tQ bSdgKdZ2BZptk8DFxZFrDpvl4nAoSHj7NWniXgrTp74ptSuFFF0xFinpaay95RvVMGLR WYGIz10xY+pTJguDNihsOZ/snSkp4revrKA5JJ8D70gT5o7+LITX2S3CdDPvcoModQSz eVT2bmZK9KIU8c4DoNsfWAIB6oAgoeVH2bz9/K0rYdudidfuESH53xeo3yQC9lrxqsLw h08B2vkeiF4bYCbASrjd+GtSZTQSocrVur4ld2Lqzioym60btbvT9jshBLAwkHszuX9c 7VDQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id u8-v6si980279plz.300.2018.06.24.09.32.27; Sun, 24 Jun 2018 09:32:41 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751624AbeFXQbs (ORCPT + 99 others); Sun, 24 Jun 2018 12:31:48 -0400 Received: from mail.CARNet.hr ([161.53.123.6]:40167 "EHLO mail.carnet.hr" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750713AbeFXQbr (ORCPT ); Sun, 24 Jun 2018 12:31:47 -0400 Received: from [2001:b68:ff:12::131] (port=56676 helo=gavran.carpriv.carnet.hr) by mail.carnet.hr with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1fX7vS-0007A8-Jy; Sun, 24 Jun 2018 18:31:43 +0200 Received: by gavran.carpriv.carnet.hr (Postfix, from userid 1000) id 722A923A9E; Sun, 24 Jun 2018 18:31:42 +0200 (CEST) From: Valentin Vidic To: Greg Kroah-Hartman Cc: =?UTF-8?q?Simon=20Sandstr=C3=B6m?= , Marcus Wolf , =?UTF-8?q?Luca=20S=C3=B6the?= , Marcin Ciupak , Michael Panzlaff , Derek Robson , devel@driverdev.osuosl.org, linux-kernel@vger.kernel.org, Hugo Lefeuvre , Dan Carpenter , Joe Perches , Valentin Vidic Date: Sun, 24 Jun 2018 18:31:38 +0200 Message-Id: <20180624163138.30976-1-Valentin.Vidic@CARNet.hr> X-Mailer: git-send-email 2.18.0.rc2 In-Reply-To: <20180624162505.29600-1-Valentin.Vidic@CARNet.hr> References: <20180624094245.23741-1-Valentin.Vidic@CARNet.hr> X-SA-Exim-Connect-IP: 2001:b68:ff:12::131 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on rigel.CARNet.hr X-Spam-Level: X-Spam-Status: No, score=-2.9 required=10.0 tests=ALL_TRUSTED,BAYES_00, URIBL_BLOCKED autolearn=ham version=3.3.2 Subject: [PATCH v3] staging: pi433: replace simple switch statements X-SA-Exim-Version: 4.2.1 (built Mon, 26 Dec 2011 16:24:06 +0000) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Use const array to map switch cases to resulting values. Signed-off-by: Valentin Vidic --- v2: use correct type for const arrays v3: add missing static keyword for af_map drivers/staging/pi433/rf69.c | 233 ++++++++++++++--------------------- 1 file changed, 93 insertions(+), 140 deletions(-) diff --git a/drivers/staging/pi433/rf69.c b/drivers/staging/pi433/rf69.c index 90280e9b006d..341d6901a801 100644 --- a/drivers/staging/pi433/rf69.c +++ b/drivers/staging/pi433/rf69.c @@ -111,27 +111,22 @@ static inline int rf69_read_mod_write(struct spi_device *spi, u8 reg, int rf69_set_mode(struct spi_device *spi, enum mode mode) { - switch (mode) { - case transmit: - return rf69_read_mod_write(spi, REG_OPMODE, MASK_OPMODE_MODE, - OPMODE_MODE_TRANSMIT); - case receive: - return rf69_read_mod_write(spi, REG_OPMODE, MASK_OPMODE_MODE, - OPMODE_MODE_RECEIVE); - case synthesizer: - return rf69_read_mod_write(spi, REG_OPMODE, MASK_OPMODE_MODE, - OPMODE_MODE_SYNTHESIZER); - case standby: - return rf69_read_mod_write(spi, REG_OPMODE, MASK_OPMODE_MODE, - OPMODE_MODE_STANDBY); - case mode_sleep: - return rf69_read_mod_write(spi, REG_OPMODE, MASK_OPMODE_MODE, - OPMODE_MODE_SLEEP); - default: + static const u8 mode_map[] = { + [transmit] = OPMODE_MODE_TRANSMIT, + [receive] = OPMODE_MODE_RECEIVE, + [synthesizer] = OPMODE_MODE_SYNTHESIZER, + [standby] = OPMODE_MODE_STANDBY, + [mode_sleep] = OPMODE_MODE_SLEEP, + }; + + if (unlikely(mode >= ARRAY_SIZE(mode_map))) { dev_dbg(&spi->dev, "set: illegal input param"); return -EINVAL; } + return rf69_read_mod_write(spi, REG_OPMODE, MASK_OPMODE_MODE, + mode_map[mode]); + // we are using packet mode, so this check is not really needed // but waiting for mode ready is necessary when going from sleep because the FIFO may not be immediately available from previous mode //while (_mode == RF69_MODE_SLEEP && (READ_REG(REG_IRQFLAGS1) & RF_IRQFLAGS1_MODEREADY) == 0x00); // Wait for ModeReady @@ -145,19 +140,19 @@ int rf69_set_data_mode(struct spi_device *spi, u8 data_mode) int rf69_set_modulation(struct spi_device *spi, enum modulation modulation) { - switch (modulation) { - case OOK: - return rf69_read_mod_write(spi, REG_DATAMODUL, - MASK_DATAMODUL_MODULATION_TYPE, - DATAMODUL_MODULATION_TYPE_OOK); - case FSK: - return rf69_read_mod_write(spi, REG_DATAMODUL, - MASK_DATAMODUL_MODULATION_TYPE, - DATAMODUL_MODULATION_TYPE_FSK); - default: + static const u8 modulation_map[] = { + [OOK] = DATAMODUL_MODULATION_TYPE_OOK, + [FSK] = DATAMODUL_MODULATION_TYPE_FSK, + }; + + if (unlikely(modulation >= ARRAY_SIZE(modulation_map))) { dev_dbg(&spi->dev, "set: illegal input param"); return -EINVAL; } + + return rf69_read_mod_write(spi, REG_DATAMODUL, + MASK_DATAMODUL_MODULATION_TYPE, + modulation_map[modulation]); } static enum modulation rf69_get_modulation(struct spi_device *spi) @@ -373,43 +368,30 @@ int rf69_set_output_power_level(struct spi_device *spi, u8 power_level) int rf69_set_pa_ramp(struct spi_device *spi, enum pa_ramp pa_ramp) { - switch (pa_ramp) { - case ramp3400: - return rf69_write_reg(spi, REG_PARAMP, PARAMP_3400); - case ramp2000: - return rf69_write_reg(spi, REG_PARAMP, PARAMP_2000); - case ramp1000: - return rf69_write_reg(spi, REG_PARAMP, PARAMP_1000); - case ramp500: - return rf69_write_reg(spi, REG_PARAMP, PARAMP_500); - case ramp250: - return rf69_write_reg(spi, REG_PARAMP, PARAMP_250); - case ramp125: - return rf69_write_reg(spi, REG_PARAMP, PARAMP_125); - case ramp100: - return rf69_write_reg(spi, REG_PARAMP, PARAMP_100); - case ramp62: - return rf69_write_reg(spi, REG_PARAMP, PARAMP_62); - case ramp50: - return rf69_write_reg(spi, REG_PARAMP, PARAMP_50); - case ramp40: - return rf69_write_reg(spi, REG_PARAMP, PARAMP_40); - case ramp31: - return rf69_write_reg(spi, REG_PARAMP, PARAMP_31); - case ramp25: - return rf69_write_reg(spi, REG_PARAMP, PARAMP_25); - case ramp20: - return rf69_write_reg(spi, REG_PARAMP, PARAMP_20); - case ramp15: - return rf69_write_reg(spi, REG_PARAMP, PARAMP_15); - case ramp12: - return rf69_write_reg(spi, REG_PARAMP, PARAMP_12); - case ramp10: - return rf69_write_reg(spi, REG_PARAMP, PARAMP_10); - default: + static const u8 pa_ramp_map[] = { + [ramp3400] = PARAMP_3400, + [ramp2000] = PARAMP_2000, + [ramp1000] = PARAMP_1000, + [ramp500] = PARAMP_500, + [ramp250] = PARAMP_250, + [ramp125] = PARAMP_125, + [ramp100] = PARAMP_100, + [ramp62] = PARAMP_62, + [ramp50] = PARAMP_50, + [ramp40] = PARAMP_40, + [ramp31] = PARAMP_31, + [ramp25] = PARAMP_25, + [ramp20] = PARAMP_20, + [ramp15] = PARAMP_15, + [ramp10] = PARAMP_10, + }; + + if (unlikely(pa_ramp >= ARRAY_SIZE(pa_ramp_map))) { dev_dbg(&spi->dev, "set: illegal input param"); return -EINVAL; } + + return rf69_write_reg(spi, REG_PARAMP, pa_ramp_map[pa_ramp]); } int rf69_set_antenna_impedance(struct spi_device *spi, @@ -428,32 +410,23 @@ int rf69_set_antenna_impedance(struct spi_device *spi, int rf69_set_lna_gain(struct spi_device *spi, enum lna_gain lna_gain) { - switch (lna_gain) { - case automatic: - return rf69_read_mod_write(spi, REG_LNA, MASK_LNA_GAIN, - LNA_GAIN_AUTO); - case max: - return rf69_read_mod_write(spi, REG_LNA, MASK_LNA_GAIN, - LNA_GAIN_MAX); - case max_minus_6: - return rf69_read_mod_write(spi, REG_LNA, MASK_LNA_GAIN, - LNA_GAIN_MAX_MINUS_6); - case max_minus_12: - return rf69_read_mod_write(spi, REG_LNA, MASK_LNA_GAIN, - LNA_GAIN_MAX_MINUS_12); - case max_minus_24: - return rf69_read_mod_write(spi, REG_LNA, MASK_LNA_GAIN, - LNA_GAIN_MAX_MINUS_24); - case max_minus_36: - return rf69_read_mod_write(spi, REG_LNA, MASK_LNA_GAIN, - LNA_GAIN_MAX_MINUS_36); - case max_minus_48: - return rf69_read_mod_write(spi, REG_LNA, MASK_LNA_GAIN, - LNA_GAIN_MAX_MINUS_48); - default: + static const u8 lna_gain_map[] = { + [automatic] = LNA_GAIN_AUTO, + [max] = LNA_GAIN_MAX, + [max_minus_6] = LNA_GAIN_MAX_MINUS_6, + [max_minus_12] = LNA_GAIN_MAX_MINUS_12, + [max_minus_24] = LNA_GAIN_MAX_MINUS_24, + [max_minus_36] = LNA_GAIN_MAX_MINUS_36, + [max_minus_48] = LNA_GAIN_MAX_MINUS_48, + }; + + if (unlikely(lna_gain >= ARRAY_SIZE(lna_gain_map))) { dev_dbg(&spi->dev, "set: illegal input param"); return -EINVAL; } + + return rf69_read_mod_write(spi, REG_LNA, MASK_LNA_GAIN, + lna_gain_map[lna_gain]); } static int rf69_set_bandwidth_intern(struct spi_device *spi, u8 reg, @@ -516,43 +489,24 @@ int rf69_set_bandwidth_during_afc(struct spi_device *spi, int rf69_set_ook_threshold_dec(struct spi_device *spi, enum threshold_decrement threshold_decrement) { - switch (threshold_decrement) { - case dec_every8th: - return rf69_read_mod_write(spi, REG_OOKPEAK, - MASK_OOKPEAK_THRESDEC, - OOKPEAK_THRESHDEC_EVERY_8TH); - case dec_every4th: - return rf69_read_mod_write(spi, REG_OOKPEAK, - MASK_OOKPEAK_THRESDEC, - OOKPEAK_THRESHDEC_EVERY_4TH); - case dec_every2nd: - return rf69_read_mod_write(spi, REG_OOKPEAK, - MASK_OOKPEAK_THRESDEC, - OOKPEAK_THRESHDEC_EVERY_2ND); - case dec_once: - return rf69_read_mod_write(spi, REG_OOKPEAK, - MASK_OOKPEAK_THRESDEC, - OOKPEAK_THRESHDEC_ONCE); - case dec_twice: - return rf69_read_mod_write(spi, REG_OOKPEAK, - MASK_OOKPEAK_THRESDEC, - OOKPEAK_THRESHDEC_TWICE); - case dec_4times: - return rf69_read_mod_write(spi, REG_OOKPEAK, - MASK_OOKPEAK_THRESDEC, - OOKPEAK_THRESHDEC_4_TIMES); - case dec_8times: - return rf69_read_mod_write(spi, REG_OOKPEAK, - MASK_OOKPEAK_THRESDEC, - OOKPEAK_THRESHDEC_8_TIMES); - case dec_16times: - return rf69_read_mod_write(spi, REG_OOKPEAK, - MASK_OOKPEAK_THRESDEC, - OOKPEAK_THRESHDEC_16_TIMES); - default: + static const u8 td_map[] = { + [dec_every8th] = OOKPEAK_THRESHDEC_EVERY_8TH, + [dec_every4th] = OOKPEAK_THRESHDEC_EVERY_4TH, + [dec_every2nd] = OOKPEAK_THRESHDEC_EVERY_2ND, + [dec_once] = OOKPEAK_THRESHDEC_ONCE, + [dec_twice] = OOKPEAK_THRESHDEC_TWICE, + [dec_4times] = OOKPEAK_THRESHDEC_4_TIMES, + [dec_8times] = OOKPEAK_THRESHDEC_8_TIMES, + [dec_16times] = OOKPEAK_THRESHDEC_16_TIMES, + }; + + if (unlikely(threshold_decrement >= ARRAY_SIZE(td_map))) { dev_dbg(&spi->dev, "set: illegal input param"); return -EINVAL; } + + return rf69_read_mod_write(spi, REG_OOKPEAK, MASK_OOKPEAK_THRESDEC, + td_map[threshold_decrement]); } int rf69_set_dio_mapping(struct spi_device *spi, u8 dio_number, u8 value) @@ -749,23 +703,21 @@ int rf69_disable_crc(struct spi_device *spi) int rf69_set_address_filtering(struct spi_device *spi, enum address_filtering address_filtering) { - switch (address_filtering) { - case filtering_off: - return rf69_read_mod_write(spi, REG_PACKETCONFIG1, - MASK_PACKETCONFIG1_ADDRESSFILTERING, - PACKETCONFIG1_ADDRESSFILTERING_OFF); - case node_address: - return rf69_read_mod_write(spi, REG_PACKETCONFIG1, - MASK_PACKETCONFIG1_ADDRESSFILTERING, - PACKETCONFIG1_ADDRESSFILTERING_NODE); - case node_or_broadcast_address: - return rf69_read_mod_write(spi, REG_PACKETCONFIG1, - MASK_PACKETCONFIG1_ADDRESSFILTERING, - PACKETCONFIG1_ADDRESSFILTERING_NODEBROADCAST); - default: + static const u8 af_map[] = { + [filtering_off] = PACKETCONFIG1_ADDRESSFILTERING_OFF, + [node_address] = PACKETCONFIG1_ADDRESSFILTERING_NODE, + [node_or_broadcast_address] = + PACKETCONFIG1_ADDRESSFILTERING_NODEBROADCAST, + }; + + if (unlikely(address_filtering >= ARRAY_SIZE(af_map))) { dev_dbg(&spi->dev, "set: illegal input param"); return -EINVAL; } + + return rf69_read_mod_write(spi, REG_PACKETCONFIG1, + MASK_PACKETCONFIG1_ADDRESSFILTERING, + af_map[address_filtering]); } int rf69_set_payload_length(struct spi_device *spi, u8 payload_length) @@ -824,17 +776,18 @@ int rf69_set_fifo_threshold(struct spi_device *spi, u8 threshold) int rf69_set_dagc(struct spi_device *spi, enum dagc dagc) { - switch (dagc) { - case normal_mode: - return rf69_write_reg(spi, REG_TESTDAGC, DAGC_NORMAL); - case improve: - return rf69_write_reg(spi, REG_TESTDAGC, DAGC_IMPROVED_LOWBETA0); - case improve_for_low_modulation_index: - return rf69_write_reg(spi, REG_TESTDAGC, DAGC_IMPROVED_LOWBETA1); - default: + static const u8 dagc_map[] = { + [normal_mode] = DAGC_NORMAL, + [improve] = DAGC_IMPROVED_LOWBETA0, + [improve_for_low_modulation_index] = DAGC_IMPROVED_LOWBETA1, + }; + + if (unlikely(dagc >= ARRAY_SIZE(dagc_map))) { dev_dbg(&spi->dev, "set: illegal input param"); return -EINVAL; } + + return rf69_write_reg(spi, REG_TESTDAGC, dagc_map[dagc]); } /*-------------------------------------------------------------------------*/ -- 2.18.0.rc2