Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 //
0003 // Copyright (C) 2021 ROHM Semiconductors
0004 // regulator IRQ based event notification helpers
0005 //
0006 // Logic has been partially adapted from qcom-labibb driver.
0007 //
0008 // Author: Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>
0009 
0010 #include <linux/device.h>
0011 #include <linux/err.h>
0012 #include <linux/interrupt.h>
0013 #include <linux/kernel.h>
0014 #include <linux/reboot.h>
0015 #include <linux/regmap.h>
0016 #include <linux/slab.h>
0017 #include <linux/spinlock.h>
0018 #include <linux/regulator/driver.h>
0019 
0020 #include "internal.h"
0021 
0022 #define REGULATOR_FORCED_SAFETY_SHUTDOWN_WAIT_MS 10000
0023 
0024 struct regulator_irq {
0025     struct regulator_irq_data rdata;
0026     struct regulator_irq_desc desc;
0027     int irq;
0028     int retry_cnt;
0029     struct delayed_work isr_work;
0030 };
0031 
0032 /*
0033  * Should only be called from threaded handler to prevent potential deadlock
0034  */
0035 static void rdev_flag_err(struct regulator_dev *rdev, int err)
0036 {
0037     spin_lock(&rdev->err_lock);
0038     rdev->cached_err |= err;
0039     spin_unlock(&rdev->err_lock);
0040 }
0041 
0042 static void rdev_clear_err(struct regulator_dev *rdev, int err)
0043 {
0044     spin_lock(&rdev->err_lock);
0045     rdev->cached_err &= ~err;
0046     spin_unlock(&rdev->err_lock);
0047 }
0048 
0049 static void regulator_notifier_isr_work(struct work_struct *work)
0050 {
0051     struct regulator_irq *h;
0052     struct regulator_irq_desc *d;
0053     struct regulator_irq_data *rid;
0054     int ret = 0;
0055     int tmo, i;
0056     int num_rdevs;
0057 
0058     h = container_of(work, struct regulator_irq,
0059                 isr_work.work);
0060     d = &h->desc;
0061     rid = &h->rdata;
0062     num_rdevs = rid->num_states;
0063 
0064 reread:
0065     if (d->fatal_cnt && h->retry_cnt > d->fatal_cnt) {
0066         if (!d->die)
0067             return hw_protection_shutdown("Regulator HW failure? - no IC recovery",
0068                               REGULATOR_FORCED_SAFETY_SHUTDOWN_WAIT_MS);
0069         ret = d->die(rid);
0070         /*
0071          * If the 'last resort' IC recovery failed we will have
0072          * nothing else left to do...
0073          */
0074         if (ret)
0075             return hw_protection_shutdown("Regulator HW failure. IC recovery failed",
0076                               REGULATOR_FORCED_SAFETY_SHUTDOWN_WAIT_MS);
0077 
0078         /*
0079          * If h->die() was implemented we assume recovery has been
0080          * attempted (probably regulator was shut down) and we
0081          * just enable IRQ and bail-out.
0082          */
0083         goto enable_out;
0084     }
0085     if (d->renable) {
0086         ret = d->renable(rid);
0087 
0088         if (ret == REGULATOR_FAILED_RETRY) {
0089             /* Driver could not get current status */
0090             h->retry_cnt++;
0091             if (!d->reread_ms)
0092                 goto reread;
0093 
0094             tmo = d->reread_ms;
0095             goto reschedule;
0096         }
0097 
0098         if (ret) {
0099             /*
0100              * IC status reading succeeded. update error info
0101              * just in case the renable changed it.
0102              */
0103             for (i = 0; i < num_rdevs; i++) {
0104                 struct regulator_err_state *stat;
0105                 struct regulator_dev *rdev;
0106 
0107                 stat = &rid->states[i];
0108                 rdev = stat->rdev;
0109                 rdev_clear_err(rdev, (~stat->errors) &
0110                               stat->possible_errs);
0111             }
0112             h->retry_cnt++;
0113             /*
0114              * The IC indicated problem is still ON - no point in
0115              * re-enabling the IRQ. Retry later.
0116              */
0117             tmo = d->irq_off_ms;
0118             goto reschedule;
0119         }
0120     }
0121 
0122     /*
0123      * Either IC reported problem cleared or no status checker was provided.
0124      * If problems are gone - good. If not - then the IRQ will fire again
0125      * and we'll have a new nice loop. In any case we should clear error
0126      * flags here and re-enable IRQs.
0127      */
0128     for (i = 0; i < num_rdevs; i++) {
0129         struct regulator_err_state *stat;
0130         struct regulator_dev *rdev;
0131 
0132         stat = &rid->states[i];
0133         rdev = stat->rdev;
0134         rdev_clear_err(rdev, stat->possible_errs);
0135     }
0136 
0137     /*
0138      * Things have been seemingly successful => zero retry-counter.
0139      */
0140     h->retry_cnt = 0;
0141 
0142 enable_out:
0143     enable_irq(h->irq);
0144 
0145     return;
0146 
0147 reschedule:
0148     if (!d->high_prio)
0149         mod_delayed_work(system_wq, &h->isr_work,
0150                  msecs_to_jiffies(tmo));
0151     else
0152         mod_delayed_work(system_highpri_wq, &h->isr_work,
0153                  msecs_to_jiffies(tmo));
0154 }
0155 
0156 static irqreturn_t regulator_notifier_isr(int irq, void *data)
0157 {
0158     struct regulator_irq *h = data;
0159     struct regulator_irq_desc *d;
0160     struct regulator_irq_data *rid;
0161     unsigned long rdev_map = 0;
0162     int num_rdevs;
0163     int ret, i;
0164 
0165     d = &h->desc;
0166     rid = &h->rdata;
0167     num_rdevs = rid->num_states;
0168 
0169     if (d->fatal_cnt)
0170         h->retry_cnt++;
0171 
0172     /*
0173      * we spare a few cycles by not clearing statuses prior to this call.
0174      * The IC driver must initialize the status buffers for rdevs
0175      * which it indicates having active events via rdev_map.
0176      *
0177      * Maybe we should just to be on a safer side(?)
0178      */
0179     ret = d->map_event(irq, rid, &rdev_map);
0180 
0181     /*
0182      * If status reading fails (which is unlikely) we don't ack/disable
0183      * IRQ but just increase fail count and retry when IRQ fires again.
0184      * If retry_count exceeds the given safety limit we call IC specific die
0185      * handler which can try disabling regulator(s).
0186      *
0187      * If no die handler is given we will just power-off as a last resort.
0188      *
0189      * We could try disabling all associated rdevs - but we might shoot
0190      * ourselves in the head and leave the problematic regulator enabled. So
0191      * if IC has no die-handler populated we just assume the regulator
0192      * can't be disabled.
0193      */
0194     if (unlikely(ret == REGULATOR_FAILED_RETRY))
0195         goto fail_out;
0196 
0197     h->retry_cnt = 0;
0198     /*
0199      * Let's not disable IRQ if there were no status bits for us. We'd
0200      * better leave spurious IRQ handling to genirq
0201      */
0202     if (ret || !rdev_map)
0203         return IRQ_NONE;
0204 
0205     /*
0206      * Some events are bogus if the regulator is disabled. Skip such events
0207      * if all relevant regulators are disabled
0208      */
0209     if (d->skip_off) {
0210         for_each_set_bit(i, &rdev_map, num_rdevs) {
0211             struct regulator_dev *rdev;
0212             const struct regulator_ops *ops;
0213 
0214             rdev = rid->states[i].rdev;
0215             ops = rdev->desc->ops;
0216 
0217             /*
0218              * If any of the flagged regulators is enabled we do
0219              * handle this
0220              */
0221             if (ops->is_enabled(rdev))
0222                 break;
0223         }
0224         if (i == num_rdevs)
0225             return IRQ_NONE;
0226     }
0227 
0228     /* Disable IRQ if HW keeps line asserted */
0229     if (d->irq_off_ms)
0230         disable_irq_nosync(irq);
0231 
0232     /*
0233      * IRQ seems to be for us. Let's fire correct notifiers / store error
0234      * flags
0235      */
0236     for_each_set_bit(i, &rdev_map, num_rdevs) {
0237         struct regulator_err_state *stat;
0238         struct regulator_dev *rdev;
0239 
0240         stat = &rid->states[i];
0241         rdev = stat->rdev;
0242 
0243         rdev_dbg(rdev, "Sending regulator notification EVT 0x%lx\n",
0244              stat->notifs);
0245 
0246         regulator_notifier_call_chain(rdev, stat->notifs, NULL);
0247         rdev_flag_err(rdev, stat->errors);
0248     }
0249 
0250     if (d->irq_off_ms) {
0251         if (!d->high_prio)
0252             schedule_delayed_work(&h->isr_work,
0253                           msecs_to_jiffies(d->irq_off_ms));
0254         else
0255             mod_delayed_work(system_highpri_wq,
0256                      &h->isr_work,
0257                      msecs_to_jiffies(d->irq_off_ms));
0258     }
0259 
0260     return IRQ_HANDLED;
0261 
0262 fail_out:
0263     if (d->fatal_cnt && h->retry_cnt > d->fatal_cnt) {
0264         /* If we have no recovery, just try shut down straight away */
0265         if (!d->die) {
0266             hw_protection_shutdown("Regulator failure. Retry count exceeded",
0267                            REGULATOR_FORCED_SAFETY_SHUTDOWN_WAIT_MS);
0268         } else {
0269             ret = d->die(rid);
0270             /* If die() failed shut down as a last attempt to save the HW */
0271             if (ret)
0272                 hw_protection_shutdown("Regulator failure. Recovery failed",
0273                                REGULATOR_FORCED_SAFETY_SHUTDOWN_WAIT_MS);
0274         }
0275     }
0276 
0277     return IRQ_NONE;
0278 }
0279 
0280 static int init_rdev_state(struct device *dev, struct regulator_irq *h,
0281                struct regulator_dev **rdev, int common_err,
0282                int *rdev_err, int rdev_amount)
0283 {
0284     int i;
0285 
0286     h->rdata.states = devm_kzalloc(dev, sizeof(*h->rdata.states) *
0287                        rdev_amount, GFP_KERNEL);
0288     if (!h->rdata.states)
0289         return -ENOMEM;
0290 
0291     h->rdata.num_states = rdev_amount;
0292     h->rdata.data = h->desc.data;
0293 
0294     for (i = 0; i < rdev_amount; i++) {
0295         h->rdata.states[i].possible_errs = common_err;
0296         if (rdev_err)
0297             h->rdata.states[i].possible_errs |= *rdev_err++;
0298         h->rdata.states[i].rdev = *rdev++;
0299     }
0300 
0301     return 0;
0302 }
0303 
0304 static void init_rdev_errors(struct regulator_irq *h)
0305 {
0306     int i;
0307 
0308     for (i = 0; i < h->rdata.num_states; i++)
0309         if (h->rdata.states[i].possible_errs)
0310             h->rdata.states[i].rdev->use_cached_err = true;
0311 }
0312 
0313 /**
0314  * regulator_irq_helper - register IRQ based regulator event/error notifier
0315  *
0316  * @dev:        device providing the IRQs
0317  * @d:          IRQ helper descriptor.
0318  * @irq:        IRQ used to inform events/errors to be notified.
0319  * @irq_flags:      Extra IRQ flags to be OR'ed with the default
0320  *          IRQF_ONESHOT when requesting the (threaded) irq.
0321  * @common_errs:    Errors which can be flagged by this IRQ for all rdevs.
0322  *          When IRQ is re-enabled these errors will be cleared
0323  *          from all associated regulators. Use this instead of the
0324  *          per_rdev_errs if you use
0325  *          regulator_irq_map_event_simple() for event mapping.
0326  * @per_rdev_errs:  Optional error flag array describing errors specific
0327  *          for only some of the regulators. These errors will be
0328  *          or'ed with common errors. If this is given the array
0329  *          should contain rdev_amount flags. Can be set to NULL
0330  *          if there is no regulator specific error flags for this
0331  *          IRQ.
0332  * @rdev:       Array of pointers to regulators associated with this
0333  *          IRQ.
0334  * @rdev_amount:    Amount of regulators associated with this IRQ.
0335  *
0336  * Return: handle to irq_helper or an ERR_PTR() encoded error code.
0337  */
0338 void *regulator_irq_helper(struct device *dev,
0339                const struct regulator_irq_desc *d, int irq,
0340                int irq_flags, int common_errs, int *per_rdev_errs,
0341                struct regulator_dev **rdev, int rdev_amount)
0342 {
0343     struct regulator_irq *h;
0344     int ret;
0345 
0346     if (!rdev_amount || !d || !d->map_event || !d->name)
0347         return ERR_PTR(-EINVAL);
0348 
0349     h = devm_kzalloc(dev, sizeof(*h), GFP_KERNEL);
0350     if (!h)
0351         return ERR_PTR(-ENOMEM);
0352 
0353     h->irq = irq;
0354     h->desc = *d;
0355 
0356     ret = init_rdev_state(dev, h, rdev, common_errs, per_rdev_errs,
0357                   rdev_amount);
0358     if (ret)
0359         return ERR_PTR(ret);
0360 
0361     init_rdev_errors(h);
0362 
0363     if (h->desc.irq_off_ms)
0364         INIT_DELAYED_WORK(&h->isr_work, regulator_notifier_isr_work);
0365 
0366     ret = request_threaded_irq(h->irq, NULL, regulator_notifier_isr,
0367                    IRQF_ONESHOT | irq_flags, h->desc.name, h);
0368     if (ret) {
0369         dev_err(dev, "Failed to request IRQ %d\n", irq);
0370 
0371         return ERR_PTR(ret);
0372     }
0373 
0374     return h;
0375 }
0376 EXPORT_SYMBOL_GPL(regulator_irq_helper);
0377 
0378 /**
0379  * regulator_irq_helper_cancel - drop IRQ based regulator event/error notifier
0380  *
0381  * @handle:     Pointer to handle returned by a successful call to
0382  *          regulator_irq_helper(). Will be NULLed upon return.
0383  *
0384  * The associated IRQ is released and work is cancelled when the function
0385  * returns.
0386  */
0387 void regulator_irq_helper_cancel(void **handle)
0388 {
0389     if (handle && *handle) {
0390         struct regulator_irq *h = *handle;
0391 
0392         free_irq(h->irq, h);
0393         if (h->desc.irq_off_ms)
0394             cancel_delayed_work_sync(&h->isr_work);
0395 
0396         h = NULL;
0397     }
0398 }
0399 EXPORT_SYMBOL_GPL(regulator_irq_helper_cancel);
0400 
0401 /**
0402  * regulator_irq_map_event_simple - regulator IRQ notification for trivial IRQs
0403  *
0404  * @irq:    Number of IRQ that occurred
0405  * @rid:    Information about the event IRQ indicates
0406  * @dev_mask:   mask indicating the regulator originating the IRQ
0407  *
0408  * Regulators whose IRQ has single, well defined purpose (always indicate
0409  * exactly one event, and are relevant to exactly one regulator device) can
0410  * use this function as their map_event callbac for their regulator IRQ
0411  * notification helperk. Exactly one rdev and exactly one error (in
0412  * "common_errs"-field) can be given at IRQ helper registration for
0413  * regulator_irq_map_event_simple() to be viable.
0414  */
0415 int regulator_irq_map_event_simple(int irq, struct regulator_irq_data *rid,
0416                 unsigned long *dev_mask)
0417 {
0418     int err = rid->states[0].possible_errs;
0419 
0420     *dev_mask = 1;
0421     /*
0422      * This helper should only be used in a situation where the IRQ
0423      * can indicate only one type of problem for one specific rdev.
0424      * Something fishy is going on if we are having multiple rdevs or ERROR
0425      * flags here.
0426      */
0427     if (WARN_ON(rid->num_states != 1 || hweight32(err) != 1))
0428         return 0;
0429 
0430     rid->states[0].errors = err;
0431     rid->states[0].notifs = regulator_err2notif(err);
0432 
0433     return 0;
0434 }
0435 EXPORT_SYMBOL_GPL(regulator_irq_map_event_simple);
0436