// SPDX-License-Identifier: GPL-2.0-only /* * NBUS driver for TS-4600 based boards * * Copyright (c) 2016 - Savoir-faire Linux * Author: Sebastien Bourdelin <sebastien.bourdelin@savoirfairelinux.com> * * This driver implements a GPIOs bit-banged bus, called the NBUS by Technologic * Systems. It is used to communicate with the peripherals in the FPGA on the * TS-4600 SoM.
*/
/* * request all gpios required by the bus.
*/ staticint ts_nbus_init_pdata(struct platform_device *pdev, struct ts_nbus *ts_nbus)
{
ts_nbus->data = devm_gpiod_get_array(&pdev->dev, "ts,data",
GPIOD_OUT_HIGH); if (IS_ERR(ts_nbus->data)) return dev_err_probe(&pdev->dev, PTR_ERR(ts_nbus->data), "failed to retrieve ts,data-gpio from dts\n");
ts_nbus->csn = devm_gpiod_get(&pdev->dev, "ts,csn", GPIOD_OUT_HIGH); if (IS_ERR(ts_nbus->csn)) return dev_err_probe(&pdev->dev, PTR_ERR(ts_nbus->csn), "failed to retrieve ts,csn-gpio from dts\n");
ts_nbus->txrx = devm_gpiod_get(&pdev->dev, "ts,txrx", GPIOD_OUT_HIGH); if (IS_ERR(ts_nbus->txrx)) return dev_err_probe(&pdev->dev, PTR_ERR(ts_nbus->txrx), "failed to retrieve ts,txrx-gpio from dts\n");
ts_nbus->strobe = devm_gpiod_get(&pdev->dev, "ts,strobe", GPIOD_OUT_HIGH); if (IS_ERR(ts_nbus->strobe)) return dev_err_probe(&pdev->dev, PTR_ERR(ts_nbus->strobe), "failed to retrieve ts,strobe-gpio from dts\n");
ts_nbus->ale = devm_gpiod_get(&pdev->dev, "ts,ale", GPIOD_OUT_HIGH); if (IS_ERR(ts_nbus->ale)) return dev_err_probe(&pdev->dev, PTR_ERR(ts_nbus->ale), "failed to retrieve ts,ale-gpio from dts\n");
ts_nbus->rdy = devm_gpiod_get(&pdev->dev, "ts,rdy", GPIOD_IN); if (IS_ERR(ts_nbus->rdy)) return dev_err_probe(&pdev->dev, PTR_ERR(ts_nbus->rdy), "failed to retrieve ts,rdy-gpio from dts\n");
return 0;
}
/* * the data gpios are used for reading and writing values, their directions * should be adjusted accordingly.
*/ staticvoid ts_nbus_set_direction(struct ts_nbus *ts_nbus, int direction)
{ int i;
for (i = 0; i < 8; i++) { if (direction == TS_NBUS_DIRECTION_IN)
gpiod_direction_input(ts_nbus->data->desc[i]); else /* when used as output the default state of the data
* lines are set to high */
gpiod_direction_output(ts_nbus->data->desc[i], 1);
}
}
/* * reset the bus in its initial state. * The data, csn, strobe and ale lines must be zero'ed to let the FPGA knows a * new transaction can be process.
*/ staticvoid ts_nbus_reset_bus(struct ts_nbus *ts_nbus)
{
DECLARE_BITMAP(values, 8);
/* * let the FPGA knows it can process.
*/ staticvoid ts_nbus_start_transaction(struct ts_nbus *ts_nbus)
{
gpiod_set_value_cansleep(ts_nbus->strobe, 1);
}
/* * read a byte value from the data gpios. * return 0 on success or negative errno on failure.
*/ staticint ts_nbus_read_byte(struct ts_nbus *ts_nbus, u8 *val)
{ struct gpio_descs *gpios = ts_nbus->data; int ret, i;
*val = 0; for (i = 0; i < 8; i++) {
ret = gpiod_get_value_cansleep(gpios->desc[i]); if (ret < 0) return ret; if (ret)
*val |= BIT(i);
}
return 0;
}
/* * set the data gpios accordingly to the byte value.
*/ staticvoid ts_nbus_write_byte(struct ts_nbus *ts_nbus, u8 byte)
{ struct gpio_descs *gpios = ts_nbus->data;
DECLARE_BITMAP(values, 8);
/* * reading the bus consists of resetting the bus, then notifying the FPGA to * send the data in the data gpios and return the read value. * return 0 on success or negative errno on failure.
*/ staticint ts_nbus_read_bus(struct ts_nbus *ts_nbus, u8 *val)
{
ts_nbus_reset_bus(ts_nbus);
ts_nbus_start_transaction(ts_nbus);
return ts_nbus_read_byte(ts_nbus, val);
}
/* * writing to the bus consists of resetting the bus, then define the type of * command (address/value), write the data and notify the FPGA to retrieve the * value in the data gpios.
*/ staticvoid ts_nbus_write_bus(struct ts_nbus *ts_nbus, int cmd, u8 val)
{
ts_nbus_reset_bus(ts_nbus);
if (cmd == TS_NBUS_WRITE_ADR)
gpiod_set_value_cansleep(ts_nbus->ale, 1);
/* * read the value in the FPGA register at the given address. * return 0 on success or negative errno on failure.
*/ int ts_nbus_read(struct ts_nbus *ts_nbus, u8 adr, u16 *val)
{ int ret, i;
u8 byte;
/* bus access must be atomic */
mutex_lock(&ts_nbus->lock);
/* set the bus in read mode */
gpiod_set_value_cansleep(ts_nbus->txrx, 0);
/* set the data gpios direction as input before reading */
ts_nbus_set_direction(ts_nbus, TS_NBUS_DIRECTION_IN);
/* reading value MSB first */ do {
*val = 0;
byte = 0; for (i = 1; i >= 0; i--) { /* read a byte from the bus, leave on error */
ret = ts_nbus_read_bus(ts_nbus, &byte); if (ret < 0) goto err;
/* append the byte read to the final value */
*val |= byte << (i * 8);
}
gpiod_set_value_cansleep(ts_nbus->csn, 1);
ret = gpiod_get_value_cansleep(ts_nbus->rdy);
} while (ret);
err: /* restore the data gpios direction as output after reading */
ts_nbus_set_direction(ts_nbus, TS_NBUS_DIRECTION_OUT);
mutex_unlock(&ts_nbus->lock);
return ret;
}
EXPORT_SYMBOL_GPL(ts_nbus_read);
/* * write the desired value in the FPGA register at the given address.
*/ int ts_nbus_write(struct ts_nbus *ts_nbus, u8 adr, u16 val)
{ int i;
/* bus access must be atomic */
mutex_lock(&ts_nbus->lock);
/* set the bus in write mode */
gpiod_set_value_cansleep(ts_nbus->txrx, 1);
ret = pwm_apply_might_sleep(pwm, &state); if (ret < 0) return dev_err_probe(dev, ret, "failed to configure PWM\n");
/* * we can now start the FPGA and populate the peripherals.
*/
ts_nbus->pwm = pwm;
/* * let the child nodes retrieve this instance of the ts-nbus.
*/
dev_set_drvdata(dev, ts_nbus);
ret = of_platform_populate(dev->of_node, NULL, NULL, dev); if (ret < 0) return dev_err_probe(dev, ret, "failed to populate platform devices on bus\n");
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.