radeon_i2c.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192
  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 "drmP.h"
  28. #include "drm_edid.h"
  29. #include "radeon_drm.h"
  30. #include "radeon.h"
  31. #include "atom.h"
  32. extern int radeon_atom_hw_i2c_xfer(struct i2c_adapter *i2c_adap,
  33. struct i2c_msg *msgs, int num);
  34. extern u32 radeon_atom_hw_i2c_func(struct i2c_adapter *adap);
  35. /**
  36. * radeon_ddc_probe
  37. *
  38. */
  39. bool radeon_ddc_probe(struct radeon_connector *radeon_connector, bool use_aux)
  40. {
  41. u8 out = 0x0;
  42. u8 buf[8];
  43. int ret;
  44. struct i2c_msg msgs[] = {
  45. {
  46. .addr = DDC_ADDR,
  47. .flags = 0,
  48. .len = 1,
  49. .buf = &out,
  50. },
  51. {
  52. .addr = DDC_ADDR,
  53. .flags = I2C_M_RD,
  54. .len = 8,
  55. .buf = buf,
  56. }
  57. };
  58. /* on hw with routers, select right port */
  59. if (radeon_connector->router.ddc_valid)
  60. radeon_router_select_ddc_port(radeon_connector);
  61. if (use_aux) {
  62. struct radeon_connector_atom_dig *dig = radeon_connector->con_priv;
  63. ret = i2c_transfer(&dig->dp_i2c_bus->adapter, msgs, 2);
  64. } else {
  65. ret = i2c_transfer(&radeon_connector->ddc_bus->adapter, msgs, 2);
  66. }
  67. if (ret != 2)
  68. /* Couldn't find an accessible DDC on this connector */
  69. return false;
  70. /* Probe also for valid EDID header
  71. * EDID header starts with:
  72. * 0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x00.
  73. * Only the first 6 bytes must be valid as
  74. * drm_edid_block_valid() can fix the last 2 bytes */
  75. if (drm_edid_header_is_valid(buf) < 6) {
  76. /* Couldn't find an accessible EDID on this
  77. * connector */
  78. return false;
  79. }
  80. return true;
  81. }
  82. /* bit banging i2c */
  83. static int pre_xfer(struct i2c_adapter *i2c_adap)
  84. {
  85. struct radeon_i2c_chan *i2c = i2c_get_adapdata(i2c_adap);
  86. struct radeon_device *rdev = i2c->dev->dev_private;
  87. struct radeon_i2c_bus_rec *rec = &i2c->rec;
  88. uint32_t temp;
  89. /* RV410 appears to have a bug where the hw i2c in reset
  90. * holds the i2c port in a bad state - switch hw i2c away before
  91. * doing DDC - do this for all r200s/r300s/r400s for safety sake
  92. */
  93. if (rec->hw_capable) {
  94. if ((rdev->family >= CHIP_R200) && !ASIC_IS_AVIVO(rdev)) {
  95. u32 reg;
  96. if (rdev->family >= CHIP_RV350)
  97. reg = RADEON_GPIO_MONID;
  98. else if ((rdev->family == CHIP_R300) ||
  99. (rdev->family == CHIP_R350))
  100. reg = RADEON_GPIO_DVI_DDC;
  101. else
  102. reg = RADEON_GPIO_CRT2_DDC;
  103. mutex_lock(&rdev->dc_hw_i2c_mutex);
  104. if (rec->a_clk_reg == reg) {
  105. WREG32(RADEON_DVI_I2C_CNTL_0, (RADEON_I2C_SOFT_RST |
  106. R200_DVI_I2C_PIN_SEL(R200_SEL_DDC1)));
  107. } else {
  108. WREG32(RADEON_DVI_I2C_CNTL_0, (RADEON_I2C_SOFT_RST |
  109. R200_DVI_I2C_PIN_SEL(R200_SEL_DDC3)));
  110. }
  111. mutex_unlock(&rdev->dc_hw_i2c_mutex);
  112. }
  113. }
  114. /* switch the pads to ddc mode */
  115. if (ASIC_IS_DCE3(rdev) && rec->hw_capable) {
  116. temp = RREG32(rec->mask_clk_reg);
  117. temp &= ~(1 << 16);
  118. WREG32(rec->mask_clk_reg, temp);
  119. }
  120. /* clear the output pin values */
  121. temp = RREG32(rec->a_clk_reg) & ~rec->a_clk_mask;
  122. WREG32(rec->a_clk_reg, temp);
  123. temp = RREG32(rec->a_data_reg) & ~rec->a_data_mask;
  124. WREG32(rec->a_data_reg, temp);
  125. /* set the pins to input */
  126. temp = RREG32(rec->en_clk_reg) & ~rec->en_clk_mask;
  127. WREG32(rec->en_clk_reg, temp);
  128. temp = RREG32(rec->en_data_reg) & ~rec->en_data_mask;
  129. WREG32(rec->en_data_reg, temp);
  130. /* mask the gpio pins for software use */
  131. temp = RREG32(rec->mask_clk_reg) | rec->mask_clk_mask;
  132. WREG32(rec->mask_clk_reg, temp);
  133. temp = RREG32(rec->mask_clk_reg);
  134. temp = RREG32(rec->mask_data_reg) | rec->mask_data_mask;
  135. WREG32(rec->mask_data_reg, temp);
  136. temp = RREG32(rec->mask_data_reg);
  137. return 0;
  138. }
  139. static void post_xfer(struct i2c_adapter *i2c_adap)
  140. {
  141. struct radeon_i2c_chan *i2c = i2c_get_adapdata(i2c_adap);
  142. struct radeon_device *rdev = i2c->dev->dev_private;
  143. struct radeon_i2c_bus_rec *rec = &i2c->rec;
  144. uint32_t temp;
  145. /* unmask the gpio pins for software use */
  146. temp = RREG32(rec->mask_clk_reg) & ~rec->mask_clk_mask;
  147. WREG32(rec->mask_clk_reg, temp);
  148. temp = RREG32(rec->mask_clk_reg);
  149. temp = RREG32(rec->mask_data_reg) & ~rec->mask_data_mask;
  150. WREG32(rec->mask_data_reg, temp);
  151. temp = RREG32(rec->mask_data_reg);
  152. }
  153. static int get_clock(void *i2c_priv)
  154. {
  155. struct radeon_i2c_chan *i2c = i2c_priv;
  156. struct radeon_device *rdev = i2c->dev->dev_private;
  157. struct radeon_i2c_bus_rec *rec = &i2c->rec;
  158. uint32_t val;
  159. /* read the value off the pin */
  160. val = RREG32(rec->y_clk_reg);
  161. val &= rec->y_clk_mask;
  162. return (val != 0);
  163. }
  164. static int get_data(void *i2c_priv)
  165. {
  166. struct radeon_i2c_chan *i2c = i2c_priv;
  167. struct radeon_device *rdev = i2c->dev->dev_private;
  168. struct radeon_i2c_bus_rec *rec = &i2c->rec;
  169. uint32_t val;
  170. /* read the value off the pin */
  171. val = RREG32(rec->y_data_reg);
  172. val &= rec->y_data_mask;
  173. return (val != 0);
  174. }
  175. static void set_clock(void *i2c_priv, int clock)
  176. {
  177. struct radeon_i2c_chan *i2c = i2c_priv;
  178. struct radeon_device *rdev = i2c->dev->dev_private;
  179. struct radeon_i2c_bus_rec *rec = &i2c->rec;
  180. uint32_t val;
  181. /* set pin direction */
  182. val = RREG32(rec->en_clk_reg) & ~rec->en_clk_mask;
  183. val |= clock ? 0 : rec->en_clk_mask;
  184. WREG32(rec->en_clk_reg, val);
  185. }
  186. static void set_data(void *i2c_priv, int data)
  187. {
  188. struct radeon_i2c_chan *i2c = i2c_priv;
  189. struct radeon_device *rdev = i2c->dev->dev_private;
  190. struct radeon_i2c_bus_rec *rec = &i2c->rec;
  191. uint32_t val;
  192. /* set pin direction */
  193. val = RREG32(rec->en_data_reg) & ~rec->en_data_mask;
  194. val |= data ? 0 : rec->en_data_mask;
  195. WREG32(rec->en_data_reg, val);
  196. }
  197. /* hw i2c */
  198. static u32 radeon_get_i2c_prescale(struct radeon_device *rdev)
  199. {
  200. u32 sclk = rdev->pm.current_sclk;
  201. u32 prescale = 0;
  202. u32 nm;
  203. u8 n, m, loop;
  204. int i2c_clock;
  205. switch (rdev->family) {
  206. case CHIP_R100:
  207. case CHIP_RV100:
  208. case CHIP_RS100:
  209. case CHIP_RV200:
  210. case CHIP_RS200:
  211. case CHIP_R200:
  212. case CHIP_RV250:
  213. case CHIP_RS300:
  214. case CHIP_RV280:
  215. case CHIP_R300:
  216. case CHIP_R350:
  217. case CHIP_RV350:
  218. i2c_clock = 60;
  219. nm = (sclk * 10) / (i2c_clock * 4);
  220. for (loop = 1; loop < 255; loop++) {
  221. if ((nm / loop) < loop)
  222. break;
  223. }
  224. n = loop - 1;
  225. m = loop - 2;
  226. prescale = m | (n << 8);
  227. break;
  228. case CHIP_RV380:
  229. case CHIP_RS400:
  230. case CHIP_RS480:
  231. case CHIP_R420:
  232. case CHIP_R423:
  233. case CHIP_RV410:
  234. prescale = (((sclk * 10)/(4 * 128 * 100) + 1) << 8) + 128;
  235. break;
  236. case CHIP_RS600:
  237. case CHIP_RS690:
  238. case CHIP_RS740:
  239. /* todo */
  240. break;
  241. case CHIP_RV515:
  242. case CHIP_R520:
  243. case CHIP_RV530:
  244. case CHIP_RV560:
  245. case CHIP_RV570:
  246. case CHIP_R580:
  247. i2c_clock = 50;
  248. if (rdev->family == CHIP_R520)
  249. prescale = (127 << 8) + ((sclk * 10) / (4 * 127 * i2c_clock));
  250. else
  251. prescale = (((sclk * 10)/(4 * 128 * 100) + 1) << 8) + 128;
  252. break;
  253. case CHIP_R600:
  254. case CHIP_RV610:
  255. case CHIP_RV630:
  256. case CHIP_RV670:
  257. /* todo */
  258. break;
  259. case CHIP_RV620:
  260. case CHIP_RV635:
  261. case CHIP_RS780:
  262. case CHIP_RS880:
  263. case CHIP_RV770:
  264. case CHIP_RV730:
  265. case CHIP_RV710:
  266. case CHIP_RV740:
  267. /* todo */
  268. break;
  269. case CHIP_CEDAR:
  270. case CHIP_REDWOOD:
  271. case CHIP_JUNIPER:
  272. case CHIP_CYPRESS:
  273. case CHIP_HEMLOCK:
  274. /* todo */
  275. break;
  276. default:
  277. DRM_ERROR("i2c: unhandled radeon chip\n");
  278. break;
  279. }
  280. return prescale;
  281. }
  282. /* hw i2c engine for r1xx-4xx hardware
  283. * hw can buffer up to 15 bytes
  284. */
  285. static int r100_hw_i2c_xfer(struct i2c_adapter *i2c_adap,
  286. struct i2c_msg *msgs, int num)
  287. {
  288. struct radeon_i2c_chan *i2c = i2c_get_adapdata(i2c_adap);
  289. struct radeon_device *rdev = i2c->dev->dev_private;
  290. struct radeon_i2c_bus_rec *rec = &i2c->rec;
  291. struct i2c_msg *p;
  292. int i, j, k, ret = num;
  293. u32 prescale;
  294. u32 i2c_cntl_0, i2c_cntl_1, i2c_data;
  295. u32 tmp, reg;
  296. mutex_lock(&rdev->dc_hw_i2c_mutex);
  297. /* take the pm lock since we need a constant sclk */
  298. mutex_lock(&rdev->pm.mutex);
  299. prescale = radeon_get_i2c_prescale(rdev);
  300. reg = ((prescale << RADEON_I2C_PRESCALE_SHIFT) |
  301. RADEON_I2C_DRIVE_EN |
  302. RADEON_I2C_START |
  303. RADEON_I2C_STOP |
  304. RADEON_I2C_GO);
  305. if (rdev->is_atom_bios) {
  306. tmp = RREG32(RADEON_BIOS_6_SCRATCH);
  307. WREG32(RADEON_BIOS_6_SCRATCH, tmp | ATOM_S6_HW_I2C_BUSY_STATE);
  308. }
  309. if (rec->mm_i2c) {
  310. i2c_cntl_0 = RADEON_I2C_CNTL_0;
  311. i2c_cntl_1 = RADEON_I2C_CNTL_1;
  312. i2c_data = RADEON_I2C_DATA;
  313. } else {
  314. i2c_cntl_0 = RADEON_DVI_I2C_CNTL_0;
  315. i2c_cntl_1 = RADEON_DVI_I2C_CNTL_1;
  316. i2c_data = RADEON_DVI_I2C_DATA;
  317. switch (rdev->family) {
  318. case CHIP_R100:
  319. case CHIP_RV100:
  320. case CHIP_RS100:
  321. case CHIP_RV200:
  322. case CHIP_RS200:
  323. case CHIP_RS300:
  324. switch (rec->mask_clk_reg) {
  325. case RADEON_GPIO_DVI_DDC:
  326. /* no gpio select bit */
  327. break;
  328. default:
  329. DRM_ERROR("gpio not supported with hw i2c\n");
  330. ret = -EINVAL;
  331. goto done;
  332. }
  333. break;
  334. case CHIP_R200:
  335. /* only bit 4 on r200 */
  336. switch (rec->mask_clk_reg) {
  337. case RADEON_GPIO_DVI_DDC:
  338. reg |= R200_DVI_I2C_PIN_SEL(R200_SEL_DDC1);
  339. break;
  340. case RADEON_GPIO_MONID:
  341. reg |= R200_DVI_I2C_PIN_SEL(R200_SEL_DDC3);
  342. break;
  343. default:
  344. DRM_ERROR("gpio not supported with hw i2c\n");
  345. ret = -EINVAL;
  346. goto done;
  347. }
  348. break;
  349. case CHIP_RV250:
  350. case CHIP_RV280:
  351. /* bits 3 and 4 */
  352. switch (rec->mask_clk_reg) {
  353. case RADEON_GPIO_DVI_DDC:
  354. reg |= R200_DVI_I2C_PIN_SEL(R200_SEL_DDC1);
  355. break;
  356. case RADEON_GPIO_VGA_DDC:
  357. reg |= R200_DVI_I2C_PIN_SEL(R200_SEL_DDC2);
  358. break;
  359. case RADEON_GPIO_CRT2_DDC:
  360. reg |= R200_DVI_I2C_PIN_SEL(R200_SEL_DDC3);
  361. break;
  362. default:
  363. DRM_ERROR("gpio not supported with hw i2c\n");
  364. ret = -EINVAL;
  365. goto done;
  366. }
  367. break;
  368. case CHIP_R300:
  369. case CHIP_R350:
  370. /* only bit 4 on r300/r350 */
  371. switch (rec->mask_clk_reg) {
  372. case RADEON_GPIO_VGA_DDC:
  373. reg |= R200_DVI_I2C_PIN_SEL(R200_SEL_DDC1);
  374. break;
  375. case RADEON_GPIO_DVI_DDC:
  376. reg |= R200_DVI_I2C_PIN_SEL(R200_SEL_DDC3);
  377. break;
  378. default:
  379. DRM_ERROR("gpio not supported with hw i2c\n");
  380. ret = -EINVAL;
  381. goto done;
  382. }
  383. break;
  384. case CHIP_RV350:
  385. case CHIP_RV380:
  386. case CHIP_R420:
  387. case CHIP_R423:
  388. case CHIP_RV410:
  389. case CHIP_RS400:
  390. case CHIP_RS480:
  391. /* bits 3 and 4 */
  392. switch (rec->mask_clk_reg) {
  393. case RADEON_GPIO_VGA_DDC:
  394. reg |= R200_DVI_I2C_PIN_SEL(R200_SEL_DDC1);
  395. break;
  396. case RADEON_GPIO_DVI_DDC:
  397. reg |= R200_DVI_I2C_PIN_SEL(R200_SEL_DDC2);
  398. break;
  399. case RADEON_GPIO_MONID:
  400. reg |= R200_DVI_I2C_PIN_SEL(R200_SEL_DDC3);
  401. break;
  402. default:
  403. DRM_ERROR("gpio not supported with hw i2c\n");
  404. ret = -EINVAL;
  405. goto done;
  406. }
  407. break;
  408. default:
  409. DRM_ERROR("unsupported asic\n");
  410. ret = -EINVAL;
  411. goto done;
  412. break;
  413. }
  414. }
  415. /* check for bus probe */
  416. p = &msgs[0];
  417. if ((num == 1) && (p->len == 0)) {
  418. WREG32(i2c_cntl_0, (RADEON_I2C_DONE |
  419. RADEON_I2C_NACK |
  420. RADEON_I2C_HALT |
  421. RADEON_I2C_SOFT_RST));
  422. WREG32(i2c_data, (p->addr << 1) & 0xff);
  423. WREG32(i2c_data, 0);
  424. WREG32(i2c_cntl_1, ((1 << RADEON_I2C_DATA_COUNT_SHIFT) |
  425. (1 << RADEON_I2C_ADDR_COUNT_SHIFT) |
  426. RADEON_I2C_EN |
  427. (48 << RADEON_I2C_TIME_LIMIT_SHIFT)));
  428. WREG32(i2c_cntl_0, reg);
  429. for (k = 0; k < 32; k++) {
  430. udelay(10);
  431. tmp = RREG32(i2c_cntl_0);
  432. if (tmp & RADEON_I2C_GO)
  433. continue;
  434. tmp = RREG32(i2c_cntl_0);
  435. if (tmp & RADEON_I2C_DONE)
  436. break;
  437. else {
  438. DRM_DEBUG("i2c write error 0x%08x\n", tmp);
  439. WREG32(i2c_cntl_0, tmp | RADEON_I2C_ABORT);
  440. ret = -EIO;
  441. goto done;
  442. }
  443. }
  444. goto done;
  445. }
  446. for (i = 0; i < num; i++) {
  447. p = &msgs[i];
  448. for (j = 0; j < p->len; j++) {
  449. if (p->flags & I2C_M_RD) {
  450. WREG32(i2c_cntl_0, (RADEON_I2C_DONE |
  451. RADEON_I2C_NACK |
  452. RADEON_I2C_HALT |
  453. RADEON_I2C_SOFT_RST));
  454. WREG32(i2c_data, ((p->addr << 1) & 0xff) | 0x1);
  455. WREG32(i2c_cntl_1, ((1 << RADEON_I2C_DATA_COUNT_SHIFT) |
  456. (1 << RADEON_I2C_ADDR_COUNT_SHIFT) |
  457. RADEON_I2C_EN |
  458. (48 << RADEON_I2C_TIME_LIMIT_SHIFT)));
  459. WREG32(i2c_cntl_0, reg | RADEON_I2C_RECEIVE);
  460. for (k = 0; k < 32; k++) {
  461. udelay(10);
  462. tmp = RREG32(i2c_cntl_0);
  463. if (tmp & RADEON_I2C_GO)
  464. continue;
  465. tmp = RREG32(i2c_cntl_0);
  466. if (tmp & RADEON_I2C_DONE)
  467. break;
  468. else {
  469. DRM_DEBUG("i2c read error 0x%08x\n", tmp);
  470. WREG32(i2c_cntl_0, tmp | RADEON_I2C_ABORT);
  471. ret = -EIO;
  472. goto done;
  473. }
  474. }
  475. p->buf[j] = RREG32(i2c_data) & 0xff;
  476. } else {
  477. WREG32(i2c_cntl_0, (RADEON_I2C_DONE |
  478. RADEON_I2C_NACK |
  479. RADEON_I2C_HALT |
  480. RADEON_I2C_SOFT_RST));
  481. WREG32(i2c_data, (p->addr << 1) & 0xff);
  482. WREG32(i2c_data, p->buf[j]);
  483. WREG32(i2c_cntl_1, ((1 << RADEON_I2C_DATA_COUNT_SHIFT) |
  484. (1 << RADEON_I2C_ADDR_COUNT_SHIFT) |
  485. RADEON_I2C_EN |
  486. (48 << RADEON_I2C_TIME_LIMIT_SHIFT)));
  487. WREG32(i2c_cntl_0, reg);
  488. for (k = 0; k < 32; k++) {
  489. udelay(10);
  490. tmp = RREG32(i2c_cntl_0);
  491. if (tmp & RADEON_I2C_GO)
  492. continue;
  493. tmp = RREG32(i2c_cntl_0);
  494. if (tmp & RADEON_I2C_DONE)
  495. break;
  496. else {
  497. DRM_DEBUG("i2c write error 0x%08x\n", tmp);
  498. WREG32(i2c_cntl_0, tmp | RADEON_I2C_ABORT);
  499. ret = -EIO;
  500. goto done;
  501. }
  502. }
  503. }
  504. }
  505. }
  506. done:
  507. WREG32(i2c_cntl_0, 0);
  508. WREG32(i2c_cntl_1, 0);
  509. WREG32(i2c_cntl_0, (RADEON_I2C_DONE |
  510. RADEON_I2C_NACK |
  511. RADEON_I2C_HALT |
  512. RADEON_I2C_SOFT_RST));
  513. if (rdev->is_atom_bios) {
  514. tmp = RREG32(RADEON_BIOS_6_SCRATCH);
  515. tmp &= ~ATOM_S6_HW_I2C_BUSY_STATE;
  516. WREG32(RADEON_BIOS_6_SCRATCH, tmp);
  517. }
  518. mutex_unlock(&rdev->pm.mutex);
  519. mutex_unlock(&rdev->dc_hw_i2c_mutex);
  520. return ret;
  521. }
  522. /* hw i2c engine for r5xx hardware
  523. * hw can buffer up to 15 bytes
  524. */
  525. static int r500_hw_i2c_xfer(struct i2c_adapter *i2c_adap,
  526. struct i2c_msg *msgs, int num)
  527. {
  528. struct radeon_i2c_chan *i2c = i2c_get_adapdata(i2c_adap);
  529. struct radeon_device *rdev = i2c->dev->dev_private;
  530. struct radeon_i2c_bus_rec *rec = &i2c->rec;
  531. struct i2c_msg *p;
  532. int i, j, remaining, current_count, buffer_offset, ret = num;
  533. u32 prescale;
  534. u32 tmp, reg;
  535. u32 saved1, saved2;
  536. mutex_lock(&rdev->dc_hw_i2c_mutex);
  537. /* take the pm lock since we need a constant sclk */
  538. mutex_lock(&rdev->pm.mutex);
  539. prescale = radeon_get_i2c_prescale(rdev);
  540. /* clear gpio mask bits */
  541. tmp = RREG32(rec->mask_clk_reg);
  542. tmp &= ~rec->mask_clk_mask;
  543. WREG32(rec->mask_clk_reg, tmp);
  544. tmp = RREG32(rec->mask_clk_reg);
  545. tmp = RREG32(rec->mask_data_reg);
  546. tmp &= ~rec->mask_data_mask;
  547. WREG32(rec->mask_data_reg, tmp);
  548. tmp = RREG32(rec->mask_data_reg);
  549. /* clear pin values */
  550. tmp = RREG32(rec->a_clk_reg);
  551. tmp &= ~rec->a_clk_mask;
  552. WREG32(rec->a_clk_reg, tmp);
  553. tmp = RREG32(rec->a_clk_reg);
  554. tmp = RREG32(rec->a_data_reg);
  555. tmp &= ~rec->a_data_mask;
  556. WREG32(rec->a_data_reg, tmp);
  557. tmp = RREG32(rec->a_data_reg);
  558. /* set the pins to input */
  559. tmp = RREG32(rec->en_clk_reg);
  560. tmp &= ~rec->en_clk_mask;
  561. WREG32(rec->en_clk_reg, tmp);
  562. tmp = RREG32(rec->en_clk_reg);
  563. tmp = RREG32(rec->en_data_reg);
  564. tmp &= ~rec->en_data_mask;
  565. WREG32(rec->en_data_reg, tmp);
  566. tmp = RREG32(rec->en_data_reg);
  567. /* */
  568. tmp = RREG32(RADEON_BIOS_6_SCRATCH);
  569. WREG32(RADEON_BIOS_6_SCRATCH, tmp | ATOM_S6_HW_I2C_BUSY_STATE);
  570. saved1 = RREG32(AVIVO_DC_I2C_CONTROL1);
  571. saved2 = RREG32(0x494);
  572. WREG32(0x494, saved2 | 0x1);
  573. WREG32(AVIVO_DC_I2C_ARBITRATION, AVIVO_DC_I2C_SW_WANTS_TO_USE_I2C);
  574. for (i = 0; i < 50; i++) {
  575. udelay(1);
  576. if (RREG32(AVIVO_DC_I2C_ARBITRATION) & AVIVO_DC_I2C_SW_CAN_USE_I2C)
  577. break;
  578. }
  579. if (i == 50) {
  580. DRM_ERROR("failed to get i2c bus\n");
  581. ret = -EBUSY;
  582. goto done;
  583. }
  584. reg = AVIVO_DC_I2C_START | AVIVO_DC_I2C_STOP | AVIVO_DC_I2C_EN;
  585. switch (rec->mask_clk_reg) {
  586. case AVIVO_DC_GPIO_DDC1_MASK:
  587. reg |= AVIVO_DC_I2C_PIN_SELECT(AVIVO_SEL_DDC1);
  588. break;
  589. case AVIVO_DC_GPIO_DDC2_MASK:
  590. reg |= AVIVO_DC_I2C_PIN_SELECT(AVIVO_SEL_DDC2);
  591. break;
  592. case AVIVO_DC_GPIO_DDC3_MASK:
  593. reg |= AVIVO_DC_I2C_PIN_SELECT(AVIVO_SEL_DDC3);
  594. break;
  595. default:
  596. DRM_ERROR("gpio not supported with hw i2c\n");
  597. ret = -EINVAL;
  598. goto done;
  599. }
  600. /* check for bus probe */
  601. p = &msgs[0];
  602. if ((num == 1) && (p->len == 0)) {
  603. WREG32(AVIVO_DC_I2C_STATUS1, (AVIVO_DC_I2C_DONE |
  604. AVIVO_DC_I2C_NACK |
  605. AVIVO_DC_I2C_HALT));
  606. WREG32(AVIVO_DC_I2C_RESET, AVIVO_DC_I2C_SOFT_RESET);
  607. udelay(1);
  608. WREG32(AVIVO_DC_I2C_RESET, 0);
  609. WREG32(AVIVO_DC_I2C_DATA, (p->addr << 1) & 0xff);
  610. WREG32(AVIVO_DC_I2C_DATA, 0);
  611. WREG32(AVIVO_DC_I2C_CONTROL3, AVIVO_DC_I2C_TIME_LIMIT(48));
  612. WREG32(AVIVO_DC_I2C_CONTROL2, (AVIVO_DC_I2C_ADDR_COUNT(1) |
  613. AVIVO_DC_I2C_DATA_COUNT(1) |
  614. (prescale << 16)));
  615. WREG32(AVIVO_DC_I2C_CONTROL1, reg);
  616. WREG32(AVIVO_DC_I2C_STATUS1, AVIVO_DC_I2C_GO);
  617. for (j = 0; j < 200; j++) {
  618. udelay(50);
  619. tmp = RREG32(AVIVO_DC_I2C_STATUS1);
  620. if (tmp & AVIVO_DC_I2C_GO)
  621. continue;
  622. tmp = RREG32(AVIVO_DC_I2C_STATUS1);
  623. if (tmp & AVIVO_DC_I2C_DONE)
  624. break;
  625. else {
  626. DRM_DEBUG("i2c write error 0x%08x\n", tmp);
  627. WREG32(AVIVO_DC_I2C_RESET, AVIVO_DC_I2C_ABORT);
  628. ret = -EIO;
  629. goto done;
  630. }
  631. }
  632. goto done;
  633. }
  634. for (i = 0; i < num; i++) {
  635. p = &msgs[i];
  636. remaining = p->len;
  637. buffer_offset = 0;
  638. if (p->flags & I2C_M_RD) {
  639. while (remaining) {
  640. if (remaining > 15)
  641. current_count = 15;
  642. else
  643. current_count = remaining;
  644. WREG32(AVIVO_DC_I2C_STATUS1, (AVIVO_DC_I2C_DONE |
  645. AVIVO_DC_I2C_NACK |
  646. AVIVO_DC_I2C_HALT));
  647. WREG32(AVIVO_DC_I2C_RESET, AVIVO_DC_I2C_SOFT_RESET);
  648. udelay(1);
  649. WREG32(AVIVO_DC_I2C_RESET, 0);
  650. WREG32(AVIVO_DC_I2C_DATA, ((p->addr << 1) & 0xff) | 0x1);
  651. WREG32(AVIVO_DC_I2C_CONTROL3, AVIVO_DC_I2C_TIME_LIMIT(48));
  652. WREG32(AVIVO_DC_I2C_CONTROL2, (AVIVO_DC_I2C_ADDR_COUNT(1) |
  653. AVIVO_DC_I2C_DATA_COUNT(current_count) |
  654. (prescale << 16)));
  655. WREG32(AVIVO_DC_I2C_CONTROL1, reg | AVIVO_DC_I2C_RECEIVE);
  656. WREG32(AVIVO_DC_I2C_STATUS1, AVIVO_DC_I2C_GO);
  657. for (j = 0; j < 200; j++) {
  658. udelay(50);
  659. tmp = RREG32(AVIVO_DC_I2C_STATUS1);
  660. if (tmp & AVIVO_DC_I2C_GO)
  661. continue;
  662. tmp = RREG32(AVIVO_DC_I2C_STATUS1);
  663. if (tmp & AVIVO_DC_I2C_DONE)
  664. break;
  665. else {
  666. DRM_DEBUG("i2c read error 0x%08x\n", tmp);
  667. WREG32(AVIVO_DC_I2C_RESET, AVIVO_DC_I2C_ABORT);
  668. ret = -EIO;
  669. goto done;
  670. }
  671. }
  672. for (j = 0; j < current_count; j++)
  673. p->buf[buffer_offset + j] = RREG32(AVIVO_DC_I2C_DATA) & 0xff;
  674. remaining -= current_count;
  675. buffer_offset += current_count;
  676. }
  677. } else {
  678. while (remaining) {
  679. if (remaining > 15)
  680. current_count = 15;
  681. else
  682. current_count = remaining;
  683. WREG32(AVIVO_DC_I2C_STATUS1, (AVIVO_DC_I2C_DONE |
  684. AVIVO_DC_I2C_NACK |
  685. AVIVO_DC_I2C_HALT));
  686. WREG32(AVIVO_DC_I2C_RESET, AVIVO_DC_I2C_SOFT_RESET);
  687. udelay(1);
  688. WREG32(AVIVO_DC_I2C_RESET, 0);
  689. WREG32(AVIVO_DC_I2C_DATA, (p->addr << 1) & 0xff);
  690. for (j = 0; j < current_count; j++)
  691. WREG32(AVIVO_DC_I2C_DATA, p->buf[buffer_offset + j]);
  692. WREG32(AVIVO_DC_I2C_CONTROL3, AVIVO_DC_I2C_TIME_LIMIT(48));
  693. WREG32(AVIVO_DC_I2C_CONTROL2, (AVIVO_DC_I2C_ADDR_COUNT(1) |
  694. AVIVO_DC_I2C_DATA_COUNT(current_count) |
  695. (prescale << 16)));
  696. WREG32(AVIVO_DC_I2C_CONTROL1, reg);
  697. WREG32(AVIVO_DC_I2C_STATUS1, AVIVO_DC_I2C_GO);
  698. for (j = 0; j < 200; j++) {
  699. udelay(50);
  700. tmp = RREG32(AVIVO_DC_I2C_STATUS1);
  701. if (tmp & AVIVO_DC_I2C_GO)
  702. continue;
  703. tmp = RREG32(AVIVO_DC_I2C_STATUS1);
  704. if (tmp & AVIVO_DC_I2C_DONE)
  705. break;
  706. else {
  707. DRM_DEBUG("i2c write error 0x%08x\n", tmp);
  708. WREG32(AVIVO_DC_I2C_RESET, AVIVO_DC_I2C_ABORT);
  709. ret = -EIO;
  710. goto done;
  711. }
  712. }
  713. remaining -= current_count;
  714. buffer_offset += current_count;
  715. }
  716. }
  717. }
  718. done:
  719. WREG32(AVIVO_DC_I2C_STATUS1, (AVIVO_DC_I2C_DONE |
  720. AVIVO_DC_I2C_NACK |
  721. AVIVO_DC_I2C_HALT));
  722. WREG32(AVIVO_DC_I2C_RESET, AVIVO_DC_I2C_SOFT_RESET);
  723. udelay(1);
  724. WREG32(AVIVO_DC_I2C_RESET, 0);
  725. WREG32(AVIVO_DC_I2C_ARBITRATION, AVIVO_DC_I2C_SW_DONE_USING_I2C);
  726. WREG32(AVIVO_DC_I2C_CONTROL1, saved1);
  727. WREG32(0x494, saved2);
  728. tmp = RREG32(RADEON_BIOS_6_SCRATCH);
  729. tmp &= ~ATOM_S6_HW_I2C_BUSY_STATE;
  730. WREG32(RADEON_BIOS_6_SCRATCH, tmp);
  731. mutex_unlock(&rdev->pm.mutex);
  732. mutex_unlock(&rdev->dc_hw_i2c_mutex);
  733. return ret;
  734. }
  735. static int radeon_hw_i2c_xfer(struct i2c_adapter *i2c_adap,
  736. struct i2c_msg *msgs, int num)
  737. {
  738. struct radeon_i2c_chan *i2c = i2c_get_adapdata(i2c_adap);
  739. struct radeon_device *rdev = i2c->dev->dev_private;
  740. struct radeon_i2c_bus_rec *rec = &i2c->rec;
  741. int ret = 0;
  742. switch (rdev->family) {
  743. case CHIP_R100:
  744. case CHIP_RV100:
  745. case CHIP_RS100:
  746. case CHIP_RV200:
  747. case CHIP_RS200:
  748. case CHIP_R200:
  749. case CHIP_RV250:
  750. case CHIP_RS300:
  751. case CHIP_RV280:
  752. case CHIP_R300:
  753. case CHIP_R350:
  754. case CHIP_RV350:
  755. case CHIP_RV380:
  756. case CHIP_R420:
  757. case CHIP_R423:
  758. case CHIP_RV410:
  759. case CHIP_RS400:
  760. case CHIP_RS480:
  761. ret = r100_hw_i2c_xfer(i2c_adap, msgs, num);
  762. break;
  763. case CHIP_RS600:
  764. case CHIP_RS690:
  765. case CHIP_RS740:
  766. /* XXX fill in hw i2c implementation */
  767. break;
  768. case CHIP_RV515:
  769. case CHIP_R520:
  770. case CHIP_RV530:
  771. case CHIP_RV560:
  772. case CHIP_RV570:
  773. case CHIP_R580:
  774. if (rec->mm_i2c)
  775. ret = r100_hw_i2c_xfer(i2c_adap, msgs, num);
  776. else
  777. ret = r500_hw_i2c_xfer(i2c_adap, msgs, num);
  778. break;
  779. case CHIP_R600:
  780. case CHIP_RV610:
  781. case CHIP_RV630:
  782. case CHIP_RV670:
  783. /* XXX fill in hw i2c implementation */
  784. break;
  785. case CHIP_RV620:
  786. case CHIP_RV635:
  787. case CHIP_RS780:
  788. case CHIP_RS880:
  789. case CHIP_RV770:
  790. case CHIP_RV730:
  791. case CHIP_RV710:
  792. case CHIP_RV740:
  793. /* XXX fill in hw i2c implementation */
  794. break;
  795. case CHIP_CEDAR:
  796. case CHIP_REDWOOD:
  797. case CHIP_JUNIPER:
  798. case CHIP_CYPRESS:
  799. case CHIP_HEMLOCK:
  800. /* XXX fill in hw i2c implementation */
  801. break;
  802. default:
  803. DRM_ERROR("i2c: unhandled radeon chip\n");
  804. ret = -EIO;
  805. break;
  806. }
  807. return ret;
  808. }
  809. static u32 radeon_hw_i2c_func(struct i2c_adapter *adap)
  810. {
  811. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  812. }
  813. static const struct i2c_algorithm radeon_i2c_algo = {
  814. .master_xfer = radeon_hw_i2c_xfer,
  815. .functionality = radeon_hw_i2c_func,
  816. };
  817. static const struct i2c_algorithm radeon_atom_i2c_algo = {
  818. .master_xfer = radeon_atom_hw_i2c_xfer,
  819. .functionality = radeon_atom_hw_i2c_func,
  820. };
  821. struct radeon_i2c_chan *radeon_i2c_create(struct drm_device *dev,
  822. struct radeon_i2c_bus_rec *rec,
  823. const char *name)
  824. {
  825. struct radeon_device *rdev = dev->dev_private;
  826. struct radeon_i2c_chan *i2c;
  827. int ret;
  828. /* don't add the mm_i2c bus unless hw_i2c is enabled */
  829. if (rec->mm_i2c && (radeon_hw_i2c == 0))
  830. return NULL;
  831. i2c = kzalloc(sizeof(struct radeon_i2c_chan), GFP_KERNEL);
  832. if (i2c == NULL)
  833. return NULL;
  834. i2c->rec = *rec;
  835. i2c->adapter.owner = THIS_MODULE;
  836. i2c->adapter.class = I2C_CLASS_DDC;
  837. i2c->adapter.dev.parent = &dev->pdev->dev;
  838. i2c->dev = dev;
  839. i2c_set_adapdata(&i2c->adapter, i2c);
  840. if (rec->mm_i2c ||
  841. (rec->hw_capable &&
  842. radeon_hw_i2c &&
  843. ((rdev->family <= CHIP_RS480) ||
  844. ((rdev->family >= CHIP_RV515) && (rdev->family <= CHIP_R580))))) {
  845. /* set the radeon hw i2c adapter */
  846. snprintf(i2c->adapter.name, sizeof(i2c->adapter.name),
  847. "Radeon i2c hw bus %s", name);
  848. i2c->adapter.algo = &radeon_i2c_algo;
  849. ret = i2c_add_adapter(&i2c->adapter);
  850. if (ret) {
  851. DRM_ERROR("Failed to register hw i2c %s\n", name);
  852. goto out_free;
  853. }
  854. } else if (rec->hw_capable &&
  855. radeon_hw_i2c &&
  856. ASIC_IS_DCE3(rdev)) {
  857. /* hw i2c using atom */
  858. snprintf(i2c->adapter.name, sizeof(i2c->adapter.name),
  859. "Radeon i2c hw bus %s", name);
  860. i2c->adapter.algo = &radeon_atom_i2c_algo;
  861. ret = i2c_add_adapter(&i2c->adapter);
  862. if (ret) {
  863. DRM_ERROR("Failed to register hw i2c %s\n", name);
  864. goto out_free;
  865. }
  866. } else {
  867. /* set the radeon bit adapter */
  868. snprintf(i2c->adapter.name, sizeof(i2c->adapter.name),
  869. "Radeon i2c bit bus %s", name);
  870. i2c->adapter.algo_data = &i2c->algo.bit;
  871. i2c->algo.bit.pre_xfer = pre_xfer;
  872. i2c->algo.bit.post_xfer = post_xfer;
  873. i2c->algo.bit.setsda = set_data;
  874. i2c->algo.bit.setscl = set_clock;
  875. i2c->algo.bit.getsda = get_data;
  876. i2c->algo.bit.getscl = get_clock;
  877. i2c->algo.bit.udelay = 10;
  878. i2c->algo.bit.timeout = usecs_to_jiffies(2200); /* from VESA */
  879. i2c->algo.bit.data = i2c;
  880. ret = i2c_bit_add_bus(&i2c->adapter);
  881. if (ret) {
  882. DRM_ERROR("Failed to register bit i2c %s\n", name);
  883. goto out_free;
  884. }
  885. }
  886. return i2c;
  887. out_free:
  888. kfree(i2c);
  889. return NULL;
  890. }
  891. struct radeon_i2c_chan *radeon_i2c_create_dp(struct drm_device *dev,
  892. struct radeon_i2c_bus_rec *rec,
  893. const char *name)
  894. {
  895. struct radeon_i2c_chan *i2c;
  896. int ret;
  897. i2c = kzalloc(sizeof(struct radeon_i2c_chan), GFP_KERNEL);
  898. if (i2c == NULL)
  899. return NULL;
  900. i2c->rec = *rec;
  901. i2c->adapter.owner = THIS_MODULE;
  902. i2c->adapter.class = I2C_CLASS_DDC;
  903. i2c->adapter.dev.parent = &dev->pdev->dev;
  904. i2c->dev = dev;
  905. snprintf(i2c->adapter.name, sizeof(i2c->adapter.name),
  906. "Radeon aux bus %s", name);
  907. i2c_set_adapdata(&i2c->adapter, i2c);
  908. i2c->adapter.algo_data = &i2c->algo.dp;
  909. i2c->algo.dp.aux_ch = radeon_dp_i2c_aux_ch;
  910. i2c->algo.dp.address = 0;
  911. ret = i2c_dp_aux_add_bus(&i2c->adapter);
  912. if (ret) {
  913. DRM_INFO("Failed to register i2c %s\n", name);
  914. goto out_free;
  915. }
  916. return i2c;
  917. out_free:
  918. kfree(i2c);
  919. return NULL;
  920. }
  921. void radeon_i2c_destroy(struct radeon_i2c_chan *i2c)
  922. {
  923. if (!i2c)
  924. return;
  925. i2c_del_adapter(&i2c->adapter);
  926. kfree(i2c);
  927. }
  928. /* Add the default buses */
  929. void radeon_i2c_init(struct radeon_device *rdev)
  930. {
  931. if (radeon_hw_i2c)
  932. DRM_INFO("hw_i2c forced on, you may experience display detection problems!\n");
  933. if (rdev->is_atom_bios)
  934. radeon_atombios_i2c_init(rdev);
  935. else
  936. radeon_combios_i2c_init(rdev);
  937. }
  938. /* remove all the buses */
  939. void radeon_i2c_fini(struct radeon_device *rdev)
  940. {
  941. int i;
  942. for (i = 0; i < RADEON_MAX_I2C_BUS; i++) {
  943. if (rdev->i2c_bus[i]) {
  944. radeon_i2c_destroy(rdev->i2c_bus[i]);
  945. rdev->i2c_bus[i] = NULL;
  946. }
  947. }
  948. }
  949. /* Add additional buses */
  950. void radeon_i2c_add(struct radeon_device *rdev,
  951. struct radeon_i2c_bus_rec *rec,
  952. const char *name)
  953. {
  954. struct drm_device *dev = rdev->ddev;
  955. int i;
  956. for (i = 0; i < RADEON_MAX_I2C_BUS; i++) {
  957. if (!rdev->i2c_bus[i]) {
  958. rdev->i2c_bus[i] = radeon_i2c_create(dev, rec, name);
  959. return;
  960. }
  961. }
  962. }
  963. /* looks up bus based on id */
  964. struct radeon_i2c_chan *radeon_i2c_lookup(struct radeon_device *rdev,
  965. struct radeon_i2c_bus_rec *i2c_bus)
  966. {
  967. int i;
  968. for (i = 0; i < RADEON_MAX_I2C_BUS; i++) {
  969. if (rdev->i2c_bus[i] &&
  970. (rdev->i2c_bus[i]->rec.i2c_id == i2c_bus->i2c_id)) {
  971. return rdev->i2c_bus[i];
  972. }
  973. }
  974. return NULL;
  975. }
  976. struct drm_encoder *radeon_best_encoder(struct drm_connector *connector)
  977. {
  978. return NULL;
  979. }
  980. void radeon_i2c_get_byte(struct radeon_i2c_chan *i2c_bus,
  981. u8 slave_addr,
  982. u8 addr,
  983. u8 *val)
  984. {
  985. u8 out_buf[2];
  986. u8 in_buf[2];
  987. struct i2c_msg msgs[] = {
  988. {
  989. .addr = slave_addr,
  990. .flags = 0,
  991. .len = 1,
  992. .buf = out_buf,
  993. },
  994. {
  995. .addr = slave_addr,
  996. .flags = I2C_M_RD,
  997. .len = 1,
  998. .buf = in_buf,
  999. }
  1000. };
  1001. out_buf[0] = addr;
  1002. out_buf[1] = 0;
  1003. if (i2c_transfer(&i2c_bus->adapter, msgs, 2) == 2) {
  1004. *val = in_buf[0];
  1005. DRM_DEBUG("val = 0x%02x\n", *val);
  1006. } else {
  1007. DRM_DEBUG("i2c 0x%02x 0x%02x read failed\n",
  1008. addr, *val);
  1009. }
  1010. }
  1011. void radeon_i2c_put_byte(struct radeon_i2c_chan *i2c_bus,
  1012. u8 slave_addr,
  1013. u8 addr,
  1014. u8 val)
  1015. {
  1016. uint8_t out_buf[2];
  1017. struct i2c_msg msg = {
  1018. .addr = slave_addr,
  1019. .flags = 0,
  1020. .len = 2,
  1021. .buf = out_buf,
  1022. };
  1023. out_buf[0] = addr;
  1024. out_buf[1] = val;
  1025. if (i2c_transfer(&i2c_bus->adapter, &msg, 1) != 1)
  1026. DRM_DEBUG("i2c 0x%02x 0x%02x write failed\n",
  1027. addr, val);
  1028. }
  1029. /* ddc router switching */
  1030. void radeon_router_select_ddc_port(struct radeon_connector *radeon_connector)
  1031. {
  1032. u8 val;
  1033. if (!radeon_connector->router.ddc_valid)
  1034. return;
  1035. if (!radeon_connector->router_bus)
  1036. return;
  1037. radeon_i2c_get_byte(radeon_connector->router_bus,
  1038. radeon_connector->router.i2c_addr,
  1039. 0x3, &val);
  1040. val &= ~radeon_connector->router.ddc_mux_control_pin;
  1041. radeon_i2c_put_byte(radeon_connector->router_bus,
  1042. radeon_connector->router.i2c_addr,
  1043. 0x3, val);
  1044. radeon_i2c_get_byte(radeon_connector->router_bus,
  1045. radeon_connector->router.i2c_addr,
  1046. 0x1, &val);
  1047. val &= ~radeon_connector->router.ddc_mux_control_pin;
  1048. val |= radeon_connector->router.ddc_mux_state;
  1049. radeon_i2c_put_byte(radeon_connector->router_bus,
  1050. radeon_connector->router.i2c_addr,
  1051. 0x1, val);
  1052. }
  1053. /* clock/data router switching */
  1054. void radeon_router_select_cd_port(struct radeon_connector *radeon_connector)
  1055. {
  1056. u8 val;
  1057. if (!radeon_connector->router.cd_valid)
  1058. return;
  1059. if (!radeon_connector->router_bus)
  1060. return;
  1061. radeon_i2c_get_byte(radeon_connector->router_bus,
  1062. radeon_connector->router.i2c_addr,
  1063. 0x3, &val);
  1064. val &= ~radeon_connector->router.cd_mux_control_pin;
  1065. radeon_i2c_put_byte(radeon_connector->router_bus,
  1066. radeon_connector->router.i2c_addr,
  1067. 0x3, val);
  1068. radeon_i2c_get_byte(radeon_connector->router_bus,
  1069. radeon_connector->router.i2c_addr,
  1070. 0x1, &val);
  1071. val &= ~radeon_connector->router.cd_mux_control_pin;
  1072. val |= radeon_connector->router.cd_mux_state;
  1073. radeon_i2c_put_byte(radeon_connector->router_bus,
  1074. radeon_connector->router.i2c_addr,
  1075. 0x1, val);
  1076. }