809-i2c-support-layerscape.patch 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. From 659aa30c59fb188b533a7edcb9bd38ac007a2739 Mon Sep 17 00:00:00 2001
  2. From: Yangbo Lu <[email protected]>
  3. Date: Wed, 17 Jan 2018 15:35:11 +0800
  4. Subject: [PATCH 21/30] i2c: support layerscape
  5. This is an integrated patch for layerscape i2c support.
  6. Signed-off-by: Zhang Ying-22455 <[email protected]>
  7. Signed-off-by: Priyanka Jain <[email protected]>
  8. Signed-off-by: Yangbo Lu <[email protected]>
  9. ---
  10. drivers/i2c/busses/i2c-imx.c | 195 +++++++++++++++++++++++++++++++++++-
  11. drivers/i2c/muxes/i2c-mux-pca954x.c | 43 ++++++++
  12. 2 files changed, 237 insertions(+), 1 deletion(-)
  13. --- a/drivers/i2c/busses/i2c-imx.c
  14. +++ b/drivers/i2c/busses/i2c-imx.c
  15. @@ -53,6 +53,11 @@
  16. #include <linux/pm_runtime.h>
  17. #include <linux/sched.h>
  18. #include <linux/slab.h>
  19. +#include <linux/gpio.h>
  20. +#include <linux/of_address.h>
  21. +#include <linux/of.h>
  22. +#include <linux/of_device.h>
  23. +#include <linux/libata.h>
  24. /* This will be the driver name the kernel reports */
  25. #define DRIVER_NAME "imx-i2c"
  26. @@ -117,6 +122,54 @@
  27. #define I2C_PM_TIMEOUT 10 /* ms */
  28. +enum pinmux_endian_type {
  29. + BIG_ENDIAN,
  30. + LITTLE_ENDIAN,
  31. +};
  32. +
  33. +struct pinmux_cfg {
  34. + enum pinmux_endian_type endian; /* endian of RCWPMUXCR0 */
  35. + u32 pmuxcr_offset;
  36. + u32 pmuxcr_set_bit; /* pin mux of RCWPMUXCR0 */
  37. +};
  38. +
  39. +static struct pinmux_cfg ls1012a_pinmux_cfg = {
  40. + .endian = BIG_ENDIAN,
  41. + .pmuxcr_offset = 0x430,
  42. + .pmuxcr_set_bit = 0x10,
  43. +};
  44. +
  45. +static struct pinmux_cfg ls1043a_pinmux_cfg = {
  46. + .endian = BIG_ENDIAN,
  47. + .pmuxcr_offset = 0x40C,
  48. + .pmuxcr_set_bit = 0x10,
  49. +};
  50. +
  51. +static struct pinmux_cfg ls1046a_pinmux_cfg = {
  52. + .endian = BIG_ENDIAN,
  53. + .pmuxcr_offset = 0x40C,
  54. + .pmuxcr_set_bit = 0x80000000,
  55. +};
  56. +
  57. +static const struct of_device_id pinmux_of_match[] = {
  58. + { .compatible = "fsl,ls1012a-vf610-i2c", .data = &ls1012a_pinmux_cfg},
  59. + { .compatible = "fsl,ls1043a-vf610-i2c", .data = &ls1043a_pinmux_cfg},
  60. + { .compatible = "fsl,ls1046a-vf610-i2c", .data = &ls1046a_pinmux_cfg},
  61. + {},
  62. +};
  63. +MODULE_DEVICE_TABLE(of, pinmux_of_match);
  64. +
  65. +/* The SCFG, Supplemental Configuration Unit, provides SoC specific
  66. + * configuration and status registers for the device. There is a
  67. + * SDHC IO VSEL control register on SCFG for some platforms. It's
  68. + * used to support SDHC IO voltage switching.
  69. + */
  70. +static const struct of_device_id scfg_device_ids[] = {
  71. + { .compatible = "fsl,ls1012a-scfg", },
  72. + { .compatible = "fsl,ls1043a-scfg", },
  73. + { .compatible = "fsl,ls1046a-scfg", },
  74. + {}
  75. +};
  76. /*
  77. * sorted list of clock divider, register value pairs
  78. * taken from table 26-5, p.26-9, Freescale i.MX
  79. @@ -210,6 +263,12 @@ struct imx_i2c_struct {
  80. struct pinctrl_state *pinctrl_pins_gpio;
  81. struct imx_i2c_dma *dma;
  82. + int layerscape_bus_recover;
  83. + int gpio;
  84. + int need_set_pmuxcr;
  85. + int pmuxcr_set;
  86. + int pmuxcr_endian;
  87. + void __iomem *pmuxcr_addr;
  88. };
  89. static const struct imx_i2c_hwdata imx1_i2c_hwdata = {
  90. @@ -879,6 +938,78 @@ static int i2c_imx_read(struct imx_i2c_s
  91. return 0;
  92. }
  93. +/*
  94. + * Based on the I2C specification, if the data line (SDA) is
  95. + * stuck low, the master should send nine * clock pulses.
  96. + * The I2C slave device that held the bus low should release it
  97. + * sometime within * those nine clocks. Due to this erratum,
  98. + * the I2C controller cannot generate nine clock pulses.
  99. + */
  100. +static int i2c_imx_recovery_for_layerscape(struct imx_i2c_struct *i2c_imx)
  101. +{
  102. + u32 pmuxcr = 0;
  103. + int ret;
  104. + unsigned int i, temp;
  105. +
  106. + /* configure IICx_SCL/GPIO pin as a GPIO */
  107. + if (i2c_imx->need_set_pmuxcr == 1) {
  108. + pmuxcr = ioread32be(i2c_imx->pmuxcr_addr);
  109. + if (i2c_imx->pmuxcr_endian == BIG_ENDIAN)
  110. + iowrite32be(i2c_imx->pmuxcr_set|pmuxcr,
  111. + i2c_imx->pmuxcr_addr);
  112. + else
  113. + iowrite32(i2c_imx->pmuxcr_set|pmuxcr,
  114. + i2c_imx->pmuxcr_addr);
  115. + }
  116. +
  117. + ret = gpio_request(i2c_imx->gpio, i2c_imx->adapter.name);
  118. + if (ret) {
  119. + dev_err(&i2c_imx->adapter.dev,
  120. + "can't get gpio: %d\n", ret);
  121. + return ret;
  122. + }
  123. +
  124. + /* Configure GPIO pin as an output and open drain. */
  125. + gpio_direction_output(i2c_imx->gpio, 1);
  126. + udelay(10);
  127. +
  128. + /* Write data to generate 9 pulses */
  129. + for (i = 0; i < 9; i++) {
  130. + gpio_set_value(i2c_imx->gpio, 1);
  131. + udelay(10);
  132. + gpio_set_value(i2c_imx->gpio, 0);
  133. + udelay(10);
  134. + }
  135. + /* ensure that the last level sent is always high */
  136. + gpio_set_value(i2c_imx->gpio, 1);
  137. +
  138. + /*
  139. + * Set I2Cx_IBCR = 0h00 to generate a STOP and then
  140. + * set I2Cx_IBCR = 0h80 to reset
  141. + */
  142. + temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
  143. + temp &= ~(I2CR_MSTA | I2CR_MTX);
  144. + imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
  145. +
  146. + /* Restore the saved value of the register SCFG_RCWPMUXCR0 */
  147. + if (i2c_imx->need_set_pmuxcr == 1) {
  148. + if (i2c_imx->pmuxcr_endian == BIG_ENDIAN)
  149. + iowrite32be(pmuxcr, i2c_imx->pmuxcr_addr);
  150. + else
  151. + iowrite32(pmuxcr, i2c_imx->pmuxcr_addr);
  152. + }
  153. + /*
  154. + * Set I2C_IBSR[IBAL] to clear the IBAL bit if-
  155. + * I2C_IBSR[IBAL] = 1
  156. + */
  157. + temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2SR);
  158. + if (temp & I2SR_IAL) {
  159. + temp &= ~I2SR_IAL;
  160. + imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2SR);
  161. + }
  162. + return 0;
  163. +}
  164. +
  165. static int i2c_imx_xfer(struct i2c_adapter *adapter,
  166. struct i2c_msg *msgs, int num)
  167. {
  168. @@ -889,6 +1020,19 @@ static int i2c_imx_xfer(struct i2c_adapt
  169. dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__);
  170. + /*
  171. + * workround for ERR010027: ensure that the I2C BUS is idle
  172. + * before switching to master mode and attempting a Start cycle
  173. + */
  174. + result = i2c_imx_bus_busy(i2c_imx, 0);
  175. + if (result) {
  176. + /* timeout */
  177. + if ((result == -ETIMEDOUT) && (i2c_imx->layerscape_bus_recover == 1))
  178. + i2c_imx_recovery_for_layerscape(i2c_imx);
  179. + else
  180. + goto out;
  181. + }
  182. +
  183. result = pm_runtime_get_sync(i2c_imx->adapter.dev.parent);
  184. if (result < 0)
  185. goto out;
  186. @@ -1031,6 +1175,50 @@ static int i2c_imx_init_recovery_info(st
  187. return 0;
  188. }
  189. +/*
  190. + * switch SCL and SDA to their GPIO function and do some bitbanging
  191. + * for bus recovery.
  192. + * There are platforms such as Layerscape that don't support pinctrl, so add
  193. + * workaround for layerscape, it has no effect for other platforms.
  194. + */
  195. +static int i2c_imx_init_recovery_for_layerscape(
  196. + struct imx_i2c_struct *i2c_imx,
  197. + struct platform_device *pdev)
  198. +{
  199. + const struct of_device_id *of_id;
  200. + struct device_node *np = pdev->dev.of_node;
  201. + struct pinmux_cfg *pinmux_cfg;
  202. + struct device_node *scfg_node;
  203. + void __iomem *scfg_base = NULL;
  204. +
  205. + i2c_imx->gpio = of_get_named_gpio(np, "fsl-scl-gpio", 0);
  206. + if (!gpio_is_valid(i2c_imx->gpio)) {
  207. + dev_info(&pdev->dev, "fsl-scl-gpio not found\n");
  208. + return 0;
  209. + }
  210. + pinmux_cfg = devm_kzalloc(&pdev->dev, sizeof(*pinmux_cfg), GFP_KERNEL);
  211. + if (!pinmux_cfg)
  212. + return -ENOMEM;
  213. +
  214. + i2c_imx->need_set_pmuxcr = 0;
  215. + of_id = of_match_node(pinmux_of_match, np);
  216. + if (of_id) {
  217. + pinmux_cfg = (struct pinmux_cfg *)of_id->data;
  218. + i2c_imx->pmuxcr_endian = pinmux_cfg->endian;
  219. + i2c_imx->pmuxcr_set = pinmux_cfg->pmuxcr_set_bit;
  220. + scfg_node = of_find_matching_node(NULL, scfg_device_ids);
  221. + if (scfg_node) {
  222. + scfg_base = of_iomap(scfg_node, 0);
  223. + if (scfg_base) {
  224. + i2c_imx->pmuxcr_addr = scfg_base + pinmux_cfg->pmuxcr_offset;
  225. + i2c_imx->need_set_pmuxcr = 1;
  226. + }
  227. + }
  228. + }
  229. + i2c_imx->layerscape_bus_recover = 1;
  230. + return 0;
  231. +}
  232. +
  233. static u32 i2c_imx_func(struct i2c_adapter *adapter)
  234. {
  235. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL
  236. @@ -1086,6 +1274,11 @@ static int i2c_imx_probe(struct platform
  237. i2c_imx->adapter.dev.of_node = pdev->dev.of_node;
  238. i2c_imx->base = base;
  239. + /* Init optional bus recovery for layerscape */
  240. + ret = i2c_imx_init_recovery_for_layerscape(i2c_imx, pdev);
  241. + if (ret)
  242. + return ret;
  243. +
  244. /* Get I2C clock */
  245. i2c_imx->clk = devm_clk_get(&pdev->dev, NULL);
  246. if (IS_ERR(i2c_imx->clk)) {
  247. @@ -1100,7 +1293,7 @@ static int i2c_imx_probe(struct platform
  248. }
  249. /* Request IRQ */
  250. - ret = devm_request_irq(&pdev->dev, irq, i2c_imx_isr, 0,
  251. + ret = devm_request_irq(&pdev->dev, irq, i2c_imx_isr, IRQF_SHARED,
  252. pdev->name, i2c_imx);
  253. if (ret) {
  254. dev_err(&pdev->dev, "can't claim irq %d\n", irq);
  255. --- a/drivers/i2c/muxes/i2c-mux-pca954x.c
  256. +++ b/drivers/i2c/muxes/i2c-mux-pca954x.c
  257. @@ -74,6 +74,7 @@ struct pca954x {
  258. u8 last_chan; /* last register value */
  259. u8 deselect;
  260. struct i2c_client *client;
  261. + u8 disable_mux; /* do not disable mux if val not 0 */
  262. };
  263. /* Provide specs for the PCA954x types we know about */
  264. @@ -196,6 +197,13 @@ static int pca954x_deselect_mux(struct i
  265. if (!(data->deselect & (1 << chan)))
  266. return 0;
  267. +#ifdef CONFIG_ARCH_LAYERSCAPE
  268. + if (data->disable_mux != 0)
  269. + data->last_chan = data->chip->nchans;
  270. + else
  271. + data->last_chan = 0;
  272. + return pca954x_reg_write(muxc->parent, client, data->disable_mux);
  273. +#endif
  274. /* Deselect active channel */
  275. data->last_chan = 0;
  276. return pca954x_reg_write(muxc->parent, client, data->last_chan);
  277. @@ -228,6 +236,28 @@ static int pca954x_probe(struct i2c_clie
  278. return -ENOMEM;
  279. data = i2c_mux_priv(muxc);
  280. +#ifdef CONFIG_ARCH_LAYERSCAPE
  281. + /* The point here is that you must not disable a mux if there
  282. + * are no pullups on the input or you mess up the I2C. This
  283. + * needs to be put into the DTS really as the kernel cannot
  284. + * know this otherwise.
  285. + */
  286. + match = of_match_device(of_match_ptr(pca954x_of_match), &client->dev);
  287. + if (match)
  288. + data->chip = of_device_get_match_data(&client->dev);
  289. + else
  290. + data->chip = &chips[id->driver_data];
  291. +
  292. + data->disable_mux = of_node &&
  293. + of_property_read_bool(of_node, "i2c-mux-never-disable") &&
  294. + data->chip->muxtype == pca954x_ismux ?
  295. + data->chip->enable : 0;
  296. + /* force the first selection */
  297. + if (data->disable_mux != 0)
  298. + data->last_chan = data->chip->nchans;
  299. + else
  300. + data->last_chan = 0;
  301. +#endif
  302. i2c_set_clientdata(client, muxc);
  303. data->client = client;
  304. @@ -240,11 +270,16 @@ static int pca954x_probe(struct i2c_clie
  305. * that the mux is in fact present. This also
  306. * initializes the mux to disconnected state.
  307. */
  308. +#ifdef CONFIG_ARCH_LAYERSCAPE
  309. + if (i2c_smbus_write_byte(client, data->disable_mux) < 0) {
  310. +#else
  311. if (i2c_smbus_write_byte(client, 0) < 0) {
  312. +#endif
  313. dev_warn(&client->dev, "probe failed\n");
  314. return -ENODEV;
  315. }
  316. +#ifndef CONFIG_ARCH_LAYERSCAPE
  317. match = of_match_device(of_match_ptr(pca954x_of_match), &client->dev);
  318. if (match)
  319. data->chip = of_device_get_match_data(&client->dev);
  320. @@ -252,6 +287,7 @@ static int pca954x_probe(struct i2c_clie
  321. data->chip = &chips[id->driver_data];
  322. data->last_chan = 0; /* force the first selection */
  323. +#endif
  324. idle_disconnect_dt = of_node &&
  325. of_property_read_bool(of_node, "i2c-mux-idle-disconnect");
  326. @@ -312,6 +348,13 @@ static int pca954x_resume(struct device
  327. struct i2c_mux_core *muxc = i2c_get_clientdata(client);
  328. struct pca954x *data = i2c_mux_priv(muxc);
  329. +#ifdef CONFIG_ARCH_LAYERSCAPE
  330. + if (data->disable_mux != 0)
  331. + data->last_chan = data->chip->nchans;
  332. + else
  333. + data->last_chan = 0;
  334. + return i2c_smbus_write_byte(client, data->disable_mux);
  335. +#endif
  336. data->last_chan = 0;
  337. return i2c_smbus_write_byte(client, 0);
  338. }