i2c.c 7.5 KB

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