xref: /openbmc/linux/drivers/gpio/gpio-twl6040.c (revision 03ab8e6297acd1bc0eedaa050e2a1635c576fd11)
1b4f53ed9SLinus Walleij // SPDX-License-Identifier: GPL-2.0+
270ffd691SPeter Ujfalusi /*
370ffd691SPeter Ujfalusi  * Access to GPOs on TWL6040 chip
470ffd691SPeter Ujfalusi  *
570ffd691SPeter Ujfalusi  * Copyright (C) 2012 Texas Instruments, Inc.
670ffd691SPeter Ujfalusi  *
770ffd691SPeter Ujfalusi  * Authors:
870ffd691SPeter Ujfalusi  *	Sergio Aguirre <saaguirre@ti.com>
970ffd691SPeter Ujfalusi  *	Peter Ujfalusi <peter.ujfalusi@ti.com>
1070ffd691SPeter Ujfalusi  */
1170ffd691SPeter Ujfalusi 
1270ffd691SPeter Ujfalusi #include <linux/module.h>
1370ffd691SPeter Ujfalusi #include <linux/init.h>
1470ffd691SPeter Ujfalusi #include <linux/kthread.h>
1570ffd691SPeter Ujfalusi #include <linux/irq.h>
16fc4f8f32SLinus Walleij #include <linux/gpio/driver.h>
1770ffd691SPeter Ujfalusi #include <linux/platform_device.h>
184bef8bf2SLinus Walleij #include <linux/bitops.h>
1970ffd691SPeter Ujfalusi #include <linux/of.h>
2070ffd691SPeter Ujfalusi 
2170ffd691SPeter Ujfalusi #include <linux/mfd/twl6040.h>
2270ffd691SPeter Ujfalusi 
twl6040gpo_get(struct gpio_chip * chip,unsigned offset)2370ffd691SPeter Ujfalusi static int twl6040gpo_get(struct gpio_chip *chip, unsigned offset)
2470ffd691SPeter Ujfalusi {
2558383c78SLinus Walleij 	struct twl6040 *twl6040 = dev_get_drvdata(chip->parent->parent);
2670ffd691SPeter Ujfalusi 	int ret = 0;
2770ffd691SPeter Ujfalusi 
2870ffd691SPeter Ujfalusi 	ret = twl6040_reg_read(twl6040, TWL6040_REG_GPOCTL);
2970ffd691SPeter Ujfalusi 	if (ret < 0)
3070ffd691SPeter Ujfalusi 		return ret;
3170ffd691SPeter Ujfalusi 
324bef8bf2SLinus Walleij 	return !!(ret & BIT(offset));
3370ffd691SPeter Ujfalusi }
3470ffd691SPeter Ujfalusi 
twl6040gpo_get_direction(struct gpio_chip * chip,unsigned offset)35ba74bd5dSLinus Walleij static int twl6040gpo_get_direction(struct gpio_chip *chip, unsigned offset)
36ba74bd5dSLinus Walleij {
37e42615ecSMatti Vaittinen 	return GPIO_LINE_DIRECTION_OUT;
38ba74bd5dSLinus Walleij }
39ba74bd5dSLinus Walleij 
twl6040gpo_direction_out(struct gpio_chip * chip,unsigned offset,int value)4070ffd691SPeter Ujfalusi static int twl6040gpo_direction_out(struct gpio_chip *chip, unsigned offset,
4170ffd691SPeter Ujfalusi 				    int value)
4270ffd691SPeter Ujfalusi {
4370ffd691SPeter Ujfalusi 	/* This only drives GPOs, and can't change direction */
4470ffd691SPeter Ujfalusi 	return 0;
4570ffd691SPeter Ujfalusi }
4670ffd691SPeter Ujfalusi 
twl6040gpo_set(struct gpio_chip * chip,unsigned offset,int value)4770ffd691SPeter Ujfalusi static void twl6040gpo_set(struct gpio_chip *chip, unsigned offset, int value)
4870ffd691SPeter Ujfalusi {
4958383c78SLinus Walleij 	struct twl6040 *twl6040 = dev_get_drvdata(chip->parent->parent);
5070ffd691SPeter Ujfalusi 	int ret;
5170ffd691SPeter Ujfalusi 	u8 gpoctl;
5270ffd691SPeter Ujfalusi 
5370ffd691SPeter Ujfalusi 	ret = twl6040_reg_read(twl6040, TWL6040_REG_GPOCTL);
5470ffd691SPeter Ujfalusi 	if (ret < 0)
5570ffd691SPeter Ujfalusi 		return;
5670ffd691SPeter Ujfalusi 
5770ffd691SPeter Ujfalusi 	if (value)
584bef8bf2SLinus Walleij 		gpoctl = ret | BIT(offset);
5970ffd691SPeter Ujfalusi 	else
604bef8bf2SLinus Walleij 		gpoctl = ret & ~BIT(offset);
6170ffd691SPeter Ujfalusi 
6270ffd691SPeter Ujfalusi 	twl6040_reg_write(twl6040, TWL6040_REG_GPOCTL, gpoctl);
6370ffd691SPeter Ujfalusi }
6470ffd691SPeter Ujfalusi 
6570ffd691SPeter Ujfalusi static struct gpio_chip twl6040gpo_chip = {
6670ffd691SPeter Ujfalusi 	.label			= "twl6040",
6770ffd691SPeter Ujfalusi 	.owner			= THIS_MODULE,
6870ffd691SPeter Ujfalusi 	.get			= twl6040gpo_get,
6970ffd691SPeter Ujfalusi 	.direction_output	= twl6040gpo_direction_out,
70ba74bd5dSLinus Walleij 	.get_direction		= twl6040gpo_get_direction,
7170ffd691SPeter Ujfalusi 	.set			= twl6040gpo_set,
729fb1f39eSLinus Walleij 	.can_sleep		= true,
7370ffd691SPeter Ujfalusi };
7470ffd691SPeter Ujfalusi 
7570ffd691SPeter Ujfalusi /*----------------------------------------------------------------------*/
7670ffd691SPeter Ujfalusi 
gpo_twl6040_probe(struct platform_device * pdev)773836309dSBill Pemberton static int gpo_twl6040_probe(struct platform_device *pdev)
7870ffd691SPeter Ujfalusi {
7970ffd691SPeter Ujfalusi 	struct device *twl6040_core_dev = pdev->dev.parent;
8070ffd691SPeter Ujfalusi 	struct twl6040 *twl6040 = dev_get_drvdata(twl6040_core_dev);
8170ffd691SPeter Ujfalusi 	int ret;
8270ffd691SPeter Ujfalusi 
83*6dbe6c07SAndy Shevchenko 	device_set_node(&pdev->dev, dev_fwnode(pdev->dev.parent));
84*6dbe6c07SAndy Shevchenko 
8570ffd691SPeter Ujfalusi 	twl6040gpo_chip.base = -1;
8670ffd691SPeter Ujfalusi 
8770ffd691SPeter Ujfalusi 	if (twl6040_get_revid(twl6040) < TWL6041_REV_ES2_0)
8870ffd691SPeter Ujfalusi 		twl6040gpo_chip.ngpio = 3; /* twl6040 have 3 GPO */
8970ffd691SPeter Ujfalusi 	else
9070ffd691SPeter Ujfalusi 		twl6040gpo_chip.ngpio = 1; /* twl6041 have 1 GPO */
9170ffd691SPeter Ujfalusi 
9258383c78SLinus Walleij 	twl6040gpo_chip.parent = &pdev->dev;
9370ffd691SPeter Ujfalusi 
94fc0292a2SLaxman Dewangan 	ret = devm_gpiochip_add_data(&pdev->dev, &twl6040gpo_chip, NULL);
9570ffd691SPeter Ujfalusi 	if (ret < 0) {
9670ffd691SPeter Ujfalusi 		dev_err(&pdev->dev, "could not register gpiochip, %d\n", ret);
9770ffd691SPeter Ujfalusi 		twl6040gpo_chip.ngpio = 0;
9870ffd691SPeter Ujfalusi 	}
9970ffd691SPeter Ujfalusi 
10070ffd691SPeter Ujfalusi 	return ret;
10170ffd691SPeter Ujfalusi }
10270ffd691SPeter Ujfalusi 
10370ffd691SPeter Ujfalusi /* Note:  this hardware lives inside an I2C-based multi-function device. */
10470ffd691SPeter Ujfalusi MODULE_ALIAS("platform:twl6040-gpo");
10570ffd691SPeter Ujfalusi 
10670ffd691SPeter Ujfalusi static struct platform_driver gpo_twl6040_driver = {
10770ffd691SPeter Ujfalusi 	.driver = {
10870ffd691SPeter Ujfalusi 		.name	= "twl6040-gpo",
10970ffd691SPeter Ujfalusi 	},
11070ffd691SPeter Ujfalusi 	.probe		= gpo_twl6040_probe,
11170ffd691SPeter Ujfalusi };
11270ffd691SPeter Ujfalusi 
11370ffd691SPeter Ujfalusi module_platform_driver(gpo_twl6040_driver);
11470ffd691SPeter Ujfalusi 
11570ffd691SPeter Ujfalusi MODULE_AUTHOR("Texas Instruments, Inc.");
11670ffd691SPeter Ujfalusi MODULE_DESCRIPTION("GPO interface for TWL6040");
11770ffd691SPeter Ujfalusi MODULE_LICENSE("GPL");
118