2 * SPI driver for the CPLD chip on the Mikrotik RB4xx boards
4 * Copyright (C) 2010 Gabor Juhos <juhosg@openwrt.org>
6 * This file was based on the patches for Linux 2.6.27.39 published by
7 * MikroTik for their RouterBoard 4xx series devices.
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License version 2 as published
11 * by the Free Software Foundation.
14 #include <linux/types.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/module.h>
19 #include <linux/device.h>
20 #include <linux/bitops.h>
21 #include <linux/spi/spi.h>
22 #include <linux/gpio.h>
23 #include <linux/slab.h>
25 #include <asm/mach-ath79/rb4xx_cpld.h>
27 #define DRV_NAME "spi-rb4xx-cpld"
28 #define DRV_DESC "RB4xx CPLD driver"
29 #define DRV_VERSION "0.1.0"
31 #define CPLD_CMD_WRITE_NAND 0x08 /* send cmd, n x send data, send indle */
32 #define CPLD_CMD_WRITE_CFG 0x09 /* send cmd, n x send cfg */
33 #define CPLD_CMD_READ_NAND 0x0a /* send cmd, send idle, n x read data */
34 #define CPLD_CMD_READ_FAST 0x0b /* send cmd, 4 x idle, n x read data */
35 #define CPLD_CMD_LED5_ON 0x0c /* send cmd */
36 #define CPLD_CMD_LED5_OFF 0x0d /* send cmd */
39 struct spi_device
*spi
;
41 struct gpio_chip chip
;
45 static struct rb4xx_cpld
*rb4xx_cpld
;
47 static inline struct rb4xx_cpld
*gpio_to_cpld(struct gpio_chip
*chip
)
49 return container_of(chip
, struct rb4xx_cpld
, chip
);
52 static int rb4xx_cpld_write_cmd(struct rb4xx_cpld
*cpld
, unsigned char cmd
)
54 struct spi_transfer t
[1];
56 unsigned char tx_buf
[1];
60 memset(&t
, 0, sizeof(t
));
63 t
[0].len
= sizeof(tx_buf
);
64 spi_message_add_tail(&t
[0], &m
);
68 err
= spi_sync(cpld
->spi
, &m
);
72 static int rb4xx_cpld_write_cfg(struct rb4xx_cpld
*cpld
, unsigned char config
)
74 struct spi_transfer t
[1];
80 memset(&t
, 0, sizeof(t
));
83 t
[0].len
= sizeof(cmd
);
84 spi_message_add_tail(&t
[0], &m
);
86 cmd
[0] = CPLD_CMD_WRITE_CFG
;
89 err
= spi_sync(cpld
->spi
, &m
);
93 static int __rb4xx_cpld_change_cfg(struct rb4xx_cpld
*cpld
, unsigned mask
,
99 config
= cpld
->config
& ~mask
;
102 if ((cpld
->config
^ config
) & 0xff) {
103 err
= rb4xx_cpld_write_cfg(cpld
, config
);
108 if ((cpld
->config
^ config
) & CPLD_CFG_nLED5
) {
109 err
= rb4xx_cpld_write_cmd(cpld
, (value
) ? CPLD_CMD_LED5_ON
:
115 cpld
->config
= config
;
119 int rb4xx_cpld_change_cfg(unsigned mask
, unsigned value
)
123 if (rb4xx_cpld
== NULL
)
126 mutex_lock(&rb4xx_cpld
->lock
);
127 ret
= __rb4xx_cpld_change_cfg(rb4xx_cpld
, mask
, value
);
128 mutex_unlock(&rb4xx_cpld
->lock
);
132 EXPORT_SYMBOL_GPL(rb4xx_cpld_change_cfg
);
134 int rb4xx_cpld_read(unsigned char *rx_buf
, unsigned count
)
136 static const unsigned char cmd
[2] = { CPLD_CMD_READ_NAND
, 0 };
137 struct spi_transfer t
[2] = {
146 struct spi_message m
;
148 if (rb4xx_cpld
== NULL
)
151 spi_message_init(&m
);
152 spi_message_add_tail(&t
[0], &m
);
153 spi_message_add_tail(&t
[1], &m
);
154 return spi_sync(rb4xx_cpld
->spi
, &m
);
156 EXPORT_SYMBOL_GPL(rb4xx_cpld_read
);
158 int rb4xx_cpld_write(const unsigned char *buf
, unsigned count
)
160 static const unsigned char cmd
= CPLD_CMD_WRITE_NAND
;
161 struct spi_transfer t
[3] = {
168 .tx_nbits
= SPI_NBITS_DUAL
,
171 .tx_nbits
= SPI_NBITS_DUAL
,
174 struct spi_message m
;
176 if (rb4xx_cpld
== NULL
)
179 spi_message_init(&m
);
180 spi_message_add_tail(&t
[0], &m
);
181 spi_message_add_tail(&t
[1], &m
);
182 spi_message_add_tail(&t
[2], &m
);
183 return spi_sync(rb4xx_cpld
->spi
, &m
);
185 EXPORT_SYMBOL_GPL(rb4xx_cpld_write
);
187 static int rb4xx_cpld_gpio_get(struct gpio_chip
*chip
, unsigned offset
)
189 struct rb4xx_cpld
*cpld
= gpio_to_cpld(chip
);
192 mutex_lock(&cpld
->lock
);
193 ret
= (cpld
->config
>> offset
) & 1;
194 mutex_unlock(&cpld
->lock
);
199 static void rb4xx_cpld_gpio_set(struct gpio_chip
*chip
, unsigned offset
,
202 struct rb4xx_cpld
*cpld
= gpio_to_cpld(chip
);
204 mutex_lock(&cpld
->lock
);
205 __rb4xx_cpld_change_cfg(cpld
, (1 << offset
), !!value
<< offset
);
206 mutex_unlock(&cpld
->lock
);
209 static int rb4xx_cpld_gpio_direction_input(struct gpio_chip
*chip
,
215 static int rb4xx_cpld_gpio_direction_output(struct gpio_chip
*chip
,
219 struct rb4xx_cpld
*cpld
= gpio_to_cpld(chip
);
222 mutex_lock(&cpld
->lock
);
223 ret
= __rb4xx_cpld_change_cfg(cpld
, (1 << offset
), !!value
<< offset
);
224 mutex_unlock(&cpld
->lock
);
229 static int rb4xx_cpld_gpio_init(struct rb4xx_cpld
*cpld
, unsigned int base
)
234 cpld
->config
= CPLD_CFG_nLED1
| CPLD_CFG_nLED2
| CPLD_CFG_nLED3
|
235 CPLD_CFG_nLED4
| CPLD_CFG_nCE
;
236 rb4xx_cpld_write_cfg(cpld
, cpld
->config
);
238 /* setup GPIO chip */
239 cpld
->chip
.label
= DRV_NAME
;
241 cpld
->chip
.get
= rb4xx_cpld_gpio_get
;
242 cpld
->chip
.set
= rb4xx_cpld_gpio_set
;
243 cpld
->chip
.direction_input
= rb4xx_cpld_gpio_direction_input
;
244 cpld
->chip
.direction_output
= rb4xx_cpld_gpio_direction_output
;
246 cpld
->chip
.base
= base
;
247 cpld
->chip
.ngpio
= CPLD_NUM_GPIOS
;
248 cpld
->chip
.can_sleep
= 1;
249 cpld
->chip
.dev
= &cpld
->spi
->dev
;
250 cpld
->chip
.owner
= THIS_MODULE
;
252 err
= gpiochip_add(&cpld
->chip
);
254 dev_err(&cpld
->spi
->dev
, "adding GPIO chip failed, err=%d\n",
260 static int rb4xx_cpld_probe(struct spi_device
*spi
)
262 struct rb4xx_cpld
*cpld
;
263 struct rb4xx_cpld_platform_data
*pdata
;
266 pdata
= spi
->dev
.platform_data
;
268 dev_dbg(&spi
->dev
, "no platform data\n");
272 cpld
= kzalloc(sizeof(*cpld
), GFP_KERNEL
);
274 dev_err(&spi
->dev
, "no memory for private data\n");
278 mutex_init(&cpld
->lock
);
279 cpld
->spi
= spi_dev_get(spi
);
280 dev_set_drvdata(&spi
->dev
, cpld
);
282 spi
->mode
= SPI_MODE_0
;
283 spi
->bits_per_word
= 8;
284 err
= spi_setup(spi
);
286 dev_err(&spi
->dev
, "spi_setup failed, err=%d\n", err
);
290 err
= rb4xx_cpld_gpio_init(cpld
, pdata
->gpio_base
);
299 dev_set_drvdata(&spi
->dev
, NULL
);
305 static int rb4xx_cpld_remove(struct spi_device
*spi
)
307 struct rb4xx_cpld
*cpld
;
310 cpld
= dev_get_drvdata(&spi
->dev
);
311 dev_set_drvdata(&spi
->dev
, NULL
);
317 static struct spi_driver rb4xx_cpld_driver
= {
320 .bus
= &spi_bus_type
,
321 .owner
= THIS_MODULE
,
323 .probe
= rb4xx_cpld_probe
,
324 .remove
= rb4xx_cpld_remove
,
327 static int __init
rb4xx_cpld_init(void)
329 return spi_register_driver(&rb4xx_cpld_driver
);
331 module_init(rb4xx_cpld_init
);
333 static void __exit
rb4xx_cpld_exit(void)
335 spi_unregister_driver(&rb4xx_cpld_driver
);
337 module_exit(rb4xx_cpld_exit
);
339 MODULE_DESCRIPTION(DRV_DESC
);
340 MODULE_VERSION(DRV_VERSION
);
341 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
342 MODULE_LICENSE("GPL v2");