xref: /linux/drivers/pwm/pwm-ab8500.c (revision ca55b2fef3a9373fcfc30f82fd26bc7fccbda732)
1 /*
2  * Copyright (C) ST-Ericsson SA 2010
3  *
4  * Author: Arun R Murthy <arun.murthy@stericsson.com>
5  * License terms: GNU General Public License (GPL) version 2
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>
14 
15 /*
16  * PWM Out generators
17  * Bank: 0x10
18  */
19 #define AB8500_PWM_OUT_CTRL1_REG	0x60
20 #define AB8500_PWM_OUT_CTRL2_REG	0x61
21 #define AB8500_PWM_OUT_CTRL7_REG	0x66
22 
23 struct ab8500_pwm_chip {
24 	struct pwm_chip chip;
25 };
26 
27 static int ab8500_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
28 			     int duty_ns, int period_ns)
29 {
30 	int ret = 0;
31 	unsigned int higher_val, lower_val;
32 	u8 reg;
33 
34 	/*
35 	 * get the first 8 bits that are be written to
36 	 * AB8500_PWM_OUT_CTRL1_REG[0:7]
37 	 */
38 	lower_val = duty_ns & 0x00FF;
39 	/*
40 	 * get bits [9:10] that are to be written to
41 	 * AB8500_PWM_OUT_CTRL2_REG[0:1]
42 	 */
43 	higher_val = ((duty_ns & 0x0300) >> 8);
44 
45 	reg = AB8500_PWM_OUT_CTRL1_REG + ((chip->base - 1) * 2);
46 
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);
53 
54 	return ret;
55 }
56 
57 static int ab8500_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
58 {
59 	int ret;
60 
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;
68 }
69 
70 static void ab8500_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
71 {
72 	int ret;
73 
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);
80 }
81 
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,
87 };
88 
89 static int ab8500_pwm_probe(struct platform_device *pdev)
90 {
91 	struct ab8500_pwm_chip *ab8500;
92 	int err;
93 
94 	/*
95 	 * Nothing to be done in probe, this is required to get the
96 	 * device which is required for ab8500 read and write
97 	 */
98 	ab8500 = devm_kzalloc(&pdev->dev, sizeof(*ab8500), GFP_KERNEL);
99 	if (ab8500 == NULL)
100 		return -ENOMEM;
101 
102 	ab8500->chip.dev = &pdev->dev;
103 	ab8500->chip.ops = &ab8500_pwm_ops;
104 	ab8500->chip.base = pdev->id;
105 	ab8500->chip.npwm = 1;
106 
107 	err = pwmchip_add(&ab8500->chip);
108 	if (err < 0)
109 		return err;
110 
111 	dev_dbg(&pdev->dev, "pwm probe successful\n");
112 	platform_set_drvdata(pdev, ab8500);
113 
114 	return 0;
115 }
116 
117 static int ab8500_pwm_remove(struct platform_device *pdev)
118 {
119 	struct ab8500_pwm_chip *ab8500 = platform_get_drvdata(pdev);
120 	int err;
121 
122 	err = pwmchip_remove(&ab8500->chip);
123 	if (err < 0)
124 		return err;
125 
126 	dev_dbg(&pdev->dev, "pwm driver removed\n");
127 
128 	return 0;
129 }
130 
131 static struct platform_driver ab8500_pwm_driver = {
132 	.driver = {
133 		.name = "ab8500-pwm",
134 	},
135 	.probe = ab8500_pwm_probe,
136 	.remove = ab8500_pwm_remove,
137 };
138 module_platform_driver(ab8500_pwm_driver);
139 
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");
144