0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #include <linux/module.h>
0013 #include <linux/init.h>
0014 #include <linux/kthread.h>
0015 #include <linux/irq.h>
0016 #include <linux/gpio/driver.h>
0017 #include <linux/platform_device.h>
0018 #include <linux/bitops.h>
0019 #include <linux/of.h>
0020
0021 #include <linux/mfd/twl6040.h>
0022
0023 static int twl6040gpo_get(struct gpio_chip *chip, unsigned offset)
0024 {
0025 struct twl6040 *twl6040 = dev_get_drvdata(chip->parent->parent);
0026 int ret = 0;
0027
0028 ret = twl6040_reg_read(twl6040, TWL6040_REG_GPOCTL);
0029 if (ret < 0)
0030 return ret;
0031
0032 return !!(ret & BIT(offset));
0033 }
0034
0035 static int twl6040gpo_get_direction(struct gpio_chip *chip, unsigned offset)
0036 {
0037 return GPIO_LINE_DIRECTION_OUT;
0038 }
0039
0040 static int twl6040gpo_direction_out(struct gpio_chip *chip, unsigned offset,
0041 int value)
0042 {
0043
0044 return 0;
0045 }
0046
0047 static void twl6040gpo_set(struct gpio_chip *chip, unsigned offset, int value)
0048 {
0049 struct twl6040 *twl6040 = dev_get_drvdata(chip->parent->parent);
0050 int ret;
0051 u8 gpoctl;
0052
0053 ret = twl6040_reg_read(twl6040, TWL6040_REG_GPOCTL);
0054 if (ret < 0)
0055 return;
0056
0057 if (value)
0058 gpoctl = ret | BIT(offset);
0059 else
0060 gpoctl = ret & ~BIT(offset);
0061
0062 twl6040_reg_write(twl6040, TWL6040_REG_GPOCTL, gpoctl);
0063 }
0064
0065 static struct gpio_chip twl6040gpo_chip = {
0066 .label = "twl6040",
0067 .owner = THIS_MODULE,
0068 .get = twl6040gpo_get,
0069 .direction_output = twl6040gpo_direction_out,
0070 .get_direction = twl6040gpo_get_direction,
0071 .set = twl6040gpo_set,
0072 .can_sleep = true,
0073 };
0074
0075
0076
0077 static int gpo_twl6040_probe(struct platform_device *pdev)
0078 {
0079 struct device *twl6040_core_dev = pdev->dev.parent;
0080 struct twl6040 *twl6040 = dev_get_drvdata(twl6040_core_dev);
0081 int ret;
0082
0083 device_set_node(&pdev->dev, dev_fwnode(pdev->dev.parent));
0084
0085 twl6040gpo_chip.base = -1;
0086
0087 if (twl6040_get_revid(twl6040) < TWL6041_REV_ES2_0)
0088 twl6040gpo_chip.ngpio = 3;
0089 else
0090 twl6040gpo_chip.ngpio = 1;
0091
0092 twl6040gpo_chip.parent = &pdev->dev;
0093
0094 ret = devm_gpiochip_add_data(&pdev->dev, &twl6040gpo_chip, NULL);
0095 if (ret < 0) {
0096 dev_err(&pdev->dev, "could not register gpiochip, %d\n", ret);
0097 twl6040gpo_chip.ngpio = 0;
0098 }
0099
0100 return ret;
0101 }
0102
0103
0104 MODULE_ALIAS("platform:twl6040-gpo");
0105
0106 static struct platform_driver gpo_twl6040_driver = {
0107 .driver = {
0108 .name = "twl6040-gpo",
0109 },
0110 .probe = gpo_twl6040_probe,
0111 };
0112
0113 module_platform_driver(gpo_twl6040_driver);
0114
0115 MODULE_AUTHOR("Texas Instruments, Inc.");
0116 MODULE_DESCRIPTION("GPO interface for TWL6040");
0117 MODULE_LICENSE("GPL");