Merge branch 'work.regset' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs
[linux/fpc-iii.git] / drivers / pwm / pwm-ab8500.c
blobfdf3964db4a6a47e3cb23d9915d97de2d411141d
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) ST-Ericsson SA 2010
5 * Author: Arun R Murthy <arun.murthy@stericsson.com>
6 */
7 #include <linux/err.h>
8 #include <linux/platform_device.h>
9 #include <linux/slab.h>
10 #include <linux/pwm.h>
11 #include <linux/mfd/abx500.h>
12 #include <linux/mfd/abx500/ab8500.h>
13 #include <linux/module.h>
16 * PWM Out generators
17 * Bank: 0x10
19 #define AB8500_PWM_OUT_CTRL1_REG 0x60
20 #define AB8500_PWM_OUT_CTRL2_REG 0x61
21 #define AB8500_PWM_OUT_CTRL7_REG 0x66
23 struct ab8500_pwm_chip {
24 struct pwm_chip chip;
27 static int ab8500_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
28 int duty_ns, int period_ns)
30 int ret = 0;
31 unsigned int higher_val, lower_val;
32 u8 reg;
35 * get the first 8 bits that are be written to
36 * AB8500_PWM_OUT_CTRL1_REG[0:7]
38 lower_val = duty_ns & 0x00FF;
40 * get bits [9:10] that are to be written to
41 * AB8500_PWM_OUT_CTRL2_REG[0:1]
43 higher_val = ((duty_ns & 0x0300) >> 8);
45 reg = AB8500_PWM_OUT_CTRL1_REG + ((chip->base - 1) * 2);
47 ret = abx500_set_register_interruptible(chip->dev, AB8500_MISC,
48 reg, (u8)lower_val);
49 if (ret < 0)
50 return ret;
51 ret = abx500_set_register_interruptible(chip->dev, AB8500_MISC,
52 (reg + 1), (u8)higher_val);
54 return ret;
57 static int ab8500_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
59 int ret;
61 ret = abx500_mask_and_set_register_interruptible(chip->dev,
62 AB8500_MISC, AB8500_PWM_OUT_CTRL7_REG,
63 1 << (chip->base - 1), 1 << (chip->base - 1));
64 if (ret < 0)
65 dev_err(chip->dev, "%s: Failed to enable PWM, Error %d\n",
66 pwm->label, ret);
67 return ret;
70 static void ab8500_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
72 int ret;
74 ret = abx500_mask_and_set_register_interruptible(chip->dev,
75 AB8500_MISC, AB8500_PWM_OUT_CTRL7_REG,
76 1 << (chip->base - 1), 0);
77 if (ret < 0)
78 dev_err(chip->dev, "%s: Failed to disable PWM, Error %d\n",
79 pwm->label, ret);
82 static const struct pwm_ops ab8500_pwm_ops = {
83 .config = ab8500_pwm_config,
84 .enable = ab8500_pwm_enable,
85 .disable = ab8500_pwm_disable,
86 .owner = THIS_MODULE,
89 static int ab8500_pwm_probe(struct platform_device *pdev)
91 struct ab8500_pwm_chip *ab8500;
92 int err;
95 * Nothing to be done in probe, this is required to get the
96 * device which is required for ab8500 read and write
98 ab8500 = devm_kzalloc(&pdev->dev, sizeof(*ab8500), GFP_KERNEL);
99 if (ab8500 == NULL)
100 return -ENOMEM;
102 ab8500->chip.dev = &pdev->dev;
103 ab8500->chip.ops = &ab8500_pwm_ops;
104 ab8500->chip.base = pdev->id;
105 ab8500->chip.npwm = 1;
107 err = pwmchip_add(&ab8500->chip);
108 if (err < 0)
109 return err;
111 dev_dbg(&pdev->dev, "pwm probe successful\n");
112 platform_set_drvdata(pdev, ab8500);
114 return 0;
117 static int ab8500_pwm_remove(struct platform_device *pdev)
119 struct ab8500_pwm_chip *ab8500 = platform_get_drvdata(pdev);
120 int err;
122 err = pwmchip_remove(&ab8500->chip);
123 if (err < 0)
124 return err;
126 dev_dbg(&pdev->dev, "pwm driver removed\n");
128 return 0;
131 static struct platform_driver ab8500_pwm_driver = {
132 .driver = {
133 .name = "ab8500-pwm",
135 .probe = ab8500_pwm_probe,
136 .remove = ab8500_pwm_remove,
138 module_platform_driver(ab8500_pwm_driver);
140 MODULE_AUTHOR("Arun MURTHY <arun.murthy@stericsson.com>");
141 MODULE_DESCRIPTION("AB8500 Pulse Width Modulation Driver");
142 MODULE_ALIAS("platform:ab8500-pwm");
143 MODULE_LICENSE("GPL v2");