Lines Matching refs:ctx

77 	struct pwm_fan_ctx *ctx = from_timer(ctx, t, rpm_timer);  in sample_timer()  local
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()
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()
121 static int pwm_fan_switch_power(struct pwm_fan_ctx *ctx, bool on) in pwm_fan_switch_power() argument
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()
140 static int pwm_fan_power_on(struct pwm_fan_ctx *ctx) in pwm_fan_power_on() argument
142 struct pwm_state *state = &ctx->pwm_state; in pwm_fan_power_on()
145 if (ctx->enabled) in pwm_fan_power_on()
148 ret = pwm_fan_switch_power(ctx, true); in pwm_fan_power_on()
150 dev_err(ctx->dev, "failed to enable power supply\n"); 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()
166 pwm_fan_switch_power(ctx, false); in pwm_fan_power_on()
170 static int pwm_fan_power_off(struct pwm_fan_ctx *ctx, bool force_disable) in pwm_fan_power_off() argument
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()
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()
192 pwm_fan_switch_power(ctx, enable_regulator); in pwm_fan_power_off()
194 ctx->enabled = false; in pwm_fan_power_off()
199 static int __set_pwm(struct pwm_fan_ctx *ctx, unsigned long pwm) in __set_pwm() argument
201 struct pwm_state *state = &ctx->pwm_state; in __set_pwm()
206 if (ctx->enable_mode == pwm_off_reg_off) in __set_pwm()
212 ret = pwm_apply_might_sleep(ctx->pwm, state); in __set_pwm()
215 ret = pwm_fan_power_on(ctx); in __set_pwm()
217 ret = pwm_fan_power_off(ctx, false); in __set_pwm()
220 ctx->pwm_value = pwm; in __set_pwm()
225 static int set_pwm(struct pwm_fan_ctx *ctx, unsigned long pwm) in set_pwm() argument
229 mutex_lock(&ctx->lock); in set_pwm()
230 ret = __set_pwm(ctx, pwm); in set_pwm()
231 mutex_unlock(&ctx->lock); in set_pwm()
236 static void pwm_fan_update_state(struct pwm_fan_ctx *ctx, unsigned long pwm) in pwm_fan_update_state() argument
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()
247 static int pwm_fan_update_enable(struct pwm_fan_ctx *ctx, long val) in pwm_fan_update_enable() argument
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()
262 if (ctx->enabled) in pwm_fan_update_enable()
263 ret = __set_pwm(ctx, 0); in pwm_fan_update_enable()
265 ret = pwm_fan_switch_power(ctx, false); in pwm_fan_update_enable()
267 ctx->enable_mode = old_val; in pwm_fan_update_enable()
268 pwm_fan_update_state(ctx, 0); 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()
283 pwm_apply_might_sleep(ctx->pwm, state); in pwm_fan_update_enable()
284 pwm_fan_switch_power(ctx, enable_regulator); in pwm_fan_update_enable()
285 pwm_fan_update_state(ctx, 0); in pwm_fan_update_enable()
289 mutex_unlock(&ctx->lock); in pwm_fan_update_enable()
297 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev); in pwm_fan_write() local
304 ret = set_pwm(ctx, val); in pwm_fan_write()
307 pwm_fan_update_state(ctx, val); in pwm_fan_write()
313 ret = pwm_fan_update_enable(ctx, val); in pwm_fan_write()
326 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev); in pwm_fan_read() local
332 *val = ctx->pwm_value; in pwm_fan_read()
335 *val = ctx->enable_mode; in pwm_fan_read()
340 *val = ctx->tachs[channel].rpm; in pwm_fan_read()
374 struct pwm_fan_ctx *ctx = cdev->devdata; in pwm_fan_get_max_state() local
376 if (!ctx) 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() local
389 if (!ctx) 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() local
403 if (!ctx || (state > ctx->pwm_fan_max_state)) 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()
415 ctx->pwm_fan_state = state; in pwm_fan_set_cur_state()
426 static int pwm_fan_get_cooling_data(struct device *dev, struct pwm_fan_ctx *ctx) in pwm_fan_get_cooling_data() argument
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()
446 ctx->pwm_fan_cooling_levels, num); 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()
460 ctx->pwm_fan_max_state = num - 1; in pwm_fan_get_cooling_data()
467 struct pwm_fan_ctx *ctx = __ctx; in pwm_fan_cleanup() local
469 del_timer_sync(&ctx->rpm_timer); in pwm_fan_cleanup()
471 ctx->enable_mode = pwm_disable_reg_disable; in pwm_fan_cleanup()
472 pwm_fan_power_off(ctx, true); in pwm_fan_cleanup()
479 struct pwm_fan_ctx *ctx; in pwm_fan_probe() local
487 ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL); in pwm_fan_probe()
488 if (!ctx) 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()
498 platform_set_drvdata(pdev, ctx); 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()
523 if (ctx->pwm_state.period > ULONG_MAX / MAX_PWM + 1) { in pwm_fan_probe()
528 ctx->enable_mode = pwm_disable_reg_enable; in pwm_fan_probe()
534 ret = set_pwm(ctx, MAX_PWM); in pwm_fan_probe()
539 timer_setup(&ctx->rpm_timer, sample_timer, 0); in pwm_fan_probe()
540 ret = devm_add_action_or_reset(dev, pwm_fan_cleanup, ctx); 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()
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()
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()
574 for (i = 0; i < ctx->tach_count; i++) in pwm_fan_probe()
575 ctx->pulses_per_revolution[i] = 2; in pwm_fan_probe()
578 ctx->pulses_per_revolution, ctx->tach_count); 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()
605 if (!ctx->pulses_per_revolution[i]) { 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()
633 ret = pwm_fan_get_cooling_data(dev, ctx); 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()
648 ctx->cdev = cdev; in pwm_fan_probe()
656 struct pwm_fan_ctx *ctx = platform_get_drvdata(pdev); in pwm_fan_shutdown() local
658 pwm_fan_cleanup(ctx); in pwm_fan_shutdown()
663 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev); in pwm_fan_suspend() local
665 return pwm_fan_power_off(ctx, true); in pwm_fan_suspend()
670 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev); in pwm_fan_resume() local
672 return set_pwm(ctx, ctx->pwm_value); in pwm_fan_resume()