1*d5cc3f52SSimon Guinot /*
2*d5cc3f52SSimon Guinot  * cpld-gpio-bus.c: provides support for the CPLD GPIO bus found on some LaCie
3*d5cc3f52SSimon Guinot  * boards (as the 2Big/5Big Network v2 and the 2Big NAS). This parallel GPIO
4*d5cc3f52SSimon Guinot  * bus exposes two registers (address and data). Each of this register is made
5*d5cc3f52SSimon Guinot  * up of several dedicated GPIOs. An extra GPIO is used to notify the CPLD that
6*d5cc3f52SSimon Guinot  * the registers have been updated.
7*d5cc3f52SSimon Guinot  *
8*d5cc3f52SSimon Guinot  * Mostly this bus is used to configure the LEDs on LaCie boards.
9*d5cc3f52SSimon Guinot  *
10*d5cc3f52SSimon Guinot  * Copyright (C) 2013 Simon Guinot <simon.guinot@sequanux.org>
11*d5cc3f52SSimon Guinot  *
12*d5cc3f52SSimon Guinot  * This program is free software; you can redistribute it and/or
13*d5cc3f52SSimon Guinot  * modify it under the terms of the GNU General Public License as
14*d5cc3f52SSimon Guinot  * published by the Free Software Foundation; either version 2 of
15*d5cc3f52SSimon Guinot  * the License, or (at your option) any later version.
16*d5cc3f52SSimon Guinot  */
17*d5cc3f52SSimon Guinot 
18*d5cc3f52SSimon Guinot #include <asm/arch/gpio.h>
19*d5cc3f52SSimon Guinot #include "cpld-gpio-bus.h"
20*d5cc3f52SSimon Guinot 
21*d5cc3f52SSimon Guinot static void cpld_gpio_bus_set_addr(struct cpld_gpio_bus *bus, unsigned addr)
22*d5cc3f52SSimon Guinot {
23*d5cc3f52SSimon Guinot 	int pin;
24*d5cc3f52SSimon Guinot 
25*d5cc3f52SSimon Guinot 	for (pin = 0; pin < bus->num_addr; pin++)
26*d5cc3f52SSimon Guinot 		kw_gpio_set_value(bus->addr[pin], (addr >> pin) & 1);
27*d5cc3f52SSimon Guinot }
28*d5cc3f52SSimon Guinot 
29*d5cc3f52SSimon Guinot static void cpld_gpio_bus_set_data(struct cpld_gpio_bus *bus, unsigned data)
30*d5cc3f52SSimon Guinot {
31*d5cc3f52SSimon Guinot 	int pin;
32*d5cc3f52SSimon Guinot 
33*d5cc3f52SSimon Guinot 	for (pin = 0; pin < bus->num_data; pin++)
34*d5cc3f52SSimon Guinot 		kw_gpio_set_value(bus->data[pin], (data >> pin) & 1);
35*d5cc3f52SSimon Guinot }
36*d5cc3f52SSimon Guinot 
37*d5cc3f52SSimon Guinot static void cpld_gpio_bus_enable_select(struct cpld_gpio_bus *bus)
38*d5cc3f52SSimon Guinot {
39*d5cc3f52SSimon Guinot 	/* The transfer is enabled on the raising edge. */
40*d5cc3f52SSimon Guinot 	kw_gpio_set_value(bus->enable, 0);
41*d5cc3f52SSimon Guinot 	kw_gpio_set_value(bus->enable, 1);
42*d5cc3f52SSimon Guinot }
43*d5cc3f52SSimon Guinot 
44*d5cc3f52SSimon Guinot void cpld_gpio_bus_write(struct cpld_gpio_bus *bus,
45*d5cc3f52SSimon Guinot 			 unsigned addr, unsigned value)
46*d5cc3f52SSimon Guinot {
47*d5cc3f52SSimon Guinot 	cpld_gpio_bus_set_addr(bus, addr);
48*d5cc3f52SSimon Guinot 	cpld_gpio_bus_set_data(bus, value);
49*d5cc3f52SSimon Guinot 	cpld_gpio_bus_enable_select(bus);
50*d5cc3f52SSimon Guinot }
51