radeon_i2c.c 27 KB

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