From patchwork Sun May 29 21:53:43 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 68800 Delivered-To: patch@linaro.org Received: by 10.140.92.199 with SMTP id b65csp1113254qge; Sun, 29 May 2016 14:54:55 -0700 (PDT) X-Received: by 10.98.100.132 with SMTP id y126mr41152598pfb.101.1464558895695; Sun, 29 May 2016 14:54:55 -0700 (PDT) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id ss2si45772571pab.111.2016.05.29.14.54.55; Sun, 29 May 2016 14:54:55 -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; dkim=pass header.i=@baylibre-com.20150623.gappssmtp.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 S932682AbcE2Vy2 (ORCPT + 30 others); Sun, 29 May 2016 17:54:28 -0400 Received: from mail-wm0-f54.google.com ([74.125.82.54]:37316 "EHLO mail-wm0-f54.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753304AbcE2VyS (ORCPT ); Sun, 29 May 2016 17:54:18 -0400 Received: by mail-wm0-f54.google.com with SMTP id z87so50831866wmh.0 for ; Sun, 29 May 2016 14:54:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=how0NunHPfbVGq7wxPqC+ZPtxUTmq4BHXPDGpPCwkII=; b=AHYILCRTsU/5RVJSNniygR4qacAjvPR/yfq6FP2aTYgXx20aceT9u/R7DJ909sNhJt /TQ3BImI6IOTfw0fYrjMqvKAPTuerIVQhdFVGUJ++OTNi0n1sPaPFi3g3mJxq9SnCCdL kOciHjrdrucD2WmJ2HpY9D/TFVrseqr1/62rO5cBjvOAd1EkII2Kja5qNaz8hGjaEWL/ ZcZD07viYqGOZ6JatDJoQVHzzS5k7eZSneoCqK6O0nNxGdvP0UMLeMunzYuFYzqrvoeY z1xk/yHld7/KDBZP+MErFUjjbEga+zPQHbKO2ophZowzlKWzkZSj+Cjg885IykagGfcX uqOQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=how0NunHPfbVGq7wxPqC+ZPtxUTmq4BHXPDGpPCwkII=; b=L1YpjGNeQGUqO8wVTisxvhe/l16f++jttn8/tAqPTOq0rbNcQDdkQT0XZHb3m4fjkT Q2POpL+9qILy4adLSnelU4awHbeW87yVUKguQHkUti4gX1ORpl7dwlg0c7J5Klm1F38d MGO5fOTRDPQ0sMRwFRf4CgIe/qCVCwyAAEx10CCmLhwFFgYdbhrSlUCEQ+ocAGzPkMMI 5wfLxBi8E49p3y/GaboPTmbt1/Lx1uSZSOGq4doBhGyYIzVkA7arn2XScbbkFtUxejqx AqB1T6iEI7OB7oum6k7gWR1zEomaAVrdwNQjkOy/q8dz777nhpcKxQbiRGLJvRl5A7wV kY8w== X-Gm-Message-State: ALyK8tJQUqW4IA7rrn4GQ1kQ2DC0KkRAryAXB4SFT9CYa9sio6Goh0OdwMjiG96mma54HkvN X-Received: by 10.194.186.200 with SMTP id fm8mr23884788wjc.174.1464558857071; Sun, 29 May 2016 14:54:17 -0700 (PDT) Received: from bgdev-debian.lan (jua06-1-82-242-157-225.fbx.proxad.net. [82.242.157.225]) by smtp.gmail.com with ESMTPSA id a128sm20490367wme.16.2016.05.29.14.54.15 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sun, 29 May 2016 14:54:16 -0700 (PDT) From: Bartosz Golaszewski To: Wolfram Sang , linux-i2c , LKML , Andrew Lunn , Srinivas Kandagatla , Maxime Ripard , GregKH Cc: Bartosz Golaszewski Subject: [PATCH 05/14] eeprom: at24: hide the read/write loop behind a macro Date: Sun, 29 May 2016 23:53:43 +0200 Message-Id: <1464558832-2262-6-git-send-email-bgolaszewski@baylibre.com> X-Mailer: git-send-email 2.1.4 In-Reply-To: <1464558832-2262-1-git-send-email-bgolaszewski@baylibre.com> References: <1464558832-2262-1-git-send-email-bgolaszewski@baylibre.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Before splitting the read/write routines into smaller, more specialized functions, unduplicate some code in advance. Use a 'for' loop instead of 'do while' when waiting for the previous write to complete and hide it behind a macro. Signed-off-by: Bartosz Golaszewski --- drivers/misc/eeprom/at24.c | 42 ++++++++++++++++++++---------------------- 1 file changed, 20 insertions(+), 22 deletions(-) -- 2.7.4 diff --git a/drivers/misc/eeprom/at24.c b/drivers/misc/eeprom/at24.c index 0621937..2efb572 100644 --- a/drivers/misc/eeprom/at24.c +++ b/drivers/misc/eeprom/at24.c @@ -113,6 +113,22 @@ MODULE_PARM_DESC(write_timeout, "Time (in ms) to try writes (default 25)"); ((1 << AT24_SIZE_FLAGS | (_flags)) \ << AT24_SIZE_BYTELEN | ilog2(_len)) +/* + * Both reads and writes fail if the previous write didn't complete yet. This + * macro loops a few times waiting at least long enough for one entire page + * write to work. + * + * It takes two parameters: a variable in which the future timeout in jiffies + * will be stored and a temporary variable holding the time of the last + * iteration of processing the request. Both should be unsigned integers + * holding at least 32 bits. + */ +#define loop_until_timeout(tout, op_time) \ + for (tout = jiffies + msecs_to_jiffies(write_timeout), \ + op_time = jiffies; \ + time_before(op_time, tout); \ + usleep_range(1000, 1500), op_time = jiffies) + static const struct i2c_device_id at24_ids[] = { /* needs 8 addresses as A0-A2 are ignored */ { "24c00", AT24_DEVICE_MAGIC(128 / 8, AT24_FLAG_TAKE8ADDR) }, @@ -225,14 +241,7 @@ static ssize_t at24_eeprom_read(struct at24_data *at24, char *buf, msg[1].len = count; } - /* - * Reads fail if the previous write didn't complete yet. We may - * loop a few times until this one succeeds, waiting at least - * long enough for one entire page write to work. - */ - timeout = jiffies + msecs_to_jiffies(write_timeout); - do { - read_time = jiffies; + loop_until_timeout(timeout, read_time) { if (at24->use_smbus) { status = i2c_smbus_read_i2c_block_data_or_emulated(client, offset, count, buf); @@ -246,9 +255,7 @@ static ssize_t at24_eeprom_read(struct at24_data *at24, char *buf, if (status == count) return count; - - usleep_range(1000, 1500); - } while (time_before(read_time, timeout)); + } return -ETIMEDOUT; } @@ -299,14 +306,7 @@ static ssize_t at24_eeprom_write(struct at24_data *at24, const char *buf, msg.len = i + count; } - /* - * Writes fail if the previous one didn't complete yet. We may - * loop a few times until this one succeeds, waiting at least - * long enough for one entire page write to work. - */ - timeout = jiffies + msecs_to_jiffies(write_timeout); - do { - write_time = jiffies; + loop_until_timeout(timeout, write_time) { if (at24->use_smbus_write) { switch (at24->use_smbus_write) { case I2C_SMBUS_I2C_BLOCK_DATA: @@ -331,9 +331,7 @@ static ssize_t at24_eeprom_write(struct at24_data *at24, const char *buf, if (status == count) return count; - - usleep_range(1000, 1500); - } while (time_before(write_time, timeout)); + } return -ETIMEDOUT; }