blob: 562b0c4d9cc888a01af998e48c337ace1a23ce4e [file] [log] [blame]
Laxman Dewangane9fe32b2012-05-14 12:46:12 +05301/*
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
32struct rc5t583_gpio {
33 struct gpio_chip gpio_chip;
34 struct rc5t583 *rc5t583;
35};
36
37static inline struct rc5t583_gpio *to_rc5t583_gpio(struct gpio_chip *chip)
38{
39 return container_of(chip, struct rc5t583_gpio, gpio_chip);
40}
41
42static int rc5t583_gpio_get(struct gpio_chip *gc, unsigned int offset)
43{
44 struct rc5t583_gpio *rc5t583_gpio = to_rc5t583_gpio(gc);
45 struct device *parent = rc5t583_gpio->rc5t583->dev;
46 uint8_t val = 0;
47 int ret;
48
49 ret = rc5t583_read(parent, RC5T583_GPIO_MON_IOIN, &val);
50 if (ret < 0)
51 return ret;
52
53 return !!(val & BIT(offset));
54}
55
56static void rc5t583_gpio_set(struct gpio_chip *gc, unsigned int offset, int val)
57{
58 struct rc5t583_gpio *rc5t583_gpio = to_rc5t583_gpio(gc);
59 struct device *parent = rc5t583_gpio->rc5t583->dev;
60 if (val)
61 rc5t583_set_bits(parent, RC5T583_GPIO_IOOUT, BIT(offset));
62 else
63 rc5t583_clear_bits(parent, RC5T583_GPIO_IOOUT, BIT(offset));
64}
65
66static int rc5t583_gpio_dir_input(struct gpio_chip *gc, unsigned int offset)
67{
68 struct rc5t583_gpio *rc5t583_gpio = to_rc5t583_gpio(gc);
69 struct device *parent = rc5t583_gpio->rc5t583->dev;
70 int ret;
71
72 ret = rc5t583_clear_bits(parent, RC5T583_GPIO_IOSEL, BIT(offset));
73 if (ret < 0)
74 return ret;
75
76 /* Set pin to gpio mode */
77 return rc5t583_clear_bits(parent, RC5T583_GPIO_PGSEL, BIT(offset));
78}
79
80static int rc5t583_gpio_dir_output(struct gpio_chip *gc, unsigned offset,
81 int value)
82{
83 struct rc5t583_gpio *rc5t583_gpio = to_rc5t583_gpio(gc);
84 struct device *parent = rc5t583_gpio->rc5t583->dev;
85 int ret;
86
87 rc5t583_gpio_set(gc, offset, value);
88 ret = rc5t583_set_bits(parent, RC5T583_GPIO_IOSEL, BIT(offset));
89 if (ret < 0)
90 return ret;
91
92 /* Set pin to gpio mode */
93 return rc5t583_clear_bits(parent, RC5T583_GPIO_PGSEL, BIT(offset));
94}
95
96static int rc5t583_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
97{
98 struct rc5t583_gpio *rc5t583_gpio = to_rc5t583_gpio(gc);
99
Alexander Shiyan3bde4d22014-02-15 17:24:07 +0400100 if (offset < RC5T583_MAX_GPIO)
Laxman Dewangane9fe32b2012-05-14 12:46:12 +0530101 return rc5t583_gpio->rc5t583->irq_base +
102 RC5T583_IRQ_GPIO0 + offset;
103 return -EINVAL;
104}
105
106static void rc5t583_gpio_free(struct gpio_chip *gc, unsigned offset)
107{
108 struct rc5t583_gpio *rc5t583_gpio = to_rc5t583_gpio(gc);
109 struct device *parent = rc5t583_gpio->rc5t583->dev;
110
111 rc5t583_set_bits(parent, RC5T583_GPIO_PGSEL, BIT(offset));
112}
113
Bill Pemberton38363092012-11-19 13:22:34 -0500114static int rc5t583_gpio_probe(struct platform_device *pdev)
Laxman Dewangane9fe32b2012-05-14 12:46:12 +0530115{
116 struct rc5t583 *rc5t583 = dev_get_drvdata(pdev->dev.parent);
117 struct rc5t583_platform_data *pdata = dev_get_platdata(rc5t583->dev);
118 struct rc5t583_gpio *rc5t583_gpio;
119
120 rc5t583_gpio = devm_kzalloc(&pdev->dev, sizeof(*rc5t583_gpio),
121 GFP_KERNEL);
Jingoo Han4b7dfd72014-04-29 17:39:42 +0900122 if (!rc5t583_gpio)
Laxman Dewangane9fe32b2012-05-14 12:46:12 +0530123 return -ENOMEM;
Laxman Dewangane9fe32b2012-05-14 12:46:12 +0530124
125 rc5t583_gpio->gpio_chip.label = "gpio-rc5t583",
126 rc5t583_gpio->gpio_chip.owner = THIS_MODULE,
127 rc5t583_gpio->gpio_chip.free = rc5t583_gpio_free,
128 rc5t583_gpio->gpio_chip.direction_input = rc5t583_gpio_dir_input,
129 rc5t583_gpio->gpio_chip.direction_output = rc5t583_gpio_dir_output,
130 rc5t583_gpio->gpio_chip.set = rc5t583_gpio_set,
131 rc5t583_gpio->gpio_chip.get = rc5t583_gpio_get,
132 rc5t583_gpio->gpio_chip.to_irq = rc5t583_gpio_to_irq,
133 rc5t583_gpio->gpio_chip.ngpio = RC5T583_MAX_GPIO,
Linus Walleij9fb1f392013-12-04 14:42:46 +0100134 rc5t583_gpio->gpio_chip.can_sleep = true,
Laxman Dewangane9fe32b2012-05-14 12:46:12 +0530135 rc5t583_gpio->gpio_chip.dev = &pdev->dev;
136 rc5t583_gpio->gpio_chip.base = -1;
137 rc5t583_gpio->rc5t583 = rc5t583;
138
139 if (pdata && pdata->gpio_base)
140 rc5t583_gpio->gpio_chip.base = pdata->gpio_base;
141
142 platform_set_drvdata(pdev, rc5t583_gpio);
143
144 return gpiochip_add(&rc5t583_gpio->gpio_chip);
145}
146
Bill Pemberton206210c2012-11-19 13:25:50 -0500147static int rc5t583_gpio_remove(struct platform_device *pdev)
Laxman Dewangane9fe32b2012-05-14 12:46:12 +0530148{
149 struct rc5t583_gpio *rc5t583_gpio = platform_get_drvdata(pdev);
150
151 return gpiochip_remove(&rc5t583_gpio->gpio_chip);
152}
153
154static struct platform_driver rc5t583_gpio_driver = {
155 .driver = {
156 .name = "rc5t583-gpio",
157 .owner = THIS_MODULE,
158 },
159 .probe = rc5t583_gpio_probe,
Bill Pemberton8283c4f2012-11-19 13:20:08 -0500160 .remove = rc5t583_gpio_remove,
Laxman Dewangane9fe32b2012-05-14 12:46:12 +0530161};
162
163static int __init rc5t583_gpio_init(void)
164{
165 return platform_driver_register(&rc5t583_gpio_driver);
166}
167subsys_initcall(rc5t583_gpio_init);
168
169static void __exit rc5t583_gpio_exit(void)
170{
171 platform_driver_unregister(&rc5t583_gpio_driver);
172}
173module_exit(rc5t583_gpio_exit);
174
175MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
176MODULE_DESCRIPTION("GPIO interface for RC5T583");
177MODULE_LICENSE("GPL v2");
178MODULE_ALIAS("platform:rc5t583-gpio");