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