amdgpu_i2c.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. 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. DRM_ERROR("Failed to register hw i2c %s\n", name);
  160. goto out_free;
  161. }
  162. } else {
  163. /* set the amdgpu bit adapter */
  164. snprintf(i2c->adapter.name, sizeof(i2c->adapter.name),
  165. "AMDGPU i2c bit bus %s", name);
  166. i2c->adapter.algo_data = &i2c->bit;
  167. i2c->bit.pre_xfer = amdgpu_i2c_pre_xfer;
  168. i2c->bit.post_xfer = amdgpu_i2c_post_xfer;
  169. i2c->bit.setsda = amdgpu_i2c_set_data;
  170. i2c->bit.setscl = amdgpu_i2c_set_clock;
  171. i2c->bit.getsda = amdgpu_i2c_get_data;
  172. i2c->bit.getscl = amdgpu_i2c_get_clock;
  173. i2c->bit.udelay = 10;
  174. i2c->bit.timeout = usecs_to_jiffies(2200); /* from VESA */
  175. i2c->bit.data = i2c;
  176. ret = i2c_bit_add_bus(&i2c->adapter);
  177. if (ret) {
  178. DRM_ERROR("Failed to register bit i2c %s\n", name);
  179. goto out_free;
  180. }
  181. }
  182. return i2c;
  183. out_free:
  184. kfree(i2c);
  185. return NULL;
  186. }
  187. void amdgpu_i2c_destroy(struct amdgpu_i2c_chan *i2c)
  188. {
  189. if (!i2c)
  190. return;
  191. i2c_del_adapter(&i2c->adapter);
  192. kfree(i2c);
  193. }
  194. /* Add the default buses */
  195. void amdgpu_i2c_init(struct amdgpu_device *adev)
  196. {
  197. if (amdgpu_hw_i2c)
  198. DRM_INFO("hw_i2c forced on, you may experience display detection problems!\n");
  199. if (adev->is_atom_bios)
  200. amdgpu_atombios_i2c_init(adev);
  201. }
  202. /* remove all the buses */
  203. void amdgpu_i2c_fini(struct amdgpu_device *adev)
  204. {
  205. int i;
  206. for (i = 0; i < AMDGPU_MAX_I2C_BUS; i++) {
  207. if (adev->i2c_bus[i]) {
  208. amdgpu_i2c_destroy(adev->i2c_bus[i]);
  209. adev->i2c_bus[i] = NULL;
  210. }
  211. }
  212. }
  213. /* Add additional buses */
  214. void amdgpu_i2c_add(struct amdgpu_device *adev,
  215. struct amdgpu_i2c_bus_rec *rec,
  216. const char *name)
  217. {
  218. struct drm_device *dev = adev->ddev;
  219. int i;
  220. for (i = 0; i < AMDGPU_MAX_I2C_BUS; i++) {
  221. if (!adev->i2c_bus[i]) {
  222. adev->i2c_bus[i] = amdgpu_i2c_create(dev, rec, name);
  223. return;
  224. }
  225. }
  226. }
  227. /* looks up bus based on id */
  228. struct amdgpu_i2c_chan *
  229. amdgpu_i2c_lookup(struct amdgpu_device *adev,
  230. struct amdgpu_i2c_bus_rec *i2c_bus)
  231. {
  232. int i;
  233. for (i = 0; i < AMDGPU_MAX_I2C_BUS; i++) {
  234. if (adev->i2c_bus[i] &&
  235. (adev->i2c_bus[i]->rec.i2c_id == i2c_bus->i2c_id)) {
  236. return adev->i2c_bus[i];
  237. }
  238. }
  239. return NULL;
  240. }
  241. static void amdgpu_i2c_get_byte(struct amdgpu_i2c_chan *i2c_bus,
  242. u8 slave_addr,
  243. u8 addr,
  244. u8 *val)
  245. {
  246. u8 out_buf[2];
  247. u8 in_buf[2];
  248. struct i2c_msg msgs[] = {
  249. {
  250. .addr = slave_addr,
  251. .flags = 0,
  252. .len = 1,
  253. .buf = out_buf,
  254. },
  255. {
  256. .addr = slave_addr,
  257. .flags = I2C_M_RD,
  258. .len = 1,
  259. .buf = in_buf,
  260. }
  261. };
  262. out_buf[0] = addr;
  263. out_buf[1] = 0;
  264. if (i2c_transfer(&i2c_bus->adapter, msgs, 2) == 2) {
  265. *val = in_buf[0];
  266. DRM_DEBUG("val = 0x%02x\n", *val);
  267. } else {
  268. DRM_DEBUG("i2c 0x%02x 0x%02x read failed\n",
  269. addr, *val);
  270. }
  271. }
  272. static void amdgpu_i2c_put_byte(struct amdgpu_i2c_chan *i2c_bus,
  273. u8 slave_addr,
  274. u8 addr,
  275. u8 val)
  276. {
  277. uint8_t out_buf[2];
  278. struct i2c_msg msg = {
  279. .addr = slave_addr,
  280. .flags = 0,
  281. .len = 2,
  282. .buf = out_buf,
  283. };
  284. out_buf[0] = addr;
  285. out_buf[1] = val;
  286. if (i2c_transfer(&i2c_bus->adapter, &msg, 1) != 1)
  287. DRM_DEBUG("i2c 0x%02x 0x%02x write failed\n",
  288. addr, val);
  289. }
  290. /* ddc router switching */
  291. void
  292. amdgpu_i2c_router_select_ddc_port(struct amdgpu_connector *amdgpu_connector)
  293. {
  294. u8 val;
  295. if (!amdgpu_connector->router.ddc_valid)
  296. return;
  297. if (!amdgpu_connector->router_bus)
  298. return;
  299. amdgpu_i2c_get_byte(amdgpu_connector->router_bus,
  300. amdgpu_connector->router.i2c_addr,
  301. 0x3, &val);
  302. val &= ~amdgpu_connector->router.ddc_mux_control_pin;
  303. amdgpu_i2c_put_byte(amdgpu_connector->router_bus,
  304. amdgpu_connector->router.i2c_addr,
  305. 0x3, val);
  306. amdgpu_i2c_get_byte(amdgpu_connector->router_bus,
  307. amdgpu_connector->router.i2c_addr,
  308. 0x1, &val);
  309. val &= ~amdgpu_connector->router.ddc_mux_control_pin;
  310. val |= amdgpu_connector->router.ddc_mux_state;
  311. amdgpu_i2c_put_byte(amdgpu_connector->router_bus,
  312. amdgpu_connector->router.i2c_addr,
  313. 0x1, val);
  314. }
  315. /* clock/data router switching */
  316. void
  317. amdgpu_i2c_router_select_cd_port(struct amdgpu_connector *amdgpu_connector)
  318. {
  319. u8 val;
  320. if (!amdgpu_connector->router.cd_valid)
  321. return;
  322. if (!amdgpu_connector->router_bus)
  323. return;
  324. amdgpu_i2c_get_byte(amdgpu_connector->router_bus,
  325. amdgpu_connector->router.i2c_addr,
  326. 0x3, &val);
  327. val &= ~amdgpu_connector->router.cd_mux_control_pin;
  328. amdgpu_i2c_put_byte(amdgpu_connector->router_bus,
  329. amdgpu_connector->router.i2c_addr,
  330. 0x3, val);
  331. amdgpu_i2c_get_byte(amdgpu_connector->router_bus,
  332. amdgpu_connector->router.i2c_addr,
  333. 0x1, &val);
  334. val &= ~amdgpu_connector->router.cd_mux_control_pin;
  335. val |= amdgpu_connector->router.cd_mux_state;
  336. amdgpu_i2c_put_byte(amdgpu_connector->router_bus,
  337. amdgpu_connector->router.i2c_addr,
  338. 0x1, val);
  339. }