blob: 96408dea7e699434fc61de6fa564f2ec747573bc [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass6a84aaf2015-06-23 15:38:43 -06002/*
3 * Copyright (C) 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
Simon Glass6a84aaf2015-06-23 15:38:43 -06005 */
6
7#include <common.h>
8#include <syscon.h>
9#include <dm.h>
10#include <errno.h>
11#include <regmap.h>
12#include <dm/device-internal.h>
13#include <dm/lists.h>
14#include <dm/root.h>
15#include <linux/err.h>
16
17struct regmap *syscon_get_regmap(struct udevice *dev)
18{
Simon Glassead64972015-07-06 12:54:38 -060019 struct syscon_uc_info *priv;
Simon Glass6a84aaf2015-06-23 15:38:43 -060020
Simon Glassead64972015-07-06 12:54:38 -060021 if (device_get_uclass_id(dev) != UCLASS_SYSCON)
22 return ERR_PTR(-ENOEXEC);
23 priv = dev_get_uclass_priv(dev);
Simon Glass6a84aaf2015-06-23 15:38:43 -060024 return priv->regmap;
25}
26
27static int syscon_pre_probe(struct udevice *dev)
28{
29 struct syscon_uc_info *priv = dev_get_uclass_priv(dev);
30
Simon Glass73dd0692016-07-04 11:58:00 -060031 /*
32 * With OF_PLATDATA we really have no way of knowing the format of
33 * the device-specific platform data. So we assume that it starts with
34 * a 'reg' member, and this holds a single address and size. Drivers
35 * using OF_PLATDATA will need to ensure that this is true.
36 */
37#if CONFIG_IS_ENABLED(OF_PLATDATA)
38 struct syscon_base_platdata *plat = dev_get_platdata(dev);
39
40 return regmap_init_mem_platdata(dev, plat->reg, ARRAY_SIZE(plat->reg),
41 &priv->regmap);
42#else
Simon Glass6a84aaf2015-06-23 15:38:43 -060043 return regmap_init_mem(dev, &priv->regmap);
Simon Glass73dd0692016-07-04 11:58:00 -060044#endif
Simon Glass6a84aaf2015-06-23 15:38:43 -060045}
46
Simon Glass6d5579c2016-01-17 16:11:08 -070047int syscon_get_by_driver_data(ulong driver_data, struct udevice **devp)
Simon Glass6a84aaf2015-06-23 15:38:43 -060048{
49 struct udevice *dev;
50 struct uclass *uc;
51 int ret;
52
Simon Glass36ab8e32016-03-11 22:06:49 -070053 *devp = NULL;
Simon Glass6a84aaf2015-06-23 15:38:43 -060054 ret = uclass_get(UCLASS_SYSCON, &uc);
55 if (ret)
Simon Glass6d5579c2016-01-17 16:11:08 -070056 return ret;
Simon Glass6a84aaf2015-06-23 15:38:43 -060057 uclass_foreach_dev(dev, uc) {
58 if (dev->driver_data == driver_data) {
Simon Glass6d5579c2016-01-17 16:11:08 -070059 *devp = dev;
60 return device_probe(dev);
Simon Glass6a84aaf2015-06-23 15:38:43 -060061 }
62 }
63
Simon Glass6d5579c2016-01-17 16:11:08 -070064 return -ENODEV;
65}
66
67struct regmap *syscon_get_regmap_by_driver_data(ulong driver_data)
68{
69 struct syscon_uc_info *priv;
70 struct udevice *dev;
71 int ret;
72
73 ret = syscon_get_by_driver_data(driver_data, &dev);
74 if (ret)
75 return ERR_PTR(ret);
76 priv = dev_get_uclass_priv(dev);
77
78 return priv->regmap;
Simon Glass6a84aaf2015-06-23 15:38:43 -060079}
80
81void *syscon_get_first_range(ulong driver_data)
82{
83 struct regmap *map;
84
85 map = syscon_get_regmap_by_driver_data(driver_data);
86 if (IS_ERR(map))
87 return map;
88 return regmap_get_range(map, 0);
89}
90
91UCLASS_DRIVER(syscon) = {
92 .id = UCLASS_SYSCON,
93 .name = "syscon",
94 .per_device_auto_alloc_size = sizeof(struct syscon_uc_info),
95 .pre_probe = syscon_pre_probe,
96};
Paul Burton8c946a72016-09-08 07:47:37 +010097
98static const struct udevice_id generic_syscon_ids[] = {
99 { .compatible = "syscon" },
100 { }
101};
102
103U_BOOT_DRIVER(generic_syscon) = {
104 .name = "syscon",
105 .id = UCLASS_SYSCON,
Simon Glass8061bc62017-07-29 11:34:52 -0600106#if !CONFIG_IS_ENABLED(OF_PLATDATA)
107 .bind = dm_scan_fdt_dev,
108#endif
Paul Burton8c946a72016-09-08 07:47:37 +0100109 .of_match = generic_syscon_ids,
110};