Source
11
11
*/
12
12
13
13
#include <linux/clk.h>
14
14
#include <linux/err.h>
15
15
#include <linux/io.h>
16
16
#include <linux/mfd/syscon.h>
17
17
#include <linux/module.h>
18
18
#include <linux/of.h>
19
19
#include <linux/of_device.h>
20
20
#include <linux/platform_device.h>
21
+
#include <linux/pm_runtime.h>
21
22
#include <linux/pwm.h>
22
23
#include <linux/regmap.h>
23
24
#include <linux/slab.h>
24
25
25
26
/* PWM registers */
26
27
#define PWM_CTRL_CFG 0x0000
27
28
#define PWM_CTRL_CFG_NO_SUB_DIV 0
28
29
#define PWM_CTRL_CFG_SUB_DIV0 1
29
30
#define PWM_CTRL_CFG_SUB_DIV1 2
30
31
#define PWM_CTRL_CFG_SUB_DIV0_DIV1 3
32
33
#define PWM_CTRL_CFG_DIV_MASK 0x3
33
34
34
35
#define PWM_CH_CFG(ch) (0x4 + (ch) * 4)
35
36
#define PWM_CH_CFG_TMBASE_SHIFT 0
36
37
#define PWM_CH_CFG_DUTY_SHIFT 16
37
38
38
39
#define PERIP_PWM_PDM_CONTROL 0x0140
39
40
#define PERIP_PWM_PDM_CONTROL_CH_MASK 0x1
40
41
#define PERIP_PWM_PDM_CONTROL_CH_SHIFT(ch) ((ch) * 4)
41
42
43
+
#define IMG_PWM_PM_TIMEOUT 1000 /* ms */
44
+
42
45
/*
43
46
* PWM period is specified with a timebase register,
44
47
* in number of step periods. The PWM duty cycle is also
45
48
* specified in step periods, in the [0, $timebase] range.
46
49
* In other words, the timebase imposes the duty cycle
47
50
* resolution. Therefore, let's constraint the timebase to
48
51
* a minimum value to allow a sane range of duty cycle values.
49
52
* Imposing a minimum timebase, will impose a maximum PWM frequency.
50
53
*
51
54
* The value chosen is completely arbitrary.
89
92
return readl(chip->base + reg);
90
93
}
91
94
92
95
static int img_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
93
96
int duty_ns, int period_ns)
94
97
{
95
98
u32 val, div, duty, timebase;
96
99
unsigned long mul, output_clk_hz, input_clk_hz;
97
100
struct img_pwm_chip *pwm_chip = to_img_pwm_chip(chip);
98
101
unsigned int max_timebase = pwm_chip->data->max_timebase;
102
+
int ret;
99
103
100
104
if (period_ns < pwm_chip->min_period_ns ||
101
105
period_ns > pwm_chip->max_period_ns) {
102
106
dev_err(chip->dev, "configured period not in range\n");
103
107
return -ERANGE;
104
108
}
105
109
106
110
input_clk_hz = clk_get_rate(pwm_chip->pwm_clk);
107
111
output_clk_hz = DIV_ROUND_UP(NSEC_PER_SEC, period_ns);
108
112
120
124
div = PWM_CTRL_CFG_SUB_DIV0_DIV1;
121
125
timebase = DIV_ROUND_UP(mul, 512);
122
126
} else if (mul > max_timebase * 512) {
123
127
dev_err(chip->dev,
124
128
"failed to configure timebase steps/divider value\n");
125
129
return -EINVAL;
126
130
}
127
131
128
132
duty = DIV_ROUND_UP(timebase * duty_ns, period_ns);
129
133
134
+
ret = pm_runtime_get_sync(chip->dev);
135
+
if (ret < 0)
136
+
return ret;
137
+
130
138
val = img_pwm_readl(pwm_chip, PWM_CTRL_CFG);
131
139
val &= ~(PWM_CTRL_CFG_DIV_MASK << PWM_CTRL_CFG_DIV_SHIFT(pwm->hwpwm));
132
140
val |= (div & PWM_CTRL_CFG_DIV_MASK) <<
133
141
PWM_CTRL_CFG_DIV_SHIFT(pwm->hwpwm);
134
142
img_pwm_writel(pwm_chip, PWM_CTRL_CFG, val);
135
143
136
144
val = (duty << PWM_CH_CFG_DUTY_SHIFT) |
137
145
(timebase << PWM_CH_CFG_TMBASE_SHIFT);
138
146
img_pwm_writel(pwm_chip, PWM_CH_CFG(pwm->hwpwm), val);
139
147
148
+
pm_runtime_mark_last_busy(chip->dev);
149
+
pm_runtime_put_autosuspend(chip->dev);
150
+
140
151
return 0;
141
152
}
142
153
143
154
static int img_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
144
155
{
145
156
u32 val;
146
157
struct img_pwm_chip *pwm_chip = to_img_pwm_chip(chip);
158
+
int ret;
159
+
160
+
ret = pm_runtime_get_sync(chip->dev);
161
+
if (ret < 0)
162
+
return ret;
147
163
148
164
val = img_pwm_readl(pwm_chip, PWM_CTRL_CFG);
149
165
val |= BIT(pwm->hwpwm);
150
166
img_pwm_writel(pwm_chip, PWM_CTRL_CFG, val);
151
167
152
168
regmap_update_bits(pwm_chip->periph_regs, PERIP_PWM_PDM_CONTROL,
153
169
PERIP_PWM_PDM_CONTROL_CH_MASK <<
154
170
PERIP_PWM_PDM_CONTROL_CH_SHIFT(pwm->hwpwm), 0);
155
171
156
172
return 0;
157
173
}
158
174
159
175
static void img_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
160
176
{
161
177
u32 val;
162
178
struct img_pwm_chip *pwm_chip = to_img_pwm_chip(chip);
163
179
164
180
val = img_pwm_readl(pwm_chip, PWM_CTRL_CFG);
165
181
val &= ~BIT(pwm->hwpwm);
166
182
img_pwm_writel(pwm_chip, PWM_CTRL_CFG, val);
183
+
184
+
pm_runtime_mark_last_busy(chip->dev);
185
+
pm_runtime_put_autosuspend(chip->dev);
167
186
}
168
187
169
188
static const struct pwm_ops img_pwm_ops = {
170
189
.config = img_pwm_config,
171
190
.enable = img_pwm_enable,
172
191
.disable = img_pwm_disable,
173
192
.owner = THIS_MODULE,
174
193
};
175
194
176
195
static const struct img_pwm_soc_data pistachio_pwm = {
179
198
180
199
static const struct of_device_id img_pwm_of_match[] = {
181
200
{
182
201
.compatible = "img,pistachio-pwm",
183
202
.data = &pistachio_pwm,
184
203
},
185
204
{ }
186
205
};
187
206
MODULE_DEVICE_TABLE(of, img_pwm_of_match);
188
207
208
+
static int img_pwm_runtime_suspend(struct device *dev)
209
+
{
210
+
struct img_pwm_chip *pwm_chip = dev_get_drvdata(dev);
211
+
212
+
clk_disable_unprepare(pwm_chip->pwm_clk);
213
+
clk_disable_unprepare(pwm_chip->sys_clk);
214
+
215
+
return 0;
216
+
}
217
+
218
+
static int img_pwm_runtime_resume(struct device *dev)
219
+
{
220
+
struct img_pwm_chip *pwm_chip = dev_get_drvdata(dev);
221
+
int ret;
222
+
223
+
ret = clk_prepare_enable(pwm_chip->sys_clk);
224
+
if (ret < 0) {
225
+
dev_err(dev, "could not prepare or enable sys clock\n");
226
+
return ret;
227
+
}
228
+
229
+
ret = clk_prepare_enable(pwm_chip->pwm_clk);
230
+
if (ret < 0) {
231
+
dev_err(dev, "could not prepare or enable pwm clock\n");
232
+
clk_disable_unprepare(pwm_chip->sys_clk);
233
+
return ret;
234
+
}
235
+
236
+
return 0;
237
+
}
238
+
189
239
static int img_pwm_probe(struct platform_device *pdev)
190
240
{
191
241
int ret;
192
242
u64 val;
193
243
unsigned long clk_rate;
194
244
struct resource *res;
195
245
struct img_pwm_chip *pwm;
196
246
const struct of_device_id *of_dev_id;
197
247
198
248
pwm = devm_kzalloc(&pdev->dev, sizeof(*pwm), GFP_KERNEL);
221
271
dev_err(&pdev->dev, "failed to get system clock\n");
222
272
return PTR_ERR(pwm->sys_clk);
223
273
}
224
274
225
275
pwm->pwm_clk = devm_clk_get(&pdev->dev, "pwm");
226
276
if (IS_ERR(pwm->pwm_clk)) {
227
277
dev_err(&pdev->dev, "failed to get pwm clock\n");
228
278
return PTR_ERR(pwm->pwm_clk);
229
279
}
230
280
231
-
ret = clk_prepare_enable(pwm->sys_clk);
232
-
if (ret < 0) {
233
-
dev_err(&pdev->dev, "could not prepare or enable sys clock\n");
234
-
return ret;
235
-
}
236
-
237
-
ret = clk_prepare_enable(pwm->pwm_clk);
238
-
if (ret < 0) {
239
-
dev_err(&pdev->dev, "could not prepare or enable pwm clock\n");
240
-
goto disable_sysclk;
281
+
pm_runtime_set_autosuspend_delay(&pdev->dev, IMG_PWM_PM_TIMEOUT);
282
+
pm_runtime_use_autosuspend(&pdev->dev);
283
+
pm_runtime_enable(&pdev->dev);
284
+
if (!pm_runtime_enabled(&pdev->dev)) {
285
+
ret = img_pwm_runtime_resume(&pdev->dev);
286
+
if (ret)
287
+
goto err_pm_disable;
241
288
}
242
289
243
290
clk_rate = clk_get_rate(pwm->pwm_clk);
244
291
if (!clk_rate) {
245
292
dev_err(&pdev->dev, "pwm clock has no frequency\n");
246
293
ret = -EINVAL;
247
-
goto disable_pwmclk;
294
+
goto err_suspend;
248
295
}
249
296
250
297
/* The maximum input clock divider is 512 */
251
298
val = (u64)NSEC_PER_SEC * 512 * pwm->data->max_timebase;
252
299
do_div(val, clk_rate);
253
300
pwm->max_period_ns = val;
254
301
255
302
val = (u64)NSEC_PER_SEC * MIN_TMBASE_STEPS;
256
303
do_div(val, clk_rate);
257
304
pwm->min_period_ns = val;
258
305
259
306
pwm->chip.dev = &pdev->dev;
260
307
pwm->chip.ops = &img_pwm_ops;
261
308
pwm->chip.base = -1;
262
309
pwm->chip.npwm = IMG_PWM_NPWM;
263
310
264
311
ret = pwmchip_add(&pwm->chip);
265
312
if (ret < 0) {
266
313
dev_err(&pdev->dev, "pwmchip_add failed: %d\n", ret);
267
-
goto disable_pwmclk;
314
+
goto err_suspend;
268
315
}
269
316
270
317
platform_set_drvdata(pdev, pwm);
271
318
return 0;
272
319
273
-
disable_pwmclk:
274
-
clk_disable_unprepare(pwm->pwm_clk);
275
-
disable_sysclk:
276
-
clk_disable_unprepare(pwm->sys_clk);
320
+
err_suspend:
321
+
if (!pm_runtime_enabled(&pdev->dev))
322
+
img_pwm_runtime_suspend(&pdev->dev);
323
+
err_pm_disable:
324
+
pm_runtime_disable(&pdev->dev);
325
+
pm_runtime_dont_use_autosuspend(&pdev->dev);
277
326
return ret;
278
327
}
279
328
280
329
static int img_pwm_remove(struct platform_device *pdev)
281
330
{
282
331
struct img_pwm_chip *pwm_chip = platform_get_drvdata(pdev);
283
332
u32 val;
284
333
unsigned int i;
334
+
int ret;
335
+
336
+
ret = pm_runtime_get_sync(&pdev->dev);
337
+
if (ret < 0)
338
+
return ret;
285
339
286
340
for (i = 0; i < pwm_chip->chip.npwm; i++) {
287
341
val = img_pwm_readl(pwm_chip, PWM_CTRL_CFG);
288
342
val &= ~BIT(i);
289
343
img_pwm_writel(pwm_chip, PWM_CTRL_CFG, val);
290
344
}
291
345
292
-
clk_disable_unprepare(pwm_chip->pwm_clk);
293
-
clk_disable_unprepare(pwm_chip->sys_clk);
346
+
pm_runtime_put(&pdev->dev);
347
+
pm_runtime_disable(&pdev->dev);
348
+
if (!pm_runtime_status_suspended(&pdev->dev))
349
+
img_pwm_runtime_suspend(&pdev->dev);
294
350
295
351
return pwmchip_remove(&pwm_chip->chip);
296
352
}
297
353
298
354
#ifdef CONFIG_PM_SLEEP
299
355
static int img_pwm_suspend(struct device *dev)
300
356
{
301
-
struct platform_device *pdev = to_platform_device(dev);
302
-
struct img_pwm_chip *pwm_chip = platform_get_drvdata(pdev);
303
-
int i;
357
+
struct img_pwm_chip *pwm_chip = dev_get_drvdata(dev);
358
+
int i, ret;
359
+
360
+
if (pm_runtime_status_suspended(dev)) {
361
+
ret = img_pwm_runtime_resume(dev);
362
+
if (ret)
363
+
return ret;
364
+
}
304
365
305
366
for (i = 0; i < pwm_chip->chip.npwm; i++)
306
367
pwm_chip->suspend_ch_cfg[i] = img_pwm_readl(pwm_chip,
307
368
PWM_CH_CFG(i));
308
369
309
370
pwm_chip->suspend_ctrl_cfg = img_pwm_readl(pwm_chip, PWM_CTRL_CFG);
310
371
311
-
clk_disable_unprepare(pwm_chip->pwm_clk);
312
-
clk_disable_unprepare(pwm_chip->sys_clk);
372
+
img_pwm_runtime_suspend(dev);
313
373
314
374
return 0;
315
375
}
316
376
317
377
static int img_pwm_resume(struct device *dev)
318
378
{
319
-
struct platform_device *pdev = to_platform_device(dev);
320
-
struct img_pwm_chip *pwm_chip = platform_get_drvdata(pdev);
379
+
struct img_pwm_chip *pwm_chip = dev_get_drvdata(dev);
321
380
int ret;
322
381
int i;
323
382
324
-
ret = clk_prepare_enable(pwm_chip->sys_clk);
325
-
if (ret < 0) {
326
-
dev_err(&pdev->dev, "could not prepare or enable sys clock\n");
383
+
ret = img_pwm_runtime_resume(dev);
384
+
if (ret)
327
385
return ret;
328
-
}
329
-
330
-
ret = clk_prepare_enable(pwm_chip->pwm_clk);
331
-
if (ret < 0) {
332
-
dev_err(&pdev->dev, "could not prepare or enable pwm clock\n");
333
-
clk_disable_unprepare(pwm_chip->sys_clk);
334
-
return ret;
335
-
}
336
386
337
387
for (i = 0; i < pwm_chip->chip.npwm; i++)
338
388
img_pwm_writel(pwm_chip, PWM_CH_CFG(i),
339
389
pwm_chip->suspend_ch_cfg[i]);
340
390
341
391
img_pwm_writel(pwm_chip, PWM_CTRL_CFG, pwm_chip->suspend_ctrl_cfg);
342
392
343
393
for (i = 0; i < pwm_chip->chip.npwm; i++)
344
394
if (pwm_chip->suspend_ctrl_cfg & BIT(i))
345
395
regmap_update_bits(pwm_chip->periph_regs,
346
396
PERIP_PWM_PDM_CONTROL,
347
397
PERIP_PWM_PDM_CONTROL_CH_MASK <<
348
398
PERIP_PWM_PDM_CONTROL_CH_SHIFT(i),
349
399
0);
350
400
401
+
if (pm_runtime_status_suspended(dev))
402
+
img_pwm_runtime_suspend(dev);
403
+
351
404
return 0;
352
405
}
353
406
#endif /* CONFIG_PM */
354
407
355
-
SIMPLE_DEV_PM_OPS(img_pwm_pm_ops, img_pwm_suspend, img_pwm_resume);
408
+
static const struct dev_pm_ops img_pwm_pm_ops = {
409
+
SET_RUNTIME_PM_OPS(img_pwm_runtime_suspend,
410
+
img_pwm_runtime_resume,
411
+
NULL)
412
+
SET_SYSTEM_SLEEP_PM_OPS(img_pwm_suspend, img_pwm_resume)
413
+
};
356
414
357
415
static struct platform_driver img_pwm_driver = {
358
416
.driver = {
359
417
.name = "img-pwm",
360
418
.pm = &img_pwm_pm_ops,
361
419
.of_match_table = img_pwm_of_match,
362
420
},
363
421
.probe = img_pwm_probe,
364
422
.remove = img_pwm_remove,
365
423
};