Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / pwm / pwm-raspberrypi-poe.c
blob8921e7ea2ceaabffdb4d3ca00da4bc41b0a49883
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/
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
12 * The current period is completed when HW is reconfigured.
15 #include <linux/module.h>
16 #include <linux/of.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;
30 unsigned int duty_cycle;
33 struct raspberrypi_pwm_prop {
34 __le32 reg;
35 __le32 val;
36 __le32 ret;
37 } __packed;
39 static inline
40 struct raspberrypi_pwm *raspberrypi_pwm_from_chip(struct pwm_chip *chip)
42 return pwmchip_get_drvdata(chip);
45 static int raspberrypi_pwm_set_property(struct rpi_firmware *firmware,
46 u32 reg, u32 val)
48 struct raspberrypi_pwm_prop msg = {
49 .reg = cpu_to_le32(reg),
50 .val = cpu_to_le32(val),
52 int ret;
54 ret = rpi_firmware_property(firmware, RPI_FIRMWARE_SET_POE_HAT_VAL,
55 &msg, sizeof(msg));
56 if (ret)
57 return ret;
58 if (msg.ret)
59 return -EIO;
61 return 0;
64 static int raspberrypi_pwm_get_property(struct rpi_firmware *firmware,
65 u32 reg, u32 *val)
67 struct raspberrypi_pwm_prop msg = {
68 .reg = cpu_to_le32(reg),
70 int ret;
72 ret = rpi_firmware_property(firmware, RPI_FIRMWARE_GET_POE_HAT_VAL,
73 &msg, sizeof(msg));
74 if (ret)
75 return ret;
76 if (msg.ret)
77 return -EIO;
79 *val = le32_to_cpu(msg.val);
81 return 0;
84 static int raspberrypi_pwm_get_state(struct pwm_chip *chip,
85 struct pwm_device *pwm,
86 struct pwm_state *state)
88 struct raspberrypi_pwm *rpipwm = raspberrypi_pwm_from_chip(chip);
90 state->period = RPI_PWM_PERIOD_NS;
91 state->duty_cycle = DIV_ROUND_UP(rpipwm->duty_cycle * RPI_PWM_PERIOD_NS,
92 RPI_PWM_MAX_DUTY);
93 state->enabled = !!(rpipwm->duty_cycle);
94 state->polarity = PWM_POLARITY_NORMAL;
96 return 0;
99 static int raspberrypi_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
100 const struct pwm_state *state)
102 struct raspberrypi_pwm *rpipwm = raspberrypi_pwm_from_chip(chip);
103 unsigned int duty_cycle;
104 int ret;
106 if (state->period < RPI_PWM_PERIOD_NS ||
107 state->polarity != PWM_POLARITY_NORMAL)
108 return -EINVAL;
110 if (!state->enabled)
111 duty_cycle = 0;
112 else if (state->duty_cycle < RPI_PWM_PERIOD_NS)
113 duty_cycle = DIV_ROUND_DOWN_ULL(state->duty_cycle * RPI_PWM_MAX_DUTY,
114 RPI_PWM_PERIOD_NS);
115 else
116 duty_cycle = RPI_PWM_MAX_DUTY;
118 if (duty_cycle == rpipwm->duty_cycle)
119 return 0;
121 ret = raspberrypi_pwm_set_property(rpipwm->firmware, RPI_PWM_CUR_DUTY_REG,
122 duty_cycle);
123 if (ret) {
124 dev_err(pwmchip_parent(chip), "Failed to set duty cycle: %pe\n",
125 ERR_PTR(ret));
126 return ret;
129 rpipwm->duty_cycle = duty_cycle;
131 return 0;
134 static const struct pwm_ops raspberrypi_pwm_ops = {
135 .get_state = raspberrypi_pwm_get_state,
136 .apply = raspberrypi_pwm_apply,
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 pwm_chip *chip;
145 struct raspberrypi_pwm *rpipwm;
146 int ret;
148 firmware_node = of_get_parent(dev->of_node);
149 if (!firmware_node) {
150 dev_err(dev, "Missing firmware node\n");
151 return -ENOENT;
154 firmware = devm_rpi_firmware_get(&pdev->dev, firmware_node);
155 of_node_put(firmware_node);
156 if (!firmware)
157 return dev_err_probe(dev, -EPROBE_DEFER,
158 "Failed to get firmware handle\n");
160 chip = devm_pwmchip_alloc(&pdev->dev, RASPBERRYPI_FIRMWARE_PWM_NUM,
161 sizeof(*rpipwm));
162 if (IS_ERR(chip))
163 return PTR_ERR(chip);
164 rpipwm = raspberrypi_pwm_from_chip(chip);
166 rpipwm->firmware = firmware;
167 chip->ops = &raspberrypi_pwm_ops;
169 ret = raspberrypi_pwm_get_property(rpipwm->firmware, RPI_PWM_CUR_DUTY_REG,
170 &rpipwm->duty_cycle);
171 if (ret) {
172 dev_err(dev, "Failed to get duty cycle: %pe\n", ERR_PTR(ret));
173 return ret;
176 return devm_pwmchip_add(dev, chip);
179 static const struct of_device_id raspberrypi_pwm_of_match[] = {
180 { .compatible = "raspberrypi,firmware-poe-pwm", },
183 MODULE_DEVICE_TABLE(of, raspberrypi_pwm_of_match);
185 static struct platform_driver raspberrypi_pwm_driver = {
186 .driver = {
187 .name = "raspberrypi-poe-pwm",
188 .of_match_table = raspberrypi_pwm_of_match,
190 .probe = raspberrypi_pwm_probe,
192 module_platform_driver(raspberrypi_pwm_driver);
194 MODULE_AUTHOR("Nicolas Saenz Julienne <nsaenzjulienne@suse.de>");
195 MODULE_DESCRIPTION("Raspberry Pi Firmware Based PWM Bus Driver");
196 MODULE_LICENSE("GPL v2");