1 // SPDX-License-Identifier: GPL-2.0
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/
8 * - No disable bit, so a disabled PWM is simulated by duty_cycle 0
9 * - Only normal polarity
10 * - Fixed 12.5 kHz period
12 * The current period is completed when HW is reconfigured.
15 #include <linux/module.h>
17 #include <linux/platform_device.h>
18 #include <linux/pwm.h>
20 #include <soc/bcm2835/raspberrypi-firmware.h>
21 #include <dt-bindings/pwm/raspberrypi,firmware-poe-pwm.h>
23 #define RPI_PWM_MAX_DUTY 255
24 #define RPI_PWM_PERIOD_NS 80000 /* 12.5 kHz */
26 #define RPI_PWM_CUR_DUTY_REG 0x0
28 struct raspberrypi_pwm {
29 struct rpi_firmware *firmware;
31 unsigned int duty_cycle;
34 struct raspberrypi_pwm_prop {
41 struct raspberrypi_pwm *raspberrypi_pwm_from_chip(struct pwm_chip *chip)
43 return container_of(chip, struct raspberrypi_pwm, chip);
46 static int raspberrypi_pwm_set_property(struct rpi_firmware *firmware,
49 struct raspberrypi_pwm_prop msg = {
50 .reg = cpu_to_le32(reg),
51 .val = cpu_to_le32(val),
55 ret = rpi_firmware_property(firmware, RPI_FIRMWARE_SET_POE_HAT_VAL,
65 static int raspberrypi_pwm_get_property(struct rpi_firmware *firmware,
68 struct raspberrypi_pwm_prop msg = {
69 .reg = cpu_to_le32(reg),
73 ret = rpi_firmware_property(firmware, RPI_FIRMWARE_GET_POE_HAT_VAL,
80 *val = le32_to_cpu(msg.val);
85 static void raspberrypi_pwm_get_state(struct pwm_chip *chip,
86 struct pwm_device *pwm,
87 struct pwm_state *state)
89 struct raspberrypi_pwm *rpipwm = raspberrypi_pwm_from_chip(chip);
91 state->period = RPI_PWM_PERIOD_NS;
92 state->duty_cycle = DIV_ROUND_UP(rpipwm->duty_cycle * RPI_PWM_PERIOD_NS,
94 state->enabled = !!(rpipwm->duty_cycle);
95 state->polarity = PWM_POLARITY_NORMAL;
98 static int raspberrypi_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
99 const struct pwm_state *state)
101 struct raspberrypi_pwm *rpipwm = raspberrypi_pwm_from_chip(chip);
102 unsigned int duty_cycle;
105 if (state->period < RPI_PWM_PERIOD_NS ||
106 state->polarity != PWM_POLARITY_NORMAL)
111 else if (state->duty_cycle < RPI_PWM_PERIOD_NS)
112 duty_cycle = DIV_ROUND_DOWN_ULL(state->duty_cycle * RPI_PWM_MAX_DUTY,
115 duty_cycle = RPI_PWM_MAX_DUTY;
117 if (duty_cycle == rpipwm->duty_cycle)
120 ret = raspberrypi_pwm_set_property(rpipwm->firmware, RPI_PWM_CUR_DUTY_REG,
123 dev_err(chip->dev, "Failed to set duty cycle: %pe\n",
128 rpipwm->duty_cycle = duty_cycle;
133 static const struct pwm_ops raspberrypi_pwm_ops = {
134 .get_state = raspberrypi_pwm_get_state,
135 .apply = raspberrypi_pwm_apply,
136 .owner = THIS_MODULE,
139 static int raspberrypi_pwm_probe(struct platform_device *pdev)
141 struct device_node *firmware_node;
142 struct device *dev = &pdev->dev;
143 struct rpi_firmware *firmware;
144 struct raspberrypi_pwm *rpipwm;
147 firmware_node = of_get_parent(dev->of_node);
148 if (!firmware_node) {
149 dev_err(dev, "Missing firmware node\n");
153 firmware = devm_rpi_firmware_get(&pdev->dev, firmware_node);
154 of_node_put(firmware_node);
156 return dev_err_probe(dev, -EPROBE_DEFER,
157 "Failed to get firmware handle\n");
159 rpipwm = devm_kzalloc(&pdev->dev, sizeof(*rpipwm), GFP_KERNEL);
163 rpipwm->firmware = firmware;
164 rpipwm->chip.dev = dev;
165 rpipwm->chip.ops = &raspberrypi_pwm_ops;
166 rpipwm->chip.npwm = RASPBERRYPI_FIRMWARE_PWM_NUM;
168 ret = raspberrypi_pwm_get_property(rpipwm->firmware, RPI_PWM_CUR_DUTY_REG,
169 &rpipwm->duty_cycle);
171 dev_err(dev, "Failed to get duty cycle: %pe\n", ERR_PTR(ret));
175 return devm_pwmchip_add(dev, &rpipwm->chip);
178 static const struct of_device_id raspberrypi_pwm_of_match[] = {
179 { .compatible = "raspberrypi,firmware-poe-pwm", },
182 MODULE_DEVICE_TABLE(of, raspberrypi_pwm_of_match);
184 static struct platform_driver raspberrypi_pwm_driver = {
186 .name = "raspberrypi-poe-pwm",
187 .of_match_table = raspberrypi_pwm_of_match,
189 .probe = raspberrypi_pwm_probe,
191 module_platform_driver(raspberrypi_pwm_driver);
193 MODULE_AUTHOR("Nicolas Saenz Julienne <nsaenzjulienne@suse.de>");
194 MODULE_DESCRIPTION("Raspberry Pi Firmware Based PWM Bus Driver");
195 MODULE_LICENSE("GPL v2");