xref: /openbmc/linux/drivers/gpio/gpio-rc5t583.c (revision 8c749ce9)
1 /*
2  * GPIO driver for RICOH583 power management chip.
3  *
4  * Copyright (c) 2012, NVIDIA CORPORATION.  All rights reserved.
5  * Author: Laxman dewangan <ldewangan@nvidia.com>
6  *
7  * Based on code
8  *	Copyright (C) 2011 RICOH COMPANY,LTD
9  *
10  * This program is free software; you can redistribute it and/or modify it
11  * under the terms and conditions of the GNU General Public License,
12  * version 2, as published by the Free Software Foundation.
13  *
14  * This program is distributed in the hope it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
17  * more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  */
23 #include <linux/init.h>
24 #include <linux/kernel.h>
25 #include <linux/slab.h>
26 #include <linux/module.h>
27 #include <linux/platform_device.h>
28 #include <linux/device.h>
29 #include <linux/gpio.h>
30 #include <linux/mfd/rc5t583.h>
31 
32 struct rc5t583_gpio {
33 	struct gpio_chip gpio_chip;
34 	struct rc5t583 *rc5t583;
35 };
36 
37 static int rc5t583_gpio_get(struct gpio_chip *gc, unsigned int offset)
38 {
39 	struct rc5t583_gpio *rc5t583_gpio = gpiochip_get_data(gc);
40 	struct device *parent = rc5t583_gpio->rc5t583->dev;
41 	uint8_t val = 0;
42 	int ret;
43 
44 	ret = rc5t583_read(parent, RC5T583_GPIO_MON_IOIN, &val);
45 	if (ret < 0)
46 		return ret;
47 
48 	return !!(val & BIT(offset));
49 }
50 
51 static void rc5t583_gpio_set(struct gpio_chip *gc, unsigned int offset, int val)
52 {
53 	struct rc5t583_gpio *rc5t583_gpio = gpiochip_get_data(gc);
54 	struct device *parent = rc5t583_gpio->rc5t583->dev;
55 	if (val)
56 		rc5t583_set_bits(parent, RC5T583_GPIO_IOOUT, BIT(offset));
57 	else
58 		rc5t583_clear_bits(parent, RC5T583_GPIO_IOOUT, BIT(offset));
59 }
60 
61 static int rc5t583_gpio_dir_input(struct gpio_chip *gc, unsigned int offset)
62 {
63 	struct rc5t583_gpio *rc5t583_gpio = gpiochip_get_data(gc);
64 	struct device *parent = rc5t583_gpio->rc5t583->dev;
65 	int ret;
66 
67 	ret = rc5t583_clear_bits(parent, RC5T583_GPIO_IOSEL, BIT(offset));
68 	if (ret < 0)
69 		return ret;
70 
71 	/* Set pin to gpio mode */
72 	return rc5t583_clear_bits(parent, RC5T583_GPIO_PGSEL, BIT(offset));
73 }
74 
75 static int rc5t583_gpio_dir_output(struct gpio_chip *gc, unsigned offset,
76 			int value)
77 {
78 	struct rc5t583_gpio *rc5t583_gpio = gpiochip_get_data(gc);
79 	struct device *parent = rc5t583_gpio->rc5t583->dev;
80 	int ret;
81 
82 	rc5t583_gpio_set(gc, offset, value);
83 	ret = rc5t583_set_bits(parent, RC5T583_GPIO_IOSEL, BIT(offset));
84 	if (ret < 0)
85 		return ret;
86 
87 	/* Set pin to gpio mode */
88 	return rc5t583_clear_bits(parent, RC5T583_GPIO_PGSEL, BIT(offset));
89 }
90 
91 static int rc5t583_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
92 {
93 	struct rc5t583_gpio *rc5t583_gpio = gpiochip_get_data(gc);
94 
95 	if (offset < RC5T583_MAX_GPIO)
96 		return rc5t583_gpio->rc5t583->irq_base +
97 				RC5T583_IRQ_GPIO0 + offset;
98 	return -EINVAL;
99 }
100 
101 static void rc5t583_gpio_free(struct gpio_chip *gc, unsigned offset)
102 {
103 	struct rc5t583_gpio *rc5t583_gpio = gpiochip_get_data(gc);
104 	struct device *parent = rc5t583_gpio->rc5t583->dev;
105 
106 	rc5t583_set_bits(parent, RC5T583_GPIO_PGSEL, BIT(offset));
107 }
108 
109 static int rc5t583_gpio_probe(struct platform_device *pdev)
110 {
111 	struct rc5t583 *rc5t583 = dev_get_drvdata(pdev->dev.parent);
112 	struct rc5t583_platform_data *pdata = dev_get_platdata(rc5t583->dev);
113 	struct rc5t583_gpio *rc5t583_gpio;
114 
115 	rc5t583_gpio = devm_kzalloc(&pdev->dev, sizeof(*rc5t583_gpio),
116 					GFP_KERNEL);
117 	if (!rc5t583_gpio)
118 		return -ENOMEM;
119 
120 	rc5t583_gpio->gpio_chip.label = "gpio-rc5t583",
121 	rc5t583_gpio->gpio_chip.owner = THIS_MODULE,
122 	rc5t583_gpio->gpio_chip.free = rc5t583_gpio_free,
123 	rc5t583_gpio->gpio_chip.direction_input = rc5t583_gpio_dir_input,
124 	rc5t583_gpio->gpio_chip.direction_output = rc5t583_gpio_dir_output,
125 	rc5t583_gpio->gpio_chip.set = rc5t583_gpio_set,
126 	rc5t583_gpio->gpio_chip.get = rc5t583_gpio_get,
127 	rc5t583_gpio->gpio_chip.to_irq = rc5t583_gpio_to_irq,
128 	rc5t583_gpio->gpio_chip.ngpio = RC5T583_MAX_GPIO,
129 	rc5t583_gpio->gpio_chip.can_sleep = true,
130 	rc5t583_gpio->gpio_chip.parent = &pdev->dev;
131 	rc5t583_gpio->gpio_chip.base = -1;
132 	rc5t583_gpio->rc5t583 = rc5t583;
133 
134 	if (pdata && pdata->gpio_base)
135 		rc5t583_gpio->gpio_chip.base = pdata->gpio_base;
136 
137 	platform_set_drvdata(pdev, rc5t583_gpio);
138 
139 	return gpiochip_add_data(&rc5t583_gpio->gpio_chip, rc5t583_gpio);
140 }
141 
142 static int rc5t583_gpio_remove(struct platform_device *pdev)
143 {
144 	struct rc5t583_gpio *rc5t583_gpio = platform_get_drvdata(pdev);
145 
146 	gpiochip_remove(&rc5t583_gpio->gpio_chip);
147 	return 0;
148 }
149 
150 static struct platform_driver rc5t583_gpio_driver = {
151 	.driver = {
152 		.name    = "rc5t583-gpio",
153 	},
154 	.probe		= rc5t583_gpio_probe,
155 	.remove		= rc5t583_gpio_remove,
156 };
157 
158 static int __init rc5t583_gpio_init(void)
159 {
160 	return platform_driver_register(&rc5t583_gpio_driver);
161 }
162 subsys_initcall(rc5t583_gpio_init);
163 
164 static void __exit rc5t583_gpio_exit(void)
165 {
166 	platform_driver_unregister(&rc5t583_gpio_driver);
167 }
168 module_exit(rc5t583_gpio_exit);
169 
170 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
171 MODULE_DESCRIPTION("GPIO interface for RC5T583");
172 MODULE_LICENSE("GPL v2");
173 MODULE_ALIAS("platform:rc5t583-gpio");
174