xref: /openbmc/linux/drivers/gpio/gpio-lp873x.c (revision abd46274)
1*abd46274SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
283f14103SKeerthy /*
383f14103SKeerthy  * Copyright (C) 2016 Texas Instruments Incorporated - http://www.ti.com/
483f14103SKeerthy  *	Keerthy <j-keerthy@ti.com>
583f14103SKeerthy  *
683f14103SKeerthy  * Based on the TPS65218 driver
783f14103SKeerthy  */
883f14103SKeerthy 
9284ead59SLinus Walleij #include <linux/gpio/driver.h>
1083f14103SKeerthy #include <linux/module.h>
1183f14103SKeerthy #include <linux/platform_device.h>
1283f14103SKeerthy #include <linux/regmap.h>
1383f14103SKeerthy 
1483f14103SKeerthy #include <linux/mfd/lp873x.h>
1583f14103SKeerthy 
1683f14103SKeerthy #define BITS_PER_GPO		0x4
1783f14103SKeerthy #define LP873X_GPO_CTRL_OD	0x2
1883f14103SKeerthy 
1983f14103SKeerthy struct lp873x_gpio {
2083f14103SKeerthy 	struct gpio_chip chip;
2183f14103SKeerthy 	struct lp873x *lp873;
2283f14103SKeerthy };
2383f14103SKeerthy 
lp873x_gpio_get_direction(struct gpio_chip * chip,unsigned int offset)2483f14103SKeerthy static int lp873x_gpio_get_direction(struct gpio_chip *chip,
2583f14103SKeerthy 				     unsigned int offset)
2683f14103SKeerthy {
2783f14103SKeerthy 	/* This device is output only */
28e42615ecSMatti Vaittinen 	return GPIO_LINE_DIRECTION_OUT;
2983f14103SKeerthy }
3083f14103SKeerthy 
lp873x_gpio_direction_input(struct gpio_chip * chip,unsigned int offset)3183f14103SKeerthy static int lp873x_gpio_direction_input(struct gpio_chip *chip,
3283f14103SKeerthy 				       unsigned int offset)
3383f14103SKeerthy {
3483f14103SKeerthy 	/* This device is output only */
3583f14103SKeerthy 	return -EINVAL;
3683f14103SKeerthy }
3783f14103SKeerthy 
lp873x_gpio_direction_output(struct gpio_chip * chip,unsigned int offset,int value)3883f14103SKeerthy static int lp873x_gpio_direction_output(struct gpio_chip *chip,
3983f14103SKeerthy 					unsigned int offset, int value)
4083f14103SKeerthy {
4183f14103SKeerthy 	struct lp873x_gpio *gpio = gpiochip_get_data(chip);
4283f14103SKeerthy 
4383f14103SKeerthy 	/* Set the initial value */
4483f14103SKeerthy 	return regmap_update_bits(gpio->lp873->regmap, LP873X_REG_GPO_CTRL,
4583f14103SKeerthy 				  BIT(offset * BITS_PER_GPO),
4683f14103SKeerthy 				  value ? BIT(offset * BITS_PER_GPO) : 0);
4783f14103SKeerthy }
4883f14103SKeerthy 
lp873x_gpio_get(struct gpio_chip * chip,unsigned int offset)4983f14103SKeerthy static int lp873x_gpio_get(struct gpio_chip *chip, unsigned int offset)
5083f14103SKeerthy {
5183f14103SKeerthy 	struct lp873x_gpio *gpio = gpiochip_get_data(chip);
5283f14103SKeerthy 	int ret, val;
5383f14103SKeerthy 
5483f14103SKeerthy 	ret = regmap_read(gpio->lp873->regmap, LP873X_REG_GPO_CTRL, &val);
5583f14103SKeerthy 	if (ret < 0)
5683f14103SKeerthy 		return ret;
5783f14103SKeerthy 
5883f14103SKeerthy 	return val & BIT(offset * BITS_PER_GPO);
5983f14103SKeerthy }
6083f14103SKeerthy 
lp873x_gpio_set(struct gpio_chip * chip,unsigned int offset,int value)6183f14103SKeerthy static void lp873x_gpio_set(struct gpio_chip *chip, unsigned int offset,
6283f14103SKeerthy 			    int value)
6383f14103SKeerthy {
6483f14103SKeerthy 	struct lp873x_gpio *gpio = gpiochip_get_data(chip);
6583f14103SKeerthy 
6683f14103SKeerthy 	regmap_update_bits(gpio->lp873->regmap, LP873X_REG_GPO_CTRL,
6783f14103SKeerthy 			   BIT(offset * BITS_PER_GPO),
6883f14103SKeerthy 			   value ? BIT(offset * BITS_PER_GPO) : 0);
6983f14103SKeerthy }
7083f14103SKeerthy 
lp873x_gpio_request(struct gpio_chip * gc,unsigned int offset)7183f14103SKeerthy static int lp873x_gpio_request(struct gpio_chip *gc, unsigned int offset)
7283f14103SKeerthy {
7383f14103SKeerthy 	struct lp873x_gpio *gpio = gpiochip_get_data(gc);
7483f14103SKeerthy 	int ret;
7583f14103SKeerthy 
7683f14103SKeerthy 	switch (offset) {
7783f14103SKeerthy 	case 0:
7883f14103SKeerthy 		/* No MUX Set up Needed for GPO */
7983f14103SKeerthy 		break;
8083f14103SKeerthy 	case 1:
8183f14103SKeerthy 		/* Setup the CLKIN_PIN_SEL MUX to GPO2 */
8283f14103SKeerthy 		ret = regmap_update_bits(gpio->lp873->regmap, LP873X_REG_CONFIG,
8383f14103SKeerthy 					 LP873X_CONFIG_CLKIN_PIN_SEL, 0);
8483f14103SKeerthy 		if (ret)
8583f14103SKeerthy 			return ret;
8683f14103SKeerthy 
8783f14103SKeerthy 		break;
8883f14103SKeerthy 	default:
8983f14103SKeerthy 		return -EINVAL;
9083f14103SKeerthy 	}
9183f14103SKeerthy 
9283f14103SKeerthy 	return 0;
9383f14103SKeerthy }
9483f14103SKeerthy 
lp873x_gpio_set_config(struct gpio_chip * gc,unsigned offset,unsigned long config)952956b5d9SMika Westerberg static int lp873x_gpio_set_config(struct gpio_chip *gc, unsigned offset,
962956b5d9SMika Westerberg 				  unsigned long config)
9783f14103SKeerthy {
9883f14103SKeerthy 	struct lp873x_gpio *gpio = gpiochip_get_data(gc);
9983f14103SKeerthy 
1002956b5d9SMika Westerberg 	switch (pinconf_to_config_param(config)) {
1012956b5d9SMika Westerberg 	case PIN_CONFIG_DRIVE_OPEN_DRAIN:
10283f14103SKeerthy 		return regmap_update_bits(gpio->lp873->regmap,
10383f14103SKeerthy 					  LP873X_REG_GPO_CTRL,
10483f14103SKeerthy 					  BIT(offset * BITS_PER_GPO +
10583f14103SKeerthy 					  LP873X_GPO_CTRL_OD),
10683f14103SKeerthy 					  BIT(offset * BITS_PER_GPO +
10783f14103SKeerthy 					  LP873X_GPO_CTRL_OD));
1082956b5d9SMika Westerberg 
1092956b5d9SMika Westerberg 	case PIN_CONFIG_DRIVE_PUSH_PULL:
11083f14103SKeerthy 		return regmap_update_bits(gpio->lp873->regmap,
11183f14103SKeerthy 					  LP873X_REG_GPO_CTRL,
11283f14103SKeerthy 					  BIT(offset * BITS_PER_GPO +
11383f14103SKeerthy 					  LP873X_GPO_CTRL_OD), 0);
11483f14103SKeerthy 	default:
11583f14103SKeerthy 		return -ENOTSUPP;
11683f14103SKeerthy 	}
11783f14103SKeerthy }
11883f14103SKeerthy 
119e35b5ab0SJulia Lawall static const struct gpio_chip template_chip = {
12083f14103SKeerthy 	.label			= "lp873x-gpio",
12183f14103SKeerthy 	.owner			= THIS_MODULE,
12283f14103SKeerthy 	.request		= lp873x_gpio_request,
12383f14103SKeerthy 	.get_direction		= lp873x_gpio_get_direction,
12483f14103SKeerthy 	.direction_input	= lp873x_gpio_direction_input,
12583f14103SKeerthy 	.direction_output	= lp873x_gpio_direction_output,
12683f14103SKeerthy 	.get			= lp873x_gpio_get,
12783f14103SKeerthy 	.set			= lp873x_gpio_set,
1282956b5d9SMika Westerberg 	.set_config		= lp873x_gpio_set_config,
12983f14103SKeerthy 	.base			= -1,
13083f14103SKeerthy 	.ngpio			= 2,
13183f14103SKeerthy 	.can_sleep		= true,
13283f14103SKeerthy };
13383f14103SKeerthy 
lp873x_gpio_probe(struct platform_device * pdev)13483f14103SKeerthy static int lp873x_gpio_probe(struct platform_device *pdev)
13583f14103SKeerthy {
13683f14103SKeerthy 	struct lp873x_gpio *gpio;
13783f14103SKeerthy 	int ret;
13883f14103SKeerthy 
13983f14103SKeerthy 	gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
14083f14103SKeerthy 	if (!gpio)
14183f14103SKeerthy 		return -ENOMEM;
14283f14103SKeerthy 
14383f14103SKeerthy 	platform_set_drvdata(pdev, gpio);
14483f14103SKeerthy 
14583f14103SKeerthy 	gpio->lp873 = dev_get_drvdata(pdev->dev.parent);
14683f14103SKeerthy 	gpio->chip = template_chip;
14783f14103SKeerthy 	gpio->chip.parent = gpio->lp873->dev;
14883f14103SKeerthy 
1499d99c41aSWei Yongjun 	ret = devm_gpiochip_add_data(&pdev->dev, &gpio->chip, gpio);
15083f14103SKeerthy 	if (ret < 0) {
15183f14103SKeerthy 		dev_err(&pdev->dev, "Could not register gpiochip, %d\n", ret);
15283f14103SKeerthy 		return ret;
15383f14103SKeerthy 	}
15483f14103SKeerthy 
15583f14103SKeerthy 	return 0;
15683f14103SKeerthy }
15783f14103SKeerthy 
15883f14103SKeerthy static const struct platform_device_id lp873x_gpio_id_table[] = {
15983f14103SKeerthy 	{ "lp873x-gpio", },
16083f14103SKeerthy 	{ /* sentinel */ }
16183f14103SKeerthy };
16283f14103SKeerthy MODULE_DEVICE_TABLE(platform, lp873x_gpio_id_table);
16383f14103SKeerthy 
16483f14103SKeerthy static struct platform_driver lp873x_gpio_driver = {
16583f14103SKeerthy 	.driver = {
16683f14103SKeerthy 		.name = "lp873x-gpio",
16783f14103SKeerthy 	},
16883f14103SKeerthy 	.probe = lp873x_gpio_probe,
16983f14103SKeerthy 	.id_table = lp873x_gpio_id_table,
17083f14103SKeerthy };
17183f14103SKeerthy module_platform_driver(lp873x_gpio_driver);
17283f14103SKeerthy 
17383f14103SKeerthy MODULE_AUTHOR("Keerthy <j-keerthy@ti.com>");
17483f14103SKeerthy MODULE_DESCRIPTION("LP873X GPIO driver");
17583f14103SKeerthy MODULE_LICENSE("GPL v2");
176