ddbridge-i2c.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. val &= 0x70000;
  69. if (val == 0x20000)
  70. dev_err(dev->dev, "I2C bus error\n");
  71. if (val)
  72. return -EIO;
  73. return 0;
  74. }
  75. static int ddb_i2c_master_xfer(struct i2c_adapter *adapter,
  76. struct i2c_msg msg[], int num)
  77. {
  78. struct ddb_i2c *i2c = (struct ddb_i2c *)i2c_get_adapdata(adapter);
  79. struct ddb *dev = i2c->dev;
  80. u8 addr = 0;
  81. addr = msg[0].addr;
  82. if (msg[0].len > i2c->bsize)
  83. return -EIO;
  84. switch (num) {
  85. case 1:
  86. if (msg[0].flags & I2C_M_RD) {
  87. ddbwritel(dev, msg[0].len << 16,
  88. i2c->regs + I2C_TASKLENGTH);
  89. if (ddb_i2c_cmd(i2c, addr, 3))
  90. break;
  91. ddbcpyfrom(dev, msg[0].buf,
  92. i2c->rbuf, msg[0].len);
  93. return num;
  94. }
  95. ddbcpyto(dev, i2c->wbuf, msg[0].buf, msg[0].len);
  96. ddbwritel(dev, msg[0].len, i2c->regs + I2C_TASKLENGTH);
  97. if (ddb_i2c_cmd(i2c, addr, 2))
  98. break;
  99. return num;
  100. case 2:
  101. if ((msg[0].flags & I2C_M_RD) == I2C_M_RD)
  102. break;
  103. if ((msg[1].flags & I2C_M_RD) != I2C_M_RD)
  104. break;
  105. if (msg[1].len > i2c->bsize)
  106. break;
  107. ddbcpyto(dev, i2c->wbuf, msg[0].buf, msg[0].len);
  108. ddbwritel(dev, msg[0].len | (msg[1].len << 16),
  109. i2c->regs + I2C_TASKLENGTH);
  110. if (ddb_i2c_cmd(i2c, addr, 1))
  111. break;
  112. ddbcpyfrom(dev, msg[1].buf,
  113. i2c->rbuf,
  114. msg[1].len);
  115. return num;
  116. default:
  117. break;
  118. }
  119. return -EIO;
  120. }
  121. static u32 ddb_i2c_functionality(struct i2c_adapter *adap)
  122. {
  123. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  124. }
  125. static const struct i2c_algorithm ddb_i2c_algo = {
  126. .master_xfer = ddb_i2c_master_xfer,
  127. .functionality = ddb_i2c_functionality,
  128. };
  129. void ddb_i2c_release(struct ddb *dev)
  130. {
  131. int i;
  132. struct ddb_i2c *i2c;
  133. for (i = 0; i < dev->i2c_num; i++) {
  134. i2c = &dev->i2c[i];
  135. i2c_del_adapter(&i2c->adap);
  136. }
  137. }
  138. static void i2c_handler(void *priv)
  139. {
  140. struct ddb_i2c *i2c = (struct ddb_i2c *)priv;
  141. complete(&i2c->completion);
  142. }
  143. static int ddb_i2c_add(struct ddb *dev, struct ddb_i2c *i2c,
  144. const struct ddb_regmap *regmap, int link,
  145. int i, int num)
  146. {
  147. struct i2c_adapter *adap;
  148. i2c->nr = i;
  149. i2c->dev = dev;
  150. i2c->link = link;
  151. i2c->bsize = regmap->i2c_buf->size;
  152. i2c->wbuf = DDB_LINK_TAG(link) |
  153. (regmap->i2c_buf->base + i2c->bsize * i);
  154. i2c->rbuf = i2c->wbuf; /* + i2c->bsize / 2 */
  155. i2c->regs = DDB_LINK_TAG(link) |
  156. (regmap->i2c->base + regmap->i2c->size * i);
  157. ddbwritel(dev, I2C_SPEED_100, i2c->regs + I2C_TIMING);
  158. ddbwritel(dev, ((i2c->rbuf & 0xffff) << 16) | (i2c->wbuf & 0xffff),
  159. i2c->regs + I2C_TASKADDRESS);
  160. init_completion(&i2c->completion);
  161. adap = &i2c->adap;
  162. i2c_set_adapdata(adap, i2c);
  163. #ifdef I2C_ADAP_CLASS_TV_DIGITAL
  164. adap->class = I2C_ADAP_CLASS_TV_DIGITAL | I2C_CLASS_TV_ANALOG;
  165. #else
  166. #ifdef I2C_CLASS_TV_ANALOG
  167. adap->class = I2C_CLASS_TV_ANALOG;
  168. #endif
  169. #endif
  170. snprintf(adap->name, I2C_NAME_SIZE, "ddbridge_%02x.%x.%x",
  171. dev->nr, i2c->link, i);
  172. adap->algo = &ddb_i2c_algo;
  173. adap->algo_data = (void *)i2c;
  174. adap->dev.parent = dev->dev;
  175. return i2c_add_adapter(adap);
  176. }
  177. int ddb_i2c_init(struct ddb *dev)
  178. {
  179. int stat = 0;
  180. u32 i, j, num = 0, l, base;
  181. struct ddb_i2c *i2c;
  182. struct i2c_adapter *adap;
  183. const struct ddb_regmap *regmap;
  184. for (l = 0; l < DDB_MAX_LINK; l++) {
  185. if (!dev->link[l].info)
  186. continue;
  187. regmap = dev->link[l].info->regmap;
  188. if (!regmap || !regmap->i2c)
  189. continue;
  190. base = regmap->irq_base_i2c;
  191. for (i = 0; i < regmap->i2c->num; i++) {
  192. if (!(dev->link[l].info->i2c_mask & (1 << i)))
  193. continue;
  194. i2c = &dev->i2c[num];
  195. ddb_irq_set(dev, l, i + base, i2c_handler, i2c);
  196. stat = ddb_i2c_add(dev, i2c, regmap, l, i, num);
  197. if (stat)
  198. break;
  199. num++;
  200. }
  201. }
  202. if (stat) {
  203. for (j = 0; j < num; j++) {
  204. i2c = &dev->i2c[j];
  205. adap = &i2c->adap;
  206. i2c_del_adapter(adap);
  207. }
  208. } else {
  209. dev->i2c_num = num;
  210. }
  211. return stat;
  212. }