blob: f1bb00a11ad628d7196494e88a56337989c99c21 [file] [log] [blame]
Thomas Gleixner9952f692019-05-28 10:10:04 -07001// SPDX-License-Identifier: GPL-2.0-only
Stephen Warrenae58d1e2012-05-18 09:29:34 -06002/*
3 * I2C multiplexer using pinctrl API
4 *
5 * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
Stephen Warrenae58d1e2012-05-18 09:29:34 -06006 */
7
8#include <linux/i2c.h>
9#include <linux/i2c-mux.h>
Stephen Warrenae58d1e2012-05-18 09:29:34 -060010#include <linux/module.h>
Stephen Warrenae58d1e2012-05-18 09:29:34 -060011#include <linux/pinctrl/consumer.h>
Stephen Warrenae58d1e2012-05-18 09:29:34 -060012#include <linux/platform_device.h>
13#include <linux/slab.h>
Sachin Kamat4edd65e2013-10-16 15:26:33 +053014#include <linux/of.h>
Peter Rosin6ef91fc2016-05-04 22:15:29 +020015#include "../../pinctrl/core.h"
Stephen Warrenae58d1e2012-05-18 09:29:34 -060016
17struct i2c_mux_pinctrl {
Stephen Warrenae58d1e2012-05-18 09:29:34 -060018 struct pinctrl *pinctrl;
Gustavo A. R. Silva90af2732019-06-03 09:53:35 -050019 struct pinctrl_state *states[];
Stephen Warrenae58d1e2012-05-18 09:29:34 -060020};
21
Peter Rosin4bbe7fb2016-04-20 08:39:31 +020022static int i2c_mux_pinctrl_select(struct i2c_mux_core *muxc, u32 chan)
Stephen Warrenae58d1e2012-05-18 09:29:34 -060023{
Peter Rosin4bbe7fb2016-04-20 08:39:31 +020024 struct i2c_mux_pinctrl *mux = i2c_mux_priv(muxc);
Stephen Warrenae58d1e2012-05-18 09:29:34 -060025
26 return pinctrl_select_state(mux->pinctrl, mux->states[chan]);
27}
28
Peter Rosin4bbe7fb2016-04-20 08:39:31 +020029static int i2c_mux_pinctrl_deselect(struct i2c_mux_core *muxc, u32 chan)
Stephen Warrenae58d1e2012-05-18 09:29:34 -060030{
Peter Rosinfc204672017-05-21 18:49:03 +020031 return i2c_mux_pinctrl_select(muxc, muxc->num_adapters);
Stephen Warrenae58d1e2012-05-18 09:29:34 -060032}
33
Peter Rosin6ef91fc2016-05-04 22:15:29 +020034static 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
Peter Rosinc4aee3e2017-05-21 18:48:07 +020054static 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
Bill Pemberton0b255e92012-11-27 15:59:38 -050073static int i2c_mux_pinctrl_probe(struct platform_device *pdev)
Stephen Warrenae58d1e2012-05-18 09:29:34 -060074{
Peter Rosinc4aee3e2017-05-21 18:48:07 +020075 struct device *dev = &pdev->dev;
76 struct device_node *np = dev->of_node;
Peter Rosin4bbe7fb2016-04-20 08:39:31 +020077 struct i2c_mux_core *muxc;
Stephen Warrenae58d1e2012-05-18 09:29:34 -060078 struct i2c_mux_pinctrl *mux;
Peter Rosinc4aee3e2017-05-21 18:48:07 +020079 struct i2c_adapter *parent;
Peter Rosin6ef91fc2016-05-04 22:15:29 +020080 struct i2c_adapter *root;
Peter Rosinc4aee3e2017-05-21 18:48:07 +020081 int num_names, i, ret;
82 const char *name;
Stephen Warrenae58d1e2012-05-18 09:29:34 -060083
Peter Rosinc4aee3e2017-05-21 18:48:07 +020084 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;
Stephen Warrenae58d1e2012-05-18 09:29:34 -060089 }
Stephen Warrenae58d1e2012-05-18 09:29:34 -060090
Peter Rosinc4aee3e2017-05-21 18:48:07 +020091 parent = i2c_mux_pinctrl_parent_adapter(dev);
92 if (IS_ERR(parent))
93 return PTR_ERR(parent);
Stephen Warrenae58d1e2012-05-18 09:29:34 -060094
Peter Rosinc4aee3e2017-05-21 18:48:07 +020095 muxc = i2c_mux_alloc(parent, dev, num_names,
Gustavo A. R. Silva90af2732019-06-03 09:53:35 -050096 struct_size(mux, states, num_names),
Peter Rosinc4aee3e2017-05-21 18:48:07 +020097 0, i2c_mux_pinctrl_select, NULL);
Peter Rosin4bbe7fb2016-04-20 08:39:31 +020098 if (!muxc) {
Stephen Warrenae58d1e2012-05-18 09:29:34 -060099 ret = -ENOMEM;
Peter Rosinc4aee3e2017-05-21 18:48:07 +0200100 goto err_put_parent;
Stephen Warrenae58d1e2012-05-18 09:29:34 -0600101 }
Peter Rosinc4aee3e2017-05-21 18:48:07 +0200102 mux = i2c_mux_priv(muxc);
Peter Rosin4bbe7fb2016-04-20 08:39:31 +0200103
104 platform_set_drvdata(pdev, muxc);
Stephen Warrenae58d1e2012-05-18 09:29:34 -0600105
Peter Rosinc4aee3e2017-05-21 18:48:07 +0200106 mux->pinctrl = devm_pinctrl_get(dev);
Stephen Warrenae58d1e2012-05-18 09:29:34 -0600107 if (IS_ERR(mux->pinctrl)) {
108 ret = PTR_ERR(mux->pinctrl);
Peter Rosinc4aee3e2017-05-21 18:48:07 +0200109 dev_err(dev, "Cannot get pinctrl: %d\n", ret);
110 goto err_put_parent;
Stephen Warrenae58d1e2012-05-18 09:29:34 -0600111 }
Peter Rosinc4aee3e2017-05-21 18:48:07 +0200112
113 for (i = 0; i < num_names; i++) {
114 ret = of_property_read_string_index(np, "pinctrl-names", i,
115 &name);
116 if (ret < 0) {
117 dev_err(dev, "Cannot parse pinctrl-names: %d\n", ret);
118 goto err_put_parent;
119 }
120
121 mux->states[i] = pinctrl_lookup_state(mux->pinctrl, name);
Wolfram Sang2c89e5d2016-04-22 15:44:07 +0200122 if (IS_ERR(mux->states[i])) {
123 ret = PTR_ERR(mux->states[i]);
Peter Rosinc4aee3e2017-05-21 18:48:07 +0200124 dev_err(dev, "Cannot look up pinctrl state %s: %d\n",
125 name, ret);
126 goto err_put_parent;
Stephen Warrenae58d1e2012-05-18 09:29:34 -0600127 }
128
Peter Rosinc4aee3e2017-05-21 18:48:07 +0200129 if (strcmp(name, "idle"))
130 continue;
131
132 if (i != num_names - 1) {
133 dev_err(dev, "idle state must be last\n");
134 ret = -EINVAL;
135 goto err_put_parent;
136 }
Peter Rosin4bbe7fb2016-04-20 08:39:31 +0200137 muxc->deselect = i2c_mux_pinctrl_deselect;
Stephen Warrenae58d1e2012-05-18 09:29:34 -0600138 }
139
Peter Rosin6ef91fc2016-05-04 22:15:29 +0200140 root = i2c_root_adapter(&muxc->parent->dev);
141
142 muxc->mux_locked = true;
Peter Rosinc4aee3e2017-05-21 18:48:07 +0200143 for (i = 0; i < num_names; i++) {
Peter Rosin6ef91fc2016-05-04 22:15:29 +0200144 if (root != i2c_mux_pinctrl_root_adapter(mux->states[i])) {
145 muxc->mux_locked = false;
146 break;
147 }
148 }
Peter Rosin6ef91fc2016-05-04 22:15:29 +0200149 if (muxc->mux_locked)
Peter Rosinc4aee3e2017-05-21 18:48:07 +0200150 dev_info(dev, "mux-locked i2c mux\n");
Peter Rosin6ef91fc2016-05-04 22:15:29 +0200151
Peter Rosinc4aee3e2017-05-21 18:48:07 +0200152 /* Do not add any adapter for the idle state (if it's there at all). */
Peter Rosinfc204672017-05-21 18:49:03 +0200153 for (i = 0; i < num_names - !!muxc->deselect; i++) {
Peter Rosinc4aee3e2017-05-21 18:48:07 +0200154 ret = i2c_mux_add_adapter(muxc, 0, i, 0);
Peter Rosinc99a23e2017-04-03 10:14:25 +0200155 if (ret)
Stephen Warrenae58d1e2012-05-18 09:29:34 -0600156 goto err_del_adapter;
Stephen Warrenae58d1e2012-05-18 09:29:34 -0600157 }
158
159 return 0;
160
161err_del_adapter:
Peter Rosin4bbe7fb2016-04-20 08:39:31 +0200162 i2c_mux_del_adapters(muxc);
Peter Rosinc4aee3e2017-05-21 18:48:07 +0200163err_put_parent:
Dan Carpenterbe9bac02017-08-17 14:53:55 +0300164 i2c_put_adapter(parent);
Peter Rosinc4aee3e2017-05-21 18:48:07 +0200165
Stephen Warrenae58d1e2012-05-18 09:29:34 -0600166 return ret;
167}
168
Bill Pemberton0b255e92012-11-27 15:59:38 -0500169static int i2c_mux_pinctrl_remove(struct platform_device *pdev)
Stephen Warrenae58d1e2012-05-18 09:29:34 -0600170{
Peter Rosin4bbe7fb2016-04-20 08:39:31 +0200171 struct i2c_mux_core *muxc = platform_get_drvdata(pdev);
Stephen Warrenae58d1e2012-05-18 09:29:34 -0600172
Peter Rosin4bbe7fb2016-04-20 08:39:31 +0200173 i2c_mux_del_adapters(muxc);
174 i2c_put_adapter(muxc->parent);
Peter Rosinc4aee3e2017-05-21 18:48:07 +0200175
Stephen Warrenae58d1e2012-05-18 09:29:34 -0600176 return 0;
177}
178
Bill Pemberton0b255e92012-11-27 15:59:38 -0500179static const struct of_device_id i2c_mux_pinctrl_of_match[] = {
Stephen Warrenae58d1e2012-05-18 09:29:34 -0600180 { .compatible = "i2c-mux-pinctrl", },
181 {},
182};
183MODULE_DEVICE_TABLE(of, i2c_mux_pinctrl_of_match);
Stephen Warrenae58d1e2012-05-18 09:29:34 -0600184
185static struct platform_driver i2c_mux_pinctrl_driver = {
186 .driver = {
187 .name = "i2c-mux-pinctrl",
Stephen Warrenae58d1e2012-05-18 09:29:34 -0600188 .of_match_table = of_match_ptr(i2c_mux_pinctrl_of_match),
189 },
190 .probe = i2c_mux_pinctrl_probe,
Bill Pemberton0b255e92012-11-27 15:59:38 -0500191 .remove = i2c_mux_pinctrl_remove,
Stephen Warrenae58d1e2012-05-18 09:29:34 -0600192};
193module_platform_driver(i2c_mux_pinctrl_driver);
194
195MODULE_DESCRIPTION("pinctrl-based I2C multiplexer driver");
196MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
197MODULE_LICENSE("GPL v2");
198MODULE_ALIAS("platform:i2c-mux-pinctrl");