gpio-i2cmux.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * I2C multiplexer using GPIO API
  3. *
  4. * Peter Korsgaard <peter.korsgaard@barco.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/i2c.h>
  11. #include <linux/i2c-mux.h>
  12. #include <linux/gpio-i2cmux.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/init.h>
  15. #include <linux/module.h>
  16. #include <linux/slab.h>
  17. #include <linux/gpio.h>
  18. struct gpiomux {
  19. struct i2c_adapter *parent;
  20. struct i2c_adapter **adap; /* child busses */
  21. struct gpio_i2cmux_platform_data data;
  22. };
  23. static void gpiomux_set(const struct gpiomux *mux, unsigned val)
  24. {
  25. int i;
  26. for (i = 0; i < mux->data.n_gpios; i++)
  27. gpio_set_value(mux->data.gpios[i], val & (1 << i));
  28. }
  29. static int gpiomux_select(struct i2c_adapter *adap, void *data, u32 chan)
  30. {
  31. struct gpiomux *mux = data;
  32. gpiomux_set(mux, mux->data.values[chan]);
  33. return 0;
  34. }
  35. static int gpiomux_deselect(struct i2c_adapter *adap, void *data, u32 chan)
  36. {
  37. struct gpiomux *mux = data;
  38. gpiomux_set(mux, mux->data.idle);
  39. return 0;
  40. }
  41. static int __devinit gpiomux_probe(struct platform_device *pdev)
  42. {
  43. struct gpiomux *mux;
  44. struct gpio_i2cmux_platform_data *pdata;
  45. struct i2c_adapter *parent;
  46. int (*deselect) (struct i2c_adapter *, void *, u32);
  47. unsigned initial_state;
  48. int i, ret;
  49. pdata = pdev->dev.platform_data;
  50. if (!pdata) {
  51. dev_err(&pdev->dev, "Missing platform data\n");
  52. return -ENODEV;
  53. }
  54. parent = i2c_get_adapter(pdata->parent);
  55. if (!parent) {
  56. dev_err(&pdev->dev, "Parent adapter (%d) not found\n",
  57. pdata->parent);
  58. return -ENODEV;
  59. }
  60. mux = kzalloc(sizeof(*mux), GFP_KERNEL);
  61. if (!mux) {
  62. ret = -ENOMEM;
  63. goto alloc_failed;
  64. }
  65. mux->parent = parent;
  66. mux->data = *pdata;
  67. mux->adap = kzalloc(sizeof(struct i2c_adapter *) * pdata->n_values,
  68. GFP_KERNEL);
  69. if (!mux->adap) {
  70. ret = -ENOMEM;
  71. goto alloc_failed2;
  72. }
  73. if (pdata->idle != GPIO_I2CMUX_NO_IDLE) {
  74. initial_state = pdata->idle;
  75. deselect = gpiomux_deselect;
  76. } else {
  77. initial_state = pdata->values[0];
  78. deselect = NULL;
  79. }
  80. for (i = 0; i < pdata->n_gpios; i++) {
  81. ret = gpio_request(pdata->gpios[i], "gpio-i2cmux");
  82. if (ret)
  83. goto err_request_gpio;
  84. gpio_direction_output(pdata->gpios[i],
  85. initial_state & (1 << i));
  86. }
  87. for (i = 0; i < pdata->n_values; i++) {
  88. u32 nr = pdata->base_nr ? (pdata->base_nr + i) : 0;
  89. mux->adap[i] = i2c_add_mux_adapter(parent, mux, nr, i,
  90. gpiomux_select, deselect);
  91. if (!mux->adap[i]) {
  92. ret = -ENODEV;
  93. dev_err(&pdev->dev, "Failed to add adapter %d\n", i);
  94. goto add_adapter_failed;
  95. }
  96. }
  97. dev_info(&pdev->dev, "%d port mux on %s adapter\n",
  98. pdata->n_values, parent->name);
  99. platform_set_drvdata(pdev, mux);
  100. return 0;
  101. add_adapter_failed:
  102. for (; i > 0; i--)
  103. i2c_del_mux_adapter(mux->adap[i - 1]);
  104. i = pdata->n_gpios;
  105. err_request_gpio:
  106. for (; i > 0; i--)
  107. gpio_free(pdata->gpios[i - 1]);
  108. kfree(mux->adap);
  109. alloc_failed2:
  110. kfree(mux);
  111. alloc_failed:
  112. i2c_put_adapter(parent);
  113. return ret;
  114. }
  115. static int __devexit gpiomux_remove(struct platform_device *pdev)
  116. {
  117. struct gpiomux *mux = platform_get_drvdata(pdev);
  118. int i;
  119. for (i = 0; i < mux->data.n_values; i++)
  120. i2c_del_mux_adapter(mux->adap[i]);
  121. for (i = 0; i < mux->data.n_gpios; i++)
  122. gpio_free(mux->data.gpios[i]);
  123. platform_set_drvdata(pdev, NULL);
  124. i2c_put_adapter(mux->parent);
  125. kfree(mux->adap);
  126. kfree(mux);
  127. return 0;
  128. }
  129. static struct platform_driver gpiomux_driver = {
  130. .probe = gpiomux_probe,
  131. .remove = __devexit_p(gpiomux_remove),
  132. .driver = {
  133. .owner = THIS_MODULE,
  134. .name = "gpio-i2cmux",
  135. },
  136. };
  137. static int __init gpiomux_init(void)
  138. {
  139. return platform_driver_register(&gpiomux_driver);
  140. }
  141. static void __exit gpiomux_exit(void)
  142. {
  143. platform_driver_unregister(&gpiomux_driver);
  144. }
  145. module_init(gpiomux_init);
  146. module_exit(gpiomux_exit);
  147. MODULE_DESCRIPTION("GPIO-based I2C multiplexer driver");
  148. MODULE_AUTHOR("Peter Korsgaard <peter.korsgaard@barco.com>");
  149. MODULE_LICENSE("GPL");
  150. MODULE_ALIAS("platform:gpio-i2cmux");