Linus Walleij | 66dbe75 | 2018-09-13 13:28:13 +0200 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Mark Brown | 38f6ce4 | 2010-01-07 16:10:08 +0000 | [diff] [blame] | 2 | /* |
Grant Likely | c103de2 | 2011-06-04 18:38:28 -0600 | [diff] [blame] | 3 | * gpiolib support for Wolfson WM835x PMICs |
Mark Brown | 38f6ce4 | 2010-01-07 16:10:08 +0000 | [diff] [blame] | 4 | * |
| 5 | * Copyright 2009 Wolfson Microelectronics PLC. |
| 6 | * |
| 7 | * Author: Mark Brown <broonie@opensource.wolfsonmicro.com> |
| 8 | * |
Mark Brown | 38f6ce4 | 2010-01-07 16:10:08 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include <linux/kernel.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 12 | #include <linux/slab.h> |
Mark Brown | 38f6ce4 | 2010-01-07 16:10:08 +0000 | [diff] [blame] | 13 | #include <linux/module.h> |
Linus Walleij | 10833c4 | 2018-09-13 13:25:58 +0200 | [diff] [blame] | 14 | #include <linux/gpio/driver.h> |
Mark Brown | 38f6ce4 | 2010-01-07 16:10:08 +0000 | [diff] [blame] | 15 | #include <linux/mfd/core.h> |
| 16 | #include <linux/platform_device.h> |
| 17 | #include <linux/seq_file.h> |
| 18 | |
| 19 | #include <linux/mfd/wm8350/core.h> |
| 20 | #include <linux/mfd/wm8350/gpio.h> |
| 21 | |
| 22 | struct wm8350_gpio_data { |
| 23 | struct wm8350 *wm8350; |
| 24 | struct gpio_chip gpio_chip; |
| 25 | }; |
| 26 | |
Mark Brown | 38f6ce4 | 2010-01-07 16:10:08 +0000 | [diff] [blame] | 27 | static int wm8350_gpio_direction_in(struct gpio_chip *chip, unsigned offset) |
| 28 | { |
Linus Walleij | dfcdf72 | 2015-12-07 15:13:39 +0100 | [diff] [blame] | 29 | struct wm8350_gpio_data *wm8350_gpio = gpiochip_get_data(chip); |
Mark Brown | 38f6ce4 | 2010-01-07 16:10:08 +0000 | [diff] [blame] | 30 | struct wm8350 *wm8350 = wm8350_gpio->wm8350; |
| 31 | |
| 32 | return wm8350_set_bits(wm8350, WM8350_GPIO_CONFIGURATION_I_O, |
| 33 | 1 << offset); |
| 34 | } |
| 35 | |
| 36 | static int wm8350_gpio_get(struct gpio_chip *chip, unsigned offset) |
| 37 | { |
Linus Walleij | dfcdf72 | 2015-12-07 15:13:39 +0100 | [diff] [blame] | 38 | struct wm8350_gpio_data *wm8350_gpio = gpiochip_get_data(chip); |
Mark Brown | 38f6ce4 | 2010-01-07 16:10:08 +0000 | [diff] [blame] | 39 | struct wm8350 *wm8350 = wm8350_gpio->wm8350; |
| 40 | int ret; |
| 41 | |
| 42 | ret = wm8350_reg_read(wm8350, WM8350_GPIO_LEVEL); |
| 43 | if (ret < 0) |
| 44 | return ret; |
| 45 | |
| 46 | if (ret & (1 << offset)) |
| 47 | return 1; |
| 48 | else |
| 49 | return 0; |
| 50 | } |
| 51 | |
| 52 | static void wm8350_gpio_set(struct gpio_chip *chip, unsigned offset, int value) |
| 53 | { |
Linus Walleij | dfcdf72 | 2015-12-07 15:13:39 +0100 | [diff] [blame] | 54 | struct wm8350_gpio_data *wm8350_gpio = gpiochip_get_data(chip); |
Mark Brown | 38f6ce4 | 2010-01-07 16:10:08 +0000 | [diff] [blame] | 55 | struct wm8350 *wm8350 = wm8350_gpio->wm8350; |
| 56 | |
| 57 | if (value) |
| 58 | wm8350_set_bits(wm8350, WM8350_GPIO_LEVEL, 1 << offset); |
| 59 | else |
| 60 | 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 | { |
Linus Walleij | dfcdf72 | 2015-12-07 15:13:39 +0100 | [diff] [blame] | 66 | struct wm8350_gpio_data *wm8350_gpio = gpiochip_get_data(chip); |
Mark Brown | 38f6ce4 | 2010-01-07 16:10:08 +0000 | [diff] [blame] | 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 | wm8350_gpio_set(chip, offset, value); |
| 77 | |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | static int wm8350_gpio_to_irq(struct gpio_chip *chip, unsigned offset) |
| 82 | { |
Linus Walleij | dfcdf72 | 2015-12-07 15:13:39 +0100 | [diff] [blame] | 83 | struct wm8350_gpio_data *wm8350_gpio = gpiochip_get_data(chip); |
Mark Brown | 38f6ce4 | 2010-01-07 16:10:08 +0000 | [diff] [blame] | 84 | struct wm8350 *wm8350 = wm8350_gpio->wm8350; |
| 85 | |
| 86 | if (!wm8350->irq_base) |
| 87 | return -EINVAL; |
| 88 | |
| 89 | return wm8350->irq_base + WM8350_IRQ_GPIO(offset); |
| 90 | } |
| 91 | |
Julia Lawall | e35b5ab | 2016-09-11 14:14:37 +0200 | [diff] [blame] | 92 | static const struct gpio_chip template_chip = { |
Mark Brown | 38f6ce4 | 2010-01-07 16:10:08 +0000 | [diff] [blame] | 93 | .label = "wm8350", |
| 94 | .owner = THIS_MODULE, |
| 95 | .direction_input = wm8350_gpio_direction_in, |
| 96 | .get = wm8350_gpio_get, |
| 97 | .direction_output = wm8350_gpio_direction_out, |
| 98 | .set = wm8350_gpio_set, |
| 99 | .to_irq = wm8350_gpio_to_irq, |
Linus Walleij | 9fb1f39 | 2013-12-04 14:42:46 +0100 | [diff] [blame] | 100 | .can_sleep = true, |
Mark Brown | 38f6ce4 | 2010-01-07 16:10:08 +0000 | [diff] [blame] | 101 | }; |
| 102 | |
Bill Pemberton | 3836309 | 2012-11-19 13:22:34 -0500 | [diff] [blame] | 103 | static int wm8350_gpio_probe(struct platform_device *pdev) |
Mark Brown | 38f6ce4 | 2010-01-07 16:10:08 +0000 | [diff] [blame] | 104 | { |
| 105 | struct wm8350 *wm8350 = dev_get_drvdata(pdev->dev.parent); |
Jingoo Han | e56aee1 | 2013-07-30 17:08:05 +0900 | [diff] [blame] | 106 | struct wm8350_platform_data *pdata = dev_get_platdata(wm8350->dev); |
Mark Brown | 38f6ce4 | 2010-01-07 16:10:08 +0000 | [diff] [blame] | 107 | struct wm8350_gpio_data *wm8350_gpio; |
| 108 | int ret; |
| 109 | |
Axel Lin | 5b42543 | 2012-09-02 11:58:59 +0800 | [diff] [blame] | 110 | wm8350_gpio = devm_kzalloc(&pdev->dev, sizeof(*wm8350_gpio), |
| 111 | GFP_KERNEL); |
Mark Brown | 38f6ce4 | 2010-01-07 16:10:08 +0000 | [diff] [blame] | 112 | if (wm8350_gpio == NULL) |
| 113 | return -ENOMEM; |
| 114 | |
| 115 | wm8350_gpio->wm8350 = wm8350; |
| 116 | wm8350_gpio->gpio_chip = template_chip; |
| 117 | wm8350_gpio->gpio_chip.ngpio = 13; |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 118 | wm8350_gpio->gpio_chip.parent = &pdev->dev; |
Mark Brown | 38f6ce4 | 2010-01-07 16:10:08 +0000 | [diff] [blame] | 119 | if (pdata && pdata->gpio_base) |
| 120 | wm8350_gpio->gpio_chip.base = pdata->gpio_base; |
| 121 | else |
| 122 | wm8350_gpio->gpio_chip.base = -1; |
| 123 | |
Laxman Dewangan | 4a4925c | 2016-02-22 17:43:28 +0530 | [diff] [blame] | 124 | ret = devm_gpiochip_add_data(&pdev->dev, &wm8350_gpio->gpio_chip, |
| 125 | wm8350_gpio); |
Mark Brown | 38f6ce4 | 2010-01-07 16:10:08 +0000 | [diff] [blame] | 126 | if (ret < 0) { |
Axel Lin | 5b42543 | 2012-09-02 11:58:59 +0800 | [diff] [blame] | 127 | dev_err(&pdev->dev, "Could not register gpiochip, %d\n", ret); |
| 128 | return ret; |
Mark Brown | 38f6ce4 | 2010-01-07 16:10:08 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | platform_set_drvdata(pdev, wm8350_gpio); |
| 132 | |
| 133 | return ret; |
Mark Brown | 38f6ce4 | 2010-01-07 16:10:08 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Mark Brown | 38f6ce4 | 2010-01-07 16:10:08 +0000 | [diff] [blame] | 136 | static struct platform_driver wm8350_gpio_driver = { |
| 137 | .driver.name = "wm8350-gpio", |
Mark Brown | 38f6ce4 | 2010-01-07 16:10:08 +0000 | [diff] [blame] | 138 | .probe = wm8350_gpio_probe, |
Mark Brown | 38f6ce4 | 2010-01-07 16:10:08 +0000 | [diff] [blame] | 139 | }; |
| 140 | |
| 141 | static int __init wm8350_gpio_init(void) |
| 142 | { |
| 143 | return platform_driver_register(&wm8350_gpio_driver); |
| 144 | } |
| 145 | subsys_initcall(wm8350_gpio_init); |
| 146 | |
| 147 | static void __exit wm8350_gpio_exit(void) |
| 148 | { |
| 149 | platform_driver_unregister(&wm8350_gpio_driver); |
| 150 | } |
| 151 | module_exit(wm8350_gpio_exit); |
| 152 | |
| 153 | MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>"); |
| 154 | MODULE_DESCRIPTION("GPIO interface for WM8350 PMICs"); |
| 155 | MODULE_LICENSE("GPL"); |
| 156 | MODULE_ALIAS("platform:wm8350-gpio"); |