From patchwork Tue Sep 9 14:54:27 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wolfram Sang X-Patchwork-Id: 4870991 Return-Path: X-Original-To: patchwork-linux-sh@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork2.web.kernel.org (Postfix) with ESMTP id CE6AAC0338 for ; Tue, 9 Sep 2014 14:58:18 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 6ADD82017E for ; Tue, 9 Sep 2014 14:58:17 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 1AB4C2017A for ; Tue, 9 Sep 2014 14:58:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932579AbaIIOya (ORCPT ); Tue, 9 Sep 2014 10:54:30 -0400 Received: from sauhun.de ([89.238.76.85]:34778 "EHLO pokefinder.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1757330AbaIIOyZ (ORCPT ); Tue, 9 Sep 2014 10:54:25 -0400 Received: from p4fe24daa.dip0.t-ipconnect.de ([79.226.77.170]:40150 helo=localhost) by pokefinder.org with esmtpsa (TLS1.2:RSA_AES_128_CBC_SHA1:128) (Exim 4.80) (envelope-from ) id 1XRMoH-0000oz-Vu; Tue, 09 Sep 2014 16:54:22 +0200 From: Wolfram Sang To: linux-i2c@vger.kernel.org Cc: linux-sh@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, Jean Delvare , Magnus Damm Subject: [RFC 1/4] i2c: core changes for slave support Date: Tue, 9 Sep 2014 16:54:27 +0200 Message-Id: <1410274470-12712-2-git-send-email-wsa@the-dreams.de> X-Mailer: git-send-email 2.0.0 In-Reply-To: <1410274470-12712-1-git-send-email-wsa@the-dreams.de> References: <1410274470-12712-1-git-send-email-wsa@the-dreams.de> Sender: linux-sh-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-sh@vger.kernel.org X-Spam-Status: No, score=-9.4 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Wolfram Sang Signed-off-by: Wolfram Sang --- drivers/i2c/i2c-core.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ include/linux/i2c.h | 22 ++++++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/drivers/i2c/i2c-core.c b/drivers/i2c/i2c-core.c index 632057a44615..6713d438539b 100644 --- a/drivers/i2c/i2c-core.c +++ b/drivers/i2c/i2c-core.c @@ -27,6 +27,7 @@ OF support is copyright (c) 2008 Jochen Friedrich (based on a previous patch from Jon Smirl ) and (c) 2013 Wolfram Sang + I2C slave support (c) 2014 by Wolfram Sang */ #include @@ -2536,6 +2537,49 @@ trace: } EXPORT_SYMBOL(i2c_smbus_xfer); +int i2c_slave_register(struct i2c_client *client, i2c_slave_cb_t slave_cb) +{ + int ret; + + if (!client || !slave_cb) + return -EINVAL; + + if (!(client->flags & I2C_CLIENT_TEN)) { + /* Enforce stricter address checking */ + ret = i2c_check_addr_validity(client->addr); + if (ret) + return ret; + } + + if (!client->adapter->algo->reg_slave) + return -EOPNOTSUPP; + + client->slave_cb = slave_cb; + + ret = client->adapter->algo->reg_slave(client); + if (ret) + client->slave_cb = NULL; + + return ret; +} +EXPORT_SYMBOL_GPL(i2c_slave_register); + +int i2c_slave_unregister(struct i2c_client *client) +{ + int ret; + + if (!client->adapter->algo->unreg_slave) + return -EOPNOTSUPP; + + ret = client->adapter->algo->unreg_slave(client); + + if (ret == 0) + client->slave_cb = NULL; + + return ret; +} +EXPORT_SYMBOL_GPL(i2c_slave_unregister); + MODULE_AUTHOR("Simon G. Vogl "); MODULE_DESCRIPTION("I2C-Bus main module"); MODULE_LICENSE("GPL"); diff --git a/include/linux/i2c.h b/include/linux/i2c.h index a95efeb53a8b..11d4edb1ab3e 100644 --- a/include/linux/i2c.h +++ b/include/linux/i2c.h @@ -121,6 +121,24 @@ extern s32 i2c_smbus_write_i2c_block_data(const struct i2c_client *client, const u8 *values); #endif /* I2C */ +/* I2C slave support */ + +enum i2c_slave_event { + I2C_SLAVE_REQ_READ_START, + I2C_SLAVE_REQ_READ_END, + I2C_SLAVE_REQ_WRITE_START, + I2C_SLAVE_REQ_WRITE_END, + I2C_SLAVE_STOP, +}; + +typedef int (*i2c_slave_cb_t)(struct i2c_client *, enum i2c_slave_event, u8 *); + +extern int i2c_slave_register(struct i2c_client *client, i2c_slave_cb_t slave_cb); +extern int i2c_slave_unregister(struct i2c_client *client); + +#define i2c_slave_event(__client, __event, __value) \ + __client->slave_cb(__client, __event, __value) + /** * struct i2c_driver - represent an I2C device driver * @class: What kind of i2c device we instantiate (for detect) @@ -224,6 +242,7 @@ struct i2c_client { struct device dev; /* the device structure */ int irq; /* irq issued by device */ struct list_head detected; + i2c_slave_cb_t slave_cb; /* callback for slave mode */ }; #define to_i2c_client(d) container_of(d, struct i2c_client, dev) @@ -377,6 +396,9 @@ struct i2c_algorithm { /* To determine what the adapter supports */ u32 (*functionality) (struct i2c_adapter *); + + int (*reg_slave)(struct i2c_client *client); + int (*unreg_slave)(struct i2c_client *client); }; /**