Thomas Gleixner | 82c2981 | 2019-05-28 09:57:05 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Richard Röjfors | 1e5db00 | 2009-09-22 16:46:34 -0700 | [diff] [blame] | 2 | /* |
Grant Likely | c103de2 | 2011-06-04 18:38:28 -0600 | [diff] [blame] | 3 | * MC33880 high-side/low-side switch GPIO driver |
Richard Röjfors | 1e5db00 | 2009-09-22 16:46:34 -0700 | [diff] [blame] | 4 | * Copyright (c) 2009 Intel Corporation |
Richard Röjfors | 1e5db00 | 2009-09-22 16:46:34 -0700 | [diff] [blame] | 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> |
Linus Walleij | 03a3f19 | 2018-04-13 15:08:00 +0200 | [diff] [blame] | 15 | #include <linux/gpio/driver.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 16 | #include <linux/slab.h> |
Paul Gortmaker | bb207ef | 2011-07-03 13:38:09 -0400 | [diff] [blame] | 17 | #include <linux/module.h> |
Richard Röjfors | 1e5db00 | 2009-09-22 16:46:34 -0700 | [diff] [blame] | 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 { |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 37 | struct mutex lock; /* protect from simultaneous accesses */ |
Richard Röjfors | 1e5db00 | 2009-09-22 16:46:34 -0700 | [diff] [blame] | 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 void mc33880_set(struct gpio_chip *chip, unsigned offset, int value) |
| 61 | { |
Linus Walleij | 609f969 | 2015-12-07 10:04:32 +0100 | [diff] [blame] | 62 | struct mc33880 *mc = gpiochip_get_data(chip); |
Richard Röjfors | 1e5db00 | 2009-09-22 16:46:34 -0700 | [diff] [blame] | 63 | |
| 64 | mutex_lock(&mc->lock); |
| 65 | |
| 66 | __mc33880_set(mc, offset, value); |
| 67 | |
| 68 | mutex_unlock(&mc->lock); |
| 69 | } |
| 70 | |
Bill Pemberton | 3836309 | 2012-11-19 13:22:34 -0500 | [diff] [blame] | 71 | static int mc33880_probe(struct spi_device *spi) |
Richard Röjfors | 1e5db00 | 2009-09-22 16:46:34 -0700 | [diff] [blame] | 72 | { |
| 73 | struct mc33880 *mc; |
| 74 | struct mc33880_platform_data *pdata; |
| 75 | int ret; |
| 76 | |
Jingoo Han | e56aee1 | 2013-07-30 17:08:05 +0900 | [diff] [blame] | 77 | pdata = dev_get_platdata(&spi->dev); |
Richard Röjfors | 1e5db00 | 2009-09-22 16:46:34 -0700 | [diff] [blame] | 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 | |
Jingoo Han | 632d8e5 | 2013-03-15 18:15:49 +0900 | [diff] [blame] | 92 | mc = devm_kzalloc(&spi->dev, sizeof(struct mc33880), GFP_KERNEL); |
Richard Röjfors | 1e5db00 | 2009-09-22 16:46:34 -0700 | [diff] [blame] | 93 | if (!mc) |
| 94 | return -ENOMEM; |
| 95 | |
| 96 | mutex_init(&mc->lock); |
| 97 | |
Jingoo Han | 493294d | 2013-03-15 18:17:54 +0900 | [diff] [blame] | 98 | spi_set_drvdata(spi, mc); |
Richard Röjfors | 1e5db00 | 2009-09-22 16:46:34 -0700 | [diff] [blame] | 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; |
Linus Walleij | 9fb1f39 | 2013-12-04 14:42:46 +0100 | [diff] [blame] | 106 | mc->chip.can_sleep = true; |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 107 | mc->chip.parent = &spi->dev; |
Richard Röjfors | 1e5db00 | 2009-09-22 16:46:34 -0700 | [diff] [blame] | 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) { |
Jingoo Han | 30db2bd | 2013-03-15 18:16:49 +0900 | [diff] [blame] | 121 | dev_err(&spi->dev, "Failed writing to " DRIVER_NAME ": %d\n", |
| 122 | ret); |
Richard Röjfors | 1e5db00 | 2009-09-22 16:46:34 -0700 | [diff] [blame] | 123 | goto exit_destroy; |
| 124 | } |
| 125 | |
Linus Walleij | 609f969 | 2015-12-07 10:04:32 +0100 | [diff] [blame] | 126 | ret = gpiochip_add_data(&mc->chip, mc); |
Richard Röjfors | 1e5db00 | 2009-09-22 16:46:34 -0700 | [diff] [blame] | 127 | if (ret) |
| 128 | goto exit_destroy; |
| 129 | |
| 130 | return ret; |
| 131 | |
| 132 | exit_destroy: |
Richard Röjfors | 1e5db00 | 2009-09-22 16:46:34 -0700 | [diff] [blame] | 133 | mutex_destroy(&mc->lock); |
Richard Röjfors | 1e5db00 | 2009-09-22 16:46:34 -0700 | [diff] [blame] | 134 | return ret; |
| 135 | } |
| 136 | |
Bill Pemberton | 206210c | 2012-11-19 13:25:50 -0500 | [diff] [blame] | 137 | static int mc33880_remove(struct spi_device *spi) |
Richard Röjfors | 1e5db00 | 2009-09-22 16:46:34 -0700 | [diff] [blame] | 138 | { |
| 139 | struct mc33880 *mc; |
Richard Röjfors | 1e5db00 | 2009-09-22 16:46:34 -0700 | [diff] [blame] | 140 | |
Jingoo Han | 493294d | 2013-03-15 18:17:54 +0900 | [diff] [blame] | 141 | mc = spi_get_drvdata(spi); |
Varka Bhadram | afeb7b4 | 2015-03-31 09:49:11 +0530 | [diff] [blame] | 142 | if (!mc) |
Richard Röjfors | 1e5db00 | 2009-09-22 16:46:34 -0700 | [diff] [blame] | 143 | return -ENODEV; |
| 144 | |
abdoulaye berthe | 9f5132a | 2014-07-12 22:30:12 +0200 | [diff] [blame] | 145 | gpiochip_remove(&mc->chip); |
| 146 | mutex_destroy(&mc->lock); |
Richard Röjfors | 1e5db00 | 2009-09-22 16:46:34 -0700 | [diff] [blame] | 147 | |
abdoulaye berthe | 9f5132a | 2014-07-12 22:30:12 +0200 | [diff] [blame] | 148 | return 0; |
Richard Röjfors | 1e5db00 | 2009-09-22 16:46:34 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | static struct spi_driver mc33880_driver = { |
| 152 | .driver = { |
| 153 | .name = DRIVER_NAME, |
Richard Röjfors | 1e5db00 | 2009-09-22 16:46:34 -0700 | [diff] [blame] | 154 | }, |
| 155 | .probe = mc33880_probe, |
Bill Pemberton | 8283c4f | 2012-11-19 13:20:08 -0500 | [diff] [blame] | 156 | .remove = mc33880_remove, |
Richard Röjfors | 1e5db00 | 2009-09-22 16:46:34 -0700 | [diff] [blame] | 157 | }; |
| 158 | |
| 159 | static int __init mc33880_init(void) |
| 160 | { |
| 161 | return spi_register_driver(&mc33880_driver); |
| 162 | } |
| 163 | /* register after spi postcore initcall and before |
| 164 | * subsys initcalls that may rely on these GPIOs |
| 165 | */ |
| 166 | subsys_initcall(mc33880_init); |
| 167 | |
| 168 | static void __exit mc33880_exit(void) |
| 169 | { |
| 170 | spi_unregister_driver(&mc33880_driver); |
| 171 | } |
| 172 | module_exit(mc33880_exit); |
| 173 | |
| 174 | MODULE_AUTHOR("Mocean Laboratories <info@mocean-labs.com>"); |
| 175 | MODULE_LICENSE("GPL v2"); |
| 176 | |