From patchwork Sat Mar 26 08:28:13 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ivaylo Dimitrov X-Patchwork-Id: 8674311 Return-Path: X-Original-To: patchwork-linux-omap@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id C75FE9F44D for ; Sat, 26 Mar 2016 08:29:38 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id D1F3F202F0 for ; Sat, 26 Mar 2016 08:29:37 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id E0B7A202B8 for ; Sat, 26 Mar 2016 08:29:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753319AbcCZI3f (ORCPT ); Sat, 26 Mar 2016 04:29:35 -0400 Received: from mail-wm0-f65.google.com ([74.125.82.65]:32834 "EHLO mail-wm0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751840AbcCZI33 (ORCPT ); Sat, 26 Mar 2016 04:29:29 -0400 Received: by mail-wm0-f65.google.com with SMTP id u125so8504765wmg.0; Sat, 26 Mar 2016 01:29:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=4LNO7WrXVi1IBMb/7ciSdkUdIm0bhznFMw4eeHHvj8s=; b=kgYDxyavYf4pS2IcOL6WfiQGcV61BEos+WGgODxuWMA4NHoDa9MgJ/aE+9FXU5FqSH SSWP15GqACiR7A2uggto3++j9jbg57eI8YAVLEAh2PwSjXUnseOQ6Ufz+bumQEdswsv8 Jh2I7rgzwr+sjW0jDJsIJwV7Hhw6/fCN1qSlX2G8rMzzL29LwJDJYKL8lpkrs4DbvD4J lwyo9hBQSOG6Bzpvzf2lKHrS67DooLzlTPu5CEpBSKo+uFDqGvh1ob/aprJWXFT4wSXL vYmNR6G6OqbBAqCN14sh2uqixCDx7N83eqT/ome6R6HXrlThXlMXAIiVZEZmmSFHCetp 2t/g== 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=4LNO7WrXVi1IBMb/7ciSdkUdIm0bhznFMw4eeHHvj8s=; b=LZrkk+8UNcLwnrM2cCsICDufTNYkFOZdpNuKHIilLFxtfyvaCYGgkfMfMvJLBK00Vc rGWS8xhM4mXkUN5R9oZs00tEKP1GD2yBHheb//QE++WzThkoM2MMJNKDH/eWgCdOwUB8 tNS0IiW5ZKYKfDrgiS3drN5fbXALCrJox3o77Ht6hV6P+DoDrJOSMzOoQpaAuF0X12Gv 3dt8X1IlJJimWyvVaPZpK/jp65/cUk5FAM0nOJZTUdJBM3HD+V3lABCm1UeTTVyQScAq Gftr3BngowFDhEmv1JuLNRIVl1gxWEoDOGTctie6aAvCodA4XJJQ1Gr2zLxVkWN7qT+c YHSw== X-Gm-Message-State: AD7BkJJlhYR+MalKLBedjXOUcjdWeLNLFvlGl6pV5sVG+TsAxN22mc+LrapHz/ptNVORBw== X-Received: by 10.28.194.132 with SMTP id s126mr827913wmf.76.1458980967640; Sat, 26 Mar 2016 01:29:27 -0700 (PDT) Received: from localhost.localdomain ([46.249.74.23]) by smtp.gmail.com with ESMTPSA id u3sm670429wmg.15.2016.03.26.01.29.26 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sat, 26 Mar 2016 01:29:26 -0700 (PDT) From: Ivaylo Dimitrov To: tony@atomide.com, lgirdwood@gmail.com, broonie@kernel.org Cc: sre@kernel.org, pali.rohar@gmail.com, linux-omap@vger.kernel.org, linux-kernel@vger.kernel.org, Ivaylo Dimitrov Subject: [PATCH 1/3] regulator: twl: Make sure we have access to powerbus before trying to write to it Date: Sat, 26 Mar 2016 10:28:13 +0200 Message-Id: <1458980895-10240-2-git-send-email-ivo.g.dimitrov.75@gmail.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1458980895-10240-1-git-send-email-ivo.g.dimitrov.75@gmail.com> References: <1458980895-10240-1-git-send-email-ivo.g.dimitrov.75@gmail.com> Sender: linux-omap-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-omap@vger.kernel.org X-Spam-Status: No, score=-7.8 required=5.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, T_DKIM_INVALID, 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 According to the TRM, we need to enable i2c access to powerbus before writing to it. Also, a new write to powerbus should not be attempted if there is a pending transfer. The current code does not implement that functionality and while there are no known problems caused by that, it is better to follow what TRM says. Signed-off-by: Ivaylo Dimitrov --- drivers/regulator/twl-regulator.c | 78 +++++++++++++++++++++++++++++++++++---- 1 file changed, 70 insertions(+), 8 deletions(-) diff --git a/drivers/regulator/twl-regulator.c b/drivers/regulator/twl-regulator.c index 955a6fb..aad748b0 100644 --- a/drivers/regulator/twl-regulator.c +++ b/drivers/regulator/twl-regulator.c @@ -21,7 +21,7 @@ #include #include #include - +#include /* * The TWL4030/TW5030/TPS659x0/TWL6030 family chips include power management, a @@ -188,6 +188,74 @@ static int twl6030reg_is_enabled(struct regulator_dev *rdev) return grp && (val == TWL6030_CFG_STATE_ON); } +#define PB_I2C_BUSY BIT(0) +#define PB_I2C_BWEN BIT(1) + +/* Wait until buffer empty/ready to send a word on power bus. */ +static int twl4030_wait_pb_ready(void) +{ + + int ret; + int timeout = 10; + u8 val; + + do { + ret = twl_i2c_read_u8(TWL_MODULE_PM_MASTER, &val, + TWL4030_PM_MASTER_PB_CFG); + if (ret < 0) + return ret; + + if (!(val & PB_I2C_BUSY)) + return 0; + + mdelay(1); + timeout--; + } while (timeout); + + return -ETIMEDOUT; +} + +/* Send a word over the powerbus */ +static int twl4030_send_pb_msg(unsigned msg) +{ + u8 val; + int ret; + + /* save powerbus configuration */ + ret = twl_i2c_read_u8(TWL_MODULE_PM_MASTER, &val, + TWL4030_PM_MASTER_PB_CFG); + if (ret < 0) + return ret; + + /* Enable i2c access to powerbus */ + ret = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, val | PB_I2C_BWEN, + TWL4030_PM_MASTER_PB_CFG); + if (ret < 0) + return ret; + + ret = twl4030_wait_pb_ready(); + if (ret < 0) + return ret; + + ret = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, msg >> 8, + TWL4030_PM_MASTER_PB_WORD_MSB); + if (ret < 0) + return ret; + + ret = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, msg & 0xff, + TWL4030_PM_MASTER_PB_WORD_LSB); + if (ret < 0) + return ret; + + ret = twl4030_wait_pb_ready(); + if (ret < 0) + return ret; + + /* Restore powerbus configuration */ + return twl_i2c_write_u8(TWL_MODULE_PM_MASTER, val, + TWL_MODULE_PM_MASTER); +} + static int twl4030reg_enable(struct regulator_dev *rdev) { struct twlreg_info *info = rdev_get_drvdata(rdev); @@ -324,13 +392,7 @@ static int twl4030reg_set_mode(struct regulator_dev *rdev, unsigned mode) if (!(status & (P3_GRP_4030 | P2_GRP_4030 | P1_GRP_4030))) return -EACCES; - status = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, - message >> 8, TWL4030_PM_MASTER_PB_WORD_MSB); - if (status < 0) - return status; - - return twl_i2c_write_u8(TWL_MODULE_PM_MASTER, - message & 0xff, TWL4030_PM_MASTER_PB_WORD_LSB); + return twl4030_send_pb_msg(message); } static int twl6030reg_set_mode(struct regulator_dev *rdev, unsigned mode)