Blame view

drivers/gpio/gpio-stp-xway.c 8.05 KB
935c500c3   John Crispin   MIPS: Lantiq: Add...
1
2
3
4
5
  /*
   *  This program is free software; you can redistribute it and/or modify it
   *  under the terms of the GNU General Public License version 2 as published
   *  by the Free Software Foundation.
   *
baddc7ca4   John Crispin   gpio: update my e...
6
   *  Copyright (C) 2012 John Crispin <john@phrozen.org>
935c500c3   John Crispin   MIPS: Lantiq: Add...
7
8
9
10
11
   *
   */
  
  #include <linux/slab.h>
  #include <linux/init.h>
54f300661   John Crispin   GPIO: MIPS: lanti...
12
  #include <linux/module.h>
935c500c3   John Crispin   MIPS: Lantiq: Add...
13
  #include <linux/types.h>
54f300661   John Crispin   GPIO: MIPS: lanti...
14
  #include <linux/of_platform.h>
935c500c3   John Crispin   MIPS: Lantiq: Add...
15
  #include <linux/mutex.h>
935c500c3   John Crispin   MIPS: Lantiq: Add...
16
  #include <linux/gpio.h>
54f300661   John Crispin   GPIO: MIPS: lanti...
17
18
19
20
  #include <linux/io.h>
  #include <linux/of_gpio.h>
  #include <linux/clk.h>
  #include <linux/err.h>
935c500c3   John Crispin   MIPS: Lantiq: Add...
21
22
  
  #include <lantiq_soc.h>
54f300661   John Crispin   GPIO: MIPS: lanti...
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
  /*
   * The Serial To Parallel (STP) is found on MIPS based Lantiq socs. It is a
   * peripheral controller used to drive external shift register cascades. At most
   * 3 groups of 8 bits can be driven. The hardware is able to allow the DSL modem
   * to drive the 2 LSBs of the cascade automatically.
   */
  
  /* control register 0 */
  #define XWAY_STP_CON0		0x00
  /* control register 1 */
  #define XWAY_STP_CON1		0x04
  /* data register 0 */
  #define XWAY_STP_CPU0		0x08
  /* data register 1 */
  #define XWAY_STP_CPU1		0x0C
  /* access register */
  #define XWAY_STP_AR		0x10
  
  /* software or hardware update select bit */
  #define XWAY_STP_CON_SWU	BIT(31)
  
  /* automatic update rates */
  #define XWAY_STP_2HZ		0
  #define XWAY_STP_4HZ		BIT(23)
  #define XWAY_STP_8HZ		BIT(24)
  #define XWAY_STP_10HZ		(BIT(24) | BIT(23))
  #define XWAY_STP_SPEED_MASK	(0xf << 23)
  
  /* clock source for automatic update */
  #define XWAY_STP_UPD_FPI	BIT(31)
  #define XWAY_STP_UPD_MASK	(BIT(31) | BIT(30))
  
  /* let the adsl core drive the 2 LSBs */
  #define XWAY_STP_ADSL_SHIFT	24
  #define XWAY_STP_ADSL_MASK	0x3
  
  /* 2 groups of 3 bits can be driven by the phys */
08b085a07   Martin Blumenstingl   gpio-stp-xway: Fi...
60
  #define XWAY_STP_PHY_MASK	0x7
54f300661   John Crispin   GPIO: MIPS: lanti...
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
  #define XWAY_STP_PHY1_SHIFT	27
  #define XWAY_STP_PHY2_SHIFT	15
  
  /* STP has 3 groups of 8 bits */
  #define XWAY_STP_GROUP0		BIT(0)
  #define XWAY_STP_GROUP1		BIT(1)
  #define XWAY_STP_GROUP2		BIT(2)
  #define XWAY_STP_GROUP_MASK	(0x7)
  
  /* Edge configuration bits */
  #define XWAY_STP_FALLING	BIT(26)
  #define XWAY_STP_EDGE_MASK	BIT(26)
  
  #define xway_stp_r32(m, reg)		__raw_readl(m + reg)
  #define xway_stp_w32(m, val, reg)	__raw_writel(val, m + reg)
  #define xway_stp_w32_mask(m, clear, set, reg) \
  		ltq_w32((ltq_r32(m + reg) & ~(clear)) | (set), \
  		m + reg)
  
  struct xway_stp {
  	struct gpio_chip gc;
  	void __iomem *virt;
  	u32 edge;	/* rising or falling edge triggered shift register */
c9e854cf9   John Crispin   GPIO: MIPS: lanti...
84
  	u32 shadow;	/* shadow the shift registers state */
54f300661   John Crispin   GPIO: MIPS: lanti...
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
  	u8 groups;	/* we can drive 1-3 groups of 8bit each */
  	u8 dsl;		/* the 2 LSBs can be driven by the dsl core */
  	u8 phy1;	/* 3 bits can be driven by phy1 */
  	u8 phy2;	/* 3 bits can be driven by phy2 */
  	u8 reserved;	/* mask out the hw driven bits in gpio_request */
  };
  
  /**
   * xway_stp_set() - gpio_chip->set - set gpios.
   * @gc:     Pointer to gpio_chip device structure.
   * @gpio:   GPIO signal number.
   * @val:    Value to be written to specified signal.
   *
   * Set the shadow value and call ltq_ebu_apply.
   */
  static void xway_stp_set(struct gpio_chip *gc, unsigned gpio, int val)
