i2c-ocores.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /*
  2. * i2c-ocores.c: I2C bus driver for OpenCores I2C controller
  3. * (http://www.opencores.org/projects.cgi/web/i2c/overview).
  4. *
  5. * Peter Korsgaard <jacmet@sunsite.dk>
  6. *
  7. * This file is licensed under the terms of the GNU General Public License
  8. * version 2. This program is licensed "as is" without any warranty of any
  9. * kind, whether express or implied.
  10. */
  11. /*
  12. * This driver can be used from the device tree, see
  13. * Documentation/devicetree/bindings/i2c/ocore-i2c.txt
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/init.h>
  18. #include <linux/errno.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/i2c.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/wait.h>
  23. #include <linux/i2c-ocores.h>
  24. #include <linux/slab.h>
  25. #include <linux/io.h>
  26. #include <linux/of_i2c.h>
  27. struct ocores_i2c {
  28. void __iomem *base;
  29. int regstep;
  30. wait_queue_head_t wait;
  31. struct i2c_adapter adap;
  32. struct i2c_msg *msg;
  33. int pos;
  34. int nmsgs;
  35. int state; /* see STATE_ */
  36. int clock_khz;
  37. };
  38. /* registers */
  39. #define OCI2C_PRELOW 0
  40. #define OCI2C_PREHIGH 1
  41. #define OCI2C_CONTROL 2
  42. #define OCI2C_DATA 3
  43. #define OCI2C_CMD 4 /* write only */
  44. #define OCI2C_STATUS 4 /* read only, same address as OCI2C_CMD */
  45. #define OCI2C_CTRL_IEN 0x40
  46. #define OCI2C_CTRL_EN 0x80
  47. #define OCI2C_CMD_START 0x91
  48. #define OCI2C_CMD_STOP 0x41
  49. #define OCI2C_CMD_READ 0x21
  50. #define OCI2C_CMD_WRITE 0x11
  51. #define OCI2C_CMD_READ_ACK 0x21
  52. #define OCI2C_CMD_READ_NACK 0x29
  53. #define OCI2C_CMD_IACK 0x01
  54. #define OCI2C_STAT_IF 0x01
  55. #define OCI2C_STAT_TIP 0x02
  56. #define OCI2C_STAT_ARBLOST 0x20
  57. #define OCI2C_STAT_BUSY 0x40
  58. #define OCI2C_STAT_NACK 0x80
  59. #define STATE_DONE 0
  60. #define STATE_START 1
  61. #define STATE_WRITE 2
  62. #define STATE_READ 3
  63. #define STATE_ERROR 4
  64. static inline void oc_setreg(struct ocores_i2c *i2c, int reg, u8 value)
  65. {
  66. iowrite8(value, i2c->base + reg * i2c->regstep);
  67. }
  68. static inline u8 oc_getreg(struct ocores_i2c *i2c, int reg)
  69. {
  70. return ioread8(i2c->base + reg * i2c->regstep);
  71. }
  72. static void ocores_process(struct ocores_i2c *i2c)
  73. {
  74. struct i2c_msg *msg = i2c->msg;
  75. u8 stat = oc_getreg(i2c, OCI2C_STATUS);
  76. if ((i2c->state == STATE_DONE) || (i2c->state == STATE_ERROR)) {
  77. /* stop has been sent */
  78. oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_IACK);
  79. wake_up(&i2c->wait);
  80. return;
  81. }
  82. /* error? */
  83. if (stat & OCI2C_STAT_ARBLOST) {
  84. i2c->state = STATE_ERROR;
  85. oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_STOP);
  86. return;
  87. }
  88. if ((i2c->state == STATE_START) || (i2c->state == STATE_WRITE)) {
  89. i2c->state =
  90. (msg->flags & I2C_M_RD) ? STATE_READ : STATE_WRITE;
  91. if (stat & OCI2C_STAT_NACK) {
  92. i2c->state = STATE_ERROR;
  93. oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_STOP);
  94. return;
  95. }
  96. } else
  97. msg->buf[i2c->pos++] = oc_getreg(i2c, OCI2C_DATA);
  98. /* end of msg? */
  99. if (i2c->pos == msg->len) {
  100. i2c->nmsgs--;
  101. i2c->msg++;
  102. i2c->pos = 0;
  103. msg = i2c->msg;
  104. if (i2c->nmsgs) { /* end? */
  105. /* send start? */
  106. if (!(msg->flags & I2C_M_NOSTART)) {
  107. u8 addr = (msg->addr << 1);
  108. if (msg->flags & I2C_M_RD)
  109. addr |= 1;
  110. i2c->state = STATE_START;
  111. oc_setreg(i2c, OCI2C_DATA, addr);
  112. oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_START);
  113. return;
  114. } else
  115. i2c->state = (msg->flags & I2C_M_RD)
  116. ? STATE_READ : STATE_WRITE;
  117. } else {
  118. i2c->state = STATE_DONE;
  119. oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_STOP);
  120. return;
  121. }
  122. }
  123. if (i2c->state == STATE_READ) {
  124. oc_setreg(i2c, OCI2C_CMD, i2c->pos == (msg->len-1) ?
  125. OCI2C_CMD_READ_NACK : OCI2C_CMD_READ_ACK);
  126. } else {
  127. oc_setreg(i2c, OCI2C_DATA, msg->buf[i2c->pos++]);
  128. oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_WRITE);
  129. }
  130. }
  131. static irqreturn_t ocores_isr(int irq, void *dev_id)
  132. {
  133. struct ocores_i2c *i2c = dev_id;
  134. ocores_process(i2c);
  135. return IRQ_HANDLED;
  136. }
  137. static int ocores_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
  138. {
  139. struct ocores_i2c *i2c = i2c_get_adapdata(adap);
  140. i2c->msg = msgs;
  141. i2c->pos = 0;
  142. i2c->nmsgs = num;
  143. i2c->state = STATE_START;
  144. oc_setreg(i2c, OCI2C_DATA,
  145. (i2c->msg->addr << 1) |
  146. ((i2c->msg->flags & I2C_M_RD) ? 1:0));
  147. oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_START);
  148. if (wait_event_timeout(i2c->wait, (i2c->state == STATE_ERROR) ||
  149. (i2c->state == STATE_DONE), HZ))
  150. return (i2c->state == STATE_DONE) ? num : -EIO;
  151. else
  152. return -ETIMEDOUT;
  153. }
  154. static void ocores_init(struct ocores_i2c *i2c)
  155. {
  156. int prescale;
  157. u8 ctrl = oc_getreg(i2c, OCI2C_CONTROL);
  158. /* make sure the device is disabled */
  159. oc_setreg(i2c, OCI2C_CONTROL, ctrl & ~(OCI2C_CTRL_EN|OCI2C_CTRL_IEN));
  160. prescale = (i2c->clock_khz / (5*100)) - 1;
  161. oc_setreg(i2c, OCI2C_PRELOW, prescale & 0xff);
  162. oc_setreg(i2c, OCI2C_PREHIGH, prescale >> 8);
  163. /* Init the device */
  164. oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_IACK);
  165. oc_setreg(i2c, OCI2C_CONTROL, ctrl | OCI2C_CTRL_IEN | OCI2C_CTRL_EN);
  166. }
  167. static u32 ocores_func(struct i2c_adapter *adap)
  168. {
  169. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  170. }
  171. static const struct i2c_algorithm ocores_algorithm = {
  172. .master_xfer = ocores_xfer,
  173. .functionality = ocores_func,
  174. };
  175. static struct i2c_adapter ocores_adapter = {
  176. .owner = THIS_MODULE,
  177. .name = "i2c-ocores",
  178. .class = I2C_CLASS_HWMON | I2C_CLASS_SPD,
  179. .algo = &ocores_algorithm,
  180. };
  181. #ifdef CONFIG_OF
  182. static int ocores_i2c_of_probe(struct platform_device *pdev,
  183. struct ocores_i2c *i2c)
  184. {
  185. const __be32* val;
  186. val = of_get_property(pdev->dev.of_node, "regstep", NULL);
  187. if (!val) {
  188. dev_err(&pdev->dev, "Missing required parameter 'regstep'\n");
  189. return -ENODEV;
  190. }
  191. i2c->regstep = be32_to_cpup(val);
  192. val = of_get_property(pdev->dev.of_node, "clock-frequency", NULL);
  193. if (!val) {
  194. dev_err(&pdev->dev,
  195. "Missing required parameter 'clock-frequency'\n");
  196. return -ENODEV;
  197. }
  198. i2c->clock_khz = be32_to_cpup(val) / 1000;
  199. return 0;
  200. }
  201. #else
  202. #define ocores_i2c_of_probe(pdev,i2c) -ENODEV
  203. #endif
  204. static int __devinit ocores_i2c_probe(struct platform_device *pdev)
  205. {
  206. struct ocores_i2c *i2c;
  207. struct ocores_i2c_platform_data *pdata;
  208. struct resource *res, *res2;
  209. int ret;
  210. int i;
  211. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  212. if (!res)
  213. return -ENODEV;
  214. res2 = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  215. if (!res2)
  216. return -ENODEV;
  217. i2c = devm_kzalloc(&pdev->dev, sizeof(*i2c), GFP_KERNEL);
  218. if (!i2c)
  219. return -ENOMEM;
  220. if (!devm_request_mem_region(&pdev->dev, res->start,
  221. resource_size(res), pdev->name)) {
  222. dev_err(&pdev->dev, "Memory region busy\n");
  223. return -EBUSY;
  224. }
  225. i2c->base = devm_ioremap_nocache(&pdev->dev, res->start,
  226. resource_size(res));
  227. if (!i2c->base) {
  228. dev_err(&pdev->dev, "Unable to map registers\n");
  229. return -EIO;
  230. }
  231. pdata = pdev->dev.platform_data;
  232. if (pdata) {
  233. i2c->regstep = pdata->regstep;
  234. i2c->clock_khz = pdata->clock_khz;
  235. } else {
  236. ret = ocores_i2c_of_probe(pdev, i2c);
  237. if (ret)
  238. return ret;
  239. }
  240. ocores_init(i2c);
  241. init_waitqueue_head(&i2c->wait);
  242. ret = devm_request_irq(&pdev->dev, res2->start, ocores_isr, 0,
  243. pdev->name, i2c);
  244. if (ret) {
  245. dev_err(&pdev->dev, "Cannot claim IRQ\n");
  246. return ret;
  247. }
  248. /* hook up driver to tree */
  249. platform_set_drvdata(pdev, i2c);
  250. i2c->adap = ocores_adapter;
  251. i2c_set_adapdata(&i2c->adap, i2c);
  252. i2c->adap.dev.parent = &pdev->dev;
  253. i2c->adap.dev.of_node = pdev->dev.of_node;
  254. /* add i2c adapter to i2c tree */
  255. ret = i2c_add_adapter(&i2c->adap);
  256. if (ret) {
  257. dev_err(&pdev->dev, "Failed to add adapter\n");
  258. return ret;
  259. }
  260. /* add in known devices to the bus */
  261. if (pdata) {
  262. for (i = 0; i < pdata->num_devices; i++)
  263. i2c_new_device(&i2c->adap, pdata->devices + i);
  264. } else {
  265. of_i2c_register_devices(&i2c->adap);
  266. }
  267. return 0;
  268. }
  269. static int __devexit ocores_i2c_remove(struct platform_device *pdev)
  270. {
  271. struct ocores_i2c *i2c = platform_get_drvdata(pdev);
  272. /* disable i2c logic */
  273. oc_setreg(i2c, OCI2C_CONTROL, oc_getreg(i2c, OCI2C_CONTROL)
  274. & ~(OCI2C_CTRL_EN|OCI2C_CTRL_IEN));
  275. /* remove adapter & data */
  276. i2c_del_adapter(&i2c->adap);
  277. platform_set_drvdata(pdev, NULL);
  278. return 0;
  279. }
  280. #ifdef CONFIG_PM
  281. static int ocores_i2c_suspend(struct device *dev)
  282. {
  283. struct ocores_i2c *i2c = dev_get_drvdata(dev);
  284. u8 ctrl = oc_getreg(i2c, OCI2C_CONTROL);
  285. /* make sure the device is disabled */
  286. oc_setreg(i2c, OCI2C_CONTROL, ctrl & ~(OCI2C_CTRL_EN|OCI2C_CTRL_IEN));
  287. return 0;
  288. }
  289. static int ocores_i2c_resume(struct device *dev)
  290. {
  291. struct ocores_i2c *i2c = dev_get_drvdata(dev);
  292. ocores_init(i2c);
  293. return 0;
  294. }
  295. static SIMPLE_DEV_PM_OPS(ocores_i2c_pm, ocores_i2c_suspend, ocores_i2c_resume);
  296. #define OCORES_I2C_PM (&ocores_i2c_pm)
  297. #else
  298. #define OCORES_I2C_PM NULL
  299. #endif
  300. static struct of_device_id ocores_i2c_match[] = {
  301. { .compatible = "opencores,i2c-ocores", },
  302. {},
  303. };
  304. MODULE_DEVICE_TABLE(of, ocores_i2c_match);
  305. static struct platform_driver ocores_i2c_driver = {
  306. .probe = ocores_i2c_probe,
  307. .remove = __devexit_p(ocores_i2c_remove),
  308. .driver = {
  309. .owner = THIS_MODULE,
  310. .name = "ocores-i2c",
  311. .of_match_table = ocores_i2c_match,
  312. .pm = OCORES_I2C_PM,
  313. },
  314. };
  315. module_platform_driver(ocores_i2c_driver);
  316. MODULE_AUTHOR("Peter Korsgaard <jacmet@sunsite.dk>");
  317. MODULE_DESCRIPTION("OpenCores I2C bus driver");
  318. MODULE_LICENSE("GPL");
  319. MODULE_ALIAS("platform:ocores-i2c");