Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp349485pxb; Tue, 1 Feb 2022 00:55:36 -0800 (PST) X-Google-Smtp-Source: ABdhPJzcUG2FmhhekzrLWFJlpGVO00W3g3bvLe2kZhrkjOsD7g8zN0GLuMpNI7mijebR2lDuwxd8 X-Received: by 2002:a17:907:16aa:: with SMTP id hc42mr19921167ejc.307.1643705736619; Tue, 01 Feb 2022 00:55:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643705736; cv=none; d=google.com; s=arc-20160816; b=XS9hEbNE60izApmWRfVpVXXhe5L3NuTVyV8ABHGGkTU5XUiGBlCO/i0LlvsSw9DNN3 UmALiFZtd9wlJ5/dUITGblKtl3unQYtVRxB77Brhb+7ecR7GSQI01a6JFdTG/mLiCTgH riD/AxK+rlbIJCr7JdEGaBkwmox0h1GocdBIzP9WdR16ZjObc5Q953BArlDyfmqFcqeE lVgvF30fmCN3vttE7N7jIy8x6mcs8NHloTS3mt5D8Ix35B4VHFj4NOh9k/4tvBLxD2/X C+Bfx/yzHGju6XvLFmBaFtHuYpnEd9RWI/DBOfVr1QwVOtgPNb3+XB5+Gst5EXAOsyFD HXNg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature:dkim-signature; bh=vrBXDS1ojZa8egDi/tODkXvIJSt4wA5hxeMg5uTMGVc=; b=FwtmWAimG8j7FuplCIAN1ACESNayzSZGGS/yfLK/p5akrOQBe84rgUAlwurBRE2Ci/ Uv38qQmQaXgRAQv0KD829OSj/th8i1gwAwaU1/QzdhoWGMl995f+cZta3Q8jXago+/CC bxfedU1P+ByMveq4bn1nGT0TX8XESYVLH0DmM5YXWNTu26olmMZC8OjYRqYNlyPsu1mW zUwFXQ0YxmWTGg6hmC1RBdT2wAHzOVlVTXNgPmY8bu0EpB8zMhtZ+YmOetQMZb/tRiU0 PmaMhlG+QOJfizm9bZCMxZtWCU3PuFE4THDZ2H6C+f2SDUWBtbGQkDA9dngqZsBlVMWN aB7A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sholland.org header.s=fm1 header.b=dqBoh0rG; dkim=pass header.i=@messagingengine.com header.s=fm1 header.b=W++CoLDg; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id a4si8670293edy.53.2022.02.01.00.55.11; Tue, 01 Feb 2022 00:55:36 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@sholland.org header.s=fm1 header.b=dqBoh0rG; dkim=pass header.i=@messagingengine.com header.s=fm1 header.b=W++CoLDg; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1353438AbiA2XBJ (ORCPT + 99 others); Sat, 29 Jan 2022 18:01:09 -0500 Received: from wout2-smtp.messagingengine.com ([64.147.123.25]:37149 "EHLO wout2-smtp.messagingengine.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1353408AbiA2XA6 (ORCPT ); Sat, 29 Jan 2022 18:00:58 -0500 Received: from compute2.internal (compute2.nyi.internal [10.202.2.46]) by mailout.west.internal (Postfix) with ESMTP id 1AFA63200AB0; Sat, 29 Jan 2022 18:00:57 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute2.internal (MEProxy); Sat, 29 Jan 2022 18:00:57 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sholland.org; h= cc:cc:content-transfer-encoding:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:sender :subject:subject:to:to; s=fm1; bh=vrBXDS1ojZa8egDi/tODkXvIJSt4wA 5hxeMg5uTMGVc=; b=dqBoh0rGN3lCWXQoPNMje0UEEIjL02b6ctRVPl/QpcWO/X SUjW4f7bz0ZG6iAYCfce4LhZcqbhI95NkJUyTuNtsw7r+VjrlgUZPTswZVBK02He Jv04cBcuaXKixmiMjrPXHUYxtC/lOmpCFPUOjjo3CYAxB1UDQXMLRfEhDln5wjxS aIze9ekuhUa3xnrPNa7o/j5cLN5h1H/9pUDmBhHbm1lpGs2/l93GZPTOGaCy+PGr Ql8+TR4FyOUEOiwrx27L36FgmftJBlVIeJHgyXcufW9LlNkBiQ17zwjlEwXj/W8U astzxdNeSvWr/oKPmGVw837wsA1yEdt0mMZFGLtQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding:date:date :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:sender:subject:subject:to:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm1; bh=vrBXDS 1ojZa8egDi/tODkXvIJSt4wA5hxeMg5uTMGVc=; b=W++CoLDg/b5GV0OtwvlAhg 3fLX+x7Hk/mzIVM3Gyk4SNmTu6Sm7/PkMjXdoNQd0HKdr903uzpfU4H9sJrFg5/R A84jTlMvwDWSc5dnDtC+zS/9fDS5vUp17s3YxRAukD6BCZefEgeVJ8+OABoYQIwP /WfF6eeZMU2hQz6Zk7X9b1jDsIx4Z5Ku69M+oMcaDUlUJApRvM21ycjKhe9vriMZ VLgqWhiIUUfnQslYA3MpHsJsRYz6oXF3jkGcecZPnWSrcxp6VuKXPurTo8WeVUF1 7aFo3ais4d/lINh6XdGWTQOYtFG5+dyBBYC+HMbPniW9uLjUr4xA0Uf12F+OiHOA == X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvvddrfeejgdduieelucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhephffvufffkffojghfggfgsedtkeertdertddtnecuhfhrohhmpefurghmuhgv lhcujfholhhlrghnugcuoehsrghmuhgvlhesshhhohhllhgrnhgurdhorhhgqeenucggtf frrghtthgvrhhnpeduhfejfedvhffgfeehtefghfeiiefgfeehgfdvvdevfeegjeehjedv gfejheeuieenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhroh hmpehsrghmuhgvlhesshhhohhllhgrnhgurdhorhhg X-ME-Proxy: Received: by mail.messagingengine.com (Postfix) with ESMTPA; Sat, 29 Jan 2022 18:00:56 -0500 (EST) From: Samuel Holland To: Dmitry Torokhov , linux-input@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Rob Herring , devicetree@vger.kernel.org, linux-i2c@vger.kernel.org, Wolfram Sang , Ondrej Jirman , Samuel Holland Subject: [PATCH 4/5] Input: pinephone-keyboard - Support the proxied I2C bus Date: Sat, 29 Jan 2022 17:00:41 -0600 Message-Id: <20220129230043.12422-5-samuel@sholland.org> X-Mailer: git-send-email 2.33.1 In-Reply-To: <20220129230043.12422-1-samuel@sholland.org> References: <20220129230043.12422-1-samuel@sholland.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The PinePhone keyboard case contains a battery managed by an integrated power bank IC. The power bank IC communicates over I2C, and the keyboard MCU firmware provides an interface to read and write its registers. Let's use this interface to implement a SMBus adapter, so we can reuse the driver for the power bank IC. Signed-off-by: Samuel Holland --- drivers/input/keyboard/pinephone-keyboard.c | 73 +++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/drivers/input/keyboard/pinephone-keyboard.c b/drivers/input/keyboard/pinephone-keyboard.c index 8065bc3e101a..7d2e16e588a0 100644 --- a/drivers/input/keyboard/pinephone-keyboard.c +++ b/drivers/input/keyboard/pinephone-keyboard.c @@ -3,6 +3,7 @@ // Copyright (C) 2021-2022 Samuel Holland #include +#include #include #include #include @@ -23,6 +24,11 @@ #define PPKB_SCAN_DATA 0x08 #define PPKB_SYS_CONFIG 0x20 #define PPKB_SYS_CONFIG_DISABLE_SCAN BIT(0) +#define PPKB_SYS_SMBUS_COMMAND 0x21 +#define PPKB_SYS_SMBUS_DATA 0x22 +#define PPKB_SYS_COMMAND 0x23 +#define PPKB_SYS_COMMAND_SMBUS_READ 0x91 +#define PPKB_SYS_COMMAND_SMBUS_WRITE 0xa1 #define PPKB_DEFAULT_KEYMAP_ROWS 6 #define PPKB_DEFAULT_KEYMAP_COLS 12 @@ -132,6 +138,7 @@ static const struct matrix_keymap_data ppkb_default_keymap_data = { }; struct pinephone_keyboard { + struct i2c_adapter adapter; struct input_dev *input; unsigned short *fn_keymap; u8 crc_table[CRC8_TABLE_SIZE]; @@ -143,6 +150,57 @@ struct pinephone_keyboard { u8 buf[]; }; +static int ppkb_adap_smbus_xfer(struct i2c_adapter *adap, u16 addr, + unsigned short flags, char read_write, + u8 command, int size, + union i2c_smbus_data *data) +{ + struct i2c_client *client = adap->algo_data; + u8 buf[3]; + int ret; + + buf[0] = command; + buf[1] = data->byte; + buf[2] = read_write == I2C_SMBUS_READ ? PPKB_SYS_COMMAND_SMBUS_READ + : PPKB_SYS_COMMAND_SMBUS_WRITE; + + ret = i2c_smbus_write_i2c_block_data(client, PPKB_SYS_SMBUS_COMMAND, + sizeof(buf), buf); + if (ret) + return ret; + + /* Read back the command status until it passes or fails. */ + do { + usleep_range(300, 500); + ret = i2c_smbus_read_byte_data(client, PPKB_SYS_COMMAND); + } while (ret == buf[2]); + if (ret < 0) + return ret; + /* Commands return 0x00 on success and 0xff on failure. */ + if (ret) + return -EIO; + + if (read_write == I2C_SMBUS_READ) { + ret = i2c_smbus_read_byte_data(client, PPKB_SYS_SMBUS_DATA); + if (ret < 0) + return ret; + + data->byte = ret; + } + + return 0; +} + +static u32 ppkg_adap_functionality(struct i2c_adapter *adap) +{ + return I2C_FUNC_SMBUS_BYTE_DATA; +} + +static const struct i2c_algorithm ppkb_adap_algo = { + .smbus_xfer = ppkb_adap_smbus_xfer, + .functionality = ppkg_adap_functionality, +}; + static int ppkb_set_scan(struct i2c_client *client, bool enable) { struct device *dev = &client->dev; @@ -265,6 +323,7 @@ static int ppkb_probe(struct i2c_client *client) unsigned int map_rows, map_cols; struct pinephone_keyboard *ppkb; u8 info[PPKB_MATRIX_SIZE + 1]; + struct device_node *i2c_bus; int ret; ret = i2c_smbus_read_i2c_block_data(client, 0, sizeof(info), info); @@ -312,6 +371,20 @@ static int ppkb_probe(struct i2c_client *client) i2c_set_clientdata(client, ppkb); + i2c_bus = of_get_child_by_name(dev->of_node, "i2c-bus"); + if (i2c_bus) { + ppkb->adapter.owner = THIS_MODULE; + ppkb->adapter.algo = &ppkb_adap_algo; + ppkb->adapter.algo_data = client; + ppkb->adapter.dev.parent = dev; + ppkb->adapter.dev.of_node = i2c_bus; + strscpy(ppkb->adapter.name, DRV_NAME, sizeof(ppkb->adapter.name)); + + ret = devm_i2c_add_adapter(dev, &ppkb->adapter); + if (ret) + return dev_err_probe(dev, ret, "Failed to add I2C adapter\n"); + } + crc8_populate_msb(ppkb->crc_table, PPKB_CRC8_POLYNOMIAL); ppkb->row_shift = get_count_order(map_cols); ppkb->rows = map_rows; -- 2.33.1