aux.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 "aux.h"
  25. #include "pad.h"
  26. static int
  27. nvkm_i2c_aux_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
  28. {
  29. struct nvkm_i2c_aux *aux = container_of(adap, typeof(*aux), i2c);
  30. struct i2c_msg *msg = msgs;
  31. int ret, mcnt = num;
  32. ret = nvkm_i2c_aux_acquire(aux);
  33. if (ret)
  34. return ret;
  35. while (mcnt--) {
  36. u8 remaining = msg->len;
  37. u8 *ptr = msg->buf;
  38. while (remaining) {
  39. u8 cnt = (remaining > 16) ? 16 : remaining;
  40. u8 cmd;
  41. if (msg->flags & I2C_M_RD)
  42. cmd = 1;
  43. else
  44. cmd = 0;
  45. if (mcnt || remaining > 16)
  46. cmd |= 4; /* MOT */
  47. ret = aux->func->xfer(aux, true, cmd, msg->addr, ptr, cnt);
  48. if (ret < 0) {
  49. nvkm_i2c_aux_release(aux);
  50. return ret;
  51. }
  52. ptr += cnt;
  53. remaining -= cnt;
  54. }
  55. msg++;
  56. }
  57. nvkm_i2c_aux_release(aux);
  58. return num;
  59. }
  60. static u32
  61. nvkm_i2c_aux_i2c_func(struct i2c_adapter *adap)
  62. {
  63. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  64. }
  65. static const struct i2c_algorithm
  66. nvkm_i2c_aux_i2c_algo = {
  67. .master_xfer = nvkm_i2c_aux_i2c_xfer,
  68. .functionality = nvkm_i2c_aux_i2c_func
  69. };
  70. void
  71. nvkm_i2c_aux_monitor(struct nvkm_i2c_aux *aux, bool monitor)
  72. {
  73. struct nvkm_i2c_pad *pad = aux->pad;
  74. AUX_TRACE(aux, "monitor: %s", monitor ? "yes" : "no");
  75. if (monitor)
  76. nvkm_i2c_pad_mode(pad, NVKM_I2C_PAD_AUX);
  77. else
  78. nvkm_i2c_pad_mode(pad, NVKM_I2C_PAD_OFF);
  79. }
  80. void
  81. nvkm_i2c_aux_release(struct nvkm_i2c_aux *aux)
  82. {
  83. struct nvkm_i2c_pad *pad = aux->pad;
  84. AUX_TRACE(aux, "release");
  85. nvkm_i2c_pad_release(pad);
  86. mutex_unlock(&aux->mutex);
  87. }
  88. int
  89. nvkm_i2c_aux_acquire(struct nvkm_i2c_aux *aux)
  90. {
  91. struct nvkm_i2c_pad *pad = aux->pad;
  92. int ret;
  93. AUX_TRACE(aux, "acquire");
  94. mutex_lock(&aux->mutex);
  95. ret = nvkm_i2c_pad_acquire(pad, NVKM_I2C_PAD_AUX);
  96. if (ret)
  97. mutex_unlock(&aux->mutex);
  98. return ret;
  99. }
  100. int
  101. nvkm_i2c_aux_xfer(struct nvkm_i2c_aux *aux, bool retry, u8 type,
  102. u32 addr, u8 *data, u8 size)
  103. {
  104. return aux->func->xfer(aux, retry, type, addr, data, size);
  105. }
  106. int
  107. nvkm_i2c_aux_lnk_ctl(struct nvkm_i2c_aux *aux, int nr, int bw, bool ef)
  108. {
  109. if (aux->func->lnk_ctl)
  110. return aux->func->lnk_ctl(aux, nr, bw, ef);
  111. return -ENODEV;
  112. }
  113. void
  114. nvkm_i2c_aux_del(struct nvkm_i2c_aux **paux)
  115. {
  116. struct nvkm_i2c_aux *aux = *paux;
  117. if (aux && !WARN_ON(!aux->func)) {
  118. AUX_TRACE(aux, "dtor");
  119. list_del(&aux->head);
  120. i2c_del_adapter(&aux->i2c);
  121. kfree(*paux);
  122. *paux = NULL;
  123. }
  124. }
  125. int
  126. nvkm_i2c_aux_ctor(const struct nvkm_i2c_aux_func *func,
  127. struct nvkm_i2c_pad *pad, int id,
  128. struct nvkm_i2c_aux *aux)
  129. {
  130. struct nvkm_device *device = pad->i2c->subdev.device;
  131. aux->func = func;
  132. aux->pad = pad;
  133. aux->id = id;
  134. mutex_init(&aux->mutex);
  135. list_add_tail(&aux->head, &pad->i2c->aux);
  136. AUX_TRACE(aux, "ctor");
  137. snprintf(aux->i2c.name, sizeof(aux->i2c.name), "nvkm-%s-aux-%04x",
  138. dev_name(device->dev), id);
  139. aux->i2c.owner = THIS_MODULE;
  140. aux->i2c.dev.parent = device->dev;
  141. aux->i2c.algo = &nvkm_i2c_aux_i2c_algo;
  142. return i2c_add_adapter(&aux->i2c);
  143. }
  144. int
  145. nvkm_i2c_aux_new_(const struct nvkm_i2c_aux_func *func,
  146. struct nvkm_i2c_pad *pad, int id,
  147. struct nvkm_i2c_aux **paux)
  148. {
  149. if (!(*paux = kzalloc(sizeof(**paux), GFP_KERNEL)))
  150. return -ENOMEM;
  151. return nvkm_i2c_aux_ctor(func, pad, id, *paux);
  152. }