^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) * GPIO Driver for Dialog DA9055 PMICs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright(c) 2012 Dialog Semiconductor Ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Author: David Dajun Chen <dchen@diasemi.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/gpio/driver.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/mfd/da9055/core.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/mfd/da9055/reg.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/mfd/da9055/pdata.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #define DA9055_VDD_IO 0x0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #define DA9055_PUSH_PULL 0x3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #define DA9055_ACT_LOW 0x0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #define DA9055_GPI 0x1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #define DA9055_PORT_MASK 0x3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #define DA9055_PORT_SHIFT(offset) (4 * (offset % 2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #define DA9055_INPUT DA9055_GPI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #define DA9055_OUTPUT DA9055_PUSH_PULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #define DA9055_IRQ_GPI0 3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) struct da9055_gpio {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) struct da9055 *da9055;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) struct gpio_chip gp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) static int da9055_gpio_get(struct gpio_chip *gc, unsigned offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) struct da9055_gpio *gpio = gpiochip_get_data(gc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) int gpio_direction = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) /* Get GPIO direction */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) ret = da9055_reg_read(gpio->da9055, (offset >> 1) + DA9055_REG_GPIO0_1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) gpio_direction = ret & (DA9055_PORT_MASK) << DA9055_PORT_SHIFT(offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) gpio_direction >>= DA9055_PORT_SHIFT(offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) switch (gpio_direction) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) case DA9055_INPUT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) ret = da9055_reg_read(gpio->da9055, DA9055_REG_STATUS_B);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) case DA9055_OUTPUT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) ret = da9055_reg_read(gpio->da9055, DA9055_REG_GPIO_MODE0_2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) return ret & (1 << offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) static void da9055_gpio_set(struct gpio_chip *gc, unsigned offset, int value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) struct da9055_gpio *gpio = gpiochip_get_data(gc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) da9055_reg_update(gpio->da9055,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) DA9055_REG_GPIO_MODE0_2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) 1 << offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) value << offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) static int da9055_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) struct da9055_gpio *gpio = gpiochip_get_data(gc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) unsigned char reg_byte;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) reg_byte = (DA9055_ACT_LOW | DA9055_GPI)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) << DA9055_PORT_SHIFT(offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) return da9055_reg_update(gpio->da9055, (offset >> 1) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) DA9055_REG_GPIO0_1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) DA9055_PORT_MASK <<
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) DA9055_PORT_SHIFT(offset),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) reg_byte);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) static int da9055_gpio_direction_output(struct gpio_chip *gc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) unsigned offset, int value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) struct da9055_gpio *gpio = gpiochip_get_data(gc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) unsigned char reg_byte;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) reg_byte = (DA9055_VDD_IO | DA9055_PUSH_PULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) << DA9055_PORT_SHIFT(offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) ret = da9055_reg_update(gpio->da9055, (offset >> 1) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) DA9055_REG_GPIO0_1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) DA9055_PORT_MASK <<
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) DA9055_PORT_SHIFT(offset),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) reg_byte);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) da9055_gpio_set(gc, offset, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) static int da9055_gpio_to_irq(struct gpio_chip *gc, u32 offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) struct da9055_gpio *gpio = gpiochip_get_data(gc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) struct da9055 *da9055 = gpio->da9055;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) return regmap_irq_get_virq(da9055->irq_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) DA9055_IRQ_GPI0 + offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) static const struct gpio_chip reference_gp = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) .label = "da9055-gpio",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) .get = da9055_gpio_get,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) .set = da9055_gpio_set,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) .direction_input = da9055_gpio_direction_input,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) .direction_output = da9055_gpio_direction_output,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) .to_irq = da9055_gpio_to_irq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) .can_sleep = true,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) .ngpio = 3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) .base = -1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) static int da9055_gpio_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) struct da9055_gpio *gpio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) struct da9055_pdata *pdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) if (!gpio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) gpio->da9055 = dev_get_drvdata(pdev->dev.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) pdata = dev_get_platdata(gpio->da9055->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) gpio->gp = reference_gp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) if (pdata && pdata->gpio_base)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) gpio->gp.base = pdata->gpio_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) ret = devm_gpiochip_add_data(&pdev->dev, &gpio->gp, gpio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) dev_err(&pdev->dev, "Could not register gpiochip, %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) platform_set_drvdata(pdev, gpio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) static struct platform_driver da9055_gpio_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) .probe = da9055_gpio_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) .name = "da9055-gpio",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) static int __init da9055_gpio_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) return platform_driver_register(&da9055_gpio_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) subsys_initcall(da9055_gpio_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) static void __exit da9055_gpio_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) platform_driver_unregister(&da9055_gpio_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) module_exit(da9055_gpio_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) MODULE_DESCRIPTION("DA9055 GPIO Device Driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) MODULE_ALIAS("platform:da9055-gpio");