Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Generic device tree based pinctrl driver for one register per pin |
| 3 | * type pinmux controllers |
| 4 | * |
| 5 | * Copyright (C) 2012 Texas Instruments, Inc. |
| 6 | * |
| 7 | * This file is licensed under the terms of the GNU General Public |
| 8 | * License version 2. This program is licensed "as is" without any |
| 9 | * warranty of any kind, whether express or implied. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/init.h> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/io.h> |
| 15 | #include <linux/slab.h> |
| 16 | #include <linux/err.h> |
| 17 | #include <linux/list.h> |
Tony Lindgren | 3e6cee1 | 2013-10-02 21:39:40 -0700 | [diff] [blame] | 18 | #include <linux/interrupt.h> |
| 19 | |
| 20 | #include <linux/irqchip/chained_irq.h> |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 21 | |
| 22 | #include <linux/of.h> |
| 23 | #include <linux/of_device.h> |
| 24 | #include <linux/of_address.h> |
Tony Lindgren | 3e6cee1 | 2013-10-02 21:39:40 -0700 | [diff] [blame] | 25 | #include <linux/of_irq.h> |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 26 | |
| 27 | #include <linux/pinctrl/pinctrl.h> |
| 28 | #include <linux/pinctrl/pinmux.h> |
Haojian Zhuang | 9dddb4d | 2013-02-17 19:42:55 +0800 | [diff] [blame] | 29 | #include <linux/pinctrl/pinconf-generic.h> |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 30 | |
Tony Lindgren | dc7743a | 2013-10-02 21:39:40 -0700 | [diff] [blame] | 31 | #include <linux/platform_data/pinctrl-single.h> |
| 32 | |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 33 | #include "core.h" |
Haojian Zhuang | 9dddb4d | 2013-02-17 19:42:55 +0800 | [diff] [blame] | 34 | #include "pinconf.h" |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 35 | |
| 36 | #define DRIVER_NAME "pinctrl-single" |
Peter Ujfalusi | 9e605cb | 2012-09-11 11:54:24 +0300 | [diff] [blame] | 37 | #define PCS_MUX_PINS_NAME "pinctrl-single,pins" |
| 38 | #define PCS_MUX_BITS_NAME "pinctrl-single,bits" |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 39 | #define PCS_OFF_DISABLED ~0U |
| 40 | |
| 41 | /** |
| 42 | * struct pcs_pingroup - pingroups for a function |
| 43 | * @np: pingroup device node pointer |
| 44 | * @name: pingroup name |
| 45 | * @gpins: array of the pins in the group |
| 46 | * @ngpins: number of pins in the group |
| 47 | * @node: list node |
| 48 | */ |
| 49 | struct pcs_pingroup { |
| 50 | struct device_node *np; |
| 51 | const char *name; |
| 52 | int *gpins; |
| 53 | int ngpins; |
| 54 | struct list_head node; |
| 55 | }; |
| 56 | |
| 57 | /** |
| 58 | * struct pcs_func_vals - mux function register offset and value pair |
| 59 | * @reg: register virtual address |
| 60 | * @val: register value |
| 61 | */ |
| 62 | struct pcs_func_vals { |
| 63 | void __iomem *reg; |
| 64 | unsigned val; |
Peter Ujfalusi | 9e605cb | 2012-09-11 11:54:24 +0300 | [diff] [blame] | 65 | unsigned mask; |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | /** |
Haojian Zhuang | 9dddb4d | 2013-02-17 19:42:55 +0800 | [diff] [blame] | 69 | * struct pcs_conf_vals - pinconf parameter, pinconf register offset |
| 70 | * and value, enable, disable, mask |
| 71 | * @param: config parameter |
| 72 | * @val: user input bits in the pinconf register |
| 73 | * @enable: enable bits in the pinconf register |
| 74 | * @disable: disable bits in the pinconf register |
| 75 | * @mask: mask bits in the register value |
| 76 | */ |
| 77 | struct pcs_conf_vals { |
| 78 | enum pin_config_param param; |
| 79 | unsigned val; |
| 80 | unsigned enable; |
| 81 | unsigned disable; |
| 82 | unsigned mask; |
| 83 | }; |
| 84 | |
| 85 | /** |
| 86 | * struct pcs_conf_type - pinconf property name, pinconf param pair |
| 87 | * @name: property name in DTS file |
| 88 | * @param: config parameter |
| 89 | */ |
| 90 | struct pcs_conf_type { |
| 91 | const char *name; |
| 92 | enum pin_config_param param; |
| 93 | }; |
| 94 | |
| 95 | /** |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 96 | * struct pcs_function - pinctrl function |
| 97 | * @name: pinctrl function name |
| 98 | * @vals: register and vals array |
| 99 | * @nvals: number of entries in vals array |
| 100 | * @pgnames: array of pingroup names the function uses |
| 101 | * @npgnames: number of pingroup names the function uses |
| 102 | * @node: list node |
| 103 | */ |
| 104 | struct pcs_function { |
| 105 | const char *name; |
| 106 | struct pcs_func_vals *vals; |
| 107 | unsigned nvals; |
| 108 | const char **pgnames; |
| 109 | int npgnames; |
Haojian Zhuang | 9dddb4d | 2013-02-17 19:42:55 +0800 | [diff] [blame] | 110 | struct pcs_conf_vals *conf; |
| 111 | int nconfs; |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 112 | struct list_head node; |
| 113 | }; |
| 114 | |
| 115 | /** |
Haojian Zhuang | a1a277e | 2013-02-17 19:42:52 +0800 | [diff] [blame] | 116 | * struct pcs_gpiofunc_range - pin ranges with same mux value of gpio function |
| 117 | * @offset: offset base of pins |
| 118 | * @npins: number pins with the same mux value of gpio function |
| 119 | * @gpiofunc: mux value of gpio function |
| 120 | * @node: list node |
| 121 | */ |
| 122 | struct pcs_gpiofunc_range { |
| 123 | unsigned offset; |
| 124 | unsigned npins; |
| 125 | unsigned gpiofunc; |
| 126 | struct list_head node; |
| 127 | }; |
| 128 | |
| 129 | /** |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 130 | * struct pcs_data - wrapper for data needed by pinctrl framework |
| 131 | * @pa: pindesc array |
| 132 | * @cur: index to current element |
| 133 | * |
| 134 | * REVISIT: We should be able to drop this eventually by adding |
| 135 | * support for registering pins individually in the pinctrl |
| 136 | * framework for those drivers that don't need a static array. |
| 137 | */ |
| 138 | struct pcs_data { |
| 139 | struct pinctrl_pin_desc *pa; |
| 140 | int cur; |
| 141 | }; |
| 142 | |
| 143 | /** |
Tony Lindgren | 02e483f | 2013-10-02 21:39:39 -0700 | [diff] [blame] | 144 | * struct pcs_soc_data - SoC specific settings |
| 145 | * @flags: initial SoC specific PCS_FEAT_xxx values |
Tony Lindgren | 3e6cee1 | 2013-10-02 21:39:40 -0700 | [diff] [blame] | 146 | * @irq: optional interrupt for the controller |
| 147 | * @irq_enable_mask: optional SoC specific interrupt enable mask |
| 148 | * @irq_status_mask: optional SoC specific interrupt status mask |
Tony Lindgren | dc7743a | 2013-10-02 21:39:40 -0700 | [diff] [blame] | 149 | * @rearm: optional SoC specific wake-up rearm function |
Tony Lindgren | 02e483f | 2013-10-02 21:39:39 -0700 | [diff] [blame] | 150 | */ |
| 151 | struct pcs_soc_data { |
| 152 | unsigned flags; |
Tony Lindgren | 3e6cee1 | 2013-10-02 21:39:40 -0700 | [diff] [blame] | 153 | int irq; |
| 154 | unsigned irq_enable_mask; |
| 155 | unsigned irq_status_mask; |
Tony Lindgren | dc7743a | 2013-10-02 21:39:40 -0700 | [diff] [blame] | 156 | void (*rearm)(void); |
Tony Lindgren | 02e483f | 2013-10-02 21:39:39 -0700 | [diff] [blame] | 157 | }; |
| 158 | |
| 159 | /** |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 160 | * struct pcs_device - pinctrl device instance |
| 161 | * @res: resources |
| 162 | * @base: virtual address of the controller |
| 163 | * @size: size of the ioremapped area |
| 164 | * @dev: device entry |
| 165 | * @pctl: pin controller device |
Tony Lindgren | 02e483f | 2013-10-02 21:39:39 -0700 | [diff] [blame] | 166 | * @flags: mask of PCS_FEAT_xxx values |
Tony Lindgren | 3e6cee1 | 2013-10-02 21:39:40 -0700 | [diff] [blame] | 167 | * @lock: spinlock for register access |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 168 | * @mutex: mutex protecting the lists |
| 169 | * @width: bits per mux register |
| 170 | * @fmask: function register mask |
| 171 | * @fshift: function register shift |
| 172 | * @foff: value to turn mux off |
| 173 | * @fmax: max number of functions in fmask |
Manjunathappa, Prakash | 4e7e801 | 2013-05-21 19:38:00 +0530 | [diff] [blame] | 174 | * @bits_per_pin:number of bits per pin |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 175 | * @pins: physical pins on the SoC |
| 176 | * @pgtree: pingroup index radix tree |
| 177 | * @ftree: function index radix tree |
| 178 | * @pingroups: list of pingroups |
| 179 | * @functions: list of functions |
Haojian Zhuang | a1a277e | 2013-02-17 19:42:52 +0800 | [diff] [blame] | 180 | * @gpiofuncs: list of gpio functions |
Tony Lindgren | 3e6cee1 | 2013-10-02 21:39:40 -0700 | [diff] [blame] | 181 | * @irqs: list of interrupt registers |
| 182 | * @chip: chip container for this instance |
| 183 | * @domain: IRQ domain for this instance |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 184 | * @ngroups: number of pingroups |
| 185 | * @nfuncs: number of functions |
| 186 | * @desc: pin controller descriptor |
| 187 | * @read: register read function to use |
| 188 | * @write: register write function to use |
| 189 | */ |
| 190 | struct pcs_device { |
| 191 | struct resource *res; |
| 192 | void __iomem *base; |
| 193 | unsigned size; |
| 194 | struct device *dev; |
| 195 | struct pinctrl_dev *pctl; |
Tony Lindgren | 02e483f | 2013-10-02 21:39:39 -0700 | [diff] [blame] | 196 | unsigned flags; |
Tony Lindgren | 3e6cee1 | 2013-10-02 21:39:40 -0700 | [diff] [blame] | 197 | #define PCS_QUIRK_SHARED_IRQ (1 << 2) |
| 198 | #define PCS_FEAT_IRQ (1 << 1) |
Tony Lindgren | 02e483f | 2013-10-02 21:39:39 -0700 | [diff] [blame] | 199 | #define PCS_FEAT_PINCONF (1 << 0) |
Tony Lindgren | 3e6cee1 | 2013-10-02 21:39:40 -0700 | [diff] [blame] | 200 | struct pcs_soc_data socdata; |
| 201 | raw_spinlock_t lock; |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 202 | struct mutex mutex; |
| 203 | unsigned width; |
| 204 | unsigned fmask; |
| 205 | unsigned fshift; |
| 206 | unsigned foff; |
| 207 | unsigned fmax; |
Peter Ujfalusi | 9e605cb | 2012-09-11 11:54:24 +0300 | [diff] [blame] | 208 | bool bits_per_mux; |
Manjunathappa, Prakash | 4e7e801 | 2013-05-21 19:38:00 +0530 | [diff] [blame] | 209 | unsigned bits_per_pin; |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 210 | struct pcs_data pins; |
| 211 | struct radix_tree_root pgtree; |
| 212 | struct radix_tree_root ftree; |
| 213 | struct list_head pingroups; |
| 214 | struct list_head functions; |
Haojian Zhuang | a1a277e | 2013-02-17 19:42:52 +0800 | [diff] [blame] | 215 | struct list_head gpiofuncs; |
Tony Lindgren | 3e6cee1 | 2013-10-02 21:39:40 -0700 | [diff] [blame] | 216 | struct list_head irqs; |
| 217 | struct irq_chip chip; |
| 218 | struct irq_domain *domain; |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 219 | unsigned ngroups; |
| 220 | unsigned nfuncs; |
| 221 | struct pinctrl_desc desc; |
| 222 | unsigned (*read)(void __iomem *reg); |
| 223 | void (*write)(unsigned val, void __iomem *reg); |
| 224 | }; |
| 225 | |
Tony Lindgren | 3e6cee1 | 2013-10-02 21:39:40 -0700 | [diff] [blame] | 226 | #define PCS_QUIRK_HAS_SHARED_IRQ (pcs->flags & PCS_QUIRK_SHARED_IRQ) |
| 227 | #define PCS_HAS_IRQ (pcs->flags & PCS_FEAT_IRQ) |
Tony Lindgren | 02e483f | 2013-10-02 21:39:39 -0700 | [diff] [blame] | 228 | #define PCS_HAS_PINCONF (pcs->flags & PCS_FEAT_PINCONF) |
| 229 | |
Haojian Zhuang | 9dddb4d | 2013-02-17 19:42:55 +0800 | [diff] [blame] | 230 | static int pcs_pinconf_get(struct pinctrl_dev *pctldev, unsigned pin, |
| 231 | unsigned long *config); |
| 232 | static int pcs_pinconf_set(struct pinctrl_dev *pctldev, unsigned pin, |
Sherman Yin | 03b054e | 2013-08-27 11:32:12 -0700 | [diff] [blame] | 233 | unsigned long *configs, unsigned num_configs); |
Haojian Zhuang | 9dddb4d | 2013-02-17 19:42:55 +0800 | [diff] [blame] | 234 | |
| 235 | static enum pin_config_param pcs_bias[] = { |
| 236 | PIN_CONFIG_BIAS_PULL_DOWN, |
| 237 | PIN_CONFIG_BIAS_PULL_UP, |
| 238 | }; |
| 239 | |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 240 | /* |
Sudeep Holla | 3c177a1 | 2016-02-01 18:28:17 +0000 | [diff] [blame] | 241 | * This lock class tells lockdep that irqchip core that this single |
| 242 | * pinctrl can be in a different category than its parents, so it won't |
| 243 | * report false recursion. |
| 244 | */ |
| 245 | static struct lock_class_key pcs_lock_class; |
| 246 | |
| 247 | /* |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 248 | * REVISIT: Reads and writes could eventually use regmap or something |
| 249 | * generic. But at least on omaps, some mux registers are performance |
| 250 | * critical as they may need to be remuxed every time before and after |
| 251 | * idle. Adding tests for register access width for every read and |
| 252 | * write like regmap is doing is not desired, and caching the registers |
| 253 | * does not help in this case. |
| 254 | */ |
| 255 | |
| 256 | static unsigned __maybe_unused pcs_readb(void __iomem *reg) |
| 257 | { |
| 258 | return readb(reg); |
| 259 | } |
| 260 | |
| 261 | static unsigned __maybe_unused pcs_readw(void __iomem *reg) |
| 262 | { |
| 263 | return readw(reg); |
| 264 | } |
| 265 | |
| 266 | static unsigned __maybe_unused pcs_readl(void __iomem *reg) |
| 267 | { |
| 268 | return readl(reg); |
| 269 | } |
| 270 | |
| 271 | static void __maybe_unused pcs_writeb(unsigned val, void __iomem *reg) |
| 272 | { |
| 273 | writeb(val, reg); |
| 274 | } |
| 275 | |
| 276 | static void __maybe_unused pcs_writew(unsigned val, void __iomem *reg) |
| 277 | { |
| 278 | writew(val, reg); |
| 279 | } |
| 280 | |
| 281 | static void __maybe_unused pcs_writel(unsigned val, void __iomem *reg) |
| 282 | { |
| 283 | writel(val, reg); |
| 284 | } |
| 285 | |
| 286 | static int pcs_get_groups_count(struct pinctrl_dev *pctldev) |
| 287 | { |
| 288 | struct pcs_device *pcs; |
| 289 | |
| 290 | pcs = pinctrl_dev_get_drvdata(pctldev); |
| 291 | |
| 292 | return pcs->ngroups; |
| 293 | } |
| 294 | |
| 295 | static const char *pcs_get_group_name(struct pinctrl_dev *pctldev, |
| 296 | unsigned gselector) |
| 297 | { |
| 298 | struct pcs_device *pcs; |
| 299 | struct pcs_pingroup *group; |
| 300 | |
| 301 | pcs = pinctrl_dev_get_drvdata(pctldev); |
| 302 | group = radix_tree_lookup(&pcs->pgtree, gselector); |
| 303 | if (!group) { |
| 304 | dev_err(pcs->dev, "%s could not find pingroup%i\n", |
| 305 | __func__, gselector); |
| 306 | return NULL; |
| 307 | } |
| 308 | |
| 309 | return group->name; |
| 310 | } |
| 311 | |
| 312 | static int pcs_get_group_pins(struct pinctrl_dev *pctldev, |
| 313 | unsigned gselector, |
| 314 | const unsigned **pins, |
| 315 | unsigned *npins) |
| 316 | { |
| 317 | struct pcs_device *pcs; |
| 318 | struct pcs_pingroup *group; |
| 319 | |
| 320 | pcs = pinctrl_dev_get_drvdata(pctldev); |
| 321 | group = radix_tree_lookup(&pcs->pgtree, gselector); |
| 322 | if (!group) { |
| 323 | dev_err(pcs->dev, "%s could not find pingroup%i\n", |
| 324 | __func__, gselector); |
| 325 | return -EINVAL; |
| 326 | } |
| 327 | |
| 328 | *pins = group->gpins; |
| 329 | *npins = group->ngpins; |
| 330 | |
| 331 | return 0; |
| 332 | } |
| 333 | |
| 334 | static void pcs_pin_dbg_show(struct pinctrl_dev *pctldev, |
| 335 | struct seq_file *s, |
Haojian Zhuang | e7ed671 | 2012-11-07 23:19:42 +0800 | [diff] [blame] | 336 | unsigned pin) |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 337 | { |
Matt Porter | 7d66ce7 | 2012-09-26 15:07:43 -0400 | [diff] [blame] | 338 | struct pcs_device *pcs; |
Haojian Zhuang | e7ed671 | 2012-11-07 23:19:42 +0800 | [diff] [blame] | 339 | unsigned val, mux_bytes; |
Tony Lindgren | 223decc | 2016-10-27 07:59:52 -0700 | [diff] [blame^] | 340 | unsigned long offset; |
| 341 | size_t pa; |
Matt Porter | 7d66ce7 | 2012-09-26 15:07:43 -0400 | [diff] [blame] | 342 | |
| 343 | pcs = pinctrl_dev_get_drvdata(pctldev); |
| 344 | |
Haojian Zhuang | e7ed671 | 2012-11-07 23:19:42 +0800 | [diff] [blame] | 345 | mux_bytes = pcs->width / BITS_PER_BYTE; |
Tony Lindgren | 223decc | 2016-10-27 07:59:52 -0700 | [diff] [blame^] | 346 | offset = pin * mux_bytes; |
| 347 | val = pcs->read(pcs->base + offset); |
| 348 | pa = pcs->res->start + offset; |
Matt Porter | 7d66ce7 | 2012-09-26 15:07:43 -0400 | [diff] [blame] | 349 | |
Tony Lindgren | 223decc | 2016-10-27 07:59:52 -0700 | [diff] [blame^] | 350 | seq_printf(s, "%zx %08x %s ", pa, val, DRIVER_NAME); |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | static void pcs_dt_free_map(struct pinctrl_dev *pctldev, |
| 354 | struct pinctrl_map *map, unsigned num_maps) |
| 355 | { |
| 356 | struct pcs_device *pcs; |
| 357 | |
| 358 | pcs = pinctrl_dev_get_drvdata(pctldev); |
| 359 | devm_kfree(pcs->dev, map); |
| 360 | } |
| 361 | |
| 362 | static int pcs_dt_node_to_map(struct pinctrl_dev *pctldev, |
| 363 | struct device_node *np_config, |
| 364 | struct pinctrl_map **map, unsigned *num_maps); |
| 365 | |
Laurent Pinchart | 022ab14 | 2013-02-16 10:25:07 +0100 | [diff] [blame] | 366 | static const struct pinctrl_ops pcs_pinctrl_ops = { |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 367 | .get_groups_count = pcs_get_groups_count, |
| 368 | .get_group_name = pcs_get_group_name, |
| 369 | .get_group_pins = pcs_get_group_pins, |
| 370 | .pin_dbg_show = pcs_pin_dbg_show, |
| 371 | .dt_node_to_map = pcs_dt_node_to_map, |
| 372 | .dt_free_map = pcs_dt_free_map, |
| 373 | }; |
| 374 | |
| 375 | static int pcs_get_functions_count(struct pinctrl_dev *pctldev) |
| 376 | { |
| 377 | struct pcs_device *pcs; |
| 378 | |
| 379 | pcs = pinctrl_dev_get_drvdata(pctldev); |
| 380 | |
| 381 | return pcs->nfuncs; |
| 382 | } |
| 383 | |
| 384 | static const char *pcs_get_function_name(struct pinctrl_dev *pctldev, |
| 385 | unsigned fselector) |
| 386 | { |
| 387 | struct pcs_device *pcs; |
| 388 | struct pcs_function *func; |
| 389 | |
| 390 | pcs = pinctrl_dev_get_drvdata(pctldev); |
| 391 | func = radix_tree_lookup(&pcs->ftree, fselector); |
| 392 | if (!func) { |
| 393 | dev_err(pcs->dev, "%s could not find function%i\n", |
| 394 | __func__, fselector); |
| 395 | return NULL; |
| 396 | } |
| 397 | |
| 398 | return func->name; |
| 399 | } |
| 400 | |
| 401 | static int pcs_get_function_groups(struct pinctrl_dev *pctldev, |
| 402 | unsigned fselector, |
| 403 | const char * const **groups, |
| 404 | unsigned * const ngroups) |
| 405 | { |
| 406 | struct pcs_device *pcs; |
| 407 | struct pcs_function *func; |
| 408 | |
| 409 | pcs = pinctrl_dev_get_drvdata(pctldev); |
| 410 | func = radix_tree_lookup(&pcs->ftree, fselector); |
| 411 | if (!func) { |
| 412 | dev_err(pcs->dev, "%s could not find function%i\n", |
| 413 | __func__, fselector); |
| 414 | return -EINVAL; |
| 415 | } |
| 416 | *groups = func->pgnames; |
| 417 | *ngroups = func->npgnames; |
| 418 | |
| 419 | return 0; |
| 420 | } |
| 421 | |
Haojian Zhuang | 9dddb4d | 2013-02-17 19:42:55 +0800 | [diff] [blame] | 422 | static int pcs_get_function(struct pinctrl_dev *pctldev, unsigned pin, |
| 423 | struct pcs_function **func) |
| 424 | { |
| 425 | struct pcs_device *pcs = pinctrl_dev_get_drvdata(pctldev); |
| 426 | struct pin_desc *pdesc = pin_desc_get(pctldev, pin); |
| 427 | const struct pinctrl_setting_mux *setting; |
| 428 | unsigned fselector; |
| 429 | |
| 430 | /* If pin is not described in DTS & enabled, mux_setting is NULL. */ |
| 431 | setting = pdesc->mux_setting; |
| 432 | if (!setting) |
| 433 | return -ENOTSUPP; |
| 434 | fselector = setting->func; |
| 435 | *func = radix_tree_lookup(&pcs->ftree, fselector); |
| 436 | if (!(*func)) { |
| 437 | dev_err(pcs->dev, "%s could not find function%i\n", |
| 438 | __func__, fselector); |
| 439 | return -ENOTSUPP; |
| 440 | } |
| 441 | return 0; |
| 442 | } |
| 443 | |
Linus Walleij | 03e9f0c | 2014-09-03 13:02:56 +0200 | [diff] [blame] | 444 | static int pcs_set_mux(struct pinctrl_dev *pctldev, unsigned fselector, |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 445 | unsigned group) |
| 446 | { |
| 447 | struct pcs_device *pcs; |
| 448 | struct pcs_function *func; |
| 449 | int i; |
| 450 | |
| 451 | pcs = pinctrl_dev_get_drvdata(pctldev); |
Haojian Zhuang | 477ac77 | 2013-02-17 19:42:54 +0800 | [diff] [blame] | 452 | /* If function mask is null, needn't enable it. */ |
| 453 | if (!pcs->fmask) |
| 454 | return 0; |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 455 | func = radix_tree_lookup(&pcs->ftree, fselector); |
| 456 | if (!func) |
| 457 | return -EINVAL; |
| 458 | |
| 459 | dev_dbg(pcs->dev, "enabling %s function%i\n", |
| 460 | func->name, fselector); |
| 461 | |
| 462 | for (i = 0; i < func->nvals; i++) { |
| 463 | struct pcs_func_vals *vals; |
Tony Lindgren | 3e6cee1 | 2013-10-02 21:39:40 -0700 | [diff] [blame] | 464 | unsigned long flags; |
Peter Ujfalusi | 9e605cb | 2012-09-11 11:54:24 +0300 | [diff] [blame] | 465 | unsigned val, mask; |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 466 | |
| 467 | vals = &func->vals[i]; |
Tony Lindgren | 3e6cee1 | 2013-10-02 21:39:40 -0700 | [diff] [blame] | 468 | raw_spin_lock_irqsave(&pcs->lock, flags); |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 469 | val = pcs->read(vals->reg); |
Manjunathappa, Prakash | 4e7e801 | 2013-05-21 19:38:00 +0530 | [diff] [blame] | 470 | |
| 471 | if (pcs->bits_per_mux) |
| 472 | mask = vals->mask; |
Peter Ujfalusi | 9e605cb | 2012-09-11 11:54:24 +0300 | [diff] [blame] | 473 | else |
Manjunathappa, Prakash | 4e7e801 | 2013-05-21 19:38:00 +0530 | [diff] [blame] | 474 | mask = pcs->fmask; |
Peter Ujfalusi | 9e605cb | 2012-09-11 11:54:24 +0300 | [diff] [blame] | 475 | |
| 476 | val &= ~mask; |
| 477 | val |= (vals->val & mask); |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 478 | pcs->write(val, vals->reg); |
Tony Lindgren | 3e6cee1 | 2013-10-02 21:39:40 -0700 | [diff] [blame] | 479 | raw_spin_unlock_irqrestore(&pcs->lock, flags); |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | return 0; |
| 483 | } |
| 484 | |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 485 | static int pcs_request_gpio(struct pinctrl_dev *pctldev, |
Haojian Zhuang | a1a277e | 2013-02-17 19:42:52 +0800 | [diff] [blame] | 486 | struct pinctrl_gpio_range *range, unsigned pin) |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 487 | { |
Haojian Zhuang | a1a277e | 2013-02-17 19:42:52 +0800 | [diff] [blame] | 488 | struct pcs_device *pcs = pinctrl_dev_get_drvdata(pctldev); |
| 489 | struct pcs_gpiofunc_range *frange = NULL; |
| 490 | struct list_head *pos, *tmp; |
| 491 | int mux_bytes = 0; |
| 492 | unsigned data; |
| 493 | |
Haojian Zhuang | 477ac77 | 2013-02-17 19:42:54 +0800 | [diff] [blame] | 494 | /* If function mask is null, return directly. */ |
| 495 | if (!pcs->fmask) |
| 496 | return -ENOTSUPP; |
| 497 | |
Haojian Zhuang | a1a277e | 2013-02-17 19:42:52 +0800 | [diff] [blame] | 498 | list_for_each_safe(pos, tmp, &pcs->gpiofuncs) { |
| 499 | frange = list_entry(pos, struct pcs_gpiofunc_range, node); |
| 500 | if (pin >= frange->offset + frange->npins |
| 501 | || pin < frange->offset) |
| 502 | continue; |
| 503 | mux_bytes = pcs->width / BITS_PER_BYTE; |
| 504 | data = pcs->read(pcs->base + pin * mux_bytes) & ~pcs->fmask; |
| 505 | data |= frange->gpiofunc; |
| 506 | pcs->write(data, pcs->base + pin * mux_bytes); |
| 507 | break; |
| 508 | } |
| 509 | return 0; |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 510 | } |
| 511 | |
Laurent Pinchart | 022ab14 | 2013-02-16 10:25:07 +0100 | [diff] [blame] | 512 | static const struct pinmux_ops pcs_pinmux_ops = { |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 513 | .get_functions_count = pcs_get_functions_count, |
| 514 | .get_function_name = pcs_get_function_name, |
| 515 | .get_function_groups = pcs_get_function_groups, |
Linus Walleij | 9e3a979 | 2014-09-05 09:53:23 +0200 | [diff] [blame] | 516 | .set_mux = pcs_set_mux, |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 517 | .gpio_request_enable = pcs_request_gpio, |
| 518 | }; |
| 519 | |
Haojian Zhuang | 9dddb4d | 2013-02-17 19:42:55 +0800 | [diff] [blame] | 520 | /* Clear BIAS value */ |
| 521 | static void pcs_pinconf_clear_bias(struct pinctrl_dev *pctldev, unsigned pin) |
| 522 | { |
| 523 | unsigned long config; |
| 524 | int i; |
| 525 | for (i = 0; i < ARRAY_SIZE(pcs_bias); i++) { |
| 526 | config = pinconf_to_config_packed(pcs_bias[i], 0); |
Sherman Yin | 03b054e | 2013-08-27 11:32:12 -0700 | [diff] [blame] | 527 | pcs_pinconf_set(pctldev, pin, &config, 1); |
Haojian Zhuang | 9dddb4d | 2013-02-17 19:42:55 +0800 | [diff] [blame] | 528 | } |
| 529 | } |
| 530 | |
| 531 | /* |
| 532 | * Check whether PIN_CONFIG_BIAS_DISABLE is valid. |
| 533 | * It's depend on that PULL_DOWN & PULL_UP configs are all invalid. |
| 534 | */ |
| 535 | static bool pcs_pinconf_bias_disable(struct pinctrl_dev *pctldev, unsigned pin) |
| 536 | { |
| 537 | unsigned long config; |
| 538 | int i; |
| 539 | |
| 540 | for (i = 0; i < ARRAY_SIZE(pcs_bias); i++) { |
| 541 | config = pinconf_to_config_packed(pcs_bias[i], 0); |
| 542 | if (!pcs_pinconf_get(pctldev, pin, &config)) |
| 543 | goto out; |
| 544 | } |
| 545 | return true; |
| 546 | out: |
| 547 | return false; |
| 548 | } |
| 549 | |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 550 | static int pcs_pinconf_get(struct pinctrl_dev *pctldev, |
| 551 | unsigned pin, unsigned long *config) |
| 552 | { |
Haojian Zhuang | 9dddb4d | 2013-02-17 19:42:55 +0800 | [diff] [blame] | 553 | struct pcs_device *pcs = pinctrl_dev_get_drvdata(pctldev); |
| 554 | struct pcs_function *func; |
| 555 | enum pin_config_param param; |
| 556 | unsigned offset = 0, data = 0, i, j, ret; |
| 557 | |
| 558 | ret = pcs_get_function(pctldev, pin, &func); |
| 559 | if (ret) |
| 560 | return ret; |
| 561 | |
| 562 | for (i = 0; i < func->nconfs; i++) { |
| 563 | param = pinconf_to_config_param(*config); |
| 564 | if (param == PIN_CONFIG_BIAS_DISABLE) { |
| 565 | if (pcs_pinconf_bias_disable(pctldev, pin)) { |
| 566 | *config = 0; |
| 567 | return 0; |
| 568 | } else { |
| 569 | return -ENOTSUPP; |
| 570 | } |
| 571 | } else if (param != func->conf[i].param) { |
| 572 | continue; |
| 573 | } |
| 574 | |
| 575 | offset = pin * (pcs->width / BITS_PER_BYTE); |
| 576 | data = pcs->read(pcs->base + offset) & func->conf[i].mask; |
| 577 | switch (func->conf[i].param) { |
| 578 | /* 4 parameters */ |
| 579 | case PIN_CONFIG_BIAS_PULL_DOWN: |
| 580 | case PIN_CONFIG_BIAS_PULL_UP: |
| 581 | case PIN_CONFIG_INPUT_SCHMITT_ENABLE: |
| 582 | if ((data != func->conf[i].enable) || |
| 583 | (data == func->conf[i].disable)) |
| 584 | return -ENOTSUPP; |
| 585 | *config = 0; |
| 586 | break; |
| 587 | /* 2 parameters */ |
| 588 | case PIN_CONFIG_INPUT_SCHMITT: |
| 589 | for (j = 0; j < func->nconfs; j++) { |
| 590 | switch (func->conf[j].param) { |
| 591 | case PIN_CONFIG_INPUT_SCHMITT_ENABLE: |
| 592 | if (data != func->conf[j].enable) |
| 593 | return -ENOTSUPP; |
| 594 | break; |
| 595 | default: |
| 596 | break; |
| 597 | } |
| 598 | } |
| 599 | *config = data; |
| 600 | break; |
| 601 | case PIN_CONFIG_DRIVE_STRENGTH: |
| 602 | case PIN_CONFIG_SLEW_RATE: |
Chao Xie | 4bd7547 | 2014-01-28 15:20:44 +0800 | [diff] [blame] | 603 | case PIN_CONFIG_LOW_POWER_MODE: |
Haojian Zhuang | 9dddb4d | 2013-02-17 19:42:55 +0800 | [diff] [blame] | 604 | default: |
| 605 | *config = data; |
| 606 | break; |
| 607 | } |
| 608 | return 0; |
| 609 | } |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 610 | return -ENOTSUPP; |
| 611 | } |
| 612 | |
| 613 | static int pcs_pinconf_set(struct pinctrl_dev *pctldev, |
Sherman Yin | 03b054e | 2013-08-27 11:32:12 -0700 | [diff] [blame] | 614 | unsigned pin, unsigned long *configs, |
| 615 | unsigned num_configs) |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 616 | { |
Haojian Zhuang | 9dddb4d | 2013-02-17 19:42:55 +0800 | [diff] [blame] | 617 | struct pcs_device *pcs = pinctrl_dev_get_drvdata(pctldev); |
| 618 | struct pcs_function *func; |
Haojian Zhuang | 7cba5b3 | 2013-03-13 16:01:26 +0800 | [diff] [blame] | 619 | unsigned offset = 0, shift = 0, i, data, ret; |
| 620 | u16 arg; |
Sherman Yin | 03b054e | 2013-08-27 11:32:12 -0700 | [diff] [blame] | 621 | int j; |
Haojian Zhuang | 9dddb4d | 2013-02-17 19:42:55 +0800 | [diff] [blame] | 622 | |
| 623 | ret = pcs_get_function(pctldev, pin, &func); |
| 624 | if (ret) |
| 625 | return ret; |
| 626 | |
Sherman Yin | 03b054e | 2013-08-27 11:32:12 -0700 | [diff] [blame] | 627 | for (j = 0; j < num_configs; j++) { |
| 628 | for (i = 0; i < func->nconfs; i++) { |
| 629 | if (pinconf_to_config_param(configs[j]) |
| 630 | != func->conf[i].param) |
| 631 | continue; |
| 632 | |
Haojian Zhuang | 9dddb4d | 2013-02-17 19:42:55 +0800 | [diff] [blame] | 633 | offset = pin * (pcs->width / BITS_PER_BYTE); |
| 634 | data = pcs->read(pcs->base + offset); |
Sherman Yin | 03b054e | 2013-08-27 11:32:12 -0700 | [diff] [blame] | 635 | arg = pinconf_to_config_argument(configs[j]); |
Haojian Zhuang | 9dddb4d | 2013-02-17 19:42:55 +0800 | [diff] [blame] | 636 | switch (func->conf[i].param) { |
| 637 | /* 2 parameters */ |
| 638 | case PIN_CONFIG_INPUT_SCHMITT: |
| 639 | case PIN_CONFIG_DRIVE_STRENGTH: |
| 640 | case PIN_CONFIG_SLEW_RATE: |
Chao Xie | 4bd7547 | 2014-01-28 15:20:44 +0800 | [diff] [blame] | 641 | case PIN_CONFIG_LOW_POWER_MODE: |
Haojian Zhuang | 9dddb4d | 2013-02-17 19:42:55 +0800 | [diff] [blame] | 642 | shift = ffs(func->conf[i].mask) - 1; |
Haojian Zhuang | 9dddb4d | 2013-02-17 19:42:55 +0800 | [diff] [blame] | 643 | data &= ~func->conf[i].mask; |
| 644 | data |= (arg << shift) & func->conf[i].mask; |
| 645 | break; |
| 646 | /* 4 parameters */ |
| 647 | case PIN_CONFIG_BIAS_DISABLE: |
| 648 | pcs_pinconf_clear_bias(pctldev, pin); |
| 649 | break; |
| 650 | case PIN_CONFIG_BIAS_PULL_DOWN: |
| 651 | case PIN_CONFIG_BIAS_PULL_UP: |
Haojian Zhuang | 7cba5b3 | 2013-03-13 16:01:26 +0800 | [diff] [blame] | 652 | if (arg) |
Haojian Zhuang | 9dddb4d | 2013-02-17 19:42:55 +0800 | [diff] [blame] | 653 | pcs_pinconf_clear_bias(pctldev, pin); |
| 654 | /* fall through */ |
| 655 | case PIN_CONFIG_INPUT_SCHMITT_ENABLE: |
| 656 | data &= ~func->conf[i].mask; |
Haojian Zhuang | 7cba5b3 | 2013-03-13 16:01:26 +0800 | [diff] [blame] | 657 | if (arg) |
Haojian Zhuang | 9dddb4d | 2013-02-17 19:42:55 +0800 | [diff] [blame] | 658 | data |= func->conf[i].enable; |
| 659 | else |
| 660 | data |= func->conf[i].disable; |
| 661 | break; |
| 662 | default: |
| 663 | return -ENOTSUPP; |
| 664 | } |
| 665 | pcs->write(data, pcs->base + offset); |
Sherman Yin | 03b054e | 2013-08-27 11:32:12 -0700 | [diff] [blame] | 666 | |
| 667 | break; |
Haojian Zhuang | 9dddb4d | 2013-02-17 19:42:55 +0800 | [diff] [blame] | 668 | } |
Sherman Yin | 03b054e | 2013-08-27 11:32:12 -0700 | [diff] [blame] | 669 | if (i >= func->nconfs) |
| 670 | return -ENOTSUPP; |
| 671 | } /* for each config */ |
| 672 | |
| 673 | return 0; |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | static int pcs_pinconf_group_get(struct pinctrl_dev *pctldev, |
| 677 | unsigned group, unsigned long *config) |
| 678 | { |
Haojian Zhuang | 9dddb4d | 2013-02-17 19:42:55 +0800 | [diff] [blame] | 679 | const unsigned *pins; |
| 680 | unsigned npins, old = 0; |
| 681 | int i, ret; |
| 682 | |
| 683 | ret = pcs_get_group_pins(pctldev, group, &pins, &npins); |
| 684 | if (ret) |
| 685 | return ret; |
| 686 | for (i = 0; i < npins; i++) { |
| 687 | if (pcs_pinconf_get(pctldev, pins[i], config)) |
| 688 | return -ENOTSUPP; |
| 689 | /* configs do not match between two pins */ |
| 690 | if (i && (old != *config)) |
| 691 | return -ENOTSUPP; |
| 692 | old = *config; |
| 693 | } |
| 694 | return 0; |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | static int pcs_pinconf_group_set(struct pinctrl_dev *pctldev, |
Sherman Yin | 03b054e | 2013-08-27 11:32:12 -0700 | [diff] [blame] | 698 | unsigned group, unsigned long *configs, |
| 699 | unsigned num_configs) |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 700 | { |
Haojian Zhuang | 9dddb4d | 2013-02-17 19:42:55 +0800 | [diff] [blame] | 701 | const unsigned *pins; |
| 702 | unsigned npins; |
| 703 | int i, ret; |
| 704 | |
| 705 | ret = pcs_get_group_pins(pctldev, group, &pins, &npins); |
| 706 | if (ret) |
| 707 | return ret; |
| 708 | for (i = 0; i < npins; i++) { |
Sherman Yin | 03b054e | 2013-08-27 11:32:12 -0700 | [diff] [blame] | 709 | if (pcs_pinconf_set(pctldev, pins[i], configs, num_configs)) |
Haojian Zhuang | 9dddb4d | 2013-02-17 19:42:55 +0800 | [diff] [blame] | 710 | return -ENOTSUPP; |
| 711 | } |
| 712 | return 0; |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | static void pcs_pinconf_dbg_show(struct pinctrl_dev *pctldev, |
Haojian Zhuang | 9dddb4d | 2013-02-17 19:42:55 +0800 | [diff] [blame] | 716 | struct seq_file *s, unsigned pin) |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 717 | { |
| 718 | } |
| 719 | |
| 720 | static void pcs_pinconf_group_dbg_show(struct pinctrl_dev *pctldev, |
| 721 | struct seq_file *s, unsigned selector) |
| 722 | { |
| 723 | } |
| 724 | |
Haojian Zhuang | 9dddb4d | 2013-02-17 19:42:55 +0800 | [diff] [blame] | 725 | static void pcs_pinconf_config_dbg_show(struct pinctrl_dev *pctldev, |
| 726 | struct seq_file *s, |
| 727 | unsigned long config) |
| 728 | { |
| 729 | pinconf_generic_dump_config(pctldev, s, config); |
| 730 | } |
| 731 | |
Laurent Pinchart | 022ab14 | 2013-02-16 10:25:07 +0100 | [diff] [blame] | 732 | static const struct pinconf_ops pcs_pinconf_ops = { |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 733 | .pin_config_get = pcs_pinconf_get, |
| 734 | .pin_config_set = pcs_pinconf_set, |
| 735 | .pin_config_group_get = pcs_pinconf_group_get, |
| 736 | .pin_config_group_set = pcs_pinconf_group_set, |
| 737 | .pin_config_dbg_show = pcs_pinconf_dbg_show, |
| 738 | .pin_config_group_dbg_show = pcs_pinconf_group_dbg_show, |
Haojian Zhuang | 9dddb4d | 2013-02-17 19:42:55 +0800 | [diff] [blame] | 739 | .pin_config_config_dbg_show = pcs_pinconf_config_dbg_show, |
Axel Lin | a7bbdd7 | 2013-03-04 13:47:39 +0800 | [diff] [blame] | 740 | .is_generic = true, |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 741 | }; |
| 742 | |
| 743 | /** |
| 744 | * pcs_add_pin() - add a pin to the static per controller pin array |
| 745 | * @pcs: pcs driver instance |
| 746 | * @offset: register offset from base |
| 747 | */ |
Manjunathappa, Prakash | 6f924b0 | 2013-05-21 19:38:01 +0530 | [diff] [blame] | 748 | static int pcs_add_pin(struct pcs_device *pcs, unsigned offset, |
| 749 | unsigned pin_pos) |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 750 | { |
Tony Lindgren | 5896862 | 2014-04-10 16:47:19 -0700 | [diff] [blame] | 751 | struct pcs_soc_data *pcs_soc = &pcs->socdata; |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 752 | struct pinctrl_pin_desc *pin; |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 753 | int i; |
| 754 | |
| 755 | i = pcs->pins.cur; |
| 756 | if (i >= pcs->desc.npins) { |
| 757 | dev_err(pcs->dev, "too many pins, max %i\n", |
| 758 | pcs->desc.npins); |
| 759 | return -ENOMEM; |
| 760 | } |
| 761 | |
Tony Lindgren | 5896862 | 2014-04-10 16:47:19 -0700 | [diff] [blame] | 762 | if (pcs_soc->irq_enable_mask) { |
| 763 | unsigned val; |
| 764 | |
| 765 | val = pcs->read(pcs->base + offset); |
| 766 | if (val & pcs_soc->irq_enable_mask) { |
| 767 | dev_dbg(pcs->dev, "irq enabled at boot for pin at %lx (%x), clearing\n", |
| 768 | (unsigned long)pcs->res->start + offset, val); |
| 769 | val &= ~pcs_soc->irq_enable_mask; |
| 770 | pcs->write(val, pcs->base + offset); |
| 771 | } |
| 772 | } |
| 773 | |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 774 | pin = &pcs->pins.pa[i]; |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 775 | pin->number = i; |
| 776 | pcs->pins.cur++; |
| 777 | |
| 778 | return i; |
| 779 | } |
| 780 | |
| 781 | /** |
| 782 | * pcs_allocate_pin_table() - adds all the pins for the pinctrl driver |
| 783 | * @pcs: pcs driver instance |
| 784 | * |
| 785 | * In case of errors, resources are freed in pcs_free_resources. |
| 786 | * |
| 787 | * If your hardware needs holes in the address space, then just set |
| 788 | * up multiple driver instances. |
| 789 | */ |
Greg Kroah-Hartman | 150632b | 2012-12-21 13:10:23 -0800 | [diff] [blame] | 790 | static int pcs_allocate_pin_table(struct pcs_device *pcs) |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 791 | { |
| 792 | int mux_bytes, nr_pins, i; |
Manjunathappa, Prakash | 6f924b0 | 2013-05-21 19:38:01 +0530 | [diff] [blame] | 793 | int num_pins_in_register = 0; |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 794 | |
| 795 | mux_bytes = pcs->width / BITS_PER_BYTE; |
Manjunathappa, Prakash | 4e7e801 | 2013-05-21 19:38:00 +0530 | [diff] [blame] | 796 | |
| 797 | if (pcs->bits_per_mux) { |
| 798 | pcs->bits_per_pin = fls(pcs->fmask); |
| 799 | nr_pins = (pcs->size * BITS_PER_BYTE) / pcs->bits_per_pin; |
Manjunathappa, Prakash | 6f924b0 | 2013-05-21 19:38:01 +0530 | [diff] [blame] | 800 | num_pins_in_register = pcs->width / pcs->bits_per_pin; |
Manjunathappa, Prakash | 4e7e801 | 2013-05-21 19:38:00 +0530 | [diff] [blame] | 801 | } else { |
| 802 | nr_pins = pcs->size / mux_bytes; |
| 803 | } |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 804 | |
| 805 | dev_dbg(pcs->dev, "allocating %i pins\n", nr_pins); |
| 806 | pcs->pins.pa = devm_kzalloc(pcs->dev, |
| 807 | sizeof(*pcs->pins.pa) * nr_pins, |
| 808 | GFP_KERNEL); |
| 809 | if (!pcs->pins.pa) |
| 810 | return -ENOMEM; |
| 811 | |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 812 | pcs->desc.pins = pcs->pins.pa; |
| 813 | pcs->desc.npins = nr_pins; |
| 814 | |
| 815 | for (i = 0; i < pcs->desc.npins; i++) { |
| 816 | unsigned offset; |
| 817 | int res; |
Manjunathappa, Prakash | 4e7e801 | 2013-05-21 19:38:00 +0530 | [diff] [blame] | 818 | int byte_num; |
Manjunathappa, Prakash | 6f924b0 | 2013-05-21 19:38:01 +0530 | [diff] [blame] | 819 | int pin_pos = 0; |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 820 | |
Manjunathappa, Prakash | 4e7e801 | 2013-05-21 19:38:00 +0530 | [diff] [blame] | 821 | if (pcs->bits_per_mux) { |
| 822 | byte_num = (pcs->bits_per_pin * i) / BITS_PER_BYTE; |
| 823 | offset = (byte_num / mux_bytes) * mux_bytes; |
Manjunathappa, Prakash | 6f924b0 | 2013-05-21 19:38:01 +0530 | [diff] [blame] | 824 | pin_pos = i % num_pins_in_register; |
Manjunathappa, Prakash | 4e7e801 | 2013-05-21 19:38:00 +0530 | [diff] [blame] | 825 | } else { |
| 826 | offset = i * mux_bytes; |
| 827 | } |
Manjunathappa, Prakash | 6f924b0 | 2013-05-21 19:38:01 +0530 | [diff] [blame] | 828 | res = pcs_add_pin(pcs, offset, pin_pos); |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 829 | if (res < 0) { |
| 830 | dev_err(pcs->dev, "error adding pins: %i\n", res); |
| 831 | return res; |
| 832 | } |
| 833 | } |
| 834 | |
| 835 | return 0; |
| 836 | } |
| 837 | |
| 838 | /** |
| 839 | * pcs_add_function() - adds a new function to the function list |
| 840 | * @pcs: pcs driver instance |
| 841 | * @np: device node of the mux entry |
| 842 | * @name: name of the function |
| 843 | * @vals: array of mux register value pairs used by the function |
| 844 | * @nvals: number of mux register value pairs |
| 845 | * @pgnames: array of pingroup names for the function |
| 846 | * @npgnames: number of pingroup names |
| 847 | */ |
| 848 | static struct pcs_function *pcs_add_function(struct pcs_device *pcs, |
| 849 | struct device_node *np, |
| 850 | const char *name, |
| 851 | struct pcs_func_vals *vals, |
| 852 | unsigned nvals, |
| 853 | const char **pgnames, |
| 854 | unsigned npgnames) |
| 855 | { |
| 856 | struct pcs_function *function; |
| 857 | |
| 858 | function = devm_kzalloc(pcs->dev, sizeof(*function), GFP_KERNEL); |
| 859 | if (!function) |
| 860 | return NULL; |
| 861 | |
| 862 | function->name = name; |
| 863 | function->vals = vals; |
| 864 | function->nvals = nvals; |
| 865 | function->pgnames = pgnames; |
| 866 | function->npgnames = npgnames; |
| 867 | |
| 868 | mutex_lock(&pcs->mutex); |
| 869 | list_add_tail(&function->node, &pcs->functions); |
| 870 | radix_tree_insert(&pcs->ftree, pcs->nfuncs, function); |
| 871 | pcs->nfuncs++; |
| 872 | mutex_unlock(&pcs->mutex); |
| 873 | |
| 874 | return function; |
| 875 | } |
| 876 | |
| 877 | static void pcs_remove_function(struct pcs_device *pcs, |
| 878 | struct pcs_function *function) |
| 879 | { |
| 880 | int i; |
| 881 | |
| 882 | mutex_lock(&pcs->mutex); |
| 883 | for (i = 0; i < pcs->nfuncs; i++) { |
| 884 | struct pcs_function *found; |
| 885 | |
| 886 | found = radix_tree_lookup(&pcs->ftree, i); |
| 887 | if (found == function) |
| 888 | radix_tree_delete(&pcs->ftree, i); |
| 889 | } |
| 890 | list_del(&function->node); |
| 891 | mutex_unlock(&pcs->mutex); |
| 892 | } |
| 893 | |
| 894 | /** |
| 895 | * pcs_add_pingroup() - add a pingroup to the pingroup list |
| 896 | * @pcs: pcs driver instance |
| 897 | * @np: device node of the mux entry |
| 898 | * @name: name of the pingroup |
| 899 | * @gpins: array of the pins that belong to the group |
| 900 | * @ngpins: number of pins in the group |
| 901 | */ |
| 902 | static int pcs_add_pingroup(struct pcs_device *pcs, |
| 903 | struct device_node *np, |
| 904 | const char *name, |
| 905 | int *gpins, |
| 906 | int ngpins) |
| 907 | { |
| 908 | struct pcs_pingroup *pingroup; |
| 909 | |
| 910 | pingroup = devm_kzalloc(pcs->dev, sizeof(*pingroup), GFP_KERNEL); |
| 911 | if (!pingroup) |
| 912 | return -ENOMEM; |
| 913 | |
| 914 | pingroup->name = name; |
| 915 | pingroup->np = np; |
| 916 | pingroup->gpins = gpins; |
| 917 | pingroup->ngpins = ngpins; |
| 918 | |
| 919 | mutex_lock(&pcs->mutex); |
| 920 | list_add_tail(&pingroup->node, &pcs->pingroups); |
| 921 | radix_tree_insert(&pcs->pgtree, pcs->ngroups, pingroup); |
| 922 | pcs->ngroups++; |
| 923 | mutex_unlock(&pcs->mutex); |
| 924 | |
| 925 | return 0; |
| 926 | } |
| 927 | |
| 928 | /** |
| 929 | * pcs_get_pin_by_offset() - get a pin index based on the register offset |
| 930 | * @pcs: pcs driver instance |
| 931 | * @offset: register offset from the base |
| 932 | * |
| 933 | * Note that this is OK as long as the pins are in a static array. |
| 934 | */ |
| 935 | static int pcs_get_pin_by_offset(struct pcs_device *pcs, unsigned offset) |
| 936 | { |
| 937 | unsigned index; |
| 938 | |
| 939 | if (offset >= pcs->size) { |
| 940 | dev_err(pcs->dev, "mux offset out of range: 0x%x (0x%x)\n", |
| 941 | offset, pcs->size); |
| 942 | return -EINVAL; |
| 943 | } |
| 944 | |
Manjunathappa, Prakash | 4e7e801 | 2013-05-21 19:38:00 +0530 | [diff] [blame] | 945 | if (pcs->bits_per_mux) |
| 946 | index = (offset * BITS_PER_BYTE) / pcs->bits_per_pin; |
| 947 | else |
| 948 | index = offset / (pcs->width / BITS_PER_BYTE); |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 949 | |
| 950 | return index; |
| 951 | } |
| 952 | |
Haojian Zhuang | 9dddb4d | 2013-02-17 19:42:55 +0800 | [diff] [blame] | 953 | /* |
| 954 | * check whether data matches enable bits or disable bits |
| 955 | * Return value: 1 for matching enable bits, 0 for matching disable bits, |
| 956 | * and negative value for matching failure. |
| 957 | */ |
| 958 | static int pcs_config_match(unsigned data, unsigned enable, unsigned disable) |
| 959 | { |
| 960 | int ret = -EINVAL; |
| 961 | |
| 962 | if (data == enable) |
| 963 | ret = 1; |
| 964 | else if (data == disable) |
| 965 | ret = 0; |
| 966 | return ret; |
| 967 | } |
| 968 | |
| 969 | static void add_config(struct pcs_conf_vals **conf, enum pin_config_param param, |
| 970 | unsigned value, unsigned enable, unsigned disable, |
| 971 | unsigned mask) |
| 972 | { |
| 973 | (*conf)->param = param; |
| 974 | (*conf)->val = value; |
| 975 | (*conf)->enable = enable; |
| 976 | (*conf)->disable = disable; |
| 977 | (*conf)->mask = mask; |
| 978 | (*conf)++; |
| 979 | } |
| 980 | |
| 981 | static void add_setting(unsigned long **setting, enum pin_config_param param, |
| 982 | unsigned arg) |
| 983 | { |
| 984 | **setting = pinconf_to_config_packed(param, arg); |
| 985 | (*setting)++; |
| 986 | } |
| 987 | |
| 988 | /* add pinconf setting with 2 parameters */ |
| 989 | static void pcs_add_conf2(struct pcs_device *pcs, struct device_node *np, |
| 990 | const char *name, enum pin_config_param param, |
| 991 | struct pcs_conf_vals **conf, unsigned long **settings) |
| 992 | { |
Haojian Zhuang | 7cba5b3 | 2013-03-13 16:01:26 +0800 | [diff] [blame] | 993 | unsigned value[2], shift; |
Haojian Zhuang | 9dddb4d | 2013-02-17 19:42:55 +0800 | [diff] [blame] | 994 | int ret; |
| 995 | |
| 996 | ret = of_property_read_u32_array(np, name, value, 2); |
| 997 | if (ret) |
| 998 | return; |
| 999 | /* set value & mask */ |
| 1000 | value[0] &= value[1]; |
Haojian Zhuang | 7cba5b3 | 2013-03-13 16:01:26 +0800 | [diff] [blame] | 1001 | shift = ffs(value[1]) - 1; |
Haojian Zhuang | 9dddb4d | 2013-02-17 19:42:55 +0800 | [diff] [blame] | 1002 | /* skip enable & disable */ |
| 1003 | add_config(conf, param, value[0], 0, 0, value[1]); |
Haojian Zhuang | 7cba5b3 | 2013-03-13 16:01:26 +0800 | [diff] [blame] | 1004 | add_setting(settings, param, value[0] >> shift); |
Haojian Zhuang | 9dddb4d | 2013-02-17 19:42:55 +0800 | [diff] [blame] | 1005 | } |
| 1006 | |
| 1007 | /* add pinconf setting with 4 parameters */ |
| 1008 | static void pcs_add_conf4(struct pcs_device *pcs, struct device_node *np, |
| 1009 | const char *name, enum pin_config_param param, |
| 1010 | struct pcs_conf_vals **conf, unsigned long **settings) |
| 1011 | { |
| 1012 | unsigned value[4]; |
| 1013 | int ret; |
| 1014 | |
| 1015 | /* value to set, enable, disable, mask */ |
| 1016 | ret = of_property_read_u32_array(np, name, value, 4); |
| 1017 | if (ret) |
| 1018 | return; |
| 1019 | if (!value[3]) { |
| 1020 | dev_err(pcs->dev, "mask field of the property can't be 0\n"); |
| 1021 | return; |
| 1022 | } |
| 1023 | value[0] &= value[3]; |
| 1024 | value[1] &= value[3]; |
| 1025 | value[2] &= value[3]; |
| 1026 | ret = pcs_config_match(value[0], value[1], value[2]); |
| 1027 | if (ret < 0) |
| 1028 | dev_dbg(pcs->dev, "failed to match enable or disable bits\n"); |
| 1029 | add_config(conf, param, value[0], value[1], value[2], value[3]); |
| 1030 | add_setting(settings, param, ret); |
| 1031 | } |
| 1032 | |
| 1033 | static int pcs_parse_pinconf(struct pcs_device *pcs, struct device_node *np, |
| 1034 | struct pcs_function *func, |
| 1035 | struct pinctrl_map **map) |
| 1036 | |
| 1037 | { |
| 1038 | struct pinctrl_map *m = *map; |
| 1039 | int i = 0, nconfs = 0; |
| 1040 | unsigned long *settings = NULL, *s = NULL; |
| 1041 | struct pcs_conf_vals *conf = NULL; |
| 1042 | struct pcs_conf_type prop2[] = { |
| 1043 | { "pinctrl-single,drive-strength", PIN_CONFIG_DRIVE_STRENGTH, }, |
| 1044 | { "pinctrl-single,slew-rate", PIN_CONFIG_SLEW_RATE, }, |
| 1045 | { "pinctrl-single,input-schmitt", PIN_CONFIG_INPUT_SCHMITT, }, |
Chao Xie | 4bd7547 | 2014-01-28 15:20:44 +0800 | [diff] [blame] | 1046 | { "pinctrl-single,low-power-mode", PIN_CONFIG_LOW_POWER_MODE, }, |
Haojian Zhuang | 9dddb4d | 2013-02-17 19:42:55 +0800 | [diff] [blame] | 1047 | }; |
| 1048 | struct pcs_conf_type prop4[] = { |
| 1049 | { "pinctrl-single,bias-pullup", PIN_CONFIG_BIAS_PULL_UP, }, |
| 1050 | { "pinctrl-single,bias-pulldown", PIN_CONFIG_BIAS_PULL_DOWN, }, |
| 1051 | { "pinctrl-single,input-schmitt-enable", |
| 1052 | PIN_CONFIG_INPUT_SCHMITT_ENABLE, }, |
| 1053 | }; |
| 1054 | |
| 1055 | /* If pinconf isn't supported, don't parse properties in below. */ |
Tony Lindgren | 02e483f | 2013-10-02 21:39:39 -0700 | [diff] [blame] | 1056 | if (!PCS_HAS_PINCONF) |
Haojian Zhuang | 9dddb4d | 2013-02-17 19:42:55 +0800 | [diff] [blame] | 1057 | return 0; |
| 1058 | |
| 1059 | /* cacluate how much properties are supported in current node */ |
| 1060 | for (i = 0; i < ARRAY_SIZE(prop2); i++) { |
| 1061 | if (of_find_property(np, prop2[i].name, NULL)) |
| 1062 | nconfs++; |
| 1063 | } |
| 1064 | for (i = 0; i < ARRAY_SIZE(prop4); i++) { |
| 1065 | if (of_find_property(np, prop4[i].name, NULL)) |
| 1066 | nconfs++; |
| 1067 | } |
| 1068 | if (!nconfs) |
| 1069 | return 0; |
| 1070 | |
| 1071 | func->conf = devm_kzalloc(pcs->dev, |
| 1072 | sizeof(struct pcs_conf_vals) * nconfs, |
| 1073 | GFP_KERNEL); |
| 1074 | if (!func->conf) |
| 1075 | return -ENOMEM; |
| 1076 | func->nconfs = nconfs; |
| 1077 | conf = &(func->conf[0]); |
| 1078 | m++; |
| 1079 | settings = devm_kzalloc(pcs->dev, sizeof(unsigned long) * nconfs, |
| 1080 | GFP_KERNEL); |
| 1081 | if (!settings) |
| 1082 | return -ENOMEM; |
| 1083 | s = &settings[0]; |
| 1084 | |
| 1085 | for (i = 0; i < ARRAY_SIZE(prop2); i++) |
| 1086 | pcs_add_conf2(pcs, np, prop2[i].name, prop2[i].param, |
| 1087 | &conf, &s); |
| 1088 | for (i = 0; i < ARRAY_SIZE(prop4); i++) |
| 1089 | pcs_add_conf4(pcs, np, prop4[i].name, prop4[i].param, |
| 1090 | &conf, &s); |
| 1091 | m->type = PIN_MAP_TYPE_CONFIGS_GROUP; |
| 1092 | m->data.configs.group_or_pin = np->name; |
| 1093 | m->data.configs.configs = settings; |
| 1094 | m->data.configs.num_configs = nconfs; |
| 1095 | return 0; |
| 1096 | } |
| 1097 | |
| 1098 | static void pcs_free_pingroups(struct pcs_device *pcs); |
| 1099 | |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 1100 | /** |
| 1101 | * smux_parse_one_pinctrl_entry() - parses a device tree mux entry |
| 1102 | * @pcs: pinctrl driver instance |
| 1103 | * @np: device node of the mux entry |
| 1104 | * @map: map entry |
Haojian Zhuang | 9dddb4d | 2013-02-17 19:42:55 +0800 | [diff] [blame] | 1105 | * @num_maps: number of map |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 1106 | * @pgnames: pingroup names |
| 1107 | * |
| 1108 | * Note that this binding currently supports only sets of one register + value. |
| 1109 | * |
| 1110 | * Also note that this driver tries to avoid understanding pin and function |
| 1111 | * names because of the extra bloat they would cause especially in the case of |
| 1112 | * a large number of pins. This driver just sets what is specified for the board |
| 1113 | * in the .dts file. Further user space debugging tools can be developed to |
| 1114 | * decipher the pin and function names using debugfs. |
| 1115 | * |
| 1116 | * If you are concerned about the boot time, set up the static pins in |
| 1117 | * the bootloader, and only set up selected pins as device tree entries. |
| 1118 | */ |
| 1119 | static int pcs_parse_one_pinctrl_entry(struct pcs_device *pcs, |
| 1120 | struct device_node *np, |
| 1121 | struct pinctrl_map **map, |
Haojian Zhuang | 9dddb4d | 2013-02-17 19:42:55 +0800 | [diff] [blame] | 1122 | unsigned *num_maps, |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 1123 | const char **pgnames) |
| 1124 | { |
| 1125 | struct pcs_func_vals *vals; |
| 1126 | const __be32 *mux; |
Manjunathappa, Prakash | 4e7e801 | 2013-05-21 19:38:00 +0530 | [diff] [blame] | 1127 | int size, rows, *pins, index = 0, found = 0, res = -ENOMEM; |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 1128 | struct pcs_function *function; |
| 1129 | |
Manjunathappa, Prakash | 4e7e801 | 2013-05-21 19:38:00 +0530 | [diff] [blame] | 1130 | mux = of_get_property(np, PCS_MUX_PINS_NAME, &size); |
| 1131 | if ((!mux) || (size < sizeof(*mux) * 2)) { |
| 1132 | dev_err(pcs->dev, "bad data for mux %s\n", |
| 1133 | np->name); |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 1134 | return -EINVAL; |
| 1135 | } |
| 1136 | |
| 1137 | size /= sizeof(*mux); /* Number of elements in array */ |
Manjunathappa, Prakash | 4e7e801 | 2013-05-21 19:38:00 +0530 | [diff] [blame] | 1138 | rows = size / 2; |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 1139 | |
| 1140 | vals = devm_kzalloc(pcs->dev, sizeof(*vals) * rows, GFP_KERNEL); |
| 1141 | if (!vals) |
| 1142 | return -ENOMEM; |
| 1143 | |
| 1144 | pins = devm_kzalloc(pcs->dev, sizeof(*pins) * rows, GFP_KERNEL); |
| 1145 | if (!pins) |
| 1146 | goto free_vals; |
| 1147 | |
| 1148 | while (index < size) { |
| 1149 | unsigned offset, val; |
| 1150 | int pin; |
| 1151 | |
| 1152 | offset = be32_to_cpup(mux + index++); |
| 1153 | val = be32_to_cpup(mux + index++); |
| 1154 | vals[found].reg = pcs->base + offset; |
| 1155 | vals[found].val = val; |
| 1156 | |
| 1157 | pin = pcs_get_pin_by_offset(pcs, offset); |
| 1158 | if (pin < 0) { |
| 1159 | dev_err(pcs->dev, |
| 1160 | "could not add functions for %s %ux\n", |
| 1161 | np->name, offset); |
| 1162 | break; |
| 1163 | } |
| 1164 | pins[found++] = pin; |
| 1165 | } |
| 1166 | |
| 1167 | pgnames[0] = np->name; |
| 1168 | function = pcs_add_function(pcs, np, np->name, vals, found, pgnames, 1); |
| 1169 | if (!function) |
| 1170 | goto free_pins; |
| 1171 | |
| 1172 | res = pcs_add_pingroup(pcs, np, np->name, pins, found); |
| 1173 | if (res < 0) |
| 1174 | goto free_function; |
| 1175 | |
| 1176 | (*map)->type = PIN_MAP_TYPE_MUX_GROUP; |
| 1177 | (*map)->data.mux.group = np->name; |
| 1178 | (*map)->data.mux.function = np->name; |
| 1179 | |
Tony Lindgren | 02e483f | 2013-10-02 21:39:39 -0700 | [diff] [blame] | 1180 | if (PCS_HAS_PINCONF) { |
Wei Yongjun | 18442e6 | 2013-05-07 20:06:19 +0800 | [diff] [blame] | 1181 | res = pcs_parse_pinconf(pcs, np, function, map); |
| 1182 | if (res) |
Haojian Zhuang | 9dddb4d | 2013-02-17 19:42:55 +0800 | [diff] [blame] | 1183 | goto free_pingroups; |
| 1184 | *num_maps = 2; |
| 1185 | } else { |
| 1186 | *num_maps = 1; |
| 1187 | } |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 1188 | return 0; |
| 1189 | |
Haojian Zhuang | 9dddb4d | 2013-02-17 19:42:55 +0800 | [diff] [blame] | 1190 | free_pingroups: |
| 1191 | pcs_free_pingroups(pcs); |
| 1192 | *num_maps = 1; |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 1193 | free_function: |
| 1194 | pcs_remove_function(pcs, function); |
| 1195 | |
| 1196 | free_pins: |
| 1197 | devm_kfree(pcs->dev, pins); |
| 1198 | |
| 1199 | free_vals: |
| 1200 | devm_kfree(pcs->dev, vals); |
| 1201 | |
| 1202 | return res; |
| 1203 | } |
Manjunathappa, Prakash | 4e7e801 | 2013-05-21 19:38:00 +0530 | [diff] [blame] | 1204 | |
| 1205 | #define PARAMS_FOR_BITS_PER_MUX 3 |
| 1206 | |
| 1207 | static int pcs_parse_bits_in_pinctrl_entry(struct pcs_device *pcs, |
| 1208 | struct device_node *np, |
| 1209 | struct pinctrl_map **map, |
| 1210 | unsigned *num_maps, |
| 1211 | const char **pgnames) |
| 1212 | { |
| 1213 | struct pcs_func_vals *vals; |
| 1214 | const __be32 *mux; |
| 1215 | int size, rows, *pins, index = 0, found = 0, res = -ENOMEM; |
| 1216 | int npins_in_row; |
| 1217 | struct pcs_function *function; |
| 1218 | |
| 1219 | mux = of_get_property(np, PCS_MUX_BITS_NAME, &size); |
| 1220 | |
| 1221 | if (!mux) { |
| 1222 | dev_err(pcs->dev, "no valid property for %s\n", np->name); |
| 1223 | return -EINVAL; |
| 1224 | } |
| 1225 | |
| 1226 | if (size < (sizeof(*mux) * PARAMS_FOR_BITS_PER_MUX)) { |
| 1227 | dev_err(pcs->dev, "bad data for %s\n", np->name); |
| 1228 | return -EINVAL; |
| 1229 | } |
| 1230 | |
| 1231 | /* Number of elements in array */ |
| 1232 | size /= sizeof(*mux); |
| 1233 | |
| 1234 | rows = size / PARAMS_FOR_BITS_PER_MUX; |
| 1235 | npins_in_row = pcs->width / pcs->bits_per_pin; |
| 1236 | |
| 1237 | vals = devm_kzalloc(pcs->dev, sizeof(*vals) * rows * npins_in_row, |
| 1238 | GFP_KERNEL); |
| 1239 | if (!vals) |
| 1240 | return -ENOMEM; |
| 1241 | |
| 1242 | pins = devm_kzalloc(pcs->dev, sizeof(*pins) * rows * npins_in_row, |
| 1243 | GFP_KERNEL); |
| 1244 | if (!pins) |
| 1245 | goto free_vals; |
| 1246 | |
| 1247 | while (index < size) { |
| 1248 | unsigned offset, val; |
| 1249 | unsigned mask, bit_pos, val_pos, mask_pos, submask; |
| 1250 | unsigned pin_num_from_lsb; |
| 1251 | int pin; |
| 1252 | |
| 1253 | offset = be32_to_cpup(mux + index++); |
| 1254 | val = be32_to_cpup(mux + index++); |
| 1255 | mask = be32_to_cpup(mux + index++); |
| 1256 | |
| 1257 | /* Parse pins in each row from LSB */ |
| 1258 | while (mask) { |
Keerthy | 56b367c | 2016-04-14 10:29:16 +0530 | [diff] [blame] | 1259 | bit_pos = __ffs(mask); |
Manjunathappa, Prakash | 4e7e801 | 2013-05-21 19:38:00 +0530 | [diff] [blame] | 1260 | pin_num_from_lsb = bit_pos / pcs->bits_per_pin; |
Keerthy | 56b367c | 2016-04-14 10:29:16 +0530 | [diff] [blame] | 1261 | mask_pos = ((pcs->fmask) << bit_pos); |
Manjunathappa, Prakash | 4e7e801 | 2013-05-21 19:38:00 +0530 | [diff] [blame] | 1262 | val_pos = val & mask_pos; |
| 1263 | submask = mask & mask_pos; |
Tomi Valkeinen | ad5d25f | 2014-01-09 14:50:29 +0200 | [diff] [blame] | 1264 | |
| 1265 | if ((mask & mask_pos) == 0) { |
| 1266 | dev_err(pcs->dev, |
| 1267 | "Invalid mask for %s at 0x%x\n", |
| 1268 | np->name, offset); |
| 1269 | break; |
| 1270 | } |
| 1271 | |
Manjunathappa, Prakash | 4e7e801 | 2013-05-21 19:38:00 +0530 | [diff] [blame] | 1272 | mask &= ~mask_pos; |
| 1273 | |
| 1274 | if (submask != mask_pos) { |
| 1275 | dev_warn(pcs->dev, |
| 1276 | "Invalid submask 0x%x for %s at 0x%x\n", |
| 1277 | submask, np->name, offset); |
| 1278 | continue; |
| 1279 | } |
| 1280 | |
| 1281 | vals[found].mask = submask; |
| 1282 | vals[found].reg = pcs->base + offset; |
| 1283 | vals[found].val = val_pos; |
| 1284 | |
| 1285 | pin = pcs_get_pin_by_offset(pcs, offset); |
| 1286 | if (pin < 0) { |
| 1287 | dev_err(pcs->dev, |
| 1288 | "could not add functions for %s %ux\n", |
| 1289 | np->name, offset); |
| 1290 | break; |
| 1291 | } |
| 1292 | pins[found++] = pin + pin_num_from_lsb; |
| 1293 | } |
| 1294 | } |
| 1295 | |
| 1296 | pgnames[0] = np->name; |
| 1297 | function = pcs_add_function(pcs, np, np->name, vals, found, pgnames, 1); |
| 1298 | if (!function) |
| 1299 | goto free_pins; |
| 1300 | |
| 1301 | res = pcs_add_pingroup(pcs, np, np->name, pins, found); |
| 1302 | if (res < 0) |
| 1303 | goto free_function; |
| 1304 | |
| 1305 | (*map)->type = PIN_MAP_TYPE_MUX_GROUP; |
| 1306 | (*map)->data.mux.group = np->name; |
| 1307 | (*map)->data.mux.function = np->name; |
| 1308 | |
Tony Lindgren | 02e483f | 2013-10-02 21:39:39 -0700 | [diff] [blame] | 1309 | if (PCS_HAS_PINCONF) { |
Manjunathappa, Prakash | 4e7e801 | 2013-05-21 19:38:00 +0530 | [diff] [blame] | 1310 | dev_err(pcs->dev, "pinconf not supported\n"); |
| 1311 | goto free_pingroups; |
| 1312 | } |
| 1313 | |
| 1314 | *num_maps = 1; |
| 1315 | return 0; |
| 1316 | |
| 1317 | free_pingroups: |
| 1318 | pcs_free_pingroups(pcs); |
| 1319 | *num_maps = 1; |
| 1320 | free_function: |
| 1321 | pcs_remove_function(pcs, function); |
| 1322 | |
| 1323 | free_pins: |
| 1324 | devm_kfree(pcs->dev, pins); |
| 1325 | |
| 1326 | free_vals: |
| 1327 | devm_kfree(pcs->dev, vals); |
| 1328 | |
| 1329 | return res; |
| 1330 | } |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 1331 | /** |
| 1332 | * pcs_dt_node_to_map() - allocates and parses pinctrl maps |
| 1333 | * @pctldev: pinctrl instance |
| 1334 | * @np_config: device tree pinmux entry |
| 1335 | * @map: array of map entries |
| 1336 | * @num_maps: number of maps |
| 1337 | */ |
| 1338 | static int pcs_dt_node_to_map(struct pinctrl_dev *pctldev, |
| 1339 | struct device_node *np_config, |
| 1340 | struct pinctrl_map **map, unsigned *num_maps) |
| 1341 | { |
| 1342 | struct pcs_device *pcs; |
| 1343 | const char **pgnames; |
| 1344 | int ret; |
| 1345 | |
| 1346 | pcs = pinctrl_dev_get_drvdata(pctldev); |
| 1347 | |
Haojian Zhuang | 9dddb4d | 2013-02-17 19:42:55 +0800 | [diff] [blame] | 1348 | /* create 2 maps. One is for pinmux, and the other is for pinconf. */ |
| 1349 | *map = devm_kzalloc(pcs->dev, sizeof(**map) * 2, GFP_KERNEL); |
Sachin Kamat | 00e79d1 | 2012-11-20 16:34:39 +0530 | [diff] [blame] | 1350 | if (!*map) |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 1351 | return -ENOMEM; |
| 1352 | |
| 1353 | *num_maps = 0; |
| 1354 | |
| 1355 | pgnames = devm_kzalloc(pcs->dev, sizeof(*pgnames), GFP_KERNEL); |
| 1356 | if (!pgnames) { |
| 1357 | ret = -ENOMEM; |
| 1358 | goto free_map; |
| 1359 | } |
| 1360 | |
Manjunathappa, Prakash | 4e7e801 | 2013-05-21 19:38:00 +0530 | [diff] [blame] | 1361 | if (pcs->bits_per_mux) { |
| 1362 | ret = pcs_parse_bits_in_pinctrl_entry(pcs, np_config, map, |
| 1363 | num_maps, pgnames); |
| 1364 | if (ret < 0) { |
| 1365 | dev_err(pcs->dev, "no pins entries for %s\n", |
| 1366 | np_config->name); |
| 1367 | goto free_pgnames; |
| 1368 | } |
| 1369 | } else { |
| 1370 | ret = pcs_parse_one_pinctrl_entry(pcs, np_config, map, |
| 1371 | num_maps, pgnames); |
| 1372 | if (ret < 0) { |
| 1373 | dev_err(pcs->dev, "no pins entries for %s\n", |
| 1374 | np_config->name); |
| 1375 | goto free_pgnames; |
| 1376 | } |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 1377 | } |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 1378 | |
| 1379 | return 0; |
| 1380 | |
| 1381 | free_pgnames: |
| 1382 | devm_kfree(pcs->dev, pgnames); |
| 1383 | free_map: |
| 1384 | devm_kfree(pcs->dev, *map); |
| 1385 | |
| 1386 | return ret; |
| 1387 | } |
| 1388 | |
| 1389 | /** |
| 1390 | * pcs_free_funcs() - free memory used by functions |
| 1391 | * @pcs: pcs driver instance |
| 1392 | */ |
| 1393 | static void pcs_free_funcs(struct pcs_device *pcs) |
| 1394 | { |
| 1395 | struct list_head *pos, *tmp; |
| 1396 | int i; |
| 1397 | |
| 1398 | mutex_lock(&pcs->mutex); |
| 1399 | for (i = 0; i < pcs->nfuncs; i++) { |
| 1400 | struct pcs_function *func; |
| 1401 | |
| 1402 | func = radix_tree_lookup(&pcs->ftree, i); |
| 1403 | if (!func) |
| 1404 | continue; |
| 1405 | radix_tree_delete(&pcs->ftree, i); |
| 1406 | } |
| 1407 | list_for_each_safe(pos, tmp, &pcs->functions) { |
| 1408 | struct pcs_function *function; |
| 1409 | |
| 1410 | function = list_entry(pos, struct pcs_function, node); |
| 1411 | list_del(&function->node); |
| 1412 | } |
| 1413 | mutex_unlock(&pcs->mutex); |
| 1414 | } |
| 1415 | |
| 1416 | /** |
| 1417 | * pcs_free_pingroups() - free memory used by pingroups |
| 1418 | * @pcs: pcs driver instance |
| 1419 | */ |
| 1420 | static void pcs_free_pingroups(struct pcs_device *pcs) |
| 1421 | { |
| 1422 | struct list_head *pos, *tmp; |
| 1423 | int i; |
| 1424 | |
| 1425 | mutex_lock(&pcs->mutex); |
| 1426 | for (i = 0; i < pcs->ngroups; i++) { |
| 1427 | struct pcs_pingroup *pingroup; |
| 1428 | |
| 1429 | pingroup = radix_tree_lookup(&pcs->pgtree, i); |
| 1430 | if (!pingroup) |
| 1431 | continue; |
| 1432 | radix_tree_delete(&pcs->pgtree, i); |
| 1433 | } |
| 1434 | list_for_each_safe(pos, tmp, &pcs->pingroups) { |
| 1435 | struct pcs_pingroup *pingroup; |
| 1436 | |
| 1437 | pingroup = list_entry(pos, struct pcs_pingroup, node); |
| 1438 | list_del(&pingroup->node); |
| 1439 | } |
| 1440 | mutex_unlock(&pcs->mutex); |
| 1441 | } |
| 1442 | |
| 1443 | /** |
Tony Lindgren | 3e6cee1 | 2013-10-02 21:39:40 -0700 | [diff] [blame] | 1444 | * pcs_irq_free() - free interrupt |
| 1445 | * @pcs: pcs driver instance |
| 1446 | */ |
| 1447 | static void pcs_irq_free(struct pcs_device *pcs) |
| 1448 | { |
| 1449 | struct pcs_soc_data *pcs_soc = &pcs->socdata; |
| 1450 | |
| 1451 | if (pcs_soc->irq < 0) |
| 1452 | return; |
| 1453 | |
| 1454 | if (pcs->domain) |
| 1455 | irq_domain_remove(pcs->domain); |
| 1456 | |
| 1457 | if (PCS_QUIRK_HAS_SHARED_IRQ) |
| 1458 | free_irq(pcs_soc->irq, pcs_soc); |
| 1459 | else |
| 1460 | irq_set_chained_handler(pcs_soc->irq, NULL); |
| 1461 | } |
| 1462 | |
| 1463 | /** |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 1464 | * pcs_free_resources() - free memory used by this driver |
| 1465 | * @pcs: pcs driver instance |
| 1466 | */ |
| 1467 | static void pcs_free_resources(struct pcs_device *pcs) |
| 1468 | { |
Tony Lindgren | 3e6cee1 | 2013-10-02 21:39:40 -0700 | [diff] [blame] | 1469 | pcs_irq_free(pcs); |
Markus Elfring | f10a258 | 2015-11-05 17:10:22 +0100 | [diff] [blame] | 1470 | pinctrl_unregister(pcs->pctl); |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 1471 | pcs_free_funcs(pcs); |
| 1472 | pcs_free_pingroups(pcs); |
| 1473 | } |
| 1474 | |
| 1475 | #define PCS_GET_PROP_U32(name, reg, err) \ |
| 1476 | do { \ |
| 1477 | ret = of_property_read_u32(np, name, reg); \ |
| 1478 | if (ret) { \ |
| 1479 | dev_err(pcs->dev, err); \ |
| 1480 | return ret; \ |
| 1481 | } \ |
| 1482 | } while (0); |
| 1483 | |
Fabian Frederick | baa9946e | 2015-03-16 20:59:09 +0100 | [diff] [blame] | 1484 | static const struct of_device_id pcs_of_match[]; |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 1485 | |
Haojian Zhuang | a1a277e | 2013-02-17 19:42:52 +0800 | [diff] [blame] | 1486 | static int pcs_add_gpio_func(struct device_node *node, struct pcs_device *pcs) |
| 1487 | { |
| 1488 | const char *propname = "pinctrl-single,gpio-range"; |
| 1489 | const char *cellname = "#pinctrl-single,gpio-range-cells"; |
| 1490 | struct of_phandle_args gpiospec; |
| 1491 | struct pcs_gpiofunc_range *range; |
| 1492 | int ret, i; |
| 1493 | |
| 1494 | for (i = 0; ; i++) { |
| 1495 | ret = of_parse_phandle_with_args(node, propname, cellname, |
| 1496 | i, &gpiospec); |
| 1497 | /* Do not treat it as error. Only treat it as end condition. */ |
| 1498 | if (ret) { |
| 1499 | ret = 0; |
| 1500 | break; |
| 1501 | } |
| 1502 | range = devm_kzalloc(pcs->dev, sizeof(*range), GFP_KERNEL); |
| 1503 | if (!range) { |
| 1504 | ret = -ENOMEM; |
| 1505 | break; |
| 1506 | } |
| 1507 | range->offset = gpiospec.args[0]; |
| 1508 | range->npins = gpiospec.args[1]; |
| 1509 | range->gpiofunc = gpiospec.args[2]; |
| 1510 | mutex_lock(&pcs->mutex); |
| 1511 | list_add_tail(&range->node, &pcs->gpiofuncs); |
| 1512 | mutex_unlock(&pcs->mutex); |
| 1513 | } |
| 1514 | return ret; |
| 1515 | } |
Tony Lindgren | 3e6cee1 | 2013-10-02 21:39:40 -0700 | [diff] [blame] | 1516 | /** |
| 1517 | * @reg: virtual address of interrupt register |
| 1518 | * @hwirq: hardware irq number |
| 1519 | * @irq: virtual irq number |
| 1520 | * @node: list node |
| 1521 | */ |
| 1522 | struct pcs_interrupt { |
| 1523 | void __iomem *reg; |
| 1524 | irq_hw_number_t hwirq; |
| 1525 | unsigned int irq; |
| 1526 | struct list_head node; |
| 1527 | }; |
| 1528 | |
| 1529 | /** |
| 1530 | * pcs_irq_set() - enables or disables an interrupt |
| 1531 | * |
| 1532 | * Note that this currently assumes one interrupt per pinctrl |
| 1533 | * register that is typically used for wake-up events. |
| 1534 | */ |
| 1535 | static inline void pcs_irq_set(struct pcs_soc_data *pcs_soc, |
| 1536 | int irq, const bool enable) |
| 1537 | { |
| 1538 | struct pcs_device *pcs; |
| 1539 | struct list_head *pos; |
| 1540 | unsigned mask; |
| 1541 | |
| 1542 | pcs = container_of(pcs_soc, struct pcs_device, socdata); |
| 1543 | list_for_each(pos, &pcs->irqs) { |
| 1544 | struct pcs_interrupt *pcswi; |
| 1545 | unsigned soc_mask; |
| 1546 | |
| 1547 | pcswi = list_entry(pos, struct pcs_interrupt, node); |
| 1548 | if (irq != pcswi->irq) |
| 1549 | continue; |
| 1550 | |
| 1551 | soc_mask = pcs_soc->irq_enable_mask; |
| 1552 | raw_spin_lock(&pcs->lock); |
| 1553 | mask = pcs->read(pcswi->reg); |
| 1554 | if (enable) |
| 1555 | mask |= soc_mask; |
| 1556 | else |
| 1557 | mask &= ~soc_mask; |
| 1558 | pcs->write(mask, pcswi->reg); |
Tony Lindgren | 0ac3c0a4 | 2016-05-31 14:17:06 -0700 | [diff] [blame] | 1559 | |
| 1560 | /* flush posted write */ |
| 1561 | mask = pcs->read(pcswi->reg); |
Tony Lindgren | 3e6cee1 | 2013-10-02 21:39:40 -0700 | [diff] [blame] | 1562 | raw_spin_unlock(&pcs->lock); |
| 1563 | } |
Roger Quadros | c9b3a7d | 2013-10-11 19:13:16 +0300 | [diff] [blame] | 1564 | |
| 1565 | if (pcs_soc->rearm) |
| 1566 | pcs_soc->rearm(); |
Tony Lindgren | 3e6cee1 | 2013-10-02 21:39:40 -0700 | [diff] [blame] | 1567 | } |
| 1568 | |
| 1569 | /** |
| 1570 | * pcs_irq_mask() - mask pinctrl interrupt |
| 1571 | * @d: interrupt data |
| 1572 | */ |
| 1573 | static void pcs_irq_mask(struct irq_data *d) |
| 1574 | { |
| 1575 | struct pcs_soc_data *pcs_soc = irq_data_get_irq_chip_data(d); |
| 1576 | |
| 1577 | pcs_irq_set(pcs_soc, d->irq, false); |
| 1578 | } |
| 1579 | |
| 1580 | /** |
| 1581 | * pcs_irq_unmask() - unmask pinctrl interrupt |
| 1582 | * @d: interrupt data |
| 1583 | */ |
| 1584 | static void pcs_irq_unmask(struct irq_data *d) |
| 1585 | { |
| 1586 | struct pcs_soc_data *pcs_soc = irq_data_get_irq_chip_data(d); |
| 1587 | |
| 1588 | pcs_irq_set(pcs_soc, d->irq, true); |
| 1589 | } |
| 1590 | |
| 1591 | /** |
| 1592 | * pcs_irq_set_wake() - toggle the suspend and resume wake up |
| 1593 | * @d: interrupt data |
| 1594 | * @state: wake-up state |
| 1595 | * |
| 1596 | * Note that this should be called only for suspend and resume. |
| 1597 | * For runtime PM, the wake-up events should be enabled by default. |
| 1598 | */ |
| 1599 | static int pcs_irq_set_wake(struct irq_data *d, unsigned int state) |
| 1600 | { |
| 1601 | if (state) |
| 1602 | pcs_irq_unmask(d); |
| 1603 | else |
| 1604 | pcs_irq_mask(d); |
| 1605 | |
| 1606 | return 0; |
| 1607 | } |
| 1608 | |
| 1609 | /** |
| 1610 | * pcs_irq_handle() - common interrupt handler |
| 1611 | * @pcs_irq: interrupt data |
| 1612 | * |
| 1613 | * Note that this currently assumes we have one interrupt bit per |
| 1614 | * mux register. This interrupt is typically used for wake-up events. |
| 1615 | * For more complex interrupts different handlers can be specified. |
| 1616 | */ |
| 1617 | static int pcs_irq_handle(struct pcs_soc_data *pcs_soc) |
| 1618 | { |
| 1619 | struct pcs_device *pcs; |
| 1620 | struct list_head *pos; |
| 1621 | int count = 0; |
| 1622 | |
| 1623 | pcs = container_of(pcs_soc, struct pcs_device, socdata); |
| 1624 | list_for_each(pos, &pcs->irqs) { |
| 1625 | struct pcs_interrupt *pcswi; |
| 1626 | unsigned mask; |
| 1627 | |
| 1628 | pcswi = list_entry(pos, struct pcs_interrupt, node); |
| 1629 | raw_spin_lock(&pcs->lock); |
| 1630 | mask = pcs->read(pcswi->reg); |
| 1631 | raw_spin_unlock(&pcs->lock); |
| 1632 | if (mask & pcs_soc->irq_status_mask) { |
| 1633 | generic_handle_irq(irq_find_mapping(pcs->domain, |
| 1634 | pcswi->hwirq)); |
| 1635 | count++; |
| 1636 | } |
| 1637 | } |
| 1638 | |
| 1639 | return count; |
| 1640 | } |
| 1641 | |
| 1642 | /** |
| 1643 | * pcs_irq_handler() - handler for the shared interrupt case |
| 1644 | * @irq: interrupt |
| 1645 | * @d: data |
| 1646 | * |
| 1647 | * Use this for cases where multiple instances of |
| 1648 | * pinctrl-single share a single interrupt like on omaps. |
| 1649 | */ |
| 1650 | static irqreturn_t pcs_irq_handler(int irq, void *d) |
| 1651 | { |
| 1652 | struct pcs_soc_data *pcs_soc = d; |
| 1653 | |
| 1654 | return pcs_irq_handle(pcs_soc) ? IRQ_HANDLED : IRQ_NONE; |
| 1655 | } |
| 1656 | |
| 1657 | /** |
| 1658 | * pcs_irq_handle() - handler for the dedicated chained interrupt case |
| 1659 | * @irq: interrupt |
| 1660 | * @desc: interrupt descriptor |
| 1661 | * |
| 1662 | * Use this if you have a separate interrupt for each |
| 1663 | * pinctrl-single instance. |
| 1664 | */ |
Thomas Gleixner | bd0b9ac | 2015-09-14 10:42:37 +0200 | [diff] [blame] | 1665 | static void pcs_irq_chain_handler(struct irq_desc *desc) |
Tony Lindgren | 3e6cee1 | 2013-10-02 21:39:40 -0700 | [diff] [blame] | 1666 | { |
| 1667 | struct pcs_soc_data *pcs_soc = irq_desc_get_handler_data(desc); |
| 1668 | struct irq_chip *chip; |
Tony Lindgren | 3e6cee1 | 2013-10-02 21:39:40 -0700 | [diff] [blame] | 1669 | |
Jiang Liu | 5663bb2 | 2015-06-04 12:13:16 +0800 | [diff] [blame] | 1670 | chip = irq_desc_get_chip(desc); |
Tony Lindgren | 3e6cee1 | 2013-10-02 21:39:40 -0700 | [diff] [blame] | 1671 | chained_irq_enter(chip, desc); |
Rickard Strandqvist | 849bfe0 | 2014-06-26 15:43:01 +0200 | [diff] [blame] | 1672 | pcs_irq_handle(pcs_soc); |
Tony Lindgren | 3e6cee1 | 2013-10-02 21:39:40 -0700 | [diff] [blame] | 1673 | /* REVISIT: export and add handle_bad_irq(irq, desc)? */ |
| 1674 | chained_irq_exit(chip, desc); |
| 1675 | |
| 1676 | return; |
| 1677 | } |
| 1678 | |
| 1679 | static int pcs_irqdomain_map(struct irq_domain *d, unsigned int irq, |
| 1680 | irq_hw_number_t hwirq) |
| 1681 | { |
| 1682 | struct pcs_soc_data *pcs_soc = d->host_data; |
| 1683 | struct pcs_device *pcs; |
| 1684 | struct pcs_interrupt *pcswi; |
| 1685 | |
| 1686 | pcs = container_of(pcs_soc, struct pcs_device, socdata); |
| 1687 | pcswi = devm_kzalloc(pcs->dev, sizeof(*pcswi), GFP_KERNEL); |
| 1688 | if (!pcswi) |
| 1689 | return -ENOMEM; |
| 1690 | |
| 1691 | pcswi->reg = pcs->base + hwirq; |
| 1692 | pcswi->hwirq = hwirq; |
| 1693 | pcswi->irq = irq; |
| 1694 | |
| 1695 | mutex_lock(&pcs->mutex); |
| 1696 | list_add_tail(&pcswi->node, &pcs->irqs); |
| 1697 | mutex_unlock(&pcs->mutex); |
| 1698 | |
| 1699 | irq_set_chip_data(irq, pcs_soc); |
| 1700 | irq_set_chip_and_handler(irq, &pcs->chip, |
| 1701 | handle_level_irq); |
Sudeep Holla | 3c177a1 | 2016-02-01 18:28:17 +0000 | [diff] [blame] | 1702 | irq_set_lockdep_class(irq, &pcs_lock_class); |
Tony Lindgren | 1b9c0fb | 2013-10-18 16:20:05 -0700 | [diff] [blame] | 1703 | irq_set_noprobe(irq); |
Tony Lindgren | 3e6cee1 | 2013-10-02 21:39:40 -0700 | [diff] [blame] | 1704 | |
| 1705 | return 0; |
| 1706 | } |
| 1707 | |
Krzysztof Kozlowski | e5b6095 | 2015-04-27 21:54:06 +0900 | [diff] [blame] | 1708 | static const struct irq_domain_ops pcs_irqdomain_ops = { |
Tony Lindgren | 3e6cee1 | 2013-10-02 21:39:40 -0700 | [diff] [blame] | 1709 | .map = pcs_irqdomain_map, |
| 1710 | .xlate = irq_domain_xlate_onecell, |
| 1711 | }; |
| 1712 | |
| 1713 | /** |
| 1714 | * pcs_irq_init_chained_handler() - set up a chained interrupt handler |
| 1715 | * @pcs: pcs driver instance |
| 1716 | * @np: device node pointer |
| 1717 | */ |
| 1718 | static int pcs_irq_init_chained_handler(struct pcs_device *pcs, |
| 1719 | struct device_node *np) |
| 1720 | { |
| 1721 | struct pcs_soc_data *pcs_soc = &pcs->socdata; |
| 1722 | const char *name = "pinctrl"; |
| 1723 | int num_irqs; |
| 1724 | |
| 1725 | if (!pcs_soc->irq_enable_mask || |
| 1726 | !pcs_soc->irq_status_mask) { |
| 1727 | pcs_soc->irq = -1; |
| 1728 | return -EINVAL; |
| 1729 | } |
| 1730 | |
| 1731 | INIT_LIST_HEAD(&pcs->irqs); |
| 1732 | pcs->chip.name = name; |
| 1733 | pcs->chip.irq_ack = pcs_irq_mask; |
| 1734 | pcs->chip.irq_mask = pcs_irq_mask; |
| 1735 | pcs->chip.irq_unmask = pcs_irq_unmask; |
| 1736 | pcs->chip.irq_set_wake = pcs_irq_set_wake; |
| 1737 | |
| 1738 | if (PCS_QUIRK_HAS_SHARED_IRQ) { |
| 1739 | int res; |
| 1740 | |
| 1741 | res = request_irq(pcs_soc->irq, pcs_irq_handler, |
Grygorii Strashko | c10372e | 2015-07-06 18:13:37 +0300 | [diff] [blame] | 1742 | IRQF_SHARED | IRQF_NO_SUSPEND | |
| 1743 | IRQF_NO_THREAD, |
Tony Lindgren | 3e6cee1 | 2013-10-02 21:39:40 -0700 | [diff] [blame] | 1744 | name, pcs_soc); |
| 1745 | if (res) { |
| 1746 | pcs_soc->irq = -1; |
| 1747 | return res; |
| 1748 | } |
| 1749 | } else { |
Thomas Gleixner | 20d5d14 | 2015-06-21 21:11:06 +0200 | [diff] [blame] | 1750 | irq_set_chained_handler_and_data(pcs_soc->irq, |
| 1751 | pcs_irq_chain_handler, |
| 1752 | pcs_soc); |
Tony Lindgren | 3e6cee1 | 2013-10-02 21:39:40 -0700 | [diff] [blame] | 1753 | } |
| 1754 | |
| 1755 | /* |
| 1756 | * We can use the register offset as the hardirq |
| 1757 | * number as irq_domain_add_simple maps them lazily. |
| 1758 | * This way we can easily support more than one |
| 1759 | * interrupt per function if needed. |
| 1760 | */ |
| 1761 | num_irqs = pcs->size; |
| 1762 | |
| 1763 | pcs->domain = irq_domain_add_simple(np, num_irqs, 0, |
| 1764 | &pcs_irqdomain_ops, |
| 1765 | pcs_soc); |
| 1766 | if (!pcs->domain) { |
| 1767 | irq_set_chained_handler(pcs_soc->irq, NULL); |
| 1768 | return -EINVAL; |
| 1769 | } |
| 1770 | |
| 1771 | return 0; |
| 1772 | } |
Haojian Zhuang | a1a277e | 2013-02-17 19:42:52 +0800 | [diff] [blame] | 1773 | |
Jean-Francois Moine | 8cb440a | 2013-07-15 10:14:26 +0200 | [diff] [blame] | 1774 | #ifdef CONFIG_PM |
Hebbar Gururaja | 0f9bc4b | 2013-05-31 15:43:01 +0530 | [diff] [blame] | 1775 | static int pinctrl_single_suspend(struct platform_device *pdev, |
| 1776 | pm_message_t state) |
| 1777 | { |
| 1778 | struct pcs_device *pcs; |
| 1779 | |
| 1780 | pcs = platform_get_drvdata(pdev); |
| 1781 | if (!pcs) |
| 1782 | return -EINVAL; |
| 1783 | |
| 1784 | return pinctrl_force_sleep(pcs->pctl); |
| 1785 | } |
| 1786 | |
| 1787 | static int pinctrl_single_resume(struct platform_device *pdev) |
| 1788 | { |
| 1789 | struct pcs_device *pcs; |
| 1790 | |
| 1791 | pcs = platform_get_drvdata(pdev); |
| 1792 | if (!pcs) |
| 1793 | return -EINVAL; |
| 1794 | |
| 1795 | return pinctrl_force_default(pcs->pctl); |
| 1796 | } |
Jean-Francois Moine | 8cb440a | 2013-07-15 10:14:26 +0200 | [diff] [blame] | 1797 | #endif |
Hebbar Gururaja | 0f9bc4b | 2013-05-31 15:43:01 +0530 | [diff] [blame] | 1798 | |
Greg Kroah-Hartman | 150632b | 2012-12-21 13:10:23 -0800 | [diff] [blame] | 1799 | static int pcs_probe(struct platform_device *pdev) |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 1800 | { |
| 1801 | struct device_node *np = pdev->dev.of_node; |
| 1802 | const struct of_device_id *match; |
Tony Lindgren | dc7743a | 2013-10-02 21:39:40 -0700 | [diff] [blame] | 1803 | struct pcs_pdata *pdata; |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 1804 | struct resource *res; |
| 1805 | struct pcs_device *pcs; |
Tony Lindgren | 02e483f | 2013-10-02 21:39:39 -0700 | [diff] [blame] | 1806 | const struct pcs_soc_data *soc; |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 1807 | int ret; |
| 1808 | |
| 1809 | match = of_match_device(pcs_of_match, &pdev->dev); |
| 1810 | if (!match) |
| 1811 | return -EINVAL; |
| 1812 | |
| 1813 | pcs = devm_kzalloc(&pdev->dev, sizeof(*pcs), GFP_KERNEL); |
| 1814 | if (!pcs) { |
| 1815 | dev_err(&pdev->dev, "could not allocate\n"); |
| 1816 | return -ENOMEM; |
| 1817 | } |
| 1818 | pcs->dev = &pdev->dev; |
Tony Lindgren | 3e6cee1 | 2013-10-02 21:39:40 -0700 | [diff] [blame] | 1819 | raw_spin_lock_init(&pcs->lock); |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 1820 | mutex_init(&pcs->mutex); |
| 1821 | INIT_LIST_HEAD(&pcs->pingroups); |
| 1822 | INIT_LIST_HEAD(&pcs->functions); |
Haojian Zhuang | a1a277e | 2013-02-17 19:42:52 +0800 | [diff] [blame] | 1823 | INIT_LIST_HEAD(&pcs->gpiofuncs); |
Tony Lindgren | 02e483f | 2013-10-02 21:39:39 -0700 | [diff] [blame] | 1824 | soc = match->data; |
| 1825 | pcs->flags = soc->flags; |
Tony Lindgren | 3e6cee1 | 2013-10-02 21:39:40 -0700 | [diff] [blame] | 1826 | memcpy(&pcs->socdata, soc, sizeof(*soc)); |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 1827 | |
| 1828 | PCS_GET_PROP_U32("pinctrl-single,register-width", &pcs->width, |
| 1829 | "register width not specified\n"); |
| 1830 | |
Haojian Zhuang | 477ac77 | 2013-02-17 19:42:54 +0800 | [diff] [blame] | 1831 | ret = of_property_read_u32(np, "pinctrl-single,function-mask", |
| 1832 | &pcs->fmask); |
| 1833 | if (!ret) { |
Keerthy | 56b367c | 2016-04-14 10:29:16 +0530 | [diff] [blame] | 1834 | pcs->fshift = __ffs(pcs->fmask); |
Haojian Zhuang | 477ac77 | 2013-02-17 19:42:54 +0800 | [diff] [blame] | 1835 | pcs->fmax = pcs->fmask >> pcs->fshift; |
| 1836 | } else { |
| 1837 | /* If mask property doesn't exist, function mux is invalid. */ |
| 1838 | pcs->fmask = 0; |
| 1839 | pcs->fshift = 0; |
| 1840 | pcs->fmax = 0; |
| 1841 | } |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 1842 | |
| 1843 | ret = of_property_read_u32(np, "pinctrl-single,function-off", |
| 1844 | &pcs->foff); |
| 1845 | if (ret) |
| 1846 | pcs->foff = PCS_OFF_DISABLED; |
| 1847 | |
Peter Ujfalusi | 9e605cb | 2012-09-11 11:54:24 +0300 | [diff] [blame] | 1848 | pcs->bits_per_mux = of_property_read_bool(np, |
| 1849 | "pinctrl-single,bit-per-mux"); |
| 1850 | |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 1851 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1852 | if (!res) { |
| 1853 | dev_err(pcs->dev, "could not get resource\n"); |
| 1854 | return -ENODEV; |
| 1855 | } |
| 1856 | |
| 1857 | pcs->res = devm_request_mem_region(pcs->dev, res->start, |
| 1858 | resource_size(res), DRIVER_NAME); |
| 1859 | if (!pcs->res) { |
| 1860 | dev_err(pcs->dev, "could not get mem_region\n"); |
| 1861 | return -EBUSY; |
| 1862 | } |
| 1863 | |
| 1864 | pcs->size = resource_size(pcs->res); |
| 1865 | pcs->base = devm_ioremap(pcs->dev, pcs->res->start, pcs->size); |
| 1866 | if (!pcs->base) { |
| 1867 | dev_err(pcs->dev, "could not ioremap\n"); |
| 1868 | return -ENODEV; |
| 1869 | } |
| 1870 | |
| 1871 | INIT_RADIX_TREE(&pcs->pgtree, GFP_KERNEL); |
| 1872 | INIT_RADIX_TREE(&pcs->ftree, GFP_KERNEL); |
| 1873 | platform_set_drvdata(pdev, pcs); |
| 1874 | |
| 1875 | switch (pcs->width) { |
| 1876 | case 8: |
| 1877 | pcs->read = pcs_readb; |
| 1878 | pcs->write = pcs_writeb; |
| 1879 | break; |
| 1880 | case 16: |
| 1881 | pcs->read = pcs_readw; |
| 1882 | pcs->write = pcs_writew; |
| 1883 | break; |
| 1884 | case 32: |
| 1885 | pcs->read = pcs_readl; |
| 1886 | pcs->write = pcs_writel; |
| 1887 | break; |
| 1888 | default: |
| 1889 | break; |
| 1890 | } |
| 1891 | |
| 1892 | pcs->desc.name = DRIVER_NAME; |
| 1893 | pcs->desc.pctlops = &pcs_pinctrl_ops; |
| 1894 | pcs->desc.pmxops = &pcs_pinmux_ops; |
Tony Lindgren | 02e483f | 2013-10-02 21:39:39 -0700 | [diff] [blame] | 1895 | if (PCS_HAS_PINCONF) |
Axel Lin | a7bbdd7 | 2013-03-04 13:47:39 +0800 | [diff] [blame] | 1896 | pcs->desc.confops = &pcs_pinconf_ops; |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 1897 | pcs->desc.owner = THIS_MODULE; |
| 1898 | |
| 1899 | ret = pcs_allocate_pin_table(pcs); |
| 1900 | if (ret < 0) |
| 1901 | goto free; |
| 1902 | |
| 1903 | pcs->pctl = pinctrl_register(&pcs->desc, pcs->dev, pcs); |
Masahiro Yamada | 323de9e | 2015-06-09 13:01:16 +0900 | [diff] [blame] | 1904 | if (IS_ERR(pcs->pctl)) { |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 1905 | dev_err(pcs->dev, "could not register single pinctrl driver\n"); |
Masahiro Yamada | 323de9e | 2015-06-09 13:01:16 +0900 | [diff] [blame] | 1906 | ret = PTR_ERR(pcs->pctl); |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 1907 | goto free; |
| 1908 | } |
| 1909 | |
Haojian Zhuang | a1a277e | 2013-02-17 19:42:52 +0800 | [diff] [blame] | 1910 | ret = pcs_add_gpio_func(np, pcs); |
| 1911 | if (ret < 0) |
| 1912 | goto free; |
| 1913 | |
Tony Lindgren | 3e6cee1 | 2013-10-02 21:39:40 -0700 | [diff] [blame] | 1914 | pcs->socdata.irq = irq_of_parse_and_map(np, 0); |
| 1915 | if (pcs->socdata.irq) |
| 1916 | pcs->flags |= PCS_FEAT_IRQ; |
| 1917 | |
Tony Lindgren | dc7743a | 2013-10-02 21:39:40 -0700 | [diff] [blame] | 1918 | /* We still need auxdata for some omaps for PRM interrupts */ |
| 1919 | pdata = dev_get_platdata(&pdev->dev); |
| 1920 | if (pdata) { |
| 1921 | if (pdata->rearm) |
| 1922 | pcs->socdata.rearm = pdata->rearm; |
| 1923 | if (pdata->irq) { |
| 1924 | pcs->socdata.irq = pdata->irq; |
| 1925 | pcs->flags |= PCS_FEAT_IRQ; |
| 1926 | } |
| 1927 | } |
| 1928 | |
Tony Lindgren | 3e6cee1 | 2013-10-02 21:39:40 -0700 | [diff] [blame] | 1929 | if (PCS_HAS_IRQ) { |
| 1930 | ret = pcs_irq_init_chained_handler(pcs, np); |
| 1931 | if (ret < 0) |
| 1932 | dev_warn(pcs->dev, "initialized with no interrupts\n"); |
| 1933 | } |
| 1934 | |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 1935 | dev_info(pcs->dev, "%i pins at pa %p size %u\n", |
| 1936 | pcs->desc.npins, pcs->base, pcs->size); |
| 1937 | |
| 1938 | return 0; |
| 1939 | |
| 1940 | free: |
| 1941 | pcs_free_resources(pcs); |
| 1942 | |
| 1943 | return ret; |
| 1944 | } |
| 1945 | |
Bill Pemberton | f90f54b | 2012-11-19 13:26:06 -0500 | [diff] [blame] | 1946 | static int pcs_remove(struct platform_device *pdev) |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 1947 | { |
| 1948 | struct pcs_device *pcs = platform_get_drvdata(pdev); |
| 1949 | |
| 1950 | if (!pcs) |
| 1951 | return 0; |
| 1952 | |
| 1953 | pcs_free_resources(pcs); |
| 1954 | |
| 1955 | return 0; |
| 1956 | } |
| 1957 | |
Tony Lindgren | 3e6cee1 | 2013-10-02 21:39:40 -0700 | [diff] [blame] | 1958 | static const struct pcs_soc_data pinctrl_single_omap_wkup = { |
| 1959 | .flags = PCS_QUIRK_SHARED_IRQ, |
| 1960 | .irq_enable_mask = (1 << 14), /* OMAP_WAKEUP_EN */ |
| 1961 | .irq_status_mask = (1 << 15), /* OMAP_WAKEUP_EVENT */ |
| 1962 | }; |
| 1963 | |
Nishanth Menon | 31320be | 2014-08-22 09:01:01 -0500 | [diff] [blame] | 1964 | static const struct pcs_soc_data pinctrl_single_dra7 = { |
Nishanth Menon | 31320be | 2014-08-22 09:01:01 -0500 | [diff] [blame] | 1965 | .irq_enable_mask = (1 << 24), /* WAKEUPENABLE */ |
| 1966 | .irq_status_mask = (1 << 25), /* WAKEUPEVENT */ |
| 1967 | }; |
| 1968 | |
Keerthy | aa2293d | 2014-08-22 09:01:02 -0500 | [diff] [blame] | 1969 | static const struct pcs_soc_data pinctrl_single_am437x = { |
| 1970 | .flags = PCS_QUIRK_SHARED_IRQ, |
| 1971 | .irq_enable_mask = (1 << 29), /* OMAP_WAKEUP_EN */ |
| 1972 | .irq_status_mask = (1 << 30), /* OMAP_WAKEUP_EVENT */ |
| 1973 | }; |
| 1974 | |
Tony Lindgren | 02e483f | 2013-10-02 21:39:39 -0700 | [diff] [blame] | 1975 | static const struct pcs_soc_data pinctrl_single = { |
| 1976 | }; |
| 1977 | |
| 1978 | static const struct pcs_soc_data pinconf_single = { |
| 1979 | .flags = PCS_FEAT_PINCONF, |
| 1980 | }; |
| 1981 | |
Fabian Frederick | baa9946e | 2015-03-16 20:59:09 +0100 | [diff] [blame] | 1982 | static const struct of_device_id pcs_of_match[] = { |
Tony Lindgren | 3e6cee1 | 2013-10-02 21:39:40 -0700 | [diff] [blame] | 1983 | { .compatible = "ti,omap3-padconf", .data = &pinctrl_single_omap_wkup }, |
| 1984 | { .compatible = "ti,omap4-padconf", .data = &pinctrl_single_omap_wkup }, |
| 1985 | { .compatible = "ti,omap5-padconf", .data = &pinctrl_single_omap_wkup }, |
Nishanth Menon | 31320be | 2014-08-22 09:01:01 -0500 | [diff] [blame] | 1986 | { .compatible = "ti,dra7-padconf", .data = &pinctrl_single_dra7 }, |
Keerthy | aa2293d | 2014-08-22 09:01:02 -0500 | [diff] [blame] | 1987 | { .compatible = "ti,am437-padconf", .data = &pinctrl_single_am437x }, |
Tony Lindgren | 02e483f | 2013-10-02 21:39:39 -0700 | [diff] [blame] | 1988 | { .compatible = "pinctrl-single", .data = &pinctrl_single }, |
| 1989 | { .compatible = "pinconf-single", .data = &pinconf_single }, |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 1990 | { }, |
| 1991 | }; |
| 1992 | MODULE_DEVICE_TABLE(of, pcs_of_match); |
| 1993 | |
| 1994 | static struct platform_driver pcs_driver = { |
| 1995 | .probe = pcs_probe, |
Bill Pemberton | 2a36f08 | 2012-11-19 13:21:27 -0500 | [diff] [blame] | 1996 | .remove = pcs_remove, |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 1997 | .driver = { |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 1998 | .name = DRIVER_NAME, |
| 1999 | .of_match_table = pcs_of_match, |
| 2000 | }, |
Hebbar Gururaja | 0f9bc4b | 2013-05-31 15:43:01 +0530 | [diff] [blame] | 2001 | #ifdef CONFIG_PM |
| 2002 | .suspend = pinctrl_single_suspend, |
| 2003 | .resume = pinctrl_single_resume, |
| 2004 | #endif |
Tony Lindgren | 8b8b091 | 2012-07-10 02:05:46 -0700 | [diff] [blame] | 2005 | }; |
| 2006 | |
| 2007 | module_platform_driver(pcs_driver); |
| 2008 | |
| 2009 | MODULE_AUTHOR("Tony Lindgren <tony@atomide.com>"); |
| 2010 | MODULE_DESCRIPTION("One-register-per-pin type device tree based pinctrl driver"); |
| 2011 | MODULE_LICENSE("GPL v2"); |