1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * I2C multiplexer using pinctrl API 4 * 5 * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved. 6 */ 7 8 #include <linux/i2c.h> 9 #include <linux/i2c-mux.h> 10 #include <linux/module.h> 11 #include <linux/pinctrl/consumer.h> 12 #include <linux/platform_device.h> 13 #include <linux/slab.h> 14 #include <linux/of.h> 15 #include "../../pinctrl/core.h" 16 17 struct i2c_mux_pinctrl { 18 struct pinctrl *pinctrl; 19 struct pinctrl_state **states; 20 }; 21 22 static int i2c_mux_pinctrl_select(struct i2c_mux_core *muxc, u32 chan) 23 { 24 struct i2c_mux_pinctrl *mux = i2c_mux_priv(muxc); 25 26 return pinctrl_select_state(mux->pinctrl, mux->states[chan]); 27 } 28 29 static int i2c_mux_pinctrl_deselect(struct i2c_mux_core *muxc, u32 chan) 30 { 31 return i2c_mux_pinctrl_select(muxc, muxc->num_adapters); 32 } 33 34 static struct i2c_adapter *i2c_mux_pinctrl_root_adapter( 35 struct pinctrl_state *state) 36 { 37 struct i2c_adapter *root = NULL; 38 struct pinctrl_setting *setting; 39 struct i2c_adapter *pin_root; 40 41 list_for_each_entry(setting, &state->settings, node) { 42 pin_root = i2c_root_adapter(setting->pctldev->dev); 43 if (!pin_root) 44 return NULL; 45 if (!root) 46 root = pin_root; 47 else if (root != pin_root) 48 return NULL; 49 } 50 51 return root; 52 } 53 54 static struct i2c_adapter *i2c_mux_pinctrl_parent_adapter(struct device *dev) 55 { 56 struct device_node *np = dev->of_node; 57 struct device_node *parent_np; 58 struct i2c_adapter *parent; 59 60 parent_np = of_parse_phandle(np, "i2c-parent", 0); 61 if (!parent_np) { 62 dev_err(dev, "Cannot parse i2c-parent\n"); 63 return ERR_PTR(-ENODEV); 64 } 65 parent = of_find_i2c_adapter_by_node(parent_np); 66 of_node_put(parent_np); 67 if (!parent) 68 return ERR_PTR(-EPROBE_DEFER); 69 70 return parent; 71 } 72 73 static int i2c_mux_pinctrl_probe(struct platform_device *pdev) 74 { 75 struct device *dev = &pdev->dev; 76 struct device_node *np = dev->of_node; 77 struct i2c_mux_core *muxc; 78 struct i2c_mux_pinctrl *mux; 79 struct i2c_adapter *parent; 80 struct i2c_adapter *root; 81 int num_names, i, ret; 82 const char *name; 83 84 num_names = of_property_count_strings(np, "pinctrl-names"); 85 if (num_names < 0) { 86 dev_err(dev, "Cannot parse pinctrl-names: %d\n", 87 num_names); 88 return num_names; 89 } 90 91 parent = i2c_mux_pinctrl_parent_adapter(dev); 92 if (IS_ERR(parent)) 93 return PTR_ERR(parent); 94 95 muxc = i2c_mux_alloc(parent, dev, num_names, 96 sizeof(*mux) + num_names * sizeof(*mux->states), 97 0, i2c_mux_pinctrl_select, NULL); 98 if (!muxc) { 99 ret = -ENOMEM; 100 goto err_put_parent; 101 } 102 mux = i2c_mux_priv(muxc); 103 mux->states = (struct pinctrl_state **)(mux + 1); 104 105 platform_set_drvdata(pdev, muxc); 106 107 mux->pinctrl = devm_pinctrl_get(dev); 108 if (IS_ERR(mux->pinctrl)) { 109 ret = PTR_ERR(mux->pinctrl); 110 dev_err(dev, "Cannot get pinctrl: %d\n", ret); 111 goto err_put_parent; 112 } 113 114 for (i = 0; i < num_names; i++) { 115 ret = of_property_read_string_index(np, "pinctrl-names", i, 116 &name); 117 if (ret < 0) { 118 dev_err(dev, "Cannot parse pinctrl-names: %d\n", ret); 119 goto err_put_parent; 120 } 121 122 mux->states[i] = pinctrl_lookup_state(mux->pinctrl, name); 123 if (IS_ERR(mux->states[i])) { 124 ret = PTR_ERR(mux->states[i]); 125 dev_err(dev, "Cannot look up pinctrl state %s: %d\n", 126 name, ret); 127 goto err_put_parent; 128 } 129 130 if (strcmp(name, "idle")) 131 continue; 132 133 if (i != num_names - 1) { 134 dev_err(dev, "idle state must be last\n"); 135 ret = -EINVAL; 136 goto err_put_parent; 137 } 138 muxc->deselect = i2c_mux_pinctrl_deselect; 139 } 140 141 root = i2c_root_adapter(&muxc->parent->dev); 142 143 muxc->mux_locked = true; 144 for (i = 0; i < num_names; i++) { 145 if (root != i2c_mux_pinctrl_root_adapter(mux->states[i])) { 146 muxc->mux_locked = false; 147 break; 148 } 149 } 150 if (muxc->mux_locked) 151 dev_info(dev, "mux-locked i2c mux\n"); 152 153 /* Do not add any adapter for the idle state (if it's there at all). */ 154 for (i = 0; i < num_names - !!muxc->deselect; i++) { 155 ret = i2c_mux_add_adapter(muxc, 0, i, 0); 156 if (ret) 157 goto err_del_adapter; 158 } 159 160 return 0; 161 162 err_del_adapter: 163 i2c_mux_del_adapters(muxc); 164 err_put_parent: 165 i2c_put_adapter(parent); 166 167 return ret; 168 } 169 170 static int i2c_mux_pinctrl_remove(struct platform_device *pdev) 171 { 172 struct i2c_mux_core *muxc = platform_get_drvdata(pdev); 173 174 i2c_mux_del_adapters(muxc); 175 i2c_put_adapter(muxc->parent); 176 177 return 0; 178 } 179 180 static const struct of_device_id i2c_mux_pinctrl_of_match[] = { 181 { .compatible = "i2c-mux-pinctrl", }, 182 {}, 183 }; 184 MODULE_DEVICE_TABLE(of, i2c_mux_pinctrl_of_match); 185 186 static struct platform_driver i2c_mux_pinctrl_driver = { 187 .driver = { 188 .name = "i2c-mux-pinctrl", 189 .of_match_table = of_match_ptr(i2c_mux_pinctrl_of_match), 190 }, 191 .probe = i2c_mux_pinctrl_probe, 192 .remove = i2c_mux_pinctrl_remove, 193 }; 194 module_platform_driver(i2c_mux_pinctrl_driver); 195 196 MODULE_DESCRIPTION("pinctrl-based I2C multiplexer driver"); 197 MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>"); 198 MODULE_LICENSE("GPL v2"); 199 MODULE_ALIAS("platform:i2c-mux-pinctrl"); 200