Blame view

drivers/pwm/pwm-atmel-hlcdc.c 7.51 KB
caab277b1   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-only
2b4984bef   Boris Brezillon   pwm: add support ...
2
3
4
5
6
  /*
   * Copyright (C) 2014 Free Electrons
   * Copyright (C) 2014 Atmel
   *
   * Author: Boris BREZILLON <boris.brezillon@free-electrons.com>
2b4984bef   Boris Brezillon   pwm: add support ...
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
   */
  
  #include <linux/clk.h>
  #include <linux/delay.h>
  #include <linux/mfd/atmel-hlcdc.h>
  #include <linux/module.h>
  #include <linux/platform_device.h>
  #include <linux/pwm.h>
  #include <linux/regmap.h>
  
  #define ATMEL_HLCDC_PWMCVAL_MASK	GENMASK(15, 8)
  #define ATMEL_HLCDC_PWMCVAL(x)		(((x) << 8) & ATMEL_HLCDC_PWMCVAL_MASK)
  #define ATMEL_HLCDC_PWMPOL		BIT(4)
  #define ATMEL_HLCDC_PWMPS_MASK		GENMASK(2, 0)
  #define ATMEL_HLCDC_PWMPS_MAX		0x6
  #define ATMEL_HLCDC_PWMPS(x)		((x) & ATMEL_HLCDC_PWMPS_MASK)
39e046f2c   Boris BREZILLON   pwm: atmel-hlcdc:...
23
24
25
26
  struct atmel_hlcdc_pwm_errata {
  	bool slow_clk_erratum;
  	bool div1_clk_erratum;
  };
2b4984bef   Boris Brezillon   pwm: add support ...
27
28
29
30
  struct atmel_hlcdc_pwm {
  	struct pwm_chip chip;
  	struct atmel_hlcdc *hlcdc;
  	struct clk *cur_clk;
39e046f2c   Boris BREZILLON   pwm: atmel-hlcdc:...
31
  	const struct atmel_hlcdc_pwm_errata *errata;
2b4984bef   Boris Brezillon   pwm: add support ...
32
33
34
35
36
37
  };
  
  static inline struct atmel_hlcdc_pwm *to_atmel_hlcdc_pwm(struct pwm_chip *chip)
  {
  	return container_of(chip, struct atmel_hlcdc_pwm, chip);
  }
2267517cd   Boris Brezillon   pwm: atmel-hlcdc:...
38
39
  static int atmel_hlcdc_pwm_apply(struct pwm_chip *c, struct pwm_device *pwm,
  				 struct pwm_state *state)
