Lines Matching refs:ctx
76 struct pwm_fan_ctx *ctx = from_timer(ctx, t, rpm_timer); in sample_timer() local
77 unsigned int delta = ktime_ms_delta(ktime_get(), ctx->sample_start); in sample_timer()
81 for (i = 0; i < ctx->tach_count; i++) { in sample_timer()
82 struct pwm_fan_tach *tach = &ctx->tachs[i]; in sample_timer()
91 ctx->sample_start = ktime_get(); in sample_timer()
94 mod_timer(&ctx->rpm_timer, jiffies + HZ); in sample_timer()
120 static int pwm_fan_switch_power(struct pwm_fan_ctx *ctx, bool on) in pwm_fan_switch_power() argument
124 if (!ctx->reg_en) in pwm_fan_switch_power()
127 if (!ctx->regulator_enabled && on) { in pwm_fan_switch_power()
128 ret = regulator_enable(ctx->reg_en); in pwm_fan_switch_power()
130 ctx->regulator_enabled = true; in pwm_fan_switch_power()
131 } else if (ctx->regulator_enabled && !on) { in pwm_fan_switch_power()
132 ret = regulator_disable(ctx->reg_en); in pwm_fan_switch_power()
134 ctx->regulator_enabled = false; in pwm_fan_switch_power()
139 static int pwm_fan_power_on(struct pwm_fan_ctx *ctx) in pwm_fan_power_on() argument
141 struct pwm_state *state = &ctx->pwm_state; in pwm_fan_power_on()
144 if (ctx->enabled) in pwm_fan_power_on()
147 ret = pwm_fan_switch_power(ctx, true); in pwm_fan_power_on()
149 dev_err(ctx->dev, "failed to enable power supply\n"); in pwm_fan_power_on()
154 ret = pwm_apply_might_sleep(ctx->pwm, state); in pwm_fan_power_on()
156 dev_err(ctx->dev, "failed to enable PWM\n"); in pwm_fan_power_on()
160 ctx->enabled = true; in pwm_fan_power_on()
165 pwm_fan_switch_power(ctx, false); in pwm_fan_power_on()
169 static int pwm_fan_power_off(struct pwm_fan_ctx *ctx) in pwm_fan_power_off() argument
171 struct pwm_state *state = &ctx->pwm_state; in pwm_fan_power_off()
175 if (!ctx->enabled) in pwm_fan_power_off()
178 pwm_fan_enable_mode_2_state(ctx->enable_mode, in pwm_fan_power_off()
184 ret = pwm_apply_might_sleep(ctx->pwm, state); in pwm_fan_power_off()
186 dev_err(ctx->dev, "failed to disable PWM\n"); in pwm_fan_power_off()
190 pwm_fan_switch_power(ctx, enable_regulator); in pwm_fan_power_off()
192 ctx->enabled = false; in pwm_fan_power_off()
197 static int __set_pwm(struct pwm_fan_ctx *ctx, unsigned long pwm) in __set_pwm() argument
199 struct pwm_state *state = &ctx->pwm_state; in __set_pwm()
204 if (ctx->enable_mode == pwm_off_reg_off) in __set_pwm()
210 ret = pwm_apply_might_sleep(ctx->pwm, state); in __set_pwm()
213 ret = pwm_fan_power_on(ctx); in __set_pwm()
215 ret = pwm_fan_power_off(ctx); in __set_pwm()
218 ctx->pwm_value = pwm; in __set_pwm()
223 static int set_pwm(struct pwm_fan_ctx *ctx, unsigned long pwm) in set_pwm() argument
227 mutex_lock(&ctx->lock); in set_pwm()
228 ret = __set_pwm(ctx, pwm); in set_pwm()
229 mutex_unlock(&ctx->lock); in set_pwm()
234 static void pwm_fan_update_state(struct pwm_fan_ctx *ctx, unsigned long pwm) in pwm_fan_update_state() argument
238 for (i = 0; i < ctx->pwm_fan_max_state; ++i) in pwm_fan_update_state()
239 if (pwm < ctx->pwm_fan_cooling_levels[i + 1]) in pwm_fan_update_state()
242 ctx->pwm_fan_state = i; in pwm_fan_update_state()
245 static int pwm_fan_update_enable(struct pwm_fan_ctx *ctx, long val) in pwm_fan_update_enable() argument
250 mutex_lock(&ctx->lock); in pwm_fan_update_enable()
252 if (ctx->enable_mode == val) in pwm_fan_update_enable()
255 old_val = ctx->enable_mode; in pwm_fan_update_enable()
256 ctx->enable_mode = val; in pwm_fan_update_enable()
260 if (ctx->enabled) in pwm_fan_update_enable()
261 ret = __set_pwm(ctx, 0); in pwm_fan_update_enable()
263 ret = pwm_fan_switch_power(ctx, false); in pwm_fan_update_enable()
265 ctx->enable_mode = old_val; in pwm_fan_update_enable()
266 pwm_fan_update_state(ctx, 0); in pwm_fan_update_enable()
272 if (!ctx->enabled) { in pwm_fan_update_enable()
273 struct pwm_state *state = &ctx->pwm_state; in pwm_fan_update_enable()
281 pwm_apply_might_sleep(ctx->pwm, state); in pwm_fan_update_enable()
282 pwm_fan_switch_power(ctx, enable_regulator); in pwm_fan_update_enable()
283 pwm_fan_update_state(ctx, 0); in pwm_fan_update_enable()
287 mutex_unlock(&ctx->lock); in pwm_fan_update_enable()
295 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev); in pwm_fan_write() local
302 ret = set_pwm(ctx, val); in pwm_fan_write()
305 pwm_fan_update_state(ctx, val); in pwm_fan_write()
311 ret = pwm_fan_update_enable(ctx, val); in pwm_fan_write()
324 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev); in pwm_fan_read() local
330 *val = ctx->pwm_value; in pwm_fan_read()
333 *val = ctx->enable_mode; in pwm_fan_read()
338 *val = ctx->tachs[channel].rpm; in pwm_fan_read()
372 struct pwm_fan_ctx *ctx = cdev->devdata; in pwm_fan_get_max_state() local
374 if (!ctx) in pwm_fan_get_max_state()
377 *state = ctx->pwm_fan_max_state; in pwm_fan_get_max_state()
385 struct pwm_fan_ctx *ctx = cdev->devdata; in pwm_fan_get_cur_state() local
387 if (!ctx) in pwm_fan_get_cur_state()
390 *state = ctx->pwm_fan_state; in pwm_fan_get_cur_state()
398 struct pwm_fan_ctx *ctx = cdev->devdata; in pwm_fan_set_cur_state() local
401 if (!ctx || (state > ctx->pwm_fan_max_state)) in pwm_fan_set_cur_state()
404 if (state == ctx->pwm_fan_state) in pwm_fan_set_cur_state()
407 ret = set_pwm(ctx, ctx->pwm_fan_cooling_levels[state]); in pwm_fan_set_cur_state()
413 ctx->pwm_fan_state = state; in pwm_fan_set_cur_state()
425 struct pwm_fan_ctx *ctx) in pwm_fan_of_get_cooling_data() argument
440 ctx->pwm_fan_cooling_levels = devm_kcalloc(dev, num, sizeof(u32), in pwm_fan_of_get_cooling_data()
442 if (!ctx->pwm_fan_cooling_levels) in pwm_fan_of_get_cooling_data()
446 ctx->pwm_fan_cooling_levels, num); in pwm_fan_of_get_cooling_data()
453 if (ctx->pwm_fan_cooling_levels[i] > MAX_PWM) { in pwm_fan_of_get_cooling_data()
455 ctx->pwm_fan_cooling_levels[i], MAX_PWM); in pwm_fan_of_get_cooling_data()
460 ctx->pwm_fan_max_state = num - 1; in pwm_fan_of_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); 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()
574 for (i = 0; i < ctx->tach_count; i++) { in pwm_fan_probe()
575 struct pwm_fan_tach *tach = &ctx->tachs[i]; in pwm_fan_probe()
608 if (ctx->tach_count > 0) { in pwm_fan_probe()
609 ctx->sample_start = ktime_get(); in pwm_fan_probe()
610 mod_timer(&ctx->rpm_timer, jiffies + HZ); in pwm_fan_probe()
612 channels[1] = &ctx->fan_channel; in pwm_fan_probe()
615 ctx->info.ops = &pwm_fan_hwmon_ops; in pwm_fan_probe()
616 ctx->info.info = channels; in pwm_fan_probe()
619 ctx, &ctx->info, NULL); in pwm_fan_probe()
625 ret = pwm_fan_of_get_cooling_data(dev, ctx); in pwm_fan_probe()
629 ctx->pwm_fan_state = ctx->pwm_fan_max_state; in pwm_fan_probe()
632 dev->of_node, "pwm-fan", ctx, &pwm_fan_cooling_ops); in pwm_fan_probe()
640 ctx->cdev = cdev; in pwm_fan_probe()
648 struct pwm_fan_ctx *ctx = platform_get_drvdata(pdev); in pwm_fan_shutdown() local
650 pwm_fan_cleanup(ctx); in pwm_fan_shutdown()
655 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev); in pwm_fan_suspend() local
657 return pwm_fan_power_off(ctx); in pwm_fan_suspend()
662 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev); in pwm_fan_resume() local
664 return set_pwm(ctx, ctx->pwm_value); in pwm_fan_resume()