blob: 9e23a5ae8108ce51a59490483ec2a258dc20e987 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
John Crispin935c5002011-03-30 09:27:56 +02002/*
John Crispin935c5002011-03-30 09:27:56 +02003 *
John Crispinbaddc7c2016-12-20 19:57:55 +01004 * Copyright (C) 2012 John Crispin <john@phrozen.org>
John Crispin935c5002011-03-30 09:27:56 +02005 */
6
7#include <linux/slab.h>
8#include <linux/init.h>
John Crispin54f30062012-05-16 22:22:47 +02009#include <linux/module.h>
John Crispin935c5002011-03-30 09:27:56 +020010#include <linux/types.h>
John Crispin54f30062012-05-16 22:22:47 +020011#include <linux/of_platform.h>
John Crispin935c5002011-03-30 09:27:56 +020012#include <linux/mutex.h>
Linus Walleij97a48fc2018-06-27 11:37:23 +020013#include <linux/gpio/driver.h>
John Crispin54f30062012-05-16 22:22:47 +020014#include <linux/io.h>
John Crispin54f30062012-05-16 22:22:47 +020015#include <linux/clk.h>
16#include <linux/err.h>
John Crispin935c5002011-03-30 09:27:56 +020017
John Crispin54f30062012-05-16 22:22:47 +020018/*
19 * The Serial To Parallel (STP) is found on MIPS based Lantiq socs. It is a
20 * peripheral controller used to drive external shift register cascades. At most
21 * 3 groups of 8 bits can be driven. The hardware is able to allow the DSL modem
22 * to drive the 2 LSBs of the cascade automatically.
23 */
John Crispin935c5002011-03-30 09:27:56 +020024
John Crispin54f30062012-05-16 22:22:47 +020025/* control register 0 */
26#define XWAY_STP_CON0 0x00
27/* control register 1 */
28#define XWAY_STP_CON1 0x04
29/* data register 0 */
30#define XWAY_STP_CPU0 0x08
31/* data register 1 */
32#define XWAY_STP_CPU1 0x0C
33/* access register */
34#define XWAY_STP_AR 0x10
John Crispin935c5002011-03-30 09:27:56 +020035
John Crispin54f30062012-05-16 22:22:47 +020036/* software or hardware update select bit */
37#define XWAY_STP_CON_SWU BIT(31)
John Crispin935c5002011-03-30 09:27:56 +020038
John Crispin54f30062012-05-16 22:22:47 +020039/* automatic update rates */
40#define XWAY_STP_2HZ 0
41#define XWAY_STP_4HZ BIT(23)
42#define XWAY_STP_8HZ BIT(24)
43#define XWAY_STP_10HZ (BIT(24) | BIT(23))
44#define XWAY_STP_SPEED_MASK (0xf << 23)
John Crispin935c5002011-03-30 09:27:56 +020045
John Crispin54f30062012-05-16 22:22:47 +020046/* clock source for automatic update */
47#define XWAY_STP_UPD_FPI BIT(31)
48#define XWAY_STP_UPD_MASK (BIT(31) | BIT(30))
John Crispin935c5002011-03-30 09:27:56 +020049
John Crispin54f30062012-05-16 22:22:47 +020050/* let the adsl core drive the 2 LSBs */
51#define XWAY_STP_ADSL_SHIFT 24
52#define XWAY_STP_ADSL_MASK 0x3
John Crispin935c5002011-03-30 09:27:56 +020053
John Crispin54f30062012-05-16 22:22:47 +020054/* 2 groups of 3 bits can be driven by the phys */
Martin Blumenstingl08b085a2015-05-25 22:39:50 +020055#define XWAY_STP_PHY_MASK 0x7
John Crispin54f30062012-05-16 22:22:47 +020056#define XWAY_STP_PHY1_SHIFT 27
57#define XWAY_STP_PHY2_SHIFT 15
John Crispin935c5002011-03-30 09:27:56 +020058
John Crispin54f30062012-05-16 22:22:47 +020059/* STP has 3 groups of 8 bits */
60#define XWAY_STP_GROUP0 BIT(0)
61#define XWAY_STP_GROUP1 BIT(1)
62#define XWAY_STP_GROUP2 BIT(2)
63#define XWAY_STP_GROUP_MASK (0x7)
John Crispin935c5002011-03-30 09:27:56 +020064
John Crispin54f30062012-05-16 22:22:47 +020065/* Edge configuration bits */
66#define XWAY_STP_FALLING BIT(26)
67#define XWAY_STP_EDGE_MASK BIT(26)
John Crispin935c5002011-03-30 09:27:56 +020068
John Crispin54f30062012-05-16 22:22:47 +020069#define xway_stp_r32(m, reg) __raw_readl(m + reg)
70#define xway_stp_w32(m, val, reg) __raw_writel(val, m + reg)
71#define xway_stp_w32_mask(m, clear, set, reg) \
Martin Blumenstinglc0ec7012019-07-03 00:32:47 +020072 xway_stp_w32(m, (xway_stp_r32(m, reg) & ~(clear)) | (set), reg)
John Crispin54f30062012-05-16 22:22:47 +020073
74struct xway_stp {
75 struct gpio_chip gc;
76 void __iomem *virt;
77 u32 edge; /* rising or falling edge triggered shift register */
John Crispinc9e854c2012-07-11 16:33:43 +020078 u32 shadow; /* shadow the shift registers state */
John Crispin54f30062012-05-16 22:22:47 +020079 u8 groups; /* we can drive 1-3 groups of 8bit each */
80 u8 dsl; /* the 2 LSBs can be driven by the dsl core */
81 u8 phy1; /* 3 bits can be driven by phy1 */
82 u8 phy2; /* 3 bits can be driven by phy2 */
83 u8 reserved; /* mask out the hw driven bits in gpio_request */
John Crispin935c5002011-03-30 09:27:56 +020084};
85
John Crispin54f30062012-05-16 22:22:47 +020086/**
Mathias Kresin5b9b2b52018-06-28 21:57:40 +020087 * xway_stp_get() - gpio_chip->get - get gpios.
88 * @gc: Pointer to gpio_chip device structure.
89 * @gpio: GPIO signal number.
90 *
91 * Gets the shadow value.
92 */
93static int xway_stp_get(struct gpio_chip *gc, unsigned int gpio)
94{
95 struct xway_stp *chip = gpiochip_get_data(gc);
96
97 return (xway_stp_r32(chip->virt, XWAY_STP_CPU0) & BIT(gpio));
98}
99
100/**
John Crispin54f30062012-05-16 22:22:47 +0200101 * xway_stp_set() - gpio_chip->set - set gpios.
102 * @gc: Pointer to gpio_chip device structure.
103 * @gpio: GPIO signal number.
104 * @val: Value to be written to specified signal.
105 *
106 * Set the shadow value and call ltq_ebu_apply.
107 */
108static void xway_stp_set(struct gpio_chip *gc, unsigned gpio, int val)
John Crispin935c5002011-03-30 09:27:56 +0200109{
Linus Walleijc63b30b2015-12-07 14:34:33 +0100110 struct xway_stp *chip = gpiochip_get_data(gc);
John Crispin935c5002011-03-30 09:27:56 +0200111
John Crispin54f30062012-05-16 22:22:47 +0200112 if (val)
113 chip->shadow |= BIT(gpio);
114 else
115 chip->shadow &= ~BIT(gpio);
116 xway_stp_w32(chip->virt, chip->shadow, XWAY_STP_CPU0);
117 xway_stp_w32_mask(chip->virt, 0, XWAY_STP_CON_SWU, XWAY_STP_CON0);
118}
John Crispin935c5002011-03-30 09:27:56 +0200119
John Crispin54f30062012-05-16 22:22:47 +0200120/**
121 * xway_stp_dir_out() - gpio_chip->dir_out - set gpio direction.
122 * @gc: Pointer to gpio_chip device structure.
123 * @gpio: GPIO signal number.
124 * @val: Value to be written to specified signal.
125 *
126 * Same as xway_stp_set, always returns 0.
127 */
128static int xway_stp_dir_out(struct gpio_chip *gc, unsigned gpio, int val)
129{
130 xway_stp_set(gc, gpio, val);
John Crispin935c5002011-03-30 09:27:56 +0200131
John Crispin935c5002011-03-30 09:27:56 +0200132 return 0;
133}
134
John Crispin54f30062012-05-16 22:22:47 +0200135/**
136 * xway_stp_request() - gpio_chip->request
137 * @gc: Pointer to gpio_chip device structure.
138 * @gpio: GPIO signal number.
139 *
140 * We mask out the HW driven pins
141 */
142static int xway_stp_request(struct gpio_chip *gc, unsigned gpio)
143{
Linus Walleijc63b30b2015-12-07 14:34:33 +0100144 struct xway_stp *chip = gpiochip_get_data(gc);
John Crispin54f30062012-05-16 22:22:47 +0200145
146 if ((gpio < 8) && (chip->reserved & BIT(gpio))) {
Linus Walleij58383c782015-11-04 09:56:26 +0100147 dev_err(gc->parent, "GPIO %d is driven by hardware\n", gpio);
John Crispin54f30062012-05-16 22:22:47 +0200148 return -ENODEV;
149 }
150
151 return 0;
152}
153
154/**
155 * xway_stp_hw_init() - Configure the STP unit and enable the clock gate
Martin Blumenstingl8a7b1792019-07-03 00:32:45 +0200156 * @chip: Pointer to the xway_stp chip structure
John Crispin54f30062012-05-16 22:22:47 +0200157 */
Martin Blumenstingl8a7b1792019-07-03 00:32:45 +0200158static void xway_stp_hw_init(struct xway_stp *chip)
John Crispin54f30062012-05-16 22:22:47 +0200159{
160 /* sane defaults */
161 xway_stp_w32(chip->virt, 0, XWAY_STP_AR);
162 xway_stp_w32(chip->virt, 0, XWAY_STP_CPU0);
163 xway_stp_w32(chip->virt, 0, XWAY_STP_CPU1);
164 xway_stp_w32(chip->virt, XWAY_STP_CON_SWU, XWAY_STP_CON0);
165 xway_stp_w32(chip->virt, 0, XWAY_STP_CON1);
166
167 /* apply edge trigger settings for the shift register */
168 xway_stp_w32_mask(chip->virt, XWAY_STP_EDGE_MASK,
169 chip->edge, XWAY_STP_CON0);
170
171 /* apply led group settings */
172 xway_stp_w32_mask(chip->virt, XWAY_STP_GROUP_MASK,
173 chip->groups, XWAY_STP_CON1);
174
175 /* tell the hardware which pins are controlled by the dsl modem */
176 xway_stp_w32_mask(chip->virt,
177 XWAY_STP_ADSL_MASK << XWAY_STP_ADSL_SHIFT,
178 chip->dsl << XWAY_STP_ADSL_SHIFT,
179 XWAY_STP_CON0);
180
181 /* tell the hardware which pins are controlled by the phys */
182 xway_stp_w32_mask(chip->virt,
183 XWAY_STP_PHY_MASK << XWAY_STP_PHY1_SHIFT,
184 chip->phy1 << XWAY_STP_PHY1_SHIFT,
185 XWAY_STP_CON0);
186 xway_stp_w32_mask(chip->virt,
187 XWAY_STP_PHY_MASK << XWAY_STP_PHY2_SHIFT,
188 chip->phy2 << XWAY_STP_PHY2_SHIFT,
189 XWAY_STP_CON1);
190
191 /* mask out the hw driven bits in gpio_request */
192 chip->reserved = (chip->phy2 << 5) | (chip->phy1 << 2) | chip->dsl;
193
194 /*
195 * if we have pins that are driven by hw, we need to tell the stp what
196 * clock to use as a timer.
197 */
198 if (chip->reserved)
199 xway_stp_w32_mask(chip->virt, XWAY_STP_UPD_MASK,
200 XWAY_STP_UPD_FPI, XWAY_STP_CON1);
John Crispin54f30062012-05-16 22:22:47 +0200201}
202
Bill Pemberton38363092012-11-19 13:22:34 -0500203static int xway_stp_probe(struct platform_device *pdev)
John Crispin935c5002011-03-30 09:27:56 +0200204{
Martin Blumenstingl50f09072015-05-26 23:12:00 +0200205 u32 shadow, groups, dsl, phy;
John Crispin54f30062012-05-16 22:22:47 +0200206 struct xway_stp *chip;
207 struct clk *clk;
John Crispin935c5002011-03-30 09:27:56 +0200208 int ret = 0;
209
John Crispin54f30062012-05-16 22:22:47 +0200210 chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
211 if (!chip)
212 return -ENOMEM;
213
Enrico Weigelt, metux IT consult6ba7c532019-03-11 19:55:10 +0100214 chip->virt = devm_platform_ioremap_resource(pdev, 0);
Thierry Reding641d0342013-01-21 11:09:01 +0100215 if (IS_ERR(chip->virt))
216 return PTR_ERR(chip->virt);
Laurent Navet8ab2a6d2013-03-20 13:16:01 +0100217
Linus Walleij58383c782015-11-04 09:56:26 +0100218 chip->gc.parent = &pdev->dev;
John Crispin54f30062012-05-16 22:22:47 +0200219 chip->gc.label = "stp-xway";
220 chip->gc.direction_output = xway_stp_dir_out;
Mathias Kresin5b9b2b52018-06-28 21:57:40 +0200221 chip->gc.get = xway_stp_get;
John Crispin54f30062012-05-16 22:22:47 +0200222 chip->gc.set = xway_stp_set;
223 chip->gc.request = xway_stp_request;
224 chip->gc.base = -1;
225 chip->gc.owner = THIS_MODULE;
226
227 /* store the shadow value if one was passed by the devicetree */
Martin Blumenstingl50f09072015-05-26 23:12:00 +0200228 if (!of_property_read_u32(pdev->dev.of_node, "lantiq,shadow", &shadow))
229 chip->shadow = shadow;
John Crispin54f30062012-05-16 22:22:47 +0200230
231 /* find out which gpio groups should be enabled */
Martin Blumenstingl50f09072015-05-26 23:12:00 +0200232 if (!of_property_read_u32(pdev->dev.of_node, "lantiq,groups", &groups))
233 chip->groups = groups & XWAY_STP_GROUP_MASK;
John Crispin54f30062012-05-16 22:22:47 +0200234 else
235 chip->groups = XWAY_STP_GROUP0;
236 chip->gc.ngpio = fls(chip->groups) * 8;
237
238 /* find out which gpios are controlled by the dsl core */
Martin Blumenstingl50f09072015-05-26 23:12:00 +0200239 if (!of_property_read_u32(pdev->dev.of_node, "lantiq,dsl", &dsl))
240 chip->dsl = dsl & XWAY_STP_ADSL_MASK;
John Crispin54f30062012-05-16 22:22:47 +0200241
242 /* find out which gpios are controlled by the phys */
243 if (of_machine_is_compatible("lantiq,ar9") ||
244 of_machine_is_compatible("lantiq,gr9") ||
245 of_machine_is_compatible("lantiq,vr9")) {
Martin Blumenstingl50f09072015-05-26 23:12:00 +0200246 if (!of_property_read_u32(pdev->dev.of_node, "lantiq,phy1", &phy))
247 chip->phy1 = phy & XWAY_STP_PHY_MASK;
248 if (!of_property_read_u32(pdev->dev.of_node, "lantiq,phy2", &phy))
249 chip->phy2 = phy & XWAY_STP_PHY_MASK;
John Crispin54f30062012-05-16 22:22:47 +0200250 }
251
252 /* check which edge trigger we should use, default to a falling edge */
253 if (!of_find_property(pdev->dev.of_node, "lantiq,rising", NULL))
254 chip->edge = XWAY_STP_FALLING;
255
Martin Blumenstinglbd791c42019-07-03 00:32:46 +0200256 clk = devm_clk_get(&pdev->dev, NULL);
John Crispin54f30062012-05-16 22:22:47 +0200257 if (IS_ERR(clk)) {
258 dev_err(&pdev->dev, "Failed to get clock\n");
259 return PTR_ERR(clk);
260 }
John Crispin54f30062012-05-16 22:22:47 +0200261
Martin Blumenstinglbd791c42019-07-03 00:32:46 +0200262 ret = clk_prepare_enable(clk);
263 if (ret)
264 return ret;
John Crispin54f30062012-05-16 22:22:47 +0200265
Martin Blumenstingl8a7b1792019-07-03 00:32:45 +0200266 xway_stp_hw_init(chip);
John Crispin935c5002011-03-30 09:27:56 +0200267
Martin Blumenstingl8a7b1792019-07-03 00:32:45 +0200268 ret = devm_gpiochip_add_data(&pdev->dev, &chip->gc, chip);
Martin Blumenstinglbd791c42019-07-03 00:32:46 +0200269 if (ret) {
270 clk_disable_unprepare(clk);
Martin Blumenstingl8a7b1792019-07-03 00:32:45 +0200271 return ret;
Martin Blumenstinglbd791c42019-07-03 00:32:46 +0200272 }
John Crispin935c5002011-03-30 09:27:56 +0200273
Martin Blumenstingl8a7b1792019-07-03 00:32:45 +0200274 dev_info(&pdev->dev, "Init done\n");
275
276 return 0;
John Crispin935c5002011-03-30 09:27:56 +0200277}
278
John Crispin54f30062012-05-16 22:22:47 +0200279static const struct of_device_id xway_stp_match[] = {
280 { .compatible = "lantiq,gpio-stp-xway" },
281 {},
282};
283MODULE_DEVICE_TABLE(of, xway_stp_match);
284
285static struct platform_driver xway_stp_driver = {
286 .probe = xway_stp_probe,
John Crispin935c5002011-03-30 09:27:56 +0200287 .driver = {
John Crispin54f30062012-05-16 22:22:47 +0200288 .name = "gpio-stp-xway",
John Crispin54f30062012-05-16 22:22:47 +0200289 .of_match_table = xway_stp_match,
John Crispin935c5002011-03-30 09:27:56 +0200290 },
291};
292
Linus Walleijafdadc02014-09-30 09:11:15 +0200293static int __init xway_stp_init(void)
John Crispin935c5002011-03-30 09:27:56 +0200294{
John Crispin54f30062012-05-16 22:22:47 +0200295 return platform_driver_register(&xway_stp_driver);
John Crispin935c5002011-03-30 09:27:56 +0200296}
297
John Crispin54f30062012-05-16 22:22:47 +0200298subsys_initcall(xway_stp_init);