0001
0002
0003
0004
0005
0006
0007
0008
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
0018 #include <linux/mfd/rohm-bd71815.h>
0019
0020 struct bd71815_gpio {
0021
0022 struct gpio_chip chip;
0023
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
0080 static int bd71815gpo_direction_get(struct gpio_chip *gc, unsigned int offset)
0081 {
0082 return GPIO_LINE_DIRECTION_OUT;
0083 }
0084
0085
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
0101
0102
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112
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
0137
0138
0139 dev = &pdev->dev;
0140
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
0151
0152
0153
0154
0155
0156
0157
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");