]> Git Repo - J-linux.git/blobdiff - drivers/hwmon/pwm-fan.c
Merge remote-tracking branch 'spi/for-5.14' into spi-linus
[J-linux.git] / drivers / hwmon / pwm-fan.c
index 111a91dc6b798e31ae6ff9998e4eea0428943bc5..17518b4cab1b007092dd4dd3973ebabe2e6b6e43 100644 (file)
 
 #define MAX_PWM 255
 
+struct pwm_fan_tach {
+       int irq;
+       atomic_t pulses;
+       unsigned int rpm;
+       u8 pulses_per_revolution;
+};
+
 struct pwm_fan_ctx {
        struct mutex lock;
        struct pwm_device *pwm;
+       struct pwm_state pwm_state;
        struct regulator *reg_en;
 
-       int irq;
-       atomic_t pulses;
-       unsigned int rpm;
-       u8 pulses_per_revolution;
+       int tach_count;
+       struct pwm_fan_tach *tachs;
        ktime_t sample_start;
        struct timer_list rpm_timer;
 
@@ -40,6 +46,7 @@ struct pwm_fan_ctx {
        struct thermal_cooling_device *cdev;
 
        struct hwmon_chip_info info;
+       struct hwmon_channel_info fan_channel;
 };
 
 static const u32 pwm_fan_channel_config_pwm[] = {
@@ -52,22 +59,12 @@ static const struct hwmon_channel_info pwm_fan_channel_pwm = {
        .config = pwm_fan_channel_config_pwm,
 };
 
-static const u32 pwm_fan_channel_config_fan[] = {
-       HWMON_F_INPUT,
-       0
-};
-
-static const struct hwmon_channel_info pwm_fan_channel_fan = {
-       .type = hwmon_fan,
-       .config = pwm_fan_channel_config_fan,
-};
-
 /* This handler assumes self resetting edge triggered interrupt. */
 static irqreturn_t pulse_handler(int irq, void *dev_id)
 {
-       struct pwm_fan_ctx *ctx = dev_id;
+       struct pwm_fan_tach *tach = dev_id;
 
-       atomic_inc(&ctx->pulses);
+       atomic_inc(&tach->pulses);
 
        return IRQ_HANDLED;
 }
@@ -76,13 +73,18 @@ static void sample_timer(struct timer_list *t)
 {
        struct pwm_fan_ctx *ctx = from_timer(ctx, t, rpm_timer);
        unsigned int delta = ktime_ms_delta(ktime_get(), ctx->sample_start);
-       int pulses;
+       int i;
 
        if (delta) {
-               pulses = atomic_read(&ctx->pulses);
-               atomic_sub(pulses, &ctx->pulses);
-               ctx->rpm = (unsigned int)(pulses * 1000 * 60) /
-                       (ctx->pulses_per_revolution * delta);
+               for (i = 0; i < ctx->tach_count; i++) {
+                       struct pwm_fan_tach *tach = &ctx->tachs[i];
+                       int pulses;
+
+                       pulses = atomic_read(&tach->pulses);
+                       atomic_sub(pulses, &tach->pulses);
+                       tach->rpm = (unsigned int)(pulses * 1000 * 60) /
+                               (tach->pulses_per_revolution * delta);
+               }
 
                ctx->sample_start = ktime_get();
        }
@@ -94,18 +96,17 @@ static int  __set_pwm(struct pwm_fan_ctx *ctx, unsigned long pwm)
 {
        unsigned long period;
        int ret = 0;
-       struct pwm_state state = { };
+       struct pwm_state *state = &ctx->pwm_state;
 
        mutex_lock(&ctx->lock);
        if (ctx->pwm_value == pwm)
                goto exit_set_pwm_err;
 
-       pwm_init_state(ctx->pwm, &state);
-       period = ctx->pwm->args.period;
-       state.duty_cycle = DIV_ROUND_UP(pwm * (period - 1), MAX_PWM);
-       state.enabled = pwm ? true : false;
+       period = state->period;
+       state->duty_cycle = DIV_ROUND_UP(pwm * (period - 1), MAX_PWM);
+       state->enabled = pwm ? true : false;
 
-       ret = pwm_apply_state(ctx->pwm, &state);
+       ret = pwm_apply_state(ctx->pwm, state);
        if (!ret)
                ctx->pwm_value = pwm;
 exit_set_pwm_err:
@@ -152,7 +153,7 @@ static int pwm_fan_read(struct device *dev, enum hwmon_sensor_types type,
                return 0;
 
        case hwmon_fan:
-               *val = ctx->rpm;
+               *val = ctx->tachs[channel].rpm;
                return 0;
 
        default:
@@ -287,7 +288,9 @@ static void pwm_fan_regulator_disable(void *data)
 static void pwm_fan_pwm_disable(void *__ctx)
 {
        struct pwm_fan_ctx *ctx = __ctx;
-       pwm_disable(ctx->pwm);
+
+       ctx->pwm_state.enabled = false;
+       pwm_apply_state(ctx->pwm, &ctx->pwm_state);
        del_timer_sync(&ctx->rpm_timer);
 }
 
@@ -298,9 +301,10 @@ static int pwm_fan_probe(struct platform_device *pdev)
        struct pwm_fan_ctx *ctx;
        struct device *hwmon;
        int ret;
-       struct pwm_state state = { };
-       int tach_count;
        const struct hwmon_channel_info **channels;
+       u32 *fan_channel_config;
+       int channel_count = 1;  /* We always have a PWM channel. */
+       int i;
 
        ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
        if (!ctx)
@@ -334,22 +338,20 @@ static int pwm_fan_probe(struct platform_device *pdev)
 
        ctx->pwm_value = MAX_PWM;
 
-       pwm_init_state(ctx->pwm, &state);
+       pwm_init_state(ctx->pwm, &ctx->pwm_state);
+
        /*
         * __set_pwm assumes that MAX_PWM * (period - 1) fits into an unsigned
         * long. Check this here to prevent the fan running at a too low
         * frequency.
         */
-       if (state.period > ULONG_MAX / MAX_PWM + 1) {
+       if (ctx->pwm_state.period > ULONG_MAX / MAX_PWM + 1) {
                dev_err(dev, "Configured period too big\n");
                return -EINVAL;
        }
 
        /* Set duty cycle to maximum allowed and enable PWM output */
-       state.duty_cycle = ctx->pwm->args.period - 1;
-       state.enabled = true;
-
-       ret = pwm_apply_state(ctx->pwm, &state);
+       ret = __set_pwm(ctx, MAX_PWM);
        if (ret) {
                dev_err(dev, "Failed to configure PWM: %d\n", ret);
                return ret;
@@ -359,27 +361,46 @@ static int pwm_fan_probe(struct platform_device *pdev)
        if (ret)
                return ret;
 
-       tach_count = platform_irq_count(pdev);
-       if (tach_count < 0)
-               return dev_err_probe(dev, tach_count,
+       ctx->tach_count = platform_irq_count(pdev);
+       if (ctx->tach_count < 0)
+               return dev_err_probe(dev, ctx->tach_count,
                                     "Could not get number of fan tachometer inputs\n");
+       dev_dbg(dev, "%d fan tachometer inputs\n", ctx->tach_count);
+
+       if (ctx->tach_count) {
+               channel_count++;        /* We also have a FAN channel. */
+
+               ctx->tachs = devm_kcalloc(dev, ctx->tach_count,
+                                         sizeof(struct pwm_fan_tach),
+                                         GFP_KERNEL);
+               if (!ctx->tachs)
+                       return -ENOMEM;
+
+               ctx->fan_channel.type = hwmon_fan;
+               fan_channel_config = devm_kcalloc(dev, ctx->tach_count + 1,
+                                                 sizeof(u32), GFP_KERNEL);
+               if (!fan_channel_config)
+                       return -ENOMEM;
+               ctx->fan_channel.config = fan_channel_config;
+       }
 
-       channels = devm_kcalloc(dev, tach_count + 2,
+       channels = devm_kcalloc(dev, channel_count + 1,
                                sizeof(struct hwmon_channel_info *), GFP_KERNEL);
        if (!channels)
                return -ENOMEM;
 
        channels[0] = &pwm_fan_channel_pwm;
 
-       if (tach_count > 0) {
+       for (i = 0; i < ctx->tach_count; i++) {
+               struct pwm_fan_tach *tach = &ctx->tachs[i];
                u32 ppr = 2;
 
-               ctx->irq = platform_get_irq(pdev, 0);
-               if (ctx->irq == -EPROBE_DEFER)
-                       return ctx->irq;
-               if (ctx->irq > 0) {
-                       ret = devm_request_irq(dev, ctx->irq, pulse_handler, 0,
-                                              pdev->name, ctx);
+               tach->irq = platform_get_irq(pdev, i);
+               if (tach->irq == -EPROBE_DEFER)
+                       return tach->irq;
+               if (tach->irq > 0) {
+                       ret = devm_request_irq(dev, tach->irq, pulse_handler, 0,
+                                              pdev->name, tach);
                        if (ret) {
                                dev_err(dev,
                                        "Failed to request interrupt: %d\n",
@@ -388,22 +409,27 @@ static int pwm_fan_probe(struct platform_device *pdev)
                        }
                }
 
-               of_property_read_u32(dev->of_node,
-                                    "pulses-per-revolution",
-                                    &ppr);
-               ctx->pulses_per_revolution = ppr;
-               if (!ctx->pulses_per_revolution) {
+               of_property_read_u32_index(dev->of_node,
+                                          "pulses-per-revolution",
+                                          i,
+                                          &ppr);
+               tach->pulses_per_revolution = ppr;
+               if (!tach->pulses_per_revolution) {
                        dev_err(dev, "pulses-per-revolution can't be zero.\n");
                        return -EINVAL;
                }
 
-               dev_dbg(dev, "tach: irq=%d, pulses_per_revolution=%d\n",
-                       ctx->irq, ctx->pulses_per_revolution);
+               fan_channel_config[i] = HWMON_F_INPUT;
 
+               dev_dbg(dev, "tach%d: irq=%d, pulses_per_revolution=%d\n",
+                       i, tach->irq, tach->pulses_per_revolution);
+       }
+
+       if (ctx->tach_count > 0) {
                ctx->sample_start = ktime_get();
                mod_timer(&ctx->rpm_timer, jiffies + HZ);
 
-               channels[1] = &pwm_fan_channel_fan;
+               channels[1] = &ctx->fan_channel;
        }
 
        ctx->info.ops = &pwm_fan_hwmon_ops;
@@ -432,7 +458,6 @@ static int pwm_fan_probe(struct platform_device *pdev)
                        return ret;
                }
                ctx->cdev = cdev;
-               thermal_cdev_update(cdev);
        }
 
        return 0;
@@ -441,17 +466,17 @@ static int pwm_fan_probe(struct platform_device *pdev)
 static int pwm_fan_disable(struct device *dev)
 {
        struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
-       struct pwm_args args;
        int ret;
 
-       pwm_get_args(ctx->pwm, &args);
-
        if (ctx->pwm_value) {
-               ret = pwm_config(ctx->pwm, 0, args.period);
+               /* keep ctx->pwm_state unmodified for pwm_fan_resume() */
+               struct pwm_state state = ctx->pwm_state;
+
+               state.duty_cycle = 0;
+               state.enabled = false;
+               ret = pwm_apply_state(ctx->pwm, &state);
                if (ret < 0)
                        return ret;
-
-               pwm_disable(ctx->pwm);
        }
 
        if (ctx->reg_en) {
@@ -479,8 +504,6 @@ static int pwm_fan_suspend(struct device *dev)
 static int pwm_fan_resume(struct device *dev)
 {
        struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
-       struct pwm_args pargs;
-       unsigned long duty;
        int ret;
 
        if (ctx->reg_en) {
@@ -494,12 +517,7 @@ static int pwm_fan_resume(struct device *dev)
        if (ctx->pwm_value == 0)
                return 0;
 
-       pwm_get_args(ctx->pwm, &pargs);
-       duty = DIV_ROUND_UP_ULL(ctx->pwm_value * (pargs.period - 1), MAX_PWM);
-       ret = pwm_config(ctx->pwm, duty, pargs.period);
-       if (ret)
-               return ret;
-       return pwm_enable(ctx->pwm);
+       return pwm_apply_state(ctx->pwm, &ctx->pwm_state);
 }
 #endif
 
This page took 0.040482 seconds and 4 git commands to generate.