1 // SPDX-License-Identifier: GPL-2.0+
3 * Azoteq IQS620A PWM Generator
5 * Copyright (C) 2019 Jeff LaBundy <jeff@labundy.com>
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.
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>
28 #define IQS620_PWR_SETTINGS 0xd2
29 #define IQS620_PWR_SETTINGS_PWM_OUT BIT(7)
31 #define IQS620_PWM_DUTY_CYCLE 0xd8
33 #define IQS620_PWM_PERIOD_NS 1000000
35 struct iqs620_pwm_private {
36 struct iqs62x_core *iqs62x;
38 struct notifier_block notifier;
44 static int iqs620_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
45 const struct pwm_state *state)
47 struct iqs620_pwm_private *iqs620_pwm;
48 struct iqs62x_core *iqs62x;
49 unsigned int duty_cycle;
50 unsigned int duty_scale;
53 if (state->polarity != PWM_POLARITY_NORMAL)
56 if (state->period < IQS620_PWM_PERIOD_NS)
59 iqs620_pwm = container_of(chip, struct iqs620_pwm_private, chip);
60 iqs62x = iqs620_pwm->iqs62x;
63 * The duty cycle generated by the device is calculated as follows:
65 * duty_cycle = (IQS620_PWM_DUTY_CYCLE + 1) / 256 * 1 ms
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.
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.
74 duty_cycle = min_t(u64, state->duty_cycle, IQS620_PWM_PERIOD_NS);
75 duty_scale = duty_cycle * 256 / IQS620_PWM_PERIOD_NS;
77 mutex_lock(&iqs620_pwm->lock);
79 if (!state->enabled || !duty_scale) {
80 ret = regmap_update_bits(iqs62x->regmap, IQS620_PWR_SETTINGS,
81 IQS620_PWR_SETTINGS_PWM_OUT, 0);
87 u8 duty_val = duty_scale - 1;
89 ret = regmap_write(iqs62x->regmap, IQS620_PWM_DUTY_CYCLE,
94 iqs620_pwm->duty_val = duty_val;
97 if (state->enabled && duty_scale) {
98 ret = regmap_update_bits(iqs62x->regmap, IQS620_PWR_SETTINGS,
99 IQS620_PWR_SETTINGS_PWM_OUT, 0xff);
104 iqs620_pwm->out_en = state->enabled;
107 mutex_unlock(&iqs620_pwm->lock);
112 static void iqs620_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
113 struct pwm_state *state)
115 struct iqs620_pwm_private *iqs620_pwm;
117 iqs620_pwm = container_of(chip, struct iqs620_pwm_private, chip);
119 mutex_lock(&iqs620_pwm->lock);
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.
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);
132 mutex_unlock(&iqs620_pwm->lock);
134 state->period = IQS620_PWM_PERIOD_NS;
135 state->polarity = PWM_POLARITY_NORMAL;
138 static int iqs620_pwm_notifier(struct notifier_block *notifier,
139 unsigned long event_flags, void *context)
141 struct iqs620_pwm_private *iqs620_pwm;
142 struct iqs62x_core *iqs62x;
145 if (!(event_flags & BIT(IQS62X_EVENT_SYS_RESET)))
148 iqs620_pwm = container_of(notifier, struct iqs620_pwm_private,
150 iqs62x = iqs620_pwm->iqs62x;
152 mutex_lock(&iqs620_pwm->lock);
155 * The parent MFD driver already prints an error message in the event
156 * of a device reset, so nothing else is printed here unless there is
157 * an additional failure.
159 ret = regmap_write(iqs62x->regmap, IQS620_PWM_DUTY_CYCLE,
160 iqs620_pwm->duty_val);
164 ret = regmap_update_bits(iqs62x->regmap, IQS620_PWR_SETTINGS,
165 IQS620_PWR_SETTINGS_PWM_OUT,
166 iqs620_pwm->out_en ? 0xff : 0);
169 mutex_unlock(&iqs620_pwm->lock);
172 dev_err(iqs620_pwm->chip.dev,
173 "Failed to re-initialize device: %d\n", ret);
180 static const struct pwm_ops iqs620_pwm_ops = {
181 .apply = iqs620_pwm_apply,
182 .get_state = iqs620_pwm_get_state,
183 .owner = THIS_MODULE,
186 static void iqs620_pwm_notifier_unregister(void *context)
188 struct iqs620_pwm_private *iqs620_pwm = context;
191 ret = blocking_notifier_chain_unregister(&iqs620_pwm->iqs62x->nh,
192 &iqs620_pwm->notifier);
194 dev_err(iqs620_pwm->chip.dev,
195 "Failed to unregister notifier: %d\n", ret);
198 static int iqs620_pwm_probe(struct platform_device *pdev)
200 struct iqs62x_core *iqs62x = dev_get_drvdata(pdev->dev.parent);
201 struct iqs620_pwm_private *iqs620_pwm;
205 iqs620_pwm = devm_kzalloc(&pdev->dev, sizeof(*iqs620_pwm), GFP_KERNEL);
209 platform_set_drvdata(pdev, iqs620_pwm);
210 iqs620_pwm->iqs62x = iqs62x;
212 ret = regmap_read(iqs62x->regmap, IQS620_PWR_SETTINGS, &val);
215 iqs620_pwm->out_en = val & IQS620_PWR_SETTINGS_PWM_OUT;
217 ret = regmap_read(iqs62x->regmap, IQS620_PWM_DUTY_CYCLE, &val);
220 iqs620_pwm->duty_val = val;
222 iqs620_pwm->chip.dev = &pdev->dev;
223 iqs620_pwm->chip.ops = &iqs620_pwm_ops;
224 iqs620_pwm->chip.base = -1;
225 iqs620_pwm->chip.npwm = 1;
227 mutex_init(&iqs620_pwm->lock);
229 iqs620_pwm->notifier.notifier_call = iqs620_pwm_notifier;
230 ret = blocking_notifier_chain_register(&iqs620_pwm->iqs62x->nh,
231 &iqs620_pwm->notifier);
233 dev_err(&pdev->dev, "Failed to register notifier: %d\n", ret);
237 ret = devm_add_action_or_reset(&pdev->dev,
238 iqs620_pwm_notifier_unregister,
243 ret = pwmchip_add(&iqs620_pwm->chip);
245 dev_err(&pdev->dev, "Failed to add device: %d\n", ret);
250 static int iqs620_pwm_remove(struct platform_device *pdev)
252 struct iqs620_pwm_private *iqs620_pwm = platform_get_drvdata(pdev);
255 ret = pwmchip_remove(&iqs620_pwm->chip);
257 dev_err(&pdev->dev, "Failed to remove device: %d\n", ret);
262 static struct platform_driver iqs620_pwm_platform_driver = {
264 .name = "iqs620a-pwm",
266 .probe = iqs620_pwm_probe,
267 .remove = iqs620_pwm_remove,
269 module_platform_driver(iqs620_pwm_platform_driver);
271 MODULE_AUTHOR("Jeff LaBundy <jeff@labundy.com>");
272 MODULE_DESCRIPTION("Azoteq IQS620A PWM Generator");
273 MODULE_LICENSE("GPL");
274 MODULE_ALIAS("platform:iqs620a-pwm");