Advertisement
Guest User

sun4i-pwm.c

a guest
Mar 17th, 2020
361
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 13.80 KB | None | 0 0
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3.  * Driver for Allwinner sun4i Pulse Width Modulation Controller
  4.  *
  5.  * Copyright (C) 2014 Alexandre Belloni <[email protected]>
  6.  */
  7.  
  8. #include <linux/bitops.h>
  9. #include <linux/clk.h>
  10. #include <linux/delay.h>
  11. #include <linux/err.h>
  12. #include <linux/io.h>
  13. #include <linux/jiffies.h>
  14. #include <linux/module.h>
  15. #include <linux/of.h>
  16. #include <linux/of_device.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/pwm.h>
  19. #include <linux/slab.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/time.h>
  22.  
  23. #define PWM_CTRL_REG        0x0
  24.  
  25. #define PWM_CH_PRD_BASE     0x4
  26. #define PWM_CH_PRD_OFFSET   0x4
  27. #define PWM_CH_PRD(ch)      (PWM_CH_PRD_BASE + PWM_CH_PRD_OFFSET * (ch))
  28.  
  29. #define PWMCH_OFFSET        15
  30. #define PWM_PRESCAL_MASK    GENMASK(3, 0)
  31. #define PWM_PRESCAL_OFF     0
  32. #define PWM_EN          BIT(4)
  33. #define PWM_ACT_STATE       BIT(5)
  34. #define PWM_CLK_GATING      BIT(6)
  35. #define PWM_MODE        BIT(7)
  36. #define PWM_PULSE       BIT(8)
  37. #define PWM_BYPASS      BIT(9)
  38.  
  39. #define PWM_RDY_BASE        28
  40. #define PWM_RDY_OFFSET      1
  41. #define PWM_RDY(ch)     BIT(PWM_RDY_BASE + PWM_RDY_OFFSET * (ch))
  42.  
  43. #define PWM_PRD(prd)        (((prd) - 1) << 16)
  44. #define PWM_PRD_MASK        GENMASK(15, 0)
  45.  
  46. #define PWM_DTY_MASK        GENMASK(15, 0)
  47.  
  48. #define PWM_REG_PRD(reg)    ((((reg) >> 16) & PWM_PRD_MASK) + 1)
  49. #define PWM_REG_DTY(reg)    ((reg) & PWM_DTY_MASK)
  50. #define PWM_REG_PRESCAL(reg, chan)  (((reg) >> ((chan) * PWMCH_OFFSET)) & PWM_PRESCAL_MASK)
  51.  
  52. #define BIT_CH(bit, chan)   ((bit) << ((chan) * PWMCH_OFFSET))
  53.  
  54. static const u32 prescaler_table[] = {
  55.     120,
  56.     180,
  57.     240,
  58.     360,
  59.     480,
  60.     0,
  61.     0,
  62.     0,
  63.     12000,
  64.     24000,
  65.     36000,
  66.     48000,
  67.     72000,
  68.     0,
  69.     0,
  70.     0, /* Actually 1 but tested separately */
  71. };
  72.  
  73. struct sun4i_pwm_data {
  74.     bool has_prescaler_bypass;
  75.     unsigned int npwm;
  76. };
  77.  
  78. struct sun4i_pwm_chip {
  79.     struct pwm_chip chip;
  80.     struct clk *clk;
  81.     void __iomem *base;
  82.     spinlock_t ctrl_lock;
  83.     const struct sun4i_pwm_data *data;
  84. };
  85.  
  86. static inline struct sun4i_pwm_chip *to_sun4i_pwm_chip(struct pwm_chip *chip)
  87. {
  88.     return container_of(chip, struct sun4i_pwm_chip, chip);
  89. }
  90.  
  91. static inline u32 sun4i_pwm_readl(struct sun4i_pwm_chip *chip,
  92.                   unsigned long offset)
  93. {
  94.     return readl(chip->base + offset);
  95. }
  96.  
  97. static inline void sun4i_pwm_writel(struct sun4i_pwm_chip *chip,
  98.                     u32 val, unsigned long offset)
  99. {
  100.     writel(val, chip->base + offset);
  101. }
  102.  
  103. static inline void sun4i_pwm_sleep_cycles(u32 prescaler_value,
  104.                       u32 clk_rate, u32 num_cycles)
  105. {
  106.     u64 tmp;
  107.     u32 ns;
  108.  
  109.     tmp = (u64)NSEC_PER_SEC * prescaler_value;
  110.     do_div(tmp, clk_rate);
  111.     ns = (u32)tmp * num_cycles;
  112.  
  113.     if (ns / 1000000U > MAX_UDELAY_MS) {
  114.         msleep(ns / 1000000U);
  115.     } else {
  116.         ndelay(ns);
  117.     }
  118. }
  119.  
  120. static void sun4i_pwm_get_state(struct pwm_chip *chip,
  121.                 struct pwm_device *pwm,
  122.                 struct pwm_state *state)
  123. {
  124.     struct sun4i_pwm_chip *sun4i_pwm = to_sun4i_pwm_chip(chip);
  125.     u64 clk_rate, tmp;
  126.     u32 val;
  127.     unsigned int prescaler;
  128.  
  129.     clk_rate = clk_get_rate(sun4i_pwm->clk);
  130.  
  131.     val = sun4i_pwm_readl(sun4i_pwm, PWM_CTRL_REG);
  132.  
  133.     if ((PWM_REG_PRESCAL(val, pwm->hwpwm) == PWM_PRESCAL_MASK) &&
  134.         sun4i_pwm->data->has_prescaler_bypass)
  135.         prescaler = 1;
  136.     else
  137.         prescaler = prescaler_table[PWM_REG_PRESCAL(val, pwm->hwpwm)];
  138.  
  139.     if (prescaler == 0)
  140.         return;
  141.  
  142.     if (val & BIT_CH(PWM_ACT_STATE, pwm->hwpwm))
  143.         state->polarity = PWM_POLARITY_NORMAL;
  144.     else
  145.         state->polarity = PWM_POLARITY_INVERSED;
  146.  
  147.     if ((val & BIT_CH(PWM_CLK_GATING | PWM_EN, pwm->hwpwm)) ==
  148.         BIT_CH(PWM_CLK_GATING | PWM_EN, pwm->hwpwm))
  149.         state->enabled = true;
  150.     else
  151.         state->enabled = false;
  152.  
  153.     val = sun4i_pwm_readl(sun4i_pwm, PWM_CH_PRD(pwm->hwpwm));
  154.  
  155.     tmp = (u64)prescaler * NSEC_PER_SEC * PWM_REG_DTY(val);
  156.     state->duty_cycle = DIV_ROUND_CLOSEST_ULL(tmp, clk_rate);
  157.  
  158.     tmp = (u64)prescaler * NSEC_PER_SEC * PWM_REG_PRD(val);
  159.     state->period = DIV_ROUND_CLOSEST_ULL(tmp, clk_rate);
  160. }
  161.  
  162. static int sun4i_pwm_calculate(struct sun4i_pwm_chip *sun4i_pwm,
  163.                    struct pwm_state *state,
  164.                    u32 *dty, u32 *prd, unsigned int *prsclr)
  165. {
  166.     u64 clk_rate, div = 0;
  167.     unsigned int pval, prescaler = 0;
  168.  
  169.     clk_rate = clk_get_rate(sun4i_pwm->clk);
  170.  
  171.     if (sun4i_pwm->data->has_prescaler_bypass) {
  172.         /* First, test without any prescaler when available */
  173.         prescaler = PWM_PRESCAL_MASK;
  174.         pval = 1;
  175.         /*
  176.          * When not using any prescaler, the clock period in nanoseconds
  177.          * is not an integer so round it half up instead of
  178.          * truncating to get less surprising values.
  179.          */
  180.         div = clk_rate * state->period + NSEC_PER_SEC / 2;
  181.         do_div(div, NSEC_PER_SEC);
  182.         if (div - 1 > PWM_PRD_MASK)
  183.             prescaler = 0;
  184.     }
  185.  
  186.     if (prescaler == 0) {
  187.         /* Go up from the first divider */
  188.         for (prescaler = 0; prescaler < PWM_PRESCAL_MASK; prescaler++) {
  189.             if (!prescaler_table[prescaler])
  190.                 continue;
  191.             pval = prescaler_table[prescaler];
  192.             div = clk_rate;
  193.             do_div(div, pval);
  194.             div = div * state->period;
  195.             do_div(div, NSEC_PER_SEC);
  196.             if (div - 1 <= PWM_PRD_MASK)
  197.                 break;
  198.         }
  199.  
  200.         if (div - 1 > PWM_PRD_MASK)
  201.             return -EINVAL;
  202.     }
  203.  
  204.     *prd = div;
  205.     div *= state->duty_cycle;
  206.     do_div(div, state->period);
  207.     *dty = div;
  208.     *prsclr = prescaler;
  209.  
  210.     div = (u64)pval * NSEC_PER_SEC * *prd;
  211.     state->period = DIV_ROUND_CLOSEST_ULL(div, clk_rate);
  212.  
  213.     div = (u64)pval * NSEC_PER_SEC * *dty;
  214.     state->duty_cycle = DIV_ROUND_CLOSEST_ULL(div, clk_rate);
  215.  
  216.     return 0;
  217. }
  218.  
  219. static int sun4i_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  220.                struct pwm_state *state)
  221. {
  222.     struct sun4i_pwm_chip *sun4i_pwm = to_sun4i_pwm_chip(chip);
  223.     struct pwm_state cstate;
  224.     u32 ctrl;
  225.     int ret;
  226.  
  227.     pwm_get_state(pwm, &cstate);
  228.  
  229.     if (!cstate.enabled) {
  230.         ret = clk_prepare_enable(sun4i_pwm->clk);
  231.         if (ret) {
  232.             dev_err(chip->dev, "failed to enable PWM clock\n");
  233.             return ret;
  234.         }
  235.     }
  236.  
  237.     spin_lock(&sun4i_pwm->ctrl_lock);
  238.     ctrl = sun4i_pwm_readl(sun4i_pwm, PWM_CTRL_REG);
  239.  
  240.     if ((cstate.period != state->period) ||
  241.         (cstate.duty_cycle != state->duty_cycle)) {
  242.         u32 period, duty, val;
  243.         unsigned int prescaler, old_prescaler;
  244.  
  245.         ret = sun4i_pwm_calculate(sun4i_pwm, state,
  246.                       &duty, &period, &prescaler);
  247.         if (ret) {
  248.             dev_err(chip->dev, "period exceeds the maximum value\n");
  249.             spin_unlock(&sun4i_pwm->ctrl_lock);
  250.             if (!cstate.enabled)
  251.                 clk_disable_unprepare(sun4i_pwm->clk);
  252.             return ret;
  253.         }
  254.  
  255.         /* A new period register value is only loaded when a period
  256.            ends in cycle mode, or a pulse is started. We want to
  257.            avoid waiting the whole previous cycle to complete, so
  258.            the strategy is to disable cycle mode (if enabled), send
  259.            a short pulse as possible and then apply the new period
  260.            register value. If we only disable cycle mode, set new
  261.            period register and then enable cycle mode, the old period
  262.            register value is still used in the first period. */
  263.         if (cstate.enabled) {
  264.             /* First disable, then wait two clock cycles
  265.                to take effect */
  266.             ctrl &= ~BIT_CH(PWM_EN, pwm->hwpwm);
  267.             sun4i_pwm_writel(sun4i_pwm, ctrl, PWM_CTRL_REG);
  268.             spin_unlock(&sun4i_pwm->ctrl_lock);
  269.  
  270.             /* The period register value to use for the pulse; it
  271.                must be set at least one clock cycle before the pulse
  272.                starts, so do it here. */
  273.             sun4i_pwm_writel(sun4i_pwm, 0, PWM_CH_PRD(pwm->hwpwm));
  274.  
  275.             old_prescaler = prescaler_table[PWM_REG_PRESCAL(ctrl,
  276.                                 pwm->hwpwm)];
  277.             sun4i_pwm_sleep_cycles(old_prescaler,
  278.                            clk_get_rate(sun4i_pwm->clk), 2);
  279.  
  280.             spin_lock(&sun4i_pwm->ctrl_lock);
  281.             ctrl = sun4i_pwm_readl(sun4i_pwm, PWM_CTRL_REG);
  282.  
  283.             /* Gate must be off when prescaler is updated */
  284.             ctrl &= ~BIT_CH(PWM_CLK_GATING, pwm->hwpwm);
  285.             sun4i_pwm_writel(sun4i_pwm, ctrl, PWM_CTRL_REG);
  286.             ctrl &= ~BIT_CH(PWM_PRESCAL_MASK, pwm->hwpwm);
  287.             ctrl |= BIT_CH(sun4i_pwm->data->has_prescaler_bypass ?
  288.             PWM_PRESCAL_MASK : 0, pwm->hwpwm);
  289.         } else {
  290.             /* The period register value to use for the pulse; it
  291.                must be set at least one clock cycle before the pulse
  292.                starts. */
  293.             sun4i_pwm_writel(sun4i_pwm, 0, PWM_CH_PRD(pwm->hwpwm));
  294.  
  295.             ctrl &= ~BIT_CH(PWM_PRESCAL_MASK, pwm->hwpwm);
  296.             ctrl |= BIT_CH(sun4i_pwm->data->has_prescaler_bypass ?
  297.                        PWM_PRESCAL_MASK : 0, pwm->hwpwm);
  298.             ctrl |= BIT_CH(PWM_CLK_GATING, pwm->hwpwm);
  299.             sun4i_pwm_writel(sun4i_pwm, ctrl, PWM_CTRL_REG);
  300.             spin_unlock(&sun4i_pwm->ctrl_lock);
  301.  
  302.             sun4i_pwm_sleep_cycles(prescaler_table[
  303.                 sun4i_pwm->data->has_prescaler_bypass ?
  304.                 PWM_PRESCAL_MASK : 0],
  305.                 clk_get_rate(sun4i_pwm->clk), 1);
  306.  
  307.             spin_lock(&sun4i_pwm->ctrl_lock);
  308.             ctrl = sun4i_pwm_readl(sun4i_pwm, PWM_CTRL_REG);
  309.         }
  310.  
  311.         /* Period register now contains 0, clk gate is open,
  312.          * so we can send pulse. */
  313.         ctrl |= BIT_CH(PWM_EN | PWM_MODE | PWM_PULSE |
  314.                 PWM_CLK_GATING, pwm->hwpwm);
  315.         sun4i_pwm_writel(sun4i_pwm, ctrl, PWM_CTRL_REG);
  316.  
  317.         /* Set the new real period register so it is ready later.
  318.            It must be set at least two clock cycles before the
  319.            cycle mode is later enabled to be ready at that point. */
  320.         val = (duty & PWM_DTY_MASK) | PWM_PRD(period);
  321.         sun4i_pwm_writel(sun4i_pwm, val, PWM_CH_PRD(pwm->hwpwm));
  322.         spin_unlock(&sun4i_pwm->ctrl_lock);
  323.  
  324.         /* Wait for pulse to finish. */
  325.         sun4i_pwm_sleep_cycles(
  326.             prescaler_table[sun4i_pwm->data->has_prescaler_bypass ?
  327.             PWM_PRESCAL_MASK : 0], clk_get_rate(sun4i_pwm->clk), 3);
  328.  
  329.         spin_lock(&sun4i_pwm->ctrl_lock);
  330.         ctrl = sun4i_pwm_readl(sun4i_pwm, PWM_CTRL_REG);
  331.         ctrl &= ~BIT_CH(PWM_EN | PWM_MODE, pwm->hwpwm);
  332.         sun4i_pwm_writel(sun4i_pwm, ctrl, PWM_CTRL_REG);
  333.         spin_unlock(&sun4i_pwm->ctrl_lock);
  334.  
  335.         /* In order for the new period register to be correctly loaded,
  336.            the mode must be set to cycle and disabled for at least
  337.            one clock cycle. */
  338.         sun4i_pwm_sleep_cycles(
  339.             prescaler_table[sun4i_pwm->data->has_prescaler_bypass ?
  340.             PWM_PRESCAL_MASK : 0], clk_get_rate(sun4i_pwm->clk), 1);
  341.  
  342.         spin_lock(&sun4i_pwm->ctrl_lock);
  343.         ctrl = sun4i_pwm_readl(sun4i_pwm, PWM_CTRL_REG);
  344.  
  345.         if (PWM_REG_PRESCAL(ctrl, pwm->hwpwm) != prescaler) {
  346.             /* Prescaler changed, the clock has to be gated */
  347.             ctrl &= ~BIT_CH(PWM_CLK_GATING, pwm->hwpwm);
  348.             sun4i_pwm_writel(sun4i_pwm, ctrl, PWM_CTRL_REG);
  349.  
  350.             ctrl &= ~BIT_CH(PWM_PRESCAL_MASK, pwm->hwpwm);
  351.             ctrl |= BIT_CH(prescaler, pwm->hwpwm);
  352.         } else {
  353.             ctrl &= ~BIT_CH(PWM_CLK_GATING, pwm->hwpwm);
  354.         }
  355.     } else if ((ctrl & BIT_CH(PWM_EN, pwm->hwpwm)) && !state->enabled) {
  356.         /* After disabling, we need to wait 2 clock cycles before
  357.          * closing the gate to avoid the output state remaining high. */
  358.         unsigned int prescaler;
  359.  
  360.         ctrl &= ~BIT_CH(PWM_EN, pwm->hwpwm);
  361.         sun4i_pwm_writel(sun4i_pwm, ctrl, PWM_CTRL_REG);
  362.         spin_unlock(&sun4i_pwm->ctrl_lock);
  363.         prescaler = prescaler_table[PWM_REG_PRESCAL(ctrl, pwm->hwpwm)];
  364.         sun4i_pwm_sleep_cycles(prescaler,
  365.                     clk_get_rate(sun4i_pwm->clk), 2);
  366.         spin_lock(&sun4i_pwm->ctrl_lock);
  367.         ctrl = sun4i_pwm_readl(sun4i_pwm, PWM_CTRL_REG);
  368.         ctrl &= ~BIT_CH(PWM_CLK_GATING, pwm->hwpwm);
  369.     }
  370.  
  371.     if (state->polarity != PWM_POLARITY_NORMAL)
  372.         ctrl &= ~BIT_CH(PWM_ACT_STATE, pwm->hwpwm);
  373.     else
  374.         ctrl |= BIT_CH(PWM_ACT_STATE, pwm->hwpwm);
  375.  
  376.     if (state->enabled)
  377.         ctrl |= BIT_CH(PWM_CLK_GATING | PWM_EN, pwm->hwpwm);
  378.  
  379.     sun4i_pwm_writel(sun4i_pwm, ctrl, PWM_CTRL_REG);
  380.  
  381.     spin_unlock(&sun4i_pwm->ctrl_lock);
  382.  
  383.     if (state->enabled)
  384.         return 0;
  385.  
  386.     clk_disable_unprepare(sun4i_pwm->clk);
  387.  
  388.     return 0;
  389. }
  390.  
  391. static const struct pwm_ops sun4i_pwm_ops = {
  392.     .apply = sun4i_pwm_apply,
  393.     .get_state = sun4i_pwm_get_state,
  394.     .owner = THIS_MODULE,
  395. };
  396.  
  397. static const struct sun4i_pwm_data sun4i_pwm_dual_nobypass = {
  398.     .has_prescaler_bypass = false,
  399.     .npwm = 2,
  400. };
  401.  
  402. static const struct sun4i_pwm_data sun4i_pwm_dual_bypass = {
  403.     .has_prescaler_bypass = true,
  404.     .npwm = 2,
  405. };
  406.  
  407. static const struct sun4i_pwm_data sun4i_pwm_single_bypass = {
  408.     .has_prescaler_bypass = true,
  409.     .npwm = 1,
  410. };
  411.  
  412. static const struct of_device_id sun4i_pwm_dt_ids[] = {
  413.     {
  414.         .compatible = "allwinner,sun4i-a10-pwm",
  415.         .data = &sun4i_pwm_dual_nobypass,
  416.     }, {
  417.         .compatible = "allwinner,sun5i-a10s-pwm",
  418.         .data = &sun4i_pwm_dual_bypass,
  419.     }, {
  420.         .compatible = "allwinner,sun5i-a13-pwm",
  421.         .data = &sun4i_pwm_single_bypass,
  422.     }, {
  423.         .compatible = "allwinner,sun7i-a20-pwm",
  424.         .data = &sun4i_pwm_dual_bypass,
  425.     }, {
  426.         .compatible = "allwinner,sun8i-h3-pwm",
  427.         .data = &sun4i_pwm_single_bypass,
  428.     }, {
  429.         /* sentinel */
  430.     },
  431. };
  432. MODULE_DEVICE_TABLE(of, sun4i_pwm_dt_ids);
  433.  
  434. static int sun4i_pwm_probe(struct platform_device *pdev)
  435. {
  436.     struct sun4i_pwm_chip *pwm;
  437.     struct resource *res;
  438.     int ret;
  439.  
  440.     pwm = devm_kzalloc(&pdev->dev, sizeof(*pwm), GFP_KERNEL);
  441.     if (!pwm)
  442.         return -ENOMEM;
  443.  
  444.     pwm->data = of_device_get_match_data(&pdev->dev);
  445.     if (!pwm->data)
  446.         return -ENODEV;
  447.  
  448.     res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  449.     pwm->base = devm_ioremap_resource(&pdev->dev, res);
  450.     if (IS_ERR(pwm->base))
  451.         return PTR_ERR(pwm->base);
  452.  
  453.     pwm->clk = devm_clk_get(&pdev->dev, NULL);
  454.     if (IS_ERR(pwm->clk))
  455.         return PTR_ERR(pwm->clk);
  456.  
  457.     pwm->chip.dev = &pdev->dev;
  458.     pwm->chip.ops = &sun4i_pwm_ops;
  459.     pwm->chip.base = -1;
  460.     pwm->chip.npwm = pwm->data->npwm;
  461.     pwm->chip.of_xlate = of_pwm_xlate_with_flags;
  462.     pwm->chip.of_pwm_n_cells = 3;
  463.  
  464.     spin_lock_init(&pwm->ctrl_lock);
  465.  
  466.     ret = pwmchip_add(&pwm->chip);
  467.     if (ret < 0) {
  468.         dev_err(&pdev->dev, "failed to add PWM chip: %d\n", ret);
  469.         return ret;
  470.     }
  471.  
  472.     platform_set_drvdata(pdev, pwm);
  473.  
  474.     return 0;
  475. }
  476.  
  477. static int sun4i_pwm_remove(struct platform_device *pdev)
  478. {
  479.     struct sun4i_pwm_chip *pwm = platform_get_drvdata(pdev);
  480.  
  481.     return pwmchip_remove(&pwm->chip);
  482. }
  483.  
  484. static struct platform_driver sun4i_pwm_driver = {
  485.     .driver = {
  486.         .name = "sun4i-pwm",
  487.         .of_match_table = sun4i_pwm_dt_ids,
  488.     },
  489.     .probe = sun4i_pwm_probe,
  490.     .remove = sun4i_pwm_remove,
  491. };
  492. module_platform_driver(sun4i_pwm_driver);
  493.  
  494. MODULE_ALIAS("platform:sun4i-pwm");
  495. MODULE_AUTHOR("Alexandre Belloni <[email protected]>");
  496. MODULE_DESCRIPTION("Allwinner sun4i PWM driver");
  497. MODULE_LICENSE("GPL v2");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement