Merge remote-tracking branch 'lsk/v3.10/topic/gator' into linux-linaro-lsk
[firefly-linux-kernel-4.4.55.git] / drivers / pinctrl / vt8500 / pinctrl-wmt.c
1 /*
2  * Pinctrl driver for the Wondermedia SoC's
3  *
4  * Copyright (c) 2013 Tony Prisk <linux@prisktech.co.nz>
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms and conditions of the GNU General Public License,
8  * version 2, as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14  */
15
16 #include <linux/err.h>
17 #include <linux/gpio.h>
18 #include <linux/interrupt.h>
19 #include <linux/io.h>
20 #include <linux/irq.h>
21 #include <linux/module.h>
22 #include <linux/of.h>
23 #include <linux/of_irq.h>
24 #include <linux/pinctrl/consumer.h>
25 #include <linux/pinctrl/machine.h>
26 #include <linux/pinctrl/pinconf.h>
27 #include <linux/pinctrl/pinconf-generic.h>
28 #include <linux/pinctrl/pinctrl.h>
29 #include <linux/pinctrl/pinmux.h>
30 #include <linux/platform_device.h>
31 #include <linux/slab.h>
32
33 #include "pinctrl-wmt.h"
34
35 static inline void wmt_setbits(struct wmt_pinctrl_data *data, u32 reg,
36                                  u32 mask)
37 {
38         u32 val;
39
40         val = readl_relaxed(data->base + reg);
41         val |= mask;
42         writel_relaxed(val, data->base + reg);
43 }
44
45 static inline void wmt_clearbits(struct wmt_pinctrl_data *data, u32 reg,
46                                    u32 mask)
47 {
48         u32 val;
49
50         val = readl_relaxed(data->base + reg);
51         val &= ~mask;
52         writel_relaxed(val, data->base + reg);
53 }
54
55 enum wmt_func_sel {
56         WMT_FSEL_GPIO_IN = 0,
57         WMT_FSEL_GPIO_OUT = 1,
58         WMT_FSEL_ALT = 2,
59         WMT_FSEL_COUNT = 3,
60 };
61
62 static const char * const wmt_functions[WMT_FSEL_COUNT] = {
63         [WMT_FSEL_GPIO_IN] = "gpio_in",
64         [WMT_FSEL_GPIO_OUT] = "gpio_out",
65         [WMT_FSEL_ALT] = "alt",
66 };
67
68 static int wmt_pmx_get_functions_count(struct pinctrl_dev *pctldev)
69 {
70         return WMT_FSEL_COUNT;
71 }
72
73 static const char *wmt_pmx_get_function_name(struct pinctrl_dev *pctldev,
74                                              unsigned selector)
75 {
76         return wmt_functions[selector];
77 }
78
79 static int wmt_pmx_get_function_groups(struct pinctrl_dev *pctldev,
80                                        unsigned selector,
81                                        const char * const **groups,
82                                        unsigned * const num_groups)
83 {
84         struct wmt_pinctrl_data *data = pinctrl_dev_get_drvdata(pctldev);
85
86         /* every pin does every function */
87         *groups = data->groups;
88         *num_groups = data->ngroups;
89
90         return 0;
91 }
92
93 static int wmt_set_pinmux(struct wmt_pinctrl_data *data, unsigned func,
94                           unsigned pin)
95 {
96         u32 bank = WMT_BANK_FROM_PIN(pin);
97         u32 bit = WMT_BIT_FROM_PIN(pin);
98         u32 reg_en = data->banks[bank].reg_en;
99         u32 reg_dir = data->banks[bank].reg_dir;
100
101         if (reg_dir == NO_REG) {
102                 dev_err(data->dev, "pin:%d no direction register defined\n",
103                         pin);
104                 return -EINVAL;
105         }
106
107         /*
108          * If reg_en == NO_REG, we assume it is a dedicated GPIO and cannot be
109          * disabled (as on VT8500) and that no alternate function is available.
110          */
111         switch (func) {
112         case WMT_FSEL_GPIO_IN:
113                 if (reg_en != NO_REG)
114                         wmt_setbits(data, reg_en, BIT(bit));
115                 wmt_clearbits(data, reg_dir, BIT(bit));
116                 break;
117         case WMT_FSEL_GPIO_OUT:
118                 if (reg_en != NO_REG)
119                         wmt_setbits(data, reg_en, BIT(bit));
120                 wmt_setbits(data, reg_dir, BIT(bit));
121                 break;
122         case WMT_FSEL_ALT:
123                 if (reg_en == NO_REG) {
124                         dev_err(data->dev, "pin:%d no alt function available\n",
125                                 pin);
126                         return -EINVAL;
127                 }
128                 wmt_clearbits(data, reg_en, BIT(bit));
129         }
130
131         return 0;
132 }
133
134 static int wmt_pmx_enable(struct pinctrl_dev *pctldev,
135                           unsigned func_selector,
136                           unsigned group_selector)
137 {
138         struct wmt_pinctrl_data *data = pinctrl_dev_get_drvdata(pctldev);
139         u32 pinnum = data->pins[group_selector].number;
140
141         return wmt_set_pinmux(data, func_selector, pinnum);
142 }
143
144 static void wmt_pmx_disable(struct pinctrl_dev *pctldev,
145                             unsigned func_selector,
146                             unsigned group_selector)
147 {
148         struct wmt_pinctrl_data *data = pinctrl_dev_get_drvdata(pctldev);
149         u32 pinnum = data->pins[group_selector].number;
150
151         /* disable by setting GPIO_IN */
152         wmt_set_pinmux(data, WMT_FSEL_GPIO_IN, pinnum);
153 }
154
155 static void wmt_pmx_gpio_disable_free(struct pinctrl_dev *pctldev,
156                                       struct pinctrl_gpio_range *range,
157                                       unsigned offset)
158 {
159         struct wmt_pinctrl_data *data = pinctrl_dev_get_drvdata(pctldev);
160
161         /* disable by setting GPIO_IN */
162         wmt_set_pinmux(data, WMT_FSEL_GPIO_IN, offset);
163 }
164
165 static int wmt_pmx_gpio_set_direction(struct pinctrl_dev *pctldev,
166                                       struct pinctrl_gpio_range *range,
167                                       unsigned offset,
168                                       bool input)
169 {
170         struct wmt_pinctrl_data *data = pinctrl_dev_get_drvdata(pctldev);
171
172         wmt_set_pinmux(data, (input ? WMT_FSEL_GPIO_IN : WMT_FSEL_GPIO_OUT),
173                        offset);
174
175         return 0;
176 }
177
178 static struct pinmux_ops wmt_pinmux_ops = {
179         .get_functions_count = wmt_pmx_get_functions_count,
180         .get_function_name = wmt_pmx_get_function_name,
181         .get_function_groups = wmt_pmx_get_function_groups,
182         .enable = wmt_pmx_enable,
183         .disable = wmt_pmx_disable,
184         .gpio_disable_free = wmt_pmx_gpio_disable_free,
185         .gpio_set_direction = wmt_pmx_gpio_set_direction,
186 };
187
188 static int wmt_get_groups_count(struct pinctrl_dev *pctldev)
189 {
190         struct wmt_pinctrl_data *data = pinctrl_dev_get_drvdata(pctldev);
191
192         return data->ngroups;
193 }
194
195 static const char *wmt_get_group_name(struct pinctrl_dev *pctldev,
196                                       unsigned selector)
197 {
198         struct wmt_pinctrl_data *data = pinctrl_dev_get_drvdata(pctldev);
199
200         return data->groups[selector];
201 }
202
203 static int wmt_get_group_pins(struct pinctrl_dev *pctldev,
204                               unsigned selector,
205                               const unsigned **pins,
206                               unsigned *num_pins)
207 {
208         struct wmt_pinctrl_data *data = pinctrl_dev_get_drvdata(pctldev);
209
210         *pins = &data->pins[selector].number;
211         *num_pins = 1;
212
213         return 0;
214 }
215
216 static int wmt_pctl_find_group_by_pin(struct wmt_pinctrl_data *data, u32 pin)
217 {
218         int i;
219
220         for (i = 0; i < data->npins; i++) {
221                 if (data->pins[i].number == pin)
222                         return i;
223         }
224
225         return -EINVAL;
226 }
227
228 static int wmt_pctl_dt_node_to_map_func(struct wmt_pinctrl_data *data,
229                                         struct device_node *np,
230                                         u32 pin, u32 fnum,
231                                         struct pinctrl_map **maps)
232 {
233         int group;
234         struct pinctrl_map *map = *maps;
235
236         if (fnum >= ARRAY_SIZE(wmt_functions)) {
237                 dev_err(data->dev, "invalid wm,function %d\n", fnum);
238                 return -EINVAL;
239         }
240
241         group = wmt_pctl_find_group_by_pin(data, pin);
242         if (group < 0) {
243                 dev_err(data->dev, "unable to match pin %d to group\n", pin);
244                 return group;
245         }
246
247         map->type = PIN_MAP_TYPE_MUX_GROUP;
248         map->data.mux.group = data->groups[group];
249         map->data.mux.function = wmt_functions[fnum];
250         (*maps)++;
251
252         return 0;
253 }
254
255 static int wmt_pctl_dt_node_to_map_pull(struct wmt_pinctrl_data *data,
256                                         struct device_node *np,
257                                         u32 pin, u32 pull,
258                                         struct pinctrl_map **maps)
259 {
260         int group;
261         unsigned long *configs;
262         struct pinctrl_map *map = *maps;
263
264         if (pull > 2) {
265                 dev_err(data->dev, "invalid wm,pull %d\n", pull);
266                 return -EINVAL;
267         }
268
269         group = wmt_pctl_find_group_by_pin(data, pin);
270         if (group < 0) {
271                 dev_err(data->dev, "unable to match pin %d to group\n", pin);
272                 return group;
273         }
274
275         configs = kzalloc(sizeof(*configs), GFP_KERNEL);
276         if (!configs)
277                 return -ENOMEM;
278
279         switch (pull) {
280         case 0:
281                 configs[0] = PIN_CONFIG_BIAS_DISABLE;
282                 break;
283         case 1:
284                 configs[0] = PIN_CONFIG_BIAS_PULL_DOWN;
285                 break;
286         case 2:
287                 configs[0] = PIN_CONFIG_BIAS_PULL_UP;
288                 break;
289         default:
290                 configs[0] = PIN_CONFIG_BIAS_DISABLE;
291                 dev_err(data->dev, "invalid pull state %d - disabling\n", pull);
292         }
293
294         map->type = PIN_MAP_TYPE_CONFIGS_PIN;
295         map->data.configs.group_or_pin = data->groups[group];
296         map->data.configs.configs = configs;
297         map->data.configs.num_configs = 1;
298         (*maps)++;
299
300         return 0;
301 }
302
303 static void wmt_pctl_dt_free_map(struct pinctrl_dev *pctldev,
304                                  struct pinctrl_map *maps,
305                                  unsigned num_maps)
306 {
307         int i;
308
309         for (i = 0; i < num_maps; i++)
310                 if (maps[i].type == PIN_MAP_TYPE_CONFIGS_PIN)
311                         kfree(maps[i].data.configs.configs);
312
313         kfree(maps);
314 }
315
316 static int wmt_pctl_dt_node_to_map(struct pinctrl_dev *pctldev,
317                                    struct device_node *np,
318                                    struct pinctrl_map **map,
319                                    unsigned *num_maps)
320 {
321         struct pinctrl_map *maps, *cur_map;
322         struct property *pins, *funcs, *pulls;
323         u32 pin, func, pull;
324         int num_pins, num_funcs, num_pulls, maps_per_pin;
325         int i, err;
326         struct wmt_pinctrl_data *data = pinctrl_dev_get_drvdata(pctldev);
327
328         pins = of_find_property(np, "wm,pins", NULL);
329         if (!pins) {
330                 dev_err(data->dev, "missing wmt,pins property\n");
331                 return -EINVAL;
332         }
333
334         funcs = of_find_property(np, "wm,function", NULL);
335         pulls = of_find_property(np, "wm,pull", NULL);
336
337         if (!funcs && !pulls) {
338                 dev_err(data->dev, "neither wm,function nor wm,pull specified\n");
339                 return -EINVAL;
340         }
341
342         /*
343          * The following lines calculate how many values are defined for each
344          * of the properties.
345          */
346         num_pins = pins->length / sizeof(u32);
347         num_funcs = funcs ? (funcs->length / sizeof(u32)) : 0;
348         num_pulls = pulls ? (pulls->length / sizeof(u32)) : 0;
349
350         if (num_funcs > 1 && num_funcs != num_pins) {
351                 dev_err(data->dev, "wm,function must have 1 or %d entries\n",
352                         num_pins);
353                 return -EINVAL;
354         }
355
356         if (num_pulls > 1 && num_pulls != num_pins) {
357                 dev_err(data->dev, "wm,pull must have 1 or %d entries\n",
358                         num_pins);
359                 return -EINVAL;
360         }
361
362         maps_per_pin = 0;
363         if (num_funcs)
364                 maps_per_pin++;
365         if (num_pulls)
366                 maps_per_pin++;
367
368         cur_map = maps = kzalloc(num_pins * maps_per_pin * sizeof(*maps),
369                                  GFP_KERNEL);
370         if (!maps)
371                 return -ENOMEM;
372
373         for (i = 0; i < num_pins; i++) {
374                 err = of_property_read_u32_index(np, "wm,pins", i, &pin);
375                 if (err)
376                         goto fail;
377
378                 if (pin >= (data->nbanks * 32)) {
379                         dev_err(data->dev, "invalid wm,pins value\n");
380                         err = -EINVAL;
381                         goto fail;
382                 }
383
384                 if (num_funcs) {
385                         err = of_property_read_u32_index(np, "wm,function",
386                                                 (num_funcs > 1 ? i : 0), &func);
387                         if (err)
388                                 goto fail;
389
390                         err = wmt_pctl_dt_node_to_map_func(data, np, pin, func,
391                                                            &cur_map);
392                         if (err)
393                                 goto fail;
394                 }
395
396                 if (num_pulls) {
397                         err = of_property_read_u32_index(np, "wm,pull",
398                                                 (num_pulls > 1 ? i : 0), &pull);
399                         if (err)
400                                 goto fail;
401
402                         err = wmt_pctl_dt_node_to_map_pull(data, np, pin, pull,
403                                                            &cur_map);
404                         if (err)
405                                 goto fail;
406                 }
407         }
408         *map = maps;
409         *num_maps = num_pins * maps_per_pin;
410         return 0;
411
412 /*
413  * The fail path removes any maps that have been allocated. The fail path is
414  * only called from code after maps has been kzalloc'd. It is also safe to
415  * pass 'num_pins * maps_per_pin' as the map count even though we probably
416  * failed before all the mappings were read as all maps are allocated at once,
417  * and configs are only allocated for .type = PIN_MAP_TYPE_CONFIGS_PIN - there
418  * is no failpath where a config can be allocated without .type being set.
419  */
420 fail:
421         wmt_pctl_dt_free_map(pctldev, maps, num_pins * maps_per_pin);
422         return err;
423 }
424
425 static struct pinctrl_ops wmt_pctl_ops = {
426         .get_groups_count = wmt_get_groups_count,
427         .get_group_name = wmt_get_group_name,
428         .get_group_pins = wmt_get_group_pins,
429         .dt_node_to_map = wmt_pctl_dt_node_to_map,
430         .dt_free_map = wmt_pctl_dt_free_map,
431 };
432
433 static int wmt_pinconf_get(struct pinctrl_dev *pctldev, unsigned pin,
434                            unsigned long *config)
435 {
436         return -ENOTSUPP;
437 }
438
439 static int wmt_pinconf_set(struct pinctrl_dev *pctldev, unsigned pin,
440                            unsigned long config)
441 {
442         struct wmt_pinctrl_data *data = pinctrl_dev_get_drvdata(pctldev);
443         enum pin_config_param param = pinconf_to_config_param(config);
444         u16 arg = pinconf_to_config_argument(config);
445         u32 bank = WMT_BANK_FROM_PIN(pin);
446         u32 bit = WMT_BIT_FROM_PIN(pin);
447         u32 reg_pull_en = data->banks[bank].reg_pull_en;
448         u32 reg_pull_cfg = data->banks[bank].reg_pull_cfg;
449
450         if ((reg_pull_en == NO_REG) || (reg_pull_cfg == NO_REG)) {
451                 dev_err(data->dev, "bias functions not supported on pin %d\n",
452                         pin);
453                 return -EINVAL;
454         }
455
456         if ((param == PIN_CONFIG_BIAS_PULL_DOWN) ||
457             (param == PIN_CONFIG_BIAS_PULL_UP)) {
458                 if (arg == 0)
459                         param = PIN_CONFIG_BIAS_DISABLE;
460         }
461
462         switch (param) {
463         case PIN_CONFIG_BIAS_DISABLE:
464                 wmt_clearbits(data, reg_pull_en, BIT(bit));
465                 break;
466         case PIN_CONFIG_BIAS_PULL_DOWN:
467                 wmt_clearbits(data, reg_pull_cfg, BIT(bit));
468                 wmt_setbits(data, reg_pull_en, BIT(bit));
469                 break;
470         case PIN_CONFIG_BIAS_PULL_UP:
471                 wmt_setbits(data, reg_pull_cfg, BIT(bit));
472                 wmt_setbits(data, reg_pull_en, BIT(bit));
473                 break;
474         default:
475                 dev_err(data->dev, "unknown pinconf param\n");
476                 return -EINVAL;
477         }
478
479         return 0;
480 }
481
482 static struct pinconf_ops wmt_pinconf_ops = {
483         .pin_config_get = wmt_pinconf_get,
484         .pin_config_set = wmt_pinconf_set,
485 };
486
487 static struct pinctrl_desc wmt_desc = {
488         .owner = THIS_MODULE,
489         .name = "pinctrl-wmt",
490         .pctlops = &wmt_pctl_ops,
491         .pmxops = &wmt_pinmux_ops,
492         .confops = &wmt_pinconf_ops,
493 };
494
495 static int wmt_gpio_request(struct gpio_chip *chip, unsigned offset)
496 {
497         return pinctrl_request_gpio(chip->base + offset);
498 }
499
500 static void wmt_gpio_free(struct gpio_chip *chip, unsigned offset)
501 {
502         pinctrl_free_gpio(chip->base + offset);
503 }
504
505 static int wmt_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
506 {
507         struct wmt_pinctrl_data *data = dev_get_drvdata(chip->dev);
508         u32 bank = WMT_BANK_FROM_PIN(offset);
509         u32 bit = WMT_BIT_FROM_PIN(offset);
510         u32 reg_dir = data->banks[bank].reg_dir;
511         u32 val;
512
513         val = readl_relaxed(data->base + reg_dir);
514         if (val & BIT(bit))
515                 return GPIOF_DIR_OUT;
516         else
517                 return GPIOF_DIR_IN;
518 }
519
520 static int wmt_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
521 {
522         return pinctrl_gpio_direction_input(chip->base + offset);
523 }
524
525 static int wmt_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
526                                      int value)
527 {
528         return pinctrl_gpio_direction_output(chip->base + offset);
529 }
530
531 static int wmt_gpio_get_value(struct gpio_chip *chip, unsigned offset)
532 {
533         struct wmt_pinctrl_data *data = dev_get_drvdata(chip->dev);
534         u32 bank = WMT_BANK_FROM_PIN(offset);
535         u32 bit = WMT_BIT_FROM_PIN(offset);
536         u32 reg_data_in = data->banks[bank].reg_data_in;
537
538         if (reg_data_in == NO_REG) {
539                 dev_err(data->dev, "no data in register defined\n");
540                 return -EINVAL;
541         }
542
543         return !!(readl_relaxed(data->base + reg_data_in) & BIT(bit));
544 }
545
546 static void wmt_gpio_set_value(struct gpio_chip *chip, unsigned offset,
547                                int val)
548 {
549         struct wmt_pinctrl_data *data = dev_get_drvdata(chip->dev);
550         u32 bank = WMT_BANK_FROM_PIN(offset);
551         u32 bit = WMT_BIT_FROM_PIN(offset);
552         u32 reg_data_out = data->banks[bank].reg_data_out;
553
554         if (reg_data_out == NO_REG) {
555                 dev_err(data->dev, "no data out register defined\n");
556                 return;
557         }
558
559         if (val)
560                 wmt_setbits(data, reg_data_out, BIT(bit));
561         else
562                 wmt_clearbits(data, reg_data_out, BIT(bit));
563 }
564
565 static struct gpio_chip wmt_gpio_chip = {
566         .label = "gpio-wmt",
567         .owner = THIS_MODULE,
568         .request = wmt_gpio_request,
569         .free = wmt_gpio_free,
570         .get_direction = wmt_gpio_get_direction,
571         .direction_input = wmt_gpio_direction_input,
572         .direction_output = wmt_gpio_direction_output,
573         .get = wmt_gpio_get_value,
574         .set = wmt_gpio_set_value,
575         .can_sleep = 0,
576 };
577
578 int wmt_pinctrl_probe(struct platform_device *pdev,
579                       struct wmt_pinctrl_data *data)
580 {
581         int err;
582         struct resource *res;
583
584         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
585         data->base = devm_request_and_ioremap(&pdev->dev, res);
586         if (!data->base) {
587                 dev_err(&pdev->dev, "failed to map memory resource\n");
588                 return -EBUSY;
589         }
590
591         wmt_desc.pins = data->pins;
592         wmt_desc.npins = data->npins;
593
594         data->gpio_chip = wmt_gpio_chip;
595         data->gpio_chip.dev = &pdev->dev;
596         data->gpio_chip.of_node = pdev->dev.of_node;
597         data->gpio_chip.ngpio = data->nbanks * 32;
598
599         platform_set_drvdata(pdev, data);
600
601         data->dev = &pdev->dev;
602
603         data->pctl_dev = pinctrl_register(&wmt_desc, &pdev->dev, data);
604         if (!data->pctl_dev) {
605                 dev_err(&pdev->dev, "Failed to register pinctrl\n");
606                 return -EINVAL;
607         }
608
609         err = gpiochip_add(&data->gpio_chip);
610         if (err) {
611                 dev_err(&pdev->dev, "could not add GPIO chip\n");
612                 goto fail_gpio;
613         }
614
615         err = gpiochip_add_pin_range(&data->gpio_chip, dev_name(data->dev),
616                                      0, 0, data->nbanks * 32);
617         if (err)
618                 goto fail_range;
619
620         dev_info(&pdev->dev, "Pin controller initialized\n");
621
622         return 0;
623
624 fail_range:
625         if (gpiochip_remove(&data->gpio_chip))
626                 dev_err(&pdev->dev, "failed to remove gpio chip\n");
627 fail_gpio:
628         pinctrl_unregister(data->pctl_dev);
629         return err;
630 }
631
632 int wmt_pinctrl_remove(struct platform_device *pdev)
633 {
634         struct wmt_pinctrl_data *data = platform_get_drvdata(pdev);
635         int err;
636
637         err = gpiochip_remove(&data->gpio_chip);
638         if (err)
639                 dev_err(&pdev->dev, "failed to remove gpio chip\n");
640
641         pinctrl_unregister(data->pctl_dev);
642
643         return 0;
644 }