Linux Audio

Check our new training course

Loading...
v6.2
  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
 21struct wm8350_gpio_data {
 22	struct wm8350 *wm8350;
 23	struct gpio_chip gpio_chip;
 24};
 25
 
 
 
 
 
 26static 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
 35static 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
 51static void wm8350_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
 52{
 53	struct wm8350_gpio_data *wm8350_gpio = gpiochip_get_data(chip);
 54	struct wm8350 *wm8350 = wm8350_gpio->wm8350;
 55
 56	if (value)
 57		wm8350_set_bits(wm8350, WM8350_GPIO_LEVEL, 1 << offset);
 58	else
 59		wm8350_clear_bits(wm8350, WM8350_GPIO_LEVEL, 1 << offset);
 60}
 61
 62static int wm8350_gpio_direction_out(struct gpio_chip *chip,
 63				     unsigned offset, int value)
 64{
 65	struct wm8350_gpio_data *wm8350_gpio = gpiochip_get_data(chip);
 66	struct wm8350 *wm8350 = wm8350_gpio->wm8350;
 67	int ret;
 68
 69	ret = wm8350_clear_bits(wm8350, WM8350_GPIO_CONFIGURATION_I_O,
 70				1 << offset);
 71	if (ret < 0)
 72		return ret;
 73
 74	/* Don't have an atomic direction/value setup */
 75	wm8350_gpio_set(chip, offset, value);
 76
 77	return 0;
 78}
 79
 80static int wm8350_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
 81{
 82	struct wm8350_gpio_data *wm8350_gpio = gpiochip_get_data(chip);
 83	struct wm8350 *wm8350 = wm8350_gpio->wm8350;
 84
 85	if (!wm8350->irq_base)
 86		return -EINVAL;
 87
 88	return wm8350->irq_base + WM8350_IRQ_GPIO(offset);
 89}
 90
 91static const struct gpio_chip template_chip = {
 92	.label			= "wm8350",
 93	.owner			= THIS_MODULE,
 94	.direction_input	= wm8350_gpio_direction_in,
 95	.get			= wm8350_gpio_get,
 96	.direction_output	= wm8350_gpio_direction_out,
 97	.set			= wm8350_gpio_set,
 98	.to_irq			= wm8350_gpio_to_irq,
 99	.can_sleep		= true,
100};
101
102static int wm8350_gpio_probe(struct platform_device *pdev)
103{
104	struct wm8350 *wm8350 = dev_get_drvdata(pdev->dev.parent);
105	struct wm8350_platform_data *pdata = dev_get_platdata(wm8350->dev);
106	struct wm8350_gpio_data *wm8350_gpio;
 
107
108	wm8350_gpio = devm_kzalloc(&pdev->dev, sizeof(*wm8350_gpio),
109				   GFP_KERNEL);
110	if (wm8350_gpio == NULL)
111		return -ENOMEM;
112
113	wm8350_gpio->wm8350 = wm8350;
114	wm8350_gpio->gpio_chip = template_chip;
115	wm8350_gpio->gpio_chip.ngpio = 13;
116	wm8350_gpio->gpio_chip.parent = &pdev->dev;
117	if (pdata && pdata->gpio_base)
118		wm8350_gpio->gpio_chip.base = pdata->gpio_base;
119	else
120		wm8350_gpio->gpio_chip.base = -1;
121
122	return devm_gpiochip_add_data(&pdev->dev, &wm8350_gpio->gpio_chip, wm8350_gpio);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
123}
124
125static struct platform_driver wm8350_gpio_driver = {
126	.driver.name	= "wm8350-gpio",
 
127	.probe		= wm8350_gpio_probe,
 
128};
129
130static int __init wm8350_gpio_init(void)
131{
132	return platform_driver_register(&wm8350_gpio_driver);
133}
134subsys_initcall(wm8350_gpio_init);
135
136static void __exit wm8350_gpio_exit(void)
137{
138	platform_driver_unregister(&wm8350_gpio_driver);
139}
140module_exit(wm8350_gpio_exit);
141
142MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
143MODULE_DESCRIPTION("GPIO interface for WM8350 PMICs");
144MODULE_LICENSE("GPL");
145MODULE_ALIAS("platform:wm8350-gpio");
v3.15
 
  1/*
  2 * gpiolib support for Wolfson WM835x PMICs
  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>
 16#include <linux/slab.h>
 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
 26struct wm8350_gpio_data {
 27	struct wm8350 *wm8350;
 28	struct gpio_chip gpio_chip;
 29};
 30
 31static inline struct wm8350_gpio_data *to_wm8350_gpio(struct gpio_chip *chip)
 32{
 33	return container_of(chip, struct wm8350_gpio_data, gpio_chip);
 34}
 35
 36static int wm8350_gpio_direction_in(struct gpio_chip *chip, unsigned offset)
 37{
 38	struct wm8350_gpio_data *wm8350_gpio = to_wm8350_gpio(chip);
 39	struct wm8350 *wm8350 = wm8350_gpio->wm8350;
 40
 41	return wm8350_set_bits(wm8350, WM8350_GPIO_CONFIGURATION_I_O,
 42			       1 << offset);
 43}
 44
 45static int wm8350_gpio_get(struct gpio_chip *chip, unsigned offset)
 46{
 47	struct wm8350_gpio_data *wm8350_gpio = to_wm8350_gpio(chip);
 48	struct wm8350 *wm8350 = wm8350_gpio->wm8350;
 49	int ret;
 50
 51	ret = wm8350_reg_read(wm8350, WM8350_GPIO_LEVEL);
 52	if (ret < 0)
 53		return ret;
 54
 55	if (ret & (1 << offset))
 56		return 1;
 57	else
 58		return 0;
 59}
 60
 61static void wm8350_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
 62{
 63	struct wm8350_gpio_data *wm8350_gpio = to_wm8350_gpio(chip);
 64	struct wm8350 *wm8350 = wm8350_gpio->wm8350;
 65
 66	if (value)
 67		wm8350_set_bits(wm8350, WM8350_GPIO_LEVEL, 1 << offset);
 68	else
 69		wm8350_clear_bits(wm8350, WM8350_GPIO_LEVEL, 1 << offset);
 70}
 71
 72static int wm8350_gpio_direction_out(struct gpio_chip *chip,
 73				     unsigned offset, int value)
 74{
 75	struct wm8350_gpio_data *wm8350_gpio = to_wm8350_gpio(chip);
 76	struct wm8350 *wm8350 = wm8350_gpio->wm8350;
 77	int ret;
 78
 79	ret = wm8350_clear_bits(wm8350, WM8350_GPIO_CONFIGURATION_I_O,
 80				1 << offset);
 81	if (ret < 0)
 82		return ret;
 83
 84	/* Don't have an atomic direction/value setup */
 85	wm8350_gpio_set(chip, offset, value);
 86
 87	return 0;
 88}
 89
 90static int wm8350_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
 91{
 92	struct wm8350_gpio_data *wm8350_gpio = to_wm8350_gpio(chip);
 93	struct wm8350 *wm8350 = wm8350_gpio->wm8350;
 94
 95	if (!wm8350->irq_base)
 96		return -EINVAL;
 97
 98	return wm8350->irq_base + WM8350_IRQ_GPIO(offset);
 99}
