--- /dev/null
+/*
+ * Configfs entries for device-tree
+ *
+ * Copyright (C) 2013 - Pantelis Antoniou <panto@antoniou-consulting.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version
+ * 2 of the License, or (at your option) any later version.
+ */
+#include <linux/ctype.h>
+#include <linux/cpu.h>
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/of_fdt.h>
+#include <linux/spinlock.h>
+#include <linux/slab.h>
+#include <linux/proc_fs.h>
+#include <linux/configfs.h>
+#include <linux/types.h>
+#include <linux/stat.h>
+#include <linux/limits.h>
+#include <linux/file.h>
+#include <linux/vmalloc.h>
+#include <linux/firmware.h>
+#include <linux/sizes.h>
+
+#include "of_private.h"
+
+struct cfs_overlay_item {
+ struct config_item item;
+
+ char path[PATH_MAX];
+
+ const struct firmware *fw;
+ struct device_node *overlay;
+ int ov_id;
+
+ void *dtbo;
+ int dtbo_size;
+};
+
+static inline struct cfs_overlay_item *to_cfs_overlay_item(
+ struct config_item *item)
+{
+ return item ? container_of(item, struct cfs_overlay_item, item) : NULL;
+}
+
+static ssize_t cfs_overlay_item_path_show(struct config_item *item,
+ char *page)
+{
+ struct cfs_overlay_item *overlay = to_cfs_overlay_item(item);
+ return sprintf(page, "%s\n", overlay->path);
+}
+
+static ssize_t cfs_overlay_item_path_store(struct config_item *item,
+ const char *page, size_t count)
+{
+ struct cfs_overlay_item *overlay = to_cfs_overlay_item(item);
+ const char *p = page;
+ char *s;
+ int err;
+
+ /* if it's set do not allow changes */
+ if (overlay->path[0] != '\0' || overlay->dtbo_size > 0)
+ return -EPERM;
+
+ /* copy to path buffer (and make sure it's always zero terminated */
+ count = snprintf(overlay->path, sizeof(overlay->path) - 1, "%s", p);
+ overlay->path[sizeof(overlay->path) - 1] = '\0';
+
+ /* strip trailing newlines */
+ s = overlay->path + strlen(overlay->path);
+ while (s > overlay->path && *--s == '\n')
+ *s = '\0';
+
+ pr_debug("%s: path is '%s'\n", __func__, overlay->path);
+
+ err = request_firmware(&overlay->fw, overlay->path, NULL);
+ if (err != 0)
+ return err;
+
+ err = of_overlay_fdt_apply((void *)overlay->fw->data,
+ (u32)overlay->fw->size, &overlay->ov_id);
+ if (err != 0)
+ goto out_err;
+
+ return count;
+
+out_err:
+
+ release_firmware(overlay->fw);
+ overlay->fw = NULL;
+
+ overlay->path[0] = '\0';
+ return err;
+}
+
+static ssize_t cfs_overlay_item_status_show(struct config_item *item,
+ char *page)
+{
+ struct cfs_overlay_item *overlay = to_cfs_overlay_item(item);
+
+ return sprintf(page, "%s\n",
+ overlay->ov_id > 0 ? "applied" : "unapplied");
+}
+
+CONFIGFS_ATTR(cfs_overlay_item_, path);
+CONFIGFS_ATTR_RO(cfs_overlay_item_, status);
+
+static struct configfs_attribute *cfs_overlay_attrs[] = {
+ &cfs_overlay_item_attr_path,
+ &cfs_overlay_item_attr_status,
+ NULL,
+};
+
+ssize_t cfs_overlay_item_dtbo_read(struct config_item *item,
+ void *buf, size_t max_count)
+{
+ struct cfs_overlay_item *overlay = to_cfs_overlay_item(item);
+
+ pr_debug("%s: buf=%p max_count=%zu\n", __func__,
+ buf, max_count);
+
+ if (overlay->dtbo == NULL)
+ return 0;
+
+ /* copy if buffer provided */
+ if (buf != NULL) {
+ /* the buffer must be large enough */
+ if (overlay->dtbo_size > max_count)
+ return -ENOSPC;
+
+ memcpy(buf, overlay->dtbo, overlay->dtbo_size);
+ }
+
+ return overlay->dtbo_size;
+}
+
+ssize_t cfs_overlay_item_dtbo_write(struct config_item *item,
+ const void *buf, size_t count)
+{
+ struct cfs_overlay_item *overlay = to_cfs_overlay_item(item);
+ int err;
+
+ /* if it's set do not allow changes */
+ if (overlay->path[0] != '\0' || overlay->dtbo_size > 0)
+ return -EPERM;
+
+ /* copy the contents */
+ overlay->dtbo = kmemdup(buf, count, GFP_KERNEL);
+ if (overlay->dtbo == NULL)
+ return -ENOMEM;
+
+ overlay->dtbo_size = count;
+
+ err = of_overlay_fdt_apply(overlay->dtbo, overlay->dtbo_size,
+ &overlay->ov_id);
+ if (err != 0)
+ goto out_err;
+
+ return count;
+
+out_err:
+ kfree(overlay->dtbo);
+ overlay->dtbo = NULL;
+ overlay->dtbo_size = 0;
+ overlay->ov_id = 0;
+
+ return err;
+}
+
+CONFIGFS_BIN_ATTR(cfs_overlay_item_, dtbo, NULL, SZ_1M);
+
+static struct configfs_bin_attribute *cfs_overlay_bin_attrs[] = {
+ &cfs_overlay_item_attr_dtbo,
+ NULL,
+};
+
+static void cfs_overlay_release(struct config_item *item)
+{
+ struct cfs_overlay_item *overlay = to_cfs_overlay_item(item);
+
+ if (overlay->ov_id > 0)
+ of_overlay_remove(&overlay->ov_id);
+ if (overlay->fw)
+ release_firmware(overlay->fw);
+ /* kfree with NULL is safe */
+ kfree(overlay->dtbo);
+ kfree(overlay);
+}
+
+static struct configfs_item_operations cfs_overlay_item_ops = {
+ .release = cfs_overlay_release,
+};
+
+static struct config_item_type cfs_overlay_type = {
+ .ct_item_ops = &cfs_overlay_item_ops,
+ .ct_attrs = cfs_overlay_attrs,
+ .ct_bin_attrs = cfs_overlay_bin_attrs,
+ .ct_owner = THIS_MODULE,
+};
+
+static struct config_item *cfs_overlay_group_make_item(
+ struct config_group *group, const char *name)
+{
+ struct cfs_overlay_item *overlay;
+
+ overlay = kzalloc(sizeof(*overlay), GFP_KERNEL);
+ if (!overlay)
+ return ERR_PTR(-ENOMEM);
+
+ config_item_init_type_name(&overlay->item, name, &cfs_overlay_type);
+ return &overlay->item;
+}
+
+static void cfs_overlay_group_drop_item(struct config_group *group,
+ struct config_item *item)
+{
+ struct cfs_overlay_item *overlay = to_cfs_overlay_item(item);
+
+ config_item_put(&overlay->item);
+}
+
+static struct configfs_group_operations overlays_ops = {
+ .make_item = cfs_overlay_group_make_item,
+ .drop_item = cfs_overlay_group_drop_item,
+};
+
+static struct config_item_type overlays_type = {
+ .ct_group_ops = &overlays_ops,
+ .ct_owner = THIS_MODULE,
+};
+
+static struct configfs_group_operations of_cfs_ops = {
+ /* empty - we don't allow anything to be created */
+};
+
+static struct config_item_type of_cfs_type = {
+ .ct_group_ops = &of_cfs_ops,
+ .ct_owner = THIS_MODULE,
+};
+
+struct config_group of_cfs_overlay_group;
+
+static struct configfs_subsystem of_cfs_subsys = {
+ .su_group = {
+ .cg_item = {
+ .ci_namebuf = "device-tree",
+ .ci_type = &of_cfs_type,
+ },
+ },
+ .su_mutex = __MUTEX_INITIALIZER(of_cfs_subsys.su_mutex),
+};
+
+static int __init of_cfs_init(void)
+{
+ int ret;
+
+ pr_info("%s\n", __func__);
+
+ config_group_init(&of_cfs_subsys.su_group);
+ config_group_init_type_name(&of_cfs_overlay_group, "overlays",
+ &overlays_type);
+ configfs_add_default_group(&of_cfs_overlay_group,
+ &of_cfs_subsys.su_group);
+
+ ret = configfs_register_subsystem(&of_cfs_subsys);
+ if (ret != 0) {
+ pr_err("%s: failed to register subsys\n", __func__);
+ goto out;
+ }
+ pr_info("%s: OK\n", __func__);
+out:
+ return ret;
+}
+late_initcall(of_cfs_init);
#include <linux/reset.h>
#include "../core.h"
+#include "../pinctrl-utils.h"
#include "../pinconf.h"
#include "../pinmux.h"
#include "pinctrl-starfive.h"
static int starfive_dt_node_to_map(struct pinctrl_dev *pctldev,
struct device_node *np,
- struct pinctrl_map **map, unsigned int *num_maps)
+ struct pinctrl_map **maps, unsigned int *num_maps)
{
- struct starfive_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
- const struct group_desc *grp;
- struct pinctrl_map *new_map;
- struct device_node *parent;
- struct starfive_pin *pin;
- int map_num = 1;
- int i, j;
+ struct starfive_pinctrl *sfp = pinctrl_dev_get_drvdata(pctldev);
+ struct device *dev = sfp->gc.parent;
+ const struct starfive_pinctrl_soc_info *info = sfp->info;
+ struct starfive_pin *pin_data;
+ struct device_node *child;
+ struct pinctrl_map *map;
+ struct group_desc *grp;
+ const char **pgnames;
+ const char *grpname;
+ int ngroups;
+ int nmaps;
+ int ret;
+ int *pins_id;
+ int psize, pin_size;
+ int size = 0;
+ int offset = 0;
+ const __be32 *list;
+ int i, child_num_pins;
- grp = starfive_pinctrl_find_group_by_name(pctldev, np->name);
- if (!grp) {
- dev_err(pctl->dev, "unable to find group for node %pOFn\n", np);
+ nmaps = 0;
+ ngroups = 0;
+ pin_size = STARFIVE_PINS_SIZE;
+
+ for_each_child_of_node(np, child) {
+ list = of_get_property(child, "sf,pins", &psize);
+ if (!list) {
+ dev_err(sfp->dev,
+ "no sf,pins and pins property in node %pOF\n", np);
+ return -EINVAL;
+ }
+ size += psize;
+ }
+
+ if (!size || size % pin_size) {
+ dev_err(sfp->dev,
+ "Invalid sf,pins or pins property in node %pOF\n", np);
return -EINVAL;
}
- map_num = grp->num_pins + 1;
- new_map = kmalloc_array(map_num, sizeof(struct pinctrl_map),
- GFP_KERNEL);
- if (!new_map)
+ nmaps = size / pin_size;
+ ngroups = size / pin_size;
+
+ pgnames = devm_kcalloc(dev, ngroups, sizeof(*pgnames), GFP_KERNEL);
+ if (!pgnames)
return -ENOMEM;
- *map = new_map;
- *num_maps = map_num;
+ map = kcalloc(nmaps, sizeof(*map), GFP_KERNEL);
+ if (!map)
+ return -ENOMEM;
- parent = of_get_parent(np);
- if (!parent) {
- kfree(new_map);
- return -EINVAL;
+ grp = devm_kzalloc(sfp->dev, sizeof(struct group_desc),
+ GFP_KERNEL);
+ if (!grp) {
+ of_node_put(child);
+ return -ENOMEM;
}
- new_map[0].type = PIN_MAP_TYPE_MUX_GROUP;
- new_map[0].data.mux.function = parent->name;
- new_map[0].data.mux.group = np->name;
- of_node_put(parent);
- new_map++;
- for (i = j = 0; i < grp->num_pins; i++) {
- pin = &((struct starfive_pin *)(grp->data))[i];
+ grp->data = devm_kcalloc(sfp->dev,
+ ngroups, sizeof(struct starfive_pin),
+ GFP_KERNEL);
+ grp->pins = devm_kcalloc(sfp->dev,
+ ngroups, sizeof(int),
+ GFP_KERNEL);
+ if (!grp->pins || !grp->data)
+ return -ENOMEM;
+
+ nmaps = 0;
+ ngroups = 0;
+ mutex_lock(&sfp->mutex);
+
+ for_each_child_of_node(np, child) {
+ grpname = devm_kasprintf(dev, GFP_KERNEL, "%pOFn.%pOFn", np, child);
+ if (!grpname) {
+ ret = -ENOMEM;
+ goto put_child;
+ }
- new_map[j].type = PIN_MAP_TYPE_CONFIGS_PIN;
- new_map[j].data.configs.group_or_pin =
- pin_get_name(pctldev, pin->pin);
- new_map[j].data.configs.configs =
- &pin->pin_config.io_config;
- new_map[j].data.configs.num_configs = 1;
- j++;
+ pgnames[ngroups++] = grpname;
+
+ list = of_get_property(child, "sf,pins", &psize);
+ if (!list) {
+ dev_err(sfp->dev,
+ "no sf,pins and pins property in node %pOF\n", np);
+ goto put_child;
+ }
+ child_num_pins = psize / pin_size;
+ grp->name = grpname;
+ grp->num_pins = child_num_pins;
+ for (i = 0; i < child_num_pins; i++) {
+ pin_data = &((struct starfive_pin *)(grp->data))[i + offset];
+ pins_id = &(grp->pins)[i + offset];
+
+ if (!info->starfive_pinctrl_parse_pin) {
+ dev_err(sfp->dev,
+ "pinmux ops lacks necessary functions\n");
+ goto put_child;
+ }
+
+ info->starfive_pinctrl_parse_pin(sfp,
+ pins_id, pin_data, list, child);
+ list++;
+ }
+ offset += i;
+
+ map[nmaps].type = PIN_MAP_TYPE_MUX_GROUP;
+ map[nmaps].data.mux.function = np->name;
+ map[nmaps].data.mux.group = grpname;
+ nmaps += 1;
+
+ ret = pinctrl_generic_add_group(pctldev,
+ grpname, pins_id, child_num_pins, pin_data);
+ if (ret < 0) {
+ dev_err(dev, "error adding group %s: %d\n", grpname, ret);
+ goto put_child;
+ }
+
+ ret = pinconf_generic_parse_dt_config(child, pctldev,
+ &map[nmaps].data.configs.configs,
+ &map[nmaps].data.configs.num_configs);
+ if (ret) {
+ dev_err(dev, "error parsing pin config of group %s: %d\n",
+ grpname, ret);
+ goto put_child;
+ }
+
+ /* don't create a map if there are no pinconf settings */
+ if (map[nmaps].data.configs.num_configs == 0)
+ continue;
+
+ map[nmaps].type = PIN_MAP_TYPE_CONFIGS_GROUP;
+ map[nmaps].data.configs.group_or_pin = grpname;
+ nmaps += 1;
+ }
+
+ ret = pinmux_generic_add_function(pctldev, np->name, pgnames, ngroups, NULL);
+ if (ret < 0) {
+ dev_err(dev, "error adding function %s: %d\n", np->name, ret);
+ goto free_map;
}
+ *maps = map;
+ *num_maps = nmaps;
+ mutex_unlock(&sfp->mutex);
return 0;
+
+put_child:
+ of_node_put(child);
+free_map:
+ pinctrl_utils_free_map(pctldev, map, nmaps);
+ mutex_unlock(&sfp->mutex);
+ return ret;
}
static void starfive_dt_free_map(struct pinctrl_dev *pctldev,
.pin_config_group_dbg_show = starfive_pinconf_group_dbg_show,
};
-
-static int starfive_pinctrl_parse_groups(struct device_node *np,
- struct group_desc *grp,
- struct starfive_pinctrl *pctl,
- u32 index)
-{
- const struct starfive_pinctrl_soc_info *info = pctl->info;
- struct starfive_pin *pin_data;
- struct device_node *child;
- int *pins_id;
- int psize, pin_size;
- int size = 0;
- int offset = 0;
- const __be32 *list;
- int j, child_num_pins;
-
- pin_size = STARFIVE_PINS_SIZE;
-
- /* Initialise group */
- grp->name = np->name;
-
- for_each_child_of_node(np, child) {
- list = of_get_property(child, "sf,pins", &psize);
- if (!list) {
- dev_err(pctl->dev,
- "no sf,pins and pins property in node %pOF\n", np);
- return -EINVAL;
- }
- size += psize;
- }
-
- if (!size || size % pin_size) {
- dev_err(pctl->dev,
- "Invalid sf,pins or pins property in node %pOF\n", np);
- return -EINVAL;
- }
-
- grp->num_pins = size / pin_size;
- grp->data = devm_kcalloc(pctl->dev,
- grp->num_pins, sizeof(struct starfive_pin),
- GFP_KERNEL);
- grp->pins = devm_kcalloc(pctl->dev,
- grp->num_pins, sizeof(int),
- GFP_KERNEL);
- if (!grp->pins || !grp->data)
- return -ENOMEM;
-
- for_each_child_of_node(np, child) {
- list = of_get_property(child, "sf,pins", &psize);
- if (!list) {
- dev_err(pctl->dev,
- "no sf,pins and pins property in node %pOF\n", np);
- return -EINVAL;
- }
-
- child_num_pins = psize / pin_size;
-
- for (j = 0; j < child_num_pins; j++) {
- pin_data = &((struct starfive_pin *)(grp->data))[j + offset];
- pins_id = &(grp->pins)[j + offset];
-
- if (!info->starfive_pinctrl_parse_pin) {
- dev_err(pctl->dev, "pinmux ops lacks necessary functions\n");
- return -EINVAL;
- }
-
- info->starfive_pinctrl_parse_pin(pctl, pins_id, pin_data, list, child);
- list++;
- }
- offset += j;
- }
-
- return 0;
-}
-
-static int starfive_pinctrl_parse_functions(struct device_node *np,
- struct starfive_pinctrl *pctl,
- u32 index)
-{
- struct pinctrl_dev *pctldev = pctl->pctl_dev;
- struct device_node *child;
- struct function_desc *func;
- struct group_desc *grp;
- u32 i = 0;
- int ret;
-
- func = pinmux_generic_get_function(pctldev, index);
- if (!func)
- return -EINVAL;
-
- func->name = np->name;
- func->num_group_names = of_get_child_count(np);
- if (func->num_group_names == 0) {
- dev_err(pctl->dev, "no groups defined in %pOF\n", np);
- return -EINVAL;
- }
- func->group_names = devm_kcalloc(pctl->dev, func->num_group_names,
- sizeof(char *), GFP_KERNEL);
- if (!func->group_names)
- return -ENOMEM;
-
- for_each_child_of_node(np, child) {
- func->group_names[i] = child->name;
- grp = devm_kzalloc(pctl->dev, sizeof(struct group_desc),
- GFP_KERNEL);
- if (!grp) {
- of_node_put(child);
- return -ENOMEM;
- }
-
- mutex_lock(&pctl->mutex);
- radix_tree_insert(&pctldev->pin_group_tree,
- pctl->group_index++, grp);
- mutex_unlock(&pctl->mutex);
-
- ret = starfive_pinctrl_parse_groups(child, grp, pctl, i++);
- if (ret < 0) {
- dev_err(pctl->dev, "parse groups failed\n");
- return ret;
- }
- }
-
- return 0;
-}
-
-static int starfive_pinctrl_probe_dt(struct platform_device *pdev,
- struct starfive_pinctrl *pctl)
-{
- struct device_node *np = pdev->dev.of_node;
- struct pinctrl_dev *pctldev = pctl->pctl_dev;
- u32 nfuncs = 1;
- u32 i = 0;
-
- if (!np)
- return -ENODEV;
-
- for (i = 0; i < nfuncs; i++) {
- struct function_desc *function;
-
- function = devm_kzalloc(&pdev->dev, sizeof(*function),
- GFP_KERNEL);
- if (!function)
- return -ENOMEM;
-
- mutex_lock(&pctl->mutex);
- radix_tree_insert(&pctldev->pin_function_tree, i, function);
- mutex_unlock(&pctl->mutex);
- }
-
- pctldev->num_functions = nfuncs;
- pctl->group_index = 0;
- pctldev->num_groups = of_get_child_count(np);
- starfive_pinctrl_parse_functions(np, pctl, 0);
-
- return 0;
-}
-
static void starfive_disable_clock(void *data)
{
clk_disable_unprepare(data);
return ret;
}
- ret = starfive_pinctrl_probe_dt(pdev, pctl);
- if (ret) {
- dev_err(&pdev->dev,
- "fail to probe dt properties\n");
- return ret;
- }
-
ret = pinctrl_enable(pctl->pctl_dev);
if (ret) {
dev_err(&pdev->dev,