serial: sh-sci: prevent lockup on full TTY buffers
[pandora-kernel.git] / drivers / gpio / gpio-tps65912.c
1 /*
2  * Copyright 2011 Texas Instruments Inc.
3  *
4  * Author: Margarita Olaya <magi@slimlogic.co.uk>
5  *
6  *  This program is free software; you can redistribute it and/or modify it
7  *  under  the terms of the GNU General  Public License as published by the
8  *  Free Software Foundation;  either version 2 of the License, or (at your
9  *  option) any later version.
10  *
11  * This driver is based on wm8350 implementation.
12  */
13
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/errno.h>
17 #include <linux/gpio.h>
18 #include <linux/mfd/core.h>
19 #include <linux/platform_device.h>
20 #include <linux/seq_file.h>
21 #include <linux/slab.h>
22 #include <linux/mfd/tps65912.h>
23
24 struct tps65912_gpio_data {
25         struct tps65912 *tps65912;
26         struct gpio_chip gpio_chip;
27 };
28
29 #define to_tgd(gc) container_of(gc, struct tps65912_gpio_data, gpio_chip)
30
31 static int tps65912_gpio_get(struct gpio_chip *gc, unsigned offset)
32 {
33         struct tps65912_gpio_data *tps65912_gpio = to_tgd(gc);
34         struct tps65912 *tps65912 = tps65912_gpio->tps65912;
35         int val;
36
37         val = tps65912_reg_read(tps65912, TPS65912_GPIO1 + offset);
38
39         if (val & GPIO_STS_MASK)
40                 return 1;
41
42         return 0;
43 }
44
45 static void tps65912_gpio_set(struct gpio_chip *gc, unsigned offset,
46                               int value)
47 {
48         struct tps65912_gpio_data *tps65912_gpio = to_tgd(gc);
49         struct tps65912 *tps65912 = tps65912_gpio->tps65912;
50
51         if (value)
52                 tps65912_set_bits(tps65912, TPS65912_GPIO1 + offset,
53                                                         GPIO_SET_MASK);
54         else
55                 tps65912_clear_bits(tps65912, TPS65912_GPIO1 + offset,
56                                                                 GPIO_SET_MASK);
57 }
58
59 static int tps65912_gpio_output(struct gpio_chip *gc, unsigned offset,
60                                 int value)
61 {
62         struct tps65912_gpio_data *tps65912_gpio = to_tgd(gc);
63         struct tps65912 *tps65912 = tps65912_gpio->tps65912;
64
65         /* Set the initial value */
66         tps65912_gpio_set(gc, offset, value);
67
68         return tps65912_set_bits(tps65912, TPS65912_GPIO1 + offset,
69                                                                 GPIO_CFG_MASK);
70 }
71
72 static int tps65912_gpio_input(struct gpio_chip *gc, unsigned offset)
73 {
74         struct tps65912_gpio_data *tps65912_gpio = to_tgd(gc);
75         struct tps65912 *tps65912 = tps65912_gpio->tps65912;
76
77         return tps65912_clear_bits(tps65912, TPS65912_GPIO1 + offset,
78                                                                 GPIO_CFG_MASK);
79
80 }
81
82 static struct gpio_chip template_chip = {
83         .label                  = "tps65912",
84         .owner                  = THIS_MODULE,
85         .direction_input        = tps65912_gpio_input,
86         .direction_output       = tps65912_gpio_output,
87         .get                    = tps65912_gpio_get,
88         .set                    = tps65912_gpio_set,
89         .can_sleep              = 1,
90         .ngpio                  = 5,
91         .base                   = -1,
92 };
93
94 static int __devinit tps65912_gpio_probe(struct platform_device *pdev)
95 {
96         struct tps65912 *tps65912 = dev_get_drvdata(pdev->dev.parent);
97         struct tps65912_board *pdata = tps65912->dev->platform_data;
98         struct tps65912_gpio_data *tps65912_gpio;
99         int ret;
100
101         tps65912_gpio = kzalloc(sizeof(*tps65912_gpio), GFP_KERNEL);
102         if (tps65912_gpio == NULL)
103                 return -ENOMEM;
104
105         tps65912_gpio->tps65912 = tps65912;
106         tps65912_gpio->gpio_chip = template_chip;
107         tps65912_gpio->gpio_chip.dev = &pdev->dev;
108         if (pdata && pdata->gpio_base)
109                 tps65912_gpio->gpio_chip.base = pdata->gpio_base;
110
111         ret = gpiochip_add(&tps65912_gpio->gpio_chip);
112         if (ret < 0) {
113                 dev_err(&pdev->dev, "Failed to register gpiochip, %d\n", ret);
114                 goto err;
115         }
116
117         platform_set_drvdata(pdev, tps65912_gpio);
118
119         return ret;
120
121 err:
122         kfree(tps65912_gpio);
123         return ret;
124 }
125
126 static int __devexit tps65912_gpio_remove(struct platform_device *pdev)
127 {
128         struct tps65912_gpio_data  *tps65912_gpio = platform_get_drvdata(pdev);
129         int ret;
130
131         ret = gpiochip_remove(&tps65912_gpio->gpio_chip);
132         if (ret == 0)
133                 kfree(tps65912_gpio);
134
135         return ret;
136 }
137
138 static struct platform_driver tps65912_gpio_driver = {
139         .driver = {
140                 .name = "tps65912-gpio",
141                 .owner = THIS_MODULE,
142         },
143         .probe = tps65912_gpio_probe,
144         .remove = __devexit_p(tps65912_gpio_remove),
145 };
146
147 static int __init tps65912_gpio_init(void)
148 {
149         return platform_driver_register(&tps65912_gpio_driver);
150 }
151 subsys_initcall(tps65912_gpio_init);
152
153 static void __exit tps65912_gpio_exit(void)
154 {
155         platform_driver_unregister(&tps65912_gpio_driver);
156 }
157 module_exit(tps65912_gpio_exit);
158
159 MODULE_AUTHOR("Margarita Olaya Cabrera <magi@slimlogic.co.uk>");
160 MODULE_DESCRIPTION("GPIO interface for TPS65912 PMICs");
161 MODULE_LICENSE("GPL v2");
162 MODULE_ALIAS("platform:tps65912-gpio");