gpio.c 5.45 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
/*
 * SuperH Pin Function Controller GPIO driver.
 *
 * Copyright (C) 2008 Magnus Damm
 * Copyright (C) 2009 - 2012 Paul Mundt
 *
 * This file is subject to the terms and conditions of the GNU General Public
 * License.  See the file "COPYING" in the main directory of this archive
 * for more details.
 */
11 12

#define pr_fmt(fmt) KBUILD_MODNAME " gpio: " fmt
13

14
#include <linux/device.h>
15
#include <linux/gpio.h>
16
#include <linux/init.h>
17
#include <linux/module.h>
18
#include <linux/pinctrl/consumer.h>
19 20
#include <linux/slab.h>
#include <linux/spinlock.h>
21

22 23
#include "core.h"

24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
struct sh_pfc_chip {
	struct sh_pfc		*pfc;
	struct gpio_chip	gpio_chip;
};

static struct sh_pfc_chip *gpio_to_pfc_chip(struct gpio_chip *gc)
{
	return container_of(gc, struct sh_pfc_chip, gpio_chip);
}

static struct sh_pfc *gpio_to_pfc(struct gpio_chip *gc)
{
	return gpio_to_pfc_chip(gc)->pfc;
}

39 40 41
/* -----------------------------------------------------------------------------
 * Pin GPIOs
 */
42

43
static int gpio_pin_request(struct gpio_chip *gc, unsigned offset)
44
{
45
	return pinctrl_request_gpio(offset);
46 47
}

48
static void gpio_pin_free(struct gpio_chip *gc, unsigned offset)
49
{
50
	return pinctrl_free_gpio(offset);
51 52
}

53
static void gpio_pin_set_value(struct sh_pfc *pfc, unsigned offset, int value)
54 55 56 57
{
	struct pinmux_data_reg *dr = NULL;
	int bit = 0;

58 59
	if (sh_pfc_get_data_reg(pfc, offset, &dr, &bit) != 0)
		BUG();
60

61
	sh_pfc_write_bit(dr, bit, value);
62 63
}

64
static int gpio_pin_direction_input(struct gpio_chip *gc, unsigned offset)
65 66 67 68
{
	return pinctrl_gpio_direction_input(offset);
}

69
static int gpio_pin_direction_output(struct gpio_chip *gc, unsigned offset,
70 71
				    int value)
{
72
	gpio_pin_set_value(gpio_to_pfc(gc), offset, value);
73 74 75 76

	return pinctrl_gpio_direction_output(offset);
}

77
static int gpio_pin_get(struct gpio_chip *gc, unsigned offset)
78
{
79 80 81 82 83 84 85 86
	struct sh_pfc *pfc = gpio_to_pfc(gc);
	struct pinmux_data_reg *dr = NULL;
	int bit = 0;

	if (sh_pfc_get_data_reg(pfc, offset, &dr, &bit) != 0)
		return -EINVAL;

	return sh_pfc_read_bit(dr, bit);
87 88
}

89
static void gpio_pin_set(struct gpio_chip *gc, unsigned offset, int value)
90
{
91
	gpio_pin_set_value(gpio_to_pfc(gc), offset, value);
92 93
}

94
static int gpio_pin_to_irq(struct gpio_chip *gc, unsigned offset)
95 96
{
	struct sh_pfc *pfc = gpio_to_pfc(gc);
97 98 99 100 101 102 103 104
	int i, k;

	for (i = 0; i < pfc->info->gpio_irq_size; i++) {
		unsigned short *gpios = pfc->info->gpio_irq[i].gpios;

		for (k = 0; gpios[k]; k++) {
			if (gpios[k] == offset)
				return pfc->info->gpio_irq[i].irq;
105 106 107 108 109 110
		}
	}

	return -ENOSYS;
}

