Simon Glass | 6a84aaf | 2015-06-23 15:38:43 -0600 | [diff] [blame] | 1 | /* |
| 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 | |
| 18 | struct regmap *syscon_get_regmap(struct udevice *dev) |
| 19 | { |
Simon Glass | ead6497 | 2015-07-06 12:54:38 -0600 | [diff] [blame] | 20 | struct syscon_uc_info *priv; |
Simon Glass | 6a84aaf | 2015-06-23 15:38:43 -0600 | [diff] [blame] | 21 | |
Simon Glass | ead6497 | 2015-07-06 12:54:38 -0600 | [diff] [blame] | 22 | if (device_get_uclass_id(dev) != UCLASS_SYSCON) |
| 23 | return ERR_PTR(-ENOEXEC); |
| 24 | priv = dev_get_uclass_priv(dev); |
Simon Glass | 6a84aaf | 2015-06-23 15:38:43 -0600 | [diff] [blame] | 25 | return priv->regmap; |
| 26 | } |
| 27 | |
| 28 | static int syscon_pre_probe(struct udevice *dev) |
| 29 | { |
| 30 | struct syscon_uc_info *priv = dev_get_uclass_priv(dev); |
| 31 | |
Simon Glass | 73dd069 | 2016-07-04 11:58:00 -0600 | [diff] [blame^] | 32 | /* |
| 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 Glass | 6a84aaf | 2015-06-23 15:38:43 -0600 | [diff] [blame] | 44 | return regmap_init_mem(dev, &priv->regmap); |
Simon Glass | 73dd069 | 2016-07-04 11:58:00 -0600 | [diff] [blame^] | 45 | #endif |
Simon Glass | 6a84aaf | 2015-06-23 15:38:43 -0600 | [diff] [blame] | 46 | } |
| 47 | |
Simon Glass | 6d5579c | 2016-01-17 16:11:08 -0700 | [diff] [blame] | 48 | int syscon_get_by_driver_data(ulong driver_data, struct udevice **devp) |
Simon Glass | 6a84aaf | 2015-06-23 15:38:43 -0600 | [diff] [blame] | 49 | { |
| 50 | struct udevice *dev; |
| 51 | struct uclass *uc; |
| 52 | int ret; |
| 53 | |
Simon Glass | 36ab8e3 | 2016-03-11 22:06:49 -0700 | [diff] [blame] | 54 | *devp = NULL; |
Simon Glass | 6a84aaf | 2015-06-23 15:38:43 -0600 | [diff] [blame] | 55 | ret = uclass_get(UCLASS_SYSCON, &uc); |
| 56 | if (ret) |
Simon Glass | 6d5579c | 2016-01-17 16:11:08 -0700 | [diff] [blame] | 57 | return ret; |
Simon Glass | 6a84aaf | 2015-06-23 15:38:43 -0600 | [diff] [blame] | 58 | uclass_foreach_dev(dev, uc) { |
| 59 | if (dev->driver_data == driver_data) { |
Simon Glass | 6d5579c | 2016-01-17 16:11:08 -0700 | [diff] [blame] | 60 | *devp = dev; |
| 61 | return device_probe(dev); |
Simon Glass | 6a84aaf | 2015-06-23 15:38:43 -0600 | [diff] [blame] | 62 | } |
| 63 | } |
| 64 | |
Simon Glass | 6d5579c | 2016-01-17 16:11:08 -0700 | [diff] [blame] | 65 | return -ENODEV; |
| 66 | } |
| 67 | |
| 68 | struct 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 Glass | 6a84aaf | 2015-06-23 15:38:43 -0600 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | void *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 | |
| 92 | UCLASS_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 | }; |