i2c-ocores.c 9.9 KB

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