Skip to content

Commit c5dbcf8

Browse files
jluebbetorvalds
authored andcommitted
pps-gpio: add device-tree binding and support
Instead of allocating a struct pps_gpio_platform_data in the DT case, store the necessary information in struct pps_gpio_device_data itself. This avoids an additional allocation and the ifdef. It also gets rid of some indirection. Also use dev_err instead of pr_err in the changed code. Signed-off-by: Jan Luebbe <jlu@pengutronix.de> Acked-by: Arnd Bergmann <arnd@arndb.de> Acked-by: Rodolfo Giometti <giometti@enneenne.com> Cc: Grant Likely <grant.likely@linaro.org> Cc: Rob Herring <rob.herring@calxeda.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 05212be commit c5dbcf8

2 files changed

Lines changed: 93 additions & 54 deletions

File tree

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Device-Tree Bindings for a PPS Signal on GPIO
2+
3+
These properties describe a PPS (pulse-per-second) signal connected to
4+
a GPIO pin.
5+
6+
Required properties:
7+
- compatible: should be "pps-gpio"
8+
- gpios: one PPS GPIO in the format described by ../gpio/gpio.txt
9+
10+
Optional properties:
11+
- assert-falling-edge: when present, assert is indicated by a falling edge
12+
(instead of by a rising edge)
13+
14+
Example:
15+
pps {
16+
compatible = "pps-gpio";
17+
gpios = <&gpio2 6 0>;
18+
19+
assert-falling-edge;
20+
};

‎drivers/pps/clients/pps-gpio.c‎

Lines changed: 73 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,17 @@
3333
#include <linux/pps-gpio.h>
3434
#include <linux/gpio.h>
3535
#include <linux/list.h>
36+
#include <linux/of_device.h>
37+
#include <linux/of_gpio.h>
3638

3739
/* Info for each registered platform device */
3840
struct pps_gpio_device_data {
3941
int irq; /* IRQ used as PPS source */
4042
struct pps_device *pps; /* PPS source device */
4143
struct pps_source_info info; /* PPS source information */
42-
const struct pps_gpio_platform_data *pdata;
44+
bool assert_falling_edge;
45+
bool capture_clear;
46+
unsigned int gpio_pin;
4347
};
4448

4549
/*
@@ -57,45 +61,25 @@ static irqreturn_t pps_gpio_irq_handler(int irq, void *data)
5761

5862
info = data;
5963

60-
rising_edge = gpio_get_value(info->pdata->gpio_pin);
61-
if ((rising_edge && !info->pdata->assert_falling_edge) ||
62-
(!rising_edge && info->pdata->assert_falling_edge))
64+
rising_edge = gpio_get_value(info->gpio_pin);
65+
if ((rising_edge && !info->assert_falling_edge) ||
66+
(!rising_edge && info->assert_falling_edge))
6367
pps_event(info->pps, &ts, PPS_CAPTUREASSERT, NULL);
64-
else if (info->pdata->capture_clear &&
65-
((rising_edge && info->pdata->assert_falling_edge) ||
66-
(!rising_edge && !info->pdata->assert_falling_edge)))
68+
else if (info->capture_clear &&
69+
((rising_edge && info->assert_falling_edge) ||
70+
(!rising_edge && !info->assert_falling_edge)))
6771
pps_event(info->pps, &ts, PPS_CAPTURECLEAR, NULL);
6872

6973
return IRQ_HANDLED;
7074
}
7175

72-
static int pps_gpio_setup(struct platform_device *pdev)
73-
{
74-
int ret;
75-
const struct pps_gpio_platform_data *pdata = pdev->dev.platform_data;
76-
77-
ret = devm_gpio_request(&pdev->dev, pdata->gpio_pin, pdata->gpio_label);
78-
if (ret) {
79-
pr_warning("failed to request GPIO %u\n", pdata->gpio_pin);
80-
return -EINVAL;
81-
}
82-
83-
ret = gpio_direction_input(pdata->gpio_pin);
84-
if (ret) {
85-
pr_warning("failed to set pin direction\n");
86-
return -EINVAL;
87-
}
88-
89-
return 0;
90-
}
91-
9276
static unsigned long
93-
get_irqf_trigger_flags(const struct pps_gpio_platform_data *pdata)
77+
get_irqf_trigger_flags(const struct pps_gpio_device_data *data)
9478
{
95-
unsigned long flags = pdata->assert_falling_edge ?
79+
unsigned long flags = data->assert_falling_edge ?
9680
IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
9781

98-
if (pdata->capture_clear) {
82+
if (data->capture_clear) {
9983
flags |= ((flags & IRQF_TRIGGER_RISING) ?
10084
IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING);
10185
}
@@ -106,34 +90,63 @@ get_irqf_trigger_flags(const struct pps_gpio_platform_data *pdata)
10690
static int pps_gpio_probe(struct platform_device *pdev)
10791
{
10892
struct pps_gpio_device_data *data;
109-
int irq;
93+
const char *gpio_label;
11094
int ret;
11195
int pps_default_params;
11296
const struct pps_gpio_platform_data *pdata = pdev->dev.platform_data;
97+
struct device_node *np = pdev->dev.of_node;
98+
99+
/* allocate space for device info */
100+
data = devm_kzalloc(&pdev->dev, sizeof(struct pps_gpio_device_data),
101+
GFP_KERNEL);
102+
if (!data)
103+
return -ENOMEM;
113104

