c0c18c80abbd856cdca81fc2bf94afa9c5a67a55
[linux-2.6-block.git] / drivers / gpio / gpio-twl6040.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Access to GPOs on TWL6040 chip
4  *
5  * Copyright (C) 2012 Texas Instruments, Inc.
6  *
7  * Authors:
8  *      Sergio Aguirre <saaguirre@ti.com>
9  *      Peter Ujfalusi <peter.ujfalusi@ti.com>
10  */
11
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>
18 #include <linux/of.h>
19
20 #include <linux/mfd/twl6040.h>
21
22 static int twl6040gpo_get(struct gpio_chip *chip, unsigned offset)
23 {
24         struct twl6040 *twl6040 = dev_get_drvdata(chip->parent->parent);
25         int ret = 0;
26
27         ret = twl6040_reg_read(twl6040, TWL6040_REG_GPOCTL);
28         if (ret < 0)
29                 return ret;
30
31         return (ret >> offset) & 1;
32 }
33
34 static int twl6040gpo_direction_out(struct gpio_chip *chip, unsigned offset,
35                                     int value)
36 {
37         /* This only drives GPOs, and can't change direction */
38         return 0;
39 }
40
41 static void twl6040gpo_set(struct gpio_chip *chip, unsigned offset, int value)
42 {
43         struct twl6040 *twl6040 = dev_get_drvdata(chip->parent->parent);
44         int ret;
45         u8 gpoctl;
46
47         ret = twl6040_reg_read(twl6040, TWL6040_REG_GPOCTL);
48         if (ret < 0)
49                 return;
50
51         if (value)
52                 gpoctl = ret | (1 << offset);
53         else
54                 gpoctl = ret & ~(1 << offset);
55
56         twl6040_reg_write(twl6040, TWL6040_REG_GPOCTL, gpoctl);
57 }
58
59 static struct gpio_chip twl6040gpo_chip = {
60         .label                  = "twl6040",
61         .owner                  = THIS_MODULE,
62         .get                    = twl6040gpo_get,
63         .direction_output       = twl6040gpo_direction_out,
64         .set                    = twl6040gpo_set,
65         .can_sleep              = true,
66 };
67
68 /*----------------------------------------------------------------------*/
69
70 static int gpo_twl6040_probe(struct platform_device *pdev)
71 {
72         struct device *twl6040_core_dev = pdev->dev.parent;
73         struct twl6040 *twl6040 = dev_get_drvdata(twl6040_core_dev);
74         int ret;
75
76         twl6040gpo_chip.base = -1;
77
78         if (twl6040_get_revid(twl6040) < TWL6041_REV_ES2_0)
79                 twl6040gpo_chip.ngpio = 3; /* twl6040 have 3 GPO */
80         else
81                 twl6040gpo_chip.ngpio = 1; /* twl6041 have 1 GPO */
82
83         twl6040gpo_chip.parent = &pdev->dev;
84 #ifdef CONFIG_OF_GPIO
85         twl6040gpo_chip.of_node = twl6040_core_dev->of_node;
86 #endif
87
88         ret = devm_gpiochip_add_data(&pdev->dev, &twl6040gpo_chip, NULL);
89         if (ret < 0) {
90                 dev_err(&pdev->dev, "could not register gpiochip, %d\n", ret);
91                 twl6040gpo_chip.ngpio = 0;
92         }
93
94         return ret;
95 }
96
97 /* Note:  this hardware lives inside an I2C-based multi-function device. */
98 MODULE_ALIAS("platform:twl6040-gpo");
99
100 static struct platform_driver gpo_twl6040_driver = {
101         .driver = {
102                 .name   = "twl6040-gpo",
103         },
104         .probe          = gpo_twl6040_probe,
105 };
106
107 module_platform_driver(gpo_twl6040_driver);
108
109 MODULE_AUTHOR("Texas Instruments, Inc.");
110 MODULE_DESCRIPTION("GPO interface for TWL6040");
111 MODULE_LICENSE("GPL");