Skip to content

Commit 8f2b1e9

Browse files
committed
drivers: tmc51xx: move DT_DRV_COMPAT to tmc51xx.c
TMC51XX would have to be remodeled as a mfd, this means that each device tmc51xx, tmc51xx_motion_controller and tmc51xx_stepper_driver will have their own DT_DRV_COMPATs. Whenever the common header is included as of now, the adi_tmc51xx DT_DRV_COMPAT would have to be undef-ed each time in order to define a new DT_DRV_COMPAT i.e. required by the devices of the tmc51xx mfd. Signed-off-by: Jilay Pandya <[email protected]>
1 parent 7a65336 commit 8f2b1e9

File tree

5 files changed

+149
-188
lines changed

5 files changed

+149
-188
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# SPDX-FileCopyrightText: Copyright (c) 2025 Dipak Shetty
22
# SPDX-License-Identifier: Apache-2.0
33

4-
zephyr_library_sources_ifdef(CONFIG_STEPPER_ADI_TMC51XX tmc51xx.c tmc51xx_spi.c tmc51xx_uart.c)
4+
zephyr_library_sources_ifdef(CONFIG_STEPPER_ADI_TMC51XX tmc51xx.c)

drivers/stepper/adi_tmc/tmc51xx/tmc51xx.c

