i2c-ocores.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * i2c-ocores.c: I2C bus driver for OpenCores I2C controller
  4. * (https://opencores.org/project/i2c/overview)
  5. *
  6. * Peter Korsgaard <peter@korsgaard.com>
  7. *
  8. * Support for the GRLIB port of the controller by
  9. * Andreas Larsson <andreas@gaisler.com>
  10. */
  11. #include <linux/clk.h>
  12. #include <linux/delay.h>
  13. #include <linux/err.h>
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/errno.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/i2c.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/wait.h>
  21. #include <linux/platform_data/i2c-ocores.h>
  22. #include <linux/slab.h>
  23. #include <linux/io.h>
  24. #include <linux/log2.h>
  25. #include <linux/spinlock.h>
  26. #include <linux/jiffies.h>
  27. /*
  28. * 'process_lock' exists because ocores_process() and ocores_process_timeout()
  29. * can't run in parallel.
  30. */
  31. struct ocores_i2c {
  32. void __iomem *base;
  33. u32 reg_shift;
  34. u32 reg_io_width;
  35. unsigned long flags;
  36. wait_queue_head_t wait;
  37. struct i2c_adapter adap;
  38. struct i2c_msg *msg;
  39. int pos;
  40. int nmsgs;
  41. int state; /* see STATE_ */
  42. spinlock_t process_lock;
  43. struct clk *clk;
  44. int ip_clock_khz;
  45. int bus_clock_khz;
  46. void (*setreg)(struct ocores_i2c *i2c, int reg, u8 value);
  47. u8 (*getreg)(struct ocores_i2c *i2c, int reg);
  48. };
  49. /* registers */
  50. #define OCI2C_PRELOW 0
  51. #define OCI2C_PREHIGH 1
  52. #define OCI2C_CONTROL 2
  53. #define OCI2C_DATA 3
  54. #define OCI2C_CMD 4 /* write only */
  55. #define OCI2C_STATUS 4 /* read only, same address as OCI2C_CMD */
  56. #define OCI2C_CTRL_IEN 0x40
  57. #define OCI2C_CTRL_EN 0x80
  58. #define OCI2C_CMD_START 0x91
  59. #define OCI2C_CMD_STOP 0x41
  60. #define OCI2C_CMD_READ 0x21
  61. #define OCI2C_CMD_WRITE 0x11
  62. #define OCI2C_CMD_READ_ACK 0x21
  63. #define OCI2C_CMD_READ_NACK 0x29
  64. #define OCI2C_CMD_IACK 0x01
  65. #define OCI2C_STAT_IF 0x01
  66. #define OCI2C_STAT_TIP 0x02
  67. #define OCI2C_STAT_ARBLOST 0x20
  68. #define OCI2C_STAT_BUSY 0x40
  69. #define OCI2C_STAT_NACK 0x80
  70. #define STATE_DONE 0
  71. #define STATE_START 1
  72. #define STATE_WRITE 2
  73. #define STATE_READ 3
  74. #define STATE_ERROR 4
  75. #define TYPE_OCORES 0
  76. #define TYPE_GRLIB 1
  77. #define OCORES_FLAG_BROKEN_IRQ BIT(1) /* Broken IRQ for FU540-C000 SoC */
  78. static void oc_setreg_8(struct ocores_i2c *i2c, int reg, u8 value)
  79. {
  80. iowrite8(value, i2c->base + (reg << i2c->reg_shift));
  81. }
  82. static void oc_setreg_16(struct ocores_i2c *i2c, int reg, u8 value)
  83. {
  84. iowrite16(value, i2c->base + (reg << i2c->reg_shift));
  85. }
  86. static void oc_setreg_32(struct ocores_i2c *i2c, int reg, u8 value)
  87. {
  88. iowrite32(value, i2c->base + (reg << i2c->reg_shift));
  89. }
  90. static void oc_setreg_16be(struct ocores_i2c *i2c, int reg, u8 value)
  91. {
  92. iowrite16be(value, i2c->base + (reg << i2c->reg_shift));
  93. }
  94. static void oc_setreg_32be(struct ocores_i2c *i2c, int reg, u8 value)
  95. {
  96. iowrite32be(value, i2c->base + (reg << i2c->reg_shift));
  97. }
  98. static inline u8 oc_getreg_8(struct ocores_i2c *i2c, int reg)
  99. {
  100. return ioread8(i2c->base + (reg << i2c->reg_shift));
  101. }
  102. static inline u8 oc_getreg_16(struct ocores_i2c *i2c, int reg)
  103. {
  104. return ioread16(i2c->base + (reg << i2c->reg_shift));
  105. }
  106. static inline u8 oc_getreg_32(struct ocores_i2c *i2c, int reg)
  107. {
  108. return ioread32(i2c->base + (reg << i2c->reg_shift));
  109. }
  110. static inline u8 oc_getreg_16be(struct ocores_i2c *i2c, int reg)
  111. {
  112. return ioread16be(i2c->base + (reg << i2c->reg_shift));
  113. }
  114. static inline u8 oc_getreg_32be(struct ocores_i2c *i2c, int reg)
  115. {
  116. return ioread32be(i2c->base + (reg << i2c->reg_shift));
  117. }
  118. static inline void oc_setreg(struct ocores_i2c *i2c, int reg, u8 value)
  119. {
  120. i2c->setreg(i2c, reg, value);
  121. }
  122. static inline u8 oc_getreg(struct ocores_i2c *i2c, int reg)
  123. {
  124. return i2c->getreg(i2c, reg);
  125. }
  126. static void ocores_process(struct ocores_i2c *i2c, u8 stat)
  127. {
  128. struct i2c_msg *msg = i2c->msg;
  129. unsigned long flags;
  130. /*
  131. * If we spin here is because we are in timeout, so we are going
  132. * to be in STATE_ERROR. See ocores_process_timeout()
  133. */
  134. spin_lock_irqsave(&i2c->process_lock, flags);
  135. if ((i2c->state == STATE_DONE) || (i2c->state == STATE_ERROR)) {
  136. /* stop has been sent */
  137. oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_IACK);
  138. wake_up(&i2c->wait);
  139. goto out;
  140. }
  141. /* error? */
  142. if (stat & OCI2C_STAT_ARBLOST) {
  143. i2c->state = STATE_ERROR;
  144. oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_STOP);
  145. goto out;
  146. }
  147. if ((i2c->state == STATE_START) || (i2c->state == STATE_WRITE)) {
  148. i2c->state =
  149. (msg->flags & I2C_M_RD) ? STATE_READ : STATE_WRITE;
  150. if (stat & OCI2C_STAT_NACK) {
  151. i2c->state = STATE_ERROR;
  152. oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_STOP);
  153. goto out;
  154. }
  155. } else {
  156. msg->buf[i2c->pos++] = oc_getreg(i2c, OCI2C_DATA);
  157. }
  158. /* end of msg? */
  159. if (i2c->pos == msg->len) {
  160. i2c->nmsgs--;
  161. i2c->msg++;
  162. i2c->pos = 0;
  163. msg = i2c->msg;
  164. if (i2c->nmsgs) { /* end? */
  165. /* send start? */
  166. if (!(msg->flags & I2C_M_NOSTART)) {
  167. u8 addr = i2c_8bit_addr_from_msg(msg);
  168. i2c->state = STATE_START;
  169. oc_setreg(i2c, OCI2C_DATA, addr);
  170. oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_START);
  171. goto out;
  172. }
  173. i2c->state = (msg->flags & I2C_M_RD)
  174. ? STATE_READ : STATE_WRITE;
  175. } else {
  176. i2c->state = STATE_DONE;
  177. oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_STOP);
  178. goto out;
  179. }
  180. }
  181. if (i2c->state == STATE_READ) {
  182. oc_setreg(i2c, OCI2C_CMD, i2c->pos == (msg->len-1) ?
  183. OCI2C_CMD_READ_NACK : OCI2C_CMD_READ_ACK);
  184. } else {
  185. oc_setreg(i2c, OCI2C_DATA, msg->buf[i2c->pos++]);
  186. oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_WRITE);
  187. }
  188. out:
  189. spin_unlock_irqrestore(&i2c->process_lock, flags);
  190. }
  191. static irqreturn_t ocores_isr(int irq, void *dev_id)
  192. {
  193. struct ocores_i2c *i2c = dev_id;
  194. u8 stat = oc_getreg(i2c, OCI2C_STATUS);
  195. if (i2c->flags & OCORES_FLAG_BROKEN_IRQ) {
  196. if ((stat & OCI2C_STAT_IF) && !(stat & OCI2C_STAT_BUSY))
  197. return IRQ_NONE;
  198. } else if (!(stat & OCI2C_STAT_IF)) {
  199. return IRQ_NONE;
  200. }
  201. ocores_process(i2c, stat);
  202. return IRQ_HANDLED;
  203. }
  204. /**
  205. * ocores_process_timeout() - Process timeout event
  206. * @i2c: ocores I2C device instance
  207. */
  208. static void ocores_process_timeout(struct ocores_i2c *i2c)
  209. {
  210. unsigned long flags;
  211. spin_lock_irqsave(&i2c->process_lock, flags);
  212. i2c->state = STATE_ERROR;
  213. oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_STOP);
  214. spin_unlock_irqrestore(&i2c->process_lock, flags);
  215. }
  216. /**
  217. * ocores_wait() - Wait until something change in a given register
  218. * @i2c: ocores I2C device instance
  219. * @reg: register to query
  220. * @mask: bitmask to apply on register value
  221. * @val: expected result
  222. * @timeout: timeout in jiffies
  223. *
  224. * Timeout is necessary to avoid to stay here forever when the chip
  225. * does not answer correctly.
  226. *
  227. * Return: 0 on success, -ETIMEDOUT on timeout
  228. */
  229. static int ocores_wait(struct ocores_i2c *i2c,
  230. int reg, u8 mask, u8 val,
  231. const unsigned long timeout)
  232. {
  233. unsigned long j;
  234. j = jiffies + timeout;
  235. while (1) {
  236. u8 status = oc_getreg(i2c, reg);
  237. if ((status & mask) == val)
  238. break;
  239. if (time_after(jiffies, j))
  240. return -ETIMEDOUT;
  241. }
  242. return 0;
  243. }
  244. /**
  245. * ocores_poll_wait() - Wait until is possible to process some data
  246. * @i2c: ocores I2C device instance
  247. *
  248. * Used when the device is in polling mode (interrupts disabled).
  249. *
  250. * Return: 0 on success, -ETIMEDOUT on timeout
  251. */
  252. static int ocores_poll_wait(struct ocores_i2c *i2c)
  253. {
  254. u8 mask;
  255. int err;
  256. if (i2c->state == STATE_DONE || i2c->state == STATE_ERROR) {
  257. /* transfer is over */
  258. mask = OCI2C_STAT_BUSY;
  259. } else {
  260. /* on going transfer */
  261. mask = OCI2C_STAT_TIP;
  262. /*
  263. * We wait for the data to be transferred (8bit),
  264. * then we start polling on the ACK/NACK bit
  265. */
  266. udelay((8 * 1000) / i2c->bus_clock_khz);
  267. }
  268. /*
  269. * once we are here we expect to get the expected result immediately
  270. * so if after 1ms we timeout then something is broken.
  271. */
  272. err = ocores_wait(i2c, OCI2C_STATUS, mask, 0, msecs_to_jiffies(1));
  273. if (err)
  274. dev_warn(i2c->adap.dev.parent,
  275. "%s: STATUS timeout, bit 0x%x did not clear in 1ms\n",
  276. __func__, mask);
  277. return err;
  278. }
  279. /**
  280. * ocores_process_polling() - It handles an IRQ-less transfer
  281. * @i2c: ocores I2C device instance
  282. *
  283. * Even if IRQ are disabled, the I2C OpenCore IP behavior is exactly the same
  284. * (only that IRQ are not produced). This means that we can re-use entirely
  285. * ocores_isr(), we just add our polling code around it.
  286. *
  287. * It can run in atomic context
  288. *
  289. * Return: 0 on success, -ETIMEDOUT on timeout
  290. */
  291. static int ocores_process_polling(struct ocores_i2c *i2c)
  292. {
  293. irqreturn_t ret;
  294. int err = 0;
  295. while (1) {
  296. err = ocores_poll_wait(i2c);
  297. if (err)
  298. break; /* timeout */
  299. ret = ocores_isr(-1, i2c);
  300. if (ret == IRQ_NONE)
  301. break; /* all messages have been transferred */
  302. else {
  303. if (i2c->flags & OCORES_FLAG_BROKEN_IRQ)
  304. if (i2c->state == STATE_DONE)
  305. break;
  306. }
  307. }
  308. return err;
  309. }
  310. static int ocores_xfer_core(struct ocores_i2c *i2c,
  311. struct i2c_msg *msgs, int num,
  312. bool polling)
  313. {
  314. int ret = 0;
  315. u8 ctrl;
  316. ctrl = oc_getreg(i2c, OCI2C_CONTROL);
  317. if (polling)
  318. oc_setreg(i2c, OCI2C_CONTROL, ctrl & ~OCI2C_CTRL_IEN);
  319. else
  320. oc_setreg(i2c, OCI2C_CONTROL, ctrl | OCI2C_CTRL_IEN);
  321. i2c->msg = msgs;
  322. i2c->pos = 0;
  323. i2c->nmsgs = num;
  324. i2c->state = STATE_START;
  325. oc_setreg(i2c, OCI2C_DATA, i2c_8bit_addr_from_msg(i2c->msg));
  326. oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_START);
  327. if (polling) {
  328. ret = ocores_process_polling(i2c);
  329. } else {
  330. if (wait_event_timeout(i2c->wait,
  331. (i2c->state == STATE_ERROR) ||
  332. (i2c->state == STATE_DONE), HZ) == 0)
  333. ret = -ETIMEDOUT;
  334. }
  335. if (ret) {
  336. ocores_process_timeout(i2c);
  337. return ret;
  338. }
  339. return (i2c->state == STATE_DONE) ? num : -EIO;
  340. }
  341. static int ocores_xfer_polling(struct i2c_adapter *adap,
  342. struct i2c_msg *msgs, int num)
  343. {
  344. return ocores_xfer_core(i2c_get_adapdata(adap), msgs, num, true);
  345. }
  346. static int ocores_xfer(struct i2c_adapter *adap,
  347. struct i2c_msg *msgs, int num)
  348. {
  349. return ocores_xfer_core(i2c_get_adapdata(adap), msgs, num, false);
  350. }
  351. static int ocores_init(struct device *dev, struct ocores_i2c *i2c)
  352. {
  353. int prescale;
  354. int diff;
  355. u8 ctrl = oc_getreg(i2c, OCI2C_CONTROL);
  356. /* make sure the device is disabled */
  357. ctrl &= ~(OCI2C_CTRL_EN | OCI2C_CTRL_IEN);
  358. oc_setreg(i2c, OCI2C_CONTROL, ctrl);
  359. prescale = (i2c->ip_clock_khz / (5 * i2c->bus_clock_khz)) - 1;
  360. prescale = clamp(prescale, 0, 0xffff);
  361. diff = i2c->ip_clock_khz / (5 * (prescale + 1)) - i2c->bus_clock_khz;
  362. if (abs(diff) > i2c->bus_clock_khz / 10) {
  363. dev_err(dev,
  364. "Unsupported clock settings: core: %d KHz, bus: %d KHz\n",
  365. i2c->ip_clock_khz, i2c->bus_clock_khz);
  366. return -EINVAL;
  367. }
  368. oc_setreg(i2c, OCI2C_PRELOW, prescale & 0xff);
  369. oc_setreg(i2c, OCI2C_PREHIGH, prescale >> 8);
  370. /* Init the device */
  371. oc_setreg(i2c, OCI2C_CONTROL, ctrl | OCI2C_CTRL_EN);
  372. oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_IACK);
  373. return 0;
  374. }
  375. static u32 ocores_func(struct i2c_adapter *adap)
  376. {
  377. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  378. }
  379. static struct i2c_algorithm ocores_algorithm = {
  380. .xfer = ocores_xfer,
  381. .xfer_atomic = ocores_xfer_polling,
  382. .functionality = ocores_func,
  383. };
  384. static const struct i2c_adapter ocores_adapter = {
  385. .owner = THIS_MODULE,
  386. .name = "i2c-ocores",
  387. .class = I2C_CLASS_DEPRECATED,
  388. .algo = &ocores_algorithm,
  389. };
  390. static const struct of_device_id ocores_i2c_match[] = {
  391. {
  392. .compatible = "opencores,i2c-ocores",
  393. .data = (void *)TYPE_OCORES,
  394. },
  395. {
  396. .compatible = "aeroflexgaisler,i2cmst",
  397. .data = (void *)TYPE_GRLIB,
  398. },
  399. {
  400. .compatible = "sifive,fu540-c000-i2c",
  401. },
  402. {
  403. .compatible = "sifive,i2c0",
  404. },
  405. {},
  406. };
  407. MODULE_DEVICE_TABLE(of, ocores_i2c_match);
  408. #ifdef CONFIG_OF
  409. /*
  410. * Read and write functions for the GRLIB port of the controller. Registers are
  411. * 32-bit big endian and the PRELOW and PREHIGH registers are merged into one
  412. * register. The subsequent registers have their offsets decreased accordingly.
  413. */
  414. static u8 oc_getreg_grlib(struct ocores_i2c *i2c, int reg)
  415. {
  416. u32 rd;
  417. int rreg = reg;
  418. if (reg != OCI2C_PRELOW)
  419. rreg--;
  420. rd = ioread32be(i2c->base + (rreg << i2c->reg_shift));
  421. if (reg == OCI2C_PREHIGH)
  422. return (u8)(rd >> 8);
  423. else
  424. return (u8)rd;
  425. }
  426. static void oc_setreg_grlib(struct ocores_i2c *i2c, int reg, u8 value)
  427. {
  428. u32 curr, wr;
  429. int rreg = reg;
  430. if (reg != OCI2C_PRELOW)
  431. rreg--;
  432. if (reg == OCI2C_PRELOW || reg == OCI2C_PREHIGH) {
  433. curr = ioread32be(i2c->base + (rreg << i2c->reg_shift));
  434. if (reg == OCI2C_PRELOW)
  435. wr = (curr & 0xff00) | value;
  436. else
  437. wr = (((u32)value) << 8) | (curr & 0xff);
  438. } else {
  439. wr = value;
  440. }
  441. iowrite32be(wr, i2c->base + (rreg << i2c->reg_shift));
  442. }
  443. static int ocores_i2c_of_probe(struct platform_device *pdev,
  444. struct ocores_i2c *i2c)
  445. {
  446. struct device_node *np = pdev->dev.of_node;
  447. const struct of_device_id *match;
  448. u32 val;
  449. u32 clock_frequency;
  450. bool clock_frequency_present;
  451. if (of_property_read_u32(np, "reg-shift", &i2c->reg_shift)) {
  452. /* no 'reg-shift', check for deprecated 'regstep' */
  453. if (!of_property_read_u32(np, "regstep", &val)) {
  454. if (!is_power_of_2(val)) {
  455. dev_err(&pdev->dev, "invalid regstep %d\n",
  456. val);
  457. return -EINVAL;
  458. }
  459. i2c->reg_shift = ilog2(val);
  460. dev_warn(&pdev->dev,
  461. "regstep property deprecated, use reg-shift\n");
  462. }
  463. }
  464. clock_frequency_present = !of_property_read_u32(np, "clock-frequency",
  465. &clock_frequency);
  466. i2c->bus_clock_khz = 100;
  467. i2c->clk = devm_clk_get_optional_enabled(&pdev->dev, NULL);
  468. if (IS_ERR(i2c->clk))
  469. return dev_err_probe(&pdev->dev, PTR_ERR(i2c->clk),
  470. "devm_clk_get_optional_enabled failed\n");
  471. i2c->ip_clock_khz = clk_get_rate(i2c->clk) / 1000;
  472. if (clock_frequency_present)
  473. i2c->bus_clock_khz = clock_frequency / 1000;
  474. if (i2c->ip_clock_khz == 0) {
  475. if (of_property_read_u32(np, "opencores,ip-clock-frequency",
  476. &val)) {
  477. if (!clock_frequency_present) {
  478. dev_err(&pdev->dev,
  479. "Missing required parameter 'opencores,ip-clock-frequency'\n");
  480. return -ENODEV;
  481. }
  482. i2c->ip_clock_khz = clock_frequency / 1000;
  483. dev_warn(&pdev->dev,
  484. "Deprecated usage of the 'clock-frequency' property, please update to 'opencores,ip-clock-frequency'\n");
  485. } else {
  486. i2c->ip_clock_khz = val / 1000;
  487. if (clock_frequency_present)
  488. i2c->bus_clock_khz = clock_frequency / 1000;
  489. }
  490. }
  491. of_property_read_u32(pdev->dev.of_node, "reg-io-width",
  492. &i2c->reg_io_width);
  493. match = of_match_node(ocores_i2c_match, pdev->dev.of_node);
  494. if (match && (long)match->data == TYPE_GRLIB) {
  495. dev_dbg(&pdev->dev, "GRLIB variant of i2c-ocores\n");
  496. i2c->setreg = oc_setreg_grlib;
  497. i2c->getreg = oc_getreg_grlib;
  498. }
  499. return 0;
  500. }
  501. #else
  502. #define ocores_i2c_of_probe(pdev, i2c) -ENODEV
  503. #endif
  504. static int ocores_i2c_probe(struct platform_device *pdev)
  505. {
  506. struct ocores_i2c *i2c;
  507. struct ocores_i2c_platform_data *pdata;
  508. struct resource *res;
  509. int irq;
  510. int ret;
  511. int i;
  512. i2c = devm_kzalloc(&pdev->dev, sizeof(*i2c), GFP_KERNEL);
  513. if (!i2c)
  514. return -ENOMEM;
  515. spin_lock_init(&i2c->process_lock);
  516. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  517. if (res) {
  518. i2c->base = devm_ioremap_resource(&pdev->dev, res);
  519. if (IS_ERR(i2c->base))
  520. return PTR_ERR(i2c->base);
  521. } else {
  522. res = platform_get_resource(pdev, IORESOURCE_IO, 0);
  523. if (!res)
  524. return -EINVAL;
  525. if (!devm_request_region(&pdev->dev, res->start,
  526. resource_size(res),
  527. pdev->name)) {
  528. dev_err(&pdev->dev, "Can't get I/O resource.\n");
  529. return -EBUSY;
  530. }
  531. i2c->base = devm_ioport_map(&pdev->dev, res->start,
  532. resource_size(res));
  533. if (!i2c->base) {
  534. dev_err(&pdev->dev, "Can't map I/O resource.\n");
  535. return -EBUSY;
  536. }
  537. i2c->reg_io_width = 1;
  538. }
  539. pdata = dev_get_platdata(&pdev->dev);
  540. if (pdata) {
  541. i2c->reg_shift = pdata->reg_shift;
  542. i2c->reg_io_width = pdata->reg_io_width;
  543. i2c->ip_clock_khz = pdata->clock_khz;
  544. if (pdata->bus_khz)
  545. i2c->bus_clock_khz = pdata->bus_khz;
  546. else
  547. i2c->bus_clock_khz = 100;
  548. } else {
  549. ret = ocores_i2c_of_probe(pdev, i2c);
  550. if (ret)
  551. return ret;
  552. }
  553. if (i2c->reg_io_width == 0)
  554. i2c->reg_io_width = 1; /* Set to default value */
  555. if (!i2c->setreg || !i2c->getreg) {
  556. bool be = pdata ? pdata->big_endian :
  557. of_device_is_big_endian(pdev->dev.of_node);
  558. switch (i2c->reg_io_width) {
  559. case 1:
  560. i2c->setreg = oc_setreg_8;
  561. i2c->getreg = oc_getreg_8;
  562. break;
  563. case 2:
  564. i2c->setreg = be ? oc_setreg_16be : oc_setreg_16;
  565. i2c->getreg = be ? oc_getreg_16be : oc_getreg_16;
  566. break;
  567. case 4:
  568. i2c->setreg = be ? oc_setreg_32be : oc_setreg_32;
  569. i2c->getreg = be ? oc_getreg_32be : oc_getreg_32;
  570. break;
  571. default:
  572. dev_err(&pdev->dev, "Unsupported I/O width (%d)\n",
  573. i2c->reg_io_width);
  574. return -EINVAL;
  575. }
  576. }
  577. init_waitqueue_head(&i2c->wait);
  578. irq = platform_get_irq_optional(pdev, 0);
  579. /*
  580. * Since the SoC does have an interrupt, its DT has an interrupt
  581. * property - But this should be bypassed as the IRQ logic in this
  582. * SoC is broken.
  583. */
  584. if (of_device_is_compatible(pdev->dev.of_node,
  585. "sifive,fu540-c000-i2c")) {
  586. i2c->flags |= OCORES_FLAG_BROKEN_IRQ;
  587. irq = -ENXIO;
  588. }
  589. if (irq == -ENXIO) {
  590. ocores_algorithm.xfer = ocores_xfer_polling;
  591. } else {
  592. if (irq < 0)
  593. return irq;
  594. }
  595. if (ocores_algorithm.xfer != ocores_xfer_polling) {
  596. ret = devm_request_any_context_irq(&pdev->dev, irq,
  597. ocores_isr, 0,
  598. pdev->name, i2c);
  599. if (ret) {
  600. dev_err(&pdev->dev, "Cannot claim IRQ\n");
  601. return ret;
  602. }
  603. }
  604. ret = ocores_init(&pdev->dev, i2c);
  605. if (ret)
  606. return ret;
  607. /* hook up driver to tree */
  608. platform_set_drvdata(pdev, i2c);
  609. i2c->adap = ocores_adapter;
  610. i2c_set_adapdata(&i2c->adap, i2c);
  611. i2c->adap.dev.parent = &pdev->dev;
  612. i2c->adap.dev.of_node = pdev->dev.of_node;
  613. /* add i2c adapter to i2c tree */
  614. ret = i2c_add_adapter(&i2c->adap);
  615. if (ret)
  616. return ret;
  617. /* add in known devices to the bus */
  618. if (pdata) {
  619. for (i = 0; i < pdata->num_devices; i++)
  620. i2c_new_client_device(&i2c->adap, pdata->devices + i);
  621. }
  622. return 0;
  623. }
  624. static void ocores_i2c_remove(struct platform_device *pdev)
  625. {
  626. struct ocores_i2c *i2c = platform_get_drvdata(pdev);
  627. u8 ctrl = oc_getreg(i2c, OCI2C_CONTROL);
  628. /* disable i2c logic */
  629. ctrl &= ~(OCI2C_CTRL_EN | OCI2C_CTRL_IEN);
  630. oc_setreg(i2c, OCI2C_CONTROL, ctrl);
  631. /* remove adapter & data */
  632. i2c_del_adapter(&i2c->adap);
  633. }
  634. static int ocores_i2c_suspend(struct device *dev)
  635. {
  636. struct ocores_i2c *i2c = dev_get_drvdata(dev);
  637. u8 ctrl = oc_getreg(i2c, OCI2C_CONTROL);
  638. /* make sure the device is disabled */
  639. ctrl &= ~(OCI2C_CTRL_EN | OCI2C_CTRL_IEN);
  640. oc_setreg(i2c, OCI2C_CONTROL, ctrl);
  641. clk_disable_unprepare(i2c->clk);
  642. return 0;
  643. }
  644. static int ocores_i2c_resume(struct device *dev)
  645. {
  646. struct ocores_i2c *i2c = dev_get_drvdata(dev);
  647. unsigned long rate;
  648. int ret;
  649. ret = clk_prepare_enable(i2c->clk);
  650. if (ret)
  651. return dev_err_probe(dev, ret, "clk_prepare_enable failed\n");
  652. rate = clk_get_rate(i2c->clk) / 1000;
  653. if (rate)
  654. i2c->ip_clock_khz = rate;
  655. return ocores_init(dev, i2c);
  656. }
  657. static DEFINE_NOIRQ_DEV_PM_OPS(ocores_i2c_pm,
  658. ocores_i2c_suspend, ocores_i2c_resume);
  659. static struct platform_driver ocores_i2c_driver = {
  660. .probe = ocores_i2c_probe,
  661. .remove_new = ocores_i2c_remove,
  662. .driver = {
  663. .name = "ocores-i2c",
  664. .of_match_table = ocores_i2c_match,
  665. .pm = pm_sleep_ptr(&ocores_i2c_pm),
  666. },
  667. };
  668. module_platform_driver(ocores_i2c_driver);
  669. MODULE_AUTHOR("Peter Korsgaard <peter@korsgaard.com>");
  670. MODULE_DESCRIPTION("OpenCores I2C bus driver");
  671. MODULE_LICENSE("GPL");
  672. MODULE_ALIAS("platform:ocores-i2c");