bus.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * Copyright 2015 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 <bskeggs@redhat.com>
  23. */
  24. #include "bus.h"
  25. #include "pad.h"
  26. #include <core/option.h>
  27. /*******************************************************************************
  28. * i2c-algo-bit
  29. ******************************************************************************/
  30. static int
  31. nvkm_i2c_bus_pre_xfer(struct i2c_adapter *adap)
  32. {
  33. struct nvkm_i2c_bus *bus = container_of(adap, typeof(*bus), i2c);
  34. return nvkm_i2c_bus_acquire(bus);
  35. }
  36. static void
  37. nvkm_i2c_bus_post_xfer(struct i2c_adapter *adap)
  38. {
  39. struct nvkm_i2c_bus *bus = container_of(adap, typeof(*bus), i2c);
  40. return nvkm_i2c_bus_release(bus);
  41. }
  42. static void
  43. nvkm_i2c_bus_setscl(void *data, int state)
  44. {
  45. struct nvkm_i2c_bus *bus = data;
  46. bus->func->drive_scl(bus, state);
  47. }
  48. static void
  49. nvkm_i2c_bus_setsda(void *data, int state)
  50. {
  51. struct nvkm_i2c_bus *bus = data;
  52. bus->func->drive_sda(bus, state);
  53. }
  54. static int
  55. nvkm_i2c_bus_getscl(void *data)
  56. {
  57. struct nvkm_i2c_bus *bus = data;
  58. return bus->func->sense_scl(bus);
  59. }
  60. static int
  61. nvkm_i2c_bus_getsda(void *data)
  62. {
  63. struct nvkm_i2c_bus *bus = data;
  64. return bus->func->sense_sda(bus);
  65. }
  66. /*******************************************************************************
  67. * !i2c-algo-bit (off-chip i2c bus / hw i2c / internal bit-banging algo)
  68. ******************************************************************************/
  69. static int
  70. nvkm_i2c_bus_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
  71. {
  72. struct nvkm_i2c_bus *bus = container_of(adap, typeof(*bus), i2c);
  73. int ret;
  74. ret = nvkm_i2c_bus_acquire(bus);
  75. if (ret)
  76. return ret;
  77. ret = bus->func->xfer(bus, msgs, num);
  78. nvkm_i2c_bus_release(bus);
  79. return ret;
  80. }
  81. static u32
  82. nvkm_i2c_bus_func(struct i2c_adapter *adap)
  83. {
  84. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  85. }
  86. static const struct i2c_algorithm
  87. nvkm_i2c_bus_algo = {
  88. .master_xfer = nvkm_i2c_bus_xfer,
  89. .functionality = nvkm_i2c_bus_func,
  90. };
  91. /*******************************************************************************
  92. * nvkm_i2c_bus base
  93. ******************************************************************************/
  94. void
  95. nvkm_i2c_bus_init(struct nvkm_i2c_bus *bus)
  96. {
  97. BUS_TRACE(bus, "init");
  98. if (bus->func->init)
  99. bus->func->init(bus);
  100. }
  101. void
  102. nvkm_i2c_bus_release(struct nvkm_i2c_bus *bus)
  103. {
  104. struct nvkm_i2c_pad *pad = bus->pad;
  105. BUS_TRACE(bus, "release");
  106. nvkm_i2c_pad_release(pad);
  107. mutex_unlock(&bus->mutex);
  108. }
  109. int
  110. nvkm_i2c_bus_acquire(struct nvkm_i2c_bus *bus)
  111. {
  112. struct nvkm_i2c_pad *pad = bus->pad;
  113. int ret;
  114. BUS_TRACE(bus, "acquire");
  115. mutex_lock(&bus->mutex);
  116. ret = nvkm_i2c_pad_acquire(pad, NVKM_I2C_PAD_I2C);
  117. if (ret)
  118. mutex_unlock(&bus->mutex);
  119. return ret;
  120. }
  121. int
  122. nvkm_i2c_bus_probe(struct nvkm_i2c_bus *bus, const char *what,
  123. struct nvkm_i2c_bus_probe *info,
  124. bool (*match)(struct nvkm_i2c_bus *,
  125. struct i2c_board_info *, void *), void *data)
  126. {
  127. int i;
  128. BUS_DBG(bus, "probing %ss", what);
  129. for (i = 0; info[i].dev.addr; i++) {
  130. u8 orig_udelay = 0;
  131. if ((bus->i2c.algo == &i2c_bit_algo) && (info[i].udelay != 0)) {
  132. struct i2c_algo_bit_data *algo = bus->i2c.algo_data;
  133. BUS_DBG(bus, "%dms delay instead of %dms",
  134. info[i].udelay, algo->udelay);
  135. orig_udelay = algo->udelay;
  136. algo->udelay = info[i].udelay;
  137. }
  138. if (nvkm_probe_i2c(&bus->i2c, info[i].dev.addr) &&
  139. (!match || match(bus, &info[i].dev, data))) {
  140. BUS_DBG(bus, "detected %s: %s",
  141. what, info[i].dev.type);
  142. return i;
  143. }
  144. if (orig_udelay) {
  145. struct i2c_algo_bit_data *algo = bus->i2c.algo_data;
  146. algo->udelay = orig_udelay;
  147. }
  148. }
  149. BUS_DBG(bus, "no devices found.");
  150. return -ENODEV;
  151. }
  152. void
  153. nvkm_i2c_bus_del(struct nvkm_i2c_bus **pbus)
  154. {
  155. struct nvkm_i2c_bus *bus = *pbus;
  156. if (bus && !WARN_ON(!bus->func)) {
  157. BUS_TRACE(bus, "dtor");
  158. list_del(&bus->head);
  159. i2c_del_adapter(&bus->i2c);
  160. kfree(bus->i2c.algo_data);
  161. kfree(*pbus);
  162. *pbus = NULL;
  163. }
  164. }
  165. int
  166. nvkm_i2c_bus_ctor(const struct nvkm_i2c_bus_func *func,
  167. struct nvkm_i2c_pad *pad, int id,
  168. struct nvkm_i2c_bus *bus)
  169. {
  170. struct nvkm_device *device = pad->i2c->subdev.device;
  171. struct i2c_algo_bit_data *bit;
  172. #ifndef CONFIG_NOUVEAU_I2C_INTERNAL_DEFAULT
  173. const bool internal = false;
  174. #else
  175. const bool internal = true;
  176. #endif
  177. int ret;
  178. bus->func = func;
  179. bus->pad = pad;
  180. bus->id = id;
  181. mutex_init(&bus->mutex);
  182. list_add_tail(&bus->head, &pad->i2c->bus);
  183. BUS_TRACE(bus, "ctor");
  184. snprintf(bus->i2c.name, sizeof(bus->i2c.name), "nvkm-%s-bus-%04x",
  185. dev_name(device->dev), id);
  186. bus->i2c.owner = THIS_MODULE;
  187. bus->i2c.dev.parent = device->dev;
  188. if ( bus->func->drive_scl &&
  189. !nvkm_boolopt(device->cfgopt, "NvI2C", internal)) {
  190. if (!(bit = kzalloc(sizeof(*bit), GFP_KERNEL)))
  191. return -ENOMEM;
  192. bit->udelay = 10;
  193. bit->timeout = usecs_to_jiffies(2200);
  194. bit->data = bus;
  195. bit->pre_xfer = nvkm_i2c_bus_pre_xfer;
  196. bit->post_xfer = nvkm_i2c_bus_post_xfer;
  197. bit->setscl = nvkm_i2c_bus_setscl;
  198. bit->setsda = nvkm_i2c_bus_setsda;
  199. bit->getscl = nvkm_i2c_bus_getscl;
  200. bit->getsda = nvkm_i2c_bus_getsda;
  201. bus->i2c.algo_data = bit;
  202. ret = i2c_bit_add_bus(&bus->i2c);
  203. } else {
  204. bus->i2c.algo = &nvkm_i2c_bus_algo;
  205. ret = i2c_add_adapter(&bus->i2c);
  206. }
  207. return ret;
  208. }
  209. int
  210. nvkm_i2c_bus_new_(const struct nvkm_i2c_bus_func *func,
  211. struct nvkm_i2c_pad *pad, int id,
  212. struct nvkm_i2c_bus **pbus)
  213. {
  214. if (!(*pbus = kzalloc(sizeof(**pbus), GFP_KERNEL)))
  215. return -ENOMEM;
  216. return nvkm_i2c_bus_ctor(func, pad, id, *pbus);
  217. }