i2c-mux-rtl9300.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * I2C multiplexer for the 2 I2C Masters of the RTL9300
  4. * with up to 8 channels each, but which are not entirely
  5. * independent of each other
  6. */
  7. #include <linux/i2c-mux.h>
  8. #include <linux/module.h>
  9. #include <linux/mux/consumer.h>
  10. #include <linux/of_device.h>
  11. #include <linux/of_address.h>
  12. #include <linux/platform_device.h>
  13. #include "../busses/i2c-rtl9300.h"
  14. #define NUM_MASTERS 2
  15. #define NUM_BUSSES 8
  16. #define REG(mst, x) (mux->base + x + (mst ? mux->i2c->mst2_offset : 0))
  17. #define REG_MASK(mst, clear, set, reg) \
  18. writel((readl(REG((mst),(reg))) & ~(clear)) | (set), REG((mst),(reg)))
  19. struct channel {
  20. u8 sda_num;
  21. u8 scl_num;
  22. };
  23. static struct channel channels[NUM_MASTERS * NUM_BUSSES];
  24. struct rtl9300_mux {
  25. void __iomem *base;
  26. struct device *dev;
  27. struct i2c_adapter *parent;
  28. struct rtl9300_i2c * i2c;
  29. };
  30. struct i2c_mux_data {
  31. int scl0_pin;
  32. int scl1_pin;
  33. int sda0_pin;
  34. int sda_pins;
  35. int (*i2c_mux_select)(struct i2c_mux_core *muxc, u32 chan);
  36. int (*i2c_mux_deselect)(struct i2c_mux_core *muxc, u32 chan);
  37. void (*sda_sel)(struct i2c_mux_core *muxc, int pin);
  38. };
  39. static int rtl9300_i2c_mux_select(struct i2c_mux_core *muxc, u32 chan)
  40. {
  41. struct rtl9300_mux *mux = i2c_mux_priv(muxc);
  42. // Set SCL pin
  43. REG_MASK(channels[chan].scl_num, 0,
  44. BIT(RTL9300_I2C_CTRL1_GPIO8_SCL_SEL), RTL9300_I2C_CTRL1);
  45. // Set SDA pin
  46. REG_MASK(channels[chan].scl_num, 0x7 << RTL9300_I2C_CTRL1_SDA_OUT_SEL,
  47. channels[chan].sda_num << RTL9300_I2C_CTRL1_SDA_OUT_SEL, RTL9300_I2C_CTRL1);
  48. mux->i2c->sda_num = channels[chan].sda_num;
  49. mux->i2c->scl_num = channels[chan].scl_num;
  50. return 0;
  51. }
  52. static int rtl9310_i2c_mux_select(struct i2c_mux_core *muxc, u32 chan)
  53. {
  54. struct rtl9300_mux *mux = i2c_mux_priv(muxc);
  55. // Set SCL pin
  56. REG_MASK(0, 0, BIT(RTL9310_I2C_MST_IF_SEL_GPIO_SCL_SEL + channels[chan].scl_num),
  57. RTL9310_I2C_MST_IF_SEL);
  58. // Set SDA pin
  59. REG_MASK(channels[chan].scl_num, 0xf << RTL9310_I2C_CTRL_SDA_OUT_SEL,
  60. channels[chan].sda_num << RTL9310_I2C_CTRL_SDA_OUT_SEL, RTL9310_I2C_CTRL);
  61. mux->i2c->sda_num = channels[chan].sda_num;
  62. mux->i2c->scl_num = channels[chan].scl_num;
  63. return 0;
  64. }
  65. static int rtl9300_i2c_mux_deselect(struct i2c_mux_core *muxc, u32 chan)
  66. {
  67. return 0;
  68. }
  69. static void rtl9300_sda_sel(struct i2c_mux_core *muxc, int pin)
  70. {
  71. struct rtl9300_mux *mux = i2c_mux_priv(muxc);
  72. u32 v;
  73. // Set SDA pin to I2C functionality
  74. v = readl(REG(0, RTL9300_I2C_MST_GLB_CTRL));
  75. v |= BIT(pin);
  76. writel(v, REG(0, RTL9300_I2C_MST_GLB_CTRL));
  77. }
  78. static void rtl9310_sda_sel(struct i2c_mux_core *muxc, int pin)
  79. {
  80. struct rtl9300_mux *mux = i2c_mux_priv(muxc);
  81. u32 v;
  82. // Set SDA pin to I2C functionality
  83. v = readl(REG(0, RTL9310_I2C_MST_IF_SEL));
  84. v |= BIT(pin);
  85. writel(v, REG(0, RTL9310_I2C_MST_IF_SEL));
  86. }
  87. static struct device_node *mux_parent_adapter(struct device *dev, struct rtl9300_mux *mux)
  88. {
  89. struct device_node *node = dev->of_node;
  90. struct device_node *parent_np;
  91. struct i2c_adapter *parent;
  92. parent_np = of_parse_phandle(node, "i2c-parent", 0);
  93. if (!parent_np) {
  94. dev_err(dev, "Cannot parse i2c-parent\n");
  95. return ERR_PTR(-ENODEV);
  96. }
  97. parent = of_find_i2c_adapter_by_node(parent_np);
  98. of_node_put(parent_np);
  99. if (!parent)
  100. return ERR_PTR(-EPROBE_DEFER);
  101. if (!(of_device_is_compatible(parent_np, "realtek,rtl9300-i2c")
  102. || of_device_is_compatible(parent_np, "realtek,rtl9310-i2c"))){
  103. dev_err(dev, "I2C parent not an RTL9300 I2C controller\n");
  104. return ERR_PTR(-ENODEV);
  105. }
  106. mux->parent = parent;
  107. mux->i2c = (struct rtl9300_i2c *)i2c_get_adapdata(parent);
  108. mux->base = mux->i2c->base;
  109. return parent_np;
  110. }
  111. struct i2c_mux_data rtl9300_i2c_mux_data = {
  112. .scl0_pin = 8,
  113. .scl1_pin = 17,
  114. .sda0_pin = 9,
  115. .sda_pins = 8,
  116. .i2c_mux_select = rtl9300_i2c_mux_select,
  117. .i2c_mux_deselect = rtl9300_i2c_mux_deselect,
  118. .sda_sel = rtl9300_sda_sel,
  119. };
  120. struct i2c_mux_data rtl9310_i2c_mux_data = {
  121. .scl0_pin = 13,
  122. .scl1_pin = 14,
  123. .sda0_pin = 0,
  124. .sda_pins = 16,
  125. .i2c_mux_select = rtl9310_i2c_mux_select,
  126. .i2c_mux_deselect = rtl9300_i2c_mux_deselect,
  127. .sda_sel = rtl9310_sda_sel,
  128. };
  129. static const struct of_device_id rtl9300_i2c_mux_of_match[] = {
  130. { .compatible = "realtek,i2c-mux-rtl9300", .data = (void *) &rtl9300_i2c_mux_data},
  131. { .compatible = "realtek,i2c-mux-rtl9310", .data = (void *) &rtl9310_i2c_mux_data},
  132. {},
  133. };
  134. MODULE_DEVICE_TABLE(of, rtl9300_i2c_mux_of_match);
  135. static int rtl9300_i2c_mux_probe(struct platform_device *pdev)
  136. {
  137. struct device *dev = &pdev->dev;
  138. struct device_node *node = dev->of_node;
  139. struct device_node *parent_np;
  140. struct device_node *child;
  141. struct i2c_mux_core *muxc;
  142. struct rtl9300_mux *mux;
  143. struct i2c_mux_data *mux_data;
  144. int children;
  145. int ret;
  146. pr_info("%s probing I2C adapter\n", __func__);
  147. if (!node) {
  148. dev_err(dev, "No DT found\n");
  149. return -EINVAL;
  150. }
  151. mux = devm_kzalloc(dev, sizeof(*mux), GFP_KERNEL);
  152. if (!mux)
  153. return -ENOMEM;
  154. mux->dev = dev;
  155. mux_data = (struct i2c_mux_data *) device_get_match_data(dev);
  156. parent_np = mux_parent_adapter(dev, mux);
  157. if (IS_ERR(parent_np))
  158. return dev_err_probe(dev, PTR_ERR(parent_np), "i2c-parent adapter not found\n");
  159. pr_info("%s base memory %08x\n", __func__, (u32)mux->base);
  160. children = of_get_child_count(node);
  161. muxc = i2c_mux_alloc(mux->parent, dev, children, 0, 0,
  162. mux_data->i2c_mux_select, mux_data->i2c_mux_deselect);
  163. if (!muxc) {
  164. ret = -ENOMEM;
  165. goto err_parent;
  166. }
  167. muxc->priv = mux;
  168. platform_set_drvdata(pdev, muxc);
  169. for_each_child_of_node(node, child) {
  170. u32 chan;
  171. u32 pin;
  172. ret = of_property_read_u32(child, "reg", &chan);
  173. if (ret < 0) {
  174. dev_err(dev, "no reg property for node '%pOFn'\n",
  175. child);
  176. goto err_children;
  177. }
  178. if (chan >= NUM_MASTERS * NUM_BUSSES) {
  179. dev_err(dev, "invalid reg %u\n", chan);
  180. ret = -EINVAL;
  181. goto err_children;
  182. }
  183. if (of_property_read_u32(child, "scl-pin", &pin)) {
  184. dev_warn(dev, "SCL pin not found in DT, using default\n");
  185. pin = mux_data->scl0_pin;
  186. }
  187. if (!(pin == mux_data->scl0_pin || pin == mux_data->scl1_pin)) {
  188. dev_warn(dev, "SCL pin %d not supported\n", pin);
  189. ret = -EINVAL;
  190. goto err_children;
  191. }
  192. channels[chan].scl_num = pin == mux_data->scl0_pin ? 0 : 1;
  193. pr_info("%s channel %d scl_num %d\n", __func__, chan, channels[chan].scl_num);
  194. if (of_property_read_u32(child, "sda-pin", &pin)) {
  195. dev_warn(dev, "SDA pin not found in DT, using default \n");
  196. pin = mux_data->sda0_pin;
  197. }
  198. channels[chan].sda_num = pin - mux_data->sda0_pin;
  199. if (channels[chan].sda_num < 0 || channels[chan].sda_num >= mux_data->sda_pins) {
  200. dev_warn(dev, "SDA pin %d not supported\n", pin);
  201. return -EINVAL;
  202. }
  203. pr_info("%s channel %d sda_num %d\n", __func__, chan, channels[chan].sda_num);
  204. mux_data->sda_sel(muxc, channels[chan].sda_num);
  205. ret = i2c_mux_add_adapter(muxc, 0, chan, 0);
  206. if (ret)
  207. goto err_children;
  208. }
  209. dev_info(dev, "%d-port mux on %s adapter\n", children, mux->parent->name);
  210. return 0;
  211. err_children:
  212. i2c_mux_del_adapters(muxc);
  213. err_parent:
  214. i2c_put_adapter(mux->parent);
  215. return ret;
  216. }
  217. static int rtl9300_i2c_mux_remove(struct platform_device *pdev)
  218. {
  219. struct i2c_mux_core *muxc = platform_get_drvdata(pdev);
  220. i2c_mux_del_adapters(muxc);
  221. i2c_put_adapter(muxc->parent);
  222. return 0;
  223. }
  224. static struct platform_driver i2c_mux_driver = {
  225. .probe = rtl9300_i2c_mux_probe,
  226. .remove = rtl9300_i2c_mux_remove,
  227. .driver = {
  228. .name = "i2c-mux-rtl9300",
  229. .of_match_table = rtl9300_i2c_mux_of_match,
  230. },
  231. };
  232. module_platform_driver(i2c_mux_driver);
  233. MODULE_DESCRIPTION("RTL9300 I2C multiplexer driver");
  234. MODULE_AUTHOR("Birger Koblitz");
  235. MODULE_LICENSE("GPL v2");