i2c-ocores.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/init.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/of_i2c.h>
  26. #include <linux/log2.h>
  27. struct ocores_i2c {
  28. void __iomem *base;
  29. u32 reg_shift;
  30. u32 reg_io_width;
  31. wait_queue_head_t wait;
  32. struct i2c_adapter adap;
  33. struct i2c_msg *msg;
  34. int pos;
  35. int nmsgs;
  36. int state; /* see STATE_ */
  37. int clock_khz;
  38. void (*setreg)(struct ocores_i2c *i2c, int reg, u8 value);
  39. u8 (*getreg)(struct ocores_i2c *i2c, int reg);
  40. };
  41. /* registers */
  42. #define OCI2C_PRELOW 0
  43. #define OCI2C_PREHIGH 1
  44. #define OCI2C_CONTROL 2
  45. #define OCI2C_DATA 3
  46. #define OCI2C_CMD 4 /* write only */
  47. #define OCI2C_STATUS 4 /* read only, same address as OCI2C_CMD */
  48. #define OCI2C_CTRL_IEN 0x40
  49. #define OCI2C_CTRL_EN 0x80
  50. #define OCI2C_CMD_START 0x91
  51. #define OCI2C_CMD_STOP 0x41
  52. #define OCI2C_CMD_READ 0x21
  53. #define OCI2C_CMD_WRITE 0x11
  54. #define OCI2C_CMD_READ_ACK 0x21
  55. #define OCI2C_CMD_READ_NACK 0x29
  56. #define OCI2C_CMD_IACK 0x01
  57. #define OCI2C_STAT_IF 0x01
  58. #define OCI2C_STAT_TIP 0x02
  59. #define OCI2C_STAT_ARBLOST 0x20
  60. #define OCI2C_STAT_BUSY 0x40
  61. #define OCI2C_STAT_NACK 0x80
  62. #define STATE_DONE 0
  63. #define STATE_START 1
  64. #define STATE_WRITE 2
  65. #define STATE_READ 3
  66. #define STATE_ERROR 4
  67. #define TYPE_OCORES 0
  68. #define TYPE_GRLIB 1
  69. static void oc_setreg_8(struct ocores_i2c *i2c, int reg, u8 value)
  70. {
  71. iowrite8(value, i2c->base + (reg << i2c->reg_shift));
  72. }
  73. static void oc_setreg_16(struct ocores_i2c *i2c, int reg, u8 value)
  74. {
  75. iowrite16(value, i2c->base + (reg << i2c->reg_shift));
  76. }
  77. static void oc_setreg_32(struct ocores_i2c *i2c, int reg, u8 value)
  78. {
  79. iowrite32(value, i2c->base + (reg << i2c->reg_shift));
  80. }
  81. static inline u8 oc_getreg_8(struct ocores_i2c *i2c, int reg)
  82. {
  83. return ioread8(i2c->base + (reg << i2c->reg_shift));
  84. }
  85. static inline u8 oc_getreg_16(struct ocores_i2c *i2c, int reg)
  86. {
  87. return ioread16(i2c->base + (reg << i2c->reg_shift));
  88. }
  89. static inline u8 oc_getreg_32(struct ocores_i2c *i2c, int reg)
  90. {
  91. return ioread32(i2c->base + (reg << i2c->reg_shift));
  92. }
  93. static inline void oc_setreg(struct ocores_i2c *i2c, int reg, u8 value)
  94. {
  95. i2c->setreg(i2c, reg, value);
  96. }
  97. static inline u8 oc_getreg(struct ocores_i2c *i2c, int reg)
  98. {
  99. return i2c->getreg(i2c, reg);
  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. static struct of_device_id ocores_i2c_match[] = {
  211. {
  212. .compatible = "opencores,i2c-ocores",
  213. .data = (void *)TYPE_OCORES,
  214. },
  215. {
  216. .compatible = "aeroflexgaisler,i2cmst",
  217. .data = (void *)TYPE_GRLIB,
  218. },
  219. {},
  220. };
  221. MODULE_DEVICE_TABLE(of, ocores_i2c_match);
  222. #ifdef CONFIG_OF
  223. /* Read and write functions for the GRLIB port of the controller. Registers are
  224. * 32-bit big endian and the PRELOW and PREHIGH registers are merged into one
  225. * register. The subsequent registers has their offset decreased accordingly. */
  226. static u8 oc_getreg_grlib(struct ocores_i2c *i2c, int reg)
  227. {
  228. u32 rd;
  229. int rreg = reg;
  230. if (reg != OCI2C_PRELOW)
  231. rreg--;
  232. rd = ioread32be(i2c->base + (rreg << i2c->reg_shift));
  233. if (reg == OCI2C_PREHIGH)
  234. return (u8)(rd >> 8);
  235. else
  236. return (u8)rd;
  237. }
  238. static void oc_setreg_grlib(struct ocores_i2c *i2c, int reg, u8 value)
  239. {
  240. u32 curr, wr;
  241. int rreg = reg;
  242. if (reg != OCI2C_PRELOW)
  243. rreg--;
  244. if (reg == OCI2C_PRELOW || reg == OCI2C_PREHIGH) {
  245. curr = ioread32be(i2c->base + (rreg << i2c->reg_shift));
  246. if (reg == OCI2C_PRELOW)
  247. wr = (curr & 0xff00) | value;
  248. else
  249. wr = (((u32)value) << 8) | (curr & 0xff);
  250. } else {
  251. wr = value;
  252. }
  253. iowrite32be(wr, i2c->base + (rreg << i2c->reg_shift));
  254. }
  255. static int ocores_i2c_of_probe(struct platform_device *pdev,
  256. struct ocores_i2c *i2c)
  257. {
  258. struct device_node *np = pdev->dev.of_node;
  259. const struct of_device_id *match;
  260. u32 val;
  261. if (of_property_read_u32(np, "reg-shift", &i2c->reg_shift)) {
  262. /* no 'reg-shift', check for deprecated 'regstep' */
  263. if (!of_property_read_u32(np, "regstep", &val)) {
  264. if (!is_power_of_2(val)) {
  265. dev_err(&pdev->dev, "invalid regstep %d\n",
  266. val);
  267. return -EINVAL;
  268. }
  269. i2c->reg_shift = ilog2(val);
  270. dev_warn(&pdev->dev,
  271. "regstep property deprecated, use reg-shift\n");
  272. }
  273. }
  274. if (of_property_read_u32(np, "clock-frequency", &val)) {
  275. dev_err(&pdev->dev,
  276. "Missing required parameter 'clock-frequency'\n");
  277. return -ENODEV;
  278. }
  279. i2c->clock_khz = val / 1000;
  280. of_property_read_u32(pdev->dev.of_node, "reg-io-width",
  281. &i2c->reg_io_width);
  282. match = of_match_node(ocores_i2c_match, pdev->dev.of_node);
  283. if (match && (int)match->data == TYPE_GRLIB) {
  284. dev_dbg(&pdev->dev, "GRLIB variant of i2c-ocores\n");
  285. i2c->setreg = oc_setreg_grlib;
  286. i2c->getreg = oc_getreg_grlib;
  287. }
  288. return 0;
  289. }
  290. #else
  291. #define ocores_i2c_of_probe(pdev,i2c) -ENODEV
  292. #endif
  293. static int ocores_i2c_probe(struct platform_device *pdev)
  294. {
  295. struct ocores_i2c *i2c;
  296. struct ocores_i2c_platform_data *pdata;
  297. struct resource *res;
  298. int irq;
  299. int ret;
  300. int i;
  301. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  302. if (!res)
  303. return -ENODEV;
  304. irq = platform_get_irq(pdev, 0);
  305. if (irq < 0)
  306. return irq;
  307. i2c = devm_kzalloc(&pdev->dev, sizeof(*i2c), GFP_KERNEL);
  308. if (!i2c)
  309. return -ENOMEM;
  310. i2c->base = devm_request_and_ioremap(&pdev->dev, res);
  311. if (!i2c->base)
  312. return -EADDRNOTAVAIL;
  313. pdata = pdev->dev.platform_data;
  314. if (pdata) {
  315. i2c->reg_shift = pdata->reg_shift;
  316. i2c->reg_io_width = pdata->reg_io_width;
  317. i2c->clock_khz = pdata->clock_khz;
  318. } else {
  319. ret = ocores_i2c_of_probe(pdev, i2c);
  320. if (ret)
  321. return ret;
  322. }
  323. if (i2c->reg_io_width == 0)
  324. i2c->reg_io_width = 1; /* Set to default value */
  325. if (!i2c->setreg || !i2c->getreg) {
  326. switch (i2c->reg_io_width) {
  327. case 1:
  328. i2c->setreg = oc_setreg_8;
  329. i2c->getreg = oc_getreg_8;
  330. break;
  331. case 2:
  332. i2c->setreg = oc_setreg_16;
  333. i2c->getreg = oc_getreg_16;
  334. break;
  335. case 4:
  336. i2c->setreg = oc_setreg_32;
  337. i2c->getreg = oc_getreg_32;
  338. break;
  339. default:
  340. dev_err(&pdev->dev, "Unsupported I/O width (%d)\n",
  341. i2c->reg_io_width);
  342. return -EINVAL;
  343. }
  344. }
  345. ocores_init(i2c);
  346. init_waitqueue_head(&i2c->wait);
  347. ret = devm_request_irq(&pdev->dev, irq, ocores_isr, 0,
  348. pdev->name, i2c);
  349. if (ret) {
  350. dev_err(&pdev->dev, "Cannot claim IRQ\n");
  351. return ret;
  352. }
  353. /* hook up driver to tree */
  354. platform_set_drvdata(pdev, i2c);
  355. i2c->adap = ocores_adapter;
  356. i2c_set_adapdata(&i2c->adap, i2c);
  357. i2c->adap.dev.parent = &pdev->dev;
  358. i2c->adap.dev.of_node = pdev->dev.of_node;
  359. /* add i2c adapter to i2c tree */
  360. ret = i2c_add_adapter(&i2c->adap);
  361. if (ret) {
  362. dev_err(&pdev->dev, "Failed to add adapter\n");
  363. return ret;
  364. }
  365. /* add in known devices to the bus */
  366. if (pdata) {
  367. for (i = 0; i < pdata->num_devices; i++)
  368. i2c_new_device(&i2c->adap, pdata->devices + i);
  369. } else {
  370. of_i2c_register_devices(&i2c->adap);
  371. }
  372. return 0;
  373. }
  374. static int ocores_i2c_remove(struct platform_device *pdev)
  375. {
  376. struct ocores_i2c *i2c = platform_get_drvdata(pdev);
  377. /* disable i2c logic */
  378. oc_setreg(i2c, OCI2C_CONTROL, oc_getreg(i2c, OCI2C_CONTROL)
  379. & ~(OCI2C_CTRL_EN|OCI2C_CTRL_IEN));
  380. /* remove adapter & data */
  381. i2c_del_adapter(&i2c->adap);
  382. platform_set_drvdata(pdev, NULL);
  383. return 0;
  384. }
  385. #ifdef CONFIG_PM
  386. static int ocores_i2c_suspend(struct device *dev)
  387. {
  388. struct ocores_i2c *i2c = dev_get_drvdata(dev);
  389. u8 ctrl = oc_getreg(i2c, OCI2C_CONTROL);
  390. /* make sure the device is disabled */
  391. oc_setreg(i2c, OCI2C_CONTROL, ctrl & ~(OCI2C_CTRL_EN|OCI2C_CTRL_IEN));
  392. return 0;
  393. }
  394. static int ocores_i2c_resume(struct device *dev)
  395. {
  396. struct ocores_i2c *i2c = dev_get_drvdata(dev);
  397. ocores_init(i2c);
  398. return 0;
  399. }
  400. static SIMPLE_DEV_PM_OPS(ocores_i2c_pm, ocores_i2c_suspend, ocores_i2c_resume);
  401. #define OCORES_I2C_PM (&ocores_i2c_pm)
  402. #else
  403. #define OCORES_I2C_PM NULL
  404. #endif
  405. static struct platform_driver ocores_i2c_driver = {
  406. .probe = ocores_i2c_probe,
  407. .remove = ocores_i2c_remove,
  408. .driver = {
  409. .owner = THIS_MODULE,
  410. .name = "ocores-i2c",
  411. .of_match_table = ocores_i2c_match,
  412. .pm = OCORES_I2C_PM,
  413. },
  414. };
  415. module_platform_driver(ocores_i2c_driver);
  416. MODULE_AUTHOR("Peter Korsgaard <jacmet@sunsite.dk>");
  417. MODULE_DESCRIPTION("OpenCores I2C bus driver");
  418. MODULE_LICENSE("GPL");
  419. MODULE_ALIAS("platform:ocores-i2c");