amdgpu_i2c.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /*
  2. * Copyright 2007-8 Advanced Micro Devices, Inc.
  3. * Copyright 2008 Red Hat Inc.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the 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 COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  19. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  20. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. * OTHER DEALINGS IN THE SOFTWARE.
  22. *
  23. * Authors: Dave Airlie
  24. * Alex Deucher
  25. */
  26. #include <linux/export.h>
  27. #include <drm/drmP.h>
  28. #include <drm/drm_edid.h>
  29. #include <drm/amdgpu_drm.h>
  30. #include "amdgpu.h"
  31. #include "amdgpu_i2c.h"
  32. #include "amdgpu_atombios.h"
  33. #include "atom.h"
  34. #include "atombios_dp.h"
  35. #include "atombios_i2c.h"
  36. /* bit banging i2c */
  37. static int amdgpu_i2c_pre_xfer(struct i2c_adapter *i2c_adap)
  38. {
  39. struct amdgpu_i2c_chan *i2c = i2c_get_adapdata(i2c_adap);
  40. struct amdgpu_device *adev = i2c->dev->dev_private;
  41. struct amdgpu_i2c_bus_rec *rec = &i2c->rec;
  42. uint32_t temp;
  43. mutex_lock(&i2c->mutex);
  44. /* switch the pads to ddc mode */
  45. if (rec->hw_capable) {
  46. temp = RREG32(rec->mask_clk_reg);
  47. temp &= ~(1 << 16);
  48. WREG32(rec->mask_clk_reg, temp);
  49. }
  50. /* clear the output pin values */
  51. temp = RREG32(rec->a_clk_reg) & ~rec->a_clk_mask;
  52. WREG32(rec->a_clk_reg, temp);
  53. temp = RREG32(rec->a_data_reg) & ~rec->a_data_mask;
  54. WREG32(rec->a_data_reg, temp);
  55. /* set the pins to input */
  56. temp = RREG32(rec->en_clk_reg) & ~rec->en_clk_mask;
  57. WREG32(rec->en_clk_reg, temp);
  58. temp = RREG32(rec->en_data_reg) & ~rec->en_data_mask;
  59. WREG32(rec->en_data_reg, temp);
  60. /* mask the gpio pins for software use */
  61. temp = RREG32(rec->mask_clk_reg) | rec->mask_clk_mask;
  62. WREG32(rec->mask_clk_reg, temp);
  63. temp = RREG32(rec->mask_clk_reg);
  64. temp = RREG32(rec->mask_data_reg) | rec->mask_data_mask;
  65. WREG32(rec->mask_data_reg, temp);
  66. temp = RREG32(rec->mask_data_reg);
  67. return 0;
  68. }
  69. static void amdgpu_i2c_post_xfer(struct i2c_adapter *i2c_adap)
  70. {
  71. struct amdgpu_i2c_chan *i2c = i2c_get_adapdata(i2c_adap);
  72. struct amdgpu_device *adev = i2c->dev->dev_private;
  73. struct amdgpu_i2c_bus_rec *rec = &i2c->rec;
  74. uint32_t temp;
  75. /* unmask the gpio pins for software use */
  76. temp = RREG32(rec->mask_clk_reg) & ~rec->mask_clk_mask;
  77. WREG32(rec->mask_clk_reg, temp);
  78. temp = RREG32(rec->mask_clk_reg);
  79. temp = RREG32(rec->mask_data_reg) & ~rec->mask_data_mask;
  80. WREG32(rec->mask_data_reg, temp);
  81. temp = RREG32(rec->mask_data_reg);
  82. mutex_unlock(&i2c->mutex);
  83. }
  84. static int amdgpu_i2c_get_clock(void *i2c_priv)
  85. {
  86. struct amdgpu_i2c_chan *i2c = i2c_priv;
  87. struct amdgpu_device *adev = i2c->dev->dev_private;
  88. struct amdgpu_i2c_bus_rec *rec = &i2c->rec;
  89. uint32_t val;
  90. /* read the value off the pin */
  91. val = RREG32(rec->y_clk_reg);
  92. val &= rec->y_clk_mask;
  93. return (val != 0);
  94. }
  95. static int amdgpu_i2c_get_data(void *i2c_priv)
  96. {
  97. struct amdgpu_i2c_chan *i2c = i2c_priv;
  98. struct amdgpu_device *adev = i2c->dev->dev_private;
  99. struct amdgpu_i2c_bus_rec *rec = &i2c->rec;
  100. uint32_t val;
  101. /* read the value off the pin */
  102. val = RREG32(rec->y_data_reg);
  103. val &= rec->y_data_mask;
  104. return (val != 0);
  105. }
  106. static void amdgpu_i2c_set_clock(void *i2c_priv, int clock)
  107. {
  108. struct amdgpu_i2c_chan *i2c = i2c_priv;
  109. struct amdgpu_device *adev = i2c->dev->dev_private;
  110. struct amdgpu_i2c_bus_rec *rec = &i2c->rec;
  111. uint32_t val;
  112. /* set pin direction */
  113. val = RREG32(rec->en_clk_reg) & ~rec->en_clk_mask;
  114. val |= clock ? 0 : rec->en_clk_mask;
  115. WREG32(rec->en_clk_reg, val);
  116. }
  117. static void amdgpu_i2c_set_data(void *i2c_priv, int data)
  118. {
  119. struct amdgpu_i2c_chan *i2c = i2c_priv;
  120. struct amdgpu_device *adev = i2c->dev->dev_private;
  121. struct amdgpu_i2c_bus_rec *rec = &i2c->rec;
  122. uint32_t val;
  123. /* set pin direction */
  124. val = RREG32(rec->en_data_reg) & ~rec->en_data_mask;
  125. val |= data ? 0 : rec->en_data_mask;
  126. WREG32(rec->en_data_reg, val);
  127. }
  128. static const struct i2c_algorithm amdgpu_atombios_i2c_algo = {
  129. .master_xfer = amdgpu_atombios_i2c_xfer,
  130. .functionality = amdgpu_atombios_i2c_func,
  131. };
  132. struct amdgpu_i2c_chan *amdgpu_i2c_create(struct drm_device *dev,
  133. const struct amdgpu_i2c_bus_rec *rec,
  134. const char *name)
  135. {
  136. struct amdgpu_i2c_chan *i2c;
  137. int ret;
  138. /* don't add the mm_i2c bus unless hw_i2c is enabled */
  139. if (rec->mm_i2c && (amdgpu_hw_i2c == 0))
  140. return NULL;
  141. i2c = kzalloc(sizeof(struct amdgpu_i2c_chan), GFP_KERNEL);
  142. if (i2c == NULL)
  143. return NULL;
  144. i2c->rec = *rec;
  145. i2c->adapter.owner = THIS_MODULE;
  146. i2c->adapter.class = I2C_CLASS_DDC;
  147. i2c->adapter.dev.parent = &dev->pdev->dev;
  148. i2c->dev = dev;
  149. i2c_set_adapdata(&i2c->adapter, i2c);
  150. mutex_init(&i2c->mutex);
  151. if (rec->hw_capable &&
  152. amdgpu_hw_i2c) {
  153. /* hw i2c using atom */
  154. snprintf(i2c->adapter.name, sizeof(i2c->adapter.name),
  155. "AMDGPU i2c hw bus %s", name);
  156. i2c->adapter.algo = &amdgpu_atombios_i2c_algo;
  157. ret = i2c_add_adapter(&i2c->adapter);
  158. if (ret)
  159. goto out_free;
  160. } else {
  161. /* set the amdgpu bit adapter */
  162. snprintf(i2c->adapter.name, sizeof(i2c->adapter.name),
  163. "AMDGPU i2c bit bus %s", name);
  164. i2c->adapter.algo_data = &i2c->bit;
  165. i2c->bit.pre_xfer = amdgpu_i2c_pre_xfer;
  166. i2c->bit.post_xfer = amdgpu_i2c_post_xfer;
  167. i2c->bit.setsda = amdgpu_i2c_set_data;
  168. i2c->bit.setscl = amdgpu_i2c_set_clock;
  169. i2c->bit.getsda = amdgpu_i2c_get_data;
  170. i2c->bit.getscl = amdgpu_i2c_get_clock;
  171. i2c->bit.udelay = 10;
  172. i2c->bit.timeout = usecs_to_jiffies(2200); /* from VESA */
  173. i2c->bit.data = i2c;
  174. ret = i2c_bit_add_bus(&i2c->adapter);
  175. if (ret) {
  176. DRM_ERROR("Failed to register bit i2c %s\n", name);
  177. goto out_free;
  178. }
  179. }
  180. return i2c;
  181. out_free:
  182. kfree(i2c);
  183. return NULL;
  184. }
  185. void amdgpu_i2c_destroy(struct amdgpu_i2c_chan *i2c)
  186. {
  187. if (!i2c)
  188. return;
  189. WARN_ON(i2c->has_aux);
  190. i2c_del_adapter(&i2c->adapter);
  191. kfree(i2c);
  192. }
  193. /* Add the default buses */
  194. void amdgpu_i2c_init(struct amdgpu_device *adev)
  195. {
  196. if (amdgpu_hw_i2c)
  197. DRM_INFO("hw_i2c forced on, you may experience display detection problems!\n");
  198. amdgpu_atombios_i2c_init(adev);
  199. }
  200. /* remove all the buses */
  201. void amdgpu_i2c_fini(struct amdgpu_device *adev)
  202. {
  203. int i;
  204. for (i = 0; i < AMDGPU_MAX_I2C_BUS; i++) {
  205. if (adev->i2c_bus[i]) {
  206. amdgpu_i2c_destroy(adev->i2c_bus[i]);
  207. adev->i2c_bus[i] = NULL;
  208. }
  209. }
  210. }
  211. /* Add additional buses */
  212. void amdgpu_i2c_add(struct amdgpu_device *adev,
  213. const struct amdgpu_i2c_bus_rec *rec,
  214. const char *name)
  215. {
  216. struct drm_device *dev = adev->ddev;
  217. int i;
  218. for (i = 0; i < AMDGPU_MAX_I2C_BUS; i++) {
  219. if (!adev->i2c_bus[i]) {
  220. adev->i2c_bus[i] = amdgpu_i2c_create(dev, rec, name);
  221. return;
  222. }
  223. }
  224. }
  225. /* looks up bus based on id */
  226. struct amdgpu_i2c_chan *
  227. amdgpu_i2c_lookup(struct amdgpu_device *adev,
  228. const struct amdgpu_i2c_bus_rec *i2c_bus)
  229. {
  230. int i;
  231. for (i = 0; i < AMDGPU_MAX_I2C_BUS; i++) {
  232. if (adev->i2c_bus[i] &&
  233. (adev->i2c_bus[i]->rec.i2c_id == i2c_bus->i2c_id)) {
  234. return adev->i2c_bus[i];
  235. }
  236. }
  237. return NULL;
  238. }
  239. static void amdgpu_i2c_get_byte(struct amdgpu_i2c_chan *i2c_bus,
  240. u8 slave_addr,
  241. u8 addr,
  242. u8 *val)
  243. {
  244. u8 out_buf[2];
  245. u8 in_buf[2];
  246. struct i2c_msg msgs[] = {
  247. {
  248. .addr = slave_addr,
  249. .flags = 0,
  250. .len = 1,
  251. .buf = out_buf,
  252. },
  253. {
  254. .addr = slave_addr,
  255. .flags = I2C_M_RD,
  256. .len = 1,
  257. .buf = in_buf,
  258. }
  259. };
  260. out_buf[0] = addr;
  261. out_buf[1] = 0;
  262. if (i2c_transfer(&i2c_bus->adapter, msgs, 2) == 2) {
  263. *val = in_buf[0];
  264. DRM_DEBUG("val = 0x%02x\n", *val);
  265. } else {
  266. DRM_DEBUG("i2c 0x%02x 0x%02x read failed\n",
  267. addr, *val);
  268. }
  269. }
  270. static void amdgpu_i2c_put_byte(struct amdgpu_i2c_chan *i2c_bus,
  271. u8 slave_addr,
  272. u8 addr,
  273. u8 val)
  274. {
  275. uint8_t out_buf[2];
  276. struct i2c_msg msg = {
  277. .addr = slave_addr,
  278. .flags = 0,
  279. .len = 2,
  280. .buf = out_buf,
  281. };
  282. out_buf[0] = addr;
  283. out_buf[1] = val;
  284. if (i2c_transfer(&i2c_bus->adapter, &msg, 1) != 1)
  285. DRM_DEBUG("i2c 0x%02x 0x%02x write failed\n",
  286. addr, val);
  287. }
  288. /* ddc router switching */
  289. void
  290. amdgpu_i2c_router_select_ddc_port(const struct amdgpu_connector *amdgpu_connector)
  291. {
  292. u8 val;
  293. if (!amdgpu_connector->router.ddc_valid)
  294. return;
  295. if (!amdgpu_connector->router_bus)
  296. return;
  297. amdgpu_i2c_get_byte(amdgpu_connector->router_bus,
  298. amdgpu_connector->router.i2c_addr,
  299. 0x3, &val);
  300. val &= ~amdgpu_connector->router.ddc_mux_control_pin;
  301. amdgpu_i2c_put_byte(amdgpu_connector->router_bus,
  302. amdgpu_connector->router.i2c_addr,
  303. 0x3, val);
  304. amdgpu_i2c_get_byte(amdgpu_connector->router_bus,
  305. amdgpu_connector->router.i2c_addr,
  306. 0x1, &val);
  307. val &= ~amdgpu_connector->router.ddc_mux_control_pin;
  308. val |= amdgpu_connector->router.ddc_mux_state;
  309. amdgpu_i2c_put_byte(amdgpu_connector->router_bus,
  310. amdgpu_connector->router.i2c_addr,
  311. 0x1, val);
  312. }
  313. /* clock/data router switching */
  314. void
  315. amdgpu_i2c_router_select_cd_port(const struct amdgpu_connector *amdgpu_connector)
  316. {
  317. u8 val;
  318. if (!amdgpu_connector->router.cd_valid)
  319. return;
  320. if (!amdgpu_connector->router_bus)
  321. return;
  322. amdgpu_i2c_get_byte(amdgpu_connector->router_bus,
  323. amdgpu_connector->router.i2c_addr,
  324. 0x3, &val);
  325. val &= ~amdgpu_connector->router.cd_mux_control_pin;
  326. amdgpu_i2c_put_byte(amdgpu_connector->router_bus,
  327. amdgpu_connector->router.i2c_addr,
  328. 0x3, val);
  329. amdgpu_i2c_get_byte(amdgpu_connector->router_bus,
  330. amdgpu_connector->router.i2c_addr,
  331. 0x1, &val);
  332. val &= ~amdgpu_connector->router.cd_mux_control_pin;
  333. val |= amdgpu_connector->router.cd_mux_state;
  334. amdgpu_i2c_put_byte(amdgpu_connector->router_bus,
  335. amdgpu_connector->router.i2c_addr,
  336. 0x1, val);
  337. }