111
static void gpio_pin_setup(struct sh_pfc_chip *chip)
112 113 114 115
{
	struct sh_pfc *pfc = chip->pfc;
	struct gpio_chip *gc = &chip->gpio_chip;

116 117 118 119 120 121 122
	gc->request = gpio_pin_request;
	gc->free = gpio_pin_free;
	gc->direction_input = gpio_pin_direction_input;
	gc->get = gpio_pin_get;
	gc->direction_output = gpio_pin_direction_output;
	gc->set = gpio_pin_set;
	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 133 134 135 136 137
/* -----------------------------------------------------------------------------
 * Function GPIOs
 */

static int gpio_function_request(struct gpio_chip *gc, unsigned offset)
{
	struct sh_pfc *pfc = gpio_to_pfc(gc);
138
	unsigned int mark = pfc->info->func_gpios[offset].enum_id;
139 140 141 142 143
	unsigned long flags;
	int ret = -EINVAL;

	pr_notice_once("Use of GPIO API for function requests is deprecated, convert to pinctrl\n");

144
	if (mark == 0)
145 146 147 148
		return ret;

	spin_lock_irqsave(&pfc->lock, flags);

149
	if (sh_pfc_config_mux(pfc, mark, PINMUX_TYPE_FUNCTION, GPIO_CFG_DRYRUN))
150 151
		goto done;

152
	if (sh_pfc_config_mux(pfc, mark, PINMUX_TYPE_FUNCTION, GPIO_CFG_REQ))
153 154 155 156 157 158 159 160 161 162 163 164
		goto done;

	ret = 0;

done:
	spin_unlock_irqrestore(&pfc->lock, flags);
	return ret;
}

static void gpio_function_free(struct gpio_chip *gc, unsigned offset)
{
	struct sh_pfc *pfc = gpio_to_pfc(gc);
165
	unsigned int mark = pfc->info->func_gpios[offset].enum_id;
166 167 168 169
	unsigned long flags;

	spin_lock_irqsave(&pfc->lock, flags);

170
	sh_pfc_config_mux(pfc, mark, PINMUX_TYPE_FUNCTION, GPIO_CFG_FREE);
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194

	spin_unlock_irqrestore(&pfc->lock, flags);
}

static void gpio_function_setup(struct sh_pfc_chip *chip)
{
	struct sh_pfc *pfc = chip->pfc;
	struct gpio_chip *gc = &chip->gpio_chip;

	gc->request = gpio_function_request;
	gc->free = gpio_function_free;

	gc->label = pfc->info->name;
	gc->owner = THIS_MODULE;
	gc->base = pfc->info->nr_pins;
	gc->ngpio = pfc->info->nr_func_gpios;
}

/* -----------------------------------------------------------------------------
 * Register/unregister
 */

static struct sh_pfc_chip *
sh_pfc_add_gpiochip(struct sh_pfc *pfc, void(*setup)(struct sh_pfc_chip *))
195 196 197 198
{
	struct sh_pfc_chip *chip;
	int ret;

199
	chip = devm_kzalloc(pfc->dev, sizeof(*chip), GFP_KERNEL);
200
	if (unlikely(!chip))
201
		return ERR_PTR(-ENOMEM);
202 203 204

	chip->pfc = pfc;

205
	setup(chip);
206 207

	ret = gpiochip_add(&chip->gpio_chip);
208
	if (unlikely(ret < 0))
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
		return ERR_PTR(ret);

	pr_info("%s handling gpio %u -> %u\n",
		chip->gpio_chip.label, chip->gpio_chip.base,
		chip->gpio_chip.base + chip->gpio_chip.ngpio - 1);

	return chip;
}

int sh_pfc_register_gpiochip(struct sh_pfc *pfc)
{
	struct sh_pfc_chip *chip;

	chip = sh_pfc_add_gpiochip(pfc, gpio_pin_setup);
	if (IS_ERR(chip))
		return PTR_ERR(chip);
225 226

	pfc->gpio = chip;
227

228 229 230 231 232
	chip = sh_pfc_add_gpiochip(pfc, gpio_function_setup);
	if (IS_ERR(chip))
		return PTR_ERR(chip);

	pfc->func = chip;
233 234 235 236

	return 0;
}

237
int sh_pfc_unregister_gpiochip(struct sh_pfc *pfc)
238
{
239
	int err;
240 241
	int ret;

242 243
	ret = gpiochip_remove(&pfc->gpio->gpio_chip);
	err = gpiochip_remove(&pfc->func->gpio_chip);
244

245
	return ret < 0 ? ret : err;
246
}