i2c-axxia.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. /*
  2. * This driver implements I2C master functionality using the LSI API2C
  3. * controller.
  4. *
  5. * NOTE: The controller has a limitation in that it can only do transfers of
  6. * maximum 255 bytes at a time. If a larger transfer is attempted, error code
  7. * (-EINVAL) is returned.
  8. *
  9. * This software is licensed under the terms of the GNU General Public
  10. * License version 2, as published by the Free Software Foundation, and
  11. * may be copied, distributed, and modified under those terms.
  12. */
  13. #include <linux/clk.h>
  14. #include <linux/clkdev.h>
  15. #include <linux/err.h>
  16. #include <linux/i2c.h>
  17. #include <linux/init.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/module.h>
  20. #include <linux/io.h>
  21. #include <linux/kernel.h>
  22. #include <linux/platform_device.h>
  23. #define SCL_WAIT_TIMEOUT_NS 25000000
  24. #define I2C_XFER_TIMEOUT (msecs_to_jiffies(250))
  25. #define I2C_STOP_TIMEOUT (msecs_to_jiffies(100))
  26. #define FIFO_SIZE 8
  27. #define GLOBAL_CONTROL 0x00
  28. #define GLOBAL_MST_EN BIT(0)
  29. #define GLOBAL_SLV_EN BIT(1)
  30. #define GLOBAL_IBML_EN BIT(2)
  31. #define INTERRUPT_STATUS 0x04
  32. #define INTERRUPT_ENABLE 0x08
  33. #define INT_SLV BIT(1)
  34. #define INT_MST BIT(0)
  35. #define WAIT_TIMER_CONTROL 0x0c
  36. #define WT_EN BIT(15)
  37. #define WT_VALUE(_x) ((_x) & 0x7fff)
  38. #define IBML_TIMEOUT 0x10
  39. #define IBML_LOW_MEXT 0x14
  40. #define IBML_LOW_SEXT 0x18
  41. #define TIMER_CLOCK_DIV 0x1c
  42. #define I2C_BUS_MONITOR 0x20
  43. #define BM_SDAC BIT(3)
  44. #define BM_SCLC BIT(2)
  45. #define BM_SDAS BIT(1)
  46. #define BM_SCLS BIT(0)
  47. #define SOFT_RESET 0x24
  48. #define MST_COMMAND 0x28
  49. #define CMD_BUSY (1<<3)
  50. #define CMD_MANUAL (0x00 | CMD_BUSY)
  51. #define CMD_AUTO (0x01 | CMD_BUSY)
  52. #define MST_RX_XFER 0x2c
  53. #define MST_TX_XFER 0x30
  54. #define MST_ADDR_1 0x34
  55. #define MST_ADDR_2 0x38
  56. #define MST_DATA 0x3c
  57. #define MST_TX_FIFO 0x40
  58. #define MST_RX_FIFO 0x44
  59. #define MST_INT_ENABLE 0x48
  60. #define MST_INT_STATUS 0x4c
  61. #define MST_STATUS_RFL (1 << 13) /* RX FIFO serivce */
  62. #define MST_STATUS_TFL (1 << 12) /* TX FIFO service */
  63. #define MST_STATUS_SNS (1 << 11) /* Manual mode done */
  64. #define MST_STATUS_SS (1 << 10) /* Automatic mode done */
  65. #define MST_STATUS_SCC (1 << 9) /* Stop complete */
  66. #define MST_STATUS_IP (1 << 8) /* Invalid parameter */
  67. #define MST_STATUS_TSS (1 << 7) /* Timeout */
  68. #define MST_STATUS_AL (1 << 6) /* Arbitration lost */
  69. #define MST_STATUS_ND (1 << 5) /* NAK on data phase */
  70. #define MST_STATUS_NA (1 << 4) /* NAK on address phase */
  71. #define MST_STATUS_NAK (MST_STATUS_NA | \
  72. MST_STATUS_ND)
  73. #define MST_STATUS_ERR (MST_STATUS_NAK | \
  74. MST_STATUS_AL | \
  75. MST_STATUS_IP)
  76. #define MST_TX_BYTES_XFRD 0x50
  77. #define MST_RX_BYTES_XFRD 0x54
  78. #define SCL_HIGH_PERIOD 0x80
  79. #define SCL_LOW_PERIOD 0x84
  80. #define SPIKE_FLTR_LEN 0x88
  81. #define SDA_SETUP_TIME 0x8c
  82. #define SDA_HOLD_TIME 0x90
  83. /**
  84. * axxia_i2c_dev - I2C device context
  85. * @base: pointer to register struct
  86. * @msg: pointer to current message
  87. * @msg_xfrd: number of bytes transferred in msg
  88. * @msg_err: error code for completed message
  89. * @msg_complete: xfer completion object
  90. * @dev: device reference
  91. * @adapter: core i2c abstraction
  92. * @i2c_clk: clock reference for i2c input clock
  93. * @bus_clk_rate: current i2c bus clock rate
  94. */
  95. struct axxia_i2c_dev {
  96. void __iomem *base;
  97. struct i2c_msg *msg;
  98. size_t msg_xfrd;
  99. int msg_err;
  100. struct completion msg_complete;
  101. struct device *dev;
  102. struct i2c_adapter adapter;
  103. struct clk *i2c_clk;
  104. u32 bus_clk_rate;
  105. };
  106. static void i2c_int_disable(struct axxia_i2c_dev *idev, u32 mask)
  107. {
  108. u32 int_en;
  109. int_en = readl(idev->base + MST_INT_ENABLE);
  110. writel(int_en & ~mask, idev->base + MST_INT_ENABLE);
  111. }
  112. static void i2c_int_enable(struct axxia_i2c_dev *idev, u32 mask)
  113. {
  114. u32 int_en;
  115. int_en = readl(idev->base + MST_INT_ENABLE);
  116. writel(int_en | mask, idev->base + MST_INT_ENABLE);
  117. }
  118. /**
  119. * ns_to_clk - Convert time (ns) to clock cycles for the given clock frequency.
  120. */
  121. static u32 ns_to_clk(u64 ns, u32 clk_mhz)
  122. {
  123. return div_u64(ns * clk_mhz, 1000);
  124. }
  125. static int axxia_i2c_init(struct axxia_i2c_dev *idev)
  126. {
  127. u32 divisor = clk_get_rate(idev->i2c_clk) / idev->bus_clk_rate;
  128. u32 clk_mhz = clk_get_rate(idev->i2c_clk) / 1000000;
  129. u32 t_setup;
  130. u32 t_high, t_low;
  131. u32 tmo_clk;
  132. u32 prescale;
  133. unsigned long timeout;
  134. dev_dbg(idev->dev, "rate=%uHz per_clk=%uMHz -> ratio=1:%u\n",
  135. idev->bus_clk_rate, clk_mhz, divisor);
  136. /* Reset controller */
  137. writel(0x01, idev->base + SOFT_RESET);
  138. timeout = jiffies + msecs_to_jiffies(100);
  139. while (readl(idev->base + SOFT_RESET) & 1) {
  140. if (time_after(jiffies, timeout)) {
  141. dev_warn(idev->dev, "Soft reset failed\n");
  142. break;
  143. }
  144. }
  145. /* Enable Master Mode */
  146. writel(0x1, idev->base + GLOBAL_CONTROL);
  147. if (idev->bus_clk_rate <= 100000) {
  148. /* Standard mode SCL 50/50, tSU:DAT = 250 ns */
  149. t_high = divisor * 1 / 2;
  150. t_low = divisor * 1 / 2;
  151. t_setup = ns_to_clk(250, clk_mhz);
  152. } else {
  153. /* Fast mode SCL 33/66, tSU:DAT = 100 ns */
  154. t_high = divisor * 1 / 3;
  155. t_low = divisor * 2 / 3;
  156. t_setup = ns_to_clk(100, clk_mhz);
  157. }
  158. /* SCL High Time */
  159. writel(t_high, idev->base + SCL_HIGH_PERIOD);
  160. /* SCL Low Time */
  161. writel(t_low, idev->base + SCL_LOW_PERIOD);
  162. /* SDA Setup Time */
  163. writel(t_setup, idev->base + SDA_SETUP_TIME);
  164. /* SDA Hold Time, 300ns */
  165. writel(ns_to_clk(300, clk_mhz), idev->base + SDA_HOLD_TIME);
  166. /* Filter <50ns spikes */
  167. writel(ns_to_clk(50, clk_mhz), idev->base + SPIKE_FLTR_LEN);
  168. /* Configure Time-Out Registers */
  169. tmo_clk = ns_to_clk(SCL_WAIT_TIMEOUT_NS, clk_mhz);
  170. /* Find prescaler value that makes tmo_clk fit in 15-bits counter. */
  171. for (prescale = 0; prescale < 15; ++prescale) {
  172. if (tmo_clk <= 0x7fff)
  173. break;
  174. tmo_clk >>= 1;
  175. }
  176. if (tmo_clk > 0x7fff)
  177. tmo_clk = 0x7fff;
  178. /* Prescale divider (log2) */
  179. writel(prescale, idev->base + TIMER_CLOCK_DIV);
  180. /* Timeout in divided clocks */
  181. writel(WT_EN | WT_VALUE(tmo_clk), idev->base + WAIT_TIMER_CONTROL);
  182. /* Mask all master interrupt bits */
  183. i2c_int_disable(idev, ~0);
  184. /* Interrupt enable */
  185. writel(0x01, idev->base + INTERRUPT_ENABLE);
  186. return 0;
  187. }
  188. static int i2c_m_rd(const struct i2c_msg *msg)
  189. {
  190. return (msg->flags & I2C_M_RD) != 0;
  191. }
  192. static int i2c_m_ten(const struct i2c_msg *msg)
  193. {
  194. return (msg->flags & I2C_M_TEN) != 0;
  195. }
  196. static int i2c_m_recv_len(const struct i2c_msg *msg)
  197. {
  198. return (msg->flags & I2C_M_RECV_LEN) != 0;
  199. }
  200. /**
  201. * axxia_i2c_empty_rx_fifo - Fetch data from RX FIFO and update SMBus block
  202. * transfer length if this is the first byte of such a transfer.
  203. */
  204. static int axxia_i2c_empty_rx_fifo(struct axxia_i2c_dev *idev)
  205. {
  206. struct i2c_msg *msg = idev->msg;
  207. size_t rx_fifo_avail = readl(idev->base + MST_RX_FIFO);
  208. int bytes_to_transfer = min(rx_fifo_avail, msg->len - idev->msg_xfrd);
  209. while (bytes_to_transfer-- > 0) {
  210. int c = readl(idev->base + MST_DATA);
  211. if (idev->msg_xfrd == 0 && i2c_m_recv_len(msg)) {
  212. /*
  213. * Check length byte for SMBus block read
  214. */
  215. if (c <= 0 || c > I2C_SMBUS_BLOCK_MAX) {
  216. idev->msg_err = -EPROTO;
  217. i2c_int_disable(idev, ~MST_STATUS_TSS);
  218. complete(&idev->msg_complete);
  219. break;
  220. }
  221. msg->len = 1 + c;
  222. writel(msg->len, idev->base + MST_RX_XFER);
  223. }
  224. msg->buf[idev->msg_xfrd++] = c;
  225. }
  226. return 0;
  227. }
  228. /**
  229. * axxia_i2c_fill_tx_fifo - Fill TX FIFO from current message buffer.
  230. * @return: Number of bytes left to transfer.
  231. */
  232. static int axxia_i2c_fill_tx_fifo(struct axxia_i2c_dev *idev)
  233. {
  234. struct i2c_msg *msg = idev->msg;
  235. size_t tx_fifo_avail = FIFO_SIZE - readl(idev->base + MST_TX_FIFO);
  236. int bytes_to_transfer = min(tx_fifo_avail, msg->len - idev->msg_xfrd);
  237. int ret = msg->len - idev->msg_xfrd - bytes_to_transfer;
  238. while (bytes_to_transfer-- > 0)
  239. writel(msg->buf[idev->msg_xfrd++], idev->base + MST_DATA);
  240. return ret;
  241. }
  242. static irqreturn_t axxia_i2c_isr(int irq, void *_dev)
  243. {
  244. struct axxia_i2c_dev *idev = _dev;
  245. u32 status;
  246. if (!(readl(idev->base + INTERRUPT_STATUS) & INT_MST))
  247. return IRQ_NONE;
  248. /* Read interrupt status bits */
  249. status = readl(idev->base + MST_INT_STATUS);
  250. if (!idev->msg) {
  251. dev_warn(idev->dev, "unexpected interrupt\n");
  252. goto out;
  253. }
  254. /* RX FIFO needs service? */
  255. if (i2c_m_rd(idev->msg) && (status & MST_STATUS_RFL))
  256. axxia_i2c_empty_rx_fifo(idev);
  257. /* TX FIFO needs service? */
  258. if (!i2c_m_rd(idev->msg) && (status & MST_STATUS_TFL)) {
  259. if (axxia_i2c_fill_tx_fifo(idev) == 0)
  260. i2c_int_disable(idev, MST_STATUS_TFL);
  261. }
  262. if (unlikely(status & MST_STATUS_ERR)) {
  263. /* Transfer error */
  264. i2c_int_disable(idev, ~0);
  265. if (status & MST_STATUS_AL)
  266. idev->msg_err = -EAGAIN;
  267. else if (status & MST_STATUS_NAK)
  268. idev->msg_err = -ENXIO;
  269. else
  270. idev->msg_err = -EIO;
  271. dev_dbg(idev->dev, "error %#x, addr=%#x rx=%u/%u tx=%u/%u\n",
  272. status,
  273. idev->msg->addr,
  274. readl(idev->base + MST_RX_BYTES_XFRD),
  275. readl(idev->base + MST_RX_XFER),
  276. readl(idev->base + MST_TX_BYTES_XFRD),
  277. readl(idev->base + MST_TX_XFER));
  278. complete(&idev->msg_complete);
  279. } else if (status & MST_STATUS_SCC) {
  280. /* Stop completed */
  281. i2c_int_disable(idev, ~MST_STATUS_TSS);
  282. complete(&idev->msg_complete);
  283. } else if (status & MST_STATUS_SNS) {
  284. /* Transfer done */
  285. i2c_int_disable(idev, ~MST_STATUS_TSS);
  286. if (i2c_m_rd(idev->msg) && idev->msg_xfrd < idev->msg->len)
  287. axxia_i2c_empty_rx_fifo(idev);
  288. complete(&idev->msg_complete);
  289. } else if (status & MST_STATUS_TSS) {
  290. /* Transfer timeout */
  291. idev->msg_err = -ETIMEDOUT;
  292. i2c_int_disable(idev, ~MST_STATUS_TSS);
  293. complete(&idev->msg_complete);
  294. }
  295. out:
  296. /* Clear interrupt */
  297. writel(INT_MST, idev->base + INTERRUPT_STATUS);
  298. return IRQ_HANDLED;
  299. }
  300. static int axxia_i2c_xfer_msg(struct axxia_i2c_dev *idev, struct i2c_msg *msg)
  301. {
  302. u32 int_mask = MST_STATUS_ERR | MST_STATUS_SNS;
  303. u32 rx_xfer, tx_xfer;
  304. u32 addr_1, addr_2;
  305. unsigned long time_left;
  306. unsigned int wt_value;
  307. idev->msg = msg;
  308. idev->msg_xfrd = 0;
  309. reinit_completion(&idev->msg_complete);
  310. if (i2c_m_ten(msg)) {
  311. /* 10-bit address
  312. * addr_1: 5'b11110 | addr[9:8] | (R/nW)
  313. * addr_2: addr[7:0]
  314. */
  315. addr_1 = 0xF0 | ((msg->addr >> 7) & 0x06);
  316. addr_2 = msg->addr & 0xFF;
  317. } else {
  318. /* 7-bit address
  319. * addr_1: addr[6:0] | (R/nW)
  320. * addr_2: dont care
  321. */
  322. addr_1 = (msg->addr << 1) & 0xFF;
  323. addr_2 = 0;
  324. }
  325. if (i2c_m_rd(msg)) {
  326. /* I2C read transfer */
  327. rx_xfer = i2c_m_recv_len(msg) ? I2C_SMBUS_BLOCK_MAX : msg->len;
  328. tx_xfer = 0;
  329. addr_1 |= 1; /* Set the R/nW bit of the address */
  330. } else {
  331. /* I2C write transfer */
  332. rx_xfer = 0;
  333. tx_xfer = msg->len;
  334. }
  335. writel(rx_xfer, idev->base + MST_RX_XFER);
  336. writel(tx_xfer, idev->base + MST_TX_XFER);
  337. writel(addr_1, idev->base + MST_ADDR_1);
  338. writel(addr_2, idev->base + MST_ADDR_2);
  339. if (i2c_m_rd(msg))
  340. int_mask |= MST_STATUS_RFL;
  341. else if (axxia_i2c_fill_tx_fifo(idev) != 0)
  342. int_mask |= MST_STATUS_TFL;
  343. wt_value = WT_VALUE(readl(idev->base + WAIT_TIMER_CONTROL));
  344. /* Disable wait timer temporarly */
  345. writel(wt_value, idev->base + WAIT_TIMER_CONTROL);
  346. /* Check if timeout error happened */
  347. if (idev->msg_err)
  348. goto out;
  349. /* Start manual mode */
  350. writel(CMD_MANUAL, idev->base + MST_COMMAND);
  351. writel(WT_EN | wt_value, idev->base + WAIT_TIMER_CONTROL);
  352. i2c_int_enable(idev, int_mask);
  353. time_left = wait_for_completion_timeout(&idev->msg_complete,
  354. I2C_XFER_TIMEOUT);
  355. i2c_int_disable(idev, int_mask);
  356. if (readl(idev->base + MST_COMMAND) & CMD_BUSY)
  357. dev_warn(idev->dev, "busy after xfer\n");
  358. if (time_left == 0) {
  359. idev->msg_err = -ETIMEDOUT;
  360. i2c_recover_bus(&idev->adapter);
  361. axxia_i2c_init(idev);
  362. }
  363. out:
  364. if (unlikely(idev->msg_err) && idev->msg_err != -ENXIO &&
  365. idev->msg_err != -ETIMEDOUT)
  366. axxia_i2c_init(idev);
  367. return idev->msg_err;
  368. }
  369. static int axxia_i2c_stop(struct axxia_i2c_dev *idev)
  370. {
  371. u32 int_mask = MST_STATUS_ERR | MST_STATUS_SCC | MST_STATUS_TSS;
  372. unsigned long time_left;
  373. reinit_completion(&idev->msg_complete);
  374. /* Issue stop */
  375. writel(0xb, idev->base + MST_COMMAND);
  376. i2c_int_enable(idev, int_mask);
  377. time_left = wait_for_completion_timeout(&idev->msg_complete,
  378. I2C_STOP_TIMEOUT);
  379. i2c_int_disable(idev, int_mask);
  380. if (time_left == 0)
  381. return -ETIMEDOUT;
  382. if (readl(idev->base + MST_COMMAND) & CMD_BUSY)
  383. dev_warn(idev->dev, "busy after stop\n");
  384. return 0;
  385. }
  386. static int
  387. axxia_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
  388. {
  389. struct axxia_i2c_dev *idev = i2c_get_adapdata(adap);
  390. int i;
  391. int ret = 0;
  392. idev->msg_err = 0;
  393. i2c_int_enable(idev, MST_STATUS_TSS);
  394. for (i = 0; ret == 0 && i < num; ++i)
  395. ret = axxia_i2c_xfer_msg(idev, &msgs[i]);
  396. axxia_i2c_stop(idev);
  397. return ret ? : i;
  398. }
  399. static int axxia_i2c_get_scl(struct i2c_adapter *adap)
  400. {
  401. struct axxia_i2c_dev *idev = i2c_get_adapdata(adap);
  402. return !!(readl(idev->base + I2C_BUS_MONITOR) & BM_SCLS);
  403. }
  404. static void axxia_i2c_set_scl(struct i2c_adapter *adap, int val)
  405. {
  406. struct axxia_i2c_dev *idev = i2c_get_adapdata(adap);
  407. u32 tmp;
  408. /* Preserve SDA Control */
  409. tmp = readl(idev->base + I2C_BUS_MONITOR) & BM_SDAC;
  410. if (!val)
  411. tmp |= BM_SCLC;
  412. writel(tmp, idev->base + I2C_BUS_MONITOR);
  413. }
  414. static int axxia_i2c_get_sda(struct i2c_adapter *adap)
  415. {
  416. struct axxia_i2c_dev *idev = i2c_get_adapdata(adap);
  417. return !!(readl(idev->base + I2C_BUS_MONITOR) & BM_SDAS);
  418. }
  419. static struct i2c_bus_recovery_info axxia_i2c_recovery_info = {
  420. .recover_bus = i2c_generic_scl_recovery,
  421. .get_scl = axxia_i2c_get_scl,
  422. .set_scl = axxia_i2c_set_scl,
  423. .get_sda = axxia_i2c_get_sda,
  424. };
  425. static u32 axxia_i2c_func(struct i2c_adapter *adap)
  426. {
  427. u32 caps = (I2C_FUNC_I2C | I2C_FUNC_10BIT_ADDR |
  428. I2C_FUNC_SMBUS_EMUL | I2C_FUNC_SMBUS_BLOCK_DATA);
  429. return caps;
  430. }
  431. static const struct i2c_algorithm axxia_i2c_algo = {
  432. .master_xfer = axxia_i2c_xfer,
  433. .functionality = axxia_i2c_func,
  434. };
  435. static struct i2c_adapter_quirks axxia_i2c_quirks = {
  436. .max_read_len = 255,
  437. .max_write_len = 255,
  438. };
  439. static int axxia_i2c_probe(struct platform_device *pdev)
  440. {
  441. struct device_node *np = pdev->dev.of_node;
  442. struct axxia_i2c_dev *idev = NULL;
  443. struct resource *res;
  444. void __iomem *base;
  445. int irq;
  446. int ret = 0;
  447. idev = devm_kzalloc(&pdev->dev, sizeof(*idev), GFP_KERNEL);
  448. if (!idev)
  449. return -ENOMEM;
  450. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  451. base = devm_ioremap_resource(&pdev->dev, res);
  452. if (IS_ERR(base))
  453. return PTR_ERR(base);
  454. irq = platform_get_irq(pdev, 0);
  455. if (irq < 0) {
  456. dev_err(&pdev->dev, "missing interrupt resource\n");
  457. return irq;
  458. }
  459. idev->i2c_clk = devm_clk_get(&pdev->dev, "i2c");
  460. if (IS_ERR(idev->i2c_clk)) {
  461. dev_err(&pdev->dev, "missing clock\n");
  462. return PTR_ERR(idev->i2c_clk);
  463. }
  464. idev->base = base;
  465. idev->dev = &pdev->dev;
  466. init_completion(&idev->msg_complete);
  467. of_property_read_u32(np, "clock-frequency", &idev->bus_clk_rate);
  468. if (idev->bus_clk_rate == 0)
  469. idev->bus_clk_rate = 100000; /* default clock rate */
  470. ret = axxia_i2c_init(idev);
  471. if (ret) {
  472. dev_err(&pdev->dev, "failed to initialize\n");
  473. return ret;
  474. }
  475. ret = devm_request_irq(&pdev->dev, irq, axxia_i2c_isr, 0,
  476. pdev->name, idev);
  477. if (ret) {
  478. dev_err(&pdev->dev, "failed to claim IRQ%d\n", irq);
  479. return ret;
  480. }
  481. clk_prepare_enable(idev->i2c_clk);
  482. i2c_set_adapdata(&idev->adapter, idev);
  483. strlcpy(idev->adapter.name, pdev->name, sizeof(idev->adapter.name));
  484. idev->adapter.owner = THIS_MODULE;
  485. idev->adapter.algo = &axxia_i2c_algo;
  486. idev->adapter.bus_recovery_info = &axxia_i2c_recovery_info;
  487. idev->adapter.quirks = &axxia_i2c_quirks;
  488. idev->adapter.dev.parent = &pdev->dev;
  489. idev->adapter.dev.of_node = pdev->dev.of_node;
  490. platform_set_drvdata(pdev, idev);
  491. ret = i2c_add_adapter(&idev->adapter);
  492. if (ret) {
  493. dev_err(&pdev->dev, "failed to add adapter\n");
  494. return ret;
  495. }
  496. return 0;
  497. }
  498. static int axxia_i2c_remove(struct platform_device *pdev)
  499. {
  500. struct axxia_i2c_dev *idev = platform_get_drvdata(pdev);
  501. clk_disable_unprepare(idev->i2c_clk);
  502. i2c_del_adapter(&idev->adapter);
  503. return 0;
  504. }
  505. /* Match table for of_platform binding */
  506. static const struct of_device_id axxia_i2c_of_match[] = {
  507. { .compatible = "lsi,api2c", },
  508. {},
  509. };
  510. MODULE_DEVICE_TABLE(of, axxia_i2c_of_match);
  511. static struct platform_driver axxia_i2c_driver = {
  512. .probe = axxia_i2c_probe,
  513. .remove = axxia_i2c_remove,
  514. .driver = {
  515. .name = "axxia-i2c",
  516. .of_match_table = axxia_i2c_of_match,
  517. },
  518. };
  519. module_platform_driver(axxia_i2c_driver);
  520. MODULE_DESCRIPTION("Axxia I2C Bus driver");
  521. MODULE_AUTHOR("Anders Berg <anders.berg@lsi.com>");
  522. MODULE_LICENSE("GPL v2");