intel_gmbus.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. /*
  2. * Copyright (c) 2006 Dave Airlie <airlied@linux.ie>
  3. * Copyright © 2006-2008,2010 Intel Corporation
  4. * Jesse Barnes <jesse.barnes@intel.com>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the "Software"),
  8. * to deal in the Software without restriction, including without limitation
  9. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the
  11. * Software is furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice (including the next
  14. * paragraph) shall be included in all copies or substantial portions of the
  15. * Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  20. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. *
  25. * Authors:
  26. * Eric Anholt <eric@anholt.net>
  27. * Chris Wilson <chris@chris-wilson.co.uk>
  28. */
  29. #include <linux/module.h>
  30. #include <linux/i2c.h>
  31. #include <linux/i2c-algo-bit.h>
  32. #include "drmP.h"
  33. #include "drm.h"
  34. #include "psb_intel_drv.h"
  35. #include "gma_drm.h"
  36. #include "psb_drv.h"
  37. #include "psb_intel_reg.h"
  38. #define _wait_for(COND, MS, W) ({ \
  39. unsigned long timeout__ = jiffies + msecs_to_jiffies(MS); \
  40. int ret__ = 0; \
  41. while (! (COND)) { \
  42. if (time_after(jiffies, timeout__)) { \
  43. ret__ = -ETIMEDOUT; \
  44. break; \
  45. } \
  46. if (W && !(in_atomic() || in_dbg_master())) msleep(W); \
  47. } \
  48. ret__; \
  49. })
  50. #define wait_for(COND, MS) _wait_for(COND, MS, 1)
  51. #define wait_for_atomic(COND, MS) _wait_for(COND, MS, 0)
  52. /* Intel GPIO access functions */
  53. #define I2C_RISEFALL_TIME 20
  54. static inline struct intel_gmbus *
  55. to_intel_gmbus(struct i2c_adapter *i2c)
  56. {
  57. return container_of(i2c, struct intel_gmbus, adapter);
  58. }
  59. struct intel_gpio {
  60. struct i2c_adapter adapter;
  61. struct i2c_algo_bit_data algo;
  62. struct drm_psb_private *dev_priv;
  63. u32 reg;
  64. };
  65. void
  66. gma_intel_i2c_reset(struct drm_device *dev)
  67. {
  68. REG_WRITE(GMBUS0, 0);
  69. }
  70. static void intel_i2c_quirk_set(struct drm_psb_private *dev_priv, bool enable)
  71. {
  72. /* When using bit bashing for I2C, this bit needs to be set to 1 */
  73. /* FIXME: We are never Pineview, right?
  74. u32 val;
  75. if (!IS_PINEVIEW(dev_priv->dev))
  76. return;
  77. val = REG_READ(DSPCLK_GATE_D);
  78. if (enable)
  79. val |= DPCUNIT_CLOCK_GATE_DISABLE;
  80. else
  81. val &= ~DPCUNIT_CLOCK_GATE_DISABLE;
  82. REG_WRITE(DSPCLK_GATE_D, val);
  83. return;
  84. */
  85. }
  86. static u32 get_reserved(struct intel_gpio *gpio)
  87. {
  88. struct drm_psb_private *dev_priv = gpio->dev_priv;
  89. struct drm_device *dev = dev_priv->dev;
  90. u32 reserved = 0;
  91. /* On most chips, these bits must be preserved in software. */
  92. reserved = REG_READ(gpio->reg) &
  93. (GPIO_DATA_PULLUP_DISABLE |
  94. GPIO_CLOCK_PULLUP_DISABLE);
  95. return reserved;
  96. }
  97. static int get_clock(void *data)
  98. {
  99. struct intel_gpio *gpio = data;
  100. struct drm_psb_private *dev_priv = gpio->dev_priv;
  101. struct drm_device *dev = dev_priv->dev;
  102. u32 reserved = get_reserved(gpio);
  103. REG_WRITE(gpio->reg, reserved | GPIO_CLOCK_DIR_MASK);
  104. REG_WRITE(gpio->reg, reserved);
  105. return (REG_READ(gpio->reg) & GPIO_CLOCK_VAL_IN) != 0;
  106. }
  107. static int get_data(void *data)
  108. {
  109. struct intel_gpio *gpio = data;
  110. struct drm_psb_private *dev_priv = gpio->dev_priv;
  111. struct drm_device *dev = dev_priv->dev;
  112. u32 reserved = get_reserved(gpio);
  113. REG_WRITE(gpio->reg, reserved | GPIO_DATA_DIR_MASK);
  114. REG_WRITE(gpio->reg, reserved);
  115. return (REG_READ(gpio->reg) & GPIO_DATA_VAL_IN) != 0;
  116. }
  117. static void set_clock(void *data, int state_high)
  118. {
  119. struct intel_gpio *gpio = data;
  120. struct drm_psb_private *dev_priv = gpio->dev_priv;
  121. struct drm_device *dev = dev_priv->dev;
  122. u32 reserved = get_reserved(gpio);
  123. u32 clock_bits;
  124. if (state_high)
  125. clock_bits = GPIO_CLOCK_DIR_IN | GPIO_CLOCK_DIR_MASK;
  126. else
  127. clock_bits = GPIO_CLOCK_DIR_OUT | GPIO_CLOCK_DIR_MASK |
  128. GPIO_CLOCK_VAL_MASK;
  129. REG_WRITE(gpio->reg, reserved | clock_bits);
  130. REG_READ(gpio->reg); /* Posting */
  131. }
  132. static void set_data(void *data, int state_high)
  133. {
  134. struct intel_gpio *gpio = data;
  135. struct drm_psb_private *dev_priv = gpio->dev_priv;
  136. struct drm_device *dev = dev_priv->dev;
  137. u32 reserved = get_reserved(gpio);
  138. u32 data_bits;
  139. if (state_high)
  140. data_bits = GPIO_DATA_DIR_IN | GPIO_DATA_DIR_MASK;
  141. else
  142. data_bits = GPIO_DATA_DIR_OUT | GPIO_DATA_DIR_MASK |
  143. GPIO_DATA_VAL_MASK;
  144. REG_WRITE(gpio->reg, reserved | data_bits);
  145. REG_READ(gpio->reg);
  146. }
  147. static struct i2c_adapter *
  148. intel_gpio_create(struct drm_psb_private *dev_priv, u32 pin)
  149. {
  150. static const int map_pin_to_reg[] = {
  151. 0,
  152. GPIOB,
  153. GPIOA,
  154. GPIOC,
  155. GPIOD,
  156. GPIOE,
  157. 0,
  158. GPIOF,
  159. };
  160. struct intel_gpio *gpio;
  161. if (pin >= ARRAY_SIZE(map_pin_to_reg) || !map_pin_to_reg[pin])
  162. return NULL;
  163. gpio = kzalloc(sizeof(struct intel_gpio), GFP_KERNEL);
  164. if (gpio == NULL)
  165. return NULL;
  166. gpio->reg = map_pin_to_reg[pin];
  167. gpio->dev_priv = dev_priv;
  168. snprintf(gpio->adapter.name, sizeof(gpio->adapter.name),
  169. "gma500 GPIO%c", "?BACDE?F"[pin]);
  170. gpio->adapter.owner = THIS_MODULE;
  171. gpio->adapter.algo_data = &gpio->algo;
  172. gpio->adapter.dev.parent = &dev_priv->dev->pdev->dev;
  173. gpio->algo.setsda = set_data;
  174. gpio->algo.setscl = set_clock;
  175. gpio->algo.getsda = get_data;
  176. gpio->algo.getscl = get_clock;
  177. gpio->algo.udelay = I2C_RISEFALL_TIME;
  178. gpio->algo.timeout = usecs_to_jiffies(2200);
  179. gpio->algo.data = gpio;
  180. if (i2c_bit_add_bus(&gpio->adapter))
  181. goto out_free;
  182. return &gpio->adapter;
  183. out_free:
  184. kfree(gpio);
  185. return NULL;
  186. }
  187. static int
  188. intel_i2c_quirk_xfer(struct drm_psb_private *dev_priv,
  189. struct i2c_adapter *adapter,
  190. struct i2c_msg *msgs,
  191. int num)
  192. {
  193. struct intel_gpio *gpio = container_of(adapter,
  194. struct intel_gpio,
  195. adapter);
  196. int ret;
  197. gma_intel_i2c_reset(dev_priv->dev);
  198. intel_i2c_quirk_set(dev_priv, true);
  199. set_data(gpio, 1);
  200. set_clock(gpio, 1);
  201. udelay(I2C_RISEFALL_TIME);
  202. ret = adapter->algo->master_xfer(adapter, msgs, num);
  203. set_data(gpio, 1);
  204. set_clock(gpio, 1);
  205. intel_i2c_quirk_set(dev_priv, false);
  206. return ret;
  207. }
  208. static int
  209. gmbus_xfer(struct i2c_adapter *adapter,
  210. struct i2c_msg *msgs,
  211. int num)
  212. {
  213. struct intel_gmbus *bus = container_of(adapter,
  214. struct intel_gmbus,
  215. adapter);
  216. struct drm_psb_private *dev_priv = adapter->algo_data;
  217. struct drm_device *dev = dev_priv->dev;
  218. int i, reg_offset;
  219. if (bus->force_bit)
  220. return intel_i2c_quirk_xfer(dev_priv,
  221. bus->force_bit, msgs, num);
  222. reg_offset = 0;
  223. REG_WRITE(GMBUS0 + reg_offset, bus->reg0);
  224. for (i = 0; i < num; i++) {
  225. u16 len = msgs[i].len;
  226. u8 *buf = msgs[i].buf;
  227. if (msgs[i].flags & I2C_M_RD) {
  228. REG_WRITE(GMBUS1 + reg_offset,
  229. GMBUS_CYCLE_WAIT | (i + 1 == num ? GMBUS_CYCLE_STOP : 0) |
  230. (len << GMBUS_BYTE_COUNT_SHIFT) |
  231. (msgs[i].addr << GMBUS_SLAVE_ADDR_SHIFT) |
  232. GMBUS_SLAVE_READ | GMBUS_SW_RDY);
  233. REG_READ(GMBUS2+reg_offset);
  234. do {
  235. u32 val, loop = 0;
  236. if (wait_for(REG_READ(GMBUS2 + reg_offset) & (GMBUS_SATOER | GMBUS_HW_RDY), 50))
  237. goto timeout;
  238. if (REG_READ(GMBUS2 + reg_offset) & GMBUS_SATOER)
  239. goto clear_err;
  240. val = REG_READ(GMBUS3 + reg_offset);
  241. do {
  242. *buf++ = val & 0xff;
  243. val >>= 8;
  244. } while (--len && ++loop < 4);
  245. } while (len);
  246. } else {
  247. u32 val, loop;
  248. val = loop = 0;
  249. do {
  250. val |= *buf++ << (8 * loop);
  251. } while (--len && ++loop < 4);
  252. REG_WRITE(GMBUS3 + reg_offset, val);
  253. REG_WRITE(GMBUS1 + reg_offset,
  254. (i + 1 == num ? GMBUS_CYCLE_STOP : GMBUS_CYCLE_WAIT) |
  255. (msgs[i].len << GMBUS_BYTE_COUNT_SHIFT) |
  256. (msgs[i].addr << GMBUS_SLAVE_ADDR_SHIFT) |
  257. GMBUS_SLAVE_WRITE | GMBUS_SW_RDY);
  258. REG_READ(GMBUS2+reg_offset);
  259. while (len) {
  260. if (wait_for(REG_READ(GMBUS2 + reg_offset) & (GMBUS_SATOER | GMBUS_HW_RDY), 50))
  261. goto timeout;
  262. if (REG_READ(GMBUS2 + reg_offset) & GMBUS_SATOER)
  263. goto clear_err;
  264. val = loop = 0;
  265. do {
  266. val |= *buf++ << (8 * loop);
  267. } while (--len && ++loop < 4);
  268. REG_WRITE(GMBUS3 + reg_offset, val);
  269. REG_READ(GMBUS2+reg_offset);
  270. }
  271. }
  272. if (i + 1 < num && wait_for(REG_READ(GMBUS2 + reg_offset) & (GMBUS_SATOER | GMBUS_HW_WAIT_PHASE), 50))
  273. goto timeout;
  274. if (REG_READ(GMBUS2 + reg_offset) & GMBUS_SATOER)
  275. goto clear_err;
  276. }
  277. goto done;
  278. clear_err:
  279. /* Toggle the Software Clear Interrupt bit. This has the effect
  280. * of resetting the GMBUS controller and so clearing the
  281. * BUS_ERROR raised by the slave's NAK.
  282. */
  283. REG_WRITE(GMBUS1 + reg_offset, GMBUS_SW_CLR_INT);
  284. REG_WRITE(GMBUS1 + reg_offset, 0);
  285. done:
  286. /* Mark the GMBUS interface as disabled. We will re-enable it at the
  287. * start of the next xfer, till then let it sleep.
  288. */
  289. REG_WRITE(GMBUS0 + reg_offset, 0);
  290. return i;
  291. timeout:
  292. DRM_INFO("GMBUS timed out, falling back to bit banging on pin %d [%s]\n",
  293. bus->reg0 & 0xff, bus->adapter.name);
  294. REG_WRITE(GMBUS0 + reg_offset, 0);
  295. /* Hardware may not support GMBUS over these pins? Try GPIO bitbanging instead. */
  296. bus->force_bit = intel_gpio_create(dev_priv, bus->reg0 & 0xff);
  297. if (!bus->force_bit)
  298. return -ENOMEM;
  299. return intel_i2c_quirk_xfer(dev_priv, bus->force_bit, msgs, num);
  300. }
  301. static u32 gmbus_func(struct i2c_adapter *adapter)
  302. {
  303. struct intel_gmbus *bus = container_of(adapter,
  304. struct intel_gmbus,
  305. adapter);
  306. if (bus->force_bit)
  307. bus->force_bit->algo->functionality(bus->force_bit);
  308. return (I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
  309. /* I2C_FUNC_10BIT_ADDR | */
  310. I2C_FUNC_SMBUS_READ_BLOCK_DATA |
  311. I2C_FUNC_SMBUS_BLOCK_PROC_CALL);
  312. }
  313. static const struct i2c_algorithm gmbus_algorithm = {
  314. .master_xfer = gmbus_xfer,
  315. .functionality = gmbus_func
  316. };
  317. /**
  318. * intel_gmbus_setup - instantiate all Intel i2c GMBuses
  319. * @dev: DRM device
  320. */
  321. int gma_intel_setup_gmbus(struct drm_device *dev)
  322. {
  323. static const char *names[GMBUS_NUM_PORTS] = {
  324. "disabled",
  325. "ssc",
  326. "vga",
  327. "panel",
  328. "dpc",
  329. "dpb",
  330. "reserved",
  331. "dpd",
  332. };
  333. struct drm_psb_private *dev_priv = dev->dev_private;
  334. int ret, i;
  335. dev_priv->gmbus = kcalloc(GMBUS_NUM_PORTS, sizeof(struct intel_gmbus),
  336. GFP_KERNEL);
  337. if (dev_priv->gmbus == NULL)
  338. return -ENOMEM;
  339. for (i = 0; i < GMBUS_NUM_PORTS; i++) {
  340. struct intel_gmbus *bus = &dev_priv->gmbus[i];
  341. bus->adapter.owner = THIS_MODULE;
  342. bus->adapter.class = I2C_CLASS_DDC;
  343. snprintf(bus->adapter.name,
  344. sizeof(bus->adapter.name),
  345. "gma500 gmbus %s",
  346. names[i]);
  347. bus->adapter.dev.parent = &dev->pdev->dev;
  348. bus->adapter.algo_data = dev_priv;
  349. bus->adapter.algo = &gmbus_algorithm;
  350. ret = i2c_add_adapter(&bus->adapter);
  351. if (ret)
  352. goto err;
  353. /* By default use a conservative clock rate */
  354. bus->reg0 = i | GMBUS_RATE_100KHZ;
  355. /* XXX force bit banging until GMBUS is fully debugged */
  356. bus->force_bit = intel_gpio_create(dev_priv, i);
  357. }
  358. gma_intel_i2c_reset(dev_priv->dev);
  359. return 0;
  360. err:
  361. while (--i) {
  362. struct intel_gmbus *bus = &dev_priv->gmbus[i];
  363. i2c_del_adapter(&bus->adapter);
  364. }
  365. kfree(dev_priv->gmbus);
  366. dev_priv->gmbus = NULL;
  367. return ret;
  368. }
  369. void gma_intel_gmbus_set_speed(struct i2c_adapter *adapter, int speed)
  370. {
  371. struct intel_gmbus *bus = to_intel_gmbus(adapter);
  372. /* speed:
  373. * 0x0 = 100 KHz
  374. * 0x1 = 50 KHz
  375. * 0x2 = 400 KHz
  376. * 0x3 = 1000 Khz
  377. */
  378. bus->reg0 = (bus->reg0 & ~(0x3 << 8)) | (speed << 8);
  379. }
  380. void gma_intel_gmbus_force_bit(struct i2c_adapter *adapter, bool force_bit)
  381. {
  382. struct intel_gmbus *bus = to_intel_gmbus(adapter);
  383. if (force_bit) {
  384. if (bus->force_bit == NULL) {
  385. struct drm_psb_private *dev_priv = adapter->algo_data;
  386. bus->force_bit = intel_gpio_create(dev_priv,
  387. bus->reg0 & 0xff);
  388. }
  389. } else {
  390. if (bus->force_bit) {
  391. i2c_del_adapter(bus->force_bit);
  392. kfree(bus->force_bit);
  393. bus->force_bit = NULL;
  394. }
  395. }
  396. }
  397. void gma_intel_teardown_gmbus(struct drm_device *dev)
  398. {
  399. struct drm_psb_private *dev_priv = dev->dev_private;
  400. int i;
  401. if (dev_priv->gmbus == NULL)
  402. return;
  403. for (i = 0; i < GMBUS_NUM_PORTS; i++) {
  404. struct intel_gmbus *bus = &dev_priv->gmbus[i];
  405. if (bus->force_bit) {
  406. i2c_del_adapter(bus->force_bit);
  407. kfree(bus->force_bit);
  408. }
  409. i2c_del_adapter(&bus->adapter);
  410. }
  411. kfree(dev_priv->gmbus);
  412. dev_priv->gmbus = NULL;
  413. }