i2c-brcmstb.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  1. /*
  2. * Copyright (C) 2014 Broadcom Corporation
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation version 2.
  7. *
  8. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  9. * kind, whether express or implied; without even the implied warranty
  10. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/clk.h>
  14. #include <linux/delay.h>
  15. #include <linux/device.h>
  16. #include <linux/i2c.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/io.h>
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/sched.h>
  23. #include <linux/slab.h>
  24. #include <linux/version.h>
  25. #define N_DATA_REGS 8
  26. #define N_DATA_BYTES (N_DATA_REGS * 4)
  27. /* BSC count register field definitions */
  28. #define BSC_CNT_REG1_MASK 0x0000003f
  29. #define BSC_CNT_REG1_SHIFT 0
  30. #define BSC_CNT_REG2_MASK 0x00000fc0
  31. #define BSC_CNT_REG2_SHIFT 6
  32. /* BSC CTL register field definitions */
  33. #define BSC_CTL_REG_DTF_MASK 0x00000003
  34. #define BSC_CTL_REG_SCL_SEL_MASK 0x00000030
  35. #define BSC_CTL_REG_SCL_SEL_SHIFT 4
  36. #define BSC_CTL_REG_INT_EN_MASK 0x00000040
  37. #define BSC_CTL_REG_INT_EN_SHIFT 6
  38. #define BSC_CTL_REG_DIV_CLK_MASK 0x00000080
  39. /* BSC_IIC_ENABLE r/w enable and interrupt field defintions */
  40. #define BSC_IIC_EN_RESTART_MASK 0x00000040
  41. #define BSC_IIC_EN_NOSTART_MASK 0x00000020
  42. #define BSC_IIC_EN_NOSTOP_MASK 0x00000010
  43. #define BSC_IIC_EN_NOACK_MASK 0x00000004
  44. #define BSC_IIC_EN_INTRP_MASK 0x00000002
  45. #define BSC_IIC_EN_ENABLE_MASK 0x00000001
  46. /* BSC_CTLHI control register field definitions */
  47. #define BSC_CTLHI_REG_INPUT_SWITCHING_LEVEL_MASK 0x00000080
  48. #define BSC_CTLHI_REG_DATAREG_SIZE_MASK 0x00000040
  49. #define BSC_CTLHI_REG_IGNORE_ACK_MASK 0x00000002
  50. #define BSC_CTLHI_REG_WAIT_DIS_MASK 0x00000001
  51. #define I2C_TIMEOUT 100 /* msecs */
  52. /* Condition mask used for non combined transfer */
  53. #define COND_RESTART BSC_IIC_EN_RESTART_MASK
  54. #define COND_NOSTART BSC_IIC_EN_NOSTART_MASK
  55. #define COND_NOSTOP BSC_IIC_EN_NOSTOP_MASK
  56. #define COND_START_STOP (COND_RESTART | COND_NOSTART | COND_NOSTOP)
  57. /* BSC data transfer direction */
  58. #define DTF_WR_MASK 0x00000000
  59. #define DTF_RD_MASK 0x00000001
  60. /* BSC data transfer direction combined format */
  61. #define DTF_RD_WR_MASK 0x00000002
  62. #define DTF_WR_RD_MASK 0x00000003
  63. #define INT_ENABLE true
  64. #define INT_DISABLE false
  65. /* BSC block register map structure to cache fields to be written */
  66. struct bsc_regs {
  67. u32 chip_address; /* slave address */
  68. u32 data_in[N_DATA_REGS]; /* tx data buffer*/
  69. u32 cnt_reg; /* rx/tx data length */
  70. u32 ctl_reg; /* control register */
  71. u32 iic_enable; /* xfer enable and status */
  72. u32 data_out[N_DATA_REGS]; /* rx data buffer */
  73. u32 ctlhi_reg; /* more control fields */
  74. u32 scl_param; /* reserved */
  75. };
  76. struct bsc_clk_param {
  77. u32 hz;
  78. u32 scl_mask;
  79. u32 div_mask;
  80. };
  81. enum bsc_xfer_cmd {
  82. CMD_WR,
  83. CMD_RD,
  84. CMD_WR_NOACK,
  85. CMD_RD_NOACK,
  86. };
  87. static char const *cmd_string[] = {
  88. [CMD_WR] = "WR",
  89. [CMD_RD] = "RD",
  90. [CMD_WR_NOACK] = "WR NOACK",
  91. [CMD_RD_NOACK] = "RD NOACK",
  92. };
  93. enum bus_speeds {
  94. SPD_375K,
  95. SPD_390K,
  96. SPD_187K,
  97. SPD_200K,
  98. SPD_93K,
  99. SPD_97K,
  100. SPD_46K,
  101. SPD_50K
  102. };
  103. static const struct bsc_clk_param bsc_clk[] = {
  104. [SPD_375K] = {
  105. .hz = 375000,
  106. .scl_mask = SPD_375K << BSC_CTL_REG_SCL_SEL_SHIFT,
  107. .div_mask = 0
  108. },
  109. [SPD_390K] = {
  110. .hz = 390000,
  111. .scl_mask = SPD_390K << BSC_CTL_REG_SCL_SEL_SHIFT,
  112. .div_mask = 0
  113. },
  114. [SPD_187K] = {
  115. .hz = 187500,
  116. .scl_mask = SPD_187K << BSC_CTL_REG_SCL_SEL_SHIFT,
  117. .div_mask = 0
  118. },
  119. [SPD_200K] = {
  120. .hz = 200000,
  121. .scl_mask = SPD_200K << BSC_CTL_REG_SCL_SEL_SHIFT,
  122. .div_mask = 0
  123. },
  124. [SPD_93K] = {
  125. .hz = 93750,
  126. .scl_mask = SPD_375K << BSC_CTL_REG_SCL_SEL_SHIFT,
  127. .div_mask = BSC_CTL_REG_DIV_CLK_MASK
  128. },
  129. [SPD_97K] = {
  130. .hz = 97500,
  131. .scl_mask = SPD_390K << BSC_CTL_REG_SCL_SEL_SHIFT,
  132. .div_mask = BSC_CTL_REG_DIV_CLK_MASK
  133. },
  134. [SPD_46K] = {
  135. .hz = 46875,
  136. .scl_mask = SPD_187K << BSC_CTL_REG_SCL_SEL_SHIFT,
  137. .div_mask = BSC_CTL_REG_DIV_CLK_MASK
  138. },
  139. [SPD_50K] = {
  140. .hz = 50000,
  141. .scl_mask = SPD_200K << BSC_CTL_REG_SCL_SEL_SHIFT,
  142. .div_mask = BSC_CTL_REG_DIV_CLK_MASK
  143. }
  144. };
  145. struct brcmstb_i2c_dev {
  146. struct device *device;
  147. void __iomem *base;
  148. void __iomem *irq_base;
  149. int irq;
  150. struct bsc_regs *bsc_regmap;
  151. struct i2c_adapter adapter;
  152. struct completion done;
  153. bool is_suspended;
  154. u32 clk_freq_hz;
  155. };
  156. /* register accessors for both be and le cpu arch */
  157. #ifdef CONFIG_CPU_BIG_ENDIAN
  158. #define __bsc_readl(_reg) ioread32be(_reg)
  159. #define __bsc_writel(_val, _reg) iowrite32be(_val, _reg)
  160. #else
  161. #define __bsc_readl(_reg) ioread32(_reg)
  162. #define __bsc_writel(_val, _reg) iowrite32(_val, _reg)
  163. #endif
  164. #define bsc_readl(_dev, _reg) \
  165. __bsc_readl(_dev->base + offsetof(struct bsc_regs, _reg))
  166. #define bsc_writel(_dev, _val, _reg) \
  167. __bsc_writel(_val, _dev->base + offsetof(struct bsc_regs, _reg))
  168. static void brcmstb_i2c_enable_disable_irq(struct brcmstb_i2c_dev *dev,
  169. bool int_en)
  170. {
  171. if (int_en)
  172. /* Enable BSC CTL interrupt line */
  173. dev->bsc_regmap->ctl_reg |= BSC_CTL_REG_INT_EN_MASK;
  174. else
  175. /* Disable BSC CTL interrupt line */
  176. dev->bsc_regmap->ctl_reg &= ~BSC_CTL_REG_INT_EN_MASK;
  177. barrier();
  178. bsc_writel(dev, dev->bsc_regmap->ctl_reg, ctl_reg);
  179. }
  180. static irqreturn_t brcmstb_i2c_isr(int irq, void *devid)
  181. {
  182. struct brcmstb_i2c_dev *dev = devid;
  183. u32 status_bsc_ctl = bsc_readl(dev, ctl_reg);
  184. u32 status_iic_intrp = bsc_readl(dev, iic_enable);
  185. dev_dbg(dev->device, "isr CTL_REG %x IIC_EN %x\n",
  186. status_bsc_ctl, status_iic_intrp);
  187. if (!(status_bsc_ctl & BSC_CTL_REG_INT_EN_MASK))
  188. return IRQ_NONE;
  189. brcmstb_i2c_enable_disable_irq(dev, INT_DISABLE);
  190. complete_all(&dev->done);
  191. dev_dbg(dev->device, "isr handled");
  192. return IRQ_HANDLED;
  193. }
  194. /* Wait for device to be ready */
  195. static int brcmstb_i2c_wait_if_busy(struct brcmstb_i2c_dev *dev)
  196. {
  197. unsigned long timeout = jiffies + msecs_to_jiffies(I2C_TIMEOUT);
  198. while ((bsc_readl(dev, iic_enable) & BSC_IIC_EN_INTRP_MASK)) {
  199. if (time_after(jiffies, timeout))
  200. return -ETIMEDOUT;
  201. cpu_relax();
  202. }
  203. return 0;
  204. }
  205. /* i2c xfer completion function, handles both irq and polling mode */
  206. static int brcmstb_i2c_wait_for_completion(struct brcmstb_i2c_dev *dev)
  207. {
  208. int ret = 0;
  209. unsigned long timeout = msecs_to_jiffies(I2C_TIMEOUT);
  210. if (dev->irq >= 0) {
  211. if (!wait_for_completion_timeout(&dev->done, timeout))
  212. ret = -ETIMEDOUT;
  213. } else {
  214. /* we are in polling mode */
  215. u32 bsc_intrp;
  216. unsigned long time_left = jiffies + timeout;
  217. do {
  218. bsc_intrp = bsc_readl(dev, iic_enable) &
  219. BSC_IIC_EN_INTRP_MASK;
  220. if (time_after(jiffies, time_left)) {
  221. ret = -ETIMEDOUT;
  222. break;
  223. }
  224. cpu_relax();
  225. } while (!bsc_intrp);
  226. }
  227. if (dev->irq < 0 || ret == -ETIMEDOUT)
  228. brcmstb_i2c_enable_disable_irq(dev, INT_DISABLE);
  229. return ret;
  230. }
  231. /* Set xfer START/STOP conditions for subsequent transfer */
  232. static void brcmstb_set_i2c_start_stop(struct brcmstb_i2c_dev *dev,
  233. u32 cond_flag)
  234. {
  235. u32 regval = dev->bsc_regmap->iic_enable;
  236. dev->bsc_regmap->iic_enable = (regval & ~COND_START_STOP) | cond_flag;
  237. }
  238. /* Send I2C request check completion */
  239. static int brcmstb_send_i2c_cmd(struct brcmstb_i2c_dev *dev,
  240. enum bsc_xfer_cmd cmd)
  241. {
  242. int rc = 0;
  243. struct bsc_regs *pi2creg = dev->bsc_regmap;
  244. /* Make sure the hardware is ready */
  245. rc = brcmstb_i2c_wait_if_busy(dev);
  246. if (rc < 0)
  247. return rc;
  248. /* only if we are in interrupt mode */
  249. if (dev->irq >= 0)
  250. reinit_completion(&dev->done);
  251. /* enable BSC CTL interrupt line */
  252. brcmstb_i2c_enable_disable_irq(dev, INT_ENABLE);
  253. /* initiate transfer by setting iic_enable */
  254. pi2creg->iic_enable |= BSC_IIC_EN_ENABLE_MASK;
  255. bsc_writel(dev, pi2creg->iic_enable, iic_enable);
  256. /* Wait for transaction to finish or timeout */
  257. rc = brcmstb_i2c_wait_for_completion(dev);
  258. if (rc) {
  259. dev_dbg(dev->device, "intr timeout for cmd %s\n",
  260. cmd_string[cmd]);
  261. goto cmd_out;
  262. }
  263. if ((CMD_RD || CMD_WR) &&
  264. bsc_readl(dev, iic_enable) & BSC_IIC_EN_NOACK_MASK) {
  265. rc = -EREMOTEIO;
  266. dev_dbg(dev->device, "controller received NOACK intr for %s\n",
  267. cmd_string[cmd]);
  268. }
  269. cmd_out:
  270. bsc_writel(dev, 0, cnt_reg);
  271. bsc_writel(dev, 0, iic_enable);
  272. return rc;
  273. }
  274. /* Actual data transfer through the BSC master */
  275. static int brcmstb_i2c_xfer_bsc_data(struct brcmstb_i2c_dev *dev,
  276. u8 *buf, unsigned int len,
  277. struct i2c_msg *pmsg)
  278. {
  279. int cnt, byte, rc;
  280. enum bsc_xfer_cmd cmd;
  281. u32 ctl_reg;
  282. struct bsc_regs *pi2creg = dev->bsc_regmap;
  283. int no_ack = pmsg->flags & I2C_M_IGNORE_NAK;
  284. /* see if the transaction needs to check NACK conditions */
  285. if (no_ack || len <= N_DATA_BYTES) {
  286. cmd = (pmsg->flags & I2C_M_RD) ? CMD_RD_NOACK
  287. : CMD_WR_NOACK;
  288. pi2creg->ctlhi_reg |= BSC_CTLHI_REG_IGNORE_ACK_MASK;
  289. } else {
  290. cmd = (pmsg->flags & I2C_M_RD) ? CMD_RD : CMD_WR;
  291. pi2creg->ctlhi_reg &= ~BSC_CTLHI_REG_IGNORE_ACK_MASK;
  292. }
  293. bsc_writel(dev, pi2creg->ctlhi_reg, ctlhi_reg);
  294. /* set data transfer direction */
  295. ctl_reg = pi2creg->ctl_reg & ~BSC_CTL_REG_DTF_MASK;
  296. if (cmd == CMD_WR || cmd == CMD_WR_NOACK)
  297. pi2creg->ctl_reg = ctl_reg | DTF_WR_MASK;
  298. else
  299. pi2creg->ctl_reg = ctl_reg | DTF_RD_MASK;
  300. /* set the read/write length */
  301. bsc_writel(dev, BSC_CNT_REG1_MASK & (len << BSC_CNT_REG1_SHIFT),
  302. cnt_reg);
  303. /* Write data into data_in register */
  304. if (cmd == CMD_WR || cmd == CMD_WR_NOACK) {
  305. for (cnt = 0; cnt < len; cnt += 4) {
  306. u32 word = 0;
  307. for (byte = 0; byte < 4; byte++) {
  308. word >>= 8;
  309. if ((cnt + byte) < len)
  310. word |= buf[cnt + byte] << 24;
  311. }
  312. bsc_writel(dev, word, data_in[cnt >> 2]);
  313. }
  314. }
  315. /* Initiate xfer, the function will return on completion */
  316. rc = brcmstb_send_i2c_cmd(dev, cmd);
  317. if (rc != 0) {
  318. dev_dbg(dev->device, "%s failure", cmd_string[cmd]);
  319. return rc;
  320. }
  321. if (cmd == CMD_RD || cmd == CMD_RD_NOACK) {
  322. for (cnt = 0; cnt < len; cnt += 4) {
  323. u32 data = bsc_readl(dev, data_out[cnt >> 2]);
  324. for (byte = 0; byte < 4 &&
  325. (byte + cnt) < len; byte++) {
  326. buf[cnt + byte] = data & 0xff;
  327. data >>= 8;
  328. }
  329. }
  330. }
  331. return 0;
  332. }
  333. /* Write a single byte of data to the i2c bus */
  334. static int brcmstb_i2c_write_data_byte(struct brcmstb_i2c_dev *dev,
  335. u8 *buf, unsigned int nak_expected)
  336. {
  337. enum bsc_xfer_cmd cmd = nak_expected ? CMD_WR : CMD_WR_NOACK;
  338. bsc_writel(dev, 1, cnt_reg);
  339. bsc_writel(dev, *buf, data_in);
  340. return brcmstb_send_i2c_cmd(dev, cmd);
  341. }
  342. /* Send i2c address */
  343. static int brcmstb_i2c_do_addr(struct brcmstb_i2c_dev *dev,
  344. struct i2c_msg *msg)
  345. {
  346. unsigned char addr;
  347. if (msg->flags & I2C_M_TEN) {
  348. /* First byte is 11110XX0 where XX is upper 2 bits */
  349. addr = 0xF0 | ((msg->addr & 0x300) >> 7);
  350. bsc_writel(dev, addr, chip_address);
  351. /* Second byte is the remaining 8 bits */
  352. addr = msg->addr & 0xFF;
  353. if (brcmstb_i2c_write_data_byte(dev, &addr, 0) < 0)
  354. return -EREMOTEIO;
  355. if (msg->flags & I2C_M_RD) {
  356. /* For read, send restart without stop condition */
  357. brcmstb_set_i2c_start_stop(dev, COND_RESTART
  358. | COND_NOSTOP);
  359. /* Then re-send the first byte with the read bit set */
  360. addr = 0xF0 | ((msg->addr & 0x300) >> 7) | 0x01;
  361. if (brcmstb_i2c_write_data_byte(dev, &addr, 0) < 0)
  362. return -EREMOTEIO;
  363. }
  364. } else {
  365. addr = msg->addr << 1;
  366. if (msg->flags & I2C_M_RD)
  367. addr |= 1;
  368. bsc_writel(dev, addr, chip_address);
  369. }
  370. return 0;
  371. }
  372. /* Master transfer function */
  373. static int brcmstb_i2c_xfer(struct i2c_adapter *adapter,
  374. struct i2c_msg msgs[], int num)
  375. {
  376. struct brcmstb_i2c_dev *dev = i2c_get_adapdata(adapter);
  377. struct i2c_msg *pmsg;
  378. int rc = 0;
  379. int i;
  380. int bytes_to_xfer;
  381. u8 *tmp_buf;
  382. int len = 0;
  383. if (dev->is_suspended)
  384. return -EBUSY;
  385. /* Loop through all messages */
  386. for (i = 0; i < num; i++) {
  387. pmsg = &msgs[i];
  388. len = pmsg->len;
  389. tmp_buf = pmsg->buf;
  390. dev_dbg(dev->device,
  391. "msg# %d/%d flg %x buf %x len %d\n", i,
  392. num - 1, pmsg->flags,
  393. pmsg->buf ? pmsg->buf[0] : '0', pmsg->len);
  394. if (i < (num - 1) && (msgs[i + 1].flags & I2C_M_NOSTART))
  395. brcmstb_set_i2c_start_stop(dev, ~(COND_START_STOP));
  396. else
  397. brcmstb_set_i2c_start_stop(dev,
  398. COND_RESTART | COND_NOSTOP);
  399. /* Send slave address */
  400. if (!(pmsg->flags & I2C_M_NOSTART)) {
  401. rc = brcmstb_i2c_do_addr(dev, pmsg);
  402. if (rc < 0) {
  403. dev_dbg(dev->device,
  404. "NACK for addr %2.2x msg#%d rc = %d\n",
  405. pmsg->addr, i, rc);
  406. goto out;
  407. }
  408. }
  409. /* Perform data transfer */
  410. while (len) {
  411. bytes_to_xfer = min(len, N_DATA_BYTES);
  412. if (len <= N_DATA_BYTES && i == (num - 1))
  413. brcmstb_set_i2c_start_stop(dev,
  414. ~(COND_START_STOP));
  415. rc = brcmstb_i2c_xfer_bsc_data(dev, tmp_buf,
  416. bytes_to_xfer, pmsg);
  417. if (rc < 0)
  418. goto out;
  419. len -= bytes_to_xfer;
  420. tmp_buf += bytes_to_xfer;
  421. }
  422. }
  423. rc = num;
  424. out:
  425. return rc;
  426. }
  427. static u32 brcmstb_i2c_functionality(struct i2c_adapter *adap)
  428. {
  429. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_10BIT_ADDR
  430. | I2C_FUNC_NOSTART | I2C_FUNC_PROTOCOL_MANGLING;
  431. }
  432. static const struct i2c_algorithm brcmstb_i2c_algo = {
  433. .master_xfer = brcmstb_i2c_xfer,
  434. .functionality = brcmstb_i2c_functionality,
  435. };
  436. static void brcmstb_i2c_set_bus_speed(struct brcmstb_i2c_dev *dev)
  437. {
  438. int i = 0, num_speeds = ARRAY_SIZE(bsc_clk);
  439. u32 clk_freq_hz = dev->clk_freq_hz;
  440. for (i = 0; i < num_speeds; i++) {
  441. if (bsc_clk[i].hz == clk_freq_hz) {
  442. dev->bsc_regmap->ctl_reg &= ~(BSC_CTL_REG_SCL_SEL_MASK
  443. | BSC_CTL_REG_DIV_CLK_MASK);
  444. dev->bsc_regmap->ctl_reg |= (bsc_clk[i].scl_mask |
  445. bsc_clk[i].div_mask);
  446. bsc_writel(dev, dev->bsc_regmap->ctl_reg, ctl_reg);
  447. break;
  448. }
  449. }
  450. /* in case we did not get find a valid speed */
  451. if (i == num_speeds) {
  452. i = (bsc_readl(dev, ctl_reg) & BSC_CTL_REG_SCL_SEL_MASK) >>
  453. BSC_CTL_REG_SCL_SEL_SHIFT;
  454. dev_warn(dev->device, "leaving current clock-frequency @ %dHz\n",
  455. bsc_clk[i].hz);
  456. }
  457. }
  458. static void brcmstb_i2c_set_bsc_reg_defaults(struct brcmstb_i2c_dev *dev)
  459. {
  460. /* 4 byte data register */
  461. dev->bsc_regmap->ctlhi_reg = BSC_CTLHI_REG_DATAREG_SIZE_MASK;
  462. bsc_writel(dev, dev->bsc_regmap->ctlhi_reg, ctlhi_reg);
  463. /* set bus speed */
  464. brcmstb_i2c_set_bus_speed(dev);
  465. }
  466. static int brcmstb_i2c_probe(struct platform_device *pdev)
  467. {
  468. int rc = 0;
  469. struct brcmstb_i2c_dev *dev;
  470. struct i2c_adapter *adap;
  471. struct resource *iomem;
  472. const char *int_name;
  473. /* Allocate memory for private data structure */
  474. dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
  475. if (!dev)
  476. return -ENOMEM;
  477. dev->bsc_regmap = devm_kzalloc(&pdev->dev, sizeof(*dev->bsc_regmap), GFP_KERNEL);
  478. if (!dev->bsc_regmap)
  479. return -ENOMEM;
  480. platform_set_drvdata(pdev, dev);
  481. dev->device = &pdev->dev;
  482. init_completion(&dev->done);
  483. /* Map hardware registers */
  484. iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  485. dev->base = devm_ioremap_resource(dev->device, iomem);
  486. if (IS_ERR(dev->base)) {
  487. rc = -ENOMEM;
  488. goto probe_errorout;
  489. }
  490. rc = of_property_read_string(dev->device->of_node, "interrupt-names",
  491. &int_name);
  492. if (rc < 0)
  493. int_name = NULL;
  494. /* Get the interrupt number */
  495. dev->irq = platform_get_irq(pdev, 0);
  496. /* disable the bsc interrupt line */
  497. brcmstb_i2c_enable_disable_irq(dev, INT_DISABLE);
  498. /* register the ISR handler */
  499. rc = devm_request_irq(&pdev->dev, dev->irq, brcmstb_i2c_isr,
  500. IRQF_SHARED,
  501. int_name ? int_name : pdev->name,
  502. dev);
  503. if (rc) {
  504. dev_dbg(dev->device, "falling back to polling mode");
  505. dev->irq = -1;
  506. }
  507. if (of_property_read_u32(dev->device->of_node,
  508. "clock-frequency", &dev->clk_freq_hz)) {
  509. dev_warn(dev->device, "setting clock-frequency@%dHz\n",
  510. bsc_clk[0].hz);
  511. dev->clk_freq_hz = bsc_clk[0].hz;
  512. }
  513. brcmstb_i2c_set_bsc_reg_defaults(dev);
  514. /* Add the i2c adapter */
  515. adap = &dev->adapter;
  516. i2c_set_adapdata(adap, dev);
  517. adap->owner = THIS_MODULE;
  518. strlcpy(adap->name, "Broadcom STB : ", sizeof(adap->name));
  519. if (int_name)
  520. strlcat(adap->name, int_name, sizeof(adap->name));
  521. adap->algo = &brcmstb_i2c_algo;
  522. adap->dev.parent = &pdev->dev;
  523. adap->dev.of_node = pdev->dev.of_node;
  524. rc = i2c_add_adapter(adap);
  525. if (rc) {
  526. dev_err(dev->device, "failed to add adapter\n");
  527. goto probe_errorout;
  528. }
  529. dev_info(dev->device, "%s@%dhz registered in %s mode\n",
  530. int_name ? int_name : " ", dev->clk_freq_hz,
  531. (dev->irq >= 0) ? "interrupt" : "polling");
  532. return 0;
  533. probe_errorout:
  534. return rc;
  535. }
  536. static int brcmstb_i2c_remove(struct platform_device *pdev)
  537. {
  538. struct brcmstb_i2c_dev *dev = platform_get_drvdata(pdev);
  539. i2c_del_adapter(&dev->adapter);
  540. return 0;
  541. }
  542. #ifdef CONFIG_PM_SLEEP
  543. static int brcmstb_i2c_suspend(struct device *dev)
  544. {
  545. struct brcmstb_i2c_dev *i2c_dev = dev_get_drvdata(dev);
  546. i2c_lock_adapter(&i2c_dev->adapter);
  547. i2c_dev->is_suspended = true;
  548. i2c_unlock_adapter(&i2c_dev->adapter);
  549. return 0;
  550. }
  551. static int brcmstb_i2c_resume(struct device *dev)
  552. {
  553. struct brcmstb_i2c_dev *i2c_dev = dev_get_drvdata(dev);
  554. i2c_lock_adapter(&i2c_dev->adapter);
  555. brcmstb_i2c_set_bsc_reg_defaults(i2c_dev);
  556. i2c_dev->is_suspended = false;
  557. i2c_unlock_adapter(&i2c_dev->adapter);
  558. return 0;
  559. }
  560. #endif
  561. static SIMPLE_DEV_PM_OPS(brcmstb_i2c_pm, brcmstb_i2c_suspend,
  562. brcmstb_i2c_resume);
  563. static const struct of_device_id brcmstb_i2c_of_match[] = {
  564. {.compatible = "brcm,brcmstb-i2c"},
  565. {},
  566. };
  567. MODULE_DEVICE_TABLE(of, brcmstb_i2c_of_match);
  568. static struct platform_driver brcmstb_i2c_driver = {
  569. .driver = {
  570. .name = "brcmstb-i2c",
  571. .of_match_table = brcmstb_i2c_of_match,
  572. .pm = &brcmstb_i2c_pm,
  573. },
  574. .probe = brcmstb_i2c_probe,
  575. .remove = brcmstb_i2c_remove,
  576. };
  577. module_platform_driver(brcmstb_i2c_driver);
  578. MODULE_AUTHOR("Kamal Dasu <kdasu@broadcom.com>");
  579. MODULE_DESCRIPTION("Broadcom Settop I2C Driver");
  580. MODULE_LICENSE("GPL v2");