Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Support to GPOs on ROHM BD71815
0004  * Copyright 2021 ROHM Semiconductors.
0005  * Author: Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>
0006  *
0007  * Copyright 2014 Embest Technology Co. Ltd. Inc.
0008  * Author: yanglsh@embest-tech.com
0009  */
0010 
0011 #include <linux/gpio/driver.h>
0012 #include <linux/init.h>
0013 #include <linux/irq.h>
0014 #include <linux/module.h>
0015 #include <linux/of.h>
0016 #include <linux/platform_device.h>
0017 /* For the BD71815 register definitions */
0018 #include <linux/mfd/rohm-bd71815.h>
0019 
0020 struct bd71815_gpio {
0021     /* chip.parent points the MFD which provides DT node and regmap */
0022     struct gpio_chip chip;
0023     /* dev points to the platform device for devm and prints */
0024     struct device *dev;
0025     struct regmap *regmap;
0026 };
0027 
0028 static int bd71815gpo_get(struct gpio_chip *chip, unsigned int offset)
0029 {
0030     struct bd71815_gpio *bd71815 = gpiochip_get_data(chip);
0031     int ret, val;
0032 
0033     ret = regmap_read(bd71815->regmap, BD71815_REG_GPO, &val);
0034     if (ret)
0035         return ret;
0036 
0037     return (val >> offset) & 1;
0038 }
0039 
0040 static void bd71815gpo_set(struct gpio_chip *chip, unsigned int offset,
0041                int value)
0042 {
0043     struct bd71815_gpio *bd71815 = gpiochip_get_data(chip);
0044     int ret, bit;
0045 
0046     bit = BIT(offset);
0047 
0048     if (value)
0049         ret = regmap_set_bits(bd71815->regmap, BD71815_REG_GPO, bit);
0050     else
0051         ret = regmap_clear_bits(bd71815->regmap, BD71815_REG_GPO, bit);
0052 
0053     if (ret)
0054         dev_warn(bd71815->dev, "failed to toggle GPO\n");
0055 }
0056 
0057 static int bd71815_gpio_set_config(struct gpio_chip *chip, unsigned int offset,
0058                    unsigned long config)
0059 {
0060     struct bd71815_gpio *bdgpio = gpiochip_get_data(chip);
0061 
0062     switch (pinconf_to_config_param(config)) {
0063     case PIN_CONFIG_DRIVE_OPEN_DRAIN:
0064         return regmap_update_bits(bdgpio->regmap,
0065                       BD71815_REG_GPO,
0066                       BD71815_GPIO_DRIVE_MASK << offset,
0067                       BD71815_GPIO_OPEN_DRAIN << offset);
0068     case PIN_CONFIG_DRIVE_PUSH_PULL:
0069         return regmap_update_bits(bdgpio->regmap,
0070                       BD71815_REG_GPO,
0071                       BD71815_GPIO_DRIVE_MASK << offset,
0072                       BD71815_GPIO_CMOS << offset);
0073     default:
0074         break;
0075     }
0076     return -ENOTSUPP;
0077 }
0078 
0079 /* BD71815 GPIO is actually GPO */
0080 static int bd71815gpo_direction_get(struct gpio_chip *gc, unsigned int offset)
0081 {
0082     return GPIO_LINE_DIRECTION_OUT;
0083 }
0084 
0085 /* Template for GPIO chip */
0086 static const struct gpio_chip bd71815gpo_chip = {
0087     .label          = "bd71815",
0088     .owner          = THIS_MODULE,
0089     .get            = bd71815gpo_get,
0090     .get_direction      = bd71815gpo_direction_get,
0091     .set            = bd71815gpo_set,
0092     .set_config     = bd71815_gpio_set_config,
0093     .can_sleep      = true,
0094 };
0095 
0096 #define BD71815_TWO_GPIOS   GENMASK(1, 0)
0097 #define BD71815_ONE_GPIO    BIT(0)
0098 
0099 /*
0100  * Sigh. The BD71815 and BD71817 were originally designed to support two GPO
0101  * pins. At some point it was noticed the second GPO pin which is the E5 pin
0102  * located at the center of IC is hard to use on PCB (due to the location). It
0103  * was decided to not promote this second GPO and the pin is marked as GND in
0104  * the datasheet. The functionality is still there though! I guess driving a GPO
0105  * connected to the ground is a bad idea. Thus we do not support it by default.
0106  * OTOH - the original driver written by colleagues at Embest did support
0107  * controlling this second GPO. It is thus possible this is used in some of the
0108  * products.
0109  *
0110  * This driver does not by default support configuring this second GPO
0111  * but allows using it by providing the DT property
0112  * "rohm,enable-hidden-gpo".
0113  */
0114 static int bd71815_init_valid_mask(struct gpio_chip *gc,
0115                    unsigned long *valid_mask,
0116                    unsigned int ngpios)
0117 {
0118     if (ngpios != 2)
0119         return 0;
0120 
0121     if (gc->parent && device_property_present(gc->parent,
0122                           "rohm,enable-hidden-gpo"))
0123         *valid_mask = BD71815_TWO_GPIOS;
0124     else
0125         *valid_mask = BD71815_ONE_GPIO;
0126 
0127     return 0;
0128 }
0129 
0130 static int gpo_bd71815_probe(struct platform_device *pdev)
0131 {
0132     struct bd71815_gpio *g;
0133     struct device *parent, *dev;
0134 
0135     /*
0136      * Bind devm lifetime to this platform device => use dev for devm.
0137      * also the prints should originate from this device.
0138      */
0139     dev = &pdev->dev;
0140     /* The device-tree and regmap come from MFD => use parent for that */
0141     parent = dev->parent;
0142 
0143     g = devm_kzalloc(dev, sizeof(*g), GFP_KERNEL);
0144     if (!g)
0145         return -ENOMEM;
0146 
0147     g->chip = bd71815gpo_chip;
0148 
0149     /*
0150      * FIXME: As writing of this the sysfs interface for GPIO control does
0151      * not respect the valid_mask. Do not trust it but rather set the ngpios
0152      * to 1 if "rohm,enable-hidden-gpo" is not given.
0153      *
0154      * This check can be removed later if the sysfs export is fixed and
0155      * if the fix is backported.
0156      *
0157      * For now it is safest to just set the ngpios though.
0158      */
0159     if (device_property_present(parent, "rohm,enable-hidden-gpo"))
0160         g->chip.ngpio = 2;
0161     else
0162         g->chip.ngpio = 1;
0163 
0164     g->chip.init_valid_mask = bd71815_init_valid_mask;
0165     g->chip.base = -1;
0166     g->chip.parent = parent;
0167     g->regmap = dev_get_regmap(parent, NULL);
0168     g->dev = dev;
0169 
0170     return devm_gpiochip_add_data(dev, &g->chip, g);
0171 }
0172 
0173 static struct platform_driver gpo_bd71815_driver = {
0174     .driver = {
0175         .name   = "bd71815-gpo",
0176     },
0177     .probe      = gpo_bd71815_probe,
0178 };
0179 module_platform_driver(gpo_bd71815_driver);
0180 
0181 MODULE_ALIAS("platform:bd71815-gpo");
0182 MODULE_AUTHOR("Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>");
0183 MODULE_AUTHOR("Peter Yang <yanglsh@embest-tech.com>");
0184 MODULE_DESCRIPTION("GPO interface for BD71815");
0185 MODULE_LICENSE("GPL");