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>
19 #include <linux/regmap.h>
21 #include <soc/bcm2835/raspberrypi-firmware.h>
22 #include <dt-bindings/pwm/raspberrypi,firmware-poe-pwm.h>
24 #define RPI_PWM_MAX_DUTY 255
25 #define RPI_PWM_PERIOD_NS 80000 /* 12.5 kHz */
27 #define RPI_PWM_CUR_DUTY_REG 0x0
29 struct raspberrypi_pwm {
30 struct rpi_firmware *firmware;
32 struct regmap *regmap;
36 unsigned int duty_cycle;
39 struct raspberrypi_pwm_prop {
46 struct raspberrypi_pwm *raspberrypi_pwm_from_chip(struct pwm_chip *chip)
48 return container_of(chip, struct raspberrypi_pwm, chip);
51 static int raspberrypi_pwm_set_property(struct raspberrypi_pwm *pwm,
54 struct raspberrypi_pwm_prop msg = {
55 .reg = cpu_to_le32(reg),
56 .val = cpu_to_le32(val),
61 ret = rpi_firmware_property(pwm->firmware, RPI_FIRMWARE_SET_POE_HAT_VAL,
66 ret = regmap_write(pwm->regmap, pwm->offset + reg, val);
72 static int raspberrypi_pwm_get_property(struct raspberrypi_pwm *pwm,
75 struct raspberrypi_pwm_prop msg = {
76 .reg = cpu_to_le32(reg),
81 ret = rpi_firmware_property(pwm->firmware, RPI_FIRMWARE_GET_POE_HAT_VAL,
85 *val = le32_to_cpu(msg.val);
87 ret = regmap_read(pwm->regmap, pwm->offset + reg, val);
93 static void raspberrypi_pwm_get_state(struct pwm_chip *chip,
94 struct pwm_device *pwm,
95 struct pwm_state *state)
97 struct raspberrypi_pwm *rpipwm = raspberrypi_pwm_from_chip(chip);
99 state->period = RPI_PWM_PERIOD_NS;
100 state->duty_cycle = DIV_ROUND_UP(rpipwm->duty_cycle * RPI_PWM_PERIOD_NS,
102 state->enabled = !!(rpipwm->duty_cycle);
103 state->polarity = PWM_POLARITY_NORMAL;
106 static int raspberrypi_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
107 const struct pwm_state *state)
109 struct raspberrypi_pwm *rpipwm = raspberrypi_pwm_from_chip(chip);
110 unsigned int duty_cycle;
113 if (state->period < RPI_PWM_PERIOD_NS ||
114 state->polarity != PWM_POLARITY_NORMAL)
119 else if (state->duty_cycle < RPI_PWM_PERIOD_NS)
120 duty_cycle = DIV_ROUND_DOWN_ULL(state->duty_cycle * RPI_PWM_MAX_DUTY,
123 duty_cycle = RPI_PWM_MAX_DUTY;
125 if (duty_cycle == rpipwm->duty_cycle)
128 ret = raspberrypi_pwm_set_property(rpipwm, RPI_PWM_CUR_DUTY_REG,
131 dev_err(chip->dev, "Failed to set duty cycle: %pe\n",
136 rpipwm->duty_cycle = duty_cycle;
141 static const struct pwm_ops raspberrypi_pwm_ops = {
142 .get_state = raspberrypi_pwm_get_state,
143 .apply = raspberrypi_pwm_apply,
144 .owner = THIS_MODULE,
147 static int raspberrypi_pwm_probe(struct platform_device *pdev)
149 struct device_node *firmware_node;
150 struct device *dev = &pdev->dev;
151 struct rpi_firmware *firmware;
152 struct raspberrypi_pwm *rpipwm;
155 rpipwm = devm_kzalloc(&pdev->dev, sizeof(*rpipwm), GFP_KERNEL);
159 if (pdev->dev.parent)
160 rpipwm->regmap = dev_get_regmap(pdev->dev.parent, NULL);
162 if (rpipwm->regmap) {
163 ret = device_property_read_u32(&pdev->dev, "reg", &rpipwm->offset);
167 firmware_node = of_get_parent(dev->of_node);
169 firmware = devm_rpi_firmware_get(&pdev->dev, firmware_node);
170 of_node_put(firmware_node);
172 return dev_err_probe(dev, -EPROBE_DEFER,
173 "Failed to get firmware handle\n");
175 rpipwm->firmware = firmware;
178 rpipwm->chip.dev = dev;
179 rpipwm->chip.ops = &raspberrypi_pwm_ops;
180 rpipwm->chip.base = -1;
181 rpipwm->chip.npwm = RASPBERRYPI_FIRMWARE_PWM_NUM;
183 ret = raspberrypi_pwm_get_property(rpipwm, RPI_PWM_CUR_DUTY_REG,
184 &rpipwm->duty_cycle);
186 dev_err(dev, "Failed to get duty cycle: %pe\n", ERR_PTR(ret));
190 return devm_pwmchip_add(dev, &rpipwm->chip);
193 static const struct of_device_id raspberrypi_pwm_of_match[] = {
194 { .compatible = "raspberrypi,firmware-poe-pwm", },
195 { .compatible = "raspberrypi,poe-pwm", },
198 MODULE_DEVICE_TABLE(of, raspberrypi_pwm_of_match);
200 static struct platform_driver raspberrypi_pwm_driver = {
202 .name = "raspberrypi-poe-pwm",
203 .of_match_table = raspberrypi_pwm_of_match,
205 .probe = raspberrypi_pwm_probe,
207 module_platform_driver(raspberrypi_pwm_driver);
209 MODULE_AUTHOR("Nicolas Saenz Julienne <nsaenzjulienne@suse.de>");
210 MODULE_DESCRIPTION("Raspberry Pi Firmware Based PWM Bus Driver");
211 MODULE_LICENSE("GPL v2");