105+
if (pdata) {
106+
data->gpio_pin = pdata->gpio_pin;
107+
gpio_label = pdata->gpio_label;
108+
109+
data->assert_falling_edge = pdata->assert_falling_edge;
110+
data->capture_clear = pdata->capture_clear;
111+
} else {
112+
ret = of_get_gpio(np, 0);
113+
if (ret < 0) {
114+
dev_err(&pdev->dev, "failed to get GPIO from device tree\n");
115+
return ret;
116+
}
117+
data->gpio_pin = ret;
118+
gpio_label = PPS_GPIO_NAME;
119+
120+
if (of_get_property(np, "assert-falling-edge", NULL))
121+
data->assert_falling_edge = true;
122+
}
114123

115124
/* GPIO setup */
116-
ret = pps_gpio_setup(pdev);
117-
if (ret)
125+
ret = devm_gpio_request(&pdev->dev, data->gpio_pin, gpio_label);
126+
if (ret) {
127+
dev_err(&pdev->dev, "failed to request GPIO %u\n",
128+
data->gpio_pin);
129+
return ret;
130+
}
131+
132+
ret = gpio_direction_input(data->gpio_pin);
133+
if (ret) {
134+
dev_err(&pdev->dev, "failed to set pin direction\n");
118135
return -EINVAL;
136+
}
119137

120138
/* IRQ setup */
121-
irq = gpio_to_irq(pdata->gpio_pin);
122-
if (irq < 0) {
123-
pr_err("failed to map GPIO to IRQ: %d\n", irq);
139+
ret = gpio_to_irq(data->gpio_pin);
140+
if (ret < 0) {
141+
dev_err(&pdev->dev, "failed to map GPIO to IRQ: %d\n", ret);
124142
return -EINVAL;
125143
}
126-
127-
/* allocate space for device info */
128-
data = devm_kzalloc(&pdev->dev, sizeof(struct pps_gpio_device_data),
129-
GFP_KERNEL);
130-
if (data == NULL)
131-
return -ENOMEM;
144+
data->irq = ret;
132145

133146
/* initialize PPS specific parts of the bookkeeping data structure. */
134147
data->info.mode = PPS_CAPTUREASSERT | PPS_OFFSETASSERT |
135148
PPS_ECHOASSERT | PPS_CANWAIT | PPS_TSFMT_TSPEC;
136-
if (pdata->capture_clear)
149+
if (data->capture_clear)
137150
data->info.mode |= PPS_CAPTURECLEAR | PPS_OFFSETCLEAR |
138151
PPS_ECHOCLEAR;
139152
data->info.owner = THIS_MODULE;
@@ -142,28 +155,27 @@ static int pps_gpio_probe(struct platform_device *pdev)
142155

143156
/* register PPS source */
144157
pps_default_params = PPS_CAPTUREASSERT | PPS_OFFSETASSERT;
145-
if (pdata->capture_clear)
158+
if (data->capture_clear)
146159
pps_default_params |= PPS_CAPTURECLEAR | PPS_OFFSETCLEAR;
147160
data->pps = pps_register_source(&data->info, pps_default_params);
148161
if (data->pps == NULL) {
149-
pr_err("failed to register IRQ %d as PPS source\n", irq);
162+
dev_err(&pdev->dev, "failed to register IRQ %d as PPS source\n",
163+
data->irq);
150164
return -EINVAL;
151165
}
152166

153-
data->irq = irq;
154-
data->pdata = pdata;
155-
156167
/* register IRQ interrupt handler */
157-
ret = devm_request_irq(&pdev->dev, irq, pps_gpio_irq_handler,
158-
get_irqf_trigger_flags(pdata), data->info.name, data);
168+
ret = devm_request_irq(&pdev->dev, data->irq, pps_gpio_irq_handler,
169+
get_irqf_trigger_flags(data), data->info.name, data);
159170
if (ret) {
160171
pps_unregister_source(data->pps);
161-
pr_err("failed to acquire IRQ %d\n", irq);
172+
dev_err(&pdev->dev, "failed to acquire IRQ %d\n", data->irq);
162173
return -EINVAL;
163174
}
164175

165176
platform_set_drvdata(pdev, data);
166-
dev_info(data->pps->dev, "Registered IRQ %d as PPS source\n", irq);
177+
dev_info(data->pps->dev, "Registered IRQ %d as PPS source\n",
178+
data->irq);
167179

168180
return 0;
169181
}
@@ -174,16 +186,23 @@ static int pps_gpio_remove(struct platform_device *pdev)
174186

175187
platform_set_drvdata(pdev, NULL);
176188
pps_unregister_source(data->pps);
177-
pr_info("removed IRQ %d as PPS source\n", data->irq);
189+
dev_info(&pdev->dev, "removed IRQ %d as PPS source\n", data->irq);
178190
return 0;
179191
}
180192

193+
static const struct of_device_id pps_gpio_dt_ids[] = {
194+
{ .compatible = "pps-gpio", },
195+
{ /* sentinel */ }
196+
};
197+
MODULE_DEVICE_TABLE(of, pps_gpio_dt_ids);
198+
181199
static struct platform_driver pps_gpio_driver = {
182200
.probe = pps_gpio_probe,
183201
.remove = pps_gpio_remove,
184202
.driver = {
185203
.name = PPS_GPIO_NAME,
186-
.owner = THIS_MODULE
204+
.owner = THIS_MODULE,
205+
.of_match_table = of_match_ptr(pps_gpio_dt_ids),
187206
},
188207
};
189208

0 commit comments

Comments
 (0)