Skip to content

Commit aca429f

Browse files
rfvirgilLee Jones
authored andcommitted
gpio: madera: Support Cirrus Logic Madera class codecs
This adds support for the GPIOs on Cirrus Logic Madera class codecs. Any pins not used for special functions (see the pinctrl driver) can be used as general single-bit input or output lines. The number of available GPIOs varies between codecs. Note that this is part of a composite MFD for these codecs and can only be used with the corresponding MFD and other child drivers on those silicon. The GPIO block on these codecs does not exist indepedently of the rest of the MFD. Signed-off-by: Nariman Poushin <[email protected]> Signed-off-by: Richard Fitzgerald <[email protected]> Signed-off-by: Charles Keepax <[email protected]> Acked-by: Linus Walleij <[email protected]> Signed-off-by: Lee Jones <[email protected]>
1 parent 218d72a commit aca429f

File tree

4 files changed

+214
-0
lines changed

4 files changed

+214
-0
lines changed

MAINTAINERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3490,6 +3490,7 @@ S: Supported
34903490
F: Documentation/devicetree/bindings/mfd/madera.txt
34913491
F: Documentation/devicetree/bindings/pinctrl/cirrus,madera-pinctrl.txt
34923492
F: include/linux/mfd/madera/*
3493+
F: drivers/gpio/gpio-madera*
34933494
F: drivers/mfd/madera*
34943495
F: drivers/mfd/cs47l*
34953496
F: drivers/pinctrl/cirrus/*

drivers/gpio/Kconfig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,6 +1027,12 @@ config GPIO_LP87565
10271027
This driver can also be built as a module. If so, the module will be
10281028
called gpio-lp87565.
10291029

1030+
config GPIO_MADERA
1031+
tristate "Cirrus Logic Madera class codecs"
1032+
depends on PINCTRL_MADERA
1033+
help
1034+
Support for GPIOs on Cirrus Logic Madera class codecs.
1035+
10301036
config GPIO_MAX77620
10311037
tristate "GPIO support for PMIC MAX77620 and MAX20024"
10321038
depends on MFD_MAX77620

drivers/gpio/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ obj-$(CONFIG_ARCH_LPC32XX) += gpio-lpc32xx.o
7171
obj-$(CONFIG_GPIO_LP873X) += gpio-lp873x.o
7272
obj-$(CONFIG_GPIO_LP87565) += gpio-lp87565.o
7373
obj-$(CONFIG_GPIO_LYNXPOINT) += gpio-lynxpoint.o
74+
obj-$(CONFIG_GPIO_MADERA) += gpio-madera.o
7475
obj-$(CONFIG_GPIO_MAX3191X) += gpio-max3191x.o
7576
obj-$(CONFIG_GPIO_MAX730X) += gpio-max730x.o
7677
obj-$(CONFIG_GPIO_MAX7300) += gpio-max7300.o

drivers/gpio/gpio-madera.c

Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/*
3+
* GPIO support for Cirrus Logic Madera codecs
4+
*
5+
* Copyright (C) 2015-2018 Cirrus Logic
6+
*
7+
* This program is free software; you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by the
9+
* Free Software Foundation; version 2.
10+
*/
11+
12+
#include <linux/gpio/driver.h>
13+
#include <linux/kernel.h>
14+
#include <linux/module.h>
15+
#include <linux/platform_device.h>
16+
17+
#include <linux/mfd/madera/core.h>
18+
#include <linux/mfd/madera/pdata.h>
19+
#include <linux/mfd/madera/registers.h>
20+
21+
struct madera_gpio {
22+
struct madera *madera;
23+
/* storage space for the gpio_chip we're using */
24+
struct gpio_chip gpio_chip;
25+
};
26+
27+
static int madera_gpio_get_direction(struct gpio_chip *chip,
28+
unsigned int offset)
29+
{
30+
struct madera_gpio *madera_gpio = gpiochip_get_data(chip);
31+
struct madera *madera = madera_gpio->madera;
32+
unsigned int reg_offset = 2 * offset;
33+
unsigned int val;
34+
int ret;
35+
36+
ret = regmap_read(madera->regmap, MADERA_GPIO1_CTRL_2 + reg_offset,
37+
&val);
38+
if (ret < 0)
39+
return ret;
40+
41+
return !!(val & MADERA_GP1_DIR_MASK);
42+
}
43+
44+
static int madera_gpio_direction_in(struct gpio_chip *chip, unsigned int offset)
45+
{
46+
struct madera_gpio *madera_gpio = gpiochip_get_data(chip);
47+
struct madera *madera = madera_gpio->madera;
48+
unsigned int reg_offset = 2 * offset;
49+
50+
return regmap_update_bits(madera->regmap,
51+
MADERA_GPIO1_CTRL_2 + reg_offset,
52+
MADERA_GP1_DIR_MASK, MADERA_GP1_DIR);
53+
}
54+
55+
static int madera_gpio_get(struct gpio_chip *chip, unsigned int offset)
56+
{
57+
struct madera_gpio *madera_gpio = gpiochip_get_data(chip);
58+
struct madera *madera = madera_gpio->madera;
59+
unsigned int reg_offset = 2 * offset;
60+
unsigned int val;
61+
int ret;
62+
63+
ret = regmap_read(madera->regmap, MADERA_GPIO1_CTRL_1 + reg_offset,
64+
&val);
65+
if (ret < 0)
66+
return ret;
67+
68+
return !!(val & MADERA_GP1_LVL_MASK);
69+
}
70+
71+
static int madera_gpio_direction_out(struct gpio_chip *chip,
72+
unsigned int offset, int value)
73+
{
74+
struct madera_gpio *madera_gpio = gpiochip_get_data(chip);
75+
struct madera *madera = madera_gpio->madera;
76+
unsigned int reg_offset = 2 * offset;
77+
unsigned int reg_val = value ? MADERA_GP1_LVL : 0;
78+
int ret;
79+
80+
ret = regmap_update_bits(madera->regmap,
81+
MADERA_GPIO1_CTRL_2 + reg_offset,
82+
MADERA_GP1_DIR_MASK, 0);
83+
if (ret < 0)
84+
return ret;
85+
86+
return regmap_update_bits(madera->regmap,
87+
MADERA_GPIO1_CTRL_1 + reg_offset,
88+
MADERA_GP1_LVL_MASK, reg_val);
89+
}
90+
91+
static void madera_gpio_set(struct gpio_chip *chip, unsigned int offset,
92+
int value)
93+
{
94+
struct madera_gpio *madera_gpio = gpiochip_get_data(chip);
95+
struct madera *madera = madera_gpio->madera;
96+
unsigned int reg_offset = 2 * offset;
97+
unsigned int reg_val = value ? MADERA_GP1_LVL : 0;
98+
int ret;
99+
100+
ret = regmap_update_bits(madera->regmap,
101+
MADERA_GPIO1_CTRL_1 + reg_offset,
102+
MADERA_GP1_LVL_MASK, reg_val);
103+
104+
/* set() doesn't return an error so log a warning */
105+
if (ret)
106+
dev_warn(madera->dev, "Failed to write to 0x%x (%d)\n",
107+
MADERA_GPIO1_CTRL_1 + reg_offset, ret);
108+
}
109+
110+
static struct gpio_chip madera_gpio_chip = {
111+
.label = "madera",
112+
.owner = THIS_MODULE,
113+
.request = gpiochip_generic_request,
114+
.free = gpiochip_generic_free,
115+
.get_direction = madera_gpio_get_direction,
116+
.direction_input = madera_gpio_direction_in,
117+
.get = madera_gpio_get,
118+
.direction_output = madera_gpio_direction_out,
119+
.set = madera_gpio_set,
120+
.set_config = gpiochip_generic_config,
121+
.can_sleep = true,
122+
};
123+
124+
static int madera_gpio_probe(struct platform_device *pdev)
125+
{
126+
struct madera *madera = dev_get_drvdata(pdev->dev.parent);
127+
struct madera_pdata *pdata = dev_get_platdata(madera->dev);
128+
struct madera_gpio *madera_gpio;
129+
int ret;
130+
131+
madera_gpio = devm_kzalloc(&pdev->dev, sizeof(*madera_gpio),
132+
GFP_KERNEL);
133+
if (!madera_gpio)
134+
return -ENOMEM;
135+
136+
madera_gpio->madera = madera;
137+
138+
/* Construct suitable gpio_chip from the template in madera_gpio_chip */
139+
madera_gpio->gpio_chip = madera_gpio_chip;
140+
madera_gpio->gpio_chip.parent = pdev->dev.parent;
141+
142+
switch (madera->type) {
143+
case CS47L35:
144+
madera_gpio->gpio_chip.ngpio = CS47L35_NUM_GPIOS;
145+
break;
146+
case CS47L85:
147+
case WM1840:
148+
madera_gpio->gpio_chip.ngpio = CS47L85_NUM_GPIOS;
149+
break;
150+
case CS47L90:
151+
case CS47L91:
152+
madera_gpio->gpio_chip.ngpio = CS47L90_NUM_GPIOS;
153+
break;
154+
default:
155+
dev_err(&pdev->dev, "Unknown chip variant %d\n", madera->type);
156+
return -EINVAL;
157+
}
158+
159+
/* We want to be usable on systems that don't use devicetree or acpi */
160+
if (pdata && pdata->gpio_base)
161+
madera_gpio->gpio_chip.base = pdata->gpio_base;
162+
else
163+
madera_gpio->gpio_chip.base = -1;
164+
165+
ret = devm_gpiochip_add_data(&pdev->dev,
166+
&madera_gpio->gpio_chip,
167+
madera_gpio);
168+
if (ret < 0) {
169+
dev_dbg(&pdev->dev, "Could not register gpiochip, %d\n", ret);
170+
return ret;
171+
}
172+
173+
/*
174+
* This is part of a composite MFD device which can only be used with
175+
* the corresponding pinctrl driver. On all supported silicon the GPIO
176+
* to pinctrl mapping is fixed in the silicon, so we register it
177+
* explicitly instead of requiring a redundant gpio-ranges in the
178+
* devicetree.
179+
* In any case we also want to work on systems that don't use devicetree
180+
* or acpi.
181+
*/
182+
ret = gpiochip_add_pin_range(&madera_gpio->gpio_chip, "madera-pinctrl",
183+
0, 0, madera_gpio->gpio_chip.ngpio);
184+
if (ret) {
185+
dev_dbg(&pdev->dev, "Failed to add pin range (%d)\n", ret);
186+
return ret;
187+
}
188+
189+
return 0;
190+
}
191+
192+
static struct platform_driver madera_gpio_driver = {
193+
.driver = {
194+
.name = "madera-gpio",
195+
},
196+
.probe = madera_gpio_probe,
197+
};
198+
199+
module_platform_driver(madera_gpio_driver);
200+
201+
MODULE_SOFTDEP("pre: pinctrl-madera");
202+
MODULE_DESCRIPTION("GPIO interface for Madera codecs");
203+
MODULE_AUTHOR("Nariman Poushin <[email protected]>");
204+
MODULE_AUTHOR("Richard Fitzgerald <[email protected]>");
205+
MODULE_LICENSE("GPL v2");
206+
MODULE_ALIAS("platform:madera-gpio");

0 commit comments

Comments
 (0)