linux/drivers/reset/core.c
<<
>>
Prefs
   1/*
   2 * Reset Controller framework
   3 *
   4 * Copyright 2013 Philipp Zabel, Pengutronix
   5 *
   6 * This program is free software; you can redistribute it and/or modify
   7 * it under the terms of the GNU General Public License as published by
   8 * the Free Software Foundation; either version 2 of the License, or
   9 * (at your option) any later version.
  10 */
  11#include <linux/device.h>
  12#include <linux/err.h>
  13#include <linux/export.h>
  14#include <linux/kernel.h>
  15#include <linux/module.h>
  16#include <linux/of.h>
  17#include <linux/reset.h>
  18#include <linux/reset-controller.h>
  19#include <linux/slab.h>
  20
  21static DEFINE_MUTEX(reset_controller_list_mutex);
  22static LIST_HEAD(reset_controller_list);
  23
  24/**
  25 * struct reset_control - a reset control
  26 * @rcdev: a pointer to the reset controller device
  27 *         this reset control belongs to
  28 * @id: ID of the reset controller in the reset
  29 *      controller device
  30 */
  31struct reset_control {
  32        struct reset_controller_dev *rcdev;
  33        struct device *dev;
  34        unsigned int id;
  35};
  36
  37/**
  38 * of_reset_simple_xlate - translate reset_spec to the reset line number
  39 * @rcdev: a pointer to the reset controller device
  40 * @reset_spec: reset line specifier as found in the device tree
  41 * @flags: a flags pointer to fill in (optional)
  42 *
  43 * This simple translation function should be used for reset controllers
  44 * with 1:1 mapping, where reset lines can be indexed by number without gaps.
  45 */
  46int of_reset_simple_xlate(struct reset_controller_dev *rcdev,
  47                          const struct of_phandle_args *reset_spec)
  48{
  49        if (WARN_ON(reset_spec->args_count != rcdev->of_reset_n_cells))
  50                return -EINVAL;
  51
  52        if (reset_spec->args[0] >= rcdev->nr_resets)
  53                return -EINVAL;
  54
  55        return reset_spec->args[0];
  56}
  57EXPORT_SYMBOL_GPL(of_reset_simple_xlate);
  58
  59/**
  60 * reset_controller_register - register a reset controller device
  61 * @rcdev: a pointer to the initialized reset controller device
  62 */
  63int reset_controller_register(struct reset_controller_dev *rcdev)
  64{
  65        if (!rcdev->of_xlate) {
  66                rcdev->of_reset_n_cells = 1;
  67                rcdev->of_xlate = of_reset_simple_xlate;
  68        }
  69
  70        mutex_lock(&reset_controller_list_mutex);
  71        list_add(&rcdev->list, &reset_controller_list);
  72        mutex_unlock(&reset_controller_list_mutex);
  73
  74        return 0;
  75}
  76EXPORT_SYMBOL_GPL(reset_controller_register);
  77
  78/**
  79 * reset_controller_unregister - unregister a reset controller device
  80 * @rcdev: a pointer to the reset controller device
  81 */
  82void reset_controller_unregister(struct reset_controller_dev *rcdev)
  83{
  84        mutex_lock(&reset_controller_list_mutex);
  85        list_del(&rcdev->list);
  86        mutex_unlock(&reset_controller_list_mutex);
  87}
  88EXPORT_SYMBOL_GPL(reset_controller_unregister);
  89
  90/**
  91 * reset_control_reset - reset the controlled device
  92 * @rstc: reset controller
  93 */
  94int reset_control_reset(struct reset_control *rstc)
  95{
  96        if (rstc->rcdev->ops->reset)
  97                return rstc->rcdev->ops->reset(rstc->rcdev, rstc->id);
  98
  99        return -ENOSYS;
 100}
 101EXPORT_SYMBOL_GPL(reset_control_reset);
 102
 103/**
 104 * reset_control_assert - asserts the reset line
 105 * @rstc: reset controller
 106 */
 107int reset_control_assert(struct reset_control *rstc)
 108{
 109        if (rstc->rcdev->ops->assert)
 110                return rstc->rcdev->ops->assert(rstc->rcdev, rstc->id);
 111
 112        return -ENOSYS;
 113}
 114EXPORT_SYMBOL_GPL(reset_control_assert);
 115
 116/**
 117 * reset_control_deassert - deasserts the reset line
 118 * @rstc: reset controller
 119 */
 120int reset_control_deassert(struct reset_control *rstc)
 121{
 122        if (rstc->rcdev->ops->deassert)
 123                return rstc->rcdev->ops->deassert(rstc->rcdev, rstc->id);
 124
 125        return -ENOSYS;
 126}
 127EXPORT_SYMBOL_GPL(reset_control_deassert);
 128
 129/**
 130 * reset_control_get - Lookup and obtain a reference to a reset controller.
 131 * @dev: device to be reset by the controller
 132 * @id: reset line name
 133 *
 134 * Returns a struct reset_control or IS_ERR() condition containing errno.
 135 *
 136 * Use of id names is optional.
 137 */
 138struct reset_control *reset_control_get(struct device *dev, const char *id)
 139{
 140        struct reset_control *rstc = ERR_PTR(-EPROBE_DEFER);
 141        struct reset_controller_dev *r, *rcdev;
 142        struct of_phandle_args args;
 143        int index = 0;
 144        int rstc_id;
 145        int ret;
 146
 147        if (!dev)
 148                return ERR_PTR(-EINVAL);
 149
 150        if (id)
 151                index = of_property_match_string(dev->of_node,
 152                                                 "reset-names", id);
 153        ret = of_parse_phandle_with_args(dev->of_node, "resets", "#reset-cells",
 154                                         index, &args);
 155        if (ret)
 156                return ERR_PTR(ret);
 157
 158        mutex_lock(&reset_controller_list_mutex);
 159        rcdev = NULL;
 160        list_for_each_entry(r, &reset_controller_list, list) {
 161                if (args.np == r->of_node) {
 162                        rcdev = r;
 163                        break;
 164                }
 165        }
 166        of_node_put(args.np);
 167
 168        if (!rcdev) {
 169                mutex_unlock(&reset_controller_list_mutex);
 170                return ERR_PTR(-ENODEV);
 171        }
 172
 173        rstc_id = rcdev->of_xlate(rcdev, &args);
 174        if (rstc_id < 0) {
 175                mutex_unlock(&reset_controller_list_mutex);
 176                return ERR_PTR(rstc_id);
 177        }
 178
 179        try_module_get(rcdev->owner);
 180        mutex_unlock(&reset_controller_list_mutex);
 181
 182        rstc = kzalloc(sizeof(*rstc), GFP_KERNEL);
 183        if (!rstc) {
 184                module_put(rcdev->owner);
 185                return ERR_PTR(-ENOMEM);
 186        }
 187
 188        rstc->dev = dev;
 189        rstc->rcdev = rcdev;
 190        rstc->id = rstc_id;
 191
 192        return rstc;
 193}
 194EXPORT_SYMBOL_GPL(reset_control_get);
 195
 196/**
 197 * reset_control_put - free the reset controller
 198 * @rstc: reset controller
 199 */
 200
 201void reset_control_put(struct reset_control *rstc)
 202{
 203        if (IS_ERR(rstc))
 204                return;
 205
 206        module_put(rstc->rcdev->owner);
 207        kfree(rstc);
 208}
 209EXPORT_SYMBOL_GPL(reset_control_put);
 210
 211static void devm_reset_control_release(struct device *dev, void *res)
 212{
 213        reset_control_put(*(struct reset_control **)res);
 214}
 215
 216/**
 131 * @dev: device to be reset by the controller2 132 * @id: reset line name2comment"> * @id: reset line name2 Managcdev: a #L202lassgs=(a. Flation function shoue="L204ransrom * @id: reset line name2 ent"> * , 197 * Use of id names is optional.2 Seeev: a #L202lassgs=(anslatm * Use of id names is optional.2 199 */
