Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 6a84aaf | 2015-06-23 15:38:43 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2015 Google, Inc |
| 4 | * Written by Simon Glass <sjg@chromium.org> |
Simon Glass | 6a84aaf | 2015-06-23 15:38:43 -0600 | [diff] [blame] | 5 | */ |
| 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 | |
| 17 | struct regmap *syscon_get_regmap(struct udevice *dev) |
| 18 | { |
Simon Glass | ead6497 | 2015-07-06 12:54:38 -0600 | [diff] [blame] | 19 | struct syscon_uc_info *priv; |
Simon Glass | 6a84aaf | 2015-06-23 15:38:43 -0600 | [diff] [blame] | 20 | |
Simon Glass | ead6497 | 2015-07-06 12:54:38 -0600 | [diff] [blame] | 21 | if (device_get_uclass_id(dev) != UCLASS_SYSCON) |
| 22 | return ERR_PTR(-ENOEXEC); |
| 23 | priv = dev_get_uclass_priv(dev); |
Simon Glass | 6a84aaf | 2015-06-23 15:38:43 -0600 | [diff] [blame] | 24 | return priv->regmap; |
| 25 | } |
| 26 | |
| 27 | static int syscon_pre_probe(struct udevice *dev) |
| 28 | { |
| 29 | struct syscon_uc_info *priv = dev_get_uclass_priv(dev); |
| 30 | |
Simon Glass | 73dd069 | 2016-07-04 11:58:00 -0600 | [diff] [blame] | 31 | /* |
| 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 |
Masahiro Yamada | e4873e3 | 2018-04-19 12:14:03 +0900 | [diff] [blame^] | 43 | return regmap_init_mem(dev_ofnode(dev), &priv->regmap); |
Simon Glass | 73dd069 | 2016-07-04 11:58:00 -0600 | [diff] [blame] | 44 | #endif |
Simon Glass | 6a84aaf | 2015-06-23 15:38:43 -0600 | [diff] [blame] | 45 | } |
| 46 | |
Simon Glass | 6d5579c | 2016-01-17 16:11:08 -0700 | [diff] [blame] | 47 | int syscon_get_by_driver_data(ulong driver_data, struct udevice **devp) |
Simon Glass | 6a84aaf | 2015-06-23 15:38:43 -0600 | [diff] [blame] | 48 | { |
| 49 | struct udevice *dev; |
| 50 | struct uclass *uc; |
| 51 | int ret; |
| 52 | |
Simon Glass | 36ab8e3 | 2016-03-11 22:06:49 -0700 | [diff] [blame] | 53 | *devp = NULL; |
Simon Glass | 6a84aaf | 2015-06-23 15:38:43 -0600 | [diff] [blame] | 54 | ret = uclass_get(UCLASS_SYSCON, &uc); |
| 55 | if (ret) |
Simon Glass | 6d5579c | 2016-01-17 16:11:08 -0700 | [diff] [blame] | 56 | return ret; |
Simon Glass | 6a84aaf | 2015-06-23 15:38:43 -0600 | [diff] [blame] | 57 | uclass_foreach_dev(dev, uc) { |
| 58 | if (dev->driver_data == driver_data) { |
Simon Glass | 6d5579c | 2016-01-17 16:11:08 -0700 | [diff] [blame] | 59 | *devp = dev; |
| 60 | return device_probe(dev); |
Simon Glass | 6a84aaf | 2015-06-23 15:38:43 -0600 | [diff] [blame] | 61 | } |
| 62 | } |
| 63 | |
Simon Glass | 6d5579c | 2016-01-17 16:11:08 -0700 | [diff] [blame] | 64 | return -ENODEV; |
| 65 | } |
| 66 | |
| 67 | struct 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 Glass | 6a84aaf | 2015-06-23 15:38:43 -0600 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | void *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 | |
| 91 | UCLASS_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 Burton | 8c946a7 | 2016-09-08 07:47:37 +0100 | [diff] [blame] | 97 | |
| 98 | static const struct udevice_id generic_syscon_ids[] = { |
| 99 | { .compatible = "syscon" }, |
| 100 | { } |
| 101 | }; |
| 102 | |
| 103 | U_BOOT_DRIVER(generic_syscon) = { |
| 104 | .name = "syscon", |
| 105 | .id = UCLASS_SYSCON, |
Simon Glass | 8061bc6 | 2017-07-29 11:34:52 -0600 | [diff] [blame] | 106 | #if !CONFIG_IS_ENABLED(OF_PLATDATA) |
| 107 | .bind = dm_scan_fdt_dev, |
| 108 | #endif |
Paul Burton | 8c946a7 | 2016-09-08 07:47:37 +0100 | [diff] [blame] | 109 | .of_match = generic_syscon_ids, |
| 110 | }; |