2019-04-04 06:55:18 -07:00
|
|
|
/*
|
|
|
|
* tle8888.c
|
|
|
|
*
|
|
|
|
* TLE8888 Engine Machine System IC driver
|
|
|
|
*
|
2019-04-06 09:26:37 -07:00
|
|
|
* This has worked on a bench - see https://youtu.be/yjs5dh_NKo4
|
|
|
|
* All SPI and CS pin in OM_DEFAULT mode
|
|
|
|
*
|
2019-04-04 06:55:18 -07:00
|
|
|
* @date Mar 25, 2019
|
|
|
|
* @author Andrey Belomutskiy, (c) 2012-2019
|
|
|
|
*
|
|
|
|
* Andrey Gusakov, (c) 2019
|
|
|
|
*/
|
|
|
|
|
2019-04-04 07:08:50 -07:00
|
|
|
#include "global.h"
|
|
|
|
|
2019-04-12 17:52:51 -07:00
|
|
|
#if EFI_TLE8888
|
2019-04-04 07:08:50 -07:00
|
|
|
|
2019-04-04 06:55:18 -07:00
|
|
|
/* to be removed */
|
|
|
|
#include "engine_configuration.h"
|
|
|
|
|
2019-04-04 16:29:33 -07:00
|
|
|
EXTERN_CONFIG;
|
|
|
|
|
|
|
|
#include "hardware.h"
|
2019-04-04 16:56:03 -07:00
|
|
|
#include "efi_gpio.h"
|
2019-04-04 06:55:18 -07:00
|
|
|
#include "gpio/gpio_ext.h"
|
|
|
|
#include "gpio/tle8888.h"
|
|
|
|
#include "pin_repository.h"
|
|
|
|
|
2019-04-12 17:52:51 -07:00
|
|
|
#if EFI_TUNER_STUDIO
|
2019-04-04 20:03:32 -07:00
|
|
|
#include "tunerstudio.h"
|
|
|
|
extern TunerStudioOutputChannels tsOutputChannels;
|
|
|
|
#endif /* EFI_TUNER_STUDIO */
|
|
|
|
|
2019-04-04 06:55:18 -07:00
|
|
|
/* TODO: move to board.h file */
|
|
|
|
#define BOARD_TLE8888_COUNT 1
|
|
|
|
|
|
|
|
#ifndef BOARD_TLE8888_COUNT
|
|
|
|
#define BOARD_TLE8888_COUNT 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if (BOARD_TLE8888_COUNT > 0)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* TODO list:
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*==========================================================================*/
|
|
|
|
/* Driver local definitions. */
|
|
|
|
/*==========================================================================*/
|
|
|
|
|
|
|
|
#define DRIVER_NAME "tle8888"
|
|
|
|
|
2019-04-12 16:22:16 -07:00
|
|
|
static bool drv_task_ready = false;
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
TLE8888_DISABLED = 0,
|
|
|
|
TLE8888_WAIT_INIT,
|
|
|
|
TLE8888_READY,
|
|
|
|
TLE8888_FAILED
|
|
|
|
} tle8888_drv_state;
|
2019-04-04 06:55:18 -07:00
|
|
|
|
|
|
|
/* C0 */
|
|
|
|
#define CMD_READ (0 << 0)
|
|
|
|
#define CMD_WRITE (1 << 0)
|
|
|
|
/* C7:1 */
|
|
|
|
#define CMD_REG_ADDR(a) (((a) & 0x7f) << 1)
|
|
|
|
/* CD7:0 */
|
|
|
|
#define CMD_REG_DATA(d) (((d) & 0xff) << 8)
|
|
|
|
|
|
|
|
#define CMD_WR(a, d) (CMD_WRITE | CMD_REG_ADDR(a) | CMD_REG_DATA(d))
|
|
|
|
|
|
|
|
#define CMD_SR CMD_WR(0x1a, 0x03)
|
2019-04-06 07:30:20 -07:00
|
|
|
// 0x238 = 568
|
2019-04-04 06:55:18 -07:00
|
|
|
#define CMD_OE_SET CMD_WR(0x1c, 0x02)
|
2019-04-06 07:30:20 -07:00
|
|
|
/* not used
|
2019-04-04 06:55:18 -07:00
|
|
|
#define CMD_OE_CLR CMD_WR(0x1c, 0x01)
|
|
|
|
#define CMD_LOCK CMD_WR(0x1e, 0x02)
|
2019-04-06 07:30:20 -07:00
|
|
|
*/
|
2019-04-04 06:55:18 -07:00
|
|
|
#define CMD_UNLOCK CMD_WR(0x1e, 0x01)
|
|
|
|
#define CMD_INCONFIG(n, d) CMD_WR(0x53 + (n & 0x03), d)
|
|
|
|
#define CMD_DDCONFIG(n, d) CMD_WR(0x57 + (n & 0x03), d)
|
|
|
|
#define CMD_OECONFIG(n, d) CMD_WR(0x5b + (n & 0x03), d)
|
2019-04-12 16:22:16 -07:00
|
|
|
#define CMD_CONT(n, d) CMD_WR(0x7b + (n & 0x03), d)
|
|
|
|
/*==========================================================================*/
|
|
|
|
/* Driver exported variables. */
|
|
|
|
/*==========================================================================*/
|
2019-04-04 06:55:18 -07:00
|
|
|
|
|
|
|
/*==========================================================================*/
|
|
|
|
/* Driver local variables and types. */
|
|
|
|
/*==========================================================================*/
|
|
|
|
|
2019-04-12 16:22:16 -07:00
|
|
|
/* OS */
|
|
|
|
SEMAPHORE_DECL(tle8888_wake, 10 /* or BOARD_TLE8888_COUNT ? */);
|
|
|
|
static THD_WORKING_AREA(tle8888_thread_1_wa, 256);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Masks/inputs bits:
|
|
|
|
* 0..3 - OUT1 .. 4 - INJ - OD: 2.2A - direct
|
|
|
|
* 4..6 - OUT5 .. 7 - OD: 4.5A
|
|
|
|
* 7..12 - OUT8 ..13 - PP: 20mA
|
|
|
|
* 13..19 - OUT14..20 - OD: 0.6A
|
|
|
|
* 20..23 - OUT21..24 - PP: ?A
|
|
|
|
* 24..27 - IGN1 .. 4 - PP: 20mA - direct
|
|
|
|
*/
|
|
|
|
|
2019-04-04 06:55:18 -07:00
|
|
|
/* Driver private data */
|
|
|
|
struct tle8888_priv {
|
|
|
|
const struct tle8888_config *cfg;
|
2019-04-12 16:22:16 -07:00
|
|
|
/* cached output state - state last send to chip */
|
|
|
|
uint32_t o_state_cached;
|
|
|
|
/* state to be sended to chip */
|
|
|
|
uint32_t o_state;
|
|
|
|
/* direct driven output mask */
|
|
|
|
uint32_t o_direct_mask;
|
|
|
|
/* output enabled mask */
|
|
|
|
uint32_t o_oe_mask;
|
|
|
|
|
|
|
|
tle8888_drv_state drv_state;
|
2019-04-04 06:55:18 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct tle8888_priv chips[BOARD_TLE8888_COUNT];
|
|
|
|
|
2019-04-12 16:22:16 -07:00
|
|
|
static const char* tle8888_pin_names[TLE8888_OUTPUTS] = {
|
|
|
|
"TLE8888.INJ1", "TLE8888.INJ2", "TLE8888.INJ3", "TLE8888.INJ4",
|
|
|
|
"TLE8888.OUT5", "TLE8888.OUT6", "TLE8888.OUT7", "TLE8888.OUT8",
|
|
|
|
"TLE8888.OUT9", "TLE8888.OUT10", "TLE8888.OUT11", "TLE8888.OUT12",
|
|
|
|
"TLE8888.OUT13", "TLE8888.OUT14", "TLE8888.OUT15", "TLE8888.OUT16",
|
|
|
|
"TLE8888.OUT17", "TLE8888.OUT18", "TLE8888.OUT19", "TLE8888.OUT20",
|
|
|
|
"TLE8888.IGN1", "TLE8888.IGN2", "TLE8888.IGN3", "TLE8888.IGN4"
|
|
|
|
};
|
|
|
|
|
2019-04-04 06:55:18 -07:00
|
|
|
/*==========================================================================*/
|
|
|
|
/* Driver local functions. */
|
|
|
|
/*==========================================================================*/
|
|
|
|
|
|
|
|
static SPIDriver *get_bus(struct tle8888_priv *chip)
|
|
|
|
{
|
|
|
|
/* return non-const SPIDriver* from const struct cfg */
|
|
|
|
return chip->cfg->spi_bus;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int tle8888_spi_rw(struct tle8888_priv *chip, uint16_t tx, uint16_t *rx)
|
|
|
|
{
|
|
|
|
|
|
|
|
uint16_t rxb;
|
|
|
|
SPIDriver *spi = get_bus(chip);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 15.1 SPI Protocol
|
|
|
|
*
|
|
|
|
* after a read or write command: the address and content of the selected register
|
|
|
|
* is transmitted with the next SPI transmission (for not existing addresses or
|
|
|
|
* wrong access mode the data is always 0)
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Acquire ownership of the bus. */
|
|
|
|
spiAcquireBus(spi);
|
|
|
|
/* Setup transfer parameters. */
|
|
|
|
spiStart(spi, &chip->cfg->spi_config);
|
|
|
|
/* Slave Select assertion. */
|
|
|
|
spiSelect(spi);
|
|
|
|
/* Atomic transfer operations. */
|
2019-04-06 04:38:02 -07:00
|
|
|
rxb = spiPolledExchange(spi, tx);
|
|
|
|
//spiExchange(spi, 2, &tx, &rxb); 8 bit version just in case?
|
2019-04-04 06:55:18 -07:00
|
|
|
/* Slave Select de-assertion. */
|
|
|
|
spiUnselect(spi);
|
|
|
|
/* Ownership release. */
|
|
|
|
spiReleaseBus(spi);
|
|
|
|
|
2019-04-12 17:52:51 -07:00
|
|
|
#if EFI_TUNER_STUDIO
|
2019-04-04 20:03:32 -07:00
|
|
|
if (engineConfiguration->debugMode == DBG_TLE8888) {
|
|
|
|
tsOutputChannels.debugIntField1++;
|
|
|
|
tsOutputChannels.debugIntField2 = tx;
|
|
|
|
tsOutputChannels.debugIntField3 = rxb;
|
|
|
|
}
|
|
|
|
#endif /* EFI_TUNER_STUDIO */
|
|
|
|
|
2019-04-04 06:55:18 -07:00
|
|
|
if (rx)
|
|
|
|
*rx = rxb;
|
|
|
|
|
|
|
|
/* no errors for now */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-04-12 16:22:16 -07:00
|
|
|
/**
|
|
|
|
* @brief TLE8888 send output registers data.
|
|
|
|
* @details Sends ORed data to register, also receive 2-bit diagnostic.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int tle8888_update_output(struct tle8888_priv *chip)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int ret = 0;
|
|
|
|
uint32_t out_data;
|
|
|
|
|
|
|
|
/* TODO: lock? */
|
|
|
|
|
|
|
|
/* set value only for non-direct driven pins */
|
|
|
|
out_data = chip->o_state & (~chip->o_direct_mask);
|
|
|
|
for (i = 0; i < 4; i++) {
|
|
|
|
uint8_t od;
|
|
|
|
|
|
|
|
od = (out_data >> (8 * i)) & 0xff;
|
|
|
|
ret |= tle8888_spi_rw(chip, CMD_CONT(i, od), NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret == 0) {
|
|
|
|
/* atomic */
|
|
|
|
chip->o_state_cached = out_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO: unlock? */
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int tle8888_update_direct_output(struct tle8888_priv *chip, int pin, int value)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
const struct tle8888_config *cfg = chip->cfg;
|
|
|
|
|
|
|
|
/* find direct drive gpio */
|
|
|
|
for (i = 0; i < TLE8888_DIRECT_MISC; i++) {
|
|
|
|
/* again: outputs in cfg counted starting from 1 - hate this */
|
|
|
|
if (cfg->direct_io[i].output == pin + 1) {
|
|
|
|
if (value)
|
|
|
|
palSetPort(cfg->direct_io[i].port,
|
|
|
|
PAL_PORT_BIT(cfg->direct_io[i].pad));
|
|
|
|
else
|
|
|
|
palClearPort(cfg->direct_io[i].port,
|
|
|
|
PAL_PORT_BIT(cfg->direct_io[i].pad));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* direct gpio not found */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief TLE8888 chip driver wakeup.
|
|
|
|
* @details Wake up driver. Will cause output register update
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int tle8888_wake_driver(struct tle8888_priv *chip)
|
|
|
|
{
|
|
|
|
(void)chip;
|
|
|
|
|
|
|
|
chSemSignal(&tle8888_wake);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2019-04-04 06:55:18 -07:00
|
|
|
|
|
|
|
/*==========================================================================*/
|
|
|
|
/* Driver thread. */
|
|
|
|
/*==========================================================================*/
|
|
|
|
|
2019-04-12 16:22:16 -07:00
|
|
|
static THD_FUNCTION(tle8888_driver_thread, p)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
msg_t msg;
|
|
|
|
|
|
|
|
(void)p;
|
|
|
|
|
|
|
|
chRegSetThreadName(DRIVER_NAME);
|
|
|
|
|
|
|
|
while(1) {
|
|
|
|
msg = chSemWaitTimeout(&tle8888_wake, TIME_MS2I(TLE8888_POLL_INTERVAL_MS));
|
|
|
|
|
|
|
|
/* should we care about msg == MSG_TIMEOUT? */
|
|
|
|
(void)msg;
|
|
|
|
|
|
|
|
for (i = 0; i < BOARD_TLE8888_COUNT; i++) {
|
|
|
|
int ret;
|
|
|
|
struct tle8888_priv *chip;
|
|
|
|
|
|
|
|
chip = &chips[i];
|
|
|
|
if ((chip->cfg == NULL) ||
|
|
|
|
(chip->drv_state == TLE8888_DISABLED) ||
|
|
|
|
(chip->drv_state == TLE8888_FAILED))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
ret = tle8888_update_output(chip);
|
|
|
|
if (ret) {
|
|
|
|
/* set state to TLE8888_FAILED? */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-04 06:55:18 -07:00
|
|
|
/*==========================================================================*/
|
|
|
|
/* Driver interrupt handlers. */
|
|
|
|
/*==========================================================================*/
|
|
|
|
|
|
|
|
/*==========================================================================*/
|
|
|
|
/* Driver exported functions. */
|
|
|
|
/*==========================================================================*/
|
|
|
|
|
2019-04-12 16:22:16 -07:00
|
|
|
int tle8888_writePad(void *data, unsigned int pin, int value)
|
|
|
|
{
|
|
|
|
struct tle8888_priv *chip;
|
|
|
|
|
|
|
|
if ((pin >= TLE8888_DIRECT_OUTPUTS) || (data == NULL))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
chip = (struct tle8888_priv *)data;
|
|
|
|
|
|
|
|
/* TODO: lock */
|
|
|
|
if (value)
|
|
|
|
chip->o_state |= (1 << pin);
|
|
|
|
else
|
|
|
|
chip->o_state &= ~(1 << pin);
|
|
|
|
/* TODO: unlock */
|
|
|
|
/* direct driven? */
|
|
|
|
if (chip->o_direct_mask & (1 << pin)) {
|
|
|
|
return tle8888_update_direct_output(chip, pin, value);
|
|
|
|
} else {
|
|
|
|
return tle8888_wake_driver(chip);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-04-04 06:55:18 -07:00
|
|
|
int tle8888_chip_init(void * data)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct tle8888_priv *chip = (struct tle8888_priv *)data;
|
|
|
|
const struct tle8888_config *cfg = chip->cfg;
|
|
|
|
|
2019-04-04 20:03:32 -07:00
|
|
|
int ret = 0;
|
2019-04-04 06:55:18 -07:00
|
|
|
/* mark pins used */
|
2019-04-12 16:22:16 -07:00
|
|
|
// we do not initialize CS pin so we should not be marking it used - i'm sad
|
|
|
|
//ret = gpio_pin_markUsed(cfg->spi_config.ssport, cfg->spi_config.sspad, DRIVER_NAME " CS");
|
2019-04-04 06:55:18 -07:00
|
|
|
if (cfg->reset.port != NULL)
|
2019-04-09 16:31:10 -07:00
|
|
|
ret |= gpio_pin_markUsed(cfg->reset.port, cfg->reset.pad, DRIVER_NAME " RST");
|
2019-04-05 15:37:00 -07:00
|
|
|
for (i = 0; i < TLE8888_DIRECT_MISC; i++)
|
2019-04-04 06:55:18 -07:00
|
|
|
if (cfg->direct_io[i].port)
|
2019-04-09 16:31:10 -07:00
|
|
|
ret |= gpio_pin_markUsed(cfg->direct_io[i].port, cfg->direct_io[i].pad, DRIVER_NAME " DIRECT IO");
|
2019-04-05 15:37:00 -07:00
|
|
|
|
2019-04-04 06:55:18 -07:00
|
|
|
if (ret) {
|
|
|
|
ret = -1;
|
|
|
|
goto err_gpios;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Software reset */
|
|
|
|
tle8888_spi_rw(chip, CMD_SR, NULL);
|
|
|
|
|
2019-04-06 07:30:20 -07:00
|
|
|
/**
|
|
|
|
* Table 8. Reset Times. All reset times not more than 20uS
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
chThdSleepMilliseconds(3);
|
2019-04-04 06:55:18 -07:00
|
|
|
|
|
|
|
/* Set LOCK bit to 0 */
|
|
|
|
tle8888_spi_rw(chip, CMD_UNLOCK, NULL);
|
|
|
|
|
2019-04-12 16:22:16 -07:00
|
|
|
chip->o_direct_mask = 0;
|
|
|
|
chip->o_oe_mask = 0;
|
2019-04-04 06:55:18 -07:00
|
|
|
/* enable direct drive of OUTPUT4..1
|
|
|
|
* ...still need INJEN signal */
|
2019-04-12 16:22:16 -07:00
|
|
|
chip->o_direct_mask |= 0x0000000f;
|
|
|
|
chip->o_oe_mask |= 0x0000000f;
|
2019-04-04 06:55:18 -07:00
|
|
|
/* enable direct drive of IGN4..1
|
|
|
|
* ...still need IGNEN signal */
|
2019-04-12 16:22:16 -07:00
|
|
|
chip->o_direct_mask |= 0x0f000000;
|
|
|
|
chip->o_oe_mask |= 0x0f000000;
|
2019-04-04 06:55:18 -07:00
|
|
|
|
|
|
|
/* map and enable outputs for direct driven channels */
|
|
|
|
for (i = 0; i < TLE8888_DIRECT_MISC; i++) {
|
2019-04-12 16:22:16 -07:00
|
|
|
int out;
|
|
|
|
uint32_t mask;
|
|
|
|
|
|
|
|
out = cfg->direct_io[i].output;
|
|
|
|
|
|
|
|
/* not used? */
|
|
|
|
if (out == 0)
|
|
|
|
continue;
|
2019-04-04 06:55:18 -07:00
|
|
|
|
2019-04-12 16:22:16 -07:00
|
|
|
/* OUT1..4 driven direct only through dedicated pins */
|
2019-04-04 06:55:18 -07:00
|
|
|
if (out < 5)
|
|
|
|
return -1;
|
|
|
|
|
2019-04-12 16:22:16 -07:00
|
|
|
/* in config counted from 1 */
|
|
|
|
mask = (1 << (out - 1));
|
2019-04-04 06:55:18 -07:00
|
|
|
|
|
|
|
/* check if output already ocupied */
|
2019-04-12 16:22:16 -07:00
|
|
|
if (chip->o_direct_mask & mask) {
|
2019-04-04 06:55:18 -07:00
|
|
|
/* incorrect config? */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* enable direct drive and output enable */
|
2019-04-12 16:22:16 -07:00
|
|
|
chip->o_direct_mask |= mask;
|
|
|
|
chip->o_oe_mask |= mask;
|
2019-04-04 06:55:18 -07:00
|
|
|
|
2019-04-12 16:22:16 -07:00
|
|
|
/* set INCONFIG - aux input mapping */
|
|
|
|
tle8888_spi_rw(chip, CMD_INCONFIG(i, out - 5), NULL);
|
2019-04-04 06:55:18 -07:00
|
|
|
}
|
|
|
|
|
2019-04-12 16:22:16 -07:00
|
|
|
/* enable all ouputs
|
|
|
|
* TODO: add API to enable/disable? */
|
|
|
|
chip->o_oe_mask |= 0x0ffffff0;
|
|
|
|
|
|
|
|
/* set OE and DD registers */
|
2019-04-04 06:55:18 -07:00
|
|
|
for (i = 0; i < 4; i++) {
|
2019-04-12 16:22:16 -07:00
|
|
|
uint8_t oe, dd;
|
|
|
|
|
|
|
|
oe = (chip->o_oe_mask >> (8 * i)) & 0xff;
|
|
|
|
dd = (chip->o_direct_mask >> (8 * i)) & 0xff;
|
|
|
|
tle8888_spi_rw(chip, CMD_OECONFIG(i, oe), NULL);
|
|
|
|
tle8888_spi_rw(chip, CMD_DDCONFIG(i, dd), NULL);
|
2019-04-04 06:55:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* enable outputs */
|
|
|
|
tle8888_spi_rw(chip, CMD_OE_SET, NULL);
|
|
|
|
|
|
|
|
err_gpios:
|
|
|
|
/* unmark pins */
|
2019-04-09 16:31:10 -07:00
|
|
|
//gpio_pin_markUnused(cfg->spi_config.ssport, cfg->spi_config.sspad);
|
2019-04-04 06:55:18 -07:00
|
|
|
if (cfg->reset.port != NULL)
|
2019-04-09 16:31:10 -07:00
|
|
|
gpio_pin_markUnused(cfg->reset.port, cfg->reset.pad);
|
2019-04-05 15:37:00 -07:00
|
|
|
for (i = 0; i < TLE8888_DIRECT_MISC; i++)
|
2019-04-04 06:55:18 -07:00
|
|
|
if (cfg->direct_io[i].port)
|
2019-04-09 16:31:10 -07:00
|
|
|
gpio_pin_markUnused(cfg->direct_io[i].port, cfg->direct_io[i].pad);
|
2019-04-05 15:37:00 -07:00
|
|
|
|
2019-04-04 06:55:18 -07:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-04-12 16:22:16 -07:00
|
|
|
int tle8888_init(void * data)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct tle8888_priv *chip;
|
|
|
|
|
|
|
|
chip = (struct tle8888_priv *)data;
|
|
|
|
|
|
|
|
/* check for multiple init */
|
|
|
|
if (chip->drv_state != TLE8888_WAIT_INIT)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
ret = tle8888_chip_init(chip);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
chip->drv_state = TLE8888_READY;
|
|
|
|
|
|
|
|
if (!drv_task_ready) {
|
|
|
|
chThdCreateStatic(tle8888_thread_1_wa, sizeof(tle8888_thread_1_wa),
|
|
|
|
NORMALPRIO + 1, tle8888_driver_thread, NULL);
|
|
|
|
drv_task_ready = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int tle8888_deinit(void *data)
|
|
|
|
{
|
|
|
|
(void)data;
|
|
|
|
|
|
|
|
/* TODO: set all pins to inactive state, stop task? */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct gpiochip_ops tle8888_ops = {
|
|
|
|
.writePad = tle8888_writePad,
|
|
|
|
.readPad = NULL, /* chip outputs only */
|
|
|
|
//.getDiag = tle8888_getDiag,
|
|
|
|
.init = tle8888_init,
|
|
|
|
.deinit = tle8888_deinit,
|
|
|
|
};
|
|
|
|
|
2019-04-04 06:55:18 -07:00
|
|
|
/**
|
|
|
|
* @brief TLE8888 driver add.
|
|
|
|
* @details Checks for valid config
|
|
|
|
*/
|
|
|
|
|
|
|
|
int tle8888_add(unsigned int index, const struct tle8888_config *cfg)
|
|
|
|
{
|
2019-04-12 16:22:16 -07:00
|
|
|
int ret;
|
2019-04-04 06:55:18 -07:00
|
|
|
struct tle8888_priv *chip;
|
|
|
|
|
2019-04-12 16:22:16 -07:00
|
|
|
efiAssert(OBD_PCM_Processor_Fault, cfg != NULL, "8888CFG", 0)
|
|
|
|
|
2019-04-04 06:55:18 -07:00
|
|
|
/* no config or no such chip */
|
|
|
|
osalDbgCheck((cfg != NULL) && (cfg->spi_bus != NULL) && (index < BOARD_TLE8888_COUNT));
|
|
|
|
|
|
|
|
/* check for valid cs.
|
2019-04-04 17:03:31 -07:00
|
|
|
* TODO: remove this check? CS can be driven by SPI */
|
2019-04-04 06:55:18 -07:00
|
|
|
if (cfg->spi_config.ssport == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
chip = &chips[index];
|
|
|
|
|
|
|
|
/* already initted? */
|
|
|
|
if (chip->cfg != NULL)
|
|
|
|
return -1;
|
2019-04-12 16:22:16 -07:00
|
|
|
|
2019-04-04 20:03:32 -07:00
|
|
|
chip->cfg = cfg;
|
2019-04-12 16:22:16 -07:00
|
|
|
chip->o_state = 0;
|
|
|
|
chip->o_state_cached = 0;
|
|
|
|
chip->o_direct_mask = 0;
|
|
|
|
chip->drv_state = TLE8888_WAIT_INIT;
|
|
|
|
|
|
|
|
/* register, return gpio chip base */
|
|
|
|
ret = gpiochip_register(DRIVER_NAME, &tle8888_ops, TLE8888_OUTPUTS, chip);
|
|
|
|
|
|
|
|
/* set default pin names, board init code can rewrite */
|
|
|
|
gpiochips_setPinNames(ret, tle8888_pin_names);
|
2019-04-04 06:55:18 -07:00
|
|
|
|
2019-04-12 16:22:16 -07:00
|
|
|
return ret;
|
2019-04-04 06:55:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#else /* BOARD_TLE8888_COUNT > 0 */
|
|
|
|
|
|
|
|
int tle8888_add(unsigned int index, const struct tle8888_config *cfg)
|
|
|
|
{
|
|
|
|
(void)index; (void)cfg;
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* BOARD_TLE8888_COUNT */
|
|
|
|
|
|
|
|
/*********TO BE REMOVED FROM THIS FILE***************/
|
|
|
|
|
|
|
|
/* this should be in board file */
|
2019-04-04 16:29:33 -07:00
|
|
|
static struct tle8888_config tle8888_cfg = {
|
2019-04-04 16:56:03 -07:00
|
|
|
.spi_bus = NULL,
|
2019-04-04 06:55:18 -07:00
|
|
|
.spi_config = {
|
|
|
|
.circular = false,
|
|
|
|
.end_cb = NULL,
|
|
|
|
.ssport = GPIOF,
|
|
|
|
.sspad = 0U,
|
2019-04-07 05:35:04 -07:00
|
|
|
#if defined(STM32F405xx) || defined(STM32F407xx) || defined (STM32F469xx)
|
2019-04-04 06:55:18 -07:00
|
|
|
.cr1 =
|
2019-04-05 15:37:00 -07:00
|
|
|
SPI_CR1_DFF | // 16-bit transfer
|
|
|
|
SPI_CR1_SSM |
|
|
|
|
SPI_CR1_SSI |
|
|
|
|
SPI_CR1_LSBFIRST | //LSB first
|
|
|
|
((3 << SPI_CR1_BR_Pos) & SPI_CR1_BR) | // div = 16
|
|
|
|
SPI_CR1_MSTR |
|
2019-04-06 10:11:46 -07:00
|
|
|
SPI_CR1_CPHA |
|
2019-04-05 15:37:00 -07:00
|
|
|
0,
|
2019-04-06 19:04:35 -07:00
|
|
|
.cr2 = 0
|
2019-04-12 13:14:25 -07:00
|
|
|
#elif defined (STM32F765xx) || defined(STM32F767xx) || defined(STM32F746xx)
|
2019-04-06 19:04:35 -07:00
|
|
|
.cr1 =
|
|
|
|
SPI_CR1_SSM |
|
|
|
|
SPI_CR1_SSI |
|
|
|
|
SPI_CR1_LSBFIRST |
|
|
|
|
SPI_CR1_MSTR |
|
|
|
|
((3 << SPI_CR1_BR_Pos) & SPI_CR1_BR) |
|
|
|
|
SPI_CR1_CPHA |
|
|
|
|
0,
|
|
|
|
/* 16-bit transfer */
|
|
|
|
.cr2 = SPI_CR2_DS_3 | SPI_CR2_DS_2 | SPI_CR2_DS_1 | SPI_CR2_DS_0
|
2019-04-07 05:35:04 -07:00
|
|
|
#else
|
|
|
|
unexpected platform
|
2019-04-06 19:04:35 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2019-04-04 06:55:18 -07:00
|
|
|
},
|
|
|
|
.direct_io = {
|
2019-04-12 16:22:16 -07:00
|
|
|
[0] = {.port = NULL, .pad = 0, .output = 9},
|
|
|
|
[1] = {.port = NULL, .pad = 0, .output = 10},
|
|
|
|
[2] = {.port = NULL, .pad = 0, .output = 11},
|
|
|
|
[3] = {.port = NULL, .pad = 0, .output = 12},
|
2019-04-04 06:55:18 -07:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
void initTle8888(DECLARE_ENGINE_PARAMETER_SIGNATURE)
|
|
|
|
{
|
2019-04-04 16:56:03 -07:00
|
|
|
if (engineConfiguration->tle8888_cs == GPIO_UNASSIGNED) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// todo: reuse initSpiCs method?
|
2019-04-12 16:22:16 -07:00
|
|
|
tle8888_cfg.spi_config.ssport = getHwPort(DRIVER_NAME " CS", engineConfiguration->tle8888_cs);
|
|
|
|
tle8888_cfg.spi_config.sspad = getHwPin(DRIVER_NAME " CS", engineConfiguration->tle8888_cs);
|
2019-04-04 16:56:03 -07:00
|
|
|
|
2019-04-04 16:29:33 -07:00
|
|
|
tle8888_cfg.spi_bus = getSpiDevice(engineConfiguration->tle8888spiDevice);
|
2019-04-04 16:56:03 -07:00
|
|
|
if (tle8888_cfg.spi_bus == NULL) {
|
|
|
|
// error already reported
|
|
|
|
return;
|
|
|
|
}
|
2019-04-04 16:29:33 -07:00
|
|
|
|
2019-04-04 06:55:18 -07:00
|
|
|
tle8888_add(0, &tle8888_cfg);
|
2019-04-12 16:22:16 -07:00
|
|
|
|
|
|
|
/* Initial idea of gpiochips:
|
|
|
|
* _add function should be called early on board init.
|
|
|
|
* _init firnction called later from gpiochips_init() after initial gpios init....
|
|
|
|
* BUT
|
|
|
|
* we want everithing to be configurable, this initTle8888 is called after gpiochips_init()
|
|
|
|
* so we need manually call _init
|
|
|
|
* HOPE THIS WILL NOT BREAK ANYTHING
|
|
|
|
*/
|
|
|
|
#if (BOARD_TLE8888_COUNT > 0)
|
|
|
|
/* thisisahack */
|
|
|
|
tle8888_chip_init(&chips[0]);
|
|
|
|
#endif
|
2019-04-04 06:55:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********TO BE REMOVED FROM THIS FILE ENDS***********/
|
2019-04-04 07:08:50 -07:00
|
|
|
|
|
|
|
#endif /* EFI_TLE8888 */
|