804-i2c-support-layerscape.patch 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. From 3f7d59061c38287bdc2fec2e94b4df9e6e62dbc6 Mon Sep 17 00:00:00 2001
  2. From: Biwen Li <[email protected]>
  3. Date: Wed, 17 Apr 2019 18:58:39 +0800
  4. Subject: [PATCH] i2c: support layerscape
  5. This is an integrated patch of i2c for layerscape
  6. Signed-off-by: Biwen Li <[email protected]>
  7. Signed-off-by: Laurentiu Tudor <[email protected]>
  8. Signed-off-by: Oleksij Rempel <[email protected]>
  9. Signed-off-by: Wolfram Sang <[email protected]>
  10. Signed-off-by: Zhang Ying-22455 <[email protected]>
  11. ---
  12. drivers/i2c/busses/i2c-imx.c | 245 +++++++++++++++++++++++++---
  13. drivers/i2c/muxes/i2c-mux-pca954x.c | 44 ++++-
  14. 2 files changed, 268 insertions(+), 21 deletions(-)
  15. --- a/drivers/i2c/busses/i2c-imx.c
  16. +++ b/drivers/i2c/busses/i2c-imx.c
  17. @@ -53,6 +53,11 @@
  18. #include <linux/pm_runtime.h>
  19. #include <linux/sched.h>
  20. #include <linux/slab.h>
  21. +#include <linux/gpio.h>
  22. +#include <linux/of_address.h>
  23. +#include <linux/of.h>
  24. +#include <linux/of_device.h>
  25. +#include <linux/libata.h>
  26. /* This will be the driver name the kernel reports */
  27. #define DRIVER_NAME "imx-i2c"
  28. @@ -117,6 +122,54 @@
  29. #define I2C_PM_TIMEOUT 10 /* ms */
  30. +enum pinmux_endian_type {
  31. + BIG_ENDIAN,
  32. + LITTLE_ENDIAN,
  33. +};
  34. +
  35. +struct pinmux_cfg {
  36. + enum pinmux_endian_type endian; /* endian of RCWPMUXCR0 */
  37. + u32 pmuxcr_offset;
  38. + u32 pmuxcr_set_bit; /* pin mux of RCWPMUXCR0 */
  39. +};
  40. +
  41. +static struct pinmux_cfg ls1012a_pinmux_cfg = {
  42. + .endian = BIG_ENDIAN,
  43. + .pmuxcr_offset = 0x430,
  44. + .pmuxcr_set_bit = 0x10,
  45. +};
  46. +
  47. +static struct pinmux_cfg ls1043a_pinmux_cfg = {
  48. + .endian = BIG_ENDIAN,
  49. + .pmuxcr_offset = 0x40C,
  50. + .pmuxcr_set_bit = 0x10,
  51. +};
  52. +
  53. +static struct pinmux_cfg ls1046a_pinmux_cfg = {
  54. + .endian = BIG_ENDIAN,
  55. + .pmuxcr_offset = 0x40C,
  56. + .pmuxcr_set_bit = 0x80000000,
  57. +};
  58. +
  59. +static const struct of_device_id pinmux_of_match[] = {
  60. + { .compatible = "fsl,ls1012a-vf610-i2c", .data = &ls1012a_pinmux_cfg},
  61. + { .compatible = "fsl,ls1043a-vf610-i2c", .data = &ls1043a_pinmux_cfg},
  62. + { .compatible = "fsl,ls1046a-vf610-i2c", .data = &ls1046a_pinmux_cfg},
  63. + {},
  64. +};
  65. +MODULE_DEVICE_TABLE(of, pinmux_of_match);
  66. +
  67. +/* The SCFG, Supplemental Configuration Unit, provides SoC specific
  68. + * configuration and status registers for the device. There is a
  69. + * SDHC IO VSEL control register on SCFG for some platforms. It's
  70. + * used to support SDHC IO voltage switching.
  71. + */
  72. +static const struct of_device_id scfg_device_ids[] = {
  73. + { .compatible = "fsl,ls1012a-scfg", },
  74. + { .compatible = "fsl,ls1043a-scfg", },
  75. + { .compatible = "fsl,ls1046a-scfg", },
  76. + {}
  77. +};
  78. /*
  79. * sorted list of clock divider, register value pairs
  80. * taken from table 26-5, p.26-9, Freescale i.MX
  81. @@ -210,6 +263,12 @@ struct imx_i2c_struct {
  82. struct pinctrl_state *pinctrl_pins_gpio;
  83. struct imx_i2c_dma *dma;
  84. + int layerscape_bus_recover;
  85. + int gpio;
  86. + int need_set_pmuxcr;
  87. + int pmuxcr_set;
  88. + int pmuxcr_endian;
  89. + void __iomem *pmuxcr_addr;
  90. };
  91. static const struct imx_i2c_hwdata imx1_i2c_hwdata = {
  92. @@ -281,8 +340,8 @@ static inline unsigned char imx_i2c_read
  93. }
  94. /* Functions for DMA support */
  95. -static void i2c_imx_dma_request(struct imx_i2c_struct *i2c_imx,
  96. - dma_addr_t phy_addr)
  97. +static int i2c_imx_dma_request(struct imx_i2c_struct *i2c_imx,
  98. + dma_addr_t phy_addr)
  99. {
  100. struct imx_i2c_dma *dma;
  101. struct dma_slave_config dma_sconfig;
  102. @@ -291,11 +350,13 @@ static void i2c_imx_dma_request(struct i
  103. dma = devm_kzalloc(dev, sizeof(*dma), GFP_KERNEL);
  104. if (!dma)
  105. - return;
  106. + return -ENOMEM;
  107. - dma->chan_tx = dma_request_slave_channel(dev, "tx");
  108. - if (!dma->chan_tx) {
  109. - dev_dbg(dev, "can't request DMA tx channel\n");
  110. + dma->chan_tx = dma_request_chan(dev, "tx");
  111. + if (IS_ERR(dma->chan_tx)) {
  112. + ret = PTR_ERR(dma->chan_tx);
  113. + if (ret != -ENODEV && ret != -EPROBE_DEFER)
  114. + dev_err(dev, "can't request DMA tx channel (%d)\n", ret);
  115. goto fail_al;
  116. }
  117. @@ -306,13 +367,15 @@ static void i2c_imx_dma_request(struct i
  118. dma_sconfig.direction = DMA_MEM_TO_DEV;
  119. ret = dmaengine_slave_config(dma->chan_tx, &dma_sconfig);
  120. if (ret < 0) {
  121. - dev_dbg(dev, "can't configure tx channel\n");
  122. + dev_err(dev, "can't configure tx channel (%d)\n", ret);
  123. goto fail_tx;
  124. }
  125. - dma->chan_rx = dma_request_slave_channel(dev, "rx");
  126. - if (!dma->chan_rx) {
  127. - dev_dbg(dev, "can't request DMA rx channel\n");
  128. + dma->chan_rx = dma_request_chan(dev, "rx");
  129. + if (IS_ERR(dma->chan_rx)) {
  130. + ret = PTR_ERR(dma->chan_rx);
  131. + if (ret != -ENODEV && ret != -EPROBE_DEFER)
  132. + dev_err(dev, "can't request DMA rx channel (%d)\n", ret);
  133. goto fail_tx;
  134. }
  135. @@ -323,7 +386,7 @@ static void i2c_imx_dma_request(struct i
  136. dma_sconfig.direction = DMA_DEV_TO_MEM;
  137. ret = dmaengine_slave_config(dma->chan_rx, &dma_sconfig);
  138. if (ret < 0) {
  139. - dev_dbg(dev, "can't configure rx channel\n");
  140. + dev_err(dev, "can't configure rx channel (%d)\n", ret);
  141. goto fail_rx;
  142. }
  143. @@ -332,7 +395,7 @@ static void i2c_imx_dma_request(struct i
  144. dev_info(dev, "using %s (tx) and %s (rx) for DMA transfers\n",
  145. dma_chan_name(dma->chan_tx), dma_chan_name(dma->chan_rx));
  146. - return;
  147. + return 0;
  148. fail_rx:
  149. dma_release_channel(dma->chan_rx);
  150. @@ -340,7 +403,8 @@ fail_tx:
  151. dma_release_channel(dma->chan_tx);
  152. fail_al:
  153. devm_kfree(dev, dma);
  154. - dev_info(dev, "can't use DMA, using PIO instead.\n");
  155. + /* return successfully if there is no dma support */
  156. + return ret == -ENODEV ? 0 : ret;
  157. }
  158. static void i2c_imx_dma_callback(void *arg)
  159. @@ -878,6 +942,78 @@ static int i2c_imx_read(struct imx_i2c_s
  160. return 0;
  161. }
  162. +/*
  163. + * Based on the I2C specification, if the data line (SDA) is
  164. + * stuck low, the master should send nine * clock pulses.
  165. + * The I2C slave device that held the bus low should release it
  166. + * sometime within * those nine clocks. Due to this erratum,
  167. + * the I2C controller cannot generate nine clock pulses.
  168. + */
  169. +static int i2c_imx_recovery_for_layerscape(struct imx_i2c_struct *i2c_imx)
  170. +{
  171. + u32 pmuxcr = 0;
  172. + int ret;
  173. + unsigned int i, temp;
  174. +
  175. + /* configure IICx_SCL/GPIO pin as a GPIO */
  176. + if (i2c_imx->need_set_pmuxcr == 1) {
  177. + pmuxcr = ioread32be(i2c_imx->pmuxcr_addr);
  178. + if (i2c_imx->pmuxcr_endian == BIG_ENDIAN)
  179. + iowrite32be(i2c_imx->pmuxcr_set|pmuxcr,
  180. + i2c_imx->pmuxcr_addr);
  181. + else
  182. + iowrite32(i2c_imx->pmuxcr_set|pmuxcr,
  183. + i2c_imx->pmuxcr_addr);
  184. + }
  185. +
  186. + ret = gpio_request(i2c_imx->gpio, i2c_imx->adapter.name);
  187. + if (ret) {
  188. + dev_err(&i2c_imx->adapter.dev,
  189. + "can't get gpio: %d\n", ret);
  190. + return ret;
  191. + }
  192. +
  193. + /* Configure GPIO pin as an output and open drain. */
  194. + gpio_direction_output(i2c_imx->gpio, 1);
  195. + udelay(10);
  196. +
  197. + /* Write data to generate 9 pulses */
  198. + for (i = 0; i < 9; i++) {
  199. + gpio_set_value(i2c_imx->gpio, 1);
  200. + udelay(10);
  201. + gpio_set_value(i2c_imx->gpio, 0);
  202. + udelay(10);
  203. + }
  204. + /* ensure that the last level sent is always high */
  205. + gpio_set_value(i2c_imx->gpio, 1);
  206. +
  207. + /*
  208. + * Set I2Cx_IBCR = 0h00 to generate a STOP and then
  209. + * set I2Cx_IBCR = 0h80 to reset
  210. + */
  211. + temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
  212. + temp &= ~(I2CR_MSTA | I2CR_MTX);
  213. + imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
  214. +
  215. + /* Restore the saved value of the register SCFG_RCWPMUXCR0 */
  216. + if (i2c_imx->need_set_pmuxcr == 1) {
  217. + if (i2c_imx->pmuxcr_endian == BIG_ENDIAN)
  218. + iowrite32be(pmuxcr, i2c_imx->pmuxcr_addr);
  219. + else
  220. + iowrite32(pmuxcr, i2c_imx->pmuxcr_addr);
  221. + }
  222. + /*
  223. + * Set I2C_IBSR[IBAL] to clear the IBAL bit if-
  224. + * I2C_IBSR[IBAL] = 1
  225. + */
  226. + temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2SR);
  227. + if (temp & I2SR_IAL) {
  228. + temp &= ~I2SR_IAL;
  229. + imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2SR);
  230. + }
  231. + return 0;
  232. +}
  233. +
  234. static int i2c_imx_xfer(struct i2c_adapter *adapter,
  235. struct i2c_msg *msgs, int num)
  236. {
  237. @@ -888,6 +1024,19 @@ static int i2c_imx_xfer(struct i2c_adapt
  238. dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__);
  239. + /*
  240. + * workround for ERR010027: ensure that the I2C BUS is idle
  241. + * before switching to master mode and attempting a Start cycle
  242. + */
  243. + result = i2c_imx_bus_busy(i2c_imx, 0);
  244. + if (result) {
  245. + /* timeout */
  246. + if ((result == -ETIMEDOUT) && (i2c_imx->layerscape_bus_recover == 1))
  247. + i2c_imx_recovery_for_layerscape(i2c_imx);
  248. + else
  249. + goto out;
  250. + }
  251. +
  252. result = pm_runtime_get_sync(i2c_imx->adapter.dev.parent);
  253. if (result < 0)
  254. goto out;
  255. @@ -1030,6 +1179,50 @@ static int i2c_imx_init_recovery_info(st
  256. return 0;
  257. }
  258. +/*
  259. + * switch SCL and SDA to their GPIO function and do some bitbanging
  260. + * for bus recovery.
  261. + * There are platforms such as Layerscape that don't support pinctrl, so add
  262. + * workaround for layerscape, it has no effect for other platforms.
  263. + */
  264. +static int i2c_imx_init_recovery_for_layerscape(
  265. + struct imx_i2c_struct *i2c_imx,
  266. + struct platform_device *pdev)
  267. +{
  268. + const struct of_device_id *of_id;
  269. + struct device_node *np = pdev->dev.of_node;
  270. + struct pinmux_cfg *pinmux_cfg;
  271. + struct device_node *scfg_node;
  272. + void __iomem *scfg_base = NULL;
  273. +
  274. + i2c_imx->gpio = of_get_named_gpio(np, "fsl-scl-gpio", 0);
  275. + if (!gpio_is_valid(i2c_imx->gpio)) {
  276. + dev_info(&pdev->dev, "fsl-scl-gpio not found\n");
  277. + return 0;
  278. + }
  279. + pinmux_cfg = devm_kzalloc(&pdev->dev, sizeof(*pinmux_cfg), GFP_KERNEL);
  280. + if (!pinmux_cfg)
  281. + return -ENOMEM;
  282. +
  283. + i2c_imx->need_set_pmuxcr = 0;
  284. + of_id = of_match_node(pinmux_of_match, np);
  285. + if (of_id) {
  286. + pinmux_cfg = (struct pinmux_cfg *)of_id->data;
  287. + i2c_imx->pmuxcr_endian = pinmux_cfg->endian;
  288. + i2c_imx->pmuxcr_set = pinmux_cfg->pmuxcr_set_bit;
  289. + scfg_node = of_find_matching_node(NULL, scfg_device_ids);
  290. + if (scfg_node) {
  291. + scfg_base = of_iomap(scfg_node, 0);
  292. + if (scfg_base) {
  293. + i2c_imx->pmuxcr_addr = scfg_base + pinmux_cfg->pmuxcr_offset;
  294. + i2c_imx->need_set_pmuxcr = 1;
  295. + }
  296. + }
  297. + }
  298. + i2c_imx->layerscape_bus_recover = 1;
  299. + return 0;
  300. +}
  301. +
  302. static u32 i2c_imx_func(struct i2c_adapter *adapter)
  303. {
  304. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL
  305. @@ -1085,6 +1278,11 @@ static int i2c_imx_probe(struct platform
  306. i2c_imx->adapter.dev.of_node = pdev->dev.of_node;
  307. i2c_imx->base = base;
  308. + /* Init optional bus recovery for layerscape */
  309. + ret = i2c_imx_init_recovery_for_layerscape(i2c_imx, pdev);
  310. + if (ret)
  311. + return ret;
  312. +
  313. /* Get I2C clock */
  314. i2c_imx->clk = devm_clk_get(&pdev->dev, NULL);
  315. if (IS_ERR(i2c_imx->clk)) {
  316. @@ -1103,7 +1301,8 @@ static int i2c_imx_probe(struct platform
  317. pdev->name, i2c_imx);
  318. if (ret) {
  319. dev_err(&pdev->dev, "can't claim irq %d\n", irq);
  320. - goto clk_disable;
  321. + clk_disable_unprepare(i2c_imx->clk);
  322. + return ret;
  323. }
  324. /* Init queue */
  325. @@ -1150,25 +1349,31 @@ static int i2c_imx_probe(struct platform
  326. pm_runtime_mark_last_busy(&pdev->dev);
  327. pm_runtime_put_autosuspend(&pdev->dev);
  328. + /* Init DMA config if supported */
  329. + ret = i2c_imx_dma_request(i2c_imx, phy_addr);
  330. + if (ret) {
  331. + if (ret != -EPROBE_DEFER)
  332. + dev_info(&pdev->dev, "can't use DMA, using PIO instead.\n");
  333. + else
  334. + goto del_adapter;
  335. + }
  336. +
  337. dev_dbg(&i2c_imx->adapter.dev, "claimed irq %d\n", irq);
  338. dev_dbg(&i2c_imx->adapter.dev, "device resources: %pR\n", res);
  339. dev_dbg(&i2c_imx->adapter.dev, "adapter name: \"%s\"\n",
  340. i2c_imx->adapter.name);
  341. - dev_info(&i2c_imx->adapter.dev, "IMX I2C adapter registered\n");
  342. -
  343. - /* Init DMA config if supported */
  344. - i2c_imx_dma_request(i2c_imx, phy_addr);
  345. + dev_info(&i2c_imx->adapter.dev, "IMX I2C adapter registered\n");
  346. return 0; /* Return OK */
  347. +del_adapter:
  348. + i2c_del_adapter(&i2c_imx->adapter);
  349. rpm_disable:
  350. pm_runtime_put_noidle(&pdev->dev);
  351. pm_runtime_disable(&pdev->dev);
  352. pm_runtime_set_suspended(&pdev->dev);
  353. pm_runtime_dont_use_autosuspend(&pdev->dev);
  354. -clk_disable:
  355. - clk_disable_unprepare(i2c_imx->clk);
  356. return ret;
  357. }
  358. --- a/drivers/i2c/muxes/i2c-mux-pca954x.c
  359. +++ b/drivers/i2c/muxes/i2c-mux-pca954x.c
  360. @@ -85,6 +85,7 @@ struct pca954x {
  361. struct irq_domain *irq;
  362. unsigned int irq_mask;
  363. raw_spinlock_t lock;
  364. + u8 disable_mux; /* do not disable mux if val not 0 */
  365. };
  366. /* Provide specs for the PCA954x types we know about */
  367. @@ -221,6 +222,13 @@ static int pca954x_deselect_mux(struct i
  368. if (!(data->deselect & (1 << chan)))
  369. return 0;
  370. +#ifdef CONFIG_ARCH_LAYERSCAPE
  371. + if (data->disable_mux != 0)
  372. + data->last_chan = data->chip->nchans;
  373. + else
  374. + data->last_chan = 0;
  375. + return pca954x_reg_write(muxc->parent, client, data->disable_mux);
  376. +#endif
  377. /* Deselect active channel */
  378. data->last_chan = 0;
  379. return pca954x_reg_write(muxc->parent, client, data->last_chan);
  380. @@ -361,6 +369,28 @@ static int pca954x_probe(struct i2c_clie
  381. return -ENOMEM;
  382. data = i2c_mux_priv(muxc);
  383. +#ifdef CONFIG_ARCH_LAYERSCAPE
  384. + /* The point here is that you must not disable a mux if there
  385. + * are no pullups on the input or you mess up the I2C. This
  386. + * needs to be put into the DTS really as the kernel cannot
  387. + * know this otherwise.
  388. + */
  389. + match = of_match_device(of_match_ptr(pca954x_of_match), &client->dev);
  390. + if (match)
  391. + data->chip = of_device_get_match_data(&client->dev);
  392. + else
  393. + data->chip = &chips[id->driver_data];
  394. +
  395. + data->disable_mux = of_node &&
  396. + of_property_read_bool(of_node, "i2c-mux-never-disable") &&
  397. + data->chip->muxtype == pca954x_ismux ?
  398. + data->chip->enable : 0;
  399. + /* force the first selection */
  400. + if (data->disable_mux != 0)
  401. + data->last_chan = data->chip->nchans;
  402. + else
  403. + data->last_chan = 0;
  404. +#endif
  405. i2c_set_clientdata(client, muxc);
  406. data->client = client;
  407. @@ -373,18 +403,23 @@ static int pca954x_probe(struct i2c_clie
  408. * that the mux is in fact present. This also
  409. * initializes the mux to disconnected state.
  410. */
  411. +#ifdef CONFIG_ARCH_LAYERSCAPE
  412. + if (i2c_smbus_write_byte(client, data->disable_mux) < 0) {
  413. +#else
  414. if (i2c_smbus_write_byte(client, 0) < 0) {
  415. +#endif
  416. dev_warn(&client->dev, "probe failed\n");
  417. return -ENODEV;
  418. }
  419. +#ifndef CONFIG_ARCH_LAYERSCAPE
  420. match = of_match_device(of_match_ptr(pca954x_of_match), &client->dev);
  421. if (match)
  422. data->chip = of_device_get_match_data(&client->dev);
  423. else
  424. data->chip = &chips[id->driver_data];
  425. -
  426. data->last_chan = 0; /* force the first selection */
  427. +#endif
  428. idle_disconnect_dt = of_node &&
  429. of_property_read_bool(of_node, "i2c-mux-idle-disconnect");
  430. @@ -454,6 +489,13 @@ static int pca954x_resume(struct device
  431. struct i2c_mux_core *muxc = i2c_get_clientdata(client);
  432. struct pca954x *data = i2c_mux_priv(muxc);
  433. +#ifdef CONFIG_ARCH_LAYERSCAPE
  434. + if (data->disable_mux != 0)
  435. + data->last_chan = data->chip->nchans;
  436. + else
  437. + data->last_chan = 0;
  438. + return i2c_smbus_write_byte(client, data->disable_mux);
  439. +#endif
  440. data->last_chan = 0;
  441. return i2c_smbus_write_byte(client, 0);
  442. }