Stephen Warren | ae58d1e | 2012-05-18 09:29:34 -0600 | [diff] [blame] | 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> |
Stephen Warren | ae58d1e | 2012-05-18 09:29:34 -0600 | [diff] [blame] | 21 | #include <linux/module.h> |
Stephen Warren | ae58d1e | 2012-05-18 09:29:34 -0600 | [diff] [blame] | 22 | #include <linux/pinctrl/consumer.h> |
| 23 | #include <linux/i2c-mux-pinctrl.h> |
| 24 | #include <linux/platform_device.h> |
| 25 | #include <linux/slab.h> |
Sachin Kamat | 4edd65e | 2013-10-16 15:26:33 +0530 | [diff] [blame] | 26 | #include <linux/of.h> |
Stephen Warren | ae58d1e | 2012-05-18 09:29:34 -0600 | [diff] [blame] | 27 | |
| 28 | struct i2c_mux_pinctrl { |
Stephen Warren | ae58d1e | 2012-05-18 09:29:34 -0600 | [diff] [blame] | 29 | struct i2c_mux_pinctrl_platform_data *pdata; |
| 30 | struct pinctrl *pinctrl; |
| 31 | struct pinctrl_state **states; |
| 32 | struct pinctrl_state *state_idle; |
Stephen Warren | ae58d1e | 2012-05-18 09:29:34 -0600 | [diff] [blame] | 33 | }; |
| 34 | |
Peter Rosin | 4bbe7fb | 2016-04-20 08:39:31 +0200 | [diff] [blame] | 35 | 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] | 36 | { |
Peter Rosin | 4bbe7fb | 2016-04-20 08:39:31 +0200 | [diff] [blame] | 37 | struct i2c_mux_pinctrl *mux = i2c_mux_priv(muxc); |
Stephen Warren | ae58d1e | 2012-05-18 09:29:34 -0600 | [diff] [blame] | 38 | |
| 39 | return pinctrl_select_state(mux->pinctrl, mux->states[chan]); |
| 40 | } |
| 41 | |
Peter Rosin | 4bbe7fb | 2016-04-20 08:39:31 +0200 | [diff] [blame] | 42 | 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] | 43 | { |
Peter Rosin | 4bbe7fb | 2016-04-20 08:39:31 +0200 | [diff] [blame] | 44 | struct i2c_mux_pinctrl *mux = i2c_mux_priv(muxc); |
Stephen Warren | ae58d1e | 2012-05-18 09:29:34 -0600 | [diff] [blame] | 45 | |
| 46 | return pinctrl_select_state(mux->pinctrl, mux->state_idle); |
| 47 | } |
| 48 | |
| 49 | #ifdef CONFIG_OF |
| 50 | static int i2c_mux_pinctrl_parse_dt(struct i2c_mux_pinctrl *mux, |
Peter Rosin | 4bbe7fb | 2016-04-20 08:39:31 +0200 | [diff] [blame] | 51 | struct platform_device *pdev) |
Stephen Warren | ae58d1e | 2012-05-18 09:29:34 -0600 | [diff] [blame] | 52 | { |
| 53 | struct device_node *np = pdev->dev.of_node; |
| 54 | int num_names, i, ret; |
| 55 | struct device_node *adapter_np; |
| 56 | struct i2c_adapter *adapter; |
| 57 | |
| 58 | if (!np) |
| 59 | return 0; |
| 60 | |
| 61 | mux->pdata = devm_kzalloc(&pdev->dev, sizeof(*mux->pdata), GFP_KERNEL); |
Peter Rosin | 4bbe7fb | 2016-04-20 08:39:31 +0200 | [diff] [blame] | 62 | if (!mux->pdata) |
Stephen Warren | ae58d1e | 2012-05-18 09:29:34 -0600 | [diff] [blame] | 63 | return -ENOMEM; |
Stephen Warren | ae58d1e | 2012-05-18 09:29:34 -0600 | [diff] [blame] | 64 | |
| 65 | num_names = of_property_count_strings(np, "pinctrl-names"); |
| 66 | if (num_names < 0) { |
Peter Rosin | 4bbe7fb | 2016-04-20 08:39:31 +0200 | [diff] [blame] | 67 | dev_err(&pdev->dev, "Cannot parse pinctrl-names: %d\n", |
Stephen Warren | ae58d1e | 2012-05-18 09:29:34 -0600 | [diff] [blame] | 68 | num_names); |
| 69 | return num_names; |
| 70 | } |
| 71 | |
| 72 | mux->pdata->pinctrl_states = devm_kzalloc(&pdev->dev, |
| 73 | sizeof(*mux->pdata->pinctrl_states) * num_names, |
| 74 | GFP_KERNEL); |
Peter Rosin | 4bbe7fb | 2016-04-20 08:39:31 +0200 | [diff] [blame] | 75 | if (!mux->pdata->pinctrl_states) |
Stephen Warren | ae58d1e | 2012-05-18 09:29:34 -0600 | [diff] [blame] | 76 | return -ENOMEM; |
Stephen Warren | ae58d1e | 2012-05-18 09:29:34 -0600 | [diff] [blame] | 77 | |
| 78 | for (i = 0; i < num_names; i++) { |
| 79 | ret = of_property_read_string_index(np, "pinctrl-names", i, |
| 80 | &mux->pdata->pinctrl_states[mux->pdata->bus_count]); |
| 81 | if (ret < 0) { |
Peter Rosin | 4bbe7fb | 2016-04-20 08:39:31 +0200 | [diff] [blame] | 82 | dev_err(&pdev->dev, "Cannot parse pinctrl-names: %d\n", |
Stephen Warren | ae58d1e | 2012-05-18 09:29:34 -0600 | [diff] [blame] | 83 | ret); |
| 84 | return ret; |
| 85 | } |
| 86 | if (!strcmp(mux->pdata->pinctrl_states[mux->pdata->bus_count], |
| 87 | "idle")) { |
| 88 | if (i != num_names - 1) { |
Peter Rosin | 4bbe7fb | 2016-04-20 08:39:31 +0200 | [diff] [blame] | 89 | dev_err(&pdev->dev, |
| 90 | "idle state must be last\n"); |
Stephen Warren | ae58d1e | 2012-05-18 09:29:34 -0600 | [diff] [blame] | 91 | return -EINVAL; |
| 92 | } |
| 93 | mux->pdata->pinctrl_state_idle = "idle"; |
| 94 | } else { |
| 95 | mux->pdata->bus_count++; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | adapter_np = of_parse_phandle(np, "i2c-parent", 0); |
| 100 | if (!adapter_np) { |
Peter Rosin | 4bbe7fb | 2016-04-20 08:39:31 +0200 | [diff] [blame] | 101 | dev_err(&pdev->dev, "Cannot parse i2c-parent\n"); |
Stephen Warren | ae58d1e | 2012-05-18 09:29:34 -0600 | [diff] [blame] | 102 | return -ENODEV; |
| 103 | } |
| 104 | adapter = of_find_i2c_adapter_by_node(adapter_np); |
Vladimir Zapolskiy | bdbf4a2 | 2015-08-26 23:59:33 +0300 | [diff] [blame] | 105 | of_node_put(adapter_np); |
Stephen Warren | ae58d1e | 2012-05-18 09:29:34 -0600 | [diff] [blame] | 106 | if (!adapter) { |
Peter Rosin | 4bbe7fb | 2016-04-20 08:39:31 +0200 | [diff] [blame] | 107 | dev_err(&pdev->dev, "Cannot find parent bus\n"); |
Wolfram Sang | 2737de4 | 2013-10-10 10:19:13 +0200 | [diff] [blame] | 108 | return -EPROBE_DEFER; |
Stephen Warren | ae58d1e | 2012-05-18 09:29:34 -0600 | [diff] [blame] | 109 | } |
| 110 | mux->pdata->parent_bus_num = i2c_adapter_id(adapter); |
| 111 | put_device(&adapter->dev); |
| 112 | |
| 113 | return 0; |
| 114 | } |
| 115 | #else |
| 116 | static inline int i2c_mux_pinctrl_parse_dt(struct i2c_mux_pinctrl *mux, |
| 117 | struct platform_device *pdev) |
| 118 | { |
| 119 | return 0; |
| 120 | } |
| 121 | #endif |
| 122 | |
Bill Pemberton | 0b255e9 | 2012-11-27 15:59:38 -0500 | [diff] [blame] | 123 | static int i2c_mux_pinctrl_probe(struct platform_device *pdev) |
Stephen Warren | ae58d1e | 2012-05-18 09:29:34 -0600 | [diff] [blame] | 124 | { |
Peter Rosin | 4bbe7fb | 2016-04-20 08:39:31 +0200 | [diff] [blame] | 125 | struct i2c_mux_core *muxc; |
Stephen Warren | ae58d1e | 2012-05-18 09:29:34 -0600 | [diff] [blame] | 126 | struct i2c_mux_pinctrl *mux; |
Stephen Warren | ae58d1e | 2012-05-18 09:29:34 -0600 | [diff] [blame] | 127 | int i, ret; |
| 128 | |
| 129 | mux = devm_kzalloc(&pdev->dev, sizeof(*mux), GFP_KERNEL); |
| 130 | if (!mux) { |
Stephen Warren | ae58d1e | 2012-05-18 09:29:34 -0600 | [diff] [blame] | 131 | ret = -ENOMEM; |
| 132 | goto err; |
| 133 | } |
Stephen Warren | ae58d1e | 2012-05-18 09:29:34 -0600 | [diff] [blame] | 134 | |
Jingoo Han | 6d4028c | 2013-07-30 16:59:33 +0900 | [diff] [blame] | 135 | mux->pdata = dev_get_platdata(&pdev->dev); |
Stephen Warren | ae58d1e | 2012-05-18 09:29:34 -0600 | [diff] [blame] | 136 | if (!mux->pdata) { |
| 137 | ret = i2c_mux_pinctrl_parse_dt(mux, pdev); |
| 138 | if (ret < 0) |
| 139 | goto err; |
| 140 | } |
| 141 | if (!mux->pdata) { |
| 142 | dev_err(&pdev->dev, "Missing platform data\n"); |
| 143 | ret = -ENODEV; |
| 144 | goto err; |
| 145 | } |
| 146 | |
| 147 | mux->states = devm_kzalloc(&pdev->dev, |
| 148 | sizeof(*mux->states) * mux->pdata->bus_count, |
| 149 | GFP_KERNEL); |
| 150 | if (!mux->states) { |
| 151 | dev_err(&pdev->dev, "Cannot allocate states\n"); |
| 152 | ret = -ENOMEM; |
| 153 | goto err; |
| 154 | } |
| 155 | |
Peter Rosin | 4bbe7fb | 2016-04-20 08:39:31 +0200 | [diff] [blame] | 156 | muxc = i2c_mux_alloc(NULL, &pdev->dev, mux->pdata->bus_count, 0, 0, |
| 157 | i2c_mux_pinctrl_select, NULL); |
| 158 | if (!muxc) { |
Stephen Warren | ae58d1e | 2012-05-18 09:29:34 -0600 | [diff] [blame] | 159 | ret = -ENOMEM; |
| 160 | goto err; |
| 161 | } |
Peter Rosin | 4bbe7fb | 2016-04-20 08:39:31 +0200 | [diff] [blame] | 162 | muxc->priv = mux; |
| 163 | |
| 164 | platform_set_drvdata(pdev, muxc); |
Stephen Warren | ae58d1e | 2012-05-18 09:29:34 -0600 | [diff] [blame] | 165 | |
| 166 | mux->pinctrl = devm_pinctrl_get(&pdev->dev); |
| 167 | if (IS_ERR(mux->pinctrl)) { |
| 168 | ret = PTR_ERR(mux->pinctrl); |
| 169 | dev_err(&pdev->dev, "Cannot get pinctrl: %d\n", ret); |
| 170 | goto err; |
| 171 | } |
| 172 | for (i = 0; i < mux->pdata->bus_count; i++) { |
| 173 | mux->states[i] = pinctrl_lookup_state(mux->pinctrl, |
| 174 | mux->pdata->pinctrl_states[i]); |
Wolfram Sang | 2c89e5d | 2016-04-22 15:44:07 +0200 | [diff] [blame^] | 175 | if (IS_ERR(mux->states[i])) { |
| 176 | ret = PTR_ERR(mux->states[i]); |
| 177 | dev_err(&pdev->dev, |
| 178 | "Cannot look up pinctrl state %s: %d\n", |
| 179 | mux->pdata->pinctrl_states[i], ret); |
| 180 | goto err; |
| 181 | } |
Stephen Warren | ae58d1e | 2012-05-18 09:29:34 -0600 | [diff] [blame] | 182 | } |
| 183 | if (mux->pdata->pinctrl_state_idle) { |
| 184 | mux->state_idle = pinctrl_lookup_state(mux->pinctrl, |
| 185 | mux->pdata->pinctrl_state_idle); |
| 186 | if (IS_ERR(mux->state_idle)) { |
| 187 | ret = PTR_ERR(mux->state_idle); |
| 188 | dev_err(&pdev->dev, |
| 189 | "Cannot look up pinctrl state %s: %d\n", |
| 190 | mux->pdata->pinctrl_state_idle, ret); |
| 191 | goto err; |
| 192 | } |
| 193 | |
Peter Rosin | 4bbe7fb | 2016-04-20 08:39:31 +0200 | [diff] [blame] | 194 | muxc->deselect = i2c_mux_pinctrl_deselect; |
Stephen Warren | ae58d1e | 2012-05-18 09:29:34 -0600 | [diff] [blame] | 195 | } |
| 196 | |
Peter Rosin | 4bbe7fb | 2016-04-20 08:39:31 +0200 | [diff] [blame] | 197 | muxc->parent = i2c_get_adapter(mux->pdata->parent_bus_num); |
| 198 | if (!muxc->parent) { |
Stephen Warren | ae58d1e | 2012-05-18 09:29:34 -0600 | [diff] [blame] | 199 | dev_err(&pdev->dev, "Parent adapter (%d) not found\n", |
| 200 | mux->pdata->parent_bus_num); |
Wolfram Sang | 2737de4 | 2013-10-10 10:19:13 +0200 | [diff] [blame] | 201 | ret = -EPROBE_DEFER; |
Stephen Warren | ae58d1e | 2012-05-18 09:29:34 -0600 | [diff] [blame] | 202 | goto err; |
| 203 | } |
| 204 | |
| 205 | for (i = 0; i < mux->pdata->bus_count; i++) { |
| 206 | u32 bus = mux->pdata->base_bus_num ? |
| 207 | (mux->pdata->base_bus_num + i) : 0; |
| 208 | |
Peter Rosin | 4bbe7fb | 2016-04-20 08:39:31 +0200 | [diff] [blame] | 209 | ret = i2c_mux_add_adapter(muxc, bus, i, 0); |
| 210 | if (ret) { |
Stephen Warren | ae58d1e | 2012-05-18 09:29:34 -0600 | [diff] [blame] | 211 | dev_err(&pdev->dev, "Failed to add adapter %d\n", i); |
| 212 | goto err_del_adapter; |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | return 0; |
| 217 | |
| 218 | err_del_adapter: |
Peter Rosin | 4bbe7fb | 2016-04-20 08:39:31 +0200 | [diff] [blame] | 219 | i2c_mux_del_adapters(muxc); |
| 220 | i2c_put_adapter(muxc->parent); |
Stephen Warren | ae58d1e | 2012-05-18 09:29:34 -0600 | [diff] [blame] | 221 | err: |
| 222 | return ret; |
| 223 | } |
| 224 | |
Bill Pemberton | 0b255e9 | 2012-11-27 15:59:38 -0500 | [diff] [blame] | 225 | static int i2c_mux_pinctrl_remove(struct platform_device *pdev) |
Stephen Warren | ae58d1e | 2012-05-18 09:29:34 -0600 | [diff] [blame] | 226 | { |
Peter Rosin | 4bbe7fb | 2016-04-20 08:39:31 +0200 | [diff] [blame] | 227 | struct i2c_mux_core *muxc = platform_get_drvdata(pdev); |
Stephen Warren | ae58d1e | 2012-05-18 09:29:34 -0600 | [diff] [blame] | 228 | |
Peter Rosin | 4bbe7fb | 2016-04-20 08:39:31 +0200 | [diff] [blame] | 229 | i2c_mux_del_adapters(muxc); |
| 230 | i2c_put_adapter(muxc->parent); |
Stephen Warren | ae58d1e | 2012-05-18 09:29:34 -0600 | [diff] [blame] | 231 | return 0; |
| 232 | } |
| 233 | |
| 234 | #ifdef CONFIG_OF |
Bill Pemberton | 0b255e9 | 2012-11-27 15:59:38 -0500 | [diff] [blame] | 235 | static const struct of_device_id i2c_mux_pinctrl_of_match[] = { |
Stephen Warren | ae58d1e | 2012-05-18 09:29:34 -0600 | [diff] [blame] | 236 | { .compatible = "i2c-mux-pinctrl", }, |
| 237 | {}, |
| 238 | }; |
| 239 | MODULE_DEVICE_TABLE(of, i2c_mux_pinctrl_of_match); |
| 240 | #endif |
| 241 | |
| 242 | static struct platform_driver i2c_mux_pinctrl_driver = { |
| 243 | .driver = { |
| 244 | .name = "i2c-mux-pinctrl", |
Stephen Warren | ae58d1e | 2012-05-18 09:29:34 -0600 | [diff] [blame] | 245 | .of_match_table = of_match_ptr(i2c_mux_pinctrl_of_match), |
| 246 | }, |
| 247 | .probe = i2c_mux_pinctrl_probe, |
Bill Pemberton | 0b255e9 | 2012-11-27 15:59:38 -0500 | [diff] [blame] | 248 | .remove = i2c_mux_pinctrl_remove, |
Stephen Warren | ae58d1e | 2012-05-18 09:29:34 -0600 | [diff] [blame] | 249 | }; |
| 250 | module_platform_driver(i2c_mux_pinctrl_driver); |
| 251 | |
| 252 | MODULE_DESCRIPTION("pinctrl-based I2C multiplexer driver"); |
| 253 | MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>"); |
| 254 | MODULE_LICENSE("GPL v2"); |
| 255 | MODULE_ALIAS("platform:i2c-mux-pinctrl"); |