1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * Copyright (C) 2015 Google, Inc 4 * Written by Simon Glass <sjg@chromium.org> 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 /* 18 * Caution: 19 * This API requires the given device has alerady been bound to syscon driver. 20 * For example, 21 * compatible = "syscon", "simple-mfd"; 22 * works, but 23 * compatible = "simple-mfd", "syscon"; 24 * does not. The behavior is different from Linux. 25 */ 26 struct regmap *syscon_get_regmap(struct udevice *dev) 27 { 28 struct syscon_uc_info *priv; 29 30 if (device_get_uclass_id(dev) != UCLASS_SYSCON) 31 return ERR_PTR(-ENOEXEC); 32 priv = dev_get_uclass_priv(dev); 33 return priv->regmap; 34 } 35 36 static int syscon_pre_probe(struct udevice *dev) 37 { 38 struct syscon_uc_info *priv = dev_get_uclass_priv(dev); 39 40 /* Special case for PCI devices, which don't have a regmap */ 41 if (device_get_uclass_id(dev->parent) == UCLASS_PCI) 42 return 0; 43 44 /* 45 * With OF_PLATDATA we really have no way of knowing the format of 46 * the device-specific platform data. So we assume that it starts with 47 * a 'reg' member, and this holds a single address and size. Drivers 48 * using OF_PLATDATA will need to ensure that this is true. 49 */ 50 #if CONFIG_IS_ENABLED(OF_PLATDATA) 51 struct syscon_base_platdata *plat = dev_get_platdata(dev); 52 53 return regmap_init_mem_platdata(dev, plat->reg, ARRAY_SIZE(plat->reg), 54 &priv->regmap); 55 #else 56 return regmap_init_mem(dev_ofnode(dev), &priv->regmap); 57 #endif 58 } 59 60 struct regmap *syscon_regmap_lookup_by_phandle(struct udevice *dev, 61 const char *name) 62 { 63 struct udevice *syscon; 64 struct regmap *r; 65 int err; 66 67 err = uclass_get_device_by_phandle(UCLASS_SYSCON, dev, 68 name, &syscon); 69 if (err) { 70 dev_dbg(dev, "unable to find syscon device\n"); 71 return ERR_PTR(err); 72 } 73 74 r = syscon_get_regmap(syscon); 75 if (!r) { 76 dev_dbg(dev, "unable to find regmap\n"); 77 return ERR_PTR(-ENODEV); 78 } 79 80 return r; 81 } 82 83 int syscon_get_by_driver_data(ulong driver_data, struct udevice **devp) 84 { 85 struct udevice *dev; 86 struct uclass *uc; 87 int ret; 88 89 *devp = NULL; 90 ret = uclass_get(UCLASS_SYSCON, &uc); 91 if (ret) 92 return ret; 93 uclass_foreach_dev(dev, uc) { 94 if (dev->driver_data == driver_data) { 95 *devp = dev; 96 return device_probe(dev); 97 } 98 } 99 100 return -ENODEV; 101 } 102 103 struct regmap *syscon_get_regmap_by_driver_data(ulong driver_data) 104 { 105 struct syscon_uc_info *priv; 106 struct udevice *dev; 107 int ret; 108 109 ret = syscon_get_by_driver_data(driver_data, &dev); 110 if (ret) 111 return ERR_PTR(ret); 112 priv = dev_get_uclass_priv(dev); 113 114 return priv->regmap; 115 } 116 117 void *syscon_get_first_range(ulong driver_data) 118 { 119 struct regmap *map; 120 121 map = syscon_get_regmap_by_driver_data(driver_data); 122 if (IS_ERR(map)) 123 return map; 124 return regmap_get_range(map, 0); 125 } 126 127 UCLASS_DRIVER(syscon) = { 128 .id = UCLASS_SYSCON, 129 .name = "syscon", 130 .per_device_auto_alloc_size = sizeof(struct syscon_uc_info), 131 .pre_probe = syscon_pre_probe, 132 }; 133 134 static const struct udevice_id generic_syscon_ids[] = { 135 { .compatible = "syscon" }, 136 { } 137 }; 138 139 U_BOOT_DRIVER(generic_syscon) = { 140 .name = "syscon", 141 .id = UCLASS_SYSCON, 142 #if !CONFIG_IS_ENABLED(OF_PLATDATA) 143 .bind = dm_scan_fdt_dev, 144 #endif 145 .of_match = generic_syscon_ids, 146 }; 147 148 /* 149 * Linux-compatible syscon-to-regmap 150 * The syscon node can be bound to another driver, but still works 151 * as a syscon provider. 152 */ 153 struct regmap *syscon_node_to_regmap(ofnode node) 154 { 155 struct udevice *dev, *parent; 156 int ret; 157 158 if (!uclass_get_device_by_ofnode(UCLASS_SYSCON, node, &dev)) 159 return syscon_get_regmap(dev); 160 161 if (!ofnode_device_is_compatible(node, "syscon")) 162 return ERR_PTR(-EINVAL); 163 164 /* bound to driver with same ofnode or to root if not found */ 165 if (device_find_global_by_ofnode(node, &parent)) 166 parent = dm_root(); 167 168 /* force bound to syscon class */ 169 ret = device_bind_driver_to_node(parent, "syscon", 170 ofnode_get_name(node), 171 node, &dev); 172 if (ret) 173 return ERR_PTR(ret); 174 175 ret = device_probe(dev); 176 if (ret) 177 return ERR_PTR(ret); 178 179 return syscon_get_regmap(dev); 180 } 181