From patchwork Tue Jun 12 15:34:46 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Akinobu Mita X-Patchwork-Id: 10460591 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 13E8F602A0 for ; Tue, 12 Jun 2018 15:35:14 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id EF56B286D9 for ; Tue, 12 Jun 2018 15:35:13 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E20C32896D; Tue, 12 Jun 2018 15:35:13 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3E031286D9 for ; Tue, 12 Jun 2018 15:35:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754312AbeFLPfL (ORCPT ); Tue, 12 Jun 2018 11:35:11 -0400 Received: from mail-io0-f193.google.com ([209.85.223.193]:36366 "EHLO mail-io0-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754056AbeFLPfK (ORCPT ); Tue, 12 Jun 2018 11:35:10 -0400 Received: by mail-io0-f193.google.com with SMTP id k3-v6so14526064iog.3; Tue, 12 Jun 2018 08:35:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=We78XeU2Ru7FLwZA0RfQVC3UVtTY7NPwCQnCbN1y13o=; b=pS6EReyB9Xa33rsL4aYDGT6LLMXKEE7kosHQDFsWZSvu8m4xP40P6LxIyKrJ4GZ+Cc H5LHLfBPsWTZmqandELYQFZsicTZUVqCfG5bv0cuZuMaVVE0WG9C3f4TF8mFNTvnYZvb 30VUKT1BKRBBqND7KU+MFtow4oKMn2Pn9evrgQHapTjFp62t/6MsLOLG7g3dphjnudaX /NzwjvZNbWfSvX7fAwgx7DUxwcRUn8R1NbPzZAUr3Tod79uxOiiltqGMp3x6YpPNFy4O N2MDZ4R+VQJGCQ5e2ohFuf65h7Gl8HVzQN1xFxuK8u6m07ec0MZRvcAeUi1JmL9elC/a gkEA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=We78XeU2Ru7FLwZA0RfQVC3UVtTY7NPwCQnCbN1y13o=; b=W2/lK3L4Xweo2/UaZvdY7w8ro1hVud00WdqfgNlU3Cw23x3Oef9dX2aud33H5yZfvR jqOlPxpvv152Nk6PzOT8QNA2IEDvePW02/hhnhqpCGSaV/w0a6L0EeWTxb0VMPvOgHMG 3F89N8tq8/QdrF1QQRFcG959Yf/g8bCl6h/uaNfxqwOBI8H/Idk1cH6DpbxBvlBUPtqi HJVTuSL9pyZsCIeYo6F4l5kpWMnEzdEF/UfqmeSQzBEmNS3UQLHoMLxIbzTpOYjBAEc+ NiqvE0duo4/C+RPFOAr1CR28dagFrIY8ObAkevXl+1+8Jn3wGOzgwBwr+qa8L36qxncD UqPA== X-Gm-Message-State: APt69E1GbqVvYfv14KIglVPKpOuKxiBgt4RIb0hC4FUWvy1CbtTd0BN9 39tujKu+kn8K6+iwhVbik1O+VA== X-Google-Smtp-Source: ADUXVKInWOJB9Q6bsDVjta3hU+En/dhJFfZXPwrFUpUxVkqJB7h2ABXeLLELP+q21lP5FOz6w3s1gA== X-Received: by 2002:a6b:b846:: with SMTP id i67-v6mr971283iof.21.1528817709285; Tue, 12 Jun 2018 08:35:09 -0700 (PDT) Received: from localhost.localdomain ([240f:34:212d:1:c80e:bb6a:719f:e05d]) by smtp.gmail.com with ESMTPSA id q203-v6sm383863itb.32.2018.06.12.08.35.04 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 12 Jun 2018 08:35:08 -0700 (PDT) From: Akinobu Mita To: linux-media@vger.kernel.org, linux-i2c@vger.kernel.org Cc: Akinobu Mita , Sebastian Reichel , Wolfram Sang , Jacopo Mondi , Laurent Pinchart , Hans Verkuil , Sakari Ailus , Mauro Carvalho Chehab Subject: [RFC PATCH v2] media: i2c: add SCCB helpers Date: Wed, 13 Jun 2018 00:34:46 +0900 Message-Id: <1528817686-7067-1-git-send-email-akinobu.mita@gmail.com> X-Mailer: git-send-email 2.7.4 Sender: linux-media-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP (This is 2nd version of SCCB helpers patch. After 1st version was submitted, I sent alternative patch titled "i2c: add I2C_M_FORCE_STOP". But it wasn't accepted because it makes the I2C core code unreadable. I couldn't find out a way to untangle it, so I returned to the original approach.) This adds Serial Camera Control Bus (SCCB) helper functions (sccb_read_byte and sccb_write_byte) that are intended to be used by some of Omnivision sensor drivers. The ov772x driver is going to use these functions in order to make it work with most i2c controllers. As the ov772x device doesn't support repeated starts, this driver currently requires I2C_FUNC_PROTOCOL_MANGLING that is not supported by many i2c controller drivers. With the sccb_read_byte() that issues two separated requests in order to avoid repeated start, the driver doesn't require I2C_FUNC_PROTOCOL_MANGLING. Cc: Sebastian Reichel Cc: Wolfram Sang Cc: Jacopo Mondi Cc: Laurent Pinchart Cc: Hans Verkuil Cc: Sakari Ailus Cc: Mauro Carvalho Chehab Signed-off-by: Akinobu Mita Reviewed-by: Sebastian Reichel --- * v2 - Convert all helpers into static inline functions, and remove C source and Kconfig option. - Acquire i2c adapter lock while issuing two requests for sccb_read_byte drivers/media/i2c/sccb.h | 74 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 drivers/media/i2c/sccb.h diff --git a/drivers/media/i2c/sccb.h b/drivers/media/i2c/sccb.h new file mode 100644 index 0000000..a531fdc --- /dev/null +++ b/drivers/media/i2c/sccb.h @@ -0,0 +1,74 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Serial Camera Control Bus (SCCB) helper functions + */ + +#ifndef __SCCB_H__ +#define __SCCB_H__ + +#include + +/** + * sccb_read_byte - Read data from SCCB slave device + * @client: Handle to slave device + * @addr: Register to be read from + * + * This executes the 2-phase write transmission cycle that is followed by a + * 2-phase read transmission cycle, returning negative errno else a data byte + * received from the device. + */ +static inline int sccb_read_byte(struct i2c_client *client, u8 addr) +{ + u8 val; + struct i2c_msg msg[] = { + { + .addr = client->addr, + .len = 1, + .buf = &addr, + }, + { + .addr = client->addr, + .flags = I2C_M_RD, + .len = 1, + .buf = &val, + }, + }; + int ret; + int i; + + i2c_lock_adapter(client->adapter); + + /* Issue two separated requests in order to avoid repeated start */ + for (i = 0; i < 2; i++) { + ret = __i2c_transfer(client->adapter, &msg[i], 1); + if (ret != 1) + break; + } + + i2c_unlock_adapter(client->adapter); + + return i == 2 ? val : ret; +} + +/** + * sccb_write_byte - Write data to SCCB slave device + * @client: Handle to slave device + * @addr: Register to write to + * @data: Value to be written + * + * This executes the SCCB 3-phase write transmission cycle, returning negative + * errno else zero on success. + */ +static inline int sccb_write_byte(struct i2c_client *client, u8 addr, u8 data) +{ + int ret; + unsigned char msgbuf[] = { addr, data }; + + ret = i2c_master_send(client, msgbuf, 2); + if (ret < 0) + return ret; + + return 0; +} + +#endif /* __SCCB_H__ */