Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751894AbdC2CHT (ORCPT ); Tue, 28 Mar 2017 22:07:19 -0400 Received: from mail-it0-f54.google.com ([209.85.214.54]:37642 "EHLO mail-it0-f54.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751407AbdC2CHR (ORCPT ); Tue, 28 Mar 2017 22:07:17 -0400 MIME-Version: 1.0 In-Reply-To: <6bf04ba1761f0692cb461558f0c8836f0d1f7ad8.1490595641.git.nandor.han@ge.com> References: <6bf04ba1761f0692cb461558f0c8836f0d1f7ad8.1490595641.git.nandor.han@ge.com> From: Linus Walleij Date: Wed, 29 Mar 2017 04:07:14 +0200 Message-ID: Subject: Re: [PATCH 1/3] gpio - Add EXAR XRA1403 SPI GPIO expander driver To: Nandor Han Cc: Alexandre Courbot , Rob Herring , Mark Rutland , "linux-gpio@vger.kernel.org" , "devicetree@vger.kernel.org" , "linux-kernel@vger.kernel.org" , Semi Malinen Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 3921 Lines: 141 On Mon, Mar 27, 2017 at 8:23 AM, Nandor Han wrote: > This is a simple driver that provides a /sys/class/gpio > interface for controlling and configuring the GPIO lines. Use the gpio tools in tools/gpio, use the characcter device. Do not use sysfs. Change this to reference the tools. > It does not provide support for chip select or interrupts. > > Signed-off-by: Nandor Han > Signed-off-by: Semi Malinen (...) > +exar Exar Corporation Send this as a separate patch to the DT bindings maintainer (Rob Herring.) > +static int xra1403_get_byte(struct xra1403 *xra, unsigned int addr) > +{ > + return spi_w8r8(xra->spi, XRA_READ | (addr << 1)); > +} > + > +static int xra1403_get_bit(struct xra1403 *xra, unsigned int addr, > + unsigned int bit) > +{ > + int ret; > + > + ret = xra1403_get_byte(xra, addr + (bit > 7)); > + if (ret < 0) > + return ret; > + > + return !!(ret & BIT(bit % 8)); > +} This looks like it can use regmap-spi right off, do you agree? git grep devm_regmap_init_spi should give you some examples of how to use it. If it's not off-the shelf regmap drivers like drivers/iio/pressure/mpl115_spi.c give examples of how to make more elaborate custom SPI transfers with regmap. > +static int xra1403_set_bit(struct xra1403 *xra, unsigned int addr, > + unsigned int bit, int value) > +{ > + int ret; > + u8 mask; > + u8 tx[2]; > + > + addr += bit > 7; > + > + mutex_lock(&xra->lock); > + > + ret = xra1403_get_byte(xra, addr); > + if (ret < 0) > + goto out_unlock; > + > + mask = BIT(bit % 8); > + if (value) > + value = ret | mask; > + else > + value = ret & ~mask; > + > + if (value != ret) { > + tx[0] = addr << 1; > + tx[1] = value; > + ret = spi_write(xra->spi, tx, sizeof(tx)); > + } else { > + ret = 0; > + } > + > +out_unlock: > + mutex_unlock(&xra->lock); > + > + return ret; > +} Classical mask-and-set implementation right? With regmap this becomes simply regmap_update_bits(map, addr, mask, set) > +static int xra1403_probe(struct spi_device *spi) > +{ > + struct xra1403 *xra; > + struct gpio_desc *reset_gpio; > + > + xra = devm_kzalloc(&spi->dev, sizeof(*xra), GFP_KERNEL); > + if (!xra) > + return -ENOMEM; > + > + /* bring the chip out of reset */ > + reset_gpio = gpiod_get_optional(&spi->dev, "reset", GPIOD_OUT_LOW); > + if (IS_ERR(reset_gpio)) > + dev_warn(&spi->dev, "could not get reset-gpios\n"); > + else if (reset_gpio) > + gpiod_put(reset_gpio); I don't think you should put it, other than in the remove() function and in that case you need to have it in the state container. > + mutex_init(&xra->lock); > + > + xra->chip.direction_input = xra1403_direction_input; > + xra->chip.direction_output = xra1403_direction_output; Please implement .get_direction(). This is very nice to have. > +static int xra1403_remove(struct spi_device *spi) > +{ > + struct xra1403 *xra = spi_get_drvdata(spi); > + > + gpiochip_remove(&xra->chip); Use devm_gpiochip_add_data() and this remove is not needed at all. > +static int __init xra1403_init(void) > +{ > + return spi_register_driver(&xra1403_driver); > +} > + > +/* > + * register after spi postcore initcall and before > + * subsys initcalls that may rely on these GPIOs > + */ > +subsys_initcall(xra1403_init); > + > +static void __exit xra1403_exit(void) > +{ > + spi_unregister_driver(&xra1403_driver); > +} > +module_exit(xra1403_exit); This seems like tricksy. Just module_spi_driver() should be fine don't you think? Yours, Linus Walleij