2b4984bef   Boris Brezillon   pwm: add support ...
40
41
42
  {
  	struct atmel_hlcdc_pwm *chip = to_atmel_hlcdc_pwm(c);
  	struct atmel_hlcdc *hlcdc = chip->hlcdc;
2267517cd   Boris Brezillon   pwm: atmel-hlcdc:...
43
44
  	unsigned int status;
  	int ret;
2b4984bef   Boris Brezillon   pwm: add support ...
45

2267517cd   Boris Brezillon   pwm: atmel-hlcdc:...
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
  	if (state->enabled) {
  		struct clk *new_clk = hlcdc->slow_clk;
  		u64 pwmcval = state->duty_cycle * 256;
  		unsigned long clk_freq;
  		u64 clk_period_ns;
  		u32 pwmcfg;
  		int pres;
  
  		if (!chip->errata || !chip->errata->slow_clk_erratum) {
  			clk_freq = clk_get_rate(new_clk);
  			if (!clk_freq)
  				return -EINVAL;
  
  			clk_period_ns = (u64)NSEC_PER_SEC * 256;
  			do_div(clk_period_ns, clk_freq);
  		}
  
  		/* Errata: cannot use slow clk on some IP revisions */
  		if ((chip->errata && chip->errata->slow_clk_erratum) ||
  		    clk_period_ns > state->period) {
  			new_clk = hlcdc->sys_clk;
  			clk_freq = clk_get_rate(new_clk);
  			if (!clk_freq)
  				return -EINVAL;
  
  			clk_period_ns = (u64)NSEC_PER_SEC * 256;
  			do_div(clk_period_ns, clk_freq);
  		}
  
  		for (pres = 0; pres <= ATMEL_HLCDC_PWMPS_MAX; pres++) {
39e046f2c   Boris BREZILLON   pwm: atmel-hlcdc:...
76
  		/* Errata: cannot divide by 1 on some IP revisions */
2267517cd   Boris Brezillon   pwm: atmel-hlcdc:...
77
78
79
  			if (!pres && chip->errata &&
  			    chip->errata->div1_clk_erratum)
  				continue;
2b4984bef   Boris Brezillon   pwm: add support ...
80

2267517cd   Boris Brezillon   pwm: atmel-hlcdc:...
81
82
83
  			if ((clk_period_ns << pres) >= state->period)
  				break;
  		}
2b4984bef   Boris Brezillon   pwm: add support ...
84

2267517cd   Boris Brezillon   pwm: atmel-hlcdc:...
85
86
  		if (pres > ATMEL_HLCDC_PWMPS_MAX)
  			return -EINVAL;
2b4984bef   Boris Brezillon   pwm: add support ...
87

2267517cd   Boris Brezillon   pwm: atmel-hlcdc:...
88
  		pwmcfg = ATMEL_HLCDC_PWMPS(pres);
2b4984bef   Boris Brezillon   pwm: add support ...
89

2267517cd   Boris Brezillon   pwm: atmel-hlcdc:...
90
91
92
  		if (new_clk != chip->cur_clk) {
  			u32 gencfg = 0;
  			int ret;
2b4984bef   Boris Brezillon   pwm: add support ...
93

2267517cd   Boris Brezillon   pwm: atmel-hlcdc:...
94
95
96
  			ret = clk_prepare_enable(new_clk);
  			if (ret)
  				return ret;
2b4984bef   Boris Brezillon   pwm: add support ...
97

2267517cd   Boris Brezillon   pwm: atmel-hlcdc:...
98
99
  			clk_disable_unprepare(chip->cur_clk);
  			chip->cur_clk = new_clk;
2b4984bef   Boris Brezillon   pwm: add support ...
100

2267517cd   Boris Brezillon   pwm: atmel-hlcdc:...
101
102
  			if (new_clk == hlcdc->sys_clk)
  				gencfg = ATMEL_HLCDC_CLKPWMSEL;
2b4984bef   Boris Brezillon   pwm: add support ...
103

2267517cd   Boris Brezillon   pwm: atmel-hlcdc:...
104
105
106
107
108
109
110
  			ret = regmap_update_bits(hlcdc->regmap,
  						 ATMEL_HLCDC_CFG(0),
  						 ATMEL_HLCDC_CLKPWMSEL,
  						 gencfg);
  			if (ret)
  				return ret;
  		}
2b4984bef   Boris Brezillon   pwm: add support ...
111

2267517cd   Boris Brezillon   pwm: atmel-hlcdc:...
112
  		do_div(pwmcval, state->period);
2b4984bef   Boris Brezillon   pwm: add support ...
113

2267517cd   Boris Brezillon   pwm: atmel-hlcdc:...
114
115
116
117
118
119
120
121
  		/*
  		 * The PWM duty cycle is configurable from 0/256 to 255/256 of
  		 * the period cycle. Hence we can't set a duty cycle occupying
  		 * the whole period cycle if we're asked to.
  		 * Set it to 255 if pwmcval is greater than 256.
  		 */
  		if (pwmcval > 255)
  			pwmcval = 255;
2b4984bef   Boris Brezillon   pwm: add support ...
122

2267517cd   Boris Brezillon   pwm: atmel-hlcdc:...
123
  		pwmcfg |= ATMEL_HLCDC_PWMCVAL(pwmcval);
2b4984bef   Boris Brezillon   pwm: add support ...
124

2267517cd   Boris Brezillon   pwm: atmel-hlcdc:...
125
126
  		if (state->polarity == PWM_POLARITY_NORMAL)
  			pwmcfg |= ATMEL_HLCDC_PWMPOL;
2b4984bef   Boris Brezillon   pwm: add support ...
127

2267517cd   Boris Brezillon   pwm: atmel-hlcdc:...
128
129
130
131
132
133
134
  		ret = regmap_update_bits(hlcdc->regmap, ATMEL_HLCDC_CFG(6),
  					 ATMEL_HLCDC_PWMCVAL_MASK |
  					 ATMEL_HLCDC_PWMPS_MASK |
  					 ATMEL_HLCDC_PWMPOL,
  					 pwmcfg);
  		if (ret)
  			return ret;
2b4984bef   Boris Brezillon   pwm: add support ...
135

2267517cd   Boris Brezillon   pwm: atmel-hlcdc:...
136
137
138
139
  		ret = regmap_write(hlcdc->regmap, ATMEL_HLCDC_EN,
  				   ATMEL_HLCDC_PWM);
  		if (ret)
  			return ret;
2b4984bef   Boris Brezillon   pwm: add support ...
140

2267517cd   Boris Brezillon   pwm: atmel-hlcdc:...
141
142
143
144
145
146
147
148
149
  		ret = regmap_read_poll_timeout(hlcdc->regmap, ATMEL_HLCDC_SR,
  					       status,
  					       status & ATMEL_HLCDC_PWM,
  					       10, 0);
  		if (ret)
  			return ret;
  	} else {
  		ret = regmap_write(hlcdc->regmap, ATMEL_HLCDC_DIS,
  				   ATMEL_HLCDC_PWM);
2b4984bef   Boris Brezillon   pwm: add support ...
150
151
  		if (ret)
  			return ret;
2267517cd   Boris Brezillon   pwm: atmel-hlcdc:...
152
153
154
155
156
157
  		ret = regmap_read_poll_timeout(hlcdc->regmap, ATMEL_HLCDC_SR,
  					       status,
  					       !(status & ATMEL_HLCDC_PWM),
  					       10, 0);
  		if (ret)
  			return ret;
2b4984bef   Boris Brezillon   pwm: add support ...
158

2267517cd   Boris Brezillon   pwm: atmel-hlcdc:...
159
160
  		clk_disable_unprepare(chip->cur_clk);
  		chip->cur_clk = NULL;
2b4984bef   Boris Brezillon   pwm: add support ...
161
162
163
164
  	}
  
  	return 0;
  }