Lines changed: 148 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,167 @@
11
/*
22
* SPDX-FileCopyrightText: Copyright (c) 2025 Prevas A/S
3-
*
3+
* SPDX-FileCopyrightText: Copyright (c) 2025 Dipak Shetty
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

7+
#define DT_DRV_COMPAT adi_tmc51xx
8+
79
#include <stdlib.h>
810

911
#include <zephyr/drivers/stepper.h>
1012

1113
#include <adi_tmc_bus.h>
14+
#include <adi_tmc_spi.h>
15+
#include <adi_tmc_uart.h>
1216
#include <adi_tmc5xxx_common.h>
1317
#include "tmc51xx.h"
1418

1519
#include <zephyr/logging/log.h>
1620
LOG_MODULE_REGISTER(tmc51xx, CONFIG_STEPPER_LOG_LEVEL);
1721

22+
/* Check for supported bus types */
23+
#define TMC51XX_BUS_SPI DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
24+
#define TMC51XX_BUS_UART DT_ANY_INST_ON_BUS_STATUS_OKAY(uart)
25+
26+
#if TMC51XX_BUS_SPI
27+
/* SPI bus I/O operations for TMC51xx devices */
28+
const struct tmc_bus_io tmc51xx_spi_bus_io;
29+
#endif
30+
31+
#if TMC51XX_BUS_UART
32+
/* UART bus I/O operations for TMC51xx devices */
33+
const struct tmc_bus_io tmc51xx_uart_bus_io;
34+
#endif
35+
36+
/* Common configuration structure for TMC51xx */
37+
struct tmc51xx_config {
38+
union tmc_bus bus;
39+
const struct tmc_bus_io *bus_io;
40+
uint8_t comm_type;
41+
const uint32_t gconf;
42+
const uint32_t clock_frequency;
43+
const uint16_t default_micro_step_res;
44+
const int8_t sg_threshold;
45+
const bool is_sg_enabled;
46+
const uint32_t sg_velocity_check_interval_ms;
47+
const uint32_t sg_threshold_velocity;
48+
#ifdef CONFIG_STEPPER_ADI_TMC51XX_RAMP_GEN
49+
const struct tmc_ramp_generator_data default_ramp_config;
50+
#endif
51+
#if TMC51XX_BUS_UART
52+
const struct gpio_dt_spec sw_sel_gpio;
53+
uint8_t uart_addr;
54+
#endif
55+
#if TMC51XX_BUS_SPI
56+
struct gpio_dt_spec diag0_gpio;
57+
#endif
58+
};
59+
60+
struct tmc51xx_data {
61+
struct k_sem sem;
62+
struct k_work_delayable stallguard_dwork;
63+
struct k_work_delayable rampstat_callback_dwork;
64+
struct gpio_callback diag0_cb;
65+
const struct device *stepper;
66+
stepper_event_callback_t callback;
67+
void *event_cb_user_data;
68+
};
69+
70+
#if TMC51XX_BUS_SPI
71+
72+
static int tmc51xx_bus_check_spi(const union tmc_bus *bus, uint8_t comm_type)
73+
{
74+
if (comm_type != TMC_COMM_SPI) {
75+
return -ENOTSUP;
76+
}
77+
return spi_is_ready_dt(&bus->spi) ? 0 : -ENODEV;
78+
}
79+
80+
static int tmc51xx_reg_write_spi(const struct device *dev, const uint8_t reg_addr,
81+
const uint32_t reg_val)
82+
{
83+
const struct tmc51xx_config *config = dev->config;
84+
int err;
85+
86+
err = tmc_spi_write_register(&config->bus.spi, TMC5XXX_WRITE_BIT, reg_addr, reg_val);
87+
if (err < 0) {
88+
LOG_ERR("Failed to write register 0x%x with value 0x%x", reg_addr, reg_val);
89+
}
90+
91+
return err;
92+
}
93+
94+
static int tmc51xx_reg_read_spi(const struct device *dev, const uint8_t reg_addr, uint32_t *reg_val)
95+
{
96+
const struct tmc51xx_config *config = dev->config;
97+
int err;
98+
99+
err = tmc_spi_read_register(&config->bus.spi, TMC5XXX_ADDRESS_MASK, reg_addr, reg_val);
100+
if (err < 0) {
101+
LOG_ERR("Failed to read register 0x%x", reg_addr);
102+
}
103+
104+
return err;
105+
}
106+
107+
const struct tmc_bus_io tmc51xx_spi_bus_io = {
108+
.check = tmc51xx_bus_check_spi,
109+
.read = tmc51xx_reg_read_spi,
110+
.write = tmc51xx_reg_write_spi,
111+
};
112+
#endif /* TMC51XX_BUS_SPI */
113+
114+
#if TMC51XX_BUS_UART
115+
116+
static int tmc51xx_bus_check_uart(const union tmc_bus *bus, uint8_t comm_type)
117+
{
118+
if (comm_type != TMC_COMM_UART) {
119+
return -ENOTSUP;
120+
}
121+
return device_is_ready(bus->uart) ? 0 : -ENODEV;
122+
}
123+
124+
static int tmc51xx_reg_write_uart(const struct device *dev, const uint8_t reg_addr,
125+
const uint32_t reg_val)
126+
{
127+
const struct tmc51xx_config *config = dev->config;
128+
int err;
129+
130+
/* Route to the adi_tmc_uart.h implementation */
131+
err = tmc_uart_write_register(config->bus.uart, config->uart_addr, reg_addr, reg_val);
132+
if (err < 0) {
133+
LOG_ERR("Failed to write register 0x%x with value 0x%x", reg_addr, reg_val);
134+
}
135+
136+
/* Wait for the write to complete */
137+
k_sleep(K_MSEC(1));
138+
return err;
139+
}
140+
141+
static int tmc51xx_reg_read_uart(const struct device *dev, const uint8_t reg_addr,
142+
uint32_t *reg_val)
143+
{
144+
const struct tmc51xx_config *config = dev->config;
145+
int err;
146+
147+
/* Route to the adi_tmc_uart.h implementation */
148+
err = tmc_uart_read_register(config->bus.uart, config->uart_addr, reg_addr, reg_val);
149+
if (err < 0) {
150+
LOG_ERR("Failed to read register 0x%x", reg_addr);
151+
}
152+
153+
/* Wait for the read to complete */
154+
k_sleep(K_MSEC(1));
155+
return err;
156+
}
157+
158+
const struct tmc_bus_io tmc51xx_uart_bus_io = {
159+
.check = tmc51xx_bus_check_uart,
160+
.read = tmc51xx_reg_read_uart,
161+
.write = tmc51xx_reg_write_uart,
162+
};
163+
#endif /* TMC51XX_BUS_UART */
164+
18165
static inline int tmc51xx_bus_check(const struct device *dev)
19166
{
20167
const struct tmc51xx_config *config = dev->config;

drivers/stepper/adi_tmc/tmc51xx/tmc51xx.h

Lines changed: 0 additions & 65 deletions
This file was deleted.

drivers/stepper/adi_tmc/tmc51xx/tmc51xx_spi.c

Lines changed: 0 additions & 58 deletions
This file was deleted.

drivers/stepper/adi_tmc/tmc51xx/tmc51xx_uart.c

Lines changed: 0 additions & 63 deletions
This file was deleted.

0 commit comments

Comments
 (0)