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