Linux Audio

Check our new training course

Loading...
v3.1
 
  1/*
  2 * MC33880 high-side/low-side switch GPIO driver
  3 * Copyright (c) 2009 Intel Corporation
  4 *
  5 * This program is free software; you can redistribute it and/or modify
  6 * it under the terms of the GNU General Public License version 2 as
  7 * published by the Free Software Foundation.
  8 *
  9 * This program is distributed in the hope that it will be useful,
 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 12 * GNU General Public License for more details.
 13 *
 14 * You should have received a copy of the GNU General Public License
 15 * along with this program; if not, write to the Free Software
 16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 17 */
 18
 19/* Supports:
 20 * Freescale MC33880 high-side/low-side switch
 21 */
 22
 23#include <linux/init.h>
 24#include <linux/mutex.h>
 25#include <linux/spi/spi.h>
 26#include <linux/spi/mc33880.h>
 27#include <linux/gpio.h>
 28#include <linux/slab.h>
 
 29
 30#define DRIVER_NAME "mc33880"
 31
 32/*
 33 * Pin configurations, see MAX7301 datasheet page 6
 34 */
 35#define PIN_CONFIG_MASK 0x03
 36#define PIN_CONFIG_IN_PULLUP 0x03
 37#define PIN_CONFIG_IN_WO_PULLUP 0x02
 38#define PIN_CONFIG_OUT 0x01
 39
 40#define PIN_NUMBER 8
 41
 42
 43/*
 44 * Some registers must be read back to modify.
 45 * To save time we cache them here in memory
 46 */
 47struct mc33880 {
 48	struct mutex	lock;	/* protect from simultaneous accesses */
 49	u8		port_config;
 50	struct gpio_chip chip;
 51	struct spi_device *spi;
 52};
 53
 54static int mc33880_write_config(struct mc33880 *mc)
 55{
 56	return spi_write(mc->spi, &mc->port_config, sizeof(mc->port_config));
 57}
 58
 59
 60static int __mc33880_set(struct mc33880 *mc, unsigned offset, int value)
 61{
 62	if (value)
 63		mc->port_config |= 1 << offset;
 64	else
 65		mc->port_config &= ~(1 << offset);
 66
 67	return mc33880_write_config(mc);
 68}
 69
 70
 71static void mc33880_set(struct gpio_chip *chip, unsigned offset, int value)
 72{
 73	struct mc33880 *mc = container_of(chip, struct mc33880, chip);
 74
 75	mutex_lock(&mc->lock);
 76
 77	__mc33880_set(mc, offset, value);
 78
 79	mutex_unlock(&mc->lock);
 80}
 81
 82static int __devinit mc33880_probe(struct spi_device *spi)
 83{
 84	struct mc33880 *mc;
 85	struct mc33880_platform_data *pdata;
 86	int ret;
 87
 88	pdata = spi->dev.platform_data;
 89	if (!pdata || !pdata->base) {
 90		dev_dbg(&spi->dev, "incorrect or missing platform data\n");
 91		return -EINVAL;
 92	}
 93
 94	/*
 95	 * bits_per_word cannot be configured in platform data
 96	 */
 97	spi->bits_per_word = 8;
 98
 99	ret = spi_setup(spi);
100	if (ret < 0)
101		return ret;
102
103	mc = kzalloc(sizeof(struct mc33880), GFP_KERNEL);
104	if (!mc)
105		return -ENOMEM;
106
107	mutex_init(&mc->lock);
108
109	dev_set_drvdata(&spi->dev, mc);
110
111	mc->spi = spi;
112
113	mc->chip.label = DRIVER_NAME,
114	mc->chip.set = mc33880_set;
115	mc->chip.base = pdata->base;
116	mc->chip.ngpio = PIN_NUMBER;
117	mc->chip.can_sleep = 1;
118	mc->chip.dev = &spi->dev;
119	mc->chip.owner = THIS_MODULE;
120
121	mc->port_config = 0x00;
122	/* write twice, because during initialisation the first setting
123	 * is just for testing SPI communication, and the second is the
124	 * "real" configuration
125	 */
126	ret = mc33880_write_config(mc);
127	mc->port_config = 0x00;
128	if (!ret)
129		ret = mc33880_write_config(mc);
130
131	if (ret) {
132		printk(KERN_ERR "Failed writing to " DRIVER_NAME ": %d\n", ret);
 
133		goto exit_destroy;
134	}
135
136	ret = gpiochip_add(&mc->chip);
137	if (ret)
138		goto exit_destroy;
139
140	return ret;
141
142exit_destroy:
143	dev_set_drvdata(&spi->dev, NULL);
144	mutex_destroy(&mc->lock);
145	kfree(mc);
146	return ret;
147}
148
149static int __devexit mc33880_remove(struct spi_device *spi)
150{
151	struct mc33880 *mc;
152	int ret;
153
154	mc = dev_get_drvdata(&spi->dev);
155	if (mc == NULL)
156		return -ENODEV;
157
158	dev_set_drvdata(&spi->dev, NULL);
159
160	ret = gpiochip_remove(&mc->chip);
161	if (!ret) {
162		mutex_destroy(&mc->lock);
163		kfree(mc);
164	} else
165		dev_err(&spi->dev, "Failed to remove the GPIO controller: %d\n",
166			ret);
167
168	return ret;
 
169}
170
171static struct spi_driver mc33880_driver = {
172	.driver = {
173		.name		= DRIVER_NAME,
174		.owner		= THIS_MODULE,
175	},
176	.probe		= mc33880_probe,
177	.remove		= __devexit_p(mc33880_remove),
178};
179
180static int __init mc33880_init(void)
181{
182	return spi_register_driver(&mc33880_driver);
183}
184/* register after spi postcore initcall and before
185 * subsys initcalls that may rely on these GPIOs
186 */
187subsys_initcall(mc33880_init);
188
189static void __exit mc33880_exit(void)
190{
191	spi_unregister_driver(&mc33880_driver);
192}
193module_exit(mc33880_exit);
194
195MODULE_AUTHOR("Mocean Laboratories <info@mocean-labs.com>");
196MODULE_LICENSE("GPL v2");
197
v6.9.4
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * MC33880 high-side/low-side switch GPIO driver
  4 * Copyright (c) 2009 Intel Corporation
 
 
 
 
 
 
 
 
 
 
 
 
 
  5 */
  6
  7/* Supports:
  8 * Freescale MC33880 high-side/low-side switch
  9 */
 10
 11#include <linux/init.h>
 12#include <linux/mutex.h>
 13#include <linux/spi/spi.h>
 14#include <linux/spi/mc33880.h>
 15#include <linux/gpio/driver.h>
 16#include <linux/slab.h>
 17#include <linux/module.h>
 18
 19#define DRIVER_NAME "mc33880"
 20
 21/*
 22 * Pin configurations, see MAX7301 datasheet page 6
 23 */
 24#define PIN_CONFIG_MASK 0x03
 25#define PIN_CONFIG_IN_PULLUP 0x03
 26#define PIN_CONFIG_IN_WO_PULLUP 0x02
 27#define PIN_CONFIG_OUT 0x01
 28
 29#define PIN_NUMBER 8
 30
 31
 32/*
 33 * Some registers must be read back to modify.
 34 * To save time we cache them here in memory
 35 */
 36struct mc33880 {
 37	struct mutex	lock;	/* protect from simultaneous accesses */
 38	u8		port_config;
 39	struct gpio_chip chip;
 40	struct spi_device *spi;
 41};
 42
 43static int mc33880_write_config(struct mc33880 *mc)
 44{
 45	return spi_write(mc->spi, &mc->port_config, sizeof(mc->port_config));
 46}
 47
 48
 49static int __mc33880_set(struct mc33880 *mc, unsigned offset, int value)
 50{
 51	if (value)
 52		mc->port_config |= 1 << offset;
 53	else
 54		mc->port_config &= ~(1 << offset);
 55
 56	return mc33880_write_config(mc);
 57}
 58
 59
 60static void mc33880_set(struct gpio_chip *chip, unsigned offset, int value)
 61{
 62	struct mc33880 *mc = gpiochip_get_data(chip);
 63
 64	mutex_lock(&mc->lock);
 65
 66	__mc33880_set(mc, offset, value);
 67
 68	mutex_unlock(&mc->lock);
 69}
 70
 71static int mc33880_probe(struct spi_device *spi)
 72{
 73	struct mc33880 *mc;
 74	struct mc33880_platform_data *pdata;
 75	int ret;
 76
 77	pdata = dev_get_platdata(&spi->dev);
 78	if (!pdata || !pdata->base) {
 79		dev_dbg(&spi->dev, "incorrect or missing platform data\n");
 80		return -EINVAL;
 81	}
 82
 83	/*
 84	 * bits_per_word cannot be configured in platform data
 85	 */
 86	spi->bits_per_word = 8;
 87
 88	ret = spi_setup(spi);
 89	if (ret < 0)
 90		return ret;
 91
 92	mc = devm_kzalloc(&spi->dev, sizeof(struct mc33880), GFP_KERNEL);
 93	if (!mc)
 94		return -ENOMEM;
 95
 96	mutex_init(&mc->lock);
 97
 98	spi_set_drvdata(spi, mc);
 99
100	mc->spi = spi;
101
102	mc->chip.label = DRIVER_NAME,
103	mc->chip.set = mc33880_set;
104	mc->chip.base = pdata->base;
105	mc->chip.ngpio = PIN_NUMBER;
106	mc->chip.can_sleep = true;
107	mc->chip.parent = &spi->dev;
108	mc->chip.owner = THIS_MODULE;
109
110	mc->port_config = 0x00;
111	/* write twice, because during initialisation the first setting
112	 * is just for testing SPI communication, and the second is the
113	 * "real" configuration
114	 */
115	ret = mc33880_write_config(mc);
116	mc->port_config = 0x00;
117	if (!ret)
118		ret = mc33880_write_config(mc);
119
120	if (ret) {
121		dev_err(&spi->dev, "Failed writing to " DRIVER_NAME ": %d\n",
122			ret);
123		goto exit_destroy;
124	}
125
126	ret = gpiochip_add_data(&mc->chip, mc);
127	if (ret)
128		goto exit_destroy;
129
130	return ret;
131
132exit_destroy:
 
133	mutex_destroy(&mc->lock);
 
134	return ret;
135}
136
137static void mc33880_remove(struct spi_device *spi)
138{
139	struct mc33880 *mc;
 
140
141	mc = spi_get_drvdata(spi);
 
 
 
 
 
 
 
 
 
 
 
 
142
143	gpiochip_remove(&mc->chip);
144	mutex_destroy(&mc->lock);
145}
146
147static struct spi_driver mc33880_driver = {
148	.driver = {
149		.name		= DRIVER_NAME,
 
150	},
151	.probe		= mc33880_probe,
152	.remove		= mc33880_remove,
153};
154
155static int __init mc33880_init(void)
156{
157	return spi_register_driver(&mc33880_driver);
158}
159/* register after spi postcore initcall and before
160 * subsys initcalls that may rely on these GPIOs
161 */
162subsys_initcall(mc33880_init);
163
164static void __exit mc33880_exit(void)
165{
166	spi_unregister_driver(&mc33880_driver);
167}
168module_exit(mc33880_exit);
169
170MODULE_AUTHOR("Mocean Laboratories <info@mocean-labs.com>");
171MODULE_LICENSE("GPL v2");
172