i2c-mux-reg.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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_find_property(np, "little-endian", NULL)) {
  96. mux->data.little_endian = true;
  97. } else if (of_find_property(np, "big-endian", NULL)) {
  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. if (of_find_property(np, "write-only", NULL))
  111. mux->data.write_only = true;
  112. else
  113. mux->data.write_only = false;
  114. values = devm_kzalloc(&pdev->dev,
  115. sizeof(*mux->data.values) * mux->data.n_values,
  116. GFP_KERNEL);
  117. if (!values) {
  118. dev_err(&pdev->dev, "Cannot allocate values array");
  119. return -ENOMEM;
  120. }
  121. for_each_child_of_node(np, child) {
  122. of_property_read_u32(child, "reg", values + i);
  123. i++;
  124. }
  125. mux->data.values = values;
  126. if (!of_property_read_u32(np, "idle-state", &mux->data.idle))
  127. mux->data.idle_in_use = true;
  128. /* map address from "reg" if exists */
  129. if (of_address_to_resource(np, 0, &res) == 0) {
  130. mux->data.reg_size = resource_size(&res);
  131. mux->data.reg = devm_ioremap_resource(&pdev->dev, &res);
  132. if (IS_ERR(mux->data.reg))
  133. return PTR_ERR(mux->data.reg);
  134. }
  135. return 0;
  136. }
  137. #else
  138. static int i2c_mux_reg_probe_dt(struct regmux *mux,
  139. struct platform_device *pdev)
  140. {
  141. return 0;
  142. }
  143. #endif
  144. static int i2c_mux_reg_probe(struct platform_device *pdev)
  145. {
  146. struct i2c_mux_core *muxc;
  147. struct regmux *mux;
  148. struct i2c_adapter *parent;
  149. struct resource *res;
  150. unsigned int class;
  151. int i, ret, nr;
  152. mux = devm_kzalloc(&pdev->dev, sizeof(*mux), GFP_KERNEL);
  153. if (!mux)
  154. return -ENOMEM;
  155. if (dev_get_platdata(&pdev->dev)) {
  156. memcpy(&mux->data, dev_get_platdata(&pdev->dev),
  157. sizeof(mux->data));
  158. } else {
  159. ret = i2c_mux_reg_probe_dt(mux, pdev);
  160. if (ret < 0) {
  161. dev_err(&pdev->dev, "Error parsing device tree");
  162. return ret;
  163. }
  164. }
  165. parent = i2c_get_adapter(mux->data.parent);
  166. if (!parent)
  167. return -EPROBE_DEFER;
  168. if (!mux->data.reg) {
  169. dev_info(&pdev->dev,
  170. "Register not set, using platform resource\n");
  171. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  172. mux->data.reg_size = resource_size(res);
  173. mux->data.reg = devm_ioremap_resource(&pdev->dev, res);
  174. if (IS_ERR(mux->data.reg)) {
  175. ret = PTR_ERR(mux->data.reg);
  176. goto err_put_parent;
  177. }
  178. }
  179. if (mux->data.reg_size != 4 && mux->data.reg_size != 2 &&
  180. mux->data.reg_size != 1) {
  181. dev_err(&pdev->dev, "Invalid register size\n");
  182. ret = -EINVAL;
  183. goto err_put_parent;
  184. }
  185. muxc = i2c_mux_alloc(parent, &pdev->dev, mux->data.n_values, 0, 0,
  186. i2c_mux_reg_select, NULL);
  187. if (!muxc) {
  188. ret = -ENOMEM;
  189. goto err_put_parent;
  190. }
  191. muxc->priv = mux;
  192. platform_set_drvdata(pdev, muxc);
  193. if (mux->data.idle_in_use)
  194. muxc->deselect = i2c_mux_reg_deselect;
  195. for (i = 0; i < mux->data.n_values; i++) {
  196. nr = mux->data.base_nr ? (mux->data.base_nr + i) : 0;
  197. class = mux->data.classes ? mux->data.classes[i] : 0;
  198. ret = i2c_mux_add_adapter(muxc, nr, mux->data.values[i], class);
  199. if (ret)
  200. goto err_del_mux_adapters;
  201. }
  202. dev_dbg(&pdev->dev, "%d port mux on %s adapter\n",
  203. mux->data.n_values, muxc->parent->name);
  204. return 0;
  205. err_del_mux_adapters:
  206. i2c_mux_del_adapters(muxc);
  207. err_put_parent:
  208. i2c_put_adapter(parent);
  209. return ret;
  210. }
  211. static int i2c_mux_reg_remove(struct platform_device *pdev)
  212. {
  213. struct i2c_mux_core *muxc = platform_get_drvdata(pdev);
  214. i2c_mux_del_adapters(muxc);
  215. i2c_put_adapter(muxc->parent);
  216. return 0;
  217. }
  218. static const struct of_device_id i2c_mux_reg_of_match[] = {
  219. { .compatible = "i2c-mux-reg", },
  220. {},
  221. };
  222. MODULE_DEVICE_TABLE(of, i2c_mux_reg_of_match);
  223. static struct platform_driver i2c_mux_reg_driver = {
  224. .probe = i2c_mux_reg_probe,
  225. .remove = i2c_mux_reg_remove,
  226. .driver = {
  227. .name = "i2c-mux-reg",
  228. .of_match_table = of_match_ptr(i2c_mux_reg_of_match),
  229. },
  230. };
  231. module_platform_driver(i2c_mux_reg_driver);
  232. MODULE_DESCRIPTION("Register-based I2C multiplexer driver");
  233. MODULE_AUTHOR("York Sun <yorksun@freescale.com>");
  234. MODULE_LICENSE("GPL");
  235. MODULE_ALIAS("platform:i2c-mux-reg");