blob: 60b3102279f37736e3e5e77032deca7354060227 [file] [log] [blame]
Mark Brown31ba56f2012-06-23 13:29:25 +01001/*
2 * gpiolib support for Wolfson Arizona class devices
3 *
4 * Copyright 2012 Wolfson Microelectronics PLC.
5 *
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
7 *
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.
12 *
13 */
14
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>
Charles Keepax11598d12017-04-05 16:50:46 +010020#include <linux/pm_runtime.h>
Mark Brown31ba56f2012-06-23 13:29:25 +010021#include <linux/seq_file.h>
22
23#include <linux/mfd/arizona/core.h>
24#include <linux/mfd/arizona/pdata.h>
25#include <linux/mfd/arizona/registers.h>
26
27struct arizona_gpio {
28 struct arizona *arizona;
29 struct gpio_chip gpio_chip;
30};
31
Mark Brown31ba56f2012-06-23 13:29:25 +010032static int arizona_gpio_direction_in(struct gpio_chip *chip, unsigned offset)
33{
Linus Walleij18992d42015-12-04 15:31:31 +010034 struct arizona_gpio *arizona_gpio = gpiochip_get_data(chip);
Mark Brown31ba56f2012-06-23 13:29:25 +010035 struct arizona *arizona = arizona_gpio->arizona;
36
37 return regmap_update_bits(arizona->regmap, ARIZONA_GPIO1_CTRL + offset,
38 ARIZONA_GPN_DIR, ARIZONA_GPN_DIR);
39}
40
41static int arizona_gpio_get(struct gpio_chip *chip, unsigned offset)
42{
Linus Walleij18992d42015-12-04 15:31:31 +010043 struct arizona_gpio *arizona_gpio = gpiochip_get_data(chip);
Mark Brown31ba56f2012-06-23 13:29:25 +010044 struct arizona *arizona = arizona_gpio->arizona;
Charles Keepax11598d12017-04-05 16:50:46 +010045 unsigned int reg, val;
Mark Brown31ba56f2012-06-23 13:29:25 +010046 int ret;
47
Charles Keepax11598d12017-04-05 16:50:46 +010048 reg = ARIZONA_GPIO1_CTRL + offset;
49 ret = regmap_read(arizona->regmap, reg, &val);
Mark Brown31ba56f2012-06-23 13:29:25 +010050 if (ret < 0)
51 return ret;
52
Charles Keepax11598d12017-04-05 16:50:46 +010053 /* Resume to read actual registers for input pins */
54 if (!(val & ARIZONA_GPN_DIR)) {
55 ret = pm_runtime_get_sync(chip->parent);
56 if (ret < 0) {
57 dev_err(chip->parent, "Failed to resume: %d\n", ret);
58 return ret;
59 }
60
61 /* Register is cached, drop it to ensure a physical read */
62 ret = regcache_drop_region(arizona->regmap, reg, reg);
63 if (ret < 0) {
64 dev_err(chip->parent, "Failed to drop cache: %d\n",
65 ret);
66 return ret;
67 }
68
69 ret = regmap_read(arizona->regmap, reg, &val);
70 if (ret < 0)
71 return ret;
72
73 pm_runtime_mark_last_busy(chip->parent);
74 pm_runtime_put_autosuspend(chip->parent);
75 }
76
Mark Brown31ba56f2012-06-23 13:29:25 +010077 if (val & ARIZONA_GPN_LVL)
78 return 1;
79 else
80 return 0;
81}
82
83static int arizona_gpio_direction_out(struct gpio_chip *chip,
84 unsigned offset, int value)
85{
Linus Walleij18992d42015-12-04 15:31:31 +010086 struct arizona_gpio *arizona_gpio = gpiochip_get_data(chip);
Mark Brown31ba56f2012-06-23 13:29:25 +010087 struct arizona *arizona = arizona_gpio->arizona;
88
89 if (value)
90 value = ARIZONA_GPN_LVL;
91
92 return regmap_update_bits(arizona->regmap, ARIZONA_GPIO1_CTRL + offset,
93 ARIZONA_GPN_DIR | ARIZONA_GPN_LVL, value);
94}
95
96static void arizona_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
97{
Linus Walleij18992d42015-12-04 15:31:31 +010098 struct arizona_gpio *arizona_gpio = gpiochip_get_data(chip);
Mark Brown31ba56f2012-06-23 13:29:25 +010099 struct arizona *arizona = arizona_gpio->arizona;
100
101 if (value)
102 value = ARIZONA_GPN_LVL;
103
104 regmap_update_bits(arizona->regmap, ARIZONA_GPIO1_CTRL + offset,
105 ARIZONA_GPN_LVL, value);
106}
107
Julia Lawalle35b5ab2016-09-11 14:14:37 +0200108static const struct gpio_chip template_chip = {
Mark Brown31ba56f2012-06-23 13:29:25 +0100109 .label = "arizona",
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,
Linus Walleij9fb1f392013-12-04 14:42:46 +0100115 .can_sleep = true,
Mark Brown31ba56f2012-06-23 13:29:25 +0100116};
117
Bill Pemberton38363092012-11-19 13:22:34 -0500118static int arizona_gpio_probe(struct platform_device *pdev)
Mark Brown31ba56f2012-06-23 13:29:25 +0100119{
120 struct arizona *arizona = dev_get_drvdata(pdev->dev.parent);
Jingoo Hane56aee12013-07-30 17:08:05 +0900121 struct arizona_pdata *pdata = dev_get_platdata(arizona->dev);
Mark Brown31ba56f2012-06-23 13:29:25 +0100122 struct arizona_gpio *arizona_gpio;
123 int ret;
124
125 arizona_gpio = devm_kzalloc(&pdev->dev, sizeof(*arizona_gpio),
126 GFP_KERNEL);
Varka Bhadramafeb7b42015-03-31 09:49:11 +0530127 if (!arizona_gpio)
Mark Brown31ba56f2012-06-23 13:29:25 +0100128 return -ENOMEM;
129
130 arizona_gpio->arizona = arizona;
131 arizona_gpio->gpio_chip = template_chip;
Linus Walleij58383c782015-11-04 09:56:26 +0100132 arizona_gpio->gpio_chip.parent = &pdev->dev;
Charles Keepax4bbd6f22013-09-29 21:00:37 +0100133#ifdef CONFIG_OF_GPIO
134 arizona_gpio->gpio_chip.of_node = arizona->dev->of_node;
135#endif
Mark Brown31ba56f2012-06-23 13:29:25 +0100136
137 switch (arizona->type) {
138 case WM5102:
139 case WM5110:
Richard Fitzgerald449c1752015-01-17 15:21:25 +0000140 case WM8280:
Charles Keepaxd9cadcc2013-09-11 14:03:27 +0100141 case WM8997:
Richard Fitzgerald633a5062015-09-10 16:59:01 +0100142 case WM8998:
143 case WM1814:
Mark Brown31ba56f2012-06-23 13:29:25 +0100144 arizona_gpio->gpio_chip.ngpio = 5;
145 break;
Richard Fitzgerald7d07d152015-11-03 15:08:34 +0000146 case WM1831:
147 case CS47L24:
148 arizona_gpio->gpio_chip.ngpio = 2;
149 break;
Mark Brown31ba56f2012-06-23 13:29:25 +0100150 default:
151 dev_err(&pdev->dev, "Unknown chip variant %d\n",
152 arizona->type);
153 return -EINVAL;
154 }
155
156 if (pdata && pdata->gpio_base)
157 arizona_gpio->gpio_chip.base = pdata->gpio_base;
158 else
159 arizona_gpio->gpio_chip.base = -1;
160
Laxman Dewangandb303a92016-02-22 17:43:28 +0530161 ret = devm_gpiochip_add_data(&pdev->dev, &arizona_gpio->gpio_chip,
162 arizona_gpio);
Mark Brown31ba56f2012-06-23 13:29:25 +0100163 if (ret < 0) {
164 dev_err(&pdev->dev, "Could not register gpiochip, %d\n",
165 ret);
Charles Keepax975acebb2016-11-25 13:48:30 +0000166 return ret;
Mark Brown31ba56f2012-06-23 13:29:25 +0100167 }
168
Charles Keepax975acebb2016-11-25 13:48:30 +0000169 return 0;
Mark Brown31ba56f2012-06-23 13:29:25 +0100170}
171
Mark Brown31ba56f2012-06-23 13:29:25 +0100172static struct platform_driver arizona_gpio_driver = {
173 .driver.name = "arizona-gpio",
Mark Brown31ba56f2012-06-23 13:29:25 +0100174 .probe = arizona_gpio_probe,
Mark Brown31ba56f2012-06-23 13:29:25 +0100175};
176
177module_platform_driver(arizona_gpio_driver);
178
179MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
180MODULE_DESCRIPTION("GPIO interface for Arizona devices");
181MODULE_LICENSE("GPL");
182MODULE_ALIAS("platform:arizona-gpio");