100
101static struct gpio_chip template_chip = {
102	.label			= "wm8350",
103	.owner			= THIS_MODULE,
104	.direction_input	= wm8350_gpio_direction_in,
105	.get			= wm8350_gpio_get,
106	.direction_output	= wm8350_gpio_direction_out,
107	.set			= wm8350_gpio_set,
108	.to_irq			= wm8350_gpio_to_irq,
109	.can_sleep		= true,
110};
111
112static int wm8350_gpio_probe(struct platform_device *pdev)
113{
114	struct wm8350 *wm8350 = dev_get_drvdata(pdev->dev.parent);
115	struct wm8350_platform_data *pdata = dev_get_platdata(wm8350->dev);
116	struct wm8350_gpio_data *wm8350_gpio;
117	int ret;
118
119	wm8350_gpio = devm_kzalloc(&pdev->dev, sizeof(*wm8350_gpio),
120				   GFP_KERNEL);
121	if (wm8350_gpio == NULL)
122		return -ENOMEM;
123
124	wm8350_gpio->wm8350 = wm8350;
125	wm8350_gpio->gpio_chip = template_chip;
126	wm8350_gpio->gpio_chip.ngpio = 13;
127	wm8350_gpio->gpio_chip.dev = &pdev->dev;
128	if (pdata && pdata->gpio_base)
129		wm8350_gpio->gpio_chip.base = pdata->gpio_base;
130	else
131		wm8350_gpio->gpio_chip.base = -1;
132
133	ret = gpiochip_add(&wm8350_gpio->gpio_chip);
134	if (ret < 0) {
135		dev_err(&pdev->dev, "Could not register gpiochip, %d\n", ret);
136		return ret;
137	}
138
139	platform_set_drvdata(pdev, wm8350_gpio);
140
141	return ret;
142}
143
144static int wm8350_gpio_remove(struct platform_device *pdev)
145{
146	struct wm8350_gpio_data *wm8350_gpio = platform_get_drvdata(pdev);
147
148	return gpiochip_remove(&wm8350_gpio->gpio_chip);
149}
150
151static struct platform_driver wm8350_gpio_driver = {
152	.driver.name	= "wm8350-gpio",
153	.driver.owner	= THIS_MODULE,
154	.probe		= wm8350_gpio_probe,
155	.remove		= wm8350_gpio_remove,
156};
157
158static int __init wm8350_gpio_init(void)
159{
160	return platform_driver_register(&wm8350_gpio_driver);
161}
162subsys_initcall(wm8350_gpio_init);
163
164static void __exit wm8350_gpio_exit(void)
165{
166	platform_driver_unregister(&wm8350_gpio_driver);
167}
168module_exit(wm8350_gpio_exit);
169
170MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
171MODULE_DESCRIPTION("GPIO interface for WM8350 PMICs");
172MODULE_LICENSE("GPL");
173MODULE_ALIAS("platform:wm8350-gpio");