blob: 454c965ea55595a6b386dadc441e4d3d387868d1 [file] [log] [blame]
Paul Mundtb3c185a2012-06-20 17:29:04 +09001/*
2 * SuperH Pin Function Controller GPIO driver.
3 *
4 * Copyright (C) 2008 Magnus Damm
5 * Copyright (C) 2009 - 2012 Paul Mundt
6 *
7 * This file is subject to the terms and conditions of the GNU General Public
8 * License. See the file "COPYING" in the main directory of this archive
9 * for more details.
10 */
Laurent Pinchartc6193ea2012-12-15 23:50:47 +010011
12#define pr_fmt(fmt) KBUILD_MODNAME " gpio: " fmt
Paul Mundtb3c185a2012-06-20 17:29:04 +090013
Laurent Pinchart1724acf2012-12-15 23:50:48 +010014#include <linux/device.h>
Paul Mundtb3c185a2012-06-20 17:29:04 +090015#include <linux/gpio.h>
Laurent Pinchart90efde22012-12-15 23:50:52 +010016#include <linux/init.h>
Paul Mundtb3c185a2012-06-20 17:29:04 +090017#include <linux/module.h>
Paul Mundtca5481c62012-07-10 12:08:14 +090018#include <linux/pinctrl/consumer.h>
Laurent Pinchart90efde22012-12-15 23:50:52 +010019#include <linux/slab.h>
20#include <linux/spinlock.h>
Paul Mundtb3c185a2012-06-20 17:29:04 +090021
Laurent Pinchartf9165132012-12-15 23:50:44 +010022#include "core.h"
23
Paul Mundtb3c185a2012-06-20 17:29:04 +090024struct sh_pfc_chip {
25 struct sh_pfc *pfc;
26 struct gpio_chip gpio_chip;
27};
28
29static struct sh_pfc_chip *gpio_to_pfc_chip(struct gpio_chip *gc)
30{
31 return container_of(gc, struct sh_pfc_chip, gpio_chip);
32}
33
34static struct sh_pfc *gpio_to_pfc(struct gpio_chip *gc)
35{
36 return gpio_to_pfc_chip(gc)->pfc;
37}
38
Laurent Pinchart16883812012-12-06 14:49:25 +010039/* -----------------------------------------------------------------------------
40 * Pin GPIOs
41 */
42
43static int gpio_pin_request(struct gpio_chip *gc, unsigned offset)
Paul Mundtb3c185a2012-06-20 17:29:04 +090044{
Laurent Pinchart16883812012-12-06 14:49:25 +010045 return pinctrl_request_gpio(offset);
Paul Mundtb3c185a2012-06-20 17:29:04 +090046}
47
Laurent Pinchart16883812012-12-06 14:49:25 +010048static void gpio_pin_free(struct gpio_chip *gc, unsigned offset)
Paul Mundtb3c185a2012-06-20 17:29:04 +090049{
Laurent Pinchart16883812012-12-06 14:49:25 +010050 return pinctrl_free_gpio(offset);
Paul Mundtb3c185a2012-06-20 17:29:04 +090051}
52
Laurent Pinchart16883812012-12-06 14:49:25 +010053static void gpio_pin_set_value(struct sh_pfc *pfc, unsigned offset, int value)
Paul Mundtb3c185a2012-06-20 17:29:04 +090054{
55 struct pinmux_data_reg *dr = NULL;
56 int bit = 0;
57
Laurent Pinchart16883812012-12-06 14:49:25 +010058 if (sh_pfc_get_data_reg(pfc, offset, &dr, &bit) != 0)
Paul Mundtb3c185a2012-06-20 17:29:04 +090059 BUG();
Laurent Pinchart16883812012-12-06 14:49:25 +010060
61 sh_pfc_write_bit(dr, bit, value);
Paul Mundtb3c185a2012-06-20 17:29:04 +090062}
63
Laurent Pinchart16883812012-12-06 14:49:25 +010064static int gpio_pin_direction_input(struct gpio_chip *gc, unsigned offset)
Paul Mundtb3c185a2012-06-20 17:29:04 +090065{
Laurent Pinchart16883812012-12-06 14:49:25 +010066 return pinctrl_gpio_direction_input(offset);
67}
68
69static int gpio_pin_direction_output(struct gpio_chip *gc, unsigned offset,
70 int value)
71{
72 gpio_pin_set_value(gpio_to_pfc(gc), offset, value);
73
74 return pinctrl_gpio_direction_output(offset);
75}
76
77static int gpio_pin_get(struct gpio_chip *gc, unsigned offset)
78{
79 struct sh_pfc *pfc = gpio_to_pfc(gc);
Paul Mundtb3c185a2012-06-20 17:29:04 +090080 struct pinmux_data_reg *dr = NULL;
81 int bit = 0;
82
Laurent Pinchart16883812012-12-06 14:49:25 +010083 if (sh_pfc_get_data_reg(pfc, offset, &dr, &bit) != 0)
Paul Mundtb3c185a2012-06-20 17:29:04 +090084 return -EINVAL;
85
86 return sh_pfc_read_bit(dr, bit);
87}
88
Laurent Pinchart16883812012-12-06 14:49:25 +010089static void gpio_pin_set(struct gpio_chip *gc, unsigned offset, int value)
Paul Mundtca5481c62012-07-10 12:08:14 +090090{
Laurent Pinchart16883812012-12-06 14:49:25 +010091 gpio_pin_set_value(gpio_to_pfc(gc), offset, value);
Paul Mundtca5481c62012-07-10 12:08:14 +090092}
93
Laurent Pinchart16883812012-12-06 14:49:25 +010094static int gpio_pin_to_irq(struct gpio_chip *gc, unsigned offset)
Paul Mundtb3c185a2012-06-20 17:29:04 +090095{
96 struct sh_pfc *pfc = gpio_to_pfc(gc);
Laurent Pinchartc07f54f2013-01-03 14:12:14 +010097 int i, k;
Paul Mundtb3c185a2012-06-20 17:29:04 +090098
Laurent Pinchartc07f54f2013-01-03 14:12:14 +010099 for (i = 0; i < pfc->info->gpio_irq_size; i++) {
100 unsigned short *gpios = pfc->info->gpio_irq[i].gpios;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900101
Laurent Pinchartc07f54f2013-01-03 14:12:14 +0100102 for (k = 0; gpios[k]; k++) {
103 if (gpios[k] == offset)
104 return pfc->info->gpio_irq[i].irq;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900105 }
106 }
107
108 return -ENOSYS;
109}
110
Laurent Pinchart16883812012-12-06 14:49:25 +0100111static void gpio_pin_setup(struct sh_pfc_chip *chip)
Paul Mundtb3c185a2012-06-20 17:29:04 +0900112{
113 struct sh_pfc *pfc = chip->pfc;
114 struct gpio_chip *gc = &chip->gpio_chip;
115
Laurent Pinchart16883812012-12-06 14:49:25 +0100116 gc->request = gpio_pin_request;
117 gc->free = gpio_pin_free;
118 gc->direction_input = gpio_pin_direction_input;
119 gc->get = gpio_pin_get;
120 gc->direction_output = gpio_pin_direction_output;
121 gc->set = gpio_pin_set;
122 gc->to_irq = gpio_pin_to_irq;
123
124 gc->label = pfc->info->name;
125 gc->dev = pfc->dev;
126 gc->owner = THIS_MODULE;
127 gc->base = 0;
128 gc->ngpio = pfc->info->nr_pins;
129}
130
131/* -----------------------------------------------------------------------------
132 * Function GPIOs
133 */
134
135static int gpio_function_request(struct gpio_chip *gc, unsigned offset)
136{
137 struct sh_pfc *pfc = gpio_to_pfc(gc);
138 unsigned int gpio = gc->base + offset;
139 unsigned long flags;
140 int ret = -EINVAL;
141
142 pr_notice_once("Use of GPIO API for function requests is deprecated, convert to pinctrl\n");
143
144 if (pfc->info->func_gpios[offset].enum_id == 0)
145 return ret;
146
147 spin_lock_irqsave(&pfc->lock, flags);
148
149 if (sh_pfc_config_gpio(pfc, gpio, PINMUX_TYPE_FUNCTION,
150 GPIO_CFG_DRYRUN))
151 goto done;
152
153 if (sh_pfc_config_gpio(pfc, gpio, PINMUX_TYPE_FUNCTION,
154 GPIO_CFG_REQ))
155 goto done;
156
157 ret = 0;
158
159done:
160 spin_unlock_irqrestore(&pfc->lock, flags);
161 return ret;
162}
163
164static void gpio_function_free(struct gpio_chip *gc, unsigned offset)
165{
166 struct sh_pfc *pfc = gpio_to_pfc(gc);
167 unsigned int gpio = gc->base + offset;
168 unsigned long flags;
169
170 spin_lock_irqsave(&pfc->lock, flags);
171
172 sh_pfc_config_gpio(pfc, gpio, PINMUX_TYPE_FUNCTION, GPIO_CFG_FREE);
173
174 spin_unlock_irqrestore(&pfc->lock, flags);
175}
176
177static void gpio_function_setup(struct sh_pfc_chip *chip)
178{
179 struct sh_pfc *pfc = chip->pfc;
180 struct gpio_chip *gc = &chip->gpio_chip;
181
182 gc->request = gpio_function_request;
183 gc->free = gpio_function_free;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900184
Laurent Pinchart19bb7fe32012-12-15 23:51:20 +0100185 gc->label = pfc->info->name;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900186 gc->owner = THIS_MODULE;
Laurent Pinchart16883812012-12-06 14:49:25 +0100187 gc->base = pfc->info->nr_pins;
188 gc->ngpio = pfc->info->nr_func_gpios;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900189}
190
Laurent Pinchart16883812012-12-06 14:49:25 +0100191/* -----------------------------------------------------------------------------
192 * Register/unregister
193 */
194
195static struct sh_pfc_chip *
196sh_pfc_add_gpiochip(struct sh_pfc *pfc, void(*setup)(struct sh_pfc_chip *))
Paul Mundtb3c185a2012-06-20 17:29:04 +0900197{
198 struct sh_pfc_chip *chip;
199 int ret;
200
Laurent Pinchart1724acf2012-12-15 23:50:48 +0100201 chip = devm_kzalloc(pfc->dev, sizeof(*chip), GFP_KERNEL);
Paul Mundtb3c185a2012-06-20 17:29:04 +0900202 if (unlikely(!chip))
Laurent Pinchart16883812012-12-06 14:49:25 +0100203 return ERR_PTR(-ENOMEM);
Paul Mundtb3c185a2012-06-20 17:29:04 +0900204
205 chip->pfc = pfc;
206
Laurent Pinchart16883812012-12-06 14:49:25 +0100207 setup(chip);
Paul Mundtb3c185a2012-06-20 17:29:04 +0900208
209 ret = gpiochip_add(&chip->gpio_chip);
Laurent Pinchart1724acf2012-12-15 23:50:48 +0100210 if (unlikely(ret < 0))
Laurent Pinchart16883812012-12-06 14:49:25 +0100211 return ERR_PTR(ret);
212
213 pr_info("%s handling gpio %u -> %u\n",
214 chip->gpio_chip.label, chip->gpio_chip.base,
215 chip->gpio_chip.base + chip->gpio_chip.ngpio - 1);
216
217 return chip;
218}
219
220int sh_pfc_register_gpiochip(struct sh_pfc *pfc)
221{
222 struct sh_pfc_chip *chip;
223
224 chip = sh_pfc_add_gpiochip(pfc, gpio_pin_setup);
225 if (IS_ERR(chip))
226 return PTR_ERR(chip);
Laurent Pinchart6f6a4a62012-12-15 23:50:46 +0100227
228 pfc->gpio = chip;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900229
Laurent Pinchart16883812012-12-06 14:49:25 +0100230 chip = sh_pfc_add_gpiochip(pfc, gpio_function_setup);
231 if (IS_ERR(chip))
232 return PTR_ERR(chip);
233
234 pfc->func = chip;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900235
Paul Mundtb3c185a2012-06-20 17:29:04 +0900236 return 0;
237}
238
Laurent Pinchart6f6a4a62012-12-15 23:50:46 +0100239int sh_pfc_unregister_gpiochip(struct sh_pfc *pfc)
Paul Mundtb3c185a2012-06-20 17:29:04 +0900240{
Laurent Pinchart16883812012-12-06 14:49:25 +0100241 int err;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900242 int ret;
243
Laurent Pinchart16883812012-12-06 14:49:25 +0100244 ret = gpiochip_remove(&pfc->gpio->gpio_chip);
245 err = gpiochip_remove(&pfc->func->gpio_chip);
Paul Mundtb3c185a2012-06-20 17:29:04 +0900246
Laurent Pinchart16883812012-12-06 14:49:25 +0100247 return ret < 0 ? ret : err;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900248}