Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp1158868pxb; Fri, 6 Nov 2020 02:30:39 -0800 (PST) X-Google-Smtp-Source: ABdhPJyoJAq5wUTvQVPgAvJ8wcBKoIENKPpt7cploPWJllCgnTmOix418sK5I39Gc5ErRFq1ghUF X-Received: by 2002:a17:906:ad8e:: with SMTP id la14mr1277443ejb.264.1604658639409; Fri, 06 Nov 2020 02:30:39 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1604658639; cv=none; d=google.com; s=arc-20160816; b=iyhCD0ViyFTtRfbhllfgQBeb66IaD+bqjTuRkt2I97zhXBARDCJjKLkRx/eyZjYq5P MaB/qOIW1Us0mrA8sDkMpTJmhdfwuaXNoy6gxLyWLAJMszIvhN+at1qBptiIV8MbOpbY pEzaOlwAbMFCSF7QNEgEfi8hA2ocfWu42lV1AuEAGm3YViLOLLGpoLHMZ4nFIUjP0Mds FFsxTAoZbU1emne9juEt2i6K3SHGU/Hu93Vz8yi0LBiMkx4rFYAuyfnpQPhRuOb3VzCN bBR5oLTaUrjJyq1BKJtWSNuxU1+CnHj9ivb0eB//9XJN1UzFHu/rEbjqP19DdI1BE3UD x/NA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:message-id:date:subject :cc:to:from:dkim-signature; bh=5CNEpUnBVq43LgBBDjkFJ/p38jSB+2E/Oazale2xNio=; b=kfdPY5s4K1jLKlkgnKO0SyExMAy8k+FcrQ6zpGN9668XXMxf3jv5/WMFtUj8stubZ2 hoe6E6jwl5wAJeuZQHPUYOJP6FjkX8pwKodvbLjuIGzkX4+etbQeqJz95DSX6DrgjC+Q 85MqvZw0E7SHCqOCNCNxVAGwyQJGyzLcOQ+8K205+L5yrj1lmNdM61v5JVmmUNClv9JO grLgjvxtIOZYTzsXIID70t0KNoeTkC+03S8HQEyx7YF8F7fJ8epAvmq6HZ/Yli6rOnh1 679h++bj2HqDCm05XZPghscuOQSWo1WjAd3g9bF88XUBhJx54OlEC3ngisKaGmdExf9O gpUw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=SDmV500V; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id c8si605312edl.252.2020.11.06.02.30.16; Fri, 06 Nov 2020 02:30:39 -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=@gmail.com header.s=20161025 header.b=SDmV500V; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727148AbgKFK01 (ORCPT + 99 others); Fri, 6 Nov 2020 05:26:27 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33868 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727169AbgKFK0V (ORCPT ); Fri, 6 Nov 2020 05:26:21 -0500 Received: from mail-pl1-x643.google.com (mail-pl1-x643.google.com [IPv6:2607:f8b0:4864:20::643]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A010CC0613CF; Fri, 6 Nov 2020 02:26:21 -0800 (PST) Received: by mail-pl1-x643.google.com with SMTP id u2so454994pls.10; Fri, 06 Nov 2020 02:26:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=5CNEpUnBVq43LgBBDjkFJ/p38jSB+2E/Oazale2xNio=; b=SDmV500Vgew6p1zHD+zB0d4PyGt9mKQKeXbJG73c/mknCgabK1SZ+oRp9MXl2I0YyW 8MM82CImHslLSjYCByzqzUmRqlpzqmAIeZwknQTLtwpzPeb1oaOgH7nggKfG2VgOltiL HnCKbFY4i/JTU4+kdR1qh6TOQahxtJ+dbMLahKr63mVT7jdWSbnuLXh94Zq2Vp/AFnMs I9crD0Og2gd7CWL8QoLHd15XdSP7UYauEg26Z9lrrqgwNQ2WZzMTIxy/5FE0Ht19vxn2 vTAfdQM3LQu/q4tyuw1ipFPx/1rmd7jEXQWNMTF0mmF9dvRFQp75xKmPaxT7Nsd5sDKl U5dg== 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:in-reply-to :references; bh=5CNEpUnBVq43LgBBDjkFJ/p38jSB+2E/Oazale2xNio=; b=Vs/gAhZNGgOGhQkrSYUk1H84NVp2TwXuIq/vyAFr8bfuAegcM3gzMOVwb817DuUELR oSg6RnTAvRo0WUm8UGMvxYRQPAHmXWTL2z9D4LopAeGUC+eUXrbxwrgHsQdzI7bMqgjr JFbpI9XsCLiGYQPZ8bP66AubhUrZMONe05S8YDu+MoArj8wqn543YAFIXoGKTrCth4QE cAuL61cGb+jvXRPtEDtWjdk8LtPI0K3GBeYRuGhCTacgsYV0nqKjwCKoXUfKV1F7q8JD IXtBzS2Y/mriIW/rWTtEebXRMrHDRIdLk4L31WulVAF+xlzJF12z/VKdsZvV1k73+R5e zzZA== X-Gm-Message-State: AOAM533F3HHDUC+479mkZBTgr4fBoj1SISyhIdHChYd4MwmheY1JXJdW LsIewfJ2hy0SD97LJCYuDFc= X-Received: by 2002:a17:902:82c3:b029:d6:c377:c87e with SMTP id u3-20020a17090282c3b02900d6c377c87emr1266894plz.85.1604658381213; Fri, 06 Nov 2020 02:26:21 -0800 (PST) Received: from localhost.localdomain ([2402:7500:57a:6823:8ab3:4b5d:4c53:f39b]) by smtp.gmail.com with ESMTPSA id g3sm1260633pgl.55.2020.11.06.02.26.17 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 06 Nov 2020 02:26:20 -0800 (PST) From: Gene Chen To: sre@kernel.org, matthias.bgg@gmail.com, robh+dt@kernel.org Cc: linux-pm@vger.kernel.org, devicetree@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mediatek@lists.infradead.org, linux-kernel@vger.kernel.org, gene_chen@richtek.com, Wilma.Wu@mediatek.com, shufan_lee@richtek.com, cy_huang@richtek.com, benjamin.chao@mediatek.com Subject: [PATCH v6 10/11] mfd: mt6360: Merge different sub-devices I2C read/write Date: Fri, 6 Nov 2020 17:53:51 +0800 Message-Id: <1604656432-10215-11-git-send-email-gene.chen.richtek@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1604656432-10215-1-git-send-email-gene.chen.richtek@gmail.com> References: <1604656432-10215-1-git-send-email-gene.chen.richtek@gmail.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Gene Chen Merge different sub-devices I2C read/write functions into one Regmap, because PMIC and LDO part need CRC bits for access protection. Signed-off-by: Gene Chen --- drivers/mfd/Kconfig | 1 + drivers/mfd/mt6360-core.c | 203 ++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 181 insertions(+), 23 deletions(-) diff --git a/drivers/mfd/Kconfig b/drivers/mfd/Kconfig index 33df083..1dfe446 100644 --- a/drivers/mfd/Kconfig +++ b/drivers/mfd/Kconfig @@ -913,6 +913,7 @@ config MFD_MT6360 select MFD_CORE select REGMAP_I2C select REGMAP_IRQ + select CRC8 depends on I2C help Say Y here to enable MT6360 PMU/PMIC/LDO functional support. diff --git a/drivers/mfd/mt6360-core.c b/drivers/mfd/mt6360-core.c index 14a9927..4bcfa7f 100644 --- a/drivers/mfd/mt6360-core.c +++ b/drivers/mfd/mt6360-core.c @@ -62,6 +62,18 @@ struct mt6360_ddata { #define MT6360_CRC8_POLYNOMIAL 0x7 +#define MT6360_CRC_I2C_ADDR_SIZE 1 +#define MT6360_CRC_REG_ADDR_SIZE 1 +/* prealloca read size = i2c device addr + i2c reg addr + val ... + crc8 */ +#define MT6360_ALLOC_READ_SIZE(_size) (_size + 3) +/* prealloca write size = i2c device addr + i2c reg addr + val ... + crc8 + dummy byte */ +#define MT6360_ALLOC_WRITE_SIZE(_size) (_size + 4) +#define MT6360_CRC_PREDATA_OFFSET (MT6360_CRC_I2C_ADDR_SIZE + MT6360_CRC_REG_ADDR_SIZE) +#define MT6360_CRC_CRC8_SIZE 1 +#define MT6360_CRC_DUMMY_BYTE_SIZE 1 +#define MT6360_REGMAP_REG_BYTE_SIZE 2 +#define I2C_ADDR_XLATE_8BIT(_addr, _rw) (((_addr & 0x7F) << 1) + _rw) + /* reg 0 -> 0 ~ 7 */ #define MT6360_CHG_TREG_EVT 4 #define MT6360_CHG_AICR_EVT 5 @@ -266,12 +278,6 @@ static const struct regmap_irq_chip mt6360_irq_chip = { .use_ack = true, }; -static const struct regmap_config mt6360_pmu_regmap_config = { - .reg_bits = 8, - .val_bits = 8, - .max_register = MT6360_PMU_MAXREG, -}; - static const struct resource mt6360_adc_resources[] = { DEFINE_RES_IRQ_NAMED(MT6360_ADC_DONEI, "adc_donei"), }; @@ -359,10 +365,160 @@ static int mt6360_check_vendor_info(struct mt6360_ddata *ddata) } static const unsigned short mt6360_slave_addr[MT6360_SLAVE_MAX] = { - MT6360_PMU_SLAVEID, + MT6360_TCPC_SLAVEID, MT6360_PMIC_SLAVEID, MT6360_LDO_SLAVEID, - MT6360_TCPC_SLAVEID, + MT6360_PMU_SLAVEID, +}; + +static int mt6360_xlate_pmicldo_addr(u8 *addr, int rw_size) +{ + /* Address is already in encoded [5:0] */ + *addr &= MT6360_ADDRESS_MASK; + + switch (rw_size) { + case 1: + *addr |= MT6360_DATA_SIZE_1_BYTE; + break; + case 2: + *addr |= MT6360_DATA_SIZE_2_BYTES; + break; + case 3: + *addr |= MT6360_DATA_SIZE_3_BYTES; + break; + case 4: + *addr |= MT6360_DATA_SIZE_4_BYTES; + break; + default: + return -EINVAL; + } + + return 0; +} + +static int mt6360_regmap_read(void *context, const void *reg, size_t reg_size, + void *val, size_t val_size) +{ + struct mt6360_ddata *ddata = context; + u8 bank = *(u8 *)reg; + u8 reg_addr = *(u8 *)(reg + 1); + struct i2c_client *i2c = ddata->i2c[bank]; + bool crc_needed = false; + u8 *buf; + int buf_len = MT6360_ALLOC_READ_SIZE(val_size); + int read_size = val_size; + u8 crc; + int ret; + + if (bank == MT6360_SLAVE_PMIC || bank == MT6360_SLAVE_LDO) { + crc_needed = true; + ret = mt6360_xlate_pmicldo_addr(®_addr, val_size); + if (ret < 0) + return ret; + read_size += MT6360_CRC_CRC8_SIZE; + } + + buf = kzalloc(buf_len, GFP_KERNEL); + if (!buf) + return -ENOMEM; + + buf[0] = I2C_ADDR_XLATE_8BIT(i2c->addr, I2C_SMBUS_READ); + buf[1] = reg_addr; + + ret = i2c_smbus_read_i2c_block_data(i2c, reg_addr, read_size, + buf + MT6360_CRC_PREDATA_OFFSET); + if (ret < 0) + goto out; + else if (ret != read_size) { + ret = -EIO; + goto out; + } + + if (crc_needed) { + crc = crc8(ddata->crc8_tbl, buf, val_size + MT6360_CRC_PREDATA_OFFSET, 0); + if (crc != buf[val_size + MT6360_CRC_PREDATA_OFFSET]) { + ret = -EIO; + goto out; + } + } + + memcpy(val, buf + MT6360_CRC_PREDATA_OFFSET, val_size); +out: + kfree(buf); + return (ret < 0) ? ret : 0; +} + +static int mt6360_regmap_write(void *context, const void *val, size_t val_size) +{ + struct mt6360_ddata *ddata = context; + u8 bank = *(u8 *)val; + u8 reg_addr = *(u8 *)(val + 1); + struct i2c_client *i2c = ddata->i2c[bank]; + bool crc_needed = false; + u8 *buf; + int buf_len = MT6360_ALLOC_WRITE_SIZE(val_size); + int write_size = val_size - MT6360_REGMAP_REG_BYTE_SIZE; + int ret; + + if (bank == MT6360_SLAVE_PMIC || bank == MT6360_SLAVE_LDO) { + crc_needed = true; + ret = mt6360_xlate_pmicldo_addr(®_addr, val_size - MT6360_REGMAP_REG_BYTE_SIZE); + if (ret < 0) + return ret; + } + + buf = kzalloc(buf_len, GFP_KERNEL); + if (!buf) + return -ENOMEM; + + buf[0] = I2C_ADDR_XLATE_8BIT(i2c->addr, I2C_SMBUS_WRITE); + buf[1] = reg_addr; + memcpy(buf + MT6360_CRC_PREDATA_OFFSET, val + MT6360_REGMAP_REG_BYTE_SIZE, write_size); + + if (crc_needed) { + buf[val_size] = crc8(ddata->crc8_tbl, buf, val_size, 0); + write_size += (MT6360_CRC_CRC8_SIZE + MT6360_CRC_DUMMY_BYTE_SIZE); + } + + ret = i2c_smbus_write_i2c_block_data(i2c, reg_addr, write_size, + buf + MT6360_CRC_PREDATA_OFFSET); + + kfree(buf); + return ret; +} + +static const struct regmap_bus mt6360_regmap_bus = { + .read = mt6360_regmap_read, + .write = mt6360_regmap_write, + + /* Due to PMIC and LDO CRC access size limit */ + .max_raw_read = 4, + .max_raw_write = 4, +}; + +static bool mt6360_is_readwrite_reg(struct device *dev, unsigned int reg) +{ + switch (reg) { + case MT6360_REG_TCPCSTART ... MT6360_REG_TCPCEND: + fallthrough; + case MT6360_REG_PMICSTART ... MT6360_REG_PMICEND: + fallthrough; + case MT6360_REG_LDOSTART ... MT6360_REG_LDOEND: + fallthrough; + case MT6360_REG_PMUSTART ... MT6360_REG_PMUEND: + return true; + } + + return false; +} + +static const struct regmap_config mt6360_regmap_config = { + .reg_bits = 16, + .val_bits = 8, + .reg_format_endian = REGMAP_ENDIAN_BIG, + .max_register = MT6360_REG_PMUEND, + .writeable_reg = mt6360_is_readwrite_reg, + .readable_reg = mt6360_is_readwrite_reg, }; static int mt6360_probe(struct i2c_client *client) @@ -377,7 +533,22 @@ static int mt6360_probe(struct i2c_client *client) ddata->dev = &client->dev; i2c_set_clientdata(client, ddata); - ddata->regmap = devm_regmap_init_i2c(client, &mt6360_pmu_regmap_config); + for (i = 0; i < MT6360_SLAVE_MAX - 1; i++) { + ddata->i2c[i] = devm_i2c_new_dummy_device(&client->dev, + client->adapter, + mt6360_slave_addr[i]); + if (IS_ERR(ddata->i2c[i])) { + dev_err(&client->dev, + "Failed to get new dummy I2C device for address 0x%x", + mt6360_slave_addr[i]); + return PTR_ERR(ddata->i2c[i]); + } + } + ddata->i2c[MT6360_SLAVE_MAX - 1] = client; + + crc8_populate_msb(ddata->crc8_tbl, MT6360_CRC8_POLYNOMIAL); + ddata->regmap = devm_regmap_init(ddata->dev, &mt6360_regmap_bus, ddata, + &mt6360_regmap_config); if (IS_ERR(ddata->regmap)) { dev_err(&client->dev, "Failed to register regmap\n"); return PTR_ERR(ddata->regmap); @@ -395,20 +566,6 @@ static int mt6360_probe(struct i2c_client *client) return ret; } - ddata->i2c[0] = client; - for (i = 1; i < MT6360_SLAVE_MAX; i++) { - ddata->i2c[i] = devm_i2c_new_dummy_device(&client->dev, - client->adapter, - mt6360_slave_addr[i]); - if (IS_ERR(ddata->i2c[i])) { - dev_err(&client->dev, - "Failed to get new dummy I2C device for address 0x%x", - mt6360_slave_addr[i]); - return PTR_ERR(ddata->i2c[i]); - } - i2c_set_clientdata(ddata->i2c[i], ddata); - } - ret = devm_mfd_add_devices(&client->dev, PLATFORM_DEVID_AUTO, mt6360_devs, ARRAY_SIZE(mt6360_devs), NULL, 0, regmap_irq_get_domain(ddata->irq_data)); -- 2.7.4