@@ -29,92 +29,108 @@ LOG_MODULE_REGISTER(maxim14819, LOG_LEVEL_DBG);
2929#define DEFAULT_DRV_CURR_LIMIT (CL_CONF(CURR_300MA))
3030#define DEFAULT_CLOCK (CLOCK_XTALEN | CLOCK_TXTXENDIS)
3131
32- struct gpio_cb_data {
32+ struct isr_data {
3333 struct gpio_callback cb ;
3434 void * arg ;
35+ isr_func_t max14819_isr ;
3536};
3637
3738struct maxim_14819_cfg {
3839 struct spi_dt_spec spi ;
39- struct gpio_dt_spec irq ;
4040 iolink_14819_cfg_t drv ;
4141 uint8_t id ;
4242};
4343
4444struct maxim_14819_data {
45- struct gpio_cb_data irq_data ;
45+ struct isr_data isr ;
4646 iolink_hw_drv_t * hw ;
47+ struct gpio_dt_spec irq_dt ;
4748};
4849
50+
4951void irq_cb (const struct device * dev , struct gpio_callback * gpio_cb , uint32_t pins ){
50- const struct gpio_cb_data * cb_data = CONTAINER_OF (gpio_cb , struct gpio_cb_data , cb );
51- iolink_14819_isr (cb_data -> arg );
52+ const struct isr_data * isr = CONTAINER_OF (gpio_cb , struct isr_data , cb );
53+ isr -> max14819_isr (isr -> arg );
54+ }
55+
56+
57+ int _iolink_setup_int (int gpio_pin , isr_func_t isr_func , void * irq_arg )
58+ {
59+ struct maxim_14819_data * data = (struct maxim_14819_data * )(uintptr_t )(uint32_t )gpio_pin ;
60+ int rc = gpio_pin_configure_dt (& data -> irq_dt , GPIO_INPUT );
61+ if (rc != 0 ) {
62+ LOG_ERR ("Failed to configure IRQ pin %d" , data -> irq_dt .pin );
63+ return rc ;
64+ }
65+ data -> isr .arg = irq_arg ;
66+ data -> isr .max14819_isr = isr_func ;
67+ gpio_init_callback (& data -> isr .cb , irq_cb , BIT (data -> irq_dt .pin ));
68+ rc = gpio_add_callback (data -> irq_dt .port , & data -> isr .cb );
69+ if (rc != 0 ) {
70+ LOG_ERR ("Failed to add IRQ callback %d" , data -> irq_dt .pin );
71+ return rc ;
72+ }
73+ rc = gpio_pin_interrupt_configure (data -> irq_dt .port , data -> irq_dt .pin , GPIO_INT_EDGE_TO_ACTIVE );
74+ if (rc != 0 ) {
75+ LOG_ERR ("Failed to configure IRQ interrupt %d" , data -> irq_dt .pin );
76+ }
77+ return 0 ;
5278}
5379
5480static int maxim_init (const struct device * dev ){
5581 const struct maxim_14819_cfg * cfg = dev -> config ;
5682 struct maxim_14819_data * data = dev -> data ;
5783 iolink_hw_drv_t * iol_drv = iolink_14819_init (& cfg -> drv );
58- int rc ;
84+ int rc = 0 ;
5985 do {
6086 if (iol_drv == NULL ) {
6187 LOG_ERR ("Failed to initialize Maxim 14819 %d" , cfg -> id );
6288 rc = - ENODEV ;
6389 break ;
6490 }
6591 data -> hw = iol_drv ;
66- rc = gpio_pin_configure_dt (& cfg -> irq , GPIO_INPUT );
67- if (rc != 0 ) {
68- LOG_ERR ("Failed to configure IRQ pin %d" , cfg -> irq .pin );
69- break ;
70- }
71- data -> irq_data .arg = iol_drv ;
72- gpio_init_callback (& data -> irq_data .cb , irq_cb , BIT (cfg -> irq .pin ));
73- rc = gpio_add_callback (cfg -> irq .port , & data -> irq_data .cb );
74- if (rc != 0 ) {
75- LOG_ERR ("Failed to add IRQ callback %d" , cfg -> irq .pin );
76- break ;
77- }
78- rc = gpio_pin_interrupt_configure (cfg -> irq .port ,cfg -> irq .pin ,GPIO_INT_EDGE_TO_ACTIVE );
79- if (rc != 0 ) {
80- LOG_ERR ("Failed to configure IRQ interrupt %d" , cfg -> irq .pin );
81- }
8292 }while (0 );
8393 return rc ;
8494}
8595
8696
87- #define MAXIM14819_DEFINE (inst ) \
88- static struct maxim_14819_data maxim_14819_data_##inst ={ \
89- .hw = NULL, \
90- .irq_data = {.cb = {0}, .arg = NULL} \
91- }; \
92- static const struct maxim_14819_cfg maxim_14819_cfg_##inst = { \
93- .id = inst, \
94- .spi = SPI_DT_SPEC_GET(DT_DRV_INST(inst), SPI_WORD_SET(8) | SPI_TRANSFER_MSB, 0), \
95- .irq = GPIO_DT_SPEC_GET(DT_DRV_INST(inst), irq_gpios), \
96- .drv={.chip_address = DT_INST_PROP(inst, chip_address), \
97- .IntE = DT_INST_PROP_OR(inst, IntE,DEFAULT_INT_E), \
98- .CQCtrlA = DT_INST_PROP_OR(inst, CQCtrlA,DEFAULT_CQ_CTRL), \
99- .spi_slave_name = &maxim_14819_cfg_##inst, \
100- .CQCtrlB = DT_INST_PROP_OR(inst, CQCtrlB,DEFAULT_CQ_CTRL), \
101- .LEDCtrl = DT_INST_PROP_OR(inst, LEDCtrl,DEFAULT_LED_CTRL), \
102- .CQCfgA = DT_INST_PROP_OR(inst, CQCfgA,DEFAULT_CQC_CFG), \
103- .CQCfgB = DT_INST_PROP_OR(inst, CQCfgB,DEFAULT_CQC_CFG), \
104- .LPCnfgA = DT_INST_PROP_OR(inst, LPCnfgA,DEFAULT_LPCNFG), \
105- .LPCnfgB = DT_INST_PROP_OR(inst, LPCnfgB,DEFAULT_LPCNFG), \
106- .IOStCfgA = DT_INST_PROP_OR(inst, IOStCfgA,DEFAULT_IOST_CFG), \
107- .IOStCfgB = DT_INST_PROP_OR(inst, IOStCfgB,DEFAULT_IOST_CFG), \
108- .DrvCurrLim = DT_INST_PROP_OR(inst, DrvCurrLim,DEFAULT_DRV_CURR_LIMIT), \
109- .Clock = DT_INST_PROP_OR(inst, Clock,DEFAULT_CLOCK)} \
110- }; \
111- DEVICE_DT_INST_DEFINE(inst, \
112- maxim_init, \
113- NULL, \
114- &maxim_14819_data_##inst, \
115- &maxim_14819_cfg_##inst, \
116- POST_KERNEL, \
117- CONFIG_TRANSCIEVER_INIT_PRIORITY, \
97+
98+ #define MAXIM14819_DEFINE (inst ) \
99+ static struct maxim_14819_data maxim_14819_data_##inst ={ \
100+ .hw = NULL, \
101+ .isr = {.cb = {{0}}, .arg = NULL}, \
102+ .irq_dt = GPIO_DT_SPEC_GET(DT_DRV_INST(inst), irq_gpios) \
103+ }; \
104+ static const struct maxim_14819_cfg maxim_14819_cfg_##inst = { \
105+ .id = inst, \
106+ .spi = SPI_DT_SPEC_GET(DT_DRV_INST(inst), \
107+ SPI_WORD_SET(8) | SPI_TRANSFER_MSB, 0), \
108+ .drv={ \
109+ .chip_address = DT_INST_PROP(inst, chip_address), \
110+ .chip_irq = (uint32_t)&maxim_14819_data_##inst, \
111+ .spi_slave_name = (const char *)&maxim_14819_cfg_##inst,\
112+ .IntE = DT_INST_PROP_OR(inst, IntE,DEFAULT_INT_E), \
113+ .CQCtrlA = DT_INST_PROP_OR(inst, CQCtrlA,DEFAULT_CQ_CTRL), \
114+ .CQCtrlB = DT_INST_PROP_OR(inst, CQCtrlB,DEFAULT_CQ_CTRL), \
115+ .LEDCtrl = DT_INST_PROP_OR(inst, LEDCtrl,DEFAULT_LED_CTRL), \
116+ .CQCfgA = DT_INST_PROP_OR(inst, CQCfgA,DEFAULT_CQC_CFG), \
117+ .CQCfgB = DT_INST_PROP_OR(inst, CQCfgB,DEFAULT_CQC_CFG), \
118+ .LPCnfgA = DT_INST_PROP_OR(inst, LPCnfgA,DEFAULT_LPCNFG), \
119+ .LPCnfgB = DT_INST_PROP_OR(inst, LPCnfgB,DEFAULT_LPCNFG), \
120+ .IOStCfgA = DT_INST_PROP_OR(inst, IOStCfgA,DEFAULT_IOST_CFG), \
121+ .IOStCfgB = DT_INST_PROP_OR(inst, IOStCfgB,DEFAULT_IOST_CFG), \
122+ .DrvCurrLim = DT_INST_PROP_OR(inst, \
123+ DrvCurrLim,DEFAULT_DRV_CURR_LIMIT), \
124+ .Clock = DT_INST_PROP_OR(inst, Clock,DEFAULT_CLOCK) \
125+ } \
126+ }; \
127+ DEVICE_DT_INST_DEFINE(inst, \
128+ maxim_init, \
129+ NULL, \
130+ &maxim_14819_data_##inst, \
131+ &maxim_14819_cfg_##inst, \
132+ POST_KERNEL, \
133+ CONFIG_TRANSCIEVER_INIT_PRIORITY, \
118134 NULL);
119135
120136DT_INST_FOREACH_STATUS_OKAY (MAXIM14819_DEFINE )
@@ -126,13 +142,16 @@ static const struct device *maxim14819_devices[] = {
126142
127143iolink_hw_drv_t * get_drv (uint8_t port ){
128144 if (port >= CONFIG_IOLINK_NUM_PORTS ) {
145+ LOG_ERR ("port >= CONFIG_IOLINK_NUM_PORTS" );
129146 return NULL ;
130147 }
131148 const uint8_t drv_idx = port / TRANSCIEVER_MAX_PORTS ;
132149 if (drv_idx > DT_NUM_INST_STATUS_OKAY (DT_DRV_COMPAT )) {
150+ LOG_ERR ("Driver instance %d > static instances" , drv_idx );
133151 return NULL ;
134152 }
135153 if (!device_is_ready (maxim14819_devices [port ])) {
154+ LOG_ERR ("Driver %d status != ok" , drv_idx );
136155 return NULL ;
137156 }
138157 struct maxim_14819_data * data = maxim14819_devices [drv_idx ]-> data ;
@@ -148,13 +167,13 @@ void _iolink_pl_hw_spi_transfer (
148167 const void * data_written ,
149168 size_t n_bytes_to_transfer )
150169{
151- const struct spi_buf tx_buf = {.buf = (uint8_t * )(data_written ), .len = n_bytes_to_transfer };
152- const struct spi_buf rx_buf = {.buf = data_read , .len = n_bytes_to_transfer };
153- struct spi_buf_set tx_set = {.buffers = & tx_buf , .count = 1 };
154- struct spi_buf_set rx_set = {.buffers = & rx_buf , .count = 1 };
155- const int spi_res = spi_transceive_dt (device ,
156- & tx_set ,
157- & rx_set );
170+ const struct spi_buf tx_buf = {.buf = (uint8_t * )(data_written ), .len = n_bytes_to_transfer };
171+ const struct spi_buf rx_buf = {.buf = data_read , .len = n_bytes_to_transfer };
172+ struct spi_buf_set tx_set = {.buffers = & tx_buf , .count = 1 };
173+ struct spi_buf_set rx_set = {.buffers = & rx_buf , .count = 1 };
174+ const int spi_res = spi_transceive_dt (device ,
175+ & tx_set ,
176+ & rx_set );
158177}
159178
160179void * _iolink_pl_hw_spi_init (const char * device )
0 commit comments