i2c-ocores.c 9.7 KB

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