Linux Audio

Check our new training course

Loading...
v6.2
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * Copyright 2015 Verifone Int.
  4 *
  5 * Author: Nicolas Saenz Julienne <nicolassaenzj@gmail.com>
  6 *
 
 
 
 
 
  7 * This driver is based on the gpio-tps65912 implementation.
  8 */
  9
 10#include <linux/kernel.h>
 11#include <linux/module.h>
 12#include <linux/errno.h>
 13#include <linux/gpio/driver.h>
 14#include <linux/platform_device.h>
 15#include <linux/regmap.h>
 16#include <linux/mfd/tps65218.h>
 17
 18struct tps65218_gpio {
 19	struct tps65218 *tps65218;
 20	struct gpio_chip gpio_chip;
 21};
 22
 23static int tps65218_gpio_get(struct gpio_chip *gc, unsigned offset)
 24{
 25	struct tps65218_gpio *tps65218_gpio = gpiochip_get_data(gc);
 26	struct tps65218 *tps65218 = tps65218_gpio->tps65218;
 27	unsigned int val;
 28	int ret;
 29
 30	ret = regmap_read(tps65218->regmap, TPS65218_REG_ENABLE2, &val);
 31	if (ret)
 32		return ret;
 33
 34	return !!(val & (TPS65218_ENABLE2_GPIO1 << offset));
 35}
 36
 37static void tps65218_gpio_set(struct gpio_chip *gc, unsigned offset,
 38			      int value)
 39{
 40	struct tps65218_gpio *tps65218_gpio = gpiochip_get_data(gc);
 41	struct tps65218 *tps65218 = tps65218_gpio->tps65218;
 42
 43	if (value)
 44		tps65218_set_bits(tps65218, TPS65218_REG_ENABLE2,
 45				  TPS65218_ENABLE2_GPIO1 << offset,
 46				  TPS65218_ENABLE2_GPIO1 << offset,
 47				  TPS65218_PROTECT_L1);
 48	else
 49		tps65218_clear_bits(tps65218, TPS65218_REG_ENABLE2,
 50				    TPS65218_ENABLE2_GPIO1 << offset,
 51				    TPS65218_PROTECT_L1);
 52}
 53
 54static int tps65218_gpio_output(struct gpio_chip *gc, unsigned offset,
 55				int value)
 56{
 57	/* Only drives GPOs */
 58	tps65218_gpio_set(gc, offset, value);
 59	return 0;
 60}
 61
 62static int tps65218_gpio_input(struct gpio_chip *gc, unsigned offset)
 63{
 64	return -EPERM;
 65}
 66
 67static int tps65218_gpio_request(struct gpio_chip *gc, unsigned offset)
 68{
 69	struct tps65218_gpio *tps65218_gpio = gpiochip_get_data(gc);
 70	struct tps65218 *tps65218 = tps65218_gpio->tps65218;
 71	int ret;
 72
 73	if (gpiochip_line_is_open_source(gc, offset)) {
 74		dev_err(gc->parent, "can't work as open source\n");
 75		return -EINVAL;
 76	}
 77
 78	switch (offset) {
 79	case 0:
 80		if (!gpiochip_line_is_open_drain(gc, offset)) {
 81			dev_err(gc->parent, "GPO1 works only as open drain\n");
 82			return -EINVAL;
 83		}
 84
 85		/* Disable sequencer for GPO1 */
 86		ret = tps65218_clear_bits(tps65218, TPS65218_REG_SEQ7,
 87					  TPS65218_SEQ7_GPO1_SEQ_MASK,
 88					  TPS65218_PROTECT_L1);
 89		if (ret)
 90			return ret;
 91
 92		/* Setup GPO1 */
 93		ret = tps65218_clear_bits(tps65218, TPS65218_REG_CONFIG1,
 94					  TPS65218_CONFIG1_IO1_SEL,
 95					  TPS65218_PROTECT_L1);
 96		if (ret)
 97			return ret;
 98
 99		break;
100	case 1:
 
 
 
 
 
 
 
 
 
 
101		/* Setup GPO2 */
102		ret = tps65218_clear_bits(tps65218, TPS65218_REG_CONFIG1,
103					  TPS65218_CONFIG1_IO1_SEL,
104					  TPS65218_PROTECT_L1);
105		if (ret)
106			return ret;
107
108		break;
109
110	case 2:
111		if (!gpiochip_line_is_open_drain(gc, offset)) {
112			dev_err(gc->parent, "GPO3 works only as open drain\n");
113			return -EINVAL;
114		}
115
116		/* Disable sequencer for GPO3 */
117		ret = tps65218_clear_bits(tps65218, TPS65218_REG_SEQ7,
118					  TPS65218_SEQ7_GPO3_SEQ_MASK,
119					  TPS65218_PROTECT_L1);
120		if (ret)
121			return ret;
122
123		/* Setup GPO3 */
124		ret = tps65218_clear_bits(tps65218, TPS65218_REG_CONFIG2,
125					  TPS65218_CONFIG2_DC12_RST,
126					  TPS65218_PROTECT_L1);
127		if (ret)
128			return ret;
129
130		break;
131	default:
132		return -EINVAL;
133	}
134
135	return 0;
136}
137
138static int tps65218_gpio_set_config(struct gpio_chip *gc, unsigned offset,
139				    unsigned long config)
140{
141	struct tps65218_gpio *tps65218_gpio = gpiochip_get_data(gc);
142	struct tps65218 *tps65218 = tps65218_gpio->tps65218;
143	enum pin_config_param param = pinconf_to_config_param(config);
144
145	switch (offset) {
146	case 0:
147	case 2:
148		/* GPO1 is hardwired to be open drain */
149		if (param == PIN_CONFIG_DRIVE_OPEN_DRAIN)
150			return 0;
151		return -ENOTSUPP;
152	case 1:
153		/* GPO2 is push-pull by default, can be set as open drain. */
154		if (param == PIN_CONFIG_DRIVE_OPEN_DRAIN)
155			return tps65218_clear_bits(tps65218,
156						   TPS65218_REG_CONFIG1,
157						   TPS65218_CONFIG1_GPO2_BUF,
158						   TPS65218_PROTECT_L1);
159		if (param == PIN_CONFIG_DRIVE_PUSH_PULL)
160			return tps65218_set_bits(tps65218,
161						 TPS65218_REG_CONFIG1,
162						 TPS65218_CONFIG1_GPO2_BUF,
163						 TPS65218_CONFIG1_GPO2_BUF,
164						 TPS65218_PROTECT_L1);
165		return -ENOTSUPP;
166	default:
167		break;
168	}
169	return -ENOTSUPP;
170}
171
172static const struct gpio_chip template_chip = {
173	.label			= "gpio-tps65218",
174	.owner			= THIS_MODULE,
175	.request		= tps65218_gpio_request,
176	.direction_output	= tps65218_gpio_output,
177	.direction_input	= tps65218_gpio_input,
178	.get			= tps65218_gpio_get,
179	.set			= tps65218_gpio_set,
180	.set_config		= tps65218_gpio_set_config,
181	.can_sleep		= true,
182	.ngpio			= 3,
183	.base			= -1,
184};
185
186static int tps65218_gpio_probe(struct platform_device *pdev)
187{
188	struct tps65218 *tps65218 = dev_get_drvdata(pdev->dev.parent);
189	struct tps65218_gpio *tps65218_gpio;
 
190
191	tps65218_gpio = devm_kzalloc(&pdev->dev, sizeof(*tps65218_gpio),
192				     GFP_KERNEL);
193	if (!tps65218_gpio)
194		return -ENOMEM;
195
196	tps65218_gpio->tps65218 = tps65218;
197	tps65218_gpio->gpio_chip = template_chip;
198	tps65218_gpio->gpio_chip.parent = &pdev->dev;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
199
200	return devm_gpiochip_add_data(&pdev->dev, &tps65218_gpio->gpio_chip,
201				      tps65218_gpio);
202}
203
204static const struct of_device_id tps65218_dt_match[] = {
205	{ .compatible = "ti,tps65218-gpio" },
206	{  }
207};
208MODULE_DEVICE_TABLE(of, tps65218_dt_match);
209
210static const struct platform_device_id tps65218_gpio_id_table[] = {
211	{ "tps65218-gpio", },
212	{ /* sentinel */ }
213};
214MODULE_DEVICE_TABLE(platform, tps65218_gpio_id_table);
215
216static struct platform_driver tps65218_gpio_driver = {
217	.driver = {
218		.name = "tps65218-gpio",
219		.of_match_table = of_match_ptr(tps65218_dt_match)
220	},
221	.probe = tps65218_gpio_probe,
222	.id_table = tps65218_gpio_id_table,
223};
224
225module_platform_driver(tps65218_gpio_driver);
226
227MODULE_AUTHOR("Nicolas Saenz Julienne <nicolassaenzj@gmail.com>");
228MODULE_DESCRIPTION("GPO interface for TPS65218 PMICs");
229MODULE_LICENSE("GPL v2");
v4.6
 
  1/*
  2 * Copyright 2015 Verifone Int.
  3 *
  4 * Author: Nicolas Saenz Julienne <nicolassaenzj@gmail.com>
  5 *
  6 * This program is free software; you can redistribute it and/or modify i t
  7 * under  the terms of the GNU General  Public License as published by th e
  8 * Free Software Foundation;  either version 2 of the License, or (at you r
  9 * option) any later version.
 10 *
 11 * This driver is based on the gpio-tps65912 implementation.
 12 */
 13
 14#include <linux/kernel.h>
 15#include <linux/module.h>
 16#include <linux/errno.h>
 17#include <linux/gpio/driver.h>
 18#include <linux/platform_device.h>
 
 19#include <linux/mfd/tps65218.h>
 20
 21struct tps65218_gpio {
 22	struct tps65218 *tps65218;
 23	struct gpio_chip gpio_chip;
 24};
 25
 26static int tps65218_gpio_get(struct gpio_chip *gc, unsigned offset)
 27{
 28	struct tps65218_gpio *tps65218_gpio = gpiochip_get_data(gc);
 29	struct tps65218 *tps65218 = tps65218_gpio->tps65218;
 30	unsigned int val;
 31	int ret;
 32
 33	ret = tps65218_reg_read(tps65218, TPS65218_REG_ENABLE2, &val);
 34	if (ret)
 35		return ret;
 36
 37	return !!(val & (TPS65218_ENABLE2_GPIO1 << offset));
 38}
 39
 40static void tps65218_gpio_set(struct gpio_chip *gc, unsigned offset,
 41			      int value)
 42{
 43	struct tps65218_gpio *tps65218_gpio = gpiochip_get_data(gc);
 44	struct tps65218 *tps65218 = tps65218_gpio->tps65218;
 45
 46	if (value)
 47		tps65218_set_bits(tps65218, TPS65218_REG_ENABLE2,
 48				  TPS65218_ENABLE2_GPIO1 << offset,
 49				  TPS65218_ENABLE2_GPIO1 << offset,
 50				  TPS65218_PROTECT_L1);
 51	else
 52		tps65218_clear_bits(tps65218, TPS65218_REG_ENABLE2,
 53				    TPS65218_ENABLE2_GPIO1 << offset,
 54				    TPS65218_PROTECT_L1);
 55}
 56
 57static int tps65218_gpio_output(struct gpio_chip *gc, unsigned offset,
 58				int value)
 59{
 60	/* Only drives GPOs */
 61	tps65218_gpio_set(gc, offset, value);
 62	return 0;
 63}
 64
 65static int tps65218_gpio_input(struct gpio_chip *gc, unsigned offset)
 66{
 67	return -EPERM;
 68}
 69
 70static int tps65218_gpio_request(struct gpio_chip *gc, unsigned offset)
 71{
 72	struct tps65218_gpio *tps65218_gpio = gpiochip_get_data(gc);
 73	struct tps65218 *tps65218 = tps65218_gpio->tps65218;
 74	int ret;
 75
 76	if (gpiochip_line_is_open_source(gc, offset)) {
 77		dev_err(gc->parent, "can't work as open source\n");
 78		return -EINVAL;
 79	}
 80
 81	switch (offset) {
 82	case 0:
 83		if (!gpiochip_line_is_open_drain(gc, offset)) {
 84			dev_err(gc->parent, "GPO1 works only as open drain\n");
 85			return -EINVAL;
 86		}
 87
 88		/* Disable sequencer for GPO1 */
 89		ret = tps65218_clear_bits(tps65218, TPS65218_REG_SEQ7,
 90					  TPS65218_SEQ7_GPO1_SEQ_MASK,
 91					  TPS65218_PROTECT_L1);
 92		if (ret)
 93			return ret;
 94
 95		/* Setup GPO1 */
 96		ret = tps65218_clear_bits(tps65218, TPS65218_REG_CONFIG1,
 97					  TPS65218_CONFIG1_IO1_SEL,
 98					  TPS65218_PROTECT_L1);
 99		if (ret)
100			return ret;
101
102		break;
103	case 1:
104		/* GP02 is push-pull by default, can be set as open drain. */
105		if (gpiochip_line_is_open_drain(gc, offset)) {
106			ret = tps65218_clear_bits(tps65218,
107						  TPS65218_REG_CONFIG1,
108						  TPS65218_CONFIG1_GPO2_BUF,
109						  TPS65218_PROTECT_L1);
110			if (ret)
111				return ret;
112		}
113
114		/* Setup GPO2 */
115		ret = tps65218_clear_bits(tps65218, TPS65218_REG_CONFIG1,
116					  TPS65218_CONFIG1_IO1_SEL,
117					  TPS65218_PROTECT_L1);
118		if (ret)
119			return ret;
120
121		break;
122
123	case 2:
124		if (!gpiochip_line_is_open_drain(gc, offset)) {
125			dev_err(gc->parent, "GPO3 works only as open drain\n");
126			return -EINVAL;
127		}
128
129		/* Disable sequencer for GPO3 */
130		ret = tps65218_clear_bits(tps65218, TPS65218_REG_SEQ7,
131					  TPS65218_SEQ7_GPO3_SEQ_MASK,
132					  TPS65218_PROTECT_L1);
133		if (ret)
134			return ret;
135
136		/* Setup GPO3 */
137		ret = tps65218_clear_bits(tps65218, TPS65218_REG_CONFIG2,
138					  TPS65218_CONFIG2_DC12_RST,
139					  TPS65218_PROTECT_L1);
140		if (ret)
141			return ret;
142
143		break;
144	default:
145		return -EINVAL;
146	}
147
148	return 0;
149}
150
151static struct gpio_chip template_chip = {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
152	.label			= "gpio-tps65218",
153	.owner			= THIS_MODULE,
154	.request		= tps65218_gpio_request,
155	.direction_output	= tps65218_gpio_output,
156	.direction_input	= tps65218_gpio_input,
157	.get			= tps65218_gpio_get,
158	.set			= tps65218_gpio_set,
 
159	.can_sleep		= true,
160	.ngpio			= 3,
161	.base			= -1,
162};
163
164static int tps65218_gpio_probe(struct platform_device *pdev)
165{
166	struct tps65218 *tps65218 = dev_get_drvdata(pdev->dev.parent);
167	struct tps65218_gpio *tps65218_gpio;
168	int ret;
169
170	tps65218_gpio = devm_kzalloc(&pdev->dev, sizeof(*tps65218_gpio),
171				     GFP_KERNEL);
172	if (!tps65218_gpio)
173		return -ENOMEM;
174
175	tps65218_gpio->tps65218 = tps65218;
176	tps65218_gpio->gpio_chip = template_chip;
177	tps65218_gpio->gpio_chip.parent = &pdev->dev;
178#ifdef CONFIG_OF_GPIO
179	tps65218_gpio->gpio_chip.of_node = pdev->dev.of_node;
180#endif
181
182	ret = gpiochip_add_data(&tps65218_gpio->gpio_chip, tps65218_gpio);
183	if (ret < 0) {
184		dev_err(&pdev->dev, "Failed to register gpiochip, %d\n", ret);
185		return ret;
186	}
187
188	platform_set_drvdata(pdev, tps65218_gpio);
189
190	return ret;
191}
192
193static int tps65218_gpio_remove(struct platform_device *pdev)
194{
195	struct tps65218_gpio *tps65218_gpio = platform_get_drvdata(pdev);
196
197	gpiochip_remove(&tps65218_gpio->gpio_chip);
198
199	return 0;
 
200}
201
202static const struct of_device_id tps65218_dt_match[] = {
203	{ .compatible = "ti,tps65218-gpio" },
204	{  }
205};
206MODULE_DEVICE_TABLE(of, tps65218_dt_match);
207
 
 
 
 
 
 
208static struct platform_driver tps65218_gpio_driver = {
209	.driver = {
210		.name = "tps65218-gpio",
211		.of_match_table = of_match_ptr(tps65218_dt_match)
212	},
213	.probe = tps65218_gpio_probe,
214	.remove = tps65218_gpio_remove,
215};
216
217module_platform_driver(tps65218_gpio_driver);
218
219MODULE_AUTHOR("Nicolas Saenz Julienne <nicolassaenzj@gmail.com>");
220MODULE_DESCRIPTION("GPO interface for TPS65218 PMICs");
221MODULE_LICENSE("GPL v2");
222MODULE_ALIAS("platform:tps65218-gpio");