blob: 8a319d56c5de731a756936ebe5ba88ca72fa52d6 [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>
Linus Walleij97a48fc2018-06-27 11:37:23 +020016#include <linux/gpio/driver.h>
John Crispin54f30062012-05-16 22:22:47 +020017#include <linux/io.h>
John Crispin54f30062012-05-16 22:22:47 +020018#include <linux/clk.h>
19#include <linux/err.h>
John Crispin935c5002011-03-30 09:27:56 +020020
21#include <lantiq_soc.h>
22
John Crispin54f30062012-05-16 22:22:47 +020023/*
24 * The Serial To Parallel (STP) is found on MIPS based Lantiq socs. It is a
25 * peripheral controller used to drive external shift register cascades. At most
26 * 3 groups of 8 bits can be driven. The hardware is able to allow the DSL modem
27 * to drive the 2 LSBs of the cascade automatically.
28 */
John Crispin935c5002011-03-30 09:27:56 +020029
John Crispin54f30062012-05-16 22:22:47 +020030/* control register 0 */
31#define XWAY_STP_CON0 0x00
32/* control register 1 */
33#define XWAY_STP_CON1 0x04
34/* data register 0 */
35#define XWAY_STP_CPU0 0x08
36/* data register 1 */
37#define XWAY_STP_CPU1 0x0C
38/* access register */
39#define XWAY_STP_AR 0x10
John Crispin935c5002011-03-30 09:27:56 +020040
John Crispin54f30062012-05-16 22:22:47 +020041/* software or hardware update select bit */
42#define XWAY_STP_CON_SWU BIT(31)
John Crispin935c5002011-03-30 09:27:56 +020043
John Crispin54f30062012-05-16 22:22:47 +020044/* automatic update rates */
45#define XWAY_STP_2HZ 0
46#define XWAY_STP_4HZ BIT(23)
47#define XWAY_STP_8HZ BIT(24)
48#define XWAY_STP_10HZ (BIT(24) | BIT(23))
49#define XWAY_STP_SPEED_MASK (0xf << 23)
John Crispin935c5002011-03-30 09:27:56 +020050
John Crispin54f30062012-05-16 22:22:47 +020051/* clock source for automatic update */
52#define XWAY_STP_UPD_FPI BIT(31)
53#define XWAY_STP_UPD_MASK (BIT(31) | BIT(30))
John Crispin935c5002011-03-30 09:27:56 +020054
John Crispin54f30062012-05-16 22:22:47 +020055/* let the adsl core drive the 2 LSBs */
56#define XWAY_STP_ADSL_SHIFT 24
57#define XWAY_STP_ADSL_MASK 0x3
John Crispin935c5002011-03-30 09:27:56 +020058
John Crispin54f30062012-05-16 22:22:47 +020059/* 2 groups of 3 bits can be driven by the phys */
Martin Blumenstingl08b085a2015-05-25 22:39:50 +020060#define XWAY_STP_PHY_MASK 0x7
John Crispin54f30062012-05-16 22:22:47 +020061#define XWAY_STP_PHY1_SHIFT 27
62#define XWAY_STP_PHY2_SHIFT 15
John Crispin935c5002011-03-30 09:27:56 +020063
John Crispin54f30062012-05-16 22:22:47 +020064/* STP has 3 groups of 8 bits */
65#define XWAY_STP_GROUP0 BIT(0)
66#define XWAY_STP_GROUP1 BIT(1)
67#define XWAY_STP_GROUP2 BIT(2)
68#define XWAY_STP_GROUP_MASK (0x7)
John Crispin935c5002011-03-30 09:27:56 +020069
John Crispin54f30062012-05-16 22:22:47 +020070/* Edge configuration bits */
71#define XWAY_STP_FALLING BIT(26)
72#define XWAY_STP_EDGE_MASK BIT(26)
John Crispin935c5002011-03-30 09:27:56 +020073
John Crispin54f30062012-05-16 22:22:47 +020074#define xway_stp_r32(m, reg) __raw_readl(m + reg)
75#define xway_stp_w32(m, val, reg) __raw_writel(val, m + reg)
76#define xway_stp_w32_mask(m, clear, set, reg) \
77 ltq_w32((ltq_r32(m + reg) & ~(clear)) | (set), \
78 m + reg)
79
80struct xway_stp {
81 struct gpio_chip gc;
82 void __iomem *virt;
83 u32 edge; /* rising or falling edge triggered shift register */
John Crispinc9e854c2012-07-11 16:33:43 +020084 u32 shadow; /* shadow the shift registers state */
John Crispin54f30062012-05-16 22:22:47 +020085 u8 groups; /* we can drive 1-3 groups of 8bit each */
86 u8 dsl; /* the 2 LSBs can be driven by the dsl core */
87 u8 phy1; /* 3 bits can be driven by phy1 */
88 u8 phy2; /* 3 bits can be driven by phy2 */
89 u8 reserved; /* mask out the hw driven bits in gpio_request */
John Crispin935c5002011-03-30 09:27:56 +020090};
91
John Crispin54f30062012-05-16 22:22:47 +020092/**
Mathias Kresin5b9b2b52018-06-28 21:57:40 +020093 * xway_stp_get() - gpio_chip->get - get gpios.
94 * @gc: Pointer to gpio_chip device structure.
95 * @gpio: GPIO signal number.
96 *
97 * Gets the shadow value.
98 */
99static int xway_stp_get(struct gpio_chip *gc, unsigned int gpio)
100{
101 struct xway_stp *chip = gpiochip_get_data(gc);
102
103 return (xway_stp_r32(chip->virt, XWAY_STP_CPU0) & BIT(gpio));
104}
105
106/**
John Crispin54f30062012-05-16 22:22:47 +0200107 * xway_stp_set() - gpio_chip->set - set gpios.
108 * @gc: Pointer to gpio_chip device structure.
109 * @gpio: GPIO signal number.
110 * @val: Value to be written to specified signal.
111 *
112 * Set the shadow value and call ltq_ebu_apply.
113 */
114static void xway_stp_set(struct gpio_chip *gc, unsigned gpio, int val)
John Crispin935c5002011-03-30 09:27:56 +0200115{
Linus Walleijc63b30b2015-12-07 14:34:33 +0100116 struct xway_stp *chip = gpiochip_get_data(gc);
John Crispin935c5002011-03-30 09:27:56 +0200117
John Crispin54f30062012-05-16 22:22:47 +0200118 if (val)
119 chip->shadow |= BIT(gpio);
120 else
121 chip->shadow &= ~BIT(gpio);
122 xway_stp_w32(chip->virt, chip->shadow, XWAY_STP_CPU0);
123 xway_stp_w32_mask(chip->virt, 0, XWAY_STP_CON_SWU, XWAY_STP_CON0);
124}
John Crispin935c5002011-03-30 09:27:56 +0200125
John Crispin54f30062012-05-16 22:22:47 +0200126/**
127 * xway_stp_dir_out() - gpio_chip->dir_out - set gpio direction.
128 * @gc: Pointer to gpio_chip device structure.
129 * @gpio: GPIO signal number.
130 * @val: Value to be written to specified signal.
131 *
132 * Same as xway_stp_set, always returns 0.
133 */
134static int xway_stp_dir_out(struct gpio_chip *gc, unsigned gpio, int val)
135{
136 xway_stp_set(gc, gpio, val);
John Crispin935c5002011-03-30 09:27:56 +0200137
John Crispin935c5002011-03-30 09:27:56 +0200138 return 0;
139}
140
John Crispin54f30062012-05-16 22:22:47 +0200141/**
142 * xway_stp_request() - gpio_chip->request
143 * @gc: Pointer to gpio_chip device structure.
144 * @gpio: GPIO signal number.
145 *
146 * We mask out the HW driven pins
147 */
148static int xway_stp_request(struct gpio_chip *gc, unsigned gpio)
149{
Linus Walleijc63b30b2015-12-07 14:34:33 +0100150 struct xway_stp *chip = gpiochip_get_data(gc);
John Crispin54f30062012-05-16 22:22:47 +0200151
152 if ((gpio < 8) && (chip->reserved & BIT(gpio))) {
Linus Walleij58383c782015-11-04 09:56:26 +0100153 dev_err(gc->parent, "GPIO %d is driven by hardware\n", gpio);
John Crispin54f30062012-05-16 22:22:47 +0200154 return -ENODEV;
155 }
156
157 return 0;
158}
159
160/**
161 * xway_stp_hw_init() - Configure the STP unit and enable the clock gate
162 * @virt: pointer to the remapped register range
163 */
164static int xway_stp_hw_init(struct xway_stp *chip)
165{
166 /* sane defaults */
167 xway_stp_w32(chip->virt, 0, XWAY_STP_AR);
168 xway_stp_w32(chip->virt, 0, XWAY_STP_CPU0);
169 xway_stp_w32(chip->virt, 0, XWAY_STP_CPU1);
170 xway_stp_w32(chip->virt, XWAY_STP_CON_SWU, XWAY_STP_CON0);
171 xway_stp_w32(chip->virt, 0, XWAY_STP_CON1);
172
173 /* apply edge trigger settings for the shift register */
174 xway_stp_w32_mask(chip->virt, XWAY_STP_EDGE_MASK,
175 chip->edge, XWAY_STP_CON0);
176
177 /* apply led group settings */
178 xway_stp_w32_mask(chip->virt, XWAY_STP_GROUP_MASK,
179 chip->groups, XWAY_STP_CON1);
180
181 /* tell the hardware which pins are controlled by the dsl modem */
182 xway_stp_w32_mask(chip->virt,
183 XWAY_STP_ADSL_MASK << XWAY_STP_ADSL_SHIFT,
184 chip->dsl << XWAY_STP_ADSL_SHIFT,
185 XWAY_STP_CON0);
186
187 /* tell the hardware which pins are controlled by the phys */
188 xway_stp_w32_mask(chip->virt,
189 XWAY_STP_PHY_MASK << XWAY_STP_PHY1_SHIFT,
190 chip->phy1 << XWAY_STP_PHY1_SHIFT,
191 XWAY_STP_CON0);
192 xway_stp_w32_mask(chip->virt,
193 XWAY_STP_PHY_MASK << XWAY_STP_PHY2_SHIFT,
194 chip->phy2 << XWAY_STP_PHY2_SHIFT,
195 XWAY_STP_CON1);
196
197 /* mask out the hw driven bits in gpio_request */
198 chip->reserved = (chip->phy2 << 5) | (chip->phy1 << 2) | chip->dsl;
199
200 /*
201 * if we have pins that are driven by hw, we need to tell the stp what
202 * clock to use as a timer.
203 */
204 if (chip->reserved)
205 xway_stp_w32_mask(chip->virt, XWAY_STP_UPD_MASK,
206 XWAY_STP_UPD_FPI, XWAY_STP_CON1);
207
208 return 0;
209}
210
Bill Pemberton38363092012-11-19 13:22:34 -0500211static int xway_stp_probe(struct platform_device *pdev)
John Crispin935c5002011-03-30 09:27:56 +0200212{
Martin Blumenstingl50f09072015-05-26 23:12:00 +0200213 u32 shadow, groups, dsl, phy;
John Crispin54f30062012-05-16 22:22:47 +0200214 struct xway_stp *chip;
215 struct clk *clk;
John Crispin935c5002011-03-30 09:27:56 +0200216 int ret = 0;
217
John Crispin54f30062012-05-16 22:22:47 +0200218 chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
219 if (!chip)
220 return -ENOMEM;
221
Enrico Weigelt, metux IT consult6ba7c532019-03-11 19:55:10 +0100222 chip->virt = devm_platform_ioremap_resource(pdev, 0);
Thierry Reding641d0342013-01-21 11:09:01 +0100223 if (IS_ERR(chip->virt))
224 return PTR_ERR(chip->virt);
Laurent Navet8ab2a6d2013-03-20 13:16:01 +0100225
Linus Walleij58383c782015-11-04 09:56:26 +0100226 chip->gc.parent = &pdev->dev;
John Crispin54f30062012-05-16 22:22:47 +0200227 chip->gc.label = "stp-xway";
228 chip->gc.direction_output = xway_stp_dir_out;
Mathias Kresin5b9b2b52018-06-28 21:57:40 +0200229 chip->gc.get = xway_stp_get;
John Crispin54f30062012-05-16 22:22:47 +0200230 chip->gc.set = xway_stp_set;
231 chip->gc.request = xway_stp_request;
232 chip->gc.base = -1;
233 chip->gc.owner = THIS_MODULE;
234
235 /* store the shadow value if one was passed by the devicetree */
Martin Blumenstingl50f09072015-05-26 23:12:00 +0200236 if (!of_property_read_u32(pdev->dev.of_node, "lantiq,shadow", &shadow))
237 chip->shadow = shadow;
John Crispin54f30062012-05-16 22:22:47 +0200238
239 /* find out which gpio groups should be enabled */
Martin Blumenstingl50f09072015-05-26 23:12:00 +0200240 if (!of_property_read_u32(pdev->dev.of_node, "lantiq,groups", &groups))
241 chip->groups = groups & XWAY_STP_GROUP_MASK;
John Crispin54f30062012-05-16 22:22:47 +0200242 else
243 chip->groups = XWAY_STP_GROUP0;
244 chip->gc.ngpio = fls(chip->groups) * 8;
245
246 /* find out which gpios are controlled by the dsl core */
Martin Blumenstingl50f09072015-05-26 23:12:00 +0200247 if (!of_property_read_u32(pdev->dev.of_node, "lantiq,dsl", &dsl))
248 chip->dsl = dsl & XWAY_STP_ADSL_MASK;
John Crispin54f30062012-05-16 22:22:47 +0200249
250 /* find out which gpios are controlled by the phys */
251 if (of_machine_is_compatible("lantiq,ar9") ||
252 of_machine_is_compatible("lantiq,gr9") ||
253 of_machine_is_compatible("lantiq,vr9")) {
Martin Blumenstingl50f09072015-05-26 23:12:00 +0200254 if (!of_property_read_u32(pdev->dev.of_node, "lantiq,phy1", &phy))
255 chip->phy1 = phy & XWAY_STP_PHY_MASK;
256 if (!of_property_read_u32(pdev->dev.of_node, "lantiq,phy2", &phy))
257 chip->phy2 = phy & XWAY_STP_PHY_MASK;
John Crispin54f30062012-05-16 22:22:47 +0200258 }
259
260 /* check which edge trigger we should use, default to a falling edge */
261 if (!of_find_property(pdev->dev.of_node, "lantiq,rising", NULL))
262 chip->edge = XWAY_STP_FALLING;
263
264 clk = clk_get(&pdev->dev, NULL);
265 if (IS_ERR(clk)) {
266 dev_err(&pdev->dev, "Failed to get clock\n");
267 return PTR_ERR(clk);
268 }
269 clk_enable(clk);
270
271 ret = xway_stp_hw_init(chip);
John Crispin935c5002011-03-30 09:27:56 +0200272 if (!ret)
Laxman Dewangan1a20cb22016-02-22 17:43:28 +0530273 ret = devm_gpiochip_add_data(&pdev->dev, &chip->gc, chip);
John Crispin54f30062012-05-16 22:22:47 +0200274
275 if (!ret)
276 dev_info(&pdev->dev, "Init done\n");
John Crispin935c5002011-03-30 09:27:56 +0200277
278 return ret;
279}
280
John Crispin54f30062012-05-16 22:22:47 +0200281static const struct of_device_id xway_stp_match[] = {
282 { .compatible = "lantiq,gpio-stp-xway" },
283 {},
284};
285MODULE_DEVICE_TABLE(of, xway_stp_match);
286
287static struct platform_driver xway_stp_driver = {
288 .probe = xway_stp_probe,
John Crispin935c5002011-03-30 09:27:56 +0200289 .driver = {
John Crispin54f30062012-05-16 22:22:47 +0200290 .name = "gpio-stp-xway",
John Crispin54f30062012-05-16 22:22:47 +0200291 .of_match_table = xway_stp_match,
John Crispin935c5002011-03-30 09:27:56 +0200292 },
293};
294
Linus Walleijafdadc02014-09-30 09:11:15 +0200295static int __init xway_stp_init(void)
John Crispin935c5002011-03-30 09:27:56 +0200296{
John Crispin54f30062012-05-16 22:22:47 +0200297 return platform_driver_register(&xway_stp_driver);
John Crispin935c5002011-03-30 09:27:56 +0200298}
299
John Crispin54f30062012-05-16 22:22:47 +0200300subsys_initcall(xway_stp_init);