01_basicLinux内核模块-CSDN博客文章浏览阅读319次,点赞3次,收藏3次。环境ID=ubuntuMakefilemodules:clean:basic.creturn 0;运行效果。https://blog.csdn.net/m0_37132481/article/details/136157384
my_pinctrl.c
#include <linux/kernel.h>
#include <linux/module.h>#include <linux/device.h>
#include <linux/slab.h>#include <linux/pinctrl/machine.h>
#include <linux/pinctrl/pinctrl.h>
#include <linux/pinctrl/pinmux.h>
#include <linux/pinctrl/pinconf.h>#define TAG "HELLO# "static int my_set_mux(struct pinctrl_dev *pctrldev, unsigned int function, unsigned int group)
{printk(TAG "%s called\n", __func__);return 0;
}static int my_get_func_groups(struct pinctrl_dev *pctrldev, unsigned int function, const char * const **groups, unsigned int * const num_groups)
{printk(TAG "%s called\n", __func__);return 1;
}
static const char *my_get_func_name(struct pinctrl_dev *pctrldev, unsigned int function)
{printk(TAG "%s called\n", __func__);return __func__;
}
static int my_get_funcs_count(struct pinctrl_dev *pctrldev)
{printk(TAG "%s called\n", __func__);return 2;
}
static int my_group_config_set(struct pinctrl_dev *pctrldev, unsigned int group, unsigned long *configs, unsigned int num_configs)
{printk(TAG "%s called\n", __func__);return 0;
}
static int my_group_config_get(struct pinctrl_dev *pctrldev, unsigned int group, unsigned long *config)
{printk(TAG "%s called\n", __func__);return 0;
}static void my_pin_dbg_show(struct pinctrl_dev *pctrldev, struct seq_file *s, unsigned int offset)
{printk(TAG "%s called\n", __func__);
}
static int my_get_group_pins(struct pinctrl_dev *pctrldev, unsigned int group, const unsigned int **pins, unsigned int *num_pins)
{printk(TAG "%s called\n", __func__);return 0;
}
static const char *my_get_group_name(struct pinctrl_dev *pctrldev, unsigned int group)
{printk(TAG "%s called\n", __func__);return __func__;
}
static int my_get_groups_count(struct pinctrl_dev *pctrldev)
{printk(TAG "%s called\n", __func__);return 1;
}static void my_pinctrl_free_map(struct pinctrl_dev *pctldev, struct pinctrl_map *map, unsigned num_maps)
{printk(TAG "%s called\n", __func__);
}
static int my_pin_config_get(struct pinctrl_dev *pctrldev, unsigned int pin, unsigned long *config)
{printk(TAG "%s called\n", __func__);return 0;
}
static int my_pin_config_set(struct pinctrl_dev *pctrldev, unsigned int pin, unsigned long *configs, unsigned int num_configs)
{printk(TAG "%s called\n", __func__);return 0;
}
static int my_pinconf_dt_node_to_map(struct pinctrl_dev *pctldev, struct device_node *np_config, struct pinctrl_map **map, unsigned *num_maps, enum pinctrl_map_type type)
{printk(TAG "%s called\n", __func__);return 0;
}
static inline int my_pinconf_dt_node_to_map_all(struct pinctrl_dev *pctldev, struct device_node *np_config, struct pinctrl_map **map, unsigned *num_maps)
{printk(TAG "%s called\n", __func__);return 0;
}static struct pinctrl_pin_desc my_pin_desc[] = {{0, "my_soc_pin1", NULL},{1, "my_soc_pin2", NULL},
};/* pinctrl_ops */
static const struct pinctrl_ops my_pinctrl_ops = {.get_groups_count = my_get_groups_count,.get_group_name = my_get_group_name,.get_group_pins = my_get_group_pins,.pin_dbg_show = my_pin_dbg_show,.dt_node_to_map = my_pinconf_dt_node_to_map_all,.dt_free_map = my_pinctrl_free_map,
};/* pinconf_ops */
static const struct pinconf_ops my_pinconf_ops = {.is_generic = true,.pin_config_get = my_pin_config_get,.pin_config_set = my_pin_config_set,.pin_config_group_get = my_group_config_get,.pin_config_group_set = my_group_config_set,
};/* pinmux_ops */
static const struct pinmux_ops my_pinmux_ops = {.get_functions_count = my_get_funcs_count,.get_function_name = my_get_func_name,.get_function_groups = my_get_func_groups,.set_mux = my_set_mux,
};/* pinctrl_desc */
static struct pinctrl_desc my_pinctrl_desc = {.owner = THIS_MODULE,.name = "my_pinctrl_desc",.pctlops = &my_pinctrl_ops,.pmxops = &my_pinmux_ops,.confops = &my_pinconf_ops,.pins = &my_pin_desc[0],.npins = sizeof(my_pin_desc) / sizeof(my_pin_desc[0]),
};static struct device *my_pinctrl_dev = NULL;static int my_pinctrl_init(void)
{int ret = 0;struct pinctrl_dev *pctldev = NULL;printk(TAG "%s called\n", __func__);/* my_pinctrl_dev */my_pinctrl_dev = kzalloc(sizeof(*my_pinctrl_dev), GFP_KERNEL);my_pinctrl_dev->init_name = "my_pinctrl_device";ret = device_register(my_pinctrl_dev);my_pinctrl_dev = get_device(my_pinctrl_dev);pctldev = devm_pinctrl_register(my_pinctrl_dev, &my_pinctrl_desc, NULL);if(IS_ERR(pctldev)){printk(TAG "fail\n");}else{printk(TAG "ok\n");}return 0;
}
static void my_pinctrl_exit(void)
{device_unregister(my_pinctrl_dev);kfree(my_pinctrl_dev);printk(TAG "%s called\n", __func__);
}module_init(my_pinctrl_init);
module_exit(my_pinctrl_exit);
MODULE_LICENSE("GPL");
效果
可以看到添加的2个pin成功注册到内核