nouveau_i2c.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. /*
  2. * Copyright 2009 Red Hat Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * Authors: Ben Skeggs
  23. */
  24. #include <linux/module.h>
  25. #include "drmP.h"
  26. #include "nouveau_drv.h"
  27. #include "nouveau_i2c.h"
  28. #include "nouveau_hw.h"
  29. #define T_TIMEOUT 2200000
  30. #define T_RISEFALL 1000
  31. #define T_HOLD 5000
  32. static void
  33. i2c_drive_scl(void *data, int state)
  34. {
  35. struct nouveau_i2c_chan *port = data;
  36. if (port->type == 0) {
  37. u8 val = NVReadVgaCrtc(port->dev, 0, port->drive);
  38. if (state) val |= 0x20;
  39. else val &= 0xdf;
  40. NVWriteVgaCrtc(port->dev, 0, port->drive, val | 0x01);
  41. } else
  42. if (port->type == 4) {
  43. nv_mask(port->dev, port->drive, 0x2f, state ? 0x21 : 0x01);
  44. } else
  45. if (port->type == 5) {
  46. if (state) port->state |= 0x01;
  47. else port->state &= 0xfe;
  48. nv_wr32(port->dev, port->drive, 4 | port->state);
  49. }
  50. }
  51. static void
  52. i2c_drive_sda(void *data, int state)
  53. {
  54. struct nouveau_i2c_chan *port = data;
  55. if (port->type == 0) {
  56. u8 val = NVReadVgaCrtc(port->dev, 0, port->drive);
  57. if (state) val |= 0x10;
  58. else val &= 0xef;
  59. NVWriteVgaCrtc(port->dev, 0, port->drive, val | 0x01);
  60. } else
  61. if (port->type == 4) {
  62. nv_mask(port->dev, port->drive, 0x1f, state ? 0x11 : 0x01);
  63. } else
  64. if (port->type == 5) {
  65. if (state) port->state |= 0x02;
  66. else port->state &= 0xfd;
  67. nv_wr32(port->dev, port->drive, 4 | port->state);
  68. }
  69. }
  70. static int
  71. i2c_sense_scl(void *data)
  72. {
  73. struct nouveau_i2c_chan *port = data;
  74. struct drm_nouveau_private *dev_priv = port->dev->dev_private;
  75. if (port->type == 0) {
  76. return !!(NVReadVgaCrtc(port->dev, 0, port->sense) & 0x04);
  77. } else
  78. if (port->type == 4) {
  79. return !!(nv_rd32(port->dev, port->sense) & 0x00040000);
  80. } else
  81. if (port->type == 5) {
  82. if (dev_priv->card_type < NV_D0)
  83. return !!(nv_rd32(port->dev, port->sense) & 0x01);
  84. else
  85. return !!(nv_rd32(port->dev, port->sense) & 0x10);
  86. }
  87. return 0;
  88. }
  89. static int
  90. i2c_sense_sda(void *data)
  91. {
  92. struct nouveau_i2c_chan *port = data;
  93. struct drm_nouveau_private *dev_priv = port->dev->dev_private;
  94. if (port->type == 0) {
  95. return !!(NVReadVgaCrtc(port->dev, 0, port->sense) & 0x08);
  96. } else
  97. if (port->type == 4) {
  98. return !!(nv_rd32(port->dev, port->sense) & 0x00080000);
  99. } else
  100. if (port->type == 5) {
  101. if (dev_priv->card_type < NV_D0)
  102. return !!(nv_rd32(port->dev, port->sense) & 0x02);
  103. else
  104. return !!(nv_rd32(port->dev, port->sense) & 0x20);
  105. }
  106. return 0;
  107. }
  108. static void
  109. i2c_delay(struct nouveau_i2c_chan *port, u32 nsec)
  110. {
  111. udelay((nsec + 500) / 1000);
  112. }
  113. static bool
  114. i2c_raise_scl(struct nouveau_i2c_chan *port)
  115. {
  116. u32 timeout = T_TIMEOUT / T_RISEFALL;
  117. i2c_drive_scl(port, 1);
  118. do {
  119. i2c_delay(port, T_RISEFALL);
  120. } while (!i2c_sense_scl(port) && --timeout);
  121. return timeout != 0;
  122. }
  123. static int
  124. i2c_start(struct nouveau_i2c_chan *port)
  125. {
  126. int ret = 0;
  127. port->state = i2c_sense_scl(port);
  128. port->state |= i2c_sense_sda(port) << 1;
  129. if (port->state != 3) {
  130. i2c_drive_scl(port, 0);
  131. i2c_drive_sda(port, 1);
  132. if (!i2c_raise_scl(port))
  133. ret = -EBUSY;
  134. }
  135. i2c_drive_sda(port, 0);
  136. i2c_delay(port, T_HOLD);
  137. i2c_drive_scl(port, 0);
  138. i2c_delay(port, T_HOLD);
  139. return ret;
  140. }
  141. static void
  142. i2c_stop(struct nouveau_i2c_chan *port)
  143. {
  144. i2c_drive_scl(port, 0);
  145. i2c_drive_sda(port, 0);
  146. i2c_delay(port, T_RISEFALL);
  147. i2c_drive_scl(port, 1);
  148. i2c_delay(port, T_HOLD);
  149. i2c_drive_sda(port, 1);
  150. i2c_delay(port, T_HOLD);
  151. }
  152. static int
  153. i2c_bitw(struct nouveau_i2c_chan *port, int sda)
  154. {
  155. i2c_drive_sda(port, sda);
  156. i2c_delay(port, T_RISEFALL);
  157. if (!i2c_raise_scl(port))
  158. return -ETIMEDOUT;
  159. i2c_delay(port, T_HOLD);
  160. i2c_drive_scl(port, 0);
  161. i2c_delay(port, T_HOLD);
  162. return 0;
  163. }
  164. static int
  165. i2c_bitr(struct nouveau_i2c_chan *port)
  166. {
  167. int sda;
  168. i2c_drive_sda(port, 1);
  169. i2c_delay(port, T_RISEFALL);
  170. if (!i2c_raise_scl(port))
  171. return -ETIMEDOUT;
  172. i2c_delay(port, T_HOLD);
  173. sda = i2c_sense_sda(port);
  174. i2c_drive_scl(port, 0);
  175. i2c_delay(port, T_HOLD);
  176. return sda;
  177. }
  178. static int
  179. i2c_get_byte(struct nouveau_i2c_chan *port, u8 *byte, bool last)
  180. {
  181. int i, bit;
  182. *byte = 0;
  183. for (i = 7; i >= 0; i--) {
  184. bit = i2c_bitr(port);
  185. if (bit < 0)
  186. return bit;
  187. *byte |= bit << i;
  188. }
  189. return i2c_bitw(port, last ? 1 : 0);
  190. }
  191. static int
  192. i2c_put_byte(struct nouveau_i2c_chan *port, u8 byte)
  193. {
  194. int i, ret;
  195. for (i = 7; i >= 0; i--) {
  196. ret = i2c_bitw(port, !!(byte & (1 << i)));
  197. if (ret < 0)
  198. return ret;
  199. }
  200. ret = i2c_bitr(port);
  201. if (ret == 1) /* nack */
  202. ret = -EIO;
  203. return ret;
  204. }
  205. static int
  206. i2c_addr(struct nouveau_i2c_chan *port, struct i2c_msg *msg)
  207. {
  208. u32 addr = msg->addr << 1;
  209. if (msg->flags & I2C_M_RD)
  210. addr |= 1;
  211. return i2c_put_byte(port, addr);
  212. }
  213. static int
  214. i2c_bit_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
  215. {
  216. struct nouveau_i2c_chan *port = (struct nouveau_i2c_chan *)adap;
  217. struct i2c_msg *msg = msgs;
  218. int ret = 0, mcnt = num;
  219. while (!ret && mcnt--) {
  220. u8 remaining = msg->len;
  221. u8 *ptr = msg->buf;
  222. ret = i2c_start(port);
  223. if (ret == 0)
  224. ret = i2c_addr(port, msg);
  225. if (msg->flags & I2C_M_RD) {
  226. while (!ret && remaining--)
  227. ret = i2c_get_byte(port, ptr++, !remaining);
  228. } else {
  229. while (!ret && remaining--)
  230. ret = i2c_put_byte(port, *ptr++);
  231. }
  232. msg++;
  233. }
  234. i2c_stop(port);
  235. return (ret < 0) ? ret : num;
  236. }
  237. static u32
  238. i2c_bit_func(struct i2c_adapter *adap)
  239. {
  240. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  241. }
  242. const struct i2c_algorithm nouveau_i2c_bit_algo = {
  243. .master_xfer = i2c_bit_xfer,
  244. .functionality = i2c_bit_func
  245. };
  246. static const uint32_t nv50_i2c_port[] = {
  247. 0x00e138, 0x00e150, 0x00e168, 0x00e180,
  248. 0x00e254, 0x00e274, 0x00e764, 0x00e780,
  249. 0x00e79c, 0x00e7b8
  250. };
  251. static u8 *
  252. i2c_table(struct drm_device *dev, u8 *version)
  253. {
  254. u8 *dcb = dcb_table(dev), *i2c = NULL;
  255. if (dcb) {
  256. if (dcb[0] >= 0x15)
  257. i2c = ROMPTR(dev, dcb[2]);
  258. if (dcb[0] >= 0x30)
  259. i2c = ROMPTR(dev, dcb[4]);
  260. }
  261. /* early revisions had no version number, use dcb version */
  262. if (i2c) {
  263. *version = dcb[0];
  264. if (*version >= 0x30)
  265. *version = i2c[0];
  266. }
  267. return i2c;
  268. }
  269. int
  270. nouveau_i2c_init(struct drm_device *dev)
  271. {
  272. struct drm_nouveau_private *dev_priv = dev->dev_private;
  273. struct nvbios *bios = &dev_priv->vbios;
  274. struct nouveau_i2c_chan *port;
  275. u8 version = 0x00, entries, recordlen;
  276. u8 *i2c, *entry, legacy[2][4] = {};
  277. int ret, i;
  278. INIT_LIST_HEAD(&dev_priv->i2c_ports);
  279. i2c = i2c_table(dev, &version);
  280. if (!i2c) {
  281. u8 *bmp = &bios->data[bios->offset];
  282. if (bios->type != NVBIOS_BMP)
  283. return -ENODEV;
  284. legacy[0][0] = NV_CIO_CRE_DDC_WR__INDEX;
  285. legacy[0][1] = NV_CIO_CRE_DDC_STATUS__INDEX;
  286. legacy[1][0] = NV_CIO_CRE_DDC0_WR__INDEX;
  287. legacy[1][1] = NV_CIO_CRE_DDC0_STATUS__INDEX;
  288. /* BMP (from v4.0) has i2c info in the structure, it's in a
  289. * fixed location on earlier VBIOS
  290. */
  291. if (bmp[5] < 4)
  292. i2c = &bios->data[0x48];
  293. else
  294. i2c = &bmp[0x36];
  295. if (i2c[4]) legacy[0][0] = i2c[4];
  296. if (i2c[5]) legacy[0][1] = i2c[5];
  297. if (i2c[6]) legacy[1][0] = i2c[6];
  298. if (i2c[7]) legacy[1][1] = i2c[7];
  299. }
  300. if (version >= 0x30) {
  301. entry = i2c[1] + i2c;
  302. entries = i2c[2];
  303. recordlen = i2c[3];
  304. } else
  305. if (version) {
  306. entry = i2c;
  307. entries = 16;
  308. recordlen = 4;
  309. } else {
  310. entry = legacy[0];
  311. entries = 2;
  312. recordlen = 4;
  313. }
  314. for (i = 0; i < entries; i++, entry += recordlen) {
  315. port = kzalloc(sizeof(*port), GFP_KERNEL);
  316. if (port == NULL) {
  317. nouveau_i2c_fini(dev);
  318. return -ENOMEM;
  319. }
  320. port->type = entry[3];
  321. if (version < 0x30) {
  322. port->type &= 0x07;
  323. if (port->type == 0x07)
  324. port->type = 0xff;
  325. }
  326. if (port->type == 0xff) {
  327. kfree(port);
  328. continue;
  329. }
  330. switch (port->type) {
  331. case 0: /* NV04:NV50 */
  332. port->drive = entry[0];
  333. port->sense = entry[1];
  334. port->adapter.algo = &nouveau_i2c_bit_algo;
  335. break;
  336. case 4: /* NV4E */
  337. port->drive = 0x600800 + entry[1];
  338. port->sense = port->drive;
  339. port->adapter.algo = &nouveau_i2c_bit_algo;
  340. break;
  341. case 5: /* NV50- */
  342. port->drive = entry[0] & 0x0f;
  343. if (dev_priv->card_type < NV_D0) {
  344. if (port->drive >= ARRAY_SIZE(nv50_i2c_port))
  345. break;
  346. port->drive = nv50_i2c_port[port->drive];
  347. port->sense = port->drive;
  348. } else {
  349. port->drive = 0x00d014 + (port->drive * 0x20);
  350. port->sense = port->drive;
  351. }
  352. port->adapter.algo = &nouveau_i2c_bit_algo;
  353. break;
  354. case 6: /* NV50- DP AUX */
  355. port->drive = entry[0];
  356. port->sense = port->drive;
  357. port->adapter.algo = &nouveau_dp_i2c_algo;
  358. break;
  359. default:
  360. break;
  361. }
  362. if (!port->adapter.algo) {
  363. NV_ERROR(dev, "I2C%d: type %d index %x/%x unknown\n",
  364. i, port->type, port->drive, port->sense);
  365. kfree(port);
  366. continue;
  367. }
  368. snprintf(port->adapter.name, sizeof(port->adapter.name),
  369. "nouveau-%s-%d", pci_name(dev->pdev), i);
  370. port->adapter.owner = THIS_MODULE;
  371. port->adapter.dev.parent = &dev->pdev->dev;
  372. port->dev = dev;
  373. port->index = i;
  374. port->dcb = ROM32(entry[0]);
  375. i2c_set_adapdata(&port->adapter, i2c);
  376. ret = i2c_add_adapter(&port->adapter);
  377. if (ret) {
  378. NV_ERROR(dev, "I2C%d: failed register: %d\n", i, ret);
  379. kfree(port);
  380. continue;
  381. }
  382. list_add_tail(&port->head, &dev_priv->i2c_ports);
  383. }
  384. return 0;
  385. }
  386. void
  387. nouveau_i2c_fini(struct drm_device *dev)
  388. {
  389. struct drm_nouveau_private *dev_priv = dev->dev_private;
  390. struct nouveau_i2c_chan *port, *tmp;
  391. list_for_each_entry_safe(port, tmp, &dev_priv->i2c_ports, head) {
  392. i2c_del_adapter(&port->adapter);
  393. kfree(port);
  394. }
  395. }
  396. struct nouveau_i2c_chan *
  397. nouveau_i2c_find(struct drm_device *dev, u8 index)
  398. {
  399. struct drm_nouveau_private *dev_priv = dev->dev_private;
  400. struct nouveau_i2c_chan *port;
  401. if (index == NV_I2C_DEFAULT(0) ||
  402. index == NV_I2C_DEFAULT(1)) {
  403. u8 version, *i2c = i2c_table(dev, &version);
  404. if (i2c && version >= 0x30) {
  405. if (index == NV_I2C_DEFAULT(0))
  406. index = (i2c[4] & 0x0f);
  407. else
  408. index = (i2c[4] & 0xf0) >> 4;
  409. } else {
  410. index = 2;
  411. }
  412. }
  413. list_for_each_entry(port, &dev_priv->i2c_ports, head) {
  414. if (port->index == index)
  415. break;
  416. }
  417. if (&port->head == &dev_priv->i2c_ports)
  418. return NULL;
  419. if (dev_priv->card_type >= NV_50 && (port->dcb & 0x00000100)) {
  420. u32 reg = 0x00e500, val;
  421. if (port->type == 6) {
  422. reg += port->drive * 0x50;
  423. val = 0x2002;
  424. } else {
  425. reg += ((port->dcb & 0x1e00) >> 9) * 0x50;
  426. val = 0xe001;
  427. }
  428. /* nfi, but neither auxch or i2c work if it's 1 */
  429. nv_mask(dev, reg + 0x0c, 0x00000001, 0x00000000);
  430. /* nfi, but switches auxch vs normal i2c */
  431. nv_mask(dev, reg + 0x00, 0x0000f003, val);
  432. }
  433. return port;
  434. }
  435. bool
  436. nouveau_probe_i2c_addr(struct nouveau_i2c_chan *i2c, int addr)
  437. {
  438. uint8_t buf[] = { 0 };
  439. struct i2c_msg msgs[] = {
  440. {
  441. .addr = addr,
  442. .flags = 0,
  443. .len = 1,
  444. .buf = buf,
  445. },
  446. {
  447. .addr = addr,
  448. .flags = I2C_M_RD,
  449. .len = 1,
  450. .buf = buf,
  451. }
  452. };
  453. return i2c_transfer(&i2c->adapter, msgs, 2) == 2;
  454. }
  455. int
  456. nouveau_i2c_identify(struct drm_device *dev, const char *what,
  457. struct i2c_board_info *info,
  458. bool (*match)(struct nouveau_i2c_chan *,
  459. struct i2c_board_info *),
  460. int index)
  461. {
  462. struct nouveau_i2c_chan *i2c = nouveau_i2c_find(dev, index);
  463. int i;
  464. if (!i2c) {
  465. NV_DEBUG(dev, "No bus when probing %s on %d\n", what, index);
  466. return -ENODEV;
  467. }
  468. NV_DEBUG(dev, "Probing %ss on I2C bus: %d\n", what, i2c->index);
  469. for (i = 0; info[i].addr; i++) {
  470. if (nouveau_probe_i2c_addr(i2c, info[i].addr) &&
  471. (!match || match(i2c, &info[i]))) {
  472. NV_INFO(dev, "Detected %s: %s\n", what, info[i].type);
  473. return i;
  474. }
  475. }
  476. NV_DEBUG(dev, "No devices found.\n");
  477. return -ENODEV;
  478. }