2b4984bef   Boris Brezillon   pwm: add support ...
165
  static const struct pwm_ops atmel_hlcdc_pwm_ops = {
2267517cd   Boris Brezillon   pwm: atmel-hlcdc:...
166
  	.apply = atmel_hlcdc_pwm_apply,
2b4984bef   Boris Brezillon   pwm: add support ...
167
168
  	.owner = THIS_MODULE,
  };
39e046f2c   Boris BREZILLON   pwm: atmel-hlcdc:...
169
170
171
172
173
174
175
  static const struct atmel_hlcdc_pwm_errata atmel_hlcdc_pwm_at91sam9x5_errata = {
  	.slow_clk_erratum = true,
  };
  
  static const struct atmel_hlcdc_pwm_errata atmel_hlcdc_pwm_sama5d3_errata = {
  	.div1_clk_erratum = true,
  };
f9bb9da7c   Boris Brezillon   pwm: atmel-hlcdc:...
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
  #ifdef CONFIG_PM_SLEEP
  static int atmel_hlcdc_pwm_suspend(struct device *dev)
  {
  	struct atmel_hlcdc_pwm *chip = dev_get_drvdata(dev);
  
  	/* Keep the periph clock enabled if the PWM is still running. */
  	if (pwm_is_enabled(&chip->chip.pwms[0]))
  		clk_disable_unprepare(chip->hlcdc->periph_clk);
  
  	return 0;
  }
  
  static int atmel_hlcdc_pwm_resume(struct device *dev)
  {
  	struct atmel_hlcdc_pwm *chip = dev_get_drvdata(dev);
  	struct pwm_state state;
  	int ret;
  
  	pwm_get_state(&chip->chip.pwms[0], &state);
  
  	/* Re-enable the periph clock it was stopped during suspend. */
  	if (!state.enabled) {
  		ret = clk_prepare_enable(chip->hlcdc->periph_clk);
  		if (ret)
  			return ret;
  	}
  
  	return atmel_hlcdc_pwm_apply(&chip->chip, &chip->chip.pwms[0], &state);
  }
  #endif
  
  static SIMPLE_DEV_PM_OPS(atmel_hlcdc_pwm_pm_ops,
  			 atmel_hlcdc_pwm_suspend, atmel_hlcdc_pwm_resume);
39e046f2c   Boris BREZILLON   pwm: atmel-hlcdc:...
209
210
  static const struct of_device_id atmel_hlcdc_dt_ids[] = {
  	{
7a5938201   Josh Wu   ARM: at91: pwm: a...
211
212
213
214
215
  		.compatible = "atmel,at91sam9n12-hlcdc",
  		/* 9n12 has same errata as 9x5 HLCDC PWM */
  		.data = &atmel_hlcdc_pwm_at91sam9x5_errata,
  	},
  	{
39e046f2c   Boris BREZILLON   pwm: atmel-hlcdc:...
216
217
218
219
  		.compatible = "atmel,at91sam9x5-hlcdc",
  		.data = &atmel_hlcdc_pwm_at91sam9x5_errata,
  	},
  	{
2b8b0ef35   Nicolas Ferre   pwm: atmel-hlcdc:...
220
221
222
  		.compatible = "atmel,sama5d2-hlcdc",
  	},
  	{
39e046f2c   Boris BREZILLON   pwm: atmel-hlcdc:...
223
224
225
  		.compatible = "atmel,sama5d3-hlcdc",
  		.data = &atmel_hlcdc_pwm_sama5d3_errata,
  	},
054d3e1f9   Nicolas Ferre   pwm: atmel-hlcdc:...
226
227
228
229
  	{
  		.compatible = "atmel,sama5d4-hlcdc",
  		.data = &atmel_hlcdc_pwm_sama5d3_errata,
  	},
da9b38649   Claudiu Beznea   pwm: atmel-hlcdc:...
230
  	{	.compatible = "microchip,sam9x60-hlcdc", },
39e046f2c   Boris BREZILLON   pwm: atmel-hlcdc:...
231
232
  	{ /* sentinel */ },
  };
