i2c-ocores.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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. * Support for the GRLIB port of the controller by
  8. * Andreas Larsson <andreas@gaisler.com>
  9. *
  10. * This file is licensed under the terms of the GNU General Public License
  11. * version 2. This program is licensed "as is" without any warranty of any
  12. * kind, whether express or implied.
  13. */
  14. #include <linux/err.h>
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/errno.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/i2c.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/wait.h>
  22. #include <linux/i2c-ocores.h>
  23. #include <linux/slab.h>
  24. #include <linux/io.h>
  25. #include <linux/log2.h>
  26. struct ocores_i2c {
  27. void __iomem *base;
  28. u32 reg_shift;
  29. u32 reg_io_width;
  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. void (*setreg)(struct ocores_i2c *i2c, int reg, u8 value);
  38. u8 (*getreg)(struct ocores_i2c *i2c, int reg);
  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. #define TYPE_OCORES 0
  67. #define TYPE_GRLIB 1
  68. static void oc_setreg_8(struct ocores_i2c *i2c, int reg, u8 value)
  69. {
  70. iowrite8(value, i2c->base + (reg << i2c->reg_shift));
  71. }
  72. static void oc_setreg_16(struct ocores_i2c *i2c, int reg, u8 value)
  73. {
  74. iowrite16(value, i2c->base + (reg << i2c->reg_shift));
  75. }
  76. static void oc_setreg_32(struct ocores_i2c *i2c, int reg, u8 value)
  77. {
  78. iowrite32(value, i2c->base + (reg << i2c->reg_shift));
  79. }
  80. static inline u8 oc_getreg_8(struct ocores_i2c *i2c, int reg)
  81. {
  82. return ioread8(i2c->base + (reg << i2c->reg_shift));
  83. }
  84. static inline u8 oc_getreg_16(struct ocores_i2c *i2c, int reg)
  85. {
  86. return ioread16(i2c->base + (reg << i2c->reg_shift));
  87. }
  88. static inline u8 oc_getreg_32(struct ocores_i2c *i2c, int reg)
  89. {
  90. return ioread32(i2c->base + (reg << i2c->reg_shift));
  91. }
  92. static inline void oc_setreg(struct ocores_i2c *i2c, int reg, u8 value)
  93. {
  94. i2c->setreg(i2c, reg, value);
  95. }
  96. static inline u8 oc_getreg(struct ocores_i2c *i2c, int reg)
  97. {
  98. return i2c->getreg(i2c, reg);
  99. }
  100. static void ocores_process(struct ocores_i2c *i2c)
  101. {
  102. struct i2c_msg *msg = i2c->msg;
  103. u8 stat = oc_getreg(i2c, OCI2C_STATUS);
  104. if ((i2c->state == STATE_DONE) || (i2c->state == STATE_ERROR)) {
  105. /* stop has been sent */
  106. oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_IACK);
  107. wake_up(&i2c->wait);
  108. return;
  109. }
  110. /* error? */
  111. if (stat & OCI2C_STAT_ARBLOST) {
  112. i2c->state = STATE_ERROR;
  113. oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_STOP);
  114. return;
  115. }
  116. if ((i2c->state == STATE_START) || (i2c->state == STATE_WRITE)) {
  117. i2c->state =
  118. (msg->flags & I2C_M_RD) ? STATE_READ : STATE_WRITE;
  119. if (stat & OCI2C_STAT_NACK) {
  120. i2c->state = STATE_ERROR;
  121. oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_STOP);
  122. return;
  123. }
  124. } else
  125. msg->buf[i2c->pos++] = oc_getreg(i2c, OCI2C_DATA);
  126. /* end of msg? */
  127. if (i2c->pos == msg->len) {
  128. i2c->nmsgs--;
  129. i2c->msg++;
  130. i2c->pos = 0;
  131. msg = i2c->msg;
  132. if (i2c->nmsgs) { /* end? */
  133. /* send start? */
  134. if (!(msg->flags & I2C_M_NOSTART)) {
  135. u8 addr = (msg->addr << 1);
  136. if (msg->flags & I2C_M_RD)
  137. addr |= 1;
  138. i2c->state = STATE_START;
  139. oc_setreg(i2c, OCI2C_DATA, addr);
  140. oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_START);
  141. return;
  142. } else
  143. i2c->state = (msg->flags & I2C_M_RD)
  144. ? STATE_READ : STATE_WRITE;
  145. } else {
  146. i2c->state = STATE_DONE;
  147. oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_STOP);
  148. return;
  149. }
  150. }
  151. if (i2c->state == STATE_READ) {
  152. oc_setreg(i2c, OCI2C_CMD, i2c->pos == (msg->len-1) ?
  153. OCI2C_CMD_READ_NACK : OCI2C_CMD_READ_ACK);
  154. } else {
  155. oc_setreg(i2c, OCI2C_DATA, msg->buf[i2c->pos++]);
  156. oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_WRITE);
  157. }
  158. }
  159. static irqreturn_t ocores_isr(int irq, void *dev_id)
  160. {
  161. struct ocores_i2c *i2c = dev_id;
  162. ocores_process(i2c);
  163. return IRQ_HANDLED;
  164. }
  165. static int ocores_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
  166. {
  167. struct ocores_i2c *i2c = i2c_get_adapdata(adap);
  168. i2c->msg = msgs;
  169. i2c->pos = 0;
  170. i2c->nmsgs = num;
  171. i2c->state = STATE_START;
  172. oc_setreg(i2c, OCI2C_DATA,
  173. (i2c->msg->addr << 1) |
  174. ((i2c->msg->flags & I2C_M_RD) ? 1:0));
  175. oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_START);
  176. if (wait_event_timeout(i2c->wait, (i2c->state == STATE_ERROR) ||
  177. (i2c->state == STATE_DONE), HZ))
  178. return (i2c->state == STATE_DONE) ? num : -EIO;
  179. else
  180. return -ETIMEDOUT;
  181. }
  182. static void ocores_init(struct ocores_i2c *i2c)
  183. {
  184. int prescale;
  185. u8 ctrl = oc_getreg(i2c, OCI2C_CONTROL);
  186. /* make sure the device is disabled */
  187. oc_setreg(i2c, OCI2C_CONTROL, ctrl & ~(OCI2C_CTRL_EN|OCI2C_CTRL_IEN));
  188. prescale = (i2c->clock_khz / (5*100)) - 1;
  189. oc_setreg(i2c, OCI2C_PRELOW, prescale & 0xff);
  190. oc_setreg(i2c, OCI2C_PREHIGH, prescale >> 8);
  191. /* Init the device */
  192. oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_IACK);
  193. oc_setreg(i2c, OCI2C_CONTROL, ctrl | OCI2C_CTRL_IEN | OCI2C_CTRL_EN);
  194. }
  195. static u32 ocores_func(struct i2c_adapter *adap)
  196. {
  197. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  198. }
  199. static const struct i2c_algorithm ocores_algorithm = {
  200. .master_xfer = ocores_xfer,
  201. .functionality = ocores_func,
  202. };
  203. static struct i2c_adapter ocores_adapter = {
  204. .owner = THIS_MODULE,
  205. .name = "i2c-ocores",
  206. .class = I2C_CLASS_HWMON | I2C_CLASS_SPD | I2C_CLASS_DEPRECATED,
  207. .algo = &ocores_algorithm,
  208. };
  209. static const struct of_device_id ocores_i2c_match[] = {
  210. {
  211. .compatible = "opencores,i2c-ocores",
  212. .data = (void *)TYPE_OCORES,
  213. },
  214. {
  215. .compatible = "aeroflexgaisler,i2cmst",
  216. .data = (void *)TYPE_GRLIB,
  217. },
  218. {},
  219. };
  220. MODULE_DEVICE_TABLE(of, ocores_i2c_match);
  221. #ifdef CONFIG_OF
  222. /* Read and write functions for the GRLIB port of the controller. Registers are
  223. * 32-bit big endian and the PRELOW and PREHIGH registers are merged into one
  224. * register. The subsequent registers has their offset decreased accordingly. */
  225. static u8 oc_getreg_grlib(struct ocores_i2c *i2c, int reg)
  226. {
  227. u32 rd;
  228. int rreg = reg;
  229. if (reg != OCI2C_PRELOW)
  230. rreg--;
  231. rd = ioread32be(i2c->base + (rreg << i2c->reg_shift));
  232. if (reg == OCI2C_PREHIGH)
  233. return (u8)(rd >> 8);
  234. else
  235. return (u8)rd;
  236. }
  237. static void oc_setreg_grlib(struct ocores_i2c *i2c, int reg, u8 value)
  238. {
  239. u32 curr, wr;
  240. int rreg = reg;
  241. if (reg != OCI2C_PRELOW)
  242. rreg--;
  243. if (reg == OCI2C_PRELOW || reg == OCI2C_PREHIGH) {
  244. curr = ioread32be(i2c->base + (rreg << i2c->reg_shift));
  245. if (reg == OCI2C_PRELOW)
  246. wr = (curr & 0xff00) | value;
  247. else
  248. wr = (((u32)value) << 8) | (curr & 0xff);
  249. } else {
  250. wr = value;
  251. }
  252. iowrite32be(wr, i2c->base + (rreg << i2c->reg_shift));
  253. }
  254. static int ocores_i2c_of_probe(struct platform_device *pdev,
  255. struct ocores_i2c *i2c)
  256. {
  257. struct device_node *np = pdev->dev.of_node;
  258. const struct of_device_id *match;
  259. u32 val;
  260. if (of_property_read_u32(np, "reg-shift", &i2c->reg_shift)) {
  261. /* no 'reg-shift', check for deprecated 'regstep' */
  262. if (!of_property_read_u32(np, "regstep", &val)) {
  263. if (!is_power_of_2(val)) {
  264. dev_err(&pdev->dev, "invalid regstep %d\n",
  265. val);
  266. return -EINVAL;
  267. }
  268. i2c->reg_shift = ilog2(val);
  269. dev_warn(&pdev->dev,
  270. "regstep property deprecated, use reg-shift\n");
  271. }
  272. }
  273. if (of_property_read_u32(np, "clock-frequency", &val)) {
  274. dev_err(&pdev->dev,
  275. "Missing required parameter 'clock-frequency'\n");
  276. return -ENODEV;
  277. }
  278. i2c->clock_khz = val / 1000;
  279. of_property_read_u32(pdev->dev.of_node, "reg-io-width",
  280. &i2c->reg_io_width);
  281. match = of_match_node(ocores_i2c_match, pdev->dev.of_node);
  282. if (match && (long)match->data == TYPE_GRLIB) {
  283. dev_dbg(&pdev->dev, "GRLIB variant of i2c-ocores\n");
  284. i2c->setreg = oc_setreg_grlib;
  285. i2c->getreg = oc_getreg_grlib;
  286. }
  287. return 0;
  288. }
  289. #else
  290. #define ocores_i2c_of_probe(pdev,i2c) -ENODEV
  291. #endif
  292. static int ocores_i2c_probe(struct platform_device *pdev)
  293. {
  294. struct ocores_i2c *i2c;
  295. struct ocores_i2c_platform_data *pdata;
  296. struct resource *res;
  297. int irq;
  298. int ret;
  299. int i;
  300. irq = platform_get_irq(pdev, 0);
  301. if (irq < 0)
  302. return irq;
  303. i2c = devm_kzalloc(&pdev->dev, sizeof(*i2c), GFP_KERNEL);
  304. if (!i2c)
  305. return -ENOMEM;
  306. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  307. i2c->base = devm_ioremap_resource(&pdev->dev, res);
  308. if (IS_ERR(i2c->base))
  309. return PTR_ERR(i2c->base);
  310. pdata = dev_get_platdata(&pdev->dev);
  311. if (pdata) {
  312. i2c->reg_shift = pdata->reg_shift;
  313. i2c->reg_io_width = pdata->reg_io_width;
  314. i2c->clock_khz = pdata->clock_khz;
  315. } else {
  316. ret = ocores_i2c_of_probe(pdev, i2c);
  317. if (ret)
  318. return ret;
  319. }
  320. if (i2c->reg_io_width == 0)
  321. i2c->reg_io_width = 1; /* Set to default value */
  322. if (!i2c->setreg || !i2c->getreg) {
  323. switch (i2c->reg_io_width) {
  324. case 1:
  325. i2c->setreg = oc_setreg_8;
  326. i2c->getreg = oc_getreg_8;
  327. break;
  328. case 2:
  329. i2c->setreg = oc_setreg_16;
  330. i2c->getreg = oc_getreg_16;
  331. break;
  332. case 4:
  333. i2c->setreg = oc_setreg_32;
  334. i2c->getreg = oc_getreg_32;
  335. break;
  336. default:
  337. dev_err(&pdev->dev, "Unsupported I/O width (%d)\n",
  338. i2c->reg_io_width);
  339. return -EINVAL;
  340. }
  341. }
  342. ocores_init(i2c);
  343. init_waitqueue_head(&i2c->wait);
  344. ret = devm_request_irq(&pdev->dev, irq, ocores_isr, 0,
  345. pdev->name, i2c);
  346. if (ret) {
  347. dev_err(&pdev->dev, "Cannot claim IRQ\n");
  348. return ret;
  349. }
  350. /* hook up driver to tree */
  351. platform_set_drvdata(pdev, i2c);
  352. i2c->adap = ocores_adapter;
  353. i2c_set_adapdata(&i2c->adap, i2c);
  354. i2c->adap.dev.parent = &pdev->dev;
  355. i2c->adap.dev.of_node = pdev->dev.of_node;
  356. /* add i2c adapter to i2c tree */
  357. ret = i2c_add_adapter(&i2c->adap);
  358. if (ret) {
  359. dev_err(&pdev->dev, "Failed to add adapter\n");
  360. return ret;
  361. }
  362. /* add in known devices to the bus */
  363. if (pdata) {
  364. for (i = 0; i < pdata->num_devices; i++)
  365. i2c_new_device(&i2c->adap, pdata->devices + i);
  366. }
  367. return 0;
  368. }
  369. static int ocores_i2c_remove(struct platform_device *pdev)
  370. {
  371. struct ocores_i2c *i2c = platform_get_drvdata(pdev);
  372. /* disable i2c logic */
  373. oc_setreg(i2c, OCI2C_CONTROL, oc_getreg(i2c, OCI2C_CONTROL)
  374. & ~(OCI2C_CTRL_EN|OCI2C_CTRL_IEN));
  375. /* remove adapter & data */
  376. i2c_del_adapter(&i2c->adap);
  377. return 0;
  378. }
  379. #ifdef CONFIG_PM_SLEEP
  380. static int ocores_i2c_suspend(struct device *dev)
  381. {
  382. struct ocores_i2c *i2c = dev_get_drvdata(dev);
  383. u8 ctrl = oc_getreg(i2c, OCI2C_CONTROL);
  384. /* make sure the device is disabled */
  385. oc_setreg(i2c, OCI2C_CONTROL, ctrl & ~(OCI2C_CTRL_EN|OCI2C_CTRL_IEN));
  386. return 0;
  387. }
  388. static int ocores_i2c_resume(struct device *dev)
  389. {
  390. struct ocores_i2c *i2c = dev_get_drvdata(dev);
  391. ocores_init(i2c);
  392. return 0;
  393. }
  394. static SIMPLE_DEV_PM_OPS(ocores_i2c_pm, ocores_i2c_suspend, ocores_i2c_resume);
  395. #define OCORES_I2C_PM (&ocores_i2c_pm)
  396. #else
  397. #define OCORES_I2C_PM NULL
  398. #endif
  399. static struct platform_driver ocores_i2c_driver = {
  400. .probe = ocores_i2c_probe,
  401. .remove = ocores_i2c_remove,
  402. .driver = {
  403. .owner = THIS_MODULE,
  404. .name = "ocores-i2c",
  405. .of_match_table = ocores_i2c_match,
  406. .pm = OCORES_I2C_PM,
  407. },
  408. };
  409. module_platform_driver(ocores_i2c_driver);
  410. MODULE_AUTHOR("Peter Korsgaard <jacmet@sunsite.dk>");
  411. MODULE_DESCRIPTION("OpenCores I2C bus driver");
  412. MODULE_LICENSE("GPL");
  413. MODULE_ALIAS("platform:ocores-i2c");