935c500c3   John Crispin   MIPS: Lantiq: Add...
101
  {
c63b30b08   Linus Walleij   gpio: stp-xway: u...
102
  	struct xway_stp *chip = gpiochip_get_data(gc);
54f300661   John Crispin   GPIO: MIPS: lanti...
103
104
105
  
  	if (val)
  		chip->shadow |= BIT(gpio);
935c500c3   John Crispin   MIPS: Lantiq: Add...
106
  	else
54f300661   John Crispin   GPIO: MIPS: lanti...
107
108
109
  		chip->shadow &= ~BIT(gpio);
  	xway_stp_w32(chip->virt, chip->shadow, XWAY_STP_CPU0);
  	xway_stp_w32_mask(chip->virt, 0, XWAY_STP_CON_SWU, XWAY_STP_CON0);
935c500c3   John Crispin   MIPS: Lantiq: Add...
110
  }
54f300661   John Crispin   GPIO: MIPS: lanti...
111
112
113
114
115
116
117
118
119
  /**
   * xway_stp_dir_out() - gpio_chip->dir_out - set gpio direction.
   * @gc:     Pointer to gpio_chip device structure.
   * @gpio:   GPIO signal number.
   * @val:    Value to be written to specified signal.
   *
   * Same as xway_stp_set, always returns 0.
   */
  static int xway_stp_dir_out(struct gpio_chip *gc, unsigned gpio, int val)
935c500c3   John Crispin   MIPS: Lantiq: Add...
120
  {
54f300661   John Crispin   GPIO: MIPS: lanti...
121
  	xway_stp_set(gc, gpio, val);
935c500c3   John Crispin   MIPS: Lantiq: Add...
122
123
124
  
  	return 0;
  }
