i2c-ocores.c 13 KB

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