Mark Brown | 38f6ce4 | 2010-01-07 16:10:08 +0000 | [diff] [blame] | 1 | /* |
Grant Likely | c103de2 | 2011-06-04 18:38:28 -0600 | [diff] [blame] | 2 | * gpiolib support for Wolfson WM835x PMICs |
Mark Brown | 38f6ce4 | 2010-01-07 16:10:08 +0000 | [diff] [blame] | 3 | * |
| 4 | * Copyright 2009 Wolfson Microelectronics PLC. |
| 5 | * |
| 6 | * Author: Mark Brown <broonie@opensource.wolfsonmicro.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify it |
| 9 | * under the terms of the GNU General Public License as published by the |
| 10 | * Free Software Foundation; either version 2 of the License, or (at your |
| 11 | * option) any later version. |
| 12 | * |
| 13 | */ |
| 14 | |
| 15 | #include <linux/kernel.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 16 | #include <linux/slab.h> |
Mark Brown | 38f6ce4 | 2010-01-07 16:10:08 +0000 | [diff] [blame] | 17 | #include <linux/module.h> |
| 18 | #include <linux/gpio.h> |
| 19 | #include <linux/mfd/core.h> |
| 20 | #include <linux/platform_device.h> |
| 21 | #include <linux/seq_file.h> |
| 22 | |
| 23 | #include <linux/mfd/wm8350/core.h> |
| 24 | #include <linux/mfd/wm8350/gpio.h> |
| 25 | |
| 26 | struct wm8350_gpio_data { |
| 27 | struct wm8350 *wm8350; |
| 28 | struct gpio_chip gpio_chip; |
| 29 | }; |
| 30 | |
Mark Brown | 38f6ce4 | 2010-01-07 16:10:08 +0000 | [diff] [blame] | 31 | static int wm8350_gpio_direction_in(struct gpio_chip *chip, unsigned offset) |
| 32 | { |
Linus Walleij | dfcdf72 | 2015-12-07 15:13:39 +0100 | [diff] [blame] | 33 | struct wm8350_gpio_data *wm8350_gpio = gpiochip_get_data(chip); |
Mark Brown | 38f6ce4 | 2010-01-07 16:10:08 +0000 | [diff] [blame] | 34 | struct wm8350 *wm8350 = wm8350_gpio->wm8350; |
| 35 | |
| 36 | return wm8350_set_bits(wm8350, WM8350_GPIO_CONFIGURATION_I_O, |
| 37 | 1 << offset); |
| 38 | } |
| 39 | |
| 40 | static int wm8350_gpio_get(struct gpio_chip *chip, unsigned offset) |
| 41 | { |
Linus Walleij | dfcdf72 | 2015-12-07 15:13:39 +0100 | [diff] [blame] | 42 | struct wm8350_gpio_data *wm8350_gpio = gpiochip_get_data(chip); |
Mark Brown | 38f6ce4 | 2010-01-07 16:10:08 +0000 | [diff] [blame] | 43 | struct wm8350 *wm8350 = wm8350_gpio->wm8350; |
| 44 | int ret; |
| 45 | |
| 46 | ret = wm8350_reg_read(wm8350, WM8350_GPIO_LEVEL); |
| 47 | if (ret < 0) |
| 48 | return ret; |
| 49 | |
| 50 | if (ret & (1 << offset)) |
| 51 | return 1; |
| 52 | else |
| 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | static void wm8350_gpio_set(struct gpio_chip *chip, unsigned offset, int value) |
| 57 | { |
Linus Walleij | dfcdf72 | 2015-12-07 15:13:39 +0100 | [diff] [blame] | 58 | struct wm8350_gpio_data *wm8350_gpio = gpiochip_get_data(chip); |
Mark Brown | 38f6ce4 | 2010-01-07 16:10:08 +0000 | [diff] [blame] | 59 | struct wm8350 *wm8350 = wm8350_gpio->wm8350; |
| 60 | |
| 61 | if (value) |
| 62 | wm8350_set_bits(wm8350, WM8350_GPIO_LEVEL, 1 << offset); |
| 63 | else |
| 64 | wm8350_clear_bits(wm8350, WM8350_GPIO_LEVEL, 1 << offset); |
| 65 | } |
| 66 | |
| 67 | static int wm8350_gpio_direction_out(struct gpio_chip *chip, |
| 68 | unsigned offset, int value) |
| 69 | { |
Linus Walleij | dfcdf72 | 2015-12-07 15:13:39 +0100 | [diff] [blame] | 70 | struct wm8350_gpio_data *wm8350_gpio = gpiochip_get_data(chip); |
Mark Brown | 38f6ce4 | 2010-01-07 16:10:08 +0000 | [diff] [blame] | 71 | struct wm8350 *wm8350 = wm8350_gpio->wm8350; |
| 72 | int ret; |
| 73 | |
| 74 | ret = wm8350_clear_bits(wm8350, WM8350_GPIO_CONFIGURATION_I_O, |
| 75 | 1 << offset); |
| 76 | if (ret < 0) |
| 77 | return ret; |
| 78 | |
| 79 | /* Don't have an atomic direction/value setup */ |
| 80 | wm8350_gpio_set(chip, offset, value); |
| 81 | |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | static int wm8350_gpio_to_irq(struct gpio_chip *chip, unsigned offset) |
| 86 | { |
Linus Walleij | dfcdf72 | 2015-12-07 15:13:39 +0100 | [diff] [blame] | 87 | struct wm8350_gpio_data *wm8350_gpio = gpiochip_get_data(chip); |
Mark Brown | 38f6ce4 | 2010-01-07 16:10:08 +0000 | [diff] [blame] | 88 | struct wm8350 *wm8350 = wm8350_gpio->wm8350; |
| 89 | |
| 90 | if (!wm8350->irq_base) |
| 91 | return -EINVAL; |
| 92 | |
| 93 | return wm8350->irq_base + WM8350_IRQ_GPIO(offset); |
| 94 | } |
| 95 | |
| 96 | static struct gpio_chip template_chip = { |
| 97 | .label = "wm8350", |
| 98 | .owner = THIS_MODULE, |
| 99 | .direction_input = wm8350_gpio_direction_in, |
| 100 | .get = wm8350_gpio_get, |
| 101 | .direction_output = wm8350_gpio_direction_out, |
| 102 | .set = wm8350_gpio_set, |
| 103 | .to_irq = wm8350_gpio_to_irq, |
Linus Walleij | 9fb1f39 | 2013-12-04 14:42:46 +0100 | [diff] [blame] | 104 | .can_sleep = true, |
Mark Brown | 38f6ce4 | 2010-01-07 16:10:08 +0000 | [diff] [blame] | 105 | }; |
| 106 | |
Bill Pemberton | 3836309 | 2012-11-19 13:22:34 -0500 | [diff] [blame] | 107 | static int wm8350_gpio_probe(struct platform_device *pdev) |
Mark Brown | 38f6ce4 | 2010-01-07 16:10:08 +0000 | [diff] [blame] | 108 | { |
| 109 | struct wm8350 *wm8350 = dev_get_drvdata(pdev->dev.parent); |
Jingoo Han | e56aee1 | 2013-07-30 17:08:05 +0900 | [diff] [blame] | 110 | struct wm8350_platform_data *pdata = dev_get_platdata(wm8350->dev); |
Mark Brown | 38f6ce4 | 2010-01-07 16:10:08 +0000 | [diff] [blame] | 111 | struct wm8350_gpio_data *wm8350_gpio; |
| 112 | int ret; |
| 113 | |
Axel Lin | 5b42543 | 2012-09-02 11:58:59 +0800 | [diff] [blame] | 114 | wm8350_gpio = devm_kzalloc(&pdev->dev, sizeof(*wm8350_gpio), |
| 115 | GFP_KERNEL); |
Mark Brown | 38f6ce4 | 2010-01-07 16:10:08 +0000 | [diff] [blame] | 116 | if (wm8350_gpio == NULL) |
| 117 | return -ENOMEM; |
| 118 | |
| 119 | wm8350_gpio->wm8350 = wm8350; |
| 120 | wm8350_gpio->gpio_chip = template_chip; |
| 121 | wm8350_gpio->gpio_chip.ngpio = 13; |
Linus Walleij | 58383c7 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 122 | wm8350_gpio->gpio_chip.parent = &pdev->dev; |
Mark Brown | 38f6ce4 | 2010-01-07 16:10:08 +0000 | [diff] [blame] | 123 | if (pdata && pdata->gpio_base) |
| 124 | wm8350_gpio->gpio_chip.base = pdata->gpio_base; |
| 125 | else |
| 126 | wm8350_gpio->gpio_chip.base = -1; |
| 127 | |
Linus Walleij | dfcdf72 | 2015-12-07 15:13:39 +0100 | [diff] [blame] | 128 | ret = gpiochip_add_data(&wm8350_gpio->gpio_chip, wm8350_gpio); |
Mark Brown | 38f6ce4 | 2010-01-07 16:10:08 +0000 | [diff] [blame] | 129 | if (ret < 0) { |
Axel Lin | 5b42543 | 2012-09-02 11:58:59 +0800 | [diff] [blame] | 130 | dev_err(&pdev->dev, "Could not register gpiochip, %d\n", ret); |
| 131 | return ret; |
Mark Brown | 38f6ce4 | 2010-01-07 16:10:08 +0000 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | platform_set_drvdata(pdev, wm8350_gpio); |
| 135 | |
| 136 | return ret; |
Mark Brown | 38f6ce4 | 2010-01-07 16:10:08 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Bill Pemberton | 206210c | 2012-11-19 13:25:50 -0500 | [diff] [blame] | 139 | static int wm8350_gpio_remove(struct platform_device *pdev) |
Mark Brown | 38f6ce4 | 2010-01-07 16:10:08 +0000 | [diff] [blame] | 140 | { |
| 141 | struct wm8350_gpio_data *wm8350_gpio = platform_get_drvdata(pdev); |
Mark Brown | 38f6ce4 | 2010-01-07 16:10:08 +0000 | [diff] [blame] | 142 | |
abdoulaye berthe | 9f5132a | 2014-07-12 22:30:12 +0200 | [diff] [blame] | 143 | gpiochip_remove(&wm8350_gpio->gpio_chip); |
| 144 | return 0; |
Mark Brown | 38f6ce4 | 2010-01-07 16:10:08 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | static struct platform_driver wm8350_gpio_driver = { |
| 148 | .driver.name = "wm8350-gpio", |
| 149 | .driver.owner = THIS_MODULE, |
| 150 | .probe = wm8350_gpio_probe, |
Bill Pemberton | 8283c4f | 2012-11-19 13:20:08 -0500 | [diff] [blame] | 151 | .remove = wm8350_gpio_remove, |
Mark Brown | 38f6ce4 | 2010-01-07 16:10:08 +0000 | [diff] [blame] | 152 | }; |
| 153 | |
| 154 | static int __init wm8350_gpio_init(void) |
| 155 | { |
| 156 | return platform_driver_register(&wm8350_gpio_driver); |
| 157 | } |
| 158 | subsys_initcall(wm8350_gpio_init); |
| 159 | |
| 160 | static void __exit wm8350_gpio_exit(void) |
| 161 | { |
| 162 | platform_driver_unregister(&wm8350_gpio_driver); |
| 163 | } |
| 164 | module_exit(wm8350_gpio_exit); |
| 165 | |
| 166 | MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>"); |
| 167 | MODULE_DESCRIPTION("GPIO interface for WM8350 PMICs"); |
| 168 | MODULE_LICENSE("GPL"); |
| 169 | MODULE_ALIAS("platform:wm8350-gpio"); |