1 /*
2 * Copyright (c) 2018 Diego Sueiro
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #define DT_DRV_COMPAT silabs_gecko_i2c
8
9 #include <errno.h>
10 #include <zephyr/drivers/i2c.h>
11 #include <zephyr/kernel.h>
12 #include <zephyr/sys/util.h>
13 #include <em_cmu.h>
14 #include <em_i2c.h>
15 #include <em_gpio.h>
16 #include <soc.h>
17
18 #define LOG_LEVEL CONFIG_I2C_LOG_LEVEL
19 #include <zephyr/logging/log.h>
20 LOG_MODULE_REGISTER(i2c_gecko);
21
22 #include "i2c-priv.h"
23
24 #define DEV_BASE(dev) ((I2C_TypeDef *)((const struct i2c_gecko_config *const)(dev)->config)->base)
25
26 struct i2c_gecko_config {
27 I2C_TypeDef *base;
28 CMU_Clock_TypeDef clock;
29 uint32_t bitrate;
30 struct soc_gpio_pin pin_sda;
31 struct soc_gpio_pin pin_scl;
32 #ifdef CONFIG_SOC_GECKO_HAS_INDIVIDUAL_PIN_LOCATION
33 uint8_t loc_sda;
34 uint8_t loc_scl;
35 #else
36 uint8_t loc;
37 #endif
38 #if defined(CONFIG_I2C_TARGET)
39 void (*irq_config_func)(const struct device *dev);
40 #endif
41 };
42
43 struct i2c_gecko_data {
44 struct k_sem device_sync_sem;
45 uint32_t dev_config;
46 #if defined(CONFIG_I2C_TARGET)
47 struct i2c_target_config *target_cfg;
48 #endif
49 };
50
i2c_gecko_config_pins(const struct device * dev,const struct soc_gpio_pin * pin_sda,const struct soc_gpio_pin * pin_scl)51 void i2c_gecko_config_pins(const struct device *dev, const struct soc_gpio_pin *pin_sda,
52 const struct soc_gpio_pin *pin_scl)
53 {
54 I2C_TypeDef *base = DEV_BASE(dev);
55 const struct i2c_gecko_config *config = dev->config;
56
57 GPIO_PinModeSet(pin_scl->port, pin_scl->pin, pin_scl->mode, pin_scl->out);
58 GPIO_PinModeSet(pin_sda->port, pin_sda->pin, pin_sda->mode, pin_sda->out);
59
60 #ifdef CONFIG_SOC_GECKO_HAS_INDIVIDUAL_PIN_LOCATION
61 base->ROUTEPEN = I2C_ROUTEPEN_SDAPEN | I2C_ROUTEPEN_SCLPEN;
62 base->ROUTELOC0 = (config->loc_sda << _I2C_ROUTELOC0_SDALOC_SHIFT) |
63 (config->loc_scl << _I2C_ROUTELOC0_SCLLOC_SHIFT);
64 #elif defined(GPIO_I2C_ROUTEEN_SCLPEN) && defined(GPIO_I2C_ROUTEEN_SDAPEN)
65 GPIO->I2CROUTE[I2C_NUM(base)].ROUTEEN = GPIO_I2C_ROUTEEN_SCLPEN | GPIO_I2C_ROUTEEN_SDAPEN;
66 GPIO->I2CROUTE[I2C_NUM(base)].SCLROUTE =
67 (config->pin_scl.pin << _GPIO_I2C_SCLROUTE_PIN_SHIFT) |
68 (config->pin_scl.port << _GPIO_I2C_SCLROUTE_PORT_SHIFT);
69 GPIO->I2CROUTE[I2C_NUM(base)].SDAROUTE =
70 (config->pin_sda.pin << _GPIO_I2C_SDAROUTE_PIN_SHIFT) |
71 (config->pin_sda.port << _GPIO_I2C_SDAROUTE_PORT_SHIFT);
72 #else
73 base->ROUTE = I2C_ROUTE_SDAPEN | I2C_ROUTE_SCLPEN | (config->loc << 8);
74 #endif
75 }
76
i2c_gecko_configure(const struct device * dev,uint32_t dev_config_raw)77 static int i2c_gecko_configure(const struct device *dev, uint32_t dev_config_raw)
78 {
79 I2C_TypeDef *base = DEV_BASE(dev);
80 struct i2c_gecko_data *data = dev->data;
81 I2C_Init_TypeDef i2cInit = I2C_INIT_DEFAULT;
82 uint32_t baudrate;
83
84 if (!(I2C_MODE_CONTROLLER & dev_config_raw)) {
85 return -EINVAL;
86 }
87
88 switch (I2C_SPEED_GET(dev_config_raw)) {
89 case I2C_SPEED_STANDARD:
90 baudrate = KHZ(100);
91 break;
92 case I2C_SPEED_FAST:
93 baudrate = KHZ(400);
94 break;
95 case I2C_SPEED_FAST_PLUS:
96 baudrate = MHZ(1);
97 break;
98 default:
99 return -EINVAL;
100 }
101
102 data->dev_config = dev_config_raw;
103 i2cInit.freq = baudrate;
104
105 #if defined(CONFIG_I2C_TARGET)
106 i2cInit.master = 0;
107 #endif
108
109 I2C_Init(base, &i2cInit);
110
111 return 0;
112 }
113
i2c_gecko_transfer(const struct device * dev,struct i2c_msg * msgs,uint8_t num_msgs,uint16_t addr)114 static int i2c_gecko_transfer(const struct device *dev, struct i2c_msg *msgs, uint8_t num_msgs,
115 uint16_t addr)
116 {
117 I2C_TypeDef *base = DEV_BASE(dev);
118 struct i2c_gecko_data *data = dev->data;
119 I2C_TransferSeq_TypeDef seq;
120 I2C_TransferReturn_TypeDef ret = -EIO;
121 uint32_t timeout = 300000U;
122
123 if (!num_msgs) {
124 return 0;
125 }
126
127 seq.addr = addr << 1;
128
129 do {
130 seq.buf[0].data = msgs->buf;
131 seq.buf[0].len = msgs->len;
132
133 if ((msgs->flags & I2C_MSG_RW_MASK) == I2C_MSG_READ) {
134 seq.flags = I2C_FLAG_READ;
135 } else {
136 seq.flags = I2C_FLAG_WRITE;
137 if (num_msgs > 1) {
138 /* Next message */
139 msgs++;
140 num_msgs--;
141 if ((msgs->flags & I2C_MSG_RW_MASK) == I2C_MSG_READ) {
142 seq.flags = I2C_FLAG_WRITE_READ;
143 } else {
144 seq.flags = I2C_FLAG_WRITE_WRITE;
145 }
146 seq.buf[1].data = msgs->buf;
147 seq.buf[1].len = msgs->len;
148 }
149 }
150
151 if (data->dev_config & I2C_ADDR_10_BITS) {
152 seq.flags |= I2C_FLAG_10BIT_ADDR;
153 }
154
155 /* Do a polled transfer */
156 ret = I2C_TransferInit(base, &seq);
157 while (ret == i2cTransferInProgress && timeout--) {
158 ret = I2C_Transfer(base);
159 }
160
161 if (ret != i2cTransferDone) {
162 goto finish;
163 }
164
165 /* Next message */
166 msgs++;
167 num_msgs--;
168 } while (num_msgs);
169
170 finish:
171 if (ret != i2cTransferDone) {
172 ret = -EIO;
173 }
174 return ret;
175 }
176
i2c_gecko_init(const struct device * dev)177 static int i2c_gecko_init(const struct device *dev)
178 {
179 const struct i2c_gecko_config *config = dev->config;
180 uint32_t bitrate_cfg;
181 int error;
182
183 CMU_ClockEnable(config->clock, true);
184
185 i2c_gecko_config_pins(dev, &config->pin_sda, &config->pin_scl);
186
187 bitrate_cfg = i2c_map_dt_bitrate(config->bitrate);
188
189 error = i2c_gecko_configure(dev, I2C_MODE_CONTROLLER | bitrate_cfg);
190 if (error) {
191 return error;
192 }
193
194 return 0;
195 }
196
197 #if defined(CONFIG_I2C_TARGET)
i2c_gecko_target_register(const struct device * dev,struct i2c_target_config * cfg)198 static int i2c_gecko_target_register(const struct device *dev, struct i2c_target_config *cfg)
199 {
200 const struct i2c_gecko_config *config = dev->config;
201 struct i2c_gecko_data *data = dev->data;
202
203 data->target_cfg = cfg;
204
205 I2C_SlaveAddressSet(config->base, cfg->address << _I2C_SADDR_ADDR_SHIFT);
206 /* Match with specified address, no wildcards in address */
207 I2C_SlaveAddressMaskSet(config->base, _I2C_SADDRMASK_SADDRMASK_MASK);
208
209 I2C_IntDisable(config->base, _I2C_IEN_MASK);
210 I2C_IntEnable(config->base, I2C_IEN_ADDR | I2C_IEN_RXDATAV | I2C_IEN_ACK | I2C_IEN_SSTOP |
211 I2C_IEN_BUSERR | I2C_IEN_ARBLOST);
212
213 config->irq_config_func(dev);
214
215 return 0;
216 }
217
i2c_gecko_target_unregister(const struct device * dev,struct i2c_target_config * cfg)218 static int i2c_gecko_target_unregister(const struct device *dev, struct i2c_target_config *cfg)
219 {
220 const struct i2c_gecko_config *config = dev->config;
221 struct i2c_gecko_data *data = dev->data;
222
223 data->target_cfg = NULL;
224
225 I2C_IntDisable(config->base, _I2C_IEN_MASK);
226
227 return 0;
228 }
229 #endif
230
231 static const struct i2c_driver_api i2c_gecko_driver_api = {
232 .configure = i2c_gecko_configure,
233 .transfer = i2c_gecko_transfer,
234 #if defined(CONFIG_I2C_TARGET)
235 .target_register = i2c_gecko_target_register,
236 .target_unregister = i2c_gecko_target_unregister,
237 #endif
238 };
239
240 #if defined(CONFIG_I2C_TARGET)
i2c_gecko_isr(const struct device * dev)241 void i2c_gecko_isr(const struct device *dev)
242 {
243 const struct i2c_gecko_config *config = dev->config;
244 struct i2c_gecko_data *data = dev->data;
245
246 uint32_t pending;
247 uint32_t rx_byte;
248 uint8_t tx_byte;
249
250 pending = config->base->IF;
251
252 /* If some sort of fault, abort transfer. */
253 if (pending & (I2C_IF_BUSERR | I2C_IF_ARBLOST)) {
254 LOG_ERR("I2C Bus Error");
255 I2C_IntClear(config->base, I2C_IF_BUSERR);
256 I2C_IntClear(config->base, I2C_IF_ARBLOST);
257 } else {
258 if (pending & I2C_IF_ADDR) {
259 /* Address Match, indicating that reception is started */
260 rx_byte = config->base->RXDATA;
261 config->base->CMD = I2C_CMD_ACK;
262
263 /* Check if read bit set */
264 if (rx_byte & 0x1) {
265 data->target_cfg->callbacks->read_requested(data->target_cfg,
266 &tx_byte);
267 config->base->TXDATA = tx_byte;
268 } else {
269 data->target_cfg->callbacks->write_requested(data->target_cfg);
270 }
271
272 I2C_IntClear(config->base, I2C_IF_ADDR | I2C_IF_RXDATAV);
273 } else if (pending & I2C_IF_RXDATAV) {
274 rx_byte = config->base->RXDATA;
275 /* Read new data and write to target address */
276 data->target_cfg->callbacks->write_received(data->target_cfg, rx_byte);
277 config->base->CMD = I2C_CMD_ACK;
278
279 I2C_IntClear(config->base, I2C_IF_RXDATAV);
280 }
281
282 if (pending & I2C_IF_ACK) {
283 /* Leader ACK'ed, so requesting more data */
284 data->target_cfg->callbacks->read_processed(data->target_cfg, &tx_byte);
285 config->base->TXDATA = tx_byte;
286
287 I2C_IntClear(config->base, I2C_IF_ACK);
288 }
289
290 if (pending & I2C_IF_SSTOP) {
291 /* End of transaction */
292 data->target_cfg->callbacks->stop(data->target_cfg);
293 I2C_IntClear(config->base, I2C_IF_SSTOP);
294 }
295 }
296 }
297 #endif
298
299 #ifdef CONFIG_SOC_GECKO_HAS_INDIVIDUAL_PIN_LOCATION
300 #define I2C_LOC_DATA(idx) \
301 .loc_sda = DT_INST_PROP_BY_IDX(idx, location_sda, 0), \
302 .loc_scl = DT_INST_PROP_BY_IDX(idx, location_scl, 0)
303 #define I2C_VALIDATE_LOC(idx) BUILD_ASSERT(true, "")
304 #else
305 #define I2C_VALIDATE_LOC(idx) \
306 BUILD_ASSERT(DT_INST_PROP_BY_IDX(idx, location_sda, 0) == \
307 DT_INST_PROP_BY_IDX(idx, location_scl, 0), \
308 "DTS location-* properties must be equal")
309 #define I2C_LOC_DATA(idx) .loc = DT_INST_PROP_BY_IDX(idx, location_scl, 0)
310 #endif
311
312 #if defined(CONFIG_I2C_TARGET)
313 #define GECKO_I2C_IRQ_DEF(idx) static void i2c_gecko_config_func_##idx(const struct device *dev);
314 #define GECKO_I2C_IRQ_DATA(idx) .irq_config_func = i2c_gecko_config_func_##idx,
315 #define GECKO_I2C_IRQ_HANDLER(idx) \
316 static void i2c_gecko_config_func_##idx(const struct device *dev) \
317 { \
318 IRQ_CONNECT(DT_INST_IRQ(idx, irq), DT_INST_IRQ(idx, priority), i2c_gecko_isr, \
319 DEVICE_DT_INST_GET(idx), 0); \
320 irq_enable(DT_INST_IRQ(idx, irq)); \
321 }
322 #else
323 #define GECKO_I2C_IRQ_HANDLER(idx)
324 #define GECKO_I2C_IRQ_DEF(idx)
325 #define GECKO_I2C_IRQ_DATA(idx)
326 #endif
327
328 #define I2C_INIT(idx) \
329 I2C_VALIDATE_LOC(idx); \
330 GECKO_I2C_IRQ_DEF(idx) \
331 static const struct i2c_gecko_config i2c_gecko_config_##idx = { \
332 .base = (I2C_TypeDef *)DT_INST_REG_ADDR(idx), \
333 .clock = cmuClock_I2C##idx, \
334 .pin_sda = {DT_INST_PROP_BY_IDX(idx, location_sda, 1), \
335 DT_INST_PROP_BY_IDX(idx, location_sda, 2), gpioModeWiredAnd, 1}, \
336 .pin_scl = {DT_INST_PROP_BY_IDX(idx, location_scl, 1), \
337 DT_INST_PROP_BY_IDX(idx, location_scl, 2), gpioModeWiredAnd, 1}, \
338 I2C_LOC_DATA(idx), \
339 .bitrate = DT_INST_PROP(idx, clock_frequency), \
340 GECKO_I2C_IRQ_DATA(idx)}; \
341 static struct i2c_gecko_data i2c_gecko_data_##idx; \
342 I2C_DEVICE_DT_INST_DEFINE(idx, i2c_gecko_init, NULL, &i2c_gecko_data_##idx, \
343 &i2c_gecko_config_##idx, POST_KERNEL, CONFIG_I2C_INIT_PRIORITY, \
344 &i2c_gecko_driver_api); \
345 GECKO_I2C_IRQ_HANDLER(idx)
346
347 DT_INST_FOREACH_STATUS_OKAY(I2C_INIT)
348