54f300661   John Crispin   GPIO: MIPS: lanti...
125
126
127
128
129
130
131
132
133
  /**
   * xway_stp_request() - gpio_chip->request
   * @gc:     Pointer to gpio_chip device structure.
   * @gpio:   GPIO signal number.
   *
   * We mask out the HW driven pins
   */
  static int xway_stp_request(struct gpio_chip *gc, unsigned gpio)
  {
c63b30b08   Linus Walleij   gpio: stp-xway: u...
134
  	struct xway_stp *chip = gpiochip_get_data(gc);
54f300661   John Crispin   GPIO: MIPS: lanti...
135
136
  
  	if ((gpio < 8) && (chip->reserved & BIT(gpio))) {
58383c784   Linus Walleij   gpio: change memb...
137
138
  		dev_err(gc->parent, "GPIO %d is driven by hardware
  ", gpio);
54f300661   John Crispin   GPIO: MIPS: lanti...
139
140
  		return -ENODEV;
  	}
935c500c3   John Crispin   MIPS: Lantiq: Add...
141

54f300661   John Crispin   GPIO: MIPS: lanti...
142
143
144
145
146
147
148
149
  	return 0;
  }
  
  /**
   * xway_stp_hw_init() - Configure the STP unit and enable the clock gate
   * @virt: pointer to the remapped register range
   */
  static int xway_stp_hw_init(struct xway_stp *chip)
935c500c3   John Crispin   MIPS: Lantiq: Add...
150
  {
935c500c3   John Crispin   MIPS: Lantiq: Add...
151
  	/* sane defaults */
54f300661   John Crispin   GPIO: MIPS: lanti...
152
153
154
155
156
  	xway_stp_w32(chip->virt, 0, XWAY_STP_AR);
  	xway_stp_w32(chip->virt, 0, XWAY_STP_CPU0);
  	xway_stp_w32(chip->virt, 0, XWAY_STP_CPU1);
  	xway_stp_w32(chip->virt, XWAY_STP_CON_SWU, XWAY_STP_CON0);
  	xway_stp_w32(chip->virt, 0, XWAY_STP_CON1);
935c500c3   John Crispin   MIPS: Lantiq: Add...
157

54f300661   John Crispin   GPIO: MIPS: lanti...
158
159
160
  	/* apply edge trigger settings for the shift register */
  	xway_stp_w32_mask(chip->virt, XWAY_STP_EDGE_MASK,
  				chip->edge, XWAY_STP_CON0);
935c500c3   John Crispin   MIPS: Lantiq: Add...
161

54f300661   John Crispin   GPIO: MIPS: lanti...
162
163
164
  	/* apply led group settings */
  	xway_stp_w32_mask(chip->virt, XWAY_STP_GROUP_MASK,
  				chip->groups, XWAY_STP_CON1);
935c500c3   John Crispin   MIPS: Lantiq: Add...
165

54f300661   John Crispin   GPIO: MIPS: lanti...
166
167
168
169
170
  	/* tell the hardware which pins are controlled by the dsl modem */
  	xway_stp_w32_mask(chip->virt,
  			XWAY_STP_ADSL_MASK << XWAY_STP_ADSL_SHIFT,
  			chip->dsl << XWAY_STP_ADSL_SHIFT,
  			XWAY_STP_CON0);
935c500c3   John Crispin   MIPS: Lantiq: Add...
171

54f300661   John Crispin   GPIO: MIPS: lanti...
172
173
174
175
176
177
178
179
180
  	/* tell the hardware which pins are controlled by the phys */
  	xway_stp_w32_mask(chip->virt,
  			XWAY_STP_PHY_MASK << XWAY_STP_PHY1_SHIFT,
  			chip->phy1 << XWAY_STP_PHY1_SHIFT,
  			XWAY_STP_CON0);
  	xway_stp_w32_mask(chip->virt,
  			XWAY_STP_PHY_MASK << XWAY_STP_PHY2_SHIFT,
  			chip->phy2 << XWAY_STP_PHY2_SHIFT,
  			XWAY_STP_CON1);
935c500c3   John Crispin   MIPS: Lantiq: Add...
181

54f300661   John Crispin   GPIO: MIPS: lanti...
182
183
184
185
186
187
  	/* mask out the hw driven bits in gpio_request */
  	chip->reserved = (chip->phy2 << 5) | (chip->phy1 << 2) | chip->dsl;
  
  	/*
  	 * if we have pins that are driven by hw, we need to tell the stp what
  	 * clock to use as a timer.
935c500c3   John Crispin   MIPS: Lantiq: Add...
188
  	 */
54f300661   John Crispin   GPIO: MIPS: lanti...
189
190
191
  	if (chip->reserved)
  		xway_stp_w32_mask(chip->virt, XWAY_STP_UPD_MASK,
  			XWAY_STP_UPD_FPI, XWAY_STP_CON1);
935c500c3   John Crispin   MIPS: Lantiq: Add...
192

935c500c3   John Crispin   MIPS: Lantiq: Add...
193
194
  	return 0;
  }
3836309d9   Bill Pemberton   gpio: remove use ...
195
  static int xway_stp_probe(struct platform_device *pdev)
935c500c3   John Crispin   MIPS: Lantiq: Add...
196
  {
d9b53c3c4   Varka Bhadram   gpio: gpio-stp-xw...
197
  	struct resource *res;
50f090739   Martin Blumenstingl   gpio: stp-xway: U...
198
  	u32 shadow, groups, dsl, phy;
54f300661   John Crispin   GPIO: MIPS: lanti...
199
200
  	struct xway_stp *chip;
  	struct clk *clk;
935c500c3   John Crispin   MIPS: Lantiq: Add...
201
  	int ret = 0;
54f300661   John Crispin   GPIO: MIPS: lanti...
202
203
204
  	chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
  	if (!chip)
  		return -ENOMEM;
d9b53c3c4   Varka Bhadram   gpio: gpio-stp-xw...
205
  	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
641d03422   Thierry Reding   gpio: Convert to ...
206
207
208
  	chip->virt = devm_ioremap_resource(&pdev->dev, res);
  	if (IS_ERR(chip->virt))
  		return PTR_ERR(chip->virt);
8ab2a6d20   Laurent Navet   gpio: gpio-stp-xw...
209

58383c784   Linus Walleij   gpio: change memb...
210
  	chip->gc.parent = &pdev->dev;
54f300661   John Crispin   GPIO: MIPS: lanti...
211
212
213
214
215
216
217
218
  	chip->gc.label = "stp-xway";
  	chip->gc.direction_output = xway_stp_dir_out;
  	chip->gc.set = xway_stp_set;
  	chip->gc.request = xway_stp_request;
  	chip->gc.base = -1;
  	chip->gc.owner = THIS_MODULE;
  
  	/* store the shadow value if one was passed by the devicetree */
50f090739   Martin Blumenstingl   gpio: stp-xway: U...
219
220
  	if (!of_property_read_u32(pdev->dev.of_node, "lantiq,shadow", &shadow))
  		chip->shadow = shadow;
54f300661   John Crispin   GPIO: MIPS: lanti...
221
222
  
  	/* find out which gpio groups should be enabled */
50f090739   Martin Blumenstingl   gpio: stp-xway: U...
223
224
  	if (!of_property_read_u32(pdev->dev.of_node, "lantiq,groups", &groups))
  		chip->groups = groups & XWAY_STP_GROUP_MASK;
54f300661   John Crispin   GPIO: MIPS: lanti...
225
226
227
228
229
  	else
  		chip->groups = XWAY_STP_GROUP0;
  	chip->gc.ngpio = fls(chip->groups) * 8;
  
  	/* find out which gpios are controlled by the dsl core */
50f090739   Martin Blumenstingl   gpio: stp-xway: U...
230
231
  	if (!of_property_read_u32(pdev->dev.of_node, "lantiq,dsl", &dsl))
  		chip->dsl = dsl & XWAY_STP_ADSL_MASK;
54f300661   John Crispin   GPIO: MIPS: lanti...
232
233
234
235
236
  
  	/* find out which gpios are controlled by the phys */
  	if (of_machine_is_compatible("lantiq,ar9") ||
  			of_machine_is_compatible("lantiq,gr9") ||
  			of_machine_is_compatible("lantiq,vr9")) {
50f090739   Martin Blumenstingl   gpio: stp-xway: U...
237
238
239
240
  		if (!of_property_read_u32(pdev->dev.of_node, "lantiq,phy1", &phy))
  			chip->phy1 = phy & XWAY_STP_PHY_MASK;
  		if (!of_property_read_u32(pdev->dev.of_node, "lantiq,phy2", &phy))
  			chip->phy2 = phy & XWAY_STP_PHY_MASK;
54f300661   John Crispin   GPIO: MIPS: lanti...
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
  	}
  
  	/* check which edge trigger we should use, default to a falling edge */
  	if (!of_find_property(pdev->dev.of_node, "lantiq,rising", NULL))
  		chip->edge = XWAY_STP_FALLING;
  
  	clk = clk_get(&pdev->dev, NULL);
  	if (IS_ERR(clk)) {
  		dev_err(&pdev->dev, "Failed to get clock
  ");
  		return PTR_ERR(clk);
  	}
  	clk_enable(clk);
  
  	ret = xway_stp_hw_init(chip);
935c500c3   John Crispin   MIPS: Lantiq: Add...
256
  	if (!ret)
1a20cb2d7   Laxman Dewangan   gpio: stp-xway: U...
257
  		ret = devm_gpiochip_add_data(&pdev->dev, &chip->gc, chip);
54f300661   John Crispin   GPIO: MIPS: lanti...
258
259
260
261
  
  	if (!ret)
  		dev_info(&pdev->dev, "Init done
  ");
935c500c3   John Crispin   MIPS: Lantiq: Add...
262
263
264
  
  	return ret;
  }
54f300661   John Crispin   GPIO: MIPS: lanti...
265
266
267
268
269
270
271
272
  static const struct of_device_id xway_stp_match[] = {
  	{ .compatible = "lantiq,gpio-stp-xway" },
  	{},
  };
  MODULE_DEVICE_TABLE(of, xway_stp_match);
  
  static struct platform_driver xway_stp_driver = {
  	.probe = xway_stp_probe,
935c500c3   John Crispin   MIPS: Lantiq: Add...
273
  	.driver = {
54f300661   John Crispin   GPIO: MIPS: lanti...
274
  		.name = "gpio-stp-xway",
54f300661   John Crispin   GPIO: MIPS: lanti...
275
  		.of_match_table = xway_stp_match,
935c500c3   John Crispin   MIPS: Lantiq: Add...
276
277
  	},
  };
afdadc06d   Linus Walleij   gpio: staticize x...
278
  static int __init xway_stp_init(void)
935c500c3   John Crispin   MIPS: Lantiq: Add...
279
  {
54f300661   John Crispin   GPIO: MIPS: lanti...
280
  	return platform_driver_register(&xway_stp_driver);
935c500c3   John Crispin   MIPS: Lantiq: Add...
281
  }
54f300661   John Crispin   GPIO: MIPS: lanti...
282
  subsys_initcall(xway_stp_init);