Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Copyright 2021 Nicolas Saenz Julienne <nsaenzjulienne@suse.de>
0004  * For more information on Raspberry Pi's PoE hat see:
0005  * https://www.raspberrypi.org/products/poe-hat/
0006  *
0007  * Limitations:
0008  *  - No disable bit, so a disabled PWM is simulated by duty_cycle 0
0009  *  - Only normal polarity
0010  *  - Fixed 12.5 kHz period
0011  *
0012  * The current period is completed when HW is reconfigured.
0013  */
0014 
0015 #include <linux/module.h>
0016 #include <linux/of.h>
0017 #include <linux/platform_device.h>
0018 #include <linux/pwm.h>
0019 
0020 #include <soc/bcm2835/raspberrypi-firmware.h>
0021 #include <dt-bindings/pwm/raspberrypi,firmware-poe-pwm.h>
0022 
0023 #define RPI_PWM_MAX_DUTY        255
0024 #define RPI_PWM_PERIOD_NS       80000 /* 12.5 kHz */
0025 
0026 #define RPI_PWM_CUR_DUTY_REG        0x0
0027 
0028 struct raspberrypi_pwm {
0029     struct rpi_firmware *firmware;
0030     struct pwm_chip chip;
0031     unsigned int duty_cycle;
0032 };
0033 
0034 struct raspberrypi_pwm_prop {
0035     __le32 reg;
0036     __le32 val;
0037     __le32 ret;
0038 } __packed;
0039 
0040 static inline
0041 struct raspberrypi_pwm *raspberrypi_pwm_from_chip(struct pwm_chip *chip)
0042 {
0043     return container_of(chip, struct raspberrypi_pwm, chip);
0044 }
0045 
0046 static int raspberrypi_pwm_set_property(struct rpi_firmware *firmware,
0047                     u32 reg, u32 val)
0048 {
0049     struct raspberrypi_pwm_prop msg = {
0050         .reg = cpu_to_le32(reg),
0051         .val = cpu_to_le32(val),
0052     };
0053     int ret;
0054 
0055     ret = rpi_firmware_property(firmware, RPI_FIRMWARE_SET_POE_HAT_VAL,
0056                     &msg, sizeof(msg));
0057     if (ret)
0058         return ret;
0059     if (msg.ret)
0060         return -EIO;
0061 
0062     return 0;
0063 }
0064 
0065 static int raspberrypi_pwm_get_property(struct rpi_firmware *firmware,
0066                     u32 reg, u32 *val)
0067 {
0068     struct raspberrypi_pwm_prop msg = {
0069         .reg = cpu_to_le32(reg),
0070     };
0071     int ret;
0072 
0073     ret = rpi_firmware_property(firmware, RPI_FIRMWARE_GET_POE_HAT_VAL,
0074                     &msg, sizeof(msg));
0075     if (ret)
0076         return ret;
0077     if (msg.ret)
0078         return -EIO;
0079 
0080     *val = le32_to_cpu(msg.val);
0081 
0082     return 0;
0083 }
0084 
0085 static void raspberrypi_pwm_get_state(struct pwm_chip *chip,
0086                       struct pwm_device *pwm,
0087                       struct pwm_state *state)
0088 {
0089     struct raspberrypi_pwm *rpipwm = raspberrypi_pwm_from_chip(chip);
0090 
0091     state->period = RPI_PWM_PERIOD_NS;
0092     state->duty_cycle = DIV_ROUND_UP(rpipwm->duty_cycle * RPI_PWM_PERIOD_NS,
0093                      RPI_PWM_MAX_DUTY);
0094     state->enabled = !!(rpipwm->duty_cycle);
0095     state->polarity = PWM_POLARITY_NORMAL;
0096 }
0097 
0098 static int raspberrypi_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
0099                  const struct pwm_state *state)
0100 {
0101     struct raspberrypi_pwm *rpipwm = raspberrypi_pwm_from_chip(chip);
0102     unsigned int duty_cycle;
0103     int ret;
0104 
0105     if (state->period < RPI_PWM_PERIOD_NS ||
0106         state->polarity != PWM_POLARITY_NORMAL)
0107         return -EINVAL;
0108 
0109     if (!state->enabled)
0110         duty_cycle = 0;
0111     else if (state->duty_cycle < RPI_PWM_PERIOD_NS)
0112         duty_cycle = DIV_ROUND_DOWN_ULL(state->duty_cycle * RPI_PWM_MAX_DUTY,
0113                         RPI_PWM_PERIOD_NS);
0114     else
0115         duty_cycle = RPI_PWM_MAX_DUTY;
0116 
0117     if (duty_cycle == rpipwm->duty_cycle)
0118         return 0;
0119 
0120     ret = raspberrypi_pwm_set_property(rpipwm->firmware, RPI_PWM_CUR_DUTY_REG,
0121                        duty_cycle);
0122     if (ret) {
0123         dev_err(chip->dev, "Failed to set duty cycle: %pe\n",
0124             ERR_PTR(ret));
0125         return ret;
0126     }
0127 
0128     rpipwm->duty_cycle = duty_cycle;
0129 
0130     return 0;
0131 }
0132 
0133 static const struct pwm_ops raspberrypi_pwm_ops = {
0134     .get_state = raspberrypi_pwm_get_state,
0135     .apply = raspberrypi_pwm_apply,
0136     .owner = THIS_MODULE,
0137 };
0138 
0139 static int raspberrypi_pwm_probe(struct platform_device *pdev)
0140 {
0141     struct device_node *firmware_node;
0142     struct device *dev = &pdev->dev;
0143     struct rpi_firmware *firmware;
0144     struct raspberrypi_pwm *rpipwm;
0145     int ret;
0146 
0147     firmware_node = of_get_parent(dev->of_node);
0148     if (!firmware_node) {
0149         dev_err(dev, "Missing firmware node\n");
0150         return -ENOENT;
0151     }
0152 
0153     firmware = devm_rpi_firmware_get(&pdev->dev, firmware_node);
0154     of_node_put(firmware_node);
0155     if (!firmware)
0156         return dev_err_probe(dev, -EPROBE_DEFER,
0157                      "Failed to get firmware handle\n");
0158 
0159     rpipwm = devm_kzalloc(&pdev->dev, sizeof(*rpipwm), GFP_KERNEL);
0160     if (!rpipwm)
0161         return -ENOMEM;
0162 
0163     rpipwm->firmware = firmware;
0164     rpipwm->chip.dev = dev;
0165     rpipwm->chip.ops = &raspberrypi_pwm_ops;
0166     rpipwm->chip.npwm = RASPBERRYPI_FIRMWARE_PWM_NUM;
0167 
0168     ret = raspberrypi_pwm_get_property(rpipwm->firmware, RPI_PWM_CUR_DUTY_REG,
0169                        &rpipwm->duty_cycle);
0170     if (ret) {
0171         dev_err(dev, "Failed to get duty cycle: %pe\n", ERR_PTR(ret));
0172         return ret;
0173     }
0174 
0175     return devm_pwmchip_add(dev, &rpipwm->chip);
0176 }
0177 
0178 static const struct of_device_id raspberrypi_pwm_of_match[] = {
0179     { .compatible = "raspberrypi,firmware-poe-pwm", },
0180     { }
0181 };
0182 MODULE_DEVICE_TABLE(of, raspberrypi_pwm_of_match);
0183 
0184 static struct platform_driver raspberrypi_pwm_driver = {
0185     .driver = {
0186         .name = "raspberrypi-poe-pwm",
0187         .of_match_table = raspberrypi_pwm_of_match,
0188     },
0189     .probe = raspberrypi_pwm_probe,
0190 };
0191 module_platform_driver(raspberrypi_pwm_driver);
0192 
0193 MODULE_AUTHOR("Nicolas Saenz Julienne <nsaenzjulienne@suse.de>");
0194 MODULE_DESCRIPTION("Raspberry Pi Firmware Based PWM Bus Driver");
0195 MODULE_LICENSE("GPL v2");