From patchwork Fri Feb 15 12:56:54 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lee Jones X-Patchwork-Id: 14908 Return-Path: X-Original-To: patchwork@peony.canonical.com Delivered-To: patchwork@peony.canonical.com Received: from fiordland.canonical.com (fiordland.canonical.com [91.189.94.145]) by peony.canonical.com (Postfix) with ESMTP id 4974923E01 for ; Fri, 15 Feb 2013 12:58:08 +0000 (UTC) Received: from mail-ve0-f170.google.com (mail-ve0-f170.google.com [209.85.128.170]) by fiordland.canonical.com (Postfix) with ESMTP id E45ABA18025 for ; Fri, 15 Feb 2013 12:58:07 +0000 (UTC) Received: by mail-ve0-f170.google.com with SMTP id 14so2994814vea.15 for ; Fri, 15 Feb 2013 04:58:07 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=x-received:x-forwarded-to:x-forwarded-for:delivered-to:x-received :received-spf:x-received:from:to:cc:subject:date:message-id:x-mailer :in-reply-to:references:x-gm-message-state; bh=weFG23do6G4x6p1Dzevf5hIYcpyKeC0YZWuCI+4qn98=; b=J4x4SgXRe6Cg1WYuYkUN8qAKS27L5BCrU4n3eGzAjdU0u5KkANIC3/LKl/3vyeIOvX T1cYqotbA6trRJhWoS2Phga5QLe5QNLTkwXl/Z1h0zl98GDkhY1hzhtBOa8byFYTYqss GeNWywfqadFLBWnJrFdg4z07z6ZDcu06RCQZyWkhrcjsm/a3y//gNMRpl8USdDNkuQ3j GKqNxIpAcZ0BcajHYtkh0rSEPFqBgsX9kTNleINHs57Roba187bToLS1Q6yIrBGxiEBa V7TIn4g0bZHoQVzJA/7ZYK4Isihn+/QsaBlAp3jXWYigmxrflmGX/kvf/O+Ee3Jy0JMG 6Suw== X-Received: by 10.52.69.74 with SMTP id c10mr2661853vdu.3.1360933087342; Fri, 15 Feb 2013 04:58:07 -0800 (PST) X-Forwarded-To: linaro-patchwork@canonical.com X-Forwarded-For: patch@linaro.org linaro-patchwork@canonical.com Delivered-To: patches@linaro.org Received: by 10.221.4.5 with SMTP id oa5csp11413vcb; Fri, 15 Feb 2013 04:58:06 -0800 (PST) X-Received: by 10.194.161.135 with SMTP id xs7mr3986508wjb.41.1360933086294; Fri, 15 Feb 2013 04:58:06 -0800 (PST) Received: from mail-wg0-f52.google.com (mail-wg0-f52.google.com [74.125.82.52]) by mx.google.com with ESMTPS id g7si19387006wjs.258.2013.02.15.04.58.05 (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Fri, 15 Feb 2013 04:58:06 -0800 (PST) Received-SPF: neutral (google.com: 74.125.82.52 is neither permitted nor denied by best guess record for domain of lee.jones@linaro.org) client-ip=74.125.82.52; Authentication-Results: mx.google.com; spf=neutral (google.com: 74.125.82.52 is neither permitted nor denied by best guess record for domain of lee.jones@linaro.org) smtp.mail=lee.jones@linaro.org Received: by mail-wg0-f52.google.com with SMTP id 12so2693660wgh.31 for ; Fri, 15 Feb 2013 04:58:05 -0800 (PST) X-Received: by 10.180.79.201 with SMTP id l9mr3891823wix.20.1360933085821; Fri, 15 Feb 2013 04:58:05 -0800 (PST) Received: from localhost.localdomain (cpc34-aztw25-2-0-cust250.18-1.cable.virginmedia.com. [86.16.136.251]) by mx.google.com with ESMTPS id j4sm2852410wiz.10.2013.02.15.04.58.03 (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Fri, 15 Feb 2013 04:58:04 -0800 (PST) From: Lee Jones To: linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, sameo@linux.intel.com Cc: arnd@arndb.de, linus.walleij@stericsson.com, Lee Jones , Alexandre Bourdiol Subject: [PATCH 23/35] mfd: ab8500-gpadc: Optimise GPADC driver Date: Fri, 15 Feb 2013 12:56:54 +0000 Message-Id: <1360933026-30325-24-git-send-email-lee.jones@linaro.org> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1360933026-30325-1-git-send-email-lee.jones@linaro.org> References: <1360933026-30325-1-git-send-email-lee.jones@linaro.org> X-Gm-Message-State: ALoCoQm1gN7Jn6mdBcglwsVCtk3PjHdFTPApyT87UjpYIW3a6aEnxiYxAVsMh19LBl7xRYF/KX4Q Optimise GPADC driver: * for code readability and maintenance by grouping similar cheking * for performance by grouping several writing to control register Signed-off-by: Alexandre Bourdiol Signed-off-by: Lee Jones Reviewed-by: Philippe LANGLAIS --- drivers/mfd/ab8500-gpadc.c | 212 ++++++++++++-------------------------------- 1 file changed, 55 insertions(+), 157 deletions(-) diff --git a/drivers/mfd/ab8500-gpadc.c b/drivers/mfd/ab8500-gpadc.c index e6fc511..4e668f0 100644 --- a/drivers/mfd/ab8500-gpadc.c +++ b/drivers/mfd/ab8500-gpadc.c @@ -363,7 +363,12 @@ int ab8500_gpadc_double_read_raw(struct ab8500_gpadc *gpadc, u8 channel, { int ret; int looplimit = 0; + unsigned long completion_timeout; u8 val, low_data, high_data, low_data2, high_data2; + u8 val_reg1 = 0; + unsigned int delay_min = 0; + unsigned int delay_max = 0; + u8 data_low_addr, data_high_addr; if (!gpadc) return -ENODEV; @@ -395,12 +400,7 @@ int ab8500_gpadc_double_read_raw(struct ab8500_gpadc *gpadc, u8 channel, } /* Enable GPADC */ - ret = abx500_mask_and_set_register_interruptible(gpadc->dev, - AB8500_GPADC, AB8500_GPADC_CTRL1_REG, EN_GPADC, EN_GPADC); - if (ret < 0) { - dev_err(gpadc->dev, "gpadc_conversion: enable gpadc failed\n"); - goto out; - } + val_reg1 |= EN_GPADC; /* Select the channel source and set average samples */ switch (avg_sample) { @@ -418,9 +418,13 @@ int ab8500_gpadc_double_read_raw(struct ab8500_gpadc *gpadc, u8 channel, break; } - if (conv_type == ADC_HW) + if (conv_type == ADC_HW) { ret = abx500_set_register_interruptible(gpadc->dev, AB8500_GPADC, AB8500_GPADC_CTRL3_REG, val); + val_reg1 |= EN_TRIG_EDGE; + if (trig_edge) + val_reg1 |= EN_FALLING; + } else ret = abx500_set_register_interruptible(gpadc->dev, AB8500_GPADC, AB8500_GPADC_CTRL2_REG, val); @@ -435,123 +439,42 @@ int ab8500_gpadc_double_read_raw(struct ab8500_gpadc *gpadc, u8 channel, * charging current sense if it needed, ABB 3.0 needs some special * treatment too. */ - if ((conv_type == ADC_HW) && (trig_edge)) { - ret = abx500_mask_and_set_register_interruptible(gpadc->dev, - AB8500_GPADC, AB8500_GPADC_CTRL1_REG, - EN_FALLING, EN_FALLING); - } - switch (channel) { case MAIN_CHARGER_C: case USB_CHARGER_C: - if (conv_type == ADC_HW) - ret = abx500_mask_and_set_register_interruptible( - gpadc->dev, - AB8500_GPADC, AB8500_GPADC_CTRL1_REG, - EN_BUF | EN_ICHAR | EN_TRIG_EDGE, - EN_BUF | EN_ICHAR | EN_TRIG_EDGE); - else - ret = abx500_mask_and_set_register_interruptible( - gpadc->dev, - AB8500_GPADC, AB8500_GPADC_CTRL1_REG, - EN_BUF | EN_ICHAR, - EN_BUF | EN_ICHAR); - break; - - case XTAL_TEMP: - if (conv_type == ADC_HW) - ret = abx500_mask_and_set_register_interruptible( - gpadc->dev, - AB8500_GPADC, AB8500_GPADC_CTRL1_REG, - EN_BUF | EN_TRIG_EDGE, - EN_BUF | EN_TRIG_EDGE); - else - ret = abx500_mask_and_set_register_interruptible( - gpadc->dev, - AB8500_GPADC, AB8500_GPADC_CTRL1_REG, - EN_BUF , - EN_BUF); - break; - - case VBAT_TRUE_MEAS: - if (conv_type == ADC_HW) - ret = abx500_mask_and_set_register_interruptible( - gpadc->dev, - AB8500_GPADC, AB8500_GPADC_CTRL1_REG, - EN_BUF | EN_TRIG_EDGE, - EN_BUF | EN_TRIG_EDGE); - else - ret = abx500_mask_and_set_register_interruptible( - gpadc->dev, - AB8500_GPADC, AB8500_GPADC_CTRL1_REG, - EN_BUF , - EN_BUF); - break; - - case BAT_CTRL_AND_IBAT: - case VBAT_MEAS_AND_IBAT: - case VBAT_TRUE_MEAS_AND_IBAT: - case BAT_TEMP_AND_IBAT: - if (conv_type == ADC_HW) - ret = abx500_mask_and_set_register_interruptible( - gpadc->dev, - AB8500_GPADC, AB8500_GPADC_CTRL1_REG, - EN_TRIG_EDGE, - EN_TRIG_EDGE); - else - ret = abx500_mask_and_set_register_interruptible( - gpadc->dev, - AB8500_GPADC, AB8500_GPADC_CTRL1_REG, - EN_BUF, - 0); + val_reg1 |= EN_BUF | EN_ICHAR; break; - case BTEMP_BALL: if (!is_ab8500_2p0_or_earlier(gpadc->parent)) { - if (conv_type == ADC_HW) - /* Turn on btemp pull-up on ABB 3.0 */ - ret = abx500_mask_and_set_register_interruptible - (gpadc->dev, - AB8500_GPADC, AB8500_GPADC_CTRL1_REG, - EN_BUF | BTEMP_PULL_UP | EN_TRIG_EDGE, - EN_BUF | BTEMP_PULL_UP | EN_TRIG_EDGE); - else - ret = abx500_mask_and_set_register_interruptible - (gpadc->dev, - AB8500_GPADC, AB8500_GPADC_CTRL1_REG, - EN_BUF | BTEMP_PULL_UP, - EN_BUF | BTEMP_PULL_UP); - - /* - * Delay might be needed for ABB8500 cut 3.0, if not, remove - * when hardware will be available - */ - usleep_range(1000, 1000); + val_reg1 |= EN_BUF | BTEMP_PULL_UP; + /* + * Delay might be needed for ABB8500 cut 3.0, if not, + * remove when hardware will be availible + */ + delay_min = 1000; /* Delay in micro seconds */ + delay_max = 10000; /* large range to optimise sleep mode */ break; } /* Intentional fallthrough */ default: - if (conv_type == ADC_HW) - ret = abx500_mask_and_set_register_interruptible( - gpadc->dev, - AB8500_GPADC, AB8500_GPADC_CTRL1_REG, - EN_BUF | EN_TRIG_EDGE, - EN_BUF | EN_TRIG_EDGE); - else - ret = abx500_mask_and_set_register_interruptible( - gpadc->dev, - AB8500_GPADC, - AB8500_GPADC_CTRL1_REG, EN_BUF, EN_BUF); + val_reg1 |= EN_BUF; break; } + + /* Write configuration to register */ + ret = abx500_set_register_interruptible(gpadc->dev, + AB8500_GPADC, AB8500_GPADC_CTRL1_REG, val_reg1); if (ret < 0) { dev_err(gpadc->dev, - "gpadc_conversion: select falling edge failed\n"); + "gpadc_conversion: set Control register failed\n"); goto out; } - /* Set trigger delay timer */ + if (delay_min != 0) + usleep_range(delay_min, delay_max); + if (conv_type == ADC_HW) { + /* Set trigger delay timer */ ret = abx500_set_register_interruptible(gpadc->dev, AB8500_GPADC, AB8500_GPADC_AUTO_TIMER_REG, trig_timer); if (ret < 0) { @@ -559,10 +482,11 @@ int ab8500_gpadc_double_read_raw(struct ab8500_gpadc *gpadc, u8 channel, "gpadc_conversion: trig timer failed\n"); goto out; } - } - - /* Start SW conversion */ - if (conv_type == ADC_SW) { + completion_timeout = 2 * HZ; + data_low_addr = AB8500_GPADC_AUTODATAL_REG; + data_high_addr = AB8500_GPADC_AUTODATAH_REG; + } else { + /* Start SW conversion */ ret = abx500_mask_and_set_register_interruptible(gpadc->dev, AB8500_GPADC, AB8500_GPADC_CTRL1_REG, ADC_SW_CONV, ADC_SW_CONV); @@ -571,61 +495,35 @@ int ab8500_gpadc_double_read_raw(struct ab8500_gpadc *gpadc, u8 channel, "gpadc_conversion: start s/w conv failed\n"); goto out; } + completion_timeout = msecs_to_jiffies(CONVERSION_TIME); + data_low_addr = AB8500_GPADC_MANDATAL_REG; + data_high_addr = AB8500_GPADC_MANDATAH_REG; } /* wait for completion of conversion */ - if (conv_type == ADC_HW) { - if (!wait_for_completion_timeout(&gpadc->ab8500_gpadc_complete, - 2 * HZ)) { - dev_err(gpadc->dev, - "timeout didn't receive hw GPADC conv interrupt\n"); - ret = -EINVAL; - goto out; - } - } else { - if (!wait_for_completion_timeout(&gpadc->ab8500_gpadc_complete, - msecs_to_jiffies(CONVERSION_TIME))) { - dev_err(gpadc->dev, - "timeout didn't receive sw GPADC conv interrupt\n"); - ret = -EINVAL; - goto out; - } + if (!wait_for_completion_timeout(&gpadc->ab8500_gpadc_complete, + completion_timeout)) { + dev_err(gpadc->dev, + "timeout didn't receive GPADC conv interrupt\n"); + ret = -EINVAL; + goto out; } /* Read the converted RAW data */ - if (conv_type == ADC_HW) { - ret = abx500_get_register_interruptible(gpadc->dev, - AB8500_GPADC, AB8500_GPADC_AUTODATAL_REG, &low_data); - if (ret < 0) { - dev_err(gpadc->dev, - "gpadc_conversion: read hw low data failed\n"); - goto out; - } - - ret = abx500_get_register_interruptible(gpadc->dev, - AB8500_GPADC, AB8500_GPADC_AUTODATAH_REG, &high_data); - if (ret < 0) { - dev_err(gpadc->dev, - "gpadc_conversion: read hw high data failed\n"); - goto out; - } - } else { - ret = abx500_get_register_interruptible(gpadc->dev, - AB8500_GPADC, AB8500_GPADC_MANDATAL_REG, &low_data); - if (ret < 0) { - dev_err(gpadc->dev, - "gpadc_conversion: read sw low data failed\n"); - goto out; - } + ret = abx500_get_register_interruptible(gpadc->dev, + AB8500_GPADC, data_low_addr, &low_data); + if (ret < 0) { + dev_err(gpadc->dev, "gpadc_conversion: read low data failed\n"); + goto out; + } - ret = abx500_get_register_interruptible(gpadc->dev, - AB8500_GPADC, AB8500_GPADC_MANDATAH_REG, &high_data); - if (ret < 0) { - dev_err(gpadc->dev, - "gpadc_conversion: read sw high data failed\n"); - goto out; - } + ret = abx500_get_register_interruptible(gpadc->dev, + AB8500_GPADC, data_high_addr, &high_data); + if (ret < 0) { + dev_err(gpadc->dev, "gpadc_conversion: read high data failed\n"); + goto out; } + /* Check if double convertion is required */ if ((channel == BAT_CTRL_AND_IBAT) || (channel == VBAT_MEAS_AND_IBAT) ||