1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * gpiolib support for Wolfson WM835x PMICs 4 * 5 * Copyright 2009 Wolfson Microelectronics PLC. 6 * 7 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com> 8 * 9 */ 10 11 #include <linux/gpio/driver.h> 12 #include <linux/kernel.h> 13 #include <linux/mfd/core.h> 14 #include <linux/module.h> 15 #include <linux/platform_device.h> 16 #include <linux/slab.h> 17 18 #include <linux/mfd/wm8350/core.h> 19 #include <linux/mfd/wm8350/gpio.h> 20 21 struct wm8350_gpio_data { 22 struct wm8350 *wm8350; 23 struct gpio_chip gpio_chip; 24 }; 25 26 static int wm8350_gpio_direction_in(struct gpio_chip *chip, unsigned offset) 27 { 28 struct wm8350_gpio_data *wm8350_gpio = gpiochip_get_data(chip); 29 struct wm8350 *wm8350 = wm8350_gpio->wm8350; 30 31 return wm8350_set_bits(wm8350, WM8350_GPIO_CONFIGURATION_I_O, 32 1 << offset); 33 } 34 35 static int wm8350_gpio_get(struct gpio_chip *chip, unsigned offset) 36 { 37 struct wm8350_gpio_data *wm8350_gpio = gpiochip_get_data(chip); 38 struct wm8350 *wm8350 = wm8350_gpio->wm8350; 39 int ret; 40 41 ret = wm8350_reg_read(wm8350, WM8350_GPIO_LEVEL); 42 if (ret < 0) 43 return ret; 44 45 if (ret & (1 << offset)) 46 return 1; 47 else 48 return 0; 49 } 50 51 static int wm8350_gpio_set(struct gpio_chip *chip, unsigned int offset, 52 int value) 53 { 54 struct wm8350_gpio_data *wm8350_gpio = gpiochip_get_data(chip); 55 struct wm8350 *wm8350 = wm8350_gpio->wm8350; 56 57 if (value) 58 return wm8350_set_bits(wm8350, WM8350_GPIO_LEVEL, 1 << offset); 59 60 return wm8350_clear_bits(wm8350, WM8350_GPIO_LEVEL, 1 << offset); 61 } 62 63 static int wm8350_gpio_direction_out(struct gpio_chip *chip, 64 unsigned offset, int value) 65 { 66 struct wm8350_gpio_data *wm8350_gpio = gpiochip_get_data(chip); 67 struct wm8350 *wm8350 = wm8350_gpio->wm8350; 68 int ret; 69 70 ret = wm8350_clear_bits(wm8350, WM8350_GPIO_CONFIGURATION_I_O, 71 1 << offset); 72 if (ret < 0) 73 return ret; 74 75 /* Don't have an atomic direction/value setup */ 76 return wm8350_gpio_set(chip, offset, value); 77 } 78 79 static int wm8350_gpio_to_irq(struct gpio_chip *chip, unsigned offset) 80 { 81 struct wm8350_gpio_data *wm8350_gpio = gpiochip_get_data(chip); 82 struct wm8350 *wm8350 = wm8350_gpio->wm8350; 83 84 if (!wm8350->irq_base) 85 return -EINVAL; 86 87 return wm8350->irq_base + WM8350_IRQ_GPIO(offset); 88 } 89 90 static const struct gpio_chip template_chip = { 91 .label = "wm8350", 92 .owner = THIS_MODULE, 93 .direction_input = wm8350_gpio_direction_in, 94 .get = wm8350_gpio_get, 95 .direction_output = wm8350_gpio_direction_out, 96 .set = wm8350_gpio_set, 97 .to_irq = wm8350_gpio_to_irq, 98 .can_sleep = true, 99 }; 100 101 static int wm8350_gpio_probe(struct platform_device *pdev) 102 { 103 struct wm8350 *wm8350 = dev_get_drvdata(pdev->dev.parent); 104 struct wm8350_platform_data *pdata = dev_get_platdata(wm8350->dev); 105 struct wm8350_gpio_data *wm8350_gpio; 106 107 wm8350_gpio = devm_kzalloc(&pdev->dev, sizeof(*wm8350_gpio), 108 GFP_KERNEL); 109 if (wm8350_gpio == NULL) 110 return -ENOMEM; 111 112 wm8350_gpio->wm8350 = wm8350; 113 wm8350_gpio->gpio_chip = template_chip; 114 wm8350_gpio->gpio_chip.ngpio = 13; 115 wm8350_gpio->gpio_chip.parent = &pdev->dev; 116 if (pdata && pdata->gpio_base) 117 wm8350_gpio->gpio_chip.base = pdata->gpio_base; 118 else 119 wm8350_gpio->gpio_chip.base = -1; 120 121 return devm_gpiochip_add_data(&pdev->dev, &wm8350_gpio->gpio_chip, wm8350_gpio); 122 } 123 124 static struct platform_driver wm8350_gpio_driver = { 125 .driver.name = "wm8350-gpio", 126 .probe = wm8350_gpio_probe, 127 }; 128 129 static int __init wm8350_gpio_init(void) 130 { 131 return platform_driver_register(&wm8350_gpio_driver); 132 } 133 subsys_initcall(wm8350_gpio_init); 134 135 static void __exit wm8350_gpio_exit(void) 136 { 137 platform_driver_unregister(&wm8350_gpio_driver); 138 } 139 module_exit(wm8350_gpio_exit); 140 141 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>"); 142 MODULE_DESCRIPTION("GPIO interface for WM8350 PMICs"); 143 MODULE_LICENSE("GPL"); 144 MODULE_ALIAS("platform:wm8350-gpio"); 145