Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * I2C multiplexer using a single register
0004  *
0005  * Copyright 2015 Freescale Semiconductor
0006  * York Sun  <yorksun@freescale.com>
0007  */
0008 
0009 #include <linux/i2c.h>
0010 #include <linux/i2c-mux.h>
0011 #include <linux/init.h>
0012 #include <linux/io.h>
0013 #include <linux/module.h>
0014 #include <linux/of_address.h>
0015 #include <linux/platform_data/i2c-mux-reg.h>
0016 #include <linux/platform_device.h>
0017 #include <linux/slab.h>
0018 
0019 struct regmux {
0020     struct i2c_mux_reg_platform_data data;
0021 };
0022 
0023 static int i2c_mux_reg_set(const struct regmux *mux, unsigned int chan_id)
0024 {
0025     if (!mux->data.reg)
0026         return -EINVAL;
0027 
0028     /*
0029      * Write to the register, followed by a read to ensure the write is
0030      * completed on a "posted" bus, for example PCI or write buffers.
0031      * The endianness of reading doesn't matter and the return data
0032      * is not used.
0033      */
0034     switch (mux->data.reg_size) {
0035     case 4:
0036         if (mux->data.little_endian)
0037             iowrite32(chan_id, mux->data.reg);
0038         else
0039             iowrite32be(chan_id, mux->data.reg);
0040         if (!mux->data.write_only)
0041             ioread32(mux->data.reg);
0042         break;
0043     case 2:
0044         if (mux->data.little_endian)
0045             iowrite16(chan_id, mux->data.reg);
0046         else
0047             iowrite16be(chan_id, mux->data.reg);
0048         if (!mux->data.write_only)
0049             ioread16(mux->data.reg);
0050         break;
0051     case 1:
0052         iowrite8(chan_id, mux->data.reg);
0053         if (!mux->data.write_only)
0054             ioread8(mux->data.reg);
0055         break;
0056     }
0057 
0058     return 0;
0059 }
0060 
0061 static int i2c_mux_reg_select(struct i2c_mux_core *muxc, u32 chan)
0062 {
0063     struct regmux *mux = i2c_mux_priv(muxc);
0064 
0065     return i2c_mux_reg_set(mux, chan);
0066 }
0067 
0068 static int i2c_mux_reg_deselect(struct i2c_mux_core *muxc, u32 chan)
0069 {
0070     struct regmux *mux = i2c_mux_priv(muxc);
0071 
0072     if (mux->data.idle_in_use)
0073         return i2c_mux_reg_set(mux, mux->data.idle);
0074 
0075     return 0;
0076 }
0077 
0078 #ifdef CONFIG_OF
0079 static int i2c_mux_reg_probe_dt(struct regmux *mux,
0080                 struct platform_device *pdev)
0081 {
0082     struct device_node *np = pdev->dev.of_node;
0083     struct device_node *adapter_np, *child;
0084     struct i2c_adapter *adapter;
0085     struct resource res;
0086     unsigned *values;
0087     int i = 0;
0088 
0089     if (!np)
0090         return -ENODEV;
0091 
0092     adapter_np = of_parse_phandle(np, "i2c-parent", 0);
0093     if (!adapter_np) {
0094         dev_err(&pdev->dev, "Cannot parse i2c-parent\n");
0095         return -ENODEV;
0096     }
0097     adapter = of_find_i2c_adapter_by_node(adapter_np);
0098     of_node_put(adapter_np);
0099     if (!adapter)
0100         return -EPROBE_DEFER;
0101 
0102     mux->data.parent = i2c_adapter_id(adapter);
0103     put_device(&adapter->dev);
0104 
0105     mux->data.n_values = of_get_child_count(np);
0106     if (of_property_read_bool(np, "little-endian")) {
0107         mux->data.little_endian = true;
0108     } else if (of_property_read_bool(np, "big-endian")) {
0109         mux->data.little_endian = false;
0110     } else {
0111 #if defined(__BYTE_ORDER) ? __BYTE_ORDER == __LITTLE_ENDIAN : \
0112     defined(__LITTLE_ENDIAN)
0113         mux->data.little_endian = true;
0114 #elif defined(__BYTE_ORDER) ? __BYTE_ORDER == __BIG_ENDIAN : \
0115     defined(__BIG_ENDIAN)
0116         mux->data.little_endian = false;
0117 #else
0118 #error Endianness not defined?
0119 #endif
0120     }
0121     mux->data.write_only = of_property_read_bool(np, "write-only");
0122 
0123     values = devm_kcalloc(&pdev->dev,
0124                   mux->data.n_values, sizeof(*mux->data.values),
0125                   GFP_KERNEL);
0126     if (!values)
0127         return -ENOMEM;
0128 
0129     for_each_child_of_node(np, child) {
0130         of_property_read_u32(child, "reg", values + i);
0131         i++;
0132     }
0133     mux->data.values = values;
0134 
0135     if (!of_property_read_u32(np, "idle-state", &mux->data.idle))
0136         mux->data.idle_in_use = true;
0137 
0138     /* map address from "reg" if exists */
0139     if (of_address_to_resource(np, 0, &res) == 0) {
0140         mux->data.reg_size = resource_size(&res);
0141         mux->data.reg = devm_ioremap_resource(&pdev->dev, &res);
0142         if (IS_ERR(mux->data.reg))
0143             return PTR_ERR(mux->data.reg);
0144     }
0145 
0146     return 0;
0147 }
0148 #else
0149 static int i2c_mux_reg_probe_dt(struct regmux *mux,
0150                 struct platform_device *pdev)
0151 {
0152     return 0;
0153 }
0154 #endif
0155 
0156 static int i2c_mux_reg_probe(struct platform_device *pdev)
0157 {
0158     struct i2c_mux_core *muxc;
0159     struct regmux *mux;
0160     struct i2c_adapter *parent;
0161     struct resource *res;
0162     unsigned int class;
0163     int i, ret, nr;
0164 
0165     mux = devm_kzalloc(&pdev->dev, sizeof(*mux), GFP_KERNEL);
0166     if (!mux)
0167         return -ENOMEM;
0168 
0169     if (dev_get_platdata(&pdev->dev)) {
0170         memcpy(&mux->data, dev_get_platdata(&pdev->dev),
0171             sizeof(mux->data));
0172     } else {
0173         ret = i2c_mux_reg_probe_dt(mux, pdev);
0174         if (ret < 0)
0175             return dev_err_probe(&pdev->dev, ret,
0176                          "Error parsing device tree");
0177     }
0178 
0179     parent = i2c_get_adapter(mux->data.parent);
0180     if (!parent)
0181         return -EPROBE_DEFER;
0182 
0183     if (!mux->data.reg) {
0184         dev_info(&pdev->dev,
0185             "Register not set, using platform resource\n");
0186         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
0187         mux->data.reg_size = resource_size(res);
0188         mux->data.reg = devm_ioremap_resource(&pdev->dev, res);
0189         if (IS_ERR(mux->data.reg)) {
0190             ret = PTR_ERR(mux->data.reg);
0191             goto err_put_parent;
0192         }
0193     }
0194 
0195     if (mux->data.reg_size != 4 && mux->data.reg_size != 2 &&
0196         mux->data.reg_size != 1) {
0197         dev_err(&pdev->dev, "Invalid register size\n");
0198         ret = -EINVAL;
0199         goto err_put_parent;
0200     }
0201 
0202     muxc = i2c_mux_alloc(parent, &pdev->dev, mux->data.n_values, 0, 0,
0203                  i2c_mux_reg_select, NULL);
0204     if (!muxc) {
0205         ret = -ENOMEM;
0206         goto err_put_parent;
0207     }
0208     muxc->priv = mux;
0209 
0210     platform_set_drvdata(pdev, muxc);
0211 
0212     if (mux->data.idle_in_use)
0213         muxc->deselect = i2c_mux_reg_deselect;
0214 
0215     for (i = 0; i < mux->data.n_values; i++) {
0216         nr = mux->data.base_nr ? (mux->data.base_nr + i) : 0;
0217         class = mux->data.classes ? mux->data.classes[i] : 0;
0218 
0219         ret = i2c_mux_add_adapter(muxc, nr, mux->data.values[i], class);
0220         if (ret)
0221             goto err_del_mux_adapters;
0222     }
0223 
0224     dev_dbg(&pdev->dev, "%d port mux on %s adapter\n",
0225          mux->data.n_values, muxc->parent->name);
0226 
0227     return 0;
0228 
0229 err_del_mux_adapters:
0230     i2c_mux_del_adapters(muxc);
0231 err_put_parent:
0232     i2c_put_adapter(parent);
0233 
0234     return ret;
0235 }
0236 
0237 static int i2c_mux_reg_remove(struct platform_device *pdev)
0238 {
0239     struct i2c_mux_core *muxc = platform_get_drvdata(pdev);
0240 
0241     i2c_mux_del_adapters(muxc);
0242     i2c_put_adapter(muxc->parent);
0243 
0244     return 0;
0245 }
0246 
0247 static const struct of_device_id i2c_mux_reg_of_match[] = {
0248     { .compatible = "i2c-mux-reg", },
0249     {},
0250 };
0251 MODULE_DEVICE_TABLE(of, i2c_mux_reg_of_match);
0252 
0253 static struct platform_driver i2c_mux_reg_driver = {
0254     .probe  = i2c_mux_reg_probe,
0255     .remove = i2c_mux_reg_remove,
0256     .driver = {
0257         .name   = "i2c-mux-reg",
0258         .of_match_table = of_match_ptr(i2c_mux_reg_of_match),
0259     },
0260 };
0261 
0262 module_platform_driver(i2c_mux_reg_driver);
0263 
0264 MODULE_DESCRIPTION("Register-based I2C multiplexer driver");
0265 MODULE_AUTHOR("York Sun <yorksun@freescale.com>");
0266 MODULE_LICENSE("GPL");
0267 MODULE_ALIAS("platform:i2c-mux-reg");