1 // SPDX-License-Identifier: GPL-2.0
3 // Copyright (C) 2021 ROHM Semiconductors
4 // regulator IRQ based event notification helpers
6 // Logic has been partially adapted from qcom-labibb driver.
8 // Author: Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>
10 #include <linux/device.h>
11 #include <linux/err.h>
12 #include <linux/interrupt.h>
13 #include <linux/kernel.h>
14 #include <linux/reboot.h>
15 #include <linux/regmap.h>
16 #include <linux/slab.h>
17 #include <linux/spinlock.h>
18 #include <linux/regulator/driver.h>
22 #define REGULATOR_FORCED_SAFETY_SHUTDOWN_WAIT_MS 10000
24 struct regulator_irq
{
25 struct regulator_irq_data rdata
;
26 struct regulator_irq_desc desc
;
29 struct delayed_work isr_work
;
33 * Should only be called from threaded handler to prevent potential deadlock
35 static void rdev_flag_err(struct regulator_dev
*rdev
, int err
)
37 spin_lock(&rdev
->err_lock
);
38 rdev
->cached_err
|= err
;
39 spin_unlock(&rdev
->err_lock
);
42 static void rdev_clear_err(struct regulator_dev
*rdev
, int err
)
44 spin_lock(&rdev
->err_lock
);
45 rdev
->cached_err
&= ~err
;
46 spin_unlock(&rdev
->err_lock
);
49 static void regulator_notifier_isr_work(struct work_struct
*work
)
51 struct regulator_irq
*h
;
52 struct regulator_irq_desc
*d
;
53 struct regulator_irq_data
*rid
;
58 h
= container_of(work
, struct regulator_irq
,
62 num_rdevs
= rid
->num_states
;
65 if (d
->fatal_cnt
&& h
->retry_cnt
> d
->fatal_cnt
) {
67 return hw_protection_shutdown("Regulator HW failure? - no IC recovery",
68 REGULATOR_FORCED_SAFETY_SHUTDOWN_WAIT_MS
);
71 * If the 'last resort' IC recovery failed we will have
72 * nothing else left to do...
75 return hw_protection_shutdown("Regulator HW failure. IC recovery failed",
76 REGULATOR_FORCED_SAFETY_SHUTDOWN_WAIT_MS
);
79 * If h->die() was implemented we assume recovery has been
80 * attempted (probably regulator was shut down) and we
81 * just enable IRQ and bail-out.
86 ret
= d
->renable(rid
);
88 if (ret
== REGULATOR_FAILED_RETRY
) {
89 /* Driver could not get current status */
100 * IC status reading succeeded. update error info
101 * just in case the renable changed it.
103 for (i
= 0; i
< num_rdevs
; i
++) {
104 struct regulator_err_state
*stat
;
105 struct regulator_dev
*rdev
;
107 stat
= &rid
->states
[i
];
109 rdev_clear_err(rdev
, (~stat
->errors
) &
110 stat
->possible_errs
);
114 * The IC indicated problem is still ON - no point in
115 * re-enabling the IRQ. Retry later.
123 * Either IC reported problem cleared or no status checker was provided.
124 * If problems are gone - good. If not - then the IRQ will fire again
125 * and we'll have a new nice loop. In any case we should clear error
126 * flags here and re-enable IRQs.
128 for (i
= 0; i
< num_rdevs
; i
++) {
129 struct regulator_err_state
*stat
;
130 struct regulator_dev
*rdev
;
132 stat
= &rid
->states
[i
];
134 rdev_clear_err(rdev
, stat
->possible_errs
);
138 * Things have been seemingly successful => zero retry-counter.
149 mod_delayed_work(system_wq
, &h
->isr_work
,
150 msecs_to_jiffies(tmo
));
152 mod_delayed_work(system_highpri_wq
, &h
->isr_work
,
153 msecs_to_jiffies(tmo
));
156 static irqreturn_t
regulator_notifier_isr(int irq
, void *data
)
158 struct regulator_irq
*h
= data
;
159 struct regulator_irq_desc
*d
;
160 struct regulator_irq_data
*rid
;
161 unsigned long rdev_map
= 0;
167 num_rdevs
= rid
->num_states
;
173 * we spare a few cycles by not clearing statuses prior to this call.
174 * The IC driver must initialize the status buffers for rdevs
175 * which it indicates having active events via rdev_map.
177 * Maybe we should just to be on a safer side(?)
179 ret
= d
->map_event(irq
, rid
, &rdev_map
);
182 * If status reading fails (which is unlikely) we don't ack/disable
183 * IRQ but just increase fail count and retry when IRQ fires again.
184 * If retry_count exceeds the given safety limit we call IC specific die
185 * handler which can try disabling regulator(s).
187 * If no die handler is given we will just power-off as a last resort.
189 * We could try disabling all associated rdevs - but we might shoot
190 * ourselves in the head and leave the problematic regulator enabled. So
191 * if IC has no die-handler populated we just assume the regulator
194 if (unlikely(ret
== REGULATOR_FAILED_RETRY
))
199 * Let's not disable IRQ if there were no status bits for us. We'd
200 * better leave spurious IRQ handling to genirq
202 if (ret
|| !rdev_map
)
206 * Some events are bogus if the regulator is disabled. Skip such events
207 * if all relevant regulators are disabled
210 for_each_set_bit(i
, &rdev_map
, num_rdevs
) {
211 struct regulator_dev
*rdev
;
212 const struct regulator_ops
*ops
;
214 rdev
= rid
->states
[i
].rdev
;
215 ops
= rdev
->desc
->ops
;
218 * If any of the flagged regulators is enabled we do
221 if (ops
->is_enabled(rdev
))
228 /* Disable IRQ if HW keeps line asserted */
230 disable_irq_nosync(irq
);
233 * IRQ seems to be for us. Let's fire correct notifiers / store error
236 for_each_set_bit(i
, &rdev_map
, num_rdevs
) {
237 struct regulator_err_state
*stat
;
238 struct regulator_dev
*rdev
;
240 stat
= &rid
->states
[i
];
243 rdev_dbg(rdev
, "Sending regulator notification EVT 0x%lx\n",
246 regulator_notifier_call_chain(rdev
, stat
->notifs
, NULL
);
247 rdev_flag_err(rdev
, stat
->errors
);
252 schedule_delayed_work(&h
->isr_work
,
253 msecs_to_jiffies(d
->irq_off_ms
));
255 mod_delayed_work(system_highpri_wq
,
257 msecs_to_jiffies(d
->irq_off_ms
));
263 if (d
->fatal_cnt
&& h
->retry_cnt
> d
->fatal_cnt
) {
264 /* If we have no recovery, just try shut down straight away */
266 hw_protection_shutdown("Regulator failure. Retry count exceeded",
267 REGULATOR_FORCED_SAFETY_SHUTDOWN_WAIT_MS
);
270 /* If die() failed shut down as a last attempt to save the HW */
272 hw_protection_shutdown("Regulator failure. Recovery failed",
273 REGULATOR_FORCED_SAFETY_SHUTDOWN_WAIT_MS
);
280 static int init_rdev_state(struct device
*dev
, struct regulator_irq
*h
,
281 struct regulator_dev
**rdev
, int common_err
,
282 int *rdev_err
, int rdev_amount
)
286 h
->rdata
.states
= devm_kzalloc(dev
, sizeof(*h
->rdata
.states
) *
287 rdev_amount
, GFP_KERNEL
);
288 if (!h
->rdata
.states
)
291 h
->rdata
.num_states
= rdev_amount
;
292 h
->rdata
.data
= h
->desc
.data
;
294 for (i
= 0; i
< rdev_amount
; i
++) {
295 h
->rdata
.states
[i
].possible_errs
= common_err
;
297 h
->rdata
.states
[i
].possible_errs
|= *rdev_err
++;
298 h
->rdata
.states
[i
].rdev
= *rdev
++;
304 static void init_rdev_errors(struct regulator_irq
*h
)
308 for (i
= 0; i
< h
->rdata
.num_states
; i
++)
309 if (h
->rdata
.states
[i
].possible_errs
)
310 h
->rdata
.states
[i
].rdev
->use_cached_err
= true;
314 * regulator_irq_helper - register IRQ based regulator event/error notifier
316 * @dev: device providing the IRQs
317 * @d: IRQ helper descriptor.
318 * @irq: IRQ used to inform events/errors to be notified.
319 * @irq_flags: Extra IRQ flags to be OR'ed with the default
320 * IRQF_ONESHOT when requesting the (threaded) irq.
321 * @common_errs: Errors which can be flagged by this IRQ for all rdevs.
322 * When IRQ is re-enabled these errors will be cleared
323 * from all associated regulators. Use this instead of the
324 * per_rdev_errs if you use
325 * regulator_irq_map_event_simple() for event mapping.
326 * @per_rdev_errs: Optional error flag array describing errors specific
327 * for only some of the regulators. These errors will be
328 * or'ed with common errors. If this is given the array
329 * should contain rdev_amount flags. Can be set to NULL
330 * if there is no regulator specific error flags for this
332 * @rdev: Array of pointers to regulators associated with this
334 * @rdev_amount: Amount of regulators associated with this IRQ.
336 * Return: handle to irq_helper or an ERR_PTR() encoded negative error number.
338 void *regulator_irq_helper(struct device
*dev
,
339 const struct regulator_irq_desc
*d
, int irq
,
340 int irq_flags
, int common_errs
, int *per_rdev_errs
,
341 struct regulator_dev
**rdev
, int rdev_amount
)
343 struct regulator_irq
*h
;
346 if (!rdev_amount
|| !d
|| !d
->map_event
|| !d
->name
)
347 return ERR_PTR(-EINVAL
);
349 h
= devm_kzalloc(dev
, sizeof(*h
), GFP_KERNEL
);
351 return ERR_PTR(-ENOMEM
);
355 h
->desc
.name
= devm_kstrdup(dev
, d
->name
, GFP_KERNEL
);
357 return ERR_PTR(-ENOMEM
);
359 ret
= init_rdev_state(dev
, h
, rdev
, common_errs
, per_rdev_errs
,
366 if (h
->desc
.irq_off_ms
)
367 INIT_DELAYED_WORK(&h
->isr_work
, regulator_notifier_isr_work
);
369 ret
= request_threaded_irq(h
->irq
, NULL
, regulator_notifier_isr
,
370 IRQF_ONESHOT
| irq_flags
, h
->desc
.name
, h
);
372 dev_err(dev
, "Failed to request IRQ %d\n", irq
);
379 EXPORT_SYMBOL_GPL(regulator_irq_helper
);
382 * regulator_irq_helper_cancel - drop IRQ based regulator event/error notifier
384 * @handle: Pointer to handle returned by a successful call to
385 * regulator_irq_helper(). Will be NULLed upon return.
387 * The associated IRQ is released and work is cancelled when the function
390 void regulator_irq_helper_cancel(void **handle
)
392 if (handle
&& *handle
) {
393 struct regulator_irq
*h
= *handle
;
396 if (h
->desc
.irq_off_ms
)
397 cancel_delayed_work_sync(&h
->isr_work
);
402 EXPORT_SYMBOL_GPL(regulator_irq_helper_cancel
);
405 * regulator_irq_map_event_simple - regulator IRQ notification for trivial IRQs
407 * @irq: Number of IRQ that occurred.
408 * @rid: Information about the event IRQ indicates.
409 * The function fills in the ®ulator_err_state->notifs
410 * and ®ulator_err_state->errors fields of
411 * ®ulator_irq_data->states as output.
412 * @dev_mask: mask indicating the regulator originating the IRQ.
414 * Regulators whose IRQ has single, well defined purpose (always indicate
415 * exactly one event, and are relevant to exactly one regulator device) can
416 * use this function as their map_event callback for their regulator IRQ
417 * notification helper. Exactly one rdev and exactly one error (in
418 * "common_errs"-field) can be given at IRQ helper registration for
419 * regulator_irq_map_event_simple() to be viable.
423 int regulator_irq_map_event_simple(int irq
, struct regulator_irq_data
*rid
,
424 unsigned long *dev_mask
)
426 int err
= rid
->states
[0].possible_errs
;
430 * This helper should only be used in a situation where the IRQ
431 * can indicate only one type of problem for one specific rdev.
432 * Something fishy is going on if we are having multiple rdevs or ERROR
435 if (WARN_ON(rid
->num_states
!= 1 || hweight32(err
) != 1))
438 rid
->states
[0].errors
= err
;
439 rid
->states
[0].notifs
= regulator_err2notif(err
);
443 EXPORT_SYMBOL_GPL(regulator_irq_map_event_simple
);