i2c-ocores.c 9.6 KB

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