gpio-mm-lantiq.c 4.03 KB
Newer Older
1 2 3 4 5
/*
 *  This program is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License version 2 as published
 *  by the Free Software Foundation.
 *
John Crispin's avatar
John Crispin committed
6
 *  Copyright (C) 2012 John Crispin <john@phrozen.org>
7 8 9
 */

#include <linux/init.h>
10
#include <linux/module.h>
11 12 13 14
#include <linux/types.h>
#include <linux/platform_device.h>
#include <linux/mutex.h>
#include <linux/gpio.h>
15 16
#include <linux/of.h>
#include <linux/of_gpio.h>
17
#include <linux/io.h>
18
#include <linux/slab.h>
19 20 21 22 23 24 25 26 27 28 29 30

#include <lantiq_soc.h>

/*
 * By attaching hardware latches to the EBU it is possible to create output
 * only gpios. This driver configures a special memory address, which when
 * written to outputs 16 bit to the latches.
 */

#define LTQ_EBU_BUSCON	0x1e7ff		/* 16 bit access, slowest timing */
#define LTQ_EBU_WP	0x80000000	/* write protect bit */

31 32 33 34
struct ltq_mm {
	struct of_mm_gpio_chip mmchip;
	u16 shadow;	/* shadow the latches state */
};
35

36 37 38 39 40 41 42 43
/**
 * ltq_mm_apply() - write the shadow value to the ebu address.
 * @chip:     Pointer to our private data structure.
 *
 * Write the shadow value to the EBU to set the gpios. We need to set the
 * global EBU lock to make sure that PCI/MTD dont break.
 */
static void ltq_mm_apply(struct ltq_mm *chip)
44 45 46 47 48
{
	unsigned long flags;

	spin_lock_irqsave(&ebu_lock, flags);
	ltq_ebu_w32(LTQ_EBU_BUSCON, LTQ_EBU_BUSCON1);
49
	__raw_writew(chip->shadow, chip->mmchip.regs);
50 51 52 53
	ltq_ebu_w32(LTQ_EBU_BUSCON | LTQ_EBU_WP, LTQ_EBU_BUSCON1);
	spin_unlock_irqrestore(&ebu_lock, flags);
}

54 55 56 57 58 59 60 61 62
/**
 * ltq_mm_set() - gpio_chip->set - set gpios.
 * @gc:     Pointer to gpio_chip device structure.
 * @gpio:   GPIO signal number.
 * @val:    Value to be written to specified signal.
 *
 * Set the shadow value and call ltq_mm_apply.
 */
static void ltq_mm_set(struct gpio_chip *gc, unsigned offset, int value)
63
{
64
	struct ltq_mm *chip = gpiochip_get_data(gc);
65

66
	if (value)
67
		chip->shadow |= (1 << offset);
68
	else
69 70
		chip->shadow &= ~(1 << offset);
	ltq_mm_apply(chip);
71 72
}

73 74 75 76 77 78 79 80 81
/**
 * ltq_mm_dir_out() - gpio_chip->dir_out - set gpio direction.
 * @gc:     Pointer to gpio_chip device structure.
 * @gpio:   GPIO signal number.
 * @val:    Value to be written to specified signal.
 *
 * Same as ltq_mm_set, always returns 0.
 */
static int ltq_mm_dir_out(struct gpio_chip *gc, unsigned offset, int value)
82
{
83
	ltq_mm_set(gc, offset, value);
84 85 86 87

	return 0;
}

88 89 90 91 92 93
/**
 * ltq_mm_save_regs() - Set initial values of GPIO pins
 * @mm_gc: pointer to memory mapped GPIO chip structure
 */
static void ltq_mm_save_regs(struct of_mm_gpio_chip *mm_gc)
{
94 95
	struct ltq_mm *chip =
		container_of(mm_gc, struct ltq_mm, mmchip);
96 97 98 99 100 101

	/* tell the ebu controller which memory address we will be using */
	ltq_ebu_w32(CPHYSADDR(chip->mmchip.regs) | 0x1, LTQ_EBU_ADDRSEL1);

	ltq_mm_apply(chip);
}
102

103
static int ltq_mm_probe(struct platform_device *pdev)
104
{
105
	struct ltq_mm *chip;
106
	u32 shadow;
107

108
	chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
109
	if (!chip)
110 111
		return -ENOMEM;

112 113
	platform_set_drvdata(pdev, chip);

114 115 116 117
	chip->mmchip.gc.ngpio = 16;
	chip->mmchip.gc.direction_output = ltq_mm_dir_out;
	chip->mmchip.gc.set = ltq_mm_set;
	chip->mmchip.save_regs = ltq_mm_save_regs;
118

119
	/* store the shadow value if one was passed by the devicetree */
120 121
	if (!of_property_read_u32(pdev->dev.of_node, "lantiq,shadow", &shadow))
		chip->shadow = shadow;
122

123
	return of_mm_gpiochip_add_data(pdev->dev.of_node, &chip->mmchip, chip);
124 125
}

126 127 128 129 130 131 132 133 134
static int ltq_mm_remove(struct platform_device *pdev)
{
	struct ltq_mm *chip = platform_get_drvdata(pdev);

	of_mm_gpiochip_remove(&chip->mmchip);

	return 0;
}

135 136 137 138 139 140 141 142
static const struct of_device_id ltq_mm_match[] = {
	{ .compatible = "lantiq,gpio-mm" },
	{},
};
MODULE_DEVICE_TABLE(of, ltq_mm_match);

static struct platform_driver ltq_mm_driver = {
	.probe = ltq_mm_probe,
143
	.remove = ltq_mm_remove,
144
	.driver = {
145 146
		.name = "gpio-mm-ltq",
		.of_match_table = ltq_mm_match,
147 148 149
	},
};

150
static int __init ltq_mm_init(void)
151
{
152
	return platform_driver_register(&ltq_mm_driver);
153 154
}

155
subsys_initcall(ltq_mm_init);
156 157 158 159 160 161

static void __exit ltq_mm_exit(void)
{
	platform_driver_unregister(&ltq_mm_driver);
}
module_exit(ltq_mm_exit);