gpio.c 9.49 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
#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
struct sh_pfc_gpio_data_reg {
	const struct pinmux_data_reg *info;
24
	u32 shadow;
25 26
};

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

static struct sh_pfc *gpio_to_pfc(struct gpio_chip *gc)
{
43 44
	struct sh_pfc_chip *chip = gpiochip_get_data(gc);
	return chip->pfc;
45 46
}

47
static void gpio_get_data_reg(struct sh_pfc_chip *chip, unsigned int offset,
48 49
			      struct sh_pfc_gpio_data_reg **reg,
			      unsigned int *bit)
50
{
51
	int idx = sh_pfc_get_pin_index(chip->pfc, offset);
52
	struct sh_pfc_gpio_pin *gpio_pin = &chip->pins[idx];
53

54 55
	*reg = &chip->regs[gpio_pin->dreg];
	*bit = gpio_pin->dbit;
56 57
}

58 59
static u32 gpio_read_data_reg(struct sh_pfc_chip *chip,
			      const struct pinmux_data_reg *dreg)
60
{
61 62
	phys_addr_t address = dreg->reg;
	void __iomem *mem = address - chip->mem->phys + chip->mem->virt;
63 64 65

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

67
static void gpio_write_data_reg(struct sh_pfc_chip *chip,
68
				const struct pinmux_data_reg *dreg, u32 value)
69
{
70 71
	phys_addr_t address = dreg->reg;
	void __iomem *mem = address - chip->mem->phys + chip->mem->virt;
72

73 74
	sh_pfc_write_raw_reg(mem, dreg->reg_width, value);
}
75

76
static void gpio_setup_data_reg(struct sh_pfc_chip *chip, unsigned idx)
77
{
78
	struct sh_pfc *pfc = chip->pfc;
79 80
	struct sh_pfc_gpio_pin *gpio_pin = &chip->pins[idx];
	const struct sh_pfc_pin *pin = &pfc->info->pins[idx];
81 82 83
	const struct pinmux_data_reg *dreg;
	unsigned int bit;
	unsigned int i;
84

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

	BUG();
}

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

104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
	/* 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);
	}
119

120 121 122 123
	for (i = 0; i < pfc->info->nr_pins; i++) {
		if (pfc->info->pins[i].enum_id == 0)
			continue;

124
		gpio_setup_data_reg(chip, i);
125
	}
126 127

	return 0;
128 129
}

130 131 132
/* -----------------------------------------------------------------------------
 * Pin GPIOs
 */
133

134
static int gpio_pin_request(struct gpio_chip *gc, unsigned offset)
135
{
136
	struct sh_pfc *pfc = gpio_to_pfc(gc);
137
	int idx = sh_pfc_get_pin_index(pfc, offset);
138

139
	if (idx < 0 || pfc->info->pins[idx].enum_id == 0)
140 141
		return -EINVAL;

142
	return pinctrl_request_gpio(offset);
143 144
}

145
static void gpio_pin_free(struct gpio_chip *gc, unsigned offset)
146
{
147
	return pinctrl_free_gpio(offset);
148 149
}

150 151
static void gpio_pin_set_value(struct sh_pfc_chip *chip, unsigned offset,
			       int value)
152
{
153
	struct sh_pfc_gpio_data_reg *reg;
154
	unsigned int bit;
155
	unsigned int pos;
156

157
	gpio_get_data_reg(chip, offset, &reg, &bit);
158

159
	pos = reg->info->reg_width - (bit + 1);
160 161

	if (value)
162
		reg->shadow |= BIT(pos);
163
	else
164
		reg->shadow &= ~BIT(pos);
165

166
	gpio_write_data_reg(chip, reg->info, reg->shadow);
167 168
}

169
static int gpio_pin_direction_input(struct gpio_chip *gc, unsigned offset)
170 171 172 173
{
	return pinctrl_gpio_direction_input(offset);
}

174
static int gpio_pin_direction_output(struct gpio_chip *gc, unsigned offset,
175 176
				    int value)
{
177
	gpio_pin_set_value(gpiochip_get_data(gc), offset, value);
178 179 180 181

	return pinctrl_gpio_direction_output(offset);
}

182
static int gpio_pin_get(struct gpio_chip *gc, unsigned offset)
183
{
184
	struct sh_pfc_chip *chip = gpiochip_get_data(gc);
185
	struct sh_pfc_gpio_data_reg *reg;
186
	unsigned int bit;
187
	unsigned int pos;
188

189
	gpio_get_data_reg(chip, offset, &reg, &bit);
190

191
	pos = reg->info->reg_width - (bit + 1);
192

193
	return (gpio_read_data_reg(chip, reg->info) >> pos) & 1;
194 195
}

196
static void gpio_pin_set(struct gpio_chip *gc, unsigned offset, int value)
197
{
198
	gpio_pin_set_value(gpiochip_get_data(gc), offset, value);
199 200
}

201
static int gpio_pin_to_irq(struct gpio_chip *gc, unsigned offset)
202 203
{
	struct sh_pfc *pfc = gpio_to_pfc(gc);
204
	unsigned int i, k;
205 206

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

209
		for (k = 0; gpios[k] >= 0; k++) {
210
			if (gpios[k] == offset)
211
				goto found;
212 213 214 215
		}
	}

	return -ENOSYS;
216 217

found:
218
	return pfc->irqs[i];
219 220
}

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
	chip->pins = devm_kzalloc(pfc->dev, pfc->info->nr_pins *
				  sizeof(*chip->pins), GFP_KERNEL);
