blob: db9af4e548a017d6404e62539530c60c8e89ba61 [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 Pinchart0b73ee52013-02-14 22:12:11 +010045 struct sh_pfc *pfc = gpio_to_pfc(gc);
46
47 if (pfc->info->pins[offset].enum_id == 0)
48 return -EINVAL;
49
Laurent Pinchart16883812012-12-06 14:49:25 +010050 return pinctrl_request_gpio(offset);
Paul Mundtb3c185a2012-06-20 17:29:04 +090051}
52
Laurent Pinchart16883812012-12-06 14:49:25 +010053static void gpio_pin_free(struct gpio_chip *gc, unsigned offset)
Paul Mundtb3c185a2012-06-20 17:29:04 +090054{
Laurent Pinchart16883812012-12-06 14:49:25 +010055 return pinctrl_free_gpio(offset);
Paul Mundtb3c185a2012-06-20 17:29:04 +090056}
57
Laurent Pinchart16883812012-12-06 14:49:25 +010058static void gpio_pin_set_value(struct sh_pfc *pfc, unsigned offset, int value)
Paul Mundtb3c185a2012-06-20 17:29:04 +090059{
Laurent Pinchart0b73ee52013-02-14 22:12:11 +010060 struct pinmux_data_reg *dr;
61 int bit;
Paul Mundtb3c185a2012-06-20 17:29:04 +090062
Laurent Pinchart0b73ee52013-02-14 22:12:11 +010063 sh_pfc_get_data_reg(pfc, offset, &dr, &bit);
Laurent Pinchart16883812012-12-06 14:49:25 +010064 sh_pfc_write_bit(dr, bit, value);
Paul Mundtb3c185a2012-06-20 17:29:04 +090065}
66
Laurent Pinchart16883812012-12-06 14:49:25 +010067static int gpio_pin_direction_input(struct gpio_chip *gc, unsigned offset)
Paul Mundtb3c185a2012-06-20 17:29:04 +090068{
Laurent Pinchart16883812012-12-06 14:49:25 +010069 return pinctrl_gpio_direction_input(offset);
70}
71
72static int gpio_pin_direction_output(struct gpio_chip *gc, unsigned offset,
73 int value)
74{
75 gpio_pin_set_value(gpio_to_pfc(gc), offset, value);
76
77 return pinctrl_gpio_direction_output(offset);
78}
79
80static int gpio_pin_get(struct gpio_chip *gc, unsigned offset)
81{
82 struct sh_pfc *pfc = gpio_to_pfc(gc);
Laurent Pinchart0b73ee52013-02-14 22:12:11 +010083 struct pinmux_data_reg *dr;
84 int bit;
Paul Mundtb3c185a2012-06-20 17:29:04 +090085
Laurent Pinchart0b73ee52013-02-14 22:12:11 +010086 sh_pfc_get_data_reg(pfc, offset, &dr, &bit);
Paul Mundtb3c185a2012-06-20 17:29:04 +090087 return sh_pfc_read_bit(dr, bit);
88}
89
Laurent Pinchart16883812012-12-06 14:49:25 +010090static void gpio_pin_set(struct gpio_chip *gc, unsigned offset, int value)
Paul Mundtca5481c62012-07-10 12:08:14 +090091{
Laurent Pinchart16883812012-12-06 14:49:25 +010092 gpio_pin_set_value(gpio_to_pfc(gc), offset, value);
Paul Mundtca5481c62012-07-10 12:08:14 +090093}
94
Laurent Pinchart16883812012-12-06 14:49:25 +010095static int gpio_pin_to_irq(struct gpio_chip *gc, unsigned offset)
Paul Mundtb3c185a2012-06-20 17:29:04 +090096{
97 struct sh_pfc *pfc = gpio_to_pfc(gc);
Laurent Pinchartc07f54f2013-01-03 14:12:14 +010098 int i, k;
Paul Mundtb3c185a2012-06-20 17:29:04 +090099
Laurent Pinchartc07f54f2013-01-03 14:12:14 +0100100 for (i = 0; i < pfc->info->gpio_irq_size; i++) {
101 unsigned short *gpios = pfc->info->gpio_irq[i].gpios;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900102
Laurent Pinchartc07f54f2013-01-03 14:12:14 +0100103 for (k = 0; gpios[k]; k++) {
104 if (gpios[k] == offset)
105 return pfc->info->gpio_irq[i].irq;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900106 }
107 }
108
109 return -ENOSYS;
110}
111
Laurent Pinchart16883812012-12-06 14:49:25 +0100112static void gpio_pin_setup(struct sh_pfc_chip *chip)
Paul Mundtb3c185a2012-06-20 17:29:04 +0900113{
114 struct sh_pfc *pfc = chip->pfc;
115 struct gpio_chip *gc = &chip->gpio_chip;
116
Laurent Pinchart16883812012-12-06 14:49:25 +0100117 gc->request = gpio_pin_request;
118 gc->free = gpio_pin_free;
119 gc->direction_input = gpio_pin_direction_input;
120 gc->get = gpio_pin_get;
121 gc->direction_output = gpio_pin_direction_output;
122 gc->set = gpio_pin_set;
123 gc->to_irq = gpio_pin_to_irq;
124
125 gc->label = pfc->info->name;
126 gc->dev = pfc->dev;
127 gc->owner = THIS_MODULE;
128 gc->base = 0;
129 gc->ngpio = pfc->info->nr_pins;
130}
131
132/* -----------------------------------------------------------------------------
133 * Function GPIOs
134 */
135
136static int gpio_function_request(struct gpio_chip *gc, unsigned offset)
137{
138 struct sh_pfc *pfc = gpio_to_pfc(gc);
Laurent Pincharta68fdca2013-02-14 17:36:56 +0100139 unsigned int mark = pfc->info->func_gpios[offset].enum_id;
Laurent Pinchart16883812012-12-06 14:49:25 +0100140 unsigned long flags;
141 int ret = -EINVAL;
142
143 pr_notice_once("Use of GPIO API for function requests is deprecated, convert to pinctrl\n");
144
Laurent Pincharta68fdca2013-02-14 17:36:56 +0100145 if (mark == 0)
Laurent Pinchart16883812012-12-06 14:49:25 +0100146 return ret;
147
148 spin_lock_irqsave(&pfc->lock, flags);
149
Laurent Pincharta68fdca2013-02-14 17:36:56 +0100150 if (sh_pfc_config_mux(pfc, mark, PINMUX_TYPE_FUNCTION, GPIO_CFG_DRYRUN))
Laurent Pinchart16883812012-12-06 14:49:25 +0100151 goto done;
152
Laurent Pincharta68fdca2013-02-14 17:36:56 +0100153 if (sh_pfc_config_mux(pfc, mark, PINMUX_TYPE_FUNCTION, GPIO_CFG_REQ))
Laurent Pinchart16883812012-12-06 14:49:25 +0100154 goto done;
155
156 ret = 0;
157
158done:
159 spin_unlock_irqrestore(&pfc->lock, flags);
160 return ret;
161}
162
163static void gpio_function_free(struct gpio_chip *gc, unsigned offset)
164{
165 struct sh_pfc *pfc = gpio_to_pfc(gc);
Laurent Pincharta68fdca2013-02-14 17:36:56 +0100166 unsigned int mark = pfc->info->func_gpios[offset].enum_id;
Laurent Pinchart16883812012-12-06 14:49:25 +0100167 unsigned long flags;
168
169 spin_lock_irqsave(&pfc->lock, flags);
170
Laurent Pincharta68fdca2013-02-14 17:36:56 +0100171 sh_pfc_config_mux(pfc, mark, PINMUX_TYPE_FUNCTION, GPIO_CFG_FREE);
Laurent Pinchart16883812012-12-06 14:49:25 +0100172
173 spin_unlock_irqrestore(&pfc->lock, flags);
174}
175
176static void gpio_function_setup(struct sh_pfc_chip *chip)
177{
178 struct sh_pfc *pfc = chip->pfc;
179 struct gpio_chip *gc = &chip->gpio_chip;
180
181 gc->request = gpio_function_request;
182 gc->free = gpio_function_free;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900183
Laurent Pinchart19bb7fe32012-12-15 23:51:20 +0100184 gc->label = pfc->info->name;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900185 gc->owner = THIS_MODULE;
Laurent Pinchart16883812012-12-06 14:49:25 +0100186 gc->base = pfc->info->nr_pins;
187 gc->ngpio = pfc->info->nr_func_gpios;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900188}
189
Laurent Pinchart16883812012-12-06 14:49:25 +0100190/* -----------------------------------------------------------------------------
191 * Register/unregister
192 */
193
194static struct sh_pfc_chip *
195sh_pfc_add_gpiochip(struct sh_pfc *pfc, void(*setup)(struct sh_pfc_chip *))
Paul Mundtb3c185a2012-06-20 17:29:04 +0900196{
197 struct sh_pfc_chip *chip;
198 int ret;
199
Laurent Pinchart1724acf2012-12-15 23:50:48 +0100200 chip = devm_kzalloc(pfc->dev, sizeof(*chip), GFP_KERNEL);
Paul Mundtb3c185a2012-06-20 17:29:04 +0900201 if (unlikely(!chip))
Laurent Pinchart16883812012-12-06 14:49:25 +0100202 return ERR_PTR(-ENOMEM);
Paul Mundtb3c185a2012-06-20 17:29:04 +0900203
204 chip->pfc = pfc;
205
Laurent Pinchart16883812012-12-06 14:49:25 +0100206 setup(chip);
Paul Mundtb3c185a2012-06-20 17:29:04 +0900207
208 ret = gpiochip_add(&chip->gpio_chip);
Laurent Pinchart1724acf2012-12-15 23:50:48 +0100209 if (unlikely(ret < 0))
Laurent Pinchart16883812012-12-06 14:49:25 +0100210 return ERR_PTR(ret);
211
212 pr_info("%s handling gpio %u -> %u\n",
213 chip->gpio_chip.label, chip->gpio_chip.base,
214 chip->gpio_chip.base + chip->gpio_chip.ngpio - 1);
215
216 return chip;
217}
218
219int sh_pfc_register_gpiochip(struct sh_pfc *pfc)
220{
221 struct sh_pfc_chip *chip;
222
223 chip = sh_pfc_add_gpiochip(pfc, gpio_pin_setup);
224 if (IS_ERR(chip))
225 return PTR_ERR(chip);
Laurent Pinchart6f6a4a62012-12-15 23:50:46 +0100226
227 pfc->gpio = chip;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900228
Laurent Pinchart16883812012-12-06 14:49:25 +0100229 chip = sh_pfc_add_gpiochip(pfc, gpio_function_setup);
230 if (IS_ERR(chip))
231 return PTR_ERR(chip);
232
233 pfc->func = chip;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900234
Paul Mundtb3c185a2012-06-20 17:29:04 +0900235 return 0;
236}
237
Laurent Pinchart6f6a4a62012-12-15 23:50:46 +0100238int sh_pfc_unregister_gpiochip(struct sh_pfc *pfc)
Paul Mundtb3c185a2012-06-20 17:29:04 +0900239{
Laurent Pinchart16883812012-12-06 14:49:25 +0100240 int err;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900241 int ret;
242
Laurent Pinchart16883812012-12-06 14:49:25 +0100243 ret = gpiochip_remove(&pfc->gpio->gpio_chip);
244 err = gpiochip_remove(&pfc->func->gpio_chip);
Paul Mundtb3c185a2012-06-20 17:29:04 +0900245
Laurent Pinchart16883812012-12-06 14:49:25 +0100246 return ret < 0 ? ret : err;
Paul Mundtb3c185a2012-06-20 17:29:04 +0900247}