Lines Matching +full:cooling +full:- +full:levels
1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * pwm-fan.c - Hwmon driver for fans connected to PWM lines.
70 atomic_inc(&tach->pulses); in pulse_handler()
78 unsigned int delta = ktime_ms_delta(ktime_get(), ctx->sample_start); in sample_timer()
82 for (i = 0; i < ctx->tach_count; i++) { in sample_timer()
83 struct pwm_fan_tach *tach = &ctx->tachs[i]; in sample_timer()
86 pulses = atomic_read(&tach->pulses); in sample_timer()
87 atomic_sub(pulses, &tach->pulses); in sample_timer()
88 tach->rpm = (unsigned int)(pulses * 1000 * 60) / in sample_timer()
89 (ctx->pulses_per_revolution[i] * delta); in sample_timer()
92 ctx->sample_start = ktime_get(); in sample_timer()
95 mod_timer(&ctx->rpm_timer, jiffies + HZ); in sample_timer()
105 state->enabled = false; in pwm_fan_enable_mode_2_state()
110 state->enabled = true; in pwm_fan_enable_mode_2_state()
116 state->enabled = false; in pwm_fan_enable_mode_2_state()
125 if (!ctx->reg_en) in pwm_fan_switch_power()
128 if (!ctx->regulator_enabled && on) { in pwm_fan_switch_power()
129 ret = regulator_enable(ctx->reg_en); in pwm_fan_switch_power()
131 ctx->regulator_enabled = true; in pwm_fan_switch_power()
132 } else if (ctx->regulator_enabled && !on) { in pwm_fan_switch_power()
133 ret = regulator_disable(ctx->reg_en); in pwm_fan_switch_power()
135 ctx->regulator_enabled = false; in pwm_fan_switch_power()
142 struct pwm_state *state = &ctx->pwm_state; in pwm_fan_power_on()
145 if (ctx->enabled) in pwm_fan_power_on()
150 dev_err(ctx->dev, "failed to enable power supply\n"); in pwm_fan_power_on()
154 state->enabled = true; in pwm_fan_power_on()
155 ret = pwm_apply_might_sleep(ctx->pwm, state); in pwm_fan_power_on()
157 dev_err(ctx->dev, "failed to enable PWM\n"); in pwm_fan_power_on()
161 ctx->enabled = true; in pwm_fan_power_on()
172 struct pwm_state *state = &ctx->pwm_state; in pwm_fan_power_off()
176 if (!ctx->enabled) in pwm_fan_power_off()
179 pwm_fan_enable_mode_2_state(ctx->enable_mode, in pwm_fan_power_off()
184 state->enabled = false; in pwm_fan_power_off()
185 state->duty_cycle = 0; in pwm_fan_power_off()
186 ret = pwm_apply_might_sleep(ctx->pwm, state); in pwm_fan_power_off()
188 dev_err(ctx->dev, "failed to disable PWM\n"); in pwm_fan_power_off()
194 ctx->enabled = false; in pwm_fan_power_off()
201 struct pwm_state *state = &ctx->pwm_state; in __set_pwm()
206 if (ctx->enable_mode == pwm_off_reg_off) in __set_pwm()
207 /* pwm-fan hard disabled */ in __set_pwm()
210 period = state->period; in __set_pwm()
211 state->duty_cycle = DIV_ROUND_UP(pwm * (period - 1), MAX_PWM); in __set_pwm()
212 ret = pwm_apply_might_sleep(ctx->pwm, state); in __set_pwm()
220 ctx->pwm_value = pwm; in __set_pwm()
229 mutex_lock(&ctx->lock); in set_pwm()
231 mutex_unlock(&ctx->lock); in set_pwm()
240 for (i = 0; i < ctx->pwm_fan_max_state; ++i) in pwm_fan_update_state()
241 if (pwm < ctx->pwm_fan_cooling_levels[i + 1]) in pwm_fan_update_state()
244 ctx->pwm_fan_state = i; in pwm_fan_update_state()
252 mutex_lock(&ctx->lock); in pwm_fan_update_enable()
254 if (ctx->enable_mode == val) in pwm_fan_update_enable()
257 old_val = ctx->enable_mode; in pwm_fan_update_enable()
258 ctx->enable_mode = val; in pwm_fan_update_enable()
261 /* Disable pwm-fan unconditionally */ in pwm_fan_update_enable()
262 if (ctx->enabled) in pwm_fan_update_enable()
267 ctx->enable_mode = old_val; in pwm_fan_update_enable()
274 if (!ctx->enabled) { in pwm_fan_update_enable()
275 struct pwm_state *state = &ctx->pwm_state; in pwm_fan_update_enable()
278 state->duty_cycle = 0; in pwm_fan_update_enable()
283 pwm_apply_might_sleep(ctx->pwm, state); in pwm_fan_update_enable()
289 mutex_unlock(&ctx->lock); in pwm_fan_update_enable()
303 return -EINVAL; in pwm_fan_write()
311 ret = -EINVAL; in pwm_fan_write()
317 return -EOPNOTSUPP; in pwm_fan_write()
332 *val = ctx->pwm_value; in pwm_fan_read()
335 *val = ctx->enable_mode; in pwm_fan_read()
338 return -EOPNOTSUPP; in pwm_fan_read()
340 *val = ctx->tachs[channel].rpm; in pwm_fan_read()
344 return -ENOTSUPP; in pwm_fan_read()
370 /* thermal cooling device callbacks */
374 struct pwm_fan_ctx *ctx = cdev->devdata; in pwm_fan_get_max_state()
377 return -EINVAL; in pwm_fan_get_max_state()
379 *state = ctx->pwm_fan_max_state; in pwm_fan_get_max_state()
387 struct pwm_fan_ctx *ctx = cdev->devdata; in pwm_fan_get_cur_state()
390 return -EINVAL; in pwm_fan_get_cur_state()
392 *state = ctx->pwm_fan_state; in pwm_fan_get_cur_state()
400 struct pwm_fan_ctx *ctx = cdev->devdata; in pwm_fan_set_cur_state()
403 if (!ctx || (state > ctx->pwm_fan_max_state)) in pwm_fan_set_cur_state()
404 return -EINVAL; in pwm_fan_set_cur_state()
406 if (state == ctx->pwm_fan_state) in pwm_fan_set_cur_state()
409 ret = set_pwm(ctx, ctx->pwm_fan_cooling_levels[state]); in pwm_fan_set_cur_state()
411 dev_err(&cdev->device, "Cannot set pwm!\n"); in pwm_fan_set_cur_state()
415 ctx->pwm_fan_state = state; in pwm_fan_set_cur_state()
430 if (!device_property_present(dev, "cooling-levels")) in pwm_fan_get_cooling_data()
433 ret = device_property_count_u32(dev, "cooling-levels"); in pwm_fan_get_cooling_data()
436 return ret ? : -EINVAL; in pwm_fan_get_cooling_data()
440 ctx->pwm_fan_cooling_levels = devm_kcalloc(dev, num, sizeof(u32), in pwm_fan_get_cooling_data()
442 if (!ctx->pwm_fan_cooling_levels) in pwm_fan_get_cooling_data()
443 return -ENOMEM; in pwm_fan_get_cooling_data()
445 ret = device_property_read_u32_array(dev, "cooling-levels", in pwm_fan_get_cooling_data()
446 ctx->pwm_fan_cooling_levels, num); in pwm_fan_get_cooling_data()
448 dev_err(dev, "Property 'cooling-levels' cannot be read!\n"); in pwm_fan_get_cooling_data()
453 if (ctx->pwm_fan_cooling_levels[i] > MAX_PWM) { in pwm_fan_get_cooling_data()
455 ctx->pwm_fan_cooling_levels[i], MAX_PWM); in pwm_fan_get_cooling_data()
456 return -EINVAL; in pwm_fan_get_cooling_data()
460 ctx->pwm_fan_max_state = num - 1; in pwm_fan_get_cooling_data()
469 del_timer_sync(&ctx->rpm_timer); in pwm_fan_cleanup()
471 ctx->enable_mode = pwm_disable_reg_disable; in pwm_fan_cleanup()
478 struct device *dev = &pdev->dev; in pwm_fan_probe()
489 return -ENOMEM; in pwm_fan_probe()
491 mutex_init(&ctx->lock); in pwm_fan_probe()
493 ctx->dev = &pdev->dev; in pwm_fan_probe()
494 ctx->pwm = devm_pwm_get(dev, NULL); in pwm_fan_probe()
495 if (IS_ERR(ctx->pwm)) in pwm_fan_probe()
496 return dev_err_probe(dev, PTR_ERR(ctx->pwm), "Could not get PWM\n"); in pwm_fan_probe()
500 ctx->reg_en = devm_regulator_get_optional(dev, "fan"); in pwm_fan_probe()
501 if (IS_ERR(ctx->reg_en)) { in pwm_fan_probe()
502 if (PTR_ERR(ctx->reg_en) != -ENODEV) in pwm_fan_probe()
503 return PTR_ERR(ctx->reg_en); in pwm_fan_probe()
505 ctx->reg_en = NULL; in pwm_fan_probe()
508 pwm_init_state(ctx->pwm, &ctx->pwm_state); in pwm_fan_probe()
516 ctx->pwm_state.usage_power = true; in pwm_fan_probe()
519 * set_pwm assumes that MAX_PWM * (period - 1) fits into an unsigned in pwm_fan_probe()
523 if (ctx->pwm_state.period > ULONG_MAX / MAX_PWM + 1) { in pwm_fan_probe()
525 return -EINVAL; in pwm_fan_probe()
528 ctx->enable_mode = pwm_disable_reg_enable; in pwm_fan_probe()
539 timer_setup(&ctx->rpm_timer, sample_timer, 0); in pwm_fan_probe()
544 ctx->tach_count = platform_irq_count(pdev); in pwm_fan_probe()
545 if (ctx->tach_count < 0) in pwm_fan_probe()
546 return dev_err_probe(dev, ctx->tach_count, in pwm_fan_probe()
548 dev_dbg(dev, "%d fan tachometer inputs\n", ctx->tach_count); in pwm_fan_probe()
550 if (ctx->tach_count) { in pwm_fan_probe()
553 ctx->tachs = devm_kcalloc(dev, ctx->tach_count, in pwm_fan_probe()
556 if (!ctx->tachs) in pwm_fan_probe()
557 return -ENOMEM; in pwm_fan_probe()
559 ctx->fan_channel.type = hwmon_fan; in pwm_fan_probe()
560 fan_channel_config = devm_kcalloc(dev, ctx->tach_count + 1, in pwm_fan_probe()
563 return -ENOMEM; in pwm_fan_probe()
564 ctx->fan_channel.config = fan_channel_config; in pwm_fan_probe()
566 ctx->pulses_per_revolution = devm_kmalloc_array(dev, in pwm_fan_probe()
567 ctx->tach_count, in pwm_fan_probe()
568 sizeof(*ctx->pulses_per_revolution), in pwm_fan_probe()
570 if (!ctx->pulses_per_revolution) in pwm_fan_probe()
571 return -ENOMEM; in pwm_fan_probe()
574 for (i = 0; i < ctx->tach_count; i++) in pwm_fan_probe()
575 ctx->pulses_per_revolution[i] = 2; in pwm_fan_probe()
577 device_property_read_u32_array(dev, "pulses-per-revolution", in pwm_fan_probe()
578 ctx->pulses_per_revolution, ctx->tach_count); in pwm_fan_probe()
584 return -ENOMEM; in pwm_fan_probe()
588 for (i = 0; i < ctx->tach_count; i++) { in pwm_fan_probe()
589 struct pwm_fan_tach *tach = &ctx->tachs[i]; in pwm_fan_probe()
591 tach->irq = platform_get_irq(pdev, i); in pwm_fan_probe()
592 if (tach->irq == -EPROBE_DEFER) in pwm_fan_probe()
593 return tach->irq; in pwm_fan_probe()
594 if (tach->irq > 0) { in pwm_fan_probe()
595 ret = devm_request_irq(dev, tach->irq, pulse_handler, 0, in pwm_fan_probe()
596 pdev->name, tach); in pwm_fan_probe()
605 if (!ctx->pulses_per_revolution[i]) { in pwm_fan_probe()
606 dev_err(dev, "pulses-per-revolution can't be zero.\n"); in pwm_fan_probe()
607 return -EINVAL; in pwm_fan_probe()
613 i, tach->irq, ctx->pulses_per_revolution[i]); in pwm_fan_probe()
616 if (ctx->tach_count > 0) { in pwm_fan_probe()
617 ctx->sample_start = ktime_get(); in pwm_fan_probe()
618 mod_timer(&ctx->rpm_timer, jiffies + HZ); in pwm_fan_probe()
620 channels[1] = &ctx->fan_channel; in pwm_fan_probe()
623 ctx->info.ops = &pwm_fan_hwmon_ops; in pwm_fan_probe()
624 ctx->info.info = channels; in pwm_fan_probe()
627 ctx, &ctx->info, NULL); in pwm_fan_probe()
637 ctx->pwm_fan_state = ctx->pwm_fan_max_state; in pwm_fan_probe()
640 dev->of_node, "pwm-fan", ctx, &pwm_fan_cooling_ops); in pwm_fan_probe()
644 "Failed to register pwm-fan as cooling device: %d\n", in pwm_fan_probe()
648 ctx->cdev = cdev; in pwm_fan_probe()
672 return set_pwm(ctx, ctx->pwm_value); in pwm_fan_resume()
678 { .compatible = "pwm-fan", },
687 .name = "pwm-fan",
696 MODULE_ALIAS("platform:pwm-fan");