nouveau_i2c.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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. static void
  30. nv04_i2c_setscl(void *data, int state)
  31. {
  32. struct nouveau_i2c_chan *i2c = data;
  33. struct drm_device *dev = i2c->dev;
  34. uint8_t val;
  35. val = (NVReadVgaCrtc(dev, 0, i2c->wr) & 0xd0) | (state ? 0x20 : 0);
  36. NVWriteVgaCrtc(dev, 0, i2c->wr, val | 0x01);
  37. }
  38. static void
  39. nv04_i2c_setsda(void *data, int state)
  40. {
  41. struct nouveau_i2c_chan *i2c = data;
  42. struct drm_device *dev = i2c->dev;
  43. uint8_t val;
  44. val = (NVReadVgaCrtc(dev, 0, i2c->wr) & 0xe0) | (state ? 0x10 : 0);
  45. NVWriteVgaCrtc(dev, 0, i2c->wr, val | 0x01);
  46. }
  47. static int
  48. nv04_i2c_getscl(void *data)
  49. {
  50. struct nouveau_i2c_chan *i2c = data;
  51. struct drm_device *dev = i2c->dev;
  52. return !!(NVReadVgaCrtc(dev, 0, i2c->rd) & 4);
  53. }
  54. static int
  55. nv04_i2c_getsda(void *data)
  56. {
  57. struct nouveau_i2c_chan *i2c = data;
  58. struct drm_device *dev = i2c->dev;
  59. return !!(NVReadVgaCrtc(dev, 0, i2c->rd) & 8);
  60. }
  61. static void
  62. nv4e_i2c_setscl(void *data, int state)
  63. {
  64. struct nouveau_i2c_chan *i2c = data;
  65. struct drm_device *dev = i2c->dev;
  66. uint8_t val;
  67. val = (nv_rd32(dev, i2c->wr) & 0xd0) | (state ? 0x20 : 0);
  68. nv_wr32(dev, i2c->wr, val | 0x01);
  69. }
  70. static void
  71. nv4e_i2c_setsda(void *data, int state)
  72. {
  73. struct nouveau_i2c_chan *i2c = data;
  74. struct drm_device *dev = i2c->dev;
  75. uint8_t val;
  76. val = (nv_rd32(dev, i2c->wr) & 0xe0) | (state ? 0x10 : 0);
  77. nv_wr32(dev, i2c->wr, val | 0x01);
  78. }
  79. static int
  80. nv4e_i2c_getscl(void *data)
  81. {
  82. struct nouveau_i2c_chan *i2c = data;
  83. struct drm_device *dev = i2c->dev;
  84. return !!((nv_rd32(dev, i2c->rd) >> 16) & 4);
  85. }
  86. static int
  87. nv4e_i2c_getsda(void *data)
  88. {
  89. struct nouveau_i2c_chan *i2c = data;
  90. struct drm_device *dev = i2c->dev;
  91. return !!((nv_rd32(dev, i2c->rd) >> 16) & 8);
  92. }
  93. static int
  94. nv50_i2c_getscl(void *data)
  95. {
  96. struct nouveau_i2c_chan *i2c = data;
  97. struct drm_device *dev = i2c->dev;
  98. return !!(nv_rd32(dev, i2c->rd) & 1);
  99. }
  100. static int
  101. nv50_i2c_getsda(void *data)
  102. {
  103. struct nouveau_i2c_chan *i2c = data;
  104. struct drm_device *dev = i2c->dev;
  105. return !!(nv_rd32(dev, i2c->rd) & 2);
  106. }
  107. static void
  108. nv50_i2c_setscl(void *data, int state)
  109. {
  110. struct nouveau_i2c_chan *i2c = data;
  111. nv_wr32(i2c->dev, i2c->wr, 4 | (i2c->data ? 2 : 0) | (state ? 1 : 0));
  112. }
  113. static void
  114. nv50_i2c_setsda(void *data, int state)
  115. {
  116. struct nouveau_i2c_chan *i2c = data;
  117. nv_mask(i2c->dev, i2c->wr, 0x00000006, 4 | (state ? 2 : 0));
  118. i2c->data = state;
  119. }
  120. static int
  121. nvd0_i2c_getscl(void *data)
  122. {
  123. struct nouveau_i2c_chan *i2c = data;
  124. return !!(nv_rd32(i2c->dev, i2c->rd) & 0x10);
  125. }
  126. static int
  127. nvd0_i2c_getsda(void *data)
  128. {
  129. struct nouveau_i2c_chan *i2c = data;
  130. return !!(nv_rd32(i2c->dev, i2c->rd) & 0x20);
  131. }
  132. static const uint32_t nv50_i2c_port[] = {
  133. 0x00e138, 0x00e150, 0x00e168, 0x00e180,
  134. 0x00e254, 0x00e274, 0x00e764, 0x00e780,
  135. 0x00e79c, 0x00e7b8
  136. };
  137. static u8 *
  138. i2c_table(struct drm_device *dev, u8 *version)
  139. {
  140. u8 *dcb = dcb_table(dev), *i2c = NULL;
  141. if (dcb) {
  142. if (dcb[0] >= 0x15)
  143. i2c = ROMPTR(dev, dcb[2]);
  144. if (dcb[0] >= 0x30)
  145. i2c = ROMPTR(dev, dcb[4]);
  146. }
  147. /* early revisions had no version number, use dcb version */
  148. if (i2c) {
  149. *version = dcb[0];
  150. if (*version >= 0x30)
  151. *version = i2c[0];
  152. }
  153. return i2c;
  154. }
  155. int
  156. nouveau_i2c_init(struct drm_device *dev)
  157. {
  158. struct drm_nouveau_private *dev_priv = dev->dev_private;
  159. struct nvbios *bios = &dev_priv->vbios;
  160. struct nouveau_i2c_chan *port;
  161. u8 *i2c, *entry, legacy[2][4] = {};
  162. u8 version, entries, recordlen;
  163. int ret, i;
  164. INIT_LIST_HEAD(&dev_priv->i2c_ports);
  165. i2c = i2c_table(dev, &version);
  166. if (!i2c) {
  167. u8 *bmp = &bios->data[bios->offset];
  168. if (bios->type != NVBIOS_BMP)
  169. return -ENODEV;
  170. legacy[0][0] = NV_CIO_CRE_DDC_WR__INDEX;
  171. legacy[0][1] = NV_CIO_CRE_DDC_STATUS__INDEX;
  172. legacy[1][0] = NV_CIO_CRE_DDC0_WR__INDEX;
  173. legacy[1][1] = NV_CIO_CRE_DDC0_STATUS__INDEX;
  174. /* BMP (from v4.0) has i2c info in the structure, it's in a
  175. * fixed location on earlier VBIOS
  176. */
  177. if (bmp[5] < 4)
  178. i2c = &bios->data[0x48];
  179. else
  180. i2c = &bmp[0x36];
  181. if (i2c[4]) legacy[0][0] = i2c[4];
  182. if (i2c[5]) legacy[0][1] = i2c[5];
  183. if (i2c[6]) legacy[1][0] = i2c[6];
  184. if (i2c[7]) legacy[1][1] = i2c[7];
  185. }
  186. if (i2c && version >= 0x30) {
  187. entry = i2c[1] + i2c;
  188. entries = i2c[2];
  189. recordlen = i2c[3];
  190. } else
  191. if (i2c) {
  192. entry = i2c;
  193. entries = 16;
  194. recordlen = 4;
  195. } else {
  196. entry = legacy[0];
  197. entries = 2;
  198. recordlen = 4;
  199. }
  200. for (i = 0; i < entries; i++, entry += recordlen) {
  201. port = kzalloc(sizeof(*port), GFP_KERNEL);
  202. if (port == NULL) {
  203. nouveau_i2c_fini(dev);
  204. return -ENOMEM;
  205. }
  206. port->type = entry[3];
  207. if (version < 0x30) {
  208. port->type &= 0x07;
  209. if (port->type == 0x07)
  210. port->type = 0xff;
  211. }
  212. if (port->type == 0xff) {
  213. kfree(port);
  214. continue;
  215. }
  216. switch (port->type) {
  217. case 0: /* NV04:NV50 */
  218. port->wr = entry[0];
  219. port->rd = entry[1];
  220. port->bit.setsda = nv04_i2c_setsda;
  221. port->bit.setscl = nv04_i2c_setscl;
  222. port->bit.getsda = nv04_i2c_getsda;
  223. port->bit.getscl = nv04_i2c_getscl;
  224. break;
  225. case 4: /* NV4E */
  226. port->wr = 0x600800 + entry[1];
  227. port->rd = port->wr;
  228. port->bit.setsda = nv4e_i2c_setsda;
  229. port->bit.setscl = nv4e_i2c_setscl;
  230. port->bit.getsda = nv4e_i2c_getsda;
  231. port->bit.getscl = nv4e_i2c_getscl;
  232. break;
  233. case 5: /* NV50- */
  234. port->wr = entry[0] & 0x0f;
  235. if (dev_priv->card_type < NV_D0) {
  236. if (port->wr >= ARRAY_SIZE(nv50_i2c_port))
  237. break;
  238. port->wr = nv50_i2c_port[port->wr];
  239. port->rd = port->wr;
  240. port->bit.getsda = nv50_i2c_getsda;
  241. port->bit.getscl = nv50_i2c_getscl;
  242. } else {
  243. port->wr = 0x00d014 + (port->wr * 0x20);
  244. port->rd = port->wr;
  245. port->bit.getsda = nvd0_i2c_getsda;
  246. port->bit.getscl = nvd0_i2c_getscl;
  247. }
  248. port->bit.setsda = nv50_i2c_setsda;
  249. port->bit.setscl = nv50_i2c_setscl;
  250. break;
  251. case 6: /* NV50- DP AUX */
  252. port->wr = entry[0];
  253. port->rd = port->wr;
  254. port->adapter.algo = &nouveau_dp_i2c_algo;
  255. break;
  256. default:
  257. break;
  258. }
  259. if (!port->adapter.algo && !port->wr) {
  260. NV_ERROR(dev, "I2C%d: type %d index %x/%x unknown\n",
  261. i, port->type, port->wr, port->rd);
  262. kfree(port);
  263. continue;
  264. }
  265. snprintf(port->adapter.name, sizeof(port->adapter.name),
  266. "nouveau-%s-%d", pci_name(dev->pdev), i);
  267. port->adapter.owner = THIS_MODULE;
  268. port->adapter.dev.parent = &dev->pdev->dev;
  269. port->dev = dev;
  270. port->index = i;
  271. port->dcb = ROM32(entry[0]);
  272. i2c_set_adapdata(&port->adapter, i2c);
  273. if (port->adapter.algo != &nouveau_dp_i2c_algo) {
  274. port->adapter.algo_data = &port->bit;
  275. port->bit.udelay = 40;
  276. port->bit.timeout = usecs_to_jiffies(5000);
  277. port->bit.data = port;
  278. ret = i2c_bit_add_bus(&port->adapter);
  279. } else {
  280. port->adapter.algo = &nouveau_dp_i2c_algo;
  281. ret = i2c_add_adapter(&port->adapter);
  282. }
  283. if (ret) {
  284. NV_ERROR(dev, "I2C%d: failed register: %d\n", i, ret);
  285. kfree(port);
  286. continue;
  287. }
  288. list_add_tail(&port->head, &dev_priv->i2c_ports);
  289. }
  290. return 0;
  291. }
  292. void
  293. nouveau_i2c_fini(struct drm_device *dev)
  294. {
  295. struct drm_nouveau_private *dev_priv = dev->dev_private;
  296. struct nouveau_i2c_chan *port, *tmp;
  297. list_for_each_entry_safe(port, tmp, &dev_priv->i2c_ports, head) {
  298. i2c_del_adapter(&port->adapter);
  299. kfree(port);
  300. }
  301. }
  302. struct nouveau_i2c_chan *
  303. nouveau_i2c_find(struct drm_device *dev, u8 index)
  304. {
  305. struct drm_nouveau_private *dev_priv = dev->dev_private;
  306. struct nouveau_i2c_chan *port;
  307. if (index == NV_I2C_DEFAULT(0) ||
  308. index == NV_I2C_DEFAULT(1)) {
  309. u8 version, *i2c = i2c_table(dev, &version);
  310. if (i2c && version >= 0x30) {
  311. if (index == NV_I2C_DEFAULT(0))
  312. index = (i2c[4] & 0x0f);
  313. else
  314. index = (i2c[4] & 0xf0) >> 4;
  315. } else {
  316. index = 2;
  317. }
  318. }
  319. list_for_each_entry(port, &dev_priv->i2c_ports, head) {
  320. if (port->index == index)
  321. break;
  322. }
  323. if (&port->head == &dev_priv->i2c_ports)
  324. return NULL;
  325. if (dev_priv->card_type >= NV_50 && (port->dcb & 0x00000100)) {
  326. u32 reg = 0x00e500, val;
  327. if (port->type == 6) {
  328. reg += port->rd * 0x50;
  329. val = 0x2002;
  330. } else {
  331. reg += ((port->dcb & 0x1e00) >> 9) * 0x50;
  332. val = 0xe001;
  333. }
  334. /* nfi, but neither auxch or i2c work if it's 1 */
  335. nv_mask(dev, reg + 0x0c, 0x00000001, 0x00000000);
  336. /* nfi, but switches auxch vs normal i2c */
  337. nv_mask(dev, reg + 0x00, 0x0000f003, val);
  338. }
  339. return port;
  340. }
  341. bool
  342. nouveau_probe_i2c_addr(struct nouveau_i2c_chan *i2c, int addr)
  343. {
  344. uint8_t buf[] = { 0 };
  345. struct i2c_msg msgs[] = {
  346. {
  347. .addr = addr,
  348. .flags = 0,
  349. .len = 1,
  350. .buf = buf,
  351. },
  352. {
  353. .addr = addr,
  354. .flags = I2C_M_RD,
  355. .len = 1,
  356. .buf = buf,
  357. }
  358. };
  359. return i2c_transfer(&i2c->adapter, msgs, 2) == 2;
  360. }
  361. int
  362. nouveau_i2c_identify(struct drm_device *dev, const char *what,
  363. struct i2c_board_info *info,
  364. bool (*match)(struct nouveau_i2c_chan *,
  365. struct i2c_board_info *),
  366. int index)
  367. {
  368. struct nouveau_i2c_chan *i2c = nouveau_i2c_find(dev, index);
  369. int i;
  370. NV_DEBUG(dev, "Probing %ss on I2C bus: %d\n", what, index);
  371. for (i = 0; i2c && info[i].addr; i++) {
  372. if (nouveau_probe_i2c_addr(i2c, info[i].addr) &&
  373. (!match || match(i2c, &info[i]))) {
  374. NV_INFO(dev, "Detected %s: %s\n", what, info[i].type);
  375. return i;
  376. }
  377. }
  378. NV_DEBUG(dev, "No devices found.\n");
  379. return -ENODEV;
  380. }