Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (C) ST-Ericsson SA 2010
0004  *
0005  * Author: Arun R Murthy <arun.murthy@stericsson.com>
0006  */
0007 #include <linux/err.h>
0008 #include <linux/platform_device.h>
0009 #include <linux/slab.h>
0010 #include <linux/pwm.h>
0011 #include <linux/mfd/abx500.h>
0012 #include <linux/mfd/abx500/ab8500.h>
0013 #include <linux/module.h>
0014 
0015 /*
0016  * PWM Out generators
0017  * Bank: 0x10
0018  */
0019 #define AB8500_PWM_OUT_CTRL1_REG    0x60
0020 #define AB8500_PWM_OUT_CTRL2_REG    0x61
0021 #define AB8500_PWM_OUT_CTRL7_REG    0x66
0022 
0023 struct ab8500_pwm_chip {
0024     struct pwm_chip chip;
0025     unsigned int hwid;
0026 };
0027 
0028 static struct ab8500_pwm_chip *ab8500_pwm_from_chip(struct pwm_chip *chip)
0029 {
0030     return container_of(chip, struct ab8500_pwm_chip, chip);
0031 }
0032 
0033 static int ab8500_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
0034                 const struct pwm_state *state)
0035 {
0036     int ret;
0037     u8 reg;
0038     unsigned int higher_val, lower_val;
0039     struct ab8500_pwm_chip *ab8500 = ab8500_pwm_from_chip(chip);
0040 
0041     if (state->polarity != PWM_POLARITY_NORMAL)
0042         return -EINVAL;
0043 
0044     if (!state->enabled) {
0045         ret = abx500_mask_and_set_register_interruptible(chip->dev,
0046                     AB8500_MISC, AB8500_PWM_OUT_CTRL7_REG,
0047                     1 << ab8500->hwid, 0);
0048 
0049         if (ret < 0)
0050             dev_err(chip->dev, "%s: Failed to disable PWM, Error %d\n",
0051                                 pwm->label, ret);
0052         return ret;
0053     }
0054 
0055     /*
0056      * get the first 8 bits that are be written to
0057      * AB8500_PWM_OUT_CTRL1_REG[0:7]
0058      */
0059     lower_val = state->duty_cycle & 0x00FF;
0060     /*
0061      * get bits [9:10] that are to be written to
0062      * AB8500_PWM_OUT_CTRL2_REG[0:1]
0063      */
0064     higher_val = ((state->duty_cycle & 0x0300) >> 8);
0065 
0066     reg = AB8500_PWM_OUT_CTRL1_REG + (ab8500->hwid * 2);
0067 
0068     ret = abx500_set_register_interruptible(chip->dev, AB8500_MISC,
0069             reg, (u8)lower_val);
0070     if (ret < 0)
0071         return ret;
0072 
0073     ret = abx500_set_register_interruptible(chip->dev, AB8500_MISC,
0074             (reg + 1), (u8)higher_val);
0075     if (ret < 0)
0076         return ret;
0077 
0078     ret = abx500_mask_and_set_register_interruptible(chip->dev,
0079                 AB8500_MISC, AB8500_PWM_OUT_CTRL7_REG,
0080                 1 << ab8500->hwid, 1 << ab8500->hwid);
0081     if (ret < 0)
0082         dev_err(chip->dev, "%s: Failed to enable PWM, Error %d\n",
0083                             pwm->label, ret);
0084 
0085     return ret;
0086 }
0087 
0088 static const struct pwm_ops ab8500_pwm_ops = {
0089     .apply = ab8500_pwm_apply,
0090     .owner = THIS_MODULE,
0091 };
0092 
0093 static int ab8500_pwm_probe(struct platform_device *pdev)
0094 {
0095     struct ab8500_pwm_chip *ab8500;
0096     int err;
0097 
0098     if (pdev->id < 1 || pdev->id > 31)
0099         return dev_err_probe(&pdev->dev, EINVAL, "Invalid device id %d\n", pdev->id);
0100 
0101     /*
0102      * Nothing to be done in probe, this is required to get the
0103      * device which is required for ab8500 read and write
0104      */
0105     ab8500 = devm_kzalloc(&pdev->dev, sizeof(*ab8500), GFP_KERNEL);
0106     if (ab8500 == NULL)
0107         return -ENOMEM;
0108 
0109     ab8500->chip.dev = &pdev->dev;
0110     ab8500->chip.ops = &ab8500_pwm_ops;
0111     ab8500->chip.npwm = 1;
0112     ab8500->hwid = pdev->id - 1;
0113 
0114     err = devm_pwmchip_add(&pdev->dev, &ab8500->chip);
0115     if (err < 0)
0116         return dev_err_probe(&pdev->dev, err, "Failed to add pwm chip\n");
0117 
0118     dev_dbg(&pdev->dev, "pwm probe successful\n");
0119 
0120     return 0;
0121 }
0122 
0123 static struct platform_driver ab8500_pwm_driver = {
0124     .driver = {
0125         .name = "ab8500-pwm",
0126     },
0127     .probe = ab8500_pwm_probe,
0128 };
0129 module_platform_driver(ab8500_pwm_driver);
0130 
0131 MODULE_AUTHOR("Arun MURTHY <arun.murthy@stericsson.com>");
0132 MODULE_DESCRIPTION("AB8500 Pulse Width Modulation Driver");
0133 MODULE_ALIAS("platform:ab8500-pwm");
0134 MODULE_LICENSE("GPL v2");