blob: 6e14811d1316c0a55d3fc183bbe6de43399ee840 [file] [log] [blame]
Thomas Abraham30574f02012-09-07 06:07:19 +09001/*
2 * pin-controller/pin-mux/pin-config/gpio-driver for Samsung's SoC's.
3 *
4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com
6 * Copyright (c) 2012 Linaro Ltd
7 * http://www.linaro.org
8 *
9 * Author: Thomas Abraham <thomas.ab@samsung.com>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This driver implements the Samsung pinctrl driver. It supports setting up of
17 * pinmux and pinconf configurations. The gpiolib interface is also included.
18 * External interrupt (gpio and wakeup) support are not included in this driver
19 * but provides extensions to which platform specific implementation of the gpio
20 * and wakeup interrupts can be hooked to.
21 */
22
23#include <linux/module.h>
24#include <linux/platform_device.h>
25#include <linux/io.h>
26#include <linux/slab.h>
27#include <linux/err.h>
28#include <linux/gpio.h>
Tomasz Figaa19fe2d2012-10-11 10:11:20 +020029#include <linux/irqdomain.h>
Tomasz Figa19846952013-03-18 22:31:50 +010030#include <linux/spinlock.h>
Doug Andersond9f99862013-05-16 21:33:18 -070031#include <linux/syscore_ops.h>
Thomas Abraham30574f02012-09-07 06:07:19 +090032
Sachin Kamatebe629a2014-07-10 17:33:27 +053033#include "../core.h"
Thomas Abraham30574f02012-09-07 06:07:19 +090034#include "pinctrl-samsung.h"
35
36#define GROUP_SUFFIX "-grp"
37#define GSUFFIX_LEN sizeof(GROUP_SUFFIX)
38#define FUNCTION_SUFFIX "-mux"
39#define FSUFFIX_LEN sizeof(FUNCTION_SUFFIX)
40
41/* list of all possible config options supported */
Axel Lind5fd5da2012-12-10 09:54:03 +090042static struct pin_config {
Tomasz Figa9a2c1c32014-07-02 17:41:03 +020043 const char *property;
44 enum pincfg_type param;
45} cfg_params[] = {
Thomas Abraham30574f02012-09-07 06:07:19 +090046 { "samsung,pin-pud", PINCFG_TYPE_PUD },
47 { "samsung,pin-drv", PINCFG_TYPE_DRV },
48 { "samsung,pin-con-pdn", PINCFG_TYPE_CON_PDN },
49 { "samsung,pin-pud-pdn", PINCFG_TYPE_PUD_PDN },
Tomasz Figa2700bc02014-07-02 17:41:04 +020050 { "samsung,pin-val", PINCFG_TYPE_DAT },
Thomas Abraham30574f02012-09-07 06:07:19 +090051};
52
Doug Andersond9f99862013-05-16 21:33:18 -070053/* Global list of devices (struct samsung_pinctrl_drv_data) */
Sachin Kamatb9408972013-06-24 16:50:21 +053054static LIST_HEAD(drvdata_list);
Doug Andersond9f99862013-05-16 21:33:18 -070055
Sachin Kamat6fb6f1ba2012-11-24 11:20:51 +090056static unsigned int pin_base;
Tomasz Figa40ba6222012-10-11 10:11:09 +020057
Tomasz Figad3a7b9e2012-10-11 10:11:17 +020058static inline struct samsung_pin_bank *gc_to_pin_bank(struct gpio_chip *gc)
59{
60 return container_of(gc, struct samsung_pin_bank, gpio_chip);
61}
62
Thomas Abraham30574f02012-09-07 06:07:19 +090063static int samsung_get_group_count(struct pinctrl_dev *pctldev)
64{
Tomasz Figa9a2c1c32014-07-02 17:41:03 +020065 struct samsung_pinctrl_drv_data *pmx = pinctrl_dev_get_drvdata(pctldev);
Thomas Abraham30574f02012-09-07 06:07:19 +090066
Tomasz Figa9a2c1c32014-07-02 17:41:03 +020067 return pmx->nr_groups;
Thomas Abraham30574f02012-09-07 06:07:19 +090068}
69
Thomas Abraham30574f02012-09-07 06:07:19 +090070static const char *samsung_get_group_name(struct pinctrl_dev *pctldev,
Tomasz Figa9a2c1c32014-07-02 17:41:03 +020071 unsigned group)
Thomas Abraham30574f02012-09-07 06:07:19 +090072{
Tomasz Figa9a2c1c32014-07-02 17:41:03 +020073 struct samsung_pinctrl_drv_data *pmx = pinctrl_dev_get_drvdata(pctldev);
Thomas Abraham30574f02012-09-07 06:07:19 +090074
Tomasz Figa9a2c1c32014-07-02 17:41:03 +020075 return pmx->pin_groups[group].name;
Thomas Abraham30574f02012-09-07 06:07:19 +090076}
77
Thomas Abraham30574f02012-09-07 06:07:19 +090078static int samsung_get_group_pins(struct pinctrl_dev *pctldev,
Tomasz Figa9a2c1c32014-07-02 17:41:03 +020079 unsigned group,
80 const unsigned **pins,
81 unsigned *num_pins)
Thomas Abraham30574f02012-09-07 06:07:19 +090082{
Tomasz Figa9a2c1c32014-07-02 17:41:03 +020083 struct samsung_pinctrl_drv_data *pmx = pinctrl_dev_get_drvdata(pctldev);
Thomas Abraham30574f02012-09-07 06:07:19 +090084
Tomasz Figa9a2c1c32014-07-02 17:41:03 +020085 *pins = pmx->pin_groups[group].pins;
86 *num_pins = pmx->pin_groups[group].num_pins;
87
Thomas Abraham30574f02012-09-07 06:07:19 +090088 return 0;
89}
90
Tomasz Figa9a2c1c32014-07-02 17:41:03 +020091static int reserve_map(struct device *dev, struct pinctrl_map **map,
92 unsigned *reserved_maps, unsigned *num_maps,
93 unsigned reserve)
Thomas Abraham30574f02012-09-07 06:07:19 +090094{
Tomasz Figa9a2c1c32014-07-02 17:41:03 +020095 unsigned old_num = *reserved_maps;
96 unsigned new_num = *num_maps + reserve;
97 struct pinctrl_map *new_map;
Thomas Abraham30574f02012-09-07 06:07:19 +090098
Tomasz Figa9a2c1c32014-07-02 17:41:03 +020099 if (old_num >= new_num)
100 return 0;
Thomas Abraham30574f02012-09-07 06:07:19 +0900101
Tomasz Figa9a2c1c32014-07-02 17:41:03 +0200102 new_map = krealloc(*map, sizeof(*new_map) * new_num, GFP_KERNEL);
103 if (!new_map) {
104 dev_err(dev, "krealloc(map) failed\n");
Thomas Abraham30574f02012-09-07 06:07:19 +0900105 return -ENOMEM;
106 }
Thomas Abraham30574f02012-09-07 06:07:19 +0900107
Tomasz Figa9a2c1c32014-07-02 17:41:03 +0200108 memset(new_map + old_num, 0, (new_num - old_num) * sizeof(*new_map));
Thomas Abraham30574f02012-09-07 06:07:19 +0900109
Tomasz Figa9a2c1c32014-07-02 17:41:03 +0200110 *map = new_map;
111 *reserved_maps = new_num;
Thomas Abraham30574f02012-09-07 06:07:19 +0900112
Thomas Abraham30574f02012-09-07 06:07:19 +0900113 return 0;
Thomas Abraham30574f02012-09-07 06:07:19 +0900114}
115
Tomasz Figa9a2c1c32014-07-02 17:41:03 +0200116static int add_map_mux(struct pinctrl_map **map, unsigned *reserved_maps,
117 unsigned *num_maps, const char *group,
118 const char *function)
Thomas Abraham30574f02012-09-07 06:07:19 +0900119{
Tomasz Figa9a2c1c32014-07-02 17:41:03 +0200120 if (WARN_ON(*num_maps == *reserved_maps))
121 return -ENOSPC;
Thomas Abraham30574f02012-09-07 06:07:19 +0900122
Tomasz Figa9a2c1c32014-07-02 17:41:03 +0200123 (*map)[*num_maps].type = PIN_MAP_TYPE_MUX_GROUP;
124 (*map)[*num_maps].data.mux.group = group;
125 (*map)[*num_maps].data.mux.function = function;
126 (*num_maps)++;
127
128 return 0;
129}
130
131static int add_map_configs(struct device *dev, struct pinctrl_map **map,
132 unsigned *reserved_maps, unsigned *num_maps,
133 const char *group, unsigned long *configs,
134 unsigned num_configs)
135{
136 unsigned long *dup_configs;
137
138 if (WARN_ON(*num_maps == *reserved_maps))
139 return -ENOSPC;
140
141 dup_configs = kmemdup(configs, num_configs * sizeof(*dup_configs),
142 GFP_KERNEL);
143 if (!dup_configs) {
144 dev_err(dev, "kmemdup(configs) failed\n");
145 return -ENOMEM;
146 }
147
148 (*map)[*num_maps].type = PIN_MAP_TYPE_CONFIGS_GROUP;
149 (*map)[*num_maps].data.configs.group_or_pin = group;
150 (*map)[*num_maps].data.configs.configs = dup_configs;
151 (*map)[*num_maps].data.configs.num_configs = num_configs;
152 (*num_maps)++;
153
154 return 0;
155}
156
157static int add_config(struct device *dev, unsigned long **configs,
158 unsigned *num_configs, unsigned long config)
159{
160 unsigned old_num = *num_configs;
161 unsigned new_num = old_num + 1;
162 unsigned long *new_configs;
163
164 new_configs = krealloc(*configs, sizeof(*new_configs) * new_num,
165 GFP_KERNEL);
166 if (!new_configs) {
167 dev_err(dev, "krealloc(configs) failed\n");
168 return -ENOMEM;
169 }
170
171 new_configs[old_num] = config;
172
173 *configs = new_configs;
174 *num_configs = new_num;
175
176 return 0;
177}
178
179static void samsung_dt_free_map(struct pinctrl_dev *pctldev,
180 struct pinctrl_map *map,
181 unsigned num_maps)
182{
183 int i;
184
185 for (i = 0; i < num_maps; i++)
186 if (map[i].type == PIN_MAP_TYPE_CONFIGS_GROUP)
187 kfree(map[i].data.configs.configs);
Thomas Abraham30574f02012-09-07 06:07:19 +0900188
189 kfree(map);
190}
191
Tomasz Figa9a2c1c32014-07-02 17:41:03 +0200192static int samsung_dt_subnode_to_map(struct samsung_pinctrl_drv_data *drvdata,
193 struct device *dev,
194 struct device_node *np,
195 struct pinctrl_map **map,
196 unsigned *reserved_maps,
197 unsigned *num_maps)
198{
199 int ret, i;
200 u32 val;
201 unsigned long config;
202 unsigned long *configs = NULL;
203 unsigned num_configs = 0;
204 unsigned reserve;
205 struct property *prop;
206 const char *group;
207 bool has_func = false;
208
209 ret = of_property_read_u32(np, "samsung,pin-function", &val);
210 if (!ret)
211 has_func = true;
212
213 for (i = 0; i < ARRAY_SIZE(cfg_params); i++) {
214 ret = of_property_read_u32(np, cfg_params[i].property, &val);
215 if (!ret) {
216 config = PINCFG_PACK(cfg_params[i].param, val);
217 ret = add_config(dev, &configs, &num_configs, config);
218 if (ret < 0)
219 goto exit;
220 /* EINVAL=missing, which is fine since it's optional */
221 } else if (ret != -EINVAL) {
222 dev_err(dev, "could not parse property %s\n",
223 cfg_params[i].property);
224 }
225 }
226
227 reserve = 0;
228 if (has_func)
229 reserve++;
230 if (num_configs)
231 reserve++;
232 ret = of_property_count_strings(np, "samsung,pins");
233 if (ret < 0) {
234 dev_err(dev, "could not parse property samsung,pins\n");
235 goto exit;
236 }
237 reserve *= ret;
238
239 ret = reserve_map(dev, map, reserved_maps, num_maps, reserve);
240 if (ret < 0)
241 goto exit;
242
243 of_property_for_each_string(np, "samsung,pins", prop, group) {
244 if (has_func) {
245 ret = add_map_mux(map, reserved_maps,
246 num_maps, group, np->full_name);
247 if (ret < 0)
248 goto exit;
249 }
250
251 if (num_configs) {
252 ret = add_map_configs(dev, map, reserved_maps,
253 num_maps, group, configs,
254 num_configs);
255 if (ret < 0)
256 goto exit;
257 }
258 }
259
260 ret = 0;
261
262exit:
263 kfree(configs);
264 return ret;
265}
266
267static int samsung_dt_node_to_map(struct pinctrl_dev *pctldev,
268 struct device_node *np_config,
269 struct pinctrl_map **map,
270 unsigned *num_maps)
271{
272 struct samsung_pinctrl_drv_data *drvdata;
273 unsigned reserved_maps;
274 struct device_node *np;
275 int ret;
276
277 drvdata = pinctrl_dev_get_drvdata(pctldev);
278
279 reserved_maps = 0;
280 *map = NULL;
281 *num_maps = 0;
282
283 if (!of_get_child_count(np_config))
284 return samsung_dt_subnode_to_map(drvdata, pctldev->dev,
285 np_config, map,
286 &reserved_maps,
287 num_maps);
288
289 for_each_child_of_node(np_config, np) {
290 ret = samsung_dt_subnode_to_map(drvdata, pctldev->dev, np, map,
291 &reserved_maps, num_maps);
292 if (ret < 0) {
293 samsung_dt_free_map(pctldev, *map, *num_maps);
294 return ret;
295 }
296 }
297
298 return 0;
299}
300
Thomas Abraham30574f02012-09-07 06:07:19 +0900301/* list of pinctrl callbacks for the pinctrl core */
Laurent Pinchart022ab142013-02-16 10:25:07 +0100302static const struct pinctrl_ops samsung_pctrl_ops = {
Thomas Abraham30574f02012-09-07 06:07:19 +0900303 .get_groups_count = samsung_get_group_count,
304 .get_group_name = samsung_get_group_name,
305 .get_group_pins = samsung_get_group_pins,
306 .dt_node_to_map = samsung_dt_node_to_map,
307 .dt_free_map = samsung_dt_free_map,
308};
309
310/* check if the selector is a valid pin function selector */
311static int samsung_get_functions_count(struct pinctrl_dev *pctldev)
312{
313 struct samsung_pinctrl_drv_data *drvdata;
314
315 drvdata = pinctrl_dev_get_drvdata(pctldev);
316 return drvdata->nr_functions;
317}
318
319/* return the name of the pin function specified */
320static const char *samsung_pinmux_get_fname(struct pinctrl_dev *pctldev,
321 unsigned selector)
322{
323 struct samsung_pinctrl_drv_data *drvdata;
324
325 drvdata = pinctrl_dev_get_drvdata(pctldev);
326 return drvdata->pmx_functions[selector].name;
327}
328
329/* return the groups associated for the specified function selector */
330static int samsung_pinmux_get_groups(struct pinctrl_dev *pctldev,
331 unsigned selector, const char * const **groups,
332 unsigned * const num_groups)
333{
334 struct samsung_pinctrl_drv_data *drvdata;
335
336 drvdata = pinctrl_dev_get_drvdata(pctldev);
337 *groups = drvdata->pmx_functions[selector].groups;
338 *num_groups = drvdata->pmx_functions[selector].num_groups;
339 return 0;
340}
341
342/*
343 * given a pin number that is local to a pin controller, find out the pin bank
344 * and the register base of the pin bank.
345 */
Tomasz Figa62f14c02012-10-11 10:11:08 +0200346static void pin_to_reg_bank(struct samsung_pinctrl_drv_data *drvdata,
347 unsigned pin, void __iomem **reg, u32 *offset,
Thomas Abraham30574f02012-09-07 06:07:19 +0900348 struct samsung_pin_bank **bank)
349{
Thomas Abraham30574f02012-09-07 06:07:19 +0900350 struct samsung_pin_bank *b;
351
Tomasz Figa1bf00d72014-09-23 21:05:40 +0200352 b = drvdata->pin_banks;
Thomas Abraham30574f02012-09-07 06:07:19 +0900353
354 while ((pin >= b->pin_base) &&
355 ((b->pin_base + b->nr_pins - 1) < pin))
356 b++;
357
358 *reg = drvdata->virt_base + b->pctl_offset;
359 *offset = pin - b->pin_base;
360 if (bank)
361 *bank = b;
Thomas Abraham30574f02012-09-07 06:07:19 +0900362}
363
364/* enable or disable a pinmux function */
365static void samsung_pinmux_setup(struct pinctrl_dev *pctldev, unsigned selector,
366 unsigned group, bool enable)
367{
368 struct samsung_pinctrl_drv_data *drvdata;
Tomasz Figa94ce9442014-09-23 21:05:39 +0200369 const struct samsung_pin_bank_type *type;
Thomas Abraham30574f02012-09-07 06:07:19 +0900370 struct samsung_pin_bank *bank;
371 void __iomem *reg;
Tomasz Figa9a2c1c32014-07-02 17:41:03 +0200372 u32 mask, shift, data, pin_offset;
Tomasz Figa19846952013-03-18 22:31:50 +0100373 unsigned long flags;
Tomasz Figa9a2c1c32014-07-02 17:41:03 +0200374 const struct samsung_pmx_func *func;
375 const struct samsung_pin_group *grp;
Thomas Abraham30574f02012-09-07 06:07:19 +0900376
377 drvdata = pinctrl_dev_get_drvdata(pctldev);
Tomasz Figa9a2c1c32014-07-02 17:41:03 +0200378 func = &drvdata->pmx_functions[selector];
379 grp = &drvdata->pin_groups[group];
Thomas Abraham30574f02012-09-07 06:07:19 +0900380
Tomasz Figa1bf00d72014-09-23 21:05:40 +0200381 pin_to_reg_bank(drvdata, grp->pins[0] - drvdata->pin_base,
Tomasz Figa9a2c1c32014-07-02 17:41:03 +0200382 &reg, &pin_offset, &bank);
383 type = bank->type;
384 mask = (1 << type->fld_width[PINCFG_TYPE_FUNC]) - 1;
385 shift = pin_offset * type->fld_width[PINCFG_TYPE_FUNC];
386 if (shift >= 32) {
387 /* Some banks have two config registers */
388 shift -= 32;
389 reg += 4;
Thomas Abraham30574f02012-09-07 06:07:19 +0900390 }
Tomasz Figa9a2c1c32014-07-02 17:41:03 +0200391
392 spin_lock_irqsave(&bank->slock, flags);
393
394 data = readl(reg + type->reg_offset[PINCFG_TYPE_FUNC]);
395 data &= ~(mask << shift);
396 if (enable)
397 data |= func->val << shift;
398 writel(data, reg + type->reg_offset[PINCFG_TYPE_FUNC]);
399
400 spin_unlock_irqrestore(&bank->slock, flags);
Thomas Abraham30574f02012-09-07 06:07:19 +0900401}
402
403/* enable a specified pinmux by writing to registers */
Linus Walleij03e9f0c2014-09-03 13:02:56 +0200404static int samsung_pinmux_set_mux(struct pinctrl_dev *pctldev,
405 unsigned selector,
406 unsigned group)
Thomas Abraham30574f02012-09-07 06:07:19 +0900407{
408 samsung_pinmux_setup(pctldev, selector, group, true);
409 return 0;
410}
411
Thomas Abraham30574f02012-09-07 06:07:19 +0900412/* list of pinmux callbacks for the pinmux vertical in pinctrl core */
Laurent Pinchart022ab142013-02-16 10:25:07 +0100413static const struct pinmux_ops samsung_pinmux_ops = {
Thomas Abraham30574f02012-09-07 06:07:19 +0900414 .get_functions_count = samsung_get_functions_count,
415 .get_function_name = samsung_pinmux_get_fname,
416 .get_function_groups = samsung_pinmux_get_groups,
Linus Walleij03e9f0c2014-09-03 13:02:56 +0200417 .set_mux = samsung_pinmux_set_mux,
Thomas Abraham30574f02012-09-07 06:07:19 +0900418};
419
420/* set or get the pin config settings for a specified pin */
421static int samsung_pinconf_rw(struct pinctrl_dev *pctldev, unsigned int pin,
422 unsigned long *config, bool set)
423{
424 struct samsung_pinctrl_drv_data *drvdata;
Tomasz Figa94ce9442014-09-23 21:05:39 +0200425 const struct samsung_pin_bank_type *type;
Thomas Abraham30574f02012-09-07 06:07:19 +0900426 struct samsung_pin_bank *bank;
427 void __iomem *reg_base;
428 enum pincfg_type cfg_type = PINCFG_UNPACK_TYPE(*config);
429 u32 data, width, pin_offset, mask, shift;
430 u32 cfg_value, cfg_reg;
Tomasz Figa19846952013-03-18 22:31:50 +0100431 unsigned long flags;
Thomas Abraham30574f02012-09-07 06:07:19 +0900432
433 drvdata = pinctrl_dev_get_drvdata(pctldev);
Tomasz Figa1bf00d72014-09-23 21:05:40 +0200434 pin_to_reg_bank(drvdata, pin - drvdata->pin_base, &reg_base,
Thomas Abraham30574f02012-09-07 06:07:19 +0900435 &pin_offset, &bank);
Tomasz Figa499147c2013-03-18 22:31:52 +0100436 type = bank->type;
Thomas Abraham30574f02012-09-07 06:07:19 +0900437
Tomasz Figa499147c2013-03-18 22:31:52 +0100438 if (cfg_type >= PINCFG_TYPE_NUM || !type->fld_width[cfg_type])
Tomasz Figa7c367d32012-10-11 10:11:07 +0200439 return -EINVAL;
440
Tomasz Figa499147c2013-03-18 22:31:52 +0100441 width = type->fld_width[cfg_type];
Tomasz Figa43fc9e72013-03-18 22:31:53 +0100442 cfg_reg = type->reg_offset[cfg_type];
Tomasz Figa499147c2013-03-18 22:31:52 +0100443
Tomasz Figa19846952013-03-18 22:31:50 +0100444 spin_lock_irqsave(&bank->slock, flags);
445
Thomas Abraham30574f02012-09-07 06:07:19 +0900446 mask = (1 << width) - 1;
447 shift = pin_offset * width;
448 data = readl(reg_base + cfg_reg);
449
450 if (set) {
451 cfg_value = PINCFG_UNPACK_VALUE(*config);
452 data &= ~(mask << shift);
453 data |= (cfg_value << shift);
454 writel(data, reg_base + cfg_reg);
455 } else {
456 data >>= shift;
457 data &= mask;
458 *config = PINCFG_PACK(cfg_type, data);
459 }
Tomasz Figa19846952013-03-18 22:31:50 +0100460
461 spin_unlock_irqrestore(&bank->slock, flags);
462
Thomas Abraham30574f02012-09-07 06:07:19 +0900463 return 0;
464}
465
466/* set the pin config settings for a specified pin */
467static int samsung_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin,
Sherman Yin03b054e2013-08-27 11:32:12 -0700468 unsigned long *configs, unsigned num_configs)
Thomas Abraham30574f02012-09-07 06:07:19 +0900469{
Sherman Yin03b054e2013-08-27 11:32:12 -0700470 int i, ret;
471
472 for (i = 0; i < num_configs; i++) {
473 ret = samsung_pinconf_rw(pctldev, pin, &configs[i], true);
474 if (ret < 0)
475 return ret;
476 } /* for each config */
477
478 return 0;
Thomas Abraham30574f02012-09-07 06:07:19 +0900479}
480
481/* get the pin config settings for a specified pin */
482static int samsung_pinconf_get(struct pinctrl_dev *pctldev, unsigned int pin,
483 unsigned long *config)
484{
485 return samsung_pinconf_rw(pctldev, pin, config, false);
486}
487
488/* set the pin config settings for a specified pin group */
489static int samsung_pinconf_group_set(struct pinctrl_dev *pctldev,
Sherman Yin03b054e2013-08-27 11:32:12 -0700490 unsigned group, unsigned long *configs,
491 unsigned num_configs)
Thomas Abraham30574f02012-09-07 06:07:19 +0900492{
493 struct samsung_pinctrl_drv_data *drvdata;
494 const unsigned int *pins;
495 unsigned int cnt;
496
497 drvdata = pinctrl_dev_get_drvdata(pctldev);
498 pins = drvdata->pin_groups[group].pins;
499
500 for (cnt = 0; cnt < drvdata->pin_groups[group].num_pins; cnt++)
Sherman Yin03b054e2013-08-27 11:32:12 -0700501 samsung_pinconf_set(pctldev, pins[cnt], configs, num_configs);
Thomas Abraham30574f02012-09-07 06:07:19 +0900502
503 return 0;
504}
505
506/* get the pin config settings for a specified pin group */
507static int samsung_pinconf_group_get(struct pinctrl_dev *pctldev,
508 unsigned int group, unsigned long *config)
509{
510 struct samsung_pinctrl_drv_data *drvdata;
511 const unsigned int *pins;
512
513 drvdata = pinctrl_dev_get_drvdata(pctldev);
514 pins = drvdata->pin_groups[group].pins;
515 samsung_pinconf_get(pctldev, pins[0], config);
516 return 0;
517}
518
519/* list of pinconfig callbacks for pinconfig vertical in the pinctrl code */
Laurent Pinchart022ab142013-02-16 10:25:07 +0100520static const struct pinconf_ops samsung_pinconf_ops = {
Thomas Abraham30574f02012-09-07 06:07:19 +0900521 .pin_config_get = samsung_pinconf_get,
522 .pin_config_set = samsung_pinconf_set,
523 .pin_config_group_get = samsung_pinconf_group_get,
524 .pin_config_group_set = samsung_pinconf_group_set,
525};
526
527/* gpiolib gpio_set callback function */
528static void samsung_gpio_set(struct gpio_chip *gc, unsigned offset, int value)
529{
Tomasz Figad3a7b9e2012-10-11 10:11:17 +0200530 struct samsung_pin_bank *bank = gc_to_pin_bank(gc);
Tomasz Figa94ce9442014-09-23 21:05:39 +0200531 const struct samsung_pin_bank_type *type = bank->type;
Tomasz Figa19846952013-03-18 22:31:50 +0100532 unsigned long flags;
Thomas Abraham30574f02012-09-07 06:07:19 +0900533 void __iomem *reg;
Tomasz Figad3a7b9e2012-10-11 10:11:17 +0200534 u32 data;
Thomas Abraham30574f02012-09-07 06:07:19 +0900535
Tomasz Figad3a7b9e2012-10-11 10:11:17 +0200536 reg = bank->drvdata->virt_base + bank->pctl_offset;
Tomasz Figa62f14c02012-10-11 10:11:08 +0200537
Tomasz Figa19846952013-03-18 22:31:50 +0100538 spin_lock_irqsave(&bank->slock, flags);
539
Tomasz Figa43fc9e72013-03-18 22:31:53 +0100540 data = readl(reg + type->reg_offset[PINCFG_TYPE_DAT]);
Tomasz Figad3a7b9e2012-10-11 10:11:17 +0200541 data &= ~(1 << offset);
Thomas Abraham30574f02012-09-07 06:07:19 +0900542 if (value)
Tomasz Figad3a7b9e2012-10-11 10:11:17 +0200543 data |= 1 << offset;
Tomasz Figa43fc9e72013-03-18 22:31:53 +0100544 writel(data, reg + type->reg_offset[PINCFG_TYPE_DAT]);
Tomasz Figa19846952013-03-18 22:31:50 +0100545
546 spin_unlock_irqrestore(&bank->slock, flags);
Thomas Abraham30574f02012-09-07 06:07:19 +0900547}
548
549/* gpiolib gpio_get callback function */
550static int samsung_gpio_get(struct gpio_chip *gc, unsigned offset)
551{
552 void __iomem *reg;
Tomasz Figad3a7b9e2012-10-11 10:11:17 +0200553 u32 data;
554 struct samsung_pin_bank *bank = gc_to_pin_bank(gc);
Tomasz Figa94ce9442014-09-23 21:05:39 +0200555 const struct samsung_pin_bank_type *type = bank->type;
Thomas Abraham30574f02012-09-07 06:07:19 +0900556
Tomasz Figad3a7b9e2012-10-11 10:11:17 +0200557 reg = bank->drvdata->virt_base + bank->pctl_offset;
Tomasz Figa62f14c02012-10-11 10:11:08 +0200558
Tomasz Figa43fc9e72013-03-18 22:31:53 +0100559 data = readl(reg + type->reg_offset[PINCFG_TYPE_DAT]);
Tomasz Figad3a7b9e2012-10-11 10:11:17 +0200560 data >>= offset;
Thomas Abraham30574f02012-09-07 06:07:19 +0900561 data &= 1;
562 return data;
563}
564
565/*
Tomasz Figa18c28ca2014-07-02 17:40:59 +0200566 * The calls to gpio_direction_output() and gpio_direction_input()
567 * leads to this function call.
Thomas Abraham30574f02012-09-07 06:07:19 +0900568 */
Tomasz Figa18c28ca2014-07-02 17:40:59 +0200569static int samsung_gpio_set_direction(struct gpio_chip *gc,
570 unsigned offset, bool input)
Thomas Abraham30574f02012-09-07 06:07:19 +0900571{
Tomasz Figa94ce9442014-09-23 21:05:39 +0200572 const struct samsung_pin_bank_type *type;
Tomasz Figa18c28ca2014-07-02 17:40:59 +0200573 struct samsung_pin_bank *bank;
574 struct samsung_pinctrl_drv_data *drvdata;
575 void __iomem *reg;
576 u32 data, mask, shift;
577 unsigned long flags;
578
579 bank = gc_to_pin_bank(gc);
580 type = bank->type;
581 drvdata = bank->drvdata;
582
583 reg = drvdata->virt_base + bank->pctl_offset +
584 type->reg_offset[PINCFG_TYPE_FUNC];
585
586 mask = (1 << type->fld_width[PINCFG_TYPE_FUNC]) - 1;
587 shift = offset * type->fld_width[PINCFG_TYPE_FUNC];
588 if (shift >= 32) {
589 /* Some banks have two config registers */
590 shift -= 32;
591 reg += 4;
592 }
593
594 spin_lock_irqsave(&bank->slock, flags);
595
596 data = readl(reg);
597 data &= ~(mask << shift);
598 if (!input)
599 data |= FUNC_OUTPUT << shift;
600 writel(data, reg);
601
602 spin_unlock_irqrestore(&bank->slock, flags);
603
604 return 0;
Thomas Abraham30574f02012-09-07 06:07:19 +0900605}
606
Tomasz Figa18c28ca2014-07-02 17:40:59 +0200607/* gpiolib gpio_direction_input callback function. */
608static int samsung_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
609{
610 return samsung_gpio_set_direction(gc, offset, true);
611}
612
613/* gpiolib gpio_direction_output callback function. */
Thomas Abraham30574f02012-09-07 06:07:19 +0900614static int samsung_gpio_direction_output(struct gpio_chip *gc, unsigned offset,
615 int value)
616{
617 samsung_gpio_set(gc, offset, value);
Tomasz Figa18c28ca2014-07-02 17:40:59 +0200618 return samsung_gpio_set_direction(gc, offset, false);
Thomas Abraham30574f02012-09-07 06:07:19 +0900619}
620
621/*
Tomasz Figaa19fe2d2012-10-11 10:11:20 +0200622 * gpiolib gpio_to_irq callback function. Creates a mapping between a GPIO pin
623 * and a virtual IRQ, if not already present.
624 */
625static int samsung_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
626{
627 struct samsung_pin_bank *bank = gc_to_pin_bank(gc);
628 unsigned int virq;
629
630 if (!bank->irq_domain)
631 return -ENXIO;
632
633 virq = irq_create_mapping(bank->irq_domain, offset);
634
635 return (virq) ? : -ENXIO;
636}
637
Tomasz Figa9a2c1c32014-07-02 17:41:03 +0200638static struct samsung_pin_group *samsung_pinctrl_create_groups(
639 struct device *dev,
640 struct samsung_pinctrl_drv_data *drvdata,
641 unsigned int *cnt)
Thomas Abraham30574f02012-09-07 06:07:19 +0900642{
Tomasz Figa9a2c1c32014-07-02 17:41:03 +0200643 struct pinctrl_desc *ctrldesc = &drvdata->pctl;
Thomas Abraham30574f02012-09-07 06:07:19 +0900644 struct samsung_pin_group *groups, *grp;
Tomasz Figa9a2c1c32014-07-02 17:41:03 +0200645 const struct pinctrl_pin_desc *pdesc;
646 int i;
Thomas Abraham30574f02012-09-07 06:07:19 +0900647
Tomasz Figa9a2c1c32014-07-02 17:41:03 +0200648 groups = devm_kzalloc(dev, ctrldesc->npins * sizeof(*groups),
649 GFP_KERNEL);
650 if (!groups)
651 return ERR_PTR(-EINVAL);
Thomas Abraham30574f02012-09-07 06:07:19 +0900652 grp = groups;
653
Tomasz Figa9a2c1c32014-07-02 17:41:03 +0200654 pdesc = ctrldesc->pins;
655 for (i = 0; i < ctrldesc->npins; ++i, ++pdesc, ++grp) {
656 grp->name = pdesc->name;
657 grp->pins = &pdesc->number;
658 grp->num_pins = 1;
659 }
660
661 *cnt = ctrldesc->npins;
662 return groups;
663}
664
665static int samsung_pinctrl_create_function(struct device *dev,
666 struct samsung_pinctrl_drv_data *drvdata,
667 struct device_node *func_np,
668 struct samsung_pmx_func *func)
669{
670 int npins;
671 int ret;
672 int i;
673
674 if (of_property_read_u32(func_np, "samsung,pin-function", &func->val))
675 return 0;
676
677 npins = of_property_count_strings(func_np, "samsung,pins");
678 if (npins < 1) {
679 dev_err(dev, "invalid pin list in %s node", func_np->name);
680 return -EINVAL;
681 }
682
683 func->name = func_np->full_name;
684
685 func->groups = devm_kzalloc(dev, npins * sizeof(char *), GFP_KERNEL);
686 if (!func->groups)
687 return -ENOMEM;
688
689 for (i = 0; i < npins; ++i) {
690 const char *gname;
691
692 ret = of_property_read_string_index(func_np, "samsung,pins",
693 i, &gname);
694 if (ret) {
695 dev_err(dev,
696 "failed to read pin name %d from %s node\n",
697 i, func_np->name);
698 return ret;
699 }
700
701 func->groups[i] = gname;
702 }
703
704 func->num_groups = npins;
705 return 1;
706}
707
708static struct samsung_pmx_func *samsung_pinctrl_create_functions(
709 struct device *dev,
710 struct samsung_pinctrl_drv_data *drvdata,
711 unsigned int *cnt)
712{
713 struct samsung_pmx_func *functions, *func;
714 struct device_node *dev_np = dev->of_node;
715 struct device_node *cfg_np;
716 unsigned int func_cnt = 0;
717 int ret;
718
719 /*
720 * Iterate over all the child nodes of the pin controller node
721 * and create pin groups and pin function lists.
722 */
723 for_each_child_of_node(dev_np, cfg_np) {
724 struct device_node *func_np;
725
726 if (!of_get_child_count(cfg_np)) {
727 if (!of_find_property(cfg_np,
728 "samsung,pin-function", NULL))
729 continue;
730 ++func_cnt;
731 continue;
732 }
733
734 for_each_child_of_node(cfg_np, func_np) {
735 if (!of_find_property(func_np,
736 "samsung,pin-function", NULL))
737 continue;
738 ++func_cnt;
739 }
740 }
741
742 functions = devm_kzalloc(dev, func_cnt * sizeof(*functions),
743 GFP_KERNEL);
Thomas Abraham30574f02012-09-07 06:07:19 +0900744 if (!functions) {
745 dev_err(dev, "failed to allocate memory for function list\n");
Tomasz Figa9a2c1c32014-07-02 17:41:03 +0200746 return ERR_PTR(-EINVAL);
Thomas Abraham30574f02012-09-07 06:07:19 +0900747 }
748 func = functions;
749
750 /*
751 * Iterate over all the child nodes of the pin controller node
752 * and create pin groups and pin function lists.
753 */
Tomasz Figa9a2c1c32014-07-02 17:41:03 +0200754 func_cnt = 0;
Thomas Abraham30574f02012-09-07 06:07:19 +0900755 for_each_child_of_node(dev_np, cfg_np) {
Tomasz Figa9a2c1c32014-07-02 17:41:03 +0200756 struct device_node *func_np;
757
758 if (!of_get_child_count(cfg_np)) {
759 ret = samsung_pinctrl_create_function(dev, drvdata,
760 cfg_np, func);
761 if (ret < 0)
762 return ERR_PTR(ret);
763 if (ret > 0) {
764 ++func;
765 ++func_cnt;
766 }
Thomas Abraham30574f02012-09-07 06:07:19 +0900767 continue;
Thomas Abraham30574f02012-09-07 06:07:19 +0900768 }
Thomas Abraham30574f02012-09-07 06:07:19 +0900769
Tomasz Figa9a2c1c32014-07-02 17:41:03 +0200770 for_each_child_of_node(cfg_np, func_np) {
771 ret = samsung_pinctrl_create_function(dev, drvdata,
772 func_np, func);
773 if (ret < 0)
774 return ERR_PTR(ret);
775 if (ret > 0) {
776 ++func;
777 ++func_cnt;
778 }
Thomas Abraham30574f02012-09-07 06:07:19 +0900779 }
Tomasz Figa9a2c1c32014-07-02 17:41:03 +0200780 }
Thomas Abraham30574f02012-09-07 06:07:19 +0900781
Tomasz Figa9a2c1c32014-07-02 17:41:03 +0200782 *cnt = func_cnt;
783 return functions;
784}
785
786/*
787 * Parse the information about all the available pin groups and pin functions
788 * from device node of the pin-controller. A pin group is formed with all
789 * the pins listed in the "samsung,pins" property.
790 */
791
792static int samsung_pinctrl_parse_dt(struct platform_device *pdev,
793 struct samsung_pinctrl_drv_data *drvdata)
794{
795 struct device *dev = &pdev->dev;
796 struct samsung_pin_group *groups;
797 struct samsung_pmx_func *functions;
798 unsigned int grp_cnt = 0, func_cnt = 0;
799
800 groups = samsung_pinctrl_create_groups(dev, drvdata, &grp_cnt);
801 if (IS_ERR(groups)) {
802 dev_err(dev, "failed to parse pin groups\n");
803 return PTR_ERR(groups);
804 }
805
806 functions = samsung_pinctrl_create_functions(dev, drvdata, &func_cnt);
807 if (IS_ERR(functions)) {
808 dev_err(dev, "failed to parse pin functions\n");
809 return PTR_ERR(groups);
Thomas Abraham30574f02012-09-07 06:07:19 +0900810 }
811
812 drvdata->pin_groups = groups;
813 drvdata->nr_groups = grp_cnt;
814 drvdata->pmx_functions = functions;
Tomasz Figa9a2c1c32014-07-02 17:41:03 +0200815 drvdata->nr_functions = func_cnt;
Thomas Abraham30574f02012-09-07 06:07:19 +0900816
817 return 0;
818}
819
820/* register the pinctrl interface with the pinctrl subsystem */
Greg Kroah-Hartman150632b2012-12-21 13:10:23 -0800821static int samsung_pinctrl_register(struct platform_device *pdev,
822 struct samsung_pinctrl_drv_data *drvdata)
Thomas Abraham30574f02012-09-07 06:07:19 +0900823{
824 struct pinctrl_desc *ctrldesc = &drvdata->pctl;
825 struct pinctrl_pin_desc *pindesc, *pdesc;
826 struct samsung_pin_bank *pin_bank;
827 char *pin_names;
828 int pin, bank, ret;
829
830 ctrldesc->name = "samsung-pinctrl";
831 ctrldesc->owner = THIS_MODULE;
832 ctrldesc->pctlops = &samsung_pctrl_ops;
833 ctrldesc->pmxops = &samsung_pinmux_ops;
834 ctrldesc->confops = &samsung_pinconf_ops;
835
836 pindesc = devm_kzalloc(&pdev->dev, sizeof(*pindesc) *
Tomasz Figa1bf00d72014-09-23 21:05:40 +0200837 drvdata->nr_pins, GFP_KERNEL);
Thomas Abraham30574f02012-09-07 06:07:19 +0900838 if (!pindesc) {
839 dev_err(&pdev->dev, "mem alloc for pin descriptors failed\n");
840 return -ENOMEM;
841 }
842 ctrldesc->pins = pindesc;
Tomasz Figa1bf00d72014-09-23 21:05:40 +0200843 ctrldesc->npins = drvdata->nr_pins;
Thomas Abraham30574f02012-09-07 06:07:19 +0900844
845 /* dynamically populate the pin number and pin name for pindesc */
846 for (pin = 0, pdesc = pindesc; pin < ctrldesc->npins; pin++, pdesc++)
Tomasz Figa1bf00d72014-09-23 21:05:40 +0200847 pdesc->number = pin + drvdata->pin_base;
Thomas Abraham30574f02012-09-07 06:07:19 +0900848
849 /*
850 * allocate space for storing the dynamically generated names for all
851 * the pins which belong to this pin-controller.
852 */
853 pin_names = devm_kzalloc(&pdev->dev, sizeof(char) * PIN_NAME_LENGTH *
Tomasz Figa1bf00d72014-09-23 21:05:40 +0200854 drvdata->nr_pins, GFP_KERNEL);
Thomas Abraham30574f02012-09-07 06:07:19 +0900855 if (!pin_names) {
856 dev_err(&pdev->dev, "mem alloc for pin names failed\n");
857 return -ENOMEM;
858 }
859
860 /* for each pin, the name of the pin is pin-bank name + pin number */
Tomasz Figa1bf00d72014-09-23 21:05:40 +0200861 for (bank = 0; bank < drvdata->nr_banks; bank++) {
862 pin_bank = &drvdata->pin_banks[bank];
Thomas Abraham30574f02012-09-07 06:07:19 +0900863 for (pin = 0; pin < pin_bank->nr_pins; pin++) {
864 sprintf(pin_names, "%s-%d", pin_bank->name, pin);
865 pdesc = pindesc + pin_bank->pin_base + pin;
866 pdesc->name = pin_names;
867 pin_names += PIN_NAME_LENGTH;
868 }
869 }
870
Tomasz Figa529301c2013-08-20 18:16:21 +0200871 ret = samsung_pinctrl_parse_dt(pdev, drvdata);
872 if (ret)
873 return ret;
874
Thomas Abraham30574f02012-09-07 06:07:19 +0900875 drvdata->pctl_dev = pinctrl_register(ctrldesc, &pdev->dev, drvdata);
876 if (!drvdata->pctl_dev) {
877 dev_err(&pdev->dev, "could not register pinctrl driver\n");
878 return -EINVAL;
879 }
880
Tomasz Figa1bf00d72014-09-23 21:05:40 +0200881 for (bank = 0; bank < drvdata->nr_banks; ++bank) {
882 pin_bank = &drvdata->pin_banks[bank];
Tomasz Figad3a7b9e2012-10-11 10:11:17 +0200883 pin_bank->grange.name = pin_bank->name;
884 pin_bank->grange.id = bank;
Tomasz Figa1bf00d72014-09-23 21:05:40 +0200885 pin_bank->grange.pin_base = drvdata->pin_base
Tomasz Figa6c6ce622014-07-02 17:41:00 +0200886 + pin_bank->pin_base;
Tomasz Figad3a7b9e2012-10-11 10:11:17 +0200887 pin_bank->grange.base = pin_bank->gpio_chip.base;
888 pin_bank->grange.npins = pin_bank->gpio_chip.ngpio;
889 pin_bank->grange.gc = &pin_bank->gpio_chip;
890 pinctrl_add_gpio_range(drvdata->pctl_dev, &pin_bank->grange);
891 }
Thomas Abraham30574f02012-09-07 06:07:19 +0900892
Thomas Abraham30574f02012-09-07 06:07:19 +0900893 return 0;
894}
895
Tomasz Figa6c6ce622014-07-02 17:41:00 +0200896static int samsung_gpio_request(struct gpio_chip *chip, unsigned offset)
897{
898 return pinctrl_request_gpio(chip->base + offset);
899}
900
901static void samsung_gpio_free(struct gpio_chip *chip, unsigned offset)
902{
903 pinctrl_free_gpio(chip->base + offset);
904}
905
Tomasz Figad3a7b9e2012-10-11 10:11:17 +0200906static const struct gpio_chip samsung_gpiolib_chip = {
Tomasz Figa6c6ce622014-07-02 17:41:00 +0200907 .request = samsung_gpio_request,
908 .free = samsung_gpio_free,
Tomasz Figad3a7b9e2012-10-11 10:11:17 +0200909 .set = samsung_gpio_set,
910 .get = samsung_gpio_get,
911 .direction_input = samsung_gpio_direction_input,
912 .direction_output = samsung_gpio_direction_output,
Tomasz Figaa19fe2d2012-10-11 10:11:20 +0200913 .to_irq = samsung_gpio_to_irq,
Tomasz Figad3a7b9e2012-10-11 10:11:17 +0200914 .owner = THIS_MODULE,
915};
916
Thomas Abraham30574f02012-09-07 06:07:19 +0900917/* register the gpiolib interface with the gpiolib subsystem */
Greg Kroah-Hartman150632b2012-12-21 13:10:23 -0800918static int samsung_gpiolib_register(struct platform_device *pdev,
919 struct samsung_pinctrl_drv_data *drvdata)
Thomas Abraham30574f02012-09-07 06:07:19 +0900920{
Tomasz Figa1bf00d72014-09-23 21:05:40 +0200921 struct samsung_pin_bank *bank = drvdata->pin_banks;
Thomas Abraham30574f02012-09-07 06:07:19 +0900922 struct gpio_chip *gc;
923 int ret;
Tomasz Figad3a7b9e2012-10-11 10:11:17 +0200924 int i;
Thomas Abraham30574f02012-09-07 06:07:19 +0900925
Tomasz Figa1bf00d72014-09-23 21:05:40 +0200926 for (i = 0; i < drvdata->nr_banks; ++i, ++bank) {
Tomasz Figad3a7b9e2012-10-11 10:11:17 +0200927 bank->gpio_chip = samsung_gpiolib_chip;
Thomas Abraham30574f02012-09-07 06:07:19 +0900928
Tomasz Figad3a7b9e2012-10-11 10:11:17 +0200929 gc = &bank->gpio_chip;
Tomasz Figa1bf00d72014-09-23 21:05:40 +0200930 gc->base = drvdata->pin_base + bank->pin_base;
Tomasz Figad3a7b9e2012-10-11 10:11:17 +0200931 gc->ngpio = bank->nr_pins;
932 gc->dev = &pdev->dev;
933 gc->of_node = bank->of_node;
934 gc->label = bank->name;
935
936 ret = gpiochip_add(gc);
937 if (ret) {
938 dev_err(&pdev->dev, "failed to register gpio_chip %s, error code: %d\n",
939 gc->label, ret);
940 goto fail;
941 }
Thomas Abraham30574f02012-09-07 06:07:19 +0900942 }
943
944 return 0;
Tomasz Figad3a7b9e2012-10-11 10:11:17 +0200945
946fail:
947 for (--i, --bank; i >= 0; --i, --bank)
Linus Walleij2fcea6c2014-09-16 15:05:41 -0700948 gpiochip_remove(&bank->gpio_chip);
Tomasz Figad3a7b9e2012-10-11 10:11:17 +0200949 return ret;
Thomas Abraham30574f02012-09-07 06:07:19 +0900950}
951
952/* unregister the gpiolib interface with the gpiolib subsystem */
Greg Kroah-Hartman150632b2012-12-21 13:10:23 -0800953static int samsung_gpiolib_unregister(struct platform_device *pdev,
954 struct samsung_pinctrl_drv_data *drvdata)
Thomas Abraham30574f02012-09-07 06:07:19 +0900955{
Tomasz Figa1bf00d72014-09-23 21:05:40 +0200956 struct samsung_pin_bank *bank = drvdata->pin_banks;
Tomasz Figad3a7b9e2012-10-11 10:11:17 +0200957 int i;
958
Tomasz Figa1bf00d72014-09-23 21:05:40 +0200959 for (i = 0; i < drvdata->nr_banks; ++i, ++bank)
Linus Walleij2fcea6c2014-09-16 15:05:41 -0700960 gpiochip_remove(&bank->gpio_chip);
Tomasz Figa1bf00d72014-09-23 21:05:40 +0200961
Linus Walleij2fcea6c2014-09-16 15:05:41 -0700962 return 0;
Thomas Abraham30574f02012-09-07 06:07:19 +0900963}
964
965static const struct of_device_id samsung_pinctrl_dt_match[];
966
967/* retrieve the soc specific data */
Tomasz Figa1bf00d72014-09-23 21:05:40 +0200968static const struct samsung_pin_ctrl *
969samsung_pinctrl_get_soc_data(struct samsung_pinctrl_drv_data *d,
970 struct platform_device *pdev)
Thomas Abraham30574f02012-09-07 06:07:19 +0900971{
972 int id;
973 const struct of_device_id *match;
Tomasz Figa6defe9a2012-10-11 10:11:14 +0200974 struct device_node *node = pdev->dev.of_node;
Tomasz Figaab663782012-10-11 10:11:13 +0200975 struct device_node *np;
Tomasz Figa1bf00d72014-09-23 21:05:40 +0200976 const struct samsung_pin_ctrl *ctrl;
Tomasz Figa40ba6222012-10-11 10:11:09 +0200977 struct samsung_pin_bank *bank;
978 int i;
Thomas Abraham30574f02012-09-07 06:07:19 +0900979
Tomasz Figa6defe9a2012-10-11 10:11:14 +0200980 id = of_alias_get_id(node, "pinctrl");
Thomas Abraham30574f02012-09-07 06:07:19 +0900981 if (id < 0) {
982 dev_err(&pdev->dev, "failed to get alias id\n");
Tomasz Figa87993272014-09-23 21:05:37 +0200983 return ERR_PTR(-ENOENT);
Thomas Abraham30574f02012-09-07 06:07:19 +0900984 }
985 match = of_match_node(samsung_pinctrl_dt_match, node);
Tomasz Figa40ba6222012-10-11 10:11:09 +0200986 ctrl = (struct samsung_pin_ctrl *)match->data + id;
987
Tomasz Figa1bf00d72014-09-23 21:05:40 +0200988 d->suspend = ctrl->suspend;
989 d->resume = ctrl->resume;
990 d->pin_banks = ctrl->pin_banks;
991 d->nr_banks = ctrl->nr_banks;
992
993 bank = d->pin_banks;
994 for (i = 0; i < d->nr_banks; ++i, ++bank) {
Tomasz Figa19846952013-03-18 22:31:50 +0100995 spin_lock_init(&bank->slock);
Tomasz Figa6defe9a2012-10-11 10:11:14 +0200996 bank->drvdata = d;
Tomasz Figa1bf00d72014-09-23 21:05:40 +0200997 bank->pin_base = d->nr_pins;
998 d->nr_pins += bank->nr_pins;
Tomasz Figa40ba6222012-10-11 10:11:09 +0200999 }
1000
Tomasz Figaab663782012-10-11 10:11:13 +02001001 for_each_child_of_node(node, np) {
1002 if (!of_find_property(np, "gpio-controller", NULL))
1003 continue;
Tomasz Figa1bf00d72014-09-23 21:05:40 +02001004 bank = d->pin_banks;
1005 for (i = 0; i < d->nr_banks; ++i, ++bank) {
Tomasz Figaab663782012-10-11 10:11:13 +02001006 if (!strcmp(bank->name, np->name)) {
1007 bank->of_node = np;
1008 break;
1009 }
1010 }
1011 }
1012
Tomasz Figa1bf00d72014-09-23 21:05:40 +02001013 d->pin_base = pin_base;
1014 pin_base += d->nr_pins;
Tomasz Figa40ba6222012-10-11 10:11:09 +02001015
1016 return ctrl;
Thomas Abraham30574f02012-09-07 06:07:19 +09001017}
1018
Greg Kroah-Hartman150632b2012-12-21 13:10:23 -08001019static int samsung_pinctrl_probe(struct platform_device *pdev)
Thomas Abraham30574f02012-09-07 06:07:19 +09001020{
1021 struct samsung_pinctrl_drv_data *drvdata;
Tomasz Figa1bf00d72014-09-23 21:05:40 +02001022 const struct samsung_pin_ctrl *ctrl;
Thomas Abraham30574f02012-09-07 06:07:19 +09001023 struct device *dev = &pdev->dev;
Thomas Abraham30574f02012-09-07 06:07:19 +09001024 struct resource *res;
1025 int ret;
1026
1027 if (!dev->of_node) {
1028 dev_err(dev, "device tree node not found\n");
1029 return -ENODEV;
1030 }
1031
Thomas Abraham30574f02012-09-07 06:07:19 +09001032 drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
1033 if (!drvdata) {
1034 dev_err(dev, "failed to allocate memory for driver's "
1035 "private data\n");
1036 return -ENOMEM;
1037 }
Tomasz Figa6defe9a2012-10-11 10:11:14 +02001038
1039 ctrl = samsung_pinctrl_get_soc_data(drvdata, pdev);
Tomasz Figa87993272014-09-23 21:05:37 +02001040 if (IS_ERR(ctrl)) {
Tomasz Figa6defe9a2012-10-11 10:11:14 +02001041 dev_err(&pdev->dev, "driver data not available\n");
Tomasz Figa87993272014-09-23 21:05:37 +02001042 return PTR_ERR(ctrl);
Tomasz Figa6defe9a2012-10-11 10:11:14 +02001043 }
Thomas Abraham30574f02012-09-07 06:07:19 +09001044 drvdata->dev = dev;
1045
1046 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thierry Reding9e0c1fb2013-01-21 11:09:14 +01001047 drvdata->virt_base = devm_ioremap_resource(&pdev->dev, res);
1048 if (IS_ERR(drvdata->virt_base))
1049 return PTR_ERR(drvdata->virt_base);
Thomas Abraham30574f02012-09-07 06:07:19 +09001050
1051 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1052 if (res)
1053 drvdata->irq = res->start;
1054
1055 ret = samsung_gpiolib_register(pdev, drvdata);
1056 if (ret)
1057 return ret;
1058
1059 ret = samsung_pinctrl_register(pdev, drvdata);
1060 if (ret) {
1061 samsung_gpiolib_unregister(pdev, drvdata);
1062 return ret;
1063 }
1064
1065 if (ctrl->eint_gpio_init)
1066 ctrl->eint_gpio_init(drvdata);
1067 if (ctrl->eint_wkup_init)
1068 ctrl->eint_wkup_init(drvdata);
1069
1070 platform_set_drvdata(pdev, drvdata);
Doug Andersond9f99862013-05-16 21:33:18 -07001071
1072 /* Add to the global list */
1073 list_add_tail(&drvdata->node, &drvdata_list);
1074
Thomas Abraham30574f02012-09-07 06:07:19 +09001075 return 0;
1076}
1077
Doug Andersond9f99862013-05-16 21:33:18 -07001078#ifdef CONFIG_PM
1079
1080/**
1081 * samsung_pinctrl_suspend_dev - save pinctrl state for suspend for a device
1082 *
1083 * Save data for all banks handled by this device.
1084 */
1085static void samsung_pinctrl_suspend_dev(
1086 struct samsung_pinctrl_drv_data *drvdata)
1087{
Doug Andersond9f99862013-05-16 21:33:18 -07001088 void __iomem *virt_base = drvdata->virt_base;
1089 int i;
1090
Tomasz Figa1bf00d72014-09-23 21:05:40 +02001091 for (i = 0; i < drvdata->nr_banks; i++) {
1092 struct samsung_pin_bank *bank = &drvdata->pin_banks[i];
Doug Andersond9f99862013-05-16 21:33:18 -07001093 void __iomem *reg = virt_base + bank->pctl_offset;
Tomasz Figa94ce9442014-09-23 21:05:39 +02001094 const u8 *offs = bank->type->reg_offset;
1095 const u8 *widths = bank->type->fld_width;
Doug Andersond9f99862013-05-16 21:33:18 -07001096 enum pincfg_type type;
1097
1098 /* Registers without a powerdown config aren't lost */
1099 if (!widths[PINCFG_TYPE_CON_PDN])
1100 continue;
1101
1102 for (type = 0; type < PINCFG_TYPE_NUM; type++)
1103 if (widths[type])
1104 bank->pm_save[type] = readl(reg + offs[type]);
1105
1106 if (widths[PINCFG_TYPE_FUNC] * bank->nr_pins > 32) {
1107 /* Some banks have two config registers */
1108 bank->pm_save[PINCFG_TYPE_NUM] =
1109 readl(reg + offs[PINCFG_TYPE_FUNC] + 4);
1110 pr_debug("Save %s @ %p (con %#010x %08x)\n",
1111 bank->name, reg,
1112 bank->pm_save[PINCFG_TYPE_FUNC],
1113 bank->pm_save[PINCFG_TYPE_NUM]);
1114 } else {
1115 pr_debug("Save %s @ %p (con %#010x)\n", bank->name,
1116 reg, bank->pm_save[PINCFG_TYPE_FUNC]);
1117 }
1118 }
Tomasz Figa21c21992013-05-17 18:24:30 +02001119
Tomasz Figa1bf00d72014-09-23 21:05:40 +02001120 if (drvdata->suspend)
1121 drvdata->suspend(drvdata);
Doug Andersond9f99862013-05-16 21:33:18 -07001122}
1123
1124/**
1125 * samsung_pinctrl_resume_dev - restore pinctrl state from suspend for a device
1126 *
1127 * Restore one of the banks that was saved during suspend.
1128 *
1129 * We don't bother doing anything complicated to avoid glitching lines since
1130 * we're called before pad retention is turned off.
1131 */
1132static void samsung_pinctrl_resume_dev(struct samsung_pinctrl_drv_data *drvdata)
1133{
Doug Andersond9f99862013-05-16 21:33:18 -07001134 void __iomem *virt_base = drvdata->virt_base;
1135 int i;
1136
Tomasz Figa1bf00d72014-09-23 21:05:40 +02001137 if (drvdata->resume)
1138 drvdata->resume(drvdata);
Tomasz Figa21c21992013-05-17 18:24:30 +02001139
Tomasz Figa1bf00d72014-09-23 21:05:40 +02001140 for (i = 0; i < drvdata->nr_banks; i++) {
1141 struct samsung_pin_bank *bank = &drvdata->pin_banks[i];
Doug Andersond9f99862013-05-16 21:33:18 -07001142 void __iomem *reg = virt_base + bank->pctl_offset;
Tomasz Figa94ce9442014-09-23 21:05:39 +02001143 const u8 *offs = bank->type->reg_offset;
1144 const u8 *widths = bank->type->fld_width;
Doug Andersond9f99862013-05-16 21:33:18 -07001145 enum pincfg_type type;
1146
1147 /* Registers without a powerdown config aren't lost */
1148 if (!widths[PINCFG_TYPE_CON_PDN])
1149 continue;
1150
1151 if (widths[PINCFG_TYPE_FUNC] * bank->nr_pins > 32) {
1152 /* Some banks have two config registers */
1153 pr_debug("%s @ %p (con %#010x %08x => %#010x %08x)\n",
1154 bank->name, reg,
1155 readl(reg + offs[PINCFG_TYPE_FUNC]),
1156 readl(reg + offs[PINCFG_TYPE_FUNC] + 4),
1157 bank->pm_save[PINCFG_TYPE_FUNC],
1158 bank->pm_save[PINCFG_TYPE_NUM]);
1159 writel(bank->pm_save[PINCFG_TYPE_NUM],
1160 reg + offs[PINCFG_TYPE_FUNC] + 4);
1161 } else {
1162 pr_debug("%s @ %p (con %#010x => %#010x)\n", bank->name,
1163 reg, readl(reg + offs[PINCFG_TYPE_FUNC]),
1164 bank->pm_save[PINCFG_TYPE_FUNC]);
1165 }
1166 for (type = 0; type < PINCFG_TYPE_NUM; type++)
1167 if (widths[type])
1168 writel(bank->pm_save[type], reg + offs[type]);
1169 }
1170}
1171
1172/**
1173 * samsung_pinctrl_suspend - save pinctrl state for suspend
1174 *
1175 * Save data for all banks across all devices.
1176 */
1177static int samsung_pinctrl_suspend(void)
1178{
1179 struct samsung_pinctrl_drv_data *drvdata;
1180
1181 list_for_each_entry(drvdata, &drvdata_list, node) {
1182 samsung_pinctrl_suspend_dev(drvdata);
1183 }
1184
1185 return 0;
1186}
1187
1188/**
1189 * samsung_pinctrl_resume - restore pinctrl state for suspend
1190 *
1191 * Restore data for all banks across all devices.
1192 */
1193static void samsung_pinctrl_resume(void)
1194{
1195 struct samsung_pinctrl_drv_data *drvdata;
1196
1197 list_for_each_entry_reverse(drvdata, &drvdata_list, node) {
1198 samsung_pinctrl_resume_dev(drvdata);
1199 }
1200}
1201
1202#else
1203#define samsung_pinctrl_suspend NULL
1204#define samsung_pinctrl_resume NULL
1205#endif
1206
1207static struct syscore_ops samsung_pinctrl_syscore_ops = {
1208 .suspend = samsung_pinctrl_suspend,
1209 .resume = samsung_pinctrl_resume,
1210};
1211
Thomas Abraham30574f02012-09-07 06:07:19 +09001212static const struct of_device_id samsung_pinctrl_dt_match[] = {
Tomasz Figad5517be2013-03-18 22:31:51 +01001213#ifdef CONFIG_PINCTRL_EXYNOS
Tomasz Figad97f5b92014-04-14 10:45:47 +09001214 { .compatible = "samsung,exynos3250-pinctrl",
1215 .data = (void *)exynos3250_pin_ctrl },
Kukjin Kimb533c862013-01-02 16:05:42 -08001216 { .compatible = "samsung,exynos4210-pinctrl",
Thomas Abraham30574f02012-09-07 06:07:19 +09001217 .data = (void *)exynos4210_pin_ctrl },
Kukjin Kimb533c862013-01-02 16:05:42 -08001218 { .compatible = "samsung,exynos4x12-pinctrl",
Tomasz Figa6edc7942012-11-07 08:44:59 +09001219 .data = (void *)exynos4x12_pin_ctrl },
Thomas Abrahamf67faf42012-12-28 10:37:27 -08001220 { .compatible = "samsung,exynos5250-pinctrl",
1221 .data = (void *)exynos5250_pin_ctrl },
Young-Gun Jang9a8b6072014-02-05 11:51:28 +05301222 { .compatible = "samsung,exynos5260-pinctrl",
1223 .data = (void *)exynos5260_pin_ctrl },
Leela Krishna Amudala983dbeb2013-06-19 22:16:26 +09001224 { .compatible = "samsung,exynos5420-pinctrl",
1225 .data = (void *)exynos5420_pin_ctrl },
Mateusz Krawczuk608a26a2013-08-27 15:08:10 +02001226 { .compatible = "samsung,s5pv210-pinctrl",
1227 .data = (void *)s5pv210_pin_ctrl },
Tomasz Figad5517be2013-03-18 22:31:51 +01001228#endif
Tomasz Figa61dd7262013-03-18 22:31:55 +01001229#ifdef CONFIG_PINCTRL_S3C64XX
1230 { .compatible = "samsung,s3c64xx-pinctrl",
1231 .data = s3c64xx_pin_ctrl },
1232#endif
Heiko Stuebneraf99a752013-05-21 00:56:13 +09001233#ifdef CONFIG_PINCTRL_S3C24XX
1234 { .compatible = "samsung,s3c2412-pinctrl",
1235 .data = s3c2412_pin_ctrl },
1236 { .compatible = "samsung,s3c2416-pinctrl",
1237 .data = s3c2416_pin_ctrl },
1238 { .compatible = "samsung,s3c2440-pinctrl",
1239 .data = s3c2440_pin_ctrl },
1240 { .compatible = "samsung,s3c2450-pinctrl",
1241 .data = s3c2450_pin_ctrl },
1242#endif
Thomas Abraham30574f02012-09-07 06:07:19 +09001243 {},
1244};
1245MODULE_DEVICE_TABLE(of, samsung_pinctrl_dt_match);
1246
1247static struct platform_driver samsung_pinctrl_driver = {
1248 .probe = samsung_pinctrl_probe,
1249 .driver = {
1250 .name = "samsung-pinctrl",
1251 .owner = THIS_MODULE,
Sachin Kamat606fca92013-09-28 17:38:48 +05301252 .of_match_table = samsung_pinctrl_dt_match,
Thomas Abraham30574f02012-09-07 06:07:19 +09001253 },
1254};
1255
1256static int __init samsung_pinctrl_drv_register(void)
1257{
Doug Andersond9f99862013-05-16 21:33:18 -07001258 /*
1259 * Register syscore ops for save/restore of registers across suspend.
1260 * It's important to ensure that this driver is running at an earlier
1261 * initcall level than any arch-specific init calls that install syscore
1262 * ops that turn off pad retention (like exynos_pm_resume).
1263 */
1264 register_syscore_ops(&samsung_pinctrl_syscore_ops);
1265
Thomas Abraham30574f02012-09-07 06:07:19 +09001266 return platform_driver_register(&samsung_pinctrl_driver);
1267}
1268postcore_initcall(samsung_pinctrl_drv_register);
1269
1270static void __exit samsung_pinctrl_drv_unregister(void)
1271{
1272 platform_driver_unregister(&samsung_pinctrl_driver);
1273}
1274module_exit(samsung_pinctrl_drv_unregister);
1275
1276MODULE_AUTHOR("Thomas Abraham <thomas.ab@samsung.com>");
1277MODULE_DESCRIPTION("Samsung pinctrl driver");
1278MODULE_LICENSE("GPL v2");