regmap: Add devm_regmap_init()
authorJean-Jacques Hiblot <jjhiblot@ti.com>
Thu, 24 Sep 2020 04:34:10 +0000 (10:04 +0530)
committerTom Rini <trini@konsulko.com>
Wed, 30 Sep 2020 15:55:22 +0000 (11:55 -0400)
Most of new linux drivers are using managed-API to allocate resources. To
ease porting drivers from linux to U-Boot, introduce devm_regmap_init() as
a managed API to get a regmap from the device tree.

Signed-off-by: Jean-Jacques Hiblot <jjhiblot@ti.com>
Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Pratyush Yadav <p.yadav@ti.com>
drivers/core/regmap.c
include/regmap.h

index a67a237..7422536 100644 (file)
 #include <regmap.h>
 #include <asm/io.h>
 #include <dm/of_addr.h>
+#include <dm/devres.h>
 #include <linux/ioport.h>
+#include <linux/compat.h>
+#include <linux/err.h>
 
 DECLARE_GLOBAL_DATA_PTR;
 
@@ -228,6 +231,32 @@ err:
 
        return ret;
 }
+
+static void devm_regmap_release(struct udevice *dev, void *res)
+{
+       regmap_uninit(*(struct regmap **)res);
+}
+
+struct regmap *devm_regmap_init(struct udevice *dev,
+                               const struct regmap_bus *bus,
+                               void *bus_context,
+                               const struct regmap_config *config)
+{
+       int rc;
+       struct regmap **mapp;
+
+       mapp = devres_alloc(devm_regmap_release, sizeof(struct regmap *),
+                           __GFP_ZERO);
+       if (unlikely(!mapp))
+               return ERR_PTR(-ENOMEM);
+
+       rc = regmap_init_mem(dev_ofnode(dev), mapp);
+       if (rc)
+               return ERR_PTR(rc);
+
+       devres_add(dev, mapp);
+       return *mapp;
+}
 #endif
 
 void *regmap_get_range(struct regmap *map, unsigned int range_num)
index 30183c5..c7dd240 100644 (file)
@@ -75,6 +75,9 @@ struct regmap_range {
        ulong size;
 };
 
+struct regmap_bus;
+struct regmap_config;
+
 /**
  * struct regmap - a way of accessing hardware/bus registers
  *
@@ -336,6 +339,21 @@ int regmap_init_mem_platdata(struct udevice *dev, fdt_val_t *reg, int count,
 int regmap_init_mem_index(ofnode node, struct regmap **mapp, int index);
 
 /**
+ * devm_regmap_init() - Initialise register map (device managed)
+ *
+ * @dev: Device that will be interacted with
+ * @bus: Bus-specific callbacks to use with device (IGNORED)
+ * @bus_context: Data passed to bus-specific callbacks (IGNORED)
+ * @config: Configuration for register map (IGNORED)
+ *
+ * @Return a valid pointer to a struct regmap or a ERR_PTR() on error.
+ * The structure is automatically freed when the device is unbound
+ */
+struct regmap *devm_regmap_init(struct udevice *dev,
+                               const struct regmap_bus *bus,
+                               void *bus_context,
+                               const struct regmap_config *config);
+/**
  * regmap_get_range() - Obtain the base memory address of a regmap range
  *
  * @map:       Regmap to query