Linux Audio

Check our new training course

Loading...
v6.2
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * GPIO Driver for Dialog DA9052 PMICs.
  4 *
  5 * Copyright(c) 2011 Dialog Semiconductor Ltd.
  6 *
  7 * Author: David Dajun Chen <dchen@diasemi.com>
 
 
 
 
 
 
  8 */
  9#include <linux/fs.h>
 10#include <linux/gpio/driver.h>
 11#include <linux/module.h>
 
 
 12#include <linux/platform_device.h>
 
 13#include <linux/syscalls.h>
 14#include <linux/uaccess.h>
 15
 16#include <linux/mfd/da9052/da9052.h>
 17#include <linux/mfd/da9052/pdata.h>
 18#include <linux/mfd/da9052/reg.h>
 
 
 19
 20#define DA9052_INPUT				1
 21#define DA9052_OUTPUT_OPENDRAIN		2
 22#define DA9052_OUTPUT_PUSHPULL			3
 23
 24#define DA9052_SUPPLY_VDD_IO1			0
 25
 26#define DA9052_DEBOUNCING_OFF			0
 27#define DA9052_DEBOUNCING_ON			1
 28
 29#define DA9052_OUTPUT_LOWLEVEL			0
 30
 31#define DA9052_ACTIVE_LOW			0
 32#define DA9052_ACTIVE_HIGH			1
 33
 34#define DA9052_GPIO_MAX_PORTS_PER_REGISTER	8
 35#define DA9052_GPIO_SHIFT_COUNT(no)		(no%8)
 36#define DA9052_GPIO_MASK_UPPER_NIBBLE		0xF0
 37#define DA9052_GPIO_MASK_LOWER_NIBBLE		0x0F
 38#define DA9052_GPIO_NIBBLE_SHIFT		4
 39#define DA9052_IRQ_GPI0			16
 40#define DA9052_GPIO_ODD_SHIFT			7
 41#define DA9052_GPIO_EVEN_SHIFT			3
 42
 43struct da9052_gpio {
 44	struct da9052 *da9052;
 45	struct gpio_chip gp;
 46};
 47
 
 
 
 
 
 48static unsigned char da9052_gpio_port_odd(unsigned offset)
 49{
 50	return offset % 2;
 51}
 52
 53static int da9052_gpio_get(struct gpio_chip *gc, unsigned offset)
 54{
 55	struct da9052_gpio *gpio = gpiochip_get_data(gc);
 56	int da9052_port_direction = 0;
 57	int ret;
 58
 59	ret = da9052_reg_read(gpio->da9052,
 60			      DA9052_GPIO_0_1_REG + (offset >> 1));
 61	if (ret < 0)
 62		return ret;
 63
 64	if (da9052_gpio_port_odd(offset)) {
 65		da9052_port_direction = ret & DA9052_GPIO_ODD_PORT_PIN;
 66		da9052_port_direction >>= 4;
 67	} else {
 68		da9052_port_direction = ret & DA9052_GPIO_EVEN_PORT_PIN;
 69	}
 70
 71	switch (da9052_port_direction) {
 72	case DA9052_INPUT:
 73		if (offset < DA9052_GPIO_MAX_PORTS_PER_REGISTER)
 74			ret = da9052_reg_read(gpio->da9052,
 75					      DA9052_STATUS_C_REG);
 76		else
 77			ret = da9052_reg_read(gpio->da9052,
 78					      DA9052_STATUS_D_REG);
 79		if (ret < 0)
 80			return ret;
 81		return !!(ret & (1 << DA9052_GPIO_SHIFT_COUNT(offset)));
 
 
 
 82	case DA9052_OUTPUT_PUSHPULL:
 83		if (da9052_gpio_port_odd(offset))
 84			return !!(ret & DA9052_GPIO_ODD_PORT_MODE);
 85		else
 86			return !!(ret & DA9052_GPIO_EVEN_PORT_MODE);
 87	default:
 88		return -EINVAL;
 89	}
 90}
 91
 92static void da9052_gpio_set(struct gpio_chip *gc, unsigned offset, int value)
 93{
 94	struct da9052_gpio *gpio = gpiochip_get_data(gc);
 
 95	int ret;
 96
 97	if (da9052_gpio_port_odd(offset)) {
 
 
 98			ret = da9052_reg_update(gpio->da9052, (offset >> 1) +
 99						DA9052_GPIO_0_1_REG,
100						DA9052_GPIO_ODD_PORT_MODE,
101						value << DA9052_GPIO_ODD_SHIFT);
102			if (ret != 0)
103				dev_err(gpio->da9052->dev,
104					"Failed to updated gpio odd reg,%d",
105					ret);
 
106	} else {
 
 
107			ret = da9052_reg_update(gpio->da9052, (offset >> 1) +
108						DA9052_GPIO_0_1_REG,
109						DA9052_GPIO_EVEN_PORT_MODE,
110						value << DA9052_GPIO_EVEN_SHIFT);
111			if (ret != 0)
112				dev_err(gpio->da9052->dev,
113					"Failed to updated gpio even reg,%d",
114					ret);
 
115	}
116}
117
118static int da9052_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
119{
120	struct da9052_gpio *gpio = gpiochip_get_data(gc);
121	unsigned char register_value;
122	int ret;
123
124	/* Format: function - 2 bits type - 1 bit mode - 1 bit */
125	register_value = DA9052_INPUT | DA9052_ACTIVE_LOW << 2 |
126			 DA9052_DEBOUNCING_ON << 3;
127
128	if (da9052_gpio_port_odd(offset))
129		ret = da9052_reg_update(gpio->da9052, (offset >> 1) +
130					DA9052_GPIO_0_1_REG,
131					DA9052_GPIO_MASK_UPPER_NIBBLE,
132					(register_value <<
133					DA9052_GPIO_NIBBLE_SHIFT));
134	else
135		ret = da9052_reg_update(gpio->da9052, (offset >> 1) +
136					DA9052_GPIO_0_1_REG,
137					DA9052_GPIO_MASK_LOWER_NIBBLE,
138					register_value);
139
140	return ret;
141}
142
143static int da9052_gpio_direction_output(struct gpio_chip *gc,
144					unsigned offset, int value)
145{
146	struct da9052_gpio *gpio = gpiochip_get_data(gc);
147	unsigned char register_value;
148	int ret;
149
150	/* Format: Function - 2 bits Type - 1 bit Mode - 1 bit */
151	register_value = DA9052_OUTPUT_PUSHPULL | DA9052_SUPPLY_VDD_IO1 << 2 |
152			 value << 3;
153
154	if (da9052_gpio_port_odd(offset))
155		ret = da9052_reg_update(gpio->da9052, (offset >> 1) +
156					DA9052_GPIO_0_1_REG,
157					DA9052_GPIO_MASK_UPPER_NIBBLE,
158					(register_value <<
159					DA9052_GPIO_NIBBLE_SHIFT));
160	else
161		ret = da9052_reg_update(gpio->da9052, (offset >> 1) +
162					DA9052_GPIO_0_1_REG,
163					DA9052_GPIO_MASK_LOWER_NIBBLE,
164					register_value);
165
166	return ret;
167}
168
169static int da9052_gpio_to_irq(struct gpio_chip *gc, u32 offset)
170{
171	struct da9052_gpio *gpio = gpiochip_get_data(gc);
172	struct da9052 *da9052 = gpio->da9052;
173
174	int irq;
175
176	irq = regmap_irq_get_virq(da9052->irq_data, DA9052_IRQ_GPI0 + offset);
177
178	return irq;
179}
180
181static const struct gpio_chip reference_gp = {
182	.label = "da9052-gpio",
183	.owner = THIS_MODULE,
184	.get = da9052_gpio_get,
185	.set = da9052_gpio_set,
186	.direction_input = da9052_gpio_direction_input,
187	.direction_output = da9052_gpio_direction_output,
188	.to_irq = da9052_gpio_to_irq,
189	.can_sleep = true,
190	.ngpio = 16,
191	.base = -1,
192};
193
194static int da9052_gpio_probe(struct platform_device *pdev)
195{
196	struct da9052_gpio *gpio;
197	struct da9052_pdata *pdata;
 
198
199	gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
200	if (!gpio)
201		return -ENOMEM;
202
203	gpio->da9052 = dev_get_drvdata(pdev->dev.parent);
204	pdata = dev_get_platdata(gpio->da9052->dev);
205
206	gpio->gp = reference_gp;
207	if (pdata && pdata->gpio_base)
208		gpio->gp.base = pdata->gpio_base;
209
210	return devm_gpiochip_add_data(&pdev->dev, &gpio->gp, gpio);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
211}
212
213static struct platform_driver da9052_gpio_driver = {
214	.probe = da9052_gpio_probe,
 
215	.driver = {
216		.name	= "da9052-gpio",
 
217	},
218};
219
220module_platform_driver(da9052_gpio_driver);
 
 
 
 
 
 
 
 
 
 
221
222MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>");
223MODULE_DESCRIPTION("DA9052 GPIO Device Driver");
224MODULE_LICENSE("GPL");
225MODULE_ALIAS("platform:da9052-gpio");
v3.1
 
  1/*
  2 * GPIO Driver for Dialog DA9052 PMICs.
  3 *
  4 * Copyright(c) 2011 Dialog Semiconductor Ltd.
  5 *
  6 * Author: David Dajun Chen <dchen@diasemi.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#include <linux/module.h>
 15#include <linux/fs.h>
 16#include <linux/uaccess.h>
 17#include <linux/platform_device.h>
 18#include <linux/gpio.h>
 19#include <linux/syscalls.h>
 20#include <linux/seq_file.h>
 21
 22#include <linux/mfd/da9052/da9052.h>
 
 23#include <linux/mfd/da9052/reg.h>
 24#include <linux/mfd/da9052/pdata.h>
 25#include <linux/mfd/da9052/gpio.h>
 26
 27#define DA9052_INPUT				1
 28#define DA9052_OUTPUT_OPENDRAIN		2
 29#define DA9052_OUTPUT_PUSHPULL			3
 30
 31#define DA9052_SUPPLY_VDD_IO1			0
 32
 33#define DA9052_DEBOUNCING_OFF			0
 34#define DA9052_DEBOUNCING_ON			1
 35
 36#define DA9052_OUTPUT_LOWLEVEL			0
 37
 38#define DA9052_ACTIVE_LOW			0
 39#define DA9052_ACTIVE_HIGH			1
 40
 41#define DA9052_GPIO_MAX_PORTS_PER_REGISTER	8
 42#define DA9052_GPIO_SHIFT_COUNT(no)		(no%8)
 43#define DA9052_GPIO_MASK_UPPER_NIBBLE		0xF0
 44#define DA9052_GPIO_MASK_LOWER_NIBBLE		0x0F
 45#define DA9052_GPIO_NIBBLE_SHIFT		4
 
 
 
 46
 47struct da9052_gpio {
 48	struct da9052 *da9052;
 49	struct gpio_chip gp;
 50};
 51
 52static inline struct da9052_gpio *to_da9052_gpio(struct gpio_chip *chip)
 53{
 54	return container_of(chip, struct da9052_gpio, gp);
 55}
 56
 57static unsigned char da9052_gpio_port_odd(unsigned offset)
 58{
 59	return offset % 2;
 60}
 61
 62static int da9052_gpio_get(struct gpio_chip *gc, unsigned offset)
 63{
 64	struct da9052_gpio *gpio = to_da9052_gpio(gc);
 65	int da9052_port_direction = 0;
 66	int ret;
 67
 68	ret = da9052_reg_read(gpio->da9052,
 69			      DA9052_GPIO_0_1_REG + (offset >> 1));
 70	if (ret < 0)
 71		return ret;
 72
 73	if (da9052_gpio_port_odd(offset)) {
 74		da9052_port_direction = ret & DA9052_GPIO_ODD_PORT_PIN;
 75		da9052_port_direction >>= 4;
 76	} else {
 77		da9052_port_direction = ret & DA9052_GPIO_EVEN_PORT_PIN;
 78	}
 79
 80	switch (da9052_port_direction) {
 81	case DA9052_INPUT:
 82		if (offset < DA9052_GPIO_MAX_PORTS_PER_REGISTER)
 83			ret = da9052_reg_read(gpio->da9052,
 84					      DA9052_STATUS_C_REG);
 85		else
 86			ret = da9052_reg_read(gpio->da9052,
 87					      DA9052_STATUS_D_REG);
 88		if (ret < 0)
 89			return ret;
 90		if (ret & (1 << DA9052_GPIO_SHIFT_COUNT(offset)))
 91			return 1;
 92		else
 93			return 0;
 94	case DA9052_OUTPUT_PUSHPULL:
 95		if (da9052_gpio_port_odd(offset))
 96			return ret & DA9052_GPIO_ODD_PORT_MODE;
 97		else
 98			return ret & DA9052_GPIO_EVEN_PORT_MODE;
 99	default:
100		return -EINVAL;
101	}
102}
103
104static void da9052_gpio_set(struct gpio_chip *gc, unsigned offset, int value)
105{
106	struct da9052_gpio *gpio = to_da9052_gpio(gc);
107	unsigned char register_value = 0;
108	int ret;
109
110	if (da9052_gpio_port_odd(offset)) {
111		if (value) {
112			register_value = DA9052_GPIO_ODD_PORT_MODE;
113			ret = da9052_reg_update(gpio->da9052, (offset >> 1) +
114						DA9052_GPIO_0_1_REG,
115						DA9052_GPIO_ODD_PORT_MODE,
116						register_value);
117			if (ret != 0)
118				dev_err(gpio->da9052->dev,
119					"Failed to updated gpio odd reg,%d",
120					ret);
121		}
122	} else {
123		if (value) {
124			register_value = DA9052_GPIO_EVEN_PORT_MODE;
125			ret = da9052_reg_update(gpio->da9052, (offset >> 1) +
126						DA9052_GPIO_0_1_REG,
127						DA9052_GPIO_EVEN_PORT_MODE,
128						register_value);
129			if (ret != 0)
130				dev_err(gpio->da9052->dev,
131					"Failed to updated gpio even reg,%d",
132					ret);
133		}
134	}
135}
136
137static int da9052_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
138{
139	struct da9052_gpio *gpio = to_da9052_gpio(gc);
140	unsigned char register_value;
141	int ret;
142
143	/* Format: function - 2 bits type - 1 bit mode - 1 bit */
144	register_value = DA9052_INPUT | DA9052_ACTIVE_LOW << 2 |
145			 DA9052_DEBOUNCING_ON << 3;
146
147	if (da9052_gpio_port_odd(offset))
148		ret = da9052_reg_update(gpio->da9052, (offset >> 1) +
149					DA9052_GPIO_0_1_REG,
150					DA9052_GPIO_MASK_UPPER_NIBBLE,
151					(register_value <<
152					DA9052_GPIO_NIBBLE_SHIFT));
153	else
154		ret = da9052_reg_update(gpio->da9052, (offset >> 1) +
155					DA9052_GPIO_0_1_REG,
156					DA9052_GPIO_MASK_LOWER_NIBBLE,
157					register_value);
158
159	return ret;
160}
161
162static int da9052_gpio_direction_output(struct gpio_chip *gc,
163					unsigned offset, int value)
164{
165	struct da9052_gpio *gpio = to_da9052_gpio(gc);
166	unsigned char register_value;
167	int ret;
168
169	/* Format: Function - 2 bits Type - 1 bit Mode - 1 bit */
170	register_value = DA9052_OUTPUT_PUSHPULL | DA9052_SUPPLY_VDD_IO1 << 2 |
171			 value << 3;
172
173	if (da9052_gpio_port_odd(offset))
174		ret = da9052_reg_update(gpio->da9052, (offset >> 1) +
175					DA9052_GPIO_0_1_REG,
176					DA9052_GPIO_MASK_UPPER_NIBBLE,
177					(register_value <<
178					DA9052_GPIO_NIBBLE_SHIFT));
179	else
180		ret = da9052_reg_update(gpio->da9052, (offset >> 1) +
181					DA9052_GPIO_0_1_REG,
182					DA9052_GPIO_MASK_LOWER_NIBBLE,
183					register_value);
184
185	return ret;
186}
187
188static int da9052_gpio_to_irq(struct gpio_chip *gc, u32 offset)
189{
190	struct da9052_gpio *gpio = to_da9052_gpio(gc);
191	struct da9052 *da9052 = gpio->da9052;
192
193	return da9052->irq_base + DA9052_IRQ_GPI0 + offset;
 
 
 
 
194}
195
196static struct gpio_chip reference_gp __devinitdata = {
197	.label = "da9052-gpio",
198	.owner = THIS_MODULE,
199	.get = da9052_gpio_get,
200	.set = da9052_gpio_set,
201	.direction_input = da9052_gpio_direction_input,
202	.direction_output = da9052_gpio_direction_output,
203	.to_irq = da9052_gpio_to_irq,
204	.can_sleep = 1;
205	.ngpio = 16;
206	.base = -1;
207};
208
209static int __devinit da9052_gpio_probe(struct platform_device *pdev)
210{
211	struct da9052_gpio *gpio;
212	struct da9052_pdata *pdata;
213	int ret;
214
215	gpio = kzalloc(sizeof(*gpio), GFP_KERNEL);
216	if (gpio == NULL)
217		return -ENOMEM;
218
219	gpio->da9052 = dev_get_drvdata(pdev->dev.parent);
220	pdata = gpio->da9052->dev->platform_data;
221
222	gpio->gp = reference_gp;
223	if (pdata && pdata->gpio_base)
224		gpio->gp.base = pdata->gpio_base;
225
226	ret = gpiochip_add(&gpio->gp);
227	if (ret < 0) {
228		dev_err(&pdev->dev, "Could not register gpiochip, %d\n", ret);
229		goto err_mem;
230	}
231
232	platform_set_drvdata(pdev, gpio);
233
234	return 0;
235
236err_mem:
237	kfree(gpio);
238	return ret;
239}
240
241static int __devexit da9052_gpio_remove(struct platform_device *pdev)
242{
243	struct da9052_gpio *gpio = platform_get_drvdata(pdev);
244	int ret;
245
246	ret = gpiochip_remove(&gpio->gp);
247	if (ret == 0)
248		kfree(gpio);
249
250	return ret;
251}
252
253static struct platform_driver da9052_gpio_driver = {
254	.probe = da9052_gpio_probe,
255	.remove = __devexit_p(da9052_gpio_remove),
256	.driver = {
257		.name	= "da9052-gpio",
258		.owner	= THIS_MODULE,
259	},
260};
261
262static int __init da9052_gpio_init(void)
263{
264	return platform_driver_register(&da9052_gpio_driver);
265}
266module_init(da9052_gpio_init);
267
268static void __exit da9052_gpio_exit(void)
269{
270	return platform_driver_unregister(&da9052_gpio_driver);
271}
272module_exit(da9052_gpio_exit);
273
274MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>");
275MODULE_DESCRIPTION("DA9052 GPIO Device Driver");
276MODULE_LICENSE("GPL");
277MODULE_ALIAS("platform:da9052-gpio");