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