Lines Matching refs:ctx
74 struct pwm_fan_ctx *ctx = from_timer(ctx, t, rpm_timer); in sample_timer() local
75 unsigned int delta = ktime_ms_delta(ktime_get(), ctx->sample_start); in sample_timer()
79 for (i = 0; i < ctx->tach_count; i++) { in sample_timer()
80 struct pwm_fan_tach *tach = &ctx->tachs[i]; in sample_timer()
89 ctx->sample_start = ktime_get(); in sample_timer()
92 mod_timer(&ctx->rpm_timer, jiffies + HZ); in sample_timer()
95 static int __set_pwm(struct pwm_fan_ctx *ctx, unsigned long pwm) in __set_pwm() argument
99 struct pwm_state *state = &ctx->pwm_state; in __set_pwm()
101 mutex_lock(&ctx->lock); in __set_pwm()
102 if (ctx->pwm_value == pwm) in __set_pwm()
109 ret = pwm_apply_state(ctx->pwm, state); in __set_pwm()
111 ctx->pwm_value = pwm; in __set_pwm()
113 mutex_unlock(&ctx->lock); in __set_pwm()
117 static void pwm_fan_update_state(struct pwm_fan_ctx *ctx, unsigned long pwm) in pwm_fan_update_state() argument
121 for (i = 0; i < ctx->pwm_fan_max_state; ++i) in pwm_fan_update_state()
122 if (pwm < ctx->pwm_fan_cooling_levels[i + 1]) in pwm_fan_update_state()
125 ctx->pwm_fan_state = i; in pwm_fan_update_state()
131 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev); in pwm_fan_write() local
137 ret = __set_pwm(ctx, val); in pwm_fan_write()
141 pwm_fan_update_state(ctx, val); in pwm_fan_write()
148 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev); in pwm_fan_read() local
152 *val = ctx->pwm_value; in pwm_fan_read()
156 *val = ctx->tachs[channel].rpm; in pwm_fan_read()
190 struct pwm_fan_ctx *ctx = cdev->devdata; in pwm_fan_get_max_state() local
192 if (!ctx) in pwm_fan_get_max_state()
195 *state = ctx->pwm_fan_max_state; in pwm_fan_get_max_state()
203 struct pwm_fan_ctx *ctx = cdev->devdata; in pwm_fan_get_cur_state() local
205 if (!ctx) in pwm_fan_get_cur_state()
208 *state = ctx->pwm_fan_state; in pwm_fan_get_cur_state()
216 struct pwm_fan_ctx *ctx = cdev->devdata; in pwm_fan_set_cur_state() local
219 if (!ctx || (state > ctx->pwm_fan_max_state)) in pwm_fan_set_cur_state()
222 if (state == ctx->pwm_fan_state) in pwm_fan_set_cur_state()
225 ret = __set_pwm(ctx, ctx->pwm_fan_cooling_levels[state]); in pwm_fan_set_cur_state()
231 ctx->pwm_fan_state = state; in pwm_fan_set_cur_state()
243 struct pwm_fan_ctx *ctx) in pwm_fan_of_get_cooling_data() argument
258 ctx->pwm_fan_cooling_levels = devm_kcalloc(dev, num, sizeof(u32), in pwm_fan_of_get_cooling_data()
260 if (!ctx->pwm_fan_cooling_levels) in pwm_fan_of_get_cooling_data()
264 ctx->pwm_fan_cooling_levels, num); in pwm_fan_of_get_cooling_data()
271 if (ctx->pwm_fan_cooling_levels[i] > MAX_PWM) { in pwm_fan_of_get_cooling_data()
273 ctx->pwm_fan_cooling_levels[i], MAX_PWM); in pwm_fan_of_get_cooling_data()
278 ctx->pwm_fan_max_state = num - 1; in pwm_fan_of_get_cooling_data()
290 struct pwm_fan_ctx *ctx = __ctx; in pwm_fan_pwm_disable() local
292 ctx->pwm_state.enabled = false; in pwm_fan_pwm_disable()
293 pwm_apply_state(ctx->pwm, &ctx->pwm_state); in pwm_fan_pwm_disable()
294 del_timer_sync(&ctx->rpm_timer); in pwm_fan_pwm_disable()
301 struct pwm_fan_ctx *ctx; in pwm_fan_probe() local
309 ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL); in pwm_fan_probe()
310 if (!ctx) in pwm_fan_probe()
313 mutex_init(&ctx->lock); in pwm_fan_probe()
315 ctx->pwm = devm_of_pwm_get(dev, dev->of_node, NULL); in pwm_fan_probe()
316 if (IS_ERR(ctx->pwm)) in pwm_fan_probe()
317 return dev_err_probe(dev, PTR_ERR(ctx->pwm), "Could not get PWM\n"); in pwm_fan_probe()
319 platform_set_drvdata(pdev, ctx); in pwm_fan_probe()
321 ctx->reg_en = devm_regulator_get_optional(dev, "fan"); in pwm_fan_probe()
322 if (IS_ERR(ctx->reg_en)) { in pwm_fan_probe()
323 if (PTR_ERR(ctx->reg_en) != -ENODEV) in pwm_fan_probe()
324 return PTR_ERR(ctx->reg_en); in pwm_fan_probe()
326 ctx->reg_en = NULL; in pwm_fan_probe()
328 ret = regulator_enable(ctx->reg_en); in pwm_fan_probe()
334 ctx->reg_en); in pwm_fan_probe()
339 pwm_init_state(ctx->pwm, &ctx->pwm_state); in pwm_fan_probe()
346 if (ctx->pwm_state.period > ULONG_MAX / MAX_PWM + 1) { in pwm_fan_probe()
352 ret = __set_pwm(ctx, MAX_PWM); in pwm_fan_probe()
357 timer_setup(&ctx->rpm_timer, sample_timer, 0); in pwm_fan_probe()
358 ret = devm_add_action_or_reset(dev, pwm_fan_pwm_disable, ctx); in pwm_fan_probe()
362 ctx->tach_count = platform_irq_count(pdev); in pwm_fan_probe()
363 if (ctx->tach_count < 0) in pwm_fan_probe()
364 return dev_err_probe(dev, ctx->tach_count, in pwm_fan_probe()
366 dev_dbg(dev, "%d fan tachometer inputs\n", ctx->tach_count); in pwm_fan_probe()
368 if (ctx->tach_count) { in pwm_fan_probe()
371 ctx->tachs = devm_kcalloc(dev, ctx->tach_count, in pwm_fan_probe()
374 if (!ctx->tachs) in pwm_fan_probe()
377 ctx->fan_channel.type = hwmon_fan; in pwm_fan_probe()
378 fan_channel_config = devm_kcalloc(dev, ctx->tach_count + 1, in pwm_fan_probe()
382 ctx->fan_channel.config = fan_channel_config; in pwm_fan_probe()
392 for (i = 0; i < ctx->tach_count; i++) { in pwm_fan_probe()
393 struct pwm_fan_tach *tach = &ctx->tachs[i]; in pwm_fan_probe()
426 if (ctx->tach_count > 0) { in pwm_fan_probe()
427 ctx->sample_start = ktime_get(); in pwm_fan_probe()
428 mod_timer(&ctx->rpm_timer, jiffies + HZ); in pwm_fan_probe()
430 channels[1] = &ctx->fan_channel; in pwm_fan_probe()
433 ctx->info.ops = &pwm_fan_hwmon_ops; in pwm_fan_probe()
434 ctx->info.info = channels; in pwm_fan_probe()
437 ctx, &ctx->info, NULL); in pwm_fan_probe()
443 ret = pwm_fan_of_get_cooling_data(dev, ctx); in pwm_fan_probe()
447 ctx->pwm_fan_state = ctx->pwm_fan_max_state; in pwm_fan_probe()
450 dev->of_node, "pwm-fan", ctx, &pwm_fan_cooling_ops); in pwm_fan_probe()
458 ctx->cdev = cdev; in pwm_fan_probe()
466 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev); in pwm_fan_disable() local
469 if (ctx->pwm_value) { in pwm_fan_disable()
471 struct pwm_state state = ctx->pwm_state; in pwm_fan_disable()
475 ret = pwm_apply_state(ctx->pwm, &state); in pwm_fan_disable()
480 if (ctx->reg_en) { in pwm_fan_disable()
481 ret = regulator_disable(ctx->reg_en); in pwm_fan_disable()
504 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev); in pwm_fan_resume() local
507 if (ctx->reg_en) { in pwm_fan_resume()
508 ret = regulator_enable(ctx->reg_en); in pwm_fan_resume()
515 if (ctx->pwm_value == 0) in pwm_fan_resume()
518 return pwm_apply_state(ctx->pwm, &ctx->pwm_state); in pwm_fan_resume()