From patchwork Tue Dec 23 20:49:14 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Antti Palosaari X-Patchwork-Id: 5535301 Return-Path: X-Original-To: patchwork-linux-media@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 C37D9BEEA8 for ; Tue, 23 Dec 2014 20:51:50 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id DEADD200F4 for ; Tue, 23 Dec 2014 20:51:49 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id DC86F20120 for ; Tue, 23 Dec 2014 20:51:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756686AbaLWUvl (ORCPT ); Tue, 23 Dec 2014 15:51:41 -0500 Received: from mail.kapsi.fi ([217.30.184.167]:45260 "EHLO mail.kapsi.fi" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1756606AbaLWUua (ORCPT ); Tue, 23 Dec 2014 15:50:30 -0500 Received: from dyn3-82-128-190-202.psoas.suomi.net ([82.128.190.202] helo=localhost.localdomain.localdomain) by mail.kapsi.fi with esmtpsa (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.72) (envelope-from ) id 1Y3WPV-0004tE-2k; Tue, 23 Dec 2014 22:50:29 +0200 From: Antti Palosaari To: linux-media@vger.kernel.org Cc: Antti Palosaari Subject: [PATCH 21/66] rtl2830: implement own I2C locking Date: Tue, 23 Dec 2014 22:49:14 +0200 Message-Id: <1419367799-14263-21-git-send-email-crope@iki.fi> X-Mailer: git-send-email 2.1.0 In-Reply-To: <1419367799-14263-1-git-send-email-crope@iki.fi> References: <1419367799-14263-1-git-send-email-crope@iki.fi> X-SA-Exim-Connect-IP: 82.128.190.202 X-SA-Exim-Mail-From: crope@iki.fi X-SA-Exim-Scanned: No (on mail.kapsi.fi); SAEximRunCond expanded to false Sender: linux-media-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, T_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 Own I2C locking is needed due to two special reasons: 1) Chips uses multiple register pages/banks on single I2C slave. Page is changed via I2C register access. 2) Chip offers muxed/gated I2C adapter for tuner. Gate/mux is controlled by I2C register access. Due to these reasons, I2C locking did not fit very well. Signed-off-by: Antti Palosaari --- drivers/media/dvb-frontends/rtl2830.c | 45 +++++++++++++++++++++++++----- drivers/media/dvb-frontends/rtl2830_priv.h | 1 + 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/drivers/media/dvb-frontends/rtl2830.c b/drivers/media/dvb-frontends/rtl2830.c index 8abaca6..3a9e4e9 100644 --- a/drivers/media/dvb-frontends/rtl2830.c +++ b/drivers/media/dvb-frontends/rtl2830.c @@ -43,7 +43,7 @@ static int rtl2830_wr(struct i2c_client *client, u8 reg, const u8 *val, int len) buf[0] = reg; memcpy(&buf[1], val, len); - ret = i2c_transfer(client->adapter, msg, 1); + ret = __i2c_transfer(client->adapter, msg, 1); if (ret == 1) { ret = 0; } else { @@ -73,7 +73,7 @@ static int rtl2830_rd(struct i2c_client *client, u8 reg, u8 *val, int len) } }; - ret = i2c_transfer(client->adapter, msg, 2); + ret = __i2c_transfer(client->adapter, msg, 2); if (ret == 2) { ret = 0; } else { @@ -93,16 +93,23 @@ static int rtl2830_wr_regs(struct i2c_client *client, u16 reg, const u8 *val, in u8 reg2 = (reg >> 0) & 0xff; u8 page = (reg >> 8) & 0xff; + mutex_lock(&dev->i2c_mutex); + /* switch bank if needed */ if (page != dev->page) { ret = rtl2830_wr(client, 0x00, &page, 1); if (ret) - return ret; + goto err_mutex_unlock; dev->page = page; } - return rtl2830_wr(client, reg2, val, len); + ret = rtl2830_wr(client, reg2, val, len); + +err_mutex_unlock: + mutex_unlock(&dev->i2c_mutex); + + return ret; } /* read multiple registers */ @@ -113,16 +120,23 @@ static int rtl2830_rd_regs(struct i2c_client *client, u16 reg, u8 *val, int len) u8 reg2 = (reg >> 0) & 0xff; u8 page = (reg >> 8) & 0xff; + mutex_lock(&dev->i2c_mutex); + /* switch bank if needed */ if (page != dev->page) { ret = rtl2830_wr(client, 0x00, &page, 1); if (ret) - return ret; + goto err_mutex_unlock; dev->page = page; } - return rtl2830_rd(client, reg2, val, len); + ret = rtl2830_rd(client, reg2, val, len); + +err_mutex_unlock: + mutex_unlock(&dev->i2c_mutex); + + return ret; } /* read single register */ @@ -815,6 +829,10 @@ static int rtl2830_select(struct i2c_adapter *adap, void *mux_priv, u32 chan_id) }; int ret; + dev_dbg(&client->dev, "\n"); + + mutex_lock(&dev->i2c_mutex); + /* select register page */ ret = __i2c_transfer(client->adapter, select_reg_page_msg, 1); if (ret != 1) { @@ -841,6 +859,18 @@ err: return ret; } +static int rtl2830_deselect(struct i2c_adapter *adap, void *mux_priv, u32 chan) +{ + struct i2c_client *client = mux_priv; + struct rtl2830_dev *dev = i2c_get_clientdata(client); + + dev_dbg(&client->dev, "\n"); + + mutex_unlock(&dev->i2c_mutex); + + return 0; +} + static struct dvb_frontend *rtl2830_get_dvb_frontend(struct i2c_client *client) { struct rtl2830_dev *dev = i2c_get_clientdata(client); @@ -886,6 +916,7 @@ static int rtl2830_probe(struct i2c_client *client, dev->client = client; dev->pdata = client->dev.platform_data; dev->sleeping = true; + mutex_init(&dev->i2c_mutex); INIT_DELAYED_WORK(&dev->stat_work, rtl2830_stat_work); /* check if the demod is there */ @@ -895,7 +926,7 @@ static int rtl2830_probe(struct i2c_client *client, /* create muxed i2c adapter for tuner */ dev->adapter = i2c_add_mux_adapter(client->adapter, &client->dev, - client, 0, 0, 0, rtl2830_select, NULL); + client, 0, 0, 0, rtl2830_select, rtl2830_deselect); if (dev->adapter == NULL) { ret = -ENODEV; goto err_kfree; diff --git a/drivers/media/dvb-frontends/rtl2830_priv.h b/drivers/media/dvb-frontends/rtl2830_priv.h index 2931889..517758a 100644 --- a/drivers/media/dvb-frontends/rtl2830_priv.h +++ b/drivers/media/dvb-frontends/rtl2830_priv.h @@ -30,6 +30,7 @@ struct rtl2830_dev { struct i2c_adapter *adapter; struct dvb_frontend fe; bool sleeping; + struct mutex i2c_mutex; u8 page; /* active register page */ unsigned long filters; struct delayed_work stat_work;