Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * Cirrus Logic CLPS711X PWM driver
0004  * Author: Alexander Shiyan <shc_work@mail.ru>
0005  */
0006 
0007 #include <linux/clk.h>
0008 #include <linux/io.h>
0009 #include <linux/module.h>
0010 #include <linux/of.h>
0011 #include <linux/platform_device.h>
0012 #include <linux/pwm.h>
0013 
0014 struct clps711x_chip {
0015     struct pwm_chip chip;
0016     void __iomem *pmpcon;
0017     struct clk *clk;
0018     spinlock_t lock;
0019 };
0020 
0021 static inline struct clps711x_chip *to_clps711x_chip(struct pwm_chip *chip)
0022 {
0023     return container_of(chip, struct clps711x_chip, chip);
0024 }
0025 
0026 static int clps711x_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
0027 {
0028     struct clps711x_chip *priv = to_clps711x_chip(chip);
0029     unsigned int freq = clk_get_rate(priv->clk);
0030 
0031     if (!freq)
0032         return -EINVAL;
0033 
0034     /* Store constant period value */
0035     pwm->args.period = DIV_ROUND_CLOSEST(NSEC_PER_SEC, freq);
0036 
0037     return 0;
0038 }
0039 
0040 static int clps711x_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
0041                   const struct pwm_state *state)
0042 {
0043     struct clps711x_chip *priv = to_clps711x_chip(chip);
0044     /* PWM0 - bits 4..7, PWM1 - bits 8..11 */
0045     u32 shift = (pwm->hwpwm + 1) * 4;
0046     unsigned long flags;
0047     u32 pmpcon, val;
0048 
0049     if (state->polarity != PWM_POLARITY_NORMAL)
0050         return -EINVAL;
0051 
0052     if (state->period != pwm->args.period)
0053         return -EINVAL;
0054 
0055     if (state->enabled)
0056         val = mul_u64_u64_div_u64(state->duty_cycle, 0xf, state->period);
0057     else
0058         val = 0;
0059 
0060     spin_lock_irqsave(&priv->lock, flags);
0061 
0062     pmpcon = readl(priv->pmpcon);
0063     pmpcon &= ~(0xf << shift);
0064     pmpcon |= val << shift;
0065     writel(pmpcon, priv->pmpcon);
0066 
0067     spin_unlock_irqrestore(&priv->lock, flags);
0068 
0069     return 0;
0070 }
0071 
0072 static const struct pwm_ops clps711x_pwm_ops = {
0073     .request = clps711x_pwm_request,
0074     .apply = clps711x_pwm_apply,
0075     .owner = THIS_MODULE,
0076 };
0077 
0078 static struct pwm_device *clps711x_pwm_xlate(struct pwm_chip *chip,
0079                          const struct of_phandle_args *args)
0080 {
0081     if (args->args[0] >= chip->npwm)
0082         return ERR_PTR(-EINVAL);
0083 
0084     return pwm_request_from_chip(chip, args->args[0], NULL);
0085 }
0086 
0087 static int clps711x_pwm_probe(struct platform_device *pdev)
0088 {
0089     struct clps711x_chip *priv;
0090 
0091     priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
0092     if (!priv)
0093         return -ENOMEM;
0094 
0095     priv->pmpcon = devm_platform_ioremap_resource(pdev, 0);
0096     if (IS_ERR(priv->pmpcon))
0097         return PTR_ERR(priv->pmpcon);
0098 
0099     priv->clk = devm_clk_get(&pdev->dev, NULL);
0100     if (IS_ERR(priv->clk))
0101         return PTR_ERR(priv->clk);
0102 
0103     priv->chip.ops = &clps711x_pwm_ops;
0104     priv->chip.dev = &pdev->dev;
0105     priv->chip.npwm = 2;
0106     priv->chip.of_xlate = clps711x_pwm_xlate;
0107     priv->chip.of_pwm_n_cells = 1;
0108 
0109     spin_lock_init(&priv->lock);
0110 
0111     return devm_pwmchip_add(&pdev->dev, &priv->chip);
0112 }
0113 
0114 static const struct of_device_id __maybe_unused clps711x_pwm_dt_ids[] = {
0115     { .compatible = "cirrus,ep7209-pwm", },
0116     { }
0117 };
0118 MODULE_DEVICE_TABLE(of, clps711x_pwm_dt_ids);
0119 
0120 static struct platform_driver clps711x_pwm_driver = {
0121     .driver = {
0122         .name = "clps711x-pwm",
0123         .of_match_table = of_match_ptr(clps711x_pwm_dt_ids),
0124     },
0125     .probe = clps711x_pwm_probe,
0126 };
0127 module_platform_driver(clps711x_pwm_driver);
0128 
0129 MODULE_AUTHOR("Alexander Shiyan <shc_work@mail.ru>");
0130 MODULE_DESCRIPTION("Cirrus Logic CLPS711X PWM driver");
0131 MODULE_LICENSE("GPL");