pwm: sti: Reorganise register names in preparation for new functionality
[cascardo/linux.git] / drivers / pwm / pwm-sti.c
index 92abbd5..388f2aa 100644 (file)
 #include <linux/slab.h>
 #include <linux/time.h>
 
-#define STI_DS_REG(ch) (4 * (ch))      /* Channel's Duty Cycle register */
-#define STI_PWMCR      0x50            /* Control/Config register */
-#define STI_INTEN      0x54            /* Interrupt Enable/Disable register */
+#define PWM_OUT_VAL(x) (0x00 + (4 * (x))) /* Device's Duty Cycle register */
+
+#define STI_PWM_CTRL           0x50    /* Control/Config register */
+#define STI_INT_EN             0x54    /* Interrupt Enable/Disable register */
 #define PWM_PRESCALE_LOW_MASK          0x0f
 #define PWM_PRESCALE_HIGH_MASK         0xf0
 
 /* Regfield IDs */
 enum {
+       /* Bits in PWM_CTRL*/
        PWMCLK_PRESCALE_LOW,
        PWMCLK_PRESCALE_HIGH,
-       PWM_EN,
-       PWM_INT_EN,
+
+       PWM_OUT_EN,
+
+       PWM_CPT_INT_EN,
 
        /* Keep last */
        MAX_REGFIELDS
@@ -40,21 +44,21 @@ enum {
 
 struct sti_pwm_compat_data {
        const struct reg_field *reg_fields;
-       unsigned int num_chan;
+       unsigned int num_devs;
        unsigned int max_pwm_cnt;
        unsigned int max_prescale;
 };
 
 struct sti_pwm_chip {
        struct device *dev;
-       struct clk *clk;
        unsigned long clk_rate;
+       struct clk *pwm_clk;
        struct regmap *regmap;
        struct sti_pwm_compat_data *cdata;
        struct regmap_field *prescale_low;
        struct regmap_field *prescale_high;
-       struct regmap_field *pwm_en;
-       struct regmap_field *pwm_int_en;
+       struct regmap_field *pwm_out_en;
+       struct regmap_field *pwm_cpt_int_en;
        struct pwm_chip chip;
        struct pwm_device *cur;
        unsigned long configured;
@@ -64,10 +68,10 @@ struct sti_pwm_chip {
 };
 
 static const struct reg_field sti_pwm_regfields[MAX_REGFIELDS] = {
-       [PWMCLK_PRESCALE_LOW]   = REG_FIELD(STI_PWMCR, 0, 3),
-       [PWMCLK_PRESCALE_HIGH]  = REG_FIELD(STI_PWMCR, 11, 14),
-       [PWM_EN]                = REG_FIELD(STI_PWMCR, 9, 9),
-       [PWM_INT_EN]            = REG_FIELD(STI_INTEN, 0, 0),
+       [PWMCLK_PRESCALE_LOW]   = REG_FIELD(STI_PWM_CTRL, 0, 3),
+       [PWMCLK_PRESCALE_HIGH]  = REG_FIELD(STI_PWM_CTRL, 11, 14),
+       [PWM_OUT_EN]            = REG_FIELD(STI_PWM_CTRL, 9, 9),
+       [PWM_CPT_INT_EN]        = REG_FIELD(STI_INT_EN, 1, 4),
 };
 
 static inline struct sti_pwm_chip *to_sti_pwmchip(struct pwm_chip *chip)
@@ -130,13 +134,13 @@ static int sti_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
 
        /* Allow configuration changes if one of the
         * following conditions satisfy.
-        * 1. No channels have been configured.
-        * 2. Only one channel has been configured and the new request
-        *    is for the same channel.
-        * 3. Only one channel has been configured and the new request is
-        *    for a new channel and period of the new channel is same as
+        * 1. No devices have been configured.
+        * 2. Only one device has been configured and the new request
+        *    is for the same device.
+        * 3. Only one device has been configured and the new request is
+        *    for a new device and period of the new device is same as
         *    the current configured period.
-        * 4. More than one channels are configured and period of the new
+        * 4. More than one devices are configured and period of the new
         *    requestis the same as the current period.
         */
        if (!ncfg ||
@@ -144,7 +148,7 @@ static int sti_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
            ((ncfg == 1) && (pwm->hwpwm != cur->hwpwm) && period_same) ||
            ((ncfg > 1) && period_same)) {
                /* Enable clock before writing to PWM registers. */
-               ret = clk_enable(pc->clk);
+               ret = clk_enable(pc->pwm_clk);
                if (ret)
                        return ret;
 
@@ -174,11 +178,12 @@ static int sti_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
                 */
                pwmvalx = cdata->max_pwm_cnt * duty_ns / period_ns;
 
-               ret = regmap_write(pc->regmap, STI_DS_REG(pwm->hwpwm), pwmvalx);
+               ret = regmap_write(pc->regmap,
+                                  PWM_OUT_VAL(pwm->hwpwm), pwmvalx);
                if (ret)
                        goto clk_dis;
 
-               ret = regmap_field_write(pc->pwm_int_en, 0);
+               ret = regmap_field_write(pc->pwm_cpt_int_en, 0);
 
                set_bit(pwm->hwpwm, &pc->configured);
                pc->cur = pwm;
@@ -190,7 +195,7 @@ static int sti_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
        }
 
 clk_dis:
-       clk_disable(pc->clk);
+       clk_disable(pc->pwm_clk);
        return ret;
 }
 
@@ -201,16 +206,16 @@ static int sti_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
        int ret = 0;
 
        /*
-        * Since we have a common enable for all PWM channels,
+        * Since we have a common enable for all PWM devices,
         * do not enable if already enabled.
         */
        mutex_lock(&pc->sti_pwm_lock);
        if (!pc->en_count) {
-               ret = clk_enable(pc->clk);
+               ret = clk_enable(pc->pwm_clk);
                if (ret)
                        goto out;
 
-               ret = regmap_field_write(pc->pwm_en, 1);
+               ret = regmap_field_write(pc->pwm_out_en, 1);
                if (ret) {
                        dev_err(dev, "failed to enable PWM device:%d\n",
                                pwm->hwpwm);
@@ -232,9 +237,9 @@ static void sti_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
                mutex_unlock(&pc->sti_pwm_lock);
                return;
        }
-       regmap_field_write(pc->pwm_en, 0);
+       regmap_field_write(pc->pwm_out_en, 0);
 
-       clk_disable(pc->clk);
+       clk_disable(pc->pwm_clk);
        mutex_unlock(&pc->sti_pwm_lock);
 }
 
@@ -259,11 +264,11 @@ static int sti_pwm_probe_dt(struct sti_pwm_chip *pc)
        const struct reg_field *reg_fields;
        struct device_node *np = dev->of_node;
        struct sti_pwm_compat_data *cdata = pc->cdata;
-       u32 num_chan;
+       u32 num_devs;
 
-       of_property_read_u32(np, "st,pwm-num-chan", &num_chan);
-       if (num_chan)
-               cdata->num_chan = num_chan;
+       of_property_read_u32(np, "st,pwm-num-chan", &num_devs);
+       if (num_devs)
+               cdata->num_devs = num_devs;
 
        reg_fields = cdata->reg_fields;
 
@@ -277,15 +282,16 @@ static int sti_pwm_probe_dt(struct sti_pwm_chip *pc)
        if (IS_ERR(pc->prescale_high))
                return PTR_ERR(pc->prescale_high);
 
-       pc->pwm_en = devm_regmap_field_alloc(dev, pc->regmap,
-                                            reg_fields[PWM_EN]);
-       if (IS_ERR(pc->pwm_en))
-               return PTR_ERR(pc->pwm_en);
 
-       pc->pwm_int_en = devm_regmap_field_alloc(dev, pc->regmap,
-                                                reg_fields[PWM_INT_EN]);
-       if (IS_ERR(pc->pwm_int_en))
-               return PTR_ERR(pc->pwm_int_en);
+       pc->pwm_out_en = devm_regmap_field_alloc(dev, pc->regmap,
+                                                reg_fields[PWM_OUT_EN]);
+       if (IS_ERR(pc->pwm_out_en))
+               return PTR_ERR(pc->pwm_out_en);
+
+       pc->pwm_cpt_int_en = devm_regmap_field_alloc(dev, pc->regmap,
+                                                reg_fields[PWM_CPT_INT_EN]);
+       if (IS_ERR(pc->pwm_cpt_int_en))
+               return PTR_ERR(pc->pwm_cpt_int_en);
 
        return 0;
 }
@@ -330,7 +336,7 @@ static int sti_pwm_probe(struct platform_device *pdev)
        cdata->reg_fields   = &sti_pwm_regfields[0];
        cdata->max_prescale = 0xff;
        cdata->max_pwm_cnt  = 255;
-       cdata->num_chan     = 1;
+       cdata->num_devs     = 1;
 
        pc->cdata = cdata;
        pc->dev = dev;
@@ -341,19 +347,19 @@ static int sti_pwm_probe(struct platform_device *pdev)
        if (ret)
                return ret;
 
-       pc->clk = of_clk_get_by_name(dev->of_node, "pwm");
-       if (IS_ERR(pc->clk)) {
+       pc->pwm_clk = of_clk_get_by_name(dev->of_node, "pwm");
+       if (IS_ERR(pc->pwm_clk)) {
                dev_err(dev, "failed to get PWM clock\n");
-               return PTR_ERR(pc->clk);
+               return PTR_ERR(pc->pwm_clk);
        }
 
-       pc->clk_rate = clk_get_rate(pc->clk);
+       pc->clk_rate = clk_get_rate(pc->pwm_clk);
        if (!pc->clk_rate) {
                dev_err(dev, "failed to get clock rate\n");
                return -EINVAL;
        }
 
-       ret = clk_prepare(pc->clk);
+       ret = clk_prepare(pc->pwm_clk);
        if (ret) {
                dev_err(dev, "failed to prepare clock\n");
                return ret;
@@ -362,12 +368,12 @@ static int sti_pwm_probe(struct platform_device *pdev)
        pc->chip.dev = dev;
        pc->chip.ops = &sti_pwm_ops;
        pc->chip.base = -1;
-       pc->chip.npwm = pc->cdata->num_chan;
+       pc->chip.npwm = pc->cdata->num_devs;
        pc->chip.can_sleep = true;
 
        ret = pwmchip_add(&pc->chip);
        if (ret < 0) {
-               clk_unprepare(pc->clk);
+               clk_unprepare(pc->pwm_clk);
                return ret;
        }
 
@@ -381,10 +387,10 @@ static int sti_pwm_remove(struct platform_device *pdev)
        struct sti_pwm_chip *pc = platform_get_drvdata(pdev);
        unsigned int i;
 
-       for (i = 0; i < pc->cdata->num_chan; i++)
+       for (i = 0; i < pc->cdata->num_devs; i++)
                pwm_disable(&pc->chip.pwms[i]);
 
-       clk_unprepare(pc->clk);
+       clk_unprepare(pc->pwm_clk);
 
        return pwmchip_remove(&pc->chip);
 }