Blame view

drivers/pwm/pwm-pxa.c 5.46 KB
75540c1ac   eric miao   [ARM] pxa: Add PX...
1
  /*
45b301d2b   Axel Lin   pwm: Convert pwm-...
2
   * drivers/pwm/pwm-pxa.c
75540c1ac   eric miao   [ARM] pxa: Add PX...
3
4
5
6
7
8
9
10
   *
   * simple driver for PWM (Pulse Width Modulator) controller
   *
   * This program is free software; you can redistribute it and/or modify
   * it under the terms of the GNU General Public License version 2 as
   * published by the Free Software Foundation.
   *
   * 2008-02-13	initial version
b07ab6639   Jingoo Han   pwm: pxa: remove ...
11
   *		eric miao <eric.miao@marvell.com>
75540c1ac   eric miao   [ARM] pxa: Add PX...
12
13
14
15
16
   */
  
  #include <linux/module.h>
  #include <linux/kernel.h>
  #include <linux/platform_device.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
17
  #include <linux/slab.h>
75540c1ac   eric miao   [ARM] pxa: Add PX...
18
19
20
21
  #include <linux/err.h>
  #include <linux/clk.h>
  #include <linux/io.h>
  #include <linux/pwm.h>
b52fa7bc5   Mike Dunn   pwm: pxa: Add dev...
22
  #include <linux/of_device.h>
75540c1ac   eric miao   [ARM] pxa: Add PX...
23
24
  
  #include <asm/div64.h>
75540c1ac   eric miao   [ARM] pxa: Add PX...
25

3d2a98cd5   Eric Miao   [ARM] pxa: simpli...
26
27
28
29
30
  #define HAS_SECONDARY_PWM	0x10
  
  static const struct platform_device_id pwm_id_table[] = {
  	/*   PWM    has_secondary_pwm? */
  	{ "pxa25x-pwm", 0 },
22976a5da   Axel Lin   pwm: pxa: Remove ...
31
32
33
  	{ "pxa27x-pwm", HAS_SECONDARY_PWM },
  	{ "pxa168-pwm", 0 },
  	{ "pxa910-pwm", 0 },
3d2a98cd5   Eric Miao   [ARM] pxa: simpli...
34
35
36
  	{ },
  };
  MODULE_DEVICE_TABLE(platform, pwm_id_table);
75540c1ac   eric miao   [ARM] pxa: Add PX...
37
38
39
40
41
42
43
  /* PWM registers and bits definitions */
  #define PWMCR		(0x00)
  #define PWMDCR		(0x04)
  #define PWMPCR		(0x08)
  
  #define PWMCR_SD	(1 << 6)
  #define PWMDCR_FD	(1 << 10)
17b2b4780   Thierry Reding   pwm: Move PXA PWM...
44
45
46
  struct pxa_pwm_chip {
  	struct pwm_chip	chip;
  	struct device	*dev;
75540c1ac   eric miao   [ARM] pxa: Add PX...
47

75540c1ac   eric miao   [ARM] pxa: Add PX...
48
49
  	struct clk	*clk;
  	void __iomem	*mmio_base;
75540c1ac   eric miao   [ARM] pxa: Add PX...
50
  };
17b2b4780   Thierry Reding   pwm: Move PXA PWM...
51
52
53
54
  static inline struct pxa_pwm_chip *to_pxa_pwm_chip(struct pwm_chip *chip)
  {
  	return container_of(chip, struct pxa_pwm_chip, chip);
  }
75540c1ac   eric miao   [ARM] pxa: Add PX...
55
56
57
58
  /*
   * period_ns = 10^9 * (PRESCALE + 1) * (PV + 1) / PWM_CLK_RATE
   * duty_ns   = 10^9 * (PRESCALE + 1) * DC / PWM_CLK_RATE
   */
17b2b4780   Thierry Reding   pwm: Move PXA PWM...
59
60
  static int pxa_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
  			  int duty_ns, int period_ns)
