solo6x10-i2c.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /*
  2. * Copyright (C) 2010-2013 Bluecherry, LLC <http://www.bluecherrydvr.com>
  3. *
  4. * Original author:
  5. * Ben Collins <bcollins@ubuntu.com>
  6. *
  7. * Additional work by:
  8. * John Brooks <john.brooks@bluecherry.net>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. */
  20. /* XXX: The SOLO6x10 i2c does not have separate interrupts for each i2c
  21. * channel. The bus can only handle one i2c event at a time. The below handles
  22. * this all wrong. We should be using the status registers to see if the bus
  23. * is in use, and have a global lock to check the status register. Also,
  24. * the bulk of the work should be handled out-of-interrupt. The ugly loops
  25. * that occur during interrupt scare me. The ISR should merely signal
  26. * thread context, ACK the interrupt, and move on. -- BenC */
  27. #include <linux/kernel.h>
  28. #include <linux/sched/signal.h>
  29. #include "solo6x10.h"
  30. u8 solo_i2c_readbyte(struct solo_dev *solo_dev, int id, u8 addr, u8 off)
  31. {
  32. struct i2c_msg msgs[2];
  33. u8 data;
  34. msgs[0].flags = 0;
  35. msgs[0].addr = addr;
  36. msgs[0].len = 1;
  37. msgs[0].buf = &off;
  38. msgs[1].flags = I2C_M_RD;
  39. msgs[1].addr = addr;
  40. msgs[1].len = 1;
  41. msgs[1].buf = &data;
  42. i2c_transfer(&solo_dev->i2c_adap[id], msgs, 2);
  43. return data;
  44. }
  45. void solo_i2c_writebyte(struct solo_dev *solo_dev, int id, u8 addr,
  46. u8 off, u8 data)
  47. {
  48. struct i2c_msg msgs;
  49. u8 buf[2];
  50. buf[0] = off;
  51. buf[1] = data;
  52. msgs.flags = 0;
  53. msgs.addr = addr;
  54. msgs.len = 2;
  55. msgs.buf = buf;
  56. i2c_transfer(&solo_dev->i2c_adap[id], &msgs, 1);
  57. }
  58. static void solo_i2c_flush(struct solo_dev *solo_dev, int wr)
  59. {
  60. u32 ctrl;
  61. ctrl = SOLO_IIC_CH_SET(solo_dev->i2c_id);
  62. if (solo_dev->i2c_state == IIC_STATE_START)
  63. ctrl |= SOLO_IIC_START;
  64. if (wr) {
  65. ctrl |= SOLO_IIC_WRITE;
  66. } else {
  67. ctrl |= SOLO_IIC_READ;
  68. if (!(solo_dev->i2c_msg->flags & I2C_M_NO_RD_ACK))
  69. ctrl |= SOLO_IIC_ACK_EN;
  70. }
  71. if (solo_dev->i2c_msg_ptr == solo_dev->i2c_msg->len)
  72. ctrl |= SOLO_IIC_STOP;
  73. solo_reg_write(solo_dev, SOLO_IIC_CTRL, ctrl);
  74. }
  75. static void solo_i2c_start(struct solo_dev *solo_dev)
  76. {
  77. u32 addr = solo_dev->i2c_msg->addr << 1;
  78. if (solo_dev->i2c_msg->flags & I2C_M_RD)
  79. addr |= 1;
  80. solo_dev->i2c_state = IIC_STATE_START;
  81. solo_reg_write(solo_dev, SOLO_IIC_TXD, addr);
  82. solo_i2c_flush(solo_dev, 1);
  83. }
  84. static void solo_i2c_stop(struct solo_dev *solo_dev)
  85. {
  86. solo_irq_off(solo_dev, SOLO_IRQ_IIC);
  87. solo_reg_write(solo_dev, SOLO_IIC_CTRL, 0);
  88. solo_dev->i2c_state = IIC_STATE_STOP;
  89. wake_up(&solo_dev->i2c_wait);
  90. }
  91. static int solo_i2c_handle_read(struct solo_dev *solo_dev)
  92. {
  93. prepare_read:
  94. if (solo_dev->i2c_msg_ptr != solo_dev->i2c_msg->len) {
  95. solo_i2c_flush(solo_dev, 0);
  96. return 0;
  97. }
  98. solo_dev->i2c_msg_ptr = 0;
  99. solo_dev->i2c_msg++;
  100. solo_dev->i2c_msg_num--;
  101. if (solo_dev->i2c_msg_num == 0) {
  102. solo_i2c_stop(solo_dev);
  103. return 0;
  104. }
  105. if (!(solo_dev->i2c_msg->flags & I2C_M_NOSTART)) {
  106. solo_i2c_start(solo_dev);
  107. } else {
  108. if (solo_dev->i2c_msg->flags & I2C_M_RD)
  109. goto prepare_read;
  110. else
  111. solo_i2c_stop(solo_dev);
  112. }
  113. return 0;
  114. }
  115. static int solo_i2c_handle_write(struct solo_dev *solo_dev)
  116. {
  117. retry_write:
  118. if (solo_dev->i2c_msg_ptr != solo_dev->i2c_msg->len) {
  119. solo_reg_write(solo_dev, SOLO_IIC_TXD,
  120. solo_dev->i2c_msg->buf[solo_dev->i2c_msg_ptr]);
  121. solo_dev->i2c_msg_ptr++;
  122. solo_i2c_flush(solo_dev, 1);
  123. return 0;
  124. }
  125. solo_dev->i2c_msg_ptr = 0;
  126. solo_dev->i2c_msg++;
  127. solo_dev->i2c_msg_num--;
  128. if (solo_dev->i2c_msg_num == 0) {
  129. solo_i2c_stop(solo_dev);
  130. return 0;
  131. }
  132. if (!(solo_dev->i2c_msg->flags & I2C_M_NOSTART)) {
  133. solo_i2c_start(solo_dev);
  134. } else {
  135. if (solo_dev->i2c_msg->flags & I2C_M_RD)
  136. solo_i2c_stop(solo_dev);
  137. else
  138. goto retry_write;
  139. }
  140. return 0;
  141. }
  142. int solo_i2c_isr(struct solo_dev *solo_dev)
  143. {
  144. u32 status = solo_reg_read(solo_dev, SOLO_IIC_CTRL);
  145. int ret = -EINVAL;
  146. if (CHK_FLAGS(status, SOLO_IIC_STATE_TRNS | SOLO_IIC_STATE_SIG_ERR)
  147. || solo_dev->i2c_id < 0) {
  148. solo_i2c_stop(solo_dev);
  149. return -ENXIO;
  150. }
  151. switch (solo_dev->i2c_state) {
  152. case IIC_STATE_START:
  153. if (solo_dev->i2c_msg->flags & I2C_M_RD) {
  154. solo_dev->i2c_state = IIC_STATE_READ;
  155. ret = solo_i2c_handle_read(solo_dev);
  156. break;
  157. }
  158. solo_dev->i2c_state = IIC_STATE_WRITE;
  159. /* fall through */
  160. case IIC_STATE_WRITE:
  161. ret = solo_i2c_handle_write(solo_dev);
  162. break;
  163. case IIC_STATE_READ:
  164. solo_dev->i2c_msg->buf[solo_dev->i2c_msg_ptr] =
  165. solo_reg_read(solo_dev, SOLO_IIC_RXD);
  166. solo_dev->i2c_msg_ptr++;
  167. ret = solo_i2c_handle_read(solo_dev);
  168. break;
  169. default:
  170. solo_i2c_stop(solo_dev);
  171. }
  172. return ret;
  173. }
  174. static int solo_i2c_master_xfer(struct i2c_adapter *adap,
  175. struct i2c_msg msgs[], int num)
  176. {
  177. struct solo_dev *solo_dev = adap->algo_data;
  178. unsigned long timeout;
  179. int ret;
  180. int i;
  181. DEFINE_WAIT(wait);
  182. for (i = 0; i < SOLO_I2C_ADAPTERS; i++) {
  183. if (&solo_dev->i2c_adap[i] == adap)
  184. break;
  185. }
  186. if (i == SOLO_I2C_ADAPTERS)
  187. return num; /* XXX Right return value for failure? */
  188. mutex_lock(&solo_dev->i2c_mutex);
  189. solo_dev->i2c_id = i;
  190. solo_dev->i2c_msg = msgs;
  191. solo_dev->i2c_msg_num = num;
  192. solo_dev->i2c_msg_ptr = 0;
  193. solo_reg_write(solo_dev, SOLO_IIC_CTRL, 0);
  194. solo_irq_on(solo_dev, SOLO_IRQ_IIC);
  195. solo_i2c_start(solo_dev);
  196. timeout = HZ / 2;
  197. for (;;) {
  198. prepare_to_wait(&solo_dev->i2c_wait, &wait,
  199. TASK_INTERRUPTIBLE);
  200. if (solo_dev->i2c_state == IIC_STATE_STOP)
  201. break;
  202. timeout = schedule_timeout(timeout);
  203. if (!timeout)
  204. break;
  205. if (signal_pending(current))
  206. break;
  207. }
  208. finish_wait(&solo_dev->i2c_wait, &wait);
  209. ret = num - solo_dev->i2c_msg_num;
  210. solo_dev->i2c_state = IIC_STATE_IDLE;
  211. solo_dev->i2c_id = -1;
  212. mutex_unlock(&solo_dev->i2c_mutex);
  213. return ret;
  214. }
  215. static u32 solo_i2c_functionality(struct i2c_adapter *adap)
  216. {
  217. return I2C_FUNC_I2C;
  218. }
  219. static const struct i2c_algorithm solo_i2c_algo = {
  220. .master_xfer = solo_i2c_master_xfer,
  221. .functionality = solo_i2c_functionality,
  222. };
  223. int solo_i2c_init(struct solo_dev *solo_dev)
  224. {
  225. int i;
  226. int ret;
  227. solo_reg_write(solo_dev, SOLO_IIC_CFG,
  228. SOLO_IIC_PRESCALE(8) | SOLO_IIC_ENABLE);
  229. solo_dev->i2c_id = -1;
  230. solo_dev->i2c_state = IIC_STATE_IDLE;
  231. init_waitqueue_head(&solo_dev->i2c_wait);
  232. mutex_init(&solo_dev->i2c_mutex);
  233. for (i = 0; i < SOLO_I2C_ADAPTERS; i++) {
  234. struct i2c_adapter *adap = &solo_dev->i2c_adap[i];
  235. snprintf(adap->name, I2C_NAME_SIZE, "%s I2C %d",
  236. SOLO6X10_NAME, i);
  237. adap->algo = &solo_i2c_algo;
  238. adap->algo_data = solo_dev;
  239. adap->retries = 1;
  240. adap->dev.parent = &solo_dev->pdev->dev;
  241. ret = i2c_add_adapter(adap);
  242. if (ret) {
  243. adap->algo_data = NULL;
  244. break;
  245. }
  246. }
  247. if (ret) {
  248. for (i = 0; i < SOLO_I2C_ADAPTERS; i++) {
  249. if (!solo_dev->i2c_adap[i].algo_data)
  250. break;
  251. i2c_del_adapter(&solo_dev->i2c_adap[i]);
  252. solo_dev->i2c_adap[i].algo_data = NULL;
  253. }
  254. return ret;
  255. }
  256. return 0;
  257. }
  258. void solo_i2c_exit(struct solo_dev *solo_dev)
  259. {
  260. int i;
  261. for (i = 0; i < SOLO_I2C_ADAPTERS; i++) {
  262. if (!solo_dev->i2c_adap[i].algo_data)
  263. continue;
  264. i2c_del_adapter(&solo_dev->i2c_adap[i]);
  265. solo_dev->i2c_adap[i].algo_data = NULL;
  266. }
  267. }