27 #include <linux/kernel.h>
28 #include <linux/module.h>
31 #include <linux/i2c.h>
38 #define DRV_NAME "pmcmsptwi"
40 #define MSP_TWI_SF_CLK_REG_OFFSET 0x00
41 #define MSP_TWI_HS_CLK_REG_OFFSET 0x04
42 #define MSP_TWI_CFG_REG_OFFSET 0x08
43 #define MSP_TWI_CMD_REG_OFFSET 0x0c
44 #define MSP_TWI_ADD_REG_OFFSET 0x10
45 #define MSP_TWI_DAT_0_REG_OFFSET 0x14
46 #define MSP_TWI_DAT_1_REG_OFFSET 0x18
47 #define MSP_TWI_INT_STS_REG_OFFSET 0x1c
48 #define MSP_TWI_INT_MSK_REG_OFFSET 0x20
49 #define MSP_TWI_BUSY_REG_OFFSET 0x24
51 #define MSP_TWI_INT_STS_DONE (1 << 0)
52 #define MSP_TWI_INT_STS_LOST_ARBITRATION (1 << 1)
53 #define MSP_TWI_INT_STS_NO_RESPONSE (1 << 2)
54 #define MSP_TWI_INT_STS_DATA_COLLISION (1 << 3)
55 #define MSP_TWI_INT_STS_BUSY (1 << 4)
56 #define MSP_TWI_INT_STS_ALL 0x1f
58 #define MSP_MAX_BYTES_PER_RW 8
59 #define MSP_MAX_POLL 5
60 #define MSP_POLL_DELAY 10
61 #define MSP_IRQ_TIMEOUT (MSP_MAX_POLL * MSP_POLL_DELAY)
64 #define pmcmsptwi_readl __raw_readl
65 #define pmcmsptwi_writel __raw_writel
150 static inline u32 pmcmsptwi_clock_to_reg(
153 return ((clock->
filter & 0xf) << 12) | (clock->
clock & 0x03ff);
156 static inline void pmcmsptwi_reg_to_clock(
159 clock->
filter = (reg >> 12) & 0xf;
160 clock->
clock = reg & 0x03ff;
165 return ((cfg->
arbf & 0xf) << 12) |
166 ((cfg->
nak & 0xf) << 8) |
167 ((cfg->
add10 & 0x1) << 7) |
169 ((cfg->
arb & 0x1) << 1) |
175 cfg->
arbf = (reg >> 12) & 0xf;
176 cfg->
nak = (reg >> 8) & 0xf;
177 cfg->
add10 = (reg >> 7) & 0x1;
179 cfg->
arb = (reg >> 1) & 0x1;
200 static void pmcmsptwi_get_twi_config(
struct pmcmsptwi_cfg *cfg,
212 static void pmcmsptwi_set_twi_config(
const struct pmcmsptwi_cfg *cfg,
227 dev_dbg(&pmcmsptwi_adapter.dev,
228 "Result: Lost arbitration\n");
231 dev_dbg(&pmcmsptwi_adapter.dev,
232 "Result: No response\n");
235 dev_dbg(&pmcmsptwi_adapter.dev,
236 "Result: Data collision\n");
239 dev_dbg(&pmcmsptwi_adapter.dev,
240 "Result: Bus busy\n");
244 dev_dbg(&pmcmsptwi_adapter.dev,
"Result: Operation succeeded\n");
260 dev_dbg(&pmcmsptwi_adapter.dev,
"Got interrupt 0x%08x\n", reason);
281 dev_err(&pldev->
dev,
"IOMEM resource not found\n");
289 "Unable to get memory/io address region 0x%08x\n",
300 "Unable to ioremap address 0x%08x\n", res->
start);
324 "Could not assign TWI IRQ handler "
325 "to irq %d (continuing with poll)\n",
334 pmcmsptwi_set_clock_config(&pmcmsptwi_defclockcfg, &
pmcmsptwi_data);
339 pmcmsptwi_adapter.dev.parent = &pldev->
dev;
340 platform_set_drvdata(pldev, &pmcmsptwi_adapter);
345 dev_err(&pldev->
dev,
"Unable to register I2C adapter\n");
352 platform_set_drvdata(pldev,
NULL);
377 platform_set_drvdata(pldev,
NULL);
414 dev_dbg(&pmcmsptwi_adapter.dev,
"Result: Poll timeout\n");
427 dev_dbg(&pmcmsptwi_adapter.dev,
"Writing cmd reg 0x%08x\n", reg);
433 dev_dbg(&pmcmsptwi_adapter.dev,
434 "Result: IRQ timeout\n");
439 pmcmsptwi_poll_complete(data);
449 return ((cmd->
type & 0x3) << 8) |
467 dev_err(&pmcmsptwi_adapter.dev,
468 "%s: Cannot transfer less than 1 byte\n",
475 dev_err(&pmcmsptwi_adapter.dev,
476 "%s: Cannot transfer more than %d bytes\n",
482 dev_dbg(&pmcmsptwi_adapter.dev,
483 "Setting address to 0x%04x\n", cmd->
addr);
490 dev_dbg(&pmcmsptwi_adapter.dev,
"Writing 0x%016llx\n", tmp);
498 retval = pmcmsptwi_do_xfer(pmcmsptwi_cmd_to_reg(cmd), data);
512 dev_dbg(&pmcmsptwi_adapter.dev,
"Read 0x%016llx\n", tmp);
529 static int pmcmsptwi_master_xfer(
struct i2c_adapter *adap,
538 dev_dbg(&adap->
dev,
"%d messages unsupported\n", num);
540 }
else if (num == 2) {
542 struct i2c_msg *nextmsg = msg + 1;
553 "Non write-read dual messages unsupported\n");
571 dev_err(&adap->
dev,
"Zero-byte messages unsupported\n");
578 pmcmsptwi_get_twi_config(&newcfg, data);
579 memcpy(&oldcfg, &newcfg,
sizeof(oldcfg));
584 pmcmsptwi_set_twi_config(&newcfg, data);
588 ret = pmcmsptwi_xfer_cmd(&cmd, data);
591 pmcmsptwi_set_twi_config(&oldcfg, data);
593 dev_dbg(&adap->
dev,
"I2C %s of %d bytes %s\n",
618 .master_xfer = pmcmsptwi_master_xfer,
619 .functionality = pmcmsptwi_i2c_func,
625 .algo = &pmcmsptwi_algo,
630 .probe = pmcmsptwi_probe,