From patchwork Sun May 29 21:53:45 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 68809 Delivered-To: patch@linaro.org Received: by 10.140.92.199 with SMTP id b65csp1113750qge; Sun, 29 May 2016 14:57:18 -0700 (PDT) X-Received: by 10.98.5.5 with SMTP id 5mr41075458pff.58.1464559036389; Sun, 29 May 2016 14:57:16 -0700 (PDT) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id ab1si32310393pad.101.2016.05.29.14.57.16; Sun, 29 May 2016 14:57:16 -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 S1161039AbcE2V5M (ORCPT + 30 others); Sun, 29 May 2016 17:57:12 -0400 Received: from mail-wm0-f48.google.com ([74.125.82.48]:37325 "EHLO mail-wm0-f48.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753315AbcE2VyV (ORCPT ); Sun, 29 May 2016 17:54:21 -0400 Received: by mail-wm0-f48.google.com with SMTP id z87so50832464wmh.0 for ; Sun, 29 May 2016 14:54:20 -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=Lir2NdUJP7crNaOhb+gzMfjfqvBWBQcEsUDL/cQavSU=; b=v++nz9eZl0m95mm9zp71ZwrVMpqEhqI5b4j4RQASyR1ElDvp4Fc2GdZbVd2WjkvGAy RsdtmUFeIXsd7vRcMoYJBywv/Eohj6zpqhzUV/h/8gCHSJ/FmJr+Dvg+ZegM9U8j2mQi cknNxOYZJbNzYsorUg9b6Fy8U1My1BuOaIW+5+LtSgu81WF47FNwnVtaHwveo5msmA5x +PBKZ2/avKpayAR3Q4+zdYi5VOWdPqQp7FaC0rxkiE08TDFKptkZhqErz/Wqsd9sujUT DrSrEAXGdRXkxnVFZy7AA1mG4m+EQBB/FMjUjhPhZcYq5pGV+rQBAr2h8fERzlrBRktF GUmQ== 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=Lir2NdUJP7crNaOhb+gzMfjfqvBWBQcEsUDL/cQavSU=; b=NxRE/M+Tr0tD1It+uW3yYPRyEBaHtEr7V/vkXWfqOmTPWJusdDcEd1HP2EpGVAL0yy Iwvi5PYgBo0VZtvxiwkAgaNrErwlLADC74bHGP8gvRp+8ygL0B4JjGp9zNKMMEgiU6Ju xas4c8Io7r6iwGqu6xo0xgz6JcCr86/8c6L4tNYpBo9dR/ZDq1GW7KJoGB/mTiMlxpNE 9EgoAx752fJ+6PKOArH9UGSldnP/9yUK9Qf1I9aa/kUMjB+/uoyi8n24TP9X3Zg6pQUf FT2B1tXttJdwagMNJwTFNPpjToVEHNp7S65gyhUmljpBTkqeyiYV0fNhCH7J8bBuKtoP 1Z7Q== X-Gm-Message-State: ALyK8tK8nFs4ABjYLP1UYoWPiABRxpDEadMNYMJ4T5XUCRlN376wx19DaA+l0/r2XV8ALKJu X-Received: by 10.194.5.129 with SMTP id s1mr2147261wjs.96.1464558859602; Sun, 29 May 2016 14:54:19 -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.18 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sun, 29 May 2016 14:54:19 -0700 (PDT) From: Bartosz Golaszewski To: Wolfram Sang , linux-i2c , LKML , Andrew Lunn , Srinivas Kandagatla , Maxime Ripard , GregKH Cc: Bartosz Golaszewski Subject: [PATCH 07/14] eeprom: at24: split at24_eeprom_write() into specialized functions Date: Sun, 29 May 2016 23:53:45 +0200 Message-Id: <1464558832-2262-8-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 Split at24_eeprom_write() into three smaller functions - one for the i2c operations and two for the smbus extensions (separate routines for block and byte transfers). Assign them in at24_probe() depending on the bus capabilities. Also: in order to avoid duplications move code adjusting the count argument into a separate function and use it for i2c and smbus block writes (no need for a roll-over for byte writes as we're always writing one byte). Signed-off-by: Bartosz Golaszewski --- drivers/misc/eeprom/at24.c | 134 ++++++++++++++++++++++++++++++--------------- 1 file changed, 91 insertions(+), 43 deletions(-) -- 2.7.4 diff --git a/drivers/misc/eeprom/at24.c b/drivers/misc/eeprom/at24.c index e7db137..6acf35a 100644 --- a/drivers/misc/eeprom/at24.c +++ b/drivers/misc/eeprom/at24.c @@ -281,21 +281,15 @@ static ssize_t at24_eeprom_read_i2c(struct at24_data *at24, char *buf, * chip is normally write protected. But there are plenty of product * variants here, including OTP fuses and partial chip protect. * - * We only use page mode writes; the alternative is sloooow. This routine - * writes at most one page. + * We only use page mode writes; the alternative is sloooow. These routines + * write at most one page. */ -static ssize_t at24_eeprom_write(struct at24_data *at24, const char *buf, - unsigned int offset, size_t count) + +static size_t at24_adjust_write_count(struct at24_data *at24, + unsigned int offset, size_t count) { - struct i2c_client *client; - struct i2c_msg msg; - ssize_t status = 0; - unsigned long timeout, write_time; unsigned next_page; - /* Get corresponding I2C address and adjust offset */ - client = at24_translate_offset(at24, &offset); - /* write_max is at most a page */ if (count > at24->write_max) count = at24->write_max; @@ -305,43 +299,90 @@ static ssize_t at24_eeprom_write(struct at24_data *at24, const char *buf, if (offset + count > next_page) count = next_page - offset; - /* If we'll use I2C calls for I/O, set up the message */ - if (!at24->use_smbus) { - int i = 0; + return count; +} + +static ssize_t at24_eeprom_write_smbus_block(struct at24_data *at24, + const char *buf, + unsigned int offset, size_t count) +{ + unsigned long timeout, write_time; + struct i2c_client *client; + ssize_t status = 0; + + client = at24_translate_offset(at24, &offset); + count = at24_adjust_write_count(at24, offset, count); - msg.addr = client->addr; - msg.flags = 0; + loop_until_timeout(timeout, write_time) { + status = i2c_smbus_write_i2c_block_data(client, + offset, count, buf); + if (status == 0) + status = count; - /* msg.buf is u8 and casts will mask the values */ - msg.buf = at24->writebuf; - if (at24->chip.flags & AT24_FLAG_ADDR16) - msg.buf[i++] = offset >> 8; + dev_dbg(&client->dev, "write %zu@%d --> %zd (%ld)\n", + count, offset, status, jiffies); - msg.buf[i++] = offset; - memcpy(&msg.buf[i], buf, count); - msg.len = i + count; + if (status == count) + return count; } + return -ETIMEDOUT; +} + +static ssize_t at24_eeprom_write_smbus_byte(struct at24_data *at24, + const char *buf, + unsigned int offset, size_t count) +{ + unsigned long timeout, write_time; + struct i2c_client *client; + ssize_t status = 0; + + client = at24_translate_offset(at24, &offset); + loop_until_timeout(timeout, write_time) { - if (at24->use_smbus_write) { - switch (at24->use_smbus_write) { - case I2C_SMBUS_I2C_BLOCK_DATA: - status = i2c_smbus_write_i2c_block_data(client, - offset, count, buf); - break; - case I2C_SMBUS_BYTE_DATA: - status = i2c_smbus_write_byte_data(client, - offset, buf[0]); - break; - } - - if (status == 0) - status = count; - } else { - status = i2c_transfer(client->adapter, &msg, 1); - if (status == 1) - status = count; - } + status = i2c_smbus_write_byte_data(client, offset, buf[0]); + if (status == 0) + status = count; + + dev_dbg(&client->dev, "write %zu@%d --> %zd (%ld)\n", + count, offset, status, jiffies); + + if (status == count) + return count; + } + + return -ETIMEDOUT; +} + +static ssize_t at24_eeprom_write_i2c(struct at24_data *at24, const char *buf, + unsigned int offset, size_t count) +{ + unsigned long timeout, write_time; + struct i2c_client *client; + struct i2c_msg msg; + ssize_t status = 0; + int i = 0; + + client = at24_translate_offset(at24, &offset); + count = at24_adjust_write_count(at24, offset, count); + + msg.addr = client->addr; + msg.flags = 0; + + /* msg.buf is u8 and casts will mask the values */ + msg.buf = at24->writebuf; + if (at24->chip.flags & AT24_FLAG_ADDR16) + msg.buf[i++] = offset >> 8; + + msg.buf[i++] = offset; + memcpy(&msg.buf[i], buf, count); + msg.len = i + count; + + loop_until_timeout(timeout, write_time) { + status = i2c_transfer(client->adapter, &msg, 1); + if (status == 1) + status = count; + dev_dbg(&client->dev, "write %zu@%d --> %zd (%ld)\n", count, offset, status, jiffies); @@ -538,7 +579,14 @@ static int at24_probe(struct i2c_client *client, const struct i2c_device_id *id) at24->read_func = at24->use_smbus ? at24_eeprom_read_smbus : at24_eeprom_read_i2c; - at24->write_func = at24_eeprom_write; + if (at24->use_smbus) { + if (at24->use_smbus_write == I2C_SMBUS_I2C_BLOCK_DATA) + at24->write_func = at24_eeprom_write_smbus_block; + else + at24->write_func = at24_eeprom_write_smbus_byte; + } else { + at24->write_func = at24_eeprom_write_i2c; + } writable = !(chip.flags & AT24_FLAG_READONLY); if (writable) {