1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Azoteq IQS620A PWM Generator
4 *
5 * Copyright (C) 2019 Jeff LaBundy <jeff@labundy.com>
6 *
7 * Limitations:
8 * - The period is fixed to 1 ms and is generated continuously despite changes
9 * to the duty cycle or enable/disable state.
10 * - Changes to the duty cycle or enable/disable state take effect immediately
11 * and may result in a glitch during the period in which the change is made.
12 * - The device cannot generate a 0% duty cycle. For duty cycles below 1 / 256
13 * ms, the output is disabled and relies upon an external pull-down resistor
14 * to hold the GPIO3/LTX pin low.
15 */
16
17 #include <linux/device.h>
18 #include <linux/kernel.h>
19 #include <linux/mfd/iqs62x.h>
20 #include <linux/module.h>
21 #include <linux/mutex.h>
22 #include <linux/notifier.h>
23 #include <linux/platform_device.h>
24 #include <linux/pwm.h>
25 #include <linux/regmap.h>
26 #include <linux/slab.h>
27
28 #define IQS620_PWR_SETTINGS 0xd2
29 #define IQS620_PWR_SETTINGS_PWM_OUT BIT(7)
30
31 #define IQS620_PWM_DUTY_CYCLE 0xd8
32
33 #define IQS620_PWM_PERIOD_NS 1000000
34
35 struct iqs620_pwm_private {
36 struct iqs62x_core *iqs62x;
37 struct pwm_chip chip;
38 struct notifier_block notifier;
39 struct mutex lock;
40 bool out_en;
41 u8 duty_val;
42 };
43
iqs620_pwm_apply(struct pwm_chip * chip,struct pwm_device * pwm,const struct pwm_state * state)44 static int iqs620_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
45 const struct pwm_state *state)
46 {
47 struct iqs620_pwm_private *iqs620_pwm;
48 struct iqs62x_core *iqs62x;
49 unsigned int duty_cycle;
50 unsigned int duty_scale;
51 int ret;
52
53 if (state->polarity != PWM_POLARITY_NORMAL)
54 return -ENOTSUPP;
55
56 if (state->period < IQS620_PWM_PERIOD_NS)
57 return -EINVAL;
58
59 iqs620_pwm = container_of(chip, struct iqs620_pwm_private, chip);
60 iqs62x = iqs620_pwm->iqs62x;
61
62 /*
63 * The duty cycle generated by the device is calculated as follows:
64 *
65 * duty_cycle = (IQS620_PWM_DUTY_CYCLE + 1) / 256 * 1 ms
66 *
67 * ...where IQS620_PWM_DUTY_CYCLE is a register value between 0 and 255
68 * (inclusive). Therefore the lowest duty cycle the device can generate
69 * while the output is enabled is 1 / 256 ms.
70 *
71 * For lower duty cycles (e.g. 0), the PWM output is simply disabled to
72 * allow an external pull-down resistor to hold the GPIO3/LTX pin low.
73 */
74 duty_cycle = min_t(u64, state->duty_cycle, IQS620_PWM_PERIOD_NS);
75 duty_scale = duty_cycle * 256 / IQS620_PWM_PERIOD_NS;
76
77 mutex_lock(&iqs620_pwm->lock);
78
79 if (!state->enabled || !duty_scale) {
80 ret = regmap_update_bits(iqs62x->regmap, IQS620_PWR_SETTINGS,
81 IQS620_PWR_SETTINGS_PWM_OUT, 0);
82 if (ret)
83 goto err_mutex;
84 }
85
86 if (duty_scale) {
87 u8 duty_val = duty_scale - 1;
88
89 ret = regmap_write(iqs62x->regmap, IQS620_PWM_DUTY_CYCLE,
90 duty_val);
91 if (ret)
92 goto err_mutex;
93
94 iqs620_pwm->duty_val = duty_val;
95 }
96
97 if (state->enabled && duty_scale) {
98 ret = regmap_update_bits(iqs62x->regmap, IQS620_PWR_SETTINGS,
99 IQS620_PWR_SETTINGS_PWM_OUT, 0xff);
100 if (ret)
101 goto err_mutex;
102 }
103
104 iqs620_pwm->out_en = state->enabled;
105
106 err_mutex:
107 mutex_unlock(&iqs620_pwm->lock);
108
109 return ret;
110 }
111
iqs620_pwm_get_state(struct pwm_chip * chip,struct pwm_device * pwm,struct pwm_state * state)112 static void iqs620_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
113 struct pwm_state *state)
114 {
115 struct iqs620_pwm_private *iqs620_pwm;
116
117 iqs620_pwm = container_of(chip, struct iqs620_pwm_private, chip);
118
119 mutex_lock(&iqs620_pwm->lock);
120
121 /*
122 * Since the device cannot generate a 0% duty cycle, requests to do so
123 * cause subsequent calls to iqs620_pwm_get_state to report the output
124 * as disabled with duty cycle equal to that which was in use prior to
125 * the request. This is not ideal, but is the best compromise based on
126 * the capabilities of the device.
127 */
128 state->enabled = iqs620_pwm->out_en;
129 state->duty_cycle = DIV_ROUND_UP((iqs620_pwm->duty_val + 1) *
130 IQS620_PWM_PERIOD_NS, 256);
131
132 mutex_unlock(&iqs620_pwm->lock);
133
134 state->period = IQS620_PWM_PERIOD_NS;
135 }
136
iqs620_pwm_notifier(struct notifier_block * notifier,unsigned long event_flags,void * context)137 static int iqs620_pwm_notifier(struct notifier_block *notifier,
138 unsigned long event_flags, void *context)
139 {
140 struct iqs620_pwm_private *iqs620_pwm;
141 struct iqs62x_core *iqs62x;
142 int ret;
143
144 if (!(event_flags & BIT(IQS62X_EVENT_SYS_RESET)))
145 return NOTIFY_DONE;
146
147 iqs620_pwm = container_of(notifier, struct iqs620_pwm_private,
148 notifier);
149 iqs62x = iqs620_pwm->iqs62x;
150
151 mutex_lock(&iqs620_pwm->lock);
152
153 /*
154 * The parent MFD driver already prints an error message in the event
155 * of a device reset, so nothing else is printed here unless there is
156 * an additional failure.
157 */
158 ret = regmap_write(iqs62x->regmap, IQS620_PWM_DUTY_CYCLE,
159 iqs620_pwm->duty_val);
160 if (ret)
161 goto err_mutex;
162
163 ret = regmap_update_bits(iqs62x->regmap, IQS620_PWR_SETTINGS,
164 IQS620_PWR_SETTINGS_PWM_OUT,
165 iqs620_pwm->out_en ? 0xff : 0);
166
167 err_mutex:
168 mutex_unlock(&iqs620_pwm->lock);
169
170 if (ret) {
171 dev_err(iqs620_pwm->chip.dev,
172 "Failed to re-initialize device: %d\n", ret);
173 return NOTIFY_BAD;
174 }
175
176 return NOTIFY_OK;
177 }
178
179 static const struct pwm_ops iqs620_pwm_ops = {
180 .apply = iqs620_pwm_apply,
181 .get_state = iqs620_pwm_get_state,
182 .owner = THIS_MODULE,
183 };
184
iqs620_pwm_notifier_unregister(void * context)185 static void iqs620_pwm_notifier_unregister(void *context)
186 {
187 struct iqs620_pwm_private *iqs620_pwm = context;
188 int ret;
189
190 ret = blocking_notifier_chain_unregister(&iqs620_pwm->iqs62x->nh,
191 &iqs620_pwm->notifier);
192 if (ret)
193 dev_err(iqs620_pwm->chip.dev,
194 "Failed to unregister notifier: %d\n", ret);
195 }
196
iqs620_pwm_probe(struct platform_device * pdev)197 static int iqs620_pwm_probe(struct platform_device *pdev)
198 {
199 struct iqs62x_core *iqs62x = dev_get_drvdata(pdev->dev.parent);
200 struct iqs620_pwm_private *iqs620_pwm;
201 unsigned int val;
202 int ret;
203
204 iqs620_pwm = devm_kzalloc(&pdev->dev, sizeof(*iqs620_pwm), GFP_KERNEL);
205 if (!iqs620_pwm)
206 return -ENOMEM;
207
208 platform_set_drvdata(pdev, iqs620_pwm);
209 iqs620_pwm->iqs62x = iqs62x;
210
211 ret = regmap_read(iqs62x->regmap, IQS620_PWR_SETTINGS, &val);
212 if (ret)
213 return ret;
214 iqs620_pwm->out_en = val & IQS620_PWR_SETTINGS_PWM_OUT;
215
216 ret = regmap_read(iqs62x->regmap, IQS620_PWM_DUTY_CYCLE, &val);
217 if (ret)
218 return ret;
219 iqs620_pwm->duty_val = val;
220
221 iqs620_pwm->chip.dev = &pdev->dev;
222 iqs620_pwm->chip.ops = &iqs620_pwm_ops;
223 iqs620_pwm->chip.base = -1;
224 iqs620_pwm->chip.npwm = 1;
225
226 mutex_init(&iqs620_pwm->lock);
227
228 iqs620_pwm->notifier.notifier_call = iqs620_pwm_notifier;
229 ret = blocking_notifier_chain_register(&iqs620_pwm->iqs62x->nh,
230 &iqs620_pwm->notifier);
231 if (ret) {
232 dev_err(&pdev->dev, "Failed to register notifier: %d\n", ret);
233 return ret;
234 }
235
236 ret = devm_add_action_or_reset(&pdev->dev,
237 iqs620_pwm_notifier_unregister,
238 iqs620_pwm);
239 if (ret)
240 return ret;
241
242 ret = pwmchip_add(&iqs620_pwm->chip);
243 if (ret)
244 dev_err(&pdev->dev, "Failed to add device: %d\n", ret);
245
246 return ret;
247 }
248
iqs620_pwm_remove(struct platform_device * pdev)249 static int iqs620_pwm_remove(struct platform_device *pdev)
250 {
251 struct iqs620_pwm_private *iqs620_pwm = platform_get_drvdata(pdev);
252 int ret;
253
254 ret = pwmchip_remove(&iqs620_pwm->chip);
255 if (ret)
256 dev_err(&pdev->dev, "Failed to remove device: %d\n", ret);
257
258 return ret;
259 }
260
261 static struct platform_driver iqs620_pwm_platform_driver = {
262 .driver = {
263 .name = "iqs620a-pwm",
264 },
265 .probe = iqs620_pwm_probe,
266 .remove = iqs620_pwm_remove,
267 };
268 module_platform_driver(iqs620_pwm_platform_driver);
269
270 MODULE_AUTHOR("Jeff LaBundy <jeff@labundy.com>");
271 MODULE_DESCRIPTION("Azoteq IQS620A PWM Generator");
272 MODULE_LICENSE("GPL");
273 MODULE_ALIAS("platform:iqs620a-pwm");
274