i2c-mux-reg.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * I2C multiplexer using a single register
  4. *
  5. * Copyright 2015 Freescale Semiconductor
  6. * York Sun <yorksun@freescale.com>
  7. */
  8. #include <linux/i2c.h>
  9. #include <linux/i2c-mux.h>
  10. #include <linux/init.h>
  11. #include <linux/io.h>
  12. #include <linux/module.h>
  13. #include <linux/of_address.h>
  14. #include <linux/platform_data/i2c-mux-reg.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/slab.h>
  17. struct regmux {
  18. struct i2c_mux_reg_platform_data data;
  19. };
  20. static int i2c_mux_reg_set(const struct regmux *mux, unsigned int chan_id)
  21. {
  22. if (!mux->data.reg)
  23. return -EINVAL;
  24. /*
  25. * Write to the register, followed by a read to ensure the write is
  26. * completed on a "posted" bus, for example PCI or write buffers.
  27. * The endianness of reading doesn't matter and the return data
  28. * is not used.
  29. */
  30. switch (mux->data.reg_size) {
  31. case 4:
  32. if (mux->data.little_endian)
  33. iowrite32(chan_id, mux->data.reg);
  34. else
  35. iowrite32be(chan_id, mux->data.reg);
  36. if (!mux->data.write_only)
  37. ioread32(mux->data.reg);
  38. break;
  39. case 2:
  40. if (mux->data.little_endian)
  41. iowrite16(chan_id, mux->data.reg);
  42. else
  43. iowrite16be(chan_id, mux->data.reg);
  44. if (!mux->data.write_only)
  45. ioread16(mux->data.reg);
  46. break;
  47. case 1:
  48. iowrite8(chan_id, mux->data.reg);
  49. if (!mux->data.write_only)
  50. ioread8(mux->data.reg);
  51. break;
  52. }
  53. return 0;
  54. }
  55. static int i2c_mux_reg_select(struct i2c_mux_core *muxc, u32 chan)
  56. {
  57. struct regmux *mux = i2c_mux_priv(muxc);
  58. return i2c_mux_reg_set(mux, chan);
  59. }
  60. static int i2c_mux_reg_deselect(struct i2c_mux_core *muxc, u32 chan)
  61. {
  62. struct regmux *mux = i2c_mux_priv(muxc);
  63. if (mux->data.idle_in_use)
  64. return i2c_mux_reg_set(mux, mux->data.idle);
  65. return 0;
  66. }
  67. #ifdef CONFIG_OF
  68. static int i2c_mux_reg_probe_dt(struct regmux *mux,
  69. struct platform_device *pdev)
  70. {
  71. struct device_node *np = pdev->dev.of_node;
  72. struct device_node *adapter_np, *child;
  73. struct i2c_adapter *adapter;
  74. struct resource res;
  75. unsigned *values;
  76. int i = 0;
  77. if (!np)
  78. return -ENODEV;
  79. adapter_np = of_parse_phandle(np, "i2c-parent", 0);
  80. if (!adapter_np) {
  81. dev_err(&pdev->dev, "Cannot parse i2c-parent\n");
  82. return -ENODEV;
  83. }
  84. adapter = of_find_i2c_adapter_by_node(adapter_np);
  85. of_node_put(adapter_np);
  86. if (!adapter)
  87. return -EPROBE_DEFER;
  88. mux->data.parent = i2c_adapter_id(adapter);
  89. put_device(&adapter->dev);
  90. mux->data.n_values = of_get_child_count(np);
  91. if (of_property_read_bool(np, "little-endian")) {
  92. mux->data.little_endian = true;
  93. } else if (of_property_read_bool(np, "big-endian")) {
  94. mux->data.little_endian = false;
  95. } else {
  96. #if defined(__BYTE_ORDER) ? __BYTE_ORDER == __LITTLE_ENDIAN : \
  97. defined(__LITTLE_ENDIAN)
  98. mux->data.little_endian = true;
  99. #elif defined(__BYTE_ORDER) ? __BYTE_ORDER == __BIG_ENDIAN : \
  100. defined(__BIG_ENDIAN)
  101. mux->data.little_endian = false;
  102. #else
  103. #error Endianness not defined?
  104. #endif
  105. }
  106. mux->data.write_only = of_property_read_bool(np, "write-only");
  107. values = devm_kcalloc(&pdev->dev,
  108. mux->data.n_values, sizeof(*mux->data.values),
  109. GFP_KERNEL);
  110. if (!values)
  111. return -ENOMEM;
  112. for_each_child_of_node(np, child) {
  113. of_property_read_u32(child, "reg", values + i);
  114. i++;
  115. }
  116. mux->data.values = values;
  117. if (!of_property_read_u32(np, "idle-state", &mux->data.idle))
  118. mux->data.idle_in_use = true;
  119. /* map address from "reg" if exists */
  120. if (of_address_to_resource(np, 0, &res) == 0) {
  121. mux->data.reg_size = resource_size(&res);
  122. mux->data.reg = devm_ioremap_resource(&pdev->dev, &res);
  123. if (IS_ERR(mux->data.reg))
  124. return PTR_ERR(mux->data.reg);
  125. }
  126. return 0;
  127. }
  128. #else
  129. static int i2c_mux_reg_probe_dt(struct regmux *mux,
  130. struct platform_device *pdev)
  131. {
  132. return 0;
  133. }
  134. #endif
  135. static int i2c_mux_reg_probe(struct platform_device *pdev)
  136. {
  137. struct i2c_mux_core *muxc;
  138. struct regmux *mux;
  139. struct i2c_adapter *parent;
  140. struct resource *res;
  141. int i, ret, nr;
  142. mux = devm_kzalloc(&pdev->dev, sizeof(*mux), GFP_KERNEL);
  143. if (!mux)
  144. return -ENOMEM;
  145. if (dev_get_platdata(&pdev->dev)) {
  146. memcpy(&mux->data, dev_get_platdata(&pdev->dev),
  147. sizeof(mux->data));
  148. } else {
  149. ret = i2c_mux_reg_probe_dt(mux, pdev);
  150. if (ret < 0)
  151. return dev_err_probe(&pdev->dev, ret,
  152. "Error parsing device tree");
  153. }
  154. parent = i2c_get_adapter(mux->data.parent);
  155. if (!parent)
  156. return -EPROBE_DEFER;
  157. if (!mux->data.reg) {
  158. dev_info(&pdev->dev,
  159. "Register not set, using platform resource\n");
  160. mux->data.reg = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
  161. if (IS_ERR(mux->data.reg)) {
  162. ret = PTR_ERR(mux->data.reg);
  163. goto err_put_parent;
  164. }
  165. mux->data.reg_size = resource_size(res);
  166. }
  167. if (mux->data.reg_size != 4 && mux->data.reg_size != 2 &&
  168. mux->data.reg_size != 1) {
  169. dev_err(&pdev->dev, "Invalid register size\n");
  170. ret = -EINVAL;
  171. goto err_put_parent;
  172. }
  173. muxc = i2c_mux_alloc(parent, &pdev->dev, mux->data.n_values, 0, 0,
  174. i2c_mux_reg_select, NULL);
  175. if (!muxc) {
  176. ret = -ENOMEM;
  177. goto err_put_parent;
  178. }
  179. muxc->priv = mux;
  180. platform_set_drvdata(pdev, muxc);
  181. if (mux->data.idle_in_use)
  182. muxc->deselect = i2c_mux_reg_deselect;
  183. for (i = 0; i < mux->data.n_values; i++) {
  184. nr = mux->data.base_nr ? (mux->data.base_nr + i) : 0;
  185. ret = i2c_mux_add_adapter(muxc, nr, mux->data.values[i]);
  186. if (ret)
  187. goto err_del_mux_adapters;
  188. }
  189. dev_dbg(&pdev->dev, "%d port mux on %s adapter\n",
  190. mux->data.n_values, muxc->parent->name);
  191. return 0;
  192. err_del_mux_adapters:
  193. i2c_mux_del_adapters(muxc);
  194. err_put_parent:
  195. i2c_put_adapter(parent);
  196. return ret;
  197. }
  198. static void i2c_mux_reg_remove(struct platform_device *pdev)
  199. {
  200. struct i2c_mux_core *muxc = platform_get_drvdata(pdev);
  201. i2c_mux_del_adapters(muxc);
  202. i2c_put_adapter(muxc->parent);
  203. }
  204. static const struct of_device_id i2c_mux_reg_of_match[] = {
  205. { .compatible = "i2c-mux-reg", },
  206. {},
  207. };
  208. MODULE_DEVICE_TABLE(of, i2c_mux_reg_of_match);
  209. static struct platform_driver i2c_mux_reg_driver = {
  210. .probe = i2c_mux_reg_probe,
  211. .remove_new = i2c_mux_reg_remove,
  212. .driver = {
  213. .name = "i2c-mux-reg",
  214. .of_match_table = of_match_ptr(i2c_mux_reg_of_match),
  215. },
  216. };
  217. module_platform_driver(i2c_mux_reg_driver);
  218. MODULE_DESCRIPTION("Register-based I2C multiplexer driver");
  219. MODULE_AUTHOR("York Sun <yorksun@freescale.com>");
  220. MODULE_LICENSE("GPL");
  221. MODULE_ALIAS("platform:i2c-mux-reg");