1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
|
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright 2021 Nicolas Saenz Julienne <nsaenzjulienne@suse.de>
* For more information on Raspberry Pi's PoE hat see:
* https://www.raspberrypi.org/products/poe-hat/
*
* Limitations:
* - No disable bit, so a disabled PWM is simulated by duty_cycle 0
* - Only normal polarity
* - Fixed 12.5 kHz period
*
* The current period is completed when HW is reconfigured.
*/
#include <linux/module.h>
#include <linux/of.h>
#include <linux/platform_device.h>
#include <linux/pwm.h>
#include <soc/bcm2835/raspberrypi-firmware.h>
#include <dt-bindings/pwm/raspberrypi,firmware-poe-pwm.h>
#define RPI_PWM_MAX_DUTY 255
#define RPI_PWM_PERIOD_NS 80000 /* 12.5 kHz */
#define RPI_PWM_CUR_DUTY_REG 0x0
struct raspberrypi_pwm {
struct rpi_firmware *firmware;
struct pwm_chip chip;
unsigned int duty_cycle;
};
struct raspberrypi_pwm_prop {
__le32 reg;
__le32 val;
__le32 ret;
} __packed;
static inline
struct raspberrypi_pwm *raspberrypi_pwm_from_chip(struct pwm_chip *chip)
{
return container_of(chip, struct raspberrypi_pwm, chip);
}
static int raspberrypi_pwm_set_property(struct rpi_firmware *firmware,
u32 reg, u32 val)
{
struct raspberrypi_pwm_prop msg = {
.reg = cpu_to_le32(reg),
.val = cpu_to_le32(val),
};
int ret;
ret = rpi_firmware_property(firmware, RPI_FIRMWARE_SET_POE_HAT_VAL,
&msg, sizeof(msg));
if (ret)
return ret;
if (msg.ret)
return -EIO;
return 0;
}
static int raspberrypi_pwm_get_property(struct rpi_firmware *firmware,
u32 reg, u32 *val)
{
struct raspberrypi_pwm_prop msg = {
.reg = cpu_to_le32(reg),
};
int ret;
ret = rpi_firmware_property(firmware, RPI_FIRMWARE_GET_POE_HAT_VAL,
&msg, sizeof(msg));
if (ret)
return ret;
if (msg.ret)
return -EIO;
*val = le32_to_cpu(msg.val);
return 0;
}
static int raspberrypi_pwm_get_state(struct pwm_chip *chip,
struct pwm_device *pwm,
struct pwm_state *state)
{
struct raspberrypi_pwm *rpipwm = raspberrypi_pwm_from_chip(chip);
state->period = RPI_PWM_PERIOD_NS;
state->duty_cycle = DIV_ROUND_UP(rpipwm->duty_cycle * RPI_PWM_PERIOD_NS,
RPI_PWM_MAX_DUTY);
state->enabled = !!(rpipwm->duty_cycle);
state->polarity = PWM_POLARITY_NORMAL;
return 0;
}
static int raspberrypi_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
const struct pwm_state *state)
{
struct raspberrypi_pwm *rpipwm = raspberrypi_pwm_from_chip(chip);
unsigned int duty_cycle;
int ret;
if (state->period < RPI_PWM_PERIOD_NS ||
state->polarity != PWM_POLARITY_NORMAL)
return -EINVAL;
if (!state->enabled)
duty_cycle = 0;
else if (state->duty_cycle < RPI_PWM_PERIOD_NS)
duty_cycle = DIV_ROUND_DOWN_ULL(state->duty_cycle * RPI_PWM_MAX_DUTY,
RPI_PWM_PERIOD_NS);
else
duty_cycle = RPI_PWM_MAX_DUTY;
if (duty_cycle == rpipwm->duty_cycle)
return 0;
ret = raspberrypi_pwm_set_property(rpipwm->firmware, RPI_PWM_CUR_DUTY_REG,
duty_cycle);
if (ret) {
dev_err(chip->dev, "Failed to set duty cycle: %pe\n",
ERR_PTR(ret));
return ret;
}
rpipwm->duty_cycle = duty_cycle;
return 0;
}
static const struct pwm_ops raspberrypi_pwm_ops = {
.get_state = raspberrypi_pwm_get_state,
.apply = raspberrypi_pwm_apply,
};
static int raspberrypi_pwm_probe(struct platform_device *pdev)
{
struct device_node *firmware_node;
struct device *dev = &pdev->dev;
struct rpi_firmware *firmware;
struct raspberrypi_pwm *rpipwm;
int ret;
firmware_node = of_get_parent(dev->of_node);
if (!firmware_node) {
dev_err(dev, "Missing firmware node\n");
return -ENOENT;
}
firmware = devm_rpi_firmware_get(&pdev->dev, firmware_node);
of_node_put(firmware_node);
if (!firmware)
return dev_err_probe(dev, -EPROBE_DEFER,
"Failed to get firmware handle\n");
rpipwm = devm_kzalloc(&pdev->dev, sizeof(*rpipwm), GFP_KERNEL);
if (!rpipwm)
return -ENOMEM;
rpipwm->firmware = firmware;
rpipwm->chip.dev = dev;
rpipwm->chip.ops = &raspberrypi_pwm_ops;
rpipwm->chip.npwm = RASPBERRYPI_FIRMWARE_PWM_NUM;
ret = raspberrypi_pwm_get_property(rpipwm->firmware, RPI_PWM_CUR_DUTY_REG,
&rpipwm->duty_cycle);
if (ret) {
dev_err(dev, "Failed to get duty cycle: %pe\n", ERR_PTR(ret));
return ret;
}
return devm_pwmchip_add(dev, &rpipwm->chip);
}
static const struct of_device_id raspberrypi_pwm_of_match[] = {
{ .compatible = "raspberrypi,firmware-poe-pwm", },
{ }
};
MODULE_DEVICE_TABLE(of, raspberrypi_pwm_of_match);
static struct platform_driver raspberrypi_pwm_driver = {
.driver = {
.name = "raspberrypi-poe-pwm",
.of_match_table = raspberrypi_pwm_of_match,
},
.probe = raspberrypi_pwm_probe,
};
module_platform_driver(raspberrypi_pwm_driver);
MODULE_AUTHOR("Nicolas Saenz Julienne <nsaenzjulienne@suse.de>");
MODULE_DESCRIPTION("Raspberry Pi Firmware Based PWM Bus Driver");
MODULE_LICENSE("GPL v2");
|