From patchwork Wed Nov 22 07:05:58 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Heiner Kallweit X-Patchwork-Id: 840326 X-Patchwork-Delegate: bartekgola@gmail.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=linux-i2c-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="kk/+ujYu"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 3yhYQk0rJZz9s1h for ; Wed, 22 Nov 2017 18:06:22 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751293AbdKVHGV (ORCPT ); Wed, 22 Nov 2017 02:06:21 -0500 Received: from mail-wm0-f68.google.com ([74.125.82.68]:46772 "EHLO mail-wm0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751216AbdKVHGU (ORCPT ); Wed, 22 Nov 2017 02:06:20 -0500 Received: by mail-wm0-f68.google.com with SMTP id u83so8054937wmb.5 for ; Tue, 21 Nov 2017 23:06:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:subject:to:cc:references:message-id:date:user-agent :mime-version:in-reply-to:content-transfer-encoding; bh=5o82NuNWhZpzHnzcBgVkm1CNquA1mxq1simKWkBWKQ8=; b=kk/+ujYuh+79H8R84qYqIQdmFCxMTl2+MzFHg4JjxD/ruBKEelpgW4exN/CDnVw14m yB5dzNYOOZbPSGiA9asMEIsHDH38Q/nQO9xB8eMrDt04Bm4sza8hti4qto3bImBAK1cg 5JLdLzVuQ5YNcg04cWk3q77YJbauQ8TTOpM4KcyeOGV1y80sH/SuT/sFUyivWSzVAu3f 4zfsFa2E+heyjzSzoAAMyz+KqyEdPhORgZmL7W41dqRl9LGEVjeCXbqCo7XYl2EvQKfA xxtWYGSlEFVMkSLkzGwNW+9/qJW8TGrW3loUBwrahR+Y84ZdEGjFs0Rw62md0wsHyJXW UcCQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:subject:to:cc:references:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding; bh=5o82NuNWhZpzHnzcBgVkm1CNquA1mxq1simKWkBWKQ8=; b=XHyjDCZP799arTYpv0RiaavToygX8A4LDZzqjAn42LY2r+hzAmVSlzPPPk/QHuT/BR uB90dxzimw7Xm1JbJcL0HQEH57cH1qHYeCndiMlFmtM6ErN/M+VyEWGnUYBmfikjYgRz Y4j2hSx/GuB+/uvRY+861rcIhgu9pN82MlpvQWre2NYspDbFz1AmDEccjerPGyJudIhf /HWe4KSfl69+1P95jtH0q+wjYntkCGhSAnZLX7n7rl08yYdWRm4D/v/Z1zL+6hpREUvA ujx0KzKSQJwxpCX+55EC297I1UgmYd9KKG86SX6ODtTq3gNHs1EfuzV8tZvh7tsDFpi4 auvw== X-Gm-Message-State: AJaThX78dsrHI3QSN+JoQh9wekIBHJ97wD8xbF4aGODdDA83JRL6wYRZ QRBKaJvvid2AYZzO1tFSnr/yFLCC6i4= X-Google-Smtp-Source: AGs4zMZrJPmF3EUqpk4JcbbZOCYeyV1UgQElfXTfxpB/pOiFpxTEJmrIm9kShzsgO9Rd9j5VYR1IvA== X-Received: by 10.80.145.81 with SMTP id f17mr28635719eda.215.1511334379378; Tue, 21 Nov 2017 23:06:19 -0800 (PST) Received: from [192.168.178.85] (pD9F90FDA.dip0.t-ipconnect.de. [217.249.15.218]) by smtp.googlemail.com with ESMTPSA id d9sm11705211edb.18.2017.11.21.23.06.18 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 21 Nov 2017 23:06:19 -0800 (PST) From: Heiner Kallweit Subject: [PATCH v3 5/7] eeprom: at24: add regmap-based read function To: Bartosz Golaszewski Cc: "linux-i2c@vger.kernel.org" References: Message-ID: Date: Wed, 22 Nov 2017 08:05:58 +0100 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.4.0 MIME-Version: 1.0 In-Reply-To: Sender: linux-i2c-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-i2c@vger.kernel.org Add regmap-based read function and instead of using three different read functions (standard, mac, serial) use just one and factor out the read offset adjustment for mac and serial to at24_adjust_read_offset. Signed-off-by: Heiner Kallweit --- v2: - rebased v3: - improve readability - re-introduce debug message - introduce at24_adjust_read_offset --- drivers/misc/eeprom/at24.c | 53 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/drivers/misc/eeprom/at24.c b/drivers/misc/eeprom/at24.c index 493e2b646..589e6d855 100644 --- a/drivers/misc/eeprom/at24.c +++ b/drivers/misc/eeprom/at24.c @@ -312,6 +312,57 @@ static ssize_t at24_eeprom_read_smbus(struct at24_data *at24, char *buf, return -ETIMEDOUT; } +static void at24_adjust_read_offset(struct at24_data *at24, + unsigned int *offset) +{ + u8 flags = at24->chip.flags; + + if (flags & AT24_FLAG_MAC) + *offset += 0x90; + else if (flags & AT24_FLAG_SERIAL && flags & AT24_FLAG_ADDR16) + /* + * For 16 bit address pointers, the word address must contain + * a '10' sequence in bits 11 and 10 regardless of the + * intended position of the address pointer. + */ + *offset |= BIT(11); + else if (flags & AT24_FLAG_SERIAL) + /* + * Otherwise the word address must begin with a '10' sequence, + * regardless of the intended address. + */ + *offset |= BIT(7); +} + +static ssize_t at24_regmap_read(struct at24_data *at24, char *buf, + unsigned int offset, size_t count) +{ + unsigned long timeout, read_time; + struct at24_client *at24_client; + struct i2c_client *client; + struct regmap *regmap; + int ret; + + at24_client = at24_translate_offset(at24, &offset); + regmap = at24_client->regmap; + client = at24_client->client; + + if (count > io_limit) + count = io_limit; + + at24_adjust_read_offset(at24, &offset); + + loop_until_timeout(timeout, read_time) { + ret = regmap_bulk_read(regmap, offset, buf, count); + dev_dbg(&client->dev, "read %zu@%d --> %d (%ld)\n", + count, offset, ret, jiffies); + if (!ret) + return count; + } + + return -ETIMEDOUT; +} + static ssize_t at24_eeprom_read_i2c(struct at24_data *at24, char *buf, unsigned int offset, size_t count) { @@ -531,7 +582,7 @@ static int at24_read(void *priv, unsigned int off, void *val, size_t count) while (count) { int status; - status = at24->read_func(at24, buf, off, count); + status = at24_regmap_read(at24, buf, off, count); if (status < 0) { mutex_unlock(&at24->lock); pm_runtime_put(&client->dev);