18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Copyright (C) 2014 Uwe Kleine-Koenig for Pengutronix 48c2ecf20Sopenharmony_ci */ 58c2ecf20Sopenharmony_ci#include <linux/module.h> 68c2ecf20Sopenharmony_ci#include <linux/platform_device.h> 78c2ecf20Sopenharmony_ci#include <linux/i2c.h> 88c2ecf20Sopenharmony_ci#include <linux/io.h> 98c2ecf20Sopenharmony_ci#include <linux/interrupt.h> 108c2ecf20Sopenharmony_ci#include <linux/err.h> 118c2ecf20Sopenharmony_ci#include <linux/clk.h> 128c2ecf20Sopenharmony_ci 138c2ecf20Sopenharmony_ci#define DRIVER_NAME "efm32-i2c" 148c2ecf20Sopenharmony_ci 158c2ecf20Sopenharmony_ci#define MASK_VAL(mask, val) ((val << __ffs(mask)) & mask) 168c2ecf20Sopenharmony_ci 178c2ecf20Sopenharmony_ci#define REG_CTRL 0x00 188c2ecf20Sopenharmony_ci#define REG_CTRL_EN 0x00001 198c2ecf20Sopenharmony_ci#define REG_CTRL_SLAVE 0x00002 208c2ecf20Sopenharmony_ci#define REG_CTRL_AUTOACK 0x00004 218c2ecf20Sopenharmony_ci#define REG_CTRL_AUTOSE 0x00008 228c2ecf20Sopenharmony_ci#define REG_CTRL_AUTOSN 0x00010 238c2ecf20Sopenharmony_ci#define REG_CTRL_ARBDIS 0x00020 248c2ecf20Sopenharmony_ci#define REG_CTRL_GCAMEN 0x00040 258c2ecf20Sopenharmony_ci#define REG_CTRL_CLHR__MASK 0x00300 268c2ecf20Sopenharmony_ci#define REG_CTRL_BITO__MASK 0x03000 278c2ecf20Sopenharmony_ci#define REG_CTRL_BITO_OFF 0x00000 288c2ecf20Sopenharmony_ci#define REG_CTRL_BITO_40PCC 0x01000 298c2ecf20Sopenharmony_ci#define REG_CTRL_BITO_80PCC 0x02000 308c2ecf20Sopenharmony_ci#define REG_CTRL_BITO_160PCC 0x03000 318c2ecf20Sopenharmony_ci#define REG_CTRL_GIBITO 0x08000 328c2ecf20Sopenharmony_ci#define REG_CTRL_CLTO__MASK 0x70000 338c2ecf20Sopenharmony_ci#define REG_CTRL_CLTO_OFF 0x00000 348c2ecf20Sopenharmony_ci 358c2ecf20Sopenharmony_ci#define REG_CMD 0x04 368c2ecf20Sopenharmony_ci#define REG_CMD_START 0x00001 378c2ecf20Sopenharmony_ci#define REG_CMD_STOP 0x00002 388c2ecf20Sopenharmony_ci#define REG_CMD_ACK 0x00004 398c2ecf20Sopenharmony_ci#define REG_CMD_NACK 0x00008 408c2ecf20Sopenharmony_ci#define REG_CMD_CONT 0x00010 418c2ecf20Sopenharmony_ci#define REG_CMD_ABORT 0x00020 428c2ecf20Sopenharmony_ci#define REG_CMD_CLEARTX 0x00040 438c2ecf20Sopenharmony_ci#define REG_CMD_CLEARPC 0x00080 448c2ecf20Sopenharmony_ci 458c2ecf20Sopenharmony_ci#define REG_STATE 0x08 468c2ecf20Sopenharmony_ci#define REG_STATE_BUSY 0x00001 478c2ecf20Sopenharmony_ci#define REG_STATE_MASTER 0x00002 488c2ecf20Sopenharmony_ci#define REG_STATE_TRANSMITTER 0x00004 498c2ecf20Sopenharmony_ci#define REG_STATE_NACKED 0x00008 508c2ecf20Sopenharmony_ci#define REG_STATE_BUSHOLD 0x00010 518c2ecf20Sopenharmony_ci#define REG_STATE_STATE__MASK 0x000e0 528c2ecf20Sopenharmony_ci#define REG_STATE_STATE_IDLE 0x00000 538c2ecf20Sopenharmony_ci#define REG_STATE_STATE_WAIT 0x00020 548c2ecf20Sopenharmony_ci#define REG_STATE_STATE_START 0x00040 558c2ecf20Sopenharmony_ci#define REG_STATE_STATE_ADDR 0x00060 568c2ecf20Sopenharmony_ci#define REG_STATE_STATE_ADDRACK 0x00080 578c2ecf20Sopenharmony_ci#define REG_STATE_STATE_DATA 0x000a0 588c2ecf20Sopenharmony_ci#define REG_STATE_STATE_DATAACK 0x000c0 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_ci#define REG_STATUS 0x0c 618c2ecf20Sopenharmony_ci#define REG_STATUS_PSTART 0x00001 628c2ecf20Sopenharmony_ci#define REG_STATUS_PSTOP 0x00002 638c2ecf20Sopenharmony_ci#define REG_STATUS_PACK 0x00004 648c2ecf20Sopenharmony_ci#define REG_STATUS_PNACK 0x00008 658c2ecf20Sopenharmony_ci#define REG_STATUS_PCONT 0x00010 668c2ecf20Sopenharmony_ci#define REG_STATUS_PABORT 0x00020 678c2ecf20Sopenharmony_ci#define REG_STATUS_TXC 0x00040 688c2ecf20Sopenharmony_ci#define REG_STATUS_TXBL 0x00080 698c2ecf20Sopenharmony_ci#define REG_STATUS_RXDATAV 0x00100 708c2ecf20Sopenharmony_ci 718c2ecf20Sopenharmony_ci#define REG_CLKDIV 0x10 728c2ecf20Sopenharmony_ci#define REG_CLKDIV_DIV__MASK 0x001ff 738c2ecf20Sopenharmony_ci#define REG_CLKDIV_DIV(div) MASK_VAL(REG_CLKDIV_DIV__MASK, (div)) 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_ci#define REG_SADDR 0x14 768c2ecf20Sopenharmony_ci#define REG_SADDRMASK 0x18 778c2ecf20Sopenharmony_ci#define REG_RXDATA 0x1c 788c2ecf20Sopenharmony_ci#define REG_RXDATAP 0x20 798c2ecf20Sopenharmony_ci#define REG_TXDATA 0x24 808c2ecf20Sopenharmony_ci#define REG_IF 0x28 818c2ecf20Sopenharmony_ci#define REG_IF_START 0x00001 828c2ecf20Sopenharmony_ci#define REG_IF_RSTART 0x00002 838c2ecf20Sopenharmony_ci#define REG_IF_ADDR 0x00004 848c2ecf20Sopenharmony_ci#define REG_IF_TXC 0x00008 858c2ecf20Sopenharmony_ci#define REG_IF_TXBL 0x00010 868c2ecf20Sopenharmony_ci#define REG_IF_RXDATAV 0x00020 878c2ecf20Sopenharmony_ci#define REG_IF_ACK 0x00040 888c2ecf20Sopenharmony_ci#define REG_IF_NACK 0x00080 898c2ecf20Sopenharmony_ci#define REG_IF_MSTOP 0x00100 908c2ecf20Sopenharmony_ci#define REG_IF_ARBLOST 0x00200 918c2ecf20Sopenharmony_ci#define REG_IF_BUSERR 0x00400 928c2ecf20Sopenharmony_ci#define REG_IF_BUSHOLD 0x00800 938c2ecf20Sopenharmony_ci#define REG_IF_TXOF 0x01000 948c2ecf20Sopenharmony_ci#define REG_IF_RXUF 0x02000 958c2ecf20Sopenharmony_ci#define REG_IF_BITO 0x04000 968c2ecf20Sopenharmony_ci#define REG_IF_CLTO 0x08000 978c2ecf20Sopenharmony_ci#define REG_IF_SSTOP 0x10000 988c2ecf20Sopenharmony_ci 998c2ecf20Sopenharmony_ci#define REG_IFS 0x2c 1008c2ecf20Sopenharmony_ci#define REG_IFC 0x30 1018c2ecf20Sopenharmony_ci#define REG_IFC__MASK 0x1ffcf 1028c2ecf20Sopenharmony_ci 1038c2ecf20Sopenharmony_ci#define REG_IEN 0x34 1048c2ecf20Sopenharmony_ci 1058c2ecf20Sopenharmony_ci#define REG_ROUTE 0x38 1068c2ecf20Sopenharmony_ci#define REG_ROUTE_SDAPEN 0x00001 1078c2ecf20Sopenharmony_ci#define REG_ROUTE_SCLPEN 0x00002 1088c2ecf20Sopenharmony_ci#define REG_ROUTE_LOCATION__MASK 0x00700 1098c2ecf20Sopenharmony_ci#define REG_ROUTE_LOCATION(n) MASK_VAL(REG_ROUTE_LOCATION__MASK, (n)) 1108c2ecf20Sopenharmony_ci 1118c2ecf20Sopenharmony_cistruct efm32_i2c_ddata { 1128c2ecf20Sopenharmony_ci struct i2c_adapter adapter; 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_ci struct clk *clk; 1158c2ecf20Sopenharmony_ci void __iomem *base; 1168c2ecf20Sopenharmony_ci unsigned int irq; 1178c2ecf20Sopenharmony_ci u8 location; 1188c2ecf20Sopenharmony_ci unsigned long frequency; 1198c2ecf20Sopenharmony_ci 1208c2ecf20Sopenharmony_ci /* transfer data */ 1218c2ecf20Sopenharmony_ci struct completion done; 1228c2ecf20Sopenharmony_ci struct i2c_msg *msgs; 1238c2ecf20Sopenharmony_ci size_t num_msgs; 1248c2ecf20Sopenharmony_ci size_t current_word, current_msg; 1258c2ecf20Sopenharmony_ci int retval; 1268c2ecf20Sopenharmony_ci}; 1278c2ecf20Sopenharmony_ci 1288c2ecf20Sopenharmony_cistatic u32 efm32_i2c_read32(struct efm32_i2c_ddata *ddata, unsigned offset) 1298c2ecf20Sopenharmony_ci{ 1308c2ecf20Sopenharmony_ci return readl(ddata->base + offset); 1318c2ecf20Sopenharmony_ci} 1328c2ecf20Sopenharmony_ci 1338c2ecf20Sopenharmony_cistatic void efm32_i2c_write32(struct efm32_i2c_ddata *ddata, 1348c2ecf20Sopenharmony_ci unsigned offset, u32 value) 1358c2ecf20Sopenharmony_ci{ 1368c2ecf20Sopenharmony_ci writel(value, ddata->base + offset); 1378c2ecf20Sopenharmony_ci} 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_cistatic void efm32_i2c_send_next_msg(struct efm32_i2c_ddata *ddata) 1408c2ecf20Sopenharmony_ci{ 1418c2ecf20Sopenharmony_ci struct i2c_msg *cur_msg = &ddata->msgs[ddata->current_msg]; 1428c2ecf20Sopenharmony_ci 1438c2ecf20Sopenharmony_ci efm32_i2c_write32(ddata, REG_CMD, REG_CMD_START); 1448c2ecf20Sopenharmony_ci efm32_i2c_write32(ddata, REG_TXDATA, i2c_8bit_addr_from_msg(cur_msg)); 1458c2ecf20Sopenharmony_ci} 1468c2ecf20Sopenharmony_ci 1478c2ecf20Sopenharmony_cistatic void efm32_i2c_send_next_byte(struct efm32_i2c_ddata *ddata) 1488c2ecf20Sopenharmony_ci{ 1498c2ecf20Sopenharmony_ci struct i2c_msg *cur_msg = &ddata->msgs[ddata->current_msg]; 1508c2ecf20Sopenharmony_ci 1518c2ecf20Sopenharmony_ci if (ddata->current_word >= cur_msg->len) { 1528c2ecf20Sopenharmony_ci /* cur_msg completely transferred */ 1538c2ecf20Sopenharmony_ci ddata->current_word = 0; 1548c2ecf20Sopenharmony_ci ddata->current_msg += 1; 1558c2ecf20Sopenharmony_ci 1568c2ecf20Sopenharmony_ci if (ddata->current_msg >= ddata->num_msgs) { 1578c2ecf20Sopenharmony_ci efm32_i2c_write32(ddata, REG_CMD, REG_CMD_STOP); 1588c2ecf20Sopenharmony_ci complete(&ddata->done); 1598c2ecf20Sopenharmony_ci } else { 1608c2ecf20Sopenharmony_ci efm32_i2c_send_next_msg(ddata); 1618c2ecf20Sopenharmony_ci } 1628c2ecf20Sopenharmony_ci } else { 1638c2ecf20Sopenharmony_ci efm32_i2c_write32(ddata, REG_TXDATA, 1648c2ecf20Sopenharmony_ci cur_msg->buf[ddata->current_word++]); 1658c2ecf20Sopenharmony_ci } 1668c2ecf20Sopenharmony_ci} 1678c2ecf20Sopenharmony_ci 1688c2ecf20Sopenharmony_cistatic void efm32_i2c_recv_next_byte(struct efm32_i2c_ddata *ddata) 1698c2ecf20Sopenharmony_ci{ 1708c2ecf20Sopenharmony_ci struct i2c_msg *cur_msg = &ddata->msgs[ddata->current_msg]; 1718c2ecf20Sopenharmony_ci 1728c2ecf20Sopenharmony_ci cur_msg->buf[ddata->current_word] = efm32_i2c_read32(ddata, REG_RXDATA); 1738c2ecf20Sopenharmony_ci ddata->current_word += 1; 1748c2ecf20Sopenharmony_ci if (ddata->current_word >= cur_msg->len) { 1758c2ecf20Sopenharmony_ci /* cur_msg completely transferred */ 1768c2ecf20Sopenharmony_ci ddata->current_word = 0; 1778c2ecf20Sopenharmony_ci ddata->current_msg += 1; 1788c2ecf20Sopenharmony_ci 1798c2ecf20Sopenharmony_ci efm32_i2c_write32(ddata, REG_CMD, REG_CMD_NACK); 1808c2ecf20Sopenharmony_ci 1818c2ecf20Sopenharmony_ci if (ddata->current_msg >= ddata->num_msgs) { 1828c2ecf20Sopenharmony_ci efm32_i2c_write32(ddata, REG_CMD, REG_CMD_STOP); 1838c2ecf20Sopenharmony_ci complete(&ddata->done); 1848c2ecf20Sopenharmony_ci } else { 1858c2ecf20Sopenharmony_ci efm32_i2c_send_next_msg(ddata); 1868c2ecf20Sopenharmony_ci } 1878c2ecf20Sopenharmony_ci } else { 1888c2ecf20Sopenharmony_ci efm32_i2c_write32(ddata, REG_CMD, REG_CMD_ACK); 1898c2ecf20Sopenharmony_ci } 1908c2ecf20Sopenharmony_ci} 1918c2ecf20Sopenharmony_ci 1928c2ecf20Sopenharmony_cistatic irqreturn_t efm32_i2c_irq(int irq, void *dev_id) 1938c2ecf20Sopenharmony_ci{ 1948c2ecf20Sopenharmony_ci struct efm32_i2c_ddata *ddata = dev_id; 1958c2ecf20Sopenharmony_ci struct i2c_msg *cur_msg = &ddata->msgs[ddata->current_msg]; 1968c2ecf20Sopenharmony_ci u32 irqflag = efm32_i2c_read32(ddata, REG_IF); 1978c2ecf20Sopenharmony_ci u32 state = efm32_i2c_read32(ddata, REG_STATE); 1988c2ecf20Sopenharmony_ci 1998c2ecf20Sopenharmony_ci efm32_i2c_write32(ddata, REG_IFC, irqflag & REG_IFC__MASK); 2008c2ecf20Sopenharmony_ci 2018c2ecf20Sopenharmony_ci switch (state & REG_STATE_STATE__MASK) { 2028c2ecf20Sopenharmony_ci case REG_STATE_STATE_IDLE: 2038c2ecf20Sopenharmony_ci /* arbitration lost? */ 2048c2ecf20Sopenharmony_ci ddata->retval = -EAGAIN; 2058c2ecf20Sopenharmony_ci complete(&ddata->done); 2068c2ecf20Sopenharmony_ci break; 2078c2ecf20Sopenharmony_ci case REG_STATE_STATE_WAIT: 2088c2ecf20Sopenharmony_ci /* 2098c2ecf20Sopenharmony_ci * huh, this shouldn't happen. 2108c2ecf20Sopenharmony_ci * Reset hardware state and get out 2118c2ecf20Sopenharmony_ci */ 2128c2ecf20Sopenharmony_ci ddata->retval = -EIO; 2138c2ecf20Sopenharmony_ci efm32_i2c_write32(ddata, REG_CMD, 2148c2ecf20Sopenharmony_ci REG_CMD_STOP | REG_CMD_ABORT | 2158c2ecf20Sopenharmony_ci REG_CMD_CLEARTX | REG_CMD_CLEARPC); 2168c2ecf20Sopenharmony_ci complete(&ddata->done); 2178c2ecf20Sopenharmony_ci break; 2188c2ecf20Sopenharmony_ci case REG_STATE_STATE_START: 2198c2ecf20Sopenharmony_ci /* "caller" is expected to send an address */ 2208c2ecf20Sopenharmony_ci break; 2218c2ecf20Sopenharmony_ci case REG_STATE_STATE_ADDR: 2228c2ecf20Sopenharmony_ci /* wait for Ack or NAck of slave */ 2238c2ecf20Sopenharmony_ci break; 2248c2ecf20Sopenharmony_ci case REG_STATE_STATE_ADDRACK: 2258c2ecf20Sopenharmony_ci if (state & REG_STATE_NACKED) { 2268c2ecf20Sopenharmony_ci efm32_i2c_write32(ddata, REG_CMD, REG_CMD_STOP); 2278c2ecf20Sopenharmony_ci ddata->retval = -ENXIO; 2288c2ecf20Sopenharmony_ci complete(&ddata->done); 2298c2ecf20Sopenharmony_ci } else if (cur_msg->flags & I2C_M_RD) { 2308c2ecf20Sopenharmony_ci /* wait for slave to send first data byte */ 2318c2ecf20Sopenharmony_ci } else { 2328c2ecf20Sopenharmony_ci efm32_i2c_send_next_byte(ddata); 2338c2ecf20Sopenharmony_ci } 2348c2ecf20Sopenharmony_ci break; 2358c2ecf20Sopenharmony_ci case REG_STATE_STATE_DATA: 2368c2ecf20Sopenharmony_ci if (cur_msg->flags & I2C_M_RD) { 2378c2ecf20Sopenharmony_ci efm32_i2c_recv_next_byte(ddata); 2388c2ecf20Sopenharmony_ci } else { 2398c2ecf20Sopenharmony_ci /* wait for Ack or Nack of slave */ 2408c2ecf20Sopenharmony_ci } 2418c2ecf20Sopenharmony_ci break; 2428c2ecf20Sopenharmony_ci case REG_STATE_STATE_DATAACK: 2438c2ecf20Sopenharmony_ci if (state & REG_STATE_NACKED) { 2448c2ecf20Sopenharmony_ci efm32_i2c_write32(ddata, REG_CMD, REG_CMD_STOP); 2458c2ecf20Sopenharmony_ci complete(&ddata->done); 2468c2ecf20Sopenharmony_ci } else { 2478c2ecf20Sopenharmony_ci efm32_i2c_send_next_byte(ddata); 2488c2ecf20Sopenharmony_ci } 2498c2ecf20Sopenharmony_ci } 2508c2ecf20Sopenharmony_ci 2518c2ecf20Sopenharmony_ci return IRQ_HANDLED; 2528c2ecf20Sopenharmony_ci} 2538c2ecf20Sopenharmony_ci 2548c2ecf20Sopenharmony_cistatic int efm32_i2c_master_xfer(struct i2c_adapter *adap, 2558c2ecf20Sopenharmony_ci struct i2c_msg *msgs, int num) 2568c2ecf20Sopenharmony_ci{ 2578c2ecf20Sopenharmony_ci struct efm32_i2c_ddata *ddata = i2c_get_adapdata(adap); 2588c2ecf20Sopenharmony_ci int ret; 2598c2ecf20Sopenharmony_ci 2608c2ecf20Sopenharmony_ci if (ddata->msgs) 2618c2ecf20Sopenharmony_ci return -EBUSY; 2628c2ecf20Sopenharmony_ci 2638c2ecf20Sopenharmony_ci ddata->msgs = msgs; 2648c2ecf20Sopenharmony_ci ddata->num_msgs = num; 2658c2ecf20Sopenharmony_ci ddata->current_word = 0; 2668c2ecf20Sopenharmony_ci ddata->current_msg = 0; 2678c2ecf20Sopenharmony_ci ddata->retval = -EIO; 2688c2ecf20Sopenharmony_ci 2698c2ecf20Sopenharmony_ci reinit_completion(&ddata->done); 2708c2ecf20Sopenharmony_ci 2718c2ecf20Sopenharmony_ci dev_dbg(&ddata->adapter.dev, "state: %08x, status: %08x\n", 2728c2ecf20Sopenharmony_ci efm32_i2c_read32(ddata, REG_STATE), 2738c2ecf20Sopenharmony_ci efm32_i2c_read32(ddata, REG_STATUS)); 2748c2ecf20Sopenharmony_ci 2758c2ecf20Sopenharmony_ci efm32_i2c_send_next_msg(ddata); 2768c2ecf20Sopenharmony_ci 2778c2ecf20Sopenharmony_ci wait_for_completion(&ddata->done); 2788c2ecf20Sopenharmony_ci 2798c2ecf20Sopenharmony_ci if (ddata->current_msg >= ddata->num_msgs) 2808c2ecf20Sopenharmony_ci ret = ddata->num_msgs; 2818c2ecf20Sopenharmony_ci else 2828c2ecf20Sopenharmony_ci ret = ddata->retval; 2838c2ecf20Sopenharmony_ci 2848c2ecf20Sopenharmony_ci return ret; 2858c2ecf20Sopenharmony_ci} 2868c2ecf20Sopenharmony_ci 2878c2ecf20Sopenharmony_cistatic u32 efm32_i2c_functionality(struct i2c_adapter *adap) 2888c2ecf20Sopenharmony_ci{ 2898c2ecf20Sopenharmony_ci return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL; 2908c2ecf20Sopenharmony_ci} 2918c2ecf20Sopenharmony_ci 2928c2ecf20Sopenharmony_cistatic const struct i2c_algorithm efm32_i2c_algo = { 2938c2ecf20Sopenharmony_ci .master_xfer = efm32_i2c_master_xfer, 2948c2ecf20Sopenharmony_ci .functionality = efm32_i2c_functionality, 2958c2ecf20Sopenharmony_ci}; 2968c2ecf20Sopenharmony_ci 2978c2ecf20Sopenharmony_cistatic u32 efm32_i2c_get_configured_location(struct efm32_i2c_ddata *ddata) 2988c2ecf20Sopenharmony_ci{ 2998c2ecf20Sopenharmony_ci u32 reg = efm32_i2c_read32(ddata, REG_ROUTE); 3008c2ecf20Sopenharmony_ci 3018c2ecf20Sopenharmony_ci return (reg & REG_ROUTE_LOCATION__MASK) >> 3028c2ecf20Sopenharmony_ci __ffs(REG_ROUTE_LOCATION__MASK); 3038c2ecf20Sopenharmony_ci} 3048c2ecf20Sopenharmony_ci 3058c2ecf20Sopenharmony_cistatic int efm32_i2c_probe(struct platform_device *pdev) 3068c2ecf20Sopenharmony_ci{ 3078c2ecf20Sopenharmony_ci struct efm32_i2c_ddata *ddata; 3088c2ecf20Sopenharmony_ci struct resource *res; 3098c2ecf20Sopenharmony_ci unsigned long rate; 3108c2ecf20Sopenharmony_ci struct device_node *np = pdev->dev.of_node; 3118c2ecf20Sopenharmony_ci u32 location, frequency; 3128c2ecf20Sopenharmony_ci int ret; 3138c2ecf20Sopenharmony_ci u32 clkdiv; 3148c2ecf20Sopenharmony_ci 3158c2ecf20Sopenharmony_ci ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL); 3168c2ecf20Sopenharmony_ci if (!ddata) 3178c2ecf20Sopenharmony_ci return -ENOMEM; 3188c2ecf20Sopenharmony_ci platform_set_drvdata(pdev, ddata); 3198c2ecf20Sopenharmony_ci 3208c2ecf20Sopenharmony_ci init_completion(&ddata->done); 3218c2ecf20Sopenharmony_ci strlcpy(ddata->adapter.name, pdev->name, sizeof(ddata->adapter.name)); 3228c2ecf20Sopenharmony_ci ddata->adapter.owner = THIS_MODULE; 3238c2ecf20Sopenharmony_ci ddata->adapter.algo = &efm32_i2c_algo; 3248c2ecf20Sopenharmony_ci ddata->adapter.dev.parent = &pdev->dev; 3258c2ecf20Sopenharmony_ci ddata->adapter.dev.of_node = pdev->dev.of_node; 3268c2ecf20Sopenharmony_ci i2c_set_adapdata(&ddata->adapter, ddata); 3278c2ecf20Sopenharmony_ci 3288c2ecf20Sopenharmony_ci ddata->clk = devm_clk_get(&pdev->dev, NULL); 3298c2ecf20Sopenharmony_ci if (IS_ERR(ddata->clk)) { 3308c2ecf20Sopenharmony_ci ret = PTR_ERR(ddata->clk); 3318c2ecf20Sopenharmony_ci dev_err(&pdev->dev, "failed to get clock: %d\n", ret); 3328c2ecf20Sopenharmony_ci return ret; 3338c2ecf20Sopenharmony_ci } 3348c2ecf20Sopenharmony_ci 3358c2ecf20Sopenharmony_ci ddata->base = devm_platform_get_and_ioremap_resource(pdev, 0, &res); 3368c2ecf20Sopenharmony_ci if (IS_ERR(ddata->base)) 3378c2ecf20Sopenharmony_ci return PTR_ERR(ddata->base); 3388c2ecf20Sopenharmony_ci 3398c2ecf20Sopenharmony_ci if (resource_size(res) < 0x42) { 3408c2ecf20Sopenharmony_ci dev_err(&pdev->dev, "memory resource too small\n"); 3418c2ecf20Sopenharmony_ci return -EINVAL; 3428c2ecf20Sopenharmony_ci } 3438c2ecf20Sopenharmony_ci 3448c2ecf20Sopenharmony_ci ret = platform_get_irq(pdev, 0); 3458c2ecf20Sopenharmony_ci if (ret <= 0) { 3468c2ecf20Sopenharmony_ci if (!ret) 3478c2ecf20Sopenharmony_ci ret = -EINVAL; 3488c2ecf20Sopenharmony_ci return ret; 3498c2ecf20Sopenharmony_ci } 3508c2ecf20Sopenharmony_ci 3518c2ecf20Sopenharmony_ci ddata->irq = ret; 3528c2ecf20Sopenharmony_ci 3538c2ecf20Sopenharmony_ci ret = clk_prepare_enable(ddata->clk); 3548c2ecf20Sopenharmony_ci if (ret < 0) { 3558c2ecf20Sopenharmony_ci dev_err(&pdev->dev, "failed to enable clock (%d)\n", ret); 3568c2ecf20Sopenharmony_ci return ret; 3578c2ecf20Sopenharmony_ci } 3588c2ecf20Sopenharmony_ci 3598c2ecf20Sopenharmony_ci 3608c2ecf20Sopenharmony_ci ret = of_property_read_u32(np, "energymicro,location", &location); 3618c2ecf20Sopenharmony_ci 3628c2ecf20Sopenharmony_ci if (ret) 3638c2ecf20Sopenharmony_ci /* fall back to wrongly namespaced property */ 3648c2ecf20Sopenharmony_ci ret = of_property_read_u32(np, "efm32,location", &location); 3658c2ecf20Sopenharmony_ci 3668c2ecf20Sopenharmony_ci if (!ret) { 3678c2ecf20Sopenharmony_ci dev_dbg(&pdev->dev, "using location %u\n", location); 3688c2ecf20Sopenharmony_ci } else { 3698c2ecf20Sopenharmony_ci /* default to location configured in hardware */ 3708c2ecf20Sopenharmony_ci location = efm32_i2c_get_configured_location(ddata); 3718c2ecf20Sopenharmony_ci 3728c2ecf20Sopenharmony_ci dev_info(&pdev->dev, "fall back to location %u\n", location); 3738c2ecf20Sopenharmony_ci } 3748c2ecf20Sopenharmony_ci 3758c2ecf20Sopenharmony_ci ddata->location = location; 3768c2ecf20Sopenharmony_ci 3778c2ecf20Sopenharmony_ci ret = of_property_read_u32(np, "clock-frequency", &frequency); 3788c2ecf20Sopenharmony_ci if (!ret) { 3798c2ecf20Sopenharmony_ci dev_dbg(&pdev->dev, "using frequency %u\n", frequency); 3808c2ecf20Sopenharmony_ci } else { 3818c2ecf20Sopenharmony_ci frequency = I2C_MAX_STANDARD_MODE_FREQ; 3828c2ecf20Sopenharmony_ci dev_info(&pdev->dev, "defaulting to 100 kHz\n"); 3838c2ecf20Sopenharmony_ci } 3848c2ecf20Sopenharmony_ci ddata->frequency = frequency; 3858c2ecf20Sopenharmony_ci 3868c2ecf20Sopenharmony_ci rate = clk_get_rate(ddata->clk); 3878c2ecf20Sopenharmony_ci if (!rate) { 3888c2ecf20Sopenharmony_ci dev_err(&pdev->dev, "there is no input clock available\n"); 3898c2ecf20Sopenharmony_ci ret = -EINVAL; 3908c2ecf20Sopenharmony_ci goto err_disable_clk; 3918c2ecf20Sopenharmony_ci } 3928c2ecf20Sopenharmony_ci clkdiv = DIV_ROUND_UP(rate, 8 * ddata->frequency) - 1; 3938c2ecf20Sopenharmony_ci if (clkdiv >= 0x200) { 3948c2ecf20Sopenharmony_ci dev_err(&pdev->dev, 3958c2ecf20Sopenharmony_ci "input clock too fast (%lu) to divide down to bus freq (%lu)", 3968c2ecf20Sopenharmony_ci rate, ddata->frequency); 3978c2ecf20Sopenharmony_ci ret = -EINVAL; 3988c2ecf20Sopenharmony_ci goto err_disable_clk; 3998c2ecf20Sopenharmony_ci } 4008c2ecf20Sopenharmony_ci 4018c2ecf20Sopenharmony_ci dev_dbg(&pdev->dev, "input clock = %lu, bus freq = %lu, clkdiv = %lu\n", 4028c2ecf20Sopenharmony_ci rate, ddata->frequency, (unsigned long)clkdiv); 4038c2ecf20Sopenharmony_ci efm32_i2c_write32(ddata, REG_CLKDIV, REG_CLKDIV_DIV(clkdiv)); 4048c2ecf20Sopenharmony_ci 4058c2ecf20Sopenharmony_ci efm32_i2c_write32(ddata, REG_ROUTE, REG_ROUTE_SDAPEN | 4068c2ecf20Sopenharmony_ci REG_ROUTE_SCLPEN | 4078c2ecf20Sopenharmony_ci REG_ROUTE_LOCATION(ddata->location)); 4088c2ecf20Sopenharmony_ci 4098c2ecf20Sopenharmony_ci efm32_i2c_write32(ddata, REG_CTRL, REG_CTRL_EN | 4108c2ecf20Sopenharmony_ci REG_CTRL_BITO_160PCC | 0 * REG_CTRL_GIBITO); 4118c2ecf20Sopenharmony_ci 4128c2ecf20Sopenharmony_ci efm32_i2c_write32(ddata, REG_IFC, REG_IFC__MASK); 4138c2ecf20Sopenharmony_ci efm32_i2c_write32(ddata, REG_IEN, REG_IF_TXC | REG_IF_ACK | REG_IF_NACK 4148c2ecf20Sopenharmony_ci | REG_IF_ARBLOST | REG_IF_BUSERR | REG_IF_RXDATAV); 4158c2ecf20Sopenharmony_ci 4168c2ecf20Sopenharmony_ci /* to make bus idle */ 4178c2ecf20Sopenharmony_ci efm32_i2c_write32(ddata, REG_CMD, REG_CMD_ABORT); 4188c2ecf20Sopenharmony_ci 4198c2ecf20Sopenharmony_ci ret = request_irq(ddata->irq, efm32_i2c_irq, 0, DRIVER_NAME, ddata); 4208c2ecf20Sopenharmony_ci if (ret < 0) { 4218c2ecf20Sopenharmony_ci dev_err(&pdev->dev, "failed to request irq (%d)\n", ret); 4228c2ecf20Sopenharmony_ci goto err_disable_clk; 4238c2ecf20Sopenharmony_ci } 4248c2ecf20Sopenharmony_ci 4258c2ecf20Sopenharmony_ci ret = i2c_add_adapter(&ddata->adapter); 4268c2ecf20Sopenharmony_ci if (ret) { 4278c2ecf20Sopenharmony_ci free_irq(ddata->irq, ddata); 4288c2ecf20Sopenharmony_ci 4298c2ecf20Sopenharmony_cierr_disable_clk: 4308c2ecf20Sopenharmony_ci clk_disable_unprepare(ddata->clk); 4318c2ecf20Sopenharmony_ci } 4328c2ecf20Sopenharmony_ci return ret; 4338c2ecf20Sopenharmony_ci} 4348c2ecf20Sopenharmony_ci 4358c2ecf20Sopenharmony_cistatic int efm32_i2c_remove(struct platform_device *pdev) 4368c2ecf20Sopenharmony_ci{ 4378c2ecf20Sopenharmony_ci struct efm32_i2c_ddata *ddata = platform_get_drvdata(pdev); 4388c2ecf20Sopenharmony_ci 4398c2ecf20Sopenharmony_ci i2c_del_adapter(&ddata->adapter); 4408c2ecf20Sopenharmony_ci free_irq(ddata->irq, ddata); 4418c2ecf20Sopenharmony_ci clk_disable_unprepare(ddata->clk); 4428c2ecf20Sopenharmony_ci 4438c2ecf20Sopenharmony_ci return 0; 4448c2ecf20Sopenharmony_ci} 4458c2ecf20Sopenharmony_ci 4468c2ecf20Sopenharmony_cistatic const struct of_device_id efm32_i2c_dt_ids[] = { 4478c2ecf20Sopenharmony_ci { 4488c2ecf20Sopenharmony_ci .compatible = "energymicro,efm32-i2c", 4498c2ecf20Sopenharmony_ci }, { 4508c2ecf20Sopenharmony_ci /* sentinel */ 4518c2ecf20Sopenharmony_ci } 4528c2ecf20Sopenharmony_ci}; 4538c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, efm32_i2c_dt_ids); 4548c2ecf20Sopenharmony_ci 4558c2ecf20Sopenharmony_cistatic struct platform_driver efm32_i2c_driver = { 4568c2ecf20Sopenharmony_ci .probe = efm32_i2c_probe, 4578c2ecf20Sopenharmony_ci .remove = efm32_i2c_remove, 4588c2ecf20Sopenharmony_ci 4598c2ecf20Sopenharmony_ci .driver = { 4608c2ecf20Sopenharmony_ci .name = DRIVER_NAME, 4618c2ecf20Sopenharmony_ci .of_match_table = efm32_i2c_dt_ids, 4628c2ecf20Sopenharmony_ci }, 4638c2ecf20Sopenharmony_ci}; 4648c2ecf20Sopenharmony_cimodule_platform_driver(efm32_i2c_driver); 4658c2ecf20Sopenharmony_ci 4668c2ecf20Sopenharmony_ciMODULE_AUTHOR("Uwe Kleine-Koenig <u.kleine-koenig@pengutronix.de>"); 4678c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("EFM32 i2c driver"); 4688c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2"); 4698c2ecf20Sopenharmony_ciMODULE_ALIAS("platform:" DRIVER_NAME); 470