Lines Matching +full:pulses +full:- +full:per +full:- +full:revolution

1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * pwm-fan.c - Hwmon driver for fans connected to PWM lines.
28 atomic_t pulses; member
74 atomic_inc(&tach->pulses); in pulse_handler()
82 unsigned int delta = ktime_ms_delta(ktime_get(), ctx->sample_start); in sample_timer()
86 for (i = 0; i < ctx->tach_count; i++) { in sample_timer()
87 struct pwm_fan_tach *tach = &ctx->tachs[i]; in sample_timer()
88 int pulses; in sample_timer() local
90 pulses = atomic_read(&tach->pulses); in sample_timer()
91 atomic_sub(pulses, &tach->pulses); in sample_timer()
92 tach->rpm = (unsigned int)(pulses * 1000 * 60) / in sample_timer()
93 (ctx->pulses_per_revolution[i] * delta); in sample_timer()
96 ctx->sample_start = ktime_get(); in sample_timer()
99 mod_timer(&ctx->rpm_timer, jiffies + HZ); in sample_timer()
109 state->enabled = false; in pwm_fan_enable_mode_2_state()
114 state->enabled = true; in pwm_fan_enable_mode_2_state()
120 state->enabled = false; in pwm_fan_enable_mode_2_state()
129 if (!ctx->reg_en) in pwm_fan_switch_power()
132 if (!ctx->regulator_enabled && on) { in pwm_fan_switch_power()
133 ret = regulator_enable(ctx->reg_en); in pwm_fan_switch_power()
135 ctx->regulator_enabled = true; in pwm_fan_switch_power()
136 } else if (ctx->regulator_enabled && !on) { in pwm_fan_switch_power()
137 ret = regulator_disable(ctx->reg_en); in pwm_fan_switch_power()
139 ctx->regulator_enabled = false; in pwm_fan_switch_power()
146 struct pwm_state *state = &ctx->pwm_state; in pwm_fan_power_on()
149 if (ctx->enabled) in pwm_fan_power_on()
154 dev_err(ctx->dev, "failed to enable power supply\n"); in pwm_fan_power_on()
158 state->enabled = true; in pwm_fan_power_on()
159 ret = pwm_apply_might_sleep(ctx->pwm, state); in pwm_fan_power_on()
161 dev_err(ctx->dev, "failed to enable PWM\n"); in pwm_fan_power_on()
165 ctx->enabled = true; in pwm_fan_power_on()
176 struct pwm_state *state = &ctx->pwm_state; in pwm_fan_power_off()
180 if (!ctx->enabled) in pwm_fan_power_off()
183 pwm_fan_enable_mode_2_state(ctx->enable_mode, in pwm_fan_power_off()
188 state->enabled = false; in pwm_fan_power_off()
189 state->duty_cycle = 0; in pwm_fan_power_off()
190 ret = pwm_apply_might_sleep(ctx->pwm, state); in pwm_fan_power_off()
192 dev_err(ctx->dev, "failed to disable PWM\n"); in pwm_fan_power_off()
198 ctx->enabled = false; in pwm_fan_power_off()
205 struct pwm_state *state = &ctx->pwm_state; in __set_pwm()
212 if (ctx->enable_mode == pwm_off_reg_off) in __set_pwm()
213 /* pwm-fan hard disabled */ in __set_pwm()
216 period = state->period; in __set_pwm()
217 update = state->duty_cycle < ctx->pwm_duty_cycle_from_stopped; in __set_pwm()
219 state->duty_cycle = ctx->pwm_duty_cycle_from_stopped; in __set_pwm()
221 state->duty_cycle = DIV_ROUND_UP(pwm * (period - 1), MAX_PWM); in __set_pwm()
222 ret = pwm_apply_might_sleep(ctx->pwm, state); in __set_pwm()
228 state->duty_cycle = DIV_ROUND_UP(pwm * (period - 1), MAX_PWM); in __set_pwm()
229 usleep_range(ctx->pwm_usec_from_stopped, in __set_pwm()
230 ctx->pwm_usec_from_stopped * 2); in __set_pwm()
231 ret = pwm_apply_might_sleep(ctx->pwm, state); in __set_pwm()
237 ctx->pwm_value = pwm; in __set_pwm()
246 mutex_lock(&ctx->lock); in set_pwm()
248 mutex_unlock(&ctx->lock); in set_pwm()
257 for (i = 0; i < ctx->pwm_fan_max_state; ++i) in pwm_fan_update_state()
258 if (pwm < ctx->pwm_fan_cooling_levels[i + 1]) in pwm_fan_update_state()
261 ctx->pwm_fan_state = i; in pwm_fan_update_state()
269 mutex_lock(&ctx->lock); in pwm_fan_update_enable()
271 if (ctx->enable_mode == val) in pwm_fan_update_enable()
274 old_val = ctx->enable_mode; in pwm_fan_update_enable()
275 ctx->enable_mode = val; in pwm_fan_update_enable()
278 /* Disable pwm-fan unconditionally */ in pwm_fan_update_enable()
279 if (ctx->enabled) in pwm_fan_update_enable()
284 ctx->enable_mode = old_val; in pwm_fan_update_enable()
291 if (!ctx->enabled) { in pwm_fan_update_enable()
292 struct pwm_state *state = &ctx->pwm_state; in pwm_fan_update_enable()
295 state->duty_cycle = 0; in pwm_fan_update_enable()
300 pwm_apply_might_sleep(ctx->pwm, state); in pwm_fan_update_enable()
306 mutex_unlock(&ctx->lock); in pwm_fan_update_enable()
320 return -EINVAL; in pwm_fan_write()
328 ret = -EINVAL; in pwm_fan_write()
334 return -EOPNOTSUPP; in pwm_fan_write()
349 *val = ctx->pwm_value; in pwm_fan_read()
352 *val = ctx->enable_mode; in pwm_fan_read()
355 return -EOPNOTSUPP; in pwm_fan_read()
357 *val = ctx->tachs[channel].rpm; in pwm_fan_read()
361 return -ENOTSUPP; in pwm_fan_read()
391 struct pwm_fan_ctx *ctx = cdev->devdata; in pwm_fan_get_max_state()
394 return -EINVAL; in pwm_fan_get_max_state()
396 *state = ctx->pwm_fan_max_state; in pwm_fan_get_max_state()
404 struct pwm_fan_ctx *ctx = cdev->devdata; in pwm_fan_get_cur_state()
407 return -EINVAL; in pwm_fan_get_cur_state()
409 *state = ctx->pwm_fan_state; in pwm_fan_get_cur_state()
417 struct pwm_fan_ctx *ctx = cdev->devdata; in pwm_fan_set_cur_state()
420 if (!ctx || (state > ctx->pwm_fan_max_state)) in pwm_fan_set_cur_state()
421 return -EINVAL; in pwm_fan_set_cur_state()
423 if (state == ctx->pwm_fan_state) in pwm_fan_set_cur_state()
426 ret = set_pwm(ctx, ctx->pwm_fan_cooling_levels[state]); in pwm_fan_set_cur_state()
428 dev_err(&cdev->device, "Cannot set pwm!\n"); in pwm_fan_set_cur_state()
432 ctx->pwm_fan_state = state; in pwm_fan_set_cur_state()
447 if (!device_property_present(dev, "cooling-levels")) in pwm_fan_get_cooling_data()
450 ret = device_property_count_u32(dev, "cooling-levels"); in pwm_fan_get_cooling_data()
453 return ret ? : -EINVAL; in pwm_fan_get_cooling_data()
457 ctx->pwm_fan_cooling_levels = devm_kcalloc(dev, num, sizeof(u32), in pwm_fan_get_cooling_data()
459 if (!ctx->pwm_fan_cooling_levels) in pwm_fan_get_cooling_data()
460 return -ENOMEM; in pwm_fan_get_cooling_data()
462 ret = device_property_read_u32_array(dev, "cooling-levels", in pwm_fan_get_cooling_data()
463 ctx->pwm_fan_cooling_levels, num); in pwm_fan_get_cooling_data()
465 dev_err(dev, "Property 'cooling-levels' cannot be read!\n"); in pwm_fan_get_cooling_data()
470 if (ctx->pwm_fan_cooling_levels[i] > MAX_PWM) { in pwm_fan_get_cooling_data()
472 ctx->pwm_fan_cooling_levels[i], MAX_PWM); in pwm_fan_get_cooling_data()
473 return -EINVAL; in pwm_fan_get_cooling_data()
477 ctx->pwm_fan_max_state = num - 1; in pwm_fan_get_cooling_data()
486 del_timer_sync(&ctx->rpm_timer); in pwm_fan_cleanup()
488 ctx->enable_mode = pwm_disable_reg_disable; in pwm_fan_cleanup()
495 struct device *dev = &pdev->dev; in pwm_fan_probe()
507 return -ENOMEM; in pwm_fan_probe()
509 mutex_init(&ctx->lock); in pwm_fan_probe()
511 ctx->dev = &pdev->dev; in pwm_fan_probe()
512 ctx->pwm = devm_pwm_get(dev, NULL); in pwm_fan_probe()
513 if (IS_ERR(ctx->pwm)) in pwm_fan_probe()
514 return dev_err_probe(dev, PTR_ERR(ctx->pwm), "Could not get PWM\n"); in pwm_fan_probe()
518 ctx->reg_en = devm_regulator_get_optional(dev, "fan"); in pwm_fan_probe()
519 if (IS_ERR(ctx->reg_en)) { in pwm_fan_probe()
520 if (PTR_ERR(ctx->reg_en) != -ENODEV) in pwm_fan_probe()
521 return PTR_ERR(ctx->reg_en); in pwm_fan_probe()
523 ctx->reg_en = NULL; in pwm_fan_probe()
526 pwm_init_state(ctx->pwm, &ctx->pwm_state); in pwm_fan_probe()
534 ctx->pwm_state.usage_power = true; in pwm_fan_probe()
537 * set_pwm assumes that MAX_PWM * (period - 1) fits into an unsigned in pwm_fan_probe()
541 if (ctx->pwm_state.period > ULONG_MAX / MAX_PWM + 1) { in pwm_fan_probe()
543 return -EINVAL; in pwm_fan_probe()
546 ctx->enable_mode = pwm_disable_reg_enable; in pwm_fan_probe()
553 if (ctx->pwm_fan_cooling_levels) in pwm_fan_probe()
554 initial_pwm = ctx->pwm_fan_cooling_levels[ctx->pwm_fan_max_state]; in pwm_fan_probe()
567 timer_setup(&ctx->rpm_timer, sample_timer, 0); in pwm_fan_probe()
572 ctx->tach_count = platform_irq_count(pdev); in pwm_fan_probe()
573 if (ctx->tach_count < 0) in pwm_fan_probe()
574 return dev_err_probe(dev, ctx->tach_count, in pwm_fan_probe()
576 dev_dbg(dev, "%d fan tachometer inputs\n", ctx->tach_count); in pwm_fan_probe()
578 if (ctx->tach_count) { in pwm_fan_probe()
581 ctx->tachs = devm_kcalloc(dev, ctx->tach_count, in pwm_fan_probe()
584 if (!ctx->tachs) in pwm_fan_probe()
585 return -ENOMEM; in pwm_fan_probe()
587 ctx->fan_channel.type = hwmon_fan; in pwm_fan_probe()
588 fan_channel_config = devm_kcalloc(dev, ctx->tach_count + 1, in pwm_fan_probe()
591 return -ENOMEM; in pwm_fan_probe()
592 ctx->fan_channel.config = fan_channel_config; in pwm_fan_probe()
594 ctx->pulses_per_revolution = devm_kmalloc_array(dev, in pwm_fan_probe()
595 ctx->tach_count, in pwm_fan_probe()
596 sizeof(*ctx->pulses_per_revolution), in pwm_fan_probe()
598 if (!ctx->pulses_per_revolution) in pwm_fan_probe()
599 return -ENOMEM; in pwm_fan_probe()
601 /* Setup default pulses per revolution */ in pwm_fan_probe()
602 for (i = 0; i < ctx->tach_count; i++) in pwm_fan_probe()
603 ctx->pulses_per_revolution[i] = 2; in pwm_fan_probe()
605 device_property_read_u32_array(dev, "pulses-per-revolution", in pwm_fan_probe()
606 ctx->pulses_per_revolution, ctx->tach_count); in pwm_fan_probe()
612 return -ENOMEM; in pwm_fan_probe()
616 for (i = 0; i < ctx->tach_count; i++) { in pwm_fan_probe()
617 struct pwm_fan_tach *tach = &ctx->tachs[i]; in pwm_fan_probe()
619 tach->irq = platform_get_irq(pdev, i); in pwm_fan_probe()
620 if (tach->irq == -EPROBE_DEFER) in pwm_fan_probe()
621 return tach->irq; in pwm_fan_probe()
622 if (tach->irq > 0) { in pwm_fan_probe()
623 ret = devm_request_irq(dev, tach->irq, pulse_handler, 0, in pwm_fan_probe()
624 pdev->name, tach); in pwm_fan_probe()
633 if (!ctx->pulses_per_revolution[i]) { in pwm_fan_probe()
634 dev_err(dev, "pulses-per-revolution can't be zero.\n"); in pwm_fan_probe()
635 return -EINVAL; in pwm_fan_probe()
641 i, tach->irq, ctx->pulses_per_revolution[i]); in pwm_fan_probe()
644 if (ctx->tach_count > 0) { in pwm_fan_probe()
645 ctx->sample_start = ktime_get(); in pwm_fan_probe()
646 mod_timer(&ctx->rpm_timer, jiffies + HZ); in pwm_fan_probe()
648 channels[1] = &ctx->fan_channel; in pwm_fan_probe()
651 ret = device_property_read_u32(dev, "fan-stop-to-start-percent", in pwm_fan_probe()
654 ctx->pwm_duty_cycle_from_stopped = in pwm_fan_probe()
656 (ctx->pwm_state.period - 1), in pwm_fan_probe()
659 ret = device_property_read_u32(dev, "fan-stop-to-start-us", in pwm_fan_probe()
660 &ctx->pwm_usec_from_stopped); in pwm_fan_probe()
662 ctx->pwm_usec_from_stopped = 250000; in pwm_fan_probe()
664 ctx->info.ops = &pwm_fan_hwmon_ops; in pwm_fan_probe()
665 ctx->info.info = channels; in pwm_fan_probe()
668 ctx, &ctx->info, NULL); in pwm_fan_probe()
674 ctx->pwm_fan_state = ctx->pwm_fan_max_state; in pwm_fan_probe()
677 dev->of_node, "pwm-fan", ctx, &pwm_fan_cooling_ops); in pwm_fan_probe()
681 "Failed to register pwm-fan as cooling device: %d\n", in pwm_fan_probe()
685 ctx->cdev = cdev; in pwm_fan_probe()
709 return set_pwm(ctx, ctx->pwm_value); in pwm_fan_resume()
715 { .compatible = "pwm-fan", },
724 .name = "pwm-fan",
733 MODULE_ALIAS("platform:pwm-fan");