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