a83a6a822   Luis de Bethencourt   pwm: atmel-hlcdc:...
233
  MODULE_DEVICE_TABLE(of, atmel_hlcdc_dt_ids);
39e046f2c   Boris BREZILLON   pwm: atmel-hlcdc:...
234

2b4984bef   Boris Brezillon   pwm: add support ...
235
236
  static int atmel_hlcdc_pwm_probe(struct platform_device *pdev)
  {
39e046f2c   Boris BREZILLON   pwm: atmel-hlcdc:...
237
  	const struct of_device_id *match;
2b4984bef   Boris Brezillon   pwm: add support ...
238
239
240
241
242
243
244
245
246
247
248
249
250
251
  	struct device *dev = &pdev->dev;
  	struct atmel_hlcdc_pwm *chip;
  	struct atmel_hlcdc *hlcdc;
  	int ret;
  
  	hlcdc = dev_get_drvdata(dev->parent);
  
  	chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
  	if (!chip)
  		return -ENOMEM;
  
  	ret = clk_prepare_enable(hlcdc->periph_clk);
  	if (ret)
  		return ret;
39e046f2c   Boris BREZILLON   pwm: atmel-hlcdc:...
252
253
254
  	match = of_match_node(atmel_hlcdc_dt_ids, dev->parent->of_node);
  	if (match)
  		chip->errata = match->data;
2b4984bef   Boris Brezillon   pwm: add support ...
255
256
257
258
259
260
261
  	chip->hlcdc = hlcdc;
  	chip->chip.ops = &atmel_hlcdc_pwm_ops;
  	chip->chip.dev = dev;
  	chip->chip.base = -1;
  	chip->chip.npwm = 1;
  	chip->chip.of_xlate = of_pwm_xlate_with_flags;
  	chip->chip.of_pwm_n_cells = 3;
2b4984bef   Boris Brezillon   pwm: add support ...
262

cc51846ba   Boris Brezillon   pwm: atmel-hlcdc:...
263
  	ret = pwmchip_add_with_polarity(&chip->chip, PWM_POLARITY_INVERSED);
2b4984bef   Boris Brezillon   pwm: add support ...
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
  	if (ret) {
  		clk_disable_unprepare(hlcdc->periph_clk);
  		return ret;
  	}
  
  	platform_set_drvdata(pdev, chip);
  
  	return 0;
  }
  
  static int atmel_hlcdc_pwm_remove(struct platform_device *pdev)
  {
  	struct atmel_hlcdc_pwm *chip = platform_get_drvdata(pdev);
  	int ret;
  
  	ret = pwmchip_remove(&chip->chip);
  	if (ret)
  		return ret;
  
  	clk_disable_unprepare(chip->hlcdc->periph_clk);
  
  	return 0;
  }
  
  static const struct of_device_id atmel_hlcdc_pwm_dt_ids[] = {
  	{ .compatible = "atmel,hlcdc-pwm" },
  	{ /* sentinel */ },
  };
  
  static struct platform_driver atmel_hlcdc_pwm_driver = {
  	.driver = {
  		.name = "atmel-hlcdc-pwm",
  		.of_match_table = atmel_hlcdc_pwm_dt_ids,
f9bb9da7c   Boris Brezillon   pwm: atmel-hlcdc:...
297
  		.pm = &atmel_hlcdc_pwm_pm_ops,
2b4984bef   Boris Brezillon   pwm: add support ...
298
299
300
301
302
303
304
305
306
307
  	},
  	.probe = atmel_hlcdc_pwm_probe,
  	.remove = atmel_hlcdc_pwm_remove,
  };
  module_platform_driver(atmel_hlcdc_pwm_driver);
  
  MODULE_ALIAS("platform:atmel-hlcdc-pwm");
  MODULE_AUTHOR("Boris Brezillon <boris.brezillon@free-electrons.com>");
  MODULE_DESCRIPTION("Atmel HLCDC PWM driver");
  MODULE_LICENSE("GPL v2");