i2c-pmcmsp.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. /*
  2. * Specific bus support for PMC-TWI compliant implementation on MSP71xx.
  3. *
  4. * Copyright 2005-2007 PMC-Sierra, Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; either version 2 of the License, or (at your
  9. * option) any later version.
  10. *
  11. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  12. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  13. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
  14. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  15. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  16. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  17. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  18. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  19. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  20. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  21. */
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/i2c.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/completion.h>
  28. #include <linux/mutex.h>
  29. #include <linux/delay.h>
  30. #include <linux/io.h>
  31. #define DRV_NAME "pmcmsptwi"
  32. #define MSP_TWI_SF_CLK_REG_OFFSET 0x00
  33. #define MSP_TWI_HS_CLK_REG_OFFSET 0x04
  34. #define MSP_TWI_CFG_REG_OFFSET 0x08
  35. #define MSP_TWI_CMD_REG_OFFSET 0x0c
  36. #define MSP_TWI_ADD_REG_OFFSET 0x10
  37. #define MSP_TWI_DAT_0_REG_OFFSET 0x14
  38. #define MSP_TWI_DAT_1_REG_OFFSET 0x18
  39. #define MSP_TWI_INT_STS_REG_OFFSET 0x1c
  40. #define MSP_TWI_INT_MSK_REG_OFFSET 0x20
  41. #define MSP_TWI_BUSY_REG_OFFSET 0x24
  42. #define MSP_TWI_INT_STS_DONE (1 << 0)
  43. #define MSP_TWI_INT_STS_LOST_ARBITRATION (1 << 1)
  44. #define MSP_TWI_INT_STS_NO_RESPONSE (1 << 2)
  45. #define MSP_TWI_INT_STS_DATA_COLLISION (1 << 3)
  46. #define MSP_TWI_INT_STS_BUSY (1 << 4)
  47. #define MSP_TWI_INT_STS_ALL 0x1f
  48. #define MSP_MAX_BYTES_PER_RW 8
  49. #define MSP_MAX_POLL 5
  50. #define MSP_POLL_DELAY 10
  51. #define MSP_IRQ_TIMEOUT (MSP_MAX_POLL * MSP_POLL_DELAY)
  52. /* IO Operation macros */
  53. #define pmcmsptwi_readl __raw_readl
  54. #define pmcmsptwi_writel __raw_writel
  55. /* TWI command type */
  56. enum pmcmsptwi_cmd_type {
  57. MSP_TWI_CMD_WRITE = 0, /* Write only */
  58. MSP_TWI_CMD_READ = 1, /* Read only */
  59. MSP_TWI_CMD_WRITE_READ = 2, /* Write then Read */
  60. };
  61. /* The possible results of the xferCmd */
  62. enum pmcmsptwi_xfer_result {
  63. MSP_TWI_XFER_OK = 0,
  64. MSP_TWI_XFER_TIMEOUT,
  65. MSP_TWI_XFER_BUSY,
  66. MSP_TWI_XFER_DATA_COLLISION,
  67. MSP_TWI_XFER_NO_RESPONSE,
  68. MSP_TWI_XFER_LOST_ARBITRATION,
  69. };
  70. /* Corresponds to a PMCTWI clock configuration register */
  71. struct pmcmsptwi_clock {
  72. u8 filter; /* Bits 15:12, default = 0x03 */
  73. u16 clock; /* Bits 9:0, default = 0x001f */
  74. };
  75. struct pmcmsptwi_clockcfg {
  76. struct pmcmsptwi_clock standard; /* The standard/fast clock config */
  77. struct pmcmsptwi_clock highspeed; /* The highspeed clock config */
  78. };
  79. /* Corresponds to the main TWI configuration register */
  80. struct pmcmsptwi_cfg {
  81. u8 arbf; /* Bits 15:12, default=0x03 */
  82. u8 nak; /* Bits 11:8, default=0x03 */
  83. u8 add10; /* Bit 7, default=0x00 */
  84. u8 mst_code; /* Bits 6:4, default=0x00 */
  85. u8 arb; /* Bit 1, default=0x01 */
  86. u8 highspeed; /* Bit 0, default=0x00 */
  87. };
  88. /* A single pmctwi command to issue */
  89. struct pmcmsptwi_cmd {
  90. u16 addr; /* The slave address (7 or 10 bits) */
  91. enum pmcmsptwi_cmd_type type; /* The command type */
  92. u8 write_len; /* Number of bytes in the write buffer */
  93. u8 read_len; /* Number of bytes in the read buffer */
  94. u8 *write_data; /* Buffer of characters to send */
  95. u8 *read_data; /* Buffer to fill with incoming data */
  96. };
  97. /* The private data */
  98. struct pmcmsptwi_data {
  99. void __iomem *iobase; /* iomapped base for IO */
  100. int irq; /* IRQ to use (0 disables) */
  101. struct completion wait; /* Completion for xfer */
  102. struct mutex lock; /* Used for threadsafeness */
  103. enum pmcmsptwi_xfer_result last_result; /* result of last xfer */
  104. };
  105. /* The default settings */
  106. static const struct pmcmsptwi_clockcfg pmcmsptwi_defclockcfg = {
  107. .standard = {
  108. .filter = 0x3,
  109. .clock = 0x1f,
  110. },
  111. .highspeed = {
  112. .filter = 0x3,
  113. .clock = 0x1f,
  114. },
  115. };
  116. static const struct pmcmsptwi_cfg pmcmsptwi_defcfg = {
  117. .arbf = 0x03,
  118. .nak = 0x03,
  119. .add10 = 0x00,
  120. .mst_code = 0x00,
  121. .arb = 0x01,
  122. .highspeed = 0x00,
  123. };
  124. static struct pmcmsptwi_data pmcmsptwi_data;
  125. static struct i2c_adapter pmcmsptwi_adapter;
  126. /* inline helper functions */
  127. static inline u32 pmcmsptwi_clock_to_reg(
  128. const struct pmcmsptwi_clock *clock)
  129. {
  130. return ((clock->filter & 0xf) << 12) | (clock->clock & 0x03ff);
  131. }
  132. static inline u32 pmcmsptwi_cfg_to_reg(const struct pmcmsptwi_cfg *cfg)
  133. {
  134. return ((cfg->arbf & 0xf) << 12) |
  135. ((cfg->nak & 0xf) << 8) |
  136. ((cfg->add10 & 0x1) << 7) |
  137. ((cfg->mst_code & 0x7) << 4) |
  138. ((cfg->arb & 0x1) << 1) |
  139. (cfg->highspeed & 0x1);
  140. }
  141. static inline void pmcmsptwi_reg_to_cfg(u32 reg, struct pmcmsptwi_cfg *cfg)
  142. {
  143. cfg->arbf = (reg >> 12) & 0xf;
  144. cfg->nak = (reg >> 8) & 0xf;
  145. cfg->add10 = (reg >> 7) & 0x1;
  146. cfg->mst_code = (reg >> 4) & 0x7;
  147. cfg->arb = (reg >> 1) & 0x1;
  148. cfg->highspeed = reg & 0x1;
  149. }
  150. /*
  151. * Sets the current clock configuration
  152. */
  153. static void pmcmsptwi_set_clock_config(const struct pmcmsptwi_clockcfg *cfg,
  154. struct pmcmsptwi_data *data)
  155. {
  156. mutex_lock(&data->lock);
  157. pmcmsptwi_writel(pmcmsptwi_clock_to_reg(&cfg->standard),
  158. data->iobase + MSP_TWI_SF_CLK_REG_OFFSET);
  159. pmcmsptwi_writel(pmcmsptwi_clock_to_reg(&cfg->highspeed),
  160. data->iobase + MSP_TWI_HS_CLK_REG_OFFSET);
  161. mutex_unlock(&data->lock);
  162. }
  163. /*
  164. * Gets the current TWI bus configuration
  165. */
  166. static void pmcmsptwi_get_twi_config(struct pmcmsptwi_cfg *cfg,
  167. struct pmcmsptwi_data *data)
  168. {
  169. mutex_lock(&data->lock);
  170. pmcmsptwi_reg_to_cfg(pmcmsptwi_readl(
  171. data->iobase + MSP_TWI_CFG_REG_OFFSET), cfg);
  172. mutex_unlock(&data->lock);
  173. }
  174. /*
  175. * Sets the current TWI bus configuration
  176. */
  177. static void pmcmsptwi_set_twi_config(const struct pmcmsptwi_cfg *cfg,
  178. struct pmcmsptwi_data *data)
  179. {
  180. mutex_lock(&data->lock);
  181. pmcmsptwi_writel(pmcmsptwi_cfg_to_reg(cfg),
  182. data->iobase + MSP_TWI_CFG_REG_OFFSET);
  183. mutex_unlock(&data->lock);
  184. }
  185. /*
  186. * Parses the 'int_sts' register and returns a well-defined error code
  187. */
  188. static enum pmcmsptwi_xfer_result pmcmsptwi_get_result(u32 reg)
  189. {
  190. if (reg & MSP_TWI_INT_STS_LOST_ARBITRATION) {
  191. dev_dbg(&pmcmsptwi_adapter.dev,
  192. "Result: Lost arbitration\n");
  193. return MSP_TWI_XFER_LOST_ARBITRATION;
  194. } else if (reg & MSP_TWI_INT_STS_NO_RESPONSE) {
  195. dev_dbg(&pmcmsptwi_adapter.dev,
  196. "Result: No response\n");
  197. return MSP_TWI_XFER_NO_RESPONSE;
  198. } else if (reg & MSP_TWI_INT_STS_DATA_COLLISION) {
  199. dev_dbg(&pmcmsptwi_adapter.dev,
  200. "Result: Data collision\n");
  201. return MSP_TWI_XFER_DATA_COLLISION;
  202. } else if (reg & MSP_TWI_INT_STS_BUSY) {
  203. dev_dbg(&pmcmsptwi_adapter.dev,
  204. "Result: Bus busy\n");
  205. return MSP_TWI_XFER_BUSY;
  206. }
  207. dev_dbg(&pmcmsptwi_adapter.dev, "Result: Operation succeeded\n");
  208. return MSP_TWI_XFER_OK;
  209. }
  210. /*
  211. * In interrupt mode, handle the interrupt.
  212. * NOTE: Assumes data->lock is held.
  213. */
  214. static irqreturn_t pmcmsptwi_interrupt(int irq, void *ptr)
  215. {
  216. struct pmcmsptwi_data *data = ptr;
  217. u32 reason = pmcmsptwi_readl(data->iobase +
  218. MSP_TWI_INT_STS_REG_OFFSET);
  219. pmcmsptwi_writel(reason, data->iobase + MSP_TWI_INT_STS_REG_OFFSET);
  220. dev_dbg(&pmcmsptwi_adapter.dev, "Got interrupt 0x%08x\n", reason);
  221. if (!(reason & MSP_TWI_INT_STS_DONE))
  222. return IRQ_NONE;
  223. data->last_result = pmcmsptwi_get_result(reason);
  224. complete(&data->wait);
  225. return IRQ_HANDLED;
  226. }
  227. /*
  228. * Probe for and register the device and return 0 if there is one.
  229. */
  230. static int pmcmsptwi_probe(struct platform_device *pldev)
  231. {
  232. struct resource *res;
  233. int rc = -ENODEV;
  234. /* get the static platform resources */
  235. res = platform_get_resource(pldev, IORESOURCE_MEM, 0);
  236. if (!res) {
  237. dev_err(&pldev->dev, "IOMEM resource not found\n");
  238. goto ret_err;
  239. }
  240. /* reserve the memory region */
  241. if (!request_mem_region(res->start, resource_size(res),
  242. pldev->name)) {
  243. dev_err(&pldev->dev,
  244. "Unable to get memory/io address region 0x%08x\n",
  245. res->start);
  246. rc = -EBUSY;
  247. goto ret_err;
  248. }
  249. /* remap the memory */
  250. pmcmsptwi_data.iobase = ioremap_nocache(res->start,
  251. resource_size(res));
  252. if (!pmcmsptwi_data.iobase) {
  253. dev_err(&pldev->dev,
  254. "Unable to ioremap address 0x%08x\n", res->start);
  255. rc = -EIO;
  256. goto ret_unreserve;
  257. }
  258. /* request the irq */
  259. pmcmsptwi_data.irq = platform_get_irq(pldev, 0);
  260. if (pmcmsptwi_data.irq) {
  261. rc = request_irq(pmcmsptwi_data.irq, &pmcmsptwi_interrupt,
  262. IRQF_SHARED, pldev->name, &pmcmsptwi_data);
  263. if (rc == 0) {
  264. /*
  265. * Enable 'DONE' interrupt only.
  266. *
  267. * If you enable all interrupts, you will get one on
  268. * error and another when the operation completes.
  269. * This way you only have to handle one interrupt,
  270. * but you can still check all result flags.
  271. */
  272. pmcmsptwi_writel(MSP_TWI_INT_STS_DONE,
  273. pmcmsptwi_data.iobase +
  274. MSP_TWI_INT_MSK_REG_OFFSET);
  275. } else {
  276. dev_warn(&pldev->dev,
  277. "Could not assign TWI IRQ handler "
  278. "to irq %d (continuing with poll)\n",
  279. pmcmsptwi_data.irq);
  280. pmcmsptwi_data.irq = 0;
  281. }
  282. }
  283. init_completion(&pmcmsptwi_data.wait);
  284. mutex_init(&pmcmsptwi_data.lock);
  285. pmcmsptwi_set_clock_config(&pmcmsptwi_defclockcfg, &pmcmsptwi_data);
  286. pmcmsptwi_set_twi_config(&pmcmsptwi_defcfg, &pmcmsptwi_data);
  287. printk(KERN_INFO DRV_NAME ": Registering MSP71xx I2C adapter\n");
  288. pmcmsptwi_adapter.dev.parent = &pldev->dev;
  289. platform_set_drvdata(pldev, &pmcmsptwi_adapter);
  290. i2c_set_adapdata(&pmcmsptwi_adapter, &pmcmsptwi_data);
  291. rc = i2c_add_adapter(&pmcmsptwi_adapter);
  292. if (rc) {
  293. dev_err(&pldev->dev, "Unable to register I2C adapter\n");
  294. goto ret_unmap;
  295. }
  296. return 0;
  297. ret_unmap:
  298. if (pmcmsptwi_data.irq) {
  299. pmcmsptwi_writel(0,
  300. pmcmsptwi_data.iobase + MSP_TWI_INT_MSK_REG_OFFSET);
  301. free_irq(pmcmsptwi_data.irq, &pmcmsptwi_data);
  302. }
  303. iounmap(pmcmsptwi_data.iobase);
  304. ret_unreserve:
  305. release_mem_region(res->start, resource_size(res));
  306. ret_err:
  307. return rc;
  308. }
  309. /*
  310. * Release the device and return 0 if there is one.
  311. */
  312. static int pmcmsptwi_remove(struct platform_device *pldev)
  313. {
  314. struct resource *res;
  315. i2c_del_adapter(&pmcmsptwi_adapter);
  316. if (pmcmsptwi_data.irq) {
  317. pmcmsptwi_writel(0,
  318. pmcmsptwi_data.iobase + MSP_TWI_INT_MSK_REG_OFFSET);
  319. free_irq(pmcmsptwi_data.irq, &pmcmsptwi_data);
  320. }
  321. iounmap(pmcmsptwi_data.iobase);
  322. res = platform_get_resource(pldev, IORESOURCE_MEM, 0);
  323. release_mem_region(res->start, resource_size(res));
  324. return 0;
  325. }
  326. /*
  327. * Polls the 'busy' register until the command is complete.
  328. * NOTE: Assumes data->lock is held.
  329. */
  330. static void pmcmsptwi_poll_complete(struct pmcmsptwi_data *data)
  331. {
  332. int i;
  333. for (i = 0; i < MSP_MAX_POLL; i++) {
  334. u32 val = pmcmsptwi_readl(data->iobase +
  335. MSP_TWI_BUSY_REG_OFFSET);
  336. if (val == 0) {
  337. u32 reason = pmcmsptwi_readl(data->iobase +
  338. MSP_TWI_INT_STS_REG_OFFSET);
  339. pmcmsptwi_writel(reason, data->iobase +
  340. MSP_TWI_INT_STS_REG_OFFSET);
  341. data->last_result = pmcmsptwi_get_result(reason);
  342. return;
  343. }
  344. udelay(MSP_POLL_DELAY);
  345. }
  346. dev_dbg(&pmcmsptwi_adapter.dev, "Result: Poll timeout\n");
  347. data->last_result = MSP_TWI_XFER_TIMEOUT;
  348. }
  349. /*
  350. * Do the transfer (low level):
  351. * May use interrupt-driven or polling, depending on if an IRQ is
  352. * presently registered.
  353. * NOTE: Assumes data->lock is held.
  354. */
  355. static enum pmcmsptwi_xfer_result pmcmsptwi_do_xfer(
  356. u32 reg, struct pmcmsptwi_data *data)
  357. {
  358. dev_dbg(&pmcmsptwi_adapter.dev, "Writing cmd reg 0x%08x\n", reg);
  359. pmcmsptwi_writel(reg, data->iobase + MSP_TWI_CMD_REG_OFFSET);
  360. if (data->irq) {
  361. unsigned long timeleft = wait_for_completion_timeout(
  362. &data->wait, MSP_IRQ_TIMEOUT);
  363. if (timeleft == 0) {
  364. dev_dbg(&pmcmsptwi_adapter.dev,
  365. "Result: IRQ timeout\n");
  366. complete(&data->wait);
  367. data->last_result = MSP_TWI_XFER_TIMEOUT;
  368. }
  369. } else
  370. pmcmsptwi_poll_complete(data);
  371. return data->last_result;
  372. }
  373. /*
  374. * Helper routine, converts 'pmctwi_cmd' struct to register format
  375. */
  376. static inline u32 pmcmsptwi_cmd_to_reg(const struct pmcmsptwi_cmd *cmd)
  377. {
  378. return ((cmd->type & 0x3) << 8) |
  379. (((cmd->write_len - 1) & 0x7) << 4) |
  380. ((cmd->read_len - 1) & 0x7);
  381. }
  382. /*
  383. * Do the transfer (high level)
  384. */
  385. static enum pmcmsptwi_xfer_result pmcmsptwi_xfer_cmd(
  386. struct pmcmsptwi_cmd *cmd,
  387. struct pmcmsptwi_data *data)
  388. {
  389. enum pmcmsptwi_xfer_result retval;
  390. if ((cmd->type == MSP_TWI_CMD_WRITE && cmd->write_len == 0) ||
  391. (cmd->type == MSP_TWI_CMD_READ && cmd->read_len == 0) ||
  392. (cmd->type == MSP_TWI_CMD_WRITE_READ &&
  393. (cmd->read_len == 0 || cmd->write_len == 0))) {
  394. dev_err(&pmcmsptwi_adapter.dev,
  395. "%s: Cannot transfer less than 1 byte\n",
  396. __func__);
  397. return -EINVAL;
  398. }
  399. mutex_lock(&data->lock);
  400. dev_dbg(&pmcmsptwi_adapter.dev,
  401. "Setting address to 0x%04x\n", cmd->addr);
  402. pmcmsptwi_writel(cmd->addr, data->iobase + MSP_TWI_ADD_REG_OFFSET);
  403. if (cmd->type == MSP_TWI_CMD_WRITE ||
  404. cmd->type == MSP_TWI_CMD_WRITE_READ) {
  405. u64 tmp = be64_to_cpup((__be64 *)cmd->write_data);
  406. tmp >>= (MSP_MAX_BYTES_PER_RW - cmd->write_len) * 8;
  407. dev_dbg(&pmcmsptwi_adapter.dev, "Writing 0x%016llx\n", tmp);
  408. pmcmsptwi_writel(tmp & 0x00000000ffffffffLL,
  409. data->iobase + MSP_TWI_DAT_0_REG_OFFSET);
  410. if (cmd->write_len > 4)
  411. pmcmsptwi_writel(tmp >> 32,
  412. data->iobase + MSP_TWI_DAT_1_REG_OFFSET);
  413. }
  414. retval = pmcmsptwi_do_xfer(pmcmsptwi_cmd_to_reg(cmd), data);
  415. if (retval != MSP_TWI_XFER_OK)
  416. goto xfer_err;
  417. if (cmd->type == MSP_TWI_CMD_READ ||
  418. cmd->type == MSP_TWI_CMD_WRITE_READ) {
  419. int i;
  420. u64 rmsk = ~(0xffffffffffffffffLL << (cmd->read_len * 8));
  421. u64 tmp = (u64)pmcmsptwi_readl(data->iobase +
  422. MSP_TWI_DAT_0_REG_OFFSET);
  423. if (cmd->read_len > 4)
  424. tmp |= (u64)pmcmsptwi_readl(data->iobase +
  425. MSP_TWI_DAT_1_REG_OFFSET) << 32;
  426. tmp &= rmsk;
  427. dev_dbg(&pmcmsptwi_adapter.dev, "Read 0x%016llx\n", tmp);
  428. for (i = 0; i < cmd->read_len; i++)
  429. cmd->read_data[i] = tmp >> i;
  430. }
  431. xfer_err:
  432. mutex_unlock(&data->lock);
  433. return retval;
  434. }
  435. /* -- Algorithm functions -- */
  436. /*
  437. * Sends an i2c command out on the adapter
  438. */
  439. static int pmcmsptwi_master_xfer(struct i2c_adapter *adap,
  440. struct i2c_msg *msg, int num)
  441. {
  442. struct pmcmsptwi_data *data = i2c_get_adapdata(adap);
  443. struct pmcmsptwi_cmd cmd;
  444. struct pmcmsptwi_cfg oldcfg, newcfg;
  445. int ret;
  446. if (num == 2) {
  447. struct i2c_msg *nextmsg = msg + 1;
  448. cmd.type = MSP_TWI_CMD_WRITE_READ;
  449. cmd.write_len = msg->len;
  450. cmd.write_data = msg->buf;
  451. cmd.read_len = nextmsg->len;
  452. cmd.read_data = nextmsg->buf;
  453. } else if (msg->flags & I2C_M_RD) {
  454. cmd.type = MSP_TWI_CMD_READ;
  455. cmd.read_len = msg->len;
  456. cmd.read_data = msg->buf;
  457. cmd.write_len = 0;
  458. cmd.write_data = NULL;
  459. } else {
  460. cmd.type = MSP_TWI_CMD_WRITE;
  461. cmd.read_len = 0;
  462. cmd.read_data = NULL;
  463. cmd.write_len = msg->len;
  464. cmd.write_data = msg->buf;
  465. }
  466. if (msg->len == 0) {
  467. dev_err(&adap->dev, "Zero-byte messages unsupported\n");
  468. return -EINVAL;
  469. }
  470. cmd.addr = msg->addr;
  471. if (msg->flags & I2C_M_TEN) {
  472. pmcmsptwi_get_twi_config(&newcfg, data);
  473. memcpy(&oldcfg, &newcfg, sizeof(oldcfg));
  474. /* Set the special 10-bit address flag */
  475. newcfg.add10 = 1;
  476. pmcmsptwi_set_twi_config(&newcfg, data);
  477. }
  478. /* Execute the command */
  479. ret = pmcmsptwi_xfer_cmd(&cmd, data);
  480. if (msg->flags & I2C_M_TEN)
  481. pmcmsptwi_set_twi_config(&oldcfg, data);
  482. dev_dbg(&adap->dev, "I2C %s of %d bytes %s\n",
  483. (msg->flags & I2C_M_RD) ? "read" : "write", msg->len,
  484. (ret == MSP_TWI_XFER_OK) ? "succeeded" : "failed");
  485. if (ret != MSP_TWI_XFER_OK) {
  486. /*
  487. * TODO: We could potentially loop and retry in the case
  488. * of MSP_TWI_XFER_TIMEOUT.
  489. */
  490. return -1;
  491. }
  492. return 0;
  493. }
  494. static u32 pmcmsptwi_i2c_func(struct i2c_adapter *adapter)
  495. {
  496. return I2C_FUNC_I2C | I2C_FUNC_10BIT_ADDR |
  497. I2C_FUNC_SMBUS_BYTE | I2C_FUNC_SMBUS_BYTE_DATA |
  498. I2C_FUNC_SMBUS_WORD_DATA | I2C_FUNC_SMBUS_PROC_CALL;
  499. }
  500. static struct i2c_adapter_quirks pmcmsptwi_i2c_quirks = {
  501. .flags = I2C_AQ_COMB_WRITE_THEN_READ,
  502. .max_write_len = MSP_MAX_BYTES_PER_RW,
  503. .max_read_len = MSP_MAX_BYTES_PER_RW,
  504. .max_comb_1st_msg_len = MSP_MAX_BYTES_PER_RW,
  505. .max_comb_2nd_msg_len = MSP_MAX_BYTES_PER_RW,
  506. };
  507. /* -- Initialization -- */
  508. static struct i2c_algorithm pmcmsptwi_algo = {
  509. .master_xfer = pmcmsptwi_master_xfer,
  510. .functionality = pmcmsptwi_i2c_func,
  511. };
  512. static struct i2c_adapter pmcmsptwi_adapter = {
  513. .owner = THIS_MODULE,
  514. .class = I2C_CLASS_HWMON | I2C_CLASS_SPD,
  515. .algo = &pmcmsptwi_algo,
  516. .quirks = &pmcmsptwi_i2c_quirks,
  517. .name = DRV_NAME,
  518. };
  519. static struct platform_driver pmcmsptwi_driver = {
  520. .probe = pmcmsptwi_probe,
  521. .remove = pmcmsptwi_remove,
  522. .driver = {
  523. .name = DRV_NAME,
  524. },
  525. };
  526. module_platform_driver(pmcmsptwi_driver);
  527. MODULE_DESCRIPTION("PMC MSP TWI/SMBus/I2C driver");
  528. MODULE_LICENSE("GPL");
  529. MODULE_ALIAS("platform:" DRV_NAME);