75540c1ac   eric miao   [ARM] pxa: Add PX...
61
  {
17b2b4780   Thierry Reding   pwm: Move PXA PWM...
62
  	struct pxa_pwm_chip *pc = to_pxa_pwm_chip(chip);
75540c1ac   eric miao   [ARM] pxa: Add PX...
63
64
  	unsigned long long c;
  	unsigned long period_cycles, prescale, pv, dc;
17b2b4780   Thierry Reding   pwm: Move PXA PWM...
65
66
  	unsigned long offset;
  	int rc;
75540c1ac   eric miao   [ARM] pxa: Add PX...
67

17b2b4780   Thierry Reding   pwm: Move PXA PWM...
68
69
70
  	offset = pwm->hwpwm ? 0x10 : 0;
  
  	c = clk_get_rate(pc->clk);
75540c1ac   eric miao   [ARM] pxa: Add PX...
71
72
73
  	c = c * period_ns;
  	do_div(c, 1000000000);
  	period_cycles = c;
71a35d756   roelkluin   [ARM] 5303/1: per...
74
  	if (period_cycles < 1)
75540c1ac   eric miao   [ARM] pxa: Add PX...
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
  		period_cycles = 1;
  	prescale = (period_cycles - 1) / 1024;
  	pv = period_cycles / (prescale + 1) - 1;
  
  	if (prescale > 63)
  		return -EINVAL;
  
  	if (duty_ns == period_ns)
  		dc = PWMDCR_FD;
  	else
  		dc = (pv + 1) * duty_ns / period_ns;
  
  	/* NOTE: the clock to PWM has to be enabled first
  	 * before writing to the registers
  	 */
17b2b4780   Thierry Reding   pwm: Move PXA PWM...
90
91
92
93
94
95
96
  	rc = clk_prepare_enable(pc->clk);
  	if (rc < 0)
  		return rc;
  
  	writel(prescale, pc->mmio_base + offset + PWMCR);
  	writel(dc, pc->mmio_base + offset + PWMDCR);
  	writel(pv, pc->mmio_base + offset + PWMPCR);
75540c1ac   eric miao   [ARM] pxa: Add PX...
97

17b2b4780   Thierry Reding   pwm: Move PXA PWM...
98
  	clk_disable_unprepare(pc->clk);
75540c1ac   eric miao   [ARM] pxa: Add PX...
99
100
  	return 0;
  }
75540c1ac   eric miao   [ARM] pxa: Add PX...
101

17b2b4780   Thierry Reding   pwm: Move PXA PWM...
102
  static int pxa_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
75540c1ac   eric miao   [ARM] pxa: Add PX...
103
  {
17b2b4780   Thierry Reding   pwm: Move PXA PWM...
104
  	struct pxa_pwm_chip *pc = to_pxa_pwm_chip(chip);
c860d701c   Robert Jarzmik   [ARM] 5087/1: Get...
105

b014a30c5   Axel Lin   pwm: pxa: Remove ...
106
  	return clk_prepare_enable(pc->clk);
75540c1ac   eric miao   [ARM] pxa: Add PX...
107
  }
75540c1ac   eric miao   [ARM] pxa: Add PX...
108

17b2b4780   Thierry Reding   pwm: Move PXA PWM...
109
  static void pxa_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
75540c1ac   eric miao   [ARM] pxa: Add PX...
110
  {
17b2b4780   Thierry Reding   pwm: Move PXA PWM...
111
  	struct pxa_pwm_chip *pc = to_pxa_pwm_chip(chip);
75540c1ac   eric miao   [ARM] pxa: Add PX...
112

b014a30c5   Axel Lin   pwm: pxa: Remove ...
113
  	clk_disable_unprepare(pc->clk);
75540c1ac   eric miao   [ARM] pxa: Add PX...
114
  }
75540c1ac   eric miao   [ARM] pxa: Add PX...
115

17b2b4780   Thierry Reding   pwm: Move PXA PWM...
116
117
118
119
120
121
  static struct pwm_ops pxa_pwm_ops = {
  	.config = pxa_pwm_config,
  	.enable = pxa_pwm_enable,
  	.disable = pxa_pwm_disable,
  	.owner = THIS_MODULE,
  };
