Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * General Purpose I2C multiplexer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2017 Axentia Technologies AB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Author: Peter Rosin <peda@axentia.se>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/i2c-mux.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/mux/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/of_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) struct mux {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 	struct mux_control *control;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 	bool do_not_deselect;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) static int i2c_mux_select(struct i2c_mux_core *muxc, u32 chan)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	struct mux *mux = i2c_mux_priv(muxc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	ret = mux_control_select(mux->control, chan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	mux->do_not_deselect = ret < 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) static int i2c_mux_deselect(struct i2c_mux_core *muxc, u32 chan)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	struct mux *mux = i2c_mux_priv(muxc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	if (mux->do_not_deselect)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	return mux_control_deselect(mux->control);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) static struct i2c_adapter *mux_parent_adapter(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	struct device_node *np = dev->of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	struct device_node *parent_np;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	struct i2c_adapter *parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	parent_np = of_parse_phandle(np, "i2c-parent", 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	if (!parent_np) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 		dev_err(dev, "Cannot parse i2c-parent\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 		return ERR_PTR(-ENODEV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	parent = of_find_i2c_adapter_by_node(parent_np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	of_node_put(parent_np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	if (!parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 		return ERR_PTR(-EPROBE_DEFER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	return parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) static const struct of_device_id i2c_mux_of_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	{ .compatible = "i2c-mux", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	{},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) MODULE_DEVICE_TABLE(of, i2c_mux_of_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) static int i2c_mux_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	struct device *dev = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	struct device_node *np = dev->of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	struct device_node *child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	struct i2c_mux_core *muxc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	struct mux *mux;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	struct i2c_adapter *parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	int children;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	if (!np)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	mux = devm_kzalloc(dev, sizeof(*mux), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	if (!mux)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	mux->control = devm_mux_control_get(dev, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	if (IS_ERR(mux->control))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 		return dev_err_probe(dev, PTR_ERR(mux->control),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 				     "failed to get control-mux\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	parent = mux_parent_adapter(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	if (IS_ERR(parent))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 		return dev_err_probe(dev, PTR_ERR(parent),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 				     "failed to get i2c-parent adapter\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	children = of_get_child_count(np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	muxc = i2c_mux_alloc(parent, dev, children, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 			     i2c_mux_select, i2c_mux_deselect);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	if (!muxc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		goto err_parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	muxc->priv = mux;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	platform_set_drvdata(pdev, muxc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	muxc->mux_locked = of_property_read_bool(np, "mux-locked");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	for_each_child_of_node(np, child) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 		u32 chan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 		ret = of_property_read_u32(child, "reg", &chan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 		if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 			dev_err(dev, "no reg property for node '%pOFn'\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 				child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 			goto err_children;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		if (chan >= mux_control_states(mux->control)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 			dev_err(dev, "invalid reg %u\n", chan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 			ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 			goto err_children;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		ret = i2c_mux_add_adapter(muxc, 0, chan, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 			goto err_children;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	dev_info(dev, "%d-port mux on %s adapter\n", children, parent->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) err_children:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	i2c_mux_del_adapters(muxc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) err_parent:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	i2c_put_adapter(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) static int i2c_mux_remove(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	struct i2c_mux_core *muxc = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	i2c_mux_del_adapters(muxc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	i2c_put_adapter(muxc->parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) static struct platform_driver i2c_mux_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	.probe	= i2c_mux_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	.remove	= i2c_mux_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	.driver	= {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 		.name	= "i2c-mux-gpmux",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		.of_match_table = i2c_mux_of_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) module_platform_driver(i2c_mux_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) MODULE_DESCRIPTION("General Purpose I2C multiplexer driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) MODULE_AUTHOR("Peter Rosin <peda@axentia.se>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) MODULE_LICENSE("GPL v2");