gpio.c 9.4 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

L
Laurent Pinchart 已提交
12
#include <linux/device.h>
13
#include <linux/gpio.h>
14
#include <linux/init.h>
15
#include <linux/module.h>
16
#include <linux/pinctrl/consumer.h>
17 18
#include <linux/slab.h>
#include <linux/spinlock.h>
19

20 21
#include "core.h"

22 23 24 25 26
struct sh_pfc_gpio_data_reg {
	const struct pinmux_data_reg *info;
	unsigned long shadow;
};

27 28 29 30 31
struct sh_pfc_gpio_pin {
	u8 dbit;
	u8 dreg;
};

32
struct sh_pfc_chip {
33 34
	struct sh_pfc			*pfc;
	struct gpio_chip		gpio_chip;
35

36
	struct sh_pfc_window		*mem;
37
	struct sh_pfc_gpio_data_reg	*regs;
38
	struct sh_pfc_gpio_pin		*pins;
39 40 41 42 43 44 45 46 47 48 49 50
};

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;
}

51 52 53
static void gpio_get_data_reg(struct sh_pfc_chip *chip, unsigned int gpio,
			      struct sh_pfc_gpio_data_reg **reg,
			      unsigned int *bit)
54
{
55 56
	int idx = sh_pfc_get_pin_index(chip->pfc, gpio);
	struct sh_pfc_gpio_pin *gpio_pin = &chip->pins[idx];
57

58 59
	*reg = &chip->regs[gpio_pin->dreg];
	*bit = gpio_pin->dbit;
60 61
}

62 63
static unsigned long gpio_read_data_reg(struct sh_pfc_chip *chip,
					const struct pinmux_data_reg *dreg)
64
{
65 66 67 68
	void __iomem *mem = dreg->reg - chip->mem->phys + chip->mem->virt;

	return sh_pfc_read_raw_reg(mem, dreg->reg_width);
}
69

70 71 72 73 74
static void gpio_write_data_reg(struct sh_pfc_chip *chip,
				const struct pinmux_data_reg *dreg,
				unsigned long value)
{
	void __iomem *mem = dreg->reg - chip->mem->phys + chip->mem->virt;
75

76 77
	sh_pfc_write_raw_reg(mem, dreg->reg_width, value);
}
78

79
static void gpio_setup_data_reg(struct sh_pfc_chip *chip, unsigned gpio)
80
{
81 82
	struct sh_pfc *pfc = chip->pfc;
	struct sh_pfc_gpio_pin *gpio_pin = &chip->pins[gpio];
L
Laurent Pinchart 已提交
83
	const struct sh_pfc_pin *pin = &pfc->info->pins[gpio];
84 85 86
	const struct pinmux_data_reg *dreg;
	unsigned int bit;
	unsigned int i;
87

88 89
	for (i = 0, dreg = pfc->info->data_regs; dreg->reg; ++i, ++dreg) {
		for (bit = 0; bit < dreg->reg_width; bit++) {
90 91 92
			if (dreg->enum_ids[bit] == pin->enum_id) {
				gpio_pin->dreg = i;
				gpio_pin->dbit = bit;
93 94 95 96 97 98 99 100
				return;
			}
		}
	}

	BUG();
}

101
static int gpio_setup_data_regs(struct sh_pfc_chip *chip)
102
{
103
	struct sh_pfc *pfc = chip->pfc;
104
	const struct pinmux_data_reg *dreg;
105
	unsigned int i;
106

107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
	/* Count the number of data registers, allocate memory and initialize
	 * them.
	 */
	for (i = 0; pfc->info->data_regs[i].reg_width; ++i)
		;

	chip->regs = devm_kzalloc(pfc->dev, i * sizeof(*chip->regs),
				  GFP_KERNEL);
	if (chip->regs == NULL)
		return -ENOMEM;

	for (i = 0, dreg = pfc->info->data_regs; dreg->reg_width; ++i, ++dreg) {
		chip->regs[i].info = dreg;
		chip->regs[i].shadow = gpio_read_data_reg(chip, dreg);
	}
122

123 124 125 126
	for (i = 0; i < pfc->info->nr_pins; i++) {
		if (pfc->info->pins[i].enum_id == 0)
			continue;

127
		gpio_setup_data_reg(chip, i);
128
	}
129 130

	return 0;
131 132
}

133 134 135
/* -----------------------------------------------------------------------------
 * Pin GPIOs
 */
136

