if (val == 0) { /* Disable pwm-fan unconditionally */ if (ctx->enabled)
ret = __set_pwm(ctx, 0); else
ret = pwm_fan_switch_power(ctx, false); if (ret)
ctx->enable_mode = old_val;
pwm_fan_update_state(ctx, 0);
} else { /* * Change PWM and/or regulator state if currently disabled * Nothing to do if currently enabled
*/ if (!ctx->enabled) { struct pwm_state *state = &ctx->pwm_state; bool enable_regulator = false;
staticint pwm_fan_write(struct device *dev, enum hwmon_sensor_types type,
u32 attr, int channel, long val)
{ struct pwm_fan_ctx *ctx = dev_get_drvdata(dev); int ret;
switch (attr) { case hwmon_pwm_input: if (val < 0 || val > MAX_PWM) return -EINVAL;
ret = set_pwm(ctx, val); if (ret) return ret;
pwm_fan_update_state(ctx, val); break; case hwmon_pwm_enable: if (val < 0 || val > 3)
ret = -EINVAL; else
ret = pwm_fan_update_enable(ctx, val);
return ret; default: return -EOPNOTSUPP;
}
return 0;
}
staticint pwm_fan_read(struct device *dev, enum hwmon_sensor_types type,
u32 attr, int channel, long *val)
{ struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
switch (type) { case hwmon_pwm: switch (attr) { case hwmon_pwm_input:
*val = ctx->pwm_value; return 0; case hwmon_pwm_enable:
*val = ctx->enable_mode; return 0;
} return -EOPNOTSUPP; case hwmon_fan:
*val = ctx->tachs[channel].rpm; return 0;
default: return -ENOTSUPP;
}
}
static umode_t pwm_fan_is_visible(constvoid *data, enum hwmon_sensor_types type,
u32 attr, int channel)
{ switch (type) { case hwmon_pwm: return 0644;
staticint pwm_fan_get_cooling_data(struct device *dev, struct pwm_fan_ctx *ctx)
{ int num, i, ret;
if (!device_property_present(dev, "cooling-levels")) return 0;
ret = device_property_count_u32(dev, "cooling-levels"); if (ret <= 0) {
dev_err(dev, "Wrong data!\n"); return ret ? : -EINVAL;
}
num = ret;
ctx->pwm_fan_cooling_levels = devm_kcalloc(dev, num, sizeof(u32),
GFP_KERNEL); if (!ctx->pwm_fan_cooling_levels) return -ENOMEM;
ret = device_property_read_u32_array(dev, "cooling-levels",
ctx->pwm_fan_cooling_levels, num); if (ret) {
dev_err(dev, "Property 'cooling-levels' cannot be read!\n"); return ret;
}
for (i = 0; i < num; i++) { if (ctx->pwm_fan_cooling_levels[i] > MAX_PWM) {
dev_err(dev, "PWM fan state[%d]:%d > %d\n", i,
ctx->pwm_fan_cooling_levels[i], MAX_PWM); return -EINVAL;
}
}
staticint pwm_fan_probe(struct platform_device *pdev)
{ struct thermal_cooling_device *cdev; struct device *dev = &pdev->dev; struct pwm_fan_ctx *ctx; struct device *hwmon; int ret; conststruct hwmon_channel_info **channels;
u32 initial_pwm, pwm_min_from_stopped = 0;
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) return -ENOMEM;
mutex_init(&ctx->lock);
ctx->dev = &pdev->dev;
ctx->pwm = devm_pwm_get(dev, NULL); if (IS_ERR(ctx->pwm)) return dev_err_probe(dev, PTR_ERR(ctx->pwm), "Could not get PWM\n");
platform_set_drvdata(pdev, ctx);
ctx->reg_en = devm_regulator_get_optional(dev, "fan"); if (IS_ERR(ctx->reg_en)) { if (PTR_ERR(ctx->reg_en) != -ENODEV) return PTR_ERR(ctx->reg_en);
ctx->reg_en = NULL;
}
pwm_init_state(ctx->pwm, &ctx->pwm_state);
/* * PWM fans are controlled solely by the duty cycle of the PWM signal, * they do not care about the exact timing. Thus set usage_power to true * to allow less flexible hardware to work as a PWM source for fan * control.
*/
ctx->pwm_state.usage_power = true;
/* * 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 (ctx->pwm_state.period > ULONG_MAX / MAX_PWM + 1) {
dev_err(dev, "Configured period too big\n"); return -EINVAL;
}
ctx->enable_mode = pwm_disable_reg_enable;
ret = pwm_fan_get_cooling_data(dev, ctx); if (ret) return ret;
/* use maximum cooling level if provided */ if (ctx->pwm_fan_cooling_levels)
initial_pwm = ctx->pwm_fan_cooling_levels[ctx->pwm_fan_max_state]; else
initial_pwm = MAX_PWM;
/* * Set duty cycle to maximum allowed and enable PWM output as well as * the regulator. In case of error nothing is changed
*/
ret = set_pwm(ctx, initial_pwm); if (ret) {
dev_err(dev, "Failed to configure PWM: %d\n", ret); return ret;
}
timer_setup(&ctx->rpm_timer, sample_timer, 0);
ret = devm_add_action_or_reset(dev, pwm_fan_cleanup, ctx); if (ret) return ret;
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. */
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.