blob: 54bd95a5cab0ccf337636c1a83bda2c5b02c8113 [file] [log] [blame]
Jeff LaBundy6f0841a2020-04-12 21:15:16 -05001// 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
Thierry Redingb8fb6422020-06-15 16:10:16 +020028#define IQS620_PWR_SETTINGS 0xd2
Jeff LaBundy6f0841a2020-04-12 21:15:16 -050029#define IQS620_PWR_SETTINGS_PWM_OUT BIT(7)
30
Thierry Redingb8fb6422020-06-15 16:10:16 +020031#define IQS620_PWM_DUTY_CYCLE 0xd8
Jeff LaBundy6f0841a2020-04-12 21:15:16 -050032
33#define IQS620_PWM_PERIOD_NS 1000000
34
35struct iqs620_pwm_private {
36 struct iqs62x_core *iqs62x;
37 struct pwm_chip chip;
38 struct notifier_block notifier;
39 struct mutex lock;
Jeff LaBundy28208c72021-01-18 22:30:29 -060040 unsigned int duty_scale;
Jeff LaBundy6f0841a2020-04-12 21:15:16 -050041};
42
Jeff LaBundy28208c72021-01-18 22:30:29 -060043static int iqs620_pwm_init(struct iqs620_pwm_private *iqs620_pwm,
44 unsigned int duty_scale)
45{
46 struct iqs62x_core *iqs62x = iqs620_pwm->iqs62x;
47 int ret;
48
49 if (!duty_scale)
50 return regmap_update_bits(iqs62x->regmap, IQS620_PWR_SETTINGS,
51 IQS620_PWR_SETTINGS_PWM_OUT, 0);
52
53 ret = regmap_write(iqs62x->regmap, IQS620_PWM_DUTY_CYCLE,
54 duty_scale - 1);
55 if (ret)
56 return ret;
57
58 return regmap_update_bits(iqs62x->regmap, IQS620_PWR_SETTINGS,
59 IQS620_PWR_SETTINGS_PWM_OUT, 0xff);
60}
61
Jeff LaBundy6f0841a2020-04-12 21:15:16 -050062static int iqs620_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
63 const struct pwm_state *state)
64{
65 struct iqs620_pwm_private *iqs620_pwm;
Uwe Kleine-König72d6b242021-01-15 08:32:39 +010066 unsigned int duty_cycle;
67 unsigned int duty_scale;
Thierry Reding5bc5d992020-06-15 16:08:28 +020068 int ret;
Jeff LaBundy6f0841a2020-04-12 21:15:16 -050069
70 if (state->polarity != PWM_POLARITY_NORMAL)
Thierry Reding2b1c1a52020-11-11 21:18:11 +010071 return -EINVAL;
Jeff LaBundy6f0841a2020-04-12 21:15:16 -050072
73 if (state->period < IQS620_PWM_PERIOD_NS)
74 return -EINVAL;
75
76 iqs620_pwm = container_of(chip, struct iqs620_pwm_private, chip);
Jeff LaBundy6f0841a2020-04-12 21:15:16 -050077
78 /*
79 * The duty cycle generated by the device is calculated as follows:
80 *
81 * duty_cycle = (IQS620_PWM_DUTY_CYCLE + 1) / 256 * 1 ms
82 *
83 * ...where IQS620_PWM_DUTY_CYCLE is a register value between 0 and 255
84 * (inclusive). Therefore the lowest duty cycle the device can generate
85 * while the output is enabled is 1 / 256 ms.
86 *
87 * For lower duty cycles (e.g. 0), the PWM output is simply disabled to
88 * allow an external pull-down resistor to hold the GPIO3/LTX pin low.
89 */
Uwe Kleine-König72d6b242021-01-15 08:32:39 +010090 duty_cycle = min_t(u64, state->duty_cycle, IQS620_PWM_PERIOD_NS);
91 duty_scale = duty_cycle * 256 / IQS620_PWM_PERIOD_NS;
Jeff LaBundy6f0841a2020-04-12 21:15:16 -050092
Jeff LaBundy28208c72021-01-18 22:30:29 -060093 if (!state->enabled)
94 duty_scale = 0;
95
Jeff LaBundy6f0841a2020-04-12 21:15:16 -050096 mutex_lock(&iqs620_pwm->lock);
97
Jeff LaBundy28208c72021-01-18 22:30:29 -060098 ret = iqs620_pwm_init(iqs620_pwm, duty_scale);
99 if (!ret)
100 iqs620_pwm->duty_scale = duty_scale;
Jeff LaBundy6f0841a2020-04-12 21:15:16 -0500101
Jeff LaBundy6f0841a2020-04-12 21:15:16 -0500102 mutex_unlock(&iqs620_pwm->lock);
103
104 return ret;
105}
106
107static void iqs620_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
108 struct pwm_state *state)
109{
110 struct iqs620_pwm_private *iqs620_pwm;
111
112 iqs620_pwm = container_of(chip, struct iqs620_pwm_private, chip);
113
114 mutex_lock(&iqs620_pwm->lock);
115
116 /*
117 * Since the device cannot generate a 0% duty cycle, requests to do so
118 * cause subsequent calls to iqs620_pwm_get_state to report the output
Jeff LaBundy28208c72021-01-18 22:30:29 -0600119 * as disabled. This is not ideal, but is the best compromise based on
Jeff LaBundy6f0841a2020-04-12 21:15:16 -0500120 * the capabilities of the device.
121 */
Jeff LaBundy28208c72021-01-18 22:30:29 -0600122 state->enabled = iqs620_pwm->duty_scale > 0;
123 state->duty_cycle = DIV_ROUND_UP(iqs620_pwm->duty_scale *
Jeff LaBundy6f0841a2020-04-12 21:15:16 -0500124 IQS620_PWM_PERIOD_NS, 256);
125
126 mutex_unlock(&iqs620_pwm->lock);
127
128 state->period = IQS620_PWM_PERIOD_NS;
129}
130
131static int iqs620_pwm_notifier(struct notifier_block *notifier,
132 unsigned long event_flags, void *context)
133{
134 struct iqs620_pwm_private *iqs620_pwm;
Jeff LaBundy6f0841a2020-04-12 21:15:16 -0500135 int ret;
136
137 if (!(event_flags & BIT(IQS62X_EVENT_SYS_RESET)))
138 return NOTIFY_DONE;
139
140 iqs620_pwm = container_of(notifier, struct iqs620_pwm_private,
141 notifier);
Jeff LaBundy6f0841a2020-04-12 21:15:16 -0500142
143 mutex_lock(&iqs620_pwm->lock);
144
145 /*
146 * The parent MFD driver already prints an error message in the event
147 * of a device reset, so nothing else is printed here unless there is
148 * an additional failure.
149 */
Jeff LaBundy28208c72021-01-18 22:30:29 -0600150 ret = iqs620_pwm_init(iqs620_pwm, iqs620_pwm->duty_scale);
Jeff LaBundy6f0841a2020-04-12 21:15:16 -0500151
Jeff LaBundy6f0841a2020-04-12 21:15:16 -0500152 mutex_unlock(&iqs620_pwm->lock);
153
154 if (ret) {
155 dev_err(iqs620_pwm->chip.dev,
156 "Failed to re-initialize device: %d\n", ret);
157 return NOTIFY_BAD;
158 }
159
160 return NOTIFY_OK;
161}
162
163static const struct pwm_ops iqs620_pwm_ops = {
164 .apply = iqs620_pwm_apply,
165 .get_state = iqs620_pwm_get_state,
166 .owner = THIS_MODULE,
167};
168
169static void iqs620_pwm_notifier_unregister(void *context)
170{
171 struct iqs620_pwm_private *iqs620_pwm = context;
172 int ret;
173
174 ret = blocking_notifier_chain_unregister(&iqs620_pwm->iqs62x->nh,
175 &iqs620_pwm->notifier);
176 if (ret)
177 dev_err(iqs620_pwm->chip.dev,
178 "Failed to unregister notifier: %d\n", ret);
179}
180
181static int iqs620_pwm_probe(struct platform_device *pdev)
182{
183 struct iqs62x_core *iqs62x = dev_get_drvdata(pdev->dev.parent);
184 struct iqs620_pwm_private *iqs620_pwm;
185 unsigned int val;
186 int ret;
187
188 iqs620_pwm = devm_kzalloc(&pdev->dev, sizeof(*iqs620_pwm), GFP_KERNEL);
189 if (!iqs620_pwm)
190 return -ENOMEM;
191
Jeff LaBundy6f0841a2020-04-12 21:15:16 -0500192 iqs620_pwm->iqs62x = iqs62x;
193
194 ret = regmap_read(iqs62x->regmap, IQS620_PWR_SETTINGS, &val);
195 if (ret)
196 return ret;
Jeff LaBundy6f0841a2020-04-12 21:15:16 -0500197
Jeff LaBundy28208c72021-01-18 22:30:29 -0600198 if (val & IQS620_PWR_SETTINGS_PWM_OUT) {
199 ret = regmap_read(iqs62x->regmap, IQS620_PWM_DUTY_CYCLE, &val);
200 if (ret)
201 return ret;
202
203 iqs620_pwm->duty_scale = val + 1;
204 }
Jeff LaBundy6f0841a2020-04-12 21:15:16 -0500205
206 iqs620_pwm->chip.dev = &pdev->dev;
207 iqs620_pwm->chip.ops = &iqs620_pwm_ops;
Jeff LaBundy6f0841a2020-04-12 21:15:16 -0500208 iqs620_pwm->chip.npwm = 1;
209
210 mutex_init(&iqs620_pwm->lock);
211
212 iqs620_pwm->notifier.notifier_call = iqs620_pwm_notifier;
213 ret = blocking_notifier_chain_register(&iqs620_pwm->iqs62x->nh,
214 &iqs620_pwm->notifier);
215 if (ret) {
216 dev_err(&pdev->dev, "Failed to register notifier: %d\n", ret);
217 return ret;
218 }
219
220 ret = devm_add_action_or_reset(&pdev->dev,
221 iqs620_pwm_notifier_unregister,
222 iqs620_pwm);
223 if (ret)
224 return ret;
225
Uwe Kleine-König2e27afd2021-07-07 18:28:06 +0200226 ret = devm_pwmchip_add(&pdev->dev, &iqs620_pwm->chip);
Jeff LaBundy6f0841a2020-04-12 21:15:16 -0500227 if (ret)
228 dev_err(&pdev->dev, "Failed to add device: %d\n", ret);
229
230 return ret;
231}
232
Jeff LaBundy6f0841a2020-04-12 21:15:16 -0500233static struct platform_driver iqs620_pwm_platform_driver = {
234 .driver = {
235 .name = "iqs620a-pwm",
236 },
237 .probe = iqs620_pwm_probe,
Jeff LaBundy6f0841a2020-04-12 21:15:16 -0500238};
239module_platform_driver(iqs620_pwm_platform_driver);
240
241MODULE_AUTHOR("Jeff LaBundy <jeff@labundy.com>");
242MODULE_DESCRIPTION("Azoteq IQS620A PWM Generator");
243MODULE_LICENSE("GPL");
244MODULE_ALIAS("platform:iqs620a-pwm");