i2c-mux-reg.c 6.9 KB

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