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