137
static int gpio_pin_request(struct gpio_chip *gc, unsigned offset)
138
{
139
	struct sh_pfc *pfc = gpio_to_pfc(gc);
140
	int idx = sh_pfc_get_pin_index(pfc, offset);
141

142
	if (idx < 0 || pfc->info->pins[idx].enum_id == 0)
143 144
		return -EINVAL;

145
	return pinctrl_request_gpio(offset);
146 147
}

148
static void gpio_pin_free(struct gpio_chip *gc, unsigned offset)
149
{
150
	return pinctrl_free_gpio(offset);
151 152
}

153 154
static void gpio_pin_set_value(struct sh_pfc_chip *chip, unsigned offset,
			       int value)
155
{
156
	struct sh_pfc_gpio_data_reg *reg;
157 158
	unsigned long pos;
	unsigned int bit;
159

160
	gpio_get_data_reg(chip, offset, &reg, &bit);
161

162
	pos = reg->info->reg_width - (bit + 1);
163 164

	if (value)
165
		set_bit(pos, &reg->shadow);
166
	else
167
		clear_bit(pos, &reg->shadow);
168

169
	gpio_write_data_reg(chip, reg->info, reg->shadow);
170 171
}

172
static int gpio_pin_direction_input(struct gpio_chip *gc, unsigned offset)
173 174 175 176
{
	return pinctrl_gpio_direction_input(offset);
}

177
static int gpio_pin_direction_output(struct gpio_chip *gc, unsigned offset,
178 179
				    int value)
{
180
	gpio_pin_set_value(gpio_to_pfc_chip(gc), offset, value);
181 182 183 184

	return pinctrl_gpio_direction_output(offset);
}

185
static int gpio_pin_get(struct gpio_chip *gc, unsigned offset)
186
{
187
	struct sh_pfc_chip *chip = gpio_to_pfc_chip(gc);
188
	struct sh_pfc_gpio_data_reg *reg;
189 190
	unsigned long pos;
	unsigned int bit;
191

192
	gpio_get_data_reg(chip, offset, &reg, &bit);
193

194
	pos = reg->info->reg_width - (bit + 1);
195

196
	return (gpio_read_data_reg(chip, reg->info) >> pos) & 1;
197 198
}

199
static void gpio_pin_set(struct gpio_chip *gc, unsigned offset, int value)
200
{
201
	gpio_pin_set_value(gpio_to_pfc_chip(gc), offset, value);
202 203
}

204
static int gpio_pin_to_irq(struct gpio_chip *gc, unsigned offset)
205 206
{
	struct sh_pfc *pfc = gpio_to_pfc(gc);
207 208 209 210 211 212 213 214
	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;
215 216 217 218 219 220
		}
	}

	return -ENOSYS;
}

221
static int gpio_pin_setup(struct sh_pfc_chip *chip)
222 223 224
{
	struct sh_pfc *pfc = chip->pfc;
	struct gpio_chip *gc = &chip->gpio_chip;
225 226
	int ret;

227 228 229 230 231
	chip->pins = devm_kzalloc(pfc->dev, pfc->nr_pins * sizeof(*chip->pins),
				  GFP_KERNEL);
	if (chip->pins == NULL)
		return -ENOMEM;

232 233 234
	ret = gpio_setup_data_regs(chip);
	if (ret < 0)
		return ret;
235

236 237 238 239 240 241 242
	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;
243

244
	gc->label = pfc->info->name;
245
	gc->dev = pfc->dev;
246
	gc->owner = THIS_MODULE;
247
	gc->base = 0;
248
	gc->ngpio = pfc->nr_pins;
249 250

	return 0;
251 252
}

253 254 255 256 257 258
/* -----------------------------------------------------------------------------
 * Function GPIOs
 */

static int gpio_function_request(struct gpio_chip *gc, unsigned offset)
{
259
	static bool __print_once;
260
	struct sh_pfc *pfc = gpio_to_pfc(gc);
261
	unsigned int mark = pfc->info->func_gpios[offset].enum_id;
262
	unsigned long flags;
263
	int ret;
264

265 266 267 268 269 270
	if (!__print_once) {
		dev_notice(pfc->dev,
			   "Use of GPIO API for function requests is deprecated."
			   " Convert to pinctrl\n");
		__print_once = true;
	}
271

272
	if (mark == 0)
273
		return -EINVAL;
274 275

	spin_lock_irqsave(&pfc->lock, flags);
276
	ret = sh_pfc_config_mux(pfc, mark, PINMUX_TYPE_FUNCTION);
277
	spin_unlock_irqrestore(&pfc->lock, flags);
278

279 280 281 282 283 284 285
	return ret;
}

