1 // SPDX-License-Identifier: GPL-2.0+
3 * Access to GPOs on TWL6040 chip
5 * Copyright (C) 2012 Texas Instruments, Inc.
8 * Sergio Aguirre <saaguirre@ti.com>
9 * Peter Ujfalusi <peter.ujfalusi@ti.com>
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/kthread.h>
15 #include <linux/irq.h>
16 #include <linux/gpio/driver.h>
17 #include <linux/platform_device.h>
20 #include <linux/mfd/twl6040.h>
22 static int twl6040gpo_get(struct gpio_chip *chip, unsigned offset)
24 struct twl6040 *twl6040 = dev_get_drvdata(chip->parent->parent);
27 ret = twl6040_reg_read(twl6040, TWL6040_REG_GPOCTL);
31 return (ret >> offset) & 1;
34 static int twl6040gpo_direction_out(struct gpio_chip *chip, unsigned offset,
37 /* This only drives GPOs, and can't change direction */
41 static void twl6040gpo_set(struct gpio_chip *chip, unsigned offset, int value)
43 struct twl6040 *twl6040 = dev_get_drvdata(chip->parent->parent);
47 ret = twl6040_reg_read(twl6040, TWL6040_REG_GPOCTL);
52 gpoctl = ret | (1 << offset);
54 gpoctl = ret & ~(1 << offset);
56 twl6040_reg_write(twl6040, TWL6040_REG_GPOCTL, gpoctl);
59 static struct gpio_chip twl6040gpo_chip = {
62 .get = twl6040gpo_get,
63 .direction_output = twl6040gpo_direction_out,
64 .set = twl6040gpo_set,
68 /*----------------------------------------------------------------------*/
70 static int gpo_twl6040_probe(struct platform_device *pdev)
72 struct device *twl6040_core_dev = pdev->dev.parent;
73 struct twl6040 *twl6040 = dev_get_drvdata(twl6040_core_dev);
76 twl6040gpo_chip.base = -1;
78 if (twl6040_get_revid(twl6040) < TWL6041_REV_ES2_0)
79 twl6040gpo_chip.ngpio = 3; /* twl6040 have 3 GPO */
81 twl6040gpo_chip.ngpio = 1; /* twl6041 have 1 GPO */
83 twl6040gpo_chip.parent = &pdev->dev;
85 twl6040gpo_chip.of_node = twl6040_core_dev->of_node;
88 ret = devm_gpiochip_add_data(&pdev->dev, &twl6040gpo_chip, NULL);
90 dev_err(&pdev->dev, "could not register gpiochip, %d\n", ret);
91 twl6040gpo_chip.ngpio = 0;
97 /* Note: this hardware lives inside an I2C-based multi-function device. */
98 MODULE_ALIAS("platform:twl6040-gpo");
100 static struct platform_driver gpo_twl6040_driver = {
102 .name = "twl6040-gpo",
104 .probe = gpo_twl6040_probe,
107 module_platform_driver(gpo_twl6040_driver);
109 MODULE_AUTHOR("Texas Instruments, Inc.");
110 MODULE_DESCRIPTION("GPO interface for TWL6040");
111 MODULE_LICENSE("GPL");