1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2015 Neil Armstrong <narmstrong@baylibre.com>
4 * Copyright (c) 2014 Joachim Eastwood <manabian@gmail.com>
5 * Copyright (c) 2012 NeilBrown <neilb@suse.de>
6 * Heavily based on earlier code which is:
7 * Copyright (c) 2010 Grant Erickson <marathon96@gmail.com>
9 * Also based on pwm-samsung.c
12 * This file is the core OMAP support for the generic, Linux
13 * PWM driver / controller, using the OMAP's dual-mode timers.
16 #include <linux/clk.h>
17 #include <linux/err.h>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/mutex.h>
22 #include <linux/of_platform.h>
23 #include <linux/platform_data/dmtimer-omap.h>
24 #include <linux/platform_data/pwm_omap_dmtimer.h>
25 #include <linux/platform_device.h>
26 #include <linux/pm_runtime.h>
27 #include <linux/pwm.h>
28 #include <linux/slab.h>
29 #include <linux/time.h>
31 #define DM_TIMER_LOAD_MIN 0xfffffffe
32 #define DM_TIMER_MAX 0xffffffff
34 struct pwm_omap_dmtimer_chip
{
37 pwm_omap_dmtimer
*dm_timer
;
38 const struct omap_dm_timer_ops
*pdata
;
39 struct platform_device
*dm_timer_pdev
;
42 static inline struct pwm_omap_dmtimer_chip
*
43 to_pwm_omap_dmtimer_chip(struct pwm_chip
*chip
)
45 return container_of(chip
, struct pwm_omap_dmtimer_chip
, chip
);
48 static u32
pwm_omap_dmtimer_get_clock_cycles(unsigned long clk_rate
, int ns
)
50 return DIV_ROUND_CLOSEST_ULL((u64
)clk_rate
* ns
, NSEC_PER_SEC
);
53 static void pwm_omap_dmtimer_start(struct pwm_omap_dmtimer_chip
*omap
)
56 * According to OMAP 4 TRM section 22.2.4.10 the counter should be
57 * started at 0xFFFFFFFE when overflow and match is used to ensure
58 * that the PWM line is toggled on the first event.
60 * Note that omap_dm_timer_enable/disable is for register access and
61 * not the timer counter itself.
63 omap
->pdata
->enable(omap
->dm_timer
);
64 omap
->pdata
->write_counter(omap
->dm_timer
, DM_TIMER_LOAD_MIN
);
65 omap
->pdata
->disable(omap
->dm_timer
);
67 omap
->pdata
->start(omap
->dm_timer
);
70 static int pwm_omap_dmtimer_enable(struct pwm_chip
*chip
,
71 struct pwm_device
*pwm
)
73 struct pwm_omap_dmtimer_chip
*omap
= to_pwm_omap_dmtimer_chip(chip
);
75 mutex_lock(&omap
->mutex
);
76 pwm_omap_dmtimer_start(omap
);
77 mutex_unlock(&omap
->mutex
);
82 static void pwm_omap_dmtimer_disable(struct pwm_chip
*chip
,
83 struct pwm_device
*pwm
)
85 struct pwm_omap_dmtimer_chip
*omap
= to_pwm_omap_dmtimer_chip(chip
);
87 mutex_lock(&omap
->mutex
);
88 omap
->pdata
->stop(omap
->dm_timer
);
89 mutex_unlock(&omap
->mutex
);
92 static int pwm_omap_dmtimer_config(struct pwm_chip
*chip
,
93 struct pwm_device
*pwm
,
94 int duty_ns
, int period_ns
)
96 struct pwm_omap_dmtimer_chip
*omap
= to_pwm_omap_dmtimer_chip(chip
);
97 u32 period_cycles
, duty_cycles
;
98 u32 load_value
, match_value
;
100 unsigned long clk_rate
;
103 dev_dbg(chip
->dev
, "requested duty cycle: %d ns, period: %d ns\n",
106 mutex_lock(&omap
->mutex
);
107 if (duty_ns
== pwm_get_duty_cycle(pwm
) &&
108 period_ns
== pwm_get_period(pwm
)) {
109 /* No change - don't cause any transients. */
110 mutex_unlock(&omap
->mutex
);
114 fclk
= omap
->pdata
->get_fclk(omap
->dm_timer
);
116 dev_err(chip
->dev
, "invalid pmtimer fclk\n");
120 clk_rate
= clk_get_rate(fclk
);
122 dev_err(chip
->dev
, "invalid pmtimer fclk rate\n");
126 dev_dbg(chip
->dev
, "clk rate: %luHz\n", clk_rate
);
129 * Calculate the appropriate load and match values based on the
130 * specified period and duty cycle. The load value determines the
131 * period time and the match value determines the duty time.
133 * The period lasts for (DM_TIMER_MAX-load_value+1) clock cycles.
134 * Similarly, the active time lasts (match_value-load_value+1) cycles.
135 * The non-active time is the remainder: (DM_TIMER_MAX-match_value)
138 * NOTE: It is required that: load_value <= match_value < DM_TIMER_MAX
141 * OMAP4430/60/70 TRM sections 22.2.4.10 and 22.2.4.11
142 * AM335x Sitara TRM sections 20.1.3.5 and 20.1.3.6
144 period_cycles
= pwm_omap_dmtimer_get_clock_cycles(clk_rate
, period_ns
);
145 duty_cycles
= pwm_omap_dmtimer_get_clock_cycles(clk_rate
, duty_ns
);
147 if (period_cycles
< 2) {
149 "period %d ns too short for clock rate %lu Hz\n",
150 period_ns
, clk_rate
);
154 if (duty_cycles
< 1) {
156 "duty cycle %d ns is too short for clock rate %lu Hz\n",
158 dev_dbg(chip
->dev
, "using minimum of 1 clock cycle\n");
160 } else if (duty_cycles
>= period_cycles
) {
162 "duty cycle %d ns is too long for period %d ns at clock rate %lu Hz\n",
163 duty_ns
, period_ns
, clk_rate
);
164 dev_dbg(chip
->dev
, "using maximum of 1 clock cycle less than period\n");
165 duty_cycles
= period_cycles
- 1;
168 dev_dbg(chip
->dev
, "effective duty cycle: %lld ns, period: %lld ns\n",
169 DIV_ROUND_CLOSEST_ULL((u64
)NSEC_PER_SEC
* duty_cycles
,
171 DIV_ROUND_CLOSEST_ULL((u64
)NSEC_PER_SEC
* period_cycles
,
174 load_value
= (DM_TIMER_MAX
- period_cycles
) + 1;
175 match_value
= load_value
+ duty_cycles
- 1;
178 * We MUST stop the associated dual-mode timer before attempting to
179 * write its registers, but calls to omap_dm_timer_start/stop must
180 * be balanced so check if timer is active before calling timer_stop.
182 timer_active
= pm_runtime_active(&omap
->dm_timer_pdev
->dev
);
184 omap
->pdata
->stop(omap
->dm_timer
);
186 omap
->pdata
->set_load(omap
->dm_timer
, true, load_value
);
187 omap
->pdata
->set_match(omap
->dm_timer
, true, match_value
);
189 dev_dbg(chip
->dev
, "load value: %#08x (%d), match value: %#08x (%d)\n",
190 load_value
, load_value
, match_value
, match_value
);
192 omap
->pdata
->set_pwm(omap
->dm_timer
,
193 pwm_get_polarity(pwm
) == PWM_POLARITY_INVERSED
,
195 PWM_OMAP_DMTIMER_TRIGGER_OVERFLOW_AND_COMPARE
);
197 /* If config was called while timer was running it must be reenabled. */
199 pwm_omap_dmtimer_start(omap
);
201 mutex_unlock(&omap
->mutex
);
206 mutex_unlock(&omap
->mutex
);
211 static int pwm_omap_dmtimer_set_polarity(struct pwm_chip
*chip
,
212 struct pwm_device
*pwm
,
213 enum pwm_polarity polarity
)
215 struct pwm_omap_dmtimer_chip
*omap
= to_pwm_omap_dmtimer_chip(chip
);
218 * PWM core will not call set_polarity while PWM is enabled so it's
219 * safe to reconfigure the timer here without stopping it first.
221 mutex_lock(&omap
->mutex
);
222 omap
->pdata
->set_pwm(omap
->dm_timer
,
223 polarity
== PWM_POLARITY_INVERSED
,
225 PWM_OMAP_DMTIMER_TRIGGER_OVERFLOW_AND_COMPARE
);
226 mutex_unlock(&omap
->mutex
);
231 static const struct pwm_ops pwm_omap_dmtimer_ops
= {
232 .enable
= pwm_omap_dmtimer_enable
,
233 .disable
= pwm_omap_dmtimer_disable
,
234 .config
= pwm_omap_dmtimer_config
,
235 .set_polarity
= pwm_omap_dmtimer_set_polarity
,
236 .owner
= THIS_MODULE
,
239 static int pwm_omap_dmtimer_probe(struct platform_device
*pdev
)
241 struct device_node
*np
= pdev
->dev
.of_node
;
242 struct device_node
*timer
;
243 struct platform_device
*timer_pdev
;
244 struct pwm_omap_dmtimer_chip
*omap
;
245 struct dmtimer_platform_data
*timer_pdata
;
246 const struct omap_dm_timer_ops
*pdata
;
247 pwm_omap_dmtimer
*dm_timer
;
251 timer
= of_parse_phandle(np
, "ti,timers", 0);
255 timer_pdev
= of_find_device_by_node(timer
);
257 dev_err(&pdev
->dev
, "Unable to find Timer pdev\n");
262 timer_pdata
= dev_get_platdata(&timer_pdev
->dev
);
265 "dmtimer pdata structure NULL, deferring probe\n");
270 pdata
= timer_pdata
->timer_ops
;
272 if (!pdata
|| !pdata
->request_by_node
||
282 !pdata
->set_prescaler
||
283 !pdata
->write_counter
) {
284 dev_err(&pdev
->dev
, "Incomplete dmtimer pdata structure\n");
289 if (!of_get_property(timer
, "ti,timer-pwm", NULL
)) {
290 dev_err(&pdev
->dev
, "Missing ti,timer-pwm capability\n");
295 dm_timer
= pdata
->request_by_node(timer
);
306 omap
= devm_kzalloc(&pdev
->dev
, sizeof(*omap
), GFP_KERNEL
);
308 pdata
->free(dm_timer
);
313 omap
->dm_timer
= dm_timer
;
314 omap
->dm_timer_pdev
= timer_pdev
;
317 * Ensure that the timer is stopped before we allow PWM core to call
320 if (pm_runtime_active(&omap
->dm_timer_pdev
->dev
))
321 omap
->pdata
->stop(omap
->dm_timer
);
323 if (!of_property_read_u32(pdev
->dev
.of_node
, "ti,prescaler", &v
))
324 omap
->pdata
->set_prescaler(omap
->dm_timer
, v
);
326 /* setup dmtimer clock source */
327 if (!of_property_read_u32(pdev
->dev
.of_node
, "ti,clock-source", &v
))
328 omap
->pdata
->set_source(omap
->dm_timer
, v
);
330 omap
->chip
.dev
= &pdev
->dev
;
331 omap
->chip
.ops
= &pwm_omap_dmtimer_ops
;
332 omap
->chip
.base
= -1;
334 omap
->chip
.of_xlate
= of_pwm_xlate_with_flags
;
335 omap
->chip
.of_pwm_n_cells
= 3;
337 mutex_init(&omap
->mutex
);
339 ret
= pwmchip_add(&omap
->chip
);
341 dev_err(&pdev
->dev
, "failed to register PWM\n");
342 omap
->pdata
->free(omap
->dm_timer
);
346 platform_set_drvdata(pdev
, omap
);
351 static int pwm_omap_dmtimer_remove(struct platform_device
*pdev
)
353 struct pwm_omap_dmtimer_chip
*omap
= platform_get_drvdata(pdev
);
355 if (pm_runtime_active(&omap
->dm_timer_pdev
->dev
))
356 omap
->pdata
->stop(omap
->dm_timer
);
358 omap
->pdata
->free(omap
->dm_timer
);
360 mutex_destroy(&omap
->mutex
);
362 return pwmchip_remove(&omap
->chip
);
365 static const struct of_device_id pwm_omap_dmtimer_of_match
[] = {
366 {.compatible
= "ti,omap-dmtimer-pwm"},
369 MODULE_DEVICE_TABLE(of
, pwm_omap_dmtimer_of_match
);
371 static struct platform_driver pwm_omap_dmtimer_driver
= {
373 .name
= "omap-dmtimer-pwm",
374 .of_match_table
= of_match_ptr(pwm_omap_dmtimer_of_match
),
376 .probe
= pwm_omap_dmtimer_probe
,
377 .remove
= pwm_omap_dmtimer_remove
,
379 module_platform_driver(pwm_omap_dmtimer_driver
);
381 MODULE_AUTHOR("Grant Erickson <marathon96@gmail.com>");
382 MODULE_AUTHOR("NeilBrown <neilb@suse.de>");
383 MODULE_AUTHOR("Neil Armstrong <narmstrong@baylibre.com>");
384 MODULE_LICENSE("GPL v2");
385 MODULE_DESCRIPTION("OMAP PWM Driver using Dual-mode Timers");