gpio-arizona.c 5.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * gpiolib support for Wolfson Arizona class devices
 *
 * Copyright 2012 Wolfson Microelectronics PLC.
 *
 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
 *
 *  This program is free software; you can redistribute  it and/or modify it
 *  under  the terms of  the GNU General  Public License as published by the
 *  Free Software Foundation;  either version 2 of the  License, or (at your
 *  option) any later version.
 *
 */

#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/module.h>
#include <linux/gpio.h>
#include <linux/platform_device.h>
20
#include <linux/pm_runtime.h>
21 22 23 24 25 26 27 28 29 30 31 32 33
#include <linux/seq_file.h>

#include <linux/mfd/arizona/core.h>
#include <linux/mfd/arizona/pdata.h>
#include <linux/mfd/arizona/registers.h>

struct arizona_gpio {
	struct arizona *arizona;
	struct gpio_chip gpio_chip;
};

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;
36 37 38
	bool persistent = gpiochip_line_is_persistent(chip, offset);
	bool change;
	int ret;
39

40 41 42 43 44 45 46 47 48 49 50 51 52
	ret = regmap_update_bits_check(arizona->regmap,
				       ARIZONA_GPIO1_CTRL + offset,
				       ARIZONA_GPN_DIR, ARIZONA_GPN_DIR,
				       &change);
	if (ret < 0)
		return ret;

	if (change && persistent) {
		pm_runtime_mark_last_busy(chip->parent);
		pm_runtime_put_autosuspend(chip->parent);
	}

	return 0;
53 54 55 56
}

static int arizona_gpio_get(struct gpio_chip *chip, unsigned offset)
{
57
	struct arizona_gpio *arizona_gpio = gpiochip_get_data(chip);
58
	struct arizona *arizona = arizona_gpio->arizona;
59
	unsigned int reg, val;
60 61
	int ret;

62 63
	reg = ARIZONA_GPIO1_CTRL + offset;
	ret = regmap_read(arizona->regmap, reg, &val);
64 65 66
	if (ret < 0)
		return ret;

67
	/* Resume to read actual registers for input pins */
68
	if (val & ARIZONA_GPN_DIR) {
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
		ret = pm_runtime_get_sync(chip->parent);
		if (ret < 0) {
			dev_err(chip->parent, "Failed to resume: %d\n", ret);
			return ret;
		}

		/* Register is cached, drop it to ensure a physical read */
		ret = regcache_drop_region(arizona->regmap, reg, reg);
		if (ret < 0) {
			dev_err(chip->parent, "Failed to drop cache: %d\n",
				ret);
			return ret;
		}

		ret = regmap_read(arizona->regmap, reg, &val);
		if (ret < 0)
			return ret;

		pm_runtime_mark_last_busy(chip->parent);
		pm_runtime_put_autosuspend(chip->parent);
	}

91 92 93 94 95 96 97 98 99
	if (val & ARIZONA_GPN_LVL)
		return 1;
	else
		return 0;
}

static int arizona_gpio_direction_out(struct gpio_chip *chip,
				     unsigned offset, int value)
{
100
	struct arizona_gpio *arizona_gpio = gpiochip_get_data(chip);
101
	struct arizona *arizona = arizona_gpio->arizona;
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
	bool persistent = gpiochip_line_is_persistent(chip, offset);
	unsigned int val;
	int ret;

	ret = regmap_read(arizona->regmap, ARIZONA_GPIO1_CTRL + offset, &val);
	if (ret < 0)
		return ret;

	if ((val & ARIZONA_GPN_DIR) && persistent) {
		ret = pm_runtime_get_sync(chip->parent);
		if (ret < 0) {
			dev_err(chip->parent, "Failed to resume: %d\n", ret);
			return ret;
		}
	}
117 118 119 120 121 122 123 124 125 126

	if (value)
		value = ARIZONA_GPN_LVL;

	return regmap_update_bits(arizona->regmap, ARIZONA_GPIO1_CTRL + offset,
				  ARIZONA_GPN_DIR | ARIZONA_GPN_LVL, value);
}

static void arizona_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
{
127
	struct arizona_gpio *arizona_gpio = gpiochip_get_data(chip);
128 129 130 131 132 133 134 135 136
	struct arizona *arizona = arizona_gpio->arizona;

	if (value)
		value = ARIZONA_GPN_LVL;

	regmap_update_bits(arizona->regmap, ARIZONA_GPIO1_CTRL + offset,
			   ARIZONA_GPN_LVL, value);
}

137
static const struct gpio_chip template_chip = {
138 139 140 141 142 143
	.label			= "arizona",
	.owner			= THIS_MODULE,
	.direction_input	= arizona_gpio_direction_in,
	.get			= arizona_gpio_get,
	.direction_output	= arizona_gpio_direction_out,
	.set			= arizona_gpio_set,
144
	.can_sleep		= true,
145 146
};

B
Bill Pemberton 已提交
147
static int arizona_gpio_probe(struct platform_device *pdev)
148 149
{
	struct arizona *arizona = dev_get_drvdata(pdev->dev.parent);
J
Jingoo Han 已提交
150
	struct arizona_pdata *pdata = dev_get_platdata(arizona->dev);
151 152 153 154 155
	struct arizona_gpio *arizona_gpio;
	int ret;

	arizona_gpio = devm_kzalloc(&pdev->dev, sizeof(*arizona_gpio),
				    GFP_KERNEL);
156
	if (!arizona_gpio)
157 158 159 160
		return -ENOMEM;

	arizona_gpio->arizona = arizona;
	arizona_gpio->gpio_chip = template_chip;
161
	arizona_gpio->gpio_chip.parent = &pdev->dev;
162 163 164
#ifdef CONFIG_OF_GPIO
	arizona_gpio->gpio_chip.of_node = arizona->dev->of_node;
#endif
165 166 167 168

	switch (arizona->type) {
	case WM5102:
	case WM5110:
169
	case WM8280:
170
	case WM8997:
171 172
	case WM8998:
	case WM1814:
173 174
		arizona_gpio->gpio_chip.ngpio = 5;
		break;
175 176 177 178
	case WM1831:
	case CS47L24:
		arizona_gpio->gpio_chip.ngpio = 2;
		break;
179 180 181 182 183 184 185 186 187 188 189
	default:
		dev_err(&pdev->dev, "Unknown chip variant %d\n",
			arizona->type);
		return -EINVAL;
	}

	if (pdata && pdata->gpio_base)
		arizona_gpio->gpio_chip.base = pdata->gpio_base;
	else
		arizona_gpio->gpio_chip.base = -1;

190 191
	pm_runtime_enable(&pdev->dev);

192 193
	ret = devm_gpiochip_add_data(&pdev->dev, &arizona_gpio->gpio_chip,
				     arizona_gpio);
194 195 196
	if (ret < 0) {
		dev_err(&pdev->dev, "Could not register gpiochip, %d\n",
			ret);
197
		return ret;
198 199
	}

200
	return 0;
201 202 203 204 205 206 207 208 209 210 211 212 213
}

static struct platform_driver arizona_gpio_driver = {
	.driver.name	= "arizona-gpio",
	.probe		= arizona_gpio_probe,
};

module_platform_driver(arizona_gpio_driver);

MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
MODULE_DESCRIPTION("GPIO interface for Arizona devices");
MODULE_LICENSE("GPL");
MODULE_ALIAS("platform:arizona-gpio");