Linux Audio

Check our new training course

Loading...
v6.2
  1// SPDX-License-Identifier: GPL-2.0+
  2/*
  3 * Cirrus Logic CLPS711X PWM driver
  4 * Author: Alexander Shiyan <shc_work@mail.ru>
  5 */
  6
  7#include <linux/clk.h>
  8#include <linux/io.h>
  9#include <linux/module.h>
 10#include <linux/of.h>
 11#include <linux/platform_device.h>
 12#include <linux/pwm.h>
 13
 14struct clps711x_chip {
 15	struct pwm_chip chip;
 16	void __iomem *pmpcon;
 17	struct clk *clk;
 18	spinlock_t lock;
 19};
 20
 21static inline struct clps711x_chip *to_clps711x_chip(struct pwm_chip *chip)
 22{
 23	return container_of(chip, struct clps711x_chip, chip);
 24}
 25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 26static int clps711x_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
 27{
 28	struct clps711x_chip *priv = to_clps711x_chip(chip);
 29	unsigned int freq = clk_get_rate(priv->clk);
 30
 31	if (!freq)
 32		return -EINVAL;
 33
 34	/* Store constant period value */
 35	pwm->args.period = DIV_ROUND_CLOSEST(NSEC_PER_SEC, freq);
 36
 37	return 0;
 38}
 39
 40static int clps711x_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
 41			      const struct pwm_state *state)
 42{
 43	struct clps711x_chip *priv = to_clps711x_chip(chip);
 44	/* PWM0 - bits 4..7, PWM1 - bits 8..11 */
 45	u32 shift = (pwm->hwpwm + 1) * 4;
 46	unsigned long flags;
 47	u32 pmpcon, val;
 48
 49	if (state->polarity != PWM_POLARITY_NORMAL)
 50		return -EINVAL;
 51
 52	if (state->period != pwm->args.period)
 53		return -EINVAL;
 54
 55	if (state->enabled)
 56		val = mul_u64_u64_div_u64(state->duty_cycle, 0xf, state->period);
 57	else
 58		val = 0;
 59
 60	spin_lock_irqsave(&priv->lock, flags);
 
 61
 62	pmpcon = readl(priv->pmpcon);
 63	pmpcon &= ~(0xf << shift);
 64	pmpcon |= val << shift;
 65	writel(pmpcon, priv->pmpcon);
 66
 67	spin_unlock_irqrestore(&priv->lock, flags);
 
 68
 69	return 0;
 70}
 71
 
 
 
 
 
 
 
 72static const struct pwm_ops clps711x_pwm_ops = {
 73	.request = clps711x_pwm_request,
 74	.apply = clps711x_pwm_apply,
 
 
 75	.owner = THIS_MODULE,
 76};
 77
 78static struct pwm_device *clps711x_pwm_xlate(struct pwm_chip *chip,
 79					     const struct of_phandle_args *args)
 80{
 81	if (args->args[0] >= chip->npwm)
 82		return ERR_PTR(-EINVAL);
 83
 84	return pwm_request_from_chip(chip, args->args[0], NULL);
 85}
 86
 87static int clps711x_pwm_probe(struct platform_device *pdev)
 88{
 89	struct clps711x_chip *priv;
 90
 91	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
 92	if (!priv)
 93		return -ENOMEM;
 94
 95	priv->pmpcon = devm_platform_ioremap_resource(pdev, 0);
 96	if (IS_ERR(priv->pmpcon))
 97		return PTR_ERR(priv->pmpcon);
 98
 99	priv->clk = devm_clk_get(&pdev->dev, NULL);
100	if (IS_ERR(priv->clk))
101		return PTR_ERR(priv->clk);
102
103	priv->chip.ops = &clps711x_pwm_ops;
104	priv->chip.dev = &pdev->dev;
105	priv->chip.npwm = 2;
106	priv->chip.of_xlate = clps711x_pwm_xlate;
107	priv->chip.of_pwm_n_cells = 1;
108
109	spin_lock_init(&priv->lock);
110
111	return devm_pwmchip_add(&pdev->dev, &priv->chip);
112}
113
114static const struct of_device_id __maybe_unused clps711x_pwm_dt_ids[] = {
115	{ .compatible = "cirrus,ep7209-pwm", },
116	{ }
117};
118MODULE_DEVICE_TABLE(of, clps711x_pwm_dt_ids);
119
120static struct platform_driver clps711x_pwm_driver = {
121	.driver = {
122		.name = "clps711x-pwm",
123		.of_match_table = of_match_ptr(clps711x_pwm_dt_ids),
124	},
125	.probe = clps711x_pwm_probe,
126};
127module_platform_driver(clps711x_pwm_driver);
128
129MODULE_AUTHOR("Alexander Shiyan <shc_work@mail.ru>");
130MODULE_DESCRIPTION("Cirrus Logic CLPS711X PWM driver");
131MODULE_LICENSE("GPL");
v5.14.15
  1// SPDX-License-Identifier: GPL-2.0+
  2/*
  3 * Cirrus Logic CLPS711X PWM driver
  4 * Author: Alexander Shiyan <shc_work@mail.ru>
  5 */
  6
  7#include <linux/clk.h>
  8#include <linux/io.h>
  9#include <linux/module.h>
 10#include <linux/of.h>
 11#include <linux/platform_device.h>
 12#include <linux/pwm.h>
 13
 14struct clps711x_chip {
 15	struct pwm_chip chip;
 16	void __iomem *pmpcon;
 17	struct clk *clk;
 18	spinlock_t lock;
 19};
 20
 21static inline struct clps711x_chip *to_clps711x_chip(struct pwm_chip *chip)
 22{
 23	return container_of(chip, struct clps711x_chip, chip);
 24}
 25
 26static void clps711x_pwm_update_val(struct clps711x_chip *priv, u32 n, u32 v)
 27{
 28	/* PWM0 - bits 4..7, PWM1 - bits 8..11 */
 29	u32 shift = (n + 1) * 4;
 30	unsigned long flags;
 31	u32 tmp;
 32
 33	spin_lock_irqsave(&priv->lock, flags);
 34
 35	tmp = readl(priv->pmpcon);
 36	tmp &= ~(0xf << shift);
 37	tmp |= v << shift;
 38	writel(tmp, priv->pmpcon);
 39
 40	spin_unlock_irqrestore(&priv->lock, flags);
 41}
 42
 43static unsigned int clps711x_get_duty(struct pwm_device *pwm, unsigned int v)
 44{
 45	/* Duty cycle 0..15 max */
 46	return DIV64_U64_ROUND_CLOSEST(v * 0xf, pwm->args.period);
 47}
 48
 49static int clps711x_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
 50{
 51	struct clps711x_chip *priv = to_clps711x_chip(chip);
 52	unsigned int freq = clk_get_rate(priv->clk);
 53
 54	if (!freq)
 55		return -EINVAL;
 56
 57	/* Store constant period value */
 58	pwm->args.period = DIV_ROUND_CLOSEST(NSEC_PER_SEC, freq);
 59
 60	return 0;
 61}
 62
 63static int clps711x_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
 64			       int duty_ns, int period_ns)
 65{
 66	struct clps711x_chip *priv = to_clps711x_chip(chip);
 67	unsigned int duty;
 
 
 
 
 
 
 68
 69	if (period_ns != pwm->args.period)
 70		return -EINVAL;
 71
 72	duty = clps711x_get_duty(pwm, duty_ns);
 73	clps711x_pwm_update_val(priv, pwm->hwpwm, duty);
 
 
 74
 75	return 0;
 76}
 77
 78static int clps711x_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
 79{
 80	struct clps711x_chip *priv = to_clps711x_chip(chip);
 81	unsigned int duty;
 82
 83	duty = clps711x_get_duty(pwm, pwm_get_duty_cycle(pwm));
 84	clps711x_pwm_update_val(priv, pwm->hwpwm, duty);
 85
 86	return 0;
 87}
 88
 89static void clps711x_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
 90{
 91	struct clps711x_chip *priv = to_clps711x_chip(chip);
 92
 93	clps711x_pwm_update_val(priv, pwm->hwpwm, 0);
 94}
 95
 96static const struct pwm_ops clps711x_pwm_ops = {
 97	.request = clps711x_pwm_request,
 98	.config = clps711x_pwm_config,
 99	.enable = clps711x_pwm_enable,
100	.disable = clps711x_pwm_disable,
101	.owner = THIS_MODULE,
102};
103
104static struct pwm_device *clps711x_pwm_xlate(struct pwm_chip *chip,
105					     const struct of_phandle_args *args)
106{
107	if (args->args[0] >= chip->npwm)
108		return ERR_PTR(-EINVAL);
109
110	return pwm_request_from_chip(chip, args->args[0], NULL);
111}
112
113static int clps711x_pwm_probe(struct platform_device *pdev)
114{
115	struct clps711x_chip *priv;
116
117	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
118	if (!priv)
119		return -ENOMEM;
120
121	priv->pmpcon = devm_platform_ioremap_resource(pdev, 0);
122	if (IS_ERR(priv->pmpcon))
123		return PTR_ERR(priv->pmpcon);
124
125	priv->clk = devm_clk_get(&pdev->dev, NULL);
126	if (IS_ERR(priv->clk))
127		return PTR_ERR(priv->clk);
128
129	priv->chip.ops = &clps711x_pwm_ops;
130	priv->chip.dev = &pdev->dev;
131	priv->chip.npwm = 2;
132	priv->chip.of_xlate = clps711x_pwm_xlate;
133	priv->chip.of_pwm_n_cells = 1;
134
135	spin_lock_init(&priv->lock);
136
137	return devm_pwmchip_add(&pdev->dev, &priv->chip);
138}
139
140static const struct of_device_id __maybe_unused clps711x_pwm_dt_ids[] = {
141	{ .compatible = "cirrus,ep7209-pwm", },
142	{ }
143};
144MODULE_DEVICE_TABLE(of, clps711x_pwm_dt_ids);
145
146static struct platform_driver clps711x_pwm_driver = {
147	.driver = {
148		.name = "clps711x-pwm",
149		.of_match_table = of_match_ptr(clps711x_pwm_dt_ids),
150	},
151	.probe = clps711x_pwm_probe,
152};
153module_platform_driver(clps711x_pwm_driver);
154
155MODULE_AUTHOR("Alexander Shiyan <shc_work@mail.ru>");
156MODULE_DESCRIPTION("Cirrus Logic CLPS711X PWM driver");
157MODULE_LICENSE("GPL");