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
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) //
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3) // General Purpose SPI multiplexer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/mux/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/spi/spi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #define SPI_MUX_NO_CS	((unsigned int)-1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15)  * DOC: Driver description
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17)  * This driver supports a MUX on an SPI bus. This can be useful when you need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18)  * more chip selects than the hardware peripherals support, or than are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  * available in a particular board setup.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  * The driver will create an additional SPI controller. Devices added under the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  * mux will be handled as 'chip selects' on this controller.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)  * struct spi_mux_priv - the basic spi_mux structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)  * @spi:		pointer to the device struct attached to the parent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28)  *			spi controller
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29)  * @current_cs:		The current chip select set in the mux
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30)  * @child_msg_complete: The mux replaces the complete callback in the child's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31)  *			message to its own callback; this field is used by the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32)  *			driver to store the child's callback during a transfer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33)  * @child_msg_context:	Used to store the child's context to the callback
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34)  * @child_msg_dev:	Used to store the spi_device pointer to the child
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35)  * @mux:		mux_control structure used to provide chip selects for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36)  *			downstream spi devices
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) struct spi_mux_priv {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	struct spi_device	*spi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	unsigned int		current_cs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	void			(*child_msg_complete)(void *context);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	void			*child_msg_context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	struct spi_device	*child_msg_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	struct mux_control	*mux;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) /* should not get called when the parent controller is doing a transfer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) static int spi_mux_select(struct spi_device *spi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	struct spi_mux_priv *priv = spi_controller_get_devdata(spi->controller);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	ret = mux_control_select(priv->mux, spi->chip_select);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	if (priv->current_cs == spi->chip_select)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	dev_dbg(&priv->spi->dev, "setting up the mux for cs %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 		spi->chip_select);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	/* copy the child device's settings except for the cs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	priv->spi->max_speed_hz = spi->max_speed_hz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	priv->spi->mode = spi->mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	priv->spi->bits_per_word = spi->bits_per_word;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	priv->current_cs = spi->chip_select;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) static int spi_mux_setup(struct spi_device *spi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	struct spi_mux_priv *priv = spi_controller_get_devdata(spi->controller);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	 * can be called multiple times, won't do a valid setup now but we will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	 * change the settings when we do a transfer (necessary because we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	 * can't predict from which device it will be anyway)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	return spi_setup(priv->spi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) static void spi_mux_complete_cb(void *context)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	struct spi_mux_priv *priv = (struct spi_mux_priv *)context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	struct spi_controller *ctlr = spi_get_drvdata(priv->spi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	struct spi_message *m = ctlr->cur_msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	m->complete = priv->child_msg_complete;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	m->context = priv->child_msg_context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	m->spi = priv->child_msg_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	spi_finalize_current_message(ctlr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	mux_control_deselect(priv->mux);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) static int spi_mux_transfer_one_message(struct spi_controller *ctlr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 						struct spi_message *m)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	struct spi_mux_priv *priv = spi_controller_get_devdata(ctlr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	struct spi_device *spi = m->spi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	ret = spi_mux_select(spi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	 * Replace the complete callback, context and spi_device with our own
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	 * pointers. Save originals
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	priv->child_msg_complete = m->complete;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	priv->child_msg_context = m->context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	priv->child_msg_dev = m->spi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	m->complete = spi_mux_complete_cb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	m->context = priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	m->spi = priv->spi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	/* do the transfer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	return spi_async(priv->spi, m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) static int spi_mux_probe(struct spi_device *spi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	struct spi_controller *ctlr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	struct spi_mux_priv *priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	ctlr = spi_alloc_master(&spi->dev, sizeof(*priv));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	if (!ctlr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	spi_set_drvdata(spi, ctlr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	priv = spi_controller_get_devdata(ctlr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	priv->spi = spi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	priv->mux = devm_mux_control_get(&spi->dev, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	if (IS_ERR(priv->mux)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 		ret = dev_err_probe(&spi->dev, PTR_ERR(priv->mux),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 				    "failed to get control-mux\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 		goto err_put_ctlr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	priv->current_cs = SPI_MUX_NO_CS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	/* supported modes are the same as our parent's */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	ctlr->mode_bits = spi->controller->mode_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	ctlr->flags = spi->controller->flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	ctlr->transfer_one_message = spi_mux_transfer_one_message;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	ctlr->setup = spi_mux_setup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	ctlr->num_chipselect = mux_control_states(priv->mux);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	ctlr->bus_num = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	ctlr->dev.of_node = spi->dev.of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	ret = devm_spi_register_controller(&spi->dev, ctlr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 		goto err_put_ctlr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) err_put_ctlr:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	spi_controller_put(ctlr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) static const struct spi_device_id spi_mux_id[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	{ "spi-mux" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	{ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) MODULE_DEVICE_TABLE(spi, spi_mux_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) static const struct of_device_id spi_mux_of_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	{ .compatible = "spi-mux" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	{ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) MODULE_DEVICE_TABLE(of, spi_mux_of_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) static struct spi_driver spi_mux_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	.probe  = spi_mux_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 		.name   = "spi-mux",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 		.of_match_table = spi_mux_of_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	.id_table = spi_mux_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) module_spi_driver(spi_mux_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) MODULE_DESCRIPTION("SPI multiplexer");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) MODULE_LICENSE("GPL");