119ad2b75SFabio Estevam // SPDX-License-Identifier: GPL-2.0+
24dce82c1SShawn Guo /*
34dce82c1SShawn Guo * Copyright 2012 Freescale Semiconductor, Inc.
44dce82c1SShawn Guo */
54dce82c1SShawn Guo
64dce82c1SShawn Guo #include <linux/clk.h>
74dce82c1SShawn Guo #include <linux/err.h>
84dce82c1SShawn Guo #include <linux/io.h>
94dce82c1SShawn Guo #include <linux/kernel.h>
104dce82c1SShawn Guo #include <linux/module.h>
114dce82c1SShawn Guo #include <linux/of.h>
124dce82c1SShawn Guo #include <linux/platform_device.h>
134dce82c1SShawn Guo #include <linux/pwm.h>
144dce82c1SShawn Guo #include <linux/slab.h>
1501bf32e9SShawn Guo #include <linux/stmp_device.h>
164dce82c1SShawn Guo
174dce82c1SShawn Guo #define SET 0x4
184dce82c1SShawn Guo #define CLR 0x8
194dce82c1SShawn Guo #define TOG 0xc
204dce82c1SShawn Guo
214dce82c1SShawn Guo #define PWM_CTRL 0x0
224dce82c1SShawn Guo #define PWM_ACTIVE0 0x10
234dce82c1SShawn Guo #define PWM_PERIOD0 0x20
244dce82c1SShawn Guo #define PERIOD_PERIOD(p) ((p) & 0xffff)
254dce82c1SShawn Guo #define PERIOD_PERIOD_MAX 0x10000
264dce82c1SShawn Guo #define PERIOD_ACTIVE_HIGH (3 << 16)
272cf0f6feSRasmus Villemoes #define PERIOD_ACTIVE_LOW (2 << 16)
282cf0f6feSRasmus Villemoes #define PERIOD_INACTIVE_HIGH (3 << 18)
294dce82c1SShawn Guo #define PERIOD_INACTIVE_LOW (2 << 18)
30bf29c2ffSRasmus Villemoes #define PERIOD_POLARITY_NORMAL (PERIOD_ACTIVE_HIGH | PERIOD_INACTIVE_LOW)
312cf0f6feSRasmus Villemoes #define PERIOD_POLARITY_INVERSE (PERIOD_ACTIVE_LOW | PERIOD_INACTIVE_HIGH)
324dce82c1SShawn Guo #define PERIOD_CDIV(div) (((div) & 0x7) << 20)
334dce82c1SShawn Guo #define PERIOD_CDIV_MAX 8
344dce82c1SShawn Guo
353c64ed74SRasmus Villemoes static const u8 cdiv_shift[PERIOD_CDIV_MAX] = {
363c64ed74SRasmus Villemoes 0, 1, 2, 3, 4, 6, 8, 10
3724ccea1cSGaetan Hug };
3824ccea1cSGaetan Hug
394dce82c1SShawn Guo struct mxs_pwm_chip {
404dce82c1SShawn Guo struct pwm_chip chip;
414dce82c1SShawn Guo struct clk *clk;
424dce82c1SShawn Guo void __iomem *base;
434dce82c1SShawn Guo };
444dce82c1SShawn Guo
454dce82c1SShawn Guo #define to_mxs_pwm_chip(_chip) container_of(_chip, struct mxs_pwm_chip, chip)
464dce82c1SShawn Guo
mxs_pwm_apply(struct pwm_chip * chip,struct pwm_device * pwm,const struct pwm_state * state)47bf29c2ffSRasmus Villemoes static int mxs_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
48bf29c2ffSRasmus Villemoes const struct pwm_state *state)
49bf29c2ffSRasmus Villemoes {
50bf29c2ffSRasmus Villemoes struct mxs_pwm_chip *mxs = to_mxs_pwm_chip(chip);
51bf29c2ffSRasmus Villemoes int ret, div = 0;
52bf29c2ffSRasmus Villemoes unsigned int period_cycles, duty_cycles;
53bf29c2ffSRasmus Villemoes unsigned long rate;
54bf29c2ffSRasmus Villemoes unsigned long long c;
552cf0f6feSRasmus Villemoes unsigned int pol_bits;
56bf29c2ffSRasmus Villemoes
57bf29c2ffSRasmus Villemoes /*
58bf29c2ffSRasmus Villemoes * If the PWM channel is disabled, make sure to turn on the
59bf29c2ffSRasmus Villemoes * clock before calling clk_get_rate() and writing to the
60bf29c2ffSRasmus Villemoes * registers. Otherwise, just keep it enabled.
61bf29c2ffSRasmus Villemoes */
62bf29c2ffSRasmus Villemoes if (!pwm_is_enabled(pwm)) {
63bf29c2ffSRasmus Villemoes ret = clk_prepare_enable(mxs->clk);
64bf29c2ffSRasmus Villemoes if (ret)
65bf29c2ffSRasmus Villemoes return ret;
66bf29c2ffSRasmus Villemoes }
67bf29c2ffSRasmus Villemoes
68bf29c2ffSRasmus Villemoes if (!state->enabled && pwm_is_enabled(pwm))
69bf29c2ffSRasmus Villemoes writel(1 << pwm->hwpwm, mxs->base + PWM_CTRL + CLR);
70bf29c2ffSRasmus Villemoes
71bf29c2ffSRasmus Villemoes rate = clk_get_rate(mxs->clk);
72bf29c2ffSRasmus Villemoes while (1) {
733c64ed74SRasmus Villemoes c = rate >> cdiv_shift[div];
74bf29c2ffSRasmus Villemoes c = c * state->period;
75bf29c2ffSRasmus Villemoes do_div(c, 1000000000);
76bf29c2ffSRasmus Villemoes if (c < PERIOD_PERIOD_MAX)
77bf29c2ffSRasmus Villemoes break;
78bf29c2ffSRasmus Villemoes div++;
79bf29c2ffSRasmus Villemoes if (div >= PERIOD_CDIV_MAX)
80bf29c2ffSRasmus Villemoes return -EINVAL;
81bf29c2ffSRasmus Villemoes }
82bf29c2ffSRasmus Villemoes
83bf29c2ffSRasmus Villemoes period_cycles = c;
84bf29c2ffSRasmus Villemoes c *= state->duty_cycle;
85bf29c2ffSRasmus Villemoes do_div(c, state->period);
86bf29c2ffSRasmus Villemoes duty_cycles = c;
87bf29c2ffSRasmus Villemoes
88bf29c2ffSRasmus Villemoes /*
89bf29c2ffSRasmus Villemoes * The data sheet the says registers must be written to in
90bf29c2ffSRasmus Villemoes * this order (ACTIVEn, then PERIODn). Also, the new settings
91bf29c2ffSRasmus Villemoes * only take effect at the beginning of a new period, avoiding
92bf29c2ffSRasmus Villemoes * glitches.
93bf29c2ffSRasmus Villemoes */
942cf0f6feSRasmus Villemoes
952cf0f6feSRasmus Villemoes pol_bits = state->polarity == PWM_POLARITY_NORMAL ?
962cf0f6feSRasmus Villemoes PERIOD_POLARITY_NORMAL : PERIOD_POLARITY_INVERSE;
97bf29c2ffSRasmus Villemoes writel(duty_cycles << 16,
98bf29c2ffSRasmus Villemoes mxs->base + PWM_ACTIVE0 + pwm->hwpwm * 0x20);
992cf0f6feSRasmus Villemoes writel(PERIOD_PERIOD(period_cycles) | pol_bits | PERIOD_CDIV(div),
100bf29c2ffSRasmus Villemoes mxs->base + PWM_PERIOD0 + pwm->hwpwm * 0x20);
101bf29c2ffSRasmus Villemoes
102bf29c2ffSRasmus Villemoes if (state->enabled) {
103bf29c2ffSRasmus Villemoes if (!pwm_is_enabled(pwm)) {
104bf29c2ffSRasmus Villemoes /*
105bf29c2ffSRasmus Villemoes * The clock was enabled above. Just enable
106bf29c2ffSRasmus Villemoes * the channel in the control register.
107bf29c2ffSRasmus Villemoes */
108bf29c2ffSRasmus Villemoes writel(1 << pwm->hwpwm, mxs->base + PWM_CTRL + SET);
109bf29c2ffSRasmus Villemoes }
110bf29c2ffSRasmus Villemoes } else {
111bf29c2ffSRasmus Villemoes clk_disable_unprepare(mxs->clk);
112bf29c2ffSRasmus Villemoes }
113bf29c2ffSRasmus Villemoes return 0;
114bf29c2ffSRasmus Villemoes }
115bf29c2ffSRasmus Villemoes
1164dce82c1SShawn Guo static const struct pwm_ops mxs_pwm_ops = {
117bf29c2ffSRasmus Villemoes .apply = mxs_pwm_apply,
1184dce82c1SShawn Guo .owner = THIS_MODULE,
1194dce82c1SShawn Guo };
1204dce82c1SShawn Guo
mxs_pwm_probe(struct platform_device * pdev)1214dce82c1SShawn Guo static int mxs_pwm_probe(struct platform_device *pdev)
1224dce82c1SShawn Guo {
1234dce82c1SShawn Guo struct device_node *np = pdev->dev.of_node;
1244dce82c1SShawn Guo struct mxs_pwm_chip *mxs;
1254dce82c1SShawn Guo int ret;
1264dce82c1SShawn Guo
1274dce82c1SShawn Guo mxs = devm_kzalloc(&pdev->dev, sizeof(*mxs), GFP_KERNEL);
1284dce82c1SShawn Guo if (!mxs)
1294dce82c1SShawn Guo return -ENOMEM;
1304dce82c1SShawn Guo
131a315614bSAnson Huang mxs->base = devm_platform_ioremap_resource(pdev, 0);
1326d4294d1SThierry Reding if (IS_ERR(mxs->base))
1336d4294d1SThierry Reding return PTR_ERR(mxs->base);
1344dce82c1SShawn Guo
13522d260bdSShawn Guo mxs->clk = devm_clk_get(&pdev->dev, NULL);
13622d260bdSShawn Guo if (IS_ERR(mxs->clk))
13722d260bdSShawn Guo return PTR_ERR(mxs->clk);
1384dce82c1SShawn Guo
1394dce82c1SShawn Guo mxs->chip.dev = &pdev->dev;
1404dce82c1SShawn Guo mxs->chip.ops = &mxs_pwm_ops;
1418c0216f3SThierry Reding
1424dce82c1SShawn Guo ret = of_property_read_u32(np, "fsl,pwm-number", &mxs->chip.npwm);
1434dce82c1SShawn Guo if (ret < 0) {
1444dce82c1SShawn Guo dev_err(&pdev->dev, "failed to get pwm number: %d\n", ret);
14522d260bdSShawn Guo return ret;
1464dce82c1SShawn Guo }
1474dce82c1SShawn Guo
148020162d6SUwe Kleine-König /* FIXME: Only do this if the PWM isn't already running */
149020162d6SUwe Kleine-König ret = stmp_reset_block(mxs->base);
150020162d6SUwe Kleine-König if (ret)
151020162d6SUwe Kleine-König return dev_err_probe(&pdev->dev, ret, "failed to reset PWM\n");
152020162d6SUwe Kleine-König
153*43f5f48dSUwe Kleine-König ret = devm_pwmchip_add(&pdev->dev, &mxs->chip);
1544dce82c1SShawn Guo if (ret < 0) {
1554dce82c1SShawn Guo dev_err(&pdev->dev, "failed to add pwm chip %d\n", ret);
15622d260bdSShawn Guo return ret;
1574dce82c1SShawn Guo }
1584dce82c1SShawn Guo
1594dce82c1SShawn Guo return 0;
1604dce82c1SShawn Guo }
1614dce82c1SShawn Guo
162f1a8870aSThierry Reding static const struct of_device_id mxs_pwm_dt_ids[] = {
163071407eeSShawn Guo { .compatible = "fsl,imx23-pwm", },
1644dce82c1SShawn Guo { /* sentinel */ }
1654dce82c1SShawn Guo };
1664dce82c1SShawn Guo MODULE_DEVICE_TABLE(of, mxs_pwm_dt_ids);
1674dce82c1SShawn Guo
1684dce82c1SShawn Guo static struct platform_driver mxs_pwm_driver = {
1694dce82c1SShawn Guo .driver = {
1704dce82c1SShawn Guo .name = "mxs-pwm",
171de02cb88SSachin Kamat .of_match_table = mxs_pwm_dt_ids,
1724dce82c1SShawn Guo },
1734dce82c1SShawn Guo .probe = mxs_pwm_probe,
1744dce82c1SShawn Guo };
1754dce82c1SShawn Guo module_platform_driver(mxs_pwm_driver);
1764dce82c1SShawn Guo
1774dce82c1SShawn Guo MODULE_ALIAS("platform:mxs-pwm");
1784dce82c1SShawn Guo MODULE_AUTHOR("Shawn Guo <shawn.guo@linaro.org>");
1794dce82c1SShawn Guo MODULE_DESCRIPTION("Freescale MXS PWM Driver");
1804dce82c1SShawn Guo MODULE_LICENSE("GPL v2");
181