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/err.h>
  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. void (*setreg)(struct ocores_i2c *i2c, int reg, u8 value);
  40. u8 (*getreg)(struct ocores_i2c *i2c, int reg);
  41. };
  42. /* registers */
  43. #define OCI2C_PRELOW 0
  44. #define OCI2C_PREHIGH 1
  45. #define OCI2C_CONTROL 2
  46. #define OCI2C_DATA 3
  47. #define OCI2C_CMD 4 /* write only */
  48. #define OCI2C_STATUS 4 /* read only, same address as OCI2C_CMD */
  49. #define OCI2C_CTRL_IEN 0x40
  50. #define OCI2C_CTRL_EN 0x80
  51. #define OCI2C_CMD_START 0x91
  52. #define OCI2C_CMD_STOP 0x41
  53. #define OCI2C_CMD_READ 0x21
  54. #define OCI2C_CMD_WRITE 0x11
  55. #define OCI2C_CMD_READ_ACK 0x21
  56. #define OCI2C_CMD_READ_NACK 0x29
  57. #define OCI2C_CMD_IACK 0x01
  58. #define OCI2C_STAT_IF 0x01
  59. #define OCI2C_STAT_TIP 0x02
  60. #define OCI2C_STAT_ARBLOST 0x20
  61. #define OCI2C_STAT_BUSY 0x40
  62. #define OCI2C_STAT_NACK 0x80
  63. #define STATE_DONE 0
  64. #define STATE_START 1
  65. #define STATE_WRITE 2
  66. #define STATE_READ 3
  67. #define STATE_ERROR 4
  68. #define TYPE_OCORES 0
  69. #define TYPE_GRLIB 1
  70. static void oc_setreg_8(struct ocores_i2c *i2c, int reg, u8 value)
  71. {
  72. iowrite8(value, i2c->base + (reg << i2c->reg_shift));
  73. }
  74. static void oc_setreg_16(struct ocores_i2c *i2c, int reg, u8 value)
  75. {
  76. iowrite16(value, i2c->base + (reg << i2c->reg_shift));
  77. }
  78. static void oc_setreg_32(struct ocores_i2c *i2c, int reg, u8 value)
  79. {
  80. iowrite32(value, i2c->base + (reg << i2c->reg_shift));
  81. }
  82. static inline u8 oc_getreg_8(struct ocores_i2c *i2c, int reg)
  83. {
  84. return ioread8(i2c->base + (reg << i2c->reg_shift));
  85. }
  86. static inline u8 oc_getreg_16(struct ocores_i2c *i2c, int reg)
  87. {
  88. return ioread16(i2c->base + (reg << i2c->reg_shift));
  89. }
  90. static inline u8 oc_getreg_32(struct ocores_i2c *i2c, int reg)
  91. {
  92. return ioread32(i2c->base + (reg << i2c->reg_shift));
  93. }
  94. static inline void oc_setreg(struct ocores_i2c *i2c, int reg, u8 value)
  95. {
  96. i2c->setreg(i2c, reg, value);
  97. }
  98. static inline u8 oc_getreg(struct ocores_i2c *i2c, int reg)
  99. {
  100. return i2c->getreg(i2c, reg);
  101. }
  102. static void ocores_process(struct ocores_i2c *i2c)
  103. {
  104. struct i2c_msg *msg = i2c->msg;
  105. u8 stat = oc_getreg(i2c, OCI2C_STATUS);
  106. if ((i2c->state == STATE_DONE) || (i2c->state == STATE_ERROR)) {
  107. /* stop has been sent */
  108. oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_IACK);
  109. wake_up(&i2c->wait);
  110. return;
  111. }
  112. /* error? */
  113. if (stat & OCI2C_STAT_ARBLOST) {
  114. i2c->state = STATE_ERROR;
  115. oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_STOP);
  116. return;
  117. }
  118. if ((i2c->state == STATE_START) || (i2c->state == STATE_WRITE)) {
  119. i2c->state =
  120. (msg->flags & I2C_M_RD) ? STATE_READ : STATE_WRITE;
  121. if (stat & OCI2C_STAT_NACK) {
  122. i2c->state = STATE_ERROR;
  123. oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_STOP);
  124. return;
  125. }
  126. } else
  127. msg->buf[i2c->pos++] = oc_getreg(i2c, OCI2C_DATA);
  128. /* end of msg? */
  129. if (i2c->pos == msg->len) {
  130. i2c->nmsgs--;
  131. i2c->msg++;
  132. i2c->pos = 0;
  133. msg = i2c->msg;
  134. if (i2c->nmsgs) { /* end? */
  135. /* send start? */
  136. if (!(msg->flags & I2C_M_NOSTART)) {
  137. u8 addr = (msg->addr << 1);
  138. if (msg->flags & I2C_M_RD)
  139. addr |= 1;
  140. i2c->state = STATE_START;
  141. oc_setreg(i2c, OCI2C_DATA, addr);
  142. oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_START);
  143. return;
  144. } else
  145. i2c->state = (msg->flags & I2C_M_RD)
  146. ? STATE_READ : STATE_WRITE;
  147. } else {
  148. i2c->state = STATE_DONE;
  149. oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_STOP);
  150. return;
  151. }
  152. }
  153. if (i2c->state == STATE_READ) {
  154. oc_setreg(i2c, OCI2C_CMD, i2c->pos == (msg->len-1) ?
  155. OCI2C_CMD_READ_NACK : OCI2C_CMD_READ_ACK);
  156. } else {
  157. oc_setreg(i2c, OCI2C_DATA, msg->buf[i2c->pos++]);
  158. oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_WRITE);
  159. }
  160. }
  161. static irqreturn_t ocores_isr(int irq, void *dev_id)
  162. {
  163. struct ocores_i2c *i2c = dev_id;
  164. ocores_process(i2c);
  165. return IRQ_HANDLED;
  166. }
  167. static int ocores_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
  168. {
  169. struct ocores_i2c *i2c = i2c_get_adapdata(adap);
  170. i2c->msg = msgs;
  171. i2c->pos = 0;
  172. i2c->nmsgs = num;
  173. i2c->state = STATE_START;
  174. oc_setreg(i2c, OCI2C_DATA,
  175. (i2c->msg->addr << 1) |
  176. ((i2c->msg->flags & I2C_M_RD) ? 1:0));
  177. oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_START);
  178. if (wait_event_timeout(i2c->wait, (i2c->state == STATE_ERROR) ||
  179. (i2c->state == STATE_DONE), HZ))
  180. return (i2c->state == STATE_DONE) ? num : -EIO;
  181. else
  182. return -ETIMEDOUT;
  183. }
  184. static void ocores_init(struct ocores_i2c *i2c)
  185. {
  186. int prescale;
  187. u8 ctrl = oc_getreg(i2c, OCI2C_CONTROL);
  188. /* make sure the device is disabled */
  189. oc_setreg(i2c, OCI2C_CONTROL, ctrl & ~(OCI2C_CTRL_EN|OCI2C_CTRL_IEN));
  190. prescale = (i2c->clock_khz / (5*100)) - 1;
  191. oc_setreg(i2c, OCI2C_PRELOW, prescale & 0xff);
  192. oc_setreg(i2c, OCI2C_PREHIGH, prescale >> 8);
  193. /* Init the device */
  194. oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_IACK);
  195. oc_setreg(i2c, OCI2C_CONTROL, ctrl | OCI2C_CTRL_IEN | OCI2C_CTRL_EN);
  196. }
  197. static u32 ocores_func(struct i2c_adapter *adap)
  198. {
  199. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  200. }
  201. static const struct i2c_algorithm ocores_algorithm = {
  202. .master_xfer = ocores_xfer,
  203. .functionality = ocores_func,
  204. };
  205. static struct i2c_adapter ocores_adapter = {
  206. .owner = THIS_MODULE,
  207. .name = "i2c-ocores",
  208. .class = I2C_CLASS_HWMON | I2C_CLASS_SPD,
  209. .algo = &ocores_algorithm,
  210. };
  211. static struct of_device_id ocores_i2c_match[] = {
  212. {
  213. .compatible = "opencores,i2c-ocores",
  214. .data = (void *)TYPE_OCORES,
  215. },
  216. {
  217. .compatible = "aeroflexgaisler,i2cmst",
  218. .data = (void *)TYPE_GRLIB,
  219. },
  220. {},
  221. };
  222. MODULE_DEVICE_TABLE(of, ocores_i2c_match);
  223. #ifdef CONFIG_OF
  224. /* Read and write functions for the GRLIB port of the controller. Registers are
  225. * 32-bit big endian and the PRELOW and PREHIGH registers are merged into one
  226. * register. The subsequent registers has their offset decreased accordingly. */
  227. static u8 oc_getreg_grlib(struct ocores_i2c *i2c, int reg)
  228. {
  229. u32 rd;
  230. int rreg = reg;
  231. if (reg != OCI2C_PRELOW)
  232. rreg--;
  233. rd = ioread32be(i2c->base + (rreg << i2c->reg_shift));
  234. if (reg == OCI2C_PREHIGH)
  235. return (u8)(rd >> 8);
  236. else
  237. return (u8)rd;
  238. }
  239. static void oc_setreg_grlib(struct ocores_i2c *i2c, int reg, u8 value)
  240. {
  241. u32 curr, wr;
  242. int rreg = reg;
  243. if (reg != OCI2C_PRELOW)
  244. rreg--;
  245. if (reg == OCI2C_PRELOW || reg == OCI2C_PREHIGH) {
  246. curr = ioread32be(i2c->base + (rreg << i2c->reg_shift));
  247. if (reg == OCI2C_PRELOW)
  248. wr = (curr & 0xff00) | value;
  249. else
  250. wr = (((u32)value) << 8) | (curr & 0xff);
  251. } else {
  252. wr = value;
  253. }
  254. iowrite32be(wr, i2c->base + (rreg << i2c->reg_shift));
  255. }
  256. static int ocores_i2c_of_probe(struct platform_device *pdev,
  257. struct ocores_i2c *i2c)
  258. {
  259. struct device_node *np = pdev->dev.of_node;
  260. const struct of_device_id *match;
  261. u32 val;
  262. if (of_property_read_u32(np, "reg-shift", &i2c->reg_shift)) {
  263. /* no 'reg-shift', check for deprecated 'regstep' */
  264. if (!of_property_read_u32(np, "regstep", &val)) {
  265. if (!is_power_of_2(val)) {
  266. dev_err(&pdev->dev, "invalid regstep %d\n",
  267. val);
  268. return -EINVAL;
  269. }
  270. i2c->reg_shift = ilog2(val);
  271. dev_warn(&pdev->dev,
  272. "regstep property deprecated, use reg-shift\n");
  273. }
  274. }
  275. if (of_property_read_u32(np, "clock-frequency", &val)) {
  276. dev_err(&pdev->dev,
  277. "Missing required parameter 'clock-frequency'\n");
  278. return -ENODEV;
  279. }
  280. i2c->clock_khz = val / 1000;
  281. of_property_read_u32(pdev->dev.of_node, "reg-io-width",
  282. &i2c->reg_io_width);
  283. match = of_match_node(ocores_i2c_match, pdev->dev.of_node);
  284. if (match && (long)match->data == TYPE_GRLIB) {
  285. dev_dbg(&pdev->dev, "GRLIB variant of i2c-ocores\n");
  286. i2c->setreg = oc_setreg_grlib;
  287. i2c->getreg = oc_getreg_grlib;
  288. }
  289. return 0;
  290. }
  291. #else
  292. #define ocores_i2c_of_probe(pdev,i2c) -ENODEV
  293. #endif
  294. static int ocores_i2c_probe(struct platform_device *pdev)
  295. {
  296. struct ocores_i2c *i2c;
  297. struct ocores_i2c_platform_data *pdata;
  298. struct resource *res;
  299. int irq;
  300. int ret;
  301. int i;
  302. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  303. if (!res)
  304. return -ENODEV;
  305. irq = platform_get_irq(pdev, 0);
  306. if (irq < 0)
  307. return irq;
  308. i2c = devm_kzalloc(&pdev->dev, sizeof(*i2c), GFP_KERNEL);
  309. if (!i2c)
  310. return -ENOMEM;
  311. i2c->base = devm_ioremap_resource(&pdev->dev, res);
  312. if (IS_ERR(i2c->base))
  313. return PTR_ERR(i2c->base);
  314. pdata = pdev->dev.platform_data;
  315. if (pdata) {
  316. i2c->reg_shift = pdata->reg_shift;
  317. i2c->reg_io_width = pdata->reg_io_width;
  318. i2c->clock_khz = pdata->clock_khz;
  319. } else {
  320. ret = ocores_i2c_of_probe(pdev, i2c);
  321. if (ret)
  322. return ret;
  323. }
  324. if (i2c->reg_io_width == 0)
  325. i2c->reg_io_width = 1; /* Set to default value */
  326. if (!i2c->setreg || !i2c->getreg) {
  327. switch (i2c->reg_io_width) {
  328. case 1:
  329. i2c->setreg = oc_setreg_8;
  330. i2c->getreg = oc_getreg_8;
  331. break;
  332. case 2:
  333. i2c->setreg = oc_setreg_16;
  334. i2c->getreg = oc_getreg_16;
  335. break;
  336. case 4:
  337. i2c->setreg = oc_setreg_32;
  338. i2c->getreg = oc_getreg_32;
  339. break;
  340. default:
  341. dev_err(&pdev->dev, "Unsupported I/O width (%d)\n",
  342. i2c->reg_io_width);
  343. return -EINVAL;
  344. }
  345. }
  346. ocores_init(i2c);
  347. init_waitqueue_head(&i2c->wait);
  348. ret = devm_request_irq(&pdev->dev, irq, ocores_isr, 0,
  349. pdev->name, i2c);
  350. if (ret) {
  351. dev_err(&pdev->dev, "Cannot claim IRQ\n");
  352. return ret;
  353. }
  354. /* hook up driver to tree */
  355. platform_set_drvdata(pdev, i2c);
  356. i2c->adap = ocores_adapter;
  357. i2c_set_adapdata(&i2c->adap, i2c);
  358. i2c->adap.dev.parent = &pdev->dev;
  359. i2c->adap.dev.of_node = pdev->dev.of_node;
  360. /* add i2c adapter to i2c tree */
  361. ret = i2c_add_adapter(&i2c->adap);
  362. if (ret) {
  363. dev_err(&pdev->dev, "Failed to add adapter\n");
  364. return ret;
  365. }
  366. /* add in known devices to the bus */
  367. if (pdata) {
  368. for (i = 0; i < pdata->num_devices; i++)
  369. i2c_new_device(&i2c->adap, pdata->devices + i);
  370. } else {
  371. of_i2c_register_devices(&i2c->adap);
  372. }
  373. return 0;
  374. }
  375. static int ocores_i2c_remove(struct platform_device *pdev)
  376. {
  377. struct ocores_i2c *i2c = platform_get_drvdata(pdev);
  378. /* disable i2c logic */
  379. oc_setreg(i2c, OCI2C_CONTROL, oc_getreg(i2c, OCI2C_CONTROL)
  380. & ~(OCI2C_CTRL_EN|OCI2C_CTRL_IEN));
  381. /* remove adapter & data */
  382. i2c_del_adapter(&i2c->adap);
  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");