1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (C) 2008, Christian Pellegrin <chripell@evolware.org>
9 * L3 bus algorithm module.
11 * Copyright (C) 2001 Russell King, All Rights Reserved.
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/delay.h>
17 #include <linux/device.h>
18 #include <linux/gpio.h>
23 * Send one byte of data to the chip. Data is latched into the chip on
24 * the rising edge of the clock.
26 static void sendbyte(struct l3_pins
*adap
, unsigned int byte
)
30 for (i
= 0; i
< 8; i
++) {
31 adap
->setclk(adap
, 0);
32 udelay(adap
->data_hold
);
33 adap
->setdat(adap
, byte
& 1);
34 udelay(adap
->data_setup
);
35 adap
->setclk(adap
, 1);
36 udelay(adap
->clock_high
);
42 * Send a set of bytes to the chip. We need to pulse the MODE line
43 * between each byte, but never at the start nor at the end of the
46 static void sendbytes(struct l3_pins
*adap
, const u8
*buf
,
51 for (i
= 0; i
< len
; i
++) {
53 udelay(adap
->mode_hold
);
54 adap
->setmode(adap
, 0);
57 adap
->setmode(adap
, 1);
58 udelay(adap
->mode_setup
);
59 sendbyte(adap
, buf
[i
]);
63 int l3_write(struct l3_pins
*adap
, u8 addr
, u8
*data
, int len
)
65 adap
->setclk(adap
, 1);
66 adap
->setdat(adap
, 1);
67 adap
->setmode(adap
, 1);
70 adap
->setmode(adap
, 0);
71 udelay(adap
->mode_setup
);
73 udelay(adap
->mode_hold
);
75 sendbytes(adap
, data
, len
);
77 adap
->setclk(adap
, 1);
78 adap
->setdat(adap
, 1);
79 adap
->setmode(adap
, 0);
83 EXPORT_SYMBOL_GPL(l3_write
);
86 static void l3_set_clk(struct l3_pins
*adap
, int val
)
88 gpio_set_value(adap
->gpio_clk
, val
);
91 static void l3_set_data(struct l3_pins
*adap
, int val
)
93 gpio_set_value(adap
->gpio_data
, val
);
96 static void l3_set_mode(struct l3_pins
*adap
, int val
)
98 gpio_set_value(adap
->gpio_mode
, val
);
101 int l3_set_gpio_ops(struct device
*dev
, struct l3_pins
*adap
)
105 if (!adap
->use_gpios
)
108 ret
= devm_gpio_request_one(dev
, adap
->gpio_data
,
109 GPIOF_OUT_INIT_LOW
, "l3_data");
112 adap
->setdat
= l3_set_data
;
114 ret
= devm_gpio_request_one(dev
, adap
->gpio_clk
,
115 GPIOF_OUT_INIT_LOW
, "l3_clk");
118 adap
->setclk
= l3_set_clk
;
120 ret
= devm_gpio_request_one(dev
, adap
->gpio_mode
,
121 GPIOF_OUT_INIT_LOW
, "l3_mode");
124 adap
->setmode
= l3_set_mode
;
128 EXPORT_SYMBOL_GPL(l3_set_gpio_ops
);
130 MODULE_DESCRIPTION("L3 bit-banging driver");
131 MODULE_AUTHOR("Christian Pellegrin <chripell@evolware.org>");
132 MODULE_LICENSE("GPL");