18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Cirrus Logic CLPS711X PWM driver
48c2ecf20Sopenharmony_ci * Author: Alexander Shiyan <shc_work@mail.ru>
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#include <linux/clk.h>
88c2ecf20Sopenharmony_ci#include <linux/io.h>
98c2ecf20Sopenharmony_ci#include <linux/module.h>
108c2ecf20Sopenharmony_ci#include <linux/of.h>
118c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
128c2ecf20Sopenharmony_ci#include <linux/pwm.h>
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_cistruct clps711x_chip {
158c2ecf20Sopenharmony_ci	struct pwm_chip chip;
168c2ecf20Sopenharmony_ci	void __iomem *pmpcon;
178c2ecf20Sopenharmony_ci	struct clk *clk;
188c2ecf20Sopenharmony_ci	spinlock_t lock;
198c2ecf20Sopenharmony_ci};
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_cistatic inline struct clps711x_chip *to_clps711x_chip(struct pwm_chip *chip)
228c2ecf20Sopenharmony_ci{
238c2ecf20Sopenharmony_ci	return container_of(chip, struct clps711x_chip, chip);
248c2ecf20Sopenharmony_ci}
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_cistatic void clps711x_pwm_update_val(struct clps711x_chip *priv, u32 n, u32 v)
278c2ecf20Sopenharmony_ci{
288c2ecf20Sopenharmony_ci	/* PWM0 - bits 4..7, PWM1 - bits 8..11 */
298c2ecf20Sopenharmony_ci	u32 shift = (n + 1) * 4;
308c2ecf20Sopenharmony_ci	unsigned long flags;
318c2ecf20Sopenharmony_ci	u32 tmp;
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci	spin_lock_irqsave(&priv->lock, flags);
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci	tmp = readl(priv->pmpcon);
368c2ecf20Sopenharmony_ci	tmp &= ~(0xf << shift);
378c2ecf20Sopenharmony_ci	tmp |= v << shift;
388c2ecf20Sopenharmony_ci	writel(tmp, priv->pmpcon);
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&priv->lock, flags);
418c2ecf20Sopenharmony_ci}
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_cistatic unsigned int clps711x_get_duty(struct pwm_device *pwm, unsigned int v)
448c2ecf20Sopenharmony_ci{
458c2ecf20Sopenharmony_ci	/* Duty cycle 0..15 max */
468c2ecf20Sopenharmony_ci	return DIV64_U64_ROUND_CLOSEST(v * 0xf, pwm->args.period);
478c2ecf20Sopenharmony_ci}
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_cistatic int clps711x_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
508c2ecf20Sopenharmony_ci{
518c2ecf20Sopenharmony_ci	struct clps711x_chip *priv = to_clps711x_chip(chip);
528c2ecf20Sopenharmony_ci	unsigned int freq = clk_get_rate(priv->clk);
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci	if (!freq)
558c2ecf20Sopenharmony_ci		return -EINVAL;
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci	/* Store constant period value */
588c2ecf20Sopenharmony_ci	pwm->args.period = DIV_ROUND_CLOSEST(NSEC_PER_SEC, freq);
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci	return 0;
618c2ecf20Sopenharmony_ci}
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_cistatic int clps711x_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
648c2ecf20Sopenharmony_ci			       int duty_ns, int period_ns)
658c2ecf20Sopenharmony_ci{
668c2ecf20Sopenharmony_ci	struct clps711x_chip *priv = to_clps711x_chip(chip);
678c2ecf20Sopenharmony_ci	unsigned int duty;
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci	if (period_ns != pwm->args.period)
708c2ecf20Sopenharmony_ci		return -EINVAL;
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci	duty = clps711x_get_duty(pwm, duty_ns);
738c2ecf20Sopenharmony_ci	clps711x_pwm_update_val(priv, pwm->hwpwm, duty);
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci	return 0;
768c2ecf20Sopenharmony_ci}
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_cistatic int clps711x_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
798c2ecf20Sopenharmony_ci{
808c2ecf20Sopenharmony_ci	struct clps711x_chip *priv = to_clps711x_chip(chip);
818c2ecf20Sopenharmony_ci	unsigned int duty;
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	duty = clps711x_get_duty(pwm, pwm_get_duty_cycle(pwm));
848c2ecf20Sopenharmony_ci	clps711x_pwm_update_val(priv, pwm->hwpwm, duty);
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci	return 0;
878c2ecf20Sopenharmony_ci}
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_cistatic void clps711x_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
908c2ecf20Sopenharmony_ci{
918c2ecf20Sopenharmony_ci	struct clps711x_chip *priv = to_clps711x_chip(chip);
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci	clps711x_pwm_update_val(priv, pwm->hwpwm, 0);
948c2ecf20Sopenharmony_ci}
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_cistatic const struct pwm_ops clps711x_pwm_ops = {
978c2ecf20Sopenharmony_ci	.request = clps711x_pwm_request,
988c2ecf20Sopenharmony_ci	.config = clps711x_pwm_config,
998c2ecf20Sopenharmony_ci	.enable = clps711x_pwm_enable,
1008c2ecf20Sopenharmony_ci	.disable = clps711x_pwm_disable,
1018c2ecf20Sopenharmony_ci	.owner = THIS_MODULE,
1028c2ecf20Sopenharmony_ci};
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_cistatic struct pwm_device *clps711x_pwm_xlate(struct pwm_chip *chip,
1058c2ecf20Sopenharmony_ci					     const struct of_phandle_args *args)
1068c2ecf20Sopenharmony_ci{
1078c2ecf20Sopenharmony_ci	if (args->args[0] >= chip->npwm)
1088c2ecf20Sopenharmony_ci		return ERR_PTR(-EINVAL);
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	return pwm_request_from_chip(chip, args->args[0], NULL);
1118c2ecf20Sopenharmony_ci}
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_cistatic int clps711x_pwm_probe(struct platform_device *pdev)
1148c2ecf20Sopenharmony_ci{
1158c2ecf20Sopenharmony_ci	struct clps711x_chip *priv;
1168c2ecf20Sopenharmony_ci	struct resource *res;
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
1198c2ecf20Sopenharmony_ci	if (!priv)
1208c2ecf20Sopenharmony_ci		return -ENOMEM;
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1238c2ecf20Sopenharmony_ci	priv->pmpcon = devm_ioremap_resource(&pdev->dev, res);
1248c2ecf20Sopenharmony_ci	if (IS_ERR(priv->pmpcon))
1258c2ecf20Sopenharmony_ci		return PTR_ERR(priv->pmpcon);
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci	priv->clk = devm_clk_get(&pdev->dev, NULL);
1288c2ecf20Sopenharmony_ci	if (IS_ERR(priv->clk))
1298c2ecf20Sopenharmony_ci		return PTR_ERR(priv->clk);
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci	priv->chip.ops = &clps711x_pwm_ops;
1328c2ecf20Sopenharmony_ci	priv->chip.dev = &pdev->dev;
1338c2ecf20Sopenharmony_ci	priv->chip.base = -1;
1348c2ecf20Sopenharmony_ci	priv->chip.npwm = 2;
1358c2ecf20Sopenharmony_ci	priv->chip.of_xlate = clps711x_pwm_xlate;
1368c2ecf20Sopenharmony_ci	priv->chip.of_pwm_n_cells = 1;
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci	spin_lock_init(&priv->lock);
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, priv);
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci	return pwmchip_add(&priv->chip);
1438c2ecf20Sopenharmony_ci}
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_cistatic int clps711x_pwm_remove(struct platform_device *pdev)
1468c2ecf20Sopenharmony_ci{
1478c2ecf20Sopenharmony_ci	struct clps711x_chip *priv = platform_get_drvdata(pdev);
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	return pwmchip_remove(&priv->chip);
1508c2ecf20Sopenharmony_ci}
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_cistatic const struct of_device_id __maybe_unused clps711x_pwm_dt_ids[] = {
1538c2ecf20Sopenharmony_ci	{ .compatible = "cirrus,ep7209-pwm", },
1548c2ecf20Sopenharmony_ci	{ }
1558c2ecf20Sopenharmony_ci};
1568c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, clps711x_pwm_dt_ids);
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_cistatic struct platform_driver clps711x_pwm_driver = {
1598c2ecf20Sopenharmony_ci	.driver = {
1608c2ecf20Sopenharmony_ci		.name = "clps711x-pwm",
1618c2ecf20Sopenharmony_ci		.of_match_table = of_match_ptr(clps711x_pwm_dt_ids),
1628c2ecf20Sopenharmony_ci	},
1638c2ecf20Sopenharmony_ci	.probe = clps711x_pwm_probe,
1648c2ecf20Sopenharmony_ci	.remove = clps711x_pwm_remove,
1658c2ecf20Sopenharmony_ci};
1668c2ecf20Sopenharmony_cimodule_platform_driver(clps711x_pwm_driver);
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ciMODULE_AUTHOR("Alexander Shiyan <shc_work@mail.ru>");
1698c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Cirrus Logic CLPS711X PWM driver");
1708c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
171