linux/drivers/mfd/syscon.c
<<
>>
Prefs
   1/*
   2 * System Control Driver
   3 *
   4 * Copyright (C) 2012 Freescale Semiconductor, Inc.
   5 * Copyright (C) 2012 Linaro Ltd.
   6 *
   7 * Author: Dong Aisheng <dong.aisheng@linaro.org>
   8 *
   9 * This program is free software; you can redistribute it and/or modify
  10 * it under the terms of the GNU General Public License as published by
  11 * the Free Software Foundation; either version 2 of the License, or
  12 * (at your option) any later version.
  13 */
  14
  15#include <linux/err.h>
  16#include <linux/io.h>
  17#include <linux/module.h>
  18#include <linux/list.h>
  19#include <linux/of.h>
  20#include <linux/of_address.h>
  21#include <linux/of_platform.h>
  22#include <linux/platform_data/syscon.h>
  23#include <linux/platform_device.h>
  24#include <linux/regmap.h>
  25#include <linux/mfd/syscon.h>
  26#include <linux/slab.h>
  27
  28static struct platform_driver syscon_driver;
  29
  30static DEFINE_SPINLOCK(syscon_list_slock);
  31static LIST_HEAD(syscon_list);
  32
  33struct syscon {
  34        struct device_node *np;
  35        struct regmap *regmap;
  36        struct list_head list;
  37};
  38
  39static struct regmap_config syscon_regmap_config = {
  40        .reg_bits = 32,
  41        .val_bits = 32,
  42        .reg_stride = 4,
  43};
  44
  45static struct syscon *of_syscon_register(struct device_node *np)
  46{
  47        struct syscon *syscon;
  48        struct regmap *regmap;
  49        void __iomem *base;
  50        int ret;
  51        struct regmap_config syscon_config = syscon_regmap_config;
  52
  53        if (!of_device_is_compatible(np, "syscon"))
  54                return ERR_PTR(-EINVAL);
  55
  56        syscon = kzalloc(sizeof(*syscon), GFP_KERNEL);
  57        if (!syscon)
  58                return ERR_PTR(-ENOMEM);
  59
  60        base = of_iomap(np, 0);
  61        if (!base) {
  62                ret = -ENOMEM;
  63                goto err_map;
  64        }
  65
  66        /* Parse the device's DT node for an endianness specification */
  67        if (of_property_read_bool(np, "big-endian"))
  68                syscon_config.val_format_endian = REGMAP_ENDIAN_BIG;
  69         else if (of_property_read_bool(np, "little-endian"))
  70                syscon_config.val_format_endian = REGMAP_ENDIAN_LITTLE;
  71
  72        regmap = regmap_init_mmio(NULL, base, &syscon_config);
  73        if (IS_ERR(regmap)) {
  74                pr_err("regmap init failed\n");
  75                ret = PTR_ERR(regmap);
  76                goto err_regmap;
  77        }
  78
  79        syscon->regmap = regmap;
  80        syscon->np = np;
  81
  82        spin_lock(&syscon_list_slock);
  83        list_add_tail(&syscon->list, &syscon_list);
  84        spin_unlock(&syscon_list_slock);
  85
  86        return syscon;
  87
  88err_regmap:
  89        iounmap(base);
  90err_map:
  91        kfree(syscon);
  92        return ERR_PTR(ret);
  93}
  94
  95struct regmap *syscon_node_to_regmap(struct device_node *np)
  96{
  97        struct syscon *entry, *syscon = NULL;
  98
  99        spin_lock(&syscon_list_slock);
 100
 101        list_for_each_entry(entry, &syscon_list, list)
 102                if (entry->np == np) {
 103                        syscon = entry;
 104                        break;
 105                }
 106
 107        spin_unlock(&syscon_list_slock);
 108
 109        if (!syscon)
 110                syscon = of_syscon_register(np);
 111
 112        if (IS_ERR(syscon))
 113                return ERR_CAST(syscon);
 114
 115        return syscon->regmap;
 116}
 117EXPORT_SYMBOL_GPL(syscon_node_to_regmap);
 118
 119struct regmap *syscon_regmap_lookup_by_compatible(const char *s)
 120{
 121        struct device_node *syscon_np;
 122        struct regmap *regmap;
 123
 124        syscon_np = of_find_compatible_node(NULL, NULL, s);
 125        if (!syscon_np)
 126                return ERR_PTR(-ENODEV);
 127
 128        regmap = syscon_node_to_regmap(syscon_np);
 129        of_node_put(syscon_np);
 130
 131        return regmap;
 132}
 133EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_compatible);
 134
 135static int syscon_match_pdevname(struct device *dev, void *data)
 136{
 137        return !strcmp(dev_name(dev), (const char *)data);
 138}
 139
 140struct regmap *syscon_regmap_lookup_by_pdevname(const char *s)
 141{
 142        struct device *dev;
 143        struct syscon *syscon;
 144
 145        dev = driver_find_device(&syscon_driver.driver, NULL, (void *)s,
 146                                 syscon_match_pdevname);
 147        if (!dev)
 148                return ERR_PTR(-EPROBE_DEFER);
 149
 150        syscon = dev_get_drvdata(dev);
 151
 152        return syscon->regmap;
 153}
 154EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_pdevname);
 155
 156struct regmap *syscon_regmap_lookup_by_phandle(struct device_node *np,
 157                                        const char *property)
 158{
 159        struct device_node *syscon_np;
 160        struct regmap *regmap;
 161
 162        if (property)
 163                syscon_np = of_parse_phandle(np, property, 0);
 164        else
 165                syscon_np = np;
 166
 167        if (!syscon_np)
 168                return ERR_PTR(-ENODEV);
 169
 170        regmap = syscon_node_to_regmap(syscon_np);
 171        of_node_put(syscon_np);
 172
 173        return regmap;
 174}
 175EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_phandle);
 176
 177static int syscon_probe(struct platform_device *pdev)
 178{
 179        struct device *dev = &pdev->dev;
 180        struct syscon_platform_data *pdata = dev_get_platdata(dev);
 181        struct syscon *syscon;
 182        struct resource *res;
 183        void __iomem *base;
 184
 185        syscon = devm_kzalloc(dev, sizeof(*syscon), GFP_KERNEL);
 186        if (!syscon)
 187                return -ENOMEM;
 188
 189        res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
 190        if (!res)
 191                return -ENOENT;
 192
 193        base = devm_ioremap(dev, res->start, resource_size(res));
 194        if (!base)
 195                return -ENOMEM;
 196
 197        syscon_regmap_config.max_register = res->end - res->start - 3;
 198        if (pdata)
 199                syscon_regmap_config.name = pdata->label;
 200        syscon->regmap = devm_regmap_init_mmio(dev, base,
 201                                        &syscon_regmap_config);
 202        if (IS_ERR(syscon->regmap)) {
 203                dev_err(dev, "regmap init failed\n");
 204                return PTR_ERR(syscon->regmap);
 205        }
 206
 207        platform_set_drvdata(pdev, syscon);
 208
 209        dev_dbg(dev, "regmap %pR registered\n", res);
 210
 211        return 0;
 212}
 213
 214static const struct platform_device_id syscon_ids[] = {
 215        { "syscon", },
 216        { }
 217};
 218
 219static struct platform_driver syscon_driver = {
 220        .driver = {
 221                .name = "syscon",
 222        },
 223        .probe          = syscon_probe,
 224        .id_table       = syscon_ids,
 225};
 226
 227static int __init syscon_init(void)
 228{
 229        return platform_driver_register(&syscon_driver);
 230}
 231postcore_initcall(syscon_init);
 232
 233static void __exit syscon_exit(void)
 234{
 235        platform_driver_unregister(&syscon_driver);
 236}
 237module_exit(syscon_exit);
 238
 239MODULE_AUTHOR("Dong Aisheng <dong.aisheng@linaro.org>");
 240MODULE_DESCRIPTION("System Control driver");
 241MODULE_LICENSE("GPL v2");
 242