Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.1 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 64FACC282CC for ; Tue, 5 Feb 2019 10:05:46 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 1D73820844 for ; Tue, 5 Feb 2019 10:05:46 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=nxp.com header.i=@nxp.com header.b="cVcAGD38" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728918AbfBEKFp (ORCPT ); Tue, 5 Feb 2019 05:05:45 -0500 Received: from mail-eopbgr00061.outbound.protection.outlook.com ([40.107.0.61]:21248 "EHLO EUR02-AM5-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1728791AbfBEKFo (ORCPT ); Tue, 5 Feb 2019 05:05:44 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nxp.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=o4TPmPlG1jZpWI4jwAbocS7uDH6oLvSsH1yeP2XPPDk=; b=cVcAGD388HsHeW03CavOFyUtfnCOfHfEISpUx5lIawLLSHQW8L1iz1yf+z95ascOIjyQF+pmtaov8e7O/6v6sFbp2ArADlHj4z6urjIG1suMzjdDsPbelJFai8fnwZFGsk6PNfNVpOBiAINY2eEaPVFcKf7GODrdxTkYNviCoXY= Received: from VI1PR0401MB2496.eurprd04.prod.outlook.com (10.168.65.10) by VI1PR0401MB2526.eurprd04.prod.outlook.com (10.168.65.16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.1580.22; Tue, 5 Feb 2019 10:05:35 +0000 Received: from VI1PR0401MB2496.eurprd04.prod.outlook.com ([fe80::208f:755e:b057:2cfe]) by VI1PR0401MB2496.eurprd04.prod.outlook.com ([fe80::208f:755e:b057:2cfe%8]) with mapi id 15.20.1580.019; Tue, 5 Feb 2019 10:05:35 +0000 From: Pankaj Bansal To: Andrew Lunn , Florian Fainelli CC: "netdev@vger.kernel.org" , Pankaj Bansal Subject: [PATCH v3 2/2] netdev/phy: add MDIO bus multiplexer driven by a regmap Thread-Topic: [PATCH v3 2/2] netdev/phy: add MDIO bus multiplexer driven by a regmap Thread-Index: AQHUvTpWcibx2mInvEK3t2shRalO6A== Date: Tue, 5 Feb 2019 10:05:35 +0000 Message-ID: <20190205153014.3807-3-pankaj.bansal@nxp.com> References: <20190205153014.3807-1-pankaj.bansal@nxp.com> In-Reply-To: <20190205153014.3807-1-pankaj.bansal@nxp.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-clientproxiedby: BMXPR01CA0041.INDPRD01.PROD.OUTLOOK.COM (2603:1096:b00:c::27) To VI1PR0401MB2496.eurprd04.prod.outlook.com (2603:10a6:800:56::10) authentication-results: spf=none (sender IP is ) smtp.mailfrom=pankaj.bansal@nxp.com; x-ms-exchange-messagesentrepresentingtype: 1 x-mailer: git-send-email 2.17.1 x-originating-ip: [92.120.1.69] x-ms-publictraffictype: Email x-microsoft-exchange-diagnostics: 1;VI1PR0401MB2526;6:ZUH44FR+6QnFjR13xNgJbrMhO3YCbc57c2qxJWdMBxPEBiIR31Ye821i9GAykEzVwI8c1ZhMyU2nIHARmd+rLGTEW9DWuF1UQcDgmR94FZkQkb9Hc+LxVBTRuea08MsY2pvs8ptJm28B3xkTA6hDw/5pgfJ2u/P8GBvcoKJe2BID/xLLQPcr2vxyr/Cy2TtOL0Nu79iu3OaUPEvM4pQvCVc0CnLDfC+dg+sREIdANzve1aMhf+pABvP5jvcZA08bLR0AzZkq9kCMu8LMI2dSvPXnWwinoAAdTDBN9kxSEyFDHvFXEkOjOCdUvHR09ty6d3doHYqlLvYRufd/qy19korrGSiPdBaf/p34cVQRd4oLHtW8kApdAT1falkNi8G61nUWZ45axFzzNvAh67cZYO0Rbi3T7jmassSUWdhascwB+L97I+Q0AjnO8AL+cIQR1H7WTJhk0hfDAqpud7N7KQ==;5:nNrc9GhIu7BnK1ywZe6KTGBPkiXoP+Z1/oEIo7pdfEsz8J2TbqMt0BouGE2/oT9qLeoR3FBJ6OMiIm3YteHvcRdOSNthu1xX2Ls2STrqnPmZkhZw2TNi8xI35Jna12F4j7VQ3yCpV8RfiVm1n5a8BNp3Wg+yT7+HhoALtrHZtklIeZx2+Ltk4Z6mRoMdEJSykiot+U0bO9Mvzzsg7tcltQ==;7:ObCvKv8LcGvGqZhZE1wGK3NCYw+1UMunimz2p8bSn0b9Z3VesSeyUohZgYjQTDZcFqqFyibw/4Ttg6ffB9vvATVUi9olNG6ra1b4jGVWF6H3fYxOlw8y+2LOUEK7kWSlmdvBFKdDn0h9BGC6BoabSg== x-ms-office365-filtering-correlation-id: 025a91f1-8a91-46b1-2bac-08d68b517891 x-ms-office365-filtering-ht: Tenant x-microsoft-antispam: BCL:0;PCL:0;RULEID:(2390118)(7020095)(4652040)(8989299)(5600110)(711020)(4605077)(4618075)(4534185)(4627221)(201703031133081)(201702281549075)(8990200)(2017052603328)(7153060)(7193020);SRVR:VI1PR0401MB2526; x-ms-traffictypediagnostic: VI1PR0401MB2526: x-microsoft-antispam-prvs: x-forefront-prvs: 0939529DE2 x-forefront-antispam-report: SFV:NSPM;SFS:(10009020)(366004)(346002)(376002)(136003)(396003)(39860400002)(199004)(189003)(186003)(26005)(305945005)(53936002)(14454004)(14444005)(7736002)(50226002)(86362001)(8676002)(8936002)(76176011)(81156014)(81166006)(316002)(446003)(110136005)(99286004)(68736007)(54906003)(256004)(66066001)(105586002)(36756003)(6436002)(2906002)(6486002)(486006)(11346002)(71200400001)(25786009)(97736004)(6512007)(5024004)(6116002)(3846002)(71190400001)(1076003)(106356001)(476003)(52116002)(102836004)(6506007)(44832011)(386003)(2616005)(478600001)(4326008);DIR:OUT;SFP:1101;SCL:1;SRVR:VI1PR0401MB2526;H:VI1PR0401MB2496.eurprd04.prod.outlook.com;FPR:;SPF:None;LANG:en;PTR:InfoNoRecords;A:1;MX:1; received-spf: None (protection.outlook.com: nxp.com does not designate permitted sender hosts) x-ms-exchange-senderadcheck: 1 x-microsoft-antispam-message-info: 4BVFEf1ykbZ9SHfz0Uvg/07woJXcwRcuRIsQ2Ya5SDFYktp+FTQlj49VTuttZOZBcA28GBNsxVbvS1hiUXbpb4fdtjraMPEhNeKx1YDPHz3q0/ALSihPsPrM2x6Nt4NfL9VHBaL8Klm5v/NRdu97uZ6Kby6yolBfqU2i4YdkpVpboX76IVHwniCMMTZmfAVvqvswpPE7E88mAqyhV27Izh8VsHV+WU4xJ/pQaGKr5hDWdAw4Y+c2/uMolrfC01UrT8c9bJY0ZmdMfemTLoO/8KVvYupvvVwrXea9WeCQI19AkMBcVEc6w87TV+QmQFTmvcNL2Vd/AHKVVi/hvjjTDYkjCnifLCb6ESfsDyHW1vO8l4RMKrOmY17m3R/n1H+I+ubRLCuqQi7fm3Su9/ZCBl1q6oR+MYsZjHwklUOOI8Q= Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-OriginatorOrg: nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: 025a91f1-8a91-46b1-2bac-08d68b517891 X-MS-Exchange-CrossTenant-originalarrivaltime: 05 Feb 2019 10:05:34.1857 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-id: 686ea1d3-bc2b-4c6f-a92c-d99c5c301635 X-MS-Exchange-Transport-CrossTenantHeadersStamped: VI1PR0401MB2526 Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Add support for an MDIO bus multiplexer controlled by a regmap device, like an FPGA. Tested on a NXP LX2160AQDS board which uses the "QIXIS" FPGA attached to the i2c bus. Signed-off-by: Pankaj Bansal --- Notes: V3: - make the return type of uninit function to void - reorder the Make file change to be alphabetically sorted - fix the compilation error when mdio-mux-regmap is built as module by adding CONFIG_MDIO_BUS_MUX_REGMAP_MODULE macro in mdio-mux.h V2: - Added Kconfig entry for regmap based mdio mux - restrict the comment lines to 80 chars - use kerneldoc formatting for this function documentation. drivers/net/phy/Kconfig | 14 +++ drivers/net/phy/Makefile | 1 + drivers/net/phy/mdio-mux-regmap.c | 169 ++++++++++++++++++++++++++++ include/linux/mdio-mux.h | 34 ++++++ 4 files changed, 218 insertions(+) diff --git a/drivers/net/phy/Kconfig b/drivers/net/phy/Kconfig index 3d187cd50eb0..93ef2505caba 100644 --- a/drivers/net/phy/Kconfig +++ b/drivers/net/phy/Kconfig @@ -87,6 +87,20 @@ config MDIO_BUS_MUX_MMIOREG =20 Currently, only 8/16/32 bits registers are supported. =20 +config MDIO_BUS_MUX_REGMAP + tristate "regmap device controlled MDIO bus multiplexers" + depends on OF_MDIO && REGMAP + select MDIO_BUS_MUX + help + This module provides a driver for MDIO bus multiplexers that + are controlled via a regmap device, like an FPGA connected to i2c bus + or spi bus or memory mapped FPGA. + The multiplexer connects one of several child MDIO busses to a + parent bus. Child bus selection is under the control of one of + the FPGA's registers. + + Currently, only 32 bits registers are supported. + config MDIO_CAVIUM tristate =20 diff --git a/drivers/net/phy/Makefile b/drivers/net/phy/Makefile index 5805c0b7d60e..99737128de8a 100644 --- a/drivers/net/phy/Makefile +++ b/drivers/net/phy/Makefile @@ -29,6 +29,7 @@ obj-$(CONFIG_MDIO_BUS_MUX) +=3D mdio-mux.o obj-$(CONFIG_MDIO_BUS_MUX_BCM_IPROC) +=3D mdio-mux-bcm-iproc.o obj-$(CONFIG_MDIO_BUS_MUX_GPIO) +=3D mdio-mux-gpio.o obj-$(CONFIG_MDIO_BUS_MUX_MMIOREG) +=3D mdio-mux-mmioreg.o +obj-$(CONFIG_MDIO_BUS_MUX_REGMAP) +=3D mdio-mux-regmap.o obj-$(CONFIG_MDIO_CAVIUM) +=3D mdio-cavium.o obj-$(CONFIG_MDIO_GPIO) +=3D mdio-gpio.o obj-$(CONFIG_MDIO_HISI_FEMAC) +=3D mdio-hisi-femac.o diff --git a/drivers/net/phy/mdio-mux-regmap.c b/drivers/net/phy/mdio-mux-r= egmap.c new file mode 100644 index 000000000000..bbec43b30f3c --- /dev/null +++ b/drivers/net/phy/mdio-mux-regmap.c @@ -0,0 +1,169 @@ +// SPDX-License-Identifier: GPL-2.0+ + +/* Simple regmap based MDIO MUX driver + * + * Copyright 2018-2019 NXP + * + * Based on mdio-mux-mmioreg.c by Timur Tabi + * + * Author: + * Pankaj Bansal + */ + +#include +#include +#include +#include +#include +#include +#include + +struct mdio_mux_regmap_state { + void *mux_handle; + struct device *dev; + struct regmap *regmap; + u32 mux_reg; + u32 mask; +}; + +/** + * mdio_mux_regmap_switch_fn - This function is called by the mdio-mux lay= er + * when it thinks the mdio bus multiplexer needs + * to switch. + * @current_child: current value of the mux register (masked via s->mask)= . + * @desired_child: value of the 'reg' property of the target child MDIO no= de. + * @data: Private data used by this switch_fn passed to mdio_mux_init func= tion + * via mdio_mux_init(.., .., .., .., data, ..). + * + * The first time this function is called, current_child =3D=3D -1. + * If current_child =3D=3D desired_child, then the mux is already set to t= he + * correct bus. + */ +static int mdio_mux_regmap_switch_fn(int current_child, int desired_child, + void *data) +{ + struct mdio_mux_regmap_state *s =3D data; + bool change; + int ret; + + ret =3D regmap_update_bits_check(s->regmap, + s->mux_reg, + s->mask, + desired_child, + &change); + + if (ret) + return ret; + if (change) + dev_dbg(s->dev, "%s %d -> %d\n", __func__, current_child, + desired_child); + return ret; +} + +/** + * mdio_mux_regmap_init - control MDIO bus muxing using regmap constructs. + * @dev: device with which regmap construct is associated. + * @mux_node: mdio bus mux node that contains parent mdio bus phandle. + * This node also contains sub nodes, where each subnode denotes + * a child mdio bus. All the child mdio buses are muxed, i.e. at a + * time only one of the child mdio buses can be used. + * @data: to store the address of data allocated by this function + */ +int mdio_mux_regmap_init(struct device *dev, + struct device_node *mux_node, + void **data) +{ + struct device_node *child; + struct mdio_mux_regmap_state *s; + int ret; + u32 val; + + dev_dbg(dev, "probing node %pOF\n", mux_node); + + s =3D devm_kzalloc(dev, sizeof(*s), GFP_KERNEL); + if (!s) + return -ENOMEM; + + s->regmap =3D dev_get_regmap(dev, NULL); + if (IS_ERR(s->regmap)) { + dev_err(dev, "Failed to get parent regmap\n"); + return PTR_ERR(s->regmap); + } + + ret =3D of_property_read_u32(mux_node, "reg", &s->mux_reg); + if (ret) { + dev_err(dev, "missing or invalid reg property\n"); + return -ENODEV; + } + + /* Test Register read write */ + ret =3D regmap_read(s->regmap, s->mux_reg, &val); + if (ret) { + dev_err(dev, "error while reading reg\n"); + return ret; + } + + ret =3D regmap_write(s->regmap, s->mux_reg, val); + if (ret) { + dev_err(dev, "error while writing reg\n"); + return ret; + } + + ret =3D of_property_read_u32(mux_node, "mux-mask", &s->mask); + if (ret) { + dev_err(dev, "missing or invalid mux-mask property\n"); + return -ENODEV; + } + + /* Verify that the 'reg' property of each child MDIO bus does not + * set any bits outside of the 'mask'. + */ + for_each_available_child_of_node(mux_node, child) { + ret =3D of_property_read_u32(child, "reg", &val); + if (ret) { + dev_err(dev, "%pOF is missing a 'reg' property\n", + child); + of_node_put(child); + return -ENODEV; + } + if (val & ~s->mask) { + dev_err(dev, + "%pOF has a 'reg' value with unmasked bits\n", + child); + of_node_put(child); + return -ENODEV; + } + } + + ret =3D mdio_mux_init(dev, mux_node, mdio_mux_regmap_switch_fn, + &s->mux_handle, s, NULL); + if (ret) { + if (ret !=3D -EPROBE_DEFER) + dev_err(dev, "failed to register mdio-mux bus %pOF\n", + mux_node); + return ret; + } + + *data =3D s; + + return 0; +} +EXPORT_SYMBOL_GPL(mdio_mux_regmap_init); + +/** + * mdio_mux_regmap_uninit - relinquish the control of MDIO bus muxing usin= g + * regmap constructs. + * @data: address of data allocated by mdio_mux_regmap_init + */ +void mdio_mux_regmap_uninit(void *data) +{ + struct mdio_mux_regmap_state *s =3D data; + + mdio_mux_uninit(s->mux_handle); +} +EXPORT_SYMBOL_GPL(mdio_mux_regmap_uninit); + +MODULE_AUTHOR("Pankaj Bansal "); +MODULE_DESCRIPTION("regmap based MDIO MUX driver"); +MODULE_LICENSE("GPL"); + diff --git a/include/linux/mdio-mux.h b/include/linux/mdio-mux.h index a5d58f221939..bb5d0e2774bf 100644 --- a/include/linux/mdio-mux.h +++ b/include/linux/mdio-mux.h @@ -29,4 +29,38 @@ int mdio_mux_init(struct device *dev, =20 void mdio_mux_uninit(void *mux_handle); =20 +#if defined(CONFIG_MDIO_BUS_MUX_REGMAP) || \ + defined(CONFIG_MDIO_BUS_MUX_REGMAP_MODULE) +/** + * mdio_mux_regmap_init - control MDIO bus muxing using regmap constructs. + * @dev: device with which regmap construct is associated. + * @mux_node: mdio bus mux node that contains parent mdio bus phandle. + * This node also contains sub nodes, where each subnode denotes + * a child mdio bus. All the child mdio buses are muxed, i.e. at a + * time only one of the child mdio buses can be used. + * @data: to store the address of data allocated by this function + */ +int mdio_mux_regmap_init(struct device *dev, + struct device_node *mux_node, + void **data); + +/** + * mdio_mux_regmap_uninit - relinquish the control of MDIO bus muxing usin= g + * regmap constructs. + * @data: address of data allocated by mdio_mux_regmap_init + */ +void mdio_mux_regmap_uninit(void *data); +#else /* CONFIG_MDIO_BUS_MUX_REGMAP(_MODULE) */ +static inline int mdio_mux_regmap_init(struct device *dev, + struct device_node *mux_node, + void **data) +{ + return -ENODEV; +} + +static inline void mdio_mux_regmap_uninit(void *data) +{ +} +#endif /* CONFIG_MDIO_BUS_MUX_REGMAP(_MODULE) */ + #endif /* __LINUX_MDIO_MUX_H */ --=20 2.17.1