Richard Röjfors | 1e5db00 | 2009-09-22 16:46:34 -0700 | [diff] [blame] | 1 | /* |
Grant Likely | c103de2 | 2011-06-04 18:38:28 -0600 | [diff] [blame] | 2 | * MC33880 high-side/low-side switch GPIO driver |
Richard Röjfors | 1e5db00 | 2009-09-22 16:46:34 -0700 | [diff] [blame] | 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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 28 | #include <linux/slab.h> |
Paul Gortmaker | bb207ef | 2011-07-03 13:38:09 -0400 | [diff] [blame] | 29 | #include <linux/module.h> |
Richard Röjfors | 1e5db00 | 2009-09-22 16:46:34 -0700 | [diff] [blame] | 30 | |
| 31 | #define DRIVER_NAME "mc33880" |
| 32 | |
| 33 | /* |
| 34 | * Pin configurations, see MAX7301 datasheet page 6 |
| 35 | */ |
| 36 | #define PIN_CONFIG_MASK 0x03 |
| 37 | #define PIN_CONFIG_IN_PULLUP 0x03 |
| 38 | #define PIN_CONFIG_IN_WO_PULLUP 0x02 |
| 39 | #define PIN_CONFIG_OUT 0x01 |
| 40 | |
| 41 | #define PIN_NUMBER 8 |
| 42 | |
| 43 | |
| 44 | /* |
| 45 | * Some registers must be read back to modify. |
| 46 | * To save time we cache them here in memory |
| 47 | */ |
| 48 | struct mc33880 { |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 49 | struct mutex lock; /* protect from simultaneous accesses */ |
Richard Röjfors | 1e5db00 | 2009-09-22 16:46:34 -0700 | [diff] [blame] | 50 | u8 port_config; |
| 51 | struct gpio_chip chip; |
| 52 | struct spi_device *spi; |
| 53 | }; |
| 54 | |
| 55 | static int mc33880_write_config(struct mc33880 *mc) |
| 56 | { |
| 57 | return spi_write(mc->spi, &mc->port_config, sizeof(mc->port_config)); |
| 58 | } |
| 59 | |
| 60 | |
| 61 | static int __mc33880_set(struct mc33880 *mc, unsigned offset, int value) |
| 62 | { |
| 63 | if (value) |
| 64 | mc->port_config |= 1 << offset; |
| 65 | else |
| 66 | mc->port_config &= ~(1 << offset); |
| 67 | |
| 68 | return mc33880_write_config(mc); |
| 69 | } |
| 70 | |
| 71 | |
| 72 | static void mc33880_set(struct gpio_chip *chip, unsigned offset, int value) |
| 73 | { |
| 74 | struct mc33880 *mc = container_of(chip, struct mc33880, chip); |
| 75 | |
| 76 | mutex_lock(&mc->lock); |
| 77 | |
| 78 | __mc33880_set(mc, offset, value); |
| 79 | |
| 80 | mutex_unlock(&mc->lock); |
| 81 | } |
| 82 | |
Bill Pemberton | 3836309 | 2012-11-19 13:22:34 -0500 | [diff] [blame] | 83 | static int mc33880_probe(struct spi_device *spi) |
Richard Röjfors | 1e5db00 | 2009-09-22 16:46:34 -0700 | [diff] [blame] | 84 | { |
| 85 | struct mc33880 *mc; |
| 86 | struct mc33880_platform_data *pdata; |
| 87 | int ret; |
| 88 | |
Jingoo Han | e56aee1 | 2013-07-30 17:08:05 +0900 | [diff] [blame] | 89 | pdata = dev_get_platdata(&spi->dev); |
Richard Röjfors | 1e5db00 | 2009-09-22 16:46:34 -0700 | [diff] [blame] | 90 | if (!pdata || !pdata->base) { |
| 91 | dev_dbg(&spi->dev, "incorrect or missing platform data\n"); |
| 92 | return -EINVAL; |
| 93 | } |
| 94 | |
| 95 | /* |
| 96 | * bits_per_word cannot be configured in platform data |
| 97 | */ |
| 98 | spi->bits_per_word = 8; |
| 99 | |
| 100 | ret = spi_setup(spi); |
| 101 | if (ret < 0) |
| 102 | return ret; |
| 103 | |
Jingoo Han | 632d8e5 | 2013-03-15 18:15:49 +0900 | [diff] [blame] | 104 | mc = devm_kzalloc(&spi->dev, sizeof(struct mc33880), GFP_KERNEL); |
Richard Röjfors | 1e5db00 | 2009-09-22 16:46:34 -0700 | [diff] [blame] | 105 | if (!mc) |
| 106 | return -ENOMEM; |
| 107 | |
| 108 | mutex_init(&mc->lock); |
| 109 | |
Jingoo Han | 493294d | 2013-03-15 18:17:54 +0900 | [diff] [blame] | 110 | spi_set_drvdata(spi, mc); |
Richard Röjfors | 1e5db00 | 2009-09-22 16:46:34 -0700 | [diff] [blame] | 111 | |
| 112 | mc->spi = spi; |
| 113 | |
| 114 | mc->chip.label = DRIVER_NAME, |
| 115 | mc->chip.set = mc33880_set; |
| 116 | mc->chip.base = pdata->base; |
| 117 | mc->chip.ngpio = PIN_NUMBER; |
Linus Walleij | 9fb1f39 | 2013-12-04 14:42:46 +0100 | [diff] [blame] | 118 | mc->chip.can_sleep = true; |
Richard Röjfors | 1e5db00 | 2009-09-22 16:46:34 -0700 | [diff] [blame] | 119 | mc->chip.dev = &spi->dev; |
| 120 | mc->chip.owner = THIS_MODULE; |
| 121 | |
| 122 | mc->port_config = 0x00; |
| 123 | /* write twice, because during initialisation the first setting |
| 124 | * is just for testing SPI communication, and the second is the |
| 125 | * "real" configuration |
| 126 | */ |
| 127 | ret = mc33880_write_config(mc); |
| 128 | mc->port_config = 0x00; |
| 129 | if (!ret) |
| 130 | ret = mc33880_write_config(mc); |
| 131 | |
| 132 | if (ret) { |
Jingoo Han | 30db2bd | 2013-03-15 18:16:49 +0900 | [diff] [blame] | 133 | dev_err(&spi->dev, "Failed writing to " DRIVER_NAME ": %d\n", |
| 134 | ret); |
Richard Röjfors | 1e5db00 | 2009-09-22 16:46:34 -0700 | [diff] [blame] | 135 | goto exit_destroy; |
| 136 | } |
| 137 | |
| 138 | ret = gpiochip_add(&mc->chip); |
| 139 | if (ret) |
| 140 | goto exit_destroy; |
| 141 | |
| 142 | return ret; |
| 143 | |
| 144 | exit_destroy: |
Richard Röjfors | 1e5db00 | 2009-09-22 16:46:34 -0700 | [diff] [blame] | 145 | mutex_destroy(&mc->lock); |
Richard Röjfors | 1e5db00 | 2009-09-22 16:46:34 -0700 | [diff] [blame] | 146 | return ret; |
| 147 | } |
| 148 | |
Bill Pemberton | 206210c | 2012-11-19 13:25:50 -0500 | [diff] [blame] | 149 | static int mc33880_remove(struct spi_device *spi) |
Richard Röjfors | 1e5db00 | 2009-09-22 16:46:34 -0700 | [diff] [blame] | 150 | { |
| 151 | struct mc33880 *mc; |
Richard Röjfors | 1e5db00 | 2009-09-22 16:46:34 -0700 | [diff] [blame] | 152 | |
Jingoo Han | 493294d | 2013-03-15 18:17:54 +0900 | [diff] [blame] | 153 | mc = spi_get_drvdata(spi); |
Richard Röjfors | 1e5db00 | 2009-09-22 16:46:34 -0700 | [diff] [blame] | 154 | if (mc == NULL) |
| 155 | return -ENODEV; |
| 156 | |
abdoulaye berthe | 9f5132a | 2014-07-12 22:30:12 +0200 | [diff] [blame] | 157 | gpiochip_remove(&mc->chip); |
| 158 | mutex_destroy(&mc->lock); |
Richard Röjfors | 1e5db00 | 2009-09-22 16:46:34 -0700 | [diff] [blame] | 159 | |
abdoulaye berthe | 9f5132a | 2014-07-12 22:30:12 +0200 | [diff] [blame] | 160 | return 0; |
Richard Röjfors | 1e5db00 | 2009-09-22 16:46:34 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | static struct spi_driver mc33880_driver = { |
| 164 | .driver = { |
| 165 | .name = DRIVER_NAME, |
| 166 | .owner = THIS_MODULE, |
| 167 | }, |
| 168 | .probe = mc33880_probe, |
Bill Pemberton | 8283c4f | 2012-11-19 13:20:08 -0500 | [diff] [blame] | 169 | .remove = mc33880_remove, |
Richard Röjfors | 1e5db00 | 2009-09-22 16:46:34 -0700 | [diff] [blame] | 170 | }; |
| 171 | |
| 172 | static int __init mc33880_init(void) |
| 173 | { |
| 174 | return spi_register_driver(&mc33880_driver); |
| 175 | } |
| 176 | /* register after spi postcore initcall and before |
| 177 | * subsys initcalls that may rely on these GPIOs |
| 178 | */ |
| 179 | subsys_initcall(mc33880_init); |
| 180 | |
| 181 | static void __exit mc33880_exit(void) |
| 182 | { |
| 183 | spi_unregister_driver(&mc33880_driver); |
| 184 | } |
| 185 | module_exit(mc33880_exit); |
| 186 | |
| 187 | MODULE_AUTHOR("Mocean Laboratories <info@mocean-labs.com>"); |
| 188 | MODULE_LICENSE("GPL v2"); |
| 189 | |