From patchwork Thu Jan 31 15:58:23 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: larsi@wh2.tu-dresden.de X-Patchwork-Id: 2074231 Return-Path: X-Original-To: patchwork-spi-devel-general@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork2.kernel.org Received: from lists.sourceforge.net (lists.sourceforge.net [216.34.181.88]) by patchwork2.kernel.org (Postfix) with ESMTP id 904E8DF2E5 for ; Thu, 31 Jan 2013 15:58:37 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=sfs-ml-1.v29.ch3.sourceforge.com) by sfs-ml-1.v29.ch3.sourceforge.com with esmtp (Exim 4.76) (envelope-from ) id 1U0wX7-0003bS-2o; Thu, 31 Jan 2013 15:58:37 +0000 Received: from sog-mx-3.v43.ch3.sourceforge.com ([172.29.43.193] helo=mx.sourceforge.net) by sfs-ml-1.v29.ch3.sourceforge.com with esmtp (Exim 4.76) (envelope-from ) id 1U0wX5-0003bK-Kk for spi-devel-general@lists.sourceforge.net; Thu, 31 Jan 2013 15:58:35 +0000 X-ACL-Warn: Received: from atlantis.wh2.tu-dresden.de ([141.30.228.39]) by sog-mx-3.v43.ch3.sourceforge.com with esmtp (Exim 4.76) id 1U0wX4-0004QK-Cm for spi-devel-general@lists.sourceforge.net; Thu, 31 Jan 2013 15:58:35 +0000 Received: from lem-wkst-02.routerb3c0c6.com (p50998852.dip0.t-ipconnect.de [80.153.136.82]) by atlantis.wh2.tu-dresden.de (Postfix) with ESMTPA id 0C2D783A6B0; Thu, 31 Jan 2013 16:58:26 +0100 (CET) From: Lars Poeschel To: poeschel@lemonage.de, grant.likely@secretlab.ca, rob.herring@calxeda.com, rob@landley.net, linus.walleij@linaro.org, devicetree-discuss@lists.ozlabs.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, spi-devel-general@lists.sourceforge.net Subject: [PATCH RFC 1/1] gpio: mcp23s08: convert driver to DT Date: Thu, 31 Jan 2013 16:58:23 +0100 Message-Id: <1359647903-15801-2-git-send-email-larsi@wh2.tu-dresden.de> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1359647903-15801-1-git-send-email-larsi@wh2.tu-dresden.de> References: <1359647903-15801-1-git-send-email-larsi@wh2.tu-dresden.de> X-Spam-Score: -0.1 (/) X-Spam-Report: Spam Filtering performed by mx.sourceforge.net. See http://spamassassin.org/tag/ for more details. -0.1 RP_MATCHES_RCVD Envelope sender domain matches handover relay domain X-Headers-End: 1U0wX4-0004QK-Cm Cc: w.sang@pengutronix.de, ben-linux@fluff.org, linux-i2c@vger.kernel.org X-BeenThere: spi-devel-general@lists.sourceforge.net X-Mailman-Version: 2.1.9 Precedence: list List-Id: Linux SPI core/device drivers discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: spi-devel-general-bounces@lists.sourceforge.net From: Lars Poeschel This converts the mcp23s08 driver to be able to be used with device tree. There are two properties taken, that correspond to the members of the struct mcp23s08_platform_data, that is the base member and the chip array member. Signed-off-by: Lars Poeschel --- .../devicetree/bindings/gpio/gpio-mcp23s08.txt | 27 ++++++ drivers/gpio/gpio-mcp23s08.c | 93 +++++++++++++++++++- include/linux/spi/mcp23s08.h | 1 + 3 files changed, 117 insertions(+), 4 deletions(-) create mode 100644 Documentation/devicetree/bindings/gpio/gpio-mcp23s08.txt diff --git a/Documentation/devicetree/bindings/gpio/gpio-mcp23s08.txt b/Documentation/devicetree/bindings/gpio/gpio-mcp23s08.txt new file mode 100644 index 0000000..572bc87 --- /dev/null +++ b/Documentation/devicetree/bindings/gpio/gpio-mcp23s08.txt @@ -0,0 +1,27 @@ +Microchip MCP2308/MCP23S08/MCP23017/MCP23S17 driver for +8-/16-bit I/O expander with serial interface (I2C/SPI) + +Required properties: +- compatible : Should be "mcp,mcp23s08-gpio", "mcp,mcp23s17-gpio", + "mcp,mcp23008-gpio" or "mcp,mcp23017-gpio" +- base : The first gpio number that should be assigned by this chip. + +Optional properties: +- chips : This is a table with 2 columns and up to 8 entries. The first column + is a is_present flag, that makes only sense for SPI chips. Multiple + chips can share the same chipselect. This flag can be 0 or 1 depending + if there is a chip at this address or not. + The second column is written to the GPPU register, selecting a 100k + pullup resistor or not. Setting a 1 is activating the pullup. + For I2C chips only the first line in this table is used. Further chips + are registered at different addresses at the I2C bus. + +Example: +&mcp0 { + compatible = "mcp,mcp23017"; + base = <128>; + chips = < + /* is_present pullups */ + 1 0x07 /* chip addr 0 */ + >; +}; diff --git a/drivers/gpio/gpio-mcp23s08.c b/drivers/gpio/gpio-mcp23s08.c index 3cea0ea..7f90d11 100644 --- a/drivers/gpio/gpio-mcp23s08.c +++ b/drivers/gpio/gpio-mcp23s08.c @@ -12,6 +12,8 @@ #include #include #include +#include +#include /** * MCP types supported by driver @@ -473,16 +475,89 @@ fail: /*----------------------------------------------------------------------*/ +#ifdef CONFIG_OF +static struct of_device_id mcp23s08_of_match[] = { +#ifdef CONFIG_SPI_MASTER + { + .compatible = "mcp,mcp23s08-gpio", + }, + { + .compatible = "mcp,mcp23s17-gpio", + }, +#endif +#if IS_ENABLED(CONFIG_I2C) + { + .compatible = "mcp,mcp23008-gpio", + }, + { + .compatible = "mcp,mcp23017-gpio", + }, +#endif + { }, +}; +MODULE_DEVICE_TABLE(of, mcp23s08_of_match); + +static struct mcp23s08_platform_data * + of_get_mcp23s08_pdata(struct device *dev) +{ + struct mcp23s08_platform_data *pdata; + struct device_node *np = dev->of_node; + u32 gpio_base; + u32 chips[sizeof(pdata->chip)]; + int ret, i, j; + + pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL); + if (!pdata) + return NULL; + + ret = of_property_read_u32(np, "base", &gpio_base); + if (!ret) + pdata->base = gpio_base; + + for (i = ARRAY_SIZE(pdata->chip) * MCP23S08_CHIP_INFO_MEMBERS; + i > 0; i -= MCP23S08_CHIP_INFO_MEMBERS) { + ret = of_property_read_u32_array(np, "chips", chips, i); + if (ret == -EOVERFLOW) + continue; + break; + } + if (!ret) { + for (j = 0; j < i / MCP23S08_CHIP_INFO_MEMBERS ; j++) { + pdata->chip[j].is_present = + chips[j * MCP23S08_CHIP_INFO_MEMBERS]; + pdata->chip[j].pullups = + chips[j * MCP23S08_CHIP_INFO_MEMBERS + 1]; + } + } + + return pdata; +} +#else +static struct mcp23s08_platform_data * + of_get_mcp23s08_pdata(struct i2c_client *client, int *chip_id) +{ + return NULL; +} +#endif /* CONFIG_OF */ + #if IS_ENABLED(CONFIG_I2C) static int mcp230xx_probe(struct i2c_client *client, const struct i2c_device_id *id) { - struct mcp23s08_platform_data *pdata; + struct mcp23s08_platform_data *pdata = NULL; struct mcp23s08 *mcp; int status; + const struct of_device_id *match; + + match = of_match_device(of_match_ptr(mcp23s08_of_match), &client->dev); + if (match) + pdata = of_get_mcp23s08_pdata(&client->dev); + + /* if there was no pdata in DT, take it the legacy way */ + if (!pdata) + pdata = client->dev.platform_data; - pdata = client->dev.platform_data; if (!pdata || !gpio_is_valid(pdata->base)) { dev_dbg(&client->dev, "invalid or missing platform data\n"); return -EINVAL; @@ -531,6 +606,7 @@ static struct i2c_driver mcp230xx_driver = { .driver = { .name = "mcp230xx", .owner = THIS_MODULE, + .of_match_table = mcp23s08_of_match, }, .probe = mcp230xx_probe, .remove = mcp230xx_remove, @@ -560,16 +636,24 @@ static void mcp23s08_i2c_exit(void) { } static int mcp23s08_probe(struct spi_device *spi) { - struct mcp23s08_platform_data *pdata; + struct mcp23s08_platform_data *pdata = NULL; unsigned addr; unsigned chips = 0; struct mcp23s08_driver_data *data; int status, type; unsigned base; + const struct of_device_id *match; type = spi_get_device_id(spi)->driver_data; - pdata = spi->dev.platform_data; + match = of_match_device(of_match_ptr(mcp23s08_of_match), &spi->dev); + if (match) + pdata = of_get_mcp23s08_pdata(&spi->dev); + + /* if there was no pdata in DT, take it the legacy way */ + if (!pdata) + pdata = spi->dev.platform_data; + if (!pdata || !gpio_is_valid(pdata->base)) { dev_dbg(&spi->dev, "invalid or missing platform data\n"); return -EINVAL; @@ -668,6 +752,7 @@ static struct spi_driver mcp23s08_driver = { .driver = { .name = "mcp23s08", .owner = THIS_MODULE, + .of_match_table = mcp23s08_of_match, }, }; diff --git a/include/linux/spi/mcp23s08.h b/include/linux/spi/mcp23s08.h index 2d676d5..3969e12 100644 --- a/include/linux/spi/mcp23s08.h +++ b/include/linux/spi/mcp23s08.h @@ -1,3 +1,4 @@ +#define MCP23S08_CHIP_INFO_MEMBERS 2 /* FIXME driver should be able to handle IRQs... */