| 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 |
*/ |
| 36 |
struct 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 |
|
| 43 |
static int mc33880_write_config(struct mc33880 *mc) |
| 44 |
{ |
| 45 |
return spi_write(mc->spi, &mc->port_config, sizeof(mc->port_config)); |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
static 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 |
|
| 60 |
static int mc33880_set(struct gpio_chip *chip, unsigned int offset, int value) |
| 61 |
{ |
| 62 |
struct mc33880 *mc = gpiochip_get_data(chip); |
| 63 |
int ret; |
| 64 |
|
| 65 |
mutex_lock(&mc->lock); |
| 66 |
|
| 67 |
ret = __mc33880_set(mc, offset, value); |
| 68 |
|
| 69 |
mutex_unlock(&mc->lock); |
| 70 |
|
| 71 |
return ret; |
| 72 |
} |
| 73 |
|
| 74 |
static int mc33880_probe(struct spi_device *spi) |
| 75 |
{ |
| 76 |
struct mc33880 *mc; |
| 77 |
struct mc33880_platform_data *pdata; |
| 78 |
int ret; |
| 79 |
|
| 80 |
pdata = dev_get_platdata(&spi->dev); |
| 81 |
if (!pdata || !pdata->base) { |
| 82 |
dev_dbg(&spi->dev, "incorrect or missing platform data\n"); |
| 83 |
return -EINVAL; |
| 84 |
} |
| 85 |
|
| 86 |
/* |
| 87 |
* bits_per_word cannot be configured in platform data |
| 88 |
*/ |
| 89 |
spi->bits_per_word = 8; |
| 90 |
|
| 91 |
ret = spi_setup(spi); |
| 92 |
if (ret < 0) |
| 93 |
return ret; |
| 94 |
|
| 95 |
mc = devm_kzalloc(&spi->dev, sizeof(struct mc33880), GFP_KERNEL); |
| 96 |
if (!mc) |
| 97 |
return -ENOMEM; |
| 98 |
|
| 99 |
mutex_init(&mc->lock); |
| 100 |
|
| 101 |
spi_set_drvdata(spi, mc); |
| 102 |
|
| 103 |
mc->spi = spi; |
| 104 |
|
| 105 |
mc->chip.label = DRIVER_NAME; |
| 106 |
mc->chip.set = mc33880_set; |
| 107 |
mc->chip.base = pdata->base; |
| 108 |
mc->chip.ngpio = PIN_NUMBER; |
| 109 |
mc->chip.can_sleep = true; |
| 110 |
mc->chip.parent = &spi->dev; |
| 111 |
mc->chip.owner = THIS_MODULE; |
| 112 |
|
| 113 |
mc->port_config = 0x00; |
| 114 |
/* write twice, because during initialisation the first setting |
| 115 |
* is just for testing SPI communication, and the second is the |
| 116 |
* "real" configuration |
| 117 |
*/ |
| 118 |
ret = mc33880_write_config(mc); |
| 119 |
mc->port_config = 0x00; |
| 120 |
if (!ret) |
| 121 |
ret = mc33880_write_config(mc); |
| 122 |
|
| 123 |
if (ret) { |
| 124 |
dev_err(&spi->dev, "Failed writing to " DRIVER_NAME ": %d\n", |
| 125 |
ret); |
| 126 |
goto exit_destroy; |
| 127 |
} |
| 128 |
|
| 129 |
ret = gpiochip_add_data(&mc->chip, mc); |
| 130 |
if (ret) |
| 131 |
goto exit_destroy; |
| 132 |
|
| 133 |
return ret; |
| 134 |
|
| 135 |
exit_destroy: |
| 136 |
mutex_destroy(&mc->lock); |
| 137 |
return ret; |
| 138 |
} |
| 139 |
|
| 140 |
static void mc33880_remove(struct spi_device *spi) |
| 141 |
{ |
| 142 |
struct mc33880 *mc; |
| 143 |
|
| 144 |
mc = spi_get_drvdata(spi); |
| 145 |
|
| 146 |
gpiochip_remove(&mc->chip); |
| 147 |
mutex_destroy(&mc->lock); |
| 148 |
} |
| 149 |
|
| 150 |
static struct spi_driver mc33880_driver = { |
| 151 |
.driver = { |
| 152 |
.name = DRIVER_NAME, |
| 153 |
}, |
| 154 |
.probe = mc33880_probe, |
| 155 |
.remove = mc33880_remove, |
| 156 |
}; |
| 157 |
|
| 158 |
static int __init mc33880_init(void) |
| 159 |
{ |
| 160 |
return spi_register_driver(&mc33880_driver); |
| 161 |
} |
| 162 |
/* register after spi postcore initcall and before |
| 163 |
* subsys initcalls that may rely on these GPIOs |
| 164 |
*/ |
| 165 |
subsys_initcall(mc33880_init); |
| 166 |
|
| 167 |
static void __exit mc33880_exit(void) |
| 168 |
{ |
| 169 |
spi_unregister_driver(&mc33880_driver); |
| 170 |
} |
| 171 |
module_exit(mc33880_exit); |
| 172 |
|
| 173 |
MODULE_AUTHOR("Mocean Laboratories <[email protected]>"); |
| 174 |
MODULE_DESCRIPTION("MC33880 high-side/low-side switch GPIO driver"); |
| 175 |
MODULE_LICENSE("GPL v2"); |
| 176 |
|