2 * mc33880.c MC33880 high-side/low-side switch GPIO driver
3 * Copyright (c) 2009 Intel Corporation
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.
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.
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.
20 * Freescale MC33880 high-side/low-side switch
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>
30 #define DRIVER_NAME "mc33880"
33 * Pin configurations, see MAX7301 datasheet page 6
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
44 * Some registers must be read back to modify.
45 * To save time we cache them here in memory
48 struct mutex lock
; /* protect from simultanous accesses */
50 struct gpio_chip chip
;
51 struct spi_device
*spi
;
54 static int mc33880_write_config(struct mc33880
*mc
)
56 return spi_write(mc
->spi
, &mc
->port_config
, sizeof(mc
->port_config
));
60 static int __mc33880_set(struct mc33880
*mc
, unsigned offset
, int value
)
63 mc
->port_config
|= 1 << offset
;
65 mc
->port_config
&= ~(1 << offset
);
67 return mc33880_write_config(mc
);
71 static void mc33880_set(struct gpio_chip
*chip
, unsigned offset
, int value
)
73 struct mc33880
*mc
= container_of(chip
, struct mc33880
, chip
);
75 mutex_lock(&mc
->lock
);
77 __mc33880_set(mc
, offset
, value
);
79 mutex_unlock(&mc
->lock
);
82 static int __devinit
mc33880_probe(struct spi_device
*spi
)
85 struct mc33880_platform_data
*pdata
;
88 pdata
= spi
->dev
.platform_data
;
89 if (!pdata
|| !pdata
->base
) {
90 dev_dbg(&spi
->dev
, "incorrect or missing platform data\n");
95 * bits_per_word cannot be configured in platform data
97 spi
->bits_per_word
= 8;
103 mc
= kzalloc(sizeof(struct mc33880
), GFP_KERNEL
);
107 mutex_init(&mc
->lock
);
109 dev_set_drvdata(&spi
->dev
, mc
);
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
;
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
126 ret
= mc33880_write_config(mc
);
127 mc
->port_config
= 0x00;
129 ret
= mc33880_write_config(mc
);
132 printk(KERN_ERR
"Failed writing to " DRIVER_NAME
": %d\n", ret
);
136 ret
= gpiochip_add(&mc
->chip
);
143 dev_set_drvdata(&spi
->dev
, NULL
);
144 mutex_destroy(&mc
->lock
);
149 static int __devexit
mc33880_remove(struct spi_device
*spi
)
154 mc
= dev_get_drvdata(&spi
->dev
);
158 dev_set_drvdata(&spi
->dev
, NULL
);
160 ret
= gpiochip_remove(&mc
->chip
);
162 mutex_destroy(&mc
->lock
);
165 dev_err(&spi
->dev
, "Failed to remove the GPIO controller: %d\n",
171 static struct spi_driver mc33880_driver
= {
174 .owner
= THIS_MODULE
,
176 .probe
= mc33880_probe
,
177 .remove
= __devexit_p(mc33880_remove
),
180 static int __init
mc33880_init(void)
182 return spi_register_driver(&mc33880_driver
);
184 /* register after spi postcore initcall and before
185 * subsys initcalls that may rely on these GPIOs
187 subsys_initcall(mc33880_init
);
189 static void __exit
mc33880_exit(void)
191 spi_unregister_driver(&mc33880_driver
);
193 module_exit(mc33880_exit
);
195 MODULE_AUTHOR("Mocean Laboratories <info@mocean-labs.com>");
196 MODULE_LICENSE("GPL v2");