radeon_i2c.c 27 KB

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