i2c-qcom-cci.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (c) 2012-2016, The Linux Foundation. All rights reserved.
  3. // Copyright (c) 2017-2022 Linaro Limited.
  4. #include <linux/clk.h>
  5. #include <linux/completion.h>
  6. #include <linux/i2c.h>
  7. #include <linux/io.h>
  8. #include <linux/interrupt.h>
  9. #include <linux/module.h>
  10. #include <linux/of.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/pm_runtime.h>
  13. #define CCI_HW_VERSION 0x0
  14. #define CCI_RESET_CMD 0x004
  15. #define CCI_RESET_CMD_MASK 0x0f73f3f7
  16. #define CCI_RESET_CMD_M0_MASK 0x000003f1
  17. #define CCI_RESET_CMD_M1_MASK 0x0003f001
  18. #define CCI_QUEUE_START 0x008
  19. #define CCI_HALT_REQ 0x034
  20. #define CCI_HALT_REQ_I2C_M0_Q0Q1 BIT(0)
  21. #define CCI_HALT_REQ_I2C_M1_Q0Q1 BIT(1)
  22. #define CCI_I2C_Mm_SCL_CTL(m) (0x100 + 0x100 * (m))
  23. #define CCI_I2C_Mm_SDA_CTL_0(m) (0x104 + 0x100 * (m))
  24. #define CCI_I2C_Mm_SDA_CTL_1(m) (0x108 + 0x100 * (m))
  25. #define CCI_I2C_Mm_SDA_CTL_2(m) (0x10c + 0x100 * (m))
  26. #define CCI_I2C_Mm_MISC_CTL(m) (0x110 + 0x100 * (m))
  27. #define CCI_I2C_Mm_READ_DATA(m) (0x118 + 0x100 * (m))
  28. #define CCI_I2C_Mm_READ_BUF_LEVEL(m) (0x11c + 0x100 * (m))
  29. #define CCI_I2C_Mm_Qn_EXEC_WORD_CNT(m, n) (0x300 + 0x200 * (m) + 0x100 * (n))
  30. #define CCI_I2C_Mm_Qn_CUR_WORD_CNT(m, n) (0x304 + 0x200 * (m) + 0x100 * (n))
  31. #define CCI_I2C_Mm_Qn_CUR_CMD(m, n) (0x308 + 0x200 * (m) + 0x100 * (n))
  32. #define CCI_I2C_Mm_Qn_REPORT_STATUS(m, n) (0x30c + 0x200 * (m) + 0x100 * (n))
  33. #define CCI_I2C_Mm_Qn_LOAD_DATA(m, n) (0x310 + 0x200 * (m) + 0x100 * (n))
  34. #define CCI_IRQ_GLOBAL_CLEAR_CMD 0xc00
  35. #define CCI_IRQ_MASK_0 0xc04
  36. #define CCI_IRQ_MASK_0_I2C_M0_RD_DONE BIT(0)
  37. #define CCI_IRQ_MASK_0_I2C_M0_Q0_REPORT BIT(4)
  38. #define CCI_IRQ_MASK_0_I2C_M0_Q1_REPORT BIT(8)
  39. #define CCI_IRQ_MASK_0_I2C_M1_RD_DONE BIT(12)
  40. #define CCI_IRQ_MASK_0_I2C_M1_Q0_REPORT BIT(16)
  41. #define CCI_IRQ_MASK_0_I2C_M1_Q1_REPORT BIT(20)
  42. #define CCI_IRQ_MASK_0_RST_DONE_ACK BIT(24)
  43. #define CCI_IRQ_MASK_0_I2C_M0_Q0Q1_HALT_ACK BIT(25)
  44. #define CCI_IRQ_MASK_0_I2C_M1_Q0Q1_HALT_ACK BIT(26)
  45. #define CCI_IRQ_MASK_0_I2C_M0_ERROR 0x18000ee6
  46. #define CCI_IRQ_MASK_0_I2C_M1_ERROR 0x60ee6000
  47. #define CCI_IRQ_CLEAR_0 0xc08
  48. #define CCI_IRQ_STATUS_0 0xc0c
  49. #define CCI_IRQ_STATUS_0_I2C_M0_RD_DONE BIT(0)
  50. #define CCI_IRQ_STATUS_0_I2C_M0_Q0_REPORT BIT(4)
  51. #define CCI_IRQ_STATUS_0_I2C_M0_Q1_REPORT BIT(8)
  52. #define CCI_IRQ_STATUS_0_I2C_M1_RD_DONE BIT(12)
  53. #define CCI_IRQ_STATUS_0_I2C_M1_Q0_REPORT BIT(16)
  54. #define CCI_IRQ_STATUS_0_I2C_M1_Q1_REPORT BIT(20)
  55. #define CCI_IRQ_STATUS_0_RST_DONE_ACK BIT(24)
  56. #define CCI_IRQ_STATUS_0_I2C_M0_Q0Q1_HALT_ACK BIT(25)
  57. #define CCI_IRQ_STATUS_0_I2C_M1_Q0Q1_HALT_ACK BIT(26)
  58. #define CCI_IRQ_STATUS_0_I2C_M0_Q0_NACK_ERR BIT(27)
  59. #define CCI_IRQ_STATUS_0_I2C_M0_Q1_NACK_ERR BIT(28)
  60. #define CCI_IRQ_STATUS_0_I2C_M1_Q0_NACK_ERR BIT(29)
  61. #define CCI_IRQ_STATUS_0_I2C_M1_Q1_NACK_ERR BIT(30)
  62. #define CCI_IRQ_STATUS_0_I2C_M0_ERROR 0x18000ee6
  63. #define CCI_IRQ_STATUS_0_I2C_M1_ERROR 0x60ee6000
  64. #define CCI_TIMEOUT (msecs_to_jiffies(100))
  65. #define NUM_MASTERS 2
  66. #define NUM_QUEUES 2
  67. /* Max number of resources + 1 for a NULL terminator */
  68. #define CCI_RES_MAX 6
  69. #define CCI_I2C_SET_PARAM 1
  70. #define CCI_I2C_REPORT 8
  71. #define CCI_I2C_WRITE 9
  72. #define CCI_I2C_READ 10
  73. #define CCI_I2C_REPORT_IRQ_EN BIT(8)
  74. enum {
  75. I2C_MODE_STANDARD,
  76. I2C_MODE_FAST,
  77. I2C_MODE_FAST_PLUS,
  78. };
  79. enum cci_i2c_queue_t {
  80. QUEUE_0,
  81. QUEUE_1
  82. };
  83. struct hw_params {
  84. u16 thigh; /* HIGH period of the SCL clock in clock ticks */
  85. u16 tlow; /* LOW period of the SCL clock */
  86. u16 tsu_sto; /* set-up time for STOP condition */
  87. u16 tsu_sta; /* set-up time for a repeated START condition */
  88. u16 thd_dat; /* data hold time */
  89. u16 thd_sta; /* hold time (repeated) START condition */
  90. u16 tbuf; /* bus free time between a STOP and START condition */
  91. u8 scl_stretch_en;
  92. u16 trdhld;
  93. u16 tsp; /* pulse width of spikes suppressed by the input filter */
  94. };
  95. struct cci;
  96. struct cci_master {
  97. struct i2c_adapter adap;
  98. u16 master;
  99. u8 mode;
  100. int status;
  101. struct completion irq_complete;
  102. struct cci *cci;
  103. };
  104. struct cci_data {
  105. unsigned int num_masters;
  106. struct i2c_adapter_quirks quirks;
  107. u16 queue_size[NUM_QUEUES];
  108. unsigned long cci_clk_rate;
  109. struct hw_params params[3];
  110. };
  111. struct cci {
  112. struct device *dev;
  113. void __iomem *base;
  114. unsigned int irq;
  115. const struct cci_data *data;
  116. struct clk_bulk_data *clocks;
  117. int nclocks;
  118. struct cci_master master[NUM_MASTERS];
  119. };
  120. static irqreturn_t cci_isr(int irq, void *dev)
  121. {
  122. struct cci *cci = dev;
  123. u32 val, reset = 0;
  124. int ret = IRQ_NONE;
  125. val = readl(cci->base + CCI_IRQ_STATUS_0);
  126. writel(val, cci->base + CCI_IRQ_CLEAR_0);
  127. writel(0x1, cci->base + CCI_IRQ_GLOBAL_CLEAR_CMD);
  128. if (val & CCI_IRQ_STATUS_0_RST_DONE_ACK) {
  129. complete(&cci->master[0].irq_complete);
  130. if (cci->master[1].master)
  131. complete(&cci->master[1].irq_complete);
  132. ret = IRQ_HANDLED;
  133. }
  134. if (val & CCI_IRQ_STATUS_0_I2C_M0_RD_DONE ||
  135. val & CCI_IRQ_STATUS_0_I2C_M0_Q0_REPORT ||
  136. val & CCI_IRQ_STATUS_0_I2C_M0_Q1_REPORT) {
  137. cci->master[0].status = 0;
  138. complete(&cci->master[0].irq_complete);
  139. ret = IRQ_HANDLED;
  140. }
  141. if (val & CCI_IRQ_STATUS_0_I2C_M1_RD_DONE ||
  142. val & CCI_IRQ_STATUS_0_I2C_M1_Q0_REPORT ||
  143. val & CCI_IRQ_STATUS_0_I2C_M1_Q1_REPORT) {
  144. cci->master[1].status = 0;
  145. complete(&cci->master[1].irq_complete);
  146. ret = IRQ_HANDLED;
  147. }
  148. if (unlikely(val & CCI_IRQ_STATUS_0_I2C_M0_Q0Q1_HALT_ACK)) {
  149. reset = CCI_RESET_CMD_M0_MASK;
  150. ret = IRQ_HANDLED;
  151. }
  152. if (unlikely(val & CCI_IRQ_STATUS_0_I2C_M1_Q0Q1_HALT_ACK)) {
  153. reset = CCI_RESET_CMD_M1_MASK;
  154. ret = IRQ_HANDLED;
  155. }
  156. if (unlikely(reset))
  157. writel(reset, cci->base + CCI_RESET_CMD);
  158. if (unlikely(val & CCI_IRQ_STATUS_0_I2C_M0_ERROR)) {
  159. if (val & CCI_IRQ_STATUS_0_I2C_M0_Q0_NACK_ERR ||
  160. val & CCI_IRQ_STATUS_0_I2C_M0_Q1_NACK_ERR)
  161. cci->master[0].status = -ENXIO;
  162. else
  163. cci->master[0].status = -EIO;
  164. writel(CCI_HALT_REQ_I2C_M0_Q0Q1, cci->base + CCI_HALT_REQ);
  165. ret = IRQ_HANDLED;
  166. }
  167. if (unlikely(val & CCI_IRQ_STATUS_0_I2C_M1_ERROR)) {
  168. if (val & CCI_IRQ_STATUS_0_I2C_M1_Q0_NACK_ERR ||
  169. val & CCI_IRQ_STATUS_0_I2C_M1_Q1_NACK_ERR)
  170. cci->master[1].status = -ENXIO;
  171. else
  172. cci->master[1].status = -EIO;
  173. writel(CCI_HALT_REQ_I2C_M1_Q0Q1, cci->base + CCI_HALT_REQ);
  174. ret = IRQ_HANDLED;
  175. }
  176. return ret;
  177. }
  178. static int cci_halt(struct cci *cci, u8 master_num)
  179. {
  180. struct cci_master *master;
  181. u32 val;
  182. if (master_num >= cci->data->num_masters) {
  183. dev_err(cci->dev, "Unsupported master idx (%u)\n", master_num);
  184. return -EINVAL;
  185. }
  186. val = BIT(master_num);
  187. master = &cci->master[master_num];
  188. reinit_completion(&master->irq_complete);
  189. writel(val, cci->base + CCI_HALT_REQ);
  190. if (!wait_for_completion_timeout(&master->irq_complete, CCI_TIMEOUT)) {
  191. dev_err(cci->dev, "CCI halt timeout\n");
  192. return -ETIMEDOUT;
  193. }
  194. return 0;
  195. }
  196. static int cci_reset(struct cci *cci)
  197. {
  198. /*
  199. * we reset the whole controller, here and for implicity use
  200. * master[0].xxx for waiting on it.
  201. */
  202. reinit_completion(&cci->master[0].irq_complete);
  203. writel(CCI_RESET_CMD_MASK, cci->base + CCI_RESET_CMD);
  204. if (!wait_for_completion_timeout(&cci->master[0].irq_complete,
  205. CCI_TIMEOUT)) {
  206. dev_err(cci->dev, "CCI reset timeout\n");
  207. return -ETIMEDOUT;
  208. }
  209. return 0;
  210. }
  211. static int cci_init(struct cci *cci)
  212. {
  213. u32 val = CCI_IRQ_MASK_0_I2C_M0_RD_DONE |
  214. CCI_IRQ_MASK_0_I2C_M0_Q0_REPORT |
  215. CCI_IRQ_MASK_0_I2C_M0_Q1_REPORT |
  216. CCI_IRQ_MASK_0_I2C_M1_RD_DONE |
  217. CCI_IRQ_MASK_0_I2C_M1_Q0_REPORT |
  218. CCI_IRQ_MASK_0_I2C_M1_Q1_REPORT |
  219. CCI_IRQ_MASK_0_RST_DONE_ACK |
  220. CCI_IRQ_MASK_0_I2C_M0_Q0Q1_HALT_ACK |
  221. CCI_IRQ_MASK_0_I2C_M1_Q0Q1_HALT_ACK |
  222. CCI_IRQ_MASK_0_I2C_M0_ERROR |
  223. CCI_IRQ_MASK_0_I2C_M1_ERROR;
  224. int i;
  225. writel(val, cci->base + CCI_IRQ_MASK_0);
  226. for (i = 0; i < cci->data->num_masters; i++) {
  227. int mode = cci->master[i].mode;
  228. const struct hw_params *hw;
  229. if (!cci->master[i].cci)
  230. continue;
  231. hw = &cci->data->params[mode];
  232. val = hw->thigh << 16 | hw->tlow;
  233. writel(val, cci->base + CCI_I2C_Mm_SCL_CTL(i));
  234. val = hw->tsu_sto << 16 | hw->tsu_sta;
  235. writel(val, cci->base + CCI_I2C_Mm_SDA_CTL_0(i));
  236. val = hw->thd_dat << 16 | hw->thd_sta;
  237. writel(val, cci->base + CCI_I2C_Mm_SDA_CTL_1(i));
  238. val = hw->tbuf;
  239. writel(val, cci->base + CCI_I2C_Mm_SDA_CTL_2(i));
  240. val = hw->scl_stretch_en << 8 | hw->trdhld << 4 | hw->tsp;
  241. writel(val, cci->base + CCI_I2C_Mm_MISC_CTL(i));
  242. }
  243. return 0;
  244. }
  245. static int cci_run_queue(struct cci *cci, u8 master, u8 queue)
  246. {
  247. u32 val;
  248. val = readl(cci->base + CCI_I2C_Mm_Qn_CUR_WORD_CNT(master, queue));
  249. writel(val, cci->base + CCI_I2C_Mm_Qn_EXEC_WORD_CNT(master, queue));
  250. reinit_completion(&cci->master[master].irq_complete);
  251. val = BIT(master * 2 + queue);
  252. writel(val, cci->base + CCI_QUEUE_START);
  253. if (!wait_for_completion_timeout(&cci->master[master].irq_complete,
  254. CCI_TIMEOUT)) {
  255. dev_err(cci->dev, "master %d queue %d timeout\n",
  256. master, queue);
  257. cci_reset(cci);
  258. cci_init(cci);
  259. return -ETIMEDOUT;
  260. }
  261. return cci->master[master].status;
  262. }
  263. static int cci_validate_queue(struct cci *cci, u8 master, u8 queue)
  264. {
  265. u32 val;
  266. val = readl(cci->base + CCI_I2C_Mm_Qn_CUR_WORD_CNT(master, queue));
  267. if (val == cci->data->queue_size[queue])
  268. return -EINVAL;
  269. if (!val)
  270. return 0;
  271. val = CCI_I2C_REPORT | CCI_I2C_REPORT_IRQ_EN;
  272. writel(val, cci->base + CCI_I2C_Mm_Qn_LOAD_DATA(master, queue));
  273. return cci_run_queue(cci, master, queue);
  274. }
  275. static int cci_i2c_read(struct cci *cci, u16 master,
  276. u16 addr, u8 *buf, u16 len)
  277. {
  278. u32 val, words_read, words_exp;
  279. u8 queue = QUEUE_1;
  280. int i, index = 0, ret;
  281. bool first = true;
  282. /*
  283. * Call validate queue to make sure queue is empty before starting.
  284. * This is to avoid overflow / underflow of queue.
  285. */
  286. ret = cci_validate_queue(cci, master, queue);
  287. if (ret < 0)
  288. return ret;
  289. val = CCI_I2C_SET_PARAM | (addr & 0x7f) << 4;
  290. writel(val, cci->base + CCI_I2C_Mm_Qn_LOAD_DATA(master, queue));
  291. val = CCI_I2C_READ | len << 4;
  292. writel(val, cci->base + CCI_I2C_Mm_Qn_LOAD_DATA(master, queue));
  293. ret = cci_run_queue(cci, master, queue);
  294. if (ret < 0)
  295. return ret;
  296. words_read = readl(cci->base + CCI_I2C_Mm_READ_BUF_LEVEL(master));
  297. words_exp = len / 4 + 1;
  298. if (words_read != words_exp) {
  299. dev_err(cci->dev, "words read = %d, words expected = %d\n",
  300. words_read, words_exp);
  301. return -EIO;
  302. }
  303. do {
  304. val = readl(cci->base + CCI_I2C_Mm_READ_DATA(master));
  305. for (i = 0; i < 4 && index < len; i++) {
  306. if (first) {
  307. /* The LS byte of this register represents the
  308. * first byte read from the slave during a read
  309. * access.
  310. */
  311. first = false;
  312. continue;
  313. }
  314. buf[index++] = (val >> (i * 8)) & 0xff;
  315. }
  316. } while (--words_read);
  317. return 0;
  318. }
  319. static int cci_i2c_write(struct cci *cci, u16 master,
  320. u16 addr, u8 *buf, u16 len)
  321. {
  322. u8 queue = QUEUE_0;
  323. u8 load[12] = { 0 };
  324. int i = 0, j, ret;
  325. u32 val;
  326. /*
  327. * Call validate queue to make sure queue is empty before starting.
  328. * This is to avoid overflow / underflow of queue.
  329. */
  330. ret = cci_validate_queue(cci, master, queue);
  331. if (ret < 0)
  332. return ret;
  333. val = CCI_I2C_SET_PARAM | (addr & 0x7f) << 4;
  334. writel(val, cci->base + CCI_I2C_Mm_Qn_LOAD_DATA(master, queue));
  335. load[i++] = CCI_I2C_WRITE | len << 4;
  336. for (j = 0; j < len; j++)
  337. load[i++] = buf[j];
  338. for (j = 0; j < i; j += 4) {
  339. val = load[j];
  340. val |= load[j + 1] << 8;
  341. val |= load[j + 2] << 16;
  342. val |= load[j + 3] << 24;
  343. writel(val, cci->base + CCI_I2C_Mm_Qn_LOAD_DATA(master, queue));
  344. }
  345. val = CCI_I2C_REPORT | CCI_I2C_REPORT_IRQ_EN;
  346. writel(val, cci->base + CCI_I2C_Mm_Qn_LOAD_DATA(master, queue));
  347. return cci_run_queue(cci, master, queue);
  348. }
  349. static int cci_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
  350. {
  351. struct cci_master *cci_master = i2c_get_adapdata(adap);
  352. struct cci *cci = cci_master->cci;
  353. int i, ret;
  354. ret = pm_runtime_get_sync(cci->dev);
  355. if (ret < 0)
  356. goto err;
  357. for (i = 0; i < num; i++) {
  358. if (msgs[i].flags & I2C_M_RD)
  359. ret = cci_i2c_read(cci, cci_master->master,
  360. msgs[i].addr, msgs[i].buf,
  361. msgs[i].len);
  362. else
  363. ret = cci_i2c_write(cci, cci_master->master,
  364. msgs[i].addr, msgs[i].buf,
  365. msgs[i].len);
  366. if (ret < 0)
  367. break;
  368. }
  369. if (!ret)
  370. ret = num;
  371. err:
  372. pm_runtime_mark_last_busy(cci->dev);
  373. pm_runtime_put_autosuspend(cci->dev);
  374. return ret;
  375. }
  376. static u32 cci_func(struct i2c_adapter *adap)
  377. {
  378. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  379. }
  380. static const struct i2c_algorithm cci_algo = {
  381. .master_xfer = cci_xfer,
  382. .functionality = cci_func,
  383. };
  384. static int cci_enable_clocks(struct cci *cci)
  385. {
  386. return clk_bulk_prepare_enable(cci->nclocks, cci->clocks);
  387. }
  388. static void cci_disable_clocks(struct cci *cci)
  389. {
  390. clk_bulk_disable_unprepare(cci->nclocks, cci->clocks);
  391. }
  392. static int __maybe_unused cci_suspend_runtime(struct device *dev)
  393. {
  394. struct cci *cci = dev_get_drvdata(dev);
  395. cci_disable_clocks(cci);
  396. return 0;
  397. }
  398. static int __maybe_unused cci_resume_runtime(struct device *dev)
  399. {
  400. struct cci *cci = dev_get_drvdata(dev);
  401. int ret;
  402. ret = cci_enable_clocks(cci);
  403. if (ret)
  404. return ret;
  405. cci_init(cci);
  406. return 0;
  407. }
  408. static int __maybe_unused cci_suspend(struct device *dev)
  409. {
  410. if (!pm_runtime_suspended(dev))
  411. return cci_suspend_runtime(dev);
  412. return 0;
  413. }
  414. static int __maybe_unused cci_resume(struct device *dev)
  415. {
  416. cci_resume_runtime(dev);
  417. pm_runtime_mark_last_busy(dev);
  418. pm_request_autosuspend(dev);
  419. return 0;
  420. }
  421. static const struct dev_pm_ops qcom_cci_pm = {
  422. SET_SYSTEM_SLEEP_PM_OPS(cci_suspend, cci_resume)
  423. SET_RUNTIME_PM_OPS(cci_suspend_runtime, cci_resume_runtime, NULL)
  424. };
  425. static int cci_probe(struct platform_device *pdev)
  426. {
  427. struct device *dev = &pdev->dev;
  428. unsigned long cci_clk_rate = 0;
  429. struct device_node *child;
  430. struct resource *r;
  431. struct cci *cci;
  432. int ret, i;
  433. u32 val;
  434. cci = devm_kzalloc(dev, sizeof(*cci), GFP_KERNEL);
  435. if (!cci)
  436. return -ENOMEM;
  437. cci->dev = dev;
  438. platform_set_drvdata(pdev, cci);
  439. cci->data = device_get_match_data(dev);
  440. if (!cci->data)
  441. return -ENOENT;
  442. for_each_available_child_of_node(dev->of_node, child) {
  443. struct cci_master *master;
  444. u32 idx;
  445. ret = of_property_read_u32(child, "reg", &idx);
  446. if (ret) {
  447. dev_err(dev, "%pOF invalid 'reg' property", child);
  448. continue;
  449. }
  450. if (idx >= cci->data->num_masters) {
  451. dev_err(dev, "%pOF invalid 'reg' value: %u (max is %u)",
  452. child, idx, cci->data->num_masters - 1);
  453. continue;
  454. }
  455. master = &cci->master[idx];
  456. master->adap.quirks = &cci->data->quirks;
  457. master->adap.algo = &cci_algo;
  458. master->adap.dev.parent = dev;
  459. master->adap.dev.of_node = of_node_get(child);
  460. master->master = idx;
  461. master->cci = cci;
  462. i2c_set_adapdata(&master->adap, master);
  463. snprintf(master->adap.name, sizeof(master->adap.name), "Qualcomm-CCI");
  464. master->mode = I2C_MODE_STANDARD;
  465. ret = of_property_read_u32(child, "clock-frequency", &val);
  466. if (!ret) {
  467. if (val == I2C_MAX_FAST_MODE_FREQ)
  468. master->mode = I2C_MODE_FAST;
  469. else if (val == I2C_MAX_FAST_MODE_PLUS_FREQ)
  470. master->mode = I2C_MODE_FAST_PLUS;
  471. }
  472. init_completion(&master->irq_complete);
  473. }
  474. /* Memory */
  475. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  476. cci->base = devm_ioremap_resource(dev, r);
  477. if (IS_ERR(cci->base))
  478. return PTR_ERR(cci->base);
  479. /* Clocks */
  480. ret = devm_clk_bulk_get_all(dev, &cci->clocks);
  481. if (ret < 1) {
  482. dev_err(dev, "failed to get clocks %d\n", ret);
  483. return ret;
  484. }
  485. cci->nclocks = ret;
  486. /* Retrieve CCI clock rate */
  487. for (i = 0; i < cci->nclocks; i++) {
  488. if (!strcmp(cci->clocks[i].id, "cci")) {
  489. cci_clk_rate = clk_get_rate(cci->clocks[i].clk);
  490. break;
  491. }
  492. }
  493. if (cci_clk_rate != cci->data->cci_clk_rate) {
  494. /* cci clock set by the bootloader or via assigned clock rate
  495. * in DT.
  496. */
  497. dev_warn(dev, "Found %lu cci clk rate while %lu was expected\n",
  498. cci_clk_rate, cci->data->cci_clk_rate);
  499. }
  500. ret = cci_enable_clocks(cci);
  501. if (ret < 0)
  502. return ret;
  503. /* Interrupt */
  504. ret = platform_get_irq(pdev, 0);
  505. if (ret < 0)
  506. goto disable_clocks;
  507. cci->irq = ret;
  508. ret = devm_request_irq(dev, cci->irq, cci_isr, 0, dev_name(dev), cci);
  509. if (ret < 0) {
  510. dev_err(dev, "request_irq failed, ret: %d\n", ret);
  511. goto disable_clocks;
  512. }
  513. val = readl(cci->base + CCI_HW_VERSION);
  514. dev_dbg(dev, "CCI HW version = 0x%08x", val);
  515. ret = cci_reset(cci);
  516. if (ret < 0)
  517. goto error;
  518. ret = cci_init(cci);
  519. if (ret < 0)
  520. goto error;
  521. pm_runtime_set_autosuspend_delay(dev, MSEC_PER_SEC);
  522. pm_runtime_use_autosuspend(dev);
  523. pm_runtime_set_active(dev);
  524. pm_runtime_enable(dev);
  525. for (i = 0; i < cci->data->num_masters; i++) {
  526. if (!cci->master[i].cci)
  527. continue;
  528. ret = i2c_add_adapter(&cci->master[i].adap);
  529. if (ret < 0) {
  530. of_node_put(cci->master[i].adap.dev.of_node);
  531. goto error_i2c;
  532. }
  533. }
  534. return 0;
  535. error_i2c:
  536. pm_runtime_disable(dev);
  537. pm_runtime_dont_use_autosuspend(dev);
  538. for (--i ; i >= 0; i--) {
  539. if (cci->master[i].cci) {
  540. i2c_del_adapter(&cci->master[i].adap);
  541. of_node_put(cci->master[i].adap.dev.of_node);
  542. }
  543. }
  544. error:
  545. disable_irq(cci->irq);
  546. disable_clocks:
  547. cci_disable_clocks(cci);
  548. return ret;
  549. }
  550. static int cci_remove(struct platform_device *pdev)
  551. {
  552. struct cci *cci = platform_get_drvdata(pdev);
  553. int i;
  554. for (i = 0; i < cci->data->num_masters; i++) {
  555. if (cci->master[i].cci) {
  556. i2c_del_adapter(&cci->master[i].adap);
  557. of_node_put(cci->master[i].adap.dev.of_node);
  558. }
  559. cci_halt(cci, i);
  560. }
  561. disable_irq(cci->irq);
  562. pm_runtime_disable(&pdev->dev);
  563. pm_runtime_set_suspended(&pdev->dev);
  564. return 0;
  565. }
  566. static const struct cci_data cci_v1_data = {
  567. .num_masters = 1,
  568. .queue_size = { 64, 16 },
  569. .quirks = {
  570. .max_write_len = 10,
  571. .max_read_len = 12,
  572. },
  573. .cci_clk_rate = 19200000,
  574. .params[I2C_MODE_STANDARD] = {
  575. .thigh = 78,
  576. .tlow = 114,
  577. .tsu_sto = 28,
  578. .tsu_sta = 28,
  579. .thd_dat = 10,
  580. .thd_sta = 77,
  581. .tbuf = 118,
  582. .scl_stretch_en = 0,
  583. .trdhld = 6,
  584. .tsp = 1
  585. },
  586. .params[I2C_MODE_FAST] = {
  587. .thigh = 20,
  588. .tlow = 28,
  589. .tsu_sto = 21,
  590. .tsu_sta = 21,
  591. .thd_dat = 13,
  592. .thd_sta = 18,
  593. .tbuf = 32,
  594. .scl_stretch_en = 0,
  595. .trdhld = 6,
  596. .tsp = 3
  597. },
  598. };
  599. static const struct cci_data cci_v1_5_data = {
  600. .num_masters = 2,
  601. .queue_size = { 64, 16 },
  602. .quirks = {
  603. .max_write_len = 10,
  604. .max_read_len = 12,
  605. },
  606. .cci_clk_rate = 19200000,
  607. .params[I2C_MODE_STANDARD] = {
  608. .thigh = 78,
  609. .tlow = 114,
  610. .tsu_sto = 28,
  611. .tsu_sta = 28,
  612. .thd_dat = 10,
  613. .thd_sta = 77,
  614. .tbuf = 118,
  615. .scl_stretch_en = 0,
  616. .trdhld = 6,
  617. .tsp = 1
  618. },
  619. .params[I2C_MODE_FAST] = {
  620. .thigh = 20,
  621. .tlow = 28,
  622. .tsu_sto = 21,
  623. .tsu_sta = 21,
  624. .thd_dat = 13,
  625. .thd_sta = 18,
  626. .tbuf = 32,
  627. .scl_stretch_en = 0,
  628. .trdhld = 6,
  629. .tsp = 3
  630. },
  631. };
  632. static const struct cci_data cci_v2_data = {
  633. .num_masters = 2,
  634. .queue_size = { 64, 16 },
  635. .quirks = {
  636. .max_write_len = 11,
  637. .max_read_len = 12,
  638. },
  639. .cci_clk_rate = 37500000,
  640. .params[I2C_MODE_STANDARD] = {
  641. .thigh = 201,
  642. .tlow = 174,
  643. .tsu_sto = 204,
  644. .tsu_sta = 231,
  645. .thd_dat = 22,
  646. .thd_sta = 162,
  647. .tbuf = 227,
  648. .scl_stretch_en = 0,
  649. .trdhld = 6,
  650. .tsp = 3
  651. },
  652. .params[I2C_MODE_FAST] = {
  653. .thigh = 38,
  654. .tlow = 56,
  655. .tsu_sto = 40,
  656. .tsu_sta = 40,
  657. .thd_dat = 22,
  658. .thd_sta = 35,
  659. .tbuf = 62,
  660. .scl_stretch_en = 0,
  661. .trdhld = 6,
  662. .tsp = 3
  663. },
  664. .params[I2C_MODE_FAST_PLUS] = {
  665. .thigh = 16,
  666. .tlow = 22,
  667. .tsu_sto = 17,
  668. .tsu_sta = 18,
  669. .thd_dat = 16,
  670. .thd_sta = 15,
  671. .tbuf = 24,
  672. .scl_stretch_en = 0,
  673. .trdhld = 3,
  674. .tsp = 3
  675. },
  676. };
  677. static const struct of_device_id cci_dt_match[] = {
  678. { .compatible = "qcom,msm8226-cci", .data = &cci_v1_data},
  679. { .compatible = "qcom,msm8916-cci", .data = &cci_v1_data},
  680. { .compatible = "qcom,msm8974-cci", .data = &cci_v1_5_data},
  681. { .compatible = "qcom,msm8996-cci", .data = &cci_v2_data},
  682. { .compatible = "qcom,sdm845-cci", .data = &cci_v2_data},
  683. { .compatible = "qcom,sm8250-cci", .data = &cci_v2_data},
  684. { .compatible = "qcom,sm8450-cci", .data = &cci_v2_data},
  685. {}
  686. };
  687. MODULE_DEVICE_TABLE(of, cci_dt_match);
  688. static struct platform_driver qcom_cci_driver = {
  689. .probe = cci_probe,
  690. .remove = cci_remove,
  691. .driver = {
  692. .name = "i2c-qcom-cci",
  693. .of_match_table = cci_dt_match,
  694. .pm = &qcom_cci_pm,
  695. },
  696. };
  697. module_platform_driver(qcom_cci_driver);
  698. MODULE_DESCRIPTION("Qualcomm Camera Control Interface driver");
  699. MODULE_AUTHOR("Todor Tomov <[email protected]>");
  700. MODULE_AUTHOR("Loic Poulain <[email protected]>");
  701. MODULE_LICENSE("GPL v2");