]>
Commit | Line | Data |
---|---|---|
d82d5776 KD |
1 | /* |
2 | * pwm-fan.c - Hwmon driver for fans connected to PWM lines. | |
3 | * | |
4 | * Copyright (c) 2014 Samsung Electronics Co., Ltd. | |
5 | * | |
6 | * Author: Kamil Debski <k.debski@samsung.com> | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation; either version 2 of the License, or | |
11 | * (at your option) any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | */ | |
18 | ||
19 | #include <linux/hwmon.h> | |
20 | #include <linux/hwmon-sysfs.h> | |
21 | #include <linux/module.h> | |
22 | #include <linux/mutex.h> | |
23 | #include <linux/of.h> | |
24 | #include <linux/platform_device.h> | |
25 | #include <linux/pwm.h> | |
26 | #include <linux/sysfs.h> | |
27 | ||
28 | #define MAX_PWM 255 | |
29 | ||
30 | struct pwm_fan_ctx { | |
31 | struct mutex lock; | |
32 | struct pwm_device *pwm; | |
33 | unsigned char pwm_value; | |
34 | }; | |
35 | ||
cb85ca33 | 36 | static int __set_pwm(struct pwm_fan_ctx *ctx, unsigned long pwm) |
d82d5776 | 37 | { |
cb85ca33 LM |
38 | unsigned long duty; |
39 | int ret = 0; | |
d82d5776 KD |
40 | |
41 | mutex_lock(&ctx->lock); | |
d82d5776 | 42 | if (ctx->pwm_value == pwm) |
cb85ca33 | 43 | goto exit_set_pwm_err; |
d82d5776 KD |
44 | |
45 | if (pwm == 0) { | |
46 | pwm_disable(ctx->pwm); | |
47 | goto exit_set_pwm; | |
48 | } | |
49 | ||
50 | duty = DIV_ROUND_UP(pwm * (ctx->pwm->period - 1), MAX_PWM); | |
51 | ret = pwm_config(ctx->pwm, duty, ctx->pwm->period); | |
52 | if (ret) | |
53 | goto exit_set_pwm_err; | |
54 | ||
55 | if (ctx->pwm_value == 0) { | |
56 | ret = pwm_enable(ctx->pwm); | |
57 | if (ret) | |
58 | goto exit_set_pwm_err; | |
59 | } | |
60 | ||
61 | exit_set_pwm: | |
62 | ctx->pwm_value = pwm; | |
d82d5776 KD |
63 | exit_set_pwm_err: |
64 | mutex_unlock(&ctx->lock); | |
65 | return ret; | |
66 | } | |
67 | ||
cb85ca33 LM |
68 | static ssize_t set_pwm(struct device *dev, struct device_attribute *attr, |
69 | const char *buf, size_t count) | |
70 | { | |
71 | struct pwm_fan_ctx *ctx = dev_get_drvdata(dev); | |
72 | unsigned long pwm; | |
73 | int ret; | |
74 | ||
75 | if (kstrtoul(buf, 10, &pwm) || pwm > MAX_PWM) | |
76 | return -EINVAL; | |
77 | ||
78 | ret = __set_pwm(ctx, pwm); | |
79 | if (ret) | |
80 | return ret; | |
81 | ||
82 | return count; | |
83 | } | |
84 | ||
d82d5776 KD |
85 | static ssize_t show_pwm(struct device *dev, |
86 | struct device_attribute *attr, char *buf) | |
87 | { | |
88 | struct pwm_fan_ctx *ctx = dev_get_drvdata(dev); | |
89 | ||
90 | return sprintf(buf, "%u\n", ctx->pwm_value); | |
91 | } | |
92 | ||
93 | ||
94 | static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 0); | |
95 | ||
96 | static struct attribute *pwm_fan_attrs[] = { | |
97 | &sensor_dev_attr_pwm1.dev_attr.attr, | |
98 | NULL, | |
99 | }; | |
100 | ||
101 | ATTRIBUTE_GROUPS(pwm_fan); | |
102 | ||
103 | static int pwm_fan_probe(struct platform_device *pdev) | |
104 | { | |
105 | struct device *hwmon; | |
106 | struct pwm_fan_ctx *ctx; | |
107 | int duty_cycle; | |
108 | int ret; | |
109 | ||
110 | ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL); | |
111 | if (!ctx) | |
112 | return -ENOMEM; | |
113 | ||
114 | mutex_init(&ctx->lock); | |
115 | ||
116 | ctx->pwm = devm_of_pwm_get(&pdev->dev, pdev->dev.of_node, NULL); | |
117 | if (IS_ERR(ctx->pwm)) { | |
118 | dev_err(&pdev->dev, "Could not get PWM\n"); | |
119 | return PTR_ERR(ctx->pwm); | |
120 | } | |
121 | ||
d82d5776 KD |
122 | platform_set_drvdata(pdev, ctx); |
123 | ||
124 | /* Set duty cycle to maximum allowed */ | |
125 | duty_cycle = ctx->pwm->period - 1; | |
126 | ctx->pwm_value = MAX_PWM; | |
127 | ||
128 | ret = pwm_config(ctx->pwm, duty_cycle, ctx->pwm->period); | |
129 | if (ret) { | |
130 | dev_err(&pdev->dev, "Failed to configure PWM\n"); | |
131 | return ret; | |
132 | } | |
133 | ||
134 | /* Enbale PWM output */ | |
135 | ret = pwm_enable(ctx->pwm); | |
136 | if (ret) { | |
137 | dev_err(&pdev->dev, "Failed to enable PWM\n"); | |
138 | return ret; | |
139 | } | |
140 | ||
141 | hwmon = devm_hwmon_device_register_with_groups(&pdev->dev, "pwmfan", | |
142 | ctx, pwm_fan_groups); | |
143 | if (IS_ERR(hwmon)) { | |
144 | dev_err(&pdev->dev, "Failed to register hwmon device\n"); | |
145 | pwm_disable(ctx->pwm); | |
146 | return PTR_ERR(hwmon); | |
147 | } | |
148 | return 0; | |
149 | } | |
150 | ||
151 | static int pwm_fan_remove(struct platform_device *pdev) | |
152 | { | |
153 | struct pwm_fan_ctx *ctx = platform_get_drvdata(pdev); | |
154 | ||
155 | if (ctx->pwm_value) | |
156 | pwm_disable(ctx->pwm); | |
157 | return 0; | |
158 | } | |
159 | ||
160 | #ifdef CONFIG_PM_SLEEP | |
161 | static int pwm_fan_suspend(struct device *dev) | |
162 | { | |
163 | struct pwm_fan_ctx *ctx = dev_get_drvdata(dev); | |
164 | ||
165 | if (ctx->pwm_value) | |
166 | pwm_disable(ctx->pwm); | |
167 | return 0; | |
168 | } | |
169 | ||
170 | static int pwm_fan_resume(struct device *dev) | |
171 | { | |
172 | struct pwm_fan_ctx *ctx = dev_get_drvdata(dev); | |
48b9d5b4 KD |
173 | unsigned long duty; |
174 | int ret; | |
d82d5776 | 175 | |
48b9d5b4 KD |
176 | if (ctx->pwm_value == 0) |
177 | return 0; | |
178 | ||
179 | duty = DIV_ROUND_UP(ctx->pwm_value * (ctx->pwm->period - 1), MAX_PWM); | |
180 | ret = pwm_config(ctx->pwm, duty, ctx->pwm->period); | |
181 | if (ret) | |
182 | return ret; | |
183 | return pwm_enable(ctx->pwm); | |
d82d5776 KD |
184 | } |
185 | #endif | |
186 | ||
187 | static SIMPLE_DEV_PM_OPS(pwm_fan_pm, pwm_fan_suspend, pwm_fan_resume); | |
188 | ||
189 | static struct of_device_id of_pwm_fan_match[] = { | |
190 | { .compatible = "pwm-fan", }, | |
191 | {}, | |
192 | }; | |
193 | ||
194 | static struct platform_driver pwm_fan_driver = { | |
195 | .probe = pwm_fan_probe, | |
196 | .remove = pwm_fan_remove, | |
197 | .driver = { | |
198 | .name = "pwm-fan", | |
199 | .pm = &pwm_fan_pm, | |
200 | .of_match_table = of_pwm_fan_match, | |
201 | }, | |
202 | }; | |
203 | ||
204 | module_platform_driver(pwm_fan_driver); | |
205 | ||
206 | MODULE_AUTHOR("Kamil Debski <k.debski@samsung.com>"); | |
207 | MODULE_ALIAS("platform:pwm-fan"); | |
208 | MODULE_DESCRIPTION("PWM FAN driver"); | |
209 | MODULE_LICENSE("GPL"); |