oaktrail_hdmi_i2c.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*
  2. * Copyright © 2010 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Li Peng <peng.li@intel.com>
  25. */
  26. #include <linux/export.h>
  27. #include <linux/mutex.h>
  28. #include <linux/pci.h>
  29. #include <linux/i2c.h>
  30. #include <linux/interrupt.h>
  31. #include <linux/delay.h>
  32. #include "psb_drv.h"
  33. #define HDMI_READ(reg) readl(hdmi_dev->regs + (reg))
  34. #define HDMI_WRITE(reg, val) writel(val, hdmi_dev->regs + (reg))
  35. #define HDMI_HCR 0x1000
  36. #define HCR_DETECT_HDP (1 << 6)
  37. #define HCR_ENABLE_HDCP (1 << 5)
  38. #define HCR_ENABLE_AUDIO (1 << 2)
  39. #define HCR_ENABLE_PIXEL (1 << 1)
  40. #define HCR_ENABLE_TMDS (1 << 0)
  41. #define HDMI_HICR 0x1004
  42. #define HDMI_INTR_I2C_ERROR (1 << 4)
  43. #define HDMI_INTR_I2C_FULL (1 << 3)
  44. #define HDMI_INTR_I2C_DONE (1 << 2)
  45. #define HDMI_INTR_HPD (1 << 0)
  46. #define HDMI_HSR 0x1008
  47. #define HDMI_HISR 0x100C
  48. #define HDMI_HI2CRDB0 0x1200
  49. #define HDMI_HI2CHCR 0x1240
  50. #define HI2C_HDCP_WRITE (0 << 2)
  51. #define HI2C_HDCP_RI_READ (1 << 2)
  52. #define HI2C_HDCP_READ (2 << 2)
  53. #define HI2C_EDID_READ (3 << 2)
  54. #define HI2C_READ_CONTINUE (1 << 1)
  55. #define HI2C_ENABLE_TRANSACTION (1 << 0)
  56. #define HDMI_ICRH 0x1100
  57. #define HDMI_HI2CTDR0 0x1244
  58. #define HDMI_HI2CTDR1 0x1248
  59. #define I2C_STAT_INIT 0
  60. #define I2C_READ_DONE 1
  61. #define I2C_TRANSACTION_DONE 2
  62. struct hdmi_i2c_dev {
  63. struct i2c_adapter *adap;
  64. struct mutex i2c_lock;
  65. struct completion complete;
  66. int status;
  67. struct i2c_msg *msg;
  68. int buf_offset;
  69. };
  70. static void hdmi_i2c_irq_enable(struct oaktrail_hdmi_dev *hdmi_dev)
  71. {
  72. u32 temp;
  73. temp = HDMI_READ(HDMI_HICR);
  74. temp |= (HDMI_INTR_I2C_ERROR | HDMI_INTR_I2C_FULL | HDMI_INTR_I2C_DONE);
  75. HDMI_WRITE(HDMI_HICR, temp);
  76. HDMI_READ(HDMI_HICR);
  77. }
  78. static void hdmi_i2c_irq_disable(struct oaktrail_hdmi_dev *hdmi_dev)
  79. {
  80. HDMI_WRITE(HDMI_HICR, 0x0);
  81. HDMI_READ(HDMI_HICR);
  82. }
  83. static int xfer_read(struct i2c_adapter *adap, struct i2c_msg *pmsg)
  84. {
  85. struct oaktrail_hdmi_dev *hdmi_dev = i2c_get_adapdata(adap);
  86. struct hdmi_i2c_dev *i2c_dev = hdmi_dev->i2c_dev;
  87. u32 temp;
  88. i2c_dev->status = I2C_STAT_INIT;
  89. i2c_dev->msg = pmsg;
  90. i2c_dev->buf_offset = 0;
  91. reinit_completion(&i2c_dev->complete);
  92. /* Enable I2C transaction */
  93. temp = ((pmsg->len) << 20) | HI2C_EDID_READ | HI2C_ENABLE_TRANSACTION;
  94. HDMI_WRITE(HDMI_HI2CHCR, temp);
  95. HDMI_READ(HDMI_HI2CHCR);
  96. while (i2c_dev->status != I2C_TRANSACTION_DONE)
  97. wait_for_completion_interruptible_timeout(&i2c_dev->complete,
  98. 10 * HZ);
  99. return 0;
  100. }
  101. static int xfer_write(struct i2c_adapter *adap, struct i2c_msg *pmsg)
  102. {
  103. /*
  104. * XXX: i2c write seems isn't useful for EDID probe, don't do anything
  105. */
  106. return 0;
  107. }
  108. static int oaktrail_hdmi_i2c_access(struct i2c_adapter *adap,
  109. struct i2c_msg *pmsg,
  110. int num)
  111. {
  112. struct oaktrail_hdmi_dev *hdmi_dev = i2c_get_adapdata(adap);
  113. struct hdmi_i2c_dev *i2c_dev = hdmi_dev->i2c_dev;
  114. int i;
  115. mutex_lock(&i2c_dev->i2c_lock);
  116. /* Enable i2c unit */
  117. HDMI_WRITE(HDMI_ICRH, 0x00008760);
  118. /* Enable irq */
  119. hdmi_i2c_irq_enable(hdmi_dev);
  120. for (i = 0; i < num; i++) {
  121. if (pmsg->len && pmsg->buf) {
  122. if (pmsg->flags & I2C_M_RD)
  123. xfer_read(adap, pmsg);
  124. else
  125. xfer_write(adap, pmsg);
  126. }
  127. pmsg++; /* next message */
  128. }
  129. /* Disable irq */
  130. hdmi_i2c_irq_disable(hdmi_dev);
  131. mutex_unlock(&i2c_dev->i2c_lock);
  132. return i;
  133. }
  134. static u32 oaktrail_hdmi_i2c_func(struct i2c_adapter *adapter)
  135. {
  136. return I2C_FUNC_I2C | I2C_FUNC_10BIT_ADDR;
  137. }
  138. static const struct i2c_algorithm oaktrail_hdmi_i2c_algorithm = {
  139. .master_xfer = oaktrail_hdmi_i2c_access,
  140. .functionality = oaktrail_hdmi_i2c_func,
  141. };
  142. static struct i2c_adapter oaktrail_hdmi_i2c_adapter = {
  143. .name = "oaktrail_hdmi_i2c",
  144. .nr = 3,
  145. .owner = THIS_MODULE,
  146. .class = I2C_CLASS_DDC,
  147. .algo = &oaktrail_hdmi_i2c_algorithm,
  148. };
  149. static void hdmi_i2c_read(struct oaktrail_hdmi_dev *hdmi_dev)
  150. {
  151. struct hdmi_i2c_dev *i2c_dev = hdmi_dev->i2c_dev;
  152. struct i2c_msg *msg = i2c_dev->msg;
  153. u8 *buf = msg->buf;
  154. u32 temp;
  155. int i, offset;
  156. offset = i2c_dev->buf_offset;
  157. for (i = 0; i < 0x10; i++) {
  158. temp = HDMI_READ(HDMI_HI2CRDB0 + (i * 4));
  159. memcpy(buf + (offset + i * 4), &temp, 4);
  160. }
  161. i2c_dev->buf_offset += (0x10 * 4);
  162. /* clearing read buffer full intr */
  163. temp = HDMI_READ(HDMI_HISR);
  164. HDMI_WRITE(HDMI_HISR, temp | HDMI_INTR_I2C_FULL);
  165. HDMI_READ(HDMI_HISR);
  166. /* continue read transaction */
  167. temp = HDMI_READ(HDMI_HI2CHCR);
  168. HDMI_WRITE(HDMI_HI2CHCR, temp | HI2C_READ_CONTINUE);
  169. HDMI_READ(HDMI_HI2CHCR);
  170. i2c_dev->status = I2C_READ_DONE;
  171. return;
  172. }
  173. static void hdmi_i2c_transaction_done(struct oaktrail_hdmi_dev *hdmi_dev)
  174. {
  175. struct hdmi_i2c_dev *i2c_dev = hdmi_dev->i2c_dev;
  176. u32 temp;
  177. /* clear transaction done intr */
  178. temp = HDMI_READ(HDMI_HISR);
  179. HDMI_WRITE(HDMI_HISR, temp | HDMI_INTR_I2C_DONE);
  180. HDMI_READ(HDMI_HISR);
  181. temp = HDMI_READ(HDMI_HI2CHCR);
  182. HDMI_WRITE(HDMI_HI2CHCR, temp & ~HI2C_ENABLE_TRANSACTION);
  183. HDMI_READ(HDMI_HI2CHCR);
  184. i2c_dev->status = I2C_TRANSACTION_DONE;
  185. return;
  186. }
  187. static irqreturn_t oaktrail_hdmi_i2c_handler(int this_irq, void *dev)
  188. {
  189. struct oaktrail_hdmi_dev *hdmi_dev = dev;
  190. struct hdmi_i2c_dev *i2c_dev = hdmi_dev->i2c_dev;
  191. u32 stat;
  192. stat = HDMI_READ(HDMI_HISR);
  193. if (stat & HDMI_INTR_HPD) {
  194. HDMI_WRITE(HDMI_HISR, stat | HDMI_INTR_HPD);
  195. HDMI_READ(HDMI_HISR);
  196. }
  197. if (stat & HDMI_INTR_I2C_FULL)
  198. hdmi_i2c_read(hdmi_dev);
  199. if (stat & HDMI_INTR_I2C_DONE)
  200. hdmi_i2c_transaction_done(hdmi_dev);
  201. complete(&i2c_dev->complete);
  202. return IRQ_HANDLED;
  203. }
  204. /*
  205. * choose alternate function 2 of GPIO pin 52, 53,
  206. * which is used by HDMI I2C logic
  207. */
  208. static void oaktrail_hdmi_i2c_gpio_fix(void)
  209. {
  210. void __iomem *base;
  211. unsigned int gpio_base = 0xff12c000;
  212. int gpio_len = 0x1000;
  213. u32 temp;
  214. base = ioremap((resource_size_t)gpio_base, gpio_len);
  215. if (base == NULL) {
  216. DRM_ERROR("gpio ioremap fail\n");
  217. return;
  218. }
  219. temp = readl(base + 0x44);
  220. DRM_DEBUG_DRIVER("old gpio val %x\n", temp);
  221. writel((temp | 0x00000a00), (base + 0x44));
  222. temp = readl(base + 0x44);
  223. DRM_DEBUG_DRIVER("new gpio val %x\n", temp);
  224. iounmap(base);
  225. }
  226. int oaktrail_hdmi_i2c_init(struct pci_dev *dev)
  227. {
  228. struct oaktrail_hdmi_dev *hdmi_dev;
  229. struct hdmi_i2c_dev *i2c_dev;
  230. int ret;
  231. hdmi_dev = pci_get_drvdata(dev);
  232. i2c_dev = kzalloc(sizeof(struct hdmi_i2c_dev), GFP_KERNEL);
  233. if (i2c_dev == NULL) {
  234. DRM_ERROR("Can't allocate interface\n");
  235. ret = -ENOMEM;
  236. goto exit;
  237. }
  238. i2c_dev->adap = &oaktrail_hdmi_i2c_adapter;
  239. i2c_dev->status = I2C_STAT_INIT;
  240. init_completion(&i2c_dev->complete);
  241. mutex_init(&i2c_dev->i2c_lock);
  242. i2c_set_adapdata(&oaktrail_hdmi_i2c_adapter, hdmi_dev);
  243. hdmi_dev->i2c_dev = i2c_dev;
  244. /* Enable HDMI I2C function on gpio */
  245. oaktrail_hdmi_i2c_gpio_fix();
  246. /* request irq */
  247. ret = request_irq(dev->irq, oaktrail_hdmi_i2c_handler, IRQF_SHARED,
  248. oaktrail_hdmi_i2c_adapter.name, hdmi_dev);
  249. if (ret) {
  250. DRM_ERROR("Failed to request IRQ for I2C controller\n");
  251. goto err;
  252. }
  253. /* Adapter registration */
  254. ret = i2c_add_numbered_adapter(&oaktrail_hdmi_i2c_adapter);
  255. return ret;
  256. err:
  257. kfree(i2c_dev);
  258. exit:
  259. return ret;
  260. }
  261. void oaktrail_hdmi_i2c_exit(struct pci_dev *dev)
  262. {
  263. struct oaktrail_hdmi_dev *hdmi_dev;
  264. struct hdmi_i2c_dev *i2c_dev;
  265. hdmi_dev = pci_get_drvdata(dev);
  266. i2c_del_adapter(&oaktrail_hdmi_i2c_adapter);
  267. i2c_dev = hdmi_dev->i2c_dev;
  268. kfree(i2c_dev);
  269. free_irq(dev->irq, hdmi_dev);
  270. }