229 230 231
	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->parent = pfc->dev;
246
	gc->owner = THIS_MODULE;
247
	gc->base = 0;
248
	gc->ngpio = pfc->nr_gpio_pins;
249 250

	return 0;
251 252
}

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

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

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

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

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

280 281 282
	return ret;
}

283
static int gpio_function_setup(struct sh_pfc_chip *chip)
284 285 286 287 288 289 290 291
{
	struct sh_pfc *pfc = chip->pfc;
	struct gpio_chip *gc = &chip->gpio_chip;

	gc->request = gpio_function_request;

	gc->label = pfc->info->name;
	gc->owner = THIS_MODULE;
292
	gc->base = pfc->nr_gpio_pins;
293
	gc->ngpio = pfc->info->nr_func_gpios;
294 295

	return 0;
296
}
297
#endif
298 299 300 301 302 303

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

static struct sh_pfc_chip *
304 305
sh_pfc_add_gpiochip(struct sh_pfc *pfc, int(*setup)(struct sh_pfc_chip *),
		    struct sh_pfc_window *mem)
306 307 308 309
{
	struct sh_pfc_chip *chip;
	int ret;

310
	chip = devm_kzalloc(pfc->dev, sizeof(*chip), GFP_KERNEL);
311
	if (unlikely(!chip))
312
		return ERR_PTR(-ENOMEM);
313

314
	chip->mem = mem;
315 316
	chip->pfc = pfc;

317 318 319
	ret = setup(chip);
	if (ret < 0)
		return ERR_PTR(ret);
320

321
	ret = gpiochip_add_data(&chip->gpio_chip, chip);
322
	if (unlikely(ret < 0))
323 324
		return ERR_PTR(ret);

325 326 327
	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);
328 329 330 331 332 333 334

	return chip;
}

int sh_pfc_register_gpiochip(struct sh_pfc *pfc)
{
	struct sh_pfc_chip *chip;
335
	phys_addr_t address;
336
	unsigned int i;
337

338 339 340
	if (pfc->info->data_regs == NULL)
		return 0;

341 342 343 344 345
	/* 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.
	 */
346
	address = pfc->info->data_regs[0].reg;
347
	for (i = 0; i < pfc->num_windows; ++i) {
348
		struct sh_pfc_window *window = &pfc->windows[i];
349

350 351
		if (address >= window->phys &&
		    address < window->phys + window->size)
352 353 354 355 356 357
			break;
	}

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

358
	/* If we have IRQ resources make sure their number is correct. */
359
	if (pfc->num_irqs != pfc->info->gpio_irq_size) {
360 361 362 363
		dev_err(pfc->dev, "invalid number of IRQ resources\n");
		return -EINVAL;
	}

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

	pfc->gpio = chip;
370

371 372
	if (IS_ENABLED(CONFIG_OF) && pfc->dev->of_node)
		return 0;
373

374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
#ifdef CONFIG_SUPERH
	/*
	 * Register the GPIO to pin mappings. As pins with GPIO ports
	 * must come first in the ranges, skip the pins without GPIO
	 * ports by stopping at the first range that contains such a
	 * pin.
	 */
	for (i = 0; i < pfc->nr_ranges; ++i) {
		const struct sh_pfc_pin_range *range = &pfc->ranges[i];
		int ret;

		if (range->start >= pfc->nr_gpio_pins)
			break;

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

395
	/* Register the function GPIOs chip. */
396 397 398
	if (pfc->info->nr_func_gpios == 0)
		return 0;

399
	chip = sh_pfc_add_gpiochip(pfc, gpio_function_setup, NULL);
400 401 402 403
	if (IS_ERR(chip))
		return PTR_ERR(chip);

	pfc->func = chip;
404
#endif /* CONFIG_SUPERH */
405 406 407 408

	return 0;
}

409
int sh_pfc_unregister_gpiochip(struct sh_pfc *pfc)
410
{
411
	gpiochip_remove(&pfc->gpio->gpio_chip);
412
#ifdef CONFIG_SUPERH
413
	gpiochip_remove(&pfc->func->gpio_chip);
414
#endif
415
	return 0;
416
}