Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * I2C multiplexer using GPIO API
0004  *
0005  * Peter Korsgaard <peter.korsgaard@barco.com>
0006  */
0007 
0008 #include <linux/i2c.h>
0009 #include <linux/i2c-mux.h>
0010 #include <linux/overflow.h>
0011 #include <linux/platform_data/i2c-mux-gpio.h>
0012 #include <linux/platform_device.h>
0013 #include <linux/module.h>
0014 #include <linux/slab.h>
0015 #include <linux/bits.h>
0016 #include <linux/gpio/consumer.h>
0017 /* FIXME: stop poking around inside gpiolib */
0018 #include "../../gpio/gpiolib.h"
0019 
0020 struct gpiomux {
0021     struct i2c_mux_gpio_platform_data data;
0022     int ngpios;
0023     struct gpio_desc **gpios;
0024 };
0025 
0026 static void i2c_mux_gpio_set(const struct gpiomux *mux, unsigned val)
0027 {
0028     DECLARE_BITMAP(values, BITS_PER_TYPE(val));
0029 
0030     values[0] = val;
0031 
0032     gpiod_set_array_value_cansleep(mux->ngpios, mux->gpios, NULL, values);
0033 }
0034 
0035 static int i2c_mux_gpio_select(struct i2c_mux_core *muxc, u32 chan)
0036 {
0037     struct gpiomux *mux = i2c_mux_priv(muxc);
0038 
0039     i2c_mux_gpio_set(mux, chan);
0040 
0041     return 0;
0042 }
0043 
0044 static int i2c_mux_gpio_deselect(struct i2c_mux_core *muxc, u32 chan)
0045 {
0046     struct gpiomux *mux = i2c_mux_priv(muxc);
0047 
0048     i2c_mux_gpio_set(mux, mux->data.idle);
0049 
0050     return 0;
0051 }
0052 
0053 static int i2c_mux_gpio_probe_fw(struct gpiomux *mux,
0054                  struct platform_device *pdev)
0055 {
0056     struct device *dev = &pdev->dev;
0057     struct fwnode_handle *fwnode = dev_fwnode(dev);
0058     struct device_node *np = dev->of_node;
0059     struct device_node *adapter_np;
0060     struct i2c_adapter *adapter = NULL;
0061     struct fwnode_handle *child;
0062     unsigned *values;
0063     int rc, i = 0;
0064 
0065     if (is_of_node(fwnode)) {
0066         if (!np)
0067             return -ENODEV;
0068 
0069         adapter_np = of_parse_phandle(np, "i2c-parent", 0);
0070         if (!adapter_np) {
0071             dev_err(&pdev->dev, "Cannot parse i2c-parent\n");
0072             return -ENODEV;
0073         }
0074         adapter = of_find_i2c_adapter_by_node(adapter_np);
0075         of_node_put(adapter_np);
0076 
0077     } else if (is_acpi_node(fwnode)) {
0078         /*
0079          * In ACPI land the mux should be a direct child of the i2c
0080          * bus it muxes.
0081          */
0082         acpi_handle dev_handle = ACPI_HANDLE(dev->parent);
0083 
0084         adapter = i2c_acpi_find_adapter_by_handle(dev_handle);
0085     }
0086 
0087     if (!adapter)
0088         return -EPROBE_DEFER;
0089 
0090     mux->data.parent = i2c_adapter_id(adapter);
0091     put_device(&adapter->dev);
0092 
0093     mux->data.n_values = device_get_child_node_count(dev);
0094     values = devm_kcalloc(dev,
0095                   mux->data.n_values, sizeof(*mux->data.values),
0096                   GFP_KERNEL);
0097     if (!values) {
0098         dev_err(dev, "Cannot allocate values array");
0099         return -ENOMEM;
0100     }
0101 
0102     device_for_each_child_node(dev, child) {
0103         if (is_of_node(child)) {
0104             fwnode_property_read_u32(child, "reg", values + i);
0105 
0106         } else if (is_acpi_node(child)) {
0107             rc = acpi_get_local_address(ACPI_HANDLE_FWNODE(child), values + i);
0108             if (rc)
0109                 return dev_err_probe(dev, rc, "Cannot get address\n");
0110         }
0111 
0112         i++;
0113     }
0114     mux->data.values = values;
0115 
0116     if (device_property_read_u32(dev, "idle-state", &mux->data.idle))
0117         mux->data.idle = I2C_MUX_GPIO_NO_IDLE;
0118 
0119     return 0;
0120 }
0121 
0122 static int i2c_mux_gpio_probe(struct platform_device *pdev)
0123 {
0124     struct i2c_mux_core *muxc;
0125     struct gpiomux *mux;
0126     struct i2c_adapter *parent;
0127     struct i2c_adapter *root;
0128     unsigned initial_state;
0129     int i, ngpios, ret;
0130 
0131     mux = devm_kzalloc(&pdev->dev, sizeof(*mux), GFP_KERNEL);
0132     if (!mux)
0133         return -ENOMEM;
0134 
0135     if (!dev_get_platdata(&pdev->dev)) {
0136         ret = i2c_mux_gpio_probe_fw(mux, pdev);
0137         if (ret < 0)
0138             return ret;
0139     } else {
0140         memcpy(&mux->data, dev_get_platdata(&pdev->dev),
0141             sizeof(mux->data));
0142     }
0143 
0144     ngpios = gpiod_count(&pdev->dev, "mux");
0145     if (ngpios <= 0) {
0146         dev_err(&pdev->dev, "no valid gpios provided\n");
0147         return ngpios ?: -EINVAL;
0148     }
0149     mux->ngpios = ngpios;
0150 
0151     parent = i2c_get_adapter(mux->data.parent);
0152     if (!parent)
0153         return -EPROBE_DEFER;
0154 
0155     muxc = i2c_mux_alloc(parent, &pdev->dev, mux->data.n_values,
0156                  array_size(ngpios, sizeof(*mux->gpios)), 0,
0157                  i2c_mux_gpio_select, NULL);
0158     if (!muxc) {
0159         ret = -ENOMEM;
0160         goto alloc_failed;
0161     }
0162     mux->gpios = muxc->priv;
0163     muxc->priv = mux;
0164 
0165     platform_set_drvdata(pdev, muxc);
0166 
0167     root = i2c_root_adapter(&parent->dev);
0168 
0169     muxc->mux_locked = true;
0170 
0171     if (mux->data.idle != I2C_MUX_GPIO_NO_IDLE) {
0172         initial_state = mux->data.idle;
0173         muxc->deselect = i2c_mux_gpio_deselect;
0174     } else {
0175         initial_state = mux->data.values[0];
0176     }
0177 
0178     for (i = 0; i < ngpios; i++) {
0179         struct device *gpio_dev;
0180         struct gpio_desc *gpiod;
0181         enum gpiod_flags flag;
0182 
0183         if (initial_state & BIT(i))
0184             flag = GPIOD_OUT_HIGH;
0185         else
0186             flag = GPIOD_OUT_LOW;
0187         gpiod = devm_gpiod_get_index(&pdev->dev, "mux", i, flag);
0188         if (IS_ERR(gpiod)) {
0189             ret = PTR_ERR(gpiod);
0190             goto alloc_failed;
0191         }
0192 
0193         mux->gpios[i] = gpiod;
0194 
0195         if (!muxc->mux_locked)
0196             continue;
0197 
0198         /* FIXME: find a proper way to access the GPIO device */
0199         gpio_dev = &gpiod->gdev->dev;
0200         muxc->mux_locked = i2c_root_adapter(gpio_dev) == root;
0201     }
0202 
0203     if (muxc->mux_locked)
0204         dev_info(&pdev->dev, "mux-locked i2c mux\n");
0205 
0206     for (i = 0; i < mux->data.n_values; i++) {
0207         u32 nr = mux->data.base_nr ? (mux->data.base_nr + i) : 0;
0208         unsigned int class = mux->data.classes ? mux->data.classes[i] : 0;
0209 
0210         ret = i2c_mux_add_adapter(muxc, nr, mux->data.values[i], class);
0211         if (ret)
0212             goto add_adapter_failed;
0213     }
0214 
0215     dev_info(&pdev->dev, "%d port mux on %s adapter\n",
0216          mux->data.n_values, parent->name);
0217 
0218     return 0;
0219 
0220 add_adapter_failed:
0221     i2c_mux_del_adapters(muxc);
0222 alloc_failed:
0223     i2c_put_adapter(parent);
0224 
0225     return ret;
0226 }
0227 
0228 static int i2c_mux_gpio_remove(struct platform_device *pdev)
0229 {
0230     struct i2c_mux_core *muxc = platform_get_drvdata(pdev);
0231 
0232     i2c_mux_del_adapters(muxc);
0233     i2c_put_adapter(muxc->parent);
0234 
0235     return 0;
0236 }
0237 
0238 static const struct of_device_id i2c_mux_gpio_of_match[] = {
0239     { .compatible = "i2c-mux-gpio", },
0240     {},
0241 };
0242 MODULE_DEVICE_TABLE(of, i2c_mux_gpio_of_match);
0243 
0244 static struct platform_driver i2c_mux_gpio_driver = {
0245     .probe  = i2c_mux_gpio_probe,
0246     .remove = i2c_mux_gpio_remove,
0247     .driver = {
0248         .name   = "i2c-mux-gpio",
0249         .of_match_table = i2c_mux_gpio_of_match,
0250     },
0251 };
0252 
0253 module_platform_driver(i2c_mux_gpio_driver);
0254 
0255 MODULE_DESCRIPTION("GPIO-based I2C multiplexer driver");
0256 MODULE_AUTHOR("Peter Korsgaard <peter.korsgaard@barco.com>");
0257 MODULE_LICENSE("GPL");
0258 MODULE_ALIAS("platform:i2c-mux-gpio");