2 * gpiolib support for Wolfson Arizona class devices
4 * Copyright 2012 Wolfson Microelectronics PLC.
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
15 #include <linux/kernel.h>
16 #include <linux/slab.h>
17 #include <linux/module.h>
18 #include <linux/gpio.h>
19 #include <linux/platform_device.h>
20 #include <linux/pm_runtime.h>
21 #include <linux/seq_file.h>
23 #include <linux/mfd/arizona/core.h>
24 #include <linux/mfd/arizona/pdata.h>
25 #include <linux/mfd/arizona/registers.h>
28 struct arizona
*arizona
;
29 struct gpio_chip gpio_chip
;
32 static int arizona_gpio_direction_in(struct gpio_chip
*chip
, unsigned offset
)
34 struct arizona_gpio
*arizona_gpio
= gpiochip_get_data(chip
);
35 struct arizona
*arizona
= arizona_gpio
->arizona
;
37 return regmap_update_bits(arizona
->regmap
, ARIZONA_GPIO1_CTRL
+ offset
,
38 ARIZONA_GPN_DIR
, ARIZONA_GPN_DIR
);
41 static int arizona_gpio_get(struct gpio_chip
*chip
, unsigned offset
)
43 struct arizona_gpio
*arizona_gpio
= gpiochip_get_data(chip
);
44 struct arizona
*arizona
= arizona_gpio
->arizona
;
45 unsigned int reg
, val
;
48 reg
= ARIZONA_GPIO1_CTRL
+ offset
;
49 ret
= regmap_read(arizona
->regmap
, reg
, &val
);
53 /* Resume to read actual registers for input pins */
54 if (val
& ARIZONA_GPN_DIR
) {
55 ret
= pm_runtime_get_sync(chip
->parent
);
57 dev_err(chip
->parent
, "Failed to resume: %d\n", ret
);
61 /* Register is cached, drop it to ensure a physical read */
62 ret
= regcache_drop_region(arizona
->regmap
, reg
, reg
);
64 dev_err(chip
->parent
, "Failed to drop cache: %d\n",
69 ret
= regmap_read(arizona
->regmap
, reg
, &val
);
73 pm_runtime_mark_last_busy(chip
->parent
);
74 pm_runtime_put_autosuspend(chip
->parent
);
77 if (val
& ARIZONA_GPN_LVL
)
83 static int arizona_gpio_direction_out(struct gpio_chip
*chip
,
84 unsigned offset
, int value
)
86 struct arizona_gpio
*arizona_gpio
= gpiochip_get_data(chip
);
87 struct arizona
*arizona
= arizona_gpio
->arizona
;
90 value
= ARIZONA_GPN_LVL
;
92 return regmap_update_bits(arizona
->regmap
, ARIZONA_GPIO1_CTRL
+ offset
,
93 ARIZONA_GPN_DIR
| ARIZONA_GPN_LVL
, value
);
96 static void arizona_gpio_set(struct gpio_chip
*chip
, unsigned offset
, int value
)
98 struct arizona_gpio
*arizona_gpio
= gpiochip_get_data(chip
);
99 struct arizona
*arizona
= arizona_gpio
->arizona
;
102 value
= ARIZONA_GPN_LVL
;
104 regmap_update_bits(arizona
->regmap
, ARIZONA_GPIO1_CTRL
+ offset
,
105 ARIZONA_GPN_LVL
, value
);
108 static const struct gpio_chip template_chip
= {
110 .owner
= THIS_MODULE
,
111 .direction_input
= arizona_gpio_direction_in
,
112 .get
= arizona_gpio_get
,
113 .direction_output
= arizona_gpio_direction_out
,
114 .set
= arizona_gpio_set
,
118 static int arizona_gpio_probe(struct platform_device
*pdev
)
120 struct arizona
*arizona
= dev_get_drvdata(pdev
->dev
.parent
);
121 struct arizona_pdata
*pdata
= dev_get_platdata(arizona
->dev
);
122 struct arizona_gpio
*arizona_gpio
;
125 arizona_gpio
= devm_kzalloc(&pdev
->dev
, sizeof(*arizona_gpio
),
130 arizona_gpio
->arizona
= arizona
;
131 arizona_gpio
->gpio_chip
= template_chip
;
132 arizona_gpio
->gpio_chip
.parent
= &pdev
->dev
;
133 #ifdef CONFIG_OF_GPIO
134 arizona_gpio
->gpio_chip
.of_node
= arizona
->dev
->of_node
;
137 switch (arizona
->type
) {
144 arizona_gpio
->gpio_chip
.ngpio
= 5;
148 arizona_gpio
->gpio_chip
.ngpio
= 2;
151 dev_err(&pdev
->dev
, "Unknown chip variant %d\n",
156 if (pdata
&& pdata
->gpio_base
)
157 arizona_gpio
->gpio_chip
.base
= pdata
->gpio_base
;
159 arizona_gpio
->gpio_chip
.base
= -1;
161 ret
= devm_gpiochip_add_data(&pdev
->dev
, &arizona_gpio
->gpio_chip
,
164 dev_err(&pdev
->dev
, "Could not register gpiochip, %d\n",
172 static struct platform_driver arizona_gpio_driver
= {
173 .driver
.name
= "arizona-gpio",
174 .probe
= arizona_gpio_probe
,
177 module_platform_driver(arizona_gpio_driver
);
179 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
180 MODULE_DESCRIPTION("GPIO interface for Arizona devices");
181 MODULE_LICENSE("GPL");
182 MODULE_ALIAS("platform:arizona-gpio");