static void gpio_function_free(struct gpio_chip *gc, unsigned offset)
{
}

286
static int gpio_function_setup(struct sh_pfc_chip *chip)
287 288 289 290 291 292 293 294 295
{
	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;
296
	gc->base = pfc->nr_pins;
297
	gc->ngpio = pfc->info->nr_func_gpios;
298 299

	return 0;
300 301 302 303 304 305 306
}

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

static struct sh_pfc_chip *
307 308
sh_pfc_add_gpiochip(struct sh_pfc *pfc, int(*setup)(struct sh_pfc_chip *),
		    struct sh_pfc_window *mem)
309 310 311 312
{
	struct sh_pfc_chip *chip;
	int ret;

L
Laurent Pinchart 已提交
313
	chip = devm_kzalloc(pfc->dev, sizeof(*chip), GFP_KERNEL);
314
	if (unlikely(!chip))
315
		return ERR_PTR(-ENOMEM);
316

317
	chip->mem = mem;
318 319
	chip->pfc = pfc;

320 321 322
	ret = setup(chip);
	if (ret < 0)
		return ERR_PTR(ret);
323 324

	ret = gpiochip_add(&chip->gpio_chip);
L
Laurent Pinchart 已提交
325
	if (unlikely(ret < 0))
326 327
		return ERR_PTR(ret);

328 329 330
	dev_info(pfc->dev, "%s handling gpio %u -> %u\n",
		 chip->gpio_chip.label, chip->gpio_chip.base,
		 chip->gpio_chip.base + chip->gpio_chip.ngpio - 1);
331 332 333 334 335 336

	return chip;
}

int sh_pfc_register_gpiochip(struct sh_pfc *pfc)
{
337 338
	const struct pinmux_range *ranges;
	struct pinmux_range def_range;
339
	struct sh_pfc_chip *chip;
340 341
	unsigned int nr_ranges;
	unsigned int i;
342
	int ret;
343

344 345 346
	if (pfc->info->data_regs == NULL)
		return 0;

347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
	/* Find the memory window that contain the GPIO registers. Boards that
	 * register a separate GPIO device will not supply a memory resource
	 * that covers the data registers. In that case don't try to handle
	 * GPIOs.
	 */
	for (i = 0; i < pfc->num_windows; ++i) {
		struct sh_pfc_window *window = &pfc->window[i];

		if (pfc->info->data_regs[0].reg >= window->phys &&
		    pfc->info->data_regs[0].reg < window->phys + window->size)
			break;
	}

	if (i == pfc->num_windows)
		return 0;

363
	/* Register the real GPIOs chip. */
364
	chip = sh_pfc_add_gpiochip(pfc, gpio_pin_setup, &pfc->window[i]);
365 366
	if (IS_ERR(chip))
		return PTR_ERR(chip);
367 368

	pfc->gpio = chip;
369

370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
	/* Register the GPIO to pin mappings. */
	if (pfc->info->ranges == NULL) {
		def_range.begin = 0;
		def_range.end = pfc->info->nr_pins - 1;
		ranges = &def_range;
		nr_ranges = 1;
	} else {
		ranges = pfc->info->ranges;
		nr_ranges = pfc->info->nr_ranges;
	}

	for (i = 0; i < nr_ranges; ++i) {
		const struct pinmux_range *range = &ranges[i];

		ret = gpiochip_add_pin_range(&chip->gpio_chip,
					     dev_name(pfc->dev),
					     range->begin, range->begin,
					     range->end - range->begin + 1);
		if (ret < 0)
			return ret;
	}
391

392
	/* Register the function GPIOs chip. */
393 394 395
	if (pfc->info->nr_func_gpios == 0)
		return 0;

396
	chip = sh_pfc_add_gpiochip(pfc, gpio_function_setup, NULL);
397 398 399 400
	if (IS_ERR(chip))
		return PTR_ERR(chip);

	pfc->func = chip;
401 402 403 404

	return 0;
}

405
int sh_pfc_unregister_gpiochip(struct sh_pfc *pfc)
406
{
407
	int err;
408 409
	int ret;

410 411
	ret = gpiochip_remove(&pfc->gpio->gpio_chip);
	err = gpiochip_remove(&pfc->func->gpio_chip);
412

413
	return ret < 0 ? ret : err;
414
}