^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * gpiolib support for Wolfson Arizona class devices
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright 2012 Wolfson Microelectronics PLC.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/gpio/driver.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/pm_runtime.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/seq_file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/mfd/arizona/core.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/mfd/arizona/pdata.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/mfd/arizona/registers.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) struct arizona_gpio {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) struct arizona *arizona;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) struct gpio_chip gpio_chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) static int arizona_gpio_direction_in(struct gpio_chip *chip, unsigned offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) struct arizona_gpio *arizona_gpio = gpiochip_get_data(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) struct arizona *arizona = arizona_gpio->arizona;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) bool persistent = gpiochip_line_is_persistent(chip, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) bool change;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) ret = regmap_update_bits_check(arizona->regmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) ARIZONA_GPIO1_CTRL + offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) ARIZONA_GPN_DIR, ARIZONA_GPN_DIR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) &change);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) if (change && persistent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) pm_runtime_mark_last_busy(chip->parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) pm_runtime_put_autosuspend(chip->parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) static int arizona_gpio_get(struct gpio_chip *chip, unsigned offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) struct arizona_gpio *arizona_gpio = gpiochip_get_data(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) struct arizona *arizona = arizona_gpio->arizona;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) unsigned int reg, val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) reg = ARIZONA_GPIO1_CTRL + offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) ret = regmap_read(arizona->regmap, reg, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) /* Resume to read actual registers for input pins */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) if (val & ARIZONA_GPN_DIR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) ret = pm_runtime_get_sync(chip->parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) dev_err(chip->parent, "Failed to resume: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) pm_runtime_put_autosuspend(chip->parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) /* Register is cached, drop it to ensure a physical read */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) ret = regcache_drop_region(arizona->regmap, reg, reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) dev_err(chip->parent, "Failed to drop cache: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) pm_runtime_put_autosuspend(chip->parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) ret = regmap_read(arizona->regmap, reg, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) pm_runtime_put_autosuspend(chip->parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) pm_runtime_mark_last_busy(chip->parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) pm_runtime_put_autosuspend(chip->parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) if (val & ARIZONA_GPN_LVL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) static int arizona_gpio_direction_out(struct gpio_chip *chip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) unsigned offset, int value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) struct arizona_gpio *arizona_gpio = gpiochip_get_data(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) struct arizona *arizona = arizona_gpio->arizona;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) bool persistent = gpiochip_line_is_persistent(chip, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) unsigned int val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) ret = regmap_read(arizona->regmap, ARIZONA_GPIO1_CTRL + offset, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) if ((val & ARIZONA_GPN_DIR) && persistent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) ret = pm_runtime_get_sync(chip->parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) dev_err(chip->parent, "Failed to resume: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) pm_runtime_put(chip->parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) if (value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) value = ARIZONA_GPN_LVL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) return regmap_update_bits(arizona->regmap, ARIZONA_GPIO1_CTRL + offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) ARIZONA_GPN_DIR | ARIZONA_GPN_LVL, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) static void arizona_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) struct arizona_gpio *arizona_gpio = gpiochip_get_data(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) struct arizona *arizona = arizona_gpio->arizona;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) if (value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) value = ARIZONA_GPN_LVL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) regmap_update_bits(arizona->regmap, ARIZONA_GPIO1_CTRL + offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) ARIZONA_GPN_LVL, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) static const struct gpio_chip template_chip = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) .label = "arizona",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) .direction_input = arizona_gpio_direction_in,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) .get = arizona_gpio_get,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) .direction_output = arizona_gpio_direction_out,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) .set = arizona_gpio_set,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) .can_sleep = true,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) static int arizona_gpio_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) struct arizona *arizona = dev_get_drvdata(pdev->dev.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) struct arizona_pdata *pdata = &arizona->pdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) struct arizona_gpio *arizona_gpio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) arizona_gpio = devm_kzalloc(&pdev->dev, sizeof(*arizona_gpio),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) if (!arizona_gpio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) arizona_gpio->arizona = arizona;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) arizona_gpio->gpio_chip = template_chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) arizona_gpio->gpio_chip.parent = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) #ifdef CONFIG_OF_GPIO
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) arizona_gpio->gpio_chip.of_node = arizona->dev->of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) switch (arizona->type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) case WM5102:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) case WM5110:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) case WM8280:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) case WM8997:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) case WM8998:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) case WM1814:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) arizona_gpio->gpio_chip.ngpio = 5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) case WM1831:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) case CS47L24:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) arizona_gpio->gpio_chip.ngpio = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) dev_err(&pdev->dev, "Unknown chip variant %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) arizona->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) if (pdata->gpio_base)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) arizona_gpio->gpio_chip.base = pdata->gpio_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) arizona_gpio->gpio_chip.base = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) pm_runtime_enable(&pdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) ret = devm_gpiochip_add_data(&pdev->dev, &arizona_gpio->gpio_chip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) arizona_gpio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) pm_runtime_disable(&pdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) dev_err(&pdev->dev, "Could not register gpiochip, %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) static struct platform_driver arizona_gpio_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) .driver.name = "arizona-gpio",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) .probe = arizona_gpio_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) module_platform_driver(arizona_gpio_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) MODULE_DESCRIPTION("GPIO interface for Arizona devices");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) MODULE_ALIAS("platform:arizona-gpio");