]>
Commit | Line | Data |
---|---|---|
935c500c JC |
1 | /* |
2 | * This program is free software; you can redistribute it and/or modify it | |
3 | * under the terms of the GNU General Public License version 2 as published | |
4 | * by the Free Software Foundation. | |
5 | * | |
a36e9a1c | 6 | * Copyright (C) 2012 John Crispin <[email protected]> |
935c500c JC |
7 | */ |
8 | ||
9 | #include <linux/init.h> | |
a36e9a1c | 10 | #include <linux/module.h> |
935c500c JC |
11 | #include <linux/types.h> |
12 | #include <linux/platform_device.h> | |
13 | #include <linux/mutex.h> | |
14 | #include <linux/gpio.h> | |
a36e9a1c JC |
15 | #include <linux/of.h> |
16 | #include <linux/of_gpio.h> | |
935c500c | 17 | #include <linux/io.h> |
a36e9a1c | 18 | #include <linux/slab.h> |
935c500c JC |
19 | |
20 | #include <lantiq_soc.h> | |
21 | ||
22 | /* | |
23 | * By attaching hardware latches to the EBU it is possible to create output | |
24 | * only gpios. This driver configures a special memory address, which when | |
25 | * written to outputs 16 bit to the latches. | |
26 | */ | |
27 | ||
28 | #define LTQ_EBU_BUSCON 0x1e7ff /* 16 bit access, slowest timing */ | |
29 | #define LTQ_EBU_WP 0x80000000 /* write protect bit */ | |
30 | ||
a36e9a1c JC |
31 | struct ltq_mm { |
32 | struct of_mm_gpio_chip mmchip; | |
33 | u16 shadow; /* shadow the latches state */ | |
34 | }; | |
935c500c | 35 | |
a36e9a1c JC |
36 | /** |
37 | * ltq_mm_apply() - write the shadow value to the ebu address. | |
38 | * @chip: Pointer to our private data structure. | |
39 | * | |
40 | * Write the shadow value to the EBU to set the gpios. We need to set the | |
41 | * global EBU lock to make sure that PCI/MTD dont break. | |
42 | */ | |
43 | static void ltq_mm_apply(struct ltq_mm *chip) | |
935c500c JC |
44 | { |
45 | unsigned long flags; | |
46 | ||
47 | spin_lock_irqsave(&ebu_lock, flags); | |
48 | ltq_ebu_w32(LTQ_EBU_BUSCON, LTQ_EBU_BUSCON1); | |
a36e9a1c | 49 | __raw_writew(chip->shadow, chip->mmchip.regs); |
935c500c JC |
50 | ltq_ebu_w32(LTQ_EBU_BUSCON | LTQ_EBU_WP, LTQ_EBU_BUSCON1); |
51 | spin_unlock_irqrestore(&ebu_lock, flags); | |
52 | } | |
53 | ||
a36e9a1c JC |
54 | /** |
55 | * ltq_mm_set() - gpio_chip->set - set gpios. | |
56 | * @gc: Pointer to gpio_chip device structure. | |
57 | * @gpio: GPIO signal number. | |
58 | * @val: Value to be written to specified signal. | |
59 | * | |
60 | * Set the shadow value and call ltq_mm_apply. | |
61 | */ | |
62 | static void ltq_mm_set(struct gpio_chip *gc, unsigned offset, int value) | |
935c500c | 63 | { |
6aa7dbfa | 64 | struct ltq_mm *chip = gpiochip_get_data(gc); |
a36e9a1c | 65 | |
935c500c | 66 | if (value) |
a36e9a1c | 67 | chip->shadow |= (1 << offset); |
935c500c | 68 | else |
a36e9a1c JC |
69 | chip->shadow &= ~(1 << offset); |
70 | ltq_mm_apply(chip); | |
935c500c JC |
71 | } |
72 | ||
a36e9a1c JC |
73 | /** |
74 | * ltq_mm_dir_out() - gpio_chip->dir_out - set gpio direction. | |
75 | * @gc: Pointer to gpio_chip device structure. | |
76 | * @gpio: GPIO signal number. | |
77 | * @val: 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) | |
935c500c | 82 | { |
a36e9a1c | 83 | ltq_mm_set(gc, offset, value); |
935c500c JC |
84 | |
85 | return 0; | |
86 | } | |
87 | ||
a36e9a1c JC |
88 | /** |
89 | * ltq_mm_save_regs() - Set initial values of GPIO pins | |
90 | * @mm_gc: pointer to memory mapped GPIO chip structure | |
91 | */ | |
92 | static void ltq_mm_save_regs(struct of_mm_gpio_chip *mm_gc) | |
93 | { | |
95c76170 GR |
94 | struct ltq_mm *chip = |
95 | container_of(mm_gc, struct ltq_mm, mmchip); | |
a36e9a1c JC |
96 | |
97 | /* tell the ebu controller which memory address we will be using */ | |
98 | ltq_ebu_w32(CPHYSADDR(chip->mmchip.regs) | 0x1, LTQ_EBU_ADDRSEL1); | |
99 | ||
100 | ltq_mm_apply(chip); | |
101 | } | |
935c500c | 102 | |
a36e9a1c | 103 | static int ltq_mm_probe(struct platform_device *pdev) |
935c500c | 104 | { |
a36e9a1c | 105 | struct ltq_mm *chip; |
68a99b18 | 106 | u32 shadow; |
935c500c | 107 | |
080440a2 | 108 | chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL); |
a36e9a1c | 109 | if (!chip) |
935c500c | 110 | return -ENOMEM; |
935c500c | 111 | |
da238221 RR |
112 | platform_set_drvdata(pdev, chip); |
113 | ||
a36e9a1c | 114 | chip->mmchip.gc.ngpio = 16; |
a36e9a1c JC |
115 | chip->mmchip.gc.direction_output = ltq_mm_dir_out; |
116 | chip->mmchip.gc.set = ltq_mm_set; | |
117 | chip->mmchip.save_regs = ltq_mm_save_regs; | |
935c500c | 118 | |
a36e9a1c | 119 | /* store the shadow value if one was passed by the devicetree */ |
68a99b18 RR |
120 | if (!of_property_read_u32(pdev->dev.of_node, "lantiq,shadow", &shadow)) |
121 | chip->shadow = shadow; | |
935c500c | 122 | |
6aa7dbfa | 123 | return of_mm_gpiochip_add_data(pdev->dev.of_node, &chip->mmchip, chip); |
935c500c JC |
124 | } |
125 | ||
da238221 RR |
126 | static int ltq_mm_remove(struct platform_device *pdev) |
127 | { | |
128 | struct ltq_mm *chip = platform_get_drvdata(pdev); | |
129 | ||
130 | of_mm_gpiochip_remove(&chip->mmchip); | |
131 | ||
132 | return 0; | |
133 | } | |
134 | ||
a36e9a1c JC |
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, | |
da238221 | 143 | .remove = ltq_mm_remove, |
935c500c | 144 | .driver = { |
a36e9a1c | 145 | .name = "gpio-mm-ltq", |
a36e9a1c | 146 | .of_match_table = ltq_mm_match, |
935c500c JC |
147 | }, |
148 | }; | |
149 | ||
a36e9a1c | 150 | static int __init ltq_mm_init(void) |
935c500c | 151 | { |
a36e9a1c | 152 | return platform_driver_register(<q_mm_driver); |
935c500c JC |
153 | } |
154 | ||
a36e9a1c | 155 | subsys_initcall(ltq_mm_init); |
da238221 RR |
156 | |
157 | static void __exit ltq_mm_exit(void) | |
158 | { | |
159 | platform_driver_unregister(<q_mm_driver); | |
160 | } | |
161 | module_exit(ltq_mm_exit); |