ddbridge-i2c.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * ddbridge-i2c.c: Digital Devices bridge i2c driver
  3. *
  4. * Copyright (C) 2010-2017 Digital Devices GmbH
  5. * Ralph Metzler <rjkm@metzlerbros.de>
  6. * Marcus Metzler <mocm@metzlerbros.de>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * version 2 only, as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. */
  18. #include <linux/module.h>
  19. #include <linux/init.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/delay.h>
  22. #include <linux/slab.h>
  23. #include <linux/poll.h>
  24. #include <linux/io.h>
  25. #include <linux/pci.h>
  26. #include <linux/pci_ids.h>
  27. #include <linux/timer.h>
  28. #include <linux/i2c.h>
  29. #include <linux/swab.h>
  30. #include <linux/vmalloc.h>
  31. #include "ddbridge.h"
  32. #include "ddbridge-i2c.h"
  33. #include "ddbridge-regs.h"
  34. #include "ddbridge-io.h"
  35. /******************************************************************************/
  36. static int ddb_i2c_cmd(struct ddb_i2c *i2c, u32 adr, u32 cmd)
  37. {
  38. struct ddb *dev = i2c->dev;
  39. unsigned long stat;
  40. u32 val;
  41. ddbwritel(dev, (adr << 9) | cmd, i2c->regs + I2C_COMMAND);
  42. stat = wait_for_completion_timeout(&i2c->completion, HZ);
  43. val = ddbreadl(dev, i2c->regs + I2C_COMMAND);
  44. if (stat == 0) {
  45. dev_err(dev->dev, "I2C timeout, card %d, port %d, link %u\n",
  46. dev->nr, i2c->nr, i2c->link);
  47. {
  48. u32 istat = ddbreadl(dev, INTERRUPT_STATUS);
  49. dev_err(dev->dev, "DDBridge IRS %08x\n", istat);
  50. if (i2c->link) {
  51. u32 listat = ddbreadl(dev,
  52. DDB_LINK_TAG(i2c->link) |
  53. INTERRUPT_STATUS);
  54. dev_err(dev->dev, "DDBridge link %u IRS %08x\n",
  55. i2c->link, listat);
  56. }
  57. if (istat & 1) {
  58. ddbwritel(dev, istat & 1, INTERRUPT_ACK);
  59. } else {
  60. u32 mon = ddbreadl(dev,
  61. i2c->regs + I2C_MONITOR);
  62. dev_err(dev->dev, "I2C cmd=%08x mon=%08x\n",
  63. val, mon);
  64. }
  65. }
  66. return -EIO;
  67. }
  68. if (val & 0x70000)
  69. return -EIO;
  70. return 0;
  71. }
  72. static int ddb_i2c_master_xfer(struct i2c_adapter *adapter,
  73. struct i2c_msg msg[], int num)
  74. {
  75. struct ddb_i2c *i2c = (struct ddb_i2c *)i2c_get_adapdata(adapter);
  76. struct ddb *dev = i2c->dev;
  77. u8 addr = 0;
  78. addr = msg[0].addr;
  79. if (msg[0].len > i2c->bsize)
  80. return -EIO;
  81. switch (num) {
  82. case 1:
  83. if (msg[0].flags & I2C_M_RD) {
  84. ddbwritel(dev, msg[0].len << 16,
  85. i2c->regs + I2C_TASKLENGTH);
  86. if (ddb_i2c_cmd(i2c, addr, 3))
  87. break;
  88. ddbcpyfrom(dev, msg[0].buf,
  89. i2c->rbuf, msg[0].len);
  90. return num;
  91. }
  92. ddbcpyto(dev, i2c->wbuf, msg[0].buf, msg[0].len);
  93. ddbwritel(dev, msg[0].len, i2c->regs + I2C_TASKLENGTH);
  94. if (ddb_i2c_cmd(i2c, addr, 2))
  95. break;
  96. return num;
  97. case 2:
  98. if ((msg[0].flags & I2C_M_RD) == I2C_M_RD)
  99. break;
  100. if ((msg[1].flags & I2C_M_RD) != I2C_M_RD)
  101. break;
  102. if (msg[1].len > i2c->bsize)
  103. break;
  104. ddbcpyto(dev, i2c->wbuf, msg[0].buf, msg[0].len);
  105. ddbwritel(dev, msg[0].len | (msg[1].len << 16),
  106. i2c->regs + I2C_TASKLENGTH);
  107. if (ddb_i2c_cmd(i2c, addr, 1))
  108. break;
  109. ddbcpyfrom(dev, msg[1].buf,
  110. i2c->rbuf,
  111. msg[1].len);
  112. return num;
  113. default:
  114. break;
  115. }
  116. return -EIO;
  117. }
  118. static u32 ddb_i2c_functionality(struct i2c_adapter *adap)
  119. {
  120. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  121. }
  122. static const struct i2c_algorithm ddb_i2c_algo = {
  123. .master_xfer = ddb_i2c_master_xfer,
  124. .functionality = ddb_i2c_functionality,
  125. };
  126. void ddb_i2c_release(struct ddb *dev)
  127. {
  128. int i;
  129. struct ddb_i2c *i2c;
  130. for (i = 0; i < dev->i2c_num; i++) {
  131. i2c = &dev->i2c[i];
  132. i2c_del_adapter(&i2c->adap);
  133. }
  134. }
  135. static void i2c_handler(void *priv)
  136. {
  137. struct ddb_i2c *i2c = (struct ddb_i2c *)priv;
  138. complete(&i2c->completion);
  139. }
  140. static int ddb_i2c_add(struct ddb *dev, struct ddb_i2c *i2c,
  141. const struct ddb_regmap *regmap, int link,
  142. int i, int num)
  143. {
  144. struct i2c_adapter *adap;
  145. i2c->nr = i;
  146. i2c->dev = dev;
  147. i2c->link = link;
  148. i2c->bsize = regmap->i2c_buf->size;
  149. i2c->wbuf = DDB_LINK_TAG(link) |
  150. (regmap->i2c_buf->base + i2c->bsize * i);
  151. i2c->rbuf = i2c->wbuf; /* + i2c->bsize / 2 */
  152. i2c->regs = DDB_LINK_TAG(link) |
  153. (regmap->i2c->base + regmap->i2c->size * i);
  154. ddbwritel(dev, I2C_SPEED_100, i2c->regs + I2C_TIMING);
  155. ddbwritel(dev, ((i2c->rbuf & 0xffff) << 16) | (i2c->wbuf & 0xffff),
  156. i2c->regs + I2C_TASKADDRESS);
  157. init_completion(&i2c->completion);
  158. adap = &i2c->adap;
  159. i2c_set_adapdata(adap, i2c);
  160. #ifdef I2C_ADAP_CLASS_TV_DIGITAL
  161. adap->class = I2C_ADAP_CLASS_TV_DIGITAL | I2C_CLASS_TV_ANALOG;
  162. #else
  163. #ifdef I2C_CLASS_TV_ANALOG
  164. adap->class = I2C_CLASS_TV_ANALOG;
  165. #endif
  166. #endif
  167. snprintf(adap->name, I2C_NAME_SIZE, "ddbridge_%02x.%x.%x",
  168. dev->nr, i2c->link, i);
  169. adap->algo = &ddb_i2c_algo;
  170. adap->algo_data = (void *)i2c;
  171. adap->dev.parent = dev->dev;
  172. return i2c_add_adapter(adap);
  173. }
  174. int ddb_i2c_init(struct ddb *dev)
  175. {
  176. int stat = 0;
  177. u32 i, j, num = 0, l, base;
  178. struct ddb_i2c *i2c;
  179. struct i2c_adapter *adap;
  180. const struct ddb_regmap *regmap;
  181. for (l = 0; l < DDB_MAX_LINK; l++) {
  182. if (!dev->link[l].info)
  183. continue;
  184. regmap = dev->link[l].info->regmap;
  185. if (!regmap || !regmap->i2c)
  186. continue;
  187. base = regmap->irq_base_i2c;
  188. for (i = 0; i < regmap->i2c->num; i++) {
  189. if (!(dev->link[l].info->i2c_mask & (1 << i)))
  190. continue;
  191. i2c = &dev->i2c[num];
  192. ddb_irq_set(dev, l, i + base, i2c_handler, i2c);
  193. stat = ddb_i2c_add(dev, i2c, regmap, l, i, num);
  194. if (stat)
  195. break;
  196. num++;
  197. }
  198. }
  199. if (stat) {
  200. for (j = 0; j < num; j++) {
  201. i2c = &dev->i2c[j];
  202. adap = &i2c->adap;
  203. i2c_del_adapter(adap);
  204. }
  205. } else {
  206. dev->i2c_num = num;
  207. }
  208. return stat;
  209. }