blob: 63c708c3f3ae54a50477aba77074976f61a8cafa [file] [log] [blame]
John Crispin935c5002011-03-30 09:27:56 +02001/*
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 *
John Crispinbaddc7c2016-12-20 19:57:55 +01006 * Copyright (C) 2012 John Crispin <john@phrozen.org>
John Crispin935c5002011-03-30 09:27:56 +02007 *
8 */
9
10#include <linux/slab.h>
11#include <linux/init.h>
John Crispin54f30062012-05-16 22:22:47 +020012#include <linux/module.h>
John Crispin935c5002011-03-30 09:27:56 +020013#include <linux/types.h>
John Crispin54f30062012-05-16 22:22:47 +020014#include <linux/of_platform.h>
John Crispin935c5002011-03-30 09:27:56 +020015#include <linux/mutex.h>
John Crispin935c5002011-03-30 09:27:56 +020016#include <linux/gpio.h>
John Crispin54f30062012-05-16 22:22:47 +020017#include <linux/io.h>
18#include <linux/of_gpio.h>
19#include <linux/clk.h>
20#include <linux/err.h>
John Crispin935c5002011-03-30 09:27:56 +020021
22#include <lantiq_soc.h>
23
John Crispin54f30062012-05-16 22:22:47 +020024/*
25 * The Serial To Parallel (STP) is found on MIPS based Lantiq socs. It is a
26 * peripheral controller used to drive external shift register cascades. At most
27 * 3 groups of 8 bits can be driven. The hardware is able to allow the DSL modem
28 * to drive the 2 LSBs of the cascade automatically.
29 */
John Crispin935c5002011-03-30 09:27:56 +020030
John Crispin54f30062012-05-16 22:22:47 +020031/* control register 0 */
32#define XWAY_STP_CON0 0x00
33/* control register 1 */
34#define XWAY_STP_CON1 0x04
35/* data register 0 */
36#define XWAY_STP_CPU0 0x08
37/* data register 1 */
38#define XWAY_STP_CPU1 0x0C
39/* access register */
40#define XWAY_STP_AR 0x10
John Crispin935c5002011-03-30 09:27:56 +020041
John Crispin54f30062012-05-16 22:22:47 +020042/* software or hardware update select bit */
43#define XWAY_STP_CON_SWU BIT(31)
John Crispin935c5002011-03-30 09:27:56 +020044
John Crispin54f30062012-05-16 22:22:47 +020045/* automatic update rates */
46#define XWAY_STP_2HZ 0
47#define XWAY_STP_4HZ BIT(23)
48#define XWAY_STP_8HZ BIT(24)
49#define XWAY_STP_10HZ (BIT(24) | BIT(23))
50#define XWAY_STP_SPEED_MASK (0xf << 23)
John Crispin935c5002011-03-30 09:27:56 +020051
John Crispin54f30062012-05-16 22:22:47 +020052/* clock source for automatic update */
53#define XWAY_STP_UPD_FPI BIT(31)
54#define XWAY_STP_UPD_MASK (BIT(31) | BIT(30))
John Crispin935c5002011-03-30 09:27:56 +020055
John Crispin54f30062012-05-16 22:22:47 +020056/* let the adsl core drive the 2 LSBs */
57#define XWAY_STP_ADSL_SHIFT 24
58#define XWAY_STP_ADSL_MASK 0x3
John Crispin935c5002011-03-30 09:27:56 +020059
John Crispin54f30062012-05-16 22:22:47 +020060/* 2 groups of 3 bits can be driven by the phys */
Martin Blumenstingl08b085a2015-05-25 22:39:50 +020061#define XWAY_STP_PHY_MASK 0x7
John Crispin54f30062012-05-16 22:22:47 +020062#define XWAY_STP_PHY1_SHIFT 27
63#define XWAY_STP_PHY2_SHIFT 15
John Crispin935c5002011-03-30 09:27:56 +020064
John Crispin54f30062012-05-16 22:22:47 +020065/* STP has 3 groups of 8 bits */
66#define XWAY_STP_GROUP0 BIT(0)
67#define XWAY_STP_GROUP1 BIT(1)
68#define XWAY_STP_GROUP2 BIT(2)
69#define XWAY_STP_GROUP_MASK (0x7)
John Crispin935c5002011-03-30 09:27:56 +020070
John Crispin54f30062012-05-16 22:22:47 +020071/* Edge configuration bits */
72#define XWAY_STP_FALLING BIT(26)
73#define XWAY_STP_EDGE_MASK BIT(26)
John Crispin935c5002011-03-30 09:27:56 +020074
John Crispin54f30062012-05-16 22:22:47 +020075#define xway_stp_r32(m, reg) __raw_readl(m + reg)
76#define xway_stp_w32(m, val, reg) __raw_writel(val, m + reg)
77#define xway_stp_w32_mask(m, clear, set, reg) \
78 ltq_w32((ltq_r32(m + reg) & ~(clear)) | (set), \
79 m + reg)
80
81struct xway_stp {
82 struct gpio_chip gc;
83 void __iomem *virt;
84 u32 edge; /* rising or falling edge triggered shift register */
John Crispinc9e854c2012-07-11 16:33:43 +020085 u32 shadow; /* shadow the shift registers state */
John Crispin54f30062012-05-16 22:22:47 +020086 u8 groups; /* we can drive 1-3 groups of 8bit each */
87 u8 dsl; /* the 2 LSBs can be driven by the dsl core */
88 u8 phy1; /* 3 bits can be driven by phy1 */
89 u8 phy2; /* 3 bits can be driven by phy2 */
90 u8 reserved; /* mask out the hw driven bits in gpio_request */
John Crispin935c5002011-03-30 09:27:56 +020091};
92
John Crispin54f30062012-05-16 22:22:47 +020093/**
Mathias Kresin5b9b2b52018-06-28 21:57:40 +020094 * xway_stp_get() - gpio_chip->get - get gpios.
95 * @gc: Pointer to gpio_chip device structure.
96 * @gpio: GPIO signal number.
97 *
98 * Gets the shadow value.
99 */
100static int xway_stp_get(struct gpio_chip *gc, unsigned int gpio)
101{
102 struct xway_stp *chip = gpiochip_get_data(gc);
103
104 return (xway_stp_r32(chip->virt, XWAY_STP_CPU0) & BIT(gpio));
105}
106
107/**
John Crispin54f30062012-05-16 22:22:47 +0200108 * xway_stp_set() - gpio_chip->set - set gpios.
109 * @gc: Pointer to gpio_chip device structure.
110 * @gpio: GPIO signal number.
111 * @val: Value to be written to specified signal.
112 *
113 * Set the shadow value and call ltq_ebu_apply.
114 */
115static void xway_stp_set(struct gpio_chip *gc, unsigned gpio, int val)
John Crispin935c5002011-03-30 09:27:56 +0200116{
Linus Walleijc63b30b2015-12-07 14:34:33 +0100117 struct xway_stp *chip = gpiochip_get_data(gc);
John Crispin935c5002011-03-30 09:27:56 +0200118
John Crispin54f30062012-05-16 22:22:47 +0200119 if (val)
120 chip->shadow |= BIT(gpio);
121 else
122 chip->shadow &= ~BIT(gpio);
123 xway_stp_w32(chip->virt, chip->shadow, XWAY_STP_CPU0);
124 xway_stp_w32_mask(chip->virt, 0, XWAY_STP_CON_SWU, XWAY_STP_CON0);
125}
John Crispin935c5002011-03-30 09:27:56 +0200126
John Crispin54f30062012-05-16 22:22:47 +0200127/**
128 * xway_stp_dir_out() - gpio_chip->dir_out - set gpio direction.
129 * @gc: Pointer to gpio_chip device structure.
130 * @gpio: GPIO signal number.
131 * @val: Value to be written to specified signal.
132 *
133 * Same as xway_stp_set, always returns 0.
134 */
135static int xway_stp_dir_out(struct gpio_chip *gc, unsigned gpio, int val)
136{
137 xway_stp_set(gc, gpio, val);
John Crispin935c5002011-03-30 09:27:56 +0200138
John Crispin935c5002011-03-30 09:27:56 +0200139 return 0;
140}
141
John Crispin54f30062012-05-16 22:22:47 +0200142/**
143 * xway_stp_request() - gpio_chip->request
144 * @gc: Pointer to gpio_chip device structure.
145 * @gpio: GPIO signal number.
146 *
147 * We mask out the HW driven pins
148 */
149static int xway_stp_request(struct gpio_chip *gc, unsigned gpio)
150{
Linus Walleijc63b30b2015-12-07 14:34:33 +0100151 struct xway_stp *chip = gpiochip_get_data(gc);
John Crispin54f30062012-05-16 22:22:47 +0200152
153 if ((gpio < 8) && (chip->reserved & BIT(gpio))) {
Linus Walleij58383c782015-11-04 09:56:26 +0100154 dev_err(gc->parent, "GPIO %d is driven by hardware\n", gpio);
John Crispin54f30062012-05-16 22:22:47 +0200155 return -ENODEV;
156 }
157
158 return 0;
159}
160
161/**
162 * xway_stp_hw_init() - Configure the STP unit and enable the clock gate
163 * @virt: pointer to the remapped register range
164 */
165static int xway_stp_hw_init(struct xway_stp *chip)
166{
167 /* sane defaults */
168 xway_stp_w32(chip->virt, 0, XWAY_STP_AR);
169 xway_stp_w32(chip->virt, 0, XWAY_STP_CPU0);
170 xway_stp_w32(chip->virt, 0, XWAY_STP_CPU1);
171 xway_stp_w32(chip->virt, XWAY_STP_CON_SWU, XWAY_STP_CON0);
172 xway_stp_w32(chip->virt, 0, XWAY_STP_CON1);
173
174 /* apply edge trigger settings for the shift register */
175 xway_stp_w32_mask(chip->virt, XWAY_STP_EDGE_MASK,
176 chip->edge, XWAY_STP_CON0);
177
178 /* apply led group settings */
179 xway_stp_w32_mask(chip->virt, XWAY_STP_GROUP_MASK,
180 chip->groups, XWAY_STP_CON1);
181
182 /* tell the hardware which pins are controlled by the dsl modem */
183 xway_stp_w32_mask(chip->virt,
184 XWAY_STP_ADSL_MASK << XWAY_STP_ADSL_SHIFT,
185 chip->dsl << XWAY_STP_ADSL_SHIFT,
186 XWAY_STP_CON0);
187
188 /* tell the hardware which pins are controlled by the phys */
189 xway_stp_w32_mask(chip->virt,
190 XWAY_STP_PHY_MASK << XWAY_STP_PHY1_SHIFT,
191 chip->phy1 << XWAY_STP_PHY1_SHIFT,
192 XWAY_STP_CON0);
193 xway_stp_w32_mask(chip->virt,
194 XWAY_STP_PHY_MASK << XWAY_STP_PHY2_SHIFT,
195 chip->phy2 << XWAY_STP_PHY2_SHIFT,
196 XWAY_STP_CON1);
197
198 /* mask out the hw driven bits in gpio_request */
199 chip->reserved = (chip->phy2 << 5) | (chip->phy1 << 2) | chip->dsl;
200
201 /*
202 * if we have pins that are driven by hw, we need to tell the stp what
203 * clock to use as a timer.
204 */
205 if (chip->reserved)
206 xway_stp_w32_mask(chip->virt, XWAY_STP_UPD_MASK,
207 XWAY_STP_UPD_FPI, XWAY_STP_CON1);
208
209 return 0;
210}
211
Bill Pemberton38363092012-11-19 13:22:34 -0500212static int xway_stp_probe(struct platform_device *pdev)
John Crispin935c5002011-03-30 09:27:56 +0200213{
Varka Bhadramd9b53c32014-10-21 12:42:59 +0530214 struct resource *res;
Martin Blumenstingl50f09072015-05-26 23:12:00 +0200215 u32 shadow, groups, dsl, phy;
John Crispin54f30062012-05-16 22:22:47 +0200216 struct xway_stp *chip;
217 struct clk *clk;
John Crispin935c5002011-03-30 09:27:56 +0200218 int ret = 0;
219
John Crispin54f30062012-05-16 22:22:47 +0200220 chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
221 if (!chip)
222 return -ENOMEM;
223
Varka Bhadramd9b53c32014-10-21 12:42:59 +0530224 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thierry Reding641d0342013-01-21 11:09:01 +0100225 chip->virt = devm_ioremap_resource(&pdev->dev, res);
226 if (IS_ERR(chip->virt))
227 return PTR_ERR(chip->virt);
Laurent Navet8ab2a6d2013-03-20 13:16:01 +0100228
Linus Walleij58383c782015-11-04 09:56:26 +0100229 chip->gc.parent = &pdev->dev;
John Crispin54f30062012-05-16 22:22:47 +0200230 chip->gc.label = "stp-xway";
231 chip->gc.direction_output = xway_stp_dir_out;
Mathias Kresin5b9b2b52018-06-28 21:57:40 +0200232 chip->gc.get = xway_stp_get;
John Crispin54f30062012-05-16 22:22:47 +0200233 chip->gc.set = xway_stp_set;
234 chip->gc.request = xway_stp_request;
235 chip->gc.base = -1;
236 chip->gc.owner = THIS_MODULE;
237
238 /* store the shadow value if one was passed by the devicetree */
Martin Blumenstingl50f09072015-05-26 23:12:00 +0200239 if (!of_property_read_u32(pdev->dev.of_node, "lantiq,shadow", &shadow))
240 chip->shadow = shadow;
John Crispin54f30062012-05-16 22:22:47 +0200241
242 /* find out which gpio groups should be enabled */
Martin Blumenstingl50f09072015-05-26 23:12:00 +0200243 if (!of_property_read_u32(pdev->dev.of_node, "lantiq,groups", &groups))
244 chip->groups = groups & XWAY_STP_GROUP_MASK;
John Crispin54f30062012-05-16 22:22:47 +0200245 else
246 chip->groups = XWAY_STP_GROUP0;
247 chip->gc.ngpio = fls(chip->groups) * 8;
248
249 /* find out which gpios are controlled by the dsl core */
Martin Blumenstingl50f09072015-05-26 23:12:00 +0200250 if (!of_property_read_u32(pdev->dev.of_node, "lantiq,dsl", &dsl))
251 chip->dsl = dsl & XWAY_STP_ADSL_MASK;
John Crispin54f30062012-05-16 22:22:47 +0200252
253 /* find out which gpios are controlled by the phys */
254 if (of_machine_is_compatible("lantiq,ar9") ||
255 of_machine_is_compatible("lantiq,gr9") ||
256 of_machine_is_compatible("lantiq,vr9")) {
Martin Blumenstingl50f09072015-05-26 23:12:00 +0200257 if (!of_property_read_u32(pdev->dev.of_node, "lantiq,phy1", &phy))
258 chip->phy1 = phy & XWAY_STP_PHY_MASK;
259 if (!of_property_read_u32(pdev->dev.of_node, "lantiq,phy2", &phy))
260 chip->phy2 = phy & XWAY_STP_PHY_MASK;
John Crispin54f30062012-05-16 22:22:47 +0200261 }
262
263 /* check which edge trigger we should use, default to a falling edge */
264 if (!of_find_property(pdev->dev.of_node, "lantiq,rising", NULL))
265 chip->edge = XWAY_STP_FALLING;
266
267 clk = clk_get(&pdev->dev, NULL);
268 if (IS_ERR(clk)) {
269 dev_err(&pdev->dev, "Failed to get clock\n");
270 return PTR_ERR(clk);
271 }
272 clk_enable(clk);
273
274 ret = xway_stp_hw_init(chip);
John Crispin935c5002011-03-30 09:27:56 +0200275 if (!ret)
Laxman Dewangan1a20cb22016-02-22 17:43:28 +0530276 ret = devm_gpiochip_add_data(&pdev->dev, &chip->gc, chip);
John Crispin54f30062012-05-16 22:22:47 +0200277
278 if (!ret)
279 dev_info(&pdev->dev, "Init done\n");
John Crispin935c5002011-03-30 09:27:56 +0200280
281 return ret;
282}
283
John Crispin54f30062012-05-16 22:22:47 +0200284static const struct of_device_id xway_stp_match[] = {
285 { .compatible = "lantiq,gpio-stp-xway" },
286 {},
287};
288MODULE_DEVICE_TABLE(of, xway_stp_match);
289
290static struct platform_driver xway_stp_driver = {
291 .probe = xway_stp_probe,
John Crispin935c5002011-03-30 09:27:56 +0200292 .driver = {
John Crispin54f30062012-05-16 22:22:47 +0200293 .name = "gpio-stp-xway",
John Crispin54f30062012-05-16 22:22:47 +0200294 .of_match_table = xway_stp_match,
John Crispin935c5002011-03-30 09:27:56 +0200295 },
296};
297
Linus Walleijafdadc02014-09-30 09:11:15 +0200298static int __init xway_stp_init(void)
John Crispin935c5002011-03-30 09:27:56 +0200299{
John Crispin54f30062012-05-16 22:22:47 +0200300 return platform_driver_register(&xway_stp_driver);
John Crispin935c5002011-03-30 09:27:56 +0200301}
302
John Crispin54f30062012-05-16 22:22:47 +0200303subsys_initcall(xway_stp_init);