From patchwork Mon Nov 27 20:34:34 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Heiner Kallweit X-Patchwork-Id: 841867 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="mn1d5vnf"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 3ylz7H57tZz9s1h for ; Tue, 28 Nov 2017 07:34:51 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752813AbdK0Uev (ORCPT ); Mon, 27 Nov 2017 15:34:51 -0500 Received: from mail-wr0-f196.google.com ([209.85.128.196]:33157 "EHLO mail-wr0-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752706AbdK0Ueu (ORCPT ); Mon, 27 Nov 2017 15:34:50 -0500 Received: by mail-wr0-f196.google.com with SMTP id 55so11426403wrx.0 for ; Mon, 27 Nov 2017 12:34:49 -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=dFxtUQeMevCq23wbXnDSKJk1l/vjKJwx0+6y2Ex1TCo=; b=mn1d5vnfUuq7gOxZd4VV8Tk4tiW9weYGh+cJ/WiRGCnvuKvl7M2VlOSpTqCt4OUbT6 BoGCOfqNMftCNF51D9UyNDsoKqOgjhiaCFc8i+yCAM+ODDKiitVn3EcrbFRZdpwmiMg2 D2anNxWxn+xqGVxwmfAVMcl5pV9T2luM/VlZxypiRVneR4pYIxTTTWOl/SnUzq6dnUE4 KDG5SbsjuLOOPLZYcLCrxdf0MJ7wMqsSXOeX/N7cc2CilrvSE4N98Fak/i/OWDH5yw1/ gtwcsLGpLBZA1XzBuBptixgljdPWstFSbKRaOu279yCOUdfgyUs6Wk2ZCr+At67u7iwk K7dA== 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=dFxtUQeMevCq23wbXnDSKJk1l/vjKJwx0+6y2Ex1TCo=; b=JhFx36c0dO1t+XnYmdS1cefMgvPCwOwfVYjEq/FfZHH5zFqD3u8rNRGl+bnUtNBohL 0puTX0TRVH2+4aTrX9YklQGlk81q/vIsEw5CmX1G99OQg9pdEuIIKaLDtThfnJMD4ZHR 5jpEbmon0+yW6aKHnTrBqtD/i7RT8HI9G0kwhhR6p6hciQAEX41SvaKnV1yPgSJq5lg/ y4pfT/ZpggLsSfh+bSUtE45yAaz/AtaHWuwKRdnkc4aApSKbBOhOD0es6cN6/O0sUT0Y NuHU5RaUBPxYgiR8mxADg9uVerK/E/4s/hSLISNQTKLLjsqUW/6TZ+VdpapDvCt14pLk 4iHA== X-Gm-Message-State: AJaThX5OQvTJ0m7sUYtfAxEFXN/Gd4ViFctYHc1a22NrlH25n1Y46o2P ZUWosBl3CEvEhmSMsH5FXxOUbQ== X-Google-Smtp-Source: AGs4zMYIFo+0URM0r0ANFDs72SGg2YcvSU15GiBJNmDtBZ8fasYF8JqSzxSlRcnqKiUHCetkCeW5RQ== X-Received: by 10.223.154.244 with SMTP id a107mr32288408wrc.8.1511814888747; Mon, 27 Nov 2017 12:34:48 -0800 (PST) Received: from ?IPv6:2003:ea:8bc0:e400:cdd0:4154:8262:d8bc? (p200300EA8BC0E400CDD041548262D8BC.dip0.t-ipconnect.de. [2003:ea:8bc0:e400:cdd0:4154:8262:d8bc]) by smtp.googlemail.com with ESMTPSA id y89sm10317296wmh.0.2017.11.27.12.34.47 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 27 Nov 2017 12:34:48 -0800 (PST) From: Heiner Kallweit Subject: [PATCH v5 5/7] eeprom: at24: add regmap-based read function To: Bartosz Golaszewski Cc: "linux-i2c@vger.kernel.org" References: <2c379f45-d5e3-71af-eb20-ca51e09d9257@gmail.com> Message-ID: <1b91f67d-2a5f-9120-09fa-d27439862ab2@gmail.com> Date: Mon, 27 Nov 2017 21:34:34 +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: <2c379f45-d5e3-71af-eb20-ca51e09d9257@gmail.com> 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 v4: - move offset adjustment calculation to probe function v5: - small style improvements to at24_get_offset_adj --- drivers/misc/eeprom/at24.c | 56 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) diff --git a/drivers/misc/eeprom/at24.c b/drivers/misc/eeprom/at24.c index 3df2301a9..87ab2746d 100644 --- a/drivers/misc/eeprom/at24.c +++ b/drivers/misc/eeprom/at24.c @@ -75,6 +75,7 @@ struct at24_data { unsigned write_max; unsigned num_addresses; + unsigned int offset_adj; struct nvmem_config nvmem_config; struct nvmem_device *nvmem; @@ -312,6 +313,36 @@ static ssize_t at24_eeprom_read_smbus(struct at24_data *at24, char *buf, return -ETIMEDOUT; } +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; + + /* adjust offset for mac and serial read ops */ + offset += at24->offset_adj; + + 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) { @@ -534,7 +565,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); @@ -627,6 +658,28 @@ static void at24_get_pdata(struct device *dev, struct at24_platform_data *chip) } } +static unsigned int at24_get_offset_adj(u8 flags) +{ + if (flags & AT24_FLAG_MAC) { + return 0x0090; + } 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. + */ + return 0x0800; + } else if (flags & AT24_FLAG_SERIAL) { + /* + * Otherwise the word address must begin with a '10' sequence, + * regardless of the intended address. + */ + return 0x0080; + } else { + return 0; + } +} + static const struct regmap_config regmap_config_8 = { .reg_bits = 8, .val_bits = 8, @@ -743,6 +796,7 @@ static int at24_probe(struct i2c_client *client, const struct i2c_device_id *id) at24->use_smbus = use_smbus; at24->chip = chip; at24->num_addresses = num_addresses; + at24->offset_adj = at24_get_offset_adj(chip.flags); at24->client[0].client = client; at24->client[0].regmap = devm_regmap_init_i2c(client, config);