i2c-pmcmsp.c 17 KB

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