| 1 |
// SPDX-License-Identifier: GPL-2.0-only |
| 2 |
/* |
| 3 |
* |
| 4 |
* Copyright (C) 2012 John Crispin <[email protected]> |
| 5 |
*/ |
| 6 |
|
| 7 |
#include <linux/init.h> |
| 8 |
#include <linux/module.h> |
| 9 |
#include <linux/types.h> |
| 10 |
#include <linux/platform_device.h> |
| 11 |
#include <linux/mutex.h> |
| 12 |
#include <linux/gpio/driver.h> |
| 13 |
#include <linux/of.h> |
| 14 |
#include <linux/io.h> |
| 15 |
#include <linux/slab.h> |
| 16 |
|
| 17 |
#include <lantiq_soc.h> |
| 18 |
|
| 19 |
/* |
| 20 |
* By attaching hardware latches to the EBU it is possible to create output |
| 21 |
* only gpios. This driver configures a special memory address, which when |
| 22 |
* written to outputs 16 bit to the latches. |
| 23 |
*/ |
| 24 |
|
| 25 |
#define LTQ_EBU_BUSCON 0x1e7ff /* 16 bit access, slowest timing */ |
| 26 |
#define LTQ_EBU_WP 0x80000000 /* write protect bit */ |
| 27 |
|
| 28 |
struct ltq_mm { |
| 29 |
struct gpio_chip gc; |
| 30 |
void __iomem *regs; |
| 31 |
u16 shadow; /* shadow the latches state */ |
| 32 |
}; |
| 33 |
|
| 34 |
/** |
| 35 |
* ltq_mm_apply() - write the shadow value to the ebu address. |
| 36 |
* @chip: Pointer to our private data structure. |
| 37 |
* |
| 38 |
* Write the shadow value to the EBU to set the gpios. We need to set the |
| 39 |
* global EBU lock to make sure that PCI/MTD don't break. |
| 40 |
*/ |
| 41 |
static void ltq_mm_apply(struct ltq_mm *chip) |
| 42 |
{ |
| 43 |
unsigned long flags; |
| 44 |
|
| 45 |
spin_lock_irqsave(&ebu_lock, flags); |
| 46 |
ltq_ebu_w32(LTQ_EBU_BUSCON, LTQ_EBU_BUSCON1); |
| 47 |
__raw_writew(chip->shadow, chip->regs); |
| 48 |
ltq_ebu_w32(LTQ_EBU_BUSCON | LTQ_EBU_WP, LTQ_EBU_BUSCON1); |
| 49 |
spin_unlock_irqrestore(&ebu_lock, flags); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* ltq_mm_set() - gpio_chip->set - set gpios. |
| 54 |
* @gc: Pointer to gpio_chip device structure. |
| 55 |
* @offset: GPIO signal number. |
| 56 |
* @value: Value to be written to specified signal. |
| 57 |
* |
| 58 |
* Set the shadow value and call ltq_mm_apply. Always returns 0. |
| 59 |
*/ |
| 60 |
static int ltq_mm_set(struct gpio_chip *gc, unsigned int offset, int value) |
| 61 |
{ |
| 62 |
struct ltq_mm *chip = gpiochip_get_data(gc); |
| 63 |
|
| 64 |
if (value) |
| 65 |
chip->shadow |= (1 << offset); |
| 66 |
else |
| 67 |
chip->shadow &= ~(1 << offset); |
| 68 |
ltq_mm_apply(chip); |
| 69 |
|
| 70 |
return 0; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* ltq_mm_dir_out() - gpio_chip->dir_out - set gpio direction. |
| 75 |
* @gc: Pointer to gpio_chip device structure. |
| 76 |
* @offset: GPIO signal number. |
| 77 |
* @value: Value to be written to specified signal. |
| 78 |
* |
| 79 |
* Same as ltq_mm_set, always returns 0. |
| 80 |
*/ |
| 81 |
static int ltq_mm_dir_out(struct gpio_chip *gc, unsigned offset, int value) |
| 82 |
{ |
| 83 |
return ltq_mm_set(gc, offset, value); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* ltq_mm_save_regs() - Set initial values of GPIO pins |
| 88 |
* @chip: Pointer to our private data structure. |
| 89 |
*/ |
| 90 |
static void ltq_mm_save_regs(struct ltq_mm *chip) |
| 91 |
{ |
| 92 |
/* tell the ebu controller which memory address we will be using */ |
| 93 |
ltq_ebu_w32(CPHYSADDR((__force void *)chip->regs) | 0x1, LTQ_EBU_ADDRSEL1); |
| 94 |
|
| 95 |
ltq_mm_apply(chip); |
| 96 |
} |
| 97 |
|
| 98 |
static int ltq_mm_probe(struct platform_device *pdev) |
| 99 |
{ |
| 100 |
struct device *dev = &pdev->dev; |
| 101 |
struct device_node *np = dev->of_node; |
| 102 |
struct gpio_chip *gc; |
| 103 |
struct ltq_mm *chip; |
| 104 |
u32 shadow; |
| 105 |
|
| 106 |
chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL); |
| 107 |
if (!chip) |
| 108 |
return -ENOMEM; |
| 109 |
|
| 110 |
gc = &chip->gc; |
| 111 |
|
| 112 |
gc->base = -1; |
| 113 |
gc->ngpio = 16; |
| 114 |
gc->direction_output = ltq_mm_dir_out; |
| 115 |
gc->set = ltq_mm_set; |
| 116 |
gc->parent = dev; |
| 117 |
gc->owner = THIS_MODULE; |
| 118 |
gc->label = devm_kasprintf(dev, GFP_KERNEL, "%pOF", np); |
| 119 |
if (!gc->label) |
| 120 |
return -ENOMEM; |
| 121 |
|
| 122 |
chip->regs = devm_of_iomap(dev, np, 0, NULL); |
| 123 |
if (IS_ERR(chip->regs)) |
| 124 |
return PTR_ERR(chip->regs); |
| 125 |
|
| 126 |
ltq_mm_save_regs(chip); |
| 127 |
|
| 128 |
/* store the shadow value if one was passed by the devicetree */ |
| 129 |
if (!of_property_read_u32(pdev->dev.of_node, "lantiq,shadow", &shadow)) |
| 130 |
chip->shadow = shadow; |
| 131 |
|
| 132 |
return devm_gpiochip_add_data(dev, gc, chip); |
| 133 |
} |
| 134 |
|
| 135 |
static const struct of_device_id ltq_mm_match[] = { |
| 136 |
{ .compatible = "lantiq,gpio-mm" }, |
| 137 |
{}, |
| 138 |
}; |
| 139 |
MODULE_DEVICE_TABLE(of, ltq_mm_match); |
| 140 |
|
| 141 |
static struct platform_driver ltq_mm_driver = { |
| 142 |
.probe = ltq_mm_probe, |
| 143 |
.driver = { |
| 144 |
.name = "gpio-mm-ltq", |
| 145 |
.of_match_table = ltq_mm_match, |
| 146 |
}, |
| 147 |
}; |
| 148 |
|
| 149 |
static int __init ltq_mm_init(void) |
| 150 |
{ |
| 151 |
return platform_driver_register(<q_mm_driver); |
| 152 |
} |
| 153 |
|
| 154 |
subsys_initcall(ltq_mm_init); |
| 155 |
|
| 156 |
static void __exit ltq_mm_exit(void) |
| 157 |
{ |
| 158 |
platform_driver_unregister(<q_mm_driver); |
| 159 |
} |
| 160 |
module_exit(ltq_mm_exit); |