Lines Matching refs:ctx
41 static int __set_pwm(struct pwm_fan_ctx *ctx, unsigned long pwm) in __set_pwm() argument
46 mutex_lock(&ctx->lock); in __set_pwm()
47 if (ctx->pwm_value == pwm) in __set_pwm()
50 duty = DIV_ROUND_UP(pwm * (ctx->pwm->period - 1), MAX_PWM); in __set_pwm()
51 ret = pwm_config(ctx->pwm, duty, ctx->pwm->period); in __set_pwm()
56 pwm_disable(ctx->pwm); in __set_pwm()
58 if (ctx->pwm_value == 0) { in __set_pwm()
59 ret = pwm_enable(ctx->pwm); in __set_pwm()
64 ctx->pwm_value = pwm; in __set_pwm()
66 mutex_unlock(&ctx->lock); in __set_pwm()
70 static void pwm_fan_update_state(struct pwm_fan_ctx *ctx, unsigned long pwm) in pwm_fan_update_state() argument
74 for (i = 0; i < ctx->pwm_fan_max_state; ++i) in pwm_fan_update_state()
75 if (pwm < ctx->pwm_fan_cooling_levels[i + 1]) in pwm_fan_update_state()
78 ctx->pwm_fan_state = i; in pwm_fan_update_state()
84 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev); in set_pwm() local
91 ret = __set_pwm(ctx, pwm); in set_pwm()
95 pwm_fan_update_state(ctx, pwm); in set_pwm()
102 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev); in show_pwm() local
104 return sprintf(buf, "%u\n", ctx->pwm_value); in show_pwm()
121 struct pwm_fan_ctx *ctx = cdev->devdata; in pwm_fan_get_max_state() local
123 if (!ctx) in pwm_fan_get_max_state()
126 *state = ctx->pwm_fan_max_state; in pwm_fan_get_max_state()
134 struct pwm_fan_ctx *ctx = cdev->devdata; in pwm_fan_get_cur_state() local
136 if (!ctx) in pwm_fan_get_cur_state()
139 *state = ctx->pwm_fan_state; in pwm_fan_get_cur_state()
147 struct pwm_fan_ctx *ctx = cdev->devdata; in pwm_fan_set_cur_state() local
150 if (!ctx || (state > ctx->pwm_fan_max_state)) in pwm_fan_set_cur_state()
153 if (state == ctx->pwm_fan_state) in pwm_fan_set_cur_state()
156 ret = __set_pwm(ctx, ctx->pwm_fan_cooling_levels[state]); in pwm_fan_set_cur_state()
162 ctx->pwm_fan_state = state; in pwm_fan_set_cur_state()
174 struct pwm_fan_ctx *ctx) in pwm_fan_of_get_cooling_data() argument
189 ctx->pwm_fan_cooling_levels = devm_kzalloc(dev, num * sizeof(u32), in pwm_fan_of_get_cooling_data()
191 if (!ctx->pwm_fan_cooling_levels) in pwm_fan_of_get_cooling_data()
195 ctx->pwm_fan_cooling_levels, num); in pwm_fan_of_get_cooling_data()
202 if (ctx->pwm_fan_cooling_levels[i] > MAX_PWM) { in pwm_fan_of_get_cooling_data()
204 ctx->pwm_fan_cooling_levels[i], MAX_PWM); in pwm_fan_of_get_cooling_data()
209 ctx->pwm_fan_max_state = num - 1; in pwm_fan_of_get_cooling_data()
217 struct pwm_fan_ctx *ctx; in pwm_fan_probe() local
222 ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL); in pwm_fan_probe()
223 if (!ctx) in pwm_fan_probe()
226 mutex_init(&ctx->lock); in pwm_fan_probe()
228 ctx->pwm = devm_of_pwm_get(&pdev->dev, pdev->dev.of_node, NULL); in pwm_fan_probe()
229 if (IS_ERR(ctx->pwm)) { in pwm_fan_probe()
231 return PTR_ERR(ctx->pwm); in pwm_fan_probe()
234 platform_set_drvdata(pdev, ctx); in pwm_fan_probe()
237 duty_cycle = ctx->pwm->period - 1; in pwm_fan_probe()
238 ctx->pwm_value = MAX_PWM; in pwm_fan_probe()
240 ret = pwm_config(ctx->pwm, duty_cycle, ctx->pwm->period); in pwm_fan_probe()
247 ret = pwm_enable(ctx->pwm); in pwm_fan_probe()
254 ctx, pwm_fan_groups); in pwm_fan_probe()
257 pwm_disable(ctx->pwm); in pwm_fan_probe()
261 ret = pwm_fan_of_get_cooling_data(&pdev->dev, ctx); in pwm_fan_probe()
265 ctx->pwm_fan_state = ctx->pwm_fan_max_state; in pwm_fan_probe()
268 "pwm-fan", ctx, in pwm_fan_probe()
273 pwm_disable(ctx->pwm); in pwm_fan_probe()
276 ctx->cdev = cdev; in pwm_fan_probe()
285 struct pwm_fan_ctx *ctx = platform_get_drvdata(pdev); in pwm_fan_remove() local
287 thermal_cooling_device_unregister(ctx->cdev); in pwm_fan_remove()
288 if (ctx->pwm_value) in pwm_fan_remove()
289 pwm_disable(ctx->pwm); in pwm_fan_remove()
296 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev); in pwm_fan_suspend() local
298 if (ctx->pwm_value) in pwm_fan_suspend()
299 pwm_disable(ctx->pwm); in pwm_fan_suspend()
305 struct pwm_fan_ctx *ctx = dev_get_drvdata(dev); in pwm_fan_resume() local
309 if (ctx->pwm_value == 0) in pwm_fan_resume()
312 duty = DIV_ROUND_UP(ctx->pwm_value * (ctx->pwm->period - 1), MAX_PWM); in pwm_fan_resume()
313 ret = pwm_config(ctx->pwm, duty, ctx->pwm->period); in pwm_fan_resume()
316 return pwm_enable(ctx->pwm); in pwm_fan_resume()