75540c1ac   eric miao   [ARM] pxa: Add PX...
122

b52fa7bc5   Mike Dunn   pwm: pxa: Add dev...
123
124
  #ifdef CONFIG_OF
  /*
fdec4f727   Thierry Reding   pwm: pxa: Fix typ...
125
   * Device tree users must create one device instance for each PWM channel.
b52fa7bc5   Mike Dunn   pwm: pxa: Add dev...
126
127
128
129
   * Hence we dispense with the HAS_SECONDARY_PWM and "tell" the original driver
   * code that this is a single channel pxa25x-pwm.  Currently all devices are
   * supported identically.
   */
2ae69a460   Thierry Reding   pwm: pxa: Constif...
130
  static const struct of_device_id pwm_of_match[] = {
b52fa7bc5   Mike Dunn   pwm: pxa: Add dev...
131
132
133
134
135
136
137
  	{ .compatible = "marvell,pxa250-pwm", .data = &pwm_id_table[0]},
  	{ .compatible = "marvell,pxa270-pwm", .data = &pwm_id_table[0]},
  	{ .compatible = "marvell,pxa168-pwm", .data = &pwm_id_table[0]},
  	{ .compatible = "marvell,pxa910-pwm", .data = &pwm_id_table[0]},
  	{ }
  };
  MODULE_DEVICE_TABLE(of, pwm_of_match);
f409cd383   Thierry Reding   Revert "pwm: pxa:...
138
139
  #else
  #define pwm_of_match NULL
b52fa7bc5   Mike Dunn   pwm: pxa: Add dev...
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
  #endif
  
  static const struct platform_device_id *pxa_pwm_get_id_dt(struct device *dev)
  {
  	const struct of_device_id *id = of_match_device(pwm_of_match, dev);
  
  	return id ? id->data : NULL;
  }
  
  static struct pwm_device *
  pxa_pwm_of_xlate(struct pwm_chip *pc, const struct of_phandle_args *args)
  {
  	struct pwm_device *pwm;
  
  	pwm = pwm_request_from_chip(pc, 0, NULL);
  	if (IS_ERR(pwm))
  		return pwm;
  
  	pwm_set_period(pwm, args->args[0]);
  
  	return pwm;
  }
3e9fe83d2   Bill Pemberton   pwm: remove use o...
162
  static int pwm_probe(struct platform_device *pdev)
75540c1ac   eric miao   [ARM] pxa: Add PX...
163
  {
b3282ab11   Uwe Kleine-König   ARM: pxa: Make id...
164
  	const struct platform_device_id *id = platform_get_device_id(pdev);
17b2b4780   Thierry Reding   pwm: Move PXA PWM...
165
  	struct pxa_pwm_chip *pwm;
75540c1ac   eric miao   [ARM] pxa: Add PX...
166
167
  	struct resource *r;
  	int ret = 0;
b52fa7bc5   Mike Dunn   pwm: pxa: Add dev...
168
169
170
171
172
  	if (IS_ENABLED(CONFIG_OF) && id == NULL)
  		id = pxa_pwm_get_id_dt(&pdev->dev);
  
  	if (id == NULL)
  		return -EINVAL;
45b301d2b   Axel Lin   pwm: Convert pwm-...
173
  	pwm = devm_kzalloc(&pdev->dev, sizeof(*pwm), GFP_KERNEL);
d93fc78f4   Jingoo Han   pwm: pxa: Remove ...
174
  	if (pwm == NULL)
3d2a98cd5   Eric Miao   [ARM] pxa: simpli...
175
  		return -ENOMEM;
75540c1ac   eric miao   [ARM] pxa: Add PX...
176

45b301d2b   Axel Lin   pwm: Convert pwm-...
177
178
179
  	pwm->clk = devm_clk_get(&pdev->dev, NULL);
  	if (IS_ERR(pwm->clk))
  		return PTR_ERR(pwm->clk);
17b2b4780   Thierry Reding   pwm: Move PXA PWM...
180
181
182
183
  	pwm->chip.dev = &pdev->dev;
  	pwm->chip.ops = &pxa_pwm_ops;
  	pwm->chip.base = -1;
  	pwm->chip.npwm = (id->driver_data & HAS_SECONDARY_PWM) ? 2 : 1;
75540c1ac   eric miao   [ARM] pxa: Add PX...
184

b52fa7bc5   Mike Dunn   pwm: pxa: Add dev...
185
186
187
188
  	if (IS_ENABLED(CONFIG_OF)) {
  		pwm->chip.of_xlate = pxa_pwm_of_xlate;
  		pwm->chip.of_pwm_n_cells = 1;
  	}
75540c1ac   eric miao   [ARM] pxa: Add PX...
189
  	r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
6d4294d16   Thierry Reding   pwm: Convert to d...
190
191
192
  	pwm->mmio_base = devm_ioremap_resource(&pdev->dev, r);
  	if (IS_ERR(pwm->mmio_base))
  		return PTR_ERR(pwm->mmio_base);
75540c1ac   eric miao   [ARM] pxa: Add PX...
193

17b2b4780   Thierry Reding   pwm: Move PXA PWM...
194
195
196
197
198
  	ret = pwmchip_add(&pwm->chip);
  	if (ret < 0) {
  		dev_err(&pdev->dev, "pwmchip_add() failed: %d
  ", ret);
  		return ret;
3d2a98cd5   Eric Miao   [ARM] pxa: simpli...
199
  	}
75540c1ac   eric miao   [ARM] pxa: Add PX...
200
  	platform_set_drvdata(pdev, pwm);
3d2a98cd5   Eric Miao   [ARM] pxa: simpli...
201
  	return 0;
75540c1ac   eric miao   [ARM] pxa: Add PX...
202
  }
77f37917a   Bill Pemberton   pwm: remove use o...
203
  static int pwm_remove(struct platform_device *pdev)
75540c1ac   eric miao   [ARM] pxa: Add PX...
204
  {
17b2b4780   Thierry Reding   pwm: Move PXA PWM...
205
  	struct pxa_pwm_chip *chip;
75540c1ac   eric miao   [ARM] pxa: Add PX...
206

17b2b4780   Thierry Reding   pwm: Move PXA PWM...
207
208
  	chip = platform_get_drvdata(pdev);
  	if (chip == NULL)
75540c1ac   eric miao   [ARM] pxa: Add PX...
209
  		return -ENODEV;
abeaf7552   Thierry Reding   pwm: pxa: Propaga...
210
  	return pwmchip_remove(&chip->chip);
75540c1ac   eric miao   [ARM] pxa: Add PX...
211
  }
3d2a98cd5   Eric Miao   [ARM] pxa: simpli...
212
  static struct platform_driver pwm_driver = {
75540c1ac   eric miao   [ARM] pxa: Add PX...
213
214
  	.driver		= {
  		.name	= "pxa25x-pwm",
f409cd383   Thierry Reding   Revert "pwm: pxa:...
215
  		.of_match_table = pwm_of_match,
75540c1ac   eric miao   [ARM] pxa: Add PX...
216
  	},
3d2a98cd5   Eric Miao   [ARM] pxa: simpli...
217
  	.probe		= pwm_probe,
fd1091125   Bill Pemberton   pwm: remove use o...
218
  	.remove		= pwm_remove,
3d2a98cd5   Eric Miao   [ARM] pxa: simpli...
219
  	.id_table	= pwm_id_table,
75540c1ac   eric miao   [ARM] pxa: Add PX...
220
  };
1e185c7aa   Mike Dunn   pwm: pxa: Use mod...
221
  module_platform_driver(pwm_driver);
b5f0228af   Guennadi Liakhovetski   [ARM] 5078/1: pxa...
222
223
  
  MODULE_LICENSE("GPL v2");