i2c-ocores.c 9.6 KB

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