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: 928386 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; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="pS6EReyB"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 414v8c34Fbz9s0w for ; Wed, 13 Jun 2018 01:35:12 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754244AbeFLPfL (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-i2c-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-i2c@vger.kernel.org (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__ */