/* SPDX-License-Identifier: GPL-2.0-only
 * This file is part of the CAN-CTRL IP Core driver for Linux
 *
 * Copyright (C) Fraunhofer-Institut for Photonic Microsystems (IPMS)
 * Maria-Reiche-Str. 2
 * 01109 Dresden
 *
 * This file is licensed under the terms of the GNU General Public
 * License version 2. This program is licensed "as is" without any
 * warranty of any kind, whether express or implied.
 *
 * @file lookup_table.h
 * @author SVT
 * @date   10/04/2024
 * @brief  lookup table for can-ctrl bittime configuration
 */

#ifndef LOOKUP_TABLE_H
#define LOOKUP_TABLE_H

#include <linux/types.h>

#define CAN_CC_SIZE_OF_LOOKUP_TABLE 9
#define CAN_FD_SIZE_OF_LOOKUP_TABLE 5
#define CAN_XL_SIZE_OF_LOOKUP_TABLE 7

/**
 * @struct can_bittiming_lookup_table
 * @brief Represents the timing parameters of a CAN bus.
 *
 * This struct defines the timing parameters for a CAN bus, such as the
 * total duration of a CAN bit, the time basis per bit, the number of Tq
 * units for the propagation segment, phase segment 1, phase segment 2, and
 * the synchronization jump width, as well as the TDC value, prescaler, and
 * desired sample point.
 */
struct can_bittiming_lookup_table {
    uint32_t bittime; /**< Total duration of a CAN bit in Tq */
    uint16_t tq; /**< Time basis per bit in Tq */
    uint16_t prop_seg; /**< Number of Tq units for the propagation segment */
    uint16_t phase_seg1; /**< Number of Tq units for phase segment 1 */
    uint16_t phase_seg2; /**< Number of Tq units for phase segment 2 */
    uint16_t sjw; /**< Number of Tq units for the synchronization jump width */
    uint32_t tdc;
    uint8_t prescaler;
    double sample_point; /**< Desired sample point in percentage */
};

struct can_bittiming_lookup_table
	can_bitrate_lookup_table[CAN_CC_SIZE_OF_LOOKUP_TABLE] = {
		{ 10000,   500, 199, 200, 100, 100, 0, 16, 80.0 }, // 10K (Prescaler 16)
    	{ 20000,   500, 199, 200, 100, 100, 0, 8,  80.0 }, // 20K (Prescaler 8)
    	{ 50000,   400, 114, 205, 80,  80,  0, 4,  80.0 }, // 50K (Prescaler 4)
    	{ 100000,  400, 159, 160, 80,  80,  0, 2,  80.0 }, // 100K (Prescaler 2)
    	{ 125000,  640, 255, 256, 128, 128, 0, 1,  80.0 }, // 125K
    	{ 250000,  320, 127, 128, 64,  64,  0, 1,  80.0 }, // 250K
    	{ 500000,  160, 95,  32,  32,  32,  0, 1,  80.0 }, // 500K
    	{ 800000,  100, 39,  40,  20,  20,  0, 1,  80.0 }, // 800K
    	{ 1000000, 80,  31,  32,  16,  16,  0, 1,  80.0 }, // 1000K
	};

struct can_bittiming_lookup_table
	can_fd_dbitrate_lookup_table[CAN_FD_SIZE_OF_LOOKUP_TABLE] = {
	    { 500000, 160, 95, 32, 32, 32, 129, 1, 80.0 }, // 500K
	    { 1000000, 80, 31, 32, 16, 16, 65, 1, 80.0 },  // 1M
    	{ 2000000, 40, 7, 24, 8, 8, 33, 1, 80.0 },     // 2M
    	{ 5000000, 16, 3,  8, 4, 4, 13, 1, 80.0 },     // 5M
    	{ 8000000, 10, 1,  6, 2, 2,  9, 1, 80.0 },     // 8M
	};
#endif // LOOKUP_TABLE_H
