Thomas Gleixner | 9952f69 | 2019-05-28 10:10:04 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Stephen Warren | ae58d1e | 2012-05-18 09:29:34 -0600 | [diff] [blame] | 2 | /* |
| 3 | * I2C multiplexer using pinctrl API |
| 4 | * |
| 5 | * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved. |
Stephen Warren | ae58d1e | 2012-05-18 09:29:34 -0600 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <linux/i2c.h> |
| 9 | #include <linux/i2c-mux.h> |
Stephen Warren | ae58d1e | 2012-05-18 09:29:34 -0600 | [diff] [blame] | 10 | #include <linux/module.h> |
Stephen Warren | ae58d1e | 2012-05-18 09:29:34 -0600 | [diff] [blame] | 11 | #include <linux/pinctrl/consumer.h> |
Stephen Warren | ae58d1e | 2012-05-18 09:29:34 -0600 | [diff] [blame] | 12 | #include <linux/platform_device.h> |
| 13 | #include <linux/slab.h> |
Sachin Kamat | 4edd65e | 2013-10-16 15:26:33 +0530 | [diff] [blame] | 14 | #include <linux/of.h> |
Peter Rosin | 6ef91fc | 2016-05-04 22:15:29 +0200 | [diff] [blame] | 15 | #include "../../pinctrl/core.h" |
Stephen Warren | ae58d1e | 2012-05-18 09:29:34 -0600 | [diff] [blame] | 16 | |
| 17 | struct i2c_mux_pinctrl { |
Stephen Warren | ae58d1e | 2012-05-18 09:29:34 -0600 | [diff] [blame] | 18 | struct pinctrl *pinctrl; |
Gustavo A. R. Silva | 90af273 | 2019-06-03 09:53:35 -0500 | [diff] [blame] | 19 | struct pinctrl_state *states[]; |
Stephen Warren | ae58d1e | 2012-05-18 09:29:34 -0600 | [diff] [blame] | 20 | }; |
| 21 | |
Peter Rosin | 4bbe7fb | 2016-04-20 08:39:31 +0200 | [diff] [blame] | 22 | static int i2c_mux_pinctrl_select(struct i2c_mux_core *muxc, u32 chan) |
Stephen Warren | ae58d1e | 2012-05-18 09:29:34 -0600 | [diff] [blame] | 23 | { |
Peter Rosin | 4bbe7fb | 2016-04-20 08:39:31 +0200 | [diff] [blame] | 24 | struct i2c_mux_pinctrl *mux = i2c_mux_priv(muxc); |
Stephen Warren | ae58d1e | 2012-05-18 09:29:34 -0600 | [diff] [blame] | 25 | |
| 26 | return pinctrl_select_state(mux->pinctrl, mux->states[chan]); |
| 27 | } |
| 28 | |
Peter Rosin | 4bbe7fb | 2016-04-20 08:39:31 +0200 | [diff] [blame] | 29 | static int i2c_mux_pinctrl_deselect(struct i2c_mux_core *muxc, u32 chan) |
Stephen Warren | ae58d1e | 2012-05-18 09:29:34 -0600 | [diff] [blame] | 30 | { |
Peter Rosin | fc20467 | 2017-05-21 18:49:03 +0200 | [diff] [blame] | 31 | return i2c_mux_pinctrl_select(muxc, muxc->num_adapters); |
Stephen Warren | ae58d1e | 2012-05-18 09:29:34 -0600 | [diff] [blame] | 32 | } |
| 33 | |
Peter Rosin | 6ef91fc | 2016-05-04 22:15:29 +0200 | [diff] [blame] | 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 | |
Peter Rosin | c4aee3e | 2017-05-21 18:48:07 +0200 | [diff] [blame] | 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 | |
Bill Pemberton | 0b255e9 | 2012-11-27 15:59:38 -0500 | [diff] [blame] | 73 | static int i2c_mux_pinctrl_probe(struct platform_device *pdev) |
Stephen Warren | ae58d1e | 2012-05-18 09:29:34 -0600 | [diff] [blame] | 74 | { |
Peter Rosin | c4aee3e | 2017-05-21 18:48:07 +0200 | [diff] [blame] | 75 | struct device *dev = &pdev->dev; |
| 76 | struct device_node *np = dev->of_node; |
Peter Rosin | 4bbe7fb | 2016-04-20 08:39:31 +0200 | [diff] [blame] | 77 | struct i2c_mux_core *muxc; |
Stephen Warren | ae58d1e | 2012-05-18 09:29:34 -0600 | [diff] [blame] | 78 | struct i2c_mux_pinctrl *mux; |
Peter Rosin | c4aee3e | 2017-05-21 18:48:07 +0200 | [diff] [blame] | 79 | struct i2c_adapter *parent; |
Peter Rosin | 6ef91fc | 2016-05-04 22:15:29 +0200 | [diff] [blame] | 80 | struct i2c_adapter *root; |
Peter Rosin | c4aee3e | 2017-05-21 18:48:07 +0200 | [diff] [blame] | 81 | int num_names, i, ret; |
| 82 | const char *name; |
Stephen Warren | ae58d1e | 2012-05-18 09:29:34 -0600 | [diff] [blame] | 83 | |
Peter Rosin | c4aee3e | 2017-05-21 18:48:07 +0200 | [diff] [blame] | 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; |
Stephen Warren | ae58d1e | 2012-05-18 09:29:34 -0600 | [diff] [blame] | 89 | } |
Stephen Warren | ae58d1e | 2012-05-18 09:29:34 -0600 | [diff] [blame] | 90 | |
Peter Rosin | c4aee3e | 2017-05-21 18:48:07 +0200 | [diff] [blame] | 91 | parent = i2c_mux_pinctrl_parent_adapter(dev); |
| 92 | if (IS_ERR(parent)) |
| 93 | return PTR_ERR(parent); |
Stephen Warren | ae58d1e | 2012-05-18 09:29:34 -0600 | [diff] [blame] | 94 | |
Peter Rosin | c4aee3e | 2017-05-21 18:48:07 +0200 | [diff] [blame] | 95 | muxc = i2c_mux_alloc(parent, dev, num_names, |
Gustavo A. R. Silva | 90af273 | 2019-06-03 09:53:35 -0500 | [diff] [blame] | 96 | struct_size(mux, states, num_names), |
Peter Rosin | c4aee3e | 2017-05-21 18:48:07 +0200 | [diff] [blame] | 97 | 0, i2c_mux_pinctrl_select, NULL); |
Peter Rosin | 4bbe7fb | 2016-04-20 08:39:31 +0200 | [diff] [blame] | 98 | if (!muxc) { |
Stephen Warren | ae58d1e | 2012-05-18 09:29:34 -0600 | [diff] [blame] | 99 | ret = -ENOMEM; |
Peter Rosin | c4aee3e | 2017-05-21 18:48:07 +0200 | [diff] [blame] | 100 | goto err_put_parent; |
Stephen Warren | ae58d1e | 2012-05-18 09:29:34 -0600 | [diff] [blame] | 101 | } |
Peter Rosin | c4aee3e | 2017-05-21 18:48:07 +0200 | [diff] [blame] | 102 | mux = i2c_mux_priv(muxc); |
Peter Rosin | 4bbe7fb | 2016-04-20 08:39:31 +0200 | [diff] [blame] | 103 | |
| 104 | platform_set_drvdata(pdev, muxc); |
Stephen Warren | ae58d1e | 2012-05-18 09:29:34 -0600 | [diff] [blame] | 105 | |
Peter Rosin | c4aee3e | 2017-05-21 18:48:07 +0200 | [diff] [blame] | 106 | mux->pinctrl = devm_pinctrl_get(dev); |
Stephen Warren | ae58d1e | 2012-05-18 09:29:34 -0600 | [diff] [blame] | 107 | if (IS_ERR(mux->pinctrl)) { |
| 108 | ret = PTR_ERR(mux->pinctrl); |
Peter Rosin | c4aee3e | 2017-05-21 18:48:07 +0200 | [diff] [blame] | 109 | dev_err(dev, "Cannot get pinctrl: %d\n", ret); |
| 110 | goto err_put_parent; |
Stephen Warren | ae58d1e | 2012-05-18 09:29:34 -0600 | [diff] [blame] | 111 | } |
Peter Rosin | c4aee3e | 2017-05-21 18:48:07 +0200 | [diff] [blame] | 112 | |
| 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 Sang | 2c89e5d | 2016-04-22 15:44:07 +0200 | [diff] [blame] | 122 | if (IS_ERR(mux->states[i])) { |
| 123 | ret = PTR_ERR(mux->states[i]); |
Peter Rosin | c4aee3e | 2017-05-21 18:48:07 +0200 | [diff] [blame] | 124 | dev_err(dev, "Cannot look up pinctrl state %s: %d\n", |
| 125 | name, ret); |
| 126 | goto err_put_parent; |
Stephen Warren | ae58d1e | 2012-05-18 09:29:34 -0600 | [diff] [blame] | 127 | } |
| 128 | |
Peter Rosin | c4aee3e | 2017-05-21 18:48:07 +0200 | [diff] [blame] | 129 | 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 Rosin | 4bbe7fb | 2016-04-20 08:39:31 +0200 | [diff] [blame] | 137 | muxc->deselect = i2c_mux_pinctrl_deselect; |
Stephen Warren | ae58d1e | 2012-05-18 09:29:34 -0600 | [diff] [blame] | 138 | } |
| 139 | |
Peter Rosin | 6ef91fc | 2016-05-04 22:15:29 +0200 | [diff] [blame] | 140 | root = i2c_root_adapter(&muxc->parent->dev); |
| 141 | |
| 142 | muxc->mux_locked = true; |
Peter Rosin | c4aee3e | 2017-05-21 18:48:07 +0200 | [diff] [blame] | 143 | for (i = 0; i < num_names; i++) { |
Peter Rosin | 6ef91fc | 2016-05-04 22:15:29 +0200 | [diff] [blame] | 144 | if (root != i2c_mux_pinctrl_root_adapter(mux->states[i])) { |
| 145 | muxc->mux_locked = false; |
| 146 | break; |
| 147 | } |
| 148 | } |
Peter Rosin | 6ef91fc | 2016-05-04 22:15:29 +0200 | [diff] [blame] | 149 | if (muxc->mux_locked) |
Peter Rosin | c4aee3e | 2017-05-21 18:48:07 +0200 | [diff] [blame] | 150 | dev_info(dev, "mux-locked i2c mux\n"); |
Peter Rosin | 6ef91fc | 2016-05-04 22:15:29 +0200 | [diff] [blame] | 151 | |
Peter Rosin | c4aee3e | 2017-05-21 18:48:07 +0200 | [diff] [blame] | 152 | /* Do not add any adapter for the idle state (if it's there at all). */ |
Peter Rosin | fc20467 | 2017-05-21 18:49:03 +0200 | [diff] [blame] | 153 | for (i = 0; i < num_names - !!muxc->deselect; i++) { |
Peter Rosin | c4aee3e | 2017-05-21 18:48:07 +0200 | [diff] [blame] | 154 | ret = i2c_mux_add_adapter(muxc, 0, i, 0); |
Peter Rosin | c99a23e | 2017-04-03 10:14:25 +0200 | [diff] [blame] | 155 | if (ret) |
Stephen Warren | ae58d1e | 2012-05-18 09:29:34 -0600 | [diff] [blame] | 156 | goto err_del_adapter; |
Stephen Warren | ae58d1e | 2012-05-18 09:29:34 -0600 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | return 0; |
| 160 | |
| 161 | err_del_adapter: |
Peter Rosin | 4bbe7fb | 2016-04-20 08:39:31 +0200 | [diff] [blame] | 162 | i2c_mux_del_adapters(muxc); |
Peter Rosin | c4aee3e | 2017-05-21 18:48:07 +0200 | [diff] [blame] | 163 | err_put_parent: |
Dan Carpenter | be9bac0 | 2017-08-17 14:53:55 +0300 | [diff] [blame] | 164 | i2c_put_adapter(parent); |
Peter Rosin | c4aee3e | 2017-05-21 18:48:07 +0200 | [diff] [blame] | 165 | |
Stephen Warren | ae58d1e | 2012-05-18 09:29:34 -0600 | [diff] [blame] | 166 | return ret; |
| 167 | } |
| 168 | |
Bill Pemberton | 0b255e9 | 2012-11-27 15:59:38 -0500 | [diff] [blame] | 169 | static int i2c_mux_pinctrl_remove(struct platform_device *pdev) |
Stephen Warren | ae58d1e | 2012-05-18 09:29:34 -0600 | [diff] [blame] | 170 | { |
Peter Rosin | 4bbe7fb | 2016-04-20 08:39:31 +0200 | [diff] [blame] | 171 | struct i2c_mux_core *muxc = platform_get_drvdata(pdev); |
Stephen Warren | ae58d1e | 2012-05-18 09:29:34 -0600 | [diff] [blame] | 172 | |
Peter Rosin | 4bbe7fb | 2016-04-20 08:39:31 +0200 | [diff] [blame] | 173 | i2c_mux_del_adapters(muxc); |
| 174 | i2c_put_adapter(muxc->parent); |
Peter Rosin | c4aee3e | 2017-05-21 18:48:07 +0200 | [diff] [blame] | 175 | |
Stephen Warren | ae58d1e | 2012-05-18 09:29:34 -0600 | [diff] [blame] | 176 | return 0; |
| 177 | } |
| 178 | |
Bill Pemberton | 0b255e9 | 2012-11-27 15:59:38 -0500 | [diff] [blame] | 179 | static const struct of_device_id i2c_mux_pinctrl_of_match[] = { |
Stephen Warren | ae58d1e | 2012-05-18 09:29:34 -0600 | [diff] [blame] | 180 | { .compatible = "i2c-mux-pinctrl", }, |
| 181 | {}, |
| 182 | }; |
| 183 | MODULE_DEVICE_TABLE(of, i2c_mux_pinctrl_of_match); |
Stephen Warren | ae58d1e | 2012-05-18 09:29:34 -0600 | [diff] [blame] | 184 | |
| 185 | static struct platform_driver i2c_mux_pinctrl_driver = { |
| 186 | .driver = { |
| 187 | .name = "i2c-mux-pinctrl", |
Stephen Warren | ae58d1e | 2012-05-18 09:29:34 -0600 | [diff] [blame] | 188 | .of_match_table = of_match_ptr(i2c_mux_pinctrl_of_match), |
| 189 | }, |
| 190 | .probe = i2c_mux_pinctrl_probe, |
Bill Pemberton | 0b255e9 | 2012-11-27 15:59:38 -0500 | [diff] [blame] | 191 | .remove = i2c_mux_pinctrl_remove, |
Stephen Warren | ae58d1e | 2012-05-18 09:29:34 -0600 | [diff] [blame] | 192 | }; |
| 193 | module_platform_driver(i2c_mux_pinctrl_driver); |
| 194 | |
| 195 | MODULE_DESCRIPTION("pinctrl-based I2C multiplexer driver"); |
| 196 | MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>"); |
| 197 | MODULE_LICENSE("GPL v2"); |
| 198 | MODULE_ALIAS("platform:i2c-mux-pinctrl"); |