reset_control_put(struct reset/core.c#L202lassgs=+code=devm_reset_control_release" get" class="sref">reset_control_get(struct device *dev, const char *id)2 212{
reset_control_put(*(struct reset_control_ptcdev->reset_control *res);
 1282->rstc = r" class="sref">EXPOR1static void kzaptcdev->"#reset-cells",2 184rstc), GFP_KERNEL);2 183ptcdev->GFP_KERNEL);2 185                return ERR_PTR(-ENOMEM);2  732 182        of_parse_phandle_with_args(id);2  653        if (IS_ERR(rstc) {2kzaptcdev->res);
  67        of_parse_phandle_with_args(res);
rstc) {2 169        of_parse_phandptcdev->res);
 214}
 1912 192        return rstc;2 193}2 194EXPORset/core.c#L202lassgs=+code=devm_reset_control_release" get" cl)+code=res" class="sref">res);
 215
 14t_control_release" tring+code=devm_reset_control_release" tringsref">devm_reset_control_release(struct device *dev, void *device *GFP_KERNEL);2rstc) {2reset_control_put(*(struct reset_control_> 182        res);
 10WARN_ONev, void * 183        if (!reset_control *GFP_KERNEL);2index = 0;2reset_control_> 182        device *index = 0;2 193}2  732 216/**
 t_control_release" lass="mmeource managcdev: a #L202lasslas()216/**
 198/**
}
trol_2 href="drivers/r" 6/**
 Deef">rateLookup and obtai ef">rated " cldeset/core.c#L202lassgs=()"> * Use of id names is optional.2 This ent"> *  " ll not need class=cacontrnlam=lly, as "srL12 " ll tak7" 6/**
 car6 207ol ont"> * ource"> * Use of id names is optional.2  81 */2reset_control_put(struct reset_control *rstc)
  83{2 145        int ret;2 215
 153        module_put("reset-nam1static void  215
 169rcdev, res);
 153        rstc)
 169, void * 153        res);
 100}2 101EXPORt_control_release" las+code=devm_reset_control_release" put" clc+code=res" class="sref">res);
 1722 103/**2 ref="+ntrol_s="cind>ol_reset - reset the controlled device2                "> *perslam>2 131 * @dev: device to be reset by the controller2}
trol_2 href="drivers/r" 6/**
 Conveni_get wrappernslatv: a #L202lassgs=(anandev: a #L202lassv: a ()"> * Use of id names is optional.2 This is usefulnslat> * @dmmeseta36>>2 t/cos"> * Use of id names is optional.2  81 */2 14t_c="+ntrol_/a>(struct devm_reset_control_release(struct device *rstc)
  83{2reset_control_put(struct reset_control *res);
 145        int ret;2 1462 182        of_parse_phandle_with_args(rcdev = res);
        if (IS_ERR(rstc))
 192        if ( code=IS_ERR" class="sref">IS_ERR(res);
 210
 153        IS_ERR(res);
 1722 213        IS_ERR(res);
 1242 145        int ret;2 126}2 127EXPORt_c="+ntrol_/a>(struct res);



l/div>


T * original LXR softwar6 * ode=res" http:// ourceslage.n">rprojects/lxr">LXR nameunityref=">span experie="Lal ss=" *  nt"ode=res" mailto:lxr@t/cux.no">lxr@t/cux.noref=.
l/div>

lxr.t/cux.no kindly hosted nt"ode=res" http://www.redpill-t/cpro.no">Redpill L/cpro ASref=">providern/a>L/cux+codeultol o"> * per="coms serspans solet 1995.
l/div>