1 // SPDX-License-Identifier: GPL-2.0-only
3 * NBUS driver for TS-4600 based boards
5 * Copyright (c) 2016 - Savoir-faire Linux
6 * Author: Sebastien Bourdelin <sebastien.bourdelin@savoirfairelinux.com>
8 * This driver implements a GPIOs bit-banged bus, called the NBUS by Technologic
9 * Systems. It is used to communicate with the peripherals in the FPGA on the
13 #include <linux/bitops.h>
14 #include <linux/gpio/consumer.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/mutex.h>
18 #include <linux/of_platform.h>
19 #include <linux/platform_device.h>
20 #include <linux/pwm.h>
21 #include <linux/ts-nbus.h>
23 #define TS_NBUS_DIRECTION_IN 0
24 #define TS_NBUS_DIRECTION_OUT 1
25 #define TS_NBUS_WRITE_ADR 0
26 #define TS_NBUS_WRITE_VAL 1
29 struct pwm_device *pwm;
30 struct gpio_descs *data;
31 struct gpio_desc *csn;
32 struct gpio_desc *txrx;
33 struct gpio_desc *strobe;
34 struct gpio_desc *ale;
35 struct gpio_desc *rdy;
40 * request all gpios required by the bus.
42 static int ts_nbus_init_pdata(struct platform_device *pdev, struct ts_nbus
45 ts_nbus->data = devm_gpiod_get_array(&pdev->dev, "ts,data",
47 if (IS_ERR(ts_nbus->data)) {
48 dev_err(&pdev->dev, "failed to retrieve ts,data-gpio from dts\n");
49 return PTR_ERR(ts_nbus->data);
52 ts_nbus->csn = devm_gpiod_get(&pdev->dev, "ts,csn", GPIOD_OUT_HIGH);
53 if (IS_ERR(ts_nbus->csn)) {
54 dev_err(&pdev->dev, "failed to retrieve ts,csn-gpio from dts\n");
55 return PTR_ERR(ts_nbus->csn);
58 ts_nbus->txrx = devm_gpiod_get(&pdev->dev, "ts,txrx", GPIOD_OUT_HIGH);
59 if (IS_ERR(ts_nbus->txrx)) {
60 dev_err(&pdev->dev, "failed to retrieve ts,txrx-gpio from dts\n");
61 return PTR_ERR(ts_nbus->txrx);
64 ts_nbus->strobe = devm_gpiod_get(&pdev->dev, "ts,strobe", GPIOD_OUT_HIGH);
65 if (IS_ERR(ts_nbus->strobe)) {
66 dev_err(&pdev->dev, "failed to retrieve ts,strobe-gpio from dts\n");
67 return PTR_ERR(ts_nbus->strobe);
70 ts_nbus->ale = devm_gpiod_get(&pdev->dev, "ts,ale", GPIOD_OUT_HIGH);
71 if (IS_ERR(ts_nbus->ale)) {
72 dev_err(&pdev->dev, "failed to retrieve ts,ale-gpio from dts\n");
73 return PTR_ERR(ts_nbus->ale);
76 ts_nbus->rdy = devm_gpiod_get(&pdev->dev, "ts,rdy", GPIOD_IN);
77 if (IS_ERR(ts_nbus->rdy)) {
78 dev_err(&pdev->dev, "failed to retrieve ts,rdy-gpio from dts\n");
79 return PTR_ERR(ts_nbus->rdy);
86 * the data gpios are used for reading and writing values, their directions
87 * should be adjusted accordingly.
89 static void ts_nbus_set_direction(struct ts_nbus *ts_nbus, int direction)
93 for (i = 0; i < 8; i++) {
94 if (direction == TS_NBUS_DIRECTION_IN)
95 gpiod_direction_input(ts_nbus->data->desc[i]);
97 /* when used as output the default state of the data
98 * lines are set to high */
99 gpiod_direction_output(ts_nbus->data->desc[i], 1);
104 * reset the bus in its initial state.
105 * The data, csn, strobe and ale lines must be zero'ed to let the FPGA knows a
106 * new transaction can be process.
108 static void ts_nbus_reset_bus(struct ts_nbus *ts_nbus)
110 DECLARE_BITMAP(values, 8);
114 gpiod_set_array_value_cansleep(8, ts_nbus->data->desc,
115 ts_nbus->data->info, values);
116 gpiod_set_value_cansleep(ts_nbus->csn, 0);
117 gpiod_set_value_cansleep(ts_nbus->strobe, 0);
118 gpiod_set_value_cansleep(ts_nbus->ale, 0);
122 * let the FPGA knows it can process.
124 static void ts_nbus_start_transaction(struct ts_nbus *ts_nbus)
126 gpiod_set_value_cansleep(ts_nbus->strobe, 1);
130 * read a byte value from the data gpios.
131 * return 0 on success or negative errno on failure.
133 static int ts_nbus_read_byte(struct ts_nbus *ts_nbus, u8 *val)
135 struct gpio_descs *gpios = ts_nbus->data;
139 for (i = 0; i < 8; i++) {
140 ret = gpiod_get_value_cansleep(gpios->desc[i]);
151 * set the data gpios accordingly to the byte value.
153 static void ts_nbus_write_byte(struct ts_nbus *ts_nbus, u8 byte)
155 struct gpio_descs *gpios = ts_nbus->data;
156 DECLARE_BITMAP(values, 8);
160 gpiod_set_array_value_cansleep(8, gpios->desc, gpios->info, values);
164 * reading the bus consists of resetting the bus, then notifying the FPGA to
165 * send the data in the data gpios and return the read value.
166 * return 0 on success or negative errno on failure.
168 static int ts_nbus_read_bus(struct ts_nbus *ts_nbus, u8 *val)
170 ts_nbus_reset_bus(ts_nbus);
171 ts_nbus_start_transaction(ts_nbus);
173 return ts_nbus_read_byte(ts_nbus, val);
177 * writing to the bus consists of resetting the bus, then define the type of
178 * command (address/value), write the data and notify the FPGA to retrieve the
179 * value in the data gpios.
181 static void ts_nbus_write_bus(struct ts_nbus *ts_nbus, int cmd, u8 val)
183 ts_nbus_reset_bus(ts_nbus);
185 if (cmd == TS_NBUS_WRITE_ADR)
186 gpiod_set_value_cansleep(ts_nbus->ale, 1);
188 ts_nbus_write_byte(ts_nbus, val);
189 ts_nbus_start_transaction(ts_nbus);
193 * read the value in the FPGA register at the given address.
194 * return 0 on success or negative errno on failure.
196 int ts_nbus_read(struct ts_nbus *ts_nbus, u8 adr, u16 *val)
201 /* bus access must be atomic */
202 mutex_lock(&ts_nbus->lock);
204 /* set the bus in read mode */
205 gpiod_set_value_cansleep(ts_nbus->txrx, 0);
208 ts_nbus_write_bus(ts_nbus, TS_NBUS_WRITE_ADR, adr);
210 /* set the data gpios direction as input before reading */
211 ts_nbus_set_direction(ts_nbus, TS_NBUS_DIRECTION_IN);
213 /* reading value MSB first */
217 for (i = 1; i >= 0; i--) {
218 /* read a byte from the bus, leave on error */
219 ret = ts_nbus_read_bus(ts_nbus, &byte);
223 /* append the byte read to the final value */
224 *val |= byte << (i * 8);
226 gpiod_set_value_cansleep(ts_nbus->csn, 1);
227 ret = gpiod_get_value_cansleep(ts_nbus->rdy);
231 /* restore the data gpios direction as output after reading */
232 ts_nbus_set_direction(ts_nbus, TS_NBUS_DIRECTION_OUT);
234 mutex_unlock(&ts_nbus->lock);
238 EXPORT_SYMBOL_GPL(ts_nbus_read);
241 * write the desired value in the FPGA register at the given address.
243 int ts_nbus_write(struct ts_nbus *ts_nbus, u8 adr, u16 val)
247 /* bus access must be atomic */
248 mutex_lock(&ts_nbus->lock);
250 /* set the bus in write mode */
251 gpiod_set_value_cansleep(ts_nbus->txrx, 1);
254 ts_nbus_write_bus(ts_nbus, TS_NBUS_WRITE_ADR, adr);
256 /* writing value MSB first */
257 for (i = 1; i >= 0; i--)
258 ts_nbus_write_bus(ts_nbus, TS_NBUS_WRITE_VAL, (u8)(val >> (i * 8)));
260 /* wait for completion */
261 gpiod_set_value_cansleep(ts_nbus->csn, 1);
262 while (gpiod_get_value_cansleep(ts_nbus->rdy) != 0) {
263 gpiod_set_value_cansleep(ts_nbus->csn, 0);
264 gpiod_set_value_cansleep(ts_nbus->csn, 1);
267 mutex_unlock(&ts_nbus->lock);
271 EXPORT_SYMBOL_GPL(ts_nbus_write);
273 static int ts_nbus_probe(struct platform_device *pdev)
275 struct pwm_device *pwm;
276 struct pwm_args pargs;
277 struct device *dev = &pdev->dev;
278 struct ts_nbus *ts_nbus;
281 ts_nbus = devm_kzalloc(dev, sizeof(*ts_nbus), GFP_KERNEL);
285 mutex_init(&ts_nbus->lock);
287 ret = ts_nbus_init_pdata(pdev, ts_nbus);
291 pwm = devm_pwm_get(dev, NULL);
294 if (ret != -EPROBE_DEFER)
295 dev_err(dev, "unable to request PWM\n");
299 pwm_get_args(pwm, &pargs);
301 dev_err(&pdev->dev, "invalid PWM period\n");
306 * FIXME: pwm_apply_args() should be removed when switching to
307 * the atomic PWM API.
310 ret = pwm_config(pwm, pargs.period, pargs.period);
315 * we can now start the FPGA and populate the peripherals.
321 * let the child nodes retrieve this instance of the ts-nbus.
323 dev_set_drvdata(dev, ts_nbus);
325 ret = of_platform_populate(dev->of_node, NULL, NULL, dev);
329 dev_info(dev, "initialized\n");
334 static int ts_nbus_remove(struct platform_device *pdev)
336 struct ts_nbus *ts_nbus = dev_get_drvdata(&pdev->dev);
338 /* shutdown the FPGA */
339 mutex_lock(&ts_nbus->lock);
340 pwm_disable(ts_nbus->pwm);
341 mutex_unlock(&ts_nbus->lock);
346 static const struct of_device_id ts_nbus_of_match[] = {
347 { .compatible = "technologic,ts-nbus", },
350 MODULE_DEVICE_TABLE(of, ts_nbus_of_match);
352 static struct platform_driver ts_nbus_driver = {
353 .probe = ts_nbus_probe,
354 .remove = ts_nbus_remove,
357 .of_match_table = ts_nbus_of_match,
361 module_platform_driver(ts_nbus_driver);
363 MODULE_ALIAS("platform:ts_nbus");
364 MODULE_AUTHOR("Sebastien Bourdelin <sebastien.bourdelin@savoirfairelinux.com>");
365 MODULE_DESCRIPTION("Technologic Systems NBUS");
366 MODULE_LICENSE("GPL v2");