brintos

brintos / linux-shallow public Read only

0
0
Text · 3.5 KiB · 295e9d0 Raw
143 lines · c
1// SPDX-License-Identifier: GPL-2.0+2/*3 * Copyright (C) 2023 Linaro Ltd.4 *5 * Author: Dmitry Baryshkov <dmitry.baryshkov@linaro.org>6 */7#include <linux/auxiliary_bus.h>8#include <linux/module.h>9#include <linux/of.h>10 11#include <drm/drm_bridge.h>12#include <drm/bridge/aux-bridge.h>13 14static DEFINE_IDA(drm_aux_bridge_ida);15 16static void drm_aux_bridge_release(struct device *dev)17{18	struct auxiliary_device *adev = to_auxiliary_dev(dev);19 20	ida_free(&drm_aux_bridge_ida, adev->id);21 22	kfree(adev);23}24 25static void drm_aux_bridge_unregister_adev(void *_adev)26{27	struct auxiliary_device *adev = _adev;28 29	auxiliary_device_delete(adev);30	auxiliary_device_uninit(adev);31}32 33/**34 * drm_aux_bridge_register - Create a simple bridge device to link the chain35 * @parent: device instance providing this bridge36 *37 * Creates a simple DRM bridge that doesn't implement any drm_bridge38 * operations. Such bridges merely fill a place in the bridge chain linking39 * surrounding DRM bridges.40 *41 * Return: zero on success, negative error code on failure42 */43int drm_aux_bridge_register(struct device *parent)44{45	struct auxiliary_device *adev;46	int ret;47 48	adev = kzalloc(sizeof(*adev), GFP_KERNEL);49	if (!adev)50		return -ENOMEM;51 52	ret = ida_alloc(&drm_aux_bridge_ida, GFP_KERNEL);53	if (ret < 0) {54		kfree(adev);55		return ret;56	}57 58	adev->id = ret;59	adev->name = "aux_bridge";60	adev->dev.parent = parent;61	adev->dev.release = drm_aux_bridge_release;62 63	device_set_of_node_from_dev(&adev->dev, parent);64 65	ret = auxiliary_device_init(adev);66	if (ret) {67		ida_free(&drm_aux_bridge_ida, adev->id);68		kfree(adev);69		return ret;70	}71 72	ret = auxiliary_device_add(adev);73	if (ret) {74		auxiliary_device_uninit(adev);75		return ret;76	}77 78	return devm_add_action_or_reset(parent, drm_aux_bridge_unregister_adev, adev);79}80EXPORT_SYMBOL_GPL(drm_aux_bridge_register);81 82struct drm_aux_bridge_data {83	struct drm_bridge bridge;84	struct drm_bridge *next_bridge;85	struct device *dev;86};87 88static int drm_aux_bridge_attach(struct drm_bridge *bridge,89				 enum drm_bridge_attach_flags flags)90{91	struct drm_aux_bridge_data *data;92 93	if (!(flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR))94		return -EINVAL;95 96	data = container_of(bridge, struct drm_aux_bridge_data, bridge);97 98	return drm_bridge_attach(bridge->encoder, data->next_bridge, bridge,99				 DRM_BRIDGE_ATTACH_NO_CONNECTOR);100}101 102static const struct drm_bridge_funcs drm_aux_bridge_funcs = {103	.attach	= drm_aux_bridge_attach,104};105 106static int drm_aux_bridge_probe(struct auxiliary_device *auxdev,107				const struct auxiliary_device_id *id)108{109	struct drm_aux_bridge_data *data;110 111	data = devm_kzalloc(&auxdev->dev, sizeof(*data), GFP_KERNEL);112	if (!data)113		return -ENOMEM;114 115	data->dev = &auxdev->dev;116	data->next_bridge = devm_drm_of_get_bridge(&auxdev->dev, auxdev->dev.of_node, 0, 0);117	if (IS_ERR(data->next_bridge))118		return dev_err_probe(&auxdev->dev, PTR_ERR(data->next_bridge),119				     "failed to acquire drm_bridge\n");120 121	data->bridge.funcs = &drm_aux_bridge_funcs;122	data->bridge.of_node = data->dev->of_node;123 124	return devm_drm_bridge_add(data->dev, &data->bridge);125}126 127static const struct auxiliary_device_id drm_aux_bridge_table[] = {128	{ .name = KBUILD_MODNAME ".aux_bridge" },129	{},130};131MODULE_DEVICE_TABLE(auxiliary, drm_aux_bridge_table);132 133static struct auxiliary_driver drm_aux_bridge_drv = {134	.name = "aux_bridge",135	.id_table = drm_aux_bridge_table,136	.probe = drm_aux_bridge_probe,137};138module_auxiliary_driver(drm_aux_bridge_drv);139 140MODULE_AUTHOR("Dmitry Baryshkov <dmitry.baryshkov@linaro.org>");141MODULE_DESCRIPTION("DRM transparent bridge");142MODULE_LICENSE("GPL");143