1 // SPDX-License-Identifier: GPL-2.0
3 * GPIO support for Cirrus Logic Madera codecs
5 * Copyright (C) 2015-2018 Cirrus Logic
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.
12 #include <linux/gpio/driver.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/platform_device.h>
17 #include <linux/mfd/madera/core.h>
18 #include <linux/mfd/madera/pdata.h>
19 #include <linux/mfd/madera/registers.h>
22 struct madera
*madera
;
23 /* storage space for the gpio_chip we're using */
24 struct gpio_chip gpio_chip
;
27 static int madera_gpio_get_direction(struct gpio_chip
*chip
,
30 struct madera_gpio
*madera_gpio
= gpiochip_get_data(chip
);
31 struct madera
*madera
= madera_gpio
->madera
;
32 unsigned int reg_offset
= 2 * offset
;
36 ret
= regmap_read(madera
->regmap
, MADERA_GPIO1_CTRL_2
+ reg_offset
,
41 return !!(val
& MADERA_GP1_DIR_MASK
);
44 static int madera_gpio_direction_in(struct gpio_chip
*chip
, unsigned int offset
)
46 struct madera_gpio
*madera_gpio
= gpiochip_get_data(chip
);
47 struct madera
*madera
= madera_gpio
->madera
;
48 unsigned int reg_offset
= 2 * offset
;
50 return regmap_update_bits(madera
->regmap
,
51 MADERA_GPIO1_CTRL_2
+ reg_offset
,
52 MADERA_GP1_DIR_MASK
, MADERA_GP1_DIR
);
55 static int madera_gpio_get(struct gpio_chip
*chip
, unsigned int offset
)
57 struct madera_gpio
*madera_gpio
= gpiochip_get_data(chip
);
58 struct madera
*madera
= madera_gpio
->madera
;
59 unsigned int reg_offset
= 2 * offset
;
63 ret
= regmap_read(madera
->regmap
, MADERA_GPIO1_CTRL_1
+ reg_offset
,
68 return !!(val
& MADERA_GP1_LVL_MASK
);
71 static int madera_gpio_direction_out(struct gpio_chip
*chip
,
72 unsigned int offset
, int value
)
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;
80 ret
= regmap_update_bits(madera
->regmap
,
81 MADERA_GPIO1_CTRL_2
+ reg_offset
,
82 MADERA_GP1_DIR_MASK
, 0);
86 return regmap_update_bits(madera
->regmap
,
87 MADERA_GPIO1_CTRL_1
+ reg_offset
,
88 MADERA_GP1_LVL_MASK
, reg_val
);
91 static void madera_gpio_set(struct gpio_chip
*chip
, unsigned int offset
,
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;
100 ret
= regmap_update_bits(madera
->regmap
,
101 MADERA_GPIO1_CTRL_1
+ reg_offset
,
102 MADERA_GP1_LVL_MASK
, reg_val
);
104 /* set() doesn't return an error so log a warning */
106 dev_warn(madera
->dev
, "Failed to write to 0x%x (%d)\n",
107 MADERA_GPIO1_CTRL_1
+ reg_offset
, ret
);
110 static struct gpio_chip madera_gpio_chip
= {
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
,
124 static int madera_gpio_probe(struct platform_device
*pdev
)
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
;
131 madera_gpio
= devm_kzalloc(&pdev
->dev
, sizeof(*madera_gpio
),
136 madera_gpio
->madera
= madera
;
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
;
142 switch (madera
->type
) {
144 madera_gpio
->gpio_chip
.ngpio
= CS47L35_NUM_GPIOS
;
148 madera_gpio
->gpio_chip
.ngpio
= CS47L85_NUM_GPIOS
;
152 madera_gpio
->gpio_chip
.ngpio
= CS47L90_NUM_GPIOS
;
155 dev_err(&pdev
->dev
, "Unknown chip variant %d\n", madera
->type
);
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
;
163 madera_gpio
->gpio_chip
.base
= -1;
165 ret
= devm_gpiochip_add_data(&pdev
->dev
,
166 &madera_gpio
->gpio_chip
,
169 dev_dbg(&pdev
->dev
, "Could not register gpiochip, %d\n", ret
);
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
179 * In any case we also want to work on systems that don't use devicetree
182 ret
= gpiochip_add_pin_range(&madera_gpio
->gpio_chip
, "madera-pinctrl",
183 0, 0, madera_gpio
->gpio_chip
.ngpio
);
185 dev_dbg(&pdev
->dev
, "Failed to add pin range (%d)\n", ret
);
192 static struct platform_driver madera_gpio_driver
= {
194 .name
= "madera-gpio",
196 .probe
= madera_gpio_probe
,
199 module_platform_driver(madera_gpio_driver
);
201 MODULE_SOFTDEP("pre: pinctrl-madera");
202 MODULE_DESCRIPTION("GPIO interface for Madera codecs");
203 MODULE_AUTHOR("Nariman Poushin <nariman@opensource.cirrus.com>");
204 MODULE_AUTHOR("Richard Fitzgerald <rf@opensource.cirrus.com>");
205 MODULE_LICENSE("GPL v2");
206 MODULE_ALIAS("platform:madera-gpio");