blob: 01bd9683a7a757ccf20a9a769c83ee1555350cd6 [file] [log] [blame]
Simon Glass6a84aaf2015-06-23 15:38:43 -06001/*
2 * Copyright (C) 2015 Google, Inc
3 * Written by Simon Glass <sjg@chromium.org>
4 *
5 * SPDX-License-Identifier: GPL-2.0+
6 */
7
8#include <common.h>
9#include <syscon.h>
10#include <dm.h>
11#include <errno.h>
12#include <regmap.h>
13#include <dm/device-internal.h>
14#include <dm/lists.h>
15#include <dm/root.h>
16#include <linux/err.h>
17
18struct regmap *syscon_get_regmap(struct udevice *dev)
19{
Simon Glassead64972015-07-06 12:54:38 -060020 struct syscon_uc_info *priv;
Simon Glass6a84aaf2015-06-23 15:38:43 -060021
Simon Glassead64972015-07-06 12:54:38 -060022 if (device_get_uclass_id(dev) != UCLASS_SYSCON)
23 return ERR_PTR(-ENOEXEC);
24 priv = dev_get_uclass_priv(dev);
Simon Glass6a84aaf2015-06-23 15:38:43 -060025 return priv->regmap;
26}
27
28static int syscon_pre_probe(struct udevice *dev)
29{
30 struct syscon_uc_info *priv = dev_get_uclass_priv(dev);
31
Simon Glass73dd0692016-07-04 11:58:00 -060032 /*
33 * With OF_PLATDATA we really have no way of knowing the format of
34 * the device-specific platform data. So we assume that it starts with
35 * a 'reg' member, and this holds a single address and size. Drivers
36 * using OF_PLATDATA will need to ensure that this is true.
37 */
38#if CONFIG_IS_ENABLED(OF_PLATDATA)
39 struct syscon_base_platdata *plat = dev_get_platdata(dev);
40
41 return regmap_init_mem_platdata(dev, plat->reg, ARRAY_SIZE(plat->reg),
42 &priv->regmap);
43#else
Simon Glass6a84aaf2015-06-23 15:38:43 -060044 return regmap_init_mem(dev, &priv->regmap);
Simon Glass73dd0692016-07-04 11:58:00 -060045#endif
Simon Glass6a84aaf2015-06-23 15:38:43 -060046}
47
Simon Glass6d5579c2016-01-17 16:11:08 -070048int syscon_get_by_driver_data(ulong driver_data, struct udevice **devp)
Simon Glass6a84aaf2015-06-23 15:38:43 -060049{
50 struct udevice *dev;
51 struct uclass *uc;
52 int ret;
53
Simon Glass36ab8e32016-03-11 22:06:49 -070054 *devp = NULL;
Simon Glass6a84aaf2015-06-23 15:38:43 -060055 ret = uclass_get(UCLASS_SYSCON, &uc);
56 if (ret)
Simon Glass6d5579c2016-01-17 16:11:08 -070057 return ret;
Simon Glass6a84aaf2015-06-23 15:38:43 -060058 uclass_foreach_dev(dev, uc) {
59 if (dev->driver_data == driver_data) {
Simon Glass6d5579c2016-01-17 16:11:08 -070060 *devp = dev;
61 return device_probe(dev);
Simon Glass6a84aaf2015-06-23 15:38:43 -060062 }
63 }
64
Simon Glass6d5579c2016-01-17 16:11:08 -070065 return -ENODEV;
66}
67
68struct regmap *syscon_get_regmap_by_driver_data(ulong driver_data)
69{
70 struct syscon_uc_info *priv;
71 struct udevice *dev;
72 int ret;
73
74 ret = syscon_get_by_driver_data(driver_data, &dev);
75 if (ret)
76 return ERR_PTR(ret);
77 priv = dev_get_uclass_priv(dev);
78
79 return priv->regmap;
Simon Glass6a84aaf2015-06-23 15:38:43 -060080}
81
82void *syscon_get_first_range(ulong driver_data)
83{
84 struct regmap *map;
85
86 map = syscon_get_regmap_by_driver_data(driver_data);
87 if (IS_ERR(map))
88 return map;
89 return regmap_get_range(map, 0);
90}
91
92UCLASS_DRIVER(syscon) = {
93 .id = UCLASS_SYSCON,
94 .name = "syscon",
95 .per_device_auto_alloc_size = sizeof(struct syscon_uc_info),
96 .pre_probe = syscon_pre_probe,
97};