Linux Audio

Check our new training course

Open-source upstreaming

Need help get the support for your hardware in upstream Linux?
Loading...
v6.2
  1// SPDX-License-Identifier: GPL-2.0+
  2/*
  3 * Copyright 2012 Freescale Semiconductor, Inc.
 
 
 
 
 
 
 
  4 */
  5
  6#include <linux/clk.h>
  7#include <linux/err.h>
  8#include <linux/io.h>
  9#include <linux/kernel.h>
 10#include <linux/module.h>
 11#include <linux/of.h>
 
 12#include <linux/platform_device.h>
 13#include <linux/pwm.h>
 14#include <linux/slab.h>
 15#include <linux/stmp_device.h>
 16
 17#define SET	0x4
 18#define CLR	0x8
 19#define TOG	0xc
 20
 21#define PWM_CTRL		0x0
 22#define PWM_ACTIVE0		0x10
 23#define PWM_PERIOD0		0x20
 24#define  PERIOD_PERIOD(p)	((p) & 0xffff)
 25#define  PERIOD_PERIOD_MAX	0x10000
 26#define  PERIOD_ACTIVE_HIGH	(3 << 16)
 27#define  PERIOD_ACTIVE_LOW	(2 << 16)
 28#define  PERIOD_INACTIVE_HIGH	(3 << 18)
 29#define  PERIOD_INACTIVE_LOW	(2 << 18)
 30#define  PERIOD_POLARITY_NORMAL	(PERIOD_ACTIVE_HIGH | PERIOD_INACTIVE_LOW)
 31#define  PERIOD_POLARITY_INVERSE	(PERIOD_ACTIVE_LOW | PERIOD_INACTIVE_HIGH)
 32#define  PERIOD_CDIV(div)	(((div) & 0x7) << 20)
 33#define  PERIOD_CDIV_MAX	8
 34
 35static const u8 cdiv_shift[PERIOD_CDIV_MAX] = {
 36	0, 1, 2, 3, 4, 6, 8, 10
 37};
 38
 39struct mxs_pwm_chip {
 40	struct pwm_chip chip;
 41	struct clk *clk;
 42	void __iomem *base;
 43};
 44
 45#define to_mxs_pwm_chip(_chip) container_of(_chip, struct mxs_pwm_chip, chip)
 46
 47static int mxs_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
 48			 const struct pwm_state *state)
 49{
 50	struct mxs_pwm_chip *mxs = to_mxs_pwm_chip(chip);
 51	int ret, div = 0;
 52	unsigned int period_cycles, duty_cycles;
 53	unsigned long rate;
 54	unsigned long long c;
 55	unsigned int pol_bits;
 56
 57	/*
 58	 * If the PWM channel is disabled, make sure to turn on the
 59	 * clock before calling clk_get_rate() and writing to the
 60	 * registers. Otherwise, just keep it enabled.
 61	 */
 62	if (!pwm_is_enabled(pwm)) {
 63		ret = clk_prepare_enable(mxs->clk);
 64		if (ret)
 65			return ret;
 66	}
 67
 68	if (!state->enabled && pwm_is_enabled(pwm))
 69		writel(1 << pwm->hwpwm, mxs->base + PWM_CTRL + CLR);
 70
 71	rate = clk_get_rate(mxs->clk);
 72	while (1) {
 73		c = rate >> cdiv_shift[div];
 74		c = c * state->period;
 75		do_div(c, 1000000000);
 76		if (c < PERIOD_PERIOD_MAX)
 77			break;
 78		div++;
 79		if (div >= PERIOD_CDIV_MAX)
 80			return -EINVAL;
 81	}
 82
 83	period_cycles = c;
 84	c *= state->duty_cycle;
 85	do_div(c, state->period);
 86	duty_cycles = c;
 87
 88	/*
 89	 * The data sheet the says registers must be written to in
 90	 * this order (ACTIVEn, then PERIODn). Also, the new settings
 91	 * only take effect at the beginning of a new period, avoiding
 92	 * glitches.
 93	 */
 
 
 
 
 
 94
 95	pol_bits = state->polarity == PWM_POLARITY_NORMAL ?
 96		PERIOD_POLARITY_NORMAL : PERIOD_POLARITY_INVERSE;
 97	writel(duty_cycles << 16,
 98	       mxs->base + PWM_ACTIVE0 + pwm->hwpwm * 0x20);
 99	writel(PERIOD_PERIOD(period_cycles) | pol_bits | PERIOD_CDIV(div),
100	       mxs->base + PWM_PERIOD0 + pwm->hwpwm * 0x20);
101
102	if (state->enabled) {
103		if (!pwm_is_enabled(pwm)) {
104			/*
105			 * The clock was enabled above. Just enable
106			 * the channel in the control register.
107			 */
108			writel(1 << pwm->hwpwm, mxs->base + PWM_CTRL + SET);
109		}
110	} else {
111		clk_disable_unprepare(mxs->clk);
112	}
113	return 0;
114}
115
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
116static const struct pwm_ops mxs_pwm_ops = {
117	.apply = mxs_pwm_apply,
 
 
118	.owner = THIS_MODULE,
119};
120
121static int mxs_pwm_probe(struct platform_device *pdev)
122{
123	struct device_node *np = pdev->dev.of_node;
124	struct mxs_pwm_chip *mxs;
 
125	int ret;
126
127	mxs = devm_kzalloc(&pdev->dev, sizeof(*mxs), GFP_KERNEL);
128	if (!mxs)
129		return -ENOMEM;
130
131	mxs->base = devm_platform_ioremap_resource(pdev, 0);
 
132	if (IS_ERR(mxs->base))
133		return PTR_ERR(mxs->base);
134
135	mxs->clk = devm_clk_get(&pdev->dev, NULL);
136	if (IS_ERR(mxs->clk))
137		return PTR_ERR(mxs->clk);
138
139	mxs->chip.dev = &pdev->dev;
140	mxs->chip.ops = &mxs_pwm_ops;
141
142	ret = of_property_read_u32(np, "fsl,pwm-number", &mxs->chip.npwm);
143	if (ret < 0) {
144		dev_err(&pdev->dev, "failed to get pwm number: %d\n", ret);
145		return ret;
146	}
147
148	/* FIXME: Only do this if the PWM isn't already running */
149	ret = stmp_reset_block(mxs->base);
150	if (ret)
151		return dev_err_probe(&pdev->dev, ret, "failed to reset PWM\n");
152
153	ret = devm_pwmchip_add(&pdev->dev, &mxs->chip);
154	if (ret < 0) {
155		dev_err(&pdev->dev, "failed to add pwm chip %d\n", ret);
156		return ret;
157	}
158
 
 
 
 
 
 
159	return 0;
 
 
 
 
 
 
 
 
 
 
 
160}
161
162static const struct of_device_id mxs_pwm_dt_ids[] = {
163	{ .compatible = "fsl,imx23-pwm", },
164	{ /* sentinel */ }
165};
166MODULE_DEVICE_TABLE(of, mxs_pwm_dt_ids);
167
168static struct platform_driver mxs_pwm_driver = {
169	.driver = {
170		.name = "mxs-pwm",
 
171		.of_match_table = mxs_pwm_dt_ids,
172	},
173	.probe = mxs_pwm_probe,
 
174};
175module_platform_driver(mxs_pwm_driver);
176
177MODULE_ALIAS("platform:mxs-pwm");
178MODULE_AUTHOR("Shawn Guo <shawn.guo@linaro.org>");
179MODULE_DESCRIPTION("Freescale MXS PWM Driver");
180MODULE_LICENSE("GPL v2");
v3.15
 
  1/*
  2 * Copyright 2012 Freescale Semiconductor, Inc.
  3 *
  4 * The code contained herein is licensed under the GNU General Public
  5 * License. You may obtain a copy of the GNU General Public License
  6 * Version 2 or later at the following locations:
  7 *
  8 * http://www.opensource.org/licenses/gpl-license.html
  9 * http://www.gnu.org/copyleft/gpl.html
 10 */
 11
 12#include <linux/clk.h>
 13#include <linux/err.h>
 14#include <linux/io.h>
 15#include <linux/kernel.h>
 16#include <linux/module.h>
 17#include <linux/of.h>
 18#include <linux/of_address.h>
 19#include <linux/platform_device.h>
 20#include <linux/pwm.h>
 21#include <linux/slab.h>
 22#include <linux/stmp_device.h>
 23
 24#define SET	0x4
 25#define CLR	0x8
 26#define TOG	0xc
 27
 28#define PWM_CTRL		0x0
 29#define PWM_ACTIVE0		0x10
 30#define PWM_PERIOD0		0x20
 31#define  PERIOD_PERIOD(p)	((p) & 0xffff)
 32#define  PERIOD_PERIOD_MAX	0x10000
 33#define  PERIOD_ACTIVE_HIGH	(3 << 16)
 
 
 34#define  PERIOD_INACTIVE_LOW	(2 << 18)
 
 
 35#define  PERIOD_CDIV(div)	(((div) & 0x7) << 20)
 36#define  PERIOD_CDIV_MAX	8
 37
 
 
 
 
 38struct mxs_pwm_chip {
 39	struct pwm_chip chip;
 40	struct clk *clk;
 41	void __iomem *base;
 42};
 43
 44#define to_mxs_pwm_chip(_chip) container_of(_chip, struct mxs_pwm_chip, chip)
 45
 46static int mxs_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
 47			  int duty_ns, int period_ns)
 48{
 49	struct mxs_pwm_chip *mxs = to_mxs_pwm_chip(chip);
 50	int ret, div = 0;
 51	unsigned int period_cycles, duty_cycles;
 52	unsigned long rate;
 53	unsigned long long c;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 54
 55	rate = clk_get_rate(mxs->clk);
 56	while (1) {
 57		c = rate / (1 << div);
 58		c = c * period_ns;
 59		do_div(c, 1000000000);
 60		if (c < PERIOD_PERIOD_MAX)
 61			break;
 62		div++;
 63		if (div > PERIOD_CDIV_MAX)
 64			return -EINVAL;
 65	}
 66
 67	period_cycles = c;
 68	c *= duty_ns;
 69	do_div(c, period_ns);
 70	duty_cycles = c;
 71
 72	/*
 73	 * If the PWM channel is disabled, make sure to turn on the clock
 74	 * before writing the register. Otherwise, keep it enabled.
 
 
 75	 */
 76	if (!test_bit(PWMF_ENABLED, &pwm->flags)) {
 77		ret = clk_prepare_enable(mxs->clk);
 78		if (ret)
 79			return ret;
 80	}
 81
 
 
 82	writel(duty_cycles << 16,
 83			mxs->base + PWM_ACTIVE0 + pwm->hwpwm * 0x20);
 84	writel(PERIOD_PERIOD(period_cycles) | PERIOD_ACTIVE_HIGH |
 85	       PERIOD_INACTIVE_LOW | PERIOD_CDIV(div),
 86			mxs->base + PWM_PERIOD0 + pwm->hwpwm * 0x20);
 87
 88	/*
 89	 * If the PWM is not enabled, turn the clock off again to save power.
 90	 */
 91	if (!test_bit(PWMF_ENABLED, &pwm->flags))
 
 
 
 
 92		clk_disable_unprepare(mxs->clk);
 93
 94	return 0;
 95}
 96
 97static int mxs_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
 98{
 99	struct mxs_pwm_chip *mxs = to_mxs_pwm_chip(chip);
100	int ret;
101
102	ret = clk_prepare_enable(mxs->clk);
103	if (ret)
104		return ret;
105
106	writel(1 << pwm->hwpwm, mxs->base + PWM_CTRL + SET);
107
108	return 0;
109}
110
111static void mxs_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
112{
113	struct mxs_pwm_chip *mxs = to_mxs_pwm_chip(chip);
114
115	writel(1 << pwm->hwpwm, mxs->base + PWM_CTRL + CLR);
116
117	clk_disable_unprepare(mxs->clk);
118}
119
120static const struct pwm_ops mxs_pwm_ops = {
121	.config = mxs_pwm_config,
122	.enable = mxs_pwm_enable,
123	.disable = mxs_pwm_disable,
124	.owner = THIS_MODULE,
125};
126
127static int mxs_pwm_probe(struct platform_device *pdev)
128{
129	struct device_node *np = pdev->dev.of_node;
130	struct mxs_pwm_chip *mxs;
131	struct resource *res;
132	int ret;
133
134	mxs = devm_kzalloc(&pdev->dev, sizeof(*mxs), GFP_KERNEL);
135	if (!mxs)
136		return -ENOMEM;
137
138	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
139	mxs->base = devm_ioremap_resource(&pdev->dev, res);
140	if (IS_ERR(mxs->base))
141		return PTR_ERR(mxs->base);
142
143	mxs->clk = devm_clk_get(&pdev->dev, NULL);
144	if (IS_ERR(mxs->clk))
145		return PTR_ERR(mxs->clk);
146
147	mxs->chip.dev = &pdev->dev;
148	mxs->chip.ops = &mxs_pwm_ops;
149	mxs->chip.base = -1;
150	ret = of_property_read_u32(np, "fsl,pwm-number", &mxs->chip.npwm);
151	if (ret < 0) {
152		dev_err(&pdev->dev, "failed to get pwm number: %d\n", ret);
153		return ret;
154	}
155
156	ret = pwmchip_add(&mxs->chip);
 
 
 
 
 
157	if (ret < 0) {
158		dev_err(&pdev->dev, "failed to add pwm chip %d\n", ret);
159		return ret;
160	}
161
162	platform_set_drvdata(pdev, mxs);
163
164	ret = stmp_reset_block(mxs->base);
165	if (ret)
166		goto pwm_remove;
167
168	return 0;
169
170pwm_remove:
171	pwmchip_remove(&mxs->chip);
172	return ret;
173}
174
175static int mxs_pwm_remove(struct platform_device *pdev)
176{
177	struct mxs_pwm_chip *mxs = platform_get_drvdata(pdev);
178
179	return pwmchip_remove(&mxs->chip);
180}
181
182static const struct of_device_id mxs_pwm_dt_ids[] = {
183	{ .compatible = "fsl,imx23-pwm", },
184	{ /* sentinel */ }
185};
186MODULE_DEVICE_TABLE(of, mxs_pwm_dt_ids);
187
188static struct platform_driver mxs_pwm_driver = {
189	.driver = {
190		.name = "mxs-pwm",
191		.owner = THIS_MODULE,
192		.of_match_table = mxs_pwm_dt_ids,
193	},
194	.probe = mxs_pwm_probe,
195	.remove = mxs_pwm_remove,
196};
197module_platform_driver(mxs_pwm_driver);
198
199MODULE_ALIAS("platform:mxs-pwm");
200MODULE_AUTHOR("Shawn Guo <shawn.guo@linaro.org>");
201MODULE_DESCRIPTION("Freescale MXS PWM Driver");
202MODULE_LICENSE("GPL v2");