Dong Aisheng | ae75ff8 | 2012-04-27 20:26:16 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Core driver for the imx pin controller |
| 3 | * |
| 4 | * Copyright (C) 2012 Freescale Semiconductor, Inc. |
| 5 | * Copyright (C) 2012 Linaro Ltd. |
| 6 | * |
| 7 | * Author: Dong Aisheng <dong.aisheng@linaro.org> |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | */ |
| 14 | |
| 15 | #include <linux/err.h> |
| 16 | #include <linux/init.h> |
| 17 | #include <linux/io.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/of.h> |
| 20 | #include <linux/of_device.h> |
| 21 | #include <linux/pinctrl/machine.h> |
| 22 | #include <linux/pinctrl/pinconf.h> |
| 23 | #include <linux/pinctrl/pinctrl.h> |
| 24 | #include <linux/pinctrl/pinmux.h> |
| 25 | #include <linux/slab.h> |
| 26 | |
| 27 | #include "core.h" |
| 28 | #include "pinctrl-imx.h" |
| 29 | |
Devendra Naga | 3a86a5f | 2012-06-09 00:52:11 +0530 | [diff] [blame] | 30 | #define IMX_PMX_DUMP(info, p, m, c, n) \ |
| 31 | { \ |
| 32 | int i, j; \ |
| 33 | printk(KERN_DEBUG "Format: Pin Mux Config\n"); \ |
| 34 | for (i = 0; i < n; i++) { \ |
| 35 | j = p[i]; \ |
| 36 | printk(KERN_DEBUG "%s %d 0x%lx\n", \ |
| 37 | info->pins[j].name, \ |
| 38 | m[i], c[i]); \ |
| 39 | } \ |
Dong Aisheng | ae75ff8 | 2012-04-27 20:26:16 +0800 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | /* The bits in CONFIG cell defined in binding doc*/ |
| 43 | #define IMX_NO_PAD_CTL 0x80000000 /* no pin config need */ |
| 44 | #define IMX_PAD_SION 0x40000000 /* set SION */ |
| 45 | |
| 46 | /** |
| 47 | * @dev: a pointer back to containing device |
| 48 | * @base: the offset to the controller in virtual memory |
| 49 | */ |
| 50 | struct imx_pinctrl { |
| 51 | struct device *dev; |
| 52 | struct pinctrl_dev *pctl; |
| 53 | void __iomem *base; |
| 54 | const struct imx_pinctrl_soc_info *info; |
| 55 | }; |
| 56 | |
Dong Aisheng | ae75ff8 | 2012-04-27 20:26:16 +0800 | [diff] [blame] | 57 | static const inline struct imx_pin_group *imx_pinctrl_find_group_by_name( |
| 58 | const struct imx_pinctrl_soc_info *info, |
| 59 | const char *name) |
| 60 | { |
| 61 | const struct imx_pin_group *grp = NULL; |
| 62 | int i; |
| 63 | |
| 64 | for (i = 0; i < info->ngroups; i++) { |
| 65 | if (!strcmp(info->groups[i].name, name)) { |
| 66 | grp = &info->groups[i]; |
| 67 | break; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | return grp; |
| 72 | } |
| 73 | |
| 74 | static int imx_get_groups_count(struct pinctrl_dev *pctldev) |
| 75 | { |
| 76 | struct imx_pinctrl *ipctl = pinctrl_dev_get_drvdata(pctldev); |
| 77 | const struct imx_pinctrl_soc_info *info = ipctl->info; |
| 78 | |
| 79 | return info->ngroups; |
| 80 | } |
| 81 | |
| 82 | static const char *imx_get_group_name(struct pinctrl_dev *pctldev, |
| 83 | unsigned selector) |
| 84 | { |
| 85 | struct imx_pinctrl *ipctl = pinctrl_dev_get_drvdata(pctldev); |
| 86 | const struct imx_pinctrl_soc_info *info = ipctl->info; |
| 87 | |
| 88 | return info->groups[selector].name; |
| 89 | } |
| 90 | |
| 91 | static int imx_get_group_pins(struct pinctrl_dev *pctldev, unsigned selector, |
| 92 | const unsigned **pins, |
| 93 | unsigned *npins) |
| 94 | { |
| 95 | struct imx_pinctrl *ipctl = pinctrl_dev_get_drvdata(pctldev); |
| 96 | const struct imx_pinctrl_soc_info *info = ipctl->info; |
| 97 | |
| 98 | if (selector >= info->ngroups) |
| 99 | return -EINVAL; |
| 100 | |
Sascha Hauer | 8f903f8 | 2013-07-28 16:29:22 +0200 | [diff] [blame^] | 101 | *pins = info->groups[selector].pin_ids; |
Dong Aisheng | ae75ff8 | 2012-04-27 20:26:16 +0800 | [diff] [blame] | 102 | *npins = info->groups[selector].npins; |
| 103 | |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | static void imx_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s, |
| 108 | unsigned offset) |
| 109 | { |
| 110 | seq_printf(s, "%s", dev_name(pctldev->dev)); |
| 111 | } |
| 112 | |
| 113 | static int imx_dt_node_to_map(struct pinctrl_dev *pctldev, |
| 114 | struct device_node *np, |
| 115 | struct pinctrl_map **map, unsigned *num_maps) |
| 116 | { |
| 117 | struct imx_pinctrl *ipctl = pinctrl_dev_get_drvdata(pctldev); |
| 118 | const struct imx_pinctrl_soc_info *info = ipctl->info; |
| 119 | const struct imx_pin_group *grp; |
| 120 | struct pinctrl_map *new_map; |
| 121 | struct device_node *parent; |
| 122 | int map_num = 1; |
Hui Wang | 1807161 | 2012-06-20 18:13:47 +0800 | [diff] [blame] | 123 | int i, j; |
Dong Aisheng | ae75ff8 | 2012-04-27 20:26:16 +0800 | [diff] [blame] | 124 | |
| 125 | /* |
| 126 | * first find the group of this node and check if we need create |
| 127 | * config maps for pins |
| 128 | */ |
| 129 | grp = imx_pinctrl_find_group_by_name(info, np->name); |
| 130 | if (!grp) { |
| 131 | dev_err(info->dev, "unable to find group for node %s\n", |
| 132 | np->name); |
| 133 | return -EINVAL; |
| 134 | } |
| 135 | |
| 136 | for (i = 0; i < grp->npins; i++) { |
Sascha Hauer | 8f903f8 | 2013-07-28 16:29:22 +0200 | [diff] [blame^] | 137 | if (!(grp->pins[i].config & IMX_NO_PAD_CTL)) |
Dong Aisheng | ae75ff8 | 2012-04-27 20:26:16 +0800 | [diff] [blame] | 138 | map_num++; |
| 139 | } |
| 140 | |
| 141 | new_map = kmalloc(sizeof(struct pinctrl_map) * map_num, GFP_KERNEL); |
| 142 | if (!new_map) |
| 143 | return -ENOMEM; |
| 144 | |
| 145 | *map = new_map; |
| 146 | *num_maps = map_num; |
| 147 | |
| 148 | /* create mux map */ |
| 149 | parent = of_get_parent(np); |
Devendra Naga | c71157c | 2012-06-07 22:19:26 +0530 | [diff] [blame] | 150 | if (!parent) { |
| 151 | kfree(new_map); |
Dong Aisheng | ae75ff8 | 2012-04-27 20:26:16 +0800 | [diff] [blame] | 152 | return -EINVAL; |
Devendra Naga | c71157c | 2012-06-07 22:19:26 +0530 | [diff] [blame] | 153 | } |
Dong Aisheng | ae75ff8 | 2012-04-27 20:26:16 +0800 | [diff] [blame] | 154 | new_map[0].type = PIN_MAP_TYPE_MUX_GROUP; |
| 155 | new_map[0].data.mux.function = parent->name; |
| 156 | new_map[0].data.mux.group = np->name; |
| 157 | of_node_put(parent); |
| 158 | |
| 159 | /* create config map */ |
| 160 | new_map++; |
Hui Wang | 1807161 | 2012-06-20 18:13:47 +0800 | [diff] [blame] | 161 | for (i = j = 0; i < grp->npins; i++) { |
Sascha Hauer | 8f903f8 | 2013-07-28 16:29:22 +0200 | [diff] [blame^] | 162 | if (!(grp->pins[i].config & IMX_NO_PAD_CTL)) { |
Hui Wang | 1807161 | 2012-06-20 18:13:47 +0800 | [diff] [blame] | 163 | new_map[j].type = PIN_MAP_TYPE_CONFIGS_PIN; |
| 164 | new_map[j].data.configs.group_or_pin = |
Sascha Hauer | 8f903f8 | 2013-07-28 16:29:22 +0200 | [diff] [blame^] | 165 | pin_get_name(pctldev, grp->pins[i].pin); |
| 166 | new_map[j].data.configs.configs = &grp->pins[i].config; |
Hui Wang | 1807161 | 2012-06-20 18:13:47 +0800 | [diff] [blame] | 167 | new_map[j].data.configs.num_configs = 1; |
| 168 | j++; |
Dong Aisheng | ae75ff8 | 2012-04-27 20:26:16 +0800 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | |
| 172 | dev_dbg(pctldev->dev, "maps: function %s group %s num %d\n", |
Dong Aisheng | 67695f2 | 2012-06-08 21:33:12 +0800 | [diff] [blame] | 173 | (*map)->data.mux.function, (*map)->data.mux.group, map_num); |
Dong Aisheng | ae75ff8 | 2012-04-27 20:26:16 +0800 | [diff] [blame] | 174 | |
| 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | static void imx_dt_free_map(struct pinctrl_dev *pctldev, |
| 179 | struct pinctrl_map *map, unsigned num_maps) |
| 180 | { |
Devendra Naga | 3a86a5f | 2012-06-09 00:52:11 +0530 | [diff] [blame] | 181 | kfree(map); |
Dong Aisheng | ae75ff8 | 2012-04-27 20:26:16 +0800 | [diff] [blame] | 182 | } |
| 183 | |
Laurent Pinchart | 022ab14 | 2013-02-16 10:25:07 +0100 | [diff] [blame] | 184 | static const struct pinctrl_ops imx_pctrl_ops = { |
Dong Aisheng | ae75ff8 | 2012-04-27 20:26:16 +0800 | [diff] [blame] | 185 | .get_groups_count = imx_get_groups_count, |
| 186 | .get_group_name = imx_get_group_name, |
| 187 | .get_group_pins = imx_get_group_pins, |
| 188 | .pin_dbg_show = imx_pin_dbg_show, |
| 189 | .dt_node_to_map = imx_dt_node_to_map, |
| 190 | .dt_free_map = imx_dt_free_map, |
| 191 | |
| 192 | }; |
| 193 | |
| 194 | static int imx_pmx_enable(struct pinctrl_dev *pctldev, unsigned selector, |
| 195 | unsigned group) |
| 196 | { |
| 197 | struct imx_pinctrl *ipctl = pinctrl_dev_get_drvdata(pctldev); |
| 198 | const struct imx_pinctrl_soc_info *info = ipctl->info; |
| 199 | const struct imx_pin_reg *pin_reg; |
Dong Aisheng | ae75ff8 | 2012-04-27 20:26:16 +0800 | [diff] [blame] | 200 | unsigned int npins, pin_id; |
| 201 | int i; |
Sascha Hauer | 8f903f8 | 2013-07-28 16:29:22 +0200 | [diff] [blame^] | 202 | struct imx_pin_group *grp; |
Dong Aisheng | ae75ff8 | 2012-04-27 20:26:16 +0800 | [diff] [blame] | 203 | |
| 204 | /* |
| 205 | * Configure the mux mode for each pin in the group for a specific |
| 206 | * function. |
| 207 | */ |
Sascha Hauer | 8f903f8 | 2013-07-28 16:29:22 +0200 | [diff] [blame^] | 208 | grp = &info->groups[group]; |
| 209 | npins = grp->npins; |
Dong Aisheng | ae75ff8 | 2012-04-27 20:26:16 +0800 | [diff] [blame] | 210 | |
| 211 | dev_dbg(ipctl->dev, "enable function %s group %s\n", |
Sascha Hauer | 8f903f8 | 2013-07-28 16:29:22 +0200 | [diff] [blame^] | 212 | info->functions[selector].name, grp->name); |
Dong Aisheng | ae75ff8 | 2012-04-27 20:26:16 +0800 | [diff] [blame] | 213 | |
| 214 | for (i = 0; i < npins; i++) { |
Sascha Hauer | 8f903f8 | 2013-07-28 16:29:22 +0200 | [diff] [blame^] | 215 | struct imx_pin *pin = &grp->pins[i]; |
| 216 | pin_id = pin->pin; |
Shawn Guo | e164153 | 2013-02-20 10:32:52 +0800 | [diff] [blame] | 217 | pin_reg = &info->pin_regs[pin_id]; |
Dong Aisheng | ae75ff8 | 2012-04-27 20:26:16 +0800 | [diff] [blame] | 218 | |
Jingchang Lu | bf5a530 | 2013-05-28 17:32:07 +0800 | [diff] [blame] | 219 | if (!(info->flags & ZERO_OFFSET_VALID) && !pin_reg->mux_reg) { |
Dong Aisheng | ae75ff8 | 2012-04-27 20:26:16 +0800 | [diff] [blame] | 220 | dev_err(ipctl->dev, "Pin(%s) does not support mux function\n", |
| 221 | info->pins[pin_id].name); |
| 222 | return -EINVAL; |
| 223 | } |
| 224 | |
Jingchang Lu | bf5a530 | 2013-05-28 17:32:07 +0800 | [diff] [blame] | 225 | if (info->flags & SHARE_MUX_CONF_REG) { |
| 226 | u32 reg; |
| 227 | reg = readl(ipctl->base + pin_reg->mux_reg); |
| 228 | reg &= ~(0x7 << 20); |
Sascha Hauer | 8f903f8 | 2013-07-28 16:29:22 +0200 | [diff] [blame^] | 229 | reg |= (pin->mux_mode << 20); |
Jingchang Lu | bf5a530 | 2013-05-28 17:32:07 +0800 | [diff] [blame] | 230 | writel(reg, ipctl->base + pin_reg->mux_reg); |
| 231 | } else { |
Sascha Hauer | 8f903f8 | 2013-07-28 16:29:22 +0200 | [diff] [blame^] | 232 | writel(pin->mux_mode, ipctl->base + pin_reg->mux_reg); |
Jingchang Lu | bf5a530 | 2013-05-28 17:32:07 +0800 | [diff] [blame] | 233 | } |
Dong Aisheng | ae75ff8 | 2012-04-27 20:26:16 +0800 | [diff] [blame] | 234 | dev_dbg(ipctl->dev, "write: offset 0x%x val 0x%x\n", |
Sascha Hauer | 8f903f8 | 2013-07-28 16:29:22 +0200 | [diff] [blame^] | 235 | pin_reg->mux_reg, pin->mux_mode); |
Dong Aisheng | ae75ff8 | 2012-04-27 20:26:16 +0800 | [diff] [blame] | 236 | |
Shawn Guo | 94176fa | 2013-08-04 21:39:23 +0800 | [diff] [blame] | 237 | /* |
| 238 | * If the select input value begins with 0xff, it's a quirky |
| 239 | * select input and the value should be interpreted as below. |
| 240 | * 31 23 15 7 0 |
| 241 | * | 0xff | shift | width | select | |
| 242 | * It's used to work around the problem that the select |
| 243 | * input for some pin is not implemented in the select |
| 244 | * input register but in some general purpose register. |
| 245 | * We encode the select input value, width and shift of |
| 246 | * the bit field into input_val cell of pin function ID |
| 247 | * in device tree, and then decode them here for setting |
| 248 | * up the select input bits in general purpose register. |
| 249 | */ |
Sascha Hauer | 8f903f8 | 2013-07-28 16:29:22 +0200 | [diff] [blame^] | 250 | if (pin->input_val >> 24 == 0xff) { |
| 251 | u32 val = pin->input_val; |
Shawn Guo | 94176fa | 2013-08-04 21:39:23 +0800 | [diff] [blame] | 252 | u8 select = val & 0xff; |
| 253 | u8 width = (val >> 8) & 0xff; |
| 254 | u8 shift = (val >> 16) & 0xff; |
| 255 | u32 mask = ((1 << width) - 1) << shift; |
| 256 | /* |
| 257 | * The input_reg[i] here is actually some IOMUXC general |
| 258 | * purpose register, not regular select input register. |
| 259 | */ |
Sascha Hauer | 8f903f8 | 2013-07-28 16:29:22 +0200 | [diff] [blame^] | 260 | val = readl(ipctl->base + pin->input_val); |
Shawn Guo | 94176fa | 2013-08-04 21:39:23 +0800 | [diff] [blame] | 261 | val &= ~mask; |
| 262 | val |= select << shift; |
Sascha Hauer | 8f903f8 | 2013-07-28 16:29:22 +0200 | [diff] [blame^] | 263 | writel(val, ipctl->base + pin->input_val); |
| 264 | } else if (pin->input_val) { |
Shawn Guo | 94176fa | 2013-08-04 21:39:23 +0800 | [diff] [blame] | 265 | /* |
| 266 | * Regular select input register can never be at offset |
| 267 | * 0, and we only print register value for regular case. |
| 268 | */ |
Sascha Hauer | 8f903f8 | 2013-07-28 16:29:22 +0200 | [diff] [blame^] | 269 | writel(pin->input_val, ipctl->base + pin->input_reg); |
Dong Aisheng | ae75ff8 | 2012-04-27 20:26:16 +0800 | [diff] [blame] | 270 | dev_dbg(ipctl->dev, |
| 271 | "==>select_input: offset 0x%x val 0x%x\n", |
Sascha Hauer | 8f903f8 | 2013-07-28 16:29:22 +0200 | [diff] [blame^] | 272 | pin->input_reg, pin->input_val); |
Dong Aisheng | ae75ff8 | 2012-04-27 20:26:16 +0800 | [diff] [blame] | 273 | } |
| 274 | } |
| 275 | |
| 276 | return 0; |
| 277 | } |
| 278 | |
Dong Aisheng | ae75ff8 | 2012-04-27 20:26:16 +0800 | [diff] [blame] | 279 | static int imx_pmx_get_funcs_count(struct pinctrl_dev *pctldev) |
| 280 | { |
| 281 | struct imx_pinctrl *ipctl = pinctrl_dev_get_drvdata(pctldev); |
| 282 | const struct imx_pinctrl_soc_info *info = ipctl->info; |
| 283 | |
| 284 | return info->nfunctions; |
| 285 | } |
| 286 | |
| 287 | static const char *imx_pmx_get_func_name(struct pinctrl_dev *pctldev, |
| 288 | unsigned selector) |
| 289 | { |
| 290 | struct imx_pinctrl *ipctl = pinctrl_dev_get_drvdata(pctldev); |
| 291 | const struct imx_pinctrl_soc_info *info = ipctl->info; |
| 292 | |
| 293 | return info->functions[selector].name; |
| 294 | } |
| 295 | |
| 296 | static int imx_pmx_get_groups(struct pinctrl_dev *pctldev, unsigned selector, |
| 297 | const char * const **groups, |
| 298 | unsigned * const num_groups) |
| 299 | { |
| 300 | struct imx_pinctrl *ipctl = pinctrl_dev_get_drvdata(pctldev); |
| 301 | const struct imx_pinctrl_soc_info *info = ipctl->info; |
| 302 | |
| 303 | *groups = info->functions[selector].groups; |
| 304 | *num_groups = info->functions[selector].num_groups; |
| 305 | |
| 306 | return 0; |
| 307 | } |
| 308 | |
Laurent Pinchart | 022ab14 | 2013-02-16 10:25:07 +0100 | [diff] [blame] | 309 | static const struct pinmux_ops imx_pmx_ops = { |
Dong Aisheng | ae75ff8 | 2012-04-27 20:26:16 +0800 | [diff] [blame] | 310 | .get_functions_count = imx_pmx_get_funcs_count, |
| 311 | .get_function_name = imx_pmx_get_func_name, |
| 312 | .get_function_groups = imx_pmx_get_groups, |
| 313 | .enable = imx_pmx_enable, |
Dong Aisheng | ae75ff8 | 2012-04-27 20:26:16 +0800 | [diff] [blame] | 314 | }; |
| 315 | |
| 316 | static int imx_pinconf_get(struct pinctrl_dev *pctldev, |
| 317 | unsigned pin_id, unsigned long *config) |
| 318 | { |
| 319 | struct imx_pinctrl *ipctl = pinctrl_dev_get_drvdata(pctldev); |
| 320 | const struct imx_pinctrl_soc_info *info = ipctl->info; |
Shawn Guo | e164153 | 2013-02-20 10:32:52 +0800 | [diff] [blame] | 321 | const struct imx_pin_reg *pin_reg = &info->pin_regs[pin_id]; |
Dong Aisheng | ae75ff8 | 2012-04-27 20:26:16 +0800 | [diff] [blame] | 322 | |
Jingchang Lu | bf5a530 | 2013-05-28 17:32:07 +0800 | [diff] [blame] | 323 | if (!(info->flags & ZERO_OFFSET_VALID) && !pin_reg->conf_reg) { |
Dong Aisheng | ae75ff8 | 2012-04-27 20:26:16 +0800 | [diff] [blame] | 324 | dev_err(info->dev, "Pin(%s) does not support config function\n", |
| 325 | info->pins[pin_id].name); |
| 326 | return -EINVAL; |
| 327 | } |
| 328 | |
| 329 | *config = readl(ipctl->base + pin_reg->conf_reg); |
| 330 | |
Jingchang Lu | bf5a530 | 2013-05-28 17:32:07 +0800 | [diff] [blame] | 331 | if (info->flags & SHARE_MUX_CONF_REG) |
| 332 | *config &= 0xffff; |
| 333 | |
Dong Aisheng | ae75ff8 | 2012-04-27 20:26:16 +0800 | [diff] [blame] | 334 | return 0; |
| 335 | } |
| 336 | |
| 337 | static int imx_pinconf_set(struct pinctrl_dev *pctldev, |
| 338 | unsigned pin_id, unsigned long config) |
| 339 | { |
| 340 | struct imx_pinctrl *ipctl = pinctrl_dev_get_drvdata(pctldev); |
| 341 | const struct imx_pinctrl_soc_info *info = ipctl->info; |
Shawn Guo | e164153 | 2013-02-20 10:32:52 +0800 | [diff] [blame] | 342 | const struct imx_pin_reg *pin_reg = &info->pin_regs[pin_id]; |
Dong Aisheng | ae75ff8 | 2012-04-27 20:26:16 +0800 | [diff] [blame] | 343 | |
Jingchang Lu | bf5a530 | 2013-05-28 17:32:07 +0800 | [diff] [blame] | 344 | if (!(info->flags & ZERO_OFFSET_VALID) && !pin_reg->conf_reg) { |
Dong Aisheng | ae75ff8 | 2012-04-27 20:26:16 +0800 | [diff] [blame] | 345 | dev_err(info->dev, "Pin(%s) does not support config function\n", |
| 346 | info->pins[pin_id].name); |
| 347 | return -EINVAL; |
| 348 | } |
| 349 | |
| 350 | dev_dbg(ipctl->dev, "pinconf set pin %s\n", |
| 351 | info->pins[pin_id].name); |
| 352 | |
Jingchang Lu | bf5a530 | 2013-05-28 17:32:07 +0800 | [diff] [blame] | 353 | if (info->flags & SHARE_MUX_CONF_REG) { |
| 354 | u32 reg; |
| 355 | reg = readl(ipctl->base + pin_reg->conf_reg); |
| 356 | reg &= ~0xffff; |
| 357 | reg |= config; |
| 358 | writel(reg, ipctl->base + pin_reg->conf_reg); |
| 359 | } else { |
| 360 | writel(config, ipctl->base + pin_reg->conf_reg); |
| 361 | } |
Dong Aisheng | ae75ff8 | 2012-04-27 20:26:16 +0800 | [diff] [blame] | 362 | dev_dbg(ipctl->dev, "write: offset 0x%x val 0x%lx\n", |
| 363 | pin_reg->conf_reg, config); |
| 364 | |
| 365 | return 0; |
| 366 | } |
| 367 | |
| 368 | static void imx_pinconf_dbg_show(struct pinctrl_dev *pctldev, |
| 369 | struct seq_file *s, unsigned pin_id) |
| 370 | { |
| 371 | struct imx_pinctrl *ipctl = pinctrl_dev_get_drvdata(pctldev); |
| 372 | const struct imx_pinctrl_soc_info *info = ipctl->info; |
Shawn Guo | e164153 | 2013-02-20 10:32:52 +0800 | [diff] [blame] | 373 | const struct imx_pin_reg *pin_reg = &info->pin_regs[pin_id]; |
Dong Aisheng | ae75ff8 | 2012-04-27 20:26:16 +0800 | [diff] [blame] | 374 | unsigned long config; |
| 375 | |
Dong Aisheng | ae75ff8 | 2012-04-27 20:26:16 +0800 | [diff] [blame] | 376 | if (!pin_reg || !pin_reg->conf_reg) { |
| 377 | seq_printf(s, "N/A"); |
| 378 | return; |
| 379 | } |
| 380 | |
| 381 | config = readl(ipctl->base + pin_reg->conf_reg); |
| 382 | seq_printf(s, "0x%lx", config); |
| 383 | } |
| 384 | |
| 385 | static void imx_pinconf_group_dbg_show(struct pinctrl_dev *pctldev, |
| 386 | struct seq_file *s, unsigned group) |
| 387 | { |
| 388 | struct imx_pinctrl *ipctl = pinctrl_dev_get_drvdata(pctldev); |
| 389 | const struct imx_pinctrl_soc_info *info = ipctl->info; |
| 390 | struct imx_pin_group *grp; |
| 391 | unsigned long config; |
| 392 | const char *name; |
| 393 | int i, ret; |
| 394 | |
| 395 | if (group > info->ngroups) |
| 396 | return; |
| 397 | |
| 398 | seq_printf(s, "\n"); |
| 399 | grp = &info->groups[group]; |
| 400 | for (i = 0; i < grp->npins; i++) { |
Sascha Hauer | 8f903f8 | 2013-07-28 16:29:22 +0200 | [diff] [blame^] | 401 | struct imx_pin *pin = &grp->pins[i]; |
| 402 | name = pin_get_name(pctldev, pin->pin); |
| 403 | ret = imx_pinconf_get(pctldev, pin->pin, &config); |
Dong Aisheng | ae75ff8 | 2012-04-27 20:26:16 +0800 | [diff] [blame] | 404 | if (ret) |
| 405 | return; |
| 406 | seq_printf(s, "%s: 0x%lx", name, config); |
| 407 | } |
| 408 | } |
| 409 | |
Laurent Pinchart | 022ab14 | 2013-02-16 10:25:07 +0100 | [diff] [blame] | 410 | static const struct pinconf_ops imx_pinconf_ops = { |
Dong Aisheng | ae75ff8 | 2012-04-27 20:26:16 +0800 | [diff] [blame] | 411 | .pin_config_get = imx_pinconf_get, |
| 412 | .pin_config_set = imx_pinconf_set, |
| 413 | .pin_config_dbg_show = imx_pinconf_dbg_show, |
| 414 | .pin_config_group_dbg_show = imx_pinconf_group_dbg_show, |
| 415 | }; |
| 416 | |
| 417 | static struct pinctrl_desc imx_pinctrl_desc = { |
| 418 | .pctlops = &imx_pctrl_ops, |
| 419 | .pmxops = &imx_pmx_ops, |
| 420 | .confops = &imx_pinconf_ops, |
| 421 | .owner = THIS_MODULE, |
| 422 | }; |
| 423 | |
Shawn Guo | e164153 | 2013-02-20 10:32:52 +0800 | [diff] [blame] | 424 | /* |
| 425 | * Each pin represented in fsl,pins consists of 5 u32 PIN_FUNC_ID and |
| 426 | * 1 u32 CONFIG, so 24 types in total for each pin. |
| 427 | */ |
| 428 | #define FSL_PIN_SIZE 24 |
Jingchang Lu | bf5a530 | 2013-05-28 17:32:07 +0800 | [diff] [blame] | 429 | #define SHARE_FSL_PIN_SIZE 20 |
Dong Aisheng | ae75ff8 | 2012-04-27 20:26:16 +0800 | [diff] [blame] | 430 | |
Greg Kroah-Hartman | 150632b | 2012-12-21 13:10:23 -0800 | [diff] [blame] | 431 | static int imx_pinctrl_parse_groups(struct device_node *np, |
| 432 | struct imx_pin_group *grp, |
| 433 | struct imx_pinctrl_soc_info *info, |
| 434 | u32 index) |
Dong Aisheng | ae75ff8 | 2012-04-27 20:26:16 +0800 | [diff] [blame] | 435 | { |
Jingchang Lu | bf5a530 | 2013-05-28 17:32:07 +0800 | [diff] [blame] | 436 | int size, pin_size; |
Richard Zhao | a695145 | 2012-09-18 14:54:00 +0800 | [diff] [blame] | 437 | const __be32 *list; |
Shawn Guo | e164153 | 2013-02-20 10:32:52 +0800 | [diff] [blame] | 438 | int i; |
Dong Aisheng | ae75ff8 | 2012-04-27 20:26:16 +0800 | [diff] [blame] | 439 | u32 config; |
| 440 | |
| 441 | dev_dbg(info->dev, "group(%d): %s\n", index, np->name); |
| 442 | |
Jingchang Lu | bf5a530 | 2013-05-28 17:32:07 +0800 | [diff] [blame] | 443 | if (info->flags & SHARE_MUX_CONF_REG) |
| 444 | pin_size = SHARE_FSL_PIN_SIZE; |
| 445 | else |
| 446 | pin_size = FSL_PIN_SIZE; |
Dong Aisheng | ae75ff8 | 2012-04-27 20:26:16 +0800 | [diff] [blame] | 447 | /* Initialise group */ |
| 448 | grp->name = np->name; |
| 449 | |
| 450 | /* |
| 451 | * the binding format is fsl,pins = <PIN_FUNC_ID CONFIG ...>, |
| 452 | * do sanity check and calculate pins number |
| 453 | */ |
| 454 | list = of_get_property(np, "fsl,pins", &size); |
Sascha Hauer | 1bf1fea9 | 2013-08-09 14:20:51 +0200 | [diff] [blame] | 455 | if (!list) { |
| 456 | dev_err(info->dev, "no fsl,pins property in node %s\n", np->full_name); |
| 457 | return -EINVAL; |
| 458 | } |
| 459 | |
Dong Aisheng | ae75ff8 | 2012-04-27 20:26:16 +0800 | [diff] [blame] | 460 | /* we do not check return since it's safe node passed down */ |
Jingchang Lu | bf5a530 | 2013-05-28 17:32:07 +0800 | [diff] [blame] | 461 | if (!size || size % pin_size) { |
Sascha Hauer | 0131251 | 2013-08-09 14:20:50 +0200 | [diff] [blame] | 462 | dev_err(info->dev, "Invalid fsl,pins property in node %s\n", np->full_name); |
Dong Aisheng | ae75ff8 | 2012-04-27 20:26:16 +0800 | [diff] [blame] | 463 | return -EINVAL; |
| 464 | } |
| 465 | |
Jingchang Lu | bf5a530 | 2013-05-28 17:32:07 +0800 | [diff] [blame] | 466 | grp->npins = size / pin_size; |
Sascha Hauer | 8f903f8 | 2013-07-28 16:29:22 +0200 | [diff] [blame^] | 467 | grp->pins = devm_kzalloc(info->dev, grp->npins * sizeof(struct imx_pin), |
Dong Aisheng | ae75ff8 | 2012-04-27 20:26:16 +0800 | [diff] [blame] | 468 | GFP_KERNEL); |
Sascha Hauer | 8f903f8 | 2013-07-28 16:29:22 +0200 | [diff] [blame^] | 469 | grp->pin_ids = devm_kzalloc(info->dev, grp->npins * sizeof(unsigned int), |
Dong Aisheng | ae75ff8 | 2012-04-27 20:26:16 +0800 | [diff] [blame] | 470 | GFP_KERNEL); |
Sascha Hauer | 8f903f8 | 2013-07-28 16:29:22 +0200 | [diff] [blame^] | 471 | if (!grp->pins || ! grp->pin_ids) |
| 472 | return -ENOMEM; |
| 473 | |
Shawn Guo | e164153 | 2013-02-20 10:32:52 +0800 | [diff] [blame] | 474 | for (i = 0; i < grp->npins; i++) { |
| 475 | u32 mux_reg = be32_to_cpu(*list++); |
Jingchang Lu | bf5a530 | 2013-05-28 17:32:07 +0800 | [diff] [blame] | 476 | u32 conf_reg; |
| 477 | unsigned int pin_id; |
| 478 | struct imx_pin_reg *pin_reg; |
Sascha Hauer | 8f903f8 | 2013-07-28 16:29:22 +0200 | [diff] [blame^] | 479 | struct imx_pin *pin = &grp->pins[i]; |
Shawn Guo | e164153 | 2013-02-20 10:32:52 +0800 | [diff] [blame] | 480 | |
Jingchang Lu | bf5a530 | 2013-05-28 17:32:07 +0800 | [diff] [blame] | 481 | if (info->flags & SHARE_MUX_CONF_REG) |
| 482 | conf_reg = mux_reg; |
| 483 | else |
| 484 | conf_reg = be32_to_cpu(*list++); |
| 485 | |
| 486 | pin_id = mux_reg ? mux_reg / 4 : conf_reg / 4; |
| 487 | pin_reg = &info->pin_regs[pin_id]; |
Sascha Hauer | 8f903f8 | 2013-07-28 16:29:22 +0200 | [diff] [blame^] | 488 | pin->pin = pin_id; |
| 489 | grp->pin_ids[i] = pin_id; |
Shawn Guo | e164153 | 2013-02-20 10:32:52 +0800 | [diff] [blame] | 490 | pin_reg->mux_reg = mux_reg; |
| 491 | pin_reg->conf_reg = conf_reg; |
Sascha Hauer | 8f903f8 | 2013-07-28 16:29:22 +0200 | [diff] [blame^] | 492 | pin->input_reg = be32_to_cpu(*list++); |
| 493 | pin->mux_mode = be32_to_cpu(*list++); |
| 494 | pin->input_val = be32_to_cpu(*list++); |
Shawn Guo | e164153 | 2013-02-20 10:32:52 +0800 | [diff] [blame] | 495 | |
Dong Aisheng | ae75ff8 | 2012-04-27 20:26:16 +0800 | [diff] [blame] | 496 | /* SION bit is in mux register */ |
| 497 | config = be32_to_cpu(*list++); |
| 498 | if (config & IMX_PAD_SION) |
Sascha Hauer | 8f903f8 | 2013-07-28 16:29:22 +0200 | [diff] [blame^] | 499 | pin->mux_mode |= IOMUXC_CONFIG_SION; |
| 500 | pin->config = config & ~IMX_PAD_SION; |
Dong Aisheng | ae75ff8 | 2012-04-27 20:26:16 +0800 | [diff] [blame] | 501 | } |
| 502 | |
Dong Aisheng | a6e7360 | 2012-06-21 18:10:35 +0800 | [diff] [blame] | 503 | #ifdef DEBUG |
Dong Aisheng | ae75ff8 | 2012-04-27 20:26:16 +0800 | [diff] [blame] | 504 | IMX_PMX_DUMP(info, grp->pins, grp->mux_mode, grp->configs, grp->npins); |
Dong Aisheng | a6e7360 | 2012-06-21 18:10:35 +0800 | [diff] [blame] | 505 | #endif |
Devendra Naga | 3a86a5f | 2012-06-09 00:52:11 +0530 | [diff] [blame] | 506 | |
Dong Aisheng | ae75ff8 | 2012-04-27 20:26:16 +0800 | [diff] [blame] | 507 | return 0; |
| 508 | } |
| 509 | |
Greg Kroah-Hartman | 150632b | 2012-12-21 13:10:23 -0800 | [diff] [blame] | 510 | static int imx_pinctrl_parse_functions(struct device_node *np, |
| 511 | struct imx_pinctrl_soc_info *info, |
| 512 | u32 index) |
Dong Aisheng | ae75ff8 | 2012-04-27 20:26:16 +0800 | [diff] [blame] | 513 | { |
| 514 | struct device_node *child; |
| 515 | struct imx_pmx_func *func; |
| 516 | struct imx_pin_group *grp; |
Dong Aisheng | ae75ff8 | 2012-04-27 20:26:16 +0800 | [diff] [blame] | 517 | static u32 grp_index; |
| 518 | u32 i = 0; |
| 519 | |
| 520 | dev_dbg(info->dev, "parse function(%d): %s\n", index, np->name); |
| 521 | |
| 522 | func = &info->functions[index]; |
| 523 | |
| 524 | /* Initialise function */ |
| 525 | func->name = np->name; |
| 526 | func->num_groups = of_get_child_count(np); |
| 527 | if (func->num_groups <= 0) { |
Sascha Hauer | 0131251 | 2013-08-09 14:20:50 +0200 | [diff] [blame] | 528 | dev_err(info->dev, "no groups defined in %s\n", np->full_name); |
Dong Aisheng | ae75ff8 | 2012-04-27 20:26:16 +0800 | [diff] [blame] | 529 | return -EINVAL; |
| 530 | } |
| 531 | func->groups = devm_kzalloc(info->dev, |
| 532 | func->num_groups * sizeof(char *), GFP_KERNEL); |
| 533 | |
| 534 | for_each_child_of_node(np, child) { |
| 535 | func->groups[i] = child->name; |
| 536 | grp = &info->groups[grp_index++]; |
Sascha Hauer | 5e13762c | 2013-08-09 14:20:52 +0200 | [diff] [blame] | 537 | imx_pinctrl_parse_groups(child, grp, info, i++); |
Dong Aisheng | ae75ff8 | 2012-04-27 20:26:16 +0800 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | return 0; |
| 541 | } |
| 542 | |
Greg Kroah-Hartman | 150632b | 2012-12-21 13:10:23 -0800 | [diff] [blame] | 543 | static int imx_pinctrl_probe_dt(struct platform_device *pdev, |
Dong Aisheng | ae75ff8 | 2012-04-27 20:26:16 +0800 | [diff] [blame] | 544 | struct imx_pinctrl_soc_info *info) |
| 545 | { |
| 546 | struct device_node *np = pdev->dev.of_node; |
| 547 | struct device_node *child; |
Dong Aisheng | ae75ff8 | 2012-04-27 20:26:16 +0800 | [diff] [blame] | 548 | u32 nfuncs = 0; |
| 549 | u32 i = 0; |
| 550 | |
| 551 | if (!np) |
| 552 | return -ENODEV; |
| 553 | |
| 554 | nfuncs = of_get_child_count(np); |
| 555 | if (nfuncs <= 0) { |
| 556 | dev_err(&pdev->dev, "no functions defined\n"); |
| 557 | return -EINVAL; |
| 558 | } |
| 559 | |
| 560 | info->nfunctions = nfuncs; |
| 561 | info->functions = devm_kzalloc(&pdev->dev, nfuncs * sizeof(struct imx_pmx_func), |
| 562 | GFP_KERNEL); |
| 563 | if (!info->functions) |
| 564 | return -ENOMEM; |
| 565 | |
| 566 | info->ngroups = 0; |
| 567 | for_each_child_of_node(np, child) |
| 568 | info->ngroups += of_get_child_count(child); |
| 569 | info->groups = devm_kzalloc(&pdev->dev, info->ngroups * sizeof(struct imx_pin_group), |
| 570 | GFP_KERNEL); |
| 571 | if (!info->groups) |
| 572 | return -ENOMEM; |
| 573 | |
Sascha Hauer | 7ea46e0 | 2013-08-09 14:20:53 +0200 | [diff] [blame] | 574 | for_each_child_of_node(np, child) |
| 575 | imx_pinctrl_parse_functions(child, info, i++); |
Dong Aisheng | ae75ff8 | 2012-04-27 20:26:16 +0800 | [diff] [blame] | 576 | |
| 577 | return 0; |
| 578 | } |
| 579 | |
Greg Kroah-Hartman | 150632b | 2012-12-21 13:10:23 -0800 | [diff] [blame] | 580 | int imx_pinctrl_probe(struct platform_device *pdev, |
| 581 | struct imx_pinctrl_soc_info *info) |
Dong Aisheng | ae75ff8 | 2012-04-27 20:26:16 +0800 | [diff] [blame] | 582 | { |
| 583 | struct imx_pinctrl *ipctl; |
| 584 | struct resource *res; |
| 585 | int ret; |
| 586 | |
Shawn Guo | e164153 | 2013-02-20 10:32:52 +0800 | [diff] [blame] | 587 | if (!info || !info->pins || !info->npins) { |
Dong Aisheng | ae75ff8 | 2012-04-27 20:26:16 +0800 | [diff] [blame] | 588 | dev_err(&pdev->dev, "wrong pinctrl info\n"); |
| 589 | return -EINVAL; |
| 590 | } |
| 591 | info->dev = &pdev->dev; |
| 592 | |
| 593 | /* Create state holders etc for this driver */ |
| 594 | ipctl = devm_kzalloc(&pdev->dev, sizeof(*ipctl), GFP_KERNEL); |
| 595 | if (!ipctl) |
| 596 | return -ENOMEM; |
| 597 | |
Shawn Guo | e164153 | 2013-02-20 10:32:52 +0800 | [diff] [blame] | 598 | info->pin_regs = devm_kzalloc(&pdev->dev, sizeof(*info->pin_regs) * |
| 599 | info->npins, GFP_KERNEL); |
| 600 | if (!info->pin_regs) |
| 601 | return -ENOMEM; |
| 602 | |
Dong Aisheng | ae75ff8 | 2012-04-27 20:26:16 +0800 | [diff] [blame] | 603 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Thierry Reding | 9e0c1fb | 2013-01-21 11:09:14 +0100 | [diff] [blame] | 604 | ipctl->base = devm_ioremap_resource(&pdev->dev, res); |
| 605 | if (IS_ERR(ipctl->base)) |
| 606 | return PTR_ERR(ipctl->base); |
Dong Aisheng | ae75ff8 | 2012-04-27 20:26:16 +0800 | [diff] [blame] | 607 | |
| 608 | imx_pinctrl_desc.name = dev_name(&pdev->dev); |
| 609 | imx_pinctrl_desc.pins = info->pins; |
| 610 | imx_pinctrl_desc.npins = info->npins; |
| 611 | |
| 612 | ret = imx_pinctrl_probe_dt(pdev, info); |
| 613 | if (ret) { |
| 614 | dev_err(&pdev->dev, "fail to probe dt properties\n"); |
| 615 | return ret; |
| 616 | } |
| 617 | |
| 618 | ipctl->info = info; |
| 619 | ipctl->dev = info->dev; |
| 620 | platform_set_drvdata(pdev, ipctl); |
| 621 | ipctl->pctl = pinctrl_register(&imx_pinctrl_desc, &pdev->dev, ipctl); |
| 622 | if (!ipctl->pctl) { |
| 623 | dev_err(&pdev->dev, "could not register IMX pinctrl driver\n"); |
| 624 | return -EINVAL; |
| 625 | } |
| 626 | |
| 627 | dev_info(&pdev->dev, "initialized IMX pinctrl driver\n"); |
| 628 | |
| 629 | return 0; |
| 630 | } |
| 631 | |
Bill Pemberton | f90f54b | 2012-11-19 13:26:06 -0500 | [diff] [blame] | 632 | int imx_pinctrl_remove(struct platform_device *pdev) |
Dong Aisheng | ae75ff8 | 2012-04-27 20:26:16 +0800 | [diff] [blame] | 633 | { |
| 634 | struct imx_pinctrl *ipctl = platform_get_drvdata(pdev); |
| 635 | |
| 636 | pinctrl_unregister(ipctl->pctl); |
| 637 | |
| 638 | return 0; |
| 639 | } |