i2c-mux